x_gui.c 20 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
/* Copyright (c) 1997-2000 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* dialogs.  LATER, deal with the situation where the object goes 
away before the panel does... */

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
8
9
#include "config.h"

Miller Puckette's avatar
Miller Puckette committed
10
#include "m_pd.h"
Ishan Kumar Kaler's avatar
Ishan Kumar Kaler committed
11
#include "g_canvas.h"
Miller Puckette's avatar
Miller Puckette committed
12
13
#include <stdio.h>
#include <string.h>
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
14
15

#ifdef HAVE_UNISTD_H
Miller Puckette's avatar
Miller Puckette committed
16
17
#include <unistd.h>
#endif
18
19
20
// jsarlo
EXTERN void magicGlass_setup(void);
// end jsarlo
Miller Puckette's avatar
Miller Puckette committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/* --------------------- graphics responder  ---------------- */

/* make one of these if you want to put up a dialog window but want to be
protected from getting deleted and then having the dialog call you back.  In
this design the calling object doesn't have to keep the address of the dialog
window around; instead we keep a list of all open dialogs.  Any object that
might have dialogs, when it is deleted, simply checks down the dialog window
list and breaks off any dialogs that might later have sent messages to it. 
Only when the dialog window itself closes do we delete the gfxstub object. */

static t_class *gfxstub_class;

typedef struct _gfxstub
{
    t_pd x_pd;
    t_pd *x_owner;
    void *x_key;
    t_symbol *x_sym;
    struct _gfxstub *x_next;
} t_gfxstub;

static t_gfxstub *gfxstub_list;

    /* create a new one.  the "key" is an address by which the owner
    will identify it later; if the owner only wants one dialog, this
    could just be a pointer to the owner itself.  The string "cmd"
    is a TK command to create the dialog, with "%s" embedded in
    it so we can provide a name by which the GUI can send us back
    messages; e.g., "pdtk_canvas_dofont %s 10". */

void gfxstub_new(t_pd *owner, void *key, const char *cmd)
{
    char buf[4*MAXPDSTRING];
    char namebuf[80];
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
56
57
58
    char sprintfbuf[MAXPDSTRING];
    char *afterpercent;
    t_int afterpercentlen;
Miller Puckette's avatar
Miller Puckette committed
59
60
61
62
63
64
65
66
67
    t_gfxstub *x;
    t_symbol *s;
        /* if any exists with matching key, burn it. */
    for (x = gfxstub_list; x; x = x->x_next)
        if (x->x_key == key)
            gfxstub_deleteforkey(key);
    if (strlen(cmd) + 50 > 4*MAXPDSTRING)
    {
        bug("audio dialog too long");
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
68
        bug("%s", cmd);
Miller Puckette's avatar
Miller Puckette committed
69
70
71
        return;
    }
    x = (t_gfxstub *)pd_new(gfxstub_class);
72
    sprintf(namebuf, ".gfxstub%zx", (t_uint)x);
Miller Puckette's avatar
Miller Puckette committed
73
74
75
76
77
78
79
80

    s = gensym(namebuf);
    pd_bind(&x->x_pd, s);
    x->x_owner = owner;
    x->x_sym = s;
    x->x_key = key;
    x->x_next = gfxstub_list;
    gfxstub_list = x;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
81
82
83
84
85
86
87
    /* only replace first %s so sprintf() doesn't crash */
    afterpercent = strchr(cmd, '%') + 2;
    afterpercentlen = afterpercent - cmd;
    strncpy(sprintfbuf, cmd, afterpercentlen);
    sprintfbuf[afterpercentlen] = '\0';
    sprintf(buf, sprintfbuf, s->s_name);
    strncat(buf, afterpercent, (4*MAXPDSTRING) - afterpercentlen);
Miller Puckette's avatar
Miller Puckette committed
88
89
90
    sys_gui(buf);
}

91
92
93
94
95
96
97
98
99
100
101
102
/* This is the new API for gfxstub.  We forgo all the
   string formatting junk and just return the string id */
char *gfxstub_new2(t_pd *owner, void *key)
{
    static char namebuf[80];
    t_gfxstub *x;
    t_symbol *s;
        /* if any exists with matching key, burn it. */
    for (x = gfxstub_list; x; x = x->x_next)
        if (x->x_key == key)
            gfxstub_deleteforkey(key);
    x = (t_gfxstub *)pd_new(gfxstub_class);
103
    sprintf(namebuf, ".gfxstub%zx", (t_uint)x);
104
105
106
107
108
109
110
111
112
113
114
115
116
    s = gensym(namebuf);
    pd_bind(&x->x_pd, s);
    x->x_owner = owner;
    x->x_sym = s;
    x->x_key = key;
    x->x_next = gfxstub_list;
    gfxstub_list = x;
    return (namebuf);
}




117
118
int gfxstub_haveproperties(void *key)
{
119
    t_gfxstub *x;
120
121
122
123
    for (x = gfxstub_list; x; x = x->x_next)
    {
        if (x->x_key == key)
        {
124
            return (t_int)x;
125
126
127
        }
    }
    return 0;
128
129
}

Miller Puckette's avatar
Miller Puckette committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
static void gfxstub_offlist(t_gfxstub *x)
{
    t_gfxstub *y1, *y2;
    if (gfxstub_list == x)
        gfxstub_list = x->x_next;
    else for (y1 = gfxstub_list; y2 = y1->x_next; y1 = y2)
        if (y2 == x) 
    {
        y1->x_next = y2->x_next;
        break;
    }
}

    /* if the owner disappears, we still may have to stay around until our
    dialog window signs off.  Anyway we can now tell the GUI to destroy the
    window.  */
void gfxstub_deleteforkey(void *key)
{
    t_gfxstub *y;
    int didit = 1;
    while (didit)
    {
        didit = 0;
        for (y = gfxstub_list; y; y = y->x_next)
        {
            if (y->x_key == key)
            {
157
                char tagbuf[MAXPDSTRING];
158
                sprintf(tagbuf, ".gfxstub%zx", (t_uint)y);
159
160
161
                gui_vmess("gui_remove_gfxstub", "s",
                    tagbuf);
                 
Miller Puckette's avatar
Miller Puckette committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
                y->x_owner = 0;
                gfxstub_offlist(y);
                didit = 1;
                break;
            }
        }
    }
}

/* --------- pd messages for gfxstub (these come from the GUI) ---------- */

    /* "cancel" to request that we close the dialog window. */
static void gfxstub_cancel(t_gfxstub *x)
{
    gfxstub_deleteforkey(x->x_key);
}

    /* "signoff" comes from the GUI to say the dialog window closed. */
static void gfxstub_signoff(t_gfxstub *x)
{
    gfxstub_offlist(x);
    pd_free(&x->x_pd);
}

static t_binbuf *gfxstub_binbuf;

    /* a series of "data" messages rebuilds a scalar */
static void gfxstub_data(t_gfxstub *x, t_symbol *s, int argc, t_atom *argv)
{
    if (!gfxstub_binbuf)
        gfxstub_binbuf = binbuf_new();
    binbuf_add(gfxstub_binbuf, argc, argv);
    binbuf_addsemi(gfxstub_binbuf);
}
    /* the "end" message terminates rebuilding the scalar */
static void gfxstub_end(t_gfxstub *x)
{
    canvas_dataproperties((t_canvas *)x->x_owner,
        (t_scalar *)x->x_key, gfxstub_binbuf);
    binbuf_free(gfxstub_binbuf);
    gfxstub_binbuf = 0;
}

    /* anything else is a message from the dialog window to the owner;
    just forward it. */
static void gfxstub_anything(t_gfxstub *x, t_symbol *s, int argc, t_atom *argv)
{
    if (x->x_owner)
        pd_typedmess(x->x_owner, s, argc, argv);
}

static void gfxstub_free(t_gfxstub *x)
{
    pd_unbind(&x->x_pd, x->x_sym);
}

static void gfxstub_setup(void)
{
220
    gfxstub_class = class_new(gensym("gfxstub"), 0, (t_method)gfxstub_free,
Miller Puckette's avatar
Miller Puckette committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
        sizeof(t_gfxstub), CLASS_PD, 0);
    class_addanything(gfxstub_class, gfxstub_anything);
    class_addmethod(gfxstub_class, (t_method)gfxstub_signoff,
        gensym("signoff"), 0);
    class_addmethod(gfxstub_class, (t_method)gfxstub_data,
        gensym("data"), A_GIMME, 0);
    class_addmethod(gfxstub_class, (t_method)gfxstub_end,
        gensym("end"), 0);
    class_addmethod(gfxstub_class, (t_method)gfxstub_cancel,
        gensym("cancel"), 0);
}

/* -------------------------- openpanel ------------------------------ */

static t_class *openpanel_class;

typedef struct _openpanel
{
    t_object x_obj;
240
    t_canvas *x_canvas;
Miller Puckette's avatar
Miller Puckette committed
241
242
243
244
245
246
247
    t_symbol *x_s;
} t_openpanel;

static void *openpanel_new( void)
{
    char buf[50];
    t_openpanel *x = (t_openpanel *)pd_new(openpanel_class);
248
    x->x_canvas = canvas_getcurrent();
249
    sprintf(buf, "d%zx", (t_uint)x);
Miller Puckette's avatar
Miller Puckette committed
250
251
252
253
254
255
256
257
    x->x_s = gensym(buf);
    pd_bind(&x->x_obj.ob_pd, x->x_s);
    outlet_new(&x->x_obj, &s_symbol);
    return (x);
}

static void openpanel_symbol(t_openpanel *x, t_symbol *s)
{
258
    char *path = (s && s->s_name) ? s->s_name : "$pd_opendir";
259
260
    gui_vmess("gui_openpanel", "xss",
        x->x_canvas,
261
262
        x->x_s->s_name,
        path);
Miller Puckette's avatar
Miller Puckette committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
}

static void openpanel_bang(t_openpanel *x)
{
    openpanel_symbol(x, &s_);
}

static void openpanel_callback(t_openpanel *x, t_symbol *s)
{
    outlet_symbol(x->x_obj.ob_outlet, s);
}


static void openpanel_free(t_openpanel *x)
{
    pd_unbind(&x->x_obj.ob_pd, x->x_s);
}

static void openpanel_setup(void)
{
    openpanel_class = class_new(gensym("openpanel"),
        (t_newmethod)openpanel_new, (t_method)openpanel_free,
        sizeof(t_openpanel), 0, 0);
    class_addbang(openpanel_class, openpanel_bang);
    class_addsymbol(openpanel_class, openpanel_symbol);
    class_addmethod(openpanel_class, (t_method)openpanel_callback,
        gensym("callback"), A_SYMBOL, 0);
}

/* -------------------------- savepanel ------------------------------ */

static t_class *savepanel_class;

typedef struct _savepanel
{
    t_object x_obj;
Miller Puckette's avatar
Miller Puckette committed
299
    t_canvas *x_canvas;
Miller Puckette's avatar
Miller Puckette committed
300
301
302
303
304
305
306
    t_symbol *x_s;
} t_savepanel;

static void *savepanel_new( void)
{
    char buf[50];
    t_savepanel *x = (t_savepanel *)pd_new(savepanel_class);
307
    sprintf(buf, "d%zx", (t_uint)x);
Miller Puckette's avatar
Miller Puckette committed
308
    x->x_s = gensym(buf);
Miller Puckette's avatar
Miller Puckette committed
309
    x->x_canvas = canvas_getcurrent();
Miller Puckette's avatar
Miller Puckette committed
310
311
312
313
314
315
316
    pd_bind(&x->x_obj.ob_pd, x->x_s);
    outlet_new(&x->x_obj, &s_symbol);
    return (x);
}

static void savepanel_symbol(t_savepanel *x, t_symbol *s)
{
317
    char *path = (s && s->s_name) ? s->s_name : "$pd_opendir";
318
319
    gui_vmess("gui_savepanel", "xss",
        x->x_canvas,
320
321
        x->x_s->s_name,
        path);
Miller Puckette's avatar
Miller Puckette committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
}

static void savepanel_bang(t_savepanel *x)
{
    savepanel_symbol(x, &s_);
}

static void savepanel_callback(t_savepanel *x, t_symbol *s)
{
    outlet_symbol(x->x_obj.ob_outlet, s);
}

static void savepanel_free(t_savepanel *x)
{
    pd_unbind(&x->x_obj.ob_pd, x->x_s);
}

static void savepanel_setup(void)
{
    savepanel_class = class_new(gensym("savepanel"),
        (t_newmethod)savepanel_new, (t_method)savepanel_free,
        sizeof(t_savepanel), 0, 0);
    class_addbang(savepanel_class, savepanel_bang);
    class_addsymbol(savepanel_class, savepanel_symbol);
    class_addmethod(savepanel_class, (t_method)savepanel_callback,
        gensym("callback"), A_SYMBOL, 0);
}

/* ---------------------- key and its relatives ------------------ */

static t_symbol *key_sym, *keyup_sym, *keyname_sym;
353
static t_symbol *key_sym_a, *keyup_sym_a, *keyname_sym_a;
Miller Puckette's avatar
Miller Puckette committed
354
355
356
357
358
static t_class *key_class, *keyup_class, *keyname_class;

typedef struct _key
{
    t_object x_obj;
359
    t_symbol *x_keysym;
Miller Puckette's avatar
Miller Puckette committed
360
361
} t_key;

362
static void *key_new(t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
363
364
365
{
    t_key *x = (t_key *)pd_new(key_class);
    outlet_new(&x->x_obj, &s_float);
366
367
368
369
370
371
372
373
374
    if (argc > 0 && argv->a_type == A_FLOAT)
        if (atom_getfloatarg(0, argc, argv) == 0)
            x->x_keysym = key_sym;
        else if (atom_getfloatarg(0, argc, argv) == 1)
            x->x_keysym = key_sym_a;

    if (!x->x_keysym)
        x->x_keysym = key_sym;
    pd_bind(&x->x_obj.ob_pd, x->x_keysym);
Miller Puckette's avatar
Miller Puckette committed
375
376
377
378
379
380
381
382
383
384
    return (x);
}

static void key_float(t_key *x, t_floatarg f)
{
    outlet_float(x->x_obj.ob_outlet, f);
}

static void key_free(t_key *x)
{
385
    pd_unbind(&x->x_obj.ob_pd, x->x_keysym);
Miller Puckette's avatar
Miller Puckette committed
386
387
388
389
390
}

typedef struct _keyup
{
    t_object x_obj;
391
    t_symbol *x_keysym;
Miller Puckette's avatar
Miller Puckette committed
392
393
} t_keyup;

394
static void *keyup_new(t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
395
396
397
{
    t_keyup *x = (t_keyup *)pd_new(keyup_class);
    outlet_new(&x->x_obj, &s_float);
398
399
400
401
402
403
404
405
406
    if (argc > 0 && argv->a_type == A_FLOAT)
        if (atom_getfloatarg(0, argc, argv) == 0)
            x->x_keysym = keyup_sym;
        else if (atom_getfloatarg(0, argc, argv) == 1)
            x->x_keysym = keyup_sym_a;

    if (!x->x_keysym)
        x->x_keysym = keyup_sym;
    pd_bind(&x->x_obj.ob_pd, x->x_keysym);
Miller Puckette's avatar
Miller Puckette committed
407
408
409
410
411
412
413
414
415
416
    return (x);
}

static void keyup_float(t_keyup *x, t_floatarg f)
{
    outlet_float(x->x_obj.ob_outlet, f);
}

static void keyup_free(t_keyup *x)
{
417
    pd_unbind(&x->x_obj.ob_pd, x->x_keysym);
Miller Puckette's avatar
Miller Puckette committed
418
419
420
421
422
}

typedef struct _keyname
{
    t_object x_obj;
423
    t_symbol *x_keysym;
Miller Puckette's avatar
Miller Puckette committed
424
425
426
427
    t_outlet *x_outlet1;
    t_outlet *x_outlet2;
} t_keyname;

428
static void *keyname_new(t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
429
430
431
432
{
    t_keyname *x = (t_keyname *)pd_new(keyname_class);
    x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet2 = outlet_new(&x->x_obj, &s_symbol);
433
434
435
436
437
438
439
440
441
    if (argc > 0 && argv->a_type == A_FLOAT)
        if (atom_getfloatarg(0, argc, argv) == 0)
            x->x_keysym = keyname_sym;
        else if (atom_getfloatarg(0, argc, argv) == 1)
            x->x_keysym = keyname_sym_a;

    if (!x->x_keysym)
        x->x_keysym = keyname_sym;
    pd_bind(&x->x_obj.ob_pd, x->x_keysym);
Miller Puckette's avatar
Miller Puckette committed
442
443
444
445
446
447
448
449
450
451
452
    return (x);
}

static void keyname_list(t_keyname *x, t_symbol *s, int ac, t_atom *av)
{
    outlet_symbol(x->x_outlet2, atom_getsymbolarg(1, ac, av));
    outlet_float(x->x_outlet1, atom_getfloatarg(0, ac, av));
}

static void keyname_free(t_keyname *x)
{
453
    pd_unbind(&x->x_obj.ob_pd, x->x_keysym);
Miller Puckette's avatar
Miller Puckette committed
454
455
456
457
458
459
}

static void key_setup(void)
{
    key_class = class_new(gensym("key"),
        (t_newmethod)key_new, (t_method)key_free,
460
        sizeof(t_key), 0, A_GIMME, 0);
Miller Puckette's avatar
Miller Puckette committed
461
462
    class_addfloat(key_class, key_float);
    key_sym = gensym("#key");
463
    key_sym_a = gensym("#key_a");
Miller Puckette's avatar
Miller Puckette committed
464
465
466

    keyup_class = class_new(gensym("keyup"),
        (t_newmethod)keyup_new, (t_method)keyup_free,
467
        sizeof(t_keyup), 0, A_GIMME, 0);
Miller Puckette's avatar
Miller Puckette committed
468
469
    class_addfloat(keyup_class, keyup_float);
    keyup_sym = gensym("#keyup");
470
    keyup_sym_a = gensym("#keyup_a");
471
    //class_sethelpsymbol(keyup_class, gensym("key"));
Miller Puckette's avatar
Miller Puckette committed
472
473
474
    
    keyname_class = class_new(gensym("keyname"),
        (t_newmethod)keyname_new, (t_method)keyname_free,
475
        sizeof(t_keyname), 0, A_GIMME, 0);
Miller Puckette's avatar
Miller Puckette committed
476
477
    class_addlist(keyname_class, keyname_list);
    keyname_sym = gensym("#keyname");
478
    keyname_sym_a = gensym("#keyname_a");
479
    //class_sethelpsymbol(keyname_class, gensym("key"));
Miller Puckette's avatar
Miller Puckette committed
480
481
}

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* ------------------ mouse classes for legacy externals ------------------ */

/* Every other legacy external library has some ad hoc code for getting
   mouse state within a Pd patch. All of them have different weird interfaces
   and some are outright buggy.

   Most of these return screen coordinates. This is unfortunately more of
   a pain than it should be in nw.js. Instead, we return window coordinates
   and hope that this is good enough for the uses to which these external
   classes have been put. At worst the user can make the relevant canvas
   full screen and get the desired behavior (minus the offset for the menu).

   Most of the uses for mouse coordinates seem to do with tutorials that map
   x/y positions to amplitude, frequency, etc. So these classes should be
   good enough to build abstractions to do an end run around the relevant
   externals.
*/

static t_symbol *mousemotion_sym, *mouseclick_sym, *mousewheel_sym;
static t_class *mousemotion_class, *mouseclick_class, *mousewheel_class;

typedef struct _mousemotion
{
    t_object x_obj;
    t_outlet *x_outlet1;
    t_outlet *x_outlet2;
} t_mousemotion;

static void *mousemotion_new( void)
{
    t_mousemotion *x = (t_mousemotion *)pd_new(mousemotion_class);
    x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
    pd_bind(&x->x_obj.ob_pd, mousemotion_sym);
    return (x);
}

static void mousemotion_list(t_mousemotion *x, t_symbol *s, int argc,
    t_atom *argv)
{
    outlet_float(x->x_outlet2, atom_getfloatarg(1, argc, argv));
    outlet_float(x->x_outlet1, atom_getfloatarg(0, argc, argv));
}

static void mousemotion_free(t_mousemotion *x)
{
    pd_unbind(&x->x_obj.ob_pd, mousemotion_sym);
}

typedef struct _mouseclick
{
    t_object x_obj;
    t_outlet *x_outlet1;
    t_outlet *x_outlet2;
    t_outlet *x_outlet3;
    t_outlet *x_outlet4;
} t_mouseclick;

static void *mouseclick_new( void)
{
    t_mouseclick *x = (t_mouseclick *)pd_new(mouseclick_class);
    x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet3 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet4 = outlet_new(&x->x_obj, &s_float);
    pd_bind(&x->x_obj.ob_pd, mouseclick_sym);
    return (x);
}

static void mouseclick_list(t_mouseclick *x, t_symbol *s, int argc,
    t_atom *argv)
{
    outlet_float(x->x_outlet4, atom_getfloatarg(3, argc, argv));
    outlet_float(x->x_outlet3, atom_getfloatarg(2, argc, argv));
    outlet_float(x->x_outlet2, atom_getfloatarg(1, argc, argv));
    outlet_float(x->x_outlet1, atom_getfloatarg(0, argc, argv));
}

static void mouseclick_free(t_mouseclick *x)
{
    pd_unbind(&x->x_obj.ob_pd, mouseclick_sym);
}

typedef struct _mousewheel
{
    t_object x_obj;
568
569
570
    t_outlet *x_outlet1;
    t_outlet *x_outlet2;
    t_outlet *x_outlet3;
571
572
573
574
575
} t_mousewheel;

static void *mousewheel_new( void)
{
    t_mousewheel *x = (t_mousewheel *)pd_new(mousewheel_class);
576
577
578
    x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
    x->x_outlet3 = outlet_new(&x->x_obj, &s_float);
579
580
581
582
    pd_bind(&x->x_obj.ob_pd, mousewheel_sym);
    return (x);
}

583
584
static void mousewheel_list(t_mousewheel *x, t_symbol *s, int argc,
    t_atom *argv)
585
{
586
587
588
    outlet_float(x->x_outlet3, atom_getfloatarg(2, argc, argv));
    outlet_float(x->x_outlet2, atom_getfloatarg(1, argc, argv));
    outlet_float(x->x_outlet1, atom_getfloatarg(0, argc, argv));
589
590
591
592
593
594
595
596
597
}

static void mousewheel_free(t_mousewheel *x)
{
    pd_unbind(&x->x_obj.ob_pd, mousewheel_sym);
}

static void mouse_setup(void)
{
598
    mousemotion_class = class_new(gensym("legacy_mousemotion"),
599
600
601
        (t_newmethod)mousemotion_new, (t_method)mousemotion_free,
        sizeof(t_mousemotion), CLASS_NOINLET, 0);
    class_addlist(mousemotion_class, mousemotion_list);
602
    mousemotion_sym = gensym("#legacy_mousemotion");
603

604
    mouseclick_class = class_new(gensym("legacy_mouseclick"),
605
606
607
        (t_newmethod)mouseclick_new, (t_method)mouseclick_free,
        sizeof(t_mouseclick), CLASS_NOINLET, 0);
    class_addlist(mouseclick_class, mouseclick_list);
608
    mouseclick_sym = gensym("#legacy_mouseclick");
609

610
    mousewheel_class = class_new(gensym("legacy_mousewheel"),
611
612
        (t_newmethod)mousewheel_new, (t_method)mousewheel_free,
        sizeof(t_mousewheel), CLASS_NOINLET, 0);
613
    class_addfloat(mousewheel_class, mousewheel_list);
614
    mousewheel_sym = gensym("#legacy_mousewheel");
615
616
}

Ishan Kumar Kaler's avatar
Ishan Kumar Kaler committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/* ------------------------ pdcontrol --------------------------------- */

static t_class *pdcontrol_class;

typedef struct _pdcontrol
{
    t_object x_obj;
    t_canvas *x_canvas;
    t_outlet *x_outlet;
} t_pdcontrol;

static void *pdcontrol_new( void)
{
    t_pdcontrol *x = (t_pdcontrol *)pd_new(pdcontrol_class);
    x->x_canvas = canvas_getcurrent();
    x->x_outlet = outlet_new(&x->x_obj, 0);
    return (x);
}

    /* output containing directory of patch.  optional args:
    1. a number, zero for this patch, one for the parent, etc.;
    2. a symbol to concatenate onto the directory; */

static void pdcontrol_dir(t_pdcontrol *x, t_symbol *s, t_floatarg f)
{
    t_canvas *c = x->x_canvas;
    int i;
    for (i = 0; i < (int)f; i++)
    {
        while (!c->gl_env)  /* back up to containing canvas or abstraction */
            c = c->gl_owner;
        if (c->gl_owner)    /* back up one more into an owner if any */
            c = c->gl_owner;
    }
    if (*s->s_name)
    {
        char buf[MAXPDSTRING];
        snprintf(buf, MAXPDSTRING, "%s/%s",
            canvas_getdir(c)->s_name, s->s_name);
        buf[MAXPDSTRING-1] = 0;
        outlet_symbol(x->x_outlet, gensym(buf));
    }
    else outlet_symbol(x->x_outlet, canvas_getdir(c));
}

static void pdcontrol_args(t_pdcontrol *x, t_floatarg f)
{
    t_canvas *c = x->x_canvas;
    int i;
    int argc;
    t_atom *argv;
    for (i = 0; i < (int)f; i++)
    {
        while (!c->gl_env)  /* back up to containing canvas or abstraction */
            c = c->gl_owner;
        if (c->gl_owner)    /* back up one more into an owner if any */
            c = c->gl_owner;
    }
    canvas_setcurrent(c);
    canvas_getargs(&argc, &argv);
    canvas_unsetcurrent(c);
    outlet_list(x->x_outlet, &s_list, argc, argv);
}

static void pdcontrol_browse(t_pdcontrol *x, t_symbol *s)
{
    gui_vmess("gui_pddplink_open", "ss", s->s_name, "");
}

static void pdcontrol_isvisible(t_pdcontrol *x)
{
    outlet_float(x->x_outlet, glist_isvisible(x->x_canvas));
}

static void pdcontrol_setup(void)
{
    pdcontrol_class = class_new(gensym("pdcontrol"),
        (t_newmethod)pdcontrol_new, 0, sizeof(t_pdcontrol), 0, 0);
    class_addmethod(pdcontrol_class, (t_method)pdcontrol_dir,
        gensym("dir"), A_DEFFLOAT, A_DEFSYMBOL, 0);
    class_addmethod(pdcontrol_class, (t_method)pdcontrol_args,
        gensym("args"), A_DEFFLOAT, 0);
    class_addmethod(pdcontrol_class, (t_method)pdcontrol_browse,
        gensym("browse"), A_SYMBOL, 0);
    class_addmethod(pdcontrol_class, (t_method)pdcontrol_isvisible,
        gensym("isvisible"), 0);
}

Miller Puckette's avatar
Miller Puckette committed
705
706
707
708
709
710
711
712
/* -------------------------- setup routine ------------------------------ */

void x_gui_setup(void)
{
    gfxstub_setup();
    openpanel_setup();
    savepanel_setup();
    key_setup();
713
    mouse_setup();
714
715
716
    // jsarlo
    magicGlass_setup();
    // end jsarlo
Ishan Kumar Kaler's avatar
Ishan Kumar Kaler committed
717
718
    pdcontrol_setup();
}