1 /*=========================================================================
3 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
5 Date: $Date: 2008/12/08 14:02:15 $
6 Version: $Revision: 1.34 $
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 "bbtkBlackBoxOutputConnector.h"
44 //#include "bbtkWxContainerBlackBox.h"
45 #include <wx/dialog.h>
47 //#include "bbtkData.h"
48 //#include "bbtkFactory.h"
59 //=========================================================================
61 //=========================================================================
65 //=========================================================================
66 WxBlackBoxWindow::WxBlackBoxWindow(WxBlackBox::Pointer box)
67 : mBox(box), mShown(false)
69 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::WxBlackBoxWindow("<<
70 mBox.lock()->bbGetFullName()<<")"<<std::endl);
71 mBox.lock()->bbSetWindow(this);
72 Wx::IncNbWindowsAlive();
74 //=========================================================================
76 //=========================================================================
77 WxBlackBoxWindow::~WxBlackBoxWindow()
79 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::~WxBlackBoxWindow() "
82 Wx::DecNbWindowsAlive();
85 mBox.lock()->bbSetWindow(0);
88 //=========================================================================
91 //=========================================================================
92 void WxBlackBoxWindow::bbShow()
94 if (bbIsShown()) return;
95 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbShow()"<<std::endl);
96 Wx::IncNbWindowsShown();
99 //=========================================================================
101 //=========================================================================
102 void WxBlackBoxWindow::bbHide()
104 if (!bbIsShown()) return;
105 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbHide()"<<std::endl);
106 Wx::DecNbWindowsShown();
109 //=========================================================================
111 //=========================================================================
112 void WxBlackBoxWindow::bbClose()
115 //=========================================================================
118 //=========================================================================
120 //=========================================================================
122 //=========================================================================
123 WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
140 WxBlackBoxWindow(box)
142 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
143 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
144 <<title<<",size)"<<std::endl);
145 // Insert the widget into the window
146 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
147 // LG 22/11/08 : new widget pipeline
148 bbGetBlackBox()->bbCreateWidgetAndEventHandler(this);
149 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
150 // old : widget->Reparent(this);
151 sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
152 //SetAutoLayout(true);
156 //=========================================================================
158 //=========================================================================
159 void WxBlackBoxDialog::bbShow()
161 if (bbIsShown()) return;
162 bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
163 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
164 WxBlackBoxWindow::bbShow();
165 SetReturnCode( wxDialog::ShowModal() );
168 //=========================================================================
170 //=========================================================================
171 void WxBlackBoxDialog::bbHide()
173 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
174 WxBlackBoxWindow::bbHide();
177 //=========================================================================
179 //=========================================================================
180 void WxBlackBoxDialog::bbClose()
182 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
185 //=========================================================================
187 //=========================================================================
188 WxBlackBoxDialog::~WxBlackBoxDialog()
191 //=========================================================================
197 //=========================================================================
199 //=========================================================================
201 //=========================================================================
202 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
218 WxBlackBoxWindow(box)
220 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
221 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
222 <<title<<",size)"<<std::endl);
223 // Insert the widget into the window
224 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
225 // LG 22/11/08 : new widget pipeline
226 bbGetBlackBox()->bbCreateWidgetAndEventHandler(this);
227 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
228 wxFrame* frame = (wxFrame*)this;
229 // old : widget->Reparent(frame);
230 sizer->Add( widget, 1, wxALL|wxGROW, 2);
231 // frame->SetAutoLayout(true);
232 frame->SetSizer(sizer);
236 //=========================================================================
238 //=========================================================================
239 WxBlackBoxFrame::~WxBlackBoxFrame()
242 //=========================================================================
244 //=========================================================================
245 void WxBlackBoxFrame::bbShow()
247 if (bbIsShown()) return;
248 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
249 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
250 WxBlackBoxWindow::bbShow();
252 // This Update is ** MANDATORY **
253 // to synchronize wxvtkRenderWindowInteractor objects
254 // (force wx objects creation **NOW**)
263 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
265 //=========================================================================
267 //=========================================================================
268 void WxBlackBoxFrame::bbHide()
270 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
271 WxBlackBoxWindow::bbHide();
273 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
275 //=========================================================================
277 //=========================================================================
278 void WxBlackBoxFrame::bbClose()
280 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
283 //=========================================================================
287 //=========================================================================
288 // WxBlackBoxWidgetEventHandler
289 //=========================================================================
291 //=========================================================================
292 WxBlackBoxWidgetEventHandler::
293 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
299 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
301 mBox.lock()->bbSetWidgetEventHandler(this);
303 Connect ( mWindow->GetId(),
305 (wxObjectEventFunction)
306 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
307 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
309 mWindow->PushEventHandler(this);
312 //=========================================================================
314 //=========================================================================
315 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
317 if (mBox.expired()) return;
318 bbtkDebugMessage("wx",9,
319 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
320 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
321 mBox.lock()->bbSetWidgetEventHandler(0);
323 //=========================================================================
325 //=========================================================================
326 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
328 if (mBox.expired()) return;
329 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
330 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
331 mBox.lock()->bbSetOutputWidget(0);
333 // mBox.lock()->bbSetModifiedStatus();
335 //=========================================================================
343 //=========================================================================
345 //=========================================================================
347 //=========================================================================
348 //=========================================================================
349 //=========================================================================
350 //=========================================================================
351 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
352 //=========================================================================
354 //=========================================================================
355 void WxBlackBox::bbUserConstructor()
357 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
360 //=========================================================================
362 //=========================================================================
363 void WxBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
365 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
369 //=========================================================================
372 //=========================================================================
373 void WxBlackBox::bbUserDestructor()
375 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
377 delete bbGetWindow();
380 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
382 //=========================================================================
386 //=========================================================================
387 WxBlackBox::Widget* WxBlackBox::bbGetWidget()
389 if (bbGetOutputWidget() && bbGetOutputWidget()->IsDead())
391 bbtkDebugMessage("wx",9,"WxBlackBox::bbGetWidget() ["<<
392 bbGetFullName()<<"] : Widget is dead : deleting it"
394 delete bbGetOutputWidget();
395 bbSetOutputWidget(0);
397 return bbGetOutputWidget();
399 //=========================================================================
403 //=========================================================================
405 * \brief Initialize the attributes of the class
408 void WxBlackBox::bbInitAttributes()
412 // bbSetInputWinParent(0);
413 bbSetInputWinTitle(bbGetName());
414 bbSetInputWinWidth(800);
415 bbSetInputWinHeight(800);
416 bbSetInputWinDialog(false);
417 bbSetOutputWidget(0);
419 bbSetWidgetEventHandler(0);
420 // bbSetUpdateTransferedToParent(false);
422 //=========================================================================
424 //=========================================================================
425 /// Main processing method of the box.
426 void WxBlackBox::bbExecute(bool force)
429 bbtkDebugMessageInc("process",2,
430 "=> WxBlackBox::bbExecute("<<(int)force<<") ["
431 <<bbGetFullName()<<"]"<<std::endl);
434 // If the output 'Widget' is connected then
435 // we must execute the parent box
437 BlackBox::OutputConnectorMapType::const_iterator i
438 = bbGetOutputConnectorMap().find("Widget");
440 if ( i->second->GetConnectionVector().size() != 0 )
442 bbtkWarning("Execution called on '"<<bbGetFullName()
443 <<"' although its Output 'Widget' is connected: "
444 <<"if the widget is not created yet then it will not be! "
445 <<"Execute the top level Layout widget to create and "
446 <<"display the widget.");
449 AtomicBlackBox::bbExecute(force);
451 bbtkDebugMessageDec("process",2,
452 "<= WxBlackBox::bbExecute() ["
453 <<bbGetFullName()<<"]"<<std::endl);
455 //=========================================================================
458 //=========================================================================
459 void WxBlackBox::bbProcess()
462 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
463 this->bbUserProcess();
465 // this->bbUserOnShow();
467 // LG 22/11/08 : new widget pipeline
468 // If output widget not connected :
469 if ( (*bbGetOutputConnectorMap().find("Widget")).second
470 ->GetConnectionVector().size() == 0 )
473 // If the window already exists : no need creating it
474 if (bbGetWindow()!=0)
476 bbtkDebugMessage("wx",2,
477 "-> Window already exists"
479 show = bbGetWindow();
481 // Else create window
484 bbtkDebugMessage("wx",2,
485 "-> Creating the window"
489 // Input WinDialog set to true : creating a Dialog
490 if (bbGetInputWinDialog())
492 bbtkDebugMessage("wx",2,
493 " Input WinDialog set to true : creating a Dialog"
495 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
497 // LG 24/11/08 : New widget pipeline
499 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
500 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
502 // Input WinDialog set to false : creating a Frame
505 bbtkDebugMessage("process",2,
506 " Input WinDialog set to false : creating a Frame"
508 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
510 // LG 24/11/08 : New widget pipeline
512 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
513 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
523 this->bbUserProcess();
526 //=========================================================================
530 // LG 24/11/08 : New widget pipeline
531 void WxBlackBox::bbCreateWidgetAndEventHandler(wxWindow* parent)
533 if (bbGetOutputWidget()==0)
535 this->bbUserCreateWidget(parent);
537 // If Event Handler for the widget does not exist or is obsolete : create it
538 if (bbGetOutputWidget()!=0)
540 if (bbGetWidgetEventHandler()==0)
542 bbtkDebugMessage("wx",3,
543 "-> No widget event handler : creating one"
545 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
546 bbGetOutputWidget());
548 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
549 ( bbGetOutputWidget() ) )
551 bbtkDebugMessage("wx",3,
552 "-> Obsolete widget event handler : re-creating one"
554 delete bbGetWidgetEventHandler();
555 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
556 bbGetOutputWidget());
558 // Sets the name of the wxWindow to the input WinTitle
559 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
567 wxWindow* WxBlackBox::bbCreateWidgetOfInput(const std::string& in, wxWindow* parent)
570 // If input is connected
571 BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
572 if ( c->IsConnected() )
574 // Get black box from
575 BlackBox::Pointer from =
576 c->GetConnection()->GetBlackBoxFrom();
577 // Cast it into a WxBlackBox
578 WxBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<WxBlackBox>(from);
579 // Call bbCreateWidgetAndEventHandler
580 wfrom->bbCreateWidgetAndEventHandler(parent);
581 // Get the widget created
582 w = wfrom->bbGetOutputWidget();
588 //==================================================================
589 /// Specific methods for window creation during pipeline execution
590 /// Shows the window associated to the box
591 /// (called after bbProcess during bbExecute)
592 void WxBlackBox::bbShowWindow()
594 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
595 <<bbGetFullName()<<"]"<<std::endl);
597 // If Event Handler for the widget does not exist or is obsolete : create it
598 if (bbGetOutputWidget()!=0)
600 if (bbGetWidgetEventHandler()==0)
602 bbtkDebugMessage("wx",3,
603 "-> No widget event handler : creating one"
605 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
606 bbGetOutputWidget());
608 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
609 ( bbGetOutputWidget() ) )
611 bbtkDebugMessage("wx",3,
612 "-> Obsolete widget event handler : re-creating one"
614 delete bbGetWidgetEventHandler();
615 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
616 bbGetOutputWidget());
618 // Sets the name of the wxWindow to the input WinTitle
619 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
622 // If the output 'Widget' is connected then it's gonna
623 // be captured by its parent window : nothing to do
624 if ( (*bbGetOutputConnectorMap().find("Widget")).second
625 ->GetConnectionVector().size() != 0 )
628 bbtkDebugMessage("wx",2,
629 "-> Output 'Widget' connected : nothing to do"
636 // If the window already exists : no need creating it
637 if (bbGetWindow()!=0)
639 bbtkDebugMessage("wx",2,
640 "-> Window already exists"
642 show = bbGetWindow();
644 // Else if the widget exists : create window
645 else if (bbGetOutputWidget()!=0)
647 bbtkDebugMessage("wx",2,
648 "-> Widget exists : creating the window"
652 // Input WinDialog set to true : creating a Dialog
653 if (bbGetInputWinDialog())
655 bbtkDebugMessage("wx",2,
656 " Input WinDialog set to true : creating a Dialog"
658 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
660 // LG 24/11/08 : New widget pipeline
662 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
663 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
665 // Input WinDialog set to false : creating a Frame
668 bbtkDebugMessage("process",2,
669 " Input WinDialog set to false : creating a Frame"
671 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
673 // LG 24/11/08 : New widget pipeline
675 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
676 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
680 // No window nor widget : error
683 bbtkError("WxBlackBox::bbShowWindow() ["
685 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
690 if (true) //!show->IsShown())
696 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
700 bbtkDebugMessage("wx",2,"<= WxBlackBox::bbShowWindow() ["
701 <<bbGetFullName()<<"]"<<std::endl);
704 //==================================================================
709 //==================================================================
710 void WxBlackBox::bbHideWindow()
712 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
713 <<bbGetFullName()<<"]"<<std::endl);
715 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
717 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbHideWindow() ["
718 <<bbGetFullName()<<"]"<<std::endl);
720 //==================================================================
723 //==================================================================
724 void WxBlackBox::bbCloseWindow()
726 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
727 <<bbGetFullName()<<"]"<<std::endl);
729 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
731 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbCloseWindow() ["
732 <<bbGetFullName()<<"]"<<std::endl);
734 //==================================================================
736 //==================================================================
737 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
739 if (bbGetWindow()!=0) return bbGetWindow();
740 BlackBox::OutputConnectorMapType::const_iterator i
741 = bbGetOutputConnectorMap().find("Widget");
742 if ( i->second->GetConnectionVector().size() != 0 )
744 return boost::static_pointer_cast<WxBlackBox>
745 (i->second->GetConnectionVector().front() //.lock()
746 ->GetBlackBoxTo())->bbGetContainingWindow();
750 //==================================================================
753 //==================================================================
754 // LG 24/11/08 : New widget pipeline
755 // wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
756 //==================================================================
759 //==================================================================
760 bool WxBlackBox::bbIsShown()
762 if (bbGetContainingWindow()!=0)
763 return bbGetContainingWindow()->bbIsShown();
766 //==================================================================