1 /*=========================================================================
3 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
5 Date: $Date: 2008/10/17 08:18:14 $
6 Version: $Revision: 1.27 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
32 #ifdef _USE_WXWIDGETS_
41 #include "bbtkWxBlackBox.h"
42 //#include "bbtkWxContainerBlackBox.h"
43 #include <wx/dialog.h>
45 //#include "bbtkData.h"
46 //#include "bbtkFactory.h"
57 //=========================================================================
59 //=========================================================================
63 //=========================================================================
64 WxBlackBoxWindow::WxBlackBoxWindow(WxBlackBox::Pointer box)
65 : mBox(box), mShown(false)
67 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::WxBlackBoxWindow("<<
68 mBox.lock()->bbGetFullName()<<")"<<std::endl);
69 mBox.lock()->bbSetWindow(this);
70 Wx::IncNbWindowsAlive();
72 //=========================================================================
74 //=========================================================================
75 WxBlackBoxWindow::~WxBlackBoxWindow()
77 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::~WxBlackBoxWindow() "
80 Wx::DecNbWindowsAlive();
83 mBox.lock()->bbSetWindow(0);
86 //=========================================================================
89 //=========================================================================
90 void WxBlackBoxWindow::bbShow()
92 if (bbIsShown()) return;
93 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbShow()"<<std::endl);
94 Wx::IncNbWindowsShown();
97 //=========================================================================
99 //=========================================================================
100 void WxBlackBoxWindow::bbHide()
102 if (!bbIsShown()) return;
103 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbHide()"<<std::endl);
104 Wx::DecNbWindowsShown();
107 //=========================================================================
109 //=========================================================================
110 void WxBlackBoxWindow::bbClose()
113 //=========================================================================
116 //=========================================================================
118 //=========================================================================
120 //=========================================================================
121 WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
138 WxBlackBoxWindow(box)
140 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
141 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
142 <<title<<",size)"<<std::endl);
143 // Insert the widget into the window
144 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
145 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
146 widget->Reparent(this);
147 sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
148 //SetAutoLayout(true);
152 //=========================================================================
154 //=========================================================================
155 void WxBlackBoxDialog::bbShow()
157 if (bbIsShown()) return;
158 bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
159 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
160 WxBlackBoxWindow::bbShow();
161 SetReturnCode( wxDialog::ShowModal() );
164 //=========================================================================
166 //=========================================================================
167 void WxBlackBoxDialog::bbHide()
169 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
170 WxBlackBoxWindow::bbHide();
173 //=========================================================================
175 //=========================================================================
176 void WxBlackBoxDialog::bbClose()
178 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
181 //=========================================================================
183 //=========================================================================
184 WxBlackBoxDialog::~WxBlackBoxDialog()
187 //=========================================================================
193 //=========================================================================
195 //=========================================================================
197 //=========================================================================
198 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
214 WxBlackBoxWindow(box)
216 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
217 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
218 <<title<<",size)"<<std::endl);
219 // Insert the widget into the window
220 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
221 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
222 wxFrame* frame = (wxFrame*)this;
223 widget->Reparent(frame);
224 sizer->Add( widget, 1, wxALL|wxGROW, 2);
225 // frame->SetAutoLayout(true);
226 frame->SetSizer(sizer);
230 //=========================================================================
232 //=========================================================================
233 WxBlackBoxFrame::~WxBlackBoxFrame()
236 //=========================================================================
238 //=========================================================================
239 void WxBlackBoxFrame::bbShow()
241 if (bbIsShown()) return;
242 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
243 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
244 WxBlackBoxWindow::bbShow();
246 // This Update is ** MANDATORY **
247 // to synchronize wxvtkRenderWindowInteractor objects
248 // (force wx objects creation **NOW**)
257 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
259 //=========================================================================
261 //=========================================================================
262 void WxBlackBoxFrame::bbHide()
264 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
265 WxBlackBoxWindow::bbHide();
267 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
269 //=========================================================================
271 //=========================================================================
272 void WxBlackBoxFrame::bbClose()
274 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
277 //=========================================================================
281 //=========================================================================
282 // WxBlackBoxWidgetEventHandler
283 //=========================================================================
285 //=========================================================================
286 WxBlackBoxWidgetEventHandler::
287 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
293 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
295 mBox.lock()->bbSetWidgetEventHandler(this);
297 Connect ( mWindow->GetId(),
299 (wxObjectEventFunction)
300 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
301 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
303 mWindow->PushEventHandler(this);
306 //=========================================================================
308 //=========================================================================
309 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
311 if (mBox.expired()) return;
312 bbtkDebugMessage("wx",9,
313 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
314 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
315 mBox.lock()->bbSetWidgetEventHandler(0);
317 //=========================================================================
319 //=========================================================================
320 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
322 if (mBox.expired()) return;
323 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
324 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
325 mBox.lock()->bbSetOutputWidget(0);
326 mBox.lock()->bbSetModifiedStatus();
328 //=========================================================================
336 //=========================================================================
338 //=========================================================================
340 //=========================================================================
341 //=========================================================================
342 //=========================================================================
343 //=========================================================================
344 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
345 //=========================================================================
347 //=========================================================================
348 void WxBlackBox::bbUserConstructor()
350 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
353 //=========================================================================
355 //=========================================================================
356 void WxBlackBox::bbUserCopyConstructor()
358 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
362 //=========================================================================
365 //=========================================================================
366 void WxBlackBox::bbUserDestructor()
368 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
370 delete bbGetWindow();
373 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
375 //=========================================================================
379 //=========================================================================
380 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
382 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
384 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
385 bbGetFullName()<<"] : Widget is dead : deleting it"
387 delete bbGetOutputWidget();
388 bbSetOutputWidget(0);
390 return bbGetOutputWidget();
392 //=========================================================================
396 //=========================================================================
398 * \brief Initialize the attributes of the class
401 void WxBlackBox::bbInitAttributes()
405 // bbSetInputWinParent(0);
406 bbSetInputWinTitle(bbGetName());
407 bbSetInputWinWidth(800);
408 bbSetInputWinHeight(800);
409 bbSetInputWinDialog(false);
410 bbSetOutputWidget(0);
412 bbSetWidgetEventHandler(0);
413 bbSetUpdateTransferedToParent(false);
415 //=========================================================================
417 //=========================================================================
418 /// Main processing method of the box.
419 void WxBlackBox::bbExecute(bool force)
421 bbtkDebugMessageInc("process",2,
422 "=> WxBlackBox::bbExecute() ["
423 <<bbGetFullName()<<"]"<<std::endl);
425 // If the output 'Widget' is connected then
426 // we must execute the parent box
427 BlackBox::OutputConnectorMapType::const_iterator i
428 = bbGetOutputConnectorMap().find("Widget");
430 if ( i->second->GetConnectionVector().size() != 0 )
432 bbtkDebugMessage("process",3,
433 "-> Output 'Widget' connected : transfering execution to parent"
436 i->second->GetConnectionVector().front() //.lock()
437 ->GetBlackBoxTo()->bbExecute(force);
440 // else call 'standard' BlackBox execution method
443 BlackBox::bbExecute(force);
447 bbtkDebugMessageDec("process",2,
448 "<= WxBlackBox::bbExecute() ["
449 <<bbGetFullName()<<"]"<<std::endl);
451 //=========================================================================
454 //=========================================================================
455 /// Main processing method of the box.
456 IOStatus WxBlackBox::bbBackwardUpdate( Connection::Pointer caller )
458 bbtkDebugMessage("process",3,
459 "=> WxBlackBox::bbBackwardUpdate("
460 <<(caller?caller->GetFullName():"0")<<") ["
461 <<bbGetFullName()<<"]"<<std::endl);
462 // If the caller's box to is not the box to connected to the
464 if ( ! (( bbGetStatus() == MODIFIED ) ||
465 ( bbBoxProcessModeIsAlways() )) )
467 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
468 bbtkDebugMessage("process",3,
469 "<= WxBlackBox::bbBackwardUpdate("
470 <<(caller?caller->GetFullName():"0")<<") ["
471 <<bbGetFullName()<<"]"<<std::endl);
472 return bbGetStatus();
475 BlackBox::OutputConnectorMapType::const_iterator i
476 = bbGetOutputConnectorMap().find("Widget") ;
477 if ( i->second->GetConnectionVector().size() != 0 )
480 BlackBox::Pointer to =
481 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
485 bbtkDebugMessage("process",3,
486 "-> Output 'Widget' connected to '"
487 <<to->bbGetFullName()<<"' - caller->to = '"
488 <<caller->GetBlackBoxTo()->bbGetFullName()
494 bbtkDebugMessage("process",3,
495 "-> Output 'Widget' connected to '"
496 <<to->bbGetFullName()<<"'"
501 (caller->GetBlackBoxTo() != to))&&
502 (!bbGetUpdateTransferedToParent())&&
503 (to->bbGetStatus()!=UPDATING))
505 bbtkDebugMessage("process",3,
506 " ... Transfering update order to parent"
509 bbSetUpdateTransferedToParent(true);
510 i->second->GetConnectionVector().front() //.lock()
511 ->GetBlackBoxTo()->bbExecute(false);
515 bbSetUpdateTransferedToParent(false);
516 bbtkDebugMessage("process",3,
517 " ... No need to transfer to parent"
523 // If the caller is not the connection to the output widget
524 // and the output 'Widget' is connected then
525 // we must execute the parent box
527 // (this is the role of the flag UpdateTransferedToParent)
529 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
534 // call 'standard' BlackBox execution method
535 if (!bbGetUpdateTransferedToParent())
537 AtomicBlackBox::bbBackwardUpdate(caller);
540 bbtkDebugMessageDec("process",3,
541 "<= WxBlackBox::bbBackwardUpdate() ["
542 <<bbGetFullName()<<"]"<<std::endl);
544 return bbGetStatus();
549 //=========================================================================
550 void WxBlackBox::bbProcess()
552 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
553 this->bbUserProcess();
555 // this->bbUserOnShow();
557 //=========================================================================
559 //==================================================================
560 /// Specific methods for window creation during pipeline execution
561 /// Shows the window associated to the box
562 /// (called after bbProcess during bbExecute)
563 void WxBlackBox::bbShowWindow()
565 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
566 <<bbGetFullName()<<"]"<<std::endl);
568 // If Event Handler for the widget does not exist or is obsolete : create it
569 if (bbGetOutputWidget()!=0)
571 if (bbGetWidgetEventHandler()==0)
573 bbtkDebugMessage("wx",3,
574 "-> No widget event handler : creating one"
576 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
577 bbGetOutputWidget());
579 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
580 ( bbGetOutputWidget() ) )
582 bbtkDebugMessage("wx",3,
583 "-> Obsolete widget event handler : re-creating one"
585 delete bbGetWidgetEventHandler();
586 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
587 bbGetOutputWidget());
589 // Sets the name of the wxWindow to the input WinTitle
590 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
593 // If the output 'Widget' is connected then it's gonna
594 // be captured by its parent window : nothing to do
595 if ( (*bbGetOutputConnectorMap().find("Widget")).second
596 ->GetConnectionVector().size() != 0 )
599 bbtkDebugMessage("wx",2,
600 "-> Output 'Widget' connected : nothing to do"
607 // If the window already exists : no need creating it
608 if (bbGetWindow()!=0)
610 bbtkDebugMessage("wx",2,
611 "-> Window already exists"
613 show = bbGetWindow();
615 // Else if the widget exists : create window
616 else if (bbGetOutputWidget()!=0)
618 bbtkDebugMessage("wx",2,
619 "-> Widget exists : creating the window"
623 // Input WinDialog set to true : creating a Dialog
624 if (bbGetInputWinDialog())
626 bbtkDebugMessage("wx",2,
627 " Input WinDialog set to true : creating a Dialog"
629 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
631 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
632 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
634 // Input WinDialog set to false : creating a Frame
637 bbtkDebugMessage("process",2,
638 " Input WinDialog set to false : creating a Frame"
640 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
642 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
643 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
647 // No window nor widget : error
650 bbtkError("WxBlackBox::bbShowWindow() ["
652 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
657 if (true) //!show->IsShown())
663 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
667 bbtkDebugMessage("wx",1,"<= WxBlackBox::bbShowWindow() ["
668 <<bbGetFullName()<<"]"<<std::endl);
671 //==================================================================
676 //==================================================================
677 void WxBlackBox::bbHideWindow()
679 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
680 <<bbGetFullName()<<"]"<<std::endl);
682 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
684 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbHideWindow() ["
685 <<bbGetFullName()<<"]"<<std::endl);
687 //==================================================================
690 //==================================================================
691 void WxBlackBox::bbCloseWindow()
693 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
694 <<bbGetFullName()<<"]"<<std::endl);
696 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
698 bbtkDebugMessageDec("wx",1,"<= WxBlackBox::bbCloseWindow() ["
699 <<bbGetFullName()<<"]"<<std::endl);
701 //==================================================================
703 //==================================================================
704 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
706 if (bbGetWindow()!=0) return bbGetWindow();
707 BlackBox::OutputConnectorMapType::const_iterator i
708 = bbGetOutputConnectorMap().find("Widget");
709 if ( i->second->GetConnectionVector().size() != 0 )
711 return boost::static_pointer_cast<WxBlackBox>
712 (i->second->GetConnectionVector().front() //.lock()
713 ->GetBlackBoxTo())->bbGetContainingWindow();
717 //==================================================================
720 //==================================================================
721 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
722 //==================================================================
725 //==================================================================
726 bool WxBlackBox::bbIsShown()
728 if (bbGetContainingWindow()!=0)
729 return bbGetContainingWindow()->bbIsShown();
732 //==================================================================