Logo ROOT   6.10/09
Reference Guide
TGToolBar.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 25/02/98
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 /**************************************************************************
12 
13  This source is based on Xclass95, a Win95-looking GUI toolkit.
14  Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15 
16  Xclass95 is free software; you can redistribute it and/or
17  modify it under the terms of the GNU Library General Public
18  License as published by the Free Software Foundation; either
19  version 2 of the License, or (at your option) any later version.
20 
21 **************************************************************************/
22 
23 //////////////////////////////////////////////////////////////////////////
24 // //
25 // TGToolBar //
26 // //
27 // A toolbar is a composite frame that contains TGPictureButtons. //
28 // Often used in combination with a TGHorizontal3DLine. //
29 // //
30 //////////////////////////////////////////////////////////////////////////
31 
32 #include "TGToolBar.h"
33 #include "TList.h"
34 #include "TGButton.h"
35 #include "TGPicture.h"
36 #include "TGToolTip.h"
37 #include "TSystem.h"
38 #include "TROOT.h"
39 #include "Riostream.h"
40 #include "TMap.h"
41 
42 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 
48  UInt_t options, ULong_t back) :
49  TGCompositeFrame(p, w, h, options, back)
50 
51 {
52  // Create toolbar widget.
53 
54  fPictures = new TList;
55  fTrash = new TList;
56  fMapOfButtons = new TMap(); // map of button/id pairs
57 
58  SetWindowName();
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Delete toolbar and its buttons and layout hints.
63 
65 {
66  if (!MustCleanup()) {
67  if (fTrash) fTrash->Clear("nodelete");
68  }
69  delete fTrash;
70  fTrash = 0;
71 
72  TIter next(fPictures);
73  const TGPicture *p;
74  while ((p = (const TGPicture *) next()))
75  fClient->FreePicture(p);
76 
77  // pictures might already have been deleted above, so avoid access
78  // to these objects
79  fPictures->Clear("nodelete");
80 
81  delete fPictures;
82  delete fMapOfButtons;
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Add button to toolbar. All buttons added via this method will be
87 /// deleted by the toolbar. On return the TGButton field of the
88 /// ToolBarData_t struct is filled in (if fPixmap was valid).
89 /// Window w is the window to which the button messages will be send.
90 
92 {
93  const TGPicture *pic = fClient->GetPicture(button->fPixmap);
94  if (!pic) {
95  Error("AddButton", "pixmap not found: %s", button->fPixmap);
96  return 0;
97  }
98  fPictures->Add((TObject*)pic);
99 
100  TGPictureButton *pbut;
101  TGLayoutHints *layout;
102 
103  pbut = new TGPictureButton(this, pic, button->fId);
104  pbut->SetStyle(gClient->GetStyle());
105  pbut->SetToolTipText(button->fTipText);
106 
107  layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
108  AddFrame(pbut, layout);
109  pbut->AllowStayDown(button->fStayDown);
110  pbut->Associate(w);
111  button->fButton = pbut;
112 
113  fTrash->Add(pbut);
114  fTrash->Add(layout);
115 
116  fMapOfButtons->Add(pbut, (TObject*)((Long_t)button->fId));
117 
118  Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
119  Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
120  Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
121 
122  return pbut;
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Add button to toolbar. All buttons added via this method will be deleted
127 /// by the toolbar, w is the window to which the button messages will be send.
128 
130 {
131  const TGPicture *pic = pbut->GetPicture();
132  fPictures->Add((TObject*)pic);
133 
134  TGLayoutHints *layout;
135  layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
136  pbut->SetStyle(gClient->GetStyle());
137  AddFrame(pbut, layout);
138  pbut->Associate(w);
139 
140  fTrash->Add(pbut);
141  fTrash->Add(layout);
142 
143  fMapOfButtons->Add(pbut, (TObject*)((Long_t)pbut->WidgetId()));
144 
145  Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
146  Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
147  Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
148 
149  return pbut;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Finds and returns a pointer to the button with the specified
154 /// identifier id. Returns null if the button was not found.
155 
157 {
158  TIter next(fMapOfButtons);
159  TGButton *item = 0;
160 
161  while ((item = (TGButton*)next())) {
162  if ((Long_t)fMapOfButtons->GetValue(item) == id) break; // found
163  }
164 
165  return item;
166 }
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// changes id for button.
170 
172 {
173  TPair *a = (TPair*) fMapOfButtons->FindObject(button);
174  if (a) {
175  a->SetValue((TObject*)id);
176  }
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Finds and returns the id of the button.
181 /// Returns -1 if the button is not a member of this group.
182 
184 {
185  TPair *a = (TPair*) fMapOfButtons->FindObject(button);
186  if (a)
187  return Long_t(a->Value());
188  else
189  return Long_t(-1);
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Change the icon of a toolbar button.
194 
195 void TGToolBar::ChangeIcon(ToolBarData_t *button, const char *new_icon)
196 {
197  const TGPicture *pic = fClient->GetPicture(new_icon);
198  if (!pic) {
199  Error("ChangeIcon", "pixmap not found: %s", new_icon);
200  return;
201  }
202  fPictures->Add((TObject*)pic);
203 
204  ((TGPictureButton *)button->fButton)->SetPicture(pic);
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Cleanup and delete all objects contained in this composite frame.
209 /// This will delete all objects added via AddFrame().
210 /// CAUTION: all objects (frames and layout hints) must be unique, i.e.
211 /// cannot be shared.
212 
214 {
215  // avoid double deletion of objects in trash
216  delete fTrash;
217  fTrash = 0;
218 
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// This slot is activated when one of the buttons in the group emits the
224 /// Pressed() signal.
225 
227 {
228  TGButton *btn = (TGButton*)gTQSender;
229 
230  TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
231  if (a) {
232  Int_t id = (Int_t)Long_t(a->Value());
233  Pressed(id);
234  }
235 }
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// This slot is activated when one of the buttons in the group emits the
239 /// Released() signal.
240 
242 {
243  TGButton *btn = (TGButton*)gTQSender;
244 
245  TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
246  if (a) {
247  Int_t id = (Int_t)Long_t(a->Value());
248  Released(id);
249  }
250 }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// This slot is activated when one of the buttons in the group emits the
254 /// Clicked() signal.
255 
257 {
258  TGButton *btn = (TGButton*)gTQSender;
259 
260  TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
261  if (a) {
262  Int_t id = (Int_t)Long_t(a->Value());
263  Clicked(id);
264  }
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Save an horizontal slider as a C++ statement(s) on output stream out.
269 
270 void TGToolBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
271 {
272  if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
273 
274  out << std::endl;
275  out << " // tool bar" << std::endl;
276 
277  out << " TGToolBar *";
278  out << GetName() << " = new TGToolBar(" << fParent->GetName()
279  << "," << GetWidth() << "," << GetHeight();
280 
282  if (!GetOptions()) {
283  out <<");" << std::endl;
284  } else {
285  out << "," << GetOptionString() <<");" << std::endl;
286  }
287  } else {
288  out << "," << GetOptionString() << ",ucolor);" << std::endl;
289  }
290  if (option && strstr(option, "keep_names"))
291  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
292 
293  char quote = '"';
294 
295  int i = 0;
296  const char *picname;
297 
298  TGFrameElement *f;
299  TIter next(GetList());
300 
301  while ((f = (TGFrameElement *) next())) {
303  if (!gROOT->ClassSaved(TGPictureButton::Class())) {
304  // declare a structure used for pictute buttons
305  out << std::endl << " ToolBarData_t t;" << std::endl;
306  }
307 
309  picname = pb->GetPicture()->GetName();
310 
311  out << " t.fPixmap = " << quote
313  << quote << ";" << std::endl;
314  out << " t.fTipText = " << quote
315  << pb->GetToolTip()->GetText()->GetString() << quote << ";" << std::endl;
316  if (pb->GetState() == kButtonDown) {
317  out << " t.fStayDown = kTRUE;" << std::endl;
318  } else {
319  out << " t.fStayDown = kFALSE;" << std::endl;
320  }
321  out << " t.fId = " << i+1 << ";" << std::endl;
322  out << " t.fButton = 0;" << std::endl;
323  out << " " << GetName() << "->AddButton(" << GetParent()->GetName()
324  << ",&t," << f->fLayout->GetPadLeft() << ");" << std::endl;
325  if (pb->GetState() == kButtonDown) {
326  out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
327  out << " " << pb->GetName() << "->SetState(kButtonDown);" << std::endl;
328  }
329  if (pb->GetState() == kButtonDisabled) {
330  out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
331  out << " " << pb->GetName() << "->SetState(kButtonDisabled);" << std::endl;
332  }
333  if (pb->GetState() == kButtonEngaged) {
334  out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
335  out << " " << pb->GetName() << "->SetState(kButtonEngaged);" << std::endl;
336  }
337  i++;
338  } else {
339  f->fFrame->SavePrimitive(out, option);
340  out << " " << GetName()<<"->AddFrame(" << f->fFrame->GetName();
341  f->fLayout->SavePrimitive(out, option);
342  out << ");"<< std::endl;
343  }
344  }
345 }
const TGWindow * fParent
Definition: TGWindow.h:37
virtual void Clicked(Int_t id)
Definition: TGToolBar.h:74
TGButton * fButton
Definition: TGToolBar.h:37
virtual TGButton * GetButton(Int_t id) const
Finds and returns a pointer to the button with the specified identifier id.
Definition: TGToolBar.cxx:156
virtual UInt_t GetOptions() const
Definition: TGFrame.h:244
TMap * fMapOfButtons
Definition: TGToolBar.h:47
const char * GetName() const
Returns name of object.
Definition: TGPicture.h:62
virtual void ChangeIcon(ToolBarData_t *button, const char *new_icon)
Change the icon of a toolbar button.
Definition: TGToolBar.cxx:195
const char Option_t
Definition: RtypesCore.h:62
virtual TGButton * AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing=0)
Add button to toolbar.
Definition: TGToolBar.cxx:91
R__EXTERN void * gTQSender
Definition: TQObject.h:45
UInt_t GetHeight() const
Definition: TGFrame.h:272
TH1 * h
Definition: legend2.C:5
Int_t WidgetId() const
Definition: TGWidget.h:80
const TGPicture * GetPicture() const
Definition: TGButton.h:256
Bool_t fStayDown
Definition: TGToolBar.h:35
void Add(TObject *obj)
This function may not be used (but we need to provide it since it is a pure virtual in TCollection)...
Definition: TMap.cxx:53
#define gROOT
Definition: TROOT.h:375
Pixel_t fBackground
Definition: TGFrame.h:142
#define gClient
Definition: TGClient.h:166
int Int_t
Definition: RtypesCore.h:41
TArc * a
Definition: textangle.C:12
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGToolBar.cxx:213
UInt_t GetWidth() const
Definition: TGFrame.h:271
virtual void Pressed(Int_t id)
Definition: TGToolBar.h:72
Handle_t GetId() const
Definition: TGObject.h:48
virtual void ButtonReleased()
This slot is activated when one of the buttons in the group emits the Released() signal.
Definition: TGToolBar.cxx:241
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1043
TList * fPictures
Definition: TGToolBar.h:45
TGLayoutHints * fLayout
Definition: TGLayout.h:121
TList * fTrash
Definition: TGToolBar.h:46
virtual void SetId(TGButton *button, Long_t id)
changes id for button.
Definition: TGToolBar.cxx:171
void Class()
Definition: Class.C:29
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:665
void FreePicture(const TGPicture *pic)
Free picture resource.
Definition: TGClient.cxx:306
virtual TList * GetList() const
Definition: TGFrame.h:369
virtual void ButtonClicked()
This slot is activated when one of the buttons in the group emits the Clicked() signal.
Definition: TGToolBar.cxx:256
virtual EButtonState GetState() const
Definition: TGButton.h:112
TObject * Value() const
Definition: TMap.h:121
A doubly linked list.
Definition: TList.h:43
virtual void SetStyle(UInt_t newstyle)
Set the button style (modern or classic).
Definition: TGButton.cxx:221
void SetValue(TObject *val)
Definition: TMap.h:122
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot...
Definition: TQObject.cxx:867
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a frame widget as a C++ statement(s) on output stream out.
Definition: TGFrame.cxx:3187
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
virtual TGToolTip * GetToolTip() const
Definition: TGButton.h:110
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:84
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TGLayout.cxx:1003
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:436
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:221
Int_t GetPadLeft() const
Definition: TGLayout.h:92
virtual void AllowStayDown(Bool_t a)
Definition: TGButton.h:113
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
TGFrame * fFrame
Definition: TGLayout.h:119
virtual ~TGToolBar()
Delete toolbar and its buttons and layout hints.
Definition: TGToolBar.cxx:64
const char * GetString() const
Definition: TGString.h:40
virtual void Released(Int_t id)
Definition: TGToolBar.h:73
long Long_t
Definition: RtypesCore.h:50
Class used by TMap to store (key,value) pairs.
Definition: TMap.h:102
#define ClassImp(name)
Definition: Rtypes.h:336
double f(double x)
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition: TGClient.cxx:287
TMap implements an associative array of (key,value) pairs using a THashTable for efficient retrieval ...
Definition: TMap.h:40
unsigned long ULong_t
Definition: RtypesCore.h:51
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
const char * fTipText
Definition: TGToolBar.h:34
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:353
const TGWindow * GetParent() const
Definition: TGWindow.h:81
Mother of all ROOT objects.
Definition: TObject.h:37
TObject * FindObject(const char *keyname) const
Check if a (key,value) pair exists with keyname as name of the key.
Definition: TMap.cxx:214
virtual void Add(TObject *obj)
Definition: TList.h:77
TGClient * fClient
Definition: TGObject.h:37
const char * fPixmap
Definition: TGToolBar.h:33
const TGString * GetText() const
Get the tool tip text.
Definition: TGToolTip.cxx:426
virtual void ButtonPressed()
This slot is activated when one of the buttons in the group emits the Pressed() signal.
Definition: TGToolBar.cxx:226
TObject * GetValue(const char *keyname) const
Returns a pointer to the value associated with keyname as name of the key.
Definition: TMap.cxx:235
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2433
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2460
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1250
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:949
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition: TGButton.cxx:395
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save an horizontal slider as a C++ statement(s) on output stream out.
Definition: TGToolBar.cxx:270
virtual Int_t MustCleanup() const
Definition: TGFrame.h:420