]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkWxGUITextEditor.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUITextEditor.cxx
index 7de71068cb5abaf552737309a7d6c7bebbd4d0f3..b1f95b4af9449dbd116d9402c1d25b2baccdd0d0 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkWxGUITextEditor.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/03/21 11:46:41 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2008/05/14 12:32:26 $
+  Version:   $Revision: 1.12 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include "bbtkWxGUITextEditor.h"
 #include "bbtkWxBlackBox.h"
 #include "bbtkConfigurationFile.h"
+#include "bbtkUtilities.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
 {
@@ -96,42 +107,6 @@ namespace bbtk
   //================================================================
   
   
-  //================================================================
-  class WxGUITextEditorPage : public wxPanel
-  {
-  public:
-    WxGUITextEditorPage(wxWindow* parent, WxGUITextEditor* editor);
-    ~WxGUITextEditorPage();
-
-    void SetPageName(const std::string& name) { mName = name; }
-    const std::string& GetPageName() const { return mName; }
-
-    bool AskFilename() const { return mAskFilename; }
-
-    WxTextCtrlGettingKeyEvents* GetTextCtrl() { return mwxInputText; }
-
-    void Load(const std::string& filename);
-    void Save(const std::string& filter);
-
-    bool IsModified() { return mwxInputText->IsModified(); }
-   
-  private:
-    WxGUITextEditor* mEditor;
-    WxTextCtrlGettingKeyEvents* mwxInputText;
-    wxTextAttr* mwxInputTextAttr;
-    std::string mName;
-    bool mAskFilename;
-
-    /*
-    enum
-    {
-      ID_InputText
-    };
-    */
-    //DECLARE_EVENT_TABLE();
-
-  } ;
-  //================================================================
 
   //================================================================  
   /*  BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
@@ -185,12 +160,19 @@ namespace bbtk
   }
   //================================================================
 
+  bool WxGUITextEditorPage::IsModified()
+   { return mwxInputText->IsModified(); }
+
+  std::string WxGUITextEditorPage::GetText()
+  {
+    return wx2std(GetTextCtrl()->GetValue());
+  }
 
   //================================================================
   void WxGUITextEditorPage::Load(const std::string& filename)
   {
-    std::cout << "-------------- LOAD ---------------"<<std::endl;
-    std::cout << "'" << filename << "'"<<std::endl;
+    //    std::cout << "-------------- LOAD ---------------"<<std::endl;
+    //    std::cout << "'" << filename << "'"<<std::endl;
     //std::string oldFilename = mFilename;
     mName = filename;
     mAskFilename = false;
@@ -201,7 +183,7 @@ namespace bbtk
   //================================================================
   void WxGUITextEditorPage::Save(const std::string& filter)
   {
-    std::cout << "-------------- SAVE ---------------"<<std::endl;
+    //    std::cout << "-------------- SAVE ---------------"<<std::endl;
     if (mAskFilename)
       {
        wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
@@ -216,36 +198,54 @@ namespace bbtk
          }
        else 
          {
-           std::cout << "-------------- CANCELLED ---------------"
-                     <<std::endl;
+           //      std::cout << "-------------- CANCELLED ---------------"
+           //                <<std::endl;
            return;
          } 
       }
-    std::cout << "file [" << mName << "]" <<std::endl;
-    mwxInputText->SaveFile(mName);
+    //    std::cout << "file [" << mName << "]" <<std::endl;
+    mwxInputText->SaveFile(std2wx(mName));
     mwxInputText->SetModified(false);
   }
   //================================================================
   
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
   //================================================================
-  WxGUITextEditor::WxGUITextEditor( wxWindow *parent )
+  // WxGUITextEditor
+  //================================================================
+
+
+  //================================================================
+  WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
+                                   WxGUITextEditorUser* user )
     : wxPanel(parent, -1),
+      mUser(user),
       mFileNameFilter("*.*")
   {
-    std::cout << "WxGUITextEditor::WxGUITextEditor"<<std::endl;
-    //    mInterpreter = new bbtk::Interpreter();
-    //mInterpreter->SetWxGUITextEditor(this);
-    //  mInterpreter->SetCommandLine(true);
     m_mgr.SetManagedWindow(this);
     
-    //==============
-    // Menu
-    wxInitAllImageHandlers();
-    
-    
-    //    wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
-    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
-    
+    //    wxInitAllImageHandlers();
+      
     mwxNotebook = new wxAuiNotebook(this,  
                                    -1,
                                    wxPoint(0, 0),
@@ -260,43 +260,75 @@ namespace bbtk
                                    //| 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);
-    
+    m_mgr.AddPane(mwxNotebook,
+                 wxAuiPaneInfo().Name(wxT("notebook"))
+                 .Caption(wxT(""))
+                 .CaptionVisible(false)
+                 .MinimizeButton(false)
+                 .MaximizeButton(false)
+                 .CloseButton(false)
+                 //              .Dockable(false).Float()
+                 .Center()
+                 .MinSize(wxSize(100,50))
+                 );   
+
+    /*   
     wxBitmap bmp_new(cc_new_xpm);
-    mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New")  );
-    btnsSizer->Add( mwxButtonNew );
     wxBitmap bmp_open(cc_open_xpm);
-    mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open")  );
-    btnsSizer->Add( mwxButtonOpen );
+    wxBitmap bmp_close(cc_stop_xpm);
     wxBitmap bmp_save(cc_save_xpm);
-    mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save")  );
-    btnsSizer->Add( mwxButtonSave );
-
-    /*
+    wxBitmap bmp_saveas(cc_save_as_xpm);
     wxBitmap bmp_run(cc_run_xpm);
-    mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_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 );
-       
+    */
+    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);
+
+    mwxToolBar = new wxToolBar(this, wxID_ANY, 
+                              wxDefaultPosition, wxDefaultSize,
+                              wxTB_FLAT | wxTB_NODIVIDER);
+    
+    mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
+                     bmp_new, wxNullBitmap, wxITEM_NORMAL,
+                     _T("New file"), _T("Create a new file"));
+    mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
+                     bmp_open, wxNullBitmap, wxITEM_NORMAL,
+                     _T("Open file"), _T("This is help for new file tool"));
+    mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
+                     bmp_close, wxNullBitmap, wxITEM_NORMAL,
+                     _T("Close file"), _T("Close current file"));
+    mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
+                     bmp_save, wxNullBitmap, wxITEM_NORMAL,
+                     _T("Save file"), _T("Save current file"));
+    mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
+                     bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
+                     _T("Save file as"), _T("Save current file as"));
+    mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
+                     bmp_run, wxNullBitmap, wxITEM_NORMAL,
+                     _T("Run file"), _T("Run current file"));
+    mwxToolBar->AddSeparator();
+    mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
+    mwxToolBar->AddControl(mwxPosition);
+    mwxToolBar->Realize();
+
+    m_mgr.AddPane(mwxToolBar, 
+                 wxAuiPaneInfo().Name(wxT("toolBar"))
+                 .Caption(wxT(""))
+                 .ToolbarPane()
+                 .Bottom()
+                 .MinSize(wxSize(100,50))
+                 .LeftDockable(false).RightDockable(false)
+                 );   
 
     NewPage("");
     UpdateInfo();
     
-    SetSizer(sizer);
+    m_mgr.Update();
     SetAutoLayout(true);
     Layout();
   }
@@ -306,6 +338,7 @@ namespace bbtk
   WxGUITextEditor::~WxGUITextEditor()
   {
     m_mgr.UnInit();
+
     //    delete mInterpreter;
   }
   //================================================================
@@ -322,7 +355,8 @@ namespace bbtk
        page->Load(name);
       }
     page->SetPageName(name);
-    mwxNotebook->AddPage(page,name,true);
+    std::string fname = Utilities::get_file_name(name);
+    mwxNotebook->AddPage(page,std2wx(fname),true);
     FocusOnCurrentPage();
   }
   //================================================================
@@ -344,14 +378,56 @@ namespace bbtk
   //================================================================
 
   //================================================================  
-  void WxGUITextEditor::OnButtonOpen(wxCommandEvent& event) 
-  { 
-    Open(); 
-    FocusOnCurrentPage();
-  } 
+  void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
+  {
+    switch (event.GetId())
+      {
+      case ID_ButtonNew :
+       New(); 
+       FocusOnCurrentPage();
+       break;
+      case ID_ButtonOpen :
+       Open(); 
+       FocusOnCurrentPage();
+       break;
+      case ID_ButtonClose :
+       CloseCurrentPage();
+       break;
+      case ID_ButtonSave :
+       SaveCurrentPage();
+       break;
+      case ID_ButtonSaveAs :
+       if (mwxNotebook->GetPageCount()==0) break;  
+       GetCurrentPage()->SetAskFilename(true);
+       SaveCurrentPage();
+       break;
+      case ID_ButtonRun :
+       if ((mUser!=0) && (mwxNotebook->GetPageCount()>0)) 
+         mUser->WxGUITextEditorRun();
+       FocusOnCurrentPage();
+       break;
+      }
+  }
+  //================================================================  
+
+  //================================================================  
+  void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
+  {
+  }
+  //================================================================  
+
+  //================================================================  
+  void WxGUITextEditor::New()
+  {
+    NewPage("");
+    UpdateInfo();
+  }
+  //================================================================
+
+  //================================================================  
   void WxGUITextEditor::Open()
   {
-    std::cout << "-------------- OPEN ---------------"<<std::endl;
+    //    std::cout << "-------------- OPEN ---------------"<<std::endl;
 
     wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
                                        _T(""),std2wx(mFileNameFilter),
@@ -366,7 +442,7 @@ namespace bbtk
       }
     else 
       {
-       std::cout << "-------------- CANCELLED ---------------"<<std::endl;
+       //      std::cout << "-------------- CANCELLED ---------------"<<std::endl;
       }
   }
   //================================================================  
@@ -379,49 +455,74 @@ namespace bbtk
   }
   //================================================================  
 
-  //================================================================  
-  void WxGUITextEditor::OnButtonSave(wxCommandEvent& event) 
-  { 
-    Save();
-  } 
-  void WxGUITextEditor::Save()
-  {
-    std::cout << "-------------- SAVE ---------------"<<std::endl;
-    if (mwxNotebook->GetPageCount()==0) return;  
-    GetCurrentPage()->Save(mFileNameFilter);
-    mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
-                            GetCurrentPage()->GetPageName());
-  }
-  //================================================================  
 
   //================================================================  
-  void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
+  bool WxGUITextEditor::CloseCurrentPage()
   {
-    std::cout << "-------------- CLOSE ---------------"<<std::endl;
-    if (mwxNotebook->GetPageCount()==0) return;
+    if (mwxNotebook->GetPageCount()==0) return true;
+
     if (GetCurrentPage()->IsModified()) 
       {
+       wxString mess = std2wx(GetCurrentPage()->GetPageName());
+       mess += _T(" modified. Save it ?");
        wxMessageDialog* d = 
          new wxMessageDialog(this,
-                             _T("Buffer modified. Save it ?")
+                             mess
                              _T("Save buffer"), 
                              wxYES_NO | wxCANCEL | wxICON_QUESTION);
        switch (d->ShowModal())
          {
          case wxID_CANCEL : 
-           evt.Veto(); 
+           return false;
            break;
          case wxID_YES : 
            GetCurrentPage()->Save(mFileNameFilter); 
            break;
          case wxID_NO : ;
          }       
+      } 
+    mwxNotebook->DeletePage(mwxNotebook->GetSelection());
+    FocusOnCurrentPage();
+    return false;
+  }
+  //================================================================  
+
+  //================================================================  
+  bool WxGUITextEditor::CloseAllPages()
+  {
+    bool ok = true;
+    while (mwxNotebook->GetPageCount()!=0)
+      {
+       if (!CloseCurrentPage()) 
+         {
+           ok = false;
+           break;
+         }
       }
-    
+    return ok;
+  }
+  //================================================================  
+
+  //================================================================  
+  void WxGUITextEditor::SaveCurrentPage()
+  {
+    if (mwxNotebook->GetPageCount()==0) return;  
+    GetCurrentPage()->Save(mFileNameFilter);
+    mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
+                            std2wx(GetCurrentPage()->GetPageName()));
+  }
+  //================================================================  
+
+
+  //================================================================  
+  void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
+  {
+    if (!CloseCurrentPage()) evt.Veto();
   }
   //================================================================
 
 
+
   /*
   //================================================================  
   void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event) 
@@ -435,14 +536,11 @@ namespace bbtk
     if (AskSave()) GetParent()->Close();
   }
   //================================================================  
-
+  */
 
   //================================================================  
-  void WxGUITextEditor::OnButtonRun(wxCommandEvent& event) 
-  { 
-    Run(); 
-    FocusOnCurrentPage();
-  }
+
+  /*
   void WxGUITextEditor::Run()
   {
     std::cout << "-------------- RUN ---------------"<<std::endl;
@@ -472,19 +570,6 @@ namespace bbtk
   
   //================================================================
   
-  //================================================================  
-  void WxGUITextEditor::OnButtonNew(wxCommandEvent& event) 
-  { 
-    New(); 
-    FocusOnCurrentPage();
-  }
-  void WxGUITextEditor::New()
-  {
-    std::cout << "-------------- NEW ---------------" << std::endl;
-    NewPage("");
-    UpdateInfo();
-  }
-  //================================================================
 
   //================================================================
   void WxGUITextEditor::UpdateInfo()
@@ -508,14 +593,15 @@ namespace bbtk
     //      pos+1,
     //      (long)mwxInputText->GetValue().length())+1;
     
-    mwxPosition->SetLabel(wxString(mess));
+   // mwxPosition->SetLabel(wxString(mess));
+    mwxPosition->SetLabel(std2wx(mess));    
     mwxPosition->Show();
     
     if (text->IsModified()) 
       {
        std::string title("*");
        title += GetCurrentPage()->GetPageName();
-       mwxNotebook->SetPageText(mwxNotebook->GetSelection(),title);
+       mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
       }
   }
   //================================================================
@@ -537,7 +623,7 @@ namespace bbtk
        switch (event.GetKeyCode())
          {
          case 'n': case 'N' : New(); break;
-         case 's': case 'S' : Save(); break;
+         case 's': case 'S' : SaveCurrentPage(); break;
          case 'o': case 'O' : Open(); break;
            //    case 'r': case 'R' : Run(); break;
            //    case 'q': case 'Q' : Quit(); break;
@@ -548,12 +634,8 @@ namespace bbtk
   
   //================================================================  
   BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
-  //    EVT_CHAR(WxGUITextEditor::OnKeyPress)
-    EVT_BUTTON(WxGUITextEditor::ID_ButtonNew, WxGUITextEditor::OnButtonNew)
-    EVT_BUTTON(WxGUITextEditor::ID_ButtonOpen, WxGUITextEditor::OnButtonOpen)
-    EVT_BUTTON(WxGUITextEditor::ID_ButtonSave, WxGUITextEditor::OnButtonSave)
-  //    EVT_BUTTON(WxGUITextEditor::ID_ButtonRun, WxGUITextEditor::OnButtonRun)
-  //    EVT_BUTTON(WxGUITextEditor::ID_ButtonQuit, WxGUITextEditor::OnButtonQuit)
+    EVT_MENU(wxID_ANY,  WxGUITextEditor::OnToolLeftClick)
+    EVT_TOOL_RCLICKED(wxID_ANY,  WxGUITextEditor::OnToolRightClick)
     EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
     END_EVENT_TABLE()
   //================================================================
@@ -580,11 +662,11 @@ namespace bbtk
     
     SetSizer(sizer);
 
-    // Creates and sets the parent window of all bbtk windows
-    wxWindow* top = new wxPanel(this,-1);
-    top->Hide();
+    // Creates the parent window of all bbtk windows as a child of this
+    Wx::CreateTopWindow(this);
+    // Add the method OnWxSignal as a Wx::Signal observer 
+    //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
     
-    Wx::SetTopWindow(top);
    
     SetAutoLayout(true);
     Layout();