]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XFormsToolbar.C
Strip out another 180 #includes.
[lyx.git] / src / frontends / xforms / XFormsToolbar.C
1 /**
2  * \file XFormsToolbar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
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 #include "XFormsToolbar.h"
17
18 #include "Tooltips.h"
19 #include "xforms_helpers.h"
20 #include "XFormsView.h"
21
22 #include "buffer.h"
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "lyxfunc.h"
28
29 #include "lyx_forms.h"
30 #include "combox.h"
31
32 using std::endl;
33
34
35 // some constants
36 const int standardspacing = 2; // the usual space between items
37 const int sepspace = 6; // extra space
38 const int buttonwidth = 30; // the standard button width
39 const int height = 30; // the height of all items in the toolbar
40
41 XFormsToolbar::toolbarItem::toolbarItem()
42         : action(LFUN_NOACTION), icon(0)
43 {}
44
45
46 XFormsToolbar::toolbarItem::~toolbarItem()
47 {
48         // Lars said here that ~XFormsView() dealt with the icons.
49         // This is not true. But enabling this causes crashes,
50         // because somehow we kill the same icon twice :(
51         // FIXME
52         //kill_icon();
53 }
54
55
56 /// Display toolbar, not implemented. But moved out of line so that
57 /// linking will work properly.
58 void XFormsToolbar::displayToolbar(ToolbarBackend::Toolbar const & /*tb*/,
59                                     bool /*show*/)
60 {}
61
62
63 void XFormsToolbar::toolbarItem::kill_icon()
64 {
65         if (icon) {
66                 fl_delete_object(icon);
67                 fl_free_object(icon);
68                 icon = 0;
69         }
70 }
71
72
73 XFormsToolbar::toolbarItem &
74 XFormsToolbar::toolbarItem::operator=(toolbarItem const & ti)
75 {
76         if (this == &ti)
77                 return *this;
78
79         // If we already have an icon, release it.
80         // But we don't copy the icon from ti
81         kill_icon();
82
83         action = ti.action;
84
85         return *this;
86 }
87
88
89
90 XFormsToolbar::XFormsToolbar(LyXView * o, int x, int y)
91         : owner_(static_cast<XFormsView *>(o)), combox_(0), xpos(x), ypos(y)
92 {
93         tooltip_ = new Tooltips();
94 }
95
96
97 XFormsToolbar::~XFormsToolbar()
98 {
99         fl_freeze_form(owner_->getForm());
100
101         // G++ vector does not have clear defined
102         //toollist.clear();
103         toollist_.erase(toollist_.begin(), toollist_.end());
104
105         fl_unfreeze_form(owner_->getForm());
106         delete tooltip_;
107 }
108
109
110 void XFormsToolbar::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 == ToolbarBackend::LAYOUTS && combox_) {
116                         LyXFunc const & lf = owner_->getLyXFunc();
117                         bool const disable =
118                                 lf.getStatus(LFUN_LAYOUT).disabled();
119                         setEnabled(combox_, !disable);
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 namespace {
152
153 void C_layoutSelectedCB(FL_OBJECT * ob, long)
154 {
155         if (!ob || !ob->u_vdata)
156                 return;
157         XFormsToolbar * ptr = static_cast<XFormsToolbar *>(ob->u_vdata);
158         ptr->layoutSelected();
159 }
160
161 } // namespace anon
162
163
164 void XFormsToolbar::layoutSelected()
165 {
166         if (!combox_)
167                 return;
168
169         string const & layoutguiname = getString(combox_);
170         LyXTextClass const & tc =
171                 owner_->buffer()->params.getLyXTextClass();
172
173         LyXTextClass::const_iterator end = tc.end();
174         for (LyXTextClass::const_iterator cit = tc.begin();
175              cit != end; ++cit) {
176                 if (_((*cit)->name()) == layoutguiname) {
177                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
178                         return;
179                 }
180         }
181         lyxerr << "ERROR (XFormsToolbar::layoutSelected): layout not found!"
182                << endl;
183 }
184
185
186 void XFormsToolbar::setLayout(string const & layout)
187 {
188         if (!combox_)
189                 return;
190
191         LyXTextClass const & tc = owner_->buffer()->params.getLyXTextClass();
192         string const layoutname = _(tc[layout]->name());
193
194         int const nnames = fl_get_combox_maxitems(combox_);
195         for (int i = 1; i <= nnames; ++i) {
196                 string const name = fl_get_combox_line(combox_, i);
197                 if (name == layoutname) {
198                         fl_set_combox(combox_, i);
199                         break;
200                 }
201         }
202 }
203
204
205 void XFormsToolbar::updateLayoutList()
206 {
207         if (!combox_)
208                 return;
209
210         fl_clear_combox(combox_);
211         LyXTextClass const & tc = owner_->buffer()->params.getLyXTextClass();
212         LyXTextClass::const_iterator end = tc.end();
213         for (LyXTextClass::const_iterator cit = tc.begin();
214              cit != end; ++cit) {
215                 // ignore obsolete entries
216                 if ((*cit)->obsoleted_by().empty()) {
217                         string const & name = _((*cit)->name());
218                         fl_addto_combox(combox_, name.c_str());
219                 }
220         }
221
222         // we need to do this.
223         fl_redraw_object(combox_);
224 }
225
226
227 void XFormsToolbar::clearLayoutList()
228 {
229         if (!combox_)
230                 return;
231
232         Toolbar::clearLayoutList();
233         fl_clear_combox(combox_);
234         fl_redraw_object(combox_);
235 }
236
237
238 void XFormsToolbar::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 } // namespace anon
267
268
269 void XFormsToolbar::add(ToolbarBackend::Toolbar const & tb)
270 {
271         // we can only handle one toolbar
272         if (!toollist_.empty())
273                 return;
274
275         ToolbarBackend::item_iterator it = tb.items.begin();
276         ToolbarBackend::item_iterator end = tb.items.end();
277         for (; it != end; ++it)
278                 add(it->first, it->second);
279 }
280
281
282 void XFormsToolbar::add(int action, string const & tooltip)
283 {
284         toolbarItem item;
285         item.action = action;
286
287         switch (action) {
288         case ToolbarBackend::SEPARATOR:
289                 xpos += sepspace;
290                 break;
291         case ToolbarBackend::MINIBUFFER:
292                 // Not implemented
293                 break;
294         case ToolbarBackend::LAYOUTS:
295                 xpos += standardspacing;
296                 if (combox_)
297                         break;
298
299                 combox_ = fl_add_combox(FL_DROPLIST_COMBOX,
300                                         xpos, ypos, 135, height, "");
301                 fl_set_combox_browser_height(combox_, 400);
302                 fl_set_object_boxtype(combox_, FL_DOWN_BOX);
303                 fl_set_object_color(combox_, FL_MCOL, FL_MCOL);
304                 fl_set_object_gravity(combox_, FL_NorthWest, FL_NorthWest);
305                 fl_set_object_resize(combox_, FL_RESIZE_ALL);
306
307                 combox_->u_vdata = this;
308                 fl_set_object_callback(combox_, C_layoutSelectedCB, 0);
309                 xpos += 135;
310                 break;
311         default: {
312                 FL_OBJECT * obj;
313
314                 xpos += standardspacing;
315                 item.icon = obj =
316                         fl_add_pixmapbutton(FL_NORMAL_BUTTON,
317                                             xpos, ypos,
318                                             buttonwidth,
319                                             height, "");
320                 fl_set_object_resize(obj, FL_RESIZE_ALL);
321                 fl_set_object_gravity(obj,
322                                       NorthWestGravity,
323                                       NorthWestGravity);
324                 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
325                                        static_cast<long>(action));
326                 // Remove the blue feedback rectangle
327                 fl_set_pixmapbutton_focus_outline(obj, 0);
328
329                 tooltip_->init(obj, tooltip);
330
331                 // The view that this object belongs to.
332                 obj->u_vdata = owner_;
333
334                 string const xpm = toolbarbackend.getIcon(action);
335                 fl_set_pixmapbutton_file(obj, xpm.c_str());
336
337                 // we must remember to update the positions
338                 xpos += buttonwidth;
339                 // ypos is constant
340                 /* Here will come a check to see if the new
341                  * pos is within the bounds of the main frame,
342                  * and perhaps wrap the toolbar if not.
343                  */
344                 break;
345         }
346         }
347
348         toollist_.push_back(item);
349 }