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
|
/*
* Copyright (C) 2003 Marco Pesenti Gritti
*
* 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "egg-toolbar-editor.h"
#include "egg-editable-toolbar.h"
#include "eggintl.h"
#include <string.h>
#include <libxml/tree.h>
static GtkTargetEntry dest_drag_types[] = {
{EGG_TOOLBAR_ITEM_TYPE, 0, 0},
};
static int n_dest_drag_types = G_N_ELEMENTS (dest_drag_types);
static GtkTargetEntry source_drag_types[] = {
{EGG_TOOLBAR_ITEM_TYPE, 0, 0},
};
static int n_source_drag_types = G_N_ELEMENTS (source_drag_types);
static void egg_toolbar_editor_class_init (EggToolbarEditorClass *klass);
static void egg_toolbar_editor_init (EggToolbarEditor *t);
static void egg_toolbar_editor_finalize (GObject *object);
static void update_editor_sheet (EggToolbarEditor *editor);
enum
{
RESPONSE_ADD_TOOLBAR
};
enum
{
PROP_0,
PROP_MENU_MERGE,
PROP_TOOLBARS_MODEL
};
static GObjectClass *parent_class = NULL;
struct EggToolbarEditorPrivate
{
EggMenuMerge *merge;
EggToolbarsModel *model;
GtkWidget *table;
GtkWidget *scrolled_window;
GList *actions_list;
};
GType
egg_toolbar_editor_get_type (void)
{
static GType egg_toolbar_editor_type = 0;
if (egg_toolbar_editor_type == 0)
{
static const GTypeInfo our_info = {
sizeof (EggToolbarEditorClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) egg_toolbar_editor_class_init,
NULL,
NULL, /* class_data */
sizeof (EggToolbarEditor),
0, /* n_preallocs */
(GInstanceInitFunc) egg_toolbar_editor_init
};
egg_toolbar_editor_type = g_type_register_static (GTK_TYPE_DIALOG,
"EggToolbarEditor",
&our_info, 0);
}
return egg_toolbar_editor_type;
}
static EggAction *
find_action (EggToolbarEditor *t,
const char *name)
{
GList *l = t->priv->merge->action_groups;
EggAction *action = NULL;
g_return_val_if_fail (IS_EGG_TOOLBAR_EDITOR (t), NULL);
g_return_val_if_fail (name != NULL, NULL);
for (; l != NULL; l = l->next)
{
EggAction *tmp;
tmp = egg_action_group_get_action (EGG_ACTION_GROUP (l->data), name);
if (tmp)
action = tmp;
}
return action;
}
static void
egg_toolbar_editor_set_merge (EggToolbarEditor *t,
EggMenuMerge *merge)
{
g_return_if_fail (EGG_IS_MENU_MERGE (merge));
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (t));
t->priv->merge = merge;
}
static void
egg_toolbar_editor_set_model (EggToolbarEditor *t,
EggToolbarsModel *model)
{
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (t));
t->priv->model = model;
}
static void
egg_toolbar_editor_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object);
switch (prop_id)
{
case PROP_MENU_MERGE:
egg_toolbar_editor_set_merge (t, g_value_get_object (value));
break;
case PROP_TOOLBARS_MODEL:
egg_toolbar_editor_set_model (t, g_value_get_object (value));
break;
}
}
static void
egg_toolbar_editor_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object);
switch (prop_id)
{
case PROP_MENU_MERGE:
g_value_set_object (value, t->priv->merge);
break;
case PROP_TOOLBARS_MODEL:
g_value_set_object (value, t->priv->model);
break;
}
}
static void
egg_toolbar_editor_class_init (EggToolbarEditorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = egg_toolbar_editor_finalize;
object_class->set_property = egg_toolbar_editor_set_property;
object_class->get_property = egg_toolbar_editor_get_property;
g_object_class_install_property (object_class,
PROP_MENU_MERGE,
g_param_spec_object ("MenuMerge",
"MenuMerge",
"Menu merge",
EGG_TYPE_MENU_MERGE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_TOOLBARS_MODEL,
g_param_spec_object ("ToolbarsModel",
"ToolbarsModel",
"Toolbars Model",
EGG_TOOLBARS_MODEL_TYPE,
G_PARAM_READWRITE));
}
static void
egg_toolbar_editor_finalize (GObject *object)
{
EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object);
g_return_if_fail (object != NULL);
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (object));
g_free (t->priv);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GtkWidget *
egg_toolbar_editor_new (EggMenuMerge *merge,
EggToolbarsModel *model)
{
EggToolbarEditor *t;
t = EGG_TOOLBAR_EDITOR (g_object_new (EGG_TOOLBAR_EDITOR_TYPE,
"MenuMerge", merge,
"ToolbarsModel", model,
NULL));
g_return_val_if_fail (t->priv != NULL, NULL);
return GTK_WIDGET (t);
}
static void
editor_drag_data_received_cb (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time_,
EggToolbarEditor *editor)
{
EggAction *action;
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor));
g_return_if_fail (selection_data != NULL);
action = find_action (editor, (const char *)selection_data->data);
g_return_if_fail (action != NULL);
editor->priv->actions_list = g_list_append
(editor->priv->actions_list, action);
update_editor_sheet (editor);
}
static void
editor_drag_data_delete_cb (GtkWidget *widget,
GdkDragContext *context,
EggToolbarEditor *editor)
{
EggAction *action;
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor));
action = EGG_ACTION (g_object_get_data (G_OBJECT (widget), "egg-action"));
if (action)
{
editor->priv->actions_list = g_list_remove
(editor->priv->actions_list, action);
}
update_editor_sheet (editor);
}
static void
drag_data_get_cb (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint32 time,
EggToolbarEditor *editor)
{
EggAction *action;
const char *target;
action = EGG_ACTION (g_object_get_data (G_OBJECT (widget), "egg-action"));
if (action)
{
target = action->name;
}
else
{
target = "separator";
}
gtk_selection_data_set (selection_data,
selection_data->target, 8, target, strlen (target));
}
static void
editor_close (EggToolbarEditor *editor)
{
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor));
gtk_widget_destroy (GTK_WIDGET (editor));
}
static void
editor_add_toolbar (EggToolbarEditor *editor)
{
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor));
egg_toolbars_model_add_toolbar (editor->priv->model, "UserCreated");
}
static void
dialog_response_cb (GtkDialog *dialog,
gint response_id,
EggToolbarEditor *editor)
{
switch (response_id)
{
case RESPONSE_ADD_TOOLBAR:
editor_add_toolbar (editor);
break;
case GTK_RESPONSE_CLOSE:
editor_close (editor);
break;
}
}
static gchar *
elide_underscores (const gchar *original)
{
gchar *q, *result;
const gchar *p;
gboolean last_underscore;
q = result = g_malloc (strlen (original) + 1);
last_underscore = FALSE;
for (p = original; *p; p++)
{
if (!last_underscore && *p == '_')
last_underscore = TRUE;
else
{
last_underscore = FALSE;
*q++ = *p;
}
}
*q = '\0';
return result;
}
static GtkWidget *
editor_create_item (EggToolbarEditor *editor,
const char *stock_id,
const char *label_text,
GdkDragAction action)
{
GtkWidget *event_box;
GtkWidget *vbox;
GtkWidget *icon;
GtkWidget *label;
gchar *label_no_mnemonic = NULL;
event_box = gtk_event_box_new ();
gtk_widget_show (event_box);
gtk_drag_source_set (event_box,
GDK_BUTTON1_MASK,
source_drag_types, n_source_drag_types, action);
g_signal_connect (event_box, "drag_data_get",
G_CALLBACK (drag_data_get_cb), editor);
g_signal_connect (event_box, "drag_data_delete",
G_CALLBACK (editor_drag_data_delete_cb), editor);
vbox = gtk_vbox_new (0, FALSE);
gtk_widget_show (vbox);
gtk_container_add (GTK_CONTAINER (event_box), vbox);
icon = gtk_image_new_from_stock
(stock_id ? stock_id : GTK_STOCK_DND,
GTK_ICON_SIZE_LARGE_TOOLBAR);
gtk_widget_show (icon);
gtk_box_pack_start (GTK_BOX (vbox), icon, FALSE, TRUE, 0);
label_no_mnemonic = elide_underscores (label_text);
label = gtk_label_new (label_no_mnemonic);
g_free (label_no_mnemonic);
gtk_widget_show (label);
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
return event_box;
}
static void
update_editor_sheet (EggToolbarEditor *editor)
{
GList *l;
GList *to_drag = editor->priv->actions_list;
int x, y, height, width;
GtkWidget *table;
GtkWidget *viewport;
GtkWidget *item;
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor));
viewport = GTK_BIN (editor->priv->scrolled_window)->child;
if (viewport)
{
table = GTK_BIN (viewport)->child;
gtk_container_remove (GTK_CONTAINER (viewport), table);
}
table = gtk_table_new (0, 0, TRUE);
editor->priv->table = table;
gtk_container_set_border_width (GTK_CONTAINER (table), 12);
gtk_widget_show (table);
gtk_scrolled_window_add_with_viewport
(GTK_SCROLLED_WINDOW (editor->priv->scrolled_window), table);
gtk_drag_dest_set (table, GTK_DEST_DEFAULT_ALL,
dest_drag_types, n_dest_drag_types, GDK_ACTION_MOVE);
g_signal_connect (table, "drag_data_received",
G_CALLBACK (editor_drag_data_received_cb), editor);
x = y = 0;
width = 4;
height = (g_list_length (to_drag) - 1) / width + 1;
gtk_table_resize (GTK_TABLE (editor->priv->table), height, width);
for (l = to_drag; l != NULL; l = l->next)
{
EggAction *action = (l->data);
item = editor_create_item (editor, action->stock_id,
action->short_label, GDK_ACTION_MOVE);
g_object_set_data (G_OBJECT (item), "egg-action", action);
gtk_table_attach_defaults (GTK_TABLE (editor->priv->table),
item, x, x + 1, y, y + 1);
x++;
if (x >= width)
{
x = 0;
y++;
}
}
item = editor_create_item (editor, NULL, _("Separator"),
GDK_ACTION_COPY);
gtk_table_attach_defaults (GTK_TABLE (editor->priv->table),
item, x, x + 1, y, y + 1);
}
static void
setup_editor (EggToolbarEditor *editor)
{
GtkWidget *scrolled_window;
GtkWidget *vbox;
GtkWidget *label_hbox;
GtkWidget *image;
GtkWidget *label;
g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor));
gtk_dialog_set_has_separator (GTK_DIALOG (editor), FALSE);
gtk_window_set_title (GTK_WINDOW (editor), "Toolbar editor");
vbox = gtk_vbox_new (FALSE, 12);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_widget_show (vbox);
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (editor)->vbox), vbox);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
editor->priv->scrolled_window = scrolled_window;
gtk_widget_show (scrolled_window);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
label_hbox = gtk_hbox_new (FALSE, 6);
gtk_widget_show (label_hbox);
gtk_box_pack_start (GTK_BOX (vbox), label_hbox, FALSE, FALSE, 0);
image =
gtk_image_new_from_stock (GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
gtk_widget_show (image);
gtk_box_pack_start (GTK_BOX (label_hbox), image, FALSE, FALSE, 0);
label = gtk_label_new (_("Drag an item onto the toolbars above to add it, "
"from the toolbars in the items table to remove it."));
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_widget_show (label);
gtk_box_pack_start (GTK_BOX (label_hbox), label, FALSE, TRUE, 0);
gtk_dialog_add_button (GTK_DIALOG (editor),
_("_Add a New Toolbar"), RESPONSE_ADD_TOOLBAR);
gtk_dialog_add_button (GTK_DIALOG (editor),
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
g_signal_connect (editor, "response",
G_CALLBACK (dialog_response_cb), editor);
}
static void
egg_toolbar_editor_init (EggToolbarEditor *t)
{
t->priv = g_new0 (EggToolbarEditorPrivate, 1);
t->priv->merge = NULL;
t->priv->actions_list = NULL;
setup_editor (t);
}
static void
egg_toolbar_editor_add_action (EggToolbarEditor *editor,
const char *action_name)
{
EggAction *action;
action = find_action (editor, action_name);
g_return_if_fail (action != NULL);
editor->priv->actions_list = g_list_append
(editor->priv->actions_list, action);
}
static void
parse_item_list (EggToolbarEditor *t,
xmlNodePtr child)
{
while (child)
{
if (xmlStrEqual (child->name, "toolitem"))
{
xmlChar *verb;
verb = xmlGetProp (child, "verb");
egg_toolbar_editor_add_action (t, verb);
xmlFree (verb);
}
child = child->next;
}
}
gboolean
model_has_action (EggToolbarsModel *model, EggAction *action)
{
int i, l, n_items, n_toolbars;
n_toolbars = egg_toolbars_model_n_toolbars (model);
for (i = 0; i < n_toolbars; i++)
{
n_items = egg_toolbars_model_n_items (model, i);
for (l = 0; l < n_items; l++)
{
const char *name;
gboolean sep;
name = egg_toolbars_model_item_nth (model, i, l, &sep);
if (!sep && strcmp (name, action->name) == 0) return TRUE;
}
}
return FALSE;
}
void
egg_toolbar_editor_load_actions (EggToolbarEditor *editor,
const char *xml_file)
{
xmlDocPtr doc;
xmlNodePtr root;
xmlNodePtr child;
GList *l, *tmp;
doc = xmlParseFile (xml_file);
root = xmlDocGetRootElement (doc);
child = root->children;
while (child)
{
if (xmlStrEqual (child->name, "available"))
{
parse_item_list (editor, child->children);
}
child = child->next;
}
xmlFreeDoc (doc);
/* Remove the already used items */
tmp = g_list_copy (editor->priv->actions_list);
for (l = editor->priv->actions_list; l != NULL; l = l->next)
{
EggAction *action = EGG_ACTION (l->data);
if (model_has_action (editor->priv->model, action))
{
tmp = g_list_remove (tmp, action);
}
}
g_list_free (editor->priv->actions_list);
editor->priv->actions_list = tmp;
update_editor_sheet (editor);
}
|