Save encoding in session as text
[geany-mirror.git] / src / callbacks.c
blob20c466a8c93b941e920ffcf812c76743881f99b6
1 /*
2 * callbacks.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Callbacks used by Glade. These are mainly in response to menu item and button events in the
24 * main window. Callbacks not used by Glade should go elsewhere.
27 #include "geany.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <glib/gstdio.h>
34 #include <time.h>
36 #include "callbacks.h"
37 #include "support.h"
39 #include "keyfile.h"
40 #include "document.h"
41 #include "documentprivate.h"
42 #include "filetypes.h"
43 #include "sciwrappers.h"
44 #include "editor.h"
45 #include "ui_utils.h"
46 #include "utils.h"
47 #include "dialogs.h"
48 #include "about.h"
49 #include "msgwindow.h"
50 #include "build.h"
51 #include "prefs.h"
52 #include "templates.h"
53 #include "sidebar.h"
54 #include "keybindings.h"
55 #include "encodings.h"
56 #include "search.h"
57 #include "main.h"
58 #include "symbols.h"
59 #include "tools.h"
60 #include "project.h"
61 #include "navqueue.h"
62 #include "printing.h"
63 #include "plugins.h"
64 #include "log.h"
65 #include "toolbar.h"
66 #include "highlighting.h"
67 #include "pluginutils.h"
70 #ifdef HAVE_VTE
71 # include "vte.h"
72 #endif
74 #ifdef HAVE_SOCKET
75 # include "socket.h"
76 #endif
80 /* flag to indicate that an insert callback was triggered from the file menu,
81 * so we need to store the current cursor position in editor_info.click_pos. */
82 static gboolean insert_callback_from_menu = FALSE;
84 /* represents the state at switching a notebook page(in the left treeviews widget), to not emit
85 * the selection-changed signal from tv.tree_openfiles */
86 /*static gboolean switch_tv_notebook_page = FALSE; */
89 static gboolean check_no_unsaved(void)
91 guint i;
93 for (i = 0; i < documents_array->len; i++)
95 if (documents[i]->is_valid && documents[i]->changed)
97 return FALSE;
100 return TRUE; /* no unsaved edits */
104 /* set editor_info.click_pos to the current cursor position if insert_callback_from_menu is TRUE
105 * to prevent invalid cursor positions which can cause segfaults */
106 static void verify_click_pos(GeanyDocument *doc)
108 if (insert_callback_from_menu)
110 editor_info.click_pos = sci_get_current_position(doc->editor->sci);
111 insert_callback_from_menu = FALSE;
116 /* should only be called from on_exit_clicked */
117 static void quit_app(void)
119 configuration_save();
121 if (app->project != NULL)
122 project_close(FALSE); /* save project session files */
124 document_close_all();
126 main_status.quitting = TRUE;
128 main_quit();
132 /* wrapper function to abort exit process if cancel button is pressed */
133 G_MODULE_EXPORT gboolean on_exit_clicked(GtkWidget *widget, gpointer gdata)
135 main_status.quitting = TRUE;
137 if (! check_no_unsaved())
139 if (document_account_for_unsaved())
141 quit_app();
142 return FALSE;
145 else
146 if (! prefs.confirm_exit ||
147 dialogs_show_question_full(NULL, GTK_STOCK_QUIT, GTK_STOCK_CANCEL, NULL,
148 _("Do you really want to quit?")))
150 quit_app();
151 return FALSE;
154 main_status.quitting = FALSE;
155 return TRUE;
160 * GUI callbacks
163 G_MODULE_EXPORT void on_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
165 document_new_file(NULL, NULL, NULL);
169 G_MODULE_EXPORT void on_save1_activate(GtkMenuItem *menuitem, gpointer user_data)
171 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
172 GeanyDocument *doc = document_get_current();
174 if (doc != NULL && cur_page >= 0)
176 document_save_file(doc, FALSE);
181 G_MODULE_EXPORT void on_save_as1_activate(GtkMenuItem *menuitem, gpointer user_data)
183 dialogs_show_save_as();
187 G_MODULE_EXPORT void on_save_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
189 guint i, max = (guint) gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
190 GeanyDocument *doc, *cur_doc = document_get_current();
191 guint count = 0;
193 /* iterate over documents in tabs order */
194 for (i = 0; i < max; i++)
196 doc = document_get_from_page(i);
197 if (! doc->changed)
198 continue;
200 if (document_save_file(doc, FALSE))
201 count++;
203 if (!count)
204 return;
206 ui_set_statusbar(FALSE, ngettext("%d file saved.", "%d files saved.", count), count);
207 /* saving may have changed window title, sidebar for another doc, so update */
208 sidebar_update_tag_list(cur_doc, TRUE);
209 ui_set_window_title(cur_doc);
213 G_MODULE_EXPORT void on_close_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
215 document_close_all();
219 G_MODULE_EXPORT void on_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
221 GeanyDocument *doc = document_get_current();
223 g_return_if_fail(doc != NULL);
225 document_close(doc);
229 G_MODULE_EXPORT void on_quit1_activate(GtkMenuItem *menuitem, gpointer user_data)
231 on_exit_clicked(NULL, NULL);
235 G_MODULE_EXPORT void on_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
237 gtk_widget_set_sensitive(ui_widgets.recent_files_menuitem,
238 g_queue_get_length(ui_prefs.recent_queue) > 0);
239 /* hide Page setup when GTK printing is not used */
240 ui_widget_show_hide(ui_widgets.print_page_setup, printing_prefs.use_gtk_printing);
244 /* edit actions, c&p & co, from menu bar and from popup menu */
245 G_MODULE_EXPORT void on_edit1_activate(GtkMenuItem *menuitem, gpointer user_data)
247 GtkWidget *item;
248 GeanyDocument *doc = document_get_current();
250 ui_update_menu_copy_items(doc);
251 ui_update_insert_include_item(doc, 1);
253 item = ui_lookup_widget(main_widgets.window, "plugin_preferences1");
254 #ifndef HAVE_PLUGINS
255 gtk_widget_hide(item);
256 #else
257 gtk_widget_set_sensitive(item, plugins_have_preferences());
258 #endif
262 G_MODULE_EXPORT void on_undo1_activate(GtkMenuItem *menuitem, gpointer user_data)
264 GeanyDocument *doc = document_get_current();
266 g_return_if_fail(doc != NULL);
268 if (document_can_undo(doc))
270 sci_cancel(doc->editor->sci);
271 document_undo(doc);
276 G_MODULE_EXPORT void on_redo1_activate(GtkMenuItem *menuitem, gpointer user_data)
278 GeanyDocument *doc = document_get_current();
280 g_return_if_fail(doc != NULL);
282 if (document_can_redo(doc))
284 sci_cancel(doc->editor->sci);
285 document_redo(doc);
290 G_MODULE_EXPORT void on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
292 GeanyDocument *doc = document_get_current();
293 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
295 if (GTK_IS_EDITABLE(focusw))
296 gtk_editable_cut_clipboard(GTK_EDITABLE(focusw));
297 else
298 if (IS_SCINTILLA(focusw) && doc != NULL)
299 sci_cut(doc->editor->sci);
300 else
301 if (GTK_IS_TEXT_VIEW(focusw))
303 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
304 GTK_TEXT_VIEW(focusw));
305 gtk_text_buffer_cut_clipboard(buffer, gtk_clipboard_get(GDK_NONE), TRUE);
310 G_MODULE_EXPORT void on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
312 GeanyDocument *doc = document_get_current();
313 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
315 if (GTK_IS_EDITABLE(focusw))
316 gtk_editable_copy_clipboard(GTK_EDITABLE(focusw));
317 else
318 if (IS_SCINTILLA(focusw) && doc != NULL)
319 sci_copy(doc->editor->sci);
320 else
321 if (GTK_IS_TEXT_VIEW(focusw))
323 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
324 GTK_TEXT_VIEW(focusw));
325 gtk_text_buffer_copy_clipboard(buffer, gtk_clipboard_get(GDK_NONE));
330 G_MODULE_EXPORT void on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
332 GeanyDocument *doc = document_get_current();
333 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
335 if (GTK_IS_EDITABLE(focusw))
336 gtk_editable_paste_clipboard(GTK_EDITABLE(focusw));
337 else
338 if (IS_SCINTILLA(focusw) && doc != NULL)
340 sci_paste(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_paste_clipboard(buffer, gtk_clipboard_get(GDK_NONE), NULL,
348 TRUE);
353 G_MODULE_EXPORT void on_delete1_activate(GtkMenuItem *menuitem, gpointer user_data)
355 GeanyDocument *doc = document_get_current();
356 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
358 if (GTK_IS_EDITABLE(focusw))
359 gtk_editable_delete_selection(GTK_EDITABLE(focusw));
360 else
361 if (IS_SCINTILLA(focusw) && doc != NULL && sci_has_selection(doc->editor->sci))
362 sci_clear(doc->editor->sci);
363 else
364 if (GTK_IS_TEXT_VIEW(focusw))
366 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
367 GTK_TEXT_VIEW(focusw));
368 gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
373 G_MODULE_EXPORT void on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
375 prefs_show_dialog();
379 /* about menu item */
380 G_MODULE_EXPORT void on_info1_activate(GtkMenuItem *menuitem, gpointer user_data)
382 about_dialog_show();
386 /* open file */
387 G_MODULE_EXPORT void on_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
389 dialogs_show_open_file();
393 /* quit toolbar button */
394 G_MODULE_EXPORT void on_toolbutton_quit_clicked(GtkAction *action, gpointer user_data)
396 on_exit_clicked(NULL, NULL);
400 /* reload file */
401 G_MODULE_EXPORT void on_toolbutton_reload_clicked(GtkAction *action, gpointer user_data)
403 on_reload_as_activate(NULL, GINT_TO_POINTER(-1));
407 /* also used for reloading when user_data is -1 */
408 G_MODULE_EXPORT void on_reload_as_activate(GtkMenuItem *menuitem, gpointer user_data)
410 GeanyDocument *doc = document_get_current();
411 gchar *base_name;
412 gint i = GPOINTER_TO_INT(user_data);
413 const gchar *charset = NULL;
415 g_return_if_fail(doc != NULL);
417 /* No need to reload "untitled" (non-file-backed) documents */
418 if (doc->file_name == NULL)
419 return;
421 if (i >= 0)
423 if (i >= GEANY_ENCODINGS_MAX || encodings[i].charset == NULL)
424 return;
425 charset = encodings[i].charset;
427 else
428 charset = doc->encoding;
430 base_name = g_path_get_basename(doc->file_name);
431 /* don't prompt if file hasn't been edited at all */
432 if ((!doc->changed && !document_can_undo(doc) && !document_can_redo(doc)) ||
433 dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
434 _("Any unsaved changes will be lost."),
435 _("Are you sure you want to reload '%s'?"), base_name))
437 document_reload_file(doc, charset);
438 if (charset != NULL)
439 ui_update_statusbar(doc, -1);
441 g_free(base_name);
445 G_MODULE_EXPORT void on_change_font1_activate(GtkMenuItem *menuitem, gpointer user_data)
447 dialogs_show_open_font();
451 /* new file */
452 G_MODULE_EXPORT void on_toolbutton_new_clicked(GtkAction *action, gpointer user_data)
454 document_new_file(NULL, NULL, NULL);
458 /* open file */
459 G_MODULE_EXPORT void on_toolbutton_open_clicked(GtkAction *action, gpointer user_data)
461 dialogs_show_open_file();
465 /* save file */
466 G_MODULE_EXPORT void on_toolbutton_save_clicked(GtkAction *action, gpointer user_data)
468 on_save1_activate(NULL, user_data);
472 /* store text, clear search flags so we can use Search->Find Next/Previous */
473 static void setup_find(const gchar *text, gboolean backwards)
475 SETPTR(search_data.text, g_strdup(text));
476 SETPTR(search_data.original_text, g_strdup(text));
477 search_data.flags = 0;
478 search_data.backwards = backwards;
479 search_data.search_bar = TRUE;
483 static void do_toolbar_search(const gchar *text, gboolean incremental, gboolean backwards)
485 GeanyDocument *doc = document_get_current();
486 gboolean result;
488 setup_find(text, backwards);
489 result = document_search_bar_find(doc, search_data.text, 0, incremental, backwards);
490 if (search_data.search_bar)
491 ui_set_search_entry_background(toolbar_get_widget_child_by_name("SearchEntry"), result);
495 /* search text */
496 G_MODULE_EXPORT void on_toolbar_search_entry_changed(GtkAction *action, const gchar *text, gpointer user_data)
498 do_toolbar_search(text, TRUE, FALSE);
502 /* search text */
503 G_MODULE_EXPORT void on_toolbar_search_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
505 do_toolbar_search(text, FALSE, GPOINTER_TO_INT(user_data));
509 /* search text */
510 G_MODULE_EXPORT void on_toolbutton_search_clicked(GtkAction *action, gpointer user_data)
512 GeanyDocument *doc = document_get_current();
513 gboolean result;
514 GtkWidget *entry = toolbar_get_widget_child_by_name("SearchEntry");
516 if (entry != NULL)
518 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
520 setup_find(text, FALSE);
521 result = document_search_bar_find(doc, search_data.text, 0, FALSE, FALSE);
522 if (search_data.search_bar)
523 ui_set_search_entry_background(entry, result);
525 else
526 on_find1_activate(NULL, NULL);
530 /* hides toolbar from toolbar popup menu */
531 G_MODULE_EXPORT void on_hide_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
533 GtkWidget *tool_item = ui_lookup_widget(GTK_WIDGET(main_widgets.window), "menu_show_toolbar1");
534 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tool_item), FALSE);
538 /* zoom in from menu bar and popup menu */
539 G_MODULE_EXPORT void on_zoom_in1_activate(GtkMenuItem *menuitem, gpointer user_data)
541 GeanyDocument *doc = document_get_current();
542 static gint done = 1;
544 g_return_if_fail(doc != NULL);
546 if (done++ % 3 == 0)
547 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin,
548 (sci_get_zoom(doc->editor->sci) / 2));
549 sci_zoom_in(doc->editor->sci);
553 /* zoom out from menu bar and popup menu */
554 G_MODULE_EXPORT void on_zoom_out1_activate(GtkMenuItem *menuitem, gpointer user_data)
556 GeanyDocument *doc = document_get_current();
558 g_return_if_fail(doc != NULL);
560 if (sci_get_zoom(doc->editor->sci) == 0)
561 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
562 sci_zoom_out(doc->editor->sci);
566 G_MODULE_EXPORT void on_normal_size1_activate(GtkMenuItem *menuitem, gpointer user_data)
568 GeanyDocument *doc = document_get_current();
570 g_return_if_fail(doc != NULL);
572 sci_zoom_off(doc->editor->sci);
573 sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
577 /* close tab */
578 G_MODULE_EXPORT void on_toolbutton_close_clicked(GtkAction *action, gpointer user_data)
580 on_close1_activate(NULL, NULL);
584 G_MODULE_EXPORT void on_toolbutton_close_all_clicked(GtkAction *action, gpointer user_data)
586 on_close_all1_activate(NULL, NULL);
590 G_MODULE_EXPORT void on_toolbutton_preferences_clicked(GtkAction *action, gpointer user_data)
592 on_preferences1_activate(NULL, NULL);
596 static gboolean delayed_check_disk_status(gpointer data)
598 document_check_disk_status(data, FALSE);
599 return FALSE;
603 /* Changes window-title after switching tabs and lots of other things.
604 * note: using 'after' makes Scintilla redraw before the UI, appearing more responsive */
605 G_MODULE_EXPORT void on_notebook1_switch_page_after(GtkNotebook *notebook, GtkNotebookPage *page,
606 guint page_num, gpointer user_data)
608 GeanyDocument *doc;
610 if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
611 return;
613 if (page_num == (guint) -1 && page != NULL)
614 doc = document_find_by_sci(SCINTILLA(page));
615 else
616 doc = document_get_from_page(page_num);
618 if (doc != NULL)
620 sidebar_select_openfiles_item(doc);
621 ui_save_buttons_toggle(doc->changed);
622 ui_set_window_title(doc);
623 ui_update_statusbar(doc, -1);
624 ui_update_popup_reundo_items(doc);
625 ui_document_show_hide(doc); /* update the document menu */
626 build_menu_update(doc);
627 sidebar_update_tag_list(doc, FALSE);
628 document_highlight_tags(doc);
630 /* We delay the check to avoid weird fast, unintended switching of notebook pages when
631 * the 'file has changed' dialog is shown while the switch event is not yet completely
632 * finished. So, we check after the switch has been performed to be safe. */
633 g_idle_add(delayed_check_disk_status, doc);
635 #ifdef HAVE_VTE
636 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
637 #endif
639 g_signal_emit_by_name(geany_object, "document-activate", doc);
644 G_MODULE_EXPORT void on_tv_notebook_switch_page(GtkNotebook *notebook, GtkNotebookPage *page,
645 guint page_num, gpointer user_data)
647 /* suppress selection changed signal when switching to the open files list */
648 ignore_callback = TRUE;
652 G_MODULE_EXPORT void on_tv_notebook_switch_page_after(GtkNotebook *notebook, GtkNotebookPage *page,
653 guint page_num, gpointer user_data)
655 ignore_callback = FALSE;
659 static void convert_eol(gint mode)
661 GeanyDocument *doc = document_get_current();
663 g_return_if_fail(doc != NULL);
665 sci_convert_eols(doc->editor->sci, mode);
666 sci_set_eol_mode(doc->editor->sci, mode);
667 ui_update_statusbar(doc, -1);
671 G_MODULE_EXPORT void on_crlf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
673 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
674 return;
676 convert_eol(SC_EOL_CRLF);
680 G_MODULE_EXPORT void on_lf_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
682 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
683 return;
685 convert_eol(SC_EOL_LF);
689 G_MODULE_EXPORT void on_cr_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
691 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
692 return;
694 convert_eol(SC_EOL_CR);
698 G_MODULE_EXPORT void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
700 GeanyDocument *doc = document_get_current();
702 g_return_if_fail(doc != NULL);
704 editor_replace_tabs(doc->editor);
708 gboolean toolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
710 if (event->button == 3)
712 gtk_menu_popup(GTK_MENU(ui_widgets.toolbar_menu), NULL, NULL, NULL, NULL, event->button, event->time);
713 return TRUE;
715 return FALSE;
719 G_MODULE_EXPORT void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data)
721 GeanyDocument *doc = document_get_current();
722 ScintillaObject *sci;
723 gchar *text;
724 gboolean keep_sel = TRUE;
726 g_return_if_fail(doc != NULL);
728 sci = doc->editor->sci;
729 if (! sci_has_selection(sci))
731 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD);
732 keep_sel = FALSE;
735 /* either we already had a selection or we created one for current word */
736 if (sci_has_selection(sci))
738 gchar *result = NULL;
739 gint cmd = SCI_LOWERCASE;
740 gint text_len = sci_get_selected_text_length(sci);
741 gboolean rectsel = (gboolean) scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0);
743 text = g_malloc(text_len + 1);
744 sci_get_selected_text(sci, text);
746 if (utils_str_has_upper(text))
748 if (rectsel)
749 cmd = SCI_LOWERCASE;
750 else
751 result = g_utf8_strdown(text, -1);
754 else
756 if (rectsel)
757 cmd = SCI_UPPERCASE;
758 else
759 result = g_utf8_strup(text, -1);
763 if (result != NULL)
765 sci_replace_sel(sci, result);
766 g_free(result);
767 if (keep_sel)
768 sci_set_selection_start(sci, sci_get_current_position(sci) - text_len + 1);
770 else
771 sci_send_command(sci, cmd);
773 g_free(text);
779 G_MODULE_EXPORT void on_show_toolbar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
781 if (ignore_callback) return;
783 toolbar_prefs.visible = (toolbar_prefs.visible) ? FALSE : TRUE;;
784 ui_widget_show_hide(GTK_WIDGET(main_widgets.toolbar), toolbar_prefs.visible);
788 G_MODULE_EXPORT void on_fullscreen1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
790 if (ignore_callback)
791 return;
793 ui_prefs.fullscreen = (ui_prefs.fullscreen) ? FALSE : TRUE;
794 ui_set_fullscreen();
798 G_MODULE_EXPORT void on_show_messages_window1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
800 if (ignore_callback)
801 return;
803 ui_prefs.msgwindow_visible = (ui_prefs.msgwindow_visible) ? FALSE : TRUE;
804 msgwin_show_hide(ui_prefs.msgwindow_visible);
808 G_MODULE_EXPORT void on_menu_color_schemes_activate(GtkImageMenuItem *imagemenuitem, gpointer user_data)
810 highlighting_show_color_scheme_dialog();
814 G_MODULE_EXPORT void on_markers_margin1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
816 if (ignore_callback)
817 return;
819 editor_prefs.show_markers_margin = ! editor_prefs.show_markers_margin;
820 ui_toggle_editor_features(GEANY_EDITOR_SHOW_MARKERS_MARGIN);
824 G_MODULE_EXPORT void on_show_line_numbers1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
826 if (ignore_callback)
827 return;
829 editor_prefs.show_linenumber_margin = ! editor_prefs.show_linenumber_margin;
830 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_NUMBERS);
834 G_MODULE_EXPORT void on_menu_show_white_space1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
836 if (ignore_callback)
837 return;
839 editor_prefs.show_white_space = ! editor_prefs.show_white_space;
840 ui_toggle_editor_features(GEANY_EDITOR_SHOW_WHITE_SPACE);
844 G_MODULE_EXPORT void on_menu_show_line_endings1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
846 if (ignore_callback)
847 return;
849 editor_prefs.show_line_endings = ! editor_prefs.show_line_endings;
850 ui_toggle_editor_features(GEANY_EDITOR_SHOW_LINE_ENDINGS);
854 G_MODULE_EXPORT void on_menu_show_indentation_guides1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
856 if (ignore_callback)
857 return;
859 editor_prefs.show_indent_guide = ! editor_prefs.show_indent_guide;
860 ui_toggle_editor_features(GEANY_EDITOR_SHOW_INDENTATION_GUIDES);
864 G_MODULE_EXPORT void on_line_wrapping1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
866 if (! ignore_callback)
868 GeanyDocument *doc = document_get_current();
869 g_return_if_fail(doc != NULL);
871 editor_set_line_wrapping(doc->editor, ! doc->editor->line_wrapping);
876 G_MODULE_EXPORT void on_set_file_readonly1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
878 if (! ignore_callback)
880 GeanyDocument *doc = document_get_current();
881 g_return_if_fail(doc != NULL);
883 doc->readonly = ! doc->readonly;
884 sci_set_readonly(doc->editor->sci, doc->readonly);
885 ui_update_tab_status(doc);
886 ui_update_statusbar(doc, -1);
891 G_MODULE_EXPORT void on_use_auto_indentation1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
893 if (! ignore_callback)
895 GeanyDocument *doc = document_get_current();
896 g_return_if_fail(doc != NULL);
898 doc->editor->auto_indent = ! doc->editor->auto_indent;
903 static void find_usage(gboolean in_session)
905 gint flags;
906 gchar *search_text;
907 GeanyDocument *doc = document_get_current();
909 g_return_if_fail(doc != NULL);
911 if (sci_has_selection(doc->editor->sci))
912 { /* take selected text if there is a selection */
913 search_text = g_malloc(sci_get_selected_text_length(doc->editor->sci) + 1);
914 sci_get_selected_text(doc->editor->sci, search_text);
915 flags = SCFIND_MATCHCASE;
917 else
919 editor_find_current_word_sciwc(doc->editor, -1,
920 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
921 search_text = g_strdup(editor_info.current_word);
922 flags = SCFIND_MATCHCASE | SCFIND_WHOLEWORD;
925 search_find_usage(search_text, search_text, flags, in_session);
926 g_free(search_text);
930 G_MODULE_EXPORT void on_find_document_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
932 find_usage(FALSE);
936 G_MODULE_EXPORT void on_find_usage1_activate(GtkMenuItem *menuitem, gpointer user_data)
938 find_usage(TRUE);
942 static void goto_tag(gboolean definition)
944 GeanyDocument *doc = document_get_current();
946 g_return_if_fail(doc != NULL);
948 /* update cursor pos for navigating back afterwards */
949 if (!sci_has_selection(doc->editor->sci))
950 sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);
952 /* use the keybinding callback as it checks for selections as well as current word */
953 if (definition)
954 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
955 else
956 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
960 G_MODULE_EXPORT void on_goto_tag_definition1(GtkMenuItem *menuitem, gpointer user_data)
962 goto_tag(TRUE);
966 G_MODULE_EXPORT void on_goto_tag_declaration1(GtkMenuItem *menuitem, gpointer user_data)
968 goto_tag(FALSE);
972 G_MODULE_EXPORT void on_count_words1_activate(GtkMenuItem *menuitem, gpointer user_data)
974 tools_word_count();
978 G_MODULE_EXPORT void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data)
980 gchar colour[9];
981 GeanyDocument *doc = document_get_current();
982 gint pos;
984 g_return_if_fail(doc != NULL);
986 pos = sci_get_current_position(doc->editor->sci);
987 editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#");
988 tools_color_chooser(colour);
992 G_MODULE_EXPORT void on_toolbutton_compile_clicked(GtkAction *action, gpointer user_data)
994 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_COMPILE);
998 G_MODULE_EXPORT void on_find1_activate(GtkMenuItem *menuitem, gpointer user_data)
1000 search_show_find_dialog();
1004 G_MODULE_EXPORT void on_find_next1_activate(GtkMenuItem *menuitem, gpointer user_data)
1006 search_find_again(FALSE);
1010 G_MODULE_EXPORT void on_find_previous1_activate(GtkMenuItem *menuitem, gpointer user_data)
1012 if (search_data.flags & SCFIND_REGEXP)
1013 /* Can't reverse search order for a regex (find next ignores search backwards) */
1014 utils_beep();
1015 else
1016 search_find_again(TRUE);
1020 G_MODULE_EXPORT void on_find_nextsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
1022 search_find_selection(document_get_current(), FALSE);
1026 G_MODULE_EXPORT void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data)
1028 search_find_selection(document_get_current(), TRUE);
1032 G_MODULE_EXPORT void on_replace1_activate(GtkMenuItem *menuitem, gpointer user_data)
1034 search_show_replace_dialog();
1038 G_MODULE_EXPORT void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
1040 search_show_find_in_files_dialog(NULL);
1044 static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
1046 if (*text == '+' || *text == '-')
1048 *line_no = atoi(text + 1);
1049 *offset = (*text == '+') ? 1 : -1;
1051 else
1053 *line_no = atoi(text) - 1;
1054 *offset = 0;
1059 G_MODULE_EXPORT void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
1061 static gchar value[16] = "";
1062 gchar *result;
1064 result = dialogs_show_input_goto_line(
1065 _("Go to Line"), GTK_WINDOW(main_widgets.window),
1066 _("Enter the line you want to go to:"), value);
1067 if (result != NULL)
1069 GeanyDocument *doc = document_get_current();
1070 gint offset;
1071 gint line_no;
1073 g_return_if_fail(doc != NULL);
1075 get_line_and_offset_from_text(result, &line_no, &offset);
1076 if (! editor_goto_line(doc->editor, line_no, offset))
1077 utils_beep();
1078 /* remember value for future calls */
1079 g_snprintf(value, sizeof(value), "%s", result);
1081 g_free(result);
1086 G_MODULE_EXPORT void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
1088 GeanyDocument *doc = document_get_current();
1089 gint offset;
1090 gint line_no;
1092 g_return_if_fail(doc != NULL);
1094 get_line_and_offset_from_text(text, &line_no, &offset);
1095 if (! editor_goto_line(doc->editor, line_no, offset))
1096 utils_beep();
1097 else
1098 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1102 G_MODULE_EXPORT void on_toolbutton_goto_clicked(GtkAction *action, gpointer user_data)
1104 GtkWidget *entry = toolbar_get_widget_child_by_name("GotoEntry");
1106 if (entry != NULL)
1108 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
1110 on_toolbutton_goto_entry_activate(NULL, text, NULL);
1112 else
1113 on_go_to_line_activate(NULL, NULL);
1117 G_MODULE_EXPORT void on_help1_activate(GtkMenuItem *menuitem, gpointer user_data)
1119 gchar *uri;
1121 uri = utils_get_help_url(NULL);
1122 utils_open_browser(uri);
1123 g_free(uri);
1127 G_MODULE_EXPORT void on_help_shortcuts1_activate(GtkMenuItem *menuitem, gpointer user_data)
1129 keybindings_show_shortcuts();
1133 G_MODULE_EXPORT void on_website1_activate(GtkMenuItem *menuitem, gpointer user_data)
1135 utils_open_browser(GEANY_HOMEPAGE);
1139 G_MODULE_EXPORT void on_help_menu_item_donate_activate(GtkMenuItem *item, gpointer user_data)
1141 utils_open_browser(GEANY_DONATE);
1145 G_MODULE_EXPORT void on_help_menu_item_wiki_activate(GtkMenuItem *item, gpointer user_data)
1147 utils_open_browser(GEANY_WIKI);
1151 G_MODULE_EXPORT void on_help_menu_item_bug_report_activate(GtkMenuItem *item, gpointer user_data)
1153 utils_open_browser(GEANY_BUG_REPORT);
1157 G_MODULE_EXPORT void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data)
1159 GeanyDocument *doc = document_get_current();
1160 gchar *text;
1161 const gchar *cur_tag = NULL;
1162 gint line = -1, pos = 0;
1164 if (doc == NULL || doc->file_type == NULL)
1166 ui_set_statusbar(FALSE,
1167 _("Please set the filetype for the current file before using this function."));
1168 return;
1171 /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line
1172 * returns the current position, so it should be safe */
1173 line = symbols_get_current_function(doc, &cur_tag);
1174 pos = sci_get_position_from_line(doc->editor->sci, line - 1);
1176 text = templates_get_template_function(doc, cur_tag);
1178 sci_start_undo_action(doc->editor->sci);
1179 sci_insert_text(doc->editor->sci, pos, text);
1180 sci_end_undo_action(doc->editor->sci);
1181 g_free(text);
1185 G_MODULE_EXPORT void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1187 GeanyDocument *doc = document_get_current();
1189 if (doc == NULL || doc->file_type == NULL)
1191 ui_set_statusbar(FALSE,
1192 _("Please set the filetype for the current file before using this function."));
1193 return;
1196 verify_click_pos(doc); /* make sure that the click_pos is valid */
1198 if (doc->file_type->comment_open || doc->file_type->comment_single)
1199 editor_insert_multiline_comment(doc->editor);
1200 else
1201 utils_beep();
1205 G_MODULE_EXPORT void on_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1207 GeanyDocument *doc = document_get_current();
1208 gchar *text;
1210 g_return_if_fail(doc != NULL);
1212 text = templates_get_template_licence(doc, GEANY_TEMPLATE_GPL);
1214 verify_click_pos(doc); /* make sure that the click_pos is valid */
1216 sci_start_undo_action(doc->editor->sci);
1217 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1218 sci_end_undo_action(doc->editor->sci);
1219 g_free(text);
1223 G_MODULE_EXPORT void on_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1225 GeanyDocument *doc = document_get_current();
1226 gchar *text;
1228 g_return_if_fail(doc != NULL);
1230 text = templates_get_template_licence(doc, GEANY_TEMPLATE_BSD);
1232 verify_click_pos(doc); /* make sure that the click_pos is valid */
1234 sci_start_undo_action(doc->editor->sci);
1235 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1236 sci_end_undo_action(doc->editor->sci);
1237 g_free(text);
1242 G_MODULE_EXPORT void on_comments_changelog_activate(GtkMenuItem *menuitem, gpointer user_data)
1244 GeanyDocument *doc = document_get_current();
1245 gchar *text;
1247 g_return_if_fail(doc != NULL);
1249 text = templates_get_template_changelog(doc);
1250 sci_start_undo_action(doc->editor->sci);
1251 sci_insert_text(doc->editor->sci, 0, text);
1252 /* sets the cursor to the right position to type the changelog text,
1253 * the template has 21 chars + length of name and email */
1254 sci_goto_pos(doc->editor->sci, 21 + strlen(template_prefs.developer) + strlen(template_prefs.mail), TRUE);
1255 sci_end_undo_action(doc->editor->sci);
1257 g_free(text);
1261 G_MODULE_EXPORT void on_comments_fileheader_activate(GtkMenuItem *menuitem, gpointer user_data)
1263 GeanyDocument *doc = document_get_current();
1264 gchar *text;
1265 const gchar *fname;
1266 GeanyFiletype *ft;
1268 g_return_if_fail(doc != NULL);
1270 ft = doc->file_type;
1271 fname = doc->file_name;
1272 text = templates_get_template_fileheader(FILETYPE_ID(ft), fname);
1274 sci_start_undo_action(doc->editor->sci);
1275 sci_insert_text(doc->editor->sci, 0, text);
1276 sci_goto_pos(doc->editor->sci, 0, FALSE);
1277 sci_end_undo_action(doc->editor->sci);
1278 g_free(text);
1282 G_MODULE_EXPORT void on_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
1284 GeanyDocument *doc = document_get_current();
1285 const gchar *format = NULL;
1286 gchar *time_str;
1288 g_return_if_fail(doc != NULL);
1290 /* set default value */
1291 if (utils_str_equal("", ui_prefs.custom_date_format))
1293 g_free(ui_prefs.custom_date_format);
1294 ui_prefs.custom_date_format = g_strdup("%d.%m.%Y");
1297 if (utils_str_equal(_("dd.mm.yyyy"), (gchar*) user_data))
1298 format = "%d.%m.%Y";
1299 else if (utils_str_equal(_("mm.dd.yyyy"), (gchar*) user_data))
1300 format = "%m.%d.%Y";
1301 else if (utils_str_equal(_("yyyy/mm/dd"), (gchar*) user_data))
1302 format = "%Y/%m/%d";
1303 else if (utils_str_equal(_("dd.mm.yyyy hh:mm:ss"), (gchar*) user_data))
1304 format = "%d.%m.%Y %H:%M:%S";
1305 else if (utils_str_equal(_("mm.dd.yyyy hh:mm:ss"), (gchar*) user_data))
1306 format = "%m.%d.%Y %H:%M:%S";
1307 else if (utils_str_equal(_("yyyy/mm/dd hh:mm:ss"), (gchar*) user_data))
1308 format = "%Y/%m/%d %H:%M:%S";
1309 else if (utils_str_equal(_("_Use Custom Date Format"), (gchar*) user_data))
1310 format = ui_prefs.custom_date_format;
1311 else
1313 gchar *str = dialogs_show_input(_("Custom Date Format"), GTK_WINDOW(main_widgets.window),
1314 _("Enter here a custom date and time format. "
1315 "You can use any conversion specifiers which can be used with the ANSI C strftime function."),
1316 ui_prefs.custom_date_format);
1317 if (str)
1318 SETPTR(ui_prefs.custom_date_format, str);
1319 return;
1322 time_str = utils_get_date_time(format, NULL);
1323 if (time_str != NULL)
1325 verify_click_pos(doc); /* make sure that the click_pos is valid */
1327 sci_start_undo_action(doc->editor->sci);
1328 sci_insert_text(doc->editor->sci, editor_info.click_pos, time_str);
1329 sci_goto_pos(doc->editor->sci, editor_info.click_pos + strlen(time_str), FALSE);
1330 sci_end_undo_action(doc->editor->sci);
1331 g_free(time_str);
1333 else
1335 utils_beep();
1336 ui_set_statusbar(TRUE,
1337 _("Date format string could not be converted (possibly too long)."));
1342 G_MODULE_EXPORT void on_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
1344 GeanyDocument *doc = document_get_current();
1345 gint pos = -1;
1346 gchar *text;
1348 g_return_if_fail(doc != NULL);
1349 g_return_if_fail(user_data != NULL);
1351 verify_click_pos(doc); /* make sure that the click_pos is valid */
1353 if (utils_str_equal(user_data, "blank"))
1355 text = g_strdup("#include \"\"\n");
1356 pos = editor_info.click_pos + 10;
1358 else
1360 text = g_strconcat("#include <", user_data, ">\n", NULL);
1363 sci_start_undo_action(doc->editor->sci);
1364 sci_insert_text(doc->editor->sci, editor_info.click_pos, text);
1365 sci_end_undo_action(doc->editor->sci);
1366 g_free(text);
1367 if (pos >= 0)
1368 sci_goto_pos(doc->editor->sci, pos, FALSE);
1372 G_MODULE_EXPORT void on_file_properties_activate(GtkMenuItem *menuitem, gpointer user_data)
1374 GeanyDocument *doc = document_get_current();
1375 g_return_if_fail(doc != NULL);
1377 dialogs_show_file_properties(doc);
1381 G_MODULE_EXPORT void on_menu_fold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1383 GeanyDocument *doc = document_get_current();
1384 g_return_if_fail(doc != NULL);
1386 editor_fold_all(doc->editor);
1390 G_MODULE_EXPORT void on_menu_unfold_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1392 GeanyDocument *doc = document_get_current();
1393 g_return_if_fail(doc != NULL);
1395 editor_unfold_all(doc->editor);
1399 G_MODULE_EXPORT void on_toolbutton_run_clicked(GtkAction *action, gpointer user_data)
1401 keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_RUN);
1405 G_MODULE_EXPORT void on_menu_remove_indicators1_activate(GtkMenuItem *menuitem, gpointer user_data)
1407 GeanyDocument *doc = document_get_current();
1408 g_return_if_fail(doc != NULL);
1410 editor_indicator_clear(doc->editor, GEANY_INDICATOR_ERROR);
1414 G_MODULE_EXPORT void on_print1_activate(GtkMenuItem *menuitem, gpointer user_data)
1416 GeanyDocument *doc = document_get_current();
1417 g_return_if_fail(doc != NULL);
1419 printing_print_doc(doc);
1423 G_MODULE_EXPORT void on_menu_select_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
1425 GeanyDocument *doc = document_get_current();
1426 g_return_if_fail(doc != NULL);
1428 sci_select_all(doc->editor->sci);
1432 G_MODULE_EXPORT void on_menu_show_sidebar1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1434 if (ignore_callback)
1435 return;
1437 ui_prefs.sidebar_visible = ! ui_prefs.sidebar_visible;
1439 /* show built-in tabs if no tabs visible */
1440 if (ui_prefs.sidebar_visible &&
1441 ! interface_prefs.sidebar_openfiles_visible && ! interface_prefs.sidebar_symbol_visible &&
1442 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.sidebar_notebook)) <= 2)
1444 interface_prefs.sidebar_openfiles_visible = TRUE;
1445 interface_prefs.sidebar_symbol_visible = TRUE;
1448 /* if window has input focus, set it back to the editor before toggling off */
1449 if (! ui_prefs.sidebar_visible &&
1450 gtk_container_get_focus_child(GTK_CONTAINER(main_widgets.sidebar_notebook)) != NULL)
1452 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1455 ui_sidebar_show_hide();
1459 G_MODULE_EXPORT void on_menu_write_unicode_bom1_toggled(GtkCheckMenuItem *checkmenuitem, gpointer user_data)
1461 if (! ignore_callback)
1463 GeanyDocument *doc = document_get_current();
1465 g_return_if_fail(doc != NULL);
1466 if (doc->readonly)
1468 utils_beep();
1469 return;
1472 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1474 doc->has_bom = ! doc->has_bom;
1476 ui_update_statusbar(doc, -1);
1481 G_MODULE_EXPORT void on_menu_comment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1483 GeanyDocument *doc = document_get_current();
1484 g_return_if_fail(doc != NULL);
1486 editor_do_comment(doc->editor, -1, FALSE, FALSE, TRUE);
1490 G_MODULE_EXPORT void on_menu_uncomment_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
1492 GeanyDocument *doc = document_get_current();
1493 g_return_if_fail(doc != NULL);
1495 editor_do_uncomment(doc->editor, -1, FALSE);
1499 G_MODULE_EXPORT void on_menu_toggle_line_commentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
1501 GeanyDocument *doc = document_get_current();
1502 g_return_if_fail(doc != NULL);
1504 editor_do_comment_toggle(doc->editor);
1508 G_MODULE_EXPORT void on_menu_increase_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1510 GeanyDocument *doc = document_get_current();
1511 g_return_if_fail(doc != NULL);
1513 editor_indent(doc->editor, TRUE);
1517 G_MODULE_EXPORT void on_menu_decrease_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
1519 GeanyDocument *doc = document_get_current();
1520 g_return_if_fail(doc != NULL);
1522 editor_indent(doc->editor, FALSE);
1526 G_MODULE_EXPORT void on_next_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1528 if (! ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_msg),
1529 msgwin_goto_messages_file_line))
1530 ui_set_statusbar(FALSE, _("No more message items."));
1534 G_MODULE_EXPORT void on_previous_message1_activate(GtkMenuItem *menuitem, gpointer user_data)
1536 if (! ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_msg),
1537 msgwin_goto_messages_file_line))
1538 ui_set_statusbar(FALSE, _("No more message items."));
1542 G_MODULE_EXPORT void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data)
1544 insert_callback_from_menu = TRUE;
1545 on_comments_multiline_activate(menuitem, user_data);
1549 G_MODULE_EXPORT void on_menu_comments_gpl_activate(GtkMenuItem *menuitem, gpointer user_data)
1551 insert_callback_from_menu = TRUE;
1552 on_comments_gpl_activate(menuitem, user_data);
1556 G_MODULE_EXPORT void on_menu_comments_bsd_activate(GtkMenuItem *menuitem, gpointer user_data)
1558 insert_callback_from_menu = TRUE;
1559 on_comments_bsd_activate(menuitem, user_data);
1563 G_MODULE_EXPORT void on_menu_insert_include_activate(GtkMenuItem *menuitem, gpointer user_data)
1565 insert_callback_from_menu = TRUE;
1566 on_insert_include_activate(menuitem, user_data);
1570 G_MODULE_EXPORT void on_menu_insert_date_activate(GtkMenuItem *menuitem, gpointer user_data)
1572 insert_callback_from_menu = TRUE;
1573 on_insert_date_activate(menuitem, user_data);
1577 G_MODULE_EXPORT void on_project_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
1579 project_new();
1583 G_MODULE_EXPORT void on_project_open1_activate(GtkMenuItem *menuitem, gpointer user_data)
1585 project_open();
1589 G_MODULE_EXPORT void on_project_close1_activate(GtkMenuItem *menuitem, gpointer user_data)
1591 project_close(TRUE);
1595 G_MODULE_EXPORT void on_project_properties1_activate(GtkMenuItem *menuitem, gpointer user_data)
1597 project_properties();
1601 G_MODULE_EXPORT void on_menu_project1_activate(GtkMenuItem *menuitem, gpointer user_data)
1603 static GtkWidget *item_close = NULL;
1604 static GtkWidget *item_properties = NULL;
1606 if (item_close == NULL)
1608 item_close = ui_lookup_widget(main_widgets.window, "project_close1");
1609 item_properties = ui_lookup_widget(main_widgets.window, "project_properties1");
1612 gtk_widget_set_sensitive(item_close, (app->project != NULL));
1613 gtk_widget_set_sensitive(item_properties, (app->project != NULL));
1614 gtk_widget_set_sensitive(ui_widgets.recent_projects_menuitem,
1615 g_queue_get_length(ui_prefs.recent_projects_queue) > 0);
1619 G_MODULE_EXPORT void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_data)
1621 GeanyDocument *doc = document_get_current();
1622 gchar *sel = NULL;
1623 const gchar *wc;
1625 #ifdef G_OS_WIN32
1626 wc = GEANY_WORDCHARS "./-" "\\";
1627 #else
1628 wc = GEANY_WORDCHARS "./-";
1629 #endif
1631 g_return_if_fail(doc != NULL);
1633 sel = editor_get_default_selection(doc->editor, TRUE, wc);
1634 SETPTR(sel, utils_get_locale_from_utf8(sel));
1636 if (sel != NULL)
1638 gchar *filename = NULL;
1640 if (g_path_is_absolute(sel))
1641 filename = g_strdup(sel);
1642 else
1643 { /* relative filename, add the path of the current file */
1644 gchar *path;
1646 path = utils_get_current_file_dir_utf8();
1647 SETPTR(path, utils_get_locale_from_utf8(path));
1648 if (!path)
1649 path = g_get_current_dir();
1651 filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
1653 if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
1654 app->project != NULL && NZV(app->project->base_path))
1656 /* try the project's base path */
1657 SETPTR(path, project_get_base_path());
1658 SETPTR(path, utils_get_locale_from_utf8(path));
1659 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
1661 g_free(path);
1662 #ifdef G_OS_UNIX
1663 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1664 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));
1666 if (! g_file_test(filename, G_FILE_TEST_EXISTS))
1667 SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
1668 #endif
1671 if (g_file_test(filename, G_FILE_TEST_EXISTS))
1672 document_open_file(filename, FALSE, NULL, NULL);
1673 else
1675 SETPTR(sel, utils_get_utf8_from_locale(sel));
1676 ui_set_statusbar(TRUE, _("Could not open file %s (File not found)"), sel);
1679 g_free(filename);
1680 g_free(sel);
1685 G_MODULE_EXPORT void on_remove_markers1_activate(GtkMenuItem *menuitem, gpointer user_data)
1687 GeanyDocument *doc = document_get_current();
1688 g_return_if_fail(doc != NULL);
1690 sci_marker_delete_all(doc->editor->sci, 0); /* delete the yellow tag marker */
1691 sci_marker_delete_all(doc->editor->sci, 1); /* delete user markers */
1692 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1696 G_MODULE_EXPORT void on_load_tags1_activate(GtkMenuItem *menuitem, gpointer user_data)
1698 symbols_show_load_tags_dialog();
1702 G_MODULE_EXPORT void on_context_action1_activate(GtkMenuItem *menuitem, gpointer user_data)
1704 gchar *word, *command;
1705 GError *error = NULL;
1706 GeanyDocument *doc = document_get_current();
1708 g_return_if_fail(doc != NULL);
1710 if (sci_has_selection(doc->editor->sci))
1711 { /* take selected text if there is a selection */
1712 word = g_malloc(sci_get_selected_text_length(doc->editor->sci) + 1);
1713 sci_get_selected_text(doc->editor->sci, word);
1715 else
1717 word = g_strdup(editor_info.current_word);
1720 /* use the filetype specific command if available, fallback to global command otherwise */
1721 if (doc->file_type != NULL &&
1722 NZV(doc->file_type->context_action_cmd))
1724 command = g_strdup(doc->file_type->context_action_cmd);
1726 else
1728 command = g_strdup(tool_prefs.context_action_cmd);
1731 /* substitute the wildcard %s and run the command if it is non empty */
1732 if (G_LIKELY(NZV(command)))
1734 utils_str_replace_all(&command, "%s", word);
1736 if (! g_spawn_command_line_async(command, &error))
1738 ui_set_statusbar(TRUE, "Context action command failed: %s", error->message);
1739 g_error_free(error);
1742 g_free(word);
1743 g_free(command);
1747 G_MODULE_EXPORT void on_menu_toggle_all_additional_widgets1_activate(GtkMenuItem *menuitem, gpointer user_data)
1749 static gint hide_all = -1;
1750 GtkCheckMenuItem *msgw = GTK_CHECK_MENU_ITEM(
1751 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1752 GtkCheckMenuItem *toolbari = GTK_CHECK_MENU_ITEM(
1753 ui_lookup_widget(main_widgets.window, "menu_show_toolbar1"));
1755 /* get the initial state (necessary if Geany was closed with hide_all = TRUE) */
1756 if (G_UNLIKELY(hide_all == -1))
1758 if (! gtk_check_menu_item_get_active(msgw) &&
1759 ! interface_prefs.show_notebook_tabs &&
1760 ! gtk_check_menu_item_get_active(toolbari))
1762 hide_all = TRUE;
1764 else
1765 hide_all = FALSE;
1768 hide_all = ! hide_all; /* toggle */
1770 if (hide_all)
1772 if (gtk_check_menu_item_get_active(msgw))
1773 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1775 interface_prefs.show_notebook_tabs = FALSE;
1776 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1778 ui_statusbar_showhide(FALSE);
1780 if (gtk_check_menu_item_get_active(toolbari))
1781 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1783 else
1786 if (! gtk_check_menu_item_get_active(msgw))
1787 gtk_check_menu_item_set_active(msgw, ! gtk_check_menu_item_get_active(msgw));
1789 interface_prefs.show_notebook_tabs = TRUE;
1790 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
1792 ui_statusbar_showhide(TRUE);
1794 if (! gtk_check_menu_item_get_active(toolbari))
1795 gtk_check_menu_item_set_active(toolbari, ! gtk_check_menu_item_get_active(toolbari));
1800 G_MODULE_EXPORT void on_forward_activate(GtkMenuItem *menuitem, gpointer user_data)
1802 navqueue_go_forward();
1806 G_MODULE_EXPORT void on_back_activate(GtkMenuItem *menuitem, gpointer user_data)
1808 navqueue_go_back();
1812 G_MODULE_EXPORT gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1814 if (prefs.auto_focus && ! GTK_WIDGET_HAS_FOCUS(widget))
1815 gtk_widget_grab_focus(widget);
1817 return FALSE;
1821 static void set_indent_type(GtkCheckMenuItem *menuitem, GeanyIndentType type)
1823 GeanyDocument *doc;
1825 if (ignore_callback || ! gtk_check_menu_item_get_active(menuitem))
1826 return;
1828 doc = document_get_current();
1829 g_return_if_fail(doc != NULL);
1831 editor_set_indent(doc->editor, type, doc->editor->indent_width);
1832 ui_update_statusbar(doc, -1);
1836 G_MODULE_EXPORT void on_tabs1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1838 set_indent_type(menuitem, GEANY_INDENT_TYPE_TABS);
1842 G_MODULE_EXPORT void on_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1844 set_indent_type(menuitem, GEANY_INDENT_TYPE_SPACES);
1848 G_MODULE_EXPORT void on_tabs_and_spaces1_activate(GtkCheckMenuItem *menuitem, gpointer user_data)
1850 set_indent_type(menuitem, GEANY_INDENT_TYPE_BOTH);
1854 G_MODULE_EXPORT void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer user_data)
1856 GeanyDocument *doc;
1858 if (ignore_callback)
1859 return;
1861 doc = document_get_current();
1862 g_return_if_fail(doc != NULL);
1864 editor_strip_trailing_spaces(doc->editor);
1868 G_MODULE_EXPORT void on_page_setup1_activate(GtkMenuItem *menuitem, gpointer user_data)
1870 printing_page_setup_gtk();
1874 G_MODULE_EXPORT gboolean on_escape_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1876 guint state = event->state & gtk_accelerator_get_default_mod_mask();
1878 /* make pressing escape in the sidebar and toolbar focus the editor */
1879 if (event->keyval == GDK_Escape && state == 0)
1881 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
1882 return TRUE;
1884 return FALSE;
1888 G_MODULE_EXPORT void on_line_breaking1_activate(GtkMenuItem *menuitem, gpointer user_data)
1890 GeanyDocument *doc;
1892 if (ignore_callback)
1893 return;
1895 doc = document_get_current();
1896 g_return_if_fail(doc != NULL);
1898 doc->editor->line_breaking = !doc->editor->line_breaking;
1902 G_MODULE_EXPORT void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
1904 GeanyDocument *doc = document_get_current();
1906 g_return_if_fail(doc != NULL);
1908 editor_replace_spaces(doc->editor);
1912 G_MODULE_EXPORT void on_search1_activate(GtkMenuItem *menuitem, gpointer user_data)
1914 GtkWidget *next_message = ui_lookup_widget(main_widgets.window, "next_message1");
1915 GtkWidget *previous_message = ui_lookup_widget(main_widgets.window, "previous_message1");
1916 gboolean have_messages;
1918 /* enable commands if the messages window has any items */
1919 have_messages = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_msg),
1920 NULL) > 0;
1922 gtk_widget_set_sensitive(next_message, have_messages);
1923 gtk_widget_set_sensitive(previous_message, have_messages);
1927 /* simple implementation (vs. close all which doesn't close documents if cancelled),
1928 * if user_data is set, it is a GtkNotebook child widget */
1929 G_MODULE_EXPORT void on_close_other_documents1_activate(GtkMenuItem *menuitem, gpointer user_data)
1931 guint i;
1932 GeanyDocument *doc, *cur_doc;
1934 if (user_data != NULL)
1936 gint page_num = gtk_notebook_page_num(
1937 GTK_NOTEBOOK(main_widgets.notebook), GTK_WIDGET(user_data));
1938 cur_doc = document_get_from_page(page_num);
1940 else
1941 cur_doc = document_get_current();
1944 for (i = 0; i < documents_array->len; i++)
1946 doc = documents[i];
1948 if (doc == cur_doc || ! doc->is_valid)
1949 continue;
1951 if (! document_close(doc))
1952 break;
1957 G_MODULE_EXPORT void on_menu_reload_configuration1_activate(GtkMenuItem *menuitem, gpointer user_data)
1959 main_reload_configuration();
1963 G_MODULE_EXPORT void on_debug_messages1_activate(GtkMenuItem *menuitem, gpointer user_data)
1965 log_show_debug_messages_dialog();
1969 G_MODULE_EXPORT void on_send_selection_to_vte1_activate(GtkMenuItem *menuitem, gpointer user_data)
1971 #ifdef HAVE_VTE
1972 if (vte_info.have_vte)
1973 vte_send_selection_to_vte();
1974 #endif
1978 G_MODULE_EXPORT gboolean on_window_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
1981 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1983 static GtkWidget *menuitem = NULL;
1985 if (menuitem == NULL)
1986 menuitem = ui_lookup_widget(widget, "menu_fullscreen1");
1988 ignore_callback = TRUE;
1990 ui_prefs.fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) ? TRUE : FALSE;
1991 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), ui_prefs.fullscreen);
1993 ignore_callback = FALSE;
1995 return FALSE;
1999 static void show_notebook_page(const gchar *notebook_name, const gchar *page_name)
2001 GtkWidget *widget;
2002 GtkNotebook *notebook;
2004 widget = ui_lookup_widget(ui_widgets.prefs_dialog, page_name);
2005 notebook = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, notebook_name));
2007 if (notebook != NULL && widget != NULL)
2008 gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, widget));
2012 G_MODULE_EXPORT void on_customize_toolbar1_activate(GtkMenuItem *menuitem, gpointer user_data)
2014 prefs_show_dialog();
2016 /* select the Interface page */
2017 show_notebook_page("notebook2", "notebook6");
2018 /* select the Toolbar subpage */
2019 show_notebook_page("notebook6", "vbox15");
2023 G_MODULE_EXPORT void on_button_customize_toolbar_clicked(GtkButton *button, gpointer user_data)
2025 toolbar_configure(GTK_WINDOW(ui_widgets.prefs_dialog));
2029 G_MODULE_EXPORT void on_cut_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
2031 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_CUTLINE);
2035 G_MODULE_EXPORT void on_copy_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
2037 keybindings_send_command(GEANY_KEY_GROUP_CLIPBOARD, GEANY_KEYS_CLIPBOARD_COPYLINE);
2041 G_MODULE_EXPORT void on_delete_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
2043 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DELETELINE);
2047 G_MODULE_EXPORT void on_duplicate_line_or_selection1_activate(GtkMenuItem *menuitem, gpointer user_data)
2049 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_DUPLICATELINE);
2053 G_MODULE_EXPORT void on_select_current_lines1_activate(GtkMenuItem *menuitem, gpointer user_data)
2055 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_LINE);
2059 G_MODULE_EXPORT void on_select_current_paragraph1_activate(GtkMenuItem *menuitem, gpointer user_data)
2061 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2065 G_MODULE_EXPORT void on_insert_alternative_white_space1_activate(GtkMenuItem *menuitem, gpointer user_data)
2067 keybindings_send_command(GEANY_KEY_GROUP_INSERT, GEANY_KEYS_INSERT_ALTWHITESPACE);
2071 G_MODULE_EXPORT void on_go_to_next_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
2073 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_NEXTMARKER);
2077 G_MODULE_EXPORT void on_go_to_previous_marker1_activate(GtkMenuItem *menuitem, gpointer user_data)
2079 keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_PREVIOUSMARKER);
2083 G_MODULE_EXPORT void on_reflow_lines_block1_activate(GtkMenuItem *menuitem, gpointer user_data)
2085 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH);
2089 G_MODULE_EXPORT void on_transpose_current_line1_activate(GtkMenuItem *menuitem, gpointer user_data)
2091 keybindings_send_command(GEANY_KEY_GROUP_EDITOR, GEANY_KEYS_EDITOR_TRANSPOSELINE);
2095 G_MODULE_EXPORT void on_smart_line_indent1_activate(GtkMenuItem *menuitem, gpointer user_data)
2097 keybindings_send_command(GEANY_KEY_GROUP_FORMAT, GEANY_KEYS_FORMAT_AUTOINDENT);
2101 G_MODULE_EXPORT void on_plugin_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
2103 #ifdef HAVE_PLUGINS
2104 plugin_show_configure(NULL);
2105 #endif
2109 G_MODULE_EXPORT void on_indent_width_activate(GtkMenuItem *menuitem, gpointer user_data)
2111 GeanyDocument *doc;
2112 gchar *label;
2113 gint width;
2115 if (ignore_callback)
2116 return;
2118 label = ui_menu_item_get_text(menuitem);
2119 width = atoi(label);
2120 g_free(label);
2122 doc = document_get_current();
2123 if (doc != NULL && width > 0)
2124 editor_set_indent_width(doc->editor, width);
2128 G_MODULE_EXPORT void on_reset_indentation1_activate(GtkMenuItem *menuitem, gpointer user_data)
2130 guint i;
2132 foreach_document(i)
2133 document_apply_indent_settings(documents[i]);
2135 ui_update_statusbar(NULL, -1);
2136 ui_document_show_hide(NULL);
2140 G_MODULE_EXPORT void on_mark_all1_activate(GtkMenuItem *menuitem, gpointer user_data)
2142 keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
2146 G_MODULE_EXPORT void on_detect_type_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
2148 GeanyDocument *doc = document_get_current();
2149 GeanyIndentType type;
2151 if (doc != NULL && document_detect_indent_type(doc, &type))
2153 editor_set_indent_type(doc->editor, type);
2154 ui_document_show_hide(doc);
2159 G_MODULE_EXPORT void on_detect_width_from_file_activate(GtkMenuItem *menuitem, gpointer user_data)
2161 GeanyDocument *doc = document_get_current();
2162 gint width;
2164 if (doc != NULL && document_detect_indent_width(doc, &width))
2166 editor_set_indent_width(doc->editor, width);
2167 ui_document_show_hide(doc);