~ubuntu-branches/ubuntu/trusty/xfce4-panel/trusty-proposed

« back to all changes in this revision

Viewing changes to plugins/systembuttons/systembuttons.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Huggins
  • Date: 2004-06-08 10:44:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040608104421-b0b77kis8o0uoi6q
Tags: upstream-4.0.5
ImportĀ upstreamĀ versionĀ 4.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  xfce4
 
2
 *
 
3
 *  Copyright (C) 2002 Jasper Huijsmans(huysmans@users.sourceforge.net)
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
/*  systembuttons.c : xfce4 panel plugin that shows one or two 
 
21
 *  'system buttons'. These are the same buttons also present on 
 
22
 *  both sides of the switcher plugin: 
 
23
 *  lock, exit, setup and info.
 
24
 */
 
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include <config.h>
 
28
#endif
 
29
 
 
30
#include <libxfce4util/i18n.h>
 
31
#include <libxfcegui4/xfce_iconbutton.h>
 
32
 
 
33
#include <panel/mcs_client.h>
 
34
#include <panel/plugins.h>
 
35
#include <panel/xfce.h>
 
36
 
 
37
#if 0
 
38
#include "mcs_client.h"
 
39
#endif
 
40
 
 
41
/* callbacks */
 
42
static void
 
43
mini_lock_cb (void)
 
44
{
 
45
    exec_cmd ("xflock4", FALSE, FALSE);
 
46
}
 
47
 
 
48
static void
 
49
mini_info_cb (void)
 
50
{
 
51
    exec_cmd ("xfce4-about", FALSE, FALSE);
 
52
}
 
53
 
 
54
static void
 
55
mini_palet_cb (void)
 
56
{
 
57
    if (disable_user_config)
 
58
    {
 
59
        xfce_info (_
 
60
                   ("Access to the configuration system has been disabled.\n\n"
 
61
                    "Ask your system administrator for more information"));
 
62
        return;
 
63
    }
 
64
 
 
65
    mcs_dialog ("all");
 
66
}
 
67
 
 
68
static void
 
69
mini_power_cb (GtkButton * b, GdkEventButton * ev, gpointer data)
 
70
{
 
71
    quit (FALSE);
 
72
}
 
73
 
 
74
/*  typedefs */
 
75
typedef struct
 
76
{
 
77
    const char *signal;
 
78
    GCallback callback;
 
79
    gpointer data;
 
80
}
 
81
SignalCallback;
 
82
 
 
83
enum
 
84
{ LOCK, EXIT, SETUP, INFO };
 
85
 
 
86
typedef struct
 
87
{
 
88
    gboolean show_two;
 
89
    int button_types[2];        /* 0, 1, 2 or 3 for lock, exit, setup and info */
 
90
 
 
91
    GtkWidget *box;
 
92
    GtkWidget *buttons[2];
 
93
#if 0
 
94
    GtkWidget *buttonbox;
 
95
    GtkWidget *align[2];        /* containers for the actual buttons */
 
96
#endif
 
97
    GList *callbacks;           /* save callbacks for when we change buttons */
 
98
    int cb_ids[2];
 
99
}
 
100
t_systembuttons;
 
101
 
 
102
typedef struct
 
103
{
 
104
    Control *control;
 
105
    t_systembuttons *sb;
 
106
 
 
107
    GtkWidget *two_checkbutton;
 
108
    GtkWidget *option_menu_hbox[2];
 
109
    GtkWidget *option_menus[2];
 
110
}
 
111
t_systembuttons_dialog;
 
112
 
 
113
/* change buttons */
 
114
static void
 
115
button_update_image (GtkWidget * button, int type)
 
116
{
 
117
    GdkPixbuf *pb = NULL;
 
118
 
 
119
    switch (type)
 
120
    {
 
121
        case LOCK:
 
122
            pb = get_minibutton_pixbuf (MINILOCK_ICON);
 
123
            break;
 
124
        case EXIT:
 
125
            pb = get_minibutton_pixbuf (MINIPOWER_ICON);
 
126
            break;
 
127
        case SETUP:
 
128
            pb = get_minibutton_pixbuf (MINIPALET_ICON);
 
129
            break;
 
130
        case INFO:
 
131
            pb = get_minibutton_pixbuf (MINIINFO_ICON);
 
132
            break;
 
133
    }
 
134
 
 
135
    xfce_iconbutton_set_pixbuf (XFCE_ICONBUTTON (button), pb);
 
136
 
 
137
    g_object_unref (pb);
 
138
}
 
139
 
 
140
static void
 
141
button_set_tip (GtkWidget * button, int type)
 
142
{
 
143
    static const char *tips[4];
 
144
    static gboolean need_init = TRUE;
 
145
 
 
146
    if (need_init)
 
147
    {
 
148
        tips[0] = _("Lock the screen");
 
149
        tips[1] = _("Exit");
 
150
        tips[2] = _("Setup");
 
151
        tips[3] = _("Info");
 
152
    }
 
153
 
 
154
    add_tooltip (button, tips[type]);
 
155
}
 
156
 
 
157
/* creation and destruction */
 
158
static int
 
159
connect_callback(GtkWidget *button, int type)
 
160
{
 
161
    GCallback callback = NULL;
 
162
    
 
163
    switch (type)
 
164
    {
 
165
        case LOCK:
 
166
            callback = G_CALLBACK (mini_lock_cb);
 
167
            break;
 
168
        case EXIT:
 
169
            callback = G_CALLBACK (mini_power_cb);
 
170
            break;
 
171
        case SETUP:
 
172
            callback = G_CALLBACK (mini_palet_cb);
 
173
            break;
 
174
        case INFO:
 
175
            callback = G_CALLBACK (mini_info_cb);
 
176
            break;
 
177
    }
 
178
  
 
179
    return g_signal_connect (button, "clicked", callback, NULL);
 
180
}
 
181
 
 
182
static void
 
183
create_systembutton (t_systembuttons *sb, int n, int type)
 
184
{
 
185
    GtkWidget *button;
 
186
 
 
187
    button = xfce_iconbutton_new ();
 
188
    gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
 
189
    gtk_widget_show (button);
 
190
 
 
191
    button_set_tip (button, type);
 
192
    button_update_image (button, type);
 
193
 
 
194
    sb->cb_ids[n] = connect_callback(button, type);
 
195
    sb->buttons[n] = button;
 
196
}
 
197
 
 
198
static void
 
199
systembuttons_change_type (t_systembuttons * sb, int n, int type)
 
200
{
 
201
    GtkWidget *button;
 
202
 
 
203
    sb->button_types[n] = type;
 
204
 
 
205
    button = sb->buttons[n];
 
206
 
 
207
    g_signal_handler_disconnect(sb->buttons[n], sb->cb_ids[n]);
 
208
 
 
209
    sb->cb_ids[n] = connect_callback(button, type);
 
210
    button_set_tip(button, type);
 
211
    button_update_image(button, type);
 
212
}
 
213
 
 
214
static void
 
215
arrange_systembuttons (t_systembuttons * sb, int orientation)
 
216
{
 
217
 
 
218
    if (sb->box)
 
219
    {
 
220
        gtk_container_remove (GTK_CONTAINER (sb->box), sb->buttons[0]);
 
221
        gtk_container_remove (GTK_CONTAINER (sb->box), sb->buttons[1]);
 
222
 
 
223
        gtk_widget_destroy (sb->box);
 
224
    }
 
225
 
 
226
    /* we need some extra spacing when the panel size <= SMALL
 
227
     * hence the strange code below :) */
 
228
    if (orientation == HORIZONTAL)
 
229
    {
 
230
        if (settings.size > SMALL)
 
231
            sb->box = gtk_vbox_new (TRUE, 0);
 
232
        else
 
233
            sb->box = gtk_hbox_new (TRUE, 0);
 
234
    }
 
235
    else
 
236
    {
 
237
        if (settings.size > SMALL)
 
238
            sb->box = gtk_hbox_new (TRUE, 0);
 
239
        else
 
240
            sb->box = gtk_hbox_new (TRUE, 0);
 
241
    }
 
242
 
 
243
    gtk_widget_show (sb->box);
 
244
 
 
245
    gtk_widget_show(sb->buttons[0]);
 
246
 
 
247
    if (sb->show_two)
 
248
        gtk_widget_show(sb->buttons[1]);
 
249
    else
 
250
        gtk_widget_hide(sb->buttons[1]);
 
251
    
 
252
    gtk_box_pack_start(GTK_BOX(sb->box), sb->buttons[0], TRUE, TRUE, 0);
 
253
    gtk_box_pack_start(GTK_BOX(sb->box), sb->buttons[1], TRUE, TRUE, 0);
 
254
}
 
255
 
 
256
static t_systembuttons *
 
257
systembuttons_new (void)
 
258
{
 
259
    t_systembuttons *sb = g_new0 (t_systembuttons, 1);
 
260
 
 
261
    sb->show_two = TRUE;
 
262
 
 
263
    sb->button_types[0] = 0;
 
264
    create_systembutton (sb,0,0);
 
265
 
 
266
    sb->button_types[1] = 1;
 
267
    create_systembutton (sb,1,1);
 
268
 
 
269
    g_object_ref (sb->buttons[0]);
 
270
    g_object_ref (sb->buttons[1]);
 
271
 
 
272
    arrange_systembuttons (sb, settings.orientation);
 
273
 
 
274
    return sb;
 
275
}
 
276
 
 
277
static void
 
278
systembuttons_free (Control * control)
 
279
{
 
280
    t_systembuttons *sb = control->data;
 
281
 
 
282
    g_list_foreach (sb->callbacks, (GFunc) g_free, NULL);
 
283
    g_list_free (sb->callbacks);
 
284
 
 
285
    g_free (sb);
 
286
}
 
287
 
 
288
static void
 
289
systembuttons_attach_callback (Control * control, const char *signal,
 
290
                               GCallback callback, gpointer data)
 
291
{
 
292
    t_systembuttons *sb = (t_systembuttons *) control->data;
 
293
    SignalCallback *cb;
 
294
 
 
295
    cb = g_new0 (SignalCallback, 1);
 
296
    cb->signal = signal;
 
297
    cb->callback = callback;
 
298
    cb->data = data;
 
299
 
 
300
    sb->callbacks = g_list_append (sb->callbacks, cb);
 
301
 
 
302
    g_signal_connect (sb->buttons[0], signal, callback, data);
 
303
    g_signal_connect (sb->buttons[1], signal, callback, data);
 
304
}
 
305
 
 
306
/* global prefs */
 
307
static void
 
308
systembuttons_set_size (Control * control, int size)
 
309
{
 
310
    int s1, s2, n;
 
311
    t_systembuttons *sb = control->data;
 
312
 
 
313
    arrange_systembuttons (sb, settings.orientation);
 
314
    gtk_container_add (GTK_CONTAINER (control->base), sb->box);
 
315
 
 
316
    n = sb->show_two ? 2 : 1;
 
317
    s1 = icon_size[size] + border_width;
 
318
    s2 = s1 * 0.75;
 
319
 
 
320
    if (settings.orientation == VERTICAL)
 
321
    {
 
322
        if (size > SMALL)
 
323
            gtk_widget_set_size_request (sb->box, s1, s2);
 
324
        else
 
325
            gtk_widget_set_size_request (sb->box, s1, s1 * n * 0.75);
 
326
    }
 
327
    else
 
328
    {
 
329
        if (size > SMALL)
 
330
            gtk_widget_set_size_request (sb->box, s2, s1);
 
331
        else
 
332
            gtk_widget_set_size_request (sb->box, s1 * n * 0.75, s1);
 
333
    }
 
334
}
 
335
 
 
336
static void
 
337
systembuttons_set_orientation (Control * control, int orientation)
 
338
{
 
339
    t_systembuttons *sb = control->data;
 
340
 
 
341
    arrange_systembuttons (sb, orientation);
 
342
    gtk_container_add (GTK_CONTAINER (control->base), sb->box);
 
343
}
 
344
 
 
345
static void
 
346
systembuttons_set_theme (Control * control, const char *theme)
 
347
{
 
348
    t_systembuttons *sb;
 
349
 
 
350
    sb = control->data;
 
351
 
 
352
    button_update_image (sb->buttons[0], sb->button_types[0]);
 
353
    button_update_image (sb->buttons[1], sb->button_types[1]);
 
354
}
 
355
 
 
356
/* settings */
 
357
static void
 
358
systembuttons_read_config (Control * control, xmlNodePtr node)
 
359
{
 
360
    xmlChar *value;
 
361
    int n;
 
362
    t_systembuttons *sb = (t_systembuttons *) control->data;
 
363
 
 
364
    value = xmlGetProp (node, (const xmlChar *) "button1");
 
365
 
 
366
    if (value)
 
367
    {
 
368
        n = atoi (value);
 
369
        g_free (value);
 
370
 
 
371
        if (n != sb->button_types[0])
 
372
            systembuttons_change_type (sb, 0, n);
 
373
    }
 
374
 
 
375
    value = xmlGetProp (node, (const xmlChar *) "button2");
 
376
 
 
377
    if (value)
 
378
    {
 
379
        n = atoi (value);
 
380
        g_free (value);
 
381
 
 
382
        if (n != sb->button_types[1])
 
383
            systembuttons_change_type (sb, 1, n);
 
384
    }
 
385
 
 
386
    value = xmlGetProp (node, (const xmlChar *) "showtwo");
 
387
 
 
388
    if (value)
 
389
    {
 
390
        n = atoi (value);
 
391
        g_free (value);
 
392
 
 
393
        if (n == 0)
 
394
        {
 
395
            sb->show_two = FALSE;
 
396
            gtk_widget_hide (sb->buttons[1]);
 
397
        }
 
398
        else
 
399
        {
 
400
            sb->show_two = TRUE;
 
401
            gtk_widget_show (sb->buttons[1]);
 
402
        }
 
403
    }
 
404
 
 
405
    /* Try to resize the systembuttons to fit the user settings */
 
406
    systembuttons_set_size (control, settings.size);    
 
407
}
 
408
 
 
409
static void
 
410
systembuttons_write_config (Control * control, xmlNodePtr node)
 
411
{
 
412
    char prop[3];
 
413
    t_systembuttons *sb = control->data;
 
414
 
 
415
    sprintf (prop, "%d", sb->button_types[0]);
 
416
 
 
417
    xmlSetProp (node, (const xmlChar *) "button1", prop);
 
418
 
 
419
    sprintf (prop, "%d", sb->button_types[1]);
 
420
 
 
421
    xmlSetProp (node, (const xmlChar *) "button2", prop);
 
422
 
 
423
    sprintf (prop, "%d", sb->show_two ? 1 : 0);
 
424
 
 
425
    xmlSetProp (node, (const xmlChar *) "showtwo", prop);
 
426
}
 
427
 
 
428
/* options dialog */
 
429
static void
 
430
checkbutton_changed (GtkToggleButton * tb, t_systembuttons_dialog * sbd)
 
431
{
 
432
    gboolean show_two;
 
433
 
 
434
    show_two = gtk_toggle_button_get_active (tb);
 
435
 
 
436
    if (show_two != sbd->sb->show_two)
 
437
    {
 
438
        sbd->sb->show_two = show_two;
 
439
 
 
440
        if (show_two)
 
441
        {
 
442
            gtk_widget_show (sbd->sb->buttons[1]);
 
443
            gtk_widget_show (sbd->option_menu_hbox[1]);
 
444
        }
 
445
        else
 
446
        {
 
447
            gtk_widget_hide (sbd->sb->buttons[1]);
 
448
            gtk_widget_hide (sbd->option_menu_hbox[1]);
 
449
        }
 
450
 
 
451
        systembuttons_set_size (sbd->control, settings.size);
 
452
    }
 
453
}
 
454
 
 
455
static void
 
456
buttons_changed (GtkOptionMenu * om, t_systembuttons_dialog * sbd)
 
457
{
 
458
    int i;
 
459
    int n = gtk_option_menu_get_history (om);
 
460
 
 
461
    i = (GTK_WIDGET (om) == sbd->option_menus[0]) ? 0 : 1;
 
462
 
 
463
    if (n == sbd->sb->button_types[i])
 
464
        return;
 
465
 
 
466
    systembuttons_change_type (sbd->sb, i, n);
 
467
}
 
468
 
 
469
void
 
470
systembuttons_create_options (Control * control, GtkContainer * container,
 
471
                           GtkWidget * done)
 
472
{
 
473
    GtkWidget *vbox, *hbox, *label, *om = NULL;
 
474
    const char *names[4];
 
475
    int i, j;
 
476
    t_systembuttons *sb;
 
477
    t_systembuttons_dialog *sbd;
 
478
    GtkSizeGroup *sg1, *sg2;
 
479
 
 
480
    sg1 = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
 
481
    sg2 = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
 
482
 
 
483
    /* initialize dialog structure */
 
484
    sb = control->data;
 
485
    sbd = g_new0 (t_systembuttons_dialog, 1);
 
486
 
 
487
    sbd->control = control;
 
488
    sbd->sb = sb;
 
489
 
 
490
    /* add widgets */
 
491
    vbox = gtk_vbox_new (FALSE, 8);
 
492
    gtk_widget_show (vbox);
 
493
    gtk_container_add (container, vbox);
 
494
 
 
495
    /* show two checkbutton */
 
496
    hbox = gtk_hbox_new (FALSE, 4);
 
497
    gtk_widget_show (hbox);
 
498
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
 
499
 
 
500
    label = gtk_label_new (_("Show two buttons:"));
 
501
    gtk_widget_show (label);
 
502
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
 
503
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
504
    gtk_size_group_add_widget (sg1, label);
 
505
 
 
506
    sbd->two_checkbutton = gtk_check_button_new ();
 
507
    gtk_widget_show (sbd->two_checkbutton);
 
508
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (sbd->two_checkbutton),
 
509
                                  sbd->sb->show_two);
 
510
    gtk_box_pack_start (GTK_BOX (hbox), sbd->two_checkbutton, FALSE, FALSE,
 
511
                        0);
 
512
 
 
513
    g_signal_connect (sbd->two_checkbutton, "toggled",
 
514
                      G_CALLBACK (checkbutton_changed), sbd);
 
515
 
 
516
    /* set names to use in option menus */
 
517
    names[0] = _("Lock");
 
518
    names[1] = _("Exit");
 
519
    names[2] = _("Setup");
 
520
    names[3] = _("Info");
 
521
 
 
522
    /* two identical option menus */
 
523
    for (i = 0; i < 2; i++)
 
524
    {
 
525
        GtkWidget *menu;
 
526
        GtkWidget *mi;
 
527
 
 
528
        om = sbd->option_menus[i] = gtk_option_menu_new ();
 
529
        menu = gtk_menu_new ();
 
530
        gtk_option_menu_set_menu (GTK_OPTION_MENU (om), menu);
 
531
 
 
532
        for (j = 0; j < 4; j++)
 
533
        {
 
534
            mi = gtk_menu_item_new_with_label (names[j]);
 
535
            gtk_widget_show (mi);
 
536
            gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
 
537
        }
 
538
 
 
539
        gtk_option_menu_set_history (GTK_OPTION_MENU (om),
 
540
                                     sb->button_types[i]);
 
541
        g_signal_connect (om, "changed", G_CALLBACK (buttons_changed), sbd);
 
542
    }
 
543
 
 
544
    /* buttons option menus */
 
545
    hbox = sbd->option_menu_hbox[0] = gtk_hbox_new (FALSE, 4);
 
546
    gtk_widget_show (hbox);
 
547
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
 
548
 
 
549
    label = gtk_label_new (_("Button 1:"));
 
550
    gtk_widget_show (label);
 
551
    gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
 
552
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
553
    gtk_size_group_add_widget (sg1, label);
 
554
 
 
555
    om = sbd->option_menus[0];
 
556
    gtk_widget_show (om);
 
557
    gtk_box_pack_start (GTK_BOX (hbox), om, FALSE, FALSE, 0);
 
558
    gtk_size_group_add_widget (sg2, om);
 
559
 
 
560
    hbox = sbd->option_menu_hbox[1] = gtk_hbox_new (FALSE, 4);
 
561
    if (sb->show_two)
 
562
        gtk_widget_show (hbox);
 
563
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
 
564
 
 
565
    label = gtk_label_new (_("Button 2:"));
 
566
    gtk_widget_show (label);
 
567
    gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
 
568
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
569
    gtk_size_group_add_widget (sg1, label);
 
570
 
 
571
    om = sbd->option_menus[1];
 
572
    gtk_widget_show (om);
 
573
    gtk_box_pack_start (GTK_BOX (hbox), om, FALSE, FALSE, 0);
 
574
    gtk_size_group_add_widget (sg2, om);
 
575
 
 
576
    /* dialog buttons */
 
577
    g_signal_connect_swapped (done, "clicked", G_CALLBACK (g_free), sbd);
 
578
}
 
579
 
 
580
/* panel control */
 
581
gboolean
 
582
create_systembuttons_control (Control * control)
 
583
{
 
584
    t_systembuttons *sb = systembuttons_new ();
 
585
 
 
586
    gtk_container_add (GTK_CONTAINER (control->base), sb->box);
 
587
 
 
588
    control->data = (gpointer) sb;
 
589
    control->with_popup = FALSE;
 
590
 
 
591
    systembuttons_set_size (control, settings.size);
 
592
 
 
593
    return TRUE;
 
594
}
 
595
 
 
596
G_MODULE_EXPORT void
 
597
xfce_control_class_init (ControlClass * cc)
 
598
{
 
599
    cc->name = "systembuttons";
 
600
    cc->caption = _("System buttons");
 
601
 
 
602
    cc->create_control = (CreateControlFunc) create_systembuttons_control;
 
603
 
 
604
    cc->free = systembuttons_free;
 
605
    cc->read_config = systembuttons_read_config;
 
606
    cc->write_config = systembuttons_write_config;
 
607
    cc->attach_callback = systembuttons_attach_callback;
 
608
 
 
609
    cc->set_size = systembuttons_set_size;
 
610
    cc->set_orientation = systembuttons_set_orientation;
 
611
    cc->set_theme = systembuttons_set_theme;
 
612
    cc->create_options = systembuttons_create_options;
 
613
}
 
614
 
 
615
XFCE_PLUGIN_CHECK_INIT