]> Creatis software - bbtk.git/blob - kernel/src/bbtkWx.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWx.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWx.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/04/08 07:56:11 $
6   Version:   $Revision: 1.13 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30 #include "bbtkWx.h"
31 #include "bbtkMessageManager.h"
32
33 #ifdef _USE_WXWIDGETS_
34
35 namespace bbtk
36 {
37   //=========================================================================
38   class WxApp;
39   //=========================================================================
40
41   //=========================================================================
42   static WxApp* mgWxApp = 0;
43   static wxWindow* mgTopWindow = 0;
44   static wxWindow* mgTopWindowParent = 0;
45   static bool mgAutoDestroyTopWindow = true;
46   static int mgNbWindowsAlive  = 0;
47   static int mgNbWindowsShown  = 0;
48   //=========================================================================
49
50   //=========================================================================
51   // BBTKWXSIG
52   static Wx::Signal_type mgWxSignal;
53   // \BBTKWXSIG
54   //=========================================================================
55
56  
57   //=========================================================================
58   // The wxApp class which is used when no user wxApp was created
59   class WxApp : public wxApp
60   {
61   public:
62     bool OnInit( );
63     int  OnExit() { return true; }
64   };
65   //=========================================================================
66   IMPLEMENT_APP_NO_MAIN(WxApp);
67   //=========================================================================
68   bool WxApp::OnInit( )
69   {      
70     wxApp::OnInit();
71 #ifdef __WXGTK__
72     //See http://www.wxwindows.org/faqgtk.htm#locale
73     setlocale(LC_NUMERIC, "C");
74 #endif
75     return true;
76   }
77   //=========================================================================
78
79   //=========================================================================
80   void Wx::CreateWxAppIfNeeded()
81   {
82     if (wxApp::GetInstance()==0)
83       {
84         if (mgWxApp != 0) 
85           {
86             bbtkGlobalError("Wx::CreateWxAppIfNeeded() : INTERNAL ERROR ! (wxApp::GetInstance()==0) && (mgWxApp != 0)");
87           }
88         bbtkDebugMessage("wx",1,"  --> Creating bbtk wxApp"<<std::endl);
89         mgWxApp = new WxApp;
90         wxApp::SetInstance(mgWxApp);
91         //int argc = 0;
92         //wxEntry(argc,0);
93         wxInitialize();
94       } 
95   }
96   //=========================================================================
97
98   //=========================================================================
99   void Wx::DestroyWxAppIfNeeded()
100   {
101     if (mgWxApp!= 0) 
102       {
103         bbtkDebugMessage("wx",1,"  --> Destructing bbtk WxApp"<<std::endl);
104         //delete mgWxApp;
105         //      mgWxApp = 0;
106         // Uninit wx
107         //      wxUninitialize();
108       }
109   }
110   //=========================================================================
111
112   //=========================================================================  
113   void Wx::SetTopWindowParent(wxWindow* w)
114   {
115     if (mgTopWindowParent != 0)
116       {
117         bbtkGlobalError("Wx::SetTopWindowParent : top window parent != 0");
118       }
119     mgTopWindowParent = w;
120   }
121   //=========================================================================
122
123   //=========================================================================
124   void Wx::CreateTopWindowIfNeeded()
125   {
126     if (mgTopWindow!=0) return;
127     bbtkDebugMessage("wx",1,"  --> Creating bbtk top window"<<std::endl);
128
129     CreateWxAppIfNeeded();
130
131     wxWindow* top = 
132       new wxFrame(mgTopWindowParent,
133                   -1,
134                   _T("TOP BBTK FRAME (YOU SHOULD NOT SEE ME !!)"));
135     top->Hide();
136
137     Wx::SetTopWindow(top);
138   }
139   //=========================================================================
140
141   //=========================================================================
142   void Wx::DestroyTopWindowIfNeeded()
143   {
144     if ( (mgNbWindowsAlive==0) && 
145          (mgAutoDestroyTopWindow) )
146       {
147         bbtkDebugMessage("wx",1,"  --> Destructing bbtk top window"<<std::endl);
148         mgTopWindow->Close();
149         mgTopWindow = 0;
150         
151         DestroyWxAppIfNeeded();
152       }
153   } 
154   //=========================================================================
155
156   //=========================================================================
157   void Wx::LoopUntilAllWindowsClose()
158   {
159     int i = 0;
160     while (mgTopWindow != 0)
161       {
162         if (i % 100 == 0) 
163           {
164             bbtkDebugMessage("wx",2,"Wx::Loop "<<i << std::endl);
165           }
166         i++;
167         wxMilliSleep(10);
168
169       }
170   }
171   //=========================================================================
172
173   //=========================================================================
174   wxWindow* Wx::GetTopWindow() 
175   { 
176     Wx::CreateTopWindowIfNeeded();
177     return mgTopWindow; 
178   }
179   //=========================================================================
180   
181   //=========================================================================
182   bool Wx::TopWindowExists()
183   {
184     return (mgTopWindow!=0);
185   }
186   //=========================================================================
187
188   // BBTKWXSIG
189   //=========================================================================
190   void Wx::AddSignalObserver(Slot_function_type f)
191   {
192     mgWxSignal.connect(f);
193   }
194   //=========================================================================
195   // \BBTKWXSIG
196
197   //=========================================================================
198   void Wx::SetAutoDestroyTopWindow(bool b)
199   {
200     mgAutoDestroyTopWindow = b;
201   }
202   //=========================================================================
203   
204   //=========================================================================
205   void Wx::SetTopWindow(wxWindow* w) 
206   {
207     if ( mgTopWindow ) 
208       {
209         bbtkGlobalError("wx::SetTopWindow : top window already set !");
210       } 
211     mgTopWindow = w;
212   }
213   //=========================================================================
214
215
216   //=========================================================================
217   void Wx::IncNbWindowsAlive() 
218   { 
219     mgNbWindowsAlive++; 
220     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
221                      <<std::endl);
222     // BBTKWXSIG
223     mgWxSignal();
224     // \BBTKWXSIG
225   }
226   //=========================================================================
227
228   //=========================================================================
229   void Wx::DecNbWindowsAlive()
230   { 
231     mgNbWindowsAlive--; 
232     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
233                      <<std::endl);
234
235     DestroyTopWindowIfNeeded();
236      // BBTKWXSIG
237     mgWxSignal();
238     // \BBTKWXSIG
239   }
240   //=========================================================================
241   
242   //=========================================================================
243   void Wx::IncNbWindowsShown() 
244   { 
245     mgNbWindowsShown++; 
246     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
247                      <<std::endl);
248     // BBTKWXSIG
249     mgWxSignal();
250     // \BBTKWXSIG
251   }
252   //=========================================================================
253
254   //=========================================================================
255   void Wx::DecNbWindowsShown()
256   { 
257     mgNbWindowsShown--; 
258     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
259                      <<std::endl);
260
261     DestroyTopWindowIfNeeded();
262
263     // BBTKWXSIG
264     mgWxSignal();
265     // \BBTKWXSIG
266   }
267   //=========================================================================
268
269
270   //=========================================================================
271   int  Wx::GetNbWindowsAlive() 
272   { 
273     return mgNbWindowsAlive; 
274   }
275   //=========================================================================
276   
277   //=========================================================================
278   bool Wx::IsSomeWindowAlive() 
279   { 
280     return (mgNbWindowsAlive>0);
281   }
282   //=========================================================================
283   
284   
285   //=========================================================================
286   int  Wx::GetNbWindowsShown() 
287   { 
288     return mgNbWindowsShown; 
289   }
290   //=========================================================================
291   
292   //=========================================================================
293   bool Wx::IsSomeWindowShown() 
294   { 
295     return (mgNbWindowsShown>0);
296   }
297   //=========================================================================
298   
299
300   //=========================================================================
301   Wx::BusyCursor::BusyCursor()
302   {
303     mCursor = 0;
304     if (wxApp::GetInstance()!=0) 
305       {         
306         bbtkDebugMessage("wx",2,
307                          "Wx::BusyCursor::BusyCursor() : creating new cursor"
308                          <<std::endl);
309         mCursor = new wxBusyCursor; 
310     }
311   }
312   Wx::BusyCursor::~BusyCursor()
313   {
314     if (mCursor)
315       { 
316         delete mCursor;
317         bbtkDebugMessage("wx",2,
318                          "Wx::BusyCursor::~BusyCursor() : deleting cursor"<<std::endl);
319       }
320   }
321   //=========================================================================
322
323   /*
324   //=========================================================================
325   void Wx::ResetCursor() 
326   {
327     if (!TopWindowExists()) return;
328     bbtkDebugMessage("wx",9,"Wx::ResetCursor()"<<std::endl);
329     while (wxIsBusy()) ::wxEndBusyCursor();
330   }
331   //=========================================================================
332   //=========================================================================
333   void Wx::BeginBusyCursor() 
334   {
335     if (!TopWindowExists()) return;
336     bbtkDebugMessage("wx",9,"Wx::BeginBusyCursor()"<<std::endl);
337     ::wxBeginBusyCursor();
338   }
339   //=========================================================================
340   //=========================================================================
341   void Wx::EndBusyCursor()
342   {
343     if (!TopWindowExists()) return;
344     bbtkDebugMessage("wx",9,"Wx::EndBusyCursor()"<<std::endl);
345     ::wxEndBusyCursor();
346   }
347   //=========================================================================
348   */
349
350 } // namespace bbtk
351
352 #else
353 //=======================================================================
354 // WITHOUT WX
355 //=========================================================================
356 namespace bbtk
357 {
358   Wx::BusyCursor::BusyCursor()
359   {
360   }
361   Wx::BusyCursor::~BusyCursor()
362   {
363   }
364   //=========================================================================
365
366 } // namespace bbtk
367 #endif
368