Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[35cc44]: plugins / splitwindow.c Maximize Restore History

Download this file

splitwindow.c    498 lines (384 with data), 13.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* splitwindow.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2008-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* Copyright 2008-2012 Enrico Trรถger <enrico(dot)troeger(at)uvena(dot)de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Split Window plugin. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "geanyplugin.h"
#include "gtkcompat.h"
#include <string.h>
PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
PLUGIN_SET_INFO(_("Split Window"), _("Splits the editor view into two windows."),
VERSION, _("The Geany developer team"))
GeanyData *geany_data;
GeanyFunctions *geany_functions;
GeanyPlugin *geany_plugin;
/* Keybinding(s) */
enum
{
KB_SPLIT_HORIZONTAL,
KB_SPLIT_VERTICAL,
KB_SPLIT_UNSPLIT,
KB_COUNT
};
enum State
{
STATE_SPLIT_HORIZONTAL,
STATE_SPLIT_VERTICAL,
STATE_UNSPLIT,
STATE_COUNT
};
static struct
{
GtkWidget *main;
GtkWidget *horizontal;
GtkWidget *vertical;
GtkWidget *unsplit;
}
menu_items;
static enum State plugin_state;
typedef struct EditWindow
{
GeanyEditor *editor; /* original editor for split view */
ScintillaObject *sci; /* new editor widget */
GtkWidget *vbox;
GtkWidget *name_label;
}
EditWindow;
static EditWindow edit_window = {NULL, NULL, NULL, NULL};
static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data);
/* line numbers visibility */
static void set_line_numbers(ScintillaObject * sci, gboolean set)
{
if (set)
{
gchar tmp_str[15];
gint len = scintilla_send_message(sci, SCI_GETLINECOUNT, 0, 0);
gint width;
g_snprintf(tmp_str, 15, "_%d", len);
width = scintilla_send_message(sci, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t) tmp_str);
scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, width);
scintilla_send_message(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
}
else
{
scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 0, 0);
}
}
static void on_sci_notify(ScintillaObject *sci, gint param,
SCNotification *nt, gpointer data)
{
gint line;
switch (nt->nmhdr.code)
{
/* adapted from editor.c: on_margin_click() */
case SCN_MARGINCLICK:
/* left click to marker margin toggles marker */
if (nt->margin == 1)
{
gboolean set;
gint marker = 1;
line = sci_get_line_from_position(sci, nt->position);
set = sci_is_marker_set_at_line(sci, line, marker);
if (!set)
sci_set_marker_at_line(sci, line, marker);
else
sci_delete_marker_at_line(sci, line, marker);
}
/* left click on the folding margin to toggle folding state of current line */
if (nt->margin == 2)
{
line = sci_get_line_from_position(sci, nt->position);
scintilla_send_message(sci, SCI_TOGGLEFOLD, line, 0);
}
break;
default: break;
}
}
static void sync_to_current(ScintillaObject *sci, ScintillaObject *current)
{
gpointer sdoc;
gint pos;
/* set the new sci widget to view the existing Scintilla document */
sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0);
scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc);
highlighting_set_styles(sci, edit_window.editor->document->file_type);
pos = sci_get_current_position(current);
sci_set_current_position(sci, pos, TRUE);
/* override some defaults */
set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin);
/* marker margin */
scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1,
scintilla_send_message(current, SCI_GETMARGINWIDTHN, 1, 0));
if (!geany->editor_prefs->folding)
scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 2, 0);
}
static void set_editor(EditWindow *editwin, GeanyEditor *editor)
{
editwin->editor = editor;
/* first destroy any widget, otherwise its signals will have an
* invalid document as user_data */
if (editwin->sci != NULL)
gtk_widget_destroy(GTK_WIDGET(editwin->sci));
editwin->sci = editor_create_widget(editor);
gtk_widget_show(GTK_WIDGET(editwin->sci));
gtk_box_pack_start(GTK_BOX(editwin->vbox), GTK_WIDGET(editwin->sci), TRUE, TRUE, 0);
sync_to_current(editwin->sci, editor->sci);
scintilla_send_message(editwin->sci, SCI_USEPOPUP, 1, 0);
/* for margin events */
g_signal_connect(editwin->sci, "sci-notify",
G_CALLBACK(on_sci_notify), NULL);
gtk_label_set_text(GTK_LABEL(editwin->name_label), DOC_FILENAME(editor->document));
}
static void set_state(enum State id)
{
gtk_widget_set_sensitive(menu_items.horizontal,
(id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
gtk_widget_set_sensitive(menu_items.vertical,
(id != STATE_SPLIT_HORIZONTAL) && (id != STATE_SPLIT_VERTICAL));
gtk_widget_set_sensitive(menu_items.unsplit,
id != STATE_UNSPLIT);
plugin_state = id;
}
/* Create a GtkToolButton with stock icon, label and tooltip.
* @param label can be NULL to use stock label text. @a label can contain underscores,
* which will be removed.
* @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
static GtkWidget *ui_tool_button_new(const gchar *stock_id, const gchar *label, const gchar *tooltip)
{
GtkToolItem *item;
gchar *dupl = NULL;
if (stock_id && !label)
{
label = ui_lookup_stock_label(stock_id);
}
dupl = utils_str_remove_chars(g_strdup(label), "_");
label = dupl;
item = gtk_tool_button_new(NULL, label);
if (stock_id)
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item), stock_id);
if (!tooltip)
tooltip = label;
if (tooltip)
gtk_widget_set_tooltip_text(GTK_WIDGET(item), tooltip);
g_free(dupl);
return GTK_WIDGET(item);
}
static void on_refresh(void)
{
GeanyDocument *doc = document_get_current();
g_return_if_fail(doc);
g_return_if_fail(edit_window.sci);
set_editor(&edit_window, doc->editor);
}
static void on_doc_menu_item_clicked(gpointer item, GeanyDocument *doc)
{
if (doc->is_valid)
set_editor(&edit_window, doc->editor);
}
static void on_doc_menu_show(GtkMenu *menu)
{
/* clear the old menu items */
gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) gtk_widget_destroy, NULL);
ui_menu_add_document_items(menu, edit_window.editor->document,
G_CALLBACK(on_doc_menu_item_clicked));
}
static GtkWidget *create_toolbar(void)
{
GtkWidget *toolbar, *item;
GtkToolItem *tool_item;
toolbar = gtk_toolbar_new();
gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
tool_item = gtk_menu_tool_button_new(NULL, NULL);
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(tool_item), GTK_STOCK_JUMP_TO);
item = (GtkWidget*)tool_item;
gtk_widget_set_tooltip_text(item, _("Show the current document"));
gtk_container_add(GTK_CONTAINER(toolbar), item);
g_signal_connect(item, "clicked", G_CALLBACK(on_refresh), NULL);
item = gtk_menu_new();
gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(tool_item), item);
g_signal_connect(item, "show", G_CALLBACK(on_doc_menu_show), NULL);
tool_item = gtk_tool_item_new();
gtk_tool_item_set_expand(tool_item, TRUE);
gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(tool_item));
item = gtk_label_new(NULL);
gtk_label_set_ellipsize(GTK_LABEL(item), PANGO_ELLIPSIZE_START);
gtk_container_add(GTK_CONTAINER(tool_item), item);
edit_window.name_label = item;
item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
gtk_container_add(GTK_CONTAINER(toolbar), item);
g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
return toolbar;
}
static void split_view(gboolean horizontal)
{
GtkWidget *notebook = geany_data->main_widgets->notebook;
GtkWidget *parent = gtk_widget_get_parent(notebook);
GtkWidget *pane, *toolbar, *box;
GeanyDocument *doc = document_get_current();
gint width = gtk_widget_get_allocated_width(notebook) / 2;
gint height = gtk_widget_get_allocated_height(notebook) / 2;
g_return_if_fail(doc);
g_return_if_fail(edit_window.editor == NULL);
set_state(horizontal ? STATE_SPLIT_HORIZONTAL : STATE_SPLIT_VERTICAL);
g_object_ref(notebook);
gtk_container_remove(GTK_CONTAINER(parent), notebook);
pane = horizontal ? gtk_hpaned_new() : gtk_vpaned_new();
gtk_container_add(GTK_CONTAINER(parent), pane);
gtk_container_add(GTK_CONTAINER(pane), notebook);
g_object_unref(notebook);
box = gtk_vbox_new(FALSE, 0);
toolbar = create_toolbar();
gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(pane), box);
edit_window.vbox = box;
set_editor(&edit_window, doc->editor);
if (horizontal)
{
gtk_paned_set_position(GTK_PANED(pane), width);
}
else
{
gtk_paned_set_position(GTK_PANED(pane), height);
}
gtk_widget_show_all(pane);
}
static void on_split_horizontally(GtkMenuItem *menuitem, gpointer user_data)
{
split_view(TRUE);
}
static void on_split_vertically(GtkMenuItem *menuitem, gpointer user_data)
{
split_view(FALSE);
}
static void on_unsplit(GtkMenuItem *menuitem, gpointer user_data)
{
GtkWidget *notebook = geany_data->main_widgets->notebook;
GtkWidget *pane = gtk_widget_get_parent(notebook);
GtkWidget *parent = gtk_widget_get_parent(pane);
set_state(STATE_UNSPLIT);
g_return_if_fail(edit_window.editor);
g_object_ref(notebook);
gtk_container_remove(GTK_CONTAINER(pane), notebook);
gtk_widget_destroy(pane);
edit_window.editor = NULL;
edit_window.sci = NULL;
gtk_container_add(GTK_CONTAINER(parent), notebook);
g_object_unref(notebook);
}
static void kb_activate(guint key_id)
{
switch (key_id)
{
case KB_SPLIT_HORIZONTAL:
if (plugin_state == STATE_UNSPLIT)
split_view(TRUE);
break;
case KB_SPLIT_VERTICAL:
if (plugin_state == STATE_UNSPLIT)
split_view(FALSE);
break;
case KB_SPLIT_UNSPLIT:
if (plugin_state != STATE_UNSPLIT)
on_unsplit(NULL, NULL);
break;
}
}
void plugin_init(GeanyData *data)
{
GtkWidget *item, *menu;
GeanyKeyGroup *key_group;
menu_items.main = item = gtk_menu_item_new_with_mnemonic(_("_Split Window"));
gtk_menu_shell_append(GTK_MENU_SHELL(geany_data->main_widgets->tools_menu), item);
ui_add_document_sensitive(item);
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_items.main), menu);
menu_items.horizontal = item =
gtk_menu_item_new_with_mnemonic(_("_Side by Side"));
g_signal_connect(item, "activate", G_CALLBACK(on_split_horizontally), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
menu_items.vertical = item =
gtk_menu_item_new_with_mnemonic(_("_Top and Bottom"));
g_signal_connect(item, "activate", G_CALLBACK(on_split_vertically), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
menu_items.unsplit = item =
gtk_menu_item_new_with_mnemonic(_("_Unsplit"));
g_signal_connect(item, "activate", G_CALLBACK(on_unsplit), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
gtk_widget_show_all(menu_items.main);
set_state(STATE_UNSPLIT);
/* setup keybindings */
key_group = plugin_set_key_group(geany_plugin, "split_window", KB_COUNT, NULL);
keybindings_set_item(key_group, KB_SPLIT_HORIZONTAL, kb_activate,
0, 0, "split_horizontal", _("Split Horizontally"), menu_items.horizontal);
keybindings_set_item(key_group, KB_SPLIT_VERTICAL, kb_activate,
0, 0, "split_vertical", _("Split Vertically"), menu_items.vertical);
keybindings_set_item(key_group, KB_SPLIT_UNSPLIT, kb_activate,
0, 0, "split_unsplit", _("_Unsplit"), menu_items.unsplit);
}
static gboolean do_select_current(gpointer data)
{
GeanyDocument *doc;
/* guard out for the unlikely case we get called after another unsplitting */
if (plugin_state == STATE_UNSPLIT)
return FALSE;
doc = document_get_current();
if (doc)
set_editor(&edit_window, doc->editor);
else
on_unsplit(NULL, NULL);
return FALSE;
}
static void on_document_close(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
if (doc->editor == edit_window.editor)
{
/* select current or unsplit in IDLE time, so the tab has changed */
plugin_idle_add(geany_plugin, do_select_current, NULL);
}
}
static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
/* update filename */
if (doc->editor == edit_window.editor)
gtk_label_set_text(GTK_LABEL(edit_window.name_label), DOC_FILENAME(doc));
}
static void on_document_filetype_set(GObject *obj, GeanyDocument *doc,
GeanyFiletype *filetype_old, gpointer user_data)
{
/* update styles */
if (edit_window.editor == doc->editor)
sync_to_current(edit_window.sci, doc->editor->sci);
}
PluginCallback plugin_callbacks[] =
{
{ "document-close", (GCallback) &on_document_close, FALSE, NULL },
{ "document-save", (GCallback) &on_document_save, FALSE, NULL },
{ "document-filetype-set", (GCallback) &on_document_filetype_set, FALSE, NULL },
{ NULL, NULL, FALSE, NULL }
};
void plugin_cleanup(void)
{
if (plugin_state != STATE_UNSPLIT)
on_unsplit(NULL, NULL);
gtk_widget_destroy(menu_items.main);
}