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

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