source: trunk/MagicSoft/Mars/mbase/MGList.cc@ 5138

Last change on this file since 5138 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 9.2 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 gROOT->GetListOfCleanups()->Add(this);
57}
58
59// --------------------------------------------------------------------------
60//
61// Before destroying the list with all its contents free all TGPicture
62// objects in the list.
63//
64MGList::~MGList()
65{
66 TObject *obj;
67 TIter Next(this);
68 while ((obj=Next()))
69 {
70 if (!obj->InheritsFrom(TGPicture::Class()))
71 continue;
72
73 //
74 // Remove the object first. Otherwise we would remove
75 // a non existing object...
76 //
77 Remove(obj);
78 gClient->FreePicture((TGPicture*)obj);
79 }
80}
81
82// --------------------------------------------------------------------------
83//
84// Does a dynamic cast from a TObject to a TGWidget. This is necesary
85// if a class derived from TObject inherits also from TGWidget and
86// you have only a pointer to the TObject available.
87//
88TGWidget *MGList::GetWidget(TObject *obj) const
89{
90 //
91 // - IsA gives a pointer to the parent class entry in the dictionary
92 // - DynamicCast tries to cast obj of class type obj->IsA to one
93 // of its base classes TGWidget
94 // - This method is ment for dynamic casts (multi inheritance)
95 //
96 // If this functions makes trouble check for the correct inheritance
97 // first via obj->InheritsFrom(TGWidget::Class())
98 //
99 // The root implementation is used, because for space reasons
100 // the C++ dynamic_cast<TGWidget*> is turned off by the option
101 // -fno-rtti, which could be used in 'plain' C++
102 //
103
104 //
105 // FIXME: This should not be necessary but it is, why??
106 //
107 // TRY: TGPicture *pic = gClient->GetPicture("pic");
108 // cout << pic->IsA()->DynamicCast(TGWidget::Class(), pic) << endl;
109 //
110 // Is this another bug in root?
111 //
112#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,07)
113 if (!obj->InheritsFrom(TGWidget::Class()))
114 return NULL;
115#endif
116
117 return (TGWidget*)obj->IsA()->DynamicCast(TGWidget::Class(), obj);
118}
119
120// --------------------------------------------------------------------------
121//
122// Returns kTRUE if the object is derived from TGWidget and a widget
123// with the TGWidget id of this object is already in the list.
124// If the object is not derived from TGWidget or no TGWidget object
125// with the same widget id is existing in the list kFALSE is returned.
126// If the TGWidget has an object id < 0 kFALSE is always retuned.
127//
128Bool_t MGList::IsExisting(TObject *obj) const
129{
130 const TGWidget *wid = GetWidget(obj);
131
132 //
133 // check whether it is a TGWidget
134 //
135 if (!wid)
136 return kFALSE;
137
138 const Int_t id = wid->WidgetId();
139
140 //
141 // check whether is has a valid id
142 // (not id=-1, which is the standard id)
143 //
144 if (id < 0)
145 return kFALSE;
146
147 //
148 // check whether Widget id is already existing in the list
149 //
150 return FindWidget(id) ? kTRUE : kFALSE;
151}
152
153// --------------------------------------------------------------------------
154//
155// If the given object obj is derived from TGWidget and a TGWidget with
156// the same widget id is already existing in the list the object is
157// ignored, otherwise it is added to the list via TList::Add(TObject *)
158//
159void MGList::Add(TObject *obj)
160{
161 if (IsExisting(obj))
162 {
163 // FIXME: Replace by gLog
164 const Int_t id = GetWidget(obj)->WidgetId();
165 cout << "Widget with id #" << id << " (";
166 cout << FindWidget(id)->ClassName() << ") already in list... ";
167 cout << obj->GetName() << " ignored." << endl;
168 return;
169 }
170
171 TList::Add(obj);
172 obj->SetBit(kMustCleanup);
173}
174
175// --------------------------------------------------------------------------
176//
177// If the given object obj is derived from TGWidget and a TGWidget with
178// the same widget id is already existing in the list the object is
179// ignored, otherwise it is added to the list via
180// TList::Add(TObject *, Option_t *)
181//
182void MGList::Add(TObject *obj, Option_t *opt)
183{
184 if (IsExisting(obj))
185 {
186 Int_t id = GetWidget(obj)->WidgetId();
187 cout << "Widget with id #" << id << " (";
188 cout << FindWidget(id)->ClassName() << ") already in list... ";
189 cout << obj->GetName() << " ignored." << endl;
190 return;
191 }
192
193 TList::Add(obj, opt);
194 obj->SetBit(kMustCleanup);
195}
196
197// --------------------------------------------------------------------------
198//
199// Adds the picture physically to the list. The list takes care of that
200// - The picture is freed as often as it was retrieved from gClient
201//
202void MGList::AddPicture(TGPicture *pic, const char *name)
203{
204 //
205 // Check whether the picture exists
206 //
207 if (!pic)
208 {
209 cout << "Warning: Requested picture '" << name << "' not found... ignored." << endl;
210 cout << " Please copy " << name << " to $HOME or $HOME/icons or add" << endl;
211 cout << " Unix.*.Gui.IconPath: ~/Path/To/The/Picture" << endl;
212 cout << " to [$HOME/].rootrc." << endl;
213 return;
214 }
215
216 //
217 // Add the picture to the list
218 //
219 TList::Add(pic);
220 pic->SetBit(kMustCleanup);
221}
222
223// --------------------------------------------------------------------------
224//
225// This gets a picture from the picture pool of the TGClient-object.
226// The pictures are freed automatically in the dstructor of the list.
227// The picture counts itself how often it got used, so that only
228// the first call to GetPicture will craete it and the last call to
229// FreePicture will destroy it. If you access the picture only via
230// MGList::GetPicture you don't have to care about.
231//
232// Don't try to call FreePicture by yourself for a picture gotten by
233// GetPicture. This is independant of the kIsOwner bit.
234//
235const TGPicture *MGList::GetPicture(const char *name)
236{
237 TGPicture *pic = const_cast<TGPicture*>(gClient->GetPicture(name));
238 AddPicture(pic, name);
239 return pic;
240}
241
242// --------------------------------------------------------------------------
243//
244// This gets a picture from the picture pool of the TGClient-object.
245// The pictures are freed automatically in the dstructor of the list.
246// The picture counts itself how often it got used, so that only
247// the first call to GetPicture will craete it and the last call to
248// FreePicture will destroy it. If you access the picture only via
249// MGList::GetPicture you don't have to care about.
250//
251// Don't try to call FreePicture by yourself for a picture gotten by
252// GetPicture. This is independant of the kIsOwner bit.
253//
254const TGPicture *MGList::GetPicture(const char *name, Int_t width, Int_t height)
255{
256 TGPicture *pic = const_cast<TGPicture*>(gClient->GetPicture(name, width, height));
257 AddPicture(pic, name);
258 return pic;
259}
260// --------------------------------------------------------------------------
261//
262// Search the list for a object derived from TGidget which has the given
263// widget id. Returns a pointer to this object otherwise NULL.
264// For IDs < 0 the function returns always NULL.
265//
266TObject *MGList::FindWidget(Int_t id) const
267{
268 if (id<0)
269 return NULL;
270
271 TObject *obj;
272 TIter Next(this);
273 while ((obj=Next()))
274 {
275 const TGWidget *wid = GetWidget(obj);
276
277 if (!wid)
278 continue;
279
280 if (id == wid->WidgetId())
281 return obj;
282 }
283 return NULL;
284}
Note: See TracBrowser for help on using the repository browser.