]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
get rid of object_signal:: GtkObject argument uses, use GObject
[~andy/gtk] / gtk / gtkcellrendererpixbuf.c
1 /* gtkcellrendererpixbuf.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 <stdlib.h>
21 #include "gtkcellrendererpixbuf.h"
22
23 #ifndef _
24 #define _(x) x
25 #endif
26
27
28 static void gtk_cell_renderer_pixbuf_get_property  (GObject                    *object,
29                                                     guint                       param_id,
30                                                     GValue                     *value,
31                                                     GParamSpec                 *pspec,
32                                                     const gchar                *trailer);
33 static void gtk_cell_renderer_pixbuf_set_property  (GObject                    *object,
34                                                     guint                       param_id,
35                                                     const GValue               *value,
36                                                     GParamSpec                 *pspec,
37                                                     const gchar                *trailer);
38 static void gtk_cell_renderer_pixbuf_init       (GtkCellRendererPixbuf      *celltext);
39 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
40 static void gtk_cell_renderer_pixbuf_get_size   (GtkCellRenderer            *cell,
41                                                  GtkWidget                  *widget,
42                                                  gint                       *width,
43                                                  gint                       *height);
44 static void gtk_cell_renderer_pixbuf_render     (GtkCellRenderer            *cell,
45                                                  GdkWindow                  *window,
46                                                  GtkWidget                  *widget,
47                                                  GdkRectangle               *background_area,
48                                                  GdkRectangle               *cell_area,
49                                                  GdkRectangle               *expose_area,
50                                                  guint                       flags);
51
52
53 enum {
54         PROP_ZERO,
55         PROP_PIXBUF
56 };
57
58
59 GtkType
60 gtk_cell_renderer_pixbuf_get_type (void)
61 {
62         static GtkType cell_pixbuf_type = 0;
63
64         if (!cell_pixbuf_type)
65         {
66                 static const GTypeInfo cell_pixbuf_info =
67                 {
68                         sizeof (GtkCellRendererPixbufClass),
69                         NULL,           /* base_init */
70                         NULL,           /* base_finalize */
71                         (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
72                         NULL,           /* class_finalize */
73                         NULL,           /* class_data */
74                         sizeof (GtkCellRendererPixbuf),
75                         0,              /* n_preallocs */
76                         (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
77                 };
78
79                 cell_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf", &cell_pixbuf_info, 0);
80         }
81
82         return cell_pixbuf_type;
83 }
84
85 static void
86 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
87 {
88 }
89
90 static void
91 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
92 {
93         GObjectClass *object_class = G_OBJECT_CLASS (class);
94         GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
95
96         object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
97         object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
98
99         cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
100         cell_class->render = gtk_cell_renderer_pixbuf_render;
101
102         g_object_class_install_property (object_class,
103                                          PROP_PIXBUF,
104                                          g_param_spec_object ("pixbuf",
105                                                               _("Pixbuf Object"),
106                                                               _("The pixbuf to render."),
107                                                               GDK_TYPE_PIXBUF,
108                                                               G_PARAM_READABLE |
109                                                               G_PARAM_WRITABLE));
110 }
111
112 static void
113 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
114                                        guint           param_id,
115                                        GValue         *value,
116                                        GParamSpec     *pspec,
117                                        const gchar    *trailer)
118 {
119   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
120   
121   switch (param_id)
122     {
123     case PROP_PIXBUF:
124       g_value_init (value, G_TYPE_OBJECT);
125       g_value_set_object (value, G_OBJECT (cellpixbuf->pixbuf));
126       break;
127     default:
128       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
129       break;
130     }
131 }
132
133
134 static void
135 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
136                                        guint         param_id,
137                                        const GValue *value,
138                                        GParamSpec   *pspec,
139                                        const gchar  *trailer)
140 {
141   GdkPixbuf *pixbuf;
142   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
143   
144   switch (param_id)
145     {
146     case PROP_PIXBUF:
147       pixbuf = GDK_PIXBUF (g_value_get_object (value));
148       g_object_ref (G_OBJECT (pixbuf));
149       if (cellpixbuf->pixbuf)
150         g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
151       cellpixbuf->pixbuf = pixbuf;
152       break;
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
155       break;
156     }
157 }
158
159 GtkCellRenderer *
160 gtk_cell_renderer_pixbuf_new (void)
161 {
162         return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
163 }
164
165 static void
166 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
167                                    GtkWidget       *widget,
168                                    gint            *width,
169                                    gint            *height)
170 {
171         GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
172
173         if (width)
174                 *width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 +
175                         (cellpixbuf->pixbuf ? gdk_pixbuf_get_width (cellpixbuf->pixbuf) : 0);
176
177         if (height)
178                 *height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 +
179                         (cellpixbuf->pixbuf ? gdk_pixbuf_get_height (cellpixbuf->pixbuf) : 0);
180 }
181
182 static void
183 gtk_cell_renderer_pixbuf_render (GtkCellRenderer    *cell,
184                                  GdkWindow          *window,
185                                  GtkWidget          *widget,
186                                  GdkRectangle       *background_area,
187                                  GdkRectangle       *cell_area,
188                                  GdkRectangle       *expose_area,
189                                  guint               flags)
190
191 {
192         GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
193         GdkPixbuf *pixbuf;
194         guchar *pixels;
195         gint rowstride;
196         gint real_xoffset;
197         gint real_yoffset;
198         GdkGC *bg_gc = NULL;
199
200         pixbuf = cellpixbuf->pixbuf;
201
202         if (!pixbuf)
203                 return;
204
205         if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
206                 bg_gc = widget->style->bg_gc [GTK_STATE_SELECTED];
207         else
208                 bg_gc = widget->style->base_gc [GTK_STATE_NORMAL];
209
210         gdk_gc_set_clip_rectangle (bg_gc, cell_area);
211
212         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
213         pixels = gdk_pixbuf_get_pixels (pixbuf);
214
215         real_xoffset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - gdk_pixbuf_get_width (pixbuf) - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
216         real_xoffset = MAX (real_xoffset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
217         real_yoffset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - gdk_pixbuf_get_height (pixbuf) - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
218         real_yoffset = MAX (real_yoffset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
219
220         if (gdk_pixbuf_get_has_alpha (pixbuf))
221                 gdk_draw_rgb_32_image (window,
222                                        bg_gc,
223                                        cell_area->x + real_xoffset,
224                                        cell_area->y + real_yoffset,
225                                        gdk_pixbuf_get_width (pixbuf),
226                                        gdk_pixbuf_get_height (pixbuf),
227                                        GDK_RGB_DITHER_NORMAL,
228                                        pixels,
229                                        rowstride);
230         else
231                 gdk_draw_rgb_image (window,
232                                     bg_gc,
233                                     cell_area->x + real_xoffset,
234                                     cell_area->y + real_yoffset,
235                                     gdk_pixbuf_get_width (pixbuf),
236                                     gdk_pixbuf_get_height (pixbuf),
237                                     GDK_RGB_DITHER_NORMAL,
238                                     pixels,
239                                     rowstride);
240
241         gdk_gc_set_clip_rectangle (bg_gc, NULL);
242 }