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