1 /*=========================================================================
3 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
5 Date: $Date: 2008/11/13 14:46:43 $
6 Version: $Revision: 1.28 $
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("<<(int)force<<") ["
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);
463 if ( ! (( bbGetStatus() == MODIFIED ) ||
464 ( bbBoxProcessModeIsAlways() )) )
466 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
467 bbtkDebugMessage("process",3,
468 "<= WxBlackBox::bbBackwardUpdate("
469 <<(caller?caller->GetFullName():"0")<<") ["
470 <<bbGetFullName()<<"]"<<std::endl);
471 return bbGetStatus();
474 // If the caller's box to is not the box to connected to the
477 BlackBox::OutputConnectorMapType::const_iterator i
478 = bbGetOutputConnectorMap().find("Widget") ;
479 if ( i->second->GetConnectionVector().size() != 0 )
482 BlackBox::Pointer to =
483 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
487 bbtkDebugMessage("process",3,
488 "-> Output 'Widget' connected to '"
489 <<to->bbGetFullName()<<"' - caller->to = '"
490 <<caller->GetBlackBoxTo()->bbGetFullName()
496 bbtkDebugMessage("process",3,
497 "-> Output 'Widget' connected to '"
498 <<to->bbGetFullName()<<"'"
503 (caller->GetBlackBoxTo() != to))&&
504 (!bbGetUpdateTransferedToParent())&&
505 (!to->bbGetExecuting()))
507 bbtkDebugMessage("process",3,
508 " ... Transfering update order to parent"
511 bbSetUpdateTransferedToParent(true);
512 i->second->GetConnectionVector().front() //.lock()
513 ->GetBlackBoxTo()->bbExecute(false);
517 bbSetUpdateTransferedToParent(false);
518 bbtkDebugMessage("process",3,
519 " ... No need to transfer to parent"
525 // If the caller is not the connection to the output widget
526 // and the output 'Widget' is connected then
527 // we must execute the parent box
529 // (this is the role of the flag UpdateTransferedToParent)
531 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
536 // call 'standard' BlackBox execution method
537 if (!bbGetUpdateTransferedToParent())
539 AtomicBlackBox::bbBackwardUpdate(caller);
542 bbtkDebugMessageDec("process",3,
543 "<= WxBlackBox::bbBackwardUpdate() ["
544 <<bbGetFullName()<<"]"<<std::endl);
546 return bbGetStatus();
551 //=========================================================================
552 void WxBlackBox::bbProcess()
554 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
555 this->bbUserProcess();
557 // this->bbUserOnShow();
559 //=========================================================================
561 //==================================================================
562 /// Specific methods for window creation during pipeline execution
563 /// Shows the window associated to the box
564 /// (called after bbProcess during bbExecute)
565 void WxBlackBox::bbShowWindow()
567 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
568 <<bbGetFullName()<<"]"<<std::endl);
570 // If Event Handler for the widget does not exist or is obsolete : create it
571 if (bbGetOutputWidget()!=0)
573 if (bbGetWidgetEventHandler()==0)
575 bbtkDebugMessage("wx",3,
576 "-> No widget event handler : creating one"
578 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
579 bbGetOutputWidget());
581 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
582 ( bbGetOutputWidget() ) )
584 bbtkDebugMessage("wx",3,
585 "-> Obsolete widget event handler : re-creating one"
587 delete bbGetWidgetEventHandler();
588 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
589 bbGetOutputWidget());
591 // Sets the name of the wxWindow to the input WinTitle
592 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
595 // If the output 'Widget' is connected then it's gonna
596 // be captured by its parent window : nothing to do
597 if ( (*bbGetOutputConnectorMap().find("Widget")).second
598 ->GetConnectionVector().size() != 0 )
601 bbtkDebugMessage("wx",2,
602 "-> Output 'Widget' connected : nothing to do"
609 // If the window already exists : no need creating it
610 if (bbGetWindow()!=0)
612 bbtkDebugMessage("wx",2,
613 "-> Window already exists"
615 show = bbGetWindow();
617 // Else if the widget exists : create window
618 else if (bbGetOutputWidget()!=0)
620 bbtkDebugMessage("wx",2,
621 "-> Widget exists : creating the window"
625 // Input WinDialog set to true : creating a Dialog
626 if (bbGetInputWinDialog())
628 bbtkDebugMessage("wx",2,
629 " Input WinDialog set to true : creating a Dialog"
631 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
633 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
634 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
636 // Input WinDialog set to false : creating a Frame
639 bbtkDebugMessage("process",2,
640 " Input WinDialog set to false : creating a Frame"
642 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
644 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
645 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
649 // No window nor widget : error
652 bbtkError("WxBlackBox::bbShowWindow() ["
654 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
659 if (true) //!show->IsShown())
665 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
669 bbtkDebugMessage("wx",2,"<= WxBlackBox::bbShowWindow() ["
670 <<bbGetFullName()<<"]"<<std::endl);
673 //==================================================================
678 //==================================================================
679 void WxBlackBox::bbHideWindow()
681 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
682 <<bbGetFullName()<<"]"<<std::endl);
684 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
686 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbHideWindow() ["
687 <<bbGetFullName()<<"]"<<std::endl);
689 //==================================================================
692 //==================================================================
693 void WxBlackBox::bbCloseWindow()
695 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
696 <<bbGetFullName()<<"]"<<std::endl);
698 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
700 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbCloseWindow() ["
701 <<bbGetFullName()<<"]"<<std::endl);
703 //==================================================================
705 //==================================================================
706 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
708 if (bbGetWindow()!=0) return bbGetWindow();
709 BlackBox::OutputConnectorMapType::const_iterator i
710 = bbGetOutputConnectorMap().find("Widget");
711 if ( i->second->GetConnectionVector().size() != 0 )
713 return boost::static_pointer_cast<WxBlackBox>
714 (i->second->GetConnectionVector().front() //.lock()
715 ->GetBlackBoxTo())->bbGetContainingWindow();
719 //==================================================================
722 //==================================================================
723 wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
724 //==================================================================
727 //==================================================================
728 bool WxBlackBox::bbIsShown()
730 if (bbGetContainingWindow()!=0)
731 return bbGetContainingWindow()->bbIsShown();
734 //==================================================================