1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/07/24 14:37:06 $
7 Version: $Revision: 1.25 $
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,
125 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()
164 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
167 //=========================================================================
169 //=========================================================================
170 WxBlackBoxDialog::~WxBlackBoxDialog()
173 //=========================================================================
179 //=========================================================================
181 //=========================================================================
183 //=========================================================================
184 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
200 WxBlackBoxWindow(box)
202 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
203 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
204 <<title<<",size)"<<std::endl);
205 // Insert the widget into the window
206 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
207 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
208 wxFrame* frame = (wxFrame*)this;
209 widget->Reparent(frame);
210 sizer->Add( widget, 1, wxALL|wxGROW, 2);
211 // frame->SetAutoLayout(true);
212 frame->SetSizer(sizer);
216 //=========================================================================
218 //=========================================================================
219 WxBlackBoxFrame::~WxBlackBoxFrame()
222 //=========================================================================
224 //=========================================================================
225 void WxBlackBoxFrame::bbShow()
227 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
228 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
229 WxBlackBoxWindow::bbShow();
231 // This Update is ** MANDATORY **
232 // to synchronize wxvtkRenderWindowInteractor objects
233 // (force wx objects creation **NOW**)
242 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
244 //=========================================================================
246 //=========================================================================
247 void WxBlackBoxFrame::bbHide()
249 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
250 WxBlackBoxWindow::bbHide();
252 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
254 //=========================================================================
256 //=========================================================================
257 void WxBlackBoxFrame::bbClose()
259 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
262 //=========================================================================
266 //=========================================================================
267 // WxBlackBoxWidgetEventHandler
268 //=========================================================================
270 //=========================================================================
271 WxBlackBoxWidgetEventHandler::
272 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
278 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
280 mBox.lock()->bbSetWidgetEventHandler(this);
282 Connect ( mWindow->GetId(),
284 (wxObjectEventFunction)
285 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
286 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
288 mWindow->PushEventHandler(this);
291 //=========================================================================
293 //=========================================================================
294 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
296 if (mBox.expired()) return;
297 bbtkDebugMessage("wx",9,
298 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
299 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
300 mBox.lock()->bbSetWidgetEventHandler(0);
302 //=========================================================================
304 //=========================================================================
305 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
307 if (mBox.expired()) return;
308 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
309 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
310 mBox.lock()->bbSetOutputWidget(0);
311 mBox.lock()->bbSetModifiedStatus();
313 //=========================================================================
321 //=========================================================================
323 //=========================================================================
325 //=========================================================================
326 //=========================================================================
327 //=========================================================================
328 //=========================================================================
329 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
330 //=========================================================================
332 //=========================================================================
333 void WxBlackBox::bbUserConstructor()
335 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
338 //=========================================================================
340 //=========================================================================
341 void WxBlackBox::bbUserCopyConstructor()
343 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
347 //=========================================================================
350 //=========================================================================
351 void WxBlackBox::bbUserDestructor()
353 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
355 delete bbGetWindow();
358 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
360 //=========================================================================
364 //=========================================================================
365 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
367 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
369 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
370 bbGetFullName()<<"] : Widget is dead : deleting it"
372 delete bbGetOutputWidget();
373 bbSetOutputWidget(0);
375 return bbGetOutputWidget();
377 //=========================================================================
381 //=========================================================================
383 * \brief Initialize the attributes of the class
386 void WxBlackBox::bbInitAttributes()
390 // bbSetInputWinParent(0);
391 bbSetInputWinTitle(bbGetName());
392 bbSetInputWinWidth(800);
393 bbSetInputWinHeight(800);
394 bbSetInputWinDialog(false);
395 bbSetOutputWidget(0);
397 bbSetWidgetEventHandler(0);
398 bbSetUpdateTransferedToParent(false);
400 //=========================================================================
402 //=========================================================================
403 /// Main processing method of the box.
404 void WxBlackBox::bbExecute(bool force)
406 bbtkDebugMessageInc("process",2,
407 "=> WxBlackBox::bbExecute() ["
408 <<bbGetFullName()<<"]"<<std::endl);
410 // If the output 'Widget' is connected then
411 // we must execute the parent box
412 BlackBox::OutputConnectorMapType::const_iterator i
413 = bbGetOutputConnectorMap().find("Widget");
415 if ( i->second->GetConnectionVector().size() != 0 )
417 bbtkDebugMessage("process",3,
418 "-> Output 'Widget' connected : transfering execution to parent"
421 i->second->GetConnectionVector().front() //.lock()
422 ->GetBlackBoxTo()->bbExecute(force);
425 // else call 'standard' BlackBox execution method
428 BlackBox::bbExecute(force);
432 bbtkDebugMessageDec("process",2,
433 "<= WxBlackBox::bbExecute() ["
434 <<bbGetFullName()<<"]"<<std::endl);
436 //=========================================================================
439 //=========================================================================
440 /// Main processing method of the box.
441 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
443 bbtkDebugMessage("process",3,
444 "=> WxBlackBox::bbBackwardUpdate("
445 <<(caller?caller->GetFullName():"0")<<") ["
446 <<bbGetFullName()<<"]"<<std::endl);
447 // If the caller's box to is not the box to connected to the
449 if ( ! (( bbGetStatus() == MODIFIED ) ||
450 ( bbBoxProcessModeIsAlways() )) )
452 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
453 bbtkDebugMessage("process",3,
454 "<= WxBlackBox::bbBackwardUpdate("
455 <<(caller?caller->GetFullName():"0")<<") ["
456 <<bbGetFullName()<<"]"<<std::endl);
457 return bbGetStatus();
460 BlackBox::OutputConnectorMapType::const_iterator i
461 = bbGetOutputConnectorMap().find("Widget") ;
462 if ( i->second->GetConnectionVector().size() != 0 )
465 BlackBox::Pointer to =
466 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
470 bbtkDebugMessage("process",3,
471 "-> Output 'Widget' connected to '"
472 <<to->bbGetFullName()<<"' - caller->to = '"
473 <<caller->GetBlackBoxTo()->bbGetFullName()
479 bbtkDebugMessage("process",3,
480 "-> Output 'Widget' connected to '"
481 <<to->bbGetFullName()<<"'"
486 (caller->GetBlackBoxTo() != to))&&
487 (!bbGetUpdateTransferedToParent())&&
488 (to->bbGetStatus()!=UPDATING))
490 bbtkDebugMessage("process",3,
491 " ... Transfering update order to parent"
494 bbSetUpdateTransferedToParent(true);
495 i->second->GetConnectionVector().front() //.lock()
496 ->GetBlackBoxTo()->bbExecute(false);
500 bbSetUpdateTransferedToParent(false);
501 bbtkDebugMessage("process",3,
502 " ... No need to transfer to parent"
508 // If the caller is not the connection to the output widget
509 // and the output 'Widget' is connected then
510 // we must execute the parent box
512 // (this is the role of the flag UpdateTransferedToParent)
514 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
519 // call 'standard' BlackBox execution method
520 if (!bbGetUpdateTransferedToParent())
522 AtomicBlackBox::bbBackwardUpdate(caller);
525 bbtkDebugMessageDec("process",3,
526 "<= WxBlackBox::bbBackwardUpdate() ["
527 <<bbGetFullName()<<"]"<<std::endl);
529 return bbGetStatus();
534 //=========================================================================
535 void WxBlackBox::bbProcess()
537 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
538 this->bbUserProcess();
540 // this->bbUserOnShow();
542 //=========================================================================
544 //==================================================================
545 /// Specific methods for window creation during pipeline execution
546 /// Shows the window associated to the box
547 /// (called after bbProcess during bbExecute)
548 void WxBlackBox::bbShowWindow()
550 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
551 <<bbGetFullName()<<"]"<<std::endl);
553 // If Event Handler for the widget does not exist or is obsolete : create it
554 if (bbGetOutputWidget()!=0)
556 if (bbGetWidgetEventHandler()==0)
558 bbtkDebugMessage("wx",3,
559 "-> No widget event handler : creating one"
561 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
562 bbGetOutputWidget());
564 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
565 ( bbGetOutputWidget() ) )
567 bbtkDebugMessage("wx",3,
568 "-> Obsolete widget event handler : re-creating one"
570 delete bbGetWidgetEventHandler();
571 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
572 bbGetOutputWidget());
574 // Sets the name of the wxWindow to the input WinTitle
575 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
578 // If the output 'Widget' is connected then it's gonna
579 // be captured by its parent window : nothing to do
580 if ( (*bbGetOutputConnectorMap().find("Widget")).second
581 ->GetConnectionVector().size() != 0 )
584 bbtkDebugMessage("wx",2,
585 "-> Output 'Widget' connected : nothing to do"
592 // If the window already exists : no need creating it
593 if (bbGetWindow()!=0)
595 bbtkDebugMessage("wx",2,
596 "-> Window already exists"
598 show = bbGetWindow();
600 // Else if the widget exists : create window
601 else if (bbGetOutputWidget()!=0)
603 bbtkDebugMessage("wx",2,
604 "-> Widget exists : creating the window"
608 // Input WinDialog set to true : creating a Dialog
609 if (bbGetInputWinDialog())
611 bbtkDebugMessage("wx",2,
612 " Input WinDialog set to true : creating a Dialog"
614 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
616 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
617 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
619 // Input WinDialog set to false : creating a Frame
622 bbtkDebugMessage("process",2,
623 " Input WinDialog set to false : creating a Frame"
625 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
627 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
628 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
632 // No window nor widget : error
635 bbtkError("WxBlackBox::bbShowWindow() ["
637 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
642 if (true) //!show->IsShown())
648 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
652 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
653 <<bbGetFullName()<<"]"<<std::endl);
656 //==================================================================
661 //==================================================================
662 void WxBlackBox::bbHideWindow()
664 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
665 <<bbGetFullName()<<"]"<<std::endl);
667 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
669 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
670 <<bbGetFullName()<<"]"<<std::endl);
672 //==================================================================
675 //==================================================================
676 void WxBlackBox::bbCloseWindow()
678 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
679 <<bbGetFullName()<<"]"<<std::endl);
681 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
683 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbCloseWindow() ["
684 <<bbGetFullName()<<"]"<<std::endl);
686 //==================================================================
688 //==================================================================
689 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
691 if (bbGetWindow()!=0) return bbGetWindow();
692 BlackBox::OutputConnectorMapType::const_iterator i
693 = bbGetOutputConnectorMap().find("Widget");
694 if ( i->second->GetConnectionVector().size() != 0 )
696 return boost::static_pointer_cast<WxBlackBox>
697 (i->second->GetConnectionVector().front() //.lock()
698 ->GetBlackBoxTo())->bbGetContainingWindow();
702 //==================================================================
705 //==================================================================
706 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
707 //==================================================================
710 //==================================================================
711 bool WxBlackBox::bbIsShown()
713 if (bbGetContainingWindow()!=0)
714 return bbGetContainingWindow()->bbIsShown();
717 //==================================================================