1 /*=========================================================================
3 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
5 Date: $Date: 2009/05/28 08:12:07 $
6 Version: $Revision: 1.20 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 * \brief Short description in one line
35 * Long description which
36 * can span multiple lines
48 #ifdef _USE_WXWIDGETS_
51 #include "bbtkWxGUITextEditor.h"
52 #include "bbtkWxBlackBox.h"
53 #include "bbtkConfigurationFile.h"
54 #include "bbtkUtilities.h"
56 //#include "icons/cc_new.xpm"
57 //#include "icons/cc_open.xpm"
58 //#include "icons/cc_stop.xpm"
59 //#include "icons/cc_save.xpm"
60 //#include "icons/cc_save_as.xpm"
61 //#include "icons/cc_run.xpm"
62 //#include "icons/cc_exit.xpm"
64 #include "../data/icons/wxart_new.xpm"
65 #include "../data/icons/wxart_fileopen.xpm"
66 #include "../data/icons/wxart_filesave.xpm"
67 #include "../data/icons/wxart_filesaveas.xpm"
68 //#include "../data/icons/wxart_exefile.xpm"
69 //#include "../data/icons/wxart_delete.xpm"
70 //#include "../data/icons/wxart_down.xpm"
71 #include "../data/icons/wxart_run.xpm"
72 #include "../data/icons/wxart_runbbi.xpm"
73 #include "../data/icons/wxart_graphsimple.xpm"
74 #include "../data/icons/wxart_graphdetail.xpm"
75 #include "../data/icons/wxart_eldel.xpm"
81 //================================================================
82 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
85 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
86 const wxPoint &pos, const wxSize &size, int style = 0)
87 : wxTextCtrl(parent, id, value, pos, size, style)
91 void SetWxGUITextEditor(WxGUITextEditor* e)
92 { mWxGUITextEditor = e; }
94 void OnKeyDown(wxKeyEvent& event);
95 void OnKeyUp(wxKeyEvent& event);
96 void OnChar(wxKeyEvent& event);
98 WxGUITextEditor* mWxGUITextEditor;
100 DECLARE_EVENT_TABLE()
103 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
104 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
105 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
106 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
110 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
115 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
117 mWxGUITextEditor->OnKeyUp(event);
121 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
123 mWxGUITextEditor->OnKeyDown(event);
126 //================================================================
129 //================================================================
130 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
131 EVT_CLOSE(WxGUITextEditorPage::OnClose)
135 //================================================================
136 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
137 WxGUITextEditor* editor)
138 : wxPanel(parent,-1),
144 // std::cout << "WxGUITextEditorPage::WxGUITextEditorPage("<<mName<<")"<<std::endl;
146 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
150 new WxTextCtrlGettingKeyEvents(this,
156 // |wxTE_PROCESS_ENTER
161 mwxInputText->SetWxGUITextEditor(mEditor);
163 new wxTextCtrl(this,-1,_T(""),
167 // |wxTE_PROCESS_ENTER
168 // | wxTE_PROCESS_TAB
173 wxFont* FixedFont = new wxFont(10,
179 mwxInputTextAttr = new wxTextAttr;
180 mwxInputTextAttr->SetFont(*FixedFont);
182 sizer->Add(mwxInputText,1,wxGROW);
187 //================================================================
189 //================================================================
190 WxGUITextEditorPage::~WxGUITextEditorPage()
193 //================================================================
195 bool WxGUITextEditorPage::IsModified()
196 { return mwxInputText->IsModified(); }
198 std::string WxGUITextEditorPage::GetText()
200 return wx2std(GetTextCtrl()->GetValue());
203 //================================================================
204 void WxGUITextEditorPage::Load(const std::string& filename)
206 // std::cout << "-------------- LOAD ---------------"<<std::endl;
207 // std::cout << "'" << filename << "'"<<std::endl;
208 //std::string oldFilename = mFilename;
210 mAskFilename = false;
211 mwxInputText->LoadFile(std2wx(mName));
213 //================================================================
215 //================================================================
216 void WxGUITextEditorPage::Save(const std::string& filter)
218 // std::cout << "-------------- SAVE ---------------"<<std::endl;
221 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
222 _T(""),std2wx(filter),
223 wxSAVE | wxOVERWRITE_PROMPT );
224 int result_fd = fd->ShowModal();
226 // This line is need it by windows // EED
227 fd->SetReturnCode( result_fd );
229 if (fd->GetReturnCode()==wxID_OK)
231 mName = wx2std(fd->GetPath());
232 mAskFilename = false;
236 // std::cout << "-------------- CANCELLED ---------------"
241 // std::cout << "file [" << mName << "]" <<std::endl;
242 // mwxInputText->SaveFile(std2wx(mName));
244 mwxInputText->SetModified(false);
246 //================================================================
249 //================================================================
250 void WxGUITextEditorPage::SaveFile(const std::string& filename)
252 mwxInputText->SaveFile( std2wx(filename) );
254 //================================================================
267 //================================================================
269 //================================================================
272 //================================================================
273 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
274 WxGUITextEditorUser* user )
275 : wxPanel(parent, -1),
277 mFileNameFilter("*.*")
279 // m_mgr.SetManagedWindow(this);
280 // wxInitAllImageHandlers();
282 // std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
284 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
286 mwxNotebook = new wxNotebook(this,-1,
287 wxDefaultPosition, wxDefaultSize,
293 sizer->Add(mwxNotebook,1,wxGROW);
297 mwxNotebook = new wxAuiNotebook(this,
303 | wxAUI_NB_TAB_EXTERNAL_MOVE
304 | wxAUI_NB_WINDOWLIST_BUTTON
305 |wxAUI_NB_SCROLL_BUTTONS
306 // | wxAUI_NB_CLOSE_BUTTON
307 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
308 //| wxAUI_NB_CLOSE_ON_ALL_TABS
311 m_mgr.AddPane(mwxNotebook,
312 wxAuiPaneInfo().Name(wxT("notebook"))
314 .CaptionVisible(false)
315 .MinimizeButton(false)
316 .MaximizeButton(false)
318 // .Dockable(false).Float()
320 .MinSize(wxSize(100,50))
325 wxBitmap bmp_new(cc_new_xpm);
326 wxBitmap bmp_open(cc_open_xpm);
327 wxBitmap bmp_close(cc_stop_xpm);
328 wxBitmap bmp_save(cc_save_xpm);
329 wxBitmap bmp_saveas(cc_save_as_xpm);
330 wxBitmap bmp_run(cc_run_xpm);
332 wxBitmap bmp_new(new_xpm);
333 wxBitmap bmp_open(fileopen_xpm);
334 wxBitmap bmp_close(eldel_xpm);
335 wxBitmap bmp_save(filesave_xpm);
336 wxBitmap bmp_saveas(filesaveas_xpm);
337 wxBitmap bmp_run(wxart_run_xpm);
338 wxBitmap bmp_runbbi(wxart_runbbi_xpm);
339 wxBitmap bmp_graphsimple(wxart_graphsimple_xpm);
340 wxBitmap bmp_graphdetail(wxart_graphdetail_xpm);
342 mwxToolBar = new wxToolBar(this, wxID_ANY,
343 wxDefaultPosition, wxDefaultSize,
344 wxTB_FLAT | wxTB_NODIVIDER);
346 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
347 bmp_new, wxNullBitmap, wxITEM_NORMAL,
348 _T("New file"), _T("Create a new file"));
349 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
350 bmp_open, wxNullBitmap, wxITEM_NORMAL,
351 _T("Open file"), _T("This is help for new file tool"));
352 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
353 bmp_close, wxNullBitmap, wxITEM_NORMAL,
354 _T("Close file"), _T("Close current file"));
355 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
356 bmp_save, wxNullBitmap, wxITEM_NORMAL,
357 _T("Save file"), _T("Save current file"));
358 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
359 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
360 _T("Save file as"), _T("Save current file as"));
361 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
362 bmp_run, wxNullBitmap, wxITEM_NORMAL,
363 _T("Run file"), _T("Run current file"));
366 mwxToolBar->AddTool(ID_ButtonGraphSimple, _T("Graph (simple)"),
367 bmp_graphsimple, wxNullBitmap, wxITEM_NORMAL,
368 _T("Graph simple"), _T("Simple graph of actual script"));
370 mwxToolBar->AddTool(ID_ButtonGraphDetail, _T("Graph (detail)"),
371 bmp_graphdetail, wxNullBitmap, wxITEM_NORMAL,
372 _T("Graph (detail)"), _T("Detail graph of actual script"));
374 mwxToolBar->AddTool(ID_ButtonRunBBI, _T("Run external bbi "),
375 bmp_runbbi, wxNullBitmap, wxITEM_NORMAL,
376 _T("Run external bbi"), _T("Run external bbi"));
380 mwxToolBar->AddSeparator();
381 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
382 mwxToolBar->AddControl(mwxPosition);
383 mwxToolBar->Realize();
385 sizer->Add(mwxToolBar,0,wxGROW);
388 m_mgr.AddPane(mwxToolBar,
389 wxAuiPaneInfo().Name(wxT("toolBar"))
393 .MinSize(wxSize(100,50))
394 .LeftDockable(false).RightDockable(false)
408 //================================================================
410 //================================================================
411 WxGUITextEditor::~WxGUITextEditor()
415 // delete mInterpreter;
417 //================================================================
419 //================================================================
420 void WxGUITextEditor::NewPage(const std::string& filename)
422 WxGUITextEditorPage* page =
423 new WxGUITextEditorPage(mwxNotebook,this);
424 std::string name("untitled");
425 if (filename.size()!=0)
430 page->SetPageName(name);
431 std::string fname = Utilities::get_file_name(name);
432 mwxNotebook->AddPage(page,std2wx(fname),true);
433 FocusOnCurrentPage();
435 //================================================================
437 //================================================================
438 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
440 return (WxGUITextEditorPage*)
441 mwxNotebook->GetPage(mwxNotebook->GetSelection());
443 //================================================================
445 //================================================================
446 void WxGUITextEditor::FocusOnCurrentPage()
448 if (mwxNotebook->GetPageCount()==0) return;
449 GetCurrentPage()->SetFocus();
451 //================================================================
453 //================================================================
454 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
456 switch (event.GetId())
460 FocusOnCurrentPage();
464 FocusOnCurrentPage();
466 case ID_ButtonClose :
472 case ID_ButtonSaveAs :
473 if (mwxNotebook->GetPageCount()==0) break;
474 GetCurrentPage()->SetAskFilename(true);
478 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
479 mUser->WxGUITextEditorRun();
480 FocusOnCurrentPage();
483 case ID_ButtonGraphSimple :
484 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
485 mUser->WxGUITextEditorGraphSimple();
486 FocusOnCurrentPage();
489 case ID_ButtonGraphDetail :
490 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
491 mUser->WxGUITextEditorGraphDetail();
492 FocusOnCurrentPage();
495 case ID_ButtonRunBBI :
496 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
497 mUser->WxGUITextEditorRunBBI();
498 FocusOnCurrentPage();
503 //================================================================
505 //================================================================
506 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
509 //================================================================
511 //================================================================
512 void WxGUITextEditor::New()
517 //================================================================
519 //================================================================
520 void WxGUITextEditor::Open()
522 // std::cout << "-------------- OPEN ---------------"<<std::endl;
524 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
525 _T(""),std2wx(mFileNameFilter),
526 wxOPEN | wxFILE_MUST_EXIST );
527 int result_fd = fd->ShowModal();
529 // This line is need it by windows //EED
530 fd->SetReturnCode( result_fd );
532 if (fd->GetReturnCode()==wxID_OK)
534 std::string filename = wx2std(fd->GetPath());
535 std::cout << "file [" << filename << "]" <<std::endl;
540 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
543 //================================================================
545 //================================================================
546 void WxGUITextEditor::Open(const std::string& filename)
551 //================================================================
554 //================================================================
555 bool WxGUITextEditor::CloseCurrentPage()
557 if (mwxNotebook->GetPageCount()==0) return true;
559 if (GetCurrentPage()->IsModified())
561 wxString mess = std2wx(GetCurrentPage()->GetPageName());
562 mess += _T(" modified. Save it ?");
564 new wxMessageDialog(this,
567 wxYES_NO | wxCANCEL | wxICON_QUESTION);
568 switch (d->ShowModal())
574 GetCurrentPage()->Save(mFileNameFilter);
579 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
580 FocusOnCurrentPage();
583 //================================================================
585 //================================================================
586 bool WxGUITextEditor::CloseAllPages()
589 while (mwxNotebook->GetPageCount()!=0)
591 if (!CloseCurrentPage())
599 //================================================================
601 //================================================================
602 void WxGUITextEditor::SaveCurrentPage()
604 if (mwxNotebook->GetPageCount()==0) return;
605 GetCurrentPage()->Save(mFileNameFilter);
606 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
607 std2wx(GetCurrentPage()->GetPageName()));
609 //================================================================
612 //================================================================
613 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
615 if (!CloseCurrentPage()) evt.Veto();
617 //================================================================
622 //================================================================
623 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
626 FocusOnCurrentPage();
628 void WxGUITextEditor::Quit()
630 std::cout << "-------------- QUIT ---------------"<<std::endl;
631 if (AskSave()) GetParent()->Close();
633 //================================================================
636 //================================================================
639 void WxGUITextEditor::Run()
641 std::cout << "-------------- RUN ---------------"<<std::endl;
643 if (!mwxSplit->IsSplit())
646 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
647 if (size<minsize) size = minsize;
648 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
651 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
652 mInterpreter->InterpretLine("reset");
655 mInterpreter->InterpretBuffer(buffer);
661 //================================================================
666 //================================================================
669 //================================================================
670 void WxGUITextEditor::UpdateInfo()
672 if (mwxNotebook->GetPageCount()==0) return;
673 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
675 long line, column, pos;
676 pos = text->GetInsertionPoint();
677 text->PositionToXY(pos, &column, &line);
680 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
684 sprintf(mess,"%ld:%ld/%ld:%ld",
686 (long)text->GetNumberOfLines(),
687 (long)text->GetLineLength(line)+1);
689 // (long)mwxInputText->GetValue().length())+1;
691 // mwxPosition->SetLabel(wxString(mess));
692 mwxPosition->SetLabel(std2wx(mess));
695 if (text->IsModified())
697 std::string title("*");
698 title += GetCurrentPage()->GetPageName();
699 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
702 //================================================================
703 //================================================================
704 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
706 // std::cout << "U" << std::endl;
709 //================================================================
711 //================================================================
712 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
714 // std::cout << "D" << std::endl;
715 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
716 if ( event.ControlDown() )
718 switch (event.GetKeyCode())
720 case 'n': case 'N' : New(); break;
721 case 's': case 'S' : SaveCurrentPage(); break;
722 case 'o': case 'O' : Open(); break;
723 // case 'r': case 'R' : Run(); break;
724 // case 'q': case 'Q' : Quit(); break;
728 //================================================================
730 //================================================================
731 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
732 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
733 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
734 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
736 //================================================================
742 //================================================================
743 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
746 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
750 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
752 mEditor = new WxGUITextEditor(this);
753 sizer->Add(mEditor,1,wxGROW);
755 // WxGUICommand* com = new WxGUICommand(this,this);
760 // parent window of all bbtk windows will be a child of this
761 // Wx::SetTopWindowParent(this);
762 Wx::SetTopWindow(this);
763 // Add the method OnWxSignal as a Wx::Signal observer
764 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
770 //================================================================
772 //================================================================
773 WxGUITextEditorWindow::~WxGUITextEditorWindow()
776 //================================================================
782 #endif //_USE_WXWIDGETS_