1 /*=========================================================================
4 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
6 Date: $Date: 2008/03/21 11:46:41 $
7 Version: $Revision: 1.1 $
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"
40 #include "icons/cc_new.xpm"
41 #include "icons/cc_open.xpm"
42 #include "icons/cc_save.xpm"
43 #include "icons/cc_run.xpm"
44 #include "icons/cc_exit.xpm"
51 //================================================================
52 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
55 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
56 const wxPoint &pos, const wxSize &size, int style = 0)
57 : wxTextCtrl(parent, id, value, pos, size, style)
61 void SetWxGUITextEditor(WxGUITextEditor* e)
62 { mWxGUITextEditor = e; }
64 void OnKeyDown(wxKeyEvent& event);
65 void OnKeyUp(wxKeyEvent& event);
66 void OnChar(wxKeyEvent& event);
68 WxGUITextEditor* mWxGUITextEditor;
73 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
74 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
75 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
76 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
80 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
85 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
87 mWxGUITextEditor->OnKeyUp(event);
91 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
93 mWxGUITextEditor->OnKeyDown(event);
96 //================================================================
99 //================================================================
100 class WxGUITextEditorPage : public wxPanel
103 WxGUITextEditorPage(wxWindow* parent, WxGUITextEditor* editor);
104 ~WxGUITextEditorPage();
106 void SetPageName(const std::string& name) { mName = name; }
107 const std::string& GetPageName() const { return mName; }
109 bool AskFilename() const { return mAskFilename; }
111 WxTextCtrlGettingKeyEvents* GetTextCtrl() { return mwxInputText; }
113 void Load(const std::string& filename);
114 void Save(const std::string& filter);
116 bool IsModified() { return mwxInputText->IsModified(); }
119 WxGUITextEditor* mEditor;
120 WxTextCtrlGettingKeyEvents* mwxInputText;
121 wxTextAttr* mwxInputTextAttr;
131 //DECLARE_EVENT_TABLE();
134 //================================================================
136 //================================================================
137 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
138 EVT_CLOSE(WxGUITextEditorPage::OnClose)
142 //================================================================
143 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
144 WxGUITextEditor* editor)
145 : wxPanel(parent,-1),
150 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
153 new WxTextCtrlGettingKeyEvents(this,
159 // |wxTE_PROCESS_ENTER
164 mwxInputText->SetWxGUITextEditor(mEditor);
166 wxFont* FixedFont = new wxFont(10,
172 mwxInputTextAttr = new wxTextAttr;
173 mwxInputTextAttr->SetFont(*FixedFont);
175 sizer->Add(mwxInputText,1,wxGROW);
180 //================================================================
182 //================================================================
183 WxGUITextEditorPage::~WxGUITextEditorPage()
186 //================================================================
189 //================================================================
190 void WxGUITextEditorPage::Load(const std::string& filename)
192 std::cout << "-------------- LOAD ---------------"<<std::endl;
193 std::cout << "'" << filename << "'"<<std::endl;
194 //std::string oldFilename = mFilename;
196 mAskFilename = false;
197 mwxInputText->LoadFile(std2wx(mName));
199 //================================================================
201 //================================================================
202 void WxGUITextEditorPage::Save(const std::string& filter)
204 std::cout << "-------------- SAVE ---------------"<<std::endl;
207 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
208 _T(""),std2wx(filter),
209 wxSAVE | wxOVERWRITE_PROMPT );
212 if (fd->GetReturnCode()==wxID_OK)
214 mName = wx2std(fd->GetPath());
215 mAskFilename = false;
219 std::cout << "-------------- CANCELLED ---------------"
224 std::cout << "file [" << mName << "]" <<std::endl;
225 mwxInputText->SaveFile(mName);
226 mwxInputText->SetModified(false);
228 //================================================================
230 //================================================================
231 WxGUITextEditor::WxGUITextEditor( wxWindow *parent )
232 : wxPanel(parent, -1),
233 mFileNameFilter("*.*")
235 std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
236 // mInterpreter = new bbtk::Interpreter();
237 //mInterpreter->SetWxGUITextEditor(this);
238 // mInterpreter->SetCommandLine(true);
239 m_mgr.SetManagedWindow(this);
243 wxInitAllImageHandlers();
246 // wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
247 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
249 mwxNotebook = new wxAuiNotebook(this,
255 | wxAUI_NB_TAB_EXTERNAL_MOVE
256 | wxAUI_NB_WINDOWLIST_BUTTON
257 |wxAUI_NB_SCROLL_BUTTONS
258 // | wxAUI_NB_CLOSE_BUTTON
259 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
260 //| wxAUI_NB_CLOSE_ON_ALL_TABS
263 sizer->Add(mwxNotebook,1,wxGROW);
266 wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
267 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
269 wxBitmap bmp_new(cc_new_xpm);
270 mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New") );
271 btnsSizer->Add( mwxButtonNew );
272 wxBitmap bmp_open(cc_open_xpm);
273 mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open") );
274 btnsSizer->Add( mwxButtonOpen );
275 wxBitmap bmp_save(cc_save_xpm);
276 mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save") );
277 btnsSizer->Add( mwxButtonSave );
280 wxBitmap bmp_run(cc_run_xpm);
281 mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run") );
282 btnsSizer->Add( mwxButtonRun );
283 wxBitmap bmp_quit(cc_exit_xpm);
284 mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") );
285 btnsSizer->Add( mwxButtonQuit );
288 mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T(""));
289 btnsSizer->Add( mwxPosition );
291 btnsCtrlPanel->SetSizer(btnsSizer);
292 sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW
303 //================================================================
305 //================================================================
306 WxGUITextEditor::~WxGUITextEditor()
309 // delete mInterpreter;
311 //================================================================
313 //================================================================
314 void WxGUITextEditor::NewPage(const std::string& filename)
316 WxGUITextEditorPage* page =
317 new WxGUITextEditorPage(mwxNotebook,this);
318 std::string name("untitled");
319 if (filename.size()!=0)
324 page->SetPageName(name);
325 mwxNotebook->AddPage(page,name,true);
326 FocusOnCurrentPage();
328 //================================================================
330 //================================================================
331 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
333 return (WxGUITextEditorPage*)
334 mwxNotebook->GetPage(mwxNotebook->GetSelection());
336 //================================================================
338 //================================================================
339 void WxGUITextEditor::FocusOnCurrentPage()
341 if (mwxNotebook->GetPageCount()==0) return;
342 GetCurrentPage()->SetFocus();
344 //================================================================
346 //================================================================
347 void WxGUITextEditor::OnButtonOpen(wxCommandEvent& event)
350 FocusOnCurrentPage();
352 void WxGUITextEditor::Open()
354 std::cout << "-------------- OPEN ---------------"<<std::endl;
356 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
357 _T(""),std2wx(mFileNameFilter),
358 wxOPEN | wxFILE_MUST_EXIST );
361 if (fd->GetReturnCode()==wxID_OK)
363 std::string filename = wx2std(fd->GetPath());
364 std::cout << "file [" << filename << "]" <<std::endl;
369 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
372 //================================================================
374 //================================================================
375 void WxGUITextEditor::Open(const std::string& filename)
380 //================================================================
382 //================================================================
383 void WxGUITextEditor::OnButtonSave(wxCommandEvent& event)
387 void WxGUITextEditor::Save()
389 std::cout << "-------------- SAVE ---------------"<<std::endl;
390 if (mwxNotebook->GetPageCount()==0) return;
391 GetCurrentPage()->Save(mFileNameFilter);
392 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
393 GetCurrentPage()->GetPageName());
395 //================================================================
397 //================================================================
398 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
400 std::cout << "-------------- CLOSE ---------------"<<std::endl;
401 if (mwxNotebook->GetPageCount()==0) return;
402 if (GetCurrentPage()->IsModified())
405 new wxMessageDialog(this,
406 _T("Buffer modified. Save it ?"),
408 wxYES_NO | wxCANCEL | wxICON_QUESTION);
409 switch (d->ShowModal())
415 GetCurrentPage()->Save(mFileNameFilter);
422 //================================================================
426 //================================================================
427 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
430 FocusOnCurrentPage();
432 void WxGUITextEditor::Quit()
434 std::cout << "-------------- QUIT ---------------"<<std::endl;
435 if (AskSave()) GetParent()->Close();
437 //================================================================
440 //================================================================
441 void WxGUITextEditor::OnButtonRun(wxCommandEvent& event)
444 FocusOnCurrentPage();
446 void WxGUITextEditor::Run()
448 std::cout << "-------------- RUN ---------------"<<std::endl;
450 if (!mwxSplit->IsSplit())
453 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
454 if (size<minsize) size = minsize;
455 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
458 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
459 mInterpreter->InterpretLine("reset");
462 mInterpreter->InterpretBuffer(buffer);
468 //================================================================
473 //================================================================
475 //================================================================
476 void WxGUITextEditor::OnButtonNew(wxCommandEvent& event)
479 FocusOnCurrentPage();
481 void WxGUITextEditor::New()
483 std::cout << "-------------- NEW ---------------" << std::endl;
487 //================================================================
489 //================================================================
490 void WxGUITextEditor::UpdateInfo()
492 if (mwxNotebook->GetPageCount()==0) return;
493 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
495 long line, column, pos;
496 pos = text->GetInsertionPoint();
497 text->PositionToXY(pos, &column, &line);
500 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
504 sprintf(mess,"%ld:%ld/%ld:%ld",
506 (long)text->GetNumberOfLines(),
507 (long)text->GetLineLength(line)+1);
509 // (long)mwxInputText->GetValue().length())+1;
511 mwxPosition->SetLabel(wxString(mess));
514 if (text->IsModified())
516 std::string title("*");
517 title += GetCurrentPage()->GetPageName();
518 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),title);
521 //================================================================
522 //================================================================
523 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
525 // std::cout << "U" << std::endl;
528 //================================================================
530 //================================================================
531 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
533 // std::cout << "D" << std::endl;
534 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
535 if ( event.ControlDown() )
537 switch (event.GetKeyCode())
539 case 'n': case 'N' : New(); break;
540 case 's': case 'S' : Save(); break;
541 case 'o': case 'O' : Open(); break;
542 // case 'r': case 'R' : Run(); break;
543 // case 'q': case 'Q' : Quit(); break;
547 //================================================================
549 //================================================================
550 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
551 // EVT_CHAR(WxGUITextEditor::OnKeyPress)
552 EVT_BUTTON(WxGUITextEditor::ID_ButtonNew, WxGUITextEditor::OnButtonNew)
553 EVT_BUTTON(WxGUITextEditor::ID_ButtonOpen, WxGUITextEditor::OnButtonOpen)
554 EVT_BUTTON(WxGUITextEditor::ID_ButtonSave, WxGUITextEditor::OnButtonSave)
555 // EVT_BUTTON(WxGUITextEditor::ID_ButtonRun, WxGUITextEditor::OnButtonRun)
556 // EVT_BUTTON(WxGUITextEditor::ID_ButtonQuit, WxGUITextEditor::OnButtonQuit)
557 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
559 //================================================================
565 //================================================================
566 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
569 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
573 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
575 mEditor = new WxGUITextEditor(this);
576 sizer->Add(mEditor,1,wxGROW);
578 // WxGUICommand* com = new WxGUICommand(this,this);
583 // Creates and sets the parent window of all bbtk windows
584 wxWindow* top = new wxPanel(this,-1);
587 Wx::SetTopWindow(top);
592 //================================================================
594 //================================================================
595 WxGUITextEditorWindow::~WxGUITextEditorWindow()
598 //================================================================
604 #endif //_USE_WXWIDGETS_