1 /*=========================================================================
4 Module: $RCSfile: bbtkWxEditor.cxx,v $
6 Date: $Date: 2008/03/18 15:31:51 $
7 Version: $Revision: 1.3 $
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)
96 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
98 mWxEditor->OnKeyPress(event);
101 //================================================================
106 //================================================================
107 WxEditor::WxEditor( wxWindow *parent )
108 : wxPanel(parent, -1)
110 mInterpreter = new bbtk::Interpreter();
111 //mInterpreter->SetWxEditor(this);
112 mInterpreter->SetCommandLine(true);
116 wxInitAllImageHandlers();
119 wxMenu *menuFile = new wxMenu;
120 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
122 wxMenu *menuAbout = new wxMenu;
123 menuAbout->Append( ID_Menu_About, _T("&About...") );
125 wxMenu *menuTools = new wxMenu;
126 menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
127 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
128 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
129 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
130 menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
133 wxMenuBar *menuBar = new wxMenuBar;
134 menuBar->Append( menuFile, _T("&File") );
135 menuBar->Append( menuTools, _T("&Tools") );
136 menuBar->Append( menuAbout, _T("About") );
138 SetMenuBar( menuBar );
141 SetStatusText( _T("Welcome to bbi !") );
144 // wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
145 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
146 mwxSplit = new wxSplitterWindow(this,
154 new WxTextCtrlGettingKeyEvents(mwxSplit,
160 // |wxTE_PROCESS_ENTER
165 mwxInputText->SetWxEditor(this);
167 wxFont* FixedFont = new wxFont(10,
173 mwxInputTextAttr = new wxTextAttr;
174 mwxInputTextAttr->SetFont(*FixedFont);
176 // sizer->AddGrowableCol(0);
177 // sizer->Add(mwxInputText,1,wxGROW);
179 mwxInputText->SetFocus();
182 new WxTextCtrlGettingKeyEvents(mwxSplit,
184 _T(""),wxDefaultPosition,
185 wxDefaultSize, //HistorySize,
189 mwxOutputText->SetWxEditor(this);
191 mwxOutputTextAttr = new wxTextAttr;
192 mwxOutputTextAttr->SetFont(*FixedFont);
193 mwxOutputText->Hide();
195 // sizer->Add(mwxOutputText,1,wxGROW);
196 sizer->Add(mwxSplit,1,wxGROW);
197 mwxSplit->Initialize(mwxInputText);
200 wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
201 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
203 wxBitmap bmp_new(cc_new_xpm);
204 mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New") );
205 btnsSizer->Add( mwxButtonNew );
206 wxBitmap bmp_open(cc_open_xpm);
207 mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open") );
208 btnsSizer->Add( mwxButtonOpen );
209 wxBitmap bmp_save(cc_save_xpm);
210 mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save") );
211 btnsSizer->Add( mwxButtonSave );
212 wxBitmap bmp_run(cc_run_xpm);
213 mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run") );
214 btnsSizer->Add( mwxButtonRun );
215 wxBitmap bmp_quit(cc_exit_xpm);
216 mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") );
217 btnsSizer->Add( mwxButtonQuit );
220 mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T(""));
221 btnsSizer->Add( mwxPosition );
224 btnsCtrlPanel->SetSizer(btnsSizer);
225 sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW
228 // Redirection of std::cout and std::cerr to mwxOutputText and printf
230 new WxStreamRedirector(std::cout,mwxOutputText,*wxBLACK,true);
232 new WxStreamRedirector(std::cerr,mwxOutputText,*wxGREEN,true);
240 //================================================================
242 //================================================================
243 WxEditor::~WxEditor()
246 delete mRedirect_cout;
247 delete mRedirect_cerr;
249 //================================================================
252 //================================================================
253 void WxEditor::OnButtonOpen(wxCommandEvent& event) { Open(); }
254 void WxEditor::Open()
256 std::cout << "-------------- OPEN ---------------"<<std::endl;
258 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
262 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
264 wxOPEN | wxFILE_MUST_EXIST );
267 if (fd->GetReturnCode()==wxID_OK)
269 std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
270 mwxInputText->LoadFile(fd->GetPath());
274 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
277 //================================================================
279 //================================================================
280 void WxEditor::Open(const std::string& filename)
282 mwxInputText->LoadFile(std2wx(filename));
283 mwxInputText->SetModified(false);
285 //================================================================
287 //================================================================
288 void WxEditor::OnButtonSave(wxCommandEvent& event) { Save(); }
289 void WxEditor::Save()
291 std::cout << "-------------- SAVE ---------------"<<std::endl;
292 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
294 wxSAVE | wxOVERWRITE_PROMPT );
297 if (fd->GetReturnCode()==wxID_OK)
299 std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
300 mwxInputText->SaveFile(fd->GetPath());
301 mwxInputText->SetModified(false);
305 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
308 //================================================================
310 //================================================================
311 void WxEditor::OnButtonQuit(wxCommandEvent& event) { Quit(); }
312 void WxEditor::Quit()
314 std::cout << "-------------- QUIT ---------------"<<std::endl;
315 if (AskSave()) GetParent()->Close();
317 //================================================================
320 //================================================================
321 void WxEditor::OnButtonRun(wxCommandEvent& event) { Run(); }
324 std::cout << "-------------- RUN ---------------"<<std::endl;
326 if (!mwxSplit->IsSplit())
329 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
330 if (size<minsize) size = minsize;
331 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
334 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
336 mInterpreter->InterpretLine("reset",com);
339 mInterpreter->InterpretBuffer(buffer);
345 //================================================================
347 //================================================================
348 bool WxEditor::AskSave()
350 if (!mwxInputText->IsModified()) return true;
351 wxMessageDialog* d = new wxMessageDialog(this,
352 _T("Buffer modified. Save it ?"),
354 wxYES_NO | wxCANCEL | wxICON_QUESTION);
355 switch (d->ShowModal())
357 case wxID_YES : Save();
358 case wxID_NO : return true;
359 case wxID_CANCEL : return false;
364 //================================================================
366 //================================================================
367 void WxEditor::OnButtonNew(wxCommandEvent& event) { New(); }
370 std::cout << "-------------- NEW ---------------" << std::endl;
372 mwxInputText->Clear();
374 //================================================================
376 //================================================================
377 void WxEditor::OnKeyPress(wxKeyEvent& event)
379 long line, column, pos = mwxInputText->GetInsertionPoint();
380 mwxInputText->PositionToXY(pos, &column, &line);
382 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
386 sprintf(mess,"%ld:%ld / %ld:%ld (%ld char)",
388 (long)mwxInputText->GetNumberOfLines(),
389 (long)mwxInputText->GetLineLength(line),
390 (long)mwxInputText->GetValue().length());
392 mwxPosition->SetLabel(wxString(mess));
393 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
394 if ( event.ControlDown() )
396 switch (event.GetKeyCode())
398 case 'n': case 'N' : New(); break;
399 case 's': case 'S' : Save(); break;
400 case 'o': case 'O' : Open(); break;
401 case 'r': case 'R' : Run(); break;
402 case 'q': case 'Q' : Quit(); break;
406 // Command completion
407 std::vector<std::string> commands;
408 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
409 int ind = sline.size();
410 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
411 if (commands.size()==1)
413 std::string com = *commands.begin();
414 for (; ind<com.size(); ++ind)
416 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
418 mwxTextCommand->TextAppend(_T(" "));
420 else if (commands.size()>1)
422 std::vector<std::string>::iterator i;
424 for (i=commands.begin();i!=commands.end();++i)
426 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
429 write(STDOUT_FILENO,"\n> ",3);
430 //for (int j=0;j<ind;++j)
432 write(STDOUT_FILENO,line,ind);
437 //================================================================
439 //================================================================
440 BEGIN_EVENT_TABLE(WxEditor, wxPanel)
441 // EVT_CHAR(WxEditor::OnKeyPress)
442 EVT_BUTTON(WxEditor::ID_ButtonNew, WxEditor::OnButtonNew)
443 EVT_BUTTON(WxEditor::ID_ButtonOpen, WxEditor::OnButtonOpen)
444 EVT_BUTTON(WxEditor::ID_ButtonSave, WxEditor::OnButtonSave)
445 EVT_BUTTON(WxEditor::ID_ButtonRun, WxEditor::OnButtonRun)
446 EVT_BUTTON(WxEditor::ID_ButtonQuit, WxEditor::OnButtonQuit)
448 //================================================================
451 //================================================================
452 WxEditorWindow::WxEditorWindow( wxWindow *parent, wxString title, wxSize size)
453 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
455 mEditor = new WxEditor(this);
457 //wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
458 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
459 //sizer->AddGrowableCol(0);
460 sizer->Add(mEditor,1,wxGROW);
462 // Creates and sets the parent window of all bbtk windows
463 wxWindow* top = new wxPanel(this,-1);//,_T("top"));
465 //new wxFrame(this,-1,_T("bbtk"),
466 // wxDefaultPosition,
468 // wxFRAME_TOOL_WINDOW) ;//wxMINIMIZE_BOX);
470 Wx::SetTopWindow(top);
477 WxEditorWindow::~WxEditorWindow()
483 #endif //_USE_WXWIDGETS_