]> git.sur5r.net Git - glabels/blob - glabels2/src/label-image.c
Initial revision
[glabels] / glabels2 / src / label-image.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  label_image.c:  GLabels label image object
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <glib.h>
24
25 #include "label-image.h"
26
27 #include "pixmaps/checkerboard.xpm"
28
29 #include "debug.h"
30
31 /*========================================================*/
32 /* Private types.                                         */
33 /*========================================================*/
34
35 struct _glLabelImagePrivate {
36         gchar            *filename;
37         GdkPixbuf        *pixbuf;
38 };
39
40 /*========================================================*/
41 /* Private globals.                                       */
42 /*========================================================*/
43
44 static GObjectClass *parent_class = NULL;
45
46 static guint instance = 0;
47
48 /*========================================================*/
49 /* Private function prototypes.                           */
50 /*========================================================*/
51
52 static void gl_label_image_class_init (glLabelImageClass *klass);
53 static void gl_label_image_instance_init (glLabelImage *limage);
54 static void gl_label_image_finalize (GObject *object);
55
56 \f
57 /*****************************************************************************/
58 /* Boilerplate object stuff.                                                 */
59 /*****************************************************************************/
60 GType
61 gl_label_image_get_type (void)
62 {
63         static GType type = 0;
64
65         if (!type) {
66                 GTypeInfo info = {
67                         sizeof (glLabelImageClass),
68                         NULL,
69                         NULL,
70                         (GClassInitFunc) gl_label_image_class_init,
71                         NULL,
72                         NULL,
73                         sizeof (glLabelImage),
74                         0,
75                         (GInstanceInitFunc) gl_label_image_instance_init,
76                 };
77
78                 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
79                                                "glLabelImage", &info, 0);
80         }
81
82         return type;
83 }
84
85 static void
86 gl_label_image_class_init (glLabelImageClass *klass)
87 {
88         GObjectClass *object_class = (GObjectClass *) klass;
89
90         parent_class = g_type_class_peek_parent (klass);
91
92         object_class->finalize = gl_label_image_finalize;
93 }
94
95 static void
96 gl_label_image_instance_init (glLabelImage *limage)
97 {
98         limage->private = g_new0 (glLabelImagePrivate, 1);
99
100         limage->private->pixbuf =
101                 gdk_pixbuf_new_from_xpm_data ((const char **)
102                                               checkerboard_xpm);
103 }
104
105 static void
106 gl_label_image_finalize (GObject *object)
107 {
108         glLabelImage *limage;
109
110         g_return_if_fail (object && GL_IS_LABEL_IMAGE (object));
111
112         limage = GL_LABEL_IMAGE (object);
113
114         g_free (limage->private);
115
116         G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 /*****************************************************************************/
120 /* NEW label "image" object.                                               */
121 /*****************************************************************************/
122 GObject *
123 gl_label_image_new (glLabel *label)
124 {
125         glLabelImage *limage;
126
127         limage = g_object_new (gl_label_image_get_type(), NULL);
128
129         gl_label_object_set_parent (GL_LABEL_OBJECT(limage), label);
130
131         return G_OBJECT (limage);
132 }
133
134 /*****************************************************************************/
135 /* Duplicate object.                                                         */
136 /*****************************************************************************/
137 glLabelImage *
138 gl_label_image_dup (glLabelImage *limage,
139                     glLabel *label)
140 {
141         glLabelImage *new_limage;
142         gdouble      x, y, w, h;
143         gchar        *filename;
144
145         gl_debug (DEBUG_LABEL, "START");
146
147         g_return_if_fail (limage && GL_IS_LABEL_IMAGE (limage));
148         g_return_if_fail (label && GL_IS_LABEL (label));
149
150         new_limage = GL_LABEL_IMAGE(gl_label_image_new (label));
151
152         gl_label_object_get_position (GL_LABEL_OBJECT(limage), &x, &y);
153         gl_label_object_get_size     (GL_LABEL_OBJECT(limage), &w, &h);
154
155         gl_label_object_set_position (GL_LABEL_OBJECT(new_limage),  x,  y);
156         gl_label_object_set_size     (GL_LABEL_OBJECT(new_limage),  w,  h);
157
158         filename = gl_label_image_get_filename (limage);
159
160         gl_label_image_set_filename (new_limage, filename);
161
162         g_free (filename);
163
164         gl_debug (DEBUG_LABEL, "END");
165
166         return new_limage;
167 }
168
169
170 /*****************************************************************************/
171 /* Set object params.                                                        */
172 /*****************************************************************************/
173 void
174 gl_label_image_set_filename (glLabelImage *limage,
175                              const gchar *filename)
176 {
177         GdkPixbuf *pixbuf;
178
179         gl_debug (DEBUG_LABEL, "START");
180
181         g_return_if_fail (limage && GL_IS_LABEL_IMAGE (limage));
182
183         if ( filename == NULL ) {
184
185                 g_free (limage->private->filename);
186                 limage->private->filename = NULL;
187
188                 g_object_unref (limage->private->pixbuf);
189                 limage->private->pixbuf =
190                         gdk_pixbuf_new_from_xpm_data ((const char **)
191                                                       checkerboard_xpm);
192
193                 gl_label_object_emit_changed (GL_LABEL_OBJECT(limage));
194         } else {
195
196                 if ( (limage->private->filename == NULL) ||
197                      (strcmp (limage->private->filename, filename) != 0) ) {
198
199                         g_free (limage->private->filename);
200                         limage->private->filename = g_strdup (filename);
201
202                         pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
203                         g_object_unref (limage->private->pixbuf);
204                         if ( pixbuf != NULL ) {
205                                 limage->private->pixbuf = pixbuf;
206                         } else {
207                                 limage->private->pixbuf =
208                                         gdk_pixbuf_new_from_xpm_data ((const char **)
209                                                                       checkerboard_xpm);
210                         }
211
212                         gl_label_object_emit_changed (GL_LABEL_OBJECT(limage));
213                 }
214
215         }
216
217         gl_debug (DEBUG_LABEL, "END");
218 }
219
220
221 /*****************************************************************************/
222 /* Get object params.                                                        */
223 /*****************************************************************************/
224 gchar *
225 gl_label_image_get_filename (glLabelImage *limage)
226 {
227         g_return_val_if_fail (limage && GL_IS_LABEL_IMAGE (limage), NULL);
228
229         return g_strdup (limage->private->filename);
230 }
231
232 const GdkPixbuf *
233 gl_label_image_get_pixbuf (glLabelImage *limage)
234 {
235         g_return_val_if_fail (limage && GL_IS_LABEL_IMAGE (limage), NULL);
236
237         return limage->private->pixbuf;
238 }
239