grid.c 29.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Copyright (c) 1997-1999 Miller Puckette.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */

/* g_grid.c written by Yves Degoyon 2002                                       */
/* grid control object : two dimensionnal grid                                 */
/* thanks to Thomas Musil, Miller Puckette, Guenther Geiger and Krzystof Czaja */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "m_pd.h"
#include "g_canvas.h"

#include "g_grid.h"

19
20
21
/* For the scalehandle... */
#include "g_all_guis.h"

22
23
24
25
26
27
28
29
30
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

#define DEFAULT_GRID_WIDTH 200
#define DEFAULT_GRID_HEIGHT 200
#define DEFAULT_GRID_NBLINES 10
31
32
#define MIN_GRID_WIDTH 20
#define MIN_GRID_HEIGHT 20
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

t_widgetbehavior grid_widgetbehavior;
static t_class *grid_class;
static int gridcount=0;

static int guidebug=0;
static int pointsize = 5;

static char   *grid_version = "grid: version 0.9, written by Yves Degoyon (ydegoyon@free.fr)";

#define GRID_SYS_VGUI2(a,b) if (guidebug) \
                         post(a,b);\
                         sys_vgui(a,b)

#define GRID_SYS_VGUI3(a,b,c) if (guidebug) \
                         post(a,b,c);\
                         sys_vgui(a,b,c)

#define GRID_SYS_VGUI4(a,b,c,d) if (guidebug) \
                         post(a,b,c,d);\
                         sys_vgui(a,b,c,d)

#define GRID_SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
                         post(a,b,c,d,e,f,g );\
                         sys_vgui(a,b,c,d,e,f,g)

#define GRID_SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
                         post(a,b,c,d,e,f,g,h);\
                         sys_vgui(a,b,c,d,e,f,g,h)

#define GRID_SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
                         post(a,b,c,d,e,f,g,h,i );\
                         sys_vgui(a,b,c,d,e,f,g,h,i)

/* drawing functions */
static void grid_draw_update(t_grid *x, t_glist *glist)
{
    t_canvas *canvas=glist_getcanvas(glist);
    t_int xpoint=x->x_current, ypoint=x->y_current;

73
    if (glist_isvisible(glist))
74
    {
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        // delete previous point if existing
        //if (x->x_point)
        //{
        //   GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
        //}

        if (x->x_current < text_xpix(&x->x_obj, glist))
            xpoint = text_xpix(&x->x_obj, glist);
        if (x->x_current > text_xpix(&x->x_obj, glist) + x->x_width - pointsize)
            xpoint = text_xpix(&x->x_obj, glist) + x->x_width - pointsize;
        if (x->y_current < text_ypix(&x->x_obj, glist))
            ypoint = text_ypix(&x->x_obj, glist);
        if (x->y_current > text_ypix(&x->x_obj, glist) + x->x_height - pointsize)
            ypoint = text_ypix(&x->x_obj, glist) + x->x_height - pointsize;
        // draw the selected point
        //GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d "
        //               "-fill #FF0000 -tags %lxPOINT\n",
        //    canvas, xpoint, ypoint, xpoint+pointsize, ypoint+pointsize, x);
        /* move the point */
        gui_vmess("gui_grid_point", "xxii",
            canvas,
            x,
            xpoint - text_xpix(&x->x_obj, glist),
            ypoint - text_ypix(&x->x_obj, glist));
        x->x_point = 1;
100
101
102
103
104
105
106
    }
    // else
    // {
    //    post( "grid : position updated in an invisible grid" );
    // }
}

107
108
static void grid_draw_select(t_grid* x, t_glist* glist);
static void grid_draw_configure(t_grid *x, t_glist *glist)
109
110
{
    t_canvas *canvas=glist_getcanvas(glist);
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
157
158
159
160
161
162
163
164
165
166
167
    //GRID_SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d "
    //               "-fill %s -tags %lxGRID\n",
    //    canvas, 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->x_bgcolor, x);
    //GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo0\n",
    //    canvas, text_xpix(&x->x_obj, glist),
    //    text_ypix(&x->x_obj, glist) + x->x_height+1,
    //    text_xpix(&x->x_obj, glist)+7,
    //    text_ypix(&x->x_obj, glist) + x->x_height+2, x);
    //GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo1\n",
    //    canvas, text_xpix(&x->x_obj, glist) + x->x_width - 7,
    //    text_ypix(&x->x_obj, glist) + x->x_height + 1,
    //    text_xpix(&x->x_obj, glist) + x->x_width,
    //    text_ypix(&x->x_obj, glist) + x->x_height + 2,
    //    x);
    //if (x->x_grid)
    //{
    //    int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
    //    int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
    //    int xcount = 1;
    //    int ycount = 1;
    //    while ( xlpos < text_xpix(&x->x_obj, glist)+x->x_width )
    //    {
    //        GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d "
    //                       "-fill #FFFFFF -tags %lxLINE%d%d\n",
    //            canvas, xlpos, text_ypix(&x->x_obj, glist),
    //            xlpos, text_ypix(&x->x_obj, glist) + x->x_height,
    //            x, xcount, 0);
    //        xlpos += x->x_width / x->x_xlines;
    //        xcount++;
    //    }
    //    while (ylpos < text_ypix(&x->x_obj, glist)+x->x_height)
    //    {
    //        GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d "
    //                       "-fill #FFFFFF -tags %lxLINE%d%d\n",
    //            canvas, text_xpix(&x->x_obj, glist), ylpos,
    //            text_xpix(&x->x_obj, glist)+x->x_width, ylpos,
    //            x, 0, ycount);
    //        ylpos += x->x_height / x->x_ylines;
    //        ycount++;
    //    }
    //}

    // gui_gobj_new, "xx type text_xpix text_ypix istoplevel"
    gui_vmess("gui_configure_grid", "xxiisiii",
        canvas,
        x,
        x->x_width,
        x->x_height,
        x->x_bgcolor,
        x->x_grid,
        x->x_xlines,
        x->x_ylines);
    if (glist_isselected(glist, &x->x_obj))
        grid_draw_select(x, glist);
168
169
170
    canvas_fixlinesfor( canvas, (t_text*)x );
}

171
172
173
174
175
176
177
178
179
180
181
182
static void grid_draw_new(t_grid *x, t_glist *glist)
{
    t_canvas *canvas=glist_getcanvas(glist);
    gui_vmess("gui_grid_new", "xxiii",
        canvas,
        x,
        text_xpix(&x->x_obj, glist),
        text_ypix(&x->x_obj, glist),
        glist_istoplevel(glist));
    grid_draw_configure(x, glist);
}

183
184
185
186
static void grid_draw_move(t_grid *x, t_glist *glist)
{
    t_canvas *canvas=glist_getcanvas(glist);

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    //GRID_SYS_VGUI7(".x%lx.c coords %lxGRID %d %d %d %d\n",
    //    canvas, 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);
    //GRID_SYS_VGUI7(".x%lx.c coords %lxo0 %d %d %d %d\n",
    //    canvas, x,
    //    text_xpix(&x->x_obj, glist),
    //    text_ypix(&x->x_obj, glist) + x->x_height + 1,
    //    text_xpix(&x->x_obj, glist) + 7,
    //    text_ypix(&x->x_obj, glist) + x->x_height + 2);
    //GRID_SYS_VGUI7(".x%lx.c coords %lxo1 %d %d %d %d\n",
    //    canvas, x,
    //    text_xpix(&x->x_obj, glist) + x->x_width - 7,
    //    text_ypix(&x->x_obj, glist) + x->x_height + 1,
    //    text_xpix(&x->x_obj, glist) + x->x_width,
    //    text_ypix(&x->x_obj, glist) + x->x_height + 2);
    if (x->x_point)
205
206
207
    {
        grid_draw_update(x, glist);
    }
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    //if (x->x_grid)
    //{
    //    int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
    //    int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
    //    int xcount = 1;
    //    int ycount = 1;
    //    while (xlpos < text_xpix(&x->x_obj, glist) + x->x_width)
    //    {
    //        GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
    //            canvas, x, xcount, 0, xlpos, text_ypix(&x->x_obj, glist),
    //            xlpos, text_ypix(&x->x_obj, glist) + x->x_height);
    //            xlpos+=x->x_width/x->x_xlines;
    //        xcount++;
    //    }
    //    while (ylpos < text_ypix(&x->x_obj, glist) + x->x_height)
    //    {
    //        GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
    //            canvas, x, 0, ycount, text_xpix(&x->x_obj, glist), ylpos,
    //            text_xpix(&x->x_obj, glist) + x->x_width, ylpos);
    //            ylpos+=x->x_height/x->x_ylines;
    //        ycount++;
    //    }
    //}
231
232
233
    canvas_fixlinesfor( canvas, (t_text*)x );
}

234
static void grid_draw_erase(t_grid* x, t_glist* glist)
235
{
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    t_canvas *canvas = glist_getcanvas(glist);

    gui_vmess("gui_gobj_erase", "xx",
        canvas, x);
    //int i;

    //GRID_SYS_VGUI3(".x%lx.c delete %lxGRID\n", canvas, x);
    //GRID_SYS_VGUI3(".x%lx.c delete %lxo0\n", canvas, x);
    //GRID_SYS_VGUI3(".x%lx.c delete %lxo1\n", canvas, x);
    //if (x->x_grid)
    //{
    //    for (i=1; i<x->x_xlines; i++)
    //    {
    //        GRID_SYS_VGUI4(".x%lx.c delete %lxLINE%d0\n", canvas, x, i);
    //    }
    //    for (i=1; i<x->x_ylines; i++)
    //    {
    //        GRID_SYS_VGUI4(".x%lx.c delete %lxLINE0%d\n", canvas, x, i);
    //    }
    //}
    //if (x->x_point)
    //{
    //    GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
    //    x->x_point = 0;
    //}
261
262
263
264
}

static void grid_draw_select(t_grid* x,t_glist* glist)
{
265
266
    t_canvas *canvas = glist_getcanvas(glist);
    if (x->x_selected)
267
    {
268
        //pd_bind(&x->x_obj.ob_pd, x->x_name);
269
        /* sets the item in blue */
270
271
272
273
        //GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID "
        //               "-outline #0000FF\n",
        //    canvas, x);
        gui_vmess("gui_gobj_select", "xx", canvas, x);
274
275
276
    }
    else
    {
277
278
279
280
281
        //pd_unbind(&x->x_obj.ob_pd, x->x_name);
        //GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID "
        //               "-outline #000000\n",
        //    canvas, x);
        gui_vmess("gui_gobj_deselect", "xx", canvas, x);
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    }
}

static void grid_output_current(t_grid* x)
{
    t_float xvalue, yvalue;
    t_float xmodstep, ymodstep;

    xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
    if (xvalue < x->x_min ) xvalue = x->x_min;
    if (xvalue > x->x_max ) xvalue = x->x_max;
    xmodstep = ((float)((int)(xvalue*10000) % (int)(x->x_xstep*10000))/10000.);
    xvalue = xvalue - xmodstep;
    outlet_float( x->x_xoutlet, xvalue );

    yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
    if (yvalue < x->y_min ) yvalue = x->y_min;
    if (yvalue > x->y_max ) yvalue = x->y_max;
    ymodstep = ((float)((int)(yvalue*10000) % (int)(x->x_ystep*10000))/10000.);
    yvalue = yvalue - ymodstep;
302
    outlet_float(x->x_youtlet, yvalue);
303
304
305
306
307
308
}

/* ------------------------ grid widgetbehaviour----------------------------- */


static void grid_getrect(t_gobj *z, t_glist *owner,
309
    int *xp1, int *yp1, int *xp2, int *yp2)
310
{
311
   t_grid* x = (t_grid*)z;
312

313
314
315
316
   *xp1 = text_xpix(&x->x_obj, x->x_glist);
   *yp1 = text_ypix(&x->x_obj, x->x_glist);
   *xp2 = text_xpix(&x->x_obj, x->x_glist)+x->x_width;
   *yp2 = text_ypix(&x->x_obj, x->x_glist)+x->x_height;
317
318
319
320
321
322
323
324
}

static void grid_save(t_gobj *z, t_binbuf *b)
{
    t_grid *x = (t_grid *)z;

    // post( "saving grid : %s", x->x_name->s_name );
    binbuf_addv(b, "ssiissiffiffiffiiff", gensym("#X"),gensym("obj"),
325
326
327
328
329
330
331
332
    (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
        atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
        x->x_name, x->x_width, x->x_min,
        x->x_max, x->x_height,
        x->y_min, x->y_max,
        x->x_grid, x->x_xstep, 
        x->x_ystep, x->x_xlines, x->x_ylines, 
        x->x_current, x->y_current );
333
334
335
336
337
338
    binbuf_addv(b, ";");
}

static void grid_properties(t_gobj *z, t_glist *owner)
{
    char buf[800];
339
    char *gfx_tag;
340
341
    t_grid *x=(t_grid *)z;

342
343
344
345
346
347
348
349
350
351
352
    //sprintf(buf, "pdtk_grid_dialog %%s %s %d %.2f %.2f %d %.2f %.2f %d %.2f %.2f %d %d\n",
    //    x->x_name->s_name, x->x_width, x->x_min, x->x_max, x->x_height, 
    //    x->y_min, x->y_max, x->x_grid, x->x_xstep, x->x_ystep,
    //    x->x_xlines, x->x_ylines );
        // post("grid_properties : %s", buf );
    //gfxstub_new(&x->x_obj.ob_pd, x, buf);
    gfx_tag = gfxstub_new2(&x->x_obj.ob_pd, x);

    gui_start_vmess("gui_external_dialog", "ss", gfx_tag, "grid");
    gui_start_array();

353
354
355
356
357
358
359
360
361
362
363
364
    gui_s("receive_symbol"); gui_s(x->x_name->s_name);
    gui_s("width"); gui_i(x->x_width);
    gui_s("min"); gui_f(x->x_min);
    gui_s("max"); gui_f(x->x_max);
    gui_s("height"); gui_i(x->x_height);
    gui_s("y-min"); gui_f(x->y_min);
    gui_s("y-max"); gui_f(x->y_max);
    gui_s("grid_toggle"); gui_i(x->x_grid);
    gui_s("x-steps"); gui_f(x->x_xstep);
    gui_s("y-steps"); gui_f(x->x_ystep);
    gui_s("x-lines"); gui_i(x->x_xlines);
    gui_s("y-lines"); gui_i(x->x_ylines);
365
366
367
368

    gui_end_array();
    gui_end_vmess();

369
370
371
372
373
374
375
}

static void grid_select(t_gobj *z, t_glist *glist, int selected)
{
    t_grid *x = (t_grid *)z;

    x->x_selected = selected;
376
    grid_draw_select(x, glist);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
}

static void grid_vis(t_gobj *z, t_glist *glist, int vis)
{
    t_grid *x = (t_grid *)z;

    if (vis)
    {
        grid_draw_new( x, glist );
        grid_draw_update( x, glist );
        grid_output_current(x);
    }
    else
    {
        grid_draw_erase( x, glist );
    }
}

static void grid_dialog(t_grid *x, t_symbol *s, int argc, t_atom *argv)
{
397
    if (!x)
398
    {
399
        error("grid : error :tried to set properties on an unexisting object");
400
    }
401
    if (argc != 12)
402
    {
403
404
405
        pd_error(x,
            "grid : error in the number of arguments ( %d instead of 12 )",
                argc );
406
407
        return;
    }
408
        if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
409
410
            argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
            argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
411
412
413
            argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT || 
            argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT || 
            argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT ) {
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
        pd_error(x, "grid : wrong arguments" );
        return;
    }
    x->x_name = argv[0].a_w.w_symbol;
    x->x_width = (int)argv[1].a_w.w_float;
    x->x_min = argv[2].a_w.w_float;
    x->x_max = argv[3].a_w.w_float;
    x->x_height = (int)argv[4].a_w.w_float;
    x->y_min = argv[5].a_w.w_float;
    x->y_max = argv[6].a_w.w_float;
    x->x_grid = argv[7].a_w.w_float;
    x->x_xstep = argv[8].a_w.w_float;
    x->x_ystep = argv[9].a_w.w_float;
    x->x_xlines = argv[10].a_w.w_float;
    x->x_ylines = argv[11].a_w.w_float;
429
430
431
    //grid_draw_erase(x, x->x_glist);
    //grid_draw_new(x, x->x_glist);
    grid_draw_configure(x, x->x_glist);
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
}

static void grid_delete(t_gobj *z, t_glist *glist)
{
    canvas_deletelinesfor(glist, (t_text *)z);
}

static void grid_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
    t_grid *x = (t_grid *)z;
    int xold = text_xpix(&x->x_obj, glist);
    int yold = text_ypix(&x->x_obj, glist);

    // post( "grid_displace dx=%d dy=%d", dx, dy );

    x->x_obj.te_xpix += dx;
    x->x_current += dx;
    x->x_obj.te_ypix += dy;
    x->y_current += dy;
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    if (xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist))
    {
	//grid_draw_move(x, x->x_glist);
        canvas_fixlinesfor(glist, (t_text *)z);
    }
}

static void grid_displace_wtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
    t_grid *x = (t_grid *)z;
    int xold = text_xpix(&x->x_obj, glist);
    int yold = text_ypix(&x->x_obj, glist);

    // post( "grid_displace dx=%d dy=%d", dx, dy );

    x->x_obj.te_xpix += dx;
    x->x_current += dx;
    x->x_obj.te_ypix += dy;
    x->y_current += dy;
    if (xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist))
471
    {
472
473
	//grid_draw_move(x, x->x_glist);
        canvas_fixlinesfor(glist, (t_text *)z);
474
475
476
    }
}

477

478
479
480
481
482
483
484
485
486
487
488
489
static void grid_motion(t_grid *x, t_floatarg dx, t_floatarg dy)
{
    int xold = x->x_current;
    int yold = x->y_current;

    // post( "grid_motion dx=%f dy=%f", dx, dy );

    x->x_current += dx;
    x->y_current += dy;
    if(xold != x->x_current || yold != x->y_current)
    {
        grid_output_current(x);
490
	grid_draw_update(x, x->x_glist);
491
492
493
494
    }
}

static int grid_click(t_gobj *z, struct _glist *glist,
495
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
496
497
498
499
{
    t_grid* x = (t_grid *)z;

    // post( "grid_click doit=%d x=%d y=%d", doit, xpix, ypix );
500
    if ( doit) 
501
502
503
504
505
506
    {
        x->x_current = xpix;
        x->y_current = ypix;
        grid_output_current(x);
        grid_draw_update(x, glist);
        glist_grab(glist, &x->x_obj.te_g, (t_glistmotionfn)grid_motion,
507
            0, xpix, ypix);
508
509
510
511
512
513
514
515
516
    }
    return (1);
}

static void grid_goto(t_grid *x, t_floatarg newx, t_floatarg newy)
{
    int xold = x->x_current;
    int yold = x->y_current;

517
518
519
520
    if (newx > x->x_width-1) newx = x->x_width-1;
    if (newx < 0) newx = 0;
    if (newy > x->x_height-1) newy = x->x_height-1;
    if (newy < 0) newy = 0;
521
522
523
524
525

    // post( "grid_set x=%f y=%f", newx, newy );

    x->x_current = newx + text_xpix(&x->x_obj, x->x_glist);
    x->y_current = newy + text_ypix(&x->x_obj, x->x_glist);
526
    if (xold != x->x_current || yold != x->y_current)
527
528
529
530
531
532
533
    {
        grid_output_current(x);
        grid_draw_update(x, x->x_glist);
    }
}


534
535
static void grid_new_color(t_grid *x, t_floatarg color1,
    t_floatarg color2, t_floatarg color3)
536
537
538
{
    char col1[10], col2[10], col3[10];

539
    if ((color1 < 0) || (color1 > 255))
540
    {
541
        pd_error(x, "wrong color component : %d", (int)color1);
542
    }
543
    if ((color2 < 0) || (color2 > 255))
544
    {
545
        pd_error(x, "wrong color component : %d", (int)color2);
546
    }
547
    if ((color3 < 0) || (color3 > 255))
548
    {
549
        pd_error(x, "wrong color component : %d", (int)color3);
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
    }

    if (color1 < 17)
        sprintf(col1,"0%X",(int) color1);
    else
        sprintf(col1,"%X",(int) color1);

    if (color2 < 17)
        sprintf(col2,"0%X",(int) color2);
    else
        sprintf(col2,"%X",(int) color2);

    if (color3 < 17)
        sprintf(col3,"0%X",(int) color3);
    else
        sprintf(col3,"%X",(int) color3);
    sprintf( x->x_bgcolor, "#%s%s%s", col1, col2, col3);

568
569
570
    //grid_draw_erase(x, x->x_glist);
    //grid_draw_new(x, x->x_glist);
    grid_draw_configure(x, x->x_glist);
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
}

static void grid_values(t_grid* x, t_floatarg xvalue, t_floatarg yvalue)
{
    int xold = x->x_current;
    int yold = x->y_current;

    if (xvalue < x->x_min ) xvalue = x->x_min;
    if (xvalue > x->x_max ) xvalue = x->x_max;

    x->x_current = text_xpix(&x->x_obj, x->x_glist)
        + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);

    if (yvalue < x->y_min ) yvalue = x->y_min;
    if (yvalue > x->y_max ) yvalue = x->y_max;

    x->y_current = text_ypix(&x->x_obj, x->x_glist)
        + abs((1 - (yvalue - x->y_min) / (x->y_max  - x->y_min)) * x->x_height);

590
    if (xold != x->x_current || yold != x->y_current)
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
    {
        grid_output_current(x);
        grid_draw_update(x, x->x_glist);
    }
}

static void grid_xvalues(t_grid* x, t_floatarg xvalue, t_floatarg yvalue)
{
    int xold = x->x_current;
    int yold = x->y_current;

    if (xvalue < x->x_min ) xvalue = x->x_min;
    if (xvalue > x->x_max ) xvalue = x->x_max;

    x->x_current = text_xpix(&x->x_obj, x->x_glist)
        + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);

    if (yvalue < x->y_min ) yvalue = x->y_min;
    if (yvalue > x->y_max ) yvalue = x->y_max;

    x->y_current = text_ypix(&x->x_obj, x->x_glist)
        + abs((1 - (yvalue - x->y_min) / (x->y_max  - x->y_min)) * x->x_height);

614
    if (xold != x->x_current || yold != x->y_current)
615
616
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
    {
        grid_draw_update(x, x->x_glist);
    }
}

static void grid_valuemotion(t_grid* x, t_floatarg dx, t_floatarg dy)
{
    int xold = x->x_current;
    int yold = x->y_current;
    t_float xvalue, yvalue;

    xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
    if (xvalue < x->x_min ) xvalue = x->x_min;
    if (xvalue > x->x_max ) xvalue = x->x_max;

    yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
    if (yvalue < x->y_min ) yvalue = x->y_min;
    if (yvalue > x->y_max ) yvalue = x->y_max;

    xvalue += dx;
    yvalue += dy;

    if (xvalue < x->x_min ) xvalue = x->x_min;
    if (xvalue > x->x_max ) xvalue = x->x_max;

    x->x_current = text_xpix(&x->x_obj, x->x_glist)
        + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);

    if (yvalue < x->y_min ) yvalue = x->y_min;
    if (yvalue > x->y_max ) yvalue = x->y_max;

646
    x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
647

648
649
650


   if (xold != x->x_current || yold != x->y_current)
651
652
653
654
655
656
657
658
659
660
661
662
    {
        grid_output_current(x);
        grid_draw_update(x, x->x_glist);
    }
}

static void grid_xvaluemotion(t_grid* x, t_floatarg dx, t_floatarg dy)
{
    int xold = x->x_current;
    int yold = x->y_current;
    t_float xvalue, yvalue;

663
664
665
    xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width;
    if (xvalue < x->x_min) xvalue = x->x_min;
    if (xvalue > x->x_max) xvalue = x->x_max;
666
667
668
669
670
671
672
673

    yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
    if (yvalue < x->y_min ) yvalue = x->y_min;
    if (yvalue > x->y_max ) yvalue = x->y_max;

    xvalue += dx;
    yvalue += dy;

674
675
    if (xvalue < x->x_min) xvalue = x->x_min;
    if (xvalue > x->x_max) xvalue = x->x_max;
676
677
678
679

    x->x_current = text_xpix(&x->x_obj, x->x_glist)
        + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);

680
681
    if (yvalue < x->y_min) yvalue = x->y_min;
    if (yvalue > x->y_max) yvalue = x->y_max;
682

683
    x->y_current =  text_ypix(&x->x_obj, x->x_glist)
684
685
        + abs((1 - (yvalue - x->y_min) / (x->y_max - x->y_min)) * x->x_height);

686
    if (xold != x->x_current || yold != x->y_current)
687
688
689
690
691
    {
        grid_draw_update(x, x->x_glist);
    }
}

692
static void grid_bang(t_grid *x) {
693
694
695
    grid_output_current(x);
}

696
697
698
static void grid__clickhook(t_scalehandle *sh, int newstate)
{
    t_grid *x = (t_grid *)(sh->h_master);
699
700
701
702
703
704
705
    /* Use constrained dragging-- see g_canvas.c clickhook */
    sh->h_constrain = newstate;
    sh->h_adjust_x = sh->h_offset_x -
        (((t_object *)x)->te_xpix + x->x_width);
    sh->h_adjust_y = sh->h_offset_y -
        (((t_object *)x)->te_ypix + x->x_height);
    canvas_apply_setundo(x->x_glist, (t_gobj *)x);
706
707
708
709
710
711
    sh->h_dragon = newstate;
}

static void grid__motionhook(t_scalehandle *sh,
    t_floatarg mouse_x, t_floatarg mouse_y)
{
712
713
714
715
716
717
718
719
720
721
722
723
    t_grid *x = (t_grid *)(sh->h_master);
    int width = (sh->h_constrain == CURSOR_EDITMODE_RESIZE_Y) ?
        x->x_width :
        (int)mouse_x - text_xpix(&x->x_obj, x->x_glist) - sh->h_adjust_x;
    int height = (sh->h_constrain == CURSOR_EDITMODE_RESIZE_X) ?
        x->x_height :
        (int)mouse_y - text_ypix(&x->x_obj, x->x_glist) - sh->h_adjust_y;
    int minw = MIN_GRID_WIDTH,
        minh = MIN_GRID_HEIGHT;
    x->x_width = width < minw ? minw : width;
    x->x_height = height < minh ? minh : height;
    if (glist_isvisible(x->x_glist))
724
    {
725
726
727
        grid_draw_configure(x, x->x_glist);
        //scalehandle_unclick_scale(sh);
    }
728

729
730
731
732
733
734
735
    int properties = gfxstub_haveproperties((void *)x);
    if (properties)
    {
        int new_w = x->x_width + sh->h_dragx;
        int new_h = x->x_height + sh->h_dragy;
        properties_set_field_int(properties,"width",new_w);
        properties_set_field_int(properties,"height",new_h);
736
737
738
739
740
741
742
743
    }
}

/* wrapper method for forwarding "scalehandle" data */
static void grid_click_for_resizing(t_grid *x, t_floatarg f,
    t_floatarg xxx, t_floatarg yyy)
{
    t_scalehandle *sh = (t_scalehandle *)x->x_handle;
744
745
    sh->h_offset_x = (int)xxx;
    sh->h_offset_y = (int)yyy;
746
747
748
749
750
751
752
753
754
755
756
    grid__clickhook(sh, f);
}

/* another wrapper for forwarding "scalehandle" motion data */
static void grid_motion_for_resizing(t_grid *x, t_floatarg xxx,
    t_floatarg yyy)
{
    t_scalehandle *sh = (t_scalehandle *)x->x_handle;
    grid__motionhook(sh, xxx, yyy);
}

757
758
759
760
761
static t_grid *grid_new(t_symbol *s, int argc, t_atom *argv)
{
    int zz;
    t_grid *x;
    char *str;
762
 
763
764
765
    // post( "grid_new : create : %s argc =%d", s->s_name, argc );

    x = (t_grid *)pd_new(grid_class);
766
767
    // new grid created from the gui 
    if (argc != 0)
768
769
770
    {
        if ( argc != 14 )
        {
771
772
773
            pd_error(x, "grid : error in the number of arguments "
                        "( %d instead of 14 )",
                argc );
774
775
            return NULL;
        }
776
777
778
779
780
781
782
        if (argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
            argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
            argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
            argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT ||
            argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT ||
            argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT ||
            argv[12].a_type != A_FLOAT || argv[13].a_type != A_FLOAT )
783
784
785
786
787
788
789
        {
            pd_error(x, "grid : wrong arguments" );
            return NULL;
        }

        // update grid count
        if (!strncmp((str = argv[0].a_w.w_symbol->s_name), "grid", 5)
790
            && (zz = atoi(str + 5)) > gridcount)
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
        {
            gridcount = zz;
        }
        x->x_name = argv[0].a_w.w_symbol;
        pd_bind(&x->x_obj.ob_pd, x->x_name);
        x->x_width = argv[1].a_w.w_float;
        x->x_min = argv[2].a_w.w_float;
        x->x_max = argv[3].a_w.w_float;
        x->x_height = argv[4].a_w.w_float;
        x->y_min = argv[5].a_w.w_float;
        x->y_max = argv[6].a_w.w_float;
        x->x_grid = argv[7].a_w.w_float;
        x->x_xstep = argv[8].a_w.w_float;
        x->x_ystep = argv[9].a_w.w_float;
        x->x_xlines = argv[10].a_w.w_float;
        x->x_ylines = argv[11].a_w.w_float;
        x->x_current = argv[12].a_w.w_float;
        x->y_current = argv[13].a_w.w_float;
        x->x_point = 1;
    }
    else
    {
        char buf[40];

        sprintf(buf, "grid%d", ++gridcount);
        s = gensym(buf);

        x->x_name = s;
        pd_bind(&x->x_obj.ob_pd, x->x_name);

        x->x_width = DEFAULT_GRID_WIDTH;
        x->x_min = 0;
        x->x_max = DEFAULT_GRID_WIDTH - 1;
        x->x_height = DEFAULT_GRID_HEIGHT;
        x->y_min = 0;
        x->y_max = DEFAULT_GRID_HEIGHT - 1;
        x->x_grid = 1;
        x->x_xstep = 1.0;
        x->x_ystep = 1.0;
        x->x_xlines = DEFAULT_GRID_NBLINES;
        x->x_ylines = DEFAULT_GRID_NBLINES;
        x->x_current = 0;
        x->y_current = 0;
    }

    // common fields for new and restored grids
    x->x_point = 0;
    x->x_selected = 0;
    x->x_glist = (t_glist *) canvas_getcurrent();
    x->x_xoutlet = outlet_new(&x->x_obj, &s_float );
    x->x_youtlet = outlet_new(&x->x_obj, &s_float );

    x->x_bgcolor = (char*)getbytes(12);
    strcpy( x->x_bgcolor, "#123589" );

846
847
848
849
850
    // post( "grid_new name : %s width: %d height : %d",
    // x->x_name->s_name, x->x_width, x->x_height );

    x->x_handle = scalehandle_new((t_object *)x, x->x_glist, 1,
        grid__clickhook, grid__motionhook);
851
852
853
854
855
856

    return (x);
}

static void grid_free(t_grid *x)
{
857
858
    pd_unbind(&x->x_obj.ob_pd, x->x_name);
    gfxstub_deleteforkey(x);
859
860
861
862
}

void grid_setup(void)
{
863
864
//#include "grid.tk2c"
    post ( grid_version );
865
    grid_class = class_new(gensym("grid"), (t_newmethod)grid_new,
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
        (t_method)grid_free, sizeof(t_grid), 0, A_GIMME, 0);
    class_addmethod(grid_class, (t_method)grid_click,
        gensym("click"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_motion,
        gensym("motion"), A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_bang,
        gensym("bang"), 0);
    class_addmethod(grid_class, (t_method)grid_values,
        gensym("values"), A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_valuemotion,
        gensym("valuemotion"), A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_xvalues,
        gensym("xvalues"), A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_xvaluemotion,
        gensym("xvaluemotion"), A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_goto,
        gensym("goto"), A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_dialog,
        gensym("dialog"), A_GIMME, 0);
    class_addmethod(grid_class, (t_method)grid_new_color,
        gensym("color"), A_FLOAT, A_FLOAT, A_FLOAT, 0);
    /* Big hack for receiving edit-mode resize anchor clicks from
       g_editor.c. */
    class_addmethod(grid_class, (t_method)grid_click_for_resizing,
890
                    gensym("_click_for_resizing"),
891
892
                    A_FLOAT, A_FLOAT, A_FLOAT, 0);
    class_addmethod(grid_class, (t_method)grid_motion_for_resizing,
893
                    gensym("_motion_for_resizing"),
894
895
896
897
898
899
900
901
                    A_FLOAT, A_FLOAT, 0);
    grid_widgetbehavior.w_getrectfn =    grid_getrect;
    grid_widgetbehavior.w_displacefn =   grid_displace;
    grid_widgetbehavior.w_selectfn =     grid_select;
    grid_widgetbehavior.w_activatefn =   NULL;
    grid_widgetbehavior.w_deletefn =     grid_delete;
    grid_widgetbehavior.w_visfn =        grid_vis;
    grid_widgetbehavior.w_clickfn =      grid_click;
902
    grid_widgetbehavior.w_displacefnwtag = grid_displace_wtag;
903
904
905
906
907
908
909
910
911
912
913

#if PD_MINOR_VERSION >= 37
    class_setpropertiesfn(grid_class, grid_properties);
    class_setsavefn(grid_class, grid_save);
#else
    grid_widgetbehavior.w_propertiesfn = grid_properties;
    grid_widgetbehavior.w_savefn =       grid_save;
#endif

    class_setwidget(grid_class, &grid_widgetbehavior);
}