summaryrefslogtreecommitdiff
path: root/applets/notification_area/na-tray.c
blob: 9db9703b07ae08b4eb9ec05c34840a890862eb78 (plain)
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*
 * Copyright (C) 2002 Red Hat, Inc.
 * Copyright (C) 2003-2006 Vincent Untz
 * Copyright (C) 2007 Christian Persch
 *
 * 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.
 */

#include <config.h>
#include <string.h>

#include <mate-panel-applet.h>

#include <gtk/gtk.h>

#include "na-tray-manager.h"
#include "fixedtip.h"

#include "na-tray.h"

#define ICON_SPACING 1
#define MIN_BOX_SIZE 3

#if GTK_CHECK_VERSION (3, 0, 0)
#define gtk_vbox_new(X, Y) gtk_box_new(GTK_ORIENTATION_VERTICAL, Y)
#define gtk_hbox_new(X, Y) gtk_box_new(GTK_ORIENTATION_HORIZONTAL, Y)
#endif

typedef struct
{
  NaTrayManager *tray_manager;
  GSList        *all_trays;
  GHashTable    *icon_table;
  GHashTable    *tip_table;
} TraysScreen;

struct _NaTrayPrivate
{
  GdkScreen   *screen;
  TraysScreen *trays_screen;

  GtkWidget *box;
  GtkWidget *frame;

  guint idle_redraw_id;

  GtkOrientation orientation;
};

typedef struct
{
  char  *text;
  glong  id;
  glong  timeout;
} IconTipBuffer;

typedef struct
{
  NaTray *tray;      /* tray containing the tray icon */
  GtkWidget  *icon;      /* tray icon sending the message */
  GtkWidget  *fixedtip;
  guint       source_id;
  glong       id;        /* id of the current message */
  GSList     *buffer;    /* buffered messages */
} IconTip;

enum
{
  PROP_0,
  PROP_ORIENTATION,
  PROP_SCREEN
};

static gboolean     initialized   = FALSE;
static TraysScreen *trays_screens = NULL;

static void icon_tip_show_next (IconTip *icontip);

#if !GTK_CHECK_VERSION (3, 0, 0)
/* NaBox, an instantiable GtkBox */

typedef GtkBox      NaBox;
typedef GtkBoxClass NaBoxClass;

static GType na_box_get_type (void);

G_DEFINE_TYPE (NaBox, na_box, GTK_TYPE_BOX)

static void
na_box_init (NaBox *box)
{
}

static void
na_box_class_init (NaBoxClass *klass)
{
}
#endif

/* NaTray */

G_DEFINE_TYPE (NaTray, na_tray, GTK_TYPE_BIN)

static NaTray *
get_tray (TraysScreen *trays_screen)
{
  if (trays_screen->all_trays == NULL)
    return NULL;
  
  return trays_screen->all_trays->data;
}

const char *ordered_roles[] = {
  "keyboard",
  "volume",
  "bluetooth",
  "network",
  "battery",
  NULL
};

const char *wmclass_roles[] = {
  "Bluetooth-applet", "bluetooth",
  "Mate-volume-control-applet", "volume",
  "Nm-applet", "network",
  "Mate-power-manager", "battery",
  "keyboard", "keyboard",
  NULL,
};

static const char *
find_role (const char *wmclass)
{
  int i;

  for (i = 0; wmclass_roles[i]; i += 2)
    {
      if (strcmp (wmclass, wmclass_roles[i]) == 0)
        return wmclass_roles[i + 1];
    }

  return NULL;
}

static int
find_role_position (const char *role)
{
  int i;

  for (i = 0; ordered_roles[i]; i++)
    {
      if (strcmp (role, ordered_roles[i]) == 0)
        break;
    }

  return i + 1;
}

static int
find_icon_position (NaTray    *tray,
                    GtkWidget *icon)
{
  NaTrayPrivate *priv;
  int            position;
  char          *class_a;
  const char    *role;
  int            role_position;
  GList         *l, *children;

  /* We insert the icons with a known roles in a specific order (the one
   * defined by ordered_roles), and all other icons at the beginning of the box
   * (left in LTR). */

  priv = tray->priv;
  position = 0;

  class_a = NULL;
  na_tray_child_get_wm_class (NA_TRAY_CHILD (icon), NULL, &class_a);
  if (!class_a)
    return position;

  role = find_role (class_a);
  g_free (class_a);
  if (!role)
    return position;

  role_position = find_role_position (role);
  g_object_set_data (G_OBJECT (icon), "role-position", GINT_TO_POINTER (role_position));

  children = gtk_container_get_children (GTK_CONTAINER (priv->box));
  for (l = g_list_last (children); l; l = l->prev)
    {
      GtkWidget *child = l->data;
      int        rp;

      rp = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), "role-position"));
      if (rp == 0 || rp < role_position)
        {
          position = g_list_index (children, child) + 1;
          break;
        }
    }
  g_list_free (children);

  /* should never happen, but it doesn't hurt to be on the safe side */
  if (position < 0)
    position = 0;

  return position;
}

static void
tray_added (NaTrayManager *manager,
            GtkWidget     *icon,
            TraysScreen   *trays_screen)
{
  NaTray *tray;
  NaTrayPrivate *priv;
  int position;

  tray = get_tray (trays_screen);
  if (tray == NULL)
    return;

  priv = tray->priv;

  g_assert (priv->trays_screen == trays_screen);

  g_hash_table_insert (trays_screen->icon_table, icon, tray);

  position = find_icon_position (tray, icon);
  gtk_box_pack_start (GTK_BOX (priv->box), icon, FALSE, FALSE, 0);
  gtk_box_reorder_child (GTK_BOX (priv->box), icon, position);

  gtk_widget_show (icon);
}

static void
tray_removed (NaTrayManager *manager,
              GtkWidget     *icon,
              TraysScreen   *trays_screen)
{
  NaTray *tray;
  NaTrayPrivate *priv;

  tray = g_hash_table_lookup (trays_screen->icon_table, icon);
  if (tray == NULL)
    return;

  priv = tray->priv;

  g_assert (tray->priv->trays_screen == trays_screen);

  gtk_container_remove (GTK_CONTAINER (priv->box), icon);

  g_hash_table_remove (trays_screen->icon_table, icon);
  /* this will also destroy the tip associated to this icon */
  g_hash_table_remove (trays_screen->tip_table, icon);
}

static void
icon_tip_buffer_free (gpointer data,
                      gpointer userdata)
{
  IconTipBuffer *buffer;

  buffer = data;

  g_free (buffer->text);
  buffer->text = NULL;

  g_free (buffer);
}

static void
icon_tip_free (gpointer data)
{
  IconTip *icontip;

  if (data == NULL)
    return;

  icontip = data;

  if (icontip->fixedtip != NULL)
    gtk_widget_destroy (GTK_WIDGET (icontip->fixedtip));
  icontip->fixedtip = NULL;

  if (icontip->source_id != 0)
    g_source_remove (icontip->source_id);
  icontip->source_id = 0;

  if (icontip->buffer != NULL)
    {
      g_slist_foreach (icontip->buffer, icon_tip_buffer_free, NULL);
      g_slist_free (icontip->buffer);
    }
  icontip->buffer = NULL;

  g_free (icontip);
}

static int
icon_tip_buffer_compare (gconstpointer a,
                         gconstpointer b)
{
  const IconTipBuffer *buffer_a = a;
  const IconTipBuffer *buffer_b = b;

  if (buffer_a == NULL || buffer_b == NULL)
    return !(buffer_a == buffer_b);

  return buffer_a->id - buffer_b->id;
}

static void
icon_tip_show_next_clicked (GtkWidget *widget,
                            gpointer   data)
{
  icon_tip_show_next ((IconTip *) data);
}

static gboolean
icon_tip_show_next_timeout (gpointer data)
{
  IconTip *icontip = (IconTip *) data;

  icon_tip_show_next (icontip);

  return FALSE;
}

static void
icon_tip_show_next (IconTip *icontip)
{
  IconTipBuffer *buffer;

  if (icontip->buffer == NULL)
    {
      /* this will also destroy the tip window */
      g_hash_table_remove (icontip->tray->priv->trays_screen->tip_table,
                           icontip->icon);
      return;
    }

  if (icontip->source_id != 0)
    g_source_remove (icontip->source_id);
  icontip->source_id = 0;

  buffer = icontip->buffer->data;
  icontip->buffer = g_slist_remove (icontip->buffer, buffer);

  if (icontip->fixedtip == NULL)
    {
      icontip->fixedtip = na_fixed_tip_new (icontip->icon,
                                            na_tray_get_orientation (icontip->tray));

      g_signal_connect (icontip->fixedtip, "clicked",
                        G_CALLBACK (icon_tip_show_next_clicked), icontip);
    }

  na_fixed_tip_set_markup (icontip->fixedtip, buffer->text);

  if (!gtk_widget_get_mapped (icontip->fixedtip))
    gtk_widget_show (icontip->fixedtip);

  icontip->id = buffer->id;

  if (buffer->timeout > 0)
    icontip->source_id = g_timeout_add_seconds (buffer->timeout,
                                                icon_tip_show_next_timeout,
                                                icontip);

  icon_tip_buffer_free (buffer, NULL);
}

static void
message_sent (NaTrayManager *manager,
              GtkWidget     *icon,
              const char    *text,
              glong          id,
              glong          timeout,
              TraysScreen   *trays_screen)
{
  IconTip       *icontip;
  IconTipBuffer  find_buffer;
  IconTipBuffer *buffer;
  gboolean       show_now;

  icontip = g_hash_table_lookup (trays_screen->tip_table, icon);

  find_buffer.id = id;
  if (icontip && 
      (icontip->id == id ||
       g_slist_find_custom (icontip->buffer, &find_buffer,
                            icon_tip_buffer_compare) != NULL))
    /* we already have this message, so ignore it */
    /* FIXME: in an ideal world, we'd remember all the past ids and ignore them
     * too */
    return;

  show_now = FALSE;

  if (icontip == NULL)
    {
      NaTray *tray;

      tray = g_hash_table_lookup (trays_screen->icon_table, icon);
      if (tray == NULL)
        {
          /* We don't know about the icon sending the message, so ignore it.
           * But this should never happen since NaTrayManager shouldn't send
           * us the message if there's no socket for it. */
          g_critical ("Ignoring a message sent by a tray icon "
                      "we don't know: \"%s\".\n", text);
          return;
        }

      icontip = g_new0 (IconTip, 1);
      icontip->tray = tray;
      icontip->icon = icon;

      g_hash_table_insert (trays_screen->tip_table, icon, icontip);

      show_now = TRUE;
    }

  buffer = g_new0 (IconTipBuffer, 1);

  buffer->text    = g_strdup (text);
  buffer->id      = id;
  buffer->timeout = timeout;

  icontip->buffer = g_slist_append (icontip->buffer, buffer);

  if (show_now)
    icon_tip_show_next (icontip);
}

static void
message_cancelled (NaTrayManager *manager,
                   GtkWidget     *icon,
                   glong          id,
                   TraysScreen   *trays_screen)
{
  IconTip       *icontip;
  IconTipBuffer  find_buffer;
  GSList        *cancel_buffer_l;
  IconTipBuffer *cancel_buffer;

  icontip = g_hash_table_lookup (trays_screen->tip_table, icon);
  if (icontip == NULL)
    return;

  if (icontip->id == id)
    {
      icon_tip_show_next (icontip);
      return;
    }

  find_buffer.id = id;
  cancel_buffer_l = g_slist_find_custom (icontip->buffer, &find_buffer,
                                         icon_tip_buffer_compare);
  if (cancel_buffer_l == NULL)
    return;

  cancel_buffer = cancel_buffer_l->data;
  icon_tip_buffer_free (cancel_buffer, NULL);

  icontip->buffer = g_slist_remove_link (icontip->buffer, cancel_buffer_l);
  g_slist_free_1 (cancel_buffer_l);
}

static void
update_orientation_for_messages (gpointer key,
                                 gpointer value,
                                 gpointer data)
{
  NaTray *tray;
  IconTip    *icontip;

  if (value == NULL)
    return;

  icontip = value;
  tray    = data;
  if (icontip->tray != tray)
    return;

  if (icontip->fixedtip)
    na_fixed_tip_set_orientation (icontip->fixedtip, tray->priv->orientation);
}

static void
update_size_and_orientation (NaTray *tray)
{
  NaTrayPrivate *priv = tray->priv;

  gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box), priv->orientation);

  /* This only happens when setting the property during object construction */
  if (!priv->trays_screen)
    return;

  g_hash_table_foreach (priv->trays_screen->tip_table,
                        update_orientation_for_messages, tray);

  if (get_tray (priv->trays_screen) == tray)
    na_tray_manager_set_orientation (priv->trays_screen->tray_manager,
                                     priv->orientation);

  /* note, you want this larger if the frame has non-NONE relief by default. */
  switch (priv->orientation)
    {
    case GTK_ORIENTATION_VERTICAL:
      /* Give box a min size so the frame doesn't look dumb */
      gtk_widget_set_size_request (priv->box, MIN_BOX_SIZE, -1);
      break;
    case GTK_ORIENTATION_HORIZONTAL:
      gtk_widget_set_size_request (priv->box, -1, MIN_BOX_SIZE);
      break;
    }
}

/* Children with alpha channels have been set to be composited by calling
 * gdk_window_set_composited(). We need to paint these children ourselves.
 */
static void
#if GTK_CHECK_VERSION (3, 0, 0)
na_tray_draw_icon (GtkWidget *widget,
#else
na_tray_expose_icon (GtkWidget *widget,
#endif
		     gpointer   data)
{
  cairo_t *cr = (cairo_t *) data;

  if (na_tray_child_has_alpha (NA_TRAY_CHILD (widget)))
    {
      GtkAllocation allocation;

      gtk_widget_get_allocation (widget, &allocation);

#if GTK_CHECK_VERSION (3, 0, 0)
      cairo_save (cr);
      gdk_cairo_set_source_window (cr,
                                   gtk_widget_get_window (widget),
                                   allocation.x,
                                   allocation.y);
      cairo_rectangle (cr, allocation.x, allocation.y, allocation.width, allocation.height);
      cairo_clip (cr);
#else
      gdk_cairo_set_source_pixmap (cr,
                                   gtk_widget_get_window (widget),
				   allocation.x,
				   allocation.y);
#endif
      cairo_paint (cr);
#if GTK_CHECK_VERSION (3, 0, 0)
      cairo_restore (cr);
#endif
    }
}

static void
#if GTK_CHECK_VERSION (3, 0, 0)
na_tray_draw_box (GtkWidget *box,
                  cairo_t   *cr)
#else
na_tray_expose_box (GtkWidget      *box,
                    GdkEventExpose *event)
#endif
{
#if GTK_CHECK_VERSION (3, 0, 0)
  gtk_container_foreach (GTK_CONTAINER (box), na_tray_draw_icon, cr);
#else
  cairo_t *cr = gdk_cairo_create (gtk_widget_get_window (box));

  gdk_cairo_region (cr, event->region);
  cairo_clip (cr);

  gtk_container_foreach (GTK_CONTAINER (box), na_tray_expose_icon, cr);

  cairo_destroy (cr);
#endif
}

static void
na_tray_init (NaTray *tray)
{
  NaTrayPrivate *priv;

  priv = tray->priv = G_TYPE_INSTANCE_GET_PRIVATE (tray, NA_TYPE_TRAY, NaTrayPrivate);

  priv->screen = NULL;
  priv->orientation = GTK_ORIENTATION_HORIZONTAL;

  priv->frame = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
  gtk_container_add (GTK_CONTAINER (tray), priv->frame);
  gtk_widget_show (priv->frame);

#if GTK_CHECK_VERSION (3, 0, 0)
  priv->box = gtk_box_new (priv->orientation, ICON_SPACING);
  g_signal_connect (priv->box, "draw",
                    G_CALLBACK (na_tray_draw_box), NULL);
#else
  priv->box = g_object_new (na_box_get_type (), NULL);
  g_signal_connect (priv->box, "expose-event",
                    G_CALLBACK (na_tray_expose_box), tray);
  gtk_box_set_spacing (GTK_BOX (priv->box), ICON_SPACING);
#endif
  gtk_container_add (GTK_CONTAINER (priv->frame), priv->box);
  gtk_widget_show (priv->box);
}

static GObject *
na_tray_constructor (GType type,
                     guint n_construct_properties,
                     GObjectConstructParam *construct_params)
{
  GObject *object;
  NaTray *tray;
  NaTrayPrivate *priv;
  int screen_number;

  object = G_OBJECT_CLASS (na_tray_parent_class)->constructor (type,
                                                               n_construct_properties,
                                                               construct_params);
  tray = NA_TRAY (object);
  priv = tray->priv;

  g_assert (priv->screen != NULL);

  if (!initialized)
    {
#if GTK_CHECK_VERSION (3, 0, 0)
      trays_screens = g_new0 (TraysScreen, 1);
#else
      GdkDisplay *display;
      int n_screens;

      display = gdk_display_get_default ();
      n_screens = gdk_display_get_n_screens (display);
      trays_screens = g_new0 (TraysScreen, n_screens);
#endif
      initialized = TRUE;
    }

  screen_number = gdk_screen_get_number (priv->screen);

  if (trays_screens [screen_number].tray_manager == NULL)
    {
      NaTrayManager *tray_manager;

      tray_manager = na_tray_manager_new ();

      if (na_tray_manager_manage_screen (tray_manager, priv->screen))
        {
          trays_screens [screen_number].tray_manager = tray_manager;

          g_signal_connect (tray_manager, "tray_icon_added",
                            G_CALLBACK (tray_added),
                            &trays_screens [screen_number]);
          g_signal_connect (tray_manager, "tray_icon_removed",
                            G_CALLBACK (tray_removed),
                            &trays_screens [screen_number]);
          g_signal_connect (tray_manager, "message_sent",
                            G_CALLBACK (message_sent),
                            &trays_screens [screen_number]);
          g_signal_connect (tray_manager, "message_cancelled",
                            G_CALLBACK (message_cancelled),
                            &trays_screens [screen_number]);

          trays_screens [screen_number].icon_table = g_hash_table_new (NULL,
                                                                       NULL);
          trays_screens [screen_number].tip_table = g_hash_table_new_full (
                                                                NULL,
                                                                NULL,
                                                                NULL,
                                                                icon_tip_free);
        }
      else
        {
          g_printerr ("System tray didn't get the system tray manager selection for screen %d\n",
		      screen_number);
          g_object_unref (tray_manager);
        }
    }
      
  priv->trays_screen = &trays_screens [screen_number];
  trays_screens [screen_number].all_trays = g_slist_append (trays_screens [screen_number].all_trays,
                                                            tray);

  update_size_and_orientation (tray);

  return object;
}

static void
na_tray_dispose (GObject *object)
{
  NaTray *tray = NA_TRAY (object);
  NaTrayPrivate *priv = tray->priv;
  TraysScreen *trays_screen = priv->trays_screen;

  if (trays_screen != NULL)
    {
      trays_screen->all_trays = g_slist_remove (trays_screen->all_trays, tray);

      if (trays_screen->all_trays == NULL)
        {
          /* Make sure we drop the manager selection */
          g_object_unref (trays_screen->tray_manager);
          trays_screen->tray_manager = NULL;

          g_hash_table_destroy (trays_screen->icon_table);
          trays_screen->icon_table = NULL;

          g_hash_table_destroy (trays_screen->tip_table);
          trays_screen->tip_table = NULL;
        }
      else
        {
          NaTray *new_tray;

          new_tray = get_tray (trays_screen);
          if (new_tray != NULL)
            na_tray_manager_set_orientation (trays_screen->tray_manager,
                                             na_tray_get_orientation (new_tray));
        }
    }

  priv->trays_screen = NULL;

  if (priv->idle_redraw_id != 0)
    {
      g_source_remove (priv->idle_redraw_id);
      priv->idle_redraw_id = 0;
    }

  G_OBJECT_CLASS (na_tray_parent_class)->dispose (object);
}

static void
na_tray_set_property (GObject      *object,
		      guint         prop_id,
		      const GValue *value,
		      GParamSpec   *pspec)
{
  NaTray *tray = NA_TRAY (object);
  NaTrayPrivate *priv = tray->priv;

  switch (prop_id)
    {
    case PROP_ORIENTATION:
      na_tray_set_orientation (tray, g_value_get_enum (value));
      break;
    case PROP_SCREEN:
      priv->screen = g_value_get_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

#if GTK_CHECK_VERSION (3, 0, 0)
static void
na_tray_get_preferred_width (GtkWidget *widget,
                             gint      *minimal_width,
                             gint      *natural_width)
{
  gtk_widget_get_preferred_width (gtk_bin_get_child (GTK_BIN (widget)),
                                  minimal_width,
                                  natural_width);
}

static void
na_tray_get_preferred_height (GtkWidget *widget,
                              gint      *minimal_height,
                              gint      *natural_height)
{
  gtk_widget_get_preferred_height (gtk_bin_get_child (GTK_BIN (widget)),
                                   minimal_height,
                                   natural_height);
}
#else
static void
na_tray_size_request (GtkWidget        *widget,
                      GtkRequisition   *requisition)
{
  gtk_widget_size_request (gtk_bin_get_child (GTK_BIN (widget)), requisition);
}
#endif

static void
na_tray_size_allocate (GtkWidget        *widget,
                       GtkAllocation    *allocation)
{
  gtk_widget_size_allocate (gtk_bin_get_child (GTK_BIN (widget)), allocation);
  gtk_widget_set_allocation (widget, allocation);
}

static void
na_tray_class_init (NaTrayClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gobject_class->constructor = na_tray_constructor;
  gobject_class->set_property = na_tray_set_property;
  gobject_class->dispose = na_tray_dispose;

#if GTK_CHECK_VERSION (3, 0, 0)
  widget_class->get_preferred_width = na_tray_get_preferred_width;
  widget_class->get_preferred_height = na_tray_get_preferred_height;
#else
  widget_class->size_request = na_tray_size_request;
#endif
  widget_class->size_allocate = na_tray_size_allocate;

  g_object_class_install_property
    (gobject_class,
     PROP_ORIENTATION,
     g_param_spec_enum ("orientation", "orientation", "orientation",
		        GTK_TYPE_ORIENTATION,
			GTK_ORIENTATION_HORIZONTAL,
			G_PARAM_WRITABLE |
			G_PARAM_CONSTRUCT_ONLY |
			G_PARAM_STATIC_NAME |
			G_PARAM_STATIC_NICK |
			G_PARAM_STATIC_BLURB));
  
  g_object_class_install_property
    (gobject_class,
     PROP_SCREEN,
     g_param_spec_object ("screen", "screen", "screen",
			  GDK_TYPE_SCREEN,
			  G_PARAM_WRITABLE |
			  G_PARAM_CONSTRUCT_ONLY |
			  G_PARAM_STATIC_NAME |
			  G_PARAM_STATIC_NICK |
			  G_PARAM_STATIC_BLURB));

  g_type_class_add_private (gobject_class, sizeof (NaTrayPrivate));
}

NaTray *
na_tray_new_for_screen (GdkScreen      *screen,
		        GtkOrientation  orientation)
{
  return g_object_new (NA_TYPE_TRAY,
		       "screen", screen,
		       "orientation", orientation,
		       NULL);
}

void
na_tray_set_orientation (NaTray         *tray,
			 GtkOrientation  orientation)
{
  NaTrayPrivate *priv = tray->priv;

  if (orientation == priv->orientation)
    return;
  
  priv->orientation = orientation;

  update_size_and_orientation (tray);
}

GtkOrientation
na_tray_get_orientation (NaTray *tray)
{
  return tray->priv->orientation;
}

static gboolean
idle_redraw_cb (NaTray *tray)
{
  NaTrayPrivate *priv = tray->priv;

  gtk_container_foreach (GTK_CONTAINER (priv->box), (GtkCallback)na_tray_child_force_redraw, tray);
  
  priv->idle_redraw_id = 0;

  return FALSE;
}

void
na_tray_set_padding (NaTray *tray,
                     gint    padding)
{
  NaTrayPrivate *priv = tray->priv;

  if (get_tray (priv->trays_screen) == tray)
    na_tray_manager_set_padding (priv->trays_screen->tray_manager, padding);
}

void
na_tray_set_icon_size (NaTray *tray,
                       gint    size)
{
  NaTrayPrivate *priv = tray->priv;

  if (get_tray (priv->trays_screen) == tray)
    na_tray_manager_set_icon_size (priv->trays_screen->tray_manager, size);
}

void
na_tray_set_colors (NaTray   *tray,
#if GTK_CHECK_VERSION (3, 0, 0)
                    GdkRGBA  *fg,
                    GdkRGBA  *error,
                    GdkRGBA  *warning,
                    GdkRGBA  *success)
#else
                    GdkColor *fg,
                    GdkColor *error,
                    GdkColor *warning,
                    GdkColor *success)
#endif
{
  NaTrayPrivate *priv = tray->priv;

  if (get_tray (priv->trays_screen) == tray)
    na_tray_manager_set_colors (priv->trays_screen->tray_manager, fg, error, warning, success);
}

void
na_tray_force_redraw (NaTray *tray)
{
  NaTrayPrivate *priv = tray->priv;

  /* Force the icons to redraw their backgrounds.
   */
  if (priv->idle_redraw_id == 0)
    priv->idle_redraw_id = g_idle_add ((GSourceFunc) idle_redraw_cb, tray);
}