1 /* gtkplotcanvas - gtkplot canvas widget for gtk+
2 * Copyright 1999-2001 Adrian E. Feiguin <feiguin@ifir.edu.ar>
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 <stdio.h>
22 #include <string.h>
23 #include <math.h>
24 #include <gtk/gtk.h>
25 #include "gtkplot.h"
26 #include "gtkplotcanvas.h"
27 #include "gtkplotcanvaspixmap.h"
28 #include "gtkplotgdk.h"
29 #include "gtkplotps.h"
30
31 #define P_(string) string
32
33 enum {
34 ARG_0,
35 ARG_PIXMAP,
36 ARG_MASK,
37 };
38
39 static void gtk_plot_canvas_pixmap_init (GtkPlotCanvasPixmap *pixmap);
40 static void gtk_plot_canvas_pixmap_class_init(GtkPlotCanvasChildClass *klass);
41 static void gtk_plot_canvas_pixmap_destroy (GtkObject *object);
42 static void gtk_plot_canvas_pixmap_draw (GtkPlotCanvas *canvas,
43 GtkPlotCanvasChild *child);
44 static void gtk_plot_canvas_pixmap_move (GtkPlotCanvas *canvas,
45 GtkPlotCanvasChild *child,
46 gdouble x, gdouble y);
47 static void gtk_plot_canvas_pixmap_resize (GtkPlotCanvas *canvas,
48 GtkPlotCanvasChild *child,
49 gdouble x1, gdouble y1,
50 gdouble x2, gdouble y2);
51 static void gtk_plot_canvas_pixmap_get_property(GObject *object,
52 guint prop_id,
53 GValue *value,
54 GParamSpec *pspec);
55 static void gtk_plot_canvas_pixmap_set_property(GObject *object,
56 guint prop_id,
57 const GValue *value,
58 GParamSpec *pspec);
59
60 static GtkPlotCanvasChildClass *parent_class = NULL;
61
62 GtkType
gtk_plot_canvas_pixmap_get_type(void)63 gtk_plot_canvas_pixmap_get_type (void)
64 {
65 static GtkType plot_canvas_pixmap_type = 0;
66
67 if (!plot_canvas_pixmap_type)
68 {
69 GtkTypeInfo plot_canvas_pixmap_info =
70 {
71 "GtkPlotCanvasPixmap",
72 sizeof (GtkPlotCanvasPixmap),
73 sizeof (GtkPlotCanvasPixmapClass),
74 (GtkClassInitFunc) gtk_plot_canvas_pixmap_class_init,
75 (GtkObjectInitFunc) gtk_plot_canvas_pixmap_init,
76 /* reserved 1*/ NULL,
77 /* reserved 2 */ NULL,
78 (GtkClassInitFunc) NULL,
79 };
80
81 plot_canvas_pixmap_type = gtk_type_unique (gtk_plot_canvas_child_get_type(), &plot_canvas_pixmap_info);
82 }
83 return plot_canvas_pixmap_type;
84 }
85
86 GtkPlotCanvasChild*
gtk_plot_canvas_pixmap_new(GdkPixmap * _pixmap,GdkBitmap * mask)87 gtk_plot_canvas_pixmap_new (GdkPixmap *_pixmap, GdkBitmap *mask)
88 {
89 GtkPlotCanvasPixmap *pixmap;
90
91 pixmap = gtk_type_new (gtk_plot_canvas_pixmap_get_type ());
92
93 pixmap->pixmap = _pixmap;
94 pixmap->mask = mask;
95
96 if(_pixmap) gdk_pixmap_ref(_pixmap);
97 if(mask) gdk_bitmap_ref(mask);
98
99 return GTK_PLOT_CANVAS_CHILD (pixmap);
100 }
101
102 static void
gtk_plot_canvas_pixmap_init(GtkPlotCanvasPixmap * pixmap)103 gtk_plot_canvas_pixmap_init (GtkPlotCanvasPixmap *pixmap)
104 {
105 pixmap->pixmap = NULL;
106 pixmap->mask = NULL;
107 }
108
109 static void
gtk_plot_canvas_pixmap_destroy(GtkObject * object)110 gtk_plot_canvas_pixmap_destroy(GtkObject *object)
111 {
112 GtkPlotCanvasPixmap *pixmap = GTK_PLOT_CANVAS_PIXMAP(object);
113
114 if(pixmap->pixmap) gdk_pixmap_unref(pixmap->pixmap);
115 if(pixmap->mask) gdk_bitmap_unref(pixmap->mask);
116 pixmap->pixmap = NULL;
117 pixmap->mask = NULL;
118 }
119
120 static void
gtk_plot_canvas_pixmap_class_init(GtkPlotCanvasChildClass * klass)121 gtk_plot_canvas_pixmap_class_init (GtkPlotCanvasChildClass *klass)
122 {
123 GtkObjectClass *object_class = (GtkObjectClass *)klass;
124 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
125
126 parent_class = gtk_type_class (gtk_plot_canvas_child_get_type ());
127
128 object_class->destroy = gtk_plot_canvas_pixmap_destroy;
129
130 gobject_class->get_property = gtk_plot_canvas_pixmap_get_property;
131 gobject_class->set_property = gtk_plot_canvas_pixmap_set_property;
132
133 g_object_class_install_property (gobject_class,
134 ARG_PIXMAP,
135 g_param_spec_pointer ("pixmap",
136 P_("Pixmap"),
137 P_("Pixmap"),
138 G_PARAM_READABLE|G_PARAM_WRITABLE));
139 g_object_class_install_property (gobject_class,
140 ARG_MASK,
141 g_param_spec_pointer ("mask_bitmap",
142 P_("Mask"),
143 P_("Mask"),
144 G_PARAM_READABLE|G_PARAM_WRITABLE));
145
146 klass->draw = gtk_plot_canvas_pixmap_draw;
147 klass->move = gtk_plot_canvas_pixmap_move;
148 klass->move_resize = gtk_plot_canvas_pixmap_resize;
149 }
150
151 static void
gtk_plot_canvas_pixmap_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)152 gtk_plot_canvas_pixmap_get_property (GObject *object,
153 guint prop_id,
154 GValue *value,
155 GParamSpec *pspec)
156 {
157 GtkPlotCanvasPixmap *pixmap = GTK_PLOT_CANVAS_PIXMAP (object);
158
159 switch(prop_id){
160 case ARG_PIXMAP:
161 g_value_set_pointer(value, pixmap->pixmap);
162 break;
163 case ARG_MASK:
164 g_value_set_pointer(value, pixmap->mask);
165 break;
166 }
167 }
168
169 static void
gtk_plot_canvas_pixmap_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)170 gtk_plot_canvas_pixmap_set_property (GObject *object,
171 guint prop_id,
172 const GValue *value,
173 GParamSpec *pspec)
174 {
175 GtkPlotCanvasPixmap *pixmap = GTK_PLOT_CANVAS_PIXMAP (object);
176
177 switch(prop_id){
178 case ARG_PIXMAP:
179 if(pixmap->pixmap) gdk_pixmap_unref(pixmap->pixmap);
180 pixmap->pixmap = (GdkPixmap *)g_value_get_pointer(value);
181 if(pixmap->pixmap) gdk_pixmap_ref(pixmap->pixmap);
182 break;
183 case ARG_MASK:
184 if(pixmap->mask) gdk_bitmap_unref(pixmap->mask);
185 pixmap->mask = (GdkBitmap *)g_value_get_pointer(value);
186 if(pixmap->mask) gdk_bitmap_ref(pixmap->mask);
187 break;
188 }
189 }
190
191 static void
gtk_plot_canvas_pixmap_draw(GtkPlotCanvas * canvas,GtkPlotCanvasChild * child)192 gtk_plot_canvas_pixmap_draw (GtkPlotCanvas *canvas,
193 GtkPlotCanvasChild *child)
194 {
195 GtkPlotCanvasPixmap *pixmap = GTK_PLOT_CANVAS_PIXMAP(child);
196
197 g_return_if_fail(GTK_WIDGET_VISIBLE(GTK_WIDGET(canvas)));
198
199 if(pixmap->pixmap){
200 gdouble scale_x, scale_y;
201 gint width, height;
202
203 gdk_window_get_size(pixmap->pixmap, &width, &height);
204 scale_x = (gdouble)child->allocation.width / (gdouble)width;
205 scale_y = (gdouble)child->allocation.height / (gdouble)height;
206
207 gtk_plot_pc_draw_pixmap(canvas->pc, pixmap->pixmap, pixmap->mask,
208 0, 0,
209 child->allocation.x,
210 child->allocation.y,
211 width,
212 height,
213 scale_x, scale_y);
214
215 } else {
216 GdkColormap *colormap = gdk_colormap_get_system();
217 GdkColor black, white;
218
219 gdk_color_black(colormap, &black);
220 gdk_color_white(colormap, &white);
221
222 gtk_plot_pc_set_color(canvas->pc, &white);
223 gtk_plot_pc_draw_rectangle(canvas->pc, TRUE,
224 child->allocation.x, child->allocation.y,
225 child->allocation.width, child->allocation.height);
226 gtk_plot_pc_set_color(canvas->pc, &black);
227 gtk_plot_pc_draw_rectangle(canvas->pc, FALSE,
228 child->allocation.x, child->allocation.y,
229 child->allocation.width, child->allocation.height);
230 }
231 }
232
233 static void
gtk_plot_canvas_pixmap_move(GtkPlotCanvas * canvas,GtkPlotCanvasChild * child,gdouble x,gdouble y)234 gtk_plot_canvas_pixmap_move (GtkPlotCanvas *canvas,
235 GtkPlotCanvasChild *child,
236 gdouble x, gdouble y)
237 {
238 return;
239 }
240
241 static void
gtk_plot_canvas_pixmap_resize(GtkPlotCanvas * canvas,GtkPlotCanvasChild * child,gdouble x1,gdouble y1,gdouble x2,gdouble y2)242 gtk_plot_canvas_pixmap_resize (GtkPlotCanvas *canvas,
243 GtkPlotCanvasChild *child,
244 gdouble x1, gdouble y1,
245 gdouble x2, gdouble y2)
246 {
247 return;
248 }
249
250