/*
* FileDialog.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL++2000
// 2000.02.18
#pragma once
#include <sol\Application.h>
#include <sol\CommonDialog.h>
#include <direct.h>
namespace SOL {
class FileDialog :public CommonDialog {
private:
DWORD flags;
TCHAR directory[_MAX_PATH];
TCHAR filename[_MAX_PATH];
TCHAR fileTitle[_MAX_PATH];
// BOOL fileSave;
int accessMode;
OPENFILENAME ofn;
public:
static const int OPEN = 0;
static const int SAVE = 1;
public:
/**
* Constructor
*/
FileDialog(): CommonDialog() {
filename[0] = Zero;
fileTitle[0] = Zero;
directory[0] = Zero;
memset(&ofn, 0, sizeof(OPENFILENAME));
}
public:
/**
* Constructor
*/
FileDialog(View* parent, const TCHAR* caption, Args& args)
:CommonDialog(parent, &ofn)
{
filename[0] = Zero;
fileTitle[0] = Zero;
directory[0] = Zero;
memset(&ofn, 0, sizeof(OPENFILENAME));
_getcwd(directory, _MAX_PATH);
ofn.lpstrInitialDir= directory;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = _T("All files (*.*) \0 *.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrFile = filename;
ofn.nMaxFile = _MAX_PATH;
ofn.lpstrFileTitle = fileTitle;
ofn.nMaxFileTitle = _MAX_PATH;
ofn.lpstrTitle = caption;
ofn.lpstrDefExt = (TCHAR*)args.get(XmNextension);
if(parent) {
ofn.hwndOwner = parent -> getWindow();
}
ofn.hInstance = GetModuleHandle(NULL);
//applet.getInstance();//Application::getInstance();
//2009/11/04
ofn.lCustData = (long)this;
flags = NULL;
// FileOpen in default.
accessMode = OPEN;
setValues(args);
}
public:
~FileDialog()
{
}
public:
virtual Boolean create(View* parent, const TCHAR* caption, Args& args)
{
Boolean rc = CommonDialog::create(parent, &ofn);
filename[0] = Zero;
fileTitle[0] = Zero;
directory[0] = Zero;
memset(&ofn, 0, sizeof(OPENFILENAME));
_getcwd(directory, _MAX_PATH);
ofn.lpstrInitialDir= directory;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = _T("All files (*.*) \0 *.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrFile = filename;
ofn.nMaxFile = _MAX_PATH;
ofn.lpstrFileTitle = fileTitle;
ofn.nMaxFileTitle = _MAX_PATH;
ofn.lpstrTitle = caption;
ofn.lpstrDefExt = (TCHAR*)args.get(XmNextension);
if(parent) {
ofn.hwndOwner = parent->getWindow();
}
ofn.hInstance = GetModuleHandle(NULL);
//applet.getInstance();//Application::getInstance();
//2009/11/04
ofn.lCustData = (long)this;
flags = NULL;
// FileOpen in default.
accessMode = OPEN;
setValues(args);
return rc;
}
public:
int open()
{
filename[0] = Zero;
fileTitle[0] = Zero;
ofn.Flags = flags|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
return ::GetOpenFileName(&ofn);
}
public:
int save()
{
filename[0] = Zero;
fileTitle[0] = Zero;
ofn.Flags = flags|OFN_OVERWRITEPROMPT;
return ::GetSaveFileName(&ofn);
}
public:
void getValues(Args& args)
{
ulong* val = NULL;
int num = args.getCount();
Arg* arg = args.getArgList();
for(int i = 0; i<num; i++) {
const TCHAR* name = arg[i].name;
//<modified date="2000.01.09">
val = (ulong*)arg[i].value;
//</modified>
if (name == XmNflags) {
*val = (ulong)flags;
continue;
}
if (name == XmNdirectory) {
*val = (ulong)directory;
continue;
}
if (name == XmNfilter) {
*val = (ulong)ofn.lpstrFilter;
continue;
}
if (name == XmNfileName) {
*val = (ulong)filename;
continue;
}
if (name == XmNfileTitle) {
*val = (ulong)fileTitle;
continue;
}
}
}
public:
void setValues(Args& args)
{
ulong val;
if (args.get(XmNaccessMode, &val)) {
accessMode = (int)val;
}
if (args.get(XmNflags, &val)) {
flags = (DWORD)val;
}
if (args.get(XmNhook, &val)) {
flags |= OFN_ENABLEHOOK;
//2009/11/01
ofn.lpfnHook = (HOOKFUN)hookProc;
}
if (args.get(XmNfileName, &val)) {
//2008/09/13 sizeof._MAX_PATH
strcpy_s(filename, _MAX_PATH, (TCHAR*)val);
}
if (args.get(XmNdirectory, &val)) {
//2008/09/13 sizeof._MAX_PATH
strcpy_s(directory, _MAX_PATH, (TCHAR*)val);
ofn.lpstrInitialDir= directory;
}
if (args.get(XmNfilter, &val)) {
ofn.lpstrFilter = (TCHAR*)val;
}
if (args.get(XmNfilterIndex, &val)) {
ofn.nFilterIndex = (int)val;
}
if (args.get(XmNdialogTitle, &val)) {
ofn.lpstrTitle = (TCHAR*)val;
}
if (args.get(XmNtemplateName, &val)) {
flags |= OFN_ENABLETEMPLATE;
ofn.lpTemplateName = (TCHAR*)val;
}
}
public:
// Modal
void popup(Action& action)
{
int result = NULL;
if(accessMode == SAVE) {
result = save();
}
else {
result = open();
}
action.setResult(result);
}
public:
void setCaption(const TCHAR* caption)
{
ofn.lpstrTitle = (TCHAR*)caption;
}
public:
TCHAR* getFileName() {
return filename;
}
public:
TCHAR* getFileTitle() {
return fileTitle;
}
};
}
|