Merge pull request #466 from techee/waf_build_tree
[geany-mirror.git] / plugins / filebrowser.c
blob9d55c59bb6a7b5fcb8d25477b0c16ae48dd4d10d
1 /*
2 * filebrowser.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-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.
22 /* Sidebar file browser plugin. */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "geanyplugin.h"
29 #include "gtkcompat.h"
30 #include <string.h>
32 #include <gdk/gdkkeysyms.h>
34 #ifdef G_OS_WIN32
35 # include <windows.h>
37 # define OPEN_CMD "explorer \"%d\""
38 #elif defined(__APPLE__)
39 # define OPEN_CMD "open \"%d\""
40 #else
41 # define OPEN_CMD "nautilus \"%d\""
42 #endif
44 GeanyPlugin *geany_plugin;
45 GeanyData *geany_data;
46 GeanyFunctions *geany_functions;
49 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
51 PLUGIN_SET_INFO(_("File Browser"), _("Adds a file browser tab to the sidebar."), VERSION,
52 _("The Geany developer team"))
55 /* Keybinding(s) */
56 enum
58 KB_FOCUS_FILE_LIST,
59 KB_FOCUS_PATH_ENTRY,
60 KB_COUNT
63 PLUGIN_KEY_GROUP(file_browser, KB_COUNT)
66 enum
68 FILEVIEW_COLUMN_ICON = 0,
69 FILEVIEW_COLUMN_NAME,
70 FILEVIEW_COLUMN_FILENAME, /* the full filename, including path for display as tooltip */
71 FILEVIEW_N_COLUMNS
74 static gboolean fb_set_project_base_path = FALSE;
75 static gboolean fb_follow_path = FALSE;
76 static gboolean show_hidden_files = FALSE;
77 static gboolean hide_object_files = TRUE;
79 static GtkWidget *file_view_vbox;
80 static GtkWidget *file_view;
81 static GtkListStore *file_store;
82 static GtkTreeIter *last_dir_iter = NULL;
83 static GtkEntryCompletion *entry_completion = NULL;
85 static GtkWidget *filter_combo;
86 static GtkWidget *filter_entry;
87 static GtkWidget *path_combo;
88 static GtkWidget *path_entry;
89 static gchar *current_dir = NULL; /* in locale-encoding */
90 static gchar *open_cmd; /* in locale-encoding */
91 static gchar *config_file;
92 static gchar **filter = NULL;
93 static gchar *hidden_file_extensions = NULL;
95 static gint page_number = 0;
97 static struct
99 GtkWidget *open;
100 GtkWidget *open_external;
101 GtkWidget *find_in_files;
102 GtkWidget *show_hidden_files;
103 } popup_items;
106 static void project_change_cb(GObject *obj, GKeyFile *config, gpointer data);
108 /* note: other callbacks connected in plugin_init */
109 PluginCallback plugin_callbacks[] =
111 { "project-open", (GCallback) &project_change_cb, TRUE, NULL },
112 { "project-save", (GCallback) &project_change_cb, TRUE, NULL },
113 { NULL, NULL, FALSE, NULL }
117 #ifdef G_OS_WIN32
118 static gboolean win32_check_hidden(const gchar *filename)
120 DWORD attrs;
121 static wchar_t w_filename[MAX_PATH];
122 MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename));
123 attrs = GetFileAttributesW(w_filename);
124 if (attrs != INVALID_FILE_ATTRIBUTES && attrs & FILE_ATTRIBUTE_HIDDEN)
125 return TRUE;
126 return FALSE;
128 #endif
131 /* Returns: whether name should be hidden. */
132 static gboolean check_hidden(const gchar *filename, const gchar *base_name)
134 gsize len;
136 #ifdef G_OS_WIN32
137 if (win32_check_hidden(filename))
138 return TRUE;
139 #else
140 if (base_name[0] == '.')
141 return TRUE;
142 #endif
144 len = strlen(base_name);
145 return base_name[len - 1] == '~';
149 static gboolean check_object(const gchar *base_name)
151 gboolean ret = FALSE;
152 gchar **ptr;
153 gchar **exts = g_strsplit(hidden_file_extensions, " ", -1);
155 foreach_strv(ptr, exts)
157 if (g_str_has_suffix(base_name, *ptr))
159 ret = TRUE;
160 break;
163 g_strfreev(exts);
164 return ret;
168 /* Returns: whether filename should be removed. */
169 static gboolean check_filtered(const gchar *base_name)
171 gchar **filter_item;
173 if (filter == NULL)
174 return FALSE;
176 foreach_strv(filter_item, filter)
178 if (utils_str_equal(*filter_item, "*") || g_pattern_match_simple(*filter_item, base_name))
180 return FALSE;
183 return TRUE;
187 /* name is in locale encoding */
188 static void add_item(const gchar *name)
190 GtkTreeIter iter;
191 gchar *fname, *utf8_name, *utf8_fullname;
192 const gchar *sep;
193 gboolean dir;
195 if (G_UNLIKELY(EMPTY(name)))
196 return;
198 /* root directory doesn't need separator */
199 sep = (utils_str_equal(current_dir, "/")) ? "" : G_DIR_SEPARATOR_S;
200 fname = g_strconcat(current_dir, sep, name, NULL);
201 dir = g_file_test(fname, G_FILE_TEST_IS_DIR);
202 utf8_fullname = utils_get_locale_from_utf8(fname);
203 utf8_name = utils_get_utf8_from_locale(name);
204 g_free(fname);
206 if (! show_hidden_files && check_hidden(utf8_fullname, utf8_name))
207 goto done;
209 if (dir)
211 if (last_dir_iter == NULL)
212 gtk_list_store_prepend(file_store, &iter);
213 else
215 gtk_list_store_insert_after(file_store, &iter, last_dir_iter);
216 gtk_tree_iter_free(last_dir_iter);
218 last_dir_iter = gtk_tree_iter_copy(&iter);
220 else
222 if (! show_hidden_files && hide_object_files && check_object(utf8_name))
223 goto done;
224 if (check_filtered(utf8_name))
225 goto done;
227 gtk_list_store_append(file_store, &iter);
229 gtk_list_store_set(file_store, &iter,
230 FILEVIEW_COLUMN_ICON, (dir) ? GTK_STOCK_DIRECTORY : GTK_STOCK_FILE,
231 FILEVIEW_COLUMN_NAME, utf8_name,
232 FILEVIEW_COLUMN_FILENAME, utf8_fullname,
233 -1);
234 done:
235 g_free(utf8_name);
236 g_free(utf8_fullname);
240 /* adds ".." to the start of the file list */
241 static void add_top_level_entry(void)
243 GtkTreeIter iter;
244 gchar *utf8_dir;
246 if (EMPTY(g_path_skip_root(current_dir)))
247 return; /* ignore 'C:\' or '/' */
249 utf8_dir = g_path_get_dirname(current_dir);
250 SETPTR(utf8_dir, utils_get_utf8_from_locale(utf8_dir));
252 gtk_list_store_prepend(file_store, &iter);
253 last_dir_iter = gtk_tree_iter_copy(&iter);
255 gtk_list_store_set(file_store, &iter,
256 FILEVIEW_COLUMN_ICON, GTK_STOCK_DIRECTORY,
257 FILEVIEW_COLUMN_NAME, "..",
258 FILEVIEW_COLUMN_FILENAME, utf8_dir,
259 -1);
260 g_free(utf8_dir);
264 static void clear(void)
266 gtk_list_store_clear(file_store);
268 /* reset the directory item pointer */
269 if (last_dir_iter != NULL)
270 gtk_tree_iter_free(last_dir_iter);
271 last_dir_iter = NULL;
275 /* recreate the tree model from current_dir. */
276 static void refresh(void)
278 gchar *utf8_dir;
279 GSList *list, *node;
281 /* don't clear when the new path doesn't exist */
282 if (! g_file_test(current_dir, G_FILE_TEST_EXISTS))
283 return;
285 clear();
287 utf8_dir = utils_get_utf8_from_locale(current_dir);
288 gtk_entry_set_text(GTK_ENTRY(path_entry), utf8_dir);
289 gtk_widget_set_tooltip_text(path_entry, utf8_dir);
290 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(path_combo), utf8_dir, 0);
291 g_free(utf8_dir);
293 add_top_level_entry(); /* ".." item */
295 list = utils_get_file_list(current_dir, NULL, NULL);
296 if (list != NULL)
298 /* free filenames as we go through the list */
299 foreach_slist(node, list)
301 gchar *fname = node->data;
303 add_item(fname);
304 g_free(fname);
306 g_slist_free(list);
308 gtk_entry_completion_set_model(entry_completion, GTK_TREE_MODEL(file_store));
312 static void on_go_home(void)
314 SETPTR(current_dir, g_strdup(g_get_home_dir()));
315 refresh();
319 /* TODO: use utils_get_default_dir_utf8() */
320 static gchar *get_default_dir(void)
322 const gchar *dir = NULL;
323 GeanyProject *project = geany->app->project;
325 if (project)
326 dir = project->base_path;
327 else
328 dir = geany->prefs->default_open_path;
330 if (!EMPTY(dir))
331 return utils_get_locale_from_utf8(dir);
333 return g_get_current_dir();
337 static void on_current_path(void)
339 gchar *fname;
340 gchar *dir;
341 GeanyDocument *doc = document_get_current();
343 if (doc == NULL || doc->file_name == NULL || ! g_path_is_absolute(doc->file_name))
345 SETPTR(current_dir, get_default_dir());
346 refresh();
347 return;
349 fname = doc->file_name;
350 fname = utils_get_locale_from_utf8(fname);
351 dir = g_path_get_dirname(fname);
352 g_free(fname);
354 SETPTR(current_dir, dir);
355 refresh();
359 static void on_go_up(void)
361 gsize len = strlen(current_dir);
362 if (current_dir[len-1] == G_DIR_SEPARATOR)
363 current_dir[len-1] = '\0';
364 /* remove the highest directory part (which becomes the basename of current_dir) */
365 SETPTR(current_dir, g_path_get_dirname(current_dir));
366 refresh();
370 static gboolean check_single_selection(GtkTreeSelection *treesel)
372 if (gtk_tree_selection_count_selected_rows(treesel) == 1)
373 return TRUE;
375 ui_set_statusbar(FALSE, _("Too many items selected!"));
376 return FALSE;
380 /* Returns: TRUE if at least one of selected_items is a folder. */
381 static gboolean is_folder_selected(GList *selected_items)
383 GList *item;
384 GtkTreeModel *model = GTK_TREE_MODEL(file_store);
385 gboolean dir_found = FALSE;
387 for (item = selected_items; item != NULL; item = g_list_next(item))
389 gchar *icon;
390 GtkTreeIter iter;
391 GtkTreePath *treepath;
393 treepath = (GtkTreePath*) item->data;
394 gtk_tree_model_get_iter(model, &iter, treepath);
395 gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_ICON, &icon, -1);
397 if (utils_str_equal(icon, GTK_STOCK_DIRECTORY))
399 dir_found = TRUE;
400 g_free(icon);
401 break;
403 g_free(icon);
405 return dir_found;
409 /* Returns: the full filename in locale encoding. */
410 static gchar *get_tree_path_filename(GtkTreePath *treepath)
412 GtkTreeModel *model = GTK_TREE_MODEL(file_store);
413 GtkTreeIter iter;
414 gchar *name, *fname;
416 gtk_tree_model_get_iter(model, &iter, treepath);
417 gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_FILENAME, &name, -1);
419 fname = utils_get_locale_from_utf8(name);
420 g_free(name);
422 return fname;
426 static void open_external(const gchar *fname, gboolean dir_found)
428 gchar *cmd;
429 gchar *locale_cmd;
430 gchar *dir;
431 GString *cmd_str = g_string_new(open_cmd);
432 GError *error = NULL;
434 if (! dir_found)
435 dir = g_path_get_dirname(fname);
436 else
437 dir = g_strdup(fname);
439 utils_string_replace_all(cmd_str, "%f", fname);
440 utils_string_replace_all(cmd_str, "%d", dir);
442 cmd = g_string_free(cmd_str, FALSE);
443 locale_cmd = utils_get_locale_from_utf8(cmd);
444 if (! g_spawn_command_line_async(locale_cmd, &error))
446 gchar *c = strchr(cmd, ' ');
448 if (c != NULL)
449 *c = '\0';
450 ui_set_statusbar(TRUE,
451 _("Could not execute configured external command '%s' (%s)."),
452 cmd, error->message);
453 g_error_free(error);
455 g_free(locale_cmd);
456 g_free(cmd);
457 g_free(dir);
461 static void on_external_open(GtkMenuItem *menuitem, gpointer user_data)
463 GtkTreeSelection *treesel;
464 GtkTreeModel *model;
465 GList *list;
466 gboolean dir_found;
468 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
470 list = gtk_tree_selection_get_selected_rows(treesel, &model);
471 dir_found = is_folder_selected(list);
473 if (! dir_found || check_single_selection(treesel))
475 GList *item;
477 for (item = list; item != NULL; item = g_list_next(item))
479 GtkTreePath *treepath = item->data;
480 gchar *fname = get_tree_path_filename(treepath);
482 open_external(fname, dir_found);
483 g_free(fname);
487 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
488 g_list_free(list);
492 /* We use document_open_files() as it's more efficient. */
493 static void open_selected_files(GList *list, gboolean do_not_focus)
495 GSList *files = NULL;
496 GList *item;
497 GeanyDocument *doc;
499 for (item = list; item != NULL; item = g_list_next(item))
501 GtkTreePath *treepath = item->data;
502 gchar *fname = get_tree_path_filename(treepath);
504 files = g_slist_prepend(files, fname);
506 files = g_slist_reverse(files);
507 document_open_files(files, FALSE, NULL, NULL);
508 doc = document_get_current();
509 if (doc != NULL && ! do_not_focus)
510 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
512 g_slist_foreach(files, (GFunc) g_free, NULL); /* free filenames */
513 g_slist_free(files);
517 static void open_folder(GtkTreePath *treepath)
519 gchar *fname = get_tree_path_filename(treepath);
521 SETPTR(current_dir, fname);
522 refresh();
526 static void on_open_clicked(GtkMenuItem *menuitem, gpointer user_data)
528 GtkTreeSelection *treesel;
529 GtkTreeModel *model;
530 GList *list;
531 gboolean dir_found;
533 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
535 list = gtk_tree_selection_get_selected_rows(treesel, &model);
536 dir_found = is_folder_selected(list);
538 if (dir_found)
540 if (check_single_selection(treesel))
542 GtkTreePath *treepath = list->data; /* first selected item */
544 open_folder(treepath);
547 else
548 open_selected_files(list, GPOINTER_TO_INT(user_data));
550 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
551 g_list_free(list);
555 static void on_find_in_files(GtkMenuItem *menuitem, gpointer user_data)
557 GtkTreeSelection *treesel;
558 GtkTreeModel *model;
559 GList *list;
560 gchar *dir;
561 gboolean is_dir = FALSE;
563 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
564 /* allow 0 or 1 selections */
565 if (gtk_tree_selection_count_selected_rows(treesel) > 0 &&
566 ! check_single_selection(treesel))
567 return;
569 list = gtk_tree_selection_get_selected_rows(treesel, &model);
570 is_dir = is_folder_selected(list);
572 if (is_dir)
574 GtkTreePath *treepath = list->data; /* first selected item */
576 dir = get_tree_path_filename(treepath);
578 else
579 dir = g_strdup(current_dir);
581 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
582 g_list_free(list);
584 SETPTR(dir, utils_get_utf8_from_locale(dir));
585 search_show_find_in_files_dialog(dir);
586 g_free(dir);
590 static void on_hidden_files_clicked(GtkCheckMenuItem *item)
592 show_hidden_files = gtk_check_menu_item_get_active(item);
593 refresh();
597 static void on_hide_sidebar(void)
599 keybindings_send_command(GEANY_KEY_GROUP_VIEW, GEANY_KEYS_VIEW_SIDEBAR);
603 static void on_show_preferences(void)
605 plugin_show_configure(geany_plugin);
609 static GtkWidget *create_popup_menu(void)
611 GtkWidget *item, *menu;
613 menu = gtk_menu_new();
615 item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open in _Geany"));
616 gtk_widget_show(item);
617 gtk_container_add(GTK_CONTAINER(menu), item);
618 g_signal_connect(item, "activate", G_CALLBACK(on_open_clicked), NULL);
619 popup_items.open = item;
621 item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open _Externally"));
622 gtk_widget_show(item);
623 gtk_container_add(GTK_CONTAINER(menu), item);
624 g_signal_connect(item, "activate", G_CALLBACK(on_external_open), NULL);
625 popup_items.open_external = item;
627 item = gtk_separator_menu_item_new();
628 gtk_widget_show(item);
629 gtk_container_add(GTK_CONTAINER(menu), item);
631 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_REFRESH, NULL);
632 gtk_widget_show(item);
633 gtk_container_add(GTK_CONTAINER(menu), item);
634 g_signal_connect(item, "activate", G_CALLBACK(refresh), NULL);
636 item = ui_image_menu_item_new(GTK_STOCK_FIND, _("_Find in Files..."));
637 gtk_widget_show(item);
638 gtk_container_add(GTK_CONTAINER(menu), item);
639 g_signal_connect(item, "activate", G_CALLBACK(on_find_in_files), NULL);
640 popup_items.find_in_files = item;
642 item = gtk_separator_menu_item_new();
643 gtk_widget_show(item);
644 gtk_container_add(GTK_CONTAINER(menu), item);
646 item = gtk_check_menu_item_new_with_mnemonic(_("Show _Hidden Files"));
647 gtk_widget_show(item);
648 gtk_container_add(GTK_CONTAINER(menu), item);
649 g_signal_connect(item, "activate", G_CALLBACK(on_hidden_files_clicked), NULL);
650 popup_items.show_hidden_files = item;
652 item = gtk_separator_menu_item_new();
653 gtk_widget_show(item);
654 gtk_container_add(GTK_CONTAINER(menu), item);
656 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
657 gtk_widget_show(item);
658 gtk_container_add(GTK_CONTAINER(menu), item);
659 g_signal_connect(item, "activate", G_CALLBACK(on_show_preferences), NULL);
661 item = gtk_separator_menu_item_new();
662 gtk_widget_show(item);
663 gtk_container_add(GTK_CONTAINER(menu), item);
665 item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("H_ide Sidebar"));
666 gtk_widget_show(item);
667 gtk_container_add(GTK_CONTAINER(menu), item);
668 g_signal_connect(item, "activate", G_CALLBACK(on_hide_sidebar), NULL);
670 return menu;
674 static void on_tree_selection_changed(GtkTreeSelection *selection, gpointer data)
676 gboolean have_sel = (gtk_tree_selection_count_selected_rows(selection) > 0);
677 gboolean multi_sel = (gtk_tree_selection_count_selected_rows(selection) > 1);
679 if (popup_items.open != NULL)
680 gtk_widget_set_sensitive(popup_items.open, have_sel);
681 if (popup_items.open_external != NULL)
682 gtk_widget_set_sensitive(popup_items.open_external, have_sel);
683 if (popup_items.find_in_files != NULL)
684 gtk_widget_set_sensitive(popup_items.find_in_files, have_sel && ! multi_sel);
688 static gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
690 if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
692 on_open_clicked(NULL, NULL);
693 return TRUE;
695 else if (event->button == 3)
697 static GtkWidget *popup_menu = NULL;
699 if (popup_menu == NULL)
700 popup_menu = create_popup_menu();
702 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(popup_items.show_hidden_files),
703 show_hidden_files);
704 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, NULL, NULL, event->button, event->time);
705 /* don't return TRUE here, unless the selection won't be changed */
707 return FALSE;
711 static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
713 if (ui_is_keyval_enter_or_return(event->keyval))
715 on_open_clicked(NULL, NULL);
716 return TRUE;
719 if (event->keyval == GDK_space)
721 on_open_clicked(NULL, GINT_TO_POINTER(TRUE));
722 return TRUE;
725 if (( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && (event->state & GDK_MOD1_MASK)) || /* FIXME: Alt-Up doesn't seem to work! */
726 (event->keyval == GDK_BackSpace) )
728 on_go_up();
729 return TRUE;
732 if ((event->keyval == GDK_F10 && event->state & GDK_SHIFT_MASK) || event->keyval == GDK_Menu)
734 GdkEventButton button_event;
736 button_event.time = event->time;
737 button_event.button = 3;
739 on_button_press(widget, &button_event, data);
740 return TRUE;
743 return FALSE;
747 static void clear_filter(void)
749 if (filter != NULL)
751 g_strfreev(filter);
752 filter = NULL;
757 static void on_clear_filter(GtkEntry *entry, gpointer user_data)
759 clear_filter();
761 gtk_entry_set_text(GTK_ENTRY(filter_entry), "");
763 refresh();
767 static void on_path_entry_activate(GtkEntry *entry, gpointer user_data)
769 gchar *new_dir = (gchar*) gtk_entry_get_text(entry);
771 if (!EMPTY(new_dir))
773 if (g_str_has_suffix(new_dir, ".."))
775 on_go_up();
776 return;
778 else if (new_dir[0] == '~')
780 GString *str = g_string_new(new_dir);
781 utils_string_replace_first(str, "~", g_get_home_dir());
782 new_dir = g_string_free(str, FALSE);
784 else
785 new_dir = utils_get_locale_from_utf8(new_dir);
787 else
788 new_dir = g_strdup(g_get_home_dir());
790 SETPTR(current_dir, new_dir);
792 on_clear_filter(NULL, NULL);
796 static void ui_combo_box_changed(GtkComboBox *combo, gpointer user_data)
798 /* we get this callback on typing as well as choosing an item */
799 if (gtk_combo_box_get_active(combo) >= 0)
800 gtk_widget_activate(gtk_bin_get_child(GTK_BIN(combo)));
804 static void on_filter_activate(GtkEntry *entry, gpointer user_data)
806 /* We use spaces for consistency with Find in Files file patterns
807 * ';' also supported like original patch. */
808 filter = g_strsplit_set(gtk_entry_get_text(entry), "; ", -1);
809 if (filter == NULL || g_strv_length(filter) == 0)
811 clear_filter();
813 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(filter_combo), NULL, 0);
814 refresh();
818 static void on_filter_clear(GtkEntry *entry, gint icon_pos,
819 GdkEvent *event, gpointer data)
821 clear_filter();
822 refresh();
826 static void prepare_file_view(void)
828 GtkCellRenderer *text_renderer, *icon_renderer;
829 GtkTreeViewColumn *column;
830 GtkTreeSelection *selection;
832 file_store = gtk_list_store_new(FILEVIEW_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
834 gtk_tree_view_set_model(GTK_TREE_VIEW(file_view), GTK_TREE_MODEL(file_store));
835 g_object_unref(file_store);
837 icon_renderer = gtk_cell_renderer_pixbuf_new();
838 text_renderer = gtk_cell_renderer_text_new();
839 column = gtk_tree_view_column_new();
840 gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
841 gtk_tree_view_column_set_attributes(column, icon_renderer, "stock-id", FILEVIEW_COLUMN_ICON, NULL);
842 gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
843 gtk_tree_view_column_set_attributes(column, text_renderer, "text", FILEVIEW_COLUMN_NAME, NULL);
844 gtk_tree_view_append_column(GTK_TREE_VIEW(file_view), column);
845 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(file_view), FALSE);
847 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(file_view), TRUE);
848 gtk_tree_view_set_search_column(GTK_TREE_VIEW(file_view), FILEVIEW_COLUMN_NAME);
850 ui_widget_modify_font_from_string(file_view, geany->interface_prefs->tagbar_font);
852 /* tooltips */
853 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(file_view), FILEVIEW_COLUMN_FILENAME);
855 /* selection handling */
856 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
857 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
859 /* Show the current path when the FB is first needed */
860 g_signal_connect(file_view, "realize", G_CALLBACK(on_current_path), NULL);
861 g_signal_connect(selection, "changed", G_CALLBACK(on_tree_selection_changed), NULL);
862 g_signal_connect(file_view, "button-press-event", G_CALLBACK(on_button_press), NULL);
863 g_signal_connect(file_view, "key-press-event", G_CALLBACK(on_key_press), NULL);
867 static GtkWidget *make_toolbar(void)
869 GtkWidget *wid, *toolbar;
871 toolbar = gtk_toolbar_new();
872 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
873 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
875 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_GO_UP));
876 gtk_widget_set_tooltip_text(wid, _("Up"));
877 g_signal_connect(wid, "clicked", G_CALLBACK(on_go_up), NULL);
878 gtk_container_add(GTK_CONTAINER(toolbar), wid);
880 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH));
881 gtk_widget_set_tooltip_text(wid, _("Refresh"));
882 g_signal_connect(wid, "clicked", G_CALLBACK(refresh), NULL);
883 gtk_container_add(GTK_CONTAINER(toolbar), wid);
885 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_HOME));
886 gtk_widget_set_tooltip_text(wid, _("Home"));
887 g_signal_connect(wid, "clicked", G_CALLBACK(on_go_home), NULL);
888 gtk_container_add(GTK_CONTAINER(toolbar), wid);
890 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_JUMP_TO));
891 gtk_widget_set_tooltip_text(wid, _("Set path from document"));
892 g_signal_connect(wid, "clicked", G_CALLBACK(on_current_path), NULL);
893 gtk_container_add(GTK_CONTAINER(toolbar), wid);
895 return toolbar;
899 static GtkWidget *make_filterbar(void)
901 GtkWidget *label, *filterbar;
903 filterbar = gtk_hbox_new(FALSE, 1);
905 label = gtk_label_new(_("Filter:"));
907 filter_combo = gtk_combo_box_text_new_with_entry();
908 filter_entry = gtk_bin_get_child(GTK_BIN(filter_combo));
910 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry));
911 g_signal_connect(filter_entry, "icon-release", G_CALLBACK(on_filter_clear), NULL);
913 gtk_widget_set_tooltip_text(filter_entry,
914 _("Filter your files with the usual wildcards. Separate multiple patterns with a space."));
915 g_signal_connect(filter_entry, "activate", G_CALLBACK(on_filter_activate), NULL);
916 g_signal_connect(filter_combo, "changed", G_CALLBACK(ui_combo_box_changed), NULL);
918 gtk_box_pack_start(GTK_BOX(filterbar), label, FALSE, FALSE, 0);
919 gtk_box_pack_start(GTK_BOX(filterbar), filter_combo, TRUE, TRUE, 0);
921 return filterbar;
925 static gboolean completion_match_func(GtkEntryCompletion *completion, const gchar *key,
926 GtkTreeIter *iter, gpointer user_data)
928 gchar *str, *icon;
929 gboolean result = FALSE;
931 gtk_tree_model_get(GTK_TREE_MODEL(file_store), iter,
932 FILEVIEW_COLUMN_ICON, &icon, FILEVIEW_COLUMN_NAME, &str, -1);
934 if (str != NULL && icon != NULL && utils_str_equal(icon, GTK_STOCK_DIRECTORY) &&
935 ! g_str_has_suffix(key, G_DIR_SEPARATOR_S))
937 /* key is something like "/tmp/te" and str is a filename like "test",
938 * so strip the path from key to make them comparable */
939 gchar *base_name = g_path_get_basename(key);
940 gchar *str_lowered = g_utf8_strdown(str, -1);
941 result = g_str_has_prefix(str_lowered, base_name);
942 g_free(base_name);
943 g_free(str_lowered);
945 g_free(str);
946 g_free(icon);
948 return result;
952 static gboolean completion_match_selected(GtkEntryCompletion *widget, GtkTreeModel *model,
953 GtkTreeIter *iter, gpointer user_data)
955 gchar *str;
956 gtk_tree_model_get(model, iter, FILEVIEW_COLUMN_NAME, &str, -1);
957 if (str != NULL)
959 gchar *text = g_strconcat(current_dir, G_DIR_SEPARATOR_S, str, NULL);
960 gtk_entry_set_text(GTK_ENTRY(path_entry), text);
961 gtk_editable_set_position(GTK_EDITABLE(path_entry), -1);
962 /* force change of directory when completion is done */
963 on_path_entry_activate(GTK_ENTRY(path_entry), NULL);
964 g_free(text);
966 g_free(str);
968 return TRUE;
972 static void completion_create(void)
974 entry_completion = gtk_entry_completion_new();
976 gtk_entry_completion_set_inline_completion(entry_completion, FALSE);
977 gtk_entry_completion_set_popup_completion(entry_completion, TRUE);
978 gtk_entry_completion_set_text_column(entry_completion, FILEVIEW_COLUMN_NAME);
979 gtk_entry_completion_set_match_func(entry_completion, completion_match_func, NULL, NULL);
981 g_signal_connect(entry_completion, "match-selected",
982 G_CALLBACK(completion_match_selected), NULL);
984 gtk_entry_set_completion(GTK_ENTRY(path_entry), entry_completion);
988 static void load_settings(void)
990 GKeyFile *config = g_key_file_new();
992 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
993 "filebrowser", G_DIR_SEPARATOR_S, "filebrowser.conf", NULL);
994 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
996 open_cmd = utils_get_setting_string(config, "filebrowser", "open_command", OPEN_CMD);
997 /* g_key_file_get_boolean defaults to FALSE */
998 show_hidden_files = g_key_file_get_boolean(config, "filebrowser", "show_hidden_files", NULL);
999 hide_object_files = utils_get_setting_boolean(config, "filebrowser", "hide_object_files", TRUE);
1000 hidden_file_extensions = utils_get_setting_string(config, "filebrowser", "hidden_file_extensions",
1001 ".o .obj .so .dll .a .lib .pyc");
1002 fb_follow_path = g_key_file_get_boolean(config, "filebrowser", "fb_follow_path", NULL);
1003 fb_set_project_base_path = g_key_file_get_boolean(config, "filebrowser", "fb_set_project_base_path", NULL);
1005 g_key_file_free(config);
1009 static void project_change_cb(G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GKeyFile *config,
1010 G_GNUC_UNUSED gpointer data)
1012 gchar *new_dir;
1013 GeanyProject *project = geany->app->project;
1015 if (! fb_set_project_base_path || project == NULL || EMPTY(project->base_path))
1016 return;
1018 /* TODO this is a copy of project_get_base_path(), add it to the plugin API */
1019 if (g_path_is_absolute(project->base_path))
1020 new_dir = g_strdup(project->base_path);
1021 else
1022 { /* build base_path out of project file name's dir and base_path */
1023 gchar *dir = g_path_get_dirname(project->file_name);
1025 new_dir = g_strconcat(dir, G_DIR_SEPARATOR_S, project->base_path, NULL);
1026 g_free(dir);
1028 /* get it into locale encoding */
1029 SETPTR(new_dir, utils_get_locale_from_utf8(new_dir));
1031 if (! utils_str_equal(current_dir, new_dir))
1033 SETPTR(current_dir, new_dir);
1034 refresh();
1036 else
1037 g_free(new_dir);
1041 static gpointer last_activate_path = NULL;
1043 static void document_activate_cb(G_GNUC_UNUSED GObject *obj, GeanyDocument *doc,
1044 G_GNUC_UNUSED gpointer data)
1046 gchar *new_dir;
1048 last_activate_path = doc->real_path;
1050 if (! fb_follow_path || doc->file_name == NULL || ! g_path_is_absolute(doc->file_name))
1051 return;
1053 new_dir = g_path_get_dirname(doc->file_name);
1054 SETPTR(new_dir, utils_get_locale_from_utf8(new_dir));
1056 if (! utils_str_equal(current_dir, new_dir))
1058 SETPTR(current_dir, new_dir);
1059 refresh();
1061 else
1062 g_free(new_dir);
1066 static void document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
1068 if (!last_activate_path)
1069 document_activate_cb(obj, doc, user_data);
1073 static void kb_activate(guint key_id)
1075 gtk_notebook_set_current_page(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook), page_number);
1076 switch (key_id)
1078 case KB_FOCUS_FILE_LIST:
1079 gtk_widget_grab_focus(file_view);
1080 break;
1081 case KB_FOCUS_PATH_ENTRY:
1082 gtk_widget_grab_focus(path_entry);
1083 break;
1088 void plugin_init(GeanyData *data)
1090 GtkWidget *scrollwin, *toolbar, *filterbar;
1092 filter = NULL;
1094 file_view_vbox = gtk_vbox_new(FALSE, 0);
1095 toolbar = make_toolbar();
1096 gtk_box_pack_start(GTK_BOX(file_view_vbox), toolbar, FALSE, FALSE, 0);
1098 filterbar = make_filterbar();
1099 gtk_box_pack_start(GTK_BOX(file_view_vbox), filterbar, FALSE, FALSE, 0);
1101 path_combo = gtk_combo_box_text_new_with_entry();
1102 gtk_box_pack_start(GTK_BOX(file_view_vbox), path_combo, FALSE, FALSE, 2);
1103 g_signal_connect(path_combo, "changed", G_CALLBACK(ui_combo_box_changed), NULL);
1104 path_entry = gtk_bin_get_child(GTK_BIN(path_combo));
1105 g_signal_connect(path_entry, "activate", G_CALLBACK(on_path_entry_activate), NULL);
1107 file_view = gtk_tree_view_new();
1108 prepare_file_view();
1109 completion_create();
1111 popup_items.open = popup_items.open_external = popup_items.find_in_files = NULL;
1113 scrollwin = gtk_scrolled_window_new(NULL, NULL);
1114 gtk_scrolled_window_set_policy(
1115 GTK_SCROLLED_WINDOW(scrollwin),
1116 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1117 gtk_container_add(GTK_CONTAINER(scrollwin), file_view);
1118 gtk_box_pack_start(GTK_BOX(file_view_vbox), scrollwin, TRUE, TRUE, 0);
1120 /* load settings before file_view "realize" callback */
1121 load_settings();
1123 gtk_widget_show_all(file_view_vbox);
1124 page_number = gtk_notebook_append_page(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
1125 file_view_vbox, gtk_label_new(_("Files")));
1127 /* setup keybindings */
1128 keybindings_set_item(plugin_key_group, KB_FOCUS_FILE_LIST, kb_activate,
1129 0, 0, "focus_file_list", _("Focus File List"), NULL);
1130 keybindings_set_item(plugin_key_group, KB_FOCUS_PATH_ENTRY, kb_activate,
1131 0, 0, "focus_path_entry", _("Focus Path Entry"), NULL);
1133 plugin_signal_connect(geany_plugin, NULL, "document-activate", TRUE,
1134 (GCallback) &document_activate_cb, NULL);
1135 plugin_signal_connect(geany_plugin, NULL, "document-save", TRUE,
1136 (GCallback) &document_save_cb, NULL);
1140 static void save_settings(void)
1142 GKeyFile *config = g_key_file_new();
1143 gchar *data;
1144 gchar *config_dir = g_path_get_dirname(config_file);
1146 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
1148 g_key_file_set_string(config, "filebrowser", "open_command", open_cmd);
1149 g_key_file_set_boolean(config, "filebrowser", "show_hidden_files", show_hidden_files);
1150 g_key_file_set_boolean(config, "filebrowser", "hide_object_files", hide_object_files);
1151 g_key_file_set_string(config, "filebrowser", "hidden_file_extensions", hidden_file_extensions);
1152 g_key_file_set_boolean(config, "filebrowser", "fb_follow_path", fb_follow_path);
1153 g_key_file_set_boolean(config, "filebrowser", "fb_set_project_base_path",
1154 fb_set_project_base_path);
1156 if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
1158 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
1159 _("Plugin configuration directory could not be created."));
1161 else
1163 /* write config to file */
1164 data = g_key_file_to_data(config, NULL, NULL);
1165 utils_write_file(config_file, data);
1166 g_free(data);
1168 g_free(config_dir);
1169 g_key_file_free(config);
1173 static struct
1175 GtkWidget *open_cmd_entry;
1176 GtkWidget *show_hidden_checkbox;
1177 GtkWidget *hide_objects_checkbox;
1178 GtkWidget *hidden_files_entry;
1179 GtkWidget *follow_path_checkbox;
1180 GtkWidget *set_project_base_path_checkbox;
1182 pref_widgets;
1184 static void
1185 on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
1187 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
1189 g_free(open_cmd);
1190 open_cmd = g_strdup(gtk_entry_get_text(GTK_ENTRY(pref_widgets.open_cmd_entry)));
1191 show_hidden_files = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.show_hidden_checkbox));
1192 hide_object_files = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.hide_objects_checkbox));
1193 g_free(hidden_file_extensions);
1194 hidden_file_extensions = g_strdup(gtk_entry_get_text(GTK_ENTRY(pref_widgets.hidden_files_entry)));
1195 fb_follow_path = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.follow_path_checkbox));
1196 fb_set_project_base_path = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
1197 pref_widgets.set_project_base_path_checkbox));
1199 /* apply the changes */
1200 refresh();
1205 static void on_toggle_hidden(void)
1207 gboolean enabled = !gtk_toggle_button_get_active(
1208 GTK_TOGGLE_BUTTON(pref_widgets.show_hidden_checkbox));
1210 gtk_widget_set_sensitive(pref_widgets.hide_objects_checkbox, enabled);
1211 enabled &= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.hide_objects_checkbox));
1212 gtk_widget_set_sensitive(pref_widgets.hidden_files_entry, enabled);
1216 GtkWidget *plugin_configure(GtkDialog *dialog)
1218 GtkWidget *label, *entry, *checkbox_of, *checkbox_hf, *checkbox_fp, *checkbox_pb, *vbox;
1219 GtkWidget *box, *align;
1221 vbox = gtk_vbox_new(FALSE, 6);
1222 box = gtk_vbox_new(FALSE, 3);
1224 label = gtk_label_new(_("External open command:"));
1225 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1226 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
1228 entry = gtk_entry_new();
1229 if (open_cmd != NULL)
1230 gtk_entry_set_text(GTK_ENTRY(entry), open_cmd);
1231 gtk_widget_set_tooltip_text(entry,
1232 _("The command to execute when using \"Open with\". You can use %f and %d wildcards.\n"
1233 "%f will be replaced with the filename including full path\n"
1234 "%d will be replaced with the path name of the selected file without the filename"));
1235 gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 0);
1236 pref_widgets.open_cmd_entry = entry;
1238 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 3);
1240 checkbox_hf = gtk_check_button_new_with_label(_("Show hidden files"));
1241 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_hf), FALSE);
1242 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_hf), show_hidden_files);
1243 gtk_box_pack_start(GTK_BOX(vbox), checkbox_hf, FALSE, FALSE, 0);
1244 pref_widgets.show_hidden_checkbox = checkbox_hf;
1245 g_signal_connect(checkbox_hf, "toggled", on_toggle_hidden, NULL);
1247 box = gtk_vbox_new(FALSE, 3);
1248 checkbox_of = gtk_check_button_new_with_label(_("Hide file extensions:"));
1249 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_of), FALSE);
1250 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_of), hide_object_files);
1251 gtk_box_pack_start(GTK_BOX(box), checkbox_of, FALSE, FALSE, 0);
1252 pref_widgets.hide_objects_checkbox = checkbox_of;
1253 g_signal_connect(checkbox_of, "toggled", on_toggle_hidden, NULL);
1255 entry = gtk_entry_new();
1256 if (hidden_file_extensions != NULL)
1257 gtk_entry_set_text(GTK_ENTRY(entry), hidden_file_extensions);
1258 gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 0);
1259 pref_widgets.hidden_files_entry = entry;
1261 align = gtk_alignment_new(1, 0.5, 1, 1);
1262 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1263 gtk_container_add(GTK_CONTAINER(align), box);
1264 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, FALSE, 0);
1265 on_toggle_hidden();
1267 checkbox_fp = gtk_check_button_new_with_label(_("Follow the path of the current file"));
1268 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_fp), FALSE);
1269 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_fp), fb_follow_path);
1270 gtk_box_pack_start(GTK_BOX(vbox), checkbox_fp, FALSE, FALSE, 0);
1271 pref_widgets.follow_path_checkbox = checkbox_fp;
1273 checkbox_pb = gtk_check_button_new_with_label(_("Use the project's base directory"));
1274 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_pb), FALSE);
1275 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_pb), fb_set_project_base_path);
1276 gtk_widget_set_tooltip_text(checkbox_pb,
1277 _("Change the directory to the base directory of the currently opened project"));
1278 gtk_box_pack_start(GTK_BOX(vbox), checkbox_pb, FALSE, FALSE, 0);
1279 pref_widgets.set_project_base_path_checkbox = checkbox_pb;
1281 gtk_widget_show_all(vbox);
1283 g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
1284 return vbox;
1288 void plugin_cleanup(void)
1290 save_settings();
1292 g_free(config_file);
1293 g_free(open_cmd);
1294 g_free(hidden_file_extensions);
1295 clear_filter();
1296 gtk_widget_destroy(file_view_vbox);
1297 g_object_unref(G_OBJECT(entry_completion));