1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/05/13 09:52:58 $
7 Version: $Revision: 1.18 $
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() );
145 //=========================================================================
147 //=========================================================================
148 void WxBlackBoxDialog::bbHide()
150 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
151 WxBlackBoxWindow::bbHide();
154 //=========================================================================
156 //=========================================================================
157 WxBlackBoxDialog::~WxBlackBoxDialog()
160 //=========================================================================
166 //=========================================================================
168 //=========================================================================
170 //=========================================================================
171 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
175 : WxBlackBoxWindow(box),
189 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
190 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
191 <<title<<",size)"<<std::endl);
192 // Insert the widget into the window
193 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
194 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
195 wxFrame* frame = (wxFrame*)this;
196 widget->Reparent(frame);
197 sizer->Add( widget, 1, wxALL|wxGROW, 2);
198 // frame->SetAutoLayout(true);
199 frame->SetSizer(sizer);
203 //=========================================================================
205 //=========================================================================
206 WxBlackBoxFrame::~WxBlackBoxFrame()
209 //=========================================================================
211 //=========================================================================
212 void WxBlackBoxFrame::bbShow()
214 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
215 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
216 WxBlackBoxWindow::bbShow();
218 // This Update is ** MANDATORY **
219 // to synchronize wxvtkRenderWindowInteractor objects
220 // (force wx objects creation **NOW**)
222 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
223 // wxFrame::SetFocus();
225 //=========================================================================
227 //=========================================================================
228 void WxBlackBoxFrame::bbHide()
230 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
231 WxBlackBoxWindow::bbHide();
233 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
235 //=========================================================================
240 //=========================================================================
241 // WxBlackBoxWidgetEventHandler
242 //=========================================================================
244 //=========================================================================
245 WxBlackBoxWidgetEventHandler::
246 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
252 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
254 mBox.lock()->bbSetWidgetEventHandler(this);
256 Connect ( mWindow->GetId(),
258 (wxObjectEventFunction)
259 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
260 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
262 mWindow->PushEventHandler(this);
265 //=========================================================================
267 //=========================================================================
268 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
270 if (mBox.expired()) return;
271 bbtkDebugMessage("wx",9,
272 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
273 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
274 mBox.lock()->bbSetWidgetEventHandler(0);
276 //=========================================================================
278 //=========================================================================
279 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
281 if (mBox.expired()) return;
282 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
283 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
284 mBox.lock()->bbSetOutputWidget(0);
285 mBox.lock()->bbSetModifiedStatus();
287 //=========================================================================
295 //=========================================================================
297 //=========================================================================
299 //=========================================================================
300 //=========================================================================
301 //=========================================================================
302 //=========================================================================
303 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
304 //=========================================================================
306 //=========================================================================
307 void WxBlackBox::bbUserConstructor()
309 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
312 //=========================================================================
314 //=========================================================================
315 void WxBlackBox::bbUserCopyConstructor()
317 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
321 //=========================================================================
324 //=========================================================================
325 void WxBlackBox::bbUserDestructor()
327 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
329 delete bbGetWindow();
332 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
334 //=========================================================================
338 //=========================================================================
339 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
341 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
343 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
344 bbGetFullName()<<"] : Widget is dead : deleting it"
346 delete bbGetOutputWidget();
347 bbSetOutputWidget(0);
349 return bbGetOutputWidget();
351 //=========================================================================
355 //=========================================================================
357 * \brief Initialize the attributes of the class
360 void WxBlackBox::bbInitAttributes()
364 // bbSetInputWinParent(0);
365 bbSetInputWinTitle(bbGetName());
366 bbSetInputWinWidth(800);
367 bbSetInputWinHeight(800);
368 bbSetInputWinDialog(false);
369 bbSetOutputWidget(0);
371 bbSetWidgetEventHandler(0);
372 bbSetUpdateTransferedToParent(false);
374 //=========================================================================
376 //=========================================================================
377 /// Main processing method of the box.
378 void WxBlackBox::bbExecute(bool force)
380 bbtkDebugMessageInc("process",2,
381 "=> WxBlackBox::bbExecute() ["
382 <<bbGetFullName()<<"]"<<std::endl);
384 // If the output 'Widget' is connected then
385 // we must execute the parent box
386 BlackBox::OutputConnectorMapType::const_iterator i
387 = bbGetOutputConnectorMap().find("Widget");
389 if ( i->second->GetConnectionVector().size() != 0 )
391 bbtkDebugMessage("process",3,
392 "-> Output 'Widget' connected : transfering execution to parent"
395 i->second->GetConnectionVector().front() //.lock()
396 ->GetBlackBoxTo()->bbExecute(force);
399 // else call 'standard' BlackBox execution method
402 BlackBox::bbExecute(force);
406 bbtkDebugMessageDec("process",2,
407 "<= WxBlackBox::bbExecute() ["
408 <<bbGetFullName()<<"]"<<std::endl);
410 //=========================================================================
413 //=========================================================================
414 /// Main processing method of the box.
415 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
417 bbtkDebugMessage("process",3,
418 "=> WxBlackBox::bbBackwardUpdate("
419 <<(caller?caller->GetFullName():"0")<<") ["
420 <<bbGetFullName()<<"]"<<std::endl);
421 // If the caller's box to is not the box to connected to the
423 if ( ! (( bbGetStatus() == MODIFIED ) ||
424 ( bbBoxProcessModeIsAlways() )) )
426 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
427 bbtkDebugMessage("process",3,
428 "<= WxBlackBox::bbBackwardUpdate("
429 <<(caller?caller->GetFullName():"0")<<") ["
430 <<bbGetFullName()<<"]"<<std::endl);
431 return bbGetStatus();
434 BlackBox::OutputConnectorMapType::const_iterator i
435 = bbGetOutputConnectorMap().find("Widget") ;
436 if ( i->second->GetConnectionVector().size() != 0 )
439 BlackBox::Pointer to =
440 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
444 bbtkDebugMessage("process",3,
445 "-> Output 'Widget' connected to '"
446 <<to->bbGetFullName()<<"' - caller->to = '"
447 <<caller->GetBlackBoxTo()->bbGetFullName()
453 bbtkDebugMessage("process",3,
454 "-> Output 'Widget' connected to '"
455 <<to->bbGetFullName()<<"'"
460 (caller->GetBlackBoxTo() != to))&&
461 (!bbGetUpdateTransferedToParent()))
463 bbtkDebugMessage("process",3,
464 " ... Transfering update order to parent"
467 bbSetUpdateTransferedToParent(true);
468 i->second->GetConnectionVector().front() //.lock()
469 ->GetBlackBoxTo()->bbExecute(false);
473 bbSetUpdateTransferedToParent(false);
474 bbtkDebugMessage("process",3,
475 " ... No need to transfer to parent"
481 // If the caller is not the connection to the output widget
482 // and the output 'Widget' is connected then
483 // we must execute the parent box
485 // (this is the role of the flag UpdateTransferedToParent)
487 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
492 // call 'standard' BlackBox execution method
493 if (!bbGetUpdateTransferedToParent())
495 AtomicBlackBox::bbBackwardUpdate(caller);
498 bbtkDebugMessageDec("process",3,
499 "<= WxBlackBox::bbBackwardUpdate() ["
500 <<bbGetFullName()<<"]"<<std::endl);
502 return bbGetStatus();
507 //=========================================================================
508 void WxBlackBox::bbProcess()
510 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
511 this->bbUserProcess();
514 //=========================================================================
516 //==================================================================
517 /// Specific methods for window creation during pipeline execution
518 /// Shows the window associated to the box
519 /// (called after bbProcess during bbExecute)
520 void WxBlackBox::bbShowWindow()
522 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
523 <<bbGetFullName()<<"]"<<std::endl);
525 // If Event Handler for the widget does not exist or is obsolete : create it
526 if (bbGetOutputWidget()!=0)
528 if (bbGetWidgetEventHandler()==0)
530 bbtkDebugMessage("wx",3,
531 "-> No widget event handler : creating one"
533 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
534 bbGetOutputWidget());
536 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
537 ( bbGetOutputWidget() ) )
539 bbtkDebugMessage("wx",3,
540 "-> Obsolete widget event handler : re-creating one"
542 delete bbGetWidgetEventHandler();
543 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
544 bbGetOutputWidget());
546 // Sets the name of the wxWindow to the input WinTitle
547 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
550 // If the output 'Widget' is connected then it's gonna
551 // be captured by its parent window : nothing to do
552 if ( (*bbGetOutputConnectorMap().find("Widget")).second
553 ->GetConnectionVector().size() != 0 )
556 bbtkDebugMessage("wx",2,
557 "-> Output 'Widget' connected : nothing to do"
564 // If the window already exists : no need creating it
565 if (bbGetWindow()!=0)
567 bbtkDebugMessage("wx",2,
568 "-> Window already exists"
570 show = bbGetWindow();
572 // Else if the widget exists : create window
573 else if (bbGetOutputWidget()!=0)
575 bbtkDebugMessage("wx",2,
576 "-> Widget exists : creating the window"
580 // Input WinDialog set to true : creating a Dialog
581 if (bbGetInputWinDialog())
583 bbtkDebugMessage("wx",2,
584 " Input WinDialog set to true : creating a Dialog"
586 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
588 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
589 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
591 // Input WinDialog set to false : creating a Frame
594 bbtkDebugMessage("process",2,
595 " Input WinDialog set to false : creating a Frame"
597 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
599 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
600 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
604 // No window nor widget : error
607 bbtkError("WxBlackBox::bbShowWindow() ["
609 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
614 if (true) //!show->IsShown())
620 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
624 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
625 <<bbGetFullName()<<"]"<<std::endl);
628 //==================================================================
633 //==================================================================
634 void WxBlackBox::bbHideWindow()
636 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
637 <<bbGetFullName()<<"]"<<std::endl);
639 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
641 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
642 <<bbGetFullName()<<"]"<<std::endl);
644 //==================================================================
647 //==================================================================
648 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
650 if (bbGetWindow()!=0) return bbGetWindow();
651 BlackBox::OutputConnectorMapType::const_iterator i
652 = bbGetOutputConnectorMap().find("Widget");
653 if ( i->second->GetConnectionVector().size() != 0 )
655 return boost::static_pointer_cast<WxBlackBox>
656 (i->second->GetConnectionVector().front() //.lock()
657 ->GetBlackBoxTo())->bbGetContainingWindow();
661 //==================================================================
664 //==================================================================
665 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
666 //==================================================================
669 //==================================================================
670 bool WxBlackBox::bbIsShown()
672 if (bbGetContainingWindow()!=0)
673 return bbGetContainingWindow()->bbIsShown();
676 //==================================================================