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