1 /*=========================================================================
3 Module: $RCSfile: bbtkKW.cxx,v $
5 Date: $Date: 2008/12/12 12:11:21 $
6 Version: $Revision: 1.3 $
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;
58 static vtkKWWindowBase* mgKWTopWindow = 0;
59 static vtkKWWindowBase* mgKWTopWindowParent = 0;
60 static bool mgKWAutoDestroyTopWindow = true;
62 static int mgKWNbWindowsAlive = 0;
63 static int mgKWNbWindowsShown = 0;
64 //=========================================================================
67 // static vtkMutexLock* mgKWAppLock;
69 //=========================================================================
70 VTK_THREAD_RETURN_TYPE vtkKWApplicationStart( void* arg )
72 bbtkDebugMessage("kw",1," --> Starting KWApplication in separate thread"
77 return VTK_THREAD_RETURN_VALUE;
81 //=========================================================================
83 //=========================================================================
85 static KW::Signal_type mgKWSignal;
87 //=========================================================================
89 //=========================================================================
90 vtkKWApplication* KW::GetApplication()
92 CreateKWAppIfNeeded();
95 //=========================================================================
98 //=========================================================================
99 void KW::CreateKWAppIfNeeded()
103 bbtkDebugMessage("kw",1," --> Initializing Tcl"<<std::endl);
105 char bbtk[5] = "bbtk";
108 Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc,
112 bbtkGlobalError("KW::CreateKWAppIfNeeded() : InitializeTcl failed");
115 bbtkDebugMessage("kw",1," --> Creating bbtk KWApplication"
117 mgKWApp = vtkKWApplication::New();
118 mgKWApp->SetName("bbtk");
124 if (mgMultiThreader == 0)
126 bbtkDebugMessage("kw",1," --> Creating MultiThreader"<<std::endl);
127 mgMultiThreader = vtkMultiThreader::New();
129 bbtkDebugMessage("kw",1," --> Spawning KWApplication"<<std::endl);
130 mgMultiThreader->SetNumberOfThreads( 1 );
131 int t = mgMultiThreader->SpawnThread ( vtkKWApplicationStart, 0);
132 bbtkDebugMessage("kw",1," --> Spawned in thread "<<t<<std::endl);
135 // Dies as soon as it starts because no window at this point !!!
140 endwait = clock () + (100 * CLOCKS_PER_SEC )/ 1000;
141 while (clock() < endwait) {}
144 bbtkDebugMessage("kw",1," --> KWApplication running ..."
148 //=========================================================================
150 //=========================================================================
151 void KW::DestroyKWAppIfNeeded()
155 bbtkDebugMessage("kw",1," --> Destructing bbtk KWApplication"<<std::endl);
159 //=========================================================================
161 //=========================================================================
163 void KW::SetTopWindowParent(vtkKWWidget* w)
165 if (mgTopWindowParent != 0)
167 bbtkGlobalError("KW::SetTopWindowParent : top window parent != 0");
169 mgKWTopWindowParent = w;
171 //=========================================================================
174 //=========================================================================
175 void KW::CreateTopWindowIfNeeded()
177 if (mgKWTopWindow!=0) return;
178 bbtkDebugMessage("kw",1," --> Creating bbtk top KW window"<<std::endl);
180 CreateKWAppIfNeeded();
182 mgKWTopWindow = vtkKWWindowBase::New();
183 mgKWTopWindow->SupportHelpOff();
184 mgKWApp->AddWindow(mgKWTopWindow);
185 mgKWTopWindow->Create();
186 // mgKWTopWindow->Withdraw()
188 //=========================================================================
190 //=========================================================================
191 void KW::DestroyTopWindowIfNeeded()
193 if ( (mgKWNbWindowsAlive==0) &&
194 (mgKWAutoDestroyTopWindow) )
196 bbtkDebugMessage("kw",1," --> Destructing bbtk top KW window"<<std::endl);
197 mgKWTopWindow->Close();
198 mgKWTopWindow->Delete();
201 DestroyKWAppIfNeeded();
204 //=========================================================================
207 //=========================================================================
208 void KW::LoopUntilAllWindowsClose()
211 while (mgTopWindow != 0)
215 bbtkDebugMessage("kw",2,"KW::Loop "<<i << std::endl);
222 //=========================================================================
226 //=========================================================================
227 vtkKWWindowBase* KW::GetTopWindow()
229 KW::CreateTopWindowIfNeeded();
230 return mgKWTopWindow;
232 //=========================================================================
234 //=========================================================================
235 bool KW::TopWindowExists()
237 return (mgKWTopWindow!=0);
239 //=========================================================================
243 //=========================================================================
244 void KW::AddSignalObserver(Slot_function_type f)
246 mgKWSignal.connect(f);
248 //=========================================================================
252 //=========================================================================
253 void KW::SetAutoDestroyTopWindow(bool b)
255 mgKWAutoDestroyTopWindow = b;
257 //=========================================================================
261 //=========================================================================
262 void KW::SetTopWindow(vtkKWWindowBase* w)
266 bbtkGlobalError("KW::SetTopWindow : top window already set !");
270 //=========================================================================
273 //=========================================================================
274 void KW::IncNbWindowsAlive()
276 mgKWNbWindowsAlive++;
277 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
284 //=========================================================================
286 //=========================================================================
287 void KW::DecNbWindowsAlive()
289 mgKWNbWindowsAlive--;
290 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
294 // DestroyTopWindowIfNeeded();
299 //=========================================================================
301 //=========================================================================
302 void KW::IncNbWindowsShown()
304 mgKWNbWindowsShown++;
305 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
312 //=========================================================================
314 //=========================================================================
315 void KW::DecNbWindowsShown()
317 mgKWNbWindowsShown--;
318 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
322 // DestroyTopWindowIfNeeded();
328 //=========================================================================
331 //=========================================================================
332 int KW::GetNbWindowsAlive()
334 return mgKWNbWindowsAlive;
336 //=========================================================================
338 //=========================================================================
339 bool KW::IsSomeWindowAlive()
341 return (mgKWNbWindowsAlive>0);
343 //=========================================================================
346 //=========================================================================
347 int KW::GetNbWindowsShown()
349 return mgKWNbWindowsShown;
351 //=========================================================================
353 //=========================================================================
354 bool KW::IsSomeWindowShown()
356 return (mgKWNbWindowsShown>0);
358 //=========================================================================
361 //=========================================================================
362 KW::BusyCursor::BusyCursor()
365 if (TopWindowExists())
367 bbtkDebugMessage("kw",2,
368 "KW::BusyCursor::BusyCursor()"<<std::endl);
369 mCursor = new kwBusyCursor;
370 //::kwBeginBusyCursor();
373 KW::BusyCursor::~BusyCursor()
375 if (mCursor) delete mCursor;
377 //=========================================================================
380 //=========================================================================
381 void KW::ResetCursor()
383 if (!TopWindowExists()) return;
384 bbtkDebugMessage("kw",9,"KW::ResetCursor()"<<std::endl);
385 while (kwIsBusy()) ::kwEndBusyCursor();
387 //=========================================================================
388 //=========================================================================
389 void KW::BeginBusyCursor()
391 if (!TopWindowExists()) return;
392 bbtkDebugMessage("kw",9,"KW::BeginBusyCursor()"<<std::endl);
393 ::kwBeginBusyCursor();
395 //=========================================================================
396 //=========================================================================
397 void KW::EndBusyCursor()
399 if (!TopWindowExists()) return;
400 bbtkDebugMessage("kw",9,"KW::EndBusyCursor()"<<std::endl);
403 //=========================================================================