/
ui_utils.c
1243 lines (1040 loc) · 40 KB
/
ui_utils.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
/*
* ui_utils.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2006-2007 Enrico Troeger <enrico.troeger@uvena.de>
* Copyright 2006-2007 Nick Treleaven <nick.treleaven@btinternet.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.
*
* $Id$
*/
#include "geany.h"
#include <string.h>
#include "ui_utils.h"
#include "sciwrappers.h"
#include "document.h"
#include "support.h"
#include "msgwindow.h"
#include "utils.h"
#include "callbacks.h"
#include "encodings.h"
#include "images.c"
#include "treeviews.h"
#include "symbols.h"
static gchar *menu_item_get_text(GtkMenuItem *menu_item);
static void update_recent_menu();
static void recent_file_loaded(const gchar *utf8_filename);
static void
recent_file_activate_cb (GtkMenuItem *menuitem,
gpointer user_data);
/* allow_override is TRUE if text can be ignored when another message has been set
* that didn't use allow_override and has not timed out. */
static void set_statusbar(const gchar *text, gboolean allow_override)
{
static glong last_time = 0;
GTimeVal timeval;
const gint GEANY_STATUS_TIMEOUT = 1;
g_get_current_time(&timeval);
if (! allow_override)
{
gtk_statusbar_pop(GTK_STATUSBAR(app->statusbar), 1);
gtk_statusbar_push(GTK_STATUSBAR(app->statusbar), 1, text);
last_time = timeval.tv_sec;
}
else
if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
{
gtk_statusbar_pop(GTK_STATUSBAR(app->statusbar), 1);
gtk_statusbar_push(GTK_STATUSBAR(app->statusbar), 1, text);
}
}
// Display text on the statusbar (without logging it to the Status window).
void ui_set_statusbar(const gchar *format, ...)
{
gchar string[512];
va_list args;
va_start(args, format);
g_vsnprintf(string, 512, format, args);
va_end(args);
set_statusbar(string, FALSE);
}
/* updates the status bar document statistics */
void ui_update_statusbar(gint idx, gint pos)
{
gchar *text;
const gchar *cur_tag;
guint line, col;
if (idx == -1) idx = document_get_cur_idx();
if (idx >= 0 && doc_list[idx].is_valid)
{
utils_get_current_function(idx, &cur_tag);
if (pos == -1) pos = sci_get_current_position(doc_list[idx].sci);
line = sci_get_line_from_position(doc_list[idx].sci, pos);
// Add temporary fix for sci infinite loop in Document::GetColumn(int)
// when current pos is beyond document end (can occur when removing
// blocks of selected lines especially esp. brace sections near end of file).
if (pos <= sci_get_length(doc_list[idx].sci))
col = sci_get_col_from_position(doc_list[idx].sci, pos);
else
col = 0;
text = g_strdup_printf(_("%c line: % 4d column: % 3d selection: % 4d %s mode: %s%s cur. function: %s encoding: %s %s filetype: %s"),
(doc_list[idx].changed) ? 42 : 32,
(line + 1), col,
sci_get_selected_text_length(doc_list[idx].sci) - 1,
sci_get_overtype(doc_list[idx].sci) ? _("OVR") : _("INS"),
document_get_eol_mode(idx),
(doc_list[idx].readonly) ? ", read only" : "",
cur_tag,
(doc_list[idx].encoding) ? doc_list[idx].encoding : _("unknown"),
(encodings_is_unicode_charset(doc_list[idx].encoding)) ? ((doc_list[idx].has_bom) ? _("(with BOM)") : _("(without BOM)")) : "",
(doc_list[idx].file_type) ? doc_list[idx].file_type->title : _("unknown"));
set_statusbar(text, TRUE); // can be overridden by status messages
g_free(text);
}
else
{
set_statusbar("", TRUE); // can be overridden by status messages
}
}
/* This sets the window title according to the current filename. */
void ui_set_window_title(gint index)
{
gchar *title;
if (index >= 0)
{
title = g_strdup_printf("Geany: %s %s",
DOC_FILENAME(index),
doc_list[index].changed ? _("(Unsaved)") : "");
gtk_window_set_title(GTK_WINDOW(app->window), title);
g_free(title);
}
else
gtk_window_set_title(GTK_WINDOW(app->window), "Geany");
}
void ui_set_editor_font(const gchar *font_name)
{
guint i;
gint size;
gchar *fname;
PangoFontDescription *font_desc;
g_return_if_fail(font_name != NULL);
// do nothing if font has not changed
if (app->editor_font != NULL)
if (strcmp(font_name, app->editor_font) == 0) return;
g_free(app->editor_font);
app->editor_font = g_strdup(font_name);
font_desc = pango_font_description_from_string(app->editor_font);
fname = g_strdup_printf("!%s", pango_font_description_get_family(font_desc));
size = pango_font_description_get_size(font_desc) / PANGO_SCALE;
/* We copy the current style, and update the font in all open tabs. */
for(i = 0; i < doc_array->len; i++)
{
if (doc_list[i].sci)
{
document_set_font(i, fname, size);
}
}
pango_font_description_free(font_desc);
msgwin_status_add(_("Font updated (%s)."), app->editor_font);
g_free(fname);
}
void ui_set_fullscreen()
{
if (app->fullscreen)
{
gtk_window_fullscreen(GTK_WINDOW(app->window));
}
else
{
gtk_window_unfullscreen(GTK_WINDOW(app->window));
}
}
static gboolean recreate_tag_list(gint idx)
{
GList *tmp;
const GList *tags;
GtkTreeIter iter;
GtkTreeModel *model;
tags = symbols_get_tag_list(idx, tm_tag_max_t);
if (doc_list[idx].tm_file == NULL || tags == NULL) return FALSE;
doc_list[idx].has_tags = TRUE;
gtk_tree_store_clear(doc_list[idx].tag_store);
// unref the store to speed up the filling(from TreeView Tutorial)
model = gtk_tree_view_get_model(GTK_TREE_VIEW(doc_list[idx].tag_tree));
g_object_ref(model); // Make sure the model stays with us after the tree view unrefs it
gtk_tree_view_set_model(GTK_TREE_VIEW(doc_list[idx].tag_tree), NULL); // Detach model from view
treeviews_init_tag_list(idx);
for (tmp = (GList*)tags; tmp; tmp = g_list_next(tmp))
{
switch (((GeanySymbol*)tmp->data)->type)
{
case tm_tag_prototype_t:
case tm_tag_method_t:
case tm_tag_function_t:
{
if (tv.tag_function.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_function));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
case tm_tag_macro_t:
case tm_tag_macro_with_arg_t:
{
if (tv.tag_macro.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_macro));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
case tm_tag_class_t:
{
if (tv.tag_class.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_class));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
case tm_tag_member_t:
case tm_tag_field_t:
{
if (tv.tag_member.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_member));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
case tm_tag_typedef_t:
case tm_tag_enum_t:
case tm_tag_union_t:
case tm_tag_struct_t:
case tm_tag_interface_t:
{
if (tv.tag_struct.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_struct));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
case tm_tag_variable_t:
{
if (tv.tag_variable.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_variable));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
case tm_tag_namespace_t:
case tm_tag_package_t:
{
if (tv.tag_namespace.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_namespace));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
break;
}
default:
{
if (tv.tag_other.stamp == -1) break;
gtk_tree_store_append(doc_list[idx].tag_store, &iter, &(tv.tag_other));
gtk_tree_store_set(doc_list[idx].tag_store, &iter, 0, ((GeanySymbol*)tmp->data)->str, -1);
}
}
}
gtk_tree_view_set_model(GTK_TREE_VIEW(doc_list[idx].tag_tree), model); // Re-attach model to view
g_object_unref(model);
gtk_tree_view_expand_all(GTK_TREE_VIEW(doc_list[idx].tag_tree));
return TRUE;
}
// update = rescan the tags for document[idx].filename
void ui_update_tag_list(gint idx, gboolean update)
{
if (gtk_bin_get_child(GTK_BIN(app->tagbar)))
gtk_container_remove(GTK_CONTAINER(app->tagbar), gtk_bin_get_child(GTK_BIN(app->tagbar)));
if (app->default_tag_tree == NULL)
{
GtkTreeIter iter;
GtkTreeStore *store = gtk_tree_store_new(1, G_TYPE_STRING);
app->default_tag_tree = gtk_tree_view_new();
treeviews_prepare_taglist(app->default_tag_tree, store);
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, _("No tags found"), -1);
gtk_widget_show(app->default_tag_tree);
g_object_ref((gpointer)app->default_tag_tree); // to hold it after removing
}
// make all inactive, because there is no more tab left, or something strange occured
if (idx == -1 || doc_list[idx].file_type == NULL || ! doc_list[idx].file_type->has_tags)
{
gtk_widget_set_sensitive(app->tagbar, FALSE);
gtk_container_add(GTK_CONTAINER(app->tagbar), app->default_tag_tree);
return;
}
if (update)
{ // updating the tag list in the left tag window
if (doc_list[idx].tag_tree == NULL)
{
doc_list[idx].tag_store = gtk_tree_store_new(1, G_TYPE_STRING);
doc_list[idx].tag_tree = gtk_tree_view_new();
treeviews_prepare_taglist(doc_list[idx].tag_tree, doc_list[idx].tag_store);
gtk_widget_show(doc_list[idx].tag_tree);
g_object_ref((gpointer)doc_list[idx].tag_tree); // to hold it after removing
}
if (recreate_tag_list(idx))
{
gtk_widget_set_sensitive(app->tagbar, TRUE);
gtk_container_add(GTK_CONTAINER(app->tagbar), doc_list[idx].tag_tree);
}
else
{ // no tags
gtk_widget_set_sensitive(app->tagbar, FALSE);
gtk_container_add(GTK_CONTAINER(app->tagbar), app->default_tag_tree);
}
}
else
{ // update == FALSE
if (doc_list[idx].has_tags)
{
gtk_widget_set_sensitive(app->tagbar, TRUE);
gtk_container_add(GTK_CONTAINER(app->tagbar), doc_list[idx].tag_tree);
}
else
{
gtk_widget_set_sensitive(app->tagbar, FALSE);
gtk_container_add(GTK_CONTAINER(app->tagbar), app->default_tag_tree);
}
}
}
void ui_update_popup_reundo_items(gint index)
{
gboolean enable_undo;
gboolean enable_redo;
if (index == -1)
{
enable_undo = FALSE;
enable_redo = FALSE;
}
else
{
enable_undo = document_can_undo(index);
enable_redo = document_can_redo(index);
}
// index 0 is the popup menu, 1 is the menubar, 2 is the toolbar
gtk_widget_set_sensitive(app->undo_items[0], enable_undo);
gtk_widget_set_sensitive(app->undo_items[1], enable_undo);
gtk_widget_set_sensitive(app->undo_items[2], enable_undo);
gtk_widget_set_sensitive(app->redo_items[0], enable_redo);
gtk_widget_set_sensitive(app->redo_items[1], enable_redo);
gtk_widget_set_sensitive(app->redo_items[2], enable_redo);
}
void ui_update_popup_copy_items(gint index)
{
gboolean enable;
guint i;
if (index == -1) enable = FALSE;
else enable = sci_can_copy(doc_list[index].sci);
for(i = 0; i < (sizeof(app->popup_items)/sizeof(GtkWidget*)); i++)
gtk_widget_set_sensitive(app->popup_items[i], enable);
}
void ui_update_popup_goto_items(gboolean enable)
{
gtk_widget_set_sensitive(app->popup_goto_items[0], enable);
gtk_widget_set_sensitive(app->popup_goto_items[1], enable);
gtk_widget_set_sensitive(app->popup_goto_items[2], enable);
}
void ui_update_menu_copy_items(gint idx)
{
gboolean enable = FALSE;
guint i;
GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(app->window));
if (IS_SCINTILLA(focusw))
enable = (idx == -1) ? FALSE : sci_can_copy(doc_list[idx].sci);
else
if (GTK_IS_EDITABLE(focusw))
enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
else
if (GTK_IS_TEXT_VIEW(focusw))
{
GtkTextBuffer *buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(focusw));
enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
}
for(i = 0; i < (sizeof(app->menu_copy_items)/sizeof(GtkWidget*)); i++)
gtk_widget_set_sensitive(app->menu_copy_items[i], enable);
}
void ui_update_insert_include_item(gint idx, gint item)
{
gboolean enable = FALSE;
if (idx == -1 || doc_list[idx].file_type == NULL) enable = FALSE;
else if (doc_list[idx].file_type->id == GEANY_FILETYPES_C ||
doc_list[idx].file_type->id == GEANY_FILETYPES_CPP)
{
enable = TRUE;
}
gtk_widget_set_sensitive(app->menu_insert_include_item[item], enable);
}
void ui_update_fold_items()
{
gtk_widget_set_sensitive(lookup_widget(app->window, "menu_fold_all1"), app->pref_editor_folding);
gtk_widget_set_sensitive(lookup_widget(app->window, "menu_unfold_all1"), app->pref_editor_folding);
}
static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
{
guint i = 0;
GtkWidget *tmp_menu;
GtkWidget *tmp_popup;
GtkWidget *edit_menu, *edit_menu_item;
GtkWidget *popup_menu, *popup_menu_item;
edit_menu = gtk_menu_new();
popup_menu = gtk_menu_new();
edit_menu_item = gtk_menu_item_new_with_label(label);
popup_menu_item = gtk_menu_item_new_with_label(label);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
while (includes[i] != NULL)
{
tmp_menu = gtk_menu_item_new_with_label(includes[i]);
tmp_popup = gtk_menu_item_new_with_label(includes[i]);
gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
g_signal_connect((gpointer) tmp_menu, "activate",
G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
g_signal_connect((gpointer) tmp_popup, "activate",
G_CALLBACK(on_insert_include_activate), (gpointer) includes[i]);
i++;
}
gtk_widget_show_all(edit_menu_item);
gtk_widget_show_all(popup_menu_item);
gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
}
void ui_create_insert_menu_items()
{
GtkMenu *menu_edit = GTK_MENU(lookup_widget(app->window, "insert_include2_menu"));
GtkMenu *menu_popup = GTK_MENU(lookup_widget(app->popup_menu, "insert_include1_menu"));
GtkWidget *blank;
const gchar *c_includes_stdlib[] = {
"assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
"signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
};
const gchar *c_includes_c99[] = {
"complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
"tgmath.h", "wchar.h", "wctype.h", NULL
};
const gchar *c_includes_cpp[] = {
"cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
};
const gchar *c_includes_cppstdlib[] = {
"iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
"memory", "locale", NULL
};
const gchar *c_includes_stl[] = {
"bitset", "dequev", "list", "map", "set", "queue", "stack", "vector", "algorithm",
"iterator", "functional", "string", "complex", "valarray", NULL
};
blank = gtk_menu_item_new_with_label("#include \"...\"");
gtk_container_add(GTK_CONTAINER(menu_edit), blank);
gtk_widget_show(blank);
g_signal_connect((gpointer) blank, "activate", G_CALLBACK(on_menu_insert_include_activate),
(gpointer) "blank");
blank = gtk_separator_menu_item_new ();
gtk_container_add(GTK_CONTAINER(menu_edit), blank);
gtk_widget_show(blank);
blank = gtk_menu_item_new_with_label("#include \"...\"");
gtk_container_add(GTK_CONTAINER(menu_popup), blank);
gtk_widget_show(blank);
g_signal_connect((gpointer) blank, "activate", G_CALLBACK(on_insert_include_activate),
(gpointer) "blank");
blank = gtk_separator_menu_item_new();
gtk_container_add(GTK_CONTAINER(menu_popup), blank);
gtk_widget_show(blank);
insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
}
static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
{
GtkWidget *item;
item = gtk_menu_item_new_with_label(label);
gtk_container_add(GTK_CONTAINER(me), item);
gtk_widget_show(item);
g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
item = gtk_menu_item_new_with_label(label);
gtk_container_add(GTK_CONTAINER(mp), item);
gtk_widget_show(item);
g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_insert_date_activate), label);
}
void ui_create_insert_date_menu_items()
{
GtkMenu *menu_edit = GTK_MENU(lookup_widget(app->window, "insert_date1_menu"));
GtkMenu *menu_popup = GTK_MENU(lookup_widget(app->popup_menu, "insert_date2_menu"));
GtkWidget *item;
gchar *str;
insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
item = gtk_separator_menu_item_new();
gtk_container_add(GTK_CONTAINER(menu_edit), item);
gtk_widget_show(item);
item = gtk_separator_menu_item_new();
gtk_container_add(GTK_CONTAINER(menu_popup), item);
gtk_widget_show(item);
insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
item = gtk_separator_menu_item_new();
gtk_container_add(GTK_CONTAINER(menu_edit), item);
gtk_widget_show(item);
item = gtk_separator_menu_item_new();
gtk_container_add(GTK_CONTAINER(menu_popup), item);
gtk_widget_show(item);
str = _("Use Custom Date Format");
item = gtk_menu_item_new_with_label(str);
gtk_container_add(GTK_CONTAINER(menu_edit), item);
gtk_widget_show(item);
g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_menu_insert_date_activate),
str);
g_object_set_data_full(G_OBJECT(app->window), "insert_date_custom1", gtk_widget_ref(item),
(GDestroyNotify)gtk_widget_unref);
item = gtk_menu_item_new_with_label(str);
gtk_container_add(GTK_CONTAINER(menu_popup), item);
gtk_widget_show(item);
g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_insert_date_activate),
str);
g_object_set_data_full(G_OBJECT(app->popup_menu), "insert_date_custom2", gtk_widget_ref(item),
(GDestroyNotify)gtk_widget_unref);
insert_date_items(menu_edit, menu_popup, _("Set Custom Date Format"));
}
void ui_save_buttons_toggle(gboolean enable)
{
guint i;
gboolean dirty_tabs = FALSE;
gtk_widget_set_sensitive(app->save_buttons[0], enable);
gtk_widget_set_sensitive(app->save_buttons[1], enable);
// save all menu item and tool button
for (i = 0; i < (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook)); i++)
{
// count the amount of files where changes were made and if there are some,
// we need the save all button / item
if (! dirty_tabs && doc_list[i].is_valid && doc_list[i].changed)
dirty_tabs = TRUE;
}
gtk_widget_set_sensitive(app->save_buttons[2], (dirty_tabs > 0) ? TRUE : FALSE);
gtk_widget_set_sensitive(app->save_buttons[3], (dirty_tabs > 0) ? TRUE : FALSE);
}
void ui_close_buttons_toggle()
{
guint i;
gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook)) ? TRUE : FALSE;
for(i = 0; i < (sizeof(app->sensitive_buttons)/sizeof(GtkWidget*)); i++)
gtk_widget_set_sensitive(app->sensitive_buttons[i], enable);
}
void ui_widget_show_hide(GtkWidget *widget, gboolean show)
{
if (show)
{
gtk_widget_show(widget);
}
else
{
gtk_widget_hide(widget);
}
}
void ui_treeviews_show_hide(G_GNUC_UNUSED gboolean force)
{
GtkWidget *widget;
/* geany_debug("\nSidebar: %s\nSymbol: %s\nFiles: %s", ui_btoa(app->sidebar_visible),
ui_btoa(app->sidebar_symbol_visible), ui_btoa(app->sidebar_openfiles_visible));
*/
if (! app->sidebar_openfiles_visible && ! app->sidebar_symbol_visible)
{
app->sidebar_visible = FALSE;
}
widget = lookup_widget(app->window, "menu_show_sidebar1");
if (app->sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
{
app->ignore_callback = TRUE;
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), app->sidebar_visible);
app->ignore_callback = FALSE;
}
ui_widget_show_hide(app->treeview_notebook, app->sidebar_visible);
ui_widget_show_hide(gtk_notebook_get_nth_page(
GTK_NOTEBOOK(app->treeview_notebook), 0), app->sidebar_symbol_visible);
ui_widget_show_hide(gtk_notebook_get_nth_page(
GTK_NOTEBOOK(app->treeview_notebook), 1), app->sidebar_openfiles_visible);
}
void ui_document_show_hide(gint idx)
{
gchar *widget_name;
if (idx == -1 || ! doc_list[idx].is_valid) return;
app->ignore_callback = TRUE;
gtk_check_menu_item_set_active(
GTK_CHECK_MENU_ITEM(lookup_widget(app->window, "menu_line_breaking1")),
doc_list[idx].line_breaking);
gtk_check_menu_item_set_active(
GTK_CHECK_MENU_ITEM(lookup_widget(app->window, "menu_use_auto_indention1")),
doc_list[idx].use_auto_indention);
gtk_check_menu_item_set_active(
GTK_CHECK_MENU_ITEM(lookup_widget(app->window, "set_file_readonly1")),
doc_list[idx].readonly);
gtk_check_menu_item_set_active(
GTK_CHECK_MENU_ITEM(lookup_widget(app->window, "menu_write_unicode_bom1")),
doc_list[idx].has_bom);
switch (sci_get_eol_mode(doc_list[idx].sci))
{
case SC_EOL_CR: widget_name = "cr"; break;
case SC_EOL_LF: widget_name = "lf"; break;
default: widget_name = "crlf"; break;
}
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(lookup_widget(app->window, widget_name)),
TRUE);
gtk_widget_set_sensitive(lookup_widget(app->window, "menu_write_unicode_bom1"),
encodings_is_unicode_charset(doc_list[idx].encoding));
encodings_select_radio_item(doc_list[idx].encoding);
filetypes_select_radio_item(doc_list[idx].file_type);
app->ignore_callback = FALSE;
}
void ui_update_toolbar_icons(GtkIconSize size)
{
GtkWidget *button_image = NULL;
GtkWidget *widget = NULL;
GtkWidget *oldwidget = NULL;
// destroy old widget
widget = lookup_widget(app->window, "toolbutton22");
oldwidget = gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(widget));
if (oldwidget && GTK_IS_WIDGET(oldwidget)) gtk_widget_destroy(oldwidget);
// create new widget
button_image = ui_new_image_from_inline(GEANY_IMAGE_SAVE_ALL, FALSE);
gtk_widget_show(button_image);
gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(widget), button_image);
gtk_toolbar_set_icon_size(GTK_TOOLBAR(app->toolbar), size);
}
void ui_update_toolbar_items()
{
// show toolbar
GtkWidget *widget = lookup_widget(app->window, "menu_show_toolbar1");
if (app->toolbar_visible && ! gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
{
app->toolbar_visible = ! app->toolbar_visible; // will be changed by the toggled callback
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), TRUE);
}
else if (! app->toolbar_visible && gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
{
app->toolbar_visible = ! app->toolbar_visible; // will be changed by the toggled callback
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), FALSE);
}
// fileops
ui_widget_show_hide(lookup_widget(app->window, "menutoolbutton1"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton9"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton10"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton22"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton23"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton15"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem7"), app->pref_toolbar_show_fileops);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem2"), app->pref_toolbar_show_fileops);
// search
ui_widget_show_hide(lookup_widget(app->window, "entry1"), app->pref_toolbar_show_search);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton18"), app->pref_toolbar_show_search);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem5"), app->pref_toolbar_show_search);
// goto line
ui_widget_show_hide(lookup_widget(app->window, "entry_goto_line"), app->pref_toolbar_show_goto);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton25"), app->pref_toolbar_show_goto);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem8"), app->pref_toolbar_show_goto);
// compile
ui_widget_show_hide(lookup_widget(app->window, "toolbutton13"), app->pref_toolbar_show_compile);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton26"), app->pref_toolbar_show_compile);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem6"), app->pref_toolbar_show_compile);
// colour
ui_widget_show_hide(lookup_widget(app->window, "toolbutton24"), app->pref_toolbar_show_colour);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem3"), app->pref_toolbar_show_colour);
// zoom
ui_widget_show_hide(lookup_widget(app->window, "toolbutton20"), app->pref_toolbar_show_zoom);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton21"), app->pref_toolbar_show_zoom);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem4"), app->pref_toolbar_show_zoom);
// undo
ui_widget_show_hide(lookup_widget(app->window, "toolbutton_undo"), app->pref_toolbar_show_undo);
ui_widget_show_hide(lookup_widget(app->window, "toolbutton_redo"), app->pref_toolbar_show_undo);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem9"), app->pref_toolbar_show_undo);
// quit
ui_widget_show_hide(lookup_widget(app->window, "toolbutton19"), app->pref_toolbar_show_quit);
ui_widget_show_hide(lookup_widget(app->window, "separatortoolitem8"), app->pref_toolbar_show_quit);
}
// Note: remember to unref the pixbuf once an image or window has added a reference.
GdkPixbuf *ui_new_pixbuf_from_inline(gint img, gboolean small_img)
{
switch(img)
{
case GEANY_IMAGE_SMALL_CROSS: return gdk_pixbuf_new_from_inline(-1, close_small_inline, FALSE, NULL); break;
case GEANY_IMAGE_LOGO: return gdk_pixbuf_new_from_inline(-1, aladin_inline, FALSE, NULL); break;
case GEANY_IMAGE_SAVE_ALL:
{
if ((app->toolbar_icon_size == GTK_ICON_SIZE_SMALL_TOOLBAR) || small_img)
{
return gdk_pixbuf_scale_simple(gdk_pixbuf_new_from_inline(-1, save_all_inline, FALSE, NULL),
16, 16, GDK_INTERP_HYPER);
}
else
{
return gdk_pixbuf_new_from_inline(-1, save_all_inline, FALSE, NULL);
}
break;
}
case GEANY_IMAGE_NEW_ARROW:
{
if ((app->toolbar_icon_size == GTK_ICON_SIZE_SMALL_TOOLBAR) || small_img)
{
return gdk_pixbuf_scale_simple(gdk_pixbuf_new_from_inline(-1, newfile_inline, FALSE, NULL),
16, 16, GDK_INTERP_HYPER);
}
else
{
return gdk_pixbuf_new_from_inline(-1, newfile_inline, FALSE, NULL);
}
break;
}
default: return NULL;
}
//return gtk_image_new_from_pixbuf(pixbuf);
}
GtkWidget *ui_new_image_from_inline(gint img, gboolean small_img)
{
GtkWidget *wid;
GdkPixbuf *pb;
pb = ui_new_pixbuf_from_inline(img, small_img);
wid = gtk_image_new_from_pixbuf(pb);
g_object_unref(pb); // the image doesn't adopt our reference, so remove our ref.
return wid;
}
void ui_create_recent_menu()
{
GtkWidget *tmp;
guint i;
gchar *filename;
if (g_queue_get_length(app->recent_queue) == 0)
{
gtk_widget_set_sensitive(app->recent_files_menubar, FALSE);
gtk_widget_set_sensitive(app->recent_files_toolbar, FALSE);
return;
}
for (i = 0; i < MIN(app->mru_length, g_queue_get_length(app->recent_queue)); i++)
{
filename = g_queue_peek_nth(app->recent_queue, i);
// create menu item for the recent files menu in the menu bar
tmp = gtk_menu_item_new_with_label(filename);
gtk_widget_show(tmp);
gtk_menu_shell_append(GTK_MENU_SHELL(app->recent_files_menubar), tmp);
g_signal_connect((gpointer) tmp, "activate",
G_CALLBACK(recent_file_activate_cb), NULL);
// create menu item for the recent files menu in the toolbar bar
tmp = gtk_menu_item_new_with_label(filename);
gtk_widget_show(tmp);
gtk_menu_shell_append(GTK_MENU_SHELL(app->recent_files_toolbar), tmp);
g_signal_connect((gpointer) tmp, "activate",
G_CALLBACK(recent_file_activate_cb), NULL);
}
}
static void
recent_file_activate_cb (GtkMenuItem *menuitem,
G_GNUC_UNUSED gpointer user_data)
{
gchar *utf8_filename = menu_item_get_text(menuitem);
gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
if (document_open_file(-1, locale_filename, 0, FALSE, NULL, NULL) > -1)
recent_file_loaded(utf8_filename);
g_free(locale_filename);
g_free(utf8_filename);
}
void ui_add_recent_file(const gchar *utf8_filename)
{
if (g_queue_find_custom(app->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
{
g_queue_push_head(app->recent_queue, g_strdup(utf8_filename));
if (g_queue_get_length(app->recent_queue) > app->mru_length)
{
g_free(g_queue_pop_tail(app->recent_queue));
}
update_recent_menu();
}
else recent_file_loaded(utf8_filename); // filename already in recent list
}
// Returns: newly allocated string with the UTF-8 menu text.
static gchar *menu_item_get_text(GtkMenuItem *menu_item)
{
const gchar *text = NULL;
if (GTK_BIN(menu_item)->child)
{
GtkWidget *child = GTK_BIN(menu_item)->child;
if (GTK_IS_LABEL(child))
text = gtk_label_get_text(GTK_LABEL(child));
}
// GTK owns text so it's much safer to return a copy of it in case the memory is reallocated
return g_strdup(text);
}
static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
{
gchar *menu_text = menu_item_get_text(GTK_MENU_ITEM(list_data));
gint result;
if (utils_str_equal(menu_text, user_data))
result = 0;
else
result = 1;
g_free(menu_text);
return result;
}
static void recent_file_loaded(const gchar *utf8_filename)
{
GList *item, *children;
void *data;
GtkWidget *tmp;
// first reorder the queue
item = g_queue_find_custom(app->recent_queue, utf8_filename, (GCompareFunc) strcmp);
g_return_if_fail(item != NULL);
data = item->data;
g_queue_remove(app->recent_queue, data);
g_queue_push_head(app->recent_queue, data);
// remove the old menuitem for the filename
children = gtk_container_get_children(GTK_CONTAINER(app->recent_files_menubar));
item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
if (item != NULL) gtk_widget_destroy(GTK_WIDGET(item->data));
children = gtk_container_get_children(GTK_CONTAINER(app->recent_files_toolbar));
item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
if (item != NULL) gtk_widget_destroy(GTK_WIDGET(item->data));
// now prepend a new menuitem for the filename, first for the recent files menu in the menu bar
tmp = gtk_menu_item_new_with_label(utf8_filename);
gtk_widget_show(tmp);
gtk_menu_shell_prepend(GTK_MENU_SHELL(app->recent_files_menubar), tmp);
g_signal_connect((gpointer) tmp, "activate",
G_CALLBACK(recent_file_activate_cb), NULL);
// then for the recent files menu in the tool bar
tmp = gtk_menu_item_new_with_label(utf8_filename);
gtk_widget_show(tmp);
gtk_menu_shell_prepend(GTK_MENU_SHELL(app->recent_files_toolbar), tmp);
g_signal_connect((gpointer) tmp, "activate",
G_CALLBACK(recent_file_activate_cb), NULL);
}
static void update_recent_menu()
{
GtkWidget *recent_files_item = lookup_widget(app->window, "recent_files1");
GtkWidget *tmp;
gchar *filename;
GList *children, *item;
if (g_queue_get_length(app->recent_queue) == 0)
{
gtk_widget_set_sensitive(recent_files_item, FALSE);
return;
}
else if (! GTK_WIDGET_SENSITIVE(recent_files_item))
{
gtk_widget_set_sensitive(recent_files_item, TRUE);
}
// clean the MRU list before adding an item (menubar)
children = gtk_container_get_children(GTK_CONTAINER(app->recent_files_menubar));
if (g_list_length(children) > app->mru_length - 1)
{
item = g_list_nth(children, app->mru_length - 1);
while (item != NULL)
{
if (GTK_IS_MENU_ITEM(item->data)) gtk_widget_destroy(GTK_WIDGET(item->data));
item = g_list_next(item);