2001-08-02 01:24:49 +04:00
|
|
|
//
|
2002-10-29 22:45:10 +03:00
|
|
|
// "$Id: Fl_Tooltip.cxx,v 1.38.2.25 2002/10/29 19:45:10 easysw Exp $"
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2001-08-02 01:24:49 +04:00
|
|
|
// Tooltip source file for the Fast Light Tool Kit (FLTK).
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2002-01-01 18:11:33 +03:00
|
|
|
// Copyright 1998-2002 by Bill Spitzak and others.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2001-08-02 01:24:49 +04:00
|
|
|
// Please report all bugs and problems to "fltk-bugs@fltk.org".
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
2001-08-02 19:32:00 +04:00
|
|
|
#include <FL/Fl_Tooltip.H>
|
2002-05-13 09:05:11 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/Fl_Menu_Window.H>
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
#include <stdio.h>
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-05-13 19:43:10 +04:00
|
|
|
float Fl_Tooltip::delay_ = 1.0f;
|
|
|
|
int Fl_Tooltip::enabled_ = 1;
|
2001-10-29 06:44:33 +03:00
|
|
|
unsigned Fl_Tooltip::color_ = fl_color_cube(FL_NUM_RED - 1,
|
2001-10-18 22:53:20 +04:00
|
|
|
FL_NUM_GREEN - 1,
|
|
|
|
FL_NUM_BLUE - 2);
|
2002-05-13 09:05:11 +04:00
|
|
|
unsigned Fl_Tooltip::textcolor_ = FL_BLACK;
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Tooltip::font_ = FL_HELVETICA;
|
|
|
|
int Fl_Tooltip::size_ = FL_NORMAL_SIZE;
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
#define MAX_WIDTH 400
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-05-13 21:23:10 +04:00
|
|
|
static const char* tip;
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
class Fl_TooltipBox : public Fl_Menu_Window {
|
2001-08-02 01:24:49 +04:00
|
|
|
public:
|
2002-05-13 09:05:11 +04:00
|
|
|
Fl_TooltipBox() : Fl_Menu_Window(0, 0) {
|
|
|
|
set_override();
|
|
|
|
end();
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
2002-05-13 09:05:11 +04:00
|
|
|
void draw();
|
|
|
|
void layout();
|
2002-05-13 21:23:10 +04:00
|
|
|
void show() {
|
|
|
|
if (tip) Fl_Menu_Window::show();
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
};
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-10-29 22:45:10 +03:00
|
|
|
Fl_Widget* Fl_Tooltip::widget_ = 0;
|
2002-05-13 09:05:11 +04:00
|
|
|
static Fl_TooltipBox *window = 0;
|
|
|
|
static int X,Y,W,H;
|
|
|
|
|
|
|
|
void Fl_TooltipBox::layout() {
|
|
|
|
fl_font(Fl_Tooltip::font(), Fl_Tooltip::size());
|
|
|
|
int ww, hh;
|
|
|
|
ww = MAX_WIDTH;
|
|
|
|
fl_measure(tip, ww, hh, FL_ALIGN_LEFT|FL_ALIGN_WRAP|FL_ALIGN_INSIDE);
|
|
|
|
ww += 6; hh += 6;
|
|
|
|
|
|
|
|
// find position on the screen of the widget:
|
|
|
|
int ox = Fl::event_x_root();
|
|
|
|
//int ox = X+W/2;
|
|
|
|
int oy = Y + H+2;
|
2002-10-29 22:45:10 +03:00
|
|
|
for (Fl_Widget* p = Fl_Tooltip::current(); p; p = p->window()) {
|
2002-05-13 09:05:11 +04:00
|
|
|
//ox += p->x();
|
|
|
|
oy += p->y();
|
2002-04-14 06:43:48 +04:00
|
|
|
}
|
2002-05-13 09:05:11 +04:00
|
|
|
if (ox+ww > Fl::w()) ox = Fl::w() - ww;
|
|
|
|
if (ox < 0) ox = 0;
|
|
|
|
if (H > 30) {
|
|
|
|
oy = Fl::event_y_root()+13;
|
|
|
|
if (oy+hh > Fl::h()) oy -= 23+hh;
|
|
|
|
} else {
|
|
|
|
if (oy+hh > Fl::h()) oy -= (4+hh+H);
|
2001-08-02 19:32:00 +04:00
|
|
|
}
|
2002-05-13 09:05:11 +04:00
|
|
|
if (oy < 0) oy = 0;
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
resize(ox, oy, ww, hh);
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
void Fl_TooltipBox::draw() {
|
|
|
|
draw_box(FL_BORDER_BOX, 0, 0, w(), h(), Fl_Tooltip::color());
|
|
|
|
fl_color(Fl_Tooltip::textcolor());
|
|
|
|
fl_font(Fl_Tooltip::font(), Fl_Tooltip::size());
|
|
|
|
fl_draw(tip, 3, 3, w()-6, h()-6, Fl_Align(FL_ALIGN_LEFT|FL_ALIGN_WRAP));
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
static char recent_tooltip;
|
2001-08-02 19:32:00 +04:00
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
static void recent_timeout(void*) {
|
|
|
|
recent_tooltip = 0;
|
|
|
|
}
|
2001-08-02 19:32:00 +04:00
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
static char recursion;
|
|
|
|
|
|
|
|
static void tooltip_timeout(void*) {
|
|
|
|
if (recursion) return;
|
|
|
|
recursion = 1;
|
2002-10-29 22:45:10 +03:00
|
|
|
if (!tip || !*tip) {
|
|
|
|
if (window) window->hide();
|
|
|
|
} else {
|
|
|
|
//if (Fl::grab()) return;
|
|
|
|
if (!window) window = new Fl_TooltipBox;
|
|
|
|
// this cast bypasses the normal Fl_Window label() code:
|
|
|
|
((Fl_Widget*)window)->label(tip);
|
|
|
|
window->layout();
|
|
|
|
window->redraw();
|
|
|
|
// printf("tooltip_timeout: Showing window %p with tooltip \"%s\"...\n",
|
|
|
|
// window, tip ? tip : "(null)");
|
|
|
|
window->show();
|
|
|
|
}
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
Fl::remove_timeout(recent_timeout);
|
|
|
|
recent_tooltip = 1;
|
|
|
|
recursion = 0;
|
|
|
|
}
|
2001-08-02 19:32:00 +04:00
|
|
|
|
2002-10-29 22:45:10 +03:00
|
|
|
// Acts as though enter(widget) was done but does not pop up a
|
|
|
|
// tooltip. This is useful to prevent a tooltip from reappearing when
|
|
|
|
// a modal overlapping window is deleted. Fltk does this automatically
|
|
|
|
// when you click the mouse button.
|
|
|
|
void Fl_Tooltip::current(Fl_Widget* w) {
|
|
|
|
enter(0);
|
|
|
|
// find the enclosing group with a tooltip:
|
|
|
|
Fl_Widget* tw = w;
|
|
|
|
for (;;) {
|
|
|
|
if (!tw) return;
|
|
|
|
if (tw->tooltip()) break;
|
|
|
|
tw = tw->parent();
|
|
|
|
}
|
|
|
|
// act just like tt_enter() except we can remember a zero:
|
|
|
|
widget_ = w;
|
|
|
|
}
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
// This is called when a widget is destroyed:
|
|
|
|
static void
|
|
|
|
tt_exit(Fl_Widget *w) {
|
2002-05-13 21:23:10 +04:00
|
|
|
// printf("tt_exit(w=%p)\n", w);
|
|
|
|
// printf(" widget=%p, window=%p\n", widget, window);
|
|
|
|
|
2002-10-29 22:45:10 +03:00
|
|
|
if (!Fl_Tooltip::current()) return;
|
|
|
|
Fl_Tooltip::current(0);
|
|
|
|
Fl::remove_timeout(tooltip_timeout);
|
|
|
|
Fl::remove_timeout(recent_timeout);
|
|
|
|
if (window) window->hide();
|
|
|
|
if (recent_tooltip) {
|
|
|
|
if (Fl::event_state() & FL_BUTTONS) recent_tooltip = 0;
|
|
|
|
else Fl::add_timeout(.2f, recent_timeout);
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
static void
|
2002-08-09 07:17:30 +04:00
|
|
|
tt_enter(Fl_Widget* wp) {
|
2002-05-13 21:23:10 +04:00
|
|
|
// printf("tt_enter(widget=%p)\n", widget);
|
|
|
|
// printf(" window=%p\n", window);
|
2002-05-13 09:05:11 +04:00
|
|
|
// find the enclosing group with a tooltip:
|
2002-08-09 07:17:30 +04:00
|
|
|
Fl_Widget* w = wp;
|
2002-05-13 09:05:11 +04:00
|
|
|
while (w && !w->tooltip()) {
|
2002-05-15 20:37:06 +04:00
|
|
|
//if (w == window) return; // don't do anything if pointed at tooltip
|
2002-05-13 09:05:11 +04:00
|
|
|
w = w->parent();
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
2002-05-13 09:05:11 +04:00
|
|
|
if (!w) {
|
|
|
|
Fl_Tooltip::enter_area(0, 0, 0, 0, 0, 0);
|
|
|
|
} else {
|
2002-08-09 07:17:30 +04:00
|
|
|
Fl_Tooltip::enter_area(wp,0,0,wp->w(), wp->h(), w->tooltip());
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
2002-05-13 09:05:11 +04:00
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
void
|
|
|
|
Fl_Tooltip::enter_area(Fl_Widget* wid, int x,int y,int w,int h, const char* t)
|
|
|
|
{
|
2002-05-13 21:23:10 +04:00
|
|
|
// printf("Fl_Tooltip::enter_area(wid=%p, x=%d, y=%d, w=%d, h=%d, t=\"%s\")\n",
|
|
|
|
// wid, x, y, w, h, t ? t : "(null)");
|
|
|
|
// printf(" recursion=%d, window=%p\n", recursion, window);
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
if (recursion) return;
|
2002-10-29 22:45:10 +03:00
|
|
|
if (!t || !*t || !enabled()) {
|
|
|
|
if (window) window->hide();
|
2002-05-14 19:55:20 +04:00
|
|
|
Fl::remove_timeout(tooltip_timeout);
|
|
|
|
Fl::remove_timeout(recent_timeout);
|
2002-10-29 22:45:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// do nothing if it is the same:
|
|
|
|
if (wid==widget_ && x==X && y==Y && w==W && h==H && t==tip) return;
|
|
|
|
Fl::remove_timeout(tooltip_timeout);
|
|
|
|
Fl::remove_timeout(recent_timeout);
|
|
|
|
// remember it:
|
|
|
|
widget_ = wid; X = x; Y = y; W = w; H = h; tip = t;
|
|
|
|
// popup the tooltip immediately if it was recently up:
|
|
|
|
if (recent_tooltip || Fl_Tooltip::delay() < .1) {
|
2002-05-14 19:24:03 +04:00
|
|
|
#ifdef WIN32
|
2002-10-29 22:45:10 +03:00
|
|
|
// possible fix for the Windows titlebar, it seems to want the
|
|
|
|
// window to be destroyed, moving it messes up the parenting:
|
|
|
|
if (window) window->hide();
|
2002-05-14 19:24:03 +04:00
|
|
|
#endif
|
2002-10-29 22:45:10 +03:00
|
|
|
tooltip_timeout(0);
|
|
|
|
} else {
|
2002-05-13 09:05:11 +04:00
|
|
|
if (window) window->hide();
|
2002-10-29 22:45:10 +03:00
|
|
|
Fl::add_timeout(Fl_Tooltip::delay(), tooltip_timeout);
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
2002-05-13 21:23:10 +04:00
|
|
|
|
|
|
|
// printf(" tip=\"%s\", window->shown()=%d\n", tip ? tip : "(null)",
|
|
|
|
// window ? window->shown() : 0);
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
2002-05-13 09:05:11 +04:00
|
|
|
void Fl_Widget::tooltip(const char *tt) {
|
|
|
|
static char beenhere = 0;
|
|
|
|
if (!beenhere) {
|
|
|
|
beenhere = 1;
|
|
|
|
Fl_Tooltip::enter = tt_enter;
|
|
|
|
Fl_Tooltip::exit = tt_exit;
|
|
|
|
}
|
|
|
|
tooltip_ = tt;
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
//
|
2002-10-29 22:45:10 +03:00
|
|
|
// End of "$Id: Fl_Tooltip.cxx,v 1.38.2.25 2002/10/29 19:45:10 easysw Exp $".
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|