Fix coverity defect
[u/mrichter/AliRoot.git] / MUON / AliMUONPainterInterfaceHelper.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 #include "AliMUONPainterInterfaceHelper.h"
19
20 ///\class AliMUONPainterInterfaceHelper
21 ///
22 /// Helper class to work with TGButtonGroup
23 ///
24 /// This class only works if the buttons in the TGButtonGroup have contiguous
25 /// ids, and if those ids start at ButtonStartingId().
26 /// Not bullet-proof, I admit, but this is the only way I've found with
27 /// the current TGButtonGroup implementation which does not allow to loop
28 /// easily on all buttons.
29 ///
30 // Author Laurent Aphecetche, Subatech
31
32 #include "AliMUONPainterEnv.h"
33 #include "AliMUONPainterHelper.h"
34 #include "AliLog.h"
35 #include <Riostream.h>
36 #include <TClass.h>
37 #include <TGButtonGroup.h>
38 #include <TGButton.h>
39 #include <TGClient.h>
40 #include <TObjArray.h>
41 #include <TObjString.h>
42 #include <TString.h>
43 #include <RVersion.h>
44 #include <cassert>
45
46 ///\cond CLASSIMP
47 ClassImp(AliMUONPainterInterfaceHelper)
48 ///\endcond
49
50 //_____________________________________________________________________________
51 AliMUONPainterInterfaceHelper::AliMUONPainterInterfaceHelper() : TObject()
52 {
53   /// ctor
54 }
55
56 //_____________________________________________________________________________
57 AliMUONPainterInterfaceHelper::~AliMUONPainterInterfaceHelper()
58 {
59   /// dtor
60 }
61
62 //_____________________________________________________________________________
63 void
64 AliMUONPainterInterfaceHelper::AddRadioButton(TGButtonGroup& bg,
65                                               const TString& name,
66                                               void* userData,
67                                               Bool_t select)
68 {
69   /// Add a radio button to a group
70   Int_t n = bg.GetCount();
71   
72   TGButton* button = new TGRadioButton(&bg,
73                                        name.Data(),
74                                        n+ButtonStartingId());
75   button->SetUserData(userData);
76   button->SetOn(select,kFALSE);  
77 }
78
79 //_____________________________________________________________________________
80 void
81 AliMUONPainterInterfaceHelper::AddCheckButton(TGButtonGroup& bg,
82                                               const TString& name,
83                                               void* userData,
84                                               Bool_t select)
85 {
86   /// Add a check button to a group
87   Int_t n = bg.GetCount();
88   
89   TGButton* button = new TGCheckButton(&bg,
90                                        name.Data(),
91                                        n+ButtonStartingId());
92   button->SetUserData(userData);
93   button->SetOn(select,kFALSE);
94 }
95
96 //_____________________________________________________________________________
97 void 
98 AliMUONPainterInterfaceHelper::ClearButtons(TGButtonGroup& bg)
99 {
100   /// Remove all buttons from group.
101   /// Bear in mind that you're thus disconnecting the group from
102   /// any signals it might have. So you must re-connect them after
103   /// a call to this method, if you want to.
104   
105   while ( bg.GetCount() > 0 )
106   {
107     TGTextButton* button = (TGTextButton*)(bg.GetButton(bg.GetCount()));
108     if ( !button ) 
109     {
110       AliFatalClass(Form("Got a null button for bg.Count()=%d",bg.GetCount()));
111     }
112     bg.Remove(button);
113 #if ROOT_VERSION_CODE <= ROOT_VERSION(5,16,0)
114     button->DestroyWindow();
115 #endif
116     delete button;
117   }
118 }
119
120 //_____________________________________________________________________________
121 void
122 AliMUONPainterInterfaceHelper::Copy(const TGButtonGroup& src, TGButtonGroup& dest)
123 {
124   /// Copy a button group into another one
125   AliDebugClass(1,Form("src=%p (%s) count=%d dest=%p (%s) count=%d",
126                        &src,src.GetTitle(),src.GetCount(),
127                        &dest,dest.GetTitle(),dest.GetCount()));
128
129   StdoutToAliDebugClass(1,cout << "---copying:" << endl; Dump(src);
130                         cout << "---to:" << endl; Dump(dest));
131   
132   ClearButtons(dest);
133   
134   dest.SetTitle(src.GetTitle());
135   
136   if ( &src != &dest )
137   {
138     for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + src.GetCount(); ++i )
139     {
140       TGTextButton* tb = static_cast<TGTextButton*>(src.GetButton(i));
141       TGButton* button = new TGRadioButton(&dest,tb->GetTitle(),tb->WidgetId());
142       assert(tb->WidgetId()==i);
143       button->SetUserData(tb->GetUserData());
144       button->SetOn(tb->IsOn(),kFALSE);
145     }
146   }
147 }
148
149 //_____________________________________________________________________________
150 void 
151 AliMUONPainterInterfaceHelper::Dump(const TGButtonGroup& bg)
152 {
153   /// Printout
154   cout << Form("ButtonGroup %s %s",bg.GetName(),bg.GetTitle()) << endl;
155   
156   for ( Int_t i = ButtonStartingId(); i < bg.GetCount() + ButtonStartingId(); ++i ) 
157   {
158     TGTextButton* button = static_cast<TGTextButton*>(bg.GetButton(i));
159     if ( button ) 
160     {
161       cout << Form("i %2d button %s id %lu wid %d ON %d",
162                  i,button->GetTitle(),button->GetId(),
163                  button->WidgetId(),
164                  button->IsOn()) << endl;
165     }
166     else
167     {
168       cout << Form("i %2d button = 0x0",i) << endl;
169     }
170   }
171 }
172
173 //_____________________________________________________________________________
174 TGButton* 
175 AliMUONPainterInterfaceHelper::FindButtonByName(const TGButtonGroup& bg, 
176                                                 const TString& name)
177 {
178   /// Find a button by name
179   
180   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i )
181   {
182     TGTextButton* button = static_cast<TGTextButton*>(bg.GetButton(i));
183     if (!button)
184     {
185       AliErrorClass(Form("(%s) : Something wrong",name.Data()));
186       Dump(bg);
187     }
188     else
189     {
190       if ( name == button->GetTitle() )
191       {
192         return button;
193       }
194     }
195   }
196   return 0x0;
197 }
198
199 //_____________________________________________________________________________
200 TGButton* 
201 AliMUONPainterInterfaceHelper::FindButtonByUserData(const TGButtonGroup& bg, 
202                                                     const void* userData)
203 {
204   /// Find a button by userData
205   
206   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i )
207   {
208     TGButton* button = bg.GetButton(i);
209     if ( button->GetUserData() == userData )
210     {
211       return button;
212     }
213   }
214   return 0x0;
215 }
216
217 //_____________________________________________________________________________
218 TGButton* 
219 AliMUONPainterInterfaceHelper::FindDownButton(const TGButtonGroup& bg)
220 {
221   /// Find which button is down (in a radio group)
222   
223   AliDebugClass(1,Form("bg %s",bg.GetTitle()));
224   
225   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i )
226   {
227     TGButton* button = bg.GetButton(i);
228     if ( button->IsOn() ) 
229     {
230       AliDebugClass(1,Form("button %s",button->GetTitle()));
231       return button;
232     }
233   }
234   return 0x0;
235 }
236
237
238 //_____________________________________________________________________________
239 void 
240 AliMUONPainterInterfaceHelper::SetBackgroundColor(const char* resourceBaseName, 
241                                                   TGWindow& window)
242 {
243   /// Set the background color of the window
244   TString rs(Form("%s.BackgroundColor",resourceBaseName));
245   TString colorName = AliMUONPainterHelper::Instance()->Env()->String(rs.Data(),"#c0c0c0");
246
247   Pixel_t color;
248   Bool_t ok = gClient->GetColorByName(colorName, color);
249   if ( ok ) 
250   {
251     window.SetBackgroundColor(color);
252     AliDebugClass(1,Form("Setting %s color to %s",rs.Data(),colorName.Data()));
253   }
254 }
255
256 //_____________________________________________________________________________
257 void 
258 AliMUONPainterInterfaceHelper::SetState(TGButtonGroup& bg, Bool_t state)
259 {
260   /// should not be needed with root > 5.16/00 as there's a TGButtonGroup::SetState
261   /// method now...
262
263 #if ROOT_VERSION_CODE > ROOT_VERSION(5,16,0)
264   bg.SetState(state);
265 #else
266   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
267   {
268     TGTextButton* button = (TGTextButton*)(bg.GetButton(i));
269     if ( state ) 
270     {
271       button->SetState(kButtonUp);
272     }
273     else
274     {
275       button->SetState(kButtonDisabled);
276     }
277   }
278 #endif  
279 }
280
281 //_____________________________________________________________________________
282 void 
283 AliMUONPainterInterfaceHelper::Select(TGButtonGroup& bg, 
284                                       const TString& buttonName,
285                                       Bool_t emit)
286 {
287   /// Select which button should be on
288   
289   AliDebugClass(1,Form("bg %s buttonName %s",bg.GetTitle(),buttonName.Data()));
290   
291   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
292   {
293     TGTextButton* button = (TGTextButton*)(bg.GetButton(i));
294     if ( buttonName == button->GetTitle() || buttonName == "*" ) 
295     {
296       if ( emit ) 
297       {
298         button->Clicked();
299       }
300       else
301       {        
302         button->SetOn(kTRUE);
303       }
304     }
305   }  
306 }
307
308 //_____________________________________________________________________________
309 void 
310 AliMUONPainterInterfaceHelper::Unselect(TGButtonGroup& bg, 
311                                         const TString& buttonName,
312                                         Bool_t emit)
313 {
314   /// Unselect a given button
315   
316   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
317   {
318     TGTextButton* button = (TGTextButton*)(bg.GetButton(i));
319     if ( buttonName == button->GetTitle() || buttonName == "*" ) 
320     {
321       if ( emit ) 
322       {
323         button->Released();
324       }
325       else
326       {        
327         button->SetOn(kFALSE);
328       }
329     }
330   }  
331 }
332
333 //_____________________________________________________________________________
334 void
335 AliMUONPainterInterfaceHelper::RemoveButton(TGButtonGroup& bg,
336                                             const TGButton* button)
337 {
338   /// Remove a button
339   
340   // need to redo it from scratch in order not the leave holes in the
341   // id numbering
342   
343   TGButtonGroup bgtmp(bg.GetParent(),bg.GetTitle());
344   
345   Int_t id(ButtonStartingId());
346   
347   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
348   {
349     TGTextButton* b = static_cast<TGTextButton*>(bg.GetButton(i));
350
351     if ( b != button ) 
352     {
353       TGButton* bc = new TGRadioButton(&bgtmp,b->GetTitle(),id);
354       ++id;
355       bc->SetUserData(b->GetUserData());
356       bc->SetOn(b->IsOn());
357     }    
358   }    
359
360   ClearButtons(bg);
361
362   Copy(bgtmp,bg);
363   
364   bg.Show();
365 }