3 * Read the file COPYING
5 * \author Angus Leeming
7 * Full author contact details are available in file CREDITS
10 /* Tooltips for xforms. xforms 0.89 supports them directly, but 0.88 needs
11 * a bit of jiggery pokery. This class wraps it all up in a neat interface.
12 * Based on code originally in Toolbar_pimpl.C that appears to have been
13 * written by Matthias Ettrich and Jean-Marc Lasgouttes.
19 #pragma implementation
23 #include "xforms_helpers.h" // formatted
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include FORMS_H_LOCATION
29 #include <boost/bind.hpp>
31 bool Tooltips::enabled_ = true;
33 boost::signal0<void> Tooltips::toggled;
36 #if FL_VERSION > 0 || FL_REVISION >= 89
40 toggled.connect(boost::bind(&Tooltips::set, this));
44 void Tooltips::toggleEnabled()
53 if (tooltipsMap.empty())
54 // There are no objects with tooltips in this dialog, so
55 // just go away. Don't change the cursor to a question mark.
58 TooltipsMap::iterator it = tooltipsMap.begin();
59 TooltipsMap::iterator end = tooltipsMap.end();
60 for (; it != end; ++it) {
61 FL_OBJECT * const ob = it->first;
62 char const * const c_str = enabled_ ? it->second.c_str() : 0;
63 fl_set_object_helper(ob, c_str);
68 void Tooltips::init(FL_OBJECT * ob, string const & tip)
70 lyx::Assert(ob && ob->form);
73 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
74 if (it != tooltipsMap.end())
77 string const str = trim(tip);
81 // Store the tooltip string
82 tooltipsMap[ob] = formatted(str, 400);
86 #else // if FL_REVISION < 89
90 int TooltipHandler(FL_OBJECT *ob, int event);
92 void TooltipTimerCB(FL_OBJECT * timer, long data);
98 static int C_TooltipHandler(FL_OBJECT * ob, int event,
99 FL_Coord, FL_Coord, int, void *)
101 return TooltipHandler(ob, event);
105 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
107 TooltipTimerCB(ob, data);
116 toggled.connect(boost::bind(&Tooltips::set, this));
120 void Tooltips::toggleEnabled()
122 enabled_ = !enabled_;
131 void Tooltips::init(FL_OBJECT * ob, string const & tip)
133 lyx::Assert(ob && ob->form);
136 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
137 if (it != tooltipsMap.end())
140 string const str = trim(tip);
144 // Store the tooltip string
145 tooltipsMap[ob] = formatted(str, 400);
147 if (!tooltip_timer_) {
148 if (fl_current_form && ob->form != fl_current_form)
151 bool const open_form = !fl_current_form;
153 fl_addto_form(ob->form);
155 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
161 fl_set_object_posthandler(ob, C_TooltipHandler);
162 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
163 tooltip_timer_->u_vdata = this;
167 string const Tooltips::get(FL_OBJECT * ob) const
169 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
170 if (it == tooltipsMap.end())
178 void TooltipTimerCB(FL_OBJECT * timer, long data)
180 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
181 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
182 FL_FORM * form = ob->form;
183 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
185 string const help = tooltip->get(ob);
189 fl_show_oneliner(help.c_str(),
190 form->x + ob->x, form->y + ob->y + ob->h);
194 // post_handler for tooltip help
195 int TooltipHandler(FL_OBJECT * ob, int event)
197 if (!Tooltips::enabled())
201 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
204 // We do not test for empty help here, since this can never happen
205 if (event == FL_ENTER) {
206 fl_set_object_callback(timer,
208 reinterpret_cast<long>(ob));
209 fl_set_timer(timer, 1);
211 else if (event != FL_MOTION) {
212 fl_set_timer(timer, 0);
220 #endif // FL_REVISION >= 89