FLTK 2 - Open and save files on Windows OS

Submitted by lnlanner on Sat, 09/27/2008 - 04:49

Open / save files on Win Os using FLTK.  

OPENFILENAME ofn;    // global
void Browse_CB(Widget*, void*) {
// OPEN WINDOWS FILE BROWSER
// The following is WIN32-specific code, and therefore not pretty and 'non-fltk-ish'; )
//
if ( ofn.lpstrFile       ) { delete [] ofn.lpstrFile; }
if ( ofn.lpstrInitialDir ) { delete [] ofn.lpstrInitialDir; }
memset((void*)&ofn, 0, sizeof(ofn));
ng code foundofn.lStructSize = sizeof(OPENFILENAME);
ofn.Flags       |= OFN_NOVALIDATE;          // prevent disabling of front slashes
ofn.Flags       |= OFN_HIDEREADONLY;        // hide readonly flag
ofn.Flags       |= OFN_EXPLORER;            // use 'newer' explorer windows
ofn.Flags       |= OFN_ENABLESIZING;        // allow window to be resized
ofn.Flags       |= OFN_NOCHANGEDIR;         // prevent dialog for messing up the cwd
ofn.lpstrFilter = "Lilypond Files (*.ly)\0*.ly\0All Files (*.*)\0*.*\0";
ofn.nMaxFile     = 4096-1;
ofn.lpstrFile    = new char[4096];
ofn.lpstrFile[0] = 0;
ofn.hwndOwner    = GetForegroundWindow();
ofn.lpstrTitle   = "Open Lilypond File";
const char *newfile = ofn.lpstrFile; 
int err = GetOpenFileName(&ofn);
if ( err == 0 ) {
err = CommDlgExtendedError();           // extended error check
if ( err == 0 ) return;                 // user hit 'cancel'
fprintf(stderr, "CommDlgExtendedError() code=%d", err);
return;
}
//printf("User picked '%s'\n", ofn.lpstrFile);
load_file(newfile, -1);
}

/*////////////////////////////////////////////////////////////*/

/*Save file*/

OPENFILENAME sfn;    // global
void save_as_win()
{
// OPEN WINDOWS FILE BROWSER
// The following is WIN32-specific code, and therefore not pretty and 'non-fltk-ish'; )
//
if ( sfn.lpstrFile       ) { delete [] sfn.lpstrFile; }
if ( sfn.lpstrInitialDir ) { delete [] sfn.lpstrInitialDir; }
memset((void*)&sfn, 0, sizeof(sfn));
sfn.lStructSize = sizeof(OPENFILENAME);
sfn.lpstrFilter = "Lilypond Files (*.ly)\0*.ly\0All Files (*.*)\0*.*\0";
sfn.nMaxFile     = 4096-1;
sfn.lpstrFile    = new char[4096];
sfn.lpstrFile[0] = 0;
sfn.hwndOwner    = GetForegroundWindow();
sfn.lpstrTitle   = "Open some file";
const char *newfile = sfn.lpstrFile;
int err = GetSaveFileName(&sfn);
if ( err == 0 ) {
err = CommDlgExtendedError();           // extended error check
if ( err == 0 ) return;                 // user hit 'cancel'
fprintf(stderr, "CommDlgExtendedError() code=%d", err);
return;
}
//printf("User picked '%s'\n", ofn.lpstrFile);
save_file(newfile);
}

The code above has been taken by the Greg Ercolano's Home Page. Underlined strings are those I added.










 

© 2012 Luigi Negretti Lanner - All rights deserved. m