1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/06/27 08:12:46 $
7 Version: $Revision: 1.23 $
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())&&
486 (to->bbGetStatus()!=UPDATING))
488 bbtkDebugMessage("process",3,
489 " ... Transfering update order to parent"
492 bbSetUpdateTransferedToParent(true);
493 i->second->GetConnectionVector().front() //.lock()
494 ->GetBlackBoxTo()->bbExecute(false);
498 bbSetUpdateTransferedToParent(false);
499 bbtkDebugMessage("process",3,
500 " ... No need to transfer to parent"
506 // If the caller is not the connection to the output widget
507 // and the output 'Widget' is connected then
508 // we must execute the parent box
510 // (this is the role of the flag UpdateTransferedToParent)
512 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
517 // call 'standard' BlackBox execution method
518 if (!bbGetUpdateTransferedToParent())
520 AtomicBlackBox::bbBackwardUpdate(caller);
523 bbtkDebugMessageDec("process",3,
524 "<= WxBlackBox::bbBackwardUpdate() ["
525 <<bbGetFullName()<<"]"<<std::endl);
527 return bbGetStatus();
532 //=========================================================================
533 void WxBlackBox::bbProcess()
535 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
536 this->bbUserProcess();
538 // this->bbUserOnShow();
540 //=========================================================================
542 //==================================================================
543 /// Specific methods for window creation during pipeline execution
544 /// Shows the window associated to the box
545 /// (called after bbProcess during bbExecute)
546 void WxBlackBox::bbShowWindow()
548 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
549 <<bbGetFullName()<<"]"<<std::endl);
551 // If Event Handler for the widget does not exist or is obsolete : create it
552 if (bbGetOutputWidget()!=0)
554 if (bbGetWidgetEventHandler()==0)
556 bbtkDebugMessage("wx",3,
557 "-> No widget event handler : creating one"
559 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
560 bbGetOutputWidget());
562 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
563 ( bbGetOutputWidget() ) )
565 bbtkDebugMessage("wx",3,
566 "-> Obsolete widget event handler : re-creating one"
568 delete bbGetWidgetEventHandler();
569 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
570 bbGetOutputWidget());
572 // Sets the name of the wxWindow to the input WinTitle
573 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
576 // If the output 'Widget' is connected then it's gonna
577 // be captured by its parent window : nothing to do
578 if ( (*bbGetOutputConnectorMap().find("Widget")).second
579 ->GetConnectionVector().size() != 0 )
582 bbtkDebugMessage("wx",2,
583 "-> Output 'Widget' connected : nothing to do"
590 // If the window already exists : no need creating it
591 if (bbGetWindow()!=0)
593 bbtkDebugMessage("wx",2,
594 "-> Window already exists"
596 show = bbGetWindow();
598 // Else if the widget exists : create window
599 else if (bbGetOutputWidget()!=0)
601 bbtkDebugMessage("wx",2,
602 "-> Widget exists : creating the window"
606 // Input WinDialog set to true : creating a Dialog
607 if (bbGetInputWinDialog())
609 bbtkDebugMessage("wx",2,
610 " Input WinDialog set to true : creating a Dialog"
612 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
614 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
615 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
617 // Input WinDialog set to false : creating a Frame
620 bbtkDebugMessage("process",2,
621 " Input WinDialog set to false : creating a Frame"
623 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
625 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
626 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
630 // No window nor widget : error
633 bbtkError("WxBlackBox::bbShowWindow() ["
635 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
640 if (true) //!show->IsShown())
646 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
650 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
651 <<bbGetFullName()<<"]"<<std::endl);
654 //==================================================================
659 //==================================================================
660 void WxBlackBox::bbHideWindow()
662 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
663 <<bbGetFullName()<<"]"<<std::endl);
665 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
667 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
668 <<bbGetFullName()<<"]"<<std::endl);
670 //==================================================================
673 //==================================================================
674 void WxBlackBox::bbCloseWindow()
676 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
677 <<bbGetFullName()<<"]"<<std::endl);
679 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
681 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbCloseWindow() ["
682 <<bbGetFullName()<<"]"<<std::endl);
684 //==================================================================
686 //==================================================================
687 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
689 if (bbGetWindow()!=0) return bbGetWindow();
690 BlackBox::OutputConnectorMapType::const_iterator i
691 = bbGetOutputConnectorMap().find("Widget");
692 if ( i->second->GetConnectionVector().size() != 0 )
694 return boost::static_pointer_cast<WxBlackBox>
695 (i->second->GetConnectionVector().front() //.lock()
696 ->GetBlackBoxTo())->bbGetContainingWindow();
700 //==================================================================
703 //==================================================================
704 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
705 //==================================================================
708 //==================================================================
709 bool WxBlackBox::bbIsShown()
711 if (bbGetContainingWindow()!=0)
712 return bbGetContainingWindow()->bbIsShown();
715 //==================================================================