Merge pull request #735 from crayxt/patch-1
[geany-mirror.git] / plugins / filebrowser.c
blobb5e6ecbcfe39a19779a665f177b9ded254d7d49a
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;
48 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
50 PLUGIN_SET_INFO(_("File Browser"), _("Adds a file browser tab to the sidebar."), VERSION,
51 _("The Geany developer team"))
54 /* Keybinding(s) */
55 enum
57 KB_FOCUS_FILE_LIST,
58 KB_FOCUS_PATH_ENTRY,
59 KB_COUNT
62 PLUGIN_KEY_GROUP(file_browser, KB_COUNT)
65 enum
67 FILEVIEW_COLUMN_ICON = 0,
68 FILEVIEW_COLUMN_NAME,
69 FILEVIEW_COLUMN_FILENAME, /* the full filename, including path for display as tooltip */
70 FILEVIEW_COLUMN_IS_DIR,
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 static GIcon *get_icon(const gchar *fname)
189 GIcon *icon = NULL;
190 gchar *ctype;
192 ctype = g_content_type_guess(fname, NULL, 0, NULL);
194 if (ctype)
196 icon = g_content_type_get_icon(ctype);
197 if (icon)
199 GtkIconInfo *icon_info;
201 icon_info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), icon, 16, 0);
202 if (!icon_info)
204 g_object_unref(icon);
205 icon = NULL;
207 else
208 gtk_icon_info_free(icon_info);
210 g_free(ctype);
213 if (!icon)
214 icon = g_themed_icon_new("text-x-generic");
216 return icon;
220 /* name is in locale encoding */
221 static void add_item(const gchar *name)
223 GtkTreeIter iter;
224 gchar *fname, *utf8_name, *utf8_fullname;
225 const gchar *sep;
226 gboolean dir;
227 GIcon *icon;
229 if (G_UNLIKELY(EMPTY(name)))
230 return;
232 /* root directory doesn't need separator */
233 sep = (utils_str_equal(current_dir, "/")) ? "" : G_DIR_SEPARATOR_S;
234 fname = g_strconcat(current_dir, sep, name, NULL);
235 dir = g_file_test(fname, G_FILE_TEST_IS_DIR);
236 utf8_fullname = utils_get_locale_from_utf8(fname);
237 utf8_name = utils_get_utf8_from_locale(name);
238 g_free(fname);
240 if (! show_hidden_files && check_hidden(utf8_fullname, utf8_name))
241 goto done;
243 if (dir)
245 if (last_dir_iter == NULL)
246 gtk_list_store_prepend(file_store, &iter);
247 else
249 gtk_list_store_insert_after(file_store, &iter, last_dir_iter);
250 gtk_tree_iter_free(last_dir_iter);
252 last_dir_iter = gtk_tree_iter_copy(&iter);
254 else
256 if (! show_hidden_files && hide_object_files && check_object(utf8_name))
257 goto done;
258 if (check_filtered(utf8_name))
259 goto done;
261 gtk_list_store_append(file_store, &iter);
264 icon = dir ? g_themed_icon_new("folder") : get_icon(utf8_name);
265 gtk_list_store_set(file_store, &iter,
266 FILEVIEW_COLUMN_ICON, icon,
267 FILEVIEW_COLUMN_NAME, utf8_name,
268 FILEVIEW_COLUMN_FILENAME, utf8_fullname,
269 FILEVIEW_COLUMN_IS_DIR, dir,
270 -1);
271 g_object_unref(icon);
272 done:
273 g_free(utf8_name);
274 g_free(utf8_fullname);
278 /* adds ".." to the start of the file list */
279 static void add_top_level_entry(void)
281 GtkTreeIter iter;
282 gchar *utf8_dir;
283 GIcon *icon;
285 if (EMPTY(g_path_skip_root(current_dir)))
286 return; /* ignore 'C:\' or '/' */
288 utf8_dir = g_path_get_dirname(current_dir);
289 SETPTR(utf8_dir, utils_get_utf8_from_locale(utf8_dir));
291 gtk_list_store_prepend(file_store, &iter);
292 last_dir_iter = gtk_tree_iter_copy(&iter);
294 icon = g_themed_icon_new("folder");
295 gtk_list_store_set(file_store, &iter,
296 FILEVIEW_COLUMN_ICON, icon,
297 FILEVIEW_COLUMN_NAME, "..",
298 FILEVIEW_COLUMN_FILENAME, utf8_dir,
299 FILEVIEW_COLUMN_IS_DIR, TRUE,
300 -1);
301 g_object_unref(icon);
302 g_free(utf8_dir);
306 static void clear(void)
308 gtk_list_store_clear(file_store);
310 /* reset the directory item pointer */
311 if (last_dir_iter != NULL)
312 gtk_tree_iter_free(last_dir_iter);
313 last_dir_iter = NULL;
317 /* recreate the tree model from current_dir. */
318 static void refresh(void)
320 gchar *utf8_dir;
321 GSList *list, *node;
323 /* don't clear when the new path doesn't exist */
324 if (! g_file_test(current_dir, G_FILE_TEST_EXISTS))
325 return;
327 clear();
329 utf8_dir = utils_get_utf8_from_locale(current_dir);
330 gtk_entry_set_text(GTK_ENTRY(path_entry), utf8_dir);
331 gtk_widget_set_tooltip_text(path_entry, utf8_dir);
332 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(path_combo), utf8_dir, 0);
333 g_free(utf8_dir);
335 add_top_level_entry(); /* ".." item */
337 list = utils_get_file_list(current_dir, NULL, NULL);
338 if (list != NULL)
340 /* free filenames as we go through the list */
341 foreach_slist(node, list)
343 gchar *fname = node->data;
345 add_item(fname);
346 g_free(fname);
348 g_slist_free(list);
350 gtk_entry_completion_set_model(entry_completion, GTK_TREE_MODEL(file_store));
354 static void on_go_home(void)
356 SETPTR(current_dir, g_strdup(g_get_home_dir()));
357 refresh();
361 /* TODO: use utils_get_default_dir_utf8() */
362 static gchar *get_default_dir(void)
364 const gchar *dir = NULL;
365 GeanyProject *project = geany->app->project;
367 if (project)
368 dir = project->base_path;
369 else
370 dir = geany->prefs->default_open_path;
372 if (!EMPTY(dir))
373 return utils_get_locale_from_utf8(dir);
375 return g_get_current_dir();
379 static void on_current_path(void)
381 gchar *fname;
382 gchar *dir;
383 GeanyDocument *doc = document_get_current();
385 if (doc == NULL || doc->file_name == NULL || ! g_path_is_absolute(doc->file_name))
387 SETPTR(current_dir, get_default_dir());
388 refresh();
389 return;
391 fname = doc->file_name;
392 fname = utils_get_locale_from_utf8(fname);
393 dir = g_path_get_dirname(fname);
394 g_free(fname);
396 SETPTR(current_dir, dir);
397 refresh();
401 static void on_go_up(void)
403 gsize len = strlen(current_dir);
404 if (current_dir[len-1] == G_DIR_SEPARATOR)
405 current_dir[len-1] = '\0';
406 /* remove the highest directory part (which becomes the basename of current_dir) */
407 SETPTR(current_dir, g_path_get_dirname(current_dir));
408 refresh();
412 static gboolean check_single_selection(GtkTreeSelection *treesel)
414 if (gtk_tree_selection_count_selected_rows(treesel) == 1)
415 return TRUE;
417 ui_set_statusbar(FALSE, _("Too many items selected!"));
418 return FALSE;
422 /* Returns: TRUE if at least one of selected_items is a folder. */
423 static gboolean is_folder_selected(GList *selected_items)
425 GList *item;
426 GtkTreeModel *model = GTK_TREE_MODEL(file_store);
427 gboolean dir_found = FALSE;
429 for (item = selected_items; item != NULL; item = g_list_next(item))
431 GtkTreeIter iter;
432 GtkTreePath *treepath;
434 treepath = (GtkTreePath*) item->data;
435 gtk_tree_model_get_iter(model, &iter, treepath);
436 gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_IS_DIR, &dir_found, -1);
438 if (dir_found)
439 break;
441 return dir_found;
445 /* Returns: the full filename in locale encoding. */
446 static gchar *get_tree_path_filename(GtkTreePath *treepath)
448 GtkTreeModel *model = GTK_TREE_MODEL(file_store);
449 GtkTreeIter iter;
450 gchar *name, *fname;
452 gtk_tree_model_get_iter(model, &iter, treepath);
453 gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_FILENAME, &name, -1);
455 fname = utils_get_locale_from_utf8(name);
456 g_free(name);
458 return fname;
462 static void open_external(const gchar *fname, gboolean dir_found)
464 gchar *cmd;
465 gchar *locale_cmd;
466 gchar *dir;
467 GString *cmd_str = g_string_new(open_cmd);
468 GError *error = NULL;
470 if (! dir_found)
471 dir = g_path_get_dirname(fname);
472 else
473 dir = g_strdup(fname);
475 utils_string_replace_all(cmd_str, "%f", fname);
476 utils_string_replace_all(cmd_str, "%d", dir);
478 cmd = g_string_free(cmd_str, FALSE);
479 locale_cmd = utils_get_locale_from_utf8(cmd);
480 if (! spawn_async(NULL, locale_cmd, NULL, NULL, NULL, &error))
482 gchar *c = strchr(cmd, ' ');
484 if (c != NULL)
485 *c = '\0';
486 ui_set_statusbar(TRUE,
487 _("Could not execute configured external command '%s' (%s)."),
488 cmd, error->message);
489 g_error_free(error);
491 g_free(locale_cmd);
492 g_free(cmd);
493 g_free(dir);
497 static void on_external_open(GtkMenuItem *menuitem, gpointer user_data)
499 GtkTreeSelection *treesel;
500 GtkTreeModel *model;
501 GList *list;
502 gboolean dir_found;
504 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
506 list = gtk_tree_selection_get_selected_rows(treesel, &model);
507 dir_found = is_folder_selected(list);
509 if (! dir_found || check_single_selection(treesel))
511 GList *item;
513 for (item = list; item != NULL; item = g_list_next(item))
515 GtkTreePath *treepath = item->data;
516 gchar *fname = get_tree_path_filename(treepath);
518 open_external(fname, dir_found);
519 g_free(fname);
523 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
524 g_list_free(list);
528 /* We use document_open_files() as it's more efficient. */
529 static void open_selected_files(GList *list, gboolean do_not_focus)
531 GSList *files = NULL;
532 GList *item;
533 GeanyDocument *doc;
535 for (item = list; item != NULL; item = g_list_next(item))
537 GtkTreePath *treepath = item->data;
538 gchar *fname = get_tree_path_filename(treepath);
540 files = g_slist_prepend(files, fname);
542 files = g_slist_reverse(files);
543 document_open_files(files, FALSE, NULL, NULL);
544 doc = document_get_current();
545 if (doc != NULL && ! do_not_focus)
546 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
548 g_slist_foreach(files, (GFunc) g_free, NULL); /* free filenames */
549 g_slist_free(files);
553 static void open_folder(GtkTreePath *treepath)
555 gchar *fname = get_tree_path_filename(treepath);
557 SETPTR(current_dir, fname);
558 refresh();
562 static void on_open_clicked(GtkMenuItem *menuitem, gpointer user_data)
564 GtkTreeSelection *treesel;
565 GtkTreeModel *model;
566 GList *list;
567 gboolean dir_found;
569 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
571 list = gtk_tree_selection_get_selected_rows(treesel, &model);
572 dir_found = is_folder_selected(list);
574 if (dir_found)
576 if (check_single_selection(treesel))
578 GtkTreePath *treepath = list->data; /* first selected item */
580 open_folder(treepath);
583 else
584 open_selected_files(list, GPOINTER_TO_INT(user_data));
586 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
587 g_list_free(list);
591 static void on_find_in_files(GtkMenuItem *menuitem, gpointer user_data)
593 GtkTreeSelection *treesel;
594 GtkTreeModel *model;
595 GList *list;
596 gchar *dir;
597 gboolean is_dir = FALSE;
599 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
600 /* allow 0 or 1 selections */
601 if (gtk_tree_selection_count_selected_rows(treesel) > 0 &&
602 ! check_single_selection(treesel))
603 return;
605 list = gtk_tree_selection_get_selected_rows(treesel, &model);
606 is_dir = is_folder_selected(list);
608 if (is_dir)
610 GtkTreePath *treepath = list->data; /* first selected item */
612 dir = get_tree_path_filename(treepath);
614 else
615 dir = g_strdup(current_dir);
617 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
618 g_list_free(list);
620 SETPTR(dir, utils_get_utf8_from_locale(dir));
621 search_show_find_in_files_dialog(dir);
622 g_free(dir);
626 static void on_hidden_files_clicked(GtkCheckMenuItem *item)
628 show_hidden_files = gtk_check_menu_item_get_active(item);
629 refresh();
633 static void on_hide_sidebar(void)
635 keybindings_send_command(GEANY_KEY_GROUP_VIEW, GEANY_KEYS_VIEW_SIDEBAR);
639 static void on_show_preferences(void)
641 plugin_show_configure(geany_plugin);
645 static GtkWidget *create_popup_menu(void)
647 GtkWidget *item, *menu;
649 menu = gtk_menu_new();
651 item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open in _Geany"));
652 gtk_widget_show(item);
653 gtk_container_add(GTK_CONTAINER(menu), item);
654 g_signal_connect(item, "activate", G_CALLBACK(on_open_clicked), NULL);
655 popup_items.open = item;
657 item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open _Externally"));
658 gtk_widget_show(item);
659 gtk_container_add(GTK_CONTAINER(menu), item);
660 g_signal_connect(item, "activate", G_CALLBACK(on_external_open), NULL);
661 popup_items.open_external = item;
663 item = gtk_separator_menu_item_new();
664 gtk_widget_show(item);
665 gtk_container_add(GTK_CONTAINER(menu), item);
667 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_REFRESH, NULL);
668 gtk_widget_show(item);
669 gtk_container_add(GTK_CONTAINER(menu), item);
670 g_signal_connect(item, "activate", G_CALLBACK(refresh), NULL);
672 item = ui_image_menu_item_new(GTK_STOCK_FIND, _("_Find in Files..."));
673 gtk_widget_show(item);
674 gtk_container_add(GTK_CONTAINER(menu), item);
675 g_signal_connect(item, "activate", G_CALLBACK(on_find_in_files), NULL);
676 popup_items.find_in_files = item;
678 item = gtk_separator_menu_item_new();
679 gtk_widget_show(item);
680 gtk_container_add(GTK_CONTAINER(menu), item);
682 item = gtk_check_menu_item_new_with_mnemonic(_("Show _Hidden Files"));
683 gtk_widget_show(item);
684 gtk_container_add(GTK_CONTAINER(menu), item);
685 g_signal_connect(item, "activate", G_CALLBACK(on_hidden_files_clicked), NULL);
686 popup_items.show_hidden_files = item;
688 item = gtk_separator_menu_item_new();
689 gtk_widget_show(item);
690 gtk_container_add(GTK_CONTAINER(menu), item);
692 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
693 gtk_widget_show(item);
694 gtk_container_add(GTK_CONTAINER(menu), item);
695 g_signal_connect(item, "activate", G_CALLBACK(on_show_preferences), NULL);
697 item = gtk_separator_menu_item_new();
698 gtk_widget_show(item);
699 gtk_container_add(GTK_CONTAINER(menu), item);
701 item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("H_ide Sidebar"));
702 gtk_widget_show(item);
703 gtk_container_add(GTK_CONTAINER(menu), item);
704 g_signal_connect(item, "activate", G_CALLBACK(on_hide_sidebar), NULL);
706 return menu;
710 static void on_tree_selection_changed(GtkTreeSelection *selection, gpointer data)
712 gboolean have_sel = (gtk_tree_selection_count_selected_rows(selection) > 0);
713 gboolean multi_sel = (gtk_tree_selection_count_selected_rows(selection) > 1);
715 if (popup_items.open != NULL)
716 gtk_widget_set_sensitive(popup_items.open, have_sel);
717 if (popup_items.open_external != NULL)
718 gtk_widget_set_sensitive(popup_items.open_external, have_sel);
719 if (popup_items.find_in_files != NULL)
720 gtk_widget_set_sensitive(popup_items.find_in_files, have_sel && ! multi_sel);
724 static gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
726 if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
728 on_open_clicked(NULL, NULL);
729 return TRUE;
731 else if (event->button == 3)
733 static GtkWidget *popup_menu = NULL;
735 if (popup_menu == NULL)
736 popup_menu = create_popup_menu();
738 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(popup_items.show_hidden_files),
739 show_hidden_files);
740 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, NULL, NULL, event->button, event->time);
741 /* don't return TRUE here, unless the selection won't be changed */
743 return FALSE;
747 static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
749 if (ui_is_keyval_enter_or_return(event->keyval))
751 on_open_clicked(NULL, NULL);
752 return TRUE;
755 if (event->keyval == GDK_space)
757 on_open_clicked(NULL, GINT_TO_POINTER(TRUE));
758 return TRUE;
761 if (( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && (event->state & GDK_MOD1_MASK)) || /* FIXME: Alt-Up doesn't seem to work! */
762 (event->keyval == GDK_BackSpace) )
764 on_go_up();
765 return TRUE;
768 if ((event->keyval == GDK_F10 && event->state & GDK_SHIFT_MASK) || event->keyval == GDK_Menu)
770 GdkEventButton button_event;
772 button_event.time = event->time;
773 button_event.button = 3;
775 on_button_press(widget, &button_event, data);
776 return TRUE;
779 return FALSE;
783 static void clear_filter(void)
785 if (filter != NULL)
787 g_strfreev(filter);
788 filter = NULL;
793 static void on_clear_filter(GtkEntry *entry, gpointer user_data)
795 clear_filter();
797 gtk_entry_set_text(GTK_ENTRY(filter_entry), "");
799 refresh();
803 static void on_path_entry_activate(GtkEntry *entry, gpointer user_data)
805 gchar *new_dir = (gchar*) gtk_entry_get_text(entry);
807 if (!EMPTY(new_dir))
809 if (g_str_has_suffix(new_dir, ".."))
811 on_go_up();
812 return;
814 else if (new_dir[0] == '~')
816 GString *str = g_string_new(new_dir);
817 utils_string_replace_first(str, "~", g_get_home_dir());
818 new_dir = g_string_free(str, FALSE);
820 else
821 new_dir = utils_get_locale_from_utf8(new_dir);
823 else
824 new_dir = g_strdup(g_get_home_dir());
826 SETPTR(current_dir, new_dir);
828 on_clear_filter(NULL, NULL);
832 static void ui_combo_box_changed(GtkComboBox *combo, gpointer user_data)
834 /* we get this callback on typing as well as choosing an item */
835 if (gtk_combo_box_get_active(combo) >= 0)
836 gtk_widget_activate(gtk_bin_get_child(GTK_BIN(combo)));
840 static void on_filter_activate(GtkEntry *entry, gpointer user_data)
842 /* We use spaces for consistency with Find in Files file patterns
843 * ';' also supported like original patch. */
844 filter = g_strsplit_set(gtk_entry_get_text(entry), "; ", -1);
845 if (filter == NULL || g_strv_length(filter) == 0)
847 clear_filter();
849 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(filter_combo), NULL, 0);
850 refresh();
854 static void on_filter_clear(GtkEntry *entry, gint icon_pos,
855 GdkEvent *event, gpointer data)
857 clear_filter();
858 refresh();
862 static void prepare_file_view(void)
864 GtkCellRenderer *text_renderer, *icon_renderer;
865 GtkTreeViewColumn *column;
866 GtkTreeSelection *selection;
868 file_store = gtk_list_store_new(FILEVIEW_N_COLUMNS, G_TYPE_ICON, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
870 gtk_tree_view_set_model(GTK_TREE_VIEW(file_view), GTK_TREE_MODEL(file_store));
871 g_object_unref(file_store);
873 icon_renderer = gtk_cell_renderer_pixbuf_new();
874 text_renderer = gtk_cell_renderer_text_new();
875 column = gtk_tree_view_column_new();
876 gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
877 gtk_tree_view_column_set_attributes(column, icon_renderer, "gicon", FILEVIEW_COLUMN_ICON, NULL);
878 gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
879 gtk_tree_view_column_set_attributes(column, text_renderer, "text", FILEVIEW_COLUMN_NAME, NULL);
880 gtk_tree_view_append_column(GTK_TREE_VIEW(file_view), column);
881 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(file_view), FALSE);
883 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(file_view), TRUE);
884 gtk_tree_view_set_search_column(GTK_TREE_VIEW(file_view), FILEVIEW_COLUMN_NAME);
886 ui_widget_modify_font_from_string(file_view, geany->interface_prefs->tagbar_font);
888 /* tooltips */
889 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(file_view), FILEVIEW_COLUMN_FILENAME);
891 /* selection handling */
892 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
893 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
895 /* Show the current path when the FB is first needed */
896 g_signal_connect(file_view, "realize", G_CALLBACK(on_current_path), NULL);
897 g_signal_connect(selection, "changed", G_CALLBACK(on_tree_selection_changed), NULL);
898 g_signal_connect(file_view, "button-press-event", G_CALLBACK(on_button_press), NULL);
899 g_signal_connect(file_view, "key-press-event", G_CALLBACK(on_key_press), NULL);
903 static GtkWidget *make_toolbar(void)
905 GtkWidget *wid, *toolbar;
907 toolbar = gtk_toolbar_new();
908 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
909 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
911 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_GO_UP));
912 gtk_widget_set_tooltip_text(wid, _("Up"));
913 g_signal_connect(wid, "clicked", G_CALLBACK(on_go_up), NULL);
914 gtk_container_add(GTK_CONTAINER(toolbar), wid);
916 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH));
917 gtk_widget_set_tooltip_text(wid, _("Refresh"));
918 g_signal_connect(wid, "clicked", G_CALLBACK(refresh), NULL);
919 gtk_container_add(GTK_CONTAINER(toolbar), wid);
921 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_HOME));
922 gtk_widget_set_tooltip_text(wid, _("Home"));
923 g_signal_connect(wid, "clicked", G_CALLBACK(on_go_home), NULL);
924 gtk_container_add(GTK_CONTAINER(toolbar), wid);
926 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_JUMP_TO));
927 gtk_widget_set_tooltip_text(wid, _("Set path from document"));
928 g_signal_connect(wid, "clicked", G_CALLBACK(on_current_path), NULL);
929 gtk_container_add(GTK_CONTAINER(toolbar), wid);
931 return toolbar;
935 static GtkWidget *make_filterbar(void)
937 GtkWidget *label, *filterbar;
939 filterbar = gtk_hbox_new(FALSE, 1);
941 label = gtk_label_new(_("Filter:"));
943 filter_combo = gtk_combo_box_text_new_with_entry();
944 filter_entry = gtk_bin_get_child(GTK_BIN(filter_combo));
946 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry));
947 g_signal_connect(filter_entry, "icon-release", G_CALLBACK(on_filter_clear), NULL);
949 gtk_widget_set_tooltip_text(filter_entry,
950 _("Filter your files with the usual wildcards. Separate multiple patterns with a space."));
951 g_signal_connect(filter_entry, "activate", G_CALLBACK(on_filter_activate), NULL);
952 g_signal_connect(filter_combo, "changed", G_CALLBACK(ui_combo_box_changed), NULL);
954 gtk_box_pack_start(GTK_BOX(filterbar), label, FALSE, FALSE, 0);
955 gtk_box_pack_start(GTK_BOX(filterbar), filter_combo, TRUE, TRUE, 0);
957 return filterbar;
961 static gboolean completion_match_func(GtkEntryCompletion *completion, const gchar *key,
962 GtkTreeIter *iter, gpointer user_data)
964 gchar *str;
965 gboolean is_dir;
966 gboolean result = FALSE;
968 gtk_tree_model_get(GTK_TREE_MODEL(file_store), iter,
969 FILEVIEW_COLUMN_IS_DIR, &is_dir, FILEVIEW_COLUMN_NAME, &str, -1);
971 if (str != NULL && is_dir && !g_str_has_suffix(key, G_DIR_SEPARATOR_S))
973 /* key is something like "/tmp/te" and str is a filename like "test",
974 * so strip the path from key to make them comparable */
975 gchar *base_name = g_path_get_basename(key);
976 gchar *str_lowered = g_utf8_strdown(str, -1);
977 result = g_str_has_prefix(str_lowered, base_name);
978 g_free(base_name);
979 g_free(str_lowered);
981 g_free(str);
983 return result;
987 static gboolean completion_match_selected(GtkEntryCompletion *widget, GtkTreeModel *model,
988 GtkTreeIter *iter, gpointer user_data)
990 gchar *str;
991 gtk_tree_model_get(model, iter, FILEVIEW_COLUMN_NAME, &str, -1);
992 if (str != NULL)
994 gchar *text = g_strconcat(current_dir, G_DIR_SEPARATOR_S, str, NULL);
995 gtk_entry_set_text(GTK_ENTRY(path_entry), text);
996 gtk_editable_set_position(GTK_EDITABLE(path_entry), -1);
997 /* force change of directory when completion is done */
998 on_path_entry_activate(GTK_ENTRY(path_entry), NULL);
999 g_free(text);
1001 g_free(str);
1003 return TRUE;
1007 static void completion_create(void)
1009 entry_completion = gtk_entry_completion_new();
1011 gtk_entry_completion_set_inline_completion(entry_completion, FALSE);
1012 gtk_entry_completion_set_popup_completion(entry_completion, TRUE);
1013 gtk_entry_completion_set_text_column(entry_completion, FILEVIEW_COLUMN_NAME);
1014 gtk_entry_completion_set_match_func(entry_completion, completion_match_func, NULL, NULL);
1016 g_signal_connect(entry_completion, "match-selected",
1017 G_CALLBACK(completion_match_selected), NULL);
1019 gtk_entry_set_completion(GTK_ENTRY(path_entry), entry_completion);
1023 static void load_settings(void)
1025 GKeyFile *config = g_key_file_new();
1027 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
1028 "filebrowser", G_DIR_SEPARATOR_S, "filebrowser.conf", NULL);
1029 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
1031 open_cmd = utils_get_setting_string(config, "filebrowser", "open_command", OPEN_CMD);
1032 /* g_key_file_get_boolean defaults to FALSE */
1033 show_hidden_files = g_key_file_get_boolean(config, "filebrowser", "show_hidden_files", NULL);
1034 hide_object_files = utils_get_setting_boolean(config, "filebrowser", "hide_object_files", TRUE);
1035 hidden_file_extensions = utils_get_setting_string(config, "filebrowser", "hidden_file_extensions",
1036 ".o .obj .so .dll .a .lib .pyc");
1037 fb_follow_path = g_key_file_get_boolean(config, "filebrowser", "fb_follow_path", NULL);
1038 fb_set_project_base_path = g_key_file_get_boolean(config, "filebrowser", "fb_set_project_base_path", NULL);
1040 g_key_file_free(config);
1044 static void project_change_cb(G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GKeyFile *config,
1045 G_GNUC_UNUSED gpointer data)
1047 gchar *new_dir;
1048 GeanyProject *project = geany->app->project;
1050 if (! fb_set_project_base_path || project == NULL || EMPTY(project->base_path))
1051 return;
1053 /* TODO this is a copy of project_get_base_path(), add it to the plugin API */
1054 if (g_path_is_absolute(project->base_path))
1055 new_dir = g_strdup(project->base_path);
1056 else
1057 { /* build base_path out of project file name's dir and base_path */
1058 gchar *dir = g_path_get_dirname(project->file_name);
1060 new_dir = g_strconcat(dir, G_DIR_SEPARATOR_S, project->base_path, NULL);
1061 g_free(dir);
1063 /* get it into locale encoding */
1064 SETPTR(new_dir, utils_get_locale_from_utf8(new_dir));
1066 if (! utils_str_equal(current_dir, new_dir))
1068 SETPTR(current_dir, new_dir);
1069 refresh();
1071 else
1072 g_free(new_dir);
1076 static gpointer last_activate_path = NULL;
1078 static void document_activate_cb(G_GNUC_UNUSED GObject *obj, GeanyDocument *doc,
1079 G_GNUC_UNUSED gpointer data)
1081 gchar *new_dir;
1083 last_activate_path = doc->real_path;
1085 if (! fb_follow_path || doc->file_name == NULL || ! g_path_is_absolute(doc->file_name))
1086 return;
1088 new_dir = g_path_get_dirname(doc->file_name);
1089 SETPTR(new_dir, utils_get_locale_from_utf8(new_dir));
1091 if (! utils_str_equal(current_dir, new_dir))
1093 SETPTR(current_dir, new_dir);
1094 refresh();
1096 else
1097 g_free(new_dir);
1101 static void document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
1103 if (!last_activate_path)
1104 document_activate_cb(obj, doc, user_data);
1108 static void kb_activate(guint key_id)
1110 gtk_notebook_set_current_page(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook), page_number);
1111 switch (key_id)
1113 case KB_FOCUS_FILE_LIST:
1114 gtk_widget_grab_focus(file_view);
1115 break;
1116 case KB_FOCUS_PATH_ENTRY:
1117 gtk_widget_grab_focus(path_entry);
1118 break;
1123 void plugin_init(GeanyData *data)
1125 GtkWidget *scrollwin, *toolbar, *filterbar;
1127 filter = NULL;
1129 file_view_vbox = gtk_vbox_new(FALSE, 0);
1130 toolbar = make_toolbar();
1131 gtk_box_pack_start(GTK_BOX(file_view_vbox), toolbar, FALSE, FALSE, 0);
1133 filterbar = make_filterbar();
1134 gtk_box_pack_start(GTK_BOX(file_view_vbox), filterbar, FALSE, FALSE, 0);
1136 path_combo = gtk_combo_box_text_new_with_entry();
1137 gtk_box_pack_start(GTK_BOX(file_view_vbox), path_combo, FALSE, FALSE, 2);
1138 g_signal_connect(path_combo, "changed", G_CALLBACK(ui_combo_box_changed), NULL);
1139 path_entry = gtk_bin_get_child(GTK_BIN(path_combo));
1140 g_signal_connect(path_entry, "activate", G_CALLBACK(on_path_entry_activate), NULL);
1142 file_view = gtk_tree_view_new();
1143 prepare_file_view();
1144 completion_create();
1146 popup_items.open = popup_items.open_external = popup_items.find_in_files = NULL;
1148 scrollwin = gtk_scrolled_window_new(NULL, NULL);
1149 gtk_scrolled_window_set_policy(
1150 GTK_SCROLLED_WINDOW(scrollwin),
1151 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1152 gtk_container_add(GTK_CONTAINER(scrollwin), file_view);
1153 gtk_box_pack_start(GTK_BOX(file_view_vbox), scrollwin, TRUE, TRUE, 0);
1155 /* load settings before file_view "realize" callback */
1156 load_settings();
1158 gtk_widget_show_all(file_view_vbox);
1159 page_number = gtk_notebook_append_page(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
1160 file_view_vbox, gtk_label_new(_("Files")));
1162 /* setup keybindings */
1163 keybindings_set_item(plugin_key_group, KB_FOCUS_FILE_LIST, kb_activate,
1164 0, 0, "focus_file_list", _("Focus File List"), NULL);
1165 keybindings_set_item(plugin_key_group, KB_FOCUS_PATH_ENTRY, kb_activate,
1166 0, 0, "focus_path_entry", _("Focus Path Entry"), NULL);
1168 plugin_signal_connect(geany_plugin, NULL, "document-activate", TRUE,
1169 (GCallback) &document_activate_cb, NULL);
1170 plugin_signal_connect(geany_plugin, NULL, "document-save", TRUE,
1171 (GCallback) &document_save_cb, NULL);
1175 static void save_settings(void)
1177 GKeyFile *config = g_key_file_new();
1178 gchar *data;
1179 gchar *config_dir = g_path_get_dirname(config_file);
1181 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
1183 g_key_file_set_string(config, "filebrowser", "open_command", open_cmd);
1184 g_key_file_set_boolean(config, "filebrowser", "show_hidden_files", show_hidden_files);
1185 g_key_file_set_boolean(config, "filebrowser", "hide_object_files", hide_object_files);
1186 g_key_file_set_string(config, "filebrowser", "hidden_file_extensions", hidden_file_extensions);
1187 g_key_file_set_boolean(config, "filebrowser", "fb_follow_path", fb_follow_path);
1188 g_key_file_set_boolean(config, "filebrowser", "fb_set_project_base_path",
1189 fb_set_project_base_path);
1191 if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
1193 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
1194 _("Plugin configuration directory could not be created."));
1196 else
1198 /* write config to file */
1199 data = g_key_file_to_data(config, NULL, NULL);
1200 utils_write_file(config_file, data);
1201 g_free(data);
1203 g_free(config_dir);
1204 g_key_file_free(config);
1208 static struct
1210 GtkWidget *open_cmd_entry;
1211 GtkWidget *show_hidden_checkbox;
1212 GtkWidget *hide_objects_checkbox;
1213 GtkWidget *hidden_files_entry;
1214 GtkWidget *follow_path_checkbox;
1215 GtkWidget *set_project_base_path_checkbox;
1217 pref_widgets;
1219 static void
1220 on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
1222 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
1224 g_free(open_cmd);
1225 open_cmd = g_strdup(gtk_entry_get_text(GTK_ENTRY(pref_widgets.open_cmd_entry)));
1226 show_hidden_files = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.show_hidden_checkbox));
1227 hide_object_files = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.hide_objects_checkbox));
1228 g_free(hidden_file_extensions);
1229 hidden_file_extensions = g_strdup(gtk_entry_get_text(GTK_ENTRY(pref_widgets.hidden_files_entry)));
1230 fb_follow_path = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.follow_path_checkbox));
1231 fb_set_project_base_path = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
1232 pref_widgets.set_project_base_path_checkbox));
1234 /* apply the changes */
1235 refresh();
1240 static void on_toggle_hidden(void)
1242 gboolean enabled = !gtk_toggle_button_get_active(
1243 GTK_TOGGLE_BUTTON(pref_widgets.show_hidden_checkbox));
1245 gtk_widget_set_sensitive(pref_widgets.hide_objects_checkbox, enabled);
1246 enabled &= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.hide_objects_checkbox));
1247 gtk_widget_set_sensitive(pref_widgets.hidden_files_entry, enabled);
1251 GtkWidget *plugin_configure(GtkDialog *dialog)
1253 GtkWidget *label, *entry, *checkbox_of, *checkbox_hf, *checkbox_fp, *checkbox_pb, *vbox;
1254 GtkWidget *box, *align;
1256 vbox = gtk_vbox_new(FALSE, 6);
1257 box = gtk_vbox_new(FALSE, 3);
1259 label = gtk_label_new(_("External open command:"));
1260 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1261 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
1263 entry = gtk_entry_new();
1264 if (open_cmd != NULL)
1265 gtk_entry_set_text(GTK_ENTRY(entry), open_cmd);
1266 gtk_widget_set_tooltip_text(entry,
1267 _("The command to execute when using \"Open with\". You can use %f and %d wildcards.\n"
1268 "%f will be replaced with the filename including full path\n"
1269 "%d will be replaced with the path name of the selected file without the filename"));
1270 gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 0);
1271 pref_widgets.open_cmd_entry = entry;
1273 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 3);
1275 checkbox_hf = gtk_check_button_new_with_label(_("Show hidden files"));
1276 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_hf), FALSE);
1277 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_hf), show_hidden_files);
1278 gtk_box_pack_start(GTK_BOX(vbox), checkbox_hf, FALSE, FALSE, 0);
1279 pref_widgets.show_hidden_checkbox = checkbox_hf;
1280 g_signal_connect(checkbox_hf, "toggled", on_toggle_hidden, NULL);
1282 box = gtk_vbox_new(FALSE, 3);
1283 checkbox_of = gtk_check_button_new_with_label(_("Hide file extensions:"));
1284 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_of), FALSE);
1285 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_of), hide_object_files);
1286 gtk_box_pack_start(GTK_BOX(box), checkbox_of, FALSE, FALSE, 0);
1287 pref_widgets.hide_objects_checkbox = checkbox_of;
1288 g_signal_connect(checkbox_of, "toggled", on_toggle_hidden, NULL);
1290 entry = gtk_entry_new();
1291 if (hidden_file_extensions != NULL)
1292 gtk_entry_set_text(GTK_ENTRY(entry), hidden_file_extensions);
1293 gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 0);
1294 pref_widgets.hidden_files_entry = entry;
1296 align = gtk_alignment_new(1, 0.5, 1, 1);
1297 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1298 gtk_container_add(GTK_CONTAINER(align), box);
1299 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, FALSE, 0);
1300 on_toggle_hidden();
1302 checkbox_fp = gtk_check_button_new_with_label(_("Follow the path of the current file"));
1303 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_fp), FALSE);
1304 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_fp), fb_follow_path);
1305 gtk_box_pack_start(GTK_BOX(vbox), checkbox_fp, FALSE, FALSE, 0);
1306 pref_widgets.follow_path_checkbox = checkbox_fp;
1308 checkbox_pb = gtk_check_button_new_with_label(_("Use the project's base directory"));
1309 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_pb), FALSE);
1310 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_pb), fb_set_project_base_path);
1311 gtk_widget_set_tooltip_text(checkbox_pb,
1312 _("Change the directory to the base directory of the currently opened project"));
1313 gtk_box_pack_start(GTK_BOX(vbox), checkbox_pb, FALSE, FALSE, 0);
1314 pref_widgets.set_project_base_path_checkbox = checkbox_pb;
1316 gtk_widget_show_all(vbox);
1318 g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
1319 return vbox;
1323 void plugin_cleanup(void)
1325 save_settings();
1327 g_free(config_file);
1328 g_free(open_cmd);
1329 g_free(hidden_file_extensions);
1330 clear_filter();
1331 gtk_widget_destroy(file_view_vbox);
1332 g_object_unref(G_OBJECT(entry_completion));