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