]> Creatis software - bbtk.git/blob - kernel/src/bbtkWtBlackBox.cxx
be524097ae1bc18de78a24626346eb011adbdf4d
[bbtk.git] / kernel / src / bbtkWtBlackBox.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkWxBlackBox.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.45 $
34 =========================================================================*/
35
36
37
38 #ifdef _USE_WT_
39
40 /**
41  *  \file 
42  *  \brief 
43  */
44  
45 #include "bbtkWtBlackBox.h"
46 #include "bbtkBlackBoxOutputConnector.h"
47
48
49 //#include "bbtkWxContainerBlackBox.h"
50 //#include <wx/dialog.h>
51
52 //#include "bbtkData.h"
53 //#include "bbtkFactory.h"
54
55
56
57
58 namespace bbtk
59 {
60
61  
62 /*      
63         class jScript
64         {
65                 public:
66                         jScript();
67                         std::string jScriptLine;
68         };
69 */
70   //=========================================================================
71   // wtData structure
72   //=========================================================================
73
74         // For the very first contruction of the application
75 /*      struct wtServerData
76         {
77           std::string docRoot;
78           std::string port;
79           std::string address;
80         
81         };
82         */
83         struct wtData
84         {
85                 Wt::WContainerWidget *parent;
86                 WtBlackBox::Pointer b;
87                 Wt::WString title;              
88         };
89
90 /*      jScript::jScript()
91         {
92                 jScriptLine = "";
93         }
94 */
95 //      static bbtk::jScript* js  = new bbtk::jScript();
96         static wtData myWtData;
97 //      static wtServerData loadedJS;
98         //static std::string* jss;
99         
100         
101
102   //=========================================================================
103   // javaScript Line
104   //=========================================================================
105         //Contains all the JS statements needed for the bbwt.
106         
107         
108   //=========================================================================
109   // WxFrame
110   //=========================================================================
111         // Application WT deployed
112   //==================================================================
113  
114
115  
116  class WtWFrame : public Wt::WApplication
117   {
118   public:
119     WtWFrame(
120                         const Wt::WEnvironment& env                     
121             );
122     ~WtWFrame();
123                 
124         
125         
126         WtBlackBox::WeakPointer mBox; 
127         
128
129   };    
130         
131
132         WtWFrame::WtWFrame(
133                         const Wt::WEnvironment& env
134                    )
135     : 
136     Wt::WApplication(env)
137   {  
138                 //std::cout<<"Adding JavaScript --  DELETE ME -- bbtkWtBlackBox.cxx"<<std::endl;
139                 //this->require("/home/gonzalez/Documents/CREATOOLS/wt_library/wt/bbtk_wt_PKG/src/js/xtk.js");
140
141                 //this->require("http://get.goXTK.com/xtk.js");
142                 //this->useStyleSheet("css/demo.css");
143                 
144         //      this->require("http://get.goXTK.com/xtk_edge.js");
145         this->require("http://get.goXTK.com/xtk_xdat.gui.js");
146         this->require("xtk.js");
147         //this->require("xtk_xdat.gui.js");
148   this->useStyleSheet("style.css");
149   
150
151         //      std::cout<<"Cargado------------           "<<loadedJS<<std::endl;
152
153                 mBox = myWtData.b;
154                 if(!myWtData.parent)
155                         {
156                                 std::cout<<"        SIN PADRE "<<std::endl;
157                                 if(myWtData.parent == 0){std::cout<<"        PARENT 0 "<<std::endl;}
158                                 if(myWtData.parent == NULL){std::cout<<"        PARENT NULL "<<std::endl;}
159                                 myWtData.parent = new Wt::WContainerWidget();
160                                 std::cout<<"Parent created --  DELETE ME -- bbtkWtBlackBox.cxx"<<std::endl;
161                         }
162
163                 Wt::WContainerWidget *myCont = new Wt::WContainerWidget(myWtData.parent);
164                 myCont->setMinimumSize(800,500);
165                 std::cout<<"Creando WebWIDGET DELETE ME ---- bbtkWtBlackBox.cxx.................50%"<<std::endl;
166                 myWtData.b->bbUserCreateWidget(myCont);
167                 root()->addWidget(myWtData.parent);
168                 //bbmWindow = myCont;
169                 this->refresh();
170                 //jss = WtBlackBox::jScript;
171                 std::cout<<"Creando WebWIDGET DELETE ME ---- bbtkWtBlackBox.cxx.................80%"<<std::endl;
172                 std::cout<<"Creando WebWIDGET DELETE ME SCRIPT : ---"<<bbtk::jScript<<".................80%"<<std::endl;
173                 //this->doJavaScript(bbtk::jScript); 
174
175
176   }
177   //==================================================================
178   //==================================================================
179   WtWFrame::~WtWFrame() 
180     { 
181       if (mBox.lock())
182         bbtkDebugMessage("widget",9,"["<<mBox.lock()->bbGetName()
183                          <<"] $$$$$$$$$$$$$$$$$$$ ~W()"
184                          <<std::endl);
185       else 
186         bbtkDebugMessage("widget",9,"[DEAD] $$$$$$$$$$$$$$$$$$$$$ ~WtContainer()"<<std::endl);
187       bbtkDebugMessage("widget",9,"WtWFrame::~WtWFrame()"<<std::endl);
188       if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
189       WWt::DecNbWindowsAlive();
190     }
191
192
193  
194    
195         Wt::WApplication *createApplication(const Wt::WEnvironment& env)
196         {
197                         return new WtWFrame(env);                       
198         }
199
200
201   //=========================================================================
202   // WxDialogWxBlackBox::Pointer box
203   //=========================================================================
204
205    //==================================================================
206   // Dialog window which is modal
207  /* class BBTK_EXPORT  WxDialog : public wxDialog
208   {
209   public:
210     WxDialog(WxBlackBox::Pointer box, 
211                      wxWindow *parent, 
212                      wxString title, 
213                      wxSize size);
214     ~WxDialog();Wt::GetTopWindow()
215     void OnCloseWindow(wxCloseEvent& event);
216
217
218     WxBlackBox::WeakPointer mBox;      
219     DECLARE_EVENT_TABLE();
220     
221   };
222   
223   BEGIN_EVENT_TABLE(WxDialog, wxDialog)
224     EVT_CLOSE(WxDialog::OnCloseWindow)
225     END_EVENT_TABLE();
226   //=========================================================================
227
228   //=========================================================================
229   WxDialog::WxDialog(WxBlackBox::Pointer b,
230                      wxWindow *parent,
231                      wxString title,
232                      wxSize size)
233     :
234     wxDialog( parent, 
235                 -1, 
236                 title,
237                 wxDefaultPosition,
238                 size,
239                 wxRESIZE_BORDER | 
240                 wxSYSTEM_MENU  |
241                 wxCLOSE_BOX |
242                 wxMAXIMIZE_BOX | 
243                 wxMINIMIZE_BOX | 
244                 wxCAPTION  
245               ),
246     mBox(b)
247   {
248     bbtkDebugMessage("widget",9,"WxDialog::WxDialog ["<<b->bbGetName()<<"]"
249                      <<std::endl);WxBlackBox::Pointer box
250     Wx::IncNbWindowsAlive();
251     b->bbSetWindow(this);
252     // Insert the widget into the window
253     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
254     b->bbUserCreateWidget(this);
255     wxWindow* widget = b->bbGetOutputWidget();
256     sizer->Add( widget, 1, wxALL|wxGROW, 2);
257     SetSizer(sizer);
258     Layout();
259   }
260   //==================================================================
261   //===========================organise=======================================
262   WxDialog::~WxDialog() 
263     { 
264       bbtkDebugMessage("widget",9,"WxDialog::~WxDialog()"<<std::endl);
265       //bbtkDebugMessage("widget",9,"WxFrame::~WxFrame()"<<std::endl);
266       //      if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
267       //      Wx::DecNbWindowsAlive();
268     }
269    //==================================================================
270    //==================================================================
271   void WxDialog::OnCloseWindow(wxCloseEvent& event)
272     {
273       bbtkDebugMessage("widget",9,"WxDialog::OnCloseWindow()"<<wxGUIEditorGraphicBBS:std::endl);
274       if (mBox.lock()) mBox.lock()->bbSetWindow(0); http://doodle.com/qx65tfxismf7d6ku5vxykghs/admin#table
275       Wx::DecNbWindowsAlive();
276       this->Destroy();
277     }
278    //==================================================================
279
280 */
281  //=========================================================================
282   // WxBlackBox
283   //=========================================================================
284
285   //=========================================================================
286   //=========================================================================
287   //=========================================================================
288   //=========================================================================
289   BBTK_BLACK_BOX_IMPLEMENTATION(WtBlackBox,WidgetBlackBox<Wt::WContainerWidget>);
290         
291         //std::string WtBlackBox::wtServerData::docRoot = "/home/gonzalez/Documents/pruebaResources";
292         //std::string WtBlackBox::wtServerData::docRoot = "/usr/local/share/wtResources";
293   //std::string WtBlackBox::wtServerData::port    = "8081";
294
295         std::string WtBlackBox::wtServerData::docRoot = "<void>";
296         std::string WtBlackBox::wtServerData::port    = "<void>";
297   std::string WtBlackBox::wtServerData::address = "0.0.0.0";
298         
299   //=========================================================================
300   //=========================================================================
301   void WtBlackBox::bbUserSetDefaultValues()
302   {
303     bbtkBlackBoxDebugMessage("widget",5,"WtBlackBox::bbUserSetDefaultValues()"<<std::endl);
304     bbmWindow = 0;
305   /*  WtBlackBox::serData.docRoot = "/home/gonzalez/Documents/pruebaResources";
306     WtBlackBox::serData.address = "0.0.0.0";
307     WtBlackBox::serData.port    = "8080";*/
308     
309
310   }
311   //=========================================================================
312
313   //=========================================================================
314   void WtBlackBox::bbUserInitializeProcessing()
315   {
316
317   }
318   //================================================================http://intranet-if.insa-lyon.fr/temps/5IF39.html==    
319
320   //=========================================================================
321   void WtBlackBox::bbUserFinalizeProcessing()
322   {
323     bbtkBlackBoxDebugMessage("widget",5,"==> WtBlackBox::bbUserFinalizeProcessing()"<<std::endl);
324     bbDestroyWindow();
325     bbtkBlackBoxDebugMessage("widget",5,"<== WtBlackBox::bbUserFinalizeProcessing()"<<std::endl);
326   }
327   //==================================================================    
328
329   //==================================================================    
330   /// Callback for creating a Dialog window (modal)
331   void WtBlackBox::bbCreateDialogWindow()
332   {
333           bbtkBlackBoxDebugMessage("widget",3,
334                      "==> WtBlackBox::bbCreateDialogWindow() - No Dialog must be created, it is a web app"
335                      <<std::endl);
336
337   } 
338   //=========================================================================
339
340   //==================================================================    
341
342   //==================================================================    
343   /// Callback for creating a Frame window 
344         void WtBlackBox::bbCreateFrameWindow()
345         {
346
347                 bbtkBlackBoxDebugMessage("widget",3,
348                         "==> WtBlackBox::bbCreateFrameWindow() - parent = "
349                         <<WWt::GetTopWindow()
350                         <<std::endl);
351
352                 //Setting necessary data for Wt server deployment
353                 myWtData.parent = WWt::GetTopWindow();
354                 myWtData.b      = GetThisPointer<WtBlackBox>();
355                 myWtData.title  = std2wt( bbGetInputWinTitle()  
356                              + " - bbtk (c) CREATIS");
357
358
359
360     
361
362                 
363                 //char  *argv[7] =  {"./WtTest","--docroot","/home/gonzalez/Documents/pruebaResources","--http-address","0.0.0.0","--http-port","8080"};
364                 //int argc = 7;
365                 
366                 
367                         char  *argv[7] =  {"./WtTest","--docroot",(char*)WtBlackBox::wtServerData::docRoot.c_str(),"--http-address",(char*)WtBlackBox::wtServerData::address.c_str(),"--http-port",(char*)WtBlackBox::wtServerData::port.c_str()};
368                 int argc = 7;
369                 
370                 std::cout<<" ---  PUERTO  :"<<WtBlackBox::wtServerData::port<<std::endl;
371                 
372         /*      if(Wt::WServer::instance()->isRunning())
373                 {
374                         std::cout<<"Checking the existence of a Server deployed  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
375                         Wt::WServer::instance()->stop();
376                 }
377         */
378                 std::cout<<"Deploying Server -- Definition of server parameters by default  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
379                 Wt::WRun(argc,argv,&createApplication); 
380         }
381
382   //==================================================================    
383
384   //==================================================================    
385   void WtBlackBox::bbSetWindow(Wt::WContainerWidget* w) 
386   {
387     bbtkBlackBoxDebugMessage("widget",9,"==> WtBlackBox::bbSetWindow("<<w<<")"<<std::endl);
388     if ((bbmWindow != 0)&&(w!=0)) 
389       { 
390         std::cout << "ERRRRRRRROOOOR"<<std::endl; 
391       }
392     bbmWindow = w;
393   }
394
395         
396
397     //==================================================================    
398
399   //==================================================================
400   void WtBlackBox::bbShowWindow()
401   {
402     if ((bbmWindow) && (!bbIsShown()))
403       {
404         bbtkBlackBoxDebugMessage("widget",3,"==> WtBlackBox::bbShowWindow()"
405                          <<std::endl);
406         if (bbGetInputWinDialog()) 
407           {
408           //  ((WxDialog*)bbmWindow)->ShowModal();
409           }
410         else
411           {
412                 // There is not such a thing like show becouse it is a webapp
413            // bbGetWindow()->Show();
414           //  WWt::CreateWtAppIfNeeded();
415             bbSetShown(true);
416           } 
417       }
418   }
419   //==================================================================    
420
421   //==================================================================    
422   void WtBlackBox::bbDestroyWindow()
423   {
424     bbtkBlackBoxDebugMessage("widget",3,"==> WtBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
425     if (bbGetWindow()!=NULL) 
426       {
427         //       wxWindow* w = bbGetWindow();
428         //bbSetWindow(0);
429         // WE SHOULD DESTROY THE WINDOW WITH THE Close METHOD
430         // HOWEVER I
431         //w->Close(true);
432         //
433         delete bbGetWindow();
434         bbSetShown(false);
435       }
436     bbtkBlackBoxDebugMessage("widget",3,"<== WtBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
437
438   }
439   //==================================================================    
440
441 }//namespace bbtk
442
443 #endif
444
445