]> Creatis software - bbtk.git/blob - kernel/src/bbtkWtBlackBox.cxx
#2536 BBTK Feature New Normal wt-version Package
[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("xtk_xdat.gui.js");
148
149         //this->require("http://get.goXTK.com/xtk.js");
150         this->require("xtk.js");
151
152   this->useStyleSheet("styleWT.css"); 
153   
154
155         //      std::cout<<"Cargado------------           "<<loadedJS<<std::endl;
156
157                 mBox = myWtData.b;
158                 if(!myWtData.parent)
159                         {
160                                 std::cout<<"        SIN PADRE "<<std::endl;
161                                 if(myWtData.parent == 0){std::cout<<"        PARENT 0 "<<std::endl;}
162                                 if(myWtData.parent == NULL){std::cout<<"        PARENT NULL "<<std::endl;}
163                                 myWtData.parent = new Wt::WContainerWidget();
164                                 std::cout<<"Parent created --  DELETE ME -- bbtkWtBlackBox.cxx"<<std::endl;
165                         }
166
167                 Wt::WContainerWidget *myCont = new Wt::WContainerWidget(myWtData.parent);
168                 myCont->setMinimumSize(800,500);
169                 std::cout<<"Creando WebWIDGET DELETE ME ---- bbtkWtBlackBox.cxx.................50%"<<std::endl;
170                 myWtData.b->bbUserCreateWidget(myCont);
171                 root()->addWidget(myWtData.parent);
172                 //bbmWindow = myCont;
173                 this->refresh();
174                 //jss = WtBlackBox::jScript;
175                 std::cout<<"Creando WebWIDGET DELETE ME ---- bbtkWtBlackBox.cxx.................80%"<<std::endl;
176                 std::cout<<"Creando WebWIDGET DELETE ME SCRIPT : ---"<<bbtk::jScript<<".................80%"<<std::endl;
177                 //this->doJavaScript(bbtk::jScript); 
178
179
180   }
181   //==================================================================
182   //==================================================================
183   WtWFrame::~WtWFrame() 
184     { 
185       if (mBox.lock())
186         bbtkDebugMessage("widget",9,"["<<mBox.lock()->bbGetName()
187                          <<"] $$$$$$$$$$$$$$$$$$$ ~W()"
188                          <<std::endl);
189       else 
190         bbtkDebugMessage("widget",9,"[DEAD] $$$$$$$$$$$$$$$$$$$$$ ~WtContainer()"<<std::endl);
191       bbtkDebugMessage("widget",9,"WtWFrame::~WtWFrame()"<<std::endl);
192       if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
193       WWt::DecNbWindowsAlive();
194     }
195
196
197  
198    
199         Wt::WApplication *createApplication(const Wt::WEnvironment& env)
200         {
201                         return new WtWFrame(env);                       
202         }
203
204
205   //=========================================================================
206   // WxDialogWxBlackBox::Pointer box
207   //=========================================================================
208
209    //==================================================================
210   // Dialog window which is modal
211  /* class BBTK_EXPORT  WxDialog : public wxDialog
212   {
213   public:
214     WxDialog(WxBlackBox::Pointer box, 
215                      wxWindow *parent, 
216                      wxString title, 
217                      wxSize size);
218     ~WxDialog();Wt::GetTopWindow()
219     void OnCloseWindow(wxCloseEvent& event);
220
221
222     WxBlackBox::WeakPointer mBox;      
223     DECLARE_EVENT_TABLE();
224     
225   };
226   
227   BEGIN_EVENT_TABLE(WxDialog, wxDialog)
228     EVT_CLOSE(WxDialog::OnCloseWindow)
229     END_EVENT_TABLE();
230   //=========================================================================
231
232   //=========================================================================
233   WxDialog::WxDialog(WxBlackBox::Pointer b,
234                      wxWindow *parent,
235                      wxString title,
236                      wxSize size)
237     :
238     wxDialog( parent, 
239                 -1, 
240                 title,
241                 wxDefaultPosition,
242                 size,
243                 wxRESIZE_BORDER | 
244                 wxSYSTEM_MENU  |
245                 wxCLOSE_BOX |
246                 wxMAXIMIZE_BOX | 
247                 wxMINIMIZE_BOX | 
248                 wxCAPTION  
249               ),
250     mBox(b)
251   {
252     bbtkDebugMessage("widget",9,"WxDialog::WxDialog ["<<b->bbGetName()<<"]"
253                      <<std::endl);WxBlackBox::Pointer box
254     Wx::IncNbWindowsAlive();
255     b->bbSetWindow(this);
256     // Insert the widget into the window
257     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
258     b->bbUserCreateWidget(this);
259     wxWindow* widget = b->bbGetOutputWidget();
260     sizer->Add( widget, 1, wxALL|wxGROW, 2);
261     SetSizer(sizer);
262     Layout();
263   }
264   //==================================================================
265   //===========================organise=======================================
266   WxDialog::~WxDialog() 
267     { 
268       bbtkDebugMessage("widget",9,"WxDialog::~WxDialog()"<<std::endl);
269       //bbtkDebugMessage("widget",9,"WxFrame::~WxFrame()"<<std::endl);
270       //      if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
271       //      Wx::DecNbWindowsAlive();
272     }
273    //==================================================================
274    //==================================================================
275   void WxDialog::OnCloseWindow(wxCloseEvent& event)
276     {
277       bbtkDebugMessage("widget",9,"WxDialog::OnCloseWindow()"<<wxGUIEditorGraphicBBS:std::endl);
278       if (mBox.lock()) mBox.lock()->bbSetWindow(0); http://doodle.com/qx65tfxismf7d6ku5vxykghs/admin#table
279       Wx::DecNbWindowsAlive();
280       this->Destroy();
281     }
282    //==================================================================
283
284 */
285  //=========================================================================
286   // WxBlackBox
287   //=========================================================================
288
289   //=========================================================================
290   //=========================================================================
291   //=========================================================================
292   //=========================================================================
293   BBTK_BLACK_BOX_IMPLEMENTATION(WtBlackBox,WidgetBlackBox<Wt::WContainerWidget>);
294         
295         //std::string WtBlackBox::wtServerData::docRoot = "/home/gonzalez/Documents/pruebaResources";
296         //std::string WtBlackBox::wtServerData::docRoot = "/usr/local/share/wtResources";
297   //std::string WtBlackBox::wtServerData::port    = "8081";
298
299         std::string WtBlackBox::wtServerData::docRoot = "<void>";
300         std::string WtBlackBox::wtServerData::port    = "<void>";
301   std::string WtBlackBox::wtServerData::address = "0.0.0.0";
302         
303   //=========================================================================
304   //=========================================================================
305   void WtBlackBox::bbUserSetDefaultValues()
306   {
307     bbtkBlackBoxDebugMessage("widget",5,"WtBlackBox::bbUserSetDefaultValues()"<<std::endl);
308     bbmWindow = 0;
309   /*  WtBlackBox::serData.docRoot = "/home/gonzalez/Documents/pruebaResources";
310     WtBlackBox::serData.address = "0.0.0.0";
311     WtBlackBox::serData.port    = "8080";*/
312     
313
314   }
315   //=========================================================================
316
317   //=========================================================================
318   void WtBlackBox::bbUserInitializeProcessing()
319   {
320
321   }
322   //================================================================http://intranet-if.insa-lyon.fr/temps/5IF39.html==    
323
324   //=========================================================================
325   void WtBlackBox::bbUserFinalizeProcessing()
326   {
327     bbtkBlackBoxDebugMessage("widget",5,"==> WtBlackBox::bbUserFinalizeProcessing()"<<std::endl);
328     bbDestroyWindow();
329     bbtkBlackBoxDebugMessage("widget",5,"<== WtBlackBox::bbUserFinalizeProcessing()"<<std::endl);
330   }
331   //==================================================================    
332
333   //==================================================================    
334   /// Callback for creating a Dialog window (modal)
335   void WtBlackBox::bbCreateDialogWindow()
336   {
337           bbtkBlackBoxDebugMessage("widget",3,
338                      "==> WtBlackBox::bbCreateDialogWindow() - No Dialog must be created, it is a web app"
339                      <<std::endl);
340
341   } 
342   //=========================================================================
343
344   //==================================================================    
345
346   //==================================================================    
347   /// Callback for creating a Frame window 
348         void WtBlackBox::bbCreateFrameWindow()
349         {
350
351                 bbtkBlackBoxDebugMessage("widget",3,
352                         "==> WtBlackBox::bbCreateFrameWindow() - parent = "
353                         <<WWt::GetTopWindow()
354                         <<std::endl);
355
356                 //Setting necessary data for Wt server deployment
357                 myWtData.parent = WWt::GetTopWindow();
358                 myWtData.b      = GetThisPointer<WtBlackBox>();
359                 myWtData.title  = std2wt( bbGetInputWinTitle()  
360                              + " - bbtk (c) CREATIS");
361
362
363
364     
365
366                 
367                 //char  *argv[7] =  {"./WtTest","--docroot","/home/gonzalez/Documents/pruebaResources","--http-address","0.0.0.0","--http-port","8080"};
368                 //int argc = 7;
369                 
370                 
371                         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()};
372                 int argc = 7;
373                 
374                 std::cout<<" ---  PUERTO  :"<<WtBlackBox::wtServerData::port<<std::endl;
375                 
376         /*      if(Wt::WServer::instance()->isRunning())
377                 {
378                         std::cout<<"Checking the existence of a Server deployed  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
379                         Wt::WServer::instance()->stop();
380                 }
381         */
382                 std::cout<<"Deploying Server -- Definition of server parameters by default  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
383                 Wt::WRun(argc,argv,&createApplication); 
384         }
385
386   //==================================================================    
387
388   //==================================================================    
389   void WtBlackBox::bbSetWindow(Wt::WContainerWidget* w) 
390   {
391     bbtkBlackBoxDebugMessage("widget",9,"==> WtBlackBox::bbSetWindow("<<w<<")"<<std::endl);
392     if ((bbmWindow != 0)&&(w!=0)) 
393       { 
394         std::cout << "ERRRRRRRROOOOR"<<std::endl; 
395       }
396     bbmWindow = w;
397   }
398
399         
400
401     //==================================================================    
402
403   //==================================================================
404   void WtBlackBox::bbShowWindow()
405   {
406     if ((bbmWindow) && (!bbIsShown()))
407       {
408         bbtkBlackBoxDebugMessage("widget",3,"==> WtBlackBox::bbShowWindow()"
409                          <<std::endl);
410         if (bbGetInputWinDialog()) 
411           {
412           //  ((WxDialog*)bbmWindow)->ShowModal();
413           }
414         else
415           {
416                 // There is not such a thing like show becouse it is a webapp
417            // bbGetWindow()->Show();
418           //  WWt::CreateWtAppIfNeeded();
419             bbSetShown(true);
420           } 
421       }
422   }
423   //==================================================================    
424
425   //==================================================================    
426   void WtBlackBox::bbDestroyWindow()
427   {
428     bbtkBlackBoxDebugMessage("widget",3,"==> WtBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
429     if (bbGetWindow()!=NULL) 
430       {
431         //       wxWindow* w = bbGetWindow();
432         //bbSetWindow(0);
433         // WE SHOULD DESTROY THE WINDOW WITH THE Close METHOD
434         // HOWEVER I
435         //w->Close(true);
436         //
437         delete bbGetWindow();
438         bbSetShown(false);
439       }
440     bbtkBlackBoxDebugMessage("widget",3,"<== WtBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
441
442   }
443   //==================================================================    
444
445 }//namespace bbtk
446
447 #endif
448
449