2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.22 $
34 =========================================================================*/
40 * \brief Short description in one line
42 * Long description which
43 * can span multiple lines
55 #ifdef _USE_WXWIDGETS_
58 #include "bbtkWxGUITextEditor.h"
59 #include "bbtkWxBlackBox.h"
60 #include "bbtkConfigurationFile.h"
61 #include "bbtkUtilities.h"
63 //#include "icons/cc_new.xpm"
64 //#include "icons/cc_open.xpm"
65 //#include "icons/cc_stop.xpm"
66 //#include "icons/cc_save.xpm"
67 //#include "icons/cc_save_as.xpm"
68 //#include "icons/cc_run.xpm"
69 //#include "icons/cc_exit.xpm"
71 #include "../data/icons/wxart_new.xpm"
72 #include "../data/icons/wxart_fileopen.xpm"
73 #include "../data/icons/wxart_filesave.xpm"
74 #include "../data/icons/wxart_filesaveas.xpm"
75 //#include "../data/icons/wxart_exefile.xpm"
76 //#include "../data/icons/wxart_delete.xpm"
77 //#include "../data/icons/wxart_down.xpm"
78 #include "../data/icons/wxart_run.xpm"
79 #include "../data/icons/wxart_runbbi.xpm"
80 #include "../data/icons/wxart_graphsimple.xpm"
81 #include "../data/icons/wxart_graphdetail.xpm"
82 #include "../data/icons/wxart_eldel.xpm"
88 //================================================================
89 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
92 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
93 const wxPoint &pos, const wxSize &size, int style = 0)
94 : wxTextCtrl(parent, id, value, pos, size, style)
98 void SetWxGUITextEditor(WxGUITextEditor* e)
99 { mWxGUITextEditor = e; }
101 void OnKeyDown(wxKeyEvent& event);
102 void OnKeyUp(wxKeyEvent& event);
103 void OnChar(wxKeyEvent& event);
105 WxGUITextEditor* mWxGUITextEditor;
107 DECLARE_EVENT_TABLE()
110 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
111 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
112 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
113 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
117 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
122 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
124 mWxGUITextEditor->OnKeyUp(event);
128 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
130 mWxGUITextEditor->OnKeyDown(event);
133 //================================================================
136 //================================================================
137 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
138 EVT_CLOSE(WxGUITextEditorPage::OnClose)
142 //================================================================
143 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
144 WxGUITextEditor* editor)
145 : wxPanel(parent,-1),
151 // std::cout << "WxGUITextEditorPage::WxGUITextEditorPage("<<mName<<")"<<std::endl;
153 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
157 new WxTextCtrlGettingKeyEvents(this,
163 // |wxTE_PROCESS_ENTER
168 mwxInputText->SetWxGUITextEditor(mEditor);
170 new wxTextCtrl(this,-1,_T(""),
174 // |wxTE_PROCESS_ENTER
175 // | wxTE_PROCESS_TAB
180 mFixedFont = new wxFont(10,
186 mwxInputTextAttr = new wxTextAttr;
187 mwxInputTextAttr->SetFont(*mFixedFont);
189 sizer->Add(mwxInputText,1,wxGROW);
194 //================================================================
196 //================================================================
197 WxGUITextEditorPage::~WxGUITextEditorPage()
199 delete mwxInputTextAttr;
202 //================================================================
204 bool WxGUITextEditorPage::IsModified()
205 { return mwxInputText->IsModified(); }
207 std::string WxGUITextEditorPage::GetText()
209 return wx2std(GetTextCtrl()->GetValue());
212 //================================================================
213 void WxGUITextEditorPage::Load(const std::string& filename)
215 // std::cout << "-------------- LOAD ---------------"<<std::endl;
216 // std::cout << "'" << filename << "'"<<std::endl;
217 //std::string oldFilename = mFilename;
219 mAskFilename = false;
220 mwxInputText->LoadFile(std2wx(mName));
222 //================================================================
224 //================================================================
225 void WxGUITextEditorPage::Save(const std::string& filter)
227 // std::cout << "-------------- SAVE ---------------"<<std::endl;
230 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
231 _T(""),std2wx(filter),
232 wxSAVE | wxOVERWRITE_PROMPT );
233 int result_fd = fd->ShowModal();
235 // This line is need it by windows // EED
236 fd->SetReturnCode( result_fd );
238 if (fd->GetReturnCode()==wxID_OK)
240 mName = wx2std(fd->GetPath());
241 mAskFilename = false;
245 // std::cout << "-------------- CANCELLED ---------------"
250 // std::cout << "file [" << mName << "]" <<std::endl;
251 // mwxInputText->SaveFile(std2wx(mName));
253 mwxInputText->SetModified(false);
255 //================================================================
258 //================================================================
259 void WxGUITextEditorPage::SaveFile(const std::string& filename)
261 mwxInputText->SaveFile( std2wx(filename) );
263 //================================================================
276 //================================================================
278 //================================================================
281 //================================================================
282 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
283 WxGUITextEditorUser* user )
284 : wxPanel(parent, -1),
286 mFileNameFilter("*.*")
288 // m_mgr.SetManagedWindow(this);
289 // wxInitAllImageHandlers();
291 // std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
293 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
295 mwxNotebook = new wxNotebook(this,-1,
296 wxDefaultPosition, wxDefaultSize,
302 sizer->Add(mwxNotebook,1,wxGROW);
306 mwxNotebook = new wxAuiNotebook(this,
312 | wxAUI_NB_TAB_EXTERNAL_MOVE
313 | wxAUI_NB_WINDOWLIST_BUTTON
314 |wxAUI_NB_SCROLL_BUTTONS
315 // | wxAUI_NB_CLOSE_BUTTON
316 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
317 //| wxAUI_NB_CLOSE_ON_ALL_TABS
320 m_mgr.AddPane(mwxNotebook,
321 wxAuiPaneInfo().Name(wxT("notebook"))
323 .CaptionVisible(false)
324 .MinimizeButton(false)
325 .MaximizeButton(false)
327 // .Dockable(false).Float()
329 .MinSize(wxSize(100,50))
334 wxBitmap bmp_new(cc_new_xpm);
335 wxBitmap bmp_open(cc_open_xpm);
336 wxBitmap bmp_close(cc_stop_xpm);
337 wxBitmap bmp_save(cc_save_xpm);
338 wxBitmap bmp_saveas(cc_save_as_xpm);
339 wxBitmap bmp_run(cc_run_xpm);
341 wxBitmap bmp_new(new_xpm);
342 wxBitmap bmp_open(fileopen_xpm);
343 wxBitmap bmp_close(eldel_xpm);
344 wxBitmap bmp_save(filesave_xpm);
345 wxBitmap bmp_saveas(filesaveas_xpm);
346 wxBitmap bmp_run(wxart_run_xpm);
347 wxBitmap bmp_runbbi(wxart_runbbi_xpm);
348 wxBitmap bmp_graphsimple(wxart_graphsimple_xpm);
349 wxBitmap bmp_graphdetail(wxart_graphdetail_xpm);
351 mwxToolBar = new wxToolBar(this, wxID_ANY,
352 wxDefaultPosition, wxDefaultSize,
353 wxTB_FLAT | wxTB_NODIVIDER);
355 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
356 bmp_new, wxNullBitmap, wxITEM_NORMAL,
357 _T("New file"), _T("Create a new file"));
358 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
359 bmp_open, wxNullBitmap, wxITEM_NORMAL,
360 _T("Open file"), _T("This is help for new file tool"));
361 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
362 bmp_close, wxNullBitmap, wxITEM_NORMAL,
363 _T("Close file"), _T("Close current file"));
364 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
365 bmp_save, wxNullBitmap, wxITEM_NORMAL,
366 _T("Save file"), _T("Save current file"));
367 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
368 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
369 _T("Save file as"), _T("Save current file as"));
370 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
371 bmp_run, wxNullBitmap, wxITEM_NORMAL,
372 _T("Run file"), _T("Run current file"));
375 mwxToolBar->AddTool(ID_ButtonGraphSimple, _T("Graph (simple)"),
376 bmp_graphsimple, wxNullBitmap, wxITEM_NORMAL,
377 _T("Graph simple"), _T("Simple graph of actual script"));
379 mwxToolBar->AddTool(ID_ButtonGraphDetail, _T("Graph (detail)"),
380 bmp_graphdetail, wxNullBitmap, wxITEM_NORMAL,
381 _T("Graph (detail)"), _T("Detail graph of actual script"));
383 mwxToolBar->AddTool(ID_ButtonRunBBI, _T("Run external bbi "),
384 bmp_runbbi, wxNullBitmap, wxITEM_NORMAL,
385 _T("Run external bbi"), _T("Run external bbi"));
389 mwxToolBar->AddSeparator();
390 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
391 mwxToolBar->AddControl(mwxPosition);
392 mwxToolBar->Realize();
394 sizer->Add(mwxToolBar,0,wxGROW);
397 m_mgr.AddPane(mwxToolBar,
398 wxAuiPaneInfo().Name(wxT("toolBar"))
402 .MinSize(wxSize(100,50))
403 .LeftDockable(false).RightDockable(false)
417 //================================================================
419 //================================================================
420 WxGUITextEditor::~WxGUITextEditor()
424 // delete mInterpreter;
426 //================================================================
428 //================================================================
429 void WxGUITextEditor::NewPage(const std::string& filename)
431 WxGUITextEditorPage* page =
432 new WxGUITextEditorPage(mwxNotebook,this);
433 std::string name("untitled");
434 if (filename.size()!=0)
439 page->SetPageName(name);
440 std::string fname = Utilities::get_file_name(name);
441 mwxNotebook->AddPage(page,std2wx(fname),true);
442 FocusOnCurrentPage();
444 //================================================================
446 //================================================================
447 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
449 return (WxGUITextEditorPage*)
450 mwxNotebook->GetPage(mwxNotebook->GetSelection());
452 //================================================================
454 //================================================================
455 void WxGUITextEditor::FocusOnCurrentPage()
457 if (mwxNotebook->GetPageCount()==0) return;
458 GetCurrentPage()->SetFocus();
460 //================================================================
462 //================================================================
463 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
465 switch (event.GetId())
469 FocusOnCurrentPage();
473 FocusOnCurrentPage();
475 case ID_ButtonClose :
481 case ID_ButtonSaveAs :
482 if (mwxNotebook->GetPageCount()==0) break;
483 GetCurrentPage()->SetAskFilename(true);
487 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
488 mUser->WxGUITextEditorRun();
489 FocusOnCurrentPage();
492 case ID_ButtonGraphSimple :
493 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
494 mUser->WxGUITextEditorGraphSimple();
495 FocusOnCurrentPage();
498 case ID_ButtonGraphDetail :
499 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
500 mUser->WxGUITextEditorGraphDetail();
501 FocusOnCurrentPage();
504 case ID_ButtonRunBBI :
505 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
506 mUser->WxGUITextEditorRunBBI();
507 FocusOnCurrentPage();
512 //================================================================
514 //================================================================
515 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
518 //================================================================
520 //================================================================
521 void WxGUITextEditor::New()
526 //================================================================
528 //================================================================
529 void WxGUITextEditor::Open()
531 // std::cout << "-------------- OPEN ---------------"<<std::endl;
533 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
534 _T(""),std2wx(mFileNameFilter),
535 wxOPEN | wxFILE_MUST_EXIST );
536 int result_fd = fd->ShowModal();
538 // This line is need it by windows //EED
539 fd->SetReturnCode( result_fd );
541 if (fd->GetReturnCode()==wxID_OK)
543 std::string filename = wx2std(fd->GetPath());
544 std::cout << "file [" << filename << "]" <<std::endl;
549 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
552 //================================================================
554 //================================================================
555 void WxGUITextEditor::Open(const std::string& filename)
560 //================================================================
563 //================================================================
564 bool WxGUITextEditor::CloseCurrentPage()
566 if (mwxNotebook->GetPageCount()==0) return true;
568 if (GetCurrentPage()->IsModified())
570 wxString mess = std2wx(GetCurrentPage()->GetPageName());
571 mess += _T(" modified. Save it ?");
573 new wxMessageDialog(this,
576 wxYES_NO | wxCANCEL | wxICON_QUESTION);
577 switch (d->ShowModal())
583 GetCurrentPage()->Save(mFileNameFilter);
588 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
589 FocusOnCurrentPage();
592 //================================================================
594 //================================================================
595 bool WxGUITextEditor::CloseAllPages()
598 while (mwxNotebook->GetPageCount()!=0)
600 if (!CloseCurrentPage())
608 //================================================================
610 //================================================================
611 void WxGUITextEditor::SaveCurrentPage()
613 if (mwxNotebook->GetPageCount()==0) return;
614 GetCurrentPage()->Save(mFileNameFilter);
615 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
616 std2wx(GetCurrentPage()->GetPageName()));
618 //================================================================
621 //================================================================
622 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
624 if (!CloseCurrentPage()) evt.Veto();
626 //================================================================
631 //================================================================
632 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
635 FocusOnCurrentPage();
637 void WxGUITextEditor::Quit()
639 std::cout << "-------------- QUIT ---------------"<<std::endl;
640 if (AskSave()) GetParent()->Close();
642 //================================================================
645 //================================================================
648 void WxGUITextEditor::Run()
650 std::cout << "-------------- RUN ---------------"<<std::endl;
652 if (!mwxSplit->IsSplit())
655 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
656 if (size<minsize) size = minsize;
657 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
660 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
661 mInterpreter->InterpretLine("reset");
664 mInterpreter->InterpretBuffer(buffer);
670 //================================================================
675 //================================================================
678 //================================================================
679 void WxGUITextEditor::UpdateInfo()
681 if (mwxNotebook->GetPageCount()==0) return;
682 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
684 long line, column, pos;
685 pos = text->GetInsertionPoint();
686 text->PositionToXY(pos, &column, &line);
689 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
693 sprintf(mess,"%ld:%ld/%ld:%ld",
695 (long)text->GetNumberOfLines(),
696 (long)text->GetLineLength(line)+1);
698 // (long)mwxInputText->GetValue().length())+1;
700 // mwxPosition->SetLabel(wxString(mess));
701 mwxPosition->SetLabel(std2wx(mess));
704 if (text->IsModified())
706 std::string title("*");
707 title += GetCurrentPage()->GetPageName();
708 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
711 //================================================================
712 //================================================================
713 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
715 // std::cout << "U" << std::endl;
718 //================================================================
720 //================================================================
721 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
723 // std::cout << "D" << std::endl;
724 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
725 if ( event.ControlDown() )
727 switch (event.GetKeyCode())
729 case 'n': case 'N' : New(); break;
730 case 's': case 'S' : SaveCurrentPage(); break;
731 case 'o': case 'O' : Open(); break;
732 // case 'r': case 'R' : Run(); break;
733 // case 'q': case 'Q' : Quit(); break;
737 //================================================================
739 //================================================================
740 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
741 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
742 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
743 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
745 //================================================================
751 //================================================================
752 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
755 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
759 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
761 mEditor = new WxGUITextEditor(this);
762 sizer->Add(mEditor,1,wxGROW);
764 // WxGUICommand* com = new WxGUICommand(this,this);
769 // parent window of all bbtk windows will be a child of this
770 // Wx::SetTopWindowParent(this);
771 Wx::SetTopWindow(this);
772 // Add the method OnWxSignal as a Wx::Signal observer
773 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
779 //================================================================
781 //================================================================
782 WxGUITextEditorWindow::~WxGUITextEditorWindow()
785 //================================================================
791 #endif //_USE_WXWIDGETS_