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