00001 /*========================================================================= 00002 Program: bbtk 00003 Module: $RCSfile: bbtkWx.cxx,v $ 00004 Language: C++ 00005 Date: $Date: 2008/10/21 13:55:49 $ 00006 Version: $Revision: 1.11 $ 00007 =========================================================================*/ 00008 00009 /* --------------------------------------------------------------------- 00010 00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) 00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux 00013 * 00014 * This software is governed by the CeCILL-B license under French law and 00015 * abiding by the rules of distribution of free software. You can use, 00016 * modify and/ or redistribute the software under the terms of the CeCILL-B 00017 * license as circulated by CEA, CNRS and INRIA at the following URL 00018 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 00019 * or in the file LICENSE.txt. 00020 * 00021 * As a counterpart to the access to the source code and rights to copy, 00022 * modify and redistribute granted by the license, users are provided only 00023 * with a limited warranty and the software's author, the holder of the 00024 * economic rights, and the successive licensors have only limited 00025 * liability. 00026 * 00027 * The fact that you are presently reading this means that you have had 00028 * knowledge of the CeCILL-B license and that you accept its terms. 00029 * ------------------------------------------------------------------------ */ 00030 00031 #ifdef _USE_WXWIDGETS_ 00032 00033 #include "bbtkWx.h" 00034 #include "bbtkMessageManager.h" 00035 00036 namespace bbtk 00037 { 00038 //========================================================================= 00039 class WxApp; 00040 //========================================================================= 00041 00042 //========================================================================= 00043 static WxApp* mgWxApp = 0; 00044 static wxWindow* mgTopWindow = 0; 00045 static wxWindow* mgTopWindowParent = 0; 00046 static bool mgAutoDestroyTopWindow = true; 00047 static int mgNbWindowsAlive = 0; 00048 static int mgNbWindowsShown = 0; 00049 //========================================================================= 00050 00051 //========================================================================= 00052 // BBTKWXSIG 00053 static Wx::Signal_type mgWxSignal; 00054 // \BBTKWXSIG 00055 //========================================================================= 00056 00057 00058 //========================================================================= 00059 // The wxApp class which is used when no user wxApp was created 00060 class WxApp : public wxApp 00061 { 00062 public: 00063 bool OnInit( ); 00064 int OnExit() { return true; } 00065 }; 00066 //========================================================================= 00067 IMPLEMENT_APP_NO_MAIN(WxApp); 00068 //========================================================================= 00069 bool WxApp::OnInit( ) 00070 { 00071 wxApp::OnInit(); 00072 #ifdef __WXGTK__ 00073 //See http://www.wxwindows.org/faqgtk.htm#locale 00074 setlocale(LC_NUMERIC, "C"); 00075 #endif 00076 return true; 00077 } 00078 //========================================================================= 00079 00080 //========================================================================= 00081 void Wx::CreateWxAppIfNeeded() 00082 { 00083 if (wxApp::GetInstance()==0) 00084 { 00085 if (mgWxApp != 0) 00086 { 00087 bbtkGlobalError("Wx::CreateWxAppIfNeeded() : INTERNAL ERROR ! (wxApp::GetInstance()==0) && (mgWxApp != 0)"); 00088 } 00089 bbtkDebugMessage("wx",1," --> Creating bbtk wxApp"<<std::endl); 00090 mgWxApp = new WxApp; 00091 wxApp::SetInstance(mgWxApp); 00092 //int argc = 0; 00093 //wxEntry(argc,0); 00094 wxInitialize(); 00095 } 00096 } 00097 //========================================================================= 00098 00099 //========================================================================= 00100 void Wx::DestroyWxAppIfNeeded() 00101 { 00102 if (mgWxApp!= 0) 00103 { 00104 bbtkDebugMessage("wx",1," --> Destructing bbtk WxApp"<<std::endl); 00105 //delete mgWxApp; 00106 // mgWxApp = 0; 00107 // Uninit wx 00108 // wxUninitialize(); 00109 } 00110 } 00111 //========================================================================= 00112 00113 //========================================================================= 00114 void Wx::SetTopWindowParent(wxWindow* w) 00115 { 00116 if (mgTopWindowParent != 0) 00117 { 00118 bbtkGlobalError("Wx::SetTopWindowParent : top window parent != 0"); 00119 } 00120 mgTopWindowParent = w; 00121 } 00122 //========================================================================= 00123 00124 //========================================================================= 00125 void Wx::CreateTopWindowIfNeeded() 00126 { 00127 if (mgTopWindow!=0) return; 00128 bbtkDebugMessage("wx",1," --> Creating bbtk top window"<<std::endl); 00129 00130 CreateWxAppIfNeeded(); 00131 00132 wxWindow* top = 00133 new wxFrame(mgTopWindowParent, 00134 -1, 00135 _T("TOP BBTK FRAME (YOU SHOULD NOT SEE ME !!)")); 00136 top->Hide(); 00137 00138 Wx::SetTopWindow(top); 00139 } 00140 //========================================================================= 00141 00142 //========================================================================= 00143 void Wx::DestroyTopWindowIfNeeded() 00144 { 00145 if ( (mgNbWindowsAlive==0) && 00146 (mgAutoDestroyTopWindow) ) 00147 { 00148 bbtkDebugMessage("wx",1," --> Destructing bbtk top window"<<std::endl); 00149 mgTopWindow->Close(); 00150 mgTopWindow = 0; 00151 00152 DestroyWxAppIfNeeded(); 00153 } 00154 } 00155 //========================================================================= 00156 00157 //========================================================================= 00158 void Wx::LoopUntilAllWindowsClose() 00159 { 00160 int i = 0; 00161 while (mgTopWindow != 0) 00162 { 00163 if (i % 100 == 0) 00164 { 00165 bbtkDebugMessage("wx",2,"Wx::Loop "<<i << std::endl); 00166 } 00167 i++; 00168 wxMilliSleep(10); 00169 00170 } 00171 } 00172 //========================================================================= 00173 00174 //========================================================================= 00175 wxWindow* Wx::GetTopWindow() 00176 { 00177 Wx::CreateTopWindowIfNeeded(); 00178 return mgTopWindow; 00179 } 00180 //========================================================================= 00181 00182 //========================================================================= 00183 bool Wx::TopWindowExists() 00184 { 00185 return (mgTopWindow!=0); 00186 } 00187 //========================================================================= 00188 00189 // BBTKWXSIG 00190 //========================================================================= 00191 void Wx::AddSignalObserver(Slot_function_type f) 00192 { 00193 mgWxSignal.connect(f); 00194 } 00195 //========================================================================= 00196 // \BBTKWXSIG 00197 00198 //========================================================================= 00199 void Wx::SetAutoDestroyTopWindow(bool b) 00200 { 00201 mgAutoDestroyTopWindow = b; 00202 } 00203 //========================================================================= 00204 00205 //========================================================================= 00206 void Wx::SetTopWindow(wxWindow* w) 00207 { 00208 if ( mgTopWindow ) 00209 { 00210 bbtkGlobalError("wx::SetTopWindow : top window already set !"); 00211 } 00212 mgTopWindow = w; 00213 } 00214 //========================================================================= 00215 00216 00217 //========================================================================= 00218 void Wx::IncNbWindowsAlive() 00219 { 00220 mgNbWindowsAlive++; 00221 bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive 00222 <<std::endl); 00223 // BBTKWXSIG 00224 mgWxSignal(); 00225 // \BBTKWXSIG 00226 } 00227 //========================================================================= 00228 00229 //========================================================================= 00230 void Wx::DecNbWindowsAlive() 00231 { 00232 mgNbWindowsAlive--; 00233 bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive 00234 <<std::endl); 00235 00236 DestroyTopWindowIfNeeded(); 00237 // BBTKWXSIG 00238 mgWxSignal(); 00239 // \BBTKWXSIG 00240 } 00241 //========================================================================= 00242 00243 //========================================================================= 00244 void Wx::IncNbWindowsShown() 00245 { 00246 mgNbWindowsShown++; 00247 bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown 00248 <<std::endl); 00249 // BBTKWXSIG 00250 mgWxSignal(); 00251 // \BBTKWXSIG 00252 } 00253 //========================================================================= 00254 00255 //========================================================================= 00256 void Wx::DecNbWindowsShown() 00257 { 00258 mgNbWindowsShown--; 00259 bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown 00260 <<std::endl); 00261 00262 DestroyTopWindowIfNeeded(); 00263 00264 // BBTKWXSIG 00265 mgWxSignal(); 00266 // \BBTKWXSIG 00267 } 00268 //========================================================================= 00269 00270 00271 //========================================================================= 00272 int Wx::GetNbWindowsAlive() 00273 { 00274 return mgNbWindowsAlive; 00275 } 00276 //========================================================================= 00277 00278 //========================================================================= 00279 bool Wx::IsSomeWindowAlive() 00280 { 00281 return (mgNbWindowsAlive>0); 00282 } 00283 //========================================================================= 00284 00285 00286 //========================================================================= 00287 int Wx::GetNbWindowsShown() 00288 { 00289 return mgNbWindowsShown; 00290 } 00291 //========================================================================= 00292 00293 //========================================================================= 00294 bool Wx::IsSomeWindowShown() 00295 { 00296 return (mgNbWindowsShown>0); 00297 } 00298 //========================================================================= 00299 00300 00301 //========================================================================= 00302 Wx::BusyCursor::BusyCursor() 00303 { 00304 mCursor = 0; 00305 if (TopWindowExists()) 00306 { 00307 bbtkDebugMessage("wx",2, 00308 "Wx::BusyCursor::BusyCursor()"<<std::endl); 00309 mCursor = new wxBusyCursor; 00310 //::wxBeginBusyCursor(); 00311 } 00312 } 00313 Wx::BusyCursor::~BusyCursor() 00314 { 00315 if (mCursor) delete mCursor; 00316 } 00317 //========================================================================= 00318 00319 /* 00320 //========================================================================= 00321 void Wx::ResetCursor() 00322 { 00323 if (!TopWindowExists()) return; 00324 bbtkDebugMessage("wx",9,"Wx::ResetCursor()"<<std::endl); 00325 while (wxIsBusy()) ::wxEndBusyCursor(); 00326 } 00327 //========================================================================= 00328 //========================================================================= 00329 void Wx::BeginBusyCursor() 00330 { 00331 if (!TopWindowExists()) return; 00332 bbtkDebugMessage("wx",9,"Wx::BeginBusyCursor()"<<std::endl); 00333 ::wxBeginBusyCursor(); 00334 } 00335 //========================================================================= 00336 //========================================================================= 00337 void Wx::EndBusyCursor() 00338 { 00339 if (!TopWindowExists()) return; 00340 bbtkDebugMessage("wx",9,"Wx::EndBusyCursor()"<<std::endl); 00341 ::wxEndBusyCursor(); 00342 } 00343 //========================================================================= 00344 */ 00345 00346 } 00347 00348 #endif