1 /*=========================================================================
3 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
5 Date: $Date: 2008/12/08 12:54:27 $
6 Version: $Revision: 1.33 $
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
436 BlackBox::OutputConnectorMapType::const_iterator i
437 = bbGetOutputConnectorMap().find("Widget");
439 if ( i->second->GetConnectionVector().size() != 0 )
441 bbtkWarning("Execution called on '"<<bbGetFullName()
442 <<"' although its Output 'Widget' is connected: "
443 <<"if the widget is not created yet then it will not be! "
444 <<"Execute the top level Layout widget to create and "
445 <<"display the widget.");
448 AtomicBlackBox::bbExecute(force);
450 bbtkDebugMessageDec("process",2,
451 "<= WxBlackBox::bbExecute() ["
452 <<bbGetFullName()<<"]"<<std::endl);
454 //=========================================================================
457 //=========================================================================
458 void WxBlackBox::bbProcess()
461 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
462 this->bbUserProcess();
464 // this->bbUserOnShow();
466 // LG 22/11/08 : new widget pipeline
467 // If output widget not connected :
468 if ( (*bbGetOutputConnectorMap().find("Widget")).second
469 ->GetConnectionVector().size() == 0 )
472 // If the window already exists : no need creating it
473 if (bbGetWindow()!=0)
475 bbtkDebugMessage("wx",2,
476 "-> Window already exists"
478 show = bbGetWindow();
480 // Else create window
483 bbtkDebugMessage("wx",2,
484 "-> Creating the window"
488 // Input WinDialog set to true : creating a Dialog
489 if (bbGetInputWinDialog())
491 bbtkDebugMessage("wx",2,
492 " Input WinDialog set to true : creating a Dialog"
494 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
496 // LG 24/11/08 : New widget pipeline
498 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
499 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
501 // Input WinDialog set to false : creating a Frame
504 bbtkDebugMessage("process",2,
505 " Input WinDialog set to false : creating a Frame"
507 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
509 // LG 24/11/08 : New widget pipeline
511 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
512 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
522 this->bbUserProcess();
525 //=========================================================================
529 // LG 24/11/08 : New widget pipeline
530 void WxBlackBox::bbCreateWidgetAndEventHandler(wxWindow* parent)
532 if (bbGetOutputWidget()==0)
534 this->bbUserCreateWidget(parent);
536 // If Event Handler for the widget does not exist or is obsolete : create it
537 if (bbGetOutputWidget()!=0)
539 if (bbGetWidgetEventHandler()==0)
541 bbtkDebugMessage("wx",3,
542 "-> No widget event handler : creating one"
544 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
545 bbGetOutputWidget());
547 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
548 ( bbGetOutputWidget() ) )
550 bbtkDebugMessage("wx",3,
551 "-> Obsolete widget event handler : re-creating one"
553 delete bbGetWidgetEventHandler();
554 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
555 bbGetOutputWidget());
557 // Sets the name of the wxWindow to the input WinTitle
558 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
566 wxWindow* WxBlackBox::bbCreateWidgetOfInput(const std::string& in, wxWindow* parent)
569 // If input is connected
570 BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
571 if ( c->IsConnected() )
573 // Get black box from
574 BlackBox::Pointer from =
575 c->GetConnection()->GetBlackBoxFrom();
576 // Cast it into a WxBlackBox
577 WxBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<WxBlackBox>(from);
578 // Call bbCreateWidgetAndEventHandler
579 wfrom->bbCreateWidgetAndEventHandler(parent);
580 // Get the widget created
581 w = wfrom->bbGetOutputWidget();
587 //==================================================================
588 /// Specific methods for window creation during pipeline execution
589 /// Shows the window associated to the box
590 /// (called after bbProcess during bbExecute)
591 void WxBlackBox::bbShowWindow()
593 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbShowWindow() ["
594 <<bbGetFullName()<<"]"<<std::endl);
596 // If Event Handler for the widget does not exist or is obsolete : create it
597 if (bbGetOutputWidget()!=0)
599 if (bbGetWidgetEventHandler()==0)
601 bbtkDebugMessage("wx",3,
602 "-> No widget event handler : creating one"
604 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
605 bbGetOutputWidget());
607 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
608 ( bbGetOutputWidget() ) )
610 bbtkDebugMessage("wx",3,
611 "-> Obsolete widget event handler : re-creating one"
613 delete bbGetWidgetEventHandler();
614 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
615 bbGetOutputWidget());
617 // Sets the name of the wxWindow to the input WinTitle
618 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
621 // If the output 'Widget' is connected then it's gonna
622 // be captured by its parent window : nothing to do
623 if ( (*bbGetOutputConnectorMap().find("Widget")).second
624 ->GetConnectionVector().size() != 0 )
627 bbtkDebugMessage("wx",2,
628 "-> Output 'Widget' connected : nothing to do"
635 // If the window already exists : no need creating it
636 if (bbGetWindow()!=0)
638 bbtkDebugMessage("wx",2,
639 "-> Window already exists"
641 show = bbGetWindow();
643 // Else if the widget exists : create window
644 else if (bbGetOutputWidget()!=0)
646 bbtkDebugMessage("wx",2,
647 "-> Widget exists : creating the window"
651 // Input WinDialog set to true : creating a Dialog
652 if (bbGetInputWinDialog())
654 bbtkDebugMessage("wx",2,
655 " Input WinDialog set to true : creating a Dialog"
657 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
659 // LG 24/11/08 : New widget pipeline
661 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
662 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
664 // Input WinDialog set to false : creating a Frame
667 bbtkDebugMessage("process",2,
668 " Input WinDialog set to false : creating a Frame"
670 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
672 // LG 24/11/08 : New widget pipeline
674 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
675 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
679 // No window nor widget : error
682 bbtkError("WxBlackBox::bbShowWindow() ["
684 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
689 if (true) //!show->IsShown())
695 bbtkDebugMessage("wx",2,"-> Already shown : nothing to do"<<std::endl);
699 bbtkDebugMessage("wx",2,"<= WxBlackBox::bbShowWindow() ["
700 <<bbGetFullName()<<"]"<<std::endl);
703 //==================================================================
708 //==================================================================
709 void WxBlackBox::bbHideWindow()
711 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
712 <<bbGetFullName()<<"]"<<std::endl);
714 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
716 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbHideWindow() ["
717 <<bbGetFullName()<<"]"<<std::endl);
719 //==================================================================
722 //==================================================================
723 void WxBlackBox::bbCloseWindow()
725 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
726 <<bbGetFullName()<<"]"<<std::endl);
728 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
730 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbCloseWindow() ["
731 <<bbGetFullName()<<"]"<<std::endl);
733 //==================================================================
735 //==================================================================
736 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
738 if (bbGetWindow()!=0) return bbGetWindow();
739 BlackBox::OutputConnectorMapType::const_iterator i
740 = bbGetOutputConnectorMap().find("Widget");
741 if ( i->second->GetConnectionVector().size() != 0 )
743 return boost::static_pointer_cast<WxBlackBox>
744 (i->second->GetConnectionVector().front() //.lock()
745 ->GetBlackBoxTo())->bbGetContainingWindow();
749 //==================================================================
752 //==================================================================
753 // LG 24/11/08 : New widget pipeline
754 // wxWindow* WxBlackBox::bbGetWxParent() { return Wx::GetTopWindow(); }
755 //==================================================================
758 //==================================================================
759 bool WxBlackBox::bbIsShown()
761 if (bbGetContainingWindow()!=0)
762 return bbGetContainingWindow()->bbIsShown();
765 //==================================================================