-/*=========================================================================
-
- Program: bbtk
- Module: $RCSfile: bbtkWxConsole.cxx,v $
- Language: C++
- Date: $Date: 2008/03/19 14:58:13 $
- Version: $Revision: 1.18 $
-
- Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
- l'Image). All rights reserved. See Doc/License.txt or
- http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
-
- This software is distributed WITHOUT ANY WARRANTY; without even
- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- PURPOSE. See the above copyright notices for more information.
-
-=========================================================================*//**
- * \brief Short description in one line
- *
- * Long description which
- * can span multiple lines
- */
-/**
- * \file
- * \brief
- */
-/**
- * \class bbtk::
- * \brief
- */
-
-
-#ifdef _USE_WXWIDGETS_
-
-#include <iostream>
-#include "bbtkWxConsole.h"
-#include "bbtkWxBlackBox.h"
-#include "bbtkConfigurationFile.h"
-#include "bbtkWxStreamRedirector.h"
-
-namespace bbtk
-{
-
-
-
- //================================================================
- WxConsole::WxConsole( wxWindow *parent, wxString title, wxSize size)
- : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
- {
-// m_mgr = new wxAuiManager(this);
- m_mgr.SetManagedWindow(this);
-
- mInterpreter = new bbtk::Interpreter();
- mInterpreter->SetWxConsole(this);
- mInterpreter->SetCommandLine(true);
- //==============
- // Menu
- wxInitAllImageHandlers();
-
- wxMenu *menuFile = new wxMenu;
- menuFile->Append( ID_Menu_Quit, _T("&Quit") );
-
- wxMenu *menuAbout = new wxMenu;
- menuAbout->Append( ID_Menu_About, _T("&About...") );
-
- wxMenu *menuTools = new wxMenu;
- menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
- menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
- menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
- menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
- menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
-
-
- wxMenuBar *menuBar = new wxMenuBar;
- menuBar->Append( menuFile, _T("&File") );
- menuBar->Append( menuTools, _T("&Tools") );
- menuBar->Append( menuAbout, _T("About") );
-
- SetMenuBar( menuBar );
-
- CreateStatusBar();
- SetStatusText( _T("Welcome to bbi !") );
-
- //==============
- // Notebook
-
- // wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
-
-//EED wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
-// mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
- mwxNotebook = new wxAuiNotebook(this,
- -1,
- wxPoint(0, 0),
- wxSize(500,500),
- wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
-
- mwxPageCommand = new wxPanel(mwxNotebook,-1);
- mwxPageHelp = new wxPanel(mwxNotebook,-1);
-
-
- wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
-
- mwxPageCommand->SetAutoLayout(true);
- mwxPageCommand->SetSizer(cmdsizer);
- cmdsizer->Fit(mwxPageCommand);
- cmdsizer->SetSizeHints(mwxPageCommand);
-
- wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
-
- mwxPageHelp->SetAutoLayout(true);
- mwxPageHelp->SetSizer(helpsizer);
- helpsizer->Fit(mwxPageHelp);
- helpsizer->SetSizeHints(mwxPageHelp);
-
- mwxHtmlWindow = new WxBrowser(mwxPageHelp,
-//EED wxSize(1200,0));
- wxSize(200,0));
-
- // mwxHtmlWindow->SetSize(wxSize(800,1000));
- helpsizer->Add (mwxHtmlWindow,1, wxGROW |wxLEFT | wxRIGHT | wxBOTTOM );
-// helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0, wxEXPAND );
-
- //==============
- // Command page
-
- mwxTextHistory =
- new wxTextCtrl(mwxPageCommand,
- ID_Text_History,
- _T(""),wxDefaultPosition,
- wxDefaultSize, //HistorySize,
- wxTE_READONLY |
- wxTE_MULTILINE );
-
- wxFont* FixedFont = new wxFont(10,
- wxFONTFAMILY_MODERN,
- wxFONTSTYLE_NORMAL,
- wxFONTWEIGHT_NORMAL,
- false);
-
- mwxTextHistoryAttr = new wxTextAttr;
- mwxTextHistoryAttr->SetFont(*FixedFont);
- /*
- mwxTextCommand =
- new wxTextCtrl(mwxPageCommand,
- ID_Text_Command,
- _T(""),wxDefaultPosition,
- wxDefaultSize,
- wxTE_PROCESS_ENTER
- | wxTE_PROCESS_TAB
- | wxWANTS_CHARS
- //| wxTAB_TRAVERSAL
- );
- mwxTextCommandAttr = new wxTextAttr;
- mwxTextCommandAttr->SetFont(*FixedFont);
- mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
- */
- mwxTextCommand =
- new wxComboBox(mwxPageCommand,
- ID_Text_Command,
- _T(""),
- wxDefaultPosition,
- wxDefaultSize,
- 0, NULL,
- wxTE_PROCESS_ENTER
-// | wxTE_PROCESS_TAB
-// | wxWANTS_CHARS
-// //| wxTAB_TRAVERSAL
- );
-
-
- mwxTextCommand->SetFocus();
-
- wxPanel *btnsCtrlPanel = CreateBtnsCtrlPanel(mwxPageCommand);
-
- wxButton *btnGo = new wxButton( mwxPageCommand,-1,_T("Go") );
- Connect(btnGo->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGo );
-
- wxFlexGridSizer *sizerCommand= new wxFlexGridSizer(2);
- sizerCommand->AddGrowableCol(0);
- sizerCommand->Add(mwxTextCommand,1,wxGROW);
- sizerCommand->Add(btnGo);
-
- cmdsizer->Add ( mwxTextHistory, 1, wxALL | wxGROW, 10);
-//EED cmdsizer->Add ( mwxTextCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
- cmdsizer->Add ( sizerCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
- cmdsizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
-
-
- // cmdsizer->AddGrowableCol(0);
- // cmdsizer->AddGrowableRow(0);
- // cmdsizer->AddGrowableRow(1);
- // cmdsizer->SetFlexibleDirection(wxBOTH);
-
- //=============================
- // Events connection
- // COMMAND
- // ENTER
- Connect( mwxTextCommand->GetId(),
- wxEVT_COMMAND_TEXT_ENTER,
- (wxObjectEventFunction)& WxConsole::OnCommandEnter );
- /*
- Connect( mwxTextCommand->GetId(),
- wxEVT_CHAR,
- //wxEVT_COMMAND_TEXT_UPDATED,
- (wxObjectEventFunction)& WxConsole::OnCommandChar );
- */
- // MENU
- // Connect (
-
- // Redirection of std::cout to mwxTextHistory and printf
- mRedirect_cout =
- new WxStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
- mRedirect_cerr =
- new WxStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
-
- // Creates and sets the parent window of all bbtk windows
- wxWindow* top = new wxPanel(this,-1);//,_T("top"));
- top->Hide();
- //new wxFrame(this,-1,_T("bbtk"),
- // wxDefaultPosition,
- // wxSize(0,0),
- // wxFRAME_TOOL_WINDOW) ;//wxMINIMIZE_BOX);
-
- Wx::SetTopWindow(top);
-
- // top->Show();
-
-
- // Layout
-//EED SetSizer(sizer);
-
- mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
- mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
- m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false));
- m_mgr.Update();
-
-
- SetAutoLayout(true);
- Layout();
-// Refresh();
-// m_mgr.Update();
- }
- //================================================================
-
- //================================================================
- WxConsole::~WxConsole()
- {
- m_mgr.UnInit();
- delete mRedirect_cout;
- delete mRedirect_cerr;
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnCommandEnter(wxCommandEvent& event)
- {
- wxString line(mwxTextCommand->GetValue());
- CommandString(line);
- }
- //================================================================
-
- //================================================================
- void WxConsole::OnBtnGo(wxCommandEvent& event)
- {
- wxString line(mwxTextCommand->GetValue());
- CommandString(line);
- }
- //================================================================
-
- //================================================================
- void WxConsole::CommandString(wxString line )
- {
- //printf("WxConsole::CommandString 01 \n");
- wxString s = _T("> ") + line + _T("\n");
- mwxTextHistoryAttr->SetTextColour(*wxRED);
- mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
- mwxTextHistory->AppendText(s);
- // send to standard console also
- //printf("WxConsole::CommandString 02 \n");
- // printf("%s",wx2std(s).c_str());
-//EED mwxTextCommand->Clear();
- mwxTextCommand->SetValue(_T(""));
- mwxTextCommand->Append(line);
-
-//EED mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
- mwxTextHistoryAttr->SetTextColour(*wxBLACK);
- mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
-
- //printf("WxConsole::CommandString 03 \n");
- if ( mInterpreter->InterpretLine( wx2std(line) ) ==
- Interpreter::QUIT )
- {
- Close(true);
- }
-
- //printf("WxConsole::CommandString 06 \n");
- }
- //================================================================
-
- //================================================================
- /// Runs the interpretation of a file
- void WxConsole::InterpretFile( const std::string& filename)
- {
- if ( mInterpreter->InterpretFile(filename) ==
- Interpreter::ERROR )
- {
- Show();
- }
- }
- //================================================================
-
- //================================================================
- void WxConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
- {
- Close(true);
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
- {
- m_mgr.Update();
- Refresh();
- wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
- _T("About ..."), wxOK | wxICON_INFORMATION,
- this);
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
- {
- std::string commandStr;
- std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
-#ifdef WIN32
- commandStr = "notepad.exe ";
-#else
- commandStr = "gedit ";
-#endif
- commandStr = commandStr + configFile;
- std::cout << "system: " << commandStr << std::endl;
- system ( commandStr.c_str() );
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
- {
-printf("EED WxConsole::OnMenuCreatePackage 01 \n");
- std::string command("toolsbbtk/appli/CreatePackage");
-printf("EED WxConsole::OnMenuCreatePackage 02 \n");
-
- bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
-printf("EED WxConsole::OnMenuCreatePackage 03 \n");
- Interpreter* I = new Interpreter;
-printf("EED WxConsole::OnMenuCreatePackage 04 \n");
- I->InterpretFile(command);
-printf("EED WxConsole::OnMenuCreatePackage 05 \n");
- delete I;
-printf("EED WxConsole::OnMenuCreatePackage 06 \n");
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
- {
- std::string command("toolsbbtk/appli/CreateBlackBox");
- bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
- Interpreter* I = new Interpreter;
- I->InterpretFile(command);
- delete I;
- }
- //================================================================
-
- //================================================================
- void WxConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
- {
- std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
-
-#if defined(WIN32)
- std::string strappli="start ";
-#else
- std::string strappli="gnome-open ";
-#endif
- std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
- std::cout << "system: " << strcommand << std::endl;
- system ( strcommand.c_str() );
-
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
- {
- std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
- std::string filepath = doc_path+"/bbdoc/make-index.bbs";
- Interpreter* I = new Interpreter;
-
-//EED std::cout << "bbi: include "<<filepath<<std::endl;
-//EED I->InterpretFile( filepath );
-
- I->InterpretLine( "exec freeze");
- I->InterpretLine( "include *");
- I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
- I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
- I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
- I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
-
- delete I;
- }
- //================================================================
-
-
-
-
- //================================================================
- void WxConsole::OnCommandChar(wxCommandEvent& event)
- {
- std::cout << "c";
- /*
- // Command completion
- std::vector<std::string> commands;
- wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
- int ind = sline.size();
- mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
- if (commands.size()==1)
- {
- std::string com = *commands.begin();
- for (; ind<com.size(); ++ind)
- {
- mwxTextCommand->TextAppend( std2wx ( com[ind]) );
- }
- mwxTextCommand->TextAppend(_T(" "));
- }
- else if (commands.size()>1)
- {
- std::vector<std::string>::iterator i;
- write(1,"\n",1);
- for (i=commands.begin();i!=commands.end();++i)
- {
- write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
- PrintChar(' ');
- }
- write(STDOUT_FILENO,"\n> ",3);
- //for (int j=0;j<ind;++j)
- //{
- write(STDOUT_FILENO,line,ind);
- // }
- }
- */
- }
- //================================================================
-
- void WxConsole::ShowHtmlPage(std::string& page)
- {
- // std::cout << "WxConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
- if (mwxHtmlWindow->GoTo(page))
- {
-//EED mwxNotebook->ChangeSelection(1);
- mwxNotebook->SetSelection(1);
- }
- else
- {
- // std::cout << "ERROR html"<<std::endl;
- }
- }
-
-
-
- //================================================================
- wxPanel* WxConsole::CreateBtnsCtrlPanel(wxWindow *parent)
- {
- wxPanel *btnsCtrlPanel = new wxPanel(parent,-1);
- wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
-
- wxButton *btnInclude = new wxButton( btnsCtrlPanel,-1,_T("Include") );
- wxButton *btnReset = new wxButton( btnsCtrlPanel,-1,_T("Reset") );
- wxButton *btnConfig = new wxButton( btnsCtrlPanel,-1,_T("Config") );
- wxButton *btnGraphS = new wxButton( btnsCtrlPanel,-1,_T("Graph simple") );
- wxButton *btnGraphD = new wxButton( btnsCtrlPanel,-1,_T("Graph detailed") );
- wxButton *btnHelp = new wxButton( btnsCtrlPanel,-1,_T("Help") );
-
- btnsSizer->Add( btnInclude );
- btnsSizer->Add( btnReset );
- btnsSizer->Add( btnConfig );
- btnsSizer->Add( btnGraphS );
- btnsSizer->Add( btnGraphD );
- btnsSizer->Add( btnHelp );
- btnsCtrlPanel->SetSizer(btnsSizer);
-
- Connect(btnInclude->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnInclude );
- Connect(btnReset->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnReset );
- Connect(btnConfig->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnConfig );
- Connect(btnGraphS->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGraphS );
- Connect(btnGraphD->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGraphD );
- Connect(btnHelp->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnHelp );
- return btnsCtrlPanel;
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnBtnInclude(wxCommandEvent& event)
- {
- std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
- std::string stdDir = default_doc_dir+"/share/bbtk/bbs";
- wxString defaultDir(stdDir.c_str(), wxConvUTF8);
-
- wxFileDialog dialog(this, _T("Choose a file"),defaultDir, _T(""), _T("*.bbs"), wxOPEN );
- if (dialog.ShowModal() == wxID_OK)
- {
- // std::string command(_T("include "));
- // std::string pathfilename = (const char *)(dialog.GetFilename().mb_str());
- wxString command(_T("include "));
- wxString pathfilename = dialog.GetPath();
- command += pathfilename;
- CommandString( command );
- }
-
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnBtnReset(wxCommandEvent& event)
- {
- CommandString(_T("reset"));
- }
- //================================================================
-
-
- //================================================================
- void WxConsole::OnBtnConfig(wxCommandEvent& event)
- {
- CommandString(_T("config"));
- }
- //================================================================
-
-
-
- //================================================================
- void WxConsole::OnBtnGraphS(wxCommandEvent& event)
- {
- CommandString(_T("graph"));
- }
- //================================================================
-
- //================================================================
- void WxConsole::OnBtnGraphD(wxCommandEvent& event)
- {
- CommandString(_T("graph . 1"));
- }
- //================================================================
-
- //================================================================
- void WxConsole::OnBtnHelp(wxCommandEvent& event)
- {
- CommandString(_T("help"));
- }
- //================================================================
-
-
- //================================================================
- BEGIN_EVENT_TABLE(WxConsole, wxFrame)
- EVT_MENU(WxConsole::ID_Menu_Quit, WxConsole::OnMenuQuit)
- EVT_MENU(WxConsole::ID_Menu_About, WxConsole::OnMenuAbout)
- EVT_MENU(WxConsole::ID_Menu_EditConfig, WxConsole::OnMenuEditConfig)
- EVT_MENU(WxConsole::ID_Menu_CreatePackage, WxConsole::OnMenuCreatePackage)
- EVT_MENU(WxConsole::ID_Menu_CreateBlackBox, WxConsole::OnMenuCreateBlackBox)
- EVT_MENU(WxConsole::ID_Menu_ShowImageGraph, WxConsole::OnMenuShowImageGraph)
- EVT_MENU(WxConsole::ID_Menu_CreateIndex, WxConsole::OnMenuCreateIndex)
- EVT_TEXT_ENTER(WxConsole::ID_Text_Command, WxConsole::OnCommandEnter)
-// EVT_CHAR(WxConsole::ID_Text_Command, WxConsole::OnCommandChar)
- END_EVENT_TABLE()
- //================================================================
-
-} // namespace bbtk
-
-
-#endif //_USE_WXWIDGETS_