make: Fix incorrectly generating tags for rules content
[geany-mirror.git] / plugins / splitwindow.c
blobed8c0f8747ad0f1b385b16391c5fe56e448823fa
1 /*
2 * splitwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2008-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
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.
22 /* Split Window plugin. */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "geanyplugin.h"
29 #include "gtkcompat.h"
30 #include <string.h>
33 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
34 PLUGIN_SET_INFO(_("Split Window"), _("Splits the editor view into two windows."),
35 VERSION, _("The Geany developer team"))
38 GeanyData *geany_data;
39 GeanyFunctions *geany_functions;
40 GeanyPlugin *geany_plugin;
43 /* Keybinding(s) */
44 enum
46 KB_SPLIT_HORIZONTAL,
47 KB_SPLIT_VERTICAL,
48 KB_SPLIT_UNSPLIT,
49 KB_COUNT
52 enum State
54 STATE_SPLIT_HORIZONTAL,
55 STATE_SPLIT_VERTICAL,
56 STATE_UNSPLIT,
57 STATE_COUNT
60 static struct
62 GtkWidget *main;
63 GtkWidget *horizontal;
64 GtkWidget *vertical;
65 GtkWidget *unsplit;
67 menu_items;
69 static enum State plugin_state;
72 typedef struct EditWindow
74 GeanyEditor *editor; /* original editor for split view */
75 ScintillaObject *sci; /* new editor widget */
76 GtkWidget *vbox;
77 GtkWidget *name_label;
79 EditWindow;
81 static EditWindow edit_window = {NULL, NULL, NULL, NULL};
84 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data);
87 /* line numbers visibility */
88 static void set_line_numbers(ScintillaObject * sci, gboolean set)
90 if (set)
92 gchar tmp_str[15];
93 gint len = scintilla_send_message(sci, SCI_GETLINECOUNT, 0, 0);
94 gint width;
96 g_snprintf(tmp_str, 15, "_%d", len);
97 width = scintilla_send_message(sci, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t) tmp_str);
98 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, width);
99 scintilla_send_message(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
101 else
103 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, 0);
108 static void on_sci_notify(ScintillaObject *sci, gint param,
109 SCNotification *nt, gpointer data)
111 gint line;
113 switch (nt->nmhdr.code)
115 /* adapted from editor.c: on_margin_click() */
116 case SCN_MARGINCLICK:
117 /* left click to marker margin toggles marker */
118 if (nt->margin == 1)
120 gboolean set;
121 gint marker = 1;
123 line = sci_get_line_from_position(sci, nt->position);
124 set = sci_is_marker_set_at_line(sci, line, marker);
125 if (!set)
126 sci_set_marker_at_line(sci, line, marker);
127 else
128 sci_delete_marker_at_line(sci, line, marker);
130 /* left click on the folding margin to toggle folding state of current line */
131 if (nt->margin == 2)
133 line = sci_get_line_from_position(sci, nt->position);
134 scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
136 break;
138 default: break;
143 static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
145 gpointer sdoc;
146 gint pos;
148 /* set the new sci widget to view the existing Scintilla document */
149 sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
150 scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
152 highlighting_set_styles(sci, edit_window.editor->document->file_type);
153 pos = sci_get_current_position(current);
154 sci_set_current_position(sci, pos, TRUE);
156 /* override some defaults */
157 set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
158 /* marker margin */
159 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1,
160 scintilla_send_message(current, SCI_GETMARGINWIDTHN, 1, 0));
161 if (!geany->editor_prefs->folding)
162 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 2, 0);
166 static void set_editor(EditWindow *editwin, GeanyEditor *editor)
168 editwin->editor = editor;
170 /* first destroy any widget, otherwise its signals will have an
171 * invalid document as user_data */
172 if (editwin->sci != NULL)
173 gtk_widget_destroy(GTK_WIDGET(editwin->sci));
175 editwin->sci = editor_create_widget(editor);
176 gtk_widget_show(GTK_WIDGET(editwin->sci));
177 gtk_box_pack_start(GTK_BOX(editwin->vbox), GTK_WIDGET(editwin->sci), TRUE, TRUE, 0);
179 sync_to_current(editwin->sci, editor->sci);
181 scintilla_send_message(editwin->sci, SCI_USEPOPUP, 1, 0);
182 /* for margin events */
183 g_signal_connect(editwin->sci, "sci-notify",
184 G_CALLBACK(on_sci_notify), NULL);
186 gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
190 static void set_state(enum State id)
192 gtk_widget_set_sensitive(menu_items.horizontal,
193 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
194 gtk_widget_set_sensitive(menu_items.vertical,
195 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
196 gtk_widget_set_sensitive(menu_items.unsplit,
197 id != STATE_UNSPLIT);
199 plugin_state = id;
203 /* Create a GtkToolButton with stock icon, label and tooltip.
204 * @param label can be NULL to use stock label text. @a label can contain underscores,
205 * which will be removed.
206 * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
207 static GtkWidget *ui_tool_button_new(const gchar *stock_id, const gchar *label, const gchar *tooltip)
209 GtkToolItem *item;
210 gchar *dupl = NULL;
212 if (stock_id && !label)
214 label = ui_lookup_stock_label(stock_id);
216 dupl = utils_str_remove_chars(g_strdup(label), "_");
217 label = dupl;
219 item = gtk_tool_button_new(NULL, label);
220 if (stock_id)
221 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item), stock_id);
223 if (!tooltip)
224 tooltip = label;
225 if (tooltip)
226 gtk_widget_set_tooltip_text(GTK_WIDGET(item), tooltip);
228 g_free(dupl);
229 return GTK_WIDGET(item);
233 static void on_refresh(void)
235 GeanyDocument *doc = document_get_current();
237 g_return_if_fail(doc);
238 g_return_if_fail(edit_window.sci);
240 set_editor(&edit_window, doc->editor);
244 static void on_doc_menu_item_clicked(gpointer item, GeanyDocument *doc)
246 if (doc->is_valid)
247 set_editor(&edit_window, doc->editor);
251 static void on_doc_menu_show(GtkMenu *menu)
253 /* clear the old menu items */
254 gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) gtk_widget_destroy, NULL);
256 ui_menu_add_document_items(menu, edit_window.editor->document,
257 G_CALLBACK(on_doc_menu_item_clicked));
261 static GtkWidget *create_toolbar(void)
263 GtkWidget *toolbar, *item;
264 GtkToolItem *tool_item;
266 toolbar = gtk_toolbar_new();
267 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
268 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
270 tool_item = gtk_menu_tool_button_new(NULL, NULL);
271 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item), GTK_STOCK_JUMP_TO);
272 item = (GtkWidget*)tool_item;
273 gtk_widget_set_tooltip_text(item, _("Show the current document"));
274 gtk_container_add(GTK_CONTAINER(toolbar), item);
275 g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
277 item = gtk_menu_new();
278 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item), item);
279 g_signal_connect(item, "show", G_CALLBACK(on_doc_menu_show), NULL);
281 tool_item = gtk_tool_item_new();
282 gtk_tool_item_set_expand(tool_item, TRUE);
283 gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(tool_item));
285 item = gtk_label_new(NULL);
286 gtk_label_set_ellipsize(GTK_LABEL(item), PANGO_ELLIPSIZE_START);
287 gtk_container_add(GTK_CONTAINER(tool_item), item);
288 edit_window.name_label = item;
290 item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
291 gtk_container_add(GTK_CONTAINER(toolbar), item);
292 g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
294 return toolbar;
298 static void split_view(gboolean horizontal)
300 GtkWidget *notebook = geany_data->main_widgets->notebook;
301 GtkWidget *parent = gtk_widget_get_parent(notebook);
302 GtkWidget *pane, *toolbar, *box, *splitwin_notebook;
303 GeanyDocument *doc = document_get_current();
304 gint width = gtk_widget_get_allocated_width(notebook) / 2;
305 gint height = gtk_widget_get_allocated_height(notebook) / 2;
307 g_return_if_fail(doc);
308 g_return_if_fail(edit_window.editor == NULL);
310 set_state(horizontal ? STATE_SPLIT_HORIZONTAL : STATE_SPLIT_VERTICAL);
312 g_object_ref(notebook);
313 gtk_container_remove(GTK_CONTAINER(parent), notebook);
315 pane = horizontal ? gtk_hpaned_new() : gtk_vpaned_new();
316 gtk_container_add(GTK_CONTAINER(parent), pane);
318 gtk_container_add(GTK_CONTAINER(pane), notebook);
319 g_object_unref(notebook);
321 box = gtk_vbox_new(FALSE, 0);
322 toolbar = create_toolbar();
323 gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
324 edit_window.vbox = box;
326 /* used just to make the split window look the same as the main editor */
327 splitwin_notebook = gtk_notebook_new();
328 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(splitwin_notebook), FALSE);
329 gtk_notebook_append_page(GTK_NOTEBOOK(splitwin_notebook), box, NULL);
330 gtk_container_add(GTK_CONTAINER(pane), splitwin_notebook);
332 set_editor(&edit_window, doc->editor);
334 if (horizontal)
336 gtk_paned_set_position(GTK_PANED(pane), width);
338 else
340 gtk_paned_set_position(GTK_PANED(pane), height);
342 gtk_widget_show_all(pane);
346 static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
348 split_view(TRUE);
352 static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
354 split_view(FALSE);
358 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
360 GtkWidget *notebook = geany_data->main_widgets->notebook;
361 GtkWidget *pane = gtk_widget_get_parent(notebook);
362 GtkWidget *parent = gtk_widget_get_parent(pane);
364 set_state(STATE_UNSPLIT);
366 g_return_if_fail(edit_window.editor);
368 g_object_ref(notebook);
369 gtk_container_remove(GTK_CONTAINER(pane), notebook);
371 gtk_widget_destroy(pane);
372 edit_window.editor = NULL;
373 edit_window.sci = NULL;
375 gtk_container_add(GTK_CONTAINER(parent), notebook);
376 g_object_unref(notebook);
380 static void kb_activate(guint key_id)
382 switch (key_id)
384 case KB_SPLIT_HORIZONTAL:
385 if (plugin_state == STATE_UNSPLIT)
386 split_view(TRUE);
387 break;
388 case KB_SPLIT_VERTICAL:
389 if (plugin_state == STATE_UNSPLIT)
390 split_view(FALSE);
391 break;
392 case KB_SPLIT_UNSPLIT:
393 if (plugin_state != STATE_UNSPLIT)
394 on_unsplit(NULL, NULL);
395 break;
400 void plugin_init(GeanyData *data)
402 GtkWidget *item, *menu;
403 GeanyKeyGroup *key_group;
405 menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
406 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
407 ui_add_document_sensitive(item);
409 menu = gtk_menu_new();
410 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
412 menu_items.horizontal = item =
413 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
414 g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
415 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
417 menu_items.vertical = item =
418 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
419 g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
420 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
422 menu_items.unsplit = item =
423 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
424 g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
425 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
427 gtk_widget_show_all(menu_items.main);
429 set_state(STATE_UNSPLIT);
431 /* setup keybindings */
432 key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
433 keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
434 0, 0, "split_horizontal", _("Side by Side"), menu_items.horizontal);
435 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
436 0, 0, "split_vertical", _("Top and Bottom"), menu_items.vertical);
437 keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
438 0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
442 static gboolean do_select_current(gpointer data)
444 GeanyDocument *doc;
446 /* guard out for the unlikely case we get called after another unsplitting */
447 if (plugin_state == STATE_UNSPLIT)
448 return FALSE;
450 doc = document_get_current();
451 if (doc)
452 set_editor(&edit_window, doc->editor);
453 else
454 on_unsplit(NULL, NULL);
456 return FALSE;
460 static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
462 if (doc->editor == edit_window.editor)
464 /* select current or unsplit in IDLE time, so the tab has changed */
465 plugin_idle_add(geany_plugin, do_select_current, NULL);
470 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
472 /* update filename */
473 if (doc->editor == edit_window.editor)
474 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
478 static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
479 GeanyFiletype *filetype_old, gpointer user_data)
481 /* update styles */
482 if (edit_window.editor == doc->editor)
483 sync_to_current(edit_window.sci, doc->editor->sci);
487 PluginCallback plugin_callbacks[] =
489 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
490 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
491 { "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
492 { NULL, NULL, FALSE, NULL }
496 void plugin_cleanup(void)
498 if (plugin_state != STATE_UNSPLIT)
499 on_unsplit(NULL, NULL);
501 gtk_widget_destroy(menu_items.main);