openFrameworks icon indicating copy to clipboard operation
openFrameworks copied to clipboard

ofSystemSaveDialog changes current directory on Windows only

Open igiso opened this issue 12 years ago • 4 comments

Hi all, I noticed that when we use the ofSystemSaveDialog() function on PC

it changes the getCurrentWorkingDirectory() and this breaks the path directory in OF

So when you try to load an image or save an xml OF can't find the path because it looks to the directory you saved your files.

This only occurs under windows.

on Mac getCurrentWorkingDirectory() points to the same spot after the dialog

///

The problem is inside the ofSystemSaveDialog in the ofSystemUtils.cpp

#ifdef TARGET_WIN32
    wchar_t fileName[MAX_PATH] = L"";
    char * extension;
    OPENFILENAMEW ofn;
    memset(&ofn, 0, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    HWND hwnd = WindowFromDC(wglGetCurrentDC());
    ofn.hwndOwner = hwnd;
    ofn.hInstance = GetModuleHandle(0);
    ofn.nMaxFileTitle = 31;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = L"All Files (*.*)\0*.*\0";
    ofn.lpstrDefExt = L"";  // we could do .rxml here?
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
    ofn.lpstrTitle = L"Select Output File";

    if (GetSaveFileNameW(&ofn)){
        results.filePath = convertWideToNarrow(fileName);
    }

#endif

the way to deal with this now is after the

: saveFileResult.bSuccess or before you need to load or save something in data again.

you put once: ofSetDataPathRoot(DEFAULT_DIRECTORY);

the DEFAULT_DIRECTORY is a string I initialize in setup with

DEFAULT_DIRECTORY = ofToDataPath("",true);

Not sure how we can solve this from within the function

related forum post:

http://forum.openframeworks.cc/index.php/topic,13382.msg57346.html#msg57346

igiso avatar Oct 08 '13 08:10 igiso

Thank you. I edited the title a bit to describe the problem better.

bilderbuchi avatar Oct 08 '13 09:10 bilderbuchi

adding OFN_NOCHANGEDIR to ofn.Flags should help

lewezero avatar May 22 '14 16:05 lewezero

@lewezero can you help a little bit more? How to add OFN_NOCHANGEDIR to ofn.Flags? I'm using Visual Studio (2015-2022 was able to compile on both, but using 2015 setup)

felipemaion avatar Aug 10 '22 23:08 felipemaion

Well, if I recall correctly what I meant back then, the following line in the snippet from ofSystemUtils.cpp posted above by @igiso: ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;

should instead read: ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;

lewezero avatar Aug 11 '22 17:08 lewezero