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