Improve making-a-release a little
[geany-mirror.git] / plugins / filebrowser.c
blob25a8c33624b2b9075d0eb83116aa8e3dee82d65a
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
63 enum
65 FILEVIEW_COLUMN_ICON = 0,
66 FILEVIEW_COLUMN_NAME,
67 FILEVIEW_COLUMN_FILENAME, /* the full filename, including path for display as tooltip */
68 FILEVIEW_COLUMN_IS_DIR,
69 FILEVIEW_N_COLUMNS
72 static gboolean fb_set_project_base_path = FALSE;
73 static gboolean fb_follow_path = FALSE;
74 static gboolean show_hidden_files = FALSE;
75 static gboolean hide_object_files = TRUE;
77 static GtkWidget *file_view_vbox;
78 static GtkWidget *file_view;
79 static GtkListStore *file_store;
80 static GtkTreeIter *last_dir_iter = NULL;
81 static GtkEntryCompletion *entry_completion = NULL;
83 static GtkWidget *filter_combo;
84 static GtkWidget *filter_entry;
85 static GtkWidget *path_combo;
86 static GtkWidget *path_entry;
87 static gchar *current_dir = NULL; /* in locale-encoding */
88 static gchar *open_cmd; /* in locale-encoding */
89 static gchar *config_file;
90 static gchar **filter = NULL;
91 static gchar *hidden_file_extensions = NULL;
93 static gint page_number = 0;
95 static struct
97 GtkWidget *open;
98 GtkWidget *open_external;
99 GtkWidget *find_in_files;
100 GtkWidget *show_hidden_files;
101 } popup_items;
104 static void project_change_cb(GObject *obj, GKeyFile *config, gpointer data);
106 /* note: other callbacks connected in plugin_init */
107 PluginCallback plugin_callbacks[] =
109 { "project-open", (GCallback) &project_change_cb, TRUE, NULL },
110 { "project-save", (GCallback) &project_change_cb, TRUE, NULL },
111 { NULL, NULL, FALSE, NULL }
115 #ifdef G_OS_WIN32
116 static gboolean win32_check_hidden(const gchar *filename)
118 DWORD attrs;
119 static wchar_t w_filename[MAX_PATH];
120 MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename));
121 attrs = GetFileAttributesW(w_filename);
122 if (attrs != INVALID_FILE_ATTRIBUTES && attrs & FILE_ATTRIBUTE_HIDDEN)
123 return TRUE;
124 return FALSE;
126 #endif
129 /* Returns: whether name should be hidden. */
130 static gboolean check_hidden(const gchar *filename, const gchar *base_name)
132 gsize len;
134 #ifdef G_OS_WIN32
135 if (win32_check_hidden(filename))
136 return TRUE;
137 #else
138 if (base_name[0] == '.')
139 return TRUE;
140 #endif
142 len = strlen(base_name);
143 return base_name[len - 1] == '~';
147 static gboolean check_object(const gchar *base_name)
149 gboolean ret = FALSE;
150 gchar **ptr;
151 gchar **exts = g_strsplit(hidden_file_extensions, " ", -1);
153 foreach_strv(ptr, exts)
155 if (g_str_has_suffix(base_name, *ptr))
157 ret = TRUE;
158 break;
161 g_strfreev(exts);
162 return ret;
166 /* Returns: whether filename should be removed. */
167 static gboolean check_filtered(const gchar *base_name)
169 gchar **filter_item;
171 if (filter == NULL)
172 return FALSE;
174 foreach_strv(filter_item, filter)
176 if (utils_str_equal(*filter_item, "*") || g_pattern_match_simple(*filter_item, base_name))
178 return FALSE;
181 return TRUE;
185 static GIcon *get_icon(const gchar *fname)
187 GIcon *icon = NULL;
188 gchar *ctype;
190 ctype = g_content_type_guess(fname, NULL, 0, NULL);
192 if (ctype)
194 icon = g_content_type_get_icon(ctype);
195 if (icon)
197 GtkIconInfo *icon_info;
199 icon_info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), icon, 16, 0);
200 if (!icon_info)
202 g_object_unref(icon);
203 icon = NULL;
205 else
206 gtk_icon_info_free(icon_info);
208 g_free(ctype);
211 if (!icon)
212 icon = g_themed_icon_new("text-x-generic");
214 return icon;
218 /* name is in locale encoding */
219 static void add_item(const gchar *name)
221 GtkTreeIter iter;
222 gchar *fname, *utf8_name, *utf8_fullname;
223 const gchar *sep;
224 gboolean dir;
225 GIcon *icon;
227 if (G_UNLIKELY(EMPTY(name)))
228 return;
230 /* root directory doesn't need separator */
231 sep = (utils_str_equal(current_dir, "/")) ? "" : G_DIR_SEPARATOR_S;
232 fname = g_strconcat(current_dir, sep, name, NULL);
233 dir = g_file_test(fname, G_FILE_TEST_IS_DIR);
234 utf8_fullname = utils_get_utf8_from_locale(fname);
235 utf8_name = utils_get_utf8_from_locale(name);
236 g_free(fname);
238 if (! show_hidden_files && check_hidden(utf8_fullname, utf8_name))
239 goto done;
241 if (dir)
243 if (last_dir_iter == NULL)
244 gtk_list_store_prepend(file_store, &iter);
245 else
247 gtk_list_store_insert_after(file_store, &iter, last_dir_iter);
248 gtk_tree_iter_free(last_dir_iter);
250 last_dir_iter = gtk_tree_iter_copy(&iter);
252 else
254 if (! show_hidden_files && hide_object_files && check_object(utf8_name))
255 goto done;
256 if (check_filtered(utf8_name))
257 goto done;
259 gtk_list_store_append(file_store, &iter);
262 icon = dir ? g_themed_icon_new("folder") : get_icon(utf8_name);
263 gtk_list_store_set(file_store, &iter,
264 FILEVIEW_COLUMN_ICON, icon,
265 FILEVIEW_COLUMN_NAME, utf8_name,
266 FILEVIEW_COLUMN_FILENAME, utf8_fullname,
267 FILEVIEW_COLUMN_IS_DIR, dir,
268 -1);
269 g_object_unref(icon);
270 done:
271 g_free(utf8_name);
272 g_free(utf8_fullname);
276 /* adds ".." to the start of the file list */
277 static void add_top_level_entry(void)
279 GtkTreeIter iter;
280 gchar *utf8_dir;
281 GIcon *icon;
283 if (EMPTY(g_path_skip_root(current_dir)))
284 return; /* ignore 'C:\' or '/' */
286 utf8_dir = g_path_get_dirname(current_dir);
287 SETPTR(utf8_dir, utils_get_utf8_from_locale(utf8_dir));
289 gtk_list_store_prepend(file_store, &iter);
290 last_dir_iter = gtk_tree_iter_copy(&iter);
292 icon = g_themed_icon_new("folder");
293 gtk_list_store_set(file_store, &iter,
294 FILEVIEW_COLUMN_ICON, icon,
295 FILEVIEW_COLUMN_NAME, "..",
296 FILEVIEW_COLUMN_FILENAME, utf8_dir,
297 FILEVIEW_COLUMN_IS_DIR, TRUE,
298 -1);
299 g_object_unref(icon);
300 g_free(utf8_dir);
304 static void clear(void)
306 gtk_list_store_clear(file_store);
308 /* reset the directory item pointer */
309 if (last_dir_iter != NULL)
310 gtk_tree_iter_free(last_dir_iter);
311 last_dir_iter = NULL;
315 /* recreate the tree model from current_dir. */
316 static void refresh(void)
318 gchar *utf8_dir;
319 GSList *list, *node;
321 /* don't clear when the new path doesn't exist */
322 if (! g_file_test(current_dir, G_FILE_TEST_EXISTS))
323 return;
325 clear();
327 utf8_dir = utils_get_utf8_from_locale(current_dir);
328 gtk_entry_set_text(GTK_ENTRY(path_entry), utf8_dir);
329 gtk_widget_set_tooltip_text(path_entry, utf8_dir);
330 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(path_combo), utf8_dir, 0);
331 g_free(utf8_dir);
333 add_top_level_entry(); /* ".." item */
335 list = utils_get_file_list(current_dir, NULL, NULL);
336 if (list != NULL)
338 /* free filenames as we go through the list */
339 foreach_slist(node, list)
341 gchar *fname = node->data;
343 add_item(fname);
344 g_free(fname);
346 g_slist_free(list);
348 gtk_entry_completion_set_model(entry_completion, GTK_TREE_MODEL(file_store));
352 static void on_go_home(void)
354 SETPTR(current_dir, g_strdup(g_get_home_dir()));
355 refresh();
359 /* TODO: use utils_get_default_dir_utf8() */
360 static gchar *get_default_dir(void)
362 const gchar *dir = NULL;
363 GeanyProject *project = geany->app->project;
365 if (project)
366 dir = project->base_path;
367 else
368 dir = geany->prefs->default_open_path;
370 if (!EMPTY(dir))
371 return utils_get_locale_from_utf8(dir);
373 return g_get_current_dir();
377 static void on_current_path(void)
379 gchar *fname;
380 gchar *dir;
381 GeanyDocument *doc = document_get_current();
383 if (doc == NULL || doc->file_name == NULL || ! g_path_is_absolute(doc->file_name))
385 SETPTR(current_dir, get_default_dir());
386 refresh();
387 return;
389 fname = doc->file_name;
390 fname = utils_get_locale_from_utf8(fname);
391 dir = g_path_get_dirname(fname);
392 g_free(fname);
394 SETPTR(current_dir, dir);
395 refresh();
399 static void on_go_up(void)
401 gsize len = strlen(current_dir);
402 if (current_dir[len-1] == G_DIR_SEPARATOR)
403 current_dir[len-1] = '\0';
404 /* remove the highest directory part (which becomes the basename of current_dir) */
405 SETPTR(current_dir, g_path_get_dirname(current_dir));
406 refresh();
410 static gboolean check_single_selection(GtkTreeSelection *treesel)
412 if (gtk_tree_selection_count_selected_rows(treesel) == 1)
413 return TRUE;
415 ui_set_statusbar(FALSE, _("Too many items selected!"));
416 return FALSE;
420 /* Returns: TRUE if at least one of selected_items is a folder. */
421 static gboolean is_folder_selected(GList *selected_items)
423 GList *item;
424 GtkTreeModel *model = GTK_TREE_MODEL(file_store);
425 gboolean dir_found = FALSE;
427 for (item = selected_items; item != NULL; item = g_list_next(item))
429 GtkTreeIter iter;
430 GtkTreePath *treepath;
432 treepath = (GtkTreePath*) item->data;
433 gtk_tree_model_get_iter(model, &iter, treepath);
434 gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_IS_DIR, &dir_found, -1);
436 if (dir_found)
437 break;
439 return dir_found;
443 /* Returns: the full filename in locale encoding. */
444 static gchar *get_tree_path_filename(GtkTreePath *treepath)
446 GtkTreeModel *model = GTK_TREE_MODEL(file_store);
447 GtkTreeIter iter;
448 gchar *name, *fname;
450 gtk_tree_model_get_iter(model, &iter, treepath);
451 gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_FILENAME, &name, -1);
453 fname = utils_get_locale_from_utf8(name);
454 g_free(name);
456 return fname;
460 static void open_external(const gchar *fname, gboolean dir_found)
462 gchar *cmd;
463 gchar *locale_cmd;
464 gchar *dir;
465 GString *cmd_str = g_string_new(open_cmd);
466 GError *error = NULL;
468 if (! dir_found)
469 dir = g_path_get_dirname(fname);
470 else
471 dir = g_strdup(fname);
473 utils_string_replace_all(cmd_str, "%f", fname);
474 utils_string_replace_all(cmd_str, "%d", dir);
476 cmd = g_string_free(cmd_str, FALSE);
477 locale_cmd = utils_get_locale_from_utf8(cmd);
478 if (! spawn_async(NULL, locale_cmd, NULL, NULL, NULL, &error))
480 gchar *c = strchr(cmd, ' ');
482 if (c != NULL)
483 *c = '\0';
484 ui_set_statusbar(TRUE,
485 _("Could not execute configured external command '%s' (%s)."),
486 cmd, error->message);
487 g_error_free(error);
489 g_free(locale_cmd);
490 g_free(cmd);
491 g_free(dir);
495 static void on_external_open(GtkMenuItem *menuitem, gpointer user_data)
497 GtkTreeSelection *treesel;
498 GtkTreeModel *model;
499 GList *list;
500 gboolean dir_found;
502 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
504 list = gtk_tree_selection_get_selected_rows(treesel, &model);
505 dir_found = is_folder_selected(list);
507 if (! dir_found || check_single_selection(treesel))
509 GList *item;
511 for (item = list; item != NULL; item = g_list_next(item))
513 GtkTreePath *treepath = item->data;
514 gchar *fname = get_tree_path_filename(treepath);
516 open_external(fname, dir_found);
517 g_free(fname);
521 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
522 g_list_free(list);
526 /* We use document_open_files() as it's more efficient. */
527 static void open_selected_files(GList *list, gboolean do_not_focus)
529 GSList *files = NULL;
530 GList *item;
531 GeanyDocument *doc;
533 for (item = list; item != NULL; item = g_list_next(item))
535 GtkTreePath *treepath = item->data;
536 gchar *fname = get_tree_path_filename(treepath);
538 files = g_slist_prepend(files, fname);
540 files = g_slist_reverse(files);
541 document_open_files(files, FALSE, NULL, NULL);
542 doc = document_get_current();
543 if (doc != NULL && ! do_not_focus)
544 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
546 g_slist_foreach(files, (GFunc) g_free, NULL); /* free filenames */
547 g_slist_free(files);
551 static void open_folder(GtkTreePath *treepath)
553 gchar *fname = get_tree_path_filename(treepath);
555 SETPTR(current_dir, fname);
556 refresh();
560 static void on_open_clicked(GtkMenuItem *menuitem, gpointer user_data)
562 GtkTreeSelection *treesel;
563 GtkTreeModel *model;
564 GList *list;
565 gboolean dir_found;
567 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
569 list = gtk_tree_selection_get_selected_rows(treesel, &model);
570 dir_found = is_folder_selected(list);
572 if (dir_found)
574 if (check_single_selection(treesel))
576 GtkTreePath *treepath = list->data; /* first selected item */
578 open_folder(treepath);
581 else
582 open_selected_files(list, GPOINTER_TO_INT(user_data));
584 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
585 g_list_free(list);
589 static void on_find_in_files(GtkMenuItem *menuitem, gpointer user_data)
591 GtkTreeSelection *treesel;
592 GtkTreeModel *model;
593 GList *list;
594 gchar *dir;
595 gboolean is_dir = FALSE;
597 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
598 /* allow 0 or 1 selections */
599 if (gtk_tree_selection_count_selected_rows(treesel) > 0 &&
600 ! check_single_selection(treesel))
601 return;
603 list = gtk_tree_selection_get_selected_rows(treesel, &model);
604 is_dir = is_folder_selected(list);
606 if (is_dir)
608 GtkTreePath *treepath = list->data; /* first selected item */
610 dir = get_tree_path_filename(treepath);
612 else
613 dir = g_strdup(current_dir);
615 g_list_foreach(list, (GFunc) gtk_tree_path_free, NULL);
616 g_list_free(list);
618 SETPTR(dir, utils_get_utf8_from_locale(dir));
619 search_show_find_in_files_dialog(dir);
620 g_free(dir);
624 static void on_hidden_files_clicked(GtkCheckMenuItem *item)
626 show_hidden_files = gtk_check_menu_item_get_active(item);
627 refresh();
631 static void on_hide_sidebar(void)
633 keybindings_send_command(GEANY_KEY_GROUP_VIEW, GEANY_KEYS_VIEW_SIDEBAR);
637 static void on_show_preferences(void)
639 plugin_show_configure(geany_plugin);
643 static GtkWidget *create_popup_menu(void)
645 GtkWidget *item, *menu;
647 menu = gtk_menu_new();
649 item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open in _Geany"));
650 gtk_widget_show(item);
651 gtk_container_add(GTK_CONTAINER(menu), item);
652 g_signal_connect(item, "activate", G_CALLBACK(on_open_clicked), NULL);
653 popup_items.open = item;
655 item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open _Externally"));
656 gtk_widget_show(item);
657 gtk_container_add(GTK_CONTAINER(menu), item);
658 g_signal_connect(item, "activate", G_CALLBACK(on_external_open), NULL);
659 popup_items.open_external = item;
661 item = gtk_separator_menu_item_new();
662 gtk_widget_show(item);
663 gtk_container_add(GTK_CONTAINER(menu), item);
665 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_REFRESH, NULL);
666 gtk_widget_show(item);
667 gtk_container_add(GTK_CONTAINER(menu), item);
668 g_signal_connect(item, "activate", G_CALLBACK(refresh), NULL);
670 item = ui_image_menu_item_new(GTK_STOCK_FIND, _("_Find in Files..."));
671 gtk_widget_show(item);
672 gtk_container_add(GTK_CONTAINER(menu), item);
673 g_signal_connect(item, "activate", G_CALLBACK(on_find_in_files), NULL);
674 popup_items.find_in_files = item;
676 item = gtk_separator_menu_item_new();
677 gtk_widget_show(item);
678 gtk_container_add(GTK_CONTAINER(menu), item);
680 item = gtk_check_menu_item_new_with_mnemonic(_("Show _Hidden Files"));
681 gtk_widget_show(item);
682 gtk_container_add(GTK_CONTAINER(menu), item);
683 g_signal_connect(item, "activate", G_CALLBACK(on_hidden_files_clicked), NULL);
684 popup_items.show_hidden_files = item;
686 item = gtk_separator_menu_item_new();
687 gtk_widget_show(item);
688 gtk_container_add(GTK_CONTAINER(menu), item);
690 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
691 gtk_widget_show(item);
692 gtk_container_add(GTK_CONTAINER(menu), item);
693 g_signal_connect(item, "activate", G_CALLBACK(on_show_preferences), NULL);
695 item = gtk_separator_menu_item_new();
696 gtk_widget_show(item);
697 gtk_container_add(GTK_CONTAINER(menu), item);
699 item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("H_ide Sidebar"));
700 gtk_widget_show(item);
701 gtk_container_add(GTK_CONTAINER(menu), item);
702 g_signal_connect(item, "activate", G_CALLBACK(on_hide_sidebar), NULL);
704 return menu;
708 static void on_tree_selection_changed(GtkTreeSelection *selection, gpointer data)
710 gboolean have_sel = (gtk_tree_selection_count_selected_rows(selection) > 0);
711 gboolean multi_sel = (gtk_tree_selection_count_selected_rows(selection) > 1);
713 if (popup_items.open != NULL)
714 gtk_widget_set_sensitive(popup_items.open, have_sel);
715 if (popup_items.open_external != NULL)
716 gtk_widget_set_sensitive(popup_items.open_external, have_sel);
717 if (popup_items.find_in_files != NULL)
718 gtk_widget_set_sensitive(popup_items.find_in_files, have_sel && ! multi_sel);
722 static gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
724 if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
726 on_open_clicked(NULL, NULL);
727 return TRUE;
729 else if (event->button == 3)
731 static GtkWidget *popup_menu = NULL;
733 if (popup_menu == NULL)
734 popup_menu = create_popup_menu();
736 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(popup_items.show_hidden_files),
737 show_hidden_files);
738 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, NULL, NULL, event->button, event->time);
739 /* don't return TRUE here, unless the selection won't be changed */
741 return FALSE;
745 static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
747 if (ui_is_keyval_enter_or_return(event->keyval))
749 on_open_clicked(NULL, NULL);
750 return TRUE;
753 if (event->keyval == GDK_space)
755 on_open_clicked(NULL, GINT_TO_POINTER(TRUE));
756 return TRUE;
759 if (( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && (event->state & GDK_MOD1_MASK)) || /* FIXME: Alt-Up doesn't seem to work! */
760 (event->keyval == GDK_BackSpace) )
762 on_go_up();
763 return TRUE;
766 if ((event->keyval == GDK_F10 && event->state & GDK_SHIFT_MASK) || event->keyval == GDK_Menu)
768 GdkEventButton button_event;
770 button_event.time = event->time;
771 button_event.button = 3;
773 on_button_press(widget, &button_event, data);
774 return TRUE;
777 return FALSE;
781 static void clear_filter(void)
783 if (filter != NULL)
785 g_strfreev(filter);
786 filter = NULL;
791 static void on_clear_filter(GtkEntry *entry, gpointer user_data)
793 clear_filter();
795 gtk_entry_set_text(GTK_ENTRY(filter_entry), "");
797 refresh();
801 static void on_path_entry_activate(GtkEntry *entry, gpointer user_data)
803 gchar *new_dir = (gchar*) gtk_entry_get_text(entry);
805 if (!EMPTY(new_dir))
807 if (g_str_has_suffix(new_dir, ".."))
809 on_go_up();
810 return;
812 else if (new_dir[0] == '~')
814 GString *str = g_string_new(new_dir);
815 utils_string_replace_first(str, "~", g_get_home_dir());
816 new_dir = g_string_free(str, FALSE);
818 else
819 new_dir = utils_get_locale_from_utf8(new_dir);
821 else
822 new_dir = g_strdup(g_get_home_dir());
824 SETPTR(current_dir, new_dir);
826 on_clear_filter(NULL, NULL);
830 static void ui_combo_box_changed(GtkComboBox *combo, gpointer user_data)
832 /* we get this callback on typing as well as choosing an item */
833 if (gtk_combo_box_get_active(combo) >= 0)
834 gtk_widget_activate(gtk_bin_get_child(GTK_BIN(combo)));
838 static void on_filter_activate(GtkEntry *entry, gpointer user_data)
840 /* We use spaces for consistency with Find in Files file patterns
841 * ';' also supported like original patch. */
842 filter = g_strsplit_set(gtk_entry_get_text(entry), "; ", -1);
843 if (filter == NULL || g_strv_length(filter) == 0)
845 clear_filter();
847 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(filter_combo), NULL, 0);
848 refresh();
852 static void on_filter_clear(GtkEntry *entry, gint icon_pos,
853 GdkEvent *event, gpointer data)
855 clear_filter();
856 refresh();
860 static void prepare_file_view(void)
862 GtkCellRenderer *text_renderer, *icon_renderer;
863 GtkTreeViewColumn *column;
864 GtkTreeSelection *selection;
866 file_store = gtk_list_store_new(FILEVIEW_N_COLUMNS, G_TYPE_ICON, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
868 gtk_tree_view_set_model(GTK_TREE_VIEW(file_view), GTK_TREE_MODEL(file_store));
869 g_object_unref(file_store);
871 icon_renderer = gtk_cell_renderer_pixbuf_new();
872 text_renderer = gtk_cell_renderer_text_new();
873 column = gtk_tree_view_column_new();
874 gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
875 gtk_tree_view_column_set_attributes(column, icon_renderer, "gicon", FILEVIEW_COLUMN_ICON, NULL);
876 gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
877 gtk_tree_view_column_set_attributes(column, text_renderer, "text", FILEVIEW_COLUMN_NAME, NULL);
878 gtk_tree_view_append_column(GTK_TREE_VIEW(file_view), column);
879 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(file_view), FALSE);
881 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(file_view), TRUE);
882 gtk_tree_view_set_search_column(GTK_TREE_VIEW(file_view), FILEVIEW_COLUMN_NAME);
884 ui_widget_modify_font_from_string(file_view, geany->interface_prefs->tagbar_font);
886 /* tooltips */
887 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(file_view), FILEVIEW_COLUMN_FILENAME);
889 /* selection handling */
890 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(file_view));
891 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
893 /* Show the current path when the FB is first needed */
894 g_signal_connect(file_view, "realize", G_CALLBACK(on_current_path), NULL);
895 g_signal_connect(selection, "changed", G_CALLBACK(on_tree_selection_changed), NULL);
896 g_signal_connect(file_view, "button-press-event", G_CALLBACK(on_button_press), NULL);
897 g_signal_connect(file_view, "key-press-event", G_CALLBACK(on_key_press), NULL);
901 static GtkWidget *make_toolbar(void)
903 GtkWidget *wid, *toolbar;
905 toolbar = gtk_toolbar_new();
906 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
907 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
909 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_GO_UP));
910 gtk_widget_set_tooltip_text(wid, _("Up"));
911 g_signal_connect(wid, "clicked", G_CALLBACK(on_go_up), NULL);
912 gtk_container_add(GTK_CONTAINER(toolbar), wid);
914 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH));
915 gtk_widget_set_tooltip_text(wid, _("Refresh"));
916 g_signal_connect(wid, "clicked", G_CALLBACK(refresh), NULL);
917 gtk_container_add(GTK_CONTAINER(toolbar), wid);
919 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_HOME));
920 gtk_widget_set_tooltip_text(wid, _("Home"));
921 g_signal_connect(wid, "clicked", G_CALLBACK(on_go_home), NULL);
922 gtk_container_add(GTK_CONTAINER(toolbar), wid);
924 wid = GTK_WIDGET(gtk_tool_button_new_from_stock(GTK_STOCK_JUMP_TO));
925 gtk_widget_set_tooltip_text(wid, _("Set path from document"));
926 g_signal_connect(wid, "clicked", G_CALLBACK(on_current_path), NULL);
927 gtk_container_add(GTK_CONTAINER(toolbar), wid);
929 return toolbar;
933 static GtkWidget *make_filterbar(void)
935 GtkWidget *label, *filterbar;
937 filterbar = gtk_hbox_new(FALSE, 1);
939 label = gtk_label_new(_("Filter:"));
941 filter_combo = gtk_combo_box_text_new_with_entry();
942 filter_entry = gtk_bin_get_child(GTK_BIN(filter_combo));
944 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry));
945 g_signal_connect(filter_entry, "icon-release", G_CALLBACK(on_filter_clear), NULL);
947 gtk_widget_set_tooltip_text(filter_entry,
948 _("Filter your files with the usual wildcards. Separate multiple patterns with a space."));
949 g_signal_connect(filter_entry, "activate", G_CALLBACK(on_filter_activate), NULL);
950 g_signal_connect(filter_combo, "changed", G_CALLBACK(ui_combo_box_changed), NULL);
952 gtk_box_pack_start(GTK_BOX(filterbar), label, FALSE, FALSE, 0);
953 gtk_box_pack_start(GTK_BOX(filterbar), filter_combo, TRUE, TRUE, 0);
955 return filterbar;
959 static gboolean completion_match_func(GtkEntryCompletion *completion, const gchar *key,
960 GtkTreeIter *iter, gpointer user_data)
962 gchar *str;
963 gboolean is_dir;
964 gboolean result = FALSE;
966 gtk_tree_model_get(GTK_TREE_MODEL(file_store), iter,
967 FILEVIEW_COLUMN_IS_DIR, &is_dir, FILEVIEW_COLUMN_NAME, &str, -1);
969 if (str != NULL && is_dir && !g_str_has_suffix(key, G_DIR_SEPARATOR_S))
971 /* key is something like "/tmp/te" and str is a filename like "test",
972 * so strip the path from key to make them comparable */
973 gchar *base_name = g_path_get_basename(key);
974 gchar *str_lowered = g_utf8_strdown(str, -1);
975 result = g_str_has_prefix(str_lowered, base_name);
976 g_free(base_name);
977 g_free(str_lowered);
979 g_free(str);
981 return result;
985 static gboolean completion_match_selected(GtkEntryCompletion *widget, GtkTreeModel *model,
986 GtkTreeIter *iter, gpointer user_data)
988 gchar *str;
989 gtk_tree_model_get(model, iter, FILEVIEW_COLUMN_NAME, &str, -1);
990 if (str != NULL)
992 gchar *text = g_strconcat(current_dir, G_DIR_SEPARATOR_S, str, NULL);
993 gtk_entry_set_text(GTK_ENTRY(path_entry), text);
994 gtk_editable_set_position(GTK_EDITABLE(path_entry), -1);
995 /* force change of directory when completion is done */
996 on_path_entry_activate(GTK_ENTRY(path_entry), NULL);
997 g_free(text);
999 g_free(str);
1001 return TRUE;
1005 static void completion_create(void)
1007 entry_completion = gtk_entry_completion_new();
1009 gtk_entry_completion_set_inline_completion(entry_completion, FALSE);
1010 gtk_entry_completion_set_popup_completion(entry_completion, TRUE);
1011 gtk_entry_completion_set_text_column(entry_completion, FILEVIEW_COLUMN_NAME);
1012 gtk_entry_completion_set_match_func(entry_completion, completion_match_func, NULL, NULL);
1014 g_signal_connect(entry_completion, "match-selected",
1015 G_CALLBACK(completion_match_selected), NULL);
1017 gtk_entry_set_completion(GTK_ENTRY(path_entry), entry_completion);
1021 static void load_settings(void)
1023 GKeyFile *config = g_key_file_new();
1025 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
1026 "filebrowser", G_DIR_SEPARATOR_S, "filebrowser.conf", NULL);
1027 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
1029 open_cmd = utils_get_setting_string(config, "filebrowser", "open_command", OPEN_CMD);
1030 /* g_key_file_get_boolean defaults to FALSE */
1031 show_hidden_files = g_key_file_get_boolean(config, "filebrowser", "show_hidden_files", NULL);
1032 hide_object_files = utils_get_setting_boolean(config, "filebrowser", "hide_object_files", TRUE);
1033 hidden_file_extensions = utils_get_setting_string(config, "filebrowser", "hidden_file_extensions",
1034 ".o .obj .so .dll .a .lib .pyc");
1035 fb_follow_path = g_key_file_get_boolean(config, "filebrowser", "fb_follow_path", NULL);
1036 fb_set_project_base_path = g_key_file_get_boolean(config, "filebrowser", "fb_set_project_base_path", NULL);
1038 g_key_file_free(config);
1042 static void project_change_cb(G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GKeyFile *config,
1043 G_GNUC_UNUSED gpointer data)
1045 gchar *new_dir;
1046 GeanyProject *project = geany->app->project;
1048 if (! fb_set_project_base_path || project == NULL || EMPTY(project->base_path))
1049 return;
1051 /* TODO this is a copy of project_get_base_path(), add it to the plugin API */
1052 if (g_path_is_absolute(project->base_path))
1053 new_dir = g_strdup(project->base_path);
1054 else
1055 { /* build base_path out of project file name's dir and base_path */
1056 gchar *dir = g_path_get_dirname(project->file_name);
1058 new_dir = g_strconcat(dir, G_DIR_SEPARATOR_S, project->base_path, NULL);
1059 g_free(dir);
1061 /* get it into locale encoding */
1062 SETPTR(new_dir, utils_get_locale_from_utf8(new_dir));
1064 if (! utils_str_equal(current_dir, new_dir))
1066 SETPTR(current_dir, new_dir);
1067 refresh();
1069 else
1070 g_free(new_dir);
1074 static gpointer last_activate_path = NULL;
1076 static void document_activate_cb(G_GNUC_UNUSED GObject *obj, GeanyDocument *doc,
1077 G_GNUC_UNUSED gpointer data)
1079 gchar *new_dir;
1081 last_activate_path = doc->real_path;
1083 if (! fb_follow_path || doc->file_name == NULL || ! g_path_is_absolute(doc->file_name))
1084 return;
1086 new_dir = g_path_get_dirname(doc->file_name);
1087 SETPTR(new_dir, utils_get_locale_from_utf8(new_dir));
1089 if (! utils_str_equal(current_dir, new_dir))
1091 SETPTR(current_dir, new_dir);
1092 refresh();
1094 else
1095 g_free(new_dir);
1099 static void document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
1101 if (!last_activate_path)
1102 document_activate_cb(obj, doc, user_data);
1106 static void kb_activate(guint key_id)
1108 gtk_notebook_set_current_page(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook), page_number);
1109 switch (key_id)
1111 case KB_FOCUS_FILE_LIST:
1112 gtk_widget_grab_focus(file_view);
1113 break;
1114 case KB_FOCUS_PATH_ENTRY:
1115 gtk_widget_grab_focus(path_entry);
1116 break;
1121 void plugin_init(GeanyData *data)
1123 GeanyKeyGroup *key_group;
1124 GtkWidget *scrollwin, *toolbar, *filterbar;
1126 filter = NULL;
1128 file_view_vbox = gtk_vbox_new(FALSE, 0);
1129 toolbar = make_toolbar();
1130 gtk_box_pack_start(GTK_BOX(file_view_vbox), toolbar, FALSE, FALSE, 0);
1132 filterbar = make_filterbar();
1133 gtk_box_pack_start(GTK_BOX(file_view_vbox), filterbar, FALSE, FALSE, 0);
1135 path_combo = gtk_combo_box_text_new_with_entry();
1136 gtk_box_pack_start(GTK_BOX(file_view_vbox), path_combo, FALSE, FALSE, 2);
1137 g_signal_connect(path_combo, "changed", G_CALLBACK(ui_combo_box_changed), NULL);
1138 path_entry = gtk_bin_get_child(GTK_BIN(path_combo));
1139 g_signal_connect(path_entry, "activate", G_CALLBACK(on_path_entry_activate), NULL);
1141 file_view = gtk_tree_view_new();
1142 prepare_file_view();
1143 completion_create();
1145 popup_items.open = popup_items.open_external = popup_items.find_in_files = NULL;
1147 scrollwin = gtk_scrolled_window_new(NULL, NULL);
1148 gtk_scrolled_window_set_policy(
1149 GTK_SCROLLED_WINDOW(scrollwin),
1150 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1151 gtk_container_add(GTK_CONTAINER(scrollwin), file_view);
1152 gtk_box_pack_start(GTK_BOX(file_view_vbox), scrollwin, TRUE, TRUE, 0);
1154 /* load settings before file_view "realize" callback */
1155 load_settings();
1157 gtk_widget_show_all(file_view_vbox);
1158 page_number = gtk_notebook_append_page(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
1159 file_view_vbox, gtk_label_new(_("Files")));
1161 /* setup keybindings */
1162 key_group = plugin_set_key_group(geany_plugin, "file_browser", KB_COUNT, NULL);
1163 keybindings_set_item(key_group, KB_FOCUS_FILE_LIST, kb_activate,
1164 0, 0, "focus_file_list", _("Focus File List"), NULL);
1165 keybindings_set_item(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));