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, GDK_TYPE_PIXBUF
, 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 text_renderer
= gtk_cell_renderer_text_new();
291 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_MIDDLE
, NULL
);
292 column
= gtk_tree_view_column_new();
293 gtk_tree_view_column_pack_start(column
, icon_renderer
, FALSE
);
294 gtk_tree_view_column_set_attributes(column
, icon_renderer
, "pixbuf", DOCUMENTS_ICON
, NULL
);
295 gtk_tree_view_column_pack_start(column
, text_renderer
, TRUE
);
296 gtk_tree_view_column_set_attributes(column
, text_renderer
, "text", DOCUMENTS_SHORTNAME
,
297 "foreground-gdk", DOCUMENTS_COLOR
, NULL
);
298 gtk_tree_view_append_column(GTK_TREE_VIEW(tv
.tree_openfiles
), column
);
299 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv
.tree_openfiles
), FALSE
);
301 gtk_tree_view_set_search_column(GTK_TREE_VIEW(tv
.tree_openfiles
),
302 DOCUMENTS_SHORTNAME
);
304 /* sort opened filenames in the store_openfiles treeview */
305 sortable
= GTK_TREE_SORTABLE(GTK_TREE_MODEL(store_openfiles
));
306 gtk_tree_sortable_set_sort_func(sortable
, DOCUMENTS_SHORTNAME
, documents_sort_func
, NULL
, NULL
);
307 gtk_tree_sortable_set_sort_column_id(sortable
, DOCUMENTS_SHORTNAME
, GTK_SORT_ASCENDING
);
309 ui_widget_modify_font_from_string(tv
.tree_openfiles
, interface_prefs
.tagbar_font
);
312 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(tv
.tree_openfiles
), DOCUMENTS_FILENAME
);
314 /* selection handling */
315 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
316 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
317 g_object_unref(store_openfiles
);
319 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "button-press-event",
320 G_CALLBACK(sidebar_button_press_cb
), NULL
);
321 g_signal_connect(GTK_TREE_VIEW(tv
.tree_openfiles
), "key-press-event",
322 G_CALLBACK(sidebar_key_press_cb
), NULL
);
326 /* iter should be toplevel */
327 static gboolean
find_tree_iter_dir(GtkTreeIter
*iter
, const gchar
*dir
)
333 if (utils_str_equal(dir
, "."))
334 dir
= GEANY_STRING_UNTITLED
;
336 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
337 g_return_val_if_fail(!doc
, FALSE
);
339 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_SHORTNAME
, &name
, -1);
341 result
= utils_filenamecmp(name
, dir
) == 0;
348 static gboolean
utils_filename_has_prefix(const gchar
*str
, const gchar
*prefix
)
350 gchar
*head
= g_strndup(str
, strlen(prefix
));
351 gboolean ret
= utils_filenamecmp(head
, prefix
) == 0;
358 static gchar
*get_doc_folder(const gchar
*path
)
360 gchar
*tmp_dirname
= g_strdup(path
);
361 gchar
*project_base_path
;
362 gchar
*dirname
= NULL
;
363 const gchar
*home_dir
= g_get_home_dir();
366 /* replace the project base path with the project name */
367 project_base_path
= project_get_base_path();
369 if (project_base_path
!= NULL
)
371 gsize len
= strlen(project_base_path
);
373 /* remove trailing separator so we can match base path exactly */
374 if (project_base_path
[len
-1] == G_DIR_SEPARATOR
)
375 project_base_path
[--len
] = '\0';
377 /* check whether the dir name matches or uses the project base path */
378 if (utils_filename_has_prefix(tmp_dirname
, project_base_path
))
380 rest
= tmp_dirname
+ len
;
381 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
383 dirname
= g_strdup_printf("%s%s", app
->project
->name
, rest
);
386 g_free(project_base_path
);
390 dirname
= tmp_dirname
;
392 /* If matches home dir, replace with tilde */
393 if (!EMPTY(home_dir
) && utils_filename_has_prefix(dirname
, home_dir
))
395 rest
= dirname
+ strlen(home_dir
);
396 if (*rest
== G_DIR_SEPARATOR
|| *rest
== '\0')
398 dirname
= g_strdup_printf("~%s", rest
);
410 static GtkTreeIter
*get_doc_parent(GeanyDocument
*doc
)
413 gchar
*dirname
= NULL
;
414 static GtkTreeIter parent
;
415 GtkTreeModel
*model
= GTK_TREE_MODEL(store_openfiles
);
416 static GdkPixbuf
*dir_icon
= NULL
;
418 if (!documents_show_paths
)
421 path
= g_path_get_dirname(DOC_FILENAME(doc
));
422 dirname
= get_doc_folder(path
);
424 if (gtk_tree_model_get_iter_first(model
, &parent
))
428 if (find_tree_iter_dir(&parent
, dirname
))
435 while (gtk_tree_model_iter_next(model
, &parent
));
437 /* no match, add dir parent */
439 dir_icon
= ui_get_mime_icon("inode/directory", GTK_ICON_SIZE_MENU
);
441 gtk_tree_store_append(store_openfiles
, &parent
, NULL
);
442 gtk_tree_store_set(store_openfiles
, &parent
, DOCUMENTS_ICON
, dir_icon
,
443 DOCUMENTS_FILENAME
, path
,
444 DOCUMENTS_SHORTNAME
, doc
->file_name
? dirname
: GEANY_STRING_UNTITLED
, -1);
452 /* Also sets doc->priv->iter.
453 * This is called recursively in sidebar_openfiles_update_all(). */
454 void sidebar_openfiles_add(GeanyDocument
*doc
)
456 GtkTreeIter
*iter
= &doc
->priv
->iter
;
457 GtkTreeIter
*parent
= get_doc_parent(doc
);
459 const GdkColor
*color
= document_get_status_color(doc
);
460 static GdkPixbuf
*file_icon
= NULL
;
462 gtk_tree_store_append(store_openfiles
, iter
, parent
);
464 /* check if new parent */
465 if (parent
&& gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), parent
) == 1)
470 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), parent
);
471 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
472 gtk_tree_path_free(path
);
475 file_icon
= ui_get_mime_icon("text/plain", GTK_ICON_SIZE_MENU
);
477 basename
= g_path_get_basename(DOC_FILENAME(doc
));
478 gtk_tree_store_set(store_openfiles
, iter
,
479 DOCUMENTS_ICON
, (doc
->file_type
&& doc
->file_type
->icon
) ? doc
->file_type
->icon
: file_icon
,
480 DOCUMENTS_SHORTNAME
, basename
, DOCUMENTS_DOCUMENT
, doc
, DOCUMENTS_COLOR
, color
,
481 DOCUMENTS_FILENAME
, DOC_FILENAME(doc
), -1);
486 static void openfiles_remove(GeanyDocument
*doc
)
488 GtkTreeIter
*iter
= &doc
->priv
->iter
;
491 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
) &&
492 gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_openfiles
), &parent
) == 1)
493 gtk_tree_store_remove(store_openfiles
, &parent
);
495 gtk_tree_store_remove(store_openfiles
, iter
);
499 void sidebar_openfiles_update(GeanyDocument
*doc
)
501 GtkTreeIter
*iter
= &doc
->priv
->iter
;
504 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_FILENAME
, &fname
, -1);
506 if (utils_str_equal(fname
, DOC_FILENAME(doc
)))
508 /* just update color and the icon */
509 const GdkColor
*color
= document_get_status_color(doc
);
510 GdkPixbuf
*icon
= doc
->file_type
->icon
;
512 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_COLOR
, color
, -1);
514 gtk_tree_store_set(store_openfiles
, iter
, DOCUMENTS_ICON
, icon
, -1);
518 /* path has changed, so remove and re-add */
519 GtkTreeSelection
*treesel
;
522 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
523 sel
= gtk_tree_selection_iter_is_selected(treesel
, &doc
->priv
->iter
);
524 openfiles_remove(doc
);
526 sidebar_openfiles_add(doc
);
528 gtk_tree_selection_select_iter(treesel
, &doc
->priv
->iter
);
534 void sidebar_openfiles_update_all(void)
538 gtk_tree_store_clear(store_openfiles
);
541 sidebar_openfiles_add(documents
[i
]);
546 void sidebar_remove_document(GeanyDocument
*doc
)
548 openfiles_remove(doc
);
550 if (GTK_IS_WIDGET(doc
->priv
->tag_tree
))
552 gtk_widget_destroy(doc
->priv
->tag_tree
); /* make GTK release its references, if any */
553 /* Because it was ref'd in sidebar_update_tag_list, it needs unref'ing */
554 g_object_unref(doc
->priv
->tag_tree
);
555 doc
->priv
->tag_tree
= NULL
;
560 static void on_hide_sidebar(void)
562 ui_prefs
.sidebar_visible
= FALSE
;
563 ui_sidebar_show_hide();
567 static gboolean
on_sidebar_display_symbol_list_show(GtkWidget
*item
)
569 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
570 interface_prefs
.sidebar_symbol_visible
);
575 static gboolean
on_sidebar_display_open_files_show(GtkWidget
*item
)
577 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
),
578 interface_prefs
.sidebar_openfiles_visible
);
583 void sidebar_add_common_menu_items(GtkMenu
*menu
)
587 item
= gtk_separator_menu_item_new();
588 gtk_widget_show(item
);
589 gtk_container_add(GTK_CONTAINER(menu
), item
);
591 item
= gtk_check_menu_item_new_with_mnemonic(_("Show S_ymbol List"));
592 gtk_container_add(GTK_CONTAINER(menu
), item
);
593 #if GTK_CHECK_VERSION(3, 0, 0)
594 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
596 g_signal_connect(item
, "expose-event",
597 G_CALLBACK(on_sidebar_display_symbol_list_show
), NULL
);
599 gtk_widget_show(item
);
600 g_signal_connect(item
, "activate",
601 G_CALLBACK(on_list_symbol_activate
), NULL
);
603 item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Document List"));
604 gtk_container_add(GTK_CONTAINER(menu
), item
);
605 #if GTK_CHECK_VERSION(3, 0, 0)
606 g_signal_connect(item
, "draw", G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
608 g_signal_connect(item
, "expose-event",
609 G_CALLBACK(on_sidebar_display_open_files_show
), NULL
);
611 gtk_widget_show(item
);
612 g_signal_connect(item
, "activate",
613 G_CALLBACK(on_list_document_activate
), NULL
);
615 item
= gtk_image_menu_item_new_with_mnemonic(_("H_ide Sidebar"));
616 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
617 gtk_image_new_from_stock(GTK_STOCK_CLOSE
, GTK_ICON_SIZE_MENU
));
618 gtk_widget_show(item
);
619 gtk_container_add(GTK_CONTAINER(menu
), item
);
620 g_signal_connect(item
, "activate", G_CALLBACK(on_hide_sidebar
), NULL
);
624 static void on_openfiles_show_paths_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
626 documents_show_paths
= gtk_check_menu_item_get_active(item
);
627 sidebar_openfiles_update_all();
631 static void on_list_document_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
633 interface_prefs
.sidebar_openfiles_visible
= gtk_check_menu_item_get_active(item
);
634 ui_sidebar_show_hide();
635 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
639 static void on_list_symbol_activate(GtkCheckMenuItem
*item
, gpointer user_data
)
641 interface_prefs
.sidebar_symbol_visible
= gtk_check_menu_item_get_active(item
);
642 ui_sidebar_show_hide();
643 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
647 static void on_find_in_files(GtkMenuItem
*menuitem
, gpointer user_data
)
649 GtkTreeSelection
*treesel
;
655 treesel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
656 if (!gtk_tree_selection_get_selected(treesel
, &model
, &iter
))
658 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
662 gtk_tree_model_get(model
, &iter
, DOCUMENTS_FILENAME
, &dir
, -1);
665 dir
= g_path_get_dirname(DOC_FILENAME(doc
));
667 search_show_find_in_files_dialog(dir
);
672 static void on_openfiles_expand_collapse(GtkMenuItem
*menuitem
, gpointer user_data
)
674 gboolean expand
= GPOINTER_TO_INT(user_data
);
677 gtk_tree_view_expand_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
679 gtk_tree_view_collapse_all(GTK_TREE_VIEW(tv
.tree_openfiles
));
683 static void create_openfiles_popup_menu(void)
687 openfiles_popup_menu
= gtk_menu_new();
689 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE
, NULL
);
690 gtk_widget_show(item
);
691 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
692 g_signal_connect(item
, "activate",
693 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_REMOVE
));
694 doc_items
.close
= item
;
696 item
= gtk_separator_menu_item_new();
697 gtk_widget_show(item
);
698 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
700 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE
, 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_SAVE
));
705 doc_items
.save
= item
;
707 item
= gtk_image_menu_item_new_with_mnemonic(_("_Reload"));
708 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
),
709 gtk_image_new_from_stock(GTK_STOCK_REVERT_TO_SAVED
, GTK_ICON_SIZE_MENU
));
710 gtk_widget_show(item
);
711 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
712 g_signal_connect(item
, "activate",
713 G_CALLBACK(on_openfiles_document_action
), GINT_TO_POINTER(OPENFILES_ACTION_RELOAD
));
714 doc_items
.reload
= item
;
716 item
= gtk_separator_menu_item_new();
717 gtk_widget_show(item
);
718 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
720 item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("_Find in Files..."));
721 gtk_widget_show(item
);
722 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
723 g_signal_connect(item
, "activate", G_CALLBACK(on_find_in_files
), NULL
);
724 doc_items
.find_in_files
= item
;
726 item
= gtk_separator_menu_item_new();
727 gtk_widget_show(item
);
728 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), item
);
730 doc_items
.show_paths
= gtk_check_menu_item_new_with_mnemonic(_("Show _Paths"));
731 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), documents_show_paths
);
732 gtk_widget_show(doc_items
.show_paths
);
733 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.show_paths
);
734 g_signal_connect(doc_items
.show_paths
, "activate",
735 G_CALLBACK(on_openfiles_show_paths_activate
), NULL
);
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
.expand_all
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
742 gtk_widget_show(doc_items
.expand_all
);
743 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.expand_all
);
744 g_signal_connect(doc_items
.expand_all
, "activate",
745 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(TRUE
));
747 doc_items
.collapse_all
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
748 gtk_widget_show(doc_items
.collapse_all
);
749 gtk_container_add(GTK_CONTAINER(openfiles_popup_menu
), doc_items
.collapse_all
);
750 g_signal_connect(doc_items
.collapse_all
, "activate",
751 G_CALLBACK(on_openfiles_expand_collapse
), GINT_TO_POINTER(FALSE
));
753 sidebar_add_common_menu_items(GTK_MENU(openfiles_popup_menu
));
757 static void unfold_parent(GtkTreeIter
*iter
)
762 if (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_openfiles
), &parent
, iter
))
764 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store_openfiles
), &parent
);
765 gtk_tree_view_expand_row(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, TRUE
);
766 gtk_tree_path_free(path
);
771 /* compares the given data with the doc pointer from the selected row of openfiles
772 * treeview, in case of a match the row is selected and TRUE is returned
773 * (called indirectly from gtk_tree_model_foreach()) */
774 static gboolean
tree_model_find_node(GtkTreeModel
*model
, GtkTreePath
*path
,
775 GtkTreeIter
*iter
, gpointer data
)
779 gtk_tree_model_get(GTK_TREE_MODEL(store_openfiles
), iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
783 /* unfolding also prevents a strange bug where the selection gets stuck on the parent
784 * when it is collapsed and then switching documents */
786 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv
.tree_openfiles
), path
, NULL
, FALSE
);
793 void sidebar_select_openfiles_item(GeanyDocument
*doc
)
795 gtk_tree_model_foreach(GTK_TREE_MODEL(store_openfiles
), tree_model_find_node
, doc
);
801 static void document_action(GeanyDocument
*doc
, gint action
)
803 if (! DOC_VALID(doc
))
808 case OPENFILES_ACTION_REMOVE
:
813 case OPENFILES_ACTION_SAVE
:
815 document_save_file(doc
, FALSE
);
818 case OPENFILES_ACTION_RELOAD
:
820 on_toolbutton_reload_clicked(NULL
, NULL
);
827 static void on_openfiles_document_action(GtkMenuItem
*menuitem
, gpointer user_data
)
830 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
.tree_openfiles
));
833 gint action
= GPOINTER_TO_INT(user_data
);
835 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
837 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
840 document_action(doc
, action
);
844 /* parent item selected */
846 gint i
= gtk_tree_model_iter_n_children(model
, &iter
) - 1;
848 while (i
>= 0 && gtk_tree_model_iter_nth_child(model
, &child
, &iter
, i
))
850 gtk_tree_model_get(model
, &child
, DOCUMENTS_DOCUMENT
, &doc
, -1);
852 document_action(doc
, action
);
860 static void change_focus_to_editor(GeanyDocument
*doc
, GtkWidget
*source_widget
)
863 document_try_focus(doc
, source_widget
);
864 may_steal_focus
= FALSE
;
868 static gboolean
openfiles_go_to_selection(GtkTreeSelection
*selection
, guint keyval
)
872 GeanyDocument
*doc
= NULL
;
874 /* use switch_notebook_page to ignore changing the notebook page because it is already done */
875 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
) && ! ignore_callback
)
877 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
, -1);
879 return FALSE
; /* parent */
881 /* switch to the doc and grab the focus */
882 document_show_tab(doc
);
883 if (keyval
!= GDK_space
)
884 change_focus_to_editor(doc
, tv
.tree_openfiles
);
890 static gboolean
taglist_go_to_selection(GtkTreeSelection
*selection
, guint keyval
, guint state
)
895 gboolean handled
= TRUE
;
897 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
901 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
905 line
= tag
->atts
.entry
.line
;
908 GeanyDocument
*doc
= document_get_current();
912 navqueue_goto_line(doc
, doc
, line
);
913 if (keyval
!= GDK_space
&& ! (state
& GDK_CONTROL_MASK
))
914 change_focus_to_editor(doc
, NULL
);
925 static gboolean
sidebar_key_press_cb(GtkWidget
*widget
, GdkEventKey
*event
,
928 may_steal_focus
= FALSE
;
929 if (ui_is_keyval_enter_or_return(event
->keyval
) || event
->keyval
== GDK_space
)
931 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
932 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
933 may_steal_focus
= TRUE
;
935 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
936 * doing so will prevent further handlers to be run in most cases, but the only one is our
937 * own, so guess it's fine. */
938 if (widget_class
->key_press_event
)
939 widget_class
->key_press_event(widget
, event
);
941 if (widget
== tv
.tree_openfiles
) /* tag and doc list have separate handlers */
942 openfiles_go_to_selection(selection
, event
->keyval
);
944 taglist_go_to_selection(selection
, event
->keyval
, event
->state
);
952 static gboolean
sidebar_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
953 G_GNUC_UNUSED gpointer user_data
)
955 GtkTreeSelection
*selection
;
956 GtkWidgetClass
*widget_class
= GTK_WIDGET_GET_CLASS(widget
);
957 gboolean handled
= FALSE
;
959 /* force the TreeView handler to run before us for it to do its job (selection & stuff).
960 * doing so will prevent further handlers to be run in most cases, but the only one is our own,
961 * so guess it's fine. */
962 if (widget_class
->button_press_event
)
963 handled
= widget_class
->button_press_event(widget
, event
);
965 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(widget
));
966 may_steal_focus
= TRUE
;
968 if (event
->type
== GDK_2BUTTON_PRESS
)
969 { /* double click on parent node(section) expands/collapses it */
973 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
975 if (gtk_tree_model_iter_has_child(model
, &iter
))
977 GtkTreePath
*path
= gtk_tree_model_get_path(model
, &iter
);
979 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget
), path
))
980 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget
), path
);
982 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget
), path
, FALSE
);
984 gtk_tree_path_free(path
);
989 else if (event
->button
== 1)
990 { /* allow reclicking of taglist treeview item */
991 if (widget
== tv
.tree_openfiles
)
993 openfiles_go_to_selection(selection
, 0);
997 handled
= taglist_go_to_selection(selection
, 0, event
->state
);
999 else if (event
->button
== 3)
1001 if (widget
== tv
.tree_openfiles
)
1003 if (!openfiles_popup_menu
)
1004 create_openfiles_popup_menu();
1006 /* update menu item sensitivity */
1007 documents_menu_update(selection
);
1008 gtk_menu_popup(GTK_MENU(openfiles_popup_menu
), NULL
, NULL
, NULL
, NULL
,
1009 event
->button
, event
->time
);
1013 gtk_menu_popup(GTK_MENU(tv
.popup_taglist
), NULL
, NULL
, NULL
, NULL
,
1014 event
->button
, event
->time
);
1022 static void documents_menu_update(GtkTreeSelection
*selection
)
1024 GtkTreeModel
*model
;
1027 gchar
*shortname
= NULL
;
1028 GeanyDocument
*doc
= NULL
;
1030 /* maybe no selection e.g. if ctrl-click deselected */
1031 sel
= gtk_tree_selection_get_selected(selection
, &model
, &iter
);
1034 gtk_tree_model_get(model
, &iter
, DOCUMENTS_DOCUMENT
, &doc
,
1035 DOCUMENTS_SHORTNAME
, &shortname
, -1);
1037 path
= !EMPTY(shortname
) &&
1038 (g_path_is_absolute(shortname
) ||
1039 (app
->project
&& g_str_has_prefix(shortname
, app
->project
->name
)));
1041 /* can close all, save all (except shortname), but only reload individually ATM */
1042 gtk_widget_set_sensitive(doc_items
.close
, sel
);
1043 gtk_widget_set_sensitive(doc_items
.save
, (doc
&& doc
->real_path
) || path
);
1044 gtk_widget_set_sensitive(doc_items
.reload
, doc
&& doc
->real_path
);
1045 gtk_widget_set_sensitive(doc_items
.find_in_files
, sel
);
1048 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(doc_items
.show_paths
), documents_show_paths
);
1049 gtk_widget_set_sensitive(doc_items
.expand_all
, documents_show_paths
);
1050 gtk_widget_set_sensitive(doc_items
.collapse_all
, documents_show_paths
);
1054 static StashGroup
*stash_group
= NULL
;
1056 static void on_load_settings(void)
1058 tag_window
= ui_lookup_widget(main_widgets
.window
, "scrolledwindow2");
1060 prepare_openfiles();
1061 /* note: ui_prefs.sidebar_page is reapplied after plugins are loaded */
1062 stash_group_display(stash_group
, NULL
);
1063 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1067 static void on_save_settings(void)
1069 stash_group_update(stash_group
, NULL
);
1070 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1074 void sidebar_init(void)
1078 group
= stash_group_new(PACKAGE
);
1079 stash_group_add_boolean(group
, &documents_show_paths
, "documents_show_paths", TRUE
);
1080 stash_group_add_widget_property(group
, &ui_prefs
.sidebar_page
, "sidebar_page", GINT_TO_POINTER(0),
1081 main_widgets
.sidebar_notebook
, "page", 0);
1082 configuration_add_pref_group(group
, FALSE
);
1083 stash_group
= group
;
1085 /* delay building documents treeview until sidebar font has been read */
1086 g_signal_connect(geany_object
, "load-settings", on_load_settings
, NULL
);
1087 g_signal_connect(geany_object
, "save-settings", on_save_settings
, NULL
);
1089 g_signal_connect(main_widgets
.sidebar_notebook
, "page-added",
1090 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1091 g_signal_connect(main_widgets
.sidebar_notebook
, "page-removed",
1092 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1093 /* tabs may have changed when sidebar is reshown */
1094 g_signal_connect(main_widgets
.sidebar_notebook
, "show",
1095 G_CALLBACK(sidebar_tabs_show_hide
), NULL
);
1097 sidebar_tabs_show_hide(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), NULL
, 0, NULL
);
1100 #define WIDGET(w) w && GTK_IS_WIDGET(w)
1102 void sidebar_finalize(void)
1104 if (WIDGET(tv
.default_tag_tree
))
1106 gtk_widget_destroy(tv
.default_tag_tree
); /* make GTK release its references, if any... */
1107 g_object_unref(tv
.default_tag_tree
); /* ...and release our own */
1109 if (WIDGET(tv
.popup_taglist
))
1110 gtk_widget_destroy(tv
.popup_taglist
);
1111 if (WIDGET(openfiles_popup_menu
))
1112 gtk_widget_destroy(openfiles_popup_menu
);
1116 void sidebar_focus_openfiles_tab(void)
1118 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_openfiles_visible
)
1120 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1122 gtk_notebook_set_current_page(notebook
, TREEVIEW_OPENFILES
);
1123 gtk_widget_grab_focus(tv
.tree_openfiles
);
1128 void sidebar_focus_symbols_tab(void)
1130 if (ui_prefs
.sidebar_visible
&& interface_prefs
.sidebar_symbol_visible
)
1132 GtkNotebook
*notebook
= GTK_NOTEBOOK(main_widgets
.sidebar_notebook
);
1133 GtkWidget
*symbol_list_scrollwin
= gtk_notebook_get_nth_page(notebook
, TREEVIEW_SYMBOL
);
1135 gtk_notebook_set_current_page(notebook
, TREEVIEW_SYMBOL
);
1136 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(symbol_list_scrollwin
)));
1141 static void sidebar_tabs_show_hide(GtkNotebook
*notebook
, GtkWidget
*child
,
1142 guint page_num
, gpointer data
)
1144 gint tabs
= gtk_notebook_get_n_pages(notebook
);
1146 if (interface_prefs
.sidebar_symbol_visible
== FALSE
)
1148 if (interface_prefs
.sidebar_openfiles_visible
== FALSE
)
1151 gtk_notebook_set_show_tabs(notebook
, (tabs
> 1));