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