Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
43 ClassImp(TGToolBar);
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 TGToolBar::TGToolBar(const TGWindow *p, UInt_t w, UInt_t h,
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 
64 TGToolBar::~TGToolBar()
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 
91 TGButton *TGToolBar::AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing)
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 
129 TGButton *TGToolBar::AddButton(const TGWindow *w, TGPictureButton *pbut, Int_t spacing)
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 
156 TGButton *TGToolBar::GetButton(Int_t id) const
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 
171 void TGToolBar::SetId(TGButton *button, Long_t id)
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 
183 Long_t TGToolBar::GetId(TGButton *button) const
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 
213 void TGToolBar::Cleanup()
214 {
215  // avoid double deletion of objects in trash
216  delete fTrash;
217  fTrash = 0;
218 
219  TGCompositeFrame::Cleanup();
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// This slot is activated when one of the buttons in the group emits the
224 /// Pressed() signal.
225 
226 void TGToolBar::ButtonPressed()
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 
241 void TGToolBar::ButtonReleased()
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 
256 void TGToolBar::ButtonClicked()
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 
281  if (fBackground == GetDefaultFrameBackground()) {
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())) {
302  if (f->fFrame->InheritsFrom(TGPictureButton::Class())) {
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 
308  TGPictureButton *pb = (TGPictureButton *)f->fFrame;
309  picname = pb->GetPicture()->GetName();
310 
311  out << " t.fPixmap = " << quote
312  << gSystem->ExpandPathName(gSystem->UnixPathName(picname))
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 }