/*========================================================================= Program: bbtk Module: $RCSfile: bbtkWxGUITextEditor.cxx,v $ Language: C++ Date: $Date: 2008/03/26 15:04:46 $ Version: $Revision: 1.7 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*//** * \brief Short description in one line * * Long description which * can span multiple lines */ /** * \file * \brief */ /** * \class bbtk:: * \brief */ #ifdef _USE_WXWIDGETS_ #include #include "bbtkWxGUITextEditor.h" #include "bbtkWxBlackBox.h" #include "bbtkConfigurationFile.h" #include "icons/cc_new.xpm" #include "icons/cc_open.xpm" #include "icons/cc_stop.xpm" #include "icons/cc_save.xpm" #include "icons/cc_save_as.xpm" #include "icons/cc_run.xpm" #include "icons/cc_exit.xpm" #include "../data/icons/wxart_new.xpm" #include "../data/icons/wxart_fileopen.xpm" #include "../data/icons/wxart_filesave.xpm" #include "../data/icons/wxart_filesaveas.xpm" #include "../data/icons/wxart_exefile.xpm" #include "../data/icons/wxart_delete.xpm" #include "../data/icons/wxart_down.xpm" #include "../data/icons/wxart_eldel.xpm" namespace bbtk { //================================================================ class WxTextCtrlGettingKeyEvents : public wxTextCtrl { public: WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos, const wxSize &size, int style = 0) : wxTextCtrl(parent, id, value, pos, size, style) { } void SetWxGUITextEditor(WxGUITextEditor* e) { mWxGUITextEditor = e; } void OnKeyDown(wxKeyEvent& event); void OnKeyUp(wxKeyEvent& event); void OnChar(wxKeyEvent& event); private : WxGUITextEditor* mWxGUITextEditor; DECLARE_EVENT_TABLE() }; BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl) EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown) EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp) EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar) END_EVENT_TABLE() void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event) { event.Skip(); } void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event) { mWxGUITextEditor->OnKeyUp(event); event.Skip(); } void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event) { mWxGUITextEditor->OnKeyDown(event); event.Skip(); } //================================================================ //================================================================ /* BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel) EVT_CLOSE(WxGUITextEditorPage::OnClose) END_EVENT_TABLE() */ //================================================================ WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent, WxGUITextEditor* editor) : wxPanel(parent,-1), mEditor(editor), mName(""), mAskFilename(true) { wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); mwxInputText = new WxTextCtrlGettingKeyEvents(this, -1, //ID_InputText, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE // |wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB // | wxWANTS_CHARS | wxTAB_TRAVERSAL ); mwxInputText->SetWxGUITextEditor(mEditor); wxFont* FixedFont = new wxFont(10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false); mwxInputTextAttr = new wxTextAttr; mwxInputTextAttr->SetFont(*FixedFont); sizer->Add(mwxInputText,1,wxGROW); SetSizer(sizer); SetAutoLayout(true); Layout(); } //================================================================ //================================================================ WxGUITextEditorPage::~WxGUITextEditorPage() { } //================================================================ bool WxGUITextEditorPage::IsModified() { return mwxInputText->IsModified(); } std::string WxGUITextEditorPage::GetText() { return wx2std(GetTextCtrl()->GetValue()); } //================================================================ void WxGUITextEditorPage::Load(const std::string& filename) { std::cout << "-------------- LOAD ---------------"<LoadFile(std2wx(mName)); } //================================================================ //================================================================ void WxGUITextEditorPage::Save(const std::string& filter) { std::cout << "-------------- SAVE ---------------"<ShowModal(); if (fd->GetReturnCode()==wxID_OK) { mName = wx2std(fd->GetPath()); mAskFilename = false; } else { std::cout << "-------------- CANCELLED ---------------" <SaveFile(std2wx(mName)); mwxInputText->SetModified(false); } //================================================================ //================================================================ WxGUITextEditor::WxGUITextEditor( wxWindow *parent, WxGUITextEditorUser* user ) : wxPanel(parent, -1), mUser(user), mFileNameFilter("*.*") { std::cout << "WxGUITextEditor::WxGUITextEditor"<SetWxGUITextEditor(this); // mInterpreter->SetCommandLine(true); m_mgr.SetManagedWindow(this); //============== // Menu wxInitAllImageHandlers(); // wxFlexGridSizer *sizer= new wxFlexGridSizer(2); wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); mwxNotebook = new wxAuiNotebook(this, -1, wxPoint(0, 0), wxSize(500,500), wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_MOVE | wxAUI_NB_TAB_EXTERNAL_MOVE | wxAUI_NB_WINDOWLIST_BUTTON |wxAUI_NB_SCROLL_BUTTONS // | wxAUI_NB_CLOSE_BUTTON | wxAUI_NB_CLOSE_ON_ACTIVE_TAB //| wxAUI_NB_CLOSE_ON_ALL_TABS | wxNO_BORDER); sizer->Add(mwxNotebook,1,wxGROW); // BUTTONS wxPanel *btnsCtrlPanel = new wxPanel(this,-1); wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL); /* wxBitmap bmp_new(cc_new_xpm); wxBitmap bmp_open(cc_open_xpm); wxBitmap bmp_close(cc_stop_xpm); wxBitmap bmp_save(cc_save_xpm); wxBitmap bmp_saveas(cc_save_as_xpm); wxBitmap bmp_run(cc_run_xpm); */ wxBitmap bmp_new(new_xpm); wxBitmap bmp_open(fileopen_xpm); wxBitmap bmp_close(eldel_xpm); wxBitmap bmp_save(filesave_xpm); wxBitmap bmp_saveas(filesaveas_xpm); wxBitmap bmp_run(down_xpm); wxSize btn_size(32,32); mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new ,wxDefaultPosition, btn_size); btnsSizer->Add( mwxButtonNew ); mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open ,wxDefaultPosition, btn_size);//,_T("Open") ); btnsSizer->Add( mwxButtonOpen ); mwxButtonClose = new wxBitmapButton( btnsCtrlPanel,ID_ButtonClose,bmp_close ,wxDefaultPosition, btn_size);//,_T("Open") ); btnsSizer->Add( mwxButtonClose ); mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save ,wxDefaultPosition, btn_size);//_T("Save") ); btnsSizer->Add( mwxButtonSave ); mwxButtonSaveAs = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSaveAs,bmp_saveas ,wxDefaultPosition, btn_size);//_T("Save") ); btnsSizer->Add( mwxButtonSaveAs ); mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run ,wxDefaultPosition, btn_size);//_T("Run") ); btnsSizer->Add( mwxButtonRun ); /* wxBitmap bmp_quit(cc_exit_xpm); mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit") ); btnsSizer->Add( mwxButtonQuit ); */ mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T("")); btnsSizer->Add( mwxPosition ); btnsCtrlPanel->SetSizer(btnsSizer); sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM //| wxGROW , 10 ); NewPage(""); UpdateInfo(); SetSizer(sizer); SetAutoLayout(true); Layout(); } //================================================================ //================================================================ WxGUITextEditor::~WxGUITextEditor() { m_mgr.UnInit(); // delete mInterpreter; } //================================================================ //================================================================ void WxGUITextEditor::NewPage(const std::string& filename) { WxGUITextEditorPage* page = new WxGUITextEditorPage(mwxNotebook,this); std::string name("untitled"); if (filename.size()!=0) { name = filename; page->Load(name); } page->SetPageName(name); mwxNotebook->AddPage(page,std2wx(name),true); FocusOnCurrentPage(); } //================================================================ //================================================================ WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage() { return (WxGUITextEditorPage*) mwxNotebook->GetPage(mwxNotebook->GetSelection()); } //================================================================ //================================================================ void WxGUITextEditor::FocusOnCurrentPage() { if (mwxNotebook->GetPageCount()==0) return; GetCurrentPage()->SetFocus(); } //================================================================ //================================================================ void WxGUITextEditor::OnButtonOpen(wxCommandEvent& event) { Open(); FocusOnCurrentPage(); } void WxGUITextEditor::Open() { std::cout << "-------------- OPEN ---------------"<ShowModal(); if (fd->GetReturnCode()==wxID_OK) { std::string filename = wx2std(fd->GetPath()); std::cout << "file [" << filename << "]" <GetPageCount()==0) return true; if (GetCurrentPage()->IsModified()) { wxString mess = std2wx(GetCurrentPage()->GetPageName()); mess += _T(" modified. Save it ?"); wxMessageDialog* d = new wxMessageDialog(this, mess, _T("Save buffer"), wxYES_NO | wxCANCEL | wxICON_QUESTION); switch (d->ShowModal()) { case wxID_CANCEL : return false; break; case wxID_YES : GetCurrentPage()->Save(mFileNameFilter); break; case wxID_NO : ; } } mwxNotebook->DeletePage(mwxNotebook->GetSelection()); FocusOnCurrentPage(); return true; } //================================================================ //================================================================ bool WxGUITextEditor::CloseAllPages() { bool ok = true; while (mwxNotebook->GetPageCount()!=0) { if (!CloseCurrentPage()) { ok = false; break; } } return ok; } //================================================================ //================================================================ void WxGUITextEditor::OnButtonSave(wxCommandEvent& event) { SaveCurrentPage(); } void WxGUITextEditor::SaveCurrentPage() { if (mwxNotebook->GetPageCount()==0) return; GetCurrentPage()->Save(mFileNameFilter); mwxNotebook->SetPageText(mwxNotebook->GetSelection(), std2wx(GetCurrentPage()->GetPageName())); } //================================================================ //================================================================ void WxGUITextEditor::OnButtonSaveAs(wxCommandEvent& event) { if (mwxNotebook->GetPageCount()==0) return; GetCurrentPage()->SetAskFilename(true); SaveCurrentPage(); } //================================================================ //================================================================ void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt) { std::cout << "-------------- CLOSE ---------------"<GetPageCount()==0) return; if (GetCurrentPage()->IsModified()) { wxMessageDialog* d = new wxMessageDialog(this, _T("Buffer modified. Save it ?"), _T("Save buffer"), wxYES_NO | wxCANCEL | wxICON_QUESTION); switch (d->ShowModal()) { case wxID_CANCEL : evt.Veto(); break; case wxID_YES : GetCurrentPage()->Save(mFileNameFilter); break; case wxID_NO : ; } } */ } //================================================================ /* //================================================================ void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event) { Quit(); FocusOnCurrentPage(); } void WxGUITextEditor::Quit() { std::cout << "-------------- QUIT ---------------"<Close(); } //================================================================ */ //================================================================ void WxGUITextEditor::OnButtonRun(wxCommandEvent& event) { if ((mUser!=0) && (mwxNotebook->GetPageCount()>0)) mUser->WxGUITextEditorRun(); FocusOnCurrentPage(); } /* void WxGUITextEditor::Run() { std::cout << "-------------- RUN ---------------"<IsSplit()) { int size = -100; int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.); if (sizeSplitHorizontally( mwxInputText, mwxOutputText, size); } std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue())); mInterpreter->InterpretLine("reset"); try { mInterpreter->InterpretBuffer(buffer); } catch (...) { } } //================================================================ */ //================================================================ //================================================================ void WxGUITextEditor::OnButtonNew(wxCommandEvent& event) { New(); FocusOnCurrentPage(); } void WxGUITextEditor::New() { std::cout << "-------------- NEW ---------------" << std::endl; NewPage(""); UpdateInfo(); } //================================================================ //================================================================ void WxGUITextEditor::UpdateInfo() { if (mwxNotebook->GetPageCount()==0) return; WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl(); long line, column, pos; pos = text->GetInsertionPoint(); text->PositionToXY(pos, &column, &line); // wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"), char mess[200]; sprintf(mess,"%ld:%ld/%ld:%ld", line+1,column+1, (long)text->GetNumberOfLines(), (long)text->GetLineLength(line)+1); // pos+1, // (long)mwxInputText->GetValue().length())+1; // mwxPosition->SetLabel(wxString(mess)); mwxPosition->SetLabel(std2wx(mess)); mwxPosition->Show(); if (text->IsModified()) { std::string title("*"); title += GetCurrentPage()->GetPageName(); mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title)); } } //================================================================ //================================================================ void WxGUITextEditor::OnKeyUp(wxKeyEvent& event) { // std::cout << "U" << std::endl; UpdateInfo(); } //================================================================ //================================================================ void WxGUITextEditor::OnKeyDown(wxKeyEvent& event) { // std::cout << "D" << std::endl; // std::cout << "Key="<Add(mEditor,1,wxGROW); // WxGUICommand* com = new WxGUICommand(this,this); // sizer->Add(com); SetSizer(sizer); // Creates and sets the parent window of all bbtk windows wxWindow* top = new wxPanel(this,-1); top->Hide(); Wx::SetTopWindow(top); SetAutoLayout(true); Layout(); } //================================================================ //================================================================ WxGUITextEditorWindow::~WxGUITextEditorWindow() { } //================================================================ } // namespace bbtk #endif //_USE_WXWIDGETS_