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"
31 bool Tooltips::enabled_ = false;
33 SigC::Signal0<void> Tooltips::toggled;
40 static bool first = true;
43 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
45 toggled.connect(slot(this, &Tooltips::set));
49 void Tooltips::toggleEnabled()
58 if (tooltipsMap.empty())
59 // There are no objects with tooltips in this dialog, so
60 // just go away. Don't change the cursor to a question mark.
63 TooltipsMap::iterator it = tooltipsMap.begin();
64 TooltipsMap::iterator end = tooltipsMap.end();
65 for (; it != end; ++it) {
66 FL_OBJECT * const ob = it->first;
67 char const * const c_str = enabled_ ? it->second.c_str() : 0;
68 fl_set_object_helper(ob, c_str);
73 void Tooltips::init(FL_OBJECT * ob, string const & tip)
75 lyx::Assert(ob && ob->form);
78 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
79 if (it != tooltipsMap.end())
82 string const str = strip(frontStrip(tip));
86 // Store the tooltip string
87 tooltipsMap[ob] = formatted(_(str), 400);
91 #else // if FL_REVISION < 89
95 int TooltipHandler(FL_OBJECT *ob, int event);
97 void TooltipTimerCB(FL_OBJECT * timer, long data);
103 static int C_TooltipHandler(FL_OBJECT * ob, int event,
104 FL_Coord, FL_Coord, int, void *)
106 return TooltipHandler(ob, event);
110 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
112 TooltipTimerCB(ob, data);
121 static bool first = true;
124 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
126 toggled.connect(slot(this, &Tooltips::set));
130 void Tooltips::toggleEnabled()
132 enabled_ = !enabled_;
141 void Tooltips::init(FL_OBJECT * ob, string const & tip)
143 lyx::Assert(ob && ob->form);
146 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
147 if (it != tooltipsMap.end())
150 string const str = strip(frontStrip(tip));
154 // Store the tooltip string
155 tooltipsMap[ob] = formatted(_(str), 400);
157 if (!tooltip_timer_) {
158 if (fl_current_form && ob->form != fl_current_form)
161 bool const open_form = !fl_current_form;
163 fl_addto_form(ob->form);
165 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
171 fl_set_object_posthandler(ob, C_TooltipHandler);
172 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
173 tooltip_timer_->u_vdata = this;
177 string const Tooltips::get(FL_OBJECT * ob) const
179 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
180 if (it == tooltipsMap.end())
188 void TooltipTimerCB(FL_OBJECT * timer, long data)
190 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
191 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
192 FL_FORM * form = ob->form;
193 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
195 string const help = tooltip->get(ob);
199 fl_show_oneliner(help.c_str(),
200 form->x + ob->x, form->y + ob->y + ob->h);
204 // post_handler for tooltip help
205 int TooltipHandler(FL_OBJECT * ob, int event)
207 if (!Tooltips::enabled())
211 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
214 // We do not test for empty help here, since this can never happen
215 if (event == FL_ENTER) {
216 fl_set_object_callback(timer,
218 reinterpret_cast<long>(ob));
219 fl_set_timer(timer, 1);
221 else if (event != FL_MOTION) {
222 fl_set_timer(timer, 0);
230 #endif // FL_REVISION >= 89