From b0114d623806df53bce55158e4ef89d62648da68 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 14 Nov 2007 03:51:35 +0000 Subject: got a quick working example pretty much straight from ggee's image.c; I need to get the shared dylib working before working on a new objectclass svn path=/trunk/externals/tkwidgets/; revision=9002 --- frosch.gif | Bin 0 -> 10278 bytes photo-help.pd | 27 ++++++ photo.c | 236 +++++++++++++++++++++++++++++++++++++++++++++++++++++ shared/tkwidgets.c | 16 ++++ shared/tkwidgets.h | 2 + text.c | 26 +++--- volga.gif | Bin 0 -> 14645 bytes 7 files changed, 294 insertions(+), 13 deletions(-) create mode 100644 frosch.gif create mode 100644 photo-help.pd create mode 100644 photo.c create mode 100644 volga.gif diff --git a/frosch.gif b/frosch.gif new file mode 100644 index 0000000..1c2fd41 Binary files /dev/null and b/frosch.gif differ diff --git a/photo-help.pd b/photo-help.pd new file mode 100644 index 0000000..af85b64 --- /dev/null +++ b/photo-help.pd @@ -0,0 +1,27 @@ +#N canvas 140 178 568 628 10; +#X text 19 18 Incorporate images. This is instantiated with; +#X msg 86 132 open \$1; +#X obj 86 88 bng 15 250 50 0 empty empty empty 0 -6 0 10 -4034 -1 -1 +; +#X obj 86 109 openpanel; +#X obj 66 555 bng 15 250 50 0 empty empty empty 0 -6 0 10 -262144 -1 +-1; +#X msg 265 152 size \$1 \$2; +#X obj 265 129 pack 0 0; +#X obj 313 106 hsl 128 15 0 127 0 0 empty empty y 7 7 0 10 -203904 +-1 -1 0 1; +#X obj 263 88 hsl 128 15 0 127 0 0 empty empty x 7 7 0 10 -204800 -1 +-1 0 1; +#X obj 309 337 photo frosch.gif; +#X obj 101 336 photo volga.gif; +#X text 19 34 [photo volga.gif]; +#X text 17 57 [photo] works with .gif \, .ppm \, and .pgm image formats +only.; +#X connect 1 0 10 0; +#X connect 2 0 3 0; +#X connect 3 0 1 0; +#X connect 5 0 10 0; +#X connect 6 0 5 0; +#X connect 7 0 6 1; +#X connect 8 0 6 0; +#X connect 10 0 4 0; diff --git a/photo.c b/photo.c new file mode 100644 index 0000000..09e542e --- /dev/null +++ b/photo.c @@ -0,0 +1,236 @@ +/* [photo] object for dislaying images in a patch + + Copyright (C) 2002-2004 Guenter Geiger + Copyright (C) 2007 Hans-Christoph Steiner + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + This is part of the tkwidgets library for Pd. + +*/ + +#include "shared/tkwidgets.h" + +/* ------------------------ photo ----------------------------- */ + +static t_class *photo_class; + +typedef struct _photo +{ + t_object x_obj; + t_glist* x_glist; + t_symbol* receive_name; + int x_width; + int x_height; + t_symbol* filename; +} t_photo; + +static char *photo_tk_options[] = { + "activeimage", + "disabledimage", + "gamma", + "image", + "state" +}; + +/* widget helper functions */ + + void photo_drawme(t_photo *x, t_glist *glist, int firsttime) +{ + if (firsttime) + { + char fname[MAXPDSTRING]; + canvas_makefilename(glist_getcanvas(x->x_glist), x->filename->s_name, + fname, MAXPDSTRING); + + sys_vgui("image create photo img%x -file {%s}\n", x, fname); + sys_vgui(".x%x.c create image %d %d -image img%x -anchor nw -tags %xS\n", + glist_getcanvas(glist),text_xpix(&x->x_obj, glist), + text_ypix(&x->x_obj, glist),x,x); + + /* TODO callback from gui + sys_vgui("photo_size logo"); + */ + } + else + { + sys_vgui(".x%x.c coords %xS %d %d\n", + glist_getcanvas(glist), x, + text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)); + } + +} + + +void photo_erase(t_photo* x,t_glist* glist) +{ + int n; + sys_vgui(".x%x.c delete %xS\n", + glist_getcanvas(glist), x); + +} + + + +/* ------------------------ photo widgetbehaviour----------------------------- */ + + +static void photo_getrect(t_gobj *z, t_glist *glist, + int *xp1, int *yp1, int *xp2, int *yp2) +{ + t_photo* x = (t_photo*)z; + + *xp1 = text_xpix(&x->x_obj, glist); + *yp1 = text_ypix(&x->x_obj, glist); + *xp2 = text_xpix(&x->x_obj, glist) + x->x_width; + *yp2 = text_ypix(&x->x_obj, glist) + x->x_height; +} + +static void photo_displace(t_gobj *z, t_glist *glist, + int dx, int dy) +{ + t_photo *x = (t_photo *)z; + x->x_obj.te_xpix += dx; + x->x_obj.te_ypix += dy; + sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n", + glist_getcanvas(glist), x, + text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), + text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height); + + photo_drawme(x, glist, 0); + canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); +} + +static void photo_select(t_gobj *z, t_glist *glist, int state) +{ + t_photo *x = (t_photo *)z; + if (state) { + sys_vgui(".x%x.c create rectangle \ +%d %d %d %d -tags %xSEL -outline blue\n", + glist_getcanvas(glist), + text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), + text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height, + x); + } + else + { + sys_vgui(".x%x.c delete %xSEL\n", + glist_getcanvas(glist), x); + } +} + + +static void photo_activate(t_gobj *z, t_glist *glist, int state) +{ +/* t_text *x = (t_text *)z; + t_rtext *y = glist_findrtext(glist, x); + if (z->g_pd != gatom_class) rtext_activate(y, state);*/ +} + +static void photo_delete(t_gobj *z, t_glist *glist) +{ + t_text *x = (t_text *)z; + canvas_deletelinesfor(glist_getcanvas(glist), x); +} + + +static void photo_vis(t_gobj *z, t_glist *glist, int vis) +{ + t_photo* s = (t_photo*)z; + if (vis) + photo_drawme(s, glist, 1); + else + photo_erase(s,glist); +} + +/* can we use the normal text save function ?? */ + +static void photo_save(t_gobj *z, t_binbuf *b) +{ + t_photo *x = (t_photo *)z; + binbuf_addv(b, "ssiiss;", gensym("#X"), gensym("obj"), + x->x_obj.te_xpix, x->x_obj.te_ypix, + atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)), + x->filename); +} + + +void photo_size(t_photo* x,t_floatarg w,t_floatarg h) { + x->x_width = w; + x->x_height = h; +} + +void photo_open(t_photo* x, t_symbol* fname) +{ + x->filename = fname; + photo_erase(x, x->x_glist); + photo_drawme(x, x->x_glist, 1); +} + + +static void photo_free(t_photo *x) +{ + pd_unbind(&x->x_obj.ob_pd, x->receive_name); +} + +static void *photo_new(t_symbol* s) +{ + t_photo *x = (t_photo *)pd_new(photo_class); + + x->x_glist = (t_glist*) canvas_getcurrent(); + + x->x_width = 15; + x->x_height = 15; + + x->x_glist = canvas_getcurrent(); + + x->filename = s; + outlet_new(&x->x_obj, &s_float); + return (x); +} + + +static t_widgetbehavior photo_widgetbehavior = { +w_getrectfn: photo_getrect, +w_displacefn: photo_displace, +w_selectfn: photo_select, +w_activatefn: photo_activate, +w_deletefn: photo_delete, +w_visfn: photo_vis, +w_clickfn: NULL, +}; + +void photo_setup(void) +{ + photo_class = class_new(gensym("photo"), (t_newmethod)photo_new, + (t_method)photo_free, + sizeof(t_photo), 0, A_DEFSYM,0); + + class_addmethod(photo_class, (t_method)photo_size, gensym("size"), + A_FLOAT, A_FLOAT, 0); + +/* + class_addmethod(photo_class, (t_method)photo_color, gensym("color"), + A_SYMBOL, 0); +*/ + + class_addmethod(photo_class, (t_method)photo_open, gensym("open"), + A_SYMBOL, 0); + + class_setwidget(photo_class, &photo_widgetbehavior); + class_setsavefn(photo_class, &photo_save); +} + + diff --git a/shared/tkwidgets.c b/shared/tkwidgets.c index 13fe311..6439842 100644 --- a/shared/tkwidgets.c +++ b/shared/tkwidgets.c @@ -22,6 +22,7 @@ #include "tkwidgets.h" #include +#include /* this should be part of the Pd API */ t_symbol *canvas_getname(t_canvas *canvas) @@ -31,6 +32,21 @@ t_symbol *canvas_getname(t_canvas *canvas) return gensym(buf); } +/* +I think I'll probably have to follow Krzsztof and make structs to make this work +tkwidgets_setcallbackname(void *x, char *widget_name) +{ + char buf[MAXPDSTRING]; + + sprintf(buf,"%s%lx", widget_name, (long unsigned int)x); + x->tcl_namespace = getbytes(strlen(buf)); + strcpy(x->tcl_namespace, buf); + + sprintf(buf,"#%s", x->tcl_namespace); + x->receive_name = gensym(buf); + pd_bind(&x->x_obj.ob_pd, x->receive_name); +} +*/ void draw_inlets(t_object *x, t_glist *glist, int firsttime, int total_inlets, int total_outlets) diff --git a/shared/tkwidgets.h b/shared/tkwidgets.h index 7ecd521..9365a7c 100644 --- a/shared/tkwidgets.h +++ b/shared/tkwidgets.h @@ -40,6 +40,8 @@ /* this should be part of the Pd API */ t_symbol *canvas_getname(t_canvas *canvas); +void tkwidgets_setcallbackname(void *x, char *widget_name); + // TODO perhaps I should try to use glist_drawiofor() from g_text.c void draw_inlets(t_object *x, t_glist *glist, int firsttime, int total_inlets, int total_outlets); diff --git a/text.c b/text.c index b77ec5e..77bce2a 100644 --- a/text.c +++ b/text.c @@ -45,7 +45,7 @@ typedef struct _textwidget t_canvas* x_canvas; t_glist* x_glist; - t_symbol* x_receive_name; + t_symbol* receive_name; int x_height; int x_width; @@ -76,7 +76,7 @@ typedef struct _textwidget static t_class *textwidget_class; -static char *tk_options[] = { +static char *textwidget_tk_options[] = { "autoseparators", "background", "borderwidth", @@ -308,7 +308,7 @@ static void create_widget(t_textwidget *x) bind_standard_keys(x); bind_button_events(x); sys_vgui("bind %s {+pd %s keyup %%N \\;} \n", - x->text_id, x->x_receive_name->s_name); + x->text_id, x->receive_name->s_name); } static void textwidget_drawme(t_textwidget *x, t_glist *glist, int firsttime) @@ -427,11 +427,11 @@ static void textwidget_activate(t_gobj *z, t_glist *glist, int state) x->handle_id, x->all_tag); sys_vgui("raise %s\n", x->handle_id); sys_vgui("bind %s