]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxBlackBox.h
Another ugly bug fixed in pipeline executing (bad transfer to parent in some cases...
[bbtk.git] / kernel / src / bbtkWxBlackBox.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxBlackBox.h,v $
4   Language:  C++
5   Date:      $Date: 2008/11/13 14:46:43 $
6   Version:   $Revision: 1.19 $
7 ========================================================================*/
8
9
10 /* ---------------------------------------------------------------------
11
12 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
13 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 *
15 *  This software is governed by the CeCILL-B license under French law and 
16 *  abiding by the rules of distribution of free software. You can  use, 
17 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
18 *  license as circulated by CEA, CNRS and INRIA at the following URL 
19 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
20 *  or in the file LICENSE.txt.
21 *
22 *  As a counterpart to the access to the source code and  rights to copy,
23 *  modify and redistribute granted by the license, users are provided only
24 *  with a limited warranty  and the software's author,  the holder of the
25 *  economic rights,  and the successive licensors  have only  limited
26 *  liability. 
27 *
28 *  The fact that you are presently reading this means that you have had
29 *  knowledge of the CeCILL-B license and that you accept its terms.
30 * ------------------------------------------------------------------------ */                                                                         
31 /**
32  * \brief Short description in one line
33  * 
34  * Long description which 
35  * can span multiple lines
36  */
37 /**
38  * \file 
39  * \brief 
40  */
41 /**
42  * \class bbtk::
43  * \brief 
44  */
45
46
47 #ifdef _USE_WXWIDGETS_
48
49
50 #ifndef __bbtkWxBlackBox_h__
51 #define __bbtkWxBlackBox_h__
52
53
54 #include "bbtkWx.h"
55 #include "bbtkAtomicBlackBox.h"
56
57
58 namespace bbtk
59 {
60
61
62
63
64   //==================================================================
65   // Forward declaration of the class of window associated to a WxBlackBox
66   class WxBlackBoxWindow;
67   //==================================================================
68
69   //==================================================================
70   // Forward declaration of the widget event handler class
71   class WxBlackBoxWidgetEventHandler;
72   //==================================================================
73
74
75   //==================================================================
76   /// Widget black boxes
77   class BBTK_EXPORT WxBlackBox : public bbtk::AtomicBlackBox  
78   { 
79     BBTK_BLACK_BOX_INTERFACE(WxBlackBox,bbtk::AtomicBlackBox);
80     //   BBTK_DECLARE_INPUT(WinParent,WxParentToChildData*);
81     BBTK_DECLARE_INPUT(WinTitle,std::string);
82     BBTK_DECLARE_INPUT(WinWidth,int);
83     BBTK_DECLARE_INPUT(WinHeight,int);
84     BBTK_DECLARE_INPUT(WinDialog,bool);
85     BBTK_DECLARE_INPUT(WinHide,Void);
86     BBTK_DECLARE_INPUT(WinClose,Void);
87     BBTK_DECLARE_OUTPUT(Widget, wxWindow*);//WxBlackBoxWidget*);
88
89   public:
90     /// Main processing method of the box. Overloaded to handle windows inclusion : if the output Widget is connected then the execution is transfered to the box to which it is connected (as the container window must be created and displayed - this box will be executed by the normal pipeline recursion mechanism)
91     virtual void bbExecute(bool force = false);
92
93
94     typedef WxBlackBoxWindow Window;
95     
96     /// Returns the **OWN** window associated to the box
97     /// If 0 returned = no window
98     Window* bbGetWindow() { return bbmWindow; }
99
100     /// Returns the window containing the widget associated to the box
101     /// Which can be the own window of **ANOTHER** box in case of 
102     /// a hierarchy of widgets.
103     /// More precisely :
104     /// If bbGetWindow() != 0 then returns bbGetWindow()
105     /// Else if the output 'Widget' is connected 
106     ///  then returns bbGetContainingWindow() of the box connected to 'Widget'
107     /// Else returns 0;
108     Window* bbGetContainingWindow();
109
110     /// Returns the parent wxWindow that must be used to create the widget 
111     wxWindow* bbGetWxParent();
112
113     /// Returns true iff the 'containing window' exists and is shown 
114     /// (see bbGetContainingWindow).
115     bool bbIsShown();
116
117     //==================================================================    
118     /// User callback invoked when the containing window is shown
119     virtual void bbUserOnShow() {}
120     //==================================================================    
121  
122     //==================================================================    
123     /// User callback invoked when the containing window is hidden
124     virtual void bbUserOnHide() {}
125     //==================================================================    
126
127   protected:
128     
129   
130     //==================================================================
131     /// User callback called in the box contructor
132     virtual void bbUserConstructor();
133     /// User callback called in the box copy constructor
134     virtual void bbUserCopyConstructor();
135     /// User callback called in the box destructor
136     virtual void bbUserDestructor();
137     //==================================================================    
138
139     //==================================================================    
140     /// User callback for creating the widget associated to the box
141     /// ** Must be defined **
142     virtual void bbUserCreateWidget() 
143     {
144       bbtkError(bbGetTypeName()<<" is a WxBlackBox whose bbUserCreateWidget methods is not overloaded : is it a feature or a bug ?!?");
145     }
146     //==================================================================    
147
148
149     
150
151     //==================================================================
152     /// Main processing method of the box.
153     virtual IOStatus bbBackwardUpdate( Connection::Pointer caller );
154     //==================================================================
155
156
157
158     //==================================================================
159     /// Overloaded processing method for WxBlackBoxes :
160     /// 1) if the widget is null then 
161     ///    calls the user defined widget creation method : bbUserCreateWidget()
162     /// 2) calls the user defined processing method : bbUserProcess()
163     /// 3) displays the window : bbShowWindow();
164     virtual void bbProcess();
165     //==================================================================
166
167     //==================================================================
168     /// If necessary creates the WxBlackBoxWindow associated to the box
169     /// and shows it 
170     /// (does nothing if the box output 'Widget' is connected which 
171     /// means that the box does not have its own window but is contained 
172     /// into another window)
173     void bbShowWindow();
174     /// Hides the WxBlackBoxWindow associated to the box (if exists)
175     void bbHideWindow();
176     /// Closes (destroys) the WxBlackBoxWindow associated to the box (if exists)
177     void bbCloseWindow();
178     //==================================================================
179
180   private:
181     /// friendship
182     friend class WxBlackBoxWindow;
183     friend class WxBlackBoxWidgetEventHandler;
184
185     /// Sets the window
186     inline void bbSetWindow(Window* w) { bbmWindow=w; }
187
188     /// Sets the Widget Event Handler
189     inline void bbSetWidgetEventHandler(WxBlackBoxWidgetEventHandler* w) 
190     { bbmWidgetEventHandler = w; }
191     /// Gets the Widget Event Handler
192     inline WxBlackBoxWidgetEventHandler* bbGetWidgetEventHandler()
193     { return bbmWidgetEventHandler; }
194
195
196     /// The WxBlackBoxWindow associated to the box
197     Window* bbmWindow;
198     /// The WxBlackBoxWidgetEventHandler associated to the box
199     WxBlackBoxWidgetEventHandler* bbmWidgetEventHandler;
200
201
202     void bbInitAttributes();
203
204   protected :
205
206
207     /// For Forward update mechanism when execution is called 
208     /// on a contained window
209     /// Is set to true before transfering update to parent 
210     /// in order to not re-transfer a second time...
211     bool bbmUpdateTransferedToParent;
212
213     bool bbGetUpdateTransferedToParent() const { return bbmUpdateTransferedToParent; }
214     void bbSetUpdateTransferedToParent(bool b) 
215     { bbmUpdateTransferedToParent = b; }
216
217   };
218   //=================================================================
219  
220
221   //======================================================================
222   /// Defines the bbUserCreateWidget method
223 #define BBTK_CREATE_WIDGET(CALLBACK)                                    \
224   public:                                                               \
225   inline void bbUserCreateWidget()                                      \
226   {                                                                     \
227     bbtkDebugMessageInc("wx",1,"**> Creating widget for ["              \
228                         <<bbGetFullName()<<"]"<<std::endl);             \
229     CALLBACK();                                                         \
230     bbtkDebugMessageInc("wx",1,"<** Creating widget for ["              \
231                         <<bbGetFullName()<<"]"<<std::endl);             \
232   }
233   
234   //======================================================================
235
236   //======================================================================
237   /// Defines the bbUserOnShow method
238 #define BBTK_ON_SHOW_WIDGET(CALLBACK)                                   \
239   public:                                                               \
240   inline void bbUserOnShow()                                    \
241   {                                                                     \
242     bbtkDebugMessageInc("wx",1,"**> Showing ["          \
243                         <<bbGetFullName()<<"]"<<std::endl);             \
244     CALLBACK();                                                         \
245     bbtkDebugMessageInc("wx",1,"<** Showing ["          \
246                         <<bbGetFullName()<<"]"<<std::endl);             \
247   }
248   
249   //======================================================================
250
251   //=================================================================
252   // WxBlackBoxDescriptor declaration
253   BBTK_BEGIN_DESCRIBE_BLACK_BOX(WxBlackBox,bbtk::AtomicBlackBox);
254   BBTK_NAME("WxBlackBox");
255   // BBTK_DESCRIPTION("Widget box. The inputs marked with (*) are only used if the widget is not inserted in another widget.\n");
256   BBTK_CATEGORY("widget");
257   BBTK_INPUT(WxBlackBox,WinTitle,
258              "Title of the window (*)",
259              std::string,"");
260   BBTK_INPUT(WxBlackBox,WinWidth,
261              "Width of the window (* : only used if the widget is not connected to a Layout box)",int,"");
262   BBTK_INPUT(WxBlackBox,WinHeight,
263              "Height of the window (*)",int,"");
264   BBTK_INPUT(WxBlackBox,WinDialog,
265              "Set to 'true' to create a dialog window, i.e. which blocks the pipeline until it is closed (modal) (*)",bool,"");
266   BBTK_INPUT(WxBlackBox,WinHide,
267              "Any signal received hides the window (*)",Void,"signal");
268   BBTK_INPUT(WxBlackBox,WinClose,
269              "Any signal received closes the window (*)",Void,"signal");
270   BBTK_OUTPUT(WxBlackBox,Widget,"Output widget",wxWindow*,"");
271   BBTK_END_DESCRIBE_BLACK_BOX(WxBlackBox);
272   //=================================================================
273
274
275
276
277
278
279
280
281
282   //==================================================================
283   // The base of the hierarchy of windows associated to a WxBlackBox
284   class BBTK_EXPORT WxBlackBoxWindow //: public wxWindow
285   {
286   public:
287     WxBlackBoxWindow(WxBlackBox::Pointer box);
288     virtual ~WxBlackBoxWindow();
289     virtual void bbShow();
290     virtual void bbHide();
291     virtual void bbClose();
292     bool bbIsShown() { return mShown; }
293     virtual WxBlackBox::Pointer bbGetBlackBox() { return mBox.lock(); }
294     virtual wxDialog* bbGetDialog() { return 0; } 
295     virtual wxFrame* bbGetFrame() { return 0; } 
296   private:
297     WxBlackBox::WeakPointer mBox;
298     bool mShown;
299   };
300   //==================================================================
301
302   //==================================================================
303   // Dialog window which is modal
304   class BBTK_EXPORT  WxBlackBoxDialog : public wxDialog, public WxBlackBoxWindow
305   {
306   public:
307     WxBlackBoxDialog(WxBlackBox::Pointer box, 
308                      wxWindow *parent, wxString title, wxSize size);
309     ~WxBlackBoxDialog();
310     void bbShow();  
311     void bbHide();
312     void bbClose();
313     wxDialog* bbGetDialog() { return this; } 
314   };
315   //==================================================================
316
317   //==================================================================
318   // Frame window which is not modal
319   class BBTK_EXPORT  WxBlackBoxFrame : public wxFrame, public WxBlackBoxWindow
320   {
321   public:
322     WxBlackBoxFrame(WxBlackBox::Pointer box,
323                     wxWindow *parent, wxString title, wxSize size);
324     ~WxBlackBoxFrame();
325     void bbShow();
326     void bbHide();
327     void bbClose();
328     wxFrame* bbGetFrame() { return this; } 
329   };
330   //==================================================================
331
332
333
334   //=================================================================
335   // Handles the destroy events of a widget associated to a WxBlackBox 
336   // in order to signal the widget death to its associated box
337   class BBTK_EXPORT WxBlackBoxWidgetEventHandler : public wxEvtHandler
338   {
339   public:
340     /// Ctor with the box and widget 
341     WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box, wxWindow *widget );
342     /// Dtor
343     ~WxBlackBoxWidgetEventHandler();
344     /// Returns true iff is the handler for that window  
345     bool IsHandlerOf( wxWindow* w ) { return mWindow == w; }
346     // wxWindow* GetWxWindow() { return mWindow; }
347     /// Method processing the destroy event of the widget
348     void OnWindowDestroy(wxWindowDestroyEvent&);
349     //
350     //bool IsDead() { return mDead; }
351
352   private:
353     WxBlackBox::WeakPointer mBox;
354     wxWindow* mWindow;
355     //bool mDead;
356   };  
357   //=================================================================
358
359
360
361 } //namespace bbtk
362
363 #endif  //__bbtkWxBlackBox_h__
364
365 #endif  //_USE_WXWIDGETS_