Remove unnecessary case statement braces.
[geany-mirror.git] / src / ui_utils.c
blobd6d527908d23567acd86eef89715d2943661f9da
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;
74 widgets;
76 enum
78 RECENT_FILE_FILE,
79 RECENT_FILE_PROJECT
82 typedef struct
84 gint type;
85 GQueue *recent_queue;
86 GtkWidget *menubar;
87 GtkWidget *toolbar;
88 void (*activate_cb)(GtkMenuItem *, gpointer);
89 } GeanyRecentFiles;
92 static void update_recent_menu(GeanyRecentFiles *grf);
93 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf);
94 static void recent_file_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
95 static void recent_project_activate_cb(GtkMenuItem *menuitem, gpointer user_data);
96 static GtkWidget *progress_bar_create(void);
99 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
100 void ui_widget_set_sensitive(GtkWidget *widget, gboolean set)
102 if (widget != NULL)
103 gtk_widget_set_sensitive(widget, set);
107 /* allow_override is TRUE if text can be ignored when another message has been set
108 * that didn't use allow_override and has not timed out. */
109 static void set_statusbar(const gchar *text, gboolean allow_override)
111 static glong last_time = 0;
112 GTimeVal timeval;
113 const gint GEANY_STATUS_TIMEOUT = 1;
115 if (! interface_prefs.statusbar_visible)
116 return; /* just do nothing if statusbar is not visible */
118 g_get_current_time(&timeval);
120 if (! allow_override)
122 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), 1);
123 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), 1, text);
124 last_time = timeval.tv_sec;
126 else
127 if (timeval.tv_sec > last_time + GEANY_STATUS_TIMEOUT)
129 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets.statusbar), 1);
130 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets.statusbar), 1, text);
135 /** Display text on the statusbar.
136 * @param log Whether the message should be recorded in the Status window.
137 * @param format A @c printf -style string. */
138 void ui_set_statusbar(gboolean log, const gchar *format, ...)
140 gchar string[512];
141 va_list args;
143 va_start(args, format);
144 g_vsnprintf(string, 512, format, args);
145 va_end(args);
147 if (! prefs.suppress_status_messages)
148 set_statusbar(string, FALSE);
150 if (log || prefs.suppress_status_messages)
151 msgwin_status_add("%s", string);
155 /* updates the status bar document statistics */
156 void ui_update_statusbar(GeanyDocument *doc, gint pos)
158 if (! interface_prefs.statusbar_visible)
159 return; /* just do nothing if statusbar is not visible */
161 if (doc == NULL)
162 doc = document_get_current();
164 if (doc != NULL)
166 static GString *stats_str = NULL;
167 const gchar sp[] = " ";
168 guint line, col;
169 const gchar *cur_tag;
170 gchar *filetype_name = doc->file_type->name;
172 if (G_UNLIKELY(stats_str == NULL))
173 stats_str = g_string_sized_new(120);
175 if (pos == -1)
176 pos = sci_get_current_position(doc->editor->sci);
177 line = sci_get_line_from_position(doc->editor->sci, pos);
179 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
180 * when current pos is beyond document end (can occur when removing
181 * blocks of selected lines especially esp. brace sections near end of file). */
182 if (pos <= sci_get_length(doc->editor->sci))
183 col = sci_get_col_from_position(doc->editor->sci, pos);
184 else
185 col = 0;
187 /* Status bar statistics: col = column, sel = selection. */
188 g_string_printf(stats_str, _("line: %d\t col: %d\t sel: %d\t "),
189 (line + 1), col,
190 sci_get_selected_text_length(doc->editor->sci) - 1);
192 g_string_append(stats_str,
193 /* RO = read-only */
194 (doc->readonly) ? _("RO ") :
195 /* OVR = overwrite/overtype, INS = insert */
196 (sci_get_overtype(doc->editor->sci) ? _("OVR") : _("INS")));
197 g_string_append(stats_str, sp);
199 switch (editor_get_indent_prefs(doc->editor)->type)
201 case GEANY_INDENT_TYPE_TABS:
202 g_string_append(stats_str, _("TAB"));
203 break;
204 case GEANY_INDENT_TYPE_SPACES:
205 g_string_append(stats_str, _("SP")); /* SP = space */
206 break;
207 case GEANY_INDENT_TYPE_BOTH:
208 g_string_append(stats_str, _("T/S")); /* T/S = tabs and spaces */
209 break;
211 g_string_append(stats_str, sp);
212 g_string_append_printf(stats_str, _("mode: %s"),
213 editor_get_eol_char_name(doc->editor));
214 g_string_append(stats_str, sp);
215 g_string_append_printf(stats_str, _("encoding: %s %s"),
216 (doc->encoding) ? doc->encoding : _("unknown"),
217 (encodings_is_unicode_charset(doc->encoding)) ?
218 /* BOM = byte order mark */
219 ((doc->has_bom) ? _("(with BOM)") : "") : "");
220 g_string_append(stats_str, sp);
221 g_string_append_printf(stats_str, _("filetype: %s"), filetype_name);
222 g_string_append(stats_str, sp);
223 if (doc->changed)
225 g_string_append(stats_str, _("MOD")); /* MOD = modified */
226 g_string_append(stats_str, sp);
229 symbols_get_current_function(doc, &cur_tag);
230 g_string_append_printf(stats_str, _("scope: %s"),
231 cur_tag);
233 #ifdef GEANY_DEBUG
234 g_string_append(stats_str, sp);
235 g_string_append_printf(stats_str, "pos: %d", pos);
236 g_string_append(stats_str, sp);
237 g_string_append_printf(stats_str, "style: %d", sci_get_style_at(doc->editor->sci, pos));
238 #endif
240 /* can be overridden by status messages */
241 set_statusbar(stats_str->str, TRUE);
243 else /* no documents */
245 set_statusbar("", TRUE); /* can be overridden by status messages */
250 /* This sets the window title according to the current filename. */
251 void ui_set_window_title(GeanyDocument *doc)
253 GString *str;
254 GeanyProject *project = app->project;
256 if (doc == NULL)
257 doc = document_get_current();
259 str = g_string_new(NULL);
261 if (doc != NULL)
263 g_string_append(str, doc->changed ? "*" : "");
265 if (doc->file_name == NULL)
266 g_string_append(str, DOC_FILENAME(doc));
267 else
269 gchar *short_name = document_get_basename_for_display(doc, 30);
270 gchar *dirname = g_path_get_dirname(DOC_FILENAME(doc));
272 g_string_append(str, short_name);
273 g_string_append(str, " - ");
274 g_string_append(str, dirname ? dirname : "");
275 g_free(short_name);
276 g_free(dirname);
278 g_string_append(str, " - ");
280 if (project)
282 g_string_append_c(str, '[');
283 g_string_append(str, project->name);
284 g_string_append(str, "] - ");
286 g_string_append(str, "Geany");
287 gtk_window_set_title(GTK_WINDOW(main_widgets.window), str->str);
288 g_string_free(str, TRUE);
292 void ui_set_editor_font(const gchar *font_name)
294 guint i;
296 g_return_if_fail(font_name != NULL);
298 /* do nothing if font has not changed */
299 if (interface_prefs.editor_font != NULL)
300 if (strcmp(font_name, interface_prefs.editor_font) == 0)
301 return;
303 g_free(interface_prefs.editor_font);
304 interface_prefs.editor_font = g_strdup(font_name);
306 /* We copy the current style, and update the font in all open tabs. */
307 for (i = 0; i < documents_array->len; i++)
309 if (documents[i]->editor)
311 editor_set_font(documents[i]->editor, interface_prefs.editor_font);
315 ui_set_statusbar(TRUE, _("Font updated (%s)."), interface_prefs.editor_font);
319 void ui_set_fullscreen(void)
321 if (ui_prefs.fullscreen)
323 gtk_window_fullscreen(GTK_WINDOW(main_widgets.window));
325 else
327 gtk_window_unfullscreen(GTK_WINDOW(main_widgets.window));
332 void ui_update_popup_reundo_items(GeanyDocument *doc)
334 gboolean enable_undo;
335 gboolean enable_redo;
336 guint i, len;
338 if (doc == NULL)
340 enable_undo = FALSE;
341 enable_redo = FALSE;
343 else
345 enable_undo = document_can_undo(doc);
346 enable_redo = document_can_redo(doc);
349 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
350 len = G_N_ELEMENTS(widgets.undo_items);
351 for (i = 0; i < len; i++)
353 ui_widget_set_sensitive(widgets.undo_items[i], enable_undo);
355 len = G_N_ELEMENTS(widgets.redo_items);
356 for (i = 0; i < len; i++)
358 ui_widget_set_sensitive(widgets.redo_items[i], enable_redo);
363 void ui_update_popup_copy_items(GeanyDocument *doc)
365 gboolean enable;
366 guint i, len;
368 if (doc == NULL)
369 enable = FALSE;
370 else
371 enable = sci_has_selection(doc->editor->sci);
373 len = G_N_ELEMENTS(widgets.popup_copy_items);
374 for (i = 0; i < len; i++)
375 ui_widget_set_sensitive(widgets.popup_copy_items[i], enable);
379 void ui_update_popup_goto_items(gboolean enable)
381 guint i, len;
382 len = G_N_ELEMENTS(widgets.popup_goto_items);
383 for (i = 0; i < len; i++)
384 ui_widget_set_sensitive(widgets.popup_goto_items[i], enable);
388 void ui_update_menu_copy_items(GeanyDocument *doc)
390 gboolean enable = FALSE;
391 guint i, len;
392 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
394 if (IS_SCINTILLA(focusw))
395 enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
396 else
397 if (GTK_IS_EDITABLE(focusw))
398 enable = gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw), NULL, NULL);
399 else
400 if (GTK_IS_TEXT_VIEW(focusw))
402 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
403 GTK_TEXT_VIEW(focusw));
404 enable = gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL);
407 len = G_N_ELEMENTS(widgets.menu_copy_items);
408 for (i = 0; i < len; i++)
409 ui_widget_set_sensitive(widgets.menu_copy_items[i], enable);
413 void ui_update_insert_include_item(GeanyDocument *doc, gint item)
415 gboolean enable = FALSE;
417 if (doc == NULL || doc->file_type == NULL)
418 enable = FALSE;
419 else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
420 enable = TRUE;
422 ui_widget_set_sensitive(widgets.menu_insert_include_items[item], enable);
426 void ui_update_fold_items(void)
428 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_fold_all1"), editor_prefs.folding);
429 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "menu_unfold_all1"), editor_prefs.folding);
430 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "separator22"), editor_prefs.folding);
434 static void insert_include_items(GtkMenu *me, GtkMenu *mp, gchar **includes, gchar *label)
436 guint i = 0;
437 GtkWidget *tmp_menu;
438 GtkWidget *tmp_popup;
439 GtkWidget *edit_menu, *edit_menu_item;
440 GtkWidget *popup_menu, *popup_menu_item;
442 edit_menu = gtk_menu_new();
443 popup_menu = gtk_menu_new();
444 edit_menu_item = gtk_menu_item_new_with_label(label);
445 popup_menu_item = gtk_menu_item_new_with_label(label);
446 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item), edit_menu);
447 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item), popup_menu);
449 while (includes[i] != NULL)
451 tmp_menu = gtk_menu_item_new_with_label(includes[i]);
452 tmp_popup = gtk_menu_item_new_with_label(includes[i]);
453 gtk_container_add(GTK_CONTAINER(edit_menu), tmp_menu);
454 gtk_container_add(GTK_CONTAINER(popup_menu), tmp_popup);
455 g_signal_connect(tmp_menu, "activate",
456 G_CALLBACK(on_menu_insert_include_activate), (gpointer) includes[i]);
457 g_signal_connect(tmp_popup, "activate",
458 G_CALLBACK(on_insert_include_activate), (gpointer) includes[i]);
459 i++;
461 gtk_widget_show_all(edit_menu_item);
462 gtk_widget_show_all(popup_menu_item);
463 gtk_container_add(GTK_CONTAINER(me), edit_menu_item);
464 gtk_container_add(GTK_CONTAINER(mp), popup_menu_item);
468 void ui_create_insert_menu_items(void)
470 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_include2_menu"));
471 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_include1_menu"));
472 GtkWidget *blank;
473 const gchar *c_includes_stdlib[] = {
474 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
475 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
477 const gchar *c_includes_c99[] = {
478 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
479 "tgmath.h", "wchar.h", "wctype.h", NULL
481 const gchar *c_includes_cpp[] = {
482 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
484 const gchar *c_includes_cppstdlib[] = {
485 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
486 "memory", "locale", NULL
488 const gchar *c_includes_stl[] = {
489 "bitset", "dequev", "list", "map", "set", "queue", "stack", "vector", "algorithm",
490 "iterator", "functional", "string", "complex", "valarray", NULL
493 blank = gtk_menu_item_new_with_label("#include \"...\"");
494 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
495 gtk_widget_show(blank);
496 g_signal_connect(blank, "activate", G_CALLBACK(on_menu_insert_include_activate),
497 (gpointer) "blank");
498 blank = gtk_separator_menu_item_new ();
499 gtk_container_add(GTK_CONTAINER(menu_edit), blank);
500 gtk_widget_show(blank);
502 blank = gtk_menu_item_new_with_label("#include \"...\"");
503 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
504 gtk_widget_show(blank);
505 g_signal_connect(blank, "activate", G_CALLBACK(on_insert_include_activate),
506 (gpointer) "blank");
507 blank = gtk_separator_menu_item_new();
508 gtk_container_add(GTK_CONTAINER(menu_popup), blank);
509 gtk_widget_show(blank);
511 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stdlib, _("C Standard Library"));
512 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_c99, _("ISO C99"));
513 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cpp, _("C++ (C Standard Library)"));
514 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_cppstdlib, _("C++ Standard Library"));
515 insert_include_items(menu_edit, menu_popup, (gchar**) c_includes_stl, _("C++ STL"));
519 static void insert_date_items(GtkMenu *me, GtkMenu *mp, gchar *label)
521 GtkWidget *item;
523 item = gtk_menu_item_new_with_mnemonic(label);
524 gtk_container_add(GTK_CONTAINER(me), item);
525 gtk_widget_show(item);
526 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), label);
528 item = gtk_menu_item_new_with_mnemonic(label);
529 gtk_container_add(GTK_CONTAINER(mp), item);
530 gtk_widget_show(item);
531 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), label);
535 void ui_create_insert_date_menu_items(void)
537 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "insert_date1_menu"));
538 GtkMenu *menu_popup = GTK_MENU(ui_lookup_widget(main_widgets.editor_menu, "insert_date2_menu"));
539 GtkWidget *item;
540 gchar *str;
542 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy"));
543 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy"));
544 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd"));
546 item = gtk_separator_menu_item_new();
547 gtk_container_add(GTK_CONTAINER(menu_edit), item);
548 gtk_widget_show(item);
549 item = gtk_separator_menu_item_new();
550 gtk_container_add(GTK_CONTAINER(menu_popup), item);
551 gtk_widget_show(item);
553 insert_date_items(menu_edit, menu_popup, _("dd.mm.yyyy hh:mm:ss"));
554 insert_date_items(menu_edit, menu_popup, _("mm.dd.yyyy hh:mm:ss"));
555 insert_date_items(menu_edit, menu_popup, _("yyyy/mm/dd hh:mm:ss"));
557 item = gtk_separator_menu_item_new();
558 gtk_container_add(GTK_CONTAINER(menu_edit), item);
559 gtk_widget_show(item);
560 item = gtk_separator_menu_item_new();
561 gtk_container_add(GTK_CONTAINER(menu_popup), item);
562 gtk_widget_show(item);
564 str = _("_Use Custom Date Format");
565 item = gtk_menu_item_new_with_mnemonic(str);
566 gtk_container_add(GTK_CONTAINER(menu_edit), item);
567 gtk_widget_show(item);
568 g_signal_connect(item, "activate", G_CALLBACK(on_menu_insert_date_activate), str);
569 g_object_set_data_full(G_OBJECT(main_widgets.window),
570 "insert_date_custom1", g_object_ref(item), (GDestroyNotify)g_object_unref);
572 item = gtk_menu_item_new_with_mnemonic(str);
573 gtk_container_add(GTK_CONTAINER(menu_popup), item);
574 gtk_widget_show(item);
575 g_signal_connect(item, "activate", G_CALLBACK(on_insert_date_activate), str);
576 g_object_set_data_full(G_OBJECT(main_widgets.editor_menu),
577 "insert_date_custom2", g_object_ref(item), (GDestroyNotify)g_object_unref);
579 insert_date_items(menu_edit, menu_popup, _("_Set Custom Date Format"));
583 void ui_save_buttons_toggle(gboolean enable)
585 guint i;
586 gboolean dirty_tabs = FALSE;
588 if (ui_prefs.allow_always_save)
589 return;
591 ui_widget_set_sensitive(widgets.save_buttons[0], enable);
592 ui_widget_set_sensitive(widgets.save_buttons[1], enable);
594 /* save all menu item and tool button */
595 for (i = 0; i < documents_array->len; i++)
597 /* check whether there are files where changes were made and if there are some,
598 * we need the save all button / item */
599 if (documents[i]->is_valid && documents[i]->changed)
601 dirty_tabs = TRUE;
602 break;
606 ui_widget_set_sensitive(widgets.save_buttons[2], dirty_tabs);
607 ui_widget_set_sensitive(widgets.save_buttons[3], dirty_tabs);
611 #define add_doc_widget(widget_name) \
612 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
614 #define add_doc_toolitem(widget_name) \
615 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
617 static void init_document_widgets(void)
619 widgets.document_buttons = g_ptr_array_new();
621 /* Cache the document-sensitive widgets so we don't have to keep looking them up
622 * when using ui_document_buttons_update(). */
623 add_doc_widget("menu_close1");
624 add_doc_widget("close_other_documents1");
625 add_doc_widget("menu_change_font1");
626 add_doc_widget("menu_close_all1");
627 add_doc_widget("menu_save1");
628 add_doc_widget("menu_save_all1");
629 add_doc_widget("menu_save_as1");
630 add_doc_widget("menu_count_words1");
631 add_doc_widget("menu_build1");
632 add_doc_widget("add_comments1");
633 add_doc_widget("menu_paste1");
634 add_doc_widget("menu_undo2");
635 add_doc_widget("preferences2");
636 add_doc_widget("menu_reload1");
637 add_doc_widget("menu_document1");
638 add_doc_widget("menu_choose_color1");
639 add_doc_widget("menu_zoom_in1");
640 add_doc_widget("menu_zoom_out1");
641 add_doc_widget("menu_view_editor1");
642 add_doc_widget("normal_size1");
643 add_doc_widget("treeview6");
644 add_doc_widget("print1");
645 add_doc_widget("menu_reload_as1");
646 add_doc_widget("menu_select_all1");
647 add_doc_widget("insert_date1");
648 add_doc_widget("menu_format1");
649 add_doc_widget("menu_open_selected_file1");
650 add_doc_widget("page_setup1");
651 add_doc_widget("find1");
652 add_doc_widget("find_next1");
653 add_doc_widget("find_previous1");
654 add_doc_widget("replace1");
655 add_doc_widget("find_nextsel1");
656 add_doc_widget("find_prevsel1");
657 add_doc_widget("go_to_line1");
658 add_doc_toolitem("Close");
659 add_doc_toolitem("CloseAll");
660 add_doc_toolitem("Search");
661 add_doc_toolitem("SearchEntry");
662 add_doc_toolitem("NavBack");
663 add_doc_toolitem("NavFor");
664 add_doc_toolitem("ZoomIn");
665 add_doc_toolitem("ZoomOut");
666 add_doc_toolitem("Indent");
667 add_doc_toolitem("UnIndent");
668 add_doc_toolitem("Cut");
669 add_doc_toolitem("Copy");
670 add_doc_toolitem("Paste");
671 add_doc_toolitem("Delete");
672 add_doc_toolitem("Save");
673 add_doc_toolitem("SaveAll");
674 add_doc_toolitem("Compile");
675 add_doc_toolitem("Run");
676 add_doc_toolitem("Reload");
677 add_doc_toolitem("Color");
678 add_doc_toolitem("Goto");
679 add_doc_toolitem("GotoEntry");
683 void ui_document_buttons_update(void)
685 guint i;
686 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) ? TRUE : FALSE;
688 for (i = 0; i < widgets.document_buttons->len; i++)
690 GtkWidget *widget = g_ptr_array_index(widgets.document_buttons, i);
691 if (GTK_IS_ACTION(widget))
692 gtk_action_set_sensitive(GTK_ACTION(widget), enable);
693 else
694 ui_widget_set_sensitive(widget, enable);
699 static void on_doc_sensitive_widget_destroy(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
701 g_ptr_array_remove_fast(widgets.document_buttons, widget);
705 /** Add a widget to the list of widgets that should be set sensitive/insensitive
706 * when some documents are present/no documents are open.
707 * It will be removed when the widget is destroyed.
708 * @param widget The widget to add.
710 * @since 0.15
712 void ui_add_document_sensitive(GtkWidget *widget)
714 gboolean enable = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) ? TRUE : FALSE;
716 ui_widget_set_sensitive(widget, enable);
718 g_ptr_array_add(widgets.document_buttons, widget);
719 g_signal_connect(widget, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy), NULL);
723 void ui_widget_show_hide(GtkWidget *widget, gboolean show)
725 if (show)
727 gtk_widget_show(widget);
729 else
731 gtk_widget_hide(widget);
736 void ui_sidebar_show_hide(void)
738 GtkWidget *widget;
740 /* check that there are no other notebook pages before hiding the sidebar completely
741 * other pages could be e.g. the file browser plugin */
742 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
743 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
745 ui_prefs.sidebar_visible = FALSE;
748 widget = ui_lookup_widget(main_widgets.window, "menu_show_sidebar1");
749 if (ui_prefs.sidebar_visible != gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
751 ignore_callback = TRUE;
752 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ui_prefs.sidebar_visible);
753 ignore_callback = FALSE;
756 ui_widget_show_hide(main_widgets.sidebar_notebook, ui_prefs.sidebar_visible);
758 ui_widget_show_hide(gtk_notebook_get_nth_page(
759 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 0), interface_prefs.sidebar_symbol_visible);
760 ui_widget_show_hide(gtk_notebook_get_nth_page(
761 GTK_NOTEBOOK(main_widgets.sidebar_notebook), 1), interface_prefs.sidebar_openfiles_visible);
765 void ui_document_show_hide(GeanyDocument *doc)
767 gchar *widget_name;
768 GtkWidget *item;
769 const GeanyIndentPrefs *iprefs;
771 if (doc == NULL)
772 doc = document_get_current();
774 if (doc == NULL)
775 return;
777 ignore_callback = TRUE;
779 gtk_check_menu_item_set_active(
780 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_line_wrapping1")),
781 doc->editor->line_wrapping);
783 gtk_check_menu_item_set_active(
784 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "line_breaking1")),
785 doc->editor->line_breaking);
787 iprefs = editor_get_indent_prefs(doc->editor);
789 item = ui_lookup_widget(main_widgets.window, "menu_use_auto_indentation1");
790 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->editor->auto_indent);
792 switch (iprefs->type)
794 case GEANY_INDENT_TYPE_SPACES:
795 widget_name = "spaces1"; break;
796 case GEANY_INDENT_TYPE_TABS:
797 widget_name = "tabs1"; break;
798 case GEANY_INDENT_TYPE_BOTH:
799 default:
800 widget_name = "tabs_and_spaces1"; break;
802 item = ui_lookup_widget(main_widgets.window, widget_name);
803 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
805 gtk_check_menu_item_set_active(
806 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "set_file_readonly1")),
807 doc->readonly);
809 item = ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1");
810 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), doc->has_bom);
811 ui_widget_set_sensitive(item, encodings_is_unicode_charset(doc->encoding));
813 switch (sci_get_eol_mode(doc->editor->sci))
815 case SC_EOL_CR: widget_name = "cr"; break;
816 case SC_EOL_LF: widget_name = "lf"; break;
817 default: widget_name = "crlf"; break;
819 gtk_check_menu_item_set_active(
820 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, widget_name)), TRUE);
822 encodings_select_radio_item(doc->encoding);
823 filetypes_select_radio_item(doc->file_type);
825 ignore_callback = FALSE;
829 void ui_set_search_entry_background(GtkWidget *widget, gboolean success)
831 static const GdkColor red = {0, 0xffff, 0x6666, 0x6666};
832 static const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
833 static gboolean old_value = TRUE;
835 g_return_if_fail(widget != NULL);
837 /* update only if really needed */
838 if (old_value != success)
840 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, success ? NULL : &red);
841 gtk_widget_modify_text(widget, GTK_STATE_NORMAL, success ? NULL : &white);
843 old_value = success;
848 static gboolean have_tango_icon_theme(void)
850 static gboolean result = FALSE;
851 static gboolean checked = FALSE;
853 if (! checked)
855 gchar *theme_name;
857 g_object_get(G_OBJECT(gtk_settings_get_default()), "gtk-icon-theme-name", &theme_name, NULL);
858 setptr(theme_name, g_utf8_strdown(theme_name, -1));
860 result = (strstr(theme_name, "tango") != NULL);
861 checked = TRUE;
863 g_free(theme_name);
866 return result;
870 /* Note: remember to unref the pixbuf once an image or window has added a reference. */
871 GdkPixbuf *ui_new_pixbuf_from_inline(gint img)
873 switch (img)
875 case GEANY_IMAGE_LOGO:
876 return gdk_pixbuf_new_from_inline(-1, aladin_inline, FALSE, NULL);
877 break;
878 case GEANY_IMAGE_SAVE_ALL:
880 /* check whether the icon theme looks like a Gnome icon theme, if so use the
881 * old Gnome based Save All icon, otherwise assume a Tango-like icon theme */
882 if (have_tango_icon_theme())
883 return gdk_pixbuf_new_from_inline(-1, save_all_tango_inline, FALSE, NULL);
884 else
885 return gdk_pixbuf_new_from_inline(-1, save_all_gnome_inline, FALSE, NULL);
886 break;
888 case GEANY_IMAGE_CLOSE_ALL:
890 return gdk_pixbuf_new_from_inline(-1, close_all_inline, FALSE, NULL);
891 break;
893 case GEANY_IMAGE_BUILD:
895 return gdk_pixbuf_new_from_inline(-1, build_inline, FALSE, NULL);
896 break;
898 default:
899 return NULL;
904 static GdkPixbuf *ui_new_pixbuf_from_stock(const gchar *stock_id)
906 if (utils_str_equal(stock_id, GEANY_STOCK_CLOSE_ALL))
907 return ui_new_pixbuf_from_inline(GEANY_IMAGE_CLOSE_ALL);
908 else if (utils_str_equal(stock_id, GEANY_STOCK_BUILD))
909 return ui_new_pixbuf_from_inline(GEANY_IMAGE_BUILD);
910 else if (utils_str_equal(stock_id, GEANY_STOCK_SAVE_ALL))
911 return ui_new_pixbuf_from_inline(GEANY_IMAGE_SAVE_ALL);
913 return NULL;
917 GtkWidget *ui_new_image_from_inline(gint img)
919 GtkWidget *wid;
920 GdkPixbuf *pb;
922 pb = ui_new_pixbuf_from_inline(img);
923 wid = gtk_image_new_from_pixbuf(pb);
924 g_object_unref(pb); /* the image doesn't adopt our reference, so remove our ref. */
925 return wid;
929 static void recent_create_menu(GeanyRecentFiles *grf)
931 GtkWidget *tmp;
932 guint i, len;
933 gchar *filename;
935 len = MIN(file_prefs.mru_length, g_queue_get_length(grf->recent_queue));
936 for (i = 0; i < len; i++)
938 filename = g_queue_peek_nth(grf->recent_queue, i);
939 /* create menu item for the recent files menu in the menu bar */
940 tmp = gtk_menu_item_new_with_label(filename);
941 gtk_widget_show(tmp);
942 gtk_container_add(GTK_CONTAINER(grf->menubar), tmp);
943 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
944 /* create menu item for the recent files menu in the toolbar */
945 if (grf->toolbar != NULL)
947 tmp = gtk_menu_item_new_with_label(filename);
948 gtk_widget_show(tmp);
949 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
950 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
956 static GeanyRecentFiles *recent_get_recent_files(void)
958 static GeanyRecentFiles grf = { RECENT_FILE_FILE, NULL, NULL, NULL, NULL };
960 if (G_UNLIKELY(grf.recent_queue == NULL))
962 grf.recent_queue = ui_prefs.recent_queue;
963 grf.menubar = ui_widgets.recent_files_menu_menubar;
964 grf.toolbar = geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
965 toolbar_get_action_by_name("Open")));
966 grf.activate_cb = recent_file_activate_cb;
968 return &grf;
972 static GeanyRecentFiles *recent_get_recent_projects(void)
974 static GeanyRecentFiles grf = { RECENT_FILE_PROJECT, NULL, NULL, NULL, NULL };
976 if (G_UNLIKELY(grf.recent_queue == NULL))
978 grf.recent_queue = ui_prefs.recent_projects_queue;
979 grf.menubar = ui_widgets.recent_projects_menu_menubar;
980 grf.toolbar = NULL;
981 grf.activate_cb = recent_project_activate_cb;
983 return &grf;
987 void ui_create_recent_menus(void)
989 recent_create_menu(recent_get_recent_files());
990 recent_create_menu(recent_get_recent_projects());
994 static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
996 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
997 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
999 if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
1000 recent_file_loaded(utf8_filename, recent_get_recent_files());
1002 g_free(locale_filename);
1003 g_free(utf8_filename);
1007 static void recent_project_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
1009 gchar *utf8_filename = ui_menu_item_get_text(menuitem);
1010 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1012 if (project_ask_close() && project_load_file_with_session(locale_filename))
1013 recent_file_loaded(utf8_filename, recent_get_recent_projects());
1015 g_free(locale_filename);
1016 g_free(utf8_filename);
1020 static void add_recent_file(const gchar *utf8_filename, GeanyRecentFiles *grf)
1022 if (g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp) == NULL)
1024 #if GTK_CHECK_VERSION(2, 10, 0)
1025 if (grf->type == RECENT_FILE_FILE)
1027 GtkRecentManager *manager = gtk_recent_manager_get_default();
1028 gchar *uri = g_filename_to_uri(utf8_filename, NULL, NULL);
1029 if (uri != NULL)
1031 gtk_recent_manager_add_item(manager, uri);
1032 g_free(uri);
1035 #endif
1036 g_queue_push_head(grf->recent_queue, g_strdup(utf8_filename));
1037 if (g_queue_get_length(grf->recent_queue) > file_prefs.mru_length)
1039 g_free(g_queue_pop_tail(grf->recent_queue));
1041 update_recent_menu(grf);
1043 /* filename already in recent list */
1044 else
1045 recent_file_loaded(utf8_filename, grf);
1049 void ui_add_recent_file(const gchar *utf8_filename)
1051 add_recent_file(utf8_filename, recent_get_recent_files());
1055 void ui_add_recent_project_file(const gchar *utf8_filename)
1057 add_recent_file(utf8_filename, recent_get_recent_projects());
1061 /* Returns: newly allocated string with the UTF-8 menu text. */
1062 gchar *ui_menu_item_get_text(GtkMenuItem *menu_item)
1064 const gchar *text = NULL;
1066 if (GTK_BIN(menu_item)->child)
1068 GtkWidget *child = GTK_BIN(menu_item)->child;
1070 if (GTK_IS_LABEL(child))
1071 text = gtk_label_get_text(GTK_LABEL(child));
1073 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1074 return g_strdup(text);
1078 static gint find_recent_file_item(gconstpointer list_data, gconstpointer user_data)
1080 gchar *menu_text = ui_menu_item_get_text(GTK_MENU_ITEM(list_data));
1081 gint result;
1083 if (utils_str_equal(menu_text, user_data))
1084 result = 0;
1085 else
1086 result = 1;
1088 g_free(menu_text);
1089 return result;
1093 static void recent_file_loaded(const gchar *utf8_filename, GeanyRecentFiles *grf)
1095 GList *item, *children;
1096 void *data;
1097 GtkWidget *tmp;
1099 /* first reorder the queue */
1100 item = g_queue_find_custom(grf->recent_queue, utf8_filename, (GCompareFunc) strcmp);
1101 g_return_if_fail(item != NULL);
1103 data = item->data;
1104 g_queue_remove(grf->recent_queue, data);
1105 g_queue_push_head(grf->recent_queue, data);
1107 /* remove the old menuitem for the filename */
1108 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1109 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1110 if (item != NULL)
1111 gtk_widget_destroy(GTK_WIDGET(item->data));
1112 g_list_free(children);
1114 if (grf->toolbar != NULL)
1116 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1117 item = g_list_find_custom(children, utf8_filename, (GCompareFunc) find_recent_file_item);
1118 if (item != NULL)
1119 gtk_widget_destroy(GTK_WIDGET(item->data));
1120 g_list_free(children);
1122 /* now prepend a new menuitem for the filename,
1123 * first for the recent files menu in the menu bar */
1124 tmp = gtk_menu_item_new_with_label(utf8_filename);
1125 gtk_widget_show(tmp);
1126 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1127 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1128 /* then for the recent files menu in the tool bar */
1129 if (grf->toolbar != NULL)
1131 tmp = gtk_menu_item_new_with_label(utf8_filename);
1132 gtk_widget_show(tmp);
1133 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1134 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1135 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal which we need in
1136 * GeanyMenubuttonAction */
1137 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1138 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1143 static void update_recent_menu(GeanyRecentFiles *grf)
1145 GtkWidget *tmp;
1146 gchar *filename;
1147 GList *children, *item;
1149 filename = g_queue_peek_head(grf->recent_queue);
1151 /* clean the MRU list before adding an item (menubar) */
1152 children = gtk_container_get_children(GTK_CONTAINER(grf->menubar));
1153 if (g_list_length(children) > file_prefs.mru_length - 1)
1155 item = g_list_nth(children, file_prefs.mru_length - 1);
1156 while (item != NULL)
1158 if (GTK_IS_MENU_ITEM(item->data))
1159 gtk_widget_destroy(GTK_WIDGET(item->data));
1160 item = g_list_next(item);
1163 g_list_free(children);
1165 /* create item for the menu bar menu */
1166 tmp = gtk_menu_item_new_with_label(filename);
1167 gtk_widget_show(tmp);
1168 gtk_menu_shell_prepend(GTK_MENU_SHELL(grf->menubar), tmp);
1169 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1171 /* clean the MRU list before adding an item (toolbar) */
1172 if (grf->toolbar != NULL)
1174 children = gtk_container_get_children(GTK_CONTAINER(grf->toolbar));
1175 if (g_list_length(children) > file_prefs.mru_length - 1)
1177 item = g_list_nth(children, file_prefs.mru_length - 1);
1178 while (item != NULL)
1180 if (GTK_IS_MENU_ITEM(item->data))
1181 gtk_widget_destroy(GTK_WIDGET(item->data));
1182 item = g_list_next(item);
1185 g_list_free(children);
1187 /* create item for the tool bar menu */
1188 tmp = gtk_menu_item_new_with_label(filename);
1189 gtk_widget_show(tmp);
1190 gtk_container_add(GTK_CONTAINER(grf->toolbar), tmp);
1191 gtk_menu_reorder_child(GTK_MENU(grf->toolbar), tmp, 0);
1192 g_signal_connect(tmp, "activate", G_CALLBACK(grf->activate_cb), NULL);
1197 void ui_toggle_editor_features(GeanyUIEditorFeatures feature)
1199 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1200 GeanyDocument *doc;
1202 for (i = 0; i < max; i++)
1204 doc = document_get_from_page(i);
1206 switch (feature)
1208 case GEANY_EDITOR_SHOW_MARKERS_MARGIN:
1209 sci_set_symbol_margin(doc->editor->sci, editor_prefs.show_markers_margin);
1210 break;
1211 case GEANY_EDITOR_SHOW_LINE_NUMBERS:
1212 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
1213 break;
1214 case GEANY_EDITOR_SHOW_WHITE_SPACE:
1215 sci_set_visible_white_spaces(doc->editor->sci, editor_prefs.show_white_space);
1216 break;
1217 case GEANY_EDITOR_SHOW_LINE_ENDINGS:
1218 sci_set_visible_eols(doc->editor->sci, editor_prefs.show_line_endings);
1219 break;
1220 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES:
1221 editor_set_indentation_guides(doc->editor);
1222 break;
1228 void ui_update_view_editor_menu_items(void)
1230 ignore_callback = TRUE;
1231 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_markers_margin1")), editor_prefs.show_markers_margin);
1232 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_linenumber_margin1")), editor_prefs.show_linenumber_margin);
1233 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);
1234 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);
1235 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);
1236 ignore_callback = FALSE;
1240 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1241 * @param label_text The label text.
1242 * @param alignment An address to store the alignment widget pointer.
1243 * @return The frame widget, setting the alignment container for packing child widgets. */
1244 GtkWidget *ui_frame_new_with_alignment(const gchar *label_text, GtkWidget **alignment)
1246 GtkWidget *label, *align;
1247 GtkWidget *frame = gtk_frame_new(NULL);
1249 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1251 align = gtk_alignment_new(0.5, 0.5, 1, 1);
1252 gtk_container_add(GTK_CONTAINER(frame), align);
1253 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
1255 label = ui_label_new_bold(label_text);
1256 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
1258 *alignment = align;
1259 return frame;
1263 const gint BUTTON_BOX_BORDER = 5;
1265 /** Convenience function for getting a fixed border for dialogs that doesn't
1266 * increase the button box border.
1267 * @param dialog The parent container for the @c GtkVBox.
1268 * @return The packed @c GtkVBox. */
1269 GtkWidget *ui_dialog_vbox_new(GtkDialog *dialog)
1271 GtkWidget *vbox = gtk_vbox_new(FALSE, 12); /* need child vbox to set a separate border. */
1273 gtk_container_set_border_width(GTK_CONTAINER(vbox), BUTTON_BOX_BORDER);
1274 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1275 return vbox;
1279 /** Create a @c GtkButton with custom text and a stock image, aligned like
1280 * @c gtk_button_new_from_stock().
1281 * @param stock_id A @c GTK_STOCK_NAME string.
1282 * @param text Button label text, can include mnemonics.
1283 * @return The new @c GtkButton.
1285 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
1287 GtkWidget *image, *label, *align, *hbox, *button;
1289 hbox = gtk_hbox_new(FALSE, 2);
1290 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
1291 label = gtk_label_new_with_mnemonic(text);
1292 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
1293 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1295 button = gtk_button_new();
1296 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
1297 gtk_container_add(GTK_CONTAINER(align), hbox);
1298 gtk_container_add(GTK_CONTAINER(button), align);
1299 gtk_widget_show_all(align);
1300 return button;
1304 /** Create 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 /** Convenience function to add 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())
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);
1527 void ui_widget_modify_font_from_string(GtkWidget *wid, const gchar *str)
1529 PangoFontDescription *pfd;
1531 pfd = pango_font_description_from_string(str);
1532 gtk_widget_modify_font(wid, pfd);
1533 pango_font_description_free(pfd);
1537 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1538 * file chooser, replacing entry text (if successful) with the path returned from the
1539 * @c GtkFileChooser.
1540 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1541 * @param title The file chooser dialog title, or @c NULL.
1542 * @param action The mode of the file chooser.
1543 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1544 * such as @c GtkComboBoxEntry.
1545 * @return The @c GtkHBox.
1547 /* @see ui_setup_open_button_callback(). */
1548 GtkWidget *ui_path_box_new(const gchar *title, GtkFileChooserAction action, GtkEntry *entry)
1550 GtkWidget *vbox, *dirbtn, *openimg, *hbox, *path_entry;
1552 hbox = gtk_hbox_new(FALSE, 6);
1553 path_entry = GTK_WIDGET(entry);
1555 /* prevent path_entry being vertically stretched to the height of dirbtn */
1556 vbox = gtk_vbox_new(FALSE, 0);
1557 if (gtk_widget_get_parent(path_entry)) /* entry->parent may be a GtkComboBoxEntry */
1559 GtkWidget *parent = gtk_widget_get_parent(path_entry);
1561 gtk_box_pack_start(GTK_BOX(vbox), parent, TRUE, FALSE, 0);
1563 else
1564 gtk_box_pack_start(GTK_BOX(vbox), path_entry, TRUE, FALSE, 0);
1566 dirbtn = gtk_button_new();
1567 openimg = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
1568 gtk_container_add(GTK_CONTAINER(dirbtn), openimg);
1569 ui_setup_open_button_callback(dirbtn, title, action, entry);
1571 gtk_box_pack_end(GTK_BOX(hbox), dirbtn, FALSE, FALSE, 0);
1572 gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1573 return hbox;
1577 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data);
1580 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1581 * title can be NULL.
1582 * action is the file chooser mode to use. */
1583 void ui_setup_open_button_callback(GtkWidget *open_btn, const gchar *title,
1584 GtkFileChooserAction action, GtkEntry *entry)
1586 GtkWidget *path_entry = GTK_WIDGET(entry);
1588 if (title)
1589 g_object_set_data_full(G_OBJECT(open_btn), "title", g_strdup(title),
1590 (GDestroyNotify) g_free);
1591 g_object_set_data(G_OBJECT(open_btn), "action", (gpointer) action);
1592 g_object_set_data_full(G_OBJECT(open_btn), "entry",
1593 g_object_ref(path_entry), (GDestroyNotify) g_object_unref);
1594 g_signal_connect(open_btn, "clicked", G_CALLBACK(ui_path_box_open_clicked), open_btn);
1598 #ifndef G_OS_WIN32
1599 static gchar *run_file_chooser(const gchar *title, GtkFileChooserAction action,
1600 const gchar *utf8_path)
1602 GtkWidget *dialog = gtk_file_chooser_dialog_new(title,
1603 GTK_WINDOW(main_widgets.window), action,
1604 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1605 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
1606 gchar *locale_path;
1607 gchar *ret_path = NULL;
1609 gtk_widget_set_name(dialog, "GeanyDialog");
1610 locale_path = utils_get_locale_from_utf8(utf8_path);
1611 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
1613 if (g_path_is_absolute(locale_path) && g_file_test(locale_path, G_FILE_TEST_IS_DIR))
1614 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
1616 g_free(locale_path);
1618 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1620 gchar *dir_locale;
1622 dir_locale = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
1623 ret_path = utils_get_utf8_from_locale(dir_locale);
1624 g_free(dir_locale);
1626 gtk_widget_destroy(dialog);
1627 return ret_path;
1629 #endif
1632 static void ui_path_box_open_clicked(GtkButton *button, gpointer user_data)
1634 GtkWidget *path_box = GTK_WIDGET(user_data);
1635 GtkFileChooserAction action =
1636 (GtkFileChooserAction) g_object_get_data(G_OBJECT(path_box), "action");
1637 GtkEntry *entry =
1638 (GtkEntry *) g_object_get_data(G_OBJECT(path_box), "entry");
1639 const gchar *title = g_object_get_data(G_OBJECT(path_box), "title");
1640 gchar *utf8_path;
1642 /* TODO: extend for other actions */
1643 g_return_if_fail(action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
1645 if (title == NULL)
1646 title = (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ?
1647 _("Select Folder") : _("Select File");
1649 #ifdef G_OS_WIN32
1650 utf8_path = win32_show_project_folder_dialog(ui_widgets.prefs_dialog, title,
1651 gtk_entry_get_text(GTK_ENTRY(entry)));
1652 #else
1653 utf8_path = run_file_chooser(title, action, gtk_entry_get_text(GTK_ENTRY(entry)));
1654 #endif
1656 if (utf8_path != NULL)
1658 gtk_entry_set_text(GTK_ENTRY(entry), utf8_path);
1659 g_free(utf8_path);
1664 void ui_statusbar_showhide(gboolean state)
1666 /* handle statusbar visibility */
1667 if (state)
1669 gtk_widget_show(ui_widgets.statusbar);
1670 ui_update_statusbar(NULL, -1);
1672 else
1673 gtk_widget_hide(ui_widgets.statusbar);
1677 /** Pack all @c GtkWidgets passed after the row argument into a table, using
1678 * one widget per cell. The first widget is not expanded as the table grows,
1679 * as this is usually a label.
1680 * @param table
1681 * @param row The row number of the table.
1683 void ui_table_add_row(GtkTable *table, gint row, ...)
1685 va_list args;
1686 gint i;
1687 GtkWidget *widget;
1689 va_start(args, row);
1690 for (i = 0; (widget = va_arg(args, GtkWidget*), widget != NULL); i++)
1692 gint options = (i == 0) ? GTK_FILL : GTK_EXPAND | GTK_FILL;
1694 gtk_table_attach(GTK_TABLE(table), widget, i, i + 1, row, row + 1,
1695 options, 0, 0, 0);
1697 va_end(args);
1701 static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
1703 const gchar *file_name = user_data;
1704 GeanyFiletype *ft = NULL;
1706 if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
1707 ft = filetypes[GEANY_FILETYPES_CONF];
1709 if (g_file_test(file_name, G_FILE_TEST_EXISTS))
1710 document_open_file(file_name, FALSE, ft, NULL);
1711 else
1713 gchar *utf8 = utils_get_utf8_from_locale(file_name);
1714 gchar *base_name = g_path_get_basename(file_name);
1715 gchar *global_file = g_build_filename(app->datadir, base_name, NULL);
1716 gchar *global_content = NULL;
1718 /* if the requested file doesn't exist in the user's config dir, try loading the file
1719 * from the global data directory and use its contents for the newly created file */
1720 if (g_file_test(global_file, G_FILE_TEST_EXISTS))
1721 g_file_get_contents(global_file, &global_content, NULL, NULL);
1723 document_new_file(utf8, ft, global_content);
1724 utils_free_pointers(utf8, base_name, global_file, global_content);
1729 /* @note You should connect to the "document-save" signal yourself to detect
1730 * if the user has just saved the config file, reloading it. */
1731 void ui_add_config_file_menu_item(const gchar *real_path, const gchar *label, GtkContainer *parent)
1733 GtkWidget *item;
1735 if (!parent)
1736 parent = GTK_CONTAINER(widgets.config_files_menu);
1738 if (!label)
1740 gchar *base_name;
1742 base_name = g_path_get_basename(real_path);
1743 item = gtk_menu_item_new_with_label(base_name);
1744 g_free(base_name);
1746 else
1747 item = gtk_menu_item_new_with_mnemonic(label);
1749 gtk_widget_show(item);
1750 gtk_container_add(parent, item);
1751 g_signal_connect(item, "activate", G_CALLBACK(on_config_file_clicked),
1752 /* this memory is kept */
1753 g_strdup(real_path));
1757 static gboolean sort_menu(gpointer data)
1759 ui_menu_sort_by_label(GTK_MENU(data));
1760 return FALSE;
1764 static void create_config_files_menu(void)
1766 GtkWidget *menu, *item;
1768 widgets.config_files_menu = menu = gtk_menu_new();
1770 item = ui_lookup_widget(main_widgets.window, "configuration_files1");
1771 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
1773 /* sort menu after all items added */
1774 g_idle_add(sort_menu, widgets.config_files_menu);
1778 void ui_init_stock_items(void)
1780 GtkIconSet *icon_set;
1781 GtkIconFactory *factory = gtk_icon_factory_new();
1782 GdkPixbuf *pb;
1783 gsize i, len;
1784 GtkStockItem items[] =
1786 { GEANY_STOCK_SAVE_ALL, _("Save All"), 0, 0, GETTEXT_PACKAGE },
1787 { GEANY_STOCK_CLOSE_ALL, _("Close All"), 0, 0, GETTEXT_PACKAGE },
1788 { GEANY_STOCK_BUILD, _("Build"), 0, 0, GETTEXT_PACKAGE }
1791 len = G_N_ELEMENTS(items);
1792 for (i = 0; i < len; i++)
1794 pb = ui_new_pixbuf_from_stock(items[i].stock_id);
1795 icon_set = gtk_icon_set_new_from_pixbuf(pb);
1797 gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
1799 gtk_icon_set_unref(icon_set);
1800 g_object_unref(pb);
1802 gtk_stock_add((GtkStockItem *) items, len);
1803 gtk_icon_factory_add_default(factory);
1804 g_object_unref(factory);
1808 void ui_init_toolbar_widgets(void)
1810 widgets.save_buttons[1] = toolbar_get_widget_by_name("Save");
1811 widgets.save_buttons[3] = toolbar_get_widget_by_name("SaveAll");
1812 widgets.redo_items[2] = toolbar_get_widget_by_name("Redo");
1813 widgets.undo_items[2] = toolbar_get_widget_by_name("Undo");
1817 static void init_recent_files(void)
1819 GtkWidget *toolbar_recent_files_menu;
1821 /* add recent files to the File menu */
1822 ui_widgets.recent_files_menuitem = ui_lookup_widget(main_widgets.window, "recent_files1");
1823 ui_widgets.recent_files_menu_menubar = gtk_menu_new();
1824 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_files_menuitem),
1825 ui_widgets.recent_files_menu_menubar);
1827 /* add recent files to the toolbar Open button */
1828 toolbar_recent_files_menu = gtk_menu_new();
1829 g_object_ref(toolbar_recent_files_menu);
1830 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
1831 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu);
1835 void ui_init(void)
1837 init_recent_files();
1839 ui_widgets.statusbar = ui_lookup_widget(main_widgets.window, "statusbar");
1840 ui_widgets.print_page_setup = ui_lookup_widget(main_widgets.window, "page_setup1");
1842 main_widgets.progressbar = progress_bar_create();
1844 widgets.popup_goto_items[0] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition1");
1845 widgets.popup_goto_items[1] = ui_lookup_widget(main_widgets.editor_menu, "goto_tag_declaration1");
1846 widgets.popup_goto_items[2] = ui_lookup_widget(main_widgets.editor_menu, "find_usage1");
1847 widgets.popup_goto_items[3] = ui_lookup_widget(main_widgets.editor_menu, "find_document_usage1");
1848 widgets.popup_copy_items[0] = ui_lookup_widget(main_widgets.editor_menu, "cut1");
1849 widgets.popup_copy_items[1] = ui_lookup_widget(main_widgets.editor_menu, "copy1");
1850 widgets.popup_copy_items[2] = ui_lookup_widget(main_widgets.editor_menu, "delete1");
1851 widgets.menu_copy_items[0] = ui_lookup_widget(main_widgets.window, "menu_cut1");
1852 widgets.menu_copy_items[1] = ui_lookup_widget(main_widgets.window, "menu_copy1");
1853 widgets.menu_copy_items[2] = ui_lookup_widget(main_widgets.window, "menu_delete1");
1854 widgets.menu_insert_include_items[0] = ui_lookup_widget(main_widgets.editor_menu, "insert_include1");
1855 widgets.menu_insert_include_items[1] = ui_lookup_widget(main_widgets.window, "insert_include2");
1856 widgets.save_buttons[0] = ui_lookup_widget(main_widgets.window, "menu_save1");
1857 widgets.save_buttons[2] = ui_lookup_widget(main_widgets.window, "menu_save_all1");
1858 widgets.redo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "redo1");
1859 widgets.redo_items[1] = ui_lookup_widget(main_widgets.window, "menu_redo2");
1860 widgets.undo_items[0] = ui_lookup_widget(main_widgets.editor_menu, "undo1");
1861 widgets.undo_items[1] = ui_lookup_widget(main_widgets.window, "menu_undo2");
1863 ui_init_toolbar_widgets();
1864 init_document_widgets();
1865 create_config_files_menu();
1869 static void auto_separator_update(GeanyAutoSeparator *autosep)
1871 g_return_if_fail(autosep->ref_count >= 0);
1873 if (autosep->widget)
1874 ui_widget_show_hide(autosep->widget, autosep->ref_count > 0);
1878 static void on_auto_separator_item_show_hide(GtkWidget *widget, gpointer user_data)
1880 GeanyAutoSeparator *autosep = user_data;
1882 if (GTK_WIDGET_VISIBLE(widget))
1883 autosep->ref_count++;
1884 else
1885 autosep->ref_count--;
1886 auto_separator_update(autosep);
1890 static void on_auto_separator_item_destroy(GtkWidget *widget, gpointer user_data)
1892 GeanyAutoSeparator *autosep = user_data;
1894 /* GTK_WIDGET_VISIBLE won't work now the widget is being destroyed,
1895 * so assume widget was visible */
1896 autosep->ref_count--;
1897 autosep->ref_count = MAX(autosep->ref_count, 0);
1898 auto_separator_update(autosep);
1902 /* Show the separator widget if @a item or another is visible. */
1903 /* Note: This would be neater taking a widget argument, setting a "visible-count"
1904 * property, and using reference counting to keep the widget alive whilst its visible group
1905 * is alive. */
1906 void ui_auto_separator_add_ref(GeanyAutoSeparator *autosep, GtkWidget *item)
1908 /* set widget ptr NULL when widget destroyed */
1909 if (autosep->ref_count == 0)
1910 g_signal_connect(autosep->widget, "destroy",
1911 G_CALLBACK(gtk_widget_destroyed), &autosep->widget);
1913 if (GTK_WIDGET_VISIBLE(item))
1915 autosep->ref_count++;
1916 auto_separator_update(autosep);
1918 g_signal_connect(item, "show", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
1919 g_signal_connect(item, "hide", G_CALLBACK(on_auto_separator_item_show_hide), autosep);
1920 g_signal_connect(item, "destroy", G_CALLBACK(on_auto_separator_item_destroy), autosep);
1925 * Sets @a text as the contents of the tooltip for @a widget.
1927 * @param widget The widget the tooltip should be set for.
1928 * @param text The text for the tooltip.
1930 * @since 0.16
1932 void ui_widget_set_tooltip_text(GtkWidget *widget, const gchar *text)
1934 #if GTK_CHECK_VERSION(2, 12, 0)
1935 gtk_widget_set_tooltip_text(widget, text);
1936 #else
1937 static GtkTooltips *tooltips = NULL;
1939 if (G_UNLIKELY(tooltips == NULL))
1940 tooltips = GTK_TOOLTIPS(ui_lookup_widget(main_widgets.window, "tooltips"));
1942 gtk_tooltips_set_tip(tooltips, widget, text, NULL);
1943 #endif
1947 /** This function returns a widget from a name in a component, usually created by Glade.
1948 * Call it with the toplevel widget in the component (i.e. a window/dialog),
1949 * or alternatively any widget in the component, and the name of the widget
1950 * you want returned.
1951 * @param widget Widget with the @a widget_name property set.
1952 * @param widget_name Name to lookup.
1953 * @return The widget found.
1954 * @see ui_hookup_widget().
1956 * @since 0.16
1958 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
1960 GtkWidget *parent, *found_widget;
1962 g_return_val_if_fail(widget != NULL, NULL);
1963 g_return_val_if_fail(widget_name != NULL, NULL);
1965 for (;;)
1967 if (GTK_IS_MENU(widget))
1968 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
1969 else
1970 parent = widget->parent;
1971 if (parent == NULL)
1972 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
1973 if (parent == NULL)
1974 break;
1975 widget = parent;
1978 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
1979 if (G_UNLIKELY(found_widget == NULL))
1980 g_warning("Widget not found: %s", widget_name);
1981 return found_widget;
1985 /* Progress Bar */
1986 static guint progress_bar_timer_id = (guint) -1;
1989 static GtkWidget *progress_bar_create(void)
1991 GtkWidget *bar = gtk_progress_bar_new();
1993 /* Set the progressbar's height to 1 to fit it in the statusbar */
1994 gtk_widget_set_size_request(bar, -1, 1);
1995 gtk_box_pack_start (GTK_BOX(ui_widgets.statusbar), bar, FALSE, FALSE, 3);
1997 return bar;
2001 static gboolean progress_bar_pulse(gpointer data)
2003 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
2005 return TRUE;
2010 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2011 * (if the statusbar is visible). This is a convenience function which adds a timer to
2012 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2013 * You can use this function when you have time consuming asynchronous operation and want to
2014 * display some activity in the GUI and when you don't know about detailed progress steps.
2015 * The progressbar widget is hidden by default when it is not active. This function and
2016 * ui_progress_bar_stop() will show and hide it automatically for you.
2018 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2019 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2020 * In this case, you need to show and hide the widget yourself. You can find some example code
2021 * in @c src/printing.c.
2023 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2025 * @since 0.16
2027 void ui_progress_bar_start(const gchar *text)
2029 g_return_if_fail(progress_bar_timer_id == (guint) -1);
2031 if (! interface_prefs.statusbar_visible)
2032 return;
2034 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
2036 progress_bar_timer_id = g_timeout_add(200, progress_bar_pulse, NULL);
2038 gtk_widget_show(GTK_WIDGET(main_widgets.progressbar));
2042 /** Stops a running progress bar and hides the widget again.
2044 * @since 0.16
2046 void ui_progress_bar_stop(void)
2048 gtk_widget_hide(GTK_WIDGET(main_widgets.progressbar));
2050 if (progress_bar_timer_id != (guint) -1)
2052 g_source_remove(progress_bar_timer_id);
2053 progress_bar_timer_id = (guint) -1;
2058 static gint compare_menu_item_labels(gconstpointer a, gconstpointer b)
2060 GtkMenuItem *item_a = GTK_MENU_ITEM(a);
2061 GtkMenuItem *item_b = GTK_MENU_ITEM(b);
2062 gchar *sa, *sb;
2063 gint result;
2065 sa = ui_menu_item_get_text(item_a);
2066 sb = ui_menu_item_get_text(item_b);
2067 result = utils_str_casecmp(sa, sb);
2068 g_free(sa);
2069 g_free(sb);
2070 return result;
2074 /* Currently @a menu should contain only GtkMenuItems with labels. */
2075 void ui_menu_sort_by_label(GtkMenu *menu)
2077 GList *list = gtk_container_get_children(GTK_CONTAINER(menu));
2078 GList *node;
2079 gint pos;
2081 list = g_list_sort(list, compare_menu_item_labels);
2082 pos = 0;
2083 foreach_list(node, list)
2085 gtk_menu_reorder_child(menu, node->data, pos);
2086 pos++;
2088 g_list_free(list);
2092 /* return value is for macros */
2093 GtkWidget *ui_label_set_markup(GtkLabel *label, const gchar *format, ...)
2095 va_list a;
2096 gchar *text;
2098 va_start(a, format);
2099 text = g_strdup_vprintf(format, a);
2100 va_end(a);
2102 gtk_label_set_text(label, text);
2103 gtk_label_set_use_markup(label, TRUE);
2104 g_free(text);
2105 return GTK_WIDGET(label);
2109 /** Add a list of document items to @a menu.
2110 * @param menu Menu.
2111 * @param active Which document to highlight, or @c NULL.
2112 * @param callback is used for each menu item's @c "activate" signal and will be passed
2113 * the corresponding document pointer as @c user_data.
2114 * @warning You should check @c doc->is_valid in the callback.
2115 * @since 0.19 */
2116 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
2118 GtkWidget *menu_item, *menu_item_label;
2119 const GdkColor *color;
2120 GeanyDocument *doc;
2121 guint i, len;
2122 gchar *base_name;
2124 len = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
2125 for (i = 0; i < len; i++)
2127 doc = document_get_from_page(i);
2128 if (! DOC_VALID(doc))
2129 continue;
2131 base_name = g_path_get_basename(DOC_FILENAME(doc));
2132 menu_item = gtk_menu_item_new_with_label(base_name);
2133 gtk_widget_show(menu_item);
2134 gtk_container_add(GTK_CONTAINER(menu), menu_item);
2135 g_signal_connect(menu_item, "activate", callback, doc);
2137 color = document_get_status_color(doc);
2138 menu_item_label = gtk_bin_get_child(GTK_BIN(menu_item));
2139 gtk_widget_modify_fg(menu_item_label, GTK_STATE_NORMAL, color);
2140 gtk_widget_modify_fg(menu_item_label, GTK_STATE_ACTIVE, color);
2142 if (doc == active)
2143 ui_label_set_markup(GTK_LABEL(menu_item_label), "<b>%s</b>", base_name);
2145 g_free(base_name);