Merge pull request #3413 from techee/config_comment
[geany-mirror.git] / src / dialogs.c
blobee6d76e978865f99f5e7ea1730fe7ab804ed4f70
1 /*
2 * dialogs.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * File related dialogs, miscellaneous dialogs, font dialog.
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "dialogs.h"
31 #include "app.h"
32 #include "build.h"
33 #include "document.h"
34 #include "encodings.h"
35 #include "encodingsprivate.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 <gtk/gtk.h>
44 #include <gdk/gdkkeysyms.h>
45 #include <string.h>
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
49 #endif
50 #include <time.h>
52 #ifdef HAVE_SYS_TYPES_H
53 # include <sys/types.h>
54 #endif
56 /* gstdio.h also includes sys/stat.h */
57 #include <glib/gstdio.h>
60 enum
62 GEANY_RESPONSE_RENAME,
63 GEANY_RESPONSE_VIEW
67 static struct FileSelState
69 struct
71 guint filter_idx;
72 gint encoding_idx;
73 gint filetype_idx;
74 gboolean show_hidden;
75 gboolean more_options_visible;
76 } open;
78 filesel_state = {
81 GEANY_ENCODINGS_MAX, /* default encoding is detect from file */
82 -1, /* default filetype is detect from extension */
83 FALSE,
84 FALSE
89 static gint filetype_combo_box_get_active_filetype(GtkComboBox *combo);
92 /* gets the ID of the current file filter */
93 static guint file_chooser_get_filter_idx(GtkFileChooser *chooser)
95 guint idx = 0;
96 GtkFileFilter *current;
97 GSList *filters, *item;
99 current = gtk_file_chooser_get_filter(chooser);
100 filters = gtk_file_chooser_list_filters(chooser);
101 foreach_slist(item, filters)
103 if (item->data == current)
104 break;
105 idx ++;
107 g_slist_free(filters);
108 return idx;
112 /* sets the current file filter from its ID */
113 static void file_chooser_set_filter_idx(GtkFileChooser *chooser, guint idx)
115 GtkFileFilter *current;
116 GSList *filters;
118 filters = gtk_file_chooser_list_filters(chooser);
119 current = g_slist_nth_data(filters, idx);
120 g_slist_free(filters);
121 gtk_file_chooser_set_filter(chooser, current);
125 static gboolean open_file_dialog_handle_response(GtkWidget *dialog, gint response)
127 gboolean ret = TRUE;
129 if (response == GTK_RESPONSE_ACCEPT || response == GEANY_RESPONSE_VIEW)
131 GSList *filelist;
132 GeanyFiletype *ft = NULL;
133 const gchar *charset = NULL;
134 GtkWidget *expander = ui_lookup_widget(dialog, "more_options_expander");
135 GtkWidget *filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
136 GtkWidget *encoding_combo = ui_lookup_widget(dialog, "encoding_combo");
137 gboolean ro = (response == GEANY_RESPONSE_VIEW); /* View clicked */
139 filesel_state.open.more_options_visible = gtk_expander_get_expanded(GTK_EXPANDER(expander));
140 filesel_state.open.filter_idx = file_chooser_get_filter_idx(GTK_FILE_CHOOSER(dialog));
141 filesel_state.open.filetype_idx = filetype_combo_box_get_active_filetype(GTK_COMBO_BOX(filetype_combo));
143 /* ignore detect from file item */
144 if (filesel_state.open.filetype_idx >= 0)
145 ft = filetypes_index(filesel_state.open.filetype_idx);
147 filesel_state.open.encoding_idx = ui_encodings_combo_box_get_active_encoding(GTK_COMBO_BOX(encoding_combo));
148 if (filesel_state.open.encoding_idx >= 0 && filesel_state.open.encoding_idx < GEANY_ENCODINGS_MAX)
149 charset = encodings[filesel_state.open.encoding_idx].charset;
151 filelist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
152 if (filelist != NULL)
154 const gchar *first = filelist->data;
156 // When there's only one filename it may have been typed manually
157 if (!filelist->next && !g_file_test(first, G_FILE_TEST_EXISTS))
159 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("\"%s\" was not found."), first);
160 ret = FALSE;
162 else
164 document_open_files(filelist, ro, ft, charset);
166 g_slist_foreach(filelist, (GFunc) g_free, NULL); /* free filenames */
168 g_slist_free(filelist);
170 if (app->project && !EMPTY(app->project->base_path))
171 gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(dialog),
172 app->project->base_path, NULL);
173 return ret;
177 static void on_file_open_show_hidden_notify(GObject *filechooser,
178 GParamSpec *pspec, gpointer data)
180 GtkWidget *toggle_button;
182 toggle_button = ui_lookup_widget(GTK_WIDGET(filechooser), "check_hidden");
184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_button),
185 gtk_file_chooser_get_show_hidden(GTK_FILE_CHOOSER(filechooser)));
189 static void
190 on_file_open_check_hidden_toggled(GtkToggleButton *togglebutton, GtkWidget *dialog)
192 filesel_state.open.show_hidden = gtk_toggle_button_get_active(togglebutton);
193 gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(dialog), filesel_state.open.show_hidden);
197 static void filetype_combo_cell_data_func(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
198 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
200 gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
201 gchar *text;
203 gtk_tree_model_get(tree_model, iter, 1, &text, -1);
204 g_object_set(cell, "sensitive", sensitive, "text", text, NULL);
205 g_free(text);
209 static GtkWidget *create_filetype_combo_box(void)
211 GtkTreeStore *store;
212 GtkTreeIter iter_compiled, iter_script, iter_markup, iter_misc, iter_detect;
213 GtkTreeIter *iter_parent;
214 GtkWidget *combo;
215 GtkCellRenderer *renderer;
216 GSList *node;
218 store = gtk_tree_store_new(2, G_TYPE_INT, G_TYPE_STRING);
220 gtk_tree_store_insert_with_values(store, &iter_detect, NULL, -1,
221 0, -1 /* auto-detect */, 1, _("Detect from file"), -1);
223 gtk_tree_store_insert_with_values(store, &iter_compiled, NULL, -1,
224 0, -1, 1, _("Programming Languages"), -1);
225 gtk_tree_store_insert_with_values(store, &iter_script, NULL, -1,
226 0, -1, 1, _("Scripting Languages"), -1);
227 gtk_tree_store_insert_with_values(store, &iter_markup, NULL, -1,
228 0, -1, 1, _("Markup Languages"), -1);
229 gtk_tree_store_insert_with_values(store, &iter_misc, NULL, -1,
230 0, -1, 1, _("Miscellaneous"), -1);
232 foreach_slist (node, filetypes_by_title)
234 GeanyFiletype *ft = node->data;
236 switch (ft->group)
238 case GEANY_FILETYPE_GROUP_COMPILED: iter_parent = &iter_compiled; break;
239 case GEANY_FILETYPE_GROUP_SCRIPT: iter_parent = &iter_script; break;
240 case GEANY_FILETYPE_GROUP_MARKUP: iter_parent = &iter_markup; break;
241 case GEANY_FILETYPE_GROUP_MISC: iter_parent = &iter_misc; break;
242 case GEANY_FILETYPE_GROUP_NONE:
243 default: iter_parent = NULL;
245 gtk_tree_store_insert_with_values(store, NULL, iter_parent, -1,
246 0, (gint) ft->id, 1, ft->title, -1);
249 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
250 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter_detect);
251 renderer = gtk_cell_renderer_text_new();
252 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
253 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
254 filetype_combo_cell_data_func, NULL, NULL);
256 g_object_unref(store);
258 return combo;
262 /* the filetype, or -1 for auto-detect */
263 static gint filetype_combo_box_get_active_filetype(GtkComboBox *combo)
265 gint id = -1;
266 GtkTreeIter iter;
268 if (gtk_combo_box_get_active_iter(combo, &iter))
270 GtkTreeModel *model = gtk_combo_box_get_model(combo);
271 gtk_tree_model_get(model, &iter, 0, &id, -1);
273 return id;
277 static gboolean filetype_combo_box_set_active_filetype(GtkComboBox *combo, const gint id)
279 GtkTreeModel *model = gtk_combo_box_get_model(combo);
280 GtkTreeIter iter;
282 if (gtk_tree_model_get_iter_first(model, &iter))
286 gint row_id;
287 gtk_tree_model_get(model, &iter, 0, &row_id, -1);
288 if (id == row_id)
290 gtk_combo_box_set_active_iter(combo, &iter);
291 return TRUE;
294 while (ui_tree_model_iter_any_next(model, &iter, TRUE));
296 return FALSE;
300 static GtkWidget *add_file_open_extra_widget(GtkWidget *dialog)
302 GtkWidget *expander, *vbox, *table, *check_hidden;
303 GtkWidget *filetype_ebox, *filetype_label, *filetype_combo;
304 GtkWidget *encoding_ebox, *encoding_label, *encoding_combo;
306 expander = gtk_expander_new_with_mnemonic(_("_More Options"));
307 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
308 gtk_container_add(GTK_CONTAINER(expander), vbox);
310 table = gtk_table_new(2, 4, FALSE);
312 /* line 1 with checkbox and encoding combo */
313 check_hidden = gtk_check_button_new_with_mnemonic(_("Show _hidden files"));
314 gtk_widget_show(check_hidden);
315 gtk_table_attach(GTK_TABLE(table), check_hidden, 0, 1, 0, 1,
316 (GtkAttachOptions) (GTK_FILL | GTK_EXPAND),
317 (GtkAttachOptions) (0), 0, 5);
319 /* spacing */
320 gtk_table_attach(GTK_TABLE(table), gtk_label_new(""), 1, 2, 0, 1,
321 (GtkAttachOptions) (GTK_FILL),
322 (GtkAttachOptions) (0), 5, 5);
324 encoding_label = gtk_label_new(_("Set encoding:"));
325 gtk_misc_set_alignment(GTK_MISC(encoding_label), 1, 0);
326 gtk_table_attach(GTK_TABLE(table), encoding_label, 2, 3, 0, 1,
327 (GtkAttachOptions) (GTK_FILL),
328 (GtkAttachOptions) (0), 4, 5);
329 /* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
330 encoding_ebox = gtk_event_box_new();
331 encoding_combo = ui_create_encodings_combo_box(TRUE, GEANY_ENCODINGS_MAX);
332 gtk_widget_set_tooltip_text(encoding_ebox,
333 _("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."));
334 gtk_container_add(GTK_CONTAINER(encoding_ebox), encoding_combo);
335 gtk_table_attach(GTK_TABLE(table), encoding_ebox, 3, 4, 0, 1,
336 (GtkAttachOptions) (GTK_FILL),
337 (GtkAttachOptions) (0), 0, 5);
339 /* line 2 with filetype combo */
340 filetype_label = gtk_label_new(_("Set filetype:"));
341 gtk_misc_set_alignment(GTK_MISC(filetype_label), 1, 0);
342 gtk_table_attach(GTK_TABLE(table), filetype_label, 2, 3, 1, 2,
343 (GtkAttachOptions) (GTK_FILL),
344 (GtkAttachOptions) (0), 4, 5);
345 /* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
346 filetype_ebox = gtk_event_box_new();
347 filetype_combo = create_filetype_combo_box();
348 gtk_widget_set_tooltip_text(filetype_ebox,
349 _("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."));
350 gtk_container_add(GTK_CONTAINER(filetype_ebox), filetype_combo);
351 gtk_table_attach(GTK_TABLE(table), filetype_ebox, 3, 4, 1, 2,
352 (GtkAttachOptions) (GTK_FILL),
353 (GtkAttachOptions) (0), 0, 5);
355 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
356 gtk_widget_show_all(vbox);
358 g_signal_connect(check_hidden, "toggled", G_CALLBACK(on_file_open_check_hidden_toggled), dialog);
360 ui_hookup_widget(dialog, expander, "more_options_expander");
361 ui_hookup_widget(dialog, check_hidden, "check_hidden");
362 ui_hookup_widget(dialog, filetype_combo, "filetype_combo");
363 ui_hookup_widget(dialog, encoding_combo, "encoding_combo");
365 return expander;
369 static GtkWidget *create_open_file_dialog(void)
371 GtkWidget *dialog;
372 GtkWidget *viewbtn;
373 GSList *node;
375 dialog = gtk_file_chooser_dialog_new(_("Open File"), GTK_WINDOW(main_widgets.window),
376 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
377 gtk_widget_set_name(dialog, "GeanyDialog");
379 viewbtn = gtk_dialog_add_button(GTK_DIALOG(dialog), C_("Open dialog action", "_View"), GEANY_RESPONSE_VIEW);
380 gtk_widget_set_tooltip_text(viewbtn,
381 _("Opens the file in read-only mode. If you choose more than one file to open, all files will be opened read-only."));
383 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
384 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
385 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
386 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
388 gtk_widget_set_size_request(dialog, -1, 460);
389 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
390 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
391 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
392 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
393 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
394 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
395 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE);
397 /* add checkboxes and filename entry */
398 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), add_file_open_extra_widget(dialog));
400 /* add FileFilters(start with "All Files") */
401 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),
402 filetypes_create_file_filter(filetypes[GEANY_FILETYPES_NONE]));
403 /* now create meta filter "All Source" */
404 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),
405 filetypes_create_file_filter_all_source());
406 foreach_slist(node, filetypes_by_title)
408 GeanyFiletype *ft = node->data;
410 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
411 continue;
412 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filetypes_create_file_filter(ft));
415 g_signal_connect(dialog, "notify::show-hidden",
416 G_CALLBACK(on_file_open_show_hidden_notify), NULL);
418 return dialog;
422 static void open_file_dialog_apply_settings(GtkWidget *dialog)
424 static gboolean initialized = FALSE;
425 GtkWidget *check_hidden = ui_lookup_widget(dialog, "check_hidden");
426 GtkWidget *filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
427 GtkWidget *encoding_combo = ui_lookup_widget(dialog, "encoding_combo");
428 GtkWidget *expander = ui_lookup_widget(dialog, "more_options_expander");
430 /* we can't know the initial position of combo boxes, so retrieve it the first time */
431 if (! initialized)
433 filesel_state.open.filter_idx = file_chooser_get_filter_idx(GTK_FILE_CHOOSER(dialog));
435 initialized = TRUE;
437 else
439 file_chooser_set_filter_idx(GTK_FILE_CHOOSER(dialog), filesel_state.open.filter_idx);
441 gtk_expander_set_expanded(GTK_EXPANDER(expander), filesel_state.open.more_options_visible);
442 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_hidden), filesel_state.open.show_hidden);
443 ui_encodings_combo_box_set_active_encoding(GTK_COMBO_BOX(encoding_combo), filesel_state.open.encoding_idx);
444 filetype_combo_box_set_active_filetype(GTK_COMBO_BOX(filetype_combo), filesel_state.open.filetype_idx);
448 /* This shows the file selection dialog to open a file. */
449 void dialogs_show_open_file(void)
451 gchar *initdir;
452 GtkWidget *dialog;
454 /* set dialog directory to the current file's directory, if present */
455 initdir = utils_get_current_file_dir_utf8();
457 /* use project or default startup directory (if set) if no files are open */
458 /** TODO should it only be used when initially open the dialog and not on every show? */
459 if (! initdir)
460 initdir = g_strdup(utils_get_default_dir_utf8());
462 SETPTR(initdir, utils_get_locale_from_utf8(initdir));
464 dialog = create_open_file_dialog();
465 open_file_dialog_apply_settings(dialog);
467 if (initdir != NULL && g_path_is_absolute(initdir))
468 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), initdir);
470 if (app->project && !EMPTY(app->project->base_path))
471 gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),
472 app->project->base_path, NULL);
474 while (!open_file_dialog_handle_response(dialog,
475 gtk_dialog_run(GTK_DIALOG(dialog))));
476 gtk_widget_destroy(dialog);
478 g_free(initdir);
482 static gboolean handle_save_as(GeanyDocument *doc,
483 const gchar *utf8_filename, gboolean rename_file)
485 gboolean success = FALSE;
486 g_return_val_if_fail(DOC_VALID(doc), FALSE);
487 g_return_val_if_fail(!EMPTY(utf8_filename), FALSE);
489 if (doc->file_name != NULL)
491 if (rename_file)
493 document_rename_file(doc, utf8_filename);
495 if (doc->tm_file)
497 /* create a new tm_source_file object otherwise tagmanager won't work correctly */
498 tm_workspace_remove_source_file(doc->tm_file);
499 tm_source_file_free(doc->tm_file);
500 doc->tm_file = NULL;
503 success = document_save_file_as(doc, utf8_filename);
505 build_menu_update(doc);
506 return success;
510 static gboolean save_as_dialog_handle_response(GeanyDocument *doc,
511 GtkWidget *dialog, gint response)
513 gboolean rename_file = FALSE;
514 gboolean success = FALSE;
515 gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
517 switch (response)
519 case GEANY_RESPONSE_RENAME:
520 /* rename doesn't check for empty filename or overwriting */
521 if (G_UNLIKELY(EMPTY(new_filename)))
523 utils_beep();
524 break;
526 if (g_file_test(new_filename, G_FILE_TEST_EXISTS) &&
527 !dialogs_show_question_full(NULL, NULL, NULL,
528 _("Overwrite?"),
529 _("Filename already exists!")))
530 break;
531 rename_file = TRUE;
532 /* fall through */
533 case GTK_RESPONSE_ACCEPT:
535 gchar *utf8_filename;
537 utf8_filename = utils_get_utf8_from_locale(new_filename);
538 success = handle_save_as(doc, utf8_filename, rename_file);
539 g_free(utf8_filename);
540 break;
542 case GTK_RESPONSE_DELETE_EVENT:
543 case GTK_RESPONSE_CANCEL:
544 success = TRUE;
545 break;
547 g_free(new_filename);
549 return success;
553 static GtkWidget *create_save_file_dialog(GeanyDocument *doc)
555 GtkWidget *dialog, *rename_btn;
556 const gchar *initdir;
558 dialog = gtk_file_chooser_dialog_new(_("Save File"), GTK_WINDOW(main_widgets.window),
559 GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
560 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
561 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
562 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
563 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
564 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
565 gtk_widget_set_name(dialog, "GeanyDialog");
567 rename_btn = gtk_dialog_add_button(GTK_DIALOG(dialog), _("R_ename"), GEANY_RESPONSE_RENAME);
568 gtk_widget_set_tooltip_text(rename_btn, _("Save the file and rename it"));
569 /* disable rename unless file exists on disk */
570 gtk_widget_set_sensitive(rename_btn, doc->real_path != NULL);
572 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
573 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
574 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
575 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
577 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
578 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE);
580 /* set the folder by default to the project base dir or the global pref for opening files */
581 initdir = utils_get_default_dir_utf8();
582 if (initdir)
584 gchar *linitdir = utils_get_locale_from_utf8(initdir);
585 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), linitdir);
586 g_free(linitdir);
588 return dialog;
592 static gboolean show_save_as_gtk(GeanyDocument *doc)
594 GtkWidget *dialog;
595 gint resp;
597 g_return_val_if_fail(DOC_VALID(doc), FALSE);
599 dialog = create_save_file_dialog(doc);
601 if (doc->file_name != NULL)
603 if (g_path_is_absolute(doc->file_name))
605 gchar *locale_filename = utils_get_locale_from_utf8(doc->file_name);
606 gchar *locale_basename = g_path_get_basename(locale_filename);
607 gchar *locale_dirname = g_path_get_dirname(locale_filename);
609 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
610 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), locale_basename);
612 g_free(locale_filename);
613 g_free(locale_basename);
614 g_free(locale_dirname);
616 else
617 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), doc->file_name);
619 else
621 gchar *fname = NULL;
623 if (doc->file_type != NULL && doc->file_type->extension != NULL)
624 fname = g_strconcat(GEANY_STRING_UNTITLED, ".",
625 doc->file_type->extension, NULL);
626 else
627 fname = g_strdup(GEANY_STRING_UNTITLED);
629 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
631 g_free(fname);
634 if (app->project && !EMPTY(app->project->base_path))
635 gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),
636 app->project->base_path, NULL);
638 /* Run the dialog synchronously, pausing this function call */
641 resp = gtk_dialog_run(GTK_DIALOG(dialog));
643 while (! save_as_dialog_handle_response(doc, dialog, resp));
645 if (app->project && !EMPTY(app->project->base_path))
646 gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(dialog),
647 app->project->base_path, NULL);
649 gtk_widget_destroy(dialog);
651 return (resp == GTK_RESPONSE_ACCEPT);
656 * Shows the Save As dialog for the current notebook page.
658 * @return @c TRUE if the file was saved, otherwise @c FALSE.
660 GEANY_API_SYMBOL
661 gboolean dialogs_show_save_as(void)
663 GeanyDocument *doc = document_get_current();
664 gboolean result = FALSE;
666 g_return_val_if_fail(doc, FALSE);
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 GEANY_API_SYMBOL
713 void dialogs_show_msgbox(GtkMessageType type, const gchar *text, ...)
715 #ifndef G_OS_WIN32
716 GtkWidget *dialog;
717 #endif
718 gchar *string;
719 va_list args;
720 GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
722 va_start(args, text);
723 string = g_strdup_vprintf(text, args);
724 va_end(args);
726 #ifdef G_OS_WIN32
727 win32_message_dialog(GTK_WIDGET(parent), type, string);
728 #else
729 dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
730 type, GTK_BUTTONS_OK, "%s", string);
731 show_msgbox_dialog(dialog, type, parent);
732 #endif
733 g_free(string);
737 void dialogs_show_msgbox_with_secondary(GtkMessageType type, const gchar *text, const gchar *secondary)
739 GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
740 #ifdef G_OS_WIN32
741 /* put the two strings together because Windows message boxes don't support secondary texts */
742 gchar *string = g_strconcat(text, "\n", secondary, NULL);
743 win32_message_dialog(GTK_WIDGET(parent), type, string);
744 g_free(string);
745 #else
746 GtkWidget *dialog;
747 dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
748 type, GTK_BUTTONS_OK, "%s", text);
749 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", secondary);
750 show_msgbox_dialog(dialog, type, parent);
751 #endif
755 static gint run_unsaved_dialog(const gchar *msg, const gchar *msg2)
757 GtkWidget *dialog, *button;
758 gint ret;
760 dialog = gtk_message_dialog_new(GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
761 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", msg);
762 gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
763 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", msg2);
764 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
766 button = ui_button_new_with_image(GTK_STOCK_CLEAR, _("_Don't save"));
767 gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button, GTK_RESPONSE_NO);
768 gtk_widget_show(button);
770 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
772 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
773 ret = gtk_dialog_run(GTK_DIALOG(dialog));
775 gtk_widget_destroy(dialog);
777 return ret;
781 gboolean dialogs_show_unsaved_file(GeanyDocument *doc)
783 gchar *msg, *short_fn = NULL;
784 const gchar *msg2;
785 gint response;
786 gboolean old_quitting_state = main_status.quitting;
788 /* display the file tab to remind the user of the document */
789 main_status.quitting = FALSE;
790 document_show_tab(doc);
791 main_status.quitting = old_quitting_state;
793 short_fn = document_get_basename_for_display(doc, -1);
795 msg = g_strdup_printf(_("The file '%s' is not saved."), short_fn);
796 msg2 = _("Do you want to save it before closing?");
797 g_free(short_fn);
799 response = run_unsaved_dialog(msg, msg2);
800 g_free(msg);
802 switch (response)
804 case GTK_RESPONSE_YES:
805 /* document_save_file() returns the status if the file could be saved */
806 return document_save_file(doc, FALSE);
808 case GTK_RESPONSE_NO:
809 return TRUE;
811 case GTK_RESPONSE_CANCEL: /* fall through to default and leave the function */
812 default:
813 return FALSE;
818 /* Use GtkFontChooserDialog on GTK3.2 for consistency, and because
819 * GtkFontSelectionDialog is somewhat broken on 3.4 */
820 #if GTK_CHECK_VERSION(3, 2, 0)
821 # undef GTK_FONT_SELECTION_DIALOG
822 # define GTK_FONT_SELECTION_DIALOG GTK_FONT_CHOOSER_DIALOG
824 # define gtk_font_selection_dialog_new(title) \
825 gtk_font_chooser_dialog_new((title), NULL)
826 # define gtk_font_selection_dialog_get_font_name(dlg) \
827 gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dlg))
828 # define gtk_font_selection_dialog_set_font_name(dlg, font) \
829 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dlg), (font))
830 #endif
832 static void
833 on_font_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
835 gboolean close = TRUE;
837 switch (response)
839 case GTK_RESPONSE_APPLY:
840 case GTK_RESPONSE_OK:
842 gchar *fontname;
844 fontname = gtk_font_selection_dialog_get_font_name(
845 GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel));
846 ui_set_editor_font(fontname);
847 g_free(fontname);
849 close = (response == GTK_RESPONSE_OK);
850 break;
854 if (close)
855 gtk_widget_hide(ui_widgets.open_fontsel);
859 /* This shows the font selection dialog to choose a font. */
860 void dialogs_show_open_font(void)
862 if (ui_widgets.open_fontsel == NULL)
864 GtkWidget *apply_button;
866 ui_widgets.open_fontsel = gtk_font_selection_dialog_new(_("Choose font"));;
867 gtk_container_set_border_width(GTK_CONTAINER(ui_widgets.open_fontsel), 4);
868 gtk_window_set_modal(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
869 gtk_window_set_destroy_with_parent(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
870 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
871 gtk_window_set_type_hint(GTK_WINDOW(ui_widgets.open_fontsel), GDK_WINDOW_TYPE_HINT_DIALOG);
872 gtk_widget_set_name(ui_widgets.open_fontsel, "GeanyDialog");
874 apply_button = gtk_dialog_get_widget_for_response(GTK_DIALOG(ui_widgets.open_fontsel), GTK_RESPONSE_APPLY);
876 if (apply_button)
877 gtk_widget_show(apply_button);
879 g_signal_connect(ui_widgets.open_fontsel,
880 "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
881 g_signal_connect(ui_widgets.open_fontsel,
882 "response", G_CALLBACK(on_font_dialog_response), NULL);
884 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_fontsel), GTK_WINDOW(main_widgets.window));
886 gtk_font_selection_dialog_set_font_name(
887 GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel), interface_prefs.editor_font);
888 /* We make sure the dialog is visible. */
889 gtk_window_present(GTK_WINDOW(ui_widgets.open_fontsel));
893 static void
894 on_input_dialog_show(GtkDialog *dialog, GtkWidget *entry)
896 gtk_widget_grab_focus(entry);
900 static void
901 on_input_entry_activate(GtkEntry *entry, GtkDialog *dialog)
903 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
907 static void
908 on_input_numeric_activate(GtkEntry *entry, GtkDialog *dialog)
910 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
914 typedef struct
916 GtkWidget *entry;
917 GtkWidget *combo;
919 GeanyInputCallback callback;
920 gpointer data;
922 InputDialogData;
925 static void
926 on_input_dialog_response(GtkDialog *dialog, gint response, InputDialogData *data)
928 if (response == GTK_RESPONSE_ACCEPT)
930 const gchar *str = gtk_entry_get_text(GTK_ENTRY(data->entry));
932 if (data->combo != NULL)
933 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(data->combo), str, 0);
935 data->callback(str, data->data);
937 gtk_widget_hide(GTK_WIDGET(dialog));
941 /* Create and display an input dialog.
942 * persistent: whether to remember previous entry text in a combo box;
943 * in this case the dialog returned is not destroyed on a response,
944 * and can be reshown.
945 * Returns: the dialog widget. */
946 static GtkWidget *
947 dialogs_show_input_full(const gchar *title, GtkWindow *parent,
948 const gchar *label_text, const gchar *default_text,
949 gboolean persistent, GeanyInputCallback input_cb, gpointer input_cb_data,
950 GCallback insert_text_cb, gpointer insert_text_cb_data)
952 GtkWidget *dialog, *vbox;
953 InputDialogData *data = g_malloc(sizeof *data);
955 dialog = gtk_dialog_new_with_buttons(title, parent,
956 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
957 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
958 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
959 gtk_widget_set_name(dialog, "GeanyDialog");
960 gtk_box_set_spacing(GTK_BOX(vbox), 6);
962 data->combo = NULL;
963 data->entry = NULL;
964 data->callback = input_cb;
965 data->data = input_cb_data;
967 if (label_text)
969 GtkWidget *label = gtk_label_new(label_text);
970 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
971 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
972 gtk_container_add(GTK_CONTAINER(vbox), label);
975 if (persistent) /* remember previous entry text in a combo box */
977 data->combo = gtk_combo_box_text_new_with_entry();
978 data->entry = gtk_bin_get_child(GTK_BIN(data->combo));
979 ui_entry_add_clear_icon(GTK_ENTRY(data->entry));
980 gtk_container_add(GTK_CONTAINER(vbox), data->combo);
982 else
984 data->entry = gtk_entry_new();
985 ui_entry_add_clear_icon(GTK_ENTRY(data->entry));
986 gtk_container_add(GTK_CONTAINER(vbox), data->entry);
989 if (default_text != NULL)
991 gtk_entry_set_text(GTK_ENTRY(data->entry), default_text);
993 gtk_entry_set_max_length(GTK_ENTRY(data->entry), 255);
994 gtk_entry_set_width_chars(GTK_ENTRY(data->entry), 30);
996 if (insert_text_cb != NULL)
997 g_signal_connect(data->entry, "insert-text", insert_text_cb, insert_text_cb_data);
998 g_signal_connect(data->entry, "activate", G_CALLBACK(on_input_entry_activate), dialog);
999 g_signal_connect(dialog, "show", G_CALLBACK(on_input_dialog_show), data->entry);
1000 g_signal_connect_data(dialog, "response", G_CALLBACK(on_input_dialog_response), data, (GClosureNotify)g_free, 0);
1002 if (persistent)
1004 /* override default handler */
1005 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
1006 gtk_widget_show_all(dialog);
1007 return dialog;
1009 gtk_widget_show_all(dialog);
1010 gtk_dialog_run(GTK_DIALOG(dialog));
1011 gtk_widget_destroy(dialog);
1012 return NULL;
1016 /* Remember previous entry text in a combo box.
1017 * Returns: the dialog widget. */
1018 GtkWidget *
1019 dialogs_show_input_persistent(const gchar *title, GtkWindow *parent,
1020 const gchar *label_text, const gchar *default_text,
1021 GeanyInputCallback input_cb, gpointer input_cb_data)
1023 return dialogs_show_input_full(title, parent, label_text, default_text, TRUE, input_cb, input_cb_data, NULL, NULL);
1027 static void on_dialog_input(const gchar *str, gpointer data)
1029 gchar **dialog_input = data;
1030 *dialog_input = g_strdup(str);
1034 /** Asks the user for text input.
1035 * @param title Dialog title.
1036 * @param parent @nullable The currently focused window, usually @c geany->main_widgets->window.
1037 * @c NULL can be used but is discouraged due to window manager effects.
1038 * @param label_text @nullable Label text, or @c NULL.
1039 * @param default_text @nullable Text to display in the input field, or @c NULL.
1040 * @return @nullable New copy of user input or @c NULL if cancelled.
1041 * @since 0.20. */
1042 GEANY_API_SYMBOL
1043 gchar *dialogs_show_input(const gchar *title, GtkWindow *parent, const gchar *label_text,
1044 const gchar *default_text)
1046 gchar *dialog_input = NULL;
1047 dialogs_show_input_full(title, parent, label_text, default_text, FALSE, on_dialog_input, &dialog_input, NULL, NULL);
1048 return dialog_input;
1052 /* Note: could be changed to dialogs_show_validated_input with argument for callback. */
1053 /* Returns: newly allocated copy of the entry text or NULL on cancel.
1054 * Specialised variant for Goto Line dialog. */
1055 gchar *dialogs_show_input_goto_line(const gchar *title, GtkWindow *parent, const gchar *label_text,
1056 const gchar *default_text)
1058 gchar *dialog_input = NULL;
1059 dialogs_show_input_full(
1060 title, parent, label_text, default_text, FALSE, on_dialog_input, &dialog_input,
1061 G_CALLBACK(ui_editable_insert_text_callback), NULL);
1062 return dialog_input;
1067 * Shows an input box to enter a numerical value using a GtkSpinButton.
1068 * If the dialog is aborted, @a value remains untouched.
1070 * @param title The dialog title.
1071 * @param label_text The shown dialog label.
1072 * @param value The default value for the spin button and the return location of the entered value.
1073 * Must be non-NULL.
1074 * @param min Minimum allowable value (see documentation for @c gtk_spin_button_new_with_range()).
1075 * @param max Maximum allowable value (see documentation for @c gtk_spin_button_new_with_range()).
1076 * @param step Increment added or subtracted by spinning the widget
1077 * (see documentation for @c gtk_spin_button_new_with_range()).
1079 * @return @c TRUE if a value was entered and the dialog closed with 'OK'. @c FALSE otherwise.
1081 * @since 0.16
1083 GEANY_API_SYMBOL
1084 gboolean dialogs_show_input_numeric(const gchar *title, const gchar *label_text,
1085 gdouble *value, gdouble min, gdouble max, gdouble step)
1087 GtkWidget *dialog, *label, *spin, *vbox;
1088 gboolean res = FALSE;
1090 g_return_val_if_fail(title != NULL, FALSE);
1091 g_return_val_if_fail(label_text != NULL, FALSE);
1092 g_return_val_if_fail(value != NULL, FALSE);
1094 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
1095 GTK_DIALOG_DESTROY_WITH_PARENT,
1096 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1097 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
1098 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
1099 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
1100 gtk_widget_set_name(dialog, "GeanyDialog");
1102 label = gtk_label_new(label_text);
1103 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1105 spin = gtk_spin_button_new_with_range(min, max, step);
1106 ui_entry_add_clear_icon(GTK_ENTRY(spin));
1107 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), *value);
1108 g_signal_connect(spin, "activate", G_CALLBACK(on_input_numeric_activate), dialog);
1110 gtk_container_add(GTK_CONTAINER(vbox), label);
1111 gtk_container_add(GTK_CONTAINER(vbox), spin);
1112 gtk_widget_show_all(vbox);
1114 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
1116 *value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
1117 res = TRUE;
1119 gtk_widget_destroy(dialog);
1121 return res;
1125 void dialogs_show_file_properties(GeanyDocument *doc)
1127 GtkWidget *dialog, *label, *image, *check;
1128 gchar *file_size, *title, *base_name, *time_changed, *time_modified, *time_accessed, *enctext;
1129 gchar *short_name;
1130 #ifdef HAVE_SYS_TYPES_H
1131 GStatBuf st;
1132 off_t filesize;
1133 mode_t mode;
1134 gchar *locale_filename;
1135 #else
1136 gint filesize = 0;
1137 gint mode = 0;
1138 #endif
1140 /* define this ones, to avoid later trouble */
1141 #ifndef S_IRUSR
1142 # define S_IRUSR 0
1143 # define S_IWUSR 0
1144 # define S_IXUSR 0
1145 #endif
1146 #ifndef S_IRGRP
1147 # define S_IRGRP 0
1148 # define S_IWGRP 0
1149 # define S_IXGRP 0
1150 # define S_IROTH 0
1151 # define S_IWOTH 0
1152 # define S_IXOTH 0
1153 #endif
1155 g_return_if_fail(doc == NULL || doc->is_valid);
1157 if (doc == NULL || doc->file_name == NULL)
1159 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
1160 _("An error occurred or file information could not be retrieved (e.g. from a new file)."));
1161 return;
1165 #ifdef HAVE_SYS_TYPES_H
1166 locale_filename = utils_get_locale_from_utf8(doc->file_name);
1167 if (g_stat(locale_filename, &st) == 0)
1169 /* first copy the returned string and the trim it, to not modify the static glibc string
1170 * g_strchomp() is used to remove trailing EOL chars, which are there for whatever reason */
1171 time_changed = g_strchomp(g_strdup(ctime(&st.st_ctime)));
1172 time_modified = g_strchomp(g_strdup(ctime(&st.st_mtime)));
1173 time_accessed = g_strchomp(g_strdup(ctime(&st.st_atime)));
1174 filesize = st.st_size;
1175 mode = st.st_mode;
1177 else
1179 time_changed = g_strdup(_("unknown"));
1180 time_modified = g_strdup(_("unknown"));
1181 time_accessed = g_strdup(_("unknown"));
1182 filesize = (off_t) 0;
1183 mode = (mode_t) 0;
1185 g_free(locale_filename);
1186 #else
1187 time_changed = g_strdup(_("unknown"));
1188 time_modified = g_strdup(_("unknown"));
1189 time_accessed = g_strdup(_("unknown"));
1190 #endif
1192 base_name = g_path_get_basename(doc->file_name);
1193 short_name = utils_str_middle_truncate(base_name, 30);
1194 title = g_strdup_printf(_("%s Properties"), short_name);
1195 dialog = ui_builder_get_object("properties_dialog");
1196 gtk_window_set_title(GTK_WINDOW(dialog), title);
1197 g_free(short_name);
1198 g_free(title);
1199 gtk_widget_set_name(dialog, "GeanyDialog");
1201 label = ui_lookup_widget(dialog, "file_name_label");
1202 gtk_label_set_text(GTK_LABEL(label), base_name);
1204 image = ui_lookup_widget(dialog, "file_type_image");
1205 gtk_image_set_from_gicon(GTK_IMAGE(image), doc->file_type->icon,
1206 GTK_ICON_SIZE_BUTTON);
1208 label = ui_lookup_widget(dialog, "file_type_label");
1209 gtk_label_set_text(GTK_LABEL(label), doc->file_type->title);
1211 label = ui_lookup_widget(dialog, "file_size_label");
1212 file_size = utils_make_human_readable_str(filesize, 1, 0);
1213 gtk_label_set_text(GTK_LABEL(label), file_size);
1214 g_free(file_size);
1216 label = ui_lookup_widget(dialog, "file_location_label");
1217 gtk_label_set_text(GTK_LABEL(label), doc->file_name);
1219 check = ui_lookup_widget(dialog, "file_read_only_check");
1220 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), doc->readonly);
1222 label = ui_lookup_widget(dialog, "file_encoding_label");
1223 enctext = g_strdup_printf("%s %s",
1224 doc->encoding,
1225 (encodings_is_unicode_charset(doc->encoding)) ?
1226 ((doc->has_bom) ? _("(with BOM)") : _("(without BOM)")) : "");
1227 gtk_label_set_text(GTK_LABEL(label), enctext);
1228 g_free(enctext);
1230 label = ui_lookup_widget(dialog, "file_modified_label");
1231 gtk_label_set_text(GTK_LABEL(label), time_modified);
1232 label = ui_lookup_widget(dialog, "file_changed_label");
1233 gtk_label_set_text(GTK_LABEL(label), time_changed);
1234 label = ui_lookup_widget(dialog, "file_accessed_label");
1235 gtk_label_set_text(GTK_LABEL(label), time_accessed);
1237 /* permissions */
1238 check = ui_lookup_widget(dialog, "file_perm_owner_r_check");
1239 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IRUSR);
1240 check = ui_lookup_widget(dialog, "file_perm_owner_w_check");
1241 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWUSR);
1242 check = ui_lookup_widget(dialog, "file_perm_owner_x_check");
1243 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXUSR);
1244 check = ui_lookup_widget(dialog, "file_perm_group_r_check");
1245 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IRGRP);
1246 check = ui_lookup_widget(dialog, "file_perm_group_w_check");
1247 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWGRP);
1248 check = ui_lookup_widget(dialog, "file_perm_group_x_check");
1249 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXGRP);
1250 check = ui_lookup_widget(dialog, "file_perm_other_r_check");
1251 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IROTH);
1252 check = ui_lookup_widget(dialog, "file_perm_other_w_check");
1253 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWOTH);
1254 check = ui_lookup_widget(dialog, "file_perm_other_x_check");
1255 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXOTH);
1257 g_free(base_name);
1258 g_free(time_changed);
1259 g_free(time_modified);
1260 g_free(time_accessed);
1262 gtk_widget_show(dialog);
1266 /* extra_text can be NULL; otherwise it is displayed below main_text.
1267 * if parent is NULL, main_widgets.window will be used
1268 * btn_1, btn_2, btn_3 can be NULL.
1269 * returns response_1, response_2, response_3, or GTK_RESPONSE_DELETE_EVENT if the dialog was discarded */
1270 static gint show_prompt(GtkWidget *parent,
1271 const gchar *btn_1, GtkResponseType response_1,
1272 const gchar *btn_2, GtkResponseType response_2,
1273 const gchar *btn_3, GtkResponseType response_3,
1274 const gchar *question_text, const gchar *extra_text)
1276 gboolean ret = FALSE;
1277 GtkWidget *dialog;
1278 GtkWidget *btn;
1280 if (btn_2 == NULL)
1282 btn_2 = GTK_STOCK_NO;
1283 response_2 = GTK_RESPONSE_NO;
1285 if (btn_3 == NULL)
1287 btn_3 = GTK_STOCK_YES;
1288 response_3 = GTK_RESPONSE_YES;
1291 #ifdef G_OS_WIN32
1292 /* our native dialog code doesn't support custom buttons */
1293 if (utils_str_equal(btn_3, GTK_STOCK_YES) &&
1294 utils_str_equal(btn_2, GTK_STOCK_NO) && btn_1 == NULL)
1296 gchar *string = (extra_text == NULL) ? g_strdup(question_text) :
1297 g_strconcat(question_text, "\n\n", extra_text, NULL);
1299 ret = win32_message_dialog(parent, GTK_MESSAGE_QUESTION, string);
1300 ret = ret ? response_3 : response_2;
1301 g_free(string);
1302 return ret;
1304 #endif
1305 if (parent == NULL && main_status.main_window_realized)
1306 parent = main_widgets.window;
1308 dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
1309 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
1310 GTK_BUTTONS_NONE, "%s", question_text);
1311 gtk_widget_set_name(dialog, "GeanyDialog");
1312 gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
1313 gtk_window_set_icon_name(GTK_WINDOW(dialog), "geany");
1315 /* question_text will be in bold if optional extra_text used */
1316 if (extra_text != NULL)
1317 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
1318 "%s", extra_text);
1320 if (btn_1 != NULL)
1321 gtk_dialog_add_button(GTK_DIALOG(dialog), btn_1, response_1);
1323 btn = gtk_dialog_add_button(GTK_DIALOG(dialog), btn_2, response_2);
1324 /* we don't want a default, but we need to override the apply button as default */
1325 gtk_widget_grab_default(btn);
1326 gtk_dialog_add_button(GTK_DIALOG(dialog), btn_3, response_3);
1328 ret = gtk_dialog_run(GTK_DIALOG(dialog));
1329 gtk_widget_destroy(dialog);
1331 return ret;
1336 * Shows a question message box with @a text and Yes/No buttons.
1337 * On Unix-like systems a GTK message dialog box is shown, on Win32 systems a native Windows
1338 * message dialog box is shown.
1340 * @param text Printf()-style format string.
1341 * @param ... Arguments for the @a text format string.
1343 * @return @c TRUE if the user answered with Yes, otherwise @c FALSE.
1345 GEANY_API_SYMBOL
1346 gboolean dialogs_show_question(const gchar *text, ...)
1348 gchar *string;
1349 va_list args;
1350 GtkWidget *parent = (main_status.main_window_realized) ? main_widgets.window : NULL;
1351 gint result;
1353 va_start(args, text);
1354 string = g_strdup_vprintf(text, args);
1355 va_end(args);
1356 result = show_prompt(parent,
1357 NULL, GTK_RESPONSE_NONE,
1358 GTK_STOCK_NO, GTK_RESPONSE_NO,
1359 GTK_STOCK_YES, GTK_RESPONSE_YES,
1360 string, NULL);
1361 g_free(string);
1362 return (result == GTK_RESPONSE_YES);
1366 /* extra_text can be NULL; otherwise it is displayed below main_text.
1367 * if parent is NULL, main_widgets.window will be used
1368 * yes_btn, no_btn can be NULL. */
1369 gboolean dialogs_show_question_full(GtkWidget *parent, const gchar *yes_btn, const gchar *no_btn,
1370 const gchar *extra_text, const gchar *main_text, ...)
1372 gint result;
1373 gchar *string;
1374 va_list args;
1376 va_start(args, main_text);
1377 string = g_strdup_vprintf(main_text, args);
1378 va_end(args);
1379 result = show_prompt(parent,
1380 NULL, GTK_RESPONSE_NONE,
1381 no_btn, GTK_RESPONSE_NO,
1382 yes_btn, GTK_RESPONSE_YES,
1383 string, extra_text);
1384 g_free(string);
1385 return (result == GTK_RESPONSE_YES);
1389 /* extra_text can be NULL; otherwise it is displayed below main_text.
1390 * if parent is NULL, main_widgets.window will be used
1391 * btn_1, btn_2, btn_3 can be NULL.
1392 * returns response_1, response_2 or response_3 */
1393 gint dialogs_show_prompt(GtkWidget *parent,
1394 const gchar *btn_1, GtkResponseType response_1,
1395 const gchar *btn_2, GtkResponseType response_2,
1396 const gchar *btn_3, GtkResponseType response_3,
1397 const gchar *extra_text, const gchar *main_text, ...)
1399 gchar *string;
1400 va_list args;
1401 gint result;
1403 va_start(args, main_text);
1404 string = g_strdup_vprintf(main_text, args);
1405 va_end(args);
1406 result = show_prompt(parent, btn_1, response_1, btn_2, response_2, btn_3, response_3,
1407 string, extra_text);
1408 g_free(string);
1409 return result;