Debian Codegraph @

scrolllist.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /*---------------------- scrolllist~ ------------------------------------------ */
2 /* */
3 /* scrolllist~ : scrolling list of text items */
4 /* constructor : scrolllist | scrolllist <capacity> <width> <height> */
5 /* */
6 /* Copyleft Yves Degoyon ( [email protected] ) */
7 /* */
8 /* This program is free software; you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation; either version 2 */
11 /* of the License, or (at your option) any later version. */
12 /* */
13 /* See file LICENSE for further informations on licensing terms. */
14 /* */
15 /* This program is distributed in the hope that it will be useful, */
16 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
17 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
18 /* GNU General Public License for more details. */
19 /* */
20 /* You should have received a copy of the GNU General Public License */
21 /* along with this program; if not, write to the Free Software */
22 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* */
24 /* Based on PureData by Miller Puckette and others. */
25 /* */
26 /* "I tried your cat's name, it tried your favorite band" */
27 /* "I have the password to your ... shell account" */
28 /* Barcelona - Shell Account */
29 /* ---------------------------------------------------------------------------- */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <ctype.h>
36 #include <regex.h>
37 #include <time.h>
38 #include <sys/time.h>
39 #include "m_pd.h"
40 #include "m_imp.h"
41 #include "g_canvas.h"
42
43
44 #ifdef _WIN32
45 #include <io.h>
46 #include <pthread.h>
47 #else
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <dirent.h>
51 #endif
52
53 t_widgetbehavior scrolllist_widgetbehavior;
54 static t_class *scrolllist_class;
55
56 static int guidebug=0;
57
58 static char *scrolllist_version = "scrolllist: scrolling list of text items : version 0.3, written by Yves Degoyon ([email protected])";
59
60 #define MIN(a,b) (a>b?b:a)
61
62 #define SYS_VGUI2(a,b) if (guidebug) \
63 post(a,b);\
64 sys_vgui(a,b)
65
66 #define SYS_VGUI3(a,b,c) if (guidebug) \
67 post(a,b,c);\
68 sys_vgui(a,b,c)
69
70 #define SYS_VGUI4(a,b,c,d) if (guidebug) \
71 post(a,b,c,d);\
72 sys_vgui(a,b,c,d)
73
74 #define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
75 post(a,b,c,d,e);\
76 sys_vgui(a,b,c,d,e)
77
78 #define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \
79 post(a,b,c,d,e,f);\
80 sys_vgui(a,b,c,d,e,f)
81
82 #define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
83 post(a,b,c,d,e,f,g);\
84 sys_vgui(a,b,c,d,e,f,g)
85
86 #define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
87 post(a,b,c,d,e,f,g,h);\
88 sys_vgui(a,b,c,d,e,f,g,h)
89
90 #define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
91 post(a,b,c,d,e,f,g,h,i );\
92 sys_vgui(a,b,c,d,e,f,g,h,i)
93
94 #define SYS_VGUI10(a,b,c,d,e,f,g,h,i,j) if (guidebug) \
95 post(a,b,c,d,e,f,g,h,i,j );\
96 sys_vgui(a,b,c,d,e,f,g,h,i,j)
97
98 #define SYS_VGUI11(a,b,c,d,e,f,g,h,i,j,k) if (guidebug) \
99 post(a,b,c,d,e,f,g,h,i,j,k );\
100 sys_vgui(a,b,c,d,e,f,g,h,i,j,k)
101
102 #define MAX_STRING_LENGTH 256
103
104 typedef struct _scrolllist
105 {
106 t_object x_obj;
107 t_glist *x_glist;
108 t_outlet *x_item; /* outlet to output current selected item */
109 t_int x_capacity; /* number of text items */
110 t_int x_height; /* height of the scrolllist */
111 t_int x_width; /* width of the scrolllist */
112 t_int x_itemselected; /* index of the selected item */
113 t_int x_selected; /* stores selected state */
114 t_int x_graphics; /* flag to draw graphics or not */
115 char **x_items; /* text items */
116 t_int x_nitems; /* number of current items */
117 t_int x_ndisplayed; /* number of displayed items */
118 t_int x_firstseen; /* first displayed item */
119 t_int x_lastseen; /* last displayed item */
120 t_int x_cdy; /* cumulated y drag */
121 char *x_font; /* font used for entries */
122 t_int x_charheight; /* height of characters */
123 t_int x_charwidth; /* width of characters */
124 char *x_bgcolor; /* background color */
125 char *x_fgcolor; /* foreground color */
126 char *x_secolor; /* selection color */
127 } t_scrolllist;
128
129 static void scrolllist_erase(t_scrolllist *x, t_glist *glist)
130 {
131 t_canvas *canvas=glist_getcanvas(glist);
132 t_int i;
133
134 // just in case we got confused
135 if ( x->x_firstseen < 0 ) x->x_firstseen=0;
136 if ( x->x_lastseen > x->x_capacity-1 ) x->x_lastseen=x->x_capacity-1;
137
138 // delete previous entries
139 for ( i=x->x_firstseen; i<=x->x_lastseen; i++ )
140 {
141 SYS_VGUI4(".x%lx.c delete %xITEM%d\n", canvas, x, i);
142 }
143 }
144
145 static void scrolllist_update(t_scrolllist *x, t_glist *glist)
146 {
147 t_canvas *canvas=glist_getcanvas(glist);
148 t_int i;
149
150 // just in case we got confused
151 if ( x->x_firstseen < 0 ) x->x_firstseen=0;
152 if ( x->x_lastseen > x->x_capacity-1 ) x->x_lastseen=x->x_capacity-1;
153
154 // display the content of text items
155 for ( i=x->x_firstseen; i<=x->x_lastseen; i++ )
156 {
157 // display the entry if displayable
158 if ( ( (i-x->x_firstseen)*x->x_charheight < x->x_height ) && ( x->x_items[i] != NULL ) )
159 {
160 SYS_VGUI11(".x%lx.c create text %d %d -fill %s -activefill %s -width %d -text \"%s\" -anchor w -font {%s} -tags %xITEM%d\n",
161 canvas,
162 text_xpix(&x->x_obj, glist)+5,
163 text_ypix(&x->x_obj, glist)+5+(i-x->x_firstseen)*x->x_charheight,
164 x->x_fgcolor,
165 x->x_secolor,
166 x->x_width,
167 x->x_items[i],
168 x->x_font,
169 x, i );
170 }
171 if ( ( x->x_itemselected >= x->x_firstseen ) && ( x->x_itemselected <= x->x_lastseen ) )
172 {
173 SYS_VGUI5(".x%lx.c itemconfigure %xITEM%d -fill %s\n",
174 canvas, x, x->x_itemselected, x->x_secolor);
175 }
176 }
177 }
178
179 static void scrolllist_output_current(t_scrolllist* x)
180 {
181 if ( x->x_items && x->x_itemselected < x->x_nitems && x->x_itemselected >= 0 )
182 {
183 outlet_symbol( x->x_item, gensym( x->x_items[x->x_itemselected] ) );
184 }
185 }
186
187 static void scrolllist_draw_new(t_scrolllist *x, t_glist *glist)
188 {
189 t_canvas *canvas=glist_getcanvas(glist);
190
191 x->x_glist = glist;
192 if ( x->x_graphics )
193 {
194 SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d -fill %s -tags %xTEXTLIST\n",
195 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
196 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height,
197 x->x_bgcolor, x);
198 }
199 else
200 {
201 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline white -fill white -tags %xTEXTLIST\n",
202 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
203 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height, x);
204 }
205 scrolllist_erase( x, glist );
206 scrolllist_update( x, glist );
207 }
208
209 static void scrolllist_draw_move(t_scrolllist *x, t_glist *glist)
210 {
211 t_canvas *canvas=glist_getcanvas(glist);
212 t_int i;
213 struct timespec tv;
214
215 tv.tv_sec = 0;
216 tv.tv_nsec = 10000000;
217
218 SYS_VGUI7(".x%lx.c coords %xTEXTLIST %d %d %d %d\n",
219 canvas, x,
220 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
221 text_xpix(&x->x_obj, glist)+x->x_width,
222 text_ypix(&x->x_obj, glist)+x->x_height);
223 for ( i=x->x_firstseen; i<=x->x_lastseen; i++ )
224 {
225 // nanosleep( &tv, NULL );
226 SYS_VGUI6(".x%lx.c coords %xITEM%d %d %d\n",
227 canvas, x, i,
228 text_xpix(&x->x_obj, glist)+5,
229 text_ypix(&x->x_obj, glist)+5+(i-x->x_firstseen)*x->x_charheight);
230 }
231
232 canvas_fixlinesfor( canvas, (t_text*)x );
233 }
234
235 static void scrolllist_draw_erase(t_scrolllist* x, t_glist* glist)
236 {
237 t_canvas *canvas=glist_getcanvas(glist);
238 t_int i;
239
240 SYS_VGUI3(".x%lx.c delete %xTEXTLIST\n", canvas, x);
241 for ( i=x->x_firstseen; i<=x->x_lastseen; i++ )
242 {
243 SYS_VGUI4(".x%lx.c delete %xITEM%d\n", canvas, x, i);
244 }
245 }
246
247 static void scrolllist_draw_select(t_scrolllist* x, t_glist* glist)
248 {
249 t_canvas *canvas=glist_getcanvas(glist);
250
251 // post( "scrolllist : select" );
252 if(x->x_selected)
253 {
254 /* sets the item in blue */
255 SYS_VGUI3(".x%lx.c itemconfigure %xTEXTLIST -outline #0000FF\n", canvas, x);
256 }
257 else
258 {
259 SYS_VGUI3(".x%lx.c itemconfigure %xTEXTLIST -outline #000000\n", canvas, x);
260 }
261 }
262
263 /* ------------------------ scrolllist widgetbehaviour----------------------------- */
264
265
266 static void scrolllist_getrect(t_gobj *z, t_glist *owner,
267 int *xp1, int *yp1, int *xp2, int *yp2)
268 {
269 t_scrolllist* x = (t_scrolllist*)z;
270
271 *xp1 = text_xpix(&x->x_obj, owner);
272 *yp1 = text_ypix(&x->x_obj, owner);
273 *xp2 = text_xpix(&x->x_obj, owner)+x->x_width;
274 *yp2 = text_ypix(&x->x_obj, owner)+x->x_height;
275 }
276
277 static void scrolllist_save(t_gobj *z, t_binbuf *b)
278 {
279 t_scrolllist *x = (t_scrolllist *)z;
280
281 // post( "saving scrolllist : %d", x->x_capacity );
282 binbuf_addv(b, "ssiisiiissss", gensym("#X"), gensym("obj"),
283 (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
284 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
285 x->x_capacity, x->x_width, x->x_height,
286 gensym(x->x_font), gensym(x->x_bgcolor),
287 gensym(x->x_fgcolor), gensym(x->x_secolor) );
288 binbuf_addv(b, ";");
289 }
290
291 static void scrolllist_select(t_gobj *z, t_glist *glist, int selected)
292 {
293 t_scrolllist *x = (t_scrolllist *)z;
294
295 x->x_selected = selected;
296
297 scrolllist_draw_select( x, glist );
298 }
299
300 static void scrolllist_vis(t_gobj *z, t_glist *glist, int vis)
301 {
302 t_scrolllist *x = (t_scrolllist *)z;
303 t_rtext *y;
304
305 // post( "scrolllist : vis (%d)", vis );
306 x->x_glist = glist;
307 if (vis)
308 {
309 scrolllist_draw_erase(x, x->x_glist);
310 scrolllist_draw_new( x, glist );
311 }
312 else
313 {
314 scrolllist_draw_erase( x, glist );
315 }
316 }
317
318 static void scrolllist_deleteobj(t_gobj *z, t_glist *glist)
319 {
320 t_scrolllist *x = (t_scrolllist *)z;
321
322 canvas_deletelinesfor(glist, (t_text *)z);
323 }
324
325 static void scrolllist_displace(t_gobj *z, t_glist *glist, int dx, int dy)
326 {
327 t_scrolllist *x = (t_scrolllist *)z;
328 t_int xold = text_xpix(&x->x_obj, glist);
329 t_int yold = text_ypix(&x->x_obj, glist);
330
331 // post( "scrolllist_displace dx=%d dy=%d", dx, dy );
332
333 x->x_obj.te_xpix += dx;
334 x->x_obj.te_ypix += dy;
335 if(xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist))
336 {
337 scrolllist_draw_move(x, glist);
338 }
339 }
340
341 static void scrolllist_motion(t_scrolllist *x, t_floatarg dx, t_floatarg dy)
342 {
343 t_int i;
344 t_canvas *canvas=glist_getcanvas(x->x_glist);
345
346 x->x_cdy+=dy;
347 scrolllist_erase( x, x->x_glist );
348
349 // check if we need to scroll
350 // eventually, move down
351 if ( x->x_cdy >= x->x_charheight )
352 {
353 if ( x->x_firstseen < x->x_nitems - x->x_ndisplayed )
354 {
355 x->x_firstseen++;
356 x->x_lastseen++;
357 // post( "scrolllist : moved down first=%d last=%d", x->x_firstseen, x->x_lastseen );
358 }
359 }
360 // eventually, move up
361 if ( x->x_cdy <= -x->x_charheight )
362 {
363 if ( x->x_firstseen-1 >= 0 )
364 {
365 x->x_firstseen--;
366 x->x_lastseen--;
367 // post( "scrolllist : moved up first=%d last=%d", x->x_firstseen, x->x_lastseen );
368 }
369 }
370 scrolllist_update(x, x->x_glist);
371 if ( ( x->x_cdy >= x->x_charheight ) || ( x->x_cdy <= -x->x_charheight ) ) x->x_cdy = 0;
372 }
373
374 static void scrolllist_scroll(t_scrolllist *x, t_floatarg fdy)
375 {
376 t_int nbsteps, si;
377
378 x->x_cdy += (t_int)fdy;
379 nbsteps = (t_int)abs(x->x_cdy/x->x_charheight);
380 // post( "scrolllist : iterations %d", nbsteps );
381 for (si=0; si<nbsteps; si++ )
382 {
383 scrolllist_motion(x, 0, (fdy/abs(fdy))*x->x_charheight);
384 }
385 }
386
387 static void scrolllist_graphics(t_scrolllist *x, t_floatarg fgraphics)
388 {
389 if ( ( (t_int)fgraphics == 0 ) || ( (t_int)fgraphics == 1 ) )
390 {
391 scrolllist_draw_erase(x, x->x_glist);
392 x->x_graphics = (t_int) fgraphics;
393 scrolllist_draw_new(x, x->x_glist);
394 }
395 }
396
397 static int scrolllist_click(t_gobj *z, struct _glist *glist,
398 int xpix, int ypix, int shift, int alt, int dbl, int doit)
399 {
400 t_scrolllist* x = (t_scrolllist *)z;
401 t_canvas *canvas=glist_getcanvas(x->x_glist);
402 t_int xoffset;
403
404 if (doit)
405 {
406 // deselect previously selected item
407 SYS_VGUI5(".x%lx.c itemconfigure %xITEM%d -fill %s\n",
408 canvas, x, x->x_itemselected, x->x_fgcolor);
409 x->x_itemselected = x->x_firstseen + (ypix-text_ypix(&x->x_obj, glist))/x->x_charheight;
410 SYS_VGUI5(".x%lx.c itemconfigure %xITEM%d -fill %s\n",
411 canvas, x, x->x_itemselected, x->x_secolor);
412 // post( "scrolllist : selected item : %d", x->x_itemselected );
413 if ( x->x_items && ( x->x_itemselected < x->x_nitems ) )
414 {
415 xoffset=(xpix-text_xpix(&x->x_obj, glist));
416 if ( xoffset <= (t_int)( x->x_width*4/5 ) )
417 {
418 scrolllist_output_current(x);
419 scrolllist_erase( x, x->x_glist );
420 scrolllist_update( x, glist );
421 }
422 else
423 {
424 x->x_itemselected=-1;
425 }
426 }
427 x->x_glist = glist;
428 glist_grab( glist, &x->x_obj.te_g, (t_glistmotionfn)scrolllist_motion,
429 NULL, xpix, ypix );
430 }
431 return (1);
432 }
433
434 static void scrolllist_properties(t_gobj *z, t_glist *owner)
435 {
436 char buf[800];
437 t_scrolllist *x=(t_scrolllist *)z;
438
439 sprintf(buf, "pdtk_scrolllist_dialog %%s %d %d %d %s %s %s %s\n",
440 (int)x->x_capacity, (int)x->x_width, (int)x->x_height,
441 x->x_font, x->x_bgcolor,
442 x->x_fgcolor, x->x_secolor );
443 // post("scrolllist_properties : %s", buf );
444 gfxstub_new(&x->x_obj.ob_pd, x, buf);
445 }
446
447 static void scrolllist_dialog(t_scrolllist *x, t_symbol *s, int argc, t_atom *argv)
448 {
449 char **titems;
450 t_int ncapacity, i, ccapacity;
451
452 scrolllist_erase( x, x->x_glist );
453 scrolllist_draw_erase(x, x->x_glist);
454
455 if ( !x )
456 {
457 post( "scrolllist : error :tried to set properties on an unexisting object" );
458 }
459 if ( argc != 9 )
460 {
461 post( "scrolllist : error in the number of arguments ( %d instead of 10 )", argc );
462 return;
463 }
464 if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT ||
465 argv[2].a_type != A_FLOAT || argv[3].a_type != A_SYMBOL ||
466 argv[4].a_type != A_FLOAT || argv[5].a_type != A_SYMBOL ||
467 argv[6].a_type != A_SYMBOL || argv[7].a_type != A_SYMBOL ||
468 argv[8].a_type != A_SYMBOL )
469 {
470 post( "scrolllist : wrong arguments" );
471 return;
472 }
473
474 ncapacity = (t_int)argv[0].a_w.w_float;
475 titems = (char**) malloc( ncapacity*sizeof(char*) );
476
477 if ( ncapacity < x->x_nitems )
478 {
479 post( "scrolllist : new size is too small : texts lost !!" );
480 ccapacity = ncapacity;
481 }
482 else
483 {
484 ccapacity = x->x_nitems;
485 }
486 for ( i=0; i<ccapacity; i++ )
487 {
488 if ( x->x_items[i] != NULL )
489 {
490 titems[i] = (char*) malloc( strlen( x->x_items[i] ) + 1 );
491 memcpy( titems[i], x->x_items[i], strlen( x->x_items[i] ) );
492 titems[i][strlen( x->x_items[i] )]='\0';
493 free( x->x_items[i] );
494 x->x_items[i] = NULL;
495 }
496 }
497 if ( x->x_items )
498 {
499 free( x->x_items );
500 x->x_items = NULL;
501 }
502 x->x_items = titems;
503 x->x_nitems = ccapacity;
504 x->x_capacity = ncapacity;
505
506 x->x_width = (int)argv[1].a_w.w_float;
507 x->x_height = (int)argv[2].a_w.w_float;
508 sprintf( x->x_font, "{%s %d %s}", argv[3].a_w.w_symbol->s_name,
509 (int)argv[4].a_w.w_float, argv[5].a_w.w_symbol->s_name );
510 x->x_charheight = (t_int)argv[4].a_w.w_float;
511 strcpy( x->x_bgcolor, argv[6].a_w.w_symbol->s_name );
512 strcpy( x->x_fgcolor, argv[7].a_w.w_symbol->s_name );
513 strcpy( x->x_secolor, argv[8].a_w.w_symbol->s_name );
514
515 x->x_firstseen = 0;
516 x->x_ndisplayed = (t_int)(x->x_height/x->x_charheight );
517 if ( x->x_nitems >= x->x_ndisplayed )
518 {
519 x->x_lastseen = x->x_ndisplayed-1;
520 }
521 else
522 {
523 x->x_lastseen = x->x_nitems-1;
524 }
525
526 scrolllist_draw_new(x, x->x_glist);
527 scrolllist_update(x, x->x_glist);
528 }
529
530 static void scrolllist_bgcolor(t_scrolllist *x, t_symbol *s)
531 {
532 scrolllist_erase( x, x->x_glist );
533 scrolllist_draw_erase(x, x->x_glist);
534
535 strcpy( x->x_bgcolor, s->s_name );
536
537 scrolllist_draw_new(x, x->x_glist);
538 scrolllist_update(x, x->x_glist);
539 }
540
541 static void scrolllist_fgcolor(t_scrolllist *x, t_symbol *s)
542 {
543 scrolllist_erase( x, x->x_glist );
544 scrolllist_draw_erase(x, x->x_glist);
545
546 strcpy( x->x_fgcolor, s->s_name );
547
548 scrolllist_draw_new(x, x->x_glist);
549 scrolllist_update(x, x->x_glist);
550 }
551
552 static void scrolllist_secolor(t_scrolllist *x, t_symbol *s)
553 {
554 scrolllist_erase( x, x->x_glist );
555 scrolllist_draw_erase(x, x->x_glist);
556
557 strcpy( x->x_secolor, s->s_name );
558
559 scrolllist_draw_new(x, x->x_glist);
560 scrolllist_update(x, x->x_glist);
561 }
562
563 static void scrolllist_sort(t_scrolllist* x)
564 {
565 char **titems;
566 t_int i, j, k, irank, indest;
567
568 scrolllist_erase( x, x->x_glist );
569
570 // sort text items
571 titems = (char**) malloc( x->x_capacity*sizeof(char*) );
572 for ( i=0; i<x->x_capacity; i++ )
573 {
574 titems[i]=NULL;
575 }
576 indest=0;
577 for ( i=0; i<x->x_nitems; i++ )
578 {
579 if ( x->x_items[i] != NULL )
580 {
581 irank=0;
582 for ( j=0; j<x->x_capacity; j++ )
583 {
584 if ( titems[j] != NULL )
585 {
586 // post( "scrollist : comparing >%s< to >%s<", titems[j], x->x_items[i] );
587 if ( strcasecmp( titems[j], x->x_items[i] ) > 0 )
588 {
589 irank=j;
590 break;
591 }
592 irank=j+1;
593 }
594 }
595 // insert in irank
596 // post( "scrollist : inserting %s at %d", x->x_items[i], irank );
597 for ( k=indest-1; k>=irank; k-- )
598 {
599 if ( (k+1) < x->x_capacity )
600 {
601 if ( titems[k+1] != NULL )
602 {
603 free( titems[k+1] );
604 titems[k+1] = NULL;
605 }
606 titems[k+1] = (char*) malloc( strlen( titems[k] ) + 1 );
607 memcpy( titems[k+1], titems[k], strlen( titems[k] ) );
608 titems[k+1][strlen( titems[k] )]='\0';
609 // post( "scrollist : copying %d to %d", k, k+1 );
610 }
611 }
612 titems[irank] = (char*) malloc( strlen( x->x_items[i] ) + 1 );
613 memcpy( titems[irank], x->x_items[i], strlen( x->x_items[i] ) );
614 titems[irank][strlen( x->x_items[i] )]='\0';
615 free( x->x_items[i] );
616 x->x_items[i]=NULL;
617 indest++;
618 }
619 }
620 if ( x->x_items )
621 {
622 free( x->x_items );
623 x->x_items = NULL;
624 }
625 x->x_items = titems;
626
627 scrolllist_update( x, x->x_glist );
628 }
629
630 static void scrolllist_font(t_scrolllist* x, t_symbol *fname, t_symbol *fcase, t_floatarg fsize)
631 {
632 if ( (t_int)fsize <= 4 )
633 {
634 post( "scrolllist : wrong font size in font message : %d", (t_int)fsize );
635 return;
636 }
637 sprintf( x->x_font, "{%s %d %s}", fname->s_name, (int)fsize, fcase->s_name );
638 x->x_charheight = (t_int)fsize;
639 x->x_charwidth = (2*x->x_charheight)/3;
640 // post( "scrolllist : setting font to : %s", x->x_font );
641 scrolllist_erase( x, x->x_glist );
642 scrolllist_update( x, x->x_glist );
643 }
644
645 static void scrolllist_add(t_scrolllist* x, t_symbol *fnewtext)
646 {
647 t_int i;
648
649 // post( "scrollist : add : nitems = %d", x->x_nitems );
650 if ( x->x_nitems >= x->x_capacity )
651 {
652 // post( "scrolllist : warning : list is full, erasing first line" );
653 for ( i=0; i<(x->x_nitems-1); i++ )
654 {
655 if ( x->x_items[i] != NULL )
656 {
657 free( x->x_items[i] );
658 x->x_items[i] = NULL;
659 }
660 x->x_items[i] = (char*) malloc( strlen( x->x_items[i+1] ) + 1 );
661 memcpy( x->x_items[i], x->x_items[i+1], strlen( x->x_items[i+1] ) );
662 x->x_items[i][strlen( x->x_items[i+1] )]='\0';
663 // post( "scrollist : copying %d to %d", i+1, i );
664 }
665 x->x_items[x->x_nitems-1] = (char*) malloc( strlen( fnewtext->s_name ) + 1 );
666 memcpy( x->x_items[x->x_nitems-1], fnewtext->s_name, strlen( fnewtext->s_name ) );
667 x->x_items[x->x_nitems-1][strlen( fnewtext->s_name )]='\0';
668 }
669 else
670 {
671 // post( "scrolllist : item #%d : %x", x->x_nitems, &x->x_items[x->x_nitems] );
672 // post( "scrolllist : allocating : %d", strlen( fnewtext->s_name ) + 1 );
673 x->x_items[x->x_nitems] = (char*) malloc( strlen( fnewtext->s_name ) + 1 );
674 memcpy( x->x_items[x->x_nitems], fnewtext->s_name, strlen( fnewtext->s_name ) );
675 x->x_items[x->x_nitems][strlen( fnewtext->s_name )]='\0';
676 x->x_nitems++;
677 if ( (x->x_nitems-x->x_firstseen)*x->x_charheight+5 < x->x_height )
678 {
679 x->x_lastseen = x->x_nitems-1;
680 }
681 }
682 scrolllist_erase( x, x->x_glist );
683 scrolllist_update( x, x->x_glist );
684 }
685
686 static void scrolllist_insert(t_scrolllist* x, t_symbol *ftext, t_floatarg frank)
687 {
688 t_int rank, i;
689
690 if ( (t_int)frank > x->x_capacity )
691 {
692 post( "scrolllist : error : incorrect rank in insert message (%d), over capacity", (t_int)frank );
693 return;
694 }
695 rank = (t_int)frank-1;
696 if ( rank < 0 )
697 {
698 rank=0;
699 }
700 if ( rank > x->x_nitems-1 )
701 {
702 rank=x->x_nitems-1;
703 }
704 for ( i=x->x_nitems-1; i>=rank; i-- )
705 {
706 if ( (i+1) < x->x_capacity )
707 {
708 if ( x->x_items[i+1] != NULL )
709 {
710 free( x->x_items[i+1] );
711 x->x_items[i+1] = NULL;
712 }
713 x->x_items[i+1] = (char*) malloc( strlen( x->x_items[i] ) + 1 );
714 memcpy( x->x_items[i+1], x->x_items[i], strlen( x->x_items[i] ) );
715 x->x_items[i+1][strlen( x->x_items[i] )]='\0';
716 // post( "scrollist : copying %d to %d", i, i+1 );
717 }
718 }
719 // post( "scrollist : inserting at %d", rank );
720 x->x_items[rank] = (char*) malloc( strlen( ftext->s_name ) + 1 );
721 memcpy( x->x_items[rank], ftext->s_name, strlen( ftext->s_name ) );
722 x->x_items[rank][strlen( ftext->s_name )]='\0';
723 if ( x->x_nitems < x->x_capacity ) x->x_nitems++;
724 if ( (x->x_nitems-x->x_firstseen)*x->x_charheight+5 < x->x_height )
725 {
726 x->x_lastseen = x->x_nitems-1;
727 }
728 scrolllist_erase( x, x->x_glist );
729 scrolllist_update( x, x->x_glist );
730 }
731
732 static void scrolllist_replace(t_scrolllist* x, t_symbol *ftext, t_floatarg frank)
733 {
734 t_int rank;
735
736 if ( ( (t_int)frank <= 0 ) || ( (t_int)frank > x->x_nitems ) )
737 {
738 post( "scrolllist : error : incorrect rank in replace message (%d), no such text", (t_int)frank );
739 return;
740 }
741
742 rank = (t_int) frank-1;
743 if ( x->x_items[rank] != NULL )
744 {
745 free( x->x_items[rank] );
746 x->x_items[rank] = NULL;
747 }
748 x->x_items[rank] = (char*) malloc( strlen( ftext->s_name ) + 1 );
749 memcpy( x->x_items[rank], ftext->s_name, strlen( ftext->s_name ) );
750 x->x_items[rank][strlen( ftext->s_name )]='\0';
751 scrolllist_erase( x, x->x_glist );
752 scrolllist_update( x, x->x_glist );
753 }
754
755 static void scrolllist_delete(t_scrolllist* x, t_floatarg frank)
756 {
757 t_int rank, i;
758
759 if ( ( (t_int)frank <= 0 ) || ( (t_int)frank > x->x_nitems ) )
760 {
761 post( "scrolllist : error : incorrect rank in delete message (%d), no such text (%d)", (t_int)frank, x->x_nitems );
762 return;
763 }
764
765 rank = (t_int) frank-1;
766 for ( i=rank; i<x->x_nitems-1; i++ )
767 {
768 if ( x->x_items[i] != NULL )
769 {
770 free( x->x_items[i] );
771 x->x_items[i] = NULL;
772 }
773 x->x_items[i] = (char*) malloc( strlen( x->x_items[i+1] ) + 1 );
774 memcpy( x->x_items[i], x->x_items[i+1], strlen( x->x_items[i+1] ) );
775 x->x_items[i][strlen( x->x_items[i+1] )]='\0';
776 // post( "scrollist : copying %d to %d", i+1, i );
777 }
778 free( x->x_items[x->x_nitems-1] );
779 x->x_items[x->x_nitems-1] = NULL;
780 if ( x->x_lastseen == x->x_nitems-2 ) x->x_lastseen--;
781 x->x_nitems--;
782 scrolllist_erase( x, x->x_glist );
783 scrolllist_update( x, x->x_glist );
784 }
785
786 static void scrolllist_clear(t_scrolllist* x)
787 {
788 t_int i;
789
790 scrolllist_erase( x, x->x_glist );
791 for ( i=0; i<x->x_capacity; i++ )
792 {
793 x->x_items[i]=NULL;
794 }
795 x->x_nitems = 0;
796 x->x_selected = 0;
797 x->x_itemselected = -1;
798 x->x_firstseen = 0;
799 x->x_lastseen = -1;
800 scrolllist_update( x, x->x_glist );
801 }
802
803 static void scrolllist_seek(t_scrolllist *x, t_floatarg fseeked)
804 {
805 t_int iout=0;
806 t_canvas *canvas=glist_getcanvas(x->x_glist);
807
808 if ( fseeked < 0 )
809 {
810 post( "scrolllist : wrong searched file : %f", fseeked );
811 return;
812 }
813 if ( x->x_nitems == 0 ) return;
814
815 iout = (t_int)fseeked % (x->x_nitems);
816 SYS_VGUI5(".x%lx.c itemconfigure %xITEM%d -fill %s\n", canvas, x, x->x_itemselected, x->x_fgcolor);
817 x->x_itemselected = iout;
818 SYS_VGUI5(".x%lx.c itemconfigure %xITEM%d -fill %s\n", canvas, x, x->x_itemselected, x->x_secolor);
819 scrolllist_output_current(x);
820 }
821
822 static t_scrolllist *scrolllist_new(t_symbol *s, int argc, t_atom *argv )
823 {
824 t_int i, argoffset=0;
825 t_scrolllist *x;
826
827 x = (t_scrolllist *)pd_new(scrolllist_class);
828
829 x->x_capacity = 100;
830 x->x_width = 400;
831 x->x_height = 200;
832 x->x_font = ( char * ) malloc( MAX_STRING_LENGTH );
833 sprintf( x->x_font, "{Helvetica 10 bold}" );
834 x->x_charheight = 10;
835 x->x_charwidth = (2*10)/3;
836 x->x_bgcolor = ( char * ) malloc( MAX_STRING_LENGTH );
837 sprintf( x->x_bgcolor, "#457782" );
838 x->x_fgcolor = ( char * ) malloc( MAX_STRING_LENGTH );
839 sprintf( x->x_fgcolor, "black" );
840 x->x_secolor = ( char * ) malloc( MAX_STRING_LENGTH );
841 sprintf( x->x_secolor, "red" );
842
843 if ( argc >= 1 )
844 {
845 if ( argv[0].a_type != A_FLOAT )
846 {
•847 pd_error(x, "scrolllist : wrong argument (capacity : 1)" );
848 return NULL;
849 }
850 x->x_capacity = (int)argv[0].a_w.w_float;
851 }
852 if ( argc >= 2 )
853 {
854 if ( argv[1].a_type != A_FLOAT )
855 {
•856 pd_error(x, "scrolllist : wrong argument (width : 2)" );
857 return NULL;
858 }
859 if ( (int)argv[1].a_w.w_float <= 0 )
860 {
•861 pd_error(x, "scrolllist : wrong width (%d)", (t_int)(int)argv[1].a_w.w_float );
862 pd_error(x, "scrolllist : usage : scrolllist <capacity> <width> <height>" );
863 return NULL;
864 }
865 x->x_width = (int)argv[1].a_w.w_float;
866 }
867 if ( argc >= 3 )
868 {
869 if ( argv[2].a_type != A_FLOAT )
870 {
•871 pd_error(x, "scrolllist : wrong argument (height : 3)" );
872 return NULL;
873 }
874 if ( (int)argv[2].a_w.w_float <= 0 )
875 {
•876 pd_error(x, "scrolllist : wrong height (%d)", (t_int)(int)argv[2].a_w.w_float );
877 pd_error(x, "scrolllist : usage : scrolllist <capacity> <width> <height>" );
878 return NULL;
879 }
880 x->x_height = (int)argv[2].a_w.w_float;
881 }
882 if ( argc >= 6 )
883 {
884 if ( argv[3].a_type != A_SYMBOL ||
885 argv[5].a_type != A_SYMBOL )
886 {
•887 pd_error(x, "scrolllist : wrong arguments (font : 4,6)" );
888 pd_error(x, "argument types : %d %d", argv[3].a_type, argv[5].a_type );
889 return NULL;
890 }
891 if ( argv[4].a_type != A_SYMBOL &&
892 argv[4].a_type != A_FLOAT )
893 {
•894 pd_error(x, "scrolllist : wrong arguments (font size : 5)" );
895 pd_error(x, "argument types : %d", argv[4].a_type );
896 return NULL;
897 }
898 if ( argv[4].a_type == A_SYMBOL )
899 {
900 sprintf( x->x_font, "%s", argv[3].a_w.w_symbol->s_name );
901 x->x_charheight = (t_int)atoi( strstr( argv[3].a_w.w_symbol->s_name, " ") );
902 argoffset=2;
903 }
904 if ( argv[4].a_type == A_FLOAT )
905 {
906 x->x_charheight = (t_int)argv[4].a_w.w_float;
907 sprintf( x->x_font, "%s %d %s", argv[3].a_w.w_symbol->s_name,
908 (int)x->x_charheight, argv[5].a_w.w_symbol->s_name );
909 argoffset=0;
910 }
911 post( "scrolllist : font : %s, size : %d", x->x_font, x->x_charheight );
912 }
913 if ( argc >= 7-argoffset )
914 {
915 if ( argv[6-argoffset].a_type != A_SYMBOL )
916 {
•917 pd_error(x, "scrolllist : wrong arguments (background color : %d)", 7-argoffset );
918 return NULL;
919 }
920 strcpy( x->x_bgcolor, argv[6-argoffset].a_w.w_symbol->s_name );
921 }
922 if ( argc >= 8-argoffset )
923 {
924 if ( argv[7-argoffset].a_type != A_SYMBOL )
925 {
•926 pd_error(x, "scrolllist : wrong arguments (foreground color : %d)", 8-argoffset );
927 return NULL;
928 }
929 strcpy( x->x_fgcolor, argv[7-argoffset].a_w.w_symbol->s_name );
930 }
931 if ( argc >= 9-argoffset )
932 {
933 if ( argv[8-argoffset].a_type != A_SYMBOL )
934 {
•935 pd_error(x, "scrolllist : wrong arguments (selection color : %d)", 9-argoffset );
936 return NULL;
937 }
938 strcpy( x->x_secolor, argv[8-argoffset].a_w.w_symbol->s_name );
939 }
940
941 x->x_item = outlet_new(&x->x_obj, &s_symbol );
942
943 x->x_glist = (t_glist *) canvas_getcurrent();
944 x->x_nitems = 0;
945 x->x_items = (char **) malloc( x->x_capacity*sizeof(char*) );
946 for ( i=0; i<x->x_capacity; i++ )
947 {
948 x->x_items[i]=NULL;
949 }
950
951 x->x_selected = 0;
952 x->x_itemselected = -1;
953 x->x_firstseen = 0;
954 x->x_lastseen = -1;
955 x->x_ndisplayed = (t_int)(x->x_height/x->x_charheight );
956
957 x->x_graphics = 1;
958
959 post( "scrolllist : capacity=%d width=%d height=%d", x->x_capacity, x->x_width, x->x_height );
960
961 return (x);
962 }
963
964 static void scrolllist_free(t_scrolllist *x)
965 {
966 t_int i;
967
968 // post( "scrolllist : scrolllist_free" );
969
970 // free text items list
971 if ( x->x_nitems )
972 {
973 for ( i=0; i<x->x_nitems; i++ )
974 {
975 if ( x->x_items[i] != NULL )
976 {
977 // post( "scrolllist : freeing entry %d size=%d : %s", i, strlen( x->x_items[i] ) + 1, x->x_items[i] );
978 free( x->x_items[i] );
979 }
980 }
981 }
982 free( x->x_items );
983 if ( x->x_font )
984 {
985 free( x->x_font );
986 }
987 if ( x->x_bgcolor )
988 {
989 free( x->x_bgcolor );
990 }
991 if ( x->x_fgcolor )
992 {
993 free( x->x_fgcolor );
994 }
995 if ( x->x_secolor )
996 {
997 free( x->x_secolor );
998 }
999 }
1000
1001 void scrolllist_setup(void)
1002 {
•1003 logpost(NULL, 4, "%s", scrolllist_version );
1004 scrolllist_class = class_new(gensym("scrolllist"), (t_newmethod)scrolllist_new,
1005 (t_method)scrolllist_free, sizeof(t_scrolllist),
1006 CLASS_DEFAULT, A_GIMME, 0);
1007 class_addmethod(scrolllist_class, (t_method)scrolllist_seek, gensym("seek"), A_DEFFLOAT, A_NULL );
1008 class_addmethod(scrolllist_class, (t_method)scrolllist_dialog, gensym("dialog"), A_GIMME, A_NULL );
1009 class_addmethod(scrolllist_class, (t_method)scrolllist_sort, gensym("sort"), A_NULL );
1010 class_addmethod(scrolllist_class, (t_method)scrolllist_graphics, gensym("graphics"), A_DEFFLOAT, A_NULL );
1011 class_addmethod(scrolllist_class, (t_method)scrolllist_scroll, gensym("scroll"), A_DEFFLOAT, A_NULL );
1012 class_addmethod(scrolllist_class, (t_method)scrolllist_font, gensym("font"), A_SYMBOL, A_SYMBOL, A_DEFFLOAT, A_NULL );
1013 class_addmethod(scrolllist_class, (t_method)scrolllist_insert, gensym("insert"), A_SYMBOL, A_DEFFLOAT, A_NULL );
1014 class_addmethod(scrolllist_class, (t_method)scrolllist_replace, gensym("replace"), A_SYMBOL, A_DEFFLOAT, A_NULL );
1015 class_addmethod(scrolllist_class, (t_method)scrolllist_add, gensym("add"), A_SYMBOL, A_NULL );
1016 class_addmethod(scrolllist_class, (t_method)scrolllist_bgcolor, gensym("bgcolor"), A_SYMBOL, A_NULL );
1017 class_addmethod(scrolllist_class, (t_method)scrolllist_fgcolor, gensym("fgcolor"), A_SYMBOL, A_NULL );
1018 class_addmethod(scrolllist_class, (t_method)scrolllist_secolor, gensym("secolor"), A_SYMBOL, A_NULL );
1019 class_addmethod(scrolllist_class, (t_method)scrolllist_delete, gensym("delete"), A_DEFFLOAT, A_NULL );
1020 class_addmethod(scrolllist_class, (t_method)scrolllist_clear, gensym("clear"), A_NULL );
1021
1022 scrolllist_widgetbehavior.w_getrectfn = scrolllist_getrect;
1023 scrolllist_widgetbehavior.w_displacefn = scrolllist_displace;
1024 scrolllist_widgetbehavior.w_selectfn = scrolllist_select;
1025 scrolllist_widgetbehavior.w_activatefn = NULL;
1026 scrolllist_widgetbehavior.w_deletefn = scrolllist_deleteobj;
1027 scrolllist_widgetbehavior.w_visfn = scrolllist_vis;
1028 scrolllist_widgetbehavior.w_clickfn = scrolllist_click;
1029
1030 #if PD_MINOR_VERSION >= 37
1031 class_setpropertiesfn(scrolllist_class, scrolllist_properties);
1032 class_setsavefn(scrolllist_class, scrolllist_save);
1033 #else
1034 scrolllist_widgetbehavior.w_propertiesfn = scrolllist_properties;
1035 scrolllist_widgetbehavior.w_savefn = scrolllist_save;
1036 #endif
1037
1038 class_setwidget(scrolllist_class, &scrolllist_widgetbehavior);
1039
1040 sys_vgui("eval [read [open {%s/%s.tcl}]]\n",
1041 scrolllist_class->c_externdir->s_name,
1042 scrolllist_class->c_name->s_name);
1043 }