Try to fix Gtk warning when using Tools->Reload Configuration.
[geany-mirror.git] / src / ui_utils.c
bloba8d35e0aa169b7514744e3b2b4fad4744bb44a70
1 /*
2 * ui_utils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2009 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>
32 #include "ui_utils.h"
33 #include "prefs.h"
34 #include "sciwrappers.h"
35 #include "document.h"
36 #include "documentprivate.h"
37 #include "filetypes.h"
38 #include "support.h"
39 #include "msgwindow.h"
40 #include "utils.h"
41 #include "callbacks.h"
42 #include "encodings.h"
43 #include "images.c"
44 #include "sidebar.h"
45 #include "win32.h"
46 #include "project.h"
47 #include "editor.h"
48 #include "plugins.h"
49 #include "symbols.h"
50 #include "toolbar.h"
51 #include "geanymenubuttonaction.h"
54 GeanyInterfacePrefs interface_prefs;
55 GeanyMainWidgets main_widgets;
57 UIPrefs ui_prefs;
58 UIWidgets ui_widgets;
60 static struct
62 /* pointers to widgets only sensitive when there is at least one document, the pointers can
63 * also be GtkAction objects, so check each pointer before using it */
64 GPtrArray *document_buttons;
65 GtkWidget *menu_insert_include_items[2];
66 GtkWidget *popup_goto_items[4];
67 GtkWidget *popup_copy_items[3];
68 GtkWidget *menu_copy_items[3];
69 GtkWidget *redo_items[3];
70 GtkWidget *undo_items[3];
71 GtkWidget *save_buttons[4];
72 GtkWidget *config_files_menu;
73 GtkWidget *commands_menu;
74 GtkWidget *format_menu;
76 widgets;
78 enum
80 RECENT_FILE_FILE,
81 RECENT_FILE_PROJECT
84 typedef struct
86 gint type;
87 GQueue *recent_queue;
88 GtkWidget *menubar;
89 GtkWidget *toolbar;
90 void (*activate_cb)(GtkMenuItem *, gpointer);
91 } GeanyRecentFiles;
94 static void update_recent_menu(GeanyRecentFiles *grf);
95 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
96 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
97 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
98 static GtkWidget *progress_bar_create(void);
101 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
102 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
104 if (widget != NULL)
105 gtk_widget_set_sensitive(widget, set);
109 /* allow_override is TRUE if text can be ignored when another message has been set
110 * that didn't use allow_override and has not timed out. */
111 static void set_statusbar(const gchar *text, gboolean allow_override)
113 static glong last_time = 0;
114 GTimeVal timeval;
115 const gint GEANY_STATUS_TIMEOUT = 1;
117 if (! interface_prefs.statusbar_visible)
118 return; /* just do nothing if statusbar is not visible */
120 g_get_current_time(&timeval);
122 if (! allow_override)
124 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), 1);
125 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), 1, text);
126 last_time = timeval.tv_sec;
128 else
129 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
131 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), 1);
132 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), 1, text);
137 /** Display text on the statusbar.
138 * @param log Whether the message should be recorded in the Status window.
139 * @param format A @c printf -style string. */
140 void ui_set_statusbar(gboolean log, const gchar *format, ...)
142 gchar string[512];
143 va_list args;
145 va_start(args, format);
146 g_vsnprintf(string, 512, format, args);
147 va_end(args);
149 if (! prefs.suppress_status_messages)
150 set_statusbar(string, FALSE);
152 if (log || prefs.suppress_status_messages)
153 msgwin_status_add("%s", string);
157 /* updates the status bar document statistics */
158 void ui_update_statusbar(GeanyDocument *doc, gint pos)
160 if (! interface_prefs.statusbar_visible)
161 return; /* just do nothing if statusbar is not visible */
163 if (doc == NULL)
164 doc = document_get_current();
166 if (doc != NULL)
168 static GString *stats_str = NULL;
169 const gchar sp[] = " ";
170 guint line, col;
171 const gchar *cur_tag;
172 gchar *filetype_name = doc->file_type->name;
174 if (G_UNLIKELY(stats_str == NULL))
175 stats_str = g_string_sized_new(120);
177 if (pos == -1)
178 pos = sci_get_current_position(doc->editor->sci);
179 line = sci_get_line_from_position(doc->editor->sci, pos);
181 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
182 * when current pos is beyond document end (can occur when removing
183 * blocks of selected lines especially esp. brace sections near end of file). */
184 if (pos <= sci_get_length(doc->editor->sci))
185 col = sci_get_col_from_position(doc->editor->sci, pos);
186 else
187 col = 0;
189 /* Status bar statistics: col = column, sel = selection. */
190 g_string_printf(stats_str, _("line: %d\t col: %d\t sel: %d\t "),
191 (line + 1), col,
192 sci_get_selected_text_length(doc->editor->sci) - 1);
194 g_string_append(stats_str,
195 /* RO = read-only */
196 (doc->readonly) ? _("RO ") :
197 /* OVR = overwrite/overtype, INS = insert */
198 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
199 g_string_append(stats_str, sp);
201 switch (editor_get_indent_prefs(doc->editor)->type)
203 case GEANY_INDENT_TYPE_TABS:
204 g_string_append(stats_str, _("TAB"));
205 break;
206 case GEANY_INDENT_TYPE_SPACES:
207 g_string_append(stats_str, _("SP")); /* SP = space */
208 break;
209 case GEANY_INDENT_TYPE_BOTH:
210 g_string_append(stats_str, _("T/S")); /* T/S = tabs and spaces */
211 break;
213 g_string_append(stats_str, sp);
214 g_string_append_printf(stats_str, _("mode: %s"),
215 editor_get_eol_char_name(doc->editor));
216 g_string_append(stats_str, sp);
217 g_string_append_printf(stats_str, _("encoding: %s %s"),
218 (doc->encoding) ? doc->encoding : _("unknown"),
219 (encodings_is_unicode_charset(doc->encoding)) ?
220 /* BOM = byte order mark */
221 ((doc->has_bom) ? _("(with BOM)") : "") : "");
222 g_string_append(stats_str, sp);
223 g_string_append_printf(stats_str, _("filetype: %s"), filetype_name);
224 g_string_append(stats_str, sp);
225 if (doc->changed)
227 g_string_append(stats_str, _("MOD")); /* MOD = modified */
228 g_string_append(stats_str, sp);
231 symbols_get_current_function(doc, &cur_tag);
232 g_string_append_printf(stats_str, _("scope: %s"),
233 cur_tag);
235 #ifdef GEANY_DEBUG
236 g_string_append(stats_str, sp);
237 g_string_append_printf(stats_str, "pos: %d", pos);
238 g_string_append(stats_str, sp);
239 g_string_append_printf(stats_str, "style: %d", sci_get_style_at(doc->editor->sci, pos));
240 #endif
242 /* can be overridden by status messages */
243 set_statusbar(stats_str->str, TRUE);
245 else /* no documents */
247 set_statusbar("", TRUE); /* can be overridden by status messages */
252 /* This sets the window title according to the current filename. */
253 void ui_set_window_title(GeanyDocument *doc)
255 GString *str;
256 GeanyProject *project = app->project;
258 if (doc == NULL)
259 doc = document_get_current();
261 str = g_string_new(NULL);
263 if (doc != NULL)
265 g_string_append(str, doc->changed ? "*" : "");
267 if (doc->file_name == NULL)
268 g_string_append(str, DOC_FILENAME(doc));
269 else
271 gchar *short_name = document_get_basename_for_display(doc, 30);
272 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
274 g_string_append(str, short_name);
275 g_string_append(str, " - ");
276 g_string_append(str, dirname ? dirname : "");
277 g_free(short_name);
278 g_free(dirname);
280 g_string_append(str, " - ");
282 if (project)
284 g_string_append_c(str, '[');
285 g_string_append(str, project->name);
286 g_string_append(str, "] - ");
288 g_string_append(str, "Geany");
289 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
290 g_string_free(str, TRUE);
294 void ui_set_editor_font(const gchar *font_name)
296 guint i;
298 g_return_if_fail(font_name != NULL);
300 /* do nothing if font has not changed */
301 if (interface_prefs.editor_font != NULL)
302 if (strcmp(font_name, interface_prefs.editor_font) == 0)
303 return;
305 g_free(interface_prefs.editor_font);
306 interface_prefs.editor_font = g_strdup(font_name);
308 /* We copy the current style, and update the font in all open tabs. */
309 for (i = 0; i < documents_array->len; i++)
311 if (documents[i]->editor)
313 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
317 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
321 void ui_set_fullscreen(void)
323 if (ui_prefs.fullscreen)
325 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
327 else
329 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
334 void ui_update_popup_reundo_items(GeanyDocument *doc)
336 gboolean enable_undo;
337 gboolean enable_redo;
338 guint i, len;
340 if (doc == NULL)
342 enable_undo = FALSE;
343 enable_redo = FALSE;
345 else
347 enable_undo = document_can_undo(doc);
348 enable_redo = document_can_redo(doc);
351 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
352 len = G_N_ELEMENTS(widgets.undo_items);
353 for (i = 0; i < len; i++)
355 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
357 len = G_N_ELEMENTS(widgets.redo_items);
358 for (i = 0; i < len; i++)
360 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
365 void ui_update_popup_copy_items(GeanyDocument *doc)
367 gboolean enable;
368 guint i, len;
370 if (doc == NULL)
371 enable = FALSE;
372 else
373 enable = sci_has_selection(doc->editor->sci);
375 len = G_N_ELEMENTS(widgets.popup_copy_items);
376 for (i = 0; i < len; i++)
377 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
381 void ui_update_popup_goto_items(gboolean enable)
383 guint i, len;
384 len = G_N_ELEMENTS(widgets.popup_goto_items);
385 for (i = 0; i < len; i++)
386 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
390 void ui_update_menu_copy_items(GeanyDocument *doc)
392 gboolean enable = FALSE;
393 guint i, len;
394 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
396 if (IS_SCINTILLA(focusw))
397 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
398 else
399 if (GTK_IS_EDITABLE(focusw))
400 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
401 else
402 if (GTK_IS_TEXT_VIEW(focusw))
404 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
405 GTK_TEXT_VIEW(focusw));
406 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
409 len = G_N_ELEMENTS(widgets.menu_copy_items);
410 for (i = 0; i < len; i++)
411 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
415 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
417 gboolean enable = FALSE;
419 if (doc == NULL || doc->file_type == NULL)
420 enable = FALSE;
421 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
422 enable = TRUE;
424 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
428 void ui_update_fold_items(void)
430 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
431 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
432 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
436 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
438 guint i = 0;
439 GtkWidget *tmp_menu;
440 GtkWidget *tmp_popup;
441 GtkWidget *edit_menu, *edit_menu_item;
442 GtkWidget *popup_menu, *popup_menu_item;
444 edit_menu = gtk_menu_new();
445 popup_menu = gtk_menu_new();
446 edit_menu_item = gtk_menu_item_new_with_label(label);
447 popup_menu_item = gtk_menu_item_new_with_label(label);
448 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
449 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
451 while (includes[i] != NULL)
453 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
454 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
455 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
456 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
457 g_signal_connect(tmp_menu, "activate",
458 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
459 g_signal_connect(tmp_popup, "activate",
460 G_CALLBACK(on_insert_include_activate), (gpointer) includes[i]);
461 i++;
463 gtk_widget_show_all(edit_menu_item);
464 gtk_widget_show_all(popup_menu_item);
465 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
466 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
470 void ui_create_insert_menu_items(void)
472 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
473 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
474 GtkWidget *blank;
475 const gchar *c_includes_stdlib[] = {
476 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
477 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
479 const gchar *c_includes_c99[] = {
480 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
481 "tgmath.h", "wchar.h", "wctype.h", NULL
483 const gchar *c_includes_cpp[] = {
484 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
486 const gchar *c_includes_cppstdlib[] = {
487 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
488 "memory", "locale", NULL
490 const gchar *c_includes_stl[] = {
491 "bitset", "dequev", "list", "map", "set", "queue", "stack", "vector", "algorithm",
492 "iterator", "functional", "string", "complex", "valarray", NULL
495 blank = gtk_menu_item_new_with_label("#include \"...\"");
496 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
497 gtk_widget_show(blank);
498 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate),
499 (gpointer) "blank");
500 blank = gtk_separator_menu_item_new ();
501 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
502 gtk_widget_show(blank);
504 blank = gtk_menu_item_new_with_label("#include \"...\"");
505 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
506 gtk_widget_show(blank);
507 g_signal_connect(blank, "activate", G_CALLBACK(on_insert_include_activate),
508 (gpointer) "blank");
509 blank = gtk_separator_menu_item_new();
510 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
511 gtk_widget_show(blank);
513 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
514 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
515 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
516 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
517 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
521 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
523 GtkWidget *item;
525 item = gtk_menu_item_new_with_mnemonic(label);
526 gtk_container_add(GTK_CONTAINER(me), item);
527 gtk_widget_show(item);
528 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
530 item = gtk_menu_item_new_with_mnemonic(label);
531 gtk_container_add(GTK_CONTAINER(mp), item);
532 gtk_widget_show(item);
533 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), label);
537 void ui_create_insert_date_menu_items(void)
539 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
540 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
541 GtkWidget *item;
542 gchar *str;
544 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
545 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
546 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
548 item = gtk_separator_menu_item_new();
549 gtk_container_add(GTK_CONTAINER(menu_edit), item);
550 gtk_widget_show(item);
551 item = gtk_separator_menu_item_new();
552 gtk_container_add(GTK_CONTAINER(menu_popup), item);
553 gtk_widget_show(item);
555 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
556 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
557 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
559 item = gtk_separator_menu_item_new();
560 gtk_container_add(GTK_CONTAINER(menu_edit), item);
561 gtk_widget_show(item);
562 item = gtk_separator_menu_item_new();
563 gtk_container_add(GTK_CONTAINER(menu_popup), item);
564 gtk_widget_show(item);
566 str = _("_Use Custom Date Format");
567 item = gtk_menu_item_new_with_mnemonic(str);
568 gtk_container_add(GTK_CONTAINER(menu_edit), item);
569 gtk_widget_show(item);
570 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
571 g_object_set_data_full(G_OBJECT(main_widgets.window),
572 "insert_date_custom1", g_object_ref(item), (GDestroyNotify)g_object_unref);
574 item = gtk_menu_item_new_with_mnemonic(str);
575 gtk_container_add(GTK_CONTAINER(menu_popup), item);
576 gtk_widget_show(item);
577 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), str);
578 g_object_set_data_full(G_OBJECT(main_widgets.editor_menu),
579 "insert_date_custom2", g_object_ref(item), (GDestroyNotify)g_object_unref);
581 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
585 void ui_save_buttons_toggle(gboolean enable)
587 guint i;
588 gboolean dirty_tabs = FALSE;
590 if (ui_prefs.allow_always_save)
591 return;
593 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
594 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
596 /* save all menu item and tool button */
597 for (i = 0; i < documents_array->len; i++)
599 /* check whether there are files where changes were made and if there are some,
600 * we need the save all button / item */
601 if (documents[i]->is_valid && documents[i]->changed)
603 dirty_tabs = TRUE;
604 break;
608 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
609 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
613 #define add_doc_widget(widget_name) \
614 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
616 #define add_doc_toolitem(widget_name) \
617 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
619 static void init_document_widgets(void)
621 widgets.document_buttons = g_ptr_array_new();
623 /* Cache the document-sensitive widgets so we don't have to keep looking them up
624 * when using ui_document_buttons_update(). */
625 add_doc_widget("menu_close1");
626 add_doc_widget("close_other_documents1");
627 add_doc_widget("menu_change_font1");
628 add_doc_widget("menu_close_all1");
629 add_doc_widget("menu_save1");
630 add_doc_widget("menu_save_all1");
631 add_doc_widget("menu_save_as1");
632 add_doc_widget("menu_count_words1");
633 add_doc_widget("menu_build1");
634 add_doc_widget("add_comments1");
635 add_doc_widget("menu_paste1");
636 add_doc_widget("menu_undo2");
637 add_doc_widget("preferences2");
638 add_doc_widget("menu_reload1");
639 add_doc_widget("menu_document1");
640 add_doc_widget("menu_choose_color1");
641 add_doc_widget("menu_zoom_in1");
642 add_doc_widget("menu_zoom_out1");
643 add_doc_widget("menu_view_editor1");
644 add_doc_widget("normal_size1");
645 add_doc_widget("treeview6");
646 add_doc_widget("print1");
647 add_doc_widget("menu_reload_as1");
648 add_doc_widget("menu_select_all1");
649 add_doc_widget("insert_date1");
650 add_doc_widget("menu_format1");
651 add_doc_widget("commands2");
652 add_doc_widget("menu_open_selected_file1");
653 add_doc_widget("page_setup1");
654 add_doc_widget("find1");
655 add_doc_widget("find_next1");
656 add_doc_widget("find_previous1");
657 add_doc_widget("replace1");
658 add_doc_widget("find_nextsel1");
659 add_doc_widget("find_prevsel1");
660 add_doc_widget("go_to_line1");
661 add_doc_toolitem("Close");
662 add_doc_toolitem("CloseAll");
663 add_doc_toolitem("Search");
664 add_doc_toolitem("SearchEntry");
665 add_doc_toolitem("NavBack");
666 add_doc_toolitem("NavFor");
667 add_doc_toolitem("ZoomIn");
668 add_doc_toolitem("ZoomOut");
669 add_doc_toolitem("Indent");
670 add_doc_toolitem("UnIndent");
671 add_doc_toolitem("Cut");
672 add_doc_toolitem("Copy");
673 add_doc_toolitem("Paste");
674 add_doc_toolitem("Delete");
675 add_doc_toolitem("Save");
676 add_doc_toolitem("SaveAll");
677 add_doc_toolitem("Compile");
678 add_doc_toolitem("Run");
679 add_doc_toolitem("Reload");
680 add_doc_toolitem("Color");
681 add_doc_toolitem("Goto");
682 add_doc_toolitem("GotoEntry");
686 void ui_document_buttons_update(void)
688 guint i;
689 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) ? TRUE : FALSE;
691 for (i = 0; i < widgets.document_buttons->len; i++)
693 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
694 if (GTK_IS_ACTION(widget))
695 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
696 else
697 ui_widget_set_sensitive(widget, enable);
702 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
704 g_ptr_array_remove_fast(widgets.document_buttons, widget);
708 /** Add a widget to the list of widgets that should be set sensitive/insensitive
709 * when some documents are present/no documents are open.
710 * It will be removed when the widget is destroyed.
711 * @param widget The widget to add.
713 * @since 0.15
715 void ui_add_document_sensitive(GtkWidget *widget)
717 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) ? TRUE : FALSE;
719 ui_widget_set_sensitive(widget, enable);
721 g_ptr_array_add(widgets.document_buttons, widget);
722 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
726 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
728 if (show)
730 gtk_widget_show(widget);
732 else
734 gtk_widget_hide(widget);
739 void ui_sidebar_show_hide(void)
741 GtkWidget *widget;
743 /* check that there are no other notebook pages before hiding the sidebar completely
744 * other pages could be e.g. the file browser plugin */
745 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
746 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
748 ui_prefs.sidebar_visible = FALSE;
751 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
752 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
754 ignore_callback = TRUE;
755 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
756 ignore_callback = FALSE;
759 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
761 ui_widget_show_hide(gtk_notebook_get_nth_page(
762 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
763 ui_widget_show_hide(gtk_notebook_get_nth_page(
764 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
768 void ui_document_show_hide(GeanyDocument *doc)
770 gchar *widget_name;
771 GtkWidget *item;
772 const GeanyIndentPrefs *iprefs;
774 if (doc == NULL)
775 doc = document_get_current();
777 if (doc == NULL)
778 return;
780 ignore_callback = TRUE;
782 gtk_check_menu_item_set_active(
783 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
784 doc->editor->line_wrapping);
786 gtk_check_menu_item_set_active(
787 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
788 doc->editor->line_breaking);
790 iprefs = editor_get_indent_prefs(doc->editor);
792 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
793 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
795 switch (iprefs->type)
797 case GEANY_INDENT_TYPE_SPACES:
798 widget_name = "spaces1"; break;
799 case GEANY_INDENT_TYPE_TABS:
800 widget_name = "tabs1"; break;
801 case GEANY_INDENT_TYPE_BOTH:
802 default:
803 widget_name = "tabs_and_spaces1"; break;
805 item = ui_lookup_widget(main_widgets.window, widget_name);
806 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
808 gtk_check_menu_item_set_active(
809 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
810 doc->readonly);
812 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
813 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
814 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
816 switch (sci_get_eol_mode(doc->editor->sci))
818 case SC_EOL_CR: widget_name = "cr"; break;
819 case SC_EOL_LF: widget_name = "lf"; break;
820 default: widget_name = "crlf"; break;
822 gtk_check_menu_item_set_active(
823 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
825 encodings_select_radio_item(doc->encoding);
826 filetypes_select_radio_item(doc->file_type);
828 ignore_callback = FALSE;
832 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
834 static const GdkColor red = {0, 0xffff, 0x6666, 0x6666};
835 static const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
836 static gboolean old_value = TRUE;
838 g_return_if_fail(widget != NULL);
840 /* update only if really needed */
841 if (old_value != success)
843 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, success ? NULL : &red);
844 gtk_widget_modify_text(widget, GTK_STATE_NORMAL, success ? NULL : &white);
846 old_value = success;
851 static gboolean have_tango_icon_theme(void)
853 static gboolean result = FALSE;
854 static gboolean checked = FALSE;
856 if (! checked)
858 gchar *theme_name;
860 g_object_get(G_OBJECT(gtk_settings_get_default()), "gtk-icon-theme-name", &theme_name, NULL);
861 setptr(theme_name, g_utf8_strdown(theme_name, -1));
863 result = (strstr(theme_name, "tango") != NULL);
864 checked = TRUE;
866 g_free(theme_name);
869 return result;
873 /* Note: remember to unref the pixbuf once an image or window has added a reference. */
874 GdkPixbuf *ui_new_pixbuf_from_inline(gint img)
876 switch (img)
878 case GEANY_IMAGE_LOGO:
879 return gdk_pixbuf_new_from_inline(-1, aladin_inline, FALSE, NULL);
880 break;
881 case GEANY_IMAGE_SAVE_ALL:
883 /* check whether the icon theme looks like a Gnome icon theme, if so use the
884 * old Gnome based Save All icon, otherwise assume a Tango-like icon theme */
885 if (have_tango_icon_theme())
886 return gdk_pixbuf_new_from_inline(-1, save_all_tango_inline, FALSE, NULL);
887 else
888 return gdk_pixbuf_new_from_inline(-1, save_all_gnome_inline, FALSE, NULL);
889 break;
891 case GEANY_IMAGE_CLOSE_ALL:
893 return gdk_pixbuf_new_from_inline(-1, close_all_inline, FALSE, NULL);
894 break;
896 case GEANY_IMAGE_BUILD:
898 return gdk_pixbuf_new_from_inline(-1, build_inline, FALSE, NULL);
899 break;
901 default:
902 return NULL;
907 static GdkPixbuf *ui_new_pixbuf_from_stock(const gchar *stock_id)
909 if (utils_str_equal(stock_id, GEANY_STOCK_CLOSE_ALL))
910 return ui_new_pixbuf_from_inline(GEANY_IMAGE_CLOSE_ALL);
911 else if (utils_str_equal(stock_id, GEANY_STOCK_BUILD))
912 return ui_new_pixbuf_from_inline(GEANY_IMAGE_BUILD);
913 else if (utils_str_equal(stock_id, GEANY_STOCK_SAVE_ALL))
914 return ui_new_pixbuf_from_inline(GEANY_IMAGE_SAVE_ALL);
916 return NULL;
920 GtkWidget *ui_new_image_from_inline(gint img)
922 GtkWidget *wid;
923 GdkPixbuf *pb;
925 pb = ui_new_pixbuf_from_inline(img);
926 wid = gtk_image_new_from_pixbuf(pb);
927 g_object_unref(pb); /* the image doesn't adopt our reference, so remove our ref. */
928 return wid;
932 static void recent_create_menu(GeanyRecentFiles *grf)
934 GtkWidget *tmp;
935 guint i, len;
936 gchar *filename;
938 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
939 for (i = 0; i < len; i++)
941 filename = g_queue_peek_nth(grf->recent_queue, i);
942 /* create menu item for the recent files menu in the menu bar */
943 tmp = gtk_menu_item_new_with_label(filename);
944 gtk_widget_show(tmp);
945 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
946 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
947 /* create menu item for the recent files menu in the toolbar */
948 if (grf->toolbar != NULL)
950 tmp = gtk_menu_item_new_with_label(filename);
951 gtk_widget_show(tmp);
952 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
953 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
959 static GeanyRecentFiles *recent_get_recent_files(void)
961 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
963 if (G_UNLIKELY(grf.recent_queue == NULL))
965 grf.recent_queue = ui_prefs.recent_queue;
966 grf.menubar = ui_widgets.recent_files_menu_menubar;
967 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
968 toolbar_get_action_by_name("Open")));
969 grf.activate_cb = recent_file_activate_cb;
971 return &grf;
975 static GeanyRecentFiles *recent_get_recent_projects(void)
977 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
979 if (G_UNLIKELY(grf.recent_queue == NULL))
981 grf.recent_queue = ui_prefs.recent_projects_queue;
982 grf.menubar = ui_widgets.recent_projects_menu_menubar;
983 grf.toolbar = NULL;
984 grf.activate_cb = recent_project_activate_cb;
986 return &grf;
990 void ui_create_recent_menus(void)
992 recent_create_menu(recent_get_recent_files());
993 recent_create_menu(recent_get_recent_projects());
997 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
999 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1000 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1002 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1003 recent_file_loaded(utf8_filename, recent_get_recent_files());
1005 g_free(locale_filename);
1006 g_free(utf8_filename);
1010 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1012 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1013 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1015 if (project_ask_close() && project_load_file_with_session(locale_filename))
1016 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1018 g_free(locale_filename);
1019 g_free(utf8_filename);
1023 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf)
1025 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1027 #if GTK_CHECK_VERSION(2, 10, 0)
1028 if (grf->type == RECENT_FILE_FILE)
1030 GtkRecentManager *manager = gtk_recent_manager_get_default();
1031 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1032 if (uri != NULL)
1034 gtk_recent_manager_add_item(manager, uri);
1035 g_free(uri);
1038 #endif
1039 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1040 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1042 g_free(g_queue_pop_tail(grf->recent_queue));
1044 update_recent_menu(grf);
1046 /* filename already in recent list */
1047 else
1048 recent_file_loaded(utf8_filename, grf);
1052 void ui_add_recent_file(const gchar *utf8_filename)
1054 add_recent_file(utf8_filename, recent_get_recent_files());
1058 void ui_add_recent_project_file(const gchar *utf8_filename)
1060 add_recent_file(utf8_filename, recent_get_recent_projects());
1064 /* Returns: newly allocated string with the UTF-8 menu text. */
1065 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1067 const gchar *text = NULL;
1069 if (GTK_BIN(menu_item)->child)
1071 GtkWidget *child = GTK_BIN(menu_item)->child;
1073 if (GTK_IS_LABEL(child))
1074 text = gtk_label_get_text(GTK_LABEL(child));
1076 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1077 return g_strdup(text);
1081 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1083 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1084 gint result;
1086 if (utils_str_equal(menu_text, user_data))
1087 result = 0;
1088 else
1089 result = 1;
1091 g_free(menu_text);
1092 return result;
1096 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1098 GList *item, *children;
1099 void *data;
1100 GtkWidget *tmp;
1102 /* first reorder the queue */
1103 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1104 g_return_if_fail(item != NULL);
1106 data = item->data;
1107 g_queue_remove(grf->recent_queue, data);
1108 g_queue_push_head(grf->recent_queue, data);
1110 /* remove the old menuitem for the filename */
1111 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1112 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1113 if (item != NULL)
1114 gtk_widget_destroy(GTK_WIDGET(item->data));
1115 g_list_free(children);
1117 if (grf->toolbar != NULL)
1119 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1120 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1121 if (item != NULL)
1122 gtk_widget_destroy(GTK_WIDGET(item->data));
1123 g_list_free(children);
1125 /* now prepend a new menuitem for the filename,
1126 * first for the recent files menu in the menu bar */
1127 tmp = gtk_menu_item_new_with_label(utf8_filename);
1128 gtk_widget_show(tmp);
1129 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1130 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1131 /* then for the recent files menu in the tool bar */
1132 if (grf->toolbar != NULL)
1134 tmp = gtk_menu_item_new_with_label(utf8_filename);
1135 gtk_widget_show(tmp);
1136 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1137 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1138 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal which we need in
1139 * GeanyMenubuttonAction */
1140 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1141 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1146 static void update_recent_menu(GeanyRecentFiles *grf)
1148 GtkWidget *tmp;
1149 gchar *filename;
1150 GList *children, *item;
1152 filename = g_queue_peek_head(grf->recent_queue);
1154 /* clean the MRU list before adding an item (menubar) */
1155 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1156 if (g_list_length(children) > file_prefs.mru_length - 1)
1158 item = g_list_nth(children, file_prefs.mru_length - 1);
1159 while (item != NULL)
1161 if (GTK_IS_MENU_ITEM(item->data))
1162 gtk_widget_destroy(GTK_WIDGET(item->data));
1163 item = g_list_next(item);
1166 g_list_free(children);
1168 /* create item for the menu bar menu */
1169 tmp = gtk_menu_item_new_with_label(filename);
1170 gtk_widget_show(tmp);
1171 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1172 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1174 /* clean the MRU list before adding an item (toolbar) */
1175 if (grf->toolbar != NULL)
1177 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1178 if (g_list_length(children) > file_prefs.mru_length - 1)
1180 item = g_list_nth(children, file_prefs.mru_length - 1);
1181 while (item != NULL)
1183 if (GTK_IS_MENU_ITEM(item->data))
1184 gtk_widget_destroy(GTK_WIDGET(item->data));
1185 item = g_list_next(item);
1188 g_list_free(children);
1190 /* create item for the tool bar menu */
1191 tmp = gtk_menu_item_new_with_label(filename);
1192 gtk_widget_show(tmp);
1193 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1194 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1195 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1200 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1202 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1203 GeanyDocument *doc;
1205 for (i = 0; i < max; i++)
1207 doc = document_get_from_page(i);
1209 switch (feature)
1211 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1212 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1213 break;
1214 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1215 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
1216 break;
1217 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1218 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1219 break;
1220 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1221 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1222 break;
1223 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1224 editor_set_indentation_guides(doc->editor);
1225 break;
1231 void ui_update_view_editor_menu_items(void)
1233 ignore_callback = TRUE;
1234 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1235 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1236 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);
1237 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);
1238 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);
1239 ignore_callback = FALSE;
1243 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1244 * @param label_text The label text.
1245 * @param alignment An address to store the alignment widget pointer.
1246 * @return The frame widget, setting the alignment container for packing child widgets. */
1247 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1249 GtkWidget *label, *align;
1250 GtkWidget *frame = gtk_frame_new(NULL);
1252 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1254 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1255 gtk_container_add(GTK_CONTAINER(frame), align);
1256 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1258 label = ui_label_new_bold(label_text);
1259 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1261 *alignment = align;
1262 return frame;
1266 /** Convenience function for getting a fixed border for dialogs that doesn't
1267 * increase the button box border.
1268 * @param dialog The parent container for the @c GtkVBox.
1269 * @return The packed @c GtkVBox. */
1270 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1272 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1274 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
1275 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1276 return vbox;
1280 /** Create a @c GtkButton with custom text and a stock image, aligned like
1281 * @c gtk_button_new_from_stock().
1282 * @param stock_id A @c GTK_STOCK_NAME string.
1283 * @param text Button label text, can include mnemonics.
1284 * @return The new @c GtkButton.
1286 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1288 GtkWidget *image, *label, *align, *hbox, *button;
1290 hbox = gtk_hbox_new(FALSE, 2);
1291 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1292 label = gtk_label_new_with_mnemonic(text);
1293 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
1294 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1296 button = gtk_button_new();
1297 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
1298 gtk_container_add(GTK_CONTAINER(align), hbox);
1299 gtk_container_add(GTK_CONTAINER(button), align);
1300 gtk_widget_show_all(align);
1301 return button;
1305 /** Create a @c GtkImageMenuItem with a stock image and a custom label.
1306 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1307 * @param label Menu item label, can include mnemonics.
1308 * @return The new @c GtkImageMenuItem.
1310 * @since 0.16
1312 GtkWidget *
1313 ui_image_menu_item_new(const gchar *stock_id, const gchar *label)
1315 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(label);
1316 GtkWidget *image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
1318 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1319 gtk_widget_show(image);
1320 return item;
1324 static void entry_clear_icon_release_cb(GtkEntry *entry, gint icon_pos,
1325 GdkEvent *event, gpointer data)
1327 if (event->button.button == 1 && icon_pos == 1)
1329 gtk_entry_set_text(entry, "");
1330 gtk_widget_grab_focus(GTK_WIDGET(entry));
1335 /** Convenience function to add a small clear icon to the right end of the passed @a entry.
1336 * A callback to clear the contents of the GtkEntry is automatically added.
1338 * This feature is only available with GTK 2.16 but implemented as a runtime check,
1339 * so it is safe to just use this function, if the code is ran with older versions,
1340 * nothing happens. If ran with GTK 2.16 or newer, the icon is displayed.
1342 * @param entry The GtkEntry object to which the icon should be attached.
1344 * @since 0.16
1346 void ui_entry_add_clear_icon(GtkEntry *entry)
1348 if (gtk_check_version(2, 15, 2) == NULL)
1350 g_object_set(entry, "secondary-icon-stock", "gtk-clear", NULL);
1351 g_signal_connect(entry, "icon-release", G_CALLBACK(entry_clear_icon_release_cb), NULL);
1356 static void add_to_size_group(GtkWidget *widget, gpointer size_group)
1358 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group));
1359 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group), widget);
1363 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1364 * the width of each button box's children.
1365 * Should be called after all child widgets have been packed. */
1366 void ui_hbutton_box_copy_layout(GtkButtonBox *master, GtkButtonBox *copy)
1368 GtkSizeGroup *size_group;
1370 gtk_box_set_spacing(GTK_BOX(copy), 10);
1371 gtk_button_box_set_layout(copy, gtk_button_box_get_layout(master));
1373 /* now we need to put the widest widget from each button box in a size group,
1374 * but we don't know the width before they are drawn, and for different label
1375 * translations the widest widget can vary, so we just add all widgets. */
1376 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1377 gtk_container_foreach(GTK_CONTAINER(master), add_to_size_group, size_group);
1378 gtk_container_foreach(GTK_CONTAINER(copy), add_to_size_group, size_group);
1379 g_object_unref(size_group);
1383 /* Prepends the active text to the drop down list, unless the first element in
1384 * the list is identical, ensuring there are <= history_len elements. */
1385 void ui_combo_box_add_to_history(GtkComboBox *combo, const gchar *text)
1387 const gint history_len = 30;
1388 GtkTreeModel *model;
1389 GtkTreeIter iter;
1390 gchar *combo_text;
1391 gboolean equal = FALSE;
1392 GtkTreePath *path;
1394 model = gtk_combo_box_get_model(combo);
1395 if (gtk_tree_model_get_iter_first(model, &iter))
1397 gtk_tree_model_get(model, &iter, 0, &combo_text, -1);
1398 equal = utils_str_equal(combo_text, text);
1399 g_free(combo_text);
1401 if (equal) return; /* don't prepend duplicate */
1403 gtk_combo_box_prepend_text(combo, text);
1405 /* limit history */
1406 path = gtk_tree_path_new_from_indices(history_len, -1);
1407 if (gtk_tree_model_get_iter(model, &iter, path))
1409 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1411 gtk_tree_path_free(path);
1415 /* Same as gtk_combo_box_prepend_text(), except that text is only prepended if it not already
1416 * exists in the combo's model. */
1417 void ui_combo_box_prepend_text_once(GtkComboBox *combo, const gchar *text)
1419 GtkTreeModel *model;
1420 GtkTreeIter iter;
1421 gchar *combo_text;
1422 gboolean found = FALSE;
1424 model = gtk_combo_box_get_model(combo);
1425 if (gtk_tree_model_get_iter_first(model, &iter))
1429 gtk_tree_model_get(model, &iter, 0, &combo_text, -1);
1430 found = utils_str_equal(combo_text, text);
1431 g_free(combo_text);
1433 while (!found && gtk_tree_model_iter_next(model, &iter));
1435 if (found)
1436 return; /* don't prepend duplicate */
1438 gtk_combo_box_prepend_text(combo, text);
1442 /* Changes the color of the notebook tab text and open files items according to
1443 * document status. */
1444 void ui_update_tab_status(GeanyDocument *doc)
1446 const GdkColor *color = document_get_status_color(doc);
1448 /* NULL color will reset to default */
1449 gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_NORMAL, color);
1450 gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_ACTIVE, color);
1452 sidebar_openfiles_update(doc);
1456 static gboolean tree_model_iter_get_next(GtkTreeModel *model, GtkTreeIter *iter,
1457 gboolean down)
1459 GtkTreePath *path;
1460 gboolean result;
1462 if (down)
1463 return gtk_tree_model_iter_next(model, iter);
1465 path = gtk_tree_model_get_path(model, iter);
1466 result = gtk_tree_path_prev(path) && gtk_tree_model_get_iter(model, iter, path);
1467 gtk_tree_path_free(path);
1468 return result;
1472 /* note: the while loop might be more efficient when searching upwards if it
1473 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1474 static gboolean tree_view_find(GtkTreeView *treeview, TVMatchCallback cb, gboolean down)
1476 GtkTreeSelection *treesel;
1477 GtkTreeIter iter;
1478 GtkTreeModel *model;
1480 treesel = gtk_tree_view_get_selection(treeview);
1481 if (gtk_tree_selection_get_selected(treesel, &model, &iter))
1483 /* get the next selected item */
1484 if (! tree_model_iter_get_next(model, &iter, down))
1485 return FALSE; /* no more items */
1487 else /* no selection */
1489 if (! gtk_tree_model_get_iter_first(model, &iter))
1490 return TRUE; /* no items */
1492 while (TRUE)
1494 gtk_tree_selection_select_iter(treesel, &iter);
1495 if (cb())
1496 break; /* found next message */
1498 if (! tree_model_iter_get_next(model, &iter, down))
1499 return FALSE; /* no more items */
1501 /* scroll item in view */
1502 if (ui_prefs.msgwindow_visible)
1504 GtkTreePath *path = gtk_tree_model_get_path(
1505 gtk_tree_view_get_model(treeview), &iter);
1507 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
1508 gtk_tree_path_free(path);
1510 return TRUE;
1514 /* Returns FALSE if the treeview has items but no matching next item. */
1515 gboolean ui_tree_view_find_next(GtkTreeView *treeview, TVMatchCallback cb)
1517 return tree_view_find(treeview, cb, TRUE);
1521 /* Returns FALSE if the treeview has items but no matching next item. */
1522 gboolean ui_tree_view_find_previous(GtkTreeView *treeview, TVMatchCallback cb)
1524 return tree_view_find(treeview, cb, FALSE);
1529 * Modifies the font of a widget using gtk_widget_modify_font().
1531 * @param widget The widget.
1532 * @param str The font name as expected by pango_font_description_from_string().
1534 void ui_widget_modify_font_from_string(GtkWidget *widget, const gchar *str)
1536 PangoFontDescription *pfd;
1538 pfd = pango_font_description_from_string(str);
1539 gtk_widget_modify_font(widget, pfd);
1540 pango_font_description_free(pfd);
1544 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1545 * file chooser, replacing entry text (if successful) with the path returned from the
1546 * @c GtkFileChooser.
1547 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1548 * @param title The file chooser dialog title, or @c NULL.
1549 * @param action The mode of the file chooser.
1550 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1551 * such as @c GtkComboBoxEntry.
1552 * @return The @c GtkHBox.
1554 /* @see ui_setup_open_button_callback(). */
1555 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1557 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1559 hbox = gtk_hbox_new(FALSE, 6);
1560 path_entry = GTK_WIDGET(entry);
1562 /* prevent path_entry being vertically stretched to the height of dirbtn */
1563 vbox = gtk_vbox_new(FALSE, 0);
1564 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1566 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1568 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1570 else
1571 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1573 dirbtn = gtk_button_new();
1574 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1575 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1576 ui_setup_open_button_callback(dirbtn, title, action, entry);
1578 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1579 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1580 return hbox;
1584 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1587 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1588 * title can be NULL.
1589 * action is the file chooser mode to use. */
1590 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1591 GtkFileChooserAction action, GtkEntry *entry)
1593 GtkWidget *path_entry = GTK_WIDGET(entry);
1595 if (title)
1596 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1597 (GDestroyNotify) g_free);
1598 g_object_set_data(G_OBJECT(open_btn), "action", (gpointer) action);
1599 g_object_set_data_full(G_OBJECT(open_btn), "entry",
1600 g_object_ref(path_entry), (GDestroyNotify) g_object_unref);
1601 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), open_btn);
1605 #ifndef G_OS_WIN32
1606 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1607 const gchar *utf8_path)
1609 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1610 GTK_WINDOW(main_widgets.window), action,
1611 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1612 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1613 gchar *locale_path;
1614 gchar *ret_path = NULL;
1616 gtk_widget_set_name(dialog, "GeanyDialog");
1617 locale_path = utils_get_locale_from_utf8(utf8_path);
1618 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1620 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1621 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1623 else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1625 if (g_path_is_absolute(locale_path))
1626 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), locale_path);
1628 g_free(locale_path);
1630 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1632 gchar *dir_locale;
1634 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1635 ret_path = utils_get_utf8_from_locale(dir_locale);
1636 g_free(dir_locale);
1638 gtk_widget_destroy(dialog);
1639 return ret_path;
1641 #endif
1644 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1646 GtkWidget *path_box = GTK_WIDGET(user_data);
1647 GtkFileChooserAction action =
1648 (GtkFileChooserAction) g_object_get_data(G_OBJECT(path_box), "action");
1649 GtkEntry *entry =
1650 (GtkEntry *) g_object_get_data(G_OBJECT(path_box), "entry");
1651 const gchar *title = g_object_get_data(G_OBJECT(path_box), "title");
1652 gchar *utf8_path;
1654 /* TODO: extend for other actions */
1655 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1656 action == GTK_FILE_CHOOSER_ACTION_OPEN);
1658 if (title == NULL)
1659 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1660 _("Select Folder") : _("Select File");
1662 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
1664 #ifdef G_OS_WIN32
1665 utf8_path = win32_show_project_folder_dialog(ui_widgets.prefs_dialog, title,
1666 gtk_entry_get_text(GTK_ENTRY(entry)));
1667 #else
1668 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1669 #endif
1671 else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1673 gchar *path = g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry)));
1674 #ifdef G_OS_WIN32
1675 /* TODO this doesn't work on Windows yet, we need a more generic win32_show_file_dialog() */
1676 /*utf8_path = win32_show_file_dialog(TRUE, ui_widgets.prefs_dialog, path);*/
1677 utf8_path = NULL;
1678 #else
1679 utf8_path = run_file_chooser(title, action, path);
1680 #endif
1681 g_free(path);
1684 if (utf8_path != NULL)
1686 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
1687 g_free(utf8_path);
1692 void ui_statusbar_showhide(gboolean state)
1694 /* handle statusbar visibility */
1695 if (state)
1697 gtk_widget_show(ui_widgets.statusbar);
1698 ui_update_statusbar(NULL, -1);
1700 else
1701 gtk_widget_hide(ui_widgets.statusbar);
1705 /** Pack all @c GtkWidgets passed after the row argument into a table, using
1706 * one widget per cell. The first widget is not expanded as the table grows,
1707 * as this is usually a label.
1708 * @param table
1709 * @param row The row number of the table.
1711 void ui_table_add_row(GtkTable *table, gint row, ...)
1713 va_list args;
1714 gint i;
1715 GtkWidget *widget;
1717 va_start(args, row);
1718 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
1720 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
1722 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
1723 options, 0, 0, 0);
1725 va_end(args);
1729 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
1731 const gchar *file_name = user_data;
1732 GeanyFiletype *ft = NULL;
1734 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
1735 ft = filetypes[GEANY_FILETYPES_CONF];
1737 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
1738 document_open_file(file_name, FALSE, ft, NULL);
1739 else
1741 gchar *utf8 = utils_get_utf8_from_locale(file_name);
1742 gchar *base_name = g_path_get_basename(file_name);
1743 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
1744 gchar *global_content = NULL;
1746 /* if the requested file doesn't exist in the user's config dir, try loading the file
1747 * from the global data directory and use its contents for the newly created file */
1748 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
1749 g_file_get_contents(global_file, &global_content, NULL, NULL);
1751 document_new_file(utf8, ft, global_content);
1752 utils_free_pointers(4, utf8, base_name, global_file, global_content, NULL);
1757 /* @note You should connect to the "document-save" signal yourself to detect
1758 * if the user has just saved the config file, reloading it. */
1759 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
1761 GtkWidget *item;
1763 if (!parent)
1764 parent = GTK_CONTAINER(widgets.config_files_menu);
1766 if (!label)
1768 gchar *base_name;
1770 base_name = g_path_get_basename(real_path);
1771 item = gtk_menu_item_new_with_label(base_name);
1772 g_free(base_name);
1774 else
1775 item = gtk_menu_item_new_with_mnemonic(label);
1777 gtk_widget_show(item);
1778 gtk_container_add(parent, item);
1779 g_signal_connect(item, "activate", G_CALLBACK(on_config_file_clicked),
1780 /* this memory is kept */
1781 g_strdup(real_path));
1785 static gboolean sort_menu(gpointer data)
1787 ui_menu_sort_by_label(GTK_MENU(data));
1788 return FALSE;
1792 static void create_config_files_menu(void)
1794 GtkWidget *menu, *item;
1796 widgets.config_files_menu = menu = gtk_menu_new();
1798 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
1799 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
1801 /* sort menu after all items added */
1802 g_idle_add(sort_menu, widgets.config_files_menu);
1806 void ui_init_stock_items(void)
1808 GtkIconSet *icon_set;
1809 GtkIconFactory *factory = gtk_icon_factory_new();
1810 GdkPixbuf *pb;
1811 gsize i, len;
1812 GtkStockItem items[] =
1814 { GEANY_STOCK_SAVE_ALL, _("Save All"), 0, 0, GETTEXT_PACKAGE },
1815 { GEANY_STOCK_CLOSE_ALL, _("Close All"), 0, 0, GETTEXT_PACKAGE },
1816 { GEANY_STOCK_BUILD, _("Build"), 0, 0, GETTEXT_PACKAGE }
1819 len = G_N_ELEMENTS(items);
1820 for (i = 0; i < len; i++)
1822 pb = ui_new_pixbuf_from_stock(items[i].stock_id);
1823 icon_set = gtk_icon_set_new_from_pixbuf(pb);
1825 gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
1827 gtk_icon_set_unref(icon_set);
1828 g_object_unref(pb);
1830 gtk_stock_add((GtkStockItem *) items, len);
1831 gtk_icon_factory_add_default(factory);
1832 g_object_unref(factory);
1836 void ui_init_toolbar_widgets(void)
1838 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
1839 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
1840 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
1841 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
1845 void ui_swap_sidebar_pos(void)
1847 GtkWidget *pane = ui_lookup_widget(main_widgets.window, "hpaned1");
1848 GtkWidget *left = gtk_paned_get_child1(GTK_PANED(pane));
1849 GtkWidget *right = gtk_paned_get_child2(GTK_PANED(pane));
1850 GtkWidget *box = ui_lookup_widget(main_widgets.window, "vbox1");
1852 /* reparenting avoids scintilla problem with middle click paste */
1853 gtk_widget_reparent(left, box);
1854 gtk_widget_reparent(right, box);
1855 gtk_widget_reparent(right, pane);
1856 gtk_widget_reparent(left, pane);
1858 gtk_paned_set_position(GTK_PANED(pane), pane->allocation.width
1859 - gtk_paned_get_position(GTK_PANED(pane)));
1863 static void init_recent_files(void)
1865 GtkWidget *toolbar_recent_files_menu;
1867 /* add recent files to the File menu */
1868 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
1869 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
1870 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
1871 ui_widgets.recent_files_menu_menubar);
1873 /* add recent files to the toolbar Open button */
1874 toolbar_recent_files_menu = gtk_menu_new();
1875 g_object_ref(toolbar_recent_files_menu);
1876 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
1877 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
1881 static void ui_menu_move(GtkWidget *menu, GtkWidget *old, GtkWidget *new)
1883 g_object_ref(menu);
1884 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old), NULL);
1885 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu);
1886 g_object_unref(menu);
1890 static void on_editor_menu_show(GtkWidget *item)
1892 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, "commands1");
1893 GtkWidget *bar = ui_lookup_widget(main_widgets.window, "commands2");
1895 ui_menu_move(widgets.commands_menu, bar, popup);
1897 popup = ui_lookup_widget(main_widgets.editor_menu, "menu_format2");
1898 bar = ui_lookup_widget(main_widgets.window, "menu_format1");
1899 ui_menu_move(widgets.format_menu, bar, popup);
1903 static void on_editor_menu_hide(GtkWidget *item)
1905 GtkWidget *popup = ui_lookup_widget(main_widgets.editor_menu, "commands1");
1906 GtkWidget *bar = ui_lookup_widget(main_widgets.window, "commands2");
1908 ui_menu_move(widgets.commands_menu, popup, bar);
1910 popup = ui_lookup_widget(main_widgets.editor_menu, "menu_format2");
1911 bar = ui_lookup_widget(main_widgets.window, "menu_format1");
1912 ui_menu_move(widgets.format_menu, popup, bar);
1916 void ui_init(void)
1918 GtkWidget *item;
1920 init_recent_files();
1922 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
1923 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
1925 main_widgets.progressbar = progress_bar_create();
1927 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition1");
1928 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_declaration1");
1929 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage1");
1930 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage1");
1931 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
1932 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
1933 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
1934 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
1935 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
1936 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
1937 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
1938 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
1939 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
1940 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
1941 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
1942 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
1943 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
1944 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
1946 item = ui_lookup_widget(main_widgets.window, "menu_format1");
1947 widgets.format_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
1948 item = ui_lookup_widget(main_widgets.window, "commands2");
1949 widgets.commands_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
1951 /* reparent edit submenus as needed */
1952 item = main_widgets.editor_menu;
1953 g_signal_connect(item, "show", G_CALLBACK(on_editor_menu_show), NULL);
1954 g_signal_connect(item, "hide", G_CALLBACK(on_editor_menu_hide), NULL);
1956 ui_init_toolbar_widgets();
1957 init_document_widgets();
1958 create_config_files_menu();
1962 static void auto_separator_update(GeanyAutoSeparator *autosep)
1964 g_return_if_fail(autosep->ref_count >= 0);
1966 if (autosep->widget)
1967 ui_widget_show_hide(autosep->widget, autosep->ref_count > 0);
1971 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
1973 GeanyAutoSeparator *autosep = user_data;
1975 if (GTK_WIDGET_VISIBLE(widget))
1976 autosep->ref_count++;
1977 else
1978 autosep->ref_count--;
1979 auto_separator_update(autosep);
1983 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
1985 GeanyAutoSeparator *autosep = user_data;
1987 /* GTK_WIDGET_VISIBLE won't work now the widget is being destroyed,
1988 * so assume widget was visible */
1989 autosep->ref_count--;
1990 autosep->ref_count = MAX(autosep->ref_count, 0);
1991 auto_separator_update(autosep);
1995 /* Show the separator widget if @a item or another is visible. */
1996 /* Note: This would be neater taking a widget argument, setting a "visible-count"
1997 * property, and using reference counting to keep the widget alive whilst its visible group
1998 * is alive. */
1999 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
2001 /* set widget ptr NULL when widget destroyed */
2002 if (autosep->ref_count == 0)
2003 g_signal_connect(autosep->widget, "destroy",
2004 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
2006 if (GTK_WIDGET_VISIBLE(item))
2008 autosep->ref_count++;
2009 auto_separator_update(autosep);
2011 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2012 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
2013 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
2018 * Sets @a text as the contents of the tooltip for @a widget.
2020 * @param widget The widget the tooltip should be set for.
2021 * @param text The text for the tooltip.
2023 * @since 0.16
2025 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
2027 #if GTK_CHECK_VERSION(2, 12, 0)
2028 gtk_widget_set_tooltip_text(widget, text);
2029 #else
2030 static GtkTooltips *tooltips = NULL;
2032 if (G_UNLIKELY(tooltips == NULL))
2033 tooltips = GTK_TOOLTIPS(ui_lookup_widget(main_widgets.window, "tooltips"));
2035 gtk_tooltips_set_tip(tooltips, widget, text, NULL);
2036 #endif
2040 /** This function returns a widget from a name in a component, usually created by Glade.
2041 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2042 * or alternatively any widget in the component, and the name of the widget
2043 * you want returned.
2044 * @param widget Widget with the @a widget_name property set.
2045 * @param widget_name Name to lookup.
2046 * @return The widget found.
2047 * @see ui_hookup_widget().
2049 * @since 0.16
2051 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
2053 GtkWidget *parent, *found_widget;
2055 g_return_val_if_fail(widget != NULL, NULL);
2056 g_return_val_if_fail(widget_name != NULL, NULL);
2058 for (;;)
2060 if (GTK_IS_MENU(widget))
2061 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
2062 else
2063 parent = widget->parent;
2064 if (parent == NULL)
2065 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
2066 if (parent == NULL)
2067 break;
2068 widget = parent;
2071 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
2072 if (G_UNLIKELY(found_widget == NULL))
2073 g_warning("Widget not found: %s", widget_name);
2074 return found_widget;
2078 /* Progress Bar */
2079 static guint progress_bar_timer_id = (guint) -1;
2082 static GtkWidget *progress_bar_create(void)
2084 GtkWidget *bar = gtk_progress_bar_new();
2086 /* Set the progressbar's height to 1 to fit it in the statusbar */
2087 gtk_widget_set_size_request(bar, -1, 1);
2088 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
2090 return bar;
2094 static gboolean progress_bar_pulse(gpointer data)
2096 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2098 return TRUE;
2103 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2104 * (if the statusbar is visible). This is a convenience function which adds a timer to
2105 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2106 * You can use this function when you have time consuming asynchronous operation and want to
2107 * display some activity in the GUI and when you don't know about detailed progress steps.
2108 * The progressbar widget is hidden by default when it is not active. This function and
2109 * ui_progress_bar_stop() will show and hide it automatically for you.
2111 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2112 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2113 * In this case, you need to show and hide the widget yourself. You can find some example code
2114 * in @c src/printing.c.
2116 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2118 * @since 0.16
2120 void ui_progress_bar_start(const gchar *text)
2122 g_return_if_fail(progress_bar_timer_id == (guint) -1);
2124 if (! interface_prefs.statusbar_visible)
2125 return;
2127 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2129 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2131 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2135 /** Stops a running progress bar and hides the widget again.
2137 * @since 0.16
2139 void ui_progress_bar_stop(void)
2141 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2143 if (progress_bar_timer_id != (guint) -1)
2145 g_source_remove(progress_bar_timer_id);
2146 progress_bar_timer_id = (guint) -1;
2151 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2153 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2154 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2155 gchar *sa, *sb;
2156 gint result;
2158 sa = ui_menu_item_get_text(item_a);
2159 sb = ui_menu_item_get_text(item_b);
2160 result = utils_str_casecmp(sa, sb);
2161 g_free(sa);
2162 g_free(sb);
2163 return result;
2167 /* Currently @a menu should contain only GtkMenuItems with labels. */
2168 void ui_menu_sort_by_label(GtkMenu *menu)
2170 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2171 GList *node;
2172 gint pos;
2174 list = g_list_sort(list, compare_menu_item_labels);
2175 pos = 0;
2176 foreach_list(node, list)
2178 gtk_menu_reorder_child(menu, node->data, pos);
2179 pos++;
2181 g_list_free(list);
2185 /* return value is for macros */
2186 GtkWidget *ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2188 va_list a;
2189 gchar *text;
2191 va_start(a, format);
2192 text = g_strdup_vprintf(format, a);
2193 va_end(a);
2195 gtk_label_set_text(label, text);
2196 gtk_label_set_use_markup(label, TRUE);
2197 g_free(text);
2198 return GTK_WIDGET(label);
2202 /** Add a list of document items to @a menu.
2203 * @param menu Menu.
2204 * @param active Which document to highlight, or @c NULL.
2205 * @param callback is used for each menu item's @c "activate" signal and will be passed
2206 * the corresponding document pointer as @c user_data.
2207 * @warning You should check @c doc->is_valid in the callback.
2208 * @since 0.19 */
2209 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2211 GtkWidget *menu_item, *menu_item_label;
2212 const GdkColor *color;
2213 GeanyDocument *doc;
2214 guint i, len;
2215 gchar *base_name;
2217 len = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2218 for (i = 0; i < len; i++)
2220 doc = document_get_from_page(i);
2221 if (! DOC_VALID(doc))
2222 continue;
2224 base_name = g_path_get_basename(DOC_FILENAME(doc));
2225 menu_item = gtk_menu_item_new_with_label(base_name);
2226 gtk_widget_show(menu_item);
2227 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2228 g_signal_connect(menu_item, "activate", callback, doc);
2230 color = document_get_status_color(doc);
2231 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2232 gtk_widget_modify_fg(menu_item_label, GTK_STATE_NORMAL, color);
2233 gtk_widget_modify_fg(menu_item_label, GTK_STATE_ACTIVE, color);
2235 if (doc == active)
2236 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", base_name);
2238 g_free(base_name);