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"
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 FuncStatus status = owner->getLyXFunc()->getStatus(p->action);
194 if (status.onoff(true)) {
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);
206 if (status.disabled()) {
207 // Is there a way here to specify a
208 // mask in order to show that the
209 // button is disabled? (JMarc)
210 fl_deactivate_object(p->icon);
213 fl_activate_object(p->icon);
219 // this one is not "C" because combox callbacks are really C++ %-|
220 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
222 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
224 tb->layoutSelected();
228 void Toolbar::Pimpl::layoutSelected()
230 string const & layoutguiname = combox->getline();
231 LyXTextClass const & tc =
232 textclasslist.TextClass(owner->buffer()->
235 LyXTextClass::const_iterator end = tc.end();
236 for (LyXTextClass::const_iterator cit = tc.begin();
238 if (_(cit->name()) == layoutguiname) {
239 owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
243 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
248 void Toolbar::Pimpl::setLayout(int layout)
251 LyXTextClass const & tc =
252 textclasslist.TextClass(owner->buffer()->
254 combox->select(_(tc[layout].name()));
259 void Toolbar::Pimpl::updateLayoutList(bool force)
261 // Update the layout display
264 // If textclass is different, we need to update the list
265 if (combox->empty() || force) {
267 LyXTextClass const & tc =
268 textclasslist.TextClass(owner->buffer()->
270 LyXTextClass::const_iterator end = tc.end();
271 for (LyXTextClass::const_iterator cit = tc.begin();
273 // ignore obsolete entries
274 if (cit->obsoleted_by().empty())
275 combox->addline(_(cit->name()));
278 // we need to do this.
283 void Toolbar::Pimpl::clearLayoutList()
292 void Toolbar::Pimpl::openLayoutList()
301 void ToolbarCB(FL_OBJECT * ob, long ac)
303 XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
305 owner->getLyXFunc()->verboseDispatch(int(ac), true);
312 void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
320 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
325 const kb_action act = lyxaction.retrieveActionArg(action, arg);
326 string const name = lyxaction.getActionName(act);
328 xpm_name = subst(name + ' ' + arg, ' ','_');
332 string fullname = LibFileSearch("images", xpm_name, "xpm");
334 if (!fullname.empty()) {
335 lyxerr[Debug::GUI] << "Full icon name is `"
336 << fullname << "'" << endl;
337 fl_set_pixmapbutton_file(obj, fullname.c_str());
341 if (act == LFUN_INSERT_MATH && !arg.empty()) {
342 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
346 lyxerr[Debug::GUI] << "Using mathed-provided icon"
348 fl_set_pixmapbutton_data(obj,
349 const_cast<char **>(pixmap));
354 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
355 fullname = LibFileSearch("images", "unknown", "xpm");
356 if (!fullname.empty()) {
357 lyxerr[Debug::GUI] << "Using default `unknown' icon"
359 fl_set_pixmapbutton_file(obj, fullname.c_str());
366 void Toolbar::Pimpl::set(bool doingmain)
368 // we shouldn't set if we have not cleaned
369 if (!cleaned) return;
374 fl_freeze_form(owner->getForm());
375 fl_addto_form(owner->getForm());
379 // add the time if it don't exist
380 if (bubble_timer == 0)
381 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
382 xpos, ypos, 0, 0, "Timer");
385 ToolbarList::iterator item = toollist.begin();
386 ToolbarList::iterator end = toollist.end();
387 for (; item != end; ++item) {
388 switch (item->action){
389 case ToolbarDefaults::SEPARATOR:
392 case ToolbarDefaults::NEWLINE:
393 // Not supported yet.
395 case ToolbarDefaults::LAYOUTS:
396 xpos += standardspacing;
398 combox = new Combox(FL_COMBOX_DROPLIST);
399 combox->add(xpos, ypos, 135, height, 400);
400 combox->setcallback(layoutSelectedCB, this);
401 combox->resize(FL_RESIZE_ALL);
402 combox->gravity(NorthWestGravity, NorthWestGravity);
406 xpos += standardspacing;
408 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
412 fl_set_object_resize(obj, FL_RESIZE_ALL);
413 fl_set_object_gravity(obj,
416 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
417 static_cast<long>(item->action));
418 // Remove the blue feedback rectangle
419 fl_set_pixmapbutton_focus_outline(obj, 0);
422 #if FL_REVISION >= 89
423 string const help(_(lyxaction.helpText(item->action)));
424 fl_set_object_helper(obj, help.c_str());
426 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
427 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
430 // The view that this object belongs to.
431 obj->u_vdata = owner;
433 setPixmap(obj, item->action, buttonwidth, height);
434 // we must remember to update the positions
437 /* Here will come a check to see if the new
438 * pos is within the bounds of the main frame,
439 * and perhaps wrap the toolbar if not.
447 fl_unfreeze_form(owner->getForm());
448 // Should be safe to do this here.
449 owner->updateLayoutChoice();
452 // set the state of the icons
459 void Toolbar::Pimpl::add(int action, bool doclean)
461 if (doclean && !cleaned) clean();
463 // this is what we do if we want to add to an existing
465 if (!doclean && owner) {
466 // first "hide" the toolbar buttons. This is not a real hide
467 // actually it deletes and frees the button altogether.
468 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
473 fl_freeze_form(owner->getForm());
475 ToolbarList::iterator p = toollist.begin();
476 ToolbarList::iterator end = toollist.end();
477 for (; p != end; ++p) {
485 fl_unfreeze_form(owner->getForm());
486 cleaned = true; // this is not completely true, but OK anyway
489 // there exist some special actions not part of
490 // kb_action: SEPARATOR, LAYOUTS
493 newItem.action = action;
494 toollist.push_back(newItem);
498 void Toolbar::Pimpl::clean()
500 //reset(); // I do not understand what this reset() is, anyway
502 //now delete all the objects..
504 fl_freeze_form(owner->getForm());
506 // G++ vector does not have clear defined
508 toollist.erase(toollist.begin(), toollist.end());
510 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
517 fl_unfreeze_form(owner->getForm());
518 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
523 void Toolbar::Pimpl::push(int nth)
525 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
526 << nth << '\'' << endl;
528 if (nth <= 0 || nth >= int(toollist.size())) {
529 // item nth not found...
533 fl_trigger_object(toollist[nth - 1].icon);
537 void Toolbar::Pimpl::reset()
539 //toollist = 0; // what is this supposed to do?
545 void Toolbar::Pimpl::lightReset() {
546 xpos = sxpos - standardspacing;