3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
11 /* Tooltips for xforms. xforms 0.89 supports them directly, but 0.88 needs
12 * a bit of jiggery pokery. This class wraps it all up in a neat interface.
13 * Based on code originally in Toolbar_pimpl.C that appears to have been
14 * written by Matthias Ettrich and Jean-Marc Lasgouttes.
20 #pragma implementation
24 #include "xforms_helpers.h" // formatted
26 #include "support/lstrings.h"
27 #include "support/LAssert.h"
28 #include FORMS_H_LOCATION
30 #include <boost/bind.hpp>
32 bool Tooltips::enabled_ = true;
34 boost::signal0<void> Tooltips::toggled;
37 #if FL_VERSION > 0 || FL_REVISION >= 89
41 toggled.connect(boost::bind(&Tooltips::set, this));
45 void Tooltips::toggleEnabled()
54 if (tooltipsMap.empty())
55 // There are no objects with tooltips in this dialog, so
56 // just go away. Don't change the cursor to a question mark.
59 TooltipsMap::iterator it = tooltipsMap.begin();
60 TooltipsMap::iterator end = tooltipsMap.end();
61 for (; it != end; ++it) {
62 FL_OBJECT * const ob = it->first;
63 char const * const c_str = enabled_ ? it->second.c_str() : 0;
64 fl_set_object_helper(ob, c_str);
69 void Tooltips::init(FL_OBJECT * ob, string const & tip)
71 lyx::Assert(ob && ob->form);
74 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
75 if (it != tooltipsMap.end())
78 string str = trim(tip);
82 // Store the tooltip string
83 str = formatted(str, 400);
84 tooltipsMap[ob] = str;
87 char const * const c_str = enabled_ ? str.c_str() : 0;
88 fl_set_object_helper(ob, c_str);
92 #else // if FL_REVISION < 89
96 int TooltipHandler(FL_OBJECT *ob, int event);
98 void TooltipTimerCB(FL_OBJECT * timer, long data);
104 static int C_TooltipHandler(FL_OBJECT * ob, int event,
105 FL_Coord, FL_Coord, int, void *)
107 return TooltipHandler(ob, event);
111 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
113 TooltipTimerCB(ob, data);
122 toggled.connect(boost::bind(&Tooltips::set, this));
126 void Tooltips::toggleEnabled()
128 enabled_ = !enabled_;
137 void Tooltips::init(FL_OBJECT * ob, string const & tip)
139 lyx::Assert(ob && ob->form);
142 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
143 if (it != tooltipsMap.end())
146 string const str = trim(tip);
150 // Store the tooltip string
151 tooltipsMap[ob] = formatted(str, 400);
153 if (!tooltip_timer_) {
154 if (fl_current_form && ob->form != fl_current_form)
157 bool const open_form = !fl_current_form;
159 fl_addto_form(ob->form);
161 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
167 fl_set_object_posthandler(ob, C_TooltipHandler);
168 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
169 tooltip_timer_->u_vdata = this;
173 string const Tooltips::get(FL_OBJECT * ob) const
175 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
176 if (it == tooltipsMap.end())
184 void TooltipTimerCB(FL_OBJECT * timer, long data)
186 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
187 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
188 FL_FORM * form = ob->form;
189 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
191 string const help = tooltip->get(ob);
195 fl_show_oneliner(help.c_str(),
196 form->x + ob->x, form->y + ob->y + ob->h);
200 // post_handler for tooltip help
201 int TooltipHandler(FL_OBJECT * ob, int event)
203 if (!Tooltips::enabled())
207 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
210 // We do not test for empty help here, since this can never happen
211 if (event == FL_ENTER) {
212 fl_set_object_callback(timer,
214 reinterpret_cast<long>(ob));
215 fl_set_timer(timer, 1);
217 else if (event != FL_MOTION) {
218 fl_set_timer(timer, 0);
226 #endif // FL_REVISION >= 89