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 "LyXAction.h"
30 #include "MathsSymbols.h"
31 #include "support/filetools.h"
32 #include "support/lstrings.h"
37 extern LyXAction lyxaction;
40 const int standardspacing = 2; // the usual space between items
41 const int sepspace = 6; // extra space
42 const int buttonwidth = 30; // the standard button width
43 const int height = 30; // the height of all items in the toolbar
45 Toolbar::Pimpl::toolbarItem::toolbarItem() {
46 action = LFUN_NOACTION;
51 Toolbar::Pimpl::toolbarItem::~toolbarItem() {
56 void Toolbar::Pimpl::toolbarItem::clean() {
58 fl_delete_object(icon);
65 Toolbar::Pimpl::toolbarItem &
66 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti) {
67 // Are we assigning the object onto itself?
71 // If we already have an icon, release it.
74 // do we have to check icon too?
76 icon = 0; // locally we need to get the icon anew
83 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
84 : owner(static_cast<XFormsView *>(o)), sxpos(x), sypos(y)
96 // timer-cb for bubble-help (Matthias)
97 void BubbleTimerCB(FL_OBJECT *, long data)
99 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
100 // The trick we use to get the help text is to read the
101 // argument of the callback that has been registered for
102 // ToolBarCB. (JMarc)
103 string help = _(lyxaction.helpText(ob->argument));
104 fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
105 ob->form->y + ob->y + ob->h);
111 void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
113 BubbleTimerCB(ob, data);
117 // post_handler for bubble-help (Matthias)
118 int BubblePost(FL_OBJECT *ob, int event,
119 FL_Coord /*mx*/, FL_Coord /*my*/,
120 int /*key*/, void * /*xev*/)
122 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
124 // We do not test for empty help here, since this can never happen
125 if (event == FL_ENTER){
126 fl_set_object_callback(bubble_timer,
127 C_Toolbar_BubbleTimerCB,
128 reinterpret_cast<long>(ob));
129 fl_set_timer(bubble_timer, 1);
131 else if (event != FL_MOTION){
132 fl_set_timer(bubble_timer, 0);
141 int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
142 FL_Coord /*mx*/, FL_Coord /*my*/,
145 return BubblePost(ob, event, 0, 0, key, xev);
152 void Toolbar::Pimpl::activate()
154 ToolbarList::const_iterator p = toollist.begin();
155 ToolbarList::const_iterator end = toollist.end();
156 for (; p != end; ++p) {
158 fl_activate_object(p->icon);
164 void Toolbar::Pimpl::deactivate()
166 ToolbarList::const_iterator p = toollist.begin();
167 ToolbarList::const_iterator end = toollist.end();
168 for (; p != end; ++p) {
170 fl_deactivate_object(p->icon);
175 void Toolbar::Pimpl::update()
177 ToolbarList::const_iterator p = toollist.begin();
178 ToolbarList::const_iterator end = toollist.end();
179 for (; p != end; ++p) {
181 int status = owner->getLyXFunc()->getStatus(p->action);
182 if (status & func_status::ToggleOn) {
183 // I'd like to use a different color
184 // here, but then the problem is to
185 // know how to use transparency with
186 // Xpm library. It seems pretty
187 // complicated to me (JMarc)
188 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
189 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
191 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
192 fl_set_object_boxtype(p->icon, FL_UP_BOX);
195 if (status & func_status::Disabled) {
196 // Is there a way here to specify a
197 // mask in order to show that the
198 // button is disabled? (JMarc)
199 fl_deactivate_object(p->icon);
202 fl_activate_object(p->icon);
208 // this one is not "C" because combox callbacks are really C++ %-|
209 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
211 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
213 tb->layoutSelected();
217 void Toolbar::Pimpl::layoutSelected()
219 string const & layoutguiname = combox->getline();
220 LyXTextClass const & tc =
221 textclasslist.TextClass(owner->buffer()->
224 LyXTextClass::const_iterator end = tc.end();
225 for (LyXTextClass::const_iterator cit = tc.begin();
227 if (_(cit->name()) == layoutguiname) {
228 owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
232 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
237 void Toolbar::Pimpl::setLayout(int layout) {
239 LyXTextClass const & tc =
240 textclasslist.TextClass(owner->buffer()->
242 combox->select(_(tc[layout].name()));
247 void Toolbar::Pimpl::updateLayoutList(bool force)
249 // Update the layout display
252 // If textclass is different, we need to update the list
253 if (combox->empty() || force) {
255 LyXTextClass const & tc =
256 textclasslist.TextClass(owner->buffer()->
258 LyXTextClass::const_iterator end = tc.end();
259 for (LyXTextClass::const_iterator cit = tc.begin();
261 // ignore obsolete entries
262 if (cit->obsoleted_by().empty())
263 combox->addline(_(cit->name()));
266 // we need to do this.
271 void Toolbar::Pimpl::clearLayoutList()
280 void Toolbar::Pimpl::openLayoutList()
289 void ToolbarCB(FL_OBJECT * ob, long ac)
291 XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
293 string res = owner->getLyXFunc()->dispatch(int(ac));
295 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
303 void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
311 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
312 string name, arg, xpm_name;
315 if (lyxaction.isPseudoAction(action)) {
316 act = lyxaction.retrieveActionArg(action, arg);
317 name = lyxaction.getActionName(act);
318 xpm_name = subst(name + ' ' + arg, ' ','_');
320 act = (kb_action)action;
321 name = lyxaction.getActionName(action);
325 string fullname = LibFileSearch("images", xpm_name, "xpm");
327 if (!fullname.empty()) {
328 lyxerr[Debug::GUI] << "Full icon name is `"
329 << fullname << "'" << endl;
330 fl_set_pixmapbutton_file(obj, fullname.c_str());
334 if (act == LFUN_INSERT_MATH && !arg.empty()) {
335 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
339 lyxerr[Debug::GUI] << "Using mathed-provided icon"
341 fl_set_pixmapbutton_data(obj,
342 const_cast<char **>(pixmap));
347 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
348 fullname = LibFileSearch("images", "unknown", "xpm");
349 if (!fullname.empty()) {
350 lyxerr[Debug::GUI] << "Using default `unknown' icon"
352 fl_set_pixmapbutton_file(obj, fullname.c_str());
359 void Toolbar::Pimpl::set(bool doingmain)
361 // we shouldn't set if we have not cleaned
362 if (!cleaned) return;
367 fl_freeze_form(owner->getForm());
368 fl_addto_form(owner->getForm());
372 // add the time if it don't exist
373 if (bubble_timer == 0)
374 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
375 xpos, ypos, 0, 0, "Timer");
378 ToolbarList::iterator item = toollist.begin();
379 ToolbarList::iterator end = toollist.end();
380 for (; item != end; ++item) {
381 switch (item->action){
382 case ToolbarDefaults::SEPARATOR:
385 case ToolbarDefaults::NEWLINE:
386 // Not supported yet.
388 case ToolbarDefaults::LAYOUTS:
389 xpos += standardspacing;
391 combox = new Combox(FL_COMBOX_DROPLIST);
392 combox->add(xpos, ypos, 135, height, 400);
393 combox->setcallback(layoutSelectedCB, this);
394 combox->resize(FL_RESIZE_ALL);
395 combox->gravity(NorthWestGravity, NorthWestGravity);
399 xpos += standardspacing;
401 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
405 fl_set_object_resize(obj, FL_RESIZE_ALL);
406 fl_set_object_gravity(obj,
409 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
410 static_cast<long>(item->action));
411 // Remove the blue feedback rectangle
412 fl_set_pixmapbutton_focus_outline(obj, 0);
415 #if FL_REVISION >= 89
416 string const help(_(lyxaction.helpText(item->action)));
417 fl_set_object_helper(obj, help.c_str());
419 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
420 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
423 // The view that this object belongs to.
424 obj->u_vdata = owner;
426 setPixmap(obj, item->action, buttonwidth, height);
427 // we must remember to update the positions
430 /* Here will come a check to see if the new
431 * pos is within the bounds of the main frame,
432 * and perhaps wrap the toolbar if not.
440 fl_unfreeze_form(owner->getForm());
441 // Should be safe to do this here.
442 owner->updateLayoutChoice();
445 // set the state of the icons
452 void Toolbar::Pimpl::add(int action, bool doclean)
454 if (doclean && !cleaned) clean();
456 // this is what we do if we want to add to an existing
458 if (!doclean && owner) {
459 // first "hide" the toolbar buttons. This is not a real hide
460 // actually it deletes and frees the button altogether.
461 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
466 fl_freeze_form(owner->getForm());
468 ToolbarList::iterator p = toollist.begin();
469 ToolbarList::iterator end = toollist.end();
470 for (; p != end; ++p) {
478 fl_unfreeze_form(owner->getForm());
479 cleaned = true; // this is not completely true, but OK anyway
482 // there exist some special actions not part of
483 // kb_action: SEPARATOR, LAYOUTS
486 newItem.action = action;
487 toollist.push_back(newItem);
491 void Toolbar::Pimpl::clean()
493 //reset(); // I do not understand what this reset() is, anyway
495 //now delete all the objects..
497 fl_freeze_form(owner->getForm());
499 // G++ vector does not have clear defined
501 toollist.erase(toollist.begin(), toollist.end());
503 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
510 fl_unfreeze_form(owner->getForm());
511 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
516 void Toolbar::Pimpl::push(int nth)
518 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
519 << nth << '\'' << endl;
521 if (nth <= 0 || nth >= int(toollist.size())) {
522 // item nth not found...
526 fl_trigger_object(toollist[nth - 1].icon);
530 void Toolbar::Pimpl::reset()
532 //toollist = 0; // what is this supposed to do?
538 void Toolbar::Pimpl::lightReset() {
539 xpos = sxpos - standardspacing;