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

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