Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
47 
48 ClassImp(TGToolTip);
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 
52 class TTipDelayTimer : public TTimer {
53 private:
54  TGToolTip *fTip; // tooltip
55 public:
56  TTipDelayTimer(TGToolTip *tip, Long_t ms) : TTimer(ms, kTRUE) { fTip = tip; }
57  Bool_t Notify();
58 };
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Notify when timer times out and reset the timer.
62 
63 Bool_t TTipDelayTimer::Notify()
64 {
65  fTip->HandleTimer(0);
66  Reset();
67  return kFALSE;
68 }
69 
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Create a tool tip. P is the tool tips parent window (normally
73 /// fClient->GetRoot(), f is the frame to which the tool tip is associated,
74 /// text is the tool tip one-liner and delayms is the delay in ms before
75 /// the tool tip is shown.
76 
77 TGToolTip::TGToolTip(const TGWindow *p, const TGFrame *f, const char *text,
78  Long_t delayms)
79  : TGCompositeFrame(p, 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
80 {
81  SetWindowAttributes_t attr;
82  attr.fMask = kWAOverrideRedirect | kWASaveUnder;
83  attr.fOverrideRedirect = kTRUE;
84  attr.fSaveUnder = kTRUE;
85 
86  gVirtualX->ChangeWindowAttributes(fId, &attr);
87  SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
88 
89  fLabel = new TGLabel(this, text);
90  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
91  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
92 
93  AddFrame(fLabel, fL1 = new TGLayoutHints(kLHintsLeft | kLHintsTop,
94  2, 3, 0, 0));
95  MapSubwindows();
96  Resize(GetDefaultSize());
97 
98  fWindow = f;
99  fPad = 0;
100  fBox = 0;
101  fX = fY = -1;
102  fDelay = new TTipDelayTimer(this, delayms);
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Create a tool tip. P is the tool tips parent window (normally
107 /// fClient->GetRoot(), box is the area to which the tool tip is associated,
108 /// text is the tool tip one-liner and delayms is the delay in ms before
109 /// the tool tip is shown. When using this ctor with the box argument
110 /// you have to use Reset(const TVirtualPad *parent).
111 
112 TGToolTip::TGToolTip(const TGWindow *p, const TBox *box, const char *text,
113  Long_t delayms)
114  : TGCompositeFrame(p, 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
115 {
116  SetWindowAttributes_t attr;
117  attr.fMask = kWAOverrideRedirect | kWASaveUnder;
118  attr.fOverrideRedirect = kTRUE;
119  attr.fSaveUnder = kTRUE;
120 
121  gVirtualX->ChangeWindowAttributes(fId, &attr);
122  SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
123 
124  fLabel = new TGLabel(this, text);
125  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
126  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
127 
128  AddFrame(fLabel, fL1 = new TGLayoutHints(kLHintsLeft | kLHintsTop,
129  2, 3, 0, 0));
130  MapSubwindows();
131  Resize(GetDefaultSize());
132 
133  fWindow = 0;
134  fPad = 0;
135  fBox = box;
136  fDelay = new TTipDelayTimer(this, delayms);
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Create a tool tip in the parent window gClient->GetRoot(),
141 /// box is the area to which the tool tip is associated,
142 /// text is the tool tip one-liner and delayms is the delay in ms before
143 /// the tool tip is shown. When using this ctor with the box argument
144 /// you have to use Reset(const TVirtualPad *parent).
145 
146 TGToolTip::TGToolTip(const TBox *box, const char *text,Long_t delayms)
147  : TGCompositeFrame(gClient->GetRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
148 {
149  SetWindowAttributes_t attr;
150  attr.fMask = kWAOverrideRedirect | kWASaveUnder;
151  attr.fOverrideRedirect = kTRUE;
152  attr.fSaveUnder = kTRUE;
153 
154  gVirtualX->ChangeWindowAttributes(fId, &attr);
155  SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
156 
157  fLabel = new TGLabel(this, text);
158  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
159  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
160 
161  AddFrame(fLabel, fL1 = new TGLayoutHints(kLHintsLeft | kLHintsTop,
162  2, 3, 0, 0));
163  MapSubwindows();
164  Resize(GetDefaultSize());
165 
166  fWindow = 0;
167  fPad = 0;
168  fBox = box;
169  fDelay = new TTipDelayTimer(this, delayms);
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Create a tool tip on global coordinates x, y.
174 /// text is the tool tip one-liner and delayms is the delay in ms before
175 /// the tool tip is shown.
176 
177 TGToolTip::TGToolTip(Int_t x, Int_t y, const char *text, Long_t delayms)
178  : TGCompositeFrame(gClient->GetDefaultRoot(), 10, 10, kTempFrame | kHorizontalFrame | kRaisedFrame)
179 {
180  SetWindowAttributes_t attr;
181  attr.fMask = kWAOverrideRedirect | kWASaveUnder;
182  attr.fOverrideRedirect = kTRUE;
183  attr.fSaveUnder = kTRUE;
184 
185  gVirtualX->ChangeWindowAttributes(fId, &attr);
186  SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
187 
188  fLabel = new TGLabel(this, text);
189  fLabel->SetBackgroundColor(fClient->GetResourcePool()->GetTipBgndColor());
190  fLabel->SetTextColor(fClient->GetResourcePool()->GetTipFgndColor());
191 
192  AddFrame(fLabel, fL1 = new TGLayoutHints(kLHintsLeft | kLHintsTop,
193  2, 3, 0, 0));
194  MapSubwindows();
195  Resize(GetDefaultSize());
196 
197  fWindow = 0;
198  fPad = 0;
199  fBox = 0;
200  fX = x;
201  fY = y;
202  fDelay = new TTipDelayTimer(this, delayms);
203 }
204 
205 ////////////////////////////////////////////////////////////////////////////////
206 /// Delete a tool tip object.
207 
208 TGToolTip::~TGToolTip()
209 {
210  delete fDelay;
211  delete fLabel;
212  delete fL1;
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Draw border of tool tip window.
217 
218 void TGToolTip::DrawBorder()
219 {
220  gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, fWidth-2, 0);
221  gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, 0, fHeight-2);
222  gVirtualX->DrawLine(fId, GetBlackGC()(), 0, fHeight-1, fWidth-1, fHeight-1);
223  gVirtualX->DrawLine(fId, GetBlackGC()(), fWidth-1, fHeight-1, fWidth-1, 0);
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Show tool tip window.
228 
229 void TGToolTip::Show(Int_t x, Int_t y)
230 {
231  Move(x, y);
232  MapWindow();
233  RaiseWindow();
234 
235  Long_t args[2];
236  args[0] = x;
237  args[1] = y;
238 
239  Emit("Show(Int_t,Int_t)", args);
240 }
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Hide tool tip window. Use this method to hide the tool tip in a client
244 /// class.
245 
246 void TGToolTip::Hide()
247 {
248  UnmapWindow();
249 
250  fDelay->Remove();
251 
252  Emit("Hide()");
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Reset tool tip popup delay timer. Use this method to activate tool tip
257 /// in a client class.
258 
259 void TGToolTip::Reset()
260 {
261  fDelay->Reset();
262  gSystem->AddTimer(fDelay);
263 
264  Emit("Reset()");
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Reset tool tip popup delay timer. Use this method to activate tool tip
269 /// in a client class. Use this method for graphics objects drawn in a
270 /// TCanvas, also the tool tip must have been created with the ctor
271 /// taking the TBox as argument.
272 
273 void TGToolTip::Reset(const TVirtualPad *parent)
274 {
275  fPad = parent;
276 
277  fDelay->Reset();
278  gSystem->AddTimer(fDelay);
279 }
280 
281 ////////////////////////////////////////////////////////////////////////////////
282 /// If tool tip delay timer times out show tool tip window.
283 
284 Bool_t TGToolTip::HandleTimer(TTimer *)
285 {
286  Int_t x = 0, y = 0, px1 = 0, px2 = 0, py1 = 0;
287  Window_t wtarget;
288 
289  if (fWindow) {
290  gVirtualX->TranslateCoordinates(fWindow->GetId(), GetParent()->GetId(),
291  fX == -1 ? Int_t(fWindow->GetWidth() >> 1) : fX,
292  fY == -1 ? Int_t(fWindow->GetHeight()) : fY,
293  x, y, wtarget);
294  } else if(fPad) {
295  if (fBox) {
296  px1 = fPad->XtoAbsPixel(fBox->GetX1());
297  px2 = fPad->XtoAbsPixel(fBox->GetX2());
298  py1 = fPad->YtoAbsPixel(fBox->GetY1());
299  // py2 = fPad->YtoAbsPixel(fBox->GetY2());
300  } else {
301  px1 = fPad->XtoAbsPixel(fPad->GetX1());
302  px2 = fPad->XtoAbsPixel(fPad->GetX2());
303  py1 = fPad->YtoAbsPixel(fPad->GetY1());
304  // py2 = fPad->YtoAbsPixel(fPad->GetY2());
305  }
306  gVirtualX->TranslateCoordinates(gVirtualX->GetWindowID(fPad->GetCanvasID()),
307  GetParent()->GetId(),
308  px1 + ((px2-px1) >> 1), py1,
309  x, y, wtarget);
310  } else {
311  x = fX;
312  y = fY;
313  }
314 
315  Int_t move = 0;
316  Window_t dum1, dum2;
317  UInt_t mask = 0;
318  Int_t mx, my;
319  UInt_t screenW = fClient->GetDisplayWidth();
320  UInt_t screenH = fClient->GetDisplayHeight();
321 
322  gVirtualX->QueryPointer(gVirtualX->GetDefaultRootWindow(),
323  dum1, dum2, mx, my, mx, my, mask);
324 
325  fLabel->SetWrapLength(-1);
326  Resize(GetDefaultSize());
327 
328  // don't allow tooltip text lines longer than half the screen size
329  if (fWidth > (screenW/2))
330  fLabel->SetWrapLength((screenW/2)-15);
331  Resize(GetDefaultSize());
332 
333  if (x + (Int_t)fWidth > (Int_t)screenW) {
334  x = screenW - fWidth;
335  move += 1;
336  }
337 
338  if (y+4 + GetHeight() > screenH) {
339  y = screenH - (fHeight + 25);
340  move += 2;
341  }
342 
343  // check if the mouse is inside the tooltip (may happen after
344  // adjusting the position when out of screen) and place the tooltip
345  // on the other side of the mouse pointer
346  TGRectangle rect(x, y, x+fWidth, y+fHeight);
347  if (rect.Contains(mx, my)) {
348  if (move == 1) { // left
349  if (fWidth+15 < (UInt_t)mx)
350  x = mx - fWidth - 15;
351  else if (my + fHeight+15 < screenH)
352  y = my + 15;
353  else if (fHeight+15 < (UInt_t)my)
354  y = my - fHeight - 15;
355  }
356  else if (move == 2) { // up
357  if (mx + fWidth+15 < screenW)
358  x = mx + 15;
359  else if (fHeight+15 < (UInt_t)my)
360  y = my - fHeight - 15;
361  else if (fWidth+15 < (UInt_t)mx)
362  x = mx - fWidth - 15;
363  }
364  else { // up & left, right, down, ...
365  if (my + fHeight+15 < screenH)
366  y = my + 15;
367  else if (mx + fWidth+15 < screenW)
368  x = mx + 15;
369  else if (fWidth+15 < (UInt_t)mx)
370  x = mx - fWidth - 15;
371  else if (fHeight+15 < (UInt_t)my)
372  y = my - fHeight - 15;
373  }
374  }
375 
376  Show(x, y+4);
377 
378  fDelay->Remove();
379 
380  return kTRUE;
381 }
382 
383 ////////////////////////////////////////////////////////////////////////////////
384 /// Set new tool tip text.
385 
386 void TGToolTip::SetText(const char *new_text)
387 {
388  fLabel->SetText(new TGString(new_text));
389  Resize(GetDefaultSize());
390 }
391 
392 ////////////////////////////////////////////////////////////////////////////////
393 /// Set delay in milliseconds.
394 
395 void TGToolTip::SetDelay(Long_t delayms)
396 {
397  fDelay->SetTime(delayms);
398 }
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 /// Set popup position within specified frame (as specified in the ctor).
402 /// To get back default behaviour (in the middle just below the designated
403 /// frame) set position to -1,-1.
404 
405 void TGToolTip::SetPosition(Int_t x, Int_t y)
406 {
407  fX = x;
408  fY = y;
409 
410  if (fX < -1)
411  fX = 0;
412  if (fY < -1)
413  fY = 0;
414 
415  if (fWindow) {
416  if (fX > (Int_t) fWindow->GetWidth())
417  fX = fWindow->GetWidth();
418  if (fY > (Int_t) fWindow->GetHeight())
419  fY = fWindow->GetHeight();
420  }
421 }
422 
423 ////////////////////////////////////////////////////////////////////////////////
424 /// Get the tool tip text.
425 
426 const TGString *TGToolTip::GetText() const
427 {
428  return fLabel->GetText();
429 
430 }