Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/** \class TGToolTip
25 \ingroup guiwidgets
26
27A tooltip can be a one or multiple lines help text that is displayed
28in a window when the mouse cursor overs a widget, without clicking
29it. A small box appears with supplementary information regarding the
30item being hovered over.
31
32A multiline tooltip can be created by inserting a new-line character
33`\n` in the tooltip string. For example:
34
35```
36fButton->SetToolTipText("Go to the ROOT page\n (http://root.cern.ch)
37```
38
39*/
40
41
42#include "TGToolTip.h"
43#include "TGLabel.h"
44#include "TGResourcePool.h"
45#include "TTimer.h"
46#include "TSystem.h"
47#include "TVirtualPad.h"
48#include "TBox.h"
49#include "TVirtualX.h"
50
51
53
54////////////////////////////////////////////////////////////////////////////////
55
56class TTipDelayTimer : public TTimer {
57private:
58 TGToolTip *fTip; // tooltip
59public:
60 TTipDelayTimer(TGToolTip *tip, Long_t ms) : TTimer(ms, kTRUE) { fTip = tip; }
61 Bool_t Notify() override;
62};
63
64////////////////////////////////////////////////////////////////////////////////
65/// Notify when timer times out and reset the timer.
66
68{
69 fTip->HandleTimer(0);
70 Reset();
71 return kFALSE;
72}
73
74
75////////////////////////////////////////////////////////////////////////////////
76/// Create a tool tip. P is the tool tips parent window (normally
77/// fClient->GetRoot(), f is the frame to which the tool tip is associated,
78/// text is the tool tip one-liner and delayms is the delay in ms before
79/// the tool tip is shown.
80
81TGToolTip::TGToolTip(const TGWindow *p, const TGFrame *f, const char *text,
82 Long_t delayms)
84{
87 attr.fOverrideRedirect = kTRUE;
88 attr.fSaveUnder = kTRUE;
89
90 gVirtualX->ChangeWindowAttributes(fId, &attr);
92
93 fLabel = new TGLabel(this, text);
96
98 2, 3, 0, 0));
101
102 fWindow = f;
103 fPad = 0;
104 fBox = 0;
105 fX = fY = -1;
106 fDelay = new TTipDelayTimer(this, delayms);
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Create a tool tip. P is the tool tips parent window (normally
111/// fClient->GetRoot(), box is the area to which the tool tip is associated,
112/// text is the tool tip one-liner and delayms is the delay in ms before
113/// the tool tip is shown. When using this ctor with the box argument
114/// you have to use Reset(const TVirtualPad *parent).
115
116TGToolTip::TGToolTip(const TGWindow *p, const TBox *box, const char *text,
117 Long_t delayms)
119{
122 attr.fOverrideRedirect = kTRUE;
123 attr.fSaveUnder = kTRUE;
124
125 gVirtualX->ChangeWindowAttributes(fId, &attr);
127
128 fLabel = new TGLabel(this, text);
131
133 2, 3, 0, 0));
136
137 fWindow = 0;
138 fPad = 0;
139 fBox = box;
140 fDelay = new TTipDelayTimer(this, delayms);
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Create a tool tip in the parent window gClient->GetRoot(),
145/// box is the area to which the tool tip is associated,
146/// text is the tool tip one-liner and delayms is the delay in ms before
147/// the tool tip is shown. When using this ctor with the box argument
148/// you have to use Reset(const TVirtualPad *parent).
149
150TGToolTip::TGToolTip(const TBox *box, const char *text,Long_t delayms)
152{
155 attr.fOverrideRedirect = kTRUE;
156 attr.fSaveUnder = kTRUE;
157
158 gVirtualX->ChangeWindowAttributes(fId, &attr);
160
161 fLabel = new TGLabel(this, text);
164
166 2, 3, 0, 0));
169
170 fWindow = 0;
171 fPad = 0;
172 fBox = box;
173 fDelay = new TTipDelayTimer(this, delayms);
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Create a tool tip on global coordinates x, y.
178/// text is the tool tip one-liner and delayms is the delay in ms before
179/// the tool tip is shown.
180
182 : TGCompositeFrame(gClient->GetDefaultRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
183{
186 attr.fOverrideRedirect = kTRUE;
187 attr.fSaveUnder = kTRUE;
188
189 gVirtualX->ChangeWindowAttributes(fId, &attr);
191
192 fLabel = new TGLabel(this, text);
195
197 2, 3, 0, 0));
200
201 fWindow = 0;
202 fPad = 0;
203 fBox = 0;
204 fX = x;
205 fY = y;
206 fDelay = new TTipDelayTimer(this, delayms);
207}
208
209////////////////////////////////////////////////////////////////////////////////
210/// Delete a tool tip object.
211
213{
214 delete fDelay;
215 delete fLabel;
216 delete fL1;
217}
218
219////////////////////////////////////////////////////////////////////////////////
220/// Draw border of tool tip window.
221
223{
224 gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, fWidth-2, 0);
225 gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, 0, fHeight-2);
226 gVirtualX->DrawLine(fId, GetBlackGC()(), 0, fHeight-1, fWidth-1, fHeight-1);
227 gVirtualX->DrawLine(fId, GetBlackGC()(), fWidth-1, fHeight-1, fWidth-1, 0);
228}
229
230////////////////////////////////////////////////////////////////////////////////
231/// Show tool tip window.
232
234{
235 Move(x, y);
236 MapWindow();
237 RaiseWindow();
238
239 Longptr_t args[2];
240 args[0] = x;
241 args[1] = y;
242
243 Emit("Show(Int_t,Int_t)", args);
244}
245
246////////////////////////////////////////////////////////////////////////////////
247/// Hide tool tip window. Use this method to hide the tool tip in a client
248/// class.
249
251{
252 UnmapWindow();
253
254 fDelay->Remove();
255
256 Emit("Hide()");
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Reset tool tip popup delay timer. Use this method to activate tool tip
261/// in a client class.
262
264{
265 fDelay->Reset();
267
268 Emit("Reset()");
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Reset tool tip popup delay timer. Use this method to activate tool tip
273/// in a client class. Use this method for graphics objects drawn in a
274/// TCanvas, also the tool tip must have been created with the ctor
275/// taking the TBox as argument.
276
277void TGToolTip::Reset(const TVirtualPad *parent)
278{
279 fPad = parent;
280
281 fDelay->Reset();
283}
284
285////////////////////////////////////////////////////////////////////////////////
286/// If tool tip delay timer times out show tool tip window.
287
289{
290 Int_t x = 0, y = 0, px1 = 0, px2 = 0, py1 = 0;
291 Window_t wtarget;
292
293 if (fWindow) {
294 gVirtualX->TranslateCoordinates(fWindow->GetId(), GetParent()->GetId(),
295 fX == -1 ? Int_t(fWindow->GetWidth() >> 1) : fX,
296 fY == -1 ? Int_t(fWindow->GetHeight()) : fY,
297 x, y, wtarget);
298 } else if(fPad) {
299 if (fBox) {
300 px1 = fPad->XtoAbsPixel(fBox->GetX1());
301 px2 = fPad->XtoAbsPixel(fBox->GetX2());
302 py1 = fPad->YtoAbsPixel(fBox->GetY1());
303 // py2 = fPad->YtoAbsPixel(fBox->GetY2());
304 } else {
305 px1 = fPad->XtoAbsPixel(fPad->GetX1());
306 px2 = fPad->XtoAbsPixel(fPad->GetX2());
307 py1 = fPad->YtoAbsPixel(fPad->GetY1());
308 // py2 = fPad->YtoAbsPixel(fPad->GetY2());
309 }
310 gVirtualX->TranslateCoordinates(gVirtualX->GetWindowID(fPad->GetCanvasID()),
311 GetParent()->GetId(),
312 px1 + ((px2-px1) >> 1), py1,
313 x, y, wtarget);
314 } else {
315 x = fX;
316 y = fY;
317 }
318
319 Int_t move = 0;
320 Window_t dum1, dum2;
321 UInt_t mask = 0;
322 Int_t mx, my;
323 UInt_t screenW = fClient->GetDisplayWidth();
324 UInt_t screenH = fClient->GetDisplayHeight();
325
326 gVirtualX->QueryPointer(gVirtualX->GetDefaultRootWindow(),
327 dum1, dum2, mx, my, mx, my, mask);
328
331
332 // don't allow tooltip text lines longer than half the screen size
333 if (fWidth > (screenW/2))
334 fLabel->SetWrapLength((screenW/2)-15);
336
337 if (x + (Int_t)fWidth > (Int_t)screenW) {
338 x = screenW - fWidth;
339 move += 1;
340 }
341
342 if (y+4 + GetHeight() > screenH) {
343 y = screenH - (fHeight + 25);
344 move += 2;
345 }
346
347 // check if the mouse is inside the tooltip (may happen after
348 // adjusting the position when out of screen) and place the tooltip
349 // on the other side of the mouse pointer
351 if (rect.Contains(mx, my)) {
352 if (move == 1) { // left
353 if (fWidth+15 < (UInt_t)mx)
354 x = mx - fWidth - 15;
355 else if (my + fHeight+15 < screenH)
356 y = my + 15;
357 else if (fHeight+15 < (UInt_t)my)
358 y = my - fHeight - 15;
359 }
360 else if (move == 2) { // up
361 if (mx + fWidth+15 < screenW)
362 x = mx + 15;
363 else if (fHeight+15 < (UInt_t)my)
364 y = my - fHeight - 15;
365 else if (fWidth+15 < (UInt_t)mx)
366 x = mx - fWidth - 15;
367 }
368 else { // up & left, right, down, ...
369 if (my + fHeight+15 < screenH)
370 y = my + 15;
371 else if (mx + fWidth+15 < screenW)
372 x = mx + 15;
373 else if (fWidth+15 < (UInt_t)mx)
374 x = mx - fWidth - 15;
375 else if (fHeight+15 < (UInt_t)my)
376 y = my - fHeight - 15;
377 }
378 }
379
380 Show(x, y+4);
381
382 fDelay->Remove();
383
384 return kTRUE;
385}
386
387////////////////////////////////////////////////////////////////////////////////
388/// Set new tool tip text.
389
390void TGToolTip::SetText(const char *new_text)
391{
392 fLabel->SetText(new TGString(new_text));
394}
395
396////////////////////////////////////////////////////////////////////////////////
397/// Set delay in milliseconds.
398
400{
401 fDelay->SetTime(delayms);
402}
403
404////////////////////////////////////////////////////////////////////////////////
405/// Set popup position within specified frame (as specified in the ctor).
406/// To get back default behaviour (in the middle just below the designated
407/// frame) set position to -1,-1.
408
410{
411 fX = x;
412 fY = y;
413
414 if (fX < -1)
415 fX = 0;
416 if (fY < -1)
417 fY = 0;
418
419 if (fWindow) {
420 if (fX > (Int_t) fWindow->GetWidth())
421 fX = fWindow->GetWidth();
422 if (fY > (Int_t) fWindow->GetHeight())
423 fY = fWindow->GetHeight();
424 }
425}
426
427////////////////////////////////////////////////////////////////////////////////
428/// Get the tool tip text.
429
431{
432 return fLabel->GetText();
433
434}
const Mask_t kWAOverrideRedirect
Definition GuiTypes.h:149
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
const Mask_t kWASaveUnder
Definition GuiTypes.h:150
@ kRaisedFrame
Definition GuiTypes.h:384
@ kTempFrame
Definition GuiTypes.h:393
@ kHorizontalFrame
Definition GuiTypes.h:382
#define f(i)
Definition RSha256.hxx:104
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
long Longptr_t
Definition RtypesCore.h:82
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
#define gClient
Definition TGClient.h:156
@ kLHintsLeft
Definition TGLayout.h:24
@ kLHintsTop
Definition TGLayout.h:27
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t rect
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
Option_t Option_t TPoint TPoint const char text
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char mx
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
#define gVirtualX
Definition TVirtualX.h:337
Create a Box.
Definition TBox.h:22
Double_t GetX1() const
Definition TBox.h:51
Double_t GetX2() const
Definition TBox.h:52
Double_t GetY1() const
Definition TBox.h:53
const TGResourcePool * GetResourcePool() const
Definition TGClient.h:124
UInt_t GetDisplayHeight() const
Get display height.
Definition TGClient.cxx:275
UInt_t GetDisplayWidth() const
Get display width.
Definition TGClient.cxx:262
The base class for composite widgets (menu bars, list boxes, etc.).
Definition TGFrame.h:287
TGDimension GetDefaultSize() const override
std::cout << fWidth << "x" << fHeight << std::endl;
Definition TGFrame.h:316
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1164
A subclasses of TGWindow, and is used as base class for some simple widgets (buttons,...
Definition TGFrame.h:80
static const TGGC & GetBlackGC()
Get black graphics context.
Definition TGFrame.cxx:735
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:605
UInt_t fHeight
frame height
Definition TGFrame.h:88
void SetBackgroundColor(Pixel_t back) override
Set background color (override from TGWindow base class).
Definition TGFrame.cxx:312
void MapWindow() override
map window
Definition TGFrame.h:204
void Move(Int_t x, Int_t y) override
Move frame.
Definition TGFrame.cxx:593
void UnmapWindow() override
unmap window
Definition TGFrame.h:206
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition TGFrame.cxx:765
UInt_t fWidth
frame width
Definition TGFrame.h:87
UInt_t GetHeight() const
Definition TGFrame.h:225
UInt_t GetWidth() const
Definition TGFrame.h:224
This class handles GUI labels.
Definition TGLabel.h:24
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition TGLabel.cxx:362
virtual void SetText(TGString *newText)
Set new text in label.
Definition TGLabel.cxx:180
void SetWrapLength(Int_t wl)
Definition TGLabel.h:95
const TGString * GetText() const
Definition TGLabel.h:74
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
TGClient * fClient
Connection to display server.
Definition TGObject.h:25
Handle_t GetId() const
Definition TGObject.h:41
Handle_t fId
X11/Win32 Window identifier.
Definition TGObject.h:24
Pixel_t GetTipBgndColor() const
Pixel_t GetTipFgndColor() const
TGString wraps a TString and adds some graphics routines like drawing, size of string on screen depen...
Definition TGString.h:20
A tooltip can be a one or multiple lines help text that is displayed in a window when the mouse curso...
Definition TGToolTip.h:24
void Show(Int_t x, Int_t y)
Show tool tip window.
void SetDelay(Long_t delayms)
Set delay in milliseconds.
Int_t fX
X position in fWindow where to popup.
Definition TGToolTip.h:33
Int_t fY
Y position in fWindow where to popup.
Definition TGToolTip.h:34
void Hide()
Hide tool tip window.
const TGFrame * fWindow
frame to which tool tip is associated
Definition TGToolTip.h:30
void DrawBorder() override
Draw border of tool tip window.
void SetPosition(Int_t x, Int_t y)
Set popup position within specified frame (as specified in the ctor).
TTimer * fDelay
popup delay timer
Definition TGToolTip.h:29
TGLayoutHints * fL1
layout used to place text in frame
Definition TGToolTip.h:28
Bool_t HandleTimer(TTimer *t) override
If tool tip delay timer times out show tool tip window.
const TBox * fBox
box in pad to which tooltip is associated
Definition TGToolTip.h:32
const TGString * GetText() const
Get the tool tip text.
TGLabel * fLabel
help text
Definition TGToolTip.h:27
void SetText(const char *new_text)
Set new tool tip text.
void Reset()
Reset tool tip popup delay timer.
TGToolTip(const TGToolTip &)=delete
const TVirtualPad * fPad
pad to which tooltip is associated
Definition TGToolTip.h:31
~TGToolTip() override
Delete a tool tip object.
ROOT GUI Window base class.
Definition TGWindow.h:23
const TGWindow * GetParent() const
Definition TGWindow.h:83
virtual void RaiseWindow()
raise window
Definition TGWindow.cxx:208
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:471
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
void Reset()
Reset the timer.
Definition TTimer.cxx:159
void SetTime(Long_t milliSec)
Definition TTimer.h:91
void Remove() override
Definition TTimer.h:86
TGToolTip * fTip
Definition TGToolTip.cxx:58
Bool_t Notify() override
Notify when timer times out and reset the timer.
Definition TGToolTip.cxx:67
TTipDelayTimer(TGToolTip *tip, Long_t ms)
Definition TGToolTip.cxx:60
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
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
Attributes that can be used when creating or changing a window.
Definition GuiTypes.h:93