1 /*=========================================================================
3 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
5 Date: $Date: 2009/12/04 10:48:34 $
6 Version: $Revision: 1.21 $
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 mFixedFont = new wxFont(10,
179 mwxInputTextAttr = new wxTextAttr;
180 mwxInputTextAttr->SetFont(*mFixedFont);
182 sizer->Add(mwxInputText,1,wxGROW);
187 //================================================================
189 //================================================================
190 WxGUITextEditorPage::~WxGUITextEditorPage()
192 delete mwxInputTextAttr;
195 //================================================================
197 bool WxGUITextEditorPage::IsModified()
198 { return mwxInputText->IsModified(); }
200 std::string WxGUITextEditorPage::GetText()
202 return wx2std(GetTextCtrl()->GetValue());
205 //================================================================
206 void WxGUITextEditorPage::Load(const std::string& filename)
208 // std::cout << "-------------- LOAD ---------------"<<std::endl;
209 // std::cout << "'" << filename << "'"<<std::endl;
210 //std::string oldFilename = mFilename;
212 mAskFilename = false;
213 mwxInputText->LoadFile(std2wx(mName));
215 //================================================================
217 //================================================================
218 void WxGUITextEditorPage::Save(const std::string& filter)
220 // std::cout << "-------------- SAVE ---------------"<<std::endl;
223 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
224 _T(""),std2wx(filter),
225 wxSAVE | wxOVERWRITE_PROMPT );
226 int result_fd = fd->ShowModal();
228 // This line is need it by windows // EED
229 fd->SetReturnCode( result_fd );
231 if (fd->GetReturnCode()==wxID_OK)
233 mName = wx2std(fd->GetPath());
234 mAskFilename = false;
238 // std::cout << "-------------- CANCELLED ---------------"
243 // std::cout << "file [" << mName << "]" <<std::endl;
244 // mwxInputText->SaveFile(std2wx(mName));
246 mwxInputText->SetModified(false);
248 //================================================================
251 //================================================================
252 void WxGUITextEditorPage::SaveFile(const std::string& filename)
254 mwxInputText->SaveFile( std2wx(filename) );
256 //================================================================
269 //================================================================
271 //================================================================
274 //================================================================
275 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
276 WxGUITextEditorUser* user )
277 : wxPanel(parent, -1),
279 mFileNameFilter("*.*")
281 // m_mgr.SetManagedWindow(this);
282 // wxInitAllImageHandlers();
284 // std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
286 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
288 mwxNotebook = new wxNotebook(this,-1,
289 wxDefaultPosition, wxDefaultSize,
295 sizer->Add(mwxNotebook,1,wxGROW);
299 mwxNotebook = new wxAuiNotebook(this,
305 | wxAUI_NB_TAB_EXTERNAL_MOVE
306 | wxAUI_NB_WINDOWLIST_BUTTON
307 |wxAUI_NB_SCROLL_BUTTONS
308 // | wxAUI_NB_CLOSE_BUTTON
309 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
310 //| wxAUI_NB_CLOSE_ON_ALL_TABS
313 m_mgr.AddPane(mwxNotebook,
314 wxAuiPaneInfo().Name(wxT("notebook"))
316 .CaptionVisible(false)
317 .MinimizeButton(false)
318 .MaximizeButton(false)
320 // .Dockable(false).Float()
322 .MinSize(wxSize(100,50))
327 wxBitmap bmp_new(cc_new_xpm);
328 wxBitmap bmp_open(cc_open_xpm);
329 wxBitmap bmp_close(cc_stop_xpm);
330 wxBitmap bmp_save(cc_save_xpm);
331 wxBitmap bmp_saveas(cc_save_as_xpm);
332 wxBitmap bmp_run(cc_run_xpm);
334 wxBitmap bmp_new(new_xpm);
335 wxBitmap bmp_open(fileopen_xpm);
336 wxBitmap bmp_close(eldel_xpm);
337 wxBitmap bmp_save(filesave_xpm);
338 wxBitmap bmp_saveas(filesaveas_xpm);
339 wxBitmap bmp_run(wxart_run_xpm);
340 wxBitmap bmp_runbbi(wxart_runbbi_xpm);
341 wxBitmap bmp_graphsimple(wxart_graphsimple_xpm);
342 wxBitmap bmp_graphdetail(wxart_graphdetail_xpm);
344 mwxToolBar = new wxToolBar(this, wxID_ANY,
345 wxDefaultPosition, wxDefaultSize,
346 wxTB_FLAT | wxTB_NODIVIDER);
348 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
349 bmp_new, wxNullBitmap, wxITEM_NORMAL,
350 _T("New file"), _T("Create a new file"));
351 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
352 bmp_open, wxNullBitmap, wxITEM_NORMAL,
353 _T("Open file"), _T("This is help for new file tool"));
354 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
355 bmp_close, wxNullBitmap, wxITEM_NORMAL,
356 _T("Close file"), _T("Close current file"));
357 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
358 bmp_save, wxNullBitmap, wxITEM_NORMAL,
359 _T("Save file"), _T("Save current file"));
360 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
361 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
362 _T("Save file as"), _T("Save current file as"));
363 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
364 bmp_run, wxNullBitmap, wxITEM_NORMAL,
365 _T("Run file"), _T("Run current file"));
368 mwxToolBar->AddTool(ID_ButtonGraphSimple, _T("Graph (simple)"),
369 bmp_graphsimple, wxNullBitmap, wxITEM_NORMAL,
370 _T("Graph simple"), _T("Simple graph of actual script"));
372 mwxToolBar->AddTool(ID_ButtonGraphDetail, _T("Graph (detail)"),
373 bmp_graphdetail, wxNullBitmap, wxITEM_NORMAL,
374 _T("Graph (detail)"), _T("Detail graph of actual script"));
376 mwxToolBar->AddTool(ID_ButtonRunBBI, _T("Run external bbi "),
377 bmp_runbbi, wxNullBitmap, wxITEM_NORMAL,
378 _T("Run external bbi"), _T("Run external bbi"));
382 mwxToolBar->AddSeparator();
383 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
384 mwxToolBar->AddControl(mwxPosition);
385 mwxToolBar->Realize();
387 sizer->Add(mwxToolBar,0,wxGROW);
390 m_mgr.AddPane(mwxToolBar,
391 wxAuiPaneInfo().Name(wxT("toolBar"))
395 .MinSize(wxSize(100,50))
396 .LeftDockable(false).RightDockable(false)
410 //================================================================
412 //================================================================
413 WxGUITextEditor::~WxGUITextEditor()
417 // delete mInterpreter;
419 //================================================================
421 //================================================================
422 void WxGUITextEditor::NewPage(const std::string& filename)
424 WxGUITextEditorPage* page =
425 new WxGUITextEditorPage(mwxNotebook,this);
426 std::string name("untitled");
427 if (filename.size()!=0)
432 page->SetPageName(name);
433 std::string fname = Utilities::get_file_name(name);
434 mwxNotebook->AddPage(page,std2wx(fname),true);
435 FocusOnCurrentPage();
437 //================================================================
439 //================================================================
440 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
442 return (WxGUITextEditorPage*)
443 mwxNotebook->GetPage(mwxNotebook->GetSelection());
445 //================================================================
447 //================================================================
448 void WxGUITextEditor::FocusOnCurrentPage()
450 if (mwxNotebook->GetPageCount()==0) return;
451 GetCurrentPage()->SetFocus();
453 //================================================================
455 //================================================================
456 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
458 switch (event.GetId())
462 FocusOnCurrentPage();
466 FocusOnCurrentPage();
468 case ID_ButtonClose :
474 case ID_ButtonSaveAs :
475 if (mwxNotebook->GetPageCount()==0) break;
476 GetCurrentPage()->SetAskFilename(true);
480 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
481 mUser->WxGUITextEditorRun();
482 FocusOnCurrentPage();
485 case ID_ButtonGraphSimple :
486 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
487 mUser->WxGUITextEditorGraphSimple();
488 FocusOnCurrentPage();
491 case ID_ButtonGraphDetail :
492 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
493 mUser->WxGUITextEditorGraphDetail();
494 FocusOnCurrentPage();
497 case ID_ButtonRunBBI :
498 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
499 mUser->WxGUITextEditorRunBBI();
500 FocusOnCurrentPage();
505 //================================================================
507 //================================================================
508 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
511 //================================================================
513 //================================================================
514 void WxGUITextEditor::New()
519 //================================================================
521 //================================================================
522 void WxGUITextEditor::Open()
524 // std::cout << "-------------- OPEN ---------------"<<std::endl;
526 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
527 _T(""),std2wx(mFileNameFilter),
528 wxOPEN | wxFILE_MUST_EXIST );
529 int result_fd = fd->ShowModal();
531 // This line is need it by windows //EED
532 fd->SetReturnCode( result_fd );
534 if (fd->GetReturnCode()==wxID_OK)
536 std::string filename = wx2std(fd->GetPath());
537 std::cout << "file [" << filename << "]" <<std::endl;
542 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
545 //================================================================
547 //================================================================
548 void WxGUITextEditor::Open(const std::string& filename)
553 //================================================================
556 //================================================================
557 bool WxGUITextEditor::CloseCurrentPage()
559 if (mwxNotebook->GetPageCount()==0) return true;
561 if (GetCurrentPage()->IsModified())
563 wxString mess = std2wx(GetCurrentPage()->GetPageName());
564 mess += _T(" modified. Save it ?");
566 new wxMessageDialog(this,
569 wxYES_NO | wxCANCEL | wxICON_QUESTION);
570 switch (d->ShowModal())
576 GetCurrentPage()->Save(mFileNameFilter);
581 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
582 FocusOnCurrentPage();
585 //================================================================
587 //================================================================
588 bool WxGUITextEditor::CloseAllPages()
591 while (mwxNotebook->GetPageCount()!=0)
593 if (!CloseCurrentPage())
601 //================================================================
603 //================================================================
604 void WxGUITextEditor::SaveCurrentPage()
606 if (mwxNotebook->GetPageCount()==0) return;
607 GetCurrentPage()->Save(mFileNameFilter);
608 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
609 std2wx(GetCurrentPage()->GetPageName()));
611 //================================================================
614 //================================================================
615 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
617 if (!CloseCurrentPage()) evt.Veto();
619 //================================================================
624 //================================================================
625 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
628 FocusOnCurrentPage();
630 void WxGUITextEditor::Quit()
632 std::cout << "-------------- QUIT ---------------"<<std::endl;
633 if (AskSave()) GetParent()->Close();
635 //================================================================
638 //================================================================
641 void WxGUITextEditor::Run()
643 std::cout << "-------------- RUN ---------------"<<std::endl;
645 if (!mwxSplit->IsSplit())
648 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
649 if (size<minsize) size = minsize;
650 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
653 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
654 mInterpreter->InterpretLine("reset");
657 mInterpreter->InterpretBuffer(buffer);
663 //================================================================
668 //================================================================
671 //================================================================
672 void WxGUITextEditor::UpdateInfo()
674 if (mwxNotebook->GetPageCount()==0) return;
675 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
677 long line, column, pos;
678 pos = text->GetInsertionPoint();
679 text->PositionToXY(pos, &column, &line);
682 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
686 sprintf(mess,"%ld:%ld/%ld:%ld",
688 (long)text->GetNumberOfLines(),
689 (long)text->GetLineLength(line)+1);
691 // (long)mwxInputText->GetValue().length())+1;
693 // mwxPosition->SetLabel(wxString(mess));
694 mwxPosition->SetLabel(std2wx(mess));
697 if (text->IsModified())
699 std::string title("*");
700 title += GetCurrentPage()->GetPageName();
701 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
704 //================================================================
705 //================================================================
706 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
708 // std::cout << "U" << std::endl;
711 //================================================================
713 //================================================================
714 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
716 // std::cout << "D" << std::endl;
717 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
718 if ( event.ControlDown() )
720 switch (event.GetKeyCode())
722 case 'n': case 'N' : New(); break;
723 case 's': case 'S' : SaveCurrentPage(); break;
724 case 'o': case 'O' : Open(); break;
725 // case 'r': case 'R' : Run(); break;
726 // case 'q': case 'Q' : Quit(); break;
730 //================================================================
732 //================================================================
733 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
734 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
735 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
736 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
738 //================================================================
744 //================================================================
745 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
748 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
752 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
754 mEditor = new WxGUITextEditor(this);
755 sizer->Add(mEditor,1,wxGROW);
757 // WxGUICommand* com = new WxGUICommand(this,this);
762 // parent window of all bbtk windows will be a child of this
763 // Wx::SetTopWindowParent(this);
764 Wx::SetTopWindow(this);
765 // Add the method OnWxSignal as a Wx::Signal observer
766 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
772 //================================================================
774 //================================================================
775 WxGUITextEditorWindow::~WxGUITextEditorWindow()
778 //================================================================
784 #endif //_USE_WXWIDGETS_