Small update of German translation
[geany-mirror.git] / plugins / splitwindow.c
blob724c765e9b9f931274f7aff0c765a9609c10d9fa
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_show_menu(GtkMenuToolButton *button, 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 #if GTK_CHECK_VERSION(3, 0, 0)
261 /* Blocks the ::show-menu signal if the menu's parent toggle button was inactive in the previous run.
262 * This is a hack to workaround https://bugzilla.gnome.org/show_bug.cgi?id=769287
263 * and should NOT be used for any other version than 3.15.9 to 3.21.4, although the code tries and
264 * not block a legitimate signal in case the GTK version in use has been patched */
265 static void show_menu_gtk316_fix(GtkMenuToolButton *button, gpointer data)
267 /* we assume only a single menu can popup at once, so reentrency isn't an issue.
268 * if it was, we could use custom data on the button, but it shouldn't be required */
269 static gboolean block_next = FALSE;
271 if (block_next)
273 g_signal_stop_emission_by_name(button, "show-menu");
274 block_next = FALSE;
276 else
278 GtkWidget *menu = gtk_menu_tool_button_get_menu(button);
279 GtkWidget *parent = gtk_menu_get_attach_widget(GTK_MENU(menu));
281 if (parent && GTK_IS_TOGGLE_BUTTON(parent) && ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(parent)))
282 block_next = TRUE;
285 #endif
288 static GtkWidget *create_toolbar(void)
290 GtkWidget *toolbar, *item;
291 GtkToolItem *tool_item;
293 toolbar = gtk_toolbar_new();
294 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
295 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
297 tool_item = gtk_menu_tool_button_new(NULL, NULL);
298 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item), GTK_STOCK_JUMP_TO);
299 item = (GtkWidget*)tool_item;
300 gtk_widget_set_tooltip_text(item, _("Show the current document"));
301 gtk_container_add(GTK_CONTAINER(toolbar), item);
302 g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
304 item = gtk_menu_new();
305 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item), item);
306 #if GTK_CHECK_VERSION (3, 0, 0)
307 /* hack for https://bugzilla.gnome.org/show_bug.cgi?id=769287 */
308 if (! gtk_check_version(3, 15, 9) && gtk_check_version(3, 21, 4+1))
309 g_signal_connect(tool_item, "show-menu", G_CALLBACK(show_menu_gtk316_fix), NULL);
310 #endif
311 g_signal_connect(tool_item, "show-menu", G_CALLBACK(on_doc_show_menu), item);
313 tool_item = gtk_tool_item_new();
314 gtk_tool_item_set_expand(tool_item, TRUE);
315 gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(tool_item));
317 item = gtk_label_new(NULL);
318 gtk_label_set_ellipsize(GTK_LABEL(item), PANGO_ELLIPSIZE_START);
319 gtk_container_add(GTK_CONTAINER(tool_item), item);
320 edit_window.name_label = item;
322 item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
323 gtk_container_add(GTK_CONTAINER(toolbar), item);
324 g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
326 return toolbar;
330 static void split_view(gboolean horizontal)
332 GtkWidget *notebook = geany_data->main_widgets->notebook;
333 GtkWidget *parent = gtk_widget_get_parent(notebook);
334 GtkWidget *pane, *toolbar, *box, *splitwin_notebook;
335 GeanyDocument *doc = document_get_current();
336 gint width = gtk_widget_get_allocated_width(notebook) / 2;
337 gint height = gtk_widget_get_allocated_height(notebook) / 2;
339 g_return_if_fail(doc);
340 g_return_if_fail(edit_window.editor == NULL);
342 set_state(horizontal ? STATE_SPLIT_HORIZONTAL : STATE_SPLIT_VERTICAL);
344 g_object_ref(notebook);
345 gtk_container_remove(GTK_CONTAINER(parent), notebook);
347 pane = horizontal ? gtk_hpaned_new() : gtk_vpaned_new();
348 gtk_container_add(GTK_CONTAINER(parent), pane);
350 gtk_container_add(GTK_CONTAINER(pane), notebook);
351 g_object_unref(notebook);
353 box = gtk_vbox_new(FALSE, 0);
354 toolbar = create_toolbar();
355 gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
356 edit_window.vbox = box;
358 /* used just to make the split window look the same as the main editor */
359 splitwin_notebook = gtk_notebook_new();
360 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(splitwin_notebook), FALSE);
361 gtk_notebook_append_page(GTK_NOTEBOOK(splitwin_notebook), box, NULL);
362 gtk_container_add(GTK_CONTAINER(pane), splitwin_notebook);
364 set_editor(&edit_window, doc->editor);
366 if (horizontal)
368 gtk_paned_set_position(GTK_PANED(pane), width);
370 else
372 gtk_paned_set_position(GTK_PANED(pane), height);
374 gtk_widget_show_all(pane);
378 static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
380 split_view(TRUE);
384 static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
386 split_view(FALSE);
390 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
392 GtkWidget *notebook = geany_data->main_widgets->notebook;
393 GtkWidget *pane = gtk_widget_get_parent(notebook);
394 GtkWidget *parent = gtk_widget_get_parent(pane);
396 set_state(STATE_UNSPLIT);
398 g_return_if_fail(edit_window.editor);
400 g_object_ref(notebook);
401 gtk_container_remove(GTK_CONTAINER(pane), notebook);
403 gtk_widget_destroy(pane);
404 edit_window.editor = NULL;
405 edit_window.sci = NULL;
407 gtk_container_add(GTK_CONTAINER(parent), notebook);
408 g_object_unref(notebook);
412 static void kb_activate(guint key_id)
414 switch (key_id)
416 case KB_SPLIT_HORIZONTAL:
417 if (plugin_state == STATE_UNSPLIT)
418 split_view(TRUE);
419 break;
420 case KB_SPLIT_VERTICAL:
421 if (plugin_state == STATE_UNSPLIT)
422 split_view(FALSE);
423 break;
424 case KB_SPLIT_UNSPLIT:
425 if (plugin_state != STATE_UNSPLIT)
426 on_unsplit(NULL, NULL);
427 break;
432 void plugin_init(GeanyData *data)
434 GtkWidget *item, *menu;
435 GeanyKeyGroup *key_group;
437 menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
438 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
439 ui_add_document_sensitive(item);
441 menu = gtk_menu_new();
442 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
444 menu_items.horizontal = item =
445 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
446 g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
447 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
449 menu_items.vertical = item =
450 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
451 g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
452 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
454 menu_items.unsplit = item =
455 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
456 g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
457 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
459 gtk_widget_show_all(menu_items.main);
461 set_state(STATE_UNSPLIT);
463 /* setup keybindings */
464 key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
465 keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
466 0, 0, "split_horizontal", _("Side by Side"), menu_items.horizontal);
467 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
468 0, 0, "split_vertical", _("Top and Bottom"), menu_items.vertical);
469 keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
470 0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
474 static gboolean do_select_current(gpointer data)
476 GeanyDocument *doc;
478 /* guard out for the unlikely case we get called after another unsplitting */
479 if (plugin_state == STATE_UNSPLIT)
480 return FALSE;
482 doc = document_get_current();
483 if (doc)
484 set_editor(&edit_window, doc->editor);
485 else
486 on_unsplit(NULL, NULL);
488 return FALSE;
492 static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
494 if (doc->editor == edit_window.editor)
496 /* select current or unsplit in IDLE time, so the tab has changed */
497 plugin_idle_add(geany_plugin, do_select_current, NULL);
502 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
504 /* update filename */
505 if (doc->editor == edit_window.editor)
506 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
510 static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
511 GeanyFiletype *filetype_old, gpointer user_data)
513 /* update styles */
514 if (edit_window.editor == doc->editor)
515 sync_to_current(edit_window.sci, doc->editor->sci);
519 PluginCallback plugin_callbacks[] =
521 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
522 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
523 { "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
524 { NULL, NULL, FALSE, NULL }
528 void plugin_cleanup(void)
530 if (plugin_state != STATE_UNSPLIT)
531 on_unsplit(NULL, NULL);
533 gtk_widget_destroy(menu_items.main);