Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CATGUI/CATApp_Win32.cpp Source File

CATApp_Win32.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file  CATApp_Win32.cpp
00003 /// \brief Application parent class - win32-specific functions
00004 /// \ingroup CATGUI
00005 /// 
00006 /// Copyright (c) 2003-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-25 05:11:25 -0600 (Fri, 25 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //---------------------------------------------------------------------------
00014 
00015 #include "CATApp.h"
00016 #include "CATWindow.h"
00017 #include "CATFileSystem.h"
00018 
00019 
00020 //---------------------------------------------------------------------------
00021 // OpenFileDialog() displays an open file dialog and returns the
00022 // selected file path, if any.
00023 //
00024 // \param title - title string
00025 // \param filetypeList - list of file type / extension pairs.
00026 //        Format should be: "Description of file (*.ext)|*.ext|"
00027 //        Any other format may cause unexpected results.
00028 // \param returnPath - ref to string to fill with received file path, if any.
00029 //        when called, this is also used as the default file name.
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     // Build up the filter / mask strings
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     // Convert filter string into filter buffer for OFN's.
00054     // We used |'s to hold the places of the nulls, so need to convert.   
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     // Pick a parent window   
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     // Fill the open filename struct
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     // Release allocated buffers.
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 // SaveFileDialog() displays a save file dialog and returns the
00107 // selected file path, if any.
00108 //
00109 // \param title - title string
00110 // \param filetypeList - list of file type / extension pairs.
00111 //        Format should be: "Description of file (*.ext)|*.ext|"
00112 //        Any other format may cause unexpected results.
00113 // \param returnPath - ref to string to fill with received file path, if any.
00114 //        when called, this is also used as the default file name.
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     // Build up filter list.
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     // Convert filter string into filter buffer for OFN's.
00140     // We used |'s to hold the places of the nulls, so need to convert.   
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     // Pick a parent window   
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     // Fill the open filename struct
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     // Release allocated buffers.
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 // OSOnAppCreate() does OS-specific app initialization such as pulling the 
00205 // startup path of the app and the like.
00206 //---------------------------------------------------------------------------
00207 void CATApp::OSOnAppCreate()
00208 {
00209     //---------------------
00210     // Get path to program
00211     //---------------------
00212     CATWChar* buffer = fProgramPath.GetUnicodeBuffer(_MAX_PATH+1);
00213     if (buffer != 0)
00214     {
00215         memset(buffer,0,_MAX_PATH + 1);
00216         // Note - GetModuleFileNameW not supported by Win98, so use ASCII version
00217         // and rely on CATString's conversion for funky paths.
00218         GetModuleFileName(fAppInstance,buffer,_MAX_PATH);
00219         fProgramPath.ReleaseBuffer();     
00220     }
00221 
00222     //------------------------------------------
00223     // Get app base dir from registry
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             // Registry key is bogus. Crap.  Empty string - we'll use program path if possible.
00254             fBaseDir = "";
00255         }
00256     }
00257 
00258     //------------------------------------------
00259     // If registry failed, use program folder
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 }

Generated on Mon Feb 11 04:09:51 2008 for Game Accessibility Suite by doxygen 1.5.4