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
52
53////////////////////////////////////////////////////////////////////////////////
54
55class TTipDelayTimer : public TTimer {
56private:
57 TGToolTip *fTip; // tooltip
58public:
60 Bool_t Notify() override;
61};
62
63////////////////////////////////////////////////////////////////////////////////
64/// Notify when timer times out and reset the timer.
65
67{
68 fTip->HandleTimer(0);
69 Reset();
70 return kFALSE;
71}
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// Create a tool tip. P is the tool tips parent window (normally
76/// fClient->GetRoot(), f is the frame to which the tool tip is associated,
77/// text is the tool tip one-liner and delayms is the delay in ms before
78/// the tool tip is shown.
79
80TGToolTip::TGToolTip(const TGWindow *p, const TGFrame *f, const char *text,
83{
86 attr.fOverrideRedirect = kTRUE;
87 attr.fSaveUnder = kTRUE;
88
89 gVirtualX->ChangeWindowAttributes(fId, &attr);
90 gVirtualX->SetWindowHint(fId, TVirtualX::kHintTooltip);
91 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
92
93 fLabel = new TGLabel(this, text);
94 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
95 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
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,
119{
122 attr.fOverrideRedirect = kTRUE;
123 attr.fSaveUnder = kTRUE;
124
125 gVirtualX->ChangeWindowAttributes(fId, &attr);
126 gVirtualX->SetWindowHint(fId, TVirtualX::kHintTooltip);
127 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
128
129 fLabel = new TGLabel(this, text);
130 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
131 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
132
134 2, 3, 0, 0));
137
138 fWindow = 0;
139 fPad = 0;
140 fBox = box;
141 fDelay = new TTipDelayTimer(this, delayms);
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Create a tool tip in the parent window gClient->GetRoot(),
146/// box is the area to which the tool tip is associated,
147/// text is the tool tip one-liner and delayms is the delay in ms before
148/// the tool tip is shown. When using this ctor with the box argument
149/// you have to use Reset(const TVirtualPad *parent).
150
153{
156 attr.fOverrideRedirect = kTRUE;
157 attr.fSaveUnder = kTRUE;
158
159 gVirtualX->ChangeWindowAttributes(fId, &attr);
160 gVirtualX->SetWindowHint(fId, TVirtualX::kHintTooltip);
161 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
162
163 fLabel = new TGLabel(this, text);
164 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
165 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
166
168 2, 3, 0, 0));
171
172 fWindow = 0;
173 fPad = 0;
174 fBox = box;
175 fDelay = new TTipDelayTimer(this, delayms);
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// Create a tool tip on global coordinates x, y.
180/// text is the tool tip one-liner and delayms is the delay in ms before
181/// the tool tip is shown.
182
184 : TGCompositeFrame(gClient->GetDefaultRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
185{
188 attr.fOverrideRedirect = kTRUE;
189 attr.fSaveUnder = kTRUE;
190
191 gVirtualX->ChangeWindowAttributes(fId, &attr);
192 gVirtualX->SetWindowHint(fId, TVirtualX::kHintTooltip);
193 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
194
195 fLabel = new TGLabel(this, text);
196 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
197 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
198
200 2, 3, 0, 0));
203
204 fWindow = 0;
205 fPad = 0;
206 fBox = 0;
207 fX = x;
208 fY = y;
209 fDelay = new TTipDelayTimer(this, delayms);
210}
211
212////////////////////////////////////////////////////////////////////////////////
213/// Delete a tool tip object.
214
216{
217 delete fDelay;
218 delete fLabel;
219 delete fL1;
220}
221
222////////////////////////////////////////////////////////////////////////////////
223/// Draw border of tool tip window.
224
226{
227 gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, fWidth-2, 0);
228 gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, 0, fHeight-2);
229 gVirtualX->DrawLine(fId, GetBlackGC()(), 0, fHeight-1, fWidth-1, fHeight-1);
230 gVirtualX->DrawLine(fId, GetBlackGC()(), fWidth-1, fHeight-1, fWidth-1, 0);
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Show tool tip window.
235
237{
238 Move(x, y);
239 MapWindow();
240 RaiseWindow();
241
242 Longptr_t args[2];
243 args[0] = x;
244 args[1] = y;
245
246 Emit("Show(Int_t,Int_t)", args);
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Hide tool tip window. Use this method to hide the tool tip in a client
251/// class.
252
254{
255 UnmapWindow();
256
257 fDelay->Remove();
258
259 Emit("Hide()");
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Reset tool tip popup delay timer. Use this method to activate tool tip
264/// in a client class.
265
267{
268 fDelay->Reset();
270
271 Emit("Reset()");
272}
273
274////////////////////////////////////////////////////////////////////////////////
275/// Reset tool tip popup delay timer. Use this method to activate tool tip
276/// in a client class. Use this method for graphics objects drawn in a
277/// TCanvas, also the tool tip must have been created with the ctor
278/// taking the TBox as argument.
279
280void TGToolTip::Reset(const TVirtualPad *parent)
281{
282 fPad = parent;
283
284 fDelay->Reset();
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// If tool tip delay timer times out show tool tip window.
290
292{
293 Int_t x = 0, y = 0, px1 = 0, px2 = 0, py1 = 0;
295
296 if (fWindow) {
297 gVirtualX->TranslateCoordinates(fWindow->GetId(), GetParent()->GetId(),
298 fX == -1 ? Int_t(fWindow->GetWidth() >> 1) : fX,
299 fY == -1 ? Int_t(fWindow->GetHeight()) : fY,
300 x, y, wtarget);
301 } else if(fPad) {
302 if (fBox) {
303 px1 = fPad->XtoAbsPixel(fBox->GetX1());
304 px2 = fPad->XtoAbsPixel(fBox->GetX2());
305 py1 = fPad->YtoAbsPixel(fBox->GetY1());
306 // py2 = fPad->YtoAbsPixel(fBox->GetY2());
307 } else {
308 px1 = fPad->XtoAbsPixel(fPad->GetX1());
309 px2 = fPad->XtoAbsPixel(fPad->GetX2());
310 py1 = fPad->YtoAbsPixel(fPad->GetY1());
311 // py2 = fPad->YtoAbsPixel(fPad->GetY2());
312 }
313 gVirtualX->TranslateCoordinates(gVirtualX->GetWindowID(fPad->GetCanvasID()),
314 GetParent()->GetId(),
315 px1 + ((px2-px1) >> 1), py1,
316 x, y, wtarget);
317 } else {
318 x = fX;
319 y = fY;
320 }
321
322 Int_t move = 0;
324 UInt_t mask = 0;
325 Int_t mx, my;
328
329 gVirtualX->QueryPointer(gVirtualX->GetDefaultRootWindow(),
330 dum1, dum2, mx, my, mx, my, mask);
331
334
335 // don't allow tooltip text lines longer than half the screen size
336 if (fWidth > (screenW/2))
339
340 if (x + (Int_t)fWidth > (Int_t)screenW) {
341 x = screenW - fWidth;
342 move += 1;
343 }
344
345 if (y+4 + GetHeight() > screenH) {
346 y = screenH - (fHeight + 25);
347 move += 2;
348 }
349
350 // check if the mouse is inside the tooltip (may happen after
351 // adjusting the position when out of screen) and place the tooltip
352 // on the other side of the mouse pointer
354 if (rect.Contains(mx, my)) {
355 if (move == 1) { // left
356 if (fWidth+15 < (UInt_t)mx)
357 x = mx - fWidth - 15;
358 else if (my + fHeight+15 < screenH)
359 y = my + 15;
360 else if (fHeight+15 < (UInt_t)my)
361 y = my - fHeight - 15;
362 }
363 else if (move == 2) { // up
364 if (mx + fWidth+15 < screenW)
365 x = mx + 15;
366 else if (fHeight+15 < (UInt_t)my)
367 y = my - fHeight - 15;
368 else if (fWidth+15 < (UInt_t)mx)
369 x = mx - fWidth - 15;
370 }
371 else { // up & left, right, down, ...
372 if (my + fHeight+15 < screenH)
373 y = my + 15;
374 else if (mx + fWidth+15 < screenW)
375 x = mx + 15;
376 else if (fWidth+15 < (UInt_t)mx)
377 x = mx - fWidth - 15;
378 else if (fHeight+15 < (UInt_t)my)
379 y = my - fHeight - 15;
380 }
381 }
382
383 Show(x, y+4);
384
385 fDelay->Remove();
386
387 return kTRUE;
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Set new tool tip text.
392
394{
397}
398
399////////////////////////////////////////////////////////////////////////////////
400/// Set delay in milliseconds.
401
406
407////////////////////////////////////////////////////////////////////////////////
408/// Set popup position within specified frame (as specified in the ctor).
409/// To get back default behaviour (in the middle just below the designated
410/// frame) set position to -1,-1.
411
413{
414 fX = x;
415 fY = y;
416
417 if (fX < -1)
418 fX = 0;
419 if (fY < -1)
420 fY = 0;
421
422 if (fWindow) {
423 if (fX > (Int_t) fWindow->GetWidth())
424 fX = fWindow->GetWidth();
425 if (fY > (Int_t) fWindow->GetHeight())
426 fY = fWindow->GetHeight();
427 }
428}
429
430////////////////////////////////////////////////////////////////////////////////
431/// Get the tool tip text.
432
434{
435 return fLabel->GetText();
436
437}
const Mask_t kWAOverrideRedirect
Definition GuiTypes.h:150
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
const Mask_t kWASaveUnder
Definition GuiTypes.h:151
@ kRaisedFrame
Definition GuiTypes.h:385
@ kTempFrame
Definition GuiTypes.h:394
@ kHorizontalFrame
Definition GuiTypes.h:383
#define f(i)
Definition RSha256.hxx:104
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:89
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gClient
Definition TGClient.h:157
@ 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:582
#define gVirtualX
Definition TVirtualX.h:377
Create a Box.
Definition TBox.h:22
const TGResourcePool * GetResourcePool() const
Definition TGClient.h:124
UInt_t GetDisplayHeight() const
Get display height.
Definition TGClient.cxx:274
UInt_t GetDisplayWidth() const
Get display width.
Definition TGClient.cxx:261
The base class for composite widgets (menu bars, list boxes, etc.).
Definition TGFrame.h:289
TGDimension GetDefaultSize() const override
std::cout << fWidth << "x" << fHeight << std::endl;
Definition TGFrame.h:318
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1110
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1157
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:728
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:598
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:305
void MapWindow() override
map window
Definition TGFrame.h:206
void Move(Int_t x, Int_t y) override
Move frame.
Definition TGFrame.cxx:586
void UnmapWindow() override
unmap window
Definition TGFrame.h:208
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition TGFrame.cxx:758
UInt_t fWidth
frame width
Definition TGFrame.h:87
UInt_t GetHeight() const
Definition TGFrame.h:227
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:361
virtual void SetText(TGString *newText)
Set new text in label.
Definition TGLabel.cxx:179
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
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:206
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:473
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
void Reset()
Reset the timer.
Definition TTimer.cxx:162
void SetTime(Long_t milliSec)
Definition TTimer.h:91
void Remove() override
Definition TTimer.h:86
TGToolTip * fTip
Definition TGToolTip.cxx:57
Bool_t Notify() override
Notify when timer times out and reset the timer.
Definition TGToolTip.cxx:66
TTipDelayTimer(TGToolTip *tip, Long_t ms)
Definition TGToolTip.cxx:59
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
@ kHintTooltip
Definition TVirtualX.h:53
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:94