Add detection of PKGBUILD files as Shell scripts
[geany-mirror.git] / src / ui_utils.c
blobf495945ed668b9da3d55e10644d47ba3a8808a99
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 #include "geany.h"
29 #include "support.h"
31 #include <string.h>
32 #include <ctype.h>
33 #include <gdk/gdkkeysyms.h>
35 #include "ui_utils.h"
36 #include "dialogs.h"
37 #include "prefs.h"
38 #include "sciwrappers.h"
39 #include "document.h"
40 #include "documentprivate.h"
41 #include "filetypes.h"
42 #include "support.h"
43 #include "msgwindow.h"
44 #include "utils.h"
45 #include "callbacks.h"
46 #include "encodings.h"
47 #include "sidebar.h"
48 #include "win32.h"
49 #include "project.h"
50 #include "editor.h"
51 #include "plugins.h"
52 #include "symbols.h"
53 #include "toolbar.h"
54 #include "geanymenubuttonaction.h"
55 #include "main.h"
56 #include "stash.h"
57 #include "keyfile.h"
58 #include "gtkcompat.h"
61 #define DEFAULT_STATUSBAR_TEMPLATE N_(\
62 "line: %l / %L\t " \
63 "col: %c\t " \
64 "sel: %s\t " \
65 "%w %t %m" \
66 "mode: %M " \
67 "encoding: %e " \
68 "filetype: %f " \
69 "scope: %S")
71 GeanyInterfacePrefs interface_prefs;
72 GeanyMainWidgets main_widgets;
74 UIPrefs ui_prefs;
75 UIWidgets ui_widgets;
77 static GtkBuilder *builder = NULL;
78 static GtkWidget* window1 = NULL;
79 static GtkWidget* toolbar_popup_menu1 = NULL;
80 static GtkWidget* edit_menu1 = NULL;
81 static GtkWidget* prefs_dialog = NULL;
82 static GtkWidget* project_dialog = NULL;
84 static struct
86 /* pointers to widgets only sensitive when there is at least one document, the pointers can
87 * also be GtkAction objects, so check each pointer before using it */
88 GPtrArray *document_buttons;
89 GtkWidget *menu_insert_include_items[2];
90 GtkWidget *popup_goto_items[4];
91 GtkWidget *popup_copy_items[3];
92 GtkWidget *menu_copy_items[3];
93 GtkWidget *redo_items[3];
94 GtkWidget *undo_items[3];
95 GtkWidget *save_buttons[4];
96 GtkWidget *config_files_menu;
98 widgets;
100 enum
102 RECENT_FILE_FILE,
103 RECENT_FILE_PROJECT
106 typedef struct
108 gint type;
109 GQueue *recent_queue;
110 GtkWidget *menubar;
111 GtkWidget *toolbar;
112 void (*activate_cb)(GtkMenuItem *, gpointer);
113 } GeanyRecentFiles;
116 static void update_recent_menu(GeanyRecentFiles *grf);
117 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
118 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
119 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
120 static GtkWidget *progress_bar_create(void);
123 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
124 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
126 if (widget != NULL)
127 gtk_widget_set_sensitive(widget, set);
131 /* allow_override is TRUE if text can be ignored when another message has been set
132 * that didn't use allow_override and has not timed out. */
133 static void set_statusbar(const gchar *text, gboolean allow_override)
135 static guint id = 0;
136 static glong last_time = 0;
137 GTimeVal timeval;
138 const gint GEANY_STATUS_TIMEOUT = 1;
140 if (! interface_prefs.statusbar_visible)
141 return; /* just do nothing if statusbar is not visible */
143 if (id == 0)
144 id = gtk_statusbar_get_context_id(GTK_STATUSBAR(ui_widgets.statusbar), "geany-main");
146 g_get_current_time(&timeval);
148 if (! allow_override)
150 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
151 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
152 last_time = timeval.tv_sec;
154 else
155 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
157 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
158 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
163 /** Displays text on the statusbar.
164 * @param log Whether the message should be recorded in the Status window.
165 * @param format A @c printf -style string. */
166 void ui_set_statusbar(gboolean log, const gchar *format, ...)
168 gchar *string;
169 va_list args;
171 va_start(args, format);
172 string = g_strdup_vprintf(format, args);
173 va_end(args);
175 if (! prefs.suppress_status_messages)
176 set_statusbar(string, FALSE);
178 if (log || prefs.suppress_status_messages)
179 msgwin_status_add("%s", string);
181 g_free(string);
185 /* note: some comments below are for translators */
186 static gchar *create_statusbar_statistics(GeanyDocument *doc,
187 guint line, guint col, guint pos)
189 const gchar *cur_tag;
190 const gchar *fmt;
191 const gchar *expos; /* % expansion position */
192 const gchar sp[] = " ";
193 GString *stats_str;
194 ScintillaObject *sci = doc->editor->sci;
196 if (!EMPTY(ui_prefs.statusbar_template))
197 fmt = ui_prefs.statusbar_template;
198 else
199 fmt = _(DEFAULT_STATUSBAR_TEMPLATE);
201 stats_str = g_string_sized_new(120);
203 while ((expos = strchr(fmt, '%')) != NULL)
205 /* append leading text before % char */
206 g_string_append_len(stats_str, fmt, expos - fmt);
208 switch (*++expos)
210 case 'l':
211 g_string_append_printf(stats_str, "%d", line + 1);
212 break;
213 case 'L':
214 g_string_append_printf(stats_str, "%d",
215 sci_get_line_count(doc->editor->sci));
216 break;
217 case 'c':
218 g_string_append_printf(stats_str, "%d", col);
219 break;
220 case 'C':
221 g_string_append_printf(stats_str, "%d", col + 1);
222 break;
223 case 'p':
224 g_string_append_printf(stats_str, "%u", pos);
225 break;
226 case 's':
228 gint len = sci_get_selected_text_length(sci) - 1;
229 /* check if whole lines are selected */
230 if (!len || sci_get_col_from_position(sci,
231 sci_get_selection_start(sci)) != 0 ||
232 sci_get_col_from_position(sci,
233 sci_get_selection_end(sci)) != 0)
234 g_string_append_printf(stats_str, "%d", len);
235 else /* L = lines */
236 g_string_append_printf(stats_str, _("%dL"),
237 sci_get_lines_selected(doc->editor->sci) - 1);
238 break;
240 case 'w':
241 /* RO = read-only */
242 g_string_append(stats_str, (doc->readonly) ? _("RO ") :
243 /* OVR = overwrite/overtype, INS = insert */
244 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
245 break;
246 case 'r':
247 if (doc->readonly)
249 g_string_append(stats_str, _("RO ")); /* RO = read-only */
250 g_string_append(stats_str, sp + 1);
252 break;
253 case 't':
255 switch (editor_get_indent_prefs(doc->editor)->type)
257 case GEANY_INDENT_TYPE_TABS:
258 g_string_append(stats_str, _("TAB"));
259 break;
260 case GEANY_INDENT_TYPE_SPACES: /* SP = space */
261 g_string_append(stats_str, _("SP"));
262 break;
263 case GEANY_INDENT_TYPE_BOTH: /* T/S = tabs and spaces */
264 g_string_append(stats_str, _("T/S"));
265 break;
267 break;
269 case 'm':
270 if (doc->changed)
272 g_string_append(stats_str, _("MOD")); /* MOD = modified */
273 g_string_append(stats_str, sp);
275 break;
276 case 'M':
277 g_string_append(stats_str, editor_get_eol_char_name(doc->editor));
278 break;
279 case 'e':
280 g_string_append(stats_str,
281 doc->encoding ? doc->encoding : _("unknown"));
282 if (encodings_is_unicode_charset(doc->encoding) && (doc->has_bom))
284 g_string_append_c(stats_str, ' ');
285 g_string_append(stats_str, _("(with BOM)")); /* BOM = byte order mark */
287 break;
288 case 'f':
289 g_string_append(stats_str, filetypes_get_display_name(doc->file_type));
290 break;
291 case 'S':
292 symbols_get_current_scope(doc, &cur_tag);
293 g_string_append(stats_str, cur_tag);
294 break;
295 case 'Y':
296 g_string_append_c(stats_str, ' ');
297 g_string_append_printf(stats_str, "%d",
298 sci_get_style_at(doc->editor->sci, pos));
299 break;
300 default:
301 g_string_append_len(stats_str, expos, 1);
304 /* skip past %c chars */
305 if (*expos)
306 fmt = expos + 1;
307 else
308 break;
310 /* add any remaining text */
311 g_string_append(stats_str, fmt);
313 return g_string_free(stats_str, FALSE);
317 /* updates the status bar document statistics */
318 void ui_update_statusbar(GeanyDocument *doc, gint pos)
320 if (! interface_prefs.statusbar_visible)
321 return; /* just do nothing if statusbar is not visible */
323 if (doc == NULL)
324 doc = document_get_current();
326 if (doc != NULL)
328 guint line, col;
329 gchar *stats_str;
331 if (pos == -1)
332 pos = sci_get_current_position(doc->editor->sci);
333 line = sci_get_line_from_position(doc->editor->sci, pos);
335 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
336 * when current pos is beyond document end (can occur when removing
337 * blocks of selected lines especially esp. brace sections near end of file). */
338 if (pos <= sci_get_length(doc->editor->sci))
339 col = sci_get_col_from_position(doc->editor->sci, pos);
340 else
341 col = 0;
343 stats_str = create_statusbar_statistics(doc, line, col, pos);
345 /* can be overridden by status messages */
346 set_statusbar(stats_str, TRUE);
347 g_free(stats_str);
349 else /* no documents */
351 set_statusbar("", TRUE); /* can be overridden by status messages */
356 /* This sets the window title according to the current filename. */
357 void ui_set_window_title(GeanyDocument *doc)
359 GString *str;
360 GeanyProject *project = app->project;
362 if (doc == NULL)
363 doc = document_get_current();
365 str = g_string_new(NULL);
367 if (doc != NULL)
369 g_string_append(str, doc->changed ? "*" : "");
371 if (doc->file_name == NULL)
372 g_string_append(str, DOC_FILENAME(doc));
373 else
375 gchar *short_name = document_get_basename_for_display(doc, 30);
376 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
378 g_string_append(str, short_name);
379 g_string_append(str, " - ");
380 g_string_append(str, dirname ? dirname : "");
381 g_free(short_name);
382 g_free(dirname);
384 g_string_append(str, " - ");
386 if (project)
388 g_string_append_c(str, '[');
389 g_string_append(str, project->name);
390 g_string_append(str, "] - ");
392 g_string_append(str, "Geany");
393 if (cl_options.new_instance)
395 g_string_append(str, _(" (new instance)"));
397 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
398 g_string_free(str, TRUE);
402 void ui_set_editor_font(const gchar *font_name)
404 guint i;
406 g_return_if_fail(font_name != NULL);
408 /* do nothing if font has not changed */
409 if (interface_prefs.editor_font != NULL)
410 if (strcmp(font_name, interface_prefs.editor_font) == 0)
411 return;
413 g_free(interface_prefs.editor_font);
414 interface_prefs.editor_font = g_strdup(font_name);
416 /* We copy the current style, and update the font in all open tabs. */
417 for (i = 0; i < documents_array->len; i++)
419 if (documents[i]->editor)
421 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
425 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
429 void ui_set_fullscreen(void)
431 if (ui_prefs.fullscreen)
433 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
435 else
437 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
442 void ui_update_popup_reundo_items(GeanyDocument *doc)
444 gboolean enable_undo;
445 gboolean enable_redo;
446 guint i, len;
448 if (doc == NULL)
450 enable_undo = FALSE;
451 enable_redo = FALSE;
453 else
455 enable_undo = document_can_undo(doc);
456 enable_redo = document_can_redo(doc);
459 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
460 len = G_N_ELEMENTS(widgets.undo_items);
461 for (i = 0; i < len; i++)
463 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
465 len = G_N_ELEMENTS(widgets.redo_items);
466 for (i = 0; i < len; i++)
468 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
473 void ui_update_popup_copy_items(GeanyDocument *doc)
475 gboolean enable;
476 guint i, len;
478 if (doc == NULL)
479 enable = FALSE;
480 else
481 enable = sci_has_selection(doc->editor->sci);
483 len = G_N_ELEMENTS(widgets.popup_copy_items);
484 for (i = 0; i < len; i++)
485 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
489 void ui_update_popup_goto_items(gboolean enable)
491 guint i, len;
492 len = G_N_ELEMENTS(widgets.popup_goto_items);
493 for (i = 0; i < len; i++)
494 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
498 void ui_update_menu_copy_items(GeanyDocument *doc)
500 gboolean enable = FALSE;
501 guint i, len;
502 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
504 if (IS_SCINTILLA(focusw))
505 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
506 else
507 if (GTK_IS_EDITABLE(focusw))
508 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
509 else
510 if (GTK_IS_TEXT_VIEW(focusw))
512 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
513 GTK_TEXT_VIEW(focusw));
514 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
517 len = G_N_ELEMENTS(widgets.menu_copy_items);
518 for (i = 0; i < len; i++)
519 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
523 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
525 gboolean enable = FALSE;
527 if (doc == NULL || doc->file_type == NULL)
528 enable = FALSE;
529 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
530 enable = TRUE;
532 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
536 void ui_update_fold_items(void)
538 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
539 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
540 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
544 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
546 guint i = 0;
547 GtkWidget *tmp_menu;
548 GtkWidget *tmp_popup;
549 GtkWidget *edit_menu, *edit_menu_item;
550 GtkWidget *popup_menu, *popup_menu_item;
552 edit_menu = gtk_menu_new();
553 popup_menu = gtk_menu_new();
554 edit_menu_item = gtk_menu_item_new_with_label(label);
555 popup_menu_item = gtk_menu_item_new_with_label(label);
556 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
557 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
559 while (includes[i] != NULL)
561 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
562 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
563 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
564 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
565 g_signal_connect(tmp_menu, "activate",
566 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
567 g_signal_connect(tmp_popup, "activate",
568 G_CALLBACK(on_insert_include_activate), (gpointer) includes[i]);
569 i++;
571 gtk_widget_show_all(edit_menu_item);
572 gtk_widget_show_all(popup_menu_item);
573 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
574 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
578 void ui_create_insert_menu_items(void)
580 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
581 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
582 GtkWidget *blank;
583 const gchar *c_includes_stdlib[] = {
584 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
585 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
587 const gchar *c_includes_c99[] = {
588 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
589 "tgmath.h", "wchar.h", "wctype.h", NULL
591 const gchar *c_includes_cpp[] = {
592 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
594 const gchar *c_includes_cppstdlib[] = {
595 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
596 "memory", "locale", NULL
598 const gchar *c_includes_stl[] = {
599 "bitset", "dequev", "list", "map", "set", "queue", "stack", "vector", "algorithm",
600 "iterator", "functional", "string", "complex", "valarray", NULL
603 blank = gtk_menu_item_new_with_label("#include \"...\"");
604 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
605 gtk_widget_show(blank);
606 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate),
607 (gpointer) "blank");
608 blank = gtk_separator_menu_item_new ();
609 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
610 gtk_widget_show(blank);
612 blank = gtk_menu_item_new_with_label("#include \"...\"");
613 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
614 gtk_widget_show(blank);
615 g_signal_connect(blank, "activate", G_CALLBACK(on_insert_include_activate),
616 (gpointer) "blank");
617 blank = gtk_separator_menu_item_new();
618 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
619 gtk_widget_show(blank);
621 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
622 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
623 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
624 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
625 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
629 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
631 GtkWidget *item;
633 item = gtk_menu_item_new_with_mnemonic(label);
634 gtk_container_add(GTK_CONTAINER(me), item);
635 gtk_widget_show(item);
636 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
638 item = gtk_menu_item_new_with_mnemonic(label);
639 gtk_container_add(GTK_CONTAINER(mp), item);
640 gtk_widget_show(item);
641 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), label);
645 void ui_create_insert_date_menu_items(void)
647 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
648 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
649 GtkWidget *item;
650 gchar *str;
652 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
653 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
654 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
656 item = gtk_separator_menu_item_new();
657 gtk_container_add(GTK_CONTAINER(menu_edit), item);
658 gtk_widget_show(item);
659 item = gtk_separator_menu_item_new();
660 gtk_container_add(GTK_CONTAINER(menu_popup), item);
661 gtk_widget_show(item);
663 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
664 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
665 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
667 item = gtk_separator_menu_item_new();
668 gtk_container_add(GTK_CONTAINER(menu_edit), item);
669 gtk_widget_show(item);
670 item = gtk_separator_menu_item_new();
671 gtk_container_add(GTK_CONTAINER(menu_popup), item);
672 gtk_widget_show(item);
674 str = _("_Use Custom Date Format");
675 item = gtk_menu_item_new_with_mnemonic(str);
676 gtk_container_add(GTK_CONTAINER(menu_edit), item);
677 gtk_widget_show(item);
678 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
679 ui_hookup_widget(main_widgets.window, item, "insert_date_custom1");
681 item = gtk_menu_item_new_with_mnemonic(str);
682 gtk_container_add(GTK_CONTAINER(menu_popup), item);
683 gtk_widget_show(item);
684 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), str);
685 ui_hookup_widget(main_widgets.editor_menu, item, "insert_date_custom2");
687 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
691 void ui_save_buttons_toggle(gboolean enable)
693 guint i;
694 gboolean dirty_tabs = FALSE;
696 if (ui_prefs.allow_always_save)
697 enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
699 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
700 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
702 /* save all menu item and tool button */
703 for (i = 0; i < documents_array->len; i++)
705 /* check whether there are files where changes were made and if there are some,
706 * we need the save all button / item */
707 if (documents[i]->is_valid && documents[i]->changed)
709 dirty_tabs = TRUE;
710 break;
714 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
715 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
719 #define add_doc_widget(widget_name) \
720 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
722 #define add_doc_toolitem(widget_name) \
723 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
725 static void init_document_widgets(void)
727 widgets.document_buttons = g_ptr_array_new();
729 /* Cache the document-sensitive widgets so we don't have to keep looking them up
730 * when using ui_document_buttons_update(). */
731 add_doc_widget("menu_close1");
732 add_doc_widget("close_other_documents1");
733 add_doc_widget("menu_change_font1");
734 add_doc_widget("menu_close_all1");
735 add_doc_widget("menu_save1");
736 add_doc_widget("menu_save_all1");
737 add_doc_widget("menu_save_as1");
738 add_doc_widget("menu_count_words1");
739 add_doc_widget("menu_build1");
740 add_doc_widget("add_comments1");
741 add_doc_widget("menu_paste1");
742 add_doc_widget("menu_undo2");
743 add_doc_widget("preferences2");
744 add_doc_widget("menu_reload1");
745 add_doc_widget("menu_document1");
746 add_doc_widget("menu_choose_color1");
747 add_doc_widget("menu_color_schemes");
748 add_doc_widget("menu_markers_margin1");
749 add_doc_widget("menu_linenumber_margin1");
750 add_doc_widget("menu_show_white_space1");
751 add_doc_widget("menu_show_line_endings1");
752 add_doc_widget("menu_show_indentation_guides1");
753 add_doc_widget("menu_zoom_in1");
754 add_doc_widget("menu_zoom_out1");
755 add_doc_widget("normal_size1");
756 add_doc_widget("treeview6");
757 add_doc_widget("print1");
758 add_doc_widget("menu_reload_as1");
759 add_doc_widget("menu_select_all1");
760 add_doc_widget("insert_date1");
761 add_doc_widget("insert_alternative_white_space1");
762 add_doc_widget("menu_format1");
763 add_doc_widget("commands2");
764 add_doc_widget("menu_open_selected_file1");
765 add_doc_widget("page_setup1");
766 add_doc_widget("find1");
767 add_doc_widget("find_next1");
768 add_doc_widget("find_previous1");
769 add_doc_widget("go_to_next_marker1");
770 add_doc_widget("go_to_previous_marker1");
771 add_doc_widget("replace1");
772 add_doc_widget("find_nextsel1");
773 add_doc_widget("find_prevsel1");
774 add_doc_widget("find_usage1");
775 add_doc_widget("find_document_usage1");
776 add_doc_widget("mark_all1");
777 add_doc_widget("go_to_line1");
778 add_doc_widget("goto_tag_definition1");
779 add_doc_widget("goto_tag_declaration1");
780 add_doc_widget("reset_indentation1");
781 add_doc_toolitem("Close");
782 add_doc_toolitem("CloseAll");
783 add_doc_toolitem("Search");
784 add_doc_toolitem("SearchEntry");
785 add_doc_toolitem("ZoomIn");
786 add_doc_toolitem("ZoomOut");
787 add_doc_toolitem("Indent");
788 add_doc_toolitem("UnIndent");
789 add_doc_toolitem("Cut");
790 add_doc_toolitem("Copy");
791 add_doc_toolitem("Paste");
792 add_doc_toolitem("Delete");
793 add_doc_toolitem("Save");
794 add_doc_toolitem("SaveAs");
795 add_doc_toolitem("SaveAll");
796 add_doc_toolitem("Compile");
797 add_doc_toolitem("Run");
798 add_doc_toolitem("Reload");
799 add_doc_toolitem("Color");
800 add_doc_toolitem("Goto");
801 add_doc_toolitem("GotoEntry");
802 add_doc_toolitem("Replace");
803 add_doc_toolitem("Print");
807 void ui_document_buttons_update(void)
809 guint i;
810 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
812 for (i = 0; i < widgets.document_buttons->len; i++)
814 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
815 if (GTK_IS_ACTION(widget))
816 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
817 else
818 ui_widget_set_sensitive(widget, enable);
823 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
825 g_ptr_array_remove_fast(widgets.document_buttons, widget);
829 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
830 * when some documents are present/no documents are open.
831 * It will be removed when the widget is destroyed.
832 * @param widget The widget to add.
834 * @since 0.15
836 void ui_add_document_sensitive(GtkWidget *widget)
838 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
840 ui_widget_set_sensitive(widget, enable);
842 g_ptr_array_add(widgets.document_buttons, widget);
843 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
847 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
849 if (show)
851 gtk_widget_show(widget);
853 else
855 gtk_widget_hide(widget);
860 void ui_sidebar_show_hide(void)
862 GtkWidget *widget;
864 /* check that there are no other notebook pages before hiding the sidebar completely
865 * other pages could be e.g. the file browser plugin */
866 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
867 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
869 ui_prefs.sidebar_visible = FALSE;
872 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
873 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
875 ignore_callback = TRUE;
876 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
877 ignore_callback = FALSE;
880 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
882 ui_widget_show_hide(gtk_notebook_get_nth_page(
883 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
884 ui_widget_show_hide(gtk_notebook_get_nth_page(
885 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
889 void ui_document_show_hide(GeanyDocument *doc)
891 const gchar *widget_name;
892 GtkWidget *item;
893 const GeanyIndentPrefs *iprefs;
895 if (doc == NULL)
896 doc = document_get_current();
898 if (doc == NULL)
899 return;
901 ignore_callback = TRUE;
903 gtk_check_menu_item_set_active(
904 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
905 doc->editor->line_wrapping);
907 gtk_check_menu_item_set_active(
908 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
909 doc->editor->line_breaking);
911 iprefs = editor_get_indent_prefs(doc->editor);
913 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
914 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
916 switch (iprefs->type)
918 case GEANY_INDENT_TYPE_SPACES:
919 widget_name = "spaces1"; break;
920 case GEANY_INDENT_TYPE_TABS:
921 widget_name = "tabs1"; break;
922 case GEANY_INDENT_TYPE_BOTH:
923 default:
924 widget_name = "tabs_and_spaces1"; break;
926 item = ui_lookup_widget(main_widgets.window, widget_name);
927 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
929 if (iprefs->width >= 1 && iprefs->width <= 8)
931 gchar *name;
933 name = g_strdup_printf("indent_width_%d", iprefs->width);
934 item = ui_lookup_widget(main_widgets.window, name);
935 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
936 g_free(name);
939 gtk_check_menu_item_set_active(
940 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
941 doc->readonly);
943 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
944 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
945 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
947 switch (sci_get_eol_mode(doc->editor->sci))
949 case SC_EOL_CR: widget_name = "cr"; break;
950 case SC_EOL_LF: widget_name = "lf"; break;
951 default: widget_name = "crlf"; break;
953 gtk_check_menu_item_set_active(
954 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
956 encodings_select_radio_item(doc->encoding);
957 filetypes_select_radio_item(doc->file_type);
959 ignore_callback = FALSE;
963 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
965 gtk_widget_set_name(widget, success ? NULL : "geany-search-entry-no-match");
969 static void recent_create_menu(GeanyRecentFiles *grf)
971 GtkWidget *tmp;
972 guint i, len;
973 gchar *filename;
975 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
976 for (i = 0; i < len; i++)
978 filename = g_queue_peek_nth(grf->recent_queue, i);
979 /* create menu item for the recent files menu in the menu bar */
980 tmp = gtk_menu_item_new_with_label(filename);
981 gtk_widget_show(tmp);
982 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
983 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
984 /* create menu item for the recent files menu in the toolbar */
985 if (grf->toolbar != NULL)
987 tmp = gtk_menu_item_new_with_label(filename);
988 gtk_widget_show(tmp);
989 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
990 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
996 static GeanyRecentFiles *recent_get_recent_files(void)
998 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
1000 if (G_UNLIKELY(grf.recent_queue == NULL))
1002 grf.recent_queue = ui_prefs.recent_queue;
1003 grf.menubar = ui_widgets.recent_files_menu_menubar;
1004 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1005 toolbar_get_action_by_name("Open")));
1006 grf.activate_cb = recent_file_activate_cb;
1008 return &grf;
1012 static GeanyRecentFiles *recent_get_recent_projects(void)
1014 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
1016 if (G_UNLIKELY(grf.recent_queue == NULL))
1018 grf.recent_queue = ui_prefs.recent_projects_queue;
1019 grf.menubar = ui_widgets.recent_projects_menu_menubar;
1020 grf.toolbar = NULL;
1021 grf.activate_cb = recent_project_activate_cb;
1023 return &grf;
1027 void ui_create_recent_menus(void)
1029 recent_create_menu(recent_get_recent_files());
1030 recent_create_menu(recent_get_recent_projects());
1034 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1036 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1037 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1039 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1040 recent_file_loaded(utf8_filename, recent_get_recent_files());
1042 g_free(locale_filename);
1043 g_free(utf8_filename);
1047 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1049 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1050 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1052 if (project_ask_close() && project_load_file_with_session(locale_filename))
1053 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1055 g_free(locale_filename);
1056 g_free(utf8_filename);
1060 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf,
1061 const GtkRecentData *rdata)
1063 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1066 if (grf->type == RECENT_FILE_FILE && rdata)
1068 GtkRecentManager *manager = gtk_recent_manager_get_default();
1069 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1070 if (uri != NULL)
1072 gtk_recent_manager_add_full(manager, uri, rdata);
1073 g_free(uri);
1077 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1078 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1080 g_free(g_queue_pop_tail(grf->recent_queue));
1082 update_recent_menu(grf);
1084 /* filename already in recent list */
1085 else
1086 recent_file_loaded(utf8_filename, grf);
1090 void ui_add_recent_document(GeanyDocument *doc)
1092 /* what are the groups for actually? */
1093 static const gchar *groups[2] = {
1094 "geany",
1095 NULL
1097 GtkRecentData rdata;
1099 /* Prepare the data for gtk_recent_manager_add_full() */
1100 rdata.display_name = NULL;
1101 rdata.description = NULL;
1102 rdata.mime_type = doc->file_type->mime_type;
1103 /* if we ain't got no mime-type, fallback to plain text */
1104 if (! rdata.mime_type)
1105 rdata.mime_type = (gchar *) "text/plain";
1106 rdata.app_name = (gchar *) "geany";
1107 rdata.app_exec = (gchar *) "geany %u";
1108 rdata.groups = (gchar **) groups;
1109 rdata.is_private = FALSE;
1111 add_recent_file(doc->file_name, recent_get_recent_files(), &rdata);
1115 void ui_add_recent_project_file(const gchar *utf8_filename)
1117 add_recent_file(utf8_filename, recent_get_recent_projects(), NULL);
1121 /* Returns: newly allocated string with the UTF-8 menu text. */
1122 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1124 const gchar *text = NULL;
1126 if (gtk_bin_get_child(GTK_BIN(menu_item)))
1128 GtkWidget *child = gtk_bin_get_child(GTK_BIN(menu_item));
1130 if (GTK_IS_LABEL(child))
1131 text = gtk_label_get_text(GTK_LABEL(child));
1133 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1134 return g_strdup(text);
1138 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1140 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1141 gint result;
1143 if (utils_str_equal(menu_text, user_data))
1144 result = 0;
1145 else
1146 result = 1;
1148 g_free(menu_text);
1149 return result;
1153 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1155 GList *item, *children;
1156 void *data;
1157 GtkWidget *tmp;
1159 /* first reorder the queue */
1160 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1161 g_return_if_fail(item != NULL);
1163 data = item->data;
1164 g_queue_remove(grf->recent_queue, data);
1165 g_queue_push_head(grf->recent_queue, data);
1167 /* remove the old menuitem for the filename */
1168 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1169 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1170 if (item != NULL)
1171 gtk_widget_destroy(GTK_WIDGET(item->data));
1172 g_list_free(children);
1174 if (grf->toolbar != NULL)
1176 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1177 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1178 if (item != NULL)
1179 gtk_widget_destroy(GTK_WIDGET(item->data));
1180 g_list_free(children);
1182 /* now prepend a new menuitem for the filename,
1183 * first for the recent files menu in the menu bar */
1184 tmp = gtk_menu_item_new_with_label(utf8_filename);
1185 gtk_widget_show(tmp);
1186 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1187 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1188 /* then for the recent files menu in the tool bar */
1189 if (grf->toolbar != NULL)
1191 tmp = gtk_menu_item_new_with_label(utf8_filename);
1192 gtk_widget_show(tmp);
1193 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1194 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1195 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal which we need in
1196 * GeanyMenubuttonAction */
1197 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1198 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1203 static void update_recent_menu(GeanyRecentFiles *grf)
1205 GtkWidget *tmp;
1206 gchar *filename;
1207 GList *children, *item;
1209 filename = g_queue_peek_head(grf->recent_queue);
1211 /* clean the MRU list before adding an item (menubar) */
1212 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1213 if (g_list_length(children) > file_prefs.mru_length - 1)
1215 item = g_list_nth(children, file_prefs.mru_length - 1);
1216 while (item != NULL)
1218 if (GTK_IS_MENU_ITEM(item->data))
1219 gtk_widget_destroy(GTK_WIDGET(item->data));
1220 item = g_list_next(item);
1223 g_list_free(children);
1225 /* create item for the menu bar menu */
1226 tmp = gtk_menu_item_new_with_label(filename);
1227 gtk_widget_show(tmp);
1228 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1229 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1231 /* clean the MRU list before adding an item (toolbar) */
1232 if (grf->toolbar != NULL)
1234 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1235 if (g_list_length(children) > file_prefs.mru_length - 1)
1237 item = g_list_nth(children, file_prefs.mru_length - 1);
1238 while (item != NULL)
1240 if (GTK_IS_MENU_ITEM(item->data))
1241 gtk_widget_destroy(GTK_WIDGET(item->data));
1242 item = g_list_next(item);
1245 g_list_free(children);
1247 /* create item for the tool bar menu */
1248 tmp = gtk_menu_item_new_with_label(filename);
1249 gtk_widget_show(tmp);
1250 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1251 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1252 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1257 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1259 guint i;
1261 foreach_document (i)
1263 GeanyDocument *doc = documents[i];
1265 switch (feature)
1267 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1268 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1269 break;
1270 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1271 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
1272 break;
1273 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1274 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1275 break;
1276 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1277 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1278 break;
1279 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1280 editor_set_indentation_guides(doc->editor);
1281 break;
1287 void ui_update_view_editor_menu_items(void)
1289 ignore_callback = TRUE;
1290 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1291 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1292 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);
1293 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);
1294 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);
1295 ignore_callback = FALSE;
1299 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1300 * @param label_text The label text.
1301 * @param alignment An address to store the alignment widget pointer.
1302 * @return The frame widget, setting the alignment container for packing child widgets. */
1303 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1305 GtkWidget *label, *align;
1306 GtkWidget *frame = gtk_frame_new(NULL);
1308 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1310 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1311 gtk_container_add(GTK_CONTAINER(frame), align);
1312 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1314 label = ui_label_new_bold(label_text);
1315 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1317 *alignment = align;
1318 return frame;
1322 /** Makes a fixed border for dialogs without increasing the button box border.
1323 * @param dialog The parent container for the @c GtkVBox.
1324 * @return The packed @c GtkVBox. */
1325 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1327 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1329 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1330 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox, TRUE, TRUE, 0);
1331 return vbox;
1335 /** Creates a @c GtkButton with custom text and a stock image similar to
1336 * @c gtk_button_new_from_stock().
1337 * @param stock_id A @c GTK_STOCK_NAME string.
1338 * @param text Button label text, can include mnemonics.
1339 * @return The new @c GtkButton.
1341 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1343 GtkWidget *image, *button;
1345 button = gtk_button_new_with_mnemonic(text);
1346 gtk_widget_show(button);
1347 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1348 gtk_button_set_image(GTK_BUTTON(button), image);
1349 /* note: image is shown by gtk */
1350 return button;
1354 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1355 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1356 * @param label Menu item label, can include mnemonics.
1357 * @return The new @c GtkImageMenuItem.
1359 * @since 0.16
1361 GtkWidget *
1362 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1364 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1365 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1367 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1368 gtk_widget_show(image);
1369 return item;
1373 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1374 GdkEvent *event, gpointer data)
1376 if (event->button.button == 1 && icon_pos == 1)
1378 gtk_entry_set_text(entry, "");
1379 gtk_widget_grab_focus(GTK_WIDGET(entry));
1384 /** Adds a small clear icon to the right end of the passed @a entry.
1385 * A callback to clear the contents of the GtkEntry is automatically added.
1387 * @param entry The GtkEntry object to which the icon should be attached.
1389 * @since 0.16
1391 void ui_entry_add_clear_icon(GtkEntry *entry)
1393 g_object_set(entry, "secondary-icon-stock", GTK_STOCK_CLEAR,
1394 "secondary-icon-activatable", TRUE, NULL);
1395 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1399 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1400 void ui_entry_add_activate_backward_signal(GtkEntry *entry)
1402 static gboolean installed = FALSE;
1404 g_return_if_fail(GTK_IS_ENTRY(entry));
1406 if (G_UNLIKELY(! installed))
1408 GtkBindingSet *binding_set;
1410 installed = TRUE;
1412 /* try to handle the unexpected case where GTK would already have installed the signal */
1413 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry)))
1415 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1416 return;
1419 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry),
1420 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 0, NULL, NULL,
1421 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1422 binding_set = gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry));
1423 gtk_binding_entry_add_signal(binding_set, GDK_Return, GDK_SHIFT_MASK, "activate-backward", 0);
1428 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1430 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1431 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1435 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1436 * the width of each button box's children.
1437 * Should be called after all child widgets have been packed. */
1438 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1440 GtkSizeGroup *size_group;
1442 gtk_box_set_spacing(GTK_BOX(copy), 10);
1443 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1445 /* now we need to put the widest widget from each button box in a size group,
1446 * but we don't know the width before they are drawn, and for different label
1447 * translations the widest widget can vary, so we just add all widgets. */
1448 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1449 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1450 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1451 g_object_unref(size_group);
1455 static gboolean tree_model_find_text(GtkTreeModel *model,
1456 GtkTreeIter *iter, gint column, const gchar *text)
1458 gchar *combo_text;
1459 gboolean found = FALSE;
1461 if (gtk_tree_model_get_iter_first(model, iter))
1465 gtk_tree_model_get(model, iter, 0, &combo_text, -1);
1466 found = utils_str_equal(combo_text, text);
1467 g_free(combo_text);
1469 if (found)
1470 return TRUE;
1472 while (gtk_tree_model_iter_next(model, iter));
1474 return FALSE;
1478 /** Prepends @a text to the drop down list, removing a duplicate element in
1479 * the list if found. Also ensures there are <= @a history_len elements.
1480 * @param combo_entry .
1481 * @param text Text to add, or @c NULL for current entry text.
1482 * @param history_len Max number of items, or @c 0 for default. */
1483 void ui_combo_box_add_to_history(GtkComboBoxText *combo_entry,
1484 const gchar *text, gint history_len)
1486 GtkComboBox *combo = GTK_COMBO_BOX(combo_entry);
1487 GtkTreeModel *model;
1488 GtkTreeIter iter;
1489 GtkTreePath *path;
1491 if (history_len <= 0)
1492 history_len = 10;
1493 if (!text)
1494 text = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
1496 model = gtk_combo_box_get_model(combo);
1498 if (tree_model_find_text(model, &iter, 0, text))
1500 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1502 gtk_combo_box_text_prepend_text(combo_entry, text);
1504 /* limit history */
1505 path = gtk_tree_path_new_from_indices(history_len, -1);
1506 if (gtk_tree_model_get_iter(model, &iter, path))
1508 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1510 gtk_tree_path_free(path);
1514 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1515 * exists in the combo's model. */
1516 void ui_combo_box_prepend_text_once(GtkComboBoxText *combo, const gchar *text)
1518 GtkTreeModel *model;
1519 GtkTreeIter iter;
1521 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
1522 if (tree_model_find_text(model, &iter, 0, text))
1523 return; /* don't prepend duplicate */
1525 gtk_combo_box_text_prepend_text(combo, text);
1529 /* Changes the color of the notebook tab text and open files items according to
1530 * document status. */
1531 void ui_update_tab_status(GeanyDocument *doc)
1533 gtk_widget_set_name(doc->priv->tab_label, document_get_status_widget_class(doc));
1535 sidebar_openfiles_update(doc);
1539 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1540 gboolean down)
1542 GtkTreePath *path;
1543 gboolean result;
1545 if (down)
1546 return gtk_tree_model_iter_next(model, iter);
1548 path = gtk_tree_model_get_path(model, iter);
1549 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1550 gtk_tree_path_free(path);
1551 return result;
1555 /* note: the while loop might be more efficient when searching upwards if it
1556 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1557 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1559 GtkTreeSelection *treesel;
1560 GtkTreeIter iter;
1561 GtkTreeModel *model;
1563 treesel = gtk_tree_view_get_selection(treeview);
1564 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1566 /* get the next selected item */
1567 if (! tree_model_iter_get_next(model, &iter, down))
1568 return FALSE; /* no more items */
1570 else /* no selection */
1572 if (! gtk_tree_model_get_iter_first(model, &iter))
1573 return TRUE; /* no items */
1575 while (TRUE)
1577 gtk_tree_selection_select_iter(treesel, &iter);
1578 if (cb(FALSE))
1579 break; /* found next message */
1581 if (! tree_model_iter_get_next(model, &iter, down))
1582 return FALSE; /* no more items */
1584 /* scroll item in view */
1585 if (ui_prefs.msgwindow_visible)
1587 GtkTreePath *path = gtk_tree_model_get_path(
1588 gtk_tree_view_get_model(treeview), &iter);
1590 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1591 gtk_tree_path_free(path);
1593 return TRUE;
1597 /* Returns FALSE if the treeview has items but no matching next item. */
1598 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1600 return tree_view_find(treeview, cb, TRUE);
1604 /* Returns FALSE if the treeview has items but no matching next item. */
1605 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1607 return tree_view_find(treeview, cb, FALSE);
1612 * Modifies the font of a widget using gtk_widget_modify_font().
1614 * @param widget The widget.
1615 * @param str The font name as expected by pango_font_description_from_string().
1617 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1619 PangoFontDescription *pfd;
1621 pfd = pango_font_description_from_string(str);
1622 gtk_widget_modify_font(widget, pfd);
1623 pango_font_description_free(pfd);
1627 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1628 * file chooser, replacing entry text (if successful) with the path returned from the
1629 * @c GtkFileChooser.
1630 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1631 * @param title The file chooser dialog title, or @c NULL.
1632 * @param action The mode of the file chooser.
1633 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1634 * such as @c GtkComboBoxEntry.
1635 * @return The @c GtkHBox.
1637 /* @see ui_setup_open_button_callback(). */
1638 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1640 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1642 hbox = gtk_hbox_new(FALSE, 6);
1643 path_entry = GTK_WIDGET(entry);
1645 /* prevent path_entry being vertically stretched to the height of dirbtn */
1646 vbox = gtk_vbox_new(FALSE, 0);
1647 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1649 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1651 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1653 else
1654 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1656 dirbtn = gtk_button_new();
1657 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1658 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1659 ui_setup_open_button_callback(dirbtn, title, action, entry);
1661 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1662 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1663 return hbox;
1667 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1670 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1671 * title can be NULL.
1672 * action is the file chooser mode to use. */
1673 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1674 GtkFileChooserAction action, GtkEntry *entry)
1676 GtkWidget *path_entry = GTK_WIDGET(entry);
1678 if (title)
1679 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1680 (GDestroyNotify) g_free);
1681 g_object_set_data(G_OBJECT(open_btn), "action", GINT_TO_POINTER(action));
1682 ui_hookup_widget(open_btn, path_entry, "entry");
1683 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), open_btn);
1687 #ifndef G_OS_WIN32
1688 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1689 const gchar *utf8_path)
1691 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1692 GTK_WINDOW(main_widgets.window), action,
1693 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1694 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1695 gchar *locale_path;
1696 gchar *ret_path = NULL;
1698 gtk_widget_set_name(dialog, "GeanyDialog");
1699 locale_path = utils_get_locale_from_utf8(utf8_path);
1700 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1702 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1703 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1705 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1707 if (g_path_is_absolute(locale_path))
1708 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1710 g_free(locale_path);
1712 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1714 gchar *dir_locale;
1716 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1717 ret_path = utils_get_utf8_from_locale(dir_locale);
1718 g_free(dir_locale);
1720 gtk_widget_destroy(dialog);
1721 return ret_path;
1723 #endif
1726 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1728 GtkWidget *path_box = GTK_WIDGET(user_data);
1729 GtkFileChooserAction action = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(path_box), "action"));
1730 GtkEntry *entry = g_object_get_data(G_OBJECT(path_box), "entry");
1731 const gchar *title = g_object_get_data(G_OBJECT(path_box), "title");
1732 gchar *utf8_path = NULL;
1734 /* TODO: extend for other actions */
1735 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1736 action == GTK_FILE_CHOOSER_ACTION_OPEN);
1738 if (title == NULL)
1739 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1740 _("Select Folder") : _("Select File");
1742 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1744 #ifdef G_OS_WIN32
1745 utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
1746 gtk_entry_get_text(GTK_ENTRY(entry)));
1747 #else
1748 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1749 #endif
1751 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1753 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
1754 #ifdef G_OS_WIN32
1755 utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
1756 gtk_entry_get_text(GTK_ENTRY(entry)));
1757 #else
1758 utf8_path = run_file_chooser(title, action, path);
1759 #endif
1760 g_free(path);
1763 if (utf8_path != NULL)
1765 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
1766 g_free(utf8_path);
1771 void ui_statusbar_showhide(gboolean state)
1773 /* handle statusbar visibility */
1774 if (state)
1776 gtk_widget_show(ui_widgets.statusbar);
1777 ui_update_statusbar(NULL, -1);
1779 else
1780 gtk_widget_hide(ui_widgets.statusbar);
1784 /** Packs all @c GtkWidgets passed after the row argument into a table, using
1785 * one widget per cell. The first widget is not expanded as the table grows,
1786 * as this is usually a label.
1787 * @param table
1788 * @param row The row number of the table.
1790 void ui_table_add_row(GtkTable *table, gint row, ...)
1792 va_list args;
1793 guint i;
1794 GtkWidget *widget;
1796 va_start(args, row);
1797 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
1799 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
1801 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
1802 options, 0, 0, 0);
1804 va_end(args);
1808 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
1810 const gchar *file_name = user_data;
1811 GeanyFiletype *ft = NULL;
1813 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
1814 ft = filetypes[GEANY_FILETYPES_CONF];
1816 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
1817 document_open_file(file_name, FALSE, ft, NULL);
1818 else
1820 gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
1821 gchar *base_name = g_path_get_basename(file_name);
1822 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
1823 gchar *global_content = NULL;
1825 /* if the requested file doesn't exist in the user's config dir, try loading the file
1826 * from the global data directory and use its contents for the newly created file */
1827 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
1828 g_file_get_contents(global_file, &global_content, NULL, NULL);
1830 document_new_file(utf8_filename, ft, global_content);
1832 utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
1837 static void free_on_closure_notify(gpointer data, GClosure *closure)
1839 g_free(data);
1843 /* @note You should connect to the "document-save" signal yourself to detect
1844 * if the user has just saved the config file, reloading it. */
1845 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
1847 GtkWidget *item;
1849 if (!parent)
1850 parent = GTK_CONTAINER(widgets.config_files_menu);
1852 if (!label)
1854 gchar *base_name;
1856 base_name = g_path_get_basename(real_path);
1857 item = gtk_menu_item_new_with_label(base_name);
1858 g_free(base_name);
1860 else
1861 item = gtk_menu_item_new_with_mnemonic(label);
1863 gtk_widget_show(item);
1864 gtk_container_add(parent, item);
1865 g_signal_connect_data(item, "activate", G_CALLBACK(on_config_file_clicked),
1866 g_strdup(real_path), free_on_closure_notify, 0);
1870 static gboolean sort_menu(gpointer data)
1872 ui_menu_sort_by_label(GTK_MENU(data));
1873 return FALSE;
1877 static void create_config_files_menu(void)
1879 GtkWidget *menu, *item;
1881 widgets.config_files_menu = menu = gtk_menu_new();
1883 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
1884 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
1886 /* sort menu after all items added */
1887 g_idle_add(sort_menu, widgets.config_files_menu);
1891 void ui_init_stock_items(void)
1893 GtkStockItem items[] =
1895 { GEANY_STOCK_SAVE_ALL, N_("Save All"), 0, 0, GETTEXT_PACKAGE },
1896 { GEANY_STOCK_CLOSE_ALL, N_("Close All"), 0, 0, GETTEXT_PACKAGE },
1897 { GEANY_STOCK_BUILD, N_("Build"), 0, 0, GETTEXT_PACKAGE }
1900 gtk_stock_add((GtkStockItem *) items, G_N_ELEMENTS(items));
1904 void ui_init_toolbar_widgets(void)
1906 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
1907 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
1908 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
1909 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
1913 void ui_swap_sidebar_pos(void)
1915 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
1916 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
1917 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
1919 g_object_ref(left);
1920 g_object_ref(right);
1921 gtk_container_remove (GTK_CONTAINER (pane), left);
1922 gtk_container_remove (GTK_CONTAINER (pane), right);
1923 /* only scintilla notebook should expand */
1924 gtk_paned_pack1(GTK_PANED(pane), right, right == main_widgets.notebook, TRUE);
1925 gtk_paned_pack2(GTK_PANED(pane), left, left == main_widgets.notebook, TRUE);
1926 g_object_unref(left);
1927 g_object_unref(right);
1929 gtk_paned_set_position(GTK_PANED(pane), gtk_widget_get_allocated_width(pane)
1930 - gtk_paned_get_position(GTK_PANED(pane)));
1934 static void init_recent_files(void)
1936 GtkWidget *toolbar_recent_files_menu;
1938 /* add recent files to the File menu */
1939 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
1940 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
1941 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
1942 ui_widgets.recent_files_menu_menubar);
1944 /* add recent files to the toolbar Open button */
1945 toolbar_recent_files_menu = gtk_menu_new();
1946 g_object_ref(toolbar_recent_files_menu);
1947 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
1948 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
1952 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
1954 g_object_ref(menu);
1955 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
1956 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
1957 g_object_unref(menu);
1961 typedef struct GeanySharedMenu
1963 const gchar *menu;
1964 const gchar *menubar_item;
1965 const gchar *popup_item;
1967 GeanySharedMenu;
1969 #define foreach_menu(item, array) \
1970 for (item = array; item->menu; item++)
1972 static void on_editor_menu_show(GtkWidget *widget, GeanySharedMenu *items)
1974 GeanySharedMenu *item;
1976 foreach_menu(item, items)
1978 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
1979 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
1980 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
1982 ui_menu_move(menu, bar, popup);
1987 static void on_editor_menu_hide(GtkWidget *widget, GeanySharedMenu *items)
1989 GeanySharedMenu *item;
1991 foreach_menu(item, items)
1993 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
1994 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
1995 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
1997 ui_menu_move(menu, popup, bar);
2002 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2003 * so this is called after that's done. */
2004 void ui_init_prefs(void)
2006 StashGroup *group = stash_group_new(PACKAGE);
2008 /* various prefs */
2009 configuration_add_various_pref_group(group);
2011 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
2012 "show_symbol_list_expanders", TRUE);
2013 stash_group_add_boolean(group, &interface_prefs.compiler_tab_autoscroll,
2014 "compiler_tab_autoscroll", TRUE);
2015 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
2016 "allow_always_save", FALSE);
2017 stash_group_add_string(group, &ui_prefs.statusbar_template,
2018 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE));
2019 stash_group_add_boolean(group, &ui_prefs.new_document_after_close,
2020 "new_document_after_close", FALSE);
2021 stash_group_add_boolean(group, &interface_prefs.msgwin_status_visible,
2022 "msgwin_status_visible", TRUE);
2023 stash_group_add_boolean(group, &interface_prefs.msgwin_compiler_visible,
2024 "msgwin_compiler_visible", TRUE);
2025 stash_group_add_boolean(group, &interface_prefs.msgwin_messages_visible,
2026 "msgwin_messages_visible", TRUE);
2027 stash_group_add_boolean(group, &interface_prefs.msgwin_scribble_visible,
2028 "msgwin_scribble_visible", TRUE);
2032 /* Used to find out the name of the GtkBuilder retrieved object since
2033 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2034 * 'name' property for that and those that don't implement GtkBuildable
2035 * will have a "gtk-builder-name" stored in the GObject's data list. */
2036 static const gchar *ui_guess_object_name(GObject *obj)
2038 const gchar *name = NULL;
2040 g_return_val_if_fail(G_IS_OBJECT(obj), NULL);
2042 if (GTK_IS_BUILDABLE(obj))
2043 name = gtk_buildable_get_name(GTK_BUILDABLE(obj));
2044 if (! name)
2045 name = g_object_get_data(obj, "gtk-builder-name");
2046 if (! name)
2047 return NULL;
2049 return name;
2053 /* Compatibility functions */
2054 GtkWidget *create_edit_menu1(void)
2056 return edit_menu1;
2060 GtkWidget *create_prefs_dialog(void)
2062 return prefs_dialog;
2066 GtkWidget *create_project_dialog(void)
2068 return project_dialog;
2072 GtkWidget *create_toolbar_popup_menu1(void)
2074 return toolbar_popup_menu1;
2078 GtkWidget *create_window1(void)
2080 return window1;
2084 static GtkWidget *ui_get_top_parent(GtkWidget *widget)
2086 GtkWidget *parent;
2088 g_return_val_if_fail(GTK_IS_WIDGET(widget), NULL);
2090 for (;;)
2092 if (GTK_IS_MENU(widget))
2093 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2094 else
2095 parent = gtk_widget_get_parent(widget);
2096 if (parent == NULL)
2097 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2098 if (parent == NULL)
2099 break;
2100 widget = parent;
2103 return widget;
2107 void ui_init_builder(void)
2109 gchar *interface_file;
2110 const gchar *name;
2111 GError *error;
2112 GSList *iter, *all_objects;
2113 GtkWidget *widget, *toplevel;
2115 /* prevent function from being called twice */
2116 if (GTK_IS_BUILDER(builder))
2117 return;
2119 builder = gtk_builder_new();
2121 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
2123 error = NULL;
2124 interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
2125 if (! gtk_builder_add_from_file(builder, interface_file, &error))
2127 /* Show the user this message so they know WTF happened */
2128 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
2129 _("Geany cannot start!"), error->message);
2130 /* Aborts */
2131 g_error("Cannot create user-interface: %s", error->message);
2132 g_error_free(error);
2133 g_free(interface_file);
2134 g_object_unref(builder);
2135 return;
2137 g_free(interface_file);
2139 gtk_builder_connect_signals(builder, NULL);
2141 edit_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "edit_menu1"));
2142 prefs_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "prefs_dialog"));
2143 project_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "project_dialog"));
2144 toolbar_popup_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar_popup_menu1"));
2145 window1 = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
2147 g_object_set_data(G_OBJECT(edit_menu1), "edit_menu1", edit_menu1);
2148 g_object_set_data(G_OBJECT(prefs_dialog), "prefs_dialog", prefs_dialog);
2149 g_object_set_data(G_OBJECT(project_dialog), "project_dialog", project_dialog);
2150 g_object_set_data(G_OBJECT(toolbar_popup_menu1), "toolbar_popup_menu1", toolbar_popup_menu1);
2151 g_object_set_data(G_OBJECT(window1), "window1", window1);
2153 all_objects = gtk_builder_get_objects(builder);
2154 for (iter = all_objects; iter != NULL; iter = g_slist_next(iter))
2156 if (! GTK_IS_WIDGET(iter->data))
2157 continue;
2159 widget = GTK_WIDGET(iter->data);
2161 name = ui_guess_object_name(G_OBJECT(widget));
2162 if (! name)
2164 g_warning("Unable to get name from GtkBuilder object");
2165 continue;
2168 toplevel = ui_get_top_parent(widget);
2169 if (toplevel)
2170 ui_hookup_widget(toplevel, widget, name);
2172 g_slist_free(all_objects);
2176 static void init_custom_style(void)
2178 #if GTK_CHECK_VERSION(3, 0, 0)
2179 gchar *css_file = g_build_filename(app->datadir, "geany.css", NULL);
2180 GtkCssProvider *css = gtk_css_provider_new();
2181 GError *error = NULL;
2183 if (! gtk_css_provider_load_from_path(css, css_file, &error))
2185 g_warning("Failed to load custom CSS: %s", error->message);
2186 g_error_free(error);
2188 else
2190 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2191 GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2194 g_object_unref(css);
2195 g_free(css_file);
2196 #else
2197 /* see setup_gtk2_styles() in main.c */
2198 #endif
2202 void ui_init(void)
2204 init_custom_style();
2206 init_recent_files();
2208 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
2209 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
2211 main_widgets.progressbar = progress_bar_create();
2213 /* current word sensitive items */
2214 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition2");
2215 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "context_action1");
2216 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage2");
2217 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage2");
2219 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
2220 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
2221 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
2222 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
2223 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
2224 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
2225 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
2226 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
2227 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
2228 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
2229 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
2230 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
2231 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
2232 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
2234 /* reparent context submenus as needed */
2236 GeanySharedMenu arr[] = {
2237 {"commands2_menu", "commands2", "commands1"},
2238 {"menu_format1_menu", "menu_format1", "menu_format2"},
2239 {"more1_menu", "more1", "search2"},
2240 {NULL, NULL, NULL}
2242 static GeanySharedMenu items[G_N_ELEMENTS(arr)];
2244 memcpy(items, arr, sizeof(arr));
2245 g_signal_connect(main_widgets.editor_menu, "show", G_CALLBACK(on_editor_menu_show), items);
2246 g_signal_connect(main_widgets.editor_menu, "hide", G_CALLBACK(on_editor_menu_hide), items);
2249 ui_init_toolbar_widgets();
2250 init_document_widgets();
2251 create_config_files_menu();
2255 void ui_finalize_builder(void)
2257 if (GTK_IS_BUILDER(builder))
2258 g_object_unref(builder);
2260 /* cleanup refs lingering even after GtkBuilder is destroyed */
2261 if (GTK_IS_WIDGET(edit_menu1))
2262 gtk_widget_destroy(edit_menu1);
2263 if (GTK_IS_WIDGET(prefs_dialog))
2264 gtk_widget_destroy(prefs_dialog);
2265 if (GTK_IS_WIDGET(project_dialog))
2266 gtk_widget_destroy(project_dialog);
2267 if (GTK_IS_WIDGET(toolbar_popup_menu1))
2268 gtk_widget_destroy(toolbar_popup_menu1);
2269 if (GTK_IS_WIDGET(window1))
2270 gtk_widget_destroy(window1);
2274 static void auto_separator_update(GeanyAutoSeparator *autosep)
2276 g_return_if_fail(autosep->item_count >= 0);
2278 if (autosep->widget)
2280 if (autosep->item_count > 0)
2281 ui_widget_show_hide(autosep->widget, autosep->show_count > 0);
2282 else
2283 gtk_widget_destroy(autosep->widget);
2288 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
2290 GeanyAutoSeparator *autosep = user_data;
2292 if (gtk_widget_get_visible(widget))
2293 autosep->show_count++;
2294 else
2295 autosep->show_count--;
2296 auto_separator_update(autosep);
2300 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
2302 GeanyAutoSeparator *autosep = user_data;
2304 autosep->item_count--;
2305 autosep->item_count = MAX(autosep->item_count, 0);
2306 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2307 * so assume widget was visible */
2308 autosep->show_count--;
2309 autosep->show_count = MAX(autosep->item_count, 0);
2310 auto_separator_update(autosep);
2314 /* Show the separator widget if @a item or another is visible. */
2315 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2316 * property, and using reference counting to keep the widget alive whilst its visible group
2317 * is alive. */
2318 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2320 /* set widget ptr NULL when widget destroyed */
2321 if (autosep->item_count == 0)
2322 g_signal_connect(autosep->widget, "destroy",
2323 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2325 if (gtk_widget_get_visible(item))
2326 autosep->show_count++;
2328 autosep->item_count++;
2329 auto_separator_update(autosep);
2331 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2332 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2333 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2338 * Sets @a text as the contents of the tooltip for @a widget.
2340 * @param widget The widget the tooltip should be set for.
2341 * @param text The text for the tooltip.
2343 * @since 0.16
2344 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2346 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2348 gtk_widget_set_tooltip_text(widget, text);
2352 /** Returns a widget from a name in a component, usually created by Glade.
2353 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2354 * or alternatively any widget in the component, and the name of the widget
2355 * you want returned.
2356 * @param widget Widget with the @a widget_name property set.
2357 * @param widget_name Name to lookup.
2358 * @return The widget found.
2359 * @see ui_hookup_widget().
2361 * @since 0.16
2363 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2365 GtkWidget *parent, *found_widget;
2367 g_return_val_if_fail(widget != NULL, NULL);
2368 g_return_val_if_fail(widget_name != NULL, NULL);
2370 for (;;)
2372 if (GTK_IS_MENU(widget))
2373 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2374 else
2375 parent = gtk_widget_get_parent(widget);
2376 if (parent == NULL)
2377 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2378 if (parent == NULL)
2379 break;
2380 widget = parent;
2383 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2384 if (G_UNLIKELY(found_widget == NULL))
2385 g_warning("Widget not found: %s", widget_name);
2386 return found_widget;
2390 /* wraps gtk_builder_get_object()
2391 * unlike ui_lookup_widget(), it does only support getting object created from the main
2392 * UI file, but it can fetch any object, not only widgets */
2393 gpointer ui_builder_get_object (const gchar *name)
2395 return gtk_builder_get_object (builder, name);
2399 /* Progress Bar */
2400 static guint progress_bar_timer_id = 0;
2403 static GtkWidget *progress_bar_create(void)
2405 GtkWidget *bar = gtk_progress_bar_new();
2407 /* Set the progressbar's height to 1 to fit it in the statusbar */
2408 gtk_widget_set_size_request(bar, -1, 1);
2409 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2411 return bar;
2415 static gboolean progress_bar_pulse(gpointer data)
2417 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2419 return TRUE;
2424 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2425 * (if the statusbar is visible). This is a convenience function which adds a timer to
2426 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2427 * You can use this function when you have time consuming asynchronous operation and want to
2428 * display some activity in the GUI and when you don't know about detailed progress steps.
2429 * The progressbar widget is hidden by default when it is not active. This function and
2430 * ui_progress_bar_stop() will show and hide it automatically for you.
2432 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2433 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2434 * In this case, you need to show and hide the widget yourself. You can find some example code
2435 * in @c src/printing.c.
2437 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2439 * @since 0.16
2441 void ui_progress_bar_start(const gchar *text)
2443 g_return_if_fail(progress_bar_timer_id == 0);
2445 if (! interface_prefs.statusbar_visible)
2446 return;
2448 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2450 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2452 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2456 /** Stops a running progress bar and hides the widget again.
2458 * @since 0.16
2460 void ui_progress_bar_stop(void)
2462 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2464 if (progress_bar_timer_id != 0)
2466 g_source_remove(progress_bar_timer_id);
2467 progress_bar_timer_id = 0;
2472 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2474 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2475 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2476 gchar *sa, *sb;
2477 gint result;
2479 sa = ui_menu_item_get_text(item_a);
2480 sb = ui_menu_item_get_text(item_b);
2481 result = utils_str_casecmp(sa, sb);
2482 g_free(sa);
2483 g_free(sb);
2484 return result;
2488 /* Currently @a menu should contain only GtkMenuItems with labels. */
2489 void ui_menu_sort_by_label(GtkMenu *menu)
2491 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2492 GList *node;
2493 gint pos;
2495 list = g_list_sort(list, compare_menu_item_labels);
2496 pos = 0;
2497 foreach_list(node, list)
2499 gtk_menu_reorder_child(menu, node->data, pos);
2500 pos++;
2502 g_list_free(list);
2506 void ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2508 va_list a;
2509 gchar *text;
2511 va_start(a, format);
2512 text = g_strdup_vprintf(format, a);
2513 va_end(a);
2515 gtk_label_set_text(label, text);
2516 gtk_label_set_use_markup(label, TRUE);
2517 g_free(text);
2521 GtkWidget *ui_label_new_bold(const gchar *text)
2523 GtkWidget *label;
2524 gchar *label_text;
2526 label_text = g_markup_escape_text(text, -1);
2527 label = gtk_label_new(NULL);
2528 ui_label_set_markup(GTK_LABEL(label), "<b>%s</b>", label_text);
2529 g_free(label_text);
2530 return label;
2534 /** Adds a list of document items to @a menu.
2535 * @param menu Menu.
2536 * @param active Which document to highlight, or @c NULL.
2537 * @param callback is used for each menu item's @c "activate" signal and will be passed
2538 * the corresponding document pointer as @c user_data.
2539 * @warning You should check @c doc->is_valid in the callback.
2540 * @since 0.19 */
2541 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2543 ui_menu_add_document_items_sorted(menu, active, callback, NULL);
2547 /** Adds a list of document items to @a menu.
2549 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2550 * the order of the document tabs is used.
2552 * See document_compare_by_display_name() for an example sort function.
2554 * @param menu Menu.
2555 * @param active Which document to highlight, or @c NULL.
2556 * @param callback is used for each menu item's @c "activate" signal and will be passed
2557 * the corresponding document pointer as @c user_data.
2558 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2559 * @warning You should check @c doc->is_valid in the callback.
2560 * @since 0.21 */
2561 void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
2562 GCallback callback, GCompareFunc compare_func)
2564 GtkWidget *menu_item, *menu_item_label, *image;
2565 GeanyDocument *doc;
2566 guint i, len;
2567 gchar *base_name, *label;
2568 GPtrArray *sorted_documents;
2570 len = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2572 sorted_documents = g_ptr_array_sized_new(len);
2573 /* copy the documents_array into the new one */
2574 foreach_document(i)
2576 g_ptr_array_add(sorted_documents, documents[i]);
2578 if (compare_func == NULL)
2579 compare_func = document_compare_by_tab_order;
2581 /* and now sort it */
2582 g_ptr_array_sort(sorted_documents, compare_func);
2584 for (i = 0; i < sorted_documents->len; i++)
2586 doc = g_ptr_array_index(sorted_documents, i);
2588 base_name = g_path_get_basename(DOC_FILENAME(doc));
2589 menu_item = gtk_image_menu_item_new_with_label(base_name);
2590 image = gtk_image_new_from_pixbuf(doc->file_type->icon);
2591 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
2593 gtk_widget_show(menu_item);
2594 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2595 g_signal_connect(menu_item, "activate", callback, doc);
2597 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2598 gtk_widget_set_name(menu_item_label, document_get_status_widget_class(doc));
2600 if (doc == active)
2602 label = g_markup_escape_text(base_name, -1);
2603 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", label);
2604 g_free(label);
2607 g_free(base_name);
2609 g_ptr_array_free(sorted_documents, TRUE);
2613 /** Checks whether the passed @a keyval is the Enter or Return key.
2614 * There are three different Enter/Return key values
2615 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2616 * This is just a convenience function.
2617 * @param keyval A keyval.
2618 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2619 * @since 0.19 */
2620 gboolean ui_is_keyval_enter_or_return(guint keyval)
2622 return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
2626 /** Reads an integer from the GTK default settings registry
2627 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2628 * @param property_name The property to read.
2629 * @param default_value The default value in case the value could not be read.
2630 * @return The value for the property if it exists, otherwise the @a default_value.
2631 * @since 0.19 */
2632 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
2634 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2635 gtk_settings_get_default())), property_name))
2637 gint value;
2638 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
2639 return value;
2641 else
2642 return default_value;
2646 void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
2647 gint new_text_len, gint *position, gpointer data)
2649 gboolean first = position != NULL && *position == 0;
2650 gint i;
2652 if (new_text_len == -1)
2653 new_text_len = (gint) strlen(new_text);
2655 for (i = 0; i < new_text_len; i++, new_text++)
2657 if ((!first || !strchr("+-", *new_text)) && !isdigit(*new_text))
2659 g_signal_stop_emission_by_name(editable, "insert-text");
2660 break;
2662 first = FALSE;
2667 /* gets the icon that applies to a particular MIME type */
2668 GdkPixbuf *ui_get_mime_icon(const gchar *mime_type, GtkIconSize size)
2670 GdkPixbuf *icon = NULL;
2671 gchar *ctype;
2672 GIcon *gicon;
2673 GtkIconInfo *info;
2674 GtkIconTheme *theme;
2675 gint real_size;
2677 if (!gtk_icon_size_lookup(size, &real_size, NULL))
2679 g_return_val_if_reached(NULL);
2681 ctype = g_content_type_from_mime_type(mime_type);
2682 if (ctype != NULL)
2684 gicon = g_content_type_get_icon(ctype);
2685 theme = gtk_icon_theme_get_default();
2686 info = gtk_icon_theme_lookup_by_gicon(theme, gicon, real_size, 0);
2687 g_object_unref(gicon);
2688 g_free(ctype);
2690 if (info != NULL)
2692 icon = gtk_icon_info_load_icon(info, NULL);
2693 gtk_icon_info_free(info);
2697 /* fallback for builds with GIO < 2.18 or if icon lookup failed, like it might happen on Windows */
2698 if (icon == NULL)
2700 const gchar *stock_id = GTK_STOCK_FILE;
2701 GtkIconSet *icon_set;
2703 if (strstr(mime_type, "directory"))
2704 stock_id = GTK_STOCK_DIRECTORY;
2706 icon_set = gtk_icon_factory_lookup_default(stock_id);
2707 if (icon_set)
2709 #if GTK_CHECK_VERSION(3, 0, 0)
2710 GtkStyleContext *ctx = gtk_style_context_new();
2711 icon = gtk_icon_set_render_icon_pixbuf(icon_set, ctx, size);
2712 g_object_unref(ctx);
2713 #else
2714 icon = gtk_icon_set_render_icon(icon_set, gtk_widget_get_default_style(),
2715 gtk_widget_get_default_direction(),
2716 GTK_STATE_NORMAL, size, NULL, NULL);
2717 #endif
2720 return icon;
2724 void ui_focus_current_document(void)
2726 GeanyDocument *doc = document_get_current();
2728 if (doc != NULL)
2729 document_grab_focus(doc);
2733 /** Finds the label text associated with stock_id
2734 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
2735 * @return The label text for stock
2736 * @since Geany 1.22 */
2737 const gchar *ui_lookup_stock_label(const gchar *stock_id)
2739 GtkStockItem item;
2741 if (gtk_stock_lookup(stock_id, &item))
2742 return item.label;
2744 g_warning("No stock id '%s'!", stock_id);
2745 return NULL;