3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
8 * Tooltips for xforms. xforms 0.89 supports them directly, but 0.88 needs
9 * a bit of jiggery pokery. This class wraps it all up in a neat interface.
10 * Based on code originally in Toolbar_pimpl.C that appears to have been
11 * written by Matthias Ettrich and Jean-Marc Lasgouttes.
17 #pragma implementation
22 #include "xforms_helpers.h" // formatted
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
30 bool Tooltips::enabled_ = false;
32 SigC::Signal0<void> Tooltips::tooltipsToggled;
39 static bool first = true;
42 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
44 tooltipsToggled.connect(slot(this, &Tooltips::toggleTooltips));
48 void Tooltips::toggleEnabled()
55 void Tooltips::toggleTooltips()
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);
70 // Set the cursor to a question mark or back to the default.
71 FL_OBJECT * const ob = tooltipsMap.begin()->first;
72 int const cursor = enabled_ ? XC_question_arrow : FL_DEFAULT_CURSOR;
73 fl_set_cursor(FL_ObjWin(ob), cursor);
77 void Tooltips::initTooltip(FL_OBJECT * ob, string const & tip)
82 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
83 if (it != tooltipsMap.end())
86 string const str = strip(frontStrip(tip));
90 // Store the tooltip string
91 tooltipsMap[ob] = formatted(_(str), 400);
93 // Set the initial state of the tooltips
94 char const * const c_str = enabled_ ? str.c_str() : 0;
95 fl_set_object_helper(ob, c_str);
99 #else // if FL_REVISION < 89
103 int TooltipHandler(FL_OBJECT *ob, int event);
105 void TooltipTimerCB(FL_OBJECT * timer, long data);
111 static int C_TooltipHandler(FL_OBJECT * ob, int event,
112 FL_Coord, FL_Coord, int, void *)
114 return TooltipHandler(ob, event);
118 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
120 TooltipTimerCB(ob, data);
129 static bool first = true;
132 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
134 tooltipsToggled.connect(slot(this, &Tooltips::toggleTooltips));
138 void Tooltips::toggleEnabled()
140 enabled_ = !enabled_;
145 void Tooltips::toggleTooltips()
147 if (tooltipsMap.empty())
148 // There are no objects with tooltips in this dialog, so
149 // just go away. Don't change the cursor to a question mark.
152 // Set the cursor to a question mark or back to the default.
153 FL_OBJECT * const ob = tooltipsMap.begin()->first;
154 int const cursor = enabled_ ? XC_question_arrow : FL_DEFAULT_CURSOR;
155 fl_set_cursor(FL_ObjWin(ob), cursor);
159 void Tooltips::initTooltip(FL_OBJECT * ob, string const & tip)
164 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
165 if (it != tooltipsMap.end())
168 string const str = strip(frontStrip(tip));
172 // Store the tooltip string
173 tooltipsMap[ob] = formatted(_(str), 400);
175 if (!tooltip_timer_) {
176 lyx::Assert(ob->form);
177 if (fl_current_form && ob->form != fl_current_form)
180 bool const open_form = !fl_current_form;
182 fl_addto_form(ob->form);
184 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
190 fl_set_object_posthandler(ob, C_TooltipHandler);
191 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
192 tooltip_timer_->u_vdata = this;
196 string const Tooltips::getTooltip(FL_OBJECT * ob) const
198 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
199 if (it == tooltipsMap.end())
207 void TooltipTimerCB(FL_OBJECT * timer, long data)
209 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
210 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
211 FL_FORM * form = ob->form;
212 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
214 string const help = tooltip->getTooltip(ob);
218 fl_show_oneliner(help.c_str(),
219 form->x + ob->x, form->y + ob->y + ob->h);
223 // post_handler for tooltip help
224 int TooltipHandler(FL_OBJECT * ob, int event)
226 if (!Tooltips::enabled())
230 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
233 // We do not test for empty help here, since this can never happen
234 if (event == FL_ENTER) {
235 fl_set_object_callback(timer,
237 reinterpret_cast<long>(ob));
238 fl_set_timer(timer, 1);
240 else if (event != FL_MOTION) {
241 fl_set_timer(timer, 0);
249 #endif // FL_REVISION >= 89