]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxEditor.cxx
Created a bbs editor : bbed
[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 12:51:26 $
7   Version:   $Revision: 1.1 $
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
147     mwxInputText = 
148       new WxTextCtrlGettingKeyEvents(this,
149                      ID_InputText,
150                      _T(""),
151                      wxDefaultPosition,
152                      wxDefaultSize,
153                      wxTE_MULTILINE 
154                      //    |wxTE_PROCESS_ENTER
155                      | wxTE_PROCESS_TAB 
156                      //             | wxWANTS_CHARS 
157                      |  wxTAB_TRAVERSAL
158                      );
159     mwxInputText->SetWxEditor(this);
160
161     wxFont* FixedFont = new wxFont(10,
162                                    wxFONTFAMILY_MODERN,
163                                    wxFONTSTYLE_NORMAL,
164                                    wxFONTWEIGHT_NORMAL,
165                                    false);
166
167    mwxInputTextAttr = new wxTextAttr;
168    mwxInputTextAttr->SetFont(*FixedFont);
169
170    //   sizer->AddGrowableCol(0);
171    sizer->Add(mwxInputText,1,wxGROW);
172
173    mwxInputText->SetFocus();
174
175    mwxOutputText = 
176       new WxTextCtrlGettingKeyEvents(this,
177                      ID_OutputText,
178                      _T(""),wxDefaultPosition,
179                      wxDefaultSize, //HistorySize,
180                      wxTE_READONLY |
181                      wxTE_MULTILINE );
182
183    mwxOutputText->SetWxEditor(this);
184
185    mwxOutputTextAttr = new wxTextAttr;
186    mwxOutputTextAttr->SetFont(*FixedFont);
187
188    sizer->Add(mwxOutputText,1,wxGROW);
189      
190    // BUTTONS
191    wxPanel *btnsCtrlPanel = new wxPanel(this,-1);
192    wxBoxSizer *btnsSizer  = new wxBoxSizer(wxHORIZONTAL);
193
194    wxBitmap bmp_new(cc_new_xpm);
195    mwxButtonNew = new wxBitmapButton( btnsCtrlPanel,ID_ButtonNew,bmp_new);//_T("New")  );
196    btnsSizer->Add( mwxButtonNew );
197    wxBitmap bmp_open(cc_open_xpm);
198    mwxButtonOpen = new wxBitmapButton( btnsCtrlPanel,ID_ButtonOpen,bmp_open);//,_T("Open")  );
199    btnsSizer->Add( mwxButtonOpen );
200    wxBitmap bmp_save(cc_save_xpm);
201    mwxButtonSave = new wxBitmapButton( btnsCtrlPanel,ID_ButtonSave,bmp_save);//_T("Save")  );
202    btnsSizer->Add( mwxButtonSave );
203    wxBitmap bmp_run(cc_run_xpm);
204    mwxButtonRun = new wxBitmapButton( btnsCtrlPanel,ID_ButtonRun,bmp_run);//_T("Run")  );
205    btnsSizer->Add( mwxButtonRun );
206    wxBitmap bmp_quit(cc_exit_xpm);
207    mwxButtonQuit = new wxBitmapButton( btnsCtrlPanel,ID_ButtonQuit,bmp_quit);//_T("Quit")  );
208    btnsSizer->Add( mwxButtonQuit );
209    
210    /*
211  toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
212 #else // !USE_XPM_BITMAPS
213     #define INIT_TOOL_BMP(bmp) \
214         toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
215 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
216
217     INIT_TOOL_BMP(new);
218     INIT_TOOL_BMP(open);
219     INIT_TOOL_BMP(save);
220     INIT_TOOL_BMP(copy);
221     INIT_TOOL_BMP(cut);
222     INIT_TOOL_BMP(paste);
223     INIT_TOOL_BMP(print);
224     INIT_TOOL_BMP(help);
225
226     int w = toolBarBitmaps[Tool_new].GetWidth(),
227         h = toolBarBitmaps[Tool_new].GetHeight();
228
229     if ( !m_smallToolbar )
230     {
231         w *= 2;
232         h *= 2;
233
234         for ( size_t n = Tool_new; n < WXSIZEOF(toolBarBitmaps); n++ )
235         {
236             toolBarBitmaps[n] =
237                 wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h));
238
239
240
241    */
242
243
244    btnsCtrlPanel->SetSizer(btnsSizer);
245    sizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM  //| wxGROW
246                 , 10 );
247
248    // Redirection of std::cout and std::cerr to mwxOutputText and printf
249     mRedirect_cout = 
250       new WxStreamRedirector(std::cout,mwxOutputText,*wxBLACK,true);
251     mRedirect_cerr = 
252       new WxStreamRedirector(std::cerr,mwxOutputText,*wxGREEN,true); 
253
254  
255    SetSizer(sizer);
256    
257    SetAutoLayout(true);
258    Layout();
259   }
260   //================================================================
261
262  //================================================================
263   WxEditor::~WxEditor()
264   {
265     delete mInterpreter;
266     delete mRedirect_cout;
267     delete mRedirect_cerr;
268   }
269   //================================================================
270
271
272   //================================================================  
273   void WxEditor::OnButtonOpen(wxCommandEvent& event) { Open(); } 
274   void WxEditor::Open()
275   {
276     std::cout << "-------------- OPEN ---------------"<<std::endl;
277     if (!AskSave()) {
278       std::cout << "-------------- CANCELLED ---------------"<<std::endl;
279       return;
280     }
281
282     wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
283                                         _T(""),_T("*.bbs"),
284                                         wxOPEN | wxFILE_MUST_EXIST );
285     fd->ShowModal();
286     
287     if (fd->GetReturnCode()==wxID_OK)
288       {
289         std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
290         mwxInputText->LoadFile(fd->GetPath());
291       }
292     else 
293       {
294         std::cout << "-------------- CANCELLED ---------------"<<std::endl;
295       }
296   }
297   //================================================================  
298   
299   //================================================================  
300   void WxEditor::Open(const std::string& filename)
301   {
302     mwxInputText->LoadFile(std2wx(filename));
303     mwxInputText->SetModified(false);
304   }
305   //================================================================  
306
307   //================================================================  
308   void WxEditor::OnButtonSave(wxCommandEvent& event) { Save(); } 
309   void WxEditor::Save()
310   {
311     std::cout << "-------------- SAVE ---------------"<<std::endl;
312     wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
313                                         _T(""),_T("*.bbs"),
314                                         wxSAVE | wxOVERWRITE_PROMPT );
315     fd->ShowModal();
316     
317     if (fd->GetReturnCode()==wxID_OK)
318       {
319         std::cout << "file [" << wx2std(fd->GetPath()) << "]" <<std::endl;
320         mwxInputText->SaveFile(fd->GetPath());
321         mwxInputText->SetModified(false);
322       }
323     else 
324       {
325         std::cout << "-------------- CANCELLED ---------------"<<std::endl;
326       }
327   }
328   //================================================================  
329
330   //================================================================  
331   void WxEditor::OnButtonQuit(wxCommandEvent& event) { Quit(); } 
332   void WxEditor::Quit()
333   {
334     std::cout << "-------------- QUIT ---------------"<<std::endl;
335     if (AskSave()) GetParent()->Close();
336   }
337   //================================================================  
338
339
340   //================================================================  
341   void WxEditor::OnButtonRun(wxCommandEvent& event) { Run(); }
342   void WxEditor::Run()
343   {
344     std::cout << "-------------- RUN ---------------"<<std::endl;
345     std::stringstream* buffer = new std::stringstream(bbtk::wx2std(mwxInputText->GetValue()));
346     bool com;
347     mInterpreter->InterpretLine("reset",com);
348     try 
349       {
350         mInterpreter->InterpretBuffer(buffer);
351       }
352     catch (...)
353       {
354       }
355   } 
356   //================================================================  
357   
358   //================================================================
359   bool WxEditor::AskSave()
360   {
361     if (!mwxInputText->IsModified()) return true;
362     wxMessageDialog* d = new wxMessageDialog(this,
363                                              _T("Buffer modified. Save it ?"), 
364                                              _T("Save buffer"), 
365                                              wxYES_NO | wxCANCEL | wxICON_QUESTION);
366     switch (d->ShowModal())
367       {
368       case wxID_YES : Save(); 
369       case wxID_NO : return true;
370       case wxID_CANCEL : return false;
371       }
372   }
373
374
375   //================================================================
376
377   //================================================================  
378   void WxEditor::OnButtonNew(wxCommandEvent& event) { New(); }
379   void WxEditor::New()
380   {
381     std::cout << "-------------- NEW ---------------" << std::endl;
382     AskSave();
383     mwxInputText->Clear();
384   }
385   //================================================================
386
387   //================================================================
388   void WxEditor::OnKeyPress(wxKeyEvent& event)
389   {
390     // std::cout << "Key="<<event.GetKeyCode()<<std::endl;
391     if ( event.ControlDown() )
392       {
393         switch (event.GetKeyCode())
394           {
395           case 'n': case 'N' : New(); break;
396           case 's': case 'S' : Save(); break;
397           case 'o': case 'O' : Open(); break;
398           case 'r': case 'R' : Run(); break;
399           case 'q': case 'Q' : Quit(); break;
400           }
401       }
402     /*
403     // Command completion  
404     std::vector<std::string> commands;
405     wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
406     int ind = sline.size();
407     mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
408     if (commands.size()==1) 
409       {
410         std::string com = *commands.begin();
411         for (; ind<com.size(); ++ind) 
412           {
413             mwxTextCommand->TextAppend( std2wx ( com[ind]) ); 
414           }
415          mwxTextCommand->TextAppend(_T(" "));
416       }
417     else if (commands.size()>1) 
418       {
419         std::vector<std::string>::iterator i;
420         write(1,"\n",1);
421         for (i=commands.begin();i!=commands.end();++i) 
422           {
423             write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
424             PrintChar(' ');
425           }
426         write(STDOUT_FILENO,"\n> ",3);
427         //for (int j=0;j<ind;++j) 
428         //{
429         write(STDOUT_FILENO,line,ind); 
430         //  }
431       }
432     */
433   }
434   //================================================================
435   
436   //================================================================  
437   BEGIN_EVENT_TABLE(WxEditor, wxPanel)
438   //    EVT_CHAR(WxEditor::OnKeyPress)
439     EVT_BUTTON(WxEditor::ID_ButtonNew, WxEditor::OnButtonNew)
440     EVT_BUTTON(WxEditor::ID_ButtonOpen, WxEditor::OnButtonOpen)
441     EVT_BUTTON(WxEditor::ID_ButtonSave, WxEditor::OnButtonSave)
442     EVT_BUTTON(WxEditor::ID_ButtonRun, WxEditor::OnButtonRun)
443     EVT_BUTTON(WxEditor::ID_ButtonQuit, WxEditor::OnButtonQuit)
444     END_EVENT_TABLE()
445   //================================================================
446
447
448   //================================================================
449   WxEditorWindow::WxEditorWindow( wxWindow *parent, wxString title, wxSize size)
450     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
451   {     
452     WxEditor* mEditor = new WxEditor(this);
453  
454     //wxFlexGridSizer *sizer= new wxFlexGridSizer(2);
455     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
456     //sizer->AddGrowableCol(0);
457     sizer->Add(mEditor,1,wxGROW);
458     SetSizer(sizer);
459     // Creates and sets the parent window of all bbtk windows
460     wxWindow* top = new wxPanel(this,-1);//,_T("top"));
461     top->Hide();
462     //new wxFrame(this,-1,_T("bbtk"),
463     //                         wxDefaultPosition,
464     //                         wxSize(0,0),
465     //                         wxFRAME_TOOL_WINDOW) ;//wxMINIMIZE_BOX);
466     
467     Wx::SetTopWindow(top);
468    
469     
470     SetAutoLayout(true);
471     Layout();
472   }
473
474   WxEditorWindow::~WxEditorWindow()
475   {
476   }
477 } // namespace bbtk
478
479
480 #endif //_USE_WXWIDGETS_