Simplify automated signal connection
[geany-mirror.git] / src / callbacks.c
blob1e7f546f471ad230d8cd2739af6b51193a59b9db
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 #ifdef HAVE_SOCKET
55 # include "socket.h"
56 #endif
57 #include "support.h"
58 #include "symbols.h"
59 #include "templates.h"
60 #include "toolbar.h"
61 #include "tools.h"
62 #include "ui_utils.h"
63 #include "utils.h"
64 #include "vte.h"
66 #include "gtkcompat.h"
68 #include <stdlib.h>
69 #include <unistd.h>
70 #include <string.h>
71 #include <gdk/gdkkeysyms.h>
72 #include <glib/gstdio.h>
73 #include <time.h>
76 /* represents the state at switching a notebook page(in the left treeviews widget), to not emit
77 * the selection-changed signal from tv.tree_openfiles */
78 /*static gboolean switch_tv_notebook_page = FALSE; */
82 /* wrapper function to abort exit process if cancel button is pressed */
83 static gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event, gpointer gdata)
85 return !main_quit();
90 * GUI callbacks
93 void on_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
95 document_new_file(NULL, NULL, NULL);
99 /* create a new file and copy file content and properties */
100 static void on_clone1_activate(GtkMenuItem *menuitem, gpointer user_data)
102 GeanyDocument *old_doc = document_get_current();
104 if (old_doc)
105 document_clone(old_doc);
109 void on_save1_activate(GtkMenuItem *menuitem, gpointer user_data)
111 GeanyDocument *doc = document_get_current();
113 if (doc != NULL)
115 document_save_file(doc, ui_prefs.allow_always_save);
120 void on_save_as1_activate(GtkMenuItem *menuitem, gpointer user_data)
122 dialogs_show_save_as();
126 void on_save_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
128 guint i, max = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
129 GeanyDocument *doc, *cur_doc = document_get_current();
130 guint count = 0;
132 /* iterate over documents in tabs order */
133 for (i = 0; i < max; i++)
135 doc = document_get_from_page(i);
136 if (! doc->changed)
137 continue;
139 if (document_save_file(doc, FALSE))
140 count++;
142 if (!count)
143 return;
145 ui_set_statusbar(FALSE, ngettext("%d file saved.", "%d files saved.", count), count);
146 /* saving may have changed window title, sidebar for another doc, so update */
147 document_show_tab(cur_doc);
148 sidebar_update_tag_list(cur_doc, TRUE);
149 ui_set_window_title(cur_doc);
153 void on_close_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
155 document_close_all();
159 void on_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
161 GeanyDocument *doc = document_get_current();
163 if (doc != NULL)
164 document_close(doc);
168 void on_quit1_activate(GtkMenuItem *menuitem, gpointer user_data)
170 main_quit();
174 static void on_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
176 gtk_widget_set_sensitive(ui_widgets.recent_files_menuitem,
177 g_queue_get_length(ui_prefs.recent_queue) > 0);
178 /* hide Page setup when GTK printing is not used */
179 ui_widget_show_hide(ui_widgets.print_page_setup, printing_prefs.use_gtk_printing);
183 /* edit actions, c&p & co, from menu bar and from popup menu */
184 static void on_edit1_activate(GtkMenuItem *menuitem, gpointer user_data)
186 GtkWidget *item;
187 GeanyDocument *doc = document_get_current();
189 ui_update_menu_copy_items(doc);
190 ui_update_insert_include_item(doc, 1);
192 item = ui_lookup_widget(main_widgets.window, "plugin_preferences1");
193 #ifndef HAVE_PLUGINS
194 gtk_widget_hide(item);
195 #else
196 gtk_widget_set_sensitive(item, plugins_have_preferences());
197 #endif
201 void on_undo1_activate(GtkMenuItem *menuitem, gpointer user_data)
203 GeanyDocument *doc = document_get_current();
205 g_return_if_fail(doc != NULL);
207 if (document_can_undo(doc))
209 sci_cancel(doc->editor->sci);
210 document_undo(doc);
215 void on_redo1_activate(GtkMenuItem *menuitem, gpointer user_data)
217 GeanyDocument *doc = document_get_current();
219 g_return_if_fail(doc != NULL);
221 if (document_can_redo(doc))
223 sci_cancel(doc->editor->sci);
224 document_redo(doc);
229 void on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
231 GeanyDocument *doc = document_get_current();
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
237 if (IS_SCINTILLA(focusw) && doc != NULL)
238 sci_cut(doc->editor->sci);
239 else
240 if (GTK_IS_TEXT_VIEW(focusw))
242 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
243 GTK_TEXT_VIEW(focusw));
244 gtk_text_buffer_cut_clipboard(buffer, gtk_clipboard_get(GDK_NONE), TRUE);
249 void on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
251 GeanyDocument *doc = document_get_current();
252 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
254 if (GTK_IS_EDITABLE(focusw))
255 gtk_editable_copy_clipboard(GTK_EDITABLE(focusw));
256 else
257 if (IS_SCINTILLA(focusw) && doc != NULL)
258 sci_copy(doc->editor->sci);
259 else
260 if (GTK_IS_TEXT_VIEW(focusw))
262 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
263 GTK_TEXT_VIEW(focusw));
264 gtk_text_buffer_copy_clipboard(buffer, gtk_clipboard_get(GDK_NONE));
269 void on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
271 GeanyDocument *doc = document_get_current();
272 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
274 if (GTK_IS_EDITABLE(focusw))
275 gtk_editable_paste_clipboard(GTK_EDITABLE(focusw));
276 else
277 if (IS_SCINTILLA(focusw) && doc != NULL)
279 sci_paste(doc->editor->sci);
281 else
282 if (GTK_IS_TEXT_VIEW(focusw))
284 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
285 GTK_TEXT_VIEW(focusw));
286 gtk_text_buffer_paste_clipboard(buffer, gtk_clipboard_get(GDK_NONE), NULL,
287 TRUE);
292 void on_delete1_activate(GtkMenuItem *menuitem, gpointer user_data)
294 GeanyDocument *doc = document_get_current();
295 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
297 if (GTK_IS_EDITABLE(focusw))
298 gtk_editable_delete_selection(GTK_EDITABLE(focusw));
299 else
300 if (IS_SCINTILLA(focusw) && doc != NULL && sci_has_selection(doc->editor->sci))
301 sci_clear(doc->editor->sci);
302 else
303 if (GTK_IS_TEXT_VIEW(focusw))
305 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
306 GTK_TEXT_VIEW(focusw));
307 gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
312 void on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
314 prefs_show_dialog();
318 /* about menu item */
319 static void on_info1_activate(GtkMenuItem *menuitem, gpointer user_data)
321 about_dialog_show();
325 /* open file */
326 void on_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
328 dialogs_show_open_file();
332 /* reload file */
333 void on_toolbutton_reload_clicked(GtkAction *action, gpointer user_data)
335 GeanyDocument *doc = document_get_current();
337 g_return_if_fail(doc != NULL);
339 document_reload_prompt(doc, NULL);
343 static void on_change_font1_activate(GtkMenuItem *menuitem, gpointer user_data)
345 dialogs_show_open_font();
349 /* store text, clear search flags so we can use Search->Find Next/Previous */
350 static void setup_find(const gchar *text, gboolean backwards)
352 SETPTR(search_data.text, g_strdup(text));
353 SETPTR(search_data.original_text, g_strdup(text));
354 search_data.flags = 0;
355 search_data.backwards = backwards;
356 search_data.search_bar = TRUE;
360 static void do_toolbar_search(const gchar *text, gboolean incremental, gboolean backwards)
362 GeanyDocument *doc = document_get_current();
363 gboolean result;
365 setup_find(text, backwards);
366 result = document_search_bar_find(doc, search_data.text, incremental, backwards);
367 if (search_data.search_bar)
368 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
372 /* search text */
373 void on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
375 do_toolbar_search(text, TRUE, FALSE);
379 /* search text */
380 void on_toolbar_search_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
382 do_toolbar_search(text, FALSE, GPOINTER_TO_INT(user_data));
386 /* search text */
387 void on_toolbutton_search_clicked(GtkAction *action, gpointer user_data)
389 GeanyDocument *doc = document_get_current();
390 gboolean result;
391 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
393 if (entry != NULL)
395 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
397 setup_find(text, FALSE);
398 result = document_search_bar_find(doc, search_data.text, FALSE, FALSE);
399 if (search_data.search_bar)
400 ui_set_search_entry_background(entry, result);
402 else
403 on_find1_activate(NULL, NULL);
407 /* hides toolbar from toolbar popup menu */
408 static void on_hide_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
410 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
411 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
415 /* zoom in from menu bar and popup menu */
416 void on_zoom_in1_activate(GtkMenuItem *menuitem, gpointer user_data)
418 GeanyDocument *doc = document_get_current();
420 g_return_if_fail(doc != NULL);
422 sci_zoom_in(doc->editor->sci);
426 /* zoom out from menu bar and popup menu */
427 void on_zoom_out1_activate(GtkMenuItem *menuitem, gpointer user_data)
429 GeanyDocument *doc = document_get_current();
431 g_return_if_fail(doc != NULL);
433 sci_zoom_out(doc->editor->sci);
437 void on_normal_size1_activate(GtkMenuItem *menuitem, gpointer user_data)
439 GeanyDocument *doc = document_get_current();
441 g_return_if_fail(doc != NULL);
443 sci_zoom_off(doc->editor->sci);
447 static gboolean delayed_check_disk_status(gpointer data)
449 document_check_disk_status(data, FALSE);
450 return FALSE;
454 /* Changes window-title after switching tabs and lots of other things.
455 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
456 static void on_notebook1_switch_page_after(GtkNotebook *notebook, gpointer page,
457 guint page_num, gpointer user_data)
459 GeanyDocument *doc;
461 if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
462 return;
464 doc = document_get_from_notebook_child(page);
466 if (doc != NULL)
468 sidebar_select_openfiles_item(doc);
469 ui_save_buttons_toggle(doc->changed);
470 ui_set_window_title(doc);
471 ui_update_statusbar(doc, -1);
472 ui_update_popup_reundo_items(doc);
473 ui_document_show_hide(doc); /* update the document menu */
474 build_menu_update(doc);
475 sidebar_update_tag_list(doc, FALSE);
476 document_highlight_tags(doc);
478 /* We delay the check to avoid weird fast, unintended switching of notebook pages when
479 * the 'file has changed' dialog is shown while the switch event is not yet completely
480 * finished. So, we check after the switch has been performed to be safe. */
481 g_idle_add(delayed_check_disk_status, doc);
483 #ifdef HAVE_VTE
484 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
485 #endif
487 g_signal_emit_by_name(geany_object, "document-activate", doc);
492 static void on_tv_notebook_switch_page(GtkNotebook *notebook, gpointer page,
493 guint page_num, gpointer user_data)
495 /* suppress selection changed signal when switching to the open files list */
496 ignore_callback = TRUE;
500 static void on_tv_notebook_switch_page_after(GtkNotebook *notebook, gpointer page,
501 guint page_num, gpointer user_data)
503 ignore_callback = FALSE;
507 static void convert_eol(gint mode)
509 GeanyDocument *doc = document_get_current();
511 g_return_if_fail(doc != NULL);
513 sci_convert_eols(doc->editor->sci, mode);
514 sci_set_eol_mode(doc->editor->sci, mode);
515 ui_update_statusbar(doc, -1);
519 static void on_crlf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
521 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
522 return;
524 convert_eol(SC_EOL_CRLF);
528 static void on_lf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
530 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
531 return;
533 convert_eol(SC_EOL_LF);
537 static void on_cr_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
539 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
540 return;
542 convert_eol(SC_EOL_CR);
546 void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
548 GeanyDocument *doc = document_get_current();
550 g_return_if_fail(doc != NULL);
552 editor_replace_tabs(doc->editor);
556 gboolean toolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
558 if (event->button == 3)
560 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
561 return TRUE;
563 return FALSE;
567 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
569 GeanyDocument *doc = document_get_current();
570 ScintillaObject *sci;
571 gchar *text;
572 gboolean keep_sel = TRUE;
574 g_return_if_fail(doc != NULL);
576 sci = doc->editor->sci;
577 if (! sci_has_selection(sci))
579 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
580 keep_sel = FALSE;
583 /* either we already had a selection or we created one for current word */
584 if (sci_has_selection(sci))
586 gchar *result = NULL;
587 gint cmd = SCI_LOWERCASE;
588 gboolean rectsel = (gboolean) scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
590 text = sci_get_selection_contents(sci);
592 if (utils_str_has_upper(text))
594 if (rectsel)
595 cmd = SCI_LOWERCASE;
596 else
597 result = g_utf8_strdown(text, -1);
599 else
601 if (rectsel)
602 cmd = SCI_UPPERCASE;
603 else
604 result = g_utf8_strup(text, -1);
607 if (result != NULL)
609 sci_replace_sel(sci, result);
610 g_free(result);
611 if (keep_sel)
612 sci_set_selection_start(sci, sci_get_current_position(sci) - strlen(text));
614 else
615 sci_send_command(sci, cmd);
617 g_free(text);
623 static void on_show_toolbar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
625 if (ignore_callback) return;
627 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
628 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
632 static void on_fullscreen1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
634 if (ignore_callback)
635 return;
637 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
638 ui_set_fullscreen();
642 static void on_show_messages_window1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
644 if (ignore_callback)
645 return;
647 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
648 msgwin_show_hide(ui_prefs.msgwindow_visible);
652 static void on_menu_color_schemes_activate(GtkImageMenuItem *imagemenuitem, gpointer user_data)
654 highlighting_show_color_scheme_dialog();
658 static void on_markers_margin1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
660 if (ignore_callback)
661 return;
663 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
664 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
668 static void on_show_line_numbers1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
670 if (ignore_callback)
671 return;
673 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
674 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
678 static void on_menu_show_white_space1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
680 if (ignore_callback)
681 return;
683 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
684 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
688 static void on_menu_show_line_endings1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
690 if (ignore_callback)
691 return;
693 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
694 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
698 static void on_menu_show_indentation_guides1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
700 if (ignore_callback)
701 return;
703 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
704 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
708 void on_line_wrapping1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
710 if (! ignore_callback)
712 GeanyDocument *doc = document_get_current();
713 g_return_if_fail(doc != NULL);
715 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
720 static void on_set_file_readonly1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
722 if (! ignore_callback)
724 GeanyDocument *doc = document_get_current();
725 g_return_if_fail(doc != NULL);
727 doc->readonly = ! doc->readonly;
728 sci_set_readonly(doc->editor->sci, doc->readonly);
729 ui_update_tab_status(doc);
730 ui_update_statusbar(doc, -1);
735 static void on_use_auto_indentation1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
737 if (! ignore_callback)
739 GeanyDocument *doc = document_get_current();
740 g_return_if_fail(doc != NULL);
742 doc->editor->auto_indent = ! doc->editor->auto_indent;
747 static void find_usage(gboolean in_session)
749 GeanyFindFlags flags;
750 gchar *search_text;
751 GeanyDocument *doc = document_get_current();
753 g_return_if_fail(doc != NULL);
755 if (sci_has_selection(doc->editor->sci))
756 { /* take selected text if there is a selection */
757 search_text = sci_get_selection_contents(doc->editor->sci);
758 flags = GEANY_FIND_MATCHCASE;
760 else
762 editor_find_current_word_sciwc(doc->editor, -1,
763 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
764 search_text = g_strdup(editor_info.current_word);
765 flags = GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD;
768 search_find_usage(search_text, search_text, flags, in_session);
769 g_free(search_text);
773 void on_find_document_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
775 find_usage(FALSE);
779 void on_find_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
781 find_usage(TRUE);
785 static void goto_tag(gboolean definition)
787 GeanyDocument *doc = document_get_current();
789 g_return_if_fail(doc != NULL);
791 /* update cursor pos for navigating back afterwards */
792 if (!sci_has_selection(doc->editor->sci))
793 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
795 /* use the keybinding callback as it checks for selections as well as current word */
796 if (definition)
797 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
798 else
799 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
803 static void on_goto_tag_definition1(GtkMenuItem *menuitem, gpointer user_data)
805 goto_tag(TRUE);
809 static void on_goto_tag_declaration1(GtkMenuItem *menuitem, gpointer user_data)
811 goto_tag(FALSE);
815 static void on_count_words1_activate(GtkMenuItem *menuitem, gpointer user_data)
817 tools_word_count();
821 void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data)
823 gchar colour[9];
824 GeanyDocument *doc = document_get_current();
825 gint pos;
827 g_return_if_fail(doc != NULL);
829 pos = sci_get_current_position(doc->editor->sci);
830 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
831 tools_color_chooser(colour);
835 void on_toolbutton_compile_clicked(GtkAction *action, gpointer user_data)
837 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
841 void on_find1_activate(GtkMenuItem *menuitem, gpointer user_data)
843 search_show_find_dialog();
847 void on_find_next1_activate(GtkMenuItem *menuitem, gpointer user_data)
849 search_find_again(FALSE);
853 void on_find_previous1_activate(GtkMenuItem *menuitem, gpointer user_data)
855 if (search_data.flags & GEANY_FIND_REGEXP)
856 /* Can't reverse search order for a regex (find next ignores search backwards) */
857 utils_beep();
858 else
859 search_find_again(TRUE);
863 void on_find_nextsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
865 search_find_selection(document_get_current(), FALSE);
869 void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
871 search_find_selection(document_get_current(), TRUE);
875 void on_replace1_activate(GtkMenuItem *menuitem, gpointer user_data)
877 search_show_replace_dialog();
881 void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
883 search_show_find_in_files_dialog(NULL);
887 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
889 if (*text == '+' || *text == '-')
891 *line_no = atoi(text + 1);
892 *offset = (*text == '+') ? 1 : -1;
894 else
896 *line_no = atoi(text) - 1;
897 *offset = 0;
902 void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
904 static gchar value[16] = "";
905 gchar *result;
907 result = dialogs_show_input_goto_line(
908 _("Go to Line"), GTK_WINDOW(main_widgets.window),
909 _("Enter the line you want to go to:"), value);
910 if (result != NULL)
912 GeanyDocument *doc = document_get_current();
913 gint offset;
914 gint line_no;
916 g_return_if_fail(doc != NULL);
918 get_line_and_offset_from_text(result, &line_no, &offset);
919 if (! editor_goto_line(doc->editor, line_no, offset))
920 utils_beep();
921 /* remember value for future calls */
922 g_snprintf(value, sizeof(value), "%s", result);
924 g_free(result);
929 void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
931 GeanyDocument *doc = document_get_current();
932 gint offset;
933 gint line_no;
935 g_return_if_fail(doc != NULL);
937 get_line_and_offset_from_text(text, &line_no, &offset);
938 if (! editor_goto_line(doc->editor, line_no, offset))
939 utils_beep();
940 else
941 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
945 void on_toolbutton_goto_clicked(GtkAction *action, gpointer user_data)
947 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
949 if (entry != NULL)
951 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
953 on_toolbutton_goto_entry_activate(NULL, text, NULL);
955 else
956 on_go_to_line_activate(NULL, NULL);
960 void on_help1_activate(GtkMenuItem *menuitem, gpointer user_data)
962 gchar *uri;
964 uri = utils_get_help_url(NULL);
965 utils_open_browser(uri);
966 g_free(uri);
970 static void on_help_shortcuts1_activate(GtkMenuItem *menuitem, gpointer user_data)
972 keybindings_show_shortcuts();
976 static void on_website1_activate(GtkMenuItem *menuitem, gpointer user_data)
978 utils_open_browser(GEANY_HOMEPAGE);
982 static void on_help_menu_item_donate_activate(GtkMenuItem *item, gpointer user_data)
984 utils_open_browser(GEANY_DONATE);
988 static void on_help_menu_item_wiki_activate(GtkMenuItem *item, gpointer user_data)
990 utils_open_browser(GEANY_WIKI);
994 static void on_help_menu_item_bug_report_activate(GtkMenuItem *item, gpointer user_data)
996 utils_open_browser(GEANY_BUG_REPORT);
1000 static void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data)
1002 GeanyDocument *doc = document_get_current();
1003 gchar *text;
1004 const gchar *cur_tag = NULL;
1005 gint line = -1, pos = 0;
1007 if (doc == NULL || doc->file_type == NULL)
1009 ui_set_statusbar(FALSE,
1010 _("Please set the filetype for the current file before using this function."));
1011 return;
1014 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
1015 * returns the current position, so it should be safe */
1016 line = symbols_get_current_function(doc, &cur_tag);
1017 pos = sci_get_position_from_line(doc->editor->sci, line);
1019 text = templates_get_template_function(doc, cur_tag);
1021 sci_start_undo_action(doc->editor->sci);
1022 sci_insert_text(doc->editor->sci, pos, text);
1023 sci_end_undo_action(doc->editor->sci);
1024 g_free(text);
1028 static void insert_multiline_comment(GeanyDocument *doc, gint pos)
1030 g_return_if_fail(doc != NULL);
1031 g_return_if_fail(pos == -1 || pos >= 0);
1033 if (doc->file_type == NULL)
1035 ui_set_statusbar(FALSE,
1036 _("Please set the filetype for the current file before using this function."));
1037 return;
1040 if (doc->file_type->comment_open || doc->file_type->comment_single)
1042 /* editor_insert_multiline_comment() uses editor_info.click_pos */
1043 if (pos == -1)
1044 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
1045 else
1046 editor_info.click_pos = pos;
1047 editor_insert_multiline_comment(doc->editor);
1049 else
1050 utils_beep();
1054 static void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1056 insert_multiline_comment(document_get_current(), editor_info.click_pos);
1060 static void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1062 insert_multiline_comment(document_get_current(), -1);
1066 static void insert_comment_template(GeanyDocument *doc, gint pos, guint template)
1068 gchar *text;
1070 g_return_if_fail(doc != NULL);
1071 g_return_if_fail(pos == -1 || pos >= 0);
1072 g_return_if_fail(template < GEANY_MAX_TEMPLATES);
1074 if (pos == -1)
1075 pos = sci_get_current_position(doc->editor->sci);
1077 text = templates_get_template_licence(doc, template);
1079 sci_start_undo_action(doc->editor->sci);
1080 sci_insert_text(doc->editor->sci, pos, text);
1081 sci_end_undo_action(doc->editor->sci);
1082 g_free(text);
1086 static void on_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1088 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_GPL);
1092 static void on_menu_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1094 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_GPL);
1098 static void on_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1100 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_BSD);
1104 static void on_menu_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1106 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_BSD);
1110 static void on_comments_changelog_activate(GtkMenuItem *menuitem, gpointer user_data)
1112 GeanyDocument *doc = document_get_current();
1113 gchar *text;
1115 g_return_if_fail(doc != NULL);
1117 text = templates_get_template_changelog(doc);
1118 sci_start_undo_action(doc->editor->sci);
1119 sci_insert_text(doc->editor->sci, 0, text);
1120 /* sets the cursor to the right position to type the changelog text,
1121 * the template has 21 chars + length of name and email */
1122 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1123 sci_end_undo_action(doc->editor->sci);
1125 g_free(text);
1129 static void on_comments_fileheader_activate(GtkMenuItem *menuitem, gpointer user_data)
1131 GeanyDocument *doc = document_get_current();
1132 gchar *text;
1133 const gchar *fname;
1134 GeanyFiletype *ft;
1136 g_return_if_fail(doc != NULL);
1138 ft = doc->file_type;
1139 fname = doc->file_name;
1140 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1142 sci_start_undo_action(doc->editor->sci);
1143 sci_insert_text(doc->editor->sci, 0, text);
1144 sci_goto_pos(doc->editor->sci, 0, FALSE);
1145 sci_end_undo_action(doc->editor->sci);
1146 g_free(text);
1150 static void on_file_properties_activate(GtkMenuItem *menuitem, gpointer user_data)
1152 GeanyDocument *doc = document_get_current();
1153 g_return_if_fail(doc != NULL);
1155 dialogs_show_file_properties(doc);
1159 static void on_menu_fold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1161 GeanyDocument *doc = document_get_current();
1162 g_return_if_fail(doc != NULL);
1164 editor_fold_all(doc->editor);
1168 static void on_menu_unfold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1170 GeanyDocument *doc = document_get_current();
1171 g_return_if_fail(doc != NULL);
1173 editor_unfold_all(doc->editor);
1177 void on_toolbutton_run_clicked(GtkAction *action, gpointer user_data)
1179 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1183 void on_menu_remove_indicators1_activate(GtkMenuItem *menuitem, gpointer user_data)
1185 GeanyDocument *doc = document_get_current();
1186 g_return_if_fail(doc != NULL);
1188 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1192 void on_print1_activate(GtkMenuItem *menuitem, gpointer user_data)
1194 GeanyDocument *doc = document_get_current();
1195 g_return_if_fail(doc != NULL);
1197 printing_print_doc(doc);
1201 void on_menu_select_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1203 GeanyDocument *doc = document_get_current();
1204 g_return_if_fail(doc != NULL);
1206 sci_select_all(doc->editor->sci);
1210 void on_menu_show_sidebar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1212 if (ignore_callback)
1213 return;
1215 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1217 /* show built-in tabs if no tabs visible */
1218 if (ui_prefs.sidebar_visible &&
1219 ! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1220 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1222 interface_prefs.sidebar_openfiles_visible = TRUE;
1223 interface_prefs.sidebar_symbol_visible = TRUE;
1226 /* if window has input focus, set it back to the editor before toggling off */
1227 if (! ui_prefs.sidebar_visible &&
1228 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1230 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1233 ui_sidebar_show_hide();
1237 static void on_menu_write_unicode_bom1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1239 if (! ignore_callback)
1241 GeanyDocument *doc = document_get_current();
1243 g_return_if_fail(doc != NULL);
1244 if (doc->readonly)
1246 utils_beep();
1247 return;
1250 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1252 doc->has_bom = ! doc->has_bom;
1254 ui_update_statusbar(doc, -1);
1259 void on_menu_comment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1261 GeanyDocument *doc = document_get_current();
1262 g_return_if_fail(doc != NULL);
1264 editor_do_comment(doc->editor, -1, FALSE, FALSE, TRUE);
1268 void on_menu_uncomment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1270 GeanyDocument *doc = document_get_current();
1271 g_return_if_fail(doc != NULL);
1273 editor_do_uncomment(doc->editor, -1, FALSE);
1277 void on_menu_toggle_line_commentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1279 GeanyDocument *doc = document_get_current();
1280 g_return_if_fail(doc != NULL);
1282 editor_do_comment_toggle(doc->editor);
1286 void on_menu_increase_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1288 GeanyDocument *doc = document_get_current();
1289 g_return_if_fail(doc != NULL);
1291 editor_indent(doc->editor, TRUE);
1295 void on_menu_decrease_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1297 GeanyDocument *doc = document_get_current();
1298 g_return_if_fail(doc != NULL);
1300 editor_indent(doc->editor, FALSE);
1304 void on_next_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1306 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1307 msgwin_goto_messages_file_line))
1308 ui_set_statusbar(FALSE, _("No more message items."));
1312 void on_previous_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1314 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1315 msgwin_goto_messages_file_line))
1316 ui_set_statusbar(FALSE, _("No more message items."));
1320 void on_project_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
1322 project_new();
1326 void on_project_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
1328 project_open();
1332 void on_project_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
1334 project_close(TRUE);
1338 void on_project_properties1_activate(GtkMenuItem *menuitem, gpointer user_data)
1340 project_properties();
1344 static void on_menu_project1_activate(GtkMenuItem *menuitem, gpointer user_data)
1346 static GtkWidget *item_close = NULL;
1347 static GtkWidget *item_properties = NULL;
1349 if (item_close == NULL)
1351 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1352 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1355 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1356 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1357 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1358 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1362 void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
1364 GeanyDocument *doc = document_get_current();
1365 gchar *sel = NULL;
1366 const gchar *wc;
1368 #ifdef G_OS_WIN32
1369 wc = GEANY_WORDCHARS "./-" "\\";
1370 #else
1371 wc = GEANY_WORDCHARS "./-";
1372 #endif
1374 g_return_if_fail(doc != NULL);
1376 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1377 SETPTR(sel, utils_get_locale_from_utf8(sel));
1379 if (sel != NULL)
1381 gchar *filename = NULL;
1383 if (g_path_is_absolute(sel))
1384 filename = g_strdup(sel);
1385 else
1386 { /* relative filename, add the path of the current file */
1387 gchar *path;
1389 path = utils_get_current_file_dir_utf8();
1390 SETPTR(path, utils_get_locale_from_utf8(path));
1391 if (!path)
1392 path = g_get_current_dir();
1394 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1396 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1397 app->project != NULL && !EMPTY(app->project->base_path))
1399 /* try the project's base path */
1400 SETPTR(path, project_get_base_path());
1401 SETPTR(path, utils_get_locale_from_utf8(path));
1402 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1404 g_free(path);
1405 #ifdef G_OS_UNIX
1406 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1407 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));
1409 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1410 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
1411 #endif
1414 if (g_file_test(filename, G_FILE_TEST_EXISTS))
1415 document_open_file(filename, FALSE, NULL, NULL);
1416 else
1418 SETPTR(sel, utils_get_utf8_from_locale(sel));
1419 ui_set_statusbar(TRUE, _("Could not open file %s (File not found)"), sel);
1422 g_free(filename);
1423 g_free(sel);
1428 void on_remove_markers1_activate(GtkMenuItem *menuitem, gpointer user_data)
1430 GeanyDocument *doc = document_get_current();
1431 g_return_if_fail(doc != NULL);
1433 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1434 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1435 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1439 static void on_load_tags1_activate(GtkMenuItem *menuitem, gpointer user_data)
1441 symbols_show_load_tags_dialog();
1445 void on_context_action1_activate(GtkMenuItem *menuitem, gpointer user_data)
1447 gchar *word, *command;
1448 GError *error = NULL;
1449 GeanyDocument *doc = document_get_current();
1451 g_return_if_fail(doc != NULL);
1453 if (sci_has_selection(doc->editor->sci))
1454 { /* take selected text if there is a selection */
1455 word = sci_get_selection_contents(doc->editor->sci);
1457 else
1459 word = g_strdup(editor_info.current_word);
1462 /* use the filetype specific command if available, fallback to global command otherwise */
1463 if (doc->file_type != NULL &&
1464 !EMPTY(doc->file_type->context_action_cmd))
1466 command = g_strdup(doc->file_type->context_action_cmd);
1468 else
1470 command = g_strdup(tool_prefs.context_action_cmd);
1473 /* substitute the wildcard %s and run the command if it is non empty */
1474 if (G_LIKELY(!EMPTY(command)))
1476 utils_str_replace_all(&command, "%s", word);
1478 if (! g_spawn_command_line_async(command, &error))
1480 ui_set_statusbar(TRUE, "Context action command failed: %s", error->message);
1481 g_error_free(error);
1484 g_free(word);
1485 g_free(command);
1489 void on_menu_toggle_all_additional_widgets1_activate(GtkMenuItem *menuitem, gpointer user_data)
1491 static gint hide_all = -1;
1492 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1493 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1494 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1495 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1497 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1498 if (G_UNLIKELY(hide_all == -1))
1500 if (! gtk_check_menu_item_get_active(msgw) &&
1501 ! interface_prefs.show_notebook_tabs &&
1502 ! gtk_check_menu_item_get_active(toolbari))
1504 hide_all = TRUE;
1506 else
1507 hide_all = FALSE;
1510 hide_all = ! hide_all; /* toggle */
1512 if (hide_all)
1514 if (gtk_check_menu_item_get_active(msgw))
1515 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1517 interface_prefs.show_notebook_tabs = FALSE;
1518 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1520 ui_statusbar_showhide(FALSE);
1522 if (gtk_check_menu_item_get_active(toolbari))
1523 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1525 else
1528 if (! gtk_check_menu_item_get_active(msgw))
1529 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1531 interface_prefs.show_notebook_tabs = TRUE;
1532 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1534 ui_statusbar_showhide(TRUE);
1536 if (! gtk_check_menu_item_get_active(toolbari))
1537 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1542 void on_toolbutton_forward_activate(GtkAction *menuitem, gpointer user_data)
1544 navqueue_go_forward();
1548 void on_toolbutton_back_activate(GtkAction *menuitem, gpointer user_data)
1550 navqueue_go_back();
1554 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1556 if (prefs.auto_focus && ! gtk_widget_has_focus(widget))
1557 gtk_widget_grab_focus(widget);
1559 return FALSE;
1563 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1565 GeanyDocument *doc;
1567 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1568 return;
1570 doc = document_get_current();
1571 g_return_if_fail(doc != NULL);
1573 editor_set_indent(doc->editor, type, doc->editor->indent_width);
1574 ui_update_statusbar(doc, -1);
1578 static void on_tabs1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1580 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1584 static void on_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1586 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
1590 static void on_tabs_and_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1592 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
1596 static void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer user_data)
1598 GeanyDocument *doc;
1600 if (ignore_callback)
1601 return;
1603 doc = document_get_current();
1604 g_return_if_fail(doc != NULL);
1606 editor_strip_trailing_spaces(doc->editor);
1610 static void on_page_setup1_activate(GtkMenuItem *menuitem, gpointer user_data)
1612 printing_page_setup_gtk();
1616 gboolean on_escape_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1618 guint state = keybindings_get_modifiers(event->state);
1620 /* make pressing escape in the sidebar and toolbar focus the editor */
1621 if (event->keyval == GDK_Escape && state == 0)
1623 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1624 return TRUE;
1626 return FALSE;
1630 void on_line_breaking1_activate(GtkMenuItem *menuitem, gpointer user_data)
1632 GeanyDocument *doc;
1634 if (ignore_callback)
1635 return;
1637 doc = document_get_current();
1638 g_return_if_fail(doc != NULL);
1640 doc->editor->line_breaking = !doc->editor->line_breaking;
1644 void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
1646 GeanyDocument *doc = document_get_current();
1648 g_return_if_fail(doc != NULL);
1650 editor_replace_spaces(doc->editor);
1654 static void on_search1_activate(GtkMenuItem *menuitem, gpointer user_data)
1656 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
1657 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
1658 gboolean have_messages;
1660 /* enable commands if the messages window has any items */
1661 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
1662 NULL) > 0;
1664 gtk_widget_set_sensitive(next_message, have_messages);
1665 gtk_widget_set_sensitive(previous_message, have_messages);
1669 /* simple implementation (vs. close all which doesn't close documents if cancelled),
1670 * if user_data is set, it is the GeanyDocument to keep */
1671 void on_close_other_documents1_activate(GtkMenuItem *menuitem, gpointer user_data)
1673 guint i;
1674 GeanyDocument *cur_doc = user_data;
1676 if (cur_doc == NULL)
1677 cur_doc = document_get_current();
1679 for (i = 0; i < documents_array->len; i++)
1681 GeanyDocument *doc = documents[i];
1683 if (doc == cur_doc || ! doc->is_valid)
1684 continue;
1686 if (! document_close(doc))
1687 break;
1692 static void on_menu_reload_configuration1_activate(GtkMenuItem *menuitem, gpointer user_data)
1694 main_reload_configuration();
1698 static void on_debug_messages1_activate(GtkMenuItem *menuitem, gpointer user_data)
1700 log_show_debug_messages_dialog();
1704 void on_send_selection_to_vte1_activate(GtkMenuItem *menuitem, gpointer user_data)
1706 #ifdef HAVE_VTE
1707 if (vte_info.have_vte)
1708 vte_send_selection_to_vte();
1709 #endif
1713 static gboolean on_window_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
1716 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1718 static GtkWidget *menuitem = NULL;
1720 if (menuitem == NULL)
1721 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
1723 ignore_callback = TRUE;
1725 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
1726 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
1728 ignore_callback = FALSE;
1730 return FALSE;
1734 static void show_notebook_page(const gchar *notebook_name, const gchar *page_name)
1736 GtkWidget *widget;
1737 GtkNotebook *notebook;
1739 widget = ui_lookup_widget(ui_widgets.prefs_dialog, page_name);
1740 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, notebook_name));
1742 if (notebook != NULL && widget != NULL)
1743 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
1747 static void on_customize_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
1749 prefs_show_dialog();
1751 /* select the Interface page */
1752 show_notebook_page("notebook2", "notebook6");
1753 /* select the Toolbar subpage */
1754 show_notebook_page("notebook6", "vbox15");
1758 static void on_button_customize_toolbar_clicked(GtkButton *button, gpointer user_data)
1760 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
1764 static void on_cut_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1766 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
1770 static void on_copy_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1772 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
1776 static void on_delete_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1778 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
1782 static void on_duplicate_line_or_selection1_activate(GtkMenuItem *menuitem, gpointer user_data)
1784 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
1788 static void on_select_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1790 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
1794 static void on_select_current_paragraph1_activate(GtkMenuItem *menuitem, gpointer user_data)
1796 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
1800 static void on_insert_alternative_white_space1_activate(GtkMenuItem *menuitem, gpointer user_data)
1802 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
1806 static void on_go_to_next_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1808 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
1812 static void on_go_to_previous_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1814 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
1818 static void on_reflow_lines_block1_activate(GtkMenuItem *menuitem, gpointer user_data)
1820 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
1824 static void on_move_lines_up1_activate(GtkMenuItem *menuitem, gpointer user_data)
1826 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEUP);
1830 static void on_move_lines_down1_activate(GtkMenuItem *menuitem, gpointer user_data)
1832 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEDOWN);
1836 static void on_smart_line_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1838 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
1842 void on_plugin_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
1844 #ifdef HAVE_PLUGINS
1845 plugin_show_configure(NULL);
1846 #endif
1850 static void on_indent_width_activate(GtkMenuItem *menuitem, gpointer user_data)
1852 GeanyDocument *doc;
1853 gchar *label;
1854 gint width;
1856 if (ignore_callback)
1857 return;
1859 label = ui_menu_item_get_text(menuitem);
1860 width = atoi(label);
1861 g_free(label);
1863 doc = document_get_current();
1864 if (doc != NULL && width > 0)
1865 editor_set_indent_width(doc->editor, width);
1869 static void on_reset_indentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1871 guint i;
1873 foreach_document(i)
1874 document_apply_indent_settings(documents[i]);
1876 ui_update_statusbar(NULL, -1);
1877 ui_document_show_hide(NULL);
1881 static void on_mark_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1883 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
1887 static void on_detect_type_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1889 GeanyDocument *doc = document_get_current();
1890 GeanyIndentType type;
1892 if (doc != NULL && document_detect_indent_type(doc, &type))
1894 editor_set_indent_type(doc->editor, type);
1895 ui_document_show_hide(doc);
1900 static void on_detect_width_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1902 GeanyDocument *doc = document_get_current();
1903 gint width;
1905 if (doc != NULL && document_detect_indent_width(doc, &width))
1907 editor_set_indent_width(doc->editor, width);
1908 ui_document_show_hide(doc);
1913 static void builder_connect_func(GtkBuilder *builder, GObject *object,
1914 const gchar *signal_name, const gchar *handler_name, GObject *connect_obj,
1915 GConnectFlags flags, gpointer user_data)
1917 GHashTable *hash = user_data;
1918 GCallback callback;
1920 callback = g_hash_table_lookup(hash, handler_name);
1921 g_return_if_fail(callback);
1923 if (connect_obj == NULL)
1924 g_signal_connect_data(object, signal_name, callback, NULL, NULL, flags);
1925 else
1926 g_signal_connect_object(object, signal_name, callback, connect_obj, flags);
1930 void callbacks_connect(GtkBuilder *builder)
1932 GHashTable *hash;
1934 g_return_if_fail(GTK_IS_BUILDER(builder));
1936 hash = g_hash_table_new(g_str_hash, g_str_equal);
1938 #define ITEM(n) g_hash_table_insert(hash, (gpointer) #n, G_CALLBACK(n));
1939 # include "signallist.i"
1940 #undef ITEM
1942 gtk_builder_connect_signals_full(builder, builder_connect_func, hash);
1943 g_hash_table_destroy(hash);