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