openframeworks / openFrameworks

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

defaultName ofSystemSaveDialog and ofSystemLoadDialog(not working in Windows 10 #6336

Open Bk8 opened 5 years ago

Bk8 commented 5 years ago

The default name and messageName parameters on the ofSystemSaveDialog and ofSystemLoadDialog functions are not working, this is a really old bug (look links below). I don't know why is not solved, help please. I'm using Windows 10 and of_v0.10.1.

SoulRyder Apr '16 I found the ofSystemUtils.cpp doesn’t do anything on windows with the strings defaultname and messagename. It is solved.

https://forum.openframeworks.cc/t/cannot-use-common-language-runtime-support/22999

fakob Apr '14 Is this a bug on the windows side? At least it wasn’t there in OSX, but it is now on windows.

https://forum.openframeworks.cc/t/ofsystemsavedialog-defaultname-messagename-have-no-influence/15264

Bk8 commented 5 years ago

I made I small patch to be able to use it, I don't really know win 32 api but the variables sent to the function where unused

ofFileDialogResult ofSystemSaveDialog(string defaultName, string fileExt){

ofFileDialogResult results;

//----------------------------------------------------------------------------------------
//------------------------------------------------------------------------------       OSX
//----------------------------------------------------------------------------------------

ifdef TARGET_OSX

@autoreleasepool {
    NSSavePanel * saveDialog = [NSSavePanel savePanel];
    NSOpenGLContext *context = [NSOpenGLContext currentContext];
    [saveDialog setMessage:[NSString stringWithUTF8String:messageName.c_str()]];
    [saveDialog setNameFieldStringValue:[NSString stringWithUTF8String:defaultName.c_str()]];

    NSInteger buttonClicked = [saveDialog runModal];
    restoreAppWindowFocus();
    [context makeCurrentContext];

    if(buttonClicked == NSFileHandlingPanelOKButton){
        results.filePath = string([[[saveDialog URL] path] UTF8String]);
    }
}

endif

//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------
//------------------------------------------------------------------------------   windoze
//----------------------------------------------------------------------------------------

ifdef TARGET_WIN32

wchar_t fileName[MAX_PATH] = L"";

for(int i =0; i <defaultName.size();i++)
    fileName[i] = defaultName[i];

wchar_t ext[10] = L"";

for (int i = 0; i <fileExt.size(); i++)
    ext[i] = fileExt[i];

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 = ext;
ofn.lpstrDefExt = ext;  // 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

fearn-e commented 5 months ago

Seems to have never been fixed, just ran into this issue myself. Tried to cook up a dirty fix for my own project but I can't get past an allocation crash. If someone more familiar with the windows API than me could take a crack at a more stable/permanent fix to this it'd be very appreciated 🙏