1 /*=========================================================================
4 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
6 Date: $Date: 2008/06/26 07:37:05 $
7 Version: $Revision: 1.13 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*//**
18 * \brief Short description in one line
20 * Long description which
21 * can span multiple lines
33 #ifdef _USE_WXWIDGETS_
36 #include "bbtkWxGUITextEditor.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
39 #include "bbtkUtilities.h"
41 #include "icons/cc_new.xpm"
42 #include "icons/cc_open.xpm"
43 #include "icons/cc_stop.xpm"
44 #include "icons/cc_save.xpm"
45 #include "icons/cc_save_as.xpm"
46 #include "icons/cc_run.xpm"
47 #include "icons/cc_exit.xpm"
49 #include "../data/icons/wxart_new.xpm"
50 #include "../data/icons/wxart_fileopen.xpm"
51 #include "../data/icons/wxart_filesave.xpm"
52 #include "../data/icons/wxart_filesaveas.xpm"
53 #include "../data/icons/wxart_exefile.xpm"
54 #include "../data/icons/wxart_delete.xpm"
55 #include "../data/icons/wxart_down.xpm"
56 #include "../data/icons/wxart_eldel.xpm"
62 //================================================================
63 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
66 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
67 const wxPoint &pos, const wxSize &size, int style = 0)
68 : wxTextCtrl(parent, id, value, pos, size, style)
72 void SetWxGUITextEditor(WxGUITextEditor* e)
73 { mWxGUITextEditor = e; }
75 void OnKeyDown(wxKeyEvent& event);
76 void OnKeyUp(wxKeyEvent& event);
77 void OnChar(wxKeyEvent& event);
79 WxGUITextEditor* mWxGUITextEditor;
84 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
85 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
86 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
87 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
91 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
96 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
98 mWxGUITextEditor->OnKeyUp(event);
102 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
104 mWxGUITextEditor->OnKeyDown(event);
107 //================================================================
111 //================================================================
112 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
113 EVT_CLOSE(WxGUITextEditorPage::OnClose)
117 //================================================================
118 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
119 WxGUITextEditor* editor)
120 : wxPanel(parent,-1),
125 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
128 new WxTextCtrlGettingKeyEvents(this,
134 // |wxTE_PROCESS_ENTER
139 mwxInputText->SetWxGUITextEditor(mEditor);
141 wxFont* FixedFont = new wxFont(10,
147 mwxInputTextAttr = new wxTextAttr;
148 mwxInputTextAttr->SetFont(*FixedFont);
150 sizer->Add(mwxInputText,1,wxGROW);
155 //================================================================
157 //================================================================
158 WxGUITextEditorPage::~WxGUITextEditorPage()
161 //================================================================
163 bool WxGUITextEditorPage::IsModified()
164 { return mwxInputText->IsModified(); }
166 std::string WxGUITextEditorPage::GetText()
168 return wx2std(GetTextCtrl()->GetValue());
171 //================================================================
172 void WxGUITextEditorPage::Load(const std::string& filename)
174 // std::cout << "-------------- LOAD ---------------"<<std::endl;
175 // std::cout << "'" << filename << "'"<<std::endl;
176 //std::string oldFilename = mFilename;
178 mAskFilename = false;
179 mwxInputText->LoadFile(std2wx(mName));
181 //================================================================
183 //================================================================
184 void WxGUITextEditorPage::Save(const std::string& filter)
186 // std::cout << "-------------- SAVE ---------------"<<std::endl;
189 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
190 _T(""),std2wx(filter),
191 wxSAVE | wxOVERWRITE_PROMPT );
192 int result_fd = fd->ShowModal();
194 // This line is need it by windows // EED
195 fd->SetReturnCode( result_fd );
197 if (fd->GetReturnCode()==wxID_OK)
199 mName = wx2std(fd->GetPath());
200 mAskFilename = false;
204 // std::cout << "-------------- CANCELLED ---------------"
209 // std::cout << "file [" << mName << "]" <<std::endl;
210 mwxInputText->SaveFile(std2wx(mName));
211 mwxInputText->SetModified(false);
213 //================================================================
236 //================================================================
238 //================================================================
241 //================================================================
242 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
243 WxGUITextEditorUser* user )
244 : wxPanel(parent, -1),
246 mFileNameFilter("*.*")
248 m_mgr.SetManagedWindow(this);
250 // wxInitAllImageHandlers();
252 mwxNotebook = new wxAuiNotebook(this,
258 | wxAUI_NB_TAB_EXTERNAL_MOVE
259 | wxAUI_NB_WINDOWLIST_BUTTON
260 |wxAUI_NB_SCROLL_BUTTONS
261 // | wxAUI_NB_CLOSE_BUTTON
262 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
263 //| wxAUI_NB_CLOSE_ON_ALL_TABS
266 m_mgr.AddPane(mwxNotebook,
267 wxAuiPaneInfo().Name(wxT("notebook"))
269 .CaptionVisible(false)
270 .MinimizeButton(false)
271 .MaximizeButton(false)
273 // .Dockable(false).Float()
275 .MinSize(wxSize(100,50))
280 wxBitmap bmp_new(cc_new_xpm);
281 wxBitmap bmp_open(cc_open_xpm);
282 wxBitmap bmp_close(cc_stop_xpm);
283 wxBitmap bmp_save(cc_save_xpm);
284 wxBitmap bmp_saveas(cc_save_as_xpm);
285 wxBitmap bmp_run(cc_run_xpm);
287 wxBitmap bmp_new(new_xpm);
288 wxBitmap bmp_open(fileopen_xpm);
289 wxBitmap bmp_close(eldel_xpm);
290 wxBitmap bmp_save(filesave_xpm);
291 wxBitmap bmp_saveas(filesaveas_xpm);
292 wxBitmap bmp_run(down_xpm);
294 mwxToolBar = new wxToolBar(this, wxID_ANY,
295 wxDefaultPosition, wxDefaultSize,
296 wxTB_FLAT | wxTB_NODIVIDER);
298 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
299 bmp_new, wxNullBitmap, wxITEM_NORMAL,
300 _T("New file"), _T("Create a new file"));
301 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
302 bmp_open, wxNullBitmap, wxITEM_NORMAL,
303 _T("Open file"), _T("This is help for new file tool"));
304 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
305 bmp_close, wxNullBitmap, wxITEM_NORMAL,
306 _T("Close file"), _T("Close current file"));
307 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
308 bmp_save, wxNullBitmap, wxITEM_NORMAL,
309 _T("Save file"), _T("Save current file"));
310 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
311 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
312 _T("Save file as"), _T("Save current file as"));
313 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
314 bmp_run, wxNullBitmap, wxITEM_NORMAL,
315 _T("Run file"), _T("Run current file"));
317 mwxToolBar->AddSeparator();
318 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
319 mwxToolBar->AddControl(mwxPosition);
320 mwxToolBar->Realize();
322 m_mgr.AddPane(mwxToolBar,
323 wxAuiPaneInfo().Name(wxT("toolBar"))
327 .MinSize(wxSize(100,50))
328 .LeftDockable(false).RightDockable(false)
338 //================================================================
340 //================================================================
341 WxGUITextEditor::~WxGUITextEditor()
345 // delete mInterpreter;
347 //================================================================
349 //================================================================
350 void WxGUITextEditor::NewPage(const std::string& filename)
352 WxGUITextEditorPage* page =
353 new WxGUITextEditorPage(mwxNotebook,this);
354 std::string name("untitled");
355 if (filename.size()!=0)
360 page->SetPageName(name);
361 std::string fname = Utilities::get_file_name(name);
362 mwxNotebook->AddPage(page,std2wx(fname),true);
363 FocusOnCurrentPage();
365 //================================================================
367 //================================================================
368 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
370 return (WxGUITextEditorPage*)
371 mwxNotebook->GetPage(mwxNotebook->GetSelection());
373 //================================================================
375 //================================================================
376 void WxGUITextEditor::FocusOnCurrentPage()
378 if (mwxNotebook->GetPageCount()==0) return;
379 GetCurrentPage()->SetFocus();
381 //================================================================
383 //================================================================
384 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
386 switch (event.GetId())
390 FocusOnCurrentPage();
394 FocusOnCurrentPage();
396 case ID_ButtonClose :
402 case ID_ButtonSaveAs :
403 if (mwxNotebook->GetPageCount()==0) break;
404 GetCurrentPage()->SetAskFilename(true);
408 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
409 mUser->WxGUITextEditorRun();
410 FocusOnCurrentPage();
414 //================================================================
416 //================================================================
417 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
420 //================================================================
422 //================================================================
423 void WxGUITextEditor::New()
428 //================================================================
430 //================================================================
431 void WxGUITextEditor::Open()
433 // std::cout << "-------------- OPEN ---------------"<<std::endl;
435 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
436 _T(""),std2wx(mFileNameFilter),
437 wxOPEN | wxFILE_MUST_EXIST );
438 int result_fd = fd->ShowModal();
440 // This line is need it by windows //EED
441 fd->SetReturnCode( result_fd );
443 if (fd->GetReturnCode()==wxID_OK)
445 std::string filename = wx2std(fd->GetPath());
446 std::cout << "file [" << filename << "]" <<std::endl;
451 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
454 //================================================================
456 //================================================================
457 void WxGUITextEditor::Open(const std::string& filename)
462 //================================================================
465 //================================================================
466 bool WxGUITextEditor::CloseCurrentPage()
468 if (mwxNotebook->GetPageCount()==0) return true;
470 if (GetCurrentPage()->IsModified())
472 wxString mess = std2wx(GetCurrentPage()->GetPageName());
473 mess += _T(" modified. Save it ?");
475 new wxMessageDialog(this,
478 wxYES_NO | wxCANCEL | wxICON_QUESTION);
479 switch (d->ShowModal())
485 GetCurrentPage()->Save(mFileNameFilter);
490 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
491 FocusOnCurrentPage();
494 //================================================================
496 //================================================================
497 bool WxGUITextEditor::CloseAllPages()
500 while (mwxNotebook->GetPageCount()!=0)
502 if (!CloseCurrentPage())
510 //================================================================
512 //================================================================
513 void WxGUITextEditor::SaveCurrentPage()
515 if (mwxNotebook->GetPageCount()==0) return;
516 GetCurrentPage()->Save(mFileNameFilter);
517 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
518 std2wx(GetCurrentPage()->GetPageName()));
520 //================================================================
523 //================================================================
524 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
526 if (!CloseCurrentPage()) evt.Veto();
528 //================================================================
533 //================================================================
534 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
537 FocusOnCurrentPage();
539 void WxGUITextEditor::Quit()
541 std::cout << "-------------- QUIT ---------------"<<std::endl;
542 if (AskSave()) GetParent()->Close();
544 //================================================================
547 //================================================================
550 void WxGUITextEditor::Run()
552 std::cout << "-------------- RUN ---------------"<<std::endl;
554 if (!mwxSplit->IsSplit())
557 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
558 if (size<minsize) size = minsize;
559 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
562 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
563 mInterpreter->InterpretLine("reset");
566 mInterpreter->InterpretBuffer(buffer);
572 //================================================================
577 //================================================================
580 //================================================================
581 void WxGUITextEditor::UpdateInfo()
583 if (mwxNotebook->GetPageCount()==0) return;
584 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
586 long line, column, pos;
587 pos = text->GetInsertionPoint();
588 text->PositionToXY(pos, &column, &line);
591 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
595 sprintf(mess,"%ld:%ld/%ld:%ld",
597 (long)text->GetNumberOfLines(),
598 (long)text->GetLineLength(line)+1);
600 // (long)mwxInputText->GetValue().length())+1;
602 // mwxPosition->SetLabel(wxString(mess));
603 mwxPosition->SetLabel(std2wx(mess));
606 if (text->IsModified())
608 std::string title("*");
609 title += GetCurrentPage()->GetPageName();
610 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
613 //================================================================
614 //================================================================
615 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
617 // std::cout << "U" << std::endl;
620 //================================================================
622 //================================================================
623 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
625 // std::cout << "D" << std::endl;
626 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
627 if ( event.ControlDown() )
629 switch (event.GetKeyCode())
631 case 'n': case 'N' : New(); break;
632 case 's': case 'S' : SaveCurrentPage(); break;
633 case 'o': case 'O' : Open(); break;
634 // case 'r': case 'R' : Run(); break;
635 // case 'q': case 'Q' : Quit(); break;
639 //================================================================
641 //================================================================
642 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
643 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
644 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
645 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
647 //================================================================
653 //================================================================
654 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
657 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
661 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
663 mEditor = new WxGUITextEditor(this);
664 sizer->Add(mEditor,1,wxGROW);
666 // WxGUICommand* com = new WxGUICommand(this,this);
671 // Creates the parent window of all bbtk windows as a child of this
672 Wx::CreateTopWindow(this);
673 // Add the method OnWxSignal as a Wx::Signal observer
674 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
680 //================================================================
682 //================================================================
683 WxGUITextEditorWindow::~WxGUITextEditorWindow()
686 //================================================================
692 #endif //_USE_WXWIDGETS_