If only given an input filename and no match string, match all changelog entries.
[geany-mirror.git] / src / callbacks.c
blob7763872dbc1470d6939dc17ecfc4ac9bfc1c14ef
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 const 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 /* don't prompt if file hasn't been edited at all */
489 if ((!doc->changed && !document_can_undo(doc) && !document_can_redo(doc)) ||
490 dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
491 _("Any unsaved changes will be lost."),
492 _("Are you sure you want to reload '%s'?"), base_name))
494 document_reload_file(doc, charset);
495 if (charset != NULL)
496 ui_update_statusbar(doc, -1);
498 g_free(base_name);
502 void
503 on_change_font1_activate (GtkMenuItem *menuitem,
504 gpointer user_data)
506 dialogs_show_open_font();
510 /* new file */
511 void
512 on_toolbutton_new_clicked (GtkAction *action,
513 gpointer user_data)
515 document_new_file(NULL, NULL, NULL);
519 /* open file */
520 void
521 on_toolbutton_open_clicked (GtkAction *action,
522 gpointer user_data)
524 dialogs_show_open_file();
528 /* save file */
529 void
530 on_toolbutton_save_clicked (GtkAction *action,
531 gpointer user_data)
533 on_save1_activate(NULL, user_data);
537 /* store text, clear search flags so we can use Search->Find Next/Previous */
538 static void setup_find_next(const gchar *text)
540 setptr(search_data.text, g_strdup(text));
541 search_data.flags = 0;
542 search_data.backwards = FALSE;
543 search_data.search_bar = TRUE;
547 /* search text */
548 void
549 on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
551 GeanyDocument *doc = document_get_current();
552 gboolean result;
554 setup_find_next(text);
555 result = document_search_bar_find(doc, search_data.text, 0, GPOINTER_TO_INT(user_data));
556 if (search_data.search_bar)
557 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
561 /* search text */
562 void
563 on_toolbutton_search_clicked (GtkAction *action,
564 gpointer user_data)
566 GeanyDocument *doc = document_get_current();
567 gboolean result;
568 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
570 if (entry != NULL)
572 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
574 setup_find_next(text);
575 result = document_search_bar_find(doc, search_data.text, 0, FALSE);
576 if (search_data.search_bar)
577 ui_set_search_entry_background(entry, result);
579 else
580 on_find1_activate(NULL, NULL);
584 /* hides toolbar from toolbar popup menu */
585 void
586 on_hide_toolbar1_activate (GtkMenuItem *menuitem,
587 gpointer user_data)
589 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
590 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
594 /* zoom in from menu bar and popup menu */
595 void
596 on_zoom_in1_activate (GtkMenuItem *menuitem,
597 gpointer user_data)
599 GeanyDocument *doc = document_get_current();
600 static gint done = 1;
602 g_return_if_fail(doc != NULL);
604 if (done++ % 3 == 0)
605 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin,
606 (sci_get_zoom(doc->editor->sci) / 2));
607 sci_zoom_in(doc->editor->sci);
611 /* zoom out from menu bar and popup menu */
612 void
613 on_zoom_out1_activate (GtkMenuItem *menuitem,
614 gpointer user_data)
616 GeanyDocument *doc = document_get_current();
618 g_return_if_fail(doc != NULL);
620 if (sci_get_zoom(doc->editor->sci) == 0)
621 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
622 sci_zoom_out(doc->editor->sci);
626 void
627 on_normal_size1_activate (GtkMenuItem *menuitem,
628 gpointer user_data)
630 GeanyDocument *doc = document_get_current();
632 g_return_if_fail(doc != NULL);
634 sci_zoom_off(doc->editor->sci);
635 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
639 /* close tab */
640 void
641 on_toolbutton_close_clicked (GtkAction *action,
642 gpointer user_data)
644 on_close1_activate(NULL, NULL);
648 void
649 on_toolbutton_close_all_clicked (GtkAction *action,
650 gpointer user_data)
652 on_close_all1_activate(NULL, NULL);
656 void
657 on_toolbutton_preferences_clicked (GtkAction *action,
658 gpointer user_data)
660 on_preferences1_activate(NULL, NULL);
664 static gboolean delayed_check_disk_status(gpointer data)
666 document_check_disk_status(data, FALSE);
667 return FALSE;
671 /* Changes window-title after switching tabs and lots of other things.
672 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
673 void
674 on_notebook1_switch_page_after (GtkNotebook *notebook,
675 GtkNotebookPage *page,
676 guint page_num,
677 gpointer user_data)
679 GeanyDocument *doc;
681 if (G_UNLIKELY(main_status.opening_session_files) || G_UNLIKELY(main_status.closing_all))
682 return;
684 if (page_num == (guint) -1 && page != NULL)
685 doc = document_find_by_sci(SCINTILLA(page));
686 else
687 doc = document_get_from_page(page_num);
689 if (doc != NULL)
691 sidebar_select_openfiles_item(doc);
692 ui_save_buttons_toggle(doc->changed);
693 ui_set_window_title(doc);
694 ui_update_statusbar(doc, -1);
695 ui_update_popup_reundo_items(doc);
696 ui_document_show_hide(doc); /* update the document menu */
697 build_menu_update(doc);
698 sidebar_update_tag_list(doc, FALSE);
700 /* We delay the check to avoid weird fast, unintended switching of notebook pages when
701 * the 'file has changed' dialog is shown while the switch event is not yet completely
702 * finished. So, we check after the switch has been performed to be safe. */
703 g_idle_add(delayed_check_disk_status, doc);
705 #ifdef HAVE_VTE
706 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
707 #endif
709 g_signal_emit_by_name(geany_object, "document-activate", doc);
714 void
715 on_tv_notebook_switch_page (GtkNotebook *notebook,
716 GtkNotebookPage *page,
717 guint page_num,
718 gpointer user_data)
720 /* suppress selection changed signal when switching to the open files list */
721 ignore_callback = TRUE;
725 void
726 on_tv_notebook_switch_page_after (GtkNotebook *notebook,
727 GtkNotebookPage *page,
728 guint page_num,
729 gpointer user_data)
731 ignore_callback = FALSE;
735 static void convert_eol(gint mode)
737 GeanyDocument *doc = document_get_current();
739 g_return_if_fail(doc != NULL);
741 sci_convert_eols(doc->editor->sci, mode);
742 sci_set_eol_mode(doc->editor->sci, mode);
743 ui_update_statusbar(doc, -1);
747 void
748 on_crlf_activate (GtkCheckMenuItem *menuitem,
749 gpointer user_data)
751 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
752 return;
754 convert_eol(SC_EOL_CRLF);
758 void
759 on_lf_activate (GtkCheckMenuItem *menuitem,
760 gpointer user_data)
762 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
763 return;
765 convert_eol(SC_EOL_LF);
769 void
770 on_cr_activate (GtkCheckMenuItem *menuitem,
771 gpointer user_data)
773 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
774 return;
776 convert_eol(SC_EOL_CR);
780 void
781 on_replace_tabs_activate (GtkMenuItem *menuitem,
782 gpointer user_data)
784 GeanyDocument *doc = document_get_current();
786 g_return_if_fail(doc != NULL);
788 editor_replace_tabs(doc->editor);
792 gboolean
793 toolbar_popup_menu (GtkWidget *widget,
794 GdkEventButton *event,
795 gpointer user_data)
797 if (event->button == 3)
799 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
800 return TRUE;
802 return FALSE;
806 void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
808 GeanyDocument *doc = document_get_current();
809 ScintillaObject *sci;
810 gchar *text;
811 gboolean keep_sel = TRUE;
813 g_return_if_fail(doc != NULL);
815 sci = doc->editor->sci;
816 if (! sci_has_selection(sci))
818 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
819 keep_sel = FALSE;
821 else
823 gchar *result = NULL;
824 gint cmd = SCI_LOWERCASE;
825 gint text_len = sci_get_selected_text_length(sci);
826 gboolean rectsel = scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
828 text = g_malloc(text_len + 1);
829 sci_get_selected_text(sci, text);
831 if (utils_str_has_upper(text))
833 if (rectsel)
834 cmd = SCI_LOWERCASE;
835 else
836 result = g_utf8_strdown(text, -1);
839 else
841 if (rectsel)
842 cmd = SCI_UPPERCASE;
843 else
844 result = g_utf8_strup(text, -1);
848 if (result != NULL)
850 sci_replace_sel(sci, result);
851 g_free(result);
852 if (keep_sel)
853 sci_set_selection_start(sci, sci_get_current_position(sci) - text_len + 1);
855 else
856 sci_send_command(sci, cmd);
858 g_free(text);
864 void
865 on_show_toolbar1_toggled (GtkCheckMenuItem *checkmenuitem,
866 gpointer user_data)
868 if (ignore_callback) return;
870 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
871 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
875 void
876 on_fullscreen1_toggled (GtkCheckMenuItem *checkmenuitem,
877 gpointer user_data)
879 if (ignore_callback)
880 return;
882 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
883 ui_set_fullscreen();
887 void
888 on_show_messages_window1_toggled (GtkCheckMenuItem *checkmenuitem,
889 gpointer user_data)
891 if (ignore_callback)
892 return;
894 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
895 msgwin_show_hide(ui_prefs.msgwindow_visible);
899 void
900 on_markers_margin1_toggled (GtkCheckMenuItem *checkmenuitem,
901 gpointer user_data)
903 if (ignore_callback)
904 return;
906 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
907 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
911 void
912 on_show_line_numbers1_toggled (GtkCheckMenuItem *checkmenuitem,
913 gpointer user_data)
915 if (ignore_callback)
916 return;
918 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
919 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
923 void
924 on_menu_show_white_space1_toggled (GtkCheckMenuItem *checkmenuitem,
925 gpointer user_data)
927 if (ignore_callback)
928 return;
930 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
931 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
935 void
936 on_menu_show_line_endings1_toggled (GtkCheckMenuItem *checkmenuitem,
937 gpointer user_data)
939 if (ignore_callback)
940 return;
942 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
943 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
947 void
948 on_menu_show_indentation_guides1_toggled (GtkCheckMenuItem *checkmenuitem,
949 gpointer user_data)
951 if (ignore_callback)
952 return;
954 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
955 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
959 void
960 on_line_wrapping1_toggled (GtkCheckMenuItem *checkmenuitem,
961 gpointer user_data)
963 if (! ignore_callback)
965 GeanyDocument *doc = document_get_current();
966 g_return_if_fail(doc != NULL);
968 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
973 void
974 on_set_file_readonly1_toggled (GtkCheckMenuItem *checkmenuitem,
975 gpointer user_data)
977 if (! ignore_callback)
979 GeanyDocument *doc = document_get_current();
980 g_return_if_fail(doc != NULL);
982 doc->readonly = ! doc->readonly;
983 sci_set_readonly(doc->editor->sci, doc->readonly);
984 ui_update_tab_status(doc);
985 ui_update_statusbar(doc, -1);
990 void
991 on_use_auto_indentation1_toggled (GtkCheckMenuItem *checkmenuitem,
992 gpointer user_data)
994 if (! ignore_callback)
996 GeanyDocument *doc = document_get_current();
997 g_return_if_fail(doc != NULL);
999 doc->editor->auto_indent = ! doc->editor->auto_indent;
1004 static void find_usage(gboolean in_session)
1006 gint flags;
1007 gchar *search_text;
1008 GeanyDocument *doc = document_get_current();
1010 g_return_if_fail(doc != NULL);
1012 if (sci_has_selection(doc->editor->sci))
1013 { /* take selected text if there is a selection */
1014 search_text = g_malloc(sci_get_selected_text_length(doc->editor->sci) + 1);
1015 sci_get_selected_text(doc->editor->sci, search_text);
1016 flags = SCFIND_MATCHCASE;
1018 else
1020 editor_find_current_word(doc->editor, -1,
1021 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1022 search_text = g_strdup(editor_info.current_word);
1023 flags = SCFIND_MATCHCASE | SCFIND_WHOLEWORD;
1026 search_find_usage(search_text, flags, in_session);
1027 g_free(search_text);
1031 void
1032 on_find_document_usage1_activate (GtkMenuItem *menuitem,
1033 gpointer user_data)
1035 find_usage(FALSE);
1039 void
1040 on_find_usage1_activate (GtkMenuItem *menuitem,
1041 gpointer user_data)
1043 find_usage(TRUE);
1047 static void goto_tag(gboolean definition)
1049 GeanyDocument *doc = document_get_current();
1051 g_return_if_fail(doc != NULL);
1053 /* update cursor pos for navigating back afterwards */
1054 if (!sci_has_selection(doc->editor->sci))
1055 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
1057 /* use the keybinding callback as it checks for selections as well as current word */
1058 if (definition)
1059 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
1060 else
1061 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
1065 void
1066 on_goto_tag_definition1 (GtkMenuItem *menuitem,
1067 gpointer user_data)
1069 goto_tag(TRUE);
1073 void
1074 on_goto_tag_declaration1 (GtkMenuItem *menuitem,
1075 gpointer user_data)
1077 goto_tag(FALSE);
1081 void
1082 on_count_words1_activate (GtkMenuItem *menuitem,
1083 gpointer user_data)
1085 tools_word_count();
1089 void
1090 on_show_color_chooser1_activate (GtkMenuItem *menuitem,
1091 gpointer user_data)
1093 gchar colour[9];
1094 GeanyDocument *doc = document_get_current();
1095 gint pos;
1097 g_return_if_fail(doc != NULL);
1099 pos = sci_get_current_position(doc->editor->sci);
1100 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
1101 tools_color_chooser(colour);
1105 void
1106 on_toolbutton_compile_clicked (GtkAction *action,
1107 gpointer user_data)
1109 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
1113 void
1114 on_find1_activate (GtkMenuItem *menuitem,
1115 gpointer user_data)
1117 search_show_find_dialog();
1121 void
1122 on_find_next1_activate (GtkMenuItem *menuitem,
1123 gpointer user_data)
1125 search_find_again(FALSE);
1129 void
1130 on_find_previous1_activate (GtkMenuItem *menuitem,
1131 gpointer user_data)
1133 if (search_data.flags & SCFIND_REGEXP)
1134 /* Can't reverse search order for a regex (find next ignores search backwards) */
1135 utils_beep();
1136 else
1137 search_find_again(TRUE);
1141 void
1142 on_find_nextsel1_activate (GtkMenuItem *menuitem,
1143 gpointer user_data)
1145 search_find_selection(document_get_current(), FALSE);
1149 void
1150 on_find_prevsel1_activate (GtkMenuItem *menuitem,
1151 gpointer user_data)
1153 search_find_selection(document_get_current(), TRUE);
1157 void
1158 on_replace1_activate (GtkMenuItem *menuitem,
1159 gpointer user_data)
1161 search_show_replace_dialog();
1165 void
1166 on_find_in_files1_activate (GtkMenuItem *menuitem,
1167 gpointer user_data)
1169 search_show_find_in_files_dialog(NULL);
1173 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
1175 if (*text == '+' || *text == '-')
1177 *line_no = atoi(text + 1);
1178 *offset = (*text == '+') ? 1 : -1;
1180 else
1182 *line_no = atoi(text) - 1;
1183 *offset = 0;
1188 void
1189 on_go_to_line_activate (GtkMenuItem *menuitem,
1190 gpointer user_data)
1192 static gchar value[16] = "";
1193 gchar *result;
1195 result = dialogs_show_input_goto_line(
1196 _("Go to Line"), GTK_WINDOW(main_widgets.window),
1197 _("Enter the line you want to go to:"), value);
1198 if (result != NULL)
1200 GeanyDocument *doc = document_get_current();
1201 gint offset;
1202 gint line_no;
1204 g_return_if_fail(doc != NULL);
1206 get_line_and_offset_from_text(result, &line_no, &offset);
1207 if (! editor_goto_line(doc->editor, line_no, offset))
1208 utils_beep();
1209 /* remember value for future calls */
1210 g_snprintf(value, sizeof(value), "%s", result);
1212 g_free(result);
1217 void
1218 on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
1220 GeanyDocument *doc = document_get_current();
1221 gint offset;
1222 gint line_no;
1224 g_return_if_fail(doc != NULL);
1226 get_line_and_offset_from_text(text, &line_no, &offset);
1227 if (! editor_goto_line(doc->editor, line_no, offset))
1228 utils_beep();
1229 else
1230 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1234 void
1235 on_toolbutton_goto_clicked (GtkAction *action,
1236 gpointer user_data)
1238 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
1240 if (entry != NULL)
1242 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
1244 on_toolbutton_goto_entry_activate(NULL, text, NULL);
1246 else
1247 on_go_to_line_activate(NULL, NULL);
1251 void
1252 on_help1_activate (GtkMenuItem *menuitem,
1253 gpointer user_data)
1255 gchar *uri;
1257 uri = utils_get_help_url(NULL);
1258 utils_open_browser(uri);
1259 g_free(uri);
1263 void
1264 on_help_shortcuts1_activate (GtkMenuItem *menuitem,
1265 gpointer user_data)
1267 keybindings_show_shortcuts();
1271 void
1272 on_website1_activate (GtkMenuItem *menuitem,
1273 gpointer user_data)
1275 utils_open_browser(GEANY_HOMEPAGE);
1279 void
1280 on_comments_function_activate (GtkMenuItem *menuitem,
1281 gpointer user_data)
1283 GeanyDocument *doc = document_get_current();
1284 gchar *text;
1285 const gchar *cur_tag = NULL;
1286 gint line = -1, pos = 0;
1288 if (doc == NULL || doc->file_type == NULL)
1290 ui_set_statusbar(FALSE,
1291 _("Please set the filetype for the current file before using this function."));
1292 return;
1295 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
1296 * returns the current position, so it should be safe */
1297 line = symbols_get_current_function(doc, &cur_tag);
1298 pos = sci_get_position_from_line(doc->editor->sci, line - 1);
1300 text = templates_get_template_function(doc, cur_tag);
1302 sci_insert_text(doc->editor->sci, pos, text);
1303 g_free(text);
1307 void
1308 on_comments_multiline_activate (GtkMenuItem *menuitem,
1309 gpointer user_data)
1311 GeanyDocument *doc = document_get_current();
1313 if (doc == NULL || doc->file_type == NULL)
1315 ui_set_statusbar(FALSE,
1316 _("Please set the filetype for the current file before using this function."));
1317 return;
1320 verify_click_pos(doc); /* make sure that the click_pos is valid */
1322 if (doc->file_type->comment_open)
1323 editor_insert_multiline_comment(doc->editor);
1324 else
1325 utils_beep();
1329 void
1330 on_comments_gpl_activate (GtkMenuItem *menuitem,
1331 gpointer user_data)
1333 GeanyDocument *doc = document_get_current();
1334 gchar *text;
1336 g_return_if_fail(doc != NULL);
1338 text = templates_get_template_licence(doc, GEANY_TEMPLATE_GPL);
1340 verify_click_pos(doc); /* make sure that the click_pos is valid */
1342 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1343 g_free(text);
1347 void
1348 on_comments_bsd_activate (GtkMenuItem *menuitem,
1349 gpointer user_data)
1352 GeanyDocument *doc = document_get_current();
1353 gchar *text;
1355 g_return_if_fail(doc != NULL);
1357 text = templates_get_template_licence(doc, GEANY_TEMPLATE_BSD);
1359 verify_click_pos(doc); /* make sure that the click_pos is valid */
1361 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1362 g_free(text);
1367 void
1368 on_comments_changelog_activate (GtkMenuItem *menuitem,
1369 gpointer user_data)
1371 GeanyDocument *doc = document_get_current();
1372 gchar *text;
1374 g_return_if_fail(doc != NULL);
1376 text = templates_get_template_changelog(doc);
1377 sci_insert_text(doc->editor->sci, 0, text);
1378 /* sets the cursor to the right position to type the changelog text,
1379 * the template has 21 chars + length of name and email */
1380 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1382 g_free(text);
1386 void
1387 on_comments_fileheader_activate (GtkMenuItem *menuitem,
1388 gpointer user_data)
1390 GeanyDocument *doc = document_get_current();
1391 gchar *text;
1392 const gchar *fname;
1393 GeanyFiletype *ft;
1395 g_return_if_fail(doc != NULL);
1397 ft = doc->file_type;
1398 fname = doc->file_name;
1399 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1401 sci_insert_text(doc->editor->sci, 0, text);
1402 sci_goto_pos(doc->editor->sci, 0, FALSE);
1403 g_free(text);
1407 void
1408 on_insert_date_activate (GtkMenuItem *menuitem,
1409 gpointer user_data)
1411 GeanyDocument *doc = document_get_current();
1412 const gchar *format = NULL;
1413 gchar *time_str;
1415 g_return_if_fail(doc != NULL);
1417 /* set default value */
1418 if (utils_str_equal("", ui_prefs.custom_date_format))
1420 g_free(ui_prefs.custom_date_format);
1421 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
1424 if (utils_str_equal(_("dd.mm.yyyy"), (gchar*) user_data))
1425 format = "%d.%m.%Y";
1426 else if (utils_str_equal(_("mm.dd.yyyy"), (gchar*) user_data))
1427 format = "%m.%d.%Y";
1428 else if (utils_str_equal(_("yyyy/mm/dd"), (gchar*) user_data))
1429 format = "%Y/%m/%d";
1430 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), (gchar*) user_data))
1431 format = "%d.%m.%Y %H:%M:%S";
1432 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), (gchar*) user_data))
1433 format = "%m.%d.%Y %H:%M:%S";
1434 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), (gchar*) user_data))
1435 format = "%Y/%m/%d %H:%M:%S";
1436 else if (utils_str_equal(_("_Use Custom Date Format"), (gchar*) user_data))
1437 format = ui_prefs.custom_date_format;
1438 else
1440 gchar *str = dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets.window),
1441 _("Enter here a custom date and time format. "
1442 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
1443 ui_prefs.custom_date_format);
1444 if (str)
1445 setptr(ui_prefs.custom_date_format, str);
1446 return;
1449 time_str = utils_get_date_time(format, NULL);
1450 if (time_str != NULL)
1452 verify_click_pos(doc); /* make sure that the click_pos is valid */
1454 sci_insert_text(doc->editor->sci, editor_info.click_pos, time_str);
1455 sci_goto_pos(doc->editor->sci, editor_info.click_pos + strlen(time_str), FALSE);
1456 g_free(time_str);
1458 else
1460 utils_beep();
1461 ui_set_statusbar(TRUE,
1462 _("Date format string could not be converted (possibly too long)."));
1467 void
1468 on_insert_include_activate (GtkMenuItem *menuitem,
1469 gpointer user_data)
1471 GeanyDocument *doc = document_get_current();
1472 gint pos = -1;
1473 gchar *text;
1475 g_return_if_fail(doc != NULL);
1476 g_return_if_fail(user_data != NULL);
1478 verify_click_pos(doc); /* make sure that the click_pos is valid */
1480 if (utils_str_equal(user_data, "blank"))
1482 text = g_strdup("#include \"\"\n");
1483 pos = editor_info.click_pos + 10;
1485 else
1487 text = g_strconcat("#include <", user_data, ">\n", NULL);
1490 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1491 g_free(text);
1492 if (pos >= 0)
1493 sci_goto_pos(doc->editor->sci, pos, FALSE);
1497 void
1498 on_file_properties_activate (GtkMenuItem *menuitem,
1499 gpointer user_data)
1501 GeanyDocument *doc = document_get_current();
1502 g_return_if_fail(doc != NULL);
1504 dialogs_show_file_properties(doc);
1508 void
1509 on_menu_fold_all1_activate (GtkMenuItem *menuitem,
1510 gpointer user_data)
1512 GeanyDocument *doc = document_get_current();
1513 g_return_if_fail(doc != NULL);
1515 editor_fold_all(doc->editor);
1519 void
1520 on_menu_unfold_all1_activate (GtkMenuItem *menuitem,
1521 gpointer user_data)
1523 GeanyDocument *doc = document_get_current();
1524 g_return_if_fail(doc != NULL);
1526 editor_unfold_all(doc->editor);
1530 void
1531 on_toolbutton_run_clicked (GtkAction *action,
1532 gpointer user_data)
1534 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1538 void
1539 on_menu_remove_indicators1_activate (GtkMenuItem *menuitem,
1540 gpointer user_data)
1542 GeanyDocument *doc = document_get_current();
1543 g_return_if_fail(doc != NULL);
1545 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1549 void
1550 on_print1_activate (GtkMenuItem *menuitem,
1551 gpointer user_data)
1553 GeanyDocument *doc = document_get_current();
1554 g_return_if_fail(doc != NULL);
1556 printing_print_doc(doc);
1560 void
1561 on_menu_select_all1_activate (GtkMenuItem *menuitem,
1562 gpointer user_data)
1564 GeanyDocument *doc = document_get_current();
1565 g_return_if_fail(doc != NULL);
1567 sci_select_all(doc->editor->sci);
1571 void
1572 on_menu_show_sidebar1_toggled (GtkCheckMenuItem *checkmenuitem,
1573 gpointer user_data)
1575 if (ignore_callback)
1576 return;
1578 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1580 /* show built-in tabs if no tabs visible */
1581 if (ui_prefs.sidebar_visible &&
1582 ! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1583 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1585 interface_prefs.sidebar_openfiles_visible = TRUE;
1586 interface_prefs.sidebar_symbol_visible = TRUE;
1589 #if GTK_CHECK_VERSION(2, 14, 0)
1590 /* if window has input focus, set it back to the editor before toggling off */
1591 if (! ui_prefs.sidebar_visible &&
1592 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1594 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1596 #endif
1598 ui_sidebar_show_hide();
1602 void
1603 on_menu_write_unicode_bom1_toggled (GtkCheckMenuItem *checkmenuitem,
1604 gpointer user_data)
1606 if (! ignore_callback)
1608 GeanyDocument *doc = document_get_current();
1610 g_return_if_fail(doc != NULL);
1611 if (doc->readonly)
1613 utils_beep();
1614 return;
1617 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1619 doc->has_bom = ! doc->has_bom;
1621 ui_update_statusbar(doc, -1);
1626 void
1627 on_menu_comment_line1_activate (GtkMenuItem *menuitem,
1628 gpointer user_data)
1630 GeanyDocument *doc = document_get_current();
1631 g_return_if_fail(doc != NULL);
1633 editor_do_comment(doc->editor, -1, FALSE, FALSE);
1637 void
1638 on_menu_uncomment_line1_activate (GtkMenuItem *menuitem,
1639 gpointer user_data)
1641 GeanyDocument *doc = document_get_current();
1642 g_return_if_fail(doc != NULL);
1644 editor_do_uncomment(doc->editor, -1, FALSE);
1648 void
1649 on_menu_toggle_line_commentation1_activate
1650 (GtkMenuItem *menuitem,
1651 gpointer user_data)
1653 GeanyDocument *doc = document_get_current();
1654 g_return_if_fail(doc != NULL);
1656 editor_do_comment_toggle(doc->editor);
1660 void
1661 on_menu_increase_indent1_activate (GtkMenuItem *menuitem,
1662 gpointer user_data)
1664 GeanyDocument *doc = document_get_current();
1665 g_return_if_fail(doc != NULL);
1667 editor_indent(doc->editor, TRUE);
1671 void
1672 on_menu_decrease_indent1_activate (GtkMenuItem *menuitem,
1673 gpointer user_data)
1675 GeanyDocument *doc = document_get_current();
1676 g_return_if_fail(doc != NULL);
1678 editor_indent(doc->editor, FALSE);
1682 void
1683 on_next_message1_activate (GtkMenuItem *menuitem,
1684 gpointer user_data)
1686 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1687 msgwin_goto_messages_file_line))
1688 ui_set_statusbar(FALSE, _("No more message items."));
1692 void
1693 on_previous_message1_activate (GtkMenuItem *menuitem,
1694 gpointer user_data)
1696 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1697 msgwin_goto_messages_file_line))
1698 ui_set_statusbar(FALSE, _("No more message items."));
1702 void
1703 on_menu_comments_multiline_activate (GtkMenuItem *menuitem,
1704 gpointer user_data)
1706 insert_callback_from_menu = TRUE;
1707 on_comments_multiline_activate(menuitem, user_data);
1711 void
1712 on_menu_comments_gpl_activate (GtkMenuItem *menuitem,
1713 gpointer user_data)
1715 insert_callback_from_menu = TRUE;
1716 on_comments_gpl_activate(menuitem, user_data);
1720 void
1721 on_menu_comments_bsd_activate (GtkMenuItem *menuitem,
1722 gpointer user_data)
1724 insert_callback_from_menu = TRUE;
1725 on_comments_bsd_activate(menuitem, user_data);
1729 void
1730 on_menu_insert_include_activate (GtkMenuItem *menuitem,
1731 gpointer user_data)
1733 insert_callback_from_menu = TRUE;
1734 on_insert_include_activate(menuitem, user_data);
1738 void
1739 on_menu_insert_date_activate (GtkMenuItem *menuitem,
1740 gpointer user_data)
1742 insert_callback_from_menu = TRUE;
1743 on_insert_date_activate(menuitem, user_data);
1747 void
1748 on_project_new1_activate (GtkMenuItem *menuitem,
1749 gpointer user_data)
1751 project_new();
1755 void
1756 on_project_open1_activate (GtkMenuItem *menuitem,
1757 gpointer user_data)
1759 project_open();
1763 void
1764 on_project_close1_activate (GtkMenuItem *menuitem,
1765 gpointer user_data)
1767 project_close(TRUE);
1771 void
1772 on_project_properties1_activate (GtkMenuItem *menuitem,
1773 gpointer user_data)
1775 project_properties();
1779 void
1780 on_menu_project1_activate (GtkMenuItem *menuitem,
1781 gpointer user_data)
1783 static GtkWidget *item_close = NULL;
1784 static GtkWidget *item_properties = NULL;
1786 if (item_close == NULL)
1788 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1789 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1792 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1793 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1794 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1795 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1799 void
1800 on_menu_open_selected_file1_activate (GtkMenuItem *menuitem,
1801 gpointer user_data)
1803 GeanyDocument *doc = document_get_current();
1804 gchar *sel = NULL;
1805 const gchar *wc;
1807 #ifdef G_OS_WIN32
1808 wc = GEANY_WORDCHARS "./-" "\\";
1809 #else
1810 wc = GEANY_WORDCHARS "./-";
1811 #endif
1813 g_return_if_fail(doc != NULL);
1815 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1817 if (sel != NULL)
1819 gchar *locale_filename, *filename = NULL;
1821 if (g_path_is_absolute(sel))
1822 filename = g_strdup(sel);
1823 else
1824 { /* relative filename, add the path of the current file */
1825 gchar *path;
1827 path = utils_get_current_file_dir_utf8();
1828 if (!path)
1829 path = g_get_current_dir();
1831 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1833 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1834 app->project != NULL && NZV(app->project->base_path))
1836 /* try the project's base path */
1837 setptr(path, project_get_base_path());
1838 setptr(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1840 g_free(path);
1843 locale_filename = utils_get_locale_from_utf8(filename);
1844 document_open_file(locale_filename, FALSE, NULL, NULL);
1846 g_free(filename);
1847 g_free(locale_filename);
1848 g_free(sel);
1853 void
1854 on_remove_markers1_activate (GtkMenuItem *menuitem,
1855 gpointer user_data)
1857 GeanyDocument *doc = document_get_current();
1858 g_return_if_fail(doc != NULL);
1860 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1861 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1862 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1866 void
1867 on_load_tags1_activate (GtkMenuItem *menuitem,
1868 gpointer user_data)
1870 symbols_show_load_tags_dialog();
1874 void
1875 on_context_action1_activate (GtkMenuItem *menuitem,
1876 gpointer user_data)
1878 gchar *word, *command;
1879 GError *error = NULL;
1880 GeanyDocument *doc = document_get_current();
1882 g_return_if_fail(doc != NULL);
1884 if (sci_has_selection(doc->editor->sci))
1885 { /* take selected text if there is a selection */
1886 word = g_malloc(sci_get_selected_text_length(doc->editor->sci) + 1);
1887 sci_get_selected_text(doc->editor->sci, word);
1889 else
1891 word = g_strdup(editor_info.current_word);
1894 /* use the filetype specific command if available, fallback to global command otherwise */
1895 if (doc->file_type != NULL &&
1896 NZV(doc->file_type->context_action_cmd))
1898 command = g_strdup(doc->file_type->context_action_cmd);
1900 else
1902 command = g_strdup(tool_prefs.context_action_cmd);
1905 /* substitute the wildcard %s and run the command if it is non empty */
1906 if (NZV(command))
1908 utils_str_replace_all(&command, "%s", word);
1910 if (! g_spawn_command_line_async(command, &error))
1912 ui_set_statusbar(TRUE, "Context action command failed: %s", error->message);
1913 g_error_free(error);
1916 g_free(word);
1917 g_free(command);
1921 void
1922 on_menu_toggle_all_additional_widgets1_activate
1923 (GtkMenuItem *menuitem,
1924 gpointer user_data)
1926 static gint hide_all = -1;
1927 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1928 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1929 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1930 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1932 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1933 if (G_UNLIKELY(hide_all == -1))
1935 if (! gtk_check_menu_item_get_active(msgw) &&
1936 ! interface_prefs.show_notebook_tabs &&
1937 ! gtk_check_menu_item_get_active(toolbari))
1939 hide_all = TRUE;
1941 else
1942 hide_all = FALSE;
1945 hide_all = ! hide_all; /* toggle */
1947 if (hide_all)
1949 if (gtk_check_menu_item_get_active(msgw))
1950 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1952 interface_prefs.show_notebook_tabs = FALSE;
1953 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1955 ui_statusbar_showhide(FALSE);
1957 if (gtk_check_menu_item_get_active(toolbari))
1958 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1960 else
1963 if (! gtk_check_menu_item_get_active(msgw))
1964 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1966 interface_prefs.show_notebook_tabs = TRUE;
1967 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1969 ui_statusbar_showhide(TRUE);
1971 if (! gtk_check_menu_item_get_active(toolbari))
1972 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1977 void
1978 on_forward_activate (GtkMenuItem *menuitem,
1979 gpointer user_data)
1981 navqueue_go_forward();
1985 void
1986 on_back_activate (GtkMenuItem *menuitem,
1987 gpointer user_data)
1989 navqueue_go_back();
1993 gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1995 if (prefs.auto_focus && ! GTK_WIDGET_HAS_FOCUS(widget))
1996 gtk_widget_grab_focus(widget);
1998 return FALSE;
2002 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
2004 GeanyDocument *doc;
2006 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
2007 return;
2009 doc = document_get_current();
2010 g_return_if_fail(doc != NULL);
2012 editor_set_indent(doc->editor, type, doc->editor->indent_width);
2013 ui_update_statusbar(doc, -1);
2017 void
2018 on_tabs1_activate (GtkCheckMenuItem *menuitem,
2019 gpointer user_data)
2021 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
2025 void
2026 on_spaces1_activate (GtkCheckMenuItem *menuitem,
2027 gpointer user_data)
2029 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
2033 void
2034 on_tabs_and_spaces1_activate (GtkCheckMenuItem *menuitem,
2035 gpointer user_data)
2037 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
2041 void
2042 on_strip_trailing_spaces1_activate (GtkMenuItem *menuitem,
2043 gpointer user_data)
2045 GeanyDocument *doc;
2047 if (ignore_callback)
2048 return;
2050 doc = document_get_current();
2051 g_return_if_fail(doc != NULL);
2053 editor_strip_trailing_spaces(doc->editor);
2057 void
2058 on_page_setup1_activate (GtkMenuItem *menuitem,
2059 gpointer user_data)
2061 #if GTK_CHECK_VERSION(2, 10, 0)
2062 printing_page_setup_gtk();
2063 #endif
2067 gboolean
2068 on_escape_key_press_event (GtkWidget *widget,
2069 GdkEventKey *event,
2070 gpointer user_data)
2072 guint state = event->state & gtk_accelerator_get_default_mod_mask();
2074 /* make pressing escape in the sidebar and toolbar focus the editor */
2075 if (event->keyval == GDK_Escape && state == 0)
2077 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
2078 return TRUE;
2080 return FALSE;
2084 void
2085 on_line_breaking1_activate (GtkMenuItem *menuitem,
2086 gpointer user_data)
2088 GeanyDocument *doc;
2090 if (ignore_callback)
2091 return;
2093 doc = document_get_current();
2094 g_return_if_fail(doc != NULL);
2096 doc->editor->line_breaking = !doc->editor->line_breaking;
2100 void
2101 on_replace_spaces_activate (GtkMenuItem *menuitem,
2102 gpointer user_data)
2104 GeanyDocument *doc = document_get_current();
2106 g_return_if_fail(doc != NULL);
2108 editor_replace_spaces(doc->editor);
2112 void
2113 on_search1_activate (GtkMenuItem *menuitem,
2114 gpointer user_data)
2116 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
2117 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
2118 gboolean have_messages;
2120 /* enable commands if the messages window has any items */
2121 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
2122 NULL) > 0;
2124 gtk_widget_set_sensitive(next_message, have_messages);
2125 gtk_widget_set_sensitive(previous_message, have_messages);
2129 /* simple implementation (vs. close all which doesn't close documents if cancelled) */
2130 void
2131 on_close_other_documents1_activate (GtkMenuItem *menuitem,
2132 gpointer user_data)
2134 guint i;
2135 GeanyDocument *doc, *cur_doc = document_get_current();
2137 for (i = 0; i < documents_array->len; i++)
2139 doc = documents[i];
2141 if (doc == cur_doc || ! doc->is_valid)
2142 continue;
2144 if (! document_close(doc))
2145 break;
2150 void
2151 on_menu_reload_configuration1_activate (GtkMenuItem *menuitem,
2152 gpointer user_data)
2154 main_reload_configuration();
2158 void
2159 on_debug_messages1_activate (GtkMenuItem *menuitem,
2160 gpointer user_data)
2162 log_show_debug_messages_dialog();
2166 void
2167 on_send_selection_to_vte1_activate (GtkMenuItem *menuitem,
2168 gpointer user_data)
2170 #ifdef HAVE_VTE
2171 if (vte_info.have_vte)
2172 vte_send_selection_to_vte();
2173 #endif
2177 gboolean on_window_state_event (GtkWidget *widget,
2178 GdkEventWindowState *event,
2179 gpointer user_data)
2182 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
2184 static GtkWidget *menuitem = NULL;
2186 if (menuitem == NULL)
2187 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
2189 ignore_callback = TRUE;
2191 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
2192 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
2194 ignore_callback = FALSE;
2196 return FALSE;
2200 void
2201 on_customize_toolbar1_activate (GtkMenuItem *menuitem,
2202 gpointer user_data)
2204 GtkWidget *widget;
2205 GtkNotebook *notebook;
2207 prefs_show_dialog();
2209 /* select the KB page */
2210 widget = ui_lookup_widget(ui_widgets.prefs_dialog, "vbox15");
2211 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
2213 if (notebook != NULL && widget != NULL)
2214 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
2218 void
2219 on_button_customize_toolbar_clicked (GtkButton *button,
2220 gpointer user_data)
2222 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
2226 void
2227 on_cut_current_line_s_1_activate (GtkMenuItem *menuitem,
2228 gpointer user_data)
2230 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
2234 void
2235 on_copy_current_line_s_1_activate (GtkMenuItem *menuitem,
2236 gpointer user_data)
2238 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
2242 void
2243 on_delete_current_line_s_1_activate (GtkMenuItem *menuitem,
2244 gpointer user_data)
2246 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
2250 void
2251 on_duplicate_line_or_selection1_activate
2252 (GtkMenuItem *menuitem,
2253 gpointer user_data)
2255 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
2259 void
2260 on_select_current_line_s_1_activate (GtkMenuItem *menuitem,
2261 gpointer user_data)
2263 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
2267 void
2268 on_select_current_paragraph1_activate (GtkMenuItem *menuitem,
2269 gpointer user_data)
2271 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2275 void
2276 on_insert_alternative_white_space1_activate
2277 (GtkMenuItem *menuitem,
2278 gpointer user_data)
2280 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
2284 void
2285 on_go_to_next_marker1_activate (GtkMenuItem *menuitem,
2286 gpointer user_data)
2288 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
2292 void
2293 on_go_to_previous_marker1_activate (GtkMenuItem *menuitem,
2294 gpointer user_data)
2296 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
2300 void
2301 on_reflow_lines_block1_activate (GtkMenuItem *menuitem,
2302 gpointer user_data)
2304 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
2308 void
2309 on_transpose_current_line1_activate (GtkMenuItem *menuitem,
2310 gpointer user_data)
2312 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_TRANSPOSELINE);
2316 void
2317 on_smart_line_indent1_activate (GtkMenuItem *menuitem,
2318 gpointer user_data)
2320 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
2324 void
2325 on_plugin_preferences1_activate (GtkMenuItem *menuitem,
2326 gpointer user_data)
2328 #ifdef HAVE_PLUGINS
2329 plugin_show_configure(NULL);
2330 #endif
2334 static void set_indent_width(guint width)
2336 GeanyDocument *doc;
2338 if (ignore_callback)
2339 return;
2341 doc = document_get_current();
2342 g_return_if_fail(doc != NULL);
2344 editor_set_indent(doc->editor, doc->editor->indent_type, width);
2348 void
2349 on_indent_width_activate (GtkMenuItem *menuitem,
2350 gpointer user_data)
2352 gchar *label = ui_menu_item_get_text(menuitem);
2353 gint width = atoi(label);
2355 g_free(label);
2356 if (width)
2357 set_indent_width(width);
2361 void
2362 on_reset_indentation1_activate (GtkMenuItem *menuitem,
2363 gpointer user_data)
2365 guint i;
2367 foreach_document(i)
2368 document_apply_indent_settings(documents[i]);
2370 ui_update_statusbar(NULL, -1);
2371 ui_document_show_hide(NULL);
2375 void
2376 on_mark_all1_activate (GtkMenuItem *menuitem,
2377 gpointer user_data)
2379 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);