Merge pull request #1133 from techee/readme_rst
[geany-mirror.git] / src / callbacks.c
blob1177925a0ea134a962bf9410c3c4b34cd8c8158d
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 *cur_doc = document_get_current();
131 guint count = 0;
133 /* iterate over documents in tabs order */
134 for (i = 0; i < max; i++)
136 GeanyDocument *doc = document_get_from_page(i);
138 if (! doc->changed)
139 continue;
141 if (document_save_file(doc, FALSE))
142 count++;
144 if (!count)
145 return;
147 ui_set_statusbar(FALSE, ngettext("%d file saved.", "%d files saved.", count), count);
148 /* saving may have changed window title, sidebar for another doc, so update */
149 document_show_tab(cur_doc);
150 sidebar_update_tag_list(cur_doc, TRUE);
151 ui_set_window_title(cur_doc);
155 void on_close_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
157 document_close_all();
161 void on_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
163 GeanyDocument *doc = document_get_current();
165 if (doc != NULL)
166 document_close(doc);
170 void on_quit1_activate(GtkMenuItem *menuitem, gpointer user_data)
172 main_quit();
176 static void on_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
178 gtk_widget_set_sensitive(ui_widgets.recent_files_menuitem,
179 g_queue_get_length(ui_prefs.recent_queue) > 0);
180 /* hide Page setup when GTK printing is not used */
181 ui_widget_show_hide(ui_widgets.print_page_setup, printing_prefs.use_gtk_printing);
185 /* edit actions, c&p & co, from menu bar and from popup menu */
186 static void on_edit1_activate(GtkMenuItem *menuitem, gpointer user_data)
188 GtkWidget *item;
189 GeanyDocument *doc = document_get_current();
191 ui_update_menu_copy_items(doc);
192 ui_update_insert_include_item(doc, 1);
194 item = ui_lookup_widget(main_widgets.window, "plugin_preferences1");
195 #ifndef HAVE_PLUGINS
196 gtk_widget_hide(item);
197 #else
198 gtk_widget_set_sensitive(item, plugins_have_preferences());
199 #endif
203 void on_undo1_activate(GtkMenuItem *menuitem, gpointer user_data)
205 GeanyDocument *doc = document_get_current();
207 g_return_if_fail(doc != NULL);
209 if (document_can_undo(doc))
211 sci_cancel(doc->editor->sci);
212 document_undo(doc);
217 void on_redo1_activate(GtkMenuItem *menuitem, gpointer user_data)
219 GeanyDocument *doc = document_get_current();
221 g_return_if_fail(doc != NULL);
223 if (document_can_redo(doc))
225 sci_cancel(doc->editor->sci);
226 document_redo(doc);
231 void on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
233 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
235 if (GTK_IS_EDITABLE(focusw))
236 gtk_editable_cut_clipboard(GTK_EDITABLE(focusw));
237 else if (IS_SCINTILLA(focusw))
238 sci_cut(SCINTILLA(focusw));
239 else if (GTK_IS_TEXT_VIEW(focusw))
241 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
242 GTK_TEXT_VIEW(focusw));
243 gtk_text_buffer_cut_clipboard(buffer, gtk_clipboard_get(GDK_NONE), TRUE);
248 void on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
250 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
252 if (GTK_IS_EDITABLE(focusw))
253 gtk_editable_copy_clipboard(GTK_EDITABLE(focusw));
254 else if (IS_SCINTILLA(focusw))
255 sci_copy(SCINTILLA(focusw));
256 else if (GTK_IS_TEXT_VIEW(focusw))
258 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
259 GTK_TEXT_VIEW(focusw));
260 gtk_text_buffer_copy_clipboard(buffer, gtk_clipboard_get(GDK_NONE));
265 void on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
267 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
269 if (GTK_IS_EDITABLE(focusw))
270 gtk_editable_paste_clipboard(GTK_EDITABLE(focusw));
271 else if (IS_SCINTILLA(focusw))
272 sci_paste(SCINTILLA(focusw));
273 else if (GTK_IS_TEXT_VIEW(focusw))
275 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
276 GTK_TEXT_VIEW(focusw));
277 gtk_text_buffer_paste_clipboard(buffer, gtk_clipboard_get(GDK_NONE), NULL,
278 TRUE);
283 void on_delete1_activate(GtkMenuItem *menuitem, gpointer user_data)
285 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
287 if (GTK_IS_EDITABLE(focusw))
288 gtk_editable_delete_selection(GTK_EDITABLE(focusw));
289 else if (IS_SCINTILLA(focusw) && sci_has_selection(SCINTILLA(focusw)))
290 sci_clear(SCINTILLA(focusw));
291 else if (GTK_IS_TEXT_VIEW(focusw))
293 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
294 GTK_TEXT_VIEW(focusw));
295 gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
300 void on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
302 prefs_show_dialog();
306 /* about menu item */
307 static void on_info1_activate(GtkMenuItem *menuitem, gpointer user_data)
309 about_dialog_show();
313 /* open file */
314 void on_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
316 dialogs_show_open_file();
320 /* reload file */
321 void on_toolbutton_reload_clicked(GtkAction *action, gpointer user_data)
323 GeanyDocument *doc = document_get_current();
325 g_return_if_fail(doc != NULL);
327 document_reload_prompt(doc, NULL);
331 static void on_change_font1_activate(GtkMenuItem *menuitem, gpointer user_data)
333 dialogs_show_open_font();
337 /* store text, clear search flags so we can use Search->Find Next/Previous */
338 static void setup_find(const gchar *text, gboolean backwards)
340 SETPTR(search_data.text, g_strdup(text));
341 SETPTR(search_data.original_text, g_strdup(text));
342 search_data.flags = 0;
343 search_data.backwards = backwards;
344 search_data.search_bar = TRUE;
348 static void do_toolbar_search(const gchar *text, gboolean incremental, gboolean backwards)
350 GeanyDocument *doc = document_get_current();
351 gboolean result;
353 setup_find(text, backwards);
354 result = document_search_bar_find(doc, search_data.text, incremental, backwards);
355 if (search_data.search_bar)
356 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
360 /* search text */
361 void on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
363 do_toolbar_search(text, TRUE, FALSE);
367 /* search text */
368 void on_toolbar_search_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
370 do_toolbar_search(text, FALSE, GPOINTER_TO_INT(user_data));
374 /* search text */
375 void on_toolbutton_search_clicked(GtkAction *action, gpointer user_data)
377 GeanyDocument *doc = document_get_current();
378 gboolean result;
379 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
381 if (entry != NULL)
383 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
385 setup_find(text, FALSE);
386 result = document_search_bar_find(doc, search_data.text, FALSE, FALSE);
387 if (search_data.search_bar)
388 ui_set_search_entry_background(entry, result);
390 else
391 on_find1_activate(NULL, NULL);
395 /* hides toolbar from toolbar popup menu */
396 static void on_hide_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
398 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
399 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
403 /* zoom in from menu bar and popup menu */
404 void on_zoom_in1_activate(GtkMenuItem *menuitem, gpointer user_data)
406 GeanyDocument *doc = document_get_current();
408 g_return_if_fail(doc != NULL);
410 sci_zoom_in(doc->editor->sci);
414 /* zoom out from menu bar and popup menu */
415 void on_zoom_out1_activate(GtkMenuItem *menuitem, gpointer user_data)
417 GeanyDocument *doc = document_get_current();
419 g_return_if_fail(doc != NULL);
421 sci_zoom_out(doc->editor->sci);
425 void on_normal_size1_activate(GtkMenuItem *menuitem, gpointer user_data)
427 GeanyDocument *doc = document_get_current();
429 g_return_if_fail(doc != NULL);
431 sci_zoom_off(doc->editor->sci);
435 /* Changes window-title after switching tabs and lots of other things.
436 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
437 static void on_notebook1_switch_page_after(GtkNotebook *notebook, gpointer page,
438 guint page_num, gpointer user_data)
440 GeanyDocument *doc;
442 if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
443 return;
445 doc = document_get_from_notebook_child(page);
447 if (doc != NULL)
449 sidebar_select_openfiles_item(doc);
450 ui_save_buttons_toggle(doc->changed);
451 ui_set_window_title(doc);
452 ui_update_statusbar(doc, -1);
453 ui_update_popup_reundo_items(doc);
454 ui_document_show_hide(doc); /* update the document menu */
455 build_menu_update(doc);
456 sidebar_update_tag_list(doc, FALSE);
457 document_highlight_tags(doc);
459 document_check_disk_status(doc, TRUE);
461 #ifdef HAVE_VTE
462 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
463 #endif
465 g_signal_emit_by_name(geany_object, "document-activate", doc);
470 static void on_tv_notebook_switch_page(GtkNotebook *notebook, gpointer page,
471 guint page_num, gpointer user_data)
473 /* suppress selection changed signal when switching to the open files list */
474 ignore_callback = TRUE;
478 static void on_tv_notebook_switch_page_after(GtkNotebook *notebook, gpointer page,
479 guint page_num, gpointer user_data)
481 ignore_callback = FALSE;
485 static void convert_eol(gint mode)
487 GeanyDocument *doc = document_get_current();
489 g_return_if_fail(doc != NULL);
491 /* sci_convert_eols() adds UNDO_SCINTILLA action in on_editor_notify().
492 * It is added to the undo stack before sci_convert_eols() finishes
493 * so after adding UNDO_EOL, UNDO_EOL will be at the top of the stack
494 * and UNDO_SCINTILLA below it. */
495 sci_convert_eols(doc->editor->sci, mode);
496 document_undo_add(doc, UNDO_EOL, GINT_TO_POINTER(sci_get_eol_mode(doc->editor->sci)));
498 sci_set_eol_mode(doc->editor->sci, mode);
500 ui_update_statusbar(doc, -1);
504 static void on_crlf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
506 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
507 return;
509 convert_eol(SC_EOL_CRLF);
513 static void on_lf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
515 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
516 return;
518 convert_eol(SC_EOL_LF);
522 static void on_cr_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
524 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
525 return;
527 convert_eol(SC_EOL_CR);
531 void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
533 GeanyDocument *doc = document_get_current();
535 g_return_if_fail(doc != NULL);
537 editor_replace_tabs(doc->editor, FALSE);
541 gboolean toolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
543 if (event->button == 3)
545 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
546 return TRUE;
548 return FALSE;
552 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
554 GeanyDocument *doc = document_get_current();
555 ScintillaObject *sci;
556 gboolean keep_sel = TRUE;
558 g_return_if_fail(doc != NULL);
560 sci = doc->editor->sci;
561 if (! sci_has_selection(sci))
563 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
564 keep_sel = FALSE;
567 /* either we already had a selection or we created one for current word */
568 if (sci_has_selection(sci))
570 gchar *result = NULL;
571 gint cmd = SCI_LOWERCASE;
572 gboolean rectsel = (gboolean) scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
573 gchar *text = sci_get_selection_contents(sci);
575 if (utils_str_has_upper(text))
577 if (rectsel)
578 cmd = SCI_LOWERCASE;
579 else
580 result = g_utf8_strdown(text, -1);
582 else
584 if (rectsel)
585 cmd = SCI_UPPERCASE;
586 else
587 result = g_utf8_strup(text, -1);
590 if (result != NULL)
592 sci_replace_sel(sci, result);
593 g_free(result);
594 if (keep_sel)
595 sci_set_selection_start(sci, sci_get_current_position(sci) - strlen(text));
597 else
598 sci_send_command(sci, cmd);
600 g_free(text);
605 static void on_show_toolbar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
607 if (ignore_callback) return;
609 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
610 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
614 static void on_fullscreen1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
616 if (ignore_callback)
617 return;
619 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
620 ui_set_fullscreen();
624 static void on_show_messages_window1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
626 if (ignore_callback)
627 return;
629 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
630 msgwin_show_hide(ui_prefs.msgwindow_visible);
634 static void on_menu_color_schemes_activate(GtkImageMenuItem *imagemenuitem, gpointer user_data)
636 highlighting_show_color_scheme_dialog();
640 static void on_markers_margin1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
642 if (ignore_callback)
643 return;
645 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
646 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
650 static void on_show_line_numbers1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
652 if (ignore_callback)
653 return;
655 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
656 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
660 static void on_menu_show_white_space1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
662 if (ignore_callback)
663 return;
665 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
666 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
670 static void on_menu_show_line_endings1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
672 if (ignore_callback)
673 return;
675 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
676 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
680 static void on_menu_show_indentation_guides1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
682 if (ignore_callback)
683 return;
685 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
686 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
690 void on_line_wrapping1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
692 if (! ignore_callback)
694 GeanyDocument *doc = document_get_current();
695 g_return_if_fail(doc != NULL);
697 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
702 static void on_set_file_readonly1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
704 if (! ignore_callback)
706 GeanyDocument *doc = document_get_current();
707 g_return_if_fail(doc != NULL);
709 doc->readonly = ! doc->readonly;
710 sci_set_readonly(doc->editor->sci, doc->readonly);
711 ui_update_tab_status(doc);
712 ui_update_statusbar(doc, -1);
717 static void on_use_auto_indentation1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
719 if (! ignore_callback)
721 GeanyDocument *doc = document_get_current();
722 g_return_if_fail(doc != NULL);
724 doc->editor->auto_indent = ! doc->editor->auto_indent;
729 static void find_usage(gboolean in_session)
731 GeanyFindFlags flags;
732 gchar *search_text;
733 GeanyDocument *doc = document_get_current();
735 g_return_if_fail(doc != NULL);
737 if (sci_has_selection(doc->editor->sci))
738 { /* take selected text if there is a selection */
739 search_text = sci_get_selection_contents(doc->editor->sci);
740 flags = GEANY_FIND_MATCHCASE;
742 else
744 editor_find_current_word_sciwc(doc->editor, -1,
745 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
746 search_text = g_strdup(editor_info.current_word);
747 flags = GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD;
750 search_find_usage(search_text, search_text, flags, in_session);
751 g_free(search_text);
755 void on_find_document_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
757 find_usage(FALSE);
761 void on_find_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
763 find_usage(TRUE);
767 static void goto_tag(gboolean definition)
769 GeanyDocument *doc = document_get_current();
771 g_return_if_fail(doc != NULL);
773 /* update cursor pos for navigating back afterwards */
774 if (!sci_has_selection(doc->editor->sci))
775 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
777 /* use the keybinding callback as it checks for selections as well as current word */
778 if (definition)
779 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
780 else
781 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
785 static void on_goto_tag_definition1(GtkMenuItem *menuitem, gpointer user_data)
787 goto_tag(TRUE);
791 static void on_goto_tag_declaration1(GtkMenuItem *menuitem, gpointer user_data)
793 goto_tag(FALSE);
797 static void on_count_words1_activate(GtkMenuItem *menuitem, gpointer user_data)
799 tools_word_count();
803 void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data)
805 gchar colour[9];
806 GeanyDocument *doc = document_get_current();
807 gint pos;
809 g_return_if_fail(doc != NULL);
811 pos = sci_get_current_position(doc->editor->sci);
812 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
813 tools_color_chooser(colour);
817 void on_toolbutton_compile_clicked(GtkAction *action, gpointer user_data)
819 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
823 void on_find1_activate(GtkMenuItem *menuitem, gpointer user_data)
825 search_show_find_dialog();
829 void on_find_next1_activate(GtkMenuItem *menuitem, gpointer user_data)
831 search_find_again(FALSE);
835 void on_find_previous1_activate(GtkMenuItem *menuitem, gpointer user_data)
837 if (search_data.flags & GEANY_FIND_REGEXP)
838 /* Can't reverse search order for a regex (find next ignores search backwards) */
839 utils_beep();
840 else
841 search_find_again(TRUE);
845 void on_find_nextsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
847 search_find_selection(document_get_current(), FALSE);
851 void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
853 search_find_selection(document_get_current(), TRUE);
857 void on_replace1_activate(GtkMenuItem *menuitem, gpointer user_data)
859 search_show_replace_dialog();
863 void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
865 search_show_find_in_files_dialog(NULL);
869 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
871 if (*text == '+' || *text == '-')
873 *line_no = atoi(text + 1);
874 *offset = (*text == '+') ? 1 : -1;
876 else
878 *line_no = atoi(text) - 1;
879 *offset = 0;
884 void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
886 static gchar value[16] = "";
887 gchar *result;
889 result = dialogs_show_input_goto_line(
890 _("Go to Line"), GTK_WINDOW(main_widgets.window),
891 _("Enter the line you want to go to:"), value);
892 if (result != NULL)
894 GeanyDocument *doc = document_get_current();
895 gint offset;
896 gint line_no;
898 g_return_if_fail(doc != NULL);
900 get_line_and_offset_from_text(result, &line_no, &offset);
901 if (! editor_goto_line(doc->editor, line_no, offset))
902 utils_beep();
903 /* remember value for future calls */
904 g_snprintf(value, sizeof(value), "%s", result);
906 g_free(result);
911 void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
913 GeanyDocument *doc = document_get_current();
914 gint offset;
915 gint line_no;
917 g_return_if_fail(doc != NULL);
919 get_line_and_offset_from_text(text, &line_no, &offset);
920 if (! editor_goto_line(doc->editor, line_no, offset))
921 utils_beep();
922 else
923 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
927 void on_toolbutton_goto_clicked(GtkAction *action, gpointer user_data)
929 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
931 if (entry != NULL)
933 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
935 on_toolbutton_goto_entry_activate(NULL, text, NULL);
937 else
938 on_go_to_line_activate(NULL, NULL);
942 void on_help1_activate(GtkMenuItem *menuitem, gpointer user_data)
944 gchar *uri;
946 uri = utils_get_help_url(NULL);
947 utils_open_browser(uri);
948 g_free(uri);
952 static void on_help_shortcuts1_activate(GtkMenuItem *menuitem, gpointer user_data)
954 keybindings_show_shortcuts();
958 static void on_website1_activate(GtkMenuItem *menuitem, gpointer user_data)
960 utils_open_browser(GEANY_HOMEPAGE);
964 static void on_help_menu_item_donate_activate(GtkMenuItem *item, gpointer user_data)
966 utils_open_browser(GEANY_DONATE);
970 static void on_help_menu_item_wiki_activate(GtkMenuItem *item, gpointer user_data)
972 utils_open_browser(GEANY_WIKI);
976 static void on_help_menu_item_bug_report_activate(GtkMenuItem *item, gpointer user_data)
978 utils_open_browser(GEANY_BUG_REPORT);
982 static void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data)
984 GeanyDocument *doc = document_get_current();
985 gchar *text;
986 const gchar *cur_tag = NULL;
987 gint line = -1, pos = 0;
989 if (doc == NULL || doc->file_type == NULL)
991 ui_set_statusbar(FALSE,
992 _("Please set the filetype for the current file before using this function."));
993 return;
996 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
997 * returns the current position, so it should be safe */
998 line = symbols_get_current_function(doc, &cur_tag);
999 pos = sci_get_position_from_line(doc->editor->sci, line);
1001 text = templates_get_template_function(doc, cur_tag);
1003 sci_start_undo_action(doc->editor->sci);
1004 sci_insert_text(doc->editor->sci, pos, text);
1005 sci_end_undo_action(doc->editor->sci);
1006 g_free(text);
1010 static void insert_multiline_comment(GeanyDocument *doc, gint pos)
1012 g_return_if_fail(doc != NULL);
1013 g_return_if_fail(pos == -1 || pos >= 0);
1015 if (doc->file_type == NULL)
1017 ui_set_statusbar(FALSE,
1018 _("Please set the filetype for the current file before using this function."));
1019 return;
1022 if (doc->file_type->comment_open || doc->file_type->comment_single)
1024 /* editor_insert_multiline_comment() uses editor_info.click_pos */
1025 if (pos == -1)
1026 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
1027 else
1028 editor_info.click_pos = pos;
1029 editor_insert_multiline_comment(doc->editor);
1031 else
1032 utils_beep();
1036 static void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1038 insert_multiline_comment(document_get_current(), editor_info.click_pos);
1042 static void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1044 insert_multiline_comment(document_get_current(), -1);
1048 static void insert_comment_template(GeanyDocument *doc, gint pos, guint template)
1050 gchar *text;
1052 g_return_if_fail(doc != NULL);
1053 g_return_if_fail(pos == -1 || pos >= 0);
1054 g_return_if_fail(template < GEANY_MAX_TEMPLATES);
1056 if (pos == -1)
1057 pos = sci_get_current_position(doc->editor->sci);
1059 text = templates_get_template_licence(doc, template);
1061 sci_start_undo_action(doc->editor->sci);
1062 sci_insert_text(doc->editor->sci, pos, text);
1063 sci_end_undo_action(doc->editor->sci);
1064 g_free(text);
1068 static void on_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1070 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_GPL);
1074 static void on_menu_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1076 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_GPL);
1080 static void on_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1082 insert_comment_template(document_get_current(), editor_info.click_pos, GEANY_TEMPLATE_BSD);
1086 static void on_menu_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1088 insert_comment_template(document_get_current(), -1, GEANY_TEMPLATE_BSD);
1092 static void on_comments_changelog_activate(GtkMenuItem *menuitem, gpointer user_data)
1094 GeanyDocument *doc = document_get_current();
1095 gchar *text;
1097 g_return_if_fail(doc != NULL);
1099 text = templates_get_template_changelog(doc);
1100 sci_start_undo_action(doc->editor->sci);
1101 sci_insert_text(doc->editor->sci, 0, text);
1102 /* sets the cursor to the right position to type the changelog text,
1103 * the template has 21 chars + length of name and email */
1104 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1105 sci_end_undo_action(doc->editor->sci);
1107 g_free(text);
1111 static void on_comments_fileheader_activate(GtkMenuItem *menuitem, gpointer user_data)
1113 GeanyDocument *doc = document_get_current();
1114 gchar *text;
1115 const gchar *fname;
1116 GeanyFiletype *ft;
1118 g_return_if_fail(doc != NULL);
1120 ft = doc->file_type;
1121 fname = doc->file_name;
1122 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1124 sci_start_undo_action(doc->editor->sci);
1125 sci_insert_text(doc->editor->sci, 0, text);
1126 sci_goto_pos(doc->editor->sci, 0, FALSE);
1127 sci_end_undo_action(doc->editor->sci);
1128 g_free(text);
1132 void on_file_properties_activate(GtkMenuItem *menuitem, gpointer user_data)
1134 GeanyDocument *doc = document_get_current();
1135 g_return_if_fail(doc != NULL);
1137 dialogs_show_file_properties(doc);
1141 static void on_menu_fold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1143 GeanyDocument *doc = document_get_current();
1144 g_return_if_fail(doc != NULL);
1146 editor_fold_all(doc->editor);
1150 static void on_menu_unfold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1152 GeanyDocument *doc = document_get_current();
1153 g_return_if_fail(doc != NULL);
1155 editor_unfold_all(doc->editor);
1159 void on_toolbutton_run_clicked(GtkAction *action, gpointer user_data)
1161 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1165 void on_menu_remove_indicators1_activate(GtkMenuItem *menuitem, gpointer user_data)
1167 GeanyDocument *doc = document_get_current();
1168 g_return_if_fail(doc != NULL);
1170 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1174 void on_print1_activate(GtkMenuItem *menuitem, gpointer user_data)
1176 GeanyDocument *doc = document_get_current();
1177 g_return_if_fail(doc != NULL);
1179 printing_print_doc(doc);
1183 void on_menu_select_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1185 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1187 /* special case for Select All in the scribble widget */
1188 if (GTK_IS_TEXT_VIEW(focusw))
1190 g_signal_emit_by_name(focusw, "select-all", TRUE);
1192 /* special case for Select All in the VTE widget */
1193 #ifdef HAVE_VTE
1194 else if (vte_info.have_vte && focusw == vc->vte)
1196 vte_select_all();
1198 #endif
1199 else if (GTK_IS_EDITABLE(focusw))
1201 gtk_editable_select_region(GTK_EDITABLE(focusw), 0, -1);
1203 else if (IS_SCINTILLA(focusw))
1205 sci_select_all(SCINTILLA(focusw));
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();
1450 const gchar *check_msg;
1452 g_return_if_fail(doc != NULL);
1454 if (sci_has_selection(doc->editor->sci))
1455 { /* take selected text if there is a selection */
1456 word = sci_get_selection_contents(doc->editor->sci);
1458 else
1460 word = g_strdup(editor_info.current_word);
1463 /* use the filetype specific command if available, fallback to global command otherwise */
1464 if (doc->file_type != NULL &&
1465 !EMPTY(doc->file_type->context_action_cmd))
1467 command = g_strdup(doc->file_type->context_action_cmd);
1468 check_msg = _("Check the path setting in Filetype configuration.");
1470 else
1472 command = g_strdup(tool_prefs.context_action_cmd);
1473 check_msg = _("Check the path setting in Preferences.");
1476 /* substitute the wildcard %s and run the command if it is non empty */
1477 if (G_LIKELY(!EMPTY(command)))
1479 gchar *command_line = g_strdup(command);
1481 utils_str_replace_all(&command_line, "%s", word);
1483 if (!spawn_async(NULL, command_line, NULL, NULL, NULL, &error))
1485 /* G_SHELL_ERROR is parsing error, it may be caused by %s word with quotes */
1486 ui_set_statusbar(TRUE, _("Cannot execute context action command \"%s\": %s. %s"),
1487 error->domain == G_SHELL_ERROR ? command_line : command, error->message,
1488 check_msg);
1489 g_error_free(error);
1491 g_free(command_line);
1493 g_free(word);
1494 g_free(command);
1498 void on_menu_toggle_all_additional_widgets1_activate(GtkMenuItem *menuitem, gpointer user_data)
1500 static gint hide_all = -1;
1501 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1502 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1503 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1504 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1506 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1507 if (G_UNLIKELY(hide_all == -1))
1509 if (! gtk_check_menu_item_get_active(msgw) &&
1510 ! interface_prefs.show_notebook_tabs &&
1511 ! gtk_check_menu_item_get_active(toolbari))
1513 hide_all = TRUE;
1515 else
1516 hide_all = FALSE;
1519 hide_all = ! hide_all; /* toggle */
1521 if (hide_all)
1523 if (gtk_check_menu_item_get_active(msgw))
1524 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1526 interface_prefs.show_notebook_tabs = FALSE;
1527 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1529 ui_statusbar_showhide(FALSE);
1531 if (gtk_check_menu_item_get_active(toolbari))
1532 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1534 else
1537 if (! gtk_check_menu_item_get_active(msgw))
1538 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1540 interface_prefs.show_notebook_tabs = TRUE;
1541 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1543 ui_statusbar_showhide(TRUE);
1545 if (! gtk_check_menu_item_get_active(toolbari))
1546 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1551 void on_toolbutton_forward_activate(GtkAction *menuitem, gpointer user_data)
1553 navqueue_go_forward();
1557 void on_toolbutton_back_activate(GtkAction *menuitem, gpointer user_data)
1559 navqueue_go_back();
1563 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1565 if (prefs.auto_focus && ! gtk_widget_has_focus(widget))
1566 gtk_widget_grab_focus(widget);
1568 return FALSE;
1572 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1574 GeanyDocument *doc;
1576 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1577 return;
1579 doc = document_get_current();
1580 g_return_if_fail(doc != NULL);
1582 editor_set_indent(doc->editor, type, doc->editor->indent_width);
1583 ui_update_statusbar(doc, -1);
1587 static void on_tabs1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1589 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1593 static void on_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1595 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
1599 static void on_tabs_and_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1601 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
1605 static void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer user_data)
1607 GeanyDocument *doc;
1609 if (ignore_callback)
1610 return;
1612 doc = document_get_current();
1613 g_return_if_fail(doc != NULL);
1615 editor_strip_trailing_spaces(doc->editor, FALSE);
1619 static void on_page_setup1_activate(GtkMenuItem *menuitem, gpointer user_data)
1621 printing_page_setup_gtk();
1625 gboolean on_escape_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1627 guint state = keybindings_get_modifiers(event->state);
1629 /* make pressing escape in the sidebar and toolbar focus the editor */
1630 if (event->keyval == GDK_Escape && state == 0)
1632 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1633 return TRUE;
1635 return FALSE;
1639 void on_line_breaking1_activate(GtkMenuItem *menuitem, gpointer user_data)
1641 GeanyDocument *doc;
1643 if (ignore_callback)
1644 return;
1646 doc = document_get_current();
1647 g_return_if_fail(doc != NULL);
1649 doc->editor->line_breaking = !doc->editor->line_breaking;
1653 void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
1655 GeanyDocument *doc = document_get_current();
1657 g_return_if_fail(doc != NULL);
1659 editor_replace_spaces(doc->editor, FALSE);
1663 static void on_search1_activate(GtkMenuItem *menuitem, gpointer user_data)
1665 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
1666 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
1667 gboolean have_messages;
1669 /* enable commands if the messages window has any items */
1670 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
1671 NULL) > 0;
1673 gtk_widget_set_sensitive(next_message, have_messages);
1674 gtk_widget_set_sensitive(previous_message, have_messages);
1678 /* simple implementation (vs. close all which doesn't close documents if cancelled),
1679 * if user_data is set, it is the GeanyDocument to keep */
1680 void on_close_other_documents1_activate(GtkMenuItem *menuitem, gpointer user_data)
1682 guint i;
1683 GeanyDocument *cur_doc = user_data;
1685 if (cur_doc == NULL)
1686 cur_doc = document_get_current();
1688 for (i = 0; i < documents_array->len; i++)
1690 GeanyDocument *doc = documents[i];
1692 if (doc == cur_doc || ! doc->is_valid)
1693 continue;
1695 if (! document_close(doc))
1696 break;
1701 static void on_menu_reload_configuration1_activate(GtkMenuItem *menuitem, gpointer user_data)
1703 main_reload_configuration();
1707 static void on_debug_messages1_activate(GtkMenuItem *menuitem, gpointer user_data)
1709 log_show_debug_messages_dialog();
1713 void on_send_selection_to_vte1_activate(GtkMenuItem *menuitem, gpointer user_data)
1715 #ifdef HAVE_VTE
1716 if (vte_info.have_vte)
1717 vte_send_selection_to_vte();
1718 #endif
1722 static gboolean on_window_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
1725 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1727 static GtkWidget *menuitem = NULL;
1729 if (menuitem == NULL)
1730 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
1732 ignore_callback = TRUE;
1734 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
1735 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
1737 ignore_callback = FALSE;
1739 return FALSE;
1743 static void show_notebook_page(const gchar *notebook_name, const gchar *page_name)
1745 GtkWidget *widget;
1746 GtkNotebook *notebook;
1748 widget = ui_lookup_widget(ui_widgets.prefs_dialog, page_name);
1749 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, notebook_name));
1751 if (notebook != NULL && widget != NULL)
1752 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
1756 static void on_customize_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
1758 prefs_show_dialog();
1760 /* select the Interface page */
1761 show_notebook_page("notebook2", "notebook6");
1762 /* select the Toolbar subpage */
1763 show_notebook_page("notebook6", "vbox15");
1767 static void on_button_customize_toolbar_clicked(GtkButton *button, gpointer user_data)
1769 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
1773 static void on_cut_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1775 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
1779 static void on_copy_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1781 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
1785 static void on_delete_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1787 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
1791 static void on_duplicate_line_or_selection1_activate(GtkMenuItem *menuitem, gpointer user_data)
1793 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
1797 static void on_select_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
1799 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
1803 static void on_select_current_paragraph1_activate(GtkMenuItem *menuitem, gpointer user_data)
1805 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
1809 static void on_insert_alternative_white_space1_activate(GtkMenuItem *menuitem, gpointer user_data)
1811 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
1815 static void on_go_to_next_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1817 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
1821 static void on_go_to_previous_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
1823 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
1827 static void on_reflow_lines_block1_activate(GtkMenuItem *menuitem, gpointer user_data)
1829 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
1833 static void on_move_lines_up1_activate(GtkMenuItem *menuitem, gpointer user_data)
1835 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEUP);
1839 static void on_move_lines_down1_activate(GtkMenuItem *menuitem, gpointer user_data)
1841 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_MOVELINEDOWN);
1845 static void on_smart_line_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1847 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
1851 void on_plugin_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
1853 #ifdef HAVE_PLUGINS
1854 plugin_show_configure(NULL);
1855 #endif
1859 static void on_indent_width_activate(GtkMenuItem *menuitem, gpointer user_data)
1861 GeanyDocument *doc;
1862 gchar *label;
1863 gint width;
1865 if (ignore_callback)
1866 return;
1868 label = ui_menu_item_get_text(menuitem);
1869 width = atoi(label);
1870 g_free(label);
1872 doc = document_get_current();
1873 if (doc != NULL && width > 0)
1874 editor_set_indent_width(doc->editor, width);
1878 static void on_reset_indentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1880 guint i;
1882 foreach_document(i)
1883 document_apply_indent_settings(documents[i]);
1885 ui_update_statusbar(NULL, -1);
1886 ui_document_show_hide(NULL);
1890 static void on_mark_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1892 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
1896 static void on_detect_type_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1898 GeanyDocument *doc = document_get_current();
1899 GeanyIndentType type;
1901 if (doc != NULL && document_detect_indent_type(doc, &type))
1903 editor_set_indent_type(doc->editor, type);
1904 ui_document_show_hide(doc);
1909 static void on_show_symbol_list_toggled(GtkToggleButton *button, gpointer user_data)
1911 GtkWidget *widget = ui_lookup_widget(ui_widgets.prefs_dialog, "box_show_symbol_list_children");
1913 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(button));
1917 static void on_detect_width_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
1919 GeanyDocument *doc = document_get_current();
1920 gint width;
1922 if (doc != NULL && document_detect_indent_width(doc, &width))
1924 editor_set_indent_width(doc->editor, width);
1925 ui_document_show_hide(doc);
1930 static void builder_connect_func(GtkBuilder *builder, GObject *object,
1931 const gchar *signal_name, const gchar *handler_name, GObject *connect_obj,
1932 GConnectFlags flags, gpointer user_data)
1934 GHashTable *hash = user_data;
1935 GCallback callback;
1937 callback = g_hash_table_lookup(hash, handler_name);
1938 g_return_if_fail(callback);
1940 if (connect_obj == NULL)
1941 g_signal_connect_data(object, signal_name, callback, NULL, NULL, flags);
1942 else
1943 g_signal_connect_object(object, signal_name, callback, connect_obj, flags);
1947 void callbacks_connect(GtkBuilder *builder)
1949 GHashTable *hash;
1951 g_return_if_fail(GTK_IS_BUILDER(builder));
1953 hash = g_hash_table_new(g_str_hash, g_str_equal);
1955 #define ITEM(n) g_hash_table_insert(hash, (gpointer) #n, G_CALLBACK(n));
1956 # include "signallist.i"
1957 #undef ITEM
1959 gtk_builder_connect_signals_full(builder, builder_connect_func, hash);
1960 g_hash_table_destroy(hash);