Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGShutter.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 18/9/2000
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 // //
14 // TGShutter, TGShutterItem //
15 // //
16 // A shutter widget contains a set of shutter items that can be //
17 // open and closed like a shutter. //
18 // This widget is usefull to group a large number of options in //
19 // a number of categories. //
20 // //
21 //////////////////////////////////////////////////////////////////////////
22 
23 #include "TGShutter.h"
24 #include "TGButton.h"
25 #include "TList.h"
26 #include "TTimer.h"
27 #include "Riostream.h"
28 
29 
30 ClassImp(TGShutterItem);
31 ClassImp(TGShutter);
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Create shutter frame.
35 
36 TGShutter::TGShutter(const TGWindow *p, UInt_t options) :
37  TGCompositeFrame(p, 10, 10, options)
38 {
39  fSelectedItem = 0;
40  fClosingItem = 0;
41  fHeightIncrement = 1;
42  fClosingHeight = 0;
43  fClosingHadScrollbar = kFALSE;
44  fTimer = 0;
45  fTrash = new TList;
46 
47  fDefWidth = fDefHeight = 0;
48 
49  // layout manager is not used
50  delete fLayoutManager;
51  fLayoutManager = 0;
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Cleanup shutter widget.
56 
57 TGShutter::~TGShutter()
58 {
59  if (fTimer) delete fTimer;
60 
61  if (!MustCleanup()) {
62  fTrash->Delete();
63  }
64  delete fTrash;
65  fTrash = 0;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Add shutter item to shutter frame.
70 
71 void TGShutter::AddItem(TGShutterItem *item)
72 {
73  TGLayoutHints *hints = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY);
74  AddFrame(item, hints);
75  fTrash->Add(hints);
76  if (!fSelectedItem) {
77  fSelectedItem = item;
78  }
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Remove item from shutter
83 
84 void TGShutter::RemoveItem(const char *name)
85 {
86  TGShutterItem *item = GetItem(name);
87 
88  if (!item) {
89  return;
90  }
91 
92  if (fList->GetEntries() <= 1) {
93  return;
94  }
95 
96  if (item == fSelectedItem) {
97  TGFrameElement *fe = (TGFrameElement*)fList->FindObject(item->GetFrameElement());
98  if (fe) {
99  TGFrameElement *sel = (TGFrameElement*)fList->Before(fe);
100  if (!sel) {
101  sel = (TGFrameElement*)fList->After(fe);
102  }
103  if (!sel) {
104  return;
105  }
106  SetSelectedItem((TGShutterItem*)sel->fFrame);
107  }
108  }
109  RemoveFrame(item);
110 
111  item->DestroyWindow();
112  delete item;
113  Layout();
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Remove selected page
118 
119 void TGShutter::RemovePage()
120 {
121  if (!fSelectedItem) {
122  return;
123  }
124  TGTextButton *btn = (TGTextButton*)fSelectedItem->GetButton();
125  RemoveItem(btn->GetString().Data());
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// Rename selected page
130 
131 void TGShutter::RenamePage(const char *name)
132 {
133  if (!fSelectedItem) {
134  return;
135  }
136  TGTextButton *btn = (TGTextButton*)fSelectedItem->GetButton();
137  btn->SetText(name);
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Add new page (shutter item)
142 
143 TGShutterItem *TGShutter::AddPage(const char *name)
144 {
145  static int id = 1000;
146  TGShutterItem *item = new TGShutterItem(this, new TGHotString(name), id++);
147  AddItem(item);
148  MapSubwindows();
149  Layout();
150  return item;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Handle shutter messages.
155 
156 Bool_t TGShutter::ProcessMessage(Long_t /*msg*/, Long_t parm1, Long_t /*parm2*/)
157 {
158  if (!fList) return kFALSE;
159 
160  TGFrameElement *el;
161  TGShutterItem *child, *item = 0;
162 
163  TIter next(fList);
164  while ((el = (TGFrameElement *) next())) {
165  child = (TGShutterItem *) el->fFrame;
166  if (parm1 == child->WidgetId()) {
167  item = child;
168  break;
169  }
170  }
171 
172  if (!item) return kFALSE;
173 
174  if (!fSelectedItem)
175  fSelectedItem = (TGShutterItem*) ((TGFrameElement*)fList->First())->fFrame;
176  if (fSelectedItem == item) return kTRUE;
177 
178  fHeightIncrement = 1;
179  fClosingItem = fSelectedItem;
180  fClosingHeight = fClosingItem->GetHeight();
181  fClosingHeight -= fClosingItem->fButton->GetDefaultHeight();
182  fSelectedItem = item;
183  Selected(fSelectedItem);
184  fSelectedItem->Selected();
185 
186  if (!fTimer) fTimer = new TTimer(this, 6); //10);
187  fTimer->Reset();
188  fTimer->TurnOn();
189 
190  return kTRUE;
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 /// Shutter item animation.
195 
196 Bool_t TGShutter::HandleTimer(TTimer *)
197 {
198  if (!fClosingItem) return kFALSE;
199  fClosingHeight -= fHeightIncrement;
200  fHeightIncrement += 5;
201  if (fClosingHeight > 0) {
202  fTimer->Reset();
203  } else {
204  fClosingItem = 0;
205  fClosingHeight = 0;
206  fTimer->TurnOff();
207  }
208  Layout();
209 
210  return kTRUE;
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// Layout shutter items.
215 
216 void TGShutter::Layout()
217 {
218  TGFrameElement *el;
219  TGShutterItem *child;
220  Int_t y, bh, exh;
221 
222  if (!fList) return;
223 
224  if (!fSelectedItem)
225  fSelectedItem = (TGShutterItem*) ((TGFrameElement*)GetList()->First())->fFrame;
226 
227  exh = Int_t(fHeight - (fBorderWidth << 1));
228  TIter next(fList);
229  while ((el = (TGFrameElement *) next())) {
230  child = (TGShutterItem *) el->fFrame;
231  bh = child->fButton->GetDefaultHeight();
232  exh -= bh;
233  }
234 
235  y = fBorderWidth;
236  next.Reset();
237  while ((el = (TGFrameElement *) next())) {
238  child = (TGShutterItem *) el->fFrame;
239  bh = child->fButton->GetDefaultHeight();
240  if (child == fSelectedItem) {
241  if (fClosingItem)
242  child->fCanvas->SetScrolling(TGCanvas::kCanvasNoScroll);
243  else
244  child->fCanvas->SetScrolling(TGCanvas::kCanvasScrollVertical);
245  child->ShowFrame(child->fCanvas);
246  child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1),
247  exh - fClosingHeight + bh);
248  y += exh - fClosingHeight + bh;
249  } else if (child == fClosingItem) {
250  child->fCanvas->SetScrolling(TGCanvas::kCanvasNoScroll);
251  child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1),
252  fClosingHeight + bh);
253  y += fClosingHeight + bh;
254  } else {
255  child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1), bh);
256  child->HideFrame(child->fCanvas);
257  y += bh;
258  }
259  }
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// Set item to be the currently open shutter item.
264 
265 void TGShutter::SetSelectedItem(TGShutterItem *item)
266 {
267  fSelectedItem = item;
268  fSelectedItem->Selected(); // emit signal
269  Layout();
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// Set item to be the currently open shutter item.
274 
275 void TGShutter::SetSelectedItem(const char *name)
276 {
277  TGShutterItem *item = GetItem(name);
278  if (!item) {
279  return;
280  }
281  SetSelectedItem(item);
282 }
283 
284 ////////////////////////////////////////////////////////////////////////////////
285 /// Disable/enbale shutter item.
286 
287 void TGShutter::EnableItem(const char *name, Bool_t on)
288 {
289  TGShutterItem *item = GetItem(name);
290  if (!item) {
291  return;
292  }
293 
294  item->GetButton()->SetEnabled(on);
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 /// returns a shutter item by name (name is hot string of shutter item)
299 
300 TGShutterItem *TGShutter::GetItem(const char *name)
301 {
302  TGFrameElement *el;
303  TGShutterItem *item = 0;
304 
305  TIter next(fList);
306 
307  while ((el = (TGFrameElement *) next())) {
308  TGTextButton *btn;
309  item = (TGShutterItem *)el->fFrame;
310  btn = (TGTextButton*)item->GetButton();
311  if (btn->GetString() == name) return item;
312  }
313 
314  return item;
315 }
316 
317 ////////////////////////////////////////////////////////////////////////////////
318 /// Return the default / minimal size of the widget.
319 
320 TGDimension TGShutter::GetDefaultSize() const
321 {
322  UInt_t w = (GetOptions() & kFixedWidth) || (fDefWidth == 0) ? fWidth : fDefWidth;
323  UInt_t h = (GetOptions() & kFixedHeight) || (fDefHeight == 0) ? fHeight : fDefHeight;
324  return TGDimension(w, h);
325 }
326 
327 ////////////////////////////////////////////////////////////////////////////////
328 /// Set the default / minimal size of the widget.
329 
330 void TGShutter::SetDefaultSize(UInt_t w, UInt_t h)
331 {
332  fDefWidth = w;
333  fDefHeight = h;
334 }
335 
336 
337 ////////////////////////////////////////////////////////////////////////////////
338 /// Create a shutter item.
339 
340 TGShutterItem::TGShutterItem(const TGWindow *p, TGHotString *s, Int_t id,
341  UInt_t options) :
342  TGVerticalFrame (p, 10, 10, options), TGWidget (id)
343 {
344  if (!p && !s) {
345  MakeZombie();
346  // coverity [uninit_ctor]
347  return;
348  }
349  fButton = new TGTextButton(this, s, id);
350  fCanvas = new TGCanvas(this, 10, 10, kChildFrame);
351  fContainer = new TGVerticalFrame(fCanvas->GetViewPort(), 10, 10, kOwnBackground);
352  fCanvas->SetContainer(fContainer);
353  fContainer->SetBackgroundColor(fClient->GetShadow(GetDefaultFrameBackground()));
354 
355  AddFrame(fButton, fL1 = new TGLayoutHints(kLHintsTop | kLHintsExpandX));
356  AddFrame(fCanvas, fL2 = new TGLayoutHints(kLHintsExpandY | kLHintsExpandX));
357 
358  fButton->Associate((TGFrame *) p);
359 
360  fCanvas->SetEditDisabled(kEditDisableGrab | kEditDisableLayout);
361  fButton->SetEditDisabled(kEditDisableGrab | kEditDisableBtnEnable);
362  fContainer->SetEditDisabled(kEditDisableGrab);
363  fEditDisabled = kEditDisableGrab | kEditDisableLayout;
364 }
365 
366 ////////////////////////////////////////////////////////////////////////////////
367 /// Clan up shutter item.
368 
369 TGShutterItem::~TGShutterItem()
370 {
371  if (!IsZombie() && !MustCleanup()) {
372  delete fL1;
373  delete fL2;
374  delete fButton;
375  delete fContainer;
376  delete fCanvas;
377  }
378 }
379 
380 ////////////////////////////////////////////////////////////////////////////////
381 /// Save a shutter item widget as a C++ statement(s) on output stream out
382 
383 void TGShutterItem::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
384 {
385  char quote = '"';
386  TGTextButton *b = (TGTextButton *)fButton;
387  const char *text = b->GetText()->GetString();
388  char hotpos = b->GetText()->GetHotPos();
389  Int_t lentext = b->GetText()->GetLength();
390  char *outext = new char[lentext+2]; // should be +2 because of \0
391  Int_t i=0;
392 
393  while (lentext) {
394  if (i == hotpos-1) {
395  outext[i] = '&';
396  i++;
397  }
398  outext[i] = *text;
399  i++;
400  text++;
401  lentext--;
402  }
403  outext[i]=0;
404 
405  out << std::endl;
406  out << " // " << quote << outext << quote << " shutter item " << std::endl;
407  out << " TGShutterItem *";
408  out << GetName() << " = new TGShutterItem(" << fParent->GetName()
409  << ", new TGHotString(" << quote << outext << quote << "),"
410  << fButton->WidgetId() << "," << GetOptionString() << ");" << std::endl;
411 
412  delete [] outext;
413  if (option && strstr(option, "keep_names"))
414  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
415 
416  TList *list = ((TGCompositeFrame *)GetContainer())->GetList();
417 
418  if (!list) return;
419 
420  out << " TGCompositeFrame *" << GetContainer()->GetName()
421  << " = (TGCompositeFrame *)" << GetName() << "->GetContainer();" << std::endl;
422 
423  TGFrameElement *el;
424  TIter next(list);
425 
426  while ((el = (TGFrameElement *) next())) {
427  el->fFrame->SavePrimitive(out, option);
428  out << " " << GetContainer()->GetName() <<"->AddFrame(" << el->fFrame->GetName();
429  el->fLayout->SavePrimitive(out, option);
430  out << ");"<< std::endl;
431  }
432 }
433 
434 ////////////////////////////////////////////////////////////////////////////////
435 /// Save a shutter widget as a C++ statement(s) on output stream out.
436 
437 void TGShutter::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
438 {
439  out << std::endl;
440  out << " // shutter" << std::endl;
441 
442  out << " TGShutter *";
443  out << GetName() << " = new TGShutter(" << fParent->GetName() << ","
444  << GetOptionString() << ");" << std::endl;
445 
446  if ((fDefWidth > 0) || (fDefHeight > 0)) {
447  out << " " << GetName() << "->SetDefaultSize(";
448  out << fDefWidth << "," << fDefHeight << ");" << std::endl;
449  }
450  if (option && strstr(option, "keep_names"))
451  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
452 
453  if (!fList) return;
454 
455  TGFrameElement *el;
456  TIter next(fList);
457 
458  while ((el = (TGFrameElement *) next())) {
459  el->fFrame->SavePrimitive(out, option);
460  out << " " << GetName() <<"->AddItem(" << el->fFrame->GetName();
461  //el->fLayout->SavePrimitive(out, option);
462  out << ");"<< std::endl;
463  }
464 
465  out << " " << GetName() << "->SetSelectedItem("
466  << GetSelectedItem()->GetName() << ");" << std::endl;
467  out << " " <<GetName()<< "->Resize("<<GetWidth()<<","<<GetHeight()<<");"<<std::endl;
468 }
469