]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
Really dull and boring header shit
[lyx.git] / src / frontends / xforms / Toolbar_pimpl.C
1 /**
2  * \file Toolbar_pimpl.C
3  * Copyright 1995 Matthias Ettrich
4  * Copyright 1996-1998 Lars Gullik Bjønnes
5  * See the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 //  Added pseudo-action handling, asierra 180296
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "Toolbar_pimpl.h"
21 #include "debug.h"
22 #include "XFormsView.h"
23 #include "lyxfunc.h"
24 #include "FuncStatus.h"
25 #include "buffer.h"
26 #include "funcrequest.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)
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 (ev.action == LFUN_INSERT_MATH && !ev.argument.empty()) {
262                 string arg = ev.argument.substr(1);
263                 fullname = LibFileSearch("images/math/", arg, "xpm");
264         }
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         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
274         fullname = LibFileSearch("images", "unknown", "xpm");
275         if (!fullname.empty()) {
276                 lyxerr[Debug::GUI] << "Using default `unknown' icon"
277                                    << endl;
278                 fl_set_pixmapbutton_file(obj, fullname.c_str());
279         }
280 }
281
282 } // namespace anon
283
284
285 void Toolbar::Pimpl::add(int action)
286 {
287         FL_OBJECT * obj;
288
289         toolbarItem item;
290         item.action = action;
291  
292         switch (action) {
293         case ToolbarDefaults::SEPARATOR:
294                 xpos += sepspace;
295                 break;
296         case ToolbarDefaults::NEWLINE:
297                 // Not supported yet.
298                 break;
299         case ToolbarDefaults::LAYOUTS:
300                 xpos += standardspacing;
301                 if (!combox_)
302                         combox_ = new Combox(FL_COMBOX_DROPLIST);
303                 combox_->add(xpos, ypos, 135, height, 400);
304                 combox_->setcallback(layoutSelectedCB, this);
305                 combox_->resize(FL_RESIZE_ALL);
306                 combox_->gravity(NorthWestGravity, NorthWestGravity);
307                 xpos += 135;
308                 break;
309         default:
310                 xpos += standardspacing;
311                 item.icon = obj =
312                         fl_add_pixmapbutton(FL_NORMAL_BUTTON,
313                                             xpos, ypos,
314                                             buttonwidth,
315                                             height, "");
316                 fl_set_object_resize(obj, FL_RESIZE_ALL);
317                 fl_set_object_gravity(obj,
318                                       NorthWestGravity,
319                                       NorthWestGravity);
320                 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
321                                        static_cast<long>(action));
322                 // Remove the blue feedback rectangle
323                 fl_set_pixmapbutton_focus_outline(obj, 0);
324
325                 // initialise the tooltip
326                 string const tip = _(lyxaction.helpText(obj->argument));
327                 tooltip_->init(obj, tip);
328
329                 // The view that this object belongs to.
330                 obj->u_vdata = owner_;
331
332                 setPixmap(obj, action);
333                 // we must remember to update the positions
334                 xpos += buttonwidth;
335                 // ypos is constant
336                 /* Here will come a check to see if the new
337                  * pos is within the bounds of the main frame,
338                  * and perhaps wrap the toolbar if not.
339                  */
340                 break;
341         }
342  
343         toollist_.push_back(item);
344 }