1 /*=========================================================================
4 Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $
6 Date: $Date: 2008/03/21 14:59:39 $
7 Version: $Revision: 1.2 $
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 //================================================================
100 //================================================================
101 /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
102 EVT_CLOSE(WxGUITextEditorPage::OnClose)
106 //================================================================
107 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
108 WxGUITextEditor* editor)
109 : wxPanel(parent,-1),
114 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
117 new WxTextCtrlGettingKeyEvents(this,
123 // |wxTE_PROCESS_ENTER
128 mwxInputText->SetWxGUITextEditor(mEditor);
130 wxFont* FixedFont = new wxFont(10,
136 mwxInputTextAttr = new wxTextAttr;
137 mwxInputTextAttr->SetFont(*FixedFont);
139 sizer->Add(mwxInputText,1,wxGROW);
144 //================================================================
146 //================================================================
147 WxGUITextEditorPage::~WxGUITextEditorPage()
150 //================================================================
152 bool WxGUITextEditorPage::IsModified()
153 { return mwxInputText->IsModified(); }
155 std::string WxGUITextEditorPage::GetText()
157 return wx2std(GetTextCtrl()->GetValue());
160 //================================================================
161 void WxGUITextEditorPage::Load(const std::string& filename)
163 std::cout << "-------------- LOAD ---------------"<<std::endl;
164 std::cout << "'" << filename << "'"<<std::endl;
165 //std::string oldFilename = mFilename;
167 mAskFilename = false;
168 mwxInputText->LoadFile(std2wx(mName));
170 //================================================================
172 //================================================================
173 void WxGUITextEditorPage::Save(const std::string& filter)
175 std::cout << "-------------- SAVE ---------------"<<std::endl;
178 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
179 _T(""),std2wx(filter),
180 wxSAVE | wxOVERWRITE_PROMPT );
183 if (fd->GetReturnCode()==wxID_OK)
185 mName = wx2std(fd->GetPath());
186 mAskFilename = false;
190 std::cout << "-------------- CANCELLED ---------------"
195 std::cout << "file [" << mName << "]" <<std::endl;
196 mwxInputText->SaveFile(mName);
197 mwxInputText->SetModified(false);
199 //================================================================
201 //================================================================
202 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
203 WxGUITextEditorUser* user )
204 : wxPanel(parent, -1),
206 mFileNameFilter("*.*")
208 std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
209 // mInterpreter = new bbtk::Interpreter();
210 //mInterpreter->SetWxGUITextEditor(this);
211 // mInterpreter->SetCommandLine(true);
212 m_mgr.SetManagedWindow(this);
216 wxInitAllImageHandlers();
219 // wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
220 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
222 mwxNotebook = new wxAuiNotebook(this,
228 | wxAUI_NB_TAB_EXTERNAL_MOVE
229 | wxAUI_NB_WINDOWLIST_BUTTON
230 |wxAUI_NB_SCROLL_BUTTONS
231 // | wxAUI_NB_CLOSE_BUTTON
232 | wxAUI_NB_CLOSE_ON_ACTIVE_TAB
233 //| wxAUI_NB_CLOSE_ON_ALL_TABS
236 sizer->Add(mwxNotebook,1,wxGROW);
239 wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
240 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
242 wxBitmap bmp_new(cc_new_xpm);
243 mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New") );
244 btnsSizer->Add( mwxButtonNew );
245 wxBitmap bmp_open(cc_open_xpm);
246 mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open") );
247 btnsSizer->Add( mwxButtonOpen );
248 wxBitmap bmp_save(cc_save_xpm);
249 mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save") );
250 btnsSizer->Add( mwxButtonSave );
252 wxBitmap bmp_run(cc_run_xpm);
253 mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run") );
254 btnsSizer->Add( mwxButtonRun );
256 wxBitmap bmp_quit(cc_exit_xpm);
257 mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") );
258 btnsSizer->Add( mwxButtonQuit );
261 mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T(""));
262 btnsSizer->Add( mwxPosition );
264 btnsCtrlPanel->SetSizer(btnsSizer);
265 sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW
276 //================================================================
278 //================================================================
279 WxGUITextEditor::~WxGUITextEditor()
282 // delete mInterpreter;
284 //================================================================
286 //================================================================
287 void WxGUITextEditor::NewPage(const std::string& filename)
289 WxGUITextEditorPage* page =
290 new WxGUITextEditorPage(mwxNotebook,this);
291 std::string name("untitled");
292 if (filename.size()!=0)
297 page->SetPageName(name);
298 mwxNotebook->AddPage(page,name,true);
299 FocusOnCurrentPage();
301 //================================================================
303 //================================================================
304 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
306 return (WxGUITextEditorPage*)
307 mwxNotebook->GetPage(mwxNotebook->GetSelection());
309 //================================================================
311 //================================================================
312 void WxGUITextEditor::FocusOnCurrentPage()
314 if (mwxNotebook->GetPageCount()==0) return;
315 GetCurrentPage()->SetFocus();
317 //================================================================
319 //================================================================
320 void WxGUITextEditor::OnButtonOpen(wxCommandEvent& event)
323 FocusOnCurrentPage();
325 void WxGUITextEditor::Open()
327 std::cout << "-------------- OPEN ---------------"<<std::endl;
329 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
330 _T(""),std2wx(mFileNameFilter),
331 wxOPEN | wxFILE_MUST_EXIST );
334 if (fd->GetReturnCode()==wxID_OK)
336 std::string filename = wx2std(fd->GetPath());
337 std::cout << "file [" << filename << "]" <<std::endl;
342 std::cout << "-------------- CANCELLED ---------------"<<std::endl;
345 //================================================================
347 //================================================================
348 void WxGUITextEditor::Open(const std::string& filename)
353 //================================================================
355 //================================================================
356 void WxGUITextEditor::OnButtonSave(wxCommandEvent& event)
360 void WxGUITextEditor::Save()
362 std::cout << "-------------- SAVE ---------------"<<std::endl;
363 if (mwxNotebook->GetPageCount()==0) return;
364 GetCurrentPage()->Save(mFileNameFilter);
365 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
366 GetCurrentPage()->GetPageName());
368 //================================================================
370 //================================================================
371 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
373 std::cout << "-------------- CLOSE ---------------"<<std::endl;
374 if (mwxNotebook->GetPageCount()==0) return;
375 if (GetCurrentPage()->IsModified())
378 new wxMessageDialog(this,
379 _T("Buffer modified. Save it ?"),
381 wxYES_NO | wxCANCEL | wxICON_QUESTION);
382 switch (d->ShowModal())
388 GetCurrentPage()->Save(mFileNameFilter);
395 //================================================================
399 //================================================================
400 void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event)
403 FocusOnCurrentPage();
405 void WxGUITextEditor::Quit()
407 std::cout << "-------------- QUIT ---------------"<<std::endl;
408 if (AskSave()) GetParent()->Close();
410 //================================================================
413 //================================================================
414 void WxGUITextEditor::OnButtonRun(wxCommandEvent& event)
416 if (mUser!=0) mUser->WxGUITextEditorRun();
417 FocusOnCurrentPage();
421 void WxGUITextEditor::Run()
423 std::cout << "-------------- RUN ---------------"<<std::endl;
425 if (!mwxSplit->IsSplit())
428 int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
429 if (size<minsize) size = minsize;
430 mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
433 std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
434 mInterpreter->InterpretLine("reset");
437 mInterpreter->InterpretBuffer(buffer);
443 //================================================================
448 //================================================================
450 //================================================================
451 void WxGUITextEditor::OnButtonNew(wxCommandEvent& event)
454 FocusOnCurrentPage();
456 void WxGUITextEditor::New()
458 std::cout << "-------------- NEW ---------------" << std::endl;
462 //================================================================
464 //================================================================
465 void WxGUITextEditor::UpdateInfo()
467 if (mwxNotebook->GetPageCount()==0) return;
468 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
470 long line, column, pos;
471 pos = text->GetInsertionPoint();
472 text->PositionToXY(pos, &column, &line);
475 // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
479 sprintf(mess,"%ld:%ld/%ld:%ld",
481 (long)text->GetNumberOfLines(),
482 (long)text->GetLineLength(line)+1);
484 // (long)mwxInputText->GetValue().length())+1;
486 mwxPosition->SetLabel(wxString(mess));
489 if (text->IsModified())
491 std::string title("*");
492 title += GetCurrentPage()->GetPageName();
493 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),title);
496 //================================================================
497 //================================================================
498 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
500 // std::cout << "U" << std::endl;
503 //================================================================
505 //================================================================
506 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
508 // std::cout << "D" << std::endl;
509 // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
510 if ( event.ControlDown() )
512 switch (event.GetKeyCode())
514 case 'n': case 'N' : New(); break;
515 case 's': case 'S' : Save(); break;
516 case 'o': case 'O' : Open(); break;
517 // case 'r': case 'R' : Run(); break;
518 // case 'q': case 'Q' : Quit(); break;
522 //================================================================
524 //================================================================
525 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
526 // EVT_CHAR(WxGUITextEditor::OnKeyPress)
527 EVT_BUTTON(WxGUITextEditor::ID_ButtonNew, WxGUITextEditor::OnButtonNew)
528 EVT_BUTTON(WxGUITextEditor::ID_ButtonOpen, WxGUITextEditor::OnButtonOpen)
529 EVT_BUTTON(WxGUITextEditor::ID_ButtonSave, WxGUITextEditor::OnButtonSave)
530 EVT_BUTTON(WxGUITextEditor::ID_ButtonRun, WxGUITextEditor::OnButtonRun)
531 // EVT_BUTTON(WxGUITextEditor::ID_ButtonQuit, WxGUITextEditor::OnButtonQuit)
532 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
534 //================================================================
540 //================================================================
541 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
544 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
548 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
550 mEditor = new WxGUITextEditor(this);
551 sizer->Add(mEditor,1,wxGROW);
553 // WxGUICommand* com = new WxGUICommand(this,this);
558 // Creates and sets the parent window of all bbtk windows
559 wxWindow* top = new wxPanel(this,-1);
562 Wx::SetTopWindow(top);
567 //================================================================
569 //================================================================
570 WxGUITextEditorWindow::~WxGUITextEditorWindow()
573 //================================================================
579 #endif //_USE_WXWIDGETS_