/*========================================================================= Program: bbtk Module: $RCSfile: bbtkWxEditor.cxx,v $ Language: C++ Date: $Date: 2008/03/18 15:31:51 $ Version: $Revision: 1.3 $ 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 #include "bbtkWxEditor.h" #include "bbtkWxBlackBox.h" #include "bbtkConfigurationFile.h" #include "bbtkWxStreamRedirector.h" #include "icons/cc_new.xpm" #include "icons/cc_open.xpm" #include "icons/cc_save.xpm" #include "icons/cc_run.xpm" #include "icons/cc_exit.xpm" // #include "icons/copy.xpm" // #include "icons/cut.xpm" // #include "icons/preview.xpm" // paste XPM // #include "icons/print.xpm" // #include "icons/help.xpm" namespace bbtk { //================================================================ class WxTextCtrlGettingKeyEvents : public wxTextCtrl { public: WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos, const wxSize &size, int style = 0) : wxTextCtrl(parent, id, value, pos, size, style) { } void SetWxEditor(WxEditor* e) { mWxEditor = e; } void OnKeyDown(wxKeyEvent& event); void OnKeyUp(wxKeyEvent& event); void OnChar(wxKeyEvent& event); private : WxEditor* mWxEditor; DECLARE_EVENT_TABLE() }; BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl) EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown) EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp) EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar) END_EVENT_TABLE() void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event) { event.Skip(); } void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event) { event.Skip(); } void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event) { mWxEditor->OnKeyPress(event); event.Skip(); } //================================================================ //================================================================ WxEditor::WxEditor( wxWindow *parent ) : wxPanel(parent, -1) { mInterpreter = new bbtk::Interpreter(); //mInterpreter->SetWxEditor(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 !") ); */ // wxFlexGridSizer *sizer= new wxFlexGridSizer(2); wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); mwxSplit = new wxSplitterWindow(this, -1, wxDefaultPosition, wxDefaultSize, //wxSize(400,200), wxSP_3D | wxSP_LIVE_UPDATE ); mwxInputText = new WxTextCtrlGettingKeyEvents(mwxSplit, ID_InputText, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE // |wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB // | wxWANTS_CHARS | wxTAB_TRAVERSAL ); mwxInputText->SetWxEditor(this); wxFont* FixedFont = new wxFont(10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false); mwxInputTextAttr = new wxTextAttr; mwxInputTextAttr->SetFont(*FixedFont); // sizer->AddGrowableCol(0); // sizer->Add(mwxInputText,1,wxGROW); mwxInputText->SetFocus(); mwxOutputText = new WxTextCtrlGettingKeyEvents(mwxSplit, ID_OutputText, _T(""),wxDefaultPosition, wxDefaultSize, //HistorySize, wxTE_READONLY | wxTE_MULTILINE ); mwxOutputText->SetWxEditor(this); mwxOutputTextAttr = new wxTextAttr; mwxOutputTextAttr->SetFont(*FixedFont); mwxOutputText->Hide(); // sizer->Add(mwxOutputText,1,wxGROW); sizer->Add(mwxSplit,1,wxGROW); mwxSplit->Initialize(mwxInputText); // BUTTONS wxPanel *btnsCtrlPanel = new wxPanel(this,-1); wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL); wxBitmap bmp_new(cc_new_xpm); mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New") ); btnsSizer->Add( mwxButtonNew ); wxBitmap bmp_open(cc_open_xpm); mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open") ); btnsSizer->Add( mwxButtonOpen ); wxBitmap bmp_save(cc_save_xpm); mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save") ); btnsSizer->Add( mwxButtonSave ); wxBitmap bmp_run(cc_run_xpm); mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run") ); btnsSizer->Add( mwxButtonRun ); wxBitmap bmp_quit(cc_exit_xpm); mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") ); btnsSizer->Add( mwxButtonQuit ); mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T("")); btnsSizer->Add( mwxPosition ); btnsCtrlPanel->SetSizer(btnsSizer); sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW , 10 ); // Redirection of std::cout and std::cerr to mwxOutputText and printf mRedirect_cout = new WxStreamRedirector(std::cout,mwxOutputText,*wxBLACK,true); mRedirect_cerr = new WxStreamRedirector(std::cerr,mwxOutputText,*wxGREEN,true); SetSizer(sizer); SetAutoLayout(true); Layout(); } //================================================================ //================================================================ WxEditor::~WxEditor() { delete mInterpreter; delete mRedirect_cout; delete mRedirect_cerr; } //================================================================ //================================================================ void WxEditor::OnButtonOpen(wxCommandEvent& event) { Open(); } void WxEditor::Open() { std::cout << "-------------- OPEN ---------------"<ShowModal(); if (fd->GetReturnCode()==wxID_OK) { std::cout << "file [" << wx2std(fd->GetPath()) << "]" <LoadFile(fd->GetPath()); } else { std::cout << "-------------- CANCELLED ---------------"<LoadFile(std2wx(filename)); mwxInputText->SetModified(false); } //================================================================ //================================================================ void WxEditor::OnButtonSave(wxCommandEvent& event) { Save(); } void WxEditor::Save() { std::cout << "-------------- SAVE ---------------"<ShowModal(); if (fd->GetReturnCode()==wxID_OK) { std::cout << "file [" << wx2std(fd->GetPath()) << "]" <SaveFile(fd->GetPath()); mwxInputText->SetModified(false); } else { std::cout << "-------------- CANCELLED ---------------"<Close(); } //================================================================ //================================================================ void WxEditor::OnButtonRun(wxCommandEvent& event) { Run(); } void WxEditor::Run() { std::cout << "-------------- RUN ---------------"<IsSplit()) { int size = -100; int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.); if (sizeSplitHorizontally( mwxInputText, mwxOutputText, size); } std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue())); bool com = false; mInterpreter->InterpretLine("reset",com); try { mInterpreter->InterpretBuffer(buffer); } catch (...) { } } //================================================================ //================================================================ bool WxEditor::AskSave() { if (!mwxInputText->IsModified()) return true; wxMessageDialog* d = new wxMessageDialog(this, _T("Buffer modified. Save it ?"), _T("Save buffer"), wxYES_NO | wxCANCEL | wxICON_QUESTION); switch (d->ShowModal()) { case wxID_YES : Save(); case wxID_NO : return true; case wxID_CANCEL : return false; } } //================================================================ //================================================================ void WxEditor::OnButtonNew(wxCommandEvent& event) { New(); } void WxEditor::New() { std::cout << "-------------- NEW ---------------" << std::endl; AskSave(); mwxInputText->Clear(); } //================================================================ //================================================================ void WxEditor::OnKeyPress(wxKeyEvent& event) { long line, column, pos = mwxInputText->GetInsertionPoint(); mwxInputText->PositionToXY(pos, &column, &line); // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"), char mess[200]; sprintf(mess,"%ld:%ld / %ld:%ld (%ld char)", line,column, (long)mwxInputText->GetNumberOfLines(), (long)mwxInputText->GetLineLength(line), (long)mwxInputText->GetValue().length()); mwxPosition->SetLabel(wxString(mess)); // std::cout << "Key="< 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 (; indTextAppend( std2wx ( com[ind]) ); } mwxTextCommand->TextAppend(_T(" ")); } else if (commands.size()>1) { std::vector::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;jAddGrowableCol(0); sizer->Add(mEditor,1,wxGROW); SetSizer(sizer); // 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); SetAutoLayout(true); Layout(); } WxEditorWindow::~WxEditorWindow() { } } // namespace bbtk #endif //_USE_WXWIDGETS_