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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/* Eye of Mate - Side bar
*
* Copyright (C) 2004 Red Hat, Inc.
* Copyright (C) 2007 The Free Software Foundation
*
* Author: Lucas Rocha <lucasr@gnome.org>
*
* Based on evince code (shell/ev-sidebar.c) by:
* - Jonathan Blandford <jrb@alum.mit.edu>
*
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "eom-sidebar.h"
enum {
PROP_0,
PROP_CURRENT_PAGE
};
enum {
PAGE_COLUMN_TITLE,
PAGE_COLUMN_MENU_ITEM,
PAGE_COLUMN_MAIN_WIDGET,
PAGE_COLUMN_NOTEBOOK_INDEX,
PAGE_COLUMN_NUM_COLS
};
enum {
SIGNAL_PAGE_ADDED,
SIGNAL_PAGE_REMOVED,
SIGNAL_LAST
};
static gint signals[SIGNAL_LAST];
struct _EomSidebarPrivate {
GtkWidget *notebook;
GtkWidget *select_button;
GtkWidget *menu;
GtkWidget *hbox;
GtkWidget *label;
GtkTreeModel *page_model;
};
G_DEFINE_TYPE (EomSidebar, eom_sidebar, GTK_TYPE_VBOX)
#define EOM_SIDEBAR_GET_PRIVATE(object) \
(G_TYPE_INSTANCE_GET_PRIVATE ((object), EOM_TYPE_SIDEBAR, EomSidebarPrivate))
static void
#if GTK_CHECK_VERSION(3, 0, 0)
eom_sidebar_destroy (GtkWidget *object)
#else
eom_sidebar_destroy (GtkObject *object)
#endif
{
EomSidebar *eom_sidebar = EOM_SIDEBAR (object);
if (eom_sidebar->priv->menu) {
gtk_menu_detach (GTK_MENU (eom_sidebar->priv->menu));
eom_sidebar->priv->menu = NULL;
}
if (eom_sidebar->priv->page_model) {
g_object_unref (eom_sidebar->priv->page_model);
eom_sidebar->priv->page_model = NULL;
}
#if GTK_CHECK_VERSION(3, 0, 0)
(* GTK_WIDGET_CLASS (eom_sidebar_parent_class)->destroy) (object);
#else
(* GTK_OBJECT_CLASS (eom_sidebar_parent_class)->destroy) (object);
#endif
}
static void
eom_sidebar_select_page (EomSidebar *eom_sidebar, GtkTreeIter *iter)
{
gchar *title;
gint index;
gtk_tree_model_get (eom_sidebar->priv->page_model, iter,
PAGE_COLUMN_TITLE, &title,
PAGE_COLUMN_NOTEBOOK_INDEX, &index,
-1);
gtk_notebook_set_current_page (GTK_NOTEBOOK (eom_sidebar->priv->notebook), index);
gtk_label_set_text (GTK_LABEL (eom_sidebar->priv->label), title);
g_free (title);
}
void
eom_sidebar_set_page (EomSidebar *eom_sidebar,
GtkWidget *main_widget)
{
GtkTreeIter iter;
gboolean valid;
valid = gtk_tree_model_get_iter_first (eom_sidebar->priv->page_model, &iter);
while (valid) {
GtkWidget *widget;
gtk_tree_model_get (eom_sidebar->priv->page_model, &iter,
PAGE_COLUMN_MAIN_WIDGET, &widget,
-1);
if (widget == main_widget) {
eom_sidebar_select_page (eom_sidebar, &iter);
valid = FALSE;
} else {
valid = gtk_tree_model_iter_next (eom_sidebar->priv->page_model, &iter);
}
g_object_unref (widget);
}
g_object_notify (G_OBJECT (eom_sidebar), "current-page");
}
static GtkWidget *
eom_sidebar_get_current_page (EomSidebar *sidebar)
{
GtkNotebook *notebook = GTK_NOTEBOOK (sidebar->priv->notebook);
return gtk_notebook_get_nth_page
(notebook, gtk_notebook_get_current_page (notebook));
}
static void
eom_sidebar_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
EomSidebar *sidebar = EOM_SIDEBAR (object);
switch (prop_id) {
case PROP_CURRENT_PAGE:
eom_sidebar_set_page (sidebar, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
eom_sidebar_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EomSidebar *sidebar = EOM_SIDEBAR (object);
switch (prop_id) {
case PROP_CURRENT_PAGE:
g_value_set_object (value, eom_sidebar_get_current_page (sidebar));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
eom_sidebar_class_init (EomSidebarClass *eom_sidebar_class)
{
GObjectClass *g_object_class;
GtkWidgetClass *widget_class;
#if !GTK_CHECK_VERSION(3, 0, 0)
GtkObjectClass *gtk_object_klass;
#endif
g_object_class = G_OBJECT_CLASS (eom_sidebar_class);
widget_class = GTK_WIDGET_CLASS (eom_sidebar_class);
#if !GTK_CHECK_VERSION(3, 0, 0)
gtk_object_klass = GTK_OBJECT_CLASS (eom_sidebar_class);
#endif
g_type_class_add_private (g_object_class, sizeof (EomSidebarPrivate));
#if GTK_CHECK_VERSION(3, 0, 0)
widget_class->destroy = eom_sidebar_destroy;
#else
gtk_object_klass->destroy = eom_sidebar_destroy;
#endif
g_object_class->get_property = eom_sidebar_get_property;
g_object_class->set_property = eom_sidebar_set_property;
g_object_class_install_property (g_object_class,
PROP_CURRENT_PAGE,
g_param_spec_object ("current-page",
"Current page",
"The currently visible page",
GTK_TYPE_WIDGET,
G_PARAM_READWRITE));
signals[SIGNAL_PAGE_ADDED] =
g_signal_new ("page-added",
EOM_TYPE_SIDEBAR,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EomSidebarClass, page_added),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
GTK_TYPE_WIDGET);
signals[SIGNAL_PAGE_REMOVED] =
g_signal_new ("page-removed",
EOM_TYPE_SIDEBAR,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EomSidebarClass, page_removed),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
GTK_TYPE_WIDGET);
}
static void
eom_sidebar_menu_position_under (GtkMenu *menu,
gint *x,
gint *y,
gboolean *push_in,
gpointer user_data)
{
GtkWidget *widget;
GtkAllocation allocation;
g_return_if_fail (GTK_IS_BUTTON (user_data));
g_return_if_fail (!gtk_widget_get_has_window (user_data));
widget = GTK_WIDGET (user_data);
gtk_widget_get_allocation (widget, &allocation);
gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
*x += allocation.x;
*y += allocation.y + allocation.height;
*push_in = FALSE;
}
static gboolean
eom_sidebar_select_button_press_cb (GtkWidget *widget,
GdkEventButton *event,
gpointer user_data)
{
EomSidebar *eom_sidebar = EOM_SIDEBAR (user_data);
if (event->button == 1) {
GtkRequisition requisition;
GtkAllocation allocation;
gtk_widget_get_allocation (widget, &allocation);
gtk_widget_set_size_request (eom_sidebar->priv->menu, -1, -1);
gtk_widget_size_request (eom_sidebar->priv->menu, &requisition);
gtk_widget_set_size_request (eom_sidebar->priv->menu,
MAX (allocation.width,
requisition.width), -1);
gtk_widget_grab_focus (widget);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
gtk_menu_popup (GTK_MENU (eom_sidebar->priv->menu),
NULL, NULL, eom_sidebar_menu_position_under, widget,
event->button, event->time);
return TRUE;
}
return FALSE;
}
static gboolean
eom_sidebar_select_button_key_press_cb (GtkWidget *widget,
GdkEventKey *event,
gpointer user_data)
{
EomSidebar *eom_sidebar = EOM_SIDEBAR (user_data);
if (event->keyval == GDK_KEY_space ||
event->keyval == GDK_KEY_KP_Space ||
event->keyval == GDK_KEY_Return ||
event->keyval == GDK_KEY_KP_Enter) {
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
gtk_menu_popup (GTK_MENU (eom_sidebar->priv->menu),
NULL, NULL, eom_sidebar_menu_position_under, widget,
1, event->time);
return TRUE;
}
return FALSE;
}
static void
eom_sidebar_close_clicked_cb (GtkWidget *widget,
gpointer user_data)
{
EomSidebar *eom_sidebar = EOM_SIDEBAR (user_data);
gtk_widget_hide (GTK_WIDGET (eom_sidebar));
}
static void
eom_sidebar_menu_deactivate_cb (GtkWidget *widget,
gpointer user_data)
{
GtkWidget *menu_button;
menu_button = GTK_WIDGET (user_data);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
}
static void
eom_sidebar_menu_detach_cb (GtkWidget *widget,
GtkMenu *menu)
{
EomSidebar *eom_sidebar = EOM_SIDEBAR (widget);
eom_sidebar->priv->menu = NULL;
}
static void
eom_sidebar_menu_item_activate_cb (GtkWidget *widget,
gpointer user_data)
{
EomSidebar *eom_sidebar = EOM_SIDEBAR (user_data);
GtkTreeIter iter;
GtkWidget *menu_item, *item;
gboolean valid;
menu_item = gtk_menu_get_active (GTK_MENU (eom_sidebar->priv->menu));
valid = gtk_tree_model_get_iter_first (eom_sidebar->priv->page_model, &iter);
while (valid) {
gtk_tree_model_get (eom_sidebar->priv->page_model, &iter,
PAGE_COLUMN_MENU_ITEM, &item,
-1);
if (item == menu_item) {
eom_sidebar_select_page (eom_sidebar, &iter);
valid = FALSE;
} else {
valid = gtk_tree_model_iter_next (eom_sidebar->priv->page_model, &iter);
}
g_object_unref (item);
}
g_object_notify (G_OBJECT (eom_sidebar), "current-page");
}
static void
eom_sidebar_init (EomSidebar *eom_sidebar)
{
GtkWidget *hbox;
GtkWidget *close_button;
GtkWidget *select_hbox;
GtkWidget *arrow;
GtkWidget *image;
eom_sidebar->priv = EOM_SIDEBAR_GET_PRIVATE (eom_sidebar);
/* data model */
eom_sidebar->priv->page_model = (GtkTreeModel *)
gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
G_TYPE_STRING,
GTK_TYPE_WIDGET,
GTK_TYPE_WIDGET,
G_TYPE_INT);
/* top option menu */
hbox = gtk_hbox_new (FALSE, 0);
eom_sidebar->priv->hbox = hbox;
gtk_box_pack_start (GTK_BOX (eom_sidebar), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
eom_sidebar->priv->select_button = gtk_toggle_button_new ();
gtk_button_set_relief (GTK_BUTTON (eom_sidebar->priv->select_button),
GTK_RELIEF_NONE);
g_signal_connect (eom_sidebar->priv->select_button, "button_press_event",
G_CALLBACK (eom_sidebar_select_button_press_cb),
eom_sidebar);
g_signal_connect (eom_sidebar->priv->select_button, "key_press_event",
G_CALLBACK (eom_sidebar_select_button_key_press_cb),
eom_sidebar);
select_hbox = gtk_hbox_new (FALSE, 0);
eom_sidebar->priv->label = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (select_hbox),
eom_sidebar->priv->label,
FALSE, FALSE, 0);
gtk_widget_show (eom_sidebar->priv->label);
arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
gtk_box_pack_end (GTK_BOX (select_hbox), arrow, FALSE, FALSE, 0);
gtk_widget_show (arrow);
gtk_container_add (GTK_CONTAINER (eom_sidebar->priv->select_button), select_hbox);
gtk_widget_show (select_hbox);
gtk_box_pack_start (GTK_BOX (hbox), eom_sidebar->priv->select_button, TRUE, TRUE, 0);
gtk_widget_show (eom_sidebar->priv->select_button);
close_button = gtk_button_new ();
gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
g_signal_connect (close_button, "clicked",
G_CALLBACK (eom_sidebar_close_clicked_cb),
eom_sidebar);
image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,
GTK_ICON_SIZE_MENU);
gtk_container_add (GTK_CONTAINER (close_button), image);
gtk_widget_show (image);
gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
gtk_widget_show (close_button);
eom_sidebar->priv->menu = gtk_menu_new ();
g_signal_connect (eom_sidebar->priv->menu, "deactivate",
G_CALLBACK (eom_sidebar_menu_deactivate_cb),
eom_sidebar->priv->select_button);
gtk_menu_attach_to_widget (GTK_MENU (eom_sidebar->priv->menu),
GTK_WIDGET (eom_sidebar),
eom_sidebar_menu_detach_cb);
gtk_widget_show (eom_sidebar->priv->menu);
eom_sidebar->priv->notebook = gtk_notebook_new ();
gtk_notebook_set_show_border (GTK_NOTEBOOK (eom_sidebar->priv->notebook), FALSE);
gtk_notebook_set_show_tabs (GTK_NOTEBOOK (eom_sidebar->priv->notebook), FALSE);
gtk_box_pack_start (GTK_BOX (eom_sidebar), eom_sidebar->priv->notebook,
TRUE, TRUE, 0);
gtk_widget_show (eom_sidebar->priv->notebook);
}
GtkWidget *
eom_sidebar_new (void)
{
GtkWidget *eom_sidebar;
eom_sidebar = g_object_new (EOM_TYPE_SIDEBAR, NULL);
return eom_sidebar;
}
void
eom_sidebar_add_page (EomSidebar *eom_sidebar,
const gchar *title,
GtkWidget *main_widget)
{
GtkTreeIter iter;
GtkWidget *menu_item;
gchar *label_title;
gint index;
g_return_if_fail (EOM_IS_SIDEBAR (eom_sidebar));
g_return_if_fail (GTK_IS_WIDGET (main_widget));
index = gtk_notebook_append_page (GTK_NOTEBOOK (eom_sidebar->priv->notebook),
main_widget, NULL);
menu_item = gtk_image_menu_item_new_with_label (title);
g_signal_connect (menu_item, "activate",
G_CALLBACK (eom_sidebar_menu_item_activate_cb),
eom_sidebar);
gtk_widget_show (menu_item);
gtk_menu_shell_append (GTK_MENU_SHELL (eom_sidebar->priv->menu),
menu_item);
/* Insert and move to end */
gtk_list_store_insert_with_values (GTK_LIST_STORE (eom_sidebar->priv->page_model),
&iter, 0,
PAGE_COLUMN_TITLE, title,
PAGE_COLUMN_MENU_ITEM, menu_item,
PAGE_COLUMN_MAIN_WIDGET, main_widget,
PAGE_COLUMN_NOTEBOOK_INDEX, index,
-1);
gtk_list_store_move_before (GTK_LIST_STORE(eom_sidebar->priv->page_model),
&iter,
NULL);
/* Set the first item added as active */
gtk_tree_model_get_iter_first (eom_sidebar->priv->page_model, &iter);
gtk_tree_model_get (eom_sidebar->priv->page_model,
&iter,
PAGE_COLUMN_TITLE, &label_title,
PAGE_COLUMN_NOTEBOOK_INDEX, &index,
-1);
gtk_menu_set_active (GTK_MENU (eom_sidebar->priv->menu), index);
gtk_label_set_text (GTK_LABEL (eom_sidebar->priv->label), label_title);
gtk_notebook_set_current_page (GTK_NOTEBOOK (eom_sidebar->priv->notebook),
index);
g_free (label_title);
g_signal_emit (G_OBJECT (eom_sidebar),
signals[SIGNAL_PAGE_ADDED], 0, main_widget);
}
void
eom_sidebar_remove_page (EomSidebar *eom_sidebar, GtkWidget *main_widget)
{
GtkTreeIter iter;
GtkWidget *widget, *menu_item;
gboolean valid;
gint index;
g_return_if_fail (EOM_IS_SIDEBAR (eom_sidebar));
g_return_if_fail (GTK_IS_WIDGET (main_widget));
valid = gtk_tree_model_get_iter_first (eom_sidebar->priv->page_model, &iter);
while (valid) {
gtk_tree_model_get (eom_sidebar->priv->page_model, &iter,
PAGE_COLUMN_NOTEBOOK_INDEX, &index,
PAGE_COLUMN_MENU_ITEM, &menu_item,
PAGE_COLUMN_MAIN_WIDGET, &widget,
-1);
if (widget == main_widget) {
break;
} else {
valid = gtk_tree_model_iter_next (eom_sidebar->priv->page_model,
&iter);
}
g_object_unref (menu_item);
g_object_unref (widget);
}
if (valid) {
gtk_notebook_remove_page (GTK_NOTEBOOK (eom_sidebar->priv->notebook),
index);
gtk_container_remove (GTK_CONTAINER (eom_sidebar->priv->menu), menu_item);
gtk_list_store_remove (GTK_LIST_STORE (eom_sidebar->priv->page_model),
&iter);
g_signal_emit (G_OBJECT (eom_sidebar),
signals[SIGNAL_PAGE_REMOVED], 0, main_widget);
}
}
gint
eom_sidebar_get_n_pages (EomSidebar *eom_sidebar)
{
g_return_val_if_fail (EOM_IS_SIDEBAR (eom_sidebar), TRUE);
return gtk_tree_model_iter_n_children (
GTK_TREE_MODEL (eom_sidebar->priv->page_model), NULL);
}
gboolean
eom_sidebar_is_empty (EomSidebar *eom_sidebar)
{
g_return_val_if_fail (EOM_IS_SIDEBAR (eom_sidebar), TRUE);
return gtk_tree_model_iter_n_children (
GTK_TREE_MODEL (eom_sidebar->priv->page_model), NULL) == 0;
}
|