1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * This file is Copyright 1996-1998
12 * ====================================================== */
14 // Added pseudo-action handling, asierra 180296
19 #pragma implementation
22 #include "Toolbar_pimpl.h"
24 #include "XFormsView.h"
26 #include "FuncStatus.h"
27 #include "BufferView.h"
29 #include "lyxtextclasslist.h"
30 #include "LyXAction.h"
31 #include "MathsSymbols.h"
35 #include "support/LAssert.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
41 extern LyXAction lyxaction;
44 const int standardspacing = 2; // the usual space between items
45 const int sepspace = 6; // extra space
46 const int buttonwidth = 30; // the standard button width
47 const int height = 30; // the height of all items in the toolbar
49 Toolbar::Pimpl::toolbarItem::toolbarItem()
51 action = LFUN_NOACTION;
56 Toolbar::Pimpl::toolbarItem::~toolbarItem()
62 void Toolbar::Pimpl::toolbarItem::clean()
65 fl_delete_object(icon);
72 Toolbar::Pimpl::toolbarItem &
73 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti)
75 // Are we assigning the object onto itself?
79 // If we already have an icon, release it.
82 // do we have to check icon too?
84 icon = 0; // locally we need to get the icon anew
91 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
92 : owner(static_cast<XFormsView *>(o)), sxpos(x), sypos(y)
95 tooltip_ = new Tooltips();
99 Toolbar::Pimpl::~Pimpl()
106 void Toolbar::Pimpl::activate()
108 ToolbarList::const_iterator p = toollist.begin();
109 ToolbarList::const_iterator end = toollist.end();
110 for (; p != end; ++p) {
112 fl_activate_object(p->icon);
118 void Toolbar::Pimpl::deactivate()
120 ToolbarList::const_iterator p = toollist.begin();
121 ToolbarList::const_iterator end = toollist.end();
122 for (; p != end; ++p) {
124 fl_deactivate_object(p->icon);
130 void Toolbar::Pimpl::update()
132 ToolbarList::const_iterator p = toollist.begin();
133 ToolbarList::const_iterator end = toollist.end();
134 for (; p != end; ++p) {
136 FuncStatus status = owner->getLyXFunc()->getStatus(p->action);
137 if (status.onoff(true)) {
138 // I'd like to use a different color
139 // here, but then the problem is to
140 // know how to use transparency with
141 // Xpm library. It seems pretty
142 // complicated to me (JMarc)
143 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
144 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
146 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
147 fl_set_object_boxtype(p->icon, FL_UP_BOX);
149 if (status.disabled()) {
150 // Is there a way here to specify a
151 // mask in order to show that the
152 // button is disabled? (JMarc)
153 fl_deactivate_object(p->icon);
156 fl_activate_object(p->icon);
157 } else if (p->action == ToolbarDefaults::LAYOUTS && combox) {
158 if (owner->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled())
159 combox->deactivate();
167 // this one is not "C" because combox callbacks are really C++ %-|
168 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
170 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
172 tb->layoutSelected();
176 void Toolbar::Pimpl::layoutSelected()
178 string const & layoutguiname = combox->getline();
179 LyXTextClass const & tc =
180 textclasslist[owner->buffer()->params.textclass];
182 LyXTextClass::const_iterator end = tc.end();
183 for (LyXTextClass::const_iterator cit = tc.begin();
185 if (_(cit->name()) == layoutguiname) {
186 owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
190 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
195 void Toolbar::Pimpl::setLayout(string const & layout)
198 LyXTextClass const & tc =
199 textclasslist[owner->buffer()->params.textclass];
200 combox->select(_(tc[layout].name()));
205 void Toolbar::Pimpl::updateLayoutList(bool force)
207 // Update the layout display
210 // If textclass is different, we need to update the list
211 if (combox->empty() || force) {
213 LyXTextClass const & tc =
214 textclasslist[owner->buffer()->params.textclass];
215 LyXTextClass::const_iterator end = tc.end();
216 for (LyXTextClass::const_iterator cit = tc.begin();
218 // ignore obsolete entries
219 if (cit->obsoleted_by().empty())
220 combox->addline(_(cit->name()));
223 // we need to do this.
228 void Toolbar::Pimpl::clearLayoutList()
237 void Toolbar::Pimpl::openLayoutList()
246 void ToolbarCB(FL_OBJECT * ob, long ac)
248 XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
250 owner->getLyXFunc()->verboseDispatch(int(ac), true);
257 void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
265 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
270 const kb_action act = lyxaction.retrieveActionArg(action, arg);
271 string const name = lyxaction.getActionName(act);
273 xpm_name = subst(name + ' ' + arg, ' ','_');
277 string fullname = LibFileSearch("images", xpm_name, "xpm");
279 if (!fullname.empty()) {
280 lyxerr[Debug::GUI] << "Full icon name is `"
281 << fullname << "'" << endl;
282 fl_set_pixmapbutton_file(obj, fullname.c_str());
286 if (act == LFUN_INSERT_MATH && !arg.empty()) {
287 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
291 lyxerr[Debug::GUI] << "Using mathed-provided icon"
293 fl_set_pixmapbutton_data(obj,
294 const_cast<char **>(pixmap));
299 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
300 fullname = LibFileSearch("images", "unknown", "xpm");
301 if (!fullname.empty()) {
302 lyxerr[Debug::GUI] << "Using default `unknown' icon"
304 fl_set_pixmapbutton_file(obj, fullname.c_str());
311 void Toolbar::Pimpl::set(bool doingmain)
313 // we shouldn't set if we have not cleaned
314 if (!cleaned) return;
319 fl_freeze_form(owner->getForm());
320 fl_addto_form(owner->getForm());
323 ToolbarList::iterator item = toollist.begin();
324 ToolbarList::iterator end = toollist.end();
325 for (; item != end; ++item) {
326 switch (item->action) {
327 case ToolbarDefaults::SEPARATOR:
330 case ToolbarDefaults::NEWLINE:
331 // Not supported yet.
333 case ToolbarDefaults::LAYOUTS:
334 xpos += standardspacing;
336 combox = new Combox(FL_COMBOX_DROPLIST);
337 combox->add(xpos, ypos, 135, height, 400);
338 combox->setcallback(layoutSelectedCB, this);
339 combox->resize(FL_RESIZE_ALL);
340 combox->gravity(NorthWestGravity, NorthWestGravity);
344 xpos += standardspacing;
346 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
350 fl_set_object_resize(obj, FL_RESIZE_ALL);
351 fl_set_object_gravity(obj,
354 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
355 static_cast<long>(item->action));
356 // Remove the blue feedback rectangle
357 fl_set_pixmapbutton_focus_outline(obj, 0);
359 // initialise the tooltip
360 string const tip = _(lyxaction.helpText(obj->argument));
361 tooltip_->initTooltip(obj, tip);
363 // The view that this object belongs to.
364 obj->u_vdata = owner;
366 setPixmap(obj, item->action, buttonwidth, height);
367 // we must remember to update the positions
370 /* Here will come a check to see if the new
371 * pos is within the bounds of the main frame,
372 * and perhaps wrap the toolbar if not.
380 fl_unfreeze_form(owner->getForm());
381 // Should be safe to do this here.
382 owner->updateLayoutChoice();
385 // set the state of the icons
392 void Toolbar::Pimpl::add(int action, bool doclean)
394 if (doclean && !cleaned) clean();
396 // this is what we do if we want to add to an existing
398 if (!doclean && owner) {
399 // first "hide" the toolbar buttons. This is not a real hide
400 // actually it deletes and frees the button altogether.
401 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
406 fl_freeze_form(owner->getForm());
408 ToolbarList::iterator p = toollist.begin();
409 ToolbarList::iterator end = toollist.end();
410 for (; p != end; ++p) {
418 fl_unfreeze_form(owner->getForm());
419 cleaned = true; // this is not completely true, but OK anyway
422 // there exist some special actions not part of
423 // kb_action: SEPARATOR, LAYOUTS
426 newItem.action = action;
427 toollist.push_back(newItem);
431 void Toolbar::Pimpl::clean()
433 //reset(); // I do not understand what this reset() is, anyway
435 //now delete all the objects..
437 fl_freeze_form(owner->getForm());
439 // G++ vector does not have clear defined
441 toollist.erase(toollist.begin(), toollist.end());
443 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
450 fl_unfreeze_form(owner->getForm());
451 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
456 void Toolbar::Pimpl::push(int nth)
458 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
459 << nth << '\'' << endl;
461 if (nth <= 0 || nth >= int(toollist.size())) {
462 // item nth not found...
466 fl_trigger_object(toollist[nth - 1].icon);
470 void Toolbar::Pimpl::reset()
472 //toollist = 0; // what is this supposed to do?
478 void Toolbar::Pimpl::lightReset() {
479 xpos = sxpos - standardspacing;