1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/06/26 07:37:04 $
7 Version: $Revision: 1.21 $
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**)
236 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
238 //=========================================================================
240 //=========================================================================
241 void WxBlackBoxFrame::bbHide()
243 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
244 WxBlackBoxWindow::bbHide();
246 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
248 //=========================================================================
250 //=========================================================================
251 void WxBlackBoxFrame::bbClose()
255 //=========================================================================
259 //=========================================================================
260 // WxBlackBoxWidgetEventHandler
261 //=========================================================================
263 //=========================================================================
264 WxBlackBoxWidgetEventHandler::
265 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
271 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
273 mBox.lock()->bbSetWidgetEventHandler(this);
275 Connect ( mWindow->GetId(),
277 (wxObjectEventFunction)
278 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
279 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
281 mWindow->PushEventHandler(this);
284 //=========================================================================
286 //=========================================================================
287 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
289 if (mBox.expired()) return;
290 bbtkDebugMessage("wx",9,
291 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
292 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
293 mBox.lock()->bbSetWidgetEventHandler(0);
295 //=========================================================================
297 //=========================================================================
298 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
300 if (mBox.expired()) return;
301 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
302 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
303 mBox.lock()->bbSetOutputWidget(0);
304 mBox.lock()->bbSetModifiedStatus();
306 //=========================================================================
314 //=========================================================================
316 //=========================================================================
318 //=========================================================================
319 //=========================================================================
320 //=========================================================================
321 //=========================================================================
322 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
323 //=========================================================================
325 //=========================================================================
326 void WxBlackBox::bbUserConstructor()
328 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
331 //=========================================================================
333 //=========================================================================
334 void WxBlackBox::bbUserCopyConstructor()
336 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
340 //=========================================================================
343 //=========================================================================
344 void WxBlackBox::bbUserDestructor()
346 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
348 delete bbGetWindow();
351 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
353 //=========================================================================
357 //=========================================================================
358 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
360 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
362 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
363 bbGetFullName()<<"] : Widget is dead : deleting it"
365 delete bbGetOutputWidget();
366 bbSetOutputWidget(0);
368 return bbGetOutputWidget();
370 //=========================================================================
374 //=========================================================================
376 * \brief Initialize the attributes of the class
379 void WxBlackBox::bbInitAttributes()
383 // bbSetInputWinParent(0);
384 bbSetInputWinTitle(bbGetName());
385 bbSetInputWinWidth(800);
386 bbSetInputWinHeight(800);
387 bbSetInputWinDialog(false);
388 bbSetOutputWidget(0);
390 bbSetWidgetEventHandler(0);
391 bbSetUpdateTransferedToParent(false);
393 //=========================================================================
395 //=========================================================================
396 /// Main processing method of the box.
397 void WxBlackBox::bbExecute(bool force)
399 bbtkDebugMessageInc("process",2,
400 "=> WxBlackBox::bbExecute() ["
401 <<bbGetFullName()<<"]"<<std::endl);
403 // If the output 'Widget' is connected then
404 // we must execute the parent box
405 BlackBox::OutputConnectorMapType::const_iterator i
406 = bbGetOutputConnectorMap().find("Widget");
408 if ( i->second->GetConnectionVector().size() != 0 )
410 bbtkDebugMessage("process",3,
411 "-> Output 'Widget' connected : transfering execution to parent"
414 i->second->GetConnectionVector().front() //.lock()
415 ->GetBlackBoxTo()->bbExecute(force);
418 // else call 'standard' BlackBox execution method
421 BlackBox::bbExecute(force);
425 bbtkDebugMessageDec("process",2,
426 "<= WxBlackBox::bbExecute() ["
427 <<bbGetFullName()<<"]"<<std::endl);
429 //=========================================================================
432 //=========================================================================
433 /// Main processing method of the box.
434 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
436 bbtkDebugMessage("process",3,
437 "=> WxBlackBox::bbBackwardUpdate("
438 <<(caller?caller->GetFullName():"0")<<") ["
439 <<bbGetFullName()<<"]"<<std::endl);
440 // If the caller's box to is not the box to connected to the
442 if ( ! (( bbGetStatus() == MODIFIED ) ||
443 ( bbBoxProcessModeIsAlways() )) )
445 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
446 bbtkDebugMessage("process",3,
447 "<= WxBlackBox::bbBackwardUpdate("
448 <<(caller?caller->GetFullName():"0")<<") ["
449 <<bbGetFullName()<<"]"<<std::endl);
450 return bbGetStatus();
453 BlackBox::OutputConnectorMapType::const_iterator i
454 = bbGetOutputConnectorMap().find("Widget") ;
455 if ( i->second->GetConnectionVector().size() != 0 )
458 BlackBox::Pointer to =
459 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
463 bbtkDebugMessage("process",3,
464 "-> Output 'Widget' connected to '"
465 <<to->bbGetFullName()<<"' - caller->to = '"
466 <<caller->GetBlackBoxTo()->bbGetFullName()
472 bbtkDebugMessage("process",3,
473 "-> Output 'Widget' connected to '"
474 <<to->bbGetFullName()<<"'"
479 (caller->GetBlackBoxTo() != to))&&
480 (!bbGetUpdateTransferedToParent()))
482 bbtkDebugMessage("process",3,
483 " ... Transfering update order to parent"
486 bbSetUpdateTransferedToParent(true);
487 i->second->GetConnectionVector().front() //.lock()
488 ->GetBlackBoxTo()->bbExecute(false);
492 bbSetUpdateTransferedToParent(false);
493 bbtkDebugMessage("process",3,
494 " ... No need to transfer to parent"
500 // If the caller is not the connection to the output widget
501 // and the output 'Widget' is connected then
502 // we must execute the parent box
504 // (this is the role of the flag UpdateTransferedToParent)
506 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
511 // call 'standard' BlackBox execution method
512 if (!bbGetUpdateTransferedToParent())
514 AtomicBlackBox::bbBackwardUpdate(caller);
517 bbtkDebugMessageDec("process",3,
518 "<= WxBlackBox::bbBackwardUpdate() ["
519 <<bbGetFullName()<<"]"<<std::endl);
521 return bbGetStatus();
526 //=========================================================================
527 void WxBlackBox::bbProcess()
529 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
530 this->bbUserProcess();
532 // this->bbUserOnShow();
534 //=========================================================================
536 //==================================================================
537 /// Specific methods for window creation during pipeline execution
538 /// Shows the window associated to the box
539 /// (called after bbProcess during bbExecute)
540 void WxBlackBox::bbShowWindow()
542 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
543 <<bbGetFullName()<<"]"<<std::endl);
545 // If Event Handler for the widget does not exist or is obsolete : create it
546 if (bbGetOutputWidget()!=0)
548 if (bbGetWidgetEventHandler()==0)
550 bbtkDebugMessage("wx",3,
551 "-> No widget event handler : creating one"
553 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
554 bbGetOutputWidget());
556 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
557 ( bbGetOutputWidget() ) )
559 bbtkDebugMessage("wx",3,
560 "-> Obsolete widget event handler : re-creating one"
562 delete bbGetWidgetEventHandler();
563 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
564 bbGetOutputWidget());
566 // Sets the name of the wxWindow to the input WinTitle
567 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
570 // If the output 'Widget' is connected then it's gonna
571 // be captured by its parent window : nothing to do
572 if ( (*bbGetOutputConnectorMap().find("Widget")).second
573 ->GetConnectionVector().size() != 0 )
576 bbtkDebugMessage("wx",2,
577 "-> Output 'Widget' connected : nothing to do"
584 // If the window already exists : no need creating it
585 if (bbGetWindow()!=0)
587 bbtkDebugMessage("wx",2,
588 "-> Window already exists"
590 show = bbGetWindow();
592 // Else if the widget exists : create window
593 else if (bbGetOutputWidget()!=0)
595 bbtkDebugMessage("wx",2,
596 "-> Widget exists : creating the window"
600 // Input WinDialog set to true : creating a Dialog
601 if (bbGetInputWinDialog())
603 bbtkDebugMessage("wx",2,
604 " Input WinDialog set to true : creating a Dialog"
606 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
608 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
609 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
611 // Input WinDialog set to false : creating a Frame
614 bbtkDebugMessage("process",2,
615 " Input WinDialog set to false : creating a Frame"
617 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
619 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
620 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
624 // No window nor widget : error
627 bbtkError("WxBlackBox::bbShowWindow() ["
629 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
634 if (true) //!show->IsShown())
640 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
644 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
645 <<bbGetFullName()<<"]"<<std::endl);
648 //==================================================================
653 //==================================================================
654 void WxBlackBox::bbHideWindow()
656 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
657 <<bbGetFullName()<<"]"<<std::endl);
659 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
661 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
662 <<bbGetFullName()<<"]"<<std::endl);
664 //==================================================================
667 //==================================================================
668 void WxBlackBox::bbCloseWindow()
670 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
671 <<bbGetFullName()<<"]"<<std::endl);
673 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
675 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbCloseWindow() ["
676 <<bbGetFullName()<<"]"<<std::endl);
678 //==================================================================
680 //==================================================================
681 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
683 if (bbGetWindow()!=0) return bbGetWindow();
684 BlackBox::OutputConnectorMapType::const_iterator i
685 = bbGetOutputConnectorMap().find("Widget");
686 if ( i->second->GetConnectionVector().size() != 0 )
688 return boost::static_pointer_cast<WxBlackBox>
689 (i->second->GetConnectionVector().front() //.lock()
690 ->GetBlackBoxTo())->bbGetContainingWindow();
694 //==================================================================
697 //==================================================================
698 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
699 //==================================================================
702 //==================================================================
703 bool WxBlackBox::bbIsShown()
705 if (bbGetContainingWindow()!=0)
706 return bbGetContainingWindow()->bbIsShown();
709 //==================================================================