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