Updated spanish translation es.po (#1288)
[geany-mirror.git] / src / sidebar.c
blob639da56b49df8286714e418596fe8e6f58db7552
1 /*
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.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "sidebar.h"
32 #include "app.h"
33 #include "callbacks.h" /* FIXME: for ignore_callback */
34 #include "documentprivate.h"
35 #include "filetypesprivate.h"
36 #include "geanyobject.h"
37 #include "keyfile.h"
38 #include "navqueue.h"
39 #include "stash.h"
40 #include "support.h"
41 #include "symbols.h"
42 #include "ui_utils.h"
43 #include "utils.h"
44 #include "keybindings.h"
46 #include <string.h>
48 #include <gdk/gdkkeysyms.h>
51 SidebarTreeviews tv = {NULL, NULL, NULL};
52 /* while typeahead searching, editor should not get focus */
53 static gboolean may_steal_focus = FALSE;
55 static struct
57 GtkWidget *close;
58 GtkWidget *save;
59 GtkWidget *reload;
60 GtkWidget *show_paths;
61 GtkWidget *find_in_files;
62 GtkWidget *expand_all;
63 GtkWidget *collapse_all;
65 doc_items = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
67 enum
69 TREEVIEW_SYMBOL = 0,
70 TREEVIEW_OPENFILES
73 enum
75 OPENFILES_ACTION_REMOVE = 0,
76 OPENFILES_ACTION_SAVE,
77 OPENFILES_ACTION_RELOAD
80 /* documents tree model columns */
81 enum
83 DOCUMENTS_ICON,
84 DOCUMENTS_SHORTNAME, /* dirname for parents, basename for children */
85 DOCUMENTS_DOCUMENT,
86 DOCUMENTS_COLOR,
87 DOCUMENTS_FILENAME /* full filename */
90 static GtkTreeStore *store_openfiles;
91 static GtkWidget *openfiles_popup_menu;
92 static gboolean documents_show_paths;
93 static GtkWidget *tag_window; /* scrolled window that holds the symbol list GtkTreeView */
95 /* callback prototypes */
96 static void on_openfiles_document_action(GtkMenuItem *menuitem, gpointer user_data);
97 static gboolean sidebar_button_press_cb(GtkWidget *widget, GdkEventButton *event,
98 gpointer user_data);
99 static gboolean sidebar_key_press_cb(GtkWidget *widget, GdkEventKey *event,
100 gpointer user_data);
101 static void on_list_document_activate(GtkCheckMenuItem *item, gpointer user_data);
102 static void on_list_symbol_activate(GtkCheckMenuItem *item, gpointer user_data);
103 static void documents_menu_update(GtkTreeSelection *selection);
104 static void sidebar_tabs_show_hide(GtkNotebook *notebook, GtkWidget *child,
105 guint page_num, gpointer data);
108 /* the prepare_* functions are document-related, but I think they fit better here than in document.c */
109 static void prepare_taglist(GtkWidget *tree, GtkTreeStore *store)
111 GtkCellRenderer *text_renderer, *icon_renderer;
112 GtkTreeViewColumn *column;
113 GtkTreeSelection *selection;
115 text_renderer = gtk_cell_renderer_text_new();
116 icon_renderer = gtk_cell_renderer_pixbuf_new();
117 column = gtk_tree_view_column_new();
119 gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
120 gtk_tree_view_column_set_attributes(column, icon_renderer, "pixbuf", SYMBOLS_COLUMN_ICON, NULL);
121 g_object_set(icon_renderer, "xalign", 0.0, NULL);
123 gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
124 gtk_tree_view_column_set_attributes(column, text_renderer, "text", SYMBOLS_COLUMN_NAME, NULL);
125 g_object_set(text_renderer, "yalign", 0.5, NULL);
126 gtk_tree_view_column_set_title(column, _("Symbols"));
128 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
129 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
131 ui_widget_modify_font_from_string(tree, interface_prefs.tagbar_font);
133 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
134 g_object_unref(store);
136 g_signal_connect(tree, "button-press-event",
137 G_CALLBACK(sidebar_button_press_cb), NULL);
138 g_signal_connect(tree, "key-press-event",
139 G_CALLBACK(sidebar_key_press_cb), NULL);
141 gtk_tree_view_set_show_expanders(GTK_TREE_VIEW(tree), interface_prefs.show_symbol_list_expanders);
142 if (! interface_prefs.show_symbol_list_expanders)
143 gtk_tree_view_set_level_indentation(GTK_TREE_VIEW(tree), 10);
144 /* Tooltips */
145 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(tree), SYMBOLS_COLUMN_TOOLTIP);
147 /* selection handling */
148 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
149 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
150 /* callback for changed selection not necessary, will be handled by button-press-event */
154 static gboolean
155 on_default_tag_tree_button_press_event(GtkWidget *widget, GdkEventButton *event,
156 gpointer user_data)
158 if (event->button == 3)
160 gtk_menu_popup(GTK_MENU(tv.popup_taglist), NULL, NULL, NULL, NULL,
161 event->button, event->time);
162 return TRUE;
164 return FALSE;
168 static void create_default_tag_tree(void)
170 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW(tag_window);
171 GtkWidget *label;
173 /* default_tag_tree is a GtkViewPort with a GtkLabel inside it */
174 tv.default_tag_tree = gtk_viewport_new(
175 gtk_scrolled_window_get_hadjustment(scrolled_window),
176 gtk_scrolled_window_get_vadjustment(scrolled_window));
177 gtk_viewport_set_shadow_type(GTK_VIEWPORT(tv.default_tag_tree), GTK_SHADOW_NONE);
178 label = gtk_label_new(_("No symbols found"));
179 gtk_misc_set_alignment(GTK_MISC(label), 0.1f, 0.01f);
180 gtk_container_add(GTK_CONTAINER(tv.default_tag_tree), label);
181 gtk_widget_show_all(tv.default_tag_tree);
182 g_signal_connect(tv.default_tag_tree, "button-press-event",
183 G_CALLBACK(on_default_tag_tree_button_press_event), NULL);
184 g_object_ref((gpointer)tv.default_tag_tree); /* to hold it after removing */
188 /* update = rescan the tags for doc->filename */
189 void sidebar_update_tag_list(GeanyDocument *doc, gboolean update)
191 GtkWidget *child = gtk_bin_get_child(GTK_BIN(tag_window));
193 g_return_if_fail(doc == NULL || doc->is_valid);
195 if (update)
196 doc->priv->tag_tree_dirty = TRUE;
198 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) != TREEVIEW_SYMBOL)
199 return; /* don't bother updating symbol tree if we don't see it */
201 /* changes the tree view to the given one, trying not to do useless changes */
202 #define CHANGE_TREE(new_child) \
203 G_STMT_START { \
204 /* only change the tag tree if it's actually not the same (to avoid flickering) and if
205 * it's the one of the current document (to avoid problems when e.g. reloading
206 * configuration files */ \
207 if (child != new_child && doc == document_get_current()) \
209 if (child) \
210 gtk_container_remove(GTK_CONTAINER(tag_window), child); \
211 gtk_container_add(GTK_CONTAINER(tag_window), new_child); \
213 } G_STMT_END
215 if (tv.default_tag_tree == NULL)
216 create_default_tag_tree();
218 /* show default empty tag tree if there are no tags */
219 if (doc == NULL || doc->file_type == NULL || ! filetype_has_tags(doc->file_type))
221 CHANGE_TREE(tv.default_tag_tree);
222 return;
225 if (doc->priv->tag_tree_dirty)
226 { /* updating the tag list in the left tag window */
227 if (doc->priv->tag_tree == NULL)
229 doc->priv->tag_store = gtk_tree_store_new(
230 SYMBOLS_N_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, TM_TYPE_TAG, G_TYPE_STRING);
231 doc->priv->tag_tree = gtk_tree_view_new();
232 prepare_taglist(doc->priv->tag_tree, doc->priv->tag_store);
233 gtk_widget_show(doc->priv->tag_tree);
234 g_object_ref((gpointer)doc->priv->tag_tree); /* to hold it after removing */
237 doc->has_tags = symbols_recreate_tag_list(doc, SYMBOLS_SORT_USE_PREVIOUS);
238 doc->priv->tag_tree_dirty = FALSE;
241 if (doc->has_tags)
243 CHANGE_TREE(doc->priv->tag_tree);
245 else
247 CHANGE_TREE(tv.default_tag_tree);
250 #undef CHANGE_TREE
254 /* cleverly sorts documents by their short name */
255 static gint documents_sort_func(GtkTreeModel *model, GtkTreeIter *iter_a,
256 GtkTreeIter *iter_b, gpointer data)
258 gchar *key_a, *key_b;
259 gchar *name_a, *name_b;
260 gint cmp;
262 gtk_tree_model_get(model, iter_a, DOCUMENTS_SHORTNAME, &name_a, -1);
263 key_a = g_utf8_collate_key_for_filename(name_a, -1);
264 g_free(name_a);
265 gtk_tree_model_get(model, iter_b, DOCUMENTS_SHORTNAME, &name_b, -1);
266 key_b = g_utf8_collate_key_for_filename(name_b, -1);
267 g_free(name_b);
268 cmp = strcmp(key_a, key_b);
269 g_free(key_b);
270 g_free(key_a);
272 return cmp;
276 /* does some preparing things to the open files list widget */
277 static void prepare_openfiles(void)
279 GtkCellRenderer *icon_renderer;
280 GtkCellRenderer *text_renderer;
281 GtkTreeViewColumn *column;
282 GtkTreeSelection *selection;
283 GtkTreeSortable *sortable;
285 tv.tree_openfiles = ui_lookup_widget(main_widgets.window, "treeview6");
287 /* store the icon and the short filename to show, and the index as reference,
288 * the colour (black/red/green) and the full name for the tooltip */
289 store_openfiles = gtk_tree_store_new(5, G_TYPE_ICON, G_TYPE_STRING,
290 G_TYPE_POINTER, GDK_TYPE_COLOR, G_TYPE_STRING);
291 gtk_tree_view_set_model(GTK_TREE_VIEW(tv.tree_openfiles), GTK_TREE_MODEL(store_openfiles));
293 /* set policy settings for the scolledwindow around the treeview again, because glade
294 * doesn't keep the settings */
295 gtk_scrolled_window_set_policy(
296 GTK_SCROLLED_WINDOW(ui_lookup_widget(main_widgets.window, "scrolledwindow7")),
297 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
299 icon_renderer = gtk_cell_renderer_pixbuf_new();
300 g_object_set(icon_renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
301 text_renderer = gtk_cell_renderer_text_new();
302 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
303 column = gtk_tree_view_column_new();
304 gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
305 gtk_tree_view_column_set_attributes(column, icon_renderer, "gicon", DOCUMENTS_ICON, NULL);
306 gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
307 gtk_tree_view_column_set_attributes(column, text_renderer, "text", DOCUMENTS_SHORTNAME,
308 "foreground-gdk", DOCUMENTS_COLOR, NULL);
309 gtk_tree_view_append_column(GTK_TREE_VIEW(tv.tree_openfiles), column);
310 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv.tree_openfiles), FALSE);
312 gtk_tree_view_set_search_column(GTK_TREE_VIEW(tv.tree_openfiles),
313 DOCUMENTS_SHORTNAME);
315 /* sort opened filenames in the store_openfiles treeview */
316 sortable = GTK_TREE_SORTABLE(GTK_TREE_MODEL(store_openfiles));
317 gtk_tree_sortable_set_sort_func(sortable, DOCUMENTS_SHORTNAME, documents_sort_func, NULL, NULL);
318 gtk_tree_sortable_set_sort_column_id(sortable, DOCUMENTS_SHORTNAME, GTK_SORT_ASCENDING);
320 ui_widget_modify_font_from_string(tv.tree_openfiles, interface_prefs.tagbar_font);
322 /* tooltips */
323 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(tv.tree_openfiles), DOCUMENTS_FILENAME);
325 /* selection handling */
326 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles));
327 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
328 g_object_unref(store_openfiles);
330 g_signal_connect(GTK_TREE_VIEW(tv.tree_openfiles), "button-press-event",
331 G_CALLBACK(sidebar_button_press_cb), NULL);
332 g_signal_connect(GTK_TREE_VIEW(tv.tree_openfiles), "key-press-event",
333 G_CALLBACK(sidebar_key_press_cb), NULL);
337 /* iter should be toplevel */
338 static gboolean find_tree_iter_dir(GtkTreeIter *iter, const gchar *dir)
340 GeanyDocument *doc;
341 gchar *name;
342 gboolean result;
344 if (utils_str_equal(dir, "."))
345 dir = GEANY_STRING_UNTITLED;
347 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles), iter, DOCUMENTS_DOCUMENT, &doc, -1);
348 g_return_val_if_fail(!doc, FALSE);
350 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles), iter, DOCUMENTS_SHORTNAME, &name, -1);
352 result = utils_filenamecmp(name, dir) == 0;
353 g_free(name);
355 return result;
359 static gboolean utils_filename_has_prefix(const gchar *str, const gchar *prefix)
361 gchar *head = g_strndup(str, strlen(prefix));
362 gboolean ret = utils_filenamecmp(head, prefix) == 0;
364 g_free(head);
365 return ret;
369 static gchar *get_doc_folder(const gchar *path)
371 gchar *tmp_dirname = g_strdup(path);
372 gchar *project_base_path;
373 gchar *dirname = NULL;
374 const gchar *home_dir = g_get_home_dir();
375 const gchar *rest;
377 /* replace the project base path with the project name */
378 project_base_path = project_get_base_path();
380 if (project_base_path != NULL)
382 gsize len = strlen(project_base_path);
384 /* remove trailing separator so we can match base path exactly */
385 if (project_base_path[len-1] == G_DIR_SEPARATOR)
386 project_base_path[--len] = '\0';
388 /* check whether the dir name matches or uses the project base path */
389 if (utils_filename_has_prefix(tmp_dirname, project_base_path))
391 rest = tmp_dirname + len;
392 if (*rest == G_DIR_SEPARATOR || *rest == '\0')
394 dirname = g_strdup_printf("%s%s", app->project->name, rest);
397 g_free(project_base_path);
399 if (dirname == NULL)
401 dirname = tmp_dirname;
403 /* If matches home dir, replace with tilde */
404 if (!EMPTY(home_dir) && utils_filename_has_prefix(dirname, home_dir))
406 rest = dirname + strlen(home_dir);
407 if (*rest == G_DIR_SEPARATOR || *rest == '\0')
409 dirname = g_strdup_printf("~%s", rest);
410 g_free(tmp_dirname);
414 else
415 g_free(tmp_dirname);
417 return dirname;
421 static GtkTreeIter *get_doc_parent(GeanyDocument *doc)
423 gchar *path;
424 gchar *dirname = NULL;
425 static GtkTreeIter parent;
426 GtkTreeModel *model = GTK_TREE_MODEL(store_openfiles);
427 static GIcon *dir_icon = NULL;
429 if (!documents_show_paths)
430 return NULL;
432 path = g_path_get_dirname(DOC_FILENAME(doc));
433 dirname = get_doc_folder(path);
435 if (gtk_tree_model_get_iter_first(model, &parent))
439 if (find_tree_iter_dir(&parent, dirname))
441 g_free(dirname);
442 g_free(path);
443 return &parent;
446 while (gtk_tree_model_iter_next(model, &parent));
448 /* no match, add dir parent */
449 if (!dir_icon)
450 dir_icon = ui_get_mime_icon("inode/directory");
452 gtk_tree_store_append(store_openfiles, &parent, NULL);
453 gtk_tree_store_set(store_openfiles, &parent, DOCUMENTS_ICON, dir_icon,
454 DOCUMENTS_FILENAME, path,
455 DOCUMENTS_SHORTNAME, doc->file_name ? dirname : GEANY_STRING_UNTITLED, -1);
457 g_free(dirname);
458 g_free(path);
459 return &parent;
463 /* Also sets doc->priv->iter.
464 * This is called recursively in sidebar_openfiles_update_all(). */
465 void sidebar_openfiles_add(GeanyDocument *doc)
467 GtkTreeIter *iter = &doc->priv->iter;
468 GtkTreeIter *parent = get_doc_parent(doc);
469 gchar *basename;
470 const GdkColor *color = document_get_status_color(doc);
471 static GIcon *file_icon = NULL;
473 gtk_tree_store_append(store_openfiles, iter, parent);
475 /* check if new parent */
476 if (parent && gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles), parent) == 1)
478 GtkTreePath *path;
480 /* expand parent */
481 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles), parent);
482 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv.tree_openfiles), path, TRUE);
483 gtk_tree_path_free(path);
485 if (!file_icon)
486 file_icon = ui_get_mime_icon("text/plain");
488 basename = g_path_get_basename(DOC_FILENAME(doc));
489 gtk_tree_store_set(store_openfiles, iter,
490 DOCUMENTS_ICON, (doc->file_type && doc->file_type->icon) ? doc->file_type->icon : file_icon,
491 DOCUMENTS_SHORTNAME, basename, DOCUMENTS_DOCUMENT, doc, DOCUMENTS_COLOR, color,
492 DOCUMENTS_FILENAME, DOC_FILENAME(doc), -1);
493 g_free(basename);
497 static void openfiles_remove(GeanyDocument *doc)
499 GtkTreeIter *iter = &doc->priv->iter;
500 GtkTreeIter parent;
502 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles), &parent, iter) &&
503 gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles), &parent) == 1)
504 gtk_tree_store_remove(store_openfiles, &parent);
505 else
506 gtk_tree_store_remove(store_openfiles, iter);
510 void sidebar_openfiles_update(GeanyDocument *doc)
512 GtkTreeIter *iter = &doc->priv->iter;
513 gchar *fname;
515 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles), iter, DOCUMENTS_FILENAME, &fname, -1);
517 if (utils_str_equal(fname, DOC_FILENAME(doc)))
519 /* just update color and the icon */
520 const GdkColor *color = document_get_status_color(doc);
521 GIcon *icon = doc->file_type->icon;
523 gtk_tree_store_set(store_openfiles, iter, DOCUMENTS_COLOR, color, -1);
524 if (icon)
525 gtk_tree_store_set(store_openfiles, iter, DOCUMENTS_ICON, icon, -1);
527 else
529 /* path has changed, so remove and re-add */
530 GtkTreeSelection *treesel;
531 gboolean sel;
533 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles));
534 sel = gtk_tree_selection_iter_is_selected(treesel, &doc->priv->iter);
535 openfiles_remove(doc);
537 sidebar_openfiles_add(doc);
538 if (sel)
539 gtk_tree_selection_select_iter(treesel, &doc->priv->iter);
541 g_free(fname);
545 void sidebar_openfiles_update_all(void)
547 guint i;
549 gtk_tree_store_clear(store_openfiles);
550 foreach_document (i)
552 sidebar_openfiles_add(documents[i]);
557 void sidebar_remove_document(GeanyDocument *doc)
559 openfiles_remove(doc);
561 if (GTK_IS_WIDGET(doc->priv->tag_tree))
563 gtk_widget_destroy(doc->priv->tag_tree); /* make GTK release its references, if any */
564 /* Because it was ref'd in sidebar_update_tag_list, it needs unref'ing */
565 g_object_unref(doc->priv->tag_tree);
566 doc->priv->tag_tree = NULL;
571 static void on_hide_sidebar(void)
573 ui_prefs.sidebar_visible = FALSE;
574 ui_sidebar_show_hide();
578 static gboolean on_sidebar_display_symbol_list_show(GtkWidget *item)
580 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),
581 interface_prefs.sidebar_symbol_visible);
582 return FALSE;
586 static gboolean on_sidebar_display_open_files_show(GtkWidget *item)
588 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),
589 interface_prefs.sidebar_openfiles_visible);
590 return FALSE;
594 void sidebar_add_common_menu_items(GtkMenu *menu)
596 GtkWidget *item;
598 item = gtk_separator_menu_item_new();
599 gtk_widget_show(item);
600 gtk_container_add(GTK_CONTAINER(menu), item);
602 item = gtk_check_menu_item_new_with_mnemonic(_("Show S_ymbol List"));
603 gtk_container_add(GTK_CONTAINER(menu), item);
604 #if GTK_CHECK_VERSION(3, 0, 0)
605 g_signal_connect(item, "draw", G_CALLBACK(on_sidebar_display_symbol_list_show), NULL);
606 #else
607 g_signal_connect(item, "expose-event",
608 G_CALLBACK(on_sidebar_display_symbol_list_show), NULL);
609 #endif
610 gtk_widget_show(item);
611 g_signal_connect(item, "activate",
612 G_CALLBACK(on_list_symbol_activate), NULL);
614 item = gtk_check_menu_item_new_with_mnemonic(_("Show _Document List"));
615 gtk_container_add(GTK_CONTAINER(menu), item);
616 #if GTK_CHECK_VERSION(3, 0, 0)
617 g_signal_connect(item, "draw", G_CALLBACK(on_sidebar_display_open_files_show), NULL);
618 #else
619 g_signal_connect(item, "expose-event",
620 G_CALLBACK(on_sidebar_display_open_files_show), NULL);
621 #endif
622 gtk_widget_show(item);
623 g_signal_connect(item, "activate",
624 G_CALLBACK(on_list_document_activate), NULL);
626 item = gtk_image_menu_item_new_with_mnemonic(_("H_ide Sidebar"));
627 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
628 gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
629 gtk_widget_show(item);
630 gtk_container_add(GTK_CONTAINER(menu), item);
631 g_signal_connect(item, "activate", G_CALLBACK(on_hide_sidebar), NULL);
635 static void on_openfiles_show_paths_activate(GtkCheckMenuItem *item, gpointer user_data)
637 documents_show_paths = gtk_check_menu_item_get_active(item);
638 sidebar_openfiles_update_all();
642 static void on_list_document_activate(GtkCheckMenuItem *item, gpointer user_data)
644 interface_prefs.sidebar_openfiles_visible = gtk_check_menu_item_get_active(item);
645 ui_sidebar_show_hide();
646 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets.sidebar_notebook), NULL, 0, NULL);
650 static void on_list_symbol_activate(GtkCheckMenuItem *item, gpointer user_data)
652 interface_prefs.sidebar_symbol_visible = gtk_check_menu_item_get_active(item);
653 ui_sidebar_show_hide();
654 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets.sidebar_notebook), NULL, 0, NULL);
658 static void on_find_in_files(GtkMenuItem *menuitem, gpointer user_data)
660 GtkTreeSelection *treesel;
661 GtkTreeIter iter;
662 GtkTreeModel *model;
663 GeanyDocument *doc;
664 gchar *dir;
666 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles));
667 if (!gtk_tree_selection_get_selected(treesel, &model, &iter))
668 return;
669 gtk_tree_model_get(model, &iter, DOCUMENTS_DOCUMENT, &doc, -1);
671 if (!doc)
673 gtk_tree_model_get(model, &iter, DOCUMENTS_FILENAME, &dir, -1);
675 else
676 dir = g_path_get_dirname(DOC_FILENAME(doc));
678 search_show_find_in_files_dialog(dir);
679 g_free(dir);
683 static void on_openfiles_expand_collapse(GtkMenuItem *menuitem, gpointer user_data)
685 gboolean expand = GPOINTER_TO_INT(user_data);
687 if (expand)
688 gtk_tree_view_expand_all(GTK_TREE_VIEW(tv.tree_openfiles));
689 else
690 gtk_tree_view_collapse_all(GTK_TREE_VIEW(tv.tree_openfiles));
694 static void create_openfiles_popup_menu(void)
696 GtkWidget *item;
698 openfiles_popup_menu = gtk_menu_new();
700 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE, NULL);
701 gtk_widget_show(item);
702 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
703 g_signal_connect(item, "activate",
704 G_CALLBACK(on_openfiles_document_action), GINT_TO_POINTER(OPENFILES_ACTION_REMOVE));
705 doc_items.close = item;
707 item = gtk_separator_menu_item_new();
708 gtk_widget_show(item);
709 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
711 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE, NULL);
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_SAVE));
716 doc_items.save = item;
718 item = gtk_image_menu_item_new_with_mnemonic(_("_Reload"));
719 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
720 gtk_image_new_from_stock(GTK_STOCK_REVERT_TO_SAVED, GTK_ICON_SIZE_MENU));
721 gtk_widget_show(item);
722 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
723 g_signal_connect(item, "activate",
724 G_CALLBACK(on_openfiles_document_action), GINT_TO_POINTER(OPENFILES_ACTION_RELOAD));
725 doc_items.reload = item;
727 item = gtk_separator_menu_item_new();
728 gtk_widget_show(item);
729 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
731 item = ui_image_menu_item_new(GTK_STOCK_FIND, _("_Find in Files..."));
732 gtk_widget_show(item);
733 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
734 g_signal_connect(item, "activate", G_CALLBACK(on_find_in_files), NULL);
735 doc_items.find_in_files = item;
737 item = gtk_separator_menu_item_new();
738 gtk_widget_show(item);
739 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
741 doc_items.show_paths = gtk_check_menu_item_new_with_mnemonic(_("Show _Paths"));
742 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items.show_paths), documents_show_paths);
743 gtk_widget_show(doc_items.show_paths);
744 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), doc_items.show_paths);
745 g_signal_connect(doc_items.show_paths, "activate",
746 G_CALLBACK(on_openfiles_show_paths_activate), NULL);
748 item = gtk_separator_menu_item_new();
749 gtk_widget_show(item);
750 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), item);
752 doc_items.expand_all = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
753 gtk_widget_show(doc_items.expand_all);
754 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), doc_items.expand_all);
755 g_signal_connect(doc_items.expand_all, "activate",
756 G_CALLBACK(on_openfiles_expand_collapse), GINT_TO_POINTER(TRUE));
758 doc_items.collapse_all = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
759 gtk_widget_show(doc_items.collapse_all);
760 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu), doc_items.collapse_all);
761 g_signal_connect(doc_items.collapse_all, "activate",
762 G_CALLBACK(on_openfiles_expand_collapse), GINT_TO_POINTER(FALSE));
764 sidebar_add_common_menu_items(GTK_MENU(openfiles_popup_menu));
768 static void unfold_parent(GtkTreeIter *iter)
770 GtkTreeIter parent;
772 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles), &parent, iter))
774 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles), &parent);
775 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv.tree_openfiles), path, TRUE);
776 gtk_tree_path_free(path);
781 /* compares the given data with the doc pointer from the selected row of openfiles
782 * treeview, in case of a match the row is selected and TRUE is returned
783 * (called indirectly from gtk_tree_model_foreach()) */
784 static gboolean tree_model_find_node(GtkTreeModel *model, GtkTreePath *path,
785 GtkTreeIter *iter, gpointer data)
787 GeanyDocument *doc;
789 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles), iter, DOCUMENTS_DOCUMENT, &doc, -1);
791 if (doc == data)
793 /* unfolding also prevents a strange bug where the selection gets stuck on the parent
794 * when it is collapsed and then switching documents */
795 unfold_parent(iter);
796 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv.tree_openfiles), path, NULL, FALSE);
797 return TRUE;
799 else return FALSE;
803 void sidebar_select_openfiles_item(GeanyDocument *doc)
805 gtk_tree_model_foreach(GTK_TREE_MODEL(store_openfiles), tree_model_find_node, doc);
809 /* callbacks */
811 static void document_action(GeanyDocument *doc, gint action)
813 if (! DOC_VALID(doc))
814 return;
816 switch (action)
818 case OPENFILES_ACTION_REMOVE:
820 document_close(doc);
821 break;
823 case OPENFILES_ACTION_SAVE:
825 document_save_file(doc, FALSE);
826 break;
828 case OPENFILES_ACTION_RELOAD:
830 document_reload_prompt(doc, NULL);
831 break;
837 static void on_openfiles_document_action(GtkMenuItem *menuitem, gpointer user_data)
839 GtkTreeIter iter;
840 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles));
841 GtkTreeModel *model;
842 GeanyDocument *doc;
843 gint action = GPOINTER_TO_INT(user_data);
845 if (gtk_tree_selection_get_selected(selection, &model, &iter))
847 gtk_tree_model_get(model, &iter, DOCUMENTS_DOCUMENT, &doc, -1);
848 if (doc)
850 document_action(doc, action);
852 else
854 /* parent item selected */
855 GtkTreeIter child;
856 gint i = gtk_tree_model_iter_n_children(model, &iter) - 1;
858 while (i >= 0 && gtk_tree_model_iter_nth_child(model, &child, &iter, i))
860 gtk_tree_model_get(model, &child, DOCUMENTS_DOCUMENT, &doc, -1);
862 document_action(doc, action);
863 i--;
870 static void change_focus_to_editor(GeanyDocument *doc, GtkWidget *source_widget)
872 if (may_steal_focus)
873 document_try_focus(doc, source_widget);
874 may_steal_focus = FALSE;
878 static gboolean openfiles_go_to_selection(GtkTreeSelection *selection, guint keyval)
880 GtkTreeIter iter;
881 GtkTreeModel *model;
882 GeanyDocument *doc = NULL;
884 /* use switch_notebook_page to ignore changing the notebook page because it is already done */
885 if (gtk_tree_selection_get_selected(selection, &model, &iter) && ! ignore_callback)
887 gtk_tree_model_get(model, &iter, DOCUMENTS_DOCUMENT, &doc, -1);
888 if (! doc)
889 return FALSE; /* parent */
891 /* switch to the doc and grab the focus */
892 document_show_tab(doc);
893 if (keyval != GDK_space)
894 change_focus_to_editor(doc, tv.tree_openfiles);
896 return FALSE;
900 static gboolean taglist_go_to_selection(GtkTreeSelection *selection, guint keyval, guint state)
902 GtkTreeIter iter;
903 GtkTreeModel *model;
904 gint line = 0;
905 gboolean handled = TRUE;
907 if (gtk_tree_selection_get_selected(selection, &model, &iter))
909 TMTag *tag;
911 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
912 if (! tag)
913 return FALSE;
915 line = tag->line;
916 if (line > 0)
918 GeanyDocument *doc = document_get_current();
920 if (doc != NULL)
922 navqueue_goto_line(doc, doc, line);
923 state = keybindings_get_modifiers(state);
924 if (keyval != GDK_space && ! (state & GEANY_PRIMARY_MOD_MASK))
925 change_focus_to_editor(doc, NULL);
926 else
927 handled = FALSE;
930 tm_tag_unref(tag);
932 return handled;
936 static gboolean sidebar_key_press_cb(GtkWidget *widget, GdkEventKey *event,
937 gpointer user_data)
939 may_steal_focus = FALSE;
940 if (ui_is_keyval_enter_or_return(event->keyval) || event->keyval == GDK_space)
942 GtkWidgetClass *widget_class = GTK_WIDGET_GET_CLASS(widget);
943 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
944 may_steal_focus = TRUE;
946 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
947 * doing so will prevent further handlers to be run in most cases, but the only one is our
948 * own, so guess it's fine. */
949 if (widget_class->key_press_event)
950 widget_class->key_press_event(widget, event);
952 if (widget == tv.tree_openfiles) /* tag and doc list have separate handlers */
953 openfiles_go_to_selection(selection, event->keyval);
954 else
955 taglist_go_to_selection(selection, event->keyval, event->state);
957 return TRUE;
959 return FALSE;
963 static gboolean sidebar_button_press_cb(GtkWidget *widget, GdkEventButton *event,
964 G_GNUC_UNUSED gpointer user_data)
966 GtkTreeSelection *selection;
967 GtkWidgetClass *widget_class = GTK_WIDGET_GET_CLASS(widget);
968 gboolean handled = FALSE;
970 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
971 * doing so will prevent further handlers to be run in most cases, but the only one is our own,
972 * so guess it's fine. */
973 if (widget_class->button_press_event)
974 handled = widget_class->button_press_event(widget, event);
976 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
977 may_steal_focus = TRUE;
979 if (event->type == GDK_2BUTTON_PRESS)
980 { /* double click on parent node(section) expands/collapses it */
981 GtkTreeModel *model;
982 GtkTreeIter iter;
984 if (gtk_tree_selection_get_selected(selection, &model, &iter))
986 if (gtk_tree_model_iter_has_child(model, &iter))
988 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
990 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget), path))
991 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget), path);
992 else
993 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget), path, FALSE);
995 gtk_tree_path_free(path);
996 return TRUE;
1000 else if (event->button == 1)
1001 { /* allow reclicking of taglist treeview item */
1002 if (widget == tv.tree_openfiles)
1004 openfiles_go_to_selection(selection, 0);
1005 handled = TRUE;
1007 else
1008 handled = taglist_go_to_selection(selection, 0, event->state);
1010 else if (event->button == 2)
1012 if (widget == tv.tree_openfiles)
1013 on_openfiles_document_action(NULL, GINT_TO_POINTER(OPENFILES_ACTION_REMOVE));
1015 else if (event->button == 3)
1017 if (widget == tv.tree_openfiles)
1019 if (!openfiles_popup_menu)
1020 create_openfiles_popup_menu();
1022 /* update menu item sensitivity */
1023 documents_menu_update(selection);
1024 gtk_menu_popup(GTK_MENU(openfiles_popup_menu), NULL, NULL, NULL, NULL,
1025 event->button, event->time);
1027 else
1029 gtk_menu_popup(GTK_MENU(tv.popup_taglist), NULL, NULL, NULL, NULL,
1030 event->button, event->time);
1032 handled = TRUE;
1034 return handled;
1038 static void documents_menu_update(GtkTreeSelection *selection)
1040 GtkTreeModel *model;
1041 GtkTreeIter iter;
1042 gboolean sel, path;
1043 gchar *shortname = NULL;
1044 GeanyDocument *doc = NULL;
1046 /* maybe no selection e.g. if ctrl-click deselected */
1047 sel = gtk_tree_selection_get_selected(selection, &model, &iter);
1048 if (sel)
1050 gtk_tree_model_get(model, &iter, DOCUMENTS_DOCUMENT, &doc,
1051 DOCUMENTS_SHORTNAME, &shortname, -1);
1053 path = !EMPTY(shortname) &&
1054 (g_path_is_absolute(shortname) ||
1055 (app->project && g_str_has_prefix(shortname, app->project->name)));
1057 /* can close all, save all (except shortname), but only reload individually ATM */
1058 gtk_widget_set_sensitive(doc_items.close, sel);
1059 gtk_widget_set_sensitive(doc_items.save, (doc && doc->real_path) || path);
1060 gtk_widget_set_sensitive(doc_items.reload, doc && doc->real_path);
1061 gtk_widget_set_sensitive(doc_items.find_in_files, sel);
1062 g_free(shortname);
1064 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items.show_paths), documents_show_paths);
1065 gtk_widget_set_sensitive(doc_items.expand_all, documents_show_paths);
1066 gtk_widget_set_sensitive(doc_items.collapse_all, documents_show_paths);
1070 static StashGroup *stash_group = NULL;
1072 static void on_load_settings(void)
1074 tag_window = ui_lookup_widget(main_widgets.window, "scrolledwindow2");
1076 prepare_openfiles();
1077 /* note: ui_prefs.sidebar_page is reapplied after plugins are loaded */
1078 stash_group_display(stash_group, NULL);
1079 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets.sidebar_notebook), NULL, 0, NULL);
1083 static void on_save_settings(void)
1085 stash_group_update(stash_group, NULL);
1086 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets.sidebar_notebook), NULL, 0, NULL);
1090 static void on_sidebar_switch_page(GtkNotebook *notebook,
1091 gpointer page, guint page_num, gpointer user_data)
1093 if (page_num == TREEVIEW_SYMBOL)
1094 sidebar_update_tag_list(document_get_current(), FALSE);
1098 void sidebar_init(void)
1100 StashGroup *group;
1102 group = stash_group_new(PACKAGE);
1103 stash_group_add_boolean(group, &documents_show_paths, "documents_show_paths", TRUE);
1104 stash_group_add_widget_property(group, &ui_prefs.sidebar_page, "sidebar_page", GINT_TO_POINTER(0),
1105 main_widgets.sidebar_notebook, "page", 0);
1106 configuration_add_pref_group(group, FALSE);
1107 stash_group = group;
1109 /* delay building documents treeview until sidebar font has been read */
1110 g_signal_connect(geany_object, "load-settings", on_load_settings, NULL);
1111 g_signal_connect(geany_object, "save-settings", on_save_settings, NULL);
1113 g_signal_connect(main_widgets.sidebar_notebook, "page-added",
1114 G_CALLBACK(sidebar_tabs_show_hide), NULL);
1115 g_signal_connect(main_widgets.sidebar_notebook, "page-removed",
1116 G_CALLBACK(sidebar_tabs_show_hide), NULL);
1117 /* tabs may have changed when sidebar is reshown */
1118 g_signal_connect(main_widgets.sidebar_notebook, "show",
1119 G_CALLBACK(sidebar_tabs_show_hide), NULL);
1120 g_signal_connect_after(main_widgets.sidebar_notebook, "switch-page",
1121 G_CALLBACK(on_sidebar_switch_page), NULL);
1123 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets.sidebar_notebook), NULL, 0, NULL);
1126 #define WIDGET(w) w && GTK_IS_WIDGET(w)
1128 void sidebar_finalize(void)
1130 if (WIDGET(tv.default_tag_tree))
1132 gtk_widget_destroy(tv.default_tag_tree); /* make GTK release its references, if any... */
1133 g_object_unref(tv.default_tag_tree); /* ...and release our own */
1135 if (WIDGET(tv.popup_taglist))
1136 gtk_widget_destroy(tv.popup_taglist);
1137 if (WIDGET(openfiles_popup_menu))
1138 gtk_widget_destroy(openfiles_popup_menu);
1142 void sidebar_focus_openfiles_tab(void)
1144 if (ui_prefs.sidebar_visible && interface_prefs.sidebar_openfiles_visible)
1146 GtkNotebook *notebook = GTK_NOTEBOOK(main_widgets.sidebar_notebook);
1148 gtk_notebook_set_current_page(notebook, TREEVIEW_OPENFILES);
1149 gtk_widget_grab_focus(tv.tree_openfiles);
1154 void sidebar_focus_symbols_tab(void)
1156 if (ui_prefs.sidebar_visible && interface_prefs.sidebar_symbol_visible)
1158 GtkNotebook *notebook = GTK_NOTEBOOK(main_widgets.sidebar_notebook);
1159 GtkWidget *symbol_list_scrollwin = gtk_notebook_get_nth_page(notebook, TREEVIEW_SYMBOL);
1161 gtk_notebook_set_current_page(notebook, TREEVIEW_SYMBOL);
1162 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(symbol_list_scrollwin)));
1167 static void sidebar_tabs_show_hide(GtkNotebook *notebook, GtkWidget *child,
1168 guint page_num, gpointer data)
1170 gint tabs = gtk_notebook_get_n_pages(notebook);
1172 if (interface_prefs.sidebar_symbol_visible == FALSE)
1173 tabs--;
1174 if (interface_prefs.sidebar_openfiles_visible == FALSE)
1175 tabs--;
1177 gtk_notebook_set_show_tabs(notebook, (tabs > 1));