]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
small cleanups
[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 "buffer.h"
25 #include "funcrequest.h"
26 #include "MathsSymbols.h"
27 #include "gettext.h"
28 #include "Tooltips.h"
29 #include FORMS_H_LOCATION
30 #include "combox.h"
31 #include "ToolbarDefaults.h"
32 #include "LyXAction.h"
33
34 #include "support/LAssert.h"
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
37
38 #include <boost/tuple/tuple.hpp>
39  
40 using std::endl;
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, int x, int y)
92         : owner_(static_cast<XFormsView *>(o)), xpos(x), ypos(y)
93 {
94         combox_ = 0;
95         tooltip_ = new Tooltips();
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         reinterpret_cast<Toolbar::Pimpl *>(arg)->layoutSelected();
159 }
160
161
162 void Toolbar::Pimpl::layoutSelected()
163 {
164         string const & layoutguiname = combox_->getline();
165         LyXTextClass const & tc =
166                 owner_->buffer()->params.getLyXTextClass();
167
168         LyXTextClass::const_iterator end = tc.end();
169         for (LyXTextClass::const_iterator cit = tc.begin();
170              cit != end; ++cit) {
171                 if (_((*cit)->name()) == layoutguiname) {
172                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()));
173                         return;
174                 }
175         }
176         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
177                << endl;
178 }
179
180
181 void Toolbar::Pimpl::setLayout(string const & layout)
182 {
183         if (combox_) {
184                 LyXTextClass const & tc =
185                         owner_->buffer()->params.getLyXTextClass();
186                 combox_->select(_(tc[layout]->name()));
187         }
188 }
189
190
191 void Toolbar::Pimpl::updateLayoutList(bool force)
192 {
193         // Update the layout display
194         if (!combox_) return;
195
196         // If textclass is different, we need to update the list
197         if (combox_->empty() || force) {
198                 combox_->clear();
199                 LyXTextClass const & tc =
200                         owner_->buffer()->params.getLyXTextClass();
201                 LyXTextClass::const_iterator end = tc.end();
202                 for (LyXTextClass::const_iterator cit = tc.begin();
203                      cit != end; ++cit) {
204                         // ignore obsolete entries
205                         if ((*cit)->obsoleted_by().empty())
206                                 combox_->addline(_((*cit)->name()));
207                 }
208         }
209         // we need to do this.
210         combox_->redraw();
211 }
212
213
214 void Toolbar::Pimpl::clearLayoutList()
215 {
216         if (combox_) {
217                 combox_->clear();
218                 combox_->redraw();
219         }
220 }
221
222
223 void Toolbar::Pimpl::openLayoutList()
224 {
225         if (combox_)
226                 combox_->show();
227 }
228
229
230 namespace {
231
232 void ToolbarCB(FL_OBJECT * ob, long ac)
233 {
234         XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
235
236         owner->getLyXFunc().dispatch(int(ac), true);
237 }
238
239
240 extern "C" {
241
242         static
243         void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
244         {
245                 ToolbarCB(ob, data);
246         }
247
248 }
249
250
251 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
252 {
253         string xpm_name;
254         FuncRequest ev = lyxaction.retrieveActionArg(action);
255  
256         string const name = lyxaction.getActionName(ev.action);
257         if (!ev.argument.empty())
258                 xpm_name = subst(name + ' ' + ev.argument, ' ','_');
259         else
260                 xpm_name = name;
261
262         string fullname = LibFileSearch("images", xpm_name, "xpm");
263
264         if (!fullname.empty()) {
265                 lyxerr[Debug::GUI] << "Full icon name is `"
266                                    << fullname << "'" << endl;
267                 fl_set_pixmapbutton_file(obj, fullname.c_str());
268                 return;
269         }
270
271         if (ev.action == LFUN_INSERT_MATH && !ev.argument.empty()) {
272                 char const ** pixmap = get_pixmap_from_symbol(ev.argument.c_str(),
273                                                               buttonwidth,
274                                                               height);
275                 if (pixmap) {
276                         lyxerr[Debug::GUI] << "Using mathed-provided icon"
277                                            << endl;
278                         fl_set_pixmapbutton_data(obj,
279                                                  const_cast<char **>(pixmap));
280                         return;
281                 }
282         }
283
284         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
285         fullname = LibFileSearch("images", "unknown", "xpm");
286         if (!fullname.empty()) {
287                 lyxerr[Debug::GUI] << "Using default `unknown' icon"
288                                    << endl;
289                 fl_set_pixmapbutton_file(obj, fullname.c_str());
290         }
291 }
292
293 } // namespace anon
294
295
296 void Toolbar::Pimpl::add(int action)
297 {
298         FL_OBJECT * obj;
299
300         toolbarItem item;
301         item.action = action;
302  
303         switch (action) {
304         case ToolbarDefaults::SEPARATOR:
305                 xpos += sepspace;
306                 break;
307         case ToolbarDefaults::NEWLINE:
308                 // Not supported yet.
309                 break;
310         case ToolbarDefaults::LAYOUTS:
311                 xpos += standardspacing;
312                 if (!combox_)
313                         combox_ = new Combox(FL_COMBOX_DROPLIST);
314                 combox_->add(xpos, ypos, 135, height, 400);
315                 combox_->setcallback(layoutSelectedCB, this);
316                 combox_->resize(FL_RESIZE_ALL);
317                 combox_->gravity(NorthWestGravity, NorthWestGravity);
318                 xpos += 135;
319                 break;
320         default:
321                 xpos += standardspacing;
322                 item.icon = obj =
323                         fl_add_pixmapbutton(FL_NORMAL_BUTTON,
324                                             xpos, ypos,
325                                             buttonwidth,
326                                             height, "");
327                 fl_set_object_resize(obj, FL_RESIZE_ALL);
328                 fl_set_object_gravity(obj,
329                                       NorthWestGravity,
330                                       NorthWestGravity);
331                 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
332                                        static_cast<long>(action));
333                 // Remove the blue feedback rectangle
334                 fl_set_pixmapbutton_focus_outline(obj, 0);
335
336                 // initialise the tooltip
337                 string const tip = _(lyxaction.helpText(obj->argument));
338                 tooltip_->init(obj, tip);
339
340                 // The view that this object belongs to.
341                 obj->u_vdata = owner_;
342
343                 setPixmap(obj, action, buttonwidth, height);
344                 // we must remember to update the positions
345                 xpos += buttonwidth;
346                 // ypos is constant
347                 /* Here will come a check to see if the new
348                  * pos is within the bounds of the main frame,
349                  * and perhaps wrap the toolbar if not.
350                  */
351                 break;
352         }
353  
354         toollist_.push_back(item);
355 }