]> Creatis software - bbtk.git/blob - kernel/src/bbtkWidgetBlackBox.txx
2aeb1783de0d188113b47349f13e75b8da9b3da7
[bbtk.git] / kernel / src / bbtkWidgetBlackBox.txx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWidgetBlackBox.txx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/19 10:19:23 $
6   Version:   $Revision: 1.4 $
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     bbtkDebugMessage("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   }
75   //=========================================================================
76
77   //=========================================================================
78   template <class T>  
79   void WidgetBlackBox<T>::bbUserFinalizeProcessing()
80   {
81     bbtkDebugMessage("widget",9,"==> WidgetBlackBox::bbUserFinalizeProcessing() ["<<bbGetFullName()<<"]"<<std::endl);
82     if (bbGetWindow()) 
83       {
84         if (GetUseCount()==0)
85           {
86             if (bbGetWindow()->bbIsShown()) DecNbWindowsShown();
87             DecNbWindowsAlive();
88           }
89         bbGetWindow()->bbDestroy();
90         bbSetWindow(0);
91       }
92     bbtkDebugMessage("widget",9,"<== WidgetBlackBox::bbUserFinalizeProcessing() ["<<bbGetFullName()<<"]"<<std::endl);
93   }
94   //=========================================================================
95   
96
97
98  
99
100   //=========================================================================
101   template <class T>  
102   void WidgetBlackBox<T>::bbProcess()
103   { 
104     // TODO : update the window size and title 
105     this->bbUserProcess(); 
106   }
107   //=========================================================================
108
109   //=========================================================================
110   template <class T>  
111   void WidgetBlackBox<T>::bbCreateWindow()
112   { 
113
114
115     // If output 'Widget' not connected then
116     // it is a top level Widget have to create and show the Window
117     if ( ! bbIsOutputWidgetConnected() )
118       {
119         // create the Window if does not exist
120         if ( bbGetWindow() == 0)
121           {
122             bbtkDebugMessage("widget",2,
123                              "-> Creating the window"
124                              <<std::endl);
125             //      this->InitWindowManagerIfNeeded();
126             // If is a Dialog requested
127             if ( bbGetInputWinDialog() )
128               {
129                 bbtkDebugMessage("process",2,
130                                  "   Input WinDialog set to true : creating a Dialog"
131                                  <<std::endl);
132                 this->bbCreateDialogWindow();
133               }
134             // Input WinDialog set to false : creating a Frame
135             else 
136               {
137                 bbtkDebugMessage("process",2,
138                                  "   Input WinDialog set to false : creating a Frame"
139                                  <<std::endl);
140                 this->bbCreateFrameWindow();
141               }
142           }
143         // Show the window
144         if ( !bbGetWindow() )
145           {
146             bbtkInternalError("Need to show the Window of widget "<<bbGetName()
147                               <<" however was not created by apropriate cb");
148           }
149         bbtkDebugMessage("widget",2,
150                          "-> Showing the window"
151                          <<std::endl);
152         bbGetWindow()->bbShow(); 
153       }           
154     //   
155   }
156   //=========================================================================
157   
158
159   //==================================================================
160   template <class T>  
161   typename WidgetBlackBox<T>::WidgetPointer  
162   WidgetBlackBox<T>::bbCreateWidgetOfInput
163   (const std::string& in, typename WidgetBlackBox<T>::WidgetPointer parent)
164   {
165     Widget* w = 0;
166     // If input is connected 
167     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
168     if ( c->IsConnected() )                     
169       {
170         // Get black box from 
171         BlackBox::Pointer from = 
172           c->GetConnection()->GetBlackBoxFrom();
173         // Cast it into a WidgetBlackBox
174         typename WidgetBlackBox<T>::Pointer wfrom 
175           = boost::dynamic_pointer_cast<WidgetBlackBox<T> >(from);
176         // If the black box from construction is not done yet : do it
177          // If not constructed do it 
178         wfrom->bbInitializeProcessing();
179         // Call bbUserCreateWidget
180         wfrom->bbUserCreateWidget(parent);
181         // Get the widget created
182         w = wfrom->bbGetOutputWidget();
183       }
184     return w;
185   }
186   //==================================================================
187
188   //=========================================================================
189   template <class T>  
190   bool WidgetBlackBox<T>::bbIsOutputWidgetConnected()
191   {
192     return ((*bbGetOutputConnectorMap().find("Widget")).second->GetConnectionVector().size() != 0 );
193   }
194   //==================================================================
195
196  //==================================================================
197   template <class T>  
198    void WidgetBlackBox<T>::bbShowWindow()
199   {
200     bbtkDebugMessageInc("widget",1,"=> WidgetBlackBox::bbShowWindow() ["
201                         <<bbGetFullName()<<"]"<<std::endl);
202
203     if (bbGetWindow()!=0) bbGetWindow()->bbShow();
204
205     bbtkDebugMessageDec("widget",2,"<= WidgetBlackBox::bbShowWindow() ["
206                         <<bbGetFullName()<<"]"<<std::endl);
207   }
208   //==================================================================
209
210   //==================================================================
211   template <class T>  
212    void WidgetBlackBox<T>::bbHideWindow()
213   {
214     bbtkDebugMessageInc("widget",1,"=> WidgetBlackBox::bbHideWindow() ["
215                         <<bbGetFullName()<<"]"<<std::endl);
216
217     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
218
219     bbtkDebugMessageDec("widget",2,"<= WidgetBlackBox::bbHideWindow() ["
220                         <<bbGetFullName()<<"]"<<std::endl);
221   }
222   //==================================================================
223
224
225   //==================================================================
226   template <class T>  
227   void WidgetBlackBox<T>::bbCloseWindow()
228   {
229     bbtkDebugMessageInc("widget",1,"=> WidgetBlackBox::bbCloseWindow() ["
230                         <<bbGetFullName()<<"]"<<std::endl);
231
232     if (bbGetWindow()!=0) bbGetWindow()->bbDestroy();
233
234     bbtkDebugMessageDec("widget",2,"<= WidgetBlackBox::bbCloseWindow() ["
235                         <<bbGetFullName()<<"]"<<std::endl);
236   }
237   //==================================================================
238
239   //==================================================================
240   template <class T>  
241   typename WidgetBlackBox<T>::Window* 
242   WidgetBlackBox<T>::bbGetContainingWindow()
243   {
244     if (bbGetWindow()!=0) return bbGetWindow();
245     BlackBox::OutputConnectorMapType::const_iterator i 
246       = bbGetOutputConnectorMap().find("Widget");
247     if ( i->second->GetConnectionVector().size() != 0 ) 
248       {
249         return boost::static_pointer_cast<WidgetBlackBox>
250           (i->second->GetConnectionVector().front() //.lock()
251            ->GetBlackBoxTo())->bbGetContainingWindow();
252       }
253     return 0;
254   }
255   //==================================================================
256
257
258   //==================================================================
259   template <class T>  
260   bool WidgetBlackBox<T>::bbIsShown()
261   {
262     if (bbGetContainingWindow()!=0)
263       return bbGetContainingWindow()->bbIsShown();
264     return false;
265   }
266   //==================================================================
267
268
269 }//namespace bbtk
270
271
272
273