Fix file dialog code

This commit is contained in:
Henrik Rydgard 2012-04-12 15:39:12 +02:00
parent 371c4db1ae
commit 3ca42c11a6
2 changed files with 81 additions and 71 deletions

View file

@ -1,8 +1,81 @@
#pragma once
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#include <string>
#include "file/dialog.h"
// For desktop operating systems only. Stubbed out on Android.
// Simplified as this will only be used in utilities / temp code.
// An false returned means cancel;
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
bool OpenFileDialog(const char *title, const char *extension, std::string *filename);
bool SaveFileDialog(const char *title, const char *extension, std::string *filename);
// These weird strings with zeroes in them can't be dealt with using normal string
// functions, so here we go - evil hackery.
char filter[256] = "XXX files\0*.XXX\0\0";
memcpy(filter, extension, 3);
memcpy(filter + 12, extension, 3);
of.lpstrFilter = filter;
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_FILEMUSTEXIST;
if (!GetOpenFileName(&of)) return false;
*filename = of.lpstrFile;
return true;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
// These weird strings with zeroes in them can't be dealt with using normal string
// functions, so here we go - evil hackery.
char filter[256] = "XXX files\0*.XXX\0\0";
memcpy(filter, extension, 3);
memcpy(filter + 12, extension, 3);
of.lpstrFilter = filter;
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
if (!GetSaveFileName(&of))
return false;
*filename = of.lpstrFile;
return true;
}
#else
#include <string>
#include "base/basictypes.h"
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for OpenFileDialog, not present on this platform.");
return false;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for SaveFileDialog, not present on this platform.");
return false;
}
#endif

View file

@ -1,73 +1,10 @@
#pragma once
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#pragma once
#include <string>
// For desktop operating systems only. Stubbed out on Android.
// Simplified as this will only be used in utilities / temp code.
// An false returned means cancel;
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
#include "file/dialog.h"
of.lpstrFilter = "All files (*.*)\0*.*\0\0";
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (!GetOpenFileName(&of)) return false;
*filename = of.lpstrFile;
return true;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
of.lpstrFilter = "All files (*.*)\0*.*\0\0";
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_HIDEREADONLY;
if (!GetSaveFileName(&of))
return false;
*filename = of.lpstrFile;
return true;
}
#else
#include <string>
#include "base/basictypes.h"
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for OpenFileDialog, not present on this platform.");
return false;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for SaveFileDialog, not present on this platform.");
return false;
}
#endif
bool OpenFileDialog(const char *title, const char *extension, std::string *filename);
bool SaveFileDialog(const char *title, const char *extension, std::string *filename);