1 /*=========================================================================
4 Module: $RCSfile: bbtkWxEditor.cxx,v $
6 Date: $Date: 2008/03/20 11:04:57 $
7 Version: $Revision: 1.7 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*//**
18 * \brief Short description in one line
20 * Long description which
21 * can span multiple lines
33 #ifdef _USE_WXWIDGETS_
36 #include "bbtkWxEditor.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
39 #include "bbtkWxStreamRedirector.h"
42 #include "icons/cc_new.xpm"
43 #include "icons/cc_open.xpm"
44 #include "icons/cc_save.xpm"
45 #include "icons/cc_run.xpm"
46 #include "icons/cc_exit.xpm"
47 // #include "icons/copy.xpm"
48 // #include "icons/cut.xpm"
49 // #include "icons/preview.xpm" // paste XPM
50 // #include "icons/print.xpm"
51 // #include "icons/help.xpm"
59 //================================================================
60 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
63 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
64 const wxPoint &pos, const wxSize &size, int style = 0)
65 : wxTextCtrl(parent, id, value, pos, size, style)
69 void SetWxEditor(WxEditor* e) { mWxEditor = e; }
71 void OnKeyDown(wxKeyEvent& event);
72 void OnKeyUp(wxKeyEvent& event);
73 void OnChar(wxKeyEvent& event);
80 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
81 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
82 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
83 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
88 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
93 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
95 mWxEditor->OnKeyUp(event);
99 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
101 mWxEditor->OnKeyDown(event);
104 //================================================================
109 //================================================================
110 WxEditor::WxEditor( wxWindow *parent )
111 : wxPanel(parent, -1)
113 mInterpreter = new bbtk::Interpreter();
114 //mInterpreter->SetWxEditor(this);
115 mInterpreter->SetCommandLine(true);
119 wxInitAllImageHandlers();
122 wxMenu *menuFile = new wxMenu;
123 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
125 wxMenu *menuAbout = new wxMenu;
126 menuAbout->Append( ID_Menu_About, _T("&About...") );
128 wxMenu *menuTools = new wxMenu;
129 menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
130 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
131 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
132 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
133 menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
136 wxMenuBar *menuBar = new wxMenuBar;
137 menuBar->Append( menuFile, _T("&File") );
138 menuBar->Append( menuTools, _T("&Tools") );
139 menuBar->Append( menuAbout, _T("About") );
141 SetMenuBar( menuBar );
144 SetStatusText( _T("Welcome to bbi !") );
147 // wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
148 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
149 mwxSplit = new wxSplitterWindow(this,
157 new WxTextCtrlGettingKeyEvents(mwxSplit,
163 // |wxTE_PROCESS_ENTER
168 mwxInputText->SetWxEditor(this);
170 wxFont* FixedFont = new wxFont(10,
176 mwxInputTextAttr = new wxTextAttr;
177 mwxInputTextAttr->SetFont(*FixedFont);
179 // sizer->AddGrowableCol(0);
180 // sizer->Add(mwxInputText,1,wxGROW);
183 new WxTextCtrlGettingKeyEvents(mwxSplit,
185 _T(""),wxDefaultPosition,
186 wxDefaultSize, //HistorySize,
190 mwxOutputText->SetWxEditor(this);
192 mwxOutputTextAttr = new wxTextAttr;
193 mwxOutputTextAttr->SetFont(*FixedFont);
194 mwxOutputText->Hide();
196 // sizer->Add(mwxOutputText,1,wxGROW);
197 sizer->Add(mwxSplit,1,wxGROW);
198 mwxSplit->Initialize(mwxInputText);
201 wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
202 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
204 wxBitmap bmp_new(cc_new_xpm);
205 mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New") );
206 btnsSizer->Add( mwxButtonNew );
207 wxBitmap bmp_open(cc_open_xpm);
208 mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open") );
209 btnsSizer->Add( mwxButtonOpen );
210 wxBitmap bmp_save(cc_save_xpm);
211 mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save") );
212 btnsSizer->Add( mwxButtonSave );
213 wxBitmap bmp_run(cc_run_xpm);
214 mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run") );
215 btnsSizer->Add( mwxButtonRun );
216 wxBitmap bmp_quit(cc_exit_xpm);
217 mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") );
218 btnsSizer->Add( mwxButtonQuit );
221 mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T(""));
222 btnsSizer->Add( mwxPosition );
225 btnsCtrlPanel->SetSizer(btnsSizer);
226 sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW
229 // Redirection of std::cout and std::cerr to mwxOutputText and printf
231 new WxStreamRedirector(std::cout,mwxOutputText,*wxBLACK,true);
233 new WxStreamRedirector(std::cerr,mwxOutputText,*wxGREEN,true);
236 mwxInputText->SetFocus();
242 //================================================================
244 //================================================================
245 WxEditor::~WxEditor()
248 delete mRedirect_cout;
249 delete mRedirect_cerr;
251 //================================================================
254 //================================================================
255 void WxEditor::OnButtonOpen(wxCommandEvent& event)
258 mwxInputText->SetFocus();
260 void WxEditor::Open()
262 std::cout << "-------------- OPEN ---------------"<<std::endl;
264 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
268 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
270 wxOPEN | wxFILE_MUST_EXIST );
273 if (fd->GetReturnCode()==wxID_OK)
275 std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
276 mwxInputText->LoadFile(fd->GetPath());
280 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
283 //================================================================
285 //================================================================
286 void WxEditor::Open(const std::string& filename)
288 mwxInputText->LoadFile(std2wx(filename));
289 mwxInputText->SetModified(false);
292 //================================================================
294 //================================================================
295 void WxEditor::OnButtonSave(wxCommandEvent& event)
298 mwxInputText->SetFocus();
300 void WxEditor::Save()
302 std::cout << "-------------- SAVE ---------------"<<std::endl;
303 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
305 wxSAVE | wxOVERWRITE_PROMPT );
308 if (fd->GetReturnCode()==wxID_OK)
310 std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
311 mwxInputText->SaveFile(fd->GetPath());
312 mwxInputText->SetModified(false);
316 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
319 //================================================================
321 //================================================================
322 void WxEditor::OnButtonQuit(wxCommandEvent& event)
325 mwxInputText->SetFocus();
327 void WxEditor::Quit()
329 std::cout << "-------------- QUIT ---------------"<<std::endl;
330 if (AskSave()) GetParent()->Close();
332 //================================================================
335 //================================================================
336 void WxEditor::OnButtonRun(wxCommandEvent& event)
339 mwxInputText->SetFocus();
343 std::cout << "-------------- RUN ---------------"<<std::endl;
345 if (!mwxSplit->IsSplit())
348 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
349 if (size<minsize) size = minsize;
350 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
353 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
354 mInterpreter->InterpretLine("reset");
357 mInterpreter->InterpretBuffer(buffer);
363 //================================================================
365 //================================================================
366 bool WxEditor::AskSave()
368 if (!mwxInputText->IsModified()) return true;
369 wxMessageDialog* d = new wxMessageDialog(this,
370 _T("Buffer modified. Save it ?"),
372 wxYES_NO | wxCANCEL | wxICON_QUESTION);
373 switch (d->ShowModal())
375 case wxID_YES : Save();
376 case wxID_NO : return true;
377 case wxID_CANCEL : return false;
382 //================================================================
384 //================================================================
385 void WxEditor::OnButtonNew(wxCommandEvent& event)
388 mwxInputText->SetFocus();
392 std::cout << "-------------- NEW ---------------" << std::endl;
394 mwxInputText->Clear();
397 //================================================================
399 //================================================================
400 void WxEditor::UpdatePosition()
403 long line, column, pos;
404 pos = mwxInputText->GetInsertionPoint();
405 mwxInputText->PositionToXY(pos, &column, &line);
408 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
412 sprintf(mess,"%ld:%ld/%ld:%ld",
414 (long)mwxInputText->GetNumberOfLines(),
415 (long)mwxInputText->GetLineLength(line)+1);
417 // (long)mwxInputText->GetValue().length())+1;
419 mwxPosition->SetLabel(std2wx(mess));
423 //================================================================
424 //================================================================
425 void WxEditor::OnKeyUp(wxKeyEvent& event)
427 // std::cout << "U" << std::endl;
430 //================================================================
432 //================================================================
433 void WxEditor::OnKeyDown(wxKeyEvent& event)
435 // std::cout << "D" << std::endl;
436 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
437 if ( event.ControlDown() )
439 switch (event.GetKeyCode())
441 case 'n': case 'N' : New(); break;
442 case 's': case 'S' : Save(); break;
443 case 'o': case 'O' : Open(); break;
444 case 'r': case 'R' : Run(); break;
445 case 'q': case 'Q' : Quit(); break;
449 // Command completion
450 std::vector<std::string> commands;
451 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
452 int ind = sline.size();
453 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
454 if (commands.size()==1)
456 std::string com = *commands.begin();
457 for (; ind<com.size(); ++ind)
459 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
461 mwxTextCommand->TextAppend(_T(" "));
463 else if (commands.size()>1)
465 std::vector<std::string>::iterator i;
467 for (i=commands.begin();i!=commands.end();++i)
469 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
472 write(STDOUT_FILENO,"\n> ",3);
473 //for (int j=0;j<ind;++j)
475 write(STDOUT_FILENO,line,ind);
480 //================================================================
482 //================================================================
483 BEGIN_EVENT_TABLE(WxEditor, wxPanel)
484 // EVT_CHAR(WxEditor::OnKeyPress)
485 EVT_BUTTON(WxEditor::ID_ButtonNew, WxEditor::OnButtonNew)
486 EVT_BUTTON(WxEditor::ID_ButtonOpen, WxEditor::OnButtonOpen)
487 EVT_BUTTON(WxEditor::ID_ButtonSave, WxEditor::OnButtonSave)
488 EVT_BUTTON(WxEditor::ID_ButtonRun, WxEditor::OnButtonRun)
489 EVT_BUTTON(WxEditor::ID_ButtonQuit, WxEditor::OnButtonQuit)
491 //================================================================
493 void WxEditorWindow::WxGUICommandEnter(const std::string& s)
495 std::cout << "Command received : '"<<s<<"'"<<std::endl;
498 //================================================================
499 WxEditorWindow::WxEditorWindow( wxWindow *parent, wxString title, wxSize size)
500 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
504 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
506 mEditor = new WxEditor(this);
507 sizer->Add(mEditor,1,wxGROW);
509 WxGUICommand* com = new WxGUICommand(this,this);
514 // Creates and sets the parent window of all bbtk windows
515 wxWindow* top = new wxPanel(this,-1);
518 Wx::SetTopWindow(top);
524 WxEditorWindow::~WxEditorWindow()
530 #endif //_USE_WXWIDGETS_