2001-08-02 01:24:49 +04:00
|
|
|
//
|
2005-02-05 21:26:21 +03:00
|
|
|
// "$Id$"
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// Fl_File_Browser routines.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2004-04-11 08:39:01 +04:00
|
|
|
// Copyright 1999-2004 by Michael Sweet.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2001-08-02 20:17:04 +04:00
|
|
|
// Please report all bugs and problems to "fltk-bugs@fltk.org".
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
// Contents:
|
|
|
|
//
|
2002-05-02 18:31:10 +04:00
|
|
|
// Fl_File_Browser::full_height() - Return the height of the list.
|
|
|
|
// Fl_File_Browser::item_height() - Return the height of a list item.
|
|
|
|
// Fl_File_Browser::item_width() - Return the width of a list item.
|
|
|
|
// Fl_File_Browser::item_draw() - Draw a list item.
|
2001-09-29 18:38:59 +04:00
|
|
|
// Fl_File_Browser::Fl_File_Browser() - Create a Fl_File_Browser widget.
|
2002-05-02 18:31:10 +04:00
|
|
|
// Fl_File_Browser::load() - Load a directory into the browser.
|
|
|
|
// Fl_File_Browser::filter() - Set the filename filter.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Include necessary header files...
|
|
|
|
//
|
|
|
|
|
2001-09-29 18:38:59 +04:00
|
|
|
#include <FL/Fl_File_Browser.H>
|
2001-08-02 20:17:04 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/filename.H>
|
|
|
|
#include <stdio.h>
|
2001-08-02 01:24:49 +04:00
|
|
|
#include <stdlib.h>
|
2001-11-25 19:38:11 +03:00
|
|
|
#include "flstring.h"
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-04-14 16:51:56 +04:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <mntent.h>
|
|
|
|
#elif defined(WIN32)
|
2001-08-02 01:24:49 +04:00
|
|
|
# include <windows.h>
|
|
|
|
# include <direct.h>
|
2002-06-13 23:36:01 +04:00
|
|
|
// Apparently Borland C++ defines DIRECTORY in <direct.h>, which
|
|
|
|
// interfers with the Fl_File_Icon enumeration of the same name.
|
2002-05-02 00:05:19 +04:00
|
|
|
# ifdef DIRECTORY
|
|
|
|
# undef DIRECTORY
|
|
|
|
# endif // DIRECTORY
|
2002-04-16 18:50:10 +04:00
|
|
|
#endif // __CYGWIN__
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-04-16 18:50:10 +04:00
|
|
|
#ifdef __EMX__
|
2002-04-14 16:51:56 +04:00
|
|
|
# define INCL_DOS
|
|
|
|
# define INCL_DOSMISC
|
|
|
|
# include <os2.h>
|
2002-04-16 18:50:10 +04:00
|
|
|
#endif // __EMX__
|
|
|
|
|
2002-07-17 19:23:58 +04:00
|
|
|
// CodeWarrior (__MWERKS__) gets its include paths confused, so we
|
|
|
|
// temporarily disable this...
|
2002-07-17 10:09:26 +04:00
|
|
|
#if defined(__APPLE__) && !defined(__MWERKS__)
|
2002-04-16 18:50:10 +04:00
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/ucred.h>
|
|
|
|
# include <sys/mount.h>
|
2002-07-17 19:23:58 +04:00
|
|
|
#endif // __APPLE__ && !__MWERKS__
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2001-08-02 20:17:04 +04:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
// FL_BLINE definition from "Fl_Browser.cxx"...
|
|
|
|
//
|
|
|
|
|
|
|
|
#define SELECTED 1
|
|
|
|
#define NOTDISPLAYED 2
|
|
|
|
|
|
|
|
struct FL_BLINE // data is in a linked list of these
|
|
|
|
{
|
|
|
|
FL_BLINE *prev; // Previous item in list
|
|
|
|
FL_BLINE *next; // Next item in list
|
|
|
|
void *data; // Pointer to data (function)
|
|
|
|
short length; // sizeof(txt)-1, may be longer than string
|
|
|
|
char flags; // selected, displayed
|
|
|
|
char txt[1]; // start of allocated array
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-08-02 20:17:04 +04:00
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::full_height()' - Return the height of the list.
|
2001-08-02 20:17:04 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
int // O - Height in pixels
|
2001-09-29 18:38:59 +04:00
|
|
|
Fl_File_Browser::full_height() const
|
2001-08-02 20:17:04 +04:00
|
|
|
{
|
|
|
|
int i, // Looping var
|
|
|
|
th; // Total height of list.
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0, th = 0; i < size(); i ++)
|
|
|
|
th += item_height(find_line(i));
|
|
|
|
|
|
|
|
return (th);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::item_height()' - Return the height of a list item.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
int // O - Height in pixels
|
2001-09-29 18:38:59 +04:00
|
|
|
Fl_File_Browser::item_height(void *p) const // I - List item data
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
FL_BLINE *line; // Pointer to line
|
2002-08-09 05:09:49 +04:00
|
|
|
char *t; // Pointer into text
|
2001-08-02 01:24:49 +04:00
|
|
|
int height; // Width of line
|
|
|
|
int textheight; // Height of text
|
|
|
|
|
|
|
|
|
|
|
|
// Figure out the standard text height...
|
2001-08-02 20:17:04 +04:00
|
|
|
fl_font(textfont(), textsize());
|
|
|
|
textheight = fl_height();
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// We always have at least 1 line...
|
|
|
|
height = textheight;
|
|
|
|
|
|
|
|
// Scan for newlines...
|
|
|
|
line = (FL_BLINE *)p;
|
|
|
|
|
|
|
|
if (line != NULL)
|
2002-08-09 05:09:49 +04:00
|
|
|
for (t = line->txt; *t != '\0'; t ++)
|
|
|
|
if (*t == '\n')
|
2001-08-02 01:24:49 +04:00
|
|
|
height += textheight;
|
|
|
|
|
|
|
|
// If we have enabled icons then add space for them...
|
2001-09-29 18:38:59 +04:00
|
|
|
if (Fl_File_Icon::first() != NULL && height < iconsize_)
|
2001-08-02 01:24:49 +04:00
|
|
|
height = iconsize_;
|
|
|
|
|
|
|
|
// Add space for the selection border..
|
|
|
|
height += 2;
|
|
|
|
|
|
|
|
// Return the height
|
|
|
|
return (height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::item_width()' - Return the width of a list item.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
int // O - Width in pixels
|
2001-09-29 18:38:59 +04:00
|
|
|
Fl_File_Browser::item_width(void *p) const // I - List item data
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
2001-08-02 20:17:04 +04:00
|
|
|
int i; // Looping var
|
2001-08-02 01:24:49 +04:00
|
|
|
FL_BLINE *line; // Pointer to line
|
2002-08-09 05:09:49 +04:00
|
|
|
char *t, // Pointer into text
|
2001-08-02 01:24:49 +04:00
|
|
|
*ptr, // Pointer into fragment
|
|
|
|
fragment[10240]; // Fragment of text
|
|
|
|
int width, // Width of line
|
|
|
|
tempwidth; // Width of fragment
|
|
|
|
int column; // Current column
|
2001-08-02 20:17:04 +04:00
|
|
|
const int *columns; // Columns
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Set the font and size...
|
2001-08-02 20:17:04 +04:00
|
|
|
fl_font(textfont(), textsize());
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// Scan for newlines...
|
2001-08-02 20:17:04 +04:00
|
|
|
line = (FL_BLINE *)p;
|
|
|
|
columns = column_widths();
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
if (strchr(line->txt, '\n') == NULL &&
|
2001-08-02 20:17:04 +04:00
|
|
|
strchr(line->txt, column_char()) == NULL)
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
// Do a fast width calculation...
|
2001-08-02 20:17:04 +04:00
|
|
|
width = (int)fl_width(line->txt);
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// More than 1 line or have columns; find the maximum width...
|
|
|
|
width = 0;
|
|
|
|
tempwidth = 0;
|
|
|
|
column = 0;
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
for (t = line->txt, ptr = fragment; *t != '\0'; t ++)
|
|
|
|
if (*t == '\n')
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
// Newline - nul terminate this fragment and get the width...
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2001-08-02 20:17:04 +04:00
|
|
|
tempwidth += (int)fl_width(fragment);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// Update the max width as needed...
|
|
|
|
if (tempwidth > width)
|
|
|
|
width = tempwidth;
|
|
|
|
|
|
|
|
// Point back to the start of the fragment...
|
|
|
|
ptr = fragment;
|
|
|
|
tempwidth = 0;
|
2001-08-02 20:17:04 +04:00
|
|
|
column = 0;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
2002-08-09 05:09:49 +04:00
|
|
|
else if (*t == column_char())
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
// Advance to the next column...
|
|
|
|
column ++;
|
2001-08-02 20:17:04 +04:00
|
|
|
if (columns)
|
|
|
|
{
|
|
|
|
for (i = 0, tempwidth = 0; i < column && columns[i]; i ++)
|
|
|
|
tempwidth += columns[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tempwidth = column * (int)(fl_height() * 0.6 * 8.0);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
if (tempwidth > width)
|
|
|
|
width = tempwidth;
|
|
|
|
|
|
|
|
ptr = fragment;
|
|
|
|
}
|
|
|
|
else
|
2002-08-09 05:09:49 +04:00
|
|
|
*ptr++ = *t;
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
if (ptr > fragment)
|
|
|
|
{
|
|
|
|
// Nul terminate this fragment and get the width...
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2001-08-02 20:17:04 +04:00
|
|
|
tempwidth += (int)fl_width(fragment);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// Update the max width as needed...
|
|
|
|
if (tempwidth > width)
|
|
|
|
width = tempwidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have enabled icons then add space for them...
|
2001-09-29 18:38:59 +04:00
|
|
|
if (Fl_File_Icon::first() != NULL)
|
2001-08-02 01:24:49 +04:00
|
|
|
width += iconsize_ + 8;
|
|
|
|
|
|
|
|
// Add space for the selection border..
|
|
|
|
width += 2;
|
|
|
|
|
|
|
|
// Return the width
|
|
|
|
return (width);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::item_draw()' - Draw a list item.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2001-09-29 18:38:59 +04:00
|
|
|
Fl_File_Browser::item_draw(void *p, // I - List item data
|
2002-08-09 05:09:49 +04:00
|
|
|
int X, // I - Upper-lefthand X coordinate
|
|
|
|
int Y, // I - Upper-lefthand Y coordinate
|
|
|
|
int W, // I - Width of item
|
2002-08-13 19:42:44 +04:00
|
|
|
int) const // I - Height of item
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
2001-08-02 20:17:04 +04:00
|
|
|
int i; // Looping var
|
2001-08-02 01:24:49 +04:00
|
|
|
FL_BLINE *line; // Pointer to line
|
2001-08-02 20:17:04 +04:00
|
|
|
Fl_Color c; // Text color
|
2002-08-09 05:09:49 +04:00
|
|
|
char *t, // Pointer into text
|
2001-08-02 20:17:04 +04:00
|
|
|
*ptr, // Pointer into fragment
|
|
|
|
fragment[10240]; // Fragment of text
|
|
|
|
int width, // Width of line
|
|
|
|
height; // Height of line
|
|
|
|
int column; // Current column
|
|
|
|
const int *columns; // Columns
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Draw the list item text...
|
|
|
|
line = (FL_BLINE *)p;
|
|
|
|
|
2001-08-03 22:46:57 +04:00
|
|
|
if (line->txt[strlen(line->txt) - 1] == '/')
|
|
|
|
fl_font(textfont() | FL_BOLD, textsize());
|
|
|
|
else
|
|
|
|
fl_font(textfont(), textsize());
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2001-08-02 20:17:04 +04:00
|
|
|
if (line->flags & SELECTED)
|
2001-10-29 06:44:33 +03:00
|
|
|
c = fl_contrast(textcolor(), selection_color());
|
2001-08-02 01:24:49 +04:00
|
|
|
else
|
2001-08-02 20:17:04 +04:00
|
|
|
c = textcolor();
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2001-09-29 18:38:59 +04:00
|
|
|
if (Fl_File_Icon::first() == NULL)
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
// No icons, just draw the text...
|
2002-08-09 05:09:49 +04:00
|
|
|
X ++;
|
|
|
|
W -= 2;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-02 20:17:04 +04:00
|
|
|
// Draw the icon if it is set...
|
2001-08-02 01:24:49 +04:00
|
|
|
if (line->data)
|
2002-08-09 05:09:49 +04:00
|
|
|
((Fl_File_Icon *)line->data)->draw(X, Y, iconsize_, iconsize_,
|
2001-08-03 22:46:57 +04:00
|
|
|
(line->flags & SELECTED) ? FL_YELLOW :
|
|
|
|
FL_LIGHT2,
|
|
|
|
active_r());
|
2001-08-02 20:17:04 +04:00
|
|
|
|
|
|
|
// Draw the text offset to the right...
|
2002-08-09 05:09:49 +04:00
|
|
|
X += iconsize_ + 9;
|
|
|
|
W -= iconsize_ - 10;
|
2001-08-02 20:17:04 +04:00
|
|
|
|
|
|
|
// Center the text vertically...
|
|
|
|
height = fl_height();
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
for (t = line->txt; *t != '\0'; t ++)
|
|
|
|
if (*t == '\n')
|
2001-08-02 20:17:04 +04:00
|
|
|
height += fl_height();
|
|
|
|
|
|
|
|
if (height < iconsize_)
|
2002-08-09 05:09:49 +04:00
|
|
|
Y += (iconsize_ - height) / 2;
|
2001-08-02 20:17:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the text...
|
|
|
|
line = (FL_BLINE *)p;
|
|
|
|
columns = column_widths();
|
|
|
|
width = 0;
|
|
|
|
column = 0;
|
|
|
|
|
|
|
|
if (active_r())
|
|
|
|
fl_color(c);
|
|
|
|
else
|
2001-10-29 06:44:33 +03:00
|
|
|
fl_color(fl_inactive(c));
|
2001-08-02 20:17:04 +04:00
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
for (t = line->txt, ptr = fragment; *t != '\0'; t ++)
|
|
|
|
if (*t == '\n')
|
2001-08-02 20:17:04 +04:00
|
|
|
{
|
|
|
|
// Newline - nul terminate this fragment and draw it...
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_draw(fragment, X + width, Y, W - width, fl_height(),
|
2001-11-03 08:11:34 +03:00
|
|
|
(Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CLIP), 0, 0);
|
2001-08-02 20:17:04 +04:00
|
|
|
|
|
|
|
// Point back to the start of the fragment...
|
|
|
|
ptr = fragment;
|
|
|
|
width = 0;
|
2002-08-09 05:09:49 +04:00
|
|
|
Y += fl_height();
|
2001-08-02 20:17:04 +04:00
|
|
|
column = 0;
|
|
|
|
}
|
2002-08-09 05:09:49 +04:00
|
|
|
else if (*t == column_char())
|
2001-08-02 20:17:04 +04:00
|
|
|
{
|
|
|
|
// Tab - nul terminate this fragment and draw it...
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
int cW = W - width; // Clip width...
|
2002-05-06 08:11:50 +04:00
|
|
|
|
|
|
|
if (columns)
|
|
|
|
{
|
|
|
|
// Try clipping inside this column...
|
|
|
|
for (i = 0; i < column && columns[i]; i ++);
|
|
|
|
|
|
|
|
if (columns[i])
|
|
|
|
cW = columns[i];
|
|
|
|
}
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_draw(fragment, X + width, Y, cW, fl_height(),
|
2001-11-03 08:11:34 +03:00
|
|
|
(Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CLIP), 0, 0);
|
2001-08-02 20:17:04 +04:00
|
|
|
|
|
|
|
// Advance to the next column...
|
|
|
|
column ++;
|
|
|
|
if (columns)
|
|
|
|
{
|
|
|
|
for (i = 0, width = 0; i < column && columns[i]; i ++)
|
|
|
|
width += columns[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
width = column * (int)(fl_height() * 0.6 * 8.0);
|
|
|
|
|
|
|
|
ptr = fragment;
|
|
|
|
}
|
|
|
|
else
|
2002-08-09 05:09:49 +04:00
|
|
|
*ptr++ = *t;
|
2001-08-02 20:17:04 +04:00
|
|
|
|
|
|
|
if (ptr > fragment)
|
|
|
|
{
|
|
|
|
// Nul terminate this fragment and draw it...
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_draw(fragment, X + width, Y, W - width, fl_height(),
|
2001-11-03 08:11:34 +03:00
|
|
|
(Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CLIP), 0, 0);
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::Fl_File_Browser()' - Create a Fl_File_Browser widget.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
Fl_File_Browser::Fl_File_Browser(int X, // I - Upper-lefthand X coordinate
|
|
|
|
int Y, // I - Upper-lefthand Y coordinate
|
|
|
|
int W, // I - Width in pixels
|
|
|
|
int H, // I - Height in pixels
|
2002-03-29 17:16:03 +03:00
|
|
|
const char *l) // I - Label text
|
2002-08-09 05:09:49 +04:00
|
|
|
: Fl_Browser(X, Y, W, H, l)
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
// Initialize the filter pattern, current directory, and icon size...
|
|
|
|
pattern_ = "*";
|
|
|
|
directory_ = "";
|
2002-11-19 19:37:36 +03:00
|
|
|
iconsize_ = (uchar)(3 * textsize() / 2);
|
2001-09-04 17:13:29 +04:00
|
|
|
filetype_ = FILES;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::load()' - Load a directory into the browser.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
2002-05-02 18:31:10 +04:00
|
|
|
int // O - Number of files loaded
|
|
|
|
Fl_File_Browser::load(const char *directory,// I - Directory to load
|
|
|
|
Fl_File_Sort_F *sort) // I - Sort function to use
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
2002-05-02 18:31:10 +04:00
|
|
|
int i; // Looping var
|
|
|
|
int num_files; // Number of files in directory
|
|
|
|
int num_dirs; // Number of directories in list
|
|
|
|
char filename[4096]; // Current file
|
|
|
|
Fl_File_Icon *icon; // Icon to use
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
|
2001-09-29 18:38:59 +04:00
|
|
|
// printf("Fl_File_Browser::load(\"%s\")\n", directory);
|
2001-08-02 20:17:04 +04:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
clear();
|
2004-02-26 06:06:41 +03:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
directory_ = directory;
|
|
|
|
|
2004-02-26 06:06:41 +03:00
|
|
|
if (!directory)
|
|
|
|
return (0);
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
if (directory_[0] == '\0')
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// No directory specified; for UNIX list all mount points. For DOS
|
|
|
|
// list all valid drive letters...
|
|
|
|
//
|
|
|
|
|
|
|
|
num_files = 0;
|
2001-09-29 18:38:59 +04:00
|
|
|
if ((icon = Fl_File_Icon::find("any", Fl_File_Icon::DEVICE)) == NULL)
|
|
|
|
icon = Fl_File_Icon::find("any", Fl_File_Icon::DIRECTORY);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-04-14 16:51:56 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
# ifdef __CYGWIN__
|
|
|
|
//
|
|
|
|
// Cygwin provides an implementation of setmntent() to get the list
|
|
|
|
// of available drives...
|
|
|
|
//
|
|
|
|
FILE *m = setmntent("/-not-used-", "r");
|
|
|
|
struct mntent *p;
|
|
|
|
|
|
|
|
while ((p = getmntent (m)) != NULL) {
|
|
|
|
add(p->mnt_dir, icon);
|
|
|
|
num_files ++;
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-04-14 16:51:56 +04:00
|
|
|
endmntent(m);
|
|
|
|
# else
|
|
|
|
//
|
|
|
|
// Normal WIN32 code uses drive bits...
|
|
|
|
//
|
|
|
|
DWORD drives; // Drive available bits
|
2001-08-02 20:17:04 +04:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
drives = GetLogicalDrives();
|
|
|
|
for (i = 'A'; i <= 'Z'; i ++, drives >>= 1)
|
|
|
|
if (drives & 1)
|
|
|
|
{
|
2001-08-03 22:46:57 +04:00
|
|
|
sprintf(filename, "%c:/", i);
|
2001-08-02 20:17:04 +04:00
|
|
|
|
|
|
|
if (i < 'C')
|
|
|
|
add(filename, icon);
|
|
|
|
else
|
|
|
|
add(filename, icon);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
num_files ++;
|
|
|
|
}
|
2002-04-14 16:51:56 +04:00
|
|
|
# endif // __CYGWIN__
|
2001-08-02 01:24:49 +04:00
|
|
|
#elif defined(__EMX__)
|
2002-04-14 16:51:56 +04:00
|
|
|
//
|
|
|
|
// OS/2 code uses drive bits...
|
|
|
|
//
|
2001-08-02 01:24:49 +04:00
|
|
|
ULONG curdrive; // Current drive
|
|
|
|
ULONG drives; // Drive available bits
|
|
|
|
int start = 3; // 'C' (MRS - dunno if this is correct!)
|
|
|
|
|
|
|
|
|
|
|
|
DosQueryCurrentDisk(&curdrive, &drives);
|
|
|
|
drives >>= start - 1;
|
|
|
|
for (i = 'A'; i <= 'Z'; i ++, drives >>= 1)
|
|
|
|
if (drives & 1)
|
|
|
|
{
|
2001-08-03 22:46:57 +04:00
|
|
|
sprintf(filename, "%c:/", i);
|
2001-08-02 01:24:49 +04:00
|
|
|
add(filename, icon);
|
|
|
|
|
|
|
|
num_files ++;
|
|
|
|
}
|
2002-07-17 10:09:26 +04:00
|
|
|
#elif defined(__APPLE__) && !defined(__MWERKS__)
|
2002-04-16 18:50:10 +04:00
|
|
|
// MacOS X and Darwin use getfsstat() system call...
|
|
|
|
int numfs; // Number of file systems
|
|
|
|
struct statfs *fs; // Buffer for file system info
|
|
|
|
|
|
|
|
|
|
|
|
numfs = getfsstat(NULL, 0, MNT_NOWAIT);
|
|
|
|
if (numfs > 0) {
|
|
|
|
// We have file systems, get them...
|
|
|
|
fs = new struct statfs[numfs];
|
|
|
|
getfsstat(fs, sizeof(struct statfs) * numfs, MNT_NOWAIT);
|
|
|
|
|
|
|
|
// Add filesystems to the list...
|
|
|
|
for (i = 0; i < numfs; i ++) {
|
|
|
|
if (fs[i].f_mntonname[1]) {
|
|
|
|
snprintf(filename, sizeof(filename), "%s/", fs[i].f_mntonname);
|
|
|
|
add(filename, icon);
|
|
|
|
} else {
|
|
|
|
add("/", icon);
|
|
|
|
}
|
|
|
|
num_files ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the memory used for the file system info array...
|
|
|
|
delete[] fs;
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
#else
|
2002-04-14 16:51:56 +04:00
|
|
|
//
|
|
|
|
// UNIX code uses /etc/fstab or similar...
|
|
|
|
//
|
2001-08-02 01:24:49 +04:00
|
|
|
FILE *mtab; // /etc/mtab or /etc/mnttab file
|
|
|
|
char line[1024]; // Input line
|
|
|
|
|
|
|
|
//
|
|
|
|
// Open the file that contains a list of mounted filesystems...
|
|
|
|
//
|
2002-04-14 16:51:56 +04:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
mtab = fopen("/etc/mnttab", "r"); // Fairly standard
|
|
|
|
if (mtab == NULL)
|
2002-04-14 16:51:56 +04:00
|
|
|
mtab = fopen("/etc/mtab", "r"); // More standard
|
|
|
|
if (mtab == NULL)
|
|
|
|
mtab = fopen("/etc/fstab", "r"); // Otherwise fallback to full list
|
|
|
|
if (mtab == NULL)
|
|
|
|
mtab = fopen("/etc/vfstab", "r"); // Alternate full list file
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
if (mtab != NULL)
|
|
|
|
{
|
|
|
|
while (fgets(line, sizeof(line), mtab) != NULL)
|
|
|
|
{
|
|
|
|
if (line[0] == '#' || line[0] == '\n')
|
|
|
|
continue;
|
|
|
|
if (sscanf(line, "%*s%4095s", filename) != 1)
|
|
|
|
continue;
|
|
|
|
|
2002-05-16 16:47:44 +04:00
|
|
|
strlcat(filename, "/", sizeof(filename));
|
2001-08-03 22:46:57 +04:00
|
|
|
|
2001-09-29 18:38:59 +04:00
|
|
|
// printf("Fl_File_Browser::load() - adding \"%s\" to list...\n", filename);
|
2001-08-02 01:24:49 +04:00
|
|
|
add(filename, icon);
|
|
|
|
num_files ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(mtab);
|
|
|
|
}
|
2001-08-02 20:17:04 +04:00
|
|
|
#endif // WIN32 || __EMX__
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirent **files; // Files in in directory
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Build the file list...
|
|
|
|
//
|
|
|
|
|
2002-04-14 16:51:56 +04:00
|
|
|
#if (defined(WIN32) && !defined(__CYGWIN__)) || defined(__EMX__)
|
2002-05-16 16:47:44 +04:00
|
|
|
strlcpy(filename, directory_, sizeof(filename));
|
2001-08-02 01:24:49 +04:00
|
|
|
i = strlen(filename) - 1;
|
|
|
|
|
|
|
|
if (i == 2 && filename[1] == ':' &&
|
|
|
|
(filename[2] == '/' || filename[2] == '\\'))
|
|
|
|
filename[2] = '/';
|
|
|
|
else if (filename[i] != '/' && filename[i] != '\\')
|
2002-05-16 16:47:44 +04:00
|
|
|
strlcat(filename, "/", sizeof(filename));
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-05-02 18:31:10 +04:00
|
|
|
num_files = fl_filename_list(filename, &files, sort);
|
2001-08-02 01:24:49 +04:00
|
|
|
#else
|
2002-05-02 18:31:10 +04:00
|
|
|
num_files = fl_filename_list(directory_, &files, sort);
|
2001-08-02 20:17:04 +04:00
|
|
|
#endif /* WIN32 || __EMX__ */
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
if (num_files <= 0)
|
|
|
|
return (0);
|
|
|
|
|
2005-02-05 21:26:21 +03:00
|
|
|
for (i = 0, num_dirs = 0; i < num_files; i ++) {
|
2003-05-05 01:45:46 +04:00
|
|
|
if (strcmp(files[i]->d_name, ".") &&
|
2005-02-05 21:26:21 +03:00
|
|
|
strcmp(files[i]->d_name, "./")) {
|
2001-08-03 22:46:57 +04:00
|
|
|
snprintf(filename, sizeof(filename), "%s/%s", directory_,
|
|
|
|
files[i]->d_name);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2005-02-05 21:26:21 +03:00
|
|
|
icon = Fl_File_Icon::find(filename);
|
|
|
|
if (icon->type() == Fl_File_Icon::DIRECTORY) {
|
2002-04-29 23:40:51 +04:00
|
|
|
num_dirs ++;
|
2005-02-05 21:26:21 +03:00
|
|
|
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
// WIN32 already has the trailing slash... :)
|
|
|
|
insert(num_dirs, files[i]->d_name, icon);
|
2002-04-29 23:40:51 +04:00
|
|
|
#else
|
2005-02-05 21:26:21 +03:00
|
|
|
// Add a trailing slash to directory names...
|
2001-08-03 22:46:57 +04:00
|
|
|
char name[1024]; // Temporary directory name
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "%s/", files[i]->d_name);
|
|
|
|
|
2005-02-05 21:26:21 +03:00
|
|
|
insert(num_dirs, name, icon);
|
2002-04-29 23:40:51 +04:00
|
|
|
#endif // WIN32 && !__CYGWIN__
|
2005-02-05 21:26:21 +03:00
|
|
|
} else if (filetype_ == FILES &&
|
|
|
|
fl_filename_match(files[i]->d_name, pattern_)) {
|
|
|
|
add(files[i]->d_name, icon);
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(files[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (num_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2001-09-29 18:38:59 +04:00
|
|
|
// 'Fl_File_Browser::filter()' - Set the filename filter.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2001-09-29 18:38:59 +04:00
|
|
|
Fl_File_Browser::filter(const char *pattern) // I - Pattern string
|
2001-08-02 01:24:49 +04:00
|
|
|
{
|
|
|
|
// If pattern is NULL set the pattern to "*"...
|
|
|
|
if (pattern)
|
|
|
|
pattern_ = pattern;
|
|
|
|
else
|
|
|
|
pattern_ = "*";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2005-02-05 21:26:21 +03:00
|
|
|
// End of "$Id$".
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|