fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Tooltip.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Tooltip source file for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2015 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include <FL/Fl_Tooltip.H>
20 #include <FL/fl_draw.H>
21 #include <FL/Fl_Menu_Window.H>
22 
23 #include <stdio.h>
24 #include <string.h> // strdup()
25 
26 float Fl_Tooltip::delay_ = 1.0f;
27 float Fl_Tooltip::hoverdelay_ = 0.2f;
29  FL_NUM_GREEN - 1,
30  FL_NUM_BLUE - 2);
34 #if FLTK_ABI_VERSION >= 10301
35 int Fl_Tooltip::margin_width_ = 3;
36 int Fl_Tooltip::margin_height_ = 3;
37 int Fl_Tooltip::wrap_width_ = 400;
38 #endif
39 
40 static const char* tip;
41 
42 // FIXME: this should be a static class variable: Fl_Tooltip::draw_symbols_
43 static const int draw_symbols_ = 1; // 1 = draw @-symbols in tooltips, 0 = no
44 
48 class Fl_TooltipBox : public Fl_Menu_Window {
49 public:
52  set_override();
54  end();
55  }
56  void draw();
57  void layout();
59  void show() {
60  if (!tip) return;
61 
63  }
64 
65  int handle(int e) {
66  if (e == FL_PUSH || e == FL_KEYDOWN) {
67  hide();
68  return 1;
69  }
70  return Fl_Menu_Window::handle(e);
71  }
72 };
73 
75 static Fl_TooltipBox *window = 0;
76 static int Y,H;
77 
78 #ifdef __APPLE__
79 // returns the unique tooltip window
80 Fl_Window *Fl_Tooltip::current_window(void)
81 {
82  return (Fl_Window*)window;
83 }
84 #endif
85 
88  int ww = Fl_Tooltip::wrap_width();
89  int hh = 0;
90  fl_measure(tip, ww, hh, draw_symbols_);
91  ww += (Fl_Tooltip::margin_width() * 2);
92  hh += (Fl_Tooltip::margin_height() * 2);
93 
94  // find position on the screen of the widget:
95  int ox = Fl::event_x_root();
96  int oy = Y + H+2;
97  for (Fl_Widget* p = Fl_Tooltip::current(); p; p = p->window()) {
98  oy += p->y();
99  }
100  int scr_x, scr_y, scr_w, scr_h;
101  Fl::screen_xywh(scr_x, scr_y, scr_w, scr_h);
102  if (ox+ww > scr_x+scr_w) ox = scr_x+scr_w - ww;
103  if (ox < scr_x) ox = scr_x;
104  if (H > 30) {
105  oy = Fl::event_y_root()+13;
106  if (oy+hh > scr_y+scr_h) oy -= 23+hh;
107  } else {
108  if (oy+hh > scr_y+scr_h) oy -= (4+hh+H);
109  }
110  if (oy < scr_y) oy = scr_y;
111 
112  resize(ox, oy, ww, hh);
113 }
114 
116  draw_box(FL_BORDER_BOX, 0, 0, w(), h(), Fl_Tooltip::color());
119  int X = Fl_Tooltip::margin_width();
121  int W = w() - (Fl_Tooltip::margin_width()*2);
122  int H = h() - (Fl_Tooltip::margin_height()*2);
124 }
125 
126 static char recent_tooltip;
127 
128 static void recent_timeout(void*) {
129 #ifdef DEBUG
130  puts("recent_timeout();");
131 #endif // DEBUG
132 
133  recent_tooltip = 0;
134 }
135 
136 static char recursion;
137 
138 // Is top level window iconified?
139 static int top_win_iconified_() {
141  if ( !w ) return 0;
142  Fl_Window *topwin = w->top_window();
143  if ( !topwin ) return 0;
144  return !topwin->visible() ? 1 : 0;
145 }
146 
147 static void tooltip_timeout(void*) {
148 #ifdef DEBUG
149  puts("tooltip_timeout();");
150 #endif // DEBUG
151 
152  if (recursion) return;
153  recursion = 1;
154  if (!top_win_iconified_()) { // no tooltip if top win iconified (STR #3157)
155  if (!tip || !*tip) {
156  if (window) window->hide();
157  } else {
158  int condition = 1;
159 #if !(defined(__APPLE__) || defined(WIN32))
160  condition = (Fl::grab() == NULL);
161 #endif
162  if ( condition ) {
163  if (!window) window = new Fl_TooltipBox;
164  // this cast bypasses the normal Fl_Window label() code:
165  ((Fl_Widget*)window)->label(tip);
166  window->layout();
167  window->redraw();
168  // printf("tooltip_timeout: Showing window %p with tooltip \"%s\"...\n",
169  // window, tip ? tip : "(null)");
170  window->show();
171  }
172  }
173  }
174 
176  recent_tooltip = 1;
177  recursion = 0;
178 }
179 
190 #ifdef DEBUG
191  printf("Fl_Tooltip::enter_(w=%p)\n", w);
192  printf(" window=%p\n", window);
193 #endif // DEBUG
194  if (w && w->as_window() && ((Fl_Window*)w)->tooltip_window()) {
195  // Fix STR #2650: if there's no better place for a tooltip window, don't move it.
196  int oldx = w->x();
197  int oldy = w->y();
198  ((Fl_TooltipBox*)w)->layout();
199  if (w->x() == oldx && w->y() == oldy) return;
200  }
201  // find the enclosing group with a tooltip:
202  Fl_Widget* tw = w;
203  for (;;) {
204  if (!tw) {exit_(0); return;}
205  if (tw == widget_) return;
206  if (tw->tooltip()) break;
207  tw = tw->parent();
208  }
209  enter_area(w, 0, 0, w->w(), w->h(), tw->tooltip());
210 }
219 #ifdef DEBUG
220  printf("Fl_Tooltip::current(w=%p)\n", w);
221 #endif // DEBUG
222 
223  exit_(0);
224  // find the enclosing group with a tooltip:
225  Fl_Widget* tw = w;
226  for (;;) {
227  if (!tw) return;
228  if (tw->tooltip()) break;
229  tw = tw->parent();
230  }
231  // act just like Fl_Tooltip::enter_() except we can remember a zero:
232  widget_ = w;
233 }
234 
235 // Hide any visible tooltip.
238 #ifdef DEBUG
239  printf("Fl_Tooltip::exit_(w=%p)\n", w);
240  printf(" widget=%p, window=%p\n", widget_, window);
241 #endif // DEBUG
242 
243  if (!widget_ || (w && w == window)) return;
244  widget_ = 0;
247  if (window && window->visible()) window->hide();
248  if (recent_tooltip) {
251  }
252 }
253 
254 // Get ready to display a tooltip. The widget and the xywh box inside
255 // it define an area the tooltip is for, this along with the current
256 // mouse position places the tooltip (the mouse is assumed to point
257 // inside or near the box).
269 void Fl_Tooltip::enter_area(Fl_Widget* wid, int x,int y,int w,int h, const char* t)
270 {
271  (void)x;
272  (void)w;
273 
274 #ifdef DEBUG
275  printf("Fl_Tooltip::enter_area(wid=%p, x=%d, y=%d, w=%d, h=%d, t=\"%s\")\n",
276  wid, x, y, w, h, t ? t : "(null)");
277  printf(" recursion=%d, window=%p\n", recursion, window);
278 #endif // DEBUG
279 
280  if (recursion) return;
281  if (!t || !*t || !enabled()) {
282  exit_(0);
283  return;
284  }
285  // do nothing if it is the same:
286  if (wid==widget_ /*&& x==X && y==Y && w==W && h==H*/ && t==tip) return;
289  // remember it:
290  widget_ = wid; Y = y; H = h; tip = t;
291  // popup the tooltip immediately if it was recently up:
292  if (recent_tooltip) {
293  if (window) window->hide();
295  } else if (Fl_Tooltip::delay() < .1) {
296 #ifdef WIN32
297  // possible fix for the Windows titlebar, it seems to want the
298  // window to be destroyed, moving it messes up the parenting:
299  if (window && window->visible()) window->hide();
300 #endif // WIN32
301  tooltip_timeout(0);
302  } else {
303  if (window && window->visible()) window->hide();
305  }
306 
307 #ifdef DEBUG
308  printf(" tip=\"%s\", window->shown()=%d\n", tip ? tip : "(null)",
309  window ? window->shown() : 0);
310 #endif // DEBUG
311 }
312 
314  static char beenhere = 0;
315  if (!beenhere) {
316  beenhere = 1;
319  }
320 }
321 
339 void Fl_Widget::tooltip(const char *text) {
341  if (flags() & COPIED_TOOLTIP) {
342  // reassigning a copied tooltip remains the same copied tooltip
343  if (tooltip_ == text) return;
344  free((void*)(tooltip_)); // free maintained copy
345  clear_flag(COPIED_TOOLTIP); // disable copy flag (WE don't make copies)
346  }
347  tooltip_ = text;
348 }
349 
365 void Fl_Widget::copy_tooltip(const char *text) {
367  if (flags() & COPIED_TOOLTIP) free((void *)(tooltip_));
368  if (text) {
370  tooltip_ = strdup(text);
371  } else {
373  tooltip_ = (char *)0;
374  }
375 }
376 
377 //
378 // End of "$Id$".
379 //
Fl_TooltipBox::handle
int handle(int e)
Definition: Fl_Tooltip.cxx:65
fl_font
void fl_font(Fl_Font face, Fl_Fontsize fsize)
Definition: fl_draw.H:509
FL_ALIGN_LEFT
const Fl_Align FL_ALIGN_LEFT
Definition: Enumerations.H:839
Fl_Tooltip::wrap_width
static int wrap_width()
Definition: Fl_Tooltip.H:97
Fl_Tooltip::enabled
static int enabled()
Definition: Fl_Tooltip.H:53
Fl_Tooltip::widget_
static Fl_Widget * widget_
Keeps track of the current target widget.
Definition: Fl_Tooltip.H:121
Fl_Widget::draw_box
void draw_box() const
Definition: fl_boxtype.cxx:442
Fl_Color
unsigned int Fl_Color
Definition: Enumerations.H:934
top_win_iconified_
static int top_win_iconified_()
Definition: Fl_Tooltip.cxx:139
Fl_Group::end
void end()
Definition: Fl_Group.cxx:75
Fl::event_state
static int event_state()
Definition: Fl.H:704
Fl_Menu_Window::hide
void hide()
Definition: Fl_Menu_Window.cxx:90
fl_color
void fl_color(Fl_Color c)
Definition: fl_draw.H:52
Fl_Tooltip::enter_area
static void enter_area(Fl_Widget *w, int X, int Y, int W, int H, const char *tip)
Definition: Fl_Tooltip.cxx:269
Fl_Tooltip::textcolor
static Fl_Color textcolor()
Definition: Fl_Tooltip.H:78
Fl::screen_xywh
static void screen_xywh(int &X, int &Y, int &W, int &H)
Definition: Fl.H:1000
free
void free()
H
static int H
Definition: Fl_Tooltip.cxx:76
FL_BUTTONS
#define FL_BUTTONS
Any mouse button is pushed.
Definition: Enumerations.H:570
Fl_Widget::set_flag
void set_flag(unsigned int c)
Definition: Fl_Widget.H:149
Fl_Tooltip::exit_
static void exit_(Fl_Widget *w)
Definition: Fl_Tooltip.cxx:237
NULL
#define NULL
Definition: forms.H:34
window
static Fl_TooltipBox * window
Definition: Fl_Tooltip.cxx:75
fl_draw
FL_EXPORT void fl_draw(const char *str, int x, int y)
Definition: fl_font.cxx:70
FL_KEYDOWN
Definition: Enumerations.H:310
Fl_Tooltip::enter_
static void enter_(Fl_Widget *w)
Definition: Fl_Tooltip.cxx:189
Fl_Window::set_tooltip_window
void set_tooltip_window()
Definition: Fl_Window.H:375
Fl_Tooltip::color
static Fl_Color color()
Definition: Fl_Tooltip.H:74
FL_BLACK
const Fl_Color FL_BLACK
Definition: Enumerations.H:956
Fl_Widget::as_window
virtual Fl_Window * as_window()
Definition: Fl_Widget.H:1000
Fl_Widget::w
int w() const
Definition: Fl_Widget.H:294
Fl_Tooltip::size
static Fl_Fontsize size()
Definition: Fl_Tooltip.H:70
recent_timeout
static void recent_timeout(void *)
Definition: Fl_Tooltip.cxx:128
tip
static const char * tip
Definition: Fl_Tooltip.cxx:40
Fl_Tooltip::color_
static Fl_Color color_
Definition: Fl_Tooltip.H:117
p
static menustate * p
Definition: Fl_Menu.cxx:606
FL_NUM_RED
#define FL_NUM_RED
Definition: Enumerations.H:980
Fl_Widget::w
void w(int v)
Definition: Fl_Widget.H:143
Fl_Widget::x
void x(int v)
Definition: Fl_Widget.H:139
Fl_Tooltip::size_
static Fl_Fontsize size_
Definition: Fl_Tooltip.H:120
FL_NUM_GREEN
#define FL_NUM_GREEN
Definition: Enumerations.H:981
Fl_TooltipBox::Fl_TooltipBox
Fl_TooltipBox()
Definition: Fl_Tooltip.cxx:51
fl_draw.H
utility header to pull drawing functions together
Fl_Widget::flags
unsigned int flags() const
Definition: Fl_Widget.H:147
FL_HELVETICA
const Fl_Font FL_HELVETICA
Helvetica (or Arial) normal (0)
Definition: Enumerations.H:879
Fl_Tooltip::margin_height
static int margin_height()
Definition: Fl_Tooltip.H:96
Fl::event_y_root
static int event_y_root()
Definition: Fl.H:617
Fl_Widget::COPIED_TOOLTIP
the widget tooltip is internally copied, its destruction is handled by the widget
Definition: Fl_Widget.H:173
Fl_Widget::visible
unsigned int visible() const
Definition: Fl_Widget.H:660
Fl_Tooltip::font
static Fl_Font font()
Definition: Fl_Tooltip.H:66
fl_measure
FL_EXPORT void fl_measure(const char *str, int &x, int &y, int draw_symbols=1)
Definition: fl_draw.cxx:380
Fl_Window
Definition: Fl_Window.H:57
Fl_Window::resize
virtual void resize(int X, int Y, int W, int H)
Definition: Fl_win32.cxx:1643
FL_BORDER_BOX
see figure 1
Definition: Enumerations.H:619
Fl_Menu_Window::show
void show()
Definition: Fl_Menu_Window.cxx:44
Fl_Fontsize
int Fl_Fontsize
Definition: Enumerations.H:906
FL_PUSH
Definition: Enumerations.H:236
Fl::remove_timeout
static void remove_timeout(Fl_Timeout_Handler, void *=0)
Definition: Fl.cxx:368
recursion
static char recursion
Definition: Fl_Tooltip.cxx:136
FL_NUM_BLUE
#define FL_NUM_BLUE
Definition: Enumerations.H:982
Fl_Widget::redraw
void redraw()
Definition: Fl.cxx:1782
Fl_Widget
Definition: Fl_Widget.H:101
Fl_Tooltip::current
static Fl_Widget * current()
Definition: Fl_Tooltip.H:62
Fl_Tooltip::hoverdelay
static float hoverdelay()
Definition: Fl_Tooltip.H:46
Fl_Widget::h
int h() const
Definition: Fl_Widget.H:299
Fl_Window::shown
int shown()
Definition: Fl_Window.H:485
Fl_TooltipBox
Definition: Fl_Tooltip.cxx:48
Fl::event_x_root
static int event_x_root()
Definition: Fl.H:610
Fl_Widget::h
void h(int v)
Definition: Fl_Widget.H:145
Fl_Tooltip.H
draw_symbols_
static const int draw_symbols_
Definition: Fl_Tooltip.cxx:43
Fl_Tooltip::enter
static void(* enter)(Fl_Widget *w)
Definition: Fl_Tooltip.H:58
fl_color_cube
Fl_Color fl_color_cube(int r, int g, int b)
Definition: Enumerations.H:1032
Fl_Window::label
const char * label() const
Definition: Fl_Window.H:450
Fl_Widget::y
void y(int v)
Definition: Fl_Widget.H:141
Fl_Window::handle
virtual int handle(int)
Definition: Fl.cxx:1684
Fl_TooltipBox::show
void show()
Definition: Fl_Tooltip.cxx:59
x
int x
Definition: test.c:73
Fl_Widget::tooltip_
const char * tooltip_
Definition: Fl_Widget.H:117
Fl_Font
int Fl_Font
Definition: Enumerations.H:877
Fl_Tooltip::delay
static float delay()
Definition: Fl_Tooltip.H:39
FL_ALIGN_WRAP
const Fl_Align FL_ALIGN_WRAP
Definition: Enumerations.H:851
Fl_Tooltip::exit
static void(* exit)(Fl_Widget *w)
Definition: Fl_Tooltip.H:60
Fl::add_timeout
static void add_timeout(double t, Fl_Timeout_Handler, void *=0)
Definition: Fl.cxx:329
Fl_TooltipBox::draw
void draw()
Definition: Fl_Tooltip.cxx:115
Fl_Widget::tooltip
const char * tooltip() const
Definition: Fl_Widget.H:552
y
int y
Definition: test.c:74
Fl_Widget::copy_tooltip
void copy_tooltip(const char *text)
Definition: Fl_Tooltip.cxx:365
Fl_Tooltip::set_enter_exit_once_
static void set_enter_exit_once_()
Definition: Fl_Tooltip.cxx:313
Fl_Widget::parent
Fl_Group * parent() const
Definition: Fl_Widget.H:254
Fl_Tooltip::margin_width
static int margin_width()
Definition: Fl_Tooltip.H:95
Y
static int Y
Definition: Fl_Tooltip.cxx:76
Fl_Window::set_override
void set_override()
Definition: Fl_Window.H:278
Fl_Tooltip::hoverdelay_
static float hoverdelay_
delay between tooltips
Definition: Fl_Tooltip.H:116
recent_tooltip
static char recent_tooltip
Definition: Fl_Tooltip.cxx:126
Fl_Tooltip::font_
static Fl_Font font_
Definition: Fl_Tooltip.H:119
Fl_Menu_Window
Definition: Fl_Menu_Window.H:33
Fl_Align
unsigned Fl_Align
Definition: Enumerations.H:828
Fl_Tooltip::delay_
static float delay_
delay before a tooltip is shown
Definition: Fl_Tooltip.H:115
Fl_Widget::top_window
Fl_Window * top_window() const
Definition: Fl_Window.cxx:130
Fl_Widget::clear_flag
void clear_flag(unsigned int c)
Definition: Fl_Widget.H:151
Fl::grab
static Fl_Window * grab()
Definition: Fl.H:555
Fl_TooltipBox::layout
void layout()
Definition: Fl_Tooltip.cxx:86
Fl_Tooltip::textcolor_
static Fl_Color textcolor_
Definition: Fl_Tooltip.H:118
Fl_Window::tooltip_window
unsigned int tooltip_window() const
Definition: Fl_Window.H:378
Fl_Menu_Window.H
beenhere
static uchar beenhere
Definition: fl_color.cxx:57
tooltip_timeout
static void tooltip_timeout(void *)
Definition: Fl_Tooltip.cxx:147