Merge pull request #386 from b4n/po/fr
[geany-mirror.git] / plugins / splitwindow.c
blob5c1786e223b14216652b0b741a71c843b6c0866c
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 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;
79 EditWindow;
81 static EditWindow edit_window = {NULL, NULL, NULL, NULL};
84 static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data);
87 /* line numbers visibility */
88 static void set_line_numbers(ScintillaObject * sci, gboolean set)
90 if (set)
92 gchar tmp_str[15];
93 gint len = scintilla_send_message(sci, SCI_GETLINECOUNT, 0, 0);
94 gint width;
96 g_snprintf(tmp_str, 15, "_%d", len);
97 width = scintilla_send_message(sci, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t) tmp_str);
98 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, width);
99 scintilla_send_message(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
101 else
103 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, 0);
108 static void on_sci_notify(ScintillaObject *sci, gint param,
109 SCNotification *nt, gpointer data)
111 gint line;
113 switch (nt->nmhdr.code)
115 /* adapted from editor.c: on_margin_click() */
116 case SCN_MARGINCLICK:
117 /* left click to marker margin toggles marker */
118 if (nt->margin == 1)
120 gboolean set;
121 gint marker = 1;
123 line = sci_get_line_from_position(sci, nt->position);
124 set = sci_is_marker_set_at_line(sci, line, marker);
125 if (!set)
126 sci_set_marker_at_line(sci, line, marker);
127 else
128 sci_delete_marker_at_line(sci, line, marker);
130 /* left click on the folding margin to toggle folding state of current line */
131 if (nt->margin == 2)
133 line = sci_get_line_from_position(sci, nt->position);
134 scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
136 break;
138 default: break;
143 static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
145 gpointer sdoc;
146 gint pos;
148 /* set the new sci widget to view the existing Scintilla document */
149 sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
150 scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
152 highlighting_set_styles(sci, edit_window.editor->document->file_type);
153 pos = sci_get_current_position(current);
154 sci_set_current_position(sci, pos, TRUE);
156 /* override some defaults */
157 set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
158 /* marker margin */
159 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1,
160 scintilla_send_message(current, SCI_GETMARGINWIDTHN, 1, 0));
161 if (!geany->editor_prefs->folding)
162 scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 2, 0);
166 static void set_editor(EditWindow *editwin, GeanyEditor *editor)
168 editwin->editor = editor;
170 /* first destroy any widget, otherwise its signals will have an
171 * invalid document as user_data */
172 if (editwin->sci != NULL)
173 gtk_widget_destroy(GTK_WIDGET(editwin->sci));
175 editwin->sci = editor_create_widget(editor);
176 gtk_widget_show(GTK_WIDGET(editwin->sci));
177 gtk_box_pack_start(GTK_BOX(editwin->vbox), GTK_WIDGET(editwin->sci), TRUE, TRUE, 0);
179 sync_to_current(editwin->sci, editor->sci);
181 scintilla_send_message(editwin->sci, SCI_USEPOPUP, 1, 0);
182 /* for margin events */
183 g_signal_connect(editwin->sci, "sci-notify",
184 G_CALLBACK(on_sci_notify), NULL);
186 gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
190 static void set_state(enum State id)
192 gtk_widget_set_sensitive(menu_items.horizontal,
193 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
194 gtk_widget_set_sensitive(menu_items.vertical,
195 (id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
196 gtk_widget_set_sensitive(menu_items.unsplit,
197 id != STATE_UNSPLIT);
199 plugin_state = id;
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_lookup_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 = gtk_widget_get_allocated_width(notebook) / 2;
305 gint height = gtk_widget_get_allocated_height(notebook) / 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 g_object_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 g_object_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 g_object_ref(notebook);
364 gtk_container_remove(GTK_CONTAINER(pane), notebook);
366 gtk_widget_destroy(pane);
367 edit_window.editor = NULL;
368 edit_window.sci = NULL;
370 gtk_container_add(GTK_CONTAINER(parent), notebook);
371 g_object_unref(notebook);
375 static void kb_activate(guint key_id)
377 switch (key_id)
379 case KB_SPLIT_HORIZONTAL:
380 if (plugin_state == STATE_UNSPLIT)
381 split_view(TRUE);
382 break;
383 case KB_SPLIT_VERTICAL:
384 if (plugin_state == STATE_UNSPLIT)
385 split_view(FALSE);
386 break;
387 case KB_SPLIT_UNSPLIT:
388 if (plugin_state != STATE_UNSPLIT)
389 on_unsplit(NULL, NULL);
390 break;
395 void plugin_init(GeanyData *data)
397 GtkWidget *item, *menu;
398 GeanyKeyGroup *key_group;
400 menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
401 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
402 ui_add_document_sensitive(item);
404 menu = gtk_menu_new();
405 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
407 menu_items.horizontal = item =
408 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
409 g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
410 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
412 menu_items.vertical = item =
413 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
414 g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
415 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
417 menu_items.unsplit = item =
418 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
419 g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
420 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
422 gtk_widget_show_all(menu_items.main);
424 set_state(STATE_UNSPLIT);
426 /* setup keybindings */
427 key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
428 keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
429 0, 0, "split_horizontal", _("Side by Side"), menu_items.horizontal);
430 keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
431 0, 0, "split_vertical", _("Top and Bottom"), menu_items.vertical);
432 keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
433 0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
437 static gboolean do_select_current(gpointer data)
439 GeanyDocument *doc;
441 /* guard out for the unlikely case we get called after another unsplitting */
442 if (plugin_state == STATE_UNSPLIT)
443 return FALSE;
445 doc = document_get_current();
446 if (doc)
447 set_editor(&edit_window, doc->editor);
448 else
449 on_unsplit(NULL, NULL);
451 return FALSE;
455 static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
457 if (doc->editor == edit_window.editor)
459 /* select current or unsplit in IDLE time, so the tab has changed */
460 plugin_idle_add(geany_plugin, do_select_current, NULL);
465 static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
467 /* update filename */
468 if (doc->editor == edit_window.editor)
469 gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
473 static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
474 GeanyFiletype *filetype_old, gpointer user_data)
476 /* update styles */
477 if (edit_window.editor == doc->editor)
478 sync_to_current(edit_window.sci, doc->editor->sci);
482 PluginCallback plugin_callbacks[] =
484 { "document-close", (GCallback) &on_document_close, FALSE, NULL },
485 { "document-save", (GCallback) &on_document_save, FALSE, NULL },
486 { "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
487 { NULL, NULL, FALSE, NULL }
491 void plugin_cleanup(void)
493 if (plugin_state != STATE_UNSPLIT)
494 on_unsplit(NULL, NULL);
496 gtk_widget_destroy(menu_items.main);