lugre_commondialog.cpp

Go to the documentation of this file.
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     // MessageBox requires user32.lib in linker settings for libraries
00032     // http://msdn.microsoft.com/en-us/library/ms645505.aspx
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     //~ IDABORT Abort button was selected.
00043     //~ IDCONTINUE  Continue button was selected.
00044     //~ IDIGNORE    Ignore button was selected.
00045     //~ IDRETRY Retry button was selected.
00046     //~ IDTRYAGAIN  Try Again button was selected.
00047 }
00048 
00049         
00050 
00051 
00052 // opens browser
00053 bool    OpenBrowser (std::string sURL) {
00054     // http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
00055     /*
00056     HINSTANCE ShellExecute(      
00057         HWND hwnd,
00058         LPCTSTR lpOperation,
00059         LPCTSTR lpFile,
00060         LPCTSTR lpParameters,
00061         LPCTSTR lpDirectory,
00062         INT nShowCmd
00063     );
00064     */
00065     ShellExecute(NULL,"open",sURL.c_str(),NULL,NULL,SW_SHOW);
00066     return true;
00067 }
00068 
00069 
00070 //~ Open and Save As Dialog Boxes 
00071 //~ http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx
00072 //~ http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx      OPENFILENAME struct
00073 //~ http://msdn.microsoft.com/en-us/library/ms646927(VS.85).aspx BOOL GetOpenFileName(LPOPENFILENAME lpofn);
00074 //~ http://msdn.microsoft.com/en-us/library/ms646928(VS.85).aspx BOOL GetSaveFileName(LPOPENFILENAME lpofn);
00075 //~ To display a dialog box that allows the user to select a directory instead of a file, call the SHBrowseForFolder function.
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     // lpstrFilter
00084     static std::string sOFNFilter;
00085     sOFNFilter += sFilter;
00086     sOFNFilter.append(1,'\0'); // each part of the label-pattern pair is zero terminated
00087     sOFNFilter += sFilter;
00088     sOFNFilter.append(2,'\0'); // double zero terminator at the end of the list of pairs
00089     ofn.lpstrFilter = (sOFNFilter.size() > 0) ? sOFNFilter.c_str() : 0;
00090     //~ Pointer to a buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters.
00091     //~ The first string in each pair is a display string that describes the filter (for example, "Text Files"), and the second string specifies the filter pattern (for example, "*.TXT"). To specify multiple filter patterns for a single display string, use a semicolon to separate the patterns (for example, "*.TXT;*.DOC;*.BAK"). A pattern string can be a combination of valid file name characters and the asterisk (*) wildcard character. Do not include spaces in the pattern string.
00092     
00093     ofn.lpstrCustomFilter = 0;
00094     ofn.nFilterIndex = 0;
00095     
00096     // lpstrFile
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     //ofn.pvReserved = NULL;
00120     //ofn.dwReserved = 0;
00121     //ofn.FlagsEx = 0;
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 typedef struct tagOFN { 
00146   DWORD         lStructSize; 
00147   HWND          hwndOwner; 
00148   HINSTANCE     hInstance; 
00149   LPCTSTR       lpstrFilter; 
00150   LPTSTR        lpstrCustomFilter; 
00151   DWORD         nMaxCustFilter; 
00152   DWORD         nFilterIndex; 
00153   LPTSTR        lpstrFile; 
00154   DWORD         nMaxFile; 
00155   LPTSTR        lpstrFileTitle; 
00156   DWORD         nMaxFileTitle; 
00157   LPCTSTR       lpstrInitialDir; 
00158   LPCTSTR       lpstrTitle; 
00159   DWORD         Flags; 
00160   WORD          nFileOffset; 
00161   WORD          nFileExtension; 
00162   LPCTSTR       lpstrDefExt; 
00163   LPARAM        lCustData; 
00164   LPOFNHOOKPROC lpfnHook; 
00165   LPCTSTR       lpTemplateName; 
00166 #if (_WIN32_WINNT >= 0x0500)
00167   void *        pvReserved;
00168   DWORD         dwReserved;
00169   DWORD         FlagsEx;
00170 #endif // (_WIN32_WINNT >= 0x0500)
00171 } OPENFILENAME, *LPOPENFILENAME;
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     //~ wxWindow *mainWindow = new wxWindow();
00193     //~ mainWindow->Show(FALSE);
00194     //~ app->SetTopWindow(mainWindow);
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     //~ delete app; // segfaults ? weird, oh well, better a small memleak than a crash
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     // deactivated, still crashing
00216     return kLugreMessageBoxResult_BoxNotImplemented;
00217 
00218     #if 0
00219     // second attempt, but still problems if executed twice 
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         // note : http://wiki.wxwidgets.org/Wx_In_Non-Wx_Applications
00231         // In short you should use wxInitialize and wxUninitialize with a message loop in between
00232         // http://wiki.wxwidgets.org/Creating_A_DLL_Of_An_Application
00233         // http://docs.wxwidgets.org/2.6.3/wx_wxapp.html
00234         /*
00235         class wxDLLApp : public wxApp
00236         {
00237             int res;
00238             std::string sTitle;
00239             std::string sText;
00240             int style;
00241             wxDLLApp(int style,std::string sTitle,std::string sText) : style(style),sTitle(sTitle),sText(sText),res(0) {}
00242             bool OnInit() {
00243                 res = wxMessageBox(wxString::FromAscii(sText.c_str()),wxString::FromAscii(sTitle.c_str()),style);
00244                 ExitMainLoop();
00245             }
00246         };
00247         */
00248 
00249         wxInitialize(); // (instead of wxEntry)
00250         //~ wxDLLApp* wxTheApp = new wxDLLApp(style,sTitle,sText);
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         //~ wxApp::CleanUp();
00260         wxUninitialize();
00261         //~ res = wxTheApp->res;
00262         //~ delete wxTheApp;
00263     } else {
00264         wxApp *app = new wxApp();
00265         //~ wxApp::SetInstance(app);
00266         int args = 0;
00267         wxEntryStart(args,(wxChar **)0);
00268         
00269         //~ wxWindow *mainWindow = new wxWindow();
00270         //~ mainWindow->Show(FALSE);
00271         //~ app->SetTopWindow(mainWindow);
00272         app->CallOnInit();
00273         
00274         res = wxMessageBox(wxString::FromAscii(sText.c_str()),wxString::FromAscii(sTitle.c_str()),style);
00275         
00276         wxEntryCleanup();
00277         //~ delete app; // segfaults ? weird, oh well, better a small memleak than a crash
00278         // deactivated, still crashing
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 // opens browser
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     // override base class virtuals
00299     // ----------------------------
00300 
00301     // this one is called on application startup and is a good place for the app
00302     // initialization (doing it here and not in the ctor allows to have an error
00303     // return: if OnInit() returns false, the application terminates)
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     // store old working directory to restore it later
00319     char old_cwd[512];
00320     getcwd(old_cwd, 512);
00321     // and set cwd to the given one
00322     chdir(sInitialDir.c_str());
00323             
00324     // create minimal app and window
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     // create and show dialog
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     // TODO manually delete app cause a segfault. unsure if this leads to a memory leak!
00356     //delete app;
00357     
00358     app = 0;
00359     mainWindow = 0;
00360     
00361     // restore working directory
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     //~ printf("## FileOpenDialog wx\n");
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     //~ printf("## FileSaveDialog wx\n");
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 // opens browser
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

Generated on Wed May 23 06:00:14 2012 for cpp by  doxygen 1.5.6