* a bit of re-factoring
[simple-launcher] / applet-wrapper.cc
1 // This file is a part of Simple Launcher
2 //
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
4 //
5 // Simple Launcher is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License version 2 as published by
7 // the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 #include <cairo/cairo.h>
19
20 #include <libhildondesktop/libhildondesktop.h>
21 #include "applet-wrapper.h"
22
23 #include "simple-launcher.h"
24
25 struct _SLAWrapperPrivate {
26   SimpleLauncherApplet *applet;
27   GdkPixmap *background_pixmap;
28 };
29
30   static void sla_wrapper_init(SLAWrapper *self);
31   static void sla_wrapper_class_init(SLAWrapperClass *klass);
32   static void sla_wrapper_finalize(GObject *object);
33   static gboolean sla_wrapper_expose(GtkWidget *widget, GdkEventExpose *event);
34   static void sla_wrapper_size_allocate(GtkWidget *widget, GtkAllocation *alloc);
35   static void sla_wrapper_size_request(GtkWidget *widget, GtkRequisition *requisition);
36   static void sla_wrapper_make_background(GtkWidget *widget, SLAWrapperPrivate *priv);
37
38 HD_DEFINE_PLUGIN(SLAWrapper, sla_wrapper, HILDON_DESKTOP_TYPE_HOME_ITEM)
39
40 static void sla_wrapper_init(SLAWrapper *self) {
41   GdkColormap *colormap = NULL;
42
43   self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self, SLA_TYPE_APPLET, SLAWrapperPrivate);
44   self->priv->background_pixmap = NULL;
45
46   self->priv->applet = new SimpleLauncherApplet(SL_APPLET_GCONF_PATH);
47
48   if ((colormap = gdk_screen_get_rgba_colormap(gdk_screen_get_default())) != NULL) {
49     gtk_widget_set_colormap(GTK_WIDGET(self), colormap);
50   }
51 }
52
53 static void sla_wrapper_class_init(SLAWrapperClass *klass) {
54   GtkWidgetClass *widget_class;
55   GObjectClass *object_class;
56
57   widget_class = GTK_WIDGET_CLASS(klass);
58   object_class = G_OBJECT_CLASS(klass);
59
60   object_class->finalize = sla_wrapper_finalize;
61
62   widget_class->expose_event = sla_wrapper_expose;
63   widget_class->size_allocate = sla_wrapper_size_allocate;
64   widget_class->size_request = sla_wrapper_size_request;
65
66   g_type_class_add_private(klass, sizeof(SLAWrapperPrivate));   // Do I need this?
67 }
68
69 static void sla_wrapper_finalize(GObject *self) {
70   SLAWrapperPrivate *priv = SLA_APPLET(self)->priv;
71
72   if (priv->background_pixmap != NULL) {
73     g_object_unref(priv->background_pixmap);
74     priv->background_pixmap = NULL;
75   }
76
77   if (priv->applet != NULL) {
78     delete priv->applet;
79   }
80 }
81
82 static void sla_wrapper_make_background(GtkWidget *widget, SLAWrapperPrivate *priv) {
83   if (priv->background_pixmap != NULL) {
84     g_object_unref(priv->background_pixmap);
85     priv->background_pixmap = NULL;
86   }
87
88   priv->background_pixmap = gdk_pixmap_new(widget->window, priv->applet->getWidth(), priv->applet->getHeight(), 32);
89
90   cairo_t *cr = gdk_cairo_create(priv->background_pixmap);
91   double red, green, blue, alpha;
92
93   priv->applet->getBackgroundColour(red, green, blue, alpha);
94
95   cairo_set_source_rgba(cr, red, green, blue, alpha);
96   cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
97   cairo_paint(cr);
98   cairo_destroy(cr);
99 }
100
101 static gboolean sla_wrapper_expose(GtkWidget *widget, GdkEventExpose *event) {
102   if (GTK_WIDGET_DRAWABLE(widget)) {
103     SLAWrapperPrivate *priv = SLA_APPLET(widget)->priv;
104
105     if (priv->background_pixmap == NULL) {
106       sla_wrapper_make_background(widget, priv);
107     }
108
109     cairo_t *cr = gdk_cairo_create(widget->window);
110     gdk_cairo_set_source_pixmap(cr, priv->background_pixmap, 0, 0);
111
112     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
113     cairo_paint(cr);
114     cairo_destroy(cr);
115
116     return GTK_WIDGET_CLASS(sla_wrapper_parent_class)->expose_event(widget, event);
117   } else {
118     return FALSE;
119   }
120 }
121
122 static void sla_wrapper_size_allocate(GtkWidget *widget, GtkAllocation *alloc) {
123   GTK_WIDGET_CLASS(sla_wrapper_parent_class)->size_allocate(widget, alloc);
124 }
125
126 static void sla_wrapper_size_request(GtkWidget *widget, GtkRequisition *requisition) {
127   if (GTK_WIDGET_DRAWABLE(widget)) {
128     SLAWrapperPrivate *priv = SLA_APPLET(widget)->priv;
129
130     requisition->width = priv->applet->getWidth();
131     requisition->height = priv->applet->getHeight();
132   }
133 }
134
135 // vim:ts=2:sw=2:et