knob.c 28.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* 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_7_guis.c written by Thomas Musil (c) IEM KUG Graz Austria 2000-2001 */
/* thanks to Miller Puckette, Guenther Geiger and Krzystof Czaja */

/* 'knob' gui object by Frank Barknecht, 'externalised' by Olaf Matthes */
/* changed for devel_0.37 by Christoph Kummerer - hardly tested         */

/* I had to out-comment the loadbang stuff because I couldn't find the code
   in Pd where it is declared */


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

#ifndef PD_MAJOR_VERSION
#include "s_stuff.h"
#else 
#include "m_imp.h"
#endif

#include "g_canvas.h"

29
#include "g_all_guis.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <math.h>

#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

#ifndef M_PI
#define M_PI 3.141592654
#endif

#define IEM_KNOB_DEFAULTSIZE 32

/* ------------ knob gui-vertical  slider ----------------------- */

t_widgetbehavior knob_widgetbehavior;
static t_class *knob_class;
t_symbol *iemgui_key_sym=0;		/* taken from g_all_guis.c */

50
typedef struct _knob			/* taken from Frank's modified g_all_guis.h */
51 52 53 54 55 56
{
    t_iemgui x_gui;
    int      x_pos;
    int      x_val;
    int      x_lin0_log1;
    int      x_steady;
57
    int      x_explained_surprising_behavior;
58 59 60 61 62 63 64 65 66
    double   x_min;
    double   x_max;
    double   x_k;
} t_knob;

/* widget helper functions */

static void knob_draw_update(t_knob *x, t_glist *glist)
{
67 68 69 70
	/* All this complicated data and no normalized value stored anywhere!
	   So we roll our own here for the sake of sanity... */
	double normalized_value = (double)(((x->x_val)*0.01*x->x_k + x->x_min)
		/ x->x_max);
71 72
	/* compute dial:*/ 
	float radius = 0.5*(float)x->x_gui.x_h;
73 74 75
	double angle = 7.0/36.0 + 34.0/36.0*2.0*M_PI*((double)x->x_val*0.01/(double)x->x_gui.x_h );
//	int start = -80;
//	int extent = 350 - (int)(360.0*angle/(2.0*M_PI));
76
	/* center point: */
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	int x1 = radius;
	int y1 = radius;
	int x2 = radius + radius * sin(-angle); 
        int y2 = radius + radius * cos(angle);

    gui_vmess("gui_turn_mknob", "xxiiiiif",
        glist_getcanvas(glist),
        x,
        x1,
        y1,
        x2,
        y2,
        1,
        normalized_value
    );
92 93
    	/* post("knob: (%d, %d) (%d, %d)", x1,y1,x2,y2); */
	
94 95 96
	//sys_vgui(".x%lx.c itemconfigure %xBASE -start %d -extent %d \n",
	//    glist_getcanvas(glist), x,
	//     start, extent);
97 98
}

99
static void knob_draw_config(t_knob *x,t_glist *glist);
100 101 102 103 104 105 106 107
static void knob_draw_new(t_knob *x, t_glist *glist)
{
    int xpos=text_xpix(&x->x_gui.x_obj, glist);
    int ypos=text_ypix(&x->x_gui.x_obj, glist);
    /* int r = ypos + x->x_gui.x_h - (x->x_val + 50)/100; */
    t_canvas *canvas=glist_getcanvas(glist);
    
    /* compute dial:*/ 
108
//    float radius = 0.5*(float)x->x_gui.x_h;
109
    /* center point: */
110 111
//    int x1 = xpos + radius;
//    int y1 = ypos + radius;
112
    /* dial */  
113 114 115
//    double angle = 7.0/36.0 + 34.0/36.0*2.0*M_PI*((float)x->x_val*0.01/(float)(x->x_gui.x_h));
//    int x2 = x1 + radius * sin(-angle);
//    int y2 = y1 + radius * cos(angle);
116
    
117 118 119 120 121 122 123 124 125 126 127 128
    /* reuse mknob drawing routine */
    gui_vmess("gui_mknob_new", "xxiiiiii",
        canvas,
        x,
        xpos,
        ypos,
        glist_istoplevel(glist),
        !iemgui_has_snd(&x->x_gui),
        !iemgui_has_rcv(&x->x_gui),
        1
    );
    knob_draw_config(x, glist);
129 130

    /* BASE2 */
131
    /*
132
    sys_vgui(".x%lx.c create arc %d %d %d %d -outline #%6.6x -style arc -width 3 -start -80 -extent 340 -tags %xBASE2\n",
133
	     canvas, 
134 135
	     xpos, ypos,
	     xpos + x->x_gui.x_h, ypos + x->x_gui.x_h,
136
	     x->x_gui.x_fcol, x);
137
    */
138
    /* BASE */
139
    /*
140
    sys_vgui(".x%lx.c create arc %d %d %d %d -fill #%6.6x -style arc -width 3 -start -80 -extent 340 -tags %xBASE\n",
141 142 143
	     canvas,
	     xpos, ypos,
	     xpos + x->x_gui.x_h, ypos + x->x_gui.x_h,
144
	     x->x_gui.x_bcol, x);
145
    */
146
    /* LINE */
147
    /*
148
    sys_vgui(".x%lx.c create line %d %d %d %d -width 3 -fill #%6.6x -capstyle round -tags %xKNOB\n",
149 150 151 152 153 154 155 156
	     canvas,
	     x1,
	     y1,
	     x2,
	     y2,
	     x->x_gui.x_fcol,
	     x);

157
    sys_vgui(".x%lx.c create text %d %d -text {%s} -anchor w \
158 159 160
	     -font {%s %d bold} -fill #%6.6x -tags %xLABEL\n",
	     canvas, xpos+x->x_gui.x_ldx, ypos+x->x_gui.x_ldy,
	     strcmp(x->x_gui.x_lab->s_name, "empty")?x->x_gui.x_lab->s_name:"",
161 162 163
	     iem_fstyletoint(&x->x_gui), x->x_gui.x_fontsize, x->x_gui.x_lcol,
             x);
    if (!iemgui_has_snd(&x->x_gui))
164
        sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %xOUT%d\n",
165 166 167 168
	     canvas,
	     xpos, ypos + x->x_gui.x_h+2,
	     xpos+7, ypos + x->x_gui.x_h+3,
	     x, 0);
169
    if (!iemgui_has_rcv(&x->x_gui))
170
	sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %xIN%d\n",
171 172 173 174
	     canvas,
	     xpos, ypos-2,
	     xpos+7, ypos-1,
	     x, 0);
175
    */
176 177
}

178
static void knob_draw_config(t_knob *x,t_glist *glist)
179
{
180 181
    t_canvas *canvas = glist_getcanvas(glist);
    char bcol[8], fcol[8], lcol[8];
182

183 184 185
    sprintf(bcol, "#%6.6x", x->x_gui.x_bcol);
    sprintf(fcol, "#%6.6x", x->x_gui.x_fcol);
    sprintf(lcol, "#%6.6x", x->x_gui.x_lcol);
186

187 188 189 190 191 192 193 194 195 196
    /* reuse mknob interface */
    gui_vmess("gui_configure_mknob", "xxissi",
        canvas,
        x,
        x->x_gui.x_h,
        bcol,
        fcol,
        1 /* flag to tell we are a footils/knob */
    );
    knob_draw_update(x, glist);
197

198
/*
199
    sys_vgui(".x%lx.c itemconfigure %xLABEL -font {%s %d bold} -fill #%6.6x -text {%s} \n",
200 201 202
	     canvas, x, iem_fstyletoint(&x->x_gui), x->x_gui.x_fontsize,
             glist_isselected(canvas, (t_gobj *)x) ?
                 0 : x->x_gui.x_lcol,
203
	     strcmp(x->x_gui.x_lab->s_name, "empty")?x->x_gui.x_lab->s_name:"");
204
    sys_vgui(".x%lx.c itemconfigure %xKNOB -fill #%6.6x\n", canvas,
205
	     x, x->x_gui.x_fcol);
206
    sys_vgui(".x%lx.c itemconfigure %xBASE2 -outline #%6.6x\n", canvas,
207
	     x, x->x_gui.x_fcol);
208
    sys_vgui(".x%lx.c itemconfigure %xBASE -fill #%6.6x\n", canvas,
209
	     x, x->x_gui.x_bcol);
210
*/
211 212
}

213
/*
214 215 216 217 218 219
static void knob_draw_io(t_knob* x,t_glist* glist, int old_snd_rcv_flags)
{
    int xpos=text_xpix(&x->x_gui.x_obj, glist);
    int ypos=text_ypix(&x->x_gui.x_obj, glist);
    t_canvas *canvas=glist_getcanvas(glist);

220
    if((old_snd_rcv_flags & IEM_GUI_OLD_SND_FLAG) && !iemgui_has_snd(&x->x_gui))
221
        sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %xOUT%d\n",
222 223 224 225
	     canvas,
	     xpos, ypos + x->x_gui.x_h+2,
	     xpos+7, ypos + x->x_gui.x_h+3,
	     x, 0);
226
    if(!(old_snd_rcv_flags & IEM_GUI_OLD_SND_FLAG) && iemgui_has_rcv(&x->x_gui))
227
        sys_vgui(".x%lx.c delete %xOUT%d\n", canvas, x, 0);
228
    if((old_snd_rcv_flags & IEM_GUI_OLD_RCV_FLAG) && !iemgui_has_rcv(&x->x_gui))
229
        sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %xIN%d\n",
230 231 232 233
	     canvas,
	     xpos, ypos-2,
	     xpos+7, ypos-1,
	     x, 0);
234
    if(!(old_snd_rcv_flags & IEM_GUI_OLD_RCV_FLAG) && iemgui_has_rcv(&x->x_gui))
235
        sys_vgui(".x%lx.c delete %xIN%d\n", canvas, x, 0);
236
}
237
*/
238

239
/*
240 241 242 243
static void knob_draw_select(t_knob *x, t_glist *glist)
{
    t_canvas *canvas=glist_getcanvas(glist);

244
    if(glist_isselected(canvas, (t_gobj *)x))
245 246
    {
	pd_bind(&x->x_gui.x_obj.ob_pd, iemgui_key_sym);
247 248 249
	sys_vgui(".x%lx.c itemconfigure %xBASE2 -outline #%6.6x\n", canvas, x, 0);
	sys_vgui(".x%lx.c itemconfigure %xBASE -fill #%6.6x\n", canvas, x, 0);
	sys_vgui(".x%lx.c itemconfigure %xLABEL -fill #%6.6x\n", canvas, x, 0);
250 251 252 253
    }
    else
    {
	pd_unbind(&x->x_gui.x_obj.ob_pd, iemgui_key_sym);
254
	sys_vgui(".x%lx.c itemconfigure %xBASE -fill #%6.6x\n", canvas, x, 0);
255 256
	sys_vgui(".x%lx.c itemconfigure %xBASE2 -outline #%6.6x\n", canvas, x, x->x_gui.x_fcol);
	sys_vgui(".x%lx.c itemconfigure %xLABEL -fill #%6.6x\n", canvas, x, x->x_gui.x_lcol);
257 258
    }
}
259
*/
260

261
static void knob_draw(t_knob *x, t_glist *glist, int mode)
262 263 264 265
{
    if(mode == IEM_GUI_DRAW_MODE_UPDATE)
	knob_draw_update(x, glist);
    else if(mode == IEM_GUI_DRAW_MODE_MOVE)
266
	iemgui_base_draw_move(&x->x_gui);
267 268
    else if(mode == IEM_GUI_DRAW_MODE_NEW)
	knob_draw_new(x, glist);
269
/*
270 271 272 273
    else if(mode == IEM_GUI_DRAW_MODE_SELECT)
	knob_draw_select(x, glist);
    else if(mode == IEM_GUI_DRAW_MODE_ERASE)
	knob_draw_erase(x, glist);
274
*/
275 276
    else if(mode == IEM_GUI_DRAW_MODE_CONFIG)
	knob_draw_config(x, glist);
277
/*
278 279
    else if(mode >= IEM_GUI_DRAW_MODE_IO)
	knob_draw_io(x, glist, mode - IEM_GUI_DRAW_MODE_IO);
280
*/
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
}

/* ------------------------ knob widgetbehaviour----------------------------- */


static void knob_getrect(t_gobj *z, t_glist *glist,
			    int *xp1, int *yp1, int *xp2, int *yp2)
{
    t_knob* x = (t_knob*)z;

    *xp1 = text_xpix(&x->x_gui.x_obj, glist);
    *yp1 = text_ypix(&x->x_gui.x_obj, glist) - 2;
    *xp2 = *xp1 + x->x_gui.x_h;
    *yp2 = *yp1 + x->x_gui.x_h + 5;
}

static void knob_save(t_gobj *z, t_binbuf *b)
{
    t_knob *x = (t_knob *)z;
300
    int bflcol[3];
301 302 303 304 305 306 307 308
    t_symbol *srl[3];

    iemgui_save(&x->x_gui, srl, bflcol);
    binbuf_addv(b, "ssiisiiffiisssiiiiiiiii", gensym("#X"),gensym("obj"),
		(t_int)x->x_gui.x_obj.te_xpix, (t_int)x->x_gui.x_obj.te_ypix,
		atom_getsymbol(binbuf_getvec(x->x_gui.x_obj.te_binbuf)),
        x->x_gui.x_h, x->x_gui.x_h,
		(float)x->x_min, (float)x->x_max,
309
		x->x_lin0_log1, iem_symargstoint(&x->x_gui),
310 311
		srl[0], srl[1], srl[2],
		x->x_gui.x_ldx, x->x_gui.x_ldy,
312
		iem_fstyletoint(&x->x_gui), x->x_gui.x_fontsize,
313 314 315 316 317
		bflcol[0], bflcol[1], bflcol[2],
		x->x_val, x->x_steady);
    binbuf_addv(b, ";");
}

318
static void knob_check_height(t_knob *x, int h)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
    if(h < IEM_SL_MINSIZE)
	h = IEM_SL_MINSIZE;
    x->x_gui.x_h = h;
    if(x->x_val > (x->x_gui.x_h*100 - 100))
    {
	x->x_pos = x->x_gui.x_h*100 - 100;
	x->x_val = x->x_pos;
    }
    if(x->x_lin0_log1)
	x->x_k = log(x->x_max/x->x_min)/(double)(x->x_gui.x_h - 1);
    else
	x->x_k = (x->x_max - x->x_min)/(double)(x->x_gui.x_h - 1);
}

334
static void knob_check_minmax(t_knob *x, double min, double max)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
{
    if(x->x_lin0_log1)
    {
	if((min == 0.0)&&(max == 0.0))
	    max = 1.0;
	if(max > 0.0)
	{
	    if(min <= 0.0)
		min = 0.01*max;
	}
	else
	{
	    if(min > 0.0)
		max = 0.01*min;
	}
    }
    x->x_min = min;
    x->x_max = max;
    if(x->x_min > x->x_max)                /* bugfix */
354
	x->x_gui.x_reverse = 1;
355
    else
356
        x->x_gui.x_reverse = 0;
357 358 359 360 361 362 363 364 365
    if(x->x_lin0_log1)
	x->x_k = log(x->x_max/x->x_min)/(double)(x->x_gui.x_h - 1);
    else
	x->x_k = (x->x_max - x->x_min)/(double)(x->x_gui.x_h - 1);
}

static void knob_properties(t_gobj *z, t_glist *owner)
{
    t_knob *x = (t_knob *)z;
366 367
//    char buf[800];
    char *gfx_tag;
368 369 370
    t_symbol *srl[3];

    iemgui_properties(&x->x_gui, srl);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    gfx_tag = gfxstub_new2(&x->x_gui.x_obj.ob_pd, x);

    gui_start_vmess("gui_iemgui_dialog", "s", gfx_tag);
    gui_start_array();

    gui_s("type");             gui_s("knob");
    /* Since mknob reuses the iemgui dialog code, we just
       use the "width" slot for "size" and the "height" slot
       for the number of steps and re-label it on the GUI side. */
//    gui_s("width");            gui_i(x->x_gui.x_w);
//    gui_s("height");           gui_i(x->x_gui.x_h);
    gui_s("size");           gui_i(x->x_gui.x_h);
    gui_s("minimum_range");    gui_f(x->x_min);
    gui_s("maximum_range");    gui_f(x->x_max);
    gui_s("log_scaling");      gui_i(x->x_lin0_log1);
    gui_s("init");             gui_i(x->x_gui.x_loadinit);
    gui_s("steady_on_click");   gui_i(x->x_steady);
    gui_s("send_symbol");      gui_s(srl[0]->s_name);
    gui_s("receive_symbol");   gui_s(srl[1]->s_name);
    gui_s("label");            gui_s(srl[2]->s_name);
    gui_s("x_offset");         gui_i(x->x_gui.x_ldx);
    gui_s("y_offset");         gui_i(x->x_gui.x_ldy);
    gui_s("font_style");       gui_i(x->x_gui.x_font_style);
    gui_s("font_size");        gui_i(x->x_gui.x_fontsize);
    gui_s("background_color"); gui_i(0xffffff & x->x_gui.x_bcol);
    gui_s("foreground_color"); gui_i(0xffffff & x->x_gui.x_fcol);
    gui_s("label_color");      gui_i(0xffffff & x->x_gui.x_lcol);

    gui_end_array();
    gui_end_vmess();

/*
403 404 405 406 407 408 409 410 411
    sprintf(buf, "pdtk_iemgui_dialog %%s KNOB \
	    --------dimensions(pix)(pix):-------- %d %d NONE: %d %d height: \
	    -----------output-range:----------- %g left: %g right: %g \
	    %d lin log %d %d empty %d \
	    %s %s \
	    %s %d %d \
	    %d %d \
	    %d %d %d\n",
	    x->x_gui.x_w, IEM_SL_MINSIZE, x->x_gui.x_h, IEM_GUI_MINSIZE,
412 413
	    x->x_min, x->x_max, 0.0, //no_schedule
	    x->x_lin0_log1, x->x_gui.x_loadinit, x->x_steady, -1,//no multi, but iem-characteristic
414 415
	    srl[0]->s_name, srl[1]->s_name,
	    srl[2]->s_name, x->x_gui.x_ldx, x->x_gui.x_ldy,
416
	    iem_fstyletoint(&x->x_gui), x->x_gui.x_fontsize,
417 418
	    0xffffff & x->x_gui.x_bcol, 0xffffff & x->x_gui.x_fcol, 0xffffff & x->x_gui.x_lcol);
    gfxstub_new(&x->x_gui.x_obj.ob_pd, x, buf);
419
*/
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}

static void knob_bang(t_knob *x)
{
    double out;
    
    if(x->x_lin0_log1)
	out = x->x_min*exp(x->x_k*(double)(x->x_val)*0.01);
    else
	out = (double)(x->x_val)*0.01*x->x_k + x->x_min;
    if((out < 1.0e-10)&&(out > -1.0e-10))
	out = 0.0;
    //post("knob_bang -- x_k: %f", x->x_k);

    outlet_float(x->x_gui.x_obj.ob_outlet, out);
435
    if(iemgui_has_snd(&x->x_gui) && x->x_gui.x_snd->s_thing)
436 437 438
	pd_float(x->x_gui.x_snd->s_thing, out);
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
static void complain_about_surprising_behavior(t_knob *x)
{
    if (!x->x_explained_surprising_behavior)
    {
        pd_error(x, "knob: value is coupled to its height. This "
             "means its output value can change when you change its size. "
             "If you don't need to change the widget's size when running "
             "your patch this is probably ok. Otherwise, "
             "consider using moonlib/knob instead. "
             "(Or perhaps question why audio interfaces are the only UIs "
             "which still employ 'knob' widgets.)");
        x->x_explained_surprising_behavior = 1;
    }
}

454 455
static void knob_dialog(t_knob *x, t_symbol *s, int argc, t_atom *argv)
{
456 457
    if (atom_getintarg(19, argc, argv))
        canvas_apply_setundo(x->x_gui.x_glist, (t_gobj *)x);
458 459
    int w = (int)atom_getintarg(0, argc, argv);
    int h = (int)atom_getintarg(1, argc, argv);
460
    int old_h = x->x_gui.x_h;
461 462 463 464 465 466 467 468 469 470 471 472
    double min = (double)atom_getfloatarg(2, argc, argv);
    double max = (double)atom_getfloatarg(3, argc, argv);
    int lilo = (int)atom_getintarg(4, argc, argv);
    int steady = (int)atom_getintarg(17, argc, argv);
    int sr_flags;

    if(lilo != 0) lilo = 1;
    x->x_lin0_log1 = lilo;
    if(steady)
	x->x_steady = 1;
    else
	x->x_steady = 0;
473
    sr_flags = iemgui_dialog(&x->x_gui, argc, argv);
474 475 476
    x->x_gui.x_h = iemgui_clip_size(w);
    knob_check_height(x, h);
    knob_check_minmax(x, min, max);
477 478 479 480 481 482
    if (old_h != x->x_gui.x_h)
        complain_about_surprising_behavior(x);
    iemgui_draw_config(&x->x_gui);
//    (*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_CONFIG);
    iemgui_draw_io(&x->x_gui, sr_flags);
/*
483
    (*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_IO + sr_flags);
484 485 486
*/
//    (*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_MOVE);
    iemgui_io_draw_move(&x->x_gui);
487 488 489 490 491 492 493
    canvas_fixlinesfor(x->x_gui.x_glist, (t_text*)x);
}

static void knob_motion(t_knob *x, t_floatarg dx, t_floatarg dy)
{
    int old = x->x_val;

494
    if(x->x_gui.x_finemoved)
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
	x->x_pos -= (int)dy;
    else
	x->x_pos -= 100*(int)dy;
    x->x_val = x->x_pos;
    if(x->x_val > (100*x->x_gui.x_h - 100))
    {
	x->x_val = 100*x->x_gui.x_h - 100;
	x->x_pos += 50;
	x->x_pos -= x->x_pos%100;
    }
    if(x->x_val < 0)
    {
	x->x_val = 0;
	x->x_pos -= 50;
	x->x_pos -= x->x_pos%100;
    }
    if(old != x->x_val)
    {
	(*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_UPDATE);
	knob_bang(x);
    }
}

static void knob_click(t_knob *x, t_floatarg xpos, t_floatarg ypos,
			  t_floatarg shift, t_floatarg ctrl, t_floatarg alt)
{
    if(!x->x_steady)
	x->x_val = (int)(100.0 * (x->x_gui.x_h + text_ypix(&x->x_gui.x_obj, x->x_gui.x_glist) - ypos));
    if(x->x_val > (100*x->x_gui.x_h - 100))
	x->x_val = 100*x->x_gui.x_h - 100;
    if(x->x_val < 0)
	x->x_val = 0;
    x->x_pos = x->x_val;
    (*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_UPDATE);
    knob_bang(x);
    glist_grab(x->x_gui.x_glist, &x->x_gui.x_obj.te_g, (t_glistmotionfn)knob_motion,
	       0, xpos, ypos);
}

static int knob_newclick(t_gobj *z, struct _glist *glist,
			    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
    t_knob* x = (t_knob *)z;

539
    if (doit)
540
    {
541
	knob_click (x, (t_floatarg)xpix, (t_floatarg)ypix, (t_floatarg)shift,
542
		       0, (t_floatarg)alt);
543 544
	if (shift)
	    x->x_gui.x_finemoved = 1;
545
	else
546
	    x->x_gui.x_finemoved = 0;
547 548 549 550 551 552 553 554
    }
    return (1);
}

static void knob_set(t_knob *x, t_floatarg f)
{
    double g;

555
    if (x->x_gui.x_reverse)    /* bugfix */
556
    {
557
	if (f > x->x_min)
558
	    f = x->x_min;
559
	if (f < x->x_max)
560 561 562 563
	    f = x->x_max;
    }
    else
    {
564
	if (f > x->x_max)
565
	    f = x->x_max;
566
	if (f < x->x_min)
567 568
	    f = x->x_min;
    }
569
    if (x->x_lin0_log1)
570 571 572 573 574 575 576 577 578
	g = log(f/x->x_min)/x->x_k;
    else
	g = (f - x->x_min) / x->x_k;
    //post("knob_set f: %f", f );
    //post("knob_set g: %f", g );
    x->x_val = (int)(100.0*g + 0.49999);
    // x->x_val = (int)(100.0*g);
    x->x_pos = x->x_val;
    //post("knob_set x_val: %f", x->x_val );
579 580
    if (glist_isvisible(x->x_gui.x_glist))
        (*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_UPDATE);
581 582 583 584
}

static void knob_float(t_knob *x, t_floatarg f)
{
585
    /*  post("knob_set infloat f: %f", f ); */
586
    knob_set(x, f);
587
    if (x->x_gui.x_put_in2out)
588 589 590 591 592 593 594
	knob_bang(x);
}

static void knob_size(t_knob *x, t_symbol *s, int ac, t_atom *av)
{
    x->x_gui.x_h = iemgui_clip_size((int)atom_getintarg(0, ac, av));
    if(ac > 1)
595 596 597
    knob_check_height(x, (int)atom_getintarg(1, ac, av));
    complain_about_surprising_behavior(x);
    iemgui_size(&x->x_gui);
598 599 600
}

static void knob_delta(t_knob *x, t_symbol *s, int ac, t_atom *av)
601 602 603
{
    iemgui_delta(&x->x_gui, s, ac, av);
}
604 605

static void knob_pos(t_knob *x, t_symbol *s, int ac, t_atom *av)
606 607 608
{
    iemgui_pos(&x->x_gui, s, ac, av);
}
609 610 611 612 613 614 615 616

static void knob_range(t_knob *x, t_symbol *s, int ac, t_atom *av)
{
    knob_check_minmax(x, (double)atom_getfloatarg(0, ac, av),
			 (double)atom_getfloatarg(1, ac, av));
}

static void knob_color(t_knob *x, t_symbol *s, int ac, t_atom *av)
617 618 619
{
    iemgui_color(&x->x_gui, s, ac, av);
}
620 621

static void knob_send(t_knob *x, t_symbol *s)
622 623 624
{
    iemgui_send(&x->x_gui, s);
}
625 626

static void knob_receive(t_knob *x, t_symbol *s)
627 628 629
{
    iemgui_receive(&x->x_gui, s);
}
630 631

static void knob_label(t_knob *x, t_symbol *s)
632 633 634
{
    iemgui_label(&x->x_gui, s);
}
635 636

static void knob_label_pos(t_knob *x, t_symbol *s, int ac, t_atom *av)
637 638 639
{
    iemgui_label_pos(&x->x_gui, s, ac, av);
}
640 641

static void knob_label_font(t_knob *x, t_symbol *s, int ac, t_atom *av)
642 643 644
{
    iemgui_label_font(&x->x_gui, s, ac, av);
}
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

static void knob_log(t_knob *x)
{
    x->x_lin0_log1 = 1;
    knob_check_minmax(x, x->x_min, x->x_max);
}

static void knob_lin(t_knob *x)
{
    x->x_lin0_log1 = 0;
    x->x_k = (x->x_max - x->x_min)/(double)(x->x_gui.x_h - 1);
}

static void knob_init(t_knob *x, t_floatarg f)
{
660
    x->x_gui.x_loadinit = (f==0.0)?0:1;
661 662 663 664 665 666 667
}

static void knob_steady(t_knob *x, t_floatarg f)
{
    x->x_steady = (f==0.0)?0:1;
}

668 669 670
#define LB_LOAD 0 /* from g_canvas.h */

static void knob_loadbang(t_knob *x, t_floatarg action)
671
{
672
    if (action == LB_LOAD && x->x_gui.x_loadinit)
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    {
	(*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_UPDATE);
	knob_bang(x);
    } 
}

/*
static void knob_list(t_knob *x, t_symbol *s, int ac, t_atom *av)
{
    int l=iemgui_list((void *)x, &x->x_gui, s, ac, av);

    if(l < 0)
    {
	if(IS_A_FLOAT(av,0))
	    knob_float(x, atom_getfloatarg(0, ac, av));
    }
    else if(l > 0)
    {
	(*x->x_gui.x_draw)(x, x->x_gui.x_glist, IEM_GUI_DRAW_MODE_MOVE);
	canvas_fixlinesfor(x->x_gui.x_glist, (t_text*)x);
    }
}
*/

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
/* we may no longer need h_dragon... */
static void knob__clickhook(t_scalehandle *sh, int newstate)
{
    t_knob *x = (t_knob *)(sh->h_master);
    if (newstate)
    {
        canvas_apply_setundo(x->x_gui.x_glist, (t_gobj *)x);
        if (!sh->h_scale) /* click on a label handle */
            scalehandle_click_label(sh);
    }
    /* We no longer need this "clickhook", as we can handle the dragging
       either in the GUI (for the label handle) or or in canvas_doclick */
    //iemgui__clickhook3(sh,newstate);
    sh->h_dragon = newstate;
}

static void knob__motionhook(t_scalehandle *sh,
                    t_floatarg mouse_x, t_floatarg mouse_y)
{
    if (sh->h_scale)
    {
        t_knob *x = (t_knob *)(sh->h_master);
        int width = x->x_gui.x_h,
            height = x->x_gui.x_h;
        int x1, y1, x2, y2, d;
        x1 = text_xpix(&x->x_gui.x_obj, x->x_gui.x_glist);
        y1 = text_ypix(&x->x_gui.x_obj, x->x_gui.x_glist);
        x2 = x1 + width;
        y2 = y1 + height;

        /* This is convoluted, but I can't think of another
           way to get this behavior... */
        if (mouse_x <= x2)
        {
            if (mouse_y > y2)
                d = mouse_y - y2;
            else if (abs(mouse_y - y2) < abs(mouse_x - x2))
                d = mouse_y - y2;
            else
                d = mouse_x - x2;
        }
        else
        {
            if (mouse_y <= y2)
                d = mouse_x - x2;
            else
                d = maxi(mouse_y - y2, mouse_x - x2);
        }
        sh->h_dragx = d;
        sh->h_dragy = d;
        scalehandle_drag_scale(sh);

        width = maxi(width + d, IEM_GUI_MINSIZE);
        height = width;

        /* Explain some suprising behavior to the user... */

        x->x_gui.x_w = width;
        x->x_gui.x_h = height;
        /* recalculate x_k and stuff */
        knob_check_height(x, height);
        knob_check_minmax(x, x->x_min, x->x_max);

        complain_about_surprising_behavior(x);

        if (glist_isvisible(x->x_gui.x_glist))
        {
            knob_draw_config(x, x->x_gui.x_glist);
            scalehandle_unclick_scale(sh);
        }

        int properties = gfxstub_haveproperties((void *)x);
        if (properties)
        {
            int new_w = x->x_gui.x_w + sh->h_dragx;
            // This should actually be "size", but we're using the
            // "width" input in dialog_iemgui and just relabelling it
            // as a kluge.
            properties_set_field_int(properties, "width", new_w);
        }
    }
    scalehandle_dragon_label(sh, mouse_x, mouse_y);
}

781 782 783 784 785 786 787 788 789
static void *knob_new(t_symbol *s, int argc, t_atom *argv)
{
    t_knob *x = (t_knob *)pd_new(knob_class);
    int bflcol[]={-262144, -1, -1};
    int w=IEM_KNOB_DEFAULTSIZE, h=IEM_KNOB_DEFAULTSIZE;
    int lilo=0, ldx=0, ldy=-8;
    int fs=8, v=0, steady=1;
    double min=0.0, max=(double)(IEM_SL_DEFAULTSIZE-1);

790 791
    iem_inttosymargs(&x->x_gui, 0);
    iem_inttofstyle(&x->x_gui, 0);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

    if(((argc == 17)||(argc == 18))&&IS_A_FLOAT(argv,0)&&IS_A_FLOAT(argv,1)
       &&IS_A_FLOAT(argv,2)&&IS_A_FLOAT(argv,3)
       &&IS_A_FLOAT(argv,4)&&IS_A_FLOAT(argv,5)
       &&(IS_A_SYMBOL(argv,6)||IS_A_FLOAT(argv,6))
       &&(IS_A_SYMBOL(argv,7)||IS_A_FLOAT(argv,7))
       &&(IS_A_SYMBOL(argv,8)||IS_A_FLOAT(argv,8))
       &&IS_A_FLOAT(argv,9)&&IS_A_FLOAT(argv,10)
       &&IS_A_FLOAT(argv,11)&&IS_A_FLOAT(argv,12)&&IS_A_FLOAT(argv,13)
       &&IS_A_FLOAT(argv,14)&&IS_A_FLOAT(argv,15)&&IS_A_FLOAT(argv,16))
   {
        w = (int)atom_getintarg(0, argc, argv);
        h = (int)atom_getintarg(1, argc, argv);
        min = (double)atom_getfloatarg(2, argc, argv);
        max = (double)atom_getfloatarg(3, argc, argv);
        lilo = (int)atom_getintarg(4, argc, argv);
808
        iem_inttosymargs(&x->x_gui, atom_getintarg(5, argc, argv));
809 810 811
        iemgui_new_getnames(&x->x_gui, 6, argv);
        ldx = (int)atom_getintarg(9, argc, argv);
        ldy = (int)atom_getintarg(10, argc, argv);
812
        iem_inttofstyle(&x->x_gui, atom_getintarg(11, argc, argv));
813 814 815 816 817 818 819
        fs = (int)atom_getintarg(12, argc, argv);
        bflcol[0] = (int)atom_getintarg(13, argc, argv);
        bflcol[1] = (int)atom_getintarg(14, argc, argv);
        bflcol[2] = (int)atom_getintarg(15, argc, argv);
        v = (int)atom_getintarg(16, argc, argv);
    }
    else iemgui_new_getnames(&x->x_gui, 6, 0);
820
    if ((argc == 18)&&IS_A_FLOAT(argv,17))
821 822 823 824
        steady = (int)atom_getintarg(17, argc, argv);

    x->x_gui.x_draw = (t_iemfunptr)knob_draw;

825
/*
826 827
    x->x_gui.x_fsf.x_snd_able = 1;
    x->x_gui.x_fsf.x_rcv_able = 1;
828
*/
829 830

    x->x_gui.x_glist = (t_glist *)canvas_getcurrent();
831
    if (x->x_gui.x_loadinit)
832 833 834 835 836 837 838 839
        x->x_val = v;
    else
        x->x_val = 0;
    x->x_pos = x->x_val;
    if(lilo != 0) lilo = 1;
    x->x_lin0_log1 = lilo;
    if(steady != 0) steady = 1;
    x->x_steady = steady;
840
/*
841 842 843 844
    if (!strcmp(x->x_gui.x_snd->s_name, "empty"))
        x->x_gui.x_fsf.x_snd_able = 0;
    if (!strcmp(x->x_gui.x_rcv->s_name, "empty"))
        x->x_gui.x_fsf.x_rcv_able = 0;
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
*/
    if (iem_fstyletoint(&x->x_gui) == 1)
    {
        //strcpy(x->x_gui.x_font, "helvetica");
    }
    else if (iem_fstyletoint(&x->x_gui) == 2)
    {
        //strcpy(x->x_gui.x_font, "times");
    }
    else
    {
        x->x_gui.x_font_style = 0;
        //strcpy(x->x_gui.x_font, "courier");
    }
    if (iemgui_has_rcv(&x->x_gui))
860 861 862
        pd_bind(&x->x_gui.x_obj.ob_pd, x->x_gui.x_rcv);
    x->x_gui.x_ldx = ldx;
    x->x_gui.x_ldy = ldy;
863
    if (fs < 4)
864 865 866 867 868 869 870 871 872
        fs = 4;
    x->x_gui.x_fontsize = fs;
    x->x_gui.x_h = iemgui_clip_size(h);
    knob_check_height(x, w);
    knob_check_minmax(x, min, max);
    iemgui_all_colfromload(&x->x_gui, bflcol);
    //x->x_thick = 0;
    iemgui_verify_snd_ne_rcv(&x->x_gui);
    outlet_new(&x->x_gui.x_obj, &s_float);
873 874 875 876 877 878 879 880
    x->x_gui.x_obj.te_iemgui = 1;
    x->x_explained_surprising_behavior = 0;

    x->x_gui.x_handle = scalehandle_new((t_object *)x,
        x->x_gui.x_glist, 1, knob__clickhook, knob__motionhook);
    x->x_gui.x_lhandle = scalehandle_new((t_object *)x,
        x->x_gui.x_glist, 0, knob__clickhook, knob__motionhook);

881 882 883 884 885 886 887
    return (x);
}



static void knob_free(t_knob *x)
{
888
    if (iemgui_has_snd(&x->x_gui))
889
	pd_unbind(&x->x_gui.x_obj.ob_pd, iemgui_key_sym);
890
    if (iemgui_has_rcv(&x->x_gui))
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	pd_unbind(&x->x_gui.x_obj.ob_pd, x->x_gui.x_rcv);
    gfxstub_deleteforkey(x);
}

void knob_setup(void)
{
    knob_class = class_new(gensym("knob"), (t_newmethod)knob_new,
			      (t_method)knob_free, sizeof(t_knob), 0, A_GIMME, 0);
    class_addcreator((t_newmethod)knob_new, gensym("knob"), A_GIMME, 0);
    class_addbang(knob_class,knob_bang);
    class_addfloat(knob_class,knob_float);

    /*    class_addlist(knob_class, knob_list); */
    class_addmethod(knob_class, (t_method)knob_click, gensym("click"),
		    A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
    class_addmethod(knob_class, (t_method)knob_motion, gensym("motion"),
		    A_FLOAT, A_FLOAT, 0);
    class_addmethod(knob_class, (t_method)knob_dialog, gensym("dialog"),
		    A_GIMME, 0);
910 911
    class_addmethod(knob_class, (t_method)knob_loadbang, gensym("loadbang"),
        A_DEFFLOAT, 0);
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
    class_addmethod(knob_class, (t_method)knob_set, gensym("set"), A_FLOAT, 0);
    class_addmethod(knob_class, (t_method)knob_size, gensym("size"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_delta, gensym("delta"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_pos, gensym("pos"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_range, gensym("range"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_color, gensym("color"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_send, gensym("send"), A_DEFSYM, 0);
    class_addmethod(knob_class, (t_method)knob_receive, gensym("receive"), A_DEFSYM, 0);
    class_addmethod(knob_class, (t_method)knob_label, gensym("label"), A_DEFSYM, 0);
    class_addmethod(knob_class, (t_method)knob_label_pos, gensym("label_pos"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_label_font, gensym("label_font"), A_GIMME, 0);
    class_addmethod(knob_class, (t_method)knob_log, gensym("log"), 0);
    class_addmethod(knob_class, (t_method)knob_lin, gensym("lin"), 0);
    class_addmethod(knob_class, (t_method)knob_init, gensym("init"), A_FLOAT, 0);
    class_addmethod(knob_class, (t_method)knob_steady, gensym("steady"), A_FLOAT, 0);
    if(!iemgui_key_sym)
 	iemgui_key_sym = gensym("#keyname");
    knob_widgetbehavior.w_getrectfn =    knob_getrect;
    knob_widgetbehavior.w_displacefn =   iemgui_displace;
931
    knob_widgetbehavior.w_displacefnwtag = iemgui_displace_withtag;
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    knob_widgetbehavior.w_selectfn =     iemgui_select;
    knob_widgetbehavior.w_activatefn =   NULL;
    knob_widgetbehavior.w_deletefn =     iemgui_delete;
    knob_widgetbehavior.w_visfn =        iemgui_vis;
    knob_widgetbehavior.w_clickfn =      knob_newclick;
#if PD_MINOR_VERSION < 37 /* TODO: remove old behaviour in exactly 2 months from now */
	knob_widgetbehavior.w_propertiesfn = knob_properties;;
    knob_widgetbehavior.w_savefn =       knob_save;
#else
	class_setpropertiesfn(knob_class, &knob_properties);
	class_setsavefn(knob_class, &knob_save);
#endif
	class_setwidget(knob_class, &knob_widgetbehavior);
    class_sethelpsymbol(knob_class, gensym("knob"));
}