Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/** \class TGToolBar
25\ingroup guiwidgets
26
27A toolbar is a composite frame that contains TGPictureButtons.
28Often used in combination with a TGHorizontal3DLine.
29
30*/
31
32
33#include "TGToolBar.h"
34#include "TList.h"
35#include "TGButton.h"
36#include "TGPicture.h"
37#include "TGToolTip.h"
38#include "TSystem.h"
39#include "TROOT.h"
40#include <iostream>
41#include "TMap.h"
42
43
45
46////////////////////////////////////////////////////////////////////////////////
47
49 UInt_t options, Pixel_t back) :
50 TGCompositeFrame(p, w, h, options, back)
51
52{
53 // Create toolbar widget.
54
55 fPictures = new TList;
56 fTrash = new TList;
57 fMapOfButtons = new TMap(); // map of button/id pairs
58
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Delete toolbar and its buttons and layout hints.
64
66{
67 if (!MustCleanup()) {
68 if (fTrash) fTrash->Clear("nodelete");
69 }
70 delete fTrash;
71 fTrash = 0;
72
73 TIter next(fPictures);
74 const TGPicture *p;
75 while ((p = (const TGPicture *) next()))
77
78 // pictures might already have been deleted above, so avoid access
79 // to these objects
80 fPictures->Clear("nodelete");
81
82 delete fPictures;
83 delete fMapOfButtons;
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Add button to toolbar. All buttons added via this method will be
88/// deleted by the toolbar. On return the TGButton field of the
89/// ToolBarData_t struct is filled in (if fPixmap was valid).
90/// Window w is the window to which the button messages will be send.
91
93{
94 const TGPicture *pic = fClient->GetPicture(button->fPixmap);
95 if (!pic) {
96 Error("AddButton", "pixmap not found: %s", button->fPixmap);
97 return 0;
98 }
99 fPictures->Add((TObject*)pic);
100
101 TGPictureButton *pbut;
102 TGLayoutHints *layout;
103
104 pbut = new TGPictureButton(this, pic, button->fId);
105 pbut->SetStyle(gClient->GetStyle());
106 pbut->SetToolTipText(button->fTipText);
107
108 layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
109 AddFrame(pbut, layout);
110 pbut->AllowStayDown(button->fStayDown);
111 pbut->Associate(w);
112 button->fButton = pbut;
113
114 fTrash->Add(pbut);
115 fTrash->Add(layout);
116
117 fMapOfButtons->Add(pbut, (TObject*)((Longptr_t)button->fId));
118
119 Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
120 Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
121 Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
122
123 return pbut;
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Add button to toolbar. All buttons added via this method will be deleted
128/// by the toolbar, w is the window to which the button messages will be send.
129
131{
132 const TGPicture *pic = pbut->GetPicture();
133 fPictures->Add((TObject*)pic);
134
135 TGLayoutHints *layout;
136 layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
137 pbut->SetStyle(gClient->GetStyle());
138 AddFrame(pbut, layout);
139 pbut->Associate(w);
140
141 fTrash->Add(pbut);
142 fTrash->Add(layout);
143
144 fMapOfButtons->Add(pbut, (TObject*)((Longptr_t)pbut->WidgetId()));
145
146 Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
147 Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
148 Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
149
150 return pbut;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Finds and returns a pointer to the button with the specified
155/// identifier id. Returns null if the button was not found.
156
158{
159 TIter next(fMapOfButtons);
160 TGButton *item = 0;
161
162 while ((item = (TGButton*)next())) {
163 if ((Longptr_t)fMapOfButtons->GetValue(item) == id) break; // found
164 }
165
166 return item;
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// changes id for button.
171
173{
175 if (a) {
176 a->SetValue((TObject*)id);
177 }
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Finds and returns the id of the button.
182/// Returns -1 if the button is not a member of this group.
183
185{
187 if (a)
188 return (Longptr_t)a->Value();
189 else
190 return (Longptr_t)-1;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Change the icon of a toolbar button.
195
196void TGToolBar::ChangeIcon(ToolBarData_t *button, const char *new_icon)
197{
198 const TGPicture *pic = fClient->GetPicture(new_icon);
199 if (!pic) {
200 Error("ChangeIcon", "pixmap not found: %s", new_icon);
201 return;
202 }
203 fPictures->Add((TObject*)pic);
204
205 ((TGPictureButton *)button->fButton)->SetPicture(pic);
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Cleanup and delete all objects contained in this composite frame.
210/// This will delete all objects added via AddFrame().
211/// CAUTION: all objects (frames and layout hints) must be unique, i.e.
212/// cannot be shared.
213
215{
216 // avoid double deletion of objects in trash
217 delete fTrash;
218 fTrash = 0;
219
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// This slot is activated when one of the buttons in the group emits the
225/// Pressed() signal.
226
228{
229 TGButton *btn = (TGButton*)gTQSender;
230
232 if (a) {
233 Int_t id = (Int_t)Longptr_t(a->Value());
234 Pressed(id);
235 }
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// This slot is activated when one of the buttons in the group emits the
240/// Released() signal.
241
243{
244 TGButton *btn = (TGButton*)gTQSender;
245
247 if (a) {
248 Int_t id = (Int_t)Longptr_t(a->Value());
249 Released(id);
250 }
251}
252
253////////////////////////////////////////////////////////////////////////////////
254/// This slot is activated when one of the buttons in the group emits the
255/// Clicked() signal.
256
258{
259 TGButton *btn = (TGButton*)gTQSender;
260
262 if (a) {
263 Int_t id = (Int_t)Longptr_t(a->Value());
264 Clicked(id);
265 }
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Save an horizontal slider as a C++ statement(s) on output stream out.
270
271void TGToolBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
272{
274
275 out << std::endl;
276 out << " // tool bar" << std::endl;
277
278 out << " TGToolBar *";
279 out << GetName() << " = new TGToolBar(" << fParent->GetName()
280 << "," << GetWidth() << "," << GetHeight();
281
283 if (!GetOptions()) {
284 out <<");" << std::endl;
285 } else {
286 out << "," << GetOptionString() <<");" << std::endl;
287 }
288 } else {
289 out << "," << GetOptionString() << ",ucolor);" << std::endl;
290 }
291 if (option && strstr(option, "keep_names"))
292 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
293
294 char quote = '"';
295
296 int i = 0;
297
299 TIter next(GetList());
300
301 while ((f = (TGFrameElement *) next())) {
302 if (f->fFrame->InheritsFrom(TGPictureButton::Class())) {
303 if (!gROOT->ClassSaved(TGPictureButton::Class())) {
304 // declare a structure used for picture buttons
305 out << std::endl << " ToolBarData_t t;" << std::endl;
306 }
307
308 TGPictureButton *pb = (TGPictureButton *)f->fFrame;
309 TString picname = gSystem->UnixPathName(pb->GetPicture()->GetName());
310 gSystem->ExpandPathName(picname);
311
312 out << " t.fPixmap = " << quote << picname << quote << ";" << std::endl;
313 out << " t.fTipText = " << quote
314 << pb->GetToolTip()->GetText()->GetString() << quote << ";" << std::endl;
315 if (pb->GetState() == kButtonDown) {
316 out << " t.fStayDown = kTRUE;" << std::endl;
317 } else {
318 out << " t.fStayDown = kFALSE;" << std::endl;
319 }
320 out << " t.fId = " << i+1 << ";" << std::endl;
321 out << " t.fButton = 0;" << std::endl;
322 out << " " << GetName() << "->AddButton(" << GetParent()->GetName()
323 << ",&t," << f->fLayout->GetPadLeft() << ");" << std::endl;
324 if (pb->GetState() == kButtonDown) {
325 out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
326 out << " " << pb->GetName() << "->SetState(kButtonDown);" << std::endl;
327 }
328 if (pb->GetState() == kButtonDisabled) {
329 out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
330 out << " " << pb->GetName() << "->SetState(kButtonDisabled);" << std::endl;
331 }
332 if (pb->GetState() == kButtonEngaged) {
333 out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
334 out << " " << pb->GetName() << "->SetState(kButtonEngaged);" << std::endl;
335 }
336 i++;
337 } else {
338 f->fFrame->SavePrimitive(out, option);
339 out << " " << GetName()<<"->AddFrame(" << f->fFrame->GetName();
340 f->fLayout->SavePrimitive(out, option);
341 out << ");"<< std::endl;
342 }
343 }
344}
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:40
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
long Longptr_t
Definition RtypesCore.h:75
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
@ kButtonDown
Definition TGButton.h:54
@ kButtonDisabled
Definition TGButton.h:56
@ kButtonEngaged
Definition TGButton.h:55
#define gClient
Definition TGClient.h:157
@ kLHintsLeft
Definition TGLayout.h:24
@ kLHintsTop
Definition TGLayout.h:27
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t button
R__EXTERN void * gTQSender
Definition TQObject.h:46
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
A button abstract base class.
Definition TGButton.h:68
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition TGButton.cxx:445
virtual EButtonState GetState() const
Definition TGButton.h:112
virtual void AllowStayDown(Bool_t a)
Definition TGButton.h:113
virtual void SetStyle(UInt_t newstyle)
Set the button style (modern or classic).
Definition TGButton.cxx:271
virtual TGToolTip * GetToolTip() const
Definition TGButton.h:110
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:289
void FreePicture(const TGPicture *pic)
Free picture resource.
Definition TGClient.cxx:317
The base class for composite widgets (menu bars, list boxes, etc.).
Definition TGFrame.h:287
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
virtual TList * GetList() const
Definition TGFrame.h:310
Int_t MustCleanup() const override
Definition TGFrame.h:360
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition TGFrame.cxx:967
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition TGFrame.cxx:683
virtual UInt_t GetOptions() const
Definition TGFrame.h:197
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition TGFrame.cxx:2506
UInt_t GetHeight() const
Definition TGFrame.h:225
UInt_t GetWidth() const
Definition TGFrame.h:224
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition TGFrame.cxx:2479
Pixel_t fBackground
frame background color
Definition TGFrame.h:95
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
TGClient * fClient
Connection to display server.
Definition TGObject.h:25
Handle_t GetId() const
Definition TGObject.h:41
Yield an action as soon as it is clicked.
Definition TGButton.h:228
const TGPicture * GetPicture() const
Definition TGButton.h:256
static TClass * Class()
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
const char * GetName() const override
Returns name of object.
Definition TGPicture.h:51
const char * GetString() const
Definition TGString.h:30
A toolbar is a composite frame that contains TGPictureButtons.
Definition TGToolBar.h:33
virtual TGButton * AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing=0)
Add button to toolbar.
Definition TGToolBar.cxx:92
TList * fTrash
list of buttons and layout hints to be deleted
Definition TGToolBar.h:37
virtual void SetId(TGButton *button, Longptr_t id)
changes id for button.
virtual void Pressed(Int_t id)
Definition TGToolBar.h:63
virtual void ButtonPressed()
This slot is activated when one of the buttons in the group emits the Pressed() signal.
virtual void Clicked(Int_t id)
Definition TGToolBar.h:65
virtual void ButtonClicked()
This slot is activated when one of the buttons in the group emits the Clicked() signal.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save an horizontal slider as a C++ statement(s) on output stream out.
TGToolBar(const TGToolBar &)=delete
virtual void ButtonReleased()
This slot is activated when one of the buttons in the group emits the Released() signal.
TList * fPictures
list of pictures that should be freed
Definition TGToolBar.h:36
virtual void ChangeIcon(ToolBarData_t *button, const char *new_icon)
Change the icon of a toolbar button.
virtual TGButton * GetButton(Int_t id) const
Finds and returns a pointer to the button with the specified identifier id.
virtual void Released(Int_t id)
Definition TGToolBar.h:64
void Cleanup() override
Cleanup and delete all objects contained in this composite frame.
TMap * fMapOfButtons
map of button/id pairs in this group
Definition TGToolBar.h:38
~TGToolBar() override
Delete toolbar and its buttons and layout hints.
Definition TGToolBar.cxx:65
const TGString * GetText() const
Get the tool tip text.
virtual void Associate(const TGWindow *w)
Definition TGWidget.h:72
Int_t WidgetId() const
Definition TGWidget.h:68
ROOT GUI Window base class.
Definition TGWindow.h:23
const TGWindow * fParent
Parent window.
Definition TGWindow.h:28
virtual void SetWindowName(const char *name=nullptr)
Set window name.
Definition TGWindow.cxx:129
const TGWindow * GetParent() const
Definition TGWindow.h:83
const char * GetName() const override
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:336
A doubly linked list.
Definition TList.h:38
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition TList.cxx:400
void Add(TObject *obj) override
Definition TList.h:81
TMap implements an associative array of (key,value) pairs using a THashTable for efficient retrieval ...
Definition TMap.h:40
void Add(TObject *obj) override
This function may not be used (but we need to provide it since it is a pure virtual in TCollection).
Definition TMap.cxx:54
TObject * FindObject(const char *keyname) const override
Check if a (key,value) pair exists with keyname as name of the key.
Definition TMap.cxx:215
TObject * GetValue(const char *keyname) const
Returns a pointer to the value associated with keyname as name of the key.
Definition TMap.cxx:236
Mother of all ROOT objects.
Definition TObject.h:41
Class used by TMap to store (key,value) pairs.
Definition TMap.h:102
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:869
Basic string class.
Definition TString.h:139
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1274
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063