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
53class TTipDelayTimer : public TTimer {
54private:
55 TGToolTip *fTip; // tooltip
56public:
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
64Bool_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
78TGToolTip::TGToolTip(const TGWindow *p, const TGFrame *f, const char *text,
79 Long_t delayms)
81{
85 attr.fSaveUnder = kTRUE;
86
87 gVirtualX->ChangeWindowAttributes(fId, &attr);
89
90 fLabel = new TGLabel(this, text);
93
95 2, 3, 0, 0));
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
113TGToolTip::TGToolTip(const TGWindow *p, const TBox *box, const char *text,
114 Long_t delayms)
116{
120 attr.fSaveUnder = kTRUE;
121
122 gVirtualX->ChangeWindowAttributes(fId, &attr);
124
125 fLabel = new TGLabel(this, text);
128
130 2, 3, 0, 0));
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
147TGToolTip::TGToolTip(const TBox *box, const char *text,Long_t delayms)
149{
153 attr.fSaveUnder = kTRUE;
154
155 gVirtualX->ChangeWindowAttributes(fId, &attr);
157
158 fLabel = new TGLabel(this, text);
161
163 2, 3, 0, 0));
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
179 : TGCompositeFrame(gClient->GetDefaultRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
180{
184 attr.fSaveUnder = kTRUE;
185
186 gVirtualX->ChangeWindowAttributes(fId, &attr);
188
189 fLabel = new TGLabel(this, text);
192
194 2, 3, 0, 0));
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
274void 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
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
387void 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}
const Mask_t kWAOverrideRedirect
Definition: GuiTypes.h:148
const Mask_t kWASaveUnder
Definition: GuiTypes.h:149
@ kRaisedFrame
Definition: GuiTypes.h:384
@ kTempFrame
Definition: GuiTypes.h:393
@ kHorizontalFrame
Definition: GuiTypes.h:382
Handle_t Window_t
Definition: GuiTypes.h:28
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
long Long_t
Definition: RtypesCore.h:52
bool Bool_t
Definition: RtypesCore.h:61
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
#define gClient
Definition: TGClient.h:166
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsTop
Definition: TGLayout.h:34
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
#define gVirtualX
Definition: TVirtualX.h:338
Create a Box.
Definition: TBox.h:24
Double_t GetX1() const
Definition: TBox.h:52
Double_t GetX2() const
Definition: TBox.h:53
Double_t GetY1() const
Definition: TBox.h:54
const TGResourcePool * GetResourcePool() const
Definition: TGClient.h:133
UInt_t GetDisplayHeight() const
Get display height.
Definition: TGClient.cxx:275
UInt_t GetDisplayWidth() const
Get display width.
Definition: TGClient.cxx:262
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1101
virtual TGDimension GetDefaultSize() const
std::cout << fWidth << "x" << fHeight << std::endl;
Definition: TGFrame.h:353
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1148
static const TGGC & GetBlackGC()
Get black graphics context.
Definition: TGFrame.cxx:719
UInt_t fHeight
Definition: TGFrame.h:113
virtual void SetBackgroundColor(Pixel_t back)
Set background color (override from TGWindow base class).
Definition: TGFrame.cxx:296
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition: TGFrame.cxx:749
virtual void Move(Int_t x, Int_t y)
Move frame.
Definition: TGFrame.cxx:577
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:589
UInt_t fWidth
Definition: TGFrame.h:112
UInt_t GetHeight() const
Definition: TGFrame.h:250
virtual void MapWindow()
map window
Definition: TGFrame.h:229
UInt_t GetWidth() const
Definition: TGFrame.h:249
virtual void UnmapWindow()
unmap window
Definition: TGFrame.h:231
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:360
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:178
void SetWrapLength(Int_t wl)
Definition: TGLabel.h:103
const TGString * GetText() const
Definition: TGLabel.h:82
TGClient * fClient
Definition: TGObject.h:37
Handle_t GetId() const
Definition: TGObject.h:47
Handle_t fId
Definition: TGObject.h:36
Bool_t Contains(Int_t px, Int_t py) const
Definition: TGDimension.h:114
Pixel_t GetTipBgndColor() const
Pixel_t GetTipFgndColor() const
void Show(Int_t x, Int_t y)
Show tool tip window.
Definition: TGToolTip.cxx:230
void SetDelay(Long_t delayms)
Set delay in milliseconds.
Definition: TGToolTip.cxx:396
Int_t fX
Definition: TGToolTip.h:44
Int_t fY
Definition: TGToolTip.h:45
void Hide()
Hide tool tip window.
Definition: TGToolTip.cxx:247
const TGFrame * fWindow
Definition: TGToolTip.h:41
void SetPosition(Int_t x, Int_t y)
Set popup position within specified frame (as specified in the ctor).
Definition: TGToolTip.cxx:406
TTimer * fDelay
Definition: TGToolTip.h:40
TGLayoutHints * fL1
Definition: TGToolTip.h:39
virtual ~TGToolTip()
Delete a tool tip object.
Definition: TGToolTip.cxx:209
const TBox * fBox
Definition: TGToolTip.h:43
TGToolTip(const TGToolTip &)
virtual void DrawBorder()
Draw border of tool tip window.
Definition: TGToolTip.cxx:219
const TGString * GetText() const
Get the tool tip text.
Definition: TGToolTip.cxx:427
TGLabel * fLabel
Definition: TGToolTip.h:38
void SetText(const char *new_text)
Set new tool tip text.
Definition: TGToolTip.cxx:387
void Reset()
Reset tool tip popup delay timer.
Definition: TGToolTip.cxx:260
const TVirtualPad * fPad
Definition: TGToolTip.h:42
Bool_t HandleTimer(TTimer *t)
If tool tip delay timer times out show tool tip window.
Definition: TGToolTip.cxx:285
const TGWindow * GetParent() const
Definition: TGWindow.h:84
virtual void RaiseWindow()
raise window
Definition: TGWindow.cxx:198
void Emit(const char *signal, const T &arg)
Activate signal with single parameter.
Definition: TQObject.h:164
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition: TSystem.cxx:469
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
void Reset()
Reset the timer.
Definition: TTimer.cxx:157
void SetTime(Long_t milliSec)
Definition: TTimer.h:90
virtual Bool_t Notify()
Notify when timer times out.
Definition: TTimer.cxx:143
void Remove()
Definition: TTimer.h:85
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:51
virtual Int_t YtoAbsPixel(Double_t y) const =0
virtual Double_t GetX2() const =0
virtual Int_t XtoAbsPixel(Double_t x) const =0
virtual Double_t GetY1() const =0
virtual Int_t GetCanvasID() const =0
virtual Double_t GetX1() const =0
TText * text
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
static constexpr double ms
Bool_t fOverrideRedirect
Definition: GuiTypes.h:106