source: branches/AddingGoogleTestEnvironment/mbase/MGList.cc@ 20034

Last change on this file since 20034 was 8562, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 9.8 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 11/2001 <mailto:tbretz@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2001
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26// //
27// MGList //
28// //
29// This is a TList object which has some enhancements for GUI elements. //
30// Use GetWidget to search for a GUI element with a given widget id. //
31// Add checkes for the existances of a GUI element with the same widget //
32// id (for IDs>=0). //
33// //
34/////////////////////////////////////////////////////////////////////////////
35#include "MGList.h"
36
37#include <iostream>
38
39#include <TROOT.h>
40#include <TClass.h>
41#include <TGClient.h>
42#include <TGWidget.h>
43#include <TGPicture.h>
44
45ClassImp(MGList);
46
47using namespace std;
48
49// --------------------------------------------------------------------------
50//
51// Add the list to the global list of cleanups
52// objects in the list.
53//
54MGList::MGList() : TList()
55{
56 // Make sure that all object deleted are also deleted from this list
57 gROOT->GetListOfCleanups()->Add(this);
58 // Make sure that this doesn't remain in ListOfCleanups after deletion
59 SetBit(kMustCleanup);
60}
61
62// --------------------------------------------------------------------------
63//
64// Before destroying the list with all its contents free all TGPicture
65// objects in the list.
66//
67MGList::~MGList()
68{
69 DeletePictures();
70}
71
72// --------------------------------------------------------------------------
73//
74// Free/Delete all TGPicture store in list.
75//
76void MGList::DeletePictures()
77{
78 TObject *obj;
79 TIter Next(this);
80 while ((obj=Next()))
81 {
82 if (!obj->InheritsFrom(TGPicture::Class()))
83 continue;
84
85 //
86 // Remove the object first. Otherwise we would remove
87 // a non existing object...
88 //
89 Remove(obj);
90 if (gClient)
91 gClient->FreePicture((TGPicture*)obj);
92 }
93}
94
95// --------------------------------------------------------------------------
96//
97// Delete all pictures. Call TList::Delete.
98//
99void MGList::Delete(Option_t *option)
100{
101 DeletePictures();
102 TList::Delete(option);
103}
104
105// --------------------------------------------------------------------------
106//
107// Does a dynamic cast from a TObject to a TGWidget. This is necesary
108// if a class derived from TObject inherits also from TGWidget and
109// you have only a pointer to the TObject available.
110//
111TGWidget *MGList::GetWidget(TObject *obj) const
112{
113 //
114 // - IsA gives a pointer to the parent class entry in the dictionary
115 // - DynamicCast tries to cast obj of class type obj->IsA to one
116 // of its base classes TGWidget
117 // - This method is ment for dynamic casts (multi inheritance)
118 //
119 // If this functions makes trouble check for the correct inheritance
120 // first via obj->InheritsFrom(TGWidget::Class())
121 //
122 // The root implementation is used, because for space reasons
123 // the C++ dynamic_cast<TGWidget*> is turned off by the option
124 // -fno-rtti, which could be used in 'plain' C++
125 //
126
127 //
128 // FIXME: This should not be necessary but it is, why??
129 //
130 // TRY: TGPicture *pic = gClient->GetPicture("pic");
131 // cout << pic->IsA()->DynamicCast(TGWidget::Class(), pic) << endl;
132 //
133 // Is this another bug in root?
134 //
135#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,07)
136 if (!obj->InheritsFrom(TGWidget::Class()))
137 return NULL;
138#endif
139
140 return (TGWidget*)obj->IsA()->DynamicCast(TGWidget::Class(), obj);
141}
142
143// --------------------------------------------------------------------------
144//
145// Returns kTRUE if the object is derived from TGWidget and a widget
146// with the TGWidget id of this object is already in the list.
147// If the object is not derived from TGWidget or no TGWidget object
148// with the same widget id is existing in the list kFALSE is returned.
149// If the TGWidget has an object id < 0 kFALSE is always retuned.
150//
151Bool_t MGList::IsExisting(TObject *obj) const
152{
153 const TGWidget *wid = GetWidget(obj);
154
155 //
156 // check whether it is a TGWidget
157 //
158 if (!wid)
159 return kFALSE;
160
161 const Int_t id = wid->WidgetId();
162
163 //
164 // check whether is has a valid id
165 // (not id=-1, which is the standard id)
166 //
167 if (id < 0)
168 return kFALSE;
169
170 //
171 // check whether Widget id is already existing in the list
172 //
173 return FindWidget(id) ? kTRUE : kFALSE;
174}
175
176// --------------------------------------------------------------------------
177//
178// If the given object obj is derived from TGWidget and a TGWidget with
179// the same widget id is already existing in the list the object is
180// ignored, otherwise it is added to the list via TList::Add(TObject *)
181//
182void MGList::Add(TObject *obj)
183{
184 if (IsExisting(obj))
185 {
186 // FIXME: Replace by gLog
187 const Int_t id = GetWidget(obj)->WidgetId();
188 cout << "Widget with id #" << id << " (";
189 cout << FindWidget(id)->ClassName() << ") already in list... ";
190 cout << obj->GetName() << " ignored." << endl;
191 return;
192 }
193
194 TList::Add(obj);
195 obj->SetBit(kMustCleanup);
196}
197
198// --------------------------------------------------------------------------
199//
200// If the given object obj is derived from TGWidget and a TGWidget with
201// the same widget id is already existing in the list the object is
202// ignored, otherwise it is added to the list via
203// TList::Add(TObject *, Option_t *)
204//
205void MGList::Add(TObject *obj, Option_t *opt)
206{
207 if (IsExisting(obj))
208 {
209 Int_t id = GetWidget(obj)->WidgetId();
210 cout << "Widget with id #" << id << " (";
211 cout << FindWidget(id)->ClassName() << ") already in list... ";
212 cout << obj->GetName() << " ignored." << endl;
213 return;
214 }
215
216 TList::Add(obj, opt);
217 obj->SetBit(kMustCleanup);
218}
219
220// --------------------------------------------------------------------------
221//
222// Adds the picture physically to the list. The list takes care of that
223// - The picture is freed as often as it was retrieved from gClient
224//
225void MGList::AddPicture(TGPicture *pic, const char *name)
226{
227 //
228 // Check whether the picture exists
229 //
230 if (!pic)
231 {
232 cout << "Warning: Requested picture '" << name << "' not found... ignored." << endl;
233 cout << " Please copy " << name << " to $HOME or $HOME/icons or add" << endl;
234 cout << " Unix.*.Gui.IconPath: ~/Path/To/The/Picture" << endl;
235 cout << " to [$HOME/].rootrc." << endl;
236 return;
237 }
238
239 //
240 // Add the picture to the list
241 //
242 TList::Add(pic);
243 pic->SetBit(kMustCleanup);
244}
245
246// --------------------------------------------------------------------------
247//
248// This gets a picture from the picture pool of the TGClient-object.
249// The pictures are freed automatically in the dstructor of the list.
250// The picture counts itself how often it got used, so that only
251// the first call to GetPicture will craete it and the last call to
252// FreePicture will destroy it. If you access the picture only via
253// MGList::GetPicture you don't have to care about.
254//
255// Don't try to call FreePicture by yourself for a picture gotten by
256// GetPicture. This is independant of the kIsOwner bit.
257//
258const TGPicture *MGList::GetPicture(const char *name)
259{
260 if (!gClient)
261 return NULL;
262
263 TGPicture *pic = const_cast<TGPicture*>(gClient->GetPicture(name));
264 AddPicture(pic, name);
265 return pic;
266}
267
268// --------------------------------------------------------------------------
269//
270// This gets a picture from the picture pool of the TGClient-object.
271// The pictures are freed automatically in the dstructor of the list.
272// The picture counts itself how often it got used, so that only
273// the first call to GetPicture will craete it and the last call to
274// FreePicture will destroy it. If you access the picture only via
275// MGList::GetPicture you don't have to care about.
276//
277// Don't try to call FreePicture by yourself for a picture gotten by
278// GetPicture. This is independant of the kIsOwner bit.
279//
280const TGPicture *MGList::GetPicture(const char *name, Int_t width, Int_t height)
281{
282 if (!gClient)
283 return NULL;
284
285 TGPicture *pic = const_cast<TGPicture*>(gClient->GetPicture(name, width, height));
286 AddPicture(pic, name);
287 return pic;
288}
289// --------------------------------------------------------------------------
290//
291// Search the list for a object derived from TGidget which has the given
292// widget id. Returns a pointer to this object otherwise NULL.
293// For IDs < 0 the function returns always NULL.
294//
295TObject *MGList::FindWidget(Int_t id) const
296{
297 if (id<0)
298 return NULL;
299
300 TObject *obj;
301 TIter Next(this);
302 while ((obj=Next()))
303 {
304 const TGWidget *wid = GetWidget(obj);
305
306 if (!wid)
307 continue;
308
309 if (id == wid->WidgetId())
310 return obj;
311 }
312 return NULL;
313}
Note: See TracBrowser for help on using the repository browser.