1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/05/06 13:45:12 $
7 Version: $Revision: 1.17 $
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 //=========================================================================
98 //=========================================================================
100 //=========================================================================
102 //=========================================================================
103 WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
108 WxBlackBoxWindow(box),
122 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
123 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
124 <<title<<",size)"<<std::endl);
125 // Insert the widget into the window
126 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
127 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
128 widget->Reparent(this);
129 sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
130 //SetAutoLayout(true);
134 //=========================================================================
136 //=========================================================================
137 void WxBlackBoxDialog::bbShow()
139 bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
140 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
141 WxBlackBoxWindow::bbShow();
142 SetReturnCode( wxDialog::ShowModal() );
144 //=========================================================================
146 //=========================================================================
147 void WxBlackBoxDialog::bbHide()
149 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
150 WxBlackBoxWindow::bbHide();
153 //=========================================================================
155 //=========================================================================
156 WxBlackBoxDialog::~WxBlackBoxDialog()
159 //=========================================================================
165 //=========================================================================
167 //=========================================================================
169 //=========================================================================
170 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
174 : WxBlackBoxWindow(box),
188 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
189 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
190 <<title<<",size)"<<std::endl);
191 // Insert the widget into the window
192 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
193 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
194 wxFrame* frame = (wxFrame*)this;
195 widget->Reparent(frame);
196 sizer->Add( widget, 1, wxALL|wxGROW, 2);
197 // frame->SetAutoLayout(true);
198 frame->SetSizer(sizer);
202 //=========================================================================
204 //=========================================================================
205 WxBlackBoxFrame::~WxBlackBoxFrame()
208 //=========================================================================
210 //=========================================================================
211 void WxBlackBoxFrame::bbShow()
213 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
214 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
215 WxBlackBoxWindow::bbShow();
217 // This Update is ** MANDATORY **
218 // to synchronize wxvtkRenderWindowInteractor objects
219 // (force wx objects creation **NOW**)
221 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
222 // wxFrame::SetFocus();
224 //=========================================================================
226 //=========================================================================
227 void WxBlackBoxFrame::bbHide()
229 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
230 WxBlackBoxWindow::bbHide();
232 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
234 //=========================================================================
239 //=========================================================================
240 // WxBlackBoxWidgetEventHandler
241 //=========================================================================
243 //=========================================================================
244 WxBlackBoxWidgetEventHandler::
245 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
251 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
253 mBox.lock()->bbSetWidgetEventHandler(this);
255 Connect ( mWindow->GetId(),
257 (wxObjectEventFunction)
258 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
259 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
261 mWindow->PushEventHandler(this);
264 //=========================================================================
266 //=========================================================================
267 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
269 if (mBox.expired()) return;
270 bbtkDebugMessage("wx",9,
271 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
272 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
273 mBox.lock()->bbSetWidgetEventHandler(0);
275 //=========================================================================
277 //=========================================================================
278 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
280 if (mBox.expired()) return;
281 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
282 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
283 mBox.lock()->bbSetOutputWidget(0);
284 mBox.lock()->bbSetModifiedStatus();
286 //=========================================================================
294 //=========================================================================
296 //=========================================================================
298 //=========================================================================
299 //=========================================================================
300 //=========================================================================
301 //=========================================================================
302 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
303 //=========================================================================
305 //=========================================================================
306 void WxBlackBox::bbUserConstructor()
308 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
311 //=========================================================================
313 //=========================================================================
314 void WxBlackBox::bbUserCopyConstructor()
316 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
320 //=========================================================================
323 //=========================================================================
324 void WxBlackBox::bbUserDestructor()
326 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
328 delete bbGetWindow();
331 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
333 //=========================================================================
337 //=========================================================================
338 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
340 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
342 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
343 bbGetFullName()<<"] : Widget is dead : deleting it"
345 delete bbGetOutputWidget();
346 bbSetOutputWidget(0);
348 return bbGetOutputWidget();
350 //=========================================================================
354 //=========================================================================
356 * \brief Initialize the attributes of the class
359 void WxBlackBox::bbInitAttributes()
363 // bbSetInputWinParent(0);
364 bbSetInputWinTitle(bbGetName());
365 bbSetInputWinWidth(800);
366 bbSetInputWinHeight(800);
367 bbSetInputWinDialog(false);
368 bbSetOutputWidget(0);
370 bbSetWidgetEventHandler(0);
371 bbSetUpdateTransferedToParent(false);
373 //=========================================================================
375 //=========================================================================
376 /// Main processing method of the box.
377 void WxBlackBox::bbExecute(bool force)
379 bbtkDebugMessageInc("process",2,
380 "=> WxBlackBox::bbExecute() ["
381 <<bbGetFullName()<<"]"<<std::endl);
383 // If the output 'Widget' is connected then
384 // we must execute the parent box
385 BlackBox::OutputConnectorMapType::const_iterator i
386 = bbGetOutputConnectorMap().find("Widget");
388 if ( i->second->GetConnectionVector().size() != 0 )
390 bbtkDebugMessage("process",3,
391 "-> Output 'Widget' connected : transfering execution to parent"
394 i->second->GetConnectionVector().front() //.lock()
395 ->GetBlackBoxTo()->bbExecute(force);
398 // else call 'standard' BlackBox execution method
401 BlackBox::bbExecute(force);
405 bbtkDebugMessageDec("process",2,
406 "<= WxBlackBox::bbExecute() ["
407 <<bbGetFullName()<<"]"<<std::endl);
409 //=========================================================================
412 //=========================================================================
413 /// Main processing method of the box.
414 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
416 bbtkDebugMessage("process",3,
417 "=> WxBlackBox::bbBackwardUpdate("
418 <<(caller?caller->GetFullName():"0")<<") ["
419 <<bbGetFullName()<<"]"<<std::endl);
420 // If the caller's box to is not the box to connected to the
422 if ( ! (( bbGetStatus() == MODIFIED ) ||
423 ( bbBoxProcessModeIsAlways() )) )
425 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
426 bbtkDebugMessage("process",3,
427 "<= WxBlackBox::bbBackwardUpdate("
428 <<(caller?caller->GetFullName():"0")<<") ["
429 <<bbGetFullName()<<"]"<<std::endl);
430 return bbGetStatus();
433 BlackBox::OutputConnectorMapType::const_iterator i
434 = bbGetOutputConnectorMap().find("Widget") ;
435 if ( i->second->GetConnectionVector().size() != 0 )
438 BlackBox::Pointer to =
439 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
443 bbtkDebugMessage("process",3,
444 "-> Output 'Widget' connected to '"
445 <<to->bbGetFullName()<<"' - caller->to = '"
446 <<caller->GetBlackBoxTo()->bbGetFullName()
452 bbtkDebugMessage("process",3,
453 "-> Output 'Widget' connected to '"
454 <<to->bbGetFullName()<<"'"
459 (caller->GetBlackBoxTo() != to))&&
460 (!bbGetUpdateTransferedToParent()))
462 bbtkDebugMessage("process",3,
463 " ... Transfering update order to parent"
466 bbSetUpdateTransferedToParent(true);
467 i->second->GetConnectionVector().front() //.lock()
468 ->GetBlackBoxTo()->bbExecute(false);
472 bbSetUpdateTransferedToParent(false);
473 bbtkDebugMessage("process",3,
474 " ... No need to transfer to parent"
480 // If the caller is not the connection to the output widget
481 // and the output 'Widget' is connected then
482 // we must execute the parent box
484 // (this is the role of the flag UpdateTransferedToParent)
486 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
491 // call 'standard' BlackBox execution method
492 if (!bbGetUpdateTransferedToParent())
494 AtomicBlackBox::bbBackwardUpdate(caller);
497 bbtkDebugMessageDec("process",3,
498 "<= WxBlackBox::bbBackwardUpdate() ["
499 <<bbGetFullName()<<"]"<<std::endl);
501 return bbGetStatus();
506 //=========================================================================
507 void WxBlackBox::bbProcess()
509 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
510 this->bbUserProcess();
513 //=========================================================================
515 //==================================================================
516 /// Specific methods for window creation during pipeline execution
517 /// Shows the window associated to the box
518 /// (called after bbProcess during bbExecute)
519 void WxBlackBox::bbShowWindow()
521 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
522 <<bbGetFullName()<<"]"<<std::endl);
524 // If Event Handler for the widget does not exist or is obsolete : create it
525 if (bbGetOutputWidget()!=0)
527 if (bbGetWidgetEventHandler()==0)
529 bbtkDebugMessage("wx",3,
530 "-> No widget event handler : creating one"
532 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
533 bbGetOutputWidget());
535 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
536 ( bbGetOutputWidget() ) )
538 bbtkDebugMessage("wx",3,
539 "-> Obsolete widget event handler : re-creating one"
541 delete bbGetWidgetEventHandler();
542 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
543 bbGetOutputWidget());
545 // Sets the name of the wxWindow to the input WinTitle
546 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
549 // If the output 'Widget' is connected then it's gonna
550 // be captured by its parent window : nothing to do
551 if ( (*bbGetOutputConnectorMap().find("Widget")).second
552 ->GetConnectionVector().size() != 0 )
555 bbtkDebugMessage("wx",2,
556 "-> Output 'Widget' connected : nothing to do"
563 // If the window already exists : no need creating it
564 if (bbGetWindow()!=0)
566 bbtkDebugMessage("wx",2,
567 "-> Window already exists"
569 show = bbGetWindow();
571 // Else if the widget exists : create window
572 else if (bbGetOutputWidget()!=0)
574 bbtkDebugMessage("wx",2,
575 "-> Widget exists : creating the window"
579 // Input WinDialog set to true : creating a Dialog
580 if (bbGetInputWinDialog())
582 bbtkDebugMessage("wx",2,
583 " Input WinDialog set to true : creating a Dialog"
585 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
587 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
588 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
590 // Input WinDialog set to false : creating a Frame
593 bbtkDebugMessage("process",2,
594 " Input WinDialog set to false : creating a Frame"
596 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
598 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
599 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
603 // No window nor widget : error
606 bbtkError("WxBlackBox::bbShowWindow() ["
608 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
613 if (true) //!show->IsShown())
619 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
623 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
624 <<bbGetFullName()<<"]"<<std::endl);
627 //==================================================================
632 //==================================================================
633 void WxBlackBox::bbHideWindow()
635 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
636 <<bbGetFullName()<<"]"<<std::endl);
638 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
640 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
641 <<bbGetFullName()<<"]"<<std::endl);
643 //==================================================================
646 //==================================================================
647 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
649 if (bbGetWindow()!=0) return bbGetWindow();
650 BlackBox::OutputConnectorMapType::const_iterator i
651 = bbGetOutputConnectorMap().find("Widget");
652 if ( i->second->GetConnectionVector().size() != 0 )
654 return boost::static_pointer_cast<WxBlackBox>
655 (i->second->GetConnectionVector().front() //.lock()
656 ->GetBlackBoxTo())->bbGetContainingWindow();
660 //==================================================================
663 //==================================================================
664 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
665 //==================================================================
668 //==================================================================
669 bool WxBlackBox::bbIsShown()
671 if (bbGetContainingWindow()!=0)
672 return bbGetContainingWindow()->bbIsShown();
675 //==================================================================