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