]> Creatis software - bbtk.git/blob - kernel/src/bbtkWtBlackBox.cxx
93ef1274417e92a2d95a68159ab270e96dd84bfe
[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 static std::string jScriptLine;
56
57 namespace bbtk
58 {
59   //=========================================================================
60   // wtData structure
61   //=========================================================================
62
63         // For the very first contruction of the application
64         struct wtData
65         {
66                 Wt::WContainerWidget *parent;
67                 WtBlackBox::Pointer b;
68                 Wt::WString title;              
69         };
70
71         static wtData myWtData;
72
73   //=========================================================================
74   // javaScript Line
75   //=========================================================================
76         //Contains all the JS statements needed for the bbwt.
77         
78   //=========================================================================
79   // WxFrame
80   //=========================================================================
81         // Application WT deployed
82   //==================================================================
83  
84
85  
86  class WtWFrame : public Wt::WApplication
87   {
88   public:
89     WtWFrame(
90                         const Wt::WEnvironment& env                     
91             );
92     ~WtWFrame();
93
94         
95         
96         WtBlackBox::WeakPointer mBox; 
97
98
99   };    
100
101
102         WtWFrame::WtWFrame(
103                         const Wt::WEnvironment& env
104                    )
105     : 
106     Wt::WApplication(env)
107   {  
108                 //std::cout<<"Adding JavaScript --  DELETE ME -- bbtkWtBlackBox.cxx"<<std::endl;
109                 this->require("js/xtk.js");
110                 //this->require("js/demo/demo2.js");
111                 //this->useStyleSheet("css/demo.css");
112                 jScriptLine = "";
113                 mBox = myWtData.b;
114                 if(!myWtData.parent)
115                         {
116                                 std::cout<<"        SIN PADRE "<<std::endl;
117                                 if(myWtData.parent == 0){std::cout<<"        PARENT 0 "<<std::endl;}
118                                 if(myWtData.parent == NULL){std::cout<<"        PARENT NULL "<<std::endl;}
119                                 myWtData.parent = new Wt::WContainerWidget();
120                                 std::cout<<"Parent created --  DELETE ME -- bbtkWtBlackBox.cxx"<<std::endl;
121                         }
122
123                 Wt::WContainerWidget *myCont = new Wt::WContainerWidget(myWtData.parent);
124                 myCont->setMinimumSize(800,500);
125                 std::cout<<"Creando WebWIDGET DELETE ME ---- bbtkWtBlackBox.cxx.................50%"<<std::endl;
126                 myWtData.b->bbUserCreateWidget(myCont);
127                 root()->addWidget(myWtData.parent);
128                 //bbmWindow = myCont;
129                 std::cout<<"Creando WebWIDGET DELETE ME ---- bbtkWtBlackBox.cxx.................80%"<<std::endl;
130                 this->doJavaScript(jScriptLine); 
131
132
133   }
134   //==================================================================
135   //==================================================================
136   WtWFrame::~WtWFrame() 
137     { 
138       if (mBox.lock())
139         bbtkDebugMessage("widget",9,"["<<mBox.lock()->bbGetName()
140                          <<"] $$$$$$$$$$$$$$$$$$$ ~W()"
141                          <<std::endl);
142       else 
143         bbtkDebugMessage("widget",9,"[DEAD] $$$$$$$$$$$$$$$$$$$$$ ~WtContainer()"<<std::endl);
144       bbtkDebugMessage("widget",9,"WtWFrame::~WtWFrame()"<<std::endl);
145       if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
146       WWt::DecNbWindowsAlive();
147     }
148
149
150  
151    
152         Wt::WApplication *createApplication(const Wt::WEnvironment& env)
153         {
154                         return new WtWFrame(env);                       
155         }
156
157
158   //=========================================================================
159   // WxDialogWxBlackBox::Pointer box
160   //=========================================================================
161
162    //==================================================================
163   // Dialog window which is modal
164  /* class BBTK_EXPORT  WxDialog : public wxDialog
165   {
166   public:
167     WxDialog(WxBlackBox::Pointer box, 
168                      wxWindow *parent, 
169                      wxString title, 
170                      wxSize size);
171     ~WxDialog();Wt::GetTopWindow()
172     void OnCloseWindow(wxCloseEvent& event);
173
174
175     WxBlackBox::WeakPointer mBox;      
176     DECLARE_EVENT_TABLE();
177     
178   };
179   
180   BEGIN_EVENT_TABLE(WxDialog, wxDialog)
181     EVT_CLOSE(WxDialog::OnCloseWindow)
182     END_EVENT_TABLE();
183   //=========================================================================
184
185   //=========================================================================
186   WxDialog::WxDialog(WxBlackBox::Pointer b,
187                      wxWindow *parent,
188                      wxString title,
189                      wxSize size)
190     :
191     wxDialog( parent, 
192                 -1, 
193                 title,
194                 wxDefaultPosition,
195                 size,
196                 wxRESIZE_BORDER | 
197                 wxSYSTEM_MENU  |
198                 wxCLOSE_BOX |
199                 wxMAXIMIZE_BOX | 
200                 wxMINIMIZE_BOX | 
201                 wxCAPTION  
202               ),
203     mBox(b)
204   {
205     bbtkDebugMessage("widget",9,"WxDialog::WxDialog ["<<b->bbGetName()<<"]"
206                      <<std::endl);WxBlackBox::Pointer box
207     Wx::IncNbWindowsAlive();
208     b->bbSetWindow(this);
209     // Insert the widget into the window
210     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
211     b->bbUserCreateWidget(this);
212     wxWindow* widget = b->bbGetOutputWidget();
213     sizer->Add( widget, 1, wxALL|wxGROW, 2);
214     SetSizer(sizer);
215     Layout();
216   }
217   //==================================================================
218   //==================================================================
219   WxDialog::~WxDialog() 
220     { 
221       bbtkDebugMessage("widget",9,"WxDialog::~WxDialog()"<<std::endl);
222       //bbtkDebugMessage("widget",9,"WxFrame::~WxFrame()"<<std::endl);
223       //      if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
224       //      Wx::DecNbWindowsAlive();
225     }
226    //==================================================================
227    //==================================================================
228   void WxDialog::OnCloseWindow(wxCloseEvent& event)
229     {
230       bbtkDebugMessage("widget",9,"WxDialog::OnCloseWindow()"<<wxGUIEditorGraphicBBS:std::endl);
231       if (mBox.lock()) mBox.lock()->bbSetWindow(0); http://doodle.com/qx65tfxismf7d6ku5vxykghs/admin#table
232       Wx::DecNbWindowsAlive();
233       this->Destroy();
234     }
235    //==================================================================
236
237 */
238  //=========================================================================
239   // WxBlackBox
240   //=========================================================================
241
242   //=========================================================================
243   //=========================================================================
244   //=========================================================================
245   //=========================================================================
246   BBTK_BLACK_BOX_IMPLEMENTATION(WtBlackBox,WidgetBlackBox<Wt::WContainerWidget>);
247
248   //=========================================================================
249   //=========================================================================
250   void WtBlackBox::bbUserSetDefaultValues()
251   {
252     bbtkBlackBoxDebugMessage("widget",5,"WtBlackBox::bbUserSetDefaultValues()"<<std::endl);
253     bbmWindow = 0;
254   }
255   //=========================================================================
256
257   //=========================================================================
258   void WtBlackBox::bbUserInitializeProcessing()
259   {
260
261   }
262   //================================================================http://intranet-if.insa-lyon.fr/temps/5IF39.html==    
263
264   //=========================================================================
265   void WtBlackBox::bbUserFinalizeProcessing()
266   {
267     bbtkBlackBoxDebugMessage("widget",5,"==> WtBlackBox::bbUserFinalizeProcessing()"<<std::endl);
268     bbDestroyWindow();
269     bbtkBlackBoxDebugMessage("widget",5,"<== WtBlackBox::bbUserFinalizeProcessing()"<<std::endl);
270   }
271   //==================================================================    
272
273   //==================================================================    
274   /// Callback for creating a Dialog window (modal)
275   void WtBlackBox::bbCreateDialogWindow()
276   {
277           bbtkBlackBoxDebugMessage("widget",3,
278                      "==> WtBlackBox::bbCreateDialogWindow() - No Dialog must be created, it is a web app"
279                      <<std::endl);
280
281   } 
282   //=========================================================================
283
284   //==================================================================    
285
286   //==================================================================    
287   /// Callback for creating a Frame window 
288         void WtBlackBox::bbCreateFrameWindow()
289         {
290
291                 bbtkBlackBoxDebugMessage("widget",3,
292                         "==> WtBlackBox::bbCreateFrameWindow() - parent = "
293                         <<WWt::GetTopWindow()
294                         <<std::endl);
295
296                 //Setting necessary data for Wt server deployment
297                 myWtData.parent = WWt::GetTopWindow();
298                 myWtData.b = GetThisPointer<WtBlackBox>();
299                 myWtData.title = std2wt( bbGetInputWinTitle()  
300                              + " - bbtk (c) CREATIS");
301
302                 std::cout<<"Creating Container WT -- Definition of server parameters by default  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
303                 char  *argv[7] =  {"./WtTest","--docroot","/home/gonzalez/Documents/pruebaResources","--http-address","0.0.0.0","--http-port","8080"};
304                 int argc = 7;
305         /*      if(Wt::WServer::instance()->isRunning())
306                 {
307                         std::cout<<"Checking the existence of a Server deployed  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
308                         Wt::WServer::instance()->stop();
309                 }
310         */
311                 std::cout<<"Deploying Server -- Definition of server parameters by default  -DELETE THIS MESSAGE bbtkWTBlackBox.cxx"<<std::endl;
312                 Wt::WRun(argc,argv,&createApplication); 
313         }
314
315   //==================================================================    
316
317   //==================================================================    
318   void WtBlackBox::bbSetWindow(Wt::WContainerWidget* w) 
319   {
320     bbtkBlackBoxDebugMessage("widget",9,"==> WtBlackBox::bbSetWindow("<<w<<")"<<std::endl);
321     if ((bbmWindow != 0)&&(w!=0)) 
322       { 
323         std::cout << "ERRRRRRRROOOOR"<<std::endl; 
324       }
325     bbmWindow = w;
326   }
327
328         
329
330     //==================================================================    
331
332   //==================================================================
333   void WtBlackBox::bbShowWindow()
334   {
335     if ((bbmWindow) && (!bbIsShown()))
336       {
337         bbtkBlackBoxDebugMessage("widget",3,"==> WtBlackBox::bbShowWindow()"
338                          <<std::endl);
339         if (bbGetInputWinDialog()) 
340           {
341           //  ((WxDialog*)bbmWindow)->ShowModal();
342           }
343         else
344           {
345                 // There is not such a thing like show becouse it is a webapp
346            // bbGetWindow()->Show();
347           //  WWt::CreateWtAppIfNeeded();
348             bbSetShown(true);
349           } 
350       }
351   }
352   //==================================================================    
353
354   //==================================================================    
355   void WtBlackBox::bbDestroyWindow()
356   {
357     bbtkBlackBoxDebugMessage("widget",3,"==> WtBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
358     if (bbGetWindow()!=NULL) 
359       {
360         //       wxWindow* w = bbGetWindow();
361         //bbSetWindow(0);
362         // WE SHOULD DESTROY THE WINDOW WITH THE Close METHOD
363         // HOWEVER I
364         //w->Close(true);
365         //
366         delete bbGetWindow();
367         bbSetShown(false);
368       }
369     bbtkBlackBoxDebugMessage("widget",3,"<== WtBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
370
371   }
372   //==================================================================    
373
374 }//namespace bbtk
375
376 #endif
377
378