Merge pull request #527 from techee/eol_undo
[geany-mirror.git] / src / callbacks.c
blob87d1657717765638b0d91a977ebe0ca21351abca
1 /*
2 * callbacks.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Callbacks used by Glade. These are mainly in response to menu item and button events in the
24 * main window. Callbacks not used by Glade should go elsewhere.
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "callbacks.h"
33 #include "about.h"
34 #include "app.h"
35 #include "build.h"
36 #include "dialogs.h"
37 #include "documentprivate.h"
38 #include "encodings.h"
39 #include "filetypes.h"
40 #include "geanyobject.h"
41 #include "highlighting.h"
42 #include "keybindings.h"
43 #include "keyfile.h"
44 #include "log.h"
45 #include "main.h"
46 #include "msgwindow.h"
47 #include "navqueue.h"
48 #include "plugins.h"
49 #include "pluginutils.h"
50 #include "prefs.h"
51 #include "printing.h"
52 #include "sciwrappers.h"
53 #include "sidebar.h"
54 #include "spawn.h"
55 #ifdef HAVE_SOCKET
56 # include "socket.h"
57 #endif
58 #include "support.h"
59 #include "symbols.h"
60 #include "templates.h"
61 #include "toolbar.h"
62 #include "tools.h"
63 #include "ui_utils.h"
64 #include "utils.h"
65 #include "vte.h"
67 #include "gtkcompat.h"
69 #include <stdlib.h>
70 #include <unistd.h>
71 #include <string.h>
72 #include <gdk/gdkkeysyms.h>
73 #include <glib/gstdio.h>
74 #include <time.h>
77 /* represents the state at switching a notebook page(in the left treeviews widget), to not emit
78 * the selection-changed signal from tv.tree_openfiles */
79 /*static gboolean switch_tv_notebook_page = FALSE; */
83 /* wrapper function to abort exit process if cancel button is pressed */
84 static gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event, gpointer gdata)
86 return !main_quit();
91 * GUI callbacks
94 void on_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
96 document_new_file(NULL, NULL, NULL);
100 /* create a new file and copy file content and properties */
101 static void on_clone1_activate(GtkMenuItem *menuitem, gpointer user_data)
103 GeanyDocument *old_doc = document_get_current();
105 if (old_doc)
106 document_clone(old_doc);
110 void on_save1_activate(GtkMenuItem *menuitem, gpointer user_data)
112 GeanyDocument *doc = document_get_current();
114 if (doc != NULL)
116 document_save_file(doc, ui_prefs.allow_always_save);
121 void on_save_as1_activate(GtkMenuItem *menuitem, gpointer user_data)
123 dialogs_show_save_as();
127 void on_save_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
129 guint i, max = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
130 GeanyDocument *doc, *cur_doc = document_get_current();
131 guint count = 0;
133 /* iterate over documents in tabs order */
134 for (i = 0; i < max; i++)
136 doc = document_get_from_page(i);
137 if (! doc->changed)
138 continue;
140 if (document_save_file(doc, FALSE))
141 count++;
143 if (!count)
144 return;
146 ui_set_statusbar(FALSE, ngettext("%d file saved.", "%d files saved.", count), count);
147 /* saving may have changed window title, sidebar for another doc, so update */
148 document_show_tab(cur_doc);
149 sidebar_update_tag_list(cur_doc, TRUE);
150 ui_set_window_title(cur_doc);
154 void on_close_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
156 document_close_all();
160 void on_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
162 GeanyDocument *doc = document_get_current();
164 if (doc != NULL)
165 document_close(doc);
169 void on_quit1_activate(GtkMenuItem *menuitem, gpointer user_data)
171 main_quit();
175 static void on_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
177 gtk_widget_set_sensitive(ui_widgets.recent_files_menuitem,
178 g_queue_get_length(ui_prefs.recent_queue) > 0);
179 /* hide Page setup when GTK printing is not used */
180 ui_widget_show_hide(ui_widgets.print_page_setup, printing_prefs.use_gtk_printing);
184 /* edit actions, c&p & co, from menu bar and from popup menu */
185 static void on_edit1_activate(GtkMenuItem *menuitem, gpointer user_data)
187 GtkWidget *item;
188 GeanyDocument *doc = document_get_current();
190 ui_update_menu_copy_items(doc);
191 ui_update_insert_include_item(doc, 1);
193 item = ui_lookup_widget(main_widgets.window, "plugin_preferences1");
194 #ifndef HAVE_PLUGINS
195 gtk_widget_hide(item);
196 #else
197 gtk_widget_set_sensitive(item, plugins_have_preferences());
198 #endif
202 void on_undo1_activate(GtkMenuItem *menuitem, gpointer user_data)
204 GeanyDocument *doc = document_get_current();
206 g_return_if_fail(doc != NULL);
208 if (document_can_undo(doc))
210 sci_cancel(doc->editor->sci);
211 document_undo(doc);
216 void on_redo1_activate(GtkMenuItem *menuitem, gpointer user_data)
218 GeanyDocument *doc = document_get_current();
220 g_return_if_fail(doc != NULL);
222 if (document_can_redo(doc))
224 sci_cancel(doc->editor->sci);
225 document_redo(doc);
230 void on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
232 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
234 if (GTK_IS_EDITABLE(focusw))
235 gtk_editable_cut_clipboard(GTK_EDITABLE(focusw));
236 else if (IS_SCINTILLA(focusw))
237 sci_cut(SCINTILLA(focusw));
238 else if (GTK_IS_TEXT_VIEW(focusw))
240 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
241 GTK_TEXT_VIEW(focusw));
242 gtk_text_buffer_cut_clipboard(buffer, gtk_clipboard_get(GDK_NONE), TRUE);
247 void on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
249 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
251 if (GTK_IS_EDITABLE(focusw))
252 gtk_editable_copy_clipboard(GTK_EDITABLE(focusw));
253 else if (IS_SCINTILLA(focusw))
254 sci_copy(SCINTILLA(focusw));
255 else if (GTK_IS_TEXT_VIEW(focusw))
257 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
258 GTK_TEXT_VIEW(focusw));
259 gtk_text_buffer_copy_clipboard(buffer, gtk_clipboard_get(GDK_NONE));
264 void on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
266 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
268 if (GTK_IS_EDITABLE(focusw))
269 gtk_editable_paste_clipboard(GTK_EDITABLE(focusw));
270 else if (IS_SCINTILLA(focusw))
271 sci_paste(SCINTILLA(focusw));
272 else if (GTK_IS_TEXT_VIEW(focusw))
274 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
275 GTK_TEXT_VIEW(focusw));
276 gtk_text_buffer_paste_clipboard(buffer, gtk_clipboard_get(GDK_NONE), NULL,
277 TRUE);
282 void on_delete1_activate(GtkMenuItem *menuitem, gpointer user_data)
284 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
286 if (GTK_IS_EDITABLE(focusw))
287 gtk_editable_delete_selection(GTK_EDITABLE(focusw));
288 else if (IS_SCINTILLA(focusw) && sci_has_selection(SCINTILLA(focusw)))
289 sci_clear(SCINTILLA(focusw));
290 else if (GTK_IS_TEXT_VIEW(focusw))
292 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
293 GTK_TEXT_VIEW(focusw));
294 gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
299 void on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
301 prefs_show_dialog();
305 /* about menu item */
306 static void on_info1_activate(GtkMenuItem *menuitem, gpointer user_data)
308 about_dialog_show();
312 /* open file */
313 void on_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
315 dialogs_show_open_file();
319 /* reload file */
320 void on_toolbutton_reload_clicked(GtkAction *action, gpointer user_data)
322 GeanyDocument *doc = document_get_current();
324 g_return_if_fail(doc != NULL);
326 document_reload_prompt(doc, NULL);
330 static void on_change_font1_activate(GtkMenuItem *menuitem, gpointer user_data)
332 dialogs_show_open_font();
336 /* store text, clear search flags so we can use Search->Find Next/Previous */
337 static void setup_find(const gchar *text, gboolean backwards)
339 SETPTR(search_data.text, g_strdup(text));
340 SETPTR(search_data.original_text, g_strdup(text));
341 search_data.flags = 0;
342 search_data.backwards = backwards;
343 search_data.search_bar = TRUE;
347 static void do_toolbar_search(const gchar *text, gboolean incremental, gboolean backwards)
349 GeanyDocument *doc = document_get_current();
350 gboolean result;
352 setup_find(text, backwards);
353 result = document_search_bar_find(doc, search_data.text, incremental, backwards);
354 if (search_data.search_bar)
355 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
359 /* search text */
360 void on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
362 do_toolbar_search(text, TRUE, FALSE);
366 /* search text */
367 void on_toolbar_search_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
369 do_toolbar_search(text, FALSE, GPOINTER_TO_INT(user_data));
373 /* search text */
374 void on_toolbutton_search_clicked(GtkAction *action, gpointer user_data)
376 GeanyDocument *doc = document_get_current();
377 gboolean result;
378 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
380 if (entry != NULL)
382 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
384 setup_find(text, FALSE);
385 result = document_search_bar_find(doc, search_data.text, FALSE, FALSE);
386 if (search_data.search_bar)
387 ui_set_search_entry_background(entry, result);
389 else
390 on_find1_activate(NULL, NULL);
394 /* hides toolbar from toolbar popup menu */
395 static void on_hide_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
397 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
398 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
402 /* zoom in from menu bar and popup menu */
403 void on_zoom_in1_activate(GtkMenuItem *menuitem, gpointer user_data)
405 GeanyDocument *doc = document_get_current();
407 g_return_if_fail(doc != NULL);
409 sci_zoom_in(doc->editor->sci);
413 /* zoom out from menu bar and popup menu */
414 void on_zoom_out1_activate(GtkMenuItem *menuitem, gpointer user_data)
416 GeanyDocument *doc = document_get_current();
418 g_return_if_fail(doc != NULL);
420 sci_zoom_out(doc->editor->sci);
424 void on_normal_size1_activate(GtkMenuItem *menuitem, gpointer user_data)
426 GeanyDocument *doc = document_get_current();
428 g_return_if_fail(doc != NULL);
430 sci_zoom_off(doc->editor->sci);
434 /* Changes window-title after switching tabs and lots of other things.
435 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
436 static void on_notebook1_switch_page_after(GtkNotebook *notebook, gpointer page,
437 guint page_num, gpointer user_data)
439 GeanyDocument *doc;
441 if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
442 return;
444 doc = document_get_from_notebook_child(page);
446 if (doc != NULL)
448 sidebar_select_openfiles_item(doc);
449 ui_save_buttons_toggle(doc->changed);
450 ui_set_window_title(doc);
451 ui_update_statusbar(doc, -1);
452 ui_update_popup_reundo_items(doc);
453 ui_document_show_hide(doc); /* update the document menu */
454 build_menu_update(doc);
455 sidebar_update_tag_list(doc, FALSE);
456 document_highlight_tags(doc);
458 document_check_disk_status(doc, TRUE);
460 #ifdef HAVE_VTE
461 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
462 #endif
464 g_signal_emit_by_name(geany_object, "document-activate", doc);
469 static void on_tv_notebook_switch_page(GtkNotebook *notebook, gpointer page,
470 guint page_num, gpointer user_data)
472 /* suppress selection changed signal when switching to the open files list */
473 ignore_callback = TRUE;
477 static void on_tv_notebook_switch_page_after(GtkNotebook *notebook, gpointer page,
478 guint page_num, gpointer user_data)
480 ignore_callback = FALSE;
484 static void convert_eol(gint mode)
486 GeanyDocument *doc = document_get_current();
488 g_return_if_fail(doc != NULL);
490 /* sci_convert_eols() adds UNDO_SCINTILLA action in on_editor_notify().
491 * It is added to the undo stack before sci_convert_eols() finishes
492 * so after adding UNDO_EOL, UNDO_EOL will be at the top of the stack
493 * and UNDO_SCINTILLA below it. */
494 sci_convert_eols(doc->editor->sci, mode);
495 document_undo_add(doc, UNDO_EOL, GINT_TO_POINTER(sci_get_eol_mode(doc->editor->sci)));
497 sci_set_eol_mode(doc->editor->sci, mode);
499 ui_update_statusbar(doc, -1);
503 static void on_crlf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
505 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
506 return;
508 convert_eol(SC_EOL_CRLF);
512 static void on_lf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
514 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
515 return;
517 convert_eol(SC_EOL_LF);
521 static void on_cr_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
523 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
524 return;
526 convert_eol(SC_EOL_CR);
530 void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
532 GeanyDocument *doc = document_get_current();
534 g_return_if_fail(doc != NULL);
536 editor_replace_tabs(doc->editor, FALSE);
540 gboolean toolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
542 if (event->button == 3)
544 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
545 return TRUE;
547 return FALSE;
551 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
553 GeanyDocument *doc = document_get_current();
554 ScintillaObject *sci;
555 gchar *text;
556 gboolean keep_sel = TRUE;
558 g_return_if_fail(doc != NULL);
560 sci = doc->editor->sci;
561 if (! sci_has_selection(sci))
563 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
564 keep_sel = FALSE;
567 /* either we already had a selection or we created one for current word */
568 if (sci_has_selection(sci))
570 gchar *result = NULL;
571 gint cmd = SCI_LOWERCASE;
572 gboolean rectsel = (gboolean) scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
574 text = sci_get_selection_contents(sci);
576 if (utils_str_has_upper(text))
578 if (rectsel)
579 cmd = SCI_LOWERCASE;
580 else
581 result = g_utf8_strdown(text, -1);
583 else
585 if (rectsel)
586 cmd = SCI_UPPERCASE;
587 else
588 result = g_utf8_strup(text, -1);
591 if (result != NULL)
593 sci_replace_sel(sci, result);
594 g_free(result);
595 if (keep_sel)
596 sci_set_selection_start(sci, sci_get_current_position(sci) - strlen(text));
598 else
599 sci_send_command(sci, cmd);
601 g_free(text);
607 static void on_show_toolbar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
609 if (ignore_callback) return;
611 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
612 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
616 static void on_fullscreen1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
618 if (ignore_callback)
619 return;
621 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
622 ui_set_fullscreen();
626 static void on_show_messages_window1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
628 if (ignore_callback)
629 return;
631 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
632 msgwin_show_hide(ui_prefs.msgwindow_visible);
636 static void on_menu_color_schemes_activate(GtkImageMenuItem *imagemenuitem, gpointer user_data)
638 highlighting_show_color_scheme_dialog();
642 static void on_markers_margin1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
644 if (ignore_callback)
645 return;
647 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
648 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
652 static void on_show_line_numbers1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
654 if (ignore_callback)
655 return;
657 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
658 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
662 static void on_menu_show_white_space1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
664 if (ignore_callback)
665 return;
667 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
668 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
672 static void on_menu_show_line_endings1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
674 if (ignore_callback)
675 return;
677 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
678 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
682 static void on_menu_show_indentation_guides1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
684 if (ignore_callback)
685 return;
687 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
688 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
692 void on_line_wrapping1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
694 if (! ignore_callback)
696 GeanyDocument *doc = document_get_current();
697 g_return_if_fail(doc != NULL);
699 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
704 static void on_set_file_readonly1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
706 if (! ignore_callback)
708 GeanyDocument *doc = document_get_current();
709 g_return_if_fail(doc != NULL);
711 doc->readonly = ! doc->readonly;
712 sci_set_readonly(doc->editor->sci, doc->readonly);
713 ui_update_tab_status(doc);
714 ui_update_statusbar(doc, -1);
719 static void on_use_auto_indentation1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
721 if (! ignore_callback)
723 GeanyDocument *doc = document_get_current();
724 g_return_if_fail(doc != NULL);
726 doc->editor->auto_indent = ! doc->editor->auto_indent;
731 static void find_usage(gboolean in_session)
733 GeanyFindFlags flags;
734 gchar *search_text;
735 GeanyDocument *doc = document_get_current();
737 g_return_if_fail(doc != NULL);
739 if (sci_has_selection(doc->editor->sci))
740 { /* take selected text if there is a selection */
741 search_text = sci_get_selection_contents(doc->editor->sci);
742 flags = GEANY_FIND_MATCHCASE;
744 else
746 editor_find_current_word_sciwc(doc->editor, -1,
747 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
748 search_text = g_strdup(editor_info.current_word);
749 flags = GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD;
752 search_find_usage(search_text, search_text, flags, in_session);
753 g_free(search_text);
757 void on_find_document_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
759 find_usage(FALSE);
763 void on_find_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
765 find_usage(TRUE);
769 static void goto_tag(gboolean definition)
771 GeanyDocument *doc = document_get_current();
773 g_return_if_fail(doc != NULL);
775 /* update cursor pos for navigating back afterwards */
776 if (!sci_has_selection(doc->editor->sci))
777 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
779 /* use the keybinding callback as it checks for selections as well as current word */
780 if (definition)
781 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
782 else
783 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
787 static void on_goto_tag_definition1(GtkMenuItem *menuitem, gpointer user_data)
789 goto_tag(TRUE);
793 static void on_goto_tag_declaration1(GtkMenuItem *menuitem, gpointer user_data)
795 goto_tag(FALSE);
799 static void on_count_words1_activate(GtkMenuItem *menuitem, gpointer user_data)
801 tools_word_count();
805 void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data)
807 gchar colour[9];
808 GeanyDocument *doc = document_get_current();
809 gint pos;
811 g_return_if_fail(doc != NULL);
813 pos = sci_get_current_position(doc->editor->sci);
814 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
815 tools_color_chooser(colour);
819 void on_toolbutton_compile_clicked(GtkAction *action, gpointer user_data)
821 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
825 void on_find1_activate(GtkMenuItem *menuitem, gpointer user_data)
827 search_show_find_dialog();
831 void on_find_next1_activate(GtkMenuItem *menuitem, gpointer user_data)
833 search_find_again(FALSE);
837 void on_find_previous1_activate(GtkMenuItem *menuitem, gpointer user_data)
839 if (search_data.flags & GEANY_FIND_REGEXP)
840 /* Can't reverse search order for a regex (find next ignores search backwards) */
841 utils_beep();
842 else
843 search_find_again(TRUE);
847 void on_find_nextsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
849 search_find_selection(document_get_current(), FALSE);
853 void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
855 search_find_selection(document_get_current(), TRUE);
859 void on_replace1_activate(GtkMenuItem *menuitem, gpointer user_data)
861 search_show_replace_dialog();
865 void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
867 search_show_find_in_files_dialog(NULL);
871 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
873 if (*text == '+' || *text == '-')
875 *line_no = atoi(text + 1);
876 *offset = (*text == '+') ? 1 : -1;
878 else
880 *line_no = atoi(text) - 1;
881 *offset = 0;
886 void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
888 static gchar value[16] = "";
889 gchar *result;
891 result = dialogs_show_input_goto_line(
892 _("Go to Line"), GTK_WINDOW(main_widgets.window),
893 _("Enter the line you want to go to:"), value);
894 if (result != NULL)
896 GeanyDocument *doc = document_get_current();
897 gint offset;
898 gint line_no;
900 g_return_if_fail(doc != NULL);
902 get_line_and_offset_from_text(result, &line_no, &offset);
903 if (! editor_goto_line(doc->editor, line_no, offset))
904 utils_beep();
905 /* remember value for future calls */
906 g_snprintf(value, sizeof(value), "%s", result);
908 g_free(result);
913 void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
915 GeanyDocument *doc = document_get_current();
916 gint offset;
917 gint line_no;
919 g_return_if_fail(doc != NULL);
921 get_line_and_offset_from_text(text, &line_no, &offset);
922 if (! editor_goto_line(doc->editor, line_no, offset))
923 utils_beep();
924 else
925 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
929 void on_toolbutton_goto_clicked(GtkAction *action, gpointer user_data)
931 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
933 if (entry != NULL)
935 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
937 on_toolbutton_goto_entry_activate(NULL, text, NULL);
939 else
940 on_go_to_line_activate(NULL, NULL);
944 void on_help1_activate(GtkMenuItem *menuitem, gpointer user_data)
946 gchar *uri;
948 uri = utils_get_help_url(NULL);
949 utils_open_browser(uri);
950 g_free(uri);
954 static void on_help_shortcuts1_activate(GtkMenuItem *menuitem, gpointer user_data)
956 keybindings_show_shortcuts();
960 static void on_website1_activate(GtkMenuItem *menuitem, gpointer user_data)
962 utils_open_browser(GEANY_HOMEPAGE);
966 static void on_help_menu_item_donate_activate(GtkMenuItem *item, gpointer user_data)
968 utils_open_browser(GEANY_DONATE);
972 static void on_help_menu_item_wiki_activate(GtkMenuItem *item, gpointer user_data)
974 utils_open_browser(GEANY_WIKI);
978 static void on_help_menu_item_bug_report_activate(GtkMenuItem *item, gpointer user_data)
980 utils_open_browser(GEANY_BUG_REPORT);
984 static void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data)
986 GeanyDocument *doc = document_get_current();
987 gchar *text;
988 const gchar *cur_tag = NULL;
989 gint line = -1, pos = 0;
991 if (doc == NULL || doc->file_type == NULL)
993 ui_set_statusbar(FALSE,
994 _("Please set the filetype for the current file before using this function."));
995 return;
998 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
999 * returns the current position, so it should be safe */
1000 line = symbols_get_current_function(doc, &cur_tag);
1001 pos = sci_get_position_from_line(doc->editor->sci, line);
1003 text = templates_get_template_function(doc, cur_tag);
1005 sci_start_undo_action(doc->editor->sci);
1006 sci_insert_text(doc->editor->sci, pos, text);
1007 sci_end_undo_action(doc->editor->sci);
1008 g_free(text);
1012 static void insert_multiline_comment(GeanyDocument *doc, gint pos)
1014 g_return_if_fail(doc != NULL);
1015 g_return_if_fail(pos == -1 || pos >= 0);
1017 if (doc->file_type == NULL)
1019 ui_set_statusbar(FALSE,
1020 _("Please set the filetype for the current file before using this function."));
1021 return;
1024 if (doc->file_type->comment_open || doc->file_type->comment_single)
1026 /* editor_insert_multiline_comment() uses editor_info.click_pos */
1027 if (pos == -1)
1028 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
1029 else
1030 editor_info.click_pos = pos;
1031 editor_insert_multiline_comment(doc->editor);
1033 else
1034 utils_beep();
1038 static void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1040 insert_multiline_comment(document_get_current(), editor_info.click_pos);
1044 static void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1046 insert_multiline_comment(document_get_current(), -1);
1050 static void insert_comment_template(GeanyDocument *doc, gint pos, guint template)
1052 gchar *text;
1054 g_return_if_fail(doc != NULL);
1055 g_return_if_fail(pos == -1 || pos >= 0);
1056 g_return_if_fail(template < GEANY_MAX_TEMPLATES);
1058 if (pos == -1)
1059 pos = sci_get_current_position(doc->editor->sci);
1061 text = templates_get_template_licence(doc, template);
1063 sci_start_undo_action(doc->editor->sci);
1064 sci_insert_text(doc->editor->sci, pos, text);
1065 sci_end_undo_action(doc->editor->sci);
1066 g_free(text);
1070 static void on_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1072 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_GPL);
1076 static void on_menu_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1078 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_GPL);
1082 static void on_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1084 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_BSD);
1088 static void on_menu_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1090 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_BSD);
1094 static void on_comments_changelog_activate(GtkMenuItem *menuitem, gpointer user_data)
1096 GeanyDocument *doc = document_get_current();
1097 gchar *text;
1099 g_return_if_fail(doc != NULL);
1101 text = templates_get_template_changelog(doc);
1102 sci_start_undo_action(doc->editor->sci);
1103 sci_insert_text(doc->editor->sci, 0, text);
1104 /* sets the cursor to the right position to type the changelog text,
1105 * the template has 21 chars + length of name and email */
1106 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1107 sci_end_undo_action(doc->editor->sci);
1109 g_free(text);
1113 static void on_comments_fileheader_activate(GtkMenuItem *menuitem, gpointer user_data)
1115 GeanyDocument *doc = document_get_current();
1116 gchar *text;
1117 const gchar *fname;
1118 GeanyFiletype *ft;
1120 g_return_if_fail(doc != NULL);
1122 ft = doc->file_type;
1123 fname = doc->file_name;
1124 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1126 sci_start_undo_action(doc->editor->sci);
1127 sci_insert_text(doc->editor->sci, 0, text);
1128 sci_goto_pos(doc->editor->sci, 0, FALSE);
1129 sci_end_undo_action(doc->editor->sci);
1130 g_free(text);
1134 void on_file_properties_activate(GtkMenuItem *menuitem, gpointer user_data)
1136 GeanyDocument *doc = document_get_current();
1137 g_return_if_fail(doc != NULL);
1139 dialogs_show_file_properties(doc);
1143 static void on_menu_fold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1145 GeanyDocument *doc = document_get_current();
1146 g_return_if_fail(doc != NULL);
1148 editor_fold_all(doc->editor);
1152 static void on_menu_unfold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1154 GeanyDocument *doc = document_get_current();
1155 g_return_if_fail(doc != NULL);
1157 editor_unfold_all(doc->editor);
1161 void on_toolbutton_run_clicked(GtkAction *action, gpointer user_data)
1163 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1167 void on_menu_remove_indicators1_activate(GtkMenuItem *menuitem, gpointer user_data)
1169 GeanyDocument *doc = document_get_current();
1170 g_return_if_fail(doc != NULL);
1172 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1176 void on_print1_activate(GtkMenuItem *menuitem, gpointer user_data)
1178 GeanyDocument *doc = document_get_current();
1179 g_return_if_fail(doc != NULL);
1181 printing_print_doc(doc);
1185 void on_menu_select_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1187 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1189 /* special case for Select All in the scribble widget */
1190 if (GTK_IS_TEXT_VIEW(focusw))
1192 g_signal_emit_by_name(focusw, "select-all", TRUE);
1194 /* special case for Select All in the VTE widget */
1195 #ifdef HAVE_VTE
1196 else if (vte_info.have_vte && focusw == vc->vte)
1198 vte_select_all();
1200 #endif
1201 else if (GTK_IS_EDITABLE(focusw))
1203 gtk_editable_select_region(GTK_EDITABLE(focusw), 0, -1);
1205 else if (IS_SCINTILLA(focusw))
1207 sci_select_all(SCINTILLA(focusw));
1212 void on_menu_show_sidebar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1214 if (ignore_callback)
1215 return;
1217 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1219 /* show built-in tabs if no tabs visible */
1220 if (ui_prefs.sidebar_visible &&
1221 ! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1222 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1224 interface_prefs.sidebar_openfiles_visible = TRUE;
1225 interface_prefs.sidebar_symbol_visible = TRUE;
1228 /* if window has input focus, set it back to the editor before toggling off */
1229 if (! ui_prefs.sidebar_visible &&
1230 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1232 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1235 ui_sidebar_show_hide();
1239 static void on_menu_write_unicode_bom1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1241 if (! ignore_callback)
1243 GeanyDocument *doc = document_get_current();
1245 g_return_if_fail(doc != NULL);
1246 if (doc->readonly)
1248 utils_beep();
1249 return;
1252 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1254 doc->has_bom = ! doc->has_bom;
1256 ui_update_statusbar(doc, -1);
1261 void on_menu_comment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1263 GeanyDocument *doc = document_get_current();
1264 g_return_if_fail(doc != NULL);
1266 editor_do_comment(doc->editor, -1, FALSE, FALSE, TRUE);
1270 void on_menu_uncomment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1272 GeanyDocument *doc = document_get_current();
1273 g_return_if_fail(doc != NULL);
1275 editor_do_uncomment(doc->editor, -1, FALSE);
1279 void on_menu_toggle_line_commentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1281 GeanyDocument *doc = document_get_current();
1282 g_return_if_fail(doc != NULL);
1284 editor_do_comment_toggle(doc->editor);
1288 void on_menu_increase_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1290 GeanyDocument *doc = document_get_current();
1291 g_return_if_fail(doc != NULL);
1293 editor_indent(doc->editor, TRUE);
1297 void on_menu_decrease_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1299 GeanyDocument *doc = document_get_current();
1300 g_return_if_fail(doc != NULL);
1302 editor_indent(doc->editor, FALSE);
1306 void on_next_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1308 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1309 msgwin_goto_messages_file_line))
1310 ui_set_statusbar(FALSE, _("No more message items."));
1314 void on_previous_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1316 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1317 msgwin_goto_messages_file_line))
1318 ui_set_statusbar(FALSE, _("No more message items."));
1322 void on_project_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
1324 project_new();
1328 void on_project_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
1330 project_open();
1334 void on_project_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
1336 project_close(TRUE);
1340 void on_project_properties1_activate(GtkMenuItem *menuitem, gpointer user_data)
1342 project_properties();
1346 static void on_menu_project1_activate(GtkMenuItem *menuitem, gpointer user_data)
1348 static GtkWidget *item_close = NULL;
1349 static GtkWidget *item_properties = NULL;
1351 if (item_close == NULL)
1353 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1354 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1357 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1358 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1359 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1360 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1364 void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
1366 GeanyDocument *doc = document_get_current();
1367 gchar *sel = NULL;
1368 const gchar *wc;
1370 #ifdef G_OS_WIN32
1371 wc = GEANY_WORDCHARS "./-" "\\";
1372 #else
1373 wc = GEANY_WORDCHARS "./-";
1374 #endif
1376 g_return_if_fail(doc != NULL);
1378 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1379 SETPTR(sel, utils_get_locale_from_utf8(sel));
1381 if (sel != NULL)
1383 gchar *filename = NULL;
1385 if (g_path_is_absolute(sel))
1386 filename = g_strdup(sel);
1387 else
1388 { /* relative filename, add the path of the current file */
1389 gchar *path;
1391 path = utils_get_current_file_dir_utf8();
1392 SETPTR(path, utils_get_locale_from_utf8(path));
1393 if (!path)
1394 path = g_get_current_dir();
1396 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1398 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1399 app->project != NULL && !EMPTY(app->project->base_path))
1401 /* try the project's base path */
1402 SETPTR(path, project_get_base_path());
1403 SETPTR(path, utils_get_locale_from_utf8(path));
1404 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1406 g_free(path);
1407 #ifdef G_OS_UNIX
1408 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1409 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));
1411 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1412 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
1413 #endif
1416 if (g_file_test(filename, G_FILE_TEST_EXISTS))
1417 document_open_file(filename, FALSE, NULL, NULL);
1418 else
1420 SETPTR(sel, utils_get_utf8_from_locale(sel));
1421 ui_set_statusbar(TRUE, _("Could not open file %s (File not found)"), sel);
1424 g_free(filename);
1425 g_free(sel);
1430 void on_remove_markers1_activate(GtkMenuItem *menuitem, gpointer user_data)
1432 GeanyDocument *doc = document_get_current();
1433 g_return_if_fail(doc != NULL);
1435 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1436 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1437 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1441 static void on_load_tags1_activate(GtkMenuItem *menuitem, gpointer user_data)
1443 symbols_show_load_tags_dialog();
1447 void on_context_action1_activate(GtkMenuItem *menuitem, gpointer user_data)
1449 gchar *word, *command;
1450 GError *error = NULL;
1451 GeanyDocument *doc = document_get_current();
1452 const gchar *check_msg;
1454 g_return_if_fail(doc != NULL);
1456 if (sci_has_selection(doc->editor->sci))
1457 { /* take selected text if there is a selection */
1458 word = sci_get_selection_contents(doc->editor->sci);
1460 else
1462 word = g_strdup(editor_info.current_word);
1465 /* use the filetype specific command if available, fallback to global command otherwise */
1466 if (doc->file_type != NULL &&
1467 !EMPTY(doc->file_type->context_action_cmd))
1469 command = g_strdup(doc->file_type->context_action_cmd);
1470 check_msg = _("Check the path setting in Filetype configuration.");
1472 else
1474 command = g_strdup(tool_prefs.context_action_cmd);
1475 check_msg = _("Check the path setting in Preferences.");
1478 /* substitute the wildcard %s and run the command if it is non empty */
1479 if (G_LIKELY(!EMPTY(command)))
1481 gchar *command_line = g_strdup(command);
1483 utils_str_replace_all(&command_line, "%s", word);
1485 if (!spawn_async(NULL, command_line, NULL, NULL, NULL, &error))
1487 /* G_SHELL_ERROR is parsing error, it may be caused by %s word with quotes */
1488 ui_set_statusbar(TRUE, _("Cannot execute context action command \"%s\": %s. %s"),
1489 error->domain == G_SHELL_ERROR ? command_line : command, error->message,
1490 check_msg);
1491 g_error_free(error);
1493 g_free(command_line);
1495 g_free(word);
1496 g_free(command);
1500 void on_menu_toggle_all_additional_widgets1_activate(GtkMenuItem *menuitem, gpointer user_data)
1502 static gint hide_all = -1;
1503 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1504 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1505 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1506 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1508 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1509 if (G_UNLIKELY(hide_all == -1))
1511 if (! gtk_check_menu_item_get_active(msgw) &&
1512 ! interface_prefs.show_notebook_tabs &&
1513 ! gtk_check_menu_item_get_active(toolbari))
1515 hide_all = TRUE;
1517 else
1518 hide_all = FALSE;
1521 hide_all = ! hide_all; /* toggle */
1523 if (hide_all)
1525 if (gtk_check_menu_item_get_active(msgw))
1526 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1528 interface_prefs.show_notebook_tabs = FALSE;
1529 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1531 ui_statusbar_showhide(FALSE);
1533 if (gtk_check_menu_item_get_active(toolbari))
1534 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1536 else
1539 if (! gtk_check_menu_item_get_active(msgw))
1540 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1542 interface_prefs.show_notebook_tabs = TRUE;
1543 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1545 ui_statusbar_showhide(TRUE);
1547 if (! gtk_check_menu_item_get_active(toolbari))
1548 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1553 void on_toolbutton_forward_activate(GtkAction *menuitem, gpointer user_data)
1555 navqueue_go_forward();
1559 void on_toolbutton_back_activate(GtkAction *menuitem, gpointer user_data)
1561 navqueue_go_back();
1565 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1567 if (prefs.auto_focus && ! gtk_widget_has_focus(widget))
1568 gtk_widget_grab_focus(widget);
1570 return FALSE;
1574 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1576 GeanyDocument *doc;
1578 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1579 return;
1581 doc = document_get_current();
1582 g_return_if_fail(doc != NULL);
1584 editor_set_indent(doc->editor, type, doc->editor->indent_width);
1585 ui_update_statusbar(doc, -1);
1589 static void on_tabs1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1591 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1595 static void on_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1597 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
1601 static void on_tabs_and_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1603 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
1607 static void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer user_data)
1609 GeanyDocument *doc;
1611 if (ignore_callback)
1612 return;
1614 doc = document_get_current();
1615 g_return_if_fail(doc != NULL);
1617 editor_strip_trailing_spaces(doc->editor, FALSE);
1621 static void on_page_setup1_activate(GtkMenuItem *menuitem, gpointer user_data)
1623 printing_page_setup_gtk();
1627 gboolean on_escape_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1629 guint state = keybindings_get_modifiers(event->state);
1631 /* make pressing escape in the sidebar and toolbar focus the editor */
1632 if (event->keyval == GDK_Escape && state == 0)
1634 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1635 return TRUE;
1637 return FALSE;
1641 void on_line_breaking1_activate(GtkMenuItem *menuitem, gpointer user_data)
1643 GeanyDocument *doc;
1645 if (ignore_callback)
1646 return;
1648 doc = document_get_current();
1649 g_return_if_fail(doc != NULL);
1651 doc->editor->line_breaking = !doc->editor->line_breaking;
1655 void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
1657 GeanyDocument *doc = document_get_current();
1659 g_return_if_fail(doc != NULL);
1661 editor_replace_spaces(doc->editor, FALSE);
1665 static void on_search1_activate(GtkMenuItem *menuitem, gpointer user_data)
1667 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
1668 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
1669 gboolean have_messages;
1671 /* enable commands if the messages window has any items */
1672 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
1673 NULL) > 0;
1675 gtk_widget_set_sensitive(next_message, have_messages);
1676 gtk_widget_set_sensitive(previous_message, have_messages);
1680 /* simple implementation (vs. close all which doesn't close documents if cancelled),
1681 * if user_data is set, it is the GeanyDocument to keep */
1682 void on_close_other_documents1_activate(GtkMenuItem *menuitem, gpointer user_data)
1684 guint i;
1685 GeanyDocument *cur_doc = user_data;
1687 if (cur_doc == NULL)
1688 cur_doc = document_get_current();
1690 for (i = 0; i < documents_array->len; i++)
1692 GeanyDocument *doc = documents[i];
1694 if (doc == cur_doc || ! doc->is_valid)
1695 continue;
1697 if (! document_close(doc))
1698 break;
1703 static void on_menu_reload_configuration1_activate(GtkMenuItem *menuitem, gpointer user_data)
1705 main_reload_configuration();
1709 static void on_debug_messages1_activate(GtkMenuItem *menuitem, gpointer user_data)
1711 log_show_debug_messages_dialog();
1715 void on_send_selection_to_vte1_activate(GtkMenuItem *menuitem, gpointer user_data)
1717 #ifdef HAVE_VTE
1718 if (vte_info.have_vte)
1719 vte_send_selection_to_vte();
1720 #endif
1724 static gboolean on_window_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
1727 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1729 static GtkWidget *menuitem = NULL;
1731 if (menuitem == NULL)
1732 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
1734 ignore_callback = TRUE;
1736 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
1737 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
1739 ignore_callback = FALSE;
1741 return FALSE;
1745 static void show_notebook_page(const gchar *notebook_name, const gchar *page_name)
1747 GtkWidget *widget;
1748 GtkNotebook *notebook;
1750 widget = ui_lookup_widget(ui_widgets.prefs_dialog, page_name);
1751 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, notebook_name));
1753 if (notebook != NULL && widget != NULL)
1754 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
1758 static void on_customize_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
1760 prefs_show_dialog();
1762 /* select the Interface page */
1763 show_notebook_page("notebook2", "notebook6");
1764 /* select the Toolbar subpage */
1765 show_notebook_page("notebook6", "vbox15");
1769 static void on_button_customize_toolbar_clicked(GtkButton *button, gpointer user_data)
1771 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
1775 static void on_cut_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1777 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
1781 static void on_copy_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1783 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
1787 static void on_delete_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1789 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
1793 static void on_duplicate_line_or_selection1_activate(GtkMenuItem *menuitem, gpointer user_data)
1795 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
1799 static void on_select_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1801 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
1805 static void on_select_current_paragraph1_activate(GtkMenuItem *menuitem, gpointer user_data)
1807 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
1811 static void on_insert_alternative_white_space1_activate(GtkMenuItem *menuitem, gpointer user_data)
1813 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
1817 static void on_go_to_next_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1819 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
1823 static void on_go_to_previous_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1825 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
1829 static void on_reflow_lines_block1_activate(GtkMenuItem *menuitem, gpointer user_data)
1831 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
1835 static void on_move_lines_up1_activate(GtkMenuItem *menuitem, gpointer user_data)
1837 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEUP);
1841 static void on_move_lines_down1_activate(GtkMenuItem *menuitem, gpointer user_data)
1843 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEDOWN);
1847 static void on_smart_line_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1849 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
1853 void on_plugin_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
1855 #ifdef HAVE_PLUGINS
1856 plugin_show_configure(NULL);
1857 #endif
1861 static void on_indent_width_activate(GtkMenuItem *menuitem, gpointer user_data)
1863 GeanyDocument *doc;
1864 gchar *label;
1865 gint width;
1867 if (ignore_callback)
1868 return;
1870 label = ui_menu_item_get_text(menuitem);
1871 width = atoi(label);
1872 g_free(label);
1874 doc = document_get_current();
1875 if (doc != NULL && width > 0)
1876 editor_set_indent_width(doc->editor, width);
1880 static void on_reset_indentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1882 guint i;
1884 foreach_document(i)
1885 document_apply_indent_settings(documents[i]);
1887 ui_update_statusbar(NULL, -1);
1888 ui_document_show_hide(NULL);
1892 static void on_mark_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1894 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
1898 static void on_detect_type_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1900 GeanyDocument *doc = document_get_current();
1901 GeanyIndentType type;
1903 if (doc != NULL && document_detect_indent_type(doc, &type))
1905 editor_set_indent_type(doc->editor, type);
1906 ui_document_show_hide(doc);
1911 static void on_show_symbol_list_toggled(GtkToggleButton *button, gpointer user_data)
1913 GtkWidget *widget = ui_lookup_widget(ui_widgets.prefs_dialog, "box_show_symbol_list_children");
1915 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(button));
1919 static void on_detect_width_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1921 GeanyDocument *doc = document_get_current();
1922 gint width;
1924 if (doc != NULL && document_detect_indent_width(doc, &width))
1926 editor_set_indent_width(doc->editor, width);
1927 ui_document_show_hide(doc);
1932 static void builder_connect_func(GtkBuilder *builder, GObject *object,
1933 const gchar *signal_name, const gchar *handler_name, GObject *connect_obj,
1934 GConnectFlags flags, gpointer user_data)
1936 GHashTable *hash = user_data;
1937 GCallback callback;
1939 callback = g_hash_table_lookup(hash, handler_name);
1940 g_return_if_fail(callback);
1942 if (connect_obj == NULL)
1943 g_signal_connect_data(object, signal_name, callback, NULL, NULL, flags);
1944 else
1945 g_signal_connect_object(object, signal_name, callback, connect_obj, flags);
1949 void callbacks_connect(GtkBuilder *builder)
1951 GHashTable *hash;
1953 g_return_if_fail(GTK_IS_BUILDER(builder));
1955 hash = g_hash_table_new(g_str_hash, g_str_equal);
1957 #define ITEM(n) g_hash_table_insert(hash, (gpointer) #n, G_CALLBACK(n));
1958 # include "signallist.i"
1959 #undef ITEM
1961 gtk_builder_connect_signals_full(builder, builder_connect_func, hash);
1962 g_hash_table_destroy(hash);