3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <leeming@lyx.org>
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
22 #include "xforms_helpers.h" // formatted
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
26 #include FORMS_H_LOCATION
28 #include <boost/bind.hpp>
30 bool Tooltips::enabled_ = true;
32 boost::signal0<void> Tooltips::toggled;
35 #if FL_VERSION > 0 || FL_REVISION >= 89
39 toggled.connect(boost::bind(&Tooltips::set, this));
43 void Tooltips::toggleEnabled()
52 if (tooltipsMap.empty())
53 // There are no objects with tooltips in this dialog, so
54 // just go away. Don't change the cursor to a question mark.
57 TooltipsMap::iterator it = tooltipsMap.begin();
58 TooltipsMap::iterator end = tooltipsMap.end();
59 for (; it != end; ++it) {
60 FL_OBJECT * const ob = it->first;
61 char const * const c_str = enabled_ ? it->second.c_str() : 0;
62 fl_set_object_helper(ob, c_str);
67 void Tooltips::init(FL_OBJECT * ob, string const & tip)
69 lyx::Assert(ob && ob->form);
72 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
73 if (it != tooltipsMap.end())
76 string const str = trim(tip);
80 // Store the tooltip string
81 tooltipsMap[ob] = formatted(str, 400);
85 #else // if FL_REVISION < 89
89 int TooltipHandler(FL_OBJECT *ob, int event);
91 void TooltipTimerCB(FL_OBJECT * timer, long data);
97 static int C_TooltipHandler(FL_OBJECT * ob, int event,
98 FL_Coord, FL_Coord, int, void *)
100 return TooltipHandler(ob, event);
104 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
106 TooltipTimerCB(ob, data);
115 toggled.connect(boost::bind(&Tooltips::set, this));
119 void Tooltips::toggleEnabled()
121 enabled_ = !enabled_;
130 void Tooltips::init(FL_OBJECT * ob, string const & tip)
132 lyx::Assert(ob && ob->form);
135 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
136 if (it != tooltipsMap.end())
139 string const str = trim(tip);
143 // Store the tooltip string
144 tooltipsMap[ob] = formatted(str, 400);
146 if (!tooltip_timer_) {
147 if (fl_current_form && ob->form != fl_current_form)
150 bool const open_form = !fl_current_form;
152 fl_addto_form(ob->form);
154 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
160 fl_set_object_posthandler(ob, C_TooltipHandler);
161 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
162 tooltip_timer_->u_vdata = this;
166 string const Tooltips::get(FL_OBJECT * ob) const
168 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
169 if (it == tooltipsMap.end())
177 void TooltipTimerCB(FL_OBJECT * timer, long data)
179 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
180 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
181 FL_FORM * form = ob->form;
182 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
184 string const help = tooltip->get(ob);
188 fl_show_oneliner(help.c_str(),
189 form->x + ob->x, form->y + ob->y + ob->h);
193 // post_handler for tooltip help
194 int TooltipHandler(FL_OBJECT * ob, int event)
196 if (!Tooltips::enabled())
200 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
203 // We do not test for empty help here, since this can never happen
204 if (event == FL_ENTER) {
205 fl_set_object_callback(timer,
207 reinterpret_cast<long>(ob));
208 fl_set_timer(timer, 1);
210 else if (event != FL_MOTION) {
211 fl_set_timer(timer, 0);
219 #endif // FL_REVISION >= 89