00001 #include "lugre_prefix.h"
00002 #include "lugre_commondialog.h"
00003 #include <string>
00004
00005 using namespace Lugre;
00006
00007
00008
00009
00010 #if LUGRE_PLATFORM == LUGRE_PLATFORM_WIN32
00011
00012 #include "windows.h"
00013
00014 #include "commdlg.h"
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <cstring>
00019
00020
00021 namespace Lugre {
00022
00023 Lugre::eLugreMessageBoxResult LugreMessageBox (Lugre::eLugreMessageBoxType iType,std::string sTitle,std::string sText) {
00024 int iFlags = MB_TASKMODAL;
00025 switch (iType) {
00026 case kLugreMessageBoxType_Ok : iFlags |= MB_OK; break;
00027 case kLugreMessageBoxType_OkCancel : iFlags |= MB_OKCANCEL; break;
00028 case kLugreMessageBoxType_YesNo : iFlags |= MB_YESNO; break;
00029 case kLugreMessageBoxType_YesNoCancel : iFlags |= MB_YESNOCANCEL; break;
00030 }
00031
00032
00033 int res = MessageBox(NULL,sText.c_str(),sTitle.c_str(), iFlags);
00034 switch (res) {
00035 case IDOK : return kLugreMessageBoxResult_Ok;
00036 case IDYES : return kLugreMessageBoxResult_Yes;
00037 case IDNO : return kLugreMessageBoxResult_No;
00038 case IDCANCEL : return kLugreMessageBoxResult_Cancel;
00039 }
00040 return kLugreMessageBoxResult_Unknown;
00041
00042
00043
00044
00045
00046
00047 }
00048
00049
00050
00051
00052
00053 bool OpenBrowser (std::string sURL) {
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 ShellExecute(NULL,"open",sURL.c_str(),NULL,NULL,SW_SHOW);
00066 return true;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076 #define kWIN32_OFN_BUFFER_SIZE 1024
00077 void LugreWin32InitOFN (OPENFILENAME& ofn,std::string sInitialDir,std::string sFilter,std::string sTitle) {
00078 memset(&ofn,0,sizeof(OPENFILENAME));
00079 ofn.lStructSize = sizeof(OPENFILENAME);
00080 ofn.hwndOwner = NULL;
00081 ofn.hInstance = NULL;
00082
00083
00084 static std::string sOFNFilter;
00085 sOFNFilter += sFilter;
00086 sOFNFilter.append(1,'\0');
00087 sOFNFilter += sFilter;
00088 sOFNFilter.append(2,'\0');
00089 ofn.lpstrFilter = (sOFNFilter.size() > 0) ? sOFNFilter.c_str() : 0;
00090
00091
00092
00093 ofn.lpstrCustomFilter = 0;
00094 ofn.nFilterIndex = 0;
00095
00096
00097 static char pMyOFNBuffer[kWIN32_OFN_BUFFER_SIZE+2];
00098 *pMyOFNBuffer = 0;
00099 ofn.lpstrFile = pMyOFNBuffer;
00100 ofn.nMaxFile = kWIN32_OFN_BUFFER_SIZE;
00101
00102 ofn.lpstrFileTitle = 0;
00103 ofn.nMaxFileTitle = 0;
00104
00105 static std::string sOFNInitialDir = sInitialDir;
00106 ofn.lpstrInitialDir = (sOFNInitialDir.size() > 0) ? sOFNInitialDir.c_str() : 0;
00107
00108 static std::string sOFNTitle = sTitle;
00109 ofn.lpstrTitle = (sOFNTitle.size() > 0) ? sOFNTitle.c_str() : 0;
00110
00111 ofn.Flags = OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;
00112
00113 ofn.nFileOffset = 0;
00114 ofn.nFileExtension = 0;
00115 ofn.lpstrDefExt = 0;
00116 ofn.lCustData = 0;
00117 ofn.lpfnHook = 0;
00118 ofn.lpTemplateName = 0;
00119
00120
00121
00122 }
00123
00124
00125 bool FileOpenDialog (const std::string& sInitialDir,const std::string& sFilePattern,const std::string& sTitle,std::string& sFilePath) {
00126 OPENFILENAME ofn;
00127 LugreWin32InitOFN(ofn,sInitialDir,sFilePattern,sTitle);
00128 if (!GetOpenFileName(&ofn)) return false;
00129 sFilePath = ofn.lpstrFile;
00130 return true;
00131 }
00132
00133 bool FileSaveDialog (const std::string& sInitialDir,const std::string& sFilePattern,const std::string& sTitle,std::string& sFilePath) {
00134 OPENFILENAME ofn;
00135 LugreWin32InitOFN(ofn,sInitialDir,sFilePattern,sTitle);
00136 if (!GetSaveFileName(&ofn)) return false;
00137 sFilePath = ofn.lpstrFile;
00138 return true;
00139 }
00140
00141 };
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 #elif LUGRE_PLATFORM == LUGRE_PLATFORM_LINUX
00177
00178 #include <string>
00179 #include <wx/app.h>
00180 #include <wx/window.h>
00181 #include <wx/filedlg.h>
00182 #include <wx/msgdlg.h>
00183 #include <unistd.h>
00184
00185
00186 namespace Lugre {
00187
00188 Lugre::eLugreMessageBoxResult LugreMessageBox (Lugre::eLugreMessageBoxType iType,std::string sTitle,std::string sText) {
00189 wxApp *app = new wxApp();
00190 int args = 0;
00191 wxEntryStart(args,(wxChar **)0);
00192
00193
00194
00195 app->CallOnInit();
00196
00197 int style = wxOK;
00198 switch (iType) {
00199 case kLugreMessageBoxType_Ok : style = wxOK; break;
00200 case kLugreMessageBoxType_OkCancel : style = wxOK | wxCANCEL ; break;
00201 case kLugreMessageBoxType_YesNo : style = wxYES_NO ; break;
00202 case kLugreMessageBoxType_YesNoCancel : style = wxYES_NO | wxCANCEL ; break;
00203 }
00204 int res = wxMessageBox(wxString::FromAscii(sText.c_str()),wxString::FromAscii(sTitle.c_str()),style);
00205
00206 wxEntryCleanup();
00207
00208
00209 switch (res) {
00210 case wxYES: return kLugreMessageBoxResult_Yes;
00211 case wxNO: return kLugreMessageBoxResult_No;
00212 case wxCANCEL: return kLugreMessageBoxResult_Cancel;
00213 case wxOK: return kLugreMessageBoxResult_Ok;
00214 }
00215
00216 return kLugreMessageBoxResult_BoxNotImplemented;
00217
00218 #if 0
00219
00220 int style = wxOK;
00221 switch (iType) {
00222 case kLugreMessageBoxType_Ok : style = wxOK; break;
00223 case kLugreMessageBoxType_OkCancel : style = wxOK | wxCANCEL ; break;
00224 case kLugreMessageBoxType_YesNo : style = wxYES_NO ; break;
00225 case kLugreMessageBoxType_YesNoCancel : style = wxYES_NO | wxCANCEL ; break;
00226 }
00227
00228 int res = 0;
00229 if (0) {
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 wxInitialize();
00250
00251
00252 wxWindow *mainWindow = new wxWindow();
00253 mainWindow->Show(FALSE);
00254 wxTheApp->SetTopWindow(mainWindow);
00255
00256 res = wxMessageBox(wxString::FromAscii(sText.c_str()),wxString::FromAscii(sTitle.c_str()),style);
00257
00258 wxTheApp->OnExit();
00259
00260 wxUninitialize();
00261
00262
00263 } else {
00264 wxApp *app = new wxApp();
00265
00266 int args = 0;
00267 wxEntryStart(args,(wxChar **)0);
00268
00269
00270
00271
00272 app->CallOnInit();
00273
00274 res = wxMessageBox(wxString::FromAscii(sText.c_str()),wxString::FromAscii(sTitle.c_str()),style);
00275
00276 wxEntryCleanup();
00277
00278
00279 }
00280
00281 switch (res) {
00282 case wxYES: return kLugreMessageBoxResult_Yes;
00283 case wxNO: return kLugreMessageBoxResult_No;
00284 case wxCANCEL: return kLugreMessageBoxResult_Cancel;
00285 case wxOK: return kLugreMessageBoxResult_Ok;
00286 }
00287
00288 return kLugreMessageBoxResult_BoxNotImplemented;
00289 #endif
00290 }
00291
00292
00293 bool OpenBrowser (std::string sURL) { printf("OpenBrowser:TODO:open browser: %s\n",sURL.c_str()); return false; }
00294
00295 class MyApp : public wxApp
00296 {
00297 public:
00298
00299
00300
00301
00302
00303
00304 virtual bool OnInit() {
00305 return true;
00306 }
00307
00308 int OnExit() {
00309 return 0;
00310 }
00311 };
00312
00313
00314
00315 bool FileUniDialog (const std::string& sInitialDir, const std::string& sFilePattern,
00316 const std::string& sTitle, std::string& sFilePath, bool open) {
00317
00318
00319 char old_cwd[512];
00320 getcwd(old_cwd, 512);
00321
00322 chdir(sInitialDir.c_str());
00323
00324
00325 wxApp *app = new wxApp();
00326 int args = 0;
00327
00328 wxEntryStart(args,(wxChar **)0);
00329
00330 wxWindow *mainWindow = new wxWindow();
00331 mainWindow->Show(FALSE);
00332 app->SetTopWindow(mainWindow);
00333
00334 app->CallOnInit();
00335
00336
00337
00338 wxFileDialog* openFileDialog =
00339 new wxFileDialog( mainWindow, wxString::FromAscii(sTitle.c_str()), _(""), _(""),
00340 wxString::FromAscii(sFilePattern.c_str()),
00341 open ? (wxOPEN | wxFD_FILE_MUST_EXIST) : (wxSAVE | wxFD_OVERWRITE_PROMPT),
00342 wxDefaultPosition);
00343
00344 bool ok = false;
00345
00346 if ( openFileDialog->ShowModal() == wxID_OK ){
00347 sFilePath = openFileDialog->GetPath().ToAscii();
00348 ok = true;
00349 }
00350
00351 wxEntryCleanup();
00352
00353 delete mainWindow;
00354
00355
00356
00357
00358 app = 0;
00359 mainWindow = 0;
00360
00361
00362 chdir(old_cwd);
00363
00364 return ok;
00365 }
00366
00367
00368 bool FileOpenDialog (const std::string& sInitialDir, const std::string& sFilePattern,
00369 const std::string& sTitle, std::string& sFilePath) {
00370
00371 return FileUniDialog(sInitialDir,sFilePattern,sTitle,sFilePath,true);
00372 }
00373
00374 bool FileSaveDialog (const std::string& sInitialDir, const std::string& sFilePattern,
00375 const std::string& sTitle, std::string& sFilePath) {
00376
00377 return FileUniDialog(sInitialDir,sFilePattern,sTitle,sFilePath,false);
00378 }
00379
00380 };
00381
00382
00383
00384 #else
00385
00386
00387 namespace Lugre {
00388
00389 Lugre::eLugreMessageBoxResult LugreMessageBox (Lugre::eLugreMessageBoxType iType,std::string sTitle,std::string sText) {
00390 return kLugreMessageBoxResult_BoxNotImplemented;
00391 }
00392
00393
00394 bool OpenBrowser (std::string sURL) { printf("OpenBrowser:TODO:open browser: %s\n",sURL.c_str()); return false; }
00395
00396 bool FileOpenDialog (const std::string& sInitialDir,const std::string& sFilePattern,const std::string& sTitle,std::string& sFilePath) {
00397 printf("## FileOpenDialog dummy plat=%d lin=%d win=%d\n",(int)LUGRE_PLATFORM,(int)LUGRE_PLATFORM_LINUX,(int)LUGRE_PLATFORM_WIN32);
00398 #if LUGRE_PLATFORM == LUGRE_PLATFORM_WIN32
00399 printf("LUGRE_PLATFORM == LUGRE_PLATFORM_WIN32\n");
00400 #elif LUGRE_PLATFORM == LUGRE_PLATFORM_LINUX
00401 printf("LUGRE_PLATFORM == LUGRE_PLATFORM_LINUX\n");
00402 #else
00403 printf("LUGRE_PLATFORM == ????\n");
00404 #endif
00405
00406 return false;
00407 }
00408 bool FileSaveDialog (const std::string& sInitialDir,const std::string& sFilePattern,const std::string& sTitle,std::string& sFilePath) {
00409 printf("## FileSaveDialog dummy\n");
00410 return false;
00411 }
00412
00413 };
00414
00415
00416 #endif