2 * splitwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2008-2010 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,
25 /* Split Window plugin. */
27 #include "geanyplugin.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
;
52 STATE_SPLIT_HORIZONTAL
,
61 GtkWidget
*horizontal
;
67 static enum State plugin_state
;
70 typedef struct EditWindow
72 GeanyEditor
*editor
; /* original editor for split view */
73 ScintillaObject
*sci
; /* new editor widget */
75 GtkWidget
*name_label
;
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
)
91 gint len
= scintilla_send_message(sci
, SCI_GETLINECOUNT
, 0, 0);
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 */
101 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 0, 0);
106 static void sync_to_current(ScintillaObject
*sci
, ScintillaObject
*current
)
111 /* set the new sci widget to view the existing Scintilla document */
112 sdoc
= (gpointer
) scintilla_send_message(current
, SCI_GETDOCPOINTER
, 0, 0);
113 scintilla_send_message(sci
, SCI_SETDOCPOINTER
, 0, (sptr_t
) sdoc
);
115 highlighting_set_styles(sci
, edit_window
.editor
->document
->file_type
);
116 pos
= sci_get_current_position(current
);
117 sci_set_current_position(sci
, pos
, TRUE
);
119 /* override some defaults */
120 set_line_numbers(sci
, geany
->editor_prefs
->show_linenumber_margin
);
121 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 1, 0 ); /* hide marker margin (no commands) */
122 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 2, 0 ); /* hide fold margin (no toggle callback) */
126 static void set_editor(EditWindow
*editwin
, GeanyEditor
*editor
)
128 editwin
->editor
= editor
;
130 /* first destroy any widget, otherwise its signals will have an
131 * invalid document as user_data */
132 if (editwin
->sci
!= NULL
)
133 gtk_widget_destroy(GTK_WIDGET(editwin
->sci
));
135 editwin
->sci
= editor_create_widget(editor
);
136 gtk_widget_show(GTK_WIDGET(editwin
->sci
));
137 gtk_container_add(GTK_CONTAINER(editwin
->vbox
), GTK_WIDGET(editwin
->sci
));
139 sync_to_current(editwin
->sci
, editor
->sci
);
141 gtk_label_set_text(GTK_LABEL(editwin
->name_label
), DOC_FILENAME(editor
->document
));
145 static void set_state(enum State id
)
147 gtk_widget_set_sensitive(menu_items
.horizontal
,
148 (id
!= STATE_SPLIT_HORIZONTAL
) && (id
!= STATE_SPLIT_VERTICAL
));
149 gtk_widget_set_sensitive(menu_items
.vertical
,
150 (id
!= STATE_SPLIT_HORIZONTAL
) && (id
!= STATE_SPLIT_VERTICAL
));
151 gtk_widget_set_sensitive(menu_items
.unsplit
,
152 id
!= STATE_UNSPLIT
);
158 static const gchar
*ui_get_stock_label(const gchar
*stock_id
)
162 if (gtk_stock_lookup(stock_id
, &item
))
165 g_warning("No stock id '%s'!", stock_id
);
170 /* Create a GtkToolButton with stock icon, label and tooltip.
171 * @param label can be NULL to use stock label text. @a label can contain underscores,
172 * which will be removed.
173 * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
174 static GtkWidget
*ui_tool_button_new(const gchar
*stock_id
, const gchar
*label
, const gchar
*tooltip
)
179 if (stock_id
&& !label
)
181 label
= ui_get_stock_label(stock_id
);
183 dup
= utils_str_remove_chars(g_strdup(label
), "_");
186 item
= gtk_tool_button_new(NULL
, label
);
188 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item
), stock_id
);
193 ui_widget_set_tooltip_text(GTK_WIDGET(item
), tooltip
);
196 return GTK_WIDGET(item
);
200 static void on_refresh(void)
202 GeanyDocument
*doc
= document_get_current();
204 g_return_if_fail(doc
);
205 g_return_if_fail(edit_window
.sci
);
207 set_editor(&edit_window
, doc
->editor
);
211 static void on_doc_menu_item_clicked(gpointer item
, GeanyDocument
*doc
)
214 set_editor(&edit_window
, doc
->editor
);
218 static void on_doc_menu_show(GtkMenu
*menu
)
220 /* clear the old menu items */
221 gtk_container_foreach(GTK_CONTAINER(menu
), (GtkCallback
) gtk_widget_destroy
, NULL
);
223 ui_menu_add_document_items(menu
, edit_window
.editor
->document
,
224 G_CALLBACK(on_doc_menu_item_clicked
));
228 static GtkWidget
*create_toolbar(void)
230 GtkWidget
*toolbar
, *item
;
231 GtkToolItem
*tool_item
;
233 toolbar
= gtk_toolbar_new();
234 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar
), GTK_ICON_SIZE_MENU
);
235 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar
), GTK_TOOLBAR_ICONS
);
237 tool_item
= gtk_menu_tool_button_new(NULL
, NULL
);
238 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item
), GTK_STOCK_JUMP_TO
);
239 item
= (GtkWidget
*)tool_item
;
240 ui_widget_set_tooltip_text(item
, _("Show the current document"));
241 gtk_container_add(GTK_CONTAINER(toolbar
), item
);
242 g_signal_connect(item
, "clicked", G_CALLBACK(on_refresh
), NULL
);
244 item
= gtk_menu_new();
245 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item
), item
);
246 g_signal_connect(item
, "show", G_CALLBACK(on_doc_menu_show
), NULL
);
248 tool_item
= gtk_tool_item_new();
249 gtk_tool_item_set_expand(tool_item
, TRUE
);
250 gtk_container_add(GTK_CONTAINER(toolbar
), GTK_WIDGET(tool_item
));
252 item
= gtk_label_new(NULL
);
253 gtk_label_set_ellipsize(GTK_LABEL(item
), PANGO_ELLIPSIZE_START
);
254 gtk_container_add(GTK_CONTAINER(tool_item
), item
);
255 edit_window
.name_label
= item
;
257 item
= ui_tool_button_new(GTK_STOCK_CLOSE
, _("_Unsplit"), NULL
);
258 gtk_container_add(GTK_CONTAINER(toolbar
), item
);
259 g_signal_connect(item
, "clicked", G_CALLBACK(on_unsplit
), NULL
);
265 static void split_view(gboolean horizontal
)
267 GtkWidget
*notebook
= geany_data
->main_widgets
->notebook
;
268 GtkWidget
*parent
= gtk_widget_get_parent(notebook
);
269 GtkWidget
*pane
, *toolbar
, *box
;
270 GeanyDocument
*doc
= document_get_current();
271 gint width
= notebook
->allocation
.width
/ 2;
272 gint height
= notebook
->allocation
.height
/ 2;
274 g_return_if_fail(doc
);
275 g_return_if_fail(edit_window
.editor
== NULL
);
277 set_state(horizontal
? STATE_SPLIT_HORIZONTAL
: STATE_SPLIT_VERTICAL
);
279 /* temporarily put document notebook in main vbox (scintilla widgets must stay
280 * in a visible parent window, otherwise there are X selection and scrollbar issues) */
281 gtk_widget_reparent(notebook
,
282 ui_lookup_widget(geany
->main_widgets
->window
, "vbox1"));
284 pane
= horizontal
? gtk_hpaned_new() : gtk_vpaned_new();
285 gtk_container_add(GTK_CONTAINER(parent
), pane
);
286 gtk_widget_reparent(notebook
, pane
);
288 box
= gtk_vbox_new(FALSE
, 0);
289 toolbar
= create_toolbar();
290 gtk_box_pack_start(GTK_BOX(box
), toolbar
, FALSE
, FALSE
, 0);
291 gtk_container_add(GTK_CONTAINER(pane
), box
);
292 edit_window
.vbox
= box
;
294 set_editor(&edit_window
, doc
->editor
);
298 gtk_paned_set_position(GTK_PANED(pane
), width
);
302 gtk_paned_set_position(GTK_PANED(pane
), height
);
304 gtk_widget_show_all(pane
);
308 static void on_split_horizontally(GtkMenuItem
*menuitem
, gpointer user_data
)
314 static void on_split_vertically(GtkMenuItem
*menuitem
, gpointer user_data
)
320 static void on_unsplit(GtkMenuItem
*menuitem
, gpointer user_data
)
322 GtkWidget
*notebook
= geany_data
->main_widgets
->notebook
;
323 GtkWidget
*pane
= gtk_widget_get_parent(notebook
);
324 GtkWidget
*parent
= gtk_widget_get_parent(pane
);
326 set_state(STATE_UNSPLIT
);
328 g_return_if_fail(edit_window
.editor
);
330 /* temporarily put document notebook in main vbox (scintilla widgets must stay
331 * in a visible parent window, otherwise there are X selection and scrollbar issues) */
332 gtk_widget_reparent(notebook
,
333 ui_lookup_widget(geany
->main_widgets
->window
, "vbox1"));
335 gtk_widget_destroy(pane
);
336 edit_window
.editor
= NULL
;
337 edit_window
.sci
= NULL
;
338 gtk_widget_reparent(notebook
, parent
);
342 static void kb_activate(guint key_id
)
346 case KB_SPLIT_HORIZONTAL
:
347 if (plugin_state
== STATE_UNSPLIT
)
350 case KB_SPLIT_VERTICAL
:
351 if (plugin_state
== STATE_UNSPLIT
)
354 case KB_SPLIT_UNSPLIT
:
355 if (plugin_state
!= STATE_UNSPLIT
)
356 on_unsplit(NULL
, NULL
);
362 void plugin_init(GeanyData
*data
)
364 GtkWidget
*item
, *menu
;
365 GeanyKeyGroup
*key_group
;
367 menu_items
.main
= item
= gtk_menu_item_new_with_mnemonic(_("_Split Window"));
368 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data
->main_widgets
->tools_menu
), item
);
369 ui_add_document_sensitive(item
);
371 menu
= gtk_menu_new();
372 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items
.main
), menu
);
374 menu_items
.horizontal
= item
=
375 gtk_menu_item_new_with_mnemonic(_("_Horizontally"));
376 g_signal_connect(item
, "activate", G_CALLBACK(on_split_horizontally
), NULL
);
377 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
379 menu_items
.vertical
= item
=
380 gtk_menu_item_new_with_mnemonic(_("_Vertically"));
381 g_signal_connect(item
, "activate", G_CALLBACK(on_split_vertically
), NULL
);
382 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
384 menu_items
.unsplit
= item
=
385 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
386 g_signal_connect(item
, "activate", G_CALLBACK(on_unsplit
), NULL
);
387 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
389 gtk_widget_show_all(menu_items
.main
);
391 set_state(STATE_UNSPLIT
);
393 /* setup keybindings */
394 key_group
= plugin_set_key_group(geany_plugin
, "split_window", KB_COUNT
, NULL
);
395 keybindings_set_item(key_group
, KB_SPLIT_HORIZONTAL
, kb_activate
,
396 0, 0, "split_horizontal", _("Split Horizontally"), menu_items
.horizontal
);
397 keybindings_set_item(key_group
, KB_SPLIT_VERTICAL
, kb_activate
,
398 0, 0, "split_vertical", _("Split Vertically"), menu_items
.vertical
);
399 keybindings_set_item(key_group
, KB_SPLIT_UNSPLIT
, kb_activate
,
400 0, 0, "split_unsplit", _("_Unsplit"), menu_items
.unsplit
);
404 static void on_document_close(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
406 /* remove the split window because the document won't exist anymore */
407 if (doc
->editor
== edit_window
.editor
)
408 on_unsplit(NULL
, NULL
);
412 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
414 /* update filename */
415 if (doc
->editor
== edit_window
.editor
)
416 gtk_label_set_text(GTK_LABEL(edit_window
.name_label
), DOC_FILENAME(doc
));
420 PluginCallback plugin_callbacks
[] =
422 { "document-close", (GCallback
) &on_document_close
, FALSE
, NULL
},
423 { "document-save", (GCallback
) &on_document_save
, FALSE
, NULL
},
424 { NULL
, NULL
, FALSE
, NULL
}
428 void plugin_cleanup(void)
430 if (plugin_state
!= STATE_UNSPLIT
)
431 on_unsplit(NULL
, NULL
);
433 gtk_widget_destroy(menu_items
.main
);