]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
remove defaults stuff, let Qt handle no toolbar
[lyx.git] / src / frontends / xforms / Toolbar_pimpl.C
1 /**
2  * \file xforms/Toolbar_pimpl.C
3  * Copyright 1995 Matthias Ettrich
4  * Copyright 1996-1998 Lars Gullik Bjønnes
5  * This file is part of LyX, the document processor.
6  * Licence details can be found in the file COPYING.
7  *
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 //  Added pseudo-action handling, asierra 180296
14
15 #include <config.h>
16
17
18 #include "Toolbar_pimpl.h"
19 #include "debug.h"
20 #include "XFormsView.h"
21 #include "lyxfunc.h"
22 #include "FuncStatus.h"
23 #include "buffer.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "Tooltips.h"
27 #include FORMS_H_LOCATION
28 #include "combox.h"
29 #include "ToolbarBackend.h"
30 #include "xforms_helpers.h"
31
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)), combox_(0), xpos(x), ypos(y)
90 {
91         tooltip_ = new Tooltips();
92 }
93
94
95 Toolbar::Pimpl::~Pimpl()
96 {
97         fl_freeze_form(owner_->getForm());
98
99         // G++ vector does not have clear defined
100         //toollist.clear();
101         toollist_.erase(toollist_.begin(), toollist_.end());
102
103         fl_unfreeze_form(owner_->getForm());
104         delete tooltip_;
105 }
106
107
108 void Toolbar::Pimpl::update()
109 {
110         ToolbarList::const_iterator p = toollist_.begin();
111         ToolbarList::const_iterator end = toollist_.end();
112         for (; p != end; ++p) {
113                 if (p->action == ToolbarBackend::LAYOUTS && combox_) {
114                         LyXFunc const & lf = owner_->getLyXFunc();
115                         bool const disable =
116                                 lf.getStatus(LFUN_LAYOUT).disabled();
117                         setEnabled(combox_, !disable);
118                         continue;
119                 }
120
121                 if (!p->icon)
122                         continue;
123
124                 FuncStatus const status = owner_->getLyXFunc().getStatus(p->action);
125                 if (status.onoff(true)) {
126                         // I'd like to use a different color
127                         // here, but then the problem is to
128                         // know how to use transparency with
129                         // Xpm library. It seems pretty
130                         // complicated to me (JMarc)
131                         fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
132                         fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
133                 } else {
134                         fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
135                         fl_set_object_boxtype(p->icon, FL_UP_BOX);
136                 }
137                 if (status.disabled()) {
138                         // Is there a way here to specify a
139                         // mask in order to show that the
140                         // button is disabled? (JMarc)
141                         fl_deactivate_object(p->icon);
142                 }
143                 else
144                         fl_activate_object(p->icon);
145         }
146 }
147
148
149 namespace {
150
151 void C_layoutSelectedCB(FL_OBJECT * ob, long)
152 {
153         if (!ob || !ob->u_vdata)
154                 return;
155         Toolbar::Pimpl * ptr = static_cast<Toolbar::Pimpl *>(ob->u_vdata);
156         ptr->layoutSelected();
157 }
158
159 } // namespace anon
160
161
162 void Toolbar::Pimpl::layoutSelected()
163 {
164         if (!combox_)
165                 return;
166
167         string const & layoutguiname = getString(combox_);
168         LyXTextClass const & tc =
169                 owner_->buffer()->params.getLyXTextClass();
170
171         LyXTextClass::const_iterator end = tc.end();
172         for (LyXTextClass::const_iterator cit = tc.begin();
173              cit != end; ++cit) {
174                 if (_((*cit)->name()) == layoutguiname) {
175                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
176                         return;
177                 }
178         }
179         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
180                << endl;
181 }
182
183
184 void Toolbar::Pimpl::setLayout(string const & layout)
185 {
186         if (!combox_)
187                 return;
188
189         LyXTextClass const & tc = owner_->buffer()->params.getLyXTextClass();
190         string const layoutname = _(tc[layout]->name());
191
192         int const nnames = fl_get_combox_maxitems(combox_);
193         for (int i = 1; i <= nnames; ++i) {
194                 string const name = fl_get_combox_line(combox_, i);
195                 if (name == layoutname) {
196                         fl_set_combox(combox_, i);
197                         break;
198                 }
199         }
200 }
201
202
203 void Toolbar::Pimpl::updateLayoutList(bool force)
204 {
205         if (!combox_)
206                 return;
207
208         // If textclass is different, we need to update the list
209         if (fl_get_combox_maxitems(combox_) == 0 || force) {
210                 fl_clear_combox(combox_);
211                 LyXTextClass const & tc =
212                         owner_->buffer()->params.getLyXTextClass();
213                 LyXTextClass::const_iterator end = tc.end();
214                 for (LyXTextClass::const_iterator cit = tc.begin();
215                      cit != end; ++cit) {
216                         // ignore obsolete entries
217                         if ((*cit)->obsoleted_by().empty()) {
218                                 string const & name = _((*cit)->name());
219                                 fl_addto_combox(combox_, name.c_str());
220                         }
221                 }
222         }
223         // we need to do this.
224         fl_redraw_object(combox_);
225 }
226
227
228 void Toolbar::Pimpl::clearLayoutList()
229 {
230         if (!combox_)
231                 return;
232
233         fl_clear_combox(combox_);
234         fl_redraw_object(combox_);
235 }
236
237
238 void Toolbar::Pimpl::openLayoutList()
239 {
240         if (!combox_)
241                 return;
242
243         fl_show_combox_browser(combox_);
244 }
245
246
247 namespace {
248
249 void ToolbarCB(FL_OBJECT * ob, long ac)
250 {
251         XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
252
253         owner->getLyXFunc().dispatch(int(ac), true);
254 }
255
256
257 extern "C" {
258
259 void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
260 {
261         ToolbarCB(ob, data);
262 }
263
264 }
265
266
267 void setPixmap(FL_OBJECT * obj, int action)
268 {
269         string xpm_name;
270         FuncRequest ev = lyxaction.retrieveActionArg(action);
271
272         string const name = lyxaction.getActionName(ev.action);
273         if (!ev.argument.empty())
274                 xpm_name = subst(name + ' ' + ev.argument, ' ','_');
275         else
276                 xpm_name = name;
277
278         string fullname = LibFileSearch("images", xpm_name, "xpm");
279
280         if (ev.action == LFUN_INSERT_MATH && !ev.argument.empty()) {
281                 string arg = ev.argument.substr(1);
282                 fullname = LibFileSearch("images/math/", arg, "xpm");
283         }
284
285         if (!fullname.empty()) {
286                 lyxerr[Debug::GUI] << "Full icon name is `"
287                                    << fullname << '\'' << endl;
288                 fl_set_pixmapbutton_file(obj, fullname.c_str());
289                 return;
290         }
291
292         lyxerr << "Unable to find icon `" << xpm_name << '\'' << endl;
293         fullname = LibFileSearch("images", "unknown", "xpm");
294         if (!fullname.empty()) {
295                 lyxerr[Debug::GUI] << "Using default `unknown' icon"
296                                    << endl;
297                 fl_set_pixmapbutton_file(obj, fullname.c_str());
298         }
299 }
300
301 } // namespace anon
302
303
304 void Toolbar::Pimpl::add(int action)
305 {
306         toolbarItem item;
307         item.action = action;
308
309         switch (action) {
310         case ToolbarBackend::SEPARATOR:
311                 xpos += sepspace;
312                 break;
313         case ToolbarBackend::NEWLINE:
314                 // Not supported yet.
315                 break;
316         case ToolbarBackend::LAYOUTS:
317                 xpos += standardspacing;
318                 if (combox_)
319                         break;
320
321                 combox_ = fl_add_combox(FL_DROPLIST_COMBOX,
322                                         xpos, ypos, 135, height, "");
323                 fl_set_combox_browser_height(combox_, 400);
324                 fl_set_object_boxtype(combox_, FL_DOWN_BOX);
325                 fl_set_object_color(combox_, FL_MCOL, FL_MCOL);
326                 fl_set_object_gravity(combox_, FL_NorthWest, FL_NorthWest);
327                 fl_set_object_resize(combox_, FL_RESIZE_ALL);
328
329                 combox_->u_vdata = this;
330                 fl_set_object_callback(combox_, C_layoutSelectedCB, 0);
331                 xpos += 135;
332                 break;
333         default: {
334                 FL_OBJECT * obj;
335
336                 xpos += standardspacing;
337                 item.icon = obj =
338                         fl_add_pixmapbutton(FL_NORMAL_BUTTON,
339                                             xpos, ypos,
340                                             buttonwidth,
341                                             height, "");
342                 fl_set_object_resize(obj, FL_RESIZE_ALL);
343                 fl_set_object_gravity(obj,
344                                       NorthWestGravity,
345                                       NorthWestGravity);
346                 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
347                                        static_cast<long>(action));
348                 // Remove the blue feedback rectangle
349                 fl_set_pixmapbutton_focus_outline(obj, 0);
350
351                 // initialise the tooltip
352                 string const tip = _(lyxaction.helpText(obj->argument));
353                 tooltip_->init(obj, tip);
354
355                 // The view that this object belongs to.
356                 obj->u_vdata = owner_;
357
358                 setPixmap(obj, action);
359                 // we must remember to update the positions
360                 xpos += buttonwidth;
361                 // ypos is constant
362                 /* Here will come a check to see if the new
363                  * pos is within the bounds of the main frame,
364                  * and perhaps wrap the toolbar if not.
365                  */
366                 break;
367         }
368         }
369
370         toollist_.push_back(item);
371 }