Fix undo of line end type change
[geany-mirror.git] / src / callbacks.c
blobf0d6b7ca9b670e4dd1de29fa6abf499a52dd6e20
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 static gboolean delayed_check_disk_status(gpointer data)
436 document_check_disk_status(data, FALSE);
437 return FALSE;
441 /* Changes window-title after switching tabs and lots of other things.
442 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
443 static void on_notebook1_switch_page_after(GtkNotebook *notebook, gpointer page,
444 guint page_num, gpointer user_data)
446 GeanyDocument *doc;
448 if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
449 return;
451 doc = document_get_from_notebook_child(page);
453 if (doc != NULL)
455 sidebar_select_openfiles_item(doc);
456 ui_save_buttons_toggle(doc->changed);
457 ui_set_window_title(doc);
458 ui_update_statusbar(doc, -1);
459 ui_update_popup_reundo_items(doc);
460 ui_document_show_hide(doc); /* update the document menu */
461 build_menu_update(doc);
462 sidebar_update_tag_list(doc, FALSE);
463 document_highlight_tags(doc);
465 /* We delay the check to avoid weird fast, unintended switching of notebook pages when
466 * the 'file has changed' dialog is shown while the switch event is not yet completely
467 * finished. So, we check after the switch has been performed to be safe. */
468 g_idle_add(delayed_check_disk_status, doc);
470 #ifdef HAVE_VTE
471 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
472 #endif
474 g_signal_emit_by_name(geany_object, "document-activate", doc);
479 static void on_tv_notebook_switch_page(GtkNotebook *notebook, gpointer page,
480 guint page_num, gpointer user_data)
482 /* suppress selection changed signal when switching to the open files list */
483 ignore_callback = TRUE;
487 static void on_tv_notebook_switch_page_after(GtkNotebook *notebook, gpointer page,
488 guint page_num, gpointer user_data)
490 ignore_callback = FALSE;
494 static void convert_eol(gint mode)
496 GeanyDocument *doc = document_get_current();
498 g_return_if_fail(doc != NULL);
500 /* sci_convert_eols() adds UNDO_SCINTILLA action in on_editor_notify().
501 * It is added to the undo stack before sci_convert_eols() finishes
502 * so after adding UNDO_EOL, UNDO_EOL will be at the top of the stack
503 * and UNDO_SCINTILLA below it. */
504 sci_convert_eols(doc->editor->sci, mode);
505 document_undo_add(doc, UNDO_EOL, GINT_TO_POINTER(sci_get_eol_mode(doc->editor->sci)));
507 sci_set_eol_mode(doc->editor->sci, mode);
509 ui_update_statusbar(doc, -1);
513 static void on_crlf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
515 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
516 return;
518 convert_eol(SC_EOL_CRLF);
522 static void on_lf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
524 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
525 return;
527 convert_eol(SC_EOL_LF);
531 static void on_cr_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
533 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
534 return;
536 convert_eol(SC_EOL_CR);
540 void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
542 GeanyDocument *doc = document_get_current();
544 g_return_if_fail(doc != NULL);
546 editor_replace_tabs(doc->editor);
550 gboolean toolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
552 if (event->button == 3)
554 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
555 return TRUE;
557 return FALSE;
561 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
563 GeanyDocument *doc = document_get_current();
564 ScintillaObject *sci;
565 gchar *text;
566 gboolean keep_sel = TRUE;
568 g_return_if_fail(doc != NULL);
570 sci = doc->editor->sci;
571 if (! sci_has_selection(sci))
573 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
574 keep_sel = FALSE;
577 /* either we already had a selection or we created one for current word */
578 if (sci_has_selection(sci))
580 gchar *result = NULL;
581 gint cmd = SCI_LOWERCASE;
582 gboolean rectsel = (gboolean) scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
584 text = sci_get_selection_contents(sci);
586 if (utils_str_has_upper(text))
588 if (rectsel)
589 cmd = SCI_LOWERCASE;
590 else
591 result = g_utf8_strdown(text, -1);
593 else
595 if (rectsel)
596 cmd = SCI_UPPERCASE;
597 else
598 result = g_utf8_strup(text, -1);
601 if (result != NULL)
603 sci_replace_sel(sci, result);
604 g_free(result);
605 if (keep_sel)
606 sci_set_selection_start(sci, sci_get_current_position(sci) - strlen(text));
608 else
609 sci_send_command(sci, cmd);
611 g_free(text);
617 static void on_show_toolbar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
619 if (ignore_callback) return;
621 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
622 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
626 static void on_fullscreen1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
628 if (ignore_callback)
629 return;
631 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
632 ui_set_fullscreen();
636 static void on_show_messages_window1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
638 if (ignore_callback)
639 return;
641 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
642 msgwin_show_hide(ui_prefs.msgwindow_visible);
646 static void on_menu_color_schemes_activate(GtkImageMenuItem *imagemenuitem, gpointer user_data)
648 highlighting_show_color_scheme_dialog();
652 static void on_markers_margin1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
654 if (ignore_callback)
655 return;
657 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
658 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
662 static void on_show_line_numbers1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
664 if (ignore_callback)
665 return;
667 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
668 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
672 static void on_menu_show_white_space1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
674 if (ignore_callback)
675 return;
677 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
678 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
682 static void on_menu_show_line_endings1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
684 if (ignore_callback)
685 return;
687 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
688 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
692 static void on_menu_show_indentation_guides1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
694 if (ignore_callback)
695 return;
697 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
698 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
702 void on_line_wrapping1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
704 if (! ignore_callback)
706 GeanyDocument *doc = document_get_current();
707 g_return_if_fail(doc != NULL);
709 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
714 static void on_set_file_readonly1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
716 if (! ignore_callback)
718 GeanyDocument *doc = document_get_current();
719 g_return_if_fail(doc != NULL);
721 doc->readonly = ! doc->readonly;
722 sci_set_readonly(doc->editor->sci, doc->readonly);
723 ui_update_tab_status(doc);
724 ui_update_statusbar(doc, -1);
729 static void on_use_auto_indentation1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
731 if (! ignore_callback)
733 GeanyDocument *doc = document_get_current();
734 g_return_if_fail(doc != NULL);
736 doc->editor->auto_indent = ! doc->editor->auto_indent;
741 static void find_usage(gboolean in_session)
743 GeanyFindFlags flags;
744 gchar *search_text;
745 GeanyDocument *doc = document_get_current();
747 g_return_if_fail(doc != NULL);
749 if (sci_has_selection(doc->editor->sci))
750 { /* take selected text if there is a selection */
751 search_text = sci_get_selection_contents(doc->editor->sci);
752 flags = GEANY_FIND_MATCHCASE;
754 else
756 editor_find_current_word_sciwc(doc->editor, -1,
757 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
758 search_text = g_strdup(editor_info.current_word);
759 flags = GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD;
762 search_find_usage(search_text, search_text, flags, in_session);
763 g_free(search_text);
767 void on_find_document_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
769 find_usage(FALSE);
773 void on_find_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
775 find_usage(TRUE);
779 static void goto_tag(gboolean definition)
781 GeanyDocument *doc = document_get_current();
783 g_return_if_fail(doc != NULL);
785 /* update cursor pos for navigating back afterwards */
786 if (!sci_has_selection(doc->editor->sci))
787 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
789 /* use the keybinding callback as it checks for selections as well as current word */
790 if (definition)
791 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
792 else
793 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
797 static void on_goto_tag_definition1(GtkMenuItem *menuitem, gpointer user_data)
799 goto_tag(TRUE);
803 static void on_goto_tag_declaration1(GtkMenuItem *menuitem, gpointer user_data)
805 goto_tag(FALSE);
809 static void on_count_words1_activate(GtkMenuItem *menuitem, gpointer user_data)
811 tools_word_count();
815 void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data)
817 gchar colour[9];
818 GeanyDocument *doc = document_get_current();
819 gint pos;
821 g_return_if_fail(doc != NULL);
823 pos = sci_get_current_position(doc->editor->sci);
824 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
825 tools_color_chooser(colour);
829 void on_toolbutton_compile_clicked(GtkAction *action, gpointer user_data)
831 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
835 void on_find1_activate(GtkMenuItem *menuitem, gpointer user_data)
837 search_show_find_dialog();
841 void on_find_next1_activate(GtkMenuItem *menuitem, gpointer user_data)
843 search_find_again(FALSE);
847 void on_find_previous1_activate(GtkMenuItem *menuitem, gpointer user_data)
849 if (search_data.flags & GEANY_FIND_REGEXP)
850 /* Can't reverse search order for a regex (find next ignores search backwards) */
851 utils_beep();
852 else
853 search_find_again(TRUE);
857 void on_find_nextsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
859 search_find_selection(document_get_current(), FALSE);
863 void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
865 search_find_selection(document_get_current(), TRUE);
869 void on_replace1_activate(GtkMenuItem *menuitem, gpointer user_data)
871 search_show_replace_dialog();
875 void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
877 search_show_find_in_files_dialog(NULL);
881 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
883 if (*text == '+' || *text == '-')
885 *line_no = atoi(text + 1);
886 *offset = (*text == '+') ? 1 : -1;
888 else
890 *line_no = atoi(text) - 1;
891 *offset = 0;
896 void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
898 static gchar value[16] = "";
899 gchar *result;
901 result = dialogs_show_input_goto_line(
902 _("Go to Line"), GTK_WINDOW(main_widgets.window),
903 _("Enter the line you want to go to:"), value);
904 if (result != NULL)
906 GeanyDocument *doc = document_get_current();
907 gint offset;
908 gint line_no;
910 g_return_if_fail(doc != NULL);
912 get_line_and_offset_from_text(result, &line_no, &offset);
913 if (! editor_goto_line(doc->editor, line_no, offset))
914 utils_beep();
915 /* remember value for future calls */
916 g_snprintf(value, sizeof(value), "%s", result);
918 g_free(result);
923 void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
925 GeanyDocument *doc = document_get_current();
926 gint offset;
927 gint line_no;
929 g_return_if_fail(doc != NULL);
931 get_line_and_offset_from_text(text, &line_no, &offset);
932 if (! editor_goto_line(doc->editor, line_no, offset))
933 utils_beep();
934 else
935 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
939 void on_toolbutton_goto_clicked(GtkAction *action, gpointer user_data)
941 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
943 if (entry != NULL)
945 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
947 on_toolbutton_goto_entry_activate(NULL, text, NULL);
949 else
950 on_go_to_line_activate(NULL, NULL);
954 void on_help1_activate(GtkMenuItem *menuitem, gpointer user_data)
956 gchar *uri;
958 uri = utils_get_help_url(NULL);
959 utils_open_browser(uri);
960 g_free(uri);
964 static void on_help_shortcuts1_activate(GtkMenuItem *menuitem, gpointer user_data)
966 keybindings_show_shortcuts();
970 static void on_website1_activate(GtkMenuItem *menuitem, gpointer user_data)
972 utils_open_browser(GEANY_HOMEPAGE);
976 static void on_help_menu_item_donate_activate(GtkMenuItem *item, gpointer user_data)
978 utils_open_browser(GEANY_DONATE);
982 static void on_help_menu_item_wiki_activate(GtkMenuItem *item, gpointer user_data)
984 utils_open_browser(GEANY_WIKI);
988 static void on_help_menu_item_bug_report_activate(GtkMenuItem *item, gpointer user_data)
990 utils_open_browser(GEANY_BUG_REPORT);
994 static void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data)
996 GeanyDocument *doc = document_get_current();
997 gchar *text;
998 const gchar *cur_tag = NULL;
999 gint line = -1, pos = 0;
1001 if (doc == NULL || doc->file_type == NULL)
1003 ui_set_statusbar(FALSE,
1004 _("Please set the filetype for the current file before using this function."));
1005 return;
1008 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
1009 * returns the current position, so it should be safe */
1010 line = symbols_get_current_function(doc, &cur_tag);
1011 pos = sci_get_position_from_line(doc->editor->sci, line);
1013 text = templates_get_template_function(doc, cur_tag);
1015 sci_start_undo_action(doc->editor->sci);
1016 sci_insert_text(doc->editor->sci, pos, text);
1017 sci_end_undo_action(doc->editor->sci);
1018 g_free(text);
1022 static void insert_multiline_comment(GeanyDocument *doc, gint pos)
1024 g_return_if_fail(doc != NULL);
1025 g_return_if_fail(pos == -1 || pos >= 0);
1027 if (doc->file_type == NULL)
1029 ui_set_statusbar(FALSE,
1030 _("Please set the filetype for the current file before using this function."));
1031 return;
1034 if (doc->file_type->comment_open || doc->file_type->comment_single)
1036 /* editor_insert_multiline_comment() uses editor_info.click_pos */
1037 if (pos == -1)
1038 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
1039 else
1040 editor_info.click_pos = pos;
1041 editor_insert_multiline_comment(doc->editor);
1043 else
1044 utils_beep();
1048 static void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1050 insert_multiline_comment(document_get_current(), editor_info.click_pos);
1054 static void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1056 insert_multiline_comment(document_get_current(), -1);
1060 static void insert_comment_template(GeanyDocument *doc, gint pos, guint template)
1062 gchar *text;
1064 g_return_if_fail(doc != NULL);
1065 g_return_if_fail(pos == -1 || pos >= 0);
1066 g_return_if_fail(template < GEANY_MAX_TEMPLATES);
1068 if (pos == -1)
1069 pos = sci_get_current_position(doc->editor->sci);
1071 text = templates_get_template_licence(doc, template);
1073 sci_start_undo_action(doc->editor->sci);
1074 sci_insert_text(doc->editor->sci, pos, text);
1075 sci_end_undo_action(doc->editor->sci);
1076 g_free(text);
1080 static void on_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1082 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_GPL);
1086 static void on_menu_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1088 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_GPL);
1092 static void on_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1094 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_BSD);
1098 static void on_menu_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1100 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_BSD);
1104 static void on_comments_changelog_activate(GtkMenuItem *menuitem, gpointer user_data)
1106 GeanyDocument *doc = document_get_current();
1107 gchar *text;
1109 g_return_if_fail(doc != NULL);
1111 text = templates_get_template_changelog(doc);
1112 sci_start_undo_action(doc->editor->sci);
1113 sci_insert_text(doc->editor->sci, 0, text);
1114 /* sets the cursor to the right position to type the changelog text,
1115 * the template has 21 chars + length of name and email */
1116 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1117 sci_end_undo_action(doc->editor->sci);
1119 g_free(text);
1123 static void on_comments_fileheader_activate(GtkMenuItem *menuitem, gpointer user_data)
1125 GeanyDocument *doc = document_get_current();
1126 gchar *text;
1127 const gchar *fname;
1128 GeanyFiletype *ft;
1130 g_return_if_fail(doc != NULL);
1132 ft = doc->file_type;
1133 fname = doc->file_name;
1134 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1136 sci_start_undo_action(doc->editor->sci);
1137 sci_insert_text(doc->editor->sci, 0, text);
1138 sci_goto_pos(doc->editor->sci, 0, FALSE);
1139 sci_end_undo_action(doc->editor->sci);
1140 g_free(text);
1144 static void on_file_properties_activate(GtkMenuItem *menuitem, gpointer user_data)
1146 GeanyDocument *doc = document_get_current();
1147 g_return_if_fail(doc != NULL);
1149 dialogs_show_file_properties(doc);
1153 static void on_menu_fold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1155 GeanyDocument *doc = document_get_current();
1156 g_return_if_fail(doc != NULL);
1158 editor_fold_all(doc->editor);
1162 static void on_menu_unfold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1164 GeanyDocument *doc = document_get_current();
1165 g_return_if_fail(doc != NULL);
1167 editor_unfold_all(doc->editor);
1171 void on_toolbutton_run_clicked(GtkAction *action, gpointer user_data)
1173 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1177 void on_menu_remove_indicators1_activate(GtkMenuItem *menuitem, gpointer user_data)
1179 GeanyDocument *doc = document_get_current();
1180 g_return_if_fail(doc != NULL);
1182 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1186 void on_print1_activate(GtkMenuItem *menuitem, gpointer user_data)
1188 GeanyDocument *doc = document_get_current();
1189 g_return_if_fail(doc != NULL);
1191 printing_print_doc(doc);
1195 void on_menu_select_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1197 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1199 /* special case for Select All in the scribble widget */
1200 if (GTK_IS_TEXT_VIEW(focusw))
1202 g_signal_emit_by_name(focusw, "select-all", TRUE);
1204 /* special case for Select All in the VTE widget */
1205 #ifdef HAVE_VTE
1206 else if (vte_info.have_vte && focusw == vc->vte)
1208 vte_select_all();
1210 #endif
1211 else if (GTK_IS_EDITABLE(focusw))
1213 gtk_editable_select_region(GTK_EDITABLE(focusw), 0, -1);
1215 else if (IS_SCINTILLA(focusw))
1217 sci_select_all(SCINTILLA(focusw));
1222 void on_menu_show_sidebar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1224 if (ignore_callback)
1225 return;
1227 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1229 /* show built-in tabs if no tabs visible */
1230 if (ui_prefs.sidebar_visible &&
1231 ! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1232 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1234 interface_prefs.sidebar_openfiles_visible = TRUE;
1235 interface_prefs.sidebar_symbol_visible = TRUE;
1238 /* if window has input focus, set it back to the editor before toggling off */
1239 if (! ui_prefs.sidebar_visible &&
1240 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1242 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1245 ui_sidebar_show_hide();
1249 static void on_menu_write_unicode_bom1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1251 if (! ignore_callback)
1253 GeanyDocument *doc = document_get_current();
1255 g_return_if_fail(doc != NULL);
1256 if (doc->readonly)
1258 utils_beep();
1259 return;
1262 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1264 doc->has_bom = ! doc->has_bom;
1266 ui_update_statusbar(doc, -1);
1271 void on_menu_comment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1273 GeanyDocument *doc = document_get_current();
1274 g_return_if_fail(doc != NULL);
1276 editor_do_comment(doc->editor, -1, FALSE, FALSE, TRUE);
1280 void on_menu_uncomment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1282 GeanyDocument *doc = document_get_current();
1283 g_return_if_fail(doc != NULL);
1285 editor_do_uncomment(doc->editor, -1, FALSE);
1289 void on_menu_toggle_line_commentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1291 GeanyDocument *doc = document_get_current();
1292 g_return_if_fail(doc != NULL);
1294 editor_do_comment_toggle(doc->editor);
1298 void on_menu_increase_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1300 GeanyDocument *doc = document_get_current();
1301 g_return_if_fail(doc != NULL);
1303 editor_indent(doc->editor, TRUE);
1307 void on_menu_decrease_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1309 GeanyDocument *doc = document_get_current();
1310 g_return_if_fail(doc != NULL);
1312 editor_indent(doc->editor, FALSE);
1316 void on_next_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1318 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1319 msgwin_goto_messages_file_line))
1320 ui_set_statusbar(FALSE, _("No more message items."));
1324 void on_previous_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1326 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1327 msgwin_goto_messages_file_line))
1328 ui_set_statusbar(FALSE, _("No more message items."));
1332 void on_project_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
1334 project_new();
1338 void on_project_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
1340 project_open();
1344 void on_project_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
1346 project_close(TRUE);
1350 void on_project_properties1_activate(GtkMenuItem *menuitem, gpointer user_data)
1352 project_properties();
1356 static void on_menu_project1_activate(GtkMenuItem *menuitem, gpointer user_data)
1358 static GtkWidget *item_close = NULL;
1359 static GtkWidget *item_properties = NULL;
1361 if (item_close == NULL)
1363 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1364 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1367 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1368 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1369 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1370 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1374 void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
1376 GeanyDocument *doc = document_get_current();
1377 gchar *sel = NULL;
1378 const gchar *wc;
1380 #ifdef G_OS_WIN32
1381 wc = GEANY_WORDCHARS "./-" "\\";
1382 #else
1383 wc = GEANY_WORDCHARS "./-";
1384 #endif
1386 g_return_if_fail(doc != NULL);
1388 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1389 SETPTR(sel, utils_get_locale_from_utf8(sel));
1391 if (sel != NULL)
1393 gchar *filename = NULL;
1395 if (g_path_is_absolute(sel))
1396 filename = g_strdup(sel);
1397 else
1398 { /* relative filename, add the path of the current file */
1399 gchar *path;
1401 path = utils_get_current_file_dir_utf8();
1402 SETPTR(path, utils_get_locale_from_utf8(path));
1403 if (!path)
1404 path = g_get_current_dir();
1406 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1408 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1409 app->project != NULL && !EMPTY(app->project->base_path))
1411 /* try the project's base path */
1412 SETPTR(path, project_get_base_path());
1413 SETPTR(path, utils_get_locale_from_utf8(path));
1414 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1416 g_free(path);
1417 #ifdef G_OS_UNIX
1418 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1419 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));
1421 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1422 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
1423 #endif
1426 if (g_file_test(filename, G_FILE_TEST_EXISTS))
1427 document_open_file(filename, FALSE, NULL, NULL);
1428 else
1430 SETPTR(sel, utils_get_utf8_from_locale(sel));
1431 ui_set_statusbar(TRUE, _("Could not open file %s (File not found)"), sel);
1434 g_free(filename);
1435 g_free(sel);
1440 void on_remove_markers1_activate(GtkMenuItem *menuitem, gpointer user_data)
1442 GeanyDocument *doc = document_get_current();
1443 g_return_if_fail(doc != NULL);
1445 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1446 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1447 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1451 static void on_load_tags1_activate(GtkMenuItem *menuitem, gpointer user_data)
1453 symbols_show_load_tags_dialog();
1457 void on_context_action1_activate(GtkMenuItem *menuitem, gpointer user_data)
1459 gchar *word, *command;
1460 GError *error = NULL;
1461 GeanyDocument *doc = document_get_current();
1463 g_return_if_fail(doc != NULL);
1465 if (sci_has_selection(doc->editor->sci))
1466 { /* take selected text if there is a selection */
1467 word = sci_get_selection_contents(doc->editor->sci);
1469 else
1471 word = g_strdup(editor_info.current_word);
1474 /* use the filetype specific command if available, fallback to global command otherwise */
1475 if (doc->file_type != NULL &&
1476 !EMPTY(doc->file_type->context_action_cmd))
1478 command = g_strdup(doc->file_type->context_action_cmd);
1480 else
1482 command = g_strdup(tool_prefs.context_action_cmd);
1485 /* substitute the wildcard %s and run the command if it is non empty */
1486 if (G_LIKELY(!EMPTY(command)))
1488 utils_str_replace_all(&command, "%s", word);
1490 if (!spawn_async(NULL, command, NULL, NULL, NULL, &error))
1492 ui_set_statusbar(TRUE, "Context action command failed: %s", error->message);
1493 g_error_free(error);
1496 g_free(word);
1497 g_free(command);
1501 void on_menu_toggle_all_additional_widgets1_activate(GtkMenuItem *menuitem, gpointer user_data)
1503 static gint hide_all = -1;
1504 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1505 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1506 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1507 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1509 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1510 if (G_UNLIKELY(hide_all == -1))
1512 if (! gtk_check_menu_item_get_active(msgw) &&
1513 ! interface_prefs.show_notebook_tabs &&
1514 ! gtk_check_menu_item_get_active(toolbari))
1516 hide_all = TRUE;
1518 else
1519 hide_all = FALSE;
1522 hide_all = ! hide_all; /* toggle */
1524 if (hide_all)
1526 if (gtk_check_menu_item_get_active(msgw))
1527 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1529 interface_prefs.show_notebook_tabs = FALSE;
1530 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1532 ui_statusbar_showhide(FALSE);
1534 if (gtk_check_menu_item_get_active(toolbari))
1535 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1537 else
1540 if (! gtk_check_menu_item_get_active(msgw))
1541 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1543 interface_prefs.show_notebook_tabs = TRUE;
1544 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1546 ui_statusbar_showhide(TRUE);
1548 if (! gtk_check_menu_item_get_active(toolbari))
1549 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1554 void on_toolbutton_forward_activate(GtkAction *menuitem, gpointer user_data)
1556 navqueue_go_forward();
1560 void on_toolbutton_back_activate(GtkAction *menuitem, gpointer user_data)
1562 navqueue_go_back();
1566 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1568 if (prefs.auto_focus && ! gtk_widget_has_focus(widget))
1569 gtk_widget_grab_focus(widget);
1571 return FALSE;
1575 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1577 GeanyDocument *doc;
1579 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1580 return;
1582 doc = document_get_current();
1583 g_return_if_fail(doc != NULL);
1585 editor_set_indent(doc->editor, type, doc->editor->indent_width);
1586 ui_update_statusbar(doc, -1);
1590 static void on_tabs1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1592 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1596 static void on_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1598 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
1602 static void on_tabs_and_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1604 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
1608 static void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer user_data)
1610 GeanyDocument *doc;
1612 if (ignore_callback)
1613 return;
1615 doc = document_get_current();
1616 g_return_if_fail(doc != NULL);
1618 editor_strip_trailing_spaces(doc->editor);
1622 static void on_page_setup1_activate(GtkMenuItem *menuitem, gpointer user_data)
1624 printing_page_setup_gtk();
1628 gboolean on_escape_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1630 guint state = keybindings_get_modifiers(event->state);
1632 /* make pressing escape in the sidebar and toolbar focus the editor */
1633 if (event->keyval == GDK_Escape && state == 0)
1635 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1636 return TRUE;
1638 return FALSE;
1642 void on_line_breaking1_activate(GtkMenuItem *menuitem, gpointer user_data)
1644 GeanyDocument *doc;
1646 if (ignore_callback)
1647 return;
1649 doc = document_get_current();
1650 g_return_if_fail(doc != NULL);
1652 doc->editor->line_breaking = !doc->editor->line_breaking;
1656 void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
1658 GeanyDocument *doc = document_get_current();
1660 g_return_if_fail(doc != NULL);
1662 editor_replace_spaces(doc->editor);
1666 static void on_search1_activate(GtkMenuItem *menuitem, gpointer user_data)
1668 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
1669 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
1670 gboolean have_messages;
1672 /* enable commands if the messages window has any items */
1673 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
1674 NULL) > 0;
1676 gtk_widget_set_sensitive(next_message, have_messages);
1677 gtk_widget_set_sensitive(previous_message, have_messages);
1681 /* simple implementation (vs. close all which doesn't close documents if cancelled),
1682 * if user_data is set, it is the GeanyDocument to keep */
1683 void on_close_other_documents1_activate(GtkMenuItem *menuitem, gpointer user_data)
1685 guint i;
1686 GeanyDocument *cur_doc = user_data;
1688 if (cur_doc == NULL)
1689 cur_doc = document_get_current();
1691 for (i = 0; i < documents_array->len; i++)
1693 GeanyDocument *doc = documents[i];
1695 if (doc == cur_doc || ! doc->is_valid)
1696 continue;
1698 if (! document_close(doc))
1699 break;
1704 static void on_menu_reload_configuration1_activate(GtkMenuItem *menuitem, gpointer user_data)
1706 main_reload_configuration();
1710 static void on_debug_messages1_activate(GtkMenuItem *menuitem, gpointer user_data)
1712 log_show_debug_messages_dialog();
1716 void on_send_selection_to_vte1_activate(GtkMenuItem *menuitem, gpointer user_data)
1718 #ifdef HAVE_VTE
1719 if (vte_info.have_vte)
1720 vte_send_selection_to_vte();
1721 #endif
1725 static gboolean on_window_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
1728 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1730 static GtkWidget *menuitem = NULL;
1732 if (menuitem == NULL)
1733 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
1735 ignore_callback = TRUE;
1737 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
1738 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
1740 ignore_callback = FALSE;
1742 return FALSE;
1746 static void show_notebook_page(const gchar *notebook_name, const gchar *page_name)
1748 GtkWidget *widget;
1749 GtkNotebook *notebook;
1751 widget = ui_lookup_widget(ui_widgets.prefs_dialog, page_name);
1752 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, notebook_name));
1754 if (notebook != NULL && widget != NULL)
1755 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
1759 static void on_customize_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
1761 prefs_show_dialog();
1763 /* select the Interface page */
1764 show_notebook_page("notebook2", "notebook6");
1765 /* select the Toolbar subpage */
1766 show_notebook_page("notebook6", "vbox15");
1770 static void on_button_customize_toolbar_clicked(GtkButton *button, gpointer user_data)
1772 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
1776 static void on_cut_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1778 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
1782 static void on_copy_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1784 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
1788 static void on_delete_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1790 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
1794 static void on_duplicate_line_or_selection1_activate(GtkMenuItem *menuitem, gpointer user_data)
1796 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
1800 static void on_select_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1802 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
1806 static void on_select_current_paragraph1_activate(GtkMenuItem *menuitem, gpointer user_data)
1808 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
1812 static void on_insert_alternative_white_space1_activate(GtkMenuItem *menuitem, gpointer user_data)
1814 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
1818 static void on_go_to_next_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1820 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
1824 static void on_go_to_previous_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1826 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
1830 static void on_reflow_lines_block1_activate(GtkMenuItem *menuitem, gpointer user_data)
1832 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
1836 static void on_move_lines_up1_activate(GtkMenuItem *menuitem, gpointer user_data)
1838 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEUP);
1842 static void on_move_lines_down1_activate(GtkMenuItem *menuitem, gpointer user_data)
1844 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEDOWN);
1848 static void on_smart_line_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1850 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
1854 void on_plugin_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
1856 #ifdef HAVE_PLUGINS
1857 plugin_show_configure(NULL);
1858 #endif
1862 static void on_indent_width_activate(GtkMenuItem *menuitem, gpointer user_data)
1864 GeanyDocument *doc;
1865 gchar *label;
1866 gint width;
1868 if (ignore_callback)
1869 return;
1871 label = ui_menu_item_get_text(menuitem);
1872 width = atoi(label);
1873 g_free(label);
1875 doc = document_get_current();
1876 if (doc != NULL && width > 0)
1877 editor_set_indent_width(doc->editor, width);
1881 static void on_reset_indentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1883 guint i;
1885 foreach_document(i)
1886 document_apply_indent_settings(documents[i]);
1888 ui_update_statusbar(NULL, -1);
1889 ui_document_show_hide(NULL);
1893 static void on_mark_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1895 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
1899 static void on_detect_type_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1901 GeanyDocument *doc = document_get_current();
1902 GeanyIndentType type;
1904 if (doc != NULL && document_detect_indent_type(doc, &type))
1906 editor_set_indent_type(doc->editor, type);
1907 ui_document_show_hide(doc);
1912 static void on_detect_width_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1914 GeanyDocument *doc = document_get_current();
1915 gint width;
1917 if (doc != NULL && document_detect_indent_width(doc, &width))
1919 editor_set_indent_width(doc->editor, width);
1920 ui_document_show_hide(doc);
1925 static void builder_connect_func(GtkBuilder *builder, GObject *object,
1926 const gchar *signal_name, const gchar *handler_name, GObject *connect_obj,
1927 GConnectFlags flags, gpointer user_data)
1929 GHashTable *hash = user_data;
1930 GCallback callback;
1932 callback = g_hash_table_lookup(hash, handler_name);
1933 g_return_if_fail(callback);
1935 if (connect_obj == NULL)
1936 g_signal_connect_data(object, signal_name, callback, NULL, NULL, flags);
1937 else
1938 g_signal_connect_object(object, signal_name, callback, connect_obj, flags);
1942 void callbacks_connect(GtkBuilder *builder)
1944 GHashTable *hash;
1946 g_return_if_fail(GTK_IS_BUILDER(builder));
1948 hash = g_hash_table_new(g_str_hash, g_str_equal);
1950 #define ITEM(n) g_hash_table_insert(hash, (gpointer) #n, G_CALLBACK(n));
1951 # include "signallist.i"
1952 #undef ITEM
1954 gtk_builder_connect_signals_full(builder, builder_connect_func, hash);
1955 g_hash_table_destroy(hash);