1 /*=========================================================================
3 Module: $RCSfile: bbtkWxBlackBox.cxx,v $
5 Date: $Date: 2008/12/12 12:56:28 $
6 Version: $Revision: 1.36 $
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();
98 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
100 //=========================================================================
102 //=========================================================================
103 void WxBlackBoxWindow::bbHide()
105 if (!bbIsShown()) return;
106 bbtkDebugMessage("wx",9,"WxBlackBoxWindow::bbHide()"<<std::endl);
107 Wx::DecNbWindowsShown();
110 //=========================================================================
112 //=========================================================================
113 void WxBlackBoxWindow::bbClose()
116 //=========================================================================
119 //=========================================================================
121 //=========================================================================
123 //=========================================================================
124 WxBlackBoxDialog::WxBlackBoxDialog(WxBlackBox::Pointer box,
141 WxBlackBoxWindow(box)
143 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::WxBlackBoxDialog("<<
144 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
145 <<title<<",size)"<<std::endl);
146 // Insert the widget into the window
147 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
148 // LG 22/11/08 : new widget pipeline
149 bbGetBlackBox()->bbCreateWidgetAndEventHandler(this);
150 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
151 // old : widget->Reparent(this);
152 sizer->Add( widget, 1, wxALL|wxEXPAND, 2);
153 //SetAutoLayout(true);
157 //=========================================================================
159 //=========================================================================
160 void WxBlackBoxDialog::bbShow()
162 if (bbIsShown()) return;
163 bbtkDebugMessage("wx",5,"WxBlackBoxDialog::bbShow() ["
164 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
165 WxBlackBoxWindow::bbShow();
166 SetReturnCode( wxDialog::ShowModal() );
169 //=========================================================================
171 //=========================================================================
172 void WxBlackBoxDialog::bbHide()
174 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbHide()"<<std::endl);
175 WxBlackBoxWindow::bbHide();
178 //=========================================================================
180 //=========================================================================
181 void WxBlackBoxDialog::bbClose()
183 bbtkDebugMessage("wx",9,"WxBlackBoxDialog::bbClose()"<<std::endl);
186 //=========================================================================
188 //=========================================================================
189 WxBlackBoxDialog::~WxBlackBoxDialog()
192 //=========================================================================
198 //=========================================================================
200 //=========================================================================
202 //=========================================================================
203 WxBlackBoxFrame::WxBlackBoxFrame(WxBlackBox::Pointer box,
219 WxBlackBoxWindow(box)
221 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::WxBlackBoxFrame("<<
222 bbGetBlackBox()->bbGetFullName()<<","<<parent<<","
223 <<title<<",size)"<<std::endl);
224 // Insert the widget into the window
225 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
226 // LG 22/11/08 : new widget pipeline
227 bbGetBlackBox()->bbCreateWidgetAndEventHandler(this);
228 wxWindow* widget = bbGetBlackBox()->bbGetOutputWidget();
229 wxFrame* frame = (wxFrame*)this;
230 // old : widget->Reparent(frame);
231 sizer->Add( widget, 1, wxALL|wxGROW, 2);
232 // frame->SetAutoLayout(true);
233 frame->SetSizer(sizer);
237 //=========================================================================
239 //=========================================================================
240 WxBlackBoxFrame::~WxBlackBoxFrame()
243 //=========================================================================
245 //=========================================================================
246 void WxBlackBoxFrame::bbShow()
248 if (bbIsShown()) return;
249 bbtkDebugMessage("wx",5,"WxBlackBoxFrame::bbShow("
250 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
251 WxBlackBoxWindow::bbShow();
253 // This Update is ** MANDATORY **
254 // to synchronize wxvtkRenderWindowInteractor objects
255 // (force wx objects creation **NOW**)
264 // if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
266 //=========================================================================
268 //=========================================================================
269 void WxBlackBoxFrame::bbHide()
271 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbHide()"<<std::endl);
272 WxBlackBoxWindow::bbHide();
274 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
276 //=========================================================================
278 //=========================================================================
279 void WxBlackBoxFrame::bbClose()
281 bbtkDebugMessage("wx",9,"WxBlackBoxFrame::bbClose()"<<std::endl);
284 //=========================================================================
288 //=========================================================================
289 // WxBlackBoxWidgetEventHandler
290 //=========================================================================
292 //=========================================================================
293 WxBlackBoxWidgetEventHandler::
294 WxBlackBoxWidgetEventHandler( WxBlackBox::Pointer box,
300 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::WxBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
302 mBox.lock()->bbSetWidgetEventHandler(this);
304 Connect ( mWindow->GetId(),
306 (wxObjectEventFunction)
307 (void (wxEvtHandler::*)(wxWindowDestroyEvent& c))
308 &WxBlackBoxWidgetEventHandler::OnWindowDestroy );
310 mWindow->PushEventHandler(this);
313 //=========================================================================
315 //=========================================================================
316 WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler()
318 if (mBox.expired()) return;
319 bbtkDebugMessage("wx",9,
320 "WxBlackBoxWidgetEventHandler::~WxBlackBoxWidgetEventHandler() ["
321 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
322 mBox.lock()->bbSetWidgetEventHandler(0);
324 //=========================================================================
326 //=========================================================================
327 void WxBlackBoxWidgetEventHandler::OnWindowDestroy(wxWindowDestroyEvent&)
329 if (mBox.expired()) return;
330 bbtkDebugMessage("wx",9,"WxBlackBoxWidgetEventHandler::OnWindowDestroy() ["
331 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
332 mBox.lock()->bbSetOutputWidget(0);
334 // mBox.lock()->bbSetModifiedStatus();
336 //=========================================================================
344 //=========================================================================
346 //=========================================================================
348 //=========================================================================
349 //=========================================================================
350 //=========================================================================
351 //=========================================================================
352 BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,AtomicBlackBox);
353 //=========================================================================
355 //=========================================================================
356 void WxBlackBox::bbUserConstructor()
358 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserConstructor()"<<std::endl);
361 //=========================================================================
363 //=========================================================================
364 void WxBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
366 bbtkDebugMessage("Kernel",9,"WxBlackBox::bbUserCopyConstructor()"
370 //=========================================================================
373 //=========================================================================
374 void WxBlackBox::bbUserDestructor()
376 bbtkDebugMessage("wx",9,"==> WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
378 delete bbGetWindow();
381 bbtkDebugMessage("wx",9,"<== WxBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
383 //=========================================================================
386 //=========================================================================
388 * \brief Initialize the attributes of the class
391 void WxBlackBox::bbInitAttributes()
395 // bbSetInputWinParent(0);
396 bbSetInputWinTitle(bbGetName());
397 bbSetInputWinWidth(800);
398 bbSetInputWinHeight(800);
399 bbSetInputWinDialog(false);
400 bbSetOutputWidget(0);
402 bbSetWidgetEventHandler(0);
403 // bbSetUpdateTransferedToParent(false);
405 //=========================================================================
407 //=========================================================================
408 void WxBlackBox::bbProcess()
410 // If output widget not connected : have to create and show the window
411 if ( (*bbGetOutputConnectorMap().find("Widget")).second
412 ->GetConnectionVector().size() == 0 )
415 // If the window already exists : no need creating it
416 if (bbGetWindow()!=0)
418 bbtkDebugMessage("wx",2,
419 "-> Window already exists"
421 show = bbGetWindow();
423 // Else create window
426 bbtkDebugMessage("wx",2,
427 "-> Creating the window"
430 // Input WinDialog set to true : creating a Dialog
431 if (bbGetInputWinDialog())
433 bbtkDebugMessage("wx",2,
434 " Input WinDialog set to true : creating a Dialog"
436 show = (Window*) new WxBlackBoxDialog( GetThisPointer<WxBlackBox>(),
438 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
439 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
441 // Input WinDialog set to false : creating a Frame
444 bbtkDebugMessage("process",2,
445 " Input WinDialog set to false : creating a Frame"
447 show = (Window*) new WxBlackBoxFrame( GetThisPointer<WxBlackBox>(),
449 std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
450 wxSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
460 this->bbUserProcess();
463 //=========================================================================
467 //=========================================================================
468 void WxBlackBox::bbCreateWidgetAndEventHandler(wxWindow* parent)
470 if (bbGetOutputWidget()==0)
472 this->bbUserCreateWidget(parent);
474 // If Event Handler for the widget does not exist or is obsolete : create it
475 if (bbGetOutputWidget()!=0)
477 if (bbGetWidgetEventHandler()==0)
479 bbtkDebugMessage("wx",3,
480 "-> No widget event handler : creating one"
482 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
483 bbGetOutputWidget());
485 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
486 ( bbGetOutputWidget() ) )
488 bbtkDebugMessage("wx",3,
489 "-> Obsolete widget event handler : re-creating one"
491 delete bbGetWidgetEventHandler();
492 new WxBlackBoxWidgetEventHandler(GetThisPointer<WxBlackBox>(),
493 bbGetOutputWidget());
495 // Sets the name of the wxWindow to the input WinTitle
496 bbGetOutputWidget()->SetName(bbtk::std2wx(bbGetInputWinTitle()));
501 //=========================================================================
504 //=========================================================================
505 wxWindow* WxBlackBox::bbCreateWidgetOfInput(const std::string& in,
509 // If input is connected
510 BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
511 if ( c->IsConnected() )
513 // Get black box from
514 BlackBox::Pointer from =
515 c->GetConnection()->GetBlackBoxFrom();
516 // Cast it into a WxBlackBox
517 WxBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<WxBlackBox>(from);
518 // Call bbCreateWidgetAndEventHandler
519 wfrom->bbCreateWidgetAndEventHandler(parent);
520 // Get the widget created
521 w = wfrom->bbGetOutputWidget();
525 //==================================================================
528 //==================================================================
529 void WxBlackBox::bbHideWindow()
531 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbHideWindow() ["
532 <<bbGetFullName()<<"]"<<std::endl);
534 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
536 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbHideWindow() ["
537 <<bbGetFullName()<<"]"<<std::endl);
539 //==================================================================
542 //==================================================================
543 void WxBlackBox::bbCloseWindow()
545 bbtkDebugMessageInc("wx",1,"=> WxBlackBox::bbCloseWindow() ["
546 <<bbGetFullName()<<"]"<<std::endl);
548 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
550 bbtkDebugMessageDec("wx",2,"<= WxBlackBox::bbCloseWindow() ["
551 <<bbGetFullName()<<"]"<<std::endl);
553 //==================================================================
555 //==================================================================
556 WxBlackBox::Window* WxBlackBox::bbGetContainingWindow()
558 if (bbGetWindow()!=0) return bbGetWindow();
559 BlackBox::OutputConnectorMapType::const_iterator i
560 = bbGetOutputConnectorMap().find("Widget");
561 if ( i->second->GetConnectionVector().size() != 0 )
563 return boost::static_pointer_cast<WxBlackBox>
564 (i->second->GetConnectionVector().front() //.lock()
565 ->GetBlackBoxTo())->bbGetContainingWindow();
569 //==================================================================
572 //==================================================================
573 bool WxBlackBox::bbIsShown()
575 if (bbGetContainingWindow()!=0)
576 return bbGetContainingWindow()->bbIsShown();
579 //==================================================================