I wish to use the Fl_File_Chooser to select a directory path for use in my main code. The main code uses a button and callback to run the Chooser in cb_dirselect(). If I set the Chooser in DIRECTORY mode, the CREATE capability (which i need) is disabled. I can use CREATE mode together with filter “*.” to give a directory listing. However selecting a directory disables the “OK” return button. A work-around of typing any character in the “Filename” box activates the “OK” button and returns the directory to the parent window as desired.
I am trying to avoid this work-around by adding another button to the Fl_File_Chooser using add_extra() from the documentation but all my compilation attempts fail, as commented in the code snippet. I have also tried to add a button with Fl_Button, which compiles, but never shows an added button. This probably cannot access the Fl_File_Chooser window and I suspect is not likely to work. I think I need to use add_extra() but I do not understand the documentation, particulary the gr item:
Fl_Widget * Fl_File_Chooser::add_extra ( Fl_Widget * gr )
Code snippet:
// try to use Fl_File_Chooser with "add_extra()" to add another button widget and select a directory and return selection to the parent window
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Widget.H>
#include <unistd.h>
#include <iostream>
#include <string.h>
using namespace std;
std::string dir_path = "/home/user/data_files"; //initial path
void * cb_dirselect(Fl_Widget* w, void* inp) { //open file chooser
const char *ptrpath = ::dir_path.c_str(); // get pointer to initial path
{ Fl_File_Chooser* fchoose = new Fl_File_Chooser(ptrpath, "*.", Fl_File_Chooser::CREATE, "Select Directory");
fchoose->add_extra(Fl_Widget new extra_but); //FAILS -
// ERROR: expected primary-expression before ‘new’
// Fl_Widget * Fl_File_Chooser::add_extra (Fl_Widget * gr) //member function documentation
fchoose->show();
while(fchoose->shown())
{ Fl::wait(); }
if (fchoose->value() != NULL){
::dir_path = fchoose->directory(); //update with the new selected path
}
}
} // end callback - return to the parent window with new path
// the parent window has an Fl_Button to request a file_system browse via this callback
kgb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.