]> 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/05/19 10:19:23 $
6   Version:   $Revision: 1.14 $
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     //if (mgTopWindowParent==0) 
138       Wx::SetTopWindow(top);
139   }
140   //=========================================================================
141
142   //=========================================================================
143   void Wx::DestroyTopWindowIfNeeded()
144   {
145     if ( (mgNbWindowsAlive==0) && 
146          (mgAutoDestroyTopWindow) &&
147          (mgTopWindow != 0))
148       {
149         bbtkDebugMessage("wx",1,"  --> Destructing bbtk top window"<<std::endl);
150         mgTopWindow->Close();
151         mgTopWindow = 0;
152         
153         DestroyWxAppIfNeeded();
154       }
155   } 
156   //=========================================================================
157
158   //=========================================================================
159   void Wx::LoopUntilAllWindowsClose()
160   {
161     int i = 0;
162     while (mgTopWindow != 0)
163       {
164         if (i % 100 == 0) 
165           {
166             bbtkDebugMessage("wx",2,"Wx::Loop "<<i << std::endl);
167           }
168         i++;
169         wxMilliSleep(10);
170
171       }
172   }
173   //=========================================================================
174
175   //=========================================================================
176   wxWindow* Wx::GetTopWindow() 
177   { 
178     Wx::CreateTopWindowIfNeeded();
179     return mgTopWindow; 
180   }
181   //=========================================================================
182   
183   //=========================================================================
184   bool Wx::TopWindowExists()
185   {
186     return (mgTopWindow!=0);
187   }
188   //=========================================================================
189
190   // BBTKWXSIG
191   //=========================================================================
192   void Wx::AddSignalObserver(Slot_function_type f)
193   {
194     mgWxSignal.connect(f);
195   }
196   //=========================================================================
197   // \BBTKWXSIG
198
199   //=========================================================================
200   void Wx::SetAutoDestroyTopWindow(bool b)
201   {
202     mgAutoDestroyTopWindow = b;
203   }
204   //=========================================================================
205   
206   //=========================================================================
207   void Wx::SetTopWindow(wxWindow* w) 
208   {
209     if ( mgTopWindow ) 
210       {
211         bbtkGlobalError("wx::SetTopWindow : top window already set !");
212       } 
213     mgTopWindow = w;
214   }
215   //=========================================================================
216
217
218   //=========================================================================
219   void Wx::IncNbWindowsAlive() 
220   { 
221     mgNbWindowsAlive++; 
222     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
223                      <<std::endl);
224     // BBTKWXSIG
225     mgWxSignal();
226     // \BBTKWXSIG
227   }
228   //=========================================================================
229
230   //=========================================================================
231   void Wx::DecNbWindowsAlive()
232   { 
233     mgNbWindowsAlive--; 
234     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
235                      <<std::endl);
236
237     DestroyTopWindowIfNeeded();
238      // BBTKWXSIG
239     mgWxSignal();
240     // \BBTKWXSIG
241   }
242   //=========================================================================
243   
244   //=========================================================================
245   void Wx::IncNbWindowsShown() 
246   { 
247     mgNbWindowsShown++; 
248     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
249                      <<std::endl);
250     // BBTKWXSIG
251     mgWxSignal();
252     // \BBTKWXSIG
253   }
254   //=========================================================================
255
256   //=========================================================================
257   void Wx::DecNbWindowsShown()
258   { 
259     mgNbWindowsShown--; 
260     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
261                      <<std::endl);
262
263     //   DestroyTopWindowIfNeeded();
264
265     // BBTKWXSIG
266     mgWxSignal();
267     // \BBTKWXSIG
268   }
269   //=========================================================================
270
271
272   //=========================================================================
273   int  Wx::GetNbWindowsAlive() 
274   { 
275     return mgNbWindowsAlive; 
276   }
277   //=========================================================================
278   
279   //=========================================================================
280   bool Wx::IsSomeWindowAlive() 
281   { 
282     return (mgNbWindowsAlive>0);
283   }
284   //=========================================================================
285   
286   
287   //=========================================================================
288   int  Wx::GetNbWindowsShown() 
289   { 
290     return mgNbWindowsShown; 
291   }
292   //=========================================================================
293   
294   //=========================================================================
295   bool Wx::IsSomeWindowShown() 
296   { 
297     return (mgNbWindowsShown>0);
298   }
299   //=========================================================================
300   
301
302   //=========================================================================
303   Wx::BusyCursor::BusyCursor()
304   {
305     mCursor = 0;
306     if (wxApp::GetInstance()!=0) 
307       {         
308         bbtkDebugMessage("wx",2,
309                          "Wx::BusyCursor::BusyCursor() : creating new cursor"
310                          <<std::endl);
311         //mCursor = new wxBusyCursor; 
312     }
313   }
314   Wx::BusyCursor::~BusyCursor()
315   {
316     if (mCursor)
317       { 
318         //delete mCursor;
319         bbtkDebugMessage("wx",2,
320                          "Wx::BusyCursor::~BusyCursor() : deleting cursor"<<std::endl);
321       }
322   }
323   //=========================================================================
324
325   /*
326   //=========================================================================
327   void Wx::ResetCursor() 
328   {
329     if (!TopWindowExists()) return;
330     bbtkDebugMessage("wx",9,"Wx::ResetCursor()"<<std::endl);
331     while (wxIsBusy()) ::wxEndBusyCursor();
332   }
333   //=========================================================================
334   //=========================================================================
335   void Wx::BeginBusyCursor() 
336   {
337     if (!TopWindowExists()) return;
338     bbtkDebugMessage("wx",9,"Wx::BeginBusyCursor()"<<std::endl);
339     ::wxBeginBusyCursor();
340   }
341   //=========================================================================
342   //=========================================================================
343   void Wx::EndBusyCursor()
344   {
345     if (!TopWindowExists()) return;
346     bbtkDebugMessage("wx",9,"Wx::EndBusyCursor()"<<std::endl);
347     ::wxEndBusyCursor();
348   }
349   //=========================================================================
350   */
351
352 } // namespace bbtk
353
354 #else
355 //=======================================================================
356 // WITHOUT WX
357 //=========================================================================
358 namespace bbtk
359 {
360   Wx::BusyCursor::BusyCursor()
361   {
362   }
363   Wx::BusyCursor::~BusyCursor()
364   {
365   }
366   //=========================================================================
367
368 } // namespace bbtk
369 #endif
370