1 /*=========================================================================
3 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
5 Date: $Date: 2008/10/17 08:18:15 $
6 Version: $Revision: 1.18 $
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_eldel.xpm"
77 //================================================================
78 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
81 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
82 const wxPoint &pos, const wxSize &size, int style = 0)
83 : wxTextCtrl(parent, id, value, pos, size, style)
87 void SetWxGUITextEditor(WxGUITextEditor* e)
88 { mWxGUITextEditor = e; }
90 void OnKeyDown(wxKeyEvent& event);
91 void OnKeyUp(wxKeyEvent& event);
92 void OnChar(wxKeyEvent& event);
94 WxGUITextEditor* mWxGUITextEditor;
99 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
100 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
101 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
102 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
106 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
111 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
113 mWxGUITextEditor->OnKeyUp(event);
117 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
119 mWxGUITextEditor->OnKeyDown(event);
122 //================================================================
125 //================================================================
126 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
127 EVT_CLOSE(WxGUITextEditorPage::OnClose)
131 //================================================================
132 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
133 WxGUITextEditor* editor)
134 : wxPanel(parent,-1),
140 // std::cout << "WxGUITextEditorPage::WxGUITextEditorPage("<<mName<<")"<<std::endl;
142 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
146 new WxTextCtrlGettingKeyEvents(this,
152 // |wxTE_PROCESS_ENTER
157 mwxInputText->SetWxGUITextEditor(mEditor);
159 new wxTextCtrl(this,-1,_T(""),
163 // |wxTE_PROCESS_ENTER
164 // | wxTE_PROCESS_TAB
169 wxFont* FixedFont = new wxFont(10,
175 mwxInputTextAttr = new wxTextAttr;
176 mwxInputTextAttr->SetFont(*FixedFont);
178 sizer->Add(mwxInputText,1,wxGROW);
183 //================================================================
185 //================================================================
186 WxGUITextEditorPage::~WxGUITextEditorPage()
189 //================================================================
191 bool WxGUITextEditorPage::IsModified()
192 { return mwxInputText->IsModified(); }
194 std::string WxGUITextEditorPage::GetText()
196 return wx2std(GetTextCtrl()->GetValue());
199 //================================================================
200 void WxGUITextEditorPage::Load(const std::string& filename)
202 // std::cout << "-------------- LOAD ---------------"<<std::endl;
203 // std::cout << "'" << filename << "'"<<std::endl;
204 //std::string oldFilename = mFilename;
206 mAskFilename = false;
207 mwxInputText->LoadFile(std2wx(mName));
209 //================================================================
211 //================================================================
212 void WxGUITextEditorPage::Save(const std::string& filter)
214 // std::cout << "-------------- SAVE ---------------"<<std::endl;
217 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
218 _T(""),std2wx(filter),
219 wxSAVE | wxOVERWRITE_PROMPT );
220 int result_fd = fd->ShowModal();
222 // This line is need it by windows // EED
223 fd->SetReturnCode( result_fd );
225 if (fd->GetReturnCode()==wxID_OK)
227 mName = wx2std(fd->GetPath());
228 mAskFilename = false;
232 // std::cout << "-------------- CANCELLED ---------------"
237 // std::cout << "file [" << mName << "]" <<std::endl;
238 mwxInputText->SaveFile(std2wx(mName));
239 mwxInputText->SetModified(false);
241 //================================================================
264 //================================================================
266 //================================================================
269 //================================================================
270 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
271 WxGUITextEditorUser* user )
272 : wxPanel(parent, -1),
274 mFileNameFilter("*.*")
276 // m_mgr.SetManagedWindow(this);
277 // wxInitAllImageHandlers();
279 // std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
281 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
283 mwxNotebook = new wxNotebook(this,-1,
284 wxDefaultPosition, wxDefaultSize,
290 sizer->Add(mwxNotebook,1,wxGROW);
294 mwxNotebook = new wxAuiNotebook(this,
300 | wxAUI_NB_TAB_EXTERNAL_MOVE
301 | wxAUI_NB_WINDOWLIST_BUTTON
302 |wxAUI_NB_SCROLL_BUTTONS
303 // | wxAUI_NB_CLOSE_BUTTON
304 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
305 //| wxAUI_NB_CLOSE_ON_ALL_TABS
308 m_mgr.AddPane(mwxNotebook,
309 wxAuiPaneInfo().Name(wxT("notebook"))
311 .CaptionVisible(false)
312 .MinimizeButton(false)
313 .MaximizeButton(false)
315 // .Dockable(false).Float()
317 .MinSize(wxSize(100,50))
322 wxBitmap bmp_new(cc_new_xpm);
323 wxBitmap bmp_open(cc_open_xpm);
324 wxBitmap bmp_close(cc_stop_xpm);
325 wxBitmap bmp_save(cc_save_xpm);
326 wxBitmap bmp_saveas(cc_save_as_xpm);
327 wxBitmap bmp_run(cc_run_xpm);
329 wxBitmap bmp_new(new_xpm);
330 wxBitmap bmp_open(fileopen_xpm);
331 wxBitmap bmp_close(eldel_xpm);
332 wxBitmap bmp_save(filesave_xpm);
333 wxBitmap bmp_saveas(filesaveas_xpm);
334 wxBitmap bmp_run(down_xpm);
336 mwxToolBar = new wxToolBar(this, wxID_ANY,
337 wxDefaultPosition, wxDefaultSize,
338 wxTB_FLAT | wxTB_NODIVIDER);
340 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
341 bmp_new, wxNullBitmap, wxITEM_NORMAL,
342 _T("New file"), _T("Create a new file"));
343 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
344 bmp_open, wxNullBitmap, wxITEM_NORMAL,
345 _T("Open file"), _T("This is help for new file tool"));
346 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
347 bmp_close, wxNullBitmap, wxITEM_NORMAL,
348 _T("Close file"), _T("Close current file"));
349 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
350 bmp_save, wxNullBitmap, wxITEM_NORMAL,
351 _T("Save file"), _T("Save current file"));
352 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
353 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
354 _T("Save file as"), _T("Save current file as"));
355 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
356 bmp_run, wxNullBitmap, wxITEM_NORMAL,
357 _T("Run file"), _T("Run current file"));
359 mwxToolBar->AddSeparator();
360 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
361 mwxToolBar->AddControl(mwxPosition);
362 mwxToolBar->Realize();
364 sizer->Add(mwxToolBar,0,wxGROW);
367 m_mgr.AddPane(mwxToolBar,
368 wxAuiPaneInfo().Name(wxT("toolBar"))
372 .MinSize(wxSize(100,50))
373 .LeftDockable(false).RightDockable(false)
387 //================================================================
389 //================================================================
390 WxGUITextEditor::~WxGUITextEditor()
394 // delete mInterpreter;
396 //================================================================
398 //================================================================
399 void WxGUITextEditor::NewPage(const std::string& filename)
401 WxGUITextEditorPage* page =
402 new WxGUITextEditorPage(mwxNotebook,this);
403 std::string name("untitled");
404 if (filename.size()!=0)
409 page->SetPageName(name);
410 std::string fname = Utilities::get_file_name(name);
411 mwxNotebook->AddPage(page,std2wx(fname),true);
412 FocusOnCurrentPage();
414 //================================================================
416 //================================================================
417 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
419 return (WxGUITextEditorPage*)
420 mwxNotebook->GetPage(mwxNotebook->GetSelection());
422 //================================================================
424 //================================================================
425 void WxGUITextEditor::FocusOnCurrentPage()
427 if (mwxNotebook->GetPageCount()==0) return;
428 GetCurrentPage()->SetFocus();
430 //================================================================
432 //================================================================
433 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
435 switch (event.GetId())
439 FocusOnCurrentPage();
443 FocusOnCurrentPage();
445 case ID_ButtonClose :
451 case ID_ButtonSaveAs :
452 if (mwxNotebook->GetPageCount()==0) break;
453 GetCurrentPage()->SetAskFilename(true);
457 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
458 mUser->WxGUITextEditorRun();
459 FocusOnCurrentPage();
463 //================================================================
465 //================================================================
466 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
469 //================================================================
471 //================================================================
472 void WxGUITextEditor::New()
477 //================================================================
479 //================================================================
480 void WxGUITextEditor::Open()
482 // std::cout << "-------------- OPEN ---------------"<<std::endl;
484 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
485 _T(""),std2wx(mFileNameFilter),
486 wxOPEN | wxFILE_MUST_EXIST );
487 int result_fd = fd->ShowModal();
489 // This line is need it by windows //EED
490 fd->SetReturnCode( result_fd );
492 if (fd->GetReturnCode()==wxID_OK)
494 std::string filename = wx2std(fd->GetPath());
495 std::cout << "file [" << filename << "]" <<std::endl;
500 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
503 //================================================================
505 //================================================================
506 void WxGUITextEditor::Open(const std::string& filename)
511 //================================================================
514 //================================================================
515 bool WxGUITextEditor::CloseCurrentPage()
517 if (mwxNotebook->GetPageCount()==0) return true;
519 if (GetCurrentPage()->IsModified())
521 wxString mess = std2wx(GetCurrentPage()->GetPageName());
522 mess += _T(" modified. Save it ?");
524 new wxMessageDialog(this,
527 wxYES_NO | wxCANCEL | wxICON_QUESTION);
528 switch (d->ShowModal())
534 GetCurrentPage()->Save(mFileNameFilter);
539 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
540 FocusOnCurrentPage();
543 //================================================================
545 //================================================================
546 bool WxGUITextEditor::CloseAllPages()
549 while (mwxNotebook->GetPageCount()!=0)
551 if (!CloseCurrentPage())
559 //================================================================
561 //================================================================
562 void WxGUITextEditor::SaveCurrentPage()
564 if (mwxNotebook->GetPageCount()==0) return;
565 GetCurrentPage()->Save(mFileNameFilter);
566 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
567 std2wx(GetCurrentPage()->GetPageName()));
569 //================================================================
572 //================================================================
573 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
575 if (!CloseCurrentPage()) evt.Veto();
577 //================================================================
582 //================================================================
583 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
586 FocusOnCurrentPage();
588 void WxGUITextEditor::Quit()
590 std::cout << "-------------- QUIT ---------------"<<std::endl;
591 if (AskSave()) GetParent()->Close();
593 //================================================================
596 //================================================================
599 void WxGUITextEditor::Run()
601 std::cout << "-------------- RUN ---------------"<<std::endl;
603 if (!mwxSplit->IsSplit())
606 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
607 if (size<minsize) size = minsize;
608 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
611 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
612 mInterpreter->InterpretLine("reset");
615 mInterpreter->InterpretBuffer(buffer);
621 //================================================================
626 //================================================================
629 //================================================================
630 void WxGUITextEditor::UpdateInfo()
632 if (mwxNotebook->GetPageCount()==0) return;
633 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
635 long line, column, pos;
636 pos = text->GetInsertionPoint();
637 text->PositionToXY(pos, &column, &line);
640 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
644 sprintf(mess,"%ld:%ld/%ld:%ld",
646 (long)text->GetNumberOfLines(),
647 (long)text->GetLineLength(line)+1);
649 // (long)mwxInputText->GetValue().length())+1;
651 // mwxPosition->SetLabel(wxString(mess));
652 mwxPosition->SetLabel(std2wx(mess));
655 if (text->IsModified())
657 std::string title("*");
658 title += GetCurrentPage()->GetPageName();
659 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
662 //================================================================
663 //================================================================
664 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
666 // std::cout << "U" << std::endl;
669 //================================================================
671 //================================================================
672 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
674 // std::cout << "D" << std::endl;
675 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
676 if ( event.ControlDown() )
678 switch (event.GetKeyCode())
680 case 'n': case 'N' : New(); break;
681 case 's': case 'S' : SaveCurrentPage(); break;
682 case 'o': case 'O' : Open(); break;
683 // case 'r': case 'R' : Run(); break;
684 // case 'q': case 'Q' : Quit(); break;
688 //================================================================
690 //================================================================
691 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
692 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
693 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
694 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
696 //================================================================
702 //================================================================
703 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
706 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
710 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
712 mEditor = new WxGUITextEditor(this);
713 sizer->Add(mEditor,1,wxGROW);
715 // WxGUICommand* com = new WxGUICommand(this,this);
720 // parent window of all bbtk windows will be a child of this
721 Wx::SetTopWindowParent(this);
722 // Add the method OnWxSignal as a Wx::Signal observer
723 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
729 //================================================================
731 //================================================================
732 WxGUITextEditorWindow::~WxGUITextEditorWindow()
735 //================================================================
741 #endif //_USE_WXWIDGETS_