]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Tooltips.C
Compilation fix for old and crappy versions of the xforms library.
[lyx.git] / src / frontends / xforms / Tooltips.C
1 /*
2  * \file Tooltips.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming <leeming@lyx.org>
7  */
8
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.
13  */
14
15 #include <config.h>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "Tooltips.h"
22 #include "xforms_helpers.h" // formatted
23 #include "gettext.h"
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
26 #include FORMS_H_LOCATION
27
28 #include <boost/bind.hpp>
29
30 bool Tooltips::enabled_ = true;
31
32 boost::signal0<void> Tooltips::toggled;
33
34
35 #if FL_VERSION > 0 || FL_REVISION >= 89
36
37 Tooltips::Tooltips()
38 {
39         toggled.connect(boost::bind(&Tooltips::set, this));
40 }
41
42
43 void Tooltips::toggleEnabled()
44 {
45         enabled_ = !enabled_;
46         toggled();
47 }
48
49
50 void Tooltips::set()
51 {
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.
55                 return;
56
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);
63         }
64 }
65
66
67 void Tooltips::init(FL_OBJECT * ob, string const & tip)
68 {
69         lyx::Assert(ob && ob->form);
70
71         // Paranoia check!
72         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
73         if (it != tooltipsMap.end())
74                 return;
75
76         string const str = trim(tip);
77         if (str.empty())
78                 return;
79
80         // Store the tooltip string
81         tooltipsMap[ob] = formatted(str, 400);
82 }
83
84
85 #else // if FL_REVISION < 89
86
87 namespace {
88
89 int TooltipHandler(FL_OBJECT *ob, int event);
90
91 void TooltipTimerCB(FL_OBJECT * timer, long data);
92
93 }
94
95 extern "C" {
96
97 static int C_TooltipHandler(FL_OBJECT * ob, int event,
98                                     FL_Coord, FL_Coord, int, void *)
99 {
100         return TooltipHandler(ob, event);
101 }
102
103
104 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
105 {
106         TooltipTimerCB(ob, data);
107 }
108
109 }
110
111
112 Tooltips::Tooltips()
113         : tooltip_timer_(0)
114 {
115         toggled.connect(boost::bind(&Tooltips::set, this));
116 }
117
118
119 void Tooltips::toggleEnabled()
120 {
121         enabled_ = !enabled_;
122         toggled();
123 }
124
125
126 void Tooltips::set()
127 {}
128
129
130 void Tooltips::init(FL_OBJECT * ob, string const & tip)
131 {
132         lyx::Assert(ob && ob->form);
133
134         // Paranoia check!
135         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
136         if (it != tooltipsMap.end())
137                 return;
138
139         string const str = trim(tip);
140         if (str.empty())
141                 return;
142
143         // Store the tooltip string
144         tooltipsMap[ob] = formatted(str, 400);
145
146         if (!tooltip_timer_) {
147                 if (fl_current_form && ob->form != fl_current_form)
148                         fl_end_form();
149
150                 bool const open_form = !fl_current_form;
151                 if (open_form)
152                         fl_addto_form(ob->form);
153
154                 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
155
156                 if (open_form)
157                         fl_end_form();
158         }
159
160         fl_set_object_posthandler(ob, C_TooltipHandler);
161         ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
162         tooltip_timer_->u_vdata = this;
163 }
164
165
166 string const Tooltips::get(FL_OBJECT * ob) const
167 {
168         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
169         if (it == tooltipsMap.end())
170                 return string();
171         return it->second;
172 }
173
174
175 namespace {
176
177 void TooltipTimerCB(FL_OBJECT * timer, long data)
178 {
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);
183
184         string const help = tooltip->get(ob);
185         if (help.empty())
186                 return;
187
188         fl_show_oneliner(help.c_str(),
189                          form->x + ob->x, form->y + ob->y + ob->h);
190 }
191
192
193 // post_handler for tooltip help
194 int TooltipHandler(FL_OBJECT * ob, int event)
195 {
196         if (!Tooltips::enabled())
197                 return 0;
198
199         lyx::Assert(ob);
200         FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
201         lyx::Assert(timer);
202
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,
206                                        C_TooltipTimerCB,
207                                        reinterpret_cast<long>(ob));
208                 fl_set_timer(timer, 1);
209         }
210         else if (event != FL_MOTION) {
211                 fl_set_timer(timer, 0);
212                 fl_hide_oneliner();
213         }
214         return 0;
215 }
216
217 } // namespace anon
218
219 #endif // FL_REVISION >= 89