home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / toolbar.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  11KB  |  459 lines

  1. /* This file is part of
  2. * ======================================================
  3. *           LyX, The Document Processor
  4. *
  5. *        Copyright (C) 1995 Matthias Ettrich
  6. *           Copyright (C) 1995-1998 The LyX Team.
  7. *
  8. *           This file is Copyright 1996-1998
  9. *           Lars Gullik Bj°nnes
  10. *
  11. *======================================================*/
  12.  
  13. //  Added pseudo-action handling, asierra 180296
  14.  
  15. #include <config.h>
  16.  
  17. #ifdef __GNUG__
  18. #pragma implementation
  19. #endif
  20.  
  21. #include "lyx_main.h"
  22. #include "lyx_gui_misc.h"
  23. #include "lyx.h"
  24. #include "toolbar.h"
  25. #include "lyxfunc.h"
  26. #include "error.h"
  27. #include "combox.h"
  28. #include "lyx_cb.h"
  29. #include "LyXView.h"
  30.  
  31. #ifdef TWO_COLOR_ICONS
  32. #include "cut_bw.xpm"
  33. #include "emph_bw.xpm"
  34. #include "fig_bw.xpm"
  35. #include "foot_bw.xpm"
  36. #include "math_bw.xpm"
  37. #include "depth_bw.xpm"
  38. #include "margin_bw.xpm"
  39. #include "melt_bw.xpm"
  40. #include "copy_bw.xpm"
  41. #include "noun_bw.xpm"
  42. #include "paste_bw.xpm"
  43. #include "free_bw.xpm"
  44. #include "tab_bw.xpm"
  45. #include "tex_bw.xpm"
  46. #include "open_bw.xpm"
  47. #include "close_bw.xpm"
  48. #include "save_bw.xpm"
  49. #include "print1_bw.xpm"
  50. #include "quit_bw.xpm"
  51. #include "unknown_bw.xpm"
  52. #else 
  53. #include "cut.xpm"
  54. #include "emph.xpm"
  55. #include "fig.xpm"
  56. #include "foot.xpm"
  57. #include "math.xpm"
  58. #include "depth.xpm"
  59. #include "margin.xpm"
  60. #include "melt.xpm"
  61. #include "copy.xpm"
  62. #include "noun.xpm"
  63. #include "paste.xpm"
  64. #include "free.xpm"
  65. #include "tab.xpm"
  66. #include "tex.xpm"
  67. #include "open.xpm"
  68. #include "close.xpm"
  69. #include "save.xpm"
  70. #include "print1.xpm"
  71. #include "quit.xpm"
  72. #include "unknown.xpm"
  73. #endif
  74.  
  75. // These pixmaps are the same regardless of color:
  76. #include "bold_bw.xpm"
  77. #include "make_ascii_bw.xpm"
  78. #include "make_latex_bw.xpm"
  79. #include "run_latex_bw.xpm"
  80. #include "sans_bw.xpm"
  81. #include "view_dvi_bw.xpm"
  82. #include "view_ps_bw.xpm"
  83.  
  84. //     $Id: toolbar.C,v 1.1.1.1 1998/04/23 16:02:58 larsbj Exp $    
  85.  
  86. #if !defined(lint) && !defined(WITH_WARNINGS)
  87. static char vcid[] = "$Id: toolbar.C,v 1.1.1.1 1998/04/23 16:02:58 larsbj Exp $";
  88. #endif /* lint */
  89.  
  90. extern void LayoutsCB(int, void*);
  91. extern char** get_pixmap_from_symbol(char const *arg, int, int);
  92. extern LyXAction lyxaction;
  93.  
  94.  
  95. Toolbar::Toolbar(ToolbarBackend const &rct, LyXView *o, int x, int y)
  96.     : owner(o), sxpos(x), sypos(y)
  97. {
  98.     toolobj = new FL_OBJECT;
  99.     combox = 0;
  100.     bubble_timer = 0;
  101.     reset();
  102.  
  103.     // extracts the toolbar struct form rct.
  104.     ToolbarBackend::toolbarFunc *tmplist = rct.toollist;
  105.     while (tmplist != NULL) {
  106.         add(tmplist->action);
  107.         lyxerr.debug(LString("tool action: ") + int(tmplist->action),
  108.                   Error::TOOLBAR);
  109.         tmplist=tmplist->next;
  110.     }
  111. }
  112.  
  113.  
  114. // timer-cb for bubble-help (Matthias)
  115. void Toolbar::BubbleTimerCB(FL_OBJECT *, long data){
  116.     FL_OBJECT* ob = (FL_OBJECT*) data;
  117.     char* help = (char*) ob->u_vdata;
  118.     fl_show_oneliner(help, ob->form->x + ob->x,
  119.              ob->form->y + ob->y + ob->h);
  120. }
  121.  
  122.  
  123. // post_handler for bubble-help (Matthias)
  124. int Toolbar::BubblePost(FL_OBJECT *ob, int event,
  125.          FL_Coord /*mx*/, FL_Coord /*my*/, int /*key*/, void */*xev*/)
  126. {
  127.     LString help = (char *)ob->u_vdata;
  128.     Toolbar *t = (Toolbar*)ob->u_ldata;
  129.     
  130.     if(event == FL_ENTER && !help.empty()){
  131.         fl_set_object_callback(t->bubble_timer,
  132.                        BubbleTimerCB, (long) ob);
  133.         fl_set_timer(t->bubble_timer, 1);
  134.     }
  135.     else if(event != FL_MOTION){
  136.         fl_set_timer(t->bubble_timer, 0);
  137.         fl_hide_oneliner();
  138.     }
  139.     return 0;
  140. }
  141.  
  142.  
  143. void Toolbar::activate()
  144. {
  145.     toolbarItem *item, *tmp=NULL;
  146.     item = toollist;
  147.     while(item){
  148.         tmp = item->next;
  149.         if (item->icon) {
  150.             fl_activate_object(item->icon);
  151.         }
  152.         item = tmp;
  153.     }
  154. }
  155.  
  156.  
  157. void Toolbar::deactivate()
  158. {
  159.     toolbarItem *item, *tmp=NULL;
  160.     item = toollist;
  161.     while(item){
  162.         tmp = item->next;
  163.         if (item->icon) {
  164.             fl_deactivate_object(item->icon);
  165.         }
  166.         item = tmp;
  167.     }
  168. }
  169.  
  170.  
  171. void Toolbar::ToolbarCB(FL_OBJECT *ob, long ac)
  172. {
  173.     Toolbar *t = (Toolbar*)ob->u_ldata;
  174.     
  175.     LString res = t->owner->getLyXFunc()->Dispatch(int(ac));
  176.     if(!res.empty())
  177.         lyxerr.print(res); 
  178. }
  179.  
  180.  
  181. int Toolbar::get_toolbar_func(LString const & func)
  182. {
  183.     int action = lyxaction.LookupFunc(func.c_str());
  184.     if (action == -1) {
  185.                if (func == "separator"){
  186.                        action = ToolbarBackend::TOOL_SEPARATOR;
  187.                } else if (func == "layouts"){
  188.                         action = ToolbarBackend::TOOL_LAYOUTS;
  189.                 } else action = 0;
  190.     }
  191.     return action;
  192. }
  193.  
  194.  
  195. void Toolbar::set(bool doingmain)
  196. {
  197.     // we shouldn't set if we have not cleaned
  198.     if (!cleaned) return;
  199.     
  200.     toolbarItem *item;
  201.     FL_OBJECT *obj;
  202.     item = toollist;
  203.     
  204.     if (!doingmain) {
  205.         fl_freeze_form(owner->getForm());
  206.         fl_addto_form(owner->getForm());
  207.     }
  208.  
  209.     // add the time if it don't exist
  210.     if (bubble_timer == NULL)
  211.         bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
  212.                         xpos,ypos,0,0,"Timer");
  213.     
  214.     while(item != NULL) {
  215.         switch(item->action){
  216.           case ToolbarBackend::TOOL_SEPARATOR:
  217.               xpos += sepspace;
  218.               item = item->next;
  219.               break;
  220.           case ToolbarBackend::TOOL_LAYOUTS:
  221.               xpos += standardspacing;
  222.               if (!combox)
  223.                   combox = new Combox(Combox::FL_COMBOX_DROPLIST);
  224.               combox->add(xpos, ypos, 135, height, 300);
  225.               combox->setcallback(LayoutsCB);
  226.               combox->resize(FL_RESIZE_ALL);
  227.               combox->gravity(NorthWestGravity, NorthWestGravity);
  228.               item = item->next;
  229.               xpos += 135;
  230.               break;
  231.           default:
  232.               xpos += standardspacing;
  233.               item->icon = obj =
  234.                   fl_add_pixmapbutton(FL_NORMAL_BUTTON,
  235.                               xpos,ypos,
  236.                               buttonwidth,
  237.                               height,"");
  238.               fl_set_object_boxtype(obj,FL_UP_BOX);
  239.               fl_set_object_color(obj,FL_MCOL,FL_BLUE);
  240.               fl_set_object_resize(obj, FL_RESIZE_ALL);
  241.               fl_set_object_gravity(obj,
  242.                         NorthWestGravity,
  243.                         NorthWestGravity);
  244.               fl_set_object_callback(obj,ToolbarCB,
  245.                          (long)item->action);
  246.               // Remove the blue feedback rectangle
  247.               fl_set_pixmapbutton_focus_outline(obj,0);
  248.  
  249.               // set the bubble-help (Matthias)
  250. #warning Hmm...
  251.               // obj->u_vdata = (void *) item->help.c_str();
  252.               // we need to know what toolbar this item
  253.               // belongs too. (Lgb)
  254.               obj->u_ldata = (long) this;
  255.               
  256.               fl_set_object_posthandler(obj, BubblePost);
  257.  
  258.               fl_set_pixmapbutton_data(obj,item->pixmap);
  259.               item = item->next;
  260.               // we must remember to update the positions
  261.               xpos += buttonwidth;
  262.               // ypos is constant
  263.               /* Here will come a check to see if the new
  264.                * pos is within the bounds of the main frame,
  265.                * and perhaps wrap the toolbar if not.
  266.                */
  267.               break;
  268.         }
  269.     }
  270.     if (!doingmain) {
  271.         fl_end_form();
  272.         fl_unfreeze_form(owner->getForm());
  273.         // Should be safe to do this here.
  274.         owner->updateLayoutChoice();
  275.     }
  276.     
  277.     cleaned = false;
  278. }
  279.  
  280.  
  281. char **Toolbar::getPixmap(kb_action action, LString const & arg)
  282. {
  283.     char **pixmap = unknown_xpm;
  284.     switch(action){
  285.     case LFUN_MENUOPEN:    pixmap = open_xpm; break;
  286.     case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
  287.     case LFUN_MENUPRINT:   pixmap = print1_xpm; break;
  288.     case LFUN_MENUWRITE:   pixmap = save_xpm; break;
  289.     case LFUN_EMPH:     pixmap = emph_xpm; break;
  290.     case LFUN_NOUN:        pixmap = noun_xpm; break;
  291.     case LFUN_FREE:        pixmap = free_xpm; break;
  292.     case LFUN_FOOTMELT:    pixmap = foot_xpm; break;
  293.     case LFUN_DEPTH:       pixmap = depth_xpm; break;
  294.     case LFUN_COPY:        pixmap = copy_xpm; break;
  295.     case LFUN_CUT:         pixmap = cut_xpm; break;
  296.     case LFUN_PASTE:       pixmap = paste_xpm; break;
  297.     case LFUN_TEX:         pixmap = tex_xpm; break;
  298.     case LFUN_MATH_MODE:   pixmap = math_xpm; break;
  299.     case LFUN_MARGINMELT:  pixmap = margin_xpm; break;
  300.     case LFUN_FIGURE:      pixmap = fig_xpm; break;
  301.     case LFUN_TABLE:       pixmap = tab_xpm; break;
  302.     case LFUN_MELT:        pixmap = melt_xpm; break;
  303.     case LFUN_QUIT:        pixmap = quit_xpm; break;
  304.     case LFUN_EXPORT:
  305.     {
  306.         if (arg == "ascii")
  307.             pixmap = make_ascii_xpm;
  308.         else if (arg == "latex")
  309.             pixmap = make_latex_xpm;
  310.     }
  311.     break; 
  312.     case LFUN_BOLD : pixmap = bold_xpm; break; 
  313.     case LFUN_SANS: pixmap = sans_xpm; break; 
  314.     case LFUN_RUNLATEX: pixmap = run_latex_xpm; break; 
  315.     case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break; 
  316.     case LFUN_PREVIEW: pixmap = view_dvi_xpm; break; 
  317.     case LFUN_INSERT_MATH:
  318.     {
  319.         if (!arg.empty())
  320.             pixmap = get_pixmap_from_symbol(arg.c_str(),
  321.                             buttonwidth,
  322.                             height);
  323.     }
  324.     break;
  325.     default:
  326.         //pixmap = unknown_xpm;
  327.         break;
  328.     }
  329.     return pixmap;
  330. }
  331.  
  332.  
  333. void Toolbar::add(int action, bool doclean)
  334. {
  335.     if (doclean && !cleaned) clean();
  336.  
  337.     // this is what we do if we want to add to an existing
  338.     // toolbar.
  339.     if (!doclean && owner) {
  340.         // first ½hide╗ the toolbar buttons. This is not a real hide
  341.         // actually it deletes and frees the button altogether.
  342.         lyxerr.print("Toolbar::add: ½hide╗ the toolbar buttons.");
  343.         toolbarItem *item, *tmp=NULL;
  344.         item = toollist;
  345.  
  346.         lightReset();
  347.         
  348.         fl_freeze_form(owner->getForm());
  349.         while(item){
  350.             tmp = item->next;
  351.             if (item->icon) {
  352.                 fl_delete_object(item->icon);
  353.                 fl_free_object(item->icon);
  354.             }
  355.             item = tmp;
  356.         }
  357.         if (combox) {
  358.             delete combox;
  359.             combox = 0;
  360.         }
  361.         fl_unfreeze_form(owner->getForm());
  362.         cleaned = true; // this is not completely true, but OK anyway
  363.     }
  364.     
  365.     // there exist some special actions not part of
  366.     // kb_action: SEPARATOR, LAYOUTS
  367.     char **pixmap = NULL;
  368.     LString help;
  369.  
  370.     toolbarItem *newItem,*tmp;
  371.  
  372.     if (lyxaction.isPseudoAction(action)) {
  373.         char const *arg;
  374.         kb_action act = (kb_action)lyxaction.retrieveActionArg(action, &arg);
  375.         pixmap = getPixmap(act, arg);
  376.         help = lyxaction.helpText(act);
  377.         help += " ";
  378.         help += arg;
  379.         lyxerr.debug(LString("Pseudo action ") + int(action));
  380.     } else {
  381.         pixmap = getPixmap((kb_action)action);
  382.         help = lyxaction.helpText((kb_action)action);
  383.     }
  384.     
  385.     // adds an item to the list
  386.     if (pixmap != NULL
  387.         || action == ToolbarBackend::TOOL_SEPARATOR
  388.         || action == ToolbarBackend::TOOL_LAYOUTS)
  389.     {
  390.         newItem = new toolbarItem;
  391.         newItem->action = action;
  392.         newItem->pixmap = pixmap;
  393. #warning Hmm...
  394.         //newItem->help = help;
  395.         // the new item is placed at the end of the list
  396.         tmp = toollist;
  397.         if (tmp != NULL){
  398.             while(tmp->next != NULL)
  399.                 tmp = tmp->next;
  400.             // here is tmp->next == NULL
  401.             tmp->next = newItem;
  402.         } else
  403.             toollist = newItem;
  404.     }
  405.     //if (action == TOOL_LAYOUTS) {
  406.     //    combox = new Combox(FL_COMBOX_DROPLIST);
  407.     //}
  408. }
  409.  
  410.  
  411. void Toolbar::clean()
  412. {
  413.     toolbarItem *item, *tmp= NULL;
  414.     item = toollist;
  415.  
  416.     reset();
  417.  
  418.     //now delete all the objects..
  419.     if (owner)
  420.         fl_freeze_form(owner->getForm());
  421.     while (item) {
  422.         tmp = item->next;
  423.         delete item;
  424.         item = tmp;
  425.     }
  426.     //lyxerr.print(LString("Combox: ") + int(combox));
  427.     if (combox) {
  428.         delete combox;
  429.         combox = 0;
  430.     }
  431.     if (owner)
  432.         fl_unfreeze_form(owner->getForm());
  433.     lyxerr.debug("toolbar cleaned",Error::TOOLBAR);
  434.     cleaned = true;
  435. }
  436.  
  437.  
  438. void Toolbar::push(int nth)
  439. {
  440.     lyxerr.debug(LString("Toolbar::push: trying to trigger no `")+nth+'\'',
  441.               Error::TOOLBAR);
  442.     
  443.     if (nth == 0) return;
  444.  
  445.     int count=0;
  446.     toolbarItem *tmp = toollist;
  447.     while (tmp) {
  448.         count++;
  449.         if (count == nth) {
  450.             fl_trigger_object(tmp->icon);
  451.             return;
  452.         }
  453.         tmp = tmp->next;
  454.     }
  455.     // item nth not found...
  456.     LyXBell();
  457. }
  458.