Fix confusing terminology in Split Window plugin menu labels
[geany-mirror.git] / plugins / splitwindow.c
blob2b2962ff8df04b1505a8e783f1aa7fb1b81a2dc9
1 /*
2 * splitwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2008-2011 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 * $Id$
25 /* Split Window plugin. */
27 #include "geanyplugin.h"
28 #include <string.h>
31 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
32 PLUGIN_SET_INFO(_("Split Window"), _("Splits the editor view into two windows."),
33 VERSION, _("The Geany developer team"))
36 GeanyData *geany_data;
37 GeanyFunctions *geany_functions;
38 GeanyPlugin *geany_plugin;
41 /* Keybinding(s) */
42 enum
44 KB_SPLIT_HORIZONTAL,
45 KB_SPLIT_VERTICAL,
46 KB_SPLIT_UNSPLIT,
47 KB_COUNT
50 enum State
52 STATE_SPLIT_HORIZONTAL,
53 STATE_SPLIT_VERTICAL,
54 STATE_UNSPLIT,
55 STATE_COUNT
58 static struct
60 GtkWidget *main;
61 GtkWidget *horizontal;
62 GtkWidget *vertical;
63 GtkWidget *unsplit;
65 menu_items;
67 static enum State plugin_state;
70 typedef struct EditWindow
72 GeanyEditor *editor; /* original editor for split view */
73 ScintillaObject *sci; /* new editor widget */
74 GtkWidget *vbox;
75 GtkWidget *name_label;
76 gint handler_id;
78 EditWindow;
80 static EditWindow edit_window = {NULL, NULL, NULL, NULL, 0 };
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, SCNotification *notif, gpointer data)
109 gint line;
111 switch (notif->nmhdr.code)
113 case SCN_MARGINCLICK:
114 if (notif->margin == 2)
116 line = sci_get_line_from_position(sci, notif->position);
117 scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
119 break;
121 default: break;
126 static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
128 gpointer sdoc;
129 gint pos;
131 /* set the new sci widget to view the existing Scintilla document */
132 sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
133 scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
135 highlighting_set_styles(sci, edit_window.editor->document->file_type);
136 pos = sci_get_current_position(current);
137 sci_set_current_position(sci, pos, TRUE);
139 /* override some defaults */
140 set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
141 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1, 0 ); /* hide marker margin (no commands) */
145 static void set_editor(EditWindow *editwin, GeanyEditor *editor)
147 editwin->editor = editor;
149 if (editwin->handler_id > 0 && editwin->sci != NULL)
151 g_signal_handler_disconnect(editwin->sci, editwin->handler_id);
152 editwin->handler_id = 0;
155 /* first destroy any widget, otherwise its signals will have an
156 * invalid document as user_data */
157 if (editwin->sci != NULL)
158 gtk_widget_destroy(GTK_WIDGET(editwin->sci));
160 editwin->sci = editor_create_widget(editor);
161 gtk_widget_show(GTK_WIDGET(editwin->sci));
162 gtk_container_add(GTK_CONTAINER(editwin->vbox), GTK_WIDGET(editwin->sci));
164 sync_to_current(editwin->sci, editor->sci);
166 if (geany->editor_prefs->folding)
167 editwin->handler_id = g_signal_connect(editwin->sci, "sci-notify",
168 G_CALLBACK(on_sci_notify), NULL);
169 else
170 scintilla_send_message(editwin->sci, SCI_SETMARGINWIDTHN, 2, 0);
172 gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
176 static void set_state(enum State id)
178 gtk_widget_set_sensitive(menu_items.horizontal,
179 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
180 gtk_widget_set_sensitive(menu_items.vertical,
181 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
182 gtk_widget_set_sensitive(menu_items.unsplit,
183 id != STATE_UNSPLIT);
185 plugin_state = id;
189 static const gchar *ui_get_stock_label(const gchar *stock_id)
191 GtkStockItem item;
193 if (gtk_stock_lookup(stock_id, &item))
194 return item.label;
196 g_warning("No stock id '%s'!", stock_id);
197 return "";
201 /* Create a GtkToolButton with stock icon, label and tooltip.
202 * @param label can be NULL to use stock label text. @a label can contain underscores,
203 * which will be removed.
204 * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
205 static GtkWidget *ui_tool_button_new(const gchar *stock_id, const gchar *label, const gchar *tooltip)
207 GtkToolItem *item;
208 gchar *dupl = NULL;
210 if (stock_id && !label)
212 label = ui_get_stock_label(stock_id);
214 dupl = utils_str_remove_chars(g_strdup(label), "_");
215 label = dupl;
217 item = gtk_tool_button_new(NULL, label);
218 if (stock_id)
219 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item), stock_id);
221 if (!tooltip)
222 tooltip = label;
223 if (tooltip)
224 ui_widget_set_tooltip_text(GTK_WIDGET(item), tooltip);
226 g_free(dupl);
227 return GTK_WIDGET(item);
231 static void on_refresh(void)
233 GeanyDocument *doc = document_get_current();
235 g_return_if_fail(doc);
236 g_return_if_fail(edit_window.sci);
238 set_editor(&edit_window, doc->editor);
242 static void on_doc_menu_item_clicked(gpointer item, GeanyDocument *doc)
244 if (doc->is_valid)
245 set_editor(&edit_window, doc->editor);
249 static void on_doc_menu_show(GtkMenu *menu)
251 /* clear the old menu items */
252 gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) gtk_widget_destroy, NULL);
254 ui_menu_add_document_items(menu, edit_window.editor->document,
255 G_CALLBACK(on_doc_menu_item_clicked));
259 static GtkWidget *create_toolbar(void)
261 GtkWidget *toolbar, *item;
262 GtkToolItem *tool_item;
264 toolbar = gtk_toolbar_new();
265 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
266 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
268 tool_item = gtk_menu_tool_button_new(NULL, NULL);
269 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item), GTK_STOCK_JUMP_TO);
270 item = (GtkWidget*)tool_item;
271 ui_widget_set_tooltip_text(item, _("Show the current document"));
272 gtk_container_add(GTK_CONTAINER(toolbar), item);
273 g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
275 item = gtk_menu_new();
276 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item), item);
277 g_signal_connect(item, "show", G_CALLBACK(on_doc_menu_show), NULL);
279 tool_item = gtk_tool_item_new();
280 gtk_tool_item_set_expand(tool_item, TRUE);
281 gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(tool_item));
283 item = gtk_label_new(NULL);
284 gtk_label_set_ellipsize(GTK_LABEL(item), PANGO_ELLIPSIZE_START);
285 gtk_container_add(GTK_CONTAINER(tool_item), item);
286 edit_window.name_label = item;
288 item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
289 gtk_container_add(GTK_CONTAINER(toolbar), item);
290 g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
292 return toolbar;
296 static void split_view(gboolean horizontal)
298 GtkWidget *notebook = geany_data->main_widgets->notebook;
299 GtkWidget *parent = gtk_widget_get_parent(notebook);
300 GtkWidget *pane, *toolbar, *box;
301 GeanyDocument *doc = document_get_current();
302 gint width = notebook->allocation.width / 2;
303 gint height = notebook->allocation.height / 2;
305 g_return_if_fail(doc);
306 g_return_if_fail(edit_window.editor == NULL);
308 set_state(horizontal ? STATE_SPLIT_HORIZONTAL : STATE_SPLIT_VERTICAL);
310 /* temporarily put document notebook in main vbox (scintilla widgets must stay
311 * in a visible parent window, otherwise there are X selection and scrollbar issues) */
312 gtk_widget_reparent(notebook,
313 ui_lookup_widget(geany->main_widgets->window, "vbox1"));
315 pane = horizontal ? gtk_hpaned_new() : gtk_vpaned_new();
316 gtk_container_add(GTK_CONTAINER(parent), pane);
317 gtk_widget_reparent(notebook, pane);
319 box = gtk_vbox_new(FALSE, 0);
320 toolbar = create_toolbar();
321 gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
322 gtk_container_add(GTK_CONTAINER(pane), box);
323 edit_window.vbox = box;
325 set_editor(&edit_window, doc->editor);
327 if (horizontal)
329 gtk_paned_set_position(GTK_PANED(pane), width);
331 else
333 gtk_paned_set_position(GTK_PANED(pane), height);
335 gtk_widget_show_all(pane);
339 static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
341 split_view(TRUE);
345 static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
347 split_view(FALSE);
351 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
353 GtkWidget *notebook = geany_data->main_widgets->notebook;
354 GtkWidget *pane = gtk_widget_get_parent(notebook);
355 GtkWidget *parent = gtk_widget_get_parent(pane);
357 set_state(STATE_UNSPLIT);
359 g_return_if_fail(edit_window.editor);
361 /* temporarily put document notebook in main vbox (scintilla widgets must stay
362 * in a visible parent window, otherwise there are X selection and scrollbar issues) */
363 gtk_widget_reparent(notebook,
364 ui_lookup_widget(geany->main_widgets->window, "vbox1"));
366 if (edit_window.sci != NULL && edit_window.handler_id > 0)
368 g_signal_handler_disconnect(edit_window.sci, edit_window.handler_id);
369 edit_window.handler_id = 0;
372 gtk_widget_destroy(pane);
373 edit_window.editor = NULL;
374 edit_window.sci = NULL;
375 gtk_widget_reparent(notebook, parent);
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", _("Split Horizontally"), menu_items.horizontal);
434 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
435 0, 0, "split_vertical", _("Split Vertically"), 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 void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
443 /* remove the split window because the document won't exist anymore */
444 if (doc->editor == edit_window.editor)
445 on_unsplit(NULL, NULL);
449 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
451 /* update filename */
452 if (doc->editor == edit_window.editor)
453 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
457 PluginCallback plugin_callbacks[] =
459 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
460 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
461 { NULL, NULL, FALSE, NULL }
465 void plugin_cleanup(void)
467 if (plugin_state != STATE_UNSPLIT)
468 on_unsplit(NULL, NULL);
470 gtk_widget_destroy(menu_items.main);