bbtkWxGUIConsole.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkWxGUIConsole.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2009/05/28 08:12:06 $
00006   Version:   $Revision: 1.16 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00031 
00048 #ifdef _USE_WXWIDGETS_
00049 
00050 #include <iostream>     
00051 #include "bbtkWxGUIConsole.h"
00052 #include "bbtkWxBlackBox.h"
00053 #include "bbtkConfigurationFile.h"
00054 #include "bbtkWxStreamRedirector.h"
00055  
00056 
00057 #include "icons/cc_run.xpm"
00058 
00059 namespace bbtk
00060 {
00061 
00062     enum
00063     {
00064       ID_Menu_Quit = 1,
00065       ID_Menu_About,
00066       ID_Menu_EditConfig,
00067       ID_Menu_CreatePackage,
00068       ID_Menu_CreateBlackBox,
00069       ID_Menu_ShowImageGraph,
00070       ID_Menu_ShowHTMLDoc,
00071       ID_Menu_CreateIndex,
00072       ID_Button_Run
00073     };
00074     
00075   
00076   //================================================================
00077   WxGUIConsole::WxGUIConsole( wxWindow *parent, wxString title, wxSize size)
00078     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
00079   {     
00080 //    m_mgr = new wxAuiManager(this);
00081         m_mgr.SetManagedWindow(this);
00082    
00083         mInterpreter = bbtk::Interpreter::New();
00084     mInterpreter->SetUser(this);
00085     mInterpreter->SetCommandLine(true);
00086     mInterpreter->SetThrow(false);
00087     //==============
00088     // Menu
00089     wxInitAllImageHandlers();
00090     
00091     wxMenu *menuFile = new wxMenu;
00092     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
00093     
00094     wxMenu *menuAbout = new wxMenu;
00095     menuAbout->Append( ID_Menu_About, _T("&About...") );
00096 
00097     wxMenu *menuTools = new wxMenu;
00098     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
00099     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
00100     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
00101     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
00102     menuTools->Append( ID_Menu_ShowHTMLDoc, _T("Show &HTML documentation") );
00103     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
00104     
00105     
00106     wxMenuBar *menuBar = new wxMenuBar;
00107     menuBar->Append( menuFile, _T("&File") );
00108     menuBar->Append( menuTools, _T("&Tools") );
00109     menuBar->Append( menuAbout, _T("About") );
00110     
00111     SetMenuBar( menuBar );
00112     
00113     CreateStatusBar();
00114     SetStatusText( _T("Welcome to bbi !") );
00115     
00116     //==============
00117     // Notebook
00118     
00119     //    wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
00120     
00121 //EED    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
00122 //    mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
00123     mwxNotebook = new wxAuiNotebook(this,  
00124                                     -1,
00125                                     wxPoint(0, 0),
00126                                     wxSize(500,500),
00127                                     wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
00128     
00129     mwxPageCommand = new wxPanel(mwxNotebook,-1);        
00130     mwxPageHelp = new wxPanel(mwxNotebook,-1);    
00131     
00132     
00133     wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
00134     
00135     mwxPageCommand->SetAutoLayout(true);    
00136     mwxPageCommand->SetSizer(cmdsizer);
00137     cmdsizer->Fit(mwxPageCommand);
00138     cmdsizer->SetSizeHints(mwxPageCommand);
00139 
00140     wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
00141     
00142     mwxPageHelp->SetAutoLayout(true);    
00143     mwxPageHelp->SetSizer(helpsizer);
00144     helpsizer->Fit(mwxPageHelp);
00145     helpsizer->SetSizeHints(mwxPageHelp);
00146    
00147     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(mwxPageHelp,
00148                                              //EED                                wxSize(1200,0));
00149                                              wxSize(200,0));
00150 
00151     //    mWxGUIHtmlBrowser->SetSize(wxSize(800,1000));
00152     helpsizer->Add (mWxGUIHtmlBrowser,1, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5  );
00153 //    helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0,  wxEXPAND  );
00154     wxBitmap bmp_run(cc_run_xpm);
00155     mwxButtonRun = new wxBitmapButton( mwxPageHelp,ID_Button_Run,bmp_run);//_T("Run")  );
00156     helpsizer->Add( mwxButtonRun, 0, wxALL, 5  );
00157 
00158   
00159     //==============
00160     // Command page 
00161     mWxGUIOutputMessages = new WxGUIOutputMessages(mwxPageCommand);
00162 
00163     mWxGUICommand = new WxGUICommand(mwxPageCommand,this);
00164    
00165     mWxGUICommand->SetFocus();
00166 
00167     cmdsizer->Add (mWxGUIOutputMessages, 1, wxALL | wxGROW, 5);
00168     cmdsizer->Add (mWxGUICommand, 0, wxALL | wxGROW, 5);
00169 
00170         
00171     // Set the parent window of all bbtk windows as a child of this
00172     //    bbtk::Wx::SetTopWindowParent(this);
00173     bbtk::Wx::SetTopWindow(this);
00174     // Top Window Auto Destroys when no more black box window alive 
00175     // : this is the default 
00176     //    bbtk::Wx::SetAutoDestroyTopWindow(true);
00177     // Add the method OnWxSignal as a Wx::Signal observer 
00178     bbtkAddWxSignalObserver(WxGUIConsole::OnWxSignal);
00179 
00180 
00181     // Layout
00182 //EED    SetSizer(sizer);
00183 
00184     mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
00185     mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
00186     m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false)); 
00187     m_mgr.Update();
00188         
00189     SetAutoLayout(true);
00190     Layout();
00191 //    Refresh();
00192 //    m_mgr.Update();
00193   }
00194   //================================================================
00195 
00196  //================================================================
00197   WxGUIConsole::~WxGUIConsole()
00198   {
00199     m_mgr.UnInit();
00200   }
00201   //================================================================
00202 
00203   //================================================================
00204   void WxGUIConsole::OnClose(wxCloseEvent& event)
00205   {
00206     bbtkDebugMessage("widget",9,"bbi::OnClose()"<<std::endl);
00207     //Wx::SetWindowsHaveBeenDestroyedByParent();
00208     //mInterpreter.reset();
00209     wxWindowList& list = GetChildren();
00210     wxWindowList::iterator iter;
00211     for (iter = list.begin(); iter != list.end(); ++iter)
00212       {
00213         (*iter)->Close();
00214       }
00215     this->Destroy();
00216   }
00217   //================================================================
00218 
00219   //================================================================
00220   void WxGUIConsole::OnWxSignal()
00221   {
00222     if ((!bbtk::Wx::IsSomeWindowAlive())&&(!IsShown())) 
00223       {
00224         bbtkDebugMessage("wx",2,"  --> bbtk top window destroyed and WxGUIConsole not shown => destructing"<<std::endl);
00225         /*
00226         wxWindowList& list = GetChildren();
00227         wxWindowList::iterator iter;
00228         for (iter = list.begin(); iter != list.end(); ++iter)
00229           {
00230             (*iter)->Close();
00231           }
00232         */
00233         Close();
00234       }
00235   }
00236   //================================================================
00237 
00238   //================================================================
00239   void WxGUIConsole::WxGUICommandEnter(const std::string& command)
00240   {
00241     std::string s("> ");
00242     s += command + "\n";
00243     mWxGUIOutputMessages->Print(s,wxRED);
00244 
00245     if (  mInterpreter->InterpretLine( command ) == 
00246           Interpreter::Interpreter_QUIT )
00247       {
00248         Close(true); 
00249       }
00250   }
00251   //================================================================
00252 
00253   //================================================================
00255   bool WxGUIConsole::InterpretFile( const std::string& filename) 
00256   { 
00257     if ( mInterpreter->InterpretFile(filename) ==
00258          Interpreter::Interpreter_ERROR ) 
00259       {
00260         return false;
00261       }
00262     return true;
00263   }
00264   //================================================================
00265  
00266   //================================================================
00267   void WxGUIConsole::OnButtonRun(wxCommandEvent& WXUNUSED(event))
00268   {
00269 //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
00270     std::string filename = mWxGUIHtmlBrowser->GetCurrentPage();//wx2std(temp);
00271     size_t s = filename.length();
00272 
00273     Interpreter::Pointer I = Interpreter::New();
00274     
00275     if ((s>3) && (filename[s-1]=='s')
00276         && (filename[s-2]=='b')
00277         && (filename[s-3]=='b')
00278         && (filename[s-4]=='.'))
00279       {
00280         std::string tmp("Executing ");
00281         tmp += filename;
00282         SetStatusText(std2wx(tmp));
00283         I->InterpretFile(filename);
00284       }
00285     else
00286       {
00287         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
00288       }
00289 
00290   }
00291   //================================================================  
00292 
00293   //================================================================
00294   void WxGUIConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
00295   {
00296     Close(true);
00297   }
00298   //================================================================
00299 
00300 
00301   //================================================================
00302   void WxGUIConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
00303   {
00304     m_mgr.Update();
00305         Refresh();
00306     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
00307                  _T("About ..."), wxOK | wxICON_INFORMATION,
00308                  this);
00309   }
00310   //================================================================
00311 
00312 
00313   //================================================================
00314   void WxGUIConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
00315   {
00316         std::string commandStr;
00317     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
00318 #ifdef WIN32
00319         commandStr = "notepad.exe ";
00320 #else
00321         commandStr = "gedit ";
00322 #endif 
00323         commandStr = commandStr + configFile;
00324         std::cout << "system: " << commandStr << std::endl;
00325         system ( commandStr.c_str() );
00326   }
00327   //================================================================
00328 
00329 
00330   //================================================================
00331   void WxGUIConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
00332   {
00333     std::string command("toolsbbtk/appli/GUICreatePackage");
00334 
00335     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
00336     Interpreter::Pointer I = Interpreter::New();    
00337     I->InterpretFile(command);
00338   }
00339   //================================================================
00340 
00341 
00342   //================================================================
00343   void WxGUIConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
00344   {
00345     std::string command("toolsbbtk/appli/GUICreateBlackBox");
00346     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
00347     Interpreter::Pointer I = Interpreter::New();    
00348     I->InterpretFile(command);
00349   }
00350   //================================================================
00351   
00352   //================================================================
00353   void WxGUIConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
00354   {
00355     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00356 
00357 #if defined(WIN32)
00358     std::string strappli="start ";
00359 #else
00360     std::string strappli="gnome-open ";
00361 #endif
00362     std::string strcommand0 = "cd \"" + default_temp_dir+"/temp_dir/" +"\"";
00363     std::string strcommand1 = strappli + "workspace_workspacePrototype.png";
00364     std::string strcommand = strcommand0 + " && " + strcommand1;
00365         std::cout << "system: " << strcommand << std::endl;
00366     system ( strcommand.c_str() );
00367 
00368   }
00369   //================================================================
00370 
00371   //================================================================
00372   void WxGUIConsole::OnMenuShowHTMLDoc(wxCommandEvent& WXUNUSED(event))
00373   {
00374         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
00375 
00376 #if defined(WIN32)
00377     std::string strappli="start ";
00378 #else
00379     std::string strappli="gnome-open ";
00380 #endif
00381     std::string strcommand0 = "cd \"" + doc_path+"\"";
00382     std::string strcommand1 = strappli + "index.html";
00383     std::string strcommand = strcommand0 + " && " + strcommand1;
00384         std::cout << "system: " << strcommand << std::endl;
00385     system ( strcommand.c_str() );
00386 
00387   }
00388   //================================================================
00389 
00390 
00391 
00392   //================================================================
00393   void WxGUIConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
00394   {
00395 /*
00396     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
00397     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
00398     Interpreter* I = new Interpreter;    
00399     I->InterpretLine( "exec freeze");
00400     I->InterpretLine( "include *");
00401     I->InterpretLine( "help packages");
00402     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
00403     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
00404     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
00405     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");    
00406     delete I;
00407 */
00408 
00409         std::string bin_path            = ConfigurationFile::GetInstance().Get_bin_path();
00410         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
00411         std::string bbdoc_path          = doc_path+"/bbdoc";
00412     std::string make_index_path = "\"" + bbdoc_path+"/make-index.bbs\"";
00413 
00414         std::string strcommand0         = "cd "+bbdoc_path+" && mkdir tmp && cd tmp";
00415 
00416         std::string strcommand1         = "cd "+bbdoc_path+"/tmp";
00417     std::string strcommand2             = "\""+bin_path+"/bbi\" -N "+make_index_path;
00418 #ifdef WIN32 
00419     std::string strcommand3             = "move index*.html ../.";
00420 #else
00421     std::string strcommand3             = "mv index*.html ../.";
00422 #endif
00423 
00424         std::string strcommand          =       strcommand1 +" && "+
00425                                                                         strcommand2 +" && "+
00426                                                                         strcommand3;
00427 
00428         std::cout << "system: " << strcommand0 << std::endl;
00429         std::cout << "system: " << strcommand << std::endl;
00430 
00431         system ( strcommand0.c_str() );
00432         system ( strcommand.c_str() );
00433   }
00434   //================================================================
00435 
00436 
00437   //================================================================
00438   void WxGUIConsole::InterpreterUserViewHtmlPage(const std::string& page)
00439   {
00440     std::string s(page);
00441     //  std::cout << "WxGUIConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
00442     if (mWxGUIHtmlBrowser->GoTo(s)) 
00443       {
00444 //EED   mwxNotebook->ChangeSelection(1);
00445         mwxNotebook->SetSelection(1);
00446       }
00447     else 
00448       {
00449          // std::cout << "ERROR html"<<std::endl;
00450       }
00451   } 
00452   //================================================================  
00453   
00454   //================================================================  
00455   // Overloaded Show method to handle bbtk::Wx::TopWindow auto-destruction
00456   // OBSOLETE !!!
00457   bool WxGUIConsole::Show(bool show)
00458   {
00459     //    bbtk::Wx::SetAutoDestroyTopWindow(!show);
00460     return wxFrame::Show(show);
00461   }
00462   //================================================================  
00463    
00464   //================================================================  
00465   BEGIN_EVENT_TABLE(WxGUIConsole, wxFrame)
00466     EVT_CLOSE(  WxGUIConsole::OnClose)
00467     EVT_MENU(ID_Menu_Quit                       , WxGUIConsole::OnMenuQuit)
00468     EVT_MENU(ID_Menu_About                      , WxGUIConsole::OnMenuAbout)
00469     EVT_MENU(ID_Menu_EditConfig         , WxGUIConsole::OnMenuEditConfig)
00470     EVT_MENU(ID_Menu_CreatePackage      , WxGUIConsole::OnMenuCreatePackage)
00471     EVT_MENU(ID_Menu_CreateBlackBox     , WxGUIConsole::OnMenuCreateBlackBox)
00472     EVT_MENU(ID_Menu_ShowImageGraph     , WxGUIConsole::OnMenuShowImageGraph)
00473     EVT_MENU(ID_Menu_ShowHTMLDoc        , WxGUIConsole::OnMenuShowHTMLDoc)
00474     EVT_MENU(ID_Menu_CreateIndex        , WxGUIConsole::OnMenuCreateIndex)
00475     EVT_BUTTON(ID_Button_Run            , WxGUIConsole::OnButtonRun )
00476     END_EVENT_TABLE()
00477   //================================================================
00478 
00479 } // namespace bbtk
00480 
00481 
00482 #endif //_USE_WXWIDGETS_

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1