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

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