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 "func_status.h"
27 #include "BufferView.h"
29 #include "lyxtextclasslist.h"
30 #include "LyXAction.h"
31 #include "MathsSymbols.h"
34 #include "support/filetools.h"
35 #include "support/lstrings.h"
39 extern LyXAction lyxaction;
42 const int standardspacing = 2; // the usual space between items
43 const int sepspace = 6; // extra space
44 const int buttonwidth = 30; // the standard button width
45 const int height = 30; // the height of all items in the toolbar
47 Toolbar::Pimpl::toolbarItem::toolbarItem()
49 action = LFUN_NOACTION;
54 Toolbar::Pimpl::toolbarItem::~toolbarItem()
60 void Toolbar::Pimpl::toolbarItem::clean()
63 fl_delete_object(icon);
70 Toolbar::Pimpl::toolbarItem &
71 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti)
73 // Are we assigning the object onto itself?
77 // If we already have an icon, release it.
80 // do we have to check icon too?
82 icon = 0; // locally we need to get the icon anew
89 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
90 : owner(static_cast<XFormsView *>(o)), sxpos(x), sypos(y)
102 // timer-cb for bubble-help (Matthias)
103 void BubbleTimerCB(FL_OBJECT *, long data)
105 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
106 // The trick we use to get the help text is to read the
107 // argument of the callback that has been registered for
108 // ToolBarCB. (JMarc)
109 string help = _(lyxaction.helpText(ob->argument));
110 fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
111 ob->form->y + ob->y + ob->h);
118 void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
120 BubbleTimerCB(ob, data);
125 // post_handler for bubble-help (Matthias)
126 int BubblePost(FL_OBJECT *ob, int event,
127 FL_Coord /*mx*/, FL_Coord /*my*/,
128 int /*key*/, void * /*xev*/)
130 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
132 // We do not test for empty help here, since this can never happen
133 if (event == FL_ENTER){
134 fl_set_object_callback(bubble_timer,
135 C_Toolbar_BubbleTimerCB,
136 reinterpret_cast<long>(ob));
137 fl_set_timer(bubble_timer, 1);
139 else if (event != FL_MOTION){
140 fl_set_timer(bubble_timer, 0);
150 int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
151 FL_Coord /*mx*/, FL_Coord /*my*/,
154 return BubblePost(ob, event, 0, 0, key, xev);
163 void Toolbar::Pimpl::activate()
165 ToolbarList::const_iterator p = toollist.begin();
166 ToolbarList::const_iterator end = toollist.end();
167 for (; p != end; ++p) {
169 fl_activate_object(p->icon);
175 void Toolbar::Pimpl::deactivate()
177 ToolbarList::const_iterator p = toollist.begin();
178 ToolbarList::const_iterator end = toollist.end();
179 for (; p != end; ++p) {
181 fl_deactivate_object(p->icon);
187 void Toolbar::Pimpl::update()
189 ToolbarList::const_iterator p = toollist.begin();
190 ToolbarList::const_iterator end = toollist.end();
191 for (; p != end; ++p) {
193 int status = owner->getLyXFunc()->getStatus(p->action);
194 if (status & func_status::ToggleOn) {
195 // I'd like to use a different color
196 // here, but then the problem is to
197 // know how to use transparency with
198 // Xpm library. It seems pretty
199 // complicated to me (JMarc)
200 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
201 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
203 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
204 fl_set_object_boxtype(p->icon, FL_UP_BOX);
207 if (status & func_status::Disabled) {
208 // Is there a way here to specify a
209 // mask in order to show that the
210 // button is disabled? (JMarc)
211 fl_deactivate_object(p->icon);
214 fl_activate_object(p->icon);
220 // this one is not "C" because combox callbacks are really C++ %-|
221 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
223 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
225 tb->layoutSelected();
229 void Toolbar::Pimpl::layoutSelected()
231 string const & layoutguiname = combox->getline();
232 LyXTextClass const & tc =
233 textclasslist.TextClass(owner->buffer()->
236 LyXTextClass::const_iterator end = tc.end();
237 for (LyXTextClass::const_iterator cit = tc.begin();
239 if (_(cit->name()) == layoutguiname) {
240 owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
244 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
249 void Toolbar::Pimpl::setLayout(int layout)
252 LyXTextClass const & tc =
253 textclasslist.TextClass(owner->buffer()->
255 combox->select(_(tc[layout].name()));
260 void Toolbar::Pimpl::updateLayoutList(bool force)
262 // Update the layout display
265 // If textclass is different, we need to update the list
266 if (combox->empty() || force) {
268 LyXTextClass const & tc =
269 textclasslist.TextClass(owner->buffer()->
271 LyXTextClass::const_iterator end = tc.end();
272 for (LyXTextClass::const_iterator cit = tc.begin();
274 // ignore obsolete entries
275 if (cit->obsoleted_by().empty())
276 combox->addline(_(cit->name()));
279 // we need to do this.
284 void Toolbar::Pimpl::clearLayoutList()
293 void Toolbar::Pimpl::openLayoutList()
302 void ToolbarCB(FL_OBJECT * ob, long ac)
304 XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
306 string res = owner->getLyXFunc()->dispatch(int(ac));
308 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
316 void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
324 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
331 if (lyxaction.isPseudoAction(action)) {
332 act = lyxaction.retrieveActionArg(action, arg);
333 name = lyxaction.getActionName(act);
334 xpm_name = subst(name + ' ' + arg, ' ','_');
336 act = (kb_action)action;
337 name = lyxaction.getActionName(action);
341 string fullname = LibFileSearch("images", xpm_name, "xpm");
343 if (!fullname.empty()) {
344 lyxerr[Debug::GUI] << "Full icon name is `"
345 << fullname << "'" << endl;
346 fl_set_pixmapbutton_file(obj, fullname.c_str());
350 if (act == LFUN_INSERT_MATH && !arg.empty()) {
351 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
355 lyxerr[Debug::GUI] << "Using mathed-provided icon"
357 fl_set_pixmapbutton_data(obj,
358 const_cast<char **>(pixmap));
363 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
364 fullname = LibFileSearch("images", "unknown", "xpm");
365 if (!fullname.empty()) {
366 lyxerr[Debug::GUI] << "Using default `unknown' icon"
368 fl_set_pixmapbutton_file(obj, fullname.c_str());
375 void Toolbar::Pimpl::set(bool doingmain)
377 // we shouldn't set if we have not cleaned
378 if (!cleaned) return;
383 fl_freeze_form(owner->getForm());
384 fl_addto_form(owner->getForm());
388 // add the time if it don't exist
389 if (bubble_timer == 0)
390 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
391 xpos, ypos, 0, 0, "Timer");
394 ToolbarList::iterator item = toollist.begin();
395 ToolbarList::iterator end = toollist.end();
396 for (; item != end; ++item) {
397 switch (item->action){
398 case ToolbarDefaults::SEPARATOR:
401 case ToolbarDefaults::NEWLINE:
402 // Not supported yet.
404 case ToolbarDefaults::LAYOUTS:
405 xpos += standardspacing;
407 combox = new Combox(FL_COMBOX_DROPLIST);
408 combox->add(xpos, ypos, 135, height, 400);
409 combox->setcallback(layoutSelectedCB, this);
410 combox->resize(FL_RESIZE_ALL);
411 combox->gravity(NorthWestGravity, NorthWestGravity);
415 xpos += standardspacing;
417 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
421 fl_set_object_resize(obj, FL_RESIZE_ALL);
422 fl_set_object_gravity(obj,
425 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
426 static_cast<long>(item->action));
427 // Remove the blue feedback rectangle
428 fl_set_pixmapbutton_focus_outline(obj, 0);
431 #if FL_REVISION >= 89
432 string const help(_(lyxaction.helpText(item->action)));
433 fl_set_object_helper(obj, help.c_str());
435 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
436 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
439 // The view that this object belongs to.
440 obj->u_vdata = owner;
442 setPixmap(obj, item->action, buttonwidth, height);
443 // we must remember to update the positions
446 /* Here will come a check to see if the new
447 * pos is within the bounds of the main frame,
448 * and perhaps wrap the toolbar if not.
456 fl_unfreeze_form(owner->getForm());
457 // Should be safe to do this here.
458 owner->updateLayoutChoice();
461 // set the state of the icons
468 void Toolbar::Pimpl::add(int action, bool doclean)
470 if (doclean && !cleaned) clean();
472 // this is what we do if we want to add to an existing
474 if (!doclean && owner) {
475 // first "hide" the toolbar buttons. This is not a real hide
476 // actually it deletes and frees the button altogether.
477 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
482 fl_freeze_form(owner->getForm());
484 ToolbarList::iterator p = toollist.begin();
485 ToolbarList::iterator end = toollist.end();
486 for (; p != end; ++p) {
494 fl_unfreeze_form(owner->getForm());
495 cleaned = true; // this is not completely true, but OK anyway
498 // there exist some special actions not part of
499 // kb_action: SEPARATOR, LAYOUTS
502 newItem.action = action;
503 toollist.push_back(newItem);
507 void Toolbar::Pimpl::clean()
509 //reset(); // I do not understand what this reset() is, anyway
511 //now delete all the objects..
513 fl_freeze_form(owner->getForm());
515 // G++ vector does not have clear defined
517 toollist.erase(toollist.begin(), toollist.end());
519 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
526 fl_unfreeze_form(owner->getForm());
527 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
532 void Toolbar::Pimpl::push(int nth)
534 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
535 << nth << '\'' << endl;
537 if (nth <= 0 || nth >= int(toollist.size())) {
538 // item nth not found...
542 fl_trigger_object(toollist[nth - 1].icon);
546 void Toolbar::Pimpl::reset()
548 //toollist = 0; // what is this supposed to do?
554 void Toolbar::Pimpl::lightReset() {
555 xpos = sxpos - standardspacing;