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 "LyXAction.h"
33 #include "support/lstrings.h"
35 #ifdef TWO_COLOR_ICONS
37 #include "emph_bw.xpm"
39 #include "foot_bw.xpm"
40 #include "math_bw.xpm"
41 #include "depth_bw.xpm"
42 #include "margin_bw.xpm"
43 #include "melt_bw.xpm"
44 #include "copy_bw.xpm"
45 #include "noun_bw.xpm"
46 #include "paste_bw.xpm"
47 #include "free_bw.xpm"
50 #include "open_bw.xpm"
51 #include "close_bw.xpm"
52 #include "save_bw.xpm"
53 #include "print1_bw.xpm"
54 #include "quit_bw.xpm"
55 #include "typeset_ps_bw.xpm"
56 #include "unknown_bw.xpm"
77 #include "typeset_ps.xpm"
78 #include "unknown.xpm"
81 // These pixmaps are the same regardless of color:
82 #include "bold_bw.xpm"
83 #include "make_ascii_bw.xpm"
84 #include "make_latex_bw.xpm"
85 #include "run_latex_bw.xpm"
86 #include "sans_bw.xpm"
87 #include "view_dvi_bw.xpm"
88 #include "view_ps_bw.xpm"
89 #include "layout_code.xpm"
90 #include "layout_latex.xpm"
91 #include "layout_scrap.xpm"
92 #include "layout_sec.xpm"
93 #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)
128 // extracts the toolbar struct form rct.
129 toolbarItem * tmplist = rct.toollist;
130 while (tmplist != 0) {
131 add(tmplist->action);
132 lyxerr[Debug::TOOLBAR] << "tool action: "
133 << tmplist->action << endl;
134 tmplist= tmplist->next;
139 // timer-cb for bubble-help (Matthias)
140 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
142 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
143 char * help = static_cast<char*>(ob->u_vdata);
144 fl_show_oneliner(help, ob->form->x + ob->x,
145 ob->form->y + ob->y + ob->h);
149 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
151 Toolbar::BubbleTimerCB(ob, data);
155 // post_handler for bubble-help (Matthias)
156 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
157 FL_Coord /*mx*/, FL_Coord /*my*/,
158 int /*key*/, void */*xev*/)
160 string help = static_cast<char *>(ob->u_vdata);
161 Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
163 if(event == FL_ENTER && !help.empty()){
164 fl_set_object_callback(t->bubble_timer,
165 C_Toolbar_BubbleTimerCB,
166 reinterpret_cast<long>(ob));
167 fl_set_timer(t->bubble_timer, 1);
169 else if(event != FL_MOTION){
170 fl_set_timer(t->bubble_timer, 0);
177 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
178 FL_Coord /*mx*/, FL_Coord /*my*/,
181 return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
185 void Toolbar::activate()
187 toolbarItem * tmp= 0;
188 toolbarItem * item = toollist;
192 fl_activate_object(item->icon);
199 void Toolbar::deactivate()
201 toolbarItem * tmp= 0;
202 toolbarItem * item = toollist;
206 fl_deactivate_object(item->icon);
213 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
215 Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
217 string res = t->owner->getLyXFunc()->Dispatch(int(ac));
219 lyxerr[Debug::TOOLBAR] << res << endl;
223 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
225 Toolbar::ToolbarCB(ob, data);
229 int Toolbar::get_toolbar_func(string const & func)
231 int action = lyxaction.LookupFunc(func.c_str());
233 if (func == "separator"){
234 action = TOOL_SEPARATOR;
235 } else if (func == "layouts"){
236 action = TOOL_LAYOUTS;
247 //add(LFUN_CLOSEBUFFER);
263 add(LFUN_MARGINMELT);
277 void Toolbar::set(bool doingmain)
279 // we shouldn't set if we have not cleaned
280 if (!cleaned) return;
283 toolbarItem * item = toollist;
286 fl_freeze_form(owner->getForm());
287 fl_addto_form(owner->getForm());
291 // Ensure borderwidth is 2 to get visual feedback
292 int bw = fl_get_border_width();
293 fl_set_border_width(-2);
296 // add the time if it don't exist
297 if (bubble_timer == 0)
298 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
299 xpos, ypos, 0, 0, "Timer");
302 switch(item->action){
308 xpos += standardspacing;
310 combox = new Combox(FL_COMBOX_DROPLIST);
311 combox->add(xpos, ypos, 135, height, 300);
312 combox->setcallback(LayoutsCB);
313 combox->resize(FL_RESIZE_ALL);
314 combox->gravity(NorthWestGravity, NorthWestGravity);
319 xpos += standardspacing;
321 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
325 fl_set_object_boxtype(obj, FL_UP_BOX);
326 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
327 fl_set_object_resize(obj, FL_RESIZE_ALL);
328 fl_set_object_gravity(obj,
331 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
332 static_cast<long>(item->action));
334 // Remove the blue feedback rectangle
335 fl_set_pixmapbutton_focus_outline(obj, 0);
338 // set the bubble-help (Matthias)
339 #warning This is dangerous!
340 obj->u_vdata = const_cast<char*>(item->help.c_str());
341 // we need to know what toolbar this item
342 // belongs too. (Lgb)
343 obj->u_ldata = reinterpret_cast<long>(this);
345 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
347 fl_set_pixmapbutton_data(obj, const_cast<char**>(item->pixmap));
349 // we must remember to update the positions
352 /* Here will come a check to see if the new
353 * pos is within the bounds of the main frame,
354 * and perhaps wrap the toolbar if not.
360 // Reset borderwidth to its default value.
361 fl_set_border_width(bw);
365 fl_unfreeze_form(owner->getForm());
366 // Should be safe to do this here.
367 owner->updateLayoutChoice();
374 char const ** Toolbar::getPixmap(kb_action action, string const & arg)
376 char const ** pixmap = unknown_xpm; //0
378 case LFUN_MENUOPEN: pixmap = open_xpm; break;
379 case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
380 case LFUN_MENUPRINT: pixmap = print1_xpm; break;
381 case LFUN_MENUWRITE: pixmap = save_xpm; break;
382 case LFUN_EMPH: pixmap = emph_xpm; break;
383 case LFUN_NOUN: pixmap = noun_xpm; break;
384 case LFUN_FREE: pixmap = free_xpm; break;
385 case LFUN_FOOTMELT: pixmap = foot_xpm; break;
386 case LFUN_DEPTH: pixmap = depth_xpm; break;
387 case LFUN_COPY: pixmap = copy_xpm; break;
388 case LFUN_CUT: pixmap = cut_xpm; break;
389 case LFUN_PASTE: pixmap = paste_xpm; break;
390 case LFUN_TEX: pixmap = tex_xpm; break;
391 case LFUN_MATH_MODE: pixmap = math_xpm; break;
392 case LFUN_MARGINMELT: pixmap = margin_xpm; break;
393 case LFUN_FIGURE: pixmap = fig_xpm; break;
394 case LFUN_TABLE: pixmap = tab_xpm; break;
395 case LFUN_MELT: pixmap = melt_xpm; break;
396 case LFUN_QUIT: pixmap = quit_xpm; break;
397 case LFUN_RUNDVIPS: pixmap = update_ps_xpm; break;
401 pixmap = make_ascii_xpm;
402 else if (arg == "latex")
403 pixmap = make_latex_xpm;
408 if (arg == "Section")
409 pixmap = layout_sec_xpm;
410 else if (arg == "LaTeX")
411 pixmap = layout_latex_xpm;
412 else if (arg == "LyX-Code")
413 pixmap = layout_code_xpm;
414 else if (arg == "Scrap")
415 pixmap = layout_scrap_xpm;
417 pixmap = layout_std_xpm;
421 case LFUN_BOLD : pixmap = bold_xpm; break;
422 case LFUN_SANS: pixmap = sans_xpm; break;
423 case LFUN_RUNLATEX: pixmap = run_latex_xpm; break;
424 case LFUN_BUILDPROG: pixmap = build_xpm; break;
425 case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break;
426 case LFUN_PREVIEW: pixmap = view_dvi_xpm; break;
427 case LFUN_INSERT_MATH:
430 pixmap = get_pixmap_from_symbol(arg.c_str(),
436 //pixmap = unknown_xpm;
443 void Toolbar::add(int action, bool doclean)
445 if (doclean && !cleaned) clean();
447 // this is what we do if we want to add to an existing
449 if (!doclean && owner) {
450 // first «hide» the toolbar buttons. This is not a real hide
451 // actually it deletes and frees the button altogether.
452 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
453 toolbarItem * tmp= 0;
454 toolbarItem * item = toollist;
458 fl_freeze_form(owner->getForm());
462 fl_delete_object(item->icon);
463 fl_free_object(item->icon);
471 fl_unfreeze_form(owner->getForm());
472 cleaned = true; // this is not completely true, but OK anyway
475 // there exist some special actions not part of
476 // kb_action: SEPARATOR, LAYOUTS
477 char const ** pixmap = 0;
480 toolbarItem * newItem, * tmp;
482 if (lyxaction.isPseudoAction(action)) {
484 kb_action act = static_cast<kb_action>
485 (lyxaction.retrieveActionArg(action, arg));
486 pixmap = getPixmap(act, arg);
487 help = lyxaction.helpText(act);
490 lyxerr.debug() << "Pseudo action " << action << endl;
492 pixmap = getPixmap(static_cast<kb_action>(action));
493 help = lyxaction.helpText(static_cast<kb_action>(action));
496 // adds an item to the list
498 || action == TOOL_SEPARATOR
499 || action == TOOL_LAYOUTS)
501 newItem = new toolbarItem;
502 newItem->action = action;
503 newItem->pixmap = pixmap;
504 newItem->help = help;
505 // the new item is placed at the end of the list
508 while(tmp->next != 0)
510 // here is tmp->next == 0
515 //if (action == TOOL_LAYOUTS) {
516 // combox = new Combox(FL_COMBOX_DROPLIST);
521 void Toolbar::add(string const & func, bool doclean)
523 int tf = lyxaction.LookupFunc(func.c_str());
526 lyxerr << "Toolbar::add: no LyX command called`"
527 << func << "'exists!" << endl;
534 void Toolbar::clean()
536 toolbarItem * tmp = 0;
537 toolbarItem * item = toollist;
541 //now delete all the objects..
543 fl_freeze_form(owner->getForm());
549 lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
555 fl_unfreeze_form(owner->getForm());
556 lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
561 void Toolbar::push(int nth)
563 lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
564 << nth << '\'' << endl;
566 if (nth == 0) return;
569 toolbarItem * tmp = toollist;
573 fl_trigger_object(tmp->icon);
578 // item nth not found...
583 void Toolbar::read(LyXLex & lex)
586 if (lex.GetString() != "\\begin_toolbar")
587 lyxerr << "Toolbar::read: ERROR wrong token:`"
588 << lex.GetString() << '\'' << endl;
594 lex.pushTable(toolTags, TO_LAST - 1);
596 if (lyxerr.debugging(Debug::PARSER))
599 while (lex.IsOK() && !quit) {
601 lyxerr[Debug::TOOLBAR] << "Toolbar::read: current lex text: `"
602 << lex.GetString() << '\'' << endl;
607 func = lex.GetString();
608 lyxerr[Debug::TOOLBAR]
609 << "Toolbar::read TO_ADD func: `"
610 << func << "'" << endl;
628 // should not set automatically
633 lex.printError("Toolbar::read: "
634 "Unknown toolbar tag: `$$Token'");