2 * ui_utils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Matthew Brush <mbrush(at)codebrainz(dot)ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * User Interface general utility functions.
34 #include "callbacks.h"
36 #include "documentprivate.h"
37 #include "encodingsprivate.h"
38 #include "filetypes.h"
39 #include "geanymenubuttonaction.h"
42 #include "msgwindow.h"
45 #include "sciwrappers.h"
55 #include "gtkcompat.h"
60 #include <gdk/gdkkeysyms.h>
63 #define DEFAULT_STATUSBAR_TEMPLATE N_(\
73 GeanyInterfacePrefs interface_prefs
;
74 GeanyMainWidgets main_widgets
;
79 static GtkBuilder
*builder
= NULL
;
80 static GtkWidget
* window1
= NULL
;
81 static GtkWidget
* toolbar_popup_menu1
= NULL
;
82 static GtkWidget
* edit_menu1
= NULL
;
83 static GtkWidget
* prefs_dialog
= NULL
;
84 static GtkWidget
* project_dialog
= NULL
;
88 /* pointers to widgets only sensitive when there is at least one document, the pointers can
89 * also be GtkAction objects, so check each pointer before using it */
90 GPtrArray
*document_buttons
;
91 GtkWidget
*menu_insert_include_items
[2];
92 GtkWidget
*popup_goto_items
[4];
93 GtkWidget
*popup_copy_items
[3];
94 GtkWidget
*menu_copy_items
[3];
95 GtkWidget
*redo_items
[3];
96 GtkWidget
*undo_items
[3];
97 GtkWidget
*save_buttons
[4];
98 GtkWidget
*config_files_menu
;
111 GQueue
*recent_queue
;
114 void (*activate_cb
)(GtkMenuItem
*, gpointer
);
118 static void update_recent_menu(GeanyRecentFiles
*grf
);
119 static void recent_file_loaded(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
);
120 static void recent_file_activate_cb(GtkMenuItem
*menuitem
, gpointer user_data
);
121 static void recent_project_activate_cb(GtkMenuItem
*menuitem
, gpointer user_data
);
122 static GtkWidget
*progress_bar_create(void);
123 static void ui_menu_sort_by_label(GtkMenu
*menu
);
126 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
127 void ui_widget_set_sensitive(GtkWidget
*widget
, gboolean set
)
130 gtk_widget_set_sensitive(widget
, set
);
134 /* allow_override is TRUE if text can be ignored when another message has been set
135 * that didn't use allow_override and has not timed out. */
136 static void set_statusbar(const gchar
*text
, gboolean allow_override
)
139 static glong last_time
= 0;
141 const gint GEANY_STATUS_TIMEOUT
= 1;
143 if (! interface_prefs
.statusbar_visible
)
144 return; /* just do nothing if statusbar is not visible */
147 id
= gtk_statusbar_get_context_id(GTK_STATUSBAR(ui_widgets
.statusbar
), "geany-main");
149 g_get_current_time(&timeval
);
151 if (! allow_override
)
153 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets
.statusbar
), id
);
154 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets
.statusbar
), id
, text
);
155 last_time
= timeval
.tv_sec
;
158 if (timeval
.tv_sec
> last_time
+ GEANY_STATUS_TIMEOUT
)
160 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets
.statusbar
), id
);
161 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets
.statusbar
), id
, text
);
166 /** Displays text on the statusbar.
167 * @param log Whether the message should be recorded in the Status window.
168 * @param format A @c printf -style string. */
170 void ui_set_statusbar(gboolean log
, const gchar
*format
, ...)
175 va_start(args
, format
);
176 string
= g_strdup_vprintf(format
, args
);
179 if (! prefs
.suppress_status_messages
)
180 set_statusbar(string
, FALSE
);
182 if (log
|| prefs
.suppress_status_messages
)
183 msgwin_status_add("%s", string
);
189 /* note: some comments below are for translators */
190 static gchar
*create_statusbar_statistics(GeanyDocument
*doc
,
191 guint line
, guint vcol
, guint pos
)
193 const gchar
*cur_tag
;
195 const gchar
*expos
; /* % expansion position */
196 const gchar sp
[] = " ";
198 ScintillaObject
*sci
= doc
->editor
->sci
;
200 if (!EMPTY(ui_prefs
.statusbar_template
))
201 fmt
= ui_prefs
.statusbar_template
;
203 fmt
= _(DEFAULT_STATUSBAR_TEMPLATE
);
205 stats_str
= g_string_sized_new(120);
207 while ((expos
= strchr(fmt
, '%')) != NULL
)
209 /* append leading text before % char */
210 g_string_append_len(stats_str
, fmt
, expos
- fmt
);
215 g_string_append_printf(stats_str
, "%d", line
+ 1);
218 g_string_append_printf(stats_str
, "%d",
219 sci_get_line_count(doc
->editor
->sci
));
222 g_string_append_printf(stats_str
, "%d", vcol
);
225 g_string_append_printf(stats_str
, "%d", vcol
+ 1);
228 g_string_append_printf(stats_str
, "%u", pos
);
232 gint len
= sci_get_selected_text_length(sci
) - 1;
233 /* check if whole lines are selected */
234 if (!len
|| sci_get_col_from_position(sci
,
235 sci_get_selection_start(sci
)) != 0 ||
236 sci_get_col_from_position(sci
,
237 sci_get_selection_end(sci
)) != 0)
238 g_string_append_printf(stats_str
, "%d", len
);
240 g_string_append_printf(stats_str
, _("%dL"),
241 sci_get_lines_selected(doc
->editor
->sci
) - 1);
245 g_string_append_printf(stats_str
, "%d",
246 sci_get_selected_text_length(doc
->editor
->sci
) - 1);
250 g_string_append(stats_str
, (doc
->readonly
) ? _("RO ") :
251 /* OVR = overwrite/overtype, INS = insert */
252 (sci_get_overtype(doc
->editor
->sci
) ? _("OVR") : _("INS")));
257 g_string_append(stats_str
, _("RO ")); /* RO = read-only */
258 g_string_append(stats_str
, sp
+ 1);
263 switch (editor_get_indent_prefs(doc
->editor
)->type
)
265 case GEANY_INDENT_TYPE_TABS
:
266 g_string_append(stats_str
, _("TAB"));
268 case GEANY_INDENT_TYPE_SPACES
: /* SP = space */
269 g_string_append(stats_str
, _("SP"));
271 case GEANY_INDENT_TYPE_BOTH
: /* T/S = tabs and spaces */
272 g_string_append(stats_str
, _("T/S"));
280 g_string_append(stats_str
, _("MOD")); /* MOD = modified */
281 g_string_append(stats_str
, sp
);
285 g_string_append(stats_str
, utils_get_eol_short_name(sci_get_eol_mode(doc
->editor
->sci
)));
288 g_string_append(stats_str
,
289 doc
->encoding
? doc
->encoding
: _("unknown"));
290 if (encodings_is_unicode_charset(doc
->encoding
) && (doc
->has_bom
))
292 g_string_append_c(stats_str
, ' ');
293 g_string_append(stats_str
, _("(with BOM)")); /* BOM = byte order mark */
297 g_string_append(stats_str
, filetypes_get_display_name(doc
->file_type
));
300 symbols_get_current_scope(doc
, &cur_tag
);
301 g_string_append(stats_str
, cur_tag
);
304 g_string_append_c(stats_str
, ' ');
305 g_string_append_printf(stats_str
, "%d",
306 sci_get_style_at(doc
->editor
->sci
, pos
));
309 g_string_append_len(stats_str
, expos
, 1);
312 /* skip past %c chars */
318 /* add any remaining text */
319 g_string_append(stats_str
, fmt
);
321 return g_string_free(stats_str
, FALSE
);
325 /* updates the status bar document statistics */
326 void ui_update_statusbar(GeanyDocument
*doc
, gint pos
)
328 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
330 if (! interface_prefs
.statusbar_visible
)
331 return; /* just do nothing if statusbar is not visible */
334 doc
= document_get_current();
342 pos
= sci_get_current_position(doc
->editor
->sci
);
343 line
= sci_get_line_from_position(doc
->editor
->sci
, pos
);
345 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
346 * when current pos is beyond document end (can occur when removing
347 * blocks of selected lines especially esp. brace sections near end of file). */
348 if (pos
<= sci_get_length(doc
->editor
->sci
))
349 vcol
= sci_get_col_from_position(doc
->editor
->sci
, pos
);
352 vcol
+= sci_get_cursor_virtual_space(doc
->editor
->sci
);
354 stats_str
= create_statusbar_statistics(doc
, line
, vcol
, pos
);
356 /* can be overridden by status messages */
357 set_statusbar(stats_str
, TRUE
);
360 else /* no documents */
362 set_statusbar("", TRUE
); /* can be overridden by status messages */
367 /* This sets the window title according to the current filename. */
368 void ui_set_window_title(GeanyDocument
*doc
)
371 GeanyProject
*project
= app
->project
;
373 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
376 doc
= document_get_current();
378 str
= g_string_new(NULL
);
382 g_string_append(str
, doc
->changed
? "*" : "");
384 if (doc
->file_name
== NULL
)
385 g_string_append(str
, DOC_FILENAME(doc
));
388 gchar
*short_name
= document_get_basename_for_display(doc
, 30);
389 gchar
*dirname
= g_path_get_dirname(DOC_FILENAME(doc
));
391 g_string_append(str
, short_name
);
392 g_string_append(str
, " - ");
393 g_string_append(str
, dirname
? dirname
: "");
397 g_string_append(str
, " - ");
401 g_string_append_c(str
, '[');
402 g_string_append(str
, project
->name
);
403 g_string_append(str
, "] - ");
405 g_string_append(str
, "Geany");
406 if (cl_options
.new_instance
)
408 g_string_append(str
, _(" (new instance)"));
410 gtk_window_set_title(GTK_WINDOW(main_widgets
.window
), str
->str
);
411 g_string_free(str
, TRUE
);
415 void ui_set_editor_font(const gchar
*font_name
)
419 g_return_if_fail(font_name
!= NULL
);
421 /* do nothing if font has not changed */
422 if (interface_prefs
.editor_font
!= NULL
)
423 if (strcmp(font_name
, interface_prefs
.editor_font
) == 0)
426 g_free(interface_prefs
.editor_font
);
427 interface_prefs
.editor_font
= g_strdup(font_name
);
429 /* We copy the current style, and update the font in all open tabs. */
430 for (i
= 0; i
< documents_array
->len
; i
++)
432 if (documents
[i
]->editor
)
434 editor_set_font(documents
[i
]->editor
, interface_prefs
.editor_font
);
438 ui_set_statusbar(TRUE
, _("Font updated (%s)."), interface_prefs
.editor_font
);
442 void ui_set_fullscreen(void)
444 if (ui_prefs
.fullscreen
)
446 gtk_window_fullscreen(GTK_WINDOW(main_widgets
.window
));
450 gtk_window_unfullscreen(GTK_WINDOW(main_widgets
.window
));
455 void ui_update_popup_reundo_items(GeanyDocument
*doc
)
457 gboolean enable_undo
;
458 gboolean enable_redo
;
461 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
470 enable_undo
= document_can_undo(doc
);
471 enable_redo
= document_can_redo(doc
);
474 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
475 len
= G_N_ELEMENTS(widgets
.undo_items
);
476 for (i
= 0; i
< len
; i
++)
478 ui_widget_set_sensitive(widgets
.undo_items
[i
], enable_undo
);
480 len
= G_N_ELEMENTS(widgets
.redo_items
);
481 for (i
= 0; i
< len
; i
++)
483 ui_widget_set_sensitive(widgets
.redo_items
[i
], enable_redo
);
488 void ui_update_popup_copy_items(GeanyDocument
*doc
)
493 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
498 enable
= sci_has_selection(doc
->editor
->sci
);
500 len
= G_N_ELEMENTS(widgets
.popup_copy_items
);
501 for (i
= 0; i
< len
; i
++)
502 ui_widget_set_sensitive(widgets
.popup_copy_items
[i
], enable
);
506 void ui_update_popup_goto_items(gboolean enable
)
509 len
= G_N_ELEMENTS(widgets
.popup_goto_items
);
510 for (i
= 0; i
< len
; i
++)
511 ui_widget_set_sensitive(widgets
.popup_goto_items
[i
], enable
);
515 void ui_menu_copy_items_set_sensitive(gboolean sensitive
)
519 len
= G_N_ELEMENTS(widgets
.menu_copy_items
);
520 for (i
= 0; i
< len
; i
++)
521 ui_widget_set_sensitive(widgets
.menu_copy_items
[i
], sensitive
);
525 void ui_update_menu_copy_items(GeanyDocument
*doc
)
527 gboolean enable
= FALSE
;
528 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
530 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
532 if (IS_SCINTILLA(focusw
))
533 enable
= (doc
== NULL
) ? FALSE
: sci_has_selection(doc
->editor
->sci
);
535 if (GTK_IS_EDITABLE(focusw
))
536 enable
= gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw
), NULL
, NULL
);
538 if (GTK_IS_TEXT_VIEW(focusw
))
540 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(
541 GTK_TEXT_VIEW(focusw
));
542 enable
= gtk_text_buffer_get_selection_bounds(buffer
, NULL
, NULL
);
545 ui_menu_copy_items_set_sensitive(enable
);
549 void ui_update_insert_include_item(GeanyDocument
*doc
, gint item
)
551 gboolean enable
= FALSE
;
553 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
555 if (doc
== NULL
|| doc
->file_type
== NULL
)
557 else if (doc
->file_type
->id
== GEANY_FILETYPES_C
|| doc
->file_type
->id
== GEANY_FILETYPES_CPP
)
560 ui_widget_set_sensitive(widgets
.menu_insert_include_items
[item
], enable
);
564 void ui_update_fold_items(void)
566 ui_widget_show_hide(ui_lookup_widget(main_widgets
.window
, "menu_fold_all1"), editor_prefs
.folding
);
567 ui_widget_show_hide(ui_lookup_widget(main_widgets
.window
, "menu_unfold_all1"), editor_prefs
.folding
);
568 ui_widget_show_hide(ui_lookup_widget(main_widgets
.window
, "separator22"), editor_prefs
.folding
);
572 /* @include include name or NULL for empty with cursor ready for typing it */
573 static void insert_include(GeanyDocument
*doc
, gint pos
, const gchar
*include
)
578 g_return_if_fail(doc
!= NULL
);
579 g_return_if_fail(pos
== -1 || pos
>= 0);
582 pos
= sci_get_current_position(doc
->editor
->sci
);
586 text
= g_strdup("#include \"\"\n");
587 pos_after
= pos
+ 10;
591 text
= g_strconcat("#include <", include
, ">\n", NULL
);
594 sci_start_undo_action(doc
->editor
->sci
);
595 sci_insert_text(doc
->editor
->sci
, pos
, text
);
596 sci_end_undo_action(doc
->editor
->sci
);
599 sci_goto_pos(doc
->editor
->sci
, pos_after
, FALSE
);
603 static void on_popup_insert_include_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
605 insert_include(document_get_current(), editor_info
.click_pos
, user_data
);
609 static void on_menu_insert_include_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
611 insert_include(document_get_current(), -1, user_data
);
615 static void insert_include_items(GtkMenu
*me
, GtkMenu
*mp
, gchar
**includes
, gchar
*label
)
618 GtkWidget
*edit_menu
, *edit_menu_item
;
619 GtkWidget
*popup_menu
, *popup_menu_item
;
621 edit_menu
= gtk_menu_new();
622 popup_menu
= gtk_menu_new();
623 edit_menu_item
= gtk_menu_item_new_with_label(label
);
624 popup_menu_item
= gtk_menu_item_new_with_label(label
);
625 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item
), edit_menu
);
626 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item
), popup_menu
);
628 while (includes
[i
] != NULL
)
630 GtkWidget
*tmp_menu
= gtk_menu_item_new_with_label(includes
[i
]);
631 GtkWidget
*tmp_popup
= gtk_menu_item_new_with_label(includes
[i
]);
633 gtk_container_add(GTK_CONTAINER(edit_menu
), tmp_menu
);
634 gtk_container_add(GTK_CONTAINER(popup_menu
), tmp_popup
);
635 g_signal_connect(tmp_menu
, "activate",
636 G_CALLBACK(on_menu_insert_include_activate
), (gpointer
) includes
[i
]);
637 g_signal_connect(tmp_popup
, "activate",
638 G_CALLBACK(on_popup_insert_include_activate
), (gpointer
) includes
[i
]);
641 gtk_widget_show_all(edit_menu_item
);
642 gtk_widget_show_all(popup_menu_item
);
643 gtk_container_add(GTK_CONTAINER(me
), edit_menu_item
);
644 gtk_container_add(GTK_CONTAINER(mp
), popup_menu_item
);
648 void ui_create_insert_menu_items(void)
650 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "insert_include2_menu"));
651 GtkMenu
*menu_popup
= GTK_MENU(ui_lookup_widget(main_widgets
.editor_menu
, "insert_include1_menu"));
653 const gchar
*c_includes_stdlib
[] = {
654 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
655 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
657 const gchar
*c_includes_c99
[] = {
658 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
659 "tgmath.h", "wchar.h", "wctype.h", NULL
661 const gchar
*c_includes_cpp
[] = {
662 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
664 const gchar
*c_includes_cppstdlib
[] = {
665 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
666 "memory", "locale", NULL
668 const gchar
*c_includes_stl
[] = {
669 "bitset", "deque", "list", "map", "set", "queue", "stack", "vector", "algorithm",
670 "iterator", "functional", "string", "complex", "valarray", NULL
673 blank
= gtk_menu_item_new_with_label("#include \"...\"");
674 gtk_container_add(GTK_CONTAINER(menu_edit
), blank
);
675 gtk_widget_show(blank
);
676 g_signal_connect(blank
, "activate", G_CALLBACK(on_menu_insert_include_activate
), NULL
);
677 blank
= gtk_separator_menu_item_new ();
678 gtk_container_add(GTK_CONTAINER(menu_edit
), blank
);
679 gtk_widget_show(blank
);
681 blank
= gtk_menu_item_new_with_label("#include \"...\"");
682 gtk_container_add(GTK_CONTAINER(menu_popup
), blank
);
683 gtk_widget_show(blank
);
684 g_signal_connect(blank
, "activate", G_CALLBACK(on_popup_insert_include_activate
), NULL
);
685 blank
= gtk_separator_menu_item_new();
686 gtk_container_add(GTK_CONTAINER(menu_popup
), blank
);
687 gtk_widget_show(blank
);
689 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_stdlib
, _("C Standard Library"));
690 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_c99
, _("ISO C99"));
691 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_cpp
, _("C++ (C Standard Library)"));
692 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_cppstdlib
, _("C++ Standard Library"));
693 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_stl
, _("C++ STL"));
697 static void insert_date(GeanyDocument
*doc
, gint pos
, const gchar
*date_style
)
699 const gchar
*format
= NULL
;
702 g_return_if_fail(doc
!= NULL
);
703 g_return_if_fail(pos
== -1 || pos
>= 0);
706 pos
= sci_get_current_position(doc
->editor
->sci
);
708 /* set default value */
709 if (utils_str_equal("", ui_prefs
.custom_date_format
))
711 g_free(ui_prefs
.custom_date_format
);
712 ui_prefs
.custom_date_format
= g_strdup("%d.%m.%Y");
715 if (utils_str_equal(_("dd.mm.yyyy"), date_style
))
717 else if (utils_str_equal(_("mm.dd.yyyy"), date_style
))
719 else if (utils_str_equal(_("yyyy/mm/dd"), date_style
))
721 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), date_style
))
722 format
= "%d.%m.%Y %H:%M:%S";
723 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), date_style
))
724 format
= "%m.%d.%Y %H:%M:%S";
725 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), date_style
))
726 format
= "%Y/%m/%d %H:%M:%S";
727 else if (utils_str_equal(_("_Use Custom Date Format"), date_style
))
728 format
= ui_prefs
.custom_date_format
;
731 gchar
*str
= dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets
.window
),
732 _("Enter here a custom date and time format. "
733 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
734 ui_prefs
.custom_date_format
);
736 SETPTR(ui_prefs
.custom_date_format
, str
);
740 time_str
= utils_get_date_time(format
, NULL
);
741 if (time_str
!= NULL
)
743 sci_start_undo_action(doc
->editor
->sci
);
744 sci_insert_text(doc
->editor
->sci
, pos
, time_str
);
745 sci_goto_pos(doc
->editor
->sci
, pos
+ strlen(time_str
), FALSE
);
746 sci_end_undo_action(doc
->editor
->sci
);
752 ui_set_statusbar(TRUE
,
753 _("Date format string could not be converted (possibly too long)."));
758 static void on_popup_insert_date_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
760 insert_date(document_get_current(), editor_info
.click_pos
, user_data
);
764 static void on_menu_insert_date_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
766 insert_date(document_get_current(), -1, user_data
);
770 static void insert_date_items(GtkMenu
*me
, GtkMenu
*mp
, gchar
*label
)
774 item
= gtk_menu_item_new_with_mnemonic(label
);
775 gtk_container_add(GTK_CONTAINER(me
), item
);
776 gtk_widget_show(item
);
777 g_signal_connect(item
, "activate", G_CALLBACK(on_menu_insert_date_activate
), label
);
779 item
= gtk_menu_item_new_with_mnemonic(label
);
780 gtk_container_add(GTK_CONTAINER(mp
), item
);
781 gtk_widget_show(item
);
782 g_signal_connect(item
, "activate", G_CALLBACK(on_popup_insert_date_activate
), label
);
786 void ui_create_insert_date_menu_items(void)
788 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "insert_date1_menu"));
789 GtkMenu
*menu_popup
= GTK_MENU(ui_lookup_widget(main_widgets
.editor_menu
, "insert_date2_menu"));
793 insert_date_items(menu_edit
, menu_popup
, _("dd.mm.yyyy"));
794 insert_date_items(menu_edit
, menu_popup
, _("mm.dd.yyyy"));
795 insert_date_items(menu_edit
, menu_popup
, _("yyyy/mm/dd"));
797 item
= gtk_separator_menu_item_new();
798 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
799 gtk_widget_show(item
);
800 item
= gtk_separator_menu_item_new();
801 gtk_container_add(GTK_CONTAINER(menu_popup
), item
);
802 gtk_widget_show(item
);
804 insert_date_items(menu_edit
, menu_popup
, _("dd.mm.yyyy hh:mm:ss"));
805 insert_date_items(menu_edit
, menu_popup
, _("mm.dd.yyyy hh:mm:ss"));
806 insert_date_items(menu_edit
, menu_popup
, _("yyyy/mm/dd hh:mm:ss"));
808 item
= gtk_separator_menu_item_new();
809 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
810 gtk_widget_show(item
);
811 item
= gtk_separator_menu_item_new();
812 gtk_container_add(GTK_CONTAINER(menu_popup
), item
);
813 gtk_widget_show(item
);
815 str
= _("_Use Custom Date Format");
816 item
= gtk_menu_item_new_with_mnemonic(str
);
817 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
818 gtk_widget_show(item
);
819 g_signal_connect(item
, "activate", G_CALLBACK(on_menu_insert_date_activate
), str
);
820 ui_hookup_widget(main_widgets
.window
, item
, "insert_date_custom1");
822 item
= gtk_menu_item_new_with_mnemonic(str
);
823 gtk_container_add(GTK_CONTAINER(menu_popup
), item
);
824 gtk_widget_show(item
);
825 g_signal_connect(item
, "activate", G_CALLBACK(on_popup_insert_date_activate
), str
);
826 ui_hookup_widget(main_widgets
.editor_menu
, item
, "insert_date_custom2");
828 insert_date_items(menu_edit
, menu_popup
, _("_Set Custom Date Format"));
832 void ui_save_buttons_toggle(gboolean enable
)
835 gboolean dirty_tabs
= FALSE
;
837 if (ui_prefs
.allow_always_save
)
838 enable
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) > 0;
840 ui_widget_set_sensitive(widgets
.save_buttons
[0], enable
);
841 ui_widget_set_sensitive(widgets
.save_buttons
[1], enable
);
843 /* save all menu item and tool button */
844 for (i
= 0; i
< documents_array
->len
; i
++)
846 /* check whether there are files where changes were made and if there are some,
847 * we need the save all button / item */
848 if (documents
[i
]->is_valid
&& documents
[i
]->changed
)
855 ui_widget_set_sensitive(widgets
.save_buttons
[2], dirty_tabs
);
856 ui_widget_set_sensitive(widgets
.save_buttons
[3], dirty_tabs
);
860 #define add_doc_widget(widget_name) \
861 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
863 #define add_doc_toolitem(widget_name) \
864 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
866 static void init_document_widgets(void)
868 widgets
.document_buttons
= g_ptr_array_new();
870 /* Cache the document-sensitive widgets so we don't have to keep looking them up
871 * when using ui_document_buttons_update(). */
872 add_doc_widget("menu_close1");
873 add_doc_widget("close_other_documents1");
874 add_doc_widget("menu_change_font1");
875 add_doc_widget("menu_close_all1");
876 add_doc_widget("menu_save1");
877 add_doc_widget("menu_save_all1");
878 add_doc_widget("menu_save_as1");
879 add_doc_widget("menu_count_words1");
880 add_doc_widget("menu_build1");
881 add_doc_widget("add_comments1");
882 add_doc_widget("menu_paste1");
883 add_doc_widget("menu_undo2");
884 add_doc_widget("properties1");
885 add_doc_widget("menu_reload1");
886 add_doc_widget("menu_document1");
887 add_doc_widget("menu_choose_color1");
888 add_doc_widget("menu_color_schemes");
889 add_doc_widget("menu_markers_margin1");
890 add_doc_widget("menu_linenumber_margin1");
891 add_doc_widget("menu_show_white_space1");
892 add_doc_widget("menu_show_line_endings1");
893 add_doc_widget("menu_show_indentation_guides1");
894 add_doc_widget("menu_zoom_in1");
895 add_doc_widget("menu_zoom_out1");
896 add_doc_widget("normal_size1");
897 add_doc_widget("treeview6");
898 add_doc_widget("print1");
899 add_doc_widget("menu_reload_as1");
900 add_doc_widget("menu_select_all1");
901 add_doc_widget("insert_date1");
902 add_doc_widget("insert_alternative_white_space1");
903 add_doc_widget("menu_format1");
904 add_doc_widget("commands2");
905 add_doc_widget("menu_open_selected_file1");
906 add_doc_widget("page_setup1");
907 add_doc_widget("find1");
908 add_doc_widget("find_next1");
909 add_doc_widget("find_previous1");
910 add_doc_widget("go_to_next_marker1");
911 add_doc_widget("go_to_previous_marker1");
912 add_doc_widget("replace1");
913 add_doc_widget("find_nextsel1");
914 add_doc_widget("find_prevsel1");
915 add_doc_widget("find_usage1");
916 add_doc_widget("find_document_usage1");
917 add_doc_widget("mark_all1");
918 add_doc_widget("go_to_line1");
919 add_doc_widget("goto_tag_definition1");
920 add_doc_widget("goto_tag_declaration1");
921 add_doc_widget("reset_indentation1");
922 add_doc_toolitem("Close");
923 add_doc_toolitem("CloseAll");
924 add_doc_toolitem("Search");
925 add_doc_toolitem("SearchEntry");
926 add_doc_toolitem("ZoomIn");
927 add_doc_toolitem("ZoomOut");
928 add_doc_toolitem("Indent");
929 add_doc_toolitem("UnIndent");
930 add_doc_toolitem("Cut");
931 add_doc_toolitem("Copy");
932 add_doc_toolitem("Paste");
933 add_doc_toolitem("Delete");
934 add_doc_toolitem("Save");
935 add_doc_toolitem("SaveAs");
936 add_doc_toolitem("SaveAll");
937 add_doc_toolitem("Compile");
938 add_doc_toolitem("Run");
939 add_doc_toolitem("Reload");
940 add_doc_toolitem("Color");
941 add_doc_toolitem("Goto");
942 add_doc_toolitem("GotoEntry");
943 add_doc_toolitem("Replace");
944 add_doc_toolitem("Print");
948 void ui_document_buttons_update(void)
951 gboolean enable
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) > 0;
953 for (i
= 0; i
< widgets
.document_buttons
->len
; i
++)
955 GtkWidget
*widget
= g_ptr_array_index(widgets
.document_buttons
, i
);
956 if (GTK_IS_ACTION(widget
))
957 gtk_action_set_sensitive(GTK_ACTION(widget
), enable
);
959 ui_widget_set_sensitive(widget
, enable
);
964 static void on_doc_sensitive_widget_destroy(GtkWidget
*widget
, G_GNUC_UNUSED gpointer user_data
)
966 g_ptr_array_remove_fast(widgets
.document_buttons
, widget
);
970 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
971 * when some documents are present/no documents are open.
972 * It will be removed when the widget is destroyed.
973 * @param widget The widget to add.
978 void ui_add_document_sensitive(GtkWidget
*widget
)
980 gboolean enable
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) > 0;
982 ui_widget_set_sensitive(widget
, enable
);
984 g_ptr_array_add(widgets
.document_buttons
, widget
);
985 g_signal_connect(widget
, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy
), NULL
);
989 void ui_widget_show_hide(GtkWidget
*widget
, gboolean show
)
993 gtk_widget_show(widget
);
997 gtk_widget_hide(widget
);
1002 void ui_sidebar_show_hide(void)
1006 /* check that there are no other notebook pages before hiding the sidebar completely
1007 * other pages could be e.g. the file browser plugin */
1008 if (! interface_prefs
.sidebar_openfiles_visible
&& ! interface_prefs
.sidebar_symbol_visible
&&
1009 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
)) <= 2)
1011 ui_prefs
.sidebar_visible
= FALSE
;
1014 widget
= ui_lookup_widget(main_widgets
.window
, "menu_show_sidebar1");
1015 if (ui_prefs
.sidebar_visible
!= gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget
)))
1017 ignore_callback
= TRUE
;
1018 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget
), ui_prefs
.sidebar_visible
);
1019 ignore_callback
= FALSE
;
1022 ui_widget_show_hide(main_widgets
.sidebar_notebook
, ui_prefs
.sidebar_visible
);
1024 ui_widget_show_hide(gtk_notebook_get_nth_page(
1025 GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), 0), interface_prefs
.sidebar_symbol_visible
);
1026 ui_widget_show_hide(gtk_notebook_get_nth_page(
1027 GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), 1), interface_prefs
.sidebar_openfiles_visible
);
1031 void ui_document_show_hide(GeanyDocument
*doc
)
1033 const gchar
*widget_name
;
1035 const GeanyIndentPrefs
*iprefs
;
1037 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
1040 doc
= document_get_current();
1045 ignore_callback
= TRUE
;
1047 gtk_check_menu_item_set_active(
1048 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_line_wrapping1")),
1049 doc
->editor
->line_wrapping
);
1051 gtk_check_menu_item_set_active(
1052 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "line_breaking1")),
1053 doc
->editor
->line_breaking
);
1055 iprefs
= editor_get_indent_prefs(doc
->editor
);
1057 item
= ui_lookup_widget(main_widgets
.window
, "menu_use_auto_indentation1");
1058 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), doc
->editor
->auto_indent
);
1060 switch (iprefs
->type
)
1062 case GEANY_INDENT_TYPE_SPACES
:
1063 widget_name
= "spaces1"; break;
1064 case GEANY_INDENT_TYPE_TABS
:
1065 widget_name
= "tabs1"; break;
1066 case GEANY_INDENT_TYPE_BOTH
:
1068 widget_name
= "tabs_and_spaces1"; break;
1070 item
= ui_lookup_widget(main_widgets
.window
, widget_name
);
1071 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), TRUE
);
1073 if (iprefs
->width
>= 1 && iprefs
->width
<= 8)
1077 name
= g_strdup_printf("indent_width_%d", iprefs
->width
);
1078 item
= ui_lookup_widget(main_widgets
.window
, name
);
1079 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), TRUE
);
1083 gtk_check_menu_item_set_active(
1084 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "set_file_readonly1")),
1087 item
= ui_lookup_widget(main_widgets
.window
, "menu_write_unicode_bom1");
1088 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), doc
->has_bom
);
1089 ui_widget_set_sensitive(item
, encodings_is_unicode_charset(doc
->encoding
));
1091 switch (sci_get_eol_mode(doc
->editor
->sci
))
1093 case SC_EOL_CR
: widget_name
= "cr"; break;
1094 case SC_EOL_LF
: widget_name
= "lf"; break;
1095 default: widget_name
= "crlf"; break;
1097 gtk_check_menu_item_set_active(
1098 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, widget_name
)), TRUE
);
1100 encodings_select_radio_item(doc
->encoding
);
1101 filetypes_select_radio_item(doc
->file_type
);
1103 ignore_callback
= FALSE
;
1107 void ui_set_search_entry_background(GtkWidget
*widget
, gboolean success
)
1109 gtk_widget_set_name(widget
, success
? NULL
: "geany-search-entry-no-match");
1113 static void recent_create_menu(GeanyRecentFiles
*grf
)
1117 len
= MIN(file_prefs
.mru_length
, g_queue_get_length(grf
->recent_queue
));
1118 for (i
= 0; i
< len
; i
++)
1120 /* create menu item for the recent files menu in the menu bar */
1121 const gchar
*filename
= g_queue_peek_nth(grf
->recent_queue
, i
);
1122 GtkWidget
*tmp
= gtk_menu_item_new_with_label(filename
);
1124 gtk_widget_show(tmp
);
1125 gtk_container_add(GTK_CONTAINER(grf
->menubar
), tmp
);
1126 g_signal_connect(tmp
, "activate", G_CALLBACK(grf
->activate_cb
), NULL
);
1127 /* create menu item for the recent files menu in the toolbar */
1128 if (grf
->toolbar
!= NULL
)
1130 tmp
= gtk_menu_item_new_with_label(filename
);
1131 gtk_widget_show(tmp
);
1132 gtk_container_add(GTK_CONTAINER(grf
->toolbar
), tmp
);
1133 g_signal_connect(tmp
, "activate", G_CALLBACK(grf
->activate_cb
), NULL
);
1139 static GeanyRecentFiles
*recent_get_recent_files(void)
1141 static GeanyRecentFiles grf
= { RECENT_FILE_FILE
, NULL
, NULL
, NULL
, NULL
};
1143 if (G_UNLIKELY(grf
.recent_queue
== NULL
))
1145 grf
.recent_queue
= ui_prefs
.recent_queue
;
1146 grf
.menubar
= ui_widgets
.recent_files_menu_menubar
;
1147 grf
.toolbar
= geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1148 toolbar_get_action_by_name("Open")));
1149 grf
.activate_cb
= recent_file_activate_cb
;
1155 static GeanyRecentFiles
*recent_get_recent_projects(void)
1157 static GeanyRecentFiles grf
= { RECENT_FILE_PROJECT
, NULL
, NULL
, NULL
, NULL
};
1159 if (G_UNLIKELY(grf
.recent_queue
== NULL
))
1161 grf
.recent_queue
= ui_prefs
.recent_projects_queue
;
1162 grf
.menubar
= ui_widgets
.recent_projects_menu_menubar
;
1164 grf
.activate_cb
= recent_project_activate_cb
;
1170 void ui_create_recent_menus(void)
1172 recent_create_menu(recent_get_recent_files());
1173 recent_create_menu(recent_get_recent_projects());
1177 static void recent_file_activate_cb(GtkMenuItem
*menuitem
, G_GNUC_UNUSED gpointer user_data
)
1179 gchar
*utf8_filename
= ui_menu_item_get_text(menuitem
);
1180 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
1182 if (document_open_file(locale_filename
, FALSE
, NULL
, NULL
) != NULL
)
1183 recent_file_loaded(utf8_filename
, recent_get_recent_files());
1185 g_free(locale_filename
);
1186 g_free(utf8_filename
);
1190 static void recent_project_activate_cb(GtkMenuItem
*menuitem
, G_GNUC_UNUSED gpointer user_data
)
1192 gchar
*utf8_filename
= ui_menu_item_get_text(menuitem
);
1193 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
1195 if (project_ask_close() && project_load_file_with_session(locale_filename
))
1196 recent_file_loaded(utf8_filename
, recent_get_recent_projects());
1198 g_free(locale_filename
);
1199 g_free(utf8_filename
);
1203 static void add_recent_file(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
,
1204 const GtkRecentData
*rdata
)
1206 if (g_queue_find_custom(grf
->recent_queue
, utf8_filename
, (GCompareFunc
) strcmp
) == NULL
)
1209 if (grf
->type
== RECENT_FILE_FILE
&& rdata
)
1211 GtkRecentManager
*manager
= gtk_recent_manager_get_default();
1212 gchar
*uri
= g_filename_to_uri(utf8_filename
, NULL
, NULL
);
1215 gtk_recent_manager_add_full(manager
, uri
, rdata
);
1220 g_queue_push_head(grf
->recent_queue
, g_strdup(utf8_filename
));
1221 if (g_queue_get_length(grf
->recent_queue
) > file_prefs
.mru_length
)
1223 g_free(g_queue_pop_tail(grf
->recent_queue
));
1225 update_recent_menu(grf
);
1227 /* filename already in recent list */
1229 recent_file_loaded(utf8_filename
, grf
);
1233 void ui_add_recent_document(GeanyDocument
*doc
)
1235 /* what are the groups for actually? */
1236 static const gchar
*groups
[2] = {
1240 GtkRecentData rdata
;
1242 /* Prepare the data for gtk_recent_manager_add_full() */
1243 rdata
.display_name
= NULL
;
1244 rdata
.description
= NULL
;
1245 rdata
.mime_type
= doc
->file_type
->mime_type
;
1246 /* if we ain't got no mime-type, fallback to plain text */
1247 if (! rdata
.mime_type
)
1248 rdata
.mime_type
= (gchar
*) "text/plain";
1249 rdata
.app_name
= (gchar
*) "geany";
1250 rdata
.app_exec
= (gchar
*) "geany %u";
1251 rdata
.groups
= (gchar
**) groups
;
1252 rdata
.is_private
= FALSE
;
1254 add_recent_file(doc
->file_name
, recent_get_recent_files(), &rdata
);
1258 void ui_add_recent_project_file(const gchar
*utf8_filename
)
1260 add_recent_file(utf8_filename
, recent_get_recent_projects(), NULL
);
1264 /* Returns: newly allocated string with the UTF-8 menu text. */
1265 gchar
*ui_menu_item_get_text(GtkMenuItem
*menu_item
)
1267 const gchar
*text
= NULL
;
1269 if (gtk_bin_get_child(GTK_BIN(menu_item
)))
1271 GtkWidget
*child
= gtk_bin_get_child(GTK_BIN(menu_item
));
1273 if (GTK_IS_LABEL(child
))
1274 text
= gtk_label_get_text(GTK_LABEL(child
));
1276 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1277 return g_strdup(text
);
1281 static gint
find_recent_file_item(gconstpointer list_data
, gconstpointer user_data
)
1283 gchar
*menu_text
= ui_menu_item_get_text(GTK_MENU_ITEM(list_data
));
1286 if (utils_str_equal(menu_text
, user_data
))
1296 /* update the project menu item's sensitivity */
1297 void ui_update_recent_project_menu(void)
1299 GeanyRecentFiles
*grf
= recent_get_recent_projects();
1300 GList
*children
, *item
;
1302 /* only need to update the menubar menu, the project doesn't have a toolbar item */
1303 children
= gtk_container_get_children(GTK_CONTAINER(grf
->menubar
));
1304 for (item
= children
; item
; item
= item
->next
)
1306 gboolean sensitive
= TRUE
;
1310 const gchar
*filename
= gtk_menu_item_get_label(item
->data
);
1311 sensitive
= g_strcmp0(app
->project
->file_name
, filename
) != 0;
1313 gtk_widget_set_sensitive(item
->data
, sensitive
);
1315 g_list_free(children
);
1319 /* Use instead of gtk_menu_reorder_child() to update the menubar properly on OS X */
1320 static void menu_reorder_child(GtkMenu
*menu
, GtkWidget
*child
, gint position
)
1322 gtk_menu_reorder_child(menu
, child
, position
);
1323 #ifdef MAC_INTEGRATION
1324 /* On OS X GtkMenuBar is kept in sync with the native OS X menubar using
1325 * signals. Unfortunately gtk_menu_reorder_child() doesn't emit anything
1326 * so we have to update the OS X menubar manually. */
1327 gtkosx_application_sync_menubar(gtkosx_application_get());
1332 static void add_recent_file_menu_item(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
, GtkWidget
*menu
)
1334 GtkWidget
*child
= gtk_menu_item_new_with_label(utf8_filename
);
1336 gtk_widget_show(child
);
1337 if (menu
!= grf
->toolbar
)
1338 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu
), child
);
1341 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1342 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal
1343 * which we need in GeanyMenubuttonAction */
1344 gtk_container_add(GTK_CONTAINER(menu
), child
);
1345 menu_reorder_child(GTK_MENU(menu
), child
, 0);
1347 g_signal_connect(child
, "activate", G_CALLBACK(grf
->activate_cb
), NULL
);
1351 static void recent_file_loaded(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
)
1354 GtkWidget
*parents
[] = { grf
->menubar
, grf
->toolbar
};
1357 /* first reorder the queue */
1358 item
= g_queue_find_custom(grf
->recent_queue
, utf8_filename
, (GCompareFunc
) strcmp
);
1359 g_return_if_fail(item
!= NULL
);
1361 g_queue_unlink(grf
->recent_queue
, item
);
1362 g_queue_push_head_link(grf
->recent_queue
, item
);
1364 for (i
= 0; i
< G_N_ELEMENTS(parents
); i
++)
1371 children
= gtk_container_get_children(GTK_CONTAINER(parents
[i
]));
1372 item
= g_list_find_custom(children
, utf8_filename
, (GCompareFunc
) find_recent_file_item
);
1373 /* either reorder or prepend a new one */
1375 menu_reorder_child(GTK_MENU(parents
[i
]), item
->data
, 0);
1377 add_recent_file_menu_item(utf8_filename
, grf
, parents
[i
]);
1378 g_list_free(children
);
1381 if (grf
->type
== RECENT_FILE_PROJECT
)
1382 ui_update_recent_project_menu();
1386 static void update_recent_menu(GeanyRecentFiles
*grf
)
1389 GtkWidget
*parents
[] = { grf
->menubar
, grf
->toolbar
};
1392 filename
= g_queue_peek_head(grf
->recent_queue
);
1394 for (i
= 0; i
< G_N_ELEMENTS(parents
); i
++)
1401 /* clean the MRU list before adding an item */
1402 children
= gtk_container_get_children(GTK_CONTAINER(parents
[i
]));
1403 if (g_list_length(children
) > file_prefs
.mru_length
- 1)
1405 GList
*item
= g_list_nth(children
, file_prefs
.mru_length
- 1);
1406 while (item
!= NULL
)
1408 if (GTK_IS_MENU_ITEM(item
->data
))
1409 gtk_widget_destroy(GTK_WIDGET(item
->data
));
1410 item
= g_list_next(item
);
1413 g_list_free(children
);
1415 /* create the new item */
1416 add_recent_file_menu_item(filename
, grf
, parents
[i
]);
1419 if (grf
->type
== RECENT_FILE_PROJECT
)
1420 ui_update_recent_project_menu();
1424 void ui_toggle_editor_features(GeanyUIEditorFeatures feature
)
1428 foreach_document (i
)
1430 GeanyDocument
*doc
= documents
[i
];
1434 case GEANY_EDITOR_SHOW_MARKERS_MARGIN
:
1435 sci_set_symbol_margin(doc
->editor
->sci
, editor_prefs
.show_markers_margin
);
1437 case GEANY_EDITOR_SHOW_LINE_NUMBERS
:
1438 sci_set_line_numbers(doc
->editor
->sci
, editor_prefs
.show_linenumber_margin
);
1440 case GEANY_EDITOR_SHOW_WHITE_SPACE
:
1441 sci_set_visible_white_spaces(doc
->editor
->sci
, editor_prefs
.show_white_space
);
1443 case GEANY_EDITOR_SHOW_LINE_ENDINGS
:
1444 sci_set_visible_eols(doc
->editor
->sci
, editor_prefs
.show_line_endings
);
1446 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES
:
1447 editor_set_indentation_guides(doc
->editor
);
1454 void ui_update_view_editor_menu_items(void)
1456 ignore_callback
= TRUE
;
1457 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_markers_margin1")), editor_prefs
.show_markers_margin
);
1458 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_linenumber_margin1")), editor_prefs
.show_linenumber_margin
);
1459 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
);
1460 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
);
1461 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
);
1462 ignore_callback
= FALSE
;
1466 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1467 * @param label_text The label text.
1468 * @param alignment An address to store the alignment widget pointer.
1470 * @return @transfer{floating} The frame widget, setting the alignment container for
1471 * packing child widgets.
1473 * @deprecated 1.29: Use GTK API directly
1476 GtkWidget
*ui_frame_new_with_alignment(const gchar
*label_text
, GtkWidget
**alignment
)
1478 GtkWidget
*label
, *align
;
1479 GtkWidget
*frame
= gtk_frame_new(NULL
);
1481 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_NONE
);
1483 align
= gtk_alignment_new(0.5, 0.5, 1, 1);
1484 gtk_container_add(GTK_CONTAINER(frame
), align
);
1485 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 0, 0, 12, 0);
1487 label
= ui_label_new_bold(label_text
);
1488 gtk_frame_set_label_widget(GTK_FRAME(frame
), label
);
1495 /** Makes a fixed border for dialogs without increasing the button box border.
1496 * @param dialog The parent container for the @c GtkVBox.
1498 * @return @transfer{none} The packed @c GtkVBox. */
1500 GtkWidget
*ui_dialog_vbox_new(GtkDialog
*dialog
)
1502 GtkWidget
*vbox
= gtk_vbox_new(FALSE
, 12); /* need child vbox to set a separate border. */
1504 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 6);
1505 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog
))), vbox
, TRUE
, TRUE
, 0);
1510 /* Reorders a dialog's buttons
1511 * @param dialog A dialog
1512 * @param response First response ID to reorder
1513 * @param ... more response IDs, terminated by -1
1515 * Like gtk_dialog_set_alternative_button_order(), but reorders the default
1516 * buttons layout, not the alternative one. This is useful if you e.g. added a
1517 * button to a dialog which already had some and need yours not to be on the
1520 /* Heavily based on gtk_dialog_set_alternative_button_order().
1521 * This relies on the action area to be a GtkBox, but although not documented
1522 * the API expose it to be a GtkHButtonBox though GtkBuilder, so it should be
1524 void ui_dialog_set_primary_button_order(GtkDialog
*dialog
, gint response
, ...)
1527 GtkWidget
*action_area
= gtk_dialog_get_action_area(dialog
);
1530 va_start(ap
, response
);
1531 for (position
= 0; response
!= -1; position
++)
1533 GtkWidget
*child
= gtk_dialog_get_widget_for_response(dialog
, response
);
1535 gtk_box_reorder_child(GTK_BOX(action_area
), child
, position
);
1537 g_warning("%s: no child button with response id %d.", G_STRFUNC
, response
);
1539 response
= va_arg(ap
, gint
);
1545 /** Creates a @c GtkButton with custom text and a stock image similar to
1546 * @c gtk_button_new_from_stock().
1547 * @param stock_id A @c GTK_STOCK_NAME string.
1548 * @param text Button label text, can include mnemonics.
1550 * @return @transfer{floating} The new @c GtkButton.
1553 GtkWidget
*ui_button_new_with_image(const gchar
*stock_id
, const gchar
*text
)
1555 GtkWidget
*image
, *button
;
1557 button
= gtk_button_new_with_mnemonic(text
);
1558 gtk_widget_show(button
);
1559 image
= gtk_image_new_from_stock(stock_id
, GTK_ICON_SIZE_BUTTON
);
1560 gtk_button_set_image(GTK_BUTTON(button
), image
);
1561 /* note: image is shown by gtk */
1566 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1567 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1568 * @param label Menu item label, can include mnemonics.
1569 * @return @transfer{floating} The new @c GtkImageMenuItem.
1575 ui_image_menu_item_new(const gchar
*stock_id
, const gchar
*label
)
1577 GtkWidget
*item
= gtk_image_menu_item_new_with_mnemonic(label
);
1578 GtkWidget
*image
= gtk_image_new_from_stock(stock_id
, GTK_ICON_SIZE_MENU
);
1580 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
), image
);
1581 gtk_widget_show(image
);
1586 static void entry_clear_icon_release_cb(GtkEntry
*entry
, gint icon_pos
,
1587 GdkEvent
*event
, gpointer data
)
1589 if (event
->button
.button
== 1 && icon_pos
== 1)
1591 gtk_entry_set_text(entry
, "");
1592 gtk_widget_grab_focus(GTK_WIDGET(entry
));
1597 /** Adds a small clear icon to the right end of the passed @a entry.
1598 * A callback to clear the contents of the GtkEntry is automatically added.
1600 * @param entry The GtkEntry object to which the icon should be attached.
1605 void ui_entry_add_clear_icon(GtkEntry
*entry
)
1607 g_object_set(entry
, "secondary-icon-stock", GTK_STOCK_CLEAR
,
1608 "secondary-icon-activatable", TRUE
, NULL
);
1609 g_signal_connect(entry
, "icon-release", G_CALLBACK(entry_clear_icon_release_cb
), NULL
);
1613 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1614 void ui_entry_add_activate_backward_signal(GtkEntry
*entry
)
1616 static gboolean installed
= FALSE
;
1618 g_return_if_fail(GTK_IS_ENTRY(entry
));
1620 if (G_UNLIKELY(! installed
))
1622 GtkBindingSet
*binding_set
;
1626 /* try to handle the unexpected case where GTK would already have installed the signal */
1627 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry
)))
1629 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1633 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry
),
1634 G_SIGNAL_RUN_LAST
| G_SIGNAL_ACTION
, 0, NULL
, NULL
,
1635 g_cclosure_marshal_VOID__VOID
, G_TYPE_NONE
, 0);
1636 binding_set
= gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry
));
1637 gtk_binding_entry_add_signal(binding_set
, GDK_Return
, GDK_SHIFT_MASK
, "activate-backward", 0);
1642 static void add_to_size_group(GtkWidget
*widget
, gpointer size_group
)
1644 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group
));
1645 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group
), widget
);
1649 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1650 * the width of each button box's children.
1651 * Should be called after all child widgets have been packed. */
1652 void ui_hbutton_box_copy_layout(GtkButtonBox
*master
, GtkButtonBox
*copy
)
1654 GtkSizeGroup
*size_group
;
1656 gtk_box_set_spacing(GTK_BOX(copy
), 10);
1657 gtk_button_box_set_layout(copy
, gtk_button_box_get_layout(master
));
1659 /* now we need to put the widest widget from each button box in a size group,
1660 * but we don't know the width before they are drawn, and for different label
1661 * translations the widest widget can vary, so we just add all widgets. */
1662 size_group
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
1663 gtk_container_foreach(GTK_CONTAINER(master
), add_to_size_group
, size_group
);
1664 gtk_container_foreach(GTK_CONTAINER(copy
), add_to_size_group
, size_group
);
1665 g_object_unref(size_group
);
1669 static gboolean
tree_model_find_text(GtkTreeModel
*model
,
1670 GtkTreeIter
*iter
, gint column
, const gchar
*text
)
1673 gboolean found
= FALSE
;
1675 if (gtk_tree_model_get_iter_first(model
, iter
))
1679 gtk_tree_model_get(model
, iter
, 0, &combo_text
, -1);
1680 found
= utils_str_equal(combo_text
, text
);
1686 while (gtk_tree_model_iter_next(model
, iter
));
1692 /** Prepends @a text to the drop down list, removing a duplicate element in
1693 * the list if found. Also ensures there are <= @a history_len elements.
1694 * @param combo_entry .
1695 * @param text @nullable Text to add, or @c NULL for current entry text.
1696 * @param history_len Max number of items, or @c 0 for default. */
1698 void ui_combo_box_add_to_history(GtkComboBoxText
*combo_entry
,
1699 const gchar
*text
, gint history_len
)
1701 GtkComboBox
*combo
= GTK_COMBO_BOX(combo_entry
);
1702 GtkTreeModel
*model
;
1706 if (history_len
<= 0)
1709 text
= gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo
))));
1711 model
= gtk_combo_box_get_model(combo
);
1713 if (tree_model_find_text(model
, &iter
, 0, text
))
1715 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1717 gtk_combo_box_text_prepend_text(combo_entry
, text
);
1720 path
= gtk_tree_path_new_from_indices(history_len
, -1);
1721 if (gtk_tree_model_get_iter(model
, &iter
, path
))
1723 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1725 gtk_tree_path_free(path
);
1729 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1730 * exists in the combo's model. */
1731 void ui_combo_box_prepend_text_once(GtkComboBoxText
*combo
, const gchar
*text
)
1733 GtkTreeModel
*model
;
1736 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(combo
));
1737 if (tree_model_find_text(model
, &iter
, 0, text
))
1738 return; /* don't prepend duplicate */
1740 gtk_combo_box_text_prepend_text(combo
, text
);
1744 /* Changes the color of the notebook tab text and open files items according to
1745 * document status. */
1746 void ui_update_tab_status(GeanyDocument
*doc
)
1748 gtk_widget_set_name(doc
->priv
->tab_label
, document_get_status_widget_class(doc
));
1750 sidebar_openfiles_update(doc
);
1754 static gboolean
tree_model_iter_get_next(GtkTreeModel
*model
, GtkTreeIter
*iter
,
1761 return gtk_tree_model_iter_next(model
, iter
);
1763 path
= gtk_tree_model_get_path(model
, iter
);
1764 result
= gtk_tree_path_prev(path
) && gtk_tree_model_get_iter(model
, iter
, path
);
1765 gtk_tree_path_free(path
);
1770 /* note: the while loop might be more efficient when searching upwards if it
1771 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1772 static gboolean
tree_view_find(GtkTreeView
*treeview
, TVMatchCallback cb
, gboolean down
)
1774 GtkTreeSelection
*treesel
;
1776 GtkTreeModel
*model
;
1778 treesel
= gtk_tree_view_get_selection(treeview
);
1779 if (gtk_tree_selection_get_selected(treesel
, &model
, &iter
))
1781 /* get the next selected item */
1782 if (! tree_model_iter_get_next(model
, &iter
, down
))
1783 return FALSE
; /* no more items */
1785 else /* no selection */
1787 if (! gtk_tree_model_get_iter_first(model
, &iter
))
1788 return TRUE
; /* no items */
1792 gtk_tree_selection_select_iter(treesel
, &iter
);
1794 break; /* found next message */
1796 if (! tree_model_iter_get_next(model
, &iter
, down
))
1797 return FALSE
; /* no more items */
1799 /* scroll item in view */
1800 if (ui_prefs
.msgwindow_visible
)
1802 GtkTreePath
*path
= gtk_tree_model_get_path(
1803 gtk_tree_view_get_model(treeview
), &iter
);
1805 gtk_tree_view_scroll_to_cell(treeview
, path
, NULL
, TRUE
, 0.5, 0.5);
1806 gtk_tree_path_free(path
);
1812 /* Returns FALSE if the treeview has items but no matching next item. */
1813 gboolean
ui_tree_view_find_next(GtkTreeView
*treeview
, TVMatchCallback cb
)
1815 return tree_view_find(treeview
, cb
, TRUE
);
1819 /* Returns FALSE if the treeview has items but no matching next item. */
1820 gboolean
ui_tree_view_find_previous(GtkTreeView
*treeview
, TVMatchCallback cb
)
1822 return tree_view_find(treeview
, cb
, FALSE
);
1826 /* Shamelessly stolen from GTK */
1827 static gboolean
ui_tree_view_query_tooltip_cb(GtkWidget
*widget
, gint x
, gint y
,
1828 gboolean keyboard_tip
, GtkTooltip
*tooltip
, gpointer data
)
1830 GValue value
= { 0 };
1831 GValue transformed
= { 0 };
1834 GtkTreeModel
*model
;
1835 GtkTreeView
*tree_view
= GTK_TREE_VIEW(widget
);
1836 gint column
= GPOINTER_TO_INT(data
);
1837 gboolean tootlip_set
= FALSE
;
1839 if (! gtk_tree_view_get_tooltip_context(tree_view
, &x
, &y
, keyboard_tip
, &model
, &path
, &iter
))
1842 gtk_tree_model_get_value(model
, &iter
, column
, &value
);
1844 g_value_init(&transformed
, G_TYPE_STRING
);
1845 if (g_value_transform(&value
, &transformed
) && g_value_get_string(&transformed
))
1847 gtk_tooltip_set_text(tooltip
, g_value_get_string(&transformed
));
1848 gtk_tree_view_set_tooltip_row(tree_view
, tooltip
, path
);
1852 g_value_unset(&transformed
);
1853 g_value_unset(&value
);
1854 gtk_tree_path_free(path
);
1860 /** Adds text tooltips to a tree view.
1862 * This is similar to gtk_tree_view_set_tooltip_column() but considers the column contents to be
1863 * text, not markup -- it uses gtk_tooltip_set_text() rather than gtk_tooltip_set_markup() to set
1864 * the tooltip's value.
1866 * @warning Unlike gtk_tree_view_set_tooltip_column() you currently cannot change or remove the
1867 * tooltip column after it has been added. Trying to do so will probably give funky results.
1869 * @param tree_view The tree view
1870 * @param column The column to get the tooltip from
1872 * @since 1.25 (API 223)
1874 /* Note: @p column is int and not uint both to match gtk_tree_view_set_tooltip_column() signature
1875 * and to allow future support of -1 to unset if ever wanted */
1877 void ui_tree_view_set_tooltip_text_column(GtkTreeView
*tree_view
, gint column
)
1879 g_return_if_fail(column
>= 0);
1880 g_return_if_fail(GTK_IS_TREE_VIEW(tree_view
));
1882 g_signal_connect(tree_view
, "query-tooltip",
1883 G_CALLBACK(ui_tree_view_query_tooltip_cb
), GINT_TO_POINTER(column
));
1884 gtk_widget_set_has_tooltip(GTK_WIDGET(tree_view
), TRUE
);
1889 * Modifies the font of a widget using gtk_widget_modify_font().
1891 * @param widget The widget.
1892 * @param str The font name as expected by pango_font_description_from_string().
1895 void ui_widget_modify_font_from_string(GtkWidget
*widget
, const gchar
*str
)
1897 PangoFontDescription
*pfd
;
1899 pfd
= pango_font_description_from_string(str
);
1900 gtk_widget_modify_font(widget
, pfd
);
1901 pango_font_description_free(pfd
);
1905 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1906 * file chooser, replacing entry text (if successful) with the path returned from the
1907 * @c GtkFileChooser.
1908 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1909 * @param title @nullable The file chooser dialog title, or @c NULL.
1910 * @param action The mode of the file chooser.
1911 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1912 * such as @c GtkComboBoxEntry.
1914 * @return @transfer{floating} The @c GtkHBox.
1916 /* @see ui_setup_open_button_callback(). */
1918 GtkWidget
*ui_path_box_new(const gchar
*title
, GtkFileChooserAction action
, GtkEntry
*entry
)
1920 GtkWidget
*vbox
, *dirbtn
, *openimg
, *hbox
, *path_entry
, *parent
, *next_parent
;
1922 hbox
= gtk_hbox_new(FALSE
, 6);
1923 path_entry
= GTK_WIDGET(entry
);
1925 /* prevent path_entry being vertically stretched to the height of dirbtn */
1926 vbox
= gtk_vbox_new(FALSE
, 0);
1928 parent
= path_entry
;
1929 while ((next_parent
= gtk_widget_get_parent(parent
)) != NULL
)
1930 parent
= next_parent
;
1932 gtk_box_pack_start(GTK_BOX(vbox
), parent
, TRUE
, FALSE
, 0);
1934 dirbtn
= gtk_button_new();
1935 openimg
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
1936 gtk_container_add(GTK_CONTAINER(dirbtn
), openimg
);
1937 ui_setup_open_button_callback(dirbtn
, title
, action
, entry
);
1939 gtk_box_pack_end(GTK_BOX(hbox
), dirbtn
, FALSE
, FALSE
, 0);
1940 gtk_box_pack_end(GTK_BOX(hbox
), vbox
, TRUE
, TRUE
, 0);
1945 static void ui_path_box_open_clicked(GtkButton
*button
, gpointer user_data
);
1948 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1949 * title can be NULL.
1950 * action is the file chooser mode to use. */
1951 void ui_setup_open_button_callback(GtkWidget
*open_btn
, const gchar
*title
,
1952 GtkFileChooserAction action
, GtkEntry
*entry
)
1954 GtkWidget
*path_entry
= GTK_WIDGET(entry
);
1957 g_object_set_data_full(G_OBJECT(open_btn
), "title", g_strdup(title
),
1958 (GDestroyNotify
) g_free
);
1959 g_object_set_data(G_OBJECT(open_btn
), "action", GINT_TO_POINTER(action
));
1960 g_signal_connect(open_btn
, "clicked", G_CALLBACK(ui_path_box_open_clicked
), path_entry
);
1965 static gchar
*run_file_chooser(const gchar
*title
, GtkFileChooserAction action
,
1966 const gchar
*utf8_path
)
1968 GtkWidget
*dialog
= gtk_file_chooser_dialog_new(title
,
1969 GTK_WINDOW(main_widgets
.window
), action
,
1970 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1971 GTK_STOCK_OPEN
, GTK_RESPONSE_OK
, NULL
);
1973 gchar
*ret_path
= NULL
;
1975 gtk_widget_set_name(dialog
, "GeanyDialog");
1976 locale_path
= utils_get_locale_from_utf8(utf8_path
);
1977 if (action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
)
1979 if (g_path_is_absolute(locale_path
) && g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
1980 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
1982 else if (action
== GTK_FILE_CHOOSER_ACTION_OPEN
)
1984 if (g_path_is_absolute(locale_path
))
1985 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), locale_path
);
1987 g_free(locale_path
);
1989 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_OK
)
1993 dir_locale
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
1994 ret_path
= utils_get_utf8_from_locale(dir_locale
);
1997 gtk_widget_destroy(dialog
);
2003 static void ui_path_box_open_clicked(GtkButton
*button
, gpointer user_data
)
2005 GtkFileChooserAction action
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button
), "action"));
2006 GtkEntry
*entry
= user_data
;
2007 const gchar
*title
= g_object_get_data(G_OBJECT(button
), "title");
2008 gchar
*utf8_path
= NULL
;
2010 /* TODO: extend for other actions */
2011 g_return_if_fail(action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
||
2012 action
== GTK_FILE_CHOOSER_ACTION_OPEN
);
2015 title
= (action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
) ?
2016 _("Select Folder") : _("Select File");
2018 if (action
== GTK_FILE_CHOOSER_ACTION_OPEN
)
2021 utf8_path
= win32_show_file_dialog(GTK_WINDOW(ui_widgets
.prefs_dialog
), title
,
2022 gtk_entry_get_text(GTK_ENTRY(entry
)));
2024 utf8_path
= run_file_chooser(title
, action
, gtk_entry_get_text(GTK_ENTRY(entry
)));
2027 else if (action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
)
2029 gchar
*path
= g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry
)));
2031 utf8_path
= win32_show_folder_dialog(ui_widgets
.prefs_dialog
, title
,
2032 gtk_entry_get_text(GTK_ENTRY(entry
)));
2034 utf8_path
= run_file_chooser(title
, action
, path
);
2039 if (utf8_path
!= NULL
)
2041 gtk_entry_set_text(GTK_ENTRY(entry
), utf8_path
);
2047 void ui_statusbar_showhide(gboolean state
)
2049 /* handle statusbar visibility */
2052 gtk_widget_show(ui_widgets
.statusbar
);
2053 ui_update_statusbar(NULL
, -1);
2056 gtk_widget_hide(ui_widgets
.statusbar
);
2060 /** Packs all @c GtkWidgets passed after the row argument into a table, using
2061 * one widget per cell. The first widget is not expanded as the table grows,
2062 * as this is usually a label.
2064 * @param row The row number of the table.
2067 void ui_table_add_row(GtkTable
*table
, gint row
, ...)
2073 va_start(args
, row
);
2074 for (i
= 0; (widget
= va_arg(args
, GtkWidget
*), widget
!= NULL
); i
++)
2076 gint options
= (i
== 0) ? GTK_FILL
: GTK_EXPAND
| GTK_FILL
;
2078 gtk_table_attach(GTK_TABLE(table
), widget
, i
, i
+ 1, row
, row
+ 1,
2085 static void on_config_file_clicked(GtkWidget
*widget
, gpointer user_data
)
2087 const gchar
*file_name
= user_data
;
2088 GeanyFiletype
*ft
= NULL
;
2090 if (strstr(file_name
, G_DIR_SEPARATOR_S
"filetypes."))
2091 ft
= filetypes
[GEANY_FILETYPES_CONF
];
2093 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
))
2094 document_open_file(file_name
, FALSE
, ft
, NULL
);
2097 gchar
*utf8_filename
= utils_get_utf8_from_locale(file_name
);
2099 gchar
*base_name
= NULL
;
2100 gchar
*global_content
= NULL
;
2102 /* get the path inside app->configdir - can contain subdirectories */
2103 if (g_str_has_prefix(file_name
, app
->configdir
))
2105 gsize len
= strlen(app
->configdir
);
2106 if (file_name
[len
] == G_DIR_SEPARATOR
)
2107 base_name
= g_strdup(file_name
+ len
+ 1);
2111 base_name
= g_path_get_basename(file_name
);
2113 global_file
= g_build_filename(app
->datadir
, base_name
, NULL
);
2115 /* if the requested file doesn't exist in the user's config dir, try loading the file
2116 * from the global data directory and use its contents for the newly created file */
2117 if (g_file_test(global_file
, G_FILE_TEST_EXISTS
))
2118 g_file_get_contents(global_file
, &global_content
, NULL
, NULL
);
2120 document_new_file(utf8_filename
, ft
, global_content
);
2122 utils_free_pointers(4, utf8_filename
, base_name
, global_file
, global_content
, NULL
);
2127 static void free_on_closure_notify(gpointer data
, GClosure
*closure
)
2133 /* @note You should connect to the "document-save" signal yourself to detect
2134 * if the user has just saved the config file, reloading it. */
2135 void ui_add_config_file_menu_item(const gchar
*real_path
, const gchar
*label
, GtkContainer
*parent
)
2140 parent
= GTK_CONTAINER(widgets
.config_files_menu
);
2146 base_name
= g_path_get_basename(real_path
);
2147 item
= gtk_menu_item_new_with_label(base_name
);
2151 item
= gtk_menu_item_new_with_mnemonic(label
);
2153 gtk_widget_show(item
);
2154 gtk_container_add(parent
, item
);
2155 g_signal_connect_data(item
, "activate", G_CALLBACK(on_config_file_clicked
),
2156 g_strdup(real_path
), free_on_closure_notify
, 0);
2160 static gboolean
sort_menu(gpointer data
)
2162 ui_menu_sort_by_label(GTK_MENU(data
));
2167 static void create_config_files_menu(void)
2169 GtkWidget
*menu
, *item
;
2171 widgets
.config_files_menu
= menu
= gtk_menu_new();
2173 item
= ui_lookup_widget(main_widgets
.window
, "configuration_files1");
2174 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), menu
);
2176 item
= gtk_menu_item_new_with_mnemonic(_("_Filetype Configuration"));
2177 gtk_container_add(GTK_CONTAINER(menu
), item
);
2178 ui_widgets
.config_files_filetype_menu
= gtk_menu_new();
2179 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), ui_widgets
.config_files_filetype_menu
);
2180 gtk_widget_show(item
);
2182 /* sort menu after all items added */
2183 g_idle_add(sort_menu
, widgets
.config_files_menu
);
2187 /* adds factory icons with a named icon source using the stock items id */
2188 static void add_stock_icons(const GtkStockItem
*items
, gsize count
)
2190 GtkIconFactory
*factory
= gtk_icon_factory_new();
2191 GtkIconSource
*source
= gtk_icon_source_new();
2194 for (i
= 0; i
< count
; i
++)
2196 GtkIconSet
*set
= gtk_icon_set_new();
2198 gtk_icon_source_set_icon_name(source
, items
[i
].stock_id
);
2199 gtk_icon_set_add_source(set
, source
);
2200 gtk_icon_factory_add(factory
, items
[i
].stock_id
, set
);
2201 gtk_icon_set_unref(set
);
2203 gtk_icon_source_free(source
);
2204 gtk_icon_factory_add_default(factory
);
2205 g_object_unref(factory
);
2209 void ui_init_stock_items(void)
2211 GtkStockItem items
[] =
2213 { GEANY_STOCK_SAVE_ALL
, N_("Save All"), 0, 0, GETTEXT_PACKAGE
},
2214 { GEANY_STOCK_CLOSE_ALL
, N_("Close All"), 0, 0, GETTEXT_PACKAGE
},
2215 { GEANY_STOCK_BUILD
, N_("Build"), 0, 0, GETTEXT_PACKAGE
}
2218 gtk_stock_add(items
, G_N_ELEMENTS(items
));
2219 add_stock_icons(items
, G_N_ELEMENTS(items
));
2223 void ui_init_toolbar_widgets(void)
2225 widgets
.save_buttons
[1] = toolbar_get_widget_by_name("Save");
2226 widgets
.save_buttons
[3] = toolbar_get_widget_by_name("SaveAll");
2227 widgets
.redo_items
[2] = toolbar_get_widget_by_name("Redo");
2228 widgets
.undo_items
[2] = toolbar_get_widget_by_name("Undo");
2232 void ui_swap_sidebar_pos(void)
2234 GtkWidget
*pane
= ui_lookup_widget(main_widgets
.window
, "hpaned1");
2235 GtkWidget
*left
= gtk_paned_get_child1(GTK_PANED(pane
));
2236 GtkWidget
*right
= gtk_paned_get_child2(GTK_PANED(pane
));
2239 g_object_ref(right
);
2240 gtk_container_remove (GTK_CONTAINER (pane
), left
);
2241 gtk_container_remove (GTK_CONTAINER (pane
), right
);
2242 /* only scintilla notebook should expand */
2243 gtk_paned_pack1(GTK_PANED(pane
), right
, right
== main_widgets
.notebook
, TRUE
);
2244 gtk_paned_pack2(GTK_PANED(pane
), left
, left
== main_widgets
.notebook
, TRUE
);
2245 g_object_unref(left
);
2246 g_object_unref(right
);
2248 gtk_paned_set_position(GTK_PANED(pane
), gtk_widget_get_allocated_width(pane
)
2249 - gtk_paned_get_position(GTK_PANED(pane
)));
2253 static void init_recent_files(void)
2255 GtkWidget
*toolbar_recent_files_menu
;
2257 /* add recent files to the File menu */
2258 ui_widgets
.recent_files_menuitem
= ui_lookup_widget(main_widgets
.window
, "recent_files1");
2259 ui_widgets
.recent_files_menu_menubar
= gtk_menu_new();
2260 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets
.recent_files_menuitem
),
2261 ui_widgets
.recent_files_menu_menubar
);
2263 /* add recent files to the toolbar Open button */
2264 toolbar_recent_files_menu
= gtk_menu_new();
2265 g_object_ref(toolbar_recent_files_menu
);
2266 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
2267 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu
);
2271 static void ui_menu_move(GtkWidget
*menu
, GtkWidget
*old
, GtkWidget
*new)
2274 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old
), NULL
);
2275 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu
);
2276 g_object_unref(menu
);
2280 typedef struct GeanySharedMenu
2283 const gchar
*menubar_item
;
2284 const gchar
*popup_item
;
2288 #define foreach_menu(item, array) \
2289 for (item = array; item->menu; item++)
2291 static void on_editor_menu_show(GtkWidget
*widget
, GeanySharedMenu
*items
)
2293 GeanySharedMenu
*item
;
2295 foreach_menu(item
, items
)
2297 GtkWidget
*popup
= ui_lookup_widget(main_widgets
.editor_menu
, item
->popup_item
);
2298 GtkWidget
*bar
= ui_lookup_widget(main_widgets
.window
, item
->menubar_item
);
2299 GtkWidget
*menu
= ui_lookup_widget(main_widgets
.window
, item
->menu
);
2301 ui_menu_move(menu
, bar
, popup
);
2306 static void on_editor_menu_hide(GtkWidget
*widget
, GeanySharedMenu
*items
)
2308 GeanySharedMenu
*item
;
2310 foreach_menu(item
, items
)
2312 GtkWidget
*popup
= ui_lookup_widget(main_widgets
.editor_menu
, item
->popup_item
);
2313 GtkWidget
*bar
= ui_lookup_widget(main_widgets
.window
, item
->menubar_item
);
2314 GtkWidget
*menu
= ui_lookup_widget(main_widgets
.window
, item
->menu
);
2316 ui_menu_move(menu
, popup
, bar
);
2321 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2322 * so this is called after that's done. */
2323 void ui_init_prefs(void)
2325 StashGroup
*group
= stash_group_new(PACKAGE
);
2328 configuration_add_various_pref_group(group
);
2330 stash_group_add_boolean(group
, &interface_prefs
.show_symbol_list_expanders
,
2331 "show_symbol_list_expanders", TRUE
);
2332 stash_group_add_boolean(group
, &interface_prefs
.compiler_tab_autoscroll
,
2333 "compiler_tab_autoscroll", TRUE
);
2334 stash_group_add_boolean(group
, &ui_prefs
.allow_always_save
,
2335 "allow_always_save", FALSE
);
2336 stash_group_add_string(group
, &ui_prefs
.statusbar_template
,
2337 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE
));
2338 stash_group_add_boolean(group
, &ui_prefs
.new_document_after_close
,
2339 "new_document_after_close", FALSE
);
2340 stash_group_add_boolean(group
, &interface_prefs
.msgwin_status_visible
,
2341 "msgwin_status_visible", TRUE
);
2342 stash_group_add_boolean(group
, &interface_prefs
.msgwin_compiler_visible
,
2343 "msgwin_compiler_visible", TRUE
);
2344 stash_group_add_boolean(group
, &interface_prefs
.msgwin_messages_visible
,
2345 "msgwin_messages_visible", TRUE
);
2346 stash_group_add_boolean(group
, &interface_prefs
.msgwin_scribble_visible
,
2347 "msgwin_scribble_visible", TRUE
);
2351 /* Used to find out the name of the GtkBuilder retrieved object since
2352 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2353 * 'name' property for that and those that don't implement GtkBuildable
2354 * will have a "gtk-builder-name" stored in the GObject's data list. */
2355 static const gchar
*ui_guess_object_name(GObject
*obj
)
2357 const gchar
*name
= NULL
;
2359 g_return_val_if_fail(G_IS_OBJECT(obj
), NULL
);
2361 if (GTK_IS_BUILDABLE(obj
))
2362 name
= gtk_buildable_get_name(GTK_BUILDABLE(obj
));
2364 name
= g_object_get_data(obj
, "gtk-builder-name");
2372 /* Compatibility functions */
2373 GtkWidget
*create_edit_menu1(void)
2379 GtkWidget
*create_prefs_dialog(void)
2381 return prefs_dialog
;
2385 GtkWidget
*create_project_dialog(void)
2387 return project_dialog
;
2391 GtkWidget
*create_toolbar_popup_menu1(void)
2393 return toolbar_popup_menu1
;
2397 GtkWidget
*create_window1(void)
2403 static GtkWidget
*ui_get_top_parent(GtkWidget
*widget
)
2407 g_return_val_if_fail(GTK_IS_WIDGET(widget
), NULL
);
2411 if (GTK_IS_MENU(widget
))
2412 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
2414 parent
= gtk_widget_get_parent(widget
);
2416 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
2426 void ui_init_builder(void)
2428 gchar
*interface_file
;
2431 GSList
*iter
, *all_objects
;
2432 GtkWidget
*widget
, *toplevel
;
2434 /* prevent function from being called twice */
2435 if (GTK_IS_BUILDER(builder
))
2438 builder
= gtk_builder_new();
2440 gtk_builder_set_translation_domain(builder
, GETTEXT_PACKAGE
);
2443 interface_file
= g_build_filename(app
->datadir
, "geany.glade", NULL
);
2444 if (! gtk_builder_add_from_file(builder
, interface_file
, &error
))
2446 /* Show the user this message so they know WTF happened */
2447 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR
,
2448 _("Geany cannot start!"), error
->message
);
2450 g_error("Cannot create user-interface: %s", error
->message
);
2451 g_error_free(error
);
2452 g_free(interface_file
);
2453 g_object_unref(builder
);
2456 g_free(interface_file
);
2458 callbacks_connect(builder
);
2460 edit_menu1
= GTK_WIDGET(gtk_builder_get_object(builder
, "edit_menu1"));
2461 prefs_dialog
= GTK_WIDGET(gtk_builder_get_object(builder
, "prefs_dialog"));
2462 project_dialog
= GTK_WIDGET(gtk_builder_get_object(builder
, "project_dialog"));
2463 toolbar_popup_menu1
= GTK_WIDGET(gtk_builder_get_object(builder
, "toolbar_popup_menu1"));
2464 window1
= GTK_WIDGET(gtk_builder_get_object(builder
, "window1"));
2466 g_object_set_data(G_OBJECT(edit_menu1
), "edit_menu1", edit_menu1
);
2467 g_object_set_data(G_OBJECT(prefs_dialog
), "prefs_dialog", prefs_dialog
);
2468 g_object_set_data(G_OBJECT(project_dialog
), "project_dialog", project_dialog
);
2469 g_object_set_data(G_OBJECT(toolbar_popup_menu1
), "toolbar_popup_menu1", toolbar_popup_menu1
);
2470 g_object_set_data(G_OBJECT(window1
), "window1", window1
);
2472 all_objects
= gtk_builder_get_objects(builder
);
2473 for (iter
= all_objects
; iter
!= NULL
; iter
= g_slist_next(iter
))
2475 if (! GTK_IS_WIDGET(iter
->data
))
2478 widget
= GTK_WIDGET(iter
->data
);
2480 name
= ui_guess_object_name(G_OBJECT(widget
));
2483 g_warning("Unable to get name from GtkBuilder object");
2487 toplevel
= ui_get_top_parent(widget
);
2489 ui_hookup_widget(toplevel
, widget
, name
);
2491 g_slist_free(all_objects
);
2495 #if GTK_CHECK_VERSION(3, 0, 0)
2496 static void load_css_theme(const gchar
*fn
, guint priority
)
2498 GtkCssProvider
*provider
= gtk_css_provider_new();
2499 GError
*error
= NULL
;
2501 if (! gtk_css_provider_load_from_path(provider
, fn
, &error
))
2503 g_warning("Failed to load custom CSS: %s", error
->message
);
2504 g_error_free(error
);
2508 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2509 GTK_STYLE_PROVIDER(provider
), priority
);
2510 geany_debug("Loaded GTK+ CSS theme '%s'", fn
);
2512 g_object_unref(provider
);
2516 // see setup_gtk2_styles() in libmain.c for GTK+ 2-specific theme initialization
2517 static void init_css_styles(void)
2521 // load the main geany.css file from system data dir
2522 theme_fn
= g_build_filename(app
->datadir
, "geany.css", NULL
);
2523 load_css_theme(theme_fn
, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
2526 // load themes to handle breakage between various GTK+ versions
2535 { 20, G_MAXUINT
, "geany-3.20.css" },
2536 { 0, 19, "geany-3.0.css" },
2539 guint gtk_version
= gtk_get_minor_version();
2540 for (guint i
= 0; i
< G_N_ELEMENTS(css_files
); i
++)
2542 if (gtk_version
>= css_files
[i
].min_version
&&
2543 gtk_version
<= css_files
[i
].max_version
)
2545 theme_fn
= g_build_filename(app
->datadir
, css_files
[i
].file
, NULL
);
2546 load_css_theme(theme_fn
, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
2551 // if the user provided a geany.css file in their config dir, try and load that
2552 theme_fn
= g_build_filename(app
->configdir
, "geany.css", NULL
);
2553 if (g_file_test(theme_fn
, G_FILE_TEST_EXISTS
))
2554 load_css_theme(theme_fn
, GTK_STYLE_PROVIDER_PRIORITY_USER
);
2559 static void add_css_config_file_item(void)
2562 theme_fn
= g_build_filename(app
->configdir
, "geany.css", NULL
);
2563 ui_add_config_file_menu_item(theme_fn
, NULL
, NULL
);
2571 #if GTK_CHECK_VERSION(3, 0, 0)
2575 init_recent_files();
2577 ui_widgets
.statusbar
= ui_lookup_widget(main_widgets
.window
, "statusbar");
2578 ui_widgets
.print_page_setup
= ui_lookup_widget(main_widgets
.window
, "page_setup1");
2580 main_widgets
.progressbar
= progress_bar_create();
2582 /* current word sensitive items */
2583 widgets
.popup_goto_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "goto_tag_definition2");
2584 widgets
.popup_goto_items
[1] = ui_lookup_widget(main_widgets
.editor_menu
, "context_action1");
2585 widgets
.popup_goto_items
[2] = ui_lookup_widget(main_widgets
.editor_menu
, "find_usage2");
2586 widgets
.popup_goto_items
[3] = ui_lookup_widget(main_widgets
.editor_menu
, "find_document_usage2");
2588 widgets
.popup_copy_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "cut1");
2589 widgets
.popup_copy_items
[1] = ui_lookup_widget(main_widgets
.editor_menu
, "copy1");
2590 widgets
.popup_copy_items
[2] = ui_lookup_widget(main_widgets
.editor_menu
, "delete1");
2591 widgets
.menu_copy_items
[0] = ui_lookup_widget(main_widgets
.window
, "menu_cut1");
2592 widgets
.menu_copy_items
[1] = ui_lookup_widget(main_widgets
.window
, "menu_copy1");
2593 widgets
.menu_copy_items
[2] = ui_lookup_widget(main_widgets
.window
, "menu_delete1");
2594 widgets
.menu_insert_include_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "insert_include1");
2595 widgets
.menu_insert_include_items
[1] = ui_lookup_widget(main_widgets
.window
, "insert_include2");
2596 widgets
.save_buttons
[0] = ui_lookup_widget(main_widgets
.window
, "menu_save1");
2597 widgets
.save_buttons
[2] = ui_lookup_widget(main_widgets
.window
, "menu_save_all1");
2598 widgets
.redo_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "redo1");
2599 widgets
.redo_items
[1] = ui_lookup_widget(main_widgets
.window
, "menu_redo2");
2600 widgets
.undo_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "undo1");
2601 widgets
.undo_items
[1] = ui_lookup_widget(main_widgets
.window
, "menu_undo2");
2603 /* reparent context submenus as needed */
2605 GeanySharedMenu arr
[] = {
2606 {"commands2_menu", "commands2", "commands1"},
2607 {"menu_format1_menu", "menu_format1", "menu_format2"},
2608 {"more1_menu", "more1", "search2"},
2611 static GeanySharedMenu items
[G_N_ELEMENTS(arr
)];
2613 memcpy(items
, arr
, sizeof(arr
));
2614 g_signal_connect(main_widgets
.editor_menu
, "show", G_CALLBACK(on_editor_menu_show
), items
);
2615 g_signal_connect(main_widgets
.editor_menu
, "hide", G_CALLBACK(on_editor_menu_hide
), items
);
2618 ui_init_toolbar_widgets();
2619 init_document_widgets();
2621 create_config_files_menu();
2622 #if GTK_CHECK_VERSION(3, 0, 0)
2623 add_css_config_file_item();
2628 void ui_finalize_builder(void)
2630 if (GTK_IS_BUILDER(builder
))
2631 g_object_unref(builder
);
2633 /* cleanup refs lingering even after GtkBuilder is destroyed */
2634 if (GTK_IS_WIDGET(edit_menu1
))
2635 gtk_widget_destroy(edit_menu1
);
2636 if (GTK_IS_WIDGET(prefs_dialog
))
2637 gtk_widget_destroy(prefs_dialog
);
2638 if (GTK_IS_WIDGET(project_dialog
))
2639 gtk_widget_destroy(project_dialog
);
2640 if (GTK_IS_WIDGET(toolbar_popup_menu1
))
2641 gtk_widget_destroy(toolbar_popup_menu1
);
2642 if (GTK_IS_WIDGET(window1
))
2643 gtk_widget_destroy(window1
);
2647 static void auto_separator_update(GeanyAutoSeparator
*autosep
)
2649 g_return_if_fail(autosep
->item_count
>= 0);
2651 if (autosep
->widget
)
2653 if (autosep
->item_count
> 0)
2654 ui_widget_show_hide(autosep
->widget
, autosep
->show_count
> 0);
2656 gtk_widget_destroy(autosep
->widget
);
2661 static void on_auto_separator_item_show_hide(GtkWidget
*widget
, gpointer user_data
)
2663 GeanyAutoSeparator
*autosep
= user_data
;
2665 if (gtk_widget_get_visible(widget
))
2666 autosep
->show_count
++;
2668 autosep
->show_count
--;
2669 auto_separator_update(autosep
);
2673 static void on_auto_separator_item_destroy(GtkWidget
*widget
, gpointer user_data
)
2675 GeanyAutoSeparator
*autosep
= user_data
;
2677 autosep
->item_count
--;
2678 autosep
->item_count
= MAX(autosep
->item_count
, 0);
2679 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2680 * so assume widget was visible */
2681 autosep
->show_count
--;
2682 autosep
->show_count
= MAX(autosep
->item_count
, 0);
2683 auto_separator_update(autosep
);
2687 /* Show the separator widget if @a item or another is visible. */
2688 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2689 * property, and using reference counting to keep the widget alive whilst its visible group
2691 void ui_auto_separator_add_ref(GeanyAutoSeparator
*autosep
, GtkWidget
*item
)
2693 /* set widget ptr NULL when widget destroyed */
2694 if (autosep
->item_count
== 0)
2695 g_signal_connect(autosep
->widget
, "destroy",
2696 G_CALLBACK(gtk_widget_destroyed
), &autosep
->widget
);
2698 if (gtk_widget_get_visible(item
))
2699 autosep
->show_count
++;
2701 autosep
->item_count
++;
2702 auto_separator_update(autosep
);
2704 g_signal_connect(item
, "show", G_CALLBACK(on_auto_separator_item_show_hide
), autosep
);
2705 g_signal_connect(item
, "hide", G_CALLBACK(on_auto_separator_item_show_hide
), autosep
);
2706 g_signal_connect(item
, "destroy", G_CALLBACK(on_auto_separator_item_destroy
), autosep
);
2711 * Sets @a text as the contents of the tooltip for @a widget.
2713 * @param widget The widget the tooltip should be set for.
2714 * @param text The text for the tooltip.
2717 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2720 void ui_widget_set_tooltip_text(GtkWidget
*widget
, const gchar
*text
)
2722 gtk_widget_set_tooltip_text(widget
, text
);
2726 /** Returns a widget from a name in a component, usually created by Glade.
2727 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2728 * or alternatively any widget in the component, and the name of the widget
2729 * you want returned.
2730 * @param widget Widget with the @a widget_name property set.
2731 * @param widget_name Name to lookup.
2733 * @return @transfer{none} The widget found.
2734 * @see ui_hookup_widget().
2739 GtkWidget
*ui_lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
2741 GtkWidget
*parent
, *found_widget
;
2743 g_return_val_if_fail(widget
!= NULL
, NULL
);
2744 g_return_val_if_fail(widget_name
!= NULL
, NULL
);
2748 if (GTK_IS_MENU(widget
))
2749 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
2751 parent
= gtk_widget_get_parent(widget
);
2753 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
2759 found_widget
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), widget_name
);
2760 if (G_UNLIKELY(found_widget
== NULL
))
2761 g_warning("Widget not found: %s", widget_name
);
2762 return found_widget
;
2766 /* wraps gtk_builder_get_object()
2767 * unlike ui_lookup_widget(), it does only support getting object created from the main
2768 * UI file, but it can fetch any object, not only widgets */
2769 gpointer
ui_builder_get_object (const gchar
*name
)
2771 return gtk_builder_get_object (builder
, name
);
2776 static guint progress_bar_timer_id
= 0;
2779 static GtkWidget
*progress_bar_create(void)
2781 GtkWidget
*bar
= gtk_progress_bar_new();
2783 /* Set the progressbar's height to 1 to fit it in the statusbar */
2784 gtk_widget_set_size_request(bar
, -1, 1);
2785 gtk_box_pack_start (GTK_BOX(ui_widgets
.statusbar
), bar
, FALSE
, FALSE
, 3);
2791 static gboolean
progress_bar_pulse(gpointer data
)
2793 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets
.progressbar
));
2800 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2801 * (if the statusbar is visible). This is a convenience function which adds a timer to
2802 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2803 * You can use this function when you have time consuming asynchronous operation and want to
2804 * display some activity in the GUI and when you don't know about detailed progress steps.
2805 * The progressbar widget is hidden by default when it is not active. This function and
2806 * ui_progress_bar_stop() will show and hide it automatically for you.
2808 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2809 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2810 * In this case, you need to show and hide the widget yourself. You can find some example code
2811 * in @c src/printing.c.
2813 * @param text @nullable The text to be shown as the progress bar label or @c NULL to leave it empty.
2818 void ui_progress_bar_start(const gchar
*text
)
2820 g_return_if_fail(progress_bar_timer_id
== 0);
2822 if (! interface_prefs
.statusbar_visible
)
2825 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets
.progressbar
), text
);
2827 progress_bar_timer_id
= g_timeout_add(200, progress_bar_pulse
, NULL
);
2829 gtk_widget_show(GTK_WIDGET(main_widgets
.progressbar
));
2833 /** Stops a running progress bar and hides the widget again.
2838 void ui_progress_bar_stop(void)
2840 gtk_widget_hide(GTK_WIDGET(main_widgets
.progressbar
));
2842 if (progress_bar_timer_id
!= 0)
2844 g_source_remove(progress_bar_timer_id
);
2845 progress_bar_timer_id
= 0;
2850 static gint
compare_menu_item_labels(gconstpointer a
, gconstpointer b
)
2852 GtkMenuItem
*item_a
= GTK_MENU_ITEM(a
);
2853 GtkMenuItem
*item_b
= GTK_MENU_ITEM(b
);
2857 /* put entries with submenus at the end of the menu */
2858 if (gtk_menu_item_get_submenu(item_a
) && !gtk_menu_item_get_submenu(item_b
))
2860 else if (!gtk_menu_item_get_submenu(item_a
) && gtk_menu_item_get_submenu(item_b
))
2863 sa
= ui_menu_item_get_text(item_a
);
2864 sb
= ui_menu_item_get_text(item_b
);
2865 result
= utils_str_casecmp(sa
, sb
);
2872 /* Currently @a menu should contain only GtkMenuItems with labels. */
2873 static void ui_menu_sort_by_label(GtkMenu
*menu
)
2875 GList
*list
= gtk_container_get_children(GTK_CONTAINER(menu
));
2879 list
= g_list_sort(list
, compare_menu_item_labels
);
2881 foreach_list(node
, list
)
2883 menu_reorder_child(menu
, node
->data
, pos
);
2890 void ui_label_set_markup(GtkLabel
*label
, const gchar
*format
, ...)
2895 va_start(a
, format
);
2896 text
= g_markup_vprintf_escaped(format
, a
);
2899 gtk_label_set_text(label
, text
);
2900 gtk_label_set_use_markup(label
, TRUE
);
2905 GtkWidget
*ui_label_new_bold(const gchar
*text
)
2909 label
= gtk_label_new(NULL
);
2910 ui_label_set_markup(GTK_LABEL(label
), "<b>%s</b>", text
);
2916 * Adds a list of document items to @a menu.
2918 * @param active @nullable Which document to highlight, or @c NULL.
2919 * @param callback is used for each menu item's @c "activate" signal and will be
2920 * passed the corresponding document pointer as @c user_data.
2921 * @warning You should check @c doc->is_valid in the callback.
2925 void ui_menu_add_document_items(GtkMenu
*menu
, GeanyDocument
*active
, GCallback callback
)
2927 ui_menu_add_document_items_sorted(menu
, active
, callback
, NULL
);
2932 * Adds a list of document items to @a menu.
2934 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2935 * the order of the document tabs is used.
2937 * See document_compare_by_display_name() for an example sort function.
2940 * @param active @nullable Which document to highlight, or @c NULL.
2941 * @param callback is used for each menu item's @c "activate" signal and will be passed
2942 * the corresponding document pointer as @c user_data.
2943 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2944 * @warning You should check @c doc->is_valid in the callback.
2948 void ui_menu_add_document_items_sorted(GtkMenu
*menu
, GeanyDocument
*active
,
2949 GCallback callback
, GCompareFunc compare_func
)
2951 GtkWidget
*menu_item
, *menu_item_label
, *image
;
2955 GPtrArray
*sorted_documents
;
2957 len
= (guint
) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
));
2959 sorted_documents
= g_ptr_array_sized_new(len
);
2960 /* copy the documents_array into the new one */
2963 g_ptr_array_add(sorted_documents
, documents
[i
]);
2965 if (compare_func
== NULL
)
2966 compare_func
= document_compare_by_tab_order
;
2968 /* and now sort it */
2969 g_ptr_array_sort(sorted_documents
, compare_func
);
2971 for (i
= 0; i
< sorted_documents
->len
; i
++)
2973 doc
= g_ptr_array_index(sorted_documents
, i
);
2975 base_name
= g_path_get_basename(DOC_FILENAME(doc
));
2976 menu_item
= gtk_image_menu_item_new_with_label(base_name
);
2977 image
= gtk_image_new_from_gicon(doc
->file_type
->icon
, GTK_ICON_SIZE_MENU
);
2978 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item
), image
);
2979 gtk_widget_set_tooltip_text(menu_item
, DOC_FILENAME(doc
));
2981 gtk_widget_show(menu_item
);
2982 gtk_container_add(GTK_CONTAINER(menu
), menu_item
);
2983 g_signal_connect(menu_item
, "activate", callback
, doc
);
2985 menu_item_label
= gtk_bin_get_child(GTK_BIN(menu_item
));
2986 gtk_widget_set_name(menu_item_label
, document_get_status_widget_class(doc
));
2989 ui_label_set_markup(GTK_LABEL(menu_item_label
), "<b>%s</b>", base_name
);
2993 g_ptr_array_free(sorted_documents
, TRUE
);
2997 /** Checks whether the passed @a keyval is the Enter or Return key.
2998 * There are three different Enter/Return key values
2999 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
3000 * This is just a convenience function.
3001 * @param keyval A keyval.
3002 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
3005 gboolean
ui_is_keyval_enter_or_return(guint keyval
)
3007 return (keyval
== GDK_Return
|| keyval
== GDK_ISO_Enter
|| keyval
== GDK_KP_Enter
);
3011 /** Reads an integer from the GTK default settings registry
3012 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
3013 * @param property_name The property to read.
3014 * @param default_value The default value in case the value could not be read.
3015 * @return The value for the property if it exists, otherwise the @a default_value.
3018 gint
ui_get_gtk_settings_integer(const gchar
*property_name
, gint default_value
)
3020 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
3021 gtk_settings_get_default())), property_name
))
3024 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name
, &value
, NULL
);
3028 return default_value
;
3032 void ui_editable_insert_text_callback(GtkEditable
*editable
, gchar
*new_text
,
3033 gint new_text_len
, gint
*position
, gpointer data
)
3035 gboolean first
= position
!= NULL
&& *position
== 0;
3038 if (new_text_len
== -1)
3039 new_text_len
= (gint
) strlen(new_text
);
3041 for (i
= 0; i
< new_text_len
; i
++, new_text
++)
3043 if ((!first
|| !strchr("+-", *new_text
)) && !isdigit(*new_text
))
3045 g_signal_stop_emission_by_name(editable
, "insert-text");
3053 /* gets the icon that applies to a particular MIME type */
3054 GIcon
*ui_get_mime_icon(const gchar
*mime_type
)
3059 ctype
= g_content_type_from_mime_type(mime_type
);
3062 GdkScreen
*screen
= gdk_screen_get_default();
3064 icon
= g_content_type_get_icon(ctype
);
3067 GtkIconInfo
*icon_info
;
3069 icon_info
= gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_for_screen(screen
), icon
, 16, 0);
3072 g_object_unref(icon
);
3076 gtk_icon_info_free(icon_info
);
3082 /* fallback if icon lookup failed, like it might happen on Windows (?) */
3085 const gchar
*icon_name
= "text-x-generic";
3087 if (strstr(mime_type
, "directory"))
3088 icon_name
= "folder";
3090 icon
= g_themed_icon_new(icon_name
);
3096 void ui_focus_current_document(void)
3098 GeanyDocument
*doc
= document_get_current();
3101 document_grab_focus(doc
);
3105 /** Finds the label text associated with stock_id
3106 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
3107 * @return The label text for stock
3108 * @since Geany 1.22 */
3110 const gchar
*ui_lookup_stock_label(const gchar
*stock_id
)
3114 if (gtk_stock_lookup(stock_id
, &item
))
3117 g_warning("No stock id '%s'!", stock_id
);
3122 /* finds the next iter at any level
3123 * @param iter in/out, the current iter, will be changed to the next one
3124 * @param down whether to try the child iter
3125 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
3126 gboolean
ui_tree_model_iter_any_next(GtkTreeModel
*model
, GtkTreeIter
*iter
, gboolean down
)
3129 GtkTreeIter copy
= *iter
;
3131 /* go down if the item has children */
3132 if (down
&& gtk_tree_model_iter_children(model
, &guess
, iter
))
3134 /* or to the next item at the same level */
3135 else if (gtk_tree_model_iter_next(model
, ©
))
3137 /* or to the next item at a parent level */
3138 else if (gtk_tree_model_iter_parent(model
, &guess
, iter
))
3143 if (gtk_tree_model_iter_next(model
, ©
))
3148 else if (gtk_tree_model_iter_parent(model
, ©
, &guess
))
3161 GtkWidget
*ui_create_encodings_combo_box(gboolean has_detect
, gint default_enc
)
3163 GtkCellRenderer
*renderer
;
3165 GtkWidget
*combo
= gtk_combo_box_new();
3166 GtkTreeStore
*store
= encodings_encoding_store_new(has_detect
);
3168 if (default_enc
< 0 || default_enc
>= GEANY_ENCODINGS_MAX
)
3169 default_enc
= has_detect
? GEANY_ENCODINGS_MAX
: GEANY_ENCODING_NONE
;
3171 gtk_combo_box_set_model(GTK_COMBO_BOX(combo
), GTK_TREE_MODEL(store
));
3172 if (encodings_encoding_store_get_iter(store
, &iter
, default_enc
))
3173 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo
), &iter
);
3174 renderer
= gtk_cell_renderer_text_new();
3175 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo
), renderer
, TRUE
);
3176 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo
), renderer
,
3177 encodings_encoding_store_cell_data_func
, NULL
, NULL
);
3183 gint
ui_encodings_combo_box_get_active_encoding(GtkComboBox
*combo
)
3186 gint enc
= GEANY_ENCODING_NONE
;
3188 /* there should always be an active iter anyway, but we check just in case */
3189 if (gtk_combo_box_get_active_iter(combo
, &iter
))
3191 GtkTreeModel
*model
= gtk_combo_box_get_model(combo
);
3192 enc
= encodings_encoding_store_get_encoding(GTK_TREE_STORE(model
), &iter
);
3199 gboolean
ui_encodings_combo_box_set_active_encoding(GtkComboBox
*combo
, gint enc
)
3202 GtkTreeModel
*model
= gtk_combo_box_get_model(combo
);
3204 if (encodings_encoding_store_get_iter(GTK_TREE_STORE(model
), &iter
, enc
))
3206 gtk_combo_box_set_active_iter(combo
, &iter
);