]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxEditor.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxEditor.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxEditor.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/20 11:04:57 $
7   Version:   $Revision: 1.7 $
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 "bbtkWxEditor.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
39 #include "bbtkWxStreamRedirector.h"
40
41
42     #include "icons/cc_new.xpm"
43     #include "icons/cc_open.xpm"
44     #include "icons/cc_save.xpm"
45     #include "icons/cc_run.xpm"
46     #include "icons/cc_exit.xpm"
47 //    #include "icons/copy.xpm"
48 //    #include "icons/cut.xpm"
49 //    #include "icons/preview.xpm"  // paste XPM
50 //    #include "icons/print.xpm"
51 //    #include "icons/help.xpm"
52
53
54
55 namespace bbtk
56 {
57
58
59   //================================================================
60   class WxTextCtrlGettingKeyEvents : public wxTextCtrl
61   {
62   public:
63     WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
64                 const wxPoint &pos, const wxSize &size, int style = 0)
65       : wxTextCtrl(parent, id, value, pos, size, style)
66     {
67     }
68     
69     void SetWxEditor(WxEditor* e) { mWxEditor = e; }
70     
71     void OnKeyDown(wxKeyEvent& event);
72     void OnKeyUp(wxKeyEvent& event);
73     void OnChar(wxKeyEvent& event);
74   private :
75     WxEditor* mWxEditor;
76     
77     DECLARE_EVENT_TABLE()
78       };
79   
80   BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
81     EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
82     EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
83     EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
84     
85     END_EVENT_TABLE()
86     
87     
88     void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
89   {
90     event.Skip();
91   }
92   
93   void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
94   {
95     mWxEditor->OnKeyUp(event);
96     event.Skip();
97   }
98   
99   void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
100   {
101     mWxEditor->OnKeyDown(event);
102     event.Skip();
103   }
104   //================================================================
105   
106
107
108
109   //================================================================
110   WxEditor::WxEditor( wxWindow *parent )
111     : wxPanel(parent, -1)
112   {     
113     mInterpreter = new bbtk::Interpreter();
114     //mInterpreter->SetWxEditor(this);
115     mInterpreter->SetCommandLine(true);
116
117     //==============
118     // Menu
119     wxInitAllImageHandlers();
120     
121     /*
122     wxMenu *menuFile = new wxMenu;
123     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
124     
125     wxMenu *menuAbout = new wxMenu;
126     menuAbout->Append( ID_Menu_About, _T("&About...") );
127
128     wxMenu *menuTools = new wxMenu;
129     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
130     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
131     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
132     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
133     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
134     
135     
136     wxMenuBar *menuBar = new wxMenuBar;
137     menuBar->Append( menuFile, _T("&File") );
138     menuBar->Append( menuTools, _T("&Tools") );
139     menuBar->Append( menuAbout, _T("About") );
140     
141     SetMenuBar( menuBar );
142     
143     CreateStatusBar();
144     SetStatusText( _T("Welcome to bbi !") );
145     */
146
147     //    wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
148     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
149     mwxSplit = new wxSplitterWindow(this,
150                                                    -1,
151                                                    wxDefaultPosition,
152                                                    wxDefaultSize,
153                                                    //wxSize(400,200),
154                                                    wxSP_3D |
155                                                    wxSP_LIVE_UPDATE );
156     mwxInputText = 
157       new WxTextCtrlGettingKeyEvents(mwxSplit,
158                      ID_InputText,
159                      _T(""),
160                      wxDefaultPosition,
161                      wxDefaultSize,
162                      wxTE_MULTILINE 
163                      //    |wxTE_PROCESS_ENTER
164                      | wxTE_PROCESS_TAB 
165                      //             | wxWANTS_CHARS 
166                      |  wxTAB_TRAVERSAL
167                      );
168     mwxInputText->SetWxEditor(this);
169
170     wxFont* FixedFont = new wxFont(10,
171                                    wxFONTFAMILY_MODERN,
172                                    wxFONTSTYLE_NORMAL,
173                                    wxFONTWEIGHT_NORMAL,
174                                    false);
175
176    mwxInputTextAttr = new wxTextAttr;
177    mwxInputTextAttr->SetFont(*FixedFont);
178
179    //   sizer->AddGrowableCol(0);
180    //   sizer->Add(mwxInputText,1,wxGROW);
181
182    mwxOutputText = 
183       new WxTextCtrlGettingKeyEvents(mwxSplit,
184                      ID_OutputText,
185                      _T(""),wxDefaultPosition,
186                      wxDefaultSize, //HistorySize,
187                      wxTE_READONLY |
188                      wxTE_MULTILINE );
189
190    mwxOutputText->SetWxEditor(this);
191
192    mwxOutputTextAttr = new wxTextAttr;
193    mwxOutputTextAttr->SetFont(*FixedFont);
194    mwxOutputText->Hide();
195
196    //   sizer->Add(mwxOutputText,1,wxGROW);
197   sizer->Add(mwxSplit,1,wxGROW);
198   mwxSplit->Initialize(mwxInputText);
199
200    // BUTTONS
201    wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
202    wxBoxSizer *btnsSizer  = new wxBoxSizer(wxHORIZONTAL);
203
204    wxBitmap bmp_new(cc_new_xpm);
205    mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New")  );
206    btnsSizer->Add( mwxButtonNew );
207    wxBitmap bmp_open(cc_open_xpm);
208    mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open")  );
209    btnsSizer->Add( mwxButtonOpen );
210    wxBitmap bmp_save(cc_save_xpm);
211    mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save")  );
212    btnsSizer->Add( mwxButtonSave );
213    wxBitmap bmp_run(cc_run_xpm);
214    mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run")  );
215    btnsSizer->Add( mwxButtonRun );
216    wxBitmap bmp_quit(cc_exit_xpm);
217    mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit")  );
218    btnsSizer->Add( mwxButtonQuit );
219    
220  
221    mwxPosition = new wxStaticText ( btnsCtrlPanel, -1, _T(""));
222    btnsSizer->Add( mwxPosition );
223   
224
225    btnsCtrlPanel->SetSizer(btnsSizer);
226    sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM  //| wxGROW
227                 , 10 );
228    
229    // Redirection of std::cout and std::cerr to mwxOutputText and printf
230    mRedirect_cout = 
231      new WxStreamRedirector(std::cout,mwxOutputText,*wxBLACK,true);
232    mRedirect_cerr = 
233      new WxStreamRedirector(std::cerr,mwxOutputText,*wxGREEN,true); 
234    
235    UpdatePosition();
236    mwxInputText->SetFocus();
237    
238    SetSizer(sizer);
239    SetAutoLayout(true);
240    Layout();
241   }
242   //================================================================
243   
244   //================================================================
245   WxEditor::~WxEditor()
246   {
247     delete mInterpreter;
248     delete mRedirect_cout;
249     delete mRedirect_cerr;
250   }
251   //================================================================
252
253
254   //================================================================  
255   void WxEditor::OnButtonOpen(wxCommandEvent& event) 
256   { 
257     Open(); 
258     mwxInputText->SetFocus();
259   } 
260   void WxEditor::Open()
261   {
262     std::cout << "-------------- OPEN ---------------"<<std::endl;
263     if (!AskSave()) {
264       std::cout << "-------------- CANCELLED ---------------"<<std::endl;
265       return;
266     }
267
268     wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
269                                         _T(""),_T("*.bbs"),
270                                         wxOPEN | wxFILE_MUST_EXIST );
271     fd->ShowModal();
272     
273     if (fd->GetReturnCode()==wxID_OK)
274       {
275         std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
276         mwxInputText->LoadFile(fd->GetPath());
277       }
278     else 
279       {
280         std::cout << "-------------- CANCELLED ---------------"<<std::endl;
281       }
282   }
283   //================================================================  
284   
285   //================================================================  
286   void WxEditor::Open(const std::string& filename)
287   {
288     mwxInputText->LoadFile(std2wx(filename));
289     mwxInputText->SetModified(false);
290     UpdatePosition();
291   }
292   //================================================================  
293
294   //================================================================  
295   void WxEditor::OnButtonSave(wxCommandEvent& event) 
296   { 
297     Save(); 
298     mwxInputText->SetFocus();
299   } 
300   void WxEditor::Save()
301   {
302     std::cout << "-------------- SAVE ---------------"<<std::endl;
303     wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
304                                         _T(""),_T("*.bbs"),
305                                         wxSAVE | wxOVERWRITE_PROMPT );
306     fd->ShowModal();
307     
308     if (fd->GetReturnCode()==wxID_OK)
309       {
310         std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
311         mwxInputText->SaveFile(fd->GetPath());
312         mwxInputText->SetModified(false);
313       }
314     else 
315       {
316         std::cout << "-------------- CANCELLED ---------------"<<std::endl;
317       }
318   }
319   //================================================================  
320
321   //================================================================  
322   void WxEditor::OnButtonQuit(wxCommandEvent& event) 
323   { 
324     Quit(); 
325     mwxInputText->SetFocus();
326   } 
327   void WxEditor::Quit()
328   {
329     std::cout << "-------------- QUIT ---------------"<<std::endl;
330     if (AskSave()) GetParent()->Close();
331   }
332   //================================================================  
333
334
335   //================================================================  
336   void WxEditor::OnButtonRun(wxCommandEvent& event) 
337   { 
338     Run(); 
339     mwxInputText->SetFocus();
340   }
341   void WxEditor::Run()
342   {
343     std::cout << "-------------- RUN ---------------"<<std::endl;
344
345     if (!mwxSplit->IsSplit()) 
346       {
347         int size = -100;
348         int minsize = - (int)(mwxInputText->GetSize().GetHeight() / 2.);
349         if (size<minsize) size = minsize;
350         mwxSplit->SplitHorizontally( mwxInputText, mwxOutputText, size);
351       }
352
353     std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
354     mInterpreter->InterpretLine("reset");
355     try 
356       {
357         mInterpreter->InterpretBuffer(buffer);
358       }
359     catch (...)
360       {
361       }
362   } 
363   //================================================================  
364   
365   //================================================================
366   bool WxEditor::AskSave()
367   {
368     if (!mwxInputText->IsModified()) return true;
369     wxMessageDialog* d = new wxMessageDialog(this,
370                                              _T("Buffer modified. Save it ?"), 
371                                              _T("Save buffer"), 
372                                              wxYES_NO | wxCANCEL | wxICON_QUESTION);
373     switch (d->ShowModal())
374       {
375       case wxID_YES : Save(); 
376       case wxID_NO : return true;
377       case wxID_CANCEL : return false;
378       }
379   }
380
381
382   //================================================================
383
384   //================================================================  
385   void WxEditor::OnButtonNew(wxCommandEvent& event) 
386   { 
387     New(); 
388     mwxInputText->SetFocus();
389   }
390   void WxEditor::New()
391   {
392     std::cout << "-------------- NEW ---------------" << std::endl;
393     AskSave();
394     mwxInputText->Clear();
395     UpdatePosition();
396   }
397   //================================================================
398
399   //================================================================
400   void WxEditor::UpdatePosition()
401   {
402
403     long line, column, pos;
404     pos = mwxInputText->GetInsertionPoint();
405     mwxInputText->PositionToXY(pos, &column, &line);
406     
407     
408      //                wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
409
410   
411      char mess[200];
412      sprintf(mess,"%ld:%ld/%ld:%ld",
413              line+1,column+1,
414              (long)mwxInputText->GetNumberOfLines(),
415              (long)mwxInputText->GetLineLength(line)+1);
416      //      pos+1,
417      //      (long)mwxInputText->GetValue().length())+1;
418
419      mwxPosition->SetLabel(std2wx(mess));
420      mwxPosition->Show();
421     
422   }
423   //================================================================
424   //================================================================
425   void WxEditor::OnKeyUp(wxKeyEvent& event)
426   {
427     //  std::cout << "U" << std::endl;
428     UpdatePosition();
429   }
430   //================================================================
431
432   //================================================================
433   void WxEditor::OnKeyDown(wxKeyEvent& event)
434   {
435     //    std::cout << "D" << std::endl;
436     // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
437     if ( event.ControlDown() )
438       {
439         switch (event.GetKeyCode())
440           {
441           case 'n': case 'N' : New(); break;
442           case 's': case 'S' : Save(); break;
443           case 'o': case 'O' : Open(); break;
444           case 'r': case 'R' : Run(); break;
445           case 'q': case 'Q' : Quit(); break;
446           }
447       }
448     /*
449     // Command completion  
450     std::vector<std::string> commands;
451     wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
452     int ind = sline.size();
453     mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
454     if (commands.size()==1) 
455       {
456         std::string com = *commands.begin();
457         for (; ind<com.size(); ++ind) 
458           {
459             mwxTextCommand->TextAppend( std2wx ( com[ind]) ); 
460           }
461          mwxTextCommand->TextAppend(_T(" "));
462       }
463     else if (commands.size()>1) 
464       {
465         std::vector<std::string>::iterator i;
466         write(1,"\n",1);
467         for (i=commands.begin();i!=commands.end();++i) 
468           {
469             write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
470             PrintChar(' ');
471           }
472         write(STDOUT_FILENO,"\n> ",3);
473         //for (int j=0;j<ind;++j) 
474         //{
475         write(STDOUT_FILENO,line,ind); 
476         //  }
477       }
478     */
479   }
480   //================================================================
481   
482   //================================================================  
483   BEGIN_EVENT_TABLE(WxEditor, wxPanel)
484   //    EVT_CHAR(WxEditor::OnKeyPress)
485     EVT_BUTTON(WxEditor::ID_ButtonNew, WxEditor::OnButtonNew)
486     EVT_BUTTON(WxEditor::ID_ButtonOpen, WxEditor::OnButtonOpen)
487     EVT_BUTTON(WxEditor::ID_ButtonSave, WxEditor::OnButtonSave)
488     EVT_BUTTON(WxEditor::ID_ButtonRun, WxEditor::OnButtonRun)
489     EVT_BUTTON(WxEditor::ID_ButtonQuit, WxEditor::OnButtonQuit)
490     END_EVENT_TABLE()
491   //================================================================
492     
493     void WxEditorWindow::WxGUICommandEnter(const std::string& s)
494   {
495     std::cout << "Command received : '"<<s<<"'"<<std::endl;
496   }
497   
498   //================================================================
499   WxEditorWindow::WxEditorWindow( wxWindow *parent, wxString title, wxSize size)
500     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
501   {     
502     
503     
504     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
505
506     mEditor = new WxEditor(this);
507     sizer->Add(mEditor,1,wxGROW);
508     
509     WxGUICommand* com = new WxGUICommand(this,this);
510     sizer->Add(com);
511     
512     SetSizer(sizer);
513
514     // Creates and sets the parent window of all bbtk windows
515     wxWindow* top = new wxPanel(this,-1);
516     top->Hide();
517     
518     Wx::SetTopWindow(top);
519    
520     SetAutoLayout(true);
521     Layout();
522   }
523
524   WxEditorWindow::~WxEditorWindow()
525   {
526   }
527 } // namespace bbtk
528
529
530 #endif //_USE_WXWIDGETS_