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