Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / image.c
blob6de48ef8fb0a91516592bd55c5ba99401e73e6cc
1 #include "m_pd.h"
2 #include "g_canvas.h"
4 #ifdef NT
5 #pragma warning( disable : 4244 )
6 #pragma warning( disable : 4305 )
7 #endif
9 /* ------------------------ image ----------------------------- */
11 static t_class *image_class;
13 typedef struct _image
15 t_object x_obj;
16 t_glist * x_glist;
17 int x_width;
18 int x_height;
19 t_symbol* x_fname;
20 } t_image;
22 /* widget helper functions */
24 void image_drawme(t_image *x, t_glist *glist, int firsttime)
26 if (firsttime) {
27 char fname[MAXPDSTRING];
28 canvas_makefilename(glist_getcanvas(x->x_glist), x->x_fname->s_name,
29 fname, MAXPDSTRING);
31 sys_vgui("image create photo img%x -file %s\n",x,fname);
32 sys_vgui(".x%x.c create image %d %d -image img%x -tags %xS\n",
33 glist_getcanvas(glist),text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x,x);
35 /* TODO callback from gui
36 sys_vgui("image_size logo");
39 else {
40 sys_vgui(".x%x.c coords %xS \
41 %d %d\n",
42 glist_getcanvas(glist), x,
43 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist));
49 void image_erase(t_image* x,t_glist* glist)
51 int n;
52 sys_vgui(".x%x.c delete %xS\n",
53 glist_getcanvas(glist), x);
59 /* ------------------------ image widgetbehaviour----------------------------- */
62 static void image_getrect(t_gobj *z, t_glist *glist,
63 int *xp1, int *yp1, int *xp2, int *yp2)
65 int width, height;
66 t_image* x = (t_image*)z;
69 width = x->x_width;
70 height = x->x_height;
71 *xp1 = text_xpix(&x->x_obj, glist);
72 *yp1 = text_ypix(&x->x_obj, glist);
73 *xp2 = text_xpix(&x->x_obj, glist) + width;
74 *yp2 = text_ypix(&x->x_obj, glist) + height;
77 static void image_displace(t_gobj *z, t_glist *glist,
78 int dx, int dy)
80 t_image *x = (t_image *)z;
81 x->x_obj.te_xpix += dx;
82 x->x_obj.te_ypix += dy;
83 sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n",
84 glist_getcanvas(glist), x,
85 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
86 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height);
88 image_drawme(x, glist, 0);
89 canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x);
92 static void image_select(t_gobj *z, t_glist *glist, int state)
94 t_image *x = (t_image *)z;
95 if (state) {
96 sys_vgui(".x%x.c create rectangle \
97 %d %d %d %d -tags %xSEL -outline blue\n",
98 glist_getcanvas(glist),
99 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
100 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height,
103 else {
104 sys_vgui(".x%x.c delete %xSEL\n",
105 glist_getcanvas(glist), x);
113 static void image_activate(t_gobj *z, t_glist *glist, int state)
115 /* t_text *x = (t_text *)z;
116 t_rtext *y = glist_findrtext(glist, x);
117 if (z->g_pd != gatom_class) rtext_activate(y, state);*/
120 static void image_delete(t_gobj *z, t_glist *glist)
122 t_text *x = (t_text *)z;
123 canvas_deletelinesfor(glist_getcanvas(glist), x);
127 static void image_vis(t_gobj *z, t_glist *glist, int vis)
129 t_image* s = (t_image*)z;
130 if (vis)
131 image_drawme(s, glist, 1);
132 else
133 image_erase(s,glist);
136 /* can we use the normal text save function ?? */
138 static void image_save(t_gobj *z, t_binbuf *b)
140 t_image *x = (t_image *)z;
141 binbuf_addv(b, "ssiiss", gensym("#X"),gensym("obj"),
142 x->x_obj.te_xpix, x->x_obj.te_ypix,
143 gensym("image"),x->x_fname);
144 binbuf_addv(b, ";");
148 t_widgetbehavior image_widgetbehavior;
150 void image_size(t_image* x,t_floatarg w,t_floatarg h) {
151 x->x_width = w;
152 x->x_height = h;
155 void image_color(t_image* x,t_symbol* col)
157 /* outlet_bang(x->x_obj.ob_outlet); only bang if there was a bang ..
158 so color black does the same as bang, but doesn't forward the bang
162 static void image_setwidget(void)
164 image_widgetbehavior.w_getrectfn = image_getrect;
165 image_widgetbehavior.w_displacefn = image_displace;
166 image_widgetbehavior.w_selectfn = image_select;
167 image_widgetbehavior.w_activatefn = image_activate;
168 image_widgetbehavior.w_deletefn = image_delete;
169 image_widgetbehavior.w_visfn = image_vis;
170 #if (PD_VERSION_MINOR > 31)
171 image_widgetbehavior.w_clickfn = NULL;
172 image_widgetbehavior.w_propertiesfn = NULL;
173 #endif
174 #if PD_MINOR_VERSION < 37
175 image_widgetbehavior.w_savefn = image_save;
176 #endif
180 static void *image_new(t_symbol* fname)
182 t_image *x = (t_image *)pd_new(image_class);
184 x->x_glist = (t_glist*) canvas_getcurrent();
186 x->x_width = 15;
187 x->x_height = 15;
189 x->x_fname = fname;
190 outlet_new(&x->x_obj, &s_float);
191 return (x);
194 void image_setup(void)
196 image_class = class_new(gensym("image"), (t_newmethod)image_new, 0,
197 sizeof(t_image),0, A_DEFSYM,0);
200 class_addmethod(image_class, (t_method)image_size, gensym("size"),
201 A_FLOAT, A_FLOAT, 0);
203 class_addmethod(image_class, (t_method)image_color, gensym("color"),
204 A_SYMBOL, 0);
207 class_addmethod(image_class, (t_method)image_open, gensym("open"),
208 A_SYMBOL, 0);
210 image_setwidget();
211 class_setwidget(image_class,&image_widgetbehavior);
212 #if PD_MINOR_VERSION >= 37
213 class_setsavefn(image_class,&image_save);
214 #endif
218 #include "m_pd.h"
219 #include "g_canvas.h"
221 #ifdef NT
222 #pragma warning( disable : 4244 )
223 #pragma warning( disable : 4305 )
224 #endif
226 /* ------------------------ image ----------------------------- */
228 static t_class *image_class;
230 typedef struct _image
232 t_object x_obj;
233 t_glist * x_glist;
234 int x_width;
235 int x_height;
236 t_symbol* x_fname;
237 } t_image;
239 /* widget helper functions */
241 void image_drawme(t_image *x, t_glist *glist, int firsttime)
243 if (firsttime) {
244 char fname[MAXPDSTRING];
245 canvas_makefilename(glist_getcanvas(x->x_glist), x->x_fname->s_name,
246 fname, MAXPDSTRING);
248 sys_vgui("image create photo img%x -file %s\n",x,fname);
249 sys_vgui(".x%x.c create image %d %d -image img%x -tags %xS\n",
250 glist_getcanvas(glist),text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x,x);
252 /* TODO callback from gui
253 sys_vgui("image_size logo");
256 else {
257 sys_vgui(".x%x.c coords %xS \
258 %d %d\n",
259 glist_getcanvas(glist), x,
260 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist));
266 void image_erase(t_image* x,t_glist* glist)
268 int n;
269 sys_vgui(".x%x.c delete %xS\n",
270 glist_getcanvas(glist), x);
276 /* ------------------------ image widgetbehaviour----------------------------- */
279 static void image_getrect(t_gobj *z, t_glist *glist,
280 int *xp1, int *yp1, int *xp2, int *yp2)
282 int width, height;
283 t_image* x = (t_image*)z;
286 width = x->x_width;
287 height = x->x_height;
288 *xp1 = text_xpix(&x->x_obj, glist);
289 *yp1 = text_ypix(&x->x_obj, glist);
290 *xp2 = text_xpix(&x->x_obj, glist) + width;
291 *yp2 = text_ypix(&x->x_obj, glist) + height;
294 static void image_displace(t_gobj *z, t_glist *glist,
295 int dx, int dy)
297 t_image *x = (t_image *)z;
298 x->x_obj.te_xpix += dx;
299 x->x_obj.te_ypix += dy;
300 sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n",
301 glist_getcanvas(glist), x,
302 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
303 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height);
305 image_drawme(x, glist, 0);
306 canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x);
309 static void image_select(t_gobj *z, t_glist *glist, int state)
311 t_image *x = (t_image *)z;
312 if (state) {
313 sys_vgui(".x%x.c create rectangle \
314 %d %d %d %d -tags %xSEL -outline blue\n",
315 glist_getcanvas(glist),
316 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
317 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height,
320 else {
321 sys_vgui(".x%x.c delete %xSEL\n",
322 glist_getcanvas(glist), x);
330 static void image_activate(t_gobj *z, t_glist *glist, int state)
332 /* t_text *x = (t_text *)z;
333 t_rtext *y = glist_findrtext(glist, x);
334 if (z->g_pd != gatom_class) rtext_activate(y, state);*/
337 static void image_delete(t_gobj *z, t_glist *glist)
339 t_text *x = (t_text *)z;
340 canvas_deletelinesfor(glist_getcanvas(glist), x);
344 static void image_vis(t_gobj *z, t_glist *glist, int vis)
346 t_image* s = (t_image*)z;
347 if (vis)
348 image_drawme(s, glist, 1);
349 else
350 image_erase(s,glist);
353 /* can we use the normal text save function ?? */
355 static void image_save(t_gobj *z, t_binbuf *b)
357 t_image *x = (t_image *)z;
358 binbuf_addv(b, "ssiiss", gensym("#X"),gensym("obj"),
359 x->x_obj.te_xpix, x->x_obj.te_ypix,
360 gensym("image"),x->x_fname);
361 binbuf_addv(b, ";");
365 t_widgetbehavior image_widgetbehavior;
367 void image_size(t_image* x,t_floatarg w,t_floatarg h) {
368 x->x_width = w;
369 x->x_height = h;
372 void image_color(t_image* x,t_symbol* col)
374 /* outlet_bang(x->x_obj.ob_outlet); only bang if there was a bang ..
375 so color black does the same as bang, but doesn't forward the bang
379 static void image_setwidget(void)
381 image_widgetbehavior.w_getrectfn = image_getrect;
382 image_widgetbehavior.w_displacefn = image_displace;
383 image_widgetbehavior.w_selectfn = image_select;
384 image_widgetbehavior.w_activatefn = image_activate;
385 image_widgetbehavior.w_deletefn = image_delete;
386 image_widgetbehavior.w_visfn = image_vis;
387 #if (PD_VERSION_MINOR > 31)
388 image_widgetbehavior.w_clickfn = NULL;
389 image_widgetbehavior.w_propertiesfn = NULL;
390 #endif
391 #if PD_MINOR_VERSION < 37
392 image_widgetbehavior.w_savefn = image_save;
393 #endif
397 static void *image_new(t_symbol* fname)
399 t_image *x = (t_image *)pd_new(image_class);
401 x->x_glist = (t_glist*) canvas_getcurrent();
403 x->x_width = 15;
404 x->x_height = 15;
406 x->x_fname = fname;
407 outlet_new(&x->x_obj, &s_float);
408 return (x);
411 void image_setup(void)
413 image_class = class_new(gensym("image"), (t_newmethod)image_new, 0,
414 sizeof(t_image),0, A_DEFSYM,0);
417 class_addmethod(image_class, (t_method)image_size, gensym("size"),
418 A_FLOAT, A_FLOAT, 0);
420 class_addmethod(image_class, (t_method)image_color, gensym("color"),
421 A_SYMBOL, 0);
424 class_addmethod(image_class, (t_method)image_open, gensym("open"),
425 A_SYMBOL, 0);
427 image_setwidget();
428 class_setwidget(image_class,&image_widgetbehavior);
429 #if PD_MINOR_VERSION >= 37
430 class_setsavefn(image_class,&image_save);
431 #endif