Fix 'null' listed in both primary and secondary keyword lists.
[geany-mirror.git] / src / dialogs.c
blob5067e56370f0306799ced3990818f041ce4280a0
1 /*
2 * dialogs.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
25 * File related dialogs, miscellaneous dialogs, font dialog.
28 #include "geany.h"
30 #include <gdk/gdkkeysyms.h>
31 #include <string.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #include <time.h>
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
42 /* gstdio.h also includes sys/stat.h */
43 #include <glib/gstdio.h>
45 #include "dialogs.h"
47 #include "callbacks.h"
48 #include "document.h"
49 #include "filetypes.h"
50 #include "win32.h"
51 #include "sciwrappers.h"
52 #include "support.h"
53 #include "utils.h"
54 #include "ui_utils.h"
55 #include "keybindings.h"
56 #include "encodings.h"
57 #include "build.h"
58 #include "main.h"
59 #include "project.h"
62 enum
64 GEANY_RESPONSE_RENAME,
65 GEANY_RESPONSE_VIEW
69 static gboolean handle_save_as(const gchar *utf8_filename, gboolean open_new_tab,
70 gboolean rename_file);
72 #if ! GEANY_USE_WIN32_DIALOG
73 static GtkWidget *add_file_open_extra_widget(void);
76 static void
77 on_file_open_dialog_response (GtkDialog *dialog,
78 gint response,
79 gpointer user_data)
81 gtk_widget_hide(ui_widgets.open_filesel);
83 if (response == GTK_RESPONSE_ACCEPT || response == GEANY_RESPONSE_VIEW)
85 GSList *filelist;
86 gint filetype_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(
87 ui_lookup_widget(GTK_WIDGET(dialog), "filetype_combo")));
88 gint encoding_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(
89 ui_lookup_widget(GTK_WIDGET(dialog), "encoding_combo")));
90 GeanyFiletype *ft = NULL;
91 const gchar *charset = NULL;
92 gboolean ro = (response == GEANY_RESPONSE_VIEW); /* View clicked */
94 /* ignore detect from file item */
95 if (filetype_idx > 0)
96 ft = g_slist_nth_data(filetypes_by_title, filetype_idx);
97 if (encoding_idx >= 0 && encoding_idx < GEANY_ENCODINGS_MAX)
98 charset = encodings[encoding_idx].charset;
100 filelist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(ui_widgets.open_filesel));
101 if (filelist != NULL)
103 document_open_files(filelist, ro, ft, charset);
104 g_slist_foreach(filelist, (GFunc) g_free, NULL); /* free filenames */
106 g_slist_free(filelist);
108 if (app->project && NZV(app->project->base_path))
109 gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(ui_widgets.open_filesel),
110 app->project->base_path, NULL);
114 static void on_file_open_notify(GObject *filechooser, GParamSpec *pspec, gpointer data)
116 GValue *value;
118 value = g_new0(GValue, 1);
119 g_value_init(value, pspec->value_type);
120 g_object_get_property(filechooser, pspec->name, value);
122 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
123 ui_lookup_widget(GTK_WIDGET(filechooser), "check_hidden")), g_value_get_boolean(value));
127 static void
128 on_file_open_check_hidden_toggled(GtkToggleButton *togglebutton, gpointer user_data)
130 gboolean is_on = gtk_toggle_button_get_active(togglebutton);
132 gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(ui_widgets.open_filesel), is_on);
136 static void create_open_file_dialog(void)
138 GtkWidget *filetype_combo, *encoding_combo;
139 GtkWidget *viewbtn;
140 guint i;
141 gchar *encoding_string;
142 GSList *node;
144 ui_widgets.open_filesel = gtk_file_chooser_dialog_new(_("Open File"), GTK_WINDOW(main_widgets.window),
145 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
146 gtk_widget_set_name(ui_widgets.open_filesel, "GeanyDialog");
148 viewbtn = gtk_dialog_add_button(GTK_DIALOG(ui_widgets.open_filesel), _("_View"),
149 GEANY_RESPONSE_VIEW);
150 ui_widget_set_tooltip_text(viewbtn,
151 _("Opens the file in read-only mode. If you choose more than one file to open, all files will be opened read-only."));
153 gtk_dialog_add_buttons(GTK_DIALOG(ui_widgets.open_filesel),
154 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
155 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
156 gtk_dialog_set_default_response(GTK_DIALOG(ui_widgets.open_filesel), GTK_RESPONSE_ACCEPT);
158 gtk_widget_set_size_request(ui_widgets.open_filesel, -1, 460);
159 gtk_window_set_modal(GTK_WINDOW(ui_widgets.open_filesel), TRUE);
160 gtk_window_set_destroy_with_parent(GTK_WINDOW(ui_widgets.open_filesel), TRUE);
161 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(ui_widgets.open_filesel), FALSE);
162 gtk_window_set_type_hint(GTK_WINDOW(ui_widgets.open_filesel), GDK_WINDOW_TYPE_HINT_DIALOG);
163 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_filesel), GTK_WINDOW(main_widgets.window));
164 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(ui_widgets.open_filesel), TRUE);
165 if (gtk_check_version(2, 14, 0) == NULL)
166 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(ui_widgets.open_filesel), FALSE);
168 /* add checkboxes and filename entry */
169 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(ui_widgets.open_filesel),
170 add_file_open_extra_widget());
171 filetype_combo = ui_lookup_widget(ui_widgets.open_filesel, "filetype_combo");
173 gtk_combo_box_append_text(GTK_COMBO_BOX(filetype_combo), _("Detect by file extension"));
174 /* add FileFilters(start with "All Files") */
175 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel),
176 filetypes_create_file_filter(filetypes[GEANY_FILETYPES_NONE]));
177 /* now create meta filter "All Source" */
178 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel),
179 filetypes_create_file_filter_all_source());
180 foreach_slist(node, filetypes_by_title)
182 GeanyFiletype *ft = node->data;
184 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
185 continue;
186 gtk_combo_box_append_text(GTK_COMBO_BOX(filetype_combo), ft->title);
187 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(ui_widgets.open_filesel),
188 filetypes_create_file_filter(ft));
190 gtk_combo_box_set_active(GTK_COMBO_BOX(filetype_combo), 0);
192 /* fill encoding combo box */
193 encoding_combo = ui_lookup_widget(ui_widgets.open_filesel, "encoding_combo");
194 for (i = 0; i < GEANY_ENCODINGS_MAX; i++)
196 encoding_string = encodings_to_string(&encodings[i]);
197 gtk_combo_box_append_text(GTK_COMBO_BOX(encoding_combo), encoding_string);
198 g_free(encoding_string);
200 gtk_combo_box_append_text(GTK_COMBO_BOX(encoding_combo), _("Detect from file"));
201 gtk_combo_box_set_active(GTK_COMBO_BOX(encoding_combo), GEANY_ENCODINGS_MAX);
203 g_signal_connect(ui_widgets.open_filesel, "notify::show-hidden",
204 G_CALLBACK(on_file_open_notify), NULL);
205 g_signal_connect(ui_widgets.open_filesel, "delete-event",
206 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
207 g_signal_connect(ui_widgets.open_filesel, "response",
208 G_CALLBACK(on_file_open_dialog_response), NULL);
210 #endif
213 /* This shows the file selection dialog to open a file. */
214 void dialogs_show_open_file()
216 gchar *initdir;
218 /* set dialog directory to the current file's directory, if present */
219 initdir = utils_get_current_file_dir_utf8();
221 /* use project or default startup directory (if set) if no files are open */
222 /** TODO should it only be used when initally open the dialog and not on every show? */
223 if (! initdir)
224 initdir = g_strdup(utils_get_default_dir_utf8());
226 setptr(initdir, utils_get_locale_from_utf8(initdir));
228 #if GEANY_USE_WIN32_DIALOG
229 win32_show_document_open_dialog(TRUE, initdir);
230 #else /* X11, not win32: use GTK_FILE_CHOOSER */
232 /* We use the same file selection widget each time, so first of all we create it
233 * if it hasn't already been created. */
234 if (ui_widgets.open_filesel == NULL)
235 create_open_file_dialog();
237 if (initdir != NULL)
239 if (g_path_is_absolute(initdir))
240 gtk_file_chooser_set_current_folder(
241 GTK_FILE_CHOOSER(ui_widgets.open_filesel), initdir);
244 if (app->project && NZV(app->project->base_path))
245 gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(ui_widgets.open_filesel),
246 app->project->base_path, NULL);
248 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(ui_widgets.open_filesel));
249 gtk_window_present(GTK_WINDOW(ui_widgets.open_filesel));
250 #endif
251 g_free(initdir);
255 #if ! GEANY_USE_WIN32_DIALOG
256 static GtkWidget *add_file_open_extra_widget()
258 GtkWidget *expander, *vbox, *table, *check_hidden;
259 GtkWidget *filetype_ebox, *filetype_label, *filetype_combo;
260 GtkWidget *encoding_ebox, *encoding_label, *encoding_combo;
262 expander = gtk_expander_new_with_mnemonic(_("_More Options"));
263 vbox = gtk_vbox_new(FALSE, 6);
264 gtk_container_add(GTK_CONTAINER(expander), vbox);
266 table = gtk_table_new(2, 4, FALSE);
268 /* line 1 with checkbox and encoding combo */
269 check_hidden = gtk_check_button_new_with_mnemonic(_("Show _hidden files"));
270 gtk_widget_show(check_hidden);
271 gtk_table_attach(GTK_TABLE(table), check_hidden, 0, 1, 0, 1,
272 (GtkAttachOptions) (GTK_FILL | GTK_EXPAND),
273 (GtkAttachOptions) (0), 0, 5);
275 /* spacing */
276 gtk_table_attach(GTK_TABLE(table), gtk_label_new(""), 1, 2, 0, 1,
277 (GtkAttachOptions) (GTK_FILL),
278 (GtkAttachOptions) (0), 5, 5);
280 encoding_label = gtk_label_new(_("Set encoding:"));
281 gtk_misc_set_alignment(GTK_MISC(encoding_label), 1, 0);
282 gtk_table_attach(GTK_TABLE(table), encoding_label, 2, 3, 0, 1,
283 (GtkAttachOptions) (GTK_FILL),
284 (GtkAttachOptions) (0), 4, 5);
285 /* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
286 encoding_ebox = gtk_event_box_new();
287 encoding_combo = gtk_combo_box_new_text();
288 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(encoding_combo), 3);
289 ui_widget_set_tooltip_text(encoding_ebox,
290 _("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."));
291 gtk_container_add(GTK_CONTAINER(encoding_ebox), encoding_combo);
292 gtk_table_attach(GTK_TABLE(table), encoding_ebox, 3, 4, 0, 1,
293 (GtkAttachOptions) (GTK_FILL),
294 (GtkAttachOptions) (0), 0, 5);
296 /* line 2 with filetype combo */
297 filetype_label = gtk_label_new(_("Set filetype:"));
298 gtk_misc_set_alignment(GTK_MISC(filetype_label), 1, 0);
299 gtk_table_attach(GTK_TABLE(table), filetype_label, 2, 3, 1, 2,
300 (GtkAttachOptions) (GTK_FILL),
301 (GtkAttachOptions) (0), 4, 5);
302 /* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
303 filetype_ebox = gtk_event_box_new();
304 filetype_combo = gtk_combo_box_new_text();
305 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(filetype_combo), 2);
306 ui_widget_set_tooltip_text(filetype_ebox,
307 _("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."));
308 gtk_container_add(GTK_CONTAINER(filetype_ebox), filetype_combo);
309 gtk_table_attach(GTK_TABLE(table), filetype_ebox, 3, 4, 1, 2,
310 (GtkAttachOptions) (GTK_FILL),
311 (GtkAttachOptions) (0), 0, 5);
313 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
314 gtk_widget_show_all(vbox);
316 g_signal_connect(check_hidden, "toggled",
317 G_CALLBACK(on_file_open_check_hidden_toggled), NULL);
319 g_object_set_data_full(G_OBJECT(ui_widgets.open_filesel), "check_hidden",
320 g_object_ref(check_hidden), (GDestroyNotify)g_object_unref);
321 g_object_set_data_full(G_OBJECT(ui_widgets.open_filesel), "filetype_combo",
322 g_object_ref(filetype_combo), (GDestroyNotify)g_object_unref);
323 g_object_set_data_full(G_OBJECT(ui_widgets.open_filesel), "encoding_combo",
324 g_object_ref(encoding_combo), (GDestroyNotify)g_object_unref);
326 return expander;
328 #endif
331 #if ! GEANY_USE_WIN32_DIALOG
332 static void on_save_as_new_tab_toggled(GtkToggleButton *togglebutton, gpointer user_data)
334 gtk_widget_set_sensitive(GTK_WIDGET(user_data),
335 ! gtk_toggle_button_get_active(togglebutton));
337 #endif
340 static gboolean handle_save_as(const gchar *utf8_filename, gboolean open_new_tab, gboolean rename_file)
342 GeanyDocument *doc = document_get_current();
343 gboolean success = FALSE;
345 g_return_val_if_fail(NZV(utf8_filename), FALSE);
347 if (open_new_tab)
348 { /* "open" the saved file in a new tab and switch to it */
349 doc = document_clone(doc, utf8_filename);
350 success = document_save_file_as(doc, NULL);
352 else
354 if (doc->file_name != NULL)
356 if (rename_file)
358 document_rename_file(doc, utf8_filename);
360 /* create a new tm_source_file object otherwise tagmanager won't work correctly */
361 tm_workspace_remove_object(doc->tm_file, TRUE, TRUE);
362 doc->tm_file = NULL;
364 success = document_save_file_as(doc, utf8_filename);
366 build_menu_update(doc);
368 return success;
372 #if ! GEANY_USE_WIN32_DIALOG
373 static void
374 on_file_save_dialog_response (GtkDialog *dialog,
375 gint response,
376 gpointer user_data)
378 gboolean rename_file = FALSE;
379 gboolean success = FALSE;
380 gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ui_widgets.save_filesel));
382 switch (response)
384 case GEANY_RESPONSE_RENAME:
385 /* rename doesn't check for empty filename or overwriting */
386 if (! NZV(new_filename))
388 utils_beep();
389 break;
391 if (g_file_test(new_filename, G_FILE_TEST_EXISTS) &&
392 !dialogs_show_question_full(NULL, NULL, NULL,
393 _("Overwrite?"),
394 _("Filename already exists!")))
395 break;
396 rename_file = TRUE;
397 /* fall through */
398 case GTK_RESPONSE_ACCEPT:
400 gboolean open_new_tab = gtk_toggle_button_get_active(
401 GTK_TOGGLE_BUTTON(ui_lookup_widget(ui_widgets.save_filesel, "check_open_new_tab")));
402 gchar *utf8_filename;
404 utf8_filename = utils_get_utf8_from_locale(new_filename);
405 success = handle_save_as(utf8_filename, open_new_tab, rename_file);
407 g_free(utf8_filename);
408 break;
410 case GTK_RESPONSE_CANCEL:
411 success = TRUE;
412 break;
414 g_free(new_filename);
416 if (success)
417 gtk_widget_hide(ui_widgets.save_filesel);
419 #endif
422 #if ! GEANY_USE_WIN32_DIALOG
423 static void create_save_file_dialog(void)
425 GtkWidget *vbox, *check_open_new_tab, *rename_btn;
426 const gchar *initdir;
428 ui_widgets.save_filesel = gtk_file_chooser_dialog_new(_("Save File"), GTK_WINDOW(main_widgets.window),
429 GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
430 gtk_window_set_modal(GTK_WINDOW(ui_widgets.save_filesel), TRUE);
431 gtk_window_set_destroy_with_parent(GTK_WINDOW(ui_widgets.save_filesel), TRUE);
432 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(ui_widgets.save_filesel), FALSE);
433 gtk_window_set_type_hint(GTK_WINDOW(ui_widgets.save_filesel), GDK_WINDOW_TYPE_HINT_DIALOG);
434 gtk_widget_set_name(ui_widgets.save_filesel, "GeanyDialog");
436 rename_btn = gtk_dialog_add_button(GTK_DIALOG(ui_widgets.save_filesel), _("R_ename"),
437 GEANY_RESPONSE_RENAME);
438 ui_widget_set_tooltip_text(rename_btn, _("Save the file and rename it"));
440 gtk_dialog_add_buttons(GTK_DIALOG(ui_widgets.save_filesel),
441 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
442 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
443 gtk_dialog_set_default_response(GTK_DIALOG(ui_widgets.save_filesel), GTK_RESPONSE_ACCEPT);
445 vbox = gtk_vbox_new(FALSE, 0);
446 check_open_new_tab = gtk_check_button_new_with_mnemonic(_("_Open file in a new tab"));
447 ui_widget_set_tooltip_text(check_open_new_tab,
448 _("Keep the current unsaved document open"
449 " and open the newly saved file in a new tab"));
450 gtk_box_pack_start(GTK_BOX(vbox), check_open_new_tab, FALSE, FALSE, 0);
451 gtk_widget_show_all(vbox);
452 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(ui_widgets.save_filesel), vbox);
453 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(ui_widgets.save_filesel), TRUE);
454 if (gtk_check_version(2, 14, 0) == NULL)
455 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(ui_widgets.save_filesel), FALSE);
457 /* set the folder by default to the project base dir or the global pref for opening files */
458 initdir = utils_get_default_dir_utf8();
459 if (initdir)
461 gchar *linitdir = utils_get_locale_from_utf8(initdir);
462 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(ui_widgets.save_filesel), linitdir);
463 g_free(linitdir);
466 g_signal_connect(check_open_new_tab, "toggled",
467 G_CALLBACK(on_save_as_new_tab_toggled), rename_btn);
469 g_object_set_data_full(G_OBJECT(ui_widgets.save_filesel), "check_open_new_tab",
470 g_object_ref(check_open_new_tab), (GDestroyNotify)g_object_unref);
472 g_signal_connect(ui_widgets.save_filesel, "delete-event",
473 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
474 g_signal_connect(ui_widgets.save_filesel, "response",
475 G_CALLBACK(on_file_save_dialog_response), NULL);
477 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.save_filesel), GTK_WINDOW(main_widgets.window));
479 #endif
482 #if ! GEANY_USE_WIN32_DIALOG
483 static gboolean gtk_show_save_as(void)
485 GeanyDocument *doc = document_get_current();
486 gint resp;
488 g_return_val_if_fail(doc != NULL, FALSE);
490 if (G_UNLIKELY(ui_widgets.save_filesel == NULL))
491 create_save_file_dialog();
493 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(ui_widgets.save_filesel));
495 if (doc->file_name != NULL)
497 if (g_path_is_absolute(doc->file_name))
499 gchar *locale_filename = utils_get_locale_from_utf8(doc->file_name);
500 gchar *locale_basename = g_path_get_basename(locale_filename);
501 gchar *locale_dirname = g_path_get_dirname(locale_filename);
503 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(ui_widgets.save_filesel),
504 locale_dirname);
505 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(ui_widgets.save_filesel),
506 locale_basename);
508 g_free(locale_filename);
509 g_free(locale_basename);
510 g_free(locale_dirname);
512 else
513 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(ui_widgets.save_filesel),
514 doc->file_name);
516 else
518 gchar *fname = NULL;
520 if (doc->file_type != NULL && doc->file_type->extension != NULL)
521 fname = g_strconcat(GEANY_STRING_UNTITLED, ".",
522 doc->file_type->extension, NULL);
523 else
524 fname = g_strdup(GEANY_STRING_UNTITLED);
526 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(ui_widgets.save_filesel), fname);
528 g_free(fname);
531 if (app->project && NZV(app->project->base_path))
532 gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(ui_widgets.save_filesel),
533 app->project->base_path, NULL);
535 /* Run the dialog synchronously, pausing this function call */
536 resp = gtk_dialog_run(GTK_DIALOG(ui_widgets.save_filesel));
538 if (app->project && NZV(app->project->base_path))
539 gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(ui_widgets.save_filesel),
540 app->project->base_path, NULL);
542 return (resp == GTK_RESPONSE_ACCEPT);
544 #endif
548 * Shows the Save As dialog for the current notebook page.
550 * @return @c TRUE if the file was saved, otherwise @c FALSE.
552 gboolean dialogs_show_save_as()
554 gboolean result;
556 #if GEANY_USE_WIN32_DIALOG
557 GeanyDocument *doc = document_get_current();
558 gchar *utf8_name = win32_show_document_save_as_dialog(GTK_WINDOW(main_widgets.window),
559 _("Save File"), DOC_FILENAME(doc));
560 result = handle_save_as(utf8_name, FALSE, FALSE);
561 #else
562 result = gtk_show_save_as();
563 #endif
564 return result;
568 #ifndef G_OS_WIN32
569 static void show_msgbox_dialog(GtkWidget *dialog, GtkMessageType type, GtkWindow *parent)
571 const gchar *title;
572 switch (type)
574 case GTK_MESSAGE_ERROR:
575 title = _("Error");
576 break;
577 case GTK_MESSAGE_QUESTION:
578 title = _("Question");
579 break;
580 case GTK_MESSAGE_WARNING:
581 title = _("Warning");
582 break;
583 default:
584 title = _("Information");
585 break;
587 gtk_window_set_title(GTK_WINDOW(dialog), title);
588 if (parent == NULL || GTK_IS_DIALOG(parent))
590 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
591 gtk_window_set_icon(GTK_WINDOW(dialog), pb);
592 g_object_unref(pb);
594 gtk_widget_set_name(dialog, "GeanyDialog");
596 gtk_dialog_run(GTK_DIALOG(dialog));
597 gtk_widget_destroy(dialog);
599 #endif
603 * Shows a message box of the type @a type with @a text.
604 * On Unix-like systems a GTK message dialog box is shown, on Win32 systems a native Windows
605 * message dialog box is shown.
607 * @param type A @c GtkMessageType, e.g. @c GTK_MESSAGE_INFO, @c GTK_MESSAGE_WARNING,
608 * @c GTK_MESSAGE_QUESTION, @c GTK_MESSAGE_ERROR.
609 * @param text Printf()-style format string.
610 * @param ... Arguments for the @a text format string.
612 void dialogs_show_msgbox(GtkMessageType type, const gchar *text, ...)
614 #ifndef G_OS_WIN32
615 GtkWidget *dialog;
616 #endif
617 gchar *string;
618 va_list args;
619 GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
621 va_start(args, text);
622 string = g_strdup_vprintf(text, args);
623 va_end(args);
625 #ifdef G_OS_WIN32
626 win32_message_dialog(GTK_WIDGET(parent), type, string);
627 #else
628 dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
629 type, GTK_BUTTONS_OK, "%s", string);
630 show_msgbox_dialog(dialog, type, parent);
631 #endif
632 g_free(string);
636 void dialogs_show_msgbox_with_secondary(GtkMessageType type, const gchar *text, const gchar *secondary)
638 GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
639 #ifdef G_OS_WIN32
640 /* put the two strings together because Windows message boxes don't support secondary texts */
641 gchar *string = g_strconcat(text, "\n", secondary, NULL);
642 win32_message_dialog(GTK_WIDGET(parent), type, string);
643 g_free(string);
644 #else
645 GtkWidget *dialog;
646 dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
647 type, GTK_BUTTONS_OK, "%s", text);
648 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", secondary);
649 show_msgbox_dialog(dialog, type, parent);
650 #endif
654 #ifndef G_OS_WIN32
655 static gint run_unsaved_dialog(const gchar *msg, const gchar *msg2)
657 GtkWidget *dialog, *button;
658 gint ret;
660 dialog = gtk_message_dialog_new(GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
661 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", msg);
662 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", msg2);
663 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
665 button = ui_button_new_with_image(GTK_STOCK_CLEAR, _("_Don't save"));
666 gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button, GTK_RESPONSE_NO);
667 gtk_widget_show(button);
669 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
671 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
672 ret = gtk_dialog_run(GTK_DIALOG(dialog));
674 gtk_widget_destroy(dialog);
676 return ret;
678 #endif
681 gboolean dialogs_show_unsaved_file(GeanyDocument *doc)
683 gchar *msg, *short_fn = NULL;
684 const gchar *msg2;
685 gint ret;
686 gboolean old_quitting_state = main_status.quitting;
688 /* display the file tab to remind the user of the document */
689 main_status.quitting = FALSE;
690 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
691 document_get_notebook_page(doc));
692 main_status.quitting = old_quitting_state;
694 short_fn = document_get_basename_for_display(doc, -1);
696 msg = g_strdup_printf(_("The file '%s' is not saved."),
697 (short_fn != NULL) ? short_fn : GEANY_STRING_UNTITLED);
698 msg2 = _("Do you want to save it before closing?");
699 g_free(short_fn);
701 #ifdef G_OS_WIN32
702 setptr(msg, g_strconcat(msg, "\n", msg2, NULL));
703 ret = win32_message_dialog_unsaved(msg);
704 #else
705 ret = run_unsaved_dialog(msg, msg2);
706 #endif
707 g_free(msg);
709 switch (ret)
711 case GTK_RESPONSE_YES:
713 if (document_need_save_as(doc))
715 ret = dialogs_show_save_as();
717 else
718 /* document_save_file() returns the status if the file could be saved */
719 ret = document_save_file(doc, FALSE);
720 break;
722 case GTK_RESPONSE_NO: ret = TRUE; break;
723 case GTK_RESPONSE_CANCEL: /* fall through to default and leave the function */
724 default: ret = FALSE; break;
727 return (gboolean) ret;
731 #ifndef G_OS_WIN32
732 static void
733 on_font_apply_button_clicked (GtkButton *button,
734 gpointer user_data)
736 gchar *fontname;
738 fontname = gtk_font_selection_dialog_get_font_name(
739 GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel));
740 ui_set_editor_font(fontname);
741 g_free(fontname);
745 static void
746 on_font_ok_button_clicked (GtkButton *button,
747 gpointer user_data)
749 /* We do the same thing as apply, but we close the dialog after. */
750 on_font_apply_button_clicked(button, NULL);
751 gtk_widget_hide(ui_widgets.open_fontsel);
755 static void
756 on_font_cancel_button_clicked (GtkButton *button,
757 gpointer user_data)
759 gtk_widget_hide(ui_widgets.open_fontsel);
761 #endif
764 /* This shows the font selection dialog to choose a font. */
765 void dialogs_show_open_font()
767 #ifdef G_OS_WIN32
768 win32_show_font_dialog();
769 #else
771 if (ui_widgets.open_fontsel == NULL)
773 ui_widgets.open_fontsel = gtk_font_selection_dialog_new(_("Choose font"));;
774 gtk_container_set_border_width(GTK_CONTAINER(ui_widgets.open_fontsel), 4);
775 gtk_window_set_modal(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
776 gtk_window_set_destroy_with_parent(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
777 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
778 gtk_window_set_type_hint(GTK_WINDOW(ui_widgets.open_fontsel), GDK_WINDOW_TYPE_HINT_DIALOG);
779 gtk_widget_set_name(ui_widgets.open_fontsel, "GeanyDialog");
781 gtk_widget_show(GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel)->apply_button);
783 g_signal_connect(ui_widgets.open_fontsel,
784 "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
785 g_signal_connect(GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel)->ok_button,
786 "clicked", G_CALLBACK(on_font_ok_button_clicked), NULL);
787 g_signal_connect(GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel)->cancel_button,
788 "clicked", G_CALLBACK(on_font_cancel_button_clicked), NULL);
789 g_signal_connect(GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel)->apply_button,
790 "clicked", G_CALLBACK(on_font_apply_button_clicked), NULL);
792 gtk_font_selection_dialog_set_font_name(
793 GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel), interface_prefs.editor_font);
794 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_fontsel), GTK_WINDOW(main_widgets.window));
796 /* We make sure the dialog is visible. */
797 gtk_window_present(GTK_WINDOW(ui_widgets.open_fontsel));
798 #endif
802 static void
803 on_input_dialog_show(GtkDialog *dialog, GtkWidget *entry)
805 gtk_widget_grab_focus(entry);
809 static void
810 on_input_entry_activate(GtkEntry *entry, GtkDialog *dialog)
812 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
816 static void
817 on_input_numeric_activate(GtkEntry *entry, GtkDialog *dialog)
819 gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
823 static void
824 on_input_dialog_response(GtkDialog *dialog,
825 gint response,
826 GtkWidget *entry)
828 gboolean persistent = (gboolean) GPOINTER_TO_INT(g_object_get_data(G_OBJECT(dialog), "has_combo"));
830 if (response == GTK_RESPONSE_ACCEPT)
832 const gchar *str = gtk_entry_get_text(GTK_ENTRY(entry));
833 GeanyInputCallback input_cb =
834 (GeanyInputCallback) g_object_get_data(G_OBJECT(dialog), "input_cb");
836 if (persistent)
838 GtkWidget *combo = (GtkWidget *) g_object_get_data(G_OBJECT(dialog), "combo");
839 ui_combo_box_add_to_history(GTK_COMBO_BOX(combo), str);
841 input_cb(str);
843 gtk_widget_hide(GTK_WIDGET(dialog));
847 static void add_input_widgets(GtkWidget *dialog, GtkWidget *vbox,
848 const gchar *label_text, const gchar *default_text, gboolean persistent,
849 GCallback insert_text_cb)
851 GtkWidget *entry;
853 if (label_text)
855 GtkWidget *label = gtk_label_new(label_text);
856 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
857 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
858 gtk_container_add(GTK_CONTAINER(vbox), label);
861 if (persistent) /* remember previous entry text in a combo box */
863 GtkWidget *combo = gtk_combo_box_entry_new_text();
865 entry = GTK_BIN(combo)->child;
866 ui_entry_add_clear_icon(GTK_ENTRY(entry));
867 g_object_set_data(G_OBJECT(dialog), "combo", combo);
868 gtk_container_add(GTK_CONTAINER(vbox), combo);
870 else
872 entry = gtk_entry_new();
873 ui_entry_add_clear_icon(GTK_ENTRY(entry));
874 gtk_container_add(GTK_CONTAINER(vbox), entry);
877 if (default_text != NULL)
879 gtk_entry_set_text(GTK_ENTRY(entry), default_text);
881 gtk_entry_set_max_length(GTK_ENTRY(entry), 255);
882 gtk_entry_set_width_chars(GTK_ENTRY(entry), 30);
884 if (insert_text_cb != NULL)
885 g_signal_connect(entry, "insert-text", insert_text_cb, NULL);
886 g_signal_connect(entry, "activate", G_CALLBACK(on_input_entry_activate), dialog);
887 g_signal_connect(dialog, "show", G_CALLBACK(on_input_dialog_show), entry);
888 g_signal_connect(dialog, "response", G_CALLBACK(on_input_dialog_response), entry);
892 /* Create and display an input dialog.
893 * persistent: whether to remember previous entry text in a combo box;
894 * in this case the dialog returned is not destroyed on a response,
895 * and can be reshown.
896 * Returns: the dialog widget. */
897 static GtkWidget *
898 dialogs_show_input_full(const gchar *title, const gchar *label_text, const gchar *default_text,
899 gboolean persistent, GeanyInputCallback input_cb, GCallback insert_text_cb)
901 GtkWidget *dialog, *vbox;
903 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
904 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
905 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
906 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
907 gtk_widget_set_name(dialog, "GeanyDialog");
908 gtk_box_set_spacing(GTK_BOX(vbox), 6);
910 g_object_set_data(G_OBJECT(dialog), "has_combo", GINT_TO_POINTER(persistent));
911 g_object_set_data(G_OBJECT(dialog), "input_cb", (gpointer) input_cb);
913 add_input_widgets(dialog, vbox, label_text, default_text, persistent, insert_text_cb);
915 if (persistent)
917 /* override default handler */
918 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
919 gtk_widget_show_all(dialog);
920 return dialog;
922 gtk_widget_show_all(dialog);
923 gtk_dialog_run(GTK_DIALOG(dialog));
924 gtk_widget_destroy(dialog);
925 return NULL;
929 /* Remember previous entry text in a combo box.
930 * Returns: the dialog widget. */
931 GtkWidget *
932 dialogs_show_input_persistent(const gchar *title, const gchar *label_text, const gchar *default_text,
933 GeanyInputCallback input_cb)
935 return dialogs_show_input_full(title, label_text, default_text, TRUE, input_cb, NULL);
939 /* ugly hack - user_data not supported for callback */
940 static gchar *dialog_input = NULL;
942 static void on_dialog_input(const gchar *str)
944 dialog_input = g_strdup(str);
948 /* Returns: newly allocated string - a copy of either the entry text or default_text. */
949 gchar *dialogs_show_input(const gchar *title, const gchar *label_text,
950 const gchar *default_text)
952 dialog_input = NULL;
953 dialogs_show_input_full(title, label_text, default_text, FALSE, on_dialog_input, NULL);
954 return NVL(dialog_input, g_strdup(default_text));
958 /* Returns: newly allocated copy of the entry text or NULL on cancel.
959 * Specialised variant for Goto Line dialog. */
960 gchar *dialogs_show_input_goto_line(const gchar *title, const gchar *label_text,
961 const gchar *default_text)
963 dialog_input = NULL;
964 dialogs_show_input_full(
965 title, label_text, default_text, FALSE, on_dialog_input,
966 G_CALLBACK(ui_editable_insert_text_callback));
967 return dialog_input;
972 * Shows an input box to enter a numerical value using a GtkSpinButton.
973 * If the dialog is aborted, @a value remains untouched.
975 * @param title The dialog title.
976 * @param label_text The shown dialog label.
977 * @param value The default value for the spin button and the return location of the entered value.
978 * Must be non-NULL.
979 * @param min Minimum allowable value (see documentation for @c gtk_spin_button_new_with_range()).
980 * @param max Maximum allowable value (see documentation for @c gtk_spin_button_new_with_range()).
981 * @param step Increment added or subtracted by spinning the widget
982 * (see documentation for @c gtk_spin_button_new_with_range()).
984 * @return @c TRUE if a value was entered and the dialog closed with 'OK'. @c FALSE otherwise.
986 * @since 0.16
988 gboolean dialogs_show_input_numeric(const gchar *title, const gchar *label_text,
989 gdouble *value, gdouble min, gdouble max, gdouble step)
991 GtkWidget *dialog, *label, *spin, *vbox;
992 gboolean res = FALSE;
994 g_return_val_if_fail(title != NULL, FALSE);
995 g_return_val_if_fail(label_text != NULL, FALSE);
996 g_return_val_if_fail(value != NULL, FALSE);
998 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
999 GTK_DIALOG_DESTROY_WITH_PARENT,
1000 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1001 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
1002 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
1003 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
1004 gtk_widget_set_name(dialog, "GeanyDialog");
1006 label = gtk_label_new(label_text);
1007 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1009 spin = gtk_spin_button_new_with_range(min, max, step);
1010 ui_entry_add_clear_icon(GTK_ENTRY(spin));
1011 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), *value);
1012 g_signal_connect(spin, "activate", G_CALLBACK(on_input_numeric_activate), dialog);
1014 gtk_container_add(GTK_CONTAINER(vbox), label);
1015 gtk_container_add(GTK_CONTAINER(vbox), spin);
1016 gtk_widget_show_all(vbox);
1018 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
1020 *value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
1021 res = TRUE;
1023 gtk_widget_destroy(dialog);
1025 return res;
1029 void dialogs_show_file_properties(GeanyDocument *doc)
1031 GtkWidget *dialog, *label, *table, *hbox, *image, *perm_table, *check, *vbox;
1032 gchar *file_size, *title, *base_name, *time_changed, *time_modified, *time_accessed, *enctext;
1033 gchar *short_name;
1034 #ifdef HAVE_SYS_TYPES_H
1035 struct stat st;
1036 off_t filesize;
1037 mode_t mode;
1038 gchar *locale_filename;
1039 #else
1040 gint filesize = 0;
1041 gint mode = 0;
1042 #endif
1044 /* define this ones, to avoid later trouble */
1045 #ifndef S_IRUSR
1046 # define S_IRUSR 0
1047 # define S_IWUSR 0
1048 # define S_IXUSR 0
1049 #endif
1050 #ifndef S_IRGRP
1051 # define S_IRGRP 0
1052 # define S_IWGRP 0
1053 # define S_IXGRP 0
1054 # define S_IROTH 0
1055 # define S_IWOTH 0
1056 # define S_IXOTH 0
1057 #endif
1059 if (doc == NULL || doc->file_name == NULL)
1061 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
1062 _("An error occurred or file information could not be retrieved (e.g. from a new file)."));
1063 return;
1067 #ifdef HAVE_SYS_TYPES_H
1068 locale_filename = utils_get_locale_from_utf8(doc->file_name);
1069 if (g_stat(locale_filename, &st) == 0)
1071 /* first copy the returned string and the trim it, to not modify the static glibc string
1072 * g_strchomp() is used to remove trailing EOL chars, which are there for whatever reason */
1073 time_changed = g_strchomp(g_strdup(ctime(&st.st_ctime)));
1074 time_modified = g_strchomp(g_strdup(ctime(&st.st_mtime)));
1075 time_accessed = g_strchomp(g_strdup(ctime(&st.st_atime)));
1076 filesize = st.st_size;
1077 mode = st.st_mode;
1079 else
1081 time_changed = g_strdup(_("unknown"));
1082 time_modified = g_strdup(_("unknown"));
1083 time_accessed = g_strdup(_("unknown"));
1084 filesize = (off_t) 0;
1085 mode = (mode_t) 0;
1087 g_free(locale_filename);
1088 #else
1089 time_changed = g_strdup(_("unknown"));
1090 time_modified = g_strdup(_("unknown"));
1091 time_accessed = g_strdup(_("unknown"));
1092 #endif
1094 base_name = g_path_get_basename(doc->file_name);
1095 short_name = utils_str_middle_truncate(base_name, 30);
1096 title = g_strconcat(short_name, " ", _("Properties"), NULL);
1097 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
1098 GTK_DIALOG_DESTROY_WITH_PARENT,
1099 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
1100 g_free(short_name);
1101 g_free(title);
1102 gtk_widget_set_name(dialog, "GeanyDialog");
1103 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
1105 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
1106 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), NULL);
1108 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, -1);
1110 label = ui_label_new_bold(base_name);
1111 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1112 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1113 image = gtk_image_new_from_stock("gtk-file", GTK_ICON_SIZE_BUTTON);
1114 gtk_misc_set_alignment(GTK_MISC(image), 1.0, 0.5);
1115 hbox = gtk_hbox_new(FALSE, 6);
1116 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1117 gtk_container_add(GTK_CONTAINER(hbox), image);
1118 gtk_container_add(GTK_CONTAINER(hbox), label);
1119 gtk_container_add(GTK_CONTAINER(vbox), hbox);
1121 table = gtk_table_new(8, 2, FALSE);
1122 gtk_table_set_row_spacings(GTK_TABLE(table), 10);
1123 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
1125 label = gtk_label_new(_("<b>Type:</b>"));
1126 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
1127 (GtkAttachOptions) (GTK_FILL),
1128 (GtkAttachOptions) (0), 0, 0);
1129 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1130 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1132 label = gtk_label_new(doc->file_type->title);
1133 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1134 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
1135 (GtkAttachOptions) (GTK_FILL),
1136 (GtkAttachOptions) (0), 0, 0);
1137 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
1139 label = gtk_label_new(_("<b>Size:</b>"));
1140 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
1141 (GtkAttachOptions) (GTK_FILL),
1142 (GtkAttachOptions) (0), 0, 0);
1143 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1144 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1146 file_size = utils_make_human_readable_str(filesize, 1, 0);
1147 label = gtk_label_new(file_size);
1148 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1149 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
1150 (GtkAttachOptions) (GTK_FILL),
1151 (GtkAttachOptions) (0), 0, 0);
1152 g_free(file_size);
1153 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
1155 label = gtk_label_new(_("<b>Location:</b>"));
1156 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
1157 (GtkAttachOptions) (GTK_FILL),
1158 (GtkAttachOptions) (0), 0, 0);
1159 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1160 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1162 label = gtk_label_new(doc->file_name);
1163 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1164 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
1165 (GtkAttachOptions) (GTK_FILL),
1166 (GtkAttachOptions) (0), 0, 0);
1167 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0);
1169 label = gtk_label_new(_("<b>Read-only:</b>"));
1170 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
1171 (GtkAttachOptions) (GTK_FILL),
1172 (GtkAttachOptions) (0), 0, 0);
1173 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1174 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1176 check = gtk_check_button_new_with_label(_("(only inside Geany)"));
1177 gtk_widget_set_sensitive(check, FALSE);
1178 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1179 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), doc->readonly);
1180 gtk_table_attach(GTK_TABLE(table), check, 1, 2, 3, 4,
1181 (GtkAttachOptions) (GTK_FILL),
1182 (GtkAttachOptions) (0), 0, 0);
1183 gtk_button_set_alignment(GTK_BUTTON(check), 0.0, 0);
1185 label = gtk_label_new(_("<b>Encoding:</b>"));
1186 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 4, 5,
1187 (GtkAttachOptions) (GTK_FILL),
1188 (GtkAttachOptions) (0), 0, 0);
1189 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1190 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1192 enctext = g_strdup_printf("%s %s",
1193 doc->encoding,
1194 (encodings_is_unicode_charset(doc->encoding)) ?
1195 ((doc->has_bom) ? _("(with BOM)") : _("(without BOM)")) : "");
1197 label = gtk_label_new(enctext);
1198 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1199 g_free(enctext);
1201 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 4, 5,
1202 (GtkAttachOptions) (GTK_FILL),
1203 (GtkAttachOptions) (0), 0, 0);
1204 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0);
1206 label = gtk_label_new(_("<b>Modified:</b>"));
1207 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 5, 6,
1208 (GtkAttachOptions) (GTK_FILL),
1209 (GtkAttachOptions) (0), 0, 0);
1210 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1211 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1213 label = gtk_label_new(time_modified);
1214 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1215 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 5, 6,
1216 (GtkAttachOptions) (GTK_FILL),
1217 (GtkAttachOptions) (0), 0, 0);
1218 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
1220 label = gtk_label_new(_("<b>Changed:</b>"));
1221 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 6, 7,
1222 (GtkAttachOptions) (GTK_FILL),
1223 (GtkAttachOptions) (0), 0, 0);
1224 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1225 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1227 label = gtk_label_new(time_changed);
1228 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1229 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 6, 7,
1230 (GtkAttachOptions) (GTK_FILL),
1231 (GtkAttachOptions) (0), 0, 0);
1232 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
1234 label = gtk_label_new(_("<b>Accessed:</b>"));
1235 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 7, 8,
1236 (GtkAttachOptions) (GTK_FILL),
1237 (GtkAttachOptions) (0), 0, 0);
1238 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1239 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
1241 label = gtk_label_new(time_accessed);
1242 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
1243 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 7, 8,
1244 (GtkAttachOptions) (GTK_FILL),
1245 (GtkAttachOptions) (0), 0, 0);
1246 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
1248 /* add table */
1249 gtk_container_add(GTK_CONTAINER(vbox), table);
1251 /* create table with the permissions */
1252 perm_table = gtk_table_new(5, 4, TRUE);
1253 gtk_table_set_row_spacings(GTK_TABLE(perm_table), 5);
1254 gtk_table_set_col_spacings(GTK_TABLE(perm_table), 5);
1256 label = gtk_label_new(_("<b>Permissions:</b>"));
1257 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1258 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1259 gtk_table_attach(GTK_TABLE(perm_table), label, 0, 4, 0, 1,
1260 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1261 (GtkAttachOptions) (0), 0, 0);
1263 /* Header */
1264 label = gtk_label_new(_("Read:"));
1265 gtk_table_attach(GTK_TABLE(perm_table), label, 1, 2, 1, 2,
1266 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1267 (GtkAttachOptions) (0), 0, 0);
1268 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1269 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0);
1271 label = gtk_label_new(_("Write:"));
1272 gtk_table_attach(GTK_TABLE(perm_table), label, 2, 3, 1, 2,
1273 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1274 (GtkAttachOptions) (0), 0, 0);
1275 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1276 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0);
1278 label = gtk_label_new(_("Execute:"));
1279 gtk_table_attach(GTK_TABLE(perm_table), label, 3, 4, 1, 2,
1280 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1281 (GtkAttachOptions) (0), 0, 0);
1282 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1283 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0);
1285 /* Owner */
1286 label = gtk_label_new(_("Owner:"));
1287 gtk_table_attach(GTK_TABLE(perm_table), label, 0, 1, 2, 3,
1288 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1289 (GtkAttachOptions) (0), 0, 0);
1290 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1291 gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0);
1293 check = gtk_check_button_new();
1294 gtk_widget_set_sensitive(check, FALSE);
1295 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1296 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IRUSR);
1297 gtk_table_attach(GTK_TABLE(perm_table), check, 1, 2, 2, 3,
1298 (GtkAttachOptions) (GTK_EXPAND | GTK_EXPAND | GTK_FILL),
1299 (GtkAttachOptions) (0), 0, 0);
1300 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1302 check = gtk_check_button_new();
1303 gtk_widget_set_sensitive(check, FALSE);
1304 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1305 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWUSR);
1306 gtk_table_attach(GTK_TABLE(perm_table), check, 2, 3, 2, 3,
1307 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1308 (GtkAttachOptions) (0), 0, 0);
1309 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1311 check = gtk_check_button_new();
1312 gtk_widget_set_sensitive(check, FALSE);
1313 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1314 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXUSR);
1315 gtk_table_attach(GTK_TABLE(perm_table), check, 3, 4, 2, 3,
1316 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1317 (GtkAttachOptions) (0), 0, 0);
1318 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1321 /* Group */
1322 label = gtk_label_new(_("Group:"));
1323 gtk_table_attach(GTK_TABLE(perm_table), label, 0, 1, 3, 4,
1324 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1325 (GtkAttachOptions) (0), 0, 0);
1326 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1327 gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0);
1329 check = gtk_check_button_new();
1330 gtk_widget_set_sensitive(check, FALSE);
1331 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1332 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IRGRP);
1333 gtk_table_attach(GTK_TABLE(perm_table), check, 1, 2, 3, 4,
1334 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1335 (GtkAttachOptions) (0), 0, 0);
1336 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1338 check = gtk_check_button_new();
1339 gtk_widget_set_sensitive(check, FALSE);
1340 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1341 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWGRP);
1342 gtk_table_attach(GTK_TABLE(perm_table), check, 2, 3, 3, 4,
1343 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1344 (GtkAttachOptions) (0), 0, 0);
1345 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1347 check = gtk_check_button_new();
1348 gtk_widget_set_sensitive(check, FALSE);
1349 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1350 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXGRP);
1351 gtk_table_attach(GTK_TABLE(perm_table), check, 3, 4, 3, 4,
1352 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1353 (GtkAttachOptions) (0), 0, 0);
1354 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1357 /* Other */
1358 label = gtk_label_new(_("Other:"));
1359 gtk_table_attach(GTK_TABLE(perm_table), label, 0, 1, 4, 5,
1360 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1361 (GtkAttachOptions) (0), 0, 0);
1362 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1363 gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0);
1365 check = gtk_check_button_new();
1366 gtk_widget_set_sensitive(check, FALSE);
1367 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1368 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IROTH);
1369 gtk_table_attach(GTK_TABLE(perm_table), check, 1, 2, 4, 5,
1370 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1371 (GtkAttachOptions) (0), 0, 0);
1372 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1374 check = gtk_check_button_new();
1375 gtk_widget_set_sensitive(check, FALSE);
1376 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1377 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IWOTH);
1378 gtk_table_attach(GTK_TABLE(perm_table), check, 2, 3, 4, 5,
1379 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1380 (GtkAttachOptions) (0), 0, 0);
1381 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1383 check = gtk_check_button_new();
1384 gtk_widget_set_sensitive(check, FALSE);
1385 gtk_button_set_focus_on_click(GTK_BUTTON(check), FALSE);
1386 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), mode & S_IXOTH);
1387 gtk_table_attach(GTK_TABLE(perm_table), check, 3, 4, 4, 5,
1388 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1389 (GtkAttachOptions) (0), 0, 0);
1390 gtk_button_set_alignment(GTK_BUTTON(check), 0.5, 0);
1392 gtk_container_add(GTK_CONTAINER(vbox), perm_table);
1394 g_free(base_name);
1395 g_free(time_changed);
1396 g_free(time_modified);
1397 g_free(time_accessed);
1399 gtk_widget_show_all(dialog);
1403 /* extra_text can be NULL; otherwise it is displayed below main_text.
1404 * if parent is NULL, main_widgets.window will be used
1405 * btn_1, btn_2, btn_3 can be NULL.
1406 * returns response_1, response_2 or response_3 */
1407 static gint show_prompt(GtkWidget *parent,
1408 const gchar *btn_1, GtkResponseType response_1,
1409 const gchar *btn_2, GtkResponseType response_2,
1410 const gchar *btn_3, GtkResponseType response_3,
1411 const gchar *question_text, const gchar *extra_text)
1413 gboolean ret = FALSE;
1414 GtkWidget *dialog;
1415 GtkWidget *btn;
1417 if (btn_2 == NULL)
1419 btn_2 = GTK_STOCK_NO;
1420 response_2 = GTK_RESPONSE_NO;
1422 if (btn_3 == NULL)
1424 btn_3 = GTK_STOCK_YES;
1425 response_3 = GTK_RESPONSE_YES;
1428 #ifdef G_OS_WIN32
1429 /* our native dialog code doesn't support custom buttons */
1430 if (btn_3 == GTK_STOCK_YES && btn_2 == GTK_STOCK_NO && btn_1 == NULL)
1432 gchar *string = (extra_text == NULL) ? g_strdup(question_text) :
1433 g_strconcat(question_text, "\n\n", extra_text, NULL);
1435 ret = win32_message_dialog(parent, GTK_MESSAGE_QUESTION, string);
1436 ret = ret ? response_3 : response_2;
1437 g_free(string);
1438 return ret;
1440 #endif
1441 if (parent == NULL && main_status.main_window_realized)
1442 parent = main_widgets.window;
1444 dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
1445 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
1446 GTK_BUTTONS_NONE, "%s", question_text);
1447 gtk_widget_set_name(dialog, "GeanyDialog");
1448 gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
1449 if (parent == NULL || GTK_IS_DIALOG(parent))
1451 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
1452 gtk_window_set_icon(GTK_WINDOW(dialog), pb);
1453 g_object_unref(pb);
1456 /* question_text will be in bold if optional extra_text used */
1457 if (extra_text != NULL)
1458 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
1459 "%s", extra_text);
1461 if (btn_1 != NULL)
1462 gtk_dialog_add_button(GTK_DIALOG(dialog), btn_1, response_1);
1464 /* For a cancel button, use cancel response so user can press escape to cancel */
1465 btn = gtk_dialog_add_button(GTK_DIALOG(dialog), btn_2,
1466 utils_str_equal(btn_2, GTK_STOCK_CANCEL) ? GTK_RESPONSE_CANCEL : response_2);
1467 /* we don't want a default, but we need to override the apply button as default */
1468 gtk_widget_grab_default(btn);
1469 gtk_dialog_add_button(GTK_DIALOG(dialog), btn_3, response_3);
1471 ret = gtk_dialog_run(GTK_DIALOG(dialog));
1472 gtk_widget_destroy(dialog);
1474 if (ret == GTK_RESPONSE_CANCEL)
1475 ret = response_2;
1476 return ret;
1481 * Shows a question message box with @a text and Yes/No buttons.
1482 * On Unix-like systems a GTK message dialog box is shown, on Win32 systems a native Windows
1483 * message dialog box is shown.
1485 * @param text Printf()-style format string.
1486 * @param ... Arguments for the @a text format string.
1488 * @return @c TRUE if the user answered with Yes, otherwise @c FALSE.
1490 gboolean dialogs_show_question(const gchar *text, ...)
1492 gchar *string;
1493 va_list args;
1494 GtkWidget *parent = (main_status.main_window_realized) ? main_widgets.window : NULL;
1495 gint result;
1497 va_start(args, text);
1498 string = g_strdup_vprintf(text, args);
1499 va_end(args);
1500 result = show_prompt(parent,
1501 NULL, GTK_RESPONSE_NONE,
1502 GTK_STOCK_NO, GTK_RESPONSE_NO,
1503 GTK_STOCK_YES, GTK_RESPONSE_YES,
1504 string, NULL);
1505 g_free(string);
1506 return (result == GTK_RESPONSE_YES);
1510 /* extra_text can be NULL; otherwise it is displayed below main_text.
1511 * if parent is NULL, main_widgets.window will be used
1512 * yes_btn, no_btn can be NULL. */
1513 gboolean dialogs_show_question_full(GtkWidget *parent, const gchar *yes_btn, const gchar *no_btn,
1514 const gchar *extra_text, const gchar *main_text, ...)
1516 gint result;
1517 gchar *string;
1518 va_list args;
1520 va_start(args, main_text);
1521 string = g_strdup_vprintf(main_text, args);
1522 va_end(args);
1523 result = show_prompt(parent,
1524 NULL, GTK_RESPONSE_NONE,
1525 no_btn, GTK_RESPONSE_NO,
1526 yes_btn, GTK_RESPONSE_YES,
1527 string, extra_text);
1528 g_free(string);
1529 return (result == GTK_RESPONSE_YES);
1533 /* extra_text can be NULL; otherwise it is displayed below main_text.
1534 * if parent is NULL, main_widgets.window will be used
1535 * btn_1, btn_2, btn_3 can be NULL.
1536 * returns response_1, response_2 or response_3 */
1537 gint dialogs_show_prompt(GtkWidget *parent,
1538 const gchar *btn_1, GtkResponseType response_1,
1539 const gchar *btn_2, GtkResponseType response_2,
1540 const gchar *btn_3, GtkResponseType response_3,
1541 const gchar *extra_text, const gchar *main_text, ...)
1543 gchar *string;
1544 va_list args;
1545 gint result;
1547 va_start(args, main_text);
1548 string = g_strdup_vprintf(main_text, args);
1549 va_end(args);
1550 result = show_prompt(parent, btn_1, response_1, btn_2, response_2, btn_3, response_3,
1551 string, extra_text);
1552 g_free(string);
1553 return result;