]> Pileus Git - ~andy/gtk/blob - gdk/gdkgc.c
8eb6eaad46f11cfdd0ac62d3341cb49ba74b9a89
[~andy/gtk] / gdk / gdkgc.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 /*
21  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <string.h>
28
29 #include "gdkgc.h"
30 #include "gdkprivate.h"
31
32 GdkGC*
33 gdk_gc_alloc (void)
34 {
35   GdkGCPrivate *private;
36
37   private = g_new (GdkGCPrivate, 1);
38   private->ref_count = 1;
39   private->klass = NULL;
40   private->klass_data = NULL;
41   private->clip_x_origin = 0;
42   private->clip_y_origin = 0;
43   private->ts_x_origin = 0;
44   private->ts_y_origin = 0;
45
46   return (GdkGC *)private;
47 }
48
49 GdkGC*
50 gdk_gc_new (GdkDrawable *drawable)
51 {
52   g_return_val_if_fail (drawable != NULL, NULL);
53
54   if (GDK_DRAWABLE_DESTROYED (drawable))
55     return NULL;
56
57   return gdk_gc_new_with_values (drawable, NULL, 0);
58 }
59
60 GdkGC*
61 gdk_gc_new_with_values (GdkDrawable     *drawable,
62                         GdkGCValues     *values,
63                         GdkGCValuesMask  values_mask)
64 {
65   GdkGC *gc;
66   GdkGCPrivate *private;
67
68   g_return_val_if_fail (drawable != NULL, NULL);
69
70   if (GDK_DRAWABLE_DESTROYED (drawable))
71     return NULL;
72
73   gc = ((GdkDrawablePrivate *)drawable)->klass->create_gc (drawable,
74                                                            values,
75                                                            values_mask);
76   private = (GdkGCPrivate *)gc;
77   
78   if (values_mask & GDK_GC_CLIP_X_ORIGIN)
79     private->clip_x_origin = values->clip_x_origin;
80   if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
81     private->clip_y_origin = values->clip_y_origin;
82   if (values_mask & GDK_GC_TS_X_ORIGIN)
83     private->ts_x_origin = values->ts_x_origin;
84   if (values_mask & GDK_GC_TS_Y_ORIGIN)
85     private->ts_y_origin = values->ts_y_origin;
86
87   return gc;
88 }
89
90 GdkGC *
91 gdk_gc_ref (GdkGC *gc)
92 {
93   GdkGCPrivate *private = (GdkGCPrivate*) gc;
94
95   g_return_val_if_fail (gc != NULL, NULL);
96   private->ref_count += 1;
97
98   return gc;
99 }
100
101 void
102 gdk_gc_unref (GdkGC *gc)
103 {
104   GdkGCPrivate *private = (GdkGCPrivate*) gc;
105   
106   g_return_if_fail (gc != NULL);
107   g_return_if_fail (private->ref_count > 0);
108   
109   private->ref_count--;
110
111   if (private->ref_count == 0)
112     {
113       private->klass->destroy (gc);
114       g_free (private);
115     }
116 }
117
118 void
119 gdk_gc_get_values (GdkGC       *gc,
120                    GdkGCValues *values)
121 {
122   g_return_if_fail (gc != NULL);
123   g_return_if_fail (values != NULL);
124
125   ((GdkGCPrivate *)gc)->klass->get_values (gc, values);
126 }
127
128 void
129 gdk_gc_set_values (GdkGC           *gc,
130                    GdkGCValues     *values,
131                    GdkGCValuesMask  values_mask)
132 {
133   GdkGCPrivate *private = (GdkGCPrivate *)gc;
134   
135   g_return_if_fail (gc != NULL);
136   g_return_if_fail (values != NULL);
137
138   if (values_mask & GDK_GC_CLIP_X_ORIGIN)
139     private->clip_x_origin = values->clip_x_origin;
140   if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
141     private->clip_y_origin = values->clip_y_origin;
142   if (values_mask & GDK_GC_TS_X_ORIGIN)
143     private->ts_x_origin = values->ts_x_origin;
144   if (values_mask & GDK_GC_TS_Y_ORIGIN)
145     private->ts_y_origin = values->ts_y_origin;
146   
147   private->klass->set_values (gc, values, values_mask);
148 }
149
150 void
151 gdk_gc_set_foreground (GdkGC    *gc,
152                        GdkColor *color)
153 {
154   GdkGCValues values;
155
156   g_return_if_fail (gc != NULL);
157   g_return_if_fail (color != NULL);
158
159   values.foreground = *color;
160   gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
161 }
162
163 void
164 gdk_gc_set_background (GdkGC    *gc,
165                        GdkColor *color)
166 {
167   GdkGCValues values;
168
169   g_return_if_fail (gc != NULL);
170   g_return_if_fail (color != NULL);
171
172   values.background = *color;
173   gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
174 }
175
176 void
177 gdk_gc_set_font (GdkGC   *gc,
178                  GdkFont *font)
179 {
180   GdkGCValues values;
181
182   g_return_if_fail (gc != NULL);
183   g_return_if_fail (font != NULL);
184
185   values.font = font;
186   gdk_gc_set_values (gc, &values, GDK_GC_FONT);
187 }
188
189 void
190 gdk_gc_set_function (GdkGC       *gc,
191                      GdkFunction  function)
192 {
193   GdkGCValues values;
194
195   g_return_if_fail (gc != NULL);
196
197   values.function = function;
198   gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
199 }
200
201 void
202 gdk_gc_set_fill (GdkGC   *gc,
203                  GdkFill  fill)
204 {
205   GdkGCValues values;
206
207   g_return_if_fail (gc != NULL);
208
209   values.fill = fill;
210   gdk_gc_set_values (gc, &values, GDK_GC_FILL);
211 }
212
213 void
214 gdk_gc_set_tile (GdkGC     *gc,
215                  GdkPixmap *tile)
216 {
217   GdkGCValues values;
218
219   g_return_if_fail (gc != NULL);
220
221   values.tile = tile;
222   gdk_gc_set_values (gc, &values, GDK_GC_TILE);
223 }
224
225 void
226 gdk_gc_set_stipple (GdkGC     *gc,
227                     GdkPixmap *stipple)
228 {
229   GdkGCValues values;
230
231   g_return_if_fail (gc != NULL);
232
233   values.stipple = stipple;
234   gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
235 }
236
237 void
238 gdk_gc_set_ts_origin (GdkGC *gc,
239                       gint   x,
240                       gint   y)
241 {
242   GdkGCValues values;
243
244   g_return_if_fail (gc != NULL);
245
246   values.ts_x_origin = x;
247   values.ts_y_origin = y;
248   
249   gdk_gc_set_values (gc, &values,
250                      GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
251 }
252
253 void
254 gdk_gc_set_clip_origin (GdkGC *gc,
255                         gint   x,
256                         gint   y)
257 {
258   GdkGCValues values;
259
260   g_return_if_fail (gc != NULL);
261
262   values.clip_x_origin = x;
263   values.clip_y_origin = y;
264   
265   gdk_gc_set_values (gc, &values,
266                      GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
267 }
268
269 void
270 gdk_gc_set_clip_mask (GdkGC     *gc,
271                       GdkBitmap *mask)
272 {
273   GdkGCValues values;
274   
275   g_return_if_fail (gc != NULL);
276   
277   values.clip_mask = mask;
278   gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
279 }
280
281
282 void
283 gdk_gc_set_subwindow (GdkGC            *gc,
284                       GdkSubwindowMode  mode)
285 {
286   GdkGCValues values;
287
288   g_return_if_fail (gc != NULL);
289
290   values.subwindow_mode = mode;
291   gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
292 }
293
294 void
295 gdk_gc_set_exposures (GdkGC     *gc,
296                       gboolean   exposures)
297 {
298   GdkGCValues values;
299
300   g_return_if_fail (gc != NULL);
301
302   values.graphics_exposures = exposures;
303   gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
304 }
305
306 void
307 gdk_gc_set_line_attributes (GdkGC       *gc,
308                             gint         line_width,
309                             GdkLineStyle line_style,
310                             GdkCapStyle  cap_style,
311                             GdkJoinStyle join_style)
312 {
313   GdkGCValues values;
314
315   values.line_width = line_width;
316   values.line_style = line_style;
317   values.cap_style = cap_style;
318   values.join_style = join_style;
319
320   gdk_gc_set_values (gc, &values,
321                      GDK_GC_LINE_WIDTH |
322                      GDK_GC_LINE_STYLE |
323                      GDK_GC_CAP_STYLE |
324                      GDK_GC_JOIN_STYLE);
325 }
326
327 void
328 gdk_gc_set_dashes (GdkGC *gc,
329                    gint   dash_offset,
330                    gint8  dash_list[],
331                    gint   n)
332 {
333   g_return_if_fail (gc != NULL);
334   g_return_if_fail (dash_list != NULL);
335
336   ((GdkGCPrivate *)gc)->klass->set_dashes (gc, dash_offset, dash_list, n);
337 }