Support {ob} and {cb} wildcards for snippets too (fixes #2937008).
[geany-mirror.git] / src / callbacks.c
blob2013945b174fd2e0e7d484ee6478fc67e2619180
1 /*
2 * callbacks.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
25 * Callbacks used by Glade. These are mainly in response to menu item and button events in the
26 * main window. Callbacks not used by Glade should go elsewhere.
29 #include "geany.h"
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gstdio.h>
36 #include <time.h>
38 #include "callbacks.h"
39 #include "support.h"
41 #include "keyfile.h"
42 #include "document.h"
43 #include "documentprivate.h"
44 #include "filetypes.h"
45 #include "sciwrappers.h"
46 #include "editor.h"
47 #include "ui_utils.h"
48 #include "utils.h"
49 #include "dialogs.h"
50 #include "about.h"
51 #include "msgwindow.h"
52 #include "build.h"
53 #include "prefs.h"
54 #include "templates.h"
55 #include "sidebar.h"
56 #include "keybindings.h"
57 #include "encodings.h"
58 #include "search.h"
59 #include "main.h"
60 #include "symbols.h"
61 #include "tools.h"
62 #include "project.h"
63 #include "navqueue.h"
64 #include "printing.h"
65 #include "plugins.h"
66 #include "log.h"
67 #include "toolbar.h"
68 #include "pluginutils.h"
71 #ifdef HAVE_VTE
72 # include "vte.h"
73 #endif
75 #ifdef HAVE_SOCKET
76 # include "socket.h"
77 #endif
81 /* flag to indicate that an insert callback was triggered from the file menu,
82 * so we need to store the current cursor position in editor_info.click_pos. */
83 static gboolean insert_callback_from_menu = FALSE;
85 /* represents the state at switching a notebook page(in the left treeviews widget), to not emit
86 * the selection-changed signal from tv.tree_openfiles */
87 /*static gboolean switch_tv_notebook_page = FALSE; */
90 static gboolean check_no_unsaved(void)
92 guint i;
94 for (i = 0; i < documents_array->len; i++)
96 if (documents[i]->is_valid && documents[i]->changed)
98 return FALSE;
101 return TRUE; /* no unsaved edits */
105 /* set editor_info.click_pos to the current cursor position if insert_callback_from_menu is TRUE
106 * to prevent invalid cursor positions which can cause segfaults */
107 static void verify_click_pos(GeanyDocument *doc)
109 if (insert_callback_from_menu)
111 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
112 insert_callback_from_menu = FALSE;
117 /* should only be called from on_exit_clicked */
118 static void quit_app(void)
120 configuration_save();
122 if (app->project != NULL)
123 project_close(FALSE); /* save project session files */
125 document_close_all();
127 main_status.quitting = TRUE;
129 main_quit();
133 /* wrapper function to abort exit process if cancel button is pressed */
134 gboolean
135 on_exit_clicked (GtkWidget *widget, gpointer gdata)
137 main_status.quitting = TRUE;
139 if (! check_no_unsaved())
141 if (document_account_for_unsaved())
143 quit_app();
144 return FALSE;
147 else
148 if (! prefs.confirm_exit ||
149 dialogs_show_question_full(NULL, GTK_STOCK_QUIT, GTK_STOCK_CANCEL, NULL,
150 _("Do you really want to quit?")))
152 quit_app();
153 return FALSE;
156 main_status.quitting = FALSE;
157 return TRUE;
162 * GUI callbacks
165 void
166 on_new1_activate (GtkMenuItem *menuitem,
167 gpointer user_data)
169 document_new_file(NULL, NULL, NULL);
173 void
174 on_save1_activate (GtkMenuItem *menuitem,
175 gpointer user_data)
177 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
178 GeanyDocument *doc = document_get_current();
180 if (doc != NULL && cur_page >= 0)
182 if (document_need_save_as(doc))
183 dialogs_show_save_as();
184 else
185 document_save_file(doc, FALSE);
190 void
191 on_save_as1_activate (GtkMenuItem *menuitem,
192 gpointer user_data)
194 dialogs_show_save_as();
198 void
199 on_save_all1_activate (GtkMenuItem *menuitem,
200 gpointer user_data)
202 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
203 GeanyDocument *doc, *cur_doc = document_get_current();
204 gint count = 0;
206 for (i = 0; i < max; i++)
208 doc = document_get_from_page(i);
209 if (! doc->changed)
210 continue;
211 if (document_need_save_as(doc))
213 /* display unnamed document */
214 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
215 document_get_notebook_page(doc));
216 if (dialogs_show_save_as())
217 count++;
219 else
221 if (document_save_file(doc, FALSE))
222 count++;
225 if (!count)
226 return;
228 ui_set_statusbar(FALSE, ngettext("%d file saved.", "%d files saved.", count), count);
229 /* saving may have changed window title, sidebar for another doc, so update */
230 sidebar_update_tag_list(cur_doc, TRUE);
231 ui_set_window_title(cur_doc);
235 void
236 on_close_all1_activate (GtkMenuItem *menuitem,
237 gpointer user_data)
239 document_close_all();
243 void
244 on_close1_activate (GtkMenuItem *menuitem,
245 gpointer user_data)
247 GeanyDocument *doc = document_get_current();
249 g_return_if_fail(doc != NULL);
251 document_close(doc);
255 void
256 on_quit1_activate (GtkMenuItem *menuitem,
257 gpointer user_data)
259 on_exit_clicked(NULL, NULL);
263 void
264 on_file1_activate (GtkMenuItem *menuitem,
265 gpointer user_data)
267 gtk_widget_set_sensitive(ui_widgets.recent_files_menuitem,
268 g_queue_get_length(ui_prefs.recent_queue) > 0);
269 #if GTK_CHECK_VERSION(2, 10, 0)
270 /* hide Page setup when GTK printing is not used
271 * (on GTK < 2.10 the menu item is hidden completely) */
272 ui_widget_show_hide(ui_widgets.print_page_setup,
273 printing_prefs.use_gtk_printing || gtk_check_version(2, 10, 0) != NULL);
274 #endif
278 /* edit actions, c&p & co, from menu bar and from popup menu */
279 void
280 on_edit1_activate (GtkMenuItem *menuitem,
281 gpointer user_data)
283 GtkWidget *item;
284 GeanyDocument *doc = document_get_current();
286 ui_update_menu_copy_items(doc);
287 ui_update_insert_include_item(doc, 1);
289 item = ui_lookup_widget(main_widgets.window, "plugin_preferences1");
290 #ifndef HAVE_PLUGINS
291 gtk_widget_hide(item);
292 #else
293 gtk_widget_set_sensitive(item, plugins_have_preferences());
294 #endif
298 void
299 on_undo1_activate (GtkMenuItem *menuitem,
300 gpointer user_data)
302 GeanyDocument *doc = document_get_current();
304 g_return_if_fail(doc != NULL);
306 if (document_can_undo(doc))
308 sci_cancel(doc->editor->sci);
309 document_undo(doc);
314 void
315 on_redo1_activate (GtkMenuItem *menuitem,
316 gpointer user_data)
318 GeanyDocument *doc = document_get_current();
320 g_return_if_fail(doc != NULL);
322 if (document_can_redo(doc))
324 sci_cancel(doc->editor->sci);
325 document_redo(doc);
330 void
331 on_cut1_activate (GtkMenuItem *menuitem,
332 gpointer user_data)
334 GeanyDocument *doc = document_get_current();
335 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
337 if (GTK_IS_EDITABLE(focusw))
338 gtk_editable_cut_clipboard(GTK_EDITABLE(focusw));
339 else
340 if (IS_SCINTILLA(focusw) && doc != NULL)
341 sci_cut(doc->editor->sci);
342 else
343 if (GTK_IS_TEXT_VIEW(focusw))
345 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
346 GTK_TEXT_VIEW(focusw));
347 gtk_text_buffer_cut_clipboard(buffer, gtk_clipboard_get(GDK_NONE), TRUE);
352 void
353 on_copy1_activate (GtkMenuItem *menuitem,
354 gpointer user_data)
356 GeanyDocument *doc = document_get_current();
357 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
359 if (GTK_IS_EDITABLE(focusw))
360 gtk_editable_copy_clipboard(GTK_EDITABLE(focusw));
361 else
362 if (IS_SCINTILLA(focusw) && doc != NULL)
363 sci_copy(doc->editor->sci);
364 else
365 if (GTK_IS_TEXT_VIEW(focusw))
367 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
368 GTK_TEXT_VIEW(focusw));
369 gtk_text_buffer_copy_clipboard(buffer, gtk_clipboard_get(GDK_NONE));
374 void
375 on_paste1_activate (GtkMenuItem *menuitem,
376 gpointer user_data)
378 GeanyDocument *doc = document_get_current();
379 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
381 if (GTK_IS_EDITABLE(focusw))
382 gtk_editable_paste_clipboard(GTK_EDITABLE(focusw));
383 else
384 if (IS_SCINTILLA(focusw) && doc != NULL)
386 sci_paste(doc->editor->sci);
388 else
389 if (GTK_IS_TEXT_VIEW(focusw))
391 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
392 GTK_TEXT_VIEW(focusw));
393 gtk_text_buffer_paste_clipboard(buffer, gtk_clipboard_get(GDK_NONE), NULL,
394 TRUE);
399 void
400 on_delete1_activate (GtkMenuItem *menuitem,
401 gpointer user_data)
403 GeanyDocument *doc = document_get_current();
404 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
406 if (GTK_IS_EDITABLE(focusw))
407 gtk_editable_delete_selection(GTK_EDITABLE(focusw));
408 else
409 if (IS_SCINTILLA(focusw) && doc != NULL && sci_has_selection(doc->editor->sci))
410 sci_clear(doc->editor->sci);
411 else
412 if (GTK_IS_TEXT_VIEW(focusw))
414 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
415 GTK_TEXT_VIEW(focusw));
416 gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
421 void
422 on_preferences1_activate (GtkMenuItem *menuitem,
423 gpointer user_data)
425 prefs_show_dialog();
429 /* about menu item */
430 void
431 on_info1_activate (GtkMenuItem *menuitem,
432 gpointer user_data)
434 about_dialog_show();
438 /* open file */
439 void
440 on_open1_activate (GtkMenuItem *menuitem,
441 gpointer user_data)
443 dialogs_show_open_file();
447 /* quit toolbar button */
448 void
449 on_toolbutton_quit_clicked (GtkAction *action,
450 gpointer user_data)
452 on_exit_clicked(NULL, NULL);
456 /* reload file */
457 void
458 on_toolbutton_reload_clicked (GtkAction *action,
459 gpointer user_data)
461 on_reload_as_activate(NULL, GINT_TO_POINTER(-1));
465 /* also used for reloading when user_data is -1 */
466 void
467 on_reload_as_activate (GtkMenuItem *menuitem,
468 gpointer user_data)
470 GeanyDocument *doc = document_get_current();
471 gchar *base_name;
472 gint i = GPOINTER_TO_INT(user_data);
473 gchar *charset = NULL;
475 g_return_if_fail(doc != NULL);
476 g_return_if_fail(doc->file_name != NULL);
478 if (i >= 0)
480 if (i >= GEANY_ENCODINGS_MAX || encodings[i].charset == NULL)
481 return;
482 charset = encodings[i].charset;
484 else
485 charset = doc->encoding;
487 base_name = g_path_get_basename(doc->file_name);
488 if (dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
489 _("Any unsaved changes will be lost."),
490 _("Are you sure you want to reload '%s'?"), base_name))
492 document_reload_file(doc, charset);
493 if (charset != NULL)
494 ui_update_statusbar(doc, -1);
496 g_free(base_name);
500 void
501 on_change_font1_activate (GtkMenuItem *menuitem,
502 gpointer user_data)
504 dialogs_show_open_font();
508 /* new file */
509 void
510 on_toolbutton_new_clicked (GtkAction *action,
511 gpointer user_data)
513 document_new_file(NULL, NULL, NULL);
517 /* open file */
518 void
519 on_toolbutton_open_clicked (GtkAction *action,
520 gpointer user_data)
522 dialogs_show_open_file();
526 /* save file */
527 void
528 on_toolbutton_save_clicked (GtkAction *action,
529 gpointer user_data)
531 on_save1_activate(NULL, user_data);
535 /* store text, clear search flags so we can use Search->Find Next/Previous */
536 static void setup_find_next(const gchar *text)
538 setptr(search_data.text, g_strdup(text));
539 search_data.flags = 0;
540 search_data.backwards = FALSE;
541 search_data.search_bar = TRUE;
545 /* search text */
546 void
547 on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
549 GeanyDocument *doc = document_get_current();
550 gboolean result;
552 setup_find_next(text);
553 result = document_search_bar_find(doc, search_data.text, 0, GPOINTER_TO_INT(user_data));
554 if (search_data.search_bar)
555 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
559 /* search text */
560 void
561 on_toolbutton_search_clicked (GtkAction *action,
562 gpointer user_data)
564 GeanyDocument *doc = document_get_current();
565 gboolean result;
566 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
568 if (entry != NULL)
570 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
572 setup_find_next(text);
573 result = document_search_bar_find(doc, search_data.text, 0, FALSE);
574 if (search_data.search_bar)
575 ui_set_search_entry_background(entry, result);
577 else
578 on_find1_activate(NULL, NULL);
582 /* hides toolbar from toolbar popup menu */
583 void
584 on_hide_toolbar1_activate (GtkMenuItem *menuitem,
585 gpointer user_data)
587 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
588 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
592 /* zoom in from menu bar and popup menu */
593 void
594 on_zoom_in1_activate (GtkMenuItem *menuitem,
595 gpointer user_data)
597 GeanyDocument *doc = document_get_current();
598 static gint done = 1;
600 g_return_if_fail(doc != NULL);
602 if (done++ % 3 == 0)
603 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin,
604 (sci_get_zoom(doc->editor->sci) / 2));
605 sci_zoom_in(doc->editor->sci);
609 /* zoom out from menu bar and popup menu */
610 void
611 on_zoom_out1_activate (GtkMenuItem *menuitem,
612 gpointer user_data)
614 GeanyDocument *doc = document_get_current();
616 g_return_if_fail(doc != NULL);
618 if (sci_get_zoom(doc->editor->sci) == 0)
619 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
620 sci_zoom_out(doc->editor->sci);
624 void
625 on_normal_size1_activate (GtkMenuItem *menuitem,
626 gpointer user_data)
628 GeanyDocument *doc = document_get_current();
630 g_return_if_fail(doc != NULL);
632 sci_zoom_off(doc->editor->sci);
633 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
637 /* close tab */
638 void
639 on_toolbutton_close_clicked (GtkAction *action,
640 gpointer user_data)
642 on_close1_activate(NULL, NULL);
646 void
647 on_toolbutton_close_all_clicked (GtkAction *action,
648 gpointer user_data)
650 on_close_all1_activate(NULL, NULL);
654 void
655 on_toolbutton_preferences_clicked (GtkAction *action,
656 gpointer user_data)
658 on_preferences1_activate(NULL, NULL);
662 static gboolean delayed_check_disk_status(gpointer data)
664 document_check_disk_status(data, FALSE);
665 return FALSE;
669 /* Changes window-title after switching tabs and lots of other things.
670 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
671 void
672 on_notebook1_switch_page_after (GtkNotebook *notebook,
673 GtkNotebookPage *page,
674 guint page_num,
675 gpointer user_data)
677 GeanyDocument *doc;
679 if (G_UNLIKELY(main_status.opening_session_files) || G_UNLIKELY(main_status.closing_all))
680 return;
682 if (page_num == (guint) -1 && page != NULL)
683 doc = document_find_by_sci(SCINTILLA(page));
684 else
685 doc = document_get_from_page(page_num);
687 if (doc != NULL)
689 sidebar_select_openfiles_item(doc);
690 document_set_text_changed(doc, doc->changed); /* also sets window title and status bar */
691 ui_update_popup_reundo_items(doc);
692 ui_document_show_hide(doc); /* update the document menu */
693 build_menu_update(doc);
694 sidebar_update_tag_list(doc, FALSE);
696 /* We delay the check to avoid weird fast, unintended switching of notebook pages when
697 * the 'file has changed' dialog is shown while the switch event is not yet completely
698 * finished. So, we check after the switch has been performed to be safe. */
699 g_idle_add(delayed_check_disk_status, doc);
701 #ifdef HAVE_VTE
702 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
703 #endif
705 g_signal_emit_by_name(geany_object, "document-activate", doc);
710 void
711 on_tv_notebook_switch_page (GtkNotebook *notebook,
712 GtkNotebookPage *page,
713 guint page_num,
714 gpointer user_data)
716 /* suppress selection changed signal when switching to the open files list */
717 ignore_callback = TRUE;
721 void
722 on_tv_notebook_switch_page_after (GtkNotebook *notebook,
723 GtkNotebookPage *page,
724 guint page_num,
725 gpointer user_data)
727 ignore_callback = FALSE;
731 static void convert_eol(gint mode)
733 GeanyDocument *doc = document_get_current();
735 g_return_if_fail(doc != NULL);
737 sci_convert_eols(doc->editor->sci, mode);
738 sci_set_eol_mode(doc->editor->sci, mode);
739 ui_update_statusbar(doc, -1);
743 void
744 on_crlf_activate (GtkCheckMenuItem *menuitem,
745 gpointer user_data)
747 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
748 return;
750 convert_eol(SC_EOL_CRLF);
754 void
755 on_lf_activate (GtkCheckMenuItem *menuitem,
756 gpointer user_data)
758 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
759 return;
761 convert_eol(SC_EOL_LF);
765 void
766 on_cr_activate (GtkCheckMenuItem *menuitem,
767 gpointer user_data)
769 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
770 return;
772 convert_eol(SC_EOL_CR);
776 void
777 on_replace_tabs_activate (GtkMenuItem *menuitem,
778 gpointer user_data)
780 GeanyDocument *doc = document_get_current();
782 g_return_if_fail(doc != NULL);
784 editor_replace_tabs(doc->editor);
788 gboolean
789 toolbar_popup_menu (GtkWidget *widget,
790 GdkEventButton *event,
791 gpointer user_data)
793 if (event->button == 3)
795 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
796 return TRUE;
798 return FALSE;
802 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
804 GeanyDocument *doc = document_get_current();
805 ScintillaObject *sci;
806 gchar *text;
807 gboolean keep_sel = TRUE;
809 g_return_if_fail(doc != NULL);
811 sci = doc->editor->sci;
812 if (! sci_has_selection(sci))
814 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
815 keep_sel = FALSE;
817 else
819 gchar *result = NULL;
820 gint cmd = SCI_LOWERCASE;
821 gint text_len = sci_get_selected_text_length(sci);
822 gboolean rectsel = scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
824 text = g_malloc(text_len + 1);
825 sci_get_selected_text(sci, text);
827 if (utils_str_has_upper(text))
829 if (rectsel)
830 cmd = SCI_LOWERCASE;
831 else
832 result = g_utf8_strdown(text, -1);
835 else
837 if (rectsel)
838 cmd = SCI_UPPERCASE;
839 else
840 result = g_utf8_strup(text, -1);
844 if (result != NULL)
846 sci_replace_sel(sci, result);
847 g_free(result);
848 if (keep_sel)
849 sci_set_selection_start(sci, sci_get_current_position(sci) - text_len + 1);
851 else
852 sci_send_command(sci, cmd);
854 g_free(text);
860 void
861 on_show_toolbar1_toggled (GtkCheckMenuItem *checkmenuitem,
862 gpointer user_data)
864 if (ignore_callback) return;
866 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
867 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
871 void
872 on_fullscreen1_toggled (GtkCheckMenuItem *checkmenuitem,
873 gpointer user_data)
875 if (ignore_callback)
876 return;
878 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
879 ui_set_fullscreen();
883 void
884 on_show_messages_window1_toggled (GtkCheckMenuItem *checkmenuitem,
885 gpointer user_data)
887 if (ignore_callback)
888 return;
890 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
891 msgwin_show_hide(ui_prefs.msgwindow_visible);
895 void
896 on_markers_margin1_toggled (GtkCheckMenuItem *checkmenuitem,
897 gpointer user_data)
899 if (ignore_callback)
900 return;
902 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
903 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
907 void
908 on_show_line_numbers1_toggled (GtkCheckMenuItem *checkmenuitem,
909 gpointer user_data)
911 if (ignore_callback)
912 return;
914 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
915 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
919 void
920 on_menu_show_white_space1_toggled (GtkCheckMenuItem *checkmenuitem,
921 gpointer user_data)
923 if (ignore_callback)
924 return;
926 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
927 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
931 void
932 on_menu_show_line_endings1_toggled (GtkCheckMenuItem *checkmenuitem,
933 gpointer user_data)
935 if (ignore_callback)
936 return;
938 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
939 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
943 void
944 on_menu_show_indentation_guides1_toggled (GtkCheckMenuItem *checkmenuitem,
945 gpointer user_data)
947 if (ignore_callback)
948 return;
950 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
951 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
955 void
956 on_line_wrapping1_toggled (GtkCheckMenuItem *checkmenuitem,
957 gpointer user_data)
959 if (! ignore_callback)
961 GeanyDocument *doc = document_get_current();
962 g_return_if_fail(doc != NULL);
964 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
969 void
970 on_set_file_readonly1_toggled (GtkCheckMenuItem *checkmenuitem,
971 gpointer user_data)
973 if (! ignore_callback)
975 GeanyDocument *doc = document_get_current();
976 g_return_if_fail(doc != NULL);
978 doc->readonly = ! doc->readonly;
979 sci_set_readonly(doc->editor->sci, doc->readonly);
980 ui_update_tab_status(doc);
981 ui_update_statusbar(doc, -1);
986 void
987 on_use_auto_indentation1_toggled (GtkCheckMenuItem *checkmenuitem,
988 gpointer user_data)
990 if (! ignore_callback)
992 GeanyDocument *doc = document_get_current();
993 g_return_if_fail(doc != NULL);
995 doc->editor->auto_indent = ! doc->editor->auto_indent;
1000 static void find_usage(gboolean in_session)
1002 gint flags;
1003 gchar *search_text;
1004 GeanyDocument *doc = document_get_current();
1006 g_return_if_fail(doc != NULL);
1008 if (sci_has_selection(doc->editor->sci))
1009 { /* take selected text if there is a selection */
1010 search_text = g_malloc(sci_get_selected_text_length(doc->editor->sci) + 1);
1011 sci_get_selected_text(doc->editor->sci, search_text);
1012 flags = SCFIND_MATCHCASE;
1014 else
1016 search_text = g_strdup(editor_info.current_word);
1017 flags = SCFIND_MATCHCASE | SCFIND_WHOLEWORD;
1020 search_find_usage(search_text, flags, in_session);
1021 g_free(search_text);
1025 void
1026 on_find_document_usage1_activate (GtkMenuItem *menuitem,
1027 gpointer user_data)
1029 find_usage(FALSE);
1033 void
1034 on_find_usage1_activate (GtkMenuItem *menuitem,
1035 gpointer user_data)
1037 find_usage(TRUE);
1041 void
1042 on_goto_tag_activate (GtkMenuItem *menuitem,
1043 gpointer user_data)
1045 gboolean definition = (menuitem ==
1046 GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu, "goto_tag_definition1")));
1047 GeanyDocument *doc = document_get_current();
1049 g_return_if_fail(doc != NULL);
1051 /* update cursor pos for navigating back afterwards */
1052 if (!sci_has_selection(doc->editor->sci))
1053 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
1055 /* use the keybinding callback as it checks for selections as well as current word */
1056 if (definition)
1057 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
1058 else
1059 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
1063 void
1064 on_count_words1_activate (GtkMenuItem *menuitem,
1065 gpointer user_data)
1067 tools_word_count();
1071 void
1072 on_show_color_chooser1_activate (GtkMenuItem *menuitem,
1073 gpointer user_data)
1075 gchar colour[9];
1076 GeanyDocument *doc = document_get_current();
1077 gint pos;
1079 g_return_if_fail(doc != NULL);
1081 pos = sci_get_current_position(doc->editor->sci);
1082 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
1083 tools_color_chooser(colour);
1087 void
1088 on_toolbutton_compile_clicked (GtkAction *action,
1089 gpointer user_data)
1091 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
1095 void
1096 on_find1_activate (GtkMenuItem *menuitem,
1097 gpointer user_data)
1099 search_show_find_dialog();
1103 static void find_again(gboolean change_direction)
1105 GeanyDocument *doc = document_get_current();
1107 g_return_if_fail(doc != NULL);
1109 if (search_data.text)
1111 gboolean forward = ! search_data.backwards;
1112 gint result = document_find_text(doc, search_data.text, search_data.flags,
1113 change_direction ? forward : !forward, FALSE, NULL);
1115 if (result > -1)
1116 editor_display_current_line(doc->editor, 0.3F);
1118 if (search_data.search_bar)
1119 ui_set_search_entry_background(
1120 toolbar_get_widget_child_by_name("SearchEntry"), (result > -1));
1125 void
1126 on_find_next1_activate (GtkMenuItem *menuitem,
1127 gpointer user_data)
1129 find_again(FALSE);
1133 void
1134 on_find_previous1_activate (GtkMenuItem *menuitem,
1135 gpointer user_data)
1137 if (search_data.flags & SCFIND_REGEXP)
1138 /* Can't reverse search order for a regex (find next ignores search backwards) */
1139 utils_beep();
1140 else
1141 find_again(TRUE);
1145 void
1146 on_find_nextsel1_activate (GtkMenuItem *menuitem,
1147 gpointer user_data)
1149 search_find_selection(document_get_current(), FALSE);
1153 void
1154 on_find_prevsel1_activate (GtkMenuItem *menuitem,
1155 gpointer user_data)
1157 search_find_selection(document_get_current(), TRUE);
1161 void
1162 on_replace1_activate (GtkMenuItem *menuitem,
1163 gpointer user_data)
1165 search_show_replace_dialog();
1169 void
1170 on_find_in_files1_activate (GtkMenuItem *menuitem,
1171 gpointer user_data)
1173 search_show_find_in_files_dialog(NULL);
1177 void
1178 on_go_to_line_activate (GtkMenuItem *menuitem,
1179 gpointer user_data)
1181 static gdouble val = 1;
1183 if (dialogs_show_input_numeric(_("Go to Line"), _("Enter the line you want to go to:"),
1184 &val, 1, 100000000, 1))
1186 GeanyDocument *doc = document_get_current();
1188 g_return_if_fail(doc != NULL);
1190 if (! editor_goto_line(doc->editor, (gint) val - 1))
1191 utils_beep();
1196 void
1197 on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
1199 GeanyDocument *doc = document_get_current();
1201 g_return_if_fail(doc != NULL);
1203 if (! editor_goto_line(doc->editor, atoi(text) - 1))
1204 utils_beep();
1205 else
1206 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1210 void
1211 on_toolbutton_goto_clicked (GtkAction *action,
1212 gpointer user_data)
1214 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
1216 if (entry != NULL)
1218 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
1220 on_toolbutton_goto_entry_activate(NULL, text, NULL);
1222 else
1223 on_go_to_line_activate(NULL, NULL);
1227 void
1228 on_help1_activate (GtkMenuItem *menuitem,
1229 gpointer user_data)
1231 gchar *uri;
1233 uri = utils_get_help_url(NULL);
1234 utils_open_browser(uri);
1235 g_free(uri);
1239 void
1240 on_help_shortcuts1_activate (GtkMenuItem *menuitem,
1241 gpointer user_data)
1243 keybindings_show_shortcuts();
1247 void
1248 on_website1_activate (GtkMenuItem *menuitem,
1249 gpointer user_data)
1251 utils_open_browser(GEANY_HOMEPAGE);
1255 void
1256 on_comments_function_activate (GtkMenuItem *menuitem,
1257 gpointer user_data)
1259 GeanyDocument *doc = document_get_current();
1260 gchar *text;
1261 const gchar *cur_tag = NULL;
1262 gint line = -1, pos = 0;
1264 if (doc == NULL || doc->file_type == NULL)
1266 ui_set_statusbar(FALSE,
1267 _("Please set the filetype for the current file before using this function."));
1268 return;
1271 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
1272 * returns the current position, so it should be safe */
1273 line = symbols_get_current_function(doc, &cur_tag);
1274 pos = sci_get_position_from_line(doc->editor->sci, line - 1);
1276 text = templates_get_template_function(doc, cur_tag);
1278 sci_insert_text(doc->editor->sci, pos, text);
1279 g_free(text);
1283 void
1284 on_comments_multiline_activate (GtkMenuItem *menuitem,
1285 gpointer user_data)
1287 GeanyDocument *doc = document_get_current();
1289 if (doc == NULL || doc->file_type == NULL)
1291 ui_set_statusbar(FALSE,
1292 _("Please set the filetype for the current file before using this function."));
1293 return;
1296 verify_click_pos(doc); /* make sure that the click_pos is valid */
1298 if (doc->file_type->comment_open)
1299 editor_insert_multiline_comment(doc->editor);
1300 else
1301 utils_beep();
1305 void
1306 on_comments_gpl_activate (GtkMenuItem *menuitem,
1307 gpointer user_data)
1309 GeanyDocument *doc = document_get_current();
1310 gchar *text;
1312 g_return_if_fail(doc != NULL);
1314 text = templates_get_template_licence(doc, GEANY_TEMPLATE_GPL);
1316 verify_click_pos(doc); /* make sure that the click_pos is valid */
1318 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1319 g_free(text);
1323 void
1324 on_comments_bsd_activate (GtkMenuItem *menuitem,
1325 gpointer user_data)
1328 GeanyDocument *doc = document_get_current();
1329 gchar *text;
1331 g_return_if_fail(doc != NULL);
1333 text = templates_get_template_licence(doc, GEANY_TEMPLATE_BSD);
1335 verify_click_pos(doc); /* make sure that the click_pos is valid */
1337 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1338 g_free(text);
1343 void
1344 on_comments_changelog_activate (GtkMenuItem *menuitem,
1345 gpointer user_data)
1347 GeanyDocument *doc = document_get_current();
1348 gchar *text;
1350 g_return_if_fail(doc != NULL);
1352 text = templates_get_template_changelog(doc);
1353 sci_insert_text(doc->editor->sci, 0, text);
1354 /* sets the cursor to the right position to type the changelog text,
1355 * the template has 21 chars + length of name and email */
1356 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1358 g_free(text);
1362 void
1363 on_comments_fileheader_activate (GtkMenuItem *menuitem,
1364 gpointer user_data)
1366 GeanyDocument *doc = document_get_current();
1367 gchar *text;
1368 gchar *fname;
1369 GeanyFiletype *ft;
1371 g_return_if_fail(doc != NULL);
1373 ft = doc->file_type;
1374 fname = doc->file_name;
1375 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1377 sci_insert_text(doc->editor->sci, 0, text);
1378 sci_goto_pos(doc->editor->sci, 0, FALSE);
1379 g_free(text);
1383 void
1384 on_insert_date_activate (GtkMenuItem *menuitem,
1385 gpointer user_data)
1387 GeanyDocument *doc = document_get_current();
1388 gchar *format = NULL;
1389 gchar *time_str;
1391 g_return_if_fail(doc != NULL);
1393 /* set default value */
1394 if (utils_str_equal("", ui_prefs.custom_date_format))
1396 g_free(ui_prefs.custom_date_format);
1397 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
1400 if (utils_str_equal(_("dd.mm.yyyy"), (gchar*) user_data))
1401 format = "%d.%m.%Y";
1402 else if (utils_str_equal(_("mm.dd.yyyy"), (gchar*) user_data))
1403 format = "%m.%d.%Y";
1404 else if (utils_str_equal(_("yyyy/mm/dd"), (gchar*) user_data))
1405 format = "%Y/%m/%d";
1406 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), (gchar*) user_data))
1407 format = "%d.%m.%Y %H:%M:%S";
1408 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), (gchar*) user_data))
1409 format = "%m.%d.%Y %H:%M:%S";
1410 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), (gchar*) user_data))
1411 format = "%Y/%m/%d %H:%M:%S";
1412 else if (utils_str_equal(_("_Use Custom Date Format"), (gchar*) user_data))
1413 format = ui_prefs.custom_date_format;
1414 else
1416 setptr(ui_prefs.custom_date_format,
1417 dialogs_show_input(_("Custom Date Format"),
1418 _("Enter here a custom date and time format. "
1419 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
1420 ui_prefs.custom_date_format));
1421 return;
1424 time_str = utils_get_date_time(format, NULL);
1425 if (time_str != NULL)
1427 verify_click_pos(doc); /* make sure that the click_pos is valid */
1429 sci_insert_text(doc->editor->sci, editor_info.click_pos, time_str);
1430 sci_goto_pos(doc->editor->sci, editor_info.click_pos + strlen(time_str), FALSE);
1431 g_free(time_str);
1433 else
1435 utils_beep();
1436 ui_set_statusbar(TRUE,
1437 _("Date format string could not be converted (possibly too long)."));
1442 void
1443 on_insert_include_activate (GtkMenuItem *menuitem,
1444 gpointer user_data)
1446 GeanyDocument *doc = document_get_current();
1447 gint pos = -1;
1448 gchar *text;
1450 g_return_if_fail(doc != NULL);
1451 g_return_if_fail(user_data != NULL);
1453 verify_click_pos(doc); /* make sure that the click_pos is valid */
1455 if (utils_str_equal(user_data, "blank"))
1457 text = g_strdup("#include \"\"\n");
1458 pos = editor_info.click_pos + 10;
1460 else
1462 text = g_strconcat("#include <", user_data, ">\n", NULL);
1465 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1466 g_free(text);
1467 if (pos >= 0)
1468 sci_goto_pos(doc->editor->sci, pos, FALSE);
1472 void
1473 on_file_properties_activate (GtkMenuItem *menuitem,
1474 gpointer user_data)
1476 GeanyDocument *doc = document_get_current();
1477 g_return_if_fail(doc != NULL);
1479 dialogs_show_file_properties(doc);
1483 void
1484 on_menu_fold_all1_activate (GtkMenuItem *menuitem,
1485 gpointer user_data)
1487 GeanyDocument *doc = document_get_current();
1488 g_return_if_fail(doc != NULL);
1490 editor_fold_all(doc->editor);
1494 void
1495 on_menu_unfold_all1_activate (GtkMenuItem *menuitem,
1496 gpointer user_data)
1498 GeanyDocument *doc = document_get_current();
1499 g_return_if_fail(doc != NULL);
1501 editor_unfold_all(doc->editor);
1505 void
1506 on_toolbutton_run_clicked (GtkAction *action,
1507 gpointer user_data)
1509 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1513 void
1514 on_menu_remove_indicators1_activate (GtkMenuItem *menuitem,
1515 gpointer user_data)
1517 GeanyDocument *doc = document_get_current();
1518 g_return_if_fail(doc != NULL);
1520 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1524 void
1525 on_print1_activate (GtkMenuItem *menuitem,
1526 gpointer user_data)
1528 GeanyDocument *doc = document_get_current();
1529 g_return_if_fail(doc != NULL);
1531 printing_print_doc(doc);
1535 void
1536 on_menu_select_all1_activate (GtkMenuItem *menuitem,
1537 gpointer user_data)
1539 GeanyDocument *doc = document_get_current();
1540 g_return_if_fail(doc != NULL);
1542 sci_select_all(doc->editor->sci);
1546 void
1547 on_menu_show_sidebar1_toggled (GtkCheckMenuItem *checkmenuitem,
1548 gpointer user_data)
1550 if (ignore_callback)
1551 return;
1553 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1555 if (! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible)
1557 interface_prefs.sidebar_openfiles_visible = TRUE;
1558 interface_prefs.sidebar_symbol_visible = TRUE;
1561 #if GTK_CHECK_VERSION(2, 14, 0)
1562 /* if window has input focus, set it back to the editor before toggling off */
1563 if (! ui_prefs.sidebar_visible &&
1564 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1566 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1568 #endif
1570 ui_sidebar_show_hide();
1574 void
1575 on_menu_write_unicode_bom1_toggled (GtkCheckMenuItem *checkmenuitem,
1576 gpointer user_data)
1578 if (! ignore_callback)
1580 GeanyDocument *doc = document_get_current();
1582 g_return_if_fail(doc != NULL);
1583 if (doc->readonly)
1585 utils_beep();
1586 return;
1589 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1591 doc->has_bom = ! doc->has_bom;
1593 ui_update_statusbar(doc, -1);
1598 void
1599 on_menu_comment_line1_activate (GtkMenuItem *menuitem,
1600 gpointer user_data)
1602 GeanyDocument *doc = document_get_current();
1603 g_return_if_fail(doc != NULL);
1605 editor_do_comment(doc->editor, -1, FALSE, FALSE);
1609 void
1610 on_menu_uncomment_line1_activate (GtkMenuItem *menuitem,
1611 gpointer user_data)
1613 GeanyDocument *doc = document_get_current();
1614 g_return_if_fail(doc != NULL);
1616 editor_do_uncomment(doc->editor, -1, FALSE);
1620 void
1621 on_menu_toggle_line_commentation1_activate
1622 (GtkMenuItem *menuitem,
1623 gpointer user_data)
1625 GeanyDocument *doc = document_get_current();
1626 g_return_if_fail(doc != NULL);
1628 editor_do_comment_toggle(doc->editor);
1632 void
1633 on_menu_increase_indent1_activate (GtkMenuItem *menuitem,
1634 gpointer user_data)
1636 GeanyDocument *doc = document_get_current();
1637 g_return_if_fail(doc != NULL);
1639 editor_indent(doc->editor, TRUE);
1643 void
1644 on_menu_decrease_indent1_activate (GtkMenuItem *menuitem,
1645 gpointer user_data)
1647 GeanyDocument *doc = document_get_current();
1648 g_return_if_fail(doc != NULL);
1650 editor_indent(doc->editor, FALSE);
1654 void
1655 on_next_message1_activate (GtkMenuItem *menuitem,
1656 gpointer user_data)
1658 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1659 msgwin_goto_messages_file_line))
1660 ui_set_statusbar(FALSE, _("No more message items."));
1664 void
1665 on_previous_message1_activate (GtkMenuItem *menuitem,
1666 gpointer user_data)
1668 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1669 msgwin_goto_messages_file_line))
1670 ui_set_statusbar(FALSE, _("No more message items."));
1674 void
1675 on_menu_comments_multiline_activate (GtkMenuItem *menuitem,
1676 gpointer user_data)
1678 insert_callback_from_menu = TRUE;
1679 on_comments_multiline_activate(menuitem, user_data);
1683 void
1684 on_menu_comments_gpl_activate (GtkMenuItem *menuitem,
1685 gpointer user_data)
1687 insert_callback_from_menu = TRUE;
1688 on_comments_gpl_activate(menuitem, user_data);
1692 void
1693 on_menu_comments_bsd_activate (GtkMenuItem *menuitem,
1694 gpointer user_data)
1696 insert_callback_from_menu = TRUE;
1697 on_comments_bsd_activate(menuitem, user_data);
1701 void
1702 on_menu_insert_include_activate (GtkMenuItem *menuitem,
1703 gpointer user_data)
1705 insert_callback_from_menu = TRUE;
1706 on_insert_include_activate(menuitem, user_data);
1710 void
1711 on_menu_insert_date_activate (GtkMenuItem *menuitem,
1712 gpointer user_data)
1714 insert_callback_from_menu = TRUE;
1715 on_insert_date_activate(menuitem, user_data);
1719 void
1720 on_project_new1_activate (GtkMenuItem *menuitem,
1721 gpointer user_data)
1723 project_new();
1727 void
1728 on_project_open1_activate (GtkMenuItem *menuitem,
1729 gpointer user_data)
1731 project_open();
1735 void
1736 on_project_close1_activate (GtkMenuItem *menuitem,
1737 gpointer user_data)
1739 project_close(TRUE);
1743 void
1744 on_project_properties1_activate (GtkMenuItem *menuitem,
1745 gpointer user_data)
1747 project_properties();
1751 void
1752 on_menu_project1_activate (GtkMenuItem *menuitem,
1753 gpointer user_data)
1755 static GtkWidget *item_close = NULL;
1756 static GtkWidget *item_properties = NULL;
1758 if (item_close == NULL)
1760 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1761 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1764 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1765 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1766 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1767 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1771 void
1772 on_menu_open_selected_file1_activate (GtkMenuItem *menuitem,
1773 gpointer user_data)
1775 GeanyDocument *doc = document_get_current();
1776 gchar *sel = NULL;
1777 const gchar *wc;
1779 #ifdef G_OS_WIN32
1780 wc = GEANY_WORDCHARS "./-" "\\";
1781 #else
1782 wc = GEANY_WORDCHARS "./-";
1783 #endif
1785 g_return_if_fail(doc != NULL);
1787 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1789 if (sel != NULL)
1791 gchar *locale_filename, *filename = NULL;
1793 if (g_path_is_absolute(sel))
1794 filename = g_strdup(sel);
1795 else
1796 { /* relative filename, add the path of the current file */
1797 gchar *path;
1799 path = utils_get_current_file_dir_utf8();
1800 if (!path)
1801 path = g_get_current_dir();
1803 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1805 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1806 app->project != NULL && NZV(app->project->base_path))
1808 /* try the project's base path */
1809 setptr(path, project_get_base_path());
1810 setptr(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1812 g_free(path);
1815 locale_filename = utils_get_locale_from_utf8(filename);
1816 document_open_file(locale_filename, FALSE, NULL, NULL);
1818 g_free(filename);
1819 g_free(locale_filename);
1820 g_free(sel);
1825 void
1826 on_remove_markers1_activate (GtkMenuItem *menuitem,
1827 gpointer user_data)
1829 GeanyDocument *doc = document_get_current();
1830 g_return_if_fail(doc != NULL);
1832 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1833 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1834 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1838 void
1839 on_load_tags1_activate (GtkMenuItem *menuitem,
1840 gpointer user_data)
1842 symbols_show_load_tags_dialog();
1846 void
1847 on_context_action1_activate (GtkMenuItem *menuitem,
1848 gpointer user_data)
1850 gchar *word, *command;
1851 GError *error = NULL;
1852 GeanyDocument *doc = document_get_current();
1854 g_return_if_fail(doc != NULL);
1856 if (sci_has_selection(doc->editor->sci))
1857 { /* take selected text if there is a selection */
1858 word = g_malloc(sci_get_selected_text_length(doc->editor->sci) + 1);
1859 sci_get_selected_text(doc->editor->sci, word);
1861 else
1863 word = g_strdup(editor_info.current_word);
1866 /* use the filetype specific command if available, fallback to global command otherwise */
1867 if (doc->file_type != NULL &&
1868 NZV(doc->file_type->context_action_cmd))
1870 command = g_strdup(doc->file_type->context_action_cmd);
1872 else
1874 command = g_strdup(tool_prefs.context_action_cmd);
1877 /* substitute the wildcard %s and run the command if it is non empty */
1878 if (NZV(command))
1880 utils_str_replace_all(&command, "%s", word);
1882 if (! g_spawn_command_line_async(command, &error))
1884 ui_set_statusbar(TRUE, "Context action command failed: %s", error->message);
1885 g_error_free(error);
1888 g_free(word);
1889 g_free(command);
1893 void
1894 on_menu_toggle_all_additional_widgets1_activate
1895 (GtkMenuItem *menuitem,
1896 gpointer user_data)
1898 static gint hide_all = -1;
1899 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1900 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1901 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1902 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1904 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1905 if (G_UNLIKELY(hide_all == -1))
1907 if (! gtk_check_menu_item_get_active(msgw) &&
1908 ! interface_prefs.show_notebook_tabs &&
1909 ! gtk_check_menu_item_get_active(toolbari))
1911 hide_all = TRUE;
1913 else
1914 hide_all = FALSE;
1917 hide_all = ! hide_all; /* toggle */
1919 if (hide_all)
1921 if (gtk_check_menu_item_get_active(msgw))
1922 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1924 interface_prefs.show_notebook_tabs = FALSE;
1925 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1927 ui_statusbar_showhide(FALSE);
1929 if (gtk_check_menu_item_get_active(toolbari))
1930 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1932 else
1935 if (! gtk_check_menu_item_get_active(msgw))
1936 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1938 interface_prefs.show_notebook_tabs = TRUE;
1939 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1941 ui_statusbar_showhide(TRUE);
1943 if (! gtk_check_menu_item_get_active(toolbari))
1944 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1949 void
1950 on_forward_activate (GtkMenuItem *menuitem,
1951 gpointer user_data)
1953 navqueue_go_forward();
1957 void
1958 on_back_activate (GtkMenuItem *menuitem,
1959 gpointer user_data)
1961 navqueue_go_back();
1965 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1967 if (prefs.auto_focus && ! GTK_WIDGET_HAS_FOCUS(widget))
1968 gtk_widget_grab_focus(widget);
1970 return FALSE;
1974 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1976 GeanyDocument *doc;
1978 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1979 return;
1981 doc = document_get_current();
1982 g_return_if_fail(doc != NULL);
1984 editor_set_indent_type(doc->editor, type);
1985 ui_update_statusbar(doc, -1);
1989 void
1990 on_tabs1_activate (GtkCheckMenuItem *menuitem,
1991 gpointer user_data)
1993 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1997 void
1998 on_spaces1_activate (GtkCheckMenuItem *menuitem,
1999 gpointer user_data)
2001 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
2005 void
2006 on_tabs_and_spaces1_activate (GtkCheckMenuItem *menuitem,
2007 gpointer user_data)
2009 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
2013 void
2014 on_strip_trailing_spaces1_activate (GtkMenuItem *menuitem,
2015 gpointer user_data)
2017 GeanyDocument *doc;
2019 if (ignore_callback)
2020 return;
2022 doc = document_get_current();
2023 g_return_if_fail(doc != NULL);
2025 editor_strip_trailing_spaces(doc->editor);
2029 void
2030 on_page_setup1_activate (GtkMenuItem *menuitem,
2031 gpointer user_data)
2033 #if GTK_CHECK_VERSION(2, 10, 0)
2034 printing_page_setup_gtk();
2035 #endif
2039 gboolean
2040 on_escape_key_press_event (GtkWidget *widget,
2041 GdkEventKey *event,
2042 gpointer user_data)
2044 guint state = event->state & gtk_accelerator_get_default_mod_mask();
2046 /* make pressing escape in the sidebar and toolbar focus the editor */
2047 if (event->keyval == GDK_Escape && state == 0)
2049 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
2050 return TRUE;
2052 return FALSE;
2056 void
2057 on_line_breaking1_activate (GtkMenuItem *menuitem,
2058 gpointer user_data)
2060 GeanyDocument *doc;
2062 if (ignore_callback)
2063 return;
2065 doc = document_get_current();
2066 g_return_if_fail(doc != NULL);
2068 doc->editor->line_breaking = !doc->editor->line_breaking;
2072 void
2073 on_replace_spaces_activate (GtkMenuItem *menuitem,
2074 gpointer user_data)
2076 GeanyDocument *doc = document_get_current();
2078 g_return_if_fail(doc != NULL);
2080 editor_replace_spaces(doc->editor);
2084 void
2085 on_search1_activate (GtkMenuItem *menuitem,
2086 gpointer user_data)
2088 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
2089 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
2090 gboolean have_messages;
2092 /* enable commands if the messages window has any items */
2093 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
2094 NULL) > 0;
2096 gtk_widget_set_sensitive(next_message, have_messages);
2097 gtk_widget_set_sensitive(previous_message, have_messages);
2101 /* simple implementation (vs. close all which doesn't close documents if cancelled) */
2102 void
2103 on_close_other_documents1_activate (GtkMenuItem *menuitem,
2104 gpointer user_data)
2106 guint i;
2107 GeanyDocument *doc, *cur_doc = document_get_current();
2109 for (i = 0; i < documents_array->len; i++)
2111 doc = documents[i];
2113 if (doc == cur_doc || ! doc->is_valid)
2114 continue;
2116 if (! document_close(doc))
2117 break;
2122 void
2123 on_menu_reload_configuration1_activate (GtkMenuItem *menuitem,
2124 gpointer user_data)
2126 main_reload_configuration();
2130 void
2131 on_debug_messages1_activate (GtkMenuItem *menuitem,
2132 gpointer user_data)
2134 log_show_debug_messages_dialog();
2138 void
2139 on_send_selection_to_vte1_activate (GtkMenuItem *menuitem,
2140 gpointer user_data)
2142 #ifdef HAVE_VTE
2143 if (vte_info.load_vte)
2144 vte_send_selection_to_vte();
2145 #endif
2149 gboolean on_window_state_event (GtkWidget *widget,
2150 GdkEventWindowState *event,
2151 gpointer user_data)
2154 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
2156 static GtkWidget *menuitem = NULL;
2158 if (menuitem == NULL)
2159 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
2161 ignore_callback = TRUE;
2163 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
2164 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
2166 ignore_callback = FALSE;
2168 return FALSE;
2172 void
2173 on_customize_toolbar1_activate (GtkMenuItem *menuitem,
2174 gpointer user_data)
2176 GtkWidget *widget;
2177 GtkNotebook *notebook;
2179 prefs_show_dialog();
2181 /* select the KB page */
2182 widget = ui_lookup_widget(ui_widgets.prefs_dialog, "vbox15");
2183 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
2185 if (notebook != NULL && widget != NULL)
2186 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
2190 void
2191 on_button_customize_toolbar_clicked (GtkButton *button,
2192 gpointer user_data)
2194 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
2198 void
2199 on_cut_current_line_s_1_activate (GtkMenuItem *menuitem,
2200 gpointer user_data)
2202 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
2206 void
2207 on_copy_current_line_s_1_activate (GtkMenuItem *menuitem,
2208 gpointer user_data)
2210 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
2214 void
2215 on_delete_current_line_s_1_activate (GtkMenuItem *menuitem,
2216 gpointer user_data)
2218 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
2222 void
2223 on_duplicate_line_or_selection1_activate
2224 (GtkMenuItem *menuitem,
2225 gpointer user_data)
2227 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
2231 void
2232 on_select_current_line_s_1_activate (GtkMenuItem *menuitem,
2233 gpointer user_data)
2235 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
2239 void
2240 on_select_current_paragraph1_activate (GtkMenuItem *menuitem,
2241 gpointer user_data)
2243 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2247 void
2248 on_insert_alternative_white_space1_activate
2249 (GtkMenuItem *menuitem,
2250 gpointer user_data)
2252 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
2256 void
2257 on_go_to_next_marker1_activate (GtkMenuItem *menuitem,
2258 gpointer user_data)
2260 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
2264 void
2265 on_go_to_previous_marker1_activate (GtkMenuItem *menuitem,
2266 gpointer user_data)
2268 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
2272 void
2273 on_reflow_lines_block1_activate (GtkMenuItem *menuitem,
2274 gpointer user_data)
2276 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
2280 void
2281 on_transpose_current_line1_activate (GtkMenuItem *menuitem,
2282 gpointer user_data)
2284 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_TRANSPOSELINE);
2288 void
2289 on_smart_line_indent1_activate (GtkMenuItem *menuitem,
2290 gpointer user_data)
2292 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
2296 void
2297 on_plugin_preferences1_activate (GtkMenuItem *menuitem,
2298 gpointer user_data)
2300 #ifdef HAVE_PLUGINS
2301 plugin_show_configure(NULL);
2302 #endif