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