diff --git a/ChangeLog b/ChangeLog index 15aab953..203a5efb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,12 @@ * src/interface.c, geany.glade: Rename 'tabulators' 'tabs'. + * src/ui_utils.h, src/search.c, src/ui_utils.c: + Add ui_path_box_new() for creating a path text entry with an open + button, which runs a file chooser to set the text entry. + Add ui_setup_open_button_callback() for setting up a button callback + that behaves like the open button in ui_path_box_new(). + Use ui_path_box_new() in FIF dialog setup. 2007-04-29 Enrico Tröger diff --git a/src/search.c b/src/search.c index 580909cc..5762eca1 100644 --- a/src/search.c +++ b/src/search.c @@ -108,8 +108,6 @@ on_find_in_files_dialog_response(GtkDialog *dialog, gint response, gpointer user static gboolean search_find_in_files(const gchar *search_text, const gchar *dir, const gchar *opts); -static void on_open_dir_dialog_clicked(GtkButton *button, gpointer user_data); - void search_init() { @@ -543,7 +541,6 @@ void search_show_find_in_files_dialog() static GtkWidget *combo = NULL; static GtkWidget *dir_combo; GtkWidget *entry; // the child GtkEntry of combo (or dir_combo) - GtkWidget *dirbtn, *openimg; gint idx = document_get_cur_idx(); gchar *sel = NULL; gchar *cur_dir; @@ -554,7 +551,7 @@ void search_show_find_in_files_dialog() { GtkWidget *label, *label1, *checkbox1, *checkbox2, *check_wholeword, *check_recursive, *check_extra, *entry_extra; - GtkWidget *dbox, *sbox, *cbox, *rbox, *rbtn, *hbox, *vbox, *box; + GtkWidget *dbox, *sbox, *cbox, *rbox, *rbtn, *hbox, *vbox; GtkSizeGroup *size_group; GtkTooltips *tooltips = GTK_TOOLTIPS(lookup_widget(app->window, "tooltips")); @@ -578,20 +575,8 @@ void search_show_find_in_files_dialog() g_object_set_data_full(G_OBJECT(widgets.find_in_files_dialog), "dir_combo", gtk_widget_ref(dir_combo), (GDestroyNotify)gtk_widget_unref); - // prevent dir_combo being vertically stretched to the height of dirbtn - box = gtk_vbox_new(FALSE, 0); - gtk_box_pack_start(GTK_BOX(box), dir_combo, TRUE, FALSE, 0); - - dirbtn = gtk_button_new(); - openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON); - gtk_container_add(GTK_CONTAINER(dirbtn), openimg); - g_signal_connect(G_OBJECT(dirbtn), "clicked", G_CALLBACK(on_open_dir_dialog_clicked), - NULL); - - dbox = gtk_hbox_new(FALSE, 6); + dbox = ui_path_box_new(GTK_ENTRY(entry), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER); gtk_box_pack_start(GTK_BOX(dbox), label1, FALSE, FALSE, 0); - gtk_box_pack_start(GTK_BOX(dbox), box, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(dbox), dirbtn, FALSE, FALSE, 0); label = gtk_label_new(_("Search for:")); gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); @@ -735,37 +720,6 @@ void search_show_find_in_files_dialog() } -static void on_open_dir_dialog_clicked(GtkButton *button, gpointer user_data) -{ - GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Select folder"), - GTK_WINDOW(app->window), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, - GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, - GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); - GtkWidget *dir_combo = - lookup_widget(widgets.find_in_files_dialog, "dir_combo"); - GtkWidget *entry_dir = gtk_bin_get_child(GTK_BIN(dir_combo)); - gchar *dir_locale; - const gchar *entry_text; - - entry_text = gtk_entry_get_text(GTK_ENTRY(entry_dir)); - dir_locale = utils_get_locale_from_utf8(entry_text); - if (g_path_is_absolute(dir_locale) && g_file_test(dir_locale, G_FILE_TEST_IS_DIR)) - gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), dir_locale); - g_free(dir_locale); - - if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) - { - gchar *dir_utf8; - dir_locale = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog)); - dir_utf8 = utils_get_utf8_from_locale(dir_locale); - g_free(dir_locale); - gtk_entry_set_text(GTK_ENTRY(entry_dir), dir_utf8); - g_free(dir_utf8); - } - gtk_widget_destroy(dialog); -} - - static void on_find_replace_checkbutton_toggled(GtkToggleButton *togglebutton, gpointer user_data) { diff --git a/src/ui_utils.c b/src/ui_utils.c index 9b284326..f60dd919 100644 --- a/src/ui_utils.c +++ b/src/ui_utils.c @@ -958,8 +958,10 @@ GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alig const gint BUTTON_BOX_BORDER = 5; -/* common convenience function for getting a fixed border for dialogs - * that doesn't increase the button box border */ +/* Convenience function for getting a fixed border for dialogs that doesn't + * increase the button box border. + * dialog is the parent container for the vbox. + * Returns: the vbox. */ GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog) { GtkWidget *vbox = gtk_vbox_new(FALSE, 12); // need child vbox to set a separate border. @@ -1119,3 +1121,95 @@ void ui_widget_modify_font_from_string(GtkWidget *wid, const gchar *str) } +/* Creates a GtkHBox with entry packed into it and an open button which runs a + * file chooser, replacing entry text if successful. + * entry can be the child of an unparented widget, such as GtkComboBoxEntry. + * action is the GtkFileChooser mode to use. */ +GtkWidget *ui_path_box_new(GtkEntry *entry, GtkFileChooserAction action) +{ + GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry; + + hbox = gtk_hbox_new(FALSE, 6); + path_entry = GTK_WIDGET(entry); + + // prevent path_entry being vertically stretched to the height of dirbtn + vbox = gtk_vbox_new(FALSE, 0); + if (gtk_widget_get_parent(path_entry)) // entry->parent may be a GtkComboBoxEntry + { + GtkWidget *parent = gtk_widget_get_parent(path_entry); + + gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0); + } + else + gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0); + + dirbtn = gtk_button_new(); + openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON); + gtk_container_add(GTK_CONTAINER(dirbtn), openimg); + ui_setup_open_button_callback(dirbtn, entry, action); + + gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0); + gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0); + return hbox; +} + + +static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data); + + +/* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful. + * action is the file chooser mode to use. */ +void ui_setup_open_button_callback(GtkWidget *open_btn, GtkEntry *entry, + GtkFileChooserAction action) +{ + GtkWidget *path_entry = GTK_WIDGET(entry); + + g_object_set_data_full(G_OBJECT(open_btn), "entry", + gtk_widget_ref(path_entry), (GDestroyNotify)gtk_widget_unref); + g_object_set_data(G_OBJECT(open_btn), "action", (gpointer) action); + g_signal_connect(G_OBJECT(open_btn), "clicked", + G_CALLBACK(ui_path_box_open_clicked), open_btn); +} + + +static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data) +{ + GtkWidget *path_box = GTK_WIDGET(user_data); + GtkFileChooserAction action = + (GtkFileChooserAction) g_object_get_data(G_OBJECT(path_box), "action"); + GtkEntry *entry = + (GtkEntry *) g_object_get_data(G_OBJECT(path_box), "entry"); + const gchar *title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ? + _("Select Folder") : _("Select File"); + GtkWidget *dialog = gtk_file_chooser_dialog_new(title, + GTK_WINDOW(app->window), action, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); + gchar *locale_path; + const gchar *utf8_path; + + // TODO: extend for other actions + g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER); + + utf8_path = gtk_entry_get_text(GTK_ENTRY(entry)); + locale_path = utils_get_locale_from_utf8(utf8_path); + if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) + { + if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR)) + gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path); + } + g_free(locale_path); + + if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) + { + gchar *dir_utf8, *dir_locale; + dir_locale = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog)); + dir_utf8 = utils_get_utf8_from_locale(dir_locale); + gtk_entry_set_text(GTK_ENTRY(entry), dir_utf8); + g_free(dir_utf8); + g_free(dir_locale); + } + gtk_widget_destroy(dialog); +} + + diff --git a/src/ui_utils.h b/src/ui_utils.h index 512c7fa8..3a5db9fd 100644 --- a/src/ui_utils.h +++ b/src/ui_utils.h @@ -99,6 +99,11 @@ void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy); void ui_combo_box_add_to_history(GtkComboBox *combo, const gchar *text); +GtkWidget *ui_path_box_new(GtkEntry *entry, GtkFileChooserAction action); + +void ui_setup_open_button_callback(GtkWidget *open_btn, GtkEntry *entry, + GtkFileChooserAction action); + void ui_update_tab_status(gint idx);