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