Merge pull request #3268 from techee/local_tags
[geany-mirror.git] / plugins / splitwindow.c
blobbbf3f4eb158129a6bf0237c3a22fd3279e8c93c5
1 /*
2 * splitwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* Split Window plugin. */
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include "geanyplugin.h"
28 #include "gtkcompat.h"
29 #include <string.h>
32 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
33 PLUGIN_SET_INFO(_("Split Window"), _("Splits the editor view into two windows."),
34 PACKAGE_VERSION, _("The Geany developer team"))
37 GeanyData *geany_data;
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;
77 EditWindow;
79 static EditWindow edit_window = {NULL, NULL, NULL, NULL};
82 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data);
85 /* line numbers visibility */
86 static void set_line_numbers(ScintillaObject * sci, gboolean set)
88 if (set)
90 gchar tmp_str[15];
91 gint len = scintilla_send_message(sci, SCI_GETLINECOUNT, 0, 0);
92 gint width;
94 g_snprintf(tmp_str, 15, "_%d", len);
95 width = scintilla_send_message(sci, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t) tmp_str);
96 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, width);
97 scintilla_send_message(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
99 else
101 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, 0);
106 static void on_sci_notify(ScintillaObject *sci, gint param,
107 SCNotification *nt, gpointer data)
109 gint line;
111 switch (nt->nmhdr.code)
113 /* adapted from editor.c: on_margin_click() */
114 case SCN_MARGINCLICK:
115 /* left click to marker margin toggles marker */
116 if (nt->margin == 1)
118 gboolean set;
119 gint marker = 1;
121 line = sci_get_line_from_position(sci, nt->position);
122 set = sci_is_marker_set_at_line(sci, line, marker);
123 if (!set)
124 sci_set_marker_at_line(sci, line, marker);
125 else
126 sci_delete_marker_at_line(sci, line, marker);
128 /* left click on the folding margin to toggle folding state of current line */
129 if (nt->margin == 2)
131 line = sci_get_line_from_position(sci, nt->position);
132 scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
134 break;
136 default: break;
141 static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
143 gpointer sdoc;
144 gint pos;
146 /* set the new sci widget to view the existing Scintilla document */
147 sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
148 scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
150 highlighting_set_styles(sci, edit_window.editor->document->file_type);
151 pos = sci_get_current_position(current);
152 sci_set_current_position(sci, pos, TRUE);
154 /* override some defaults */
155 set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
156 /* marker margin */
157 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1,
158 scintilla_send_message(current, SCI_GETMARGINWIDTHN, 1, 0));
159 if (!geany->editor_prefs->folding)
160 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 2, 0);
164 static void set_editor(EditWindow *editwin, GeanyEditor *editor)
166 editwin->editor = editor;
168 /* first destroy any widget, otherwise its signals will have an
169 * invalid document as user_data */
170 if (editwin->sci != NULL)
171 gtk_widget_destroy(GTK_WIDGET(editwin->sci));
173 editwin->sci = editor_create_widget(editor);
174 gtk_widget_show(GTK_WIDGET(editwin->sci));
175 gtk_box_pack_start(GTK_BOX(editwin->vbox), GTK_WIDGET(editwin->sci), TRUE, TRUE, 0);
177 sync_to_current(editwin->sci, editor->sci);
179 scintilla_send_message(editwin->sci, SCI_USEPOPUP, 1, 0);
180 /* for margin events */
181 g_signal_connect(editwin->sci, "sci-notify",
182 G_CALLBACK(on_sci_notify), NULL);
184 gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
188 static void set_state(enum State id)
190 gtk_widget_set_sensitive(menu_items.horizontal,
191 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
192 gtk_widget_set_sensitive(menu_items.vertical,
193 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
194 gtk_widget_set_sensitive(menu_items.unsplit,
195 id != STATE_UNSPLIT);
197 plugin_state = id;
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_lookup_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 gtk_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_show_menu(GtkMenuToolButton *button, 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 /* Blocks the ::show-menu signal if the menu's parent toggle button was inactive in the previous run.
260 * This is a hack to workaround https://bugzilla.gnome.org/show_bug.cgi?id=769287
261 * and should NOT be used for any other version than 3.15.9 to 3.21.4, although the code tries and
262 * not block a legitimate signal in case the GTK version in use has been patched */
263 static void show_menu_gtk316_fix(GtkMenuToolButton *button, gpointer data)
265 /* we assume only a single menu can popup at once, so reentrency isn't an issue.
266 * if it was, we could use custom data on the button, but it shouldn't be required */
267 static gboolean block_next = FALSE;
269 if (block_next)
271 g_signal_stop_emission_by_name(button, "show-menu");
272 block_next = FALSE;
274 else
276 GtkWidget *menu = gtk_menu_tool_button_get_menu(button);
277 GtkWidget *parent = gtk_menu_get_attach_widget(GTK_MENU(menu));
279 if (parent && GTK_IS_TOGGLE_BUTTON(parent) && ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(parent)))
280 block_next = TRUE;
285 static GtkWidget *create_toolbar(void)
287 GtkWidget *toolbar, *item;
288 GtkToolItem *tool_item;
290 toolbar = gtk_toolbar_new();
291 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
292 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
294 tool_item = gtk_menu_tool_button_new(NULL, NULL);
295 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item), GTK_STOCK_JUMP_TO);
296 item = (GtkWidget*)tool_item;
297 gtk_widget_set_tooltip_text(item, _("Show the current document"));
298 gtk_container_add(GTK_CONTAINER(toolbar), item);
299 g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
301 item = gtk_menu_new();
302 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item), item);
303 /* hack for https://bugzilla.gnome.org/show_bug.cgi?id=769287 */
304 if (! gtk_check_version(3, 15, 9) && gtk_check_version(3, 21, 4+1))
305 g_signal_connect(tool_item, "show-menu", G_CALLBACK(show_menu_gtk316_fix), NULL);
306 g_signal_connect(tool_item, "show-menu", G_CALLBACK(on_doc_show_menu), item);
308 tool_item = gtk_tool_item_new();
309 gtk_tool_item_set_expand(tool_item, TRUE);
310 gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(tool_item));
312 item = gtk_label_new(NULL);
313 gtk_label_set_ellipsize(GTK_LABEL(item), PANGO_ELLIPSIZE_START);
314 gtk_container_add(GTK_CONTAINER(tool_item), item);
315 edit_window.name_label = item;
317 item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
318 gtk_container_add(GTK_CONTAINER(toolbar), item);
319 g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
321 return toolbar;
325 static void split_view(gboolean horizontal)
327 GtkWidget *notebook = geany_data->main_widgets->notebook;
328 GtkWidget *parent = gtk_widget_get_parent(notebook);
329 GtkWidget *pane, *toolbar, *box, *splitwin_notebook;
330 GeanyDocument *doc = document_get_current();
331 gint width = gtk_widget_get_allocated_width(notebook) / 2;
332 gint height = gtk_widget_get_allocated_height(notebook) / 2;
334 g_return_if_fail(doc);
335 g_return_if_fail(edit_window.editor == NULL);
337 set_state(horizontal ? STATE_SPLIT_HORIZONTAL : STATE_SPLIT_VERTICAL);
339 g_object_ref(notebook);
340 gtk_container_remove(GTK_CONTAINER(parent), notebook);
342 pane = gtk_paned_new(horizontal ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL);
343 gtk_container_add(GTK_CONTAINER(parent), pane);
345 gtk_container_add(GTK_CONTAINER(pane), notebook);
346 g_object_unref(notebook);
348 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
349 toolbar = create_toolbar();
350 gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
351 edit_window.vbox = box;
353 /* used just to make the split window look the same as the main editor */
354 splitwin_notebook = gtk_notebook_new();
355 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(splitwin_notebook), FALSE);
356 gtk_notebook_append_page(GTK_NOTEBOOK(splitwin_notebook), box, NULL);
357 gtk_container_add(GTK_CONTAINER(pane), splitwin_notebook);
359 set_editor(&edit_window, doc->editor);
361 if (horizontal)
363 gtk_paned_set_position(GTK_PANED(pane), width);
365 else
367 gtk_paned_set_position(GTK_PANED(pane), height);
369 gtk_widget_show_all(pane);
373 static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
375 split_view(TRUE);
379 static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
381 split_view(FALSE);
385 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
387 GtkWidget *notebook = geany_data->main_widgets->notebook;
388 GtkWidget *pane = gtk_widget_get_parent(notebook);
389 GtkWidget *parent = gtk_widget_get_parent(pane);
391 set_state(STATE_UNSPLIT);
393 g_return_if_fail(edit_window.editor);
395 g_object_ref(notebook);
396 gtk_container_remove(GTK_CONTAINER(pane), notebook);
398 gtk_widget_destroy(pane);
399 edit_window.editor = NULL;
400 edit_window.sci = NULL;
402 gtk_container_add(GTK_CONTAINER(parent), notebook);
403 g_object_unref(notebook);
407 static void kb_activate(guint key_id)
409 switch (key_id)
411 case KB_SPLIT_HORIZONTAL:
412 if (plugin_state == STATE_UNSPLIT)
413 split_view(TRUE);
414 break;
415 case KB_SPLIT_VERTICAL:
416 if (plugin_state == STATE_UNSPLIT)
417 split_view(FALSE);
418 break;
419 case KB_SPLIT_UNSPLIT:
420 if (plugin_state != STATE_UNSPLIT)
421 on_unsplit(NULL, NULL);
422 break;
427 void plugin_init(GeanyData *data)
429 GtkWidget *item, *menu;
430 GeanyKeyGroup *key_group;
432 menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
433 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
434 ui_add_document_sensitive(item);
436 menu = gtk_menu_new();
437 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
439 menu_items.horizontal = item =
440 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
441 g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
442 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
444 menu_items.vertical = item =
445 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
446 g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
447 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
449 menu_items.unsplit = item =
450 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
451 g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
452 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
454 gtk_widget_show_all(menu_items.main);
456 set_state(STATE_UNSPLIT);
458 /* setup keybindings */
459 key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
460 keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
461 0, 0, "split_horizontal", _("Side by Side"), menu_items.horizontal);
462 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
463 0, 0, "split_vertical", _("Top and Bottom"), menu_items.vertical);
464 keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
465 0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
469 static gboolean do_select_current(gpointer data)
471 GeanyDocument *doc;
473 /* guard out for the unlikely case we get called after another unsplitting */
474 if (plugin_state == STATE_UNSPLIT)
475 return FALSE;
477 doc = document_get_current();
478 if (doc)
479 set_editor(&edit_window, doc->editor);
480 else
481 on_unsplit(NULL, NULL);
483 return FALSE;
487 static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
489 if (doc->editor == edit_window.editor)
491 /* select current or unsplit in IDLE time, so the tab has changed */
492 plugin_idle_add(geany_plugin, do_select_current, NULL);
497 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
499 /* update filename */
500 if (doc->editor == edit_window.editor)
501 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
505 static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
506 GeanyFiletype *filetype_old, gpointer user_data)
508 /* update styles */
509 if (edit_window.editor == doc->editor)
510 sync_to_current(edit_window.sci, doc->editor->sci);
514 PluginCallback plugin_callbacks[] =
516 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
517 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
518 { "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
519 { NULL, NULL, FALSE, NULL }
523 void plugin_cleanup(void)
525 if (plugin_state != STATE_UNSPLIT)
526 on_unsplit(NULL, NULL);
528 gtk_widget_destroy(menu_items.main);