1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
//===============================================================
// vfilesel.cxx - vFileSelect class functions - Windows
//
// Copyright (C) 1995,1996, 1997, 1998 Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================
#include <v/vwin32.h> // for Win 32 stuff
#include <v/vfilesel.h> // our header
#include <v/vapp.h>
#include <v/vbasewin.h>
// Define static data of the class
static char* defaultFilter = // The default filter list
"All files(*.*)|*.*|";
//===================>>> vFileSelect::vFileSelect <<<=======================
vFileSelect::vFileSelect(vBaseWindow* bw, VCONST char* /* title */) // constructor
{
_parentHWND = bw->winHwnd(); // track parent's HWND
init();
}
//===================>>> vFileSelect::vFileSelect <<<=======================
vFileSelect::vFileSelect(vApp* aw, VCONST char* /* title */)
{
_parentHWND = aw->winHwnd(); // track parent's HWND
init();
}
//===================>>> vFileSelect::init <<<=======================
void vFileSelect::init()
{
memset(&ofn,0,sizeof(OPENFILENAME)); // zap entire thing
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = _parentHWND;
}
//======================>>> vFileSelect::FileSelect <<<=======================
int vFileSelect::FileSelect(const char* msg, char* filename,
const int maxlen, char** filter, int& filterIndex)
{
// Show the file selection dialog.
// returns 0 on cancel, 1 otherwise
char fp[256];
int filterI = filterIndex;
// filename[0] = 0;
buildFilter(fp,filter,filterI); // build a proper filter string
ofn.lpstrFilter = fp;
ofn.nFilterIndex = filterI;
ofn.lpstrFile = filename;
ofn.nMaxFile = maxlen;
ofn.lpstrTitle = msg;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY;
if (::GetOpenFileName(&ofn)) // Call Windows dialog
{
strcpy(filename, (char*)ofn.lpstrFile);
filterIndex = ofn.nFilterIndex - 1;
return 1;
}
return 0;
}
//======================>>> vFileSelect::FileSelectSave <<<=======================
int vFileSelect::FileSelectSave(const char* msg, char* filename,
const int maxlen, char** filter, int& filterIndex, const int findDir)
{
// Show the file selection dialog.
// returns 0 on cancel, 1 otherwise
char fp[256];
int filterI = filterIndex;
//filename[0] = 0;
buildFilter(fp,filter,filterI); // build a proper filter string
ofn.lpstrFilter = fp;
ofn.nFilterIndex = filterI;
ofn.lpstrFile = filename;
ofn.nMaxFile = maxlen;
ofn.lpstrTitle = msg;
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
if (findDir)
ofn.Flags |= OFN_NOTESTFILECREATE; // cheap work around
if (::GetSaveFileName(&ofn)) // Call Windows dialog
{
strcpy(filename, (char*)ofn.lpstrFile);
if (findDir) // strip off file name
{
int len = strlen(filename);
for (int ix = len-1 ; ix > 0 ; --ix)
{
if (filename[ix] == '\\')
{
filename[ix] = 0; // change last \\ to 0
break;
}
}
}
filterIndex = ofn.nFilterIndex - 1;
return 1;
}
filename[0] = 0;
return 0;
}
//======================>>> vFileSelect::buildFilter <<<=======================
void vFileSelect::buildFilter(char* fbuff, char** filter, int& fi)
{
if (filter == 0 || *filter[0] == 0)
{
strcpy(fbuff,defaultFilter);
fi = 1;
}
else // copy the filter over
{
*fbuff = 0;
for (int ix = 0 ; filter[ix] != 0 && *filter[ix] != 0 ; ++ix)
{
if (strlen(filter[ix])*2 + strlen(fbuff) > 255)
break; // safety bailout
// Since the V model doesn't have the description/filter
// organization, we will simply use the filter as the name
strcat(fbuff, filter[ix]); strcat(fbuff,"|");
strcat(fbuff, filter[ix]); strcat(fbuff,"|");
}
fi = fi + 1; // 1 based, not 0 based
}
// Fixup the filter now -- replacing | with 0 makes it what Windows needs
for (char *cp = fbuff ; *cp ; ++cp)
{
if (*cp == '|')
*cp = 0;
}
}
|