00001 /*========================================================================= 00002 Program: bbtk 00003 Module: $RCSfile: bbtkKW.cxx,v $ 00004 Language: C++ 00005 Date: $Date: 2008/12/12 12:11:21 $ 00006 Version: $Revision: 1.3 $ 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_KWWIDGETS 00032 00033 #include "bbtkKW.h" 00034 #include "bbtkMessageManager.h" 00035 #include "vtkKWApplication.h" 00036 00037 00038 //#include "vtkConditionVariable.h" 00039 //#include "vtkMultiThreader.h" 00040 00041 #include <ctime> 00042 00043 extern "C" int Bbtk_Init(Tcl_Interp *interp); 00044 00045 namespace bbtk 00046 { 00047 00048 bool _dummy_rmt = 00049 bbtk::MessageManager::RegisterMessageType("kw", 00050 "KWWidgets related messages",0); 00051 00052 00053 00054 //========================================================================= 00055 static vtkKWApplication* mgKWApp = 0; 00056 //static vtkMultiThreader* mgMultiThreader = 0; 00057 /* 00058 static vtkKWWindowBase* mgKWTopWindow = 0; 00059 static vtkKWWindowBase* mgKWTopWindowParent = 0; 00060 static bool mgKWAutoDestroyTopWindow = true; 00061 */ 00062 static int mgKWNbWindowsAlive = 0; 00063 static int mgKWNbWindowsShown = 0; 00064 //========================================================================= 00065 00066 00067 // static vtkMutexLock* mgKWAppLock; 00068 00069 //========================================================================= 00070 VTK_THREAD_RETURN_TYPE vtkKWApplicationStart( void* arg ) 00071 { 00072 bbtkDebugMessage("kw",1," --> Starting KWApplication in separate thread" 00073 <<std::endl); 00074 00075 mgKWApp->Start(); 00076 00077 return VTK_THREAD_RETURN_VALUE; 00078 } 00079 00080 00081 //========================================================================= 00082 00083 //========================================================================= 00084 // BBTKKWSIG 00085 static KW::Signal_type mgKWSignal; 00086 // \BBTKKWSIG 00087 //========================================================================= 00088 00089 //========================================================================= 00090 vtkKWApplication* KW::GetApplication() 00091 { 00092 CreateKWAppIfNeeded(); 00093 return mgKWApp; 00094 } 00095 //========================================================================= 00096 00097 00098 //========================================================================= 00099 void KW::CreateKWAppIfNeeded() 00100 { 00101 if (mgKWApp == 0) 00102 { 00103 bbtkDebugMessage("kw",1," --> Initializing Tcl"<<std::endl); 00104 int argc = 1; 00105 char bbtk[5] = "bbtk"; 00106 char* argv[1]; 00107 argv[0] = bbtk; 00108 Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc, 00109 argv, &std::cerr); 00110 if (!interp) 00111 { 00112 bbtkGlobalError("KW::CreateKWAppIfNeeded() : InitializeTcl failed"); 00113 } 00114 00115 bbtkDebugMessage("kw",1," --> Creating bbtk KWApplication" 00116 <<std::endl); 00117 mgKWApp = vtkKWApplication::New(); 00118 mgKWApp->SetName("bbtk"); 00119 00120 Bbtk_Init(interp); 00121 00122 00123 /* 00124 if (mgMultiThreader == 0) 00125 { 00126 bbtkDebugMessage("kw",1," --> Creating MultiThreader"<<std::endl); 00127 mgMultiThreader = vtkMultiThreader::New(); 00128 } 00129 bbtkDebugMessage("kw",1," --> Spawning KWApplication"<<std::endl); 00130 mgMultiThreader->SetNumberOfThreads( 1 ); 00131 int t = mgMultiThreader->SpawnThread ( vtkKWApplicationStart, 0); 00132 bbtkDebugMessage("kw",1," --> Spawned in thread "<<t<<std::endl); 00133 */ 00134 00135 // Dies as soon as it starts because no window at this point !!! 00136 // mgKWApp->Start(); 00137 00138 /* 00139 clock_t endwait; 00140 endwait = clock () + (100 * CLOCKS_PER_SEC )/ 1000; 00141 while (clock() < endwait) {} 00142 */ 00143 00144 bbtkDebugMessage("kw",1," --> KWApplication running ..." 00145 <<std::endl); 00146 } 00147 } 00148 //========================================================================= 00149 00150 //========================================================================= 00151 void KW::DestroyKWAppIfNeeded() 00152 { 00153 if (mgKWApp!= 0) 00154 { 00155 bbtkDebugMessage("kw",1," --> Destructing bbtk KWApplication"<<std::endl); 00156 mgKWApp->Delete(); 00157 } 00158 } 00159 //========================================================================= 00160 00161 //========================================================================= 00162 /* 00163 void KW::SetTopWindowParent(vtkKWWidget* w) 00164 { 00165 if (mgTopWindowParent != 0) 00166 { 00167 bbtkGlobalError("KW::SetTopWindowParent : top window parent != 0"); 00168 } 00169 mgKWTopWindowParent = w; 00170 } 00171 //========================================================================= 00172 */ 00173 /* 00174 //========================================================================= 00175 void KW::CreateTopWindowIfNeeded() 00176 { 00177 if (mgKWTopWindow!=0) return; 00178 bbtkDebugMessage("kw",1," --> Creating bbtk top KW window"<<std::endl); 00179 00180 CreateKWAppIfNeeded(); 00181 00182 mgKWTopWindow = vtkKWWindowBase::New(); 00183 mgKWTopWindow->SupportHelpOff(); 00184 mgKWApp->AddWindow(mgKWTopWindow); 00185 mgKWTopWindow->Create(); 00186 // mgKWTopWindow->Withdraw() 00187 } 00188 //========================================================================= 00189 00190 //========================================================================= 00191 void KW::DestroyTopWindowIfNeeded() 00192 { 00193 if ( (mgKWNbWindowsAlive==0) && 00194 (mgKWAutoDestroyTopWindow) ) 00195 { 00196 bbtkDebugMessage("kw",1," --> Destructing bbtk top KW window"<<std::endl); 00197 mgKWTopWindow->Close(); 00198 mgKWTopWindow->Delete(); 00199 mgKWTopWindow = 0; 00200 00201 DestroyKWAppIfNeeded(); 00202 } 00203 } 00204 //========================================================================= 00205 */ 00206 /* 00207 //========================================================================= 00208 void KW::LoopUntilAllWindowsClose() 00209 { 00210 int i = 0; 00211 while (mgTopWindow != 0) 00212 { 00213 if (i % 100 == 0) 00214 { 00215 bbtkDebugMessage("kw",2,"KW::Loop "<<i << std::endl); 00216 } 00217 i++; 00218 kwMilliSleep(10); 00219 00220 } 00221 } 00222 //========================================================================= 00223 */ 00224 00225 /* 00226 //========================================================================= 00227 vtkKWWindowBase* KW::GetTopWindow() 00228 { 00229 KW::CreateTopWindowIfNeeded(); 00230 return mgKWTopWindow; 00231 } 00232 //========================================================================= 00233 00234 //========================================================================= 00235 bool KW::TopWindowExists() 00236 { 00237 return (mgKWTopWindow!=0); 00238 } 00239 //========================================================================= 00240 */ 00241 00242 // BBTKKWSIG 00243 //========================================================================= 00244 void KW::AddSignalObserver(Slot_function_type f) 00245 { 00246 mgKWSignal.connect(f); 00247 } 00248 //========================================================================= 00249 // \BBTKKWSIG 00250 00251 /* 00252 //========================================================================= 00253 void KW::SetAutoDestroyTopWindow(bool b) 00254 { 00255 mgKWAutoDestroyTopWindow = b; 00256 } 00257 //========================================================================= 00258 */ 00259 00260 /* 00261 //========================================================================= 00262 void KW::SetTopWindow(vtkKWWindowBase* w) 00263 { 00264 if ( mgKWTopWindow ) 00265 { 00266 bbtkGlobalError("KW::SetTopWindow : top window already set !"); 00267 } 00268 mgKWTopWindow = w; 00269 } 00270 //========================================================================= 00271 */ 00272 00273 //========================================================================= 00274 void KW::IncNbWindowsAlive() 00275 { 00276 mgKWNbWindowsAlive++; 00277 bbtkDebugMessage("kw",2,"* Number of KW windows alive = " 00278 <<mgKWNbWindowsAlive 00279 <<std::endl); 00280 // BBTKKWSIG 00281 mgKWSignal(); 00282 // \BBTKKWSIG 00283 } 00284 //========================================================================= 00285 00286 //========================================================================= 00287 void KW::DecNbWindowsAlive() 00288 { 00289 mgKWNbWindowsAlive--; 00290 bbtkDebugMessage("kw",2,"* Number of KW windows alive = " 00291 <<mgKWNbWindowsAlive 00292 <<std::endl); 00293 00294 // DestroyTopWindowIfNeeded(); 00295 // BBTKKWSIG 00296 mgKWSignal(); 00297 // \BBTKKWSIG 00298 } 00299 //========================================================================= 00300 00301 //========================================================================= 00302 void KW::IncNbWindowsShown() 00303 { 00304 mgKWNbWindowsShown++; 00305 bbtkDebugMessage("kw",2,"* Number of KW windows shown = " 00306 <<mgKWNbWindowsShown 00307 <<std::endl); 00308 // BBTKKWSIG 00309 mgKWSignal(); 00310 // \BBTKKWSIG 00311 } 00312 //========================================================================= 00313 00314 //========================================================================= 00315 void KW::DecNbWindowsShown() 00316 { 00317 mgKWNbWindowsShown--; 00318 bbtkDebugMessage("kw",2,"* Number of KW windows shown = " 00319 <<mgKWNbWindowsShown 00320 <<std::endl); 00321 00322 // DestroyTopWindowIfNeeded(); 00323 00324 // BBTKKWSIG 00325 mgKWSignal(); 00326 // \BBTKKWSIG 00327 } 00328 //========================================================================= 00329 00330 00331 //========================================================================= 00332 int KW::GetNbWindowsAlive() 00333 { 00334 return mgKWNbWindowsAlive; 00335 } 00336 //========================================================================= 00337 00338 //========================================================================= 00339 bool KW::IsSomeWindowAlive() 00340 { 00341 return (mgKWNbWindowsAlive>0); 00342 } 00343 //========================================================================= 00344 00345 00346 //========================================================================= 00347 int KW::GetNbWindowsShown() 00348 { 00349 return mgKWNbWindowsShown; 00350 } 00351 //========================================================================= 00352 00353 //========================================================================= 00354 bool KW::IsSomeWindowShown() 00355 { 00356 return (mgKWNbWindowsShown>0); 00357 } 00358 //========================================================================= 00359 00360 /* 00361 //========================================================================= 00362 KW::BusyCursor::BusyCursor() 00363 { 00364 mCursor = 0; 00365 if (TopWindowExists()) 00366 { 00367 bbtkDebugMessage("kw",2, 00368 "KW::BusyCursor::BusyCursor()"<<std::endl); 00369 mCursor = new kwBusyCursor; 00370 //::kwBeginBusyCursor(); 00371 } 00372 } 00373 KW::BusyCursor::~BusyCursor() 00374 { 00375 if (mCursor) delete mCursor; 00376 } 00377 //========================================================================= 00378 */ 00379 /* 00380 //========================================================================= 00381 void KW::ResetCursor() 00382 { 00383 if (!TopWindowExists()) return; 00384 bbtkDebugMessage("kw",9,"KW::ResetCursor()"<<std::endl); 00385 while (kwIsBusy()) ::kwEndBusyCursor(); 00386 } 00387 //========================================================================= 00388 //========================================================================= 00389 void KW::BeginBusyCursor() 00390 { 00391 if (!TopWindowExists()) return; 00392 bbtkDebugMessage("kw",9,"KW::BeginBusyCursor()"<<std::endl); 00393 ::kwBeginBusyCursor(); 00394 } 00395 //========================================================================= 00396 //========================================================================= 00397 void KW::EndBusyCursor() 00398 { 00399 if (!TopWindowExists()) return; 00400 bbtkDebugMessage("kw",9,"KW::EndBusyCursor()"<<std::endl); 00401 ::kwEndBusyCursor(); 00402 } 00403 //========================================================================= 00404 */ 00405 00406 } 00407 00408 #endif