3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
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
25 #include "xforms_helpers.h" // formatted
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
33 bool Tooltips::enabled_ = false;
35 SigC::Signal0<void> Tooltips::tooltipsToggled;
42 static bool first = true;
45 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
47 tooltipsToggled.connect(slot(this, &Tooltips::toggleTooltips));
51 void Tooltips::toggleEnabled()
58 void Tooltips::toggleTooltips()
60 if (tooltipsMap.empty())
61 // There are no objects with tooltips in this dialog, so
62 // just go away. Don't change the cursor to a question mark.
65 TooltipsMap::iterator it = tooltipsMap.begin();
66 TooltipsMap::iterator end = tooltipsMap.end();
67 for (; it != end; ++it) {
68 FL_OBJECT * const ob = it->first;
69 char const * const c_str = enabled_ ? it->second.c_str() : 0;
70 fl_set_object_helper(ob, c_str);
73 // Set the cursor to a question mark or back to the default.
74 FL_OBJECT * const ob = tooltipsMap.begin()->first;
75 int const cursor = enabled_ ? XC_question_arrow : FL_DEFAULT_CURSOR;
76 fl_set_cursor(FL_ObjWin(ob), cursor);
80 void Tooltips::initTooltip(FL_OBJECT * ob, string const & tip)
85 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
86 if (it != tooltipsMap.end())
89 string const str = strip(frontStrip(tip));
93 // Store the tooltip string
94 tooltipsMap[ob] = formatted(_(str), 400);
96 // Set the initial state of the tooltips
97 char const * const c_str = enabled_ ? str.c_str() : 0;
98 fl_set_object_helper(ob, c_str);
102 #else // if FL_REVISION < 89
106 int TooltipHandler(FL_OBJECT *ob, int event);
108 void TooltipTimerCB(FL_OBJECT * timer, long data);
114 static int C_TooltipHandler(FL_OBJECT * ob, int event,
115 FL_Coord, FL_Coord, int, void *)
117 return TooltipHandler(ob, event);
121 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
123 TooltipTimerCB(ob, data);
132 static bool first = true;
135 Dialogs::toggleTooltips.connect(slot(&Tooltips::toggleEnabled));
137 tooltipsToggled.connect(slot(this, &Tooltips::toggleTooltips));
141 void Tooltips::toggleEnabled()
143 enabled_ = !enabled_;
148 void Tooltips::toggleTooltips()
150 if (tooltipsMap.empty())
151 // There are no objects with tooltips in this dialog, so
152 // just go away. Don't change the cursor to a question mark.
155 // Set the cursor to a question mark or back to the default.
156 FL_OBJECT * const ob = tooltipsMap.begin()->first;
157 int const cursor = enabled_ ? XC_question_arrow : FL_DEFAULT_CURSOR;
158 fl_set_cursor(FL_ObjWin(ob), cursor);
162 void Tooltips::initTooltip(FL_OBJECT * ob, string const & tip)
167 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
168 if (it != tooltipsMap.end())
171 string const str = strip(frontStrip(tip));
175 // Store the tooltip string
176 tooltipsMap[ob] = formatted(_(str), 400);
178 if (!tooltip_timer_) {
179 lyx::Assert(ob->form);
180 if (fl_current_form && ob->form != fl_current_form)
183 bool const open_form = !fl_current_form;
185 fl_addto_form(ob->form);
187 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
193 fl_set_object_posthandler(ob, C_TooltipHandler);
194 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
195 tooltip_timer_->u_vdata = this;
199 string const Tooltips::getTooltip(FL_OBJECT * ob) const
201 TooltipsMap::const_iterator it = tooltipsMap.find(ob);
202 if (it == tooltipsMap.end())
210 void TooltipTimerCB(FL_OBJECT * timer, long data)
212 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
213 lyx::Assert(ob && ob->form && timer && timer->u_vdata);
214 FL_FORM * form = ob->form;
215 Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
217 string const help = tooltip->getTooltip(ob);
221 fl_show_oneliner(help.c_str(),
222 form->x + ob->x, form->y + ob->y + ob->h);
226 // post_handler for tooltip help
227 int TooltipHandler(FL_OBJECT * ob, int event)
229 if (!Tooltips::enabled())
233 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
236 // We do not test for empty help here, since this can never happen
237 if (event == FL_ENTER) {
238 fl_set_object_callback(timer,
240 reinterpret_cast<long>(ob));
241 fl_set_timer(timer, 1);
243 else if (event != FL_MOTION) {
244 fl_set_timer(timer, 0);
252 #endif // FL_REVISION >= 89