Debian Codegraph @

playlist.c

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