diff options
-rw-r--r-- | text-help.pd | 35 | ||||
-rw-r--r-- | text.c | 82 |
2 files changed, 85 insertions, 32 deletions
diff --git a/text-help.pd b/text-help.pd index 838bf34..c26f022 100644 --- a/text-help.pd +++ b/text-help.pd @@ -77,7 +77,7 @@ Black -highlightthickness 1 -insertbackground Black -insertborderwidth #X msg 159 250 gumby; #X msg 202 250 gobbler; #X obj 404 -212 hsl 128 15 0 127 0 0 empty empty empty -2 -6 0 10 -262144 --1 -1 200 1; +-1 -1 0 1; #X obj 470 -90 tgl 15 0 empty empty empty 0 -6 0 10 -262144 -1 -1 0 1; #X msg 109 -80 key a; @@ -99,7 +99,7 @@ Black -highlightthickness 1 -insertbackground Black -insertborderwidth #X msg 401 -192 borderwidth \$1; #X msg 582 -192 relief \$1; #X msg 582 -58 wrap \$1; -#X msg 328 -70 insertwidth \$1; +#X msg 329 -70 insertwidth \$1; #X msg 557 305 undo \$1; #X msg 652 304 state \$1; #X msg 113 110 size 200 100; @@ -141,6 +141,37 @@ TkWidget library; #X msg 399 75 #ff0; #X msg 435 75 grey; #X msg 471 75 black; +#N canvas 410 182 450 300 adding 0; +#X obj 90 229 text 200 60 0; +#X text 53 23 You can add text one character at a time using the "key" +message. It works either with the character itself or the ASCII value +of the character:; +#X msg 90 102 key \$1; +#X obj 94 77 hsl 128 15 0 127 0 0 empty empty empty -2 -8 0 10 -262144 +-1 -1 0 1; +#X msg 126 202 key "; +#X msg 169 202 key a; +#X msg 215 202 key B; +#X msg 145 102 key 10; +#X msg 259 202 key [; +#X msg 145 122 key 32; +#X text 198 103 carriage return; +#X text 198 122 space; +#X msg 145 142 key 100; +#X text 198 142 letter "d"; +#X msg 145 163 key 92; +#X text 199 161 backslash; +#X connect 2 0 0 0; +#X connect 3 0 2 0; +#X connect 4 0 0 0; +#X connect 5 0 0 0; +#X connect 6 0 0 0; +#X connect 7 0 0 0; +#X connect 8 0 0 0; +#X connect 9 0 0 0; +#X connect 12 0 0 0; +#X connect 14 0 0 0; +#X restore 199 -200 pd adding with the key message; #X connect 0 0 2 0; #X connect 0 0 7 0; #X connect 0 1 6 0; @@ -27,6 +27,9 @@ /* TODO: window name "handle1376fc00" already exists in parent */ /* TODO: figure out window vs. text width/height */ /* TODO: add x scrollbar */ +/* TODO: make "insert" function based on the text widget "insert" */ +/* TODO: make [key( support chars > 127 */ + #define TEXT_DEFAULT_COLOR "grey90" #define TEXT_DEFAULT_WIDTH 200 @@ -109,17 +112,19 @@ static char *textwidget_tk_options[] = { /* common symbols to preload */ -static t_symbol *scrollbars_symbol; -static t_symbol *size_symbol; static t_symbol *backspace_symbol; -static t_symbol *return_symbol; -static t_symbol *space_symbol; -static t_symbol *tab_symbol; +static t_symbol *down_symbol; static t_symbol *escape_symbol; +static t_symbol *id_symbol; static t_symbol *left_symbol; +static t_symbol *query_callback_symbol; +static t_symbol *return_symbol; static t_symbol *right_symbol; +static t_symbol *scrollbars_symbol; +static t_symbol *size_symbol; +static t_symbol *space_symbol; +static t_symbol *tab_symbol; static t_symbol *up_symbol; -static t_symbol *down_symbol; /* -------------------- function prototypes --------------------------------- */ @@ -128,12 +133,22 @@ static void textwidget_query_callback(t_textwidget *x, t_symbol *s, int argc, t_ /* -------------------- widget helper functions ----------------------------- */ + +static void query_id(t_textwidget *x) +{ + t_atom id[2]; + t_symbol *widget_id = x->widget_id; + SETSYMBOL(id, id_symbol); + SETSYMBOL(id + 1, widget_id); + textwidget_query_callback(x, query_callback_symbol, 2, id); +} + static void query_scrollbars(t_textwidget *x) { t_atom state[2]; SETSYMBOL(state, scrollbars_symbol); SETFLOAT(state + 1, (t_float)x->have_scrollbars); - textwidget_query_callback(x, gensym("query_callback"), 2, state); + textwidget_query_callback(x, query_callback_symbol, 2, state); } static void query_size(t_textwidget *x) @@ -142,7 +157,7 @@ static void query_size(t_textwidget *x) SETSYMBOL(coords, size_symbol); SETFLOAT(coords + 1, (t_float)x->width); SETFLOAT(coords + 2, (t_float)x->height); - textwidget_query_callback(x, gensym("query_callback"), 3, coords); + textwidget_query_callback(x, query_callback_symbol, 3, coords); } static void set_tkwidgets_ids(t_textwidget *x, t_canvas *canvas) @@ -420,30 +435,32 @@ static void textwidget_key(t_textwidget* x, t_symbol *s, int argc, t_atom *argv { DEBUG(post("textwidget_key");); t_symbol *tmp_symbol = s; /* <-- this gets rid of the unused variable warning */ - t_int tmp_int; + char charbuf; tmp_symbol = atom_getsymbolarg(0, argc, argv); if(tmp_symbol == &s_) + charbuf = (t_int) atom_getfloatarg(0, argc , argv); + else + charbuf = tmp_symbol->s_name[0]; + if(charbuf < 10) { - tmp_int = (t_int) atom_getfloatarg(0, argc , argv); - if(tmp_int < 10) - { - sys_vgui("%s insert end %d\n", x->widget_id->s_name, tmp_int); - } - else if(tmp_int == 10) - { - sys_vgui("%s insert end {\n}\n", x->widget_id->s_name); - } - else - { - sys_vgui("%s insert end [format \"%c\" %d]\n", x->widget_id->s_name, tmp_int); - } + sys_vgui("%s insert end %d\n", x->widget_id->s_name, charbuf); } else { - sys_vgui("%s insert end %s\n", x->widget_id->s_name, tmp_symbol->s_name ); + switch(charbuf) + { + case '\\': + sys_vgui("%s insert end \"\\\\\"\n", x->widget_id->s_name); break; + case '{': + sys_vgui("%s insert end {\\{}\n", x->widget_id->s_name); break; + case '}': + sys_vgui("%s insert end \"}\"\n", x->widget_id->s_name); break; + default: + sys_vgui("%s insert end {%c}\n", x->widget_id->s_name, charbuf); + } } - sys_vgui("%s yview end-2char \n", x->widget_id->s_name ); + sys_vgui("%s yview end-2char\n", x->widget_id->s_name ); } /* Function to reset the contents of the textwidget box */ @@ -470,9 +487,12 @@ static void textwidget_query(t_textwidget *x, t_symbol *s) tkwidgets_query_options(x->receive_name, x->widget_id, sizeof(textwidget_tk_options)/sizeof(char *), textwidget_tk_options); + query_id(x); query_scrollbars(x); query_size(x); } + else if(s == id_symbol) + query_id(x); else if(s == scrollbars_symbol) query_scrollbars(x); else if(s == size_symbol) @@ -730,15 +750,17 @@ void text_setup(void) { class_setsavefn(textwidget_class, &textwidget_save); /* commonly used symbols */ - size_symbol = gensym("size"); - scrollbars_symbol = gensym("scrollbars"); backspace_symbol = gensym("backspace"); - return_symbol = gensym("return"); - space_symbol = gensym("space"); - tab_symbol = gensym("tab"); + down_symbol = gensym("down"); escape_symbol = gensym("escape"); + id_symbol = gensym("id"); left_symbol = gensym("left"); + query_callback_symbol = gensym("query_callback"); + return_symbol = gensym("return"); right_symbol = gensym("right"); + size_symbol = gensym("size"); + scrollbars_symbol = gensym("scrollbars"); + space_symbol = gensym("space"); + tab_symbol = gensym("tab"); up_symbol = gensym("up"); - down_symbol = gensym("down"); } |