1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * This file is Copyright 1996-1998
12 * ====================================================== */
14 // Added pseudo-action handling, asierra 180296
19 #pragma implementation "toolbar.h"
23 #include "lyx_gui_misc.h"
31 #include "LyXAction.h"
32 #include "support/lstrings.h"
34 #ifdef TWO_COLOR_ICONS
36 #include "emph_bw.xpm"
38 #include "foot_bw.xpm"
39 #include "math_bw.xpm"
40 #include "depth_bw.xpm"
41 #include "margin_bw.xpm"
42 #include "melt_bw.xpm"
43 #include "copy_bw.xpm"
44 #include "noun_bw.xpm"
45 #include "paste_bw.xpm"
46 #include "free_bw.xpm"
49 #include "open_bw.xpm"
50 #include "close_bw.xpm"
51 #include "save_bw.xpm"
52 #include "print1_bw.xpm"
53 #include "quit_bw.xpm"
54 #include "typeset_ps_bw.xpm"
55 #include "unknown_bw.xpm"
76 #include "typeset_ps.xpm"
77 #include "unknown.xpm"
80 // These pixmaps are the same regardless of color:
81 #include "bold_bw.xpm"
82 #include "make_ascii_bw.xpm"
83 #include "make_latex_bw.xpm"
84 #include "run_latex_bw.xpm"
85 #include "sans_bw.xpm"
86 #include "view_dvi_bw.xpm"
87 #include "view_ps_bw.xpm"
88 #include "layout_code.xpm"
89 #include "layout_latex.xpm"
90 #include "layout_scrap.xpm"
91 #include "layout_sec.xpm"
92 #include "layout_std.xpm"
96 // this one is not "C" because combox callbacks are really C++ %-|
97 extern void LayoutsCB(int, void *);
98 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
99 extern LyXAction lyxaction;
112 struct keyword_item toolTags[TO_LAST - 1] = {
114 { "\\end_toolbar", TO_ENDTOOLBAR },
115 { "\\layouts", TO_LAYOUTS },
116 { "\\newline", TO_NEWLINE },
117 { "\\separator", TO_SEPARATOR }
121 Toolbar::Toolbar(Toolbar const &rct, LyXView *o, int x, int y)
122 : owner(o), sxpos(x), sypos(y)
130 // extracts the toolbar struct form rct.
131 toolbarItem * tmplist = rct.toollist;
132 while (tmplist != 0) {
133 add(tmplist->action);
134 lyxerr[Debug::TOOLBAR] << "tool action: "
135 << tmplist->action << endl;
136 tmplist= tmplist->next;
142 // timer-cb for bubble-help (Matthias)
143 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
145 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
146 char * help = static_cast<char*>(ob->u_vdata);
147 fl_show_oneliner(help, ob->form->x + ob->x,
148 ob->form->y + ob->y + ob->h);
152 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
154 Toolbar::BubbleTimerCB(ob, data);
158 // post_handler for bubble-help (Matthias)
159 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
160 FL_Coord /*mx*/, FL_Coord /*my*/,
161 int /*key*/, void */*xev*/)
163 string help = static_cast<char *>(ob->u_vdata);
164 Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
166 if(event == FL_ENTER && !help.empty()){
167 fl_set_object_callback(t->bubble_timer,
168 C_Toolbar_BubbleTimerCB,
169 reinterpret_cast<long>(ob));
170 fl_set_timer(t->bubble_timer, 1);
172 else if(event != FL_MOTION){
173 fl_set_timer(t->bubble_timer, 0);
180 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
181 FL_Coord /*mx*/, FL_Coord /*my*/,
184 return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
189 void Toolbar::activate()
191 toolbarItem * tmp= 0;
192 toolbarItem * item = toollist;
196 fl_activate_object(item->icon);
203 void Toolbar::deactivate()
205 toolbarItem * tmp= 0;
206 toolbarItem * item = toollist;
210 fl_deactivate_object(item->icon);
217 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
219 #if FL_REVISION >= 89
220 Toolbar * t = static_cast<Toolbar*>(ob->u_vdata);
222 Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
225 string res = t->owner->getLyXFunc()->Dispatch(int(ac));
227 lyxerr[Debug::TOOLBAR] << res << endl;
231 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
233 Toolbar::ToolbarCB(ob, data);
237 int Toolbar::get_toolbar_func(string const & func)
239 int action = lyxaction.LookupFunc(func.c_str());
241 if (func == "separator"){
242 action = TOOL_SEPARATOR;
243 } else if (func == "layouts"){
244 action = TOOL_LAYOUTS;
255 //add(LFUN_CLOSEBUFFER);
271 add(LFUN_MARGINMELT);
285 void Toolbar::set(bool doingmain)
287 // we shouldn't set if we have not cleaned
288 if (!cleaned) return;
291 toolbarItem * item = toollist;
294 fl_freeze_form(owner->getForm());
295 fl_addto_form(owner->getForm());
299 // add the time if it don't exist
300 if (bubble_timer == 0)
301 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
302 xpos, ypos, 0, 0, "Timer");
306 switch(item->action){
312 xpos += standardspacing;
314 combox = new Combox(FL_COMBOX_DROPLIST);
315 combox->add(xpos, ypos, 135, height, 400);
316 combox->setcallback(LayoutsCB);
317 combox->resize(FL_RESIZE_ALL);
318 combox->gravity(NorthWestGravity, NorthWestGravity);
323 xpos += standardspacing;
325 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
329 fl_set_object_boxtype(obj, FL_UP_BOX);
330 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
331 fl_set_object_resize(obj, FL_RESIZE_ALL);
332 fl_set_object_gravity(obj,
335 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
336 static_cast<long>(item->action));
337 // Remove the blue feedback rectangle
338 fl_set_pixmapbutton_focus_outline(obj, 0);
340 // set the bubble-help (Matthias)
341 #if FL_REVISION >= 89
343 fl_set_object_helper(obj, item->help.c_str());
344 // The toolbar that this object belongs too.
350 #warning This is dangerous!
352 obj->u_vdata = const_cast<char*>(item->help.c_str());
353 // we need to know what toolbar this item
354 // belongs too. (Lgb)
355 obj->u_ldata = reinterpret_cast<long>(this);
357 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
360 fl_set_pixmapbutton_data(obj, const_cast<char**>(item->pixmap));
362 // we must remember to update the positions
365 /* Here will come a check to see if the new
366 * pos is within the bounds of the main frame,
367 * and perhaps wrap the toolbar if not.
375 fl_unfreeze_form(owner->getForm());
376 // Should be safe to do this here.
377 owner->updateLayoutChoice();
384 char const ** Toolbar::getPixmap(kb_action action, string const & arg)
386 char const ** pixmap = unknown_xpm; //0
388 case LFUN_MENUOPEN: pixmap = open_xpm; break;
389 case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
390 case LFUN_MENUPRINT: pixmap = print1_xpm; break;
391 case LFUN_MENUWRITE: pixmap = save_xpm; break;
392 case LFUN_EMPH: pixmap = emph_xpm; break;
393 case LFUN_NOUN: pixmap = noun_xpm; break;
394 case LFUN_FREE: pixmap = free_xpm; break;
395 case LFUN_FOOTMELT: pixmap = foot_xpm; break;
396 case LFUN_DEPTH: pixmap = depth_xpm; break;
397 case LFUN_COPY: pixmap = copy_xpm; break;
398 case LFUN_CUT: pixmap = cut_xpm; break;
399 case LFUN_PASTE: pixmap = paste_xpm; break;
400 case LFUN_TEX: pixmap = tex_xpm; break;
401 case LFUN_MATH_MODE: pixmap = math_xpm; break;
402 case LFUN_MARGINMELT: pixmap = margin_xpm; break;
403 case LFUN_FIGURE: pixmap = fig_xpm; break;
404 case LFUN_TABLE: pixmap = tab_xpm; break;
405 case LFUN_MELT: pixmap = melt_xpm; break;
406 case LFUN_QUIT: pixmap = quit_xpm; break;
407 case LFUN_RUNDVIPS: pixmap = update_ps_xpm; break;
411 pixmap = make_ascii_xpm;
412 else if (arg == "latex")
413 pixmap = make_latex_xpm;
418 if (arg == "Section")
419 pixmap = layout_sec_xpm;
420 else if (arg == "LaTeX")
421 pixmap = layout_latex_xpm;
422 else if (arg == "LyX-Code")
423 pixmap = layout_code_xpm;
424 else if (arg == "Scrap")
425 pixmap = layout_scrap_xpm;
427 pixmap = layout_std_xpm;
431 case LFUN_BOLD : pixmap = bold_xpm; break;
432 case LFUN_SANS: pixmap = sans_xpm; break;
433 case LFUN_RUNLATEX: pixmap = run_latex_xpm; break;
434 case LFUN_BUILDPROG: pixmap = build_xpm; break;
435 case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break;
436 case LFUN_PREVIEW: pixmap = view_dvi_xpm; break;
437 case LFUN_INSERT_MATH:
440 pixmap = get_pixmap_from_symbol(arg.c_str(),
446 //pixmap = unknown_xpm;
453 void Toolbar::add(int action, bool doclean)
455 if (doclean && !cleaned) clean();
457 // this is what we do if we want to add to an existing
459 if (!doclean && owner) {
460 // first «hide» the toolbar buttons. This is not a real hide
461 // actually it deletes and frees the button altogether.
462 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
463 toolbarItem * tmp= 0;
464 toolbarItem * item = toollist;
468 fl_freeze_form(owner->getForm());
472 fl_delete_object(item->icon);
473 fl_free_object(item->icon);
481 fl_unfreeze_form(owner->getForm());
482 cleaned = true; // this is not completely true, but OK anyway
485 // there exist some special actions not part of
486 // kb_action: SEPARATOR, LAYOUTS
487 char const ** pixmap = 0;
490 toolbarItem * newItem, * tmp;
492 if (lyxaction.isPseudoAction(action)) {
494 kb_action act = static_cast<kb_action>
495 (lyxaction.retrieveActionArg(action, arg));
496 pixmap = getPixmap(act, arg);
497 help = lyxaction.helpText(act);
500 lyxerr.debug() << "Pseudo action " << action << endl;
502 pixmap = getPixmap(static_cast<kb_action>(action));
503 help = lyxaction.helpText(static_cast<kb_action>(action));
506 // adds an item to the list
508 || action == TOOL_SEPARATOR
509 || action == TOOL_LAYOUTS) {
510 newItem = new toolbarItem;
511 newItem->action = action;
512 newItem->pixmap = pixmap;
513 newItem->help = help;
514 // the new item is placed at the end of the list
517 while(tmp->next != 0)
519 // here is tmp->next == 0
527 void Toolbar::add(string const & func, bool doclean)
529 int tf = lyxaction.LookupFunc(func.c_str());
532 lyxerr << "Toolbar::add: no LyX command called`"
533 << func << "'exists!" << endl;
540 void Toolbar::clean()
542 toolbarItem * tmp = 0;
543 toolbarItem * item = toollist;
547 //now delete all the objects..
549 fl_freeze_form(owner->getForm());
555 lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
561 fl_unfreeze_form(owner->getForm());
562 lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
567 void Toolbar::push(int nth)
569 lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
570 << nth << '\'' << endl;
572 if (nth == 0) return;
575 toolbarItem * tmp = toollist;
579 fl_trigger_object(tmp->icon);
584 // item nth not found...
589 void Toolbar::read(LyXLex & lex)
592 if (lex.GetString() != "\\begin_toolbar")
593 lyxerr << "Toolbar::read: ERROR wrong token:`"
594 << lex.GetString() << '\'' << endl;
600 lex.pushTable(toolTags, TO_LAST - 1);
602 if (lyxerr.debugging(Debug::PARSER))
605 while (lex.IsOK() && !quit) {
607 lyxerr[Debug::TOOLBAR] << "Toolbar::read: current lex text: `"
608 << lex.GetString() << '\'' << endl;
613 func = lex.GetString();
614 lyxerr[Debug::TOOLBAR]
615 << "Toolbar::read TO_ADD func: `"
616 << func << "'" << endl;
634 // should not set automatically
639 lex.printError("Toolbar::read: "
640 "Unknown toolbar tag: `$$Token'");