blob: 98c5ff1ddd5b613efafbdd3caa3b70e1066ff7e9 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
2// "$Id: Fl_File_Browser.cxx 8063 2010-12-19 21:20:10Z matt $"
3//
4// Fl_File_Browser routines.
5//
6// Copyright 1999-2010 by Michael Sweet.
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Library General Public
10// License as published by the Free Software Foundation; either
11// version 2 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Library General Public License for more details.
17//
18// You should have received a copy of the GNU Library General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21// USA.
22//
23// Please report all bugs and problems on the following page:
24//
25// http://www.fltk.org/str.php
26//
27// Contents:
28//
29// Fl_File_Browser::full_height() - Return the height of the list.
30// Fl_File_Browser::item_height() - Return the height of a list item.
31// Fl_File_Browser::item_width() - Return the width of a list item.
32// Fl_File_Browser::item_draw() - Draw a list item.
33// Fl_File_Browser::Fl_File_Browser() - Create a Fl_File_Browser widget.
34// Fl_File_Browser::load() - Load a directory into the browser.
35// Fl_File_Browser::filter() - Set the filename filter.
36//
37
38//
39// Include necessary header files...
40//
41
42#include <FL/Fl_File_Browser.H>
43#include <FL/fl_draw.H>
44#include <FL/filename.H>
45#include <FL/Fl_Image.H> // icon
46#include <stdio.h>
47#include <stdlib.h>
48#include "flstring.h"
49
50#ifdef __CYGWIN__
51# include <mntent.h>
52#elif defined(WIN32)
53# include <windows.h>
54# include <direct.h>
55// Apparently Borland C++ defines DIRECTORY in <direct.h>, which
56// interfers with the Fl_File_Icon enumeration of the same name.
57# ifdef DIRECTORY
58# undef DIRECTORY
59# endif // DIRECTORY
60#endif // __CYGWIN__
61
62#ifdef __EMX__
63# define INCL_DOS
64# define INCL_DOSMISC
65# include <os2.h>
66#endif // __EMX__
67
68#if defined(__APPLE__)
69# include <sys/param.h>
70# include <sys/ucred.h>
71# include <sys/mount.h>
72#endif // __APPLE__
73
74//
75// FL_BLINE definition from "Fl_Browser.cxx"...
76//
77
78#define SELECTED 1
79#define NOTDISPLAYED 2
80
81// TODO -- Warning: The definition of FL_BLINE here is a hack.
82// Fl_File_Browser should not do this. PLEASE FIX.
83// FL_BLINE should be private to Fl_Browser, and not re-defined here.
84// For now, make sure this struct is precisely consistent with Fl_Browser.cxx.
85//
86struct FL_BLINE // data is in a linked list of these
87{
88 FL_BLINE *prev; // Previous item in list
89 FL_BLINE *next; // Next item in list
90 void *data; // Pointer to data (function)
91 Fl_Image *icon; // Pointer to optional icon
92 short length; // sizeof(txt)-1, may be longer than string
93 char flags; // selected, displayed
94 char txt[1]; // start of allocated array
95};
96
97
98//
99// 'Fl_File_Browser::full_height()' - Return the height of the list.
100//
101
102int // O - Height in pixels
103Fl_File_Browser::full_height() const
104{
105 int i, // Looping var
106 th; // Total height of list.
107
108
109 for (i = 0, th = 0; i < size(); i ++)
110 th += item_height(find_line(i));
111
112 return (th);
113}
114
115
116//
117// 'Fl_File_Browser::item_height()' - Return the height of a list item.
118//
119
120int // O - Height in pixels
121Fl_File_Browser::item_height(void *p) const // I - List item data
122{
123 FL_BLINE *line; // Pointer to line
124 char *t; // Pointer into text
125 int height; // Width of line
126 int textheight; // Height of text
127
128
129 // Figure out the standard text height...
130 fl_font(textfont(), textsize());
131 textheight = fl_height();
132
133 // We always have at least 1 line...
134 height = textheight;
135
136 // Scan for newlines...
137 line = (FL_BLINE *)p;
138
139 if (line != NULL)
140 for (t = line->txt; *t != '\0'; t ++)
141 if (*t == '\n')
142 height += textheight;
143
144 // If we have enabled icons then add space for them...
145 if (Fl_File_Icon::first() != NULL && height < iconsize_)
146 height = iconsize_;
147
148 // Add space for the selection border..
149 height += 2;
150
151 // Return the height
152 return (height);
153}
154
155
156//
157// 'Fl_File_Browser::item_width()' - Return the width of a list item.
158//
159
160int // O - Width in pixels
161Fl_File_Browser::item_width(void *p) const // I - List item data
162{
163 int i; // Looping var
164 FL_BLINE *line; // Pointer to line
165 char *t, // Pointer into text
166 *ptr, // Pointer into fragment
167 fragment[10240]; // Fragment of text
168 int width, // Width of line
169 tempwidth; // Width of fragment
170 int column; // Current column
171 const int *columns; // Columns
172
173
174 // Scan for newlines...
175 line = (FL_BLINE *)p;
176 columns = column_widths();
177
178 // Set the font and size...
179 if (line->txt[strlen(line->txt) - 1] == '/')
180 fl_font(textfont() | FL_BOLD, textsize());
181 else
182 fl_font(textfont(), textsize());
183
184 if (strchr(line->txt, '\n') == NULL &&
185 strchr(line->txt, column_char()) == NULL)
186 {
187 // Do a fast width calculation...
188 width = (int)fl_width(line->txt);
189 }
190 else
191 {
192 // More than 1 line or have columns; find the maximum width...
193 width = 0;
194 tempwidth = 0;
195 column = 0;
196
197 for (t = line->txt, ptr = fragment; *t != '\0'; t ++)
198 if (*t == '\n')
199 {
200 // Newline - nul terminate this fragment and get the width...
201 *ptr = '\0';
202
203 tempwidth += (int)fl_width(fragment);
204
205 // Update the max width as needed...
206 if (tempwidth > width)
207 width = tempwidth;
208
209 // Point back to the start of the fragment...
210 ptr = fragment;
211 tempwidth = 0;
212 column = 0;
213 }
214 else if (*t == column_char())
215 {
216 // Advance to the next column...
217 column ++;
218 if (columns)
219 {
220 for (i = 0, tempwidth = 0; i < column && columns[i]; i ++)
221 tempwidth += columns[i];
222 }
223 else
224 tempwidth = column * (int)(fl_height() * 0.6 * 8.0);
225
226 if (tempwidth > width)
227 width = tempwidth;
228
229 ptr = fragment;
230 }
231 else
232 *ptr++ = *t;
233
234 if (ptr > fragment)
235 {
236 // Nul terminate this fragment and get the width...
237 *ptr = '\0';
238
239 tempwidth += (int)fl_width(fragment);
240
241 // Update the max width as needed...
242 if (tempwidth > width)
243 width = tempwidth;
244 }
245 }
246
247 // If we have enabled icons then add space for them...
248 if (Fl_File_Icon::first() != NULL)
249 width += iconsize_ + 8;
250
251 // Add space for the selection border..
252 width += 2;
253
254 // Return the width
255 return (width);
256}
257
258
259//
260// 'Fl_File_Browser::item_draw()' - Draw a list item.
261//
262
263void
264Fl_File_Browser::item_draw(void *p, // I - List item data
265 int X, // I - Upper-lefthand X coordinate
266 int Y, // I - Upper-lefthand Y coordinate
267 int W, // I - Width of item
268 int) const // I - Height of item
269{
270 int i; // Looping var
271 FL_BLINE *line; // Pointer to line
272 Fl_Color c; // Text color
273 char *t, // Pointer into text
274 *ptr, // Pointer into fragment
275 fragment[10240]; // Fragment of text
276 int width, // Width of line
277 height; // Height of line
278 int column; // Current column
279 const int *columns; // Columns
280
281
282 // Draw the list item text...
283 line = (FL_BLINE *)p;
284
285 if (line->txt[strlen(line->txt) - 1] == '/')
286 fl_font(textfont() | FL_BOLD, textsize());
287 else
288 fl_font(textfont(), textsize());
289
290 if (line->flags & SELECTED)
291 c = fl_contrast(textcolor(), selection_color());
292 else
293 c = textcolor();
294
295 if (Fl_File_Icon::first() == NULL)
296 {
297 // No icons, just draw the text...
298 X ++;
299 W -= 2;
300 }
301 else
302 {
303 // Draw the icon if it is set...
304 if (line->data)
305 ((Fl_File_Icon *)line->data)->draw(X, Y, iconsize_, iconsize_,
306 (line->flags & SELECTED) ? FL_YELLOW :
307 FL_LIGHT2,
308 active_r());
309
310 // Draw the text offset to the right...
311 X += iconsize_ + 9;
312 W -= iconsize_ - 10;
313
314 // Center the text vertically...
315 height = fl_height();
316
317 for (t = line->txt; *t != '\0'; t ++)
318 if (*t == '\n')
319 height += fl_height();
320
321 if (height < iconsize_)
322 Y += (iconsize_ - height) / 2;
323 }
324
325 // Draw the text...
326 line = (FL_BLINE *)p;
327 columns = column_widths();
328 width = 0;
329 column = 0;
330
331 if (active_r())
332 fl_color(c);
333 else
334 fl_color(fl_inactive(c));
335
336 for (t = line->txt, ptr = fragment; *t != '\0'; t ++)
337 if (*t == '\n')
338 {
339 // Newline - nul terminate this fragment and draw it...
340 *ptr = '\0';
341
342 fl_draw(fragment, X + width, Y, W - width, fl_height(),
343 (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CLIP), 0, 0);
344
345 // Point back to the start of the fragment...
346 ptr = fragment;
347 width = 0;
348 Y += fl_height();
349 column = 0;
350 }
351 else if (*t == column_char())
352 {
353 // Tab - nul terminate this fragment and draw it...
354 *ptr = '\0';
355
356 int cW = W - width; // Clip width...
357
358 if (columns)
359 {
360 // Try clipping inside this column...
361 for (i = 0; i < column && columns[i]; i ++);
362
363 if (columns[i])
364 cW = columns[i];
365 }
366
367 fl_draw(fragment, X + width, Y, cW, fl_height(),
368 (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CLIP), 0, 0);
369
370 // Advance to the next column...
371 column ++;
372 if (columns)
373 {
374 for (i = 0, width = 0; i < column && columns[i]; i ++)
375 width += columns[i];
376 }
377 else
378 width = column * (int)(fl_height() * 0.6 * 8.0);
379
380 ptr = fragment;
381 }
382 else
383 *ptr++ = *t;
384
385 if (ptr > fragment)
386 {
387 // Nul terminate this fragment and draw it...
388 *ptr = '\0';
389
390 fl_draw(fragment, X + width, Y, W - width, fl_height(),
391 (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CLIP), 0, 0);
392 }
393}
394
395
396//
397// 'Fl_File_Browser::Fl_File_Browser()' - Create a Fl_File_Browser widget.
398//
399
400Fl_File_Browser::Fl_File_Browser(int X, // I - Upper-lefthand X coordinate
401 int Y, // I - Upper-lefthand Y coordinate
402 int W, // I - Width in pixels
403 int H, // I - Height in pixels
404 const char *l) // I - Label text
405 : Fl_Browser(X, Y, W, H, l)
406{
407 // Initialize the filter pattern, current directory, and icon size...
408 pattern_ = "*";
409 directory_ = "";
410 iconsize_ = (uchar)(3 * textsize() / 2);
411 filetype_ = FILES;
412}
413
414
415//
416// 'Fl_File_Browser::load()' - Load a directory into the browser.
417//
418
419int // O - Number of files loaded
420Fl_File_Browser::load(const char *directory,// I - Directory to load
421 Fl_File_Sort_F *sort) // I - Sort function to use
422{
423 int i; // Looping var
424 int num_files; // Number of files in directory
425 int num_dirs; // Number of directories in list
426 char filename[4096]; // Current file
427 Fl_File_Icon *icon; // Icon to use
428
429
430// printf("Fl_File_Browser::load(\"%s\")\n", directory);
431
432 clear();
433
434 directory_ = directory;
435
436 if (!directory)
437 return (0);
438
439 if (directory_[0] == '\0')
440 {
441 //
442 // No directory specified; for UNIX list all mount points. For DOS
443 // list all valid drive letters...
444 //
445
446 num_files = 0;
447 if ((icon = Fl_File_Icon::find("any", Fl_File_Icon::DEVICE)) == NULL)
448 icon = Fl_File_Icon::find("any", Fl_File_Icon::DIRECTORY);
449
450#ifdef WIN32
451# ifdef __CYGWIN__
452 //
453 // Cygwin provides an implementation of setmntent() to get the list
454 // of available drives...
455 //
456 FILE *m = setmntent("/-not-used-", "r");
457 struct mntent *p;
458
459 while ((p = getmntent (m)) != NULL) {
460 add(p->mnt_dir, icon);
461 num_files ++;
462 }
463
464 endmntent(m);
465# else
466 //
467 // Normal WIN32 code uses drive bits...
468 //
469 DWORD drives; // Drive available bits
470
471 drives = GetLogicalDrives();
472 for (i = 'A'; i <= 'Z'; i ++, drives >>= 1)
473 if (drives & 1)
474 {
475 sprintf(filename, "%c:/", i);
476
477 if (i < 'C') // see also: GetDriveType and GetVolumeInformation in WIN32
478 add(filename, icon);
479 else
480 add(filename, icon);
481
482 num_files ++;
483 }
484# endif // __CYGWIN__
485#elif defined(__EMX__)
486 //
487 // OS/2 code uses drive bits...
488 //
489 ULONG curdrive; // Current drive
490 ULONG drives; // Drive available bits
491 int start = 3; // 'C' (MRS - dunno if this is correct!)
492
493
494 DosQueryCurrentDisk(&curdrive, &drives);
495 drives >>= start - 1;
496 for (i = 'A'; i <= 'Z'; i ++, drives >>= 1)
497 if (drives & 1)
498 {
499 sprintf(filename, "%c:/", i);
500 add(filename, icon);
501
502 num_files ++;
503 }
504#elif defined(__APPLE__)
505 // MacOS X and Darwin use getfsstat() system call...
506 int numfs; // Number of file systems
507 struct statfs *fs; // Buffer for file system info
508
509
510 // We always have the root filesystem.
511 add("/", icon);
512
513 // Get the mounted filesystems...
514 numfs = getfsstat(NULL, 0, MNT_NOWAIT);
515 if (numfs > 0) {
516 // We have file systems, get them...
517 fs = new struct statfs[numfs];
518 getfsstat(fs, sizeof(struct statfs) * numfs, MNT_NOWAIT);
519
520 // Add filesystems to the list...
521 for (i = 0; i < numfs; i ++) {
522 // Ignore "/", "/dev", and "/.vol"...
523 if (fs[i].f_mntonname[1] && strcmp(fs[i].f_mntonname, "/dev") &&
524 strcmp(fs[i].f_mntonname, "/.vol")) {
525 snprintf(filename, sizeof(filename), "%s/", fs[i].f_mntonname);
526 add(filename, icon);
527 }
528 num_files ++;
529 }
530
531 // Free the memory used for the file system info array...
532 delete[] fs;
533 }
534#else
535 //
536 // UNIX code uses /etc/fstab or similar...
537 //
538 FILE *mtab; // /etc/mtab or /etc/mnttab file
539 char line[FL_PATH_MAX]; // Input line
540
541 //
542 // Open the file that contains a list of mounted filesystems...
543 //
544
545 mtab = fl_fopen("/etc/mnttab", "r"); // Fairly standard
546 if (mtab == NULL)
547 mtab = fl_fopen("/etc/mtab", "r"); // More standard
548 if (mtab == NULL)
549 mtab = fl_fopen("/etc/fstab", "r"); // Otherwise fallback to full list
550 if (mtab == NULL)
551 mtab = fl_fopen("/etc/vfstab", "r"); // Alternate full list file
552
553 if (mtab != NULL)
554 {
555 while (fgets(line, sizeof(line), mtab) != NULL)
556 {
557 if (line[0] == '#' || line[0] == '\n')
558 continue;
559 if (sscanf(line, "%*s%4095s", filename) != 1)
560 continue;
561
562 strlcat(filename, "/", sizeof(filename));
563
564// printf("Fl_File_Browser::load() - adding \"%s\" to list...\n", filename);
565 add(filename, icon);
566 num_files ++;
567 }
568
569 fclose(mtab);
570 }
571#endif // WIN32 || __EMX__
572 }
573 else
574 {
575 dirent **files; // Files in in directory
576
577
578 //
579 // Build the file list...
580 //
581
582#if (defined(WIN32) && !defined(__CYGWIN__)) || defined(__EMX__)
583 strlcpy(filename, directory_, sizeof(filename));
584 i = strlen(filename) - 1;
585
586 if (i == 2 && filename[1] == ':' &&
587 (filename[2] == '/' || filename[2] == '\\'))
588 filename[2] = '/';
589 else if (filename[i] != '/' && filename[i] != '\\')
590 strlcat(filename, "/", sizeof(filename));
591
592 num_files = fl_filename_list(filename, &files, sort);
593#else
594 num_files = fl_filename_list(directory_, &files, sort);
595#endif /* WIN32 || __EMX__ */
596
597 if (num_files <= 0)
598 return (0);
599
600 for (i = 0, num_dirs = 0; i < num_files; i ++) {
601 if (strcmp(files[i]->d_name, "./")) {
602 snprintf(filename, sizeof(filename), "%s/%s", directory_,
603 files[i]->d_name);
604
605 icon = Fl_File_Icon::find(filename);
606 if ((icon && icon->type() == Fl_File_Icon::DIRECTORY) ||
607 _fl_filename_isdir_quick(filename)) {
608 num_dirs ++;
609 insert(num_dirs, files[i]->d_name, icon);
610 } else if (filetype_ == FILES &&
611 fl_filename_match(files[i]->d_name, pattern_)) {
612 add(files[i]->d_name, icon);
613 }
614 }
615
616 free(files[i]);
617 }
618
619 free(files);
620 }
621
622 return (num_files);
623}
624
625
626//
627// 'Fl_File_Browser::filter()' - Set the filename filter.
628//
629
630void
631Fl_File_Browser::filter(const char *pattern) // I - Pattern string
632{
633 // If pattern is NULL set the pattern to "*"...
634 if (pattern)
635 pattern_ = pattern;
636 else
637 pattern_ = "*";
638}
639
640
641//
642// End of "$Id: Fl_File_Browser.cxx 8063 2010-12-19 21:20:10Z matt $".
643//