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