]> Creatis software - bbtk.git/blob - kernel/src/bbtkWx.h
Fixed Window deletion mechanism
[bbtk.git] / kernel / src / bbtkWx.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWx.h,v $
4   Language:  C++
5   Date:      $Date: 2009/05/28 08:12:06 $
6   Version:   $Revision: 1.11 $
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
32 /* ---------------------------------------------------------------------
33
34 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
35 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
36 *
37 *  This software is governed by the CeCILL-B license under French law and 
38 *  abiding by the rules of distribution of free software. You can  use, 
39 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
40 *  license as circulated by CEA, CNRS and INRIA at the following URL 
41 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
42 *  or in the file LICENSE.txt.
43 *
44 *  As a counterpart to the access to the source code and  rights to copy,
45 *  modify and redistribute granted by the license, users are provided only
46 *  with a limited warranty  and the software's author,  the holder of the
47 *  economic rights,  and the successive licensors  have only  limited
48 *  liability. 
49 *
50 *  The fact that you are presently reading this means that you have had
51 *  knowledge of the CeCILL-B license and that you accept its terms.
52 * ------------------------------------------------------------------------ */                                                                         
53
54
55 #ifndef __bbtkWx_h_INCLUDED__
56 #define __bbtkWx_h_INCLUDED__
57
58 //===========================================================================
59 // Wx headers
60 #ifdef USE_WXWIDGETS
61 // For compilers that support precompilation, includes "wx/wx.h".
62 #include "wx/wxprec.h"
63 #include <wx/datetime.h>
64
65
66 #ifdef __BORLANDC__
67 #pragma hdrstop
68 #endif
69
70 #ifndef WX_PRECOMP
71 #include <wx/wx.h>
72 #endif
73
74 #ifdef __WXGTK__
75 # include <locale.h>
76 #endif //__WXGTK__
77 // EO Wx headers
78
79 #else //USE_WXWIDGETS
80
81 // define wxWindow
82 typedef void wxWindow;
83 typedef void wxBusyCursor;
84
85 #endif // EO USE_WXWIDGETS
86 //===========================================================================
87
88 //===========================================================================
89 #include "bbtkSystem.h"
90 //===========================================================================
91
92 //===========================================================================
93 // SIGNAL/SLOT MECHANISM 
94 // FOR bbtk WINDOWS CREATION/DESTRUCTION OBSERVATION 
95 // BBTKWXSIG
96 #include <boost/signal.hpp>
97 #include <boost/bind.hpp>
98 // \BBTKWXSIG
99 //===========================================================================
100
101 namespace bbtk
102 {
103
104   //==================================================================
105   /// Global wx handlers (cursor...)
106   
107 // BBTKWXSIG
108 #define bbtkAddWxSignalObserver(METHOD)         \
109   Wx::AddSignalObserver(boost::bind(&METHOD, this))
110 // \BBTKWXSIG
111
112 #define BBTK_BUSY_CURSOR bbtk::Wx::BusyCursor __bbtk_dummy_busy_cursor;
113
114   struct BBTK_EXPORT Wx
115   {     
116   public:
117     // BBTKWXSIG
118     typedef boost::signals::trackable SignalObserver;
119     typedef boost::signal<void ()>  Signal_type;
120     typedef Signal_type::slot_function_type Slot_function_type;
121     // \BBTKWXSIG
122
123     class BusyCursor
124     {
125     public:
126       BusyCursor();
127       ~BusyCursor();
128       wxBusyCursor* mCursor;
129     };
130
131     static void ProcessPendingEvents();
132     
133     /// Sets the parent of all bbtk windows
134     static void SetTopWindow(wxWindow*);
135     /// Returns the parent of all bbtk windows 
136     static wxWindow* GetTopWindow();
137
138
139     static void IncNbWindowsAlive();
140     static void DecNbWindowsAlive();
141     static int  GetNbWindowsAlive();
142     static bool IsSomeWindowAlive();
143     
144      // BBTKWXSIG
145     static void AddSignalObserver(Slot_function_type);
146     // \BBTKWXSIG
147
148   
149   private:
150
151     static void CreateWxAppIfNeeded();
152     static void DestroyWxAppIfNeeded();
153
154   }; // struct Wx
155   //==================================================================
156   
157
158
159
160 #ifdef USE_WXWIDGETS
161   //==================================================================
162   /// Conversion std::string to wxString 
163   inline wxString std2wx(const std::string& s){
164     wxString wx;
165     const char* my_string=s.c_str();
166     wxMBConvUTF8 *wxconv= new wxMBConvUTF8();
167     wx=wxString(wxconv->cMB2WC(my_string),wxConvUTF8);
168     delete wxconv;
169     // test if conversion works of not. In case it fails convert from Ascii
170     if(wx.length()==0)
171       wx=wxString(wxString::FromAscii(s.c_str()));
172     return wx;
173   }
174   //==================================================================
175
176   //==================================================================
177   /// Conversion wxString to std::string
178   inline std::string wx2std(const wxString& s){
179     std::string s2;
180     if(s.wxString::IsAscii()) {
181       s2=s.wxString::ToAscii();
182     } else {
183       const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(s);
184       const char *tmp_str = (const char*) tmp_buf;
185       s2=std::string(tmp_str, strlen(tmp_str));
186     }
187     return s2;
188   }
189   //==================================================================
190 #endif // EO _USE_WXWIDGETS
191
192   
193 } // namespace bbtk
194   
195
196 #endif // EO __bbtkWx_h_INCLUDED__