]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
fix tooltips in toolbar
[lyx.git] / src / frontends / xforms / Toolbar_pimpl.C
1 /**
2  * \file Toolbar_pimpl.C
3  * Copyright 1995 Matthias Ettrich
4  * Copyright 1995-2001 The LyX Team.
5  * Copyright 1996-1998 Lars Gullik Bjønnes
6  * See the file COPYING.
7  *
8  * \author Lars Gullik Bjønnes, larsbj@lyx.org
9  */
10
11 //  Added pseudo-action handling, asierra 180296
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "Toolbar_pimpl.h"
20 #include "debug.h"
21 #include "XFormsView.h"
22 #include "lyxfunc.h"
23 #include "FuncStatus.h"
24 #include "BufferView.h"
25 #include "buffer.h"
26 #include "LyXAction.h"
27 #include "MathsSymbols.h"
28 #include "gettext.h"
29 #include "Tooltips.h"
30 #include FORMS_H_LOCATION
31 #include "combox.h"
32 #include "ToolbarDefaults.h"
33
34 #include "support/LAssert.h"
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
37
38 using std::endl;
39
40 extern LyXAction lyxaction;
41
42 // some constants
43 const int standardspacing = 2; // the usual space between items
44 const int sepspace = 6; // extra space
45 const int buttonwidth = 30; // the standard button width
46 const int height = 30; // the height of all items in the toolbar
47
48 Toolbar::Pimpl::toolbarItem::toolbarItem()
49         : action(LFUN_NOACTION), icon(0)
50 {
51 }
52
53
54 Toolbar::Pimpl::toolbarItem::~toolbarItem()
55 {
56         // Lars said here that ~XFormsView() dealt with the icons.
57         // This is not true. But enabling this causes crashes,
58         // because somehow we kill the same icon twice :(
59         // FIXME
60         //kill_icon();
61 }
62
63
64 void Toolbar::Pimpl::toolbarItem::kill_icon()
65 {
66         if (icon) {
67                 fl_delete_object(icon);
68                 fl_free_object(icon);
69                 icon = 0;
70         }
71 }
72
73
74 Toolbar::Pimpl::toolbarItem &
75 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti)
76 {
77         if (this == &ti)
78                 return *this;
79
80         // If we already have an icon, release it.
81         // But we don't copy the icon from ti
82         kill_icon();
83  
84         action = ti.action;
85
86         return *this;
87 }
88
89
90
91 Toolbar::Pimpl::Pimpl(LyXView * o, Dialogs & d, int x, int y)
92         : owner_(static_cast<XFormsView *>(o)), xpos(x), ypos(y)
93 {
94         combox_ = 0;
95         tooltip_ = new Tooltips(d);
96 }
97
98
99 Toolbar::Pimpl::~Pimpl()
100 {
101         fl_freeze_form(owner_->getForm());
102
103         // G++ vector does not have clear defined
104         //toollist.clear();
105         toollist_.erase(toollist_.begin(), toollist_.end());
106
107         delete combox_;
108
109         fl_unfreeze_form(owner_->getForm());
110         delete tooltip_;
111 }
112
113
114 void Toolbar::Pimpl::update()
115 {
116         ToolbarList::const_iterator p = toollist_.begin();
117         ToolbarList::const_iterator end = toollist_.end();
118         for (; p != end; ++p) {
119                 if (p->action == ToolbarDefaults::LAYOUTS && combox_) {
120                         if (owner_->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled())
121                                 combox_->deactivate();
122                         else
123                                 combox_->activate();
124                         continue;
125                 }
126
127                 if (!p->icon)
128                         continue;
129  
130                 FuncStatus const status = owner_->getLyXFunc()->getStatus(p->action);
131                 if (status.onoff(true)) {
132                         // I'd like to use a different color
133                         // here, but then the problem is to
134                         // know how to use transparency with
135                         // Xpm library. It seems pretty
136                         // complicated to me (JMarc)
137                         fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
138                         fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
139                 } else {
140                         fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
141                         fl_set_object_boxtype(p->icon, FL_UP_BOX);
142                 }
143                 if (status.disabled()) {
144                         // Is there a way here to specify a
145                         // mask in order to show that the
146                         // button is disabled? (JMarc)
147                         fl_deactivate_object(p->icon);
148                 }
149                 else
150                         fl_activate_object(p->icon);
151         }
152 }
153
154
155 // this one is not "C" because combox callbacks are really C++ %-|
156 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
157 {
158         Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
159
160         tb->layoutSelected();
161 }
162
163
164 void Toolbar::Pimpl::layoutSelected()
165 {
166         string const & layoutguiname = combox_->getline();
167         LyXTextClass const & tc =
168                 owner_->buffer()->params.getLyXTextClass();
169
170         LyXTextClass::const_iterator end = tc.end();
171         for (LyXTextClass::const_iterator cit = tc.begin();
172              cit != end; ++cit) {
173                 if (_((*cit)->name()) == layoutguiname) {
174                         owner_->getLyXFunc()->dispatch(LFUN_LAYOUT, (*cit)->name());
175                         return;
176                 }
177         }
178         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
179                << endl;
180 }
181
182
183 void Toolbar::Pimpl::setLayout(string const & layout)
184 {
185         if (combox_) {
186                 LyXTextClass const & tc =
187                         owner_->buffer()->params.getLyXTextClass();
188                 combox_->select(_(tc[layout]->name()));
189         }
190 }
191
192
193 void Toolbar::Pimpl::updateLayoutList(bool force)
194 {
195         // Update the layout display
196         if (!combox_) return;
197
198         // If textclass is different, we need to update the list
199         if (combox_->empty() || force) {
200                 combox_->clear();
201                 LyXTextClass const & tc =
202                         owner_->buffer()->params.getLyXTextClass();
203                 LyXTextClass::const_iterator end = tc.end();
204                 for (LyXTextClass::const_iterator cit = tc.begin();
205                      cit != end; ++cit) {
206                         // ignore obsolete entries
207                         if ((*cit)->obsoleted_by().empty())
208                                 combox_->addline(_((*cit)->name()));
209                 }
210         }
211         // we need to do this.
212         combox_->redraw();
213 }
214
215
216 void Toolbar::Pimpl::clearLayoutList()
217 {
218         if (combox_) {
219                 combox_->clear();
220                 combox_->redraw();
221         }
222 }
223
224
225 void Toolbar::Pimpl::openLayoutList()
226 {
227         if (combox_)
228                 combox_->show();
229 }
230
231
232 namespace {
233
234 void ToolbarCB(FL_OBJECT * ob, long ac)
235 {
236         XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
237
238         owner->getLyXFunc()->dispatch(int(ac), true);
239 }
240
241
242 extern "C" {
243
244         static
245         void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
246         {
247                 ToolbarCB(ob, data);
248         }
249
250 }
251
252
253 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
254 {
255         string arg;
256         string xpm_name;
257
258         const kb_action act = lyxaction.retrieveActionArg(action, arg);
259         string const name = lyxaction.getActionName(act);
260         if (!arg.empty())
261                 xpm_name = subst(name + ' ' + arg, ' ','_');
262         else
263                 xpm_name = name;
264
265         string fullname = LibFileSearch("images", xpm_name, "xpm");
266
267         if (!fullname.empty()) {
268                 lyxerr[Debug::GUI] << "Full icon name is `"
269                                    << fullname << "'" << endl;
270                 fl_set_pixmapbutton_file(obj, fullname.c_str());
271                 return;
272         }
273
274         if (act == LFUN_INSERT_MATH && !arg.empty()) {
275                 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
276                                                               buttonwidth,
277                                                               height);
278                 if (pixmap) {
279                         lyxerr[Debug::GUI] << "Using mathed-provided icon"
280                                            << endl;
281                         fl_set_pixmapbutton_data(obj,
282                                                  const_cast<char **>(pixmap));
283                         return;
284                 }
285         }
286
287         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
288         fullname = LibFileSearch("images", "unknown", "xpm");
289         if (!fullname.empty()) {
290                 lyxerr[Debug::GUI] << "Using default `unknown' icon"
291                                    << endl;
292                 fl_set_pixmapbutton_file(obj, fullname.c_str());
293         }
294 }
295
296 } // namespace anon
297
298
299 void Toolbar::Pimpl::add(int action)
300 {
301         FL_OBJECT * obj;
302
303         toolbarItem item;
304         item.action = action;
305  
306         switch (action) {
307         case ToolbarDefaults::SEPARATOR:
308                 xpos += sepspace;
309                 break;
310         case ToolbarDefaults::NEWLINE:
311                 // Not supported yet.
312                 break;
313         case ToolbarDefaults::LAYOUTS:
314                 xpos += standardspacing;
315                 if (!combox_)
316                         combox_ = new Combox(FL_COMBOX_DROPLIST);
317                 combox_->add(xpos, ypos, 135, height, 400);
318                 combox_->setcallback(layoutSelectedCB, this);
319                 combox_->resize(FL_RESIZE_ALL);
320                 combox_->gravity(NorthWestGravity, NorthWestGravity);
321                 xpos += 135;
322                 break;
323         default:
324                 xpos += standardspacing;
325                 item.icon = obj =
326                         fl_add_pixmapbutton(FL_NORMAL_BUTTON,
327                                             xpos, ypos,
328                                             buttonwidth,
329                                             height, "");
330                 fl_set_object_resize(obj, FL_RESIZE_ALL);
331                 fl_set_object_gravity(obj,
332                                       NorthWestGravity,
333                                       NorthWestGravity);
334                 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
335                                        static_cast<long>(action));
336                 // Remove the blue feedback rectangle
337                 fl_set_pixmapbutton_focus_outline(obj, 0);
338
339                 // initialise the tooltip
340                 string const tip = _(lyxaction.helpText(obj->argument));
341                 tooltip_->init(obj, tip);
342
343                 // The view that this object belongs to.
344                 obj->u_vdata = owner_;
345
346                 setPixmap(obj, action, buttonwidth, height);
347                 // we must remember to update the positions
348                 xpos += buttonwidth;
349                 // ypos is constant
350                 /* Here will come a check to see if the new
351                  * pos is within the bounds of the main frame,
352                  * and perhaps wrap the toolbar if not.
353                  */
354                 break;
355         }
356  
357         toollist_.push_back(item);
358 }