/
dialogs.c
1703 lines (1425 loc) · 55.7 KB
/
dialogs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dialogs.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* File related dialogs, miscellaneous dialogs, font dialog.
*/
#include "geany.h"
#include <gdk/gdkkeysyms.h>
#include <string.h>
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <time.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
/* gstdio.h also includes sys/stat.h */
#include <glib/gstdio.h>
#include "dialogs.h"
#include "callbacks.h"
#include "document.h"
#include "filetypes.h"
#include "win32.h"
#include "sciwrappers.h"
#include "support.h"
#include "utils.h"
#include "ui_utils.h"
#include "keybindings.h"
#include "encodings.h"
#include "build.h"
#include "main.h"
#include "project.h"
#include "gtkcompat.h"
enum
{
GEANY_RESPONSE_RENAME,
GEANY_RESPONSE_VIEW
};
static struct FileSelState
{
struct
{
guint filter_idx;
gint encoding_idx;
gint filetype_idx;
gboolean show_hidden;
gboolean more_options_visible;
} open;
struct
{
gboolean open_in_new_tab;
} save;
}
filesel_state = {
{
0,
GEANY_ENCODINGS_MAX, /* default encoding is detect from file */
0,
FALSE,
FALSE
},
{
FALSE
}
};
/* gets the ID of the current file filter */
static guint file_chooser_get_filter_idx(GtkFileChooser *chooser)
{
guint idx = 0;
GtkFileFilter *current;
GSList *filters, *item;
current = gtk_file_chooser_get_filter(chooser);
filters = gtk_file_chooser_list_filters(chooser);
foreach_slist(item, filters)
{
if (item->data == current)
break;
idx ++;
}
g_slist_free(filters);
return idx;
}
/* sets the current file filter from its ID */
static void file_chooser_set_filter_idx(GtkFileChooser *chooser, guint idx)
{
GtkFileFilter *current;
GSList *filters;
filters = gtk_file_chooser_list_filters(chooser);
current = g_slist_nth_data(filters, idx);
g_slist_free(filters);
gtk_file_chooser_set_filter(chooser, current);
}
static void open_file_dialog_handle_response(GtkWidget *dialog, gint response)
{
if (response == GTK_RESPONSE_ACCEPT || response == GEANY_RESPONSE_VIEW)
{
GSList *filelist;
GtkTreeModel *encoding_model;
GtkTreeIter encoding_iter;
GeanyFiletype *ft = NULL;
const gchar *charset = NULL;
GtkWidget *expander = ui_lookup_widget(dialog, "more_options_expander");
GtkWidget *filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
GtkWidget *encoding_combo = ui_lookup_widget(dialog, "encoding_combo");
gboolean ro = (response == GEANY_RESPONSE_VIEW); /* View clicked */
filesel_state.open.more_options_visible = gtk_expander_get_expanded(GTK_EXPANDER(expander));
filesel_state.open.filter_idx = file_chooser_get_filter_idx(GTK_FILE_CHOOSER(dialog));
filesel_state.open.filetype_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(filetype_combo));
/* ignore detect from file item */
if (filesel_state.open.filetype_idx > 0)
ft = g_slist_nth_data(filetypes_by_title, (guint) filesel_state.open.filetype_idx);
encoding_model = gtk_combo_box_get_model(GTK_COMBO_BOX(encoding_combo));
gtk_combo_box_get_active_iter(GTK_COMBO_BOX(encoding_combo), &encoding_iter);
gtk_tree_model_get(encoding_model, &encoding_iter, 0, &filesel_state.open.encoding_idx, -1);
if (filesel_state.open.encoding_idx >= 0 && filesel_state.open.encoding_idx < GEANY_ENCODINGS_MAX)
charset = encodings[filesel_state.open.encoding_idx].charset;
filelist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
if (filelist != NULL)
{
document_open_files(filelist, ro, ft, charset);
g_slist_foreach(filelist, (GFunc) g_free, NULL); /* free filenames */
}
g_slist_free(filelist);
}
if (app->project && NZV(app->project->base_path))
gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(dialog),
app->project->base_path, NULL);
}
static void on_file_open_show_hidden_notify(GObject *filechooser,
GParamSpec *pspec, gpointer data)
{
GtkWidget *toggle_button;
toggle_button = ui_lookup_widget(GTK_WIDGET(filechooser), "check_hidden");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_button),
gtk_file_chooser_get_show_hidden(GTK_FILE_CHOOSER(filechooser)));
}
static void
on_file_open_check_hidden_toggled(GtkToggleButton *togglebutton, GtkWidget *dialog)
{
filesel_state.open.show_hidden = gtk_toggle_button_get_active(togglebutton);
gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(dialog), filesel_state.open.show_hidden);
}
static gint encoding_combo_store_sort_func(GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer data)
{
gboolean a_has_child = gtk_tree_model_iter_has_child(model, a);
gboolean b_has_child = gtk_tree_model_iter_has_child(model, b);
gchar *a_string;
gchar *b_string;
gint cmp_res;
if (a_has_child != b_has_child)
return a_has_child ? -1 : 1;
gtk_tree_model_get(model, a, 1, &a_string, -1);
gtk_tree_model_get(model, b, 1, &b_string, -1);
cmp_res = strcmp(a_string, b_string);
g_free(a_string);
g_free(b_string);
return cmp_res;
}
static GtkTreeStore *create_encoding_combo_store(GtkTreeIter *iter_detect)
{
GtkTreeStore *store;
GtkTreeIter iter_current, iter_westeuro, iter_easteuro, iter_eastasian, iter_asian,
iter_utf8, iter_middleeast;
GtkTreeIter *iter_parent;
gchar *encoding_string;
gint i;
store = gtk_tree_store_new(2, G_TYPE_INT, G_TYPE_STRING);
gtk_tree_store_append(store, iter_detect, NULL);
gtk_tree_store_set(store, iter_detect, 0, GEANY_ENCODINGS_MAX, 1, _("Detect from file"), -1);
gtk_tree_store_append(store, &iter_westeuro, NULL);
gtk_tree_store_set(store, &iter_westeuro, 0, -1, 1, _("West European"), -1);
gtk_tree_store_append(store, &iter_easteuro, NULL);
gtk_tree_store_set(store, &iter_easteuro, 0, -1, 1, _("East European"), -1);
gtk_tree_store_append(store, &iter_eastasian, NULL);
gtk_tree_store_set(store, &iter_eastasian, 0, -1, 1, _("East Asian"), -1);
gtk_tree_store_append(store, &iter_asian, NULL);
gtk_tree_store_set(store, &iter_asian, 0, -1, 1, _("SE & SW Asian"), -1);
gtk_tree_store_append(store, &iter_middleeast, NULL);
gtk_tree_store_set(store, &iter_middleeast, 0, -1, 1, _("Middle Eastern"), -1);
gtk_tree_store_append(store, &iter_utf8, NULL);
gtk_tree_store_set(store, &iter_utf8, 0, -1, 1, _("Unicode"), -1);
for (i = 0; i < GEANY_ENCODINGS_MAX; i++)
{
switch (encodings[i].group)
{
case WESTEUROPEAN: iter_parent = &iter_westeuro; break;
case EASTEUROPEAN: iter_parent = &iter_easteuro; break;
case EASTASIAN: iter_parent = &iter_eastasian; break;
case ASIAN: iter_parent = &iter_asian; break;
case MIDDLEEASTERN: iter_parent = &iter_middleeast; break;
case UNICODE: iter_parent = &iter_utf8; break;
case NONE:
default: iter_parent = NULL;
}
gtk_tree_store_append(store, &iter_current, iter_parent);
encoding_string = encodings_to_string(&encodings[i]);
gtk_tree_store_set(store, &iter_current, 0, i, 1, encoding_string, -1);
g_free(encoding_string);
/* restore the saved state */
if (i == filesel_state.open.encoding_idx)
*iter_detect = iter_current;
}
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), 1, GTK_SORT_ASCENDING);
gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), 1, encoding_combo_store_sort_func, NULL, NULL);
return store;
}
static void encoding_combo_cell_data_func(GtkCellLayout *cell_layout,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer data)
{
gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
g_object_set(cell, "sensitive", sensitive, NULL);
}
static GtkWidget *add_file_open_extra_widget(GtkWidget *dialog)
{
GtkWidget *expander, *vbox, *table, *check_hidden;
GtkWidget *filetype_ebox, *filetype_label, *filetype_combo;
GtkWidget *encoding_ebox, *encoding_label, *encoding_combo;
expander = gtk_expander_new_with_mnemonic(_("_More Options"));
vbox = gtk_vbox_new(FALSE, 6);
gtk_container_add(GTK_CONTAINER(expander), vbox);
table = gtk_table_new(2, 4, FALSE);
/* line 1 with checkbox and encoding combo */
check_hidden = gtk_check_button_new_with_mnemonic(_("Show _hidden files"));
gtk_widget_show(check_hidden);
gtk_table_attach(GTK_TABLE(table), check_hidden, 0, 1, 0, 1,
(GtkAttachOptions) (GTK_FILL | GTK_EXPAND),
(GtkAttachOptions) (0), 0, 5);
/* spacing */
gtk_table_attach(GTK_TABLE(table), gtk_label_new(""), 1, 2, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 5, 5);
encoding_label = gtk_label_new(_("Set encoding:"));
gtk_misc_set_alignment(GTK_MISC(encoding_label), 1, 0);
gtk_table_attach(GTK_TABLE(table), encoding_label, 2, 3, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 4, 5);
/* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
encoding_ebox = gtk_event_box_new();
encoding_combo = gtk_combo_box_new();
gtk_widget_set_tooltip_text(encoding_ebox,
_("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."));
gtk_container_add(GTK_CONTAINER(encoding_ebox), encoding_combo);
gtk_table_attach(GTK_TABLE(table), encoding_ebox, 3, 4, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 5);
/* line 2 with filetype combo */
filetype_label = gtk_label_new(_("Set filetype:"));
gtk_misc_set_alignment(GTK_MISC(filetype_label), 1, 0);
gtk_table_attach(GTK_TABLE(table), filetype_label, 2, 3, 1, 2,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 4, 5);
/* the ebox is for the tooltip, because gtk_combo_box can't show tooltips */
filetype_ebox = gtk_event_box_new();
filetype_combo = gtk_combo_box_text_new();
gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(filetype_combo), 2);
gtk_widget_set_tooltip_text(filetype_ebox,
_("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."));
gtk_container_add(GTK_CONTAINER(filetype_ebox), filetype_combo);
gtk_table_attach(GTK_TABLE(table), filetype_ebox, 3, 4, 1, 2,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 5);
gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
gtk_widget_show_all(vbox);
g_signal_connect(check_hidden, "toggled", G_CALLBACK(on_file_open_check_hidden_toggled), dialog);
ui_hookup_widget(dialog, expander, "more_options_expander");
ui_hookup_widget(dialog, check_hidden, "check_hidden");
ui_hookup_widget(dialog, filetype_combo, "filetype_combo");
ui_hookup_widget(dialog, encoding_combo, "encoding_combo");
return expander;
}
static GtkWidget *create_open_file_dialog(void)
{
GtkWidget *dialog;
GtkWidget *filetype_combo, *encoding_combo;
GtkWidget *viewbtn;
GtkCellRenderer *encoding_renderer;
GtkTreeIter encoding_iter;
GSList *node;
dialog = gtk_file_chooser_dialog_new(_("Open File"), GTK_WINDOW(main_widgets.window),
GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
gtk_widget_set_name(dialog, "GeanyDialog");
viewbtn = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_View"), GEANY_RESPONSE_VIEW);
gtk_widget_set_tooltip_text(viewbtn,
_("Opens the file in read-only mode. If you choose more than one file to open, all files will be opened read-only."));
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
gtk_widget_set_size_request(dialog, -1, 460);
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE);
/* add checkboxes and filename entry */
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), add_file_open_extra_widget(dialog));
filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(filetype_combo), _("Detect by file extension"));
/* add FileFilters(start with "All Files") */
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),
filetypes_create_file_filter(filetypes[GEANY_FILETYPES_NONE]));
/* now create meta filter "All Source" */
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),
filetypes_create_file_filter_all_source());
foreach_slist(node, filetypes_by_title)
{
GeanyFiletype *ft = node->data;
if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
continue;
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(filetype_combo), ft->title);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filetypes_create_file_filter(ft));
}
gtk_combo_box_set_active(GTK_COMBO_BOX(filetype_combo), 0);
/* fill encoding combo box */
encoding_combo = ui_lookup_widget(dialog, "encoding_combo");
gtk_combo_box_set_model(GTK_COMBO_BOX(encoding_combo), GTK_TREE_MODEL(
create_encoding_combo_store(&encoding_iter)));
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(encoding_combo), &encoding_iter);
encoding_renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(encoding_combo), encoding_renderer, TRUE);
gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(encoding_combo), encoding_renderer, "text", 1);
gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(encoding_combo), encoding_renderer,
encoding_combo_cell_data_func, NULL, NULL);
g_signal_connect(dialog, "notify::show-hidden",
G_CALLBACK(on_file_open_show_hidden_notify), NULL);
return dialog;
}
static void open_file_dialog_apply_settings(GtkWidget *dialog)
{
static gboolean initialized = FALSE;
GtkWidget *check_hidden = ui_lookup_widget(dialog, "check_hidden");
GtkWidget *filetype_combo = ui_lookup_widget(dialog, "filetype_combo");
GtkWidget *expander = ui_lookup_widget(dialog, "more_options_expander");
/* we can't know the initial position of combo boxes, so retreive it the first time */
if (! initialized)
{
filesel_state.open.filter_idx = file_chooser_get_filter_idx(GTK_FILE_CHOOSER(dialog));
filesel_state.open.filetype_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(filetype_combo));
initialized = TRUE;
}
else
{
file_chooser_set_filter_idx(GTK_FILE_CHOOSER(dialog), filesel_state.open.filter_idx);
gtk_combo_box_set_active(GTK_COMBO_BOX(filetype_combo), filesel_state.open.filetype_idx);
}
gtk_expander_set_expanded(GTK_EXPANDER(expander), filesel_state.open.more_options_visible);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_hidden), filesel_state.open.show_hidden);
/* encoding combo is restored at creating time, see create_encoding_combo_store() */
}
/* This shows the file selection dialog to open a file. */
void dialogs_show_open_file(void)
{
gchar *initdir;
/* set dialog directory to the current file's directory, if present */
initdir = utils_get_current_file_dir_utf8();
/* use project or default startup directory (if set) if no files are open */
/** TODO should it only be used when initally open the dialog and not on every show? */
if (! initdir)
initdir = g_strdup(utils_get_default_dir_utf8());
SETPTR(initdir, utils_get_locale_from_utf8(initdir));
#ifdef G_OS_WIN32
if (interface_prefs.use_native_windows_dialogs)
win32_show_document_open_dialog(GTK_WINDOW(main_widgets.window), _("Open File"), initdir);
else
#endif
{
GtkWidget *dialog = create_open_file_dialog();
gint response;
open_file_dialog_apply_settings(dialog);
if (initdir != NULL && g_path_is_absolute(initdir))
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), initdir);
if (app->project && NZV(app->project->base_path))
gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),
app->project->base_path, NULL);
response = gtk_dialog_run(GTK_DIALOG(dialog));
open_file_dialog_handle_response(dialog, response);
gtk_widget_destroy(dialog);
}
g_free(initdir);
}
static void on_save_as_new_tab_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
gtk_widget_set_sensitive(GTK_WIDGET(user_data), ! gtk_toggle_button_get_active(togglebutton));
}
static gboolean handle_save_as(const gchar *utf8_filename, gboolean open_new_tab, gboolean rename_file)
{
GeanyDocument *doc = document_get_current();
gboolean success = FALSE;
g_return_val_if_fail(NZV(utf8_filename), FALSE);
if (open_new_tab)
{ /* "open" the saved file in a new tab and switch to it */
doc = document_clone(doc, utf8_filename);
success = document_save_file_as(doc, NULL);
}
else
{
if (doc->file_name != NULL)
{
if (rename_file)
{
document_rename_file(doc, utf8_filename);
}
/* create a new tm_source_file object otherwise tagmanager won't work correctly */
tm_workspace_remove_object(doc->tm_file, TRUE, TRUE);
doc->tm_file = NULL;
}
success = document_save_file_as(doc, utf8_filename);
build_menu_update(doc);
}
return success;
}
static gboolean save_as_dialog_handle_response(GtkWidget *dialog, gint response)
{
gboolean rename_file = FALSE;
gboolean success = FALSE;
gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
switch (response)
{
case GEANY_RESPONSE_RENAME:
/* rename doesn't check for empty filename or overwriting */
if (G_UNLIKELY(! NZV(new_filename)))
{
utils_beep();
break;
}
if (g_file_test(new_filename, G_FILE_TEST_EXISTS) &&
!dialogs_show_question_full(NULL, NULL, NULL,
_("Overwrite?"),
_("Filename already exists!")))
break;
rename_file = TRUE;
/* fall through */
case GTK_RESPONSE_ACCEPT:
{
gboolean open_new_tab = gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(ui_lookup_widget(dialog, "check_open_new_tab")));
gchar *utf8_filename;
utf8_filename = utils_get_utf8_from_locale(new_filename);
success = handle_save_as(utf8_filename, open_new_tab, rename_file);
if (success)
filesel_state.save.open_in_new_tab = open_new_tab;
g_free(utf8_filename);
break;
}
case GTK_RESPONSE_DELETE_EVENT:
case GTK_RESPONSE_CANCEL:
success = TRUE;
break;
}
g_free(new_filename);
return success;
}
static GtkWidget *create_save_file_dialog(void)
{
GtkWidget *dialog, *vbox, *check_open_new_tab, *rename_btn;
const gchar *initdir;
dialog = gtk_file_chooser_dialog_new(_("Save File"), GTK_WINDOW(main_widgets.window),
GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
gtk_widget_set_name(dialog, "GeanyDialog");
rename_btn = gtk_dialog_add_button(GTK_DIALOG(dialog), _("R_ename"), GEANY_RESPONSE_RENAME);
gtk_widget_set_tooltip_text(rename_btn, _("Save the file and rename it"));
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
vbox = gtk_vbox_new(FALSE, 0);
check_open_new_tab = gtk_check_button_new_with_mnemonic(_("_Open file in a new tab"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_open_new_tab), filesel_state.save.open_in_new_tab);
gtk_widget_set_tooltip_text(check_open_new_tab,
_("Keep the current unsaved document open"
" and open the newly saved file in a new tab"));
gtk_box_pack_start(GTK_BOX(vbox), check_open_new_tab, FALSE, FALSE, 0);
gtk_widget_show_all(vbox);
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE);
/* set the folder by default to the project base dir or the global pref for opening files */
initdir = utils_get_default_dir_utf8();
if (initdir)
{
gchar *linitdir = utils_get_locale_from_utf8(initdir);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), linitdir);
g_free(linitdir);
}
g_signal_connect(check_open_new_tab, "toggled",
G_CALLBACK(on_save_as_new_tab_toggled), rename_btn);
ui_hookup_widget(dialog, check_open_new_tab, "check_open_new_tab");
return dialog;
}
static gboolean show_save_as_gtk(GeanyDocument *doc)
{
GtkWidget *dialog;
gint resp;
g_return_val_if_fail(doc != NULL, FALSE);
dialog = create_save_file_dialog();
if (doc->file_name != NULL)
{
if (g_path_is_absolute(doc->file_name))
{
gchar *locale_filename = utils_get_locale_from_utf8(doc->file_name);
gchar *locale_basename = g_path_get_basename(locale_filename);
gchar *locale_dirname = g_path_get_dirname(locale_filename);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), locale_basename);
g_free(locale_filename);
g_free(locale_basename);
g_free(locale_dirname);
}
else
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), doc->file_name);
}
else
{
gchar *fname = NULL;
if (doc->file_type != NULL && doc->file_type->extension != NULL)
fname = g_strconcat(GEANY_STRING_UNTITLED, ".",
doc->file_type->extension, NULL);
else
fname = g_strdup(GEANY_STRING_UNTITLED);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
g_free(fname);
}
if (app->project && NZV(app->project->base_path))
gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),
app->project->base_path, NULL);
/* Run the dialog synchronously, pausing this function call */
do
{
resp = gtk_dialog_run(GTK_DIALOG(dialog));
}
while (! save_as_dialog_handle_response(dialog, resp));
if (app->project && NZV(app->project->base_path))
gtk_file_chooser_remove_shortcut_folder(GTK_FILE_CHOOSER(dialog),
app->project->base_path, NULL);
gtk_widget_destroy(dialog);
return (resp == GTK_RESPONSE_ACCEPT);
}
/**
* Shows the Save As dialog for the current notebook page.
*
* @return @c TRUE if the file was saved, otherwise @c FALSE.
**/
gboolean dialogs_show_save_as()
{
GeanyDocument *doc = document_get_current();
gboolean result = FALSE;
g_return_val_if_fail(doc, FALSE);
#ifdef G_OS_WIN32
if (interface_prefs.use_native_windows_dialogs)
{
gchar *utf8_name = win32_show_document_save_as_dialog(GTK_WINDOW(main_widgets.window),
_("Save File"), DOC_FILENAME(doc));
if (utf8_name != NULL)
result = handle_save_as(utf8_name, FALSE, FALSE);
}
else
#endif
result = show_save_as_gtk(doc);
return result;
}
#ifndef G_OS_WIN32
static void show_msgbox_dialog(GtkWidget *dialog, GtkMessageType type, GtkWindow *parent)
{
const gchar *title;
switch (type)
{
case GTK_MESSAGE_ERROR:
title = _("Error");
break;
case GTK_MESSAGE_QUESTION:
title = _("Question");
break;
case GTK_MESSAGE_WARNING:
title = _("Warning");
break;
default:
title = _("Information");
break;
}
gtk_window_set_title(GTK_WINDOW(dialog), title);
if (parent == NULL || GTK_IS_DIALOG(parent))
{
GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
gtk_window_set_icon(GTK_WINDOW(dialog), pb);
g_object_unref(pb);
}
gtk_widget_set_name(dialog, "GeanyDialog");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
#endif
/**
* Shows a message box of the type @a type with @a text.
* On Unix-like systems a GTK message dialog box is shown, on Win32 systems a native Windows
* message dialog box is shown.
*
* @param type A @c GtkMessageType, e.g. @c GTK_MESSAGE_INFO, @c GTK_MESSAGE_WARNING,
* @c GTK_MESSAGE_QUESTION, @c GTK_MESSAGE_ERROR.
* @param text Printf()-style format string.
* @param ... Arguments for the @a text format string.
**/
void dialogs_show_msgbox(GtkMessageType type, const gchar *text, ...)
{
#ifndef G_OS_WIN32
GtkWidget *dialog;
#endif
gchar *string;
va_list args;
GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
va_start(args, text);
string = g_strdup_vprintf(text, args);
va_end(args);
#ifdef G_OS_WIN32
win32_message_dialog(GTK_WIDGET(parent), type, string);
#else
dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
type, GTK_BUTTONS_OK, "%s", string);
show_msgbox_dialog(dialog, type, parent);
#endif
g_free(string);
}
void dialogs_show_msgbox_with_secondary(GtkMessageType type, const gchar *text, const gchar *secondary)
{
GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL;
#ifdef G_OS_WIN32
/* put the two strings together because Windows message boxes don't support secondary texts */
gchar *string = g_strconcat(text, "\n", secondary, NULL);
win32_message_dialog(GTK_WIDGET(parent), type, string);
g_free(string);
#else
GtkWidget *dialog;
dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT,
type, GTK_BUTTONS_OK, "%s", text);
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", secondary);
show_msgbox_dialog(dialog, type, parent);
#endif
}
static gint run_unsaved_dialog(const gchar *msg, const gchar *msg2)
{
GtkWidget *dialog, *button;
gint ret;
dialog = gtk_message_dialog_new(GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", msg);
gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", msg2);
gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
button = ui_button_new_with_image(GTK_STOCK_CLEAR, _("_Don't save"));
gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button, GTK_RESPONSE_NO);
gtk_widget_show(button);
gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return ret;
}
gboolean dialogs_show_unsaved_file(GeanyDocument *doc)
{
gchar *msg, *short_fn = NULL;
const gchar *msg2;
gint response;
gboolean old_quitting_state = main_status.quitting;
/* display the file tab to remind the user of the document */
main_status.quitting = FALSE;
document_show_tab(doc);
main_status.quitting = old_quitting_state;
short_fn = document_get_basename_for_display(doc, -1);
msg = g_strdup_printf(_("The file '%s' is not saved."), short_fn);
msg2 = _("Do you want to save it before closing?");
g_free(short_fn);
response = run_unsaved_dialog(msg, msg2);
g_free(msg);
switch (response)
{
case GTK_RESPONSE_YES:
/* document_save_file() returns the status if the file could be saved */
return document_save_file(doc, FALSE);
case GTK_RESPONSE_NO:
return TRUE;
case GTK_RESPONSE_CANCEL: /* fall through to default and leave the function */
default:
return FALSE;
}
}
#ifndef G_OS_WIN32
static void
on_font_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
{
gboolean close = TRUE;
switch (response)
{
case GTK_RESPONSE_APPLY:
case GTK_RESPONSE_OK:
{
gchar *fontname;
fontname = gtk_font_selection_dialog_get_font_name(
GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel));
ui_set_editor_font(fontname);
g_free(fontname);
close = (response == GTK_RESPONSE_OK);
break;
}
}
if (close)
gtk_widget_hide(ui_widgets.open_fontsel);
}
#endif
/* This shows the font selection dialog to choose a font. */
void dialogs_show_open_font()
{
#ifdef G_OS_WIN32
win32_show_font_dialog();
#else
if (ui_widgets.open_fontsel == NULL)
{
GtkWidget *apply_button;
ui_widgets.open_fontsel = gtk_font_selection_dialog_new(_("Choose font"));;
gtk_container_set_border_width(GTK_CONTAINER(ui_widgets.open_fontsel), 4);
gtk_window_set_modal(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
gtk_window_set_destroy_with_parent(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(ui_widgets.open_fontsel), TRUE);
gtk_window_set_type_hint(GTK_WINDOW(ui_widgets.open_fontsel), GDK_WINDOW_TYPE_HINT_DIALOG);
gtk_widget_set_name(ui_widgets.open_fontsel, "GeanyDialog");
#if GTK_CHECK_VERSION(2, 20, 0)
/* apply button doesn't have a getter and is hidden by default, but we'd like to show it */
apply_button = gtk_dialog_get_widget_for_response(GTK_DIALOG(ui_widgets.open_fontsel), GTK_RESPONSE_APPLY);
#else
apply_button = GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel)->apply_button;
#endif
if (apply_button)
gtk_widget_show(apply_button);
g_signal_connect(ui_widgets.open_fontsel,
"delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
g_signal_connect(ui_widgets.open_fontsel,
"response", G_CALLBACK(on_font_dialog_response), NULL);
gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_fontsel), GTK_WINDOW(main_widgets.window));
}
gtk_font_selection_dialog_set_font_name(
GTK_FONT_SELECTION_DIALOG(ui_widgets.open_fontsel), interface_prefs.editor_font);
/* We make sure the dialog is visible. */
gtk_window_present(GTK_WINDOW(ui_widgets.open_fontsel));
#endif
}
static void
on_input_dialog_show(GtkDialog *dialog, GtkWidget *entry)
{
gtk_widget_grab_focus(entry);
}
static void
on_input_entry_activate(GtkEntry *entry, GtkDialog *dialog)
{
gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
}
static void
on_input_numeric_activate(GtkEntry *entry, GtkDialog *dialog)
{
gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
}
static void
on_input_dialog_response(GtkDialog *dialog, gint response, GtkWidget *entry)
{
gboolean persistent = (gboolean) GPOINTER_TO_INT(g_object_get_data(G_OBJECT(dialog), "has_combo"));
if (response == GTK_RESPONSE_ACCEPT)
{
const gchar *str = gtk_entry_get_text(GTK_ENTRY(entry));
GeanyInputCallback input_cb =
(GeanyInputCallback) g_object_get_data(G_OBJECT(dialog), "input_cb");
if (persistent)
{
GtkWidget *combo = (GtkWidget *) g_object_get_data(G_OBJECT(dialog), "combo");
ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(combo), str, 0);
}
input_cb(str);
}
gtk_widget_hide(GTK_WIDGET(dialog));
}
static void add_input_widgets(GtkWidget *dialog, GtkWidget *vbox,
const gchar *label_text, const gchar *default_text, gboolean persistent,
GCallback insert_text_cb)
{
GtkWidget *entry;
if (label_text)
{
GtkWidget *label = gtk_label_new(label_text);
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
gtk_container_add(GTK_CONTAINER(vbox), label);
}
if (persistent) /* remember previous entry text in a combo box */
{
GtkWidget *combo = gtk_combo_box_text_new_with_entry();