]> Creatis software - bbtk.git/blob - kernel/src/bbtkWx.h
d810bbf528a03d43e4722e49abc36514e27eec63
[bbtk.git] / kernel / src / bbtkWx.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWx.h,v $
4   Language:  C++
5   Date:      $Date: 2010/01/14 13:17:27 $
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
32 #ifndef __bbtkWx_h_INCLUDED__
33 #define __bbtkWx_h_INCLUDED__
34
35 //===========================================================================
36 // Wx headers
37 #ifdef USE_WXWIDGETS
38 // For compilers that support precompilation, includes "wx/wx.h".
39 #include "wx/wxprec.h"
40 #include <wx/datetime.h>
41
42
43 #ifdef __BORLANDC__
44 #pragma hdrstop
45 #endif
46
47 #ifndef WX_PRECOMP
48 #include <wx/wx.h>
49 #endif
50
51 #ifdef __WXGTK__
52 # include <locale.h>
53 #endif //__WXGTK__
54 // EO Wx headers
55
56 #else //USE_WXWIDGETS
57
58 // define wxWindow
59 typedef void wxWindow;
60 typedef void wxBusyCursor;
61
62 #endif // EO USE_WXWIDGETS
63 //===========================================================================
64
65 //===========================================================================
66 #include "bbtkSystem.h"
67 //===========================================================================
68
69 //===========================================================================
70 // SIGNAL/SLOT MECHANISM 
71 // FOR bbtk WINDOWS CREATION/DESTRUCTION OBSERVATION 
72 // BBTKWXSIG
73 #include <boost/signal.hpp>
74 #include <boost/bind.hpp>
75 // \BBTKWXSIG
76 //===========================================================================
77
78 namespace bbtk
79 {
80
81   //==================================================================
82   /// Global wx handlers (cursor...)
83   
84 // BBTKWXSIG
85 #define bbtkAddWxSignalObserver(METHOD)         \
86   Wx::AddSignalObserver(boost::bind(&METHOD, this))
87 // \BBTKWXSIG
88
89 #define BBTK_BUSY_CURSOR bbtk::Wx::BusyCursor __bbtk_dummy_busy_cursor;
90
91   struct BBTK_EXPORT Wx
92   {     
93   public:
94     // BBTKWXSIG
95     typedef boost::signals::trackable SignalObserver;
96     typedef boost::signal<void ()>  Signal_type;
97     typedef Signal_type::slot_function_type Slot_function_type;
98     // \BBTKWXSIG
99
100     class BusyCursor
101     {
102     public:
103       BusyCursor();
104       ~BusyCursor();
105       wxBusyCursor* mCursor;
106     };
107
108     static void ProcessPendingEvents();
109     
110     /// Sets the parent of all bbtk windows
111     static void SetTopWindow(wxWindow*);
112     /// Returns the parent of all bbtk windows 
113     static wxWindow* GetTopWindow();
114
115
116     static void IncNbWindowsAlive();
117     static void DecNbWindowsAlive();
118     static int  GetNbWindowsAlive();
119     static bool IsSomeWindowAlive();
120     
121      // BBTKWXSIG
122     static void AddSignalObserver(Slot_function_type);
123     // \BBTKWXSIG
124
125   
126   private:
127
128     static void CreateWxAppIfNeeded();
129     static void DestroyWxAppIfNeeded();
130
131   }; // struct Wx
132   //==================================================================
133
134
135 #ifdef USE_WXWIDGETS
136   //==================================================================
137   /// Conversion std::string to wxString 
138   inline wxString std2wx(const std::string& s){
139     wxString wx;
140     const char* my_string=s.c_str();
141     wxMBConvUTF8 *wxconv= new wxMBConvUTF8();
142     wx=wxString(wxconv->cMB2WC(my_string),wxConvUTF8);
143     delete wxconv;
144     // test if conversion works of not. In case it fails convert from Ascii
145     if(wx.length()==0)
146       wx=wxString(wxString::FromAscii(s.c_str()));
147     return wx;
148   }
149   //==================================================================
150
151   //==================================================================
152   /// Conversion wxString to std::string
153   inline std::string wx2std(const wxString& s){
154     std::string s2;
155     if(s.wxString::IsAscii()) {
156       s2=s.wxString::ToAscii();
157     } else {
158       const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(s);
159       const char *tmp_str = (const char*) tmp_buf;
160       s2=std::string(tmp_str, strlen(tmp_str));
161     }
162     return s2;
163   }
164   //==================================================================
165 #endif // EO _USE_WXWIDGETS
166
167   
168 } // namespace bbtk
169   
170
171 #endif // EO __bbtkWx_h_INCLUDED__