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 / lyxdraw.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  12KB  |  521 lines

  1. #include <config.h>
  2.  
  3. #include "lyxdraw.h"
  4. #include "error.h"
  5.  
  6. extern int reverse_video;
  7. extern char label_color[];
  8. extern char math_color[];
  9. extern char math_frame_color[];
  10. extern char latex_color[];
  11. extern char foot_color[];
  12. extern char fill_color[];
  13. extern char on_off_line_color[];
  14. extern LString background_color;
  15. extern char lighted_color[];
  16. extern char selection_color[];
  17. extern char note_color[];
  18. extern char note_frame_color[];
  19.  
  20. Colormap color_map = 0;
  21. long int background_pixels;
  22.  
  23. // X11 color names
  24. char const * const X11Color[11] =
  25. { "black", "black", "white", "red", "green", "blue", "cyan", "magenta", 
  26.   "yellow", "black", "black" };
  27.  
  28. // Different graphic contexts
  29. static GC clear_gc = NULL;
  30. static GC latex_gc = NULL;
  31. static GC foot_gc = NULL;
  32. static GC math_gc = NULL;
  33. static GC math_frame_gc = NULL;
  34. static GC fill_gc = NULL;
  35. static GC note_gc = NULL;
  36. static GC note_frame_gc = NULL;
  37. static GC copy_gc = NULL;
  38. static GC select_gc = NULL;
  39. static GC on_off_line_gc = NULL;
  40. static GC thin_on_off_line_gc = NULL;
  41. static GC thick_line_gc = NULL;
  42. static GC lighted_gc = NULL;
  43. static GC selection_gc = NULL;
  44. static GC accent_gc[10] = { NULL, NULL, NULL, NULL, NULL,
  45.                 NULL, NULL, NULL, NULL, NULL };
  46. static GC color_gc[11] = { NULL, NULL, NULL, NULL, NULL, NULL,
  47.                NULL, NULL, NULL, NULL, NULL };
  48. static GC minipage_gc = NULL;
  49.  
  50.  
  51. // Allocates color and sets it as foreground
  52. // Returns "false" if unsuccesful
  53. bool setForegroundColor(char const * const color, XGCValues & val)
  54. {
  55.     XColor xcol;
  56.     if (!color_map) {
  57.         color_map = fl_state[fl_get_vclass()].colormap;
  58.             //DefaultColormap(fl_display, DefaultScreen(fl_display));
  59.     }
  60.     if (XParseColor(fl_display, color_map, color, &xcol)
  61.         && XAllocColor(fl_display, color_map, &xcol)) {
  62.         val.foreground = xcol.pixel;
  63.     } else {
  64.         lyxerr.print(LString("LyX: Couldn't get color ") + color);
  65.         return false;
  66.     }
  67.     return true;
  68. }
  69.  
  70.  
  71. static
  72. void do_reverse_video(XGCValues &val)
  73. {
  74.     if (reverse_video) {
  75.         val.foreground=WhitePixel(fl_display,
  76.                       DefaultScreen(fl_display));
  77.         val.background=BlackPixel(fl_display,
  78.                       DefaultScreen(fl_display));
  79.     } else {
  80.         val.foreground=BlackPixel(fl_display,
  81.                       DefaultScreen(fl_display));
  82.         val.background=WhitePixel(fl_display,
  83.                       DefaultScreen(fl_display));
  84.     }
  85. }
  86.  
  87.  
  88. static
  89. GC make_normal_gc(const char* color)
  90. {
  91.     XGCValues val;
  92.     do_reverse_video(val);
  93.     val.function = GXcopy;
  94.     val.graphics_exposures = false;
  95.     setForegroundColor(color, val);
  96.     return XCreateGC(fl_display, RootWindow(fl_display, 0),
  97.              GCBackground | GCForeground | GCFunction |
  98.              GCGraphicsExposures, &val);
  99. }
  100.  
  101.  
  102. static
  103. GC GetNoteGC()
  104. {
  105.     if (note_gc) return note_gc;
  106.     
  107.      note_gc = make_normal_gc(note_color);
  108.  
  109.     return note_gc;
  110. }
  111.  
  112.  
  113. static
  114. GC GetNoteFrameGC()
  115. {
  116.     if (note_frame_gc) return note_frame_gc;
  117.     
  118.      note_frame_gc = make_normal_gc(note_frame_color);
  119.  
  120.     return note_frame_gc;
  121. }
  122.  
  123.  
  124. static
  125. GC GetMathGC()
  126. {
  127.     if (math_gc) return math_gc;
  128.     
  129.      math_gc = make_normal_gc(math_color);
  130.  
  131.     return math_gc;
  132. }
  133.  
  134.  
  135. static
  136. GC GetLatexGC()
  137. {
  138.     if (latex_gc) return latex_gc;
  139.  
  140.     XGCValues val;
  141.     if (reverse_video) {
  142.         val.foreground=WhitePixel(fl_display, DefaultScreen(fl_display));
  143.         val.background=BlackPixel(fl_display, DefaultScreen(fl_display));
  144.     } else {
  145.         val.foreground=BlackPixel(fl_display, DefaultScreen(fl_display));
  146.         val.background=WhitePixel(fl_display, DefaultScreen(fl_display));
  147.     }
  148.     val.function=GXcopy;
  149.     val.graphics_exposures = false;
  150.     setForegroundColor(latex_color, val);
  151.     latex_gc = XCreateGC(fl_display, RootWindow(fl_display, 0), GCBackground 
  152.                  | GCForeground | GCFunction | GCGraphicsExposures, 
  153.                  &val);
  154.     XFlush(fl_display);
  155.     
  156.     return latex_gc;
  157. }
  158.  
  159.  
  160. static
  161. GC GetFootGC()
  162. {
  163.     if (foot_gc) return foot_gc;
  164.     
  165.     foot_gc = make_normal_gc(foot_color);
  166.     
  167.     return foot_gc;
  168. }
  169.  
  170.  
  171. static
  172. GC GetMathFrameGC()
  173. {
  174.     if (math_frame_gc) return math_frame_gc;
  175.     
  176.     math_frame_gc = make_normal_gc(math_frame_color);
  177.     
  178.     return math_frame_gc;
  179. }
  180.  
  181.  
  182. static
  183. GC GetFillGC()
  184. {
  185.     if (fill_gc) return fill_gc;
  186.     
  187.     fill_gc = make_normal_gc(fill_color);
  188.     
  189.     return fill_gc;
  190. }
  191.  
  192.  
  193. static
  194. GC GetClearGC()
  195. {
  196.     if (clear_gc) return clear_gc;
  197.     
  198.     XGCValues val;
  199.  
  200.     if (reverse_video) {
  201.         val.foreground=BlackPixel(fl_display,
  202.                       DefaultScreen(fl_display));
  203.         val.background=WhitePixel(fl_display,
  204.                       DefaultScreen(fl_display));
  205.     } else {
  206.         val.background=BlackPixel(fl_display,
  207.                       DefaultScreen(fl_display));
  208.         val.foreground=WhitePixel(fl_display,
  209.                       DefaultScreen(fl_display));
  210.     }
  211.     val.function=GXcopy;
  212.     val.graphics_exposures = false;
  213.     if (background_color != "white") {
  214.         setForegroundColor(background_color.c_str(), val);
  215.     }
  216.     background_pixels = val.foreground;
  217.     
  218.     clear_gc = XCreateGC(fl_display, RootWindow(fl_display, 0), GCBackground 
  219.                  | GCForeground | GCFunction | GCGraphicsExposures, 
  220.                  &val);
  221.     XFlush(fl_display);
  222.     
  223.     return clear_gc;
  224. }
  225.  
  226.  
  227. static
  228. GC GetOnOffLineGC()
  229. {
  230.     if (on_off_line_gc) return on_off_line_gc;
  231.     
  232.     XGCValues val;
  233.     do_reverse_video(val);
  234.  
  235.     val.function=GXcopy;
  236.     val.graphics_exposures = false;
  237.     setForegroundColor(on_off_line_color, val);
  238.     val.line_width = 0;
  239.     val.line_style = LineOnOffDash;
  240.     on_off_line_gc = XCreateGC(fl_display, RootWindow(fl_display, 0),
  241.                    GCBackground | GCForeground | GCFunction |
  242.                    GCGraphicsExposures | GCLineWidth |
  243.                    GCLineStyle , &val);
  244.     XFlush(fl_display);
  245.  
  246.     return on_off_line_gc;
  247. }
  248.  
  249.  
  250. static
  251. GC GetThickLineGC()
  252. {
  253.     if (thick_line_gc) return thick_line_gc;
  254.     XGCValues val;
  255.     do_reverse_video(val);
  256.  
  257.     val.function=GXcopy;
  258.     val.graphics_exposures = false;
  259.     val.line_width = 2;
  260.     val.line_style = LineSolid;
  261.     thick_line_gc = XCreateGC(fl_display, RootWindow(fl_display, 0),GCBackground 
  262.                   | GCForeground | GCFunction | GCGraphicsExposures
  263.                   | GCLineWidth | GCLineStyle , &val);
  264.     XFlush(fl_display);
  265.     
  266.     return thick_line_gc;
  267. }
  268.  
  269.  
  270. static
  271. GC GetThinOnOffLineGC()
  272. {
  273.     if (thin_on_off_line_gc) return thin_on_off_line_gc;
  274.     XGCValues val;
  275.     do_reverse_video(val);
  276.  
  277.     val.function=GXcopy;
  278.     val.graphics_exposures = false;
  279.     val.line_style = LineOnOffDash;
  280.     val.line_width = 0;
  281.     thin_on_off_line_gc =
  282.         XCreateGC(fl_display, RootWindow(fl_display, 0), GCBackground
  283.               | GCForeground | GCFunction | GCGraphicsExposures
  284.               | GCLineWidth | GCLineStyle , &val);
  285.     XFlush(fl_display);
  286.     
  287.     return thin_on_off_line_gc;
  288. }
  289.  
  290.  
  291. static
  292. GC GetCopyGC()
  293. {
  294.     if (copy_gc) return copy_gc;
  295.     XGCValues val;
  296.     do_reverse_video(val);
  297.  
  298.     val.function=GXcopy;
  299.     val.graphics_exposures = false;
  300.     val.line_style = LineSolid;
  301.     val.line_width = 0;
  302.     copy_gc = XCreateGC(fl_display, RootWindow(fl_display, 0), GCBackground
  303.                 | GCForeground | GCFunction | GCGraphicsExposures
  304.                 | GCLineWidth | GCLineStyle , &val);
  305.     XFlush(fl_display);
  306.  
  307.     return copy_gc;
  308. }
  309.  
  310.  
  311. static
  312. GC GetSelectGC()
  313. {
  314.     if (select_gc) return select_gc;
  315.     XGCValues val;
  316.     unsigned int a = BlackPixel(fl_display, DefaultScreen(fl_display));
  317.     unsigned int b = WhitePixel(fl_display, DefaultScreen(fl_display)); 
  318.     val.plane_mask = a^b;
  319.     val.line_style = LineSolid;
  320.     val.line_width = 2;
  321.     val.graphics_exposures = false;
  322.     val.function=GXinvert;
  323.     select_gc = XCreateGC(fl_display, RootWindow(fl_display, 0),
  324.                   GCFunction | GCGraphicsExposures | GCPlaneMask
  325.                       | GCLineWidth | GCLineStyle , &val);
  326.     XFlush(fl_display);
  327.  
  328.     return select_gc; 
  329. }
  330.  
  331.  
  332. static
  333. GC GetSelectionGC()
  334. {
  335.     if (selection_gc) return selection_gc;
  336.     
  337.     XGCValues val;
  338.  
  339.     if (!reverse_video) {
  340.         val.foreground=BlackPixel(fl_display,
  341.                       DefaultScreen(fl_display));
  342.         val.background=WhitePixel(fl_display,
  343.                       DefaultScreen(fl_display));
  344.     } else {
  345.         val.background=BlackPixel(fl_display,
  346.                       DefaultScreen(fl_display));
  347.         val.foreground=WhitePixel(fl_display,
  348.                       DefaultScreen(fl_display));
  349.     }
  350.     
  351.     val.function=GXcopy;
  352.     val.graphics_exposures = false;
  353.     if (selection_color[0] != 0) {
  354.         if (!setForegroundColor(selection_color, val)) {
  355.             //if (clear_gc) clear_gc = 0;
  356.             lyxerr.print("Used to use FastSelection-method.");
  357.             lyxerr.print("Now we must think up something else.");
  358.         }
  359.     }
  360.     selection_gc = XCreateGC(fl_display, RootWindow(fl_display, 0),
  361.                  GCBackground | GCForeground | GCFunction
  362.                  | GCGraphicsExposures, &val);
  363.  
  364.     return selection_gc;
  365. }
  366.  
  367.  
  368. static
  369. GC GetLightedGC()
  370. {
  371.     if (lighted_gc) return lighted_gc;
  372.     XGCValues val;
  373.     if (reverse_video) {
  374.         val.background=BlackPixel(fl_display, DefaultScreen(fl_display));
  375.     } else {
  376.         val.background=WhitePixel(fl_display, DefaultScreen(fl_display));
  377.     }
  378.     val.foreground=val.background;
  379.     val.function=GXcopy;
  380.     val.graphics_exposures = false;
  381.     val.line_style = LineSolid;
  382.     val.line_width = 0;
  383.     setForegroundColor(lighted_color, val);
  384.     lighted_gc = XCreateGC(fl_display, RootWindow(fl_display, 0), GCBackground
  385.                    | GCForeground | GCFunction | GCGraphicsExposures
  386.                    | GCLineWidth | GCLineStyle , &val);
  387.     XFlush(fl_display);
  388.  
  389.     return lighted_gc;
  390. }
  391.  
  392.  
  393. GC GetColorGC(LyXFont::FONT_COLOR color)
  394. {
  395.     if (color_gc[color]) return color_gc[color];
  396.                
  397.     XGCValues val;
  398.     do_reverse_video(val);
  399.  
  400.     val.function=GXcopy;
  401.     val.graphics_exposures = false;
  402.     setForegroundColor(X11Color[color], val);
  403.     val.line_width = 0;
  404.     val.line_style = LineSolid;
  405.     color_gc[color] = XCreateGC(fl_display, RootWindow(fl_display, 0),
  406.                     GCBackground | GCForeground | GCFunction |
  407.                     GCGraphicsExposures | GCLineWidth |
  408.                     GCLineStyle , &val);
  409.     XFlush(fl_display);
  410.  
  411.     return color_gc[color];
  412. }
  413.  
  414.  
  415. GC GetAccentGC(LyXFont const &f, int line_width)
  416. {
  417.     if (line_width>=10) line_width = 9;
  418.     
  419.     if (accent_gc[line_width]) return accent_gc[line_width];
  420.     
  421.     // Build this one from normal text GC, but change linewidth.
  422.     XGCValues val;
  423.     GC gc = f.getGC();
  424.     XGetGCValues(fl_display, gc, GCBackground | GCForeground | 
  425.              GCFunction | GCGraphicsExposures | GCLineWidth | 
  426.              GCLineStyle | GCPlaneMask, &val);
  427.  
  428.     val.line_width = line_width;
  429.     val.cap_style = CapRound;
  430.     val.join_style = JoinRound;
  431.     
  432.     GC ac = XCreateGC(fl_display, RootWindow(fl_display, 0), 
  433.               GCBackground | GCForeground | GCFunction | 
  434.               GCGraphicsExposures | GCLineWidth | 
  435.               GCLineStyle | GCPlaneMask, &val);
  436.     
  437.     XFlush(fl_display);
  438.     accent_gc[line_width] = ac;
  439.  
  440.     return accent_gc[line_width];
  441. }
  442.  
  443.  
  444. static
  445. GC GetMinipageGC()
  446. {
  447.     if (minipage_gc) return minipage_gc;
  448.         XGCValues val;
  449.     do_reverse_video(val);
  450.  
  451.         val.function=GXcopy;
  452.         val.graphics_exposures = false;
  453.         val.line_style = LineOnOffDash;
  454.         val.line_width = 0;
  455.         setForegroundColor(fill_color, val);
  456.         minipage_gc = XCreateGC(fl_display, RootWindow(fl_display, 0),
  457.                                 GCBackground | GCForeground | GCFunction |
  458.                                 GCGraphicsExposures | GCLineWidth |
  459.                                 GCLineStyle , &val);
  460.         XFlush(fl_display);
  461.  
  462.     return minipage_gc;
  463. }
  464.  
  465.  
  466. GC getGC(gc_type typ)
  467. {
  468.     GC gc = NULL;
  469.     switch(typ) {
  470.     case gc_clear:
  471.         gc = GetClearGC();
  472.         break;
  473.     case gc_latex:
  474.         gc = GetLatexGC();
  475.         break;
  476.     case gc_foot:
  477.         gc = GetFootGC();
  478.         break;
  479.     case gc_math:
  480.         gc = GetMathGC();
  481.         break;
  482.     case gc_math_frame:
  483.         gc = GetMathFrameGC();
  484.         break;
  485.     case gc_fill:
  486.         gc = GetFillGC();
  487.         break;
  488.     case gc_copy:
  489.         gc = GetCopyGC();
  490.         break;
  491.     case gc_select:
  492.         gc = GetSelectGC();
  493.         break;
  494.     case gc_on_off_line:
  495.         gc = GetOnOffLineGC();
  496.         break;
  497.     case gc_thin_on_off_line:
  498.         gc = GetThinOnOffLineGC();
  499.         break;
  500.     case gc_thick_line:
  501.         gc = GetThickLineGC();
  502.         break;
  503.     case gc_lighted:
  504.         gc = GetLightedGC();
  505.         break;
  506.     case gc_selection:
  507.         gc = GetSelectionGC();
  508.         break;
  509.     case gc_minipage:
  510.         gc = GetMinipageGC();
  511.         break;
  512.     case gc_note:
  513.         gc = GetNoteGC();
  514.         break;
  515.     case gc_note_frame:
  516.         gc = GetNoteFrameGC();
  517.         break;
  518.     }
  519.     return gc;
  520. }
  521.