1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 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"
26 #include "BufferView.h"
28 #include "LyXAction.h"
29 #include "MathsSymbols.h"
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
36 extern LyXAction lyxaction;
39 const int standardspacing = 2; // the usual space between items
40 const int sepspace = 6; // extra space
41 const int buttonwidth = 30; // the standard button width
42 const int height = 30; // the height of all items in the toolbar
44 Toolbar::Pimpl::toolbarItem::toolbarItem() {
45 action = LFUN_NOACTION;
50 Toolbar::Pimpl::toolbarItem::~toolbarItem() {
55 void Toolbar::Pimpl::toolbarItem::clean() {
57 fl_delete_object(icon);
64 Toolbar::Pimpl::toolbarItem &
65 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti) {
66 // Are we assigning the object onto itself?
70 // If we already have an icon, release it.
73 // do we have to check icon too?
75 icon = 0; // locally we need to get the icon anew
82 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
83 : owner(o), sxpos(x), sypos(y)
95 // timer-cb for bubble-help (Matthias)
96 void BubbleTimerCB(FL_OBJECT *, long data)
98 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
99 // The trick we use to get the help text is to read the
100 // argument of the callback that has been registered for
101 // ToolBarCB. (JMarc)
102 string help = _(lyxaction.helpText(ob->argument));
103 fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
104 ob->form->y + ob->y + ob->h);
108 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
110 BubbleTimerCB(ob, data);
114 // post_handler for bubble-help (Matthias)
115 int BubblePost(FL_OBJECT *ob, int event,
116 FL_Coord /*mx*/, FL_Coord /*my*/,
117 int /*key*/, void * /*xev*/)
119 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
121 // We do not test for empty help here, since this can never happen
122 if (event == FL_ENTER){
123 fl_set_object_callback(bubble_timer,
124 C_Toolbar_BubbleTimerCB,
125 reinterpret_cast<long>(ob));
126 fl_set_timer(bubble_timer, 1);
128 else if (event != FL_MOTION){
129 fl_set_timer(bubble_timer, 0);
136 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
137 FL_Coord /*mx*/, FL_Coord /*my*/,
140 return BubblePost(ob, event, 0, 0, key, xev);
147 // this one is not "C" because combox callbacks are really C++ %-|
148 void Toolbar::Pimpl::layoutSelectedCB(int sel, void * arg, Combox *)
150 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
152 tb->layoutSelected(sel);
156 void Toolbar::Pimpl::layoutSelected(int sel)
158 string const tmp = tostr(sel);
159 owner->getLyXFunc()->Dispatch(LFUN_LAYOUTNO, tmp);
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);
186 void Toolbar::Pimpl::update()
188 ToolbarList::const_iterator p = toollist.begin();
189 ToolbarList::const_iterator end = toollist.end();
190 for (; p != end; ++p) {
192 int status = owner->getLyXFunc()->getStatus(p->action);
193 if (status & LyXFunc::ToggleOn) {
194 // I'd like to use a different color
195 // here, but then the problem is to
196 // know how to use transparency with
197 // Xpm library. It seems pretty
198 // complicated to me (JMarc)
199 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
200 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
202 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
203 fl_set_object_boxtype(p->icon, FL_UP_BOX);
206 if (status & LyXFunc::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 void Toolbar::Pimpl::setLayout(int layout) {
221 combox->select(layout+1);
225 void Toolbar::Pimpl::updateLayoutList(bool force)
227 // Update the layout display
230 // If textclass is different, we need to update the list
231 if (combox->empty() || force) {
233 LyXTextClass const & tc =
234 textclasslist.TextClass(owner->buffer()->
236 LyXTextClass::const_iterator end = tc.end();
237 for (LyXTextClass::const_iterator cit = tc.begin();
239 if ((*cit).obsoleted_by().empty())
240 combox->addline(_((*cit).name()));
242 combox->addline("@N" + _((*cit).name()));
245 // we need to do this.
250 void Toolbar::Pimpl::clearLayoutList()
259 void Toolbar::Pimpl::openLayoutList()
268 void ToolbarCB(FL_OBJECT * ob, long ac)
270 LyXView * owner = static_cast<LyXView *>(ob->u_vdata);
272 string res = owner->getLyXFunc()->Dispatch(int(ac));
274 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
281 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
288 // What are we supposed to do with that??
289 int Toolbar::get_toolbar_func(string const & func)
291 int action = lyxaction.LookupFunc(func);
293 if (func == "separator"){
294 action = TOOL_SEPARATOR;
295 } else if (func == "layouts"){
296 action = TOOL_LAYOUTS;
306 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
307 string name, arg, xpm_name;
310 if (lyxaction.isPseudoAction(action)) {
311 act = lyxaction.retrieveActionArg(action, arg);
312 name = lyxaction.getActionName(act);
313 xpm_name = subst(name + ' ' + arg, ' ','_');
315 act = (kb_action)action;
316 name = lyxaction.getActionName(action);
320 string fullname = LibFileSearch("images", xpm_name, "xpm");
322 if (!fullname.empty()) {
323 lyxerr[Debug::GUI] << "Full icon name is `"
324 << fullname << "'" << endl;
325 fl_set_pixmapbutton_file(obj, fullname.c_str());
329 if (act == LFUN_INSERT_MATH && !arg.empty()) {
330 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
334 lyxerr[Debug::GUI] << "Using mathed-provided icon"
336 fl_set_pixmapbutton_data(obj,
337 const_cast<char **>(pixmap));
342 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
343 fullname = LibFileSearch("images", "unknown", "xpm");
344 if (!fullname.empty()) {
345 lyxerr[Debug::GUI] << "Using default `unknown' icon"
347 fl_set_pixmapbutton_file(obj, fullname.c_str());
354 void Toolbar::Pimpl::set(bool doingmain)
356 // we shouldn't set if we have not cleaned
357 if (!cleaned) return;
362 fl_freeze_form(owner->getForm());
363 fl_addto_form(owner->getForm());
367 // add the time if it don't exist
368 if (bubble_timer == 0)
369 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
370 xpos, ypos, 0, 0, "Timer");
373 ToolbarList::iterator item = toollist.begin();
374 ToolbarList::iterator end = toollist.end();
375 for (; item != end; ++item) {
376 switch (item->action){
377 case ToolbarDefaults::SEPARATOR:
380 case ToolbarDefaults::NEWLINE:
381 // Not supported yet.
383 case ToolbarDefaults::LAYOUTS:
384 xpos += standardspacing;
386 combox = new Combox(FL_COMBOX_DROPLIST);
387 combox->add(xpos, ypos, 135, height, 400);
388 combox->setcallback(layoutSelectedCB, this);
389 combox->resize(FL_RESIZE_ALL);
390 combox->gravity(NorthWestGravity, NorthWestGravity);
394 xpos += standardspacing;
396 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
400 fl_set_object_resize(obj, FL_RESIZE_ALL);
401 fl_set_object_gravity(obj,
404 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
405 static_cast<long>(item->action));
406 // Remove the blue feedback rectangle
407 fl_set_pixmapbutton_focus_outline(obj, 0);
410 #if FL_REVISION >= 89
411 string help = _(lyxaction.helpText(item->action));
412 fl_set_object_helper(obj, help.c_str());
414 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
415 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
418 // The view that this object belongs to.
419 obj->u_vdata = owner;
421 setPixmap(obj, item->action, buttonwidth, height);
422 // we must remember to update the positions
425 /* Here will come a check to see if the new
426 * pos is within the bounds of the main frame,
427 * and perhaps wrap the toolbar if not.
435 fl_unfreeze_form(owner->getForm());
436 // Should be safe to do this here.
437 owner->updateLayoutChoice();
440 // set the state of the icons
447 void Toolbar::Pimpl::add(int action, bool doclean)
449 if (doclean && !cleaned) clean();
451 // this is what we do if we want to add to an existing
453 if (!doclean && owner) {
454 // first "hide" the toolbar buttons. This is not a real hide
455 // actually it deletes and frees the button altogether.
456 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
461 fl_freeze_form(owner->getForm());
463 ToolbarList::iterator p = toollist.begin();
464 ToolbarList::iterator end = toollist.end();
465 for (; p != end; ++p) {
473 fl_unfreeze_form(owner->getForm());
474 cleaned = true; // this is not completely true, but OK anyway
477 // there exist some special actions not part of
478 // kb_action: SEPARATOR, LAYOUTS
481 newItem.action = action;
482 toollist.push_back(newItem);
486 void Toolbar::Pimpl::clean()
488 //reset(); // I do not understand what this reset() is, anyway
490 //now delete all the objects..
492 fl_freeze_form(owner->getForm());
494 // G++ vector does not have clear defined
496 toollist.erase(toollist.begin(), toollist.end());
498 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
505 fl_unfreeze_form(owner->getForm());
506 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
511 void Toolbar::Pimpl::push(int nth)
513 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
514 << nth << '\'' << endl;
516 if (nth <= 0 || nth >= int(toollist.size())) {
517 // item nth not found...
521 fl_trigger_object(toollist[nth - 1].icon);
525 void Toolbar::Pimpl::reset()
527 //toollist = 0; // what is this supposed to do?
532 void Toolbar::Pimpl::lightReset() {
533 xpos = sxpos - standardspacing;