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

Last change on this file since 1119 was 1116, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 8.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.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 (!obj->IsA()->InheritsFrom(TGWidget::Class()))
101 return NULL;
102
103 return (TGWidget*)obj->IsA()->DynamicCast(TGWidget::Class(), obj);
104}
105
106// --------------------------------------------------------------------------
107//
108// Returns kTRUE if the object is derived from TGWidget and a widget
109// with the TGWidget id of this object is already in the list.
110// If the object is not derived from TGWidget or no TGWidget object
111// with the same widget id is existing in the list kFALSE is returned.
112// If the TGWidget has an object id < 0 kFALSE is always retuned.
113//
114Bool_t MGList::IsExisting(TObject *obj) const
115{
116 const TGWidget *wid = GetWidget(obj);
117
118 //
119 // check whether it is a TGWidget
120 //
121 if (!wid)
122 return kFALSE;
123
124 const Int_t id = wid->WidgetId();
125
126 //
127 // check whether is has a valid id
128 // (not id=-1, which is the standard id)
129 //
130 if (id < 0)
131 return kFALSE;
132
133 //
134 // check whether Widget id is already existing in the list
135 //
136 return FindWidget(id) ? kTRUE : kFALSE;
137}
138
139// --------------------------------------------------------------------------
140//
141// If the given object obj is derived from TGWidget and a TGWidget with
142// the same widget id is already existing in the list the object is
143// ignored, otherwise it is added to the list via TList::Add(TObject *)
144//
145void MGList::Add(TObject *obj)
146{
147 if (IsExisting(obj))
148 {
149 const Int_t id = GetWidget(obj)->WidgetId();
150 cout << "Widget with id #" << id << " (";
151 cout << FindWidget(id)->ClassName() << ") already in list... ";
152 cout << obj->GetName() << " ignored." << endl;
153 return;
154 }
155
156 TList::Add(obj);
157}
158
159// --------------------------------------------------------------------------
160//
161// If the given object obj is derived from TGWidget and a TGWidget with
162// the same widget id is already existing in the list the object is
163// ignored, otherwise it is added to the list via
164// TList::Add(TObject *, Option_t *)
165//
166void MGList::Add(TObject *obj, Option_t *opt)
167{
168 if (IsExisting(obj))
169 {
170 Int_t id = GetWidget(obj)->WidgetId();
171 cout << "Widget with id #" << id << " (";
172 cout << FindWidget(id)->ClassName() << ") already in list... ";
173 cout << obj->GetName() << " ignored." << endl;
174 return;
175 }
176
177 TList::Add(obj, opt);
178}
179
180// --------------------------------------------------------------------------
181//
182// Adds the picture physically to the list. The list takes care of that
183// - The picture is freed as often as it was retrieved from gClient
184//
185void MGList::AddPicture(const TGPicture *pic, const char *name)
186{
187 //
188 // Check whether the picture exists
189 //
190 if (!pic)
191 {
192 cout << "Warning: Requested picture '" << name << "' not found... ignored." << endl;
193 cout << " Please copy " << name << " to $HOME or $HOME/icons or add" << endl;
194 cout << " Unix.*.Gui.IconPath: ~/Path/To/The/Picture" << endl;
195 cout << " to [$HOME/].rootrc." << endl;
196 return;
197 }
198
199 //
200 // Add the picture to the list
201 //
202 TList::Add(const_cast<TGPicture*>(pic));
203}
204
205// --------------------------------------------------------------------------
206//
207// This gets a picture from the picture pool of the TGClient-object.
208// The pictures are freed automatically in the dstructor of the list.
209// The picture counts itself how often it got used, so that only
210// the first call to GetPicture will craete it and the last call to
211// FreePicture will destroy it. If you access the picture only via
212// MGList::GetPicture you don't have to care about.
213//
214// Don't try to call FreePicture by yourself for a picture gotten by
215// GetPicture. This is independant of the kIsOwner bit.
216//
217const TGPicture *MGList::GetPicture(const char *name)
218{
219 const TGPicture *pic = gClient->GetPicture(name);
220 AddPicture(pic, name);
221 return pic;
222}
223
224// --------------------------------------------------------------------------
225//
226// This gets a picture from the picture pool of the TGClient-object.
227// The pictures are freed automatically in the dstructor of the list.
228// The picture counts itself how often it got used, so that only
229// the first call to GetPicture will craete it and the last call to
230// FreePicture will destroy it. If you access the picture only via
231// MGList::GetPicture you don't have to care about.
232//
233// Don't try to call FreePicture by yourself for a picture gotten by
234// GetPicture. This is independant of the kIsOwner bit.
235//
236const TGPicture *MGList::GetPicture(const char *name, Int_t width, Int_t height)
237{
238 const TGPicture *pic = gClient->GetPicture(name, width, height);
239 AddPicture(pic, name);
240 return pic;
241}
242// --------------------------------------------------------------------------
243//
244// Search the list for a object derived from TGidget which has the given
245// widget id. Returns a pointer to this object otherwise NULL.
246// For IDs < 0 the function returns always NULL.
247//
248TObject *MGList::FindWidget(Int_t id) const
249{
250 if (id<0)
251 return NULL;
252
253 TObject *obj;
254 TIter Next(this);
255 while ((obj=Next()))
256 {
257 const TGWidget *wid = GetWidget(obj);
258
259 if (!wid)
260 continue;
261
262 if (id == wid->WidgetId())
263 return obj;
264 }
265 return NULL;
266}
Note: See TracBrowser for help on using the repository browser.