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. */
27 #include "geanyplugin.h"
28 #include "gtkcompat.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
;
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 on_sci_notify(ScintillaObject
*sci
, gint param
,
107 SCNotification
*nt
, gpointer data
)
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 */
121 line
= sci_get_line_from_position(sci
, nt
->position
);
122 set
= sci_is_marker_set_at_line(sci
, line
, marker
);
124 sci_set_marker_at_line(sci
, line
, marker
);
126 sci_delete_marker_at_line(sci
, line
, marker
);
128 /* left click on the folding margin to toggle folding state of current line */
131 line
= sci_get_line_from_position(sci
, nt
->position
);
132 scintilla_send_message(sci
, SCI_TOGGLEFOLD
, line
, 0);
141 static void sync_to_current(ScintillaObject
*sci
, ScintillaObject
*current
)
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
);
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
);
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
)
210 if (stock_id
&& !label
)
212 label
= ui_lookup_stock_label(stock_id
);
214 dupl
= utils_str_remove_chars(g_strdup(label
), "_");
217 item
= gtk_tool_button_new(NULL
, label
);
219 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item
), stock_id
);
224 gtk_widget_set_tooltip_text(GTK_WIDGET(item
), tooltip
);
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
)
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
) (void(*)(void)) gtk_widget_destroy
, NULL
);
254 ui_menu_add_document_items(menu
, edit_window
.editor
->document
,
255 G_CALLBACK(on_doc_menu_item_clicked
));
259 static GtkWidget
*create_toolbar(void)
261 GtkWidget
*toolbar
, *item
;
262 GtkToolItem
*tool_item
;
264 toolbar
= gtk_toolbar_new();
265 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar
), GTK_ICON_SIZE_MENU
);
266 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar
), GTK_TOOLBAR_ICONS
);
268 tool_item
= gtk_menu_tool_button_new(NULL
, NULL
);
269 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item
), GTK_STOCK_JUMP_TO
);
270 item
= (GtkWidget
*)tool_item
;
271 gtk_widget_set_tooltip_text(item
, _("Show the current document"));
272 gtk_container_add(GTK_CONTAINER(toolbar
), item
);
273 g_signal_connect(item
, "clicked", G_CALLBACK(on_refresh
), NULL
);
275 item
= gtk_menu_new();
276 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item
), item
);
277 g_signal_connect(tool_item
, "show-menu", G_CALLBACK(on_doc_show_menu
), item
);
279 tool_item
= gtk_tool_item_new();
280 gtk_tool_item_set_expand(tool_item
, TRUE
);
281 gtk_container_add(GTK_CONTAINER(toolbar
), GTK_WIDGET(tool_item
));
283 item
= gtk_label_new(NULL
);
284 gtk_label_set_ellipsize(GTK_LABEL(item
), PANGO_ELLIPSIZE_START
);
285 gtk_container_add(GTK_CONTAINER(tool_item
), item
);
286 edit_window
.name_label
= item
;
288 item
= ui_tool_button_new(GTK_STOCK_CLOSE
, _("_Unsplit"), NULL
);
289 gtk_container_add(GTK_CONTAINER(toolbar
), item
);
290 g_signal_connect(item
, "clicked", G_CALLBACK(on_unsplit
), NULL
);
296 static void split_view(gboolean horizontal
)
298 GtkWidget
*notebook
= geany_data
->main_widgets
->notebook
;
299 GtkWidget
*parent
= gtk_widget_get_parent(notebook
);
300 GtkWidget
*pane
, *toolbar
, *box
, *splitwin_notebook
;
301 GeanyDocument
*doc
= document_get_current();
302 gint width
= gtk_widget_get_allocated_width(notebook
) / 2;
303 gint height
= gtk_widget_get_allocated_height(notebook
) / 2;
305 g_return_if_fail(doc
);
306 g_return_if_fail(edit_window
.editor
== NULL
);
308 set_state(horizontal
? STATE_SPLIT_HORIZONTAL
: STATE_SPLIT_VERTICAL
);
310 g_object_ref(notebook
);
311 gtk_container_remove(GTK_CONTAINER(parent
), notebook
);
313 pane
= gtk_paned_new(horizontal
? GTK_ORIENTATION_HORIZONTAL
: GTK_ORIENTATION_VERTICAL
);
314 gtk_container_add(GTK_CONTAINER(parent
), pane
);
316 gtk_container_add(GTK_CONTAINER(pane
), notebook
);
317 g_object_unref(notebook
);
319 box
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
320 toolbar
= create_toolbar();
321 gtk_box_pack_start(GTK_BOX(box
), toolbar
, FALSE
, FALSE
, 0);
322 edit_window
.vbox
= box
;
324 /* used just to make the split window look the same as the main editor */
325 splitwin_notebook
= gtk_notebook_new();
326 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(splitwin_notebook
), FALSE
);
327 gtk_notebook_append_page(GTK_NOTEBOOK(splitwin_notebook
), box
, NULL
);
328 gtk_container_add(GTK_CONTAINER(pane
), splitwin_notebook
);
330 set_editor(&edit_window
, doc
->editor
);
334 gtk_paned_set_position(GTK_PANED(pane
), width
);
338 gtk_paned_set_position(GTK_PANED(pane
), height
);
340 gtk_widget_show_all(pane
);
344 static void on_split_horizontally(GtkMenuItem
*menuitem
, gpointer user_data
)
350 static void on_split_vertically(GtkMenuItem
*menuitem
, gpointer user_data
)
356 static void on_unsplit(GtkMenuItem
*menuitem
, gpointer user_data
)
358 GtkWidget
*notebook
= geany_data
->main_widgets
->notebook
;
359 GtkWidget
*pane
= gtk_widget_get_parent(notebook
);
360 GtkWidget
*parent
= gtk_widget_get_parent(pane
);
362 set_state(STATE_UNSPLIT
);
364 g_return_if_fail(edit_window
.editor
);
366 g_object_ref(notebook
);
367 gtk_container_remove(GTK_CONTAINER(pane
), notebook
);
369 gtk_widget_destroy(pane
);
370 edit_window
.editor
= NULL
;
371 edit_window
.sci
= NULL
;
373 gtk_container_add(GTK_CONTAINER(parent
), notebook
);
374 g_object_unref(notebook
);
378 static void kb_activate(guint key_id
)
382 case KB_SPLIT_HORIZONTAL
:
383 if (plugin_state
== STATE_UNSPLIT
)
386 case KB_SPLIT_VERTICAL
:
387 if (plugin_state
== STATE_UNSPLIT
)
390 case KB_SPLIT_UNSPLIT
:
391 if (plugin_state
!= STATE_UNSPLIT
)
392 on_unsplit(NULL
, NULL
);
398 void plugin_init(GeanyData
*data
)
400 GtkWidget
*item
, *menu
;
401 GeanyKeyGroup
*key_group
;
403 menu_items
.main
= item
= gtk_menu_item_new_with_mnemonic(_("_Split Window"));
404 gtk_menu_shell_append(GTK_MENU_SHELL(geany_data
->main_widgets
->tools_menu
), item
);
405 ui_add_document_sensitive(item
);
407 menu
= gtk_menu_new();
408 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items
.main
), menu
);
410 menu_items
.horizontal
= item
=
411 gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
412 g_signal_connect(item
, "activate", G_CALLBACK(on_split_horizontally
), NULL
);
413 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
415 menu_items
.vertical
= item
=
416 gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
417 g_signal_connect(item
, "activate", G_CALLBACK(on_split_vertically
), NULL
);
418 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
420 menu_items
.unsplit
= item
=
421 gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
422 g_signal_connect(item
, "activate", G_CALLBACK(on_unsplit
), NULL
);
423 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
425 gtk_widget_show_all(menu_items
.main
);
427 set_state(STATE_UNSPLIT
);
429 /* setup keybindings */
430 key_group
= plugin_set_key_group(geany_plugin
, "split_window", KB_COUNT
, NULL
);
431 keybindings_set_item(key_group
, KB_SPLIT_HORIZONTAL
, kb_activate
,
432 0, 0, "split_horizontal", _("Side by Side"), menu_items
.horizontal
);
433 keybindings_set_item(key_group
, KB_SPLIT_VERTICAL
, kb_activate
,
434 0, 0, "split_vertical", _("Top and Bottom"), menu_items
.vertical
);
435 keybindings_set_item(key_group
, KB_SPLIT_UNSPLIT
, kb_activate
,
436 0, 0, "split_unsplit", _("_Unsplit"), menu_items
.unsplit
);
440 static gboolean
do_select_current(gpointer data
)
444 /* guard out for the unlikely case we get called after another unsplitting */
445 if (plugin_state
== STATE_UNSPLIT
)
448 doc
= document_get_current();
450 set_editor(&edit_window
, doc
->editor
);
452 on_unsplit(NULL
, NULL
);
458 static void on_document_close(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
460 if (doc
->editor
== edit_window
.editor
)
462 /* select current or unsplit in IDLE time, so the tab has changed */
463 plugin_idle_add(geany_plugin
, do_select_current
, NULL
);
468 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
470 /* update filename */
471 if (doc
->editor
== edit_window
.editor
)
472 gtk_label_set_text(GTK_LABEL(edit_window
.name_label
), DOC_FILENAME(doc
));
476 static void on_document_filetype_set(GObject
*obj
, GeanyDocument
*doc
,
477 GeanyFiletype
*filetype_old
, gpointer user_data
)
480 if (edit_window
.editor
== doc
->editor
)
481 sync_to_current(edit_window
.sci
, doc
->editor
->sci
);
485 PluginCallback plugin_callbacks
[] =
487 { "document-close", (GCallback
) &on_document_close
, FALSE
, NULL
},
488 { "document-save", (GCallback
) &on_document_save
, FALSE
, NULL
},
489 { "document-filetype-set", (GCallback
) &on_document_filetype_set
, FALSE
, NULL
},
490 { NULL
, NULL
, FALSE
, NULL
}
494 void plugin_cleanup(void)
496 if (plugin_state
!= STATE_UNSPLIT
)
497 on_unsplit(NULL
, NULL
);
499 gtk_widget_destroy(menu_items
.main
);