Author: bgbnbigben
Date: 2011-03-06 02:18:05 -0800 (Sun, 06 Mar 2011)
New Revision: 8507
Log:
Updated the FileChooser. Summary of the changes follow:
- RFE/STR #49 (yes, 49!) has been implemented. This allows the user to select
a specific sorting function
- Updated methods inside the FileChooser class.
This was to a) inline functions that didn't need to be in a separate file
b) add necessary APIs for the new sort menu (sort_visible()
functions, add/remove_sort_fn())
- Updated members inside the FileChooser class
Added static const char *sort_menu_label to allow a developer to change the
name of the sort menu
Added the sortButton pulldown menu and callbacks
- Added a new sorting function, fltk::modificationsort(). This takes two
const dirent*const* parameters and
returns 1 if the first parameter was modified AFTER the second, or 0
otherwise.
- Changed the File_Sort_F typedef to FileSortF, mostly to bring this in line
with a 2.x style API.
- Changed the default FileChooser sorting function to fltk::casenumericsort,
as
case-insensitive sorting seems to be the favoured option amongst the
Win32API/GNOME/everywhere I looked
Finally closed STR #1960.
Modified:
trunk/fltk/FileBrowser.h
trunk/fltk/FileChooser.h
trunk/fltk/filename.h
trunk/src/FileBrowser.cxx
trunk/src/FileChooser.cxx
trunk/src/FileChooser2.cxx
trunk/src/filename_isdir.cxx
trunk/src/filename_list.cxx
Modified: trunk/fltk/FileBrowser.h
===================================================================
--- trunk/fltk/FileBrowser.h 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/fltk/FileBrowser.h 2011-03-06 10:18:05 UTC (rev 8507)
@@ -63,7 +63,7 @@
void filter(const char *pattern);
const char *filter() const { return (pattern_); };
- int load(const char *directory, File_Sort_F *sort = (File_Sort_F*)
fltk::numericsort);
+ int load(const char *directory, FileSortF *sort = (FileSortF*)
fltk::numericsort);
float textsize() const { return (Browser::textsize()); };
void textsize(float s) { Browser::textsize(s); icon_size_ =
(uchar)(3 * s / 2); };
Modified: trunk/fltk/FileChooser.h
===================================================================
--- trunk/fltk/FileChooser.h 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/fltk/FileChooser.h 2011-03-06 10:18:05 UTC (rev 8507)
@@ -61,6 +61,7 @@
void showChoiceCB();
void update_favorites();
void update_preview();
+ void update_sort();
int favorites_showing;
void activate_okButton_if_file();
void directory(const char *d, bool);
@@ -150,54 +151,64 @@
inline void cb_favOkButton_i(fltk::ReturnButton*, void*);
static void cb_favOkButton(fltk::ReturnButton*, void*);
+ // Sort button
+ fltk::PopupMenu *sortButton;
+ void cb_sortButton_i(fltk::PopupMenu*, void*);
+ static void cb_sortButton(fltk::PopupMenu*, void*);
+
public:
enum { SINGLE = 0, MULTI = 1, CREATE = 2, DIRECTORY = 4 };
FileChooser(const char *d, const char *p, int t, const char *title);
~FileChooser();
- void callback(void (*cb)(FileChooser *, void *), void *d = 0);
- void color(Color c);
- Color color();
+ inline void add_sort_fn(const char* label, FileSortF func) {
sortButton->add(label, (void*)func); }
+ inline void callback(void (*cb)(FileChooser *, void *), void *d = 0) {
callback_ = cb, data_ = d; }
+ inline void color(Color c) { fileList->color(c); }
+ inline Color color() const { return fileList->color(); }
int count();
void directory(const char *d);
- char * directory();
+ inline char * directory() const { return (char*)directory_ ; }
+ bool exec(Window* p, bool grab);
+ void favorites(int e);
+ inline int favorites() const { return favorites_showing; }
void filter(const char *p);
- const char * filter();
- int filter_value();
+ inline const char * filter() const { return fileList->filter(); }
+ inline int filter_value() const { return showChoice->value(); }
void filter_value(int f);
- void hide();
- void icon_size(uchar s);
- uchar icon_size();
- void label(const char *l);
- const char * label();
+ inline void hide() { return window->hide(); }
+ void icon_size(uchar s) { fileList->icon_size(s); }
+ inline uchar icon_size() const { return (uchar)fileList->icon_size(); }
+ inline void label(const char *l) { window->label(l); }
+ inline const char * label() const { return window->label(); }
void ok_label(const char *l);
- const char * ok_label();
+ inline const char * ok_label() const { return okButton->label(); }
void preview(bool e);
- int preview() const { return previewButton->value(); };
+ inline int preview() const { return previewButton->value(); };
+ inline void remove_sort_fn(const char* label) { sortButton->remove(label); }
void rescan();
void show();
void show(int x, int y);
- bool exec(Window* p, bool grab);
- int shown();
- void textcolor(Color c);
- Color textcolor();
- void textfont(Font* f);
- Font* textfont();
- void textsize(float s);
- float textsize();
+ inline int shown() const { return window->shown(); }
+ void sort_visible(int e);
+ int sort_visible() const;
+ inline void textcolor(Color c) { fileList->textcolor(c); }
+ inline Color textcolor() const { return fileList->textcolor(); }
+ inline void textfont(Font* f) { fileList->textfont(f); }
+ inline Font* textfont() const { return fileList->textfont(); }
+ inline void textsize(float s) { fileList->textsize(s); }
+ inline float textsize() const { return fileList->textsize(); }
void type(int t);
- int type();
- void * user_data() const;
- void user_data(void *d);
+ inline int type() const { return type_; }
+ inline void * user_data() const { return data_; }
+ inline void user_data(void *d) { data_ = d; }
const char *value(int f = 1);
void value(const char *filename);
- int visible();
- void favorites(int e);
- int favorites() const;
+ inline int visible() const { return window->visible(); }
static const char *add_favorites_label;
static const char *all_files_label;
static const char *custom_filter_label;
static const char *existing_file_label;
static const char *favorites_label;
+ static const char *sort_menu_label;
static const char *filename_label;
static const char *filesystems_label;
static const char *manage_favorites_label;
@@ -206,7 +217,7 @@
static const char *preview_label;
static const char *save_label;
static const char *show_label;
- static File_Sort_F *sort;
+ static FileSortF *sort;
fltk::Button *newButton;
fltk::CheckButton *previewButton;
Modified: trunk/fltk/filename.h
===================================================================
--- trunk/fltk/filename.h 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/fltk/filename.h 2011-03-06 10:18:05 UTC (rev 8507)
@@ -130,12 +130,13 @@
FL_API FL_FILESIZE_T filename_size(const char *); // return size of file
FL_API long int filename_mtime(const char *); // return modification time
-typedef int (File_Sort_F)(const dirent*const*, const dirent*const*);
+typedef int (FileSortF)(const dirent*const*, const dirent*const*);
FL_API int alphasort(const dirent*const*, const dirent*const*);
FL_API int casealphasort(const dirent*const*, const dirent*const*);
FL_API int casenumericsort(const dirent*const*, const dirent*const*);
FL_API int numericsort(const dirent*const*, const dirent*const*);
-FL_API int filename_list(const char *d, dirent ***list, File_Sort_F *sort);
+FL_API int modificationsort(const dirent*const*, const dirent*const*);
+FL_API int filename_list(const char *d, dirent ***list, FileSortF *sort);
FL_API int filename_list(const char *d, dirent ***list); // uses numericsort
//@}
Modified: trunk/src/FileBrowser.cxx
===================================================================
--- trunk/src/FileBrowser.cxx 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/src/FileBrowser.cxx 2011-03-06 10:18:05 UTC (rev 8507)
@@ -103,7 +103,7 @@
int // O - Number of files loaded
FileBrowser::load(const char *directory,// I - Directory to load
- File_Sort_F *sort) // I - Sort function to use
+ FileSortF *sort) // I - Sort function to use
{
int i; // Looping var
int num_files; // Number of files in directory
Modified: trunk/src/FileChooser.cxx
===================================================================
--- trunk/src/FileChooser.cxx 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/src/FileChooser.cxx 2011-03-06 10:18:05 UTC (rev 8507)
@@ -52,10 +52,20 @@
inline void FileChooser::cb_favoritesButton_i(fltk::PopupMenu*, void*) {
favoritesButtonCB();
}
+
void FileChooser::cb_favoritesButton(fltk::PopupMenu* o, void* v) {
((FileChooser*)(o->parent()->parent()->user_data()))->cb_favoritesButton_i(o,v);
}
+void FileChooser::cb_sortButton_i(fltk::PopupMenu* o, void* v) {
+ FileChooser::sort = (FileSortF*)o->item()->user_data();
+ update_sort();
+}
+
+void FileChooser::cb_sortButton(fltk::PopupMenu* o, void* v) {
+
((FileChooser*)(o->parent()->parent()->parent()->user_data()))->cb_sortButton_i(o,v);
+}
+
inline void FileChooser::cb_newButton_i(fltk::Button*, void*) {
newdir();
}
@@ -205,7 +215,7 @@
o->labelsize(14);
o->callback((fltk::Callback*)cb_fileList);
((fltk::Window*)(o->parent()->parent()))->hotspot(o);
- fileList->type(0);fileList->when(fltk::WHEN_CHANGED);
+ fileList->type(0);fileList->when(fltk::WHEN_CHANGED);
}
{fltk::InvisibleBox* o = previewBox = new fltk::InvisibleBox(295, 0,
175, 225, "?");
o->set_vertical();
@@ -233,7 +243,12 @@
o->callback((fltk::Callback*)cb_showHiddenButton);
o->value(fileList->show_hidden() ? 1 : 0);
}
- new fltk::InvisibleBox(180, 0, 290, 20);
+ {fltk::PopupMenu* o = sortButton = new fltk::PopupMenu(270, 0, 200,
20, "Sort method");
+ o->callback((fltk::Callback*)cb_sortButton);
+ o->align(fltk::ALIGN_CENTER|fltk::ALIGN_INSIDE);
+ sortButton->label(sort_menu_label);
+ }
+ //new fltk::InvisibleBox(180, 0, 290, 20);
o->end();
}
{fltk::FileInput* o = fileName = new fltk::FileInput(105, 25, 365, 35);
@@ -327,6 +342,7 @@
type(t);
filter(p);
update_favorites();
+ sortButton->hide();
value(d);
type(t);
int e;
@@ -340,56 +356,11 @@
delete favWindow;
}
-void FileChooser::callback(void (*cb)(FileChooser *, void *), void *d ) {
- callback_ = cb;
- data_ = d;
-}
-
-void FileChooser::color(Color c) {
- fileList->color(c);
-}
-
-Color FileChooser::color() {
- return (fileList->color());
-}
-
-char * FileChooser::directory() {
- return directory_;
-}
-
-const char * FileChooser::filter() {
- return (fileList->filter());
-}
-
-int FileChooser::filter_value() {
- return showChoice->value();
-}
-
void FileChooser::filter_value(int f) {
showChoice->value(f);
showChoiceCB();
}
-void FileChooser::hide() {
- window->hide();
-}
-
-void FileChooser::icon_size(uchar s) {
- fileList->icon_size(s);
-}
-
-uchar FileChooser::icon_size() {
- return ((uchar) fileList->icon_size());
-}
-
-void FileChooser::label(const char *l) {
- window->label(l);
-}
-
-const char * FileChooser::label() {
- return (window->label());
-}
-
void FileChooser::ok_label(const char *l) {
okButton->label(l);
int w=0, h=0;
@@ -399,10 +370,6 @@
okButton->parent()->init_sizes();
}
-const char * FileChooser::ok_label() {
- return (okButton->label());
-}
-
void FileChooser::show() {
window->hotspot(fileList);
window->show();
@@ -428,34 +395,6 @@
return window->exec(p, grab);
}
-int FileChooser::shown() {
- return window->shown();
-}
-
-void FileChooser::textcolor(Color c) {
- fileList->textcolor(c);
-}
-
-Color FileChooser::textcolor() {
- return (fileList->textcolor());
-}
-
-void FileChooser::textfont(Font* f) {
- fileList->textfont(f);
-}
-
-Font* FileChooser::textfont() {
- return (fileList->textfont());
-}
-
-void FileChooser::textsize(float s) {
- fileList->textsize(s);
-}
-
-float FileChooser::textsize() {
- return (fileList->textsize());
-}
-
void FileChooser::type(int t) {
type_ = t;
if (t & MULTI)
@@ -472,30 +411,10 @@
fileList->filetype(FileBrowser::FILES);
}
-int FileChooser::type() {
- return (type_);
-}
-
-void * FileChooser::user_data() const {
- return (data_);
-}
-
-void FileChooser::user_data(void *d) {
- data_ = d;
-}
-
-int FileChooser::visible() {
- return window->visible();
-}
-
void FileChooser::favorites(int e) {
favorites_showing = e;
update_favorites();
}
-int FileChooser::favorites() const {
- return favorites_showing;
-}
-
// End of "$Id$"
Modified: trunk/src/FileChooser2.cxx
===================================================================
--- trunk/src/FileChooser2.cxx 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/src/FileChooser2.cxx 2011-03-06 10:18:05 UTC (rev 8507)
@@ -95,6 +95,7 @@
const char *FileChooser::custom_filter_label = "Custom Filter";
const char *FileChooser::existing_file_label = "Please choose an existing
file!";
const char *FileChooser::favorites_label = "Favorites";
+const char *FileChooser::sort_menu_label = "Sort method";
const char *FileChooser::filename_label = "Filename:";
#ifdef WIN32
const char *FileChooser::filesystems_label = "My Computer";
@@ -107,7 +108,7 @@
const char *FileChooser::preview_label = "Preview";
const char *FileChooser::save_label = "Save";
const char *FileChooser::show_label = "Show:";
-File_Sort_F *FileChooser::sort = fltk::numericsort;
+FileSortF *FileChooser::sort = fltk::casenumericsort;
//
@@ -232,6 +233,7 @@
// Strip trailing "."...
dirptr[1] = '\0';
}
+
}
else
directory_[0] = '\0';
@@ -250,6 +252,7 @@
if (!(type_ & CREATE) && !fltk::filename_isfile(pathname))
*dirptr = 0;
}
+
fileName->text(pathname);
}
@@ -430,7 +433,6 @@
}
}
-
/**
Handle clicks (and double-clicks) in the FileBrowser.
@@ -501,6 +503,12 @@
// Strip any trailing slash from the directory name...
if (*filename == '/') *filename = '\0';
+ filename = strrchr((char*)fileName->text(), '/');
+ puts(pathname);
+ puts((filename&&filename+1)? filename+1:"\0");
+ sprintf(pathname, "%s/%s", pathname, (filename && filename+1) ? filename+1
: '\0');
+ if (*filename == '/') *filename = '\0';
+
fileName->value(pathname);
// Update the preview box...
@@ -1092,7 +1100,37 @@
previewBox->redraw();
}
+/** Update the sorting method
+ This function completely re-loads the displayed FileList.
+ This may be slow for large directories as it rescans completely
+ \todo Only resort the contents of the directory, not rescan
+*/
+
+void FileChooser::update_sort() {
+ fileList->load(directory_, sort);
+};
+
+/** Toggle the visibility of the sort menu
+
+ \param e determines whether or not to show the menu
+*/
+
+void FileChooser::sort_visible(int e) {
+ if (e)
+ sortButton->show();
+ else sortButton->hide();
+ sortButton->relayout();
+}
+
+/** Tests the visility of the sort menu
+
+ \return whether or not the sort menu is visible.
+*/
+int FileChooser::sort_visible() const {
+ return (int)sortButton->visible();
+}
+
/**
Return a selected filename.
Modified: trunk/src/filename_isdir.cxx
===================================================================
--- trunk/src/filename_isdir.cxx 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/src/filename_isdir.cxx 2011-03-06 10:18:05 UTC (rev 8507)
@@ -122,6 +122,12 @@
return (long) last_stat.st_ctime;
}
+/**
+ Sorts two files based on their modification date.
+*/
+int fltk::modificationsort(const dirent*const* a, const dirent*const* b) {
+ return fltk::filename_mtime((*a)->d_name) <
fltk::filename_mtime((*b)->d_name);
+}
//
// End of "$Id$".
//
Modified: trunk/src/filename_list.cxx
===================================================================
--- trunk/src/filename_list.cxx 2011-03-04 17:51:22 UTC (rev 8506)
+++ trunk/src/filename_list.cxx 2011-03-06 10:18:05 UTC (rev 8507)
@@ -54,7 +54,7 @@
}
int fltk::filename_list(const char *d, dirent ***list,
- File_Sort_F *sort) {
+ FileSortF *sort) {
// Nobody defines the comparison function prototype correctly!
// It should be "const dirent* const*". I don't seem to be able to
// do this even for our own internal version because some compilers
_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit