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;
231 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
232 #if wxMAJOR_VERSION <= 2
233 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
234 _T(""),std2wx(filter),
235 wxSAVE | wxOVERWRITE_PROMPT );
237 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
238 _T(""),std2wx(filter),
239 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
242 int result_fd = fd->ShowModal();
244 // This line is need it by windows // EED
245 fd->SetReturnCode( result_fd );
247 if (fd->GetReturnCode()==wxID_OK)
249 mName = wx2std(fd->GetPath());
250 mAskFilename = false;
254 // std::cout << "-------------- CANCELLED ---------------"
259 // std::cout << "file [" << mName << "]" <<std::endl;
260 // mwxInputText->SaveFile(std2wx(mName));
262 mwxInputText->SetModified(false);
264 //================================================================
267 //================================================================
268 void WxGUITextEditorPage::SaveFile(const std::string& filename)
270 mwxInputText->SaveFile( std2wx(filename) );
272 //================================================================
285 //================================================================
287 //================================================================
290 //================================================================
291 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
292 WxGUITextEditorUser* user )
293 : wxPanel(parent, -1),
295 mFileNameFilter("*.*")
297 // m_mgr.SetManagedWindow(this);
298 // wxInitAllImageHandlers();
300 // std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
302 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
304 mwxNotebook = new wxNotebook(this,-1,
305 wxDefaultPosition, wxDefaultSize,
311 sizer->Add(mwxNotebook,1,wxGROW);
315 mwxNotebook = new wxAuiNotebook(this,
321 | wxAUI_NB_TAB_EXTERNAL_MOVE
322 | wxAUI_NB_WINDOWLIST_BUTTON
323 |wxAUI_NB_SCROLL_BUTTONS
324 // | wxAUI_NB_CLOSE_BUTTON
325 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
326 //| wxAUI_NB_CLOSE_ON_ALL_TABS
329 m_mgr.AddPane(mwxNotebook,
330 wxAuiPaneInfo().Name(wxT("notebook"))
332 .CaptionVisible(false)
333 .MinimizeButton(false)
334 .MaximizeButton(false)
336 // .Dockable(false).Float()
338 .MinSize(wxSize(100,50))
343 wxBitmap bmp_new(cc_new_xpm);
344 wxBitmap bmp_open(cc_open_xpm);
345 wxBitmap bmp_close(cc_stop_xpm);
346 wxBitmap bmp_save(cc_save_xpm);
347 wxBitmap bmp_saveas(cc_save_as_xpm);
348 wxBitmap bmp_run(cc_run_xpm);
350 wxBitmap bmp_new(new_xpm);
351 wxBitmap bmp_open(fileopen_xpm);
352 wxBitmap bmp_close(eldel_xpm);
353 wxBitmap bmp_save(filesave_xpm);
354 wxBitmap bmp_saveas(filesaveas_xpm);
355 wxBitmap bmp_run(wxart_run_xpm);
356 wxBitmap bmp_runbbi(wxart_runbbi_xpm);
357 wxBitmap bmp_graphsimple(wxart_graphsimple_xpm);
358 wxBitmap bmp_graphdetail(wxart_graphdetail_xpm);
360 mwxToolBar = new wxToolBar(this, wxID_ANY,
361 wxDefaultPosition, wxDefaultSize,
362 wxTB_FLAT | wxTB_NODIVIDER);
364 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
365 bmp_new, wxNullBitmap, wxITEM_NORMAL,
366 _T("New file"), _T("Create a new file"));
367 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
368 bmp_open, wxNullBitmap, wxITEM_NORMAL,
369 _T("Open file"), _T("This is help for new file tool"));
370 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
371 bmp_close, wxNullBitmap, wxITEM_NORMAL,
372 _T("Close file"), _T("Close current file"));
373 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
374 bmp_save, wxNullBitmap, wxITEM_NORMAL,
375 _T("Save file"), _T("Save current file"));
376 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
377 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
378 _T("Save file as"), _T("Save current file as"));
379 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
380 bmp_run, wxNullBitmap, wxITEM_NORMAL,
381 _T("Run file"), _T("Run current file"));
384 mwxToolBar->AddTool(ID_ButtonGraphSimple, _T("Graph (simple)"),
385 bmp_graphsimple, wxNullBitmap, wxITEM_NORMAL,
386 _T("Graph simple"), _T("Simple graph of actual script"));
388 mwxToolBar->AddTool(ID_ButtonGraphDetail, _T("Graph (detail)"),
389 bmp_graphdetail, wxNullBitmap, wxITEM_NORMAL,
390 _T("Graph (detail)"), _T("Detail graph of actual script"));
392 mwxToolBar->AddTool(ID_ButtonRunBBI, _T("Run external bbi "),
393 bmp_runbbi, wxNullBitmap, wxITEM_NORMAL,
394 _T("Run external bbi"), _T("Run external bbi"));
398 mwxToolBar->AddSeparator();
399 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
400 mwxToolBar->AddControl(mwxPosition);
401 mwxToolBar->Realize();
403 sizer->Add(mwxToolBar,0,wxGROW);
406 m_mgr.AddPane(mwxToolBar,
407 wxAuiPaneInfo().Name(wxT("toolBar"))
411 .MinSize(wxSize(100,50))
412 .LeftDockable(false).RightDockable(false)
426 //================================================================
428 //================================================================
429 WxGUITextEditor::~WxGUITextEditor()
433 // delete mInterpreter;
435 //================================================================
437 //================================================================
438 void WxGUITextEditor::NewPage(const std::string& filename)
440 WxGUITextEditorPage* page =
441 new WxGUITextEditorPage(mwxNotebook,this);
442 std::string name("untitled");
443 if (filename.size()!=0)
448 page->SetPageName(name);
449 std::string fname = Utilities::get_file_name(name);
450 mwxNotebook->AddPage(page,std2wx(fname),true);
451 FocusOnCurrentPage();
453 //================================================================
455 //================================================================
456 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
458 return (WxGUITextEditorPage*)
459 mwxNotebook->GetPage(mwxNotebook->GetSelection());
461 //================================================================
463 //================================================================
464 void WxGUITextEditor::FocusOnCurrentPage()
466 if (mwxNotebook->GetPageCount()==0) return;
467 GetCurrentPage()->SetFocus();
469 //================================================================
471 //================================================================
472 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
474 switch (event.GetId())
478 FocusOnCurrentPage();
482 FocusOnCurrentPage();
484 case ID_ButtonClose :
490 case ID_ButtonSaveAs :
491 if (mwxNotebook->GetPageCount()==0) break;
492 GetCurrentPage()->SetAskFilename(true);
496 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
497 mUser->WxGUITextEditorRun();
498 FocusOnCurrentPage();
501 case ID_ButtonGraphSimple :
502 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
503 mUser->WxGUITextEditorGraphSimple();
504 FocusOnCurrentPage();
507 case ID_ButtonGraphDetail :
508 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
509 mUser->WxGUITextEditorGraphDetail();
510 FocusOnCurrentPage();
513 case ID_ButtonRunBBI :
514 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
515 mUser->WxGUITextEditorRunBBI();
516 FocusOnCurrentPage();
521 //================================================================
523 //================================================================
524 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
527 //================================================================
529 //================================================================
530 void WxGUITextEditor::New()
535 //================================================================
537 //================================================================
538 void WxGUITextEditor::Open()
540 // std::cout << "-------------- OPEN ---------------"<<std::endl;
542 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
543 #if wxMAJOR_VERSION <= 2
544 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
545 _T(""),std2wx(mFileNameFilter),
546 wxOPEN | wxFILE_MUST_EXIST );
548 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
549 _T(""),std2wx(mFileNameFilter),
550 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
552 int result_fd = fd->ShowModal();
554 // This line is need it by windows //EED
555 fd->SetReturnCode( result_fd );
557 if (fd->GetReturnCode()==wxID_OK)
559 std::string filename = wx2std(fd->GetPath());
560 std::cout << "file [" << filename << "]" <<std::endl;
565 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
568 //================================================================
570 //================================================================
571 void WxGUITextEditor::Open(const std::string& filename)
576 //================================================================
579 //================================================================
580 bool WxGUITextEditor::CloseCurrentPage()
582 if (mwxNotebook->GetPageCount()==0) return true;
584 if (GetCurrentPage()->IsModified())
586 wxString mess = std2wx(GetCurrentPage()->GetPageName());
587 mess += _T(" modified. Save it ?");
589 new wxMessageDialog(this,
592 wxYES_NO | wxCANCEL | wxICON_QUESTION);
593 switch (d->ShowModal())
599 GetCurrentPage()->Save(mFileNameFilter);
604 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
605 FocusOnCurrentPage();
608 //================================================================
610 //================================================================
611 bool WxGUITextEditor::CloseAllPages()
614 while (mwxNotebook->GetPageCount()!=0)
616 if (!CloseCurrentPage())
624 //================================================================
626 //================================================================
627 void WxGUITextEditor::SaveCurrentPage()
629 if (mwxNotebook->GetPageCount()==0) return;
630 GetCurrentPage()->Save(mFileNameFilter);
631 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
632 std2wx(GetCurrentPage()->GetPageName()));
634 //================================================================
637 //================================================================
638 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
640 if (!CloseCurrentPage()) evt.Veto();
642 //================================================================
647 //================================================================
648 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
651 FocusOnCurrentPage();
653 void WxGUITextEditor::Quit()
655 std::cout << "-------------- QUIT ---------------"<<std::endl;
656 if (AskSave()) GetParent()->Close();
658 //================================================================
661 //================================================================
664 void WxGUITextEditor::Run()
666 std::cout << "-------------- RUN ---------------"<<std::endl;
668 if (!mwxSplit->IsSplit())
671 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
672 if (size<minsize) size = minsize;
673 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
676 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
677 mInterpreter->InterpretLine("reset");
680 mInterpreter->InterpretBuffer(buffer);
686 //================================================================
691 //================================================================
694 //================================================================
695 void WxGUITextEditor::UpdateInfo()
697 if (mwxNotebook->GetPageCount()==0) return;
698 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
700 long line, column, pos;
701 pos = text->GetInsertionPoint();
702 text->PositionToXY(pos, &column, &line);
705 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
709 sprintf(mess,"%ld:%ld/%ld:%ld",
711 (long)text->GetNumberOfLines(),
712 (long)text->GetLineLength(line)+1);
714 // (long)mwxInputText->GetValue().length())+1;
716 // mwxPosition->SetLabel(wxString(mess));
717 mwxPosition->SetLabel(std2wx(mess));
720 if (text->IsModified())
722 std::string title("*");
723 title += GetCurrentPage()->GetPageName();
724 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
727 //================================================================
728 //================================================================
729 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
731 // std::cout << "U" << std::endl;
734 //================================================================
736 //================================================================
737 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
739 // std::cout << "D" << std::endl;
740 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
741 if ( event.ControlDown() )
743 switch (event.GetKeyCode())
745 case 'n': case 'N' : New(); break;
746 case 's': case 'S' : SaveCurrentPage(); break;
747 case 'o': case 'O' : Open(); break;
748 // case 'r': case 'R' : Run(); break;
749 // case 'q': case 'Q' : Quit(); break;
753 //================================================================
755 //================================================================
756 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
757 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
758 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
759 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
761 //================================================================
767 //================================================================
768 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
771 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
775 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
777 mEditor = new WxGUITextEditor(this);
778 sizer->Add(mEditor,1,wxGROW);
780 // WxGUICommand* com = new WxGUICommand(this,this);
785 // parent window of all bbtk windows will be a child of this
786 // Wx::SetTopWindowParent(this);
787 Wx::SetTopWindow(this);
788 // Add the method OnWxSignal as a Wx::Signal observer
789 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
795 //================================================================
797 //================================================================
798 WxGUITextEditorWindow::~WxGUITextEditorWindow()
801 //================================================================
807 #endif //_USE_WXWIDGETS_