1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/06/26 18:45:58 $
7 Version: $Revision: 1.22 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #ifdef _USE_WXWIDGETS_
28 #include "bbtkWxBlackBox.h"
29 //#include "bbtkWxContainerBlackBox.h"
30 #include <wx/dialog.h>
32 //#include "bbtkData.h"
33 //#include "bbtkFactory.h"
44 //=========================================================================
46 //=========================================================================
50 //=========================================================================
51 WxBlackBoxWindow::WxBlackBoxWindow(WxBlackBox::Pointer box)
52 : mBox(box), mShown(false)
54 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::WxBlackBoxWindow("<<
55 mBox.lock()->bbGetFullName()<<")"<<std::endl);
56 mBox.lock()->bbSetWindow(this);
57 Wx::IncNbWindowsAlive();
59 //=========================================================================
61 //=========================================================================
62 WxBlackBoxWindow::~WxBlackBoxWindow()
64 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::~WxBlackBoxWindow() "
67 Wx::DecNbWindowsAlive();
70 mBox.lock()->bbSetWindow(0);
73 //=========================================================================
76 //=========================================================================
77 void WxBlackBoxWindow::bbShow()
79 if (bbIsShown()) return;
80 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbShow()"<<std::endl);
81 Wx::IncNbWindowsShown();
84 //=========================================================================
86 //=========================================================================
87 void WxBlackBoxWindow::bbHide()
89 if (!bbIsShown()) return;
90 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbHide()"<<std::endl);
91 Wx::DecNbWindowsShown();
94 //=========================================================================
96 //=========================================================================
97 void WxBlackBoxWindow::bbClose()
100 //=========================================================================
103 //=========================================================================
105 //=========================================================================
107 //=========================================================================
108 WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
113 WxBlackBoxWindow(box),
127 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
128 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
129 <<title<<",size)"<<std::endl);
130 // Insert the widget into the window
131 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
132 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
133 widget->Reparent(this);
134 sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
135 //SetAutoLayout(true);
139 //=========================================================================
141 //=========================================================================
142 void WxBlackBoxDialog::bbShow()
144 bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
145 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
146 WxBlackBoxWindow::bbShow();
147 SetReturnCode( wxDialog::ShowModal() );
150 //=========================================================================
152 //=========================================================================
153 void WxBlackBoxDialog::bbHide()
155 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
156 WxBlackBoxWindow::bbHide();
159 //=========================================================================
161 //=========================================================================
162 void WxBlackBoxDialog::bbClose()
166 //=========================================================================
168 //=========================================================================
169 WxBlackBoxDialog::~WxBlackBoxDialog()
172 //=========================================================================
178 //=========================================================================
180 //=========================================================================
182 //=========================================================================
183 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
187 : WxBlackBoxWindow(box),
201 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
202 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
203 <<title<<",size)"<<std::endl);
204 // Insert the widget into the window
205 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
206 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
207 wxFrame* frame = (wxFrame*)this;
208 widget->Reparent(frame);
209 sizer->Add( widget, 1, wxALL|wxGROW, 2);
210 // frame->SetAutoLayout(true);
211 frame->SetSizer(sizer);
215 //=========================================================================
217 //=========================================================================
218 WxBlackBoxFrame::~WxBlackBoxFrame()
221 //=========================================================================
223 //=========================================================================
224 void WxBlackBoxFrame::bbShow()
226 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
227 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
228 WxBlackBoxWindow::bbShow();
230 // This Update is ** MANDATORY **
231 // to synchronize wxvtkRenderWindowInteractor objects
232 // (force wx objects creation **NOW**)
241 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
243 //=========================================================================
245 //=========================================================================
246 void WxBlackBoxFrame::bbHide()
248 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
249 WxBlackBoxWindow::bbHide();
251 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
253 //=========================================================================
255 //=========================================================================
256 void WxBlackBoxFrame::bbClose()
260 //=========================================================================
264 //=========================================================================
265 // WxBlackBoxWidgetEventHandler
266 //=========================================================================
268 //=========================================================================
269 WxBlackBoxWidgetEventHandler::
270 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
276 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
278 mBox.lock()->bbSetWidgetEventHandler(this);
280 Connect ( mWindow->GetId(),
282 (wxObjectEventFunction)
283 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
284 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
286 mWindow->PushEventHandler(this);
289 //=========================================================================
291 //=========================================================================
292 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
294 if (mBox.expired()) return;
295 bbtkDebugMessage("wx",9,
296 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
297 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
298 mBox.lock()->bbSetWidgetEventHandler(0);
300 //=========================================================================
302 //=========================================================================
303 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
305 if (mBox.expired()) return;
306 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
307 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
308 mBox.lock()->bbSetOutputWidget(0);
309 mBox.lock()->bbSetModifiedStatus();
311 //=========================================================================
319 //=========================================================================
321 //=========================================================================
323 //=========================================================================
324 //=========================================================================
325 //=========================================================================
326 //=========================================================================
327 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
328 //=========================================================================
330 //=========================================================================
331 void WxBlackBox::bbUserConstructor()
333 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
336 //=========================================================================
338 //=========================================================================
339 void WxBlackBox::bbUserCopyConstructor()
341 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
345 //=========================================================================
348 //=========================================================================
349 void WxBlackBox::bbUserDestructor()
351 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
353 delete bbGetWindow();
356 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
358 //=========================================================================
362 //=========================================================================
363 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
365 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
367 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
368 bbGetFullName()<<"] : Widget is dead : deleting it"
370 delete bbGetOutputWidget();
371 bbSetOutputWidget(0);
373 return bbGetOutputWidget();
375 //=========================================================================
379 //=========================================================================
381 * \brief Initialize the attributes of the class
384 void WxBlackBox::bbInitAttributes()
388 // bbSetInputWinParent(0);
389 bbSetInputWinTitle(bbGetName());
390 bbSetInputWinWidth(800);
391 bbSetInputWinHeight(800);
392 bbSetInputWinDialog(false);
393 bbSetOutputWidget(0);
395 bbSetWidgetEventHandler(0);
396 bbSetUpdateTransferedToParent(false);
398 //=========================================================================
400 //=========================================================================
401 /// Main processing method of the box.
402 void WxBlackBox::bbExecute(bool force)
404 bbtkDebugMessageInc("process",2,
405 "=> WxBlackBox::bbExecute() ["
406 <<bbGetFullName()<<"]"<<std::endl);
408 // If the output 'Widget' is connected then
409 // we must execute the parent box
410 BlackBox::OutputConnectorMapType::const_iterator i
411 = bbGetOutputConnectorMap().find("Widget");
413 if ( i->second->GetConnectionVector().size() != 0 )
415 bbtkDebugMessage("process",3,
416 "-> Output 'Widget' connected : transfering execution to parent"
419 i->second->GetConnectionVector().front() //.lock()
420 ->GetBlackBoxTo()->bbExecute(force);
423 // else call 'standard' BlackBox execution method
426 BlackBox::bbExecute(force);
430 bbtkDebugMessageDec("process",2,
431 "<= WxBlackBox::bbExecute() ["
432 <<bbGetFullName()<<"]"<<std::endl);
434 //=========================================================================
437 //=========================================================================
438 /// Main processing method of the box.
439 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
441 bbtkDebugMessage("process",3,
442 "=> WxBlackBox::bbBackwardUpdate("
443 <<(caller?caller->GetFullName():"0")<<") ["
444 <<bbGetFullName()<<"]"<<std::endl);
445 // If the caller's box to is not the box to connected to the
447 if ( ! (( bbGetStatus() == MODIFIED ) ||
448 ( bbBoxProcessModeIsAlways() )) )
450 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
451 bbtkDebugMessage("process",3,
452 "<= WxBlackBox::bbBackwardUpdate("
453 <<(caller?caller->GetFullName():"0")<<") ["
454 <<bbGetFullName()<<"]"<<std::endl);
455 return bbGetStatus();
458 BlackBox::OutputConnectorMapType::const_iterator i
459 = bbGetOutputConnectorMap().find("Widget") ;
460 if ( i->second->GetConnectionVector().size() != 0 )
463 BlackBox::Pointer to =
464 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
468 bbtkDebugMessage("process",3,
469 "-> Output 'Widget' connected to '"
470 <<to->bbGetFullName()<<"' - caller->to = '"
471 <<caller->GetBlackBoxTo()->bbGetFullName()
477 bbtkDebugMessage("process",3,
478 "-> Output 'Widget' connected to '"
479 <<to->bbGetFullName()<<"'"
484 (caller->GetBlackBoxTo() != to))&&
485 (!bbGetUpdateTransferedToParent()))
487 bbtkDebugMessage("process",3,
488 " ... Transfering update order to parent"
491 bbSetUpdateTransferedToParent(true);
492 i->second->GetConnectionVector().front() //.lock()
493 ->GetBlackBoxTo()->bbExecute(false);
497 bbSetUpdateTransferedToParent(false);
498 bbtkDebugMessage("process",3,
499 " ... No need to transfer to parent"
505 // If the caller is not the connection to the output widget
506 // and the output 'Widget' is connected then
507 // we must execute the parent box
509 // (this is the role of the flag UpdateTransferedToParent)
511 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
516 // call 'standard' BlackBox execution method
517 if (!bbGetUpdateTransferedToParent())
519 AtomicBlackBox::bbBackwardUpdate(caller);
522 bbtkDebugMessageDec("process",3,
523 "<= WxBlackBox::bbBackwardUpdate() ["
524 <<bbGetFullName()<<"]"<<std::endl);
526 return bbGetStatus();
531 //=========================================================================
532 void WxBlackBox::bbProcess()
534 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
535 this->bbUserProcess();
537 // this->bbUserOnShow();
539 //=========================================================================
541 //==================================================================
542 /// Specific methods for window creation during pipeline execution
543 /// Shows the window associated to the box
544 /// (called after bbProcess during bbExecute)
545 void WxBlackBox::bbShowWindow()
547 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
548 <<bbGetFullName()<<"]"<<std::endl);
550 // If Event Handler for the widget does not exist or is obsolete : create it
551 if (bbGetOutputWidget()!=0)
553 if (bbGetWidgetEventHandler()==0)
555 bbtkDebugMessage("wx",3,
556 "-> No widget event handler : creating one"
558 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
559 bbGetOutputWidget());
561 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
562 ( bbGetOutputWidget() ) )
564 bbtkDebugMessage("wx",3,
565 "-> Obsolete widget event handler : re-creating one"
567 delete bbGetWidgetEventHandler();
568 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
569 bbGetOutputWidget());
571 // Sets the name of the wxWindow to the input WinTitle
572 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
575 // If the output 'Widget' is connected then it's gonna
576 // be captured by its parent window : nothing to do
577 if ( (*bbGetOutputConnectorMap().find("Widget")).second
578 ->GetConnectionVector().size() != 0 )
581 bbtkDebugMessage("wx",2,
582 "-> Output 'Widget' connected : nothing to do"
589 // If the window already exists : no need creating it
590 if (bbGetWindow()!=0)
592 bbtkDebugMessage("wx",2,
593 "-> Window already exists"
595 show = bbGetWindow();
597 // Else if the widget exists : create window
598 else if (bbGetOutputWidget()!=0)
600 bbtkDebugMessage("wx",2,
601 "-> Widget exists : creating the window"
605 // Input WinDialog set to true : creating a Dialog
606 if (bbGetInputWinDialog())
608 bbtkDebugMessage("wx",2,
609 " Input WinDialog set to true : creating a Dialog"
611 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
613 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
614 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
616 // Input WinDialog set to false : creating a Frame
619 bbtkDebugMessage("process",2,
620 " Input WinDialog set to false : creating a Frame"
622 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
624 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
625 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
629 // No window nor widget : error
632 bbtkError("WxBlackBox::bbShowWindow() ["
634 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
639 if (true) //!show->IsShown())
645 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
649 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
650 <<bbGetFullName()<<"]"<<std::endl);
653 //==================================================================
658 //==================================================================
659 void WxBlackBox::bbHideWindow()
661 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
662 <<bbGetFullName()<<"]"<<std::endl);
664 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
666 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
667 <<bbGetFullName()<<"]"<<std::endl);
669 //==================================================================
672 //==================================================================
673 void WxBlackBox::bbCloseWindow()
675 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
676 <<bbGetFullName()<<"]"<<std::endl);
678 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
680 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbCloseWindow() ["
681 <<bbGetFullName()<<"]"<<std::endl);
683 //==================================================================
685 //==================================================================
686 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
688 if (bbGetWindow()!=0) return bbGetWindow();
689 BlackBox::OutputConnectorMapType::const_iterator i
690 = bbGetOutputConnectorMap().find("Widget");
691 if ( i->second->GetConnectionVector().size() != 0 )
693 return boost::static_pointer_cast<WxBlackBox>
694 (i->second->GetConnectionVector().front() //.lock()
695 ->GetBlackBoxTo())->bbGetContainingWindow();
699 //==================================================================
702 //==================================================================
703 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
704 //==================================================================
707 //==================================================================
708 bool WxBlackBox::bbIsShown()
710 if (bbGetContainingWindow()!=0)
711 return bbGetContainingWindow()->bbIsShown();
714 //==================================================================