1 /*=========================================================================
4 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
6 Date: $Date: 2008/10/03 14:27:52 $
7 Version: $Revision: 1.17 $
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 //================================================================
110 //================================================================
111 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
112 EVT_CLOSE(WxGUITextEditorPage::OnClose)
116 //================================================================
117 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
118 WxGUITextEditor* editor)
119 : wxPanel(parent,-1),
125 // std::cout << "WxGUITextEditorPage::WxGUITextEditorPage("<<mName<<")"<<std::endl;
127 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
131 new WxTextCtrlGettingKeyEvents(this,
137 // |wxTE_PROCESS_ENTER
142 mwxInputText->SetWxGUITextEditor(mEditor);
144 new wxTextCtrl(this,-1,_T(""),
148 // |wxTE_PROCESS_ENTER
149 // | wxTE_PROCESS_TAB
154 wxFont* FixedFont = new wxFont(10,
160 mwxInputTextAttr = new wxTextAttr;
161 mwxInputTextAttr->SetFont(*FixedFont);
163 sizer->Add(mwxInputText,1,wxGROW);
168 //================================================================
170 //================================================================
171 WxGUITextEditorPage::~WxGUITextEditorPage()
174 //================================================================
176 bool WxGUITextEditorPage::IsModified()
177 { return mwxInputText->IsModified(); }
179 std::string WxGUITextEditorPage::GetText()
181 return wx2std(GetTextCtrl()->GetValue());
184 //================================================================
185 void WxGUITextEditorPage::Load(const std::string& filename)
187 // std::cout << "-------------- LOAD ---------------"<<std::endl;
188 // std::cout << "'" << filename << "'"<<std::endl;
189 //std::string oldFilename = mFilename;
191 mAskFilename = false;
192 mwxInputText->LoadFile(std2wx(mName));
194 //================================================================
196 //================================================================
197 void WxGUITextEditorPage::Save(const std::string& filter)
199 // std::cout << "-------------- SAVE ---------------"<<std::endl;
202 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
203 _T(""),std2wx(filter),
204 wxSAVE | wxOVERWRITE_PROMPT );
205 int result_fd = fd->ShowModal();
207 // This line is need it by windows // EED
208 fd->SetReturnCode( result_fd );
210 if (fd->GetReturnCode()==wxID_OK)
212 mName = wx2std(fd->GetPath());
213 mAskFilename = false;
217 // std::cout << "-------------- CANCELLED ---------------"
222 // std::cout << "file [" << mName << "]" <<std::endl;
223 mwxInputText->SaveFile(std2wx(mName));
224 mwxInputText->SetModified(false);
226 //================================================================
249 //================================================================
251 //================================================================
254 //================================================================
255 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
256 WxGUITextEditorUser* user )
257 : wxPanel(parent, -1),
259 mFileNameFilter("*.*")
261 // m_mgr.SetManagedWindow(this);
262 // wxInitAllImageHandlers();
264 // std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
266 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
268 mwxNotebook = new wxNotebook(this,-1,
269 wxDefaultPosition, wxDefaultSize,
275 sizer->Add(mwxNotebook,1,wxGROW);
279 mwxNotebook = new wxAuiNotebook(this,
285 | wxAUI_NB_TAB_EXTERNAL_MOVE
286 | wxAUI_NB_WINDOWLIST_BUTTON
287 |wxAUI_NB_SCROLL_BUTTONS
288 // | wxAUI_NB_CLOSE_BUTTON
289 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
290 //| wxAUI_NB_CLOSE_ON_ALL_TABS
293 m_mgr.AddPane(mwxNotebook,
294 wxAuiPaneInfo().Name(wxT("notebook"))
296 .CaptionVisible(false)
297 .MinimizeButton(false)
298 .MaximizeButton(false)
300 // .Dockable(false).Float()
302 .MinSize(wxSize(100,50))
307 wxBitmap bmp_new(cc_new_xpm);
308 wxBitmap bmp_open(cc_open_xpm);
309 wxBitmap bmp_close(cc_stop_xpm);
310 wxBitmap bmp_save(cc_save_xpm);
311 wxBitmap bmp_saveas(cc_save_as_xpm);
312 wxBitmap bmp_run(cc_run_xpm);
314 wxBitmap bmp_new(new_xpm);
315 wxBitmap bmp_open(fileopen_xpm);
316 wxBitmap bmp_close(eldel_xpm);
317 wxBitmap bmp_save(filesave_xpm);
318 wxBitmap bmp_saveas(filesaveas_xpm);
319 wxBitmap bmp_run(down_xpm);
321 mwxToolBar = new wxToolBar(this, wxID_ANY,
322 wxDefaultPosition, wxDefaultSize,
323 wxTB_FLAT | wxTB_NODIVIDER);
325 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
326 bmp_new, wxNullBitmap, wxITEM_NORMAL,
327 _T("New file"), _T("Create a new file"));
328 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
329 bmp_open, wxNullBitmap, wxITEM_NORMAL,
330 _T("Open file"), _T("This is help for new file tool"));
331 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
332 bmp_close, wxNullBitmap, wxITEM_NORMAL,
333 _T("Close file"), _T("Close current file"));
334 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
335 bmp_save, wxNullBitmap, wxITEM_NORMAL,
336 _T("Save file"), _T("Save current file"));
337 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
338 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
339 _T("Save file as"), _T("Save current file as"));
340 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
341 bmp_run, wxNullBitmap, wxITEM_NORMAL,
342 _T("Run file"), _T("Run current file"));
344 mwxToolBar->AddSeparator();
345 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
346 mwxToolBar->AddControl(mwxPosition);
347 mwxToolBar->Realize();
349 sizer->Add(mwxToolBar,0,wxGROW);
352 m_mgr.AddPane(mwxToolBar,
353 wxAuiPaneInfo().Name(wxT("toolBar"))
357 .MinSize(wxSize(100,50))
358 .LeftDockable(false).RightDockable(false)
372 //================================================================
374 //================================================================
375 WxGUITextEditor::~WxGUITextEditor()
379 // delete mInterpreter;
381 //================================================================
383 //================================================================
384 void WxGUITextEditor::NewPage(const std::string& filename)
386 WxGUITextEditorPage* page =
387 new WxGUITextEditorPage(mwxNotebook,this);
388 std::string name("untitled");
389 if (filename.size()!=0)
394 page->SetPageName(name);
395 std::string fname = Utilities::get_file_name(name);
396 mwxNotebook->AddPage(page,std2wx(fname),true);
397 FocusOnCurrentPage();
399 //================================================================
401 //================================================================
402 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
404 return (WxGUITextEditorPage*)
405 mwxNotebook->GetPage(mwxNotebook->GetSelection());
407 //================================================================
409 //================================================================
410 void WxGUITextEditor::FocusOnCurrentPage()
412 if (mwxNotebook->GetPageCount()==0) return;
413 GetCurrentPage()->SetFocus();
415 //================================================================
417 //================================================================
418 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
420 switch (event.GetId())
424 FocusOnCurrentPage();
428 FocusOnCurrentPage();
430 case ID_ButtonClose :
436 case ID_ButtonSaveAs :
437 if (mwxNotebook->GetPageCount()==0) break;
438 GetCurrentPage()->SetAskFilename(true);
442 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
443 mUser->WxGUITextEditorRun();
444 FocusOnCurrentPage();
448 //================================================================
450 //================================================================
451 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
454 //================================================================
456 //================================================================
457 void WxGUITextEditor::New()
462 //================================================================
464 //================================================================
465 void WxGUITextEditor::Open()
467 // std::cout << "-------------- OPEN ---------------"<<std::endl;
469 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
470 _T(""),std2wx(mFileNameFilter),
471 wxOPEN | wxFILE_MUST_EXIST );
472 int result_fd = fd->ShowModal();
474 // This line is need it by windows //EED
475 fd->SetReturnCode( result_fd );
477 if (fd->GetReturnCode()==wxID_OK)
479 std::string filename = wx2std(fd->GetPath());
480 std::cout << "file [" << filename << "]" <<std::endl;
485 // std::cout << "-------------- CANCELLED ---------------"<<std::endl;
488 //================================================================
490 //================================================================
491 void WxGUITextEditor::Open(const std::string& filename)
496 //================================================================
499 //================================================================
500 bool WxGUITextEditor::CloseCurrentPage()
502 if (mwxNotebook->GetPageCount()==0) return true;
504 if (GetCurrentPage()->IsModified())
506 wxString mess = std2wx(GetCurrentPage()->GetPageName());
507 mess += _T(" modified. Save it ?");
509 new wxMessageDialog(this,
512 wxYES_NO | wxCANCEL | wxICON_QUESTION);
513 switch (d->ShowModal())
519 GetCurrentPage()->Save(mFileNameFilter);
524 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
525 FocusOnCurrentPage();
528 //================================================================
530 //================================================================
531 bool WxGUITextEditor::CloseAllPages()
534 while (mwxNotebook->GetPageCount()!=0)
536 if (!CloseCurrentPage())
544 //================================================================
546 //================================================================
547 void WxGUITextEditor::SaveCurrentPage()
549 if (mwxNotebook->GetPageCount()==0) return;
550 GetCurrentPage()->Save(mFileNameFilter);
551 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
552 std2wx(GetCurrentPage()->GetPageName()));
554 //================================================================
557 //================================================================
558 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
560 if (!CloseCurrentPage()) evt.Veto();
562 //================================================================
567 //================================================================
568 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
571 FocusOnCurrentPage();
573 void WxGUITextEditor::Quit()
575 std::cout << "-------------- QUIT ---------------"<<std::endl;
576 if (AskSave()) GetParent()->Close();
578 //================================================================
581 //================================================================
584 void WxGUITextEditor::Run()
586 std::cout << "-------------- RUN ---------------"<<std::endl;
588 if (!mwxSplit->IsSplit())
591 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
592 if (size<minsize) size = minsize;
593 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
596 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
597 mInterpreter->InterpretLine("reset");
600 mInterpreter->InterpretBuffer(buffer);
606 //================================================================
611 //================================================================
614 //================================================================
615 void WxGUITextEditor::UpdateInfo()
617 if (mwxNotebook->GetPageCount()==0) return;
618 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
620 long line, column, pos;
621 pos = text->GetInsertionPoint();
622 text->PositionToXY(pos, &column, &line);
625 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
629 sprintf(mess,"%ld:%ld/%ld:%ld",
631 (long)text->GetNumberOfLines(),
632 (long)text->GetLineLength(line)+1);
634 // (long)mwxInputText->GetValue().length())+1;
636 // mwxPosition->SetLabel(wxString(mess));
637 mwxPosition->SetLabel(std2wx(mess));
640 if (text->IsModified())
642 std::string title("*");
643 title += GetCurrentPage()->GetPageName();
644 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
647 //================================================================
648 //================================================================
649 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
651 // std::cout << "U" << std::endl;
654 //================================================================
656 //================================================================
657 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
659 // std::cout << "D" << std::endl;
660 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
661 if ( event.ControlDown() )
663 switch (event.GetKeyCode())
665 case 'n': case 'N' : New(); break;
666 case 's': case 'S' : SaveCurrentPage(); break;
667 case 'o': case 'O' : Open(); break;
668 // case 'r': case 'R' : Run(); break;
669 // case 'q': case 'Q' : Quit(); break;
673 //================================================================
675 //================================================================
676 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
677 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
678 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
679 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
681 //================================================================
687 //================================================================
688 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
691 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
695 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
697 mEditor = new WxGUITextEditor(this);
698 sizer->Add(mEditor,1,wxGROW);
700 // WxGUICommand* com = new WxGUICommand(this,this);
705 // parent window of all bbtk windows will be a child of this
706 Wx::SetTopWindowParent(this);
707 // Add the method OnWxSignal as a Wx::Signal observer
708 //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
714 //================================================================
716 //================================================================
717 WxGUITextEditorWindow::~WxGUITextEditorWindow()
720 //================================================================
726 #endif //_USE_WXWIDGETS_