FreeBasic: Update keywords
[geany-mirror.git] / src / dialogs.c
blob86e0f9a5cf176080fbe6a02a833b224746a2b8c7
1 /*
2 * dialogs.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * File related dialogs, miscellaneous dialogs, font dialog.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "dialogs.h"
32 #include "app.h"
33 #include "build.h"
34 #include "document.h"
35 #include "encodings.h"
36 #include "filetypes.h"
37 #include "main.h"
38 #include "support.h"
39 #include "utils.h"
40 #include "ui_utils.h"
41 #include "win32.h"
43 #include "gtkcompat.h"
45 #include <gdk/gdkkeysyms.h>
46 #include <string.h>
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
50 #endif
51 #include <time.h>
53 #ifdef HAVE_SYS_TYPES_H
54 # include <sys/types.h>
55 #endif
57 /* gstdio.h also includes sys/stat.h */
58 #include <glib/gstdio.h>
61 enum
63 GEANY_RESPONSE_RENAME,
64 GEANY_RESPONSE_VIEW
68 static struct FileSelState
70 struct
72 guint filter_idx;
73 gint encoding_idx;
74 gint filetype_idx;
75 gboolean show_hidden;
76 gboolean more_options_visible;
77 } open;
79 filesel_state = {
82 GEANY_ENCODINGS_MAX, /* default encoding is detect from file */
83 GEANY_FILETYPES_NONE, /* default filetype is detect from extension */
84 FALSE,
85 FALSE
90 static gint filetype_combo_box_get_active_filetype(GtkComboBox *combo);
93 /* gets the ID of the current file filter */
94 static guint file_chooser_get_filter_idx(GtkFileChooser *chooser)
96 guint idx = 0;
97 GtkFileFilter *current;
98 GSList *filters, *item;
100 current = gtk_file_chooser_get_filter(chooser);
101 filters = gtk_file_chooser_list_filters(chooser);
102 foreach_slist(item, filters)
104 if (item->data == current)
105 break;
106 idx ++;
108 g_slist_free(filters);
109 return idx;
113 /* sets the current file filter from its ID */
114 static void file_chooser_set_filter_idx(GtkFileChooser *chooser, guint idx)
116 GtkFileFilter *current;
117 GSList *filters;
119 filters = gtk_file_chooser_list_filters(chooser);
120 current = g_slist_nth_data(filters, idx);
121 g_slist_free(filters);
122 gtk_file_chooser_set_filter(chooser, current);
126 static void open_file_dialog_handle_response(GtkWidget *dialog, gint response)
128 if (response == GTK_RESPONSE_ACCEPT || response == GEANY_RESPONSE_VIEW)
130 GSList *filelist;
131 GeanyFiletype *ft = NULL;
132 const gchar *charset = NULL;
133 GtkWidget *expander = ui_lookup_widget(dialog, "more_options_expander");
134 GtkWidget *filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
135 GtkWidget *encoding_combo = ui_lookup_widget(dialog, "encoding_combo");
136 gboolean ro = (response == GEANY_RESPONSE_VIEW); /* View clicked */
138 filesel_state.open.more_options_visible = gtk_expander_get_expanded(GTK_EXPANDER(expander));
139 filesel_state.open.filter_idx = file_chooser_get_filter_idx(GTK_FILE_CHOOSER(dialog));
140 filesel_state.open.filetype_idx = filetype_combo_box_get_active_filetype(GTK_COMBO_BOX(filetype_combo));
142 /* ignore detect from file item */
143 if (filesel_state.open.filetype_idx > 0)
144 ft = filetypes_index(filesel_state.open.filetype_idx);
146 filesel_state.open.encoding_idx = ui_encodings_combo_box_get_active_encoding(GTK_COMBO_BOX(encoding_combo));
147 if (filesel_state.open.encoding_idx >= 0 && filesel_state.open.encoding_idx < GEANY_ENCODINGS_MAX)
148 charset = encodings[filesel_state.open.encoding_idx].charset;
150 filelist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
151 if (filelist != NULL)
153 document_open_files(filelist, ro, ft, charset);
154 g_slist_foreach(filelist, (GFunc) g_free, NULL); /* free filenames */
156 g_slist_free(filelist);
158 if (app->project && !EMPTY(app->project->base_path))
159 gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(dialog),
160 app->project->base_path, NULL);
164 static void on_file_open_show_hidden_notify(GObject *filechooser,
165 GParamSpec *pspec, gpointer data)
167 GtkWidget *toggle_button;
169 toggle_button = ui_lookup_widget(GTK_WIDGET(filechooser), "check_hidden");
171 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_button),
172 gtk_file_chooser_get_show_hidden(GTK_FILE_CHOOSER(filechooser)));
176 static void
177 on_file_open_check_hidden_toggled(GtkToggleButton *togglebutton, GtkWidget *dialog)
179 filesel_state.open.show_hidden = gtk_toggle_button_get_active(togglebutton);
180 gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(dialog), filesel_state.open.show_hidden);
184 static void filetype_combo_cell_data_func(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
185 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
187 gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
188 gchar *text;
190 gtk_tree_model_get(tree_model, iter, 1, &text, -1);
191 g_object_set(cell, "sensitive", sensitive, "text", text, NULL);
192 g_free(text);
196 static GtkWidget *create_filetype_combo_box(void)
198 GtkTreeStore *store;
199 GtkTreeIter iter_compiled, iter_script, iter_markup, iter_misc, iter_detect;
200 GtkTreeIter *iter_parent;
201 GtkWidget *combo;
202 GtkCellRenderer *renderer;
203 GSList *node;
205 store = gtk_tree_store_new(2, G_TYPE_INT, G_TYPE_STRING);
207 gtk_tree_store_insert_with_values(store, &iter_detect, NULL, -1,
208 0, GEANY_FILETYPES_NONE, 1, _("Detect from file"), -1);
210 gtk_tree_store_insert_with_values(store, &iter_compiled, NULL, -1,
211 0, -1, 1, _("Programming Languages"), -1);
212 gtk_tree_store_insert_with_values(store, &iter_script, NULL, -1,
213 0, -1, 1, _("Scripting Languages"), -1);
214 gtk_tree_store_insert_with_values(store, &iter_markup, NULL, -1,
215 0, -1, 1, _("Markup Languages"), -1);
216 gtk_tree_store_insert_with_values(store, &iter_misc, NULL, -1,
217 0, -1, 1, _("Miscellaneous"), -1);
219 foreach_slist (node, filetypes_by_title)
221 GeanyFiletype *ft = node->data;
223 if (ft->id == GEANY_FILETYPES_NONE)
224 continue;
226 switch (ft->group)
228 case GEANY_FILETYPE_GROUP_COMPILED: iter_parent = &iter_compiled; break;
229 case GEANY_FILETYPE_GROUP_SCRIPT: iter_parent = &iter_script; break;
230 case GEANY_FILETYPE_GROUP_MARKUP: iter_parent = &iter_markup; break;
231 case GEANY_FILETYPE_GROUP_MISC: iter_parent = &iter_misc; break;
232 case GEANY_FILETYPE_GROUP_NONE:
233 default: iter_parent = NULL;
235 gtk_tree_store_insert_with_values(store, NULL, iter_parent, -1,
236 0, (gint) ft->id, 1, ft->title, -1);
239 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
240 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter_detect);
241 renderer = gtk_cell_renderer_text_new();
242 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
243 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
244 filetype_combo_cell_data_func, NULL, NULL);
246 g_object_unref(store);
248 return combo;
252 static gint filetype_combo_box_get_active_filetype(GtkComboBox *combo)
254 gint id = 0;
255 GtkTreeIter iter;
257 if (gtk_combo_box_get_active_iter(combo, &iter))
259 GtkTreeModel *model = gtk_combo_box_get_model(combo);
260 gtk_tree_model_get(model, &iter, 0, &id, -1);
262 return id;
266 static gboolean filetype_combo_box_set_active_filetype(GtkComboBox *combo, const gint id)
268 GtkTreeModel *model = gtk_combo_box_get_model(combo);
269 GtkTreeIter iter;
271 if (gtk_tree_model_get_iter_first(model, &iter))
275 gint row_id;
276 gtk_tree_model_get(model, &iter, 0, &row_id, -1);
277 if (id == row_id)
279 gtk_combo_box_set_active_iter(combo, &iter);
280 return TRUE;
283 while (ui_tree_model_iter_any_next(model, &iter, TRUE));
285 return FALSE;
289 static GtkWidget *add_file_open_extra_widget(GtkWidget *dialog)
291 GtkWidget *expander, *vbox, *table, *check_hidden;
292 GtkWidget *filetype_ebox, *filetype_label, *filetype_combo;
293 GtkWidget *encoding_ebox, *encoding_label, *encoding_combo;
295 expander = gtk_expander_new_with_mnemonic(_("_More Options"));
296 vbox = gtk_vbox_new(FALSE, 6);
297 gtk_container_add(GTK_CONTAINER(expander), vbox);
299 table = gtk_table_new(2, 4, FALSE);
301 /* line 1 with checkbox and encoding combo */
302 check_hidden = gtk_check_button_new_with_mnemonic(_("Show _hidden files"));
303 gtk_widget_show(check_hidden);
304 gtk_table_attach(GTK_TABLE(table), check_hidden, 0, 1, 0, 1,
305 (GtkAttachOptions) (GTK_FILL | GTK_EXPAND),
306 (GtkAttachOptions) (0), 0, 5);
308 /* spacing */
309 gtk_table_attach(GTK_TABLE(table), gtk_label_new(""), 1, 2, 0, 1,
310 (GtkAttachOptions) (GTK_FILL),
311 (GtkAttachOptions) (0), 5, 5);
313 encoding_label = gtk_label_new(_("Set encoding:"));
314 gtk_misc_set_alignment(GTK_MISC(encoding_label), 1, 0);
315 gtk_table_attach(GTK_TABLE(table), encoding_label, 2, 3, 0, 1,
316 (GtkAttachOptions) (GTK_FILL),
317 (GtkAttachOptions) (0), 4, 5);
318 /* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
319 encoding_ebox = gtk_event_box_new();
320 encoding_combo = ui_create_encodings_combo_box(TRUE, GEANY_ENCODINGS_MAX);
321 gtk_widget_set_tooltip_text(encoding_ebox,
322 _("Explicitly defines an encoding for the file, if it would not be detected. This is useful when you know that the encoding of a file cannot be detected correctly by Geany.\nNote if you choose multiple files, they will all be opened with the chosen encoding."));
323 gtk_container_add(GTK_CONTAINER(encoding_ebox), encoding_combo);
324 gtk_table_attach(GTK_TABLE(table), encoding_ebox, 3, 4, 0, 1,
325 (GtkAttachOptions) (GTK_FILL),
326 (GtkAttachOptions) (0), 0, 5);
328 /* line 2 with filetype combo */
329 filetype_label = gtk_label_new(_("Set filetype:"));
330 gtk_misc_set_alignment(GTK_MISC(filetype_label), 1, 0);
331 gtk_table_attach(GTK_TABLE(table), filetype_label, 2, 3, 1, 2,
332 (GtkAttachOptions) (GTK_FILL),
333 (GtkAttachOptions) (0), 4, 5);
334 /* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
335 filetype_ebox = gtk_event_box_new();
336 filetype_combo = create_filetype_combo_box();
337 gtk_widget_set_tooltip_text(filetype_ebox,
338 _("Explicitly defines a filetype for the file, if it would not be detected by filename extension.\nNote if you choose multiple files, they will all be opened with the chosen filetype."));
339 gtk_container_add(GTK_CONTAINER(filetype_ebox), filetype_combo);
340 gtk_table_attach(GTK_TABLE(table), filetype_ebox, 3, 4, 1, 2,
341 (GtkAttachOptions) (GTK_FILL),
342 (GtkAttachOptions) (0), 0, 5);
344 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
345 gtk_widget_show_all(vbox);
347 g_signal_connect(check_hidden, "toggled", G_CALLBACK(on_file_open_check_hidden_toggled), dialog);
349 ui_hookup_widget(dialog, expander, "more_options_expander");
350 ui_hookup_widget(dialog, check_hidden, "check_hidden");
351 ui_hookup_widget(dialog, filetype_combo, "filetype_combo");
352 ui_hookup_widget(dialog, encoding_combo, "encoding_combo");
354 return expander;
358 static GtkWidget *create_open_file_dialog(void)
360 GtkWidget *dialog;
361 GtkWidget *viewbtn;
362 GSList *node;
364 dialog = gtk_file_chooser_dialog_new(_("Open File"), GTK_WINDOW(main_widgets.window),
365 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
366 gtk_widget_set_name(dialog, "GeanyDialog");
368 viewbtn = gtk_dialog_add_button(GTK_DIALOG(dialog), C_("Open dialog action", "_View"), GEANY_RESPONSE_VIEW);
369 gtk_widget_set_tooltip_text(viewbtn,
370 _("Opens the file in read-only mode. If you choose more than one file to open, all files will be opened read-only."));
372 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
373 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
374 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
375 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
377 gtk_widget_set_size_request(dialog, -1, 460);
378 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
379 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
380 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
381 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
382 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
383 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
384 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE);
386 /* add checkboxes and filename entry */
387 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), add_file_open_extra_widget(dialog));
389 /* add FileFilters(start with "All Files") */
390 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),
391 filetypes_create_file_filter(filetypes[GEANY_FILETYPES_NONE]));
392 /* now create meta filter "All Source" */
393 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),
394 filetypes_create_file_filter_all_source());
395 foreach_slist(node, filetypes_by_title)
397 GeanyFiletype *ft = node->data;
399 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
400 continue;
401 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filetypes_create_file_filter(ft));
404 g_signal_connect(dialog, "notify::show-hidden",
405 G_CALLBACK(on_file_open_show_hidden_notify), NULL);
407 return dialog;
411 static void open_file_dialog_apply_settings(GtkWidget *dialog)
413 static gboolean initialized = FALSE;
414 GtkWidget *check_hidden = ui_lookup_widget(dialog, "check_hidden");
415 GtkWidget *filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
416 GtkWidget *encoding_combo = ui_lookup_widget(dialog, "encoding_combo");
417 GtkWidget *expander = ui_lookup_widget(dialog, "more_options_expander");
419 /* we can't know the initial position of combo boxes, so retreive it the first time */
420 if (! initialized)
422 filesel_state.open.filter_idx = file_chooser_get_filter_idx(GTK_FILE_CHOOSER(dialog));
424 initialized = TRUE;
426 else
428 file_chooser_set_filter_idx(GTK_FILE_CHOOSER(dialog), filesel_state.open.filter_idx);
430 gtk_expander_set_expanded(GTK_EXPANDER(expander), filesel_state.open.more_options_visible);
431 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_hidden), filesel_state.open.show_hidden);
432 ui_encodings_combo_box_set_active_encoding(GTK_COMBO_BOX(encoding_combo), filesel_state.open.encoding_idx);
433 filetype_combo_box_set_active_filetype(GTK_COMBO_BOX(filetype_combo), filesel_state.open.filetype_idx);
437 /* This shows the file selection dialog to open a file. */
438 void dialogs_show_open_file(void)
440 gchar *initdir;
442 /* set dialog directory to the current file's directory, if present */
443 initdir = utils_get_current_file_dir_utf8();
445 /* use project or default startup directory (if set) if no files are open */
446 /** TODO should it only be used when initally open the dialog and not on every show? */
447 if (! initdir)
448 initdir = g_strdup(utils_get_default_dir_utf8());
450 SETPTR(initdir, utils_get_locale_from_utf8(initdir));
452 #ifdef G_OS_WIN32
453 if (interface_prefs.use_native_windows_dialogs)
454 win32_show_document_open_dialog(GTK_WINDOW(main_widgets.window), _("Open File"), initdir);
455 else
456 #endif
458 GtkWidget *dialog = create_open_file_dialog();
459 gint response;
461 open_file_dialog_apply_settings(dialog);
463 if (initdir != NULL && g_path_is_absolute(initdir))
464 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), initdir);
466 if (app->project && !EMPTY(app->project->base_path))
467 gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),
468 app->project->base_path, NULL);
470 response = gtk_dialog_run(GTK_DIALOG(dialog));
471 open_file_dialog_handle_response(dialog, response);
472 gtk_widget_destroy(dialog);
474 g_free(initdir);
478 static gboolean handle_save_as(const gchar *utf8_filename, gboolean rename_file)
480 GeanyDocument *doc = document_get_current();
481 gboolean success = FALSE;
483 g_return_val_if_fail(!EMPTY(utf8_filename), FALSE);
485 if (doc->file_name != NULL)
487 if (rename_file)
489 document_rename_file(doc, utf8_filename);
491 /* create a new tm_source_file object otherwise tagmanager won't work correctly */
492 tm_workspace_remove_object(doc->tm_file, TRUE, TRUE);
493 doc->tm_file = NULL;
495 success = document_save_file_as(doc, utf8_filename);
497 build_menu_update(doc);
498 return success;
502 static gboolean save_as_dialog_handle_response(GtkWidget *dialog, gint response)
504 gboolean rename_file = FALSE;
505 gboolean success = FALSE;
506 gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
508 switch (response)
510 case GEANY_RESPONSE_RENAME:
511 /* rename doesn't check for empty filename or overwriting */
512 if (G_UNLIKELY(EMPTY(new_filename)))
514 utils_beep();
515 break;
517 if (g_file_test(new_filename, G_FILE_TEST_EXISTS) &&
518 !dialogs_show_question_full(NULL, NULL, NULL,
519 _("Overwrite?"),
520 _("Filename already exists!")))
521 break;
522 rename_file = TRUE;
523 /* fall through */
524 case GTK_RESPONSE_ACCEPT:
526 gchar *utf8_filename;
528 utf8_filename = utils_get_utf8_from_locale(new_filename);
529 success = handle_save_as(utf8_filename, rename_file);
530 g_free(utf8_filename);
531 break;
533 case GTK_RESPONSE_DELETE_EVENT:
534 case GTK_RESPONSE_CANCEL:
535 success = TRUE;
536 break;
538 g_free(new_filename);
540 return success;
544 static GtkWidget *create_save_file_dialog(GeanyDocument *doc)
546 GtkWidget *dialog, *rename_btn;
547 const gchar *initdir;
549 dialog = gtk_file_chooser_dialog_new(_("Save File"), GTK_WINDOW(main_widgets.window),
550 GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
551 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
552 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
553 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
554 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
555 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
556 gtk_widget_set_name(dialog, "GeanyDialog");
558 rename_btn = gtk_dialog_add_button(GTK_DIALOG(dialog), _("R_ename"), GEANY_RESPONSE_RENAME);
559 gtk_widget_set_tooltip_text(rename_btn, _("Save the file and rename it"));
560 /* disable rename unless file exists on disk */
561 gtk_widget_set_sensitive(rename_btn, doc->real_path != NULL);
563 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
564 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
565 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
566 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
568 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
569 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE);
571 /* set the folder by default to the project base dir or the global pref for opening files */
572 initdir = utils_get_default_dir_utf8();
573 if (initdir)
575 gchar *linitdir = utils_get_locale_from_utf8(initdir);
576 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), linitdir);
577 g_free(linitdir);
579 return dialog;
583 static gboolean show_save_as_gtk(GeanyDocument *doc)
585 GtkWidget *dialog;
586 gint resp;
588 g_return_val_if_fail(DOC_VALID(doc), FALSE);
590 dialog = create_save_file_dialog(doc);
592 if (doc->file_name != NULL)
594 if (g_path_is_absolute(doc->file_name))
596 gchar *locale_filename = utils_get_locale_from_utf8(doc->file_name);
597 gchar *locale_basename = g_path_get_basename(locale_filename);
598 gchar *locale_dirname = g_path_get_dirname(locale_filename);
600 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
601 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), locale_basename);
603 g_free(locale_filename);
604 g_free(locale_basename);
605 g_free(locale_dirname);
607 else
608 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), doc->file_name);
610 else
612 gchar *fname = NULL;
614 if (doc->file_type != NULL && doc->file_type->extension != NULL)
615 fname = g_strconcat(GEANY_STRING_UNTITLED, ".",
616 doc->file_type->extension, NULL);
617 else
618 fname = g_strdup(GEANY_STRING_UNTITLED);
620 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
622 g_free(fname);
625 if (app->project && !EMPTY(app->project->base_path))
626 gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),
627 app->project->base_path, NULL);
629 /* Run the dialog synchronously, pausing this function call */
632 resp = gtk_dialog_run(GTK_DIALOG(dialog));
634 while (! save_as_dialog_handle_response(dialog, resp));
636 if (app->project && !EMPTY(app->project->base_path))
637 gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(dialog),
638 app->project->base_path, NULL);
640 gtk_widget_destroy(dialog);
642 return (resp == GTK_RESPONSE_ACCEPT);
647 * Shows the Save As dialog for the current notebook page.
649 * @return @c TRUE if the file was saved, otherwise @c FALSE.
651 gboolean dialogs_show_save_as(void)
653 GeanyDocument *doc = document_get_current();
654 gboolean result = FALSE;
656 g_return_val_if_fail(doc, FALSE);
658 #ifdef G_OS_WIN32
659 if (interface_prefs.use_native_windows_dialogs)
661 gchar *utf8_name = win32_show_document_save_as_dialog(GTK_WINDOW(main_widgets.window),
662 _("Save File"), doc);
663 if (utf8_name != NULL)
664 result = handle_save_as(utf8_name, FALSE);
666 else
667 #endif
668 result = show_save_as_gtk(doc);
669 return result;
673 #ifndef G_OS_WIN32
674 static void show_msgbox_dialog(GtkWidget *dialog, GtkMessageType type, GtkWindow *parent)
676 const gchar *title;
677 switch (type)
679 case GTK_MESSAGE_ERROR:
680 title = _("Error");
681 break;
682 case GTK_MESSAGE_QUESTION:
683 title = _("Question");
684 break;
685 case GTK_MESSAGE_WARNING:
686 title = _("Warning");
687 break;
688 default:
689 title = _("Information");
690 break;
692 gtk_window_set_title(GTK_WINDOW(dialog), title);
693 gtk_window_set_icon_name(GTK_WINDOW(dialog), "geany");
694 gtk_widget_set_name(dialog, "GeanyDialog");
696 gtk_dialog_run(GTK_DIALOG(dialog));
697 gtk_widget_destroy(dialog);
699 #endif
703 * Shows a message box of the type @a type with @a text.
704 * On Unix-like systems a GTK message dialog box is shown, on Win32 systems a native Windows
705 * message dialog box is shown.
707 * @param type A @c GtkMessageType, e.g. @c GTK_MESSAGE_INFO, @c GTK_MESSAGE_WARNING,
708 * @c GTK_MESSAGE_QUESTION, @c GTK_MESSAGE_ERROR.
709 * @param text Printf()-style format string.
710 * @param ... Arguments for the @a text format string.
712 void dialogs_show_msgbox(GtkMessageType type, const gchar *text, ...)
714 #ifndef G_OS_WIN32
715 GtkWidget *dialog;
716 #endif
717 gchar *string;
718 va_list args;
719 GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
721 va_start(args, text);
722 string = g_strdup_vprintf(text, args);
723 va_end(args);
725 #ifdef G_OS_WIN32
726 win32_message_dialog(GTK_WIDGET(parent), type, string);
727 #else
728 dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
729 type, GTK_BUTTONS_OK, "%s", string);
730 show_msgbox_dialog(dialog, type, parent);
731 #endif
732 g_free(string);
736 void dialogs_show_msgbox_with_secondary(GtkMessageType type, const gchar *text, const gchar *secondary)
738 GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
739 #ifdef G_OS_WIN32
740 /* put the two strings together because Windows message boxes don't support secondary texts */
741 gchar *string = g_strconcat(text, "\n", secondary, NULL);
742 win32_message_dialog(GTK_WIDGET(parent), type, string);
743 g_free(string);
744 #else
745 GtkWidget *dialog;
746 dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
747 type, GTK_BUTTONS_OK, "%s", text);
748 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", secondary);
749 show_msgbox_dialog(dialog, type, parent);
750 #endif
754 static gint run_unsaved_dialog(const gchar *msg, const gchar *msg2)
756 GtkWidget *dialog, *button;
757 gint ret;
759 dialog = gtk_message_dialog_new(GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
760 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", msg);
761 gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
762 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", msg2);
763 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
765 button = ui_button_new_with_image(GTK_STOCK_CLEAR, _("_Don't save"));
766 gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button, GTK_RESPONSE_NO);
767 gtk_widget_show(button);
769 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
771 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
772 ret = gtk_dialog_run(GTK_DIALOG(dialog));
774 gtk_widget_destroy(dialog);
776 return ret;
780 gboolean dialogs_show_unsaved_file(GeanyDocument *doc)
782 gchar *msg, *short_fn = NULL;
783 const gchar *msg2;
784 gint response;
785 gboolean old_quitting_state = main_status.quitting;
787 /* display the file tab to remind the user of the document */
788 main_status.quitting = FALSE;
789 document_show_tab(doc);
790 main_status.quitting = old_quitting_state;
792 short_fn = document_get_basename_for_display(doc, -1);
794 msg = g_strdup_printf(_("The file '%s' is not saved."), short_fn);
795 msg2 = _("Do you want to save it before closing?");
796 g_free(short_fn);
798 response = run_unsaved_dialog(msg, msg2);
799 g_free(msg);
801 switch (response)
803 case GTK_RESPONSE_YES:
804 /* document_save_file() returns the status if the file could be saved */
805 return document_save_file(doc, FALSE);
807 case GTK_RESPONSE_NO:
808 return TRUE;
810 case GTK_RESPONSE_CANCEL: /* fall through to default and leave the function */
811 default:
812 return FALSE;
817 /* Use GtkFontChooserDialog on GTK3.2 for consistency, and because
818 * GtkFontSelectionDialog is somewhat broken on 3.4 */
819 #if GTK_CHECK_VERSION(3, 2, 0)
820 # undef GTK_FONT_SELECTION_DIALOG
821 # define GTK_FONT_SELECTION_DIALOG GTK_FONT_CHOOSER_DIALOG
823 # define gtk_font_selection_dialog_new(title) \
824 gtk_font_chooser_dialog_new((title), NULL)
825 # define gtk_font_selection_dialog_get_font_name(dlg) \
826 gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dlg))
827 # define gtk_font_selection_dialog_set_font_name(dlg, font) \
828 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dlg), (font))
829 #endif
831 static void
832 on_font_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
834 gboolean close = TRUE;
836 switch (response)
838 case GTK_RESPONSE_APPLY:
839 case GTK_RESPONSE_OK:
841 gchar *fontname;
843 fontname = gtk_font_selection_dialog_get_font_name(
844 GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel));
845 ui_set_editor_font(fontname);
846 g_free(fontname);
848 close = (response == GTK_RESPONSE_OK);
849 break;
853 if (close)
854 gtk_widget_hide(ui_widgets.open_fontsel);
858 /* This shows the font selection dialog to choose a font. */
859 void dialogs_show_open_font(void)
861 #ifdef G_OS_WIN32
862 if (interface_prefs.use_native_windows_dialogs)
864 win32_show_font_dialog();
865 return;
867 #endif
869 if (ui_widgets.open_fontsel == NULL)
871 GtkWidget *apply_button;
873 ui_widgets.open_fontsel = gtk_font_selection_dialog_new(_("Choose font"));;
874 gtk_container_set_border_width(GTK_CONTAINER(ui_widgets.open_fontsel), 4);
875 gtk_window_set_modal(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
876 gtk_window_set_destroy_with_parent(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
877 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
878 gtk_window_set_type_hint(GTK_WINDOW(ui_widgets.open_fontsel), GDK_WINDOW_TYPE_HINT_DIALOG);
879 gtk_widget_set_name(ui_widgets.open_fontsel, "GeanyDialog");
881 #if GTK_CHECK_VERSION(2, 20, 0)
882 /* apply button doesn't have a getter and is hidden by default, but we'd like to show it */
883 apply_button = gtk_dialog_get_widget_for_response(GTK_DIALOG(ui_widgets.open_fontsel), GTK_RESPONSE_APPLY);
884 #else
885 apply_button = GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel)->apply_button;
886 #endif
887 if (apply_button)
888 gtk_widget_show(apply_button);
890 g_signal_connect(ui_widgets.open_fontsel,
891 "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
892 g_signal_connect(ui_widgets.open_fontsel,
893 "response", G_CALLBACK(on_font_dialog_response), NULL);
895 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_fontsel), GTK_WINDOW(main_widgets.window));
897 gtk_font_selection_dialog_set_font_name(
898 GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel), interface_prefs.editor_font);
899 /* We make sure the dialog is visible. */
900 gtk_window_present(GTK_WINDOW(ui_widgets.open_fontsel));
904 static void
905 on_input_dialog_show(GtkDialog *dialog, GtkWidget *entry)
907 gtk_widget_grab_focus(entry);
911 static void
912 on_input_entry_activate(GtkEntry *entry, GtkDialog *dialog)
914 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
918 static void
919 on_input_numeric_activate(GtkEntry *entry, GtkDialog *dialog)
921 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
925 typedef struct
927 GtkWidget *entry;
928 GtkWidget *combo;
930 GeanyInputCallback callback;
931 gpointer data;
933 InputDialogData;
936 static void
937 on_input_dialog_response(GtkDialog *dialog, gint response, InputDialogData *data)
939 if (response == GTK_RESPONSE_ACCEPT)
941 const gchar *str = gtk_entry_get_text(GTK_ENTRY(data->entry));
943 if (data->combo != NULL)
944 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(data->combo), str, 0);
946 data->callback(str, data->data);
948 gtk_widget_hide(GTK_WIDGET(dialog));
952 /* Create and display an input dialog.
953 * persistent: whether to remember previous entry text in a combo box;
954 * in this case the dialog returned is not destroyed on a response,
955 * and can be reshown.
956 * Returns: the dialog widget. */
957 static GtkWidget *
958 dialogs_show_input_full(const gchar *title, GtkWindow *parent,
959 const gchar *label_text, const gchar *default_text,
960 gboolean persistent, GeanyInputCallback input_cb, gpointer input_cb_data,
961 GCallback insert_text_cb, gpointer insert_text_cb_data)
963 GtkWidget *dialog, *vbox;
964 InputDialogData *data = g_malloc(sizeof *data);
966 dialog = gtk_dialog_new_with_buttons(title, parent,
967 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
968 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
969 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
970 gtk_widget_set_name(dialog, "GeanyDialog");
971 gtk_box_set_spacing(GTK_BOX(vbox), 6);
973 data->combo = NULL;
974 data->entry = NULL;
975 data->callback = input_cb;
976 data->data = input_cb_data;
978 if (label_text)
980 GtkWidget *label = gtk_label_new(label_text);
981 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
982 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
983 gtk_container_add(GTK_CONTAINER(vbox), label);
986 if (persistent) /* remember previous entry text in a combo box */
988 data->combo = gtk_combo_box_text_new_with_entry();
989 data->entry = gtk_bin_get_child(GTK_BIN(data->combo));
990 ui_entry_add_clear_icon(GTK_ENTRY(data->entry));
991 gtk_container_add(GTK_CONTAINER(vbox), data->combo);
993 else
995 data->entry = gtk_entry_new();
996 ui_entry_add_clear_icon(GTK_ENTRY(data->entry));
997 gtk_container_add(GTK_CONTAINER(vbox), data->entry);
1000 if (default_text != NULL)
1002 gtk_entry_set_text(GTK_ENTRY(data->entry), default_text);
1004 gtk_entry_set_max_length(GTK_ENTRY(data->entry), 255);
1005 gtk_entry_set_width_chars(GTK_ENTRY(data->entry), 30);
1007 if (insert_text_cb != NULL)
1008 g_signal_connect(data->entry, "insert-text", insert_text_cb, insert_text_cb_data);
1009 g_signal_connect(data->entry, "activate", G_CALLBACK(on_input_entry_activate), dialog);
1010 g_signal_connect(dialog, "show", G_CALLBACK(on_input_dialog_show), data->entry);
1011 g_signal_connect_data(dialog, "response", G_CALLBACK(on_input_dialog_response), data, (GClosureNotify)g_free, 0);
1013 if (persistent)
1015 /* override default handler */
1016 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
1017 gtk_widget_show_all(dialog);
1018 return dialog;
1020 gtk_widget_show_all(dialog);
1021 gtk_dialog_run(GTK_DIALOG(dialog));
1022 gtk_widget_destroy(dialog);
1023 return NULL;
1027 /* Remember previous entry text in a combo box.
1028 * Returns: the dialog widget. */
1029 GtkWidget *
1030 dialogs_show_input_persistent(const gchar *title, GtkWindow *parent,
1031 const gchar *label_text, const gchar *default_text,
1032 GeanyInputCallback input_cb, gpointer input_cb_data)
1034 return dialogs_show_input_full(title, parent, label_text, default_text, TRUE, input_cb, input_cb_data, NULL, NULL);
1038 static void on_dialog_input(const gchar *str, gpointer data)
1040 gchar **dialog_input = data;
1041 *dialog_input = g_strdup(str);
1045 /** Asks the user for text input.
1046 * @param title Dialog title.
1047 * @param parent The currently focused window, usually @c geany->main_widgets->window.
1048 * @c NULL can be used but is discouraged due to window manager effects.
1049 * @param label_text Label text, or @c NULL.
1050 * @param default_text Text to display in the input field, or @c NULL.
1051 * @return New copy of user input or @c NULL if cancelled.
1052 * @since 0.20. */
1053 gchar *dialogs_show_input(const gchar *title, GtkWindow *parent, const gchar *label_text,
1054 const gchar *default_text)
1056 gchar *dialog_input = NULL;
1057 dialogs_show_input_full(title, parent, label_text, default_text, FALSE, on_dialog_input, &dialog_input, NULL, NULL);
1058 return dialog_input;
1062 /* Note: could be changed to dialogs_show_validated_input with argument for callback. */
1063 /* Returns: newly allocated copy of the entry text or NULL on cancel.
1064 * Specialised variant for Goto Line dialog. */
1065 gchar *dialogs_show_input_goto_line(const gchar *title, GtkWindow *parent, const gchar *label_text,
1066 const gchar *default_text)
1068 gchar *dialog_input = NULL;
1069 dialogs_show_input_full(
1070 title, parent, label_text, default_text, FALSE, on_dialog_input, &dialog_input,
1071 G_CALLBACK(ui_editable_insert_text_callback), NULL);
1072 return dialog_input;
1077 * Shows an input box to enter a numerical value using a GtkSpinButton.
1078 * If the dialog is aborted, @a value remains untouched.
1080 * @param title The dialog title.
1081 * @param label_text The shown dialog label.
1082 * @param value The default value for the spin button and the return location of the entered value.
1083 * Must be non-NULL.
1084 * @param min Minimum allowable value (see documentation for @c gtk_spin_button_new_with_range()).
1085 * @param max Maximum allowable value (see documentation for @c gtk_spin_button_new_with_range()).
1086 * @param step Increment added or subtracted by spinning the widget
1087 * (see documentation for @c gtk_spin_button_new_with_range()).
1089 * @return @c TRUE if a value was entered and the dialog closed with 'OK'. @c FALSE otherwise.
1091 * @since 0.16
1093 gboolean dialogs_show_input_numeric(const gchar *title, const gchar *label_text,
1094 gdouble *value, gdouble min, gdouble max, gdouble step)
1096 GtkWidget *dialog, *label, *spin, *vbox;
1097 gboolean res = FALSE;
1099 g_return_val_if_fail(title != NULL, FALSE);
1100 g_return_val_if_fail(label_text != NULL, FALSE);
1101 g_return_val_if_fail(value != NULL, FALSE);
1103 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
1104 GTK_DIALOG_DESTROY_WITH_PARENT,
1105 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1106 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
1107 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
1108 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
1109 gtk_widget_set_name(dialog, "GeanyDialog");
1111 label = gtk_label_new(label_text);
1112 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1114 spin = gtk_spin_button_new_with_range(min, max, step);
1115 ui_entry_add_clear_icon(GTK_ENTRY(spin));
1116 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), *value);
1117 g_signal_connect(spin, "activate", G_CALLBACK(on_input_numeric_activate), dialog);
1119 gtk_container_add(GTK_CONTAINER(vbox), label);
1120 gtk_container_add(GTK_CONTAINER(vbox), spin);
1121 gtk_widget_show_all(vbox);
1123 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
1125 *value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
1126 res = TRUE;
1128 gtk_widget_destroy(dialog);
1130 return res;
1134 void dialogs_show_file_properties(GeanyDocument *doc)
1136 GtkWidget *dialog, *label, *image, *check;
1137 gchar *file_size, *title, *base_name, *time_changed, *time_modified, *time_accessed, *enctext;
1138 gchar *short_name;
1139 #ifdef HAVE_SYS_TYPES_H
1140 struct stat st;
1141 off_t filesize;
1142 mode_t mode;
1143 gchar *locale_filename;
1144 #else
1145 gint filesize = 0;
1146 gint mode = 0;
1147 #endif
1149 /* define this ones, to avoid later trouble */
1150 #ifndef S_IRUSR
1151 # define S_IRUSR 0
1152 # define S_IWUSR 0
1153 # define S_IXUSR 0
1154 #endif
1155 #ifndef S_IRGRP
1156 # define S_IRGRP 0
1157 # define S_IWGRP 0
1158 # define S_IXGRP 0
1159 # define S_IROTH 0
1160 # define S_IWOTH 0
1161 # define S_IXOTH 0
1162 #endif
1164 g_return_if_fail(doc == NULL || doc->is_valid);
1166 if (doc == NULL || doc->file_name == NULL)
1168 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
1169 _("An error occurred or file information could not be retrieved (e.g. from a new file)."));
1170 return;
1174 #ifdef HAVE_SYS_TYPES_H
1175 locale_filename = utils_get_locale_from_utf8(doc->file_name);
1176 if (g_stat(locale_filename, &st) == 0)
1178 /* first copy the returned string and the trim it, to not modify the static glibc string
1179 * g_strchomp() is used to remove trailing EOL chars, which are there for whatever reason */
1180 time_changed = g_strchomp(g_strdup(ctime(&st.st_ctime)));
1181 time_modified = g_strchomp(g_strdup(ctime(&st.st_mtime)));
1182 time_accessed = g_strchomp(g_strdup(ctime(&st.st_atime)));
1183 filesize = st.st_size;
1184 mode = st.st_mode;
1186 else
1188 time_changed = g_strdup(_("unknown"));
1189 time_modified = g_strdup(_("unknown"));
1190 time_accessed = g_strdup(_("unknown"));
1191 filesize = (off_t) 0;
1192 mode = (mode_t) 0;
1194 g_free(locale_filename);
1195 #else
1196 time_changed = g_strdup(_("unknown"));
1197 time_modified = g_strdup(_("unknown"));
1198 time_accessed = g_strdup(_("unknown"));
1199 #endif
1201 base_name = g_path_get_basename(doc->file_name);
1202 short_name = utils_str_middle_truncate(base_name, 30);
1203 title = g_strdup_printf(_("%s Properties"), short_name);
1204 dialog = ui_builder_get_object("properties_dialog");
1205 gtk_window_set_title(GTK_WINDOW(dialog), title);
1206 g_free(short_name);
1207 g_free(title);
1208 gtk_widget_set_name(dialog, "GeanyDialog");
1210 label = ui_lookup_widget(dialog, "file_name_label");
1211 gtk_label_set_text(GTK_LABEL(label), base_name);
1213 image = ui_lookup_widget(dialog, "file_type_image");
1214 gtk_image_set_from_gicon(GTK_IMAGE(image), doc->file_type->icon,
1215 GTK_ICON_SIZE_BUTTON);
1217 label = ui_lookup_widget(dialog, "file_type_label");
1218 gtk_label_set_text(GTK_LABEL(label), doc->file_type->title);
1220 label = ui_lookup_widget(dialog, "file_size_label");
1221 file_size = utils_make_human_readable_str(filesize, 1, 0);
1222 gtk_label_set_text(GTK_LABEL(label), file_size);
1223 g_free(file_size);
1225 label = ui_lookup_widget(dialog, "file_location_label");
1226 gtk_label_set_text(GTK_LABEL(label), doc->file_name);
1228 check = ui_lookup_widget(dialog, "file_read_only_check");
1229 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), doc->readonly);
1231 label = ui_lookup_widget(dialog, "file_encoding_label");
1232 enctext = g_strdup_printf("%s %s",
1233 doc->encoding,
1234 (encodings_is_unicode_charset(doc->encoding)) ?
1235 ((doc->has_bom) ? _("(with BOM)") : _("(without BOM)")) : "");
1236 gtk_label_set_text(GTK_LABEL(label), enctext);
1237 g_free(enctext);
1239 label = ui_lookup_widget(dialog, "file_modified_label");
1240 gtk_label_set_text(GTK_LABEL(label), time_modified);
1241 label = ui_lookup_widget(dialog, "file_changed_label");
1242 gtk_label_set_text(GTK_LABEL(label), time_changed);
1243 label = ui_lookup_widget(dialog, "file_accessed_label");
1244 gtk_label_set_text(GTK_LABEL(label), time_accessed);
1246 /* permissions */
1247 check = ui_lookup_widget(dialog, "file_perm_owner_r_check");
1248 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IRUSR);
1249 check = ui_lookup_widget(dialog, "file_perm_owner_w_check");
1250 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWUSR);
1251 check = ui_lookup_widget(dialog, "file_perm_owner_x_check");
1252 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXUSR);
1253 check = ui_lookup_widget(dialog, "file_perm_group_r_check");
1254 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IRGRP);
1255 check = ui_lookup_widget(dialog, "file_perm_group_w_check");
1256 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWGRP);
1257 check = ui_lookup_widget(dialog, "file_perm_group_x_check");
1258 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXGRP);
1259 check = ui_lookup_widget(dialog, "file_perm_other_r_check");
1260 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IROTH);
1261 check = ui_lookup_widget(dialog, "file_perm_other_w_check");
1262 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWOTH);
1263 check = ui_lookup_widget(dialog, "file_perm_other_x_check");
1264 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXOTH);
1266 g_free(base_name);
1267 g_free(time_changed);
1268 g_free(time_modified);
1269 g_free(time_accessed);
1271 gtk_widget_show(dialog);
1275 /* extra_text can be NULL; otherwise it is displayed below main_text.
1276 * if parent is NULL, main_widgets.window will be used
1277 * btn_1, btn_2, btn_3 can be NULL.
1278 * returns response_1, response_2 or response_3 */
1279 static gint show_prompt(GtkWidget *parent,
1280 const gchar *btn_1, GtkResponseType response_1,
1281 const gchar *btn_2, GtkResponseType response_2,
1282 const gchar *btn_3, GtkResponseType response_3,
1283 const gchar *question_text, const gchar *extra_text)
1285 gboolean ret = FALSE;
1286 GtkWidget *dialog;
1287 GtkWidget *btn;
1289 if (btn_2 == NULL)
1291 btn_2 = GTK_STOCK_NO;
1292 response_2 = GTK_RESPONSE_NO;
1294 if (btn_3 == NULL)
1296 btn_3 = GTK_STOCK_YES;
1297 response_3 = GTK_RESPONSE_YES;
1300 #ifdef G_OS_WIN32
1301 /* our native dialog code doesn't support custom buttons */
1302 if (utils_str_equal(btn_3, GTK_STOCK_YES) &&
1303 utils_str_equal(btn_2, GTK_STOCK_NO) && btn_1 == NULL)
1305 gchar *string = (extra_text == NULL) ? g_strdup(question_text) :
1306 g_strconcat(question_text, "\n\n", extra_text, NULL);
1308 ret = win32_message_dialog(parent, GTK_MESSAGE_QUESTION, string);
1309 ret = ret ? response_3 : response_2;
1310 g_free(string);
1311 return ret;
1313 #endif
1314 if (parent == NULL && main_status.main_window_realized)
1315 parent = main_widgets.window;
1317 dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
1318 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
1319 GTK_BUTTONS_NONE, "%s", question_text);
1320 gtk_widget_set_name(dialog, "GeanyDialog");
1321 gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
1322 gtk_window_set_icon_name(GTK_WINDOW(dialog), "geany");
1324 /* question_text will be in bold if optional extra_text used */
1325 if (extra_text != NULL)
1326 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
1327 "%s", extra_text);
1329 if (btn_1 != NULL)
1330 gtk_dialog_add_button(GTK_DIALOG(dialog), btn_1, response_1);
1332 /* For a cancel button, use cancel response so user can press escape to cancel */
1333 btn = gtk_dialog_add_button(GTK_DIALOG(dialog), btn_2,
1334 utils_str_equal(btn_2, GTK_STOCK_CANCEL) ? GTK_RESPONSE_CANCEL : response_2);
1335 /* we don't want a default, but we need to override the apply button as default */
1336 gtk_widget_grab_default(btn);
1337 gtk_dialog_add_button(GTK_DIALOG(dialog), btn_3, response_3);
1339 ret = gtk_dialog_run(GTK_DIALOG(dialog));
1340 gtk_widget_destroy(dialog);
1342 if (ret == GTK_RESPONSE_CANCEL)
1343 ret = response_2;
1344 return ret;
1349 * Shows a question message box with @a text and Yes/No buttons.
1350 * On Unix-like systems a GTK message dialog box is shown, on Win32 systems a native Windows
1351 * message dialog box is shown.
1353 * @param text Printf()-style format string.
1354 * @param ... Arguments for the @a text format string.
1356 * @return @c TRUE if the user answered with Yes, otherwise @c FALSE.
1358 gboolean dialogs_show_question(const gchar *text, ...)
1360 gchar *string;
1361 va_list args;
1362 GtkWidget *parent = (main_status.main_window_realized) ? main_widgets.window : NULL;
1363 gint result;
1365 va_start(args, text);
1366 string = g_strdup_vprintf(text, args);
1367 va_end(args);
1368 result = show_prompt(parent,
1369 NULL, GTK_RESPONSE_NONE,
1370 GTK_STOCK_NO, GTK_RESPONSE_NO,
1371 GTK_STOCK_YES, GTK_RESPONSE_YES,
1372 string, NULL);
1373 g_free(string);
1374 return (result == GTK_RESPONSE_YES);
1378 /* extra_text can be NULL; otherwise it is displayed below main_text.
1379 * if parent is NULL, main_widgets.window will be used
1380 * yes_btn, no_btn can be NULL. */
1381 gboolean dialogs_show_question_full(GtkWidget *parent, const gchar *yes_btn, const gchar *no_btn,
1382 const gchar *extra_text, const gchar *main_text, ...)
1384 gint result;
1385 gchar *string;
1386 va_list args;
1388 va_start(args, main_text);
1389 string = g_strdup_vprintf(main_text, args);
1390 va_end(args);
1391 result = show_prompt(parent,
1392 NULL, GTK_RESPONSE_NONE,
1393 no_btn, GTK_RESPONSE_NO,
1394 yes_btn, GTK_RESPONSE_YES,
1395 string, extra_text);
1396 g_free(string);
1397 return (result == GTK_RESPONSE_YES);
1401 /* extra_text can be NULL; otherwise it is displayed below main_text.
1402 * if parent is NULL, main_widgets.window will be used
1403 * btn_1, btn_2, btn_3 can be NULL.
1404 * returns response_1, response_2 or response_3 */
1405 gint dialogs_show_prompt(GtkWidget *parent,
1406 const gchar *btn_1, GtkResponseType response_1,
1407 const gchar *btn_2, GtkResponseType response_2,
1408 const gchar *btn_3, GtkResponseType response_3,
1409 const gchar *extra_text, const gchar *main_text, ...)
1411 gchar *string;
1412 va_list args;
1413 gint result;
1415 va_start(args, main_text);
1416 string = g_strdup_vprintf(main_text, args);
1417 va_end(args);
1418 result = show_prompt(parent, btn_1, response_1, btn_2, response_2, btn_3, response_3,
1419 string, extra_text);
1420 g_free(string);
1421 return result;