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 "support/filetools.h"
34 // this one is not "C" because combox callbacks are really C++ %-|
35 extern void LayoutsCB(int, void *, Combox *);
36 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
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(o), sxpos(x), sypos(y)
94 // 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)
116 int BubblePost(FL_OBJECT *ob, int event,
117 FL_Coord /*mx*/, FL_Coord /*my*/,
118 int /*key*/, void */*xev*/)
120 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
122 // We do not test for empty help here, since this can never happen
123 if(event == FL_ENTER){
124 fl_set_object_callback(bubble_timer,
125 C_Toolbar_BubbleTimerCB,
126 reinterpret_cast<long>(ob));
127 fl_set_timer(bubble_timer, 1);
129 else if(event != FL_MOTION){
130 fl_set_timer(bubble_timer, 0);
137 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
138 FL_Coord /*mx*/, FL_Coord /*my*/,
141 return BubblePost(ob, event, 0, 0, key, xev);
146 void Toolbar::Pimpl::activate()
148 ToolbarList::const_iterator p = toollist.begin();
149 ToolbarList::const_iterator end = toollist.end();
150 for (; p != end; ++p) {
152 fl_activate_object(p->icon);
158 void Toolbar::Pimpl::deactivate()
160 ToolbarList::const_iterator p = toollist.begin();
161 ToolbarList::const_iterator end = toollist.end();
162 for (; p != end; ++p) {
164 fl_deactivate_object(p->icon);
169 void Toolbar::Pimpl::update()
171 ToolbarList::const_iterator p = toollist.begin();
172 ToolbarList::const_iterator end = toollist.end();
173 for (; p != end; ++p) {
175 int status = owner->getLyXFunc()->getStatus(p->action);
176 if (status & LyXFunc::ToggleOn) {
177 // I'd like to use a different color
178 // here, but then the problem is to
179 // know how to use transparency with
180 // Xpm library. It seems pretty
181 // complicated to me (JMarc)
182 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
183 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
185 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
186 fl_set_object_boxtype(p->icon, FL_UP_BOX);
189 if (status & LyXFunc::Disabled) {
190 // Is there a way here to specify a
191 // mask in order to show that the
192 // button is disabled? (JMarc)
193 fl_deactivate_object(p->icon);
196 fl_activate_object(p->icon);
202 void Toolbar::Pimpl::setLayout(int layout) {
204 combox->select(layout+1);
208 void Toolbar::Pimpl::updateLayoutList(bool force)
210 // Update the layout display
213 // If textclass is different, we need to update the list
214 if (combox->empty() || force) {
216 LyXTextClass const & tc =
217 textclasslist.TextClass(owner->buffer()->
219 LyXTextClass::const_iterator end = tc.end();
220 for (LyXTextClass::const_iterator cit = tc.begin();
222 if ((*cit).obsoleted_by().empty())
223 combox->addline(_((*cit).name().c_str()));
225 combox->addline(("@N" + string(_((*cit).name().c_str()))).c_str());
228 // we need to do this.
232 void Toolbar::Pimpl::clearLayoutList()
240 void Toolbar::Pimpl::openLayoutList()
247 void ToolbarCB(FL_OBJECT * ob, long ac)
249 LyXView * owner = static_cast<LyXView *>(ob->u_vdata);
251 string res = owner->getLyXFunc()->Dispatch(int(ac));
253 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
258 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
264 // What are we supposed to do with that??
265 int Toolbar::get_toolbar_func(string const & func)
267 int action = lyxaction.LookupFunc(func);
269 if (func == "separator"){
270 action = TOOL_SEPARATOR;
271 } else if (func == "layouts"){
272 action = TOOL_LAYOUTS;
281 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
282 string name, arg, xpm_name;
285 if (lyxaction.isPseudoAction(action)) {
286 act = lyxaction.retrieveActionArg(action, arg);
287 name = lyxaction.getActionName(act);
288 xpm_name = subst(name + ' ' + arg, ' ','_');
290 act = (kb_action)action;
291 name = lyxaction.getActionName(action);
295 string fullname = LibFileSearch("images", xpm_name, "xpm");
297 if (!fullname.empty()) {
298 lyxerr[Debug::GUI] << "Full icon name is `"
299 << fullname << "'" << endl;
300 fl_set_pixmapbutton_file(obj, fullname.c_str());
304 if (act == LFUN_INSERT_MATH && !arg.empty()) {
305 lyxerr[Debug::GUI] << "Using mathed-provided icon" << endl;
306 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
309 fl_set_pixmapbutton_data(obj, const_cast<char **>(pixmap));
313 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
314 fullname = LibFileSearch("images", "unknown", "xpm");
315 if (!fullname.empty()) {
316 lyxerr[Debug::GUI] << "Using default `unknown' icon"
318 fl_set_pixmapbutton_file(obj, fullname.c_str());
322 void Toolbar::Pimpl::set(bool doingmain)
324 // we shouldn't set if we have not cleaned
325 if (!cleaned) return;
330 fl_freeze_form(owner->getForm());
331 fl_addto_form(owner->getForm());
335 // add the time if it don't exist
336 if (bubble_timer == 0)
337 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
338 xpos, ypos, 0, 0, "Timer");
341 ToolbarList::iterator item = toollist.begin();
342 ToolbarList::iterator end = toollist.end();
343 for (; item != end; ++item) {
344 switch(item->action){
345 case ToolbarDefaults::SEPARATOR:
348 case ToolbarDefaults::NEWLINE:
349 // Not supported yet.
351 case ToolbarDefaults::LAYOUTS:
352 xpos += standardspacing;
354 combox = new Combox(FL_COMBOX_DROPLIST);
355 combox->add(xpos, ypos, 135, height, 400);
356 combox->setcallback(LayoutsCB);
357 combox->resize(FL_RESIZE_ALL);
358 combox->gravity(NorthWestGravity, NorthWestGravity);
362 xpos += standardspacing;
364 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
368 fl_set_object_resize(obj, FL_RESIZE_ALL);
369 fl_set_object_gravity(obj,
372 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
373 static_cast<long>(item->action));
374 // Remove the blue feedback rectangle
375 fl_set_pixmapbutton_focus_outline(obj, 0);
378 #if FL_REVISION >= 89
379 string help = lyxaction.helpText(item->action);
380 fl_set_object_helper(obj, help.c_str());
382 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
383 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
386 // The view that this object belongs to.
387 obj->u_vdata = owner;
389 setPixmap(obj, item->action, buttonwidth, height);
390 // we must remember to update the positions
393 /* Here will come a check to see if the new
394 * pos is within the bounds of the main frame,
395 * and perhaps wrap the toolbar if not.
403 fl_unfreeze_form(owner->getForm());
404 // Should be safe to do this here.
405 owner->updateLayoutChoice();
408 // set the state of the icons
415 void Toolbar::Pimpl::add(int action, bool doclean)
417 if (doclean && !cleaned) clean();
419 // this is what we do if we want to add to an existing
421 if (!doclean && owner) {
422 // first "hide" the toolbar buttons. This is not a real hide
423 // actually it deletes and frees the button altogether.
424 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
429 fl_freeze_form(owner->getForm());
431 ToolbarList::iterator p = toollist.begin();
432 ToolbarList::iterator end = toollist.end();
433 for (; p != end; ++p) {
441 fl_unfreeze_form(owner->getForm());
442 cleaned = true; // this is not completely true, but OK anyway
445 // there exist some special actions not part of
446 // kb_action: SEPARATOR, LAYOUTS
449 newItem.action = action;
450 toollist.push_back(newItem);
454 void Toolbar::Pimpl::clean()
456 //reset(); // I do not understand what this reset() is, anyway
458 //now delete all the objects..
460 fl_freeze_form(owner->getForm());
462 // G++ vector does not have clear defined
464 toollist.erase(toollist.begin(), toollist.end());
466 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
473 fl_unfreeze_form(owner->getForm());
474 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
479 void Toolbar::Pimpl::push(int nth)
481 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
482 << nth << '\'' << endl;
484 if (nth <= 0 || nth >= int(toollist.size())) {
485 // item nth not found...
489 fl_trigger_object(toollist[nth - 1].icon);
493 void Toolbar::Pimpl::reset()
495 //toollist = 0; // what is this supposed to do?
500 void Toolbar::Pimpl::lightReset() {
501 xpos = sxpos - standardspacing;