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
44
45////////////////////////////////////////////////////////////////////////////////
46
48 UInt_t options, Pixel_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
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()))
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 }
99
102
103 pbut = new TGPictureButton(this, pic, button->fId);
104 pbut->SetStyle(gClient->GetStyle());
105 pbut->SetToolTipText(button->fTipText);
106
109 pbut->AllowStayDown(button->fStayDown);
110 pbut->Associate(w);
111 button->fButton = pbut;
112
113 fTrash->Add(pbut);
114 fTrash->Add(layout);
115
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();
133
136 pbut->SetStyle(gClient->GetStyle());
138 pbut->Associate(w);
139
140 fTrash->Add(pbut);
141 fTrash->Add(layout);
142
143 fMapOfButtons->Add(pbut, (TObject*)((Longptr_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 ((Longptr_t)fMapOfButtons->GetValue(item) == id) break; // found
163 }
164
165 return item;
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// changes id for button.
170
172{
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{
186 if (a)
187 return (Longptr_t)a->Value();
188 else
189 return (Longptr_t)-1;
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// Change the icon of a toolbar button.
194
196{
198 if (!pic) {
199 Error("ChangeIcon", "pixmap not found: %s", new_icon);
200 return;
201 }
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{
229
231 if (a) {
232 Int_t id = (Int_t)Longptr_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{
244
246 if (a) {
247 Int_t id = (Int_t)Longptr_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{
259
261 if (a) {
262 Int_t id = (Int_t)Longptr_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
270void TGToolBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
271{
272 auto extra_args = SaveCtorArgs(out);
273
274 out << "\n // tool bar\n";
275
276 out << " TGToolBar *" << GetName() << " = new TGToolBar(" << fParent->GetName() << "," << GetWidth() << ","
277 << GetHeight() << extra_args << ");\n";
278
279 if (option && strstr(option, "keep_names"))
280 out << " " << GetName() << "->SetName(\"" << GetName() << "\");\n";
281
282 int i = 0;
283
284 TIter next(GetList());
285
286 while (auto f = static_cast<TGFrameElement *>(next())) {
287 if (f->fFrame->InheritsFrom(TGPictureButton::Class())) {
288 if (!gROOT->ClassSaved(TGPictureButton::Class())) {
289 // declare a structure used for picture buttons
290 out << " ToolBarData_t toolbardata;\n";
291 }
292
293 TGPictureButton *pb = (TGPictureButton *)f->fFrame;
294 TString picname = gSystem->UnixPathName(pb->GetPicture()->GetName());
296
297 out << " toolbardata.fPixmap = \"" << picname.ReplaceSpecialCppChars() << "\";\n";
298 out << " toolbardata.fTipText = \""
299 << TString(pb->GetToolTip()->GetText()->GetString()).ReplaceSpecialCppChars() << "\";\n";
300 if (pb->GetState() == kButtonDown)
301 out << " toolbardata.fStayDown = kTRUE;\n";
302 else
303 out << " toolbardata.fStayDown = kFALSE;\n";
304 out << " toolbardata.fId = " << i + 1 << ";\n";
305 out << " toolbardata.fButton = nullptr;\n";
306 out << " " << GetName() << "->AddButton(" << GetParent()->GetName() << ", &toolbardata, "
307 << f->fLayout->GetPadLeft() << ");\n";
308 if (pb->GetState() == kButtonDown) {
309 out << " toolbardata.fButton->SetState(kButtonDown);\n";
310 }
311 if (pb->GetState() == kButtonDisabled) {
312 out << " toolbardata.fButton->SetState(kButtonDisabled);\n";
313 }
314 if (pb->GetState() == kButtonEngaged) {
315 out << " toolbardata.fButton->SetState(kButtonEngaged);\n";
316 }
317 i++;
318 } else {
319 f->fFrame->SavePrimitive(out, option);
320 out << " " << GetName() << "->AddFrame(" << f->fFrame->GetName();
321 f->fLayout->SavePrimitive(out, option);
322 out << ");\n";
323 }
324 }
325}
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
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:89
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
@ 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:411
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
A button abstract base class.
Definition TGButton.h:68
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:288
void FreePicture(const TGPicture *pic)
Free picture resource.
Definition TGClient.cxx:316
The base class for composite widgets (menu bars, list boxes, etc.).
Definition TGFrame.h:289
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1109
virtual TList * GetList() const
Definition TGFrame.h:312
Int_t MustCleanup() const override
Definition TGFrame.h:362
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition TGFrame.cxx:959
UInt_t GetHeight() const
Definition TGFrame.h:227
TString SaveCtorArgs(std::ostream &out, UInt_t dflt_options=kChildFrame, Bool_t check_white_pixel=kFALSE)
Return options and custom color as constructor args Used in the SavePrimitive methods,...
Definition TGFrame.cxx:2493
UInt_t GetWidth() const
Definition TGFrame.h:226
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
static TClass * Class()
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
virtual TGButton * AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing=0)
Add button to toolbar.
Definition TGToolBar.cxx:91
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:64
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:127
const TGWindow * GetParent() const
Definition TGWindow.h:83
const char * GetName() const override
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:334
A doubly linked list.
Definition TList.h:38
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition TList.cxx:399
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:53
TObject * FindObject(const char *keyname) const override
Check if a (key,value) pair exists with keyname as name of the key.
Definition TMap.cxx:214
TObject * GetValue(const char *keyname) const
Returns a pointer to the value associated with keyname as name of the key.
Definition TMap.cxx:235
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:865
Basic string class.
Definition TString.h:138
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1285
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1073