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 "encodings.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);
125 /* simple wrapper for gtk_widget_set_sensitive() to allow widget being NULL */
126 void ui_widget_set_sensitive(GtkWidget
*widget
, gboolean set
)
129 gtk_widget_set_sensitive(widget
, set
);
133 /* allow_override is TRUE if text can be ignored when another message has been set
134 * that didn't use allow_override and has not timed out. */
135 static void set_statusbar(const gchar
*text
, gboolean allow_override
)
138 static glong last_time
= 0;
140 const gint GEANY_STATUS_TIMEOUT
= 1;
142 if (! interface_prefs
.statusbar_visible
)
143 return; /* just do nothing if statusbar is not visible */
146 id
= gtk_statusbar_get_context_id(GTK_STATUSBAR(ui_widgets
.statusbar
), "geany-main");
148 g_get_current_time(&timeval
);
150 if (! allow_override
)
152 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets
.statusbar
), id
);
153 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets
.statusbar
), id
, text
);
154 last_time
= timeval
.tv_sec
;
157 if (timeval
.tv_sec
> last_time
+ GEANY_STATUS_TIMEOUT
)
159 gtk_statusbar_pop(GTK_STATUSBAR(ui_widgets
.statusbar
), id
);
160 gtk_statusbar_push(GTK_STATUSBAR(ui_widgets
.statusbar
), id
, text
);
165 /** Displays text on the statusbar.
166 * @param log Whether the message should be recorded in the Status window.
167 * @param format A @c printf -style string. */
169 void ui_set_statusbar(gboolean log
, const gchar
*format
, ...)
174 va_start(args
, format
);
175 string
= g_strdup_vprintf(format
, args
);
178 if (! prefs
.suppress_status_messages
)
179 set_statusbar(string
, FALSE
);
181 if (log
|| prefs
.suppress_status_messages
)
182 msgwin_status_add("%s", string
);
188 /* note: some comments below are for translators */
189 static gchar
*create_statusbar_statistics(GeanyDocument
*doc
,
190 guint line
, guint vcol
, guint pos
)
192 const gchar
*cur_tag
;
194 const gchar
*expos
; /* % expansion position */
195 const gchar sp
[] = " ";
197 ScintillaObject
*sci
= doc
->editor
->sci
;
199 if (!EMPTY(ui_prefs
.statusbar_template
))
200 fmt
= ui_prefs
.statusbar_template
;
202 fmt
= _(DEFAULT_STATUSBAR_TEMPLATE
);
204 stats_str
= g_string_sized_new(120);
206 while ((expos
= strchr(fmt
, '%')) != NULL
)
208 /* append leading text before % char */
209 g_string_append_len(stats_str
, fmt
, expos
- fmt
);
214 g_string_append_printf(stats_str
, "%d", line
+ 1);
217 g_string_append_printf(stats_str
, "%d",
218 sci_get_line_count(doc
->editor
->sci
));
221 g_string_append_printf(stats_str
, "%d", vcol
);
224 g_string_append_printf(stats_str
, "%d", vcol
+ 1);
227 g_string_append_printf(stats_str
, "%u", pos
);
231 gint len
= sci_get_selected_text_length(sci
) - 1;
232 /* check if whole lines are selected */
233 if (!len
|| sci_get_col_from_position(sci
,
234 sci_get_selection_start(sci
)) != 0 ||
235 sci_get_col_from_position(sci
,
236 sci_get_selection_end(sci
)) != 0)
237 g_string_append_printf(stats_str
, "%d", len
);
239 g_string_append_printf(stats_str
, _("%dL"),
240 sci_get_lines_selected(doc
->editor
->sci
) - 1);
244 g_string_append_printf(stats_str
, "%d",
245 sci_get_selected_text_length(doc
->editor
->sci
) - 1);
249 g_string_append(stats_str
, (doc
->readonly
) ? _("RO ") :
250 /* OVR = overwrite/overtype, INS = insert */
251 (sci_get_overtype(doc
->editor
->sci
) ? _("OVR") : _("INS")));
256 g_string_append(stats_str
, _("RO ")); /* RO = read-only */
257 g_string_append(stats_str
, sp
+ 1);
262 switch (editor_get_indent_prefs(doc
->editor
)->type
)
264 case GEANY_INDENT_TYPE_TABS
:
265 g_string_append(stats_str
, _("TAB"));
267 case GEANY_INDENT_TYPE_SPACES
: /* SP = space */
268 g_string_append(stats_str
, _("SP"));
270 case GEANY_INDENT_TYPE_BOTH
: /* T/S = tabs and spaces */
271 g_string_append(stats_str
, _("T/S"));
279 g_string_append(stats_str
, _("MOD")); /* MOD = modified */
280 g_string_append(stats_str
, sp
);
284 g_string_append(stats_str
, utils_get_eol_short_name(sci_get_eol_mode(doc
->editor
->sci
)));
287 g_string_append(stats_str
,
288 doc
->encoding
? doc
->encoding
: _("unknown"));
289 if (encodings_is_unicode_charset(doc
->encoding
) && (doc
->has_bom
))
291 g_string_append_c(stats_str
, ' ');
292 g_string_append(stats_str
, _("(with BOM)")); /* BOM = byte order mark */
296 g_string_append(stats_str
, filetypes_get_display_name(doc
->file_type
));
299 symbols_get_current_scope(doc
, &cur_tag
);
300 g_string_append(stats_str
, cur_tag
);
303 g_string_append_c(stats_str
, ' ');
304 g_string_append_printf(stats_str
, "%d",
305 sci_get_style_at(doc
->editor
->sci
, pos
));
308 g_string_append_len(stats_str
, expos
, 1);
311 /* skip past %c chars */
317 /* add any remaining text */
318 g_string_append(stats_str
, fmt
);
320 return g_string_free(stats_str
, FALSE
);
324 /* updates the status bar document statistics */
325 void ui_update_statusbar(GeanyDocument
*doc
, gint pos
)
327 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
329 if (! interface_prefs
.statusbar_visible
)
330 return; /* just do nothing if statusbar is not visible */
333 doc
= document_get_current();
341 pos
= sci_get_current_position(doc
->editor
->sci
);
342 line
= sci_get_line_from_position(doc
->editor
->sci
, pos
);
344 /* Add temporary fix for sci infinite loop in Document::GetColumn(int)
345 * when current pos is beyond document end (can occur when removing
346 * blocks of selected lines especially esp. brace sections near end of file). */
347 if (pos
<= sci_get_length(doc
->editor
->sci
))
348 vcol
= sci_get_col_from_position(doc
->editor
->sci
, pos
);
351 vcol
+= sci_get_cursor_virtual_space(doc
->editor
->sci
);
353 stats_str
= create_statusbar_statistics(doc
, line
, vcol
, pos
);
355 /* can be overridden by status messages */
356 set_statusbar(stats_str
, TRUE
);
359 else /* no documents */
361 set_statusbar("", TRUE
); /* can be overridden by status messages */
366 /* This sets the window title according to the current filename. */
367 void ui_set_window_title(GeanyDocument
*doc
)
370 GeanyProject
*project
= app
->project
;
372 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
375 doc
= document_get_current();
377 str
= g_string_new(NULL
);
381 g_string_append(str
, doc
->changed
? "*" : "");
383 if (doc
->file_name
== NULL
)
384 g_string_append(str
, DOC_FILENAME(doc
));
387 gchar
*short_name
= document_get_basename_for_display(doc
, 30);
388 gchar
*dirname
= g_path_get_dirname(DOC_FILENAME(doc
));
390 g_string_append(str
, short_name
);
391 g_string_append(str
, " - ");
392 g_string_append(str
, dirname
? dirname
: "");
396 g_string_append(str
, " - ");
400 g_string_append_c(str
, '[');
401 g_string_append(str
, project
->name
);
402 g_string_append(str
, "] - ");
404 g_string_append(str
, "Geany");
405 if (cl_options
.new_instance
)
407 g_string_append(str
, _(" (new instance)"));
409 gtk_window_set_title(GTK_WINDOW(main_widgets
.window
), str
->str
);
410 g_string_free(str
, TRUE
);
414 void ui_set_editor_font(const gchar
*font_name
)
418 g_return_if_fail(font_name
!= NULL
);
420 /* do nothing if font has not changed */
421 if (interface_prefs
.editor_font
!= NULL
)
422 if (strcmp(font_name
, interface_prefs
.editor_font
) == 0)
425 g_free(interface_prefs
.editor_font
);
426 interface_prefs
.editor_font
= g_strdup(font_name
);
428 /* We copy the current style, and update the font in all open tabs. */
429 for (i
= 0; i
< documents_array
->len
; i
++)
431 if (documents
[i
]->editor
)
433 editor_set_font(documents
[i
]->editor
, interface_prefs
.editor_font
);
437 ui_set_statusbar(TRUE
, _("Font updated (%s)."), interface_prefs
.editor_font
);
441 void ui_set_fullscreen(void)
443 if (ui_prefs
.fullscreen
)
445 gtk_window_fullscreen(GTK_WINDOW(main_widgets
.window
));
449 gtk_window_unfullscreen(GTK_WINDOW(main_widgets
.window
));
454 void ui_update_popup_reundo_items(GeanyDocument
*doc
)
456 gboolean enable_undo
;
457 gboolean enable_redo
;
460 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
469 enable_undo
= document_can_undo(doc
);
470 enable_redo
= document_can_redo(doc
);
473 /* index 0 is the popup menu, 1 is the menubar, 2 is the toolbar */
474 len
= G_N_ELEMENTS(widgets
.undo_items
);
475 for (i
= 0; i
< len
; i
++)
477 ui_widget_set_sensitive(widgets
.undo_items
[i
], enable_undo
);
479 len
= G_N_ELEMENTS(widgets
.redo_items
);
480 for (i
= 0; i
< len
; i
++)
482 ui_widget_set_sensitive(widgets
.redo_items
[i
], enable_redo
);
487 void ui_update_popup_copy_items(GeanyDocument
*doc
)
492 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
497 enable
= sci_has_selection(doc
->editor
->sci
);
499 len
= G_N_ELEMENTS(widgets
.popup_copy_items
);
500 for (i
= 0; i
< len
; i
++)
501 ui_widget_set_sensitive(widgets
.popup_copy_items
[i
], enable
);
505 void ui_update_popup_goto_items(gboolean enable
)
508 len
= G_N_ELEMENTS(widgets
.popup_goto_items
);
509 for (i
= 0; i
< len
; i
++)
510 ui_widget_set_sensitive(widgets
.popup_goto_items
[i
], enable
);
514 void ui_update_menu_copy_items(GeanyDocument
*doc
)
516 gboolean enable
= FALSE
;
518 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
520 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
522 if (IS_SCINTILLA(focusw
))
523 enable
= (doc
== NULL
) ? FALSE
: sci_has_selection(doc
->editor
->sci
);
525 if (GTK_IS_EDITABLE(focusw
))
526 enable
= gtk_editable_get_selection_bounds(GTK_EDITABLE(focusw
), NULL
, NULL
);
528 if (GTK_IS_TEXT_VIEW(focusw
))
530 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(
531 GTK_TEXT_VIEW(focusw
));
532 enable
= gtk_text_buffer_get_selection_bounds(buffer
, NULL
, NULL
);
535 len
= G_N_ELEMENTS(widgets
.menu_copy_items
);
536 for (i
= 0; i
< len
; i
++)
537 ui_widget_set_sensitive(widgets
.menu_copy_items
[i
], enable
);
541 void ui_update_insert_include_item(GeanyDocument
*doc
, gint item
)
543 gboolean enable
= FALSE
;
545 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
547 if (doc
== NULL
|| doc
->file_type
== NULL
)
549 else if (doc
->file_type
->id
== GEANY_FILETYPES_C
|| doc
->file_type
->id
== GEANY_FILETYPES_CPP
)
552 ui_widget_set_sensitive(widgets
.menu_insert_include_items
[item
], enable
);
556 void ui_update_fold_items(void)
558 ui_widget_show_hide(ui_lookup_widget(main_widgets
.window
, "menu_fold_all1"), editor_prefs
.folding
);
559 ui_widget_show_hide(ui_lookup_widget(main_widgets
.window
, "menu_unfold_all1"), editor_prefs
.folding
);
560 ui_widget_show_hide(ui_lookup_widget(main_widgets
.window
, "separator22"), editor_prefs
.folding
);
564 /* @include include name or NULL for empty with cursor ready for typing it */
565 static void insert_include(GeanyDocument
*doc
, gint pos
, const gchar
*include
)
570 g_return_if_fail(doc
!= NULL
);
571 g_return_if_fail(pos
== -1 || pos
>= 0);
574 pos
= sci_get_current_position(doc
->editor
->sci
);
578 text
= g_strdup("#include \"\"\n");
579 pos_after
= pos
+ 10;
583 text
= g_strconcat("#include <", include
, ">\n", NULL
);
586 sci_start_undo_action(doc
->editor
->sci
);
587 sci_insert_text(doc
->editor
->sci
, pos
, text
);
588 sci_end_undo_action(doc
->editor
->sci
);
591 sci_goto_pos(doc
->editor
->sci
, pos_after
, FALSE
);
595 static void on_popup_insert_include_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
597 insert_include(document_get_current(), editor_info
.click_pos
, user_data
);
601 static void on_menu_insert_include_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
603 insert_include(document_get_current(), -1, user_data
);
607 static void insert_include_items(GtkMenu
*me
, GtkMenu
*mp
, gchar
**includes
, gchar
*label
)
611 GtkWidget
*tmp_popup
;
612 GtkWidget
*edit_menu
, *edit_menu_item
;
613 GtkWidget
*popup_menu
, *popup_menu_item
;
615 edit_menu
= gtk_menu_new();
616 popup_menu
= gtk_menu_new();
617 edit_menu_item
= gtk_menu_item_new_with_label(label
);
618 popup_menu_item
= gtk_menu_item_new_with_label(label
);
619 gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_menu_item
), edit_menu
);
620 gtk_menu_item_set_submenu(GTK_MENU_ITEM(popup_menu_item
), popup_menu
);
622 while (includes
[i
] != NULL
)
624 tmp_menu
= gtk_menu_item_new_with_label(includes
[i
]);
625 tmp_popup
= gtk_menu_item_new_with_label(includes
[i
]);
626 gtk_container_add(GTK_CONTAINER(edit_menu
), tmp_menu
);
627 gtk_container_add(GTK_CONTAINER(popup_menu
), tmp_popup
);
628 g_signal_connect(tmp_menu
, "activate",
629 G_CALLBACK(on_menu_insert_include_activate
), (gpointer
) includes
[i
]);
630 g_signal_connect(tmp_popup
, "activate",
631 G_CALLBACK(on_popup_insert_include_activate
), (gpointer
) includes
[i
]);
634 gtk_widget_show_all(edit_menu_item
);
635 gtk_widget_show_all(popup_menu_item
);
636 gtk_container_add(GTK_CONTAINER(me
), edit_menu_item
);
637 gtk_container_add(GTK_CONTAINER(mp
), popup_menu_item
);
641 void ui_create_insert_menu_items(void)
643 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "insert_include2_menu"));
644 GtkMenu
*menu_popup
= GTK_MENU(ui_lookup_widget(main_widgets
.editor_menu
, "insert_include1_menu"));
646 const gchar
*c_includes_stdlib
[] = {
647 "assert.h", "ctype.h", "errno.h", "float.h", "limits.h", "locale.h", "math.h", "setjmp.h",
648 "signal.h", "stdarg.h", "stddef.h", "stdio.h", "stdlib.h", "string.h", "time.h", NULL
650 const gchar
*c_includes_c99
[] = {
651 "complex.h", "fenv.h", "inttypes.h", "iso646.h", "stdbool.h", "stdint.h",
652 "tgmath.h", "wchar.h", "wctype.h", NULL
654 const gchar
*c_includes_cpp
[] = {
655 "cstdio", "cstring", "cctype", "cmath", "ctime", "cstdlib", "cstdarg", NULL
657 const gchar
*c_includes_cppstdlib
[] = {
658 "iostream", "fstream", "iomanip", "sstream", "exception", "stdexcept",
659 "memory", "locale", NULL
661 const gchar
*c_includes_stl
[] = {
662 "bitset", "deque", "list", "map", "set", "queue", "stack", "vector", "algorithm",
663 "iterator", "functional", "string", "complex", "valarray", NULL
666 blank
= gtk_menu_item_new_with_label("#include \"...\"");
667 gtk_container_add(GTK_CONTAINER(menu_edit
), blank
);
668 gtk_widget_show(blank
);
669 g_signal_connect(blank
, "activate", G_CALLBACK(on_menu_insert_include_activate
), NULL
);
670 blank
= gtk_separator_menu_item_new ();
671 gtk_container_add(GTK_CONTAINER(menu_edit
), blank
);
672 gtk_widget_show(blank
);
674 blank
= gtk_menu_item_new_with_label("#include \"...\"");
675 gtk_container_add(GTK_CONTAINER(menu_popup
), blank
);
676 gtk_widget_show(blank
);
677 g_signal_connect(blank
, "activate", G_CALLBACK(on_popup_insert_include_activate
), NULL
);
678 blank
= gtk_separator_menu_item_new();
679 gtk_container_add(GTK_CONTAINER(menu_popup
), blank
);
680 gtk_widget_show(blank
);
682 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_stdlib
, _("C Standard Library"));
683 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_c99
, _("ISO C99"));
684 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_cpp
, _("C++ (C Standard Library)"));
685 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_cppstdlib
, _("C++ Standard Library"));
686 insert_include_items(menu_edit
, menu_popup
, (gchar
**) c_includes_stl
, _("C++ STL"));
690 static void insert_date(GeanyDocument
*doc
, gint pos
, const gchar
*date_style
)
692 const gchar
*format
= NULL
;
695 g_return_if_fail(doc
!= NULL
);
696 g_return_if_fail(pos
== -1 || pos
>= 0);
699 pos
= sci_get_current_position(doc
->editor
->sci
);
701 /* set default value */
702 if (utils_str_equal("", ui_prefs
.custom_date_format
))
704 g_free(ui_prefs
.custom_date_format
);
705 ui_prefs
.custom_date_format
= g_strdup("%d.%m.%Y");
708 if (utils_str_equal(_("dd.mm.yyyy"), date_style
))
710 else if (utils_str_equal(_("mm.dd.yyyy"), date_style
))
712 else if (utils_str_equal(_("yyyy/mm/dd"), date_style
))
714 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), date_style
))
715 format
= "%d.%m.%Y %H:%M:%S";
716 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), date_style
))
717 format
= "%m.%d.%Y %H:%M:%S";
718 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), date_style
))
719 format
= "%Y/%m/%d %H:%M:%S";
720 else if (utils_str_equal(_("_Use Custom Date Format"), date_style
))
721 format
= ui_prefs
.custom_date_format
;
724 gchar
*str
= dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets
.window
),
725 _("Enter here a custom date and time format. "
726 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
727 ui_prefs
.custom_date_format
);
729 SETPTR(ui_prefs
.custom_date_format
, str
);
733 time_str
= utils_get_date_time(format
, NULL
);
734 if (time_str
!= NULL
)
736 sci_start_undo_action(doc
->editor
->sci
);
737 sci_insert_text(doc
->editor
->sci
, pos
, time_str
);
738 sci_goto_pos(doc
->editor
->sci
, pos
+ strlen(time_str
), FALSE
);
739 sci_end_undo_action(doc
->editor
->sci
);
745 ui_set_statusbar(TRUE
,
746 _("Date format string could not be converted (possibly too long)."));
751 static void on_popup_insert_date_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
753 insert_date(document_get_current(), editor_info
.click_pos
, user_data
);
757 static void on_menu_insert_date_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
759 insert_date(document_get_current(), -1, user_data
);
763 static void insert_date_items(GtkMenu
*me
, GtkMenu
*mp
, gchar
*label
)
767 item
= gtk_menu_item_new_with_mnemonic(label
);
768 gtk_container_add(GTK_CONTAINER(me
), item
);
769 gtk_widget_show(item
);
770 g_signal_connect(item
, "activate", G_CALLBACK(on_menu_insert_date_activate
), label
);
772 item
= gtk_menu_item_new_with_mnemonic(label
);
773 gtk_container_add(GTK_CONTAINER(mp
), item
);
774 gtk_widget_show(item
);
775 g_signal_connect(item
, "activate", G_CALLBACK(on_popup_insert_date_activate
), label
);
779 void ui_create_insert_date_menu_items(void)
781 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "insert_date1_menu"));
782 GtkMenu
*menu_popup
= GTK_MENU(ui_lookup_widget(main_widgets
.editor_menu
, "insert_date2_menu"));
786 insert_date_items(menu_edit
, menu_popup
, _("dd.mm.yyyy"));
787 insert_date_items(menu_edit
, menu_popup
, _("mm.dd.yyyy"));
788 insert_date_items(menu_edit
, menu_popup
, _("yyyy/mm/dd"));
790 item
= gtk_separator_menu_item_new();
791 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
792 gtk_widget_show(item
);
793 item
= gtk_separator_menu_item_new();
794 gtk_container_add(GTK_CONTAINER(menu_popup
), item
);
795 gtk_widget_show(item
);
797 insert_date_items(menu_edit
, menu_popup
, _("dd.mm.yyyy hh:mm:ss"));
798 insert_date_items(menu_edit
, menu_popup
, _("mm.dd.yyyy hh:mm:ss"));
799 insert_date_items(menu_edit
, menu_popup
, _("yyyy/mm/dd hh:mm:ss"));
801 item
= gtk_separator_menu_item_new();
802 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
803 gtk_widget_show(item
);
804 item
= gtk_separator_menu_item_new();
805 gtk_container_add(GTK_CONTAINER(menu_popup
), item
);
806 gtk_widget_show(item
);
808 str
= _("_Use Custom Date Format");
809 item
= gtk_menu_item_new_with_mnemonic(str
);
810 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
811 gtk_widget_show(item
);
812 g_signal_connect(item
, "activate", G_CALLBACK(on_menu_insert_date_activate
), str
);
813 ui_hookup_widget(main_widgets
.window
, item
, "insert_date_custom1");
815 item
= gtk_menu_item_new_with_mnemonic(str
);
816 gtk_container_add(GTK_CONTAINER(menu_popup
), item
);
817 gtk_widget_show(item
);
818 g_signal_connect(item
, "activate", G_CALLBACK(on_popup_insert_date_activate
), str
);
819 ui_hookup_widget(main_widgets
.editor_menu
, item
, "insert_date_custom2");
821 insert_date_items(menu_edit
, menu_popup
, _("_Set Custom Date Format"));
825 void ui_save_buttons_toggle(gboolean enable
)
828 gboolean dirty_tabs
= FALSE
;
830 if (ui_prefs
.allow_always_save
)
831 enable
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) > 0;
833 ui_widget_set_sensitive(widgets
.save_buttons
[0], enable
);
834 ui_widget_set_sensitive(widgets
.save_buttons
[1], enable
);
836 /* save all menu item and tool button */
837 for (i
= 0; i
< documents_array
->len
; i
++)
839 /* check whether there are files where changes were made and if there are some,
840 * we need the save all button / item */
841 if (documents
[i
]->is_valid
&& documents
[i
]->changed
)
848 ui_widget_set_sensitive(widgets
.save_buttons
[2], dirty_tabs
);
849 ui_widget_set_sensitive(widgets
.save_buttons
[3], dirty_tabs
);
853 #define add_doc_widget(widget_name) \
854 g_ptr_array_add(widgets.document_buttons, ui_lookup_widget(main_widgets.window, widget_name))
856 #define add_doc_toolitem(widget_name) \
857 g_ptr_array_add(widgets.document_buttons, toolbar_get_action_by_name(widget_name))
859 static void init_document_widgets(void)
861 widgets
.document_buttons
= g_ptr_array_new();
863 /* Cache the document-sensitive widgets so we don't have to keep looking them up
864 * when using ui_document_buttons_update(). */
865 add_doc_widget("menu_close1");
866 add_doc_widget("close_other_documents1");
867 add_doc_widget("menu_change_font1");
868 add_doc_widget("menu_close_all1");
869 add_doc_widget("menu_save1");
870 add_doc_widget("menu_save_all1");
871 add_doc_widget("menu_save_as1");
872 add_doc_widget("menu_count_words1");
873 add_doc_widget("menu_build1");
874 add_doc_widget("add_comments1");
875 add_doc_widget("menu_paste1");
876 add_doc_widget("menu_undo2");
877 add_doc_widget("preferences2");
878 add_doc_widget("menu_reload1");
879 add_doc_widget("menu_document1");
880 add_doc_widget("menu_choose_color1");
881 add_doc_widget("menu_color_schemes");
882 add_doc_widget("menu_markers_margin1");
883 add_doc_widget("menu_linenumber_margin1");
884 add_doc_widget("menu_show_white_space1");
885 add_doc_widget("menu_show_line_endings1");
886 add_doc_widget("menu_show_indentation_guides1");
887 add_doc_widget("menu_zoom_in1");
888 add_doc_widget("menu_zoom_out1");
889 add_doc_widget("normal_size1");
890 add_doc_widget("treeview6");
891 add_doc_widget("print1");
892 add_doc_widget("menu_reload_as1");
893 add_doc_widget("menu_select_all1");
894 add_doc_widget("insert_date1");
895 add_doc_widget("insert_alternative_white_space1");
896 add_doc_widget("menu_format1");
897 add_doc_widget("commands2");
898 add_doc_widget("menu_open_selected_file1");
899 add_doc_widget("page_setup1");
900 add_doc_widget("find1");
901 add_doc_widget("find_next1");
902 add_doc_widget("find_previous1");
903 add_doc_widget("go_to_next_marker1");
904 add_doc_widget("go_to_previous_marker1");
905 add_doc_widget("replace1");
906 add_doc_widget("find_nextsel1");
907 add_doc_widget("find_prevsel1");
908 add_doc_widget("find_usage1");
909 add_doc_widget("find_document_usage1");
910 add_doc_widget("mark_all1");
911 add_doc_widget("go_to_line1");
912 add_doc_widget("goto_tag_definition1");
913 add_doc_widget("goto_tag_declaration1");
914 add_doc_widget("reset_indentation1");
915 add_doc_toolitem("Close");
916 add_doc_toolitem("CloseAll");
917 add_doc_toolitem("Search");
918 add_doc_toolitem("SearchEntry");
919 add_doc_toolitem("ZoomIn");
920 add_doc_toolitem("ZoomOut");
921 add_doc_toolitem("Indent");
922 add_doc_toolitem("UnIndent");
923 add_doc_toolitem("Cut");
924 add_doc_toolitem("Copy");
925 add_doc_toolitem("Paste");
926 add_doc_toolitem("Delete");
927 add_doc_toolitem("Save");
928 add_doc_toolitem("SaveAs");
929 add_doc_toolitem("SaveAll");
930 add_doc_toolitem("Compile");
931 add_doc_toolitem("Run");
932 add_doc_toolitem("Reload");
933 add_doc_toolitem("Color");
934 add_doc_toolitem("Goto");
935 add_doc_toolitem("GotoEntry");
936 add_doc_toolitem("Replace");
937 add_doc_toolitem("Print");
941 void ui_document_buttons_update(void)
944 gboolean enable
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) > 0;
946 for (i
= 0; i
< widgets
.document_buttons
->len
; i
++)
948 GtkWidget
*widget
= g_ptr_array_index(widgets
.document_buttons
, i
);
949 if (GTK_IS_ACTION(widget
))
950 gtk_action_set_sensitive(GTK_ACTION(widget
), enable
);
952 ui_widget_set_sensitive(widget
, enable
);
957 static void on_doc_sensitive_widget_destroy(GtkWidget
*widget
, G_GNUC_UNUSED gpointer user_data
)
959 g_ptr_array_remove_fast(widgets
.document_buttons
, widget
);
963 /** Adds a widget to the list of widgets that should be set sensitive/insensitive
964 * when some documents are present/no documents are open.
965 * It will be removed when the widget is destroyed.
966 * @param widget The widget to add.
971 void ui_add_document_sensitive(GtkWidget
*widget
)
973 gboolean enable
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) > 0;
975 ui_widget_set_sensitive(widget
, enable
);
977 g_ptr_array_add(widgets
.document_buttons
, widget
);
978 g_signal_connect(widget
, "destroy", G_CALLBACK(on_doc_sensitive_widget_destroy
), NULL
);
982 void ui_widget_show_hide(GtkWidget
*widget
, gboolean show
)
986 gtk_widget_show(widget
);
990 gtk_widget_hide(widget
);
995 void ui_sidebar_show_hide(void)
999 /* check that there are no other notebook pages before hiding the sidebar completely
1000 * other pages could be e.g. the file browser plugin */
1001 if (! interface_prefs
.sidebar_openfiles_visible
&& ! interface_prefs
.sidebar_symbol_visible
&&
1002 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
)) <= 2)
1004 ui_prefs
.sidebar_visible
= FALSE
;
1007 widget
= ui_lookup_widget(main_widgets
.window
, "menu_show_sidebar1");
1008 if (ui_prefs
.sidebar_visible
!= gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget
)))
1010 ignore_callback
= TRUE
;
1011 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget
), ui_prefs
.sidebar_visible
);
1012 ignore_callback
= FALSE
;
1015 ui_widget_show_hide(main_widgets
.sidebar_notebook
, ui_prefs
.sidebar_visible
);
1017 ui_widget_show_hide(gtk_notebook_get_nth_page(
1018 GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), 0), interface_prefs
.sidebar_symbol_visible
);
1019 ui_widget_show_hide(gtk_notebook_get_nth_page(
1020 GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), 1), interface_prefs
.sidebar_openfiles_visible
);
1024 void ui_document_show_hide(GeanyDocument
*doc
)
1026 const gchar
*widget_name
;
1028 const GeanyIndentPrefs
*iprefs
;
1030 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
1033 doc
= document_get_current();
1038 ignore_callback
= TRUE
;
1040 gtk_check_menu_item_set_active(
1041 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_line_wrapping1")),
1042 doc
->editor
->line_wrapping
);
1044 gtk_check_menu_item_set_active(
1045 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "line_breaking1")),
1046 doc
->editor
->line_breaking
);
1048 iprefs
= editor_get_indent_prefs(doc
->editor
);
1050 item
= ui_lookup_widget(main_widgets
.window
, "menu_use_auto_indentation1");
1051 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), doc
->editor
->auto_indent
);
1053 switch (iprefs
->type
)
1055 case GEANY_INDENT_TYPE_SPACES
:
1056 widget_name
= "spaces1"; break;
1057 case GEANY_INDENT_TYPE_TABS
:
1058 widget_name
= "tabs1"; break;
1059 case GEANY_INDENT_TYPE_BOTH
:
1061 widget_name
= "tabs_and_spaces1"; break;
1063 item
= ui_lookup_widget(main_widgets
.window
, widget_name
);
1064 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), TRUE
);
1066 if (iprefs
->width
>= 1 && iprefs
->width
<= 8)
1070 name
= g_strdup_printf("indent_width_%d", iprefs
->width
);
1071 item
= ui_lookup_widget(main_widgets
.window
, name
);
1072 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), TRUE
);
1076 gtk_check_menu_item_set_active(
1077 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "set_file_readonly1")),
1080 item
= ui_lookup_widget(main_widgets
.window
, "menu_write_unicode_bom1");
1081 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), doc
->has_bom
);
1082 ui_widget_set_sensitive(item
, encodings_is_unicode_charset(doc
->encoding
));
1084 switch (sci_get_eol_mode(doc
->editor
->sci
))
1086 case SC_EOL_CR
: widget_name
= "cr"; break;
1087 case SC_EOL_LF
: widget_name
= "lf"; break;
1088 default: widget_name
= "crlf"; break;
1090 gtk_check_menu_item_set_active(
1091 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, widget_name
)), TRUE
);
1093 encodings_select_radio_item(doc
->encoding
);
1094 filetypes_select_radio_item(doc
->file_type
);
1096 ignore_callback
= FALSE
;
1100 void ui_set_search_entry_background(GtkWidget
*widget
, gboolean success
)
1102 gtk_widget_set_name(widget
, success
? NULL
: "geany-search-entry-no-match");
1106 static void recent_create_menu(GeanyRecentFiles
*grf
)
1112 len
= MIN(file_prefs
.mru_length
, g_queue_get_length(grf
->recent_queue
));
1113 for (i
= 0; i
< len
; i
++)
1115 filename
= g_queue_peek_nth(grf
->recent_queue
, i
);
1116 /* create menu item for the recent files menu in the menu bar */
1117 tmp
= gtk_menu_item_new_with_label(filename
);
1118 gtk_widget_show(tmp
);
1119 gtk_container_add(GTK_CONTAINER(grf
->menubar
), tmp
);
1120 g_signal_connect(tmp
, "activate", G_CALLBACK(grf
->activate_cb
), NULL
);
1121 /* create menu item for the recent files menu in the toolbar */
1122 if (grf
->toolbar
!= NULL
)
1124 tmp
= gtk_menu_item_new_with_label(filename
);
1125 gtk_widget_show(tmp
);
1126 gtk_container_add(GTK_CONTAINER(grf
->toolbar
), tmp
);
1127 g_signal_connect(tmp
, "activate", G_CALLBACK(grf
->activate_cb
), NULL
);
1133 static GeanyRecentFiles
*recent_get_recent_files(void)
1135 static GeanyRecentFiles grf
= { RECENT_FILE_FILE
, NULL
, NULL
, NULL
, NULL
};
1137 if (G_UNLIKELY(grf
.recent_queue
== NULL
))
1139 grf
.recent_queue
= ui_prefs
.recent_queue
;
1140 grf
.menubar
= ui_widgets
.recent_files_menu_menubar
;
1141 grf
.toolbar
= geany_menu_button_action_get_menu(GEANY_MENU_BUTTON_ACTION(
1142 toolbar_get_action_by_name("Open")));
1143 grf
.activate_cb
= recent_file_activate_cb
;
1149 static GeanyRecentFiles
*recent_get_recent_projects(void)
1151 static GeanyRecentFiles grf
= { RECENT_FILE_PROJECT
, NULL
, NULL
, NULL
, NULL
};
1153 if (G_UNLIKELY(grf
.recent_queue
== NULL
))
1155 grf
.recent_queue
= ui_prefs
.recent_projects_queue
;
1156 grf
.menubar
= ui_widgets
.recent_projects_menu_menubar
;
1158 grf
.activate_cb
= recent_project_activate_cb
;
1164 void ui_create_recent_menus(void)
1166 recent_create_menu(recent_get_recent_files());
1167 recent_create_menu(recent_get_recent_projects());
1171 static void recent_file_activate_cb(GtkMenuItem
*menuitem
, G_GNUC_UNUSED gpointer user_data
)
1173 gchar
*utf8_filename
= ui_menu_item_get_text(menuitem
);
1174 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
1176 if (document_open_file(locale_filename
, FALSE
, NULL
, NULL
) != NULL
)
1177 recent_file_loaded(utf8_filename
, recent_get_recent_files());
1179 g_free(locale_filename
);
1180 g_free(utf8_filename
);
1184 static void recent_project_activate_cb(GtkMenuItem
*menuitem
, G_GNUC_UNUSED gpointer user_data
)
1186 gchar
*utf8_filename
= ui_menu_item_get_text(menuitem
);
1187 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
1189 if (project_ask_close() && project_load_file_with_session(locale_filename
))
1190 recent_file_loaded(utf8_filename
, recent_get_recent_projects());
1192 g_free(locale_filename
);
1193 g_free(utf8_filename
);
1197 static void add_recent_file(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
,
1198 const GtkRecentData
*rdata
)
1200 if (g_queue_find_custom(grf
->recent_queue
, utf8_filename
, (GCompareFunc
) strcmp
) == NULL
)
1203 if (grf
->type
== RECENT_FILE_FILE
&& rdata
)
1205 GtkRecentManager
*manager
= gtk_recent_manager_get_default();
1206 gchar
*uri
= g_filename_to_uri(utf8_filename
, NULL
, NULL
);
1209 gtk_recent_manager_add_full(manager
, uri
, rdata
);
1214 g_queue_push_head(grf
->recent_queue
, g_strdup(utf8_filename
));
1215 if (g_queue_get_length(grf
->recent_queue
) > file_prefs
.mru_length
)
1217 g_free(g_queue_pop_tail(grf
->recent_queue
));
1219 update_recent_menu(grf
);
1221 /* filename already in recent list */
1223 recent_file_loaded(utf8_filename
, grf
);
1227 void ui_add_recent_document(GeanyDocument
*doc
)
1229 /* what are the groups for actually? */
1230 static const gchar
*groups
[2] = {
1234 GtkRecentData rdata
;
1236 /* Prepare the data for gtk_recent_manager_add_full() */
1237 rdata
.display_name
= NULL
;
1238 rdata
.description
= NULL
;
1239 rdata
.mime_type
= doc
->file_type
->mime_type
;
1240 /* if we ain't got no mime-type, fallback to plain text */
1241 if (! rdata
.mime_type
)
1242 rdata
.mime_type
= (gchar
*) "text/plain";
1243 rdata
.app_name
= (gchar
*) "geany";
1244 rdata
.app_exec
= (gchar
*) "geany %u";
1245 rdata
.groups
= (gchar
**) groups
;
1246 rdata
.is_private
= FALSE
;
1248 add_recent_file(doc
->file_name
, recent_get_recent_files(), &rdata
);
1252 void ui_add_recent_project_file(const gchar
*utf8_filename
)
1254 add_recent_file(utf8_filename
, recent_get_recent_projects(), NULL
);
1258 /* Returns: newly allocated string with the UTF-8 menu text. */
1259 gchar
*ui_menu_item_get_text(GtkMenuItem
*menu_item
)
1261 const gchar
*text
= NULL
;
1263 if (gtk_bin_get_child(GTK_BIN(menu_item
)))
1265 GtkWidget
*child
= gtk_bin_get_child(GTK_BIN(menu_item
));
1267 if (GTK_IS_LABEL(child
))
1268 text
= gtk_label_get_text(GTK_LABEL(child
));
1270 /* GTK owns text so it's much safer to return a copy of it in case the memory is reallocated */
1271 return g_strdup(text
);
1275 static gint
find_recent_file_item(gconstpointer list_data
, gconstpointer user_data
)
1277 gchar
*menu_text
= ui_menu_item_get_text(GTK_MENU_ITEM(list_data
));
1280 if (utils_str_equal(menu_text
, user_data
))
1290 /* update the project menu item's sensitivity */
1291 void ui_update_recent_project_menu(void)
1293 GeanyRecentFiles
*grf
= recent_get_recent_projects();
1294 GList
*children
, *item
;
1296 /* only need to update the menubar menu, the project doesn't have a toolbar item */
1297 children
= gtk_container_get_children(GTK_CONTAINER(grf
->menubar
));
1298 for (item
= children
; item
; item
= item
->next
)
1300 gboolean sensitive
= TRUE
;
1304 const gchar
*filename
= gtk_menu_item_get_label(item
->data
);
1305 sensitive
= g_strcmp0(app
->project
->file_name
, filename
) != 0;
1307 gtk_widget_set_sensitive(item
->data
, sensitive
);
1309 g_list_free(children
);
1313 /* Use instead of gtk_menu_reorder_child() to update the menubar properly on OS X */
1314 static void menu_reorder_child(GtkMenu
*menu
, GtkWidget
*child
, gint position
)
1316 gtk_menu_reorder_child(menu
, child
, position
);
1317 #ifdef MAC_INTEGRATION
1318 /* On OS X GtkMenuBar is kept in sync with the native OS X menubar using
1319 * signals. Unfortunately gtk_menu_reorder_child() doesn't emit anything
1320 * so we have to update the OS X menubar manually. */
1321 gtkosx_application_sync_menubar(gtkosx_application_get());
1326 static void add_recent_file_menu_item(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
, GtkWidget
*menu
)
1328 GtkWidget
*child
= gtk_menu_item_new_with_label(utf8_filename
);
1330 gtk_widget_show(child
);
1331 if (menu
!= grf
->toolbar
)
1332 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu
), child
);
1335 /* this is a bit ugly, but we need to use gtk_container_add(). Using
1336 * gtk_menu_shell_prepend() doesn't emit GtkContainer's "add" signal
1337 * which we need in GeanyMenubuttonAction */
1338 gtk_container_add(GTK_CONTAINER(menu
), child
);
1339 menu_reorder_child(GTK_MENU(menu
), child
, 0);
1341 g_signal_connect(child
, "activate", G_CALLBACK(grf
->activate_cb
), NULL
);
1345 static void recent_file_loaded(const gchar
*utf8_filename
, GeanyRecentFiles
*grf
)
1348 GtkWidget
*parents
[] = { grf
->menubar
, grf
->toolbar
};
1351 /* first reorder the queue */
1352 item
= g_queue_find_custom(grf
->recent_queue
, utf8_filename
, (GCompareFunc
) strcmp
);
1353 g_return_if_fail(item
!= NULL
);
1355 g_queue_unlink(grf
->recent_queue
, item
);
1356 g_queue_push_head_link(grf
->recent_queue
, item
);
1358 for (i
= 0; i
< G_N_ELEMENTS(parents
); i
++)
1365 children
= gtk_container_get_children(GTK_CONTAINER(parents
[i
]));
1366 item
= g_list_find_custom(children
, utf8_filename
, (GCompareFunc
) find_recent_file_item
);
1367 /* either reorder or prepend a new one */
1369 menu_reorder_child(GTK_MENU(parents
[i
]), item
->data
, 0);
1371 add_recent_file_menu_item(utf8_filename
, grf
, parents
[i
]);
1372 g_list_free(children
);
1375 if (grf
->type
== RECENT_FILE_PROJECT
)
1376 ui_update_recent_project_menu();
1380 static void update_recent_menu(GeanyRecentFiles
*grf
)
1383 GtkWidget
*parents
[] = { grf
->menubar
, grf
->toolbar
};
1386 filename
= g_queue_peek_head(grf
->recent_queue
);
1388 for (i
= 0; i
< G_N_ELEMENTS(parents
); i
++)
1395 /* clean the MRU list before adding an item */
1396 children
= gtk_container_get_children(GTK_CONTAINER(parents
[i
]));
1397 if (g_list_length(children
) > file_prefs
.mru_length
- 1)
1399 GList
*item
= g_list_nth(children
, file_prefs
.mru_length
- 1);
1400 while (item
!= NULL
)
1402 if (GTK_IS_MENU_ITEM(item
->data
))
1403 gtk_widget_destroy(GTK_WIDGET(item
->data
));
1404 item
= g_list_next(item
);
1407 g_list_free(children
);
1409 /* create the new item */
1410 add_recent_file_menu_item(filename
, grf
, parents
[i
]);
1413 if (grf
->type
== RECENT_FILE_PROJECT
)
1414 ui_update_recent_project_menu();
1418 void ui_toggle_editor_features(GeanyUIEditorFeatures feature
)
1422 foreach_document (i
)
1424 GeanyDocument
*doc
= documents
[i
];
1428 case GEANY_EDITOR_SHOW_MARKERS_MARGIN
:
1429 sci_set_symbol_margin(doc
->editor
->sci
, editor_prefs
.show_markers_margin
);
1431 case GEANY_EDITOR_SHOW_LINE_NUMBERS
:
1432 sci_set_line_numbers(doc
->editor
->sci
, editor_prefs
.show_linenumber_margin
);
1434 case GEANY_EDITOR_SHOW_WHITE_SPACE
:
1435 sci_set_visible_white_spaces(doc
->editor
->sci
, editor_prefs
.show_white_space
);
1437 case GEANY_EDITOR_SHOW_LINE_ENDINGS
:
1438 sci_set_visible_eols(doc
->editor
->sci
, editor_prefs
.show_line_endings
);
1440 case GEANY_EDITOR_SHOW_INDENTATION_GUIDES
:
1441 editor_set_indentation_guides(doc
->editor
);
1448 void ui_update_view_editor_menu_items(void)
1450 ignore_callback
= TRUE
;
1451 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_markers_margin1")), editor_prefs
.show_markers_margin
);
1452 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_linenumber_margin1")), editor_prefs
.show_linenumber_margin
);
1453 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
);
1454 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
);
1455 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
);
1456 ignore_callback
= FALSE
;
1460 /** Creates a GNOME HIG-style frame (with no border and indented child alignment).
1461 * @param label_text The label text.
1462 * @param alignment An address to store the alignment widget pointer.
1463 * @return The frame widget, setting the alignment container for packing child widgets. */
1465 GtkWidget
*ui_frame_new_with_alignment(const gchar
*label_text
, GtkWidget
**alignment
)
1467 GtkWidget
*label
, *align
;
1468 GtkWidget
*frame
= gtk_frame_new(NULL
);
1470 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_NONE
);
1472 align
= gtk_alignment_new(0.5, 0.5, 1, 1);
1473 gtk_container_add(GTK_CONTAINER(frame
), align
);
1474 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 0, 0, 12, 0);
1476 label
= ui_label_new_bold(label_text
);
1477 gtk_frame_set_label_widget(GTK_FRAME(frame
), label
);
1484 /** Makes a fixed border for dialogs without increasing the button box border.
1485 * @param dialog The parent container for the @c GtkVBox.
1486 * @return The packed @c GtkVBox. */
1488 GtkWidget
*ui_dialog_vbox_new(GtkDialog
*dialog
)
1490 GtkWidget
*vbox
= gtk_vbox_new(FALSE
, 12); /* need child vbox to set a separate border. */
1492 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 6);
1493 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog
))), vbox
, TRUE
, TRUE
, 0);
1498 /* Reorders a dialog's buttons
1499 * @param dialog A dialog
1500 * @param response First response ID to reorder
1501 * @param ... more response IDs, terminated by -1
1503 * Like gtk_dialog_set_alternative_button_order(), but reorders the default
1504 * buttons layout, not the alternative one. This is useful if you e.g. added a
1505 * button to a dialog which already had some and need yours not to be on the
1508 /* Heavily based on gtk_dialog_set_alternative_button_order().
1509 * This relies on the action area to be a GtkBox, but although not documented
1510 * the API expose it to be a GtkHButtonBox though GtkBuilder, so it should be
1512 void ui_dialog_set_primary_button_order(GtkDialog
*dialog
, gint response
, ...)
1515 GtkWidget
*action_area
= gtk_dialog_get_action_area(dialog
);
1518 va_start(ap
, response
);
1519 for (position
= 0; response
!= -1; position
++)
1521 GtkWidget
*child
= gtk_dialog_get_widget_for_response(dialog
, response
);
1523 gtk_box_reorder_child(GTK_BOX(action_area
), child
, position
);
1525 g_warning("%s: no child button with response id %d.", G_STRFUNC
, response
);
1527 response
= va_arg(ap
, gint
);
1533 /** Creates a @c GtkButton with custom text and a stock image similar to
1534 * @c gtk_button_new_from_stock().
1535 * @param stock_id A @c GTK_STOCK_NAME string.
1536 * @param text Button label text, can include mnemonics.
1537 * @return The new @c GtkButton.
1540 GtkWidget
*ui_button_new_with_image(const gchar
*stock_id
, const gchar
*text
)
1542 GtkWidget
*image
, *button
;
1544 button
= gtk_button_new_with_mnemonic(text
);
1545 gtk_widget_show(button
);
1546 image
= gtk_image_new_from_stock(stock_id
, GTK_ICON_SIZE_BUTTON
);
1547 gtk_button_set_image(GTK_BUTTON(button
), image
);
1548 /* note: image is shown by gtk */
1553 /** Creates a @c GtkImageMenuItem with a stock image and a custom label.
1554 * @param stock_id Stock image ID, e.g. @c GTK_STOCK_OPEN.
1555 * @param label Menu item label, can include mnemonics.
1556 * @return The new @c GtkImageMenuItem.
1562 ui_image_menu_item_new(const gchar
*stock_id
, const gchar
*label
)
1564 GtkWidget
*item
= gtk_image_menu_item_new_with_mnemonic(label
);
1565 GtkWidget
*image
= gtk_image_new_from_stock(stock_id
, GTK_ICON_SIZE_MENU
);
1567 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
), image
);
1568 gtk_widget_show(image
);
1573 static void entry_clear_icon_release_cb(GtkEntry
*entry
, gint icon_pos
,
1574 GdkEvent
*event
, gpointer data
)
1576 if (event
->button
.button
== 1 && icon_pos
== 1)
1578 gtk_entry_set_text(entry
, "");
1579 gtk_widget_grab_focus(GTK_WIDGET(entry
));
1584 /** Adds a small clear icon to the right end of the passed @a entry.
1585 * A callback to clear the contents of the GtkEntry is automatically added.
1587 * @param entry The GtkEntry object to which the icon should be attached.
1592 void ui_entry_add_clear_icon(GtkEntry
*entry
)
1594 g_object_set(entry
, "secondary-icon-stock", GTK_STOCK_CLEAR
,
1595 "secondary-icon-activatable", TRUE
, NULL
);
1596 g_signal_connect(entry
, "icon-release", G_CALLBACK(entry_clear_icon_release_cb
), NULL
);
1600 /* Adds a :activate-backwards signal emitted by default when <Shift>Return is pressed */
1601 void ui_entry_add_activate_backward_signal(GtkEntry
*entry
)
1603 static gboolean installed
= FALSE
;
1605 g_return_if_fail(GTK_IS_ENTRY(entry
));
1607 if (G_UNLIKELY(! installed
))
1609 GtkBindingSet
*binding_set
;
1613 /* try to handle the unexpected case where GTK would already have installed the signal */
1614 if (g_signal_lookup("activate-backward", G_TYPE_FROM_INSTANCE(entry
)))
1616 g_warning("Signal GtkEntry:activate-backward is unexpectedly already installed");
1620 g_signal_new("activate-backward", G_TYPE_FROM_INSTANCE(entry
),
1621 G_SIGNAL_RUN_LAST
| G_SIGNAL_ACTION
, 0, NULL
, NULL
,
1622 g_cclosure_marshal_VOID__VOID
, G_TYPE_NONE
, 0);
1623 binding_set
= gtk_binding_set_by_class(GTK_ENTRY_GET_CLASS(entry
));
1624 gtk_binding_entry_add_signal(binding_set
, GDK_Return
, GDK_SHIFT_MASK
, "activate-backward", 0);
1629 static void add_to_size_group(GtkWidget
*widget
, gpointer size_group
)
1631 g_return_if_fail(GTK_IS_SIZE_GROUP(size_group
));
1632 gtk_size_group_add_widget(GTK_SIZE_GROUP(size_group
), widget
);
1636 /* Copies the spacing and layout of the master GtkHButtonBox and synchronises
1637 * the width of each button box's children.
1638 * Should be called after all child widgets have been packed. */
1639 void ui_hbutton_box_copy_layout(GtkButtonBox
*master
, GtkButtonBox
*copy
)
1641 GtkSizeGroup
*size_group
;
1643 gtk_box_set_spacing(GTK_BOX(copy
), 10);
1644 gtk_button_box_set_layout(copy
, gtk_button_box_get_layout(master
));
1646 /* now we need to put the widest widget from each button box in a size group,
1647 * but we don't know the width before they are drawn, and for different label
1648 * translations the widest widget can vary, so we just add all widgets. */
1649 size_group
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
1650 gtk_container_foreach(GTK_CONTAINER(master
), add_to_size_group
, size_group
);
1651 gtk_container_foreach(GTK_CONTAINER(copy
), add_to_size_group
, size_group
);
1652 g_object_unref(size_group
);
1656 static gboolean
tree_model_find_text(GtkTreeModel
*model
,
1657 GtkTreeIter
*iter
, gint column
, const gchar
*text
)
1660 gboolean found
= FALSE
;
1662 if (gtk_tree_model_get_iter_first(model
, iter
))
1666 gtk_tree_model_get(model
, iter
, 0, &combo_text
, -1);
1667 found
= utils_str_equal(combo_text
, text
);
1673 while (gtk_tree_model_iter_next(model
, iter
));
1679 /** Prepends @a text to the drop down list, removing a duplicate element in
1680 * the list if found. Also ensures there are <= @a history_len elements.
1681 * @param combo_entry .
1682 * @param text Text to add, or @c NULL for current entry text.
1683 * @param history_len Max number of items, or @c 0 for default. */
1685 void ui_combo_box_add_to_history(GtkComboBoxText
*combo_entry
,
1686 const gchar
*text
, gint history_len
)
1688 GtkComboBox
*combo
= GTK_COMBO_BOX(combo_entry
);
1689 GtkTreeModel
*model
;
1693 if (history_len
<= 0)
1696 text
= gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo
))));
1698 model
= gtk_combo_box_get_model(combo
);
1700 if (tree_model_find_text(model
, &iter
, 0, text
))
1702 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1704 gtk_combo_box_text_prepend_text(combo_entry
, text
);
1707 path
= gtk_tree_path_new_from_indices(history_len
, -1);
1708 if (gtk_tree_model_get_iter(model
, &iter
, path
))
1710 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1712 gtk_tree_path_free(path
);
1716 /* Same as gtk_combo_box_text_prepend_text(), except that text is only prepended if it not already
1717 * exists in the combo's model. */
1718 void ui_combo_box_prepend_text_once(GtkComboBoxText
*combo
, const gchar
*text
)
1720 GtkTreeModel
*model
;
1723 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(combo
));
1724 if (tree_model_find_text(model
, &iter
, 0, text
))
1725 return; /* don't prepend duplicate */
1727 gtk_combo_box_text_prepend_text(combo
, text
);
1731 /* Changes the color of the notebook tab text and open files items according to
1732 * document status. */
1733 void ui_update_tab_status(GeanyDocument
*doc
)
1735 gtk_widget_set_name(doc
->priv
->tab_label
, document_get_status_widget_class(doc
));
1737 sidebar_openfiles_update(doc
);
1741 static gboolean
tree_model_iter_get_next(GtkTreeModel
*model
, GtkTreeIter
*iter
,
1748 return gtk_tree_model_iter_next(model
, iter
);
1750 path
= gtk_tree_model_get_path(model
, iter
);
1751 result
= gtk_tree_path_prev(path
) && gtk_tree_model_get_iter(model
, iter
, path
);
1752 gtk_tree_path_free(path
);
1757 /* note: the while loop might be more efficient when searching upwards if it
1758 * used tree paths instead of tree iters, but in practice it probably doesn't matter much. */
1759 static gboolean
tree_view_find(GtkTreeView
*treeview
, TVMatchCallback cb
, gboolean down
)
1761 GtkTreeSelection
*treesel
;
1763 GtkTreeModel
*model
;
1765 treesel
= gtk_tree_view_get_selection(treeview
);
1766 if (gtk_tree_selection_get_selected(treesel
, &model
, &iter
))
1768 /* get the next selected item */
1769 if (! tree_model_iter_get_next(model
, &iter
, down
))
1770 return FALSE
; /* no more items */
1772 else /* no selection */
1774 if (! gtk_tree_model_get_iter_first(model
, &iter
))
1775 return TRUE
; /* no items */
1779 gtk_tree_selection_select_iter(treesel
, &iter
);
1781 break; /* found next message */
1783 if (! tree_model_iter_get_next(model
, &iter
, down
))
1784 return FALSE
; /* no more items */
1786 /* scroll item in view */
1787 if (ui_prefs
.msgwindow_visible
)
1789 GtkTreePath
*path
= gtk_tree_model_get_path(
1790 gtk_tree_view_get_model(treeview
), &iter
);
1792 gtk_tree_view_scroll_to_cell(treeview
, path
, NULL
, TRUE
, 0.5, 0.5);
1793 gtk_tree_path_free(path
);
1799 /* Returns FALSE if the treeview has items but no matching next item. */
1800 gboolean
ui_tree_view_find_next(GtkTreeView
*treeview
, TVMatchCallback cb
)
1802 return tree_view_find(treeview
, cb
, TRUE
);
1806 /* Returns FALSE if the treeview has items but no matching next item. */
1807 gboolean
ui_tree_view_find_previous(GtkTreeView
*treeview
, TVMatchCallback cb
)
1809 return tree_view_find(treeview
, cb
, FALSE
);
1813 /* Shamelessly stolen from GTK */
1814 static gboolean
ui_tree_view_query_tooltip_cb(GtkWidget
*widget
, gint x
, gint y
,
1815 gboolean keyboard_tip
, GtkTooltip
*tooltip
, gpointer data
)
1817 GValue value
= { 0 };
1818 GValue transformed
= { 0 };
1821 GtkTreeModel
*model
;
1822 GtkTreeView
*tree_view
= GTK_TREE_VIEW(widget
);
1823 gint column
= GPOINTER_TO_INT(data
);
1824 gboolean tootlip_set
= FALSE
;
1826 if (! gtk_tree_view_get_tooltip_context(tree_view
, &x
, &y
, keyboard_tip
, &model
, &path
, &iter
))
1829 gtk_tree_model_get_value(model
, &iter
, column
, &value
);
1831 g_value_init(&transformed
, G_TYPE_STRING
);
1832 if (g_value_transform(&value
, &transformed
) && g_value_get_string(&transformed
))
1834 gtk_tooltip_set_text(tooltip
, g_value_get_string(&transformed
));
1835 gtk_tree_view_set_tooltip_row(tree_view
, tooltip
, path
);
1839 g_value_unset(&transformed
);
1840 g_value_unset(&value
);
1841 gtk_tree_path_free(path
);
1847 /** Adds text tooltips to a tree view.
1849 * This is similar to gtk_tree_view_set_tooltip_column() but considers the column contents to be
1850 * text, not markup -- it uses gtk_tooltip_set_text() rather than gtk_tooltip_set_markup() to set
1851 * the tooltip's value.
1853 * @warning Unlike gtk_tree_view_set_tooltip_column() you currently cannot change or remove the
1854 * tooltip column after it has been added. Trying to do so will probably give funky results.
1856 * @param tree_view The tree view
1857 * @param column The column to get the tooltip from
1859 * @since 1.25 (API 223)
1861 /* Note: @p column is int and not uint both to match gtk_tree_view_set_tooltip_column() signature
1862 * and to allow future support of -1 to unset if ever wanted */
1864 void ui_tree_view_set_tooltip_text_column(GtkTreeView
*tree_view
, gint column
)
1866 g_return_if_fail(column
>= 0);
1867 g_return_if_fail(GTK_IS_TREE_VIEW(tree_view
));
1869 g_signal_connect(tree_view
, "query-tooltip",
1870 G_CALLBACK(ui_tree_view_query_tooltip_cb
), GINT_TO_POINTER(column
));
1871 gtk_widget_set_has_tooltip(GTK_WIDGET(tree_view
), TRUE
);
1876 * Modifies the font of a widget using gtk_widget_modify_font().
1878 * @param widget The widget.
1879 * @param str The font name as expected by pango_font_description_from_string().
1882 void ui_widget_modify_font_from_string(GtkWidget
*widget
, const gchar
*str
)
1884 PangoFontDescription
*pfd
;
1886 pfd
= pango_font_description_from_string(str
);
1887 gtk_widget_modify_font(widget
, pfd
);
1888 pango_font_description_free(pfd
);
1892 /** Creates a @c GtkHBox with @a entry packed into it and an open button which runs a
1893 * file chooser, replacing entry text (if successful) with the path returned from the
1894 * @c GtkFileChooser.
1895 * @note @a entry can be the child of an unparented widget, such as @c GtkComboBoxEntry.
1896 * @param title The file chooser dialog title, or @c NULL.
1897 * @param action The mode of the file chooser.
1898 * @param entry Can be an unpacked @c GtkEntry, or the child of an unpacked widget,
1899 * such as @c GtkComboBoxEntry.
1900 * @return The @c GtkHBox.
1902 /* @see ui_setup_open_button_callback(). */
1904 GtkWidget
*ui_path_box_new(const gchar
*title
, GtkFileChooserAction action
, GtkEntry
*entry
)
1906 GtkWidget
*vbox
, *dirbtn
, *openimg
, *hbox
, *path_entry
;
1908 hbox
= gtk_hbox_new(FALSE
, 6);
1909 path_entry
= GTK_WIDGET(entry
);
1911 /* prevent path_entry being vertically stretched to the height of dirbtn */
1912 vbox
= gtk_vbox_new(FALSE
, 0);
1913 if (gtk_widget_get_parent(path_entry
)) /* entry->parent may be a GtkComboBoxEntry */
1915 GtkWidget
*parent
= gtk_widget_get_parent(path_entry
);
1917 gtk_box_pack_start(GTK_BOX(vbox
), parent
, TRUE
, FALSE
, 0);
1920 gtk_box_pack_start(GTK_BOX(vbox
), path_entry
, TRUE
, FALSE
, 0);
1922 dirbtn
= gtk_button_new();
1923 openimg
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
1924 gtk_container_add(GTK_CONTAINER(dirbtn
), openimg
);
1925 ui_setup_open_button_callback(dirbtn
, title
, action
, entry
);
1927 gtk_box_pack_end(GTK_BOX(hbox
), dirbtn
, FALSE
, FALSE
, 0);
1928 gtk_box_pack_end(GTK_BOX(hbox
), vbox
, TRUE
, TRUE
, 0);
1933 static void ui_path_box_open_clicked(GtkButton
*button
, gpointer user_data
);
1936 /* Setup a GtkButton to run a GtkFileChooser, setting entry text if successful.
1937 * title can be NULL.
1938 * action is the file chooser mode to use. */
1939 void ui_setup_open_button_callback(GtkWidget
*open_btn
, const gchar
*title
,
1940 GtkFileChooserAction action
, GtkEntry
*entry
)
1942 GtkWidget
*path_entry
= GTK_WIDGET(entry
);
1945 g_object_set_data_full(G_OBJECT(open_btn
), "title", g_strdup(title
),
1946 (GDestroyNotify
) g_free
);
1947 g_object_set_data(G_OBJECT(open_btn
), "action", GINT_TO_POINTER(action
));
1948 g_signal_connect(open_btn
, "clicked", G_CALLBACK(ui_path_box_open_clicked
), path_entry
);
1953 static gchar
*run_file_chooser(const gchar
*title
, GtkFileChooserAction action
,
1954 const gchar
*utf8_path
)
1956 GtkWidget
*dialog
= gtk_file_chooser_dialog_new(title
,
1957 GTK_WINDOW(main_widgets
.window
), action
,
1958 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1959 GTK_STOCK_OPEN
, GTK_RESPONSE_OK
, NULL
);
1961 gchar
*ret_path
= NULL
;
1963 gtk_widget_set_name(dialog
, "GeanyDialog");
1964 locale_path
= utils_get_locale_from_utf8(utf8_path
);
1965 if (action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
)
1967 if (g_path_is_absolute(locale_path
) && g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
1968 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
1970 else if (action
== GTK_FILE_CHOOSER_ACTION_OPEN
)
1972 if (g_path_is_absolute(locale_path
))
1973 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), locale_path
);
1975 g_free(locale_path
);
1977 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_OK
)
1981 dir_locale
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
1982 ret_path
= utils_get_utf8_from_locale(dir_locale
);
1985 gtk_widget_destroy(dialog
);
1991 static void ui_path_box_open_clicked(GtkButton
*button
, gpointer user_data
)
1993 GtkFileChooserAction action
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button
), "action"));
1994 GtkEntry
*entry
= user_data
;
1995 const gchar
*title
= g_object_get_data(G_OBJECT(button
), "title");
1996 gchar
*utf8_path
= NULL
;
1998 /* TODO: extend for other actions */
1999 g_return_if_fail(action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
||
2000 action
== GTK_FILE_CHOOSER_ACTION_OPEN
);
2003 title
= (action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
) ?
2004 _("Select Folder") : _("Select File");
2006 if (action
== GTK_FILE_CHOOSER_ACTION_OPEN
)
2009 utf8_path
= win32_show_file_dialog(GTK_WINDOW(ui_widgets
.prefs_dialog
), title
,
2010 gtk_entry_get_text(GTK_ENTRY(entry
)));
2012 utf8_path
= run_file_chooser(title
, action
, gtk_entry_get_text(GTK_ENTRY(entry
)));
2015 else if (action
== GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
)
2017 gchar
*path
= g_path_get_dirname(gtk_entry_get_text(GTK_ENTRY(entry
)));
2019 utf8_path
= win32_show_folder_dialog(ui_widgets
.prefs_dialog
, title
,
2020 gtk_entry_get_text(GTK_ENTRY(entry
)));
2022 utf8_path
= run_file_chooser(title
, action
, path
);
2027 if (utf8_path
!= NULL
)
2029 gtk_entry_set_text(GTK_ENTRY(entry
), utf8_path
);
2035 void ui_statusbar_showhide(gboolean state
)
2037 /* handle statusbar visibility */
2040 gtk_widget_show(ui_widgets
.statusbar
);
2041 ui_update_statusbar(NULL
, -1);
2044 gtk_widget_hide(ui_widgets
.statusbar
);
2048 /** Packs all @c GtkWidgets passed after the row argument into a table, using
2049 * one widget per cell. The first widget is not expanded as the table grows,
2050 * as this is usually a label.
2052 * @param row The row number of the table.
2055 void ui_table_add_row(GtkTable
*table
, gint row
, ...)
2061 va_start(args
, row
);
2062 for (i
= 0; (widget
= va_arg(args
, GtkWidget
*), widget
!= NULL
); i
++)
2064 gint options
= (i
== 0) ? GTK_FILL
: GTK_EXPAND
| GTK_FILL
;
2066 gtk_table_attach(GTK_TABLE(table
), widget
, i
, i
+ 1, row
, row
+ 1,
2073 static void on_config_file_clicked(GtkWidget
*widget
, gpointer user_data
)
2075 const gchar
*file_name
= user_data
;
2076 GeanyFiletype
*ft
= NULL
;
2078 if (strstr(file_name
, G_DIR_SEPARATOR_S
"filetypes."))
2079 ft
= filetypes
[GEANY_FILETYPES_CONF
];
2081 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
))
2082 document_open_file(file_name
, FALSE
, ft
, NULL
);
2085 gchar
*utf8_filename
= utils_get_utf8_from_locale(file_name
);
2086 gchar
*base_name
= g_path_get_basename(file_name
);
2087 gchar
*global_file
= g_build_filename(app
->datadir
, base_name
, NULL
);
2088 gchar
*global_content
= NULL
;
2090 /* if the requested file doesn't exist in the user's config dir, try loading the file
2091 * from the global data directory and use its contents for the newly created file */
2092 if (g_file_test(global_file
, G_FILE_TEST_EXISTS
))
2093 g_file_get_contents(global_file
, &global_content
, NULL
, NULL
);
2095 document_new_file(utf8_filename
, ft
, global_content
);
2097 utils_free_pointers(4, utf8_filename
, base_name
, global_file
, global_content
, NULL
);
2102 static void free_on_closure_notify(gpointer data
, GClosure
*closure
)
2108 /* @note You should connect to the "document-save" signal yourself to detect
2109 * if the user has just saved the config file, reloading it. */
2110 void ui_add_config_file_menu_item(const gchar
*real_path
, const gchar
*label
, GtkContainer
*parent
)
2115 parent
= GTK_CONTAINER(widgets
.config_files_menu
);
2121 base_name
= g_path_get_basename(real_path
);
2122 item
= gtk_menu_item_new_with_label(base_name
);
2126 item
= gtk_menu_item_new_with_mnemonic(label
);
2128 gtk_widget_show(item
);
2129 gtk_container_add(parent
, item
);
2130 g_signal_connect_data(item
, "activate", G_CALLBACK(on_config_file_clicked
),
2131 g_strdup(real_path
), free_on_closure_notify
, 0);
2135 static gboolean
sort_menu(gpointer data
)
2137 ui_menu_sort_by_label(GTK_MENU(data
));
2142 static void create_config_files_menu(void)
2144 GtkWidget
*menu
, *item
;
2146 widgets
.config_files_menu
= menu
= gtk_menu_new();
2148 item
= ui_lookup_widget(main_widgets
.window
, "configuration_files1");
2149 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), menu
);
2151 /* sort menu after all items added */
2152 g_idle_add(sort_menu
, widgets
.config_files_menu
);
2156 /* adds factory icons with a named icon source using the stock items id */
2157 static void add_stock_icons(const GtkStockItem
*items
, gsize count
)
2159 GtkIconFactory
*factory
= gtk_icon_factory_new();
2160 GtkIconSource
*source
= gtk_icon_source_new();
2163 for (i
= 0; i
< count
; i
++)
2165 GtkIconSet
*set
= gtk_icon_set_new();
2167 gtk_icon_source_set_icon_name(source
, items
[i
].stock_id
);
2168 gtk_icon_set_add_source(set
, source
);
2169 gtk_icon_factory_add(factory
, items
[i
].stock_id
, set
);
2170 gtk_icon_set_unref(set
);
2172 gtk_icon_source_free(source
);
2173 gtk_icon_factory_add_default(factory
);
2174 g_object_unref(factory
);
2178 void ui_init_stock_items(void)
2180 GtkStockItem items
[] =
2182 { GEANY_STOCK_SAVE_ALL
, N_("Save All"), 0, 0, GETTEXT_PACKAGE
},
2183 { GEANY_STOCK_CLOSE_ALL
, N_("Close All"), 0, 0, GETTEXT_PACKAGE
},
2184 { GEANY_STOCK_BUILD
, N_("Build"), 0, 0, GETTEXT_PACKAGE
}
2187 gtk_stock_add(items
, G_N_ELEMENTS(items
));
2188 add_stock_icons(items
, G_N_ELEMENTS(items
));
2192 void ui_init_toolbar_widgets(void)
2194 widgets
.save_buttons
[1] = toolbar_get_widget_by_name("Save");
2195 widgets
.save_buttons
[3] = toolbar_get_widget_by_name("SaveAll");
2196 widgets
.redo_items
[2] = toolbar_get_widget_by_name("Redo");
2197 widgets
.undo_items
[2] = toolbar_get_widget_by_name("Undo");
2201 void ui_swap_sidebar_pos(void)
2203 GtkWidget
*pane
= ui_lookup_widget(main_widgets
.window
, "hpaned1");
2204 GtkWidget
*left
= gtk_paned_get_child1(GTK_PANED(pane
));
2205 GtkWidget
*right
= gtk_paned_get_child2(GTK_PANED(pane
));
2208 g_object_ref(right
);
2209 gtk_container_remove (GTK_CONTAINER (pane
), left
);
2210 gtk_container_remove (GTK_CONTAINER (pane
), right
);
2211 /* only scintilla notebook should expand */
2212 gtk_paned_pack1(GTK_PANED(pane
), right
, right
== main_widgets
.notebook
, TRUE
);
2213 gtk_paned_pack2(GTK_PANED(pane
), left
, left
== main_widgets
.notebook
, TRUE
);
2214 g_object_unref(left
);
2215 g_object_unref(right
);
2217 gtk_paned_set_position(GTK_PANED(pane
), gtk_widget_get_allocated_width(pane
)
2218 - gtk_paned_get_position(GTK_PANED(pane
)));
2222 static void init_recent_files(void)
2224 GtkWidget
*toolbar_recent_files_menu
;
2226 /* add recent files to the File menu */
2227 ui_widgets
.recent_files_menuitem
= ui_lookup_widget(main_widgets
.window
, "recent_files1");
2228 ui_widgets
.recent_files_menu_menubar
= gtk_menu_new();
2229 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets
.recent_files_menuitem
),
2230 ui_widgets
.recent_files_menu_menubar
);
2232 /* add recent files to the toolbar Open button */
2233 toolbar_recent_files_menu
= gtk_menu_new();
2234 g_object_ref(toolbar_recent_files_menu
);
2235 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
2236 toolbar_get_action_by_name("Open")), toolbar_recent_files_menu
);
2240 static void ui_menu_move(GtkWidget
*menu
, GtkWidget
*old
, GtkWidget
*new)
2243 gtk_menu_item_set_submenu(GTK_MENU_ITEM(old
), NULL
);
2244 gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), menu
);
2245 g_object_unref(menu
);
2249 typedef struct GeanySharedMenu
2252 const gchar
*menubar_item
;
2253 const gchar
*popup_item
;
2257 #define foreach_menu(item, array) \
2258 for (item = array; item->menu; item++)
2260 static void on_editor_menu_show(GtkWidget
*widget
, GeanySharedMenu
*items
)
2262 GeanySharedMenu
*item
;
2264 foreach_menu(item
, items
)
2266 GtkWidget
*popup
= ui_lookup_widget(main_widgets
.editor_menu
, item
->popup_item
);
2267 GtkWidget
*bar
= ui_lookup_widget(main_widgets
.window
, item
->menubar_item
);
2268 GtkWidget
*menu
= ui_lookup_widget(main_widgets
.window
, item
->menu
);
2270 ui_menu_move(menu
, bar
, popup
);
2275 static void on_editor_menu_hide(GtkWidget
*widget
, GeanySharedMenu
*items
)
2277 GeanySharedMenu
*item
;
2279 foreach_menu(item
, items
)
2281 GtkWidget
*popup
= ui_lookup_widget(main_widgets
.editor_menu
, item
->popup_item
);
2282 GtkWidget
*bar
= ui_lookup_widget(main_widgets
.window
, item
->menubar_item
);
2283 GtkWidget
*menu
= ui_lookup_widget(main_widgets
.window
, item
->menu
);
2285 ui_menu_move(menu
, popup
, bar
);
2290 /* Currently ui_init() is called before keyfile.c stash group code is initialized,
2291 * so this is called after that's done. */
2292 void ui_init_prefs(void)
2294 StashGroup
*group
= stash_group_new(PACKAGE
);
2297 configuration_add_various_pref_group(group
);
2299 stash_group_add_boolean(group
, &interface_prefs
.show_symbol_list_expanders
,
2300 "show_symbol_list_expanders", TRUE
);
2301 stash_group_add_boolean(group
, &interface_prefs
.compiler_tab_autoscroll
,
2302 "compiler_tab_autoscroll", TRUE
);
2303 stash_group_add_boolean(group
, &ui_prefs
.allow_always_save
,
2304 "allow_always_save", FALSE
);
2305 stash_group_add_string(group
, &ui_prefs
.statusbar_template
,
2306 "statusbar_template", _(DEFAULT_STATUSBAR_TEMPLATE
));
2307 stash_group_add_boolean(group
, &ui_prefs
.new_document_after_close
,
2308 "new_document_after_close", FALSE
);
2309 stash_group_add_boolean(group
, &interface_prefs
.msgwin_status_visible
,
2310 "msgwin_status_visible", TRUE
);
2311 stash_group_add_boolean(group
, &interface_prefs
.msgwin_compiler_visible
,
2312 "msgwin_compiler_visible", TRUE
);
2313 stash_group_add_boolean(group
, &interface_prefs
.msgwin_messages_visible
,
2314 "msgwin_messages_visible", TRUE
);
2315 stash_group_add_boolean(group
, &interface_prefs
.msgwin_scribble_visible
,
2316 "msgwin_scribble_visible", TRUE
);
2320 /* Used to find out the name of the GtkBuilder retrieved object since
2321 * some objects will be GTK_IS_BUILDABLE() and use the GtkBuildable
2322 * 'name' property for that and those that don't implement GtkBuildable
2323 * will have a "gtk-builder-name" stored in the GObject's data list. */
2324 static const gchar
*ui_guess_object_name(GObject
*obj
)
2326 const gchar
*name
= NULL
;
2328 g_return_val_if_fail(G_IS_OBJECT(obj
), NULL
);
2330 if (GTK_IS_BUILDABLE(obj
))
2331 name
= gtk_buildable_get_name(GTK_BUILDABLE(obj
));
2333 name
= g_object_get_data(obj
, "gtk-builder-name");
2341 /* Compatibility functions */
2342 GtkWidget
*create_edit_menu1(void)
2348 GtkWidget
*create_prefs_dialog(void)
2350 return prefs_dialog
;
2354 GtkWidget
*create_project_dialog(void)
2356 return project_dialog
;
2360 GtkWidget
*create_toolbar_popup_menu1(void)
2362 return toolbar_popup_menu1
;
2366 GtkWidget
*create_window1(void)
2372 static GtkWidget
*ui_get_top_parent(GtkWidget
*widget
)
2376 g_return_val_if_fail(GTK_IS_WIDGET(widget
), NULL
);
2380 if (GTK_IS_MENU(widget
))
2381 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
2383 parent
= gtk_widget_get_parent(widget
);
2385 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
2395 void ui_init_builder(void)
2397 gchar
*interface_file
;
2400 GSList
*iter
, *all_objects
;
2401 GtkWidget
*widget
, *toplevel
;
2403 /* prevent function from being called twice */
2404 if (GTK_IS_BUILDER(builder
))
2407 builder
= gtk_builder_new();
2409 gtk_builder_set_translation_domain(builder
, GETTEXT_PACKAGE
);
2412 interface_file
= g_build_filename(app
->datadir
, "geany.glade", NULL
);
2413 if (! gtk_builder_add_from_file(builder
, interface_file
, &error
))
2415 /* Show the user this message so they know WTF happened */
2416 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR
,
2417 _("Geany cannot start!"), error
->message
);
2419 g_error("Cannot create user-interface: %s", error
->message
);
2420 g_error_free(error
);
2421 g_free(interface_file
);
2422 g_object_unref(builder
);
2425 g_free(interface_file
);
2427 callbacks_connect(builder
);
2429 edit_menu1
= GTK_WIDGET(gtk_builder_get_object(builder
, "edit_menu1"));
2430 prefs_dialog
= GTK_WIDGET(gtk_builder_get_object(builder
, "prefs_dialog"));
2431 project_dialog
= GTK_WIDGET(gtk_builder_get_object(builder
, "project_dialog"));
2432 toolbar_popup_menu1
= GTK_WIDGET(gtk_builder_get_object(builder
, "toolbar_popup_menu1"));
2433 window1
= GTK_WIDGET(gtk_builder_get_object(builder
, "window1"));
2435 g_object_set_data(G_OBJECT(edit_menu1
), "edit_menu1", edit_menu1
);
2436 g_object_set_data(G_OBJECT(prefs_dialog
), "prefs_dialog", prefs_dialog
);
2437 g_object_set_data(G_OBJECT(project_dialog
), "project_dialog", project_dialog
);
2438 g_object_set_data(G_OBJECT(toolbar_popup_menu1
), "toolbar_popup_menu1", toolbar_popup_menu1
);
2439 g_object_set_data(G_OBJECT(window1
), "window1", window1
);
2441 all_objects
= gtk_builder_get_objects(builder
);
2442 for (iter
= all_objects
; iter
!= NULL
; iter
= g_slist_next(iter
))
2444 if (! GTK_IS_WIDGET(iter
->data
))
2447 widget
= GTK_WIDGET(iter
->data
);
2449 name
= ui_guess_object_name(G_OBJECT(widget
));
2452 g_warning("Unable to get name from GtkBuilder object");
2456 toplevel
= ui_get_top_parent(widget
);
2458 ui_hookup_widget(toplevel
, widget
, name
);
2460 g_slist_free(all_objects
);
2464 static void init_custom_style(void)
2466 #if GTK_CHECK_VERSION(3, 0, 0)
2467 gchar
*css_file
= g_build_filename(app
->datadir
, "geany.css", NULL
);
2468 GtkCssProvider
*css
= gtk_css_provider_new();
2469 GError
*error
= NULL
;
2471 if (! gtk_css_provider_load_from_path(css
, css_file
, &error
))
2473 g_warning("Failed to load custom CSS: %s", error
->message
);
2474 g_error_free(error
);
2478 gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
2479 GTK_STYLE_PROVIDER(css
), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
2482 g_object_unref(css
);
2485 /* see setup_gtk2_styles() in main.c */
2492 init_custom_style();
2494 init_recent_files();
2496 ui_widgets
.statusbar
= ui_lookup_widget(main_widgets
.window
, "statusbar");
2497 ui_widgets
.print_page_setup
= ui_lookup_widget(main_widgets
.window
, "page_setup1");
2499 main_widgets
.progressbar
= progress_bar_create();
2501 /* current word sensitive items */
2502 widgets
.popup_goto_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "goto_tag_definition2");
2503 widgets
.popup_goto_items
[1] = ui_lookup_widget(main_widgets
.editor_menu
, "context_action1");
2504 widgets
.popup_goto_items
[2] = ui_lookup_widget(main_widgets
.editor_menu
, "find_usage2");
2505 widgets
.popup_goto_items
[3] = ui_lookup_widget(main_widgets
.editor_menu
, "find_document_usage2");
2507 widgets
.popup_copy_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "cut1");
2508 widgets
.popup_copy_items
[1] = ui_lookup_widget(main_widgets
.editor_menu
, "copy1");
2509 widgets
.popup_copy_items
[2] = ui_lookup_widget(main_widgets
.editor_menu
, "delete1");
2510 widgets
.menu_copy_items
[0] = ui_lookup_widget(main_widgets
.window
, "menu_cut1");
2511 widgets
.menu_copy_items
[1] = ui_lookup_widget(main_widgets
.window
, "menu_copy1");
2512 widgets
.menu_copy_items
[2] = ui_lookup_widget(main_widgets
.window
, "menu_delete1");
2513 widgets
.menu_insert_include_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "insert_include1");
2514 widgets
.menu_insert_include_items
[1] = ui_lookup_widget(main_widgets
.window
, "insert_include2");
2515 widgets
.save_buttons
[0] = ui_lookup_widget(main_widgets
.window
, "menu_save1");
2516 widgets
.save_buttons
[2] = ui_lookup_widget(main_widgets
.window
, "menu_save_all1");
2517 widgets
.redo_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "redo1");
2518 widgets
.redo_items
[1] = ui_lookup_widget(main_widgets
.window
, "menu_redo2");
2519 widgets
.undo_items
[0] = ui_lookup_widget(main_widgets
.editor_menu
, "undo1");
2520 widgets
.undo_items
[1] = ui_lookup_widget(main_widgets
.window
, "menu_undo2");
2522 /* reparent context submenus as needed */
2524 GeanySharedMenu arr
[] = {
2525 {"commands2_menu", "commands2", "commands1"},
2526 {"menu_format1_menu", "menu_format1", "menu_format2"},
2527 {"more1_menu", "more1", "search2"},
2530 static GeanySharedMenu items
[G_N_ELEMENTS(arr
)];
2532 memcpy(items
, arr
, sizeof(arr
));
2533 g_signal_connect(main_widgets
.editor_menu
, "show", G_CALLBACK(on_editor_menu_show
), items
);
2534 g_signal_connect(main_widgets
.editor_menu
, "hide", G_CALLBACK(on_editor_menu_hide
), items
);
2537 ui_init_toolbar_widgets();
2538 init_document_widgets();
2539 create_config_files_menu();
2543 void ui_finalize_builder(void)
2545 if (GTK_IS_BUILDER(builder
))
2546 g_object_unref(builder
);
2548 /* cleanup refs lingering even after GtkBuilder is destroyed */
2549 if (GTK_IS_WIDGET(edit_menu1
))
2550 gtk_widget_destroy(edit_menu1
);
2551 if (GTK_IS_WIDGET(prefs_dialog
))
2552 gtk_widget_destroy(prefs_dialog
);
2553 if (GTK_IS_WIDGET(project_dialog
))
2554 gtk_widget_destroy(project_dialog
);
2555 if (GTK_IS_WIDGET(toolbar_popup_menu1
))
2556 gtk_widget_destroy(toolbar_popup_menu1
);
2557 if (GTK_IS_WIDGET(window1
))
2558 gtk_widget_destroy(window1
);
2562 static void auto_separator_update(GeanyAutoSeparator
*autosep
)
2564 g_return_if_fail(autosep
->item_count
>= 0);
2566 if (autosep
->widget
)
2568 if (autosep
->item_count
> 0)
2569 ui_widget_show_hide(autosep
->widget
, autosep
->show_count
> 0);
2571 gtk_widget_destroy(autosep
->widget
);
2576 static void on_auto_separator_item_show_hide(GtkWidget
*widget
, gpointer user_data
)
2578 GeanyAutoSeparator
*autosep
= user_data
;
2580 if (gtk_widget_get_visible(widget
))
2581 autosep
->show_count
++;
2583 autosep
->show_count
--;
2584 auto_separator_update(autosep
);
2588 static void on_auto_separator_item_destroy(GtkWidget
*widget
, gpointer user_data
)
2590 GeanyAutoSeparator
*autosep
= user_data
;
2592 autosep
->item_count
--;
2593 autosep
->item_count
= MAX(autosep
->item_count
, 0);
2594 /* gtk_widget_get_visible() won't work now the widget is being destroyed,
2595 * so assume widget was visible */
2596 autosep
->show_count
--;
2597 autosep
->show_count
= MAX(autosep
->item_count
, 0);
2598 auto_separator_update(autosep
);
2602 /* Show the separator widget if @a item or another is visible. */
2603 /* Note: This would be neater taking a widget argument, setting a "visible-count"
2604 * property, and using reference counting to keep the widget alive whilst its visible group
2606 void ui_auto_separator_add_ref(GeanyAutoSeparator
*autosep
, GtkWidget
*item
)
2608 /* set widget ptr NULL when widget destroyed */
2609 if (autosep
->item_count
== 0)
2610 g_signal_connect(autosep
->widget
, "destroy",
2611 G_CALLBACK(gtk_widget_destroyed
), &autosep
->widget
);
2613 if (gtk_widget_get_visible(item
))
2614 autosep
->show_count
++;
2616 autosep
->item_count
++;
2617 auto_separator_update(autosep
);
2619 g_signal_connect(item
, "show", G_CALLBACK(on_auto_separator_item_show_hide
), autosep
);
2620 g_signal_connect(item
, "hide", G_CALLBACK(on_auto_separator_item_show_hide
), autosep
);
2621 g_signal_connect(item
, "destroy", G_CALLBACK(on_auto_separator_item_destroy
), autosep
);
2626 * Sets @a text as the contents of the tooltip for @a widget.
2628 * @param widget The widget the tooltip should be set for.
2629 * @param text The text for the tooltip.
2632 * @deprecated 0.21 use gtk_widget_set_tooltip_text() instead
2635 void ui_widget_set_tooltip_text(GtkWidget
*widget
, const gchar
*text
)
2637 gtk_widget_set_tooltip_text(widget
, text
);
2641 /** Returns a widget from a name in a component, usually created by Glade.
2642 * Call it with the toplevel widget in the component (i.e. a window/dialog),
2643 * or alternatively any widget in the component, and the name of the widget
2644 * you want returned.
2645 * @param widget Widget with the @a widget_name property set.
2646 * @param widget_name Name to lookup.
2647 * @return The widget found.
2648 * @see ui_hookup_widget().
2653 GtkWidget
*ui_lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
2655 GtkWidget
*parent
, *found_widget
;
2657 g_return_val_if_fail(widget
!= NULL
, NULL
);
2658 g_return_val_if_fail(widget_name
!= NULL
, NULL
);
2662 if (GTK_IS_MENU(widget
))
2663 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
2665 parent
= gtk_widget_get_parent(widget
);
2667 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
2673 found_widget
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), widget_name
);
2674 if (G_UNLIKELY(found_widget
== NULL
))
2675 g_warning("Widget not found: %s", widget_name
);
2676 return found_widget
;
2680 /* wraps gtk_builder_get_object()
2681 * unlike ui_lookup_widget(), it does only support getting object created from the main
2682 * UI file, but it can fetch any object, not only widgets */
2683 gpointer
ui_builder_get_object (const gchar
*name
)
2685 return gtk_builder_get_object (builder
, name
);
2690 static guint progress_bar_timer_id
= 0;
2693 static GtkWidget
*progress_bar_create(void)
2695 GtkWidget
*bar
= gtk_progress_bar_new();
2697 /* Set the progressbar's height to 1 to fit it in the statusbar */
2698 gtk_widget_set_size_request(bar
, -1, 1);
2699 gtk_box_pack_start (GTK_BOX(ui_widgets
.statusbar
), bar
, FALSE
, FALSE
, 3);
2705 static gboolean
progress_bar_pulse(gpointer data
)
2707 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets
.progressbar
));
2714 * Starts a constantly pulsing progressbar in the right corner of the statusbar
2715 * (if the statusbar is visible). This is a convenience function which adds a timer to
2716 * pulse the progressbar constantly until ui_progress_bar_stop() is called.
2717 * You can use this function when you have time consuming asynchronous operation and want to
2718 * display some activity in the GUI and when you don't know about detailed progress steps.
2719 * The progressbar widget is hidden by default when it is not active. This function and
2720 * ui_progress_bar_stop() will show and hide it automatically for you.
2722 * You can also access the progressbar widget directly using @c geany->main_widgets->progressbar
2723 * and use the GtkProgressBar API to set discrete fractions to display better progress information.
2724 * In this case, you need to show and hide the widget yourself. You can find some example code
2725 * in @c src/printing.c.
2727 * @param text The text to be shown as the progress bar label or NULL to leave it empty.
2732 void ui_progress_bar_start(const gchar
*text
)
2734 g_return_if_fail(progress_bar_timer_id
== 0);
2736 if (! interface_prefs
.statusbar_visible
)
2739 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets
.progressbar
), text
);
2741 progress_bar_timer_id
= g_timeout_add(200, progress_bar_pulse
, NULL
);
2743 gtk_widget_show(GTK_WIDGET(main_widgets
.progressbar
));
2747 /** Stops a running progress bar and hides the widget again.
2752 void ui_progress_bar_stop(void)
2754 gtk_widget_hide(GTK_WIDGET(main_widgets
.progressbar
));
2756 if (progress_bar_timer_id
!= 0)
2758 g_source_remove(progress_bar_timer_id
);
2759 progress_bar_timer_id
= 0;
2764 static gint
compare_menu_item_labels(gconstpointer a
, gconstpointer b
)
2766 GtkMenuItem
*item_a
= GTK_MENU_ITEM(a
);
2767 GtkMenuItem
*item_b
= GTK_MENU_ITEM(b
);
2771 sa
= ui_menu_item_get_text(item_a
);
2772 sb
= ui_menu_item_get_text(item_b
);
2773 result
= utils_str_casecmp(sa
, sb
);
2780 /* Currently @a menu should contain only GtkMenuItems with labels. */
2781 void ui_menu_sort_by_label(GtkMenu
*menu
)
2783 GList
*list
= gtk_container_get_children(GTK_CONTAINER(menu
));
2787 list
= g_list_sort(list
, compare_menu_item_labels
);
2789 foreach_list(node
, list
)
2791 menu_reorder_child(menu
, node
->data
, pos
);
2798 void ui_label_set_markup(GtkLabel
*label
, const gchar
*format
, ...)
2803 va_start(a
, format
);
2804 text
= g_strdup_vprintf(format
, a
);
2807 gtk_label_set_text(label
, text
);
2808 gtk_label_set_use_markup(label
, TRUE
);
2813 GtkWidget
*ui_label_new_bold(const gchar
*text
)
2818 label_text
= g_markup_escape_text(text
, -1);
2819 label
= gtk_label_new(NULL
);
2820 ui_label_set_markup(GTK_LABEL(label
), "<b>%s</b>", label_text
);
2826 /** Adds a list of document items to @a menu.
2828 * @param active Which document to highlight, or @c NULL.
2829 * @param callback is used for each menu item's @c "activate" signal and will be passed
2830 * the corresponding document pointer as @c user_data.
2831 * @warning You should check @c doc->is_valid in the callback.
2834 void ui_menu_add_document_items(GtkMenu
*menu
, GeanyDocument
*active
, GCallback callback
)
2836 ui_menu_add_document_items_sorted(menu
, active
, callback
, NULL
);
2840 /** Adds a list of document items to @a menu.
2842 * @a compare_func might be NULL to not sort the documents in the menu. In this case,
2843 * the order of the document tabs is used.
2845 * See document_compare_by_display_name() for an example sort function.
2848 * @param active Which document to highlight, or @c NULL.
2849 * @param callback is used for each menu item's @c "activate" signal and will be passed
2850 * the corresponding document pointer as @c user_data.
2851 * @param compare_func is used to sort the list. Might be @c NULL to not sort the list.
2852 * @warning You should check @c doc->is_valid in the callback.
2855 void ui_menu_add_document_items_sorted(GtkMenu
*menu
, GeanyDocument
*active
,
2856 GCallback callback
, GCompareFunc compare_func
)
2858 GtkWidget
*menu_item
, *menu_item_label
, *image
;
2861 gchar
*base_name
, *label
;
2862 GPtrArray
*sorted_documents
;
2864 len
= (guint
) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
));
2866 sorted_documents
= g_ptr_array_sized_new(len
);
2867 /* copy the documents_array into the new one */
2870 g_ptr_array_add(sorted_documents
, documents
[i
]);
2872 if (compare_func
== NULL
)
2873 compare_func
= document_compare_by_tab_order
;
2875 /* and now sort it */
2876 g_ptr_array_sort(sorted_documents
, compare_func
);
2878 for (i
= 0; i
< sorted_documents
->len
; i
++)
2880 doc
= g_ptr_array_index(sorted_documents
, i
);
2882 base_name
= g_path_get_basename(DOC_FILENAME(doc
));
2883 menu_item
= gtk_image_menu_item_new_with_label(base_name
);
2884 image
= gtk_image_new_from_gicon(doc
->file_type
->icon
, GTK_ICON_SIZE_MENU
);
2885 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item
), image
);
2887 gtk_widget_show(menu_item
);
2888 gtk_container_add(GTK_CONTAINER(menu
), menu_item
);
2889 g_signal_connect(menu_item
, "activate", callback
, doc
);
2891 menu_item_label
= gtk_bin_get_child(GTK_BIN(menu_item
));
2892 gtk_widget_set_name(menu_item_label
, document_get_status_widget_class(doc
));
2896 label
= g_markup_escape_text(base_name
, -1);
2897 ui_label_set_markup(GTK_LABEL(menu_item_label
), "<b>%s</b>", label
);
2903 g_ptr_array_free(sorted_documents
, TRUE
);
2907 /** Checks whether the passed @a keyval is the Enter or Return key.
2908 * There are three different Enter/Return key values
2909 * (@c GDK_Return, @c GDK_ISO_Enter, @c GDK_KP_Enter).
2910 * This is just a convenience function.
2911 * @param keyval A keyval.
2912 * @return @c TRUE if @a keyval is the one of the Enter/Return key values, otherwise @c FALSE.
2915 gboolean
ui_is_keyval_enter_or_return(guint keyval
)
2917 return (keyval
== GDK_Return
|| keyval
== GDK_ISO_Enter
|| keyval
== GDK_KP_Enter
);
2921 /** Reads an integer from the GTK default settings registry
2922 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
2923 * @param property_name The property to read.
2924 * @param default_value The default value in case the value could not be read.
2925 * @return The value for the property if it exists, otherwise the @a default_value.
2928 gint
ui_get_gtk_settings_integer(const gchar
*property_name
, gint default_value
)
2930 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
2931 gtk_settings_get_default())), property_name
))
2934 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name
, &value
, NULL
);
2938 return default_value
;
2942 void ui_editable_insert_text_callback(GtkEditable
*editable
, gchar
*new_text
,
2943 gint new_text_len
, gint
*position
, gpointer data
)
2945 gboolean first
= position
!= NULL
&& *position
== 0;
2948 if (new_text_len
== -1)
2949 new_text_len
= (gint
) strlen(new_text
);
2951 for (i
= 0; i
< new_text_len
; i
++, new_text
++)
2953 if ((!first
|| !strchr("+-", *new_text
)) && !isdigit(*new_text
))
2955 g_signal_stop_emission_by_name(editable
, "insert-text");
2963 /* gets the icon that applies to a particular MIME type */
2964 GIcon
*ui_get_mime_icon(const gchar
*mime_type
)
2969 ctype
= g_content_type_from_mime_type(mime_type
);
2972 icon
= g_content_type_get_icon(ctype
);
2975 GtkIconInfo
*icon_info
;
2977 icon_info
= gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), icon
, 16, 0);
2980 g_object_unref(icon
);
2984 gtk_icon_info_free(icon_info
);
2990 /* fallback if icon lookup failed, like it might happen on Windows (?) */
2993 const gchar
*icon_name
= "text-x-generic";
2995 if (strstr(mime_type
, "directory"))
2996 icon_name
= "folder";
2998 icon
= g_themed_icon_new(icon_name
);
3004 void ui_focus_current_document(void)
3006 GeanyDocument
*doc
= document_get_current();
3009 document_grab_focus(doc
);
3013 /** Finds the label text associated with stock_id
3014 * @param stock_id stock_id to lookup e.g. @c GTK_STOCK_OPEN.
3015 * @return The label text for stock
3016 * @since Geany 1.22 */
3018 const gchar
*ui_lookup_stock_label(const gchar
*stock_id
)
3022 if (gtk_stock_lookup(stock_id
, &item
))
3025 g_warning("No stock id '%s'!", stock_id
);
3030 /* finds the next iter at any level
3031 * @param iter in/out, the current iter, will be changed to the next one
3032 * @param down whether to try the child iter
3033 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
3034 gboolean
ui_tree_model_iter_any_next(GtkTreeModel
*model
, GtkTreeIter
*iter
, gboolean down
)
3037 GtkTreeIter copy
= *iter
;
3039 /* go down if the item has children */
3040 if (down
&& gtk_tree_model_iter_children(model
, &guess
, iter
))
3042 /* or to the next item at the same level */
3043 else if (gtk_tree_model_iter_next(model
, ©
))
3045 /* or to the next item at a parent level */
3046 else if (gtk_tree_model_iter_parent(model
, &guess
, iter
))
3051 if (gtk_tree_model_iter_next(model
, ©
))
3056 else if (gtk_tree_model_iter_parent(model
, ©
, &guess
))
3069 GtkWidget
*ui_create_encodings_combo_box(gboolean has_detect
, gint default_enc
)
3071 GtkCellRenderer
*renderer
;
3073 GtkWidget
*combo
= gtk_combo_box_new();
3074 GtkTreeStore
*store
= encodings_encoding_store_new(has_detect
);
3076 if (default_enc
< 0 || default_enc
>= GEANY_ENCODINGS_MAX
)
3077 default_enc
= has_detect
? GEANY_ENCODINGS_MAX
: GEANY_ENCODING_NONE
;
3079 gtk_combo_box_set_model(GTK_COMBO_BOX(combo
), GTK_TREE_MODEL(store
));
3080 if (encodings_encoding_store_get_iter(store
, &iter
, default_enc
))
3081 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo
), &iter
);
3082 renderer
= gtk_cell_renderer_text_new();
3083 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo
), renderer
, TRUE
);
3084 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo
), renderer
,
3085 encodings_encoding_store_cell_data_func
, NULL
, NULL
);
3091 gint
ui_encodings_combo_box_get_active_encoding(GtkComboBox
*combo
)
3094 gint enc
= GEANY_ENCODING_NONE
;
3096 /* there should always be an active iter anyway, but we check just in case */
3097 if (gtk_combo_box_get_active_iter(combo
, &iter
))
3099 GtkTreeModel
*model
= gtk_combo_box_get_model(combo
);
3100 enc
= encodings_encoding_store_get_encoding(GTK_TREE_STORE(model
), &iter
);
3107 gboolean
ui_encodings_combo_box_set_active_encoding(GtkComboBox
*combo
, gint enc
)
3110 GtkTreeModel
*model
= gtk_combo_box_get_model(combo
);
3112 if (encodings_encoding_store_get_iter(GTK_TREE_STORE(model
), &iter
, enc
))
3114 gtk_combo_box_set_active_iter(combo
, &iter
);