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