sprint 1-alpha
|
00001 /****************************************************************************** 00002 * GTL: GUI Template Library 00003 * A Cross Platform C++ Wrapper for Win32 users 00004 * Copyright (C) 2007-2011 Paolo Medici (www.pmx.it) 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00022 00023 #ifndef _COMMON_DIALOG_H 00024 #define _COMMON_DIALOG_H 00025 00033 #include <commdlg.h> 00034 #include <sprint/gtl/instance.h> 00035 #include <sprint/log.h> 00036 00037 namespace sprint { 00038 namespace gtl { 00039 00045 class OpenDlg { 00046 00047 OPENFILENAMEA of; 00048 00049 public: 00051 OpenDlg() { 00052 memset(&of, 0, sizeof(OPENFILENAME)); 00053 of.lStructSize = sizeof(OPENFILENAME); 00054 } 00055 00058 inline void SetInstance(HINSTANCE inst) 00059 { 00060 of.hInstance = inst; 00061 } 00067 inline void SetTitle(const char *title) 00068 { 00069 of.lpstrTitle = title; 00070 } 00071 00077 inline void SetFilter(const char *filter) 00078 { 00079 of.lpstrFilter = filter; 00080 } 00081 00092 inline void SetDefaultExtension(const char *def) 00093 { 00094 of.lpstrDefExt = def; 00095 } 00096 00097 void SetInitialDir(const char *dir) 00098 { 00099 of.lpstrInitialDir = dir; 00100 } 00101 00102 inline void SetBuffer(char *filename, int lenght) 00103 { 00104 of.lpstrFile = filename; 00105 of.nMaxFile = lenght; 00106 } 00107 00108 inline const char *GetFileName() const { 00109 return of.lpstrFile; 00110 }; 00111 00112 inline const char *GetExtension() const { 00113 return (of.nFileExtension == 0) ? NULL : (of.lpstrFile + of.nFileExtension); 00114 }; 00115 00117 inline int GetFilterIndex() const { 00118 return of.nFilterIndex; 00119 } 00120 00127 inline bool Open(HWND hparent, int flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST) 00128 { 00129 of.hwndOwner = hparent; 00130 of.Flags = flags; 00131 of.hInstance = sprint::gtl::ThisInstance(); 00132 00133 bool ret = GetOpenFileNameA( &of )!=0; 00134 if(!ret) 00135 { logPutsf(LOG_LEVEL_WARNING, "GetOpenFileName Error: %d", CommDlgExtendedError()); } 00136 return ret; 00137 } 00138 00140 inline bool Save(HWND hparent, int flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT) 00141 { 00142 of.hwndOwner = hparent; 00143 of.Flags = flags; 00144 of.hInstance = sprint::gtl::ThisInstance(); 00145 00146 bool ret = GetSaveFileNameA( &of )!=0; 00147 if(!ret) 00148 { logPutsf(LOG_LEVEL_WARNING, "GetSaveFileName Error: %d", CommDlgExtendedError()); } 00149 return ret; 00150 } 00151 00152 00153 }; 00154 00166 class BrowseForFile : public OpenDlg { 00167 public: 00168 char buffer[MAX_PATH]; 00169 public: 00170 BrowseForFile() 00171 { 00172 buffer[0]=0; 00173 OpenDlg::SetBuffer(buffer, MAX_PATH); 00174 } 00175 00177 void OpenAndSet(HWND hWnd, int id) 00178 { 00179 GetDlgItemTextA(hWnd, id, buffer, MAX_PATH); 00180 if(OpenDlg::Open(hWnd)) 00181 { 00182 SetDlgItemTextA(hWnd, id, buffer); 00183 } 00184 } 00186 void SaveAndSet(HWND hWnd, int id) 00187 { 00188 GetDlgItemTextA(hWnd, id, buffer, MAX_PATH); 00189 if(OpenDlg::Save(hWnd)) 00190 { 00191 SetDlgItemTextA(hWnd, id, buffer); 00192 } 00193 } 00194 }; 00195 00196 00197 } 00198 } 00199 00200 #endif