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