Merge pull request #735 from crayxt/patch-1
[geany-mirror.git] / plugins / splitwindow.c
blob4f0dec3ac898f6be0630f930c34d540373ee4748
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 GeanyPlugin *geany_plugin;
42 /* Keybinding(s) */
43 enum
45 KB_SPLIT_HORIZONTAL,
46 KB_SPLIT_VERTICAL,
47 KB_SPLIT_UNSPLIT,
48 KB_COUNT
51 enum State
53 STATE_SPLIT_HORIZONTAL,
54 STATE_SPLIT_VERTICAL,
55 STATE_UNSPLIT,
56 STATE_COUNT
59 static struct
61 GtkWidget *main;
62 GtkWidget *horizontal;
63 GtkWidget *vertical;
64 GtkWidget *unsplit;
66 menu_items;
68 static enum State plugin_state;
71 typedef struct EditWindow
73 GeanyEditor *editor; /* original editor for split view */
74 ScintillaObject *sci; /* new editor widget */
75 GtkWidget *vbox;
76 GtkWidget *name_label;
78 EditWindow;
80 static EditWindow edit_window = {NULL, NULL, NULL, NULL};
83 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data);
86 /* line numbers visibility */
87 static void set_line_numbers(ScintillaObject * sci, gboolean set)
89 if (set)
91 gchar tmp_str[15];
92 gint len = scintilla_send_message(sci, SCI_GETLINECOUNT, 0, 0);
93 gint width;
95 g_snprintf(tmp_str, 15, "_%d", len);
96 width = scintilla_send_message(sci, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t) tmp_str);
97 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, width);
98 scintilla_send_message(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
100 else
102 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, 0);
107 static void on_sci_notify(ScintillaObject *sci, gint param,
108 SCNotification *nt, gpointer data)
110 gint line;
112 switch (nt->nmhdr.code)
114 /* adapted from editor.c: on_margin_click() */
115 case SCN_MARGINCLICK:
116 /* left click to marker margin toggles marker */
117 if (nt->margin == 1)
119 gboolean set;
120 gint marker = 1;
122 line = sci_get_line_from_position(sci, nt->position);
123 set = sci_is_marker_set_at_line(sci, line, marker);
124 if (!set)
125 sci_set_marker_at_line(sci, line, marker);
126 else
127 sci_delete_marker_at_line(sci, line, marker);
129 /* left click on the folding margin to toggle folding state of current line */
130 if (nt->margin == 2)
132 line = sci_get_line_from_position(sci, nt->position);
133 scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
135 break;
137 default: break;
142 static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
144 gpointer sdoc;
145 gint pos;
147 /* set the new sci widget to view the existing Scintilla document */
148 sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
149 scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
151 highlighting_set_styles(sci, edit_window.editor->document->file_type);
152 pos = sci_get_current_position(current);
153 sci_set_current_position(sci, pos, TRUE);
155 /* override some defaults */
156 set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
157 /* marker margin */
158 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1,
159 scintilla_send_message(current, SCI_GETMARGINWIDTHN, 1, 0));
160 if (!geany->editor_prefs->folding)
161 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 2, 0);
165 static void set_editor(EditWindow *editwin, GeanyEditor *editor)
167 editwin->editor = editor;
169 /* first destroy any widget, otherwise its signals will have an
170 * invalid document as user_data */
171 if (editwin->sci != NULL)
172 gtk_widget_destroy(GTK_WIDGET(editwin->sci));
174 editwin->sci = editor_create_widget(editor);
175 gtk_widget_show(GTK_WIDGET(editwin->sci));
176 gtk_box_pack_start(GTK_BOX(editwin->vbox), GTK_WIDGET(editwin->sci), TRUE, TRUE, 0);
178 sync_to_current(editwin->sci, editor->sci);
180 scintilla_send_message(editwin->sci, SCI_USEPOPUP, 1, 0);
181 /* for margin events */
182 g_signal_connect(editwin->sci, "sci-notify",
183 G_CALLBACK(on_sci_notify), NULL);
185 gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
189 static void set_state(enum State id)
191 gtk_widget_set_sensitive(menu_items.horizontal,
192 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
193 gtk_widget_set_sensitive(menu_items.vertical,
194 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
195 gtk_widget_set_sensitive(menu_items.unsplit,
196 id != STATE_UNSPLIT);
198 plugin_state = id;
202 /* Create a GtkToolButton with stock icon, label and tooltip.
203 * @param label can be NULL to use stock label text. @a label can contain underscores,
204 * which will be removed.
205 * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
206 static GtkWidget *ui_tool_button_new(const gchar *stock_id, const gchar *label, const gchar *tooltip)
208 GtkToolItem *item;
209 gchar *dupl = NULL;
211 if (stock_id && !label)
213 label = ui_lookup_stock_label(stock_id);
215 dupl = utils_str_remove_chars(g_strdup(label), "_");
216 label = dupl;
218 item = gtk_tool_button_new(NULL, label);
219 if (stock_id)
220 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item), stock_id);
222 if (!tooltip)
223 tooltip = label;
224 if (tooltip)
225 gtk_widget_set_tooltip_text(GTK_WIDGET(item), tooltip);
227 g_free(dupl);
228 return GTK_WIDGET(item);
232 static void on_refresh(void)
234 GeanyDocument *doc = document_get_current();
236 g_return_if_fail(doc);
237 g_return_if_fail(edit_window.sci);
239 set_editor(&edit_window, doc->editor);
243 static void on_doc_menu_item_clicked(gpointer item, GeanyDocument *doc)
245 if (doc->is_valid)
246 set_editor(&edit_window, doc->editor);
250 static void on_doc_menu_show(GtkMenu *menu)
252 /* clear the old menu items */
253 gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) gtk_widget_destroy, NULL);
255 ui_menu_add_document_items(menu, edit_window.editor->document,
256 G_CALLBACK(on_doc_menu_item_clicked));
260 static GtkWidget *create_toolbar(void)
262 GtkWidget *toolbar, *item;
263 GtkToolItem *tool_item;
265 toolbar = gtk_toolbar_new();
266 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
267 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
269 tool_item = gtk_menu_tool_button_new(NULL, NULL);
270 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item), GTK_STOCK_JUMP_TO);
271 item = (GtkWidget*)tool_item;
272 gtk_widget_set_tooltip_text(item, _("Show the current document"));
273 gtk_container_add(GTK_CONTAINER(toolbar), item);
274 g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
276 item = gtk_menu_new();
277 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item), item);
278 g_signal_connect(item, "show", G_CALLBACK(on_doc_menu_show), NULL);
280 tool_item = gtk_tool_item_new();
281 gtk_tool_item_set_expand(tool_item, TRUE);
282 gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(tool_item));
284 item = gtk_label_new(NULL);
285 gtk_label_set_ellipsize(GTK_LABEL(item), PANGO_ELLIPSIZE_START);
286 gtk_container_add(GTK_CONTAINER(tool_item), item);
287 edit_window.name_label = item;
289 item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
290 gtk_container_add(GTK_CONTAINER(toolbar), item);
291 g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
293 return toolbar;
297 static void split_view(gboolean horizontal)
299 GtkWidget *notebook = geany_data->main_widgets->notebook;
300 GtkWidget *parent = gtk_widget_get_parent(notebook);
301 GtkWidget *pane, *toolbar, *box, *splitwin_notebook;
302 GeanyDocument *doc = document_get_current();
303 gint width = gtk_widget_get_allocated_width(notebook) / 2;
304 gint height = gtk_widget_get_allocated_height(notebook) / 2;
306 g_return_if_fail(doc);
307 g_return_if_fail(edit_window.editor == NULL);
309 set_state(horizontal ? STATE_SPLIT_HORIZONTAL : STATE_SPLIT_VERTICAL);
311 g_object_ref(notebook);
312 gtk_container_remove(GTK_CONTAINER(parent), notebook);
314 pane = horizontal ? gtk_hpaned_new() : gtk_vpaned_new();
315 gtk_container_add(GTK_CONTAINER(parent), pane);
317 gtk_container_add(GTK_CONTAINER(pane), notebook);
318 g_object_unref(notebook);
320 box = gtk_vbox_new(FALSE, 0);
321 toolbar = create_toolbar();
322 gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
323 edit_window.vbox = box;
325 /* used just to make the split window look the same as the main editor */
326 splitwin_notebook = gtk_notebook_new();
327 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(splitwin_notebook), FALSE);
328 gtk_notebook_append_page(GTK_NOTEBOOK(splitwin_notebook), box, NULL);
329 gtk_container_add(GTK_CONTAINER(pane), splitwin_notebook);
331 set_editor(&edit_window, doc->editor);
333 if (horizontal)
335 gtk_paned_set_position(GTK_PANED(pane), width);
337 else
339 gtk_paned_set_position(GTK_PANED(pane), height);
341 gtk_widget_show_all(pane);
345 static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
347 split_view(TRUE);
351 static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
353 split_view(FALSE);
357 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
359 GtkWidget *notebook = geany_data->main_widgets->notebook;
360 GtkWidget *pane = gtk_widget_get_parent(notebook);
361 GtkWidget *parent = gtk_widget_get_parent(pane);
363 set_state(STATE_UNSPLIT);
365 g_return_if_fail(edit_window.editor);
367 g_object_ref(notebook);
368 gtk_container_remove(GTK_CONTAINER(pane), notebook);
370 gtk_widget_destroy(pane);
371 edit_window.editor = NULL;
372 edit_window.sci = NULL;
374 gtk_container_add(GTK_CONTAINER(parent), notebook);
375 g_object_unref(notebook);
379 static void kb_activate(guint key_id)
381 switch (key_id)
383 case KB_SPLIT_HORIZONTAL:
384 if (plugin_state == STATE_UNSPLIT)
385 split_view(TRUE);
386 break;
387 case KB_SPLIT_VERTICAL:
388 if (plugin_state == STATE_UNSPLIT)
389 split_view(FALSE);
390 break;
391 case KB_SPLIT_UNSPLIT:
392 if (plugin_state != STATE_UNSPLIT)
393 on_unsplit(NULL, NULL);
394 break;
399 void plugin_init(GeanyData *data)
401 GtkWidget *item, *menu;
402 GeanyKeyGroup *key_group;
404 menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
405 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
406 ui_add_document_sensitive(item);
408 menu = gtk_menu_new();
409 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
411 menu_items.horizontal = item =
412 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
413 g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
414 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
416 menu_items.vertical = item =
417 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
418 g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
419 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
421 menu_items.unsplit = item =
422 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
423 g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
424 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
426 gtk_widget_show_all(menu_items.main);
428 set_state(STATE_UNSPLIT);
430 /* setup keybindings */
431 key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
432 keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
433 0, 0, "split_horizontal", _("Side by Side"), menu_items.horizontal);
434 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
435 0, 0, "split_vertical", _("Top and Bottom"), menu_items.vertical);
436 keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
437 0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
441 static gboolean do_select_current(gpointer data)
443 GeanyDocument *doc;
445 /* guard out for the unlikely case we get called after another unsplitting */
446 if (plugin_state == STATE_UNSPLIT)
447 return FALSE;
449 doc = document_get_current();
450 if (doc)
451 set_editor(&edit_window, doc->editor);
452 else
453 on_unsplit(NULL, NULL);
455 return FALSE;
459 static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
461 if (doc->editor == edit_window.editor)
463 /* select current or unsplit in IDLE time, so the tab has changed */
464 plugin_idle_add(geany_plugin, do_select_current, NULL);
469 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
471 /* update filename */
472 if (doc->editor == edit_window.editor)
473 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
477 static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
478 GeanyFiletype *filetype_old, gpointer user_data)
480 /* update styles */
481 if (edit_window.editor == doc->editor)
482 sync_to_current(edit_window.sci, doc->editor->sci);
486 PluginCallback plugin_callbacks[] =
488 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
489 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
490 { "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
491 { NULL, NULL, FALSE, NULL }
495 void plugin_cleanup(void)
497 if (plugin_state != STATE_UNSPLIT)
498 on_unsplit(NULL, NULL);
500 gtk_widget_destroy(menu_items.main);