00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "helper.h"
00024 #include <khtmllayout.h>
00025 #include <qmap.h>
00026 #include <qpainter.h>
00027 #include <dom/dom_string.h>
00028 #include <xml/dom_stringimpl.h>
00029 #include <qptrlist.h>
00030 #include <kstaticdeleter.h>
00031 #include <kapplication.h>
00032 #include <kconfig.h>
00033 #include <qtooltip.h>
00034
00035 using namespace DOM;
00036 using namespace khtml;
00037
00038 struct HTMLColors {
00039 QMap<QString,QColor> map;
00040 HTMLColors();
00041 };
00042
00043 struct colorMap {
00044 const char * name;
00045 const char * value;
00046 };
00047
00048 static const colorMap cmap[] = {
00049 { "green", "#008000" },
00050 { "gray", "#808080" },
00051 { "grey", "#808080" },
00052 { "silver", "#c0c0c0" },
00053 { "lime", "#00ff00" },
00054 { "olive", "#808000" },
00055 { "maroon", "#800000" },
00056 { "purple", "#800080" },
00057 { "teal", "#008080" },
00058 { "fuchsia", "#ff00ff" },
00059 { "aqua", "#00ffff" },
00060 { "crimson", "#dc143c" },
00061 { "indigo", "#4b0082" },
00062 { 0, 0 }
00063 };
00064
00065 struct uiColors {
00066 const char * name;
00067 const char * configGroup;
00068 const char * configEntry;
00069 QPalette::ColorGroup group;
00070 QColorGroup::ColorRole role;
00071 };
00072
00073 const char * const wmgroup = "WM";
00074 const char * const generalgroup = "General";
00075
00076 static const uiColors uimap[] = {
00077
00078 { "activeborder", wmgroup, "background", QPalette::Active, QColorGroup::Light },
00079
00080 { "activecaption", wmgroup, "background", QPalette::Active, QColorGroup::Text },
00081
00082 { "captiontext", wmgroup, "activeForeground", QPalette::Active, QColorGroup::Text },
00083
00084 { "buttonface", wmgroup, 0, QPalette::Inactive, QColorGroup::Button },
00085
00086 { "buttonhighlight", wmgroup, 0, QPalette::Inactive, QColorGroup::Light },
00087
00088 { "buttonshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Shadow },
00089
00090 { "buttontext", wmgroup, "buttonForeground", QPalette::Inactive, QColorGroup::ButtonText },
00091
00092 { "threeddarkshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Dark },
00093
00094 { "threedface", wmgroup, 0, QPalette::Inactive, QColorGroup::Button },
00095
00096 { "threedhighlight", wmgroup, 0, QPalette::Inactive, QColorGroup::Light },
00097
00098 { "threedlightshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Midlight },
00099
00100 { "threedshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Shadow },
00101
00102
00103 { "inactiveborder", wmgroup, "background", QPalette::Disabled, QColorGroup::Background },
00104
00105 { "inactivecaption", wmgroup, "inactiveBackground", QPalette::Disabled, QColorGroup::Background },
00106
00107 { "inactivecaptiontext", wmgroup, "inactiveForeground", QPalette::Disabled, QColorGroup::Text },
00108 { "graytext", wmgroup, 0, QPalette::Disabled, QColorGroup::Text },
00109
00110
00111 { "menu", generalgroup, "background", QPalette::Inactive, QColorGroup::Background },
00112
00113 { "menutext", generalgroup, "foreground", QPalette::Inactive, QColorGroup::Background },
00114
00115
00116 { "highlight", generalgroup, "selectBackground", QPalette::Inactive, QColorGroup::Background },
00117
00118
00119 { "highlighttext", generalgroup, "selectForeground", QPalette::Inactive, QColorGroup::Background },
00120
00121
00122 { "appworkspace", generalgroup, "background", QPalette::Inactive, QColorGroup::Text },
00123
00124
00125 { "scrollbar", generalgroup, "background", QPalette::Inactive, QColorGroup::Background },
00126
00127
00128 { "window", generalgroup, "windowBackground", QPalette::Inactive, QColorGroup::Background },
00129
00130 { "windowframe", generalgroup, "windowBackground", QPalette::Inactive, QColorGroup::Background },
00131
00132 { "windowtext", generalgroup, "windowForeground", QPalette::Inactive, QColorGroup::Text },
00133 { "text", generalgroup, 0, QPalette::Inactive, QColorGroup::Text },
00134 { 0, 0, 0, QPalette::NColorGroups, QColorGroup::NColorRoles }
00135 };
00136
00137 HTMLColors::HTMLColors()
00138 {
00139 const colorMap *color = cmap;
00140 while ( color->name ) {
00141 map[color->name] = color->value;
00142 ++color;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 KConfig *globalConfig = KGlobal::config();
00152 const QPalette &pal = kapp->palette();
00153
00154 const uiColors *uicol = uimap;
00155 const char *lastConfigGroup = 0;
00156 while( uicol->name ) {
00157 if ( lastConfigGroup != uicol->configGroup ) {
00158 lastConfigGroup = uicol->configGroup;
00159 globalConfig->setGroup( lastConfigGroup );
00160 }
00161 QColor c = pal.color( uicol->group, uicol->role );
00162 if ( uicol->configEntry )
00163 c = globalConfig->readColorEntry( uicol->configEntry, &c );
00164 map[uicol->name] = c;
00165 ++uicol;
00166 }
00167
00168 #ifndef QT_NO_TOOLTIP
00169
00170 map["infobackground"] = QToolTip::palette().inactive().background();
00171
00172 map["infotext"] = QToolTip::palette().inactive().foreground();
00173 #endif
00174
00175 KConfig bckgrConfig("kdesktoprc", true, false);
00176 bckgrConfig.setGroup("Desktop0");
00177
00178 map["background"] = bckgrConfig.readColorEntry("Color1", &pal.disabled().background());
00179 }
00180
00181
00182
00183 static HTMLColors *htmlColors = 0L;
00184
00185 static KStaticDeleter<HTMLColors> hcsd;
00186
00187 QRgb khtml::parseColor(QString name, bool strictParsing)
00188 {
00189 if( !htmlColors )
00190 htmlColors = hcsd.setObject( new HTMLColors );
00191
00192 int pos;
00193
00194 while ( ( pos = name.find( ' ' ) ) != -1 ) name.remove( pos, 1 );
00195
00196 int len = name.length();
00197
00198 if (!len || (strictParsing && len < 3))
00199 return invalidColor;
00200
00201 if(len == 11 && name.find("transparent", 0, false) == 0)
00202 return transparentColor;
00203
00204 if(len == 10 && name == "-konq-text")
00205 return defaultTextColor;
00206
00207
00208 if (len == 6)
00209 {
00210 bool ok;
00211 int val = name.toInt(&ok, 16);
00212 if(ok)
00213 return (0xff << 24) | val;
00214
00215
00216 if(!strictParsing && name[0] == '#') {
00217 bool ok;
00218 int val = name.right(5).toInt(&ok, 16);
00219 if(ok)
00220 return (0xff << 24) | (val * 16 + ( val&0xf ));
00221 }
00222 if ( !name[0].isLetter() )
00223 return invalidColor;
00224 }
00225
00226
00227 if ( name[0] == '#' && len > 7)
00228 name = name.left(7);
00229
00230 if ( len > 4 && name[0].lower() == 'r' && name[1].lower() == 'g' &&
00231 name[2].lower() == 'b' && name[3] == '(' &&
00232 name[len-1] == ')')
00233 {
00234
00235 DOMString rgb = name.mid(4, name.length()-5);
00236 int count;
00237 khtml::Length* l = rgb.implementation()->toLengthArray(count);
00238 if (count != 3)
00239 return transparentColor;
00240
00241 int c[3];
00242 for (int i = 0; i < 3; ++i) {
00243 c[i] = l[i].width(255);
00244 if (c[i] < 0) c[i] = 0;
00245 if (c[i] > 255) c[i] = 255;
00246 }
00247
00248 QRgb col = qRgb(c[0], c[1], c[2]);
00249 delete [] l;
00250 return col;
00251 }
00252
00253 QColor tc = htmlColors->map[name];
00254 if ( !tc.isValid() )
00255 tc = htmlColors->map[name.lower()];
00256
00257 if (tc.isValid())
00258 return tc.rgb();
00259
00260 tc.setNamedColor(name);
00261 if (tc.isValid()) return tc.rgb();
00262
00263 tc.setNamedColor(name.lower());
00264 if (tc.isValid()) return tc.rgb();
00265
00266 if(!strictParsing) {
00267 bool hasalpha = false;
00268 for(unsigned int i = 0; i < name.length(); i++)
00269 if(name[i].isLetterOrNumber()) {
00270 hasalpha = true;
00271 break;
00272 }
00273
00274 if(!hasalpha)
00275 return qRgb(0, 0, 0);
00276 }
00277
00278 return invalidColor;
00279 }
00280
00281 namespace khtml {
00282 QPainter *printpainter = 0;
00283 }
00284
00285 void khtml::setPrintPainter( QPainter *printer )
00286 {
00287 printpainter = printer;
00288 }