2 * sidebar.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Sidebar related code for the Symbol list and Open files GtkTreeViews.
33 #include "callbacks.h" /* FIXME: for ignore_callback */
34 #include "documentprivate.h"
35 #include "filetypesprivate.h"
36 #include "geanyobject.h"
47 #include <gdk/gdkkeysyms.h>
50 SidebarTreeviews tv
= {NULL
, NULL
, NULL
};
51 /* while typeahead searching, editor should not get focus */
52 static gboolean may_steal_focus
= FALSE
;
59 GtkWidget
*show_paths
;
60 GtkWidget
*find_in_files
;
61 GtkWidget
*expand_all
;
62 GtkWidget
*collapse_all
;
64 doc_items
= {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
74 OPENFILES_ACTION_REMOVE
= 0,
75 OPENFILES_ACTION_SAVE
,
76 OPENFILES_ACTION_RELOAD
79 /* documents tree model columns */
83 DOCUMENTS_SHORTNAME
, /* dirname for parents, basename for children */
86 DOCUMENTS_FILENAME
/* full filename */
89 static GtkTreeStore
*store_openfiles
;
90 static GtkWidget
*openfiles_popup_menu
;
91 static gboolean documents_show_paths
;
92 static GtkWidget
*tag_window
; /* scrolled window that holds the symbol list GtkTreeView */
94 /* callback prototypes */
95 static void on_openfiles_document_action(GtkMenuItem
*menuitem
, gpointer user_data
);
96 static gboolean
sidebar_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
98 static gboolean
sidebar_key_press_cb(GtkWidget
*widget
, GdkEventKey
*event
,
100 static void on_list_document_activate(GtkCheckMenuItem
*item
, gpointer user_data
);
101 static void on_list_symbol_activate(GtkCheckMenuItem
*item
, gpointer user_data
);
102 static void documents_menu_update(GtkTreeSelection
*selection
);
103 static void sidebar_tabs_show_hide(GtkNotebook
*notebook
, GtkWidget
*child
,
104 guint page_num
, gpointer data
);
107 /* the prepare_* functions are document-related, but I think they fit better here than in document.c */
108 static void prepare_taglist(GtkWidget
*tree
, GtkTreeStore
*store
)
110 GtkCellRenderer
*text_renderer
, *icon_renderer
;
111 GtkTreeViewColumn
*column
;
112 GtkTreeSelection
*selection
;
114 text_renderer
= gtk_cell_renderer_text_new();
115 icon_renderer
= gtk_cell_renderer_pixbuf_new();
116 column
= gtk_tree_view_column_new();
118 gtk_tree_view_column_pack_start(column
, icon_renderer
, FALSE
);
119 gtk_tree_view_column_set_attributes(column
, icon_renderer
, "pixbuf", SYMBOLS_COLUMN_ICON
, NULL
);
120 g_object_set(icon_renderer
, "xalign", 0.0, NULL
);
122 gtk_tree_view_column_pack_start(column
, text_renderer
, TRUE
);
123 gtk_tree_view_column_set_attributes(column
, text_renderer
, "text", SYMBOLS_COLUMN_NAME
, NULL
);
124 g_object_set(text_renderer
, "yalign", 0.5, NULL
);
125 gtk_tree_view_column_set_title(column
, _("Symbols"));
127 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
128 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
130 ui_widget_modify_font_from_string(tree
, interface_prefs
.tagbar_font
);
132 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), GTK_TREE_MODEL(store
));
133 g_object_unref(store
);
135 g_signal_connect(tree
, "button-press-event",
136 G_CALLBACK(sidebar_button_press_cb
), NULL
);
137 g_signal_connect(tree
, "key-press-event",
138 G_CALLBACK(sidebar_key_press_cb
), NULL
);
140 gtk_tree_view_set_show_expanders(GTK_TREE_VIEW(tree
), interface_prefs
.show_symbol_list_expanders
);
141 if (! interface_prefs
.show_symbol_list_expanders
)
142 gtk_tree_view_set_level_indentation(GTK_TREE_VIEW(tree
), 10);
144 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(tree
), SYMBOLS_COLUMN_TOOLTIP
);
146 /* selection handling */
147 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
148 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
149 /* callback for changed selection not necessary, will be handled by button-press-event */
154 on_default_tag_tree_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
157 if (event
->button
== 3)
159 gtk_menu_popup(GTK_MENU(tv
.popup_taglist
), NULL
, NULL
, NULL
, NULL
,
160 event
->button
, event
->time
);
167 static void create_default_tag_tree(void)
169 GtkScrolledWindow
*scrolled_window
= GTK_SCROLLED_WINDOW(tag_window
);
172 /* default_tag_tree is a GtkViewPort with a GtkLabel inside it */
173 tv
.default_tag_tree
= gtk_viewport_new(
174 gtk_scrolled_window_get_hadjustment(scrolled_window
),
175 gtk_scrolled_window_get_vadjustment(scrolled_window
));
176 label
= gtk_label_new(_("No tags found"));
177 gtk_misc_set_alignment(GTK_MISC(label
), 0.1f
, 0.01f
);
178 gtk_container_add(GTK_CONTAINER(tv
.default_tag_tree
), label
);
179 gtk_widget_show_all(tv
.default_tag_tree
);
180 g_signal_connect(tv
.default_tag_tree
, "button-press-event",
181 G_CALLBACK(on_default_tag_tree_button_press_event
), NULL
);
182 g_object_ref((gpointer
)tv
.default_tag_tree
); /* to hold it after removing */
186 /* update = rescan the tags for doc->filename */
187 void sidebar_update_tag_list(GeanyDocument
*doc
, gboolean update
)
189 GtkWidget
*child
= gtk_bin_get_child(GTK_BIN(tag_window
));
191 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
193 /* changes the tree view to the given one, trying not to do useless changes */
194 #define CHANGE_TREE(new_child) \
196 /* only change the tag tree if it's actually not the same (to avoid flickering) and if
197 * it's the one of the current document (to avoid problems when e.g. reloading
198 * configuration files */ \
199 if (child != new_child && doc == document_get_current()) \
202 gtk_container_remove(GTK_CONTAINER(tag_window), child); \
203 gtk_container_add(GTK_CONTAINER(tag_window), new_child); \
207 if (tv
.default_tag_tree
== NULL
)
208 create_default_tag_tree();
210 /* show default empty tag tree if there are no tags */
211 if (doc
== NULL
|| doc
->file_type
== NULL
|| ! filetype_has_tags(doc
->file_type
))
213 CHANGE_TREE(tv
.default_tag_tree
);
218 { /* updating the tag list in the left tag window */
219 if (doc
->priv
->tag_tree
== NULL
)
221 doc
->priv
->tag_store
= gtk_tree_store_new(
222 SYMBOLS_N_COLUMNS
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
, TM_TYPE_TAG
, G_TYPE_STRING
);
223 doc
->priv
->tag_tree
= gtk_tree_view_new();
224 prepare_taglist(doc
->priv
->tag_tree
, doc
->priv
->tag_store
);
225 gtk_widget_show(doc
->priv
->tag_tree
);
226 g_object_ref((gpointer
)doc
->priv
->tag_tree
); /* to hold it after removing */
229 doc
->has_tags
= symbols_recreate_tag_list(doc
, SYMBOLS_SORT_USE_PREVIOUS
);
234 CHANGE_TREE(doc
->priv
->tag_tree
);
238 CHANGE_TREE(tv
.default_tag_tree
);
245 /* cleverly sorts documents by their short name */
246 static gint
documents_sort_func(GtkTreeModel
*model
, GtkTreeIter
*iter_a
,
247 GtkTreeIter
*iter_b
, gpointer data
)
249 gchar
*key_a
, *key_b
;
250 gchar
*name_a
, *name_b
;
253 gtk_tree_model_get(model
, iter_a
, DOCUMENTS_SHORTNAME
, &name_a
, -1);
254 key_a
= g_utf8_collate_key_for_filename(name_a
, -1);
256 gtk_tree_model_get(model
, iter_b
, DOCUMENTS_SHORTNAME
, &name_b
, -1);
257 key_b
= g_utf8_collate_key_for_filename(name_b
, -1);
259 cmp
= strcmp(key_a
, key_b
);
267 /* does some preparing things to the open files list widget */
268 static void prepare_openfiles(void)
270 GtkCellRenderer
*icon_renderer
;
271 GtkCellRenderer
*text_renderer
;
272 GtkTreeViewColumn
*column
;
273 GtkTreeSelection
*selection
;
274 GtkTreeSortable
*sortable
;
276 tv
.tree_openfiles
= ui_lookup_widget(main_widgets
.window
, "treeview6");
278 /* store the icon and the short filename to show, and the index as reference,
279 * the colour (black/red/green) and the full name for the tooltip */
280 store_openfiles
= gtk_tree_store_new(5, G_TYPE_ICON
, G_TYPE_STRING
,
281 G_TYPE_POINTER
, GDK_TYPE_COLOR
, G_TYPE_STRING
);
282 gtk_tree_view_set_model(GTK_TREE_VIEW(tv
.tree_openfiles
), GTK_TREE_MODEL(store_openfiles
));
284 /* set policy settings for the scolledwindow around the treeview again, because glade
285 * doesn't keep the settings */
286 gtk_scrolled_window_set_policy(
287 GTK_SCROLLED_WINDOW(ui_lookup_widget(main_widgets
.window
, "scrolledwindow7")),
288 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
290 icon_renderer
= gtk_cell_renderer_pixbuf_new();
291 g_object_set(icon_renderer
, "stock-size", GTK_ICON_SIZE_MENU
, NULL
);
292 text_renderer
= gtk_cell_renderer_text_new();
293 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_MIDDLE
, NULL
);
294 column
= gtk_tree_view_column_new();
295 gtk_tree_view_column_pack_start(column
, icon_renderer
, FALSE
);
296 gtk_tree_view_column_set_attributes(column
, icon_renderer
, "gicon", DOCUMENTS_ICON
, NULL
);
297 gtk_tree_view_column_pack_start(column
, text_renderer
, TRUE
);
298 gtk_tree_view_column_set_attributes(column
, text_renderer
, "text", DOCUMENTS_SHORTNAME
,
299 "foreground-gdk", DOCUMENTS_COLOR
, NULL
);
300 gtk_tree_view_append_column(GTK_TREE_VIEW(tv
.tree_openfiles
), column
);
301 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv
.tree_openfiles
), FALSE
);
303 gtk_tree_view_set_search_column(GTK_TREE_VIEW(tv
.tree_openfiles
),
304 DOCUMENTS_SHORTNAME
);
306 /* sort opened filenames in the store_openfiles treeview */
307 sortable
= GTK_TREE_SORTABLE(GTK_TREE_MODEL(store_openfiles
));
308 gtk_tree_sortable_set_sort_func(sortable
, DOCUMENTS_SHORTNAME
, documents_sort_func
, NULL
, NULL
);
309 gtk_tree_sortable_set_sort_column_id(sortable
, DOCUMENTS_SHORTNAME
, GTK_SORT_ASCENDING
);
311 ui_widget_modify_font_from_string(tv
.tree_openfiles
, interface_prefs
.tagbar_font
);
314 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(tv
.tree_openfiles
), DOCUMENTS_FILENAME
);
316 /* selection handling */
317 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
318 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
319 g_object_unref(store_openfiles
);
321 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "button-press-event",
322 G_CALLBACK(sidebar_button_press_cb
), NULL
);
323 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "key-press-event",
324 G_CALLBACK(sidebar_key_press_cb
), NULL
);
328 /* iter should be toplevel */
329 static gboolean
find_tree_iter_dir(GtkTreeIter
*iter
, const gchar
*dir
)
335 if (utils_str_equal(dir
, "."))
336 dir
= GEANY_STRING_UNTITLED
;
338 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
339 g_return_val_if_fail(!doc
, FALSE
);
341 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_SHORTNAME
, &name
, -1);
343 result
= utils_filenamecmp(name
, dir
) == 0;
350 static gboolean
utils_filename_has_prefix(const gchar
*str
, const gchar
*prefix
)
352 gchar
*head
= g_strndup(str
, strlen(prefix
));
353 gboolean ret
= utils_filenamecmp(head
, prefix
) == 0;
360 static gchar
*get_doc_folder(const gchar
*path
)
362 gchar
*tmp_dirname
= g_strdup(path
);
363 gchar
*project_base_path
;
364 gchar
*dirname
= NULL
;
365 const gchar
*home_dir
= g_get_home_dir();
368 /* replace the project base path with the project name */
369 project_base_path
= project_get_base_path();
371 if (project_base_path
!= NULL
)
373 gsize len
= strlen(project_base_path
);
375 /* remove trailing separator so we can match base path exactly */
376 if (project_base_path
[len
-1] == G_DIR_SEPARATOR
)
377 project_base_path
[--len
] = '\0';
379 /* check whether the dir name matches or uses the project base path */
380 if (utils_filename_has_prefix(tmp_dirname
, project_base_path
))
382 rest
= tmp_dirname
+ len
;
383 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
385 dirname
= g_strdup_printf("%s%s", app
->project
->name
, rest
);
388 g_free(project_base_path
);
392 dirname
= tmp_dirname
;
394 /* If matches home dir, replace with tilde */
395 if (!EMPTY(home_dir
) && utils_filename_has_prefix(dirname
, home_dir
))
397 rest
= dirname
+ strlen(home_dir
);
398 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
400 dirname
= g_strdup_printf("~%s", rest
);
412 static GtkTreeIter
*get_doc_parent(GeanyDocument
*doc
)
415 gchar
*dirname
= NULL
;
416 static GtkTreeIter parent
;
417 GtkTreeModel
*model
= GTK_TREE_MODEL(store_openfiles
);
418 static GIcon
*dir_icon
= NULL
;
420 if (!documents_show_paths
)
423 path
= g_path_get_dirname(DOC_FILENAME(doc
));
424 dirname
= get_doc_folder(path
);
426 if (gtk_tree_model_get_iter_first(model
, &parent
))
430 if (find_tree_iter_dir(&parent
, dirname
))
437 while (gtk_tree_model_iter_next(model
, &parent
));
439 /* no match, add dir parent */
441 dir_icon
= ui_get_mime_icon("inode/directory");
443 gtk_tree_store_append(store_openfiles
, &parent
, NULL
);
444 gtk_tree_store_set(store_openfiles
, &parent
, DOCUMENTS_ICON
, dir_icon
,
445 DOCUMENTS_FILENAME
, path
,
446 DOCUMENTS_SHORTNAME
, doc
->file_name
? dirname
: GEANY_STRING_UNTITLED
, -1);
454 /* Also sets doc->priv->iter.
455 * This is called recursively in sidebar_openfiles_update_all(). */
456 void sidebar_openfiles_add(GeanyDocument
*doc
)
458 GtkTreeIter
*iter
= &doc
->priv
->iter
;
459 GtkTreeIter
*parent
= get_doc_parent(doc
);
461 const GdkColor
*color
= document_get_status_color(doc
);
462 static GIcon
*file_icon
= NULL
;
464 gtk_tree_store_append(store_openfiles
, iter
, parent
);
466 /* check if new parent */
467 if (parent
&& gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), parent
) == 1)
472 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), parent
);
473 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
474 gtk_tree_path_free(path
);
477 file_icon
= ui_get_mime_icon("text/plain");
479 basename
= g_path_get_basename(DOC_FILENAME(doc
));
480 gtk_tree_store_set(store_openfiles
, iter
,
481 DOCUMENTS_ICON
, (doc
->file_type
&& doc
->file_type
->icon
) ? doc
->file_type
->icon
: file_icon
,
482 DOCUMENTS_SHORTNAME
, basename
, DOCUMENTS_DOCUMENT
, doc
, DOCUMENTS_COLOR
, color
,
483 DOCUMENTS_FILENAME
, DOC_FILENAME(doc
), -1);
488 static void openfiles_remove(GeanyDocument
*doc
)
490 GtkTreeIter
*iter
= &doc
->priv
->iter
;
493 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
) &&
494 gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), &parent
) == 1)
495 gtk_tree_store_remove(store_openfiles
, &parent
);
497 gtk_tree_store_remove(store_openfiles
, iter
);
501 void sidebar_openfiles_update(GeanyDocument
*doc
)
503 GtkTreeIter
*iter
= &doc
->priv
->iter
;
506 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_FILENAME
, &fname
, -1);
508 if (utils_str_equal(fname
, DOC_FILENAME(doc
)))
510 /* just update color and the icon */
511 const GdkColor
*color
= document_get_status_color(doc
);
512 GIcon
*icon
= doc
->file_type
->icon
;
514 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_COLOR
, color
, -1);
516 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_ICON
, icon
, -1);
520 /* path has changed, so remove and re-add */
521 GtkTreeSelection
*treesel
;
524 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
525 sel
= gtk_tree_selection_iter_is_selected(treesel
, &doc
->priv
->iter
);
526 openfiles_remove(doc
);
528 sidebar_openfiles_add(doc
);
530 gtk_tree_selection_select_iter(treesel
, &doc
->priv
->iter
);
536 void sidebar_openfiles_update_all(void)
540 gtk_tree_store_clear(store_openfiles
);
543 sidebar_openfiles_add(documents
[i
]);
548 void sidebar_remove_document(GeanyDocument
*doc
)
550 openfiles_remove(doc
);
552 if (GTK_IS_WIDGET(doc
->priv
->tag_tree
))
554 gtk_widget_destroy(doc
->priv
->tag_tree
); /* make GTK release its references, if any */
555 /* Because it was ref'd in sidebar_update_tag_list, it needs unref'ing */
556 g_object_unref(doc
->priv
->tag_tree
);
557 doc
->priv
->tag_tree
= NULL
;
562 static void on_hide_sidebar(void)
564 ui_prefs
.sidebar_visible
= FALSE
;
565 ui_sidebar_show_hide();
569 static gboolean
on_sidebar_display_symbol_list_show(GtkWidget
*item
)
571 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
572 interface_prefs
.sidebar_symbol_visible
);
577 static gboolean
on_sidebar_display_open_files_show(GtkWidget
*item
)
579 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
580 interface_prefs
.sidebar_openfiles_visible
);
585 void sidebar_add_common_menu_items(GtkMenu
*menu
)
589 item
= gtk_separator_menu_item_new();
590 gtk_widget_show(item
);
591 gtk_container_add(GTK_CONTAINER(menu
), item
);
593 item
= gtk_check_menu_item_new_with_mnemonic(_("Show S_ymbol List"));
594 gtk_container_add(GTK_CONTAINER(menu
), item
);
595 #if GTK_CHECK_VERSION(3, 0, 0)
596 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
598 g_signal_connect(item
, "expose-event",
599 G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
601 gtk_widget_show(item
);
602 g_signal_connect(item
, "activate",
603 G_CALLBACK(on_list_symbol_activate
), NULL
);
605 item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Document List"));
606 gtk_container_add(GTK_CONTAINER(menu
), item
);
607 #if GTK_CHECK_VERSION(3, 0, 0)
608 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
610 g_signal_connect(item
, "expose-event",
611 G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
613 gtk_widget_show(item
);
614 g_signal_connect(item
, "activate",
615 G_CALLBACK(on_list_document_activate
), NULL
);
617 item
= gtk_image_menu_item_new_with_mnemonic(_("H_ide Sidebar"));
618 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
619 gtk_image_new_from_stock(GTK_STOCK_CLOSE
, GTK_ICON_SIZE_MENU
));
620 gtk_widget_show(item
);
621 gtk_container_add(GTK_CONTAINER(menu
), item
);
622 g_signal_connect(item
, "activate", G_CALLBACK(on_hide_sidebar
), NULL
);
626 static void on_openfiles_show_paths_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
628 documents_show_paths
= gtk_check_menu_item_get_active(item
);
629 sidebar_openfiles_update_all();
633 static void on_list_document_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
635 interface_prefs
.sidebar_openfiles_visible
= gtk_check_menu_item_get_active(item
);
636 ui_sidebar_show_hide();
637 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
641 static void on_list_symbol_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
643 interface_prefs
.sidebar_symbol_visible
= gtk_check_menu_item_get_active(item
);
644 ui_sidebar_show_hide();
645 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
649 static void on_find_in_files(GtkMenuItem
*menuitem
, gpointer user_data
)
651 GtkTreeSelection
*treesel
;
657 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
658 if (!gtk_tree_selection_get_selected(treesel
, &model
, &iter
))
660 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
664 gtk_tree_model_get(model
, &iter
, DOCUMENTS_FILENAME
, &dir
, -1);
667 dir
= g_path_get_dirname(DOC_FILENAME(doc
));
669 search_show_find_in_files_dialog(dir
);
674 static void on_openfiles_expand_collapse(GtkMenuItem
*menuitem
, gpointer user_data
)
676 gboolean expand
= GPOINTER_TO_INT(user_data
);
679 gtk_tree_view_expand_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
681 gtk_tree_view_collapse_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
685 static void create_openfiles_popup_menu(void)
689 openfiles_popup_menu
= gtk_menu_new();
691 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE
, NULL
);
692 gtk_widget_show(item
);
693 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
694 g_signal_connect(item
, "activate",
695 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
696 doc_items
.close
= item
;
698 item
= gtk_separator_menu_item_new();
699 gtk_widget_show(item
);
700 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
702 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE
, NULL
);
703 gtk_widget_show(item
);
704 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
705 g_signal_connect(item
, "activate",
706 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_SAVE
));
707 doc_items
.save
= item
;
709 item
= gtk_image_menu_item_new_with_mnemonic(_("_Reload"));
710 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
711 gtk_image_new_from_stock(GTK_STOCK_REVERT_TO_SAVED
, GTK_ICON_SIZE_MENU
));
712 gtk_widget_show(item
);
713 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
714 g_signal_connect(item
, "activate",
715 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_RELOAD
));
716 doc_items
.reload
= item
;
718 item
= gtk_separator_menu_item_new();
719 gtk_widget_show(item
);
720 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
722 item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("_Find in Files..."));
723 gtk_widget_show(item
);
724 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
725 g_signal_connect(item
, "activate", G_CALLBACK(on_find_in_files
), NULL
);
726 doc_items
.find_in_files
= item
;
728 item
= gtk_separator_menu_item_new();
729 gtk_widget_show(item
);
730 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
732 doc_items
.show_paths
= gtk_check_menu_item_new_with_mnemonic(_("Show _Paths"));
733 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), documents_show_paths
);
734 gtk_widget_show(doc_items
.show_paths
);
735 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.show_paths
);
736 g_signal_connect(doc_items
.show_paths
, "activate",
737 G_CALLBACK(on_openfiles_show_paths_activate
), NULL
);
739 item
= gtk_separator_menu_item_new();
740 gtk_widget_show(item
);
741 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
743 doc_items
.expand_all
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
744 gtk_widget_show(doc_items
.expand_all
);
745 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.expand_all
);
746 g_signal_connect(doc_items
.expand_all
, "activate",
747 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(TRUE
));
749 doc_items
.collapse_all
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
750 gtk_widget_show(doc_items
.collapse_all
);
751 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.collapse_all
);
752 g_signal_connect(doc_items
.collapse_all
, "activate",
753 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(FALSE
));
755 sidebar_add_common_menu_items(GTK_MENU(openfiles_popup_menu
));
759 static void unfold_parent(GtkTreeIter
*iter
)
764 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
))
766 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), &parent
);
767 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
768 gtk_tree_path_free(path
);
773 /* compares the given data with the doc pointer from the selected row of openfiles
774 * treeview, in case of a match the row is selected and TRUE is returned
775 * (called indirectly from gtk_tree_model_foreach()) */
776 static gboolean
tree_model_find_node(GtkTreeModel
*model
, GtkTreePath
*path
,
777 GtkTreeIter
*iter
, gpointer data
)
781 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
785 /* unfolding also prevents a strange bug where the selection gets stuck on the parent
786 * when it is collapsed and then switching documents */
788 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, NULL
, FALSE
);
795 void sidebar_select_openfiles_item(GeanyDocument
*doc
)
797 gtk_tree_model_foreach(GTK_TREE_MODEL(store_openfiles
), tree_model_find_node
, doc
);
803 static void document_action(GeanyDocument
*doc
, gint action
)
805 if (! DOC_VALID(doc
))
810 case OPENFILES_ACTION_REMOVE
:
815 case OPENFILES_ACTION_SAVE
:
817 document_save_file(doc
, FALSE
);
820 case OPENFILES_ACTION_RELOAD
:
822 document_reload_prompt(doc
, NULL
);
829 static void on_openfiles_document_action(GtkMenuItem
*menuitem
, gpointer user_data
)
832 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
835 gint action
= GPOINTER_TO_INT(user_data
);
837 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
839 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
842 document_action(doc
, action
);
846 /* parent item selected */
848 gint i
= gtk_tree_model_iter_n_children(model
, &iter
) - 1;
850 while (i
>= 0 && gtk_tree_model_iter_nth_child(model
, &child
, &iter
, i
))
852 gtk_tree_model_get(model
, &child
, DOCUMENTS_DOCUMENT
, &doc
, -1);
854 document_action(doc
, action
);
862 static void change_focus_to_editor(GeanyDocument
*doc
, GtkWidget
*source_widget
)
865 document_try_focus(doc
, source_widget
);
866 may_steal_focus
= FALSE
;
870 static gboolean
openfiles_go_to_selection(GtkTreeSelection
*selection
, guint keyval
)
874 GeanyDocument
*doc
= NULL
;
876 /* use switch_notebook_page to ignore changing the notebook page because it is already done */
877 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
) && ! ignore_callback
)
879 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
881 return FALSE
; /* parent */
883 /* switch to the doc and grab the focus */
884 document_show_tab(doc
);
885 if (keyval
!= GDK_space
)
886 change_focus_to_editor(doc
, tv
.tree_openfiles
);
892 static gboolean
taglist_go_to_selection(GtkTreeSelection
*selection
, guint keyval
, guint state
)
897 gboolean handled
= TRUE
;
899 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
903 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
907 line
= tag
->atts
.entry
.line
;
910 GeanyDocument
*doc
= document_get_current();
914 navqueue_goto_line(doc
, doc
, line
);
915 if (keyval
!= GDK_space
&& ! (state
& GDK_CONTROL_MASK
))
916 change_focus_to_editor(doc
, NULL
);
927 static gboolean
sidebar_key_press_cb(GtkWidget
*widget
, GdkEventKey
*event
,
930 may_steal_focus
= FALSE
;
931 if (ui_is_keyval_enter_or_return(event
->keyval
) || event
->keyval
== GDK_space
)
933 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
934 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
935 may_steal_focus
= TRUE
;
937 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
938 * doing so will prevent further handlers to be run in most cases, but the only one is our
939 * own, so guess it's fine. */
940 if (widget_class
->key_press_event
)
941 widget_class
->key_press_event(widget
, event
);
943 if (widget
== tv
.tree_openfiles
) /* tag and doc list have separate handlers */
944 openfiles_go_to_selection(selection
, event
->keyval
);
946 taglist_go_to_selection(selection
, event
->keyval
, event
->state
);
954 static gboolean
sidebar_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
955 G_GNUC_UNUSED gpointer user_data
)
957 GtkTreeSelection
*selection
;
958 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
959 gboolean handled
= FALSE
;
961 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
962 * doing so will prevent further handlers to be run in most cases, but the only one is our own,
963 * so guess it's fine. */
964 if (widget_class
->button_press_event
)
965 handled
= widget_class
->button_press_event(widget
, event
);
967 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
968 may_steal_focus
= TRUE
;
970 if (event
->type
== GDK_2BUTTON_PRESS
)
971 { /* double click on parent node(section) expands/collapses it */
975 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
977 if (gtk_tree_model_iter_has_child(model
, &iter
))
979 GtkTreePath
*path
= gtk_tree_model_get_path(model
, &iter
);
981 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget
), path
))
982 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget
), path
);
984 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget
), path
, FALSE
);
986 gtk_tree_path_free(path
);
991 else if (event
->button
== 1)
992 { /* allow reclicking of taglist treeview item */
993 if (widget
== tv
.tree_openfiles
)
995 openfiles_go_to_selection(selection
, 0);
999 handled
= taglist_go_to_selection(selection
, 0, event
->state
);
1001 else if (event
->button
== 2)
1003 if (widget
== tv
.tree_openfiles
)
1004 on_openfiles_document_action(NULL
, GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
1006 else if (event
->button
== 3)
1008 if (widget
== tv
.tree_openfiles
)
1010 if (!openfiles_popup_menu
)
1011 create_openfiles_popup_menu();
1013 /* update menu item sensitivity */
1014 documents_menu_update(selection
);
1015 gtk_menu_popup(GTK_MENU(openfiles_popup_menu
), NULL
, NULL
, NULL
, NULL
,
1016 event
->button
, event
->time
);
1020 gtk_menu_popup(GTK_MENU(tv
.popup_taglist
), NULL
, NULL
, NULL
, NULL
,
1021 event
->button
, event
->time
);
1029 static void documents_menu_update(GtkTreeSelection
*selection
)
1031 GtkTreeModel
*model
;
1034 gchar
*shortname
= NULL
;
1035 GeanyDocument
*doc
= NULL
;
1037 /* maybe no selection e.g. if ctrl-click deselected */
1038 sel
= gtk_tree_selection_get_selected(selection
, &model
, &iter
);
1041 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
,
1042 DOCUMENTS_SHORTNAME
, &shortname
, -1);
1044 path
= !EMPTY(shortname
) &&
1045 (g_path_is_absolute(shortname
) ||
1046 (app
->project
&& g_str_has_prefix(shortname
, app
->project
->name
)));
1048 /* can close all, save all (except shortname), but only reload individually ATM */
1049 gtk_widget_set_sensitive(doc_items
.close
, sel
);
1050 gtk_widget_set_sensitive(doc_items
.save
, (doc
&& doc
->real_path
) || path
);
1051 gtk_widget_set_sensitive(doc_items
.reload
, doc
&& doc
->real_path
);
1052 gtk_widget_set_sensitive(doc_items
.find_in_files
, sel
);
1055 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), documents_show_paths
);
1056 gtk_widget_set_sensitive(doc_items
.expand_all
, documents_show_paths
);
1057 gtk_widget_set_sensitive(doc_items
.collapse_all
, documents_show_paths
);
1061 static StashGroup
*stash_group
= NULL
;
1063 static void on_load_settings(void)
1065 tag_window
= ui_lookup_widget(main_widgets
.window
, "scrolledwindow2");
1067 prepare_openfiles();
1068 /* note: ui_prefs.sidebar_page is reapplied after plugins are loaded */
1069 stash_group_display(stash_group
, NULL
);
1070 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1074 static void on_save_settings(void)
1076 stash_group_update(stash_group
, NULL
);
1077 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1081 void sidebar_init(void)
1085 group
= stash_group_new(PACKAGE
);
1086 stash_group_add_boolean(group
, &documents_show_paths
, "documents_show_paths", TRUE
);
1087 stash_group_add_widget_property(group
, &ui_prefs
.sidebar_page
, "sidebar_page", GINT_TO_POINTER(0),
1088 main_widgets
.sidebar_notebook
, "page", 0);
1089 configuration_add_pref_group(group
, FALSE
);
1090 stash_group
= group
;
1092 /* delay building documents treeview until sidebar font has been read */
1093 g_signal_connect(geany_object
, "load-settings", on_load_settings
, NULL
);
1094 g_signal_connect(geany_object
, "save-settings", on_save_settings
, NULL
);
1096 g_signal_connect(main_widgets
.sidebar_notebook
, "page-added",
1097 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1098 g_signal_connect(main_widgets
.sidebar_notebook
, "page-removed",
1099 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1100 /* tabs may have changed when sidebar is reshown */
1101 g_signal_connect(main_widgets
.sidebar_notebook
, "show",
1102 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1104 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1107 #define WIDGET(w) w && GTK_IS_WIDGET(w)
1109 void sidebar_finalize(void)
1111 if (WIDGET(tv
.default_tag_tree
))
1113 gtk_widget_destroy(tv
.default_tag_tree
); /* make GTK release its references, if any... */
1114 g_object_unref(tv
.default_tag_tree
); /* ...and release our own */
1116 if (WIDGET(tv
.popup_taglist
))
1117 gtk_widget_destroy(tv
.popup_taglist
);
1118 if (WIDGET(openfiles_popup_menu
))
1119 gtk_widget_destroy(openfiles_popup_menu
);
1123 void sidebar_focus_openfiles_tab(void)
1125 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_openfiles_visible
)
1127 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1129 gtk_notebook_set_current_page(notebook
, TREEVIEW_OPENFILES
);
1130 gtk_widget_grab_focus(tv
.tree_openfiles
);
1135 void sidebar_focus_symbols_tab(void)
1137 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_symbol_visible
)
1139 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1140 GtkWidget
*symbol_list_scrollwin
= gtk_notebook_get_nth_page(notebook
, TREEVIEW_SYMBOL
);
1142 gtk_notebook_set_current_page(notebook
, TREEVIEW_SYMBOL
);
1143 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(symbol_list_scrollwin
)));
1148 static void sidebar_tabs_show_hide(GtkNotebook
*notebook
, GtkWidget
*child
,
1149 guint page_num
, gpointer data
)
1151 gint tabs
= gtk_notebook_get_n_pages(notebook
);
1153 if (interface_prefs
.sidebar_symbol_visible
== FALSE
)
1155 if (interface_prefs
.sidebar_openfiles_visible
== FALSE
)
1158 gtk_notebook_set_show_tabs(notebook
, (tabs
> 1));