/* 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 */ #include "config.h" #include #include #include #include #include "m_pd.h" #include "g_canvas.h" #include "m_imp.h" #include "g_all_guis.h" #include t_symbol *s_empty; int iemgui_color_hex[] = { 0xfcfcfc, 0xa0a0a0, 0x404040, 0xfce0e0, 0xfce0c0, 0xfcfcc8, 0xd8fcd8, 0xd8fcfc, 0xdce4fc, 0xf8d8fc, 0xe0e0e0, 0x7c7c7c, 0x202020, 0xfc2828, 0xfcac44, 0xe8e828, 0x14e814, 0x28f4f4, 0x3c50fc, 0xf430f0, 0xbcbcbc, 0x606060, 0x000000, 0x8c0808, 0x583000, 0x782814, 0x285014, 0x004450, 0x001488, 0x580050 }; int iemgui_clip_size(int size) {return maxi(size,IEM_GUI_MINSIZE);} int iemgui_clip_font(int size) {return maxi(size,IEM_FONT_MINSIZE);} static void scalehandle_check_and_redraw(t_iemgui *x); static int iemgui_modulo_color(int col) { const int IEM_GUI_MAX_COLOR = 30; col %= IEM_GUI_MAX_COLOR; if (col<0) col += IEM_GUI_MAX_COLOR; return col; } t_symbol *iemgui_dollar2raute(t_symbol *s) { char buf[MAXPDSTRING+1], *s1, *s2; if (strlen(s->s_name) >= MAXPDSTRING) return (s); for (s1 = s->s_name, s2 = buf; ; s1++, s2++) { if (*s1 == '$') *s2 = '#'; else if (!(*s2 = *s1)) break; } return(gensym(buf)); } t_symbol *iemgui_raute2dollar(t_symbol *s) { char buf[MAXPDSTRING+1], *s1, *s2; if (strlen(s->s_name) >= MAXPDSTRING) return (s); for (s1 = s->s_name, s2 = buf; ; s1++, s2++) { if (*s1 == '#') *s2 = '$'; else if (!(*s2 = *s1)) break; } return(gensym(buf)); } void iemgui_verify_snd_ne_rcv(t_iemgui *x) { x->x_put_in2out = !(iemgui_has_snd(x) && iemgui_has_rcv(x) && x->x_snd==x->x_rcv); } t_symbol *iemgui_getfloatsym(t_atom *a) { if (IS_A_SYMBOL(a,0)) return (atom_getsymbol(a)); if (IS_A_FLOAT(a,0)) { char str[40]; sprintf(str, "%d", (int)atom_getint(a)); return gensym(str); } return s_empty; } t_symbol *iemgui_getfloatsymarg(int i, int argc, t_atom *argv) { if (i>=0 && ix_snd = iemgui_getfloatsym(argv+indx+0); x->x_rcv = iemgui_getfloatsym(argv+indx+1); x->x_lab = iemgui_getfloatsym(argv+indx+2); } else x->x_snd = x->x_rcv = x->x_lab = s_empty; x->x_snd_unexpanded = x->x_rcv_unexpanded = x->x_lab_unexpanded = 0; x->x_binbufindex = indx; x->x_labelbindex = indx + 3; } /* initialize a single symbol in unexpanded form. We reach into the binbuf to grab them; if there's nothing there, set it to the fallback; if still nothing, set to "empty". */ static void iemgui_init_sym2dollararg(t_iemgui *x, t_symbol **symp, int indx, t_symbol *fallback) { if (!*symp) { t_binbuf *b = x->x_obj.ob_binbuf; if (binbuf_getnatom(b) > indx) { char buf[80]; atom_string(binbuf_getvec(b) + indx, buf, 80); *symp = gensym(buf); } else if (fallback) *symp = fallback; else *symp = s_empty; } } /* get the unexpanded versions of the symbols; initialize them if necessary. */ void iemgui_all_sym2dollararg(t_iemgui *x, t_symbol **srlsym) { iemgui_init_sym2dollararg(x, &x->x_snd_unexpanded, x->x_binbufindex+1, x->x_snd); iemgui_init_sym2dollararg(x, &x->x_rcv_unexpanded, x->x_binbufindex+2, x->x_rcv); iemgui_init_sym2dollararg(x, &x->x_lab_unexpanded, x->x_labelbindex, x->x_lab); srlsym[0] = x->x_snd_unexpanded; srlsym[1] = x->x_rcv_unexpanded; srlsym[2] = x->x_lab_unexpanded; } static int col2save(int col) { return -1-(((0xfc0000 & col) >> 6)|((0xfc00 & col) >> 4)|((0xfc & col) >> 2)); } void iemgui_all_col2save(t_iemgui *x, int *bflcol) { bflcol[0] = col2save(x->x_bcol); bflcol[1] = col2save(x->x_fcol); bflcol[2] = col2save(x->x_lcol); } static int colfromload(int col) { if(col) { col = -1-col; return ((col & 0x3f000) << 6)|((col & 0xfc0) << 4)|((col & 0x3f) << 2); } else return iemgui_color_hex[iemgui_modulo_color(col)]; } void iemgui_all_colfromload(t_iemgui *x, int *bflcol) { x->x_bcol = colfromload(bflcol[0]); x->x_fcol = colfromload(bflcol[1]); x->x_lcol = colfromload(bflcol[2]); } static int iemgui_compatible_col(int i) { if(i >= 0) return(iemgui_color_hex[(iemgui_modulo_color(i))]); return((-1-i)&0xffffff); } void iemgui_all_raute2dollar(t_symbol **srlsym) { srlsym[0] = iemgui_raute2dollar(srlsym[0]); srlsym[1] = iemgui_raute2dollar(srlsym[1]); srlsym[2] = iemgui_raute2dollar(srlsym[2]); } void iemgui_send(t_iemgui *x, t_symbol *s) { t_symbol *snd; if (s == &s_) s = s_empty; //tb: fix for empty label int oldsndrcvable=0; if(iemgui_has_rcv(x)) oldsndrcvable += IEM_GUI_OLD_RCV_FLAG; if(iemgui_has_snd(x)) oldsndrcvable += IEM_GUI_OLD_SND_FLAG; snd = iemgui_raute2dollar(s); x->x_snd_unexpanded = snd; x->x_snd = snd = canvas_realizedollar(x->x_glist, snd); iemgui_verify_snd_ne_rcv(x); iemgui_draw_io(x, x->x_glist, oldsndrcvable); } void iemgui_receive(t_iemgui *x, t_symbol *s) { t_symbol *rcv; if (s == &s_) s = s_empty; //tb: fix for empty label int oldsndrcvable=0; if(iemgui_has_rcv(x)) oldsndrcvable += IEM_GUI_OLD_RCV_FLAG; if(iemgui_has_snd(x)) oldsndrcvable += IEM_GUI_OLD_SND_FLAG; rcv = iemgui_raute2dollar(s); x->x_rcv_unexpanded = rcv; rcv = canvas_realizedollar(x->x_glist, rcv); if(s!=s_empty) { if(rcv!=x->x_rcv) { if(iemgui_has_rcv(x)) pd_unbind((t_pd *)x, x->x_rcv); x->x_rcv = rcv; pd_bind((t_pd *)x, x->x_rcv); } } else if(s==s_empty && iemgui_has_rcv(x)) { pd_unbind((t_pd *)x, x->x_rcv); x->x_rcv = rcv; } iemgui_verify_snd_ne_rcv(x); iemgui_draw_io(x, x->x_glist, oldsndrcvable); } void iemgui_label(t_iemgui *x, t_symbol *s) { if (s == &s_) s = s_empty; //tb: fix for empty label t_symbol *lab = iemgui_raute2dollar(s); x->x_lab_unexpanded = lab; x->x_lab = lab = canvas_realizedollar(x->x_glist, lab); if(glist_isvisible(x->x_glist)) { sys_vgui(".x%lx.c itemconfigure %lxLABEL -text {%s} \n", glist_getcanvas(x->x_glist), x, s!=s_empty?x->x_lab->s_name:""); iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_CONFIG); } } void iemgui_label_pos(t_iemgui *x, t_symbol *s, int ac, t_atom *av) { x->x_ldx = atom_getintarg(0, ac, av); x->x_ldy = atom_getintarg(1, ac, av); if(glist_isvisible(x->x_glist)) { sys_vgui(".x%lx.c coords %lxLABEL %d %d\n", glist_getcanvas(x->x_glist), x, text_xpix((t_object *)x,x->x_glist)+x->x_ldx, text_ypix((t_object *)x,x->x_glist)+x->x_ldy); iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_CONFIG); } } void iemgui_label_font(t_iemgui *x, t_symbol *s, int ac, t_atom *av) { int f = atom_getintarg(0, ac, av); if (f<0 || f>2) f=0; x->x_font_style = f; x->x_fontsize = maxi(atom_getintarg(1, ac, av),4); if(glist_isvisible(x->x_glist)) { sys_vgui(".x%lx.c itemconfigure %lxLABEL -font %s\n", glist_getcanvas(x->x_glist), x, iemgui_font(x), x->x_fontsize, sys_fontweight); iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_CONFIG); } } //Sans: 84 x 10 (14) -> 6 x 10 -> 1.0 //Helvetica: 70 x 10 (14) -> 5 x 10 -> 0.83333 //Times: 61 x 10 (14) -> 4.357 x 10 -> 0.72619; 0.735 appears to work better // We use this global var to check when getrect should report label: // It should report it when drawing inside gop to see if we truly fit. // Otherwise we should not report it while inside gop to avoid label being // misinterpreted as part of the "hot" area of a widget (e.g. toggle) extern int gop_redraw; void iemgui_label_getrect(t_iemgui x_gui, t_glist *x, int *xp1, int *yp1, int *xp2, int *yp2) { //fprintf(stderr,"gop_redraw = %d\n", gop_redraw); if (!gop_redraw) { //fprintf(stderr,"ignoring label\n"); return; } t_float width_multiplier; int label_length; int label_x1; int label_y1; int label_x2; int label_y2; int actual_fontsize; //seems tk does its own thing when rendering int actual_height; if (x->gl_isgraph && !glist_istoplevel(x)) { if (x_gui.x_lab!=s_empty) { switch(x_gui.x_font_style) { case 1: width_multiplier = 0.83333; break; case 2: width_multiplier = 0.735; break; default: width_multiplier = 1.0; break; } actual_fontsize = x_gui.x_fontsize; actual_height = actual_fontsize; //exceptions if (x_gui.x_font_style == 0 && (actual_fontsize == 8 || actual_fontsize == 13 || actual_fontsize % 10 == 1 || actual_fontsize % 10 == 6 || (actual_fontsize > 48 && actual_fontsize < 100 && (actual_fontsize %10 == 4 || actual_fontsize %10 == 9)))) { actual_fontsize += 1; } else if (x_gui.x_font_style == 1 && actual_fontsize >= 5 && actual_fontsize < 13 && actual_fontsize % 2 == 1) actual_fontsize += 1; else if (x_gui.x_font_style == 2 && actual_fontsize >= 5 && actual_fontsize % 2 == 1) actual_fontsize += 1; if (actual_height == 9) actual_height += 1; //done with exceptions width_multiplier = width_multiplier * (actual_fontsize * 0.6); label_length = strlen(x_gui.x_lab->s_name); label_x1 = *xp1 + x_gui.x_ldx; label_y1 = *yp1 + x_gui.x_ldy - actual_height/2; label_x2 = label_x1 + (label_length * width_multiplier); label_y2 = label_y1 + actual_height*1.1; //DEBUG //fprintf(stderr,"%f %d %d\n", width_multiplier, // label_length, x_gui.x_font_style); //sys_vgui(".x%lx.c delete iemguiDEBUG\n", x); //sys_vgui(".x%lx.c create rectangle %d %d %d %d " // "-tags iemguiDEBUG\n", // x, label_x1, label_y1, label_x2, label_y2); if (label_x1 < *xp1) *xp1 = label_x1; if (label_x2 > *xp2) *xp2 = label_x2; if (label_y1 < *yp1) *yp1 = label_y1; if (label_y2 > *yp2) *yp2 = label_y2; //DEBUG //sys_vgui(".x%lx.c delete iemguiDEBUG\n", x); //sys_vgui(".x%lx.c create rectangle %d %d %d %d " // "-tags iemguiDEBUG\n", x, *xp1, *yp1, *xp2, *yp2); } } } void iemgui_shouldvis(t_iemgui *x, int mode) { gop_redraw = 1; if(gobj_shouldvis((t_gobj *)x, x->x_glist)) { if (!x->x_vis) { //fprintf(stderr,"draw new %d\n", mode); iemgui_draw_new(x, x->x_glist); canvas_fixlinesfor(glist_getcanvas(x->x_glist), (t_text*)x); x->x_vis = 1; if (x->x_glist != glist_getcanvas(x->x_glist)) { /* if we are inside gop and just have had our object's properties changed we'll adjust our layer position to ensure that ordering is honored */ t_canvas *canvas = glist_getcanvas(x->x_glist); t_gobj *y = (t_gobj *)x->x_glist; gobj_vis(y, canvas, 0); gobj_vis(y, canvas, 1); // reorder it visually glist_redraw(canvas); /* // some day when the object tagging is // properly done for all GUI objects glist_noselect(canvas); glist_select(canvas, y); t_gobj *yy = canvas->gl_list; if (yy != y) { fprintf(stderr,"not bottom\n"); while (yy && yy->g_next != y) { fprintf(stderr,"+\n"); yy = yy->g_next; } // now we have yy which is right before our y graph t_object *ob = NULL; t_rtext *yr = NULL; if (yy) { yr = glist_findrtext(canvas, (t_text *)yy); } if (yr) { fprintf(stderr,"lower\n"); sys_vgui(".x%lx.c lower selected %s\n", canvas, rtext_gettag(yr)); sys_vgui(".x%lx.c raise selected %s\n", canvas, rtext_gettag(yr)); //canvas_raise_all_cords(canvas); } else { // fall back to legacy redraw for objects // that are not patchable fprintf(stderr,"lower fallback redraw\n"); canvas_redraw(canvas); } } else { // we get here if we are supposed to go // all the way to the bottom fprintf(stderr,"lower to the bottom\n"); sys_vgui(".x%lx.c lower selected\n", canvas); } glist_noselect(canvas); */ } } //fprintf(stderr,"draw move x->x_w=%d\n", x->x_w); x->x_draw(x, x->x_glist, mode); scalehandle_check_and_redraw(x); canvas_fixlinesfor(glist_getcanvas(x->x_glist), (t_text*)x); } else if (x->x_vis) { //fprintf(stderr,"draw erase %d\n", mode); iemgui_draw_erase(x, x->x_glist); x->x_vis = 0; } gop_redraw = 0; } void iemgui_size(t_iemgui *x) { if(glist_isvisible(x->x_glist)) iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE); } void iemgui_delta(t_iemgui *x, t_symbol *s, int ac, t_atom *av) { x->x_obj.te_xpix += atom_getintarg(0, ac, av); x->x_obj.te_ypix += atom_getintarg(1, ac, av); if(glist_isvisible(x->x_glist)) iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE); } void iemgui_pos(t_iemgui *x, t_symbol *s, int ac, t_atom *av) { x->x_obj.te_xpix = atom_getintarg(0, ac, av); x->x_obj.te_ypix = atom_getintarg(1, ac, av); if(glist_isvisible(x->x_glist)) iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE); } void iemgui_color(t_iemgui *x, t_symbol *s, int ac, t_atom *av) { x->x_bcol = iemgui_compatible_col(atom_getintarg(0, ac, av)); if(ac > 2) { x->x_fcol = iemgui_compatible_col(atom_getintarg(1, ac, av)); x->x_lcol = iemgui_compatible_col(atom_getintarg(2, ac, av)); } else x->x_lcol = iemgui_compatible_col(atom_getintarg(1, ac, av)); if(glist_isvisible(x->x_glist)) x->x_draw(x, x->x_glist, IEM_GUI_DRAW_MODE_CONFIG); } void iemgui_displace(t_gobj *z, t_glist *glist, int dx, int dy) { t_iemgui *x = (t_iemgui *)z; x->x_obj.te_xpix += dx; x->x_obj.te_ypix += dy; iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE); } void iemgui_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy) { t_iemgui *x = (t_iemgui *)z; x->x_obj.te_xpix += dx; x->x_obj.te_ypix += dy; //x->x_gui.x_draw((void *)z, glist, IEM_GUI_DRAW_MODE_MOVE); canvas_fixlinesfor(glist_getcanvas(glist), (t_text *)z); } void iemgui_select(t_gobj *z, t_glist *glist, int selected) { t_iemgui *x = (t_iemgui *)z; t_canvas *canvas=glist_getcanvas(glist); if (selected) x->x_selected = canvas; else x->x_selected = NULL; char fcol[8]; sprintf(fcol,"#%6.6x", x->x_fcol); sys_vgui(".x%lx.c itemconfigure {x%lx&&border} -stroke %s\n", canvas, x, x->x_selected && x->x_glist == canvas ? selection_color : fcol); x->x_draw((void *)z, glist, IEM_GUI_DRAW_MODE_SELECT); scalehandle_draw(x,glist); iemgui_label_draw_select(x,canvas); iemgui_tag_selected(x,canvas); } void iemgui_delete(t_gobj *z, t_glist *glist) { canvas_deletelinesfor(glist, (t_text *)z); } void iemgui_vis(t_gobj *z, t_glist *glist, int vis) { t_iemgui *x = (t_iemgui *)z; if (gobj_shouldvis(z, glist)) { if (vis) iemgui_draw_new(x, glist); else { iemgui_draw_erase(x, x->x_glist); sys_unqueuegui(z); } x->x_vis = vis; } } void iemgui_save(t_iemgui *x, t_symbol **srl, int *bflcol) { if (srl) { srl[0] = x->x_snd; srl[1] = x->x_rcv; srl[2] = x->x_lab; } iemgui_all_sym2dollararg(x, srl); iemgui_all_col2save(x, bflcol); } void iemgui_properties(t_iemgui *x, t_symbol **srl) { srl[0] = x->x_snd; srl[1] = x->x_rcv; srl[2] = x->x_lab; iemgui_all_sym2dollararg(x, srl); srl[0] = iemgui_dollar2raute(srl[0]); srl[1] = iemgui_dollar2raute(srl[1]); srl[2] = iemgui_dollar2raute(srl[2]); } int iemgui_dialog(t_iemgui *x, int argc, t_atom *argv) { t_symbol *srl[3]; x->x_loadinit = !!atom_getintarg(5, argc, argv); srl[0] = iemgui_getfloatsymarg(7,argc,argv); srl[1] = iemgui_getfloatsymarg(8,argc,argv); srl[2] = iemgui_getfloatsymarg(9,argc,argv); x->x_ldx = atom_getintarg(10, argc, argv); x->x_ldy = atom_getintarg(11, argc, argv); int f = atom_getintarg(12, argc, argv); x->x_fontsize = maxi(atom_getintarg(13, argc, argv),4); x->x_bcol = atom_getintarg(14, argc, argv) & 0xffffff; x->x_fcol = atom_getintarg(15, argc, argv) & 0xffffff; x->x_lcol = atom_getintarg(16, argc, argv) & 0xffffff; int oldsndrcvable=0; if(iemgui_has_rcv(x)) oldsndrcvable |= IEM_GUI_OLD_RCV_FLAG; if(iemgui_has_snd(x)) oldsndrcvable |= IEM_GUI_OLD_SND_FLAG; iemgui_all_raute2dollar(srl); x->x_snd_unexpanded=srl[0]; srl[0]=canvas_realizedollar(x->x_glist, srl[0]); x->x_rcv_unexpanded=srl[1]; srl[1]=canvas_realizedollar(x->x_glist, srl[1]); x->x_lab_unexpanded=srl[2]; srl[2]=canvas_realizedollar(x->x_glist, srl[2]); if(srl[1]!=x->x_rcv) { if(iemgui_has_rcv(x)) pd_unbind((t_pd *)x, x->x_rcv); x->x_rcv = srl[1]; pd_bind((t_pd *)x, x->x_rcv); } x->x_snd = srl[0]; x->x_lab = srl[2]; if(f<0 || f>2) f=0; x->x_font_style = f; iemgui_verify_snd_ne_rcv(x); canvas_dirty(x->x_glist, 1); return oldsndrcvable; } void iem_inttosymargs(t_iemgui *x, int n) { x->x_loadinit = (n >> 0); x->x_locked = 0; x->x_reverse = 0; } int iem_symargstoint(t_iemgui *x) { return ((x->x_loadinit & 1) << 0); } void iem_inttofstyle(t_iemgui *x, int n) { x->x_font_style = (n >> 0); x->x_selected = NULL; x->x_finemoved = 0; x->x_put_in2out = 0; x->x_change = 0; } int iem_fstyletoint(t_iemgui *x) { return ((x->x_font_style << 0) & 63); } //---------------------------------------------------------------- // SCALEHANDLE COMMON CODE (by Mathieu, refactored from existing code) extern int gfxstub_haveproperties(void *key); int mini(int a, int b) {return ab?a:b;} float minf(float a, float b) {return ab?a:b;} // in all 20 casesĀ : // [bng], [tgl], [hradio], [vradio], [hsl], [vsl], [cnv], [nbx], [vu] // for both scale & label, plus canvas' scale & move. void scalehandle_bind(t_scalehandle *h) { sys_vgui("bind %s