Make sure not to leak tmp_errfile if only outf creation fails
[geany-mirror.git] / src / ui_utils.c
blob22ab68a1ecdefdc1749ccb34c4585732cedddd77
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 *tmp_menu;
612 GtkWidget *tmp_popup;
613 GtkWidget *edit_menu, *edit_menu_item;
614 GtkWidget *popup_menu, *popup_menu_item;
616 edit_menu = gtk_menu_new();
617 popup_menu = gtk_menu_new();
618 edit_menu_item = gtk_menu_item_new_with_label(label);
619 popup_menu_item = gtk_menu_item_new_with_label(label);
620 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
621 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
623 while (includes[i] != NULL)
625 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
626 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
627 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
628 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
629 g_signal_connect(tmp_menu, "activate",
630 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
631 g_signal_connect(tmp_popup, "activate",
632 G_CALLBACK(on_popup_insert_include_activate), (gpointer) includes[i]);
633 i++;
635 gtk_widget_show_all(edit_menu_item);
636 gtk_widget_show_all(popup_menu_item);
637 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
638 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
642 void ui_create_insert_menu_items(void)
644 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
645 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
646 GtkWidget *blank;
647 const gchar *c_includes_stdlib[] = {
648 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
649 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
651 const gchar *c_includes_c99[] = {
652 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
653 "tgmath.h", "wchar.h", "wctype.h", NULL
655 const gchar *c_includes_cpp[] = {
656 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
658 const gchar *c_includes_cppstdlib[] = {
659 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
660 "memory", "locale", NULL
662 const gchar *c_includes_stl[] = {
663 "bitset", "deque", "list", "map", "set", "queue", "stack", "vector", "algorithm",
664 "iterator", "functional", "string", "complex", "valarray", NULL
667 blank = gtk_menu_item_new_with_label("#include \"...\"");
668 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
669 gtk_widget_show(blank);
670 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate), NULL);
671 blank = gtk_separator_menu_item_new ();
672 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
673 gtk_widget_show(blank);
675 blank = gtk_menu_item_new_with_label("#include \"...\"");
676 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
677 gtk_widget_show(blank);
678 g_signal_connect(blank, "activate", G_CALLBACK(on_popup_insert_include_activate), NULL);
679 blank = gtk_separator_menu_item_new();
680 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
681 gtk_widget_show(blank);
683 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
684 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
685 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
686 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
687 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
691 static void insert_date(GeanyDocument *doc, gint pos, const gchar *date_style)
693 const gchar *format = NULL;
694 gchar *time_str;
696 g_return_if_fail(doc != NULL);
697 g_return_if_fail(pos == -1 || pos >= 0);
699 if (pos == -1)
700 pos = sci_get_current_position(doc->editor->sci);
702 /* set default value */
703 if (utils_str_equal("", ui_prefs.custom_date_format))
705 g_free(ui_prefs.custom_date_format);
706 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
709 if (utils_str_equal(_("dd.mm.yyyy"), date_style))
710 format = "%d.%m.%Y";
711 else if (utils_str_equal(_("mm.dd.yyyy"), date_style))
712 format = "%m.%d.%Y";
713 else if (utils_str_equal(_("yyyy/mm/dd"), date_style))
714 format = "%Y/%m/%d";
715 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), date_style))
716 format = "%d.%m.%Y %H:%M:%S";
717 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), date_style))
718 format = "%m.%d.%Y %H:%M:%S";
719 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), date_style))
720 format = "%Y/%m/%d %H:%M:%S";
721 else if (utils_str_equal(_("_Use Custom Date Format"), date_style))
722 format = ui_prefs.custom_date_format;
723 else
725 gchar *str = dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets.window),
726 _("Enter here a custom date and time format. "
727 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
728 ui_prefs.custom_date_format);
729 if (str)
730 SETPTR(ui_prefs.custom_date_format, str);
731 return;
734 time_str = utils_get_date_time(format, NULL);
735 if (time_str != NULL)
737 sci_start_undo_action(doc->editor->sci);
738 sci_insert_text(doc->editor->sci, pos, time_str);
739 sci_goto_pos(doc->editor->sci, pos + strlen(time_str), FALSE);
740 sci_end_undo_action(doc->editor->sci);
741 g_free(time_str);
743 else
745 utils_beep();
746 ui_set_statusbar(TRUE,
747 _("Date format string could not be converted (possibly too long)."));
752 static void on_popup_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
754 insert_date(document_get_current(), editor_info.click_pos, user_data);
758 static void on_menu_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
760 insert_date(document_get_current(), -1, user_data);
764 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
766 GtkWidget *item;
768 item = gtk_menu_item_new_with_mnemonic(label);
769 gtk_container_add(GTK_CONTAINER(me), item);
770 gtk_widget_show(item);
771 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
773 item = gtk_menu_item_new_with_mnemonic(label);
774 gtk_container_add(GTK_CONTAINER(mp), item);
775 gtk_widget_show(item);
776 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), label);
780 void ui_create_insert_date_menu_items(void)
782 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
783 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
784 GtkWidget *item;
785 gchar *str;
787 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
788 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
789 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
791 item = gtk_separator_menu_item_new();
792 gtk_container_add(GTK_CONTAINER(menu_edit), item);
793 gtk_widget_show(item);
794 item = gtk_separator_menu_item_new();
795 gtk_container_add(GTK_CONTAINER(menu_popup), item);
796 gtk_widget_show(item);
798 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
799 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
800 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
802 item = gtk_separator_menu_item_new();
803 gtk_container_add(GTK_CONTAINER(menu_edit), item);
804 gtk_widget_show(item);
805 item = gtk_separator_menu_item_new();
806 gtk_container_add(GTK_CONTAINER(menu_popup), item);
807 gtk_widget_show(item);
809 str = _("_Use Custom Date Format");
810 item = gtk_menu_item_new_with_mnemonic(str);
811 gtk_container_add(GTK_CONTAINER(menu_edit), item);
812 gtk_widget_show(item);
813 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
814 ui_hookup_widget(main_widgets.window, item, "insert_date_custom1");
816 item = gtk_menu_item_new_with_mnemonic(str);
817 gtk_container_add(GTK_CONTAINER(menu_popup), item);
818 gtk_widget_show(item);
819 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), str);
820 ui_hookup_widget(main_widgets.editor_menu, item, "insert_date_custom2");
822 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
826 void ui_save_buttons_toggle(gboolean enable)
828 guint i;
829 gboolean dirty_tabs = FALSE;
831 if (ui_prefs.allow_always_save)
832 enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
834 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
835 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
837 /* save all menu item and tool button */
838 for (i = 0; i < documents_array->len; i++)
840 /* check whether there are files where changes were made and if there are some,
841 * we need the save all button / item */
842 if (documents[i]->is_valid && documents[i]->changed)
844 dirty_tabs = TRUE;
845 break;
849 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
850 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
854 #define add_doc_widget(widget_name) \
855 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
857 #define add_doc_toolitem(widget_name) \
858 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
860 static void init_document_widgets(void)
862 widgets.document_buttons = g_ptr_array_new();
864 /* Cache the document-sensitive widgets so we don't have to keep looking them up
865 * when using ui_document_buttons_update(). */
866 add_doc_widget("menu_close1");
867 add_doc_widget("close_other_documents1");
868 add_doc_widget("menu_change_font1");
869 add_doc_widget("menu_close_all1");
870 add_doc_widget("menu_save1");
871 add_doc_widget("menu_save_all1");
872 add_doc_widget("menu_save_as1");
873 add_doc_widget("menu_count_words1");
874 add_doc_widget("menu_build1");
875 add_doc_widget("add_comments1");
876 add_doc_widget("menu_paste1");
877 add_doc_widget("menu_undo2");
878 add_doc_widget("properties1");
879 add_doc_widget("menu_reload1");
880 add_doc_widget("menu_document1");
881 add_doc_widget("menu_choose_color1");
882 add_doc_widget("menu_color_schemes");
883 add_doc_widget("menu_markers_margin1");
884 add_doc_widget("menu_linenumber_margin1");
885 add_doc_widget("menu_show_white_space1");
886 add_doc_widget("menu_show_line_endings1");
887 add_doc_widget("menu_show_indentation_guides1");
888 add_doc_widget("menu_zoom_in1");
889 add_doc_widget("menu_zoom_out1");
890 add_doc_widget("normal_size1");
891 add_doc_widget("treeview6");
892 add_doc_widget("print1");
893 add_doc_widget("menu_reload_as1");
894 add_doc_widget("menu_select_all1");
895 add_doc_widget("insert_date1");
896 add_doc_widget("insert_alternative_white_space1");
897 add_doc_widget("menu_format1");
898 add_doc_widget("commands2");
899 add_doc_widget("menu_open_selected_file1");
900 add_doc_widget("page_setup1");
901 add_doc_widget("find1");
902 add_doc_widget("find_next1");
903 add_doc_widget("find_previous1");
904 add_doc_widget("go_to_next_marker1");
905 add_doc_widget("go_to_previous_marker1");
906 add_doc_widget("replace1");
907 add_doc_widget("find_nextsel1");
908 add_doc_widget("find_prevsel1");
909 add_doc_widget("find_usage1");
910 add_doc_widget("find_document_usage1");
911 add_doc_widget("mark_all1");
912 add_doc_widget("go_to_line1");
913 add_doc_widget("goto_tag_definition1");
914 add_doc_widget("goto_tag_declaration1");
915 add_doc_widget("reset_indentation1");
916 add_doc_toolitem("Close");
917 add_doc_toolitem("CloseAll");
918 add_doc_toolitem("Search");
919 add_doc_toolitem("SearchEntry");
920 add_doc_toolitem("ZoomIn");
921 add_doc_toolitem("ZoomOut");
922 add_doc_toolitem("Indent");
923 add_doc_toolitem("UnIndent");
924 add_doc_toolitem("Cut");
925 add_doc_toolitem("Copy");
926 add_doc_toolitem("Paste");
927 add_doc_toolitem("Delete");
928 add_doc_toolitem("Save");
929 add_doc_toolitem("SaveAs");
930 add_doc_toolitem("SaveAll");
931 add_doc_toolitem("Compile");
932 add_doc_toolitem("Run");
933 add_doc_toolitem("Reload");
934 add_doc_toolitem("Color");
935 add_doc_toolitem("Goto");
936 add_doc_toolitem("GotoEntry");
937 add_doc_toolitem("Replace");
938 add_doc_toolitem("Print");
942 void ui_document_buttons_update(void)
944 guint i;
945 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
947 for (i = 0; i < widgets.document_buttons->len; i++)
949 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
950 if (GTK_IS_ACTION(widget))
951 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
952 else
953 ui_widget_set_sensitive(widget, enable);
958 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
960 g_ptr_array_remove_fast(widgets.document_buttons, widget);
964 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
965 * when some documents are present/no documents are open.
966 * It will be removed when the widget is destroyed.
967 * @param widget The widget to add.
969 * @since 0.15
971 GEANY_API_SYMBOL
972 void ui_add_document_sensitive(GtkWidget *widget)
974 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
976 ui_widget_set_sensitive(widget, enable);
978 g_ptr_array_add(widgets.document_buttons, widget);
979 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
983 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
985 if (show)
987 gtk_widget_show(widget);
989 else
991 gtk_widget_hide(widget);
996 void ui_sidebar_show_hide(void)
998 GtkWidget *widget;
1000 /* check that there are no other notebook pages before hiding the sidebar completely
1001 * other pages could be e.g. the file browser plugin */
1002 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1003 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1005 ui_prefs.sidebar_visible = FALSE;
1008 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
1009 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
1011 ignore_callback = TRUE;
1012 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
1013 ignore_callback = FALSE;
1016 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
1018 ui_widget_show_hide(gtk_notebook_get_nth_page(
1019 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
1020 ui_widget_show_hide(gtk_notebook_get_nth_page(
1021 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
1025 void ui_document_show_hide(GeanyDocument *doc)
1027 const gchar *widget_name;
1028 GtkWidget *item;
1029 const GeanyIndentPrefs *iprefs;
1031 g_return_if_fail(doc == NULL || doc->is_valid);
1033 if (doc == NULL)
1034 doc = document_get_current();
1036 if (doc == NULL)
1037 return;
1039 ignore_callback = TRUE;
1041 gtk_check_menu_item_set_active(
1042 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
1043 doc->editor->line_wrapping);
1045 gtk_check_menu_item_set_active(
1046 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
1047 doc->editor->line_breaking);
1049 iprefs = editor_get_indent_prefs(doc->editor);
1051 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
1052 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
1054 switch (iprefs->type)
1056 case GEANY_INDENT_TYPE_SPACES:
1057 widget_name = "spaces1"; break;
1058 case GEANY_INDENT_TYPE_TABS:
1059 widget_name = "tabs1"; break;
1060 case GEANY_INDENT_TYPE_BOTH:
1061 default:
1062 widget_name = "tabs_and_spaces1"; break;
1064 item = ui_lookup_widget(main_widgets.window, widget_name);
1065 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1067 if (iprefs->width >= 1 && iprefs->width <= 8)
1069 gchar *name;
1071 name = g_strdup_printf("indent_width_%d", iprefs->width);
1072 item = ui_lookup_widget(main_widgets.window, name);
1073 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1074 g_free(name);
1077 gtk_check_menu_item_set_active(
1078 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
1079 doc->readonly);
1081 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
1082 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
1083 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
1085 switch (sci_get_eol_mode(doc->editor->sci))
1087 case SC_EOL_CR: widget_name = "cr"; break;
1088 case SC_EOL_LF: widget_name = "lf"; break;
1089 default: widget_name = "crlf"; break;
1091 gtk_check_menu_item_set_active(
1092 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
1094 encodings_select_radio_item(doc->encoding);
1095 filetypes_select_radio_item(doc->file_type);
1097 ignore_callback = FALSE;
1101 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
1103 gtk_widget_set_name(widget, success ? NULL : "geany-search-entry-no-match");
1107 static void recent_create_menu(GeanyRecentFiles *grf)
1109 GtkWidget *tmp;
1110 guint i, len;
1111 gchar *filename;
1113 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
1114 for (i = 0; i < len; i++)
1116 filename = g_queue_peek_nth(grf->recent_queue, i);
1117 /* create menu item for the recent files menu in the menu bar */
1118 tmp = gtk_menu_item_new_with_label(filename);
1119 gtk_widget_show(tmp);
1120 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
1121 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1122 /* create menu item for the recent files menu in the toolbar */
1123 if (grf->toolbar != NULL)
1125 tmp = gtk_menu_item_new_with_label(filename);
1126 gtk_widget_show(tmp);
1127 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1128 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1134 static GeanyRecentFiles *recent_get_recent_files(void)
1136 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
1138 if (G_UNLIKELY(grf.recent_queue == NULL))
1140 grf.recent_queue = ui_prefs.recent_queue;
1141 grf.menubar = ui_widgets.recent_files_menu_menubar;
1142 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1143 toolbar_get_action_by_name("Open")));
1144 grf.activate_cb = recent_file_activate_cb;
1146 return &grf;
1150 static GeanyRecentFiles *recent_get_recent_projects(void)
1152 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
1154 if (G_UNLIKELY(grf.recent_queue == NULL))
1156 grf.recent_queue = ui_prefs.recent_projects_queue;
1157 grf.menubar = ui_widgets.recent_projects_menu_menubar;
1158 grf.toolbar = NULL;
1159 grf.activate_cb = recent_project_activate_cb;
1161 return &grf;
1165 void ui_create_recent_menus(void)
1167 recent_create_menu(recent_get_recent_files());
1168 recent_create_menu(recent_get_recent_projects());
1172 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1174 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1175 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1177 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1178 recent_file_loaded(utf8_filename, recent_get_recent_files());
1180 g_free(locale_filename);
1181 g_free(utf8_filename);
1185 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1187 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1188 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1190 if (project_ask_close() && project_load_file_with_session(locale_filename))
1191 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1193 g_free(locale_filename);
1194 g_free(utf8_filename);
1198 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf,
1199 const GtkRecentData *rdata)
1201 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1204 if (grf->type == RECENT_FILE_FILE && rdata)
1206 GtkRecentManager *manager = gtk_recent_manager_get_default();
1207 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1208 if (uri != NULL)
1210 gtk_recent_manager_add_full(manager, uri, rdata);
1211 g_free(uri);
1215 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1216 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1218 g_free(g_queue_pop_tail(grf->recent_queue));
1220 update_recent_menu(grf);
1222 /* filename already in recent list */
1223 else
1224 recent_file_loaded(utf8_filename, grf);
1228 void ui_add_recent_document(GeanyDocument *doc)
1230 /* what are the groups for actually? */
1231 static const gchar *groups[2] = {
1232 "geany",
1233 NULL
1235 GtkRecentData rdata;
1237 /* Prepare the data for gtk_recent_manager_add_full() */
1238 rdata.display_name = NULL;
1239 rdata.description = NULL;
1240 rdata.mime_type = doc->file_type->mime_type;
1241 /* if we ain't got no mime-type, fallback to plain text */
1242 if (! rdata.mime_type)
1243 rdata.mime_type = (gchar *) "text/plain";
1244 rdata.app_name = (gchar *) "geany";
1245 rdata.app_exec = (gchar *) "geany %u";
1246 rdata.groups = (gchar **) groups;
1247 rdata.is_private = FALSE;
1249 add_recent_file(doc->file_name, recent_get_recent_files(), &rdata);
1253 void ui_add_recent_project_file(const gchar *utf8_filename)
1255 add_recent_file(utf8_filename, recent_get_recent_projects(), NULL);
1259 /* Returns: newly allocated string with the UTF-8 menu text. */
1260 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1262 const gchar *text = NULL;
1264 if (gtk_bin_get_child(GTK_BIN(menu_item)))
1266 GtkWidget *child = gtk_bin_get_child(GTK_BIN(menu_item));
1268 if (GTK_IS_LABEL(child))
1269 text = gtk_label_get_text(GTK_LABEL(child));
1271 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1272 return g_strdup(text);
1276 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1278 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1279 gint result;
1281 if (utils_str_equal(menu_text, user_data))
1282 result = 0;
1283 else
1284 result = 1;
1286 g_free(menu_text);
1287 return result;
1291 /* update the project menu item's sensitivity */
1292 void ui_update_recent_project_menu(void)
1294 GeanyRecentFiles *grf = recent_get_recent_projects();
1295 GList *children, *item;
1297 /* only need to update the menubar menu, the project doesn't have a toolbar item */
1298 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1299 for (item = children; item; item = item->next)
1301 gboolean sensitive = TRUE;
1303 if (app->project)
1305 const gchar *filename = gtk_menu_item_get_label(item->data);
1306 sensitive = g_strcmp0(app->project->file_name, filename) != 0;
1308 gtk_widget_set_sensitive(item->data, sensitive);
1310 g_list_free(children);
1314 /* Use instead of gtk_menu_reorder_child() to update the menubar properly on OS X */
1315 static void menu_reorder_child(GtkMenu *menu, GtkWidget *child, gint position)
1317 gtk_menu_reorder_child(menu, child, position);
1318 #ifdef MAC_INTEGRATION
1319 /* On OS X GtkMenuBar is kept in sync with the native OS X menubar using
1320 * signals. Unfortunately gtk_menu_reorder_child() doesn't emit anything
1321 * so we have to update the OS X menubar manually. */
1322 gtkosx_application_sync_menubar(gtkosx_application_get());
1323 #endif
1327 static void add_recent_file_menu_item(const gchar *utf8_filename, GeanyRecentFiles *grf, GtkWidget *menu)
1329 GtkWidget *child = gtk_menu_item_new_with_label(utf8_filename);
1331 gtk_widget_show(child);
1332 if (menu != grf->toolbar)
1333 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), child);
1334 else
1336 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1337 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal
1338 * which we need in GeanyMenubuttonAction */
1339 gtk_container_add(GTK_CONTAINER(menu), child);
1340 menu_reorder_child(GTK_MENU(menu), child, 0);
1342 g_signal_connect(child, "activate", G_CALLBACK(grf->activate_cb), NULL);
1346 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1348 GList *item;
1349 GtkWidget *parents[] = { grf->menubar, grf->toolbar };
1350 guint i;
1352 /* first reorder the queue */
1353 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1354 g_return_if_fail(item != NULL);
1356 g_queue_unlink(grf->recent_queue, item);
1357 g_queue_push_head_link(grf->recent_queue, item);
1359 for (i = 0; i < G_N_ELEMENTS(parents); i++)
1361 GList *children;
1363 if (! parents[i])
1364 continue;
1366 children = gtk_container_get_children(GTK_CONTAINER(parents[i]));
1367 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1368 /* either reorder or prepend a new one */
1369 if (item)
1370 menu_reorder_child(GTK_MENU(parents[i]), item->data, 0);
1371 else
1372 add_recent_file_menu_item(utf8_filename, grf, parents[i]);
1373 g_list_free(children);
1376 if (grf->type == RECENT_FILE_PROJECT)
1377 ui_update_recent_project_menu();
1381 static void update_recent_menu(GeanyRecentFiles *grf)
1383 gchar *filename;
1384 GtkWidget *parents[] = { grf->menubar, grf->toolbar };
1385 guint i;
1387 filename = g_queue_peek_head(grf->recent_queue);
1389 for (i = 0; i < G_N_ELEMENTS(parents); i++)
1391 GList *children;
1393 if (! parents[i])
1394 continue;
1396 /* clean the MRU list before adding an item */
1397 children = gtk_container_get_children(GTK_CONTAINER(parents[i]));
1398 if (g_list_length(children) > file_prefs.mru_length - 1)
1400 GList *item = g_list_nth(children, file_prefs.mru_length - 1);
1401 while (item != NULL)
1403 if (GTK_IS_MENU_ITEM(item->data))
1404 gtk_widget_destroy(GTK_WIDGET(item->data));
1405 item = g_list_next(item);
1408 g_list_free(children);
1410 /* create the new item */
1411 add_recent_file_menu_item(filename, grf, parents[i]);
1414 if (grf->type == RECENT_FILE_PROJECT)
1415 ui_update_recent_project_menu();
1419 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1421 guint i;
1423 foreach_document (i)
1425 GeanyDocument *doc = documents[i];
1427 switch (feature)
1429 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1430 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1431 break;
1432 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1433 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin);
1434 break;
1435 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1436 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1437 break;
1438 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1439 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1440 break;
1441 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1442 editor_set_indentation_guides(doc->editor);
1443 break;
1449 void ui_update_view_editor_menu_items(void)
1451 ignore_callback = TRUE;
1452 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1453 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1454 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);
1455 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);
1456 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);
1457 ignore_callback = FALSE;
1461 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1462 * @param label_text The label text.
1463 * @param alignment An address to store the alignment widget pointer.
1465 * @return @transfer{floating} The frame widget, setting the alignment container for
1466 * packing child widgets.
1468 GEANY_API_SYMBOL
1469 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1471 GtkWidget *label, *align;
1472 GtkWidget *frame = gtk_frame_new(NULL);
1474 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1476 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1477 gtk_container_add(GTK_CONTAINER(frame), align);
1478 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1480 label = ui_label_new_bold(label_text);
1481 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1483 *alignment = align;
1484 return frame;
1488 /** Makes a fixed border for dialogs without increasing the button box border.
1489 * @param dialog The parent container for the @c GtkVBox.
1491 * @return @transfer{none} The packed @c GtkVBox. */
1492 GEANY_API_SYMBOL
1493 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1495 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1497 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1498 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox, TRUE, TRUE, 0);
1499 return vbox;
1503 /* Reorders a dialog's buttons
1504 * @param dialog A dialog
1505 * @param response First response ID to reorder
1506 * @param ... more response IDs, terminated by -1
1508 * Like gtk_dialog_set_alternative_button_order(), but reorders the default
1509 * buttons layout, not the alternative one. This is useful if you e.g. added a
1510 * button to a dialog which already had some and need yours not to be on the
1511 * end.
1513 /* Heavily based on gtk_dialog_set_alternative_button_order().
1514 * This relies on the action area to be a GtkBox, but although not documented
1515 * the API expose it to be a GtkHButtonBox though GtkBuilder, so it should be
1516 * fine */
1517 void ui_dialog_set_primary_button_order(GtkDialog *dialog, gint response, ...)
1519 va_list ap;
1520 GtkWidget *action_area = gtk_dialog_get_action_area(dialog);
1521 gint position;
1523 va_start(ap, response);
1524 for (position = 0; response != -1; position++)
1526 GtkWidget *child = gtk_dialog_get_widget_for_response(dialog, response);
1527 if (child)
1528 gtk_box_reorder_child(GTK_BOX(action_area), child, position);
1529 else
1530 g_warning("%s: no child button with response id %d.", G_STRFUNC, response);
1532 response = va_arg(ap, gint);
1534 va_end(ap);
1538 /** Creates a @c GtkButton with custom text and a stock image similar to
1539 * @c gtk_button_new_from_stock().
1540 * @param stock_id A @c GTK_STOCK_NAME string.
1541 * @param text Button label text, can include mnemonics.
1543 * @return @transfer{floating} The new @c GtkButton.
1545 GEANY_API_SYMBOL
1546 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1548 GtkWidget *image, *button;
1550 button = gtk_button_new_with_mnemonic(text);
1551 gtk_widget_show(button);
1552 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1553 gtk_button_set_image(GTK_BUTTON(button), image);
1554 /* note: image is shown by gtk */
1555 return button;
1559 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1560 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1561 * @param label Menu item label, can include mnemonics.
1562 * @return @transfer{floating} The new @c GtkImageMenuItem.
1564 * @since 0.16
1566 GEANY_API_SYMBOL
1567 GtkWidget *
1568 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1570 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1571 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1573 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1574 gtk_widget_show(image);
1575 return item;
1579 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1580 GdkEvent *event, gpointer data)
1582 if (event->button.button == 1 && icon_pos == 1)
1584 gtk_entry_set_text(entry, "");
1585 gtk_widget_grab_focus(GTK_WIDGET(entry));
1590 /** Adds a small clear icon to the right end of the passed @a entry.
1591 * A callback to clear the contents of the GtkEntry is automatically added.
1593 * @param entry The GtkEntry object to which the icon should be attached.
1595 * @since 0.16
1597 GEANY_API_SYMBOL
1598 void ui_entry_add_clear_icon(GtkEntry *entry)
1600 g_object_set(entry, "secondary-icon-stock", GTK_STOCK_CLEAR,
1601 "secondary-icon-activatable", TRUE, NULL);
1602 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1606 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1607 void ui_entry_add_activate_backward_signal(GtkEntry *entry)
1609 static gboolean installed = FALSE;
1611 g_return_if_fail(GTK_IS_ENTRY(entry));
1613 if (G_UNLIKELY(! installed))
1615 GtkBindingSet *binding_set;
1617 installed = TRUE;
1619 /* try to handle the unexpected case where GTK would already have installed the signal */
1620 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry)))
1622 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1623 return;
1626 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry),
1627 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 0, NULL, NULL,
1628 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1629 binding_set = gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry));
1630 gtk_binding_entry_add_signal(binding_set, GDK_Return, GDK_SHIFT_MASK, "activate-backward", 0);
1635 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1637 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1638 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1642 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1643 * the width of each button box's children.
1644 * Should be called after all child widgets have been packed. */
1645 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1647 GtkSizeGroup *size_group;
1649 gtk_box_set_spacing(GTK_BOX(copy), 10);
1650 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1652 /* now we need to put the widest widget from each button box in a size group,
1653 * but we don't know the width before they are drawn, and for different label
1654 * translations the widest widget can vary, so we just add all widgets. */
1655 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1656 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1657 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1658 g_object_unref(size_group);
1662 static gboolean tree_model_find_text(GtkTreeModel *model,
1663 GtkTreeIter *iter, gint column, const gchar *text)
1665 gchar *combo_text;
1666 gboolean found = FALSE;
1668 if (gtk_tree_model_get_iter_first(model, iter))
1672 gtk_tree_model_get(model, iter, 0, &combo_text, -1);
1673 found = utils_str_equal(combo_text, text);
1674 g_free(combo_text);
1676 if (found)
1677 return TRUE;
1679 while (gtk_tree_model_iter_next(model, iter));
1681 return FALSE;
1685 /** Prepends @a text to the drop down list, removing a duplicate element in
1686 * the list if found. Also ensures there are <= @a history_len elements.
1687 * @param combo_entry .
1688 * @param text @nullable Text to add, or @c NULL for current entry text.
1689 * @param history_len Max number of items, or @c 0 for default. */
1690 GEANY_API_SYMBOL
1691 void ui_combo_box_add_to_history(GtkComboBoxText *combo_entry,
1692 const gchar *text, gint history_len)
1694 GtkComboBox *combo = GTK_COMBO_BOX(combo_entry);
1695 GtkTreeModel *model;
1696 GtkTreeIter iter;
1697 GtkTreePath *path;
1699 if (history_len <= 0)
1700 history_len = 10;
1701 if (!text)
1702 text = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
1704 model = gtk_combo_box_get_model(combo);
1706 if (tree_model_find_text(model, &iter, 0, text))
1708 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1710 gtk_combo_box_text_prepend_text(combo_entry, text);
1712 /* limit history */
1713 path = gtk_tree_path_new_from_indices(history_len, -1);
1714 if (gtk_tree_model_get_iter(model, &iter, path))
1716 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1718 gtk_tree_path_free(path);
1722 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1723 * exists in the combo's model. */
1724 void ui_combo_box_prepend_text_once(GtkComboBoxText *combo, const gchar *text)
1726 GtkTreeModel *model;
1727 GtkTreeIter iter;
1729 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
1730 if (tree_model_find_text(model, &iter, 0, text))
1731 return; /* don't prepend duplicate */
1733 gtk_combo_box_text_prepend_text(combo, text);
1737 /* Changes the color of the notebook tab text and open files items according to
1738 * document status. */
1739 void ui_update_tab_status(GeanyDocument *doc)
1741 gtk_widget_set_name(doc->priv->tab_label, document_get_status_widget_class(doc));
1743 sidebar_openfiles_update(doc);
1747 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1748 gboolean down)
1750 GtkTreePath *path;
1751 gboolean result;
1753 if (down)
1754 return gtk_tree_model_iter_next(model, iter);
1756 path = gtk_tree_model_get_path(model, iter);
1757 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1758 gtk_tree_path_free(path);
1759 return result;
1763 /* note: the while loop might be more efficient when searching upwards if it
1764 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1765 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1767 GtkTreeSelection *treesel;
1768 GtkTreeIter iter;
1769 GtkTreeModel *model;
1771 treesel = gtk_tree_view_get_selection(treeview);
1772 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1774 /* get the next selected item */
1775 if (! tree_model_iter_get_next(model, &iter, down))
1776 return FALSE; /* no more items */
1778 else /* no selection */
1780 if (! gtk_tree_model_get_iter_first(model, &iter))
1781 return TRUE; /* no items */
1783 while (TRUE)
1785 gtk_tree_selection_select_iter(treesel, &iter);
1786 if (cb(FALSE))
1787 break; /* found next message */
1789 if (! tree_model_iter_get_next(model, &iter, down))
1790 return FALSE; /* no more items */
1792 /* scroll item in view */
1793 if (ui_prefs.msgwindow_visible)
1795 GtkTreePath *path = gtk_tree_model_get_path(
1796 gtk_tree_view_get_model(treeview), &iter);
1798 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1799 gtk_tree_path_free(path);
1801 return TRUE;
1805 /* Returns FALSE if the treeview has items but no matching next item. */
1806 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1808 return tree_view_find(treeview, cb, TRUE);
1812 /* Returns FALSE if the treeview has items but no matching next item. */
1813 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1815 return tree_view_find(treeview, cb, FALSE);
1819 /* Shamelessly stolen from GTK */
1820 static gboolean ui_tree_view_query_tooltip_cb(GtkWidget *widget, gint x, gint y,
1821 gboolean keyboard_tip, GtkTooltip *tooltip, gpointer data)
1823 GValue value = { 0 };
1824 GValue transformed = { 0 };
1825 GtkTreeIter iter;
1826 GtkTreePath *path;
1827 GtkTreeModel *model;
1828 GtkTreeView *tree_view = GTK_TREE_VIEW(widget);
1829 gint column = GPOINTER_TO_INT(data);
1830 gboolean tootlip_set = FALSE;
1832 if (! gtk_tree_view_get_tooltip_context(tree_view, &x, &y, keyboard_tip, &model, &path, &iter))
1833 return FALSE;
1835 gtk_tree_model_get_value(model, &iter, column, &value);
1837 g_value_init(&transformed, G_TYPE_STRING);
1838 if (g_value_transform(&value, &transformed) && g_value_get_string(&transformed))
1840 gtk_tooltip_set_text(tooltip, g_value_get_string(&transformed));
1841 gtk_tree_view_set_tooltip_row(tree_view, tooltip, path);
1842 tootlip_set = TRUE;
1845 g_value_unset(&transformed);
1846 g_value_unset(&value);
1847 gtk_tree_path_free(path);
1849 return tootlip_set;
1853 /** Adds text tooltips to a tree view.
1855 * This is similar to gtk_tree_view_set_tooltip_column() but considers the column contents to be
1856 * text, not markup -- it uses gtk_tooltip_set_text() rather than gtk_tooltip_set_markup() to set
1857 * the tooltip's value.
1859 * @warning Unlike gtk_tree_view_set_tooltip_column() you currently cannot change or remove the
1860 * tooltip column after it has been added. Trying to do so will probably give funky results.
1862 * @param tree_view The tree view
1863 * @param column The column to get the tooltip from
1865 * @since 1.25 (API 223)
1867 /* Note: @p column is int and not uint both to match gtk_tree_view_set_tooltip_column() signature
1868 * and to allow future support of -1 to unset if ever wanted */
1869 GEANY_API_SYMBOL
1870 void ui_tree_view_set_tooltip_text_column(GtkTreeView *tree_view, gint column)
1872 g_return_if_fail(column >= 0);
1873 g_return_if_fail(GTK_IS_TREE_VIEW(tree_view));
1875 g_signal_connect(tree_view, "query-tooltip",
1876 G_CALLBACK(ui_tree_view_query_tooltip_cb), GINT_TO_POINTER(column));
1877 gtk_widget_set_has_tooltip(GTK_WIDGET(tree_view), TRUE);
1882 * Modifies the font of a widget using gtk_widget_modify_font().
1884 * @param widget The widget.
1885 * @param str The font name as expected by pango_font_description_from_string().
1887 GEANY_API_SYMBOL
1888 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1890 PangoFontDescription *pfd;
1892 pfd = pango_font_description_from_string(str);
1893 gtk_widget_modify_font(widget, pfd);
1894 pango_font_description_free(pfd);
1898 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1899 * file chooser, replacing entry text (if successful) with the path returned from the
1900 * @c GtkFileChooser.
1901 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1902 * @param title @nullable The file chooser dialog title, or @c NULL.
1903 * @param action The mode of the file chooser.
1904 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1905 * such as @c GtkComboBoxEntry.
1907 * @return @transfer{floating} The @c GtkHBox.
1909 /* @see ui_setup_open_button_callback(). */
1910 GEANY_API_SYMBOL
1911 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1913 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1915 hbox = gtk_hbox_new(FALSE, 6);
1916 path_entry = GTK_WIDGET(entry);
1918 /* prevent path_entry being vertically stretched to the height of dirbtn */
1919 vbox = gtk_vbox_new(FALSE, 0);
1920 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1922 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1924 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1926 else
1927 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1929 dirbtn = gtk_button_new();
1930 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1931 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1932 ui_setup_open_button_callback(dirbtn, title, action, entry);
1934 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1935 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1936 return hbox;
1940 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1943 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1944 * title can be NULL.
1945 * action is the file chooser mode to use. */
1946 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1947 GtkFileChooserAction action, GtkEntry *entry)
1949 GtkWidget *path_entry = GTK_WIDGET(entry);
1951 if (title)
1952 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1953 (GDestroyNotify) g_free);
1954 g_object_set_data(G_OBJECT(open_btn), "action", GINT_TO_POINTER(action));
1955 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), path_entry);
1959 #ifndef G_OS_WIN32
1960 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1961 const gchar *utf8_path)
1963 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1964 GTK_WINDOW(main_widgets.window), action,
1965 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1966 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1967 gchar *locale_path;
1968 gchar *ret_path = NULL;
1970 gtk_widget_set_name(dialog, "GeanyDialog");
1971 locale_path = utils_get_locale_from_utf8(utf8_path);
1972 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1974 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1975 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1977 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1979 if (g_path_is_absolute(locale_path))
1980 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1982 g_free(locale_path);
1984 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1986 gchar *dir_locale;
1988 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1989 ret_path = utils_get_utf8_from_locale(dir_locale);
1990 g_free(dir_locale);
1992 gtk_widget_destroy(dialog);
1993 return ret_path;
1995 #endif
1998 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
2000 GtkFileChooserAction action = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "action"));
2001 GtkEntry *entry = user_data;
2002 const gchar *title = g_object_get_data(G_OBJECT(button), "title");
2003 gchar *utf8_path = NULL;
2005 /* TODO: extend for other actions */
2006 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2007 action == GTK_FILE_CHOOSER_ACTION_OPEN);
2009 if (title == NULL)
2010 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
2011 _("Select Folder") : _("Select File");
2013 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
2015 #ifdef G_OS_WIN32
2016 utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
2017 gtk_entry_get_text(GTK_ENTRY(entry)));
2018 #else
2019 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
2020 #endif
2022 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
2024 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
2025 #ifdef G_OS_WIN32
2026 utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
2027 gtk_entry_get_text(GTK_ENTRY(entry)));
2028 #else
2029 utf8_path = run_file_chooser(title, action, path);
2030 #endif
2031 g_free(path);
2034 if (utf8_path != NULL)
2036 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
2037 g_free(utf8_path);
2042 void ui_statusbar_showhide(gboolean state)
2044 /* handle statusbar visibility */
2045 if (state)
2047 gtk_widget_show(ui_widgets.statusbar);
2048 ui_update_statusbar(NULL, -1);
2050 else
2051 gtk_widget_hide(ui_widgets.statusbar);
2055 /** Packs all @c GtkWidgets passed after the row argument into a table, using
2056 * one widget per cell. The first widget is not expanded as the table grows,
2057 * as this is usually a label.
2058 * @param table
2059 * @param row The row number of the table.
2061 GEANY_API_SYMBOL
2062 void ui_table_add_row(GtkTable *table, gint row, ...)
2064 va_list args;
2065 guint i;
2066 GtkWidget *widget;
2068 va_start(args, row);
2069 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
2071 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
2073 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
2074 options, 0, 0, 0);
2076 va_end(args);
2080 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
2082 const gchar *file_name = user_data;
2083 GeanyFiletype *ft = NULL;
2085 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
2086 ft = filetypes[GEANY_FILETYPES_CONF];
2088 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
2089 document_open_file(file_name, FALSE, ft, NULL);
2090 else
2092 gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
2093 gchar *base_name = g_path_get_basename(file_name);
2094 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
2095 gchar *global_content = NULL;
2097 /* if the requested file doesn't exist in the user's config dir, try loading the file
2098 * from the global data directory and use its contents for the newly created file */
2099 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
2100 g_file_get_contents(global_file, &global_content, NULL, NULL);
2102 document_new_file(utf8_filename, ft, global_content);
2104 utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
2109 static void free_on_closure_notify(gpointer data, GClosure *closure)
2111 g_free(data);
2115 /* @note You should connect to the "document-save" signal yourself to detect
2116 * if the user has just saved the config file, reloading it. */
2117 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
2119 GtkWidget *item;
2121 if (!parent)
2122 parent = GTK_CONTAINER(widgets.config_files_menu);
2124 if (!label)
2126 gchar *base_name;
2128 base_name = g_path_get_basename(real_path);
2129 item = gtk_menu_item_new_with_label(base_name);
2130 g_free(base_name);
2132 else
2133 item = gtk_menu_item_new_with_mnemonic(label);
2135 gtk_widget_show(item);
2136 gtk_container_add(parent, item);
2137 g_signal_connect_data(item, "activate", G_CALLBACK(on_config_file_clicked),
2138 g_strdup(real_path), free_on_closure_notify, 0);
2142 static gboolean sort_menu(gpointer data)
2144 ui_menu_sort_by_label(GTK_MENU(data));
2145 return FALSE;
2149 static void create_config_files_menu(void)
2151 GtkWidget *menu, *item;
2153 widgets.config_files_menu = menu = gtk_menu_new();
2155 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
2156 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
2158 item = gtk_menu_item_new_with_mnemonic(_("_Filetype Configuration"));
2159 gtk_container_add(GTK_CONTAINER(menu), item);
2160 ui_widgets.config_files_filetype_menu = gtk_menu_new();
2161 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), ui_widgets.config_files_filetype_menu);
2162 gtk_widget_show(item);
2164 /* sort menu after all items added */
2165 g_idle_add(sort_menu, widgets.config_files_menu);
2169 /* adds factory icons with a named icon source using the stock items id */
2170 static void add_stock_icons(const GtkStockItem *items, gsize count)
2172 GtkIconFactory *factory = gtk_icon_factory_new();
2173 GtkIconSource *source = gtk_icon_source_new();
2174 gsize i;
2176 for (i = 0; i < count; i++)
2178 GtkIconSet *set = gtk_icon_set_new();
2180 gtk_icon_source_set_icon_name(source, items[i].stock_id);
2181 gtk_icon_set_add_source(set, source);
2182 gtk_icon_factory_add(factory, items[i].stock_id, set);
2183 gtk_icon_set_unref(set);
2185 gtk_icon_source_free(source);
2186 gtk_icon_factory_add_default(factory);
2187 g_object_unref(factory);
2191 void ui_init_stock_items(void)
2193 GtkStockItem items[] =
2195 { GEANY_STOCK_SAVE_ALL, N_("Save All"), 0, 0, GETTEXT_PACKAGE },
2196 { GEANY_STOCK_CLOSE_ALL, N_("Close All"), 0, 0, GETTEXT_PACKAGE },
2197 { GEANY_STOCK_BUILD, N_("Build"), 0, 0, GETTEXT_PACKAGE }
2200 gtk_stock_add(items, G_N_ELEMENTS(items));
2201 add_stock_icons(items, G_N_ELEMENTS(items));
2205 void ui_init_toolbar_widgets(void)
2207 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
2208 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
2209 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
2210 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
2214 void ui_swap_sidebar_pos(void)
2216 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
2217 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
2218 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
2220 g_object_ref(left);
2221 g_object_ref(right);
2222 gtk_container_remove (GTK_CONTAINER (pane), left);
2223 gtk_container_remove (GTK_CONTAINER (pane), right);
2224 /* only scintilla notebook should expand */
2225 gtk_paned_pack1(GTK_PANED(pane), right, right == main_widgets.notebook, TRUE);
2226 gtk_paned_pack2(GTK_PANED(pane), left, left == main_widgets.notebook, TRUE);
2227 g_object_unref(left);
2228 g_object_unref(right);
2230 gtk_paned_set_position(GTK_PANED(pane), gtk_widget_get_allocated_width(pane)
2231 - gtk_paned_get_position(GTK_PANED(pane)));
2235 static void init_recent_files(void)
2237 GtkWidget *toolbar_recent_files_menu;
2239 /* add recent files to the File menu */
2240 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
2241 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
2242 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
2243 ui_widgets.recent_files_menu_menubar);
2245 /* add recent files to the toolbar Open button */
2246 toolbar_recent_files_menu = gtk_menu_new();
2247 g_object_ref(toolbar_recent_files_menu);
2248 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
2249 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
2253 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
2255 g_object_ref(menu);
2256 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
2257 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
2258 g_object_unref(menu);
2262 typedef struct GeanySharedMenu
2264 const gchar *menu;
2265 const gchar *menubar_item;
2266 const gchar *popup_item;
2268 GeanySharedMenu;
2270 #define foreach_menu(item, array) \
2271 for (item = array; item->menu; item++)
2273 static void on_editor_menu_show(GtkWidget *widget, GeanySharedMenu *items)
2275 GeanySharedMenu *item;
2277 foreach_menu(item, items)
2279 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2280 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2281 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2283 ui_menu_move(menu, bar, popup);
2288 static void on_editor_menu_hide(GtkWidget *widget, GeanySharedMenu *items)
2290 GeanySharedMenu *item;
2292 foreach_menu(item, items)
2294 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2295 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2296 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2298 ui_menu_move(menu, popup, bar);
2303 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2304 * so this is called after that's done. */
2305 void ui_init_prefs(void)
2307 StashGroup *group = stash_group_new(PACKAGE);
2309 /* various prefs */
2310 configuration_add_various_pref_group(group);
2312 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
2313 "show_symbol_list_expanders", TRUE);
2314 stash_group_add_boolean(group, &interface_prefs.compiler_tab_autoscroll,
2315 "compiler_tab_autoscroll", TRUE);
2316 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
2317 "allow_always_save", FALSE);
2318 stash_group_add_string(group, &ui_prefs.statusbar_template,
2319 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE));
2320 stash_group_add_boolean(group, &ui_prefs.new_document_after_close,
2321 "new_document_after_close", FALSE);
2322 stash_group_add_boolean(group, &interface_prefs.msgwin_status_visible,
2323 "msgwin_status_visible", TRUE);
2324 stash_group_add_boolean(group, &interface_prefs.msgwin_compiler_visible,
2325 "msgwin_compiler_visible", TRUE);
2326 stash_group_add_boolean(group, &interface_prefs.msgwin_messages_visible,
2327 "msgwin_messages_visible", TRUE);
2328 stash_group_add_boolean(group, &interface_prefs.msgwin_scribble_visible,
2329 "msgwin_scribble_visible", TRUE);
2333 /* Used to find out the name of the GtkBuilder retrieved object since
2334 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2335 * 'name' property for that and those that don't implement GtkBuildable
2336 * will have a "gtk-builder-name" stored in the GObject's data list. */
2337 static const gchar *ui_guess_object_name(GObject *obj)
2339 const gchar *name = NULL;
2341 g_return_val_if_fail(G_IS_OBJECT(obj), NULL);
2343 if (GTK_IS_BUILDABLE(obj))
2344 name = gtk_buildable_get_name(GTK_BUILDABLE(obj));
2345 if (! name)
2346 name = g_object_get_data(obj, "gtk-builder-name");
2347 if (! name)
2348 return NULL;
2350 return name;
2354 /* Compatibility functions */
2355 GtkWidget *create_edit_menu1(void)
2357 return edit_menu1;
2361 GtkWidget *create_prefs_dialog(void)
2363 return prefs_dialog;
2367 GtkWidget *create_project_dialog(void)
2369 return project_dialog;
2373 GtkWidget *create_toolbar_popup_menu1(void)
2375 return toolbar_popup_menu1;
2379 GtkWidget *create_window1(void)
2381 return window1;
2385 static GtkWidget *ui_get_top_parent(GtkWidget *widget)
2387 GtkWidget *parent;
2389 g_return_val_if_fail(GTK_IS_WIDGET(widget), NULL);
2391 for (;;)
2393 if (GTK_IS_MENU(widget))
2394 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2395 else
2396 parent = gtk_widget_get_parent(widget);
2397 if (parent == NULL)
2398 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2399 if (parent == NULL)
2400 break;
2401 widget = parent;
2404 return widget;
2408 void ui_init_builder(void)
2410 gchar *interface_file;
2411 const gchar *name;
2412 GError *error;
2413 GSList *iter, *all_objects;
2414 GtkWidget *widget, *toplevel;
2416 /* prevent function from being called twice */
2417 if (GTK_IS_BUILDER(builder))
2418 return;
2420 builder = gtk_builder_new();
2422 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
2424 error = NULL;
2425 interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
2426 if (! gtk_builder_add_from_file(builder, interface_file, &error))
2428 /* Show the user this message so they know WTF happened */
2429 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
2430 _("Geany cannot start!"), error->message);
2431 /* Aborts */
2432 g_error("Cannot create user-interface: %s", error->message);
2433 g_error_free(error);
2434 g_free(interface_file);
2435 g_object_unref(builder);
2436 return;
2438 g_free(interface_file);
2440 callbacks_connect(builder);
2442 edit_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "edit_menu1"));
2443 prefs_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "prefs_dialog"));
2444 project_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "project_dialog"));
2445 toolbar_popup_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar_popup_menu1"));
2446 window1 = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
2448 g_object_set_data(G_OBJECT(edit_menu1), "edit_menu1", edit_menu1);
2449 g_object_set_data(G_OBJECT(prefs_dialog), "prefs_dialog", prefs_dialog);
2450 g_object_set_data(G_OBJECT(project_dialog), "project_dialog", project_dialog);
2451 g_object_set_data(G_OBJECT(toolbar_popup_menu1), "toolbar_popup_menu1", toolbar_popup_menu1);
2452 g_object_set_data(G_OBJECT(window1), "window1", window1);
2454 all_objects = gtk_builder_get_objects(builder);
2455 for (iter = all_objects; iter != NULL; iter = g_slist_next(iter))
2457 if (! GTK_IS_WIDGET(iter->data))
2458 continue;
2460 widget = GTK_WIDGET(iter->data);
2462 name = ui_guess_object_name(G_OBJECT(widget));
2463 if (! name)
2465 g_warning("Unable to get name from GtkBuilder object");
2466 continue;
2469 toplevel = ui_get_top_parent(widget);
2470 if (toplevel)
2471 ui_hookup_widget(toplevel, widget, name);
2473 g_slist_free(all_objects);
2477 static void init_custom_style(void)
2479 #if GTK_CHECK_VERSION(3, 0, 0)
2480 gchar *css_file = g_build_filename(app->datadir, "geany.css", NULL);
2481 GtkCssProvider *css = gtk_css_provider_new();
2482 GError *error = NULL;
2484 if (! gtk_css_provider_load_from_path(css, css_file, &error))
2486 g_warning("Failed to load custom CSS: %s", error->message);
2487 g_error_free(error);
2489 else
2491 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2492 GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2495 g_object_unref(css);
2496 g_free(css_file);
2497 #else
2498 /* see setup_gtk2_styles() in main.c */
2499 #endif
2503 void ui_init(void)
2505 init_custom_style();
2507 init_recent_files();
2509 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
2510 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
2512 main_widgets.progressbar = progress_bar_create();
2514 /* current word sensitive items */
2515 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition2");
2516 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "context_action1");
2517 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage2");
2518 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage2");
2520 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
2521 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
2522 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
2523 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
2524 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
2525 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
2526 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
2527 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
2528 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
2529 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
2530 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
2531 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
2532 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
2533 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
2535 /* reparent context submenus as needed */
2537 GeanySharedMenu arr[] = {
2538 {"commands2_menu", "commands2", "commands1"},
2539 {"menu_format1_menu", "menu_format1", "menu_format2"},
2540 {"more1_menu", "more1", "search2"},
2541 {NULL, NULL, NULL}
2543 static GeanySharedMenu items[G_N_ELEMENTS(arr)];
2545 memcpy(items, arr, sizeof(arr));
2546 g_signal_connect(main_widgets.editor_menu, "show", G_CALLBACK(on_editor_menu_show), items);
2547 g_signal_connect(main_widgets.editor_menu, "hide", G_CALLBACK(on_editor_menu_hide), items);
2550 ui_init_toolbar_widgets();
2551 init_document_widgets();
2552 create_config_files_menu();
2556 void ui_finalize_builder(void)
2558 if (GTK_IS_BUILDER(builder))
2559 g_object_unref(builder);
2561 /* cleanup refs lingering even after GtkBuilder is destroyed */
2562 if (GTK_IS_WIDGET(edit_menu1))
2563 gtk_widget_destroy(edit_menu1);
2564 if (GTK_IS_WIDGET(prefs_dialog))
2565 gtk_widget_destroy(prefs_dialog);
2566 if (GTK_IS_WIDGET(project_dialog))
2567 gtk_widget_destroy(project_dialog);
2568 if (GTK_IS_WIDGET(toolbar_popup_menu1))
2569 gtk_widget_destroy(toolbar_popup_menu1);
2570 if (GTK_IS_WIDGET(window1))
2571 gtk_widget_destroy(window1);
2575 static void auto_separator_update(GeanyAutoSeparator *autosep)
2577 g_return_if_fail(autosep->item_count >= 0);
2579 if (autosep->widget)
2581 if (autosep->item_count > 0)
2582 ui_widget_show_hide(autosep->widget, autosep->show_count > 0);
2583 else
2584 gtk_widget_destroy(autosep->widget);
2589 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
2591 GeanyAutoSeparator *autosep = user_data;
2593 if (gtk_widget_get_visible(widget))
2594 autosep->show_count++;
2595 else
2596 autosep->show_count--;
2597 auto_separator_update(autosep);
2601 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
2603 GeanyAutoSeparator *autosep = user_data;
2605 autosep->item_count--;
2606 autosep->item_count = MAX(autosep->item_count, 0);
2607 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2608 * so assume widget was visible */
2609 autosep->show_count--;
2610 autosep->show_count = MAX(autosep->item_count, 0);
2611 auto_separator_update(autosep);
2615 /* Show the separator widget if @a item or another is visible. */
2616 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2617 * property, and using reference counting to keep the widget alive whilst its visible group
2618 * is alive. */
2619 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2621 /* set widget ptr NULL when widget destroyed */
2622 if (autosep->item_count == 0)
2623 g_signal_connect(autosep->widget, "destroy",
2624 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2626 if (gtk_widget_get_visible(item))
2627 autosep->show_count++;
2629 autosep->item_count++;
2630 auto_separator_update(autosep);
2632 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2633 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2634 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2639 * Sets @a text as the contents of the tooltip for @a widget.
2641 * @param widget The widget the tooltip should be set for.
2642 * @param text The text for the tooltip.
2644 * @since 0.16
2645 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2647 GEANY_API_SYMBOL
2648 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2650 gtk_widget_set_tooltip_text(widget, text);
2654 /** Returns a widget from a name in a component, usually created by Glade.
2655 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2656 * or alternatively any widget in the component, and the name of the widget
2657 * you want returned.
2658 * @param widget Widget with the @a widget_name property set.
2659 * @param widget_name Name to lookup.
2661 * @return @transfer{none} The widget found.
2662 * @see ui_hookup_widget().
2664 * @since 0.16
2666 GEANY_API_SYMBOL
2667 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2669 GtkWidget *parent, *found_widget;
2671 g_return_val_if_fail(widget != NULL, NULL);
2672 g_return_val_if_fail(widget_name != NULL, NULL);
2674 for (;;)
2676 if (GTK_IS_MENU(widget))
2677 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2678 else
2679 parent = gtk_widget_get_parent(widget);
2680 if (parent == NULL)
2681 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2682 if (parent == NULL)
2683 break;
2684 widget = parent;
2687 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2688 if (G_UNLIKELY(found_widget == NULL))
2689 g_warning("Widget not found: %s", widget_name);
2690 return found_widget;
2694 /* wraps gtk_builder_get_object()
2695 * unlike ui_lookup_widget(), it does only support getting object created from the main
2696 * UI file, but it can fetch any object, not only widgets */
2697 gpointer ui_builder_get_object (const gchar *name)
2699 return gtk_builder_get_object (builder, name);
2703 /* Progress Bar */
2704 static guint progress_bar_timer_id = 0;
2707 static GtkWidget *progress_bar_create(void)
2709 GtkWidget *bar = gtk_progress_bar_new();
2711 /* Set the progressbar's height to 1 to fit it in the statusbar */
2712 gtk_widget_set_size_request(bar, -1, 1);
2713 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2715 return bar;
2719 static gboolean progress_bar_pulse(gpointer data)
2721 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2723 return TRUE;
2728 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2729 * (if the statusbar is visible). This is a convenience function which adds a timer to
2730 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2731 * You can use this function when you have time consuming asynchronous operation and want to
2732 * display some activity in the GUI and when you don't know about detailed progress steps.
2733 * The progressbar widget is hidden by default when it is not active. This function and
2734 * ui_progress_bar_stop() will show and hide it automatically for you.
2736 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2737 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2738 * In this case, you need to show and hide the widget yourself. You can find some example code
2739 * in @c src/printing.c.
2741 * @param text @nullable The text to be shown as the progress bar label or @c NULL to leave it empty.
2743 * @since 0.16
2745 GEANY_API_SYMBOL
2746 void ui_progress_bar_start(const gchar *text)
2748 g_return_if_fail(progress_bar_timer_id == 0);
2750 if (! interface_prefs.statusbar_visible)
2751 return;
2753 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2755 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2757 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2761 /** Stops a running progress bar and hides the widget again.
2763 * @since 0.16
2765 GEANY_API_SYMBOL
2766 void ui_progress_bar_stop(void)
2768 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2770 if (progress_bar_timer_id != 0)
2772 g_source_remove(progress_bar_timer_id);
2773 progress_bar_timer_id = 0;
2778 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2780 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2781 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2782 gchar *sa, *sb;
2783 gint result;
2785 /* put entries with submenus at the end of the menu */
2786 if (gtk_menu_item_get_submenu(item_a) && !gtk_menu_item_get_submenu(item_b))
2787 return 1;
2788 else if (!gtk_menu_item_get_submenu(item_a) && gtk_menu_item_get_submenu(item_b))
2789 return -1;
2791 sa = ui_menu_item_get_text(item_a);
2792 sb = ui_menu_item_get_text(item_b);
2793 result = utils_str_casecmp(sa, sb);
2794 g_free(sa);
2795 g_free(sb);
2796 return result;
2800 /* Currently @a menu should contain only GtkMenuItems with labels. */
2801 static void ui_menu_sort_by_label(GtkMenu *menu)
2803 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2804 GList *node;
2805 gint pos;
2807 list = g_list_sort(list, compare_menu_item_labels);
2808 pos = 0;
2809 foreach_list(node, list)
2811 menu_reorder_child(menu, node->data, pos);
2812 pos++;
2814 g_list_free(list);
2818 void ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2820 va_list a;
2821 gchar *text;
2823 va_start(a, format);
2824 text = g_strdup_vprintf(format, a);
2825 va_end(a);
2827 gtk_label_set_text(label, text);
2828 gtk_label_set_use_markup(label, TRUE);
2829 g_free(text);
2833 GtkWidget *ui_label_new_bold(const gchar *text)
2835 GtkWidget *label;
2836 gchar *label_text;
2838 label_text = g_markup_escape_text(text, -1);
2839 label = gtk_label_new(NULL);
2840 ui_label_set_markup(GTK_LABEL(label), "<b>%s</b>", label_text);
2841 g_free(label_text);
2842 return label;
2846 /** @girskip
2847 * Adds a list of document items to @a menu.
2848 * @param menu Menu.
2849 * @param active @nullable Which document to highlight, or @c NULL.
2850 * @param callback is used for each menu item's @c "activate" signal and will be
2851 * passed the corresponding document pointer as @c user_data.
2852 * @warning You should check @c doc->is_valid in the callback.
2853 * @since 0.19
2855 GEANY_API_SYMBOL
2856 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2858 ui_menu_add_document_items_sorted(menu, active, callback, NULL);
2862 /** @girskip
2863 * Adds a list of document items to @a menu.
2865 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2866 * the order of the document tabs is used.
2868 * See document_compare_by_display_name() for an example sort function.
2870 * @param menu Menu.
2871 * @param active @nullable Which document to highlight, or @c NULL.
2872 * @param callback is used for each menu item's @c "activate" signal and will be passed
2873 * the corresponding document pointer as @c user_data.
2874 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2875 * @warning You should check @c doc->is_valid in the callback.
2876 * @since 0.21
2878 GEANY_API_SYMBOL
2879 void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
2880 GCallback callback, GCompareFunc compare_func)
2882 GtkWidget *menu_item, *menu_item_label, *image;
2883 GeanyDocument *doc;
2884 guint i, len;
2885 gchar *base_name, *label;
2886 GPtrArray *sorted_documents;
2888 len = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2890 sorted_documents = g_ptr_array_sized_new(len);
2891 /* copy the documents_array into the new one */
2892 foreach_document(i)
2894 g_ptr_array_add(sorted_documents, documents[i]);
2896 if (compare_func == NULL)
2897 compare_func = document_compare_by_tab_order;
2899 /* and now sort it */
2900 g_ptr_array_sort(sorted_documents, compare_func);
2902 for (i = 0; i < sorted_documents->len; i++)
2904 doc = g_ptr_array_index(sorted_documents, i);
2906 base_name = g_path_get_basename(DOC_FILENAME(doc));
2907 menu_item = gtk_image_menu_item_new_with_label(base_name);
2908 image = gtk_image_new_from_gicon(doc->file_type->icon, GTK_ICON_SIZE_MENU);
2909 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
2911 gtk_widget_show(menu_item);
2912 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2913 g_signal_connect(menu_item, "activate", callback, doc);
2915 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2916 gtk_widget_set_name(menu_item_label, document_get_status_widget_class(doc));
2918 if (doc == active)
2920 label = g_markup_escape_text(base_name, -1);
2921 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", label);
2922 g_free(label);
2925 g_free(base_name);
2927 g_ptr_array_free(sorted_documents, TRUE);
2931 /** Checks whether the passed @a keyval is the Enter or Return key.
2932 * There are three different Enter/Return key values
2933 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2934 * This is just a convenience function.
2935 * @param keyval A keyval.
2936 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2937 * @since 0.19 */
2938 GEANY_API_SYMBOL
2939 gboolean ui_is_keyval_enter_or_return(guint keyval)
2941 return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
2945 /** Reads an integer from the GTK default settings registry
2946 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2947 * @param property_name The property to read.
2948 * @param default_value The default value in case the value could not be read.
2949 * @return The value for the property if it exists, otherwise the @a default_value.
2950 * @since 0.19 */
2951 GEANY_API_SYMBOL
2952 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
2954 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2955 gtk_settings_get_default())), property_name))
2957 gint value;
2958 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
2959 return value;
2961 else
2962 return default_value;
2966 void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
2967 gint new_text_len, gint *position, gpointer data)
2969 gboolean first = position != NULL && *position == 0;
2970 gint i;
2972 if (new_text_len == -1)
2973 new_text_len = (gint) strlen(new_text);
2975 for (i = 0; i < new_text_len; i++, new_text++)
2977 if ((!first || !strchr("+-", *new_text)) && !isdigit(*new_text))
2979 g_signal_stop_emission_by_name(editable, "insert-text");
2980 break;
2982 first = FALSE;
2987 /* gets the icon that applies to a particular MIME type */
2988 GIcon *ui_get_mime_icon(const gchar *mime_type)
2990 GIcon *icon = NULL;
2991 gchar *ctype;
2993 ctype = g_content_type_from_mime_type(mime_type);
2994 if (ctype)
2996 GdkScreen *screen = gdk_screen_get_default();
2998 icon = g_content_type_get_icon(ctype);
2999 if (screen && icon)
3001 GtkIconInfo *icon_info;
3003 icon_info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_for_screen(screen), icon, 16, 0);
3004 if (!icon_info)
3006 g_object_unref(icon);
3007 icon = NULL;
3009 else
3010 gtk_icon_info_free(icon_info);
3013 g_free(ctype);
3016 /* fallback if icon lookup failed, like it might happen on Windows (?) */
3017 if (! icon)
3019 const gchar *icon_name = "text-x-generic";
3021 if (strstr(mime_type, "directory"))
3022 icon_name = "folder";
3024 icon = g_themed_icon_new(icon_name);
3026 return icon;
3030 void ui_focus_current_document(void)
3032 GeanyDocument *doc = document_get_current();
3034 if (doc != NULL)
3035 document_grab_focus(doc);
3039 /** Finds the label text associated with stock_id
3040 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
3041 * @return The label text for stock
3042 * @since Geany 1.22 */
3043 GEANY_API_SYMBOL
3044 const gchar *ui_lookup_stock_label(const gchar *stock_id)
3046 GtkStockItem item;
3048 if (gtk_stock_lookup(stock_id, &item))
3049 return item.label;
3051 g_warning("No stock id '%s'!", stock_id);
3052 return NULL;
3056 /* finds the next iter at any level
3057 * @param iter in/out, the current iter, will be changed to the next one
3058 * @param down whether to try the child iter
3059 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
3060 gboolean ui_tree_model_iter_any_next(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
3062 GtkTreeIter guess;
3063 GtkTreeIter copy = *iter;
3065 /* go down if the item has children */
3066 if (down && gtk_tree_model_iter_children(model, &guess, iter))
3067 *iter = guess;
3068 /* or to the next item at the same level */
3069 else if (gtk_tree_model_iter_next(model, &copy))
3070 *iter = copy;
3071 /* or to the next item at a parent level */
3072 else if (gtk_tree_model_iter_parent(model, &guess, iter))
3074 copy = guess;
3075 while (TRUE)
3077 if (gtk_tree_model_iter_next(model, &copy))
3079 *iter = copy;
3080 return TRUE;
3082 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
3083 guess = copy;
3084 else
3085 return FALSE;
3088 else
3089 return FALSE;
3091 return TRUE;
3095 GtkWidget *ui_create_encodings_combo_box(gboolean has_detect, gint default_enc)
3097 GtkCellRenderer *renderer;
3098 GtkTreeIter iter;
3099 GtkWidget *combo = gtk_combo_box_new();
3100 GtkTreeStore *store = encodings_encoding_store_new(has_detect);
3102 if (default_enc < 0 || default_enc >= GEANY_ENCODINGS_MAX)
3103 default_enc = has_detect ? GEANY_ENCODINGS_MAX : GEANY_ENCODING_NONE;
3105 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
3106 if (encodings_encoding_store_get_iter(store, &iter, default_enc))
3107 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
3108 renderer = gtk_cell_renderer_text_new();
3109 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
3110 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
3111 encodings_encoding_store_cell_data_func, NULL, NULL);
3113 return combo;
3117 gint ui_encodings_combo_box_get_active_encoding(GtkComboBox *combo)
3119 GtkTreeIter iter;
3120 gint enc = GEANY_ENCODING_NONE;
3122 /* there should always be an active iter anyway, but we check just in case */
3123 if (gtk_combo_box_get_active_iter(combo, &iter))
3125 GtkTreeModel *model = gtk_combo_box_get_model(combo);
3126 enc = encodings_encoding_store_get_encoding(GTK_TREE_STORE(model), &iter);
3129 return enc;
3133 gboolean ui_encodings_combo_box_set_active_encoding(GtkComboBox *combo, gint enc)
3135 GtkTreeIter iter;
3136 GtkTreeModel *model = gtk_combo_box_get_model(combo);
3138 if (encodings_encoding_store_get_iter(GTK_TREE_STORE(model), &iter, enc))
3140 gtk_combo_box_set_active_iter(combo, &iter);
3141 return TRUE;
3143 return FALSE;