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"
30 #include "Scintilla.h"
31 #include "ScintillaWidget.h"
35 PLUGIN_VERSION_CHECK(GEANY_API_VERSION
)
36 PLUGIN_SET_INFO(_("Split Window"), _("Splits the editor view into two windows."),
37 VERSION
, _("The Geany developer team"))
40 GeanyData
*geany_data
;
41 GeanyFunctions
*geany_functions
;
42 GeanyPlugin
*geany_plugin
;
56 STATE_SPLIT_HORIZONTAL
,
65 GtkWidget
*horizontal
;
71 static enum State plugin_state
;
74 typedef struct EditWindow
76 GeanyEditor
*editor
; /* original editor for split view */
77 ScintillaObject
*sci
; /* new editor widget */
79 GtkWidget
*name_label
;
83 static EditWindow edit_window
= {NULL
, NULL
, NULL
, NULL
};
86 static void on_unsplit(GtkMenuItem
*menuitem
, gpointer user_data
);
89 /* line numbers visibility */
90 static void set_line_numbers(ScintillaObject
* sci
, gboolean set
)
95 gint len
= scintilla_send_message(sci
, SCI_GETLINECOUNT
, 0, 0);
98 g_snprintf(tmp_str
, 15, "_%d", len
);
99 width
= scintilla_send_message(sci
, SCI_TEXTWIDTH
, STYLE_LINENUMBER
, (sptr_t
) tmp_str
);
100 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 0, width
);
101 scintilla_send_message(sci
, SCI_SETMARGINSENSITIVEN
, 0, FALSE
); /* use default behaviour */
105 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 0, 0);
110 static void sync_to_current(ScintillaObject
*sci
, ScintillaObject
*current
)
115 /* set the new sci widget to view the existing Scintilla document */
116 sdoc
= (gpointer
) scintilla_send_message(current
, SCI_GETDOCPOINTER
, 0, 0);
117 scintilla_send_message(sci
, SCI_SETDOCPOINTER
, 0, (sptr_t
) sdoc
);
119 highlighting_set_styles(sci
, edit_window
.editor
->document
->file_type
);
120 pos
= sci_get_current_position(current
);
121 sci_set_current_position(sci
, pos
, TRUE
);
123 /* override some defaults */
124 set_line_numbers(sci
, geany
->editor_prefs
->show_linenumber_margin
);
125 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 1, 0 ); /* hide marker margin (no commands) */
126 scintilla_send_message(sci
, SCI_SETMARGINWIDTHN
, 2, 0 ); /* hide fold margin (no toggle callback) */
130 static void set_editor(EditWindow
*editwin
, GeanyEditor
*editor
)
132 editwin
->editor
= editor
;
134 /* first destroy any widget, otherwise its signals will have an
135 * invalid document as user_data */
136 if (editwin
->sci
!= NULL
)
137 gtk_widget_destroy(GTK_WIDGET(editwin
->sci
));
139 editwin
->sci
= editor_create_widget(editor
);
140 gtk_widget_show(GTK_WIDGET(editwin
->sci
));
141 gtk_container_add(GTK_CONTAINER(editwin
->vbox
), GTK_WIDGET(editwin
->sci
));
143 sync_to_current(editwin
->sci
, editor
->sci
);
145 gtk_label_set_text(GTK_LABEL(editwin
->name_label
), DOC_FILENAME(editor
->document
));
149 static void set_state(enum State id
)
151 gtk_widget_set_sensitive(menu_items
.horizontal
,
152 (id
!= STATE_SPLIT_HORIZONTAL
) && (id
!= STATE_SPLIT_VERTICAL
));
153 gtk_widget_set_sensitive(menu_items
.vertical
,
154 (id
!= STATE_SPLIT_HORIZONTAL
) && (id
!= STATE_SPLIT_VERTICAL
));
155 gtk_widget_set_sensitive(menu_items
.unsplit
,
156 id
!= STATE_UNSPLIT
);
162 static const gchar
*ui_get_stock_label(const gchar
*stock_id
)
166 if (gtk_stock_lookup(stock_id
, &item
))
169 g_warning("No stock id '%s'!", stock_id
);
174 /* Create a GtkToolButton with stock icon, label and tooltip.
175 * @param label can be NULL to use stock label text. @a label can contain underscores,
176 * which will be removed.
177 * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
178 static GtkWidget
*ui_tool_button_new(const gchar
*stock_id
, const gchar
*label
, const gchar
*tooltip
)
183 if (stock_id
&& !label
)
185 label
= ui_get_stock_label(stock_id
);
187 dup
= utils_str_remove_chars(g_strdup(label
), "_");
190 item
= gtk_tool_button_new(NULL
, label
);
192 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item
), stock_id
);
197 ui_widget_set_tooltip_text(GTK_WIDGET(item
), tooltip
);
200 return GTK_WIDGET(item
);
204 static void on_refresh(void)
206 GeanyDocument
*doc
= document_get_current();
208 g_return_if_fail(doc
);
209 g_return_if_fail(edit_window
.sci
);
211 set_editor(&edit_window
, doc
->editor
);
215 static void on_doc_menu_item_clicked(gpointer item
, GeanyDocument
*doc
)
218 set_editor(&edit_window
, doc
->editor
);
222 static void on_doc_menu_show(GtkMenu
*menu
)
224 /* clear the old menu items */
225 gtk_container_foreach(GTK_CONTAINER(menu
), (GtkCallback
) gtk_widget_destroy
, NULL
);
227 ui_menu_add_document_items(menu
, edit_window
.editor
->document
,
228 G_CALLBACK(on_doc_menu_item_clicked
));
232 static GtkWidget
*create_toolbar(void)
234 GtkWidget
*toolbar
, *item
;
235 GtkToolItem
*tool_item
;
237 toolbar
= gtk_toolbar_new();
238 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar
), GTK_ICON_SIZE_MENU
);
239 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar
), GTK_TOOLBAR_ICONS
);
241 tool_item
= gtk_menu_tool_button_new(NULL
, NULL
);
242 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item
), GTK_STOCK_JUMP_TO
);
243 item
= (GtkWidget
*)tool_item
;
244 ui_widget_set_tooltip_text(item
, _("Show the current document"));
245 gtk_container_add(GTK_CONTAINER(toolbar
), item
);
246 g_signal_connect(item
, "clicked", G_CALLBACK(on_refresh
), NULL
);
248 item
= gtk_menu_new();
249 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item
), item
);
250 g_signal_connect(item
, "show", G_CALLBACK(on_doc_menu_show
), NULL
);
252 tool_item
= gtk_tool_item_new();
253 gtk_tool_item_set_expand(tool_item
, TRUE
);
254 gtk_container_add(GTK_CONTAINER(toolbar
), GTK_WIDGET(tool_item
));
256 item
= gtk_label_new(NULL
);
257 gtk_label_set_ellipsize(GTK_LABEL(item
), PANGO_ELLIPSIZE_START
);
258 gtk_container_add(GTK_CONTAINER(tool_item
), item
);
259 edit_window
.name_label
= item
;
261 item
= ui_tool_button_new(GTK_STOCK_CLOSE
, _("_Unsplit"), NULL
);
262 gtk_container_add(GTK_CONTAINER(toolbar
), item
);
263 g_signal_connect(item
, "clicked", G_CALLBACK(on_unsplit
), NULL
);
269 static void split_view(gboolean horizontal
)
271 GtkWidget
*notebook
= geany_data
->main_widgets
->notebook
;
272 GtkWidget
*parent
= gtk_widget_get_parent(notebook
);
273 GtkWidget
*pane
, *toolbar
, *box
;
274 GeanyDocument
*doc
= document_get_current();
275 gint width
= notebook
->allocation
.width
/ 2;
276 gint height
= notebook
->allocation
.height
/ 2;
278 g_return_if_fail(doc
);
279 g_return_if_fail(edit_window
.editor
== NULL
);
281 set_state(horizontal
? STATE_SPLIT_HORIZONTAL
: STATE_SPLIT_VERTICAL
);
283 /* temporarily put document notebook in main vbox (scintilla widgets must stay
284 * in a visible parent window, otherwise there are X selection and scrollbar issues) */
285 gtk_widget_reparent(notebook
,
286 ui_lookup_widget(geany
->main_widgets
->window
, "vbox1"));
288 pane
= horizontal
? gtk_hpaned_new() : gtk_vpaned_new();
289 gtk_container_add(GTK_CONTAINER(parent
), pane
);
290 gtk_widget_reparent(notebook
, pane
);
292 box
= gtk_vbox_new(FALSE
, 0);
293 toolbar
= create_toolbar();
294 gtk_box_pack_start(GTK_BOX(box
), toolbar
, FALSE
, FALSE
, 0);
295 gtk_container_add(GTK_CONTAINER(pane
), box
);
296 edit_window
.vbox
= box
;
298 set_editor(&edit_window
, doc
->editor
);
302 gtk_paned_set_position(GTK_PANED(pane
), width
);
306 gtk_paned_set_position(GTK_PANED(pane
), height
);
308 gtk_widget_show_all(pane
);
312 static void on_split_horizontally(GtkMenuItem
*menuitem
, gpointer user_data
)
318 static void on_split_vertically(GtkMenuItem
*menuitem
, gpointer user_data
)
324 static void on_unsplit(GtkMenuItem
*menuitem
, gpointer user_data
)
326 GtkWidget
*notebook
= geany_data
->main_widgets
->notebook
;
327 GtkWidget
*pane
= gtk_widget_get_parent(notebook
);
328 GtkWidget
*parent
= gtk_widget_get_parent(pane
);
330 set_state(STATE_UNSPLIT
);
332 g_return_if_fail(edit_window
.editor
);
334 /* temporarily put document notebook in main vbox (scintilla widgets must stay
335 * in a visible parent window, otherwise there are X selection and scrollbar issues) */
336 gtk_widget_reparent(notebook
,
337 ui_lookup_widget(geany
->main_widgets
->window
, "vbox1"));
339 gtk_widget_destroy(pane
);
340 edit_window
.editor
= NULL
;
341 edit_window
.sci
= NULL
;
342 gtk_widget_reparent(notebook
, parent
);
346 static void kb_activate(guint key_id
)
350 case KB_SPLIT_HORIZONTAL
:
351 if (plugin_state
== STATE_UNSPLIT
)
354 case KB_SPLIT_VERTICAL
:
355 if (plugin_state
== STATE_UNSPLIT
)
358 case KB_SPLIT_UNSPLIT
:
359 if (plugin_state
!= STATE_UNSPLIT
)
360 on_unsplit(NULL
, NULL
);
366 void plugin_init(GeanyData
*data
)
368 GtkWidget
*item
, *menu
;
369 GeanyKeyGroup
*key_group
;
371 menu_items
.main
= item
= gtk_menu_item_new_with_mnemonic(_("_Split Window"));
372 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data
->main_widgets
->tools_menu
), item
);
373 ui_add_document_sensitive(item
);
375 menu
= gtk_menu_new();
376 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items
.main
), menu
);
378 menu_items
.horizontal
= item
=
379 gtk_menu_item_new_with_mnemonic(_("_Horizontally"));
380 g_signal_connect(item
, "activate", G_CALLBACK(on_split_horizontally
), NULL
);
381 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
383 menu_items
.vertical
= item
=
384 gtk_menu_item_new_with_mnemonic(_("_Vertically"));
385 g_signal_connect(item
, "activate", G_CALLBACK(on_split_vertically
), NULL
);
386 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
388 menu_items
.unsplit
= item
=
389 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
390 g_signal_connect(item
, "activate", G_CALLBACK(on_unsplit
), NULL
);
391 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
393 gtk_widget_show_all(menu_items
.main
);
395 set_state(STATE_UNSPLIT
);
397 /* setup keybindings */
398 key_group
= plugin_set_key_group(geany_plugin
, "split_window", KB_COUNT
, NULL
);
399 keybindings_set_item(key_group
, KB_SPLIT_HORIZONTAL
, kb_activate
,
400 0, 0, "split_horizontal", _("Split Horizontally"), menu_items
.horizontal
);
401 keybindings_set_item(key_group
, KB_SPLIT_VERTICAL
, kb_activate
,
402 0, 0, "split_vertical", _("Split Vertically"), menu_items
.vertical
);
403 keybindings_set_item(key_group
, KB_SPLIT_UNSPLIT
, kb_activate
,
404 0, 0, "split_unsplit", _("_Unsplit"), menu_items
.unsplit
);
408 static void on_document_close(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
410 /* remove the split window because the document won't exist anymore */
411 if (doc
->editor
== edit_window
.editor
)
412 on_unsplit(NULL
, NULL
);
416 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
418 /* update filename */
419 if (doc
->editor
== edit_window
.editor
)
420 gtk_label_set_text(GTK_LABEL(edit_window
.name_label
), DOC_FILENAME(doc
));
424 PluginCallback plugin_callbacks
[] =
426 { "document-close", (GCallback
) &on_document_close
, FALSE
, NULL
},
427 { "document-save", (GCallback
) &on_document_save
, FALSE
, NULL
},
428 { NULL
, NULL
, FALSE
, NULL
}
432 void plugin_cleanup(void)
434 if (plugin_state
!= STATE_UNSPLIT
)
435 on_unsplit(NULL
, NULL
);
437 gtk_widget_destroy(menu_items
.main
);