]> 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: 2008/12/12 12:56:28 $
6   Version:   $Revision: 1.36 $
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   // WxBlackBoxWindow
61   //=========================================================================
62
63
64
65   //=========================================================================
66   WxBlackBoxWindow::WxBlackBoxWindow(WxBlackBox::Pointer box)
67     : mBox(box), mShown(false)
68   {
69     bbtkDebugMessage("wx",9,"WxBlackBoxWindow::WxBlackBoxWindow("<<
70                      mBox.lock()->bbGetFullName()<<")"<<std::endl);
71     mBox.lock()->bbSetWindow(this);
72     Wx::IncNbWindowsAlive();
73   }
74   //=========================================================================
75
76   //=========================================================================
77   WxBlackBoxWindow::~WxBlackBoxWindow()
78   {
79     bbtkDebugMessage("wx",9,"WxBlackBoxWindow::~WxBlackBoxWindow() "
80                      <<this<<std::endl);
81     bbHide();
82     Wx::DecNbWindowsAlive();
83     if (!mBox.expired())
84       {
85         mBox.lock()->bbSetWindow(0);
86       }
87   }
88   //========================================================================= 
89
90
91   //=========================================================================
92   void WxBlackBoxWindow::bbShow()
93   {
94     if (bbIsShown()) return;
95     bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbShow()"<<std::endl);
96     Wx::IncNbWindowsShown();
97     mShown = true;
98     if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
99   }
100   //=========================================================================
101
102   //=========================================================================
103   void WxBlackBoxWindow::bbHide()
104   {
105     if (!bbIsShown()) return;
106     bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbHide()"<<std::endl);
107     Wx::DecNbWindowsShown();
108     mShown = false;
109   }
110   //=========================================================================
111
112   //=========================================================================
113   void WxBlackBoxWindow::bbClose()
114   {
115   }
116   //=========================================================================
117
118
119   //=========================================================================
120   // WxBlackBoxDialog
121   //=========================================================================
122
123   //=========================================================================
124   WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
125                                      wxWindow *parent,
126                                      wxString title,
127                                      wxSize size)
128     :
129     wxDialog( parent, 
130                 -1, 
131                 title,
132                 wxDefaultPosition,
133                 size,
134                 wxRESIZE_BORDER | 
135                 wxSYSTEM_MENU  |
136                 wxCLOSE_BOX |
137                 wxMAXIMIZE_BOX | 
138                 wxMINIMIZE_BOX | 
139                 wxCAPTION  
140               ),
141     WxBlackBoxWindow(box)
142   {
143     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
144                      bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
145                      <<title<<",size)"<<std::endl);
146     // Insert the widget into the window
147     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
148           // LG 22/11/08 : new widget pipeline
149           bbGetBlackBox()->bbCreateWidgetAndEventHandler(this);
150     wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
151 // old :    widget->Reparent(this);
152     sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
153     //SetAutoLayout(true);
154     SetSizer(sizer);
155     Layout();
156   }
157   //=========================================================================
158   
159   //=========================================================================
160   void WxBlackBoxDialog::bbShow()
161   { 
162     if (bbIsShown()) return;
163     bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
164                      <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
165     WxBlackBoxWindow::bbShow();
166     SetReturnCode( wxDialog::ShowModal() ); 
167     bbClose();
168   }
169   //=========================================================================
170
171   //=========================================================================
172   void WxBlackBoxDialog::bbHide()
173   {
174     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
175     WxBlackBoxWindow::bbHide();
176     Hide();
177   }
178   //=========================================================================
179
180   //=========================================================================
181   void WxBlackBoxDialog::bbClose()
182   {
183     bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
184     wxDialog::Destroy();
185   }
186   //=========================================================================
187
188   //=========================================================================
189   WxBlackBoxDialog::~WxBlackBoxDialog()
190   {
191   }
192   //=========================================================================
193
194
195
196
197
198   //=========================================================================
199   // WxBlackBoxFrame
200   //=========================================================================
201
202   //=========================================================================
203   WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
204                                    wxWindow *parent,
205                                    wxString title,
206                                    wxSize size)
207     :  wxFrame( parent, 
208                -1, 
209                title,
210                wxDefaultPosition,
211                size,
212                wxRESIZE_BORDER | 
213                wxSYSTEM_MENU  |
214                wxCLOSE_BOX |
215                wxMAXIMIZE_BOX | 
216                wxMINIMIZE_BOX | 
217                wxCAPTION  
218                 ),
219        WxBlackBoxWindow(box)
220   {
221     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
222                      bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
223                      <<title<<",size)"<<std::endl);
224     // Insert the widget into the window
225     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
226           // LG 22/11/08 : new widget pipeline
227           bbGetBlackBox()->bbCreateWidgetAndEventHandler(this);
228     wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
229     wxFrame* frame = (wxFrame*)this;
230 // old :    widget->Reparent(frame);
231     sizer->Add( widget, 1, wxALL|wxGROW, 2);
232     //  frame->SetAutoLayout(true);
233     frame->SetSizer(sizer);
234     //frame->Fit();
235     frame->Layout();
236   }
237   //=========================================================================
238   
239   //=========================================================================
240   WxBlackBoxFrame::~WxBlackBoxFrame()
241   {
242   }
243   //=========================================================================
244
245   //=========================================================================
246   void WxBlackBoxFrame::bbShow() 
247   { 
248     if (bbIsShown()) return;
249     bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
250                      <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
251     WxBlackBoxWindow::bbShow();
252     wxFrame::Show();
253     // This Update is ** MANDATORY ** 
254     // to synchronize wxvtkRenderWindowInteractor objects
255     // (force wx objects creation **NOW**)
256     
257     
258 #if defined(_WIN32)
259     wxFrame::Refresh();
260 #endif
261     
262     wxFrame::Update();
263     wxFrame::SetFocus();
264     //    if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
265   }
266   //=========================================================================
267
268   //=========================================================================
269   void WxBlackBoxFrame::bbHide()
270   {
271     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
272     WxBlackBoxWindow::bbHide();
273     wxFrame::Hide();
274     if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
275   }
276   //=========================================================================
277
278   //=========================================================================
279   void WxBlackBoxFrame::bbClose()
280   {
281     bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
282     wxFrame::Close();
283   }
284   //=========================================================================
285
286
287
288   //=========================================================================
289   // WxBlackBoxWidgetEventHandler
290   //=========================================================================
291
292   //=========================================================================
293   WxBlackBoxWidgetEventHandler::
294   WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box, 
295                                 wxWindow *widget )
296     :
297     mBox(box),
298     mWindow(widget)
299   { 
300     bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
301
302     mBox.lock()->bbSetWidgetEventHandler(this);
303
304     Connect (  mWindow->GetId(),
305                wxEVT_DESTROY,
306                (wxObjectEventFunction) 
307                (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
308                 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
309     
310     mWindow->PushEventHandler(this);
311     
312   }
313   //=========================================================================
314
315   //=========================================================================
316   WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
317   {
318     if (mBox.expired()) return;
319     bbtkDebugMessage("wx",9,
320                      "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
321                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
322      mBox.lock()->bbSetWidgetEventHandler(0);   
323   }
324   //=========================================================================
325
326   //=========================================================================
327   void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
328   {
329     if (mBox.expired()) return;
330     bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
331                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
332     mBox.lock()->bbSetOutputWidget(0);
333     // LGSIGNAL
334     //    mBox.lock()->bbSetModifiedStatus();
335   }
336   //=========================================================================
337
338
339
340
341
342
343
344   //=========================================================================
345   // WxBlackBox
346   //=========================================================================
347
348   //=========================================================================
349   //=========================================================================
350   //=========================================================================
351   //=========================================================================
352   BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
353   //=========================================================================
354   
355   //=========================================================================
356   void WxBlackBox::bbUserConstructor()
357   {
358     bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
359     bbInitAttributes();
360   }
361   //=========================================================================
362
363   //=========================================================================
364   void WxBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
365   {
366     bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
367                      <<std::endl);
368     bbInitAttributes();
369   }
370   //=========================================================================
371
372
373   //=========================================================================
374   void WxBlackBox::bbUserDestructor()
375   {
376     bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
377     if (bbGetWindow()) {
378       delete bbGetWindow();
379       bbSetWindow(0);
380     }
381     bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
382   }
383   //=========================================================================
384   
385
386   //=========================================================================
387   /**
388    * \brief Initialize the attributes of the class
389    *
390    */
391   void WxBlackBox::bbInitAttributes()
392   {
393     bbmWindow = 0;
394     //    bbmWidget = 0;
395     //    bbSetInputWinParent(0);
396     bbSetInputWinTitle(bbGetName());
397     bbSetInputWinWidth(800);
398     bbSetInputWinHeight(800);
399     bbSetInputWinDialog(false);
400     bbSetOutputWidget(0);
401
402     bbSetWidgetEventHandler(0);
403     //    bbSetUpdateTransferedToParent(false);
404   }
405   //=========================================================================
406
407   //=========================================================================
408   void WxBlackBox::bbProcess()
409   { 
410     // If output widget not connected : have to create and show the window
411     if ( (*bbGetOutputConnectorMap().find("Widget")).second
412          ->GetConnectionVector().size() == 0 ) 
413       {
414         Window* show = 0;
415         // If the window already exists : no need creating it
416         if (bbGetWindow()!=0)
417           {
418             bbtkDebugMessage("wx",2,
419                              "-> Window already exists"
420                              <<std::endl);
421             show = bbGetWindow();
422           }
423         // Else create window 
424         else 
425           {
426             bbtkDebugMessage("wx",2,
427                              "-> Creating the window"
428                              <<std::endl);
429             
430             // Input WinDialog set to true : creating a Dialog
431             if (bbGetInputWinDialog()) 
432               {
433                 bbtkDebugMessage("wx",2,
434                                  "   Input WinDialog set to true : creating a Dialog"
435                                  <<std::endl);
436                 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
437                                                        Wx::GetTopWindow(),
438                                                        std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
439                                                        wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
440               }
441             // Input WinDialog set to false : creating a Frame
442             else 
443               {
444                 bbtkDebugMessage("process",2,
445                                  "   Input WinDialog set to false : creating a Frame"
446                                  <<std::endl);
447                 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
448                                                       Wx::GetTopWindow(),
449                                                       std2wx( bbGetInputWinTitle()  + " - bbtk (c) CREATIS LRMN"),
450                                                       wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
451               }
452             
453           }
454         
455         // Show the window
456         show->bbShow(); 
457         
458         
459       }           
460     this->bbUserProcess(); 
461     
462   }
463   //=========================================================================
464  
465         
466         
467   //=========================================================================
468   void WxBlackBox::bbCreateWidgetAndEventHandler(wxWindow* parent)
469   {
470     if (bbGetOutputWidget()==0)
471       {
472         this->bbUserCreateWidget(parent);
473       }         
474     // If Event Handler for the widget does not exist or is obsolete : create it 
475     if (bbGetOutputWidget()!=0)
476       {
477         if (bbGetWidgetEventHandler()==0)
478           {
479             bbtkDebugMessage("wx",3,
480                              "-> No widget event handler : creating one"
481                              <<std::endl);
482             new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
483                                              bbGetOutputWidget());
484           }
485         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
486                   ( bbGetOutputWidget() ) )
487           {
488             bbtkDebugMessage("wx",3,
489                              "-> Obsolete widget event handler : re-creating one"
490                              <<std::endl);
491             delete bbGetWidgetEventHandler();
492             new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
493                                              bbGetOutputWidget());
494           }
495         // Sets the name of the wxWindow to the input WinTitle
496         bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
497       }
498     
499     
500   }
501   //=========================================================================
502   
503   
504   //========================================================================= 
505   wxWindow*  WxBlackBox::bbCreateWidgetOfInput(const std::string& in, 
506                                                wxWindow* parent)
507   {
508     wxWindow* w = 0;
509     // If input is connected 
510     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
511     if ( c->IsConnected() )                     
512       {
513         // Get black box from 
514         BlackBox::Pointer from = 
515           c->GetConnection()->GetBlackBoxFrom();
516         // Cast it into a WxBlackBox
517         WxBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<WxBlackBox>(from);
518         // Call bbCreateWidgetAndEventHandler
519         wfrom->bbCreateWidgetAndEventHandler(parent);
520         // Get the widget created
521         w = wfrom->bbGetOutputWidget();
522       }
523     return w;
524   }
525   //==================================================================
526
527
528   //==================================================================
529    void WxBlackBox::bbHideWindow()
530   {
531     bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
532                         <<bbGetFullName()<<"]"<<std::endl);
533
534     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
535
536     bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbHideWindow() ["
537                         <<bbGetFullName()<<"]"<<std::endl);
538   }
539   //==================================================================
540
541
542   //==================================================================
543    void WxBlackBox::bbCloseWindow()
544   {
545     bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
546                         <<bbGetFullName()<<"]"<<std::endl);
547
548     if (bbGetWindow()!=0) bbGetWindow()->bbClose();
549
550     bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbCloseWindow() ["
551                         <<bbGetFullName()<<"]"<<std::endl);
552   }
553   //==================================================================
554
555   //==================================================================
556   WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
557   {
558     if (bbGetWindow()!=0) return bbGetWindow();
559     BlackBox::OutputConnectorMapType::const_iterator i 
560       = bbGetOutputConnectorMap().find("Widget");
561     if ( i->second->GetConnectionVector().size() != 0 ) 
562       {
563         return boost::static_pointer_cast<WxBlackBox>
564           (i->second->GetConnectionVector().front() //.lock()
565            ->GetBlackBoxTo())->bbGetContainingWindow();
566       }
567     return 0;
568   }
569   //==================================================================
570
571
572   //==================================================================
573   bool WxBlackBox::bbIsShown()
574   {
575     if (bbGetContainingWindow()!=0)
576       return bbGetContainingWindow()->bbIsShown();
577     return false;
578   }
579   //==================================================================
580
581
582 }//namespace bbtk
583
584
585 #endif
586