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

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