]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxBlackBox.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxBlackBox.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxBlackBox.h,v $
4   Language:  C++
5   Date:      $Date: 2008/10/17 08:18:15 $
6   Version:   $Revision: 1.18 $
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("process",1,"**> Creating widget for ["         \
228                         <<bbGetFullName()<<"]"<<std::endl);             \
229     CALLBACK();                                                         \
230     bbtkDebugMessageInc("process",2,"<** Creating widget for ["         \
231                         <<bbGetFullName()<<"]"<<std::endl);             \
232   }
233   
234   //======================================================================
235
236   //=================================================================
237   // WxBlackBoxDescriptor declaration
238   BBTK_BEGIN_DESCRIBE_BLACK_BOX(WxBlackBox,bbtk::AtomicBlackBox);
239   BBTK_NAME("WxBlackBox");
240   // BBTK_DESCRIPTION("Widget box. The inputs marked with (*) are only used if the widget is not inserted in another widget.\n");
241   BBTK_CATEGORY("widget");
242   BBTK_INPUT(WxBlackBox,WinTitle,
243              "Title of the window (*)",
244              std::string,"");
245   BBTK_INPUT(WxBlackBox,WinWidth,
246              "Width of the window (* : only used if the widget is not connected to a Layout box)",int,"");
247   BBTK_INPUT(WxBlackBox,WinHeight,
248              "Height of the window (*)",int,"");
249   BBTK_INPUT(WxBlackBox,WinDialog,
250              "Set to 'true' to create a dialog window, i.e. which blocks the pipeline until it is closed (modal) (*)",bool,"");
251   BBTK_INPUT(WxBlackBox,WinHide,
252              "Any signal received hides the window (*)",Void,"signal");
253   BBTK_INPUT(WxBlackBox,WinClose,
254              "Any signal received closes the window (*)",Void,"signal");
255   BBTK_OUTPUT(WxBlackBox,Widget,"Output widget",wxWindow*,"");
256   BBTK_END_DESCRIBE_BLACK_BOX(WxBlackBox);
257   //=================================================================
258
259
260
261
262
263
264
265
266
267   //==================================================================
268   // The base of the hierarchy of windows associated to a WxBlackBox
269   class BBTK_EXPORT WxBlackBoxWindow //: public wxWindow
270   {
271   public:
272     WxBlackBoxWindow(WxBlackBox::Pointer box);
273     virtual ~WxBlackBoxWindow();
274     virtual void bbShow();
275     virtual void bbHide();
276     virtual void bbClose();
277     bool bbIsShown() { return mShown; }
278     virtual WxBlackBox::Pointer bbGetBlackBox() { return mBox.lock(); }
279     virtual wxDialog* bbGetDialog() { return 0; } 
280     virtual wxFrame* bbGetFrame() { return 0; } 
281   private:
282     WxBlackBox::WeakPointer mBox;
283     bool mShown;
284   };
285   //==================================================================
286
287   //==================================================================
288   // Dialog window which is modal
289   class BBTK_EXPORT  WxBlackBoxDialog : public wxDialog, public WxBlackBoxWindow
290   {
291   public:
292     WxBlackBoxDialog(WxBlackBox::Pointer box, 
293                      wxWindow *parent, wxString title, wxSize size);
294     ~WxBlackBoxDialog();
295     void bbShow();  
296     void bbHide();
297     void bbClose();
298     wxDialog* bbGetDialog() { return this; } 
299   };
300   //==================================================================
301
302   //==================================================================
303   // Frame window which is not modal
304   class BBTK_EXPORT  WxBlackBoxFrame : public wxFrame, public WxBlackBoxWindow
305   {
306   public:
307     WxBlackBoxFrame(WxBlackBox::Pointer box,
308                     wxWindow *parent, wxString title, wxSize size);
309     ~WxBlackBoxFrame();
310     void bbShow();
311     void bbHide();
312     void bbClose();
313     wxFrame* bbGetFrame() { return this; } 
314   };
315   //==================================================================
316
317
318
319   //=================================================================
320   // Handles the destroy events of a widget associated to a WxBlackBox 
321   // in order to signal the widget death to its associated box
322   class BBTK_EXPORT WxBlackBoxWidgetEventHandler : public wxEvtHandler
323   {
324   public:
325     /// Ctor with the box and widget 
326     WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box, wxWindow *widget );
327     /// Dtor
328     ~WxBlackBoxWidgetEventHandler();
329     /// Returns true iff is the handler for that window  
330     bool IsHandlerOf( wxWindow* w ) { return mWindow == w; }
331     // wxWindow* GetWxWindow() { return mWindow; }
332     /// Method processing the destroy event of the widget
333     void OnWindowDestroy(wxWindowDestroyEvent&);
334     //
335     //bool IsDead() { return mDead; }
336
337   private:
338     WxBlackBox::WeakPointer mBox;
339     wxWindow* mWindow;
340     //bool mDead;
341   };  
342   //=================================================================
343
344
345
346 } //namespace bbtk
347
348 #endif  //__bbtkWxBlackBox_h__
349
350 #endif  //_USE_WXWIDGETS_