1 /*=========================================================================
3 Module: $RCSfile: bbtkKW.cxx,v $
5 Date: $Date: 2008/11/29 21:41:32 $
6 Version: $Revision: 1.1 $
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"
46 bbtk::MessageManager::RegisterMessageType("kw",
47 "KWWidgets related messages",0);
51 //=========================================================================
52 static vtkKWApplication* mgKWApp = 0;
53 static vtkMultiThreader* mgMultiThreader = 0; /*
54 static vtkKWWindowBase* mgKWTopWindow = 0;
55 static vtkKWWindowBase* mgKWTopWindowParent = 0;
56 static bool mgKWAutoDestroyTopWindow = true;
58 static int mgKWNbWindowsAlive = 0;
59 static int mgKWNbWindowsShown = 0;
60 //=========================================================================
63 // static vtkMutexLock* mgKWAppLock;
65 //=========================================================================
66 VTK_THREAD_RETURN_TYPE vtkKWApplicationStart( void* arg )
68 bbtkDebugMessage("kw",1," --> Starting KWApplication in separate thread"
73 return VTK_THREAD_RETURN_VALUE;
77 //=========================================================================
79 //=========================================================================
81 static KW::Signal_type mgKWSignal;
83 //=========================================================================
85 //=========================================================================
86 vtkKWApplication* KW::GetApplication()
88 CreateKWAppIfNeeded();
91 //=========================================================================
94 //=========================================================================
95 void KW::CreateKWAppIfNeeded()
99 bbtkDebugMessage("kw",1," --> Initializing Tcl"<<std::endl);
101 char bbtk[5] = "bbtk";
104 Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc,
108 bbtkGlobalError("KW::CreateKWAppIfNeeded() : InitializeTcl failed");
111 bbtkDebugMessage("kw",1," --> Creating bbtk KWApplication"
113 mgKWApp = vtkKWApplication::New();
114 mgKWApp->SetName("bbtk");
117 if (mgMultiThreader == 0)
119 bbtkDebugMessage("kw",1," --> Creating MultiThreader"<<std::endl);
120 mgMultiThreader = vtkMultiThreader::New();
122 bbtkDebugMessage("kw",1," --> Spawning KWApplication"<<std::endl);
123 mgMultiThreader->SetNumberOfThreads( 1 );
124 int t = mgMultiThreader->SpawnThread ( vtkKWApplicationStart, 0);
125 bbtkDebugMessage("kw",1," --> Spawned in thread "<<t<<std::endl);
128 // Dies as soon as it starts because no window at this point !!!
133 endwait = clock () + (100 * CLOCKS_PER_SEC )/ 1000;
134 while (clock() < endwait) {}
137 bbtkDebugMessage("kw",1," --> KWApplication running ..."
141 //=========================================================================
143 //=========================================================================
144 void KW::DestroyKWAppIfNeeded()
148 bbtkDebugMessage("kw",1," --> Destructing bbtk KWApplication"<<std::endl);
152 //=========================================================================
154 //=========================================================================
156 void KW::SetTopWindowParent(vtkKWWidget* w)
158 if (mgTopWindowParent != 0)
160 bbtkGlobalError("KW::SetTopWindowParent : top window parent != 0");
162 mgKWTopWindowParent = w;
164 //=========================================================================
167 //=========================================================================
168 void KW::CreateTopWindowIfNeeded()
170 if (mgKWTopWindow!=0) return;
171 bbtkDebugMessage("kw",1," --> Creating bbtk top KW window"<<std::endl);
173 CreateKWAppIfNeeded();
175 mgKWTopWindow = vtkKWWindowBase::New();
176 mgKWTopWindow->SupportHelpOff();
177 mgKWApp->AddWindow(mgKWTopWindow);
178 mgKWTopWindow->Create();
179 // mgKWTopWindow->Withdraw()
181 //=========================================================================
183 //=========================================================================
184 void KW::DestroyTopWindowIfNeeded()
186 if ( (mgKWNbWindowsAlive==0) &&
187 (mgKWAutoDestroyTopWindow) )
189 bbtkDebugMessage("kw",1," --> Destructing bbtk top KW window"<<std::endl);
190 mgKWTopWindow->Close();
191 mgKWTopWindow->Delete();
194 DestroyKWAppIfNeeded();
197 //=========================================================================
200 //=========================================================================
201 void KW::LoopUntilAllWindowsClose()
204 while (mgTopWindow != 0)
208 bbtkDebugMessage("kw",2,"KW::Loop "<<i << std::endl);
215 //=========================================================================
219 //=========================================================================
220 vtkKWWindowBase* KW::GetTopWindow()
222 KW::CreateTopWindowIfNeeded();
223 return mgKWTopWindow;
225 //=========================================================================
227 //=========================================================================
228 bool KW::TopWindowExists()
230 return (mgKWTopWindow!=0);
232 //=========================================================================
236 //=========================================================================
237 void KW::AddSignalObserver(Slot_function_type f)
239 mgKWSignal.connect(f);
241 //=========================================================================
245 //=========================================================================
246 void KW::SetAutoDestroyTopWindow(bool b)
248 mgKWAutoDestroyTopWindow = b;
250 //=========================================================================
254 //=========================================================================
255 void KW::SetTopWindow(vtkKWWindowBase* w)
259 bbtkGlobalError("KW::SetTopWindow : top window already set !");
263 //=========================================================================
266 //=========================================================================
267 void KW::IncNbWindowsAlive()
269 mgKWNbWindowsAlive++;
270 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
277 //=========================================================================
279 //=========================================================================
280 void KW::DecNbWindowsAlive()
282 mgKWNbWindowsAlive--;
283 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
287 // DestroyTopWindowIfNeeded();
292 //=========================================================================
294 //=========================================================================
295 void KW::IncNbWindowsShown()
297 mgKWNbWindowsShown++;
298 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
305 //=========================================================================
307 //=========================================================================
308 void KW::DecNbWindowsShown()
310 mgKWNbWindowsShown--;
311 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
315 // DestroyTopWindowIfNeeded();
321 //=========================================================================
324 //=========================================================================
325 int KW::GetNbWindowsAlive()
327 return mgKWNbWindowsAlive;
329 //=========================================================================
331 //=========================================================================
332 bool KW::IsSomeWindowAlive()
334 return (mgKWNbWindowsAlive>0);
336 //=========================================================================
339 //=========================================================================
340 int KW::GetNbWindowsShown()
342 return mgKWNbWindowsShown;
344 //=========================================================================
346 //=========================================================================
347 bool KW::IsSomeWindowShown()
349 return (mgKWNbWindowsShown>0);
351 //=========================================================================
354 //=========================================================================
355 KW::BusyCursor::BusyCursor()
358 if (TopWindowExists())
360 bbtkDebugMessage("kw",2,
361 "KW::BusyCursor::BusyCursor()"<<std::endl);
362 mCursor = new kwBusyCursor;
363 //::kwBeginBusyCursor();
366 KW::BusyCursor::~BusyCursor()
368 if (mCursor) delete mCursor;
370 //=========================================================================
373 //=========================================================================
374 void KW::ResetCursor()
376 if (!TopWindowExists()) return;
377 bbtkDebugMessage("kw",9,"KW::ResetCursor()"<<std::endl);
378 while (kwIsBusy()) ::kwEndBusyCursor();
380 //=========================================================================
381 //=========================================================================
382 void KW::BeginBusyCursor()
384 if (!TopWindowExists()) return;
385 bbtkDebugMessage("kw",9,"KW::BeginBusyCursor()"<<std::endl);
386 ::kwBeginBusyCursor();
388 //=========================================================================
389 //=========================================================================
390 void KW::EndBusyCursor()
392 if (!TopWindowExists()) return;
393 bbtkDebugMessage("kw",9,"KW::EndBusyCursor()"<<std::endl);
396 //=========================================================================