fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_File_Icon.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Fl_File_Icon routines.
5 //
6 // KDE icon code donated by Maarten De Boer.
7 //
8 // Copyright 1999-2010 by Michael Sweet.
9 //
10 // This library is free software. Distribution and use rights are outlined in
11 // the file "COPYING" which should have been included with this file. If this
12 // file is missing or damaged, see the license at:
13 //
14 // http://www.fltk.org/COPYING.php
15 //
16 // Please report all bugs and problems on the following page:
17 //
18 // http://www.fltk.org/str.php
19 //
20 // Contents:
21 //
22 // Fl_File_Icon::Fl_File_Icon() - Create a new file icon.
23 // Fl_File_Icon::~Fl_File_Icon() - Remove a file icon.
24 // Fl_File_Icon::add() - Add data to an icon.
25 // Fl_File_Icon::find() - Find an icon based upon a given file.
26 // Fl_File_Icon::draw() - Draw an icon.
27 // Fl_File_Icon::label() - Set the widgets label to an icon.
28 // Fl_File_Icon::labeltype() - Draw the icon label.
29 //
30 
31 //
32 // Include necessary header files...
33 //
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <FL/fl_utf8.h>
38 #include "flstring.h"
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
43 # include <io.h>
44 # define F_OK 0
45 #else
46 # include <unistd.h>
47 #endif /* WIN32 || __EMX__ */
48 
49 #include <FL/Fl_File_Icon.H>
50 #include <FL/Fl_Widget.H>
51 #include <FL/fl_draw.H>
52 #include <FL/filename.H>
53 
54 
55 //
56 // Define missing POSIX/XPG4 macros as needed...
57 //
58 
59 #ifndef S_ISDIR
60 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
61 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
62 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
63 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
64 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
65 #endif /* !S_ISDIR */
66 
67 
68 //
69 // Icon cache...
70 //
71 
73 
74 
82 Fl_File_Icon::Fl_File_Icon(const char *p, /* I - Filename pattern */
83  int t, /* I - File type */
84  int nd, /* I - Number of data values */
85  short *d) /* I - Data values */
86 {
87  // Initialize the pattern and type...
88  pattern_ = p;
89  type_ = t;
90 
91  // Copy icon data as needed...
92  if (nd)
93  {
94  num_data_ = nd;
95  alloc_data_ = nd + 1;
96  data_ = (short *)calloc(sizeof(short), nd + 1);
97  memcpy(data_, d, nd * sizeof(short));
98  }
99  else
100  {
101  num_data_ = 0;
102  alloc_data_ = 0;
103  }
104 
105  // And add the icon to the list of icons...
106  next_ = first_;
107  first_ = this;
108 }
109 
110 
116  Fl_File_Icon *current, // Current icon in list
117  *prev; // Previous icon in list
118 
119 
120  // Find the icon in the list...
121  for (current = first_, prev = (Fl_File_Icon *)0;
122  current != this && current != (Fl_File_Icon *)0;
123  prev = current, current = current->next_) {/*empty*/}
124 
125  // Remove the icon from the list as needed...
126  if (current)
127  {
128  if (prev)
129  prev->next_ = current->next_;
130  else
131  first_ = current->next_;
132  }
133 
134  // Free any memory used...
135  if (alloc_data_)
136  free(data_);
137 }
138 
139 
144 short * // O - Pointer to new data value
145 Fl_File_Icon::add(short d) // I - Data to add
146 {
147  short *dptr; // Pointer to new data value
148 
149 
150  // Allocate/reallocate memory as needed
151  if ((num_data_ + 1) >= alloc_data_)
152  {
153  alloc_data_ += 128;
154 
155  if (alloc_data_ == 128)
156  dptr = (short *)malloc(sizeof(short) * alloc_data_);
157  else
158  dptr = (short *)realloc(data_, sizeof(short) * alloc_data_);
159 
160  if (dptr == NULL)
161  return (NULL);
162 
163  data_ = dptr;
164  }
165 
166  // Store the new data value and return
167  data_[num_data_++] = d;
168  data_[num_data_] = END;
169 
170  return (data_ + num_data_ - 1);
171 }
172 
173 
180 Fl_File_Icon * // O - Matching file icon or NULL
181 Fl_File_Icon::find(const char *filename,// I - Name of file */
182  int filetype) // I - Enumerated file type
183 {
184  Fl_File_Icon *current; // Current file in list
185 #ifndef WIN32
186  struct stat fileinfo; // Information on file
187 #endif // !WIN32
188  const char *name; // Base name of filename
189 
190 
191  // Get file information if needed...
192  if (filetype == ANY)
193  {
194 #ifdef WIN32
195  if (filename[strlen(filename) - 1] == '/')
196  filetype = DIRECTORY;
197  else if (fl_filename_isdir(filename))
198  filetype = DIRECTORY;
199  else
200  filetype = PLAIN;
201 #else
202  if (!fl_stat(filename, &fileinfo))
203  {
204  if (S_ISDIR(fileinfo.st_mode))
205  filetype = DIRECTORY;
206 # ifdef S_IFIFO
207  else if (S_ISFIFO(fileinfo.st_mode))
208  filetype = FIFO;
209 # endif // S_IFIFO
210 # if defined(S_ICHR) && defined(S_IBLK)
211  else if (S_ISCHR(fileinfo.st_mode) || S_ISBLK(fileinfo.st_mode))
212  filetype = DEVICE;
213 # endif // S_ICHR && S_IBLK
214 # ifdef S_ILNK
215  else if (S_ISLNK(fileinfo.st_mode))
216  filetype = LINK;
217 # endif // S_ILNK
218  else
219  filetype = PLAIN;
220  }
221  else
222  filetype = PLAIN;
223 #endif // WIN32
224  }
225 
226  // Look at the base name in the filename
228 
229  // Loop through the available file types and return any match that
230  // is found...
231  for (current = first_; current != (Fl_File_Icon *)0; current = current->next_)
232  if ((current->type_ == filetype || current->type_ == ANY) &&
233  (fl_filename_match(filename, current->pattern_) ||
234  fl_filename_match(name, current->pattern_)))
235  break;
236 
237  // Return the match (if any)...
238  return (current);
239 }
240 
247 void
248 Fl_File_Icon::draw(int x, // I - Upper-lefthand X
249  int y, // I - Upper-lefthand Y
250  int w, // I - Width of bounding box
251  int h, // I - Height of bounding box
252  Fl_Color ic, // I - Icon color...
253  int active) // I - Active or inactive?
254 {
255  Fl_Color c, // Current color
256  oc; // Outline color
257  short *d, // Pointer to data
258  *dend; // End of data...
259  short *prim; // Pointer to start of primitive...
260  double scale; // Scale of icon
261 
262 
263  // Don't try to draw a NULL array!
264  if (num_data_ == 0)
265  return;
266 
267  // Setup the transform matrix as needed...
268  scale = w < h ? w : h;
269 
270  fl_push_matrix();
271  fl_translate((float)x + 0.5 * ((float)w - scale),
272  (float)y + 0.5 * ((float)h + scale));
273  fl_scale(scale, -scale);
274 
275  // Loop through the array until we see an unmatched END...
276  d = data_;
277  dend = data_ + num_data_;
278  prim = NULL;
279  c = ic;
280 
281  if (active)
282  fl_color(c);
283  else
284  fl_color(fl_inactive(c));
285 
286  while (d < dend)
287  switch (*d)
288  {
289  case END :
290  if (prim)
291  switch (*prim)
292  {
293  case LINE :
294  fl_end_line();
295  break;
296 
297  case CLOSEDLINE :
298  fl_end_loop();
299  break;
300 
301  case POLYGON :
303  break;
304 
305  case OUTLINEPOLYGON :
307 
308  oc = (Fl_Color)((((unsigned short *)prim)[1] << 16) |
309  ((unsigned short *)prim)[2]);
310  if (active)
311  {
312  if (oc == FL_ICON_COLOR)
313  fl_color(ic);
314  else
315  fl_color(oc);
316  }
317  else
318  {
319  if (oc == FL_ICON_COLOR)
320  fl_color(fl_inactive(ic));
321  else
322  fl_color(fl_inactive(oc));
323  }
324 
325  fl_begin_loop();
326 
327  prim += 3;
328  while (*prim == VERTEX)
329  {
330  fl_vertex(prim[1] * 0.0001, prim[2] * 0.0001);
331  prim += 3;
332  }
333 
334  fl_end_loop();
335  fl_color(c);
336  break;
337  }
338 
339  prim = NULL;
340  d ++;
341  break;
342 
343  case COLOR :
344  c = (Fl_Color)((((unsigned short *)d)[1] << 16) |
345  ((unsigned short *)d)[2]);
346 
347  if (c == FL_ICON_COLOR)
348  c = ic;
349 
350  if (!active)
351  c = fl_inactive(c);
352 
353  fl_color(c);
354  d += 3;
355  break;
356 
357  case LINE :
358  prim = d;
359  d ++;
360  fl_begin_line();
361  break;
362 
363  case CLOSEDLINE :
364  prim = d;
365  d ++;
366  fl_begin_loop();
367  break;
368 
369  case POLYGON :
370  prim = d;
371  d ++;
373  break;
374 
375  case OUTLINEPOLYGON :
376  prim = d;
377  d += 3;
379  break;
380 
381  case VERTEX :
382  if (prim)
383  fl_vertex(d[1] * 0.0001, d[2] * 0.0001);
384  d += 3;
385  break;
386 
387  default : // Ignore invalid data...
388  d ++;
389  }
390 
391  // If we still have an open primitive, close it...
392  if (prim)
393  switch (*prim)
394  {
395  case LINE :
396  fl_end_line();
397  break;
398 
399  case CLOSEDLINE :
400  fl_end_loop();
401  break;
402 
403  case POLYGON :
404  fl_end_polygon();
405  break;
406 
407  case OUTLINEPOLYGON :
408  fl_end_polygon();
409 
410  oc = (Fl_Color)((((unsigned short *)prim)[1] << 16) |
411  ((unsigned short *)prim)[2]);
412  if (active)
413  {
414  if (oc == FL_ICON_COLOR)
415  fl_color(ic);
416  else
417  fl_color(oc);
418  }
419  else
420  {
421  if (oc == FL_ICON_COLOR)
422  fl_color(fl_inactive(ic));
423  else
424  fl_color(fl_inactive(oc));
425  }
426 
427  fl_begin_loop();
428 
429  prim += 3;
430  while (*prim == VERTEX)
431  {
432  fl_vertex(prim[1] * 0.0001, prim[2] * 0.0001);
433  prim += 3;
434  }
435 
436  fl_end_loop();
437  fl_color(c);
438  break;
439  }
440 
441  // Restore the transform matrix
442  fl_pop_matrix();
443 }
444 
450 void Fl_File_Icon::label(Fl_Widget *w) // I - Widget to label
451 {
453  w->label(_FL_ICON_LABEL, (const char*)this);
454 }
455 
456 
463 void
464 Fl_File_Icon::labeltype(const Fl_Label *o, // I - Label data
465  int x, // I - X position of label
466  int y, // I - Y position of label
467  int w, // I - Width of label
468  int h, // I - Height of label
469  Fl_Align a) // I - Label alignment (not used)
470 {
471  Fl_File_Icon *icon; // Pointer to icon data
472 
473 
474  (void)a;
475 
476  icon = (Fl_File_Icon *)(o->value);
477  if (icon) icon->draw(x, y, w, h, (Fl_Color)(o->color));
478 }
479 
480 
481 //
482 // End of "$Id$".
483 //
Fl_Widget.H
Fl_File_Icon
Definition: Fl_File_Icon.H:47
Fl_File_Icon::num_data_
int num_data_
Definition: Fl_File_Icon.H:53
Fl_File_Icon::ANY
Definition: Fl_File_Icon.H:61
Fl_Color
unsigned int Fl_Color
Definition: Enumerations.H:934
Fl_File_Icon::type_
int type_
Definition: Fl_File_Icon.H:52
Fl_File_Icon::next_
Fl_File_Icon * next_
Definition: Fl_File_Icon.H:50
fl_begin_complex_polygon
void fl_begin_complex_polygon()
Definition: fl_draw.H:455
S_ISDIR
#define S_ISDIR(m)
Definition: Fl_File_Icon.cxx:62
Fl_File_Icon::draw
void draw(int x, int y, int w, int h, Fl_Color ic, int active=1)
Definition: Fl_File_Icon.cxx:248
_FL_ICON_LABEL
draws the icon associated with the text
Definition: Enumerations.H:770
Fl_File_Icon::alloc_data_
int alloc_data_
Definition: Fl_File_Icon.H:54
fl_push_matrix
void fl_push_matrix()
Definition: fl_draw.H:326
fl_color
void fl_color(Fl_Color c)
Definition: fl_draw.H:52
Fl_File_Icon::PLAIN
Definition: Fl_File_Icon.H:62
filename.H
Fl_File_Icon::labeltype
static void labeltype(const Fl_Label *o, int x, int y, int w, int h, Fl_Align a)
Definition: Fl_File_Icon.cxx:464
free
void free()
NULL
#define NULL
Definition: forms.H:34
FL_ICON_COLOR
#define FL_ICON_COLOR
Definition: Fl_File_Icon.H:36
Fl_Label::color
Fl_Color color
Definition: Fl_Widget.H:77
fl_filename_isdir
int fl_filename_isdir(const char *name)
Definition: filename_isdir.cxx:52
fl_begin_loop
void fl_begin_loop()
Definition: fl_draw.H:370
icon
static Fl_Box * icon
Definition: fl_ask.cxx:48
Fl_File_Icon::~Fl_File_Icon
~Fl_File_Icon()
Definition: Fl_File_Icon.cxx:115
Fl_File_Icon::OUTLINEPOLYGON
Definition: Fl_File_Icon.H:76
Fl_File_Icon::VERTEX
Definition: Fl_File_Icon.H:77
Fl_File_Icon::first_
static Fl_File_Icon * first_
Definition: Fl_File_Icon.H:49
Fl_File_Icon::find
static Fl_File_Icon * find(const char *filename, int filetype=ANY)
Definition: Fl_File_Icon.cxx:181
fl_translate
void fl_translate(double x, double y)
Definition: fl_draw.H:345
fl_end_loop
void fl_end_loop()
Definition: fl_draw.H:436
p
static menustate * p
Definition: Fl_Menu.cxx:606
fl_utf8.h
header for Unicode and UTF-8 character handling
Fl_File_Icon::add
short * add(short d)
Definition: Fl_File_Icon.cxx:145
fl_filename_match
int fl_filename_match(const char *name, const char *pattern)
Definition: filename_match.cxx:44
fl_draw.H
utility header to pull drawing functions together
filename
static const char * filename
Definition: fluid.cxx:119
fl_filename_name
const char * fl_filename_name(const char *filename)
Definition: Fl_win32.cxx:2179
Fl_Label
Definition: Fl_Widget.H:65
fl_inactive
Fl_Color fl_inactive(Fl_Color c)
Definition: fl_color.cxx:423
S_ISLNK
#define S_ISLNK(m)
Definition: Fl_File_Icon.cxx:64
fl_stat
int fl_stat(const char *path, struct stat *buffer)
Definition: fl_utf8.cxx:669
fl_end_line
void fl_end_line()
Definition: fl_draw.H:432
Fl_File_Icon::FIFO
Definition: Fl_File_Icon.H:63
Fl_Widget
Definition: Fl_Widget.H:101
fl_end_polygon
void fl_end_polygon()
Definition: fl_draw.H:440
S_ISCHR
#define S_ISCHR(m)
Definition: Fl_File_Icon.cxx:61
Fl_File_Icon::DIRECTORY
Definition: Fl_File_Icon.H:66
Fl_File_Icon::label
void label(Fl_Widget *w)
Definition: Fl_File_Icon.cxx:450
Fl_Box::draw
void draw()
Definition: Fl_Box.cxx:34
Fl_File_Icon.H
Fl_Widget::label
const char * label() const
Definition: Fl_Widget.H:421
Fl_File_Icon::pattern_
const char * pattern_
Definition: Fl_File_Icon.H:51
Fl_File_Icon::LINE
Definition: Fl_File_Icon.H:73
Fl::set_labeltype
static void set_labeltype(Fl_Labeltype, Fl_Label_Draw_F *, Fl_Label_Measure_F *)
Definition: fl_labeltype.cxx:69
Fl_File_Icon::LINK
Definition: Fl_File_Icon.H:65
Fl_File_Icon::POLYGON
Definition: Fl_File_Icon.H:75
x
int x
Definition: test.c:73
fl_begin_line
void fl_begin_line()
Definition: fl_draw.H:366
Fl_File_Icon::DEVICE
Definition: Fl_File_Icon.H:64
Fl_Label::value
const char * value
Definition: Fl_Widget.H:67
y
int y
Definition: test.c:74
fl_pop_matrix
void fl_pop_matrix()
Definition: fl_draw.H:330
calloc
voidp calloc()
malloc
voidp malloc()
flstring.h
fl_vertex
void fl_vertex(double x, double y)
Definition: fl_draw.H:379
fl_end_complex_polygon
void fl_end_complex_polygon()
Definition: fl_draw.H:466
S_ISFIFO
#define S_ISFIFO(m)
Definition: Fl_File_Icon.cxx:63
Fl_Align
unsigned Fl_Align
Definition: Enumerations.H:828
name
static const char * name
Definition: Fl_arg.cxx:53
Fl_File_Icon::data_
short * data_
Definition: Fl_File_Icon.H:55
Fl_File_Icon::COLOR
Definition: Fl_File_Icon.H:72
Fl_File_Icon::END
Definition: Fl_File_Icon.H:71
Fl_File_Icon::Fl_File_Icon
Fl_File_Icon(const char *p, int t, int nd=0, short *d=0)
Definition: Fl_File_Icon.cxx:82
S_ISBLK
#define S_ISBLK(m)
Definition: Fl_File_Icon.cxx:60
fl_scale
void fl_scale(double x, double y)
Definition: fl_draw.H:335
Fl_File_Icon::CLOSEDLINE
Definition: Fl_File_Icon.H:74