#include #include #include #include #include #include // GLOBALS Fl_Input *G_inptitle = NULL; Fl_Input *G_inpfilter = NULL; Fl_Input *G_inpdirname = NULL; Fl_Choice *G_chotype = NULL; Fl_File_Chooser *G_chooser = NULL; int GetType() { switch (G_chotype->value()) { case 0: return(-1); case 1: return(Fl_File_Chooser::SINGLE); case 2: return(Fl_File_Chooser::MULTI); case 3: return(Fl_File_Chooser::CREATE); case 4: return(Fl_File_Chooser::DIRECTORY); case 5: return(Fl_File_Chooser::DIRECTORY|Fl_File_Chooser::MULTI); default: return(-1); } } void Exit_CB(Fl_Widget*w, void*data) { exit(0); } void Butt_CB(Fl_Widget*w, void*data) { const char *title = G_inptitle->value(); const char *filter = G_inpfilter->value(); const char *directory = G_inpdirname->value(); int type = GetType(); if ( ! G_chooser ) { G_chooser = new Fl_File_Chooser("", "", Fl_File_Chooser::SINGLE, ""); } G_chooser->directory(directory[0]?directory:NULL); G_chooser->filter(filter[0]?filter:NULL); G_chooser->type(type>=0?type:Fl_File_Chooser::SINGLE); G_chooser->label(title[0]?title:NULL); G_chooser->show(); // Block until user picks something. // (The other way to do this is to use a callback()) // while(G_chooser->shown()) { Fl::wait(); } // Print the results if ( G_chooser->value() == NULL ) { fprintf(stderr, "(User hit 'Cancel')\n"); return; } fprintf(stderr, "DIRECTORY: '%s'\n", G_chooser->directory()); fprintf(stderr, " VALUE: '%s'\n", G_chooser->value()); fprintf(stderr, " COUNT: %d files selected\n", G_chooser->count()); if ( G_chooser->count() > 1 ) { for ( int t=1; t<=G_chooser->count(); t++ ) { fprintf(stderr, " VALUE[%d]: '%s'\n", t, G_chooser->value(t)); } } fprintf(stderr, "--------------------\n"); } int main() { Fl_Window *win = new Fl_Window(300, 180, "Test Fl_File_Chooser"); int y = 10; G_inptitle = new Fl_Input(80,y,200,25,"Title"); G_inptitle->value(""); y += 30; G_inpfilter = new Fl_Input(80,y,200,25,"Filter"); G_inpfilter->value(""); y += 30; G_inpdirname = new Fl_Input(80,y,200,25,"Filename"); G_inpdirname->value(""); y += 30; G_chotype = new Fl_Choice(80,y,130,25,"Type"); G_chotype->add("default"); G_chotype->add("Single"); G_chotype->add("Multi"); G_chotype->add("Create"); G_chotype->add("Directory"); G_chotype->add("Multi-Directory"); G_chotype->value(0); y += 30; Fl_Button *ebut = new Fl_Button(80,win->h()-25-20,80,25,"Exit"); ebut->callback(Exit_CB); Fl_Button *cbut = new Fl_Button(win->w()-80-20,win->h()-25-20,80,25,"Chooser.."); cbut->callback(Butt_CB); win->show(); return(Fl::run()); }