]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxBlackBox.h
ddd35779880b48ef216d4b6c9a2c8d43f55f3890
[bbtk.git] / kernel / src / bbtkWxBlackBox.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxBlackBox.h,v $
5   Language:  C++
6   Date:      $Date: 2008/05/15 08:02:36 $
7   Version:   $Revision: 1.16 $
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     typedef WxBlackBoxWindow Window;
77     
78     /// Returns the **OWN** window associated to the box
79     /// If 0 returned = no window
80     Window* bbGetWindow() { return bbmWindow; }
81
82     /// Returns the window containing the widget associated to the box
83     /// Which can be the own window of **ANOTHER** box in case of 
84     /// a hierarchy of widgets.
85     /// More precisely :
86     /// If bbGetWindow() != 0 then returns bbGetWindow()
87     /// Else if the output 'Widget' is connected 
88     ///  then returns bbGetContainingWindow() of the box connected to 'Widget'
89     /// Else returns 0;
90     Window* bbGetContainingWindow();
91
92     /// Returns the parent wxWindow that must be used to create the widget 
93     wxWindow* bbGetWxParent();
94
95     /// Returns true iff the 'containing window' exists and is shown 
96     /// (see bbGetContainingWindow).
97     bool bbIsShown();
98
99     //==================================================================    
100     /// User callback invoked when the containing window is shown
101     virtual void bbUserOnShow() {}
102     //==================================================================    
103  
104     //==================================================================    
105     /// User callback invoked when the containing window is hidden
106     virtual void bbUserOnHide() {}
107     //==================================================================    
108
109   protected:
110     
111   
112     //==================================================================
113     /// User callback called in the box contructor
114     virtual void bbUserConstructor();
115     /// User callback called in the box copy constructor
116     virtual void bbUserCopyConstructor();
117     /// User callback called in the box destructor
118     virtual void bbUserDestructor();
119     //==================================================================    
120
121     //==================================================================    
122     /// User callback for creating the widget associated to the box
123     /// ** Must be defined **
124     virtual void bbUserCreateWidget() 
125     {
126       bbtkError(bbGetTypeName()<<" is a WxBlackBox whose bbUserCreateWidget methods is not overloaded : is it a feature or a bug ?!?");
127     }
128     //==================================================================    
129
130
131     
132
133     //==================================================================
134     /// Main processing method of the box.
135     virtual IOStatus bbBackwardUpdate( Connection::Pointer caller );
136     //==================================================================
137
138
139
140     //==================================================================
141     /// Overloaded processing method for WxBlackBoxes :
142     /// 1) if the widget is null then 
143     ///    calls the user defined widget creation method : bbUserCreateWidget()
144     /// 2) calls the user defined processing method : bbUserProcess()
145     /// 3) displays the window : bbShowWindow();
146     virtual void bbProcess();
147     //==================================================================
148
149     //==================================================================
150     /// If necessary creates the WxBlackBoxWindow associated to the box
151     /// and shows it 
152     /// (does nothing if the box output 'Widget' is connected which 
153     /// means that the box does not have its own window but is contained 
154     /// into another window)
155     void bbShowWindow();
156     /// Hides the WxBlackBoxWindow associated to the box (if exists)
157     void bbHideWindow();
158     /// Closes (destroys) the WxBlackBoxWindow associated to the box (if exists)
159     void bbCloseWindow();
160     //==================================================================
161
162   private:
163     /// friendship
164     friend class WxBlackBoxWindow;
165     friend class WxBlackBoxWidgetEventHandler;
166
167     /// Sets the window
168     inline void bbSetWindow(Window* w) { bbmWindow=w; }
169
170     /// Sets the Widget Event Handler
171     inline void bbSetWidgetEventHandler(WxBlackBoxWidgetEventHandler* w) 
172     { bbmWidgetEventHandler = w; }
173     /// Gets the Widget Event Handler
174     inline WxBlackBoxWidgetEventHandler* bbGetWidgetEventHandler()
175     { return bbmWidgetEventHandler; }
176
177
178     /// The WxBlackBoxWindow associated to the box
179     Window* bbmWindow;
180     /// The WxBlackBoxWidgetEventHandler associated to the box
181     WxBlackBoxWidgetEventHandler* bbmWidgetEventHandler;
182
183
184     void bbInitAttributes();
185
186   protected :
187     /// 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 (the container window must be created and displayed - this box will be also executed by the normal pipeline recursion mechanism)
188     virtual void bbExecute(bool force = false);
189
190
191     /// Is set to true before transfering update to parent 
192     /// in order to not re-transfer a second time...
193     bool bbmUpdateTransferedToParent;
194
195     bool bbGetUpdateTransferedToParent() const { return bbmUpdateTransferedToParent; }
196     void bbSetUpdateTransferedToParent(bool b) 
197     { bbmUpdateTransferedToParent = b; }
198
199   };
200   //=================================================================
201  
202
203   //======================================================================
204   /// Defines the bbUserCreateWidget method
205 #define BBTK_CREATE_WIDGET(CALLBACK)                                    \
206   public:                                                               \
207   inline void bbUserCreateWidget()                                      \
208   {                                                                     \
209     bbtkDebugMessageInc("process",1,"**> Creating widget for ["         \
210                         <<bbGetFullName()<<"]"<<std::endl);             \
211     CALLBACK();                                                         \
212     bbtkDebugMessageInc("process",2,"<** Creating widget for ["         \
213                         <<bbGetFullName()<<"]"<<std::endl);             \
214   }
215   
216   //======================================================================
217
218   //=================================================================
219   // WxBlackBoxDescriptor declaration
220   BBTK_BEGIN_DESCRIBE_BLACK_BOX(WxBlackBox,bbtk::AtomicBlackBox);
221   BBTK_NAME("WxBlackBox");
222   // BBTK_DESCRIPTION("Widget box. The inputs marked with (*) are only used if the widget is not inserted in another widget.\n");
223   BBTK_CATEGORY("widget");
224   BBTK_INPUT(WxBlackBox,WinTitle,
225              "Title of the window (*)",
226              std::string,"");
227   BBTK_INPUT(WxBlackBox,WinWidth,
228              "Width of the window (* : only used if the widget is not connected to a Layout box)",int,"");
229   BBTK_INPUT(WxBlackBox,WinHeight,
230              "Height of the window (*)",int,"");
231   BBTK_INPUT(WxBlackBox,WinDialog,
232              "Set to 'true' to create a dialog window, i.e. which blocks the pipeline until it is closed (modal) (*)",bool,"");
233   BBTK_INPUT(WxBlackBox,WinHide,
234              "Any signal received hides the window (*)",Void,"signal");
235   BBTK_INPUT(WxBlackBox,WinClose,
236              "Any signal received closes the window (*)",Void,"signal");
237   BBTK_OUTPUT(WxBlackBox,Widget,"Output widget",wxWindow*,"");
238   BBTK_END_DESCRIBE_BLACK_BOX(WxBlackBox);
239   //=================================================================
240
241
242
243
244
245
246
247
248
249   //==================================================================
250   // The base of the hierarchy of windows associated to a WxBlackBox
251   class BBTK_EXPORT WxBlackBoxWindow //: public wxWindow
252   {
253   public:
254     WxBlackBoxWindow(WxBlackBox::Pointer box);
255     virtual ~WxBlackBoxWindow();
256     virtual void bbShow();
257     virtual void bbHide();
258     virtual void bbClose();
259     bool bbIsShown() { return mShown; }
260     virtual WxBlackBox::Pointer bbGetBlackBox() { return mBox.lock(); }
261     virtual wxDialog* bbGetDialog() { return 0; } 
262     virtual wxFrame* bbGetFrame() { return 0; } 
263   private:
264     WxBlackBox::WeakPointer mBox;
265     bool mShown;
266   };
267   //==================================================================
268
269   //==================================================================
270   // Dialog window which is modal
271   class BBTK_EXPORT  WxBlackBoxDialog : public wxDialog, public WxBlackBoxWindow
272   {
273   public:
274     WxBlackBoxDialog(WxBlackBox::Pointer box, 
275                      wxWindow *parent, wxString title, wxSize size);
276     ~WxBlackBoxDialog();
277     void bbShow();  
278     void bbHide();
279     void bbClose();
280     wxDialog* bbGetDialog() { return this; } 
281   };
282   //==================================================================
283
284   //==================================================================
285   // Frame window which is not modal
286   class BBTK_EXPORT  WxBlackBoxFrame : public wxFrame, public WxBlackBoxWindow
287   {
288   public:
289     WxBlackBoxFrame(WxBlackBox::Pointer box,
290                     wxWindow *parent, wxString title, wxSize size);
291     ~WxBlackBoxFrame();
292     void bbShow();
293     void bbHide();
294     void bbClose();
295     wxFrame* bbGetFrame() { return this; } 
296   };
297   //==================================================================
298
299
300
301   //=================================================================
302   // Handles the destroy events of a widget associated to a WxBlackBox 
303   // in order to signal the widget death to its associated box
304   class BBTK_EXPORT WxBlackBoxWidgetEventHandler : public wxEvtHandler
305   {
306   public:
307     /// Ctor with the box and widget 
308     WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box, wxWindow *widget );
309     /// Dtor
310     ~WxBlackBoxWidgetEventHandler();
311     /// Returns true iff is the handler for that window  
312     bool IsHandlerOf( wxWindow* w ) { return mWindow == w; }
313     // wxWindow* GetWxWindow() { return mWindow; }
314     /// Method processing the destroy event of the widget
315     void OnWindowDestroy(wxWindowDestroyEvent&);
316     //
317     //bool IsDead() { return mDead; }
318
319   private:
320     WxBlackBox::WeakPointer mBox;
321     wxWindow* mWindow;
322     //bool mDead;
323   };  
324   //=================================================================
325
326
327
328 } //namespace bbtk
329
330 #endif  //__bbtkWxBlackBox_h__
331
332 #endif  //_USE_WXWIDGETS_