1 /*=========================================================================
3 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
5 Date: $Date: 2008/11/17 10:00:24 $
6 Version: $Revision: 1.29 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
32 #ifdef _USE_WXWIDGETS_
41 #include "bbtkWxBlackBox.h"
42 //#include "bbtkWxContainerBlackBox.h"
43 #include <wx/dialog.h>
45 //#include "bbtkData.h"
46 //#include "bbtkFactory.h"
57 //=========================================================================
59 //=========================================================================
63 //=========================================================================
64 WxBlackBoxWindow::WxBlackBoxWindow(WxBlackBox::Pointer box)
65 : mBox(box), mShown(false)
67 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::WxBlackBoxWindow("<<
68 mBox.lock()->bbGetFullName()<<")"<<std::endl);
69 mBox.lock()->bbSetWindow(this);
70 Wx::IncNbWindowsAlive();
72 //=========================================================================
74 //=========================================================================
75 WxBlackBoxWindow::~WxBlackBoxWindow()
77 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::~WxBlackBoxWindow() "
80 Wx::DecNbWindowsAlive();
83 mBox.lock()->bbSetWindow(0);
86 //=========================================================================
89 //=========================================================================
90 void WxBlackBoxWindow::bbShow()
92 if (bbIsShown()) return;
93 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbShow()"<<std::endl);
94 Wx::IncNbWindowsShown();
97 //=========================================================================
99 //=========================================================================
100 void WxBlackBoxWindow::bbHide()
102 if (!bbIsShown()) return;
103 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbHide()"<<std::endl);
104 Wx::DecNbWindowsShown();
107 //=========================================================================
109 //=========================================================================
110 void WxBlackBoxWindow::bbClose()
113 //=========================================================================
116 //=========================================================================
118 //=========================================================================
120 //=========================================================================
121 WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
138 WxBlackBoxWindow(box)
140 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
141 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
142 <<title<<",size)"<<std::endl);
143 // Insert the widget into the window
144 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
145 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
146 widget->Reparent(this);
147 sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
148 //SetAutoLayout(true);
152 //=========================================================================
154 //=========================================================================
155 void WxBlackBoxDialog::bbShow()
157 if (bbIsShown()) return;
158 bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
159 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
160 WxBlackBoxWindow::bbShow();
161 SetReturnCode( wxDialog::ShowModal() );
164 //=========================================================================
166 //=========================================================================
167 void WxBlackBoxDialog::bbHide()
169 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
170 WxBlackBoxWindow::bbHide();
173 //=========================================================================
175 //=========================================================================
176 void WxBlackBoxDialog::bbClose()
178 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
181 //=========================================================================
183 //=========================================================================
184 WxBlackBoxDialog::~WxBlackBoxDialog()
187 //=========================================================================
193 //=========================================================================
195 //=========================================================================
197 //=========================================================================
198 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
214 WxBlackBoxWindow(box)
216 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
217 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
218 <<title<<",size)"<<std::endl);
219 // Insert the widget into the window
220 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
221 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
222 wxFrame* frame = (wxFrame*)this;
223 widget->Reparent(frame);
224 sizer->Add( widget, 1, wxALL|wxGROW, 2);
225 // frame->SetAutoLayout(true);
226 frame->SetSizer(sizer);
230 //=========================================================================
232 //=========================================================================
233 WxBlackBoxFrame::~WxBlackBoxFrame()
236 //=========================================================================
238 //=========================================================================
239 void WxBlackBoxFrame::bbShow()
241 if (bbIsShown()) return;
242 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
243 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
244 WxBlackBoxWindow::bbShow();
246 // This Update is ** MANDATORY **
247 // to synchronize wxvtkRenderWindowInteractor objects
248 // (force wx objects creation **NOW**)
257 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
259 //=========================================================================
261 //=========================================================================
262 void WxBlackBoxFrame::bbHide()
264 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
265 WxBlackBoxWindow::bbHide();
267 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
269 //=========================================================================
271 //=========================================================================
272 void WxBlackBoxFrame::bbClose()
274 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
277 //=========================================================================
281 //=========================================================================
282 // WxBlackBoxWidgetEventHandler
283 //=========================================================================
285 //=========================================================================
286 WxBlackBoxWidgetEventHandler::
287 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
293 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
295 mBox.lock()->bbSetWidgetEventHandler(this);
297 Connect ( mWindow->GetId(),
299 (wxObjectEventFunction)
300 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
301 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
303 mWindow->PushEventHandler(this);
306 //=========================================================================
308 //=========================================================================
309 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
311 if (mBox.expired()) return;
312 bbtkDebugMessage("wx",9,
313 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
314 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
315 mBox.lock()->bbSetWidgetEventHandler(0);
317 //=========================================================================
319 //=========================================================================
320 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
322 if (mBox.expired()) return;
323 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
324 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
325 mBox.lock()->bbSetOutputWidget(0);
326 mBox.lock()->bbSetModifiedStatus();
328 //=========================================================================
336 //=========================================================================
338 //=========================================================================
340 //=========================================================================
341 //=========================================================================
342 //=========================================================================
343 //=========================================================================
344 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
345 //=========================================================================
347 //=========================================================================
348 void WxBlackBox::bbUserConstructor()
350 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
353 //=========================================================================
355 //=========================================================================
356 void WxBlackBox::bbUserCopyConstructor()
358 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
362 //=========================================================================
365 //=========================================================================
366 void WxBlackBox::bbUserDestructor()
368 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
370 delete bbGetWindow();
373 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
375 //=========================================================================
379 //=========================================================================
380 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
382 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
384 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
385 bbGetFullName()<<"] : Widget is dead : deleting it"
387 delete bbGetOutputWidget();
388 bbSetOutputWidget(0);
390 return bbGetOutputWidget();
392 //=========================================================================
396 //=========================================================================
398 * \brief Initialize the attributes of the class
401 void WxBlackBox::bbInitAttributes()
405 // bbSetInputWinParent(0);
406 bbSetInputWinTitle(bbGetName());
407 bbSetInputWinWidth(800);
408 bbSetInputWinHeight(800);
409 bbSetInputWinDialog(false);
410 bbSetOutputWidget(0);
412 bbSetWidgetEventHandler(0);
413 bbSetUpdateTransferedToParent(false);
415 //=========================================================================
417 //=========================================================================
418 /// Main processing method of the box.
419 void WxBlackBox::bbExecute(bool force)
421 bbtkDebugMessageInc("process",2,
422 "=> WxBlackBox::bbExecute("<<(int)force<<") ["
423 <<bbGetFullName()<<"]"<<std::endl);
425 // If the output 'Widget' is connected then
426 // we must execute the parent box
427 BlackBox::OutputConnectorMapType::const_iterator i
428 = bbGetOutputConnectorMap().find("Widget");
430 if ( i->second->GetConnectionVector().size() != 0 )
432 bbtkDebugMessage("process",3,
433 "-> Output 'Widget' connected : transfering execution to parent"
436 i->second->GetConnectionVector().front() //.lock()
437 ->GetBlackBoxTo()->bbExecute(force);
440 // else call 'standard' BlackBox execution method
443 BlackBox::bbExecute(force);
447 bbtkDebugMessageDec("process",2,
448 "<= WxBlackBox::bbExecute() ["
449 <<bbGetFullName()<<"]"<<std::endl);
451 //=========================================================================
454 //=========================================================================
455 /// Main processing method of the box.
456 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
458 bbtkDebugMessage("process",3,
459 "=> WxBlackBox::bbBackwardUpdate("
460 <<(caller?caller->GetFullName():"0")<<") ["
461 <<bbGetFullName()<<"]"<<std::endl);
463 if ( ! (( bbGetStatus() == MODIFIED ) ||
464 ( bbBoxProcessModeIsAlways() )) )
466 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
467 bbtkDebugMessage("process",3,
468 "<= WxBlackBox::bbBackwardUpdate("
469 <<(caller?caller->GetFullName():"0")<<") ["
470 <<bbGetFullName()<<"]"<<std::endl);
471 return bbGetStatus();
474 // If the caller's box to is not the box to connected to the
477 BlackBox::OutputConnectorMapType::const_iterator i
478 = bbGetOutputConnectorMap().find("Widget") ;
479 if ( i->second->GetConnectionVector().size() != 0 )
482 BlackBox::Pointer to =
483 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
487 bbtkDebugMessage("process",3,
488 "-> Output 'Widget' connected to '"
489 <<to->bbGetFullName()<<"' - caller->to = '"
490 <<caller->GetBlackBoxTo()->bbGetFullName()
496 bbtkDebugMessage("process",3,
497 "-> Output 'Widget' connected to '"
498 <<to->bbGetFullName()<<"'"
503 (caller->GetBlackBoxTo() != to)&&
504 (!bbGetUpdateTransferedToParent())&&
505 (!to->bbGetExecuting())
509 bbtkDebugMessage("process",3,
510 " ... Transfering update order to parent"
513 bbSetUpdateTransferedToParent(true);
514 i->second->GetConnectionVector().front() //.lock()
515 ->GetBlackBoxTo()->bbExecute(false);
519 bbSetUpdateTransferedToParent(false);
520 bbtkDebugMessage("process",3,
521 " ... No need to transfer to parent"
527 // If the caller is not the connection to the output widget
528 // and the output 'Widget' is connected then
529 // we must execute the parent box
531 // (this is the role of the flag UpdateTransferedToParent)
533 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
538 // call 'standard' BlackBox execution method
539 if (!bbGetUpdateTransferedToParent())
541 AtomicBlackBox::bbBackwardUpdate(caller);
544 bbtkDebugMessageDec("process",3,
545 "<= WxBlackBox::bbBackwardUpdate() ["
546 <<bbGetFullName()<<"]"<<std::endl);
548 return bbGetStatus();
553 //=========================================================================
554 void WxBlackBox::bbProcess()
556 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
557 this->bbUserProcess();
559 // this->bbUserOnShow();
561 //=========================================================================
563 //==================================================================
564 /// Specific methods for window creation during pipeline execution
565 /// Shows the window associated to the box
566 /// (called after bbProcess during bbExecute)
567 void WxBlackBox::bbShowWindow()
569 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
570 <<bbGetFullName()<<"]"<<std::endl);
572 // If Event Handler for the widget does not exist or is obsolete : create it
573 if (bbGetOutputWidget()!=0)
575 if (bbGetWidgetEventHandler()==0)
577 bbtkDebugMessage("wx",3,
578 "-> No widget event handler : creating one"
580 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
581 bbGetOutputWidget());
583 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
584 ( bbGetOutputWidget() ) )
586 bbtkDebugMessage("wx",3,
587 "-> Obsolete widget event handler : re-creating one"
589 delete bbGetWidgetEventHandler();
590 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
591 bbGetOutputWidget());
593 // Sets the name of the wxWindow to the input WinTitle
594 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
597 // If the output 'Widget' is connected then it's gonna
598 // be captured by its parent window : nothing to do
599 if ( (*bbGetOutputConnectorMap().find("Widget")).second
600 ->GetConnectionVector().size() != 0 )
603 bbtkDebugMessage("wx",2,
604 "-> Output 'Widget' connected : nothing to do"
611 // If the window already exists : no need creating it
612 if (bbGetWindow()!=0)
614 bbtkDebugMessage("wx",2,
615 "-> Window already exists"
617 show = bbGetWindow();
619 // Else if the widget exists : create window
620 else if (bbGetOutputWidget()!=0)
622 bbtkDebugMessage("wx",2,
623 "-> Widget exists : creating the window"
627 // Input WinDialog set to true : creating a Dialog
628 if (bbGetInputWinDialog())
630 bbtkDebugMessage("wx",2,
631 " Input WinDialog set to true : creating a Dialog"
633 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
635 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
636 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
638 // Input WinDialog set to false : creating a Frame
641 bbtkDebugMessage("process",2,
642 " Input WinDialog set to false : creating a Frame"
644 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
646 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
647 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
651 // No window nor widget : error
654 bbtkError("WxBlackBox::bbShowWindow() ["
656 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
661 if (true) //!show->IsShown())
667 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
671 bbtkDebugMessage("wx",2,"<= WxBlackBox::bbShowWindow() ["
672 <<bbGetFullName()<<"]"<<std::endl);
675 //==================================================================
680 //==================================================================
681 void WxBlackBox::bbHideWindow()
683 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
684 <<bbGetFullName()<<"]"<<std::endl);
686 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
688 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbHideWindow() ["
689 <<bbGetFullName()<<"]"<<std::endl);
691 //==================================================================
694 //==================================================================
695 void WxBlackBox::bbCloseWindow()
697 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
698 <<bbGetFullName()<<"]"<<std::endl);
700 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
702 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbCloseWindow() ["
703 <<bbGetFullName()<<"]"<<std::endl);
705 //==================================================================
707 //==================================================================
708 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
710 if (bbGetWindow()!=0) return bbGetWindow();
711 BlackBox::OutputConnectorMapType::const_iterator i
712 = bbGetOutputConnectorMap().find("Widget");
713 if ( i->second->GetConnectionVector().size() != 0 )
715 return boost::static_pointer_cast<WxBlackBox>
716 (i->second->GetConnectionVector().front() //.lock()
717 ->GetBlackBoxTo())->bbGetContainingWindow();
721 //==================================================================
724 //==================================================================
725 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
726 //==================================================================
729 //==================================================================
730 bool WxBlackBox::bbIsShown()
732 if (bbGetContainingWindow()!=0)
733 return bbGetContainingWindow()->bbIsShown();
736 //==================================================================