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