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