]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Tooltips.C
having broken Rob's word count update, I guess I should fix it too ;-)
[lyx.git] / src / frontends / xforms / Tooltips.C
1 /**
2  * \file Tooltips.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
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.
15  */
16
17 #include <config.h>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "Tooltips.h"
24 #include "xforms_helpers.h" // formatted
25 #include "gettext.h"
26 #include "support/lstrings.h"
27 #include "support/LAssert.h"
28 #include FORMS_H_LOCATION
29
30 #include <boost/bind.hpp>
31
32 bool Tooltips::enabled_ = true;
33
34 boost::signal0<void> Tooltips::toggled;
35
36
37 #if FL_VERSION > 0 || FL_REVISION >= 89
38
39 Tooltips::Tooltips()
40 {
41         toggled.connect(boost::bind(&Tooltips::set, this));
42 }
43
44
45 void Tooltips::toggleEnabled()
46 {
47         enabled_ = !enabled_;
48         toggled();
49 }
50
51
52 void Tooltips::set()
53 {
54         if (tooltipsMap.empty())
55                 return;
56
57         TooltipsMap::const_iterator it  = tooltipsMap.begin();
58         TooltipsMap::const_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         // Store the tooltip string
72         string const str = formatted(trim(tip), 400);
73         tooltipsMap[ob] = str;
74
75         // Set the tooltip
76         char const * const c_str = enabled_ ? str.c_str() : 0;
77         fl_set_object_helper(ob, c_str);
78 }
79
80
81 #else // if FL_REVISION < 89
82
83 namespace {
84
85 int TooltipHandler(FL_OBJECT *ob, int event);
86
87 void TooltipTimerCB(FL_OBJECT * timer, long data);
88
89 }
90
91 extern "C" {
92
93 static int C_TooltipHandler(FL_OBJECT * ob, int event,
94                                     FL_Coord, FL_Coord, int, void *)
95 {
96         return TooltipHandler(ob, event);
97 }
98
99
100 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
101 {
102         TooltipTimerCB(ob, data);
103 }
104
105 }
106
107
108 Tooltips::Tooltips()
109         : tooltip_timer_(0)
110 {
111         toggled.connect(boost::bind(&Tooltips::set, this));
112 }
113
114
115 void Tooltips::toggleEnabled()
116 {
117         enabled_ = !enabled_;
118         toggled();
119 }
120
121
122 void Tooltips::set()
123 {}
124
125
126 void Tooltips::init(FL_OBJECT * ob, string const & tip)
127 {
128         lyx::Assert(ob && ob->form);
129
130         // Paranoia check!
131         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
132         if (it != tooltipsMap.end())
133                 return;
134
135         string const str = trim(tip);
136         if (str.empty())
137                 return;
138
139         // Store the tooltip string
140         tooltipsMap[ob] = formatted(str, 400);
141
142         if (!tooltip_timer_) {
143                 if (fl_current_form && ob->form != fl_current_form)
144                         fl_end_form();
145
146                 bool const open_form = !fl_current_form;
147                 if (open_form)
148                         fl_addto_form(ob->form);
149
150                 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
151
152                 if (open_form)
153                         fl_end_form();
154         }
155
156         fl_set_object_posthandler(ob, C_TooltipHandler);
157         ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
158         tooltip_timer_->u_vdata = this;
159 }
160
161
162 string const Tooltips::get(FL_OBJECT * ob) const
163 {
164         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
165         if (it == tooltipsMap.end())
166                 return string();
167         return it->second;
168 }
169
170
171 namespace {
172
173 void TooltipTimerCB(FL_OBJECT * timer, long data)
174 {
175         FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
176         lyx::Assert(ob && ob->form && timer && timer->u_vdata);
177         FL_FORM * form = ob->form;
178         Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
179
180         string const help = tooltip->get(ob);
181         if (help.empty())
182                 return;
183
184         fl_show_oneliner(help.c_str(),
185                          form->x + ob->x, form->y + ob->y + ob->h);
186 }
187
188
189 // post_handler for tooltip help
190 int TooltipHandler(FL_OBJECT * ob, int event)
191 {
192         if (!Tooltips::enabled())
193                 return 0;
194
195         lyx::Assert(ob);
196         FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
197         lyx::Assert(timer);
198
199         // We do not test for empty help here, since this can never happen
200         if (event == FL_ENTER) {
201                 fl_set_object_callback(timer,
202                                        C_TooltipTimerCB,
203                                        reinterpret_cast<long>(ob));
204                 fl_set_timer(timer, 1);
205         }
206         else if (event != FL_MOTION) {
207                 fl_set_timer(timer, 0);
208                 fl_hide_oneliner();
209         }
210         return 0;
211 }
212
213 } // namespace anon
214
215 #endif // FL_REVISION >= 89