]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Tooltips.C
Yet more dialog tweaking from Rob.
[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                 // There are no objects with tooltips in this dialog, so
56                 // just go away. Don't change the cursor to a question mark.
57                 return;
58
59         TooltipsMap::iterator it  = tooltipsMap.begin();
60         TooltipsMap::iterator end = tooltipsMap.end();
61         for (; it != end; ++it) {
62                 FL_OBJECT * const ob = it->first;
63                 char const * const c_str = enabled_ ? it->second.c_str() : 0;
64                 fl_set_object_helper(ob, c_str);
65         }
66 }
67
68
69 void Tooltips::init(FL_OBJECT * ob, string const & tip)
70 {
71         lyx::Assert(ob && ob->form);
72
73         // Paranoia check!
74         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
75         if (it != tooltipsMap.end())
76                 return;
77
78         string const str = trim(tip);
79         if (str.empty())
80                 return;
81
82         // Store the tooltip string
83         tooltipsMap[ob] = formatted(str, 400);
84 }
85
86
87 #else // if FL_REVISION < 89
88
89 namespace {
90
91 int TooltipHandler(FL_OBJECT *ob, int event);
92
93 void TooltipTimerCB(FL_OBJECT * timer, long data);
94
95 }
96
97 extern "C" {
98
99 static int C_TooltipHandler(FL_OBJECT * ob, int event,
100                                     FL_Coord, FL_Coord, int, void *)
101 {
102         return TooltipHandler(ob, event);
103 }
104
105
106 static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
107 {
108         TooltipTimerCB(ob, data);
109 }
110
111 }
112
113
114 Tooltips::Tooltips()
115         : tooltip_timer_(0)
116 {
117         toggled.connect(boost::bind(&Tooltips::set, this));
118 }
119
120
121 void Tooltips::toggleEnabled()
122 {
123         enabled_ = !enabled_;
124         toggled();
125 }
126
127
128 void Tooltips::set()
129 {}
130
131
132 void Tooltips::init(FL_OBJECT * ob, string const & tip)
133 {
134         lyx::Assert(ob && ob->form);
135
136         // Paranoia check!
137         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
138         if (it != tooltipsMap.end())
139                 return;
140
141         string const str = trim(tip);
142         if (str.empty())
143                 return;
144
145         // Store the tooltip string
146         tooltipsMap[ob] = formatted(str, 400);
147
148         if (!tooltip_timer_) {
149                 if (fl_current_form && ob->form != fl_current_form)
150                         fl_end_form();
151
152                 bool const open_form = !fl_current_form;
153                 if (open_form)
154                         fl_addto_form(ob->form);
155
156                 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
157
158                 if (open_form)
159                         fl_end_form();
160         }
161
162         fl_set_object_posthandler(ob, C_TooltipHandler);
163         ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
164         tooltip_timer_->u_vdata = this;
165 }
166
167
168 string const Tooltips::get(FL_OBJECT * ob) const
169 {
170         TooltipsMap::const_iterator it = tooltipsMap.find(ob);
171         if (it == tooltipsMap.end())
172                 return string();
173         return it->second;
174 }
175
176
177 namespace {
178
179 void TooltipTimerCB(FL_OBJECT * timer, long data)
180 {
181         FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
182         lyx::Assert(ob && ob->form && timer && timer->u_vdata);
183         FL_FORM * form = ob->form;
184         Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
185
186         string const help = tooltip->get(ob);
187         if (help.empty())
188                 return;
189
190         fl_show_oneliner(help.c_str(),
191                          form->x + ob->x, form->y + ob->y + ob->h);
192 }
193
194
195 // post_handler for tooltip help
196 int TooltipHandler(FL_OBJECT * ob, int event)
197 {
198         if (!Tooltips::enabled())
199                 return 0;
200
201         lyx::Assert(ob);
202         FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
203         lyx::Assert(timer);
204
205         // We do not test for empty help here, since this can never happen
206         if (event == FL_ENTER) {
207                 fl_set_object_callback(timer,
208                                        C_TooltipTimerCB,
209                                        reinterpret_cast<long>(ob));
210                 fl_set_timer(timer, 1);
211         }
212         else if (event != FL_MOTION) {
213                 fl_set_timer(timer, 0);
214                 fl_hide_oneliner();
215         }
216         return 0;
217 }
218
219 } // namespace anon
220
221 #endif // FL_REVISION >= 89