1 /*=========================================================================
3 Module: $RCSfile: bbtkKW.cxx,v $
5 Date: $Date: 2008/12/02 08:38:08 $
6 Version: $Revision: 1.2 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
34 #include "bbtkMessageManager.h"
35 #include "vtkKWApplication.h"
38 //#include "vtkConditionVariable.h"
39 #include "vtkMultiThreader.h"
43 extern "C" int Bbtk_Init(Tcl_Interp *interp);
49 bbtk::MessageManager::RegisterMessageType("kw",
50 "KWWidgets related messages",0);
54 //=========================================================================
55 static vtkKWApplication* mgKWApp = 0;
56 static vtkMultiThreader* mgMultiThreader = 0; /*
57 static vtkKWWindowBase* mgKWTopWindow = 0;
58 static vtkKWWindowBase* mgKWTopWindowParent = 0;
59 static bool mgKWAutoDestroyTopWindow = true;
61 static int mgKWNbWindowsAlive = 0;
62 static int mgKWNbWindowsShown = 0;
63 //=========================================================================
66 // static vtkMutexLock* mgKWAppLock;
68 //=========================================================================
69 VTK_THREAD_RETURN_TYPE vtkKWApplicationStart( void* arg )
71 bbtkDebugMessage("kw",1," --> Starting KWApplication in separate thread"
76 return VTK_THREAD_RETURN_VALUE;
80 //=========================================================================
82 //=========================================================================
84 static KW::Signal_type mgKWSignal;
86 //=========================================================================
88 //=========================================================================
89 vtkKWApplication* KW::GetApplication()
91 CreateKWAppIfNeeded();
94 //=========================================================================
97 //=========================================================================
98 void KW::CreateKWAppIfNeeded()
102 bbtkDebugMessage("kw",1," --> Initializing Tcl"<<std::endl);
104 char bbtk[5] = "bbtk";
107 Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc,
111 bbtkGlobalError("KW::CreateKWAppIfNeeded() : InitializeTcl failed");
114 bbtkDebugMessage("kw",1," --> Creating bbtk KWApplication"
116 mgKWApp = vtkKWApplication::New();
117 mgKWApp->SetName("bbtk");
123 if (mgMultiThreader == 0)
125 bbtkDebugMessage("kw",1," --> Creating MultiThreader"<<std::endl);
126 mgMultiThreader = vtkMultiThreader::New();
128 bbtkDebugMessage("kw",1," --> Spawning KWApplication"<<std::endl);
129 mgMultiThreader->SetNumberOfThreads( 1 );
130 int t = mgMultiThreader->SpawnThread ( vtkKWApplicationStart, 0);
131 bbtkDebugMessage("kw",1," --> Spawned in thread "<<t<<std::endl);
134 // Dies as soon as it starts because no window at this point !!!
139 endwait = clock () + (100 * CLOCKS_PER_SEC )/ 1000;
140 while (clock() < endwait) {}
143 bbtkDebugMessage("kw",1," --> KWApplication running ..."
147 //=========================================================================
149 //=========================================================================
150 void KW::DestroyKWAppIfNeeded()
154 bbtkDebugMessage("kw",1," --> Destructing bbtk KWApplication"<<std::endl);
158 //=========================================================================
160 //=========================================================================
162 void KW::SetTopWindowParent(vtkKWWidget* w)
164 if (mgTopWindowParent != 0)
166 bbtkGlobalError("KW::SetTopWindowParent : top window parent != 0");
168 mgKWTopWindowParent = w;
170 //=========================================================================
173 //=========================================================================
174 void KW::CreateTopWindowIfNeeded()
176 if (mgKWTopWindow!=0) return;
177 bbtkDebugMessage("kw",1," --> Creating bbtk top KW window"<<std::endl);
179 CreateKWAppIfNeeded();
181 mgKWTopWindow = vtkKWWindowBase::New();
182 mgKWTopWindow->SupportHelpOff();
183 mgKWApp->AddWindow(mgKWTopWindow);
184 mgKWTopWindow->Create();
185 // mgKWTopWindow->Withdraw()
187 //=========================================================================
189 //=========================================================================
190 void KW::DestroyTopWindowIfNeeded()
192 if ( (mgKWNbWindowsAlive==0) &&
193 (mgKWAutoDestroyTopWindow) )
195 bbtkDebugMessage("kw",1," --> Destructing bbtk top KW window"<<std::endl);
196 mgKWTopWindow->Close();
197 mgKWTopWindow->Delete();
200 DestroyKWAppIfNeeded();
203 //=========================================================================
206 //=========================================================================
207 void KW::LoopUntilAllWindowsClose()
210 while (mgTopWindow != 0)
214 bbtkDebugMessage("kw",2,"KW::Loop "<<i << std::endl);
221 //=========================================================================
225 //=========================================================================
226 vtkKWWindowBase* KW::GetTopWindow()
228 KW::CreateTopWindowIfNeeded();
229 return mgKWTopWindow;
231 //=========================================================================
233 //=========================================================================
234 bool KW::TopWindowExists()
236 return (mgKWTopWindow!=0);
238 //=========================================================================
242 //=========================================================================
243 void KW::AddSignalObserver(Slot_function_type f)
245 mgKWSignal.connect(f);
247 //=========================================================================
251 //=========================================================================
252 void KW::SetAutoDestroyTopWindow(bool b)
254 mgKWAutoDestroyTopWindow = b;
256 //=========================================================================
260 //=========================================================================
261 void KW::SetTopWindow(vtkKWWindowBase* w)
265 bbtkGlobalError("KW::SetTopWindow : top window already set !");
269 //=========================================================================
272 //=========================================================================
273 void KW::IncNbWindowsAlive()
275 mgKWNbWindowsAlive++;
276 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
283 //=========================================================================
285 //=========================================================================
286 void KW::DecNbWindowsAlive()
288 mgKWNbWindowsAlive--;
289 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
293 // DestroyTopWindowIfNeeded();
298 //=========================================================================
300 //=========================================================================
301 void KW::IncNbWindowsShown()
303 mgKWNbWindowsShown++;
304 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
311 //=========================================================================
313 //=========================================================================
314 void KW::DecNbWindowsShown()
316 mgKWNbWindowsShown--;
317 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
321 // DestroyTopWindowIfNeeded();
327 //=========================================================================
330 //=========================================================================
331 int KW::GetNbWindowsAlive()
333 return mgKWNbWindowsAlive;
335 //=========================================================================
337 //=========================================================================
338 bool KW::IsSomeWindowAlive()
340 return (mgKWNbWindowsAlive>0);
342 //=========================================================================
345 //=========================================================================
346 int KW::GetNbWindowsShown()
348 return mgKWNbWindowsShown;
350 //=========================================================================
352 //=========================================================================
353 bool KW::IsSomeWindowShown()
355 return (mgKWNbWindowsShown>0);
357 //=========================================================================
360 //=========================================================================
361 KW::BusyCursor::BusyCursor()
364 if (TopWindowExists())
366 bbtkDebugMessage("kw",2,
367 "KW::BusyCursor::BusyCursor()"<<std::endl);
368 mCursor = new kwBusyCursor;
369 //::kwBeginBusyCursor();
372 KW::BusyCursor::~BusyCursor()
374 if (mCursor) delete mCursor;
376 //=========================================================================
379 //=========================================================================
380 void KW::ResetCursor()
382 if (!TopWindowExists()) return;
383 bbtkDebugMessage("kw",9,"KW::ResetCursor()"<<std::endl);
384 while (kwIsBusy()) ::kwEndBusyCursor();
386 //=========================================================================
387 //=========================================================================
388 void KW::BeginBusyCursor()
390 if (!TopWindowExists()) return;
391 bbtkDebugMessage("kw",9,"KW::BeginBusyCursor()"<<std::endl);
392 ::kwBeginBusyCursor();
394 //=========================================================================
395 //=========================================================================
396 void KW::EndBusyCursor()
398 if (!TopWindowExists()) return;
399 bbtkDebugMessage("kw",9,"KW::EndBusyCursor()"<<std::endl);
402 //=========================================================================