2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkKW.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.4 $
34 =========================================================================*/
41 #include "bbtkMessageManager.h"
42 #include "vtkKWApplication.h"
45 //#include "vtkConditionVariable.h"
46 //#include "vtkMultiThreader.h"
50 extern "C" int Bbtk_Init(Tcl_Interp *interp);
56 bbtk::MessageManager::RegisterMessageType("kw",
57 "KWWidgets related messages",0);
61 //=========================================================================
62 static vtkKWApplication* mgKWApp = 0;
63 //static vtkMultiThreader* mgMultiThreader = 0;
65 static vtkKWWindowBase* mgKWTopWindow = 0;
66 static vtkKWWindowBase* mgKWTopWindowParent = 0;
67 static bool mgKWAutoDestroyTopWindow = true;
69 static int mgKWNbWindowsAlive = 0;
70 static int mgKWNbWindowsShown = 0;
71 //=========================================================================
74 // static vtkMutexLock* mgKWAppLock;
76 //=========================================================================
77 VTK_THREAD_RETURN_TYPE vtkKWApplicationStart( void* arg )
79 bbtkDebugMessage("kw",1," --> Starting KWApplication in separate thread"
84 return VTK_THREAD_RETURN_VALUE;
88 //=========================================================================
90 //=========================================================================
92 static KW::Signal_type mgKWSignal;
94 //=========================================================================
96 //=========================================================================
97 vtkKWApplication* KW::GetApplication()
99 CreateKWAppIfNeeded();
102 //=========================================================================
105 //=========================================================================
106 void KW::CreateKWAppIfNeeded()
110 bbtkDebugMessage("kw",1," --> Initializing Tcl"<<std::endl);
112 char bbtk[5] = "bbtk";
115 Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc,
119 bbtkGlobalError("KW::CreateKWAppIfNeeded() : InitializeTcl failed");
122 bbtkDebugMessage("kw",1," --> Creating bbtk KWApplication"
124 mgKWApp = vtkKWApplication::New();
125 mgKWApp->SetName("bbtk");
131 if (mgMultiThreader == 0)
133 bbtkDebugMessage("kw",1," --> Creating MultiThreader"<<std::endl);
134 mgMultiThreader = vtkMultiThreader::New();
136 bbtkDebugMessage("kw",1," --> Spawning KWApplication"<<std::endl);
137 mgMultiThreader->SetNumberOfThreads( 1 );
138 int t = mgMultiThreader->SpawnThread ( vtkKWApplicationStart, 0);
139 bbtkDebugMessage("kw",1," --> Spawned in thread "<<t<<std::endl);
142 // Dies as soon as it starts because no window at this point !!!
147 endwait = clock () + (100 * CLOCKS_PER_SEC )/ 1000;
148 while (clock() < endwait) {}
151 bbtkDebugMessage("kw",1," --> KWApplication running ..."
155 //=========================================================================
157 //=========================================================================
158 void KW::DestroyKWAppIfNeeded()
162 bbtkDebugMessage("kw",1," --> Destructing bbtk KWApplication"<<std::endl);
166 //=========================================================================
168 //=========================================================================
170 void KW::SetTopWindowParent(vtkKWWidget* w)
172 if (mgTopWindowParent != 0)
174 bbtkGlobalError("KW::SetTopWindowParent : top window parent != 0");
176 mgKWTopWindowParent = w;
178 //=========================================================================
181 //=========================================================================
182 void KW::CreateTopWindowIfNeeded()
184 if (mgKWTopWindow!=0) return;
185 bbtkDebugMessage("kw",1," --> Creating bbtk top KW window"<<std::endl);
187 CreateKWAppIfNeeded();
189 mgKWTopWindow = vtkKWWindowBase::New();
190 mgKWTopWindow->SupportHelpOff();
191 mgKWApp->AddWindow(mgKWTopWindow);
192 mgKWTopWindow->Create();
193 // mgKWTopWindow->Withdraw()
195 //=========================================================================
197 //=========================================================================
198 void KW::DestroyTopWindowIfNeeded()
200 if ( (mgKWNbWindowsAlive==0) &&
201 (mgKWAutoDestroyTopWindow) )
203 bbtkDebugMessage("kw",1," --> Destructing bbtk top KW window"<<std::endl);
204 mgKWTopWindow->Close();
205 mgKWTopWindow->Delete();
208 DestroyKWAppIfNeeded();
211 //=========================================================================
214 //=========================================================================
215 void KW::LoopUntilAllWindowsClose()
218 while (mgTopWindow != 0)
222 bbtkDebugMessage("kw",2,"KW::Loop "<<i << std::endl);
229 //=========================================================================
233 //=========================================================================
234 vtkKWWindowBase* KW::GetTopWindow()
236 KW::CreateTopWindowIfNeeded();
237 return mgKWTopWindow;
239 //=========================================================================
241 //=========================================================================
242 bool KW::TopWindowExists()
244 return (mgKWTopWindow!=0);
246 //=========================================================================
250 //=========================================================================
251 void KW::AddSignalObserver(Slot_function_type f)
253 mgKWSignal.connect(f);
255 //=========================================================================
259 //=========================================================================
260 void KW::SetAutoDestroyTopWindow(bool b)
262 mgKWAutoDestroyTopWindow = b;
264 //=========================================================================
268 //=========================================================================
269 void KW::SetTopWindow(vtkKWWindowBase* w)
273 bbtkGlobalError("KW::SetTopWindow : top window already set !");
277 //=========================================================================
280 //=========================================================================
281 void KW::IncNbWindowsAlive()
283 mgKWNbWindowsAlive++;
284 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
291 //=========================================================================
293 //=========================================================================
294 void KW::DecNbWindowsAlive()
296 mgKWNbWindowsAlive--;
297 bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
301 // DestroyTopWindowIfNeeded();
306 //=========================================================================
308 //=========================================================================
309 void KW::IncNbWindowsShown()
311 mgKWNbWindowsShown++;
312 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
319 //=========================================================================
321 //=========================================================================
322 void KW::DecNbWindowsShown()
324 mgKWNbWindowsShown--;
325 bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
329 // DestroyTopWindowIfNeeded();
335 //=========================================================================
338 //=========================================================================
339 int KW::GetNbWindowsAlive()
341 return mgKWNbWindowsAlive;
343 //=========================================================================
345 //=========================================================================
346 bool KW::IsSomeWindowAlive()
348 return (mgKWNbWindowsAlive>0);
350 //=========================================================================
353 //=========================================================================
354 int KW::GetNbWindowsShown()
356 return mgKWNbWindowsShown;
358 //=========================================================================
360 //=========================================================================
361 bool KW::IsSomeWindowShown()
363 return (mgKWNbWindowsShown>0);
365 //=========================================================================
368 //=========================================================================
369 KW::BusyCursor::BusyCursor()
372 if (TopWindowExists())
374 bbtkDebugMessage("kw",2,
375 "KW::BusyCursor::BusyCursor()"<<std::endl);
376 mCursor = new kwBusyCursor;
377 //::kwBeginBusyCursor();
380 KW::BusyCursor::~BusyCursor()
382 if (mCursor) delete mCursor;
384 //=========================================================================
387 //=========================================================================
388 void KW::ResetCursor()
390 if (!TopWindowExists()) return;
391 bbtkDebugMessage("kw",9,"KW::ResetCursor()"<<std::endl);
392 while (kwIsBusy()) ::kwEndBusyCursor();
394 //=========================================================================
395 //=========================================================================
396 void KW::BeginBusyCursor()
398 if (!TopWindowExists()) return;
399 bbtkDebugMessage("kw",9,"KW::BeginBusyCursor()"<<std::endl);
400 ::kwBeginBusyCursor();
402 //=========================================================================
403 //=========================================================================
404 void KW::EndBusyCursor()
406 if (!TopWindowExists()) return;
407 bbtkDebugMessage("kw",9,"KW::EndBusyCursor()"<<std::endl);
410 //=========================================================================