3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
9 /* Tooltips for xforms. xforms 0.89 supports them directly, but 0.88 needs
10 * a bit of jiggery pokery. This class wraps it all up in a neat interface.
11 * Based on code originally in Toolbar_pimpl.C that appears to have been
12 * written by Matthias Ettrich and Jean-Marc Lasgouttes.
18 #pragma implementation
23 #include "xforms_helpers.h" // formatted
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
28 #include <boost/bind.hpp>
30 bool Tooltips::enabled_ = true;
32 boost::signal0<void> Tooltips::toggled;
39 static bool first = true;
42 Dialogs::toggleTooltips.connect(boost::bind(&Tooltips::toggleEnabled));
44 toggled.connect(boost::bind(&Tooltips::set, this));
48 void Tooltips::toggleEnabled()
57 if (tooltipsMap.empty())
58 // There are no objects with tooltips in this dialog, so
59 // just go away. Don't change the cursor to a question mark.
62 TooltipsMap::iterator it = tooltipsMap.begin();
63 TooltipsMap::iterator end = tooltipsMap.end();
64 for (; it != end; ++it) {
65 FL_OBJECT * const ob = it->first;
66 char const * const c_str = enabled_ ? it->second.c_str() : 0;
67 fl_set_object_helper(ob, c_str);
72 void Tooltips::init(FL_OBJECT * ob, string const & tip)
74 lyx::Assert(ob && ob->form);
77 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
78 if (it != tooltipsMap.end())
81 string const str = strip(frontStrip(tip));
85 // Store the tooltip string
86 tooltipsMap[ob] = formatted(str, 400);
90 #else // if FL_REVISION < 89
94 int TooltipHandler(FL_OBJECT *ob, int event);
96 void TooltipTimerCB(FL_OBJECT * timer, long data);
102 static int C_TooltipHandler(FL_OBJECT * ob, int event,
103 FL_Coord, FL_Coord, int, void *)
105 return TooltipHandler(ob, event);
109 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
111 TooltipTimerCB(ob, data);
120 static bool first = true;
123 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
125 toggled.connect(slot(this, &Tooltips::set));
129 void Tooltips::toggleEnabled()
131 enabled_ = !enabled_;
140 void Tooltips::init(FL_OBJECT * ob, string const & tip)
142 lyx::Assert(ob && ob->form);
145 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
146 if (it != tooltipsMap.end())
149 string const str = strip(frontStrip(tip));
153 // Store the tooltip string
154 tooltipsMap[ob] = formatted(str, 400);
156 if (!tooltip_timer_) {
157 if (fl_current_form && ob->form != fl_current_form)
160 bool const open_form = !fl_current_form;
162 fl_addto_form(ob->form);
164 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
170 fl_set_object_posthandler(ob, C_TooltipHandler);
171 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
172 tooltip_timer_->u_vdata = this;
176 string const Tooltips::get(FL_OBJECT * ob) const
178 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
179 if (it == tooltipsMap.end())
187 void TooltipTimerCB(FL_OBJECT * timer, long data)
189 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
190 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
191 FL_FORM * form = ob->form;
192 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
194 string const help = tooltip->get(ob);
198 fl_show_oneliner(help.c_str(),
199 form->x + ob->x, form->y + ob->y + ob->h);
203 // post_handler for tooltip help
204 int TooltipHandler(FL_OBJECT * ob, int event)
206 if (!Tooltips::enabled())
210 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
213 // We do not test for empty help here, since this can never happen
214 if (event == FL_ENTER) {
215 fl_set_object_callback(timer,
217 reinterpret_cast<long>(ob));
218 fl_set_timer(timer, 1);
220 else if (event != FL_MOTION) {
221 fl_set_timer(timer, 0);
229 #endif // FL_REVISION >= 89