]> Creatis software - bbtk.git/blob - kernel/src/bbtkWidgetBlackBox.txx
3da66eb0a4facfd0325cfbe8608dc2ed46178bb4
[bbtk.git] / kernel / src / bbtkWidgetBlackBox.txx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWidgetBlackBox.txx,v $
4   Language:  C++
5   Date:      $Date: 2010/01/14 13:17:27 $
6   Version:   $Revision: 1.6 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief 
34  */
35
36
37  
38 //#include "bbtkWidgetBlackBoxWindow.h"
39 #include "bbtkBlackBoxOutputConnector.h"
40
41
42 namespace bbtk
43 {
44
45   //=========================================================================
46   // WidgetBlackBox
47   //=========================================================================
48
49   //=========================================================================
50   //=========================================================================
51   //=========================================================================
52   //=========================================================================
53   BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(WidgetBlackBox,AtomicBlackBox);
54   //=========================================================================
55   
56 //=========================================================================
57   template <class T>  
58   void WidgetBlackBox<T>::bbUserSetDefaultValues()
59   {
60     bbtkBlackBoxDebugMessage("widget",9,"WidgetBlackBox::bbUserSetDefaultValues()"<<std::endl);
61     //    bbmWindow = 0;
62     bbSetInputWinTitle(bbGetName());
63     bbSetInputWinWidth(800);
64     bbSetInputWinHeight(800);
65     bbSetInputWinDialog(false);
66     bbSetOutputWidget(0);
67   }
68   //=========================================================================
69
70  
71   //=========================================================================
72   template <class T>  
73   void WidgetBlackBox<T>::bbUserInitializeProcessing()
74   {
75     bbmShown = false;
76   }
77   //=========================================================================
78
79   //=========================================================================
80   template <class T>  
81   void WidgetBlackBox<T>::bbUserFinalizeProcessing()
82   {
83     /*
84     std::cout << "****** WidgetBlackBox<T>::bbUserFinalizeProcessing()"<<std::endl;
85     bbtkBlackBoxDebugMessage("widget",9,"==> WidgetBlackBox::bbUserFinalizeProcessing() ["<<bbGetFullName()<<"]"<<std::endl);
86     if (bbGetWindow()) 
87       {
88         bool shown = bbGetWindow()->bbIsShown();
89         bbGetWindow()->bbDestroy();
90         bbSetWindow(0);
91         
92         if (GetUseCount()==0)
93           {    
94             bbtkBlackBoxDebugMessage("widget",9,"**** HERE use count == 0"<<std::endl);
95             if (shown) DecNbWindowsShown();
96             DecNbWindowsAlive();
97           }
98
99       }
100     bbtkBlackBoxDebugMessage("widget",9,"<== WidgetBlackBox::bbUserFinalizeProcessing() ["<<bbGetFullName()<<"]"<<std::endl);
101     */
102   }
103   //=========================================================================
104   
105
106
107  
108
109   //=========================================================================
110   template <class T>  
111   void WidgetBlackBox<T>::bbProcess()
112   { 
113     // TODO : update the window size and title 
114     this->bbUserProcess(); 
115   }
116   //=========================================================================
117
118   //=========================================================================
119   template <class T>  
120   void WidgetBlackBox<T>::bbCreateWindow()
121   { 
122
123
124     // If output 'Widget' not connected then
125     // it is a top level Widget have to create and show the Window
126     if ( ! bbIsOutputWidgetConnected() )
127       {
128         // create the Window if does not exist
129         if (!bbWindowIsCreated())
130           {
131             bbtkBlackBoxDebugMessage("widget",2,
132                              "-> Creating the window"
133                              <<std::endl);
134             // Reinitializing
135             bbSetShown(false);
136             bbmNestedWidgetBoxes.clear();
137             
138             //      this->InitWindowManagerIfNeeded();
139             // If is a Dialog requested
140             if ( bbGetInputWinDialog() )
141               {
142                 bbtkBlackBoxDebugMessage("widget",2,
143                                  "   Input WinDialog set to true : creating a Dialog"
144                                  <<std::endl);
145                 this->bbCreateDialogWindow();
146               }
147             // Input WinDialog set to false : creating a Frame
148             else 
149               {
150                 bbtkBlackBoxDebugMessage("widget",2,
151                                  "   Input WinDialog set to false : creating a Frame"
152                                  <<std::endl);
153                 this->bbCreateFrameWindow();
154               }
155           }
156         /*
157         // Show the window
158         if ( !bbGetWindow() )
159           {
160             bbtkInternalError("Need to show the Window of widget "<<bbGetName()
161                               <<" however was not created by apropriate cb");
162           }
163         */
164
165         /*
166         bbtkBlackBoxDebugMessage("widget",2,
167                          "-> Showing the window"
168                          <<std::endl);
169         //      bbGetWindow()->bbShow(); 
170         this->bbShowWindow();
171         */
172       }           
173     //   
174   }
175   //=========================================================================
176   
177
178   //==================================================================
179   template <class T>  
180   typename WidgetBlackBox<T>::WidgetPointer  
181   WidgetBlackBox<T>::bbCreateWidgetOfInput
182   (const std::string& in, typename WidgetBlackBox<T>::WidgetPointer parent)
183   {
184     bbtkBlackBoxDebugMessage("widget",2,"bbCreateWidgetOfInput("
185                              <<in<<","<<parent<<")"
186                              <<std::endl);
187     Widget* w = 0;
188     // If input is connected 
189     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
190     if ( c->IsConnected() )                     
191       {
192         // Get black box from 
193         BlackBox::Pointer from = 
194           c->GetConnection()->GetBlackBoxFrom();
195         // Cast it into a WidgetBlackBox
196         typename WidgetBlackBox<T>::Pointer wfrom 
197           = boost::dynamic_pointer_cast<WidgetBlackBox<T> >(from);
198         // If the black box from construction is not done yet : do it
199          // If not constructed do it 
200         wfrom->bbInitializeProcessing();
201         // Call bbUserCreateWidget
202         wfrom->bbUserCreateWidget(parent);
203         // Get the widget created
204         w = wfrom->bbGetOutputWidget();
205         // Insert the box into the NestedWidgetBoxes list
206         bbmNestedWidgetBoxes.push_back(from);
207       }
208     return w;
209   }
210   //==================================================================
211
212   //=========================================================================
213   template <class T>  
214   bool WidgetBlackBox<T>::bbIsOutputWidgetConnected()
215   {
216     return ((*bbGetOutputConnectorMap().find("Widget")).second->GetConnectionVector().size() != 0 );
217   }
218   //==================================================================
219
220   //==================================================================
221   template <class T>  
222   void WidgetBlackBox<T>::bbSetShown(bool b)
223   {
224     if (b==bbmShown) return;
225
226     bbtkBlackBoxDebugMessage("widget",5,"WidgetBlackBox::bbSetShown("
227                              <<b<<")"
228                              <<std::endl);
229     
230     bbmShown = b;
231     if (b)
232       {
233         this->bbUserOnShow();
234      }
235     // Have to propagate to nested widgets
236     std::vector<BlackBox::WeakPointer>::const_iterator i;
237     for (i =bbmNestedWidgetBoxes.begin();
238          i!=bbmNestedWidgetBoxes.end();
239          ++i)
240       {
241         if (!i->expired()) i->lock()->bbSetShown(b);
242       }
243          
244   }
245   //==================================================================
246
247   //==================================================================
248    template <class T>  
249    bool WidgetBlackBox<T>::bbIsShown()
250   {
251     return bbmShown;
252   }
253   //==================================================================
254  
255   /*
256  //==================================================================
257   template <class T>  
258    void WidgetBlackBox<T>::bbShowWindow()
259   {
260     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbShowWindow() ["
261                         <<bbGetFullName()<<"]"<<std::endl);
262
263     if (bbGetWindow()!=0) bbGetWindow()->bbShow();
264
265     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbShowWindow() ["
266                         <<bbGetFullName()<<"]"<<std::endl);
267   }
268   //==================================================================
269
270   //==================================================================
271   template <class T>  
272    void WidgetBlackBox<T>::bbHideWindow()
273   {
274     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbHideWindow() ["
275                         <<bbGetFullName()<<"]"<<std::endl);
276
277     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
278
279     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbHideWindow() ["
280                         <<bbGetFullName()<<"]"<<std::endl);
281   }
282   //==================================================================
283
284
285   //==================================================================
286   template <class T>  
287   void WidgetBlackBox<T>::bbCloseWindow()
288   {
289     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbCloseWindow() ["
290                         <<bbGetFullName()<<"]"<<std::endl);
291
292     if (bbGetWindow()!=0) bbGetWindow()->bbDestroy();
293
294     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbCloseWindow() ["
295                         <<bbGetFullName()<<"]"<<std::endl);
296   }
297   //==================================================================
298
299   //==================================================================
300   template <class T>  
301   typename WidgetBlackBox<T>::Window* 
302   WidgetBlackBox<T>::bbGetContainingWindow()
303   {
304     if (bbGetWindow()!=0) return bbGetWindow();
305     BlackBox::OutputConnectorMapType::const_iterator i 
306       = bbGetOutputConnectorMap().find("Widget");
307     if ( i->second->GetConnectionVector().size() != 0 ) 
308       {
309         return boost::static_pointer_cast<WidgetBlackBox>
310           (i->second->GetConnectionVector().front() //.lock()
311            ->GetBlackBoxTo())->bbGetContainingWindow();
312       }
313     return 0;
314   }
315   //==================================================================
316
317
318   //==================================================================
319   template <class T>  
320   bool WidgetBlackBox<T>::bbIsShown()
321   {
322     if (bbGetContainingWindow()!=0)
323       return bbGetContainingWindow()->bbIsShown();
324     return false;
325   }
326   //==================================================================
327   */
328
329 }//namespace bbtk
330
331
332
333