aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans-Christoph Steiner <eighthave@users.sourceforge.net>2011-10-23 03:46:41 +0000
committerHans-Christoph Steiner <eighthave@users.sourceforge.net>2011-10-23 03:46:41 +0000
commit0aa52cf06bbdf0983159ad63661680d8ce617e65 (patch)
tree45c5fa1c3e57269c4bdf29e1e83a7e660f1764fb
parent2a584e4c004f83c55b09a5f1fb76db79813851a8 (diff)
allow getting canvas name using names or depth, via args or the inlet
svn path=/trunk/externals/hcs/; revision=15642
-rw-r--r--canvas_name-help.pd126
-rw-r--r--canvas_name.c105
2 files changed, 158 insertions, 73 deletions
diff --git a/canvas_name-help.pd b/canvas_name-help.pd
index a5b718a..5d1c03e 100644
--- a/canvas_name-help.pd
+++ b/canvas_name-help.pd
@@ -1,4 +1,4 @@
-#N canvas 0 26 479 386 10;
+#N canvas 178 98 493 398 10;
#X obj 136 290 sys_gui;
#X symbolatom 254 206 0 0 0 0 - - -;
#X obj 241 145 loadbang;
@@ -48,7 +48,7 @@
#X msg 148 162 white;
#X msg 158 183 pink;
#X msg 162 205 black;
-#N canvas 373 75 457 313 get 0;
+#N canvas 660 76 459 460 get 0;
#X symbolatom 42 225 0 0 0 0 - - -;
#X obj 42 204 canvas_name;
#X obj 42 181 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
@@ -66,13 +66,6 @@
#X symbolatom 209 225 0 0 0 0 - - -;
#X obj 209 181 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
-1 -1;
-#N canvas 496 269 450 300 get 0;
-#X obj 117 21 inlet;
-#X obj 119 265 outlet;
-#X obj 109 137 canvas_name 1;
-#X connect 0 0 2 0;
-#X connect 2 0 1 0;
-#X restore 209 204 pd get parent;
#X symbolatom 73 118 0 0 0 0 - - -;
#X obj 73 74 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
-1;
@@ -84,14 +77,53 @@ one is embedded in by setting the argument to the level:;
#X symbolatom 299 225 0 0 0 0 - - -;
#X obj 299 181 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
-1 -1;
-#N canvas 496 269 450 300 get 0;
+#X text 43 268 some more examples:;
+#X text 35 308 get the canvas name using a float message representing
+depth;
+#X obj 94 363 canvas_name;
+#X symbolatom 93 389 10 0 0 0 - - -;
+#X msg 78 335 0;
+#X msg 108 335 1;
+#N canvas 0 22 450 300 open 0;
+#N canvas 511 55 450 300 open 0;
+#X obj 131 37 inlet;
+#X obj 129 109 outlet;
+#N canvas 772 55 450 300 open 0;
+#X obj 131 37 inlet;
+#X obj 129 109 outlet;
+#N canvas 0 22 450 300 open 0;
+#X obj 141 47 inlet;
+#X obj 139 119 outlet;
+#X obj 143 84 canvas_name;
+#X connect 0 0 2 0;
+#X connect 2 0 1 0;
+#X restore 128 76 pd open me;
+#X connect 0 0 2 0;
+#X connect 2 0 1 0;
+#X restore 129 74 pd open me;
+#X connect 0 0 2 0;
+#X connect 2 0 1 0;
+#X restore 138 87 pd open me;
+#X obj 141 47 inlet;
+#X obj 139 119 outlet;
+#X connect 0 0 2 0;
+#X connect 1 0 0 0;
+#X restore 260 382 pd open me;
+#N canvas 496 269 450 300 get_parent 0;
+#X obj 117 21 inlet;
+#X obj 119 265 outlet;
+#X obj 109 137 canvas_name 1;
+#X connect 0 0 2 0;
+#X connect 2 0 1 0;
+#X restore 209 204 pd get_parent;
+#N canvas 496 269 450 300 get_parent_of_parent 0;
#X obj 117 21 inlet;
#X obj 119 265 outlet;
#X obj 109 137 canvas_name 2;
#X connect 0 0 2 0;
#X connect 2 0 1 0;
-#X restore 299 204 pd get parent of parent;
-#N canvas 169 157 481 329 get 0;
+#X restore 299 204 pd get_parent_of_parent;
+#N canvas 169 157 481 329 get-from-parent 0;
#X symbolatom 114 120 0 0 0 0 - - -;
#X obj 101 96 canvas_name 1;
#X symbolatom 119 205 0 0 0 0 - - -;
@@ -130,30 +162,36 @@ how many levels up you want to get:;
#X connect 3 0 6 0;
#X connect 4 0 2 0;
#X connect 6 0 5 0;
-#X restore 183 267 pd get from parent;
-#X text 43 268 some more examples:;
+#X restore 183 267 pd get-from-parent;
+#X symbolatom 260 406 10 0 0 0 - - -;
+#X obj 260 338 hradio 15 1 0 8 empty empty empty 0 -8 0 10 -262144
+-1 -1 0;
+#X text 121 429 .x0.c means a non-existant window;
+#X floatatom 271 360 5 0 0 0 - - -;
+#X msg 139 335 2;
#X connect 1 0 0 0;
#X connect 2 0 1 0;
#X connect 4 0 5 0;
#X connect 5 0 3 0;
-#X connect 7 0 8 0;
-#X connect 8 0 6 0;
-#X connect 10 0 12 0;
-#X connect 12 0 9 0;
-#X connect 15 0 16 0;
-#X connect 16 0 14 0;
+#X connect 7 0 22 0;
+#X connect 9 0 11 0;
+#X connect 11 0 8 0;
+#X connect 14 0 23 0;
+#X connect 17 0 18 0;
+#X connect 19 0 17 0;
+#X connect 20 0 17 0;
+#X connect 21 0 25 0;
+#X connect 22 0 6 0;
+#X connect 23 0 13 0;
+#X connect 26 0 21 0;
+#X connect 26 0 28 0;
+#X connect 29 0 17 0;
#X restore 265 51 pd get from different levels;
-#N canvas 0 22 450 300 get 0;
-#X symbolatom 73 259 0 0 0 0 - - -;
-#X obj 73 215 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
--1;
+#N canvas 196 75 460 490 get 0;
+#X symbolatom 77 428 0 0 0 0 - - -;
#X symbolatom 72 176 0 0 0 0 - - -;
#X obj 72 132 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
-1;
-#X obj 72 155 canvas_name pd-test;
-#X obj 73 238 canvas_name pd-canvas_name-help.pd;
-#N canvas 0 22 450 300 test 0;
-#X restore 221 155 pd test;
#X obj 220 77 namecanvas myname;
#X symbolatom 73 99 0 0 0 0 - - -;
#X obj 73 55 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
@@ -161,12 +199,32 @@ how many levels up you want to get:;
#X obj 73 78 canvas_name myname;
#X text 53 14 You can also use the canvas receive symbol to get the
canvas_name:;
-#X connect 1 0 5 0;
-#X connect 3 0 4 0;
-#X connect 4 0 2 0;
-#X connect 5 0 0 0;
-#X connect 9 0 10 0;
-#X connect 10 0 8 0;
+#X obj 77 407 canvas_name;
+#X msg 129 376 pd-canvas_name-help.pd;
+#X symbolatom 71 275 0 0 0 0 - - -;
+#X obj 71 229 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 71 254 canvas_name pd-canvas_name-help.pd;
+#X text 65 213 this help patch:;
+#X text 49 302 or via the inlet:;
+#N canvas 0 22 450 300 getmyname 0;
+#X restore 250 155 pd getmyname;
+#X obj 72 155 canvas_name pd-getmyname;
+#X msg 79 351 myname;
+#X msg 64 325 symbol this_does_not_exist;
+#X text 91 459 .x0.c means a non-existant window;
+#X msg 129 351 symbol pd-getmyname;
+#X connect 2 0 16 0;
+#X connect 5 0 6 0;
+#X connect 6 0 4 0;
+#X connect 8 0 0 0;
+#X connect 9 0 8 0;
+#X connect 11 0 12 0;
+#X connect 12 0 10 0;
+#X connect 16 0 1 0;
+#X connect 17 0 8 0;
+#X connect 18 0 8 0;
+#X connect 20 0 8 0;
#X restore 265 76 pd get by canvas receive symbol;
#N canvas 410 155 494 344 META 0;
#X text 12 125 HELP_PATCH_AUTHORS "pd meta" information added by Jonathan
diff --git a/canvas_name.c b/canvas_name.c
index 4f2f88c..cf27875 100644
--- a/canvas_name.c
+++ b/canvas_name.c
@@ -3,65 +3,91 @@
#include <m_pd.h>
#include "g_canvas.h"
+/* TODO make the query level settable by 2nd inlet */
+
#define DEBUG(x)
static t_class *canvas_name_class;
-static t_canvas *canvas;
typedef struct _canvas_name
{
- t_object x_obj;
- t_atom x_atom;
- t_symbol *x_canvas_name;
- t_symbol *x_remote_name;
+ t_object x_obj;
+ t_canvas *x_canvas;
+ t_symbol *receive_name;
+ t_int depth;
} t_canvas_name;
-static void canvas_name_bang(t_canvas_name *x)
+static t_symbol* make_canvas_symbol(t_canvas* canvas)
{
- if (x->x_atom.a_type == A_SYMBOL)
- {
- canvas=(t_canvas *)pd_findbyclass(x->x_remote_name, canvas_class);
char buf[MAXPDSTRING];
-
snprintf(buf, MAXPDSTRING, ".x%lx.c", (long unsigned int)canvas);
- x->x_canvas_name = gensym(buf);
- }
- outlet_symbol(x->x_obj.ob_outlet,x->x_canvas_name);
+ return gensym(buf);
}
-static void *canvas_name_new(t_symbol *s, int argc, t_atom *argv)
+static t_symbol* get_canvas_by_depth(t_canvas_name *x)
{
- t_atom a;
- if (argc == 0)
+ t_canvas *canvas = x->x_canvas;
+ int depth = x->depth;
+ if(depth<0)depth=0;
+ while(depth && canvas)
{
- argc = 1;
- SETFLOAT(&a, 0);
- argv = &a;
+ canvas = canvas->gl_owner;
+ depth--;
}
- t_canvas_name *x = (t_canvas_name *)pd_new(canvas_name_class);
- x->x_atom = *argv;
- if (argv->a_type == A_FLOAT)
- { // thx to IOhannes's iemguts:
- t_glist *glist=(t_glist *)canvas_getcurrent();
- canvas=(t_canvas *)glist_getcanvas(glist);
- int depth=(int)atom_getint(&x->x_atom);
+ return make_canvas_symbol(canvas);
+}
- if(depth<0)depth=0;
- while(depth && canvas) {
- canvas=canvas->gl_owner;
- depth--;
- }
- char buf[MAXPDSTRING];
+static t_symbol* get_canvas_by_name(t_canvas_name *x)
+{
+ return make_canvas_symbol((t_canvas *)pd_findbyclass(x->receive_name, canvas_class));
+}
- snprintf(buf, MAXPDSTRING, ".x%lx.c", (long unsigned int)canvas);
- x->x_canvas_name = gensym(buf);
- }
+static t_symbol* get_canvas_name(t_canvas_name *x)
+{
+ if(x->receive_name == &s_)
+ return get_canvas_by_depth(x);
else
- {
- x->x_remote_name = (t_symbol *)atom_getsymbol(&x->x_atom);
- }
+ return get_canvas_by_name(x);
+}
+
+static void canvas_name_bang(t_canvas_name *x)
+{
+ /* actually get the canvas name each time to make sure we get
+ * accurate info in case of changes. If we cache the result, a
+ * canvas could have been deleted or renamed. */
+ outlet_symbol(x->x_obj.ob_outlet, get_canvas_name(x));
+}
+
+static void canvas_name_anything(t_canvas_name *x, t_symbol *s, int argc, t_atom *argv)
+{
+ t_symbol *first_symbol = atom_getsymbolarg(0,argc,argv);
+ x->depth = 0;
+ x->receive_name = &s_;
+ if(s == &s_float)
+ x->depth = (t_int) atom_getfloatarg(0,argc,argv);
+ else if (first_symbol == &s_)
+ x->receive_name = s;
+ else
+ x->receive_name = first_symbol;
+ canvas_name_bang(x);
+}
+
+static void *canvas_name_new(t_symbol *s, int argc, t_atom *argv)
+{
+ t_canvas_name *x = (t_canvas_name *)pd_new(canvas_name_class);
+
+ t_glist* glist = (t_glist *)canvas_getcurrent();
+ x->x_canvas = (t_canvas *)glist_getcanvas(glist);
+
+ t_symbol *tmp = atom_getsymbolarg(0,argc,argv);
+ x->depth = 0;
+ x->receive_name = &s_;
+ if(tmp == &s_)
+ x->depth = (t_int) atom_getfloatarg(0,argc,argv);
+ else
+ x->receive_name = tmp;
- outlet_new(&x->x_obj, &s_symbol);
+ outlet_new(&x->x_obj, &s_symbol);
return(x);
}
@@ -73,4 +99,5 @@ void canvas_name_setup(void)
sizeof(t_canvas_name), 0, A_GIMME, 0);
class_addbang(canvas_name_class, (t_method)canvas_name_bang);
+ class_addanything(canvas_name_class, (t_method)canvas_name_anything);
}