FreeBasic: Update keywords
[geany-mirror.git] / src / ui_utils.c
blob8fb7dad41497d0ac34545a24386e7bbc8d017c5e
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"
52 #include "win32.h"
54 #include "gtkcompat.h"
56 #include <string.h>
57 #include <ctype.h>
58 #include <stdarg.h>
59 #include <gdk/gdkkeysyms.h>
62 #define DEFAULT_STATUSBAR_TEMPLATE N_(\
63 "line: %l / %L\t " \
64 "col: %c\t " \
65 "sel: %s\t " \
66 "%w %t %m" \
67 "mode: %M " \
68 "encoding: %e " \
69 "filetype: %f " \
70 "scope: %S")
72 GeanyInterfacePrefs interface_prefs;
73 GeanyMainWidgets main_widgets;
75 UIPrefs ui_prefs;
76 UIWidgets ui_widgets;
78 static GtkBuilder *builder = NULL;
79 static GtkWidget* window1 = NULL;
80 static GtkWidget* toolbar_popup_menu1 = NULL;
81 static GtkWidget* edit_menu1 = NULL;
82 static GtkWidget* prefs_dialog = NULL;
83 static GtkWidget* project_dialog = NULL;
85 static struct
87 /* pointers to widgets only sensitive when there is at least one document, the pointers can
88 * also be GtkAction objects, so check each pointer before using it */
89 GPtrArray *document_buttons;
90 GtkWidget *menu_insert_include_items[2];
91 GtkWidget *popup_goto_items[4];
92 GtkWidget *popup_copy_items[3];
93 GtkWidget *menu_copy_items[3];
94 GtkWidget *redo_items[3];
95 GtkWidget *undo_items[3];
96 GtkWidget *save_buttons[4];
97 GtkWidget *config_files_menu;
99 widgets;
101 enum
103 RECENT_FILE_FILE,
104 RECENT_FILE_PROJECT
107 typedef struct
109 gint type;
110 GQueue *recent_queue;
111 GtkWidget *menubar;
112 GtkWidget *toolbar;
113 void (*activate_cb)(GtkMenuItem *, gpointer);
114 } GeanyRecentFiles;
117 static void update_recent_menu(GeanyRecentFiles *grf);
118 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
119 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
120 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
121 static GtkWidget *progress_bar_create(void);
124 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
125 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
127 if (widget != NULL)
128 gtk_widget_set_sensitive(widget, set);
132 /* allow_override is TRUE if text can be ignored when another message has been set
133 * that didn't use allow_override and has not timed out. */
134 static void set_statusbar(const gchar *text, gboolean allow_override)
136 static guint id = 0;
137 static glong last_time = 0;
138 GTimeVal timeval;
139 const gint GEANY_STATUS_TIMEOUT = 1;
141 if (! interface_prefs.statusbar_visible)
142 return; /* just do nothing if statusbar is not visible */
144 if (id == 0)
145 id = gtk_statusbar_get_context_id(GTK_STATUSBAR(ui_widgets.statusbar), "geany-main");
147 g_get_current_time(&timeval);
149 if (! allow_override)
151 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
152 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
153 last_time = timeval.tv_sec;
155 else
156 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
158 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), id);
159 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), id, text);
164 /** Displays text on the statusbar.
165 * @param log Whether the message should be recorded in the Status window.
166 * @param format A @c printf -style string. */
167 void ui_set_statusbar(gboolean log, const gchar *format, ...)
169 gchar *string;
170 va_list args;
172 va_start(args, format);
173 string = g_strdup_vprintf(format, args);
174 va_end(args);
176 if (! prefs.suppress_status_messages)
177 set_statusbar(string, FALSE);
179 if (log || prefs.suppress_status_messages)
180 msgwin_status_add("%s", string);
182 g_free(string);
186 /* note: some comments below are for translators */
187 static gchar *create_statusbar_statistics(GeanyDocument *doc,
188 guint line, guint col, guint pos)
190 const gchar *cur_tag;
191 const gchar *fmt;
192 const gchar *expos; /* % expansion position */
193 const gchar sp[] = " ";
194 GString *stats_str;
195 ScintillaObject *sci = doc->editor->sci;
197 if (!EMPTY(ui_prefs.statusbar_template))
198 fmt = ui_prefs.statusbar_template;
199 else
200 fmt = _(DEFAULT_STATUSBAR_TEMPLATE);
202 stats_str = g_string_sized_new(120);
204 while ((expos = strchr(fmt, '%')) != NULL)
206 /* append leading text before % char */
207 g_string_append_len(stats_str, fmt, expos - fmt);
209 switch (*++expos)
211 case 'l':
212 g_string_append_printf(stats_str, "%d", line + 1);
213 break;
214 case 'L':
215 g_string_append_printf(stats_str, "%d",
216 sci_get_line_count(doc->editor->sci));
217 break;
218 case 'c':
219 g_string_append_printf(stats_str, "%d", col);
220 break;
221 case 'C':
222 g_string_append_printf(stats_str, "%d", col + 1);
223 break;
224 case 'p':
225 g_string_append_printf(stats_str, "%u", pos);
226 break;
227 case 's':
229 gint len = sci_get_selected_text_length(sci) - 1;
230 /* check if whole lines are selected */
231 if (!len || sci_get_col_from_position(sci,
232 sci_get_selection_start(sci)) != 0 ||
233 sci_get_col_from_position(sci,
234 sci_get_selection_end(sci)) != 0)
235 g_string_append_printf(stats_str, "%d", len);
236 else /* L = lines */
237 g_string_append_printf(stats_str, _("%dL"),
238 sci_get_lines_selected(doc->editor->sci) - 1);
239 break;
241 case 'w':
242 /* RO = read-only */
243 g_string_append(stats_str, (doc->readonly) ? _("RO ") :
244 /* OVR = overwrite/overtype, INS = insert */
245 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
246 break;
247 case 'r':
248 if (doc->readonly)
250 g_string_append(stats_str, _("RO ")); /* RO = read-only */
251 g_string_append(stats_str, sp + 1);
253 break;
254 case 't':
256 switch (editor_get_indent_prefs(doc->editor)->type)
258 case GEANY_INDENT_TYPE_TABS:
259 g_string_append(stats_str, _("TAB"));
260 break;
261 case GEANY_INDENT_TYPE_SPACES: /* SP = space */
262 g_string_append(stats_str, _("SP"));
263 break;
264 case GEANY_INDENT_TYPE_BOTH: /* T/S = tabs and spaces */
265 g_string_append(stats_str, _("T/S"));
266 break;
268 break;
270 case 'm':
271 if (doc->changed)
273 g_string_append(stats_str, _("MOD")); /* MOD = modified */
274 g_string_append(stats_str, sp);
276 break;
277 case 'M':
278 g_string_append(stats_str, editor_get_eol_char_name(doc->editor));
279 break;
280 case 'e':
281 g_string_append(stats_str,
282 doc->encoding ? doc->encoding : _("unknown"));
283 if (encodings_is_unicode_charset(doc->encoding) && (doc->has_bom))
285 g_string_append_c(stats_str, ' ');
286 g_string_append(stats_str, _("(with BOM)")); /* BOM = byte order mark */
288 break;
289 case 'f':
290 g_string_append(stats_str, filetypes_get_display_name(doc->file_type));
291 break;
292 case 'S':
293 symbols_get_current_scope(doc, &cur_tag);
294 g_string_append(stats_str, cur_tag);
295 break;
296 case 'Y':
297 g_string_append_c(stats_str, ' ');
298 g_string_append_printf(stats_str, "%d",
299 sci_get_style_at(doc->editor->sci, pos));
300 break;
301 default:
302 g_string_append_len(stats_str, expos, 1);
305 /* skip past %c chars */
306 if (*expos)
307 fmt = expos + 1;
308 else
309 break;
311 /* add any remaining text */
312 g_string_append(stats_str, fmt);
314 return g_string_free(stats_str, FALSE);
318 /* updates the status bar document statistics */
319 void ui_update_statusbar(GeanyDocument *doc, gint pos)
321 g_return_if_fail(doc == NULL || doc->is_valid);
323 if (! interface_prefs.statusbar_visible)
324 return; /* just do nothing if statusbar is not visible */
326 if (doc == NULL)
327 doc = document_get_current();
329 if (doc != NULL)
331 guint line, col;
332 gchar *stats_str;
334 if (pos == -1)
335 pos = sci_get_current_position(doc->editor->sci);
336 line = sci_get_line_from_position(doc->editor->sci, pos);
338 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
339 * when current pos is beyond document end (can occur when removing
340 * blocks of selected lines especially esp. brace sections near end of file). */
341 if (pos <= sci_get_length(doc->editor->sci))
342 col = sci_get_col_from_position(doc->editor->sci, pos);
343 else
344 col = 0;
346 stats_str = create_statusbar_statistics(doc, line, col, pos);
348 /* can be overridden by status messages */
349 set_statusbar(stats_str, TRUE);
350 g_free(stats_str);
352 else /* no documents */
354 set_statusbar("", TRUE); /* can be overridden by status messages */
359 /* This sets the window title according to the current filename. */
360 void ui_set_window_title(GeanyDocument *doc)
362 GString *str;
363 GeanyProject *project = app->project;
365 g_return_if_fail(doc == NULL || doc->is_valid);
367 if (doc == NULL)
368 doc = document_get_current();
370 str = g_string_new(NULL);
372 if (doc != NULL)
374 g_string_append(str, doc->changed ? "*" : "");
376 if (doc->file_name == NULL)
377 g_string_append(str, DOC_FILENAME(doc));
378 else
380 gchar *short_name = document_get_basename_for_display(doc, 30);
381 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
383 g_string_append(str, short_name);
384 g_string_append(str, " - ");
385 g_string_append(str, dirname ? dirname : "");
386 g_free(short_name);
387 g_free(dirname);
389 g_string_append(str, " - ");
391 if (project)
393 g_string_append_c(str, '[');
394 g_string_append(str, project->name);
395 g_string_append(str, "] - ");
397 g_string_append(str, "Geany");
398 if (cl_options.new_instance)
400 g_string_append(str, _(" (new instance)"));
402 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
403 g_string_free(str, TRUE);
407 void ui_set_editor_font(const gchar *font_name)
409 guint i;
411 g_return_if_fail(font_name != NULL);
413 /* do nothing if font has not changed */
414 if (interface_prefs.editor_font != NULL)
415 if (strcmp(font_name, interface_prefs.editor_font) == 0)
416 return;
418 g_free(interface_prefs.editor_font);
419 interface_prefs.editor_font = g_strdup(font_name);
421 /* We copy the current style, and update the font in all open tabs. */
422 for (i = 0; i < documents_array->len; i++)
424 if (documents[i]->editor)
426 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
430 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
434 void ui_set_fullscreen(void)
436 if (ui_prefs.fullscreen)
438 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
440 else
442 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
447 void ui_update_popup_reundo_items(GeanyDocument *doc)
449 gboolean enable_undo;
450 gboolean enable_redo;
451 guint i, len;
453 g_return_if_fail(doc == NULL || doc->is_valid);
455 if (doc == NULL)
457 enable_undo = FALSE;
458 enable_redo = FALSE;
460 else
462 enable_undo = document_can_undo(doc);
463 enable_redo = document_can_redo(doc);
466 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
467 len = G_N_ELEMENTS(widgets.undo_items);
468 for (i = 0; i < len; i++)
470 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
472 len = G_N_ELEMENTS(widgets.redo_items);
473 for (i = 0; i < len; i++)
475 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
480 void ui_update_popup_copy_items(GeanyDocument *doc)
482 gboolean enable;
483 guint i, len;
485 g_return_if_fail(doc == NULL || doc->is_valid);
487 if (doc == NULL)
488 enable = FALSE;
489 else
490 enable = sci_has_selection(doc->editor->sci);
492 len = G_N_ELEMENTS(widgets.popup_copy_items);
493 for (i = 0; i < len; i++)
494 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
498 void ui_update_popup_goto_items(gboolean enable)
500 guint i, len;
501 len = G_N_ELEMENTS(widgets.popup_goto_items);
502 for (i = 0; i < len; i++)
503 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
507 void ui_update_menu_copy_items(GeanyDocument *doc)
509 gboolean enable = FALSE;
510 guint i, len;
511 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
513 g_return_if_fail(doc == NULL || doc->is_valid);
515 if (IS_SCINTILLA(focusw))
516 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
517 else
518 if (GTK_IS_EDITABLE(focusw))
519 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
520 else
521 if (GTK_IS_TEXT_VIEW(focusw))
523 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
524 GTK_TEXT_VIEW(focusw));
525 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
528 len = G_N_ELEMENTS(widgets.menu_copy_items);
529 for (i = 0; i < len; i++)
530 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
534 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
536 gboolean enable = FALSE;
538 g_return_if_fail(doc == NULL || doc->is_valid);
540 if (doc == NULL || doc->file_type == NULL)
541 enable = FALSE;
542 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
543 enable = TRUE;
545 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
549 void ui_update_fold_items(void)
551 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
552 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
553 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
557 /* @include include name or NULL for empty with cursor ready for typing it */
558 static void insert_include(GeanyDocument *doc, gint pos, const gchar *include)
560 gint pos_after = -1;
561 gchar *text;
563 g_return_if_fail(doc != NULL);
564 g_return_if_fail(pos == -1 || pos >= 0);
566 if (pos == -1)
567 pos = sci_get_current_position(doc->editor->sci);
569 if (! include)
571 text = g_strdup("#include \"\"\n");
572 pos_after = pos + 10;
574 else
576 text = g_strconcat("#include <", include, ">\n", NULL);
579 sci_start_undo_action(doc->editor->sci);
580 sci_insert_text(doc->editor->sci, pos, text);
581 sci_end_undo_action(doc->editor->sci);
582 g_free(text);
583 if (pos_after >= 0)
584 sci_goto_pos(doc->editor->sci, pos_after, FALSE);
588 static void on_popup_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
590 insert_include(document_get_current(), editor_info.click_pos, user_data);
594 static void on_menu_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
596 insert_include(document_get_current(), -1, user_data);
600 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
602 guint i = 0;
603 GtkWidget *tmp_menu;
604 GtkWidget *tmp_popup;
605 GtkWidget *edit_menu, *edit_menu_item;
606 GtkWidget *popup_menu, *popup_menu_item;
608 edit_menu = gtk_menu_new();
609 popup_menu = gtk_menu_new();
610 edit_menu_item = gtk_menu_item_new_with_label(label);
611 popup_menu_item = gtk_menu_item_new_with_label(label);
612 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
613 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
615 while (includes[i] != NULL)
617 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
618 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
619 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
620 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
621 g_signal_connect(tmp_menu, "activate",
622 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
623 g_signal_connect(tmp_popup, "activate",
624 G_CALLBACK(on_popup_insert_include_activate), (gpointer) includes[i]);
625 i++;
627 gtk_widget_show_all(edit_menu_item);
628 gtk_widget_show_all(popup_menu_item);
629 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
630 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
634 void ui_create_insert_menu_items(void)
636 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
637 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
638 GtkWidget *blank;
639 const gchar *c_includes_stdlib[] = {
640 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
641 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
643 const gchar *c_includes_c99[] = {
644 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
645 "tgmath.h", "wchar.h", "wctype.h", NULL
647 const gchar *c_includes_cpp[] = {
648 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
650 const gchar *c_includes_cppstdlib[] = {
651 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
652 "memory", "locale", NULL
654 const gchar *c_includes_stl[] = {
655 "bitset", "deque", "list", "map", "set", "queue", "stack", "vector", "algorithm",
656 "iterator", "functional", "string", "complex", "valarray", NULL
659 blank = gtk_menu_item_new_with_label("#include \"...\"");
660 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
661 gtk_widget_show(blank);
662 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate), NULL);
663 blank = gtk_separator_menu_item_new ();
664 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
665 gtk_widget_show(blank);
667 blank = gtk_menu_item_new_with_label("#include \"...\"");
668 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
669 gtk_widget_show(blank);
670 g_signal_connect(blank, "activate", G_CALLBACK(on_popup_insert_include_activate), NULL);
671 blank = gtk_separator_menu_item_new();
672 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
673 gtk_widget_show(blank);
675 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
676 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
677 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
678 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
679 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
683 static void insert_date(GeanyDocument *doc, gint pos, const gchar *date_style)
685 const gchar *format = NULL;
686 gchar *time_str;
688 g_return_if_fail(doc != NULL);
689 g_return_if_fail(pos == -1 || pos >= 0);
691 if (pos == -1)
692 pos = sci_get_current_position(doc->editor->sci);
694 /* set default value */
695 if (utils_str_equal("", ui_prefs.custom_date_format))
697 g_free(ui_prefs.custom_date_format);
698 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
701 if (utils_str_equal(_("dd.mm.yyyy"), date_style))
702 format = "%d.%m.%Y";
703 else if (utils_str_equal(_("mm.dd.yyyy"), date_style))
704 format = "%m.%d.%Y";
705 else if (utils_str_equal(_("yyyy/mm/dd"), date_style))
706 format = "%Y/%m/%d";
707 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), date_style))
708 format = "%d.%m.%Y %H:%M:%S";
709 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), date_style))
710 format = "%m.%d.%Y %H:%M:%S";
711 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), date_style))
712 format = "%Y/%m/%d %H:%M:%S";
713 else if (utils_str_equal(_("_Use Custom Date Format"), date_style))
714 format = ui_prefs.custom_date_format;
715 else
717 gchar *str = dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets.window),
718 _("Enter here a custom date and time format. "
719 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
720 ui_prefs.custom_date_format);
721 if (str)
722 SETPTR(ui_prefs.custom_date_format, str);
723 return;
726 time_str = utils_get_date_time(format, NULL);
727 if (time_str != NULL)
729 sci_start_undo_action(doc->editor->sci);
730 sci_insert_text(doc->editor->sci, pos, time_str);
731 sci_goto_pos(doc->editor->sci, pos + strlen(time_str), FALSE);
732 sci_end_undo_action(doc->editor->sci);
733 g_free(time_str);
735 else
737 utils_beep();
738 ui_set_statusbar(TRUE,
739 _("Date format string could not be converted (possibly too long)."));
744 static void on_popup_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
746 insert_date(document_get_current(), editor_info.click_pos, user_data);
750 static void on_menu_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
752 insert_date(document_get_current(), -1, user_data);
756 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
758 GtkWidget *item;
760 item = gtk_menu_item_new_with_mnemonic(label);
761 gtk_container_add(GTK_CONTAINER(me), item);
762 gtk_widget_show(item);
763 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
765 item = gtk_menu_item_new_with_mnemonic(label);
766 gtk_container_add(GTK_CONTAINER(mp), item);
767 gtk_widget_show(item);
768 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), label);
772 void ui_create_insert_date_menu_items(void)
774 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
775 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
776 GtkWidget *item;
777 gchar *str;
779 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
780 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
781 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
783 item = gtk_separator_menu_item_new();
784 gtk_container_add(GTK_CONTAINER(menu_edit), item);
785 gtk_widget_show(item);
786 item = gtk_separator_menu_item_new();
787 gtk_container_add(GTK_CONTAINER(menu_popup), item);
788 gtk_widget_show(item);
790 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
791 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
792 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
794 item = gtk_separator_menu_item_new();
795 gtk_container_add(GTK_CONTAINER(menu_edit), item);
796 gtk_widget_show(item);
797 item = gtk_separator_menu_item_new();
798 gtk_container_add(GTK_CONTAINER(menu_popup), item);
799 gtk_widget_show(item);
801 str = _("_Use Custom Date Format");
802 item = gtk_menu_item_new_with_mnemonic(str);
803 gtk_container_add(GTK_CONTAINER(menu_edit), item);
804 gtk_widget_show(item);
805 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
806 ui_hookup_widget(main_widgets.window, item, "insert_date_custom1");
808 item = gtk_menu_item_new_with_mnemonic(str);
809 gtk_container_add(GTK_CONTAINER(menu_popup), item);
810 gtk_widget_show(item);
811 g_signal_connect(item, "activate", G_CALLBACK(on_popup_insert_date_activate), str);
812 ui_hookup_widget(main_widgets.editor_menu, item, "insert_date_custom2");
814 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
818 void ui_save_buttons_toggle(gboolean enable)
820 guint i;
821 gboolean dirty_tabs = FALSE;
823 if (ui_prefs.allow_always_save)
824 enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
826 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
827 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
829 /* save all menu item and tool button */
830 for (i = 0; i < documents_array->len; i++)
832 /* check whether there are files where changes were made and if there are some,
833 * we need the save all button / item */
834 if (documents[i]->is_valid && documents[i]->changed)
836 dirty_tabs = TRUE;
837 break;
841 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
842 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
846 #define add_doc_widget(widget_name) \
847 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
849 #define add_doc_toolitem(widget_name) \
850 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
852 static void init_document_widgets(void)
854 widgets.document_buttons = g_ptr_array_new();
856 /* Cache the document-sensitive widgets so we don't have to keep looking them up
857 * when using ui_document_buttons_update(). */
858 add_doc_widget("menu_close1");
859 add_doc_widget("close_other_documents1");
860 add_doc_widget("menu_change_font1");
861 add_doc_widget("menu_close_all1");
862 add_doc_widget("menu_save1");
863 add_doc_widget("menu_save_all1");
864 add_doc_widget("menu_save_as1");
865 add_doc_widget("menu_count_words1");
866 add_doc_widget("menu_build1");
867 add_doc_widget("add_comments1");
868 add_doc_widget("menu_paste1");
869 add_doc_widget("menu_undo2");
870 add_doc_widget("preferences2");
871 add_doc_widget("menu_reload1");
872 add_doc_widget("menu_document1");
873 add_doc_widget("menu_choose_color1");
874 add_doc_widget("menu_color_schemes");
875 add_doc_widget("menu_markers_margin1");
876 add_doc_widget("menu_linenumber_margin1");
877 add_doc_widget("menu_show_white_space1");
878 add_doc_widget("menu_show_line_endings1");
879 add_doc_widget("menu_show_indentation_guides1");
880 add_doc_widget("menu_zoom_in1");
881 add_doc_widget("menu_zoom_out1");
882 add_doc_widget("normal_size1");
883 add_doc_widget("treeview6");
884 add_doc_widget("print1");
885 add_doc_widget("menu_reload_as1");
886 add_doc_widget("menu_select_all1");
887 add_doc_widget("insert_date1");
888 add_doc_widget("insert_alternative_white_space1");
889 add_doc_widget("menu_format1");
890 add_doc_widget("commands2");
891 add_doc_widget("menu_open_selected_file1");
892 add_doc_widget("page_setup1");
893 add_doc_widget("find1");
894 add_doc_widget("find_next1");
895 add_doc_widget("find_previous1");
896 add_doc_widget("go_to_next_marker1");
897 add_doc_widget("go_to_previous_marker1");
898 add_doc_widget("replace1");
899 add_doc_widget("find_nextsel1");
900 add_doc_widget("find_prevsel1");
901 add_doc_widget("find_usage1");
902 add_doc_widget("find_document_usage1");
903 add_doc_widget("mark_all1");
904 add_doc_widget("go_to_line1");
905 add_doc_widget("goto_tag_definition1");
906 add_doc_widget("goto_tag_declaration1");
907 add_doc_widget("reset_indentation1");
908 add_doc_toolitem("Close");
909 add_doc_toolitem("CloseAll");
910 add_doc_toolitem("Search");
911 add_doc_toolitem("SearchEntry");
912 add_doc_toolitem("ZoomIn");
913 add_doc_toolitem("ZoomOut");
914 add_doc_toolitem("Indent");
915 add_doc_toolitem("UnIndent");
916 add_doc_toolitem("Cut");
917 add_doc_toolitem("Copy");
918 add_doc_toolitem("Paste");
919 add_doc_toolitem("Delete");
920 add_doc_toolitem("Save");
921 add_doc_toolitem("SaveAs");
922 add_doc_toolitem("SaveAll");
923 add_doc_toolitem("Compile");
924 add_doc_toolitem("Run");
925 add_doc_toolitem("Reload");
926 add_doc_toolitem("Color");
927 add_doc_toolitem("Goto");
928 add_doc_toolitem("GotoEntry");
929 add_doc_toolitem("Replace");
930 add_doc_toolitem("Print");
934 void ui_document_buttons_update(void)
936 guint i;
937 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
939 for (i = 0; i < widgets.document_buttons->len; i++)
941 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
942 if (GTK_IS_ACTION(widget))
943 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
944 else
945 ui_widget_set_sensitive(widget, enable);
950 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
952 g_ptr_array_remove_fast(widgets.document_buttons, widget);
956 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
957 * when some documents are present/no documents are open.
958 * It will be removed when the widget is destroyed.
959 * @param widget The widget to add.
961 * @since 0.15
963 void ui_add_document_sensitive(GtkWidget *widget)
965 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0;
967 ui_widget_set_sensitive(widget, enable);
969 g_ptr_array_add(widgets.document_buttons, widget);
970 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
974 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
976 if (show)
978 gtk_widget_show(widget);
980 else
982 gtk_widget_hide(widget);
987 void ui_sidebar_show_hide(void)
989 GtkWidget *widget;
991 /* check that there are no other notebook pages before hiding the sidebar completely
992 * other pages could be e.g. the file browser plugin */
993 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
994 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
996 ui_prefs.sidebar_visible = FALSE;
999 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
1000 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
1002 ignore_callback = TRUE;
1003 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
1004 ignore_callback = FALSE;
1007 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
1009 ui_widget_show_hide(gtk_notebook_get_nth_page(
1010 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
1011 ui_widget_show_hide(gtk_notebook_get_nth_page(
1012 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
1016 void ui_document_show_hide(GeanyDocument *doc)
1018 const gchar *widget_name;
1019 GtkWidget *item;
1020 const GeanyIndentPrefs *iprefs;
1022 g_return_if_fail(doc == NULL || doc->is_valid);
1024 if (doc == NULL)
1025 doc = document_get_current();
1027 if (doc == NULL)
1028 return;
1030 ignore_callback = TRUE;
1032 gtk_check_menu_item_set_active(
1033 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
1034 doc->editor->line_wrapping);
1036 gtk_check_menu_item_set_active(
1037 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
1038 doc->editor->line_breaking);
1040 iprefs = editor_get_indent_prefs(doc->editor);
1042 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
1043 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
1045 switch (iprefs->type)
1047 case GEANY_INDENT_TYPE_SPACES:
1048 widget_name = "spaces1"; break;
1049 case GEANY_INDENT_TYPE_TABS:
1050 widget_name = "tabs1"; break;
1051 case GEANY_INDENT_TYPE_BOTH:
1052 default:
1053 widget_name = "tabs_and_spaces1"; break;
1055 item = ui_lookup_widget(main_widgets.window, widget_name);
1056 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1058 if (iprefs->width >= 1 && iprefs->width <= 8)
1060 gchar *name;
1062 name = g_strdup_printf("indent_width_%d", iprefs->width);
1063 item = ui_lookup_widget(main_widgets.window, name);
1064 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1065 g_free(name);
1068 gtk_check_menu_item_set_active(
1069 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
1070 doc->readonly);
1072 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
1073 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
1074 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
1076 switch (sci_get_eol_mode(doc->editor->sci))
1078 case SC_EOL_CR: widget_name = "cr"; break;
1079 case SC_EOL_LF: widget_name = "lf"; break;
1080 default: widget_name = "crlf"; break;
1082 gtk_check_menu_item_set_active(
1083 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
1085 encodings_select_radio_item(doc->encoding);
1086 filetypes_select_radio_item(doc->file_type);
1088 ignore_callback = FALSE;
1092 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
1094 gtk_widget_set_name(widget, success ? NULL : "geany-search-entry-no-match");
1098 static void recent_create_menu(GeanyRecentFiles *grf)
1100 GtkWidget *tmp;
1101 guint i, len;
1102 gchar *filename;
1104 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
1105 for (i = 0; i < len; i++)
1107 filename = g_queue_peek_nth(grf->recent_queue, i);
1108 /* create menu item for the recent files menu in the menu bar */
1109 tmp = gtk_menu_item_new_with_label(filename);
1110 gtk_widget_show(tmp);
1111 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
1112 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1113 /* create menu item for the recent files menu in the toolbar */
1114 if (grf->toolbar != NULL)
1116 tmp = gtk_menu_item_new_with_label(filename);
1117 gtk_widget_show(tmp);
1118 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1119 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1125 static GeanyRecentFiles *recent_get_recent_files(void)
1127 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
1129 if (G_UNLIKELY(grf.recent_queue == NULL))
1131 grf.recent_queue = ui_prefs.recent_queue;
1132 grf.menubar = ui_widgets.recent_files_menu_menubar;
1133 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1134 toolbar_get_action_by_name("Open")));
1135 grf.activate_cb = recent_file_activate_cb;
1137 return &grf;
1141 static GeanyRecentFiles *recent_get_recent_projects(void)
1143 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
1145 if (G_UNLIKELY(grf.recent_queue == NULL))
1147 grf.recent_queue = ui_prefs.recent_projects_queue;
1148 grf.menubar = ui_widgets.recent_projects_menu_menubar;
1149 grf.toolbar = NULL;
1150 grf.activate_cb = recent_project_activate_cb;
1152 return &grf;
1156 void ui_create_recent_menus(void)
1158 recent_create_menu(recent_get_recent_files());
1159 recent_create_menu(recent_get_recent_projects());
1163 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1165 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1166 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1168 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1169 recent_file_loaded(utf8_filename, recent_get_recent_files());
1171 g_free(locale_filename);
1172 g_free(utf8_filename);
1176 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1178 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1179 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1181 if (project_ask_close() && project_load_file_with_session(locale_filename))
1182 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1184 g_free(locale_filename);
1185 g_free(utf8_filename);
1189 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf,
1190 const GtkRecentData *rdata)
1192 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1195 if (grf->type == RECENT_FILE_FILE && rdata)
1197 GtkRecentManager *manager = gtk_recent_manager_get_default();
1198 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1199 if (uri != NULL)
1201 gtk_recent_manager_add_full(manager, uri, rdata);
1202 g_free(uri);
1206 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1207 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1209 g_free(g_queue_pop_tail(grf->recent_queue));
1211 update_recent_menu(grf);
1213 /* filename already in recent list */
1214 else
1215 recent_file_loaded(utf8_filename, grf);
1219 void ui_add_recent_document(GeanyDocument *doc)
1221 /* what are the groups for actually? */
1222 static const gchar *groups[2] = {
1223 "geany",
1224 NULL
1226 GtkRecentData rdata;
1228 /* Prepare the data for gtk_recent_manager_add_full() */
1229 rdata.display_name = NULL;
1230 rdata.description = NULL;
1231 rdata.mime_type = doc->file_type->mime_type;
1232 /* if we ain't got no mime-type, fallback to plain text */
1233 if (! rdata.mime_type)
1234 rdata.mime_type = (gchar *) "text/plain";
1235 rdata.app_name = (gchar *) "geany";
1236 rdata.app_exec = (gchar *) "geany %u";
1237 rdata.groups = (gchar **) groups;
1238 rdata.is_private = FALSE;
1240 add_recent_file(doc->file_name, recent_get_recent_files(), &rdata);
1244 void ui_add_recent_project_file(const gchar *utf8_filename)
1246 add_recent_file(utf8_filename, recent_get_recent_projects(), NULL);
1250 /* Returns: newly allocated string with the UTF-8 menu text. */
1251 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1253 const gchar *text = NULL;
1255 if (gtk_bin_get_child(GTK_BIN(menu_item)))
1257 GtkWidget *child = gtk_bin_get_child(GTK_BIN(menu_item));
1259 if (GTK_IS_LABEL(child))
1260 text = gtk_label_get_text(GTK_LABEL(child));
1262 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1263 return g_strdup(text);
1267 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1269 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1270 gint result;
1272 if (utils_str_equal(menu_text, user_data))
1273 result = 0;
1274 else
1275 result = 1;
1277 g_free(menu_text);
1278 return result;
1282 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1284 GList *item, *children;
1285 void *data;
1286 GtkWidget *tmp;
1288 /* first reorder the queue */
1289 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1290 g_return_if_fail(item != NULL);
1292 data = item->data;
1293 g_queue_remove(grf->recent_queue, data);
1294 g_queue_push_head(grf->recent_queue, data);
1296 /* remove the old menuitem for the filename */
1297 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1298 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1299 if (item != NULL)
1300 gtk_widget_destroy(GTK_WIDGET(item->data));
1301 g_list_free(children);
1303 if (grf->toolbar != NULL)
1305 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1306 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1307 if (item != NULL)
1308 gtk_widget_destroy(GTK_WIDGET(item->data));
1309 g_list_free(children);
1311 /* now prepend a new menuitem for the filename,
1312 * first for the recent files menu in the menu bar */
1313 tmp = gtk_menu_item_new_with_label(utf8_filename);
1314 gtk_widget_show(tmp);
1315 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1316 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1317 /* then for the recent files menu in the tool bar */
1318 if (grf->toolbar != NULL)
1320 tmp = gtk_menu_item_new_with_label(utf8_filename);
1321 gtk_widget_show(tmp);
1322 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1323 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1324 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal which we need in
1325 * GeanyMenubuttonAction */
1326 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1327 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1332 static void update_recent_menu(GeanyRecentFiles *grf)
1334 GtkWidget *tmp;
1335 gchar *filename;
1336 GList *children, *item;
1338 filename = g_queue_peek_head(grf->recent_queue);
1340 /* clean the MRU list before adding an item (menubar) */
1341 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1342 if (g_list_length(children) > file_prefs.mru_length - 1)
1344 item = g_list_nth(children, file_prefs.mru_length - 1);
1345 while (item != NULL)
1347 if (GTK_IS_MENU_ITEM(item->data))
1348 gtk_widget_destroy(GTK_WIDGET(item->data));
1349 item = g_list_next(item);
1352 g_list_free(children);
1354 /* create item for the menu bar menu */
1355 tmp = gtk_menu_item_new_with_label(filename);
1356 gtk_widget_show(tmp);
1357 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1358 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1360 /* clean the MRU list before adding an item (toolbar) */
1361 if (grf->toolbar != NULL)
1363 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1364 if (g_list_length(children) > file_prefs.mru_length - 1)
1366 item = g_list_nth(children, file_prefs.mru_length - 1);
1367 while (item != NULL)
1369 if (GTK_IS_MENU_ITEM(item->data))
1370 gtk_widget_destroy(GTK_WIDGET(item->data));
1371 item = g_list_next(item);
1374 g_list_free(children);
1376 /* create item for the tool bar menu */
1377 tmp = gtk_menu_item_new_with_label(filename);
1378 gtk_widget_show(tmp);
1379 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1380 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1381 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1386 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1388 guint i;
1390 foreach_document (i)
1392 GeanyDocument *doc = documents[i];
1394 switch (feature)
1396 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1397 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1398 break;
1399 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1400 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin);
1401 break;
1402 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1403 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1404 break;
1405 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1406 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1407 break;
1408 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1409 editor_set_indentation_guides(doc->editor);
1410 break;
1416 void ui_update_view_editor_menu_items(void)
1418 ignore_callback = TRUE;
1419 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1420 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1421 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);
1422 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);
1423 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);
1424 ignore_callback = FALSE;
1428 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1429 * @param label_text The label text.
1430 * @param alignment An address to store the alignment widget pointer.
1431 * @return The frame widget, setting the alignment container for packing child widgets. */
1432 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1434 GtkWidget *label, *align;
1435 GtkWidget *frame = gtk_frame_new(NULL);
1437 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1439 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1440 gtk_container_add(GTK_CONTAINER(frame), align);
1441 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1443 label = ui_label_new_bold(label_text);
1444 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1446 *alignment = align;
1447 return frame;
1451 /** Makes a fixed border for dialogs without increasing the button box border.
1452 * @param dialog The parent container for the @c GtkVBox.
1453 * @return The packed @c GtkVBox. */
1454 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1456 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1458 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1459 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox, TRUE, TRUE, 0);
1460 return vbox;
1464 static GtkWidget *dialog_get_widget_for_response(GtkDialog *dialog, gint response_id)
1466 #if GTK_CHECK_VERSION(2, 20, 0)
1467 return gtk_dialog_get_widget_for_response(dialog, response_id);
1468 #else /* GTK < 2.20 */
1469 /* base logic stolen from GTK */
1470 GtkWidget *action_area = gtk_dialog_get_action_area(dialog);
1471 GtkWidget *widget = NULL;
1472 GList *children, *node;
1474 children = gtk_container_get_children(GTK_CONTAINER(action_area));
1475 for (node = children; node && ! widget; node = node->next)
1477 if (gtk_dialog_get_response_for_widget(dialog, node->data) == response_id)
1478 widget = node->data;
1480 g_list_free(children);
1482 return widget;
1483 #endif
1487 /* Reorders a dialog's buttons
1488 * @param dialog A dialog
1489 * @param response First response ID to reorder
1490 * @param ... more response IDs, terminated by -1
1492 * Like gtk_dialog_set_alternative_button_order(), but reorders the default
1493 * buttons layout, not the alternative one. This is useful if you e.g. added a
1494 * button to a dialog which already had some and need yours not to be on the
1495 * end.
1497 /* Heavily based on gtk_dialog_set_alternative_button_order().
1498 * This relies on the action area to be a GtkBox, but although not documented
1499 * the API expose it to be a GtkHButtonBox though GtkBuilder, so it should be
1500 * fine */
1501 void ui_dialog_set_primary_button_order(GtkDialog *dialog, gint response, ...)
1503 va_list ap;
1504 GtkWidget *action_area = gtk_dialog_get_action_area(dialog);
1505 gint position;
1507 va_start(ap, response);
1508 for (position = 0; response != -1; position++)
1510 GtkWidget *child = dialog_get_widget_for_response(dialog, response);
1511 if (child)
1512 gtk_box_reorder_child(GTK_BOX(action_area), child, position);
1513 else
1514 g_warning("%s: no child button with response id %d.", G_STRFUNC, response);
1516 response = va_arg(ap, gint);
1518 va_end(ap);
1522 /** Creates a @c GtkButton with custom text and a stock image similar to
1523 * @c gtk_button_new_from_stock().
1524 * @param stock_id A @c GTK_STOCK_NAME string.
1525 * @param text Button label text, can include mnemonics.
1526 * @return The new @c GtkButton.
1528 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1530 GtkWidget *image, *button;
1532 button = gtk_button_new_with_mnemonic(text);
1533 gtk_widget_show(button);
1534 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1535 gtk_button_set_image(GTK_BUTTON(button), image);
1536 /* note: image is shown by gtk */
1537 return button;
1541 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1542 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1543 * @param label Menu item label, can include mnemonics.
1544 * @return The new @c GtkImageMenuItem.
1546 * @since 0.16
1548 GtkWidget *
1549 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1551 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1552 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1554 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1555 gtk_widget_show(image);
1556 return item;
1560 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1561 GdkEvent *event, gpointer data)
1563 if (event->button.button == 1 && icon_pos == 1)
1565 gtk_entry_set_text(entry, "");
1566 gtk_widget_grab_focus(GTK_WIDGET(entry));
1571 /** Adds a small clear icon to the right end of the passed @a entry.
1572 * A callback to clear the contents of the GtkEntry is automatically added.
1574 * @param entry The GtkEntry object to which the icon should be attached.
1576 * @since 0.16
1578 void ui_entry_add_clear_icon(GtkEntry *entry)
1580 g_object_set(entry, "secondary-icon-stock", GTK_STOCK_CLEAR,
1581 "secondary-icon-activatable", TRUE, NULL);
1582 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1586 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1587 void ui_entry_add_activate_backward_signal(GtkEntry *entry)
1589 static gboolean installed = FALSE;
1591 g_return_if_fail(GTK_IS_ENTRY(entry));
1593 if (G_UNLIKELY(! installed))
1595 GtkBindingSet *binding_set;
1597 installed = TRUE;
1599 /* try to handle the unexpected case where GTK would already have installed the signal */
1600 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry)))
1602 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1603 return;
1606 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry),
1607 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 0, NULL, NULL,
1608 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1609 binding_set = gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry));
1610 gtk_binding_entry_add_signal(binding_set, GDK_Return, GDK_SHIFT_MASK, "activate-backward", 0);
1615 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1617 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1618 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1622 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1623 * the width of each button box's children.
1624 * Should be called after all child widgets have been packed. */
1625 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1627 GtkSizeGroup *size_group;
1629 gtk_box_set_spacing(GTK_BOX(copy), 10);
1630 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1632 /* now we need to put the widest widget from each button box in a size group,
1633 * but we don't know the width before they are drawn, and for different label
1634 * translations the widest widget can vary, so we just add all widgets. */
1635 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1636 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1637 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1638 g_object_unref(size_group);
1642 static gboolean tree_model_find_text(GtkTreeModel *model,
1643 GtkTreeIter *iter, gint column, const gchar *text)
1645 gchar *combo_text;
1646 gboolean found = FALSE;
1648 if (gtk_tree_model_get_iter_first(model, iter))
1652 gtk_tree_model_get(model, iter, 0, &combo_text, -1);
1653 found = utils_str_equal(combo_text, text);
1654 g_free(combo_text);
1656 if (found)
1657 return TRUE;
1659 while (gtk_tree_model_iter_next(model, iter));
1661 return FALSE;
1665 /** Prepends @a text to the drop down list, removing a duplicate element in
1666 * the list if found. Also ensures there are <= @a history_len elements.
1667 * @param combo_entry .
1668 * @param text Text to add, or @c NULL for current entry text.
1669 * @param history_len Max number of items, or @c 0 for default. */
1670 void ui_combo_box_add_to_history(GtkComboBoxText *combo_entry,
1671 const gchar *text, gint history_len)
1673 GtkComboBox *combo = GTK_COMBO_BOX(combo_entry);
1674 GtkTreeModel *model;
1675 GtkTreeIter iter;
1676 GtkTreePath *path;
1678 if (history_len <= 0)
1679 history_len = 10;
1680 if (!text)
1681 text = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
1683 model = gtk_combo_box_get_model(combo);
1685 if (tree_model_find_text(model, &iter, 0, text))
1687 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1689 gtk_combo_box_text_prepend_text(combo_entry, text);
1691 /* limit history */
1692 path = gtk_tree_path_new_from_indices(history_len, -1);
1693 if (gtk_tree_model_get_iter(model, &iter, path))
1695 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1697 gtk_tree_path_free(path);
1701 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1702 * exists in the combo's model. */
1703 void ui_combo_box_prepend_text_once(GtkComboBoxText *combo, const gchar *text)
1705 GtkTreeModel *model;
1706 GtkTreeIter iter;
1708 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
1709 if (tree_model_find_text(model, &iter, 0, text))
1710 return; /* don't prepend duplicate */
1712 gtk_combo_box_text_prepend_text(combo, text);
1716 /* Changes the color of the notebook tab text and open files items according to
1717 * document status. */
1718 void ui_update_tab_status(GeanyDocument *doc)
1720 gtk_widget_set_name(doc->priv->tab_label, document_get_status_widget_class(doc));
1722 sidebar_openfiles_update(doc);
1726 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1727 gboolean down)
1729 GtkTreePath *path;
1730 gboolean result;
1732 if (down)
1733 return gtk_tree_model_iter_next(model, iter);
1735 path = gtk_tree_model_get_path(model, iter);
1736 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1737 gtk_tree_path_free(path);
1738 return result;
1742 /* note: the while loop might be more efficient when searching upwards if it
1743 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1744 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1746 GtkTreeSelection *treesel;
1747 GtkTreeIter iter;
1748 GtkTreeModel *model;
1750 treesel = gtk_tree_view_get_selection(treeview);
1751 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1753 /* get the next selected item */
1754 if (! tree_model_iter_get_next(model, &iter, down))
1755 return FALSE; /* no more items */
1757 else /* no selection */
1759 if (! gtk_tree_model_get_iter_first(model, &iter))
1760 return TRUE; /* no items */
1762 while (TRUE)
1764 gtk_tree_selection_select_iter(treesel, &iter);
1765 if (cb(FALSE))
1766 break; /* found next message */
1768 if (! tree_model_iter_get_next(model, &iter, down))
1769 return FALSE; /* no more items */
1771 /* scroll item in view */
1772 if (ui_prefs.msgwindow_visible)
1774 GtkTreePath *path = gtk_tree_model_get_path(
1775 gtk_tree_view_get_model(treeview), &iter);
1777 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1778 gtk_tree_path_free(path);
1780 return TRUE;
1784 /* Returns FALSE if the treeview has items but no matching next item. */
1785 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1787 return tree_view_find(treeview, cb, TRUE);
1791 /* Returns FALSE if the treeview has items but no matching next item. */
1792 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1794 return tree_view_find(treeview, cb, FALSE);
1799 * Modifies the font of a widget using gtk_widget_modify_font().
1801 * @param widget The widget.
1802 * @param str The font name as expected by pango_font_description_from_string().
1804 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1806 PangoFontDescription *pfd;
1808 pfd = pango_font_description_from_string(str);
1809 gtk_widget_modify_font(widget, pfd);
1810 pango_font_description_free(pfd);
1814 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1815 * file chooser, replacing entry text (if successful) with the path returned from the
1816 * @c GtkFileChooser.
1817 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1818 * @param title The file chooser dialog title, or @c NULL.
1819 * @param action The mode of the file chooser.
1820 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1821 * such as @c GtkComboBoxEntry.
1822 * @return The @c GtkHBox.
1824 /* @see ui_setup_open_button_callback(). */
1825 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1827 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1829 hbox = gtk_hbox_new(FALSE, 6);
1830 path_entry = GTK_WIDGET(entry);
1832 /* prevent path_entry being vertically stretched to the height of dirbtn */
1833 vbox = gtk_vbox_new(FALSE, 0);
1834 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1836 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1838 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1840 else
1841 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1843 dirbtn = gtk_button_new();
1844 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1845 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1846 ui_setup_open_button_callback(dirbtn, title, action, entry);
1848 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1849 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1850 return hbox;
1854 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1857 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1858 * title can be NULL.
1859 * action is the file chooser mode to use. */
1860 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1861 GtkFileChooserAction action, GtkEntry *entry)
1863 GtkWidget *path_entry = GTK_WIDGET(entry);
1865 if (title)
1866 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1867 (GDestroyNotify) g_free);
1868 g_object_set_data(G_OBJECT(open_btn), "action", GINT_TO_POINTER(action));
1869 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), path_entry);
1873 #ifndef G_OS_WIN32
1874 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1875 const gchar *utf8_path)
1877 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1878 GTK_WINDOW(main_widgets.window), action,
1879 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1880 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1881 gchar *locale_path;
1882 gchar *ret_path = NULL;
1884 gtk_widget_set_name(dialog, "GeanyDialog");
1885 locale_path = utils_get_locale_from_utf8(utf8_path);
1886 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1888 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1889 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1891 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1893 if (g_path_is_absolute(locale_path))
1894 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1896 g_free(locale_path);
1898 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1900 gchar *dir_locale;
1902 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1903 ret_path = utils_get_utf8_from_locale(dir_locale);
1904 g_free(dir_locale);
1906 gtk_widget_destroy(dialog);
1907 return ret_path;
1909 #endif
1912 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1914 GtkFileChooserAction action = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "action"));
1915 GtkEntry *entry = user_data;
1916 const gchar *title = g_object_get_data(G_OBJECT(button), "title");
1917 gchar *utf8_path = NULL;
1919 /* TODO: extend for other actions */
1920 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1921 action == GTK_FILE_CHOOSER_ACTION_OPEN);
1923 if (title == NULL)
1924 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1925 _("Select Folder") : _("Select File");
1927 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1929 #ifdef G_OS_WIN32
1930 utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
1931 gtk_entry_get_text(GTK_ENTRY(entry)));
1932 #else
1933 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1934 #endif
1936 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1938 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
1939 #ifdef G_OS_WIN32
1940 utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
1941 gtk_entry_get_text(GTK_ENTRY(entry)));
1942 #else
1943 utf8_path = run_file_chooser(title, action, path);
1944 #endif
1945 g_free(path);
1948 if (utf8_path != NULL)
1950 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
1951 g_free(utf8_path);
1956 void ui_statusbar_showhide(gboolean state)
1958 /* handle statusbar visibility */
1959 if (state)
1961 gtk_widget_show(ui_widgets.statusbar);
1962 ui_update_statusbar(NULL, -1);
1964 else
1965 gtk_widget_hide(ui_widgets.statusbar);
1969 /** Packs all @c GtkWidgets passed after the row argument into a table, using
1970 * one widget per cell. The first widget is not expanded as the table grows,
1971 * as this is usually a label.
1972 * @param table
1973 * @param row The row number of the table.
1975 void ui_table_add_row(GtkTable *table, gint row, ...)
1977 va_list args;
1978 guint i;
1979 GtkWidget *widget;
1981 va_start(args, row);
1982 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
1984 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
1986 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
1987 options, 0, 0, 0);
1989 va_end(args);
1993 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
1995 const gchar *file_name = user_data;
1996 GeanyFiletype *ft = NULL;
1998 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
1999 ft = filetypes[GEANY_FILETYPES_CONF];
2001 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
2002 document_open_file(file_name, FALSE, ft, NULL);
2003 else
2005 gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
2006 gchar *base_name = g_path_get_basename(file_name);
2007 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
2008 gchar *global_content = NULL;
2010 /* if the requested file doesn't exist in the user's config dir, try loading the file
2011 * from the global data directory and use its contents for the newly created file */
2012 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
2013 g_file_get_contents(global_file, &global_content, NULL, NULL);
2015 document_new_file(utf8_filename, ft, global_content);
2017 utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
2022 static void free_on_closure_notify(gpointer data, GClosure *closure)
2024 g_free(data);
2028 /* @note You should connect to the "document-save" signal yourself to detect
2029 * if the user has just saved the config file, reloading it. */
2030 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
2032 GtkWidget *item;
2034 if (!parent)
2035 parent = GTK_CONTAINER(widgets.config_files_menu);
2037 if (!label)
2039 gchar *base_name;
2041 base_name = g_path_get_basename(real_path);
2042 item = gtk_menu_item_new_with_label(base_name);
2043 g_free(base_name);
2045 else
2046 item = gtk_menu_item_new_with_mnemonic(label);
2048 gtk_widget_show(item);
2049 gtk_container_add(parent, item);
2050 g_signal_connect_data(item, "activate", G_CALLBACK(on_config_file_clicked),
2051 g_strdup(real_path), free_on_closure_notify, 0);
2055 static gboolean sort_menu(gpointer data)
2057 ui_menu_sort_by_label(GTK_MENU(data));
2058 return FALSE;
2062 static void create_config_files_menu(void)
2064 GtkWidget *menu, *item;
2066 widgets.config_files_menu = menu = gtk_menu_new();
2068 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
2069 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
2071 /* sort menu after all items added */
2072 g_idle_add(sort_menu, widgets.config_files_menu);
2076 /* adds factory icons with a named icon source using the stock items id */
2077 static void add_stock_icons(const GtkStockItem *items, gsize count)
2079 GtkIconFactory *factory = gtk_icon_factory_new();
2080 GtkIconSource *source = gtk_icon_source_new();
2081 gsize i;
2083 for (i = 0; i < count; i++)
2085 GtkIconSet *set = gtk_icon_set_new();
2087 gtk_icon_source_set_icon_name(source, items[i].stock_id);
2088 gtk_icon_set_add_source(set, source);
2089 gtk_icon_factory_add(factory, items[i].stock_id, set);
2090 gtk_icon_set_unref(set);
2092 gtk_icon_source_free(source);
2093 gtk_icon_factory_add_default(factory);
2094 g_object_unref(factory);
2098 void ui_init_stock_items(void)
2100 GtkStockItem items[] =
2102 { GEANY_STOCK_SAVE_ALL, N_("Save All"), 0, 0, GETTEXT_PACKAGE },
2103 { GEANY_STOCK_CLOSE_ALL, N_("Close All"), 0, 0, GETTEXT_PACKAGE },
2104 { GEANY_STOCK_BUILD, N_("Build"), 0, 0, GETTEXT_PACKAGE }
2107 gtk_stock_add(items, G_N_ELEMENTS(items));
2108 add_stock_icons(items, G_N_ELEMENTS(items));
2112 void ui_init_toolbar_widgets(void)
2114 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
2115 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
2116 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
2117 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
2121 void ui_swap_sidebar_pos(void)
2123 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
2124 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
2125 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
2127 g_object_ref(left);
2128 g_object_ref(right);
2129 gtk_container_remove (GTK_CONTAINER (pane), left);
2130 gtk_container_remove (GTK_CONTAINER (pane), right);
2131 /* only scintilla notebook should expand */
2132 gtk_paned_pack1(GTK_PANED(pane), right, right == main_widgets.notebook, TRUE);
2133 gtk_paned_pack2(GTK_PANED(pane), left, left == main_widgets.notebook, TRUE);
2134 g_object_unref(left);
2135 g_object_unref(right);
2137 gtk_paned_set_position(GTK_PANED(pane), gtk_widget_get_allocated_width(pane)
2138 - gtk_paned_get_position(GTK_PANED(pane)));
2142 static void init_recent_files(void)
2144 GtkWidget *toolbar_recent_files_menu;
2146 /* add recent files to the File menu */
2147 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
2148 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
2149 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
2150 ui_widgets.recent_files_menu_menubar);
2152 /* add recent files to the toolbar Open button */
2153 toolbar_recent_files_menu = gtk_menu_new();
2154 g_object_ref(toolbar_recent_files_menu);
2155 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
2156 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
2160 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
2162 g_object_ref(menu);
2163 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
2164 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
2165 g_object_unref(menu);
2169 typedef struct GeanySharedMenu
2171 const gchar *menu;
2172 const gchar *menubar_item;
2173 const gchar *popup_item;
2175 GeanySharedMenu;
2177 #define foreach_menu(item, array) \
2178 for (item = array; item->menu; item++)
2180 static void on_editor_menu_show(GtkWidget *widget, GeanySharedMenu *items)
2182 GeanySharedMenu *item;
2184 foreach_menu(item, items)
2186 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2187 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2188 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2190 ui_menu_move(menu, bar, popup);
2195 static void on_editor_menu_hide(GtkWidget *widget, GeanySharedMenu *items)
2197 GeanySharedMenu *item;
2199 foreach_menu(item, items)
2201 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, item->popup_item);
2202 GtkWidget *bar = ui_lookup_widget(main_widgets.window, item->menubar_item);
2203 GtkWidget *menu = ui_lookup_widget(main_widgets.window, item->menu);
2205 ui_menu_move(menu, popup, bar);
2210 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2211 * so this is called after that's done. */
2212 void ui_init_prefs(void)
2214 StashGroup *group = stash_group_new(PACKAGE);
2216 /* various prefs */
2217 configuration_add_various_pref_group(group);
2219 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
2220 "show_symbol_list_expanders", TRUE);
2221 stash_group_add_boolean(group, &interface_prefs.compiler_tab_autoscroll,
2222 "compiler_tab_autoscroll", TRUE);
2223 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
2224 "allow_always_save", FALSE);
2225 stash_group_add_string(group, &ui_prefs.statusbar_template,
2226 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE));
2227 stash_group_add_boolean(group, &ui_prefs.new_document_after_close,
2228 "new_document_after_close", FALSE);
2229 stash_group_add_boolean(group, &interface_prefs.msgwin_status_visible,
2230 "msgwin_status_visible", TRUE);
2231 stash_group_add_boolean(group, &interface_prefs.msgwin_compiler_visible,
2232 "msgwin_compiler_visible", TRUE);
2233 stash_group_add_boolean(group, &interface_prefs.msgwin_messages_visible,
2234 "msgwin_messages_visible", TRUE);
2235 stash_group_add_boolean(group, &interface_prefs.msgwin_scribble_visible,
2236 "msgwin_scribble_visible", TRUE);
2240 /* Used to find out the name of the GtkBuilder retrieved object since
2241 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2242 * 'name' property for that and those that don't implement GtkBuildable
2243 * will have a "gtk-builder-name" stored in the GObject's data list. */
2244 static const gchar *ui_guess_object_name(GObject *obj)
2246 const gchar *name = NULL;
2248 g_return_val_if_fail(G_IS_OBJECT(obj), NULL);
2250 if (GTK_IS_BUILDABLE(obj))
2251 name = gtk_buildable_get_name(GTK_BUILDABLE(obj));
2252 if (! name)
2253 name = g_object_get_data(obj, "gtk-builder-name");
2254 if (! name)
2255 return NULL;
2257 return name;
2261 /* Compatibility functions */
2262 GtkWidget *create_edit_menu1(void)
2264 return edit_menu1;
2268 GtkWidget *create_prefs_dialog(void)
2270 return prefs_dialog;
2274 GtkWidget *create_project_dialog(void)
2276 return project_dialog;
2280 GtkWidget *create_toolbar_popup_menu1(void)
2282 return toolbar_popup_menu1;
2286 GtkWidget *create_window1(void)
2288 return window1;
2292 static GtkWidget *ui_get_top_parent(GtkWidget *widget)
2294 GtkWidget *parent;
2296 g_return_val_if_fail(GTK_IS_WIDGET(widget), NULL);
2298 for (;;)
2300 if (GTK_IS_MENU(widget))
2301 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2302 else
2303 parent = gtk_widget_get_parent(widget);
2304 if (parent == NULL)
2305 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2306 if (parent == NULL)
2307 break;
2308 widget = parent;
2311 return widget;
2315 void ui_init_builder(void)
2317 gchar *interface_file;
2318 const gchar *name;
2319 GError *error;
2320 GSList *iter, *all_objects;
2321 GtkWidget *widget, *toplevel;
2323 /* prevent function from being called twice */
2324 if (GTK_IS_BUILDER(builder))
2325 return;
2327 builder = gtk_builder_new();
2329 gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
2331 error = NULL;
2332 interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
2333 if (! gtk_builder_add_from_file(builder, interface_file, &error))
2335 /* Show the user this message so they know WTF happened */
2336 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
2337 _("Geany cannot start!"), error->message);
2338 /* Aborts */
2339 g_error("Cannot create user-interface: %s", error->message);
2340 g_error_free(error);
2341 g_free(interface_file);
2342 g_object_unref(builder);
2343 return;
2345 g_free(interface_file);
2347 gtk_builder_connect_signals(builder, NULL);
2349 edit_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "edit_menu1"));
2350 prefs_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "prefs_dialog"));
2351 project_dialog = GTK_WIDGET(gtk_builder_get_object(builder, "project_dialog"));
2352 toolbar_popup_menu1 = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar_popup_menu1"));
2353 window1 = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
2355 g_object_set_data(G_OBJECT(edit_menu1), "edit_menu1", edit_menu1);
2356 g_object_set_data(G_OBJECT(prefs_dialog), "prefs_dialog", prefs_dialog);
2357 g_object_set_data(G_OBJECT(project_dialog), "project_dialog", project_dialog);
2358 g_object_set_data(G_OBJECT(toolbar_popup_menu1), "toolbar_popup_menu1", toolbar_popup_menu1);
2359 g_object_set_data(G_OBJECT(window1), "window1", window1);
2361 all_objects = gtk_builder_get_objects(builder);
2362 for (iter = all_objects; iter != NULL; iter = g_slist_next(iter))
2364 if (! GTK_IS_WIDGET(iter->data))
2365 continue;
2367 widget = GTK_WIDGET(iter->data);
2369 name = ui_guess_object_name(G_OBJECT(widget));
2370 if (! name)
2372 g_warning("Unable to get name from GtkBuilder object");
2373 continue;
2376 toplevel = ui_get_top_parent(widget);
2377 if (toplevel)
2378 ui_hookup_widget(toplevel, widget, name);
2380 g_slist_free(all_objects);
2384 static void init_custom_style(void)
2386 #if GTK_CHECK_VERSION(3, 0, 0)
2387 gchar *css_file = g_build_filename(app->datadir, "geany.css", NULL);
2388 GtkCssProvider *css = gtk_css_provider_new();
2389 GError *error = NULL;
2391 if (! gtk_css_provider_load_from_path(css, css_file, &error))
2393 g_warning("Failed to load custom CSS: %s", error->message);
2394 g_error_free(error);
2396 else
2398 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2399 GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2402 g_object_unref(css);
2403 g_free(css_file);
2404 #else
2405 /* see setup_gtk2_styles() in main.c */
2406 #endif
2410 void ui_init(void)
2412 init_custom_style();
2414 init_recent_files();
2416 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
2417 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
2419 main_widgets.progressbar = progress_bar_create();
2421 /* current word sensitive items */
2422 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition2");
2423 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "context_action1");
2424 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage2");
2425 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage2");
2427 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
2428 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
2429 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
2430 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
2431 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
2432 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
2433 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
2434 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
2435 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
2436 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
2437 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
2438 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
2439 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
2440 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
2442 /* reparent context submenus as needed */
2444 GeanySharedMenu arr[] = {
2445 {"commands2_menu", "commands2", "commands1"},
2446 {"menu_format1_menu", "menu_format1", "menu_format2"},
2447 {"more1_menu", "more1", "search2"},
2448 {NULL, NULL, NULL}
2450 static GeanySharedMenu items[G_N_ELEMENTS(arr)];
2452 memcpy(items, arr, sizeof(arr));
2453 g_signal_connect(main_widgets.editor_menu, "show", G_CALLBACK(on_editor_menu_show), items);
2454 g_signal_connect(main_widgets.editor_menu, "hide", G_CALLBACK(on_editor_menu_hide), items);
2457 ui_init_toolbar_widgets();
2458 init_document_widgets();
2459 create_config_files_menu();
2463 void ui_finalize_builder(void)
2465 if (GTK_IS_BUILDER(builder))
2466 g_object_unref(builder);
2468 /* cleanup refs lingering even after GtkBuilder is destroyed */
2469 if (GTK_IS_WIDGET(edit_menu1))
2470 gtk_widget_destroy(edit_menu1);
2471 if (GTK_IS_WIDGET(prefs_dialog))
2472 gtk_widget_destroy(prefs_dialog);
2473 if (GTK_IS_WIDGET(project_dialog))
2474 gtk_widget_destroy(project_dialog);
2475 if (GTK_IS_WIDGET(toolbar_popup_menu1))
2476 gtk_widget_destroy(toolbar_popup_menu1);
2477 if (GTK_IS_WIDGET(window1))
2478 gtk_widget_destroy(window1);
2482 static void auto_separator_update(GeanyAutoSeparator *autosep)
2484 g_return_if_fail(autosep->item_count >= 0);
2486 if (autosep->widget)
2488 if (autosep->item_count > 0)
2489 ui_widget_show_hide(autosep->widget, autosep->show_count > 0);
2490 else
2491 gtk_widget_destroy(autosep->widget);
2496 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
2498 GeanyAutoSeparator *autosep = user_data;
2500 if (gtk_widget_get_visible(widget))
2501 autosep->show_count++;
2502 else
2503 autosep->show_count--;
2504 auto_separator_update(autosep);
2508 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
2510 GeanyAutoSeparator *autosep = user_data;
2512 autosep->item_count--;
2513 autosep->item_count = MAX(autosep->item_count, 0);
2514 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2515 * so assume widget was visible */
2516 autosep->show_count--;
2517 autosep->show_count = MAX(autosep->item_count, 0);
2518 auto_separator_update(autosep);
2522 /* Show the separator widget if @a item or another is visible. */
2523 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2524 * property, and using reference counting to keep the widget alive whilst its visible group
2525 * is alive. */
2526 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2528 /* set widget ptr NULL when widget destroyed */
2529 if (autosep->item_count == 0)
2530 g_signal_connect(autosep->widget, "destroy",
2531 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2533 if (gtk_widget_get_visible(item))
2534 autosep->show_count++;
2536 autosep->item_count++;
2537 auto_separator_update(autosep);
2539 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2540 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2541 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2546 * Sets @a text as the contents of the tooltip for @a widget.
2548 * @param widget The widget the tooltip should be set for.
2549 * @param text The text for the tooltip.
2551 * @since 0.16
2552 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2554 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2556 gtk_widget_set_tooltip_text(widget, text);
2560 /** Returns a widget from a name in a component, usually created by Glade.
2561 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2562 * or alternatively any widget in the component, and the name of the widget
2563 * you want returned.
2564 * @param widget Widget with the @a widget_name property set.
2565 * @param widget_name Name to lookup.
2566 * @return The widget found.
2567 * @see ui_hookup_widget().
2569 * @since 0.16
2571 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2573 GtkWidget *parent, *found_widget;
2575 g_return_val_if_fail(widget != NULL, NULL);
2576 g_return_val_if_fail(widget_name != NULL, NULL);
2578 for (;;)
2580 if (GTK_IS_MENU(widget))
2581 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2582 else
2583 parent = gtk_widget_get_parent(widget);
2584 if (parent == NULL)
2585 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2586 if (parent == NULL)
2587 break;
2588 widget = parent;
2591 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2592 if (G_UNLIKELY(found_widget == NULL))
2593 g_warning("Widget not found: %s", widget_name);
2594 return found_widget;
2598 /* wraps gtk_builder_get_object()
2599 * unlike ui_lookup_widget(), it does only support getting object created from the main
2600 * UI file, but it can fetch any object, not only widgets */
2601 gpointer ui_builder_get_object (const gchar *name)
2603 return gtk_builder_get_object (builder, name);
2607 /* Progress Bar */
2608 static guint progress_bar_timer_id = 0;
2611 static GtkWidget *progress_bar_create(void)
2613 GtkWidget *bar = gtk_progress_bar_new();
2615 /* Set the progressbar's height to 1 to fit it in the statusbar */
2616 gtk_widget_set_size_request(bar, -1, 1);
2617 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2619 return bar;
2623 static gboolean progress_bar_pulse(gpointer data)
2625 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2627 return TRUE;
2632 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2633 * (if the statusbar is visible). This is a convenience function which adds a timer to
2634 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2635 * You can use this function when you have time consuming asynchronous operation and want to
2636 * display some activity in the GUI and when you don't know about detailed progress steps.
2637 * The progressbar widget is hidden by default when it is not active. This function and
2638 * ui_progress_bar_stop() will show and hide it automatically for you.
2640 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2641 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2642 * In this case, you need to show and hide the widget yourself. You can find some example code
2643 * in @c src/printing.c.
2645 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2647 * @since 0.16
2649 void ui_progress_bar_start(const gchar *text)
2651 g_return_if_fail(progress_bar_timer_id == 0);
2653 if (! interface_prefs.statusbar_visible)
2654 return;
2656 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2658 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2660 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2664 /** Stops a running progress bar and hides the widget again.
2666 * @since 0.16
2668 void ui_progress_bar_stop(void)
2670 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2672 if (progress_bar_timer_id != 0)
2674 g_source_remove(progress_bar_timer_id);
2675 progress_bar_timer_id = 0;
2680 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2682 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2683 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2684 gchar *sa, *sb;
2685 gint result;
2687 sa = ui_menu_item_get_text(item_a);
2688 sb = ui_menu_item_get_text(item_b);
2689 result = utils_str_casecmp(sa, sb);
2690 g_free(sa);
2691 g_free(sb);
2692 return result;
2696 /* Currently @a menu should contain only GtkMenuItems with labels. */
2697 void ui_menu_sort_by_label(GtkMenu *menu)
2699 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2700 GList *node;
2701 gint pos;
2703 list = g_list_sort(list, compare_menu_item_labels);
2704 pos = 0;
2705 foreach_list(node, list)
2707 gtk_menu_reorder_child(menu, node->data, pos);
2708 pos++;
2710 g_list_free(list);
2714 void ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2716 va_list a;
2717 gchar *text;
2719 va_start(a, format);
2720 text = g_strdup_vprintf(format, a);
2721 va_end(a);
2723 gtk_label_set_text(label, text);
2724 gtk_label_set_use_markup(label, TRUE);
2725 g_free(text);
2729 GtkWidget *ui_label_new_bold(const gchar *text)
2731 GtkWidget *label;
2732 gchar *label_text;
2734 label_text = g_markup_escape_text(text, -1);
2735 label = gtk_label_new(NULL);
2736 ui_label_set_markup(GTK_LABEL(label), "<b>%s</b>", label_text);
2737 g_free(label_text);
2738 return label;
2742 /** Adds a list of document items to @a menu.
2743 * @param menu Menu.
2744 * @param active Which document to highlight, or @c NULL.
2745 * @param callback is used for each menu item's @c "activate" signal and will be passed
2746 * the corresponding document pointer as @c user_data.
2747 * @warning You should check @c doc->is_valid in the callback.
2748 * @since 0.19 */
2749 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2751 ui_menu_add_document_items_sorted(menu, active, callback, NULL);
2755 /** Adds a list of document items to @a menu.
2757 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2758 * the order of the document tabs is used.
2760 * See document_compare_by_display_name() for an example sort function.
2762 * @param menu Menu.
2763 * @param active Which document to highlight, or @c NULL.
2764 * @param callback is used for each menu item's @c "activate" signal and will be passed
2765 * the corresponding document pointer as @c user_data.
2766 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2767 * @warning You should check @c doc->is_valid in the callback.
2768 * @since 0.21 */
2769 void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
2770 GCallback callback, GCompareFunc compare_func)
2772 GtkWidget *menu_item, *menu_item_label, *image;
2773 GeanyDocument *doc;
2774 guint i, len;
2775 gchar *base_name, *label;
2776 GPtrArray *sorted_documents;
2778 len = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2780 sorted_documents = g_ptr_array_sized_new(len);
2781 /* copy the documents_array into the new one */
2782 foreach_document(i)
2784 g_ptr_array_add(sorted_documents, documents[i]);
2786 if (compare_func == NULL)
2787 compare_func = document_compare_by_tab_order;
2789 /* and now sort it */
2790 g_ptr_array_sort(sorted_documents, compare_func);
2792 for (i = 0; i < sorted_documents->len; i++)
2794 doc = g_ptr_array_index(sorted_documents, i);
2796 base_name = g_path_get_basename(DOC_FILENAME(doc));
2797 menu_item = gtk_image_menu_item_new_with_label(base_name);
2798 image = gtk_image_new_from_gicon(doc->file_type->icon, GTK_ICON_SIZE_MENU);
2799 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
2801 gtk_widget_show(menu_item);
2802 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2803 g_signal_connect(menu_item, "activate", callback, doc);
2805 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2806 gtk_widget_set_name(menu_item_label, document_get_status_widget_class(doc));
2808 if (doc == active)
2810 label = g_markup_escape_text(base_name, -1);
2811 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", label);
2812 g_free(label);
2815 g_free(base_name);
2817 g_ptr_array_free(sorted_documents, TRUE);
2821 /** Checks whether the passed @a keyval is the Enter or Return key.
2822 * There are three different Enter/Return key values
2823 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2824 * This is just a convenience function.
2825 * @param keyval A keyval.
2826 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2827 * @since 0.19 */
2828 gboolean ui_is_keyval_enter_or_return(guint keyval)
2830 return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
2834 /** Reads an integer from the GTK default settings registry
2835 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2836 * @param property_name The property to read.
2837 * @param default_value The default value in case the value could not be read.
2838 * @return The value for the property if it exists, otherwise the @a default_value.
2839 * @since 0.19 */
2840 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
2842 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2843 gtk_settings_get_default())), property_name))
2845 gint value;
2846 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
2847 return value;
2849 else
2850 return default_value;
2854 void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
2855 gint new_text_len, gint *position, gpointer data)
2857 gboolean first = position != NULL && *position == 0;
2858 gint i;
2860 if (new_text_len == -1)
2861 new_text_len = (gint) strlen(new_text);
2863 for (i = 0; i < new_text_len; i++, new_text++)
2865 if ((!first || !strchr("+-", *new_text)) && !isdigit(*new_text))
2867 g_signal_stop_emission_by_name(editable, "insert-text");
2868 break;
2870 first = FALSE;
2875 /* gets the icon that applies to a particular MIME type */
2876 GIcon *ui_get_mime_icon(const gchar *mime_type)
2878 GIcon *icon = NULL;
2879 gchar *ctype;
2881 ctype = g_content_type_from_mime_type(mime_type);
2882 if (ctype)
2884 icon = g_content_type_get_icon(ctype);
2885 g_free(ctype);
2888 /* fallback if icon lookup failed, like it might happen on Windows (?) */
2889 if (! icon)
2891 const gchar *stock_id = GTK_STOCK_FILE;
2893 if (strstr(mime_type, "directory"))
2894 stock_id = GTK_STOCK_DIRECTORY;
2896 icon = g_themed_icon_new(stock_id);
2898 return icon;
2902 void ui_focus_current_document(void)
2904 GeanyDocument *doc = document_get_current();
2906 if (doc != NULL)
2907 document_grab_focus(doc);
2911 /** Finds the label text associated with stock_id
2912 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
2913 * @return The label text for stock
2914 * @since Geany 1.22 */
2915 const gchar *ui_lookup_stock_label(const gchar *stock_id)
2917 GtkStockItem item;
2919 if (gtk_stock_lookup(stock_id, &item))
2920 return item.label;
2922 g_warning("No stock id '%s'!", stock_id);
2923 return NULL;
2927 /* finds the next iter at any level
2928 * @param iter in/out, the current iter, will be changed to the next one
2929 * @param down whether to try the child iter
2930 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
2931 gboolean ui_tree_model_iter_any_next(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
2933 GtkTreeIter guess;
2934 GtkTreeIter copy = *iter;
2936 /* go down if the item has children */
2937 if (down && gtk_tree_model_iter_children(model, &guess, iter))
2938 *iter = guess;
2939 /* or to the next item at the same level */
2940 else if (gtk_tree_model_iter_next(model, &copy))
2941 *iter = copy;
2942 /* or to the next item at a parent level */
2943 else if (gtk_tree_model_iter_parent(model, &guess, iter))
2945 copy = guess;
2946 while (TRUE)
2948 if (gtk_tree_model_iter_next(model, &copy))
2950 *iter = copy;
2951 return TRUE;
2953 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
2954 guess = copy;
2955 else
2956 return FALSE;
2959 else
2960 return FALSE;
2962 return TRUE;
2966 GtkWidget *ui_create_encodings_combo_box(gboolean has_detect, gint default_enc)
2968 GtkCellRenderer *renderer;
2969 GtkTreeIter iter;
2970 GtkWidget *combo = gtk_combo_box_new();
2971 GtkTreeStore *store = encodings_encoding_store_new(has_detect);
2973 if (default_enc < 0 || default_enc >= GEANY_ENCODINGS_MAX)
2974 default_enc = has_detect ? GEANY_ENCODINGS_MAX : GEANY_ENCODING_NONE;
2976 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
2977 if (encodings_encoding_store_get_iter(store, &iter, default_enc))
2978 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
2979 renderer = gtk_cell_renderer_text_new();
2980 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
2981 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
2982 encodings_encoding_store_cell_data_func, NULL, NULL);
2984 return combo;
2988 gint ui_encodings_combo_box_get_active_encoding(GtkComboBox *combo)
2990 GtkTreeIter iter;
2991 gint enc = GEANY_ENCODING_NONE;
2993 /* there should always be an active iter anyway, but we check just in case */
2994 if (gtk_combo_box_get_active_iter(combo, &iter))
2996 GtkTreeModel *model = gtk_combo_box_get_model(combo);
2997 enc = encodings_encoding_store_get_encoding(GTK_TREE_STORE(model), &iter);
3000 return enc;
3004 gboolean ui_encodings_combo_box_set_active_encoding(GtkComboBox *combo, gint enc)
3006 GtkTreeIter iter;
3007 GtkTreeModel *model = gtk_combo_box_get_model(combo);
3009 if (encodings_encoding_store_get_iter(GTK_TREE_STORE(model), &iter, enc))
3011 gtk_combo_box_set_active_iter(combo, &iter);
3012 return TRUE;
3014 return FALSE;