-/* This file is part of
- * ======================================================
+/**
+ * \file ColorHandler.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * LyX, The Document Processor
+ * \author unknown
*
- * Copyright 1998-2001 The LyX Team
- *
- *======================================================*/
+ * Full author contact details are available in file CREDITS.
+ */
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "ColorHandler.h"
-#include "LColor.h"
-#include "gettext.h"
+
#include "debug.h"
+#include "gettext.h"
+#include "LColor.h"
+
+#include "support/lstrings.h"
+#include "support/tostr.h"
-#include "frontends/GUIRunTime.h"
+#include "lyx_forms.h"
#include <boost/scoped_array.hpp>
#include <cmath>
+using lyx::support::bformat;
+
+#ifndef CXX_GLOBAL_CSTD
+using std::pow;
+#endif
+
using std::endl;
+using std::string;
+
+
+namespace {
+
+ string tostr(XColor const & col)
+ {
+ return bformat("(%1$s,%2$s,%3$s)",
+ ::tostr(col.red), ::tostr(col.green), ::tostr(col.blue));
+ }
+
+}
LyXColorHandler::LyXColorHandler()
+ : colorGCcache(LColor::ignore + 1)
{
- display = GUIRunTime::x11Display();
+ display = fl_get_display();
drawable = XCreatePixmap(display,
- RootWindow(display, GUIRunTime::x11Screen()),
- 10, 10,
- GUIRunTime::x11VisualDepth());
+ RootWindow(display, fl_screen),
+ 10, 10, fl_get_visual_depth());
- colormap = GUIRunTime::x11Colormap();
+ colormap = fl_state[fl_get_vclass()].colormap;
// Clear the GC cache
- for (int i = 0; i <= LColor::ignore; ++i) {
+ for (string::size_type i = 0; i < colorGCcache.size(); ++i) {
colorGCcache[i] = 0;
}
}
LyXColorHandler::~LyXColorHandler()
{
// Release all the registered GCs
- for (int i = 0; i <= LColor::ignore; ++i) {
+ for (unsigned i = 0; i < colorGCcache.size(); ++i) {
if (colorGCcache[i] != 0) {
XFreeGC(display, colorGCcache[i]);
}
}
-unsigned long LyXColorHandler::colorPixel(LColor::color c)
+unsigned long LyXColorHandler::colorPixel(LColor_color c)
{
XGCValues val;
XGetGCValues(display, getGCForeground(c), GCForeground, &val);
}
-// Gets GC according to color
-// Uses caching
-GC LyXColorHandler::getGCForeground(LColor::color c)
+GC LyXColorHandler::getGCForeground(string const & s)
{
- //if (lyxerr.debugging()) {
- // lyxerr << "Painter drawable: " << drawable() << endl;
- //}
-
- if (colorGCcache[c] != 0)
- return colorGCcache[c];
-
XColor xcol;
XColor ccol;
- string const 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;
+ lyxerr << bformat(
+ _("LyX: Unknown X11 color %1$s\n"
+ " Using black instead, sorry!"),
+ s) << 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(Debug::GUI)) {
- lyxerr << _("LyX: X11 color ") << s
- << _(" allocated for ")
- << lcolor.getGUIName(c) << endl;
+ lyxerr << bformat(_("LyX: X11 color %1$s allocated"),
+ s) << 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.
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;
+
+ lyxerr << bformat(
+ _("LyX: Couldn't allocate '%1$s' with (r,g,b)=%3$s.\n"),
+ s, tostr(xcol));
+
+ lyxerr << bformat(
+ _(" Using closest allocated color with (r,g,b)=%1$s instead.\n"
+ "Pixel [%2$s] is used."),
+ tostr(cmap[closest_pixel]), tostr(closest_pixel)) << endl;
+
val.foreground = cmap[closest_pixel].pixel;
}
-
val.function = GXcopy;
- return colorGCcache[c] = XCreateGC(display, drawable,
- GCForeground | GCFunction, &val);
+ return XCreateGC(display, drawable,
+ GCForeground | GCFunction, &val);
+}
+
+// Gets GC according to color
+// Uses caching
+GC LyXColorHandler::getGCForeground(LColor_color c)
+{
+ if (static_cast<unsigned>(c) >= colorGCcache.size()) {
+ colorGCcache.resize(c + 1, 0);
+ }
+
+ if (colorGCcache[c] != 0) {
+ return colorGCcache[c];
+ }
+ XColor xcol;
+ XColor ccol;
+ string const s = lcolor.getX11Name(c);
+ // 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 << bformat(
+ _("LyX: Unknown X11 color %1$s for %2$s\n"),
+ s, lcolor.getGUIName(c)) << endl;
+ }
+ return colorGCcache[c] = getGCForeground(s);
}
// Gets GC for line
-GC LyXColorHandler::getGCLinepars(PainterBase::line_style ls,
- PainterBase::line_width lw, LColor::color c)
+GC LyXColorHandler::getGCLinepars(Painter::line_style ls,
+ Painter::line_width lw, LColor_color c)
{
//if (lyxerr.debugging()) {
// lyxerr << "Painter drawable: " << drawable() << endl;
XGetGCValues(display, getGCForeground(c), GCForeground, &val);
switch (lw) {
- case PainterBase::line_thin:
+ case Painter::line_thin:
val.line_width = 0;
break;
- case PainterBase::line_thick:
+ case Painter::line_thick:
val.line_width = 2;
break;
}
switch (ls) {
- case PainterBase::line_solid:
+ case Painter::line_solid:
val.line_style = LineSolid;
break;
- case PainterBase::line_onoffdash:
+ case Painter::line_onoffdash:
val.line_style = LineOnOffDash;
break;
- case PainterBase::line_doubledash:
- val.line_style = LineDoubleDash;
- break;
}
// update GC cache after color redefinition
-void LyXColorHandler::updateColor (LColor::color c)
+void LyXColorHandler::updateColor (LColor_color c)
{
// color GC cache
GC gc = colorGCcache[c];
gc = it->second;
XFreeGC(display, gc);
lineGCcache.erase(it);
- getGCLinepars(PainterBase::line_style(ls),
- PainterBase::line_width(lw), c);
+ getGCLinepars(Painter::line_style(ls),
+ Painter::line_width(lw), c);
}
}
-
}
+
//
boost::scoped_ptr<LyXColorHandler> lyxColorHandler;