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