]> 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/04/08 07:56:11 $
6   Version:   $Revision: 1.38 $
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     bbClose();
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::bbClose()
123   {
124     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
125     wxDialog::Destroy();
126   }
127   //=========================================================================
128
129   //=========================================================================
130   WxBlackBoxDialog::~WxBlackBoxDialog()
131   {
132   }
133   //=========================================================================
134
135
136
137
138
139   //=========================================================================
140   // WxBlackBoxFrame
141   //=========================================================================
142
143   //=========================================================================
144   WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
145                                    wxWindow *parent,
146                                    wxString title,
147                                    wxSize size)
148     :  wxFrame( parent, 
149                -1, 
150                title,
151                wxDefaultPosition,
152                size,
153                wxRESIZE_BORDER | 
154                wxSYSTEM_MENU  |
155                wxCLOSE_BOX |
156                wxMAXIMIZE_BOX | 
157                wxMINIMIZE_BOX | 
158                wxCAPTION  
159                 ),
160        Parent(box)
161   {
162     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
163                      bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
164                      <<title<<",size)"<<std::endl);
165     // Insert the widget into the window
166     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
167     box->bbCreateWidgetAndEventHandler(this);
168     wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
169     wxFrame* frame = (wxFrame*)this;
170     sizer->Add( widget, 1, wxALL|wxGROW, 2);
171     //  frame->SetAutoLayout(true);
172     frame->SetSizer(sizer);
173     //frame->Fit();
174     frame->Layout();
175   }
176   //=========================================================================
177   
178   //=========================================================================
179   WxBlackBoxFrame::~WxBlackBoxFrame()
180   {
181   }
182   //=========================================================================
183
184   //=========================================================================
185   void WxBlackBoxFrame::bbShow() 
186   { 
187     if (bbIsShown()) return;
188     bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
189                      <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
190     Parent::bbShow();
191     wxFrame::Show();
192     // This Update is ** MANDATORY ** 
193     // to synchronize wxvtkRenderWindowInteractor objects
194     // (force wx objects creation **NOW**)
195     
196     
197 #if defined(_WIN32)
198     wxFrame::Refresh();
199 #endif
200     
201     wxFrame::Update();
202     wxFrame::SetFocus();
203     //    if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
204   }
205   //=========================================================================
206
207   //=========================================================================
208   void WxBlackBoxFrame::bbHide()
209   {
210     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
211     Parent::bbHide();
212     wxFrame::Hide();
213     if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
214   }
215   //=========================================================================
216
217   //=========================================================================
218   void WxBlackBoxFrame::bbClose()
219   {
220     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
221     wxFrame::Close();
222   }
223   //=========================================================================
224
225
226
227   //=========================================================================
228   // WxBlackBoxWidgetEventHandler
229   //=========================================================================
230
231   //=========================================================================
232   WxBlackBoxWidgetEventHandler::
233   WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box, 
234                                 wxWindow *widget )
235     :
236     mBox(box),
237     mWindow(widget)
238   { 
239     bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
240
241     mBox.lock()->bbSetWidgetEventHandler(this);
242
243     Connect (  mWindow->GetId(),
244                wxEVT_DESTROY,
245                (wxObjectEventFunction) 
246                (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
247                 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
248     
249     mWindow->PushEventHandler(this);
250     
251   }
252   //=========================================================================
253
254   //=========================================================================
255   WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
256   {
257     if (mBox.expired()) return;
258     bbtkDebugMessage("wx",9,
259                      "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
260                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
261      mBox.lock()->bbSetWidgetEventHandler(0);   
262   }
263   //=========================================================================
264
265   //=========================================================================
266   void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
267   {
268     if (mBox.expired()) return;
269     bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
270                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
271     mBox.lock()->bbSetOutputWidget(0);
272     // LGSIGNAL
273     //    mBox.lock()->bbSetModifiedStatus();
274   }
275   //=========================================================================
276
277
278
279
280
281
282
283   //=========================================================================
284   // WxBlackBox
285   //=========================================================================
286
287   //=========================================================================
288   //=========================================================================
289   //=========================================================================
290   //=========================================================================
291   BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,WidgetBlackBox<wxWindow>);
292   //=========================================================================
293   
294   //=========================================================================
295   void WxBlackBox::bbUserConstructor()
296   {
297     bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
298     bbInitAttributes();
299   }
300   //=========================================================================
301
302   //=========================================================================
303   void WxBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
304   {
305     bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
306                      <<std::endl);
307     bbInitAttributes();
308   }
309   //=========================================================================
310
311
312
313   //=========================================================================
314   /// Initialize the attributes of the class
315   void WxBlackBox::bbInitAttributes()
316   {
317     bbSetWidgetEventHandler(0);
318   }
319   //=========================================================================
320
321   //==================================================================    
322   /// Callback for creating a Dialog window (modal)
323   void WxBlackBox::bbCreateDialogWindow()
324   {
325     WxBlackBoxDialog* w = 0;  
326     w = new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
327                               Wx::GetTopWindow(),
328                               std2wx( bbGetInputWinTitle() 
329                                       + " - bbtk (c) CREATIS LRMN"),
330                               wxSize( bbGetInputWinWidth() , 
331                                       bbGetInputWinHeight()  ));
332     w->Show(true);  
333   }
334   //==================================================================    
335
336   //==================================================================    
337   /// Callback for creating a Frame window 
338   void WxBlackBox::bbCreateFrameWindow()
339   {
340     WxBlackBoxFrame* w = 0;  
341     w = new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
342                              Wx::GetTopWindow(),
343                              std2wx( bbGetInputWinTitle()  
344                                      + " - bbtk (c) CREATIS LRMN"),
345                              wxSize( bbGetInputWinWidth() , 
346                                      bbGetInputWinHeight() ) );
347     w->Show();
348   }
349   //==================================================================    
350
351  
352   
353         
354   //=========================================================================
355   void WxBlackBox::bbCreateWidgetAndEventHandler(wxWindow* parent)
356   {
357     if (bbGetOutputWidget()==0)
358       {
359         this->bbUserCreateWidget(parent);
360       }         
361     // If Event Handler for the widget does not exist or is obsolete:
362     // create it 
363     if (bbGetOutputWidget()!=0)
364       {
365         if (bbGetWidgetEventHandler()==0)
366           {
367             bbtkDebugMessage("wx",3,
368                              "-> No widget event handler : creating one"
369                              <<std::endl);
370             new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
371                                              bbGetOutputWidget());
372           }
373         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
374                   ( bbGetOutputWidget() ) )
375           {
376             bbtkDebugMessage("wx",3,
377                              "-> Obsolete widget event handler : re-creating one"
378                              <<std::endl);
379             delete bbGetWidgetEventHandler();
380             new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
381                                              bbGetOutputWidget());
382           }
383         // Sets the name of the wxWindow to the input WinTitle
384         bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
385       }
386     
387     
388   }
389   //=========================================================================
390   
391   
392   //==================================================================
393   //  void WxBlackBox::InitWindowManagerIfNeeded() { Wx::
394   void WxBlackBox::IncNbWindowsAlive() { Wx::IncNbWindowsAlive(); }
395   void WxBlackBox::DecNbWindowsAlive() { Wx::DecNbWindowsAlive(); }
396   int  WxBlackBox::GetNbWindowsAlive() { return Wx::GetNbWindowsAlive(); }
397   bool WxBlackBox::IsSomeWindowAlive() { return Wx::IsSomeWindowAlive(); }
398   
399   void WxBlackBox::IncNbWindowsShown() { Wx::IncNbWindowsShown(); }
400   void WxBlackBox::DecNbWindowsShown() { Wx::DecNbWindowsShown(); }
401   int  WxBlackBox::GetNbWindowsShown() { return Wx::GetNbWindowsShown(); }
402   bool WxBlackBox::IsSomeWindowShown() { return Wx::GetNbWindowsShown(); }
403   //==================================================================
404   
405
406 }//namespace bbtk
407
408
409 #endif
410