00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CATApp.h"
00016 #include "CATWindow.h"
00017 #include "CATFileSystem.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 CATResult CATApp::OpenFileDialog( const CATString& title,
00033 std::vector<CATString>& filetypeList,
00034 CATString& returnPath,
00035 CATWindow* wnd)
00036 {
00037 OPENFILENAME ofn;
00038
00039
00040
00041 CATString filterString;
00042
00043 CATUInt32 i;
00044 CATUInt32 numEntries = (CATUInt32)filetypeList.size();
00045 for (i = 0; i < numEntries; i++)
00046 {
00047 CATString curString = filetypeList[i];
00048 filterString << curString;
00049 if (filterString.GetWChar(filterString.Length() - 1) != '|')
00050 filterString << "|";
00051 }
00052
00053
00054
00055 CATUInt32 filterBufLen = filterString.Length();
00056 CATWChar* filterBuf = filterString.GetUnicodeBuffer();
00057 for (i = 0; i < filterBufLen; i++)
00058 {
00059 if (filterBuf[i] == '|')
00060 {
00061 filterBuf[i] = 0;
00062 }
00063 }
00064
00065
00066 HWND parentWnd = 0;
00067
00068 if (wnd == 0)
00069 {
00070 parentWnd = ::GetActiveWindow();
00071 }
00072 else
00073 {
00074 parentWnd = wnd->OSGetWnd();
00075 }
00076
00077 CATString titleBufStr = title;
00078
00079
00080 memset(&ofn,0,sizeof(ofn));
00081 ofn.lStructSize = sizeof(OPENFILENAME);
00082 ofn.lpstrFilter = filterBuf;
00083 ofn.lpstrTitle = titleBufStr.GetUnicodeBuffer();
00084 ofn.lpstrFile = returnPath.GetUnicodeBuffer(_MAX_PATH+1);
00085 ofn.nMaxFile = _MAX_PATH;
00086 ofn.hwndOwner = parentWnd;
00087 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
00088
00089 BOOL dlgRes = ::GetOpenFileName(&ofn);
00090
00091
00092 filterString.ReleaseBuffer();
00093 titleBufStr.ReleaseBuffer();
00094 returnPath.ReleaseBuffer();
00095
00096 if (!dlgRes)
00097 {
00098 returnPath = L"";
00099 return CATRESULT(CAT_ERR_FILEOPEN_CANCELLED);
00100 }
00101
00102 return CAT_SUCCESS;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 CATResult CATApp::SaveFileDialog( const CATString& title,
00118 std::vector<CATString>& filetypeList,
00119 CATString& returnPath,
00120 CATWindow* wnd,
00121 bool promptOverwrite,
00122 const CATString& fileExtension)
00123 {
00124 OPENFILENAME ofn;
00125
00126
00127
00128 CATString filterString;
00129 CATUInt32 i;
00130 CATUInt32 numEntries = filetypeList.size();
00131 for (i = 0; i < numEntries; i++)
00132 {
00133 CATString curString = filetypeList[i];
00134 filterString << curString;
00135 if (filterString.GetWChar(filterString.Length() - 1) != '|')
00136 filterString << L"|";
00137 }
00138
00139
00140
00141 CATUInt32 filterBufLen = filterString.Length();
00142 CATWChar* filterBuf = filterString.GetUnicodeBuffer();
00143 for (i = 0; i < filterBufLen; i++)
00144 {
00145 if (filterBuf[i] == '|')
00146 {
00147 filterBuf[i] = 0;
00148 }
00149 }
00150
00151
00152 HWND parentWnd = 0;
00153 if (wnd == 0)
00154 {
00155 parentWnd = ::GetActiveWindow();
00156 }
00157 else
00158 {
00159 parentWnd = wnd->OSGetWnd();
00160 }
00161
00162 CATString titleBufStr = title;
00163
00164
00165 memset(&ofn,0,sizeof(ofn));
00166 ofn.lStructSize = sizeof(OPENFILENAME);
00167 ofn.lpstrFilter = filterBuf;
00168 ofn.lpstrTitle = titleBufStr.GetUnicodeBuffer();
00169 ofn.lpstrFile = returnPath.GetUnicodeBuffer(_MAX_PATH+1);
00170 ofn.nMaxFile = _MAX_PATH;
00171 ofn.hwndOwner = parentWnd;
00172 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
00173
00174
00175 CATString defExtStr;
00176 if (fileExtension.IsEmpty() == false)
00177 {
00178 defExtStr = fileExtension;
00179 ofn.lpstrDefExt = defExtStr.GetUnicodeBuffer();
00180 }
00181
00182 BOOL dlgRes = ::GetSaveFileName(&ofn);
00183
00184
00185 filterString.ReleaseBuffer();
00186 titleBufStr.ReleaseBuffer();
00187 returnPath.ReleaseBuffer();
00188
00189 if (fileExtension.IsEmpty() == false)
00190 defExtStr.ReleaseBuffer();
00191
00192 if (!dlgRes)
00193 {
00194 returnPath = "";
00195 return CATRESULT(CAT_ERR_FILESAVE_CANCELLED);
00196 }
00197
00198 return CAT_SUCCESS;
00199
00200 }
00201
00202
00203
00204
00205
00206
00207 void CATApp::OSOnAppCreate()
00208 {
00209
00210
00211
00212 CATWChar* buffer = fProgramPath.GetUnicodeBuffer(_MAX_PATH+1);
00213 if (buffer != 0)
00214 {
00215 memset(buffer,0,_MAX_PATH + 1);
00216
00217
00218 GetModuleFileName(fAppInstance,buffer,_MAX_PATH);
00219 fProgramPath.ReleaseBuffer();
00220 }
00221
00222
00223
00224
00225 CATTRACE("Reading application path from registry, if available...");
00226 CATString regPath = L"Software";
00227 regPath << '\\' << this->GetAppName();
00228 HKEY hKey = 0;
00229 if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER,regPath,0,KEY_READ,&hKey))
00230 {
00231 DWORD type = 0;
00232 DWORD size = 1024*2;
00233
00234 if (ERROR_SUCCESS != ::RegQueryValueEx(hKey,L"",0,&type,(LPBYTE)fBaseDir.GetUnicodeBuffer(size/2+2),&size))
00235 {
00236 CATTRACE("Error reading base directory key. Using defaults");
00237 }
00238
00239 type = 0;
00240 size = 1024*2;
00241
00242 if (ERROR_SUCCESS != ::RegQueryValueEx(hKey,L"Data_Dir",0,&type,(LPBYTE)fDataDir.GetUnicodeBuffer(size/2+2),&size))
00243 {
00244 CATTRACE("Error reading data directory key. Using defaults.");
00245 }
00246
00247 ::RegCloseKey(hKey);
00248 fBaseDir.ReleaseBuffer();
00249 fDataDir.ReleaseBuffer();
00250
00251 if (CATFAILED(fGlobalFileSystem->DirExists(fBaseDir)))
00252 {
00253
00254 fBaseDir = "";
00255 }
00256 }
00257
00258
00259
00260
00261 if (fBaseDir.IsEmpty())
00262 {
00263 CATString progName;
00264 fGlobalFileSystem->SplitPath(fProgramPath, fBaseDir, progName,true);
00265 }
00266
00267 if (fDataDir.IsEmpty())
00268 {
00269 fDataDir = fGlobalFileSystem->BuildPath(fBaseDir,L"Data",true);
00270 }
00271
00272 fGlobalFileSystem->EnsureTerminator(fBaseDir);
00273 fGlobalFileSystem->EnsureTerminator(fDataDir);
00274
00275 }