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);
112 void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
114 BubbleTimerCB(ob, data);
119 // post_handler for bubble-help (Matthias)
120 int BubblePost(FL_OBJECT *ob, int event,
121 FL_Coord /*mx*/, FL_Coord /*my*/,
122 int /*key*/, void * /*xev*/)
124 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
126 // We do not test for empty help here, since this can never happen
127 if (event == FL_ENTER){
128 fl_set_object_callback(bubble_timer,
129 C_Toolbar_BubbleTimerCB,
130 reinterpret_cast<long>(ob));
131 fl_set_timer(bubble_timer, 1);
133 else if (event != FL_MOTION){
134 fl_set_timer(bubble_timer, 0);
144 int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
145 FL_Coord /*mx*/, FL_Coord /*my*/,
148 return BubblePost(ob, event, 0, 0, key, xev);
157 void Toolbar::Pimpl::activate()
159 ToolbarList::const_iterator p = toollist.begin();
160 ToolbarList::const_iterator end = toollist.end();
161 for (; p != end; ++p) {
163 fl_activate_object(p->icon);
169 void Toolbar::Pimpl::deactivate()
171 ToolbarList::const_iterator p = toollist.begin();
172 ToolbarList::const_iterator end = toollist.end();
173 for (; p != end; ++p) {
175 fl_deactivate_object(p->icon);
180 void Toolbar::Pimpl::update()
182 ToolbarList::const_iterator p = toollist.begin();
183 ToolbarList::const_iterator end = toollist.end();
184 for (; p != end; ++p) {
186 int status = owner->getLyXFunc()->getStatus(p->action);
187 if (status & func_status::ToggleOn) {
188 // I'd like to use a different color
189 // here, but then the problem is to
190 // know how to use transparency with
191 // Xpm library. It seems pretty
192 // complicated to me (JMarc)
193 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
194 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
196 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
197 fl_set_object_boxtype(p->icon, FL_UP_BOX);
200 if (status & func_status::Disabled) {
201 // Is there a way here to specify a
202 // mask in order to show that the
203 // button is disabled? (JMarc)
204 fl_deactivate_object(p->icon);
207 fl_activate_object(p->icon);
213 // this one is not "C" because combox callbacks are really C++ %-|
214 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
216 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
218 tb->layoutSelected();
222 void Toolbar::Pimpl::layoutSelected()
224 string const & layoutguiname = combox->getline();
225 LyXTextClass const & tc =
226 textclasslist.TextClass(owner->buffer()->
229 LyXTextClass::const_iterator end = tc.end();
230 for (LyXTextClass::const_iterator cit = tc.begin();
232 if (_(cit->name()) == layoutguiname) {
233 owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
237 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
242 void Toolbar::Pimpl::setLayout(int layout) {
244 LyXTextClass const & tc =
245 textclasslist.TextClass(owner->buffer()->
247 combox->select(_(tc[layout].name()));
252 void Toolbar::Pimpl::updateLayoutList(bool force)
254 // Update the layout display
257 // If textclass is different, we need to update the list
258 if (combox->empty() || force) {
260 LyXTextClass const & tc =
261 textclasslist.TextClass(owner->buffer()->
263 LyXTextClass::const_iterator end = tc.end();
264 for (LyXTextClass::const_iterator cit = tc.begin();
266 // ignore obsolete entries
267 if (cit->obsoleted_by().empty())
268 combox->addline(_(cit->name()));
271 // we need to do this.
276 void Toolbar::Pimpl::clearLayoutList()
285 void Toolbar::Pimpl::openLayoutList()
294 void ToolbarCB(FL_OBJECT * ob, long ac)
296 XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
298 string res = owner->getLyXFunc()->dispatch(int(ac));
300 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
308 void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
316 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
317 string name, arg, xpm_name;
320 if (lyxaction.isPseudoAction(action)) {
321 act = lyxaction.retrieveActionArg(action, arg);
322 name = lyxaction.getActionName(act);
323 xpm_name = subst(name + ' ' + arg, ' ','_');
325 act = (kb_action)action;
326 name = lyxaction.getActionName(action);
330 string fullname = LibFileSearch("images", xpm_name, "xpm");
332 if (!fullname.empty()) {
333 lyxerr[Debug::GUI] << "Full icon name is `"
334 << fullname << "'" << endl;
335 fl_set_pixmapbutton_file(obj, fullname.c_str());
339 if (act == LFUN_INSERT_MATH && !arg.empty()) {
340 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
344 lyxerr[Debug::GUI] << "Using mathed-provided icon"
346 fl_set_pixmapbutton_data(obj,
347 const_cast<char **>(pixmap));
352 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
353 fullname = LibFileSearch("images", "unknown", "xpm");
354 if (!fullname.empty()) {
355 lyxerr[Debug::GUI] << "Using default `unknown' icon"
357 fl_set_pixmapbutton_file(obj, fullname.c_str());
364 void Toolbar::Pimpl::set(bool doingmain)
366 // we shouldn't set if we have not cleaned
367 if (!cleaned) return;
372 fl_freeze_form(owner->getForm());
373 fl_addto_form(owner->getForm());
377 // add the time if it don't exist
378 if (bubble_timer == 0)
379 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
380 xpos, ypos, 0, 0, "Timer");
383 ToolbarList::iterator item = toollist.begin();
384 ToolbarList::iterator end = toollist.end();
385 for (; item != end; ++item) {
386 switch (item->action){
387 case ToolbarDefaults::SEPARATOR:
390 case ToolbarDefaults::NEWLINE:
391 // Not supported yet.
393 case ToolbarDefaults::LAYOUTS:
394 xpos += standardspacing;
396 combox = new Combox(FL_COMBOX_DROPLIST);
397 combox->add(xpos, ypos, 135, height, 400);
398 combox->setcallback(layoutSelectedCB, this);
399 combox->resize(FL_RESIZE_ALL);
400 combox->gravity(NorthWestGravity, NorthWestGravity);
404 xpos += standardspacing;
406 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
410 fl_set_object_resize(obj, FL_RESIZE_ALL);
411 fl_set_object_gravity(obj,
414 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
415 static_cast<long>(item->action));
416 // Remove the blue feedback rectangle
417 fl_set_pixmapbutton_focus_outline(obj, 0);
420 #if FL_REVISION >= 89
421 string const help(_(lyxaction.helpText(item->action)));
422 fl_set_object_helper(obj, help.c_str());
424 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
425 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
428 // The view that this object belongs to.
429 obj->u_vdata = owner;
431 setPixmap(obj, item->action, buttonwidth, height);
432 // we must remember to update the positions
435 /* Here will come a check to see if the new
436 * pos is within the bounds of the main frame,
437 * and perhaps wrap the toolbar if not.
445 fl_unfreeze_form(owner->getForm());
446 // Should be safe to do this here.
447 owner->updateLayoutChoice();
450 // set the state of the icons
457 void Toolbar::Pimpl::add(int action, bool doclean)
459 if (doclean && !cleaned) clean();
461 // this is what we do if we want to add to an existing
463 if (!doclean && owner) {
464 // first "hide" the toolbar buttons. This is not a real hide
465 // actually it deletes and frees the button altogether.
466 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
471 fl_freeze_form(owner->getForm());
473 ToolbarList::iterator p = toollist.begin();
474 ToolbarList::iterator end = toollist.end();
475 for (; p != end; ++p) {
483 fl_unfreeze_form(owner->getForm());
484 cleaned = true; // this is not completely true, but OK anyway
487 // there exist some special actions not part of
488 // kb_action: SEPARATOR, LAYOUTS
491 newItem.action = action;
492 toollist.push_back(newItem);
496 void Toolbar::Pimpl::clean()
498 //reset(); // I do not understand what this reset() is, anyway
500 //now delete all the objects..
502 fl_freeze_form(owner->getForm());
504 // G++ vector does not have clear defined
506 toollist.erase(toollist.begin(), toollist.end());
508 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
515 fl_unfreeze_form(owner->getForm());
516 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
521 void Toolbar::Pimpl::push(int nth)
523 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
524 << nth << '\'' << endl;
526 if (nth <= 0 || nth >= int(toollist.size())) {
527 // item nth not found...
531 fl_trigger_object(toollist[nth - 1].icon);
535 void Toolbar::Pimpl::reset()
537 //toollist = 0; // what is this supposed to do?
543 void Toolbar::Pimpl::lightReset() {
544 xpos = sxpos - standardspacing;