Store "equal" tags into binary trees instead of lists in Symbol tree
[geany-mirror.git] / src / ui_utils.c
blob66693c1bc9986be78adc460ee4fadef023f82713
1 /*
2 * ui_utils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Matthew Brush <mbrush(at)codebrainz(dot)ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /** @file ui_utils.h
24 * User Interface general utility functions.
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "ui_utils.h"
33 #include "app.h"
34 #include "callbacks.h"
35 #include "dialogs.h"
36 #include "documentprivate.h"
37 #include "encodingsprivate.h"
38 #include "filetypes.h"
39 #include "geanymenubuttonaction.h"
40 #include "keyfile.h"
41 #include "main.h"
42 #include "msgwindow.h"
43 #include "prefs.h"
44 #include "project.h"
45 #include "sciwrappers.h"
46 #include "sidebar.h"
47 #include "stash.h"
48 #include "support.h"
49 #include "symbols.h"
50 #include "toolbar.h"
51 #include "utils.h"
52 #include "win32.h"
53 #include "osx.h"
55 #include "gtkcompat.h"
57 #include <string.h>
58 #include <ctype.h>
59 #include <stdarg.h>
60 #include <gdk/gdkkeysyms.h>
63 #define DEFAULT_STATUSBAR_TEMPLATE N_(\
64 "line: %l / %L\t " \
65 "col: %c\t " \
66 "sel: %s\t " \
67 "%w %t %m" \
68 "mode: %M " \
69 "encoding: %e " \
70 "filetype: %f " \
71 "scope: %S")
73 GeanyInterfacePrefs interface_prefs;
74 GeanyMainWidgets main_widgets;
76 UIPrefs ui_prefs;
77 UIWidgets ui_widgets;
79 static GtkBuilder *builder = NULL;
80 static GtkWidget* window1 = NULL;
81 static GtkWidget* toolbar_popup_menu1 = NULL;
82 static GtkWidget* edit_menu1 = NULL;
83 static GtkWidget* prefs_dialog = NULL;
84 static GtkWidget* project_dialog = NULL;
86 static struct
88 /* pointers to widgets only sensitive when there is at least one document, the pointers can
89 * also be GtkAction objects, so check each pointer before using it */
90 GPtrArray *document_buttons;
91 GtkWidget *menu_insert_include_items[2];
92 GtkWidget *popup_goto_items[4];
93 GtkWidget *popup_copy_items[3];
94 GtkWidget *menu_copy_items[3];
95 GtkWidget *redo_items[3];
96 GtkWidget *undo_items[3];
97 GtkWidget *save_buttons[4];
98 GtkWidget *config_files_menu;
100 widgets;
102 enum
104 RECENT_FILE_FILE,
105 RECENT_FILE_PROJECT
108 typedef struct
110 gint type;
111 GQueue *recent_queue;
112 GtkWidget *menubar;
113 GtkWidget *toolbar;
114 void (*activate_cb)(GtkMenuItem *, gpointer);
115 } GeanyRecentFiles;
118 static void update_recent_menu(GeanyRecentFiles *grf);
119 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
120 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
121 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
122 static GtkWidget *progress_bar_create(void);
123 static void ui_menu_sort_by_label(GtkMenu *menu);
126 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
127 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
129 if (widget != NULL)
130 gtk_widget_set_sensitive(widget, set);
134 /* allow_override is TRUE if text can be ignored when another message has been set
135 * that didn't use allow_override and has not timed out. */
136 static void set_statusbar(const gchar *text, gboolean allow_override)
138 static guint id = 0;
139 static glong last_time = 0;
140 GTimeVal timeval;
141 const gint GEANY_STATUS_TIMEOUT = 1;
143 if (! interface_prefs.statusbar_visible)
144 return; /* just do nothing if statusbar is not visible */
146 if (id == 0)
147 id = gtk_statusbar_get_context_id(GTK_STATUSBAR(ui_widgets.statusbar), "geany-main");
149 g_get_current_time(&timeval);
151 if (! allow_override)
153 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
154 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
155 last_time = timeval.tv_sec;
157 else
158 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
160 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
161 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
166 /** Displays text on the statusbar.
167 * @param log Whether the message should be recorded in the Status window.
168 * @param format A @c printf -style string. */
169 GEANY_API_SYMBOL
170 void ui_set_statusbar(gboolean log, const gchar *format, ...)
172 gchar *string;
173 va_list args;
175 va_start(args, format);
176 string = g_strdup_vprintf(format, args);
177 va_end(args);
179 if (! prefs.suppress_status_messages)
180 set_statusbar(string, FALSE);
182 if (log || prefs.suppress_status_messages)
183 msgwin_status_add("%s", string);
185 g_free(string);
189 /* note: some comments below are for translators */
190 static gchar *create_statusbar_statistics(GeanyDocument *doc,
191 guint line, guint vcol, guint pos)
193 const gchar *cur_tag;
194 const gchar *fmt;
195 const gchar *expos; /* % expansion position */
196 const gchar sp[] = " ";
197 GString *stats_str;
198 ScintillaObject *sci = doc->editor->sci;
200 if (!EMPTY(ui_prefs.statusbar_template))
201 fmt = ui_prefs.statusbar_template;
202 else
203 fmt = _(DEFAULT_STATUSBAR_TEMPLATE);
205 stats_str = g_string_sized_new(120);
207 while ((expos = strchr(fmt, '%')) != NULL)
209 /* append leading text before % char */
210 g_string_append_len(stats_str, fmt, expos - fmt);
212 switch (*++expos)
214 case 'l':
215 g_string_append_printf(stats_str, "%d", line + 1);
216 break;
217 case 'L':
218 g_string_append_printf(stats_str, "%d",
219 sci_get_line_count(doc->editor->sci));
220 break;
221 case 'c':
222 g_string_append_printf(stats_str, "%d", vcol);
223 break;
224 case 'C':
225 g_string_append_printf(stats_str, "%d", vcol + 1);
226 break;
227 case 'p':
228 g_string_append_printf(stats_str, "%u", pos);
229 break;
230 case 's':
232 gint len = sci_get_selected_text_length(sci) - 1;
233 /* check if whole lines are selected */
234 if (!len || sci_get_col_from_position(sci,
235 sci_get_selection_start(sci)) != 0 ||
236 sci_get_col_from_position(sci,
237 sci_get_selection_end(sci)) != 0)
238 g_string_append_printf(stats_str, "%d", len);
239 else /* L = lines */
240 g_string_append_printf(stats_str, _("%dL"),
241 sci_get_lines_selected(doc->editor->sci) - 1);
242 break;
244 case 'n' :
245 g_string_append_printf(stats_str, "%d",
246 sci_get_selected_text_length(doc->editor->sci) - 1);
247 break;
248 case 'w':
249 /* RO = read-only */
250 g_string_append(stats_str, (doc->readonly) ? _("RO ") :
251 /* OVR = overwrite/overtype, INS = insert */
252 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
253 break;
254 case 'r':
255 if (doc->readonly)
257 g_string_append(stats_str, _("RO ")); /* RO = read-only */
258 g_string_append(stats_str, sp + 1);
260 break;
261 case 't':
263 switch (editor_get_indent_prefs(doc->editor)->type)
265 case GEANY_INDENT_TYPE_TABS:
266 g_string_append(stats_str, _("TAB"));
267 break;
268 case GEANY_INDENT_TYPE_SPACES: /* SP = space */
269 g_string_append(stats_str, _("SP"));
270 break;
271 case GEANY_INDENT_TYPE_BOTH: /* T/S = tabs and spaces */
272 g_string_append(stats_str, _("T/S"));
273 break;
275 break;
277 case 'm':
278 if (doc->changed)
280 g_string_append(stats_str, _("MOD")); /* MOD = modified */
281 g_string_append(stats_str, sp);
283 break;
284 case 'M':
285 g_string_append(stats_str, utils_get_eol_short_name(sci_get_eol_mode(doc->editor->sci)));
286 break;
287 case 'e':
288 g_string_append(stats_str,
289 doc->encoding ? doc->encoding : _("unknown"));
290 if (encodings_is_unicode_charset(doc->encoding) && (doc->has_bom))
292 g_string_append_c(stats_str, ' ');
293 g_string_append(stats_str, _("(with BOM)")); /* BOM = byte order mark */
295 break;
296 case 'f':
297 g_string_append(stats_str, filetypes_get_display_name(doc->file_type));
298 break;
299 case 'S':
300 symbols_get_current_scope(doc, &cur_tag);
301 g_string_append(stats_str, cur_tag);
302 break;
303 case 'Y':
304 g_string_append_c(stats_str, ' ');
305 g_string_append_printf(stats_str, "%d",
306 sci_get_style_at(doc->editor->sci, pos));
307 break;
308 default:
309 g_string_append_len(stats_str, expos, 1);
312 /* skip past %c chars */
313 if (*expos)
314 fmt = expos + 1;
315 else
316 break;
318 /* add any remaining text */
319 g_string_append(stats_str, fmt);
321 return g_string_free(stats_str, FALSE);
325 /* updates the status bar document statistics */
326 void ui_update_statusbar(GeanyDocument *doc, gint pos)
328 g_return_if_fail(doc == NULL || doc->is_valid);
330 if (! interface_prefs.statusbar_visible)
331 return; /* just do nothing if statusbar is not visible */
333 if (doc == NULL)
334 doc = document_get_current();
336 if (doc != NULL)
338 guint line, vcol;
339 gchar *stats_str;
341 if (pos == -1)
342 pos = sci_get_current_position(doc->editor->sci);
343 line = sci_get_line_from_position(doc->editor->sci, pos);
345 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
346 * when current pos is beyond document end (can occur when removing
347 * blocks of selected lines especially esp. brace sections near end of file). */
348 if (pos <= sci_get_length(doc->editor->sci))
349 vcol = sci_get_col_from_position(doc->editor->sci, pos);
350 else
351 vcol = 0;
352 vcol += sci_get_cursor_virtual_space(doc->editor->sci);
354 stats_str = create_statusbar_statistics(doc, line, vcol, pos);
356 /* can be overridden by status messages */
357 set_statusbar(stats_str, TRUE);
358 g_free(stats_str);
360 else /* no documents */
362 set_statusbar("", TRUE); /* can be overridden by status messages */
367 /* This sets the window title according to the current filename. */
368 void ui_set_window_title(GeanyDocument *doc)
370 GString *str;
371 GeanyProject *project = app->project;
373 g_return_if_fail(doc == NULL || doc->is_valid);
375 if (doc == NULL)
376 doc = document_get_current();
378 str = g_string_new(NULL);
380 if (doc != NULL)
382 g_string_append(str, doc->changed ? "*" : "");
384 if (doc->file_name == NULL)
385 g_string_append(str, DOC_FILENAME(doc));
386 else
388 gchar *short_name = document_get_basename_for_display(doc, 30);
389 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
391 g_string_append(str, short_name);
392 g_string_append(str, " - ");
393 g_string_append(str, dirname ? dirname : "");
394 g_free(short_name);
395 g_free(dirname);
397 g_string_append(str, " - ");
399 if (project)
401 g_string_append_c(str, '[');
402 g_string_append(str, project->name);
403 g_string_append(str, "] - ");
405 g_string_append(str, "Geany");
406 if (cl_options.new_instance)
408 g_string_append(str, _(" (new instance)"));
410 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
411 g_string_free(str, TRUE);
415 void ui_set_editor_font(const gchar *font_name)
417 guint i;
419 g_return_if_fail(font_name != NULL);
421 /* do nothing if font has not changed */
422 if (interface_prefs.editor_font != NULL)
423 if (strcmp(font_name, interface_prefs.editor_font) == 0)
424 return;
426 g_free(interface_prefs.editor_font);
427 interface_prefs.editor_font = g_strdup(font_name);
429 /* We copy the current style, and update the font in all open tabs. */
430 for (i = 0; i < documents_array->len; i++)
432 if (documents[i]->editor)
434 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
438 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
442 void ui_set_fullscreen(void)
444 if (ui_prefs.fullscreen)
446 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
448 else
450 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
455 void ui_update_popup_reundo_items(GeanyDocument *doc)
457 gboolean enable_undo;
458 gboolean enable_redo;
459 guint i, len;
461 g_return_if_fail(doc == NULL || doc->is_valid);
463 if (doc == NULL)
465 enable_undo = FALSE;
466 enable_redo = FALSE;
468 else
470 enable_undo = document_can_undo(doc);
471 enable_redo = document_can_redo(doc);
474 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
475 len = G_N_ELEMENTS(widgets.undo_items);
476 for (i = 0; i < len; i++)
478 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
480 len = G_N_ELEMENTS(widgets.redo_items);
481 for (i = 0; i < len; i++)
483 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
488 void ui_update_popup_copy_items(GeanyDocument *doc)
490 gboolean enable;
491 guint i, len;
493 g_return_if_fail(doc == NULL || doc->is_valid);
495 if (doc == NULL)
496 enable = FALSE;
497 else
498 enable = sci_has_selection(doc->editor->sci);
500 len = G_N_ELEMENTS(widgets.popup_copy_items);
501 for (i = 0; i < len; i++)
502 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
506 void ui_update_popup_goto_items(gboolean enable)
508 guint i, len;
509 len = G_N_ELEMENTS(widgets.popup_goto_items);
510 for (i = 0; i < len; i++)
511 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
515 void ui_update_menu_copy_items(GeanyDocument *doc)
517 gboolean enable = FALSE;
518 guint i, len;
519 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
521 g_return_if_fail(doc == NULL || doc->is_valid);
523 if (IS_SCINTILLA(focusw))
524 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
525 else
526 if (GTK_IS_EDITABLE(focusw))
527 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
528 else
529 if (GTK_IS_TEXT_VIEW(focusw))
531 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
532 GTK_TEXT_VIEW(focusw));
533 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
536 len = G_N_ELEMENTS(widgets.menu_copy_items);
537 for (i = 0; i < len; i++)
538 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
542 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
544 gboolean enable = FALSE;
546 g_return_if_fail(doc == NULL || doc->is_valid);
548 if (doc == NULL || doc->file_type == NULL)
549 enable = FALSE;
550 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
551 enable = TRUE;
553 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
557 void ui_update_fold_items(void)
559 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
560 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
561 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
565 /* @include include name or NULL for empty with cursor ready for typing it */
566 static void insert_include(GeanyDocument *doc, gint pos, const gchar *include)
568 gint pos_after = -1;
569 gchar *text;
571 g_return_if_fail(doc != NULL);
572 g_return_if_fail(pos == -1 || pos >= 0);
574 if (pos == -1)
575 pos = sci_get_current_position(doc->editor->sci);
577 if (! include)
579 text = g_strdup("#include \"\"\n");
580 pos_after = pos + 10;
582 else
584 text = g_strconcat("#include <", include, ">\n", NULL);
587 sci_start_undo_action(doc->editor->sci);
588 sci_insert_text(doc->editor->sci, pos, text);
589 sci_end_undo_action(doc->editor->sci);
590 g_free(text);
591 if (pos_after >= 0)
592 sci_goto_pos(doc->editor->sci, pos_after, FALSE);
596 static void on_popup_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
598 insert_include(document_get_current(), editor_info.click_pos, user_data);
602 static void on_menu_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
604 insert_include(document_get_current(), -1, user_data);
608 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
610 guint i = 0;
611 GtkWidget *edit_menu, *edit_menu_item;
612 GtkWidget *popup_menu, *popup_menu_item;
614 edit_menu = gtk_menu_new();
615 popup_menu = gtk_menu_new();
616 edit_menu_item = gtk_menu_item_new_with_label(label);
617 popup_menu_item = gtk_menu_item_new_with_label(label);
618 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
619 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
621 while (includes[i] != NULL)
623 GtkWidget *tmp_menu = gtk_menu_item_new_with_label(includes[i]);
624 GtkWidget *tmp_popup = gtk_menu_item_new_with_label(includes[i]);
626 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
627 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
628 g_signal_connect(tmp_menu, "activate",
629 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
630 g_signal_connect(tmp_popup, "activate",
631 G_CALLBACK(on_popup_insert_include_activate), (gpointer) includes[i]);
632 i++;
634 gtk_widget_show_all(edit_menu_item);
635 gtk_widget_show_all(popup_menu_item);
636 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
637 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
641 void ui_create_insert_menu_items(void)
643 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
644 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
645 GtkWidget *blank;
646 const gchar *c_includes_stdlib[] = {
647 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
648 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
650 const gchar *c_includes_c99[] = {
651 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
652 "tgmath.h", "wchar.h", "wctype.h", NULL
654 const gchar *c_includes_cpp[] = {
655 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
657 const gchar *c_includes_cppstdlib[] = {
658 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
659 "memory", "locale", NULL
661 const gchar *c_includes_stl[] = {
662 "bitset", "deque", "list", "map", "set", "queue", "stack", "vector", "algorithm",
663 "iterator", "functional", "string", "complex", "valarray", NULL
666 blank = gtk_menu_item_new_with_label("#include \"...\"");
667 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
668 gtk_widget_show(blank);
669 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate), NULL);
670 blank = gtk_separator_menu_item_new ();
671 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
672 gtk_widget_show(blank);
674 blank = gtk_menu_item_new_with_label("#include \"...\"");
675 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
676 gtk_widget_show(blank);
677 g_signal_connect(blank, "activate", G_CALLBACK(on_popup_insert_include_activate), NULL);
678 blank = gtk_separator_menu_item_new();
679 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
680 gtk_widget_show(blank);
682 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
683 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
684 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
685 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
686 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
690 static void insert_date(GeanyDocument *doc, gint pos, const gchar *date_style)
692 const gchar *format = NULL;
693 gchar *time_str;
695 g_return_if_fail(doc != NULL);
696 g_return_if_fail(pos == -1 || pos >= 0);
698 if (pos == -1)
699 pos = sci_get_current_position(doc->editor->sci);
701 /* set default value */
702 if (utils_str_equal("", ui_prefs.custom_date_format))
704 g_free(ui_prefs.custom_date_format);
705 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
708 if (utils_str_equal(_("dd.mm.yyyy"), date_style))
709 format = "%d.%m.%Y";
710 else if (utils_str_equal(_("mm.dd.yyyy"), date_style))
711 format = "%m.%d.%Y";
712 else if (utils_str_equal(_("yyyy/mm/dd"), date_style))
713 format = "%Y/%m/%d";
714 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), date_style))
715 format = "%d.%m.%Y %H:%M:%S";
716 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), date_style))
717 format = "%m.%d.%Y %H:%M:%S";
718 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), date_style))
719 format = "%Y/%m/%d %H:%M:%S";
720 else if (utils_str_equal(_("_Use Custom Date Format"), date_style))
721 format = ui_prefs.custom_date_format;
722 else
724 gchar *str = dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets.window),
725 _("Enter here a custom date and time format. "
726 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
727 ui_prefs.custom_date_format);
728 if (str)
729 SETPTR(ui_prefs.custom_date_format, str);
730 return;
733 time_str = utils_get_date_time(format, NULL);
734 if (time_str != NULL)
736 sci_start_undo_action(doc->editor->sci);
737 sci_insert_text(doc->editor->sci, pos, time_str);
738 sci_goto_pos(doc->editor->sci, pos + strlen(time_str), FALSE);
739 sci_end_undo_action(doc->editor->sci);
740 g_free(time_str);
742 else
744 utils_beep();
745 ui_set_statusbar(TRUE,
746 _("Date format string could not be converted (possibly too long)."));
751 static void on_popup_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
753 insert_date(document_get_current(), editor_info.click_pos, user_data);
757 static void on_menu_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
759 insert_date(document_get_current(), -1, user_data);
763 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
765 GtkWidget *item;
767 item = gtk_menu_item_new_with_mnemonic(label);
768 gtk_container_add(GTK_CONTAINER(me), item);
769 gtk_widget_show(item);
770 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
772 item = gtk_menu_item_new_with_mnemonic(label);
773 gtk_container_add(GTK_CONTAINER(mp), item);
774 gtk_widget_show(item);
775 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), label);
779 void ui_create_insert_date_menu_items(void)
781 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
782 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
783 GtkWidget *item;
784 gchar *str;
786 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
787 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
788 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
790 item = gtk_separator_menu_item_new();
791 gtk_container_add(GTK_CONTAINER(menu_edit), item);
792 gtk_widget_show(item);
793 item = gtk_separator_menu_item_new();
794 gtk_container_add(GTK_CONTAINER(menu_popup), item);
795 gtk_widget_show(item);
797 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
798 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
799 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
801 item = gtk_separator_menu_item_new();
802 gtk_container_add(GTK_CONTAINER(menu_edit), item);
803 gtk_widget_show(item);
804 item = gtk_separator_menu_item_new();
805 gtk_container_add(GTK_CONTAINER(menu_popup), item);
806 gtk_widget_show(item);
808 str = _("_Use Custom Date Format");
809 item = gtk_menu_item_new_with_mnemonic(str);
810 gtk_container_add(GTK_CONTAINER(menu_edit), item);
811 gtk_widget_show(item);
812 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
813 ui_hookup_widget(main_widgets.window, item, "insert_date_custom1");
815 item = gtk_menu_item_new_with_mnemonic(str);
816 gtk_container_add(GTK_CONTAINER(menu_popup), item);
817 gtk_widget_show(item);
818 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), str);
819 ui_hookup_widget(main_widgets.editor_menu, item, "insert_date_custom2");
821 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
825 void ui_save_buttons_toggle(gboolean enable)
827 guint i;
828 gboolean dirty_tabs = FALSE;
830 if (ui_prefs.allow_always_save)
831 enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
833 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
834 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
836 /* save all menu item and tool button */
837 for (i = 0; i < documents_array->len; i++)
839 /* check whether there are files where changes were made and if there are some,
840 * we need the save all button / item */
841 if (documents[i]->is_valid && documents[i]->changed)
843 dirty_tabs = TRUE;
844 break;
848 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
849 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
853 #define add_doc_widget(widget_name) \
854 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
856 #define add_doc_toolitem(widget_name) \
857 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
859 static void init_document_widgets(void)
861 widgets.document_buttons = g_ptr_array_new();
863 /* Cache the document-sensitive widgets so we don't have to keep looking them up
864 * when using ui_document_buttons_update(). */
865 add_doc_widget("menu_close1");
866 add_doc_widget("close_other_documents1");
867 add_doc_widget("menu_change_font1");
868 add_doc_widget("menu_close_all1");
869 add_doc_widget("menu_save1");
870 add_doc_widget("menu_save_all1");
871 add_doc_widget("menu_save_as1");
872 add_doc_widget("menu_count_words1");
873 add_doc_widget("menu_build1");
874 add_doc_widget("add_comments1");
875 add_doc_widget("menu_paste1");
876 add_doc_widget("menu_undo2");
877 add_doc_widget("properties1");
878 add_doc_widget("menu_reload1");
879 add_doc_widget("menu_document1");
880 add_doc_widget("menu_choose_color1");
881 add_doc_widget("menu_color_schemes");
882 add_doc_widget("menu_markers_margin1");
883 add_doc_widget("menu_linenumber_margin1");
884 add_doc_widget("menu_show_white_space1");
885 add_doc_widget("menu_show_line_endings1");
886 add_doc_widget("menu_show_indentation_guides1");
887 add_doc_widget("menu_zoom_in1");
888 add_doc_widget("menu_zoom_out1");
889 add_doc_widget("normal_size1");
890 add_doc_widget("treeview6");
891 add_doc_widget("print1");
892 add_doc_widget("menu_reload_as1");
893 add_doc_widget("menu_select_all1");
894 add_doc_widget("insert_date1");
895 add_doc_widget("insert_alternative_white_space1");
896 add_doc_widget("menu_format1");
897 add_doc_widget("commands2");
898 add_doc_widget("menu_open_selected_file1");
899 add_doc_widget("page_setup1");
900 add_doc_widget("find1");
901 add_doc_widget("find_next1");
902 add_doc_widget("find_previous1");
903 add_doc_widget("go_to_next_marker1");
904 add_doc_widget("go_to_previous_marker1");
905 add_doc_widget("replace1");
906 add_doc_widget("find_nextsel1");
907 add_doc_widget("find_prevsel1");
908 add_doc_widget("find_usage1");
909 add_doc_widget("find_document_usage1");
910 add_doc_widget("mark_all1");
911 add_doc_widget("go_to_line1");
912 add_doc_widget("goto_tag_definition1");
913 add_doc_widget("goto_tag_declaration1");
914 add_doc_widget("reset_indentation1");
915 add_doc_toolitem("Close");
916 add_doc_toolitem("CloseAll");
917 add_doc_toolitem("Search");
918 add_doc_toolitem("SearchEntry");
919 add_doc_toolitem("ZoomIn");
920 add_doc_toolitem("ZoomOut");
921 add_doc_toolitem("Indent");
922 add_doc_toolitem("UnIndent");
923 add_doc_toolitem("Cut");
924 add_doc_toolitem("Copy");
925 add_doc_toolitem("Paste");
926 add_doc_toolitem("Delete");
927 add_doc_toolitem("Save");
928 add_doc_toolitem("SaveAs");
929 add_doc_toolitem("SaveAll");
930 add_doc_toolitem("Compile");
931 add_doc_toolitem("Run");
932 add_doc_toolitem("Reload");
933 add_doc_toolitem("Color");
934 add_doc_toolitem("Goto");
935 add_doc_toolitem("GotoEntry");
936 add_doc_toolitem("Replace");
937 add_doc_toolitem("Print");
941 void ui_document_buttons_update(void)
943 guint i;
944 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
946 for (i = 0; i < widgets.document_buttons->len; i++)
948 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
949 if (GTK_IS_ACTION(widget))
950 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
951 else
952 ui_widget_set_sensitive(widget, enable);
957 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
959 g_ptr_array_remove_fast(widgets.document_buttons, widget);
963 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
964 * when some documents are present/no documents are open.
965 * It will be removed when the widget is destroyed.
966 * @param widget The widget to add.
968 * @since 0.15
970 GEANY_API_SYMBOL
971 void ui_add_document_sensitive(GtkWidget *widget)
973 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
975 ui_widget_set_sensitive(widget, enable);
977 g_ptr_array_add(widgets.document_buttons, widget);
978 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
982 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
984 if (show)
986 gtk_widget_show(widget);
988 else
990 gtk_widget_hide(widget);
995 void ui_sidebar_show_hide(void)
997 GtkWidget *widget;
999 /* check that there are no other notebook pages before hiding the sidebar completely
1000 * other pages could be e.g. the file browser plugin */
1001 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1002 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1004 ui_prefs.sidebar_visible = FALSE;
1007 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
1008 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
1010 ignore_callback = TRUE;
1011 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
1012 ignore_callback = FALSE;
1015 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
1017 ui_widget_show_hide(gtk_notebook_get_nth_page(
1018 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
1019 ui_widget_show_hide(gtk_notebook_get_nth_page(
1020 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
1024 void ui_document_show_hide(GeanyDocument *doc)
1026 const gchar *widget_name;
1027 GtkWidget *item;
1028 const GeanyIndentPrefs *iprefs;
1030 g_return_if_fail(doc == NULL || doc->is_valid);
1032 if (doc == NULL)
1033 doc = document_get_current();
1035 if (doc == NULL)
1036 return;
1038 ignore_callback = TRUE;
1040 gtk_check_menu_item_set_active(
1041 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
1042 doc->editor->line_wrapping);
1044 gtk_check_menu_item_set_active(
1045 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
1046 doc->editor->line_breaking);
1048 iprefs = editor_get_indent_prefs(doc->editor);
1050 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
1051 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
1053 switch (iprefs->type)
1055 case GEANY_INDENT_TYPE_SPACES:
1056 widget_name = "spaces1"; break;
1057 case GEANY_INDENT_TYPE_TABS:
1058 widget_name = "tabs1"; break;
1059 case GEANY_INDENT_TYPE_BOTH:
1060 default:
1061 widget_name = "tabs_and_spaces1"; break;
1063 item = ui_lookup_widget(main_widgets.window, widget_name);
1064 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1066 if (iprefs->width >= 1 && iprefs->width <= 8)
1068 gchar *name;
1070 name = g_strdup_printf("indent_width_%d", iprefs->width);
1071 item = ui_lookup_widget(main_widgets.window, name);
1072 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1073 g_free(name);
1076 gtk_check_menu_item_set_active(
1077 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
1078 doc->readonly);
1080 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
1081 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
1082 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
1084 switch (sci_get_eol_mode(doc->editor->sci))
1086 case SC_EOL_CR: widget_name = "cr"; break;
1087 case SC_EOL_LF: widget_name = "lf"; break;
1088 default: widget_name = "crlf"; break;
1090 gtk_check_menu_item_set_active(
1091 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
1093 encodings_select_radio_item(doc->encoding);
1094 filetypes_select_radio_item(doc->file_type);
1096 ignore_callback = FALSE;
1100 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
1102 gtk_widget_set_name(widget, success ? NULL : "geany-search-entry-no-match");
1106 static void recent_create_menu(GeanyRecentFiles *grf)
1108 guint i, len;
1110 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
1111 for (i = 0; i < len; i++)
1113 /* create menu item for the recent files menu in the menu bar */
1114 const gchar *filename = g_queue_peek_nth(grf->recent_queue, i);
1115 GtkWidget *tmp = gtk_menu_item_new_with_label(filename);
1117 gtk_widget_show(tmp);
1118 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
1119 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1120 /* create menu item for the recent files menu in the toolbar */
1121 if (grf->toolbar != NULL)
1123 tmp = gtk_menu_item_new_with_label(filename);
1124 gtk_widget_show(tmp);
1125 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1126 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1132 static GeanyRecentFiles *recent_get_recent_files(void)
1134 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
1136 if (G_UNLIKELY(grf.recent_queue == NULL))
1138 grf.recent_queue = ui_prefs.recent_queue;
1139 grf.menubar = ui_widgets.recent_files_menu_menubar;
1140 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1141 toolbar_get_action_by_name("Open")));
1142 grf.activate_cb = recent_file_activate_cb;
1144 return &grf;
1148 static GeanyRecentFiles *recent_get_recent_projects(void)
1150 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
1152 if (G_UNLIKELY(grf.recent_queue == NULL))
1154 grf.recent_queue = ui_prefs.recent_projects_queue;
1155 grf.menubar = ui_widgets.recent_projects_menu_menubar;
1156 grf.toolbar = NULL;
1157 grf.activate_cb = recent_project_activate_cb;
1159 return &grf;
1163 void ui_create_recent_menus(void)
1165 recent_create_menu(recent_get_recent_files());
1166 recent_create_menu(recent_get_recent_projects());
1170 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1172 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1173 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1175 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1176 recent_file_loaded(utf8_filename, recent_get_recent_files());
1178 g_free(locale_filename);
1179 g_free(utf8_filename);
1183 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1185 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1186 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1188 if (project_ask_close() && project_load_file_with_session(locale_filename))
1189 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1191 g_free(locale_filename);
1192 g_free(utf8_filename);
1196 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf,
1197 const GtkRecentData *rdata)
1199 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1202 if (grf->type == RECENT_FILE_FILE && rdata)
1204 GtkRecentManager *manager = gtk_recent_manager_get_default();
1205 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1206 if (uri != NULL)
1208 gtk_recent_manager_add_full(manager, uri, rdata);
1209 g_free(uri);
1213 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1214 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1216 g_free(g_queue_pop_tail(grf->recent_queue));
1218 update_recent_menu(grf);
1220 /* filename already in recent list */
1221 else
1222 recent_file_loaded(utf8_filename, grf);
1226 void ui_add_recent_document(GeanyDocument *doc)
1228 /* what are the groups for actually? */
1229 static const gchar *groups[2] = {
1230 "geany",
1231 NULL
1233 GtkRecentData rdata;
1235 /* Prepare the data for gtk_recent_manager_add_full() */
1236 rdata.display_name = NULL;
1237 rdata.description = NULL;
1238 rdata.mime_type = doc->file_type->mime_type;
1239 /* if we ain't got no mime-type, fallback to plain text */
1240 if (! rdata.mime_type)
1241 rdata.mime_type = (gchar *) "text/plain";
1242 rdata.app_name = (gchar *) "geany";
1243 rdata.app_exec = (gchar *) "geany %u";
1244 rdata.groups = (gchar **) groups;
1245 rdata.is_private = FALSE;
1247 add_recent_file(doc->file_name, recent_get_recent_files(), &rdata);
1251 void ui_add_recent_project_file(const gchar *utf8_filename)
1253 add_recent_file(utf8_filename, recent_get_recent_projects(), NULL);
1257 /* Returns: newly allocated string with the UTF-8 menu text. */
1258 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1260 const gchar *text = NULL;
1262 if (gtk_bin_get_child(GTK_BIN(menu_item)))
1264 GtkWidget *child = gtk_bin_get_child(GTK_BIN(menu_item));
1266 if (GTK_IS_LABEL(child))
1267 text = gtk_label_get_text(GTK_LABEL(child));
1269 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1270 return g_strdup(text);
1274 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1276 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1277 gint result;
1279 if (utils_str_equal(menu_text, user_data))
1280 result = 0;
1281 else
1282 result = 1;
1284 g_free(menu_text);
1285 return result;
1289 /* update the project menu item's sensitivity */
1290 void ui_update_recent_project_menu(void)
1292 GeanyRecentFiles *grf = recent_get_recent_projects();
1293 GList *children, *item;
1295 /* only need to update the menubar menu, the project doesn't have a toolbar item */
1296 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1297 for (item = children; item; item = item->next)
1299 gboolean sensitive = TRUE;
1301 if (app->project)
1303 const gchar *filename = gtk_menu_item_get_label(item->data);
1304 sensitive = g_strcmp0(app->project->file_name, filename) != 0;
1306 gtk_widget_set_sensitive(item->data, sensitive);
1308 g_list_free(children);
1312 /* Use instead of gtk_menu_reorder_child() to update the menubar properly on OS X */
1313 static void menu_reorder_child(GtkMenu *menu, GtkWidget *child, gint position)
1315 gtk_menu_reorder_child(menu, child, position);
1316 #ifdef MAC_INTEGRATION
1317 /* On OS X GtkMenuBar is kept in sync with the native OS X menubar using
1318 * signals. Unfortunately gtk_menu_reorder_child() doesn't emit anything
1319 * so we have to update the OS X menubar manually. */
1320 gtkosx_application_sync_menubar(gtkosx_application_get());
1321 #endif
1325 static void add_recent_file_menu_item(const gchar *utf8_filename, GeanyRecentFiles *grf, GtkWidget *menu)
1327 GtkWidget *child = gtk_menu_item_new_with_label(utf8_filename);
1329 gtk_widget_show(child);
1330 if (menu != grf->toolbar)
1331 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), child);
1332 else
1334 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1335 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal
1336 * which we need in GeanyMenubuttonAction */
1337 gtk_container_add(GTK_CONTAINER(menu), child);
1338 menu_reorder_child(GTK_MENU(menu), child, 0);
1340 g_signal_connect(child, "activate", G_CALLBACK(grf->activate_cb), NULL);
1344 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1346 GList *item;
1347 GtkWidget *parents[] = { grf->menubar, grf->toolbar };
1348 guint i;
1350 /* first reorder the queue */
1351 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1352 g_return_if_fail(item != NULL);
1354 g_queue_unlink(grf->recent_queue, item);
1355 g_queue_push_head_link(grf->recent_queue, item);
1357 for (i = 0; i < G_N_ELEMENTS(parents); i++)
1359 GList *children;
1361 if (! parents[i])
1362 continue;
1364 children = gtk_container_get_children(GTK_CONTAINER(parents[i]));
1365 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1366 /* either reorder or prepend a new one */
1367 if (item)
1368 menu_reorder_child(GTK_MENU(parents[i]), item->data, 0);
1369 else
1370 add_recent_file_menu_item(utf8_filename, grf, parents[i]);
1371 g_list_free(children);
1374 if (grf->type == RECENT_FILE_PROJECT)
1375 ui_update_recent_project_menu();
1379 static void update_recent_menu(GeanyRecentFiles *grf)
1381 gchar *filename;
1382 GtkWidget *parents[] = { grf->menubar, grf->toolbar };
1383 guint i;
1385 filename = g_queue_peek_head(grf->recent_queue);
1387 for (i = 0; i < G_N_ELEMENTS(parents); i++)
1389 GList *children;
1391 if (! parents[i])
1392 continue;
1394 /* clean the MRU list before adding an item */
1395 children = gtk_container_get_children(GTK_CONTAINER(parents[i]));
1396 if (g_list_length(children) > file_prefs.mru_length - 1)
1398 GList *item = g_list_nth(children, file_prefs.mru_length - 1);
1399 while (item != NULL)
1401 if (GTK_IS_MENU_ITEM(item->data))
1402 gtk_widget_destroy(GTK_WIDGET(item->data));
1403 item = g_list_next(item);
1406 g_list_free(children);
1408 /* create the new item */
1409 add_recent_file_menu_item(filename, grf, parents[i]);
1412 if (grf->type == RECENT_FILE_PROJECT)
1413 ui_update_recent_project_menu();
1417 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1419 guint i;
1421 foreach_document (i)
1423 GeanyDocument *doc = documents[i];
1425 switch (feature)
1427 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1428 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1429 break;
1430 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1431 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin);
1432 break;
1433 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1434 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1435 break;
1436 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1437 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1438 break;
1439 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1440 editor_set_indentation_guides(doc->editor);
1441 break;
1447 void ui_update_view_editor_menu_items(void)
1449 ignore_callback = TRUE;
1450 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1451 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1452 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_white_space1")), editor_prefs.show_white_space);
1453 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_line_endings1")), editor_prefs.show_line_endings);
1454 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_indentation_guides1")), editor_prefs.show_indent_guide);
1455 ignore_callback = FALSE;
1459 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1460 * @param label_text The label text.
1461 * @param alignment An address to store the alignment widget pointer.
1463 * @return @transfer{floating} The frame widget, setting the alignment container for
1464 * packing child widgets.
1466 GEANY_API_SYMBOL
1467 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1469 GtkWidget *label, *align;
1470 GtkWidget *frame = gtk_frame_new(NULL);
1472 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1474 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1475 gtk_container_add(GTK_CONTAINER(frame), align);
1476 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1478 label = ui_label_new_bold(label_text);
1479 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1481 *alignment = align;
1482 return frame;
1486 /** Makes a fixed border for dialogs without increasing the button box border.
1487 * @param dialog The parent container for the @c GtkVBox.
1489 * @return @transfer{none} The packed @c GtkVBox. */
1490 GEANY_API_SYMBOL
1491 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1493 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1495 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1496 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox, TRUE, TRUE, 0);
1497 return vbox;
1501 /* Reorders a dialog's buttons
1502 * @param dialog A dialog
1503 * @param response First response ID to reorder
1504 * @param ... more response IDs, terminated by -1
1506 * Like gtk_dialog_set_alternative_button_order(), but reorders the default
1507 * buttons layout, not the alternative one. This is useful if you e.g. added a
1508 * button to a dialog which already had some and need yours not to be on the
1509 * end.
1511 /* Heavily based on gtk_dialog_set_alternative_button_order().
1512 * This relies on the action area to be a GtkBox, but although not documented
1513 * the API expose it to be a GtkHButtonBox though GtkBuilder, so it should be
1514 * fine */
1515 void ui_dialog_set_primary_button_order(GtkDialog *dialog, gint response, ...)
1517 va_list ap;
1518 GtkWidget *action_area = gtk_dialog_get_action_area(dialog);
1519 gint position;
1521 va_start(ap, response);
1522 for (position = 0; response != -1; position++)
1524 GtkWidget *child = gtk_dialog_get_widget_for_response(dialog, response);
1525 if (child)
1526 gtk_box_reorder_child(GTK_BOX(action_area), child, position);
1527 else
1528 g_warning("%s: no child button with response id %d.", G_STRFUNC, response);
1530 response = va_arg(ap, gint);
1532 va_end(ap);
1536 /** Creates a @c GtkButton with custom text and a stock image similar to
1537 * @c gtk_button_new_from_stock().
1538 * @param stock_id A @c GTK_STOCK_NAME string.
1539 * @param text Button label text, can include mnemonics.
1541 * @return @transfer{floating} The new @c GtkButton.
1543 GEANY_API_SYMBOL
1544 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1546 GtkWidget *image, *button;
1548 button = gtk_button_new_with_mnemonic(text);
1549 gtk_widget_show(button);
1550 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1551 gtk_button_set_image(GTK_BUTTON(button), image);
1552 /* note: image is shown by gtk */
1553 return button;
1557 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1558 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1559 * @param label Menu item label, can include mnemonics.
1560 * @return @transfer{floating} The new @c GtkImageMenuItem.
1562 * @since 0.16
1564 GEANY_API_SYMBOL
1565 GtkWidget *
1566 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1568 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1569 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1571 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1572 gtk_widget_show(image);
1573 return item;
1577 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1578 GdkEvent *event, gpointer data)
1580 if (event->button.button == 1 && icon_pos == 1)
1582 gtk_entry_set_text(entry, "");
1583 gtk_widget_grab_focus(GTK_WIDGET(entry));
1588 /** Adds a small clear icon to the right end of the passed @a entry.
1589 * A callback to clear the contents of the GtkEntry is automatically added.
1591 * @param entry The GtkEntry object to which the icon should be attached.
1593 * @since 0.16
1595 GEANY_API_SYMBOL
1596 void ui_entry_add_clear_icon(GtkEntry *entry)
1598 g_object_set(entry, "secondary-icon-stock", GTK_STOCK_CLEAR,
1599 "secondary-icon-activatable", TRUE, NULL);
1600 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1604 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1605 void ui_entry_add_activate_backward_signal(GtkEntry *entry)
1607 static gboolean installed = FALSE;
1609 g_return_if_fail(GTK_IS_ENTRY(entry));
1611 if (G_UNLIKELY(! installed))
1613 GtkBindingSet *binding_set;
1615 installed = TRUE;
1617 /* try to handle the unexpected case where GTK would already have installed the signal */
1618 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry)))
1620 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1621 return;
1624 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry),
1625 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 0, NULL, NULL,
1626 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1627 binding_set = gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry));
1628 gtk_binding_entry_add_signal(binding_set, GDK_Return, GDK_SHIFT_MASK, "activate-backward", 0);
1633 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1635 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1636 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1640 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1641 * the width of each button box's children.
1642 * Should be called after all child widgets have been packed. */
1643 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1645 GtkSizeGroup *size_group;
1647 gtk_box_set_spacing(GTK_BOX(copy), 10);
1648 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1650 /* now we need to put the widest widget from each button box in a size group,
1651 * but we don't know the width before they are drawn, and for different label
1652 * translations the widest widget can vary, so we just add all widgets. */
1653 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1654 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1655 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1656 g_object_unref(size_group);
1660 static gboolean tree_model_find_text(GtkTreeModel *model,
1661 GtkTreeIter *iter, gint column, const gchar *text)
1663 gchar *combo_text;
1664 gboolean found = FALSE;
1666 if (gtk_tree_model_get_iter_first(model, iter))
1670 gtk_tree_model_get(model, iter, 0, &combo_text, -1);
1671 found = utils_str_equal(combo_text, text);
1672 g_free(combo_text);
1674 if (found)
1675 return TRUE;
1677 while (gtk_tree_model_iter_next(model, iter));
1679 return FALSE;
1683 /** Prepends @a text to the drop down list, removing a duplicate element in
1684 * the list if found. Also ensures there are <= @a history_len elements.
1685 * @param combo_entry .
1686 * @param text @nullable Text to add, or @c NULL for current entry text.
1687 * @param history_len Max number of items, or @c 0 for default. */
1688 GEANY_API_SYMBOL
1689 void ui_combo_box_add_to_history(GtkComboBoxText *combo_entry,
1690 const gchar *text, gint history_len)
1692 GtkComboBox *combo = GTK_COMBO_BOX(combo_entry);
1693 GtkTreeModel *model;
1694 GtkTreeIter iter;
1695 GtkTreePath *path;
1697 if (history_len <= 0)
1698 history_len = 10;
1699 if (!text)
1700 text = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
1702 model = gtk_combo_box_get_model(combo);
1704 if (tree_model_find_text(model, &iter, 0, text))
1706 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1708 gtk_combo_box_text_prepend_text(combo_entry, text);
1710 /* limit history */
1711 path = gtk_tree_path_new_from_indices(history_len, -1);
1712 if (gtk_tree_model_get_iter(model, &iter, path))
1714 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1716 gtk_tree_path_free(path);
1720 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1721 * exists in the combo's model. */
1722 void ui_combo_box_prepend_text_once(GtkComboBoxText *combo, const gchar *text)
1724 GtkTreeModel *model;
1725 GtkTreeIter iter;
1727 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
1728 if (tree_model_find_text(model, &iter, 0, text))
1729 return; /* don't prepend duplicate */
1731 gtk_combo_box_text_prepend_text(combo, text);
1735 /* Changes the color of the notebook tab text and open files items according to
1736 * document status. */
1737 void ui_update_tab_status(GeanyDocument *doc)
1739 gtk_widget_set_name(doc->priv->tab_label, document_get_status_widget_class(doc));
1741 sidebar_openfiles_update(doc);
1745 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1746 gboolean down)
1748 GtkTreePath *path;
1749 gboolean result;
1751 if (down)
1752 return gtk_tree_model_iter_next(model, iter);
1754 path = gtk_tree_model_get_path(model, iter);
1755 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1756 gtk_tree_path_free(path);
1757 return result;
1761 /* note: the while loop might be more efficient when searching upwards if it
1762 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1763 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1765 GtkTreeSelection *treesel;
1766 GtkTreeIter iter;
1767 GtkTreeModel *model;
1769 treesel = gtk_tree_view_get_selection(treeview);
1770 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1772 /* get the next selected item */
1773 if (! tree_model_iter_get_next(model, &iter, down))
1774 return FALSE; /* no more items */
1776 else /* no selection */
1778 if (! gtk_tree_model_get_iter_first(model, &iter))
1779 return TRUE; /* no items */
1781 while (TRUE)
1783 gtk_tree_selection_select_iter(treesel, &iter);
1784 if (cb(FALSE))
1785 break; /* found next message */
1787 if (! tree_model_iter_get_next(model, &iter, down))
1788 return FALSE; /* no more items */
1790 /* scroll item in view */
1791 if (ui_prefs.msgwindow_visible)
1793 GtkTreePath *path = gtk_tree_model_get_path(
1794 gtk_tree_view_get_model(treeview), &iter);
1796 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1797 gtk_tree_path_free(path);
1799 return TRUE;
1803 /* Returns FALSE if the treeview has items but no matching next item. */
1804 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1806 return tree_view_find(treeview, cb, TRUE);
1810 /* Returns FALSE if the treeview has items but no matching next item. */
1811 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1813 return tree_view_find(treeview, cb, FALSE);
1817 /* Shamelessly stolen from GTK */
1818 static gboolean ui_tree_view_query_tooltip_cb(GtkWidget *widget, gint x, gint y,
1819 gboolean keyboard_tip, GtkTooltip *tooltip, gpointer data)
1821 GValue value = { 0 };
1822 GValue transformed = { 0 };
1823 GtkTreeIter iter;
1824 GtkTreePath *path;
1825 GtkTreeModel *model;
1826 GtkTreeView *tree_view = GTK_TREE_VIEW(widget);
1827 gint column = GPOINTER_TO_INT(data);
1828 gboolean tootlip_set = FALSE;
1830 if (! gtk_tree_view_get_tooltip_context(tree_view, &x, &y, keyboard_tip, &model, &path, &iter))
1831 return FALSE;
1833 gtk_tree_model_get_value(model, &iter, column, &value);
1835 g_value_init(&transformed, G_TYPE_STRING);
1836 if (g_value_transform(&value, &transformed) && g_value_get_string(&transformed))
1838 gtk_tooltip_set_text(tooltip, g_value_get_string(&transformed));
1839 gtk_tree_view_set_tooltip_row(tree_view, tooltip, path);
1840 tootlip_set = TRUE;
1843 g_value_unset(&transformed);
1844 g_value_unset(&value);
1845 gtk_tree_path_free(path);
1847 return tootlip_set;
1851 /** Adds text tooltips to a tree view.
1853 * This is similar to gtk_tree_view_set_tooltip_column() but considers the column contents to be
1854 * text, not markup -- it uses gtk_tooltip_set_text() rather than gtk_tooltip_set_markup() to set
1855 * the tooltip's value.
1857 * @warning Unlike gtk_tree_view_set_tooltip_column() you currently cannot change or remove the
1858 * tooltip column after it has been added. Trying to do so will probably give funky results.
1860 * @param tree_view The tree view
1861 * @param column The column to get the tooltip from
1863 * @since 1.25 (API 223)
1865 /* Note: @p column is int and not uint both to match gtk_tree_view_set_tooltip_column() signature
1866 * and to allow future support of -1 to unset if ever wanted */
1867 GEANY_API_SYMBOL
1868 void ui_tree_view_set_tooltip_text_column(GtkTreeView *tree_view, gint column)
1870 g_return_if_fail(column >= 0);
1871 g_return_if_fail(GTK_IS_TREE_VIEW(tree_view));
1873 g_signal_connect(tree_view, "query-tooltip",
1874 G_CALLBACK(ui_tree_view_query_tooltip_cb), GINT_TO_POINTER(column));
1875 gtk_widget_set_has_tooltip(GTK_WIDGET(tree_view), TRUE);
1880 * Modifies the font of a widget using gtk_widget_modify_font().
1882 * @param widget The widget.
1883 * @param str The font name as expected by pango_font_description_from_string().
1885 GEANY_API_SYMBOL
1886 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1888 PangoFontDescription *pfd;
1890 pfd = pango_font_description_from_string(str);
1891 gtk_widget_modify_font(widget, pfd);
1892 pango_font_description_free(pfd);
1896 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1897 * file chooser, replacing entry text (if successful) with the path returned from the
1898 * @c GtkFileChooser.
1899 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1900 * @param title @nullable The file chooser dialog title, or @c NULL.
1901 * @param action The mode of the file chooser.
1902 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1903 * such as @c GtkComboBoxEntry.
1905 * @return @transfer{floating} The @c GtkHBox.
1907 /* @see ui_setup_open_button_callback(). */
1908 GEANY_API_SYMBOL
1909 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1911 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry, *parent, *next_parent;
1913 hbox = gtk_hbox_new(FALSE, 6);
1914 path_entry = GTK_WIDGET(entry);
1916 /* prevent path_entry being vertically stretched to the height of dirbtn */
1917 vbox = gtk_vbox_new(FALSE, 0);
1919 parent = path_entry;
1920 while ((next_parent = gtk_widget_get_parent(parent)) != NULL)
1921 parent = next_parent;
1923 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1925 dirbtn = gtk_button_new();
1926 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1927 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1928 ui_setup_open_button_callback(dirbtn, title, action, entry);
1930 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1931 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1932 return hbox;
1936 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1939 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1940 * title can be NULL.
1941 * action is the file chooser mode to use. */
1942 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1943 GtkFileChooserAction action, GtkEntry *entry)
1945 GtkWidget *path_entry = GTK_WIDGET(entry);
1947 if (title)
1948 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1949 (GDestroyNotify) g_free);
1950 g_object_set_data(G_OBJECT(open_btn), "action", GINT_TO_POINTER(action));
1951 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), path_entry);
1955 #ifndef G_OS_WIN32
1956 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1957 const gchar *utf8_path)
1959 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1960 GTK_WINDOW(main_widgets.window), action,
1961 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1962 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1963 gchar *locale_path;
1964 gchar *ret_path = NULL;
1966 gtk_widget_set_name(dialog, "GeanyDialog");
1967 locale_path = utils_get_locale_from_utf8(utf8_path);
1968 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1970 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1971 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1973 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1975 if (g_path_is_absolute(locale_path))
1976 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1978 g_free(locale_path);
1980 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1982 gchar *dir_locale;
1984 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1985 ret_path = utils_get_utf8_from_locale(dir_locale);
1986 g_free(dir_locale);
1988 gtk_widget_destroy(dialog);
1989 return ret_path;
1991 #endif
1994 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1996 GtkFileChooserAction action = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "action"));
1997 GtkEntry *entry = user_data;
1998 const gchar *title = g_object_get_data(G_OBJECT(button), "title");
1999 gchar *utf8_path = NULL;
2001 /* TODO: extend for other actions */
2002 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2003 action == GTK_FILE_CHOOSER_ACTION_OPEN);
2005 if (title == NULL)
2006 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
2007 _("Select Folder") : _("Select File");
2009 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
2011 #ifdef G_OS_WIN32
2012 utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
2013 gtk_entry_get_text(GTK_ENTRY(entry)));
2014 #else
2015 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
2016 #endif
2018 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
2020 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
2021 #ifdef G_OS_WIN32
2022 utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
2023 gtk_entry_get_text(GTK_ENTRY(entry)));
2024 #else
2025 utf8_path = run_file_chooser(title, action, path);
2026 #endif
2027 g_free(path);
2030 if (utf8_path != NULL)
2032 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
2033 g_free(utf8_path);
2038 void ui_statusbar_showhide(gboolean state)
2040 /* handle statusbar visibility */
2041 if (state)
2043 gtk_widget_show(ui_widgets.statusbar);
2044 ui_update_statusbar(NULL, -1);
2046 else
2047 gtk_widget_hide(ui_widgets.statusbar);
2051 /** Packs all @c GtkWidgets passed after the row argument into a table, using
2052 * one widget per cell. The first widget is not expanded as the table grows,
2053 * as this is usually a label.
2054 * @param table
2055 * @param row The row number of the table.
2057 GEANY_API_SYMBOL
2058 void ui_table_add_row(GtkTable *table, gint row, ...)
2060 va_list args;
2061 guint i;
2062 GtkWidget *widget;
2064 va_start(args, row);
2065 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
2067 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
2069 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
2070 options, 0, 0, 0);
2072 va_end(args);
2076 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
2078 const gchar *file_name = user_data;
2079 GeanyFiletype *ft = NULL;
2081 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
2082 ft = filetypes[GEANY_FILETYPES_CONF];
2084 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
2085 document_open_file(file_name, FALSE, ft, NULL);
2086 else
2088 gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
2089 gchar *base_name = g_path_get_basename(file_name);
2090 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
2091 gchar *global_content = NULL;
2093 /* if the requested file doesn't exist in the user's config dir, try loading the file
2094 * from the global data directory and use its contents for the newly created file */
2095 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
2096 g_file_get_contents(global_file, &global_content, NULL, NULL);
2098 document_new_file(utf8_filename, ft, global_content);
2100 utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
2105 static void free_on_closure_notify(gpointer data, GClosure *closure)
2107 g_free(data);
2111 /* @note You should connect to the "document-save" signal yourself to detect
2112 * if the user has just saved the config file, reloading it. */
2113 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
2115 GtkWidget *item;
2117 if (!parent)
2118 parent = GTK_CONTAINER(widgets.config_files_menu);
2120 if (!label)
2122 gchar *base_name;
2124 base_name = g_path_get_basename(real_path);
2125 item = gtk_menu_item_new_with_label(base_name);
2126 g_free(base_name);
2128 else
2129 item = gtk_menu_item_new_with_mnemonic(label);
2131 gtk_widget_show(item);
2132 gtk_container_add(parent, item);
2133 g_signal_connect_data(item, "activate", G_CALLBACK(on_config_file_clicked),
2134 g_strdup(real_path), free_on_closure_notify, 0);
2138 static gboolean sort_menu(gpointer data)
2140 ui_menu_sort_by_label(GTK_MENU(data));
2141 return FALSE;
2145 static void create_config_files_menu(void)
2147 GtkWidget *menu, *item;
2149 widgets.config_files_menu = menu = gtk_menu_new();
2151 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
2152 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
2154 item = gtk_menu_item_new_with_mnemonic(_("_Filetype Configuration"));
2155 gtk_container_add(GTK_CONTAINER(menu), item);
2156 ui_widgets.config_files_filetype_menu = gtk_menu_new();
2157 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), ui_widgets.config_files_filetype_menu);
2158 gtk_widget_show(item);
2160 /* sort menu after all items added */
2161 g_idle_add(sort_menu, widgets.config_files_menu);
2165 /* adds factory icons with a named icon source using the stock items id */
2166 static void add_stock_icons(const GtkStockItem *items, gsize count)
2168 GtkIconFactory *factory = gtk_icon_factory_new();
2169 GtkIconSource *source = gtk_icon_source_new();
2170 gsize i;
2172 for (i = 0; i < count; i++)
2174 GtkIconSet *set = gtk_icon_set_new();
2176 gtk_icon_source_set_icon_name(source, items[i].stock_id);
2177 gtk_icon_set_add_source(set, source);
2178 gtk_icon_factory_add(factory, items[i].stock_id, set);
2179 gtk_icon_set_unref(set);
2181 gtk_icon_source_free(source);
2182 gtk_icon_factory_add_default(factory);
2183 g_object_unref(factory);
2187 void ui_init_stock_items(void)
2189 GtkStockItem items[] =
2191 { GEANY_STOCK_SAVE_ALL, N_("Save All"), 0, 0, GETTEXT_PACKAGE },
2192 { GEANY_STOCK_CLOSE_ALL, N_("Close All"), 0, 0, GETTEXT_PACKAGE },
2193 { GEANY_STOCK_BUILD, N_("Build"), 0, 0, GETTEXT_PACKAGE }
2196 gtk_stock_add(items, G_N_ELEMENTS(items));
2197 add_stock_icons(items, G_N_ELEMENTS(items));
2201 void ui_init_toolbar_widgets(void)
2203 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
2204 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
2205 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
2206 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
2210 void ui_swap_sidebar_pos(void)
2212 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
2213 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
2214 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
2216 g_object_ref(left);
2217 g_object_ref(right);
2218 gtk_container_remove (GTK_CONTAINER (pane), left);
2219 gtk_container_remove (GTK_CONTAINER (pane), right);
2220 /* only scintilla notebook should expand */
2221 gtk_paned_pack1(GTK_PANED(pane), right, right == main_widgets.notebook, TRUE);
2222 gtk_paned_pack2(GTK_PANED(pane), left, left == main_widgets.notebook, TRUE);
2223 g_object_unref(left);
2224 g_object_unref(right);
2226 gtk_paned_set_position(GTK_PANED(pane), gtk_widget_get_allocated_width(pane)
2227 - gtk_paned_get_position(GTK_PANED(pane)));
2231 static void init_recent_files(void)
2233 GtkWidget *toolbar_recent_files_menu;
2235 /* add recent files to the File menu */
2236 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
2237 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
2238 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
2239 ui_widgets.recent_files_menu_menubar);
2241 /* add recent files to the toolbar Open button */
2242 toolbar_recent_files_menu = gtk_menu_new();
2243 g_object_ref(toolbar_recent_files_menu);
2244 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
2245 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
2249 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
2251 g_object_ref(menu);
2252 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
2253 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
2254 g_object_unref(menu);
2258 typedef struct GeanySharedMenu
2260 const gchar *menu;
2261 const gchar *menubar_item;
2262 const gchar *popup_item;
2264 GeanySharedMenu;
2266 #define foreach_menu(item, array) \
2267 for (item = array; item->menu; item++)
2269 static void on_editor_menu_show(GtkWidget *widget, GeanySharedMenu *items)
2271 GeanySharedMenu *item;
2273 foreach_menu(item, items)
2275 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2276 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2277 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2279 ui_menu_move(menu, bar, popup);
2284 static void on_editor_menu_hide(GtkWidget *widget, GeanySharedMenu *items)
2286 GeanySharedMenu *item;
2288 foreach_menu(item, items)
2290 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2291 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2292 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2294 ui_menu_move(menu, popup, bar);
2299 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2300 * so this is called after that's done. */
2301 void ui_init_prefs(void)
2303 StashGroup *group = stash_group_new(PACKAGE);
2305 /* various prefs */
2306 configuration_add_various_pref_group(group);
2308 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
2309 "show_symbol_list_expanders", TRUE);
2310 stash_group_add_boolean(group, &interface_prefs.compiler_tab_autoscroll,
2311 "compiler_tab_autoscroll", TRUE);
2312 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
2313 "allow_always_save", FALSE);
2314 stash_group_add_string(group, &ui_prefs.statusbar_template,
2315 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE));
2316 stash_group_add_boolean(group, &ui_prefs.new_document_after_close,
2317 "new_document_after_close", FALSE);
2318 stash_group_add_boolean(group, &interface_prefs.msgwin_status_visible,
2319 "msgwin_status_visible", TRUE);
2320 stash_group_add_boolean(group, &interface_prefs.msgwin_compiler_visible,
2321 "msgwin_compiler_visible", TRUE);
2322 stash_group_add_boolean(group, &interface_prefs.msgwin_messages_visible,
2323 "msgwin_messages_visible", TRUE);
2324 stash_group_add_boolean(group, &interface_prefs.msgwin_scribble_visible,
2325 "msgwin_scribble_visible", TRUE);
2329 /* Used to find out the name of the GtkBuilder retrieved object since
2330 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2331 * 'name' property for that and those that don't implement GtkBuildable
2332 * will have a "gtk-builder-name" stored in the GObject's data list. */
2333 static const gchar *ui_guess_object_name(GObject *obj)
2335 const gchar *name = NULL;
2337 g_return_val_if_fail(G_IS_OBJECT(obj), NULL);
2339 if (GTK_IS_BUILDABLE(obj))
2340 name = gtk_buildable_get_name(GTK_BUILDABLE(obj));
2341 if (! name)
2342 name = g_object_get_data(obj, "gtk-builder-name");
2343 if (! name)
2344 return NULL;
2346 return name;
2350 /* Compatibility functions */
2351 GtkWidget *create_edit_menu1(void)
2353 return edit_menu1;
2357 GtkWidget *create_prefs_dialog(void)
2359 return prefs_dialog;
2363 GtkWidget *create_project_dialog(void)
2365 return project_dialog;
2369 GtkWidget *create_toolbar_popup_menu1(void)
2371 return toolbar_popup_menu1;
2375 GtkWidget *create_window1(void)
2377 return window1;
2381 static GtkWidget *ui_get_top_parent(GtkWidget *widget)
2383 GtkWidget *parent;
2385 g_return_val_if_fail(GTK_IS_WIDGET(widget), NULL);
2387 for (;;)
2389 if (GTK_IS_MENU(widget))
2390 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2391 else
2392 parent = gtk_widget_get_parent(widget);
2393 if (parent == NULL)
2394 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2395 if (parent == NULL)
2396 break;
2397 widget = parent;
2400 return widget;
2404 void ui_init_builder(void)
2406 gchar *interface_file;
2407 const gchar *name;
2408 GError *error;
2409 GSList *iter, *all_objects;
2410 GtkWidget *widget, *toplevel;
2412 /* prevent function from being called twice */
2413 if (GTK_IS_BUILDER(builder))
2414 return;
2416 builder = gtk_builder_new();
2418 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
2420 error = NULL;
2421 interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
2422 if (! gtk_builder_add_from_file(builder, interface_file, &error))
2424 /* Show the user this message so they know WTF happened */
2425 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
2426 _("Geany cannot start!"), error->message);
2427 /* Aborts */
2428 g_error("Cannot create user-interface: %s", error->message);
2429 g_error_free(error);
2430 g_free(interface_file);
2431 g_object_unref(builder);
2432 return;
2434 g_free(interface_file);
2436 callbacks_connect(builder);
2438 edit_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "edit_menu1"));
2439 prefs_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "prefs_dialog"));
2440 project_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "project_dialog"));
2441 toolbar_popup_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar_popup_menu1"));
2442 window1 = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
2444 g_object_set_data(G_OBJECT(edit_menu1), "edit_menu1", edit_menu1);
2445 g_object_set_data(G_OBJECT(prefs_dialog), "prefs_dialog", prefs_dialog);
2446 g_object_set_data(G_OBJECT(project_dialog), "project_dialog", project_dialog);
2447 g_object_set_data(G_OBJECT(toolbar_popup_menu1), "toolbar_popup_menu1", toolbar_popup_menu1);
2448 g_object_set_data(G_OBJECT(window1), "window1", window1);
2450 all_objects = gtk_builder_get_objects(builder);
2451 for (iter = all_objects; iter != NULL; iter = g_slist_next(iter))
2453 if (! GTK_IS_WIDGET(iter->data))
2454 continue;
2456 widget = GTK_WIDGET(iter->data);
2458 name = ui_guess_object_name(G_OBJECT(widget));
2459 if (! name)
2461 g_warning("Unable to get name from GtkBuilder object");
2462 continue;
2465 toplevel = ui_get_top_parent(widget);
2466 if (toplevel)
2467 ui_hookup_widget(toplevel, widget, name);
2469 g_slist_free(all_objects);
2473 static void init_custom_style(void)
2475 #if GTK_CHECK_VERSION(3, 0, 0)
2476 const struct {
2477 guint version;
2478 const gchar *file;
2479 } css_files[] = {
2481 * Keep these from newest to oldest,
2482 * and make sure 0 remains last
2484 { 20, "geany-3.20.css" },
2485 { 0, "geany-3.0.css" },
2487 guint gtk_version = gtk_get_minor_version();
2488 gsize i = 0;
2489 gchar *css_file;
2490 GtkCssProvider *css = gtk_css_provider_new();
2491 GError *error = NULL;
2493 /* gtk_version will never be smaller than 0 */
2494 while (css_files[i].version > gtk_version)
2495 ++i;
2497 css_file = g_build_filename(app->datadir, css_files[i].file, NULL);
2498 if (! gtk_css_provider_load_from_path(css, css_file, &error))
2500 g_warning("Failed to load custom CSS: %s", error->message);
2501 g_error_free(error);
2503 else
2505 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2506 GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2509 g_object_unref(css);
2510 g_free(css_file);
2511 #else
2512 /* see setup_gtk2_styles() in main.c */
2513 #endif
2517 void ui_init(void)
2519 init_custom_style();
2521 init_recent_files();
2523 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
2524 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
2526 main_widgets.progressbar = progress_bar_create();
2528 /* current word sensitive items */
2529 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition2");
2530 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "context_action1");
2531 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage2");
2532 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage2");
2534 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
2535 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
2536 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
2537 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
2538 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
2539 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
2540 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
2541 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
2542 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
2543 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
2544 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
2545 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
2546 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
2547 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
2549 /* reparent context submenus as needed */
2551 GeanySharedMenu arr[] = {
2552 {"commands2_menu", "commands2", "commands1"},
2553 {"menu_format1_menu", "menu_format1", "menu_format2"},
2554 {"more1_menu", "more1", "search2"},
2555 {NULL, NULL, NULL}
2557 static GeanySharedMenu items[G_N_ELEMENTS(arr)];
2559 memcpy(items, arr, sizeof(arr));
2560 g_signal_connect(main_widgets.editor_menu, "show", G_CALLBACK(on_editor_menu_show), items);
2561 g_signal_connect(main_widgets.editor_menu, "hide", G_CALLBACK(on_editor_menu_hide), items);
2564 ui_init_toolbar_widgets();
2565 init_document_widgets();
2566 create_config_files_menu();
2570 void ui_finalize_builder(void)
2572 if (GTK_IS_BUILDER(builder))
2573 g_object_unref(builder);
2575 /* cleanup refs lingering even after GtkBuilder is destroyed */
2576 if (GTK_IS_WIDGET(edit_menu1))
2577 gtk_widget_destroy(edit_menu1);
2578 if (GTK_IS_WIDGET(prefs_dialog))
2579 gtk_widget_destroy(prefs_dialog);
2580 if (GTK_IS_WIDGET(project_dialog))
2581 gtk_widget_destroy(project_dialog);
2582 if (GTK_IS_WIDGET(toolbar_popup_menu1))
2583 gtk_widget_destroy(toolbar_popup_menu1);
2584 if (GTK_IS_WIDGET(window1))
2585 gtk_widget_destroy(window1);
2589 static void auto_separator_update(GeanyAutoSeparator *autosep)
2591 g_return_if_fail(autosep->item_count >= 0);
2593 if (autosep->widget)
2595 if (autosep->item_count > 0)
2596 ui_widget_show_hide(autosep->widget, autosep->show_count > 0);
2597 else
2598 gtk_widget_destroy(autosep->widget);
2603 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
2605 GeanyAutoSeparator *autosep = user_data;
2607 if (gtk_widget_get_visible(widget))
2608 autosep->show_count++;
2609 else
2610 autosep->show_count--;
2611 auto_separator_update(autosep);
2615 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
2617 GeanyAutoSeparator *autosep = user_data;
2619 autosep->item_count--;
2620 autosep->item_count = MAX(autosep->item_count, 0);
2621 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2622 * so assume widget was visible */
2623 autosep->show_count--;
2624 autosep->show_count = MAX(autosep->item_count, 0);
2625 auto_separator_update(autosep);
2629 /* Show the separator widget if @a item or another is visible. */
2630 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2631 * property, and using reference counting to keep the widget alive whilst its visible group
2632 * is alive. */
2633 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2635 /* set widget ptr NULL when widget destroyed */
2636 if (autosep->item_count == 0)
2637 g_signal_connect(autosep->widget, "destroy",
2638 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2640 if (gtk_widget_get_visible(item))
2641 autosep->show_count++;
2643 autosep->item_count++;
2644 auto_separator_update(autosep);
2646 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2647 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2648 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2653 * Sets @a text as the contents of the tooltip for @a widget.
2655 * @param widget The widget the tooltip should be set for.
2656 * @param text The text for the tooltip.
2658 * @since 0.16
2659 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2661 GEANY_API_SYMBOL
2662 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2664 gtk_widget_set_tooltip_text(widget, text);
2668 /** Returns a widget from a name in a component, usually created by Glade.
2669 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2670 * or alternatively any widget in the component, and the name of the widget
2671 * you want returned.
2672 * @param widget Widget with the @a widget_name property set.
2673 * @param widget_name Name to lookup.
2675 * @return @transfer{none} The widget found.
2676 * @see ui_hookup_widget().
2678 * @since 0.16
2680 GEANY_API_SYMBOL
2681 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2683 GtkWidget *parent, *found_widget;
2685 g_return_val_if_fail(widget != NULL, NULL);
2686 g_return_val_if_fail(widget_name != NULL, NULL);
2688 for (;;)
2690 if (GTK_IS_MENU(widget))
2691 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2692 else
2693 parent = gtk_widget_get_parent(widget);
2694 if (parent == NULL)
2695 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2696 if (parent == NULL)
2697 break;
2698 widget = parent;
2701 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2702 if (G_UNLIKELY(found_widget == NULL))
2703 g_warning("Widget not found: %s", widget_name);
2704 return found_widget;
2708 /* wraps gtk_builder_get_object()
2709 * unlike ui_lookup_widget(), it does only support getting object created from the main
2710 * UI file, but it can fetch any object, not only widgets */
2711 gpointer ui_builder_get_object (const gchar *name)
2713 return gtk_builder_get_object (builder, name);
2717 /* Progress Bar */
2718 static guint progress_bar_timer_id = 0;
2721 static GtkWidget *progress_bar_create(void)
2723 GtkWidget *bar = gtk_progress_bar_new();
2725 /* Set the progressbar's height to 1 to fit it in the statusbar */
2726 gtk_widget_set_size_request(bar, -1, 1);
2727 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2729 return bar;
2733 static gboolean progress_bar_pulse(gpointer data)
2735 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2737 return TRUE;
2742 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2743 * (if the statusbar is visible). This is a convenience function which adds a timer to
2744 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2745 * You can use this function when you have time consuming asynchronous operation and want to
2746 * display some activity in the GUI and when you don't know about detailed progress steps.
2747 * The progressbar widget is hidden by default when it is not active. This function and
2748 * ui_progress_bar_stop() will show and hide it automatically for you.
2750 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2751 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2752 * In this case, you need to show and hide the widget yourself. You can find some example code
2753 * in @c src/printing.c.
2755 * @param text @nullable The text to be shown as the progress bar label or @c NULL to leave it empty.
2757 * @since 0.16
2759 GEANY_API_SYMBOL
2760 void ui_progress_bar_start(const gchar *text)
2762 g_return_if_fail(progress_bar_timer_id == 0);
2764 if (! interface_prefs.statusbar_visible)
2765 return;
2767 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2769 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2771 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2775 /** Stops a running progress bar and hides the widget again.
2777 * @since 0.16
2779 GEANY_API_SYMBOL
2780 void ui_progress_bar_stop(void)
2782 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2784 if (progress_bar_timer_id != 0)
2786 g_source_remove(progress_bar_timer_id);
2787 progress_bar_timer_id = 0;
2792 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2794 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2795 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2796 gchar *sa, *sb;
2797 gint result;
2799 /* put entries with submenus at the end of the menu */
2800 if (gtk_menu_item_get_submenu(item_a) && !gtk_menu_item_get_submenu(item_b))
2801 return 1;
2802 else if (!gtk_menu_item_get_submenu(item_a) && gtk_menu_item_get_submenu(item_b))
2803 return -1;
2805 sa = ui_menu_item_get_text(item_a);
2806 sb = ui_menu_item_get_text(item_b);
2807 result = utils_str_casecmp(sa, sb);
2808 g_free(sa);
2809 g_free(sb);
2810 return result;
2814 /* Currently @a menu should contain only GtkMenuItems with labels. */
2815 static void ui_menu_sort_by_label(GtkMenu *menu)
2817 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2818 GList *node;
2819 gint pos;
2821 list = g_list_sort(list, compare_menu_item_labels);
2822 pos = 0;
2823 foreach_list(node, list)
2825 menu_reorder_child(menu, node->data, pos);
2826 pos++;
2828 g_list_free(list);
2832 void ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2834 va_list a;
2835 gchar *text;
2837 va_start(a, format);
2838 text = g_strdup_vprintf(format, a);
2839 va_end(a);
2841 gtk_label_set_text(label, text);
2842 gtk_label_set_use_markup(label, TRUE);
2843 g_free(text);
2847 GtkWidget *ui_label_new_bold(const gchar *text)
2849 GtkWidget *label;
2850 gchar *label_text;
2852 label_text = g_markup_escape_text(text, -1);
2853 label = gtk_label_new(NULL);
2854 ui_label_set_markup(GTK_LABEL(label), "<b>%s</b>", label_text);
2855 g_free(label_text);
2856 return label;
2860 /** @girskip
2861 * Adds a list of document items to @a menu.
2862 * @param menu Menu.
2863 * @param active @nullable Which document to highlight, or @c NULL.
2864 * @param callback is used for each menu item's @c "activate" signal and will be
2865 * passed the corresponding document pointer as @c user_data.
2866 * @warning You should check @c doc->is_valid in the callback.
2867 * @since 0.19
2869 GEANY_API_SYMBOL
2870 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2872 ui_menu_add_document_items_sorted(menu, active, callback, NULL);
2876 /** @girskip
2877 * Adds a list of document items to @a menu.
2879 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2880 * the order of the document tabs is used.
2882 * See document_compare_by_display_name() for an example sort function.
2884 * @param menu Menu.
2885 * @param active @nullable Which document to highlight, or @c NULL.
2886 * @param callback is used for each menu item's @c "activate" signal and will be passed
2887 * the corresponding document pointer as @c user_data.
2888 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2889 * @warning You should check @c doc->is_valid in the callback.
2890 * @since 0.21
2892 GEANY_API_SYMBOL
2893 void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
2894 GCallback callback, GCompareFunc compare_func)
2896 GtkWidget *menu_item, *menu_item_label, *image;
2897 GeanyDocument *doc;
2898 guint i, len;
2899 gchar *base_name, *label;
2900 GPtrArray *sorted_documents;
2902 len = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2904 sorted_documents = g_ptr_array_sized_new(len);
2905 /* copy the documents_array into the new one */
2906 foreach_document(i)
2908 g_ptr_array_add(sorted_documents, documents[i]);
2910 if (compare_func == NULL)
2911 compare_func = document_compare_by_tab_order;
2913 /* and now sort it */
2914 g_ptr_array_sort(sorted_documents, compare_func);
2916 for (i = 0; i < sorted_documents->len; i++)
2918 doc = g_ptr_array_index(sorted_documents, i);
2920 base_name = g_path_get_basename(DOC_FILENAME(doc));
2921 menu_item = gtk_image_menu_item_new_with_label(base_name);
2922 image = gtk_image_new_from_gicon(doc->file_type->icon, GTK_ICON_SIZE_MENU);
2923 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
2925 gtk_widget_show(menu_item);
2926 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2927 g_signal_connect(menu_item, "activate", callback, doc);
2929 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2930 gtk_widget_set_name(menu_item_label, document_get_status_widget_class(doc));
2932 if (doc == active)
2934 label = g_markup_escape_text(base_name, -1);
2935 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", label);
2936 g_free(label);
2939 g_free(base_name);
2941 g_ptr_array_free(sorted_documents, TRUE);
2945 /** Checks whether the passed @a keyval is the Enter or Return key.
2946 * There are three different Enter/Return key values
2947 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2948 * This is just a convenience function.
2949 * @param keyval A keyval.
2950 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2951 * @since 0.19 */
2952 GEANY_API_SYMBOL
2953 gboolean ui_is_keyval_enter_or_return(guint keyval)
2955 return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
2959 /** Reads an integer from the GTK default settings registry
2960 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2961 * @param property_name The property to read.
2962 * @param default_value The default value in case the value could not be read.
2963 * @return The value for the property if it exists, otherwise the @a default_value.
2964 * @since 0.19 */
2965 GEANY_API_SYMBOL
2966 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
2968 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2969 gtk_settings_get_default())), property_name))
2971 gint value;
2972 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
2973 return value;
2975 else
2976 return default_value;
2980 void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
2981 gint new_text_len, gint *position, gpointer data)
2983 gboolean first = position != NULL && *position == 0;
2984 gint i;
2986 if (new_text_len == -1)
2987 new_text_len = (gint) strlen(new_text);
2989 for (i = 0; i < new_text_len; i++, new_text++)
2991 if ((!first || !strchr("+-", *new_text)) && !isdigit(*new_text))
2993 g_signal_stop_emission_by_name(editable, "insert-text");
2994 break;
2996 first = FALSE;
3001 /* gets the icon that applies to a particular MIME type */
3002 GIcon *ui_get_mime_icon(const gchar *mime_type)
3004 GIcon *icon = NULL;
3005 gchar *ctype;
3007 ctype = g_content_type_from_mime_type(mime_type);
3008 if (ctype)
3010 GdkScreen *screen = gdk_screen_get_default();
3012 icon = g_content_type_get_icon(ctype);
3013 if (screen && icon)
3015 GtkIconInfo *icon_info;
3017 icon_info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_for_screen(screen), icon, 16, 0);
3018 if (!icon_info)
3020 g_object_unref(icon);
3021 icon = NULL;
3023 else
3024 gtk_icon_info_free(icon_info);
3027 g_free(ctype);
3030 /* fallback if icon lookup failed, like it might happen on Windows (?) */
3031 if (! icon)
3033 const gchar *icon_name = "text-x-generic";
3035 if (strstr(mime_type, "directory"))
3036 icon_name = "folder";
3038 icon = g_themed_icon_new(icon_name);
3040 return icon;
3044 void ui_focus_current_document(void)
3046 GeanyDocument *doc = document_get_current();
3048 if (doc != NULL)
3049 document_grab_focus(doc);
3053 /** Finds the label text associated with stock_id
3054 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
3055 * @return The label text for stock
3056 * @since Geany 1.22 */
3057 GEANY_API_SYMBOL
3058 const gchar *ui_lookup_stock_label(const gchar *stock_id)
3060 GtkStockItem item;
3062 if (gtk_stock_lookup(stock_id, &item))
3063 return item.label;
3065 g_warning("No stock id '%s'!", stock_id);
3066 return NULL;
3070 /* finds the next iter at any level
3071 * @param iter in/out, the current iter, will be changed to the next one
3072 * @param down whether to try the child iter
3073 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
3074 gboolean ui_tree_model_iter_any_next(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
3076 GtkTreeIter guess;
3077 GtkTreeIter copy = *iter;
3079 /* go down if the item has children */
3080 if (down && gtk_tree_model_iter_children(model, &guess, iter))
3081 *iter = guess;
3082 /* or to the next item at the same level */
3083 else if (gtk_tree_model_iter_next(model, &copy))
3084 *iter = copy;
3085 /* or to the next item at a parent level */
3086 else if (gtk_tree_model_iter_parent(model, &guess, iter))
3088 copy = guess;
3089 while (TRUE)
3091 if (gtk_tree_model_iter_next(model, &copy))
3093 *iter = copy;
3094 return TRUE;
3096 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
3097 guess = copy;
3098 else
3099 return FALSE;
3102 else
3103 return FALSE;
3105 return TRUE;
3109 GtkWidget *ui_create_encodings_combo_box(gboolean has_detect, gint default_enc)
3111 GtkCellRenderer *renderer;
3112 GtkTreeIter iter;
3113 GtkWidget *combo = gtk_combo_box_new();
3114 GtkTreeStore *store = encodings_encoding_store_new(has_detect);
3116 if (default_enc < 0 || default_enc >= GEANY_ENCODINGS_MAX)
3117 default_enc = has_detect ? GEANY_ENCODINGS_MAX : GEANY_ENCODING_NONE;
3119 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
3120 if (encodings_encoding_store_get_iter(store, &iter, default_enc))
3121 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
3122 renderer = gtk_cell_renderer_text_new();
3123 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
3124 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
3125 encodings_encoding_store_cell_data_func, NULL, NULL);
3127 return combo;
3131 gint ui_encodings_combo_box_get_active_encoding(GtkComboBox *combo)
3133 GtkTreeIter iter;
3134 gint enc = GEANY_ENCODING_NONE;
3136 /* there should always be an active iter anyway, but we check just in case */
3137 if (gtk_combo_box_get_active_iter(combo, &iter))
3139 GtkTreeModel *model = gtk_combo_box_get_model(combo);
3140 enc = encodings_encoding_store_get_encoding(GTK_TREE_STORE(model), &iter);
3143 return enc;
3147 gboolean ui_encodings_combo_box_set_active_encoding(GtkComboBox *combo, gint enc)
3149 GtkTreeIter iter;
3150 GtkTreeModel *model = gtk_combo_box_get_model(combo);
3152 if (encodings_encoding_store_get_iter(GTK_TREE_STORE(model), &iter, enc))
3154 gtk_combo_box_set_active_iter(combo, &iter);
3155 return TRUE;
3157 return FALSE;