2 * sidebar.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Sidebar related code for the Symbol list and Open files GtkTreeViews.
32 #include "callbacks.h" /* FIXME: for ignore_callback */
33 #include "documentprivate.h"
34 #include "filetypesprivate.h"
35 #include "geanyobject.h"
43 #include "keybindings.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 GtkWidget
*tag_window
; /* scrolled window that holds the symbol list GtkTreeView */
93 /* callback prototypes */
94 static void on_openfiles_document_action(GtkMenuItem
*menuitem
, gpointer user_data
);
95 static gboolean
sidebar_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
97 static gboolean
sidebar_key_press_cb(GtkWidget
*widget
, GdkEventKey
*event
,
99 static void on_list_document_activate(GtkCheckMenuItem
*item
, gpointer user_data
);
100 static void on_list_symbol_activate(GtkCheckMenuItem
*item
, gpointer user_data
);
101 static void documents_menu_update(GtkTreeSelection
*selection
);
102 static void sidebar_tabs_show_hide(GtkNotebook
*notebook
, GtkWidget
*child
,
103 guint page_num
, gpointer data
);
106 /* the prepare_* functions are document-related, but I think they fit better here than in document.c */
107 static void prepare_taglist(GtkWidget
*tree
, GtkTreeStore
*store
)
109 GtkCellRenderer
*text_renderer
, *icon_renderer
;
110 GtkTreeViewColumn
*column
;
111 GtkTreeSelection
*selection
;
113 text_renderer
= gtk_cell_renderer_text_new();
114 icon_renderer
= gtk_cell_renderer_pixbuf_new();
115 column
= gtk_tree_view_column_new();
117 gtk_tree_view_column_pack_start(column
, icon_renderer
, FALSE
);
118 gtk_tree_view_column_set_attributes(column
, icon_renderer
, "pixbuf", SYMBOLS_COLUMN_ICON
, NULL
);
119 g_object_set(icon_renderer
, "xalign", 0.0, NULL
);
121 gtk_tree_view_column_pack_start(column
, text_renderer
, TRUE
);
122 gtk_tree_view_column_set_attributes(column
, text_renderer
, "text", SYMBOLS_COLUMN_NAME
, NULL
);
123 g_object_set(text_renderer
, "yalign", 0.5, NULL
);
124 gtk_tree_view_column_set_title(column
, _("Symbols"));
126 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
127 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
129 ui_widget_modify_font_from_string(tree
, interface_prefs
.tagbar_font
);
131 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), GTK_TREE_MODEL(store
));
132 g_object_unref(store
);
134 g_signal_connect(tree
, "button-press-event",
135 G_CALLBACK(sidebar_button_press_cb
), NULL
);
136 g_signal_connect(tree
, "key-press-event",
137 G_CALLBACK(sidebar_key_press_cb
), NULL
);
139 gtk_tree_view_set_show_expanders(GTK_TREE_VIEW(tree
), interface_prefs
.show_symbol_list_expanders
);
140 if (! interface_prefs
.show_symbol_list_expanders
)
141 gtk_tree_view_set_level_indentation(GTK_TREE_VIEW(tree
), 10);
143 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(tree
), SYMBOLS_COLUMN_TOOLTIP
);
145 /* selection handling */
146 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
147 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
148 /* callback for changed selection not necessary, will be handled by button-press-event */
153 on_default_tag_tree_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
156 if (event
->button
== 3)
158 gtk_menu_popup(GTK_MENU(tv
.popup_taglist
), NULL
, NULL
, NULL
, NULL
,
159 event
->button
, event
->time
);
166 static void create_default_tag_tree(void)
168 GtkScrolledWindow
*scrolled_window
= GTK_SCROLLED_WINDOW(tag_window
);
171 /* default_tag_tree is a GtkViewPort with a GtkLabel inside it */
172 tv
.default_tag_tree
= gtk_viewport_new(
173 gtk_scrolled_window_get_hadjustment(scrolled_window
),
174 gtk_scrolled_window_get_vadjustment(scrolled_window
));
175 gtk_viewport_set_shadow_type(GTK_VIEWPORT(tv
.default_tag_tree
), GTK_SHADOW_NONE
);
176 label
= gtk_label_new(_("No symbols 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 if (update
&& doc
!= NULL
)
194 doc
->priv
->tag_tree_dirty
= TRUE
;
196 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
)) != TREEVIEW_SYMBOL
)
197 return; /* don't bother updating symbol tree if we don't see it */
199 /* changes the tree view to the given one, trying not to do useless changes */
200 #define CHANGE_TREE(new_child) \
202 /* only change the tag tree if it's actually not the same (to avoid flickering) and if
203 * it's the one of the current document (to avoid problems when e.g. reloading
204 * configuration files */ \
205 if (child != new_child && doc == document_get_current()) \
208 gtk_container_remove(GTK_CONTAINER(tag_window), child); \
209 gtk_container_add(GTK_CONTAINER(tag_window), new_child); \
213 if (tv
.default_tag_tree
== NULL
)
214 create_default_tag_tree();
216 /* show default empty tag tree if there are no tags */
217 if (doc
== NULL
|| doc
->file_type
== NULL
|| ! filetype_has_tags(doc
->file_type
))
219 CHANGE_TREE(tv
.default_tag_tree
);
223 if (doc
->priv
->tag_tree_dirty
)
224 { /* updating the tag list in the left tag window */
225 if (doc
->priv
->tag_tree
== NULL
)
227 doc
->priv
->tag_store
= gtk_tree_store_new(
228 SYMBOLS_N_COLUMNS
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
, TM_TYPE_TAG
, G_TYPE_STRING
);
229 doc
->priv
->tag_tree
= gtk_tree_view_new();
230 prepare_taglist(doc
->priv
->tag_tree
, doc
->priv
->tag_store
);
231 gtk_widget_show(doc
->priv
->tag_tree
);
232 g_object_ref((gpointer
)doc
->priv
->tag_tree
); /* to hold it after removing */
235 doc
->has_tags
= symbols_recreate_tag_list(doc
, SYMBOLS_SORT_USE_PREVIOUS
);
236 doc
->priv
->tag_tree_dirty
= FALSE
;
241 CHANGE_TREE(doc
->priv
->tag_tree
);
245 CHANGE_TREE(tv
.default_tag_tree
);
252 /* cleverly sorts documents by their short name */
253 static gint
documents_sort_func(GtkTreeModel
*model
, GtkTreeIter
*iter_a
,
254 GtkTreeIter
*iter_b
, gpointer data
)
256 gchar
*key_a
, *key_b
;
257 gchar
*name_a
, *name_b
;
260 gtk_tree_model_get(model
, iter_a
, DOCUMENTS_SHORTNAME
, &name_a
, -1);
261 key_a
= g_utf8_collate_key_for_filename(name_a
, -1);
263 gtk_tree_model_get(model
, iter_b
, DOCUMENTS_SHORTNAME
, &name_b
, -1);
264 key_b
= g_utf8_collate_key_for_filename(name_b
, -1);
266 cmp
= strcmp(key_a
, key_b
);
274 /* does some preparing things to the open files list widget */
275 static void prepare_openfiles(void)
277 GtkCellRenderer
*icon_renderer
;
278 GtkCellRenderer
*text_renderer
;
279 GtkTreeViewColumn
*column
;
280 GtkTreeSelection
*selection
;
281 GtkTreeSortable
*sortable
;
283 tv
.tree_openfiles
= ui_lookup_widget(main_widgets
.window
, "treeview6");
285 /* store the icon and the short filename to show, and the index as reference,
286 * the colour (black/red/green) and the full name for the tooltip */
287 store_openfiles
= gtk_tree_store_new(5, G_TYPE_ICON
, G_TYPE_STRING
,
288 G_TYPE_POINTER
, GDK_TYPE_COLOR
, G_TYPE_STRING
);
289 gtk_tree_view_set_model(GTK_TREE_VIEW(tv
.tree_openfiles
), GTK_TREE_MODEL(store_openfiles
));
291 /* set policy settings for the scolledwindow around the treeview again, because glade
292 * doesn't keep the settings */
293 gtk_scrolled_window_set_policy(
294 GTK_SCROLLED_WINDOW(ui_lookup_widget(main_widgets
.window
, "scrolledwindow7")),
295 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
297 icon_renderer
= gtk_cell_renderer_pixbuf_new();
298 g_object_set(icon_renderer
, "stock-size", GTK_ICON_SIZE_MENU
, NULL
);
299 text_renderer
= gtk_cell_renderer_text_new();
300 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_MIDDLE
, NULL
);
301 column
= gtk_tree_view_column_new();
302 gtk_tree_view_column_pack_start(column
, icon_renderer
, FALSE
);
303 gtk_tree_view_column_set_attributes(column
, icon_renderer
, "gicon", DOCUMENTS_ICON
, NULL
);
304 gtk_tree_view_column_pack_start(column
, text_renderer
, TRUE
);
305 gtk_tree_view_column_set_attributes(column
, text_renderer
, "text", DOCUMENTS_SHORTNAME
,
306 "foreground-gdk", DOCUMENTS_COLOR
, NULL
);
307 gtk_tree_view_append_column(GTK_TREE_VIEW(tv
.tree_openfiles
), column
);
308 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv
.tree_openfiles
), FALSE
);
310 gtk_tree_view_set_search_column(GTK_TREE_VIEW(tv
.tree_openfiles
),
311 DOCUMENTS_SHORTNAME
);
313 /* sort opened filenames in the store_openfiles treeview */
314 sortable
= GTK_TREE_SORTABLE(GTK_TREE_MODEL(store_openfiles
));
315 gtk_tree_sortable_set_sort_func(sortable
, DOCUMENTS_SHORTNAME
, documents_sort_func
, NULL
, NULL
);
316 gtk_tree_sortable_set_sort_column_id(sortable
, DOCUMENTS_SHORTNAME
, GTK_SORT_ASCENDING
);
318 ui_widget_modify_font_from_string(tv
.tree_openfiles
, interface_prefs
.tagbar_font
);
321 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(tv
.tree_openfiles
), DOCUMENTS_FILENAME
);
323 /* selection handling */
324 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
325 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
326 g_object_unref(store_openfiles
);
328 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "button-press-event",
329 G_CALLBACK(sidebar_button_press_cb
), NULL
);
330 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "key-press-event",
331 G_CALLBACK(sidebar_key_press_cb
), NULL
);
335 /* iter should be toplevel */
336 static gboolean
find_tree_iter_dir(GtkTreeIter
*iter
, const gchar
*dir
)
342 if (utils_str_equal(dir
, "."))
343 dir
= GEANY_STRING_UNTITLED
;
345 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
346 g_return_val_if_fail(!doc
, FALSE
);
348 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_SHORTNAME
, &name
, -1);
350 result
= utils_filenamecmp(name
, dir
) == 0;
357 static gboolean
utils_filename_has_prefix(const gchar
*str
, const gchar
*prefix
)
359 gchar
*head
= g_strndup(str
, strlen(prefix
));
360 gboolean ret
= utils_filenamecmp(head
, prefix
) == 0;
367 static gchar
*get_doc_folder(const gchar
*path
)
369 gchar
*tmp_dirname
= g_strdup(path
);
370 gchar
*project_base_path
;
371 gchar
*dirname
= NULL
;
372 const gchar
*home_dir
= g_get_home_dir();
375 /* replace the project base path with the project name */
376 project_base_path
= project_get_base_path();
378 if (project_base_path
!= NULL
)
380 gsize len
= strlen(project_base_path
);
382 /* remove trailing separator so we can match base path exactly */
383 if (project_base_path
[len
-1] == G_DIR_SEPARATOR
)
384 project_base_path
[--len
] = '\0';
386 /* check whether the dir name matches or uses the project base path */
387 if (utils_filename_has_prefix(tmp_dirname
, project_base_path
))
389 rest
= tmp_dirname
+ len
;
390 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
392 dirname
= g_strdup_printf("%s%s", app
->project
->name
, rest
);
395 g_free(project_base_path
);
399 dirname
= tmp_dirname
;
401 /* If matches home dir, replace with tilde */
402 if (!EMPTY(home_dir
) && utils_filename_has_prefix(dirname
, home_dir
))
404 rest
= dirname
+ strlen(home_dir
);
405 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
407 dirname
= g_strdup_printf("~%s", rest
);
419 static GtkTreeIter
*get_doc_parent(GeanyDocument
*doc
)
422 gchar
*dirname
= NULL
;
423 static GtkTreeIter parent
;
424 GtkTreeModel
*model
= GTK_TREE_MODEL(store_openfiles
);
425 static GIcon
*dir_icon
= NULL
;
427 if (!interface_prefs
.documents_show_paths
)
430 path
= g_path_get_dirname(DOC_FILENAME(doc
));
431 dirname
= get_doc_folder(path
);
433 if (gtk_tree_model_get_iter_first(model
, &parent
))
437 if (find_tree_iter_dir(&parent
, dirname
))
444 while (gtk_tree_model_iter_next(model
, &parent
));
446 /* no match, add dir parent */
448 dir_icon
= ui_get_mime_icon("inode/directory");
450 gtk_tree_store_append(store_openfiles
, &parent
, NULL
);
451 gtk_tree_store_set(store_openfiles
, &parent
, DOCUMENTS_ICON
, dir_icon
,
452 DOCUMENTS_FILENAME
, path
,
453 DOCUMENTS_SHORTNAME
, doc
->file_name
? dirname
: GEANY_STRING_UNTITLED
, -1);
461 /* Also sets doc->priv->iter.
462 * This is called recursively in sidebar_openfiles_update_all(). */
463 void sidebar_openfiles_add(GeanyDocument
*doc
)
465 GtkTreeIter
*iter
= &doc
->priv
->iter
;
466 GtkTreeIter
*parent
= get_doc_parent(doc
);
468 const GdkColor
*color
= document_get_status_color(doc
);
469 static GIcon
*file_icon
= NULL
;
471 gtk_tree_store_append(store_openfiles
, iter
, parent
);
473 /* check if new parent */
474 if (parent
&& gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), parent
) == 1)
479 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), parent
);
480 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
481 gtk_tree_path_free(path
);
484 file_icon
= ui_get_mime_icon("text/plain");
486 basename
= g_path_get_basename(DOC_FILENAME(doc
));
487 gtk_tree_store_set(store_openfiles
, iter
,
488 DOCUMENTS_ICON
, (doc
->file_type
&& doc
->file_type
->icon
) ? doc
->file_type
->icon
: file_icon
,
489 DOCUMENTS_SHORTNAME
, basename
, DOCUMENTS_DOCUMENT
, doc
, DOCUMENTS_COLOR
, color
,
490 DOCUMENTS_FILENAME
, DOC_FILENAME(doc
), -1);
495 static void openfiles_remove(GeanyDocument
*doc
)
497 GtkTreeIter
*iter
= &doc
->priv
->iter
;
500 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
) &&
501 gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), &parent
) == 1)
502 gtk_tree_store_remove(store_openfiles
, &parent
);
504 gtk_tree_store_remove(store_openfiles
, iter
);
508 void sidebar_openfiles_update(GeanyDocument
*doc
)
510 GtkTreeIter
*iter
= &doc
->priv
->iter
;
513 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_FILENAME
, &fname
, -1);
515 if (utils_str_equal(fname
, DOC_FILENAME(doc
)))
517 /* just update color and the icon */
518 const GdkColor
*color
= document_get_status_color(doc
);
519 GIcon
*icon
= doc
->file_type
->icon
;
521 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_COLOR
, color
, -1);
523 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_ICON
, icon
, -1);
527 /* path has changed, so remove and re-add */
528 GtkTreeSelection
*treesel
;
531 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
532 sel
= gtk_tree_selection_iter_is_selected(treesel
, &doc
->priv
->iter
);
533 openfiles_remove(doc
);
535 sidebar_openfiles_add(doc
);
537 gtk_tree_selection_select_iter(treesel
, &doc
->priv
->iter
);
543 void sidebar_openfiles_update_all(void)
547 gtk_tree_store_clear(store_openfiles
);
550 sidebar_openfiles_add(documents
[i
]);
555 void sidebar_remove_document(GeanyDocument
*doc
)
557 openfiles_remove(doc
);
559 if (GTK_IS_WIDGET(doc
->priv
->tag_tree
))
561 gtk_widget_destroy(doc
->priv
->tag_tree
); /* make GTK release its references, if any */
562 /* Because it was ref'd in sidebar_update_tag_list, it needs unref'ing */
563 g_object_unref(doc
->priv
->tag_tree
);
564 doc
->priv
->tag_tree
= NULL
;
569 static void on_hide_sidebar(void)
571 ui_prefs
.sidebar_visible
= FALSE
;
572 ui_sidebar_show_hide();
576 static gboolean
on_sidebar_display_symbol_list_show(GtkWidget
*item
)
578 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
579 interface_prefs
.sidebar_symbol_visible
);
584 static gboolean
on_sidebar_display_open_files_show(GtkWidget
*item
)
586 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
587 interface_prefs
.sidebar_openfiles_visible
);
592 void sidebar_add_common_menu_items(GtkMenu
*menu
)
596 item
= gtk_separator_menu_item_new();
597 gtk_widget_show(item
);
598 gtk_container_add(GTK_CONTAINER(menu
), item
);
600 item
= gtk_check_menu_item_new_with_mnemonic(_("Show S_ymbol List"));
601 gtk_container_add(GTK_CONTAINER(menu
), item
);
602 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
603 gtk_widget_show(item
);
604 g_signal_connect(item
, "activate",
605 G_CALLBACK(on_list_symbol_activate
), NULL
);
607 item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Document List"));
608 gtk_container_add(GTK_CONTAINER(menu
), item
);
609 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
610 gtk_widget_show(item
);
611 g_signal_connect(item
, "activate",
612 G_CALLBACK(on_list_document_activate
), NULL
);
614 item
= gtk_image_menu_item_new_with_mnemonic(_("H_ide Sidebar"));
615 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
616 gtk_image_new_from_stock(GTK_STOCK_CLOSE
, GTK_ICON_SIZE_MENU
));
617 gtk_widget_show(item
);
618 gtk_container_add(GTK_CONTAINER(menu
), item
);
619 g_signal_connect(item
, "activate", G_CALLBACK(on_hide_sidebar
), NULL
);
622 static void on_openfiles_show_paths_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
624 interface_prefs
.documents_show_paths
= gtk_check_menu_item_get_active(item
);
625 sidebar_openfiles_update_all();
629 static void on_list_document_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
631 interface_prefs
.sidebar_openfiles_visible
= gtk_check_menu_item_get_active(item
);
632 ui_sidebar_show_hide();
633 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
637 static void on_list_symbol_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
639 interface_prefs
.sidebar_symbol_visible
= gtk_check_menu_item_get_active(item
);
640 ui_sidebar_show_hide();
641 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
645 static void on_find_in_files(GtkMenuItem
*menuitem
, gpointer user_data
)
647 GtkTreeSelection
*treesel
;
653 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
654 if (!gtk_tree_selection_get_selected(treesel
, &model
, &iter
))
656 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
660 gtk_tree_model_get(model
, &iter
, DOCUMENTS_FILENAME
, &dir
, -1);
663 dir
= g_path_get_dirname(DOC_FILENAME(doc
));
665 search_show_find_in_files_dialog(dir
);
670 static void on_openfiles_expand_collapse(GtkMenuItem
*menuitem
, gpointer user_data
)
672 gboolean expand
= GPOINTER_TO_INT(user_data
);
675 gtk_tree_view_expand_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
677 gtk_tree_view_collapse_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
681 static void create_openfiles_popup_menu(void)
685 openfiles_popup_menu
= gtk_menu_new();
687 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE
, NULL
);
688 gtk_widget_show(item
);
689 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
690 g_signal_connect(item
, "activate",
691 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
692 doc_items
.close
= item
;
694 item
= gtk_separator_menu_item_new();
695 gtk_widget_show(item
);
696 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
698 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE
, NULL
);
699 gtk_widget_show(item
);
700 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
701 g_signal_connect(item
, "activate",
702 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_SAVE
));
703 doc_items
.save
= item
;
705 item
= gtk_image_menu_item_new_with_mnemonic(_("_Reload"));
706 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
707 gtk_image_new_from_stock(GTK_STOCK_REVERT_TO_SAVED
, GTK_ICON_SIZE_MENU
));
708 gtk_widget_show(item
);
709 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
710 g_signal_connect(item
, "activate",
711 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_RELOAD
));
712 doc_items
.reload
= item
;
714 item
= gtk_separator_menu_item_new();
715 gtk_widget_show(item
);
716 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
718 item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("_Find in Files..."));
719 gtk_widget_show(item
);
720 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
721 g_signal_connect(item
, "activate", G_CALLBACK(on_find_in_files
), NULL
);
722 doc_items
.find_in_files
= item
;
724 item
= gtk_separator_menu_item_new();
725 gtk_widget_show(item
);
726 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
728 doc_items
.show_paths
= gtk_check_menu_item_new_with_mnemonic(_("Show _Paths"));
729 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), interface_prefs
.documents_show_paths
);
730 gtk_widget_show(doc_items
.show_paths
);
731 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.show_paths
);
732 g_signal_connect(doc_items
.show_paths
, "activate",
733 G_CALLBACK(on_openfiles_show_paths_activate
), NULL
);
735 item
= gtk_separator_menu_item_new();
736 gtk_widget_show(item
);
737 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
739 doc_items
.expand_all
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
740 gtk_widget_show(doc_items
.expand_all
);
741 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.expand_all
);
742 g_signal_connect(doc_items
.expand_all
, "activate",
743 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(TRUE
));
745 doc_items
.collapse_all
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
746 gtk_widget_show(doc_items
.collapse_all
);
747 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.collapse_all
);
748 g_signal_connect(doc_items
.collapse_all
, "activate",
749 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(FALSE
));
751 sidebar_add_common_menu_items(GTK_MENU(openfiles_popup_menu
));
755 static void unfold_parent(GtkTreeIter
*iter
)
759 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
))
761 GtkTreePath
*path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), &parent
);
762 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
763 gtk_tree_path_free(path
);
768 /* compares the given data with the doc pointer from the selected row of openfiles
769 * treeview, in case of a match the row is selected and TRUE is returned
770 * (called indirectly from gtk_tree_model_foreach()) */
771 static gboolean
tree_model_find_node(GtkTreeModel
*model
, GtkTreePath
*path
,
772 GtkTreeIter
*iter
, gpointer data
)
776 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
780 /* unfolding also prevents a strange bug where the selection gets stuck on the parent
781 * when it is collapsed and then switching documents */
783 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, NULL
, FALSE
);
790 void sidebar_select_openfiles_item(GeanyDocument
*doc
)
792 gtk_tree_model_foreach(GTK_TREE_MODEL(store_openfiles
), tree_model_find_node
, doc
);
798 static void document_action(GeanyDocument
*doc
, gint action
)
800 if (! DOC_VALID(doc
))
805 case OPENFILES_ACTION_REMOVE
:
810 case OPENFILES_ACTION_SAVE
:
812 document_save_file(doc
, FALSE
);
815 case OPENFILES_ACTION_RELOAD
:
817 document_reload_prompt(doc
, NULL
);
824 static void on_openfiles_document_action(GtkMenuItem
*menuitem
, gpointer user_data
)
827 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
830 gint action
= GPOINTER_TO_INT(user_data
);
832 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
834 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
837 document_action(doc
, action
);
841 /* parent item selected */
843 gint i
= gtk_tree_model_iter_n_children(model
, &iter
) - 1;
845 while (i
>= 0 && gtk_tree_model_iter_nth_child(model
, &child
, &iter
, i
))
847 gtk_tree_model_get(model
, &child
, DOCUMENTS_DOCUMENT
, &doc
, -1);
849 document_action(doc
, action
);
857 static void change_focus_to_editor(GeanyDocument
*doc
, GtkWidget
*source_widget
)
860 document_try_focus(doc
, source_widget
);
861 may_steal_focus
= FALSE
;
865 static gboolean
openfiles_go_to_selection(GtkTreeSelection
*selection
, guint keyval
)
869 GeanyDocument
*doc
= NULL
;
871 /* use switch_notebook_page to ignore changing the notebook page because it is already done */
872 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
) && ! ignore_callback
)
874 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
876 return FALSE
; /* parent */
878 /* switch to the doc and grab the focus */
879 document_show_tab(doc
);
880 if (keyval
!= GDK_KEY_space
)
881 change_focus_to_editor(doc
, tv
.tree_openfiles
);
887 static gboolean
taglist_go_to_selection(GtkTreeSelection
*selection
, guint keyval
, guint state
)
892 gboolean handled
= TRUE
;
894 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
898 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
905 GeanyDocument
*doc
= document_get_current();
909 navqueue_goto_line(doc
, doc
, line
);
910 state
= keybindings_get_modifiers(state
);
911 if (keyval
!= GDK_KEY_space
&& ! (state
& GEANY_PRIMARY_MOD_MASK
))
912 change_focus_to_editor(doc
, NULL
);
923 static gboolean
sidebar_key_press_cb(GtkWidget
*widget
, GdkEventKey
*event
,
926 may_steal_focus
= FALSE
;
927 if (ui_is_keyval_enter_or_return(event
->keyval
) || event
->keyval
== GDK_KEY_space
)
929 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
930 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
931 may_steal_focus
= TRUE
;
933 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
934 * doing so will prevent further handlers to be run in most cases, but the only one is our
935 * own, so guess it's fine. */
936 if (widget_class
->key_press_event
)
937 widget_class
->key_press_event(widget
, event
);
939 if (widget
== tv
.tree_openfiles
) /* tag and doc list have separate handlers */
940 openfiles_go_to_selection(selection
, event
->keyval
);
942 taglist_go_to_selection(selection
, event
->keyval
, event
->state
);
950 static gboolean
sidebar_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
951 G_GNUC_UNUSED gpointer user_data
)
953 GtkTreeSelection
*selection
;
954 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
955 gboolean handled
= FALSE
;
957 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
958 * doing so will prevent further handlers to be run in most cases, but the only one is our own,
959 * so guess it's fine. */
960 if (widget_class
->button_press_event
)
961 handled
= widget_class
->button_press_event(widget
, event
);
963 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
964 may_steal_focus
= TRUE
;
966 if (event
->type
== GDK_2BUTTON_PRESS
)
967 { /* double click on parent node(section) expands/collapses it */
971 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
973 if (gtk_tree_model_iter_has_child(model
, &iter
))
975 GtkTreePath
*path
= gtk_tree_model_get_path(model
, &iter
);
977 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget
), path
))
978 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget
), path
);
980 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget
), path
, FALSE
);
982 gtk_tree_path_free(path
);
987 else if (event
->button
== 1)
988 { /* allow reclicking of taglist treeview item */
989 if (widget
== tv
.tree_openfiles
)
991 openfiles_go_to_selection(selection
, 0);
995 handled
= taglist_go_to_selection(selection
, 0, event
->state
);
997 else if (event
->button
== 2)
999 if (widget
== tv
.tree_openfiles
)
1000 on_openfiles_document_action(NULL
, GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
1002 else if (event
->button
== 3)
1004 if (widget
== tv
.tree_openfiles
)
1006 if (!openfiles_popup_menu
)
1007 create_openfiles_popup_menu();
1009 /* update menu item sensitivity */
1010 documents_menu_update(selection
);
1011 gtk_menu_popup(GTK_MENU(openfiles_popup_menu
), NULL
, NULL
, NULL
, NULL
,
1012 event
->button
, event
->time
);
1016 gtk_menu_popup(GTK_MENU(tv
.popup_taglist
), NULL
, NULL
, NULL
, NULL
,
1017 event
->button
, event
->time
);
1025 static void documents_menu_update(GtkTreeSelection
*selection
)
1027 GtkTreeModel
*model
;
1030 gchar
*shortname
= NULL
;
1031 GeanyDocument
*doc
= NULL
;
1033 /* maybe no selection e.g. if ctrl-click deselected */
1034 sel
= gtk_tree_selection_get_selected(selection
, &model
, &iter
);
1037 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
,
1038 DOCUMENTS_SHORTNAME
, &shortname
, -1);
1040 path
= !EMPTY(shortname
) &&
1041 (g_path_is_absolute(shortname
) ||
1042 (app
->project
&& g_str_has_prefix(shortname
, app
->project
->name
)));
1044 /* can close all, save all (except shortname), but only reload individually ATM */
1045 gtk_widget_set_sensitive(doc_items
.close
, sel
);
1046 gtk_widget_set_sensitive(doc_items
.save
, (doc
&& doc
->real_path
) || path
);
1047 gtk_widget_set_sensitive(doc_items
.reload
, doc
&& doc
->real_path
);
1048 gtk_widget_set_sensitive(doc_items
.find_in_files
, sel
);
1051 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), interface_prefs
.documents_show_paths
);
1052 gtk_widget_set_sensitive(doc_items
.expand_all
, interface_prefs
.documents_show_paths
);
1053 gtk_widget_set_sensitive(doc_items
.collapse_all
, interface_prefs
.documents_show_paths
);
1057 static StashGroup
*stash_group
= NULL
;
1059 static void on_load_settings(void)
1061 tag_window
= ui_lookup_widget(main_widgets
.window
, "scrolledwindow2");
1063 prepare_openfiles();
1064 /* note: ui_prefs.sidebar_page is reapplied after plugins are loaded */
1065 stash_group_display(stash_group
, NULL
);
1066 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1070 static void on_save_settings(void)
1072 stash_group_update(stash_group
, NULL
);
1073 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1077 static void on_sidebar_switch_page(GtkNotebook
*notebook
,
1078 gpointer page
, guint page_num
, gpointer user_data
)
1080 if (page_num
== TREEVIEW_SYMBOL
)
1081 sidebar_update_tag_list(document_get_current(), FALSE
);
1085 void sidebar_init(void)
1089 group
= stash_group_new(PACKAGE
);
1090 stash_group_add_widget_property(group
, &ui_prefs
.sidebar_page
, "sidebar_page", GINT_TO_POINTER(0),
1091 main_widgets
.sidebar_notebook
, "page", 0);
1092 configuration_add_session_group(group
, FALSE
);
1093 stash_group
= group
;
1095 /* delay building documents treeview until sidebar font has been read */
1096 g_signal_connect(geany_object
, "load-settings", on_load_settings
, NULL
);
1097 g_signal_connect(geany_object
, "save-settings", on_save_settings
, NULL
);
1099 g_signal_connect(main_widgets
.sidebar_notebook
, "page-added",
1100 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1101 g_signal_connect(main_widgets
.sidebar_notebook
, "page-removed",
1102 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1103 /* tabs may have changed when sidebar is reshown */
1104 g_signal_connect(main_widgets
.sidebar_notebook
, "show",
1105 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1106 g_signal_connect_after(main_widgets
.sidebar_notebook
, "switch-page",
1107 G_CALLBACK(on_sidebar_switch_page
), NULL
);
1109 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1112 #define WIDGET(w) w && GTK_IS_WIDGET(w)
1114 void sidebar_finalize(void)
1116 if (WIDGET(tv
.default_tag_tree
))
1118 gtk_widget_destroy(tv
.default_tag_tree
); /* make GTK release its references, if any... */
1119 g_object_unref(tv
.default_tag_tree
); /* ...and release our own */
1121 if (WIDGET(tv
.popup_taglist
))
1122 gtk_widget_destroy(tv
.popup_taglist
);
1123 if (WIDGET(openfiles_popup_menu
))
1124 gtk_widget_destroy(openfiles_popup_menu
);
1128 void sidebar_focus_openfiles_tab(void)
1130 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_openfiles_visible
)
1132 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1134 gtk_notebook_set_current_page(notebook
, TREEVIEW_OPENFILES
);
1135 gtk_widget_grab_focus(tv
.tree_openfiles
);
1140 void sidebar_focus_symbols_tab(void)
1142 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_symbol_visible
)
1144 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1145 GtkWidget
*symbol_list_scrollwin
= gtk_notebook_get_nth_page(notebook
, TREEVIEW_SYMBOL
);
1147 gtk_notebook_set_current_page(notebook
, TREEVIEW_SYMBOL
);
1148 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(symbol_list_scrollwin
)));
1153 static void sidebar_tabs_show_hide(GtkNotebook
*notebook
, GtkWidget
*child
,
1154 guint page_num
, gpointer data
)
1156 gint tabs
= gtk_notebook_get_n_pages(notebook
);
1158 if (interface_prefs
.sidebar_symbol_visible
== FALSE
)
1160 if (interface_prefs
.sidebar_openfiles_visible
== FALSE
)
1163 gtk_notebook_set_show_tabs(notebook
, (tabs
> 1));