Logo ROOT   6.10/09
Reference Guide
TGToolTip.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 22/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 // TGToolTip //
26 // //
27 // A tooltip can be a one or multiple lines help text that is displayed //
28 // in a window when the mouse cursor overs a widget, without clicking //
29 // it. A small box appears with suplementary information regarding the //
30 // item being hovered over. // //
31 // //
32 // A multiline tooltip can be created by inserting a new-line character //
33 // "\n" in the tooltip string. For example: //
34 // //
35 // fButton->SetToolTipText("Go to the ROOT page\n (http://root.cern.ch) //
36 // //
37 //////////////////////////////////////////////////////////////////////////
38 
39 #include "TGToolTip.h"
40 #include "TGLabel.h"
41 #include "TGResourcePool.h"
42 #include "TTimer.h"
43 #include "TSystem.h"
44 #include "TVirtualPad.h"
45 #include "TBox.h"
46 
47 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 
52 class TTipDelayTimer : public TTimer {
53 private:
54  TGToolTip *fTip; // tooltip
55 public:
56  TTipDelayTimer(TGToolTip *tip, Long_t ms) : TTimer(ms, kTRUE) { fTip = tip; }
57  Bool_t Notify();
58 };
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Notify when timer times out and reset the timer.
62 
63 Bool_t TTipDelayTimer::Notify()
64 {
65  fTip->HandleTimer(0);
66  Reset();
67  return kFALSE;
68 }
69 
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Create a tool tip. P is the tool tips parent window (normally
73 /// fClient->GetRoot(), f is the frame to which the tool tip is associated,
74 /// text is the tool tip one-liner and delayms is the delay in ms before
75 /// the tool tip is shown.
76 
77 TGToolTip::TGToolTip(const TGWindow *p, const TGFrame *f, const char *text,
78  Long_t delayms)
80 {
83  attr.fOverrideRedirect = kTRUE;
84  attr.fSaveUnder = kTRUE;
85 
86  gVirtualX->ChangeWindowAttributes(fId, &attr);
88 
89  fLabel = new TGLabel(this, text);
90  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
91  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
92 
94  2, 3, 0, 0));
95  MapSubwindows();
97 
98  fWindow = f;
99  fPad = 0;
100  fBox = 0;
101  fX = fY = -1;
102  fDelay = new TTipDelayTimer(this, delayms);
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Create a tool tip. P is the tool tips parent window (normally
107 /// fClient->GetRoot(), box is the area to which the tool tip is associated,
108 /// text is the tool tip one-liner and delayms is the delay in ms before
109 /// the tool tip is shown. When using this ctor with the box argument
110 /// you have to use Reset(const TVirtualPad *parent).
111 
112 TGToolTip::TGToolTip(const TGWindow *p, const TBox *box, const char *text,
113  Long_t delayms)
115 {
118  attr.fOverrideRedirect = kTRUE;
119  attr.fSaveUnder = kTRUE;
120 
121  gVirtualX->ChangeWindowAttributes(fId, &attr);
123 
124  fLabel = new TGLabel(this, text);
125  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
126  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
127 
129  2, 3, 0, 0));
130  MapSubwindows();
132 
133  fWindow = 0;
134  fPad = 0;
135  fBox = box;
136  fDelay = new TTipDelayTimer(this, delayms);
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Create a tool tip in the parent window gClient->GetRoot(),
141 /// box is the area to which the tool tip is associated,
142 /// text is the tool tip one-liner and delayms is the delay in ms before
143 /// the tool tip is shown. When using this ctor with the box argument
144 /// you have to use Reset(const TVirtualPad *parent).
145 
146 TGToolTip::TGToolTip(const TBox *box, const char *text,Long_t delayms)
148 {
151  attr.fOverrideRedirect = kTRUE;
152  attr.fSaveUnder = kTRUE;
153 
154  gVirtualX->ChangeWindowAttributes(fId, &attr);
156 
157  fLabel = new TGLabel(this, text);
158  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
159  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
160 
162  2, 3, 0, 0));
163  MapSubwindows();
165 
166  fWindow = 0;
167  fPad = 0;
168  fBox = box;
169  fDelay = new TTipDelayTimer(this, delayms);
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Create a tool tip on global coordinates x, y.
174 /// text is the tool tip one-liner and delayms is the delay in ms before
175 /// the tool tip is shown.
176 
177 TGToolTip::TGToolTip(Int_t x, Int_t y, const char *text, Long_t delayms)
178  : TGCompositeFrame(gClient->GetDefaultRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
179 {
182  attr.fOverrideRedirect = kTRUE;
183  attr.fSaveUnder = kTRUE;
184 
185  gVirtualX->ChangeWindowAttributes(fId, &attr);
187 
188  fLabel = new TGLabel(this, text);
189  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
190  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
191 
193  2, 3, 0, 0));
194  MapSubwindows();
196 
197  fWindow = 0;
198  fPad = 0;
199  fBox = 0;
200  fX = x;
201  fY = y;
202  fDelay = new TTipDelayTimer(this, delayms);
203 }
204 
205 ////////////////////////////////////////////////////////////////////////////////
206 /// Delete a tool tip object.
207 
209 {
210  delete fDelay;
211  delete fLabel;
212  delete fL1;
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Draw border of tool tip window.
217 
219 {
220  gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, fWidth-2, 0);
221  gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, 0, fHeight-2);
222  gVirtualX->DrawLine(fId, GetBlackGC()(), 0, fHeight-1, fWidth-1, fHeight-1);
223  gVirtualX->DrawLine(fId, GetBlackGC()(), fWidth-1, fHeight-1, fWidth-1, 0);
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Show tool tip window.
228 
230 {
231  Move(x, y);
232  MapWindow();
233  RaiseWindow();
234 
235  Long_t args[2];
236  args[0] = x;
237  args[1] = y;
238 
239  Emit("Show(Int_t,Int_t)", args);
240 }
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Hide tool tip window. Use this method to hide the tool tip in a client
244 /// class.
245 
247 {
248  UnmapWindow();
249 
250  fDelay->Remove();
251 
252  Emit("Hide()");
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Reset tool tip popup delay timer. Use this method to activate tool tip
257 /// in a client class.
258 
260 {
261  fDelay->Reset();
263 
264  Emit("Reset()");
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Reset tool tip popup delay timer. Use this method to activate tool tip
269 /// in a client class. Use this method for graphics objects drawn in a
270 /// TCanvas, also the tool tip must have been created with the ctor
271 /// taking the TBox as argument.
272 
273 void TGToolTip::Reset(const TVirtualPad *parent)
274 {
275  fPad = parent;
276 
277  fDelay->Reset();
279 }
280 
281 ////////////////////////////////////////////////////////////////////////////////
282 /// If tool tip delay timer times out show tool tip window.
283 
285 {
286  Int_t x = 0, y = 0, px1 = 0, px2 = 0, py1 = 0;
287  Window_t wtarget;
288 
289  if (fWindow) {
290  gVirtualX->TranslateCoordinates(fWindow->GetId(), GetParent()->GetId(),
291  fX == -1 ? Int_t(fWindow->GetWidth() >> 1) : fX,
292  fY == -1 ? Int_t(fWindow->GetHeight()) : fY,
293  x, y, wtarget);
294  } else if(fPad) {
295  if (fBox) {
296  px1 = fPad->XtoAbsPixel(fBox->GetX1());
297  px2 = fPad->XtoAbsPixel(fBox->GetX2());
298  py1 = fPad->YtoAbsPixel(fBox->GetY1());
299  // py2 = fPad->YtoAbsPixel(fBox->GetY2());
300  } else {
301  px1 = fPad->XtoAbsPixel(fPad->GetX1());
302  px2 = fPad->XtoAbsPixel(fPad->GetX2());
303  py1 = fPad->YtoAbsPixel(fPad->GetY1());
304  // py2 = fPad->YtoAbsPixel(fPad->GetY2());
305  }
306  gVirtualX->TranslateCoordinates(gVirtualX->GetWindowID(fPad->GetCanvasID()),
307  GetParent()->GetId(),
308  px1 + ((px2-px1) >> 1), py1,
309  x, y, wtarget);
310  } else {
311  x = fX;
312  y = fY;
313  }
314 
315  Int_t move = 0;
316  Window_t dum1, dum2;
317  UInt_t mask = 0;
318  Int_t mx, my;
319  UInt_t screenW = fClient->GetDisplayWidth();
320  UInt_t screenH = fClient->GetDisplayHeight();
321 
322  gVirtualX->QueryPointer(gVirtualX->GetDefaultRootWindow(),
323  dum1, dum2, mx, my, mx, my, mask);
324 
325  fLabel->SetWrapLength(-1);
327 
328  // don't allow tooltip text lines longer than half the screen size
329  if (fWidth > (screenW/2))
330  fLabel->SetWrapLength((screenW/2)-15);
332 
333  if (x + fWidth > screenW) {
334  x = screenW - fWidth;
335  move += 1;
336  }
337 
338  if (y+4 + GetHeight() > screenH) {
339  y = screenH - (fHeight + 25);
340  move += 2;
341  }
342 
343  // check if the mouse is inside the tooltip (may happen after
344  // adjusting the position when out of screen) and place the tooltip
345  // on the other side of the mouse pointer
346  TGRectangle rect(x, y, x+fWidth, y+fHeight);
347  if (rect.Contains(mx, my)) {
348  if (move == 1) { // left
349  if (fWidth+15 < (UInt_t)mx)
350  x = mx - fWidth - 15;
351  else if (my + fHeight+15 < screenH)
352  y = my + 15;
353  else if (fHeight+15 < (UInt_t)my)
354  y = my - fHeight - 15;
355  }
356  else if (move == 2) { // up
357  if (mx + fWidth+15 < screenW)
358  x = mx + 15;
359  else if (fHeight+15 < (UInt_t)my)
360  y = my - fHeight - 15;
361  else if (fWidth+15 < (UInt_t)mx)
362  x = mx - fWidth - 15;
363  }
364  else { // up & left, right, down, ...
365  if (my + fHeight+15 < screenH)
366  y = my + 15;
367  else if (mx + fWidth+15 < screenW)
368  x = mx + 15;
369  else if (fWidth+15 < (UInt_t)mx)
370  x = mx - fWidth - 15;
371  else if (fHeight+15 < (UInt_t)my)
372  y = my - fHeight - 15;
373  }
374  }
375 
376  Show(x, y+4);
377 
378  fDelay->Remove();
379 
380  return kTRUE;
381 }
382 
383 ////////////////////////////////////////////////////////////////////////////////
384 /// Set new tool tip text.
385 
386 void TGToolTip::SetText(const char *new_text)
387 {
388  fLabel->SetText(new TGString(new_text));
390 }
391 
392 ////////////////////////////////////////////////////////////////////////////////
393 /// Set delay in milliseconds.
394 
396 {
397  fDelay->SetTime(delayms);
398 }
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 /// Set popup position within specified frame (as specified in the ctor).
402 /// To get back default behaviour (in the middle just below the designated
403 /// frame) set position to -1,-1.
404 
406 {
407  fX = x;
408  fY = y;
409 
410  if (fX < -1)
411  fX = 0;
412  if (fY < -1)
413  fY = 0;
414 
415  if (fWindow) {
416  if (fX > (Int_t) fWindow->GetWidth())
417  fX = fWindow->GetWidth();
418  if (fY > (Int_t) fWindow->GetHeight())
419  fY = fWindow->GetHeight();
420  }
421 }
422 
423 ////////////////////////////////////////////////////////////////////////////////
424 /// Get the tool tip text.
425 
427 {
428  return fLabel->GetText();
429 
430 }
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
Int_t fX
Definition: TGToolTip.h:44
const TVirtualPad * fPad
Definition: TGToolTip.h:42
Double_t GetX2() const
Definition: TBox.h:53
void Reset()
Reset the timer.
Definition: TTimer.cxx:157
Create a Box.
Definition: TBox.h:24
const TGString * GetText() const
Definition: TGLabel.h:82
Bool_t Contains(Int_t px, Int_t py) const
Definition: TGDimension.h:129
void Reset()
Reset tool tip popup delay timer.
Definition: TGToolTip.cxx:259
void SetText(const char *new_text)
Set new tool tip text.
Definition: TGToolTip.cxx:386
UInt_t GetHeight() const
Definition: TGFrame.h:272
const TGResourcePool * GetResourcePool() const
Definition: TGClient.h:133
void Show(Int_t x, Int_t y)
Show tool tip window.
Definition: TGToolTip.cxx:229
#define gClient
Definition: TGClient.h:166
Int_t fY
Definition: TGToolTip.h:45
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void Hide()
Hide tool tip window.
Definition: TGToolTip.cxx:246
Pixel_t GetTipFgndColor() const
UInt_t GetWidth() const
Definition: TGFrame.h:271
void SetWrapLength(Int_t wl)
Definition: TGLabel.h:103
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
TGLabel * fLabel
Definition: TGToolTip.h:38
Handle_t GetId() const
Definition: TGObject.h:48
virtual Double_t GetX1() const =0
UInt_t GetDisplayWidth() const
Get display width.
Definition: TGClient.cxx:260
Double_t x[n]
Definition: legend1.C:17
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:177
Pixel_t GetTipBgndColor() const
virtual void SetBackgroundColor(Pixel_t back)
Set background color (override from TGWindow base class).
Definition: TGFrame.cxx:294
void SetDelay(Long_t delayms)
Set delay in milliseconds.
Definition: TGToolTip.cxx:395
void SetPosition(Int_t x, Int_t y)
Set popup position within specified frame (as specified in the ctor).
Definition: TGToolTip.cxx:405
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
virtual Bool_t Notify()
Notify when timer times out.
Definition: TTimer.cxx:143
Bool_t fOverrideRedirect
Definition: GuiTypes.h:106
void Remove()
Definition: TTimer.h:85
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
const Mask_t kWASaveUnder
Definition: GuiTypes.h:149
virtual TGDimension GetDefaultSize() const
std::cout << fWidth << "x" << fHeight << std::endl;
Definition: TGFrame.h:375
virtual Int_t XtoAbsPixel(Double_t x) const =0
Bool_t HandleTimer(TTimer *t)
If tool tip delay timer times out show tool tip window.
Definition: TGToolTip.cxx:284
virtual ~TGToolTip()
Delete a tool tip object.
Definition: TGToolTip.cxx:208
unsigned int UInt_t
Definition: RtypesCore.h:42
TGToolTip(const TGToolTip &)
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
virtual Int_t GetCanvasID() const =0
static const TGGC & GetBlackGC()
Get black graphics context.
Definition: TGFrame.cxx:717
TTimer * fDelay
Definition: TGToolTip.h:40
#define gVirtualX
Definition: TVirtualX.h:350
virtual Double_t GetY1() const =0
UInt_t fWidth
Definition: TGFrame.h:134
virtual void Move(Int_t x, Int_t y)
Move frame.
Definition: TGFrame.cxx:575
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual Int_t YtoAbsPixel(Double_t y) const =0
long Long_t
Definition: RtypesCore.h:50
#define ClassImp(name)
Definition: Rtypes.h:336
double f(double x)
Double_t GetX1() const
Definition: TBox.h:52
TText * text
virtual void RaiseWindow()
Definition: TGWindow.h:90
const Mask_t kWAOverrideRedirect
Definition: GuiTypes.h:148
Double_t y[n]
Definition: legend1.C:17
UInt_t fHeight
Definition: TGFrame.h:135
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
UInt_t GetDisplayHeight() const
Get display height.
Definition: TGClient.cxx:273
const TGWindow * GetParent() const
Definition: TGWindow.h:81
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1146
Handle_t fId
Definition: TGObject.h:36
Handle_t Window_t
Definition: GuiTypes.h:28
virtual void UnmapWindow()
Definition: TGFrame.h:253
const TBox * fBox
Definition: TGToolTip.h:43
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition: TSystem.cxx:478
virtual void MapWindow()
Definition: TGFrame.h:251
TGClient * fClient
Definition: TGObject.h:37
virtual Double_t GetX2() const =0
TGLayoutHints * fL1
Definition: TGToolTip.h:39
const TGString * GetText() const
Get the tool tip text.
Definition: TGToolTip.cxx:426
Double_t GetY1() const
Definition: TBox.h:54
void Emit(const char *signal, const T &arg)
Activate signal with single parameter.
Definition: TQObject.h:164
const Bool_t kTRUE
Definition: RtypesCore.h:91
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition: TGFrame.cxx:747
virtual void DrawBorder()
Draw border of tool tip window.
Definition: TGToolTip.cxx:218
const TGFrame * fWindow
Definition: TGToolTip.h:41
void SetTime(Long_t milliSec)
Definition: TTimer.h:90