~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to systray/src/cd-tray.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Neil Jagdish Patel
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <string.h>
 
21
#include <stdio.h>
 
22
#include <gtk/gtk.h>
 
23
#include <X11/Xatom.h>
 
24
#include <math.h>
 
25
 
 
26
#include <cairo-dock.h>
 
27
#include "cd-tray.h"
 
28
#include "systray-struct.h"
 
29
 
 
30
 
 
31
//static const guint icon_size_w = 24;
 
32
//static const guint icon_size_h = 24;
 
33
 
 
34
static void
 
35
tray_icon_added (NaTrayManager *manager,
 
36
                 GtkWidget      *icon,
 
37
                 TrayApplet     *applet);
 
38
 
 
39
static void
 
40
tray_icon_removed (NaTrayManager *manager,
 
41
                   GtkWidget      *icon,
 
42
                   TrayApplet     *applet);
 
43
 
 
44
static void
 
45
tray_icon_message_sent (NaTrayManager *manager,
 
46
                        GtkWidget      *icon,
 
47
                        const char     *text,
 
48
                        glong           id,
 
49
                        glong           timeout,
 
50
                        TrayApplet     *applet);
 
51
 
 
52
static void
 
53
tray_icon_message_cancelled (NaTrayManager *manager,
 
54
                             GtkWidget      *icon,
 
55
                             glong           id,
 
56
                             TrayApplet     *applet);
 
57
 
 
58
 
 
59
 
 
60
static void tray_resize_container(TrayApplet *applet)
 
61
{
 
62
/*   GtkRequisition req; */
 
63
 
 
64
/*   gtk_widget_size_request(GTK_WIDGET(applet->box), &req); */
 
65
/*   if (systray.dialog && &systray.dialog->pWidget) */
 
66
/* /\*     gtk_widget_set_size_request(GTK_WINDOW(systray.dialog->pWidget), req.width, req.height); *\/ */
 
67
/*   //    gtk_window_set_default_size(GTK_WINDOW(systray.dialog->pWidget), req.width, req.height); */
 
68
/*   gtk_window_resize(GTK_WINDOW(systray.dialog->pWidget), req.width, req.height); */
 
69
}
 
70
 
 
71
 
 
72
 
 
73
static gboolean idle_redraw_cb (TrayApplet *applet)
 
74
{
 
75
  applet->idle_redraw_id = 0;
 
76
  gtk_widget_hide(applet->box);
 
77
  gtk_widget_show(applet->box);
 
78
  return FALSE;
 
79
}
 
80
 
 
81
static void force_redraw (TrayApplet *applet)
 
82
{
 
83
  /* Force the icons to redraw their backgrounds.
 
84
   * gtk_widget_queue_draw() doesn't work across process boundaries,
 
85
   * so we do this instead.
 
86
   */
 
87
  if (applet->idle_redraw_id == 0)
 
88
    applet->idle_redraw_id = g_idle_add ((GSourceFunc) idle_redraw_cb, applet);
 
89
}
 
90
 
 
91
 
 
92
static void
 
93
tray_icon_added (NaTrayManager *manager,
 
94
                 GtkWidget      *icon,
 
95
                 TrayApplet     *applet)
 
96
{
 
97
  applet->icons = g_list_append (applet->icons, icon);
 
98
  gtk_widget_set_colormap(icon, gdk_screen_get_rgb_colormap (gdk_screen_get_default()));
 
99
  gtk_box_pack_start(GTK_BOX(applet->box), icon, TRUE, TRUE, 0);
 
100
  //gtk_widget_set_size_request((applet->box), 52, 28);
 
101
  gtk_widget_set_size_request(icon, myConfig.iIconSize, myConfig.iIconSize);
 
102
  //tray_resize_container(applet);
 
103
  force_redraw(applet);
 
104
}
 
105
 
 
106
static void
 
107
tray_icon_removed (NaTrayManager *manager,
 
108
                   GtkWidget      *icon,
 
109
                   TrayApplet     *applet)
 
110
{
 
111
  applet->icons = g_list_remove (applet->icons, icon);
 
112
  tray_resize_container(applet);
 
113
  gtk_container_remove(GTK_CONTAINER(applet->box), icon);
 
114
}
 
115
 
 
116
static void
 
117
tray_icon_message_sent (NaTrayManager *manager,
 
118
                        GtkWidget      *icon,
 
119
                        const char     *text,
 
120
                        glong           id,
 
121
                        glong           timeout,
 
122
                        TrayApplet     *applet)
 
123
{
 
124
  g_warning ("tray_icon_message_sent : %s\n", text);
 
125
  cairo_dock_show_temporary_dialog(text, myIcon, myContainer, timeout);
 
126
}
 
127
 
 
128
static void tray_icon_message_cancelled (NaTrayManager *manager,
 
129
                                         GtkWidget      *icon,
 
130
                                         glong           id,
 
131
                                         TrayApplet     *applet)
 
132
{
 
133
  /* FIXME: Er, cancel the message :-/? */
 
134
}
 
135
 
 
136
static gboolean tray_clean_up(GtkWidget *widget,
 
137
                              GdkEvent  *event,
 
138
                              gpointer   user_data)
 
139
{
 
140
  TrayApplet *tray = (TrayApplet *)user_data;
 
141
  g_warning("free baby\n");
 
142
  g_object_unref (tray->manager);
 
143
  g_object_unref (tray);
 
144
}
 
145
 
 
146
 
 
147
  static gboolean cd_desklet_on_expose(GtkWidget *pWidget, 
 
148
                                      GdkEventExpose *pExpose, 
 
149
                                      gpointer pDialog) 
 
150
 { 
 
151
 cairo_t *pCairoContext = gdk_cairo_create (pWidget->window); 
 
152
   if (cairo_status (pCairoContext) != CAIRO_STATUS_SUCCESS, FALSE) { 
 
153
     cairo_destroy (pCairoContext); 
 
154
     return FALSE; 
 
155
   } 
 
156
   //erase the background
 
157
   cairo_set_source_rgba (pCairoContext, 0., 0., 1., 0.15); 
 
158
   cairo_set_operator (pCairoContext, CAIRO_OPERATOR_SOURCE); 
 
159
   cairo_paint (pCairoContext); 
 
160
   cairo_destroy (pCairoContext); 
 
161
   return FALSE; 
 
162
 } 
 
163
 
 
164
static void tray_create_widget(TrayApplet *applet)
 
165
{
 
166
  applet->manager = na_tray_manager_new();
 
167
  if (!na_tray_manager_manage_screen (applet->manager, applet->screen))
 
168
    g_warning ("The notification area could not manage the screen \n");
 
169
  g_signal_connect (applet->box, "delete-event",
 
170
                    G_CALLBACK (tray_clean_up), applet);
 
171
 
 
172
  g_signal_connect (applet->manager, "tray_icon_added",
 
173
                    G_CALLBACK (tray_icon_added), applet);
 
174
  g_signal_connect (applet->manager, "tray_icon_removed",
 
175
                    G_CALLBACK (tray_icon_removed), applet);
 
176
  g_signal_connect (applet->manager, "message_sent",
 
177
                    G_CALLBACK (tray_icon_message_sent), applet);
 
178
  g_signal_connect (applet->manager, "message_cancelled",
 
179
                    G_CALLBACK (tray_icon_message_cancelled), applet);
 
180
  g_signal_connect (applet->box, "expose-event",
 
181
                     G_CALLBACK (cd_desklet_on_expose), applet->box);
 
182
 
 
183
 
 
184
        GdkColormap* pColormap = gdk_screen_get_rgba_colormap (applet->screen);
 
185
        gtk_widget_set_colormap (applet->box, pColormap);
 
186
        
 
187
//      GdkVisual* pVisual = gdk_rgb_get_visual ();
 
188
//      Visual *vis = GDK_VISUAL_XVISUAL (pVisual);
 
189
//      VisualID visualid = vis->visualid;
 
190
//      
 
191
//      Window Xid = GDK_DRAWABLE_XID (applet->box);
 
192
//      Atom aNetVisualID = XInternAtom (cairo_dock_get_Xdisplay (), "_NET_SYSTEM_TRAY_VISUAL", False);
 
193
//      XChangeProperty (cairo_dock_get_Xdisplay (),
 
194
//              Xid,
 
195
//              aNetVisualID,
 
196
//              XA_VISUALID, 32, PropModeReplace,
 
197
//              (guchar *)&visualid, 1);
 
198
        
 
199
        gtk_container_add (GTK_CONTAINER (applet->widget), applet->box);
 
200
}
 
201
 
 
202
static void tray_icon_cb_click_steal(GtkWidget *w, TrayApplet* applet)
 
203
{
 
204
  gtk_container_remove(GTK_CONTAINER(applet->widget), w);
 
205
  tray_create_widget(applet);
 
206
}
 
207
 
 
208
TrayApplet* tray_init (GtkWidget *parent)
 
209
{
 
210
  TrayApplet *applet = g_new0 (TrayApplet, 1);
 
211
  GdkScreen  *screen;
 
212
  GtkWidget *widget = GTK_WIDGET(parent);
 
213
 
 
214
  //get the real parent
 
215
  while (widget->parent)
 
216
    widget = widget->parent;
 
217
  screen = gtk_widget_get_screen(GTK_WIDGET(widget));
 
218
 
 
219
  applet->box = (myConfig.iIconPacking == 0 ? gtk_hbox_new(TRUE, 0) : gtk_vbox_new(TRUE, 0));
 
220
  gtk_widget_show(applet->box);
 
221
  //gtk_widget_set_size_request(applet->box, icon_size_w*10, icon_size_h + 6);
 
222
  tray_resize_container(applet);
 
223
  applet->icons = NULL;
 
224
  applet->screen = screen;
 
225
 
 
226
  applet->widget = gtk_event_box_new ();
 
227
  //gtk_event_box_set_visible_window(GTK_EVENT_BOX (applet->widget), TRUE);  /// utile ?...
 
228
  ///gtk_widget_set_colormap(applet->widget, gdk_screen_get_rgb_colormap (screen));
 
229
        //GdkColormap* pColormap = gdk_screen_get_rgba_colormap (screen);  /// utile ?...
 
230
        //if (!pColormap)
 
231
        //      pColormap = gdk_screen_get_rgb_colormap (screen);
 
232
        //gtk_widget_set_colormap (applet->widget, pColormap);
 
233
  
 
234
  
 
235
  if (na_tray_manager_check_running(screen)) {
 
236
    cd_warning ("There is already another notification area running on this screen");
 
237
    GtkWidget *w = gtk_button_new_with_label("TRY to steal systray icons");
 
238
    gtk_widget_show(w);
 
239
    gtk_container_add (GTK_CONTAINER (applet->widget), w);
 
240
    g_signal_connect (w, "clicked",
 
241
                      G_CALLBACK (tray_icon_cb_click_steal), applet);
 
242
    return applet;
 
243
  }
 
244
 
 
245
  tray_create_widget(applet);
 
246
 
 
247
  return applet;
 
248
}