]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
whitespace changes only (believe it or not!)
[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 "BufferView.h"
25 #include "buffer.h"
26 #include "lyxtextclasslist.h"
27 #include "LyXAction.h"
28 #include "MathsSymbols.h" 
29 #include "gettext.h"
30 #include "Tooltips.h"
31
32 #include "support/LAssert.h"
33 #include "support/filetools.h"
34 #include "support/lstrings.h" 
35
36 using std::endl;
37
38 extern LyXAction lyxaction;
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 {
48         action = LFUN_NOACTION;
49         icon = 0;
50 }
51
52
53 Toolbar::Pimpl::toolbarItem::~toolbarItem()
54 {
55         clean();
56 }
57
58
59 void Toolbar::Pimpl::toolbarItem::clean()
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         // Are we assigning the object onto itself?
73         if (this == &ti)
74                 return *this;
75
76         // If we already have an icon, release it.
77         clean();
78     
79         // do we have to check icon too?
80         action = ti.action;
81         icon = 0; // locally we need to get the icon anew
82         
83         return *this;
84 }
85
86
87
88 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
89         : owner(static_cast<XFormsView *>(o)), sxpos(x), sypos(y)
90 {
91         combox = 0;
92         tooltip_ = new Tooltips();
93 }
94
95
96 Toolbar::Pimpl::~Pimpl()
97 {
98         clean();
99         delete tooltip_;
100 }
101
102
103 void Toolbar::Pimpl::activate()
104 {
105         ToolbarList::const_iterator p = toollist.begin();
106         ToolbarList::const_iterator end = toollist.end();
107         for (; p != end; ++p) {
108                 if (p->icon) {
109                         fl_activate_object(p->icon);
110                 }
111         }
112 }
113
114
115 void Toolbar::Pimpl::deactivate()
116 {
117         ToolbarList::const_iterator p = toollist.begin();
118         ToolbarList::const_iterator end = toollist.end();
119         for (; p != end; ++p) {
120                 if (p->icon) {
121                         fl_deactivate_object(p->icon);
122                 }
123         }
124 }
125
126
127 void Toolbar::Pimpl::update()
128 {
129         ToolbarList::const_iterator p = toollist.begin();
130         ToolbarList::const_iterator end = toollist.end();
131         for (; p != end; ++p) {
132                 if (p->icon) {
133                         FuncStatus status = owner->getLyXFunc()->getStatus(p->action);
134                         if (status.onoff(true)) {
135                                 // I'd like to use a different color
136                                 // here, but then the problem is to
137                                 // know how to use transparency with
138                                 // Xpm library. It seems pretty
139                                 // complicated to me (JMarc)
140                                 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
141                                 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
142                         } else {
143                                 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
144                                 fl_set_object_boxtype(p->icon, FL_UP_BOX);
145                         }
146                         if (status.disabled()) {
147                                 // Is there a way here to specify a
148                                 // mask in order to show that the
149                                 // button is disabled? (JMarc)
150                                 fl_deactivate_object(p->icon);
151                         }
152                         else
153                                 fl_activate_object(p->icon);
154                 } else if (p->action == ToolbarDefaults::LAYOUTS && combox) {
155                         if (owner->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled())
156                                 combox->deactivate();
157                         else
158                                 combox->activate();
159                 }
160         }
161 }
162
163
164 // this one is not "C" because combox callbacks are really C++ %-|
165 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
166 {
167         Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
168
169         tb->layoutSelected();
170 }
171
172
173 void Toolbar::Pimpl::layoutSelected()
174 {
175         string const & layoutguiname = combox->getline();
176         LyXTextClass const & tc =
177                 textclasslist[owner->buffer()->params.textclass];
178         
179         LyXTextClass::const_iterator end = tc.end();
180         for (LyXTextClass::const_iterator cit = tc.begin();
181              cit != end; ++cit) {
182                 if (_(cit->name()) == layoutguiname) {
183                         owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
184                         return;
185                 }
186         }
187         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
188                << endl;
189 }
190  
191
192 void Toolbar::Pimpl::setLayout(string const & layout)
193 {
194         if (combox) {
195                 LyXTextClass const & tc =
196                         textclasslist[owner->buffer()->params.textclass];
197                 combox->select(_(tc[layout].name()));
198         }
199 }
200
201
202 void Toolbar::Pimpl::updateLayoutList(bool force)
203 {
204         // Update the layout display
205         if (!combox) return;
206
207         // If textclass is different, we need to update the list
208         if (combox->empty() || force) {
209                 combox->clear();
210                 LyXTextClass const & tc =
211                         textclasslist[owner->buffer()->params.textclass];
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                                 combox->addline(_(cit->name()));
218                 }
219         }
220         // we need to do this.
221         combox->redraw();
222 }
223
224
225 void Toolbar::Pimpl::clearLayoutList()
226 {
227         if (combox) {
228                 combox->clear();
229                 combox->redraw();
230         }
231 }
232
233
234 void Toolbar::Pimpl::openLayoutList()
235 {
236         if (combox)
237                 combox->show();
238 }
239
240
241 namespace {
242
243 void ToolbarCB(FL_OBJECT * ob, long ac)
244 {
245         XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
246         
247         owner->getLyXFunc()->verboseDispatch(int(ac), true);
248 }
249
250
251 extern "C" {
252         
253         static
254         void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
255         {
256                 ToolbarCB(ob, data);
257         }
258         
259 }
260
261
262 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
263 {
264         string arg;
265         string xpm_name;
266
267         const kb_action act = lyxaction.retrieveActionArg(action, arg);
268         string const name = lyxaction.getActionName(act);
269         if (!arg.empty())
270                 xpm_name = subst(name + ' ' + arg, ' ','_');
271         else 
272                 xpm_name = name;
273
274         string fullname = LibFileSearch("images", xpm_name, "xpm");
275
276         if (!fullname.empty()) {
277                 lyxerr[Debug::GUI] << "Full icon name is `" 
278                                    << fullname << "'" << endl;
279                 fl_set_pixmapbutton_file(obj, fullname.c_str());
280                 return;
281         }
282
283         if (act == LFUN_INSERT_MATH && !arg.empty()) {
284                 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
285                                                               buttonwidth,
286                                                               height);
287                 if (pixmap) {
288                         lyxerr[Debug::GUI] << "Using mathed-provided icon"
289                                            << endl;
290                         fl_set_pixmapbutton_data(obj,
291                                                  const_cast<char **>(pixmap));
292                         return;
293                 }
294         }
295         
296         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
297         fullname = LibFileSearch("images", "unknown", "xpm");
298         if (!fullname.empty()) {
299                 lyxerr[Debug::GUI] << "Using default `unknown' icon" 
300                                    << endl;
301                 fl_set_pixmapbutton_file(obj, fullname.c_str());
302         }
303 }
304
305 } // namespace anon
306
307
308 void Toolbar::Pimpl::set(bool doingmain)
309 {
310         // we shouldn't set if we have not cleaned
311         if (!cleaned) return;
312         
313         FL_OBJECT * obj;
314         
315         if (!doingmain) {
316                 fl_freeze_form(owner->getForm());
317                 fl_addto_form(owner->getForm());
318         }
319
320         ToolbarList::iterator item = toollist.begin();
321         ToolbarList::iterator end = toollist.end();
322         for (; item != end; ++item) {
323                 switch (item->action) {
324                 case ToolbarDefaults::SEPARATOR:
325                         xpos += sepspace;
326                         break;
327                 case ToolbarDefaults::NEWLINE:
328                         // Not supported yet.
329                         break;
330                 case ToolbarDefaults::LAYOUTS:
331                         xpos += standardspacing;
332                         if (!combox)
333                                 combox = new Combox(FL_COMBOX_DROPLIST);
334                         combox->add(xpos, ypos, 135, height, 400);
335                         combox->setcallback(layoutSelectedCB, this);
336                         combox->resize(FL_RESIZE_ALL);
337                         combox->gravity(NorthWestGravity, NorthWestGravity);
338                         xpos += 135;
339                         break;
340                 default:
341                         xpos += standardspacing;
342                         item->icon = obj = 
343                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
344                                                     xpos, ypos,
345                                                     buttonwidth,
346                                                     height, "");
347                         fl_set_object_resize(obj, FL_RESIZE_ALL);
348                         fl_set_object_gravity(obj,
349                                               NorthWestGravity,
350                                               NorthWestGravity);
351                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
352                                                static_cast<long>(item->action));
353                         // Remove the blue feedback rectangle
354                         fl_set_pixmapbutton_focus_outline(obj, 0);
355
356                         // initialise the tooltip
357                         string const tip = _(lyxaction.helpText(obj->argument));
358                         tooltip_->init(obj, tip);
359                         
360                         // The view that this object belongs to.
361                         obj->u_vdata = owner;
362
363                         setPixmap(obj, item->action, buttonwidth, height);
364                         // we must remember to update the positions
365                         xpos += buttonwidth;
366                         // ypos is constant
367                         /* Here will come a check to see if the new
368                          * pos is within the bounds of the main frame,
369                          * and perhaps wrap the toolbar if not.
370                          */
371                         break;
372                 }
373         }
374
375         if (!doingmain) {
376                 fl_end_form();
377                 fl_unfreeze_form(owner->getForm());
378                 // Should be safe to do this here.
379                 owner->updateLayoutChoice();
380         }
381
382         // set the state of the icons
383         //update();
384
385         cleaned = false;
386 }
387
388
389 void Toolbar::Pimpl::add(int action, bool doclean)
390 {
391         if (doclean && !cleaned) clean();
392
393         // this is what we do if we want to add to an existing
394         // toolbar.
395         if (!doclean && owner) {
396                 // first "hide" the toolbar buttons. This is not a real hide
397                 // actually it deletes and frees the button altogether.
398                 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons." 
399                        << endl;
400
401                 lightReset();
402                 
403                 fl_freeze_form(owner->getForm());
404
405                 ToolbarList::iterator p = toollist.begin();
406                 ToolbarList::iterator end = toollist.end();
407                 for (; p != end; ++p) {
408                         p->clean();
409                 }
410
411                 if (combox) {
412                         delete combox;
413                         combox = 0;
414                 }
415                 fl_unfreeze_form(owner->getForm());
416                 cleaned = true; // this is not completely true, but OK anyway
417         }
418         
419         // there exist some special actions not part of
420         // kb_action: SEPARATOR, LAYOUTS
421
422         toolbarItem newItem;
423         newItem.action = action;
424         toollist.push_back(newItem);
425 }
426
427
428 void Toolbar::Pimpl::clean()
429 {
430         //reset(); // I do not understand what this reset() is, anyway
431
432         //now delete all the objects..
433         if (owner)
434                 fl_freeze_form(owner->getForm());
435
436         // G++ vector does not have clear defined
437         //toollist.clear();
438         toollist.erase(toollist.begin(), toollist.end());
439
440         lyxerr[Debug::GUI] << "Combox: " << combox << endl;
441         if (combox) {
442                 delete combox;
443                 combox = 0;
444         }
445
446         if (owner)
447                 fl_unfreeze_form(owner->getForm());
448         lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
449         cleaned = true;
450 }
451
452
453 void Toolbar::Pimpl::push(int nth)
454 {
455         lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
456                            << nth << '\'' << endl;
457         
458         if (nth <= 0 || nth >= int(toollist.size())) {
459                 // item nth not found...
460                 return;
461         }
462
463         fl_trigger_object(toollist[nth - 1].icon);
464 }
465
466
467 void Toolbar::Pimpl::reset() 
468 {
469         //toollist = 0; // what is this supposed to do?
470         cleaned = false;
471         lightReset();
472 }
473
474
475 void Toolbar::Pimpl::lightReset() {
476         xpos = sxpos - standardspacing;
477         ypos = sypos;
478 }