Update French translation
[geany-mirror.git] / src / callbacks.c
blob2e13d6d039ea66da53a630e962d81543d2810c5c
1 /*
2 * callbacks.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Callbacks used by Glade. These are mainly in response to menu item and button events in the
23 * main window. Callbacks not used by Glade should go elsewhere.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "callbacks.h"
32 #include "about.h"
33 #include "app.h"
34 #include "build.h"
35 #include "dialogs.h"
36 #include "documentprivate.h"
37 #include "encodings.h"
38 #include "filetypes.h"
39 #include "geanyobject.h"
40 #include "highlighting.h"
41 #include "keybindings.h"
42 #include "keyfile.h"
43 #include "log.h"
44 #include "main.h"
45 #include "msgwindow.h"
46 #include "navqueue.h"
47 #include "plugins.h"
48 #include "pluginutils.h"
49 #include "prefs.h"
50 #include "printing.h"
51 #include "sciwrappers.h"
52 #include "sidebar.h"
53 #include "spawn.h"
54 #ifdef HAVE_SOCKET
55 # include "socket.h"
56 #endif
57 #include "support.h"
58 #include "symbols.h"
59 #include "templates.h"
60 #include "toolbar.h"
61 #include "tools.h"
62 #include "ui_utils.h"
63 #include "utils.h"
64 #include "vte.h"
66 #include <stdlib.h>
67 #include <unistd.h>
68 #include <string.h>
69 #include <gtk/gtk.h>
70 #include <gdk/gdkkeysyms.h>
71 #include <glib/gstdio.h>
72 #include <time.h>
75 /* represents the state at switching a notebook page(in the left treeviews widget), to not emit
76 * the selection-changed signal from tv.tree_openfiles */
77 /*static gboolean switch_tv_notebook_page = FALSE; */
81 /* wrapper function to abort exit process if cancel button is pressed */
82 static gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event, gpointer gdata)
84 return !main_quit();
89 * GUI callbacks
92 void on_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
94 document_new_file(NULL, NULL, NULL);
98 /* create a new file and copy file content and properties */
99 static void on_clone1_activate(GtkMenuItem *menuitem, gpointer user_data)
101 GeanyDocument *old_doc = document_get_current();
103 if (old_doc)
104 document_clone(old_doc);
108 void on_save1_activate(GtkMenuItem *menuitem, gpointer user_data)
110 GeanyDocument *doc = document_get_current();
112 if (doc != NULL)
114 document_save_file(doc, ui_prefs.allow_always_save);
119 void on_save_as1_activate(GtkMenuItem *menuitem, gpointer user_data)
121 dialogs_show_save_as();
125 void on_save_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
127 guint i, max = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
128 GeanyDocument *cur_doc = document_get_current();
129 guint count = 0;
131 /* iterate over documents in tabs order */
132 for (i = 0; i < max; i++)
134 GeanyDocument *doc = document_get_from_page(i);
136 if (! doc->changed)
137 continue;
139 if (document_save_file(doc, FALSE))
140 count++;
142 if (!count)
143 return;
145 ui_set_statusbar(FALSE, ngettext("%d file saved.", "%d files saved.", count), count);
146 /* saving may have changed window title, sidebar for another doc, so update */
147 document_show_tab(cur_doc);
148 sidebar_update_tag_list(cur_doc, TRUE);
149 ui_set_window_title(cur_doc);
153 void on_close_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
155 document_close_all();
159 void on_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
161 GeanyDocument *doc = document_get_current();
163 if (doc != NULL)
164 document_close(doc);
168 void on_quit1_activate(GtkMenuItem *menuitem, gpointer user_data)
170 main_quit();
174 static void on_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
176 gtk_widget_set_sensitive(ui_widgets.recent_files_menuitem,
177 g_queue_get_length(ui_prefs.recent_queue) > 0);
178 /* hide Page setup when GTK printing is not used */
179 ui_widget_show_hide(ui_widgets.print_page_setup, printing_prefs.use_gtk_printing);
183 /* edit actions, c&p & co, from menu bar and from popup menu */
184 static void on_edit1_select(GtkMenuItem *menuitem, gpointer user_data)
186 GtkWidget *item;
187 GeanyDocument *doc = document_get_current();
189 ui_update_menu_copy_items(doc);
190 ui_update_insert_include_item(doc, 1);
192 item = ui_lookup_widget(main_widgets.window, "plugin_preferences1");
193 #ifndef HAVE_PLUGINS
194 gtk_widget_hide(item);
195 #else
196 gtk_widget_set_sensitive(item, plugins_have_preferences());
197 #endif
201 static void on_edit1_deselect(GtkMenuShell *menushell, gpointer user_data)
203 /* we re-enable items that were disabled in on_edit1_select() on menu popdown to
204 * workaround mutli-layout keyboard issues in our keybinding handling code, so that
205 * GTK's accelerator handling can catch them.
206 * See https://github.com/geany/geany/issues/1368#issuecomment-273678207 */
207 ui_menu_copy_items_set_sensitive(TRUE);
211 void on_undo1_activate(GtkMenuItem *menuitem, gpointer user_data)
213 GeanyDocument *doc = document_get_current();
215 g_return_if_fail(doc != NULL);
217 if (document_can_undo(doc))
219 sci_cancel(doc->editor->sci);
220 document_undo(doc);
225 void on_redo1_activate(GtkMenuItem *menuitem, gpointer user_data)
227 GeanyDocument *doc = document_get_current();
229 g_return_if_fail(doc != NULL);
231 if (document_can_redo(doc))
233 sci_cancel(doc->editor->sci);
234 document_redo(doc);
239 void on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
241 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
243 if (GTK_IS_EDITABLE(focusw))
244 gtk_editable_cut_clipboard(GTK_EDITABLE(focusw));
245 else if (IS_SCINTILLA(focusw))
246 sci_cut(SCINTILLA(focusw));
247 else if (GTK_IS_TEXT_VIEW(focusw))
249 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
250 GTK_TEXT_VIEW(focusw));
251 gtk_text_buffer_cut_clipboard(buffer, gtk_clipboard_get(GDK_NONE), TRUE);
256 void on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
258 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
260 if (GTK_IS_EDITABLE(focusw))
261 gtk_editable_copy_clipboard(GTK_EDITABLE(focusw));
262 else if (IS_SCINTILLA(focusw))
263 sci_copy(SCINTILLA(focusw));
264 else if (GTK_IS_TEXT_VIEW(focusw))
266 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
267 GTK_TEXT_VIEW(focusw));
268 gtk_text_buffer_copy_clipboard(buffer, gtk_clipboard_get(GDK_NONE));
273 void on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
275 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
277 if (GTK_IS_EDITABLE(focusw))
278 gtk_editable_paste_clipboard(GTK_EDITABLE(focusw));
279 else if (IS_SCINTILLA(focusw))
280 sci_paste(SCINTILLA(focusw));
281 else if (GTK_IS_TEXT_VIEW(focusw))
283 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
284 GTK_TEXT_VIEW(focusw));
285 gtk_text_buffer_paste_clipboard(buffer, gtk_clipboard_get(GDK_NONE), NULL,
286 TRUE);
291 void on_delete1_activate(GtkMenuItem *menuitem, gpointer user_data)
293 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
295 if (GTK_IS_EDITABLE(focusw))
296 gtk_editable_delete_selection(GTK_EDITABLE(focusw));
297 else if (IS_SCINTILLA(focusw) && sci_has_selection(SCINTILLA(focusw)))
298 sci_clear(SCINTILLA(focusw));
299 else if (GTK_IS_TEXT_VIEW(focusw))
301 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
302 GTK_TEXT_VIEW(focusw));
303 gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
308 void on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
310 prefs_show_dialog();
314 /* about menu item */
315 static void on_info1_activate(GtkMenuItem *menuitem, gpointer user_data)
317 about_dialog_show();
321 /* open file */
322 void on_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
324 dialogs_show_open_file();
328 /* reload file */
329 void on_toolbutton_reload_clicked(GtkAction *action, gpointer user_data)
331 GeanyDocument *doc = document_get_current();
333 g_return_if_fail(doc != NULL);
335 document_reload_prompt(doc, NULL);
338 /* reload all files */
339 void on_reload_all(GtkAction *action, gpointer user_data)
341 guint i;
342 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
344 if (!file_prefs.keep_edit_history_on_reload)
346 GeanyDocument *doc;
347 foreach_document(i)
349 doc = documents[i];
350 if (doc->changed || document_can_undo(doc) || document_can_redo(doc))
352 if (dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
353 _("Changes detected, reloading all will lose any changes and history."),
354 _("Are you sure you want to reload all files?")))
356 break; // break the loop and continue with reloading below
358 else
360 return; // cancel reloading
366 foreach_document(i)
368 if (! (documents[i]->file_name == NULL))
369 document_reload_force(documents[i], documents[i]->encoding);
372 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), cur_page);
376 static void on_change_font1_activate(GtkMenuItem *menuitem, gpointer user_data)
378 dialogs_show_open_font();
382 /* store text, clear search flags so we can use Search->Find Next/Previous */
383 static void setup_find(const gchar *text, gboolean backwards)
385 SETPTR(search_data.text, g_strdup(text));
386 SETPTR(search_data.original_text, g_strdup(text));
387 search_data.flags = 0;
388 search_data.backwards = backwards;
389 search_data.search_bar = TRUE;
393 static void do_toolbar_search(const gchar *text, gboolean incremental, gboolean backwards)
395 GeanyDocument *doc = document_get_current();
396 gboolean result;
398 setup_find(text, backwards);
399 result = document_search_bar_find(doc, search_data.text, incremental, backwards);
400 if (search_data.search_bar)
401 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
405 /* search text */
406 void on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
408 do_toolbar_search(text, TRUE, FALSE);
412 /* search text */
413 void on_toolbar_search_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
415 do_toolbar_search(text, FALSE, GPOINTER_TO_INT(user_data));
419 /* search text */
420 void on_toolbutton_search_clicked(GtkAction *action, gpointer user_data)
422 GeanyDocument *doc = document_get_current();
423 gboolean result;
424 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
426 if (entry != NULL)
428 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
430 setup_find(text, FALSE);
431 result = document_search_bar_find(doc, search_data.text, FALSE, FALSE);
432 if (search_data.search_bar)
433 ui_set_search_entry_background(entry, result);
435 else
436 on_find1_activate(NULL, NULL);
440 /* hides toolbar from toolbar popup menu */
441 static void on_hide_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
443 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
444 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
448 /* zoom in from menu bar and popup menu */
449 void on_zoom_in1_activate(GtkMenuItem *menuitem, gpointer user_data)
451 GeanyDocument *doc = document_get_current();
453 g_return_if_fail(doc != NULL);
455 sci_zoom_in(doc->editor->sci);
459 /* zoom out from menu bar and popup menu */
460 void on_zoom_out1_activate(GtkMenuItem *menuitem, gpointer user_data)
462 GeanyDocument *doc = document_get_current();
464 g_return_if_fail(doc != NULL);
466 sci_zoom_out(doc->editor->sci);
470 void on_normal_size1_activate(GtkMenuItem *menuitem, gpointer user_data)
472 GeanyDocument *doc = document_get_current();
474 g_return_if_fail(doc != NULL);
476 sci_zoom_off(doc->editor->sci);
480 /* Changes window-title after switching tabs and lots of other things.
481 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
482 static void on_notebook1_switch_page_after(GtkNotebook *notebook, gpointer page,
483 guint page_num, gpointer user_data)
485 GeanyDocument *doc;
487 if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
488 return;
490 doc = document_get_from_notebook_child(page);
492 if (doc != NULL)
494 sidebar_select_openfiles_item(doc);
495 ui_save_buttons_toggle(doc->changed);
496 ui_set_window_title(doc);
497 ui_update_statusbar(doc, -1);
498 ui_update_popup_reundo_items(doc);
499 ui_document_show_hide(doc); /* update the document menu */
500 build_menu_update(doc);
501 sidebar_update_tag_list(doc, FALSE);
502 document_highlight_tags(doc);
504 document_check_disk_status(doc, TRUE);
506 #ifdef HAVE_VTE
507 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
508 #endif
510 g_signal_emit_by_name(geany_object, "document-activate", doc);
515 static void on_tv_notebook_switch_page(GtkNotebook *notebook, gpointer page,
516 guint page_num, gpointer user_data)
518 /* suppress selection changed signal when switching to the open files list */
519 ignore_callback = TRUE;
523 static void on_tv_notebook_switch_page_after(GtkNotebook *notebook, gpointer page,
524 guint page_num, gpointer user_data)
526 ignore_callback = FALSE;
530 static void convert_eol(gint mode)
532 GeanyDocument *doc = document_get_current();
534 g_return_if_fail(doc != NULL);
536 /* sci_convert_eols() adds UNDO_SCINTILLA action in on_editor_notify().
537 * It is added to the undo stack before sci_convert_eols() finishes
538 * so after adding UNDO_EOL, UNDO_EOL will be at the top of the stack
539 * and UNDO_SCINTILLA below it. */
540 sci_convert_eols(doc->editor->sci, mode);
541 document_undo_add(doc, UNDO_EOL, GINT_TO_POINTER(sci_get_eol_mode(doc->editor->sci)));
543 sci_set_eol_mode(doc->editor->sci, mode);
545 ui_update_statusbar(doc, -1);
549 static void on_crlf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
551 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
552 return;
554 convert_eol(SC_EOL_CRLF);
558 static void on_lf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
560 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
561 return;
563 convert_eol(SC_EOL_LF);
567 static void on_cr_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
569 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
570 return;
572 convert_eol(SC_EOL_CR);
576 void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
578 GeanyDocument *doc = document_get_current();
580 g_return_if_fail(doc != NULL);
582 editor_replace_tabs(doc->editor, FALSE);
586 gboolean toolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
588 if (event->button == 3)
590 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
591 return TRUE;
593 return FALSE;
597 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
599 GeanyDocument *doc = document_get_current();
600 ScintillaObject *sci;
601 gboolean keep_sel = TRUE;
603 g_return_if_fail(doc != NULL);
605 sci = doc->editor->sci;
606 if (! sci_has_selection(sci))
608 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
609 keep_sel = FALSE;
612 /* either we already had a selection or we created one for current word */
613 if (sci_has_selection(sci))
615 gchar *result = NULL;
616 gint cmd = SCI_LOWERCASE;
617 gboolean rectsel = (gboolean) SSM(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
618 gchar *text = sci_get_selection_contents(sci);
620 if (utils_str_has_upper(text))
622 if (rectsel)
623 cmd = SCI_LOWERCASE;
624 else
625 result = g_utf8_strdown(text, -1);
627 else
629 if (rectsel)
630 cmd = SCI_UPPERCASE;
631 else
632 result = g_utf8_strup(text, -1);
635 if (result != NULL)
637 sci_replace_sel(sci, result);
638 g_free(result);
639 if (keep_sel)
640 sci_set_selection_start(sci, sci_get_current_position(sci) - strlen(text));
642 else
643 sci_send_command(sci, cmd);
645 g_free(text);
650 static void on_show_toolbar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
652 if (ignore_callback) return;
654 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
655 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
659 static void on_fullscreen1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
661 if (ignore_callback)
662 return;
664 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
665 ui_set_fullscreen();
669 static void on_show_messages_window1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
671 if (ignore_callback)
672 return;
674 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
675 msgwin_show_hide(ui_prefs.msgwindow_visible);
679 static void on_menu_color_schemes_activate(GtkImageMenuItem *imagemenuitem, gpointer user_data)
681 highlighting_show_color_scheme_dialog();
685 static void on_markers_margin1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
687 if (ignore_callback)
688 return;
690 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
691 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
695 static void on_show_line_numbers1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
697 if (ignore_callback)
698 return;
700 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
701 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
705 static void on_menu_show_white_space1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
707 if (ignore_callback)
708 return;
710 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
711 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
715 static void on_menu_show_line_endings1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
717 if (ignore_callback)
718 return;
720 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
721 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
725 static void on_menu_show_indentation_guides1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
727 if (ignore_callback)
728 return;
730 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
731 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
735 void on_line_wrapping1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
737 if (! ignore_callback)
739 GeanyDocument *doc = document_get_current();
740 g_return_if_fail(doc != NULL);
742 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
747 static void on_set_file_readonly1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
749 if (! ignore_callback)
751 GeanyDocument *doc = document_get_current();
752 g_return_if_fail(doc != NULL);
754 doc->readonly = ! doc->readonly;
755 sci_set_readonly(doc->editor->sci, doc->readonly);
756 ui_update_tab_status(doc);
757 ui_update_statusbar(doc, -1);
762 static void on_use_auto_indentation1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
764 if (! ignore_callback)
766 GeanyDocument *doc = document_get_current();
767 g_return_if_fail(doc != NULL);
769 doc->editor->auto_indent = ! doc->editor->auto_indent;
774 static void find_usage(gboolean in_session)
776 GeanyFindFlags flags;
777 gchar *search_text;
778 GeanyDocument *doc = document_get_current();
780 g_return_if_fail(doc != NULL);
782 if (sci_has_selection(doc->editor->sci))
783 { /* take selected text if there is a selection */
784 search_text = sci_get_selection_contents(doc->editor->sci);
785 flags = GEANY_FIND_MATCHCASE;
787 else
789 editor_find_current_word_sciwc(doc->editor, -1,
790 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
791 search_text = g_strdup(editor_info.current_word);
792 flags = GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD;
795 search_find_usage(search_text, search_text, flags, in_session);
796 g_free(search_text);
800 void on_find_document_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
802 find_usage(FALSE);
806 void on_find_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
808 find_usage(TRUE);
812 static void goto_tag(gboolean definition)
814 GeanyDocument *doc = document_get_current();
816 g_return_if_fail(doc != NULL);
818 /* update cursor pos for navigating back afterwards */
819 if (!sci_has_selection(doc->editor->sci))
820 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
822 /* use the keybinding callback as it checks for selections as well as current word */
823 if (definition)
824 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
825 else
826 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
830 static void on_goto_tag_definition1(GtkMenuItem *menuitem, gpointer user_data)
832 goto_tag(TRUE);
836 static void on_goto_tag_declaration1(GtkMenuItem *menuitem, gpointer user_data)
838 goto_tag(FALSE);
842 static void on_count_words1_activate(GtkMenuItem *menuitem, gpointer user_data)
844 tools_word_count();
848 void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data)
850 gchar colour[9];
851 GeanyDocument *doc = document_get_current();
852 gint pos;
854 g_return_if_fail(doc != NULL);
856 pos = sci_get_current_position(doc->editor->sci);
857 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
858 tools_color_chooser(colour);
862 void on_toolbutton_compile_clicked(GtkAction *action, gpointer user_data)
864 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
868 void on_find1_activate(GtkMenuItem *menuitem, gpointer user_data)
870 search_show_find_dialog();
874 void on_find_next1_activate(GtkMenuItem *menuitem, gpointer user_data)
876 search_find_again(FALSE);
880 void on_find_previous1_activate(GtkMenuItem *menuitem, gpointer user_data)
882 if (search_data.flags & GEANY_FIND_REGEXP)
883 /* Can't reverse search order for a regex (find next ignores search backwards) */
884 utils_beep();
885 else
886 search_find_again(TRUE);
890 void on_find_nextsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
892 search_find_selection(document_get_current(), FALSE);
896 void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
898 search_find_selection(document_get_current(), TRUE);
902 void on_replace1_activate(GtkMenuItem *menuitem, gpointer user_data)
904 search_show_replace_dialog();
908 void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
910 search_show_find_in_files_dialog(NULL);
914 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
916 if (*text == '+' || *text == '-')
918 *line_no = atoi(text + 1);
919 *offset = (*text == '+') ? 1 : -1;
921 else
923 *line_no = atoi(text) - 1;
924 *offset = 0;
929 void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
931 static gchar value[16] = "";
932 gchar *result;
934 result = dialogs_show_input_goto_line(
935 _("Go to Line"), GTK_WINDOW(main_widgets.window),
936 _("Enter the line you want to go to:"), value);
937 if (result != NULL)
939 GeanyDocument *doc = document_get_current();
940 gint offset;
941 gint line_no;
943 g_return_if_fail(doc != NULL);
945 get_line_and_offset_from_text(result, &line_no, &offset);
946 if (! editor_goto_line(doc->editor, line_no, offset))
947 utils_beep();
948 /* remember value for future calls */
949 g_snprintf(value, sizeof(value), "%s", result);
951 g_free(result);
956 void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
958 GeanyDocument *doc = document_get_current();
959 gint offset;
960 gint line_no;
962 g_return_if_fail(doc != NULL);
964 get_line_and_offset_from_text(text, &line_no, &offset);
965 if (! editor_goto_line(doc->editor, line_no, offset))
966 utils_beep();
967 else
968 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
972 void on_toolbutton_goto_clicked(GtkAction *action, gpointer user_data)
974 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
976 if (entry != NULL)
978 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
980 on_toolbutton_goto_entry_activate(NULL, text, NULL);
982 else
983 on_go_to_line_activate(NULL, NULL);
987 void on_help1_activate(GtkMenuItem *menuitem, gpointer user_data)
989 gchar *uri;
991 uri = utils_get_help_url(NULL);
992 utils_open_browser(uri);
993 g_free(uri);
997 static void on_help_shortcuts1_activate(GtkMenuItem *menuitem, gpointer user_data)
999 keybindings_show_shortcuts();
1003 static void on_website1_activate(GtkMenuItem *menuitem, gpointer user_data)
1005 utils_open_browser(GEANY_HOMEPAGE);
1009 static void on_help_menu_item_donate_activate(GtkMenuItem *item, gpointer user_data)
1011 utils_open_browser(GEANY_DONATE);
1015 static void on_help_menu_item_wiki_activate(GtkMenuItem *item, gpointer user_data)
1017 utils_open_browser(GEANY_WIKI);
1021 static void on_help_menu_item_bug_report_activate(GtkMenuItem *item, gpointer user_data)
1023 utils_open_browser(GEANY_BUG_REPORT);
1027 static void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data)
1029 GeanyDocument *doc = document_get_current();
1030 gchar *text;
1031 const gchar *cur_tag = NULL;
1032 gint line = -1, pos = 0;
1034 if (doc == NULL || doc->file_type == NULL)
1036 ui_set_statusbar(FALSE,
1037 _("Please set the filetype for the current file before using this function."));
1038 return;
1041 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
1042 * returns the current position, so it should be safe */
1043 line = symbols_get_current_function(doc, &cur_tag);
1044 pos = sci_get_position_from_line(doc->editor->sci, line);
1046 text = templates_get_template_function(doc, cur_tag);
1048 sci_start_undo_action(doc->editor->sci);
1049 sci_insert_text(doc->editor->sci, pos, text);
1050 sci_end_undo_action(doc->editor->sci);
1051 g_free(text);
1055 static void insert_multiline_comment(GeanyDocument *doc, gint pos)
1057 g_return_if_fail(doc != NULL);
1058 g_return_if_fail(pos == -1 || pos >= 0);
1060 if (doc->file_type == NULL)
1062 ui_set_statusbar(FALSE,
1063 _("Please set the filetype for the current file before using this function."));
1064 return;
1067 if (doc->file_type->comment_open || doc->file_type->comment_single)
1069 /* editor_insert_multiline_comment() uses editor_info.click_pos */
1070 if (pos == -1)
1071 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
1072 else
1073 editor_info.click_pos = pos;
1074 editor_insert_multiline_comment(doc->editor);
1076 else
1077 utils_beep();
1081 static void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1083 insert_multiline_comment(document_get_current(), editor_info.click_pos);
1087 static void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1089 insert_multiline_comment(document_get_current(), -1);
1093 static void insert_comment_template(GeanyDocument *doc, gint pos, guint template)
1095 gchar *text;
1097 g_return_if_fail(doc != NULL);
1098 g_return_if_fail(pos == -1 || pos >= 0);
1099 g_return_if_fail(template < GEANY_MAX_TEMPLATES);
1101 if (pos == -1)
1102 pos = sci_get_current_position(doc->editor->sci);
1104 text = templates_get_template_licence(doc, template);
1106 sci_start_undo_action(doc->editor->sci);
1107 sci_insert_text(doc->editor->sci, pos, text);
1108 sci_end_undo_action(doc->editor->sci);
1109 g_free(text);
1113 static void on_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1115 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_GPL);
1119 static void on_menu_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1121 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_GPL);
1125 static void on_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1127 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_BSD);
1131 static void on_menu_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1133 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_BSD);
1137 static void on_comments_changelog_activate(GtkMenuItem *menuitem, gpointer user_data)
1139 GeanyDocument *doc = document_get_current();
1140 gchar *text;
1142 g_return_if_fail(doc != NULL);
1144 text = templates_get_template_changelog(doc);
1145 sci_start_undo_action(doc->editor->sci);
1146 sci_insert_text(doc->editor->sci, 0, text);
1147 /* sets the cursor to the right position to type the changelog text,
1148 * the template has 21 chars + length of name and email */
1149 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1150 sci_end_undo_action(doc->editor->sci);
1152 g_free(text);
1156 static void on_comments_fileheader_activate(GtkMenuItem *menuitem, gpointer user_data)
1158 GeanyDocument *doc = document_get_current();
1159 gchar *text;
1160 const gchar *fname;
1161 GeanyFiletype *ft;
1163 g_return_if_fail(doc != NULL);
1165 ft = doc->file_type;
1166 fname = doc->file_name;
1167 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1169 sci_start_undo_action(doc->editor->sci);
1170 sci_insert_text(doc->editor->sci, 0, text);
1171 sci_goto_pos(doc->editor->sci, 0, FALSE);
1172 sci_end_undo_action(doc->editor->sci);
1173 g_free(text);
1177 void on_file_properties_activate(GtkMenuItem *menuitem, gpointer user_data)
1179 GeanyDocument *doc = document_get_current();
1180 g_return_if_fail(doc != NULL);
1182 dialogs_show_file_properties(doc);
1186 static void on_menu_fold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1188 GeanyDocument *doc = document_get_current();
1189 g_return_if_fail(doc != NULL);
1191 editor_fold_all(doc->editor);
1195 static void on_menu_unfold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1197 GeanyDocument *doc = document_get_current();
1198 g_return_if_fail(doc != NULL);
1200 editor_unfold_all(doc->editor);
1204 void on_toolbutton_run_clicked(GtkAction *action, gpointer user_data)
1206 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1210 void on_menu_remove_indicators1_activate(GtkMenuItem *menuitem, gpointer user_data)
1212 GeanyDocument *doc = document_get_current();
1213 g_return_if_fail(doc != NULL);
1215 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1219 void on_print1_activate(GtkMenuItem *menuitem, gpointer user_data)
1221 GeanyDocument *doc = document_get_current();
1222 g_return_if_fail(doc != NULL);
1224 printing_print_doc(doc);
1228 void on_menu_select_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1230 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1232 /* special case for Select All in the scribble widget */
1233 if (GTK_IS_TEXT_VIEW(focusw))
1235 g_signal_emit_by_name(focusw, "select-all", TRUE);
1237 /* special case for Select All in the VTE widget */
1238 #ifdef HAVE_VTE
1239 else if (vte_info.have_vte && focusw == vc->vte)
1241 vte_select_all();
1243 #endif
1244 else if (GTK_IS_EDITABLE(focusw))
1246 gtk_editable_select_region(GTK_EDITABLE(focusw), 0, -1);
1248 else if (IS_SCINTILLA(focusw))
1250 sci_select_all(SCINTILLA(focusw));
1255 void on_menu_show_sidebar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1257 if (ignore_callback)
1258 return;
1260 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1262 /* show built-in tabs if no tabs visible */
1263 if (ui_prefs.sidebar_visible &&
1264 ! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1265 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1267 interface_prefs.sidebar_openfiles_visible = TRUE;
1268 interface_prefs.sidebar_symbol_visible = TRUE;
1271 /* if window has input focus, set it back to the editor before toggling off */
1272 if (! ui_prefs.sidebar_visible &&
1273 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1275 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1278 ui_sidebar_show_hide();
1282 static void on_menu_write_unicode_bom1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1284 if (! ignore_callback)
1286 GeanyDocument *doc = document_get_current();
1288 g_return_if_fail(doc != NULL);
1289 if (doc->readonly)
1291 utils_beep();
1292 return;
1295 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1297 doc->has_bom = ! doc->has_bom;
1299 ui_update_statusbar(doc, -1);
1304 void on_menu_comment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1306 GeanyDocument *doc = document_get_current();
1307 g_return_if_fail(doc != NULL);
1309 editor_do_comment(doc->editor, -1, FALSE, FALSE, TRUE);
1313 void on_menu_uncomment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1315 GeanyDocument *doc = document_get_current();
1316 g_return_if_fail(doc != NULL);
1318 editor_do_uncomment(doc->editor, -1, FALSE);
1322 void on_menu_toggle_line_commentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1324 GeanyDocument *doc = document_get_current();
1325 g_return_if_fail(doc != NULL);
1327 editor_do_comment_toggle(doc->editor);
1331 void on_menu_increase_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1333 GeanyDocument *doc = document_get_current();
1334 g_return_if_fail(doc != NULL);
1336 editor_indent(doc->editor, TRUE);
1340 void on_menu_decrease_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1342 GeanyDocument *doc = document_get_current();
1343 g_return_if_fail(doc != NULL);
1345 editor_indent(doc->editor, FALSE);
1349 void on_next_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1351 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1352 msgwin_goto_messages_file_line))
1353 ui_set_statusbar(FALSE, _("No more message items."));
1357 void on_previous_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1359 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1360 msgwin_goto_messages_file_line))
1361 ui_set_statusbar(FALSE, _("No more message items."));
1365 void on_project_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
1367 project_new();
1371 void on_project_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
1373 project_open();
1377 void on_project_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
1379 project_close(TRUE);
1383 void on_project_properties1_activate(GtkMenuItem *menuitem, gpointer user_data)
1385 project_properties();
1389 static void on_menu_project1_activate(GtkMenuItem *menuitem, gpointer user_data)
1391 static GtkWidget *item_close = NULL;
1392 static GtkWidget *item_properties = NULL;
1394 if (item_close == NULL)
1396 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1397 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1400 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1401 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1402 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1403 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1407 void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
1409 GeanyDocument *doc = document_get_current();
1410 gchar *sel = NULL;
1411 const gchar *wc;
1413 #ifdef G_OS_WIN32
1414 wc = GEANY_WORDCHARS "./-" "\\";
1415 #else
1416 wc = GEANY_WORDCHARS "./-";
1417 #endif
1419 g_return_if_fail(doc != NULL);
1421 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1422 SETPTR(sel, utils_get_locale_from_utf8(sel));
1424 if (sel != NULL)
1426 gchar *filename = NULL;
1428 if (g_path_is_absolute(sel))
1429 filename = g_strdup(sel);
1430 else
1431 { /* relative filename, add the path of the current file */
1432 gchar *path;
1434 path = utils_get_current_file_dir_utf8();
1435 SETPTR(path, utils_get_locale_from_utf8(path));
1436 if (!path)
1437 path = g_get_current_dir();
1439 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1441 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1442 app->project != NULL && !EMPTY(app->project->base_path))
1444 /* try the project's base path */
1445 SETPTR(path, project_get_base_path());
1446 SETPTR(path, utils_get_locale_from_utf8(path));
1447 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1449 g_free(path);
1450 #ifdef G_OS_UNIX
1451 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1452 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));
1454 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1455 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
1456 #endif
1459 if (g_file_test(filename, G_FILE_TEST_EXISTS))
1460 document_open_file(filename, FALSE, NULL, NULL);
1461 else
1463 SETPTR(sel, utils_get_utf8_from_locale(sel));
1464 ui_set_statusbar(TRUE, _("Could not open file %s (File not found)"), sel);
1467 g_free(filename);
1468 g_free(sel);
1473 void on_remove_markers1_activate(GtkMenuItem *menuitem, gpointer user_data)
1475 GeanyDocument *doc = document_get_current();
1476 g_return_if_fail(doc != NULL);
1478 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1479 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1480 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1484 static void on_load_tags1_activate(GtkMenuItem *menuitem, gpointer user_data)
1486 symbols_show_load_tags_dialog();
1490 void on_context_action1_activate(GtkMenuItem *menuitem, gpointer user_data)
1492 gchar *word, *command;
1493 GError *error = NULL;
1494 GeanyDocument *doc = document_get_current();
1495 const gchar *check_msg;
1497 g_return_if_fail(doc != NULL);
1499 if (sci_has_selection(doc->editor->sci))
1500 { /* take selected text if there is a selection */
1501 word = sci_get_selection_contents(doc->editor->sci);
1503 else
1505 word = g_strdup(editor_info.current_word);
1508 /* use the filetype specific command if available, fallback to global command otherwise */
1509 if (doc->file_type != NULL &&
1510 !EMPTY(doc->file_type->context_action_cmd))
1512 command = g_strdup(doc->file_type->context_action_cmd);
1513 check_msg = _("Check the path setting in Filetype configuration.");
1515 else
1517 command = g_strdup(tool_prefs.context_action_cmd);
1518 check_msg = _("Check the path setting in Preferences.");
1521 /* substitute the wildcard %s and run the command if it is non empty */
1522 if (G_LIKELY(!EMPTY(command)))
1524 gchar *command_line = g_strdup(command);
1526 utils_str_replace_all(&command_line, "%s", word);
1528 if (!spawn_async(NULL, command_line, NULL, NULL, NULL, &error))
1530 /* G_SHELL_ERROR is parsing error, it may be caused by %s word with quotes */
1531 ui_set_statusbar(TRUE, _("Cannot execute context action command \"%s\": %s. %s"),
1532 error->domain == G_SHELL_ERROR ? command_line : command, error->message,
1533 check_msg);
1534 g_error_free(error);
1536 g_free(command_line);
1538 else
1540 ui_set_statusbar(TRUE, _("No context action set."));
1542 g_free(word);
1543 g_free(command);
1547 void on_menu_toggle_all_additional_widgets1_activate(GtkMenuItem *menuitem, gpointer user_data)
1549 static gint hide_all = -1;
1550 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1551 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1552 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1553 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1555 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1556 if (G_UNLIKELY(hide_all == -1))
1558 if (! gtk_check_menu_item_get_active(msgw) &&
1559 ! interface_prefs.show_notebook_tabs &&
1560 ! gtk_check_menu_item_get_active(toolbari))
1562 hide_all = TRUE;
1564 else
1565 hide_all = FALSE;
1568 hide_all = ! hide_all; /* toggle */
1570 if (hide_all)
1572 if (gtk_check_menu_item_get_active(msgw))
1573 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1575 interface_prefs.show_notebook_tabs = FALSE;
1576 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1578 ui_statusbar_showhide(FALSE);
1580 if (gtk_check_menu_item_get_active(toolbari))
1581 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1583 else
1586 if (! gtk_check_menu_item_get_active(msgw))
1587 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1589 interface_prefs.show_notebook_tabs = TRUE;
1590 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1592 ui_statusbar_showhide(TRUE);
1594 if (! gtk_check_menu_item_get_active(toolbari))
1595 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1600 void on_toolbutton_forward_activate(GtkAction *menuitem, gpointer user_data)
1602 navqueue_go_forward();
1606 void on_toolbutton_back_activate(GtkAction *menuitem, gpointer user_data)
1608 navqueue_go_back();
1612 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1614 if (prefs.auto_focus && ! gtk_widget_has_focus(widget))
1615 gtk_widget_grab_focus(widget);
1617 return FALSE;
1621 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1623 GeanyDocument *doc;
1625 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1626 return;
1628 doc = document_get_current();
1629 g_return_if_fail(doc != NULL);
1631 editor_set_indent(doc->editor, type, doc->editor->indent_width);
1632 ui_update_statusbar(doc, -1);
1636 static void on_tabs1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1638 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1642 static void on_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1644 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
1648 static void on_tabs_and_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1650 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
1654 static void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer user_data)
1656 GeanyDocument *doc;
1658 if (ignore_callback)
1659 return;
1661 doc = document_get_current();
1662 g_return_if_fail(doc != NULL);
1664 editor_strip_trailing_spaces(doc->editor, FALSE);
1668 static void on_page_setup1_activate(GtkMenuItem *menuitem, gpointer user_data)
1670 printing_page_setup_gtk();
1674 gboolean on_escape_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1676 guint state = keybindings_get_modifiers(event->state);
1678 /* make pressing escape in the sidebar and toolbar focus the editor */
1679 if (event->keyval == GDK_KEY_Escape && state == 0)
1681 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1682 return TRUE;
1684 return FALSE;
1688 void on_line_breaking1_activate(GtkMenuItem *menuitem, gpointer user_data)
1690 GeanyDocument *doc;
1692 if (ignore_callback)
1693 return;
1695 doc = document_get_current();
1696 g_return_if_fail(doc != NULL);
1698 doc->editor->line_breaking = !doc->editor->line_breaking;
1702 void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
1704 GeanyDocument *doc = document_get_current();
1706 g_return_if_fail(doc != NULL);
1708 editor_replace_spaces(doc->editor, FALSE);
1712 static void on_search1_activate(GtkMenuItem *menuitem, gpointer user_data)
1714 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
1715 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
1716 gboolean have_messages;
1718 /* enable commands if the messages window has any items */
1719 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
1720 NULL) > 0;
1722 gtk_widget_set_sensitive(next_message, have_messages);
1723 gtk_widget_set_sensitive(previous_message, have_messages);
1727 /* simple implementation (vs. close all which doesn't close documents if cancelled),
1728 * if user_data is set, it is the GeanyDocument to keep */
1729 void on_close_other_documents1_activate(GtkMenuItem *menuitem, gpointer user_data)
1731 guint i;
1732 GeanyDocument *cur_doc = user_data;
1734 if (cur_doc == NULL)
1735 cur_doc = document_get_current();
1737 for (i = 0; i < documents_array->len; i++)
1739 GeanyDocument *doc = documents[i];
1741 if (doc == cur_doc || ! doc->is_valid)
1742 continue;
1744 if (! document_close(doc))
1745 break;
1750 static void on_menu_reload_configuration1_activate(GtkMenuItem *menuitem, gpointer user_data)
1752 main_reload_configuration();
1756 static void on_debug_messages1_activate(GtkMenuItem *menuitem, gpointer user_data)
1758 log_show_debug_messages_dialog();
1762 void on_send_selection_to_vte1_activate(GtkMenuItem *menuitem, gpointer user_data)
1764 #ifdef HAVE_VTE
1765 if (vte_info.have_vte)
1766 vte_send_selection_to_vte();
1767 #endif
1771 static gboolean on_window_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
1774 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1776 static GtkWidget *menuitem = NULL;
1778 if (menuitem == NULL)
1779 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
1781 ignore_callback = TRUE;
1783 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
1784 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
1786 ignore_callback = FALSE;
1788 return FALSE;
1792 static void show_notebook_page(const gchar *notebook_name, const gchar *page_name)
1794 GtkWidget *widget;
1795 GtkNotebook *notebook;
1797 widget = ui_lookup_widget(ui_widgets.prefs_dialog, page_name);
1798 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, notebook_name));
1800 if (notebook != NULL && widget != NULL)
1801 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
1805 static void on_customize_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
1807 prefs_show_dialog();
1809 /* select the Interface page */
1810 show_notebook_page("notebook2", "notebook6");
1811 /* select the Toolbar subpage */
1812 show_notebook_page("notebook6", "vbox15");
1816 static void on_button_customize_toolbar_clicked(GtkButton *button, gpointer user_data)
1818 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
1822 static void on_cut_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1824 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
1828 static void on_copy_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1830 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
1834 static void on_delete_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1836 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
1840 static void on_duplicate_line_or_selection1_activate(GtkMenuItem *menuitem, gpointer user_data)
1842 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
1846 static void on_select_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1848 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
1852 static void on_select_current_paragraph1_activate(GtkMenuItem *menuitem, gpointer user_data)
1854 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
1858 static void on_insert_alternative_white_space1_activate(GtkMenuItem *menuitem, gpointer user_data)
1860 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
1864 static void on_go_to_next_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1866 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
1870 static void on_go_to_previous_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1872 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
1876 static void on_reflow_lines_block1_activate(GtkMenuItem *menuitem, gpointer user_data)
1878 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
1882 static void on_move_lines_up1_activate(GtkMenuItem *menuitem, gpointer user_data)
1884 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEUP);
1888 static void on_move_lines_down1_activate(GtkMenuItem *menuitem, gpointer user_data)
1890 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEDOWN);
1894 static void on_smart_line_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1896 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
1900 void on_plugin_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
1902 #ifdef HAVE_PLUGINS
1903 plugin_show_configure(NULL);
1904 #endif
1908 static void on_indent_width_activate(GtkMenuItem *menuitem, gpointer user_data)
1910 GeanyDocument *doc;
1911 gchar *label;
1912 gint width;
1914 if (ignore_callback)
1915 return;
1917 label = ui_menu_item_get_text(menuitem);
1918 width = atoi(label);
1919 g_free(label);
1921 doc = document_get_current();
1922 if (doc != NULL && width > 0)
1923 editor_set_indent_width(doc->editor, width);
1927 static void on_reset_indentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1929 guint i;
1931 foreach_document(i)
1932 document_apply_indent_settings(documents[i]);
1934 ui_update_statusbar(NULL, -1);
1935 ui_document_show_hide(NULL);
1939 static void on_mark_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1941 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
1945 static void on_detect_type_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1947 GeanyDocument *doc = document_get_current();
1948 GeanyIndentType type;
1950 if (doc != NULL && document_detect_indent_type(doc, &type))
1952 editor_set_indent_type(doc->editor, type);
1953 ui_document_show_hide(doc);
1954 ui_update_statusbar(doc, -1);
1959 static void on_show_symbol_list_toggled(GtkToggleButton *button, gpointer user_data)
1961 GtkWidget *widget = ui_lookup_widget(ui_widgets.prefs_dialog, "box_show_symbol_list_children");
1963 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(button));
1967 static void on_detect_width_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1969 GeanyDocument *doc = document_get_current();
1970 gint width;
1972 if (doc != NULL && document_detect_indent_width(doc, &width))
1974 editor_set_indent_width(doc->editor, width);
1975 ui_document_show_hide(doc);
1980 static void builder_connect_func(GtkBuilder *builder, GObject *object,
1981 const gchar *signal_name, const gchar *handler_name, GObject *connect_obj,
1982 GConnectFlags flags, gpointer user_data)
1984 GHashTable *hash = user_data;
1985 GCallback callback;
1987 callback = g_hash_table_lookup(hash, handler_name);
1988 g_return_if_fail(callback);
1990 if (connect_obj == NULL)
1991 g_signal_connect_data(object, signal_name, callback, NULL, NULL, flags);
1992 else
1993 g_signal_connect_object(object, signal_name, callback, connect_obj, flags);
1997 void callbacks_connect(GtkBuilder *builder)
1999 GHashTable *hash;
2001 g_return_if_fail(GTK_IS_BUILDER(builder));
2003 hash = g_hash_table_new(g_str_hash, g_str_equal);
2005 #define ITEM(n) g_hash_table_insert(hash, (gpointer) #n, G_CALLBACK(n));
2006 # include "signallist.i"
2007 #undef ITEM
2009 gtk_builder_connect_signals_full(builder, builder_connect_func, hash);
2010 g_hash_table_destroy(hash);