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