]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxBlackBox.cxx
#3296 BBTK Bug New Normal - Error windows Close application
[bbtk.git] / kernel / src / bbtkWxBlackBox.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkWxBlackBox.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.45 $
34 =========================================================================*/
35
36
37
38 #ifdef _USE_WXWIDGETS_
39
40 /**
41  *  \file 
42  *  \brief 
43  */
44  
45 #include "bbtkWxBlackBox.h"
46 #include "bbtkBlackBoxOutputConnector.h"
47
48 //#include "bbtkWxContainerBlackBox.h"
49 #include <wx/dialog.h>
50
51 //#include "bbtkData.h"
52 //#include "bbtkFactory.h"
53
54 namespace bbtk
55 {
56   //=========================================================================
57   // WxFrame
58   //=========================================================================
59
60   //==================================================================
61   class WxFrame : public wxFrame
62   {
63   public:
64     WxFrame(WxBlackBox::Pointer b,
65             wxWindow *parent,
66             wxString title,
67             wxSize size);
68     ~WxFrame();
69     void OnCloseWindow(wxCloseEvent& event);
70
71
72     WxBlackBox::WeakPointer mBox;      
73     DECLARE_EVENT_TABLE();
74     
75   };
76   
77   BEGIN_EVENT_TABLE(WxFrame, wxFrame)
78   //  EVT_CLOSE(WxFrame::OnCloseWindow)
79     END_EVENT_TABLE();
80      //==========================================Window  ========================
81   
82      //==================================================================
83   WxFrame::WxFrame(WxBlackBox::Pointer b,
84                    wxWindow *parent,
85                    wxString title,
86                    wxSize size) 
87     : 
88     wxFrame( parent, 
89              -1, 
90              title,
91              wxDefaultPosition,
92              size,
93              wxRESIZE_BORDER | 
94              wxSYSTEM_MENU  |
95              wxCLOSE_BOX |
96              wxMAXIMIZE_BOX | 
97              wxMINIMIZE_BOX | 
98              wxCAPTION  
99              ),
100     mBox(b)
101   {   
102     bbtkDebugMessage("widget",9,"["<<b->bbGetName()<<"] WxFrame()"
103                      <<std::endl);
104     Wx::IncNbWindowsAlive();
105     b->bbSetWindow(this);
106     // Insert the widget into the window
107     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
108     b->bbUserCreateWidget(this);
109     wxWindow* widget = b->bbGetOutputWidget();
110     sizer->Add( widget, 1, wxALL|wxGROW, 2);
111     SetSizer(sizer);
112     Layout();
113   }
114   //==================================================================
115   //==================================================================
116   WxFrame::~WxFrame() 
117     { 
118         
119 printf("EED WxFrame::~WxFrame\n");      
120         
121       if (mBox.lock())
122           {
123                 bbtkDebugMessage("widget",9,"["<<mBox.lock()->bbGetName()
124                          <<"] $$$$$$$$$$$$$$$$$$$ ~WxFrame()"
125                          <<std::endl);
126       } else { 
127                 bbtkDebugMessage("widget",9,"[DEAD] $$$$$$$$$$$$$$$$$$$$$ ~WxFrame()"<<std::endl);
128                 //bbtkDebugMessage("widget",9,"WxFrame::~WxFrame()"<<std::endl);
129           } // if mBox.lock()
130       if (mBox.lock()) 
131           {
132                 mBox.lock()->bbSetWindow(0); 
133           } // if mBox.lock()
134       Wx::DecNbWindowsAlive();
135     }
136    //==================================================================
137    //==================================================================
138   void WxFrame::OnCloseWindow(wxCloseEvent& event)
139     {
140       if (mBox.lock())
141         bbtkDebugMessage("widget",9,"["<<mBox.lock()->bbGetName()
142                          <<"] $$$$$$$$$$$$$$$ WxFrame::OnCloseWindow()"<<std::endl);
143       else
144         bbtkDebugMessage("widget",9,"[DEAD] WxFrame::OnCloseWindow()"
145                          <<std::endl);
146       if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
147       Wx::DecNbWindowsAlive();
148       
149         bbtkDebugMessage("widget",9,
150                          "$$$$$$$$$$$$$$$ Destroy !!!"<<std::endl);
151         //      this->Destroy();
152     }
153    //==================================================================
154
155   //====================================================SetWin=====================
156   // WxDialog
157   //=========================================================================
158
159    //==================================================================
160   // Dialog window which is modal
161   class BBTK_EXPORT  WxDialog : public wxDialog
162   {
163   public:
164     WxDialog(WxBlackBox::Pointer box, 
165                      wxWindow *parent, 
166                      wxString title, 
167                      wxSize size);
168     ~WxDialog();
169     void OnCloseWindow(wxCloseEvent& event);
170
171
172     WxBlackBox::WeakPointer mBox;      
173     DECLARE_EVENT_TABLE();
174     
175   };
176   
177   BEGIN_EVENT_TABLE(WxDialog, wxDialog)
178     EVT_CLOSE(WxDialog::OnCloseWindow)
179     END_EVENT_TABLE();
180   //=========================================================================
181
182   //=========================================================================
183   WxDialog::WxDialog(WxBlackBox::Pointer b,
184                      wxWindow *parent,
185                      wxString title,
186                      wxSize size)
187     :
188     wxDialog( parent, 
189                 -1, 
190                 title,
191                 wxDefaultPosition,
192                 size,
193                 wxRESIZE_BORDER | 
194                 wxSYSTEM_MENU  |
195                 wxCLOSE_BOX |
196                 wxMAXIMIZE_BOX | 
197                 wxMINIMIZE_BOX | 
198                 wxCAPTION  
199               ),
200     mBox(b)
201   {
202                 
203     bbtkDebugMessage("widget",9,"WxDialog::WxDialog ["<<b->bbGetName()<<"]"
204                      <<std::endl);
205     Wx::IncNbWindowsAlive();
206     b->bbSetWindow(this);
207     // Insert the widget into the window
208     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
209     b->bbUserCreateWidget(this);
210     wxWindow* widget = b->bbGetOutputWidget();
211     sizer->Add( widget, 1, wxALL|wxGROW, 2);
212     SetSizer(sizer);
213     Layout();
214   }
215   //==================================================================
216   //==================================================================
217   WxDialog::~WxDialog() 
218     { 
219       bbtkDebugMessage("widget",9,"WxDialog::~WxDialog()"<<std::endl);
220       //bbtkDebugMessage("widget",9,"WxFrame::~WxFrame()"<<std::endl);
221       //      if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
222       //      Wx::DecNbWindowsAlive();
223     }
224    //==================================================================
225    //==================================================================
226   void WxDialog::OnCloseWindow(wxCloseEvent& event)
227     {
228       bbtkDebugMessage("widget",9,"WxDialog::OnCloseWindow()"<<std::endl);
229       if (mBox.lock()) mBox.lock()->bbSetWindow(0); 
230       Wx::DecNbWindowsAlive();
231       this->Destroy();
232     }
233    //==================================================================
234
235
236  //=========================================================================
237   // WxBlackBox
238   //=========================================================================
239
240   //=========================================================================
241   //=========================================================================
242   //=========================================================================
243   //=========================================================================
244   BBTK_BLACK_BOX_IMPLEMENTATION(WxBlackBox,WidgetBlackBox<wxWindow>);
245   //=========================================================================
246   
247   //=========================================================================
248   void WxBlackBox::bbUserSetDefaultValues()
249   {
250     bbtkBlackBoxDebugMessage("widget",5,"WxBlackBox::bbUserSetDefaultValues()"<<std::endl);
251     bbmWindow = 0;  
252   }
253   //=========================================================================
254
255   //=========================================================================
256   void WxBlackBox::bbUserInitializeProcessing()
257   {
258   }
259   //==================================================================    
260
261   //=========================================================================
262   void WxBlackBox::bbUserFinalizeProcessing()
263   {
264     bbtkBlackBoxDebugMessage("widget",5,"==> WxBlackBox::bbUserFinalizeProcessing()"<<std::endl);
265     bbDestroyWindow();
266     bbtkBlackBoxDebugMessage("widget",5,"<== WxBlackBox::bbUserFinalizeProcessing()"<<std::endl);
267   }
268   //==================================================================    
269
270   //==================================================================    
271   /// Callback for creating a Dialog window (modal)
272   void WxBlackBox::bbCreateDialogWindow()
273   {
274   bbtkBlackBoxDebugMessage("widget",3,
275                      "==> WxBlackBox::bbCreateDialogWindow() - parent = "
276                      <<Wx::GetTopWindow()
277                      <<std::endl);
278     WxDialog* w = 0;  
279     w = new WxDialog( GetThisPointer<WxBlackBox>(),
280                       Wx::GetTopWindow(),
281 //                    std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS"),
282                       std2wx( ConstructWinTitle() ),
283                       wxSize( bbGetInputWinWidth() , 
284                               bbGetInputWinHeight() ) );
285     w->Show(false);
286     //    w->ShowModal();
287   } 
288   //=========================================================================
289
290   //==================================================================    
291
292   //==================================================================    
293   /// Callback for creating a Frame window 
294   void WxBlackBox::bbCreateFrameWindow()
295   {
296     bbtkBlackBoxDebugMessage("widget",3,
297                      "==> WxBlackBox::bbCreateFrameWindow() - parent = "
298                      <<Wx::GetTopWindow()
299                      <<std::endl);
300     WxFrame* w = 0;  
301     w = new WxFrame( GetThisPointer<WxBlackBox>(),
302                      Wx::GetTopWindow(),
303 //                   std2wx( bbGetInputWinTitle() + " - bbtk (c) CREATIS"),
304                      std2wx( ConstructWinTitle() ),
305                      wxSize( bbGetInputWinWidth() , 
306                              bbGetInputWinHeight() ) );
307     w->Show();
308
309
310    }
311
312   //==================================================================    
313
314   //==================================================================    
315   void WxBlackBox::bbSetWindow(wxWindow* w) 
316   {
317     bbtkBlackBoxDebugMessage("widget",9,"==> WxBlackBox::bbSetWindow("<<w<<")"<<std::endl);
318     if ((bbmWindow != 0)&&(w!=0)) 
319         { 
320                 std::cout << "WxBlackBox::bbSetWindow   ?? EED ?? ERRRRRRRROOOOR"<<std::endl; 
321         }
322     bbmWindow = w;
323   }
324   //==================================================================    
325
326   //==================================================================    
327   void WxBlackBox::bbShowWindow()
328   {
329     if ((bbmWindow) && (!bbIsShown()))
330       {
331         bbtkBlackBoxDebugMessage("widget",3,"==> WxBlackBox::bbShowWindow()"
332                          <<std::endl);
333         if (bbGetInputWinDialog()) 
334           {
335             ((WxDialog*)bbmWindow)->ShowModal();
336           }
337         else    
338           {
339             bbGetWindow()->Show();
340             bbSetShown(true);
341           } 
342       } // if
343
344
345   }
346   //==================================================================    
347
348   //==================================================================    
349   void WxBlackBox::bbDestroyWindow()
350   {
351     bbtkBlackBoxDebugMessage("widget",3,"==> WxBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
352     if (bbGetWindow()!=NULL) 
353       {
354         //       wxWindow* w = bbGetWindow();
355         //bbSetWindow(0);
356         // WE SHOULD DESTROY THE WINDOW WITH THE Close METHOD
357         // HOWEVER I
358         //w->Close(true);
359         //
360         delete bbGetWindow();
361         bbSetShown(false);
362       }
363     bbtkBlackBoxDebugMessage("widget",3,"<== WxBlackBox::bbDestroyWindow("<<bbmWindow<<")"<<std::endl);
364
365   }
366   //==================================================================    
367   
368   //==================================================================    
369   void WxBlackBox::PutWinTitle()
370   {
371         if ( ! bbIsOutputWidgetConnected() )
372     {
373                 wxTopLevelWindow* wParent = (wxTopLevelWindow*)( bbGetOutputWidget()->GetParent() );
374                 wParent->SetTitle( std2wx( ConstructWinTitle() ) );
375         } // if
376   }
377   //==================================================================    
378
379   //==================================================================    
380   std::string WxBlackBox::ConstructWinTitle()
381   {
382         std::string title( bbGetInputWinTitle() + " - bbtk (c) CREATIS");
383         return title;
384   }
385   //==================================================================    
386
387
388
389 }//namespace bbtk
390
391 #endif
392