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 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
91
92 fLabel = new TGLabel(this, text);
93 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
94 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
95
97 2, 3, 0, 0));
100
101 fWindow = f;
102 fPad = 0;
103 fBox = 0;
104 fX = fY = -1;
105 fDelay = new TTipDelayTimer(this, delayms);
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Create a tool tip. P is the tool tips parent window (normally
110/// fClient->GetRoot(), box is the area to which the tool tip is associated,
111/// text is the tool tip one-liner and delayms is the delay in ms before
112/// the tool tip is shown. When using this ctor with the box argument
113/// you have to use Reset(const TVirtualPad *parent).
114
115TGToolTip::TGToolTip(const TGWindow *p, const TBox *box, const char *text,
118{
121 attr.fOverrideRedirect = kTRUE;
122 attr.fSaveUnder = kTRUE;
123
124 gVirtualX->ChangeWindowAttributes(fId, &attr);
125 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
126
127 fLabel = new TGLabel(this, text);
128 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
129 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
130
132 2, 3, 0, 0));
135
136 fWindow = 0;
137 fPad = 0;
138 fBox = box;
139 fDelay = new TTipDelayTimer(this, delayms);
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Create a tool tip in the parent window gClient->GetRoot(),
144/// box is the area to which the tool tip is associated,
145/// text is the tool tip one-liner and delayms is the delay in ms before
146/// the tool tip is shown. When using this ctor with the box argument
147/// you have to use Reset(const TVirtualPad *parent).
148
151{
154 attr.fOverrideRedirect = kTRUE;
155 attr.fSaveUnder = kTRUE;
156
157 gVirtualX->ChangeWindowAttributes(fId, &attr);
158 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
159
160 fLabel = new TGLabel(this, text);
161 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
162 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
163
165 2, 3, 0, 0));
168
169 fWindow = 0;
170 fPad = 0;
171 fBox = box;
172 fDelay = new TTipDelayTimer(this, delayms);
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Create a tool tip on global coordinates x, y.
177/// text is the tool tip one-liner and delayms is the delay in ms before
178/// the tool tip is shown.
179
181 : TGCompositeFrame(gClient->GetDefaultRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
182{
185 attr.fOverrideRedirect = kTRUE;
186 attr.fSaveUnder = kTRUE;
187
188 gVirtualX->ChangeWindowAttributes(fId, &attr);
189 SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
190
191 fLabel = new TGLabel(this, text);
192 fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
193 fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
194
196 2, 3, 0, 0));
199
200 fWindow = 0;
201 fPad = 0;
202 fBox = 0;
203 fX = x;
204 fY = y;
205 fDelay = new TTipDelayTimer(this, delayms);
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Delete a tool tip object.
210
212{
213 delete fDelay;
214 delete fLabel;
215 delete fL1;
216}
217
218////////////////////////////////////////////////////////////////////////////////
219/// Draw border of tool tip window.
220
222{
223 gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, fWidth-2, 0);
224 gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, 0, fHeight-2);
225 gVirtualX->DrawLine(fId, GetBlackGC()(), 0, fHeight-1, fWidth-1, fHeight-1);
226 gVirtualX->DrawLine(fId, GetBlackGC()(), fWidth-1, fHeight-1, fWidth-1, 0);
227}
228
229////////////////////////////////////////////////////////////////////////////////
230/// Show tool tip window.
231
233{
234 Move(x, y);
235 MapWindow();
236 RaiseWindow();
237
238 Longptr_t args[2];
239 args[0] = x;
240 args[1] = y;
241
242 Emit("Show(Int_t,Int_t)", args);
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// Hide tool tip window. Use this method to hide the tool tip in a client
247/// class.
248
250{
251 UnmapWindow();
252
253 fDelay->Remove();
254
255 Emit("Hide()");
256}
257
258////////////////////////////////////////////////////////////////////////////////
259/// Reset tool tip popup delay timer. Use this method to activate tool tip
260/// in a client class.
261
263{
264 fDelay->Reset();
266
267 Emit("Reset()");
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// Reset tool tip popup delay timer. Use this method to activate tool tip
272/// in a client class. Use this method for graphics objects drawn in a
273/// TCanvas, also the tool tip must have been created with the ctor
274/// taking the TBox as argument.
275
276void TGToolTip::Reset(const TVirtualPad *parent)
277{
278 fPad = parent;
279
280 fDelay->Reset();
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// If tool tip delay timer times out show tool tip window.
286
288{
289 Int_t x = 0, y = 0, px1 = 0, px2 = 0, py1 = 0;
291
292 if (fWindow) {
293 gVirtualX->TranslateCoordinates(fWindow->GetId(), GetParent()->GetId(),
294 fX == -1 ? Int_t(fWindow->GetWidth() >> 1) : fX,
295 fY == -1 ? Int_t(fWindow->GetHeight()) : fY,
296 x, y, wtarget);
297 } else if(fPad) {
298 if (fBox) {
299 px1 = fPad->XtoAbsPixel(fBox->GetX1());
300 px2 = fPad->XtoAbsPixel(fBox->GetX2());
301 py1 = fPad->YtoAbsPixel(fBox->GetY1());
302 // py2 = fPad->YtoAbsPixel(fBox->GetY2());
303 } else {
304 px1 = fPad->XtoAbsPixel(fPad->GetX1());
305 px2 = fPad->XtoAbsPixel(fPad->GetX2());
306 py1 = fPad->YtoAbsPixel(fPad->GetY1());
307 // py2 = fPad->YtoAbsPixel(fPad->GetY2());
308 }
309 gVirtualX->TranslateCoordinates(gVirtualX->GetWindowID(fPad->GetCanvasID()),
310 GetParent()->GetId(),
311 px1 + ((px2-px1) >> 1), py1,
312 x, y, wtarget);
313 } else {
314 x = fX;
315 y = fY;
316 }
317
318 Int_t move = 0;
320 UInt_t mask = 0;
321 Int_t mx, my;
324
325 gVirtualX->QueryPointer(gVirtualX->GetDefaultRootWindow(),
326 dum1, dum2, mx, my, mx, my, mask);
327
330
331 // don't allow tooltip text lines longer than half the screen size
332 if (fWidth > (screenW/2))
335
336 if (x + (Int_t)fWidth > (Int_t)screenW) {
337 x = screenW - fWidth;
338 move += 1;
339 }
340
341 if (y+4 + GetHeight() > screenH) {
342 y = screenH - (fHeight + 25);
343 move += 2;
344 }
345
346 // check if the mouse is inside the tooltip (may happen after
347 // adjusting the position when out of screen) and place the tooltip
348 // on the other side of the mouse pointer
350 if (rect.Contains(mx, my)) {
351 if (move == 1) { // left
352 if (fWidth+15 < (UInt_t)mx)
353 x = mx - fWidth - 15;
354 else if (my + fHeight+15 < screenH)
355 y = my + 15;
356 else if (fHeight+15 < (UInt_t)my)
357 y = my - fHeight - 15;
358 }
359 else if (move == 2) { // up
360 if (mx + fWidth+15 < screenW)
361 x = mx + 15;
362 else if (fHeight+15 < (UInt_t)my)
363 y = my - fHeight - 15;
364 else if (fWidth+15 < (UInt_t)mx)
365 x = mx - fWidth - 15;
366 }
367 else { // up & left, right, down, ...
368 if (my + fHeight+15 < screenH)
369 y = my + 15;
370 else if (mx + fWidth+15 < screenW)
371 x = mx + 15;
372 else if (fWidth+15 < (UInt_t)mx)
373 x = mx - fWidth - 15;
374 else if (fHeight+15 < (UInt_t)my)
375 y = my - fHeight - 15;
376 }
377 }
378
379 Show(x, y+4);
380
381 fDelay->Remove();
382
383 return kTRUE;
384}
385
386////////////////////////////////////////////////////////////////////////////////
387/// Set new tool tip text.
388
390{
393}
394
395////////////////////////////////////////////////////////////////////////////////
396/// Set delay in milliseconds.
397
402
403////////////////////////////////////////////////////////////////////////////////
404/// Set popup position within specified frame (as specified in the ctor).
405/// To get back default behaviour (in the middle just below the designated
406/// frame) set position to -1,-1.
407
409{
410 fX = x;
411 fY = y;
412
413 if (fX < -1)
414 fX = 0;
415 if (fY < -1)
416 fY = 0;
417
418 if (fWindow) {
419 if (fX > (Int_t) fWindow->GetWidth())
420 fX = fWindow->GetWidth();
421 if (fY > (Int_t) fWindow->GetHeight())
422 fY = fWindow->GetHeight();
423 }
424}
425
426////////////////////////////////////////////////////////////////////////////////
427/// Get the tool tip text.
428
430{
431 return fLabel->GetText();
432
433}
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
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:572
#define gVirtualX
Definition TVirtualX.h:337
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:1109
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1156
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:727
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:597
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:304
void MapWindow() override
map window
Definition TGFrame.h:206
void Move(Int_t x, Int_t y) override
Move frame.
Definition TGFrame.cxx:585
void UnmapWindow() override
unmap window
Definition TGFrame.h:208
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition TGFrame.cxx:757
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:469
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
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