]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUITextEditor.cxx
b1f95b4af9449dbd116d9402c1d25b2baccdd0d0
[bbtk.git] / kernel / src / bbtkWxGUITextEditor.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUITextEditor.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/05/14 12:32:26 $
7   Version:   $Revision: 1.12 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*//**
18  * \brief Short description in one line
19  * 
20  * Long description which 
21  * can span multiple lines
22  */
23 /**
24  * \file 
25  * \brief 
26  */
27 /**
28  * \class bbtk::
29  * \brief 
30  */
31
32
33 #ifdef _USE_WXWIDGETS_
34
35 #include <iostream>     
36 #include "bbtkWxGUITextEditor.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
39 #include "bbtkUtilities.h"
40
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"
48
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"
57
58 namespace bbtk
59 {
60
61
62   //================================================================
63   class WxTextCtrlGettingKeyEvents : public wxTextCtrl
64   {
65   public:
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)
69     {
70     }
71     
72     void SetWxGUITextEditor(WxGUITextEditor* e) 
73     { mWxGUITextEditor = e; }
74     
75     void OnKeyDown(wxKeyEvent& event);
76     void OnKeyUp(wxKeyEvent& event);
77     void OnChar(wxKeyEvent& event);
78   private :
79     WxGUITextEditor* mWxGUITextEditor;
80     
81     DECLARE_EVENT_TABLE()
82       };
83   
84   BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
85     EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
86     EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
87     EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
88     END_EVENT_TABLE()
89     
90     
91     void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
92   {
93     event.Skip();
94   }
95   
96   void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
97   {
98     mWxGUITextEditor->OnKeyUp(event);
99     event.Skip();
100   }
101   
102   void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
103   {
104     mWxGUITextEditor->OnKeyDown(event);
105     event.Skip();
106   }
107   //================================================================
108   
109   
110
111   //================================================================  
112   /*  BEGIN_EVENT_TABLE(WxGUITextEditorPage, wxPanel)
113     EVT_CLOSE(WxGUITextEditorPage::OnClose)
114     END_EVENT_TABLE()
115   */
116  
117   //================================================================
118   WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
119                                                WxGUITextEditor* editor)
120     : wxPanel(parent,-1),
121       mEditor(editor),
122       mName(""),
123       mAskFilename(true)
124   {  
125     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
126  
127     mwxInputText = 
128       new WxTextCtrlGettingKeyEvents(this,
129                                      -1, //ID_InputText,
130                                      _T(""),
131                                      wxDefaultPosition,
132                                      wxDefaultSize,
133                                      wxTE_MULTILINE 
134                                      //    |wxTE_PROCESS_ENTER
135                                      | wxTE_PROCESS_TAB 
136                                      //             | wxWANTS_CHARS 
137                                      |  wxTAB_TRAVERSAL
138                                      );
139     mwxInputText->SetWxGUITextEditor(mEditor);
140     
141     wxFont* FixedFont = new wxFont(10,
142                                    wxFONTFAMILY_MODERN,
143                                    wxFONTSTYLE_NORMAL,
144                                    wxFONTWEIGHT_NORMAL,
145                                    false);
146     
147     mwxInputTextAttr = new wxTextAttr;
148     mwxInputTextAttr->SetFont(*FixedFont);
149    
150     sizer->Add(mwxInputText,1,wxGROW);
151     SetSizer(sizer);
152     SetAutoLayout(true);
153     Layout();
154   }
155   //================================================================
156
157   //================================================================
158   WxGUITextEditorPage::~WxGUITextEditorPage()
159   {
160   }
161   //================================================================
162
163   bool WxGUITextEditorPage::IsModified()
164    { return mwxInputText->IsModified(); }
165
166   std::string WxGUITextEditorPage::GetText()
167   {
168     return wx2std(GetTextCtrl()->GetValue());
169   }
170
171   //================================================================
172   void WxGUITextEditorPage::Load(const std::string& filename)
173   {
174     //    std::cout << "-------------- LOAD ---------------"<<std::endl;
175     //    std::cout << "'" << filename << "'"<<std::endl;
176     //std::string oldFilename = mFilename;
177     mName = filename;
178     mAskFilename = false;
179     mwxInputText->LoadFile(std2wx(mName));
180   }
181   //================================================================
182
183   //================================================================
184   void WxGUITextEditorPage::Save(const std::string& filter)
185   {
186     //    std::cout << "-------------- SAVE ---------------"<<std::endl;
187     if (mAskFilename)
188       {
189         wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
190                                             _T(""),std2wx(filter),
191                                             wxSAVE | wxOVERWRITE_PROMPT );
192         fd->ShowModal();
193     
194         if (fd->GetReturnCode()==wxID_OK)
195           {
196             mName = wx2std(fd->GetPath());
197             mAskFilename = false;
198           }
199         else 
200           {
201             //      std::cout << "-------------- CANCELLED ---------------"
202             //                <<std::endl;
203             return;
204           } 
205       }
206     //    std::cout << "file [" << mName << "]" <<std::endl;
207     mwxInputText->SaveFile(std2wx(mName));
208     mwxInputText->SetModified(false);
209   }
210   //================================================================
211   
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233   //================================================================
234   // WxGUITextEditor
235   //================================================================
236
237
238   //================================================================
239   WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
240                                     WxGUITextEditorUser* user )
241     : wxPanel(parent, -1),
242       mUser(user),
243       mFileNameFilter("*.*")
244   {
245     m_mgr.SetManagedWindow(this);
246     
247     //    wxInitAllImageHandlers();
248       
249     mwxNotebook = new wxAuiNotebook(this,  
250                                     -1,
251                                     wxPoint(0, 0),
252                                     wxSize(500,500),
253                                     wxAUI_NB_TAB_SPLIT 
254                                     | wxAUI_NB_TAB_MOVE
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
261                                     | wxNO_BORDER);
262     
263     m_mgr.AddPane(mwxNotebook,
264                   wxAuiPaneInfo().Name(wxT("notebook"))
265                   .Caption(wxT(""))
266                   .CaptionVisible(false)
267                   .MinimizeButton(false)
268                   .MaximizeButton(false)
269                   .CloseButton(false)
270                   //              .Dockable(false).Float()
271                   .Center()
272                   .MinSize(wxSize(100,50))
273                   );   
274
275  
276     /*   
277     wxBitmap bmp_new(cc_new_xpm);
278     wxBitmap bmp_open(cc_open_xpm);
279     wxBitmap bmp_close(cc_stop_xpm);
280     wxBitmap bmp_save(cc_save_xpm);
281     wxBitmap bmp_saveas(cc_save_as_xpm);
282     wxBitmap bmp_run(cc_run_xpm);
283     */
284     wxBitmap bmp_new(new_xpm);
285     wxBitmap bmp_open(fileopen_xpm);
286     wxBitmap bmp_close(eldel_xpm);
287     wxBitmap bmp_save(filesave_xpm);
288     wxBitmap bmp_saveas(filesaveas_xpm);
289     wxBitmap bmp_run(down_xpm);
290
291     mwxToolBar = new wxToolBar(this, wxID_ANY, 
292                                wxDefaultPosition, wxDefaultSize,
293                                wxTB_FLAT | wxTB_NODIVIDER);
294     
295     mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
296                      bmp_new, wxNullBitmap, wxITEM_NORMAL,
297                      _T("New file"), _T("Create a new file"));
298     mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
299                      bmp_open, wxNullBitmap, wxITEM_NORMAL,
300                      _T("Open file"), _T("This is help for new file tool"));
301     mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
302                      bmp_close, wxNullBitmap, wxITEM_NORMAL,
303                      _T("Close file"), _T("Close current file"));
304     mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
305                      bmp_save, wxNullBitmap, wxITEM_NORMAL,
306                      _T("Save file"), _T("Save current file"));
307     mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
308                      bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
309                      _T("Save file as"), _T("Save current file as"));
310     mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
311                      bmp_run, wxNullBitmap, wxITEM_NORMAL,
312                      _T("Run file"), _T("Run current file"));
313  
314     mwxToolBar->AddSeparator();
315     mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
316     mwxToolBar->AddControl(mwxPosition);
317     mwxToolBar->Realize();
318
319     m_mgr.AddPane(mwxToolBar, 
320                   wxAuiPaneInfo().Name(wxT("toolBar"))
321                   .Caption(wxT(""))
322                   .ToolbarPane()
323                   .Bottom()
324                   .MinSize(wxSize(100,50))
325                   .LeftDockable(false).RightDockable(false)
326                   );   
327
328     NewPage("");
329     UpdateInfo();
330     
331     m_mgr.Update();
332     SetAutoLayout(true);
333     Layout();
334   }
335   //================================================================
336   
337   //================================================================
338   WxGUITextEditor::~WxGUITextEditor()
339   {
340     m_mgr.UnInit();
341
342     //    delete mInterpreter;
343   }
344   //================================================================
345
346   //================================================================
347   void WxGUITextEditor::NewPage(const std::string& filename)
348   {
349    WxGUITextEditorPage* page = 
350       new WxGUITextEditorPage(mwxNotebook,this);
351     std::string name("untitled");
352     if (filename.size()!=0) 
353       {
354         name = filename;
355         page->Load(name);
356       }
357     page->SetPageName(name);
358     std::string fname = Utilities::get_file_name(name);
359     mwxNotebook->AddPage(page,std2wx(fname),true);
360     FocusOnCurrentPage();
361   }
362   //================================================================
363
364   //================================================================
365   WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
366   {
367     return (WxGUITextEditorPage*)
368       mwxNotebook->GetPage(mwxNotebook->GetSelection());
369   }
370   //================================================================
371
372   //================================================================
373   void WxGUITextEditor::FocusOnCurrentPage()
374   {
375     if (mwxNotebook->GetPageCount()==0) return;
376     GetCurrentPage()->SetFocus();    
377   }
378   //================================================================
379
380   //================================================================  
381   void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
382   {
383     switch (event.GetId())
384       {
385       case ID_ButtonNew :
386         New(); 
387         FocusOnCurrentPage();
388         break;
389       case ID_ButtonOpen :
390         Open(); 
391         FocusOnCurrentPage();
392         break;
393       case ID_ButtonClose :
394         CloseCurrentPage();
395         break;
396       case ID_ButtonSave :
397         SaveCurrentPage();
398         break;
399       case ID_ButtonSaveAs :
400         if (mwxNotebook->GetPageCount()==0) break;  
401         GetCurrentPage()->SetAskFilename(true);
402         SaveCurrentPage();
403         break;
404       case ID_ButtonRun :
405         if ((mUser!=0) && (mwxNotebook->GetPageCount()>0)) 
406           mUser->WxGUITextEditorRun();
407         FocusOnCurrentPage();
408         break;
409       }
410   }
411   //================================================================  
412
413   //================================================================  
414   void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
415   {
416   }
417   //================================================================  
418
419   //================================================================  
420   void WxGUITextEditor::New()
421   {
422     NewPage("");
423     UpdateInfo();
424   }
425   //================================================================
426
427   //================================================================  
428   void WxGUITextEditor::Open()
429   {
430     //    std::cout << "-------------- OPEN ---------------"<<std::endl;
431
432     wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
433                                         _T(""),std2wx(mFileNameFilter),
434                                         wxOPEN | wxFILE_MUST_EXIST );
435     fd->ShowModal();
436     
437     if (fd->GetReturnCode()==wxID_OK)
438       {
439         std::string filename = wx2std(fd->GetPath());
440         std::cout << "file [" << filename << "]" <<std::endl;
441         Open(filename);
442       }
443     else 
444       {
445         //      std::cout << "-------------- CANCELLED ---------------"<<std::endl;
446       }
447   }
448   //================================================================  
449   
450   //================================================================  
451   void WxGUITextEditor::Open(const std::string& filename)
452   {
453     NewPage(filename);
454     UpdateInfo();
455   }
456   //================================================================  
457
458
459   //================================================================  
460   bool WxGUITextEditor::CloseCurrentPage()
461   {
462     if (mwxNotebook->GetPageCount()==0) return true;
463
464     if (GetCurrentPage()->IsModified()) 
465       {
466         wxString mess = std2wx(GetCurrentPage()->GetPageName());
467         mess += _T(" modified. Save it ?");
468         wxMessageDialog* d = 
469           new wxMessageDialog(this,
470                               mess, 
471                               _T("Save buffer"), 
472                               wxYES_NO | wxCANCEL | wxICON_QUESTION);
473         switch (d->ShowModal())
474           {
475           case wxID_CANCEL : 
476             return false;
477             break;
478           case wxID_YES : 
479             GetCurrentPage()->Save(mFileNameFilter); 
480             break;
481           case wxID_NO : ;
482           }       
483       } 
484     mwxNotebook->DeletePage(mwxNotebook->GetSelection());
485     FocusOnCurrentPage();
486     return false;
487   }
488   //================================================================  
489
490   //================================================================  
491   bool WxGUITextEditor::CloseAllPages()
492   {
493     bool ok = true;
494     while (mwxNotebook->GetPageCount()!=0)
495       {
496         if (!CloseCurrentPage()) 
497           {
498             ok = false;
499             break;
500           }
501       }
502     return ok;
503   }
504   //================================================================  
505
506   //================================================================  
507   void WxGUITextEditor::SaveCurrentPage()
508   {
509     if (mwxNotebook->GetPageCount()==0) return;  
510     GetCurrentPage()->Save(mFileNameFilter);
511     mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
512                              std2wx(GetCurrentPage()->GetPageName()));
513   }
514   //================================================================  
515
516
517   //================================================================  
518   void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
519   {
520     if (!CloseCurrentPage()) evt.Veto();
521   }
522   //================================================================
523
524
525
526   /*
527   //================================================================  
528   void WxGUITextEditor::OnButtonQuit(wxCommandEvent& event) 
529   { 
530     Quit(); 
531     FocusOnCurrentPage();
532   }
533   void WxGUITextEditor::Quit()
534   {
535     std::cout << "-------------- QUIT ---------------"<<std::endl;
536     if (AskSave()) GetParent()->Close();
537   }
538   //================================================================  
539   */
540
541   //================================================================  
542
543   /*
544   void WxGUITextEditor::Run()
545   {
546     std::cout << "-------------- RUN ---------------"<<std::endl;
547
548     if (!mwxSplit->IsSplit()) 
549       {
550         int size = -100;
551         int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
552         if (size<minsize) size = minsize;
553         mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
554       }
555
556     std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
557     mInterpreter->InterpretLine("reset");
558     try 
559       {
560         mInterpreter->InterpretBuffer(buffer);
561       }
562     catch (...)
563       {
564       }
565   } 
566   //================================================================  
567 */
568   
569  
570   
571   //================================================================
572   
573
574   //================================================================
575   void WxGUITextEditor::UpdateInfo()
576   {
577     if (mwxNotebook->GetPageCount()==0) return;
578     WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
579     
580     long line, column, pos;
581     pos = text->GetInsertionPoint();
582     text->PositionToXY(pos, &column, &line);
583     
584     
585     //                wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
586     
587     
588     char mess[200];
589     sprintf(mess,"%ld:%ld/%ld:%ld",
590             line+1,column+1,
591             (long)text->GetNumberOfLines(),
592             (long)text->GetLineLength(line)+1);
593     //       pos+1,
594     //       (long)mwxInputText->GetValue().length())+1;
595     
596    // mwxPosition->SetLabel(wxString(mess));
597     mwxPosition->SetLabel(std2wx(mess));    
598     mwxPosition->Show();
599     
600     if (text->IsModified()) 
601       {
602         std::string title("*");
603         title += GetCurrentPage()->GetPageName();
604         mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
605       }
606   }
607   //================================================================
608   //================================================================
609   void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
610   {
611     //  std::cout << "U" << std::endl;
612     UpdateInfo();
613   }
614   //================================================================
615
616   //================================================================
617   void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
618   {
619     //    std::cout << "D" << std::endl;
620     // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
621     if ( event.ControlDown() )
622       {
623         switch (event.GetKeyCode())
624           {
625           case 'n': case 'N' : New(); break;
626           case 's': case 'S' : SaveCurrentPage(); break;
627           case 'o': case 'O' : Open(); break;
628             //    case 'r': case 'R' : Run(); break;
629             //    case 'q': case 'Q' : Quit(); break;
630           }
631       }
632   }
633   //================================================================
634   
635   //================================================================  
636   BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
637     EVT_MENU(wxID_ANY,  WxGUITextEditor::OnToolLeftClick)
638     EVT_TOOL_RCLICKED(wxID_ANY,  WxGUITextEditor::OnToolRightClick)
639     EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
640     END_EVENT_TABLE()
641   //================================================================
642     
643
644
645
646   
647   //================================================================
648   WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent, 
649                                                     wxString title, 
650                                                     wxSize size)
651     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
652   {     
653     
654     
655     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
656
657     mEditor = new WxGUITextEditor(this);
658     sizer->Add(mEditor,1,wxGROW);
659     
660     //    WxGUICommand* com = new WxGUICommand(this,this);
661     //    sizer->Add(com);
662     
663     SetSizer(sizer);
664
665     // Creates the parent window of all bbtk windows as a child of this
666     Wx::CreateTopWindow(this);
667     // Add the method OnWxSignal as a Wx::Signal observer 
668     //bbtkAddWxSignalObserver(WxGUITextEditorWindow::OnWxSignal);
669     
670    
671     SetAutoLayout(true);
672     Layout();
673   }
674   //================================================================
675
676   //================================================================
677   WxGUITextEditorWindow::~WxGUITextEditorWindow()
678   {
679   }
680   //================================================================
681
682
683 } // namespace bbtk
684
685
686 #endif //_USE_WXWIDGETS_