1 /*=========================================================================
4 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
6 Date: $Date: 2008/05/15 08:02:36 $
7 Version: $Revision: 1.20 $
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**)
234 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
235 // wxFrame::SetFocus();
237 //=========================================================================
239 //=========================================================================
240 void WxBlackBoxFrame::bbHide()
242 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
243 WxBlackBoxWindow::bbHide();
245 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
247 //=========================================================================
249 //=========================================================================
250 void WxBlackBoxFrame::bbClose()
254 //=========================================================================
258 //=========================================================================
259 // WxBlackBoxWidgetEventHandler
260 //=========================================================================
262 //=========================================================================
263 WxBlackBoxWidgetEventHandler::
264 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
270 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
272 mBox.lock()->bbSetWidgetEventHandler(this);
274 Connect ( mWindow->GetId(),
276 (wxObjectEventFunction)
277 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
278 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
280 mWindow->PushEventHandler(this);
283 //=========================================================================
285 //=========================================================================
286 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
288 if (mBox.expired()) return;
289 bbtkDebugMessage("wx",9,
290 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
291 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
292 mBox.lock()->bbSetWidgetEventHandler(0);
294 //=========================================================================
296 //=========================================================================
297 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
299 if (mBox.expired()) return;
300 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
301 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
302 mBox.lock()->bbSetOutputWidget(0);
303 mBox.lock()->bbSetModifiedStatus();
305 //=========================================================================
313 //=========================================================================
315 //=========================================================================
317 //=========================================================================
318 //=========================================================================
319 //=========================================================================
320 //=========================================================================
321 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
322 //=========================================================================
324 //=========================================================================
325 void WxBlackBox::bbUserConstructor()
327 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
330 //=========================================================================
332 //=========================================================================
333 void WxBlackBox::bbUserCopyConstructor()
335 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
339 //=========================================================================
342 //=========================================================================
343 void WxBlackBox::bbUserDestructor()
345 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
347 delete bbGetWindow();
350 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
352 //=========================================================================
356 //=========================================================================
357 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
359 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
361 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
362 bbGetFullName()<<"] : Widget is dead : deleting it"
364 delete bbGetOutputWidget();
365 bbSetOutputWidget(0);
367 return bbGetOutputWidget();
369 //=========================================================================
373 //=========================================================================
375 * \brief Initialize the attributes of the class
378 void WxBlackBox::bbInitAttributes()
382 // bbSetInputWinParent(0);
383 bbSetInputWinTitle(bbGetName());
384 bbSetInputWinWidth(800);
385 bbSetInputWinHeight(800);
386 bbSetInputWinDialog(false);
387 bbSetOutputWidget(0);
389 bbSetWidgetEventHandler(0);
390 bbSetUpdateTransferedToParent(false);
392 //=========================================================================
394 //=========================================================================
395 /// Main processing method of the box.
396 void WxBlackBox::bbExecute(bool force)
398 bbtkDebugMessageInc("process",2,
399 "=> WxBlackBox::bbExecute() ["
400 <<bbGetFullName()<<"]"<<std::endl);
402 // If the output 'Widget' is connected then
403 // we must execute the parent box
404 BlackBox::OutputConnectorMapType::const_iterator i
405 = bbGetOutputConnectorMap().find("Widget");
407 if ( i->second->GetConnectionVector().size() != 0 )
409 bbtkDebugMessage("process",3,
410 "-> Output 'Widget' connected : transfering execution to parent"
413 i->second->GetConnectionVector().front() //.lock()
414 ->GetBlackBoxTo()->bbExecute(force);
417 // else call 'standard' BlackBox execution method
420 BlackBox::bbExecute(force);
424 bbtkDebugMessageDec("process",2,
425 "<= WxBlackBox::bbExecute() ["
426 <<bbGetFullName()<<"]"<<std::endl);
428 //=========================================================================
431 //=========================================================================
432 /// Main processing method of the box.
433 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
435 bbtkDebugMessage("process",3,
436 "=> WxBlackBox::bbBackwardUpdate("
437 <<(caller?caller->GetFullName():"0")<<") ["
438 <<bbGetFullName()<<"]"<<std::endl);
439 // If the caller's box to is not the box to connected to the
441 if ( ! (( bbGetStatus() == MODIFIED ) ||
442 ( bbBoxProcessModeIsAlways() )) )
444 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
445 bbtkDebugMessage("process",3,
446 "<= WxBlackBox::bbBackwardUpdate("
447 <<(caller?caller->GetFullName():"0")<<") ["
448 <<bbGetFullName()<<"]"<<std::endl);
449 return bbGetStatus();
452 BlackBox::OutputConnectorMapType::const_iterator i
453 = bbGetOutputConnectorMap().find("Widget") ;
454 if ( i->second->GetConnectionVector().size() != 0 )
457 BlackBox::Pointer to =
458 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
462 bbtkDebugMessage("process",3,
463 "-> Output 'Widget' connected to '"
464 <<to->bbGetFullName()<<"' - caller->to = '"
465 <<caller->GetBlackBoxTo()->bbGetFullName()
471 bbtkDebugMessage("process",3,
472 "-> Output 'Widget' connected to '"
473 <<to->bbGetFullName()<<"'"
478 (caller->GetBlackBoxTo() != to))&&
479 (!bbGetUpdateTransferedToParent()))
481 bbtkDebugMessage("process",3,
482 " ... Transfering update order to parent"
485 bbSetUpdateTransferedToParent(true);
486 i->second->GetConnectionVector().front() //.lock()
487 ->GetBlackBoxTo()->bbExecute(false);
491 bbSetUpdateTransferedToParent(false);
492 bbtkDebugMessage("process",3,
493 " ... No need to transfer to parent"
499 // If the caller is not the connection to the output widget
500 // and the output 'Widget' is connected then
501 // we must execute the parent box
503 // (this is the role of the flag UpdateTransferedToParent)
505 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
510 // call 'standard' BlackBox execution method
511 if (!bbGetUpdateTransferedToParent())
513 AtomicBlackBox::bbBackwardUpdate(caller);
516 bbtkDebugMessageDec("process",3,
517 "<= WxBlackBox::bbBackwardUpdate() ["
518 <<bbGetFullName()<<"]"<<std::endl);
520 return bbGetStatus();
525 //=========================================================================
526 void WxBlackBox::bbProcess()
528 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
529 this->bbUserProcess();
531 // this->bbUserOnShow();
533 //=========================================================================
535 //==================================================================
536 /// Specific methods for window creation during pipeline execution
537 /// Shows the window associated to the box
538 /// (called after bbProcess during bbExecute)
539 void WxBlackBox::bbShowWindow()
541 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
542 <<bbGetFullName()<<"]"<<std::endl);
544 // If Event Handler for the widget does not exist or is obsolete : create it
545 if (bbGetOutputWidget()!=0)
547 if (bbGetWidgetEventHandler()==0)
549 bbtkDebugMessage("wx",3,
550 "-> No widget event handler : creating one"
552 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
553 bbGetOutputWidget());
555 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
556 ( bbGetOutputWidget() ) )
558 bbtkDebugMessage("wx",3,
559 "-> Obsolete widget event handler : re-creating one"
561 delete bbGetWidgetEventHandler();
562 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
563 bbGetOutputWidget());
565 // Sets the name of the wxWindow to the input WinTitle
566 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
569 // If the output 'Widget' is connected then it's gonna
570 // be captured by its parent window : nothing to do
571 if ( (*bbGetOutputConnectorMap().find("Widget")).second
572 ->GetConnectionVector().size() != 0 )
575 bbtkDebugMessage("wx",2,
576 "-> Output 'Widget' connected : nothing to do"
583 // If the window already exists : no need creating it
584 if (bbGetWindow()!=0)
586 bbtkDebugMessage("wx",2,
587 "-> Window already exists"
589 show = bbGetWindow();
591 // Else if the widget exists : create window
592 else if (bbGetOutputWidget()!=0)
594 bbtkDebugMessage("wx",2,
595 "-> Widget exists : creating the window"
599 // Input WinDialog set to true : creating a Dialog
600 if (bbGetInputWinDialog())
602 bbtkDebugMessage("wx",2,
603 " Input WinDialog set to true : creating a Dialog"
605 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
607 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
608 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
610 // Input WinDialog set to false : creating a Frame
613 bbtkDebugMessage("process",2,
614 " Input WinDialog set to false : creating a Frame"
616 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
618 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
619 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
623 // No window nor widget : error
626 bbtkError("WxBlackBox::bbShowWindow() ["
628 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
633 if (true) //!show->IsShown())
639 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
643 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
644 <<bbGetFullName()<<"]"<<std::endl);
647 //==================================================================
652 //==================================================================
653 void WxBlackBox::bbHideWindow()
655 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
656 <<bbGetFullName()<<"]"<<std::endl);
658 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
660 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
661 <<bbGetFullName()<<"]"<<std::endl);
663 //==================================================================
666 //==================================================================
667 void WxBlackBox::bbCloseWindow()
669 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
670 <<bbGetFullName()<<"]"<<std::endl);
672 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
674 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbCloseWindow() ["
675 <<bbGetFullName()<<"]"<<std::endl);
677 //==================================================================
679 //==================================================================
680 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
682 if (bbGetWindow()!=0) return bbGetWindow();
683 BlackBox::OutputConnectorMapType::const_iterator i
684 = bbGetOutputConnectorMap().find("Widget");
685 if ( i->second->GetConnectionVector().size() != 0 )
687 return boost::static_pointer_cast<WxBlackBox>
688 (i->second->GetConnectionVector().front() //.lock()
689 ->GetBlackBoxTo())->bbGetContainingWindow();
693 //==================================================================
696 //==================================================================
697 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
698 //==================================================================
701 //==================================================================
702 bool WxBlackBox::bbIsShown()
704 if (bbGetContainingWindow()!=0)
705 return bbGetContainingWindow()->bbIsShown();
708 //==================================================================