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){
140 FL_OBJECT* ob = (FL_OBJECT*) data;
141 char* help = (char*) ob->u_vdata;
142 fl_show_oneliner(help, ob->form->x + ob->x,
143 ob->form->y + ob->y + ob->h);
147 // post_handler for bubble-help (Matthias)
148 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
149 FL_Coord /*mx*/, FL_Coord /*my*/, int /*key*/, void */*xev*/)
151 string help = (char *)ob->u_vdata;
152 Toolbar *t = (Toolbar*)ob->u_ldata;
154 if(event == FL_ENTER && !help.empty()){
155 fl_set_object_callback(t->bubble_timer,
156 BubbleTimerCB, (long) ob);
157 fl_set_timer(t->bubble_timer, 1);
159 else if(event != FL_MOTION){
160 fl_set_timer(t->bubble_timer, 0);
167 void Toolbar::activate()
169 toolbarItem *item, *tmp=0;
174 fl_activate_object(item->icon);
181 void Toolbar::deactivate()
183 toolbarItem *item, *tmp=0;
188 fl_deactivate_object(item->icon);
195 void Toolbar::ToolbarCB(FL_OBJECT *ob, long ac)
197 Toolbar *t = (Toolbar*)ob->u_ldata;
199 string res = t->owner->getLyXFunc()->Dispatch(int(ac));
201 lyxerr[Debug::TOOLBAR] << res << endl;
205 int Toolbar::get_toolbar_func(string const & func)
207 int action = lyxaction.LookupFunc(func.c_str());
209 if (func == "separator"){
210 action = TOOL_SEPARATOR;
211 } else if (func == "layouts"){
212 action = TOOL_LAYOUTS;
223 //add(LFUN_CLOSEBUFFER);
239 add(LFUN_MARGINMELT);
253 void Toolbar::set(bool doingmain)
255 // we shouldn't set if we have not cleaned
256 if (!cleaned) return;
263 fl_freeze_form(owner->getForm());
264 fl_addto_form(owner->getForm());
268 // Ensure borderwidth is 2 to get visual feedback
269 int bw = fl_get_border_width();
270 fl_set_border_width(-2);
273 // add the time if it don't exist
274 if (bubble_timer == 0)
275 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
276 xpos,ypos,0,0,"Timer");
279 switch(item->action){
285 xpos += standardspacing;
287 combox = new Combox(FL_COMBOX_DROPLIST);
288 combox->add(xpos, ypos, 135, height, 300);
289 combox->setcallback(LayoutsCB);
290 combox->resize(FL_RESIZE_ALL);
291 combox->gravity(NorthWestGravity, NorthWestGravity);
296 xpos += standardspacing;
298 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
302 fl_set_object_boxtype(obj,FL_UP_BOX);
303 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
304 fl_set_object_resize(obj, FL_RESIZE_ALL);
305 fl_set_object_gravity(obj,
308 fl_set_object_callback(obj,ToolbarCB,
311 // Remove the blue feedback rectangle
312 fl_set_pixmapbutton_focus_outline(obj,0);
315 // set the bubble-help (Matthias)
316 obj->u_vdata = (void *) item->help.c_str();
317 // we need to know what toolbar this item
318 // belongs too. (Lgb)
319 obj->u_ldata = (long) this;
321 fl_set_object_posthandler(obj, BubblePost);
323 fl_set_pixmapbutton_data(obj,item->pixmap);
325 // we must remember to update the positions
328 /* Here will come a check to see if the new
329 * pos is within the bounds of the main frame,
330 * and perhaps wrap the toolbar if not.
336 // Reset borderwidth to its default value.
337 fl_set_border_width(bw);
341 fl_unfreeze_form(owner->getForm());
342 // Should be safe to do this here.
343 owner->updateLayoutChoice();
350 char **Toolbar::getPixmap(kb_action action, string const & arg)
352 char **pixmap = unknown_xpm; //0
354 case LFUN_MENUOPEN: pixmap = open_xpm; break;
355 case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
356 case LFUN_MENUPRINT: pixmap = print1_xpm; break;
357 case LFUN_MENUWRITE: pixmap = save_xpm; break;
358 case LFUN_EMPH: pixmap = emph_xpm; break;
359 case LFUN_NOUN: pixmap = noun_xpm; break;
360 case LFUN_FREE: pixmap = free_xpm; break;
361 case LFUN_FOOTMELT: pixmap = foot_xpm; break;
362 case LFUN_DEPTH: pixmap = depth_xpm; break;
363 case LFUN_COPY: pixmap = copy_xpm; break;
364 case LFUN_CUT: pixmap = cut_xpm; break;
365 case LFUN_PASTE: pixmap = paste_xpm; break;
366 case LFUN_TEX: pixmap = tex_xpm; break;
367 case LFUN_MATH_MODE: pixmap = math_xpm; break;
368 case LFUN_MARGINMELT: pixmap = margin_xpm; break;
369 case LFUN_FIGURE: pixmap = fig_xpm; break;
370 case LFUN_TABLE: pixmap = tab_xpm; break;
371 case LFUN_MELT: pixmap = melt_xpm; break;
372 case LFUN_QUIT: pixmap = quit_xpm; break;
373 case LFUN_RUNDVIPS: pixmap = update_ps_xpm; break;
377 pixmap = make_ascii_xpm;
378 else if (arg == "latex")
379 pixmap = make_latex_xpm;
384 if (arg == "Section")
385 pixmap = layout_sec_xpm;
386 else if (arg == "LaTeX")
387 pixmap = layout_latex_xpm;
388 else if (arg == "LyX-Code")
389 pixmap = layout_code_xpm;
390 else if (arg == "Scrap")
391 pixmap = layout_scrap_xpm;
393 pixmap = layout_std_xpm;
397 case LFUN_BOLD : pixmap = bold_xpm; break;
398 case LFUN_SANS: pixmap = sans_xpm; break;
399 case LFUN_RUNLATEX: pixmap = run_latex_xpm; break;
400 case LFUN_BUILDPROG: pixmap = build_xpm; break;
401 case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break;
402 case LFUN_PREVIEW: pixmap = view_dvi_xpm; break;
403 case LFUN_INSERT_MATH:
406 pixmap = get_pixmap_from_symbol(arg.c_str(),
412 //pixmap = unknown_xpm;
419 void Toolbar::add(int action, bool doclean)
421 if (doclean && !cleaned) clean();
423 // this is what we do if we want to add to an existing
425 if (!doclean && owner) {
426 // first «hide» the toolbar buttons. This is not a real hide
427 // actually it deletes and frees the button altogether.
428 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
429 toolbarItem *item, *tmp=0;
434 fl_freeze_form(owner->getForm());
438 fl_delete_object(item->icon);
439 fl_free_object(item->icon);
447 fl_unfreeze_form(owner->getForm());
448 cleaned = true; // this is not completely true, but OK anyway
451 // there exist some special actions not part of
452 // kb_action: SEPARATOR, LAYOUTS
456 toolbarItem *newItem,*tmp;
458 if (lyxaction.isPseudoAction(action)) {
460 kb_action act = (kb_action)lyxaction.retrieveActionArg(action, &arg);
461 pixmap = getPixmap(act, arg);
462 help = lyxaction.helpText(act);
465 lyxerr.debug() << "Pseudo action " << action << endl;
467 pixmap = getPixmap((kb_action)action);
468 help = lyxaction.helpText((kb_action)action);
471 // adds an item to the list
473 || action == TOOL_SEPARATOR
474 || action == TOOL_LAYOUTS)
476 newItem = new toolbarItem;
477 newItem->action = action;
478 newItem->pixmap = pixmap;
479 newItem->help = help;
480 // the new item is placed at the end of the list
483 while(tmp->next != 0)
485 // here is tmp->next == 0
490 //if (action == TOOL_LAYOUTS) {
491 // combox = new Combox(FL_COMBOX_DROPLIST);
496 void Toolbar::add(string const & func, bool doclean)
498 int tf = lyxaction.LookupFunc(func.c_str());
501 lyxerr << "Toolbar::add: no LyX command called`"
502 << func << "'exists!" << endl;
509 void Toolbar::clean()
511 toolbarItem *item, *tmp= 0;
516 //now delete all the objects..
518 fl_freeze_form(owner->getForm());
524 lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
530 fl_unfreeze_form(owner->getForm());
531 lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
536 void Toolbar::push(int nth)
538 lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
539 << nth << '\'' << endl;
541 if (nth == 0) return;
544 toolbarItem *tmp = toollist;
548 fl_trigger_object(tmp->icon);
553 // item nth not found...
558 void Toolbar::read(LyXLex &lex)
561 if (lex.GetString() != "\\begin_toolbar")
562 lyxerr << "Toolbar::read: ERROR wrong token:`"
563 << lex.GetString() << '\'' << endl;
569 lex.pushTable(toolTags, TO_LAST - 1);
571 if (lyxerr.debugging(Debug::PARSER))
574 while (lex.IsOK() && !quit) {
576 lyxerr[Debug::TOOLBAR] << "Toolbar::read: current lex text: `"
577 << lex.GetString() << '\'' << endl;
582 func = lex.GetString();
583 lyxerr[Debug::TOOLBAR]
584 << "Toolbar::read TO_ADD func: `"
585 << func << "'" << endl;
603 // should not set automatically
608 lex.printError("Toolbar::read: "
609 "Unknown toolbar tag: `$$Token'");