]> Creatis software - bbtk.git/blob - kernel/src/bbtkKW.cxx
f4eb6fcd5d31276d505753d9d92bb2fe3446fb7b
[bbtk.git] / kernel / src / bbtkKW.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkKW.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/11/29 21:41:32 $
6   Version:   $Revision: 1.1 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31 #ifdef USE_KWWIDGETS
32
33 #include "bbtkKW.h"
34 #include "bbtkMessageManager.h"
35 #include "vtkKWApplication.h"
36
37
38 //#include "vtkConditionVariable.h"
39 #include "vtkMultiThreader.h"
40
41 #include <ctime>
42 namespace bbtk
43 {
44
45   bool _dummy_rmt = 
46     bbtk::MessageManager::RegisterMessageType("kw",
47                                               "KWWidgets related messages",0);
48
49
50
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;
57   */
58   static int mgKWNbWindowsAlive  = 0;
59   static int mgKWNbWindowsShown  = 0;
60   //=========================================================================
61
62   
63   // static vtkMutexLock* mgKWAppLock;
64
65   //=========================================================================
66   VTK_THREAD_RETURN_TYPE vtkKWApplicationStart( void* arg )
67   {  
68     bbtkDebugMessage("kw",1,"  --> Starting KWApplication in separate thread"
69                      <<std::endl);
70
71     mgKWApp->Start();
72     
73     return VTK_THREAD_RETURN_VALUE;
74   }
75
76
77   //=========================================================================
78
79   //=========================================================================
80   // BBTKKWSIG
81   static KW::Signal_type mgKWSignal;
82   // \BBTKKWSIG
83   //=========================================================================
84
85   //=========================================================================
86   vtkKWApplication* KW::GetApplication()
87   {
88     CreateKWAppIfNeeded();
89     return mgKWApp;
90   }
91   //=========================================================================
92
93
94   //=========================================================================
95   void KW::CreateKWAppIfNeeded()
96   {
97     if (mgKWApp == 0) 
98       {
99         bbtkDebugMessage("kw",1,"  --> Initializing Tcl"<<std::endl);
100         int argc = 1;
101         char bbtk[5] = "bbtk";
102         char* argv[1];
103         argv[0] = bbtk;
104         Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc, 
105                                                              argv, &std::cerr);
106         if (!interp)
107           {
108             bbtkGlobalError("KW::CreateKWAppIfNeeded() : InitializeTcl failed");
109           }
110
111         bbtkDebugMessage("kw",1,"  --> Creating bbtk KWApplication"
112                          <<std::endl);
113         mgKWApp = vtkKWApplication::New();
114         mgKWApp->SetName("bbtk");
115
116         /*
117         if (mgMultiThreader == 0)
118           {
119             bbtkDebugMessage("kw",1,"  --> Creating MultiThreader"<<std::endl);
120             mgMultiThreader = vtkMultiThreader::New();
121           }
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);
126         */
127
128         // Dies as soon as it starts because no window at this point !!!
129         mgKWApp->Start();
130
131         /*
132         clock_t endwait;
133         endwait = clock () + (100 * CLOCKS_PER_SEC )/ 1000;
134         while (clock() < endwait) {}
135         */
136  
137         bbtkDebugMessage("kw",1,"  --> KWApplication running ..."
138                          <<std::endl);
139      } 
140   }
141   //=========================================================================
142
143   //=========================================================================
144   void KW::DestroyKWAppIfNeeded()
145   {
146     if (mgKWApp!= 0) 
147       {
148         bbtkDebugMessage("kw",1,"  --> Destructing bbtk KWApplication"<<std::endl);
149         mgKWApp->Delete();
150       }
151   }
152   //=========================================================================
153
154   //========================================================================= 
155   /* 
156   void KW::SetTopWindowParent(vtkKWWidget* w)
157   {
158     if (mgTopWindowParent != 0)
159       {
160         bbtkGlobalError("KW::SetTopWindowParent : top window parent != 0");
161       }
162     mgKWTopWindowParent = w;
163   }
164   //=========================================================================
165   */
166   /*
167   //=========================================================================
168   void KW::CreateTopWindowIfNeeded()
169   {
170     if (mgKWTopWindow!=0) return;
171     bbtkDebugMessage("kw",1,"  --> Creating bbtk top KW window"<<std::endl);
172
173     CreateKWAppIfNeeded();
174
175     mgKWTopWindow = vtkKWWindowBase::New();
176     mgKWTopWindow->SupportHelpOff();
177     mgKWApp->AddWindow(mgKWTopWindow);
178     mgKWTopWindow->Create();
179     //    mgKWTopWindow->Withdraw()
180   }
181   //=========================================================================
182
183   //=========================================================================
184   void KW::DestroyTopWindowIfNeeded()
185   {
186     if ( (mgKWNbWindowsAlive==0) && 
187          (mgKWAutoDestroyTopWindow) )
188       {
189         bbtkDebugMessage("kw",1,"  --> Destructing bbtk top KW window"<<std::endl);
190         mgKWTopWindow->Close();
191         mgKWTopWindow->Delete();
192         mgKWTopWindow = 0;
193         
194         DestroyKWAppIfNeeded();
195       }
196   } 
197   //=========================================================================
198   */
199   /*
200   //=========================================================================
201   void KW::LoopUntilAllWindowsClose()
202   {
203     int i = 0;
204     while (mgTopWindow != 0)
205       {
206         if (i % 100 == 0) 
207           {
208             bbtkDebugMessage("kw",2,"KW::Loop "<<i << std::endl);
209           }
210         i++;
211         kwMilliSleep(10);
212
213       }
214   }
215   //=========================================================================
216   */
217
218   /*
219   //=========================================================================
220   vtkKWWindowBase* KW::GetTopWindow() 
221   { 
222     KW::CreateTopWindowIfNeeded();
223     return mgKWTopWindow; 
224   }
225   //=========================================================================
226   
227   //=========================================================================
228   bool KW::TopWindowExists()
229   {
230     return (mgKWTopWindow!=0);
231   }
232   //=========================================================================
233   */
234
235   // BBTKKWSIG
236   //=========================================================================
237   void KW::AddSignalObserver(Slot_function_type f)
238   {
239     mgKWSignal.connect(f);
240   }
241   //=========================================================================
242   // \BBTKKWSIG
243
244   /*
245   //=========================================================================
246   void KW::SetAutoDestroyTopWindow(bool b)
247   {
248     mgKWAutoDestroyTopWindow = b;
249   }
250   //=========================================================================
251   */  
252
253   /*
254   //=========================================================================
255   void KW::SetTopWindow(vtkKWWindowBase* w) 
256   {
257     if ( mgKWTopWindow ) 
258       {
259         bbtkGlobalError("KW::SetTopWindow : top window already set !");
260       } 
261     mgKWTopWindow = w;
262   }
263   //=========================================================================
264   */
265
266   //=========================================================================
267   void KW::IncNbWindowsAlive() 
268   { 
269     mgKWNbWindowsAlive++; 
270     bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
271                      <<mgKWNbWindowsAlive
272                      <<std::endl);
273     // BBTKKWSIG
274     mgKWSignal();
275     // \BBTKKWSIG
276   }
277   //=========================================================================
278
279   //=========================================================================
280   void KW::DecNbWindowsAlive()
281   { 
282     mgKWNbWindowsAlive--; 
283     bbtkDebugMessage("kw",2,"* Number of KW windows alive = "
284                      <<mgKWNbWindowsAlive
285                      <<std::endl);
286
287     //    DestroyTopWindowIfNeeded();
288      // BBTKKWSIG
289     mgKWSignal();
290     // \BBTKKWSIG
291   }
292   //=========================================================================
293   
294   //=========================================================================
295   void KW::IncNbWindowsShown() 
296   { 
297     mgKWNbWindowsShown++; 
298     bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
299                      <<mgKWNbWindowsShown
300                      <<std::endl);
301     // BBTKKWSIG
302     mgKWSignal();
303     // \BBTKKWSIG
304   }
305   //=========================================================================
306
307   //=========================================================================
308   void KW::DecNbWindowsShown()
309   { 
310     mgKWNbWindowsShown--; 
311     bbtkDebugMessage("kw",2,"* Number of KW windows shown = "
312                      <<mgKWNbWindowsShown
313                      <<std::endl);
314
315     //   DestroyTopWindowIfNeeded();
316
317     // BBTKKWSIG
318     mgKWSignal();
319     // \BBTKKWSIG
320   }
321   //=========================================================================
322
323
324   //=========================================================================
325   int  KW::GetNbWindowsAlive() 
326   { 
327     return mgKWNbWindowsAlive; 
328   }
329   //=========================================================================
330   
331   //=========================================================================
332   bool KW::IsSomeWindowAlive() 
333   { 
334     return (mgKWNbWindowsAlive>0);
335   }
336   //=========================================================================
337   
338   
339   //=========================================================================
340   int  KW::GetNbWindowsShown() 
341   { 
342     return mgKWNbWindowsShown; 
343   }
344   //=========================================================================
345   
346   //=========================================================================
347   bool KW::IsSomeWindowShown() 
348   { 
349     return (mgKWNbWindowsShown>0);
350   }
351   //=========================================================================
352   
353   /*
354   //=========================================================================
355   KW::BusyCursor::BusyCursor()
356   {
357     mCursor = 0;
358     if (TopWindowExists()) 
359       {         
360         bbtkDebugMessage("kw",2,
361                          "KW::BusyCursor::BusyCursor()"<<std::endl);
362            mCursor = new kwBusyCursor; 
363         //::kwBeginBusyCursor(); 
364     }
365   }
366   KW::BusyCursor::~BusyCursor()
367   {
368     if (mCursor) delete mCursor;
369   }
370   //=========================================================================
371   */
372   /*
373   //=========================================================================
374   void KW::ResetCursor() 
375   {
376     if (!TopWindowExists()) return;
377     bbtkDebugMessage("kw",9,"KW::ResetCursor()"<<std::endl);
378     while (kwIsBusy()) ::kwEndBusyCursor();
379   }
380   //=========================================================================
381   //=========================================================================
382   void KW::BeginBusyCursor() 
383   {
384     if (!TopWindowExists()) return;
385     bbtkDebugMessage("kw",9,"KW::BeginBusyCursor()"<<std::endl);
386     ::kwBeginBusyCursor();
387   }
388   //=========================================================================
389   //=========================================================================
390   void KW::EndBusyCursor()
391   {
392     if (!TopWindowExists()) return;
393     bbtkDebugMessage("kw",9,"KW::EndBusyCursor()"<<std::endl);
394     ::kwEndBusyCursor();
395   }
396   //=========================================================================
397   */
398
399 }
400
401 #endif