Merge branch 'callbacks-cleanup'
[geany-mirror.git] / src / ui_utils.c
blobcb60ed2ac0a33db975bfd0abb7fc0b4e2c517c61
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 "encodings.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"
53 #include "gtkcompat.h"
55 #include <string.h>
56 #include <ctype.h>
57 #include <stdarg.h>
58 #include <gdk/gdkkeysyms.h>
61 #define DEFAULT_STATUSBAR_TEMPLATE N_(\
62 "line: %l / %L\t " \
63 "col: %c\t " \
64 "sel: %s\t " \
65 "%w %t %m" \
66 "mode: %M " \
67 "encoding: %e " \
68 "filetype: %f " \
69 "scope: %S")
71 GeanyInterfacePrefs interface_prefs;
72 GeanyMainWidgets main_widgets;
74 UIPrefs ui_prefs;
75 UIWidgets ui_widgets;
77 static GtkBuilder *builder = NULL;
78 static GtkWidget* window1 = NULL;
79 static GtkWidget* toolbar_popup_menu1 = NULL;
80 static GtkWidget* edit_menu1 = NULL;
81 static GtkWidget* prefs_dialog = NULL;
82 static GtkWidget* project_dialog = NULL;
84 static struct
86 /* pointers to widgets only sensitive when there is at least one document, the pointers can
87 * also be GtkAction objects, so check each pointer before using it */
88 GPtrArray *document_buttons;
89 GtkWidget *menu_insert_include_items[2];
90 GtkWidget *popup_goto_items[4];
91 GtkWidget *popup_copy_items[3];
92 GtkWidget *menu_copy_items[3];
93 GtkWidget *redo_items[3];
94 GtkWidget *undo_items[3];
95 GtkWidget *save_buttons[4];
96 GtkWidget *config_files_menu;
98 widgets;
100 enum
102 RECENT_FILE_FILE,
103 RECENT_FILE_PROJECT
106 typedef struct
108 gint type;
109 GQueue *recent_queue;
110 GtkWidget *menubar;
111 GtkWidget *toolbar;
112 void (*activate_cb)(GtkMenuItem *, gpointer);
113 } GeanyRecentFiles;
116 static void update_recent_menu(GeanyRecentFiles *grf);
117 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
118 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
119 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
120 static GtkWidget *progress_bar_create(void);
123 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
124 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
126 if (widget != NULL)
127 gtk_widget_set_sensitive(widget, set);
131 /* allow_override is TRUE if text can be ignored when another message has been set
132 * that didn't use allow_override and has not timed out. */
133 static void set_statusbar(const gchar *text, gboolean allow_override)
135 static guint id = 0;
136 static glong last_time = 0;
137 GTimeVal timeval;
138 const gint GEANY_STATUS_TIMEOUT = 1;
140 if (! interface_prefs.statusbar_visible)
141 return; /* just do nothing if statusbar is not visible */
143 if (id == 0)
144 id = gtk_statusbar_get_context_id(GTK_STATUSBAR(ui_widgets.statusbar), "geany-main");
146 g_get_current_time(&timeval);
148 if (! allow_override)
150 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
151 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
152 last_time = timeval.tv_sec;
154 else
155 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
157 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
158 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
163 /** Displays text on the statusbar.
164 * @param log Whether the message should be recorded in the Status window.
165 * @param format A @c printf -style string. */
166 void ui_set_statusbar(gboolean log, const gchar *format, ...)
168 gchar *string;
169 va_list args;
171 va_start(args, format);
172 string = g_strdup_vprintf(format, args);
173 va_end(args);
175 if (! prefs.suppress_status_messages)
176 set_statusbar(string, FALSE);
178 if (log || prefs.suppress_status_messages)
179 msgwin_status_add("%s", string);
181 g_free(string);
185 /* note: some comments below are for translators */
186 static gchar *create_statusbar_statistics(GeanyDocument *doc,
187 guint line, guint col, guint pos)
189 const gchar *cur_tag;
190 const gchar *fmt;
191 const gchar *expos; /* % expansion position */
192 const gchar sp[] = " ";
193 GString *stats_str;
194 ScintillaObject *sci = doc->editor->sci;
196 if (!EMPTY(ui_prefs.statusbar_template))
197 fmt = ui_prefs.statusbar_template;
198 else
199 fmt = _(DEFAULT_STATUSBAR_TEMPLATE);
201 stats_str = g_string_sized_new(120);
203 while ((expos = strchr(fmt, '%')) != NULL)
205 /* append leading text before % char */
206 g_string_append_len(stats_str, fmt, expos - fmt);
208 switch (*++expos)
210 case 'l':
211 g_string_append_printf(stats_str, "%d", line + 1);
212 break;
213 case 'L':
214 g_string_append_printf(stats_str, "%d",
215 sci_get_line_count(doc->editor->sci));
216 break;
217 case 'c':
218 g_string_append_printf(stats_str, "%d", col);
219 break;
220 case 'C':
221 g_string_append_printf(stats_str, "%d", col + 1);
222 break;
223 case 'p':
224 g_string_append_printf(stats_str, "%u", pos);
225 break;
226 case 's':
228 gint len = sci_get_selected_text_length(sci) - 1;
229 /* check if whole lines are selected */
230 if (!len || sci_get_col_from_position(sci,
231 sci_get_selection_start(sci)) != 0 ||
232 sci_get_col_from_position(sci,
233 sci_get_selection_end(sci)) != 0)
234 g_string_append_printf(stats_str, "%d", len);
235 else /* L = lines */
236 g_string_append_printf(stats_str, _("%dL"),
237 sci_get_lines_selected(doc->editor->sci) - 1);
238 break;
240 case 'w':
241 /* RO = read-only */
242 g_string_append(stats_str, (doc->readonly) ? _("RO ") :
243 /* OVR = overwrite/overtype, INS = insert */
244 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
245 break;
246 case 'r':
247 if (doc->readonly)
249 g_string_append(stats_str, _("RO ")); /* RO = read-only */
250 g_string_append(stats_str, sp + 1);
252 break;
253 case 't':
255 switch (editor_get_indent_prefs(doc->editor)->type)
257 case GEANY_INDENT_TYPE_TABS:
258 g_string_append(stats_str, _("TAB"));
259 break;
260 case GEANY_INDENT_TYPE_SPACES: /* SP = space */
261 g_string_append(stats_str, _("SP"));
262 break;
263 case GEANY_INDENT_TYPE_BOTH: /* T/S = tabs and spaces */
264 g_string_append(stats_str, _("T/S"));
265 break;
267 break;
269 case 'm':
270 if (doc->changed)
272 g_string_append(stats_str, _("MOD")); /* MOD = modified */
273 g_string_append(stats_str, sp);
275 break;
276 case 'M':
277 g_string_append(stats_str, editor_get_eol_char_name(doc->editor));
278 break;
279 case 'e':
280 g_string_append(stats_str,
281 doc->encoding ? doc->encoding : _("unknown"));
282 if (encodings_is_unicode_charset(doc->encoding) && (doc->has_bom))
284 g_string_append_c(stats_str, ' ');
285 g_string_append(stats_str, _("(with BOM)")); /* BOM = byte order mark */
287 break;
288 case 'f':
289 g_string_append(stats_str, filetypes_get_display_name(doc->file_type));
290 break;
291 case 'S':
292 symbols_get_current_scope(doc, &cur_tag);
293 g_string_append(stats_str, cur_tag);
294 break;
295 case 'Y':
296 g_string_append_c(stats_str, ' ');
297 g_string_append_printf(stats_str, "%d",
298 sci_get_style_at(doc->editor->sci, pos));
299 break;
300 default:
301 g_string_append_len(stats_str, expos, 1);
304 /* skip past %c chars */
305 if (*expos)
306 fmt = expos + 1;
307 else
308 break;
310 /* add any remaining text */
311 g_string_append(stats_str, fmt);
313 return g_string_free(stats_str, FALSE);
317 /* updates the status bar document statistics */
318 void ui_update_statusbar(GeanyDocument *doc, gint pos)
320 g_return_if_fail(doc == NULL || doc->is_valid);
322 if (! interface_prefs.statusbar_visible)
323 return; /* just do nothing if statusbar is not visible */
325 if (doc == NULL)
326 doc = document_get_current();
328 if (doc != NULL)
330 guint line, col;
331 gchar *stats_str;
333 if (pos == -1)
334 pos = sci_get_current_position(doc->editor->sci);
335 line = sci_get_line_from_position(doc->editor->sci, pos);
337 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
338 * when current pos is beyond document end (can occur when removing
339 * blocks of selected lines especially esp. brace sections near end of file). */
340 if (pos <= sci_get_length(doc->editor->sci))
341 col = sci_get_col_from_position(doc->editor->sci, pos);
342 else
343 col = 0;
345 stats_str = create_statusbar_statistics(doc, line, col, pos);
347 /* can be overridden by status messages */
348 set_statusbar(stats_str, TRUE);
349 g_free(stats_str);
351 else /* no documents */
353 set_statusbar("", TRUE); /* can be overridden by status messages */
358 /* This sets the window title according to the current filename. */
359 void ui_set_window_title(GeanyDocument *doc)
361 GString *str;
362 GeanyProject *project = app->project;
364 g_return_if_fail(doc == NULL || doc->is_valid);
366 if (doc == NULL)
367 doc = document_get_current();
369 str = g_string_new(NULL);
371 if (doc != NULL)
373 g_string_append(str, doc->changed ? "*" : "");
375 if (doc->file_name == NULL)
376 g_string_append(str, DOC_FILENAME(doc));
377 else
379 gchar *short_name = document_get_basename_for_display(doc, 30);
380 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
382 g_string_append(str, short_name);
383 g_string_append(str, " - ");
384 g_string_append(str, dirname ? dirname : "");
385 g_free(short_name);
386 g_free(dirname);
388 g_string_append(str, " - ");
390 if (project)
392 g_string_append_c(str, '[');
393 g_string_append(str, project->name);
394 g_string_append(str, "] - ");
396 g_string_append(str, "Geany");
397 if (cl_options.new_instance)
399 g_string_append(str, _(" (new instance)"));
401 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
402 g_string_free(str, TRUE);
406 void ui_set_editor_font(const gchar *font_name)
408 guint i;
410 g_return_if_fail(font_name != NULL);
412 /* do nothing if font has not changed */
413 if (interface_prefs.editor_font != NULL)
414 if (strcmp(font_name, interface_prefs.editor_font) == 0)
415 return;
417 g_free(interface_prefs.editor_font);
418 interface_prefs.editor_font = g_strdup(font_name);
420 /* We copy the current style, and update the font in all open tabs. */
421 for (i = 0; i < documents_array->len; i++)
423 if (documents[i]->editor)
425 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
429 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
433 void ui_set_fullscreen(void)
435 if (ui_prefs.fullscreen)
437 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
439 else
441 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
446 void ui_update_popup_reundo_items(GeanyDocument *doc)
448 gboolean enable_undo;
449 gboolean enable_redo;
450 guint i, len;
452 g_return_if_fail(doc == NULL || doc->is_valid);
454 if (doc == NULL)
456 enable_undo = FALSE;
457 enable_redo = FALSE;
459 else
461 enable_undo = document_can_undo(doc);
462 enable_redo = document_can_redo(doc);
465 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
466 len = G_N_ELEMENTS(widgets.undo_items);
467 for (i = 0; i < len; i++)
469 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
471 len = G_N_ELEMENTS(widgets.redo_items);
472 for (i = 0; i < len; i++)
474 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
479 void ui_update_popup_copy_items(GeanyDocument *doc)
481 gboolean enable;
482 guint i, len;
484 g_return_if_fail(doc == NULL || doc->is_valid);
486 if (doc == NULL)
487 enable = FALSE;
488 else
489 enable = sci_has_selection(doc->editor->sci);
491 len = G_N_ELEMENTS(widgets.popup_copy_items);
492 for (i = 0; i < len; i++)
493 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
497 void ui_update_popup_goto_items(gboolean enable)
499 guint i, len;
500 len = G_N_ELEMENTS(widgets.popup_goto_items);
501 for (i = 0; i < len; i++)
502 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
506 void ui_update_menu_copy_items(GeanyDocument *doc)
508 gboolean enable = FALSE;
509 guint i, len;
510 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
512 g_return_if_fail(doc == NULL || doc->is_valid);
514 if (IS_SCINTILLA(focusw))
515 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
516 else
517 if (GTK_IS_EDITABLE(focusw))
518 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
519 else
520 if (GTK_IS_TEXT_VIEW(focusw))
522 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
523 GTK_TEXT_VIEW(focusw));
524 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
527 len = G_N_ELEMENTS(widgets.menu_copy_items);
528 for (i = 0; i < len; i++)
529 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
533 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
535 gboolean enable = FALSE;
537 g_return_if_fail(doc == NULL || doc->is_valid);
539 if (doc == NULL || doc->file_type == NULL)
540 enable = FALSE;
541 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
542 enable = TRUE;
544 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
548 void ui_update_fold_items(void)
550 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
551 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
552 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
556 /* @include include name or NULL for empty with cursor ready for typing it */
557 static void insert_include(GeanyDocument *doc, gint pos, const gchar *include)
559 gint pos_after = -1;
560 gchar *text;
562 g_return_if_fail(doc != NULL);
563 g_return_if_fail(pos == -1 || pos >= 0);
565 if (pos == -1)
566 pos = sci_get_current_position(doc->editor->sci);
568 if (! include)
570 text = g_strdup("#include \"\"\n");
571 pos_after = pos + 10;
573 else
575 text = g_strconcat("#include <", include, ">\n", NULL);
578 sci_start_undo_action(doc->editor->sci);
579 sci_insert_text(doc->editor->sci, pos, text);
580 sci_end_undo_action(doc->editor->sci);
581 g_free(text);
582 if (pos_after >= 0)
583 sci_goto_pos(doc->editor->sci, pos_after, FALSE);
587 static void on_popup_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
589 insert_include(document_get_current(), editor_info.click_pos, user_data);
593 static void on_menu_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
595 insert_include(document_get_current(), -1, user_data);
599 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
601 guint i = 0;
602 GtkWidget *tmp_menu;
603 GtkWidget *tmp_popup;
604 GtkWidget *edit_menu, *edit_menu_item;
605 GtkWidget *popup_menu, *popup_menu_item;
607 edit_menu = gtk_menu_new();
608 popup_menu = gtk_menu_new();
609 edit_menu_item = gtk_menu_item_new_with_label(label);
610 popup_menu_item = gtk_menu_item_new_with_label(label);
611 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
612 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
614 while (includes[i] != NULL)
616 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
617 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
618 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
619 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
620 g_signal_connect(tmp_menu, "activate",
621 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
622 g_signal_connect(tmp_popup, "activate",
623 G_CALLBACK(on_popup_insert_include_activate), (gpointer) includes[i]);
624 i++;
626 gtk_widget_show_all(edit_menu_item);
627 gtk_widget_show_all(popup_menu_item);
628 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
629 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
633 void ui_create_insert_menu_items(void)
635 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
636 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
637 GtkWidget *blank;
638 const gchar *c_includes_stdlib[] = {
639 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
640 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
642 const gchar *c_includes_c99[] = {
643 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
644 "tgmath.h", "wchar.h", "wctype.h", NULL
646 const gchar *c_includes_cpp[] = {
647 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
649 const gchar *c_includes_cppstdlib[] = {
650 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
651 "memory", "locale", NULL
653 const gchar *c_includes_stl[] = {
654 "bitset", "deque", "list", "map", "set", "queue", "stack", "vector", "algorithm",
655 "iterator", "functional", "string", "complex", "valarray", NULL
658 blank = gtk_menu_item_new_with_label("#include \"...\"");
659 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
660 gtk_widget_show(blank);
661 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate), NULL);
662 blank = gtk_separator_menu_item_new ();
663 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
664 gtk_widget_show(blank);
666 blank = gtk_menu_item_new_with_label("#include \"...\"");
667 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
668 gtk_widget_show(blank);
669 g_signal_connect(blank, "activate", G_CALLBACK(on_popup_insert_include_activate), NULL);
670 blank = gtk_separator_menu_item_new();
671 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
672 gtk_widget_show(blank);
674 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
675 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
676 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
677 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
678 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
682 static void insert_date(GeanyDocument *doc, gint pos, const gchar *date_style)
684 const gchar *format = NULL;
685 gchar *time_str;
687 g_return_if_fail(doc != NULL);
688 g_return_if_fail(pos == -1 || pos >= 0);
690 if (pos == -1)
691 pos = sci_get_current_position(doc->editor->sci);
693 /* set default value */
694 if (utils_str_equal("", ui_prefs.custom_date_format))
696 g_free(ui_prefs.custom_date_format);
697 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
700 if (utils_str_equal(_("dd.mm.yyyy"), date_style))
701 format = "%d.%m.%Y";
702 else if (utils_str_equal(_("mm.dd.yyyy"), date_style))
703 format = "%m.%d.%Y";
704 else if (utils_str_equal(_("yyyy/mm/dd"), date_style))
705 format = "%Y/%m/%d";
706 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), date_style))
707 format = "%d.%m.%Y %H:%M:%S";
708 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), date_style))
709 format = "%m.%d.%Y %H:%M:%S";
710 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), date_style))
711 format = "%Y/%m/%d %H:%M:%S";
712 else if (utils_str_equal(_("_Use Custom Date Format"), date_style))
713 format = ui_prefs.custom_date_format;
714 else
716 gchar *str = dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets.window),
717 _("Enter here a custom date and time format. "
718 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
719 ui_prefs.custom_date_format);
720 if (str)
721 SETPTR(ui_prefs.custom_date_format, str);
722 return;
725 time_str = utils_get_date_time(format, NULL);
726 if (time_str != NULL)
728 sci_start_undo_action(doc->editor->sci);
729 sci_insert_text(doc->editor->sci, pos, time_str);
730 sci_goto_pos(doc->editor->sci, pos + strlen(time_str), FALSE);
731 sci_end_undo_action(doc->editor->sci);
732 g_free(time_str);
734 else
736 utils_beep();
737 ui_set_statusbar(TRUE,
738 _("Date format string could not be converted (possibly too long)."));
743 static void on_popup_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
745 insert_date(document_get_current(), editor_info.click_pos, user_data);
749 static void on_menu_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
751 insert_date(document_get_current(), -1, user_data);
755 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
757 GtkWidget *item;
759 item = gtk_menu_item_new_with_mnemonic(label);
760 gtk_container_add(GTK_CONTAINER(me), item);
761 gtk_widget_show(item);
762 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
764 item = gtk_menu_item_new_with_mnemonic(label);
765 gtk_container_add(GTK_CONTAINER(mp), item);
766 gtk_widget_show(item);
767 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), label);
771 void ui_create_insert_date_menu_items(void)
773 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
774 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
775 GtkWidget *item;
776 gchar *str;
778 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
779 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
780 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
782 item = gtk_separator_menu_item_new();
783 gtk_container_add(GTK_CONTAINER(menu_edit), item);
784 gtk_widget_show(item);
785 item = gtk_separator_menu_item_new();
786 gtk_container_add(GTK_CONTAINER(menu_popup), item);
787 gtk_widget_show(item);
789 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
790 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
791 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
793 item = gtk_separator_menu_item_new();
794 gtk_container_add(GTK_CONTAINER(menu_edit), item);
795 gtk_widget_show(item);
796 item = gtk_separator_menu_item_new();
797 gtk_container_add(GTK_CONTAINER(menu_popup), item);
798 gtk_widget_show(item);
800 str = _("_Use Custom Date Format");
801 item = gtk_menu_item_new_with_mnemonic(str);
802 gtk_container_add(GTK_CONTAINER(menu_edit), item);
803 gtk_widget_show(item);
804 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
805 ui_hookup_widget(main_widgets.window, item, "insert_date_custom1");
807 item = gtk_menu_item_new_with_mnemonic(str);
808 gtk_container_add(GTK_CONTAINER(menu_popup), item);
809 gtk_widget_show(item);
810 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), str);
811 ui_hookup_widget(main_widgets.editor_menu, item, "insert_date_custom2");
813 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
817 void ui_save_buttons_toggle(gboolean enable)
819 guint i;
820 gboolean dirty_tabs = FALSE;
822 if (ui_prefs.allow_always_save)
823 enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
825 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
826 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
828 /* save all menu item and tool button */
829 for (i = 0; i < documents_array->len; i++)
831 /* check whether there are files where changes were made and if there are some,
832 * we need the save all button / item */
833 if (documents[i]->is_valid && documents[i]->changed)
835 dirty_tabs = TRUE;
836 break;
840 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
841 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
845 #define add_doc_widget(widget_name) \
846 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
848 #define add_doc_toolitem(widget_name) \
849 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
851 static void init_document_widgets(void)
853 widgets.document_buttons = g_ptr_array_new();
855 /* Cache the document-sensitive widgets so we don't have to keep looking them up
856 * when using ui_document_buttons_update(). */
857 add_doc_widget("menu_close1");
858 add_doc_widget("close_other_documents1");
859 add_doc_widget("menu_change_font1");
860 add_doc_widget("menu_close_all1");
861 add_doc_widget("menu_save1");
862 add_doc_widget("menu_save_all1");
863 add_doc_widget("menu_save_as1");
864 add_doc_widget("menu_count_words1");
865 add_doc_widget("menu_build1");
866 add_doc_widget("add_comments1");
867 add_doc_widget("menu_paste1");
868 add_doc_widget("menu_undo2");
869 add_doc_widget("preferences2");
870 add_doc_widget("menu_reload1");
871 add_doc_widget("menu_document1");
872 add_doc_widget("menu_choose_color1");
873 add_doc_widget("menu_color_schemes");
874 add_doc_widget("menu_markers_margin1");
875 add_doc_widget("menu_linenumber_margin1");
876 add_doc_widget("menu_show_white_space1");
877 add_doc_widget("menu_show_line_endings1");
878 add_doc_widget("menu_show_indentation_guides1");
879 add_doc_widget("menu_zoom_in1");
880 add_doc_widget("menu_zoom_out1");
881 add_doc_widget("normal_size1");
882 add_doc_widget("treeview6");
883 add_doc_widget("print1");
884 add_doc_widget("menu_reload_as1");
885 add_doc_widget("menu_select_all1");
886 add_doc_widget("insert_date1");
887 add_doc_widget("insert_alternative_white_space1");
888 add_doc_widget("menu_format1");
889 add_doc_widget("commands2");
890 add_doc_widget("menu_open_selected_file1");
891 add_doc_widget("page_setup1");
892 add_doc_widget("find1");
893 add_doc_widget("find_next1");
894 add_doc_widget("find_previous1");
895 add_doc_widget("go_to_next_marker1");
896 add_doc_widget("go_to_previous_marker1");
897 add_doc_widget("replace1");
898 add_doc_widget("find_nextsel1");
899 add_doc_widget("find_prevsel1");
900 add_doc_widget("find_usage1");
901 add_doc_widget("find_document_usage1");
902 add_doc_widget("mark_all1");
903 add_doc_widget("go_to_line1");
904 add_doc_widget("goto_tag_definition1");
905 add_doc_widget("goto_tag_declaration1");
906 add_doc_widget("reset_indentation1");
907 add_doc_toolitem("Close");
908 add_doc_toolitem("CloseAll");
909 add_doc_toolitem("Search");
910 add_doc_toolitem("SearchEntry");
911 add_doc_toolitem("ZoomIn");
912 add_doc_toolitem("ZoomOut");
913 add_doc_toolitem("Indent");
914 add_doc_toolitem("UnIndent");
915 add_doc_toolitem("Cut");
916 add_doc_toolitem("Copy");
917 add_doc_toolitem("Paste");
918 add_doc_toolitem("Delete");
919 add_doc_toolitem("Save");
920 add_doc_toolitem("SaveAs");
921 add_doc_toolitem("SaveAll");
922 add_doc_toolitem("Compile");
923 add_doc_toolitem("Run");
924 add_doc_toolitem("Reload");
925 add_doc_toolitem("Color");
926 add_doc_toolitem("Goto");
927 add_doc_toolitem("GotoEntry");
928 add_doc_toolitem("Replace");
929 add_doc_toolitem("Print");
933 void ui_document_buttons_update(void)
935 guint i;
936 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
938 for (i = 0; i < widgets.document_buttons->len; i++)
940 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
941 if (GTK_IS_ACTION(widget))
942 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
943 else
944 ui_widget_set_sensitive(widget, enable);
949 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
951 g_ptr_array_remove_fast(widgets.document_buttons, widget);
955 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
956 * when some documents are present/no documents are open.
957 * It will be removed when the widget is destroyed.
958 * @param widget The widget to add.
960 * @since 0.15
962 void ui_add_document_sensitive(GtkWidget *widget)
964 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
966 ui_widget_set_sensitive(widget, enable);
968 g_ptr_array_add(widgets.document_buttons, widget);
969 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
973 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
975 if (show)
977 gtk_widget_show(widget);
979 else
981 gtk_widget_hide(widget);
986 void ui_sidebar_show_hide(void)
988 GtkWidget *widget;
990 /* check that there are no other notebook pages before hiding the sidebar completely
991 * other pages could be e.g. the file browser plugin */
992 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
993 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
995 ui_prefs.sidebar_visible = FALSE;
998 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
999 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
1001 ignore_callback = TRUE;
1002 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
1003 ignore_callback = FALSE;
1006 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
1008 ui_widget_show_hide(gtk_notebook_get_nth_page(
1009 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
1010 ui_widget_show_hide(gtk_notebook_get_nth_page(
1011 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
1015 void ui_document_show_hide(GeanyDocument *doc)
1017 const gchar *widget_name;
1018 GtkWidget *item;
1019 const GeanyIndentPrefs *iprefs;
1021 g_return_if_fail(doc == NULL || doc->is_valid);
1023 if (doc == NULL)
1024 doc = document_get_current();
1026 if (doc == NULL)
1027 return;
1029 ignore_callback = TRUE;
1031 gtk_check_menu_item_set_active(
1032 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
1033 doc->editor->line_wrapping);
1035 gtk_check_menu_item_set_active(
1036 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
1037 doc->editor->line_breaking);
1039 iprefs = editor_get_indent_prefs(doc->editor);
1041 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
1042 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
1044 switch (iprefs->type)
1046 case GEANY_INDENT_TYPE_SPACES:
1047 widget_name = "spaces1"; break;
1048 case GEANY_INDENT_TYPE_TABS:
1049 widget_name = "tabs1"; break;
1050 case GEANY_INDENT_TYPE_BOTH:
1051 default:
1052 widget_name = "tabs_and_spaces1"; break;
1054 item = ui_lookup_widget(main_widgets.window, widget_name);
1055 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1057 if (iprefs->width >= 1 && iprefs->width <= 8)
1059 gchar *name;
1061 name = g_strdup_printf("indent_width_%d", iprefs->width);
1062 item = ui_lookup_widget(main_widgets.window, name);
1063 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1064 g_free(name);
1067 gtk_check_menu_item_set_active(
1068 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
1069 doc->readonly);
1071 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
1072 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
1073 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
1075 switch (sci_get_eol_mode(doc->editor->sci))
1077 case SC_EOL_CR: widget_name = "cr"; break;
1078 case SC_EOL_LF: widget_name = "lf"; break;
1079 default: widget_name = "crlf"; break;
1081 gtk_check_menu_item_set_active(
1082 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
1084 encodings_select_radio_item(doc->encoding);
1085 filetypes_select_radio_item(doc->file_type);
1087 ignore_callback = FALSE;
1091 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
1093 gtk_widget_set_name(widget, success ? NULL : "geany-search-entry-no-match");
1097 static void recent_create_menu(GeanyRecentFiles *grf)
1099 GtkWidget *tmp;
1100 guint i, len;
1101 gchar *filename;
1103 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
1104 for (i = 0; i < len; i++)
1106 filename = g_queue_peek_nth(grf->recent_queue, i);
1107 /* create menu item for the recent files menu in the menu bar */
1108 tmp = gtk_menu_item_new_with_label(filename);
1109 gtk_widget_show(tmp);
1110 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
1111 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1112 /* create menu item for the recent files menu in the toolbar */
1113 if (grf->toolbar != NULL)
1115 tmp = gtk_menu_item_new_with_label(filename);
1116 gtk_widget_show(tmp);
1117 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1118 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1124 static GeanyRecentFiles *recent_get_recent_files(void)
1126 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
1128 if (G_UNLIKELY(grf.recent_queue == NULL))
1130 grf.recent_queue = ui_prefs.recent_queue;
1131 grf.menubar = ui_widgets.recent_files_menu_menubar;
1132 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1133 toolbar_get_action_by_name("Open")));
1134 grf.activate_cb = recent_file_activate_cb;
1136 return &grf;
1140 static GeanyRecentFiles *recent_get_recent_projects(void)
1142 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
1144 if (G_UNLIKELY(grf.recent_queue == NULL))
1146 grf.recent_queue = ui_prefs.recent_projects_queue;
1147 grf.menubar = ui_widgets.recent_projects_menu_menubar;
1148 grf.toolbar = NULL;
1149 grf.activate_cb = recent_project_activate_cb;
1151 return &grf;
1155 void ui_create_recent_menus(void)
1157 recent_create_menu(recent_get_recent_files());
1158 recent_create_menu(recent_get_recent_projects());
1162 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1164 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1165 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1167 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1168 recent_file_loaded(utf8_filename, recent_get_recent_files());
1170 g_free(locale_filename);
1171 g_free(utf8_filename);
1175 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1177 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1178 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1180 if (project_ask_close() && project_load_file_with_session(locale_filename))
1181 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1183 g_free(locale_filename);
1184 g_free(utf8_filename);
1188 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf,
1189 const GtkRecentData *rdata)
1191 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1194 if (grf->type == RECENT_FILE_FILE && rdata)
1196 GtkRecentManager *manager = gtk_recent_manager_get_default();
1197 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1198 if (uri != NULL)
1200 gtk_recent_manager_add_full(manager, uri, rdata);
1201 g_free(uri);
1205 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1206 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1208 g_free(g_queue_pop_tail(grf->recent_queue));
1210 update_recent_menu(grf);
1212 /* filename already in recent list */
1213 else
1214 recent_file_loaded(utf8_filename, grf);
1218 void ui_add_recent_document(GeanyDocument *doc)
1220 /* what are the groups for actually? */
1221 static const gchar *groups[2] = {
1222 "geany",
1223 NULL
1225 GtkRecentData rdata;
1227 /* Prepare the data for gtk_recent_manager_add_full() */
1228 rdata.display_name = NULL;
1229 rdata.description = NULL;
1230 rdata.mime_type = doc->file_type->mime_type;
1231 /* if we ain't got no mime-type, fallback to plain text */
1232 if (! rdata.mime_type)
1233 rdata.mime_type = (gchar *) "text/plain";
1234 rdata.app_name = (gchar *) "geany";
1235 rdata.app_exec = (gchar *) "geany %u";
1236 rdata.groups = (gchar **) groups;
1237 rdata.is_private = FALSE;
1239 add_recent_file(doc->file_name, recent_get_recent_files(), &rdata);
1243 void ui_add_recent_project_file(const gchar *utf8_filename)
1245 add_recent_file(utf8_filename, recent_get_recent_projects(), NULL);
1249 /* Returns: newly allocated string with the UTF-8 menu text. */
1250 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1252 const gchar *text = NULL;
1254 if (gtk_bin_get_child(GTK_BIN(menu_item)))
1256 GtkWidget *child = gtk_bin_get_child(GTK_BIN(menu_item));
1258 if (GTK_IS_LABEL(child))
1259 text = gtk_label_get_text(GTK_LABEL(child));
1261 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1262 return g_strdup(text);
1266 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1268 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1269 gint result;
1271 if (utils_str_equal(menu_text, user_data))
1272 result = 0;
1273 else
1274 result = 1;
1276 g_free(menu_text);
1277 return result;
1281 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1283 GList *item, *children;
1284 void *data;
1285 GtkWidget *tmp;
1287 /* first reorder the queue */
1288 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1289 g_return_if_fail(item != NULL);
1291 data = item->data;
1292 g_queue_remove(grf->recent_queue, data);
1293 g_queue_push_head(grf->recent_queue, data);
1295 /* remove the old menuitem for the filename */
1296 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1297 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1298 if (item != NULL)
1299 gtk_widget_destroy(GTK_WIDGET(item->data));
1300 g_list_free(children);
1302 if (grf->toolbar != NULL)
1304 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1305 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1306 if (item != NULL)
1307 gtk_widget_destroy(GTK_WIDGET(item->data));
1308 g_list_free(children);
1310 /* now prepend a new menuitem for the filename,
1311 * first for the recent files menu in the menu bar */
1312 tmp = gtk_menu_item_new_with_label(utf8_filename);
1313 gtk_widget_show(tmp);
1314 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1315 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1316 /* then for the recent files menu in the tool bar */
1317 if (grf->toolbar != NULL)
1319 tmp = gtk_menu_item_new_with_label(utf8_filename);
1320 gtk_widget_show(tmp);
1321 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1322 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1323 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal which we need in
1324 * GeanyMenubuttonAction */
1325 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1326 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1331 static void update_recent_menu(GeanyRecentFiles *grf)
1333 GtkWidget *tmp;
1334 gchar *filename;
1335 GList *children, *item;
1337 filename = g_queue_peek_head(grf->recent_queue);
1339 /* clean the MRU list before adding an item (menubar) */
1340 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1341 if (g_list_length(children) > file_prefs.mru_length - 1)
1343 item = g_list_nth(children, file_prefs.mru_length - 1);
1344 while (item != NULL)
1346 if (GTK_IS_MENU_ITEM(item->data))
1347 gtk_widget_destroy(GTK_WIDGET(item->data));
1348 item = g_list_next(item);
1351 g_list_free(children);
1353 /* create item for the menu bar menu */
1354 tmp = gtk_menu_item_new_with_label(filename);
1355 gtk_widget_show(tmp);
1356 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1357 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1359 /* clean the MRU list before adding an item (toolbar) */
1360 if (grf->toolbar != NULL)
1362 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1363 if (g_list_length(children) > file_prefs.mru_length - 1)
1365 item = g_list_nth(children, file_prefs.mru_length - 1);
1366 while (item != NULL)
1368 if (GTK_IS_MENU_ITEM(item->data))
1369 gtk_widget_destroy(GTK_WIDGET(item->data));
1370 item = g_list_next(item);
1373 g_list_free(children);
1375 /* create item for the tool bar menu */
1376 tmp = gtk_menu_item_new_with_label(filename);
1377 gtk_widget_show(tmp);
1378 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1379 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1380 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1385 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1387 guint i;
1389 foreach_document (i)
1391 GeanyDocument *doc = documents[i];
1393 switch (feature)
1395 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1396 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1397 break;
1398 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1399 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin);
1400 break;
1401 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1402 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1403 break;
1404 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1405 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1406 break;
1407 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1408 editor_set_indentation_guides(doc->editor);
1409 break;
1415 void ui_update_view_editor_menu_items(void)
1417 ignore_callback = TRUE;
1418 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1419 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1420 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);
1421 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);
1422 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);
1423 ignore_callback = FALSE;
1427 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1428 * @param label_text The label text.
1429 * @param alignment An address to store the alignment widget pointer.
1430 * @return The frame widget, setting the alignment container for packing child widgets. */
1431 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1433 GtkWidget *label, *align;
1434 GtkWidget *frame = gtk_frame_new(NULL);
1436 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1438 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1439 gtk_container_add(GTK_CONTAINER(frame), align);
1440 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1442 label = ui_label_new_bold(label_text);
1443 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1445 *alignment = align;
1446 return frame;
1450 /** Makes a fixed border for dialogs without increasing the button box border.
1451 * @param dialog The parent container for the @c GtkVBox.
1452 * @return The packed @c GtkVBox. */
1453 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1455 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1457 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1458 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox, TRUE, TRUE, 0);
1459 return vbox;
1463 static GtkWidget *dialog_get_widget_for_response(GtkDialog *dialog, gint response_id)
1465 #if GTK_CHECK_VERSION(2, 20, 0)
1466 return gtk_dialog_get_widget_for_response(dialog, response_id);
1467 #else /* GTK < 2.20 */
1468 /* base logic stolen from GTK */
1469 GtkWidget *action_area = gtk_dialog_get_action_area(dialog);
1470 GtkWidget *widget = NULL;
1471 GList *children, *node;
1473 children = gtk_container_get_children(GTK_CONTAINER(action_area));
1474 for (node = children; node && ! widget; node = node->next)
1476 if (gtk_dialog_get_response_for_widget(dialog, node->data) == response_id)
1477 widget = node->data;
1479 g_list_free(children);
1481 return widget;
1482 #endif
1486 /* Reorders a dialog's buttons
1487 * @param dialog A dialog
1488 * @param response First response ID to reorder
1489 * @param ... more response IDs, terminated by -1
1491 * Like gtk_dialog_set_alternative_button_order(), but reorders the default
1492 * buttons layout, not the alternative one. This is useful if you e.g. added a
1493 * button to a dialog which already had some and need yours not to be on the
1494 * end.
1496 /* Heavily based on gtk_dialog_set_alternative_button_order().
1497 * This relies on the action area to be a GtkBox, but although not documented
1498 * the API expose it to be a GtkHButtonBox though GtkBuilder, so it should be
1499 * fine */
1500 void ui_dialog_set_primary_button_order(GtkDialog *dialog, gint response, ...)
1502 va_list ap;
1503 GtkWidget *action_area = gtk_dialog_get_action_area(dialog);
1504 gint position;
1506 va_start(ap, response);
1507 for (position = 0; response != -1; position++)
1509 GtkWidget *child = dialog_get_widget_for_response(dialog, response);
1510 if (child)
1511 gtk_box_reorder_child(GTK_BOX(action_area), child, position);
1512 else
1513 g_warning("%s: no child button with response id %d.", G_STRFUNC, response);
1515 response = va_arg(ap, gint);
1517 va_end(ap);
1521 /** Creates a @c GtkButton with custom text and a stock image similar to
1522 * @c gtk_button_new_from_stock().
1523 * @param stock_id A @c GTK_STOCK_NAME string.
1524 * @param text Button label text, can include mnemonics.
1525 * @return The new @c GtkButton.
1527 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1529 GtkWidget *image, *button;
1531 button = gtk_button_new_with_mnemonic(text);
1532 gtk_widget_show(button);
1533 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1534 gtk_button_set_image(GTK_BUTTON(button), image);
1535 /* note: image is shown by gtk */
1536 return button;
1540 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1541 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1542 * @param label Menu item label, can include mnemonics.
1543 * @return The new @c GtkImageMenuItem.
1545 * @since 0.16
1547 GtkWidget *
1548 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1550 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1551 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1553 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1554 gtk_widget_show(image);
1555 return item;
1559 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1560 GdkEvent *event, gpointer data)
1562 if (event->button.button == 1 && icon_pos == 1)
1564 gtk_entry_set_text(entry, "");
1565 gtk_widget_grab_focus(GTK_WIDGET(entry));
1570 /** Adds a small clear icon to the right end of the passed @a entry.
1571 * A callback to clear the contents of the GtkEntry is automatically added.
1573 * @param entry The GtkEntry object to which the icon should be attached.
1575 * @since 0.16
1577 void ui_entry_add_clear_icon(GtkEntry *entry)
1579 g_object_set(entry, "secondary-icon-stock", GTK_STOCK_CLEAR,
1580 "secondary-icon-activatable", TRUE, NULL);
1581 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1585 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1586 void ui_entry_add_activate_backward_signal(GtkEntry *entry)
1588 static gboolean installed = FALSE;
1590 g_return_if_fail(GTK_IS_ENTRY(entry));
1592 if (G_UNLIKELY(! installed))
1594 GtkBindingSet *binding_set;
1596 installed = TRUE;
1598 /* try to handle the unexpected case where GTK would already have installed the signal */
1599 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry)))
1601 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1602 return;
1605 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry),
1606 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 0, NULL, NULL,
1607 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1608 binding_set = gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry));
1609 gtk_binding_entry_add_signal(binding_set, GDK_Return, GDK_SHIFT_MASK, "activate-backward", 0);
1614 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1616 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1617 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1621 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1622 * the width of each button box's children.
1623 * Should be called after all child widgets have been packed. */
1624 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1626 GtkSizeGroup *size_group;
1628 gtk_box_set_spacing(GTK_BOX(copy), 10);
1629 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1631 /* now we need to put the widest widget from each button box in a size group,
1632 * but we don't know the width before they are drawn, and for different label
1633 * translations the widest widget can vary, so we just add all widgets. */
1634 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1635 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1636 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1637 g_object_unref(size_group);
1641 static gboolean tree_model_find_text(GtkTreeModel *model,
1642 GtkTreeIter *iter, gint column, const gchar *text)
1644 gchar *combo_text;
1645 gboolean found = FALSE;
1647 if (gtk_tree_model_get_iter_first(model, iter))
1651 gtk_tree_model_get(model, iter, 0, &combo_text, -1);
1652 found = utils_str_equal(combo_text, text);
1653 g_free(combo_text);
1655 if (found)
1656 return TRUE;
1658 while (gtk_tree_model_iter_next(model, iter));
1660 return FALSE;
1664 /** Prepends @a text to the drop down list, removing a duplicate element in
1665 * the list if found. Also ensures there are <= @a history_len elements.
1666 * @param combo_entry .
1667 * @param text Text to add, or @c NULL for current entry text.
1668 * @param history_len Max number of items, or @c 0 for default. */
1669 void ui_combo_box_add_to_history(GtkComboBoxText *combo_entry,
1670 const gchar *text, gint history_len)
1672 GtkComboBox *combo = GTK_COMBO_BOX(combo_entry);
1673 GtkTreeModel *model;
1674 GtkTreeIter iter;
1675 GtkTreePath *path;
1677 if (history_len <= 0)
1678 history_len = 10;
1679 if (!text)
1680 text = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
1682 model = gtk_combo_box_get_model(combo);
1684 if (tree_model_find_text(model, &iter, 0, text))
1686 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1688 gtk_combo_box_text_prepend_text(combo_entry, text);
1690 /* limit history */
1691 path = gtk_tree_path_new_from_indices(history_len, -1);
1692 if (gtk_tree_model_get_iter(model, &iter, path))
1694 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1696 gtk_tree_path_free(path);
1700 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1701 * exists in the combo's model. */
1702 void ui_combo_box_prepend_text_once(GtkComboBoxText *combo, const gchar *text)
1704 GtkTreeModel *model;
1705 GtkTreeIter iter;
1707 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
1708 if (tree_model_find_text(model, &iter, 0, text))
1709 return; /* don't prepend duplicate */
1711 gtk_combo_box_text_prepend_text(combo, text);
1715 /* Changes the color of the notebook tab text and open files items according to
1716 * document status. */
1717 void ui_update_tab_status(GeanyDocument *doc)
1719 gtk_widget_set_name(doc->priv->tab_label, document_get_status_widget_class(doc));
1721 sidebar_openfiles_update(doc);
1725 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1726 gboolean down)
1728 GtkTreePath *path;
1729 gboolean result;
1731 if (down)
1732 return gtk_tree_model_iter_next(model, iter);
1734 path = gtk_tree_model_get_path(model, iter);
1735 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1736 gtk_tree_path_free(path);
1737 return result;
1741 /* note: the while loop might be more efficient when searching upwards if it
1742 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1743 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1745 GtkTreeSelection *treesel;
1746 GtkTreeIter iter;
1747 GtkTreeModel *model;
1749 treesel = gtk_tree_view_get_selection(treeview);
1750 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1752 /* get the next selected item */
1753 if (! tree_model_iter_get_next(model, &iter, down))
1754 return FALSE; /* no more items */
1756 else /* no selection */
1758 if (! gtk_tree_model_get_iter_first(model, &iter))
1759 return TRUE; /* no items */
1761 while (TRUE)
1763 gtk_tree_selection_select_iter(treesel, &iter);
1764 if (cb(FALSE))
1765 break; /* found next message */
1767 if (! tree_model_iter_get_next(model, &iter, down))
1768 return FALSE; /* no more items */
1770 /* scroll item in view */
1771 if (ui_prefs.msgwindow_visible)
1773 GtkTreePath *path = gtk_tree_model_get_path(
1774 gtk_tree_view_get_model(treeview), &iter);
1776 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1777 gtk_tree_path_free(path);
1779 return TRUE;
1783 /* Returns FALSE if the treeview has items but no matching next item. */
1784 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1786 return tree_view_find(treeview, cb, TRUE);
1790 /* Returns FALSE if the treeview has items but no matching next item. */
1791 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1793 return tree_view_find(treeview, cb, FALSE);
1798 * Modifies the font of a widget using gtk_widget_modify_font().
1800 * @param widget The widget.
1801 * @param str The font name as expected by pango_font_description_from_string().
1803 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1805 PangoFontDescription *pfd;
1807 pfd = pango_font_description_from_string(str);
1808 gtk_widget_modify_font(widget, pfd);
1809 pango_font_description_free(pfd);
1813 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1814 * file chooser, replacing entry text (if successful) with the path returned from the
1815 * @c GtkFileChooser.
1816 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1817 * @param title The file chooser dialog title, or @c NULL.
1818 * @param action The mode of the file chooser.
1819 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1820 * such as @c GtkComboBoxEntry.
1821 * @return The @c GtkHBox.
1823 /* @see ui_setup_open_button_callback(). */
1824 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1826 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1828 hbox = gtk_hbox_new(FALSE, 6);
1829 path_entry = GTK_WIDGET(entry);
1831 /* prevent path_entry being vertically stretched to the height of dirbtn */
1832 vbox = gtk_vbox_new(FALSE, 0);
1833 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1835 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1837 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1839 else
1840 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1842 dirbtn = gtk_button_new();
1843 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1844 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1845 ui_setup_open_button_callback(dirbtn, title, action, entry);
1847 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1848 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1849 return hbox;
1853 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1856 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1857 * title can be NULL.
1858 * action is the file chooser mode to use. */
1859 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1860 GtkFileChooserAction action, GtkEntry *entry)
1862 GtkWidget *path_entry = GTK_WIDGET(entry);
1864 if (title)
1865 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1866 (GDestroyNotify) g_free);
1867 g_object_set_data(G_OBJECT(open_btn), "action", GINT_TO_POINTER(action));
1868 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), path_entry);
1872 #ifndef G_OS_WIN32
1873 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1874 const gchar *utf8_path)
1876 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1877 GTK_WINDOW(main_widgets.window), action,
1878 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1879 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1880 gchar *locale_path;
1881 gchar *ret_path = NULL;
1883 gtk_widget_set_name(dialog, "GeanyDialog");
1884 locale_path = utils_get_locale_from_utf8(utf8_path);
1885 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1887 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1888 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1890 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1892 if (g_path_is_absolute(locale_path))
1893 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1895 g_free(locale_path);
1897 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1899 gchar *dir_locale;
1901 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1902 ret_path = utils_get_utf8_from_locale(dir_locale);
1903 g_free(dir_locale);
1905 gtk_widget_destroy(dialog);
1906 return ret_path;
1908 #endif
1911 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1913 GtkFileChooserAction action = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "action"));
1914 GtkEntry *entry = user_data;
1915 const gchar *title = g_object_get_data(G_OBJECT(button), "title");
1916 gchar *utf8_path = NULL;
1918 /* TODO: extend for other actions */
1919 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1920 action == GTK_FILE_CHOOSER_ACTION_OPEN);
1922 if (title == NULL)
1923 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1924 _("Select Folder") : _("Select File");
1926 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1928 #ifdef G_OS_WIN32
1929 utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
1930 gtk_entry_get_text(GTK_ENTRY(entry)));
1931 #else
1932 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1933 #endif
1935 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1937 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
1938 #ifdef G_OS_WIN32
1939 utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
1940 gtk_entry_get_text(GTK_ENTRY(entry)));
1941 #else
1942 utf8_path = run_file_chooser(title, action, path);
1943 #endif
1944 g_free(path);
1947 if (utf8_path != NULL)
1949 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
1950 g_free(utf8_path);
1955 void ui_statusbar_showhide(gboolean state)
1957 /* handle statusbar visibility */
1958 if (state)
1960 gtk_widget_show(ui_widgets.statusbar);
1961 ui_update_statusbar(NULL, -1);
1963 else
1964 gtk_widget_hide(ui_widgets.statusbar);
1968 /** Packs all @c GtkWidgets passed after the row argument into a table, using
1969 * one widget per cell. The first widget is not expanded as the table grows,
1970 * as this is usually a label.
1971 * @param table
1972 * @param row The row number of the table.
1974 void ui_table_add_row(GtkTable *table, gint row, ...)
1976 va_list args;
1977 guint i;
1978 GtkWidget *widget;
1980 va_start(args, row);
1981 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
1983 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
1985 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
1986 options, 0, 0, 0);
1988 va_end(args);
1992 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
1994 const gchar *file_name = user_data;
1995 GeanyFiletype *ft = NULL;
1997 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
1998 ft = filetypes[GEANY_FILETYPES_CONF];
2000 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
2001 document_open_file(file_name, FALSE, ft, NULL);
2002 else
2004 gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
2005 gchar *base_name = g_path_get_basename(file_name);
2006 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
2007 gchar *global_content = NULL;
2009 /* if the requested file doesn't exist in the user's config dir, try loading the file
2010 * from the global data directory and use its contents for the newly created file */
2011 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
2012 g_file_get_contents(global_file, &global_content, NULL, NULL);
2014 document_new_file(utf8_filename, ft, global_content);
2016 utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
2021 static void free_on_closure_notify(gpointer data, GClosure *closure)
2023 g_free(data);
2027 /* @note You should connect to the "document-save" signal yourself to detect
2028 * if the user has just saved the config file, reloading it. */
2029 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
2031 GtkWidget *item;
2033 if (!parent)
2034 parent = GTK_CONTAINER(widgets.config_files_menu);
2036 if (!label)
2038 gchar *base_name;
2040 base_name = g_path_get_basename(real_path);
2041 item = gtk_menu_item_new_with_label(base_name);
2042 g_free(base_name);
2044 else
2045 item = gtk_menu_item_new_with_mnemonic(label);
2047 gtk_widget_show(item);
2048 gtk_container_add(parent, item);
2049 g_signal_connect_data(item, "activate", G_CALLBACK(on_config_file_clicked),
2050 g_strdup(real_path), free_on_closure_notify, 0);
2054 static gboolean sort_menu(gpointer data)
2056 ui_menu_sort_by_label(GTK_MENU(data));
2057 return FALSE;
2061 static void create_config_files_menu(void)
2063 GtkWidget *menu, *item;
2065 widgets.config_files_menu = menu = gtk_menu_new();
2067 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
2068 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
2070 /* sort menu after all items added */
2071 g_idle_add(sort_menu, widgets.config_files_menu);
2075 /* adds factory icons with a named icon source using the stock items id */
2076 static void add_stock_icons(const GtkStockItem *items, gsize count)
2078 GtkIconFactory *factory = gtk_icon_factory_new();
2079 GtkIconSource *source = gtk_icon_source_new();
2080 gsize i;
2082 for (i = 0; i < count; i++)
2084 GtkIconSet *set = gtk_icon_set_new();
2086 gtk_icon_source_set_icon_name(source, items[i].stock_id);
2087 gtk_icon_set_add_source(set, source);
2088 gtk_icon_factory_add(factory, items[i].stock_id, set);
2089 gtk_icon_set_unref(set);
2091 gtk_icon_source_free(source);
2092 gtk_icon_factory_add_default(factory);
2093 g_object_unref(factory);
2097 void ui_init_stock_items(void)
2099 GtkStockItem items[] =
2101 { GEANY_STOCK_SAVE_ALL, N_("Save All"), 0, 0, GETTEXT_PACKAGE },
2102 { GEANY_STOCK_CLOSE_ALL, N_("Close All"), 0, 0, GETTEXT_PACKAGE },
2103 { GEANY_STOCK_BUILD, N_("Build"), 0, 0, GETTEXT_PACKAGE }
2106 gtk_stock_add(items, G_N_ELEMENTS(items));
2107 add_stock_icons(items, G_N_ELEMENTS(items));
2111 void ui_init_toolbar_widgets(void)
2113 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
2114 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
2115 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
2116 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
2120 void ui_swap_sidebar_pos(void)
2122 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
2123 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
2124 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
2126 g_object_ref(left);
2127 g_object_ref(right);
2128 gtk_container_remove (GTK_CONTAINER (pane), left);
2129 gtk_container_remove (GTK_CONTAINER (pane), right);
2130 /* only scintilla notebook should expand */
2131 gtk_paned_pack1(GTK_PANED(pane), right, right == main_widgets.notebook, TRUE);
2132 gtk_paned_pack2(GTK_PANED(pane), left, left == main_widgets.notebook, TRUE);
2133 g_object_unref(left);
2134 g_object_unref(right);
2136 gtk_paned_set_position(GTK_PANED(pane), gtk_widget_get_allocated_width(pane)
2137 - gtk_paned_get_position(GTK_PANED(pane)));
2141 static void init_recent_files(void)
2143 GtkWidget *toolbar_recent_files_menu;
2145 /* add recent files to the File menu */
2146 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
2147 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
2148 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
2149 ui_widgets.recent_files_menu_menubar);
2151 /* add recent files to the toolbar Open button */
2152 toolbar_recent_files_menu = gtk_menu_new();
2153 g_object_ref(toolbar_recent_files_menu);
2154 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
2155 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
2159 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
2161 g_object_ref(menu);
2162 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
2163 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
2164 g_object_unref(menu);
2168 typedef struct GeanySharedMenu
2170 const gchar *menu;
2171 const gchar *menubar_item;
2172 const gchar *popup_item;
2174 GeanySharedMenu;
2176 #define foreach_menu(item, array) \
2177 for (item = array; item->menu; item++)
2179 static void on_editor_menu_show(GtkWidget *widget, GeanySharedMenu *items)
2181 GeanySharedMenu *item;
2183 foreach_menu(item, items)
2185 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2186 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2187 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2189 ui_menu_move(menu, bar, popup);
2194 static void on_editor_menu_hide(GtkWidget *widget, GeanySharedMenu *items)
2196 GeanySharedMenu *item;
2198 foreach_menu(item, items)
2200 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2201 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2202 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2204 ui_menu_move(menu, popup, bar);
2209 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2210 * so this is called after that's done. */
2211 void ui_init_prefs(void)
2213 StashGroup *group = stash_group_new(PACKAGE);
2215 /* various prefs */
2216 configuration_add_various_pref_group(group);
2218 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
2219 "show_symbol_list_expanders", TRUE);
2220 stash_group_add_boolean(group, &interface_prefs.compiler_tab_autoscroll,
2221 "compiler_tab_autoscroll", TRUE);
2222 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
2223 "allow_always_save", FALSE);
2224 stash_group_add_string(group, &ui_prefs.statusbar_template,
2225 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE));
2226 stash_group_add_boolean(group, &ui_prefs.new_document_after_close,
2227 "new_document_after_close", FALSE);
2228 stash_group_add_boolean(group, &interface_prefs.msgwin_status_visible,
2229 "msgwin_status_visible", TRUE);
2230 stash_group_add_boolean(group, &interface_prefs.msgwin_compiler_visible,
2231 "msgwin_compiler_visible", TRUE);
2232 stash_group_add_boolean(group, &interface_prefs.msgwin_messages_visible,
2233 "msgwin_messages_visible", TRUE);
2234 stash_group_add_boolean(group, &interface_prefs.msgwin_scribble_visible,
2235 "msgwin_scribble_visible", TRUE);
2239 /* Used to find out the name of the GtkBuilder retrieved object since
2240 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2241 * 'name' property for that and those that don't implement GtkBuildable
2242 * will have a "gtk-builder-name" stored in the GObject's data list. */
2243 static const gchar *ui_guess_object_name(GObject *obj)
2245 const gchar *name = NULL;
2247 g_return_val_if_fail(G_IS_OBJECT(obj), NULL);
2249 if (GTK_IS_BUILDABLE(obj))
2250 name = gtk_buildable_get_name(GTK_BUILDABLE(obj));
2251 if (! name)
2252 name = g_object_get_data(obj, "gtk-builder-name");
2253 if (! name)
2254 return NULL;
2256 return name;
2260 /* Compatibility functions */
2261 GtkWidget *create_edit_menu1(void)
2263 return edit_menu1;
2267 GtkWidget *create_prefs_dialog(void)
2269 return prefs_dialog;
2273 GtkWidget *create_project_dialog(void)
2275 return project_dialog;
2279 GtkWidget *create_toolbar_popup_menu1(void)
2281 return toolbar_popup_menu1;
2285 GtkWidget *create_window1(void)
2287 return window1;
2291 static GtkWidget *ui_get_top_parent(GtkWidget *widget)
2293 GtkWidget *parent;
2295 g_return_val_if_fail(GTK_IS_WIDGET(widget), NULL);
2297 for (;;)
2299 if (GTK_IS_MENU(widget))
2300 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2301 else
2302 parent = gtk_widget_get_parent(widget);
2303 if (parent == NULL)
2304 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2305 if (parent == NULL)
2306 break;
2307 widget = parent;
2310 return widget;
2314 void ui_init_builder(void)
2316 gchar *interface_file;
2317 const gchar *name;
2318 GError *error;
2319 GSList *iter, *all_objects;
2320 GtkWidget *widget, *toplevel;
2322 /* prevent function from being called twice */
2323 if (GTK_IS_BUILDER(builder))
2324 return;
2326 builder = gtk_builder_new();
2328 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
2330 error = NULL;
2331 interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
2332 if (! gtk_builder_add_from_file(builder, interface_file, &error))
2334 /* Show the user this message so they know WTF happened */
2335 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
2336 _("Geany cannot start!"), error->message);
2337 /* Aborts */
2338 g_error("Cannot create user-interface: %s", error->message);
2339 g_error_free(error);
2340 g_free(interface_file);
2341 g_object_unref(builder);
2342 return;
2344 g_free(interface_file);
2346 gtk_builder_connect_signals(builder, NULL);
2348 edit_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "edit_menu1"));
2349 prefs_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "prefs_dialog"));
2350 project_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "project_dialog"));
2351 toolbar_popup_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar_popup_menu1"));
2352 window1 = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
2354 g_object_set_data(G_OBJECT(edit_menu1), "edit_menu1", edit_menu1);
2355 g_object_set_data(G_OBJECT(prefs_dialog), "prefs_dialog", prefs_dialog);
2356 g_object_set_data(G_OBJECT(project_dialog), "project_dialog", project_dialog);
2357 g_object_set_data(G_OBJECT(toolbar_popup_menu1), "toolbar_popup_menu1", toolbar_popup_menu1);
2358 g_object_set_data(G_OBJECT(window1), "window1", window1);
2360 all_objects = gtk_builder_get_objects(builder);
2361 for (iter = all_objects; iter != NULL; iter = g_slist_next(iter))
2363 if (! GTK_IS_WIDGET(iter->data))
2364 continue;
2366 widget = GTK_WIDGET(iter->data);
2368 name = ui_guess_object_name(G_OBJECT(widget));
2369 if (! name)
2371 g_warning("Unable to get name from GtkBuilder object");
2372 continue;
2375 toplevel = ui_get_top_parent(widget);
2376 if (toplevel)
2377 ui_hookup_widget(toplevel, widget, name);
2379 g_slist_free(all_objects);
2383 static void init_custom_style(void)
2385 #if GTK_CHECK_VERSION(3, 0, 0)
2386 gchar *css_file = g_build_filename(app->datadir, "geany.css", NULL);
2387 GtkCssProvider *css = gtk_css_provider_new();
2388 GError *error = NULL;
2390 if (! gtk_css_provider_load_from_path(css, css_file, &error))
2392 g_warning("Failed to load custom CSS: %s", error->message);
2393 g_error_free(error);
2395 else
2397 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2398 GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2401 g_object_unref(css);
2402 g_free(css_file);
2403 #else
2404 /* see setup_gtk2_styles() in main.c */
2405 #endif
2409 void ui_init(void)
2411 init_custom_style();
2413 init_recent_files();
2415 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
2416 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
2418 main_widgets.progressbar = progress_bar_create();
2420 /* current word sensitive items */
2421 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition2");
2422 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "context_action1");
2423 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage2");
2424 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage2");
2426 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
2427 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
2428 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
2429 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
2430 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
2431 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
2432 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
2433 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
2434 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
2435 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
2436 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
2437 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
2438 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
2439 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
2441 /* reparent context submenus as needed */
2443 GeanySharedMenu arr[] = {
2444 {"commands2_menu", "commands2", "commands1"},
2445 {"menu_format1_menu", "menu_format1", "menu_format2"},
2446 {"more1_menu", "more1", "search2"},
2447 {NULL, NULL, NULL}
2449 static GeanySharedMenu items[G_N_ELEMENTS(arr)];
2451 memcpy(items, arr, sizeof(arr));
2452 g_signal_connect(main_widgets.editor_menu, "show", G_CALLBACK(on_editor_menu_show), items);
2453 g_signal_connect(main_widgets.editor_menu, "hide", G_CALLBACK(on_editor_menu_hide), items);
2456 ui_init_toolbar_widgets();
2457 init_document_widgets();
2458 create_config_files_menu();
2462 void ui_finalize_builder(void)
2464 if (GTK_IS_BUILDER(builder))
2465 g_object_unref(builder);
2467 /* cleanup refs lingering even after GtkBuilder is destroyed */
2468 if (GTK_IS_WIDGET(edit_menu1))
2469 gtk_widget_destroy(edit_menu1);
2470 if (GTK_IS_WIDGET(prefs_dialog))
2471 gtk_widget_destroy(prefs_dialog);
2472 if (GTK_IS_WIDGET(project_dialog))
2473 gtk_widget_destroy(project_dialog);
2474 if (GTK_IS_WIDGET(toolbar_popup_menu1))
2475 gtk_widget_destroy(toolbar_popup_menu1);
2476 if (GTK_IS_WIDGET(window1))
2477 gtk_widget_destroy(window1);
2481 static void auto_separator_update(GeanyAutoSeparator *autosep)
2483 g_return_if_fail(autosep->item_count >= 0);
2485 if (autosep->widget)
2487 if (autosep->item_count > 0)
2488 ui_widget_show_hide(autosep->widget, autosep->show_count > 0);
2489 else
2490 gtk_widget_destroy(autosep->widget);
2495 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
2497 GeanyAutoSeparator *autosep = user_data;
2499 if (gtk_widget_get_visible(widget))
2500 autosep->show_count++;
2501 else
2502 autosep->show_count--;
2503 auto_separator_update(autosep);
2507 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
2509 GeanyAutoSeparator *autosep = user_data;
2511 autosep->item_count--;
2512 autosep->item_count = MAX(autosep->item_count, 0);
2513 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2514 * so assume widget was visible */
2515 autosep->show_count--;
2516 autosep->show_count = MAX(autosep->item_count, 0);
2517 auto_separator_update(autosep);
2521 /* Show the separator widget if @a item or another is visible. */
2522 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2523 * property, and using reference counting to keep the widget alive whilst its visible group
2524 * is alive. */
2525 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2527 /* set widget ptr NULL when widget destroyed */
2528 if (autosep->item_count == 0)
2529 g_signal_connect(autosep->widget, "destroy",
2530 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2532 if (gtk_widget_get_visible(item))
2533 autosep->show_count++;
2535 autosep->item_count++;
2536 auto_separator_update(autosep);
2538 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2539 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2540 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2545 * Sets @a text as the contents of the tooltip for @a widget.
2547 * @param widget The widget the tooltip should be set for.
2548 * @param text The text for the tooltip.
2550 * @since 0.16
2551 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2553 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2555 gtk_widget_set_tooltip_text(widget, text);
2559 /** Returns a widget from a name in a component, usually created by Glade.
2560 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2561 * or alternatively any widget in the component, and the name of the widget
2562 * you want returned.
2563 * @param widget Widget with the @a widget_name property set.
2564 * @param widget_name Name to lookup.
2565 * @return The widget found.
2566 * @see ui_hookup_widget().
2568 * @since 0.16
2570 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2572 GtkWidget *parent, *found_widget;
2574 g_return_val_if_fail(widget != NULL, NULL);
2575 g_return_val_if_fail(widget_name != NULL, NULL);
2577 for (;;)
2579 if (GTK_IS_MENU(widget))
2580 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2581 else
2582 parent = gtk_widget_get_parent(widget);
2583 if (parent == NULL)
2584 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2585 if (parent == NULL)
2586 break;
2587 widget = parent;
2590 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2591 if (G_UNLIKELY(found_widget == NULL))
2592 g_warning("Widget not found: %s", widget_name);
2593 return found_widget;
2597 /* wraps gtk_builder_get_object()
2598 * unlike ui_lookup_widget(), it does only support getting object created from the main
2599 * UI file, but it can fetch any object, not only widgets */
2600 gpointer ui_builder_get_object (const gchar *name)
2602 return gtk_builder_get_object (builder, name);
2606 /* Progress Bar */
2607 static guint progress_bar_timer_id = 0;
2610 static GtkWidget *progress_bar_create(void)
2612 GtkWidget *bar = gtk_progress_bar_new();
2614 /* Set the progressbar's height to 1 to fit it in the statusbar */
2615 gtk_widget_set_size_request(bar, -1, 1);
2616 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2618 return bar;
2622 static gboolean progress_bar_pulse(gpointer data)
2624 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2626 return TRUE;
2631 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2632 * (if the statusbar is visible). This is a convenience function which adds a timer to
2633 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2634 * You can use this function when you have time consuming asynchronous operation and want to
2635 * display some activity in the GUI and when you don't know about detailed progress steps.
2636 * The progressbar widget is hidden by default when it is not active. This function and
2637 * ui_progress_bar_stop() will show and hide it automatically for you.
2639 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2640 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2641 * In this case, you need to show and hide the widget yourself. You can find some example code
2642 * in @c src/printing.c.
2644 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2646 * @since 0.16
2648 void ui_progress_bar_start(const gchar *text)
2650 g_return_if_fail(progress_bar_timer_id == 0);
2652 if (! interface_prefs.statusbar_visible)
2653 return;
2655 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2657 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2659 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2663 /** Stops a running progress bar and hides the widget again.
2665 * @since 0.16
2667 void ui_progress_bar_stop(void)
2669 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2671 if (progress_bar_timer_id != 0)
2673 g_source_remove(progress_bar_timer_id);
2674 progress_bar_timer_id = 0;
2679 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2681 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2682 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2683 gchar *sa, *sb;
2684 gint result;
2686 sa = ui_menu_item_get_text(item_a);
2687 sb = ui_menu_item_get_text(item_b);
2688 result = utils_str_casecmp(sa, sb);
2689 g_free(sa);
2690 g_free(sb);
2691 return result;
2695 /* Currently @a menu should contain only GtkMenuItems with labels. */
2696 void ui_menu_sort_by_label(GtkMenu *menu)
2698 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2699 GList *node;
2700 gint pos;
2702 list = g_list_sort(list, compare_menu_item_labels);
2703 pos = 0;
2704 foreach_list(node, list)
2706 gtk_menu_reorder_child(menu, node->data, pos);
2707 pos++;
2709 g_list_free(list);
2713 void ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2715 va_list a;
2716 gchar *text;
2718 va_start(a, format);
2719 text = g_strdup_vprintf(format, a);
2720 va_end(a);
2722 gtk_label_set_text(label, text);
2723 gtk_label_set_use_markup(label, TRUE);
2724 g_free(text);
2728 GtkWidget *ui_label_new_bold(const gchar *text)
2730 GtkWidget *label;
2731 gchar *label_text;
2733 label_text = g_markup_escape_text(text, -1);
2734 label = gtk_label_new(NULL);
2735 ui_label_set_markup(GTK_LABEL(label), "<b>%s</b>", label_text);
2736 g_free(label_text);
2737 return label;
2741 /** Adds a list of document items to @a menu.
2742 * @param menu Menu.
2743 * @param active Which document to highlight, or @c NULL.
2744 * @param callback is used for each menu item's @c "activate" signal and will be passed
2745 * the corresponding document pointer as @c user_data.
2746 * @warning You should check @c doc->is_valid in the callback.
2747 * @since 0.19 */
2748 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2750 ui_menu_add_document_items_sorted(menu, active, callback, NULL);
2754 /** Adds a list of document items to @a menu.
2756 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2757 * the order of the document tabs is used.
2759 * See document_compare_by_display_name() for an example sort function.
2761 * @param menu Menu.
2762 * @param active Which document to highlight, or @c NULL.
2763 * @param callback is used for each menu item's @c "activate" signal and will be passed
2764 * the corresponding document pointer as @c user_data.
2765 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2766 * @warning You should check @c doc->is_valid in the callback.
2767 * @since 0.21 */
2768 void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
2769 GCallback callback, GCompareFunc compare_func)
2771 GtkWidget *menu_item, *menu_item_label, *image;
2772 GeanyDocument *doc;
2773 guint i, len;
2774 gchar *base_name, *label;
2775 GPtrArray *sorted_documents;
2777 len = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2779 sorted_documents = g_ptr_array_sized_new(len);
2780 /* copy the documents_array into the new one */
2781 foreach_document(i)
2783 g_ptr_array_add(sorted_documents, documents[i]);
2785 if (compare_func == NULL)
2786 compare_func = document_compare_by_tab_order;
2788 /* and now sort it */
2789 g_ptr_array_sort(sorted_documents, compare_func);
2791 for (i = 0; i < sorted_documents->len; i++)
2793 doc = g_ptr_array_index(sorted_documents, i);
2795 base_name = g_path_get_basename(DOC_FILENAME(doc));
2796 menu_item = gtk_image_menu_item_new_with_label(base_name);
2797 image = gtk_image_new_from_gicon(doc->file_type->icon, GTK_ICON_SIZE_MENU);
2798 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
2800 gtk_widget_show(menu_item);
2801 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2802 g_signal_connect(menu_item, "activate", callback, doc);
2804 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2805 gtk_widget_set_name(menu_item_label, document_get_status_widget_class(doc));
2807 if (doc == active)
2809 label = g_markup_escape_text(base_name, -1);
2810 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", label);
2811 g_free(label);
2814 g_free(base_name);
2816 g_ptr_array_free(sorted_documents, TRUE);
2820 /** Checks whether the passed @a keyval is the Enter or Return key.
2821 * There are three different Enter/Return key values
2822 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2823 * This is just a convenience function.
2824 * @param keyval A keyval.
2825 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2826 * @since 0.19 */
2827 gboolean ui_is_keyval_enter_or_return(guint keyval)
2829 return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
2833 /** Reads an integer from the GTK default settings registry
2834 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2835 * @param property_name The property to read.
2836 * @param default_value The default value in case the value could not be read.
2837 * @return The value for the property if it exists, otherwise the @a default_value.
2838 * @since 0.19 */
2839 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
2841 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2842 gtk_settings_get_default())), property_name))
2844 gint value;
2845 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
2846 return value;
2848 else
2849 return default_value;
2853 void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
2854 gint new_text_len, gint *position, gpointer data)
2856 gboolean first = position != NULL && *position == 0;
2857 gint i;
2859 if (new_text_len == -1)
2860 new_text_len = (gint) strlen(new_text);
2862 for (i = 0; i < new_text_len; i++, new_text++)
2864 if ((!first || !strchr("+-", *new_text)) && !isdigit(*new_text))
2866 g_signal_stop_emission_by_name(editable, "insert-text");
2867 break;
2869 first = FALSE;
2874 /* gets the icon that applies to a particular MIME type */
2875 GIcon *ui_get_mime_icon(const gchar *mime_type)
2877 GIcon *icon = NULL;
2878 gchar *ctype;
2880 ctype = g_content_type_from_mime_type(mime_type);
2881 if (ctype)
2883 icon = g_content_type_get_icon(ctype);
2884 g_free(ctype);
2887 /* fallback if icon lookup failed, like it might happen on Windows (?) */
2888 if (! icon)
2890 const gchar *stock_id = GTK_STOCK_FILE;
2892 if (strstr(mime_type, "directory"))
2893 stock_id = GTK_STOCK_DIRECTORY;
2895 icon = g_themed_icon_new(stock_id);
2897 return icon;
2901 void ui_focus_current_document(void)
2903 GeanyDocument *doc = document_get_current();
2905 if (doc != NULL)
2906 document_grab_focus(doc);
2910 /** Finds the label text associated with stock_id
2911 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
2912 * @return The label text for stock
2913 * @since Geany 1.22 */
2914 const gchar *ui_lookup_stock_label(const gchar *stock_id)
2916 GtkStockItem item;
2918 if (gtk_stock_lookup(stock_id, &item))
2919 return item.label;
2921 g_warning("No stock id '%s'!", stock_id);
2922 return NULL;
2926 /* finds the next iter at any level
2927 * @param iter in/out, the current iter, will be changed to the next one
2928 * @param down whether to try the child iter
2929 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
2930 gboolean ui_tree_model_iter_any_next(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
2932 GtkTreeIter guess;
2933 GtkTreeIter copy = *iter;
2935 /* go down if the item has children */
2936 if (down && gtk_tree_model_iter_children(model, &guess, iter))
2937 *iter = guess;
2938 /* or to the next item at the same level */
2939 else if (gtk_tree_model_iter_next(model, &copy))
2940 *iter = copy;
2941 /* or to the next item at a parent level */
2942 else if (gtk_tree_model_iter_parent(model, &guess, iter))
2944 copy = guess;
2945 while (TRUE)
2947 if (gtk_tree_model_iter_next(model, &copy))
2949 *iter = copy;
2950 return TRUE;
2952 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
2953 guess = copy;
2954 else
2955 return FALSE;
2958 else
2959 return FALSE;
2961 return TRUE;
2965 GtkWidget *ui_create_encodings_combo_box(gboolean has_detect, gint default_enc)
2967 GtkCellRenderer *renderer;
2968 GtkTreeIter iter;
2969 GtkWidget *combo = gtk_combo_box_new();
2970 GtkTreeStore *store = encodings_encoding_store_new(has_detect);
2972 if (default_enc < 0 || default_enc >= GEANY_ENCODINGS_MAX)
2973 default_enc = has_detect ? GEANY_ENCODINGS_MAX : GEANY_ENCODING_NONE;
2975 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
2976 if (encodings_encoding_store_get_iter(store, &iter, default_enc))
2977 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
2978 renderer = gtk_cell_renderer_text_new();
2979 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
2980 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
2981 encodings_encoding_store_cell_data_func, NULL, NULL);
2983 return combo;
2987 gint ui_encodings_combo_box_get_active_encoding(GtkComboBox *combo)
2989 GtkTreeIter iter;
2990 gint enc = GEANY_ENCODING_NONE;
2992 /* there should always be an active iter anyway, but we check just in case */
2993 if (gtk_combo_box_get_active_iter(combo, &iter))
2995 GtkTreeModel *model = gtk_combo_box_get_model(combo);
2996 enc = encodings_encoding_store_get_encoding(GTK_TREE_STORE(model), &iter);
2999 return enc;
3003 gboolean ui_encodings_combo_box_set_active_encoding(GtkComboBox *combo, gint enc)
3005 GtkTreeIter iter;
3006 GtkTreeModel *model = gtk_combo_box_get_model(combo);
3008 if (encodings_encoding_store_get_iter(GTK_TREE_STORE(model), &iter, enc))
3010 gtk_combo_box_set_active_iter(combo, &iter);
3011 return TRUE;
3013 return FALSE;