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"
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"
95 // this one is not "C" because combox callbacks are really C++ %-|
96 extern void LayoutsCB(int, void*);
97 extern char** get_pixmap_from_symbol(char const *arg, int, int);
98 extern LyXAction lyxaction;
111 struct keyword_item toolTags[TO_LAST-1] = {
113 { "\\end_toolbar", TO_ENDTOOLBAR },
114 { "\\layouts", TO_LAYOUTS },
115 { "\\newline", TO_NEWLINE },
116 { "\\separator", TO_SEPARATOR }
120 Toolbar::Toolbar(Toolbar const &rct, LyXView *o, int x, int y)
121 : owner(o), sxpos(x), sypos(y)
127 // extracts the toolbar struct form rct.
128 toolbarItem *tmplist = rct.toollist;
129 while (tmplist != 0) {
130 add(tmplist->action);
131 lyxerr[Debug::TOOLBAR] << "tool action: "
132 << tmplist->action << endl;
133 tmplist=tmplist->next;
138 // timer-cb for bubble-help (Matthias)
139 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
141 FL_OBJECT* ob = (FL_OBJECT*) data;
142 char* help = (char*) ob->u_vdata;
143 fl_show_oneliner(help, ob->form->x + ob->x,
144 ob->form->y + ob->y + ob->h);
147 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT *ob, long data)
149 Toolbar::BubbleTimerCB(ob, data);
152 // post_handler for bubble-help (Matthias)
153 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
154 FL_Coord /*mx*/, FL_Coord /*my*/, int /*key*/, void */*xev*/)
156 string help = (char *)ob->u_vdata;
157 Toolbar *t = (Toolbar*)ob->u_ldata;
159 if(event == FL_ENTER && !help.empty()){
160 fl_set_object_callback(t->bubble_timer,
161 C_Toolbar_BubbleTimerCB, (long) ob);
162 fl_set_timer(t->bubble_timer, 1);
164 else if(event != FL_MOTION){
165 fl_set_timer(t->bubble_timer, 0);
171 extern "C" int C_Toolbar_BubblePost(FL_OBJECT *ob, int event,
172 FL_Coord /*mx*/, FL_Coord /*my*/,
175 return Toolbar::BubblePost(ob,event,0,0,key,xev);
178 void Toolbar::activate()
180 toolbarItem *item, *tmp=0;
185 fl_activate_object(item->icon);
192 void Toolbar::deactivate()
194 toolbarItem *item, *tmp=0;
199 fl_deactivate_object(item->icon);
206 void Toolbar::ToolbarCB(FL_OBJECT *ob, long ac)
208 Toolbar *t = (Toolbar*)ob->u_ldata;
210 string res = t->owner->getLyXFunc()->Dispatch(int(ac));
212 lyxerr[Debug::TOOLBAR] << res << endl;
215 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT *ob, long data)
217 Toolbar::ToolbarCB(ob, data);
220 int Toolbar::get_toolbar_func(string const & func)
222 int action = lyxaction.LookupFunc(func.c_str());
224 if (func == "separator"){
225 action = TOOL_SEPARATOR;
226 } else if (func == "layouts"){
227 action = TOOL_LAYOUTS;
238 //add(LFUN_CLOSEBUFFER);
254 add(LFUN_MARGINMELT);
268 void Toolbar::set(bool doingmain)
270 // we shouldn't set if we have not cleaned
271 if (!cleaned) return;
278 fl_freeze_form(owner->getForm());
279 fl_addto_form(owner->getForm());
283 // Ensure borderwidth is 2 to get visual feedback
284 int bw = fl_get_border_width();
285 fl_set_border_width(-2);
288 // add the time if it don't exist
289 if (bubble_timer == 0)
290 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
291 xpos,ypos,0,0,"Timer");
294 switch(item->action){
300 xpos += standardspacing;
302 combox = new Combox(FL_COMBOX_DROPLIST);
303 combox->add(xpos, ypos, 135, height, 300);
304 combox->setcallback(LayoutsCB);
305 combox->resize(FL_RESIZE_ALL);
306 combox->gravity(NorthWestGravity, NorthWestGravity);
311 xpos += standardspacing;
313 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
317 fl_set_object_boxtype(obj,FL_UP_BOX);
318 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
319 fl_set_object_resize(obj, FL_RESIZE_ALL);
320 fl_set_object_gravity(obj,
323 fl_set_object_callback(obj,C_Toolbar_ToolbarCB,
326 // Remove the blue feedback rectangle
327 fl_set_pixmapbutton_focus_outline(obj,0);
330 // set the bubble-help (Matthias)
331 obj->u_vdata = (void *) item->help.c_str();
332 // we need to know what toolbar this item
333 // belongs too. (Lgb)
334 obj->u_ldata = (long) this;
336 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
338 fl_set_pixmapbutton_data(obj,item->pixmap);
340 // we must remember to update the positions
343 /* Here will come a check to see if the new
344 * pos is within the bounds of the main frame,
345 * and perhaps wrap the toolbar if not.
351 // Reset borderwidth to its default value.
352 fl_set_border_width(bw);
356 fl_unfreeze_form(owner->getForm());
357 // Should be safe to do this here.
358 owner->updateLayoutChoice();
365 char **Toolbar::getPixmap(kb_action action, string const & arg)
367 char **pixmap = unknown_xpm; //0
369 case LFUN_MENUOPEN: pixmap = open_xpm; break;
370 case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
371 case LFUN_MENUPRINT: pixmap = print1_xpm; break;
372 case LFUN_MENUWRITE: pixmap = save_xpm; break;
373 case LFUN_EMPH: pixmap = emph_xpm; break;
374 case LFUN_NOUN: pixmap = noun_xpm; break;
375 case LFUN_FREE: pixmap = free_xpm; break;
376 case LFUN_FOOTMELT: pixmap = foot_xpm; break;
377 case LFUN_DEPTH: pixmap = depth_xpm; break;
378 case LFUN_COPY: pixmap = copy_xpm; break;
379 case LFUN_CUT: pixmap = cut_xpm; break;
380 case LFUN_PASTE: pixmap = paste_xpm; break;
381 case LFUN_TEX: pixmap = tex_xpm; break;
382 case LFUN_MATH_MODE: pixmap = math_xpm; break;
383 case LFUN_MARGINMELT: pixmap = margin_xpm; break;
384 case LFUN_FIGURE: pixmap = fig_xpm; break;
385 case LFUN_TABLE: pixmap = tab_xpm; break;
386 case LFUN_MELT: pixmap = melt_xpm; break;
387 case LFUN_QUIT: pixmap = quit_xpm; break;
388 case LFUN_RUNDVIPS: pixmap = update_ps_xpm; break;
392 pixmap = make_ascii_xpm;
393 else if (arg == "latex")
394 pixmap = make_latex_xpm;
399 if (arg == "Section")
400 pixmap = layout_sec_xpm;
401 else if (arg == "LaTeX")
402 pixmap = layout_latex_xpm;
403 else if (arg == "LyX-Code")
404 pixmap = layout_code_xpm;
405 else if (arg == "Scrap")
406 pixmap = layout_scrap_xpm;
408 pixmap = layout_std_xpm;
412 case LFUN_BOLD : pixmap = bold_xpm; break;
413 case LFUN_SANS: pixmap = sans_xpm; break;
414 case LFUN_RUNLATEX: pixmap = run_latex_xpm; break;
415 case LFUN_BUILDPROG: pixmap = build_xpm; break;
416 case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break;
417 case LFUN_PREVIEW: pixmap = view_dvi_xpm; break;
418 case LFUN_INSERT_MATH:
421 pixmap = get_pixmap_from_symbol(arg.c_str(),
427 //pixmap = unknown_xpm;
434 void Toolbar::add(int action, bool doclean)
436 if (doclean && !cleaned) clean();
438 // this is what we do if we want to add to an existing
440 if (!doclean && owner) {
441 // first «hide» the toolbar buttons. This is not a real hide
442 // actually it deletes and frees the button altogether.
443 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
444 toolbarItem *item, *tmp=0;
449 fl_freeze_form(owner->getForm());
453 fl_delete_object(item->icon);
454 fl_free_object(item->icon);
462 fl_unfreeze_form(owner->getForm());
463 cleaned = true; // this is not completely true, but OK anyway
466 // there exist some special actions not part of
467 // kb_action: SEPARATOR, LAYOUTS
471 toolbarItem *newItem,*tmp;
473 if (lyxaction.isPseudoAction(action)) {
475 kb_action act = (kb_action)lyxaction.retrieveActionArg(action, &arg);
476 pixmap = getPixmap(act, arg);
477 help = lyxaction.helpText(act);
480 lyxerr.debug() << "Pseudo action " << action << endl;
482 pixmap = getPixmap((kb_action)action);
483 help = lyxaction.helpText((kb_action)action);
486 // adds an item to the list
488 || action == TOOL_SEPARATOR
489 || action == TOOL_LAYOUTS)
491 newItem = new toolbarItem;
492 newItem->action = action;
493 newItem->pixmap = pixmap;
494 newItem->help = help;
495 // the new item is placed at the end of the list
498 while(tmp->next != 0)
500 // here is tmp->next == 0
505 //if (action == TOOL_LAYOUTS) {
506 // combox = new Combox(FL_COMBOX_DROPLIST);
511 void Toolbar::add(string const & func, bool doclean)
513 int tf = lyxaction.LookupFunc(func.c_str());
516 lyxerr << "Toolbar::add: no LyX command called`"
517 << func << "'exists!" << endl;
524 void Toolbar::clean()
526 toolbarItem *item, *tmp= 0;
531 //now delete all the objects..
533 fl_freeze_form(owner->getForm());
539 lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
545 fl_unfreeze_form(owner->getForm());
546 lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
551 void Toolbar::push(int nth)
553 lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
554 << nth << '\'' << endl;
556 if (nth == 0) return;
559 toolbarItem *tmp = toollist;
563 fl_trigger_object(tmp->icon);
568 // item nth not found...
573 void Toolbar::read(LyXLex &lex)
576 if (lex.GetString() != "\\begin_toolbar")
577 lyxerr << "Toolbar::read: ERROR wrong token:`"
578 << lex.GetString() << '\'' << endl;
584 lex.pushTable(toolTags, TO_LAST - 1);
586 if (lyxerr.debugging(Debug::PARSER))
589 while (lex.IsOK() && !quit) {
591 lyxerr[Debug::TOOLBAR] << "Toolbar::read: current lex text: `"
592 << lex.GetString() << '\'' << endl;
597 func = lex.GetString();
598 lyxerr[Debug::TOOLBAR]
599 << "Toolbar::read TO_ADD func: `"
600 << func << "'" << endl;
618 // should not set automatically
623 lex.printError("Toolbar::read: "
624 "Unknown toolbar tag: `$$Token'");