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"
33 // this one is not "C" because combox callbacks are really C++ %-|
34 extern void LayoutsCB(int, void *);
35 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
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=(const toolbarItem & ti) {
66 // do we have to check icon too?
68 icon = 0; // locally we need to get the icon anew
75 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
76 : owner(o), sxpos(x), sypos(y)
86 // timer-cb for bubble-help (Matthias)
88 void BubbleTimerCB(FL_OBJECT *, long data)
90 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
91 // The trick we use to get the help text is to read the
92 // argument of the callback that has been registered for
94 string help = lyxaction.helpText(ob->argument);
95 fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
96 ob->form->y + ob->y + ob->h);
100 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
102 BubbleTimerCB(ob, data);
106 // post_handler for bubble-help (Matthias)
108 int BubblePost(FL_OBJECT *ob, int event,
109 FL_Coord /*mx*/, FL_Coord /*my*/,
110 int /*key*/, void */*xev*/)
112 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
114 // We do not test for empty help here, since this can never happen
115 if(event == FL_ENTER){
116 fl_set_object_callback(bubble_timer,
117 C_Toolbar_BubbleTimerCB,
118 reinterpret_cast<long>(ob));
119 fl_set_timer(bubble_timer, 1);
121 else if(event != FL_MOTION){
122 fl_set_timer(bubble_timer, 0);
129 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
130 FL_Coord /*mx*/, FL_Coord /*my*/,
133 return BubblePost(ob, event, 0, 0, key, xev);
138 void Toolbar::Pimpl::activate()
140 ToolbarList::const_iterator p = toollist.begin();
141 for (; p != toollist.end(); ++p) {
143 fl_activate_object(p->icon);
149 void Toolbar::Pimpl::deactivate()
151 ToolbarList::const_iterator p = toollist.begin();
152 for (; p != toollist.end(); ++p) {
154 fl_deactivate_object(p->icon);
159 void Toolbar::Pimpl::update()
161 ToolbarList::const_iterator p = toollist.begin();
162 for (; p != toollist.end(); ++p) {
164 int status = owner->getLyXFunc()->getStatus(p->action);
165 if (status & LyXFunc::ToggleOn) {
166 // I'd like to use a different color
167 // here, but then the problem is to
168 // know how to use transparency with
169 // Xpm library. It seems pretty
170 // complicated to me (JMarc)
171 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
172 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
174 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
175 fl_set_object_boxtype(p->icon, FL_UP_BOX);
178 if (status & LyXFunc::Disabled) {
179 // Is there a way here to specify a
180 // mask in order to show that the
181 // button is disabled? (JMarc)
182 fl_deactivate_object(p->icon);
185 fl_activate_object(p->icon);
191 void Toolbar::Pimpl::setLayout(int layout) {
193 combox->select(layout+1);
197 void Toolbar::Pimpl::updateLayoutList(bool force)
199 // Update the layout display
202 // If textclass is different, we need to update the list
203 if (combox->empty() || force) {
205 LyXTextClass const & tc =
206 textclasslist.TextClass(owner->buffer()->
208 for (LyXTextClass::const_iterator cit = tc.begin();
209 cit != tc.end(); ++cit) {
210 if ((*cit).obsoleted_by().empty())
211 combox->addline((*cit).name().c_str());
213 combox->addline(("@N" + (*cit).name()).c_str());
216 // we need to do this.
220 void Toolbar::Pimpl::clearLayoutList()
228 void Toolbar::Pimpl::openLayoutList()
235 void ToolbarCB(FL_OBJECT * ob, long ac)
237 LyXView * owner = static_cast<LyXView *>(ob->u_vdata);
239 string res = owner->getLyXFunc()->Dispatch(int(ac));
241 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
246 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
252 // What are we supposed to do with that??
253 int Toolbar::get_toolbar_func(string const & func)
255 int action = lyxaction.LookupFunc(func.c_str());
257 if (func == "separator"){
258 action = TOOL_SEPARATOR;
259 } else if (func == "layouts"){
260 action = TOOL_LAYOUTS;
269 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
270 string name, arg, xpm_name;
273 if (lyxaction.isPseudoAction(action)) {
274 act = lyxaction.retrieveActionArg(action, arg);
275 name = lyxaction.getActionName(act);
276 xpm_name = subst(name + ' ' + arg, ' ','_');
278 act = (kb_action)action;
279 name = lyxaction.getActionName(action);
283 string fullname = LibFileSearch("images", xpm_name, "xpm");
285 if (!fullname.empty()) {
286 lyxerr[Debug::GUI] << "Full icon name is `"
287 << fullname << "'" << endl;
288 fl_set_pixmapbutton_file(obj, fullname.c_str());
292 if (act == LFUN_INSERT_MATH && !arg.empty()) {
293 lyxerr[Debug::GUI] << "Using mathed-provided icon" << endl;
294 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
297 fl_set_pixmapbutton_data(obj, const_cast<char **>(pixmap));
301 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
302 fullname = LibFileSearch("images", "unknown", "xpm");
303 if (!fullname.empty()) {
304 lyxerr[Debug::GUI] << "Using default `unknown' icon"
306 fl_set_pixmapbutton_file(obj, fullname.c_str());
310 void Toolbar::Pimpl::set(bool doingmain)
312 // we shouldn't set if we have not cleaned
313 if (!cleaned) return;
318 fl_freeze_form(owner->getForm());
319 fl_addto_form(owner->getForm());
323 // add the time if it don't exist
324 if (bubble_timer == 0)
325 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
326 xpos, ypos, 0, 0, "Timer");
329 ToolbarList::iterator item = toollist.begin();
330 for (; item != toollist.end(); ++item) {
331 switch(item->action){
332 case ToolbarDefaults::SEPARATOR:
335 case ToolbarDefaults::NEWLINE:
336 // Not supported yet.
338 case ToolbarDefaults::LAYOUTS:
339 xpos += standardspacing;
341 combox = new Combox(FL_COMBOX_DROPLIST);
342 combox->add(xpos, ypos, 135, height, 400);
343 combox->setcallback(LayoutsCB);
344 combox->resize(FL_RESIZE_ALL);
345 combox->gravity(NorthWestGravity, NorthWestGravity);
349 xpos += standardspacing;
351 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
355 fl_set_object_resize(obj, FL_RESIZE_ALL);
356 fl_set_object_gravity(obj,
359 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
360 static_cast<long>(item->action));
361 // Remove the blue feedback rectangle
362 fl_set_pixmapbutton_focus_outline(obj, 0);
365 #if FL_REVISION >= 89
366 string help = lyxaction.helpText(item->action);
367 fl_set_object_helper(obj, help.c_str());
369 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
370 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
373 // The view that this object belongs to.
374 obj->u_vdata = owner;
376 setPixmap(obj, item->action, buttonwidth, height);
377 // we must remember to update the positions
380 /* Here will come a check to see if the new
381 * pos is within the bounds of the main frame,
382 * and perhaps wrap the toolbar if not.
390 fl_unfreeze_form(owner->getForm());
391 // Should be safe to do this here.
392 owner->updateLayoutChoice();
395 // set the state of the icons
402 void Toolbar::Pimpl::add(int action, bool doclean)
404 if (doclean && !cleaned) clean();
406 // this is what we do if we want to add to an existing
408 if (!doclean && owner) {
409 // first "hide" the toolbar buttons. This is not a real hide
410 // actually it deletes and frees the button altogether.
411 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
416 fl_freeze_form(owner->getForm());
418 ToolbarList::iterator p = toollist.begin();
419 for (; p != toollist.end(); ++p) {
427 fl_unfreeze_form(owner->getForm());
428 cleaned = true; // this is not completely true, but OK anyway
431 // there exist some special actions not part of
432 // kb_action: SEPARATOR, LAYOUTS
435 newItem.action = action;
436 toollist.push_back(newItem);
440 void Toolbar::Pimpl::clean()
442 //reset(); // I do not understand what this reset() is, anyway
444 //now delete all the objects..
446 fl_freeze_form(owner->getForm());
448 // G++ vector does not have clear defined
450 toollist.erase(toollist.begin(), toollist.end());
452 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
459 fl_unfreeze_form(owner->getForm());
460 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
465 void Toolbar::Pimpl::push(int nth)
467 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
468 << nth << '\'' << endl;
470 if (nth <= 0 || nth >= int(toollist.size())) {
471 // item nth not found...
475 fl_trigger_object(toollist[nth - 1].icon);
479 void Toolbar::Pimpl::reset()
481 //toollist = 0; // what is this supposed to do?
486 void Toolbar::Pimpl::lightReset() {
487 xpos = sxpos - standardspacing;