Convert old Glade2 user-interface file to Glade3/GtkBuilder format.
[geany-mirror.git] / plugins / splitwindow.c
blob473c2819faff5f322e767e776841ffa01a4c0b22
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.
23 /* Split Window plugin. */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "geanyplugin.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;
78 gint handler_id;
80 EditWindow;
82 static EditWindow edit_window = {NULL, NULL, NULL, NULL, 0 };
85 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data);
88 /* line numbers visibility */
89 static void set_line_numbers(ScintillaObject * sci, gboolean set)
91 if (set)
93 gchar tmp_str[15];
94 gint len = scintilla_send_message(sci, SCI_GETLINECOUNT, 0, 0);
95 gint width;
97 g_snprintf(tmp_str, 15, "_%d", len);
98 width = scintilla_send_message(sci, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t) tmp_str);
99 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, width);
100 scintilla_send_message(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
102 else
104 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, 0);
109 static void on_sci_notify (ScintillaObject *sci, gint param, SCNotification *notif, gpointer data)
111 gint line;
113 switch (notif->nmhdr.code)
115 case SCN_MARGINCLICK:
116 if (notif->margin == 2)
118 line = sci_get_line_from_position(sci, notif->position);
119 scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
121 break;
123 default: break;
128 static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
130 gpointer sdoc;
131 gint pos;
133 /* set the new sci widget to view the existing Scintilla document */
134 sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
135 scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
137 highlighting_set_styles(sci, edit_window.editor->document->file_type);
138 pos = sci_get_current_position(current);
139 sci_set_current_position(sci, pos, TRUE);
141 /* override some defaults */
142 set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
143 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1, 0 ); /* hide marker margin (no commands) */
147 static void set_editor(EditWindow *editwin, GeanyEditor *editor)
149 editwin->editor = editor;
151 if (editwin->handler_id > 0 && editwin->sci != NULL)
153 g_signal_handler_disconnect(editwin->sci, editwin->handler_id);
154 editwin->handler_id = 0;
157 /* first destroy any widget, otherwise its signals will have an
158 * invalid document as user_data */
159 if (editwin->sci != NULL)
160 gtk_widget_destroy(GTK_WIDGET(editwin->sci));
162 editwin->sci = editor_create_widget(editor);
163 gtk_widget_show(GTK_WIDGET(editwin->sci));
164 gtk_container_add(GTK_CONTAINER(editwin->vbox), GTK_WIDGET(editwin->sci));
166 sync_to_current(editwin->sci, editor->sci);
168 if (geany->editor_prefs->folding)
169 editwin->handler_id = g_signal_connect(editwin->sci, "sci-notify",
170 G_CALLBACK(on_sci_notify), NULL);
171 else
172 scintilla_send_message(editwin->sci, SCI_SETMARGINWIDTHN, 2, 0);
174 gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
178 static void set_state(enum State id)
180 gtk_widget_set_sensitive(menu_items.horizontal,
181 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
182 gtk_widget_set_sensitive(menu_items.vertical,
183 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
184 gtk_widget_set_sensitive(menu_items.unsplit,
185 id != STATE_UNSPLIT);
187 plugin_state = id;
191 static const gchar *ui_get_stock_label(const gchar *stock_id)
193 GtkStockItem item;
195 if (gtk_stock_lookup(stock_id, &item))
196 return item.label;
198 g_warning("No stock id '%s'!", stock_id);
199 return "";
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_get_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;
303 GeanyDocument *doc = document_get_current();
304 gint width = notebook->allocation.width / 2;
305 gint height = notebook->allocation.height / 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 gtk_widget_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 gtk_widget_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 gtk_container_add(GTK_CONTAINER(pane), box);
325 edit_window.vbox = box;
327 set_editor(&edit_window, doc->editor);
329 if (horizontal)
331 gtk_paned_set_position(GTK_PANED(pane), width);
333 else
335 gtk_paned_set_position(GTK_PANED(pane), height);
337 gtk_widget_show_all(pane);
341 static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
343 split_view(TRUE);
347 static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
349 split_view(FALSE);
353 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
355 GtkWidget *notebook = geany_data->main_widgets->notebook;
356 GtkWidget *pane = gtk_widget_get_parent(notebook);
357 GtkWidget *parent = gtk_widget_get_parent(pane);
359 set_state(STATE_UNSPLIT);
361 g_return_if_fail(edit_window.editor);
363 gtk_widget_ref(notebook);
364 gtk_container_remove(GTK_CONTAINER(pane), notebook);
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;
376 gtk_container_add(GTK_CONTAINER(parent), notebook);
377 gtk_widget_unref(notebook);
381 static void kb_activate(guint key_id)
383 switch (key_id)
385 case KB_SPLIT_HORIZONTAL:
386 if (plugin_state == STATE_UNSPLIT)
387 split_view(TRUE);
388 break;
389 case KB_SPLIT_VERTICAL:
390 if (plugin_state == STATE_UNSPLIT)
391 split_view(FALSE);
392 break;
393 case KB_SPLIT_UNSPLIT:
394 if (plugin_state != STATE_UNSPLIT)
395 on_unsplit(NULL, NULL);
396 break;
401 void plugin_init(GeanyData *data)
403 GtkWidget *item, *menu;
404 GeanyKeyGroup *key_group;
406 menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
407 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
408 ui_add_document_sensitive(item);
410 menu = gtk_menu_new();
411 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
413 menu_items.horizontal = item =
414 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
415 g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
416 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
418 menu_items.vertical = item =
419 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
420 g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
421 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
423 menu_items.unsplit = item =
424 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
425 g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
426 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
428 gtk_widget_show_all(menu_items.main);
430 set_state(STATE_UNSPLIT);
432 /* setup keybindings */
433 key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
434 keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
435 0, 0, "split_horizontal", _("Split Horizontally"), menu_items.horizontal);
436 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
437 0, 0, "split_vertical", _("Split Vertically"), menu_items.vertical);
438 keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
439 0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
443 static gboolean do_select_current(gpointer data)
445 GeanyDocument *doc;
447 /* guard out for the unlikely case we get called after another unsplitting */
448 if (plugin_state == STATE_UNSPLIT)
449 return FALSE;
451 doc = document_get_current();
452 if (doc)
453 set_editor(&edit_window, doc->editor);
454 else
455 on_unsplit(NULL, NULL);
457 return FALSE;
461 static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
463 if (doc->editor == edit_window.editor)
465 /* select current or unsplit in IDLE time, so the tab has changed */
466 plugin_idle_add(geany_plugin, do_select_current, NULL);
471 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
473 /* update filename */
474 if (doc->editor == edit_window.editor)
475 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
479 static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
480 GeanyFiletype *filetype_old, gpointer user_data)
482 /* update styles */
483 if (edit_window.editor == doc->editor)
484 sync_to_current(edit_window.sci, doc->editor->sci);
488 PluginCallback plugin_callbacks[] =
490 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
491 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
492 { "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
493 { NULL, NULL, FALSE, NULL }
497 void plugin_cleanup(void)
499 if (plugin_state != STATE_UNSPLIT)
500 on_unsplit(NULL, NULL);
502 gtk_widget_destroy(menu_items.main);