openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.95k stars 2.55k forks source link

ofSystemSaveDialog changes current directory on Windows only #2599

Open igiso opened 11 years ago

igiso commented 11 years ago

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

bilderbuchi commented 11 years ago

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

lewezero commented 10 years ago

adding OFN_NOCHANGEDIR to ofn.Flags should help

felipemaion commented 2 years ago

@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)

lewezero commented 2 years ago

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;