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