r5162 | eht16 | 2010-08-15 13:53:09 +0100 (Sun, 15 Aug 2010) | 1 line
[geany-mirror.git] / src / ui_utils.c
blobd973fa8ee625bd78d866c622ca8547d91b9a7b35
1 /*
2 * ui_utils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
24 /** @file ui_utils.h
25 * User Interface general utility functions.
28 #include "geany.h"
30 #include <string.h>
31 #include <ctype.h>
32 #include <gdk/gdkkeysyms.h>
34 #include "ui_utils.h"
35 #include "prefs.h"
36 #include "sciwrappers.h"
37 #include "document.h"
38 #include "documentprivate.h"
39 #include "filetypes.h"
40 #include "support.h"
41 #include "msgwindow.h"
42 #include "utils.h"
43 #include "callbacks.h"
44 #include "encodings.h"
45 #include "images.c"
46 #include "sidebar.h"
47 #include "win32.h"
48 #include "project.h"
49 #include "editor.h"
50 #include "plugins.h"
51 #include "symbols.h"
52 #include "toolbar.h"
53 #include "geanymenubuttonaction.h"
56 GeanyInterfacePrefs interface_prefs;
57 GeanyMainWidgets main_widgets;
59 UIPrefs ui_prefs;
60 UIWidgets ui_widgets;
62 static struct
64 /* pointers to widgets only sensitive when there is at least one document, the pointers can
65 * also be GtkAction objects, so check each pointer before using it */
66 GPtrArray *document_buttons;
67 GtkWidget *menu_insert_include_items[2];
68 GtkWidget *popup_goto_items[4];
69 GtkWidget *popup_copy_items[3];
70 GtkWidget *menu_copy_items[3];
71 GtkWidget *redo_items[3];
72 GtkWidget *undo_items[3];
73 GtkWidget *save_buttons[4];
74 GtkWidget *config_files_menu;
75 GtkWidget *commands_menu;
76 GtkWidget *format_menu;
78 widgets;
80 enum
82 RECENT_FILE_FILE,
83 RECENT_FILE_PROJECT
86 typedef struct
88 gint type;
89 GQueue *recent_queue;
90 GtkWidget *menubar;
91 GtkWidget *toolbar;
92 void (*activate_cb)(GtkMenuItem *, gpointer);
93 } GeanyRecentFiles;
96 static void update_recent_menu(GeanyRecentFiles *grf);
97 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
98 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
99 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
100 static GtkWidget *progress_bar_create(void);
103 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
104 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
106 if (widget != NULL)
107 gtk_widget_set_sensitive(widget, set);
111 /* allow_override is TRUE if text can be ignored when another message has been set
112 * that didn't use allow_override and has not timed out. */
113 static void set_statusbar(const gchar *text, gboolean allow_override)
115 static glong last_time = 0;
116 GTimeVal timeval;
117 const gint GEANY_STATUS_TIMEOUT = 1;
119 if (! interface_prefs.statusbar_visible)
120 return; /* just do nothing if statusbar is not visible */
122 g_get_current_time(&timeval);
124 if (! allow_override)
126 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), 1);
127 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), 1, text);
128 last_time = timeval.tv_sec;
130 else
131 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
133 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), 1);
134 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), 1, text);
139 /** Displays text on the statusbar.
140 * @param log Whether the message should be recorded in the Status window.
141 * @param format A @c printf -style string. */
142 void ui_set_statusbar(gboolean log, const gchar *format, ...)
144 gchar *string;
145 va_list args;
147 va_start(args, format);
148 string = g_strdup_vprintf(format, args);
149 va_end(args);
151 if (! prefs.suppress_status_messages)
152 set_statusbar(string, FALSE);
154 if (log || prefs.suppress_status_messages)
155 msgwin_status_add("%s", string);
157 g_free(string);
161 /* updates the status bar document statistics */
162 void ui_update_statusbar(GeanyDocument *doc, gint pos)
164 if (! interface_prefs.statusbar_visible)
165 return; /* just do nothing if statusbar is not visible */
167 if (doc == NULL)
168 doc = document_get_current();
170 if (doc != NULL)
172 static GString *stats_str = NULL;
173 const gchar sp[] = " ";
174 guint line, col;
175 const gchar *cur_tag;
176 gchar *filetype_name = doc->file_type->name;
178 if (G_UNLIKELY(stats_str == NULL))
179 stats_str = g_string_sized_new(120);
181 if (pos == -1)
182 pos = sci_get_current_position(doc->editor->sci);
183 line = sci_get_line_from_position(doc->editor->sci, pos);
185 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
186 * when current pos is beyond document end (can occur when removing
187 * blocks of selected lines especially esp. brace sections near end of file). */
188 if (pos <= sci_get_length(doc->editor->sci))
189 col = sci_get_col_from_position(doc->editor->sci, pos);
190 else
191 col = 0;
193 /* Status bar statistics: col = column, sel = selection. */
194 g_string_printf(stats_str, _("line: %d / %d\t col: %d\t sel: %d\t "),
195 (line + 1), sci_get_line_count(doc->editor->sci), col,
196 sci_get_selected_text_length(doc->editor->sci) - 1);
198 g_string_append(stats_str,
199 /* RO = read-only */
200 (doc->readonly) ? _("RO ") :
201 /* OVR = overwrite/overtype, INS = insert */
202 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
203 g_string_append(stats_str, sp);
205 switch (editor_get_indent_prefs(doc->editor)->type)
207 case GEANY_INDENT_TYPE_TABS:
208 g_string_append(stats_str, _("TAB"));
209 break;
210 case GEANY_INDENT_TYPE_SPACES:
211 g_string_append(stats_str, _("SP")); /* SP = space */
212 break;
213 case GEANY_INDENT_TYPE_BOTH:
214 g_string_append(stats_str, _("T/S")); /* T/S = tabs and spaces */
215 break;
217 g_string_append(stats_str, sp);
218 g_string_append_printf(stats_str, _("mode: %s"),
219 editor_get_eol_char_name(doc->editor));
220 g_string_append(stats_str, sp);
221 g_string_append_printf(stats_str, _("encoding: %s %s"),
222 (doc->encoding) ? doc->encoding : _("unknown"),
223 (encodings_is_unicode_charset(doc->encoding)) ?
224 /* BOM = byte order mark */
225 ((doc->has_bom) ? _("(with BOM)") : "") : "");
226 g_string_append(stats_str, sp);
227 g_string_append_printf(stats_str, _("filetype: %s"), filetype_name);
228 g_string_append(stats_str, sp);
229 if (doc->changed)
231 g_string_append(stats_str, _("MOD")); /* MOD = modified */
232 g_string_append(stats_str, sp);
235 symbols_get_current_function(doc, &cur_tag);
236 g_string_append_printf(stats_str, _("scope: %s"),
237 cur_tag);
239 #ifdef GEANY_DEBUG
240 g_string_append(stats_str, sp);
241 g_string_append_printf(stats_str, "pos: %d", pos);
242 g_string_append(stats_str, sp);
243 g_string_append_printf(stats_str, "style: %d", sci_get_style_at(doc->editor->sci, pos));
244 #endif
246 /* can be overridden by status messages */
247 set_statusbar(stats_str->str, TRUE);
249 else /* no documents */
251 set_statusbar("", TRUE); /* can be overridden by status messages */
256 /* This sets the window title according to the current filename. */
257 void ui_set_window_title(GeanyDocument *doc)
259 GString *str;
260 GeanyProject *project = app->project;
262 if (doc == NULL)
263 doc = document_get_current();
265 str = g_string_new(NULL);
267 if (doc != NULL)
269 g_string_append(str, doc->changed ? "*" : "");
271 if (doc->file_name == NULL)
272 g_string_append(str, DOC_FILENAME(doc));
273 else
275 gchar *short_name = document_get_basename_for_display(doc, 30);
276 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
278 g_string_append(str, short_name);
279 g_string_append(str, " - ");
280 g_string_append(str, dirname ? dirname : "");
281 g_free(short_name);
282 g_free(dirname);
284 g_string_append(str, " - ");
286 if (project)
288 g_string_append_c(str, '[');
289 g_string_append(str, project->name);
290 g_string_append(str, "] - ");
292 g_string_append(str, "Geany");
293 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
294 g_string_free(str, TRUE);
298 void ui_set_editor_font(const gchar *font_name)
300 guint i;
302 g_return_if_fail(font_name != NULL);
304 /* do nothing if font has not changed */
305 if (interface_prefs.editor_font != NULL)
306 if (strcmp(font_name, interface_prefs.editor_font) == 0)
307 return;
309 g_free(interface_prefs.editor_font);
310 interface_prefs.editor_font = g_strdup(font_name);
312 /* We copy the current style, and update the font in all open tabs. */
313 for (i = 0; i < documents_array->len; i++)
315 if (documents[i]->editor)
317 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
321 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
325 void ui_set_fullscreen(void)
327 if (ui_prefs.fullscreen)
329 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
331 else
333 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
338 void ui_update_popup_reundo_items(GeanyDocument *doc)
340 gboolean enable_undo;
341 gboolean enable_redo;
342 guint i, len;
344 if (doc == NULL)
346 enable_undo = FALSE;
347 enable_redo = FALSE;
349 else
351 enable_undo = document_can_undo(doc);
352 enable_redo = document_can_redo(doc);
355 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
356 len = G_N_ELEMENTS(widgets.undo_items);
357 for (i = 0; i < len; i++)
359 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
361 len = G_N_ELEMENTS(widgets.redo_items);
362 for (i = 0; i < len; i++)
364 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
369 void ui_update_popup_copy_items(GeanyDocument *doc)
371 gboolean enable;
372 guint i, len;
374 if (doc == NULL)
375 enable = FALSE;
376 else
377 enable = sci_has_selection(doc->editor->sci);
379 len = G_N_ELEMENTS(widgets.popup_copy_items);
380 for (i = 0; i < len; i++)
381 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
385 void ui_update_popup_goto_items(gboolean enable)
387 guint i, len;
388 len = G_N_ELEMENTS(widgets.popup_goto_items);
389 for (i = 0; i < len; i++)
390 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
394 void ui_update_menu_copy_items(GeanyDocument *doc)
396 gboolean enable = FALSE;
397 guint i, len;
398 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
400 if (IS_SCINTILLA(focusw))
401 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
402 else
403 if (GTK_IS_EDITABLE(focusw))
404 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
405 else
406 if (GTK_IS_TEXT_VIEW(focusw))
408 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
409 GTK_TEXT_VIEW(focusw));
410 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
413 len = G_N_ELEMENTS(widgets.menu_copy_items);
414 for (i = 0; i < len; i++)
415 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
419 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
421 gboolean enable = FALSE;
423 if (doc == NULL || doc->file_type == NULL)
424 enable = FALSE;
425 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
426 enable = TRUE;
428 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
432 void ui_update_fold_items(void)
434 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
435 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
436 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
440 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
442 guint i = 0;
443 GtkWidget *tmp_menu;
444 GtkWidget *tmp_popup;
445 GtkWidget *edit_menu, *edit_menu_item;
446 GtkWidget *popup_menu, *popup_menu_item;
448 edit_menu = gtk_menu_new();
449 popup_menu = gtk_menu_new();
450 edit_menu_item = gtk_menu_item_new_with_label(label);
451 popup_menu_item = gtk_menu_item_new_with_label(label);
452 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
453 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
455 while (includes[i] != NULL)
457 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
458 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
459 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
460 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
461 g_signal_connect(tmp_menu, "activate",
462 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
463 g_signal_connect(tmp_popup, "activate",
464 G_CALLBACK(on_insert_include_activate), (gpointer) includes[i]);
465 i++;
467 gtk_widget_show_all(edit_menu_item);
468 gtk_widget_show_all(popup_menu_item);
469 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
470 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
474 void ui_create_insert_menu_items(void)
476 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
477 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
478 GtkWidget *blank;
479 const gchar *c_includes_stdlib[] = {
480 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
481 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
483 const gchar *c_includes_c99[] = {
484 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
485 "tgmath.h", "wchar.h", "wctype.h", NULL
487 const gchar *c_includes_cpp[] = {
488 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
490 const gchar *c_includes_cppstdlib[] = {
491 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
492 "memory", "locale", NULL
494 const gchar *c_includes_stl[] = {
495 "bitset", "dequev", "list", "map", "set", "queue", "stack", "vector", "algorithm",
496 "iterator", "functional", "string", "complex", "valarray", NULL
499 blank = gtk_menu_item_new_with_label("#include \"...\"");
500 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
501 gtk_widget_show(blank);
502 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate),
503 (gpointer) "blank");
504 blank = gtk_separator_menu_item_new ();
505 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
506 gtk_widget_show(blank);
508 blank = gtk_menu_item_new_with_label("#include \"...\"");
509 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
510 gtk_widget_show(blank);
511 g_signal_connect(blank, "activate", G_CALLBACK(on_insert_include_activate),
512 (gpointer) "blank");
513 blank = gtk_separator_menu_item_new();
514 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
515 gtk_widget_show(blank);
517 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
518 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
519 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
520 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
521 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
525 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
527 GtkWidget *item;
529 item = gtk_menu_item_new_with_mnemonic(label);
530 gtk_container_add(GTK_CONTAINER(me), item);
531 gtk_widget_show(item);
532 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
534 item = gtk_menu_item_new_with_mnemonic(label);
535 gtk_container_add(GTK_CONTAINER(mp), item);
536 gtk_widget_show(item);
537 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), label);
541 void ui_create_insert_date_menu_items(void)
543 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
544 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
545 GtkWidget *item;
546 gchar *str;
548 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
549 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
550 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
552 item = gtk_separator_menu_item_new();
553 gtk_container_add(GTK_CONTAINER(menu_edit), item);
554 gtk_widget_show(item);
555 item = gtk_separator_menu_item_new();
556 gtk_container_add(GTK_CONTAINER(menu_popup), item);
557 gtk_widget_show(item);
559 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
560 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
561 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
563 item = gtk_separator_menu_item_new();
564 gtk_container_add(GTK_CONTAINER(menu_edit), item);
565 gtk_widget_show(item);
566 item = gtk_separator_menu_item_new();
567 gtk_container_add(GTK_CONTAINER(menu_popup), item);
568 gtk_widget_show(item);
570 str = _("_Use Custom Date Format");
571 item = gtk_menu_item_new_with_mnemonic(str);
572 gtk_container_add(GTK_CONTAINER(menu_edit), item);
573 gtk_widget_show(item);
574 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
575 g_object_set_data_full(G_OBJECT(main_widgets.window),
576 "insert_date_custom1", g_object_ref(item), (GDestroyNotify)g_object_unref);
578 item = gtk_menu_item_new_with_mnemonic(str);
579 gtk_container_add(GTK_CONTAINER(menu_popup), item);
580 gtk_widget_show(item);
581 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), str);
582 g_object_set_data_full(G_OBJECT(main_widgets.editor_menu),
583 "insert_date_custom2", g_object_ref(item), (GDestroyNotify)g_object_unref);
585 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
589 void ui_save_buttons_toggle(gboolean enable)
591 guint i;
592 gboolean dirty_tabs = FALSE;
594 if (ui_prefs.allow_always_save)
595 return;
597 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
598 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
600 /* save all menu item and tool button */
601 for (i = 0; i < documents_array->len; i++)
603 /* check whether there are files where changes were made and if there are some,
604 * we need the save all button / item */
605 if (documents[i]->is_valid && documents[i]->changed)
607 dirty_tabs = TRUE;
608 break;
612 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
613 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
617 #define add_doc_widget(widget_name) \
618 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
620 #define add_doc_toolitem(widget_name) \
621 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
623 static void init_document_widgets(void)
625 widgets.document_buttons = g_ptr_array_new();
627 /* Cache the document-sensitive widgets so we don't have to keep looking them up
628 * when using ui_document_buttons_update(). */
629 add_doc_widget("menu_close1");
630 add_doc_widget("close_other_documents1");
631 add_doc_widget("menu_change_font1");
632 add_doc_widget("menu_close_all1");
633 add_doc_widget("menu_save1");
634 add_doc_widget("menu_save_all1");
635 add_doc_widget("menu_save_as1");
636 add_doc_widget("menu_count_words1");
637 add_doc_widget("menu_build1");
638 add_doc_widget("add_comments1");
639 add_doc_widget("menu_paste1");
640 add_doc_widget("menu_undo2");
641 add_doc_widget("preferences2");
642 add_doc_widget("menu_reload1");
643 add_doc_widget("menu_document1");
644 add_doc_widget("menu_choose_color1");
645 add_doc_widget("menu_zoom_in1");
646 add_doc_widget("menu_zoom_out1");
647 add_doc_widget("menu_view_editor1");
648 add_doc_widget("normal_size1");
649 add_doc_widget("treeview6");
650 add_doc_widget("print1");
651 add_doc_widget("menu_reload_as1");
652 add_doc_widget("menu_select_all1");
653 add_doc_widget("insert_date1");
654 add_doc_widget("menu_format1");
655 add_doc_widget("commands2");
656 add_doc_widget("menu_open_selected_file1");
657 add_doc_widget("page_setup1");
658 add_doc_widget("find1");
659 add_doc_widget("find_next1");
660 add_doc_widget("find_previous1");
661 add_doc_widget("replace1");
662 add_doc_widget("find_nextsel1");
663 add_doc_widget("find_prevsel1");
664 add_doc_widget("go_to_line1");
665 add_doc_toolitem("Close");
666 add_doc_toolitem("CloseAll");
667 add_doc_toolitem("Search");
668 add_doc_toolitem("SearchEntry");
669 add_doc_toolitem("NavBack");
670 add_doc_toolitem("NavFor");
671 add_doc_toolitem("ZoomIn");
672 add_doc_toolitem("ZoomOut");
673 add_doc_toolitem("Indent");
674 add_doc_toolitem("UnIndent");
675 add_doc_toolitem("Cut");
676 add_doc_toolitem("Copy");
677 add_doc_toolitem("Paste");
678 add_doc_toolitem("Delete");
679 add_doc_toolitem("Save");
680 add_doc_toolitem("SaveAll");
681 add_doc_toolitem("Compile");
682 add_doc_toolitem("Run");
683 add_doc_toolitem("Reload");
684 add_doc_toolitem("Color");
685 add_doc_toolitem("Goto");
686 add_doc_toolitem("GotoEntry");
687 add_doc_toolitem("Replace");
688 add_doc_toolitem("Print");
692 void ui_document_buttons_update(void)
694 guint i;
695 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) ? TRUE : FALSE;
697 for (i = 0; i < widgets.document_buttons->len; i++)
699 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
700 if (GTK_IS_ACTION(widget))
701 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
702 else
703 ui_widget_set_sensitive(widget, enable);
708 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
710 g_ptr_array_remove_fast(widgets.document_buttons, widget);
714 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
715 * when some documents are present/no documents are open.
716 * It will be removed when the widget is destroyed.
717 * @param widget The widget to add.
719 * @since 0.15
721 void ui_add_document_sensitive(GtkWidget *widget)
723 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) ? TRUE : FALSE;
725 ui_widget_set_sensitive(widget, enable);
727 g_ptr_array_add(widgets.document_buttons, widget);
728 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
732 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
734 if (show)
736 gtk_widget_show(widget);
738 else
740 gtk_widget_hide(widget);
745 void ui_sidebar_show_hide(void)
747 GtkWidget *widget;
749 /* check that there are no other notebook pages before hiding the sidebar completely
750 * other pages could be e.g. the file browser plugin */
751 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
752 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
754 ui_prefs.sidebar_visible = FALSE;
757 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
758 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
760 ignore_callback = TRUE;
761 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
762 ignore_callback = FALSE;
765 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
767 ui_widget_show_hide(gtk_notebook_get_nth_page(
768 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
769 ui_widget_show_hide(gtk_notebook_get_nth_page(
770 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
774 void ui_document_show_hide(GeanyDocument *doc)
776 const gchar *widget_name;
777 GtkWidget *item;
778 const GeanyIndentPrefs *iprefs;
780 if (doc == NULL)
781 doc = document_get_current();
783 if (doc == NULL)
784 return;
786 ignore_callback = TRUE;
788 gtk_check_menu_item_set_active(
789 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
790 doc->editor->line_wrapping);
792 gtk_check_menu_item_set_active(
793 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
794 doc->editor->line_breaking);
796 iprefs = editor_get_indent_prefs(doc->editor);
798 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
799 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
801 switch (iprefs->type)
803 case GEANY_INDENT_TYPE_SPACES:
804 widget_name = "spaces1"; break;
805 case GEANY_INDENT_TYPE_TABS:
806 widget_name = "tabs1"; break;
807 case GEANY_INDENT_TYPE_BOTH:
808 default:
809 widget_name = "tabs_and_spaces1"; break;
811 item = ui_lookup_widget(main_widgets.window, widget_name);
812 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
814 gtk_check_menu_item_set_active(
815 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
816 doc->readonly);
818 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
819 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
820 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
822 switch (sci_get_eol_mode(doc->editor->sci))
824 case SC_EOL_CR: widget_name = "cr"; break;
825 case SC_EOL_LF: widget_name = "lf"; break;
826 default: widget_name = "crlf"; break;
828 gtk_check_menu_item_set_active(
829 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
831 encodings_select_radio_item(doc->encoding);
832 filetypes_select_radio_item(doc->file_type);
834 ignore_callback = FALSE;
838 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
840 static const GdkColor red = {0, 0xffff, 0x6666, 0x6666};
841 static const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
842 static gboolean old_value = TRUE;
844 g_return_if_fail(widget != NULL);
846 /* update only if really needed */
847 if (old_value != success)
849 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, success ? NULL : &red);
850 gtk_widget_modify_text(widget, GTK_STATE_NORMAL, success ? NULL : &white);
852 old_value = success;
857 static gboolean have_tango_icon_theme(void)
859 static gboolean result = FALSE;
860 static gboolean checked = FALSE;
862 if (! checked)
864 gchar *theme_name;
866 g_object_get(G_OBJECT(gtk_settings_get_default()), "gtk-icon-theme-name", &theme_name, NULL);
867 setptr(theme_name, g_utf8_strdown(theme_name, -1));
869 result = (strstr(theme_name, "tango") != NULL);
870 checked = TRUE;
872 g_free(theme_name);
875 return result;
879 /* Note: remember to unref the pixbuf once an image or window has added a reference. */
880 GdkPixbuf *ui_new_pixbuf_from_inline(gint img)
882 switch (img)
884 case GEANY_IMAGE_LOGO:
885 return gdk_pixbuf_new_from_inline(-1, aladin_inline, FALSE, NULL);
886 break;
887 case GEANY_IMAGE_SAVE_ALL:
889 /* check whether the icon theme looks like a Gnome icon theme, if so use the
890 * old Gnome based Save All icon, otherwise assume a Tango-like icon theme */
891 if (have_tango_icon_theme())
892 return gdk_pixbuf_new_from_inline(-1, save_all_tango_inline, FALSE, NULL);
893 else
894 return gdk_pixbuf_new_from_inline(-1, save_all_gnome_inline, FALSE, NULL);
895 break;
897 case GEANY_IMAGE_CLOSE_ALL:
899 return gdk_pixbuf_new_from_inline(-1, close_all_inline, FALSE, NULL);
900 break;
902 case GEANY_IMAGE_BUILD:
904 return gdk_pixbuf_new_from_inline(-1, build_inline, FALSE, NULL);
905 break;
907 default:
908 return NULL;
913 static GdkPixbuf *ui_new_pixbuf_from_stock(const gchar *stock_id)
915 if (utils_str_equal(stock_id, GEANY_STOCK_CLOSE_ALL))
916 return ui_new_pixbuf_from_inline(GEANY_IMAGE_CLOSE_ALL);
917 else if (utils_str_equal(stock_id, GEANY_STOCK_BUILD))
918 return ui_new_pixbuf_from_inline(GEANY_IMAGE_BUILD);
919 else if (utils_str_equal(stock_id, GEANY_STOCK_SAVE_ALL))
920 return ui_new_pixbuf_from_inline(GEANY_IMAGE_SAVE_ALL);
922 return NULL;
926 GtkWidget *ui_new_image_from_inline(gint img)
928 GtkWidget *wid;
929 GdkPixbuf *pb;
931 pb = ui_new_pixbuf_from_inline(img);
932 wid = gtk_image_new_from_pixbuf(pb);
933 g_object_unref(pb); /* the image doesn't adopt our reference, so remove our ref. */
934 return wid;
938 static void recent_create_menu(GeanyRecentFiles *grf)
940 GtkWidget *tmp;
941 guint i, len;
942 gchar *filename;
944 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
945 for (i = 0; i < len; i++)
947 filename = g_queue_peek_nth(grf->recent_queue, i);
948 /* create menu item for the recent files menu in the menu bar */
949 tmp = gtk_menu_item_new_with_label(filename);
950 gtk_widget_show(tmp);
951 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
952 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
953 /* create menu item for the recent files menu in the toolbar */
954 if (grf->toolbar != NULL)
956 tmp = gtk_menu_item_new_with_label(filename);
957 gtk_widget_show(tmp);
958 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
959 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
965 static GeanyRecentFiles *recent_get_recent_files(void)
967 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
969 if (G_UNLIKELY(grf.recent_queue == NULL))
971 grf.recent_queue = ui_prefs.recent_queue;
972 grf.menubar = ui_widgets.recent_files_menu_menubar;
973 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
974 toolbar_get_action_by_name("Open")));
975 grf.activate_cb = recent_file_activate_cb;
977 return &grf;
981 static GeanyRecentFiles *recent_get_recent_projects(void)
983 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
985 if (G_UNLIKELY(grf.recent_queue == NULL))
987 grf.recent_queue = ui_prefs.recent_projects_queue;
988 grf.menubar = ui_widgets.recent_projects_menu_menubar;
989 grf.toolbar = NULL;
990 grf.activate_cb = recent_project_activate_cb;
992 return &grf;
996 void ui_create_recent_menus(void)
998 recent_create_menu(recent_get_recent_files());
999 recent_create_menu(recent_get_recent_projects());
1003 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1005 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1006 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1008 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1009 recent_file_loaded(utf8_filename, recent_get_recent_files());
1011 g_free(locale_filename);
1012 g_free(utf8_filename);
1016 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1018 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1019 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1021 if (project_ask_close() && project_load_file_with_session(locale_filename))
1022 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1024 g_free(locale_filename);
1025 g_free(utf8_filename);
1029 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf)
1031 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1033 #if GTK_CHECK_VERSION(2, 10, 0)
1034 if (grf->type == RECENT_FILE_FILE)
1036 GtkRecentManager *manager = gtk_recent_manager_get_default();
1037 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1038 if (uri != NULL)
1040 gtk_recent_manager_add_item(manager, uri);
1041 g_free(uri);
1044 #endif
1045 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1046 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1048 g_free(g_queue_pop_tail(grf->recent_queue));
1050 update_recent_menu(grf);
1052 /* filename already in recent list */
1053 else
1054 recent_file_loaded(utf8_filename, grf);
1058 void ui_add_recent_file(const gchar *utf8_filename)
1060 add_recent_file(utf8_filename, recent_get_recent_files());
1064 void ui_add_recent_project_file(const gchar *utf8_filename)
1066 add_recent_file(utf8_filename, recent_get_recent_projects());
1070 /* Returns: newly allocated string with the UTF-8 menu text. */
1071 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1073 const gchar *text = NULL;
1075 if (GTK_BIN(menu_item)->child)
1077 GtkWidget *child = GTK_BIN(menu_item)->child;
1079 if (GTK_IS_LABEL(child))
1080 text = gtk_label_get_text(GTK_LABEL(child));
1082 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1083 return g_strdup(text);
1087 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1089 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1090 gint result;
1092 if (utils_str_equal(menu_text, user_data))
1093 result = 0;
1094 else
1095 result = 1;
1097 g_free(menu_text);
1098 return result;
1102 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1104 GList *item, *children;
1105 void *data;
1106 GtkWidget *tmp;
1108 /* first reorder the queue */
1109 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1110 g_return_if_fail(item != NULL);
1112 data = item->data;
1113 g_queue_remove(grf->recent_queue, data);
1114 g_queue_push_head(grf->recent_queue, data);
1116 /* remove the old menuitem for the filename */
1117 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1118 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1119 if (item != NULL)
1120 gtk_widget_destroy(GTK_WIDGET(item->data));
1121 g_list_free(children);
1123 if (grf->toolbar != NULL)
1125 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1126 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1127 if (item != NULL)
1128 gtk_widget_destroy(GTK_WIDGET(item->data));
1129 g_list_free(children);
1131 /* now prepend a new menuitem for the filename,
1132 * first for the recent files menu in the menu bar */
1133 tmp = gtk_menu_item_new_with_label(utf8_filename);
1134 gtk_widget_show(tmp);
1135 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1136 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1137 /* then for the recent files menu in the tool bar */
1138 if (grf->toolbar != NULL)
1140 tmp = gtk_menu_item_new_with_label(utf8_filename);
1141 gtk_widget_show(tmp);
1142 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1143 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1144 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal which we need in
1145 * GeanyMenubuttonAction */
1146 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1147 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1152 static void update_recent_menu(GeanyRecentFiles *grf)
1154 GtkWidget *tmp;
1155 gchar *filename;
1156 GList *children, *item;
1158 filename = g_queue_peek_head(grf->recent_queue);
1160 /* clean the MRU list before adding an item (menubar) */
1161 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1162 if (g_list_length(children) > file_prefs.mru_length - 1)
1164 item = g_list_nth(children, file_prefs.mru_length - 1);
1165 while (item != NULL)
1167 if (GTK_IS_MENU_ITEM(item->data))
1168 gtk_widget_destroy(GTK_WIDGET(item->data));
1169 item = g_list_next(item);
1172 g_list_free(children);
1174 /* create item for the menu bar menu */
1175 tmp = gtk_menu_item_new_with_label(filename);
1176 gtk_widget_show(tmp);
1177 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1178 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1180 /* clean the MRU list before adding an item (toolbar) */
1181 if (grf->toolbar != NULL)
1183 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1184 if (g_list_length(children) > file_prefs.mru_length - 1)
1186 item = g_list_nth(children, file_prefs.mru_length - 1);
1187 while (item != NULL)
1189 if (GTK_IS_MENU_ITEM(item->data))
1190 gtk_widget_destroy(GTK_WIDGET(item->data));
1191 item = g_list_next(item);
1194 g_list_free(children);
1196 /* create item for the tool bar menu */
1197 tmp = gtk_menu_item_new_with_label(filename);
1198 gtk_widget_show(tmp);
1199 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1200 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1201 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1206 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1208 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1209 GeanyDocument *doc;
1211 for (i = 0; i < max; i++)
1213 doc = document_get_from_page(i);
1215 switch (feature)
1217 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1218 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1219 break;
1220 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1221 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
1222 break;
1223 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1224 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1225 break;
1226 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1227 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1228 break;
1229 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1230 editor_set_indentation_guides(doc->editor);
1231 break;
1237 void ui_update_view_editor_menu_items(void)
1239 ignore_callback = TRUE;
1240 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1241 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1242 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);
1243 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);
1244 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);
1245 ignore_callback = FALSE;
1249 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1250 * @param label_text The label text.
1251 * @param alignment An address to store the alignment widget pointer.
1252 * @return The frame widget, setting the alignment container for packing child widgets. */
1253 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1255 GtkWidget *label, *align;
1256 GtkWidget *frame = gtk_frame_new(NULL);
1258 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1260 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1261 gtk_container_add(GTK_CONTAINER(frame), align);
1262 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1264 label = ui_label_new_bold(label_text);
1265 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1267 *alignment = align;
1268 return frame;
1272 /** Makes a fixed border for dialogs without increasing the button box border.
1273 * @param dialog The parent container for the @c GtkVBox.
1274 * @return The packed @c GtkVBox. */
1275 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1277 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1279 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1280 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1281 return vbox;
1285 /** Creates a @c GtkButton with custom text and a stock image similar to
1286 * @c gtk_button_new_from_stock().
1287 * @param stock_id A @c GTK_STOCK_NAME string.
1288 * @param text Button label text, can include mnemonics.
1289 * @return The new @c GtkButton.
1291 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1293 GtkWidget *image, *button;
1295 button = gtk_button_new_with_mnemonic(text);
1296 gtk_widget_show(button);
1297 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1298 gtk_button_set_image(GTK_BUTTON(button), image);
1299 /* note: image is shown by gtk */
1300 return button;
1304 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1305 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1306 * @param label Menu item label, can include mnemonics.
1307 * @return The new @c GtkImageMenuItem.
1309 * @since 0.16
1311 GtkWidget *
1312 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1314 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1315 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1317 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1318 gtk_widget_show(image);
1319 return item;
1323 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1324 GdkEvent *event, gpointer data)
1326 if (event->button.button == 1 && icon_pos == 1)
1328 gtk_entry_set_text(entry, "");
1329 gtk_widget_grab_focus(GTK_WIDGET(entry));
1334 /** Adds a small clear icon to the right end of the passed @a entry.
1335 * A callback to clear the contents of the GtkEntry is automatically added.
1337 * This feature is only available with GTK 2.16 but implemented as a runtime check,
1338 * so it is safe to just use this function, if the code is ran with older versions,
1339 * nothing happens. If ran with GTK 2.16 or newer, the icon is displayed.
1341 * @param entry The GtkEntry object to which the icon should be attached.
1343 * @since 0.16
1345 void ui_entry_add_clear_icon(GtkEntry *entry)
1347 if (gtk_check_version(2, 15, 2) == NULL)
1349 g_object_set(entry, "secondary-icon-stock", "gtk-clear", NULL);
1350 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1355 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1357 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1358 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1362 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1363 * the width of each button box's children.
1364 * Should be called after all child widgets have been packed. */
1365 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1367 GtkSizeGroup *size_group;
1369 gtk_box_set_spacing(GTK_BOX(copy), 10);
1370 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1372 /* now we need to put the widest widget from each button box in a size group,
1373 * but we don't know the width before they are drawn, and for different label
1374 * translations the widest widget can vary, so we just add all widgets. */
1375 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1376 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1377 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1378 g_object_unref(size_group);
1382 /* Prepends the active text to the drop down list, unless the first element in
1383 * the list is identical, ensuring there are <= history_len elements. */
1384 void ui_combo_box_add_to_history(GtkComboBox *combo, const gchar *text)
1386 const gint history_len = 30;
1387 GtkTreeModel *model;
1388 GtkTreeIter iter;
1389 gchar *combo_text;
1390 gboolean equal = FALSE;
1391 GtkTreePath *path;
1393 model = gtk_combo_box_get_model(combo);
1394 if (gtk_tree_model_get_iter_first(model, &iter))
1396 gtk_tree_model_get(model, &iter, 0, &combo_text, -1);
1397 equal = utils_str_equal(combo_text, text);
1398 g_free(combo_text);
1400 if (equal) return; /* don't prepend duplicate */
1402 gtk_combo_box_prepend_text(combo, text);
1404 /* limit history */
1405 path = gtk_tree_path_new_from_indices(history_len, -1);
1406 if (gtk_tree_model_get_iter(model, &iter, path))
1408 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1410 gtk_tree_path_free(path);
1414 /* Same as gtk_combo_box_prepend_text(), except that text is only prepended if it not already
1415 * exists in the combo's model. */
1416 void ui_combo_box_prepend_text_once(GtkComboBox *combo, const gchar *text)
1418 GtkTreeModel *model;
1419 GtkTreeIter iter;
1420 gchar *combo_text;
1421 gboolean found = FALSE;
1423 model = gtk_combo_box_get_model(combo);
1424 if (gtk_tree_model_get_iter_first(model, &iter))
1428 gtk_tree_model_get(model, &iter, 0, &combo_text, -1);
1429 found = utils_str_equal(combo_text, text);
1430 g_free(combo_text);
1432 while (!found && gtk_tree_model_iter_next(model, &iter));
1434 if (found)
1435 return; /* don't prepend duplicate */
1437 gtk_combo_box_prepend_text(combo, text);
1441 /* Changes the color of the notebook tab text and open files items according to
1442 * document status. */
1443 void ui_update_tab_status(GeanyDocument *doc)
1445 const GdkColor *color = document_get_status_color(doc);
1447 /* NULL color will reset to default */
1448 gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_NORMAL, color);
1449 gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_ACTIVE, color);
1451 sidebar_openfiles_update(doc);
1455 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1456 gboolean down)
1458 GtkTreePath *path;
1459 gboolean result;
1461 if (down)
1462 return gtk_tree_model_iter_next(model, iter);
1464 path = gtk_tree_model_get_path(model, iter);
1465 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1466 gtk_tree_path_free(path);
1467 return result;
1471 /* note: the while loop might be more efficient when searching upwards if it
1472 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1473 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1475 GtkTreeSelection *treesel;
1476 GtkTreeIter iter;
1477 GtkTreeModel *model;
1479 treesel = gtk_tree_view_get_selection(treeview);
1480 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1482 /* get the next selected item */
1483 if (! tree_model_iter_get_next(model, &iter, down))
1484 return FALSE; /* no more items */
1486 else /* no selection */
1488 if (! gtk_tree_model_get_iter_first(model, &iter))
1489 return TRUE; /* no items */
1491 while (TRUE)
1493 gtk_tree_selection_select_iter(treesel, &iter);
1494 if (cb(0))
1495 break; /* found next message */
1497 if (! tree_model_iter_get_next(model, &iter, down))
1498 return FALSE; /* no more items */
1500 /* scroll item in view */
1501 if (ui_prefs.msgwindow_visible)
1503 GtkTreePath *path = gtk_tree_model_get_path(
1504 gtk_tree_view_get_model(treeview), &iter);
1506 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1507 gtk_tree_path_free(path);
1509 return TRUE;
1513 /* Returns FALSE if the treeview has items but no matching next item. */
1514 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1516 return tree_view_find(treeview, cb, TRUE);
1520 /* Returns FALSE if the treeview has items but no matching next item. */
1521 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1523 return tree_view_find(treeview, cb, FALSE);
1528 * Modifies the font of a widget using gtk_widget_modify_font().
1530 * @param widget The widget.
1531 * @param str The font name as expected by pango_font_description_from_string().
1533 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1535 PangoFontDescription *pfd;
1537 pfd = pango_font_description_from_string(str);
1538 gtk_widget_modify_font(widget, pfd);
1539 pango_font_description_free(pfd);
1543 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1544 * file chooser, replacing entry text (if successful) with the path returned from the
1545 * @c GtkFileChooser.
1546 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1547 * @param title The file chooser dialog title, or @c NULL.
1548 * @param action The mode of the file chooser.
1549 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1550 * such as @c GtkComboBoxEntry.
1551 * @return The @c GtkHBox.
1553 /* @see ui_setup_open_button_callback(). */
1554 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1556 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1558 hbox = gtk_hbox_new(FALSE, 6);
1559 path_entry = GTK_WIDGET(entry);
1561 /* prevent path_entry being vertically stretched to the height of dirbtn */
1562 vbox = gtk_vbox_new(FALSE, 0);
1563 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1565 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1567 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1569 else
1570 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1572 dirbtn = gtk_button_new();
1573 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1574 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1575 ui_setup_open_button_callback(dirbtn, title, action, entry);
1577 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1578 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1579 return hbox;
1583 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1586 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1587 * title can be NULL.
1588 * action is the file chooser mode to use. */
1589 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1590 GtkFileChooserAction action, GtkEntry *entry)
1592 GtkWidget *path_entry = GTK_WIDGET(entry);
1594 if (title)
1595 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1596 (GDestroyNotify) g_free);
1597 g_object_set_data(G_OBJECT(open_btn), "action", (gpointer) action);
1598 g_object_set_data_full(G_OBJECT(open_btn), "entry",
1599 g_object_ref(path_entry), (GDestroyNotify) g_object_unref);
1600 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), open_btn);
1604 #ifndef G_OS_WIN32
1605 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1606 const gchar *utf8_path)
1608 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1609 GTK_WINDOW(main_widgets.window), action,
1610 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1611 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1612 gchar *locale_path;
1613 gchar *ret_path = NULL;
1615 gtk_widget_set_name(dialog, "GeanyDialog");
1616 locale_path = utils_get_locale_from_utf8(utf8_path);
1617 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1619 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1620 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1622 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1624 if (g_path_is_absolute(locale_path))
1625 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1627 g_free(locale_path);
1629 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1631 gchar *dir_locale;
1633 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1634 ret_path = utils_get_utf8_from_locale(dir_locale);
1635 g_free(dir_locale);
1637 gtk_widget_destroy(dialog);
1638 return ret_path;
1640 #endif
1643 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1645 GtkWidget *path_box = GTK_WIDGET(user_data);
1646 GtkFileChooserAction action =
1647 (GtkFileChooserAction) g_object_get_data(G_OBJECT(path_box), "action");
1648 GtkEntry *entry =
1649 (GtkEntry *) g_object_get_data(G_OBJECT(path_box), "entry");
1650 const gchar *title = g_object_get_data(G_OBJECT(path_box), "title");
1651 gchar *utf8_path = NULL;
1653 /* TODO: extend for other actions */
1654 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1655 action == GTK_FILE_CHOOSER_ACTION_OPEN);
1657 if (title == NULL)
1658 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1659 _("Select Folder") : _("Select File");
1661 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1663 #ifdef G_OS_WIN32
1664 utf8_path = win32_show_file_dialog(GTK_WINDOW(ui_widgets.prefs_dialog), title,
1665 gtk_entry_get_text(GTK_ENTRY(entry)));
1666 #else
1667 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1668 #endif
1670 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1672 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
1673 #ifdef G_OS_WIN32
1674 utf8_path = win32_show_folder_dialog(ui_widgets.prefs_dialog, title,
1675 gtk_entry_get_text(GTK_ENTRY(entry)));
1676 #else
1677 utf8_path = run_file_chooser(title, action, path);
1678 #endif
1679 g_free(path);
1682 if (utf8_path != NULL)
1684 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
1685 g_free(utf8_path);
1690 void ui_statusbar_showhide(gboolean state)
1692 /* handle statusbar visibility */
1693 if (state)
1695 gtk_widget_show(ui_widgets.statusbar);
1696 ui_update_statusbar(NULL, -1);
1698 else
1699 gtk_widget_hide(ui_widgets.statusbar);
1703 /** Packs all @c GtkWidgets passed after the row argument into a table, using
1704 * one widget per cell. The first widget is not expanded as the table grows,
1705 * as this is usually a label.
1706 * @param table
1707 * @param row The row number of the table.
1709 void ui_table_add_row(GtkTable *table, gint row, ...)
1711 va_list args;
1712 gint i;
1713 GtkWidget *widget;
1715 va_start(args, row);
1716 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
1718 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
1720 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
1721 options, 0, 0, 0);
1723 va_end(args);
1727 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
1729 const gchar *file_name = user_data;
1730 GeanyFiletype *ft = NULL;
1732 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
1733 ft = filetypes[GEANY_FILETYPES_CONF];
1735 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
1736 document_open_file(file_name, FALSE, ft, NULL);
1737 else
1739 gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
1740 gchar *base_name = g_path_get_basename(file_name);
1741 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
1742 gchar *global_content = NULL;
1744 /* if the requested file doesn't exist in the user's config dir, try loading the file
1745 * from the global data directory and use its contents for the newly created file */
1746 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
1747 g_file_get_contents(global_file, &global_content, NULL, NULL);
1749 document_new_file(utf8_filename, ft, global_content);
1751 utils_free_pointers(4, utf8_filename, base_name, global_file, global_content, NULL);
1756 /* @note You should connect to the "document-save" signal yourself to detect
1757 * if the user has just saved the config file, reloading it. */
1758 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
1760 GtkWidget *item;
1762 if (!parent)
1763 parent = GTK_CONTAINER(widgets.config_files_menu);
1765 if (!label)
1767 gchar *base_name;
1769 base_name = g_path_get_basename(real_path);
1770 item = gtk_menu_item_new_with_label(base_name);
1771 g_free(base_name);
1773 else
1774 item = gtk_menu_item_new_with_mnemonic(label);
1776 gtk_widget_show(item);
1777 gtk_container_add(parent, item);
1778 g_signal_connect(item, "activate", G_CALLBACK(on_config_file_clicked),
1779 /* this memory is kept */
1780 g_strdup(real_path));
1784 static gboolean sort_menu(gpointer data)
1786 ui_menu_sort_by_label(GTK_MENU(data));
1787 return FALSE;
1791 static void create_config_files_menu(void)
1793 GtkWidget *menu, *item;
1795 widgets.config_files_menu = menu = gtk_menu_new();
1797 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
1798 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
1800 /* sort menu after all items added */
1801 g_idle_add(sort_menu, widgets.config_files_menu);
1805 void ui_init_stock_items(void)
1807 GtkIconSet *icon_set;
1808 GtkIconFactory *factory = gtk_icon_factory_new();
1809 GdkPixbuf *pb;
1810 gsize i, len;
1811 GtkStockItem items[] =
1813 { GEANY_STOCK_SAVE_ALL, _("Save All"), 0, 0, GETTEXT_PACKAGE },
1814 { GEANY_STOCK_CLOSE_ALL, _("Close All"), 0, 0, GETTEXT_PACKAGE },
1815 { GEANY_STOCK_BUILD, _("Build"), 0, 0, GETTEXT_PACKAGE }
1818 len = G_N_ELEMENTS(items);
1819 for (i = 0; i < len; i++)
1821 pb = ui_new_pixbuf_from_stock(items[i].stock_id);
1822 icon_set = gtk_icon_set_new_from_pixbuf(pb);
1824 gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
1826 gtk_icon_set_unref(icon_set);
1827 g_object_unref(pb);
1829 gtk_stock_add((GtkStockItem *) items, len);
1830 gtk_icon_factory_add_default(factory);
1831 g_object_unref(factory);
1835 void ui_init_toolbar_widgets(void)
1837 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
1838 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
1839 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
1840 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
1844 void ui_swap_sidebar_pos(void)
1846 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
1847 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
1848 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
1849 GtkWidget *box = ui_lookup_widget(main_widgets.window, "vbox1");
1851 /* reparenting avoids scintilla problem with middle click paste */
1852 gtk_widget_reparent(left, box);
1853 gtk_widget_reparent(right, box);
1854 gtk_widget_reparent(right, pane);
1855 gtk_widget_reparent(left, pane);
1857 gtk_paned_set_position(GTK_PANED(pane), pane->allocation.width
1858 - gtk_paned_get_position(GTK_PANED(pane)));
1862 static void init_recent_files(void)
1864 GtkWidget *toolbar_recent_files_menu;
1866 /* add recent files to the File menu */
1867 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
1868 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
1869 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
1870 ui_widgets.recent_files_menu_menubar);
1872 /* add recent files to the toolbar Open button */
1873 toolbar_recent_files_menu = gtk_menu_new();
1874 g_object_ref(toolbar_recent_files_menu);
1875 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
1876 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
1880 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
1882 g_object_ref(menu);
1883 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
1884 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
1885 g_object_unref(menu);
1889 static void on_editor_menu_show(GtkWidget *item)
1891 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, "commands1");
1892 GtkWidget *bar = ui_lookup_widget(main_widgets.window, "commands2");
1894 ui_menu_move(widgets.commands_menu, bar, popup);
1896 popup = ui_lookup_widget(main_widgets.editor_menu, "menu_format2");
1897 bar = ui_lookup_widget(main_widgets.window, "menu_format1");
1898 ui_menu_move(widgets.format_menu, bar, popup);
1902 static void on_editor_menu_hide(GtkWidget *item)
1904 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, "commands1");
1905 GtkWidget *bar = ui_lookup_widget(main_widgets.window, "commands2");
1907 ui_menu_move(widgets.commands_menu, popup, bar);
1909 popup = ui_lookup_widget(main_widgets.editor_menu, "menu_format2");
1910 bar = ui_lookup_widget(main_widgets.window, "menu_format1");
1911 ui_menu_move(widgets.format_menu, popup, bar);
1915 void ui_init(void)
1917 GtkWidget *item;
1919 init_recent_files();
1921 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
1922 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
1924 main_widgets.progressbar = progress_bar_create();
1926 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition1");
1927 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_declaration1");
1928 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage1");
1929 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage1");
1930 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
1931 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
1932 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
1933 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
1934 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
1935 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
1936 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
1937 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
1938 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
1939 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
1940 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
1941 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
1942 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
1943 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
1945 item = ui_lookup_widget(main_widgets.window, "menu_format1");
1946 widgets.format_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
1947 item = ui_lookup_widget(main_widgets.window, "commands2");
1948 widgets.commands_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
1950 /* reparent edit submenus as needed */
1951 item = main_widgets.editor_menu;
1952 g_signal_connect(item, "show", G_CALLBACK(on_editor_menu_show), NULL);
1953 g_signal_connect(item, "hide", G_CALLBACK(on_editor_menu_hide), NULL);
1955 ui_init_toolbar_widgets();
1956 init_document_widgets();
1957 create_config_files_menu();
1961 static void auto_separator_update(GeanyAutoSeparator *autosep)
1963 g_return_if_fail(autosep->ref_count >= 0);
1965 if (autosep->widget)
1966 ui_widget_show_hide(autosep->widget, autosep->ref_count > 0);
1970 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
1972 GeanyAutoSeparator *autosep = user_data;
1974 if (GTK_WIDGET_VISIBLE(widget))
1975 autosep->ref_count++;
1976 else
1977 autosep->ref_count--;
1978 auto_separator_update(autosep);
1982 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
1984 GeanyAutoSeparator *autosep = user_data;
1986 /* GTK_WIDGET_VISIBLE won't work now the widget is being destroyed,
1987 * so assume widget was visible */
1988 autosep->ref_count--;
1989 autosep->ref_count = MAX(autosep->ref_count, 0);
1990 auto_separator_update(autosep);
1994 /* Show the separator widget if @a item or another is visible. */
1995 /* Note: This would be neater taking a widget argument, setting a "visible-count"
1996 * property, and using reference counting to keep the widget alive whilst its visible group
1997 * is alive. */
1998 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2000 /* set widget ptr NULL when widget destroyed */
2001 if (autosep->ref_count == 0)
2002 g_signal_connect(autosep->widget, "destroy",
2003 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2005 if (GTK_WIDGET_VISIBLE(item))
2007 autosep->ref_count++;
2008 auto_separator_update(autosep);
2010 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2011 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2012 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2017 * Sets @a text as the contents of the tooltip for @a widget.
2019 * @param widget The widget the tooltip should be set for.
2020 * @param text The text for the tooltip.
2022 * @since 0.16
2024 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2026 #if GTK_CHECK_VERSION(2, 12, 0)
2027 gtk_widget_set_tooltip_text(widget, text);
2028 #else
2029 static GtkTooltips *tooltips = NULL;
2031 if (G_UNLIKELY(tooltips == NULL))
2032 tooltips = GTK_TOOLTIPS(ui_lookup_widget(main_widgets.window, "tooltips"));
2034 gtk_tooltips_set_tip(tooltips, widget, text, NULL);
2035 #endif
2039 /** Returns a widget from a name in a component, usually created by Glade.
2040 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2041 * or alternatively any widget in the component, and the name of the widget
2042 * you want returned.
2043 * @param widget Widget with the @a widget_name property set.
2044 * @param widget_name Name to lookup.
2045 * @return The widget found.
2046 * @see ui_hookup_widget().
2048 * @since 0.16
2050 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2052 GtkWidget *parent, *found_widget;
2054 g_return_val_if_fail(widget != NULL, NULL);
2055 g_return_val_if_fail(widget_name != NULL, NULL);
2057 for (;;)
2059 if (GTK_IS_MENU(widget))
2060 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2061 else
2062 parent = widget->parent;
2063 if (parent == NULL)
2064 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2065 if (parent == NULL)
2066 break;
2067 widget = parent;
2070 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2071 if (G_UNLIKELY(found_widget == NULL))
2072 g_warning("Widget not found: %s", widget_name);
2073 return found_widget;
2077 /* Progress Bar */
2078 static guint progress_bar_timer_id = (guint) -1;
2081 static GtkWidget *progress_bar_create(void)
2083 GtkWidget *bar = gtk_progress_bar_new();
2085 /* Set the progressbar's height to 1 to fit it in the statusbar */
2086 gtk_widget_set_size_request(bar, -1, 1);
2087 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2089 return bar;
2093 static gboolean progress_bar_pulse(gpointer data)
2095 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2097 return TRUE;
2102 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2103 * (if the statusbar is visible). This is a convenience function which adds a timer to
2104 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2105 * You can use this function when you have time consuming asynchronous operation and want to
2106 * display some activity in the GUI and when you don't know about detailed progress steps.
2107 * The progressbar widget is hidden by default when it is not active. This function and
2108 * ui_progress_bar_stop() will show and hide it automatically for you.
2110 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2111 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2112 * In this case, you need to show and hide the widget yourself. You can find some example code
2113 * in @c src/printing.c.
2115 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2117 * @since 0.16
2119 void ui_progress_bar_start(const gchar *text)
2121 g_return_if_fail(progress_bar_timer_id == (guint) -1);
2123 if (! interface_prefs.statusbar_visible)
2124 return;
2126 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2128 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2130 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2134 /** Stops a running progress bar and hides the widget again.
2136 * @since 0.16
2138 void ui_progress_bar_stop(void)
2140 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2142 if (progress_bar_timer_id != (guint) -1)
2144 g_source_remove(progress_bar_timer_id);
2145 progress_bar_timer_id = (guint) -1;
2150 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2152 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2153 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2154 gchar *sa, *sb;
2155 gint result;
2157 sa = ui_menu_item_get_text(item_a);
2158 sb = ui_menu_item_get_text(item_b);
2159 result = utils_str_casecmp(sa, sb);
2160 g_free(sa);
2161 g_free(sb);
2162 return result;
2166 /* Currently @a menu should contain only GtkMenuItems with labels. */
2167 void ui_menu_sort_by_label(GtkMenu *menu)
2169 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2170 GList *node;
2171 gint pos;
2173 list = g_list_sort(list, compare_menu_item_labels);
2174 pos = 0;
2175 foreach_list(node, list)
2177 gtk_menu_reorder_child(menu, node->data, pos);
2178 pos++;
2180 g_list_free(list);
2184 /* return value is for macros */
2185 GtkWidget *ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2187 va_list a;
2188 gchar *text;
2190 va_start(a, format);
2191 text = g_strdup_vprintf(format, a);
2192 va_end(a);
2194 gtk_label_set_text(label, text);
2195 gtk_label_set_use_markup(label, TRUE);
2196 g_free(text);
2197 return GTK_WIDGET(label);
2201 GtkWidget *ui_label_new_bold(const gchar *text)
2203 GtkWidget *label;
2204 gchar *label_text;
2206 label_text = g_markup_escape_text(text, -1);
2207 label = ui_label_set_markup(GTK_LABEL(gtk_label_new(NULL)), "<b>%s</b>", label_text);
2208 g_free(label_text);
2209 return label;
2213 /** Adds a list of document items to @a menu.
2214 * @param menu Menu.
2215 * @param active Which document to highlight, or @c NULL.
2216 * @param callback is used for each menu item's @c "activate" signal and will be passed
2217 * the corresponding document pointer as @c user_data.
2218 * @warning You should check @c doc->is_valid in the callback.
2219 * @since 0.19 */
2220 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2222 GtkWidget *menu_item, *menu_item_label;
2223 const GdkColor *color;
2224 GeanyDocument *doc;
2225 guint i, len;
2226 gchar *base_name, *label;
2228 len = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2229 for (i = 0; i < len; i++)
2231 doc = document_get_from_page(i);
2232 if (! DOC_VALID(doc))
2233 continue;
2235 base_name = g_path_get_basename(DOC_FILENAME(doc));
2236 menu_item = gtk_menu_item_new_with_label(base_name);
2237 gtk_widget_show(menu_item);
2238 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2239 g_signal_connect(menu_item, "activate", callback, doc);
2241 color = document_get_status_color(doc);
2242 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2243 gtk_widget_modify_fg(menu_item_label, GTK_STATE_NORMAL, color);
2244 gtk_widget_modify_fg(menu_item_label, GTK_STATE_ACTIVE, color);
2246 if (doc == active)
2248 label = g_markup_escape_text(base_name, -1);
2249 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", label);
2250 g_free(label);
2253 g_free(base_name);
2258 /** Checks whether the passed @a keyval is the Enter or Return key.
2259 * There are three different Enter/Return key values
2260 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2261 * This is just a convenience function.
2262 * @param keyval A keyval.
2263 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2264 * @since 0.19 */
2265 gboolean ui_is_keyval_enter_or_return(guint keyval)
2267 return (keyval == GDK_Return || keyval == GDK_ISO_Enter|| keyval == GDK_KP_Enter);
2271 /** Reads an integer from the GTK default settings registry
2272 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2273 * @param property_name The property to read.
2274 * @param default_value The default value in case the value could not be read.
2275 * @return The value for the property if it exists, otherwise the @a default_value.
2276 * @since 0.19 */
2277 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
2279 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2280 gtk_settings_get_default())), property_name))
2282 gint value;
2283 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
2284 return value;
2286 else
2287 return default_value;
2291 void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
2292 gint new_text_len, gint *position, gpointer data)
2294 gboolean stop_signal = FALSE;
2295 const gchar c = *new_text;
2297 /* allow inserting '+' and '-' as the first character */
2298 if (position != NULL && *position == 0)
2300 if (c != '+' && c != '-' && ! isdigit(c))
2301 stop_signal = TRUE;
2303 /* don't insert any text when it is not a digit */
2304 else if (! isdigit(c))
2305 stop_signal = TRUE;
2307 if (stop_signal)
2308 g_signal_stop_emission_by_name(editable, "insert-text");