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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
/* controls.c
*
* Copyright (C) 2002 Jasper Huijsmans (huysmans@users.sourceforge.net)
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* Controls
* --------
* Controls provide a unified interface for panel items. Each control has an
* associated control class that defines interface functions for the control.
*
* A control is created using the create_control function of its associated
* class. This takes an allocated control structure as argument, containing
* only a base container widget.
*
* On startup a list of control classes is made from which new controls can
* be created.
*
* Plugins
* -------
* A control class can also be provided by a plugin. The plugin is accessed
* using the g_module interface.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libxfce4util/i18n.h>
#include <libxfce4util/debug.h>
#include "xfce.h"
#include "item.h"
#include "popup.h"
#include "groups.h"
#include "plugins.h"
#include "controls_dialog.h"
#include "settings.h"
static GSList *control_class_list = NULL;
/* Control classes
* ---------------
*/
static gint
compare_classes (gconstpointer class_a, gconstpointer class_b)
{
g_assert (class_a != NULL);
g_assert (class_b != NULL);
return (g_ascii_strcasecmp (CONTROL_CLASS (class_a)->name,
CONTROL_CLASS (class_b)->name));
}
static gint
lookup_classes_by_filename (gconstpointer class, gconstpointer filename)
{
char *fn;
int result;
g_assert (class != NULL);
g_assert (filename != NULL);
if (CONTROL_CLASS (class)->gmodule)
fn = g_path_get_basename (g_module_name
(CONTROL_CLASS (class)->gmodule));
else
return -1;
result = g_ascii_strcasecmp (fn, filename);
g_free (fn);
return result;
}
/* free a class */
static void
control_class_free (ControlClass * cc)
{
if (cc->id == PLUGIN)
{
g_module_close (cc->gmodule);
g_free (cc->filename);
}
g_free (cc);
}
/* Plugins
* -------
*/
#define SOEXT ("." G_MODULE_SUFFIX)
#define SOEXT_LEN (strlen (SOEXT))
#define API_VERSION 5
gchar *
xfce_plugin_check_version (gint version)
{
if (version != API_VERSION)
return "Incompatible plugin version";
else
return NULL;
}
static void
load_plugin (gchar * path)
{
gpointer tmp;
ControlClass *cc;
GModule *gm;
void (*init) (ControlClass * cc);
cc = g_new0 (ControlClass, 1);
cc->id = PLUGIN;
cc->gmodule = gm = g_module_open (path, 0);
if (gm && g_module_symbol (gm, "xfce_control_class_init", &tmp))
{
init = tmp;
init (cc);
if (g_slist_find_custom (control_class_list, cc, compare_classes))
{
g_message ("%s: module %s has already been loaded",
PACKAGE, cc->name);
control_class_free (cc);
}
else
{
g_message ("%s: module %s successfully loaded", PACKAGE,
cc->name);
control_class_list = g_slist_append (control_class_list, cc);
cc->filename = g_path_get_basename (path);
}
}
else if (gm)
{
g_warning ("%s: incompatible module %s", PACKAGE, path);
g_module_close (gm);
g_free (cc);
}
else
{
g_warning ("%s: module %s cannot be opened (%s)",
PACKAGE, path, g_module_error ());
g_free (cc);
}
}
static void
load_plugin_dir (const char *dir)
{
GDir *gdir = g_dir_open (dir, 0, NULL);
const char *file;
if (!gdir)
return;
while ((file = g_dir_read_name (gdir)))
{
const char *s = file;
s += strlen (file) - SOEXT_LEN;
if (strequal (s, SOEXT))
{
char *path = g_build_filename (dir, file, NULL);
load_plugin (path);
g_free (path);
}
}
g_dir_close (gdir);
}
/* Control class list
* ------------------
*/
static void
add_plugin_classes (void)
{
char **dirs, **d;
dirs = get_plugin_dirs ();
for (d = dirs; *d; d++)
load_plugin_dir (*d);
g_strfreev (dirs);
}
static void
add_launcher_class (void)
{
ControlClass *cc = g_new0 (ControlClass, 1);
panel_item_class_init (cc);
control_class_list = g_slist_append (control_class_list, cc);
}
void
control_class_list_init (void)
{
add_launcher_class ();
add_plugin_classes ();
}
void
control_class_list_cleanup (void)
{
GSList *li;
for (li = control_class_list; li; li = li->next)
{
ControlClass *cc = li->data;
control_class_free (cc);
}
g_slist_free (control_class_list);
control_class_list = NULL;
}
GSList *
get_control_class_list (void)
{
return control_class_list;
}
/* Control right click menu
* ------------------------
*/
static Control *popup_control = NULL;
static void
edit_control (void)
{
if (popup_control)
controls_dialog (popup_control);
popup_control = NULL;
}
static void
remove_control (void)
{
if (popup_control)
{
PanelPopup *pp;
pp = groups_get_popup (popup_control->index);
if (!(popup_control->with_popup) || pp->items == NULL ||
confirm (_("Removing an item will also remove its popup menu.\n\n"
"Do you want to remove the item?"),
GTK_STOCK_REMOVE, NULL))
{
groups_remove (popup_control->index);
}
}
popup_control = NULL;
}
static void
add_control (gpointer data, int n, GtkWidget * w)
{
gboolean hidden = settings.autohide;
ControlClass *cc;
Control *control;
int index;
if (hidden)
{
DBG ("unhide before adding new item");
panel_set_autohide (FALSE);
while (gtk_events_pending ())
gtk_main_iteration ();
}
cc = g_slist_nth (control_class_list, n - 1)->data;
index = popup_control ? popup_control->index : -1;
groups_add_control (cc->id, cc->filename, index);
control = groups_get_control (index >= 0 ? index :
settings.num_groups - 1);
controls_dialog (control);
write_panel_config ();
popup_control = NULL;
if (hidden)
panel_set_autohide (TRUE);
}
static GtkItemFactoryEntry control_items[] = {
{N_("/Add _new item"), NULL, NULL, 0, "<Branch>"},
{"/sep", NULL, NULL, 0, "<Separator>"},
{N_("/_Properties..."), NULL, edit_control, 0, "<Item>"},
{N_("/_Remove"), NULL, remove_control, 0, "<Item>"},
};
static const char *
translate_menu (const char *msg)
{
#if ENABLE_NLS
/* ensure we use the panel domain and not that of a plugin */
return dgettext (GETTEXT_PACKAGE, msg);
#else
return msg;
#endif
}
void
free_controls_menu_entries (GtkItemFactoryEntry * entries, int n)
{
int i;
for (i = 0; i < n; i++)
{
g_free ((entries + i)->path);
}
g_free (entries);
}
int
get_controls_menu_entries (GtkItemFactoryEntry ** entries, const char *base)
{
GSList *li;
int i, n;
GtkItemFactoryEntry *entry;
n = g_slist_length (control_class_list);
*entries = g_new0 (GtkItemFactoryEntry, n);
for (entry = *entries, li = control_class_list, i = 1;
li; li = li->next, entry++, i++)
{
ControlClass *cc = li->data;
entry->path = g_strconcat (base, "/", cc->caption, NULL);
entry->callback = add_control;
entry->callback_action = i;
entry->item_type = "<Item>";
}
return n;
}
static GtkWidget *
get_control_menu (void)
{
static GtkItemFactory *factory;
static GtkWidget *menu = NULL;
if (!menu)
{
GtkItemFactoryEntry *entries;
int n_entries;
factory = gtk_item_factory_new (GTK_TYPE_MENU, "<popup>", NULL);
gtk_item_factory_set_translate_func (factory,
(GtkTranslateFunc)
translate_menu, NULL, NULL);
gtk_item_factory_create_items (factory, G_N_ELEMENTS (control_items),
control_items, NULL);
n_entries = get_controls_menu_entries (&entries, "/Add new item");
gtk_item_factory_create_items (factory, n_entries, entries, NULL);
/* free_controls_menu_entries(entries, n_entries);*/
menu = gtk_item_factory_get_widget (factory, "<popup>");
}
return menu;
}
static gboolean
control_press_cb (GtkWidget * b, GdkEventButton * ev, Control * control)
{
if (disable_user_config)
return FALSE;
if (ev->button == 3 || (ev->button == 1 && (ev->state & GDK_SHIFT_MASK)))
{
GtkWidget *menu;
hide_current_popup_menu ();
gtk_widget_grab_focus (b);
popup_control = control;
menu = get_control_menu ();
gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
ev->button, ev->time);
return TRUE;
}
else
{
return FALSE;
}
}
/* Controls creation and destruction
* ---------------------------------
*/
static gboolean
create_plugin (Control * control, const char *filename)
{
GSList *li = NULL;
ControlClass *cc;
li = g_slist_find_custom (control_class_list, filename,
lookup_classes_by_filename);
if (!li)
return FALSE;
cc = control->cclass = li->data;
return cc->create_control (control);
}
static void
create_launcher (Control * control)
{
/* we know it is the first item in the list */
control->cclass = control_class_list->data;
control->cclass->create_control (control);
}
void
create_control (Control * control, int id, const char *filename)
{
ControlClass *cc;
switch (id)
{
case PLUGIN:
if (!create_plugin (control, filename))
create_launcher (control);
break;
default:
create_launcher (control);
}
/* the control class is set in the create_* functions above */
cc = control->cclass;
/* these are required for proper operation */
if (!gtk_bin_get_child (GTK_BIN (control->base)))
{
if (cc && cc->free)
cc->free (control);
create_launcher (control);
}
control_attach_callbacks (control);
control_set_settings (control);
}
Control *
control_new (int index)
{
Control *control = g_new0 (Control, 1);
control->index = index;
control->with_popup = TRUE;
control->base = gtk_event_box_new ();
gtk_widget_show (control->base);
/* protect against destruction when unpacking */
g_object_ref (control->base);
gtk_object_sink (GTK_OBJECT (control->base));
return control;
}
void
control_free (Control * control)
{
ControlClass *cc = control->cclass;
if (cc && cc->free)
cc->free (control);
gtk_widget_destroy (control->base);
g_object_unref (control->base);
g_free (control);
}
void
control_pack (Control * control, GtkBox * box)
{
gtk_box_pack_start (box, control->base, TRUE, TRUE, 0);
}
void
control_unpack (Control * control)
{
gtk_container_remove (GTK_CONTAINER (control->base->parent),
control->base);
}
void
control_attach_callbacks (Control * control)
{
ControlClass *cc = control->cclass;
cc->attach_callback (control, "button-press-event",
G_CALLBACK (control_press_cb), control);
g_signal_connect (control->base, "button-press-event",
G_CALLBACK (control_press_cb), control);
}
/* Controls configuration
* ----------------------
*/
/* module configuration */
static void
control_read_config (Control * control, xmlNodePtr node)
{
ControlClass *cc = control->cclass;
if (cc && cc->read_config)
cc->read_config (control, node);
}
static void
control_write_config (Control * control, xmlNodePtr node)
{
ControlClass *cc = control->cclass;
if (cc && cc->write_config)
cc->write_config (control, node);
}
/* control configuration */
void
control_set_from_xml (Control * control, xmlNodePtr node)
{
xmlChar *value;
int id = ICON;
char *filename = NULL;
if (!node)
{
create_control (control, ICON, NULL);
return;
}
/* get id and filename */
value = xmlGetProp (node, (const xmlChar *) "id");
if (value)
{
id = atoi (value);
g_free (value);
}
if (id == PLUGIN)
filename = (char *) xmlGetProp (node, (const xmlChar *) "filename");
/* create a default control of specified type */
create_control (control, id, filename);
g_free (filename);
/* allow the control to read its configuration */
control_read_config (control, node);
/* hide popup? also check if added to the panel */
if (!control->with_popup && control->base->parent)
groups_show_popup (control->index, FALSE);
}
void
control_write_xml (Control * control, xmlNodePtr parent)
{
xmlNodePtr node;
char value[4];
ControlClass *cc = control->cclass;
node = xmlNewTextChild (parent, NULL, "Control", NULL);
snprintf (value, 3, "%d", cc->id);
xmlSetProp (node, "id", value);
if (cc->filename)
xmlSetProp (node, "filename", cc->filename);
/* allow the control to write its configuration */
control_write_config (control, node);
}
/* options dialog */
void
control_create_options (Control * control, GtkContainer * container,
GtkWidget * done)
{
ControlClass *cc = control->cclass;
if (cc && cc->create_options)
{
cc->create_options (control, container, done);
}
else
{
GtkWidget *hbox, *image, *label;
hbox = gtk_hbox_new (FALSE, 6);
gtk_widget_show (hbox);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
image =
gtk_image_new_from_stock (GTK_STOCK_DIALOG_INFO,
GTK_ICON_SIZE_LARGE_TOOLBAR);
gtk_widget_show (image);
gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
label = gtk_label_new (_("This item has no configuration options"));
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
gtk_widget_show (label);
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
gtk_container_add (container, hbox);
}
}
/* Controls global preferences
* ---------------------------
*/
void
control_set_settings (Control * control)
{
control_set_orientation (control, settings.orientation);
control_set_size (control, settings.size);
control_set_theme (control, settings.theme);
}
void
control_set_orientation (Control * control, int orientation)
{
ControlClass *cc = control->cclass;
if (cc && cc->set_orientation)
cc->set_orientation (control, orientation);
}
void
control_set_size (Control * control, int size)
{
ControlClass *cc = control->cclass;
if (cc && cc->set_size)
cc->set_size (control, size);
else
{
int s = icon_size[size] + border_width;
gtk_widget_set_size_request (control->base, s, s);
}
}
void
control_set_theme (Control * control, const char *theme)
{
ControlClass *cc = control->cclass;
if (cc && cc->set_theme)
cc->set_theme (control, theme);
}
|