]> git.lyx.org Git - features.git/blobdiff - src/Painter.C
make the graphics backround render ok + other stuff more changes than Id like at...
[features.git] / src / Painter.C
index 8d44c84fb244872c03f71c0eb96e569081b6e8a3..b638691601f259d1424b79e0b799204f03cc207f 100644 (file)
@@ -29,6 +29,7 @@
 #include "support/lstrings.h"
 #include "WorkArea.h"
 #include "font.h"
+#include "ColorHandler.h"
 
 using std::endl;
 using std::max;
@@ -36,33 +37,11 @@ using std::max;
 Painter::Painter(WorkArea & wa)
        : PainterBase(wa)
 {
-       colormap = fl_state[fl_get_vclass()].colormap;
-       // Clear the GC cache
-       for (int i = 0; i <= LColor::ignore; ++i) {
-               colorGCcache[i] = 0;
-       }
-}
-
-
-Painter::~Painter() {
-       // Release all the registered GCs
-       for (int i = 0; i <= LColor::ignore; ++i) {
-               if (colorGCcache[i] != 0) {
-                       XFreeGC(display, colorGCcache[i]);
-               }
-       }
-       // Iterate over the line cache and Free the GCs
-       for (LineGCCache::iterator lit = lineGCcache.begin();
-            lit != lineGCcache.end(); ++lit) {
-               XFreeGC(display, (*lit).second);
-       }
+       display = fl_display;
 }
 
 
-Drawable Painter::drawable() const 
-{
-       return owner.getPixmap();
-}
+Painter::~Painter() {}
 
 
 /* Basic drawing routines */
@@ -75,10 +54,12 @@ PainterBase & Painter::point(int x, int y, LColor::color c)
                if (!Lgb_bug_find_hack)
                        lyxerr << "point not called from "
                                "workarea::workhandler\n";
-               lyxerr.debug() << "Painter drawable: " << drawable() << endl;
+               lyxerr.debug() << "Painter drawable: "
+                              << owner.getPixmap() << endl;
        }
        
-       XDrawPoint(display, drawable(), getGCForeground(c), x, y);
+       XDrawPoint(display, owner.getPixmap(),
+                  lyxColorHandler->getGCForeground(c), x, y);
        return *this;
 }
 
@@ -92,11 +73,13 @@ PainterBase & Painter::line(int x1, int y1, int x2, int y2,
                if (!Lgb_bug_find_hack)
                        lyxerr << "line not called from "
                                "workarea::workhandler\n";
-               lyxerr.debug() << "Painter drawable: " << drawable() << endl;
+               lyxerr.debug() << "Painter drawable: "
+                              << owner.getPixmap() << endl;
        }
        
-       XDrawLine(display, drawable(), 
-                 getGCLinepars(ls, lw, col), x1, y1, x2, y2);
+       XDrawLine(display, owner.getPixmap(), 
+                 lyxColorHandler->getGCLinepars(ls, lw, col),
+                 x1, y1, x2, y2);
        return *this;
 }
 
@@ -110,7 +93,8 @@ PainterBase & Painter::lines(int const * xp, int const * yp, int np,
                if (!Lgb_bug_find_hack)
                        lyxerr << "lines not called from "
                                "workarea::workhandler\n";
-               lyxerr.debug() << "Painter drawable: " << drawable() << endl;
+               lyxerr.debug() << "Painter drawable: "
+                              << owner.getPixmap() << endl;
        }
        
 #ifndef HAVE_AUTO_PTR
@@ -123,7 +107,8 @@ PainterBase & Painter::lines(int const * xp, int const * yp, int np,
                points[i].y = yp[i];
        }
 
-        XDrawLines(display, drawable(), getGCLinepars(ls, lw, col), 
+        XDrawLines(display, owner.getPixmap(),
+                  lyxColorHandler->getGCLinepars(ls, lw, col), 
                   points, np, CoordModeOrigin);
 
 #ifndef HAVE_AUTO_PTR
@@ -142,10 +127,12 @@ PainterBase & Painter::rectangle(int x, int y, int w, int h,
                if (!Lgb_bug_find_hack)
                        lyxerr << "rectangle not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: "
+                      << owner.getPixmap() << endl;
        }
        
-       XDrawRectangle(display, drawable(), getGCLinepars(ls, lw, col), 
+       XDrawRectangle(display, owner.getPixmap(),
+                      lyxColorHandler->getGCLinepars(ls, lw, col), 
                       x, y, w, h);
        return *this;
 }
@@ -158,10 +145,12 @@ PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
                if (!Lgb_bug_find_hack)
                        lyxerr << "fillrectangle not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: "
+                      << owner.getPixmap() << endl;
        }
        
-       XFillRectangle(display, drawable(), getGCForeground(col), x, y, w, h);
+       XFillRectangle(display, owner.getPixmap(),
+                      lyxColorHandler->getGCForeground(col), x, y, w, h);
        return *this;
 }
 
@@ -173,7 +162,7 @@ PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
                if (!Lgb_bug_find_hack)
                        lyxerr <<"fillpolygon not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
        }
        
 #ifndef HAVE_AUTO_PTR
@@ -186,7 +175,8 @@ PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
                points[i].y = yp[i];
        }
 
-       XFillPolygon(display, drawable(), getGCForeground(col), points, np, 
+       XFillPolygon(display, owner.getPixmap(),
+                    lyxColorHandler->getGCForeground(col), points, np, 
                     Nonconvex, CoordModeOrigin);
 #ifndef HAVE_AUTO_PTR
        delete[] points;
@@ -203,10 +193,11 @@ PainterBase & Painter::arc(int x, int y,
                if (!Lgb_bug_find_hack)
                        lyxerr << "arc not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
        }
        
-        XDrawArc(display, drawable(), getGCForeground(col),
+        XDrawArc(display, owner.getPixmap(),
+                lyxColorHandler->getGCForeground(col),
                  x, y, w, h, a1, a2);
        return *this;
 }     
@@ -222,7 +213,7 @@ PainterBase & Painter::segments(int const * x1, int const * y1,
                if (!Lgb_bug_find_hack)
                        lyxerr << "segments not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
        }
        
 #ifndef HAVE_AUTO_PTR
@@ -236,7 +227,8 @@ PainterBase & Painter::segments(int const * x1, int const * y1,
                s[i].x2 = x2[i];
                s[i].y2 = y2[i];
        }
-       XDrawSegments(display, drawable(), getGCLinepars(ls, lw, col), s, ns);
+       XDrawSegments(display, owner.getPixmap(),
+                     lyxColorHandler->getGCLinepars(ls, lw, col), s, ns);
 
 #ifndef HAVE_AUTO_PTR
        delete [] s;
@@ -251,14 +243,14 @@ PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
                if (!Lgb_bug_find_hack)
                        lyxerr << "workAreaExpose not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
        }
        
        XGCValues val;
        val.function = GXcopy;
-       GC gc = XCreateGC(display, drawable(),
+       GC gc = XCreateGC(display, owner.getPixmap(),
                          GCFunction, &val);
-       XCopyArea(display, bitmap, drawable(), gc,
+       XCopyArea(display, bitmap, owner.getPixmap(), gc,
                  0, 0, w, h, x, y);
        XFreeGC(display, gc);
        return *this;
@@ -285,12 +277,12 @@ PainterBase & Painter::text(int x, int y, char const * s, int ls,
                if (!Lgb_bug_find_hack)
                        lyxerr << "text not called from "
                                "workarea::workhandler\n";
-               lyxerr << "Painter drawable: " << drawable() << endl;
+               lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
        }
-       GC gc = getGCForeground(f.realColor());
+       GC gc = lyxColorHandler->getGCForeground(f.realColor());
        if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
                lyxfont::XSetFont(display, gc, f);
-               XDrawString(display, drawable(), gc, x, y, s, ls);
+               XDrawString(display, owner.getPixmap(), gc, x, y, s, ls);
        } else {
                LyXFont smallfont(f);
                smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
@@ -301,13 +293,13 @@ PainterBase & Painter::text(int x, int y, char const * s, int ls,
                        if (islower(static_cast<unsigned char>(c))) {
                                c = toupper(c);
                                lyxfont::XSetFont(display, gc, smallfont);
-                               XDrawString(display, drawable(),
+                               XDrawString(display, owner.getPixmap(),
                                            gc, tmpx, y, &c, 1);
                                tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
                                //tmpx += lyxfont::width(c, f);
                        } else {
                                lyxfont::XSetFont(display, gc, f);
-                               XDrawString(display, drawable(),
+                               XDrawString(display, owner.getPixmap(),
                                            gc, tmpx, y, &c, 1);
                                tmpx += lyxfont::XTextWidth(f, &c, 1);
                                //tmpx += lyxfont::width(c, f);
@@ -332,136 +324,3 @@ void Painter::underline(LyXFont const & f, int x, int y, int width)
                                      f.color());
        }
 }
-
-
-// Gets GC according to color
-// Uses caching
-GC Painter::getGCForeground(LColor::color c)
-{
-       if (lyxerr.debugging()) {
-               lyxerr << "Painter drawable: " << drawable() << endl;
-       }
-       
-       if (colorGCcache[c] != 0) return colorGCcache[c];
-
-       XColor xcol, ccol;
-       string s = lcolor.getX11Name(c);
-       XGCValues val;
-
-       // Look up the RGB values for the color, and an approximate
-       // color that we can hope to get on this display.
-        if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
-               lyxerr << _("LyX: Unknown X11 color ") << s
-                      << _(" for ") << lcolor.getGUIName(c) << '\n'
-                      << _("     Using black instead, sorry!.") << endl;
-               unsigned long bla = BlackPixel(display,
-                                              DefaultScreen(display));
-               val.foreground = bla;
-       // Try the exact RGB values first, then the approximate.
-       } else if (XAllocColor(display, colormap, &xcol) != 0) {
-               if (lyxerr.debugging()) {
-                       lyxerr << _("LyX: X11 color ") << s
-                              << _(" allocated for ") 
-                              << lcolor.getGUIName(c) << endl;
-               }
-               val.foreground = xcol.pixel;
-       } else if (XAllocColor(display, colormap, &ccol)) {
-               lyxerr << _("LyX: Using approximated X11 color ") << s
-                      << _(" allocated for ")
-                      << lcolor.getGUIName(c) << endl;
-               val.foreground = xcol.pixel;
-       } else {
-               // Here we are traversing the current colormap to find
-               // the color closest to the one we want.
-               Visual * vi = DefaultVisual(display, DefaultScreen(display));
-
-               XColor * cmap = new XColor[vi->map_entries];
-
-               for(int i = 0; i < vi->map_entries; ++i) {
-                       cmap[i].pixel = i;
-               }
-               XQueryColors(display, colormap, cmap, vi->map_entries);
-
-               // Walk through the cmap and look for close colors.
-               int closest_pixel = 0;
-               double closest_distance = 1e20; // we want to minimize this
-               double distance = 0;
-               for(int t = 0; t < vi->map_entries; ++t) {
-                       // The Euclidean distance between two points in 
-                       // a three-dimensional space, the RGB color-cube,
-                       // is used as the distance measurement between two
-                       // colors.
-
-                       // Since square-root is monotonous, we don't have to
-                       // take the square-root to find the minimum, and thus 
-                       // we use the squared distance instead to be faster.
-
-                       // If we want to get fancy, we could convert the RGB
-                       // coordinates to a different color-cube, maybe HSV,
-                       // but the RGB cube seems to work great.  (Asger)
-                       distance = pow(cmap[t].red   - xcol.red,   2.0) +
-                                  pow(cmap[t].green - xcol.green, 2.0) +
-                                  pow(cmap[t].blue  - xcol.blue,  2.0);
-                       if (distance < closest_distance) {
-                               closest_distance = distance;
-                               closest_pixel = t;
-                       }
-               }
-               lyxerr << _("LyX: Couldn't allocate '") << s 
-                      << _("' for ") << lcolor.getGUIName(c)
-                      << _(" with (r,g,b)=(") 
-                      << xcol.red << "," << xcol.green << ","
-                      << xcol.blue << ").\n"
-                      << _("     Using closest allocated "
-                           "color with (r,g,b)=(") 
-                      << cmap[closest_pixel].red << ","
-                      << cmap[closest_pixel].green << ","
-                      << cmap[closest_pixel].blue << ") instead.\n"
-                      << "Pixel [" << closest_pixel << "] is used." << endl;
-               val.foreground = cmap[closest_pixel].pixel;
-               delete[] cmap;
-       }
-
-       val.function = GXcopy;
-       return colorGCcache[c] = XCreateGC(display, drawable(),
-                                   GCForeground | GCFunction, &val);
-}
-
-
-// Gets GC for line
-GC Painter::getGCLinepars(enum line_style ls,
-                         enum line_width lw, LColor::color c)
-{
-       if (lyxerr.debugging()) {
-               lyxerr << "Painter drawable: " << drawable() << endl;
-       }
-       
-       int index = lw + (ls << 1) + (c << 3);
-
-       if (lineGCcache.find(index) != lineGCcache.end())
-               return lineGCcache[index];
-
-       XGCValues val;
-       XGetGCValues(display, getGCForeground(c), GCForeground, &val);
-       
-       switch (lw) {
-       case line_thin:         val.line_width = 0; break;
-       case line_thick:        val.line_width = 2; break;
-       }
-       
-       switch (ls) {
-       case line_solid:        val.line_style = LineSolid; break;
-       case line_onoffdash:    val.line_style = LineOnOffDash; break;
-       case line_doubledash:   val.line_style = LineDoubleDash; break;
-       }
-
-
-       val.cap_style = CapRound;
-       val.join_style = JoinRound;
-       val.function = GXcopy;
-
-       return lineGCcache[index] =
-               XCreateGC(display, drawable(), 
-                         GCForeground | GCLineStyle | GCLineWidth | 
-                         GCCapStyle | GCJoinStyle | GCFunction, &val);
-}