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