]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxBlackBox.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxBlackBox.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxBlackBox.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/19 10:19:23 $
6   Version:   $Revision: 1.40 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31
32 #ifdef _USE_WXWIDGETS_
33
34
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40  
41 #include "bbtkWxBlackBox.h"
42 #include "bbtkBlackBoxOutputConnector.h"
43
44 //#include "bbtkWxContainerBlackBox.h"
45 #include <wx/dialog.h>
46
47 //#include "bbtkData.h"
48 //#include "bbtkFactory.h"
49
50
51
52
53 namespace bbtk
54 {
55
56
57
58
59
60   //=========================================================================
61   // WxBlackBoxDialog
62   //=========================================================================
63
64   //=========================================================================
65   WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
66                                      wxWindow *parent,
67                                      wxString title,
68                                      wxSize size)
69     :
70     wxDialog( parent, 
71                 -1, 
72                 title,
73                 wxDefaultPosition,
74                 size,
75                 wxRESIZE_BORDER | 
76                 wxSYSTEM_MENU  |
77                 wxCLOSE_BOX |
78                 wxMAXIMIZE_BOX | 
79                 wxMINIMIZE_BOX | 
80                 wxCAPTION  
81               ),
82     Parent(box)
83   {
84     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
85                      bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
86                      <<title<<",size)"<<std::endl);
87     // Insert the widget into the window
88     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
89     // Create the widget
90     box->bbCreateWidgetAndEventHandler(this);
91     wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
92     sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
93     //SetAutoLayout(true);
94     SetSizer(sizer);
95     Layout();
96   }
97   //=========================================================================
98   
99   //=========================================================================
100   void WxBlackBoxDialog::bbShow()
101   { 
102     if (bbIsShown()) return;
103     bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
104                      <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
105     Parent::bbShow();
106     Show(false);
107     SetReturnCode( wxDialog::ShowModal() ); 
108     bbDestroy();
109   }
110   //=========================================================================
111
112   //=========================================================================
113   void WxBlackBoxDialog::bbHide()
114   {
115     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
116     Parent::bbHide();
117     Hide();
118   }
119   //=========================================================================
120
121   //=========================================================================
122   void WxBlackBoxDialog::bbDestroy()
123   {
124     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbDestroy()"<<std::endl);
125     wxDialog::Destroy();
126   }
127   //=========================================================================
128
129   //=========================================================================
130   WxBlackBoxDialog::~WxBlackBoxDialog()
131   {
132     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::~WxBlackBoxDialog()"
133                      <<std::endl);
134   }
135   //=========================================================================
136
137
138
139
140
141   //=========================================================================
142   // WxBlackBoxFrame
143   //=========================================================================
144
145   //=========================================================================
146   WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
147                                    wxWindow *parent,
148                                    wxString title,
149                                    wxSize size)
150     :  wxFrame( parent, 
151                -1, 
152                title,
153                wxDefaultPosition,
154                size,
155                wxRESIZE_BORDER | 
156                wxSYSTEM_MENU  |
157                wxCLOSE_BOX |
158                wxMAXIMIZE_BOX | 
159                wxMINIMIZE_BOX | 
160                wxCAPTION  
161                 ),
162        Parent(box)
163   {
164     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
165                      bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
166                      <<title<<",size)"<<std::endl);
167     // Insert the widget into the window
168     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
169     box->bbCreateWidgetAndEventHandler(this);
170     wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
171     wxFrame* frame = (wxFrame*)this;
172     sizer->Add( widget, 1, wxALL|wxGROW, 2);
173     //  frame->SetAutoLayout(true);
174     frame->SetSizer(sizer);
175     //frame->Fit();
176     frame->Layout();
177   }
178   //=========================================================================
179   
180   //=========================================================================
181   WxBlackBoxFrame::~WxBlackBoxFrame()
182   {
183     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::~WxBlackBoxFrame()"
184                      <<std::endl);
185   }
186   //=========================================================================
187
188   //=========================================================================
189   void WxBlackBoxFrame::bbShow() 
190   { 
191     if (bbIsShown()) return;
192     bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
193                      <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
194     Parent::bbShow();
195     wxFrame::Show();
196     // This Update is ** MANDATORY ** 
197     // to synchronize wxvtkRenderWindowInteractor objects
198     // (force wx objects creation **NOW**)
199     
200     
201 #if defined(_WIN32)
202     wxFrame::Refresh();
203 #endif
204     
205     wxFrame::Update();
206     wxFrame::SetFocus();
207     //    if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
208   }
209   //=========================================================================
210
211   //=========================================================================
212   void WxBlackBoxFrame::bbHide()
213   {
214     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
215     Parent::bbHide();
216     wxFrame::Hide();
217     if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
218   }
219   //=========================================================================
220
221   //=========================================================================
222   void WxBlackBoxFrame::bbDestroy()
223   {
224     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbDestroy()"<<std::endl);
225     wxFrame::Destroy();
226   }
227   //=========================================================================
228
229
230
231   //=========================================================================
232   // WxBlackBoxWidgetEventHandler
233   //=========================================================================
234
235   //=========================================================================
236   WxBlackBoxWidgetEventHandler::
237   WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box, 
238                                 wxWindow *widget )
239     :
240     mBox(box),
241     mWindow(widget)
242   { 
243     bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
244
245     mBox.lock()->bbSetWidgetEventHandler(this);
246
247     Connect (  mWindow->GetId(),
248                wxEVT_DESTROY,
249                (wxObjectEventFunction) 
250                (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
251                 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
252     
253     mWindow->PushEventHandler(this);
254     
255   }
256   //=========================================================================
257
258   //=========================================================================
259   WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
260   {
261   bbtkDebugMessage("wx",9,
262                      "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
263                 <<std::endl);
264
265
266     Disconnect (  mWindow->GetId() );
267
268     if (mBox.expired()) return;
269     bbtkDebugMessage("wx",9,
270                      "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
271                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
272
273     mBox.lock()->bbSetWidgetEventHandler(0);   
274   }
275   //=========================================================================
276
277   //=========================================================================
278   void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
279   {
280    bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() []"<<std::endl);
281     if (mBox.expired()) return;
282     bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() alive ["
283                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
284     mBox.lock()->bbSetOutputWidget(0);
285   }
286   //=========================================================================
287
288
289
290
291
292
293
294   //=========================================================================
295   // WxBlackBox
296   //=========================================================================
297
298   //=========================================================================
299   //=========================================================================
300   //=========================================================================
301   //=========================================================================
302   BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,WidgetBlackBox<wxWindow>);
303   //=========================================================================
304   
305   //=========================================================================
306   void WxBlackBox::bbUserSetDefaultValues()
307   {
308     bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserSetDefaultValues()"<<std::endl);
309     bbSetWidgetEventHandler(0);
310   }
311   //=========================================================================
312
313   //=========================================================================
314   void WxBlackBox::bbUserInitializeProcessing()
315   {
316   }
317   //==================================================================    
318
319   //=========================================================================
320   void WxBlackBox::bbUserFinalizeProcessing()
321   {
322     bbtkDebugMessage("wx",9,"WxBlackBox::bbUserFinalizeProcessing()"<<std::endl);
323     if (bbGetWidgetEventHandler()!=0)
324       {
325         bbtkDebugMessage("wx",9,"Deleting Widget Event Handler"<<std::endl);
326         delete bbGetWidgetEventHandler();
327       }
328     
329   }
330   //==================================================================    
331
332   //==================================================================    
333   /// Callback for creating a Dialog window (modal)
334   void WxBlackBox::bbCreateDialogWindow()
335   {
336     WxBlackBoxDialog* w = 0;  
337     w = new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
338                               Wx::GetTopWindow(),
339                               std2wx( bbGetInputWinTitle() 
340                                       + " - bbtk (c) CREATIS LRMN"),
341                               wxSize( bbGetInputWinWidth() , 
342                                       bbGetInputWinHeight()  ));
343     w->Show(true);  
344   }
345   //==================================================================    
346
347   //==================================================================    
348   /// Callback for creating a Frame window 
349   void WxBlackBox::bbCreateFrameWindow()
350   {
351     WxBlackBoxFrame* w = 0;  
352     w = new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
353                              Wx::GetTopWindow(),
354                              std2wx( bbGetInputWinTitle()  
355                                      + " - bbtk (c) CREATIS LRMN"),
356                              wxSize( bbGetInputWinWidth() , 
357                                      bbGetInputWinHeight() ) );
358     w->Show();
359   }
360   //==================================================================    
361
362  
363   
364         
365   //=========================================================================
366   void WxBlackBox::bbCreateWidgetAndEventHandler(wxWindow* parent)
367   {
368     if (bbGetOutputWidget()==0)
369       {
370         this->bbUserCreateWidget(parent);
371       }         
372     // If Event Handler for the widget does not exist or is obsolete:
373     // create it 
374     if (bbGetOutputWidget()!=0)
375       {
376         if (bbGetWidgetEventHandler()==0)
377           {
378             bbtkDebugMessage("wx",3,
379                              "-> No widget event handler : creating one"
380                              <<std::endl);
381             new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
382                                              bbGetOutputWidget());
383           }
384         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
385                   ( bbGetOutputWidget() ) )
386           {
387             bbtkDebugMessage("wx",3,
388                              "-> Obsolete widget event handler : re-creating one"
389                              <<std::endl);
390             delete bbGetWidgetEventHandler();
391             new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
392                                              bbGetOutputWidget());
393           }
394         // Sets the name of the wxWindow to the input WinTitle
395         bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
396       }
397     
398     
399   }
400   //=========================================================================
401   
402   
403   //==================================================================
404   //  void WxBlackBox::InitWindowManagerIfNeeded() { Wx::
405   void WxBlackBox::IncNbWindowsAlive() { Wx::IncNbWindowsAlive(); }
406   void WxBlackBox::DecNbWindowsAlive() { Wx::DecNbWindowsAlive(); }
407   int  WxBlackBox::GetNbWindowsAlive() { return Wx::GetNbWindowsAlive(); }
408   bool WxBlackBox::IsSomeWindowAlive() { return Wx::IsSomeWindowAlive(); }
409   
410   void WxBlackBox::IncNbWindowsShown() { Wx::IncNbWindowsShown(); }
411   void WxBlackBox::DecNbWindowsShown() { Wx::DecNbWindowsShown(); }
412   int  WxBlackBox::GetNbWindowsShown() { return Wx::GetNbWindowsShown(); }
413   bool WxBlackBox::IsSomeWindowShown() { return Wx::GetNbWindowsShown(); }
414   //==================================================================
415   
416
417 }//namespace bbtk
418
419
420 #endif
421