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.
30 #include "callbacks.h"
34 #include "documentprivate.h"
35 #include "filetypes.h"
43 #include "sciwrappers.h"
46 #include <gdk/gdkkeysyms.h>
49 SidebarTreeviews tv
= {NULL
, NULL
, NULL
};
50 /* while typeahead searching, editor should not get focus */
51 static gboolean may_steal_focus
= FALSE
;
58 GtkWidget
*show_paths
;
59 GtkWidget
*find_in_files
;
60 GtkWidget
*expand_all
;
61 GtkWidget
*collapse_all
;
63 doc_items
= {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
73 OPENFILES_ACTION_REMOVE
= 0,
74 OPENFILES_ACTION_SAVE
,
75 OPENFILES_ACTION_RELOAD
78 /* documents tree model columns */
82 DOCUMENTS_SHORTNAME
, /* dirname for parents, basename for children */
85 DOCUMENTS_FILENAME
/* full filename */
88 static GtkTreeStore
*store_openfiles
;
89 static GtkWidget
*openfiles_popup_menu
;
90 static gboolean documents_show_paths
;
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 gtk_tree_view_set_tooltip_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 label
= gtk_label_new(_("No tags found"));
176 gtk_misc_set_alignment(GTK_MISC(label
), 0.1f
, 0.01f
);
177 gtk_container_add(GTK_CONTAINER(tv
.default_tag_tree
), label
);
178 gtk_widget_show_all(tv
.default_tag_tree
);
179 g_signal_connect(tv
.default_tag_tree
, "button-press-event",
180 G_CALLBACK(on_default_tag_tree_button_press_event
), NULL
);
181 g_object_ref((gpointer
)tv
.default_tag_tree
); /* to hold it after removing */
185 /* update = rescan the tags for doc->filename */
186 void sidebar_update_tag_list(GeanyDocument
*doc
, gboolean update
)
188 GtkWidget
*child
= gtk_bin_get_child(GTK_BIN(tag_window
));
190 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
192 /* changes the tree view to the given one, trying not to do useless changes */
193 #define CHANGE_TREE(new_child) \
195 /* only change the tag tree if it's actually not the same (to avoid flickering) and if
196 * it's the one of the current document (to avoid problems when e.g. reloading
197 * configuration files */ \
198 if (child != new_child && doc == document_get_current()) \
201 gtk_container_remove(GTK_CONTAINER(tag_window), child); \
202 gtk_container_add(GTK_CONTAINER(tag_window), new_child); \
206 if (tv
.default_tag_tree
== NULL
)
207 create_default_tag_tree();
209 /* show default empty tag tree if there are no tags */
210 if (doc
== NULL
|| doc
->file_type
== NULL
|| ! filetype_has_tags(doc
->file_type
))
212 CHANGE_TREE(tv
.default_tag_tree
);
217 { /* updating the tag list in the left tag window */
218 if (doc
->priv
->tag_tree
== NULL
)
220 doc
->priv
->tag_store
= gtk_tree_store_new(
221 SYMBOLS_N_COLUMNS
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
, TM_TYPE_TAG
, G_TYPE_STRING
);
222 doc
->priv
->tag_tree
= gtk_tree_view_new();
223 prepare_taglist(doc
->priv
->tag_tree
, doc
->priv
->tag_store
);
224 gtk_widget_show(doc
->priv
->tag_tree
);
225 g_object_ref((gpointer
)doc
->priv
->tag_tree
); /* to hold it after removing */
228 doc
->has_tags
= symbols_recreate_tag_list(doc
, SYMBOLS_SORT_USE_PREVIOUS
);
233 CHANGE_TREE(doc
->priv
->tag_tree
);
237 CHANGE_TREE(tv
.default_tag_tree
);
244 /* cleverly sorts documents by their short name */
245 static gint
documents_sort_func(GtkTreeModel
*model
, GtkTreeIter
*iter_a
,
246 GtkTreeIter
*iter_b
, gpointer data
)
248 gchar
*key_a
, *key_b
;
249 gchar
*name_a
, *name_b
;
252 gtk_tree_model_get(model
, iter_a
, DOCUMENTS_SHORTNAME
, &name_a
, -1);
253 key_a
= g_utf8_collate_key_for_filename(name_a
, -1);
255 gtk_tree_model_get(model
, iter_b
, DOCUMENTS_SHORTNAME
, &name_b
, -1);
256 key_b
= g_utf8_collate_key_for_filename(name_b
, -1);
258 cmp
= strcmp(key_a
, key_b
);
266 /* does some preparing things to the open files list widget */
267 static void prepare_openfiles(void)
269 GtkCellRenderer
*icon_renderer
;
270 GtkCellRenderer
*text_renderer
;
271 GtkTreeViewColumn
*column
;
272 GtkTreeSelection
*selection
;
273 GtkTreeSortable
*sortable
;
275 tv
.tree_openfiles
= ui_lookup_widget(main_widgets
.window
, "treeview6");
277 /* store the icon and the short filename to show, and the index as reference,
278 * the colour (black/red/green) and the full name for the tooltip */
279 store_openfiles
= gtk_tree_store_new(5, G_TYPE_ICON
, G_TYPE_STRING
,
280 G_TYPE_POINTER
, GDK_TYPE_COLOR
, G_TYPE_STRING
);
281 gtk_tree_view_set_model(GTK_TREE_VIEW(tv
.tree_openfiles
), GTK_TREE_MODEL(store_openfiles
));
283 /* set policy settings for the scolledwindow around the treeview again, because glade
284 * doesn't keep the settings */
285 gtk_scrolled_window_set_policy(
286 GTK_SCROLLED_WINDOW(ui_lookup_widget(main_widgets
.window
, "scrolledwindow7")),
287 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
289 icon_renderer
= gtk_cell_renderer_pixbuf_new();
290 g_object_set(icon_renderer
, "stock-size", GTK_ICON_SIZE_MENU
, NULL
);
291 text_renderer
= gtk_cell_renderer_text_new();
292 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_MIDDLE
, NULL
);
293 column
= gtk_tree_view_column_new();
294 gtk_tree_view_column_pack_start(column
, icon_renderer
, FALSE
);
295 gtk_tree_view_column_set_attributes(column
, icon_renderer
, "gicon", DOCUMENTS_ICON
, NULL
);
296 gtk_tree_view_column_pack_start(column
, text_renderer
, TRUE
);
297 gtk_tree_view_column_set_attributes(column
, text_renderer
, "text", DOCUMENTS_SHORTNAME
,
298 "foreground-gdk", DOCUMENTS_COLOR
, NULL
);
299 gtk_tree_view_append_column(GTK_TREE_VIEW(tv
.tree_openfiles
), column
);
300 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv
.tree_openfiles
), FALSE
);
302 gtk_tree_view_set_search_column(GTK_TREE_VIEW(tv
.tree_openfiles
),
303 DOCUMENTS_SHORTNAME
);
305 /* sort opened filenames in the store_openfiles treeview */
306 sortable
= GTK_TREE_SORTABLE(GTK_TREE_MODEL(store_openfiles
));
307 gtk_tree_sortable_set_sort_func(sortable
, DOCUMENTS_SHORTNAME
, documents_sort_func
, NULL
, NULL
);
308 gtk_tree_sortable_set_sort_column_id(sortable
, DOCUMENTS_SHORTNAME
, GTK_SORT_ASCENDING
);
310 ui_widget_modify_font_from_string(tv
.tree_openfiles
, interface_prefs
.tagbar_font
);
313 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(tv
.tree_openfiles
), DOCUMENTS_FILENAME
);
315 /* selection handling */
316 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
317 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
318 g_object_unref(store_openfiles
);
320 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "button-press-event",
321 G_CALLBACK(sidebar_button_press_cb
), NULL
);
322 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "key-press-event",
323 G_CALLBACK(sidebar_key_press_cb
), NULL
);
327 /* iter should be toplevel */
328 static gboolean
find_tree_iter_dir(GtkTreeIter
*iter
, const gchar
*dir
)
334 if (utils_str_equal(dir
, "."))
335 dir
= GEANY_STRING_UNTITLED
;
337 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
338 g_return_val_if_fail(!doc
, FALSE
);
340 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_SHORTNAME
, &name
, -1);
342 result
= utils_filenamecmp(name
, dir
) == 0;
349 static gboolean
utils_filename_has_prefix(const gchar
*str
, const gchar
*prefix
)
351 gchar
*head
= g_strndup(str
, strlen(prefix
));
352 gboolean ret
= utils_filenamecmp(head
, prefix
) == 0;
359 static gchar
*get_doc_folder(const gchar
*path
)
361 gchar
*tmp_dirname
= g_strdup(path
);
362 gchar
*project_base_path
;
363 gchar
*dirname
= NULL
;
364 const gchar
*home_dir
= g_get_home_dir();
367 /* replace the project base path with the project name */
368 project_base_path
= project_get_base_path();
370 if (project_base_path
!= NULL
)
372 gsize len
= strlen(project_base_path
);
374 /* remove trailing separator so we can match base path exactly */
375 if (project_base_path
[len
-1] == G_DIR_SEPARATOR
)
376 project_base_path
[--len
] = '\0';
378 /* check whether the dir name matches or uses the project base path */
379 if (utils_filename_has_prefix(tmp_dirname
, project_base_path
))
381 rest
= tmp_dirname
+ len
;
382 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
384 dirname
= g_strdup_printf("%s%s", app
->project
->name
, rest
);
387 g_free(project_base_path
);
391 dirname
= tmp_dirname
;
393 /* If matches home dir, replace with tilde */
394 if (!EMPTY(home_dir
) && utils_filename_has_prefix(dirname
, home_dir
))
396 rest
= dirname
+ strlen(home_dir
);
397 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
399 dirname
= g_strdup_printf("~%s", rest
);
411 static GtkTreeIter
*get_doc_parent(GeanyDocument
*doc
)
414 gchar
*dirname
= NULL
;
415 static GtkTreeIter parent
;
416 GtkTreeModel
*model
= GTK_TREE_MODEL(store_openfiles
);
417 static GIcon
*dir_icon
= NULL
;
419 if (!documents_show_paths
)
422 path
= g_path_get_dirname(DOC_FILENAME(doc
));
423 dirname
= get_doc_folder(path
);
425 if (gtk_tree_model_get_iter_first(model
, &parent
))
429 if (find_tree_iter_dir(&parent
, dirname
))
436 while (gtk_tree_model_iter_next(model
, &parent
));
438 /* no match, add dir parent */
440 dir_icon
= ui_get_mime_icon("inode/directory");
442 gtk_tree_store_append(store_openfiles
, &parent
, NULL
);
443 gtk_tree_store_set(store_openfiles
, &parent
, DOCUMENTS_ICON
, dir_icon
,
444 DOCUMENTS_FILENAME
, path
,
445 DOCUMENTS_SHORTNAME
, doc
->file_name
? dirname
: GEANY_STRING_UNTITLED
, -1);
453 /* Also sets doc->priv->iter.
454 * This is called recursively in sidebar_openfiles_update_all(). */
455 void sidebar_openfiles_add(GeanyDocument
*doc
)
457 GtkTreeIter
*iter
= &doc
->priv
->iter
;
458 GtkTreeIter
*parent
= get_doc_parent(doc
);
460 const GdkColor
*color
= document_get_status_color(doc
);
461 static GIcon
*file_icon
= NULL
;
463 gtk_tree_store_append(store_openfiles
, iter
, parent
);
465 /* check if new parent */
466 if (parent
&& gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), parent
) == 1)
471 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), parent
);
472 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
473 gtk_tree_path_free(path
);
476 file_icon
= ui_get_mime_icon("text/plain");
478 basename
= g_path_get_basename(DOC_FILENAME(doc
));
479 gtk_tree_store_set(store_openfiles
, iter
,
480 DOCUMENTS_ICON
, (doc
->file_type
&& doc
->file_type
->icon
) ? doc
->file_type
->icon
: file_icon
,
481 DOCUMENTS_SHORTNAME
, basename
, DOCUMENTS_DOCUMENT
, doc
, DOCUMENTS_COLOR
, color
,
482 DOCUMENTS_FILENAME
, DOC_FILENAME(doc
), -1);
487 static void openfiles_remove(GeanyDocument
*doc
)
489 GtkTreeIter
*iter
= &doc
->priv
->iter
;
492 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
) &&
493 gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), &parent
) == 1)
494 gtk_tree_store_remove(store_openfiles
, &parent
);
496 gtk_tree_store_remove(store_openfiles
, iter
);
500 void sidebar_openfiles_update(GeanyDocument
*doc
)
502 GtkTreeIter
*iter
= &doc
->priv
->iter
;
505 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_FILENAME
, &fname
, -1);
507 if (utils_str_equal(fname
, DOC_FILENAME(doc
)))
509 /* just update color and the icon */
510 const GdkColor
*color
= document_get_status_color(doc
);
511 GIcon
*icon
= doc
->file_type
->icon
;
513 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_COLOR
, color
, -1);
515 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_ICON
, icon
, -1);
519 /* path has changed, so remove and re-add */
520 GtkTreeSelection
*treesel
;
523 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
524 sel
= gtk_tree_selection_iter_is_selected(treesel
, &doc
->priv
->iter
);
525 openfiles_remove(doc
);
527 sidebar_openfiles_add(doc
);
529 gtk_tree_selection_select_iter(treesel
, &doc
->priv
->iter
);
535 void sidebar_openfiles_update_all(void)
539 gtk_tree_store_clear(store_openfiles
);
542 sidebar_openfiles_add(documents
[i
]);
547 void sidebar_remove_document(GeanyDocument
*doc
)
549 openfiles_remove(doc
);
551 if (GTK_IS_WIDGET(doc
->priv
->tag_tree
))
553 gtk_widget_destroy(doc
->priv
->tag_tree
); /* make GTK release its references, if any */
554 /* Because it was ref'd in sidebar_update_tag_list, it needs unref'ing */
555 g_object_unref(doc
->priv
->tag_tree
);
556 doc
->priv
->tag_tree
= NULL
;
561 static void on_hide_sidebar(void)
563 ui_prefs
.sidebar_visible
= FALSE
;
564 ui_sidebar_show_hide();
568 static gboolean
on_sidebar_display_symbol_list_show(GtkWidget
*item
)
570 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
571 interface_prefs
.sidebar_symbol_visible
);
576 static gboolean
on_sidebar_display_open_files_show(GtkWidget
*item
)
578 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
579 interface_prefs
.sidebar_openfiles_visible
);
584 void sidebar_add_common_menu_items(GtkMenu
*menu
)
588 item
= gtk_separator_menu_item_new();
589 gtk_widget_show(item
);
590 gtk_container_add(GTK_CONTAINER(menu
), item
);
592 item
= gtk_check_menu_item_new_with_mnemonic(_("Show S_ymbol List"));
593 gtk_container_add(GTK_CONTAINER(menu
), item
);
594 #if GTK_CHECK_VERSION(3, 0, 0)
595 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
597 g_signal_connect(item
, "expose-event",
598 G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
600 gtk_widget_show(item
);
601 g_signal_connect(item
, "activate",
602 G_CALLBACK(on_list_symbol_activate
), NULL
);
604 item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Document List"));
605 gtk_container_add(GTK_CONTAINER(menu
), item
);
606 #if GTK_CHECK_VERSION(3, 0, 0)
607 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
609 g_signal_connect(item
, "expose-event",
610 G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
612 gtk_widget_show(item
);
613 g_signal_connect(item
, "activate",
614 G_CALLBACK(on_list_document_activate
), NULL
);
616 item
= gtk_image_menu_item_new_with_mnemonic(_("H_ide Sidebar"));
617 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
618 gtk_image_new_from_stock(GTK_STOCK_CLOSE
, GTK_ICON_SIZE_MENU
));
619 gtk_widget_show(item
);
620 gtk_container_add(GTK_CONTAINER(menu
), item
);
621 g_signal_connect(item
, "activate", G_CALLBACK(on_hide_sidebar
), NULL
);
625 static void on_openfiles_show_paths_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
627 documents_show_paths
= gtk_check_menu_item_get_active(item
);
628 sidebar_openfiles_update_all();
632 static void on_list_document_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
634 interface_prefs
.sidebar_openfiles_visible
= gtk_check_menu_item_get_active(item
);
635 ui_sidebar_show_hide();
636 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
640 static void on_list_symbol_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
642 interface_prefs
.sidebar_symbol_visible
= gtk_check_menu_item_get_active(item
);
643 ui_sidebar_show_hide();
644 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
648 static void on_find_in_files(GtkMenuItem
*menuitem
, gpointer user_data
)
650 GtkTreeSelection
*treesel
;
656 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
657 if (!gtk_tree_selection_get_selected(treesel
, &model
, &iter
))
659 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
663 gtk_tree_model_get(model
, &iter
, DOCUMENTS_FILENAME
, &dir
, -1);
666 dir
= g_path_get_dirname(DOC_FILENAME(doc
));
668 search_show_find_in_files_dialog(dir
);
673 static void on_openfiles_expand_collapse(GtkMenuItem
*menuitem
, gpointer user_data
)
675 gboolean expand
= GPOINTER_TO_INT(user_data
);
678 gtk_tree_view_expand_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
680 gtk_tree_view_collapse_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
684 static void create_openfiles_popup_menu(void)
688 openfiles_popup_menu
= gtk_menu_new();
690 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE
, NULL
);
691 gtk_widget_show(item
);
692 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
693 g_signal_connect(item
, "activate",
694 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
695 doc_items
.close
= item
;
697 item
= gtk_separator_menu_item_new();
698 gtk_widget_show(item
);
699 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
701 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE
, NULL
);
702 gtk_widget_show(item
);
703 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
704 g_signal_connect(item
, "activate",
705 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_SAVE
));
706 doc_items
.save
= item
;
708 item
= gtk_image_menu_item_new_with_mnemonic(_("_Reload"));
709 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
710 gtk_image_new_from_stock(GTK_STOCK_REVERT_TO_SAVED
, GTK_ICON_SIZE_MENU
));
711 gtk_widget_show(item
);
712 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
713 g_signal_connect(item
, "activate",
714 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_RELOAD
));
715 doc_items
.reload
= item
;
717 item
= gtk_separator_menu_item_new();
718 gtk_widget_show(item
);
719 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
721 item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("_Find in Files..."));
722 gtk_widget_show(item
);
723 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
724 g_signal_connect(item
, "activate", G_CALLBACK(on_find_in_files
), NULL
);
725 doc_items
.find_in_files
= item
;
727 item
= gtk_separator_menu_item_new();
728 gtk_widget_show(item
);
729 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
731 doc_items
.show_paths
= gtk_check_menu_item_new_with_mnemonic(_("Show _Paths"));
732 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), documents_show_paths
);
733 gtk_widget_show(doc_items
.show_paths
);
734 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.show_paths
);
735 g_signal_connect(doc_items
.show_paths
, "activate",
736 G_CALLBACK(on_openfiles_show_paths_activate
), NULL
);
738 item
= gtk_separator_menu_item_new();
739 gtk_widget_show(item
);
740 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
742 doc_items
.expand_all
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
743 gtk_widget_show(doc_items
.expand_all
);
744 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.expand_all
);
745 g_signal_connect(doc_items
.expand_all
, "activate",
746 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(TRUE
));
748 doc_items
.collapse_all
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
749 gtk_widget_show(doc_items
.collapse_all
);
750 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.collapse_all
);
751 g_signal_connect(doc_items
.collapse_all
, "activate",
752 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(FALSE
));
754 sidebar_add_common_menu_items(GTK_MENU(openfiles_popup_menu
));
758 static void unfold_parent(GtkTreeIter
*iter
)
763 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
))
765 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), &parent
);
766 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
767 gtk_tree_path_free(path
);
772 /* compares the given data with the doc pointer from the selected row of openfiles
773 * treeview, in case of a match the row is selected and TRUE is returned
774 * (called indirectly from gtk_tree_model_foreach()) */
775 static gboolean
tree_model_find_node(GtkTreeModel
*model
, GtkTreePath
*path
,
776 GtkTreeIter
*iter
, gpointer data
)
780 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
784 /* unfolding also prevents a strange bug where the selection gets stuck on the parent
785 * when it is collapsed and then switching documents */
787 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, NULL
, FALSE
);
794 void sidebar_select_openfiles_item(GeanyDocument
*doc
)
796 gtk_tree_model_foreach(GTK_TREE_MODEL(store_openfiles
), tree_model_find_node
, doc
);
802 static void document_action(GeanyDocument
*doc
, gint action
)
804 if (! DOC_VALID(doc
))
809 case OPENFILES_ACTION_REMOVE
:
814 case OPENFILES_ACTION_SAVE
:
816 document_save_file(doc
, FALSE
);
819 case OPENFILES_ACTION_RELOAD
:
821 on_toolbutton_reload_clicked(NULL
, NULL
);
828 static void on_openfiles_document_action(GtkMenuItem
*menuitem
, gpointer user_data
)
831 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
834 gint action
= GPOINTER_TO_INT(user_data
);
836 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
838 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
841 document_action(doc
, action
);
845 /* parent item selected */
847 gint i
= gtk_tree_model_iter_n_children(model
, &iter
) - 1;
849 while (i
>= 0 && gtk_tree_model_iter_nth_child(model
, &child
, &iter
, i
))
851 gtk_tree_model_get(model
, &child
, DOCUMENTS_DOCUMENT
, &doc
, -1);
853 document_action(doc
, action
);
861 static void change_focus_to_editor(GeanyDocument
*doc
, GtkWidget
*source_widget
)
864 document_try_focus(doc
, source_widget
);
865 may_steal_focus
= FALSE
;
869 static gboolean
openfiles_go_to_selection(GtkTreeSelection
*selection
, guint keyval
)
873 GeanyDocument
*doc
= NULL
;
875 /* use switch_notebook_page to ignore changing the notebook page because it is already done */
876 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
) && ! ignore_callback
)
878 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
880 return FALSE
; /* parent */
882 /* switch to the doc and grab the focus */
883 document_show_tab(doc
);
884 if (keyval
!= GDK_space
)
885 change_focus_to_editor(doc
, tv
.tree_openfiles
);
891 static gboolean
taglist_go_to_selection(GtkTreeSelection
*selection
, guint keyval
, guint state
)
896 gboolean handled
= TRUE
;
898 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
902 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
906 line
= tag
->atts
.entry
.line
;
909 GeanyDocument
*doc
= document_get_current();
913 navqueue_goto_line(doc
, doc
, line
);
914 if (keyval
!= GDK_space
&& ! (state
& GDK_CONTROL_MASK
))
915 change_focus_to_editor(doc
, NULL
);
926 static gboolean
sidebar_key_press_cb(GtkWidget
*widget
, GdkEventKey
*event
,
929 may_steal_focus
= FALSE
;
930 if (ui_is_keyval_enter_or_return(event
->keyval
) || event
->keyval
== GDK_space
)
932 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
933 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
934 may_steal_focus
= TRUE
;
936 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
937 * doing so will prevent further handlers to be run in most cases, but the only one is our
938 * own, so guess it's fine. */
939 if (widget_class
->key_press_event
)
940 widget_class
->key_press_event(widget
, event
);
942 if (widget
== tv
.tree_openfiles
) /* tag and doc list have separate handlers */
943 openfiles_go_to_selection(selection
, event
->keyval
);
945 taglist_go_to_selection(selection
, event
->keyval
, event
->state
);
953 static gboolean
sidebar_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
954 G_GNUC_UNUSED gpointer user_data
)
956 GtkTreeSelection
*selection
;
957 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
958 gboolean handled
= FALSE
;
960 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
961 * doing so will prevent further handlers to be run in most cases, but the only one is our own,
962 * so guess it's fine. */
963 if (widget_class
->button_press_event
)
964 handled
= widget_class
->button_press_event(widget
, event
);
966 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
967 may_steal_focus
= TRUE
;
969 if (event
->type
== GDK_2BUTTON_PRESS
)
970 { /* double click on parent node(section) expands/collapses it */
974 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
976 if (gtk_tree_model_iter_has_child(model
, &iter
))
978 GtkTreePath
*path
= gtk_tree_model_get_path(model
, &iter
);
980 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget
), path
))
981 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget
), path
);
983 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget
), path
, FALSE
);
985 gtk_tree_path_free(path
);
990 else if (event
->button
== 1)
991 { /* allow reclicking of taglist treeview item */
992 if (widget
== tv
.tree_openfiles
)
994 openfiles_go_to_selection(selection
, 0);
998 handled
= taglist_go_to_selection(selection
, 0, event
->state
);
1000 else if (event
->button
== 2)
1002 if (widget
== tv
.tree_openfiles
)
1003 on_openfiles_document_action(NULL
, GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
1005 else if (event
->button
== 3)
1007 if (widget
== tv
.tree_openfiles
)
1009 if (!openfiles_popup_menu
)
1010 create_openfiles_popup_menu();
1012 /* update menu item sensitivity */
1013 documents_menu_update(selection
);
1014 gtk_menu_popup(GTK_MENU(openfiles_popup_menu
), NULL
, NULL
, NULL
, NULL
,
1015 event
->button
, event
->time
);
1019 gtk_menu_popup(GTK_MENU(tv
.popup_taglist
), NULL
, NULL
, NULL
, NULL
,
1020 event
->button
, event
->time
);
1028 static void documents_menu_update(GtkTreeSelection
*selection
)
1030 GtkTreeModel
*model
;
1033 gchar
*shortname
= NULL
;
1034 GeanyDocument
*doc
= NULL
;
1036 /* maybe no selection e.g. if ctrl-click deselected */
1037 sel
= gtk_tree_selection_get_selected(selection
, &model
, &iter
);
1040 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
,
1041 DOCUMENTS_SHORTNAME
, &shortname
, -1);
1043 path
= !EMPTY(shortname
) &&
1044 (g_path_is_absolute(shortname
) ||
1045 (app
->project
&& g_str_has_prefix(shortname
, app
->project
->name
)));
1047 /* can close all, save all (except shortname), but only reload individually ATM */
1048 gtk_widget_set_sensitive(doc_items
.close
, sel
);
1049 gtk_widget_set_sensitive(doc_items
.save
, (doc
&& doc
->real_path
) || path
);
1050 gtk_widget_set_sensitive(doc_items
.reload
, doc
&& doc
->real_path
);
1051 gtk_widget_set_sensitive(doc_items
.find_in_files
, sel
);
1054 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), documents_show_paths
);
1055 gtk_widget_set_sensitive(doc_items
.expand_all
, documents_show_paths
);
1056 gtk_widget_set_sensitive(doc_items
.collapse_all
, documents_show_paths
);
1060 static StashGroup
*stash_group
= NULL
;
1062 static void on_load_settings(void)
1064 tag_window
= ui_lookup_widget(main_widgets
.window
, "scrolledwindow2");
1066 prepare_openfiles();
1067 /* note: ui_prefs.sidebar_page is reapplied after plugins are loaded */
1068 stash_group_display(stash_group
, NULL
);
1069 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1073 static void on_save_settings(void)
1075 stash_group_update(stash_group
, NULL
);
1076 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1080 void sidebar_init(void)
1084 group
= stash_group_new(PACKAGE
);
1085 stash_group_add_boolean(group
, &documents_show_paths
, "documents_show_paths", TRUE
);
1086 stash_group_add_widget_property(group
, &ui_prefs
.sidebar_page
, "sidebar_page", GINT_TO_POINTER(0),
1087 main_widgets
.sidebar_notebook
, "page", 0);
1088 configuration_add_pref_group(group
, FALSE
);
1089 stash_group
= group
;
1091 /* delay building documents treeview until sidebar font has been read */
1092 g_signal_connect(geany_object
, "load-settings", on_load_settings
, NULL
);
1093 g_signal_connect(geany_object
, "save-settings", on_save_settings
, NULL
);
1095 g_signal_connect(main_widgets
.sidebar_notebook
, "page-added",
1096 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1097 g_signal_connect(main_widgets
.sidebar_notebook
, "page-removed",
1098 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1099 /* tabs may have changed when sidebar is reshown */
1100 g_signal_connect(main_widgets
.sidebar_notebook
, "show",
1101 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1103 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1106 #define WIDGET(w) w && GTK_IS_WIDGET(w)
1108 void sidebar_finalize(void)
1110 if (WIDGET(tv
.default_tag_tree
))
1112 gtk_widget_destroy(tv
.default_tag_tree
); /* make GTK release its references, if any... */
1113 g_object_unref(tv
.default_tag_tree
); /* ...and release our own */
1115 if (WIDGET(tv
.popup_taglist
))
1116 gtk_widget_destroy(tv
.popup_taglist
);
1117 if (WIDGET(openfiles_popup_menu
))
1118 gtk_widget_destroy(openfiles_popup_menu
);
1122 void sidebar_focus_openfiles_tab(void)
1124 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_openfiles_visible
)
1126 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1128 gtk_notebook_set_current_page(notebook
, TREEVIEW_OPENFILES
);
1129 gtk_widget_grab_focus(tv
.tree_openfiles
);
1134 void sidebar_focus_symbols_tab(void)
1136 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_symbol_visible
)
1138 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1139 GtkWidget
*symbol_list_scrollwin
= gtk_notebook_get_nth_page(notebook
, TREEVIEW_SYMBOL
);
1141 gtk_notebook_set_current_page(notebook
, TREEVIEW_SYMBOL
);
1142 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(symbol_list_scrollwin
)));
1147 static void sidebar_tabs_show_hide(GtkNotebook
*notebook
, GtkWidget
*child
,
1148 guint page_num
, gpointer data
)
1150 gint tabs
= gtk_notebook_get_n_pages(notebook
);
1152 if (interface_prefs
.sidebar_symbol_visible
== FALSE
)
1154 if (interface_prefs
.sidebar_openfiles_visible
== FALSE
)
1157 gtk_notebook_set_show_tabs(notebook
, (tabs
> 1));