1 /*=========================================================================
4 Module: $RCSfile: bbtkWxEditor.cxx,v $
6 Date: $Date: 2008/03/19 08:03:59 $
7 Version: $Revision: 1.4 $
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"
57 //================================================================
58 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
61 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
62 const wxPoint &pos, const wxSize &size, int style = 0)
63 : wxTextCtrl(parent, id, value, pos, size, style)
67 void SetWxEditor(WxEditor* e) { mWxEditor = e; }
69 void OnKeyDown(wxKeyEvent& event);
70 void OnKeyUp(wxKeyEvent& event);
71 void OnChar(wxKeyEvent& event);
78 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
79 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
80 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
81 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
86 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
91 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
93 mWxEditor->OnKeyUp(event);
97 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
99 mWxEditor->OnKeyDown(event);
102 //================================================================
107 //================================================================
108 WxEditor::WxEditor( wxWindow *parent )
109 : wxPanel(parent, -1)
111 mInterpreter = new bbtk::Interpreter();
112 //mInterpreter->SetWxEditor(this);
113 mInterpreter->SetCommandLine(true);
117 wxInitAllImageHandlers();
120 wxMenu *menuFile = new wxMenu;
121 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
123 wxMenu *menuAbout = new wxMenu;
124 menuAbout->Append( ID_Menu_About, _T("&About...") );
126 wxMenu *menuTools = new wxMenu;
127 menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
128 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
129 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
130 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
131 menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
134 wxMenuBar *menuBar = new wxMenuBar;
135 menuBar->Append( menuFile, _T("&File") );
136 menuBar->Append( menuTools, _T("&Tools") );
137 menuBar->Append( menuAbout, _T("About") );
139 SetMenuBar( menuBar );
142 SetStatusText( _T("Welcome to bbi !") );
145 // wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
146 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
147 mwxSplit = new wxSplitterWindow(this,
155 new WxTextCtrlGettingKeyEvents(mwxSplit,
161 // |wxTE_PROCESS_ENTER
166 mwxInputText->SetWxEditor(this);
168 wxFont* FixedFont = new wxFont(10,
174 mwxInputTextAttr = new wxTextAttr;
175 mwxInputTextAttr->SetFont(*FixedFont);
177 // sizer->AddGrowableCol(0);
178 // sizer->Add(mwxInputText,1,wxGROW);
181 new WxTextCtrlGettingKeyEvents(mwxSplit,
183 _T(""),wxDefaultPosition,
184 wxDefaultSize, //HistorySize,
188 mwxOutputText->SetWxEditor(this);
190 mwxOutputTextAttr = new wxTextAttr;
191 mwxOutputTextAttr->SetFont(*FixedFont);
192 mwxOutputText->Hide();
194 // sizer->Add(mwxOutputText,1,wxGROW);
195 sizer->Add(mwxSplit,1,wxGROW);
196 mwxSplit->Initialize(mwxInputText);
199 wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
200 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
202 wxBitmap bmp_new(cc_new_xpm);
203 mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New") );
204 btnsSizer->Add( mwxButtonNew );
205 wxBitmap bmp_open(cc_open_xpm);
206 mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open") );
207 btnsSizer->Add( mwxButtonOpen );
208 wxBitmap bmp_save(cc_save_xpm);
209 mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save") );
210 btnsSizer->Add( mwxButtonSave );
211 wxBitmap bmp_run(cc_run_xpm);
212 mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run") );
213 btnsSizer->Add( mwxButtonRun );
214 wxBitmap bmp_quit(cc_exit_xpm);
215 mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") );
216 btnsSizer->Add( mwxButtonQuit );
219 mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T(""));
220 btnsSizer->Add( mwxPosition );
223 btnsCtrlPanel->SetSizer(btnsSizer);
224 sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW
227 // Redirection of std::cout and std::cerr to mwxOutputText and printf
229 new WxStreamRedirector(std::cout,mwxOutputText,*wxBLACK,true);
231 new WxStreamRedirector(std::cerr,mwxOutputText,*wxGREEN,true);
234 mwxInputText->SetFocus();
240 //================================================================
242 //================================================================
243 WxEditor::~WxEditor()
246 delete mRedirect_cout;
247 delete mRedirect_cerr;
249 //================================================================
252 //================================================================
253 void WxEditor::OnButtonOpen(wxCommandEvent& event)
256 mwxInputText->SetFocus();
258 void WxEditor::Open()
260 std::cout << "-------------- OPEN ---------------"<<std::endl;
262 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
266 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
268 wxOPEN | wxFILE_MUST_EXIST );
271 if (fd->GetReturnCode()==wxID_OK)
273 std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
274 mwxInputText->LoadFile(fd->GetPath());
278 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
281 //================================================================
283 //================================================================
284 void WxEditor::Open(const std::string& filename)
286 mwxInputText->LoadFile(std2wx(filename));
287 mwxInputText->SetModified(false);
290 //================================================================
292 //================================================================
293 void WxEditor::OnButtonSave(wxCommandEvent& event)
296 mwxInputText->SetFocus();
298 void WxEditor::Save()
300 std::cout << "-------------- SAVE ---------------"<<std::endl;
301 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
303 wxSAVE | wxOVERWRITE_PROMPT );
306 if (fd->GetReturnCode()==wxID_OK)
308 std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
309 mwxInputText->SaveFile(fd->GetPath());
310 mwxInputText->SetModified(false);
314 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
317 //================================================================
319 //================================================================
320 void WxEditor::OnButtonQuit(wxCommandEvent& event)
323 mwxInputText->SetFocus();
325 void WxEditor::Quit()
327 std::cout << "-------------- QUIT ---------------"<<std::endl;
328 if (AskSave()) GetParent()->Close();
330 //================================================================
333 //================================================================
334 void WxEditor::OnButtonRun(wxCommandEvent& event)
337 mwxInputText->SetFocus();
341 std::cout << "-------------- RUN ---------------"<<std::endl;
343 if (!mwxSplit->IsSplit())
346 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
347 if (size<minsize) size = minsize;
348 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
351 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
353 mInterpreter->InterpretLine("reset",com);
356 mInterpreter->InterpretBuffer(buffer);
362 //================================================================
364 //================================================================
365 bool WxEditor::AskSave()
367 if (!mwxInputText->IsModified()) return true;
368 wxMessageDialog* d = new wxMessageDialog(this,
369 _T("Buffer modified. Save it ?"),
371 wxYES_NO | wxCANCEL | wxICON_QUESTION);
372 switch (d->ShowModal())
374 case wxID_YES : Save();
375 case wxID_NO : return true;
376 case wxID_CANCEL : return false;
381 //================================================================
383 //================================================================
384 void WxEditor::OnButtonNew(wxCommandEvent& event)
387 mwxInputText->SetFocus();
391 std::cout << "-------------- NEW ---------------" << std::endl;
393 mwxInputText->Clear();
396 //================================================================
398 //================================================================
399 void WxEditor::UpdatePosition()
402 long line, column, pos;
403 pos = mwxInputText->GetInsertionPoint();
404 mwxInputText->PositionToXY(pos, &column, &line);
407 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
411 sprintf(mess,"%ld:%ld/%ld:%ld",
413 (long)mwxInputText->GetNumberOfLines(),
414 (long)mwxInputText->GetLineLength(line)+1);
416 // (long)mwxInputText->GetValue().length())+1;
418 mwxPosition->SetLabel(wxString(mess));
422 //================================================================
423 //================================================================
424 void WxEditor::OnKeyUp(wxKeyEvent& event)
426 // std::cout << "U" << std::endl;
429 //================================================================
431 //================================================================
432 void WxEditor::OnKeyDown(wxKeyEvent& event)
434 // std::cout << "D" << std::endl;
435 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
436 if ( event.ControlDown() )
438 switch (event.GetKeyCode())
440 case 'n': case 'N' : New(); break;
441 case 's': case 'S' : Save(); break;
442 case 'o': case 'O' : Open(); break;
443 case 'r': case 'R' : Run(); break;
444 case 'q': case 'Q' : Quit(); break;
448 // Command completion
449 std::vector<std::string> commands;
450 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
451 int ind = sline.size();
452 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
453 if (commands.size()==1)
455 std::string com = *commands.begin();
456 for (; ind<com.size(); ++ind)
458 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
460 mwxTextCommand->TextAppend(_T(" "));
462 else if (commands.size()>1)
464 std::vector<std::string>::iterator i;
466 for (i=commands.begin();i!=commands.end();++i)
468 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
471 write(STDOUT_FILENO,"\n> ",3);
472 //for (int j=0;j<ind;++j)
474 write(STDOUT_FILENO,line,ind);
479 //================================================================
481 //================================================================
482 BEGIN_EVENT_TABLE(WxEditor, wxPanel)
483 // EVT_CHAR(WxEditor::OnKeyPress)
484 EVT_BUTTON(WxEditor::ID_ButtonNew, WxEditor::OnButtonNew)
485 EVT_BUTTON(WxEditor::ID_ButtonOpen, WxEditor::OnButtonOpen)
486 EVT_BUTTON(WxEditor::ID_ButtonSave, WxEditor::OnButtonSave)
487 EVT_BUTTON(WxEditor::ID_ButtonRun, WxEditor::OnButtonRun)
488 EVT_BUTTON(WxEditor::ID_ButtonQuit, WxEditor::OnButtonQuit)
490 //================================================================
493 //================================================================
494 WxEditorWindow::WxEditorWindow( wxWindow *parent, wxString title, wxSize size)
495 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
497 mEditor = new WxEditor(this);
499 //wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
500 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
501 //sizer->AddGrowableCol(0);
502 sizer->Add(mEditor,1,wxGROW);
504 // Creates and sets the parent window of all bbtk windows
505 wxWindow* top = new wxPanel(this,-1);//,_T("top"));
507 //new wxFrame(this,-1,_T("bbtk"),
508 // wxDefaultPosition,
510 // wxFRAME_TOOL_WINDOW) ;//wxMINIMIZE_BOX);
512 Wx::SetTopWindow(top);
519 WxEditorWindow::~WxEditorWindow()
525 #endif //_USE_WXWIDGETS_