khtml Library API Documentation

helper.cpp

00001 /*
00002  * This file is part of the CSS implementation for KDE.
00003  *
00004  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  *
00021  * $Id: helper.cpp,v 1.51 2002/10/27 17:13:23 mueller Exp $
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     // Active window border.
00078     { "activeborder", wmgroup, "background", QPalette::Active, QColorGroup::Light },
00079     // Active window caption.
00080     { "activecaption", wmgroup, "background", QPalette::Active, QColorGroup::Text },
00081         // Text in caption, size box, and scrollbar arrow box.
00082     { "captiontext", wmgroup, "activeForeground", QPalette::Active, QColorGroup::Text },
00083     // Face color for three-dimensional display elements.
00084     { "buttonface", wmgroup, 0, QPalette::Inactive, QColorGroup::Button },
00085     // Dark shadow for three-dimensional display elements (for edges facing away from the light source).
00086     { "buttonhighlight", wmgroup, 0, QPalette::Inactive, QColorGroup::Light },
00087     // Shadow color for three-dimensional display elements.
00088     { "buttonshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Shadow },
00089     // Text on push buttons.
00090     { "buttontext", wmgroup, "buttonForeground", QPalette::Inactive, QColorGroup::ButtonText },
00091     // Dark shadow for three-dimensional display elements.
00092     { "threeddarkshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Dark },
00093     // Face color for three-dimensional display elements.
00094     { "threedface", wmgroup, 0, QPalette::Inactive, QColorGroup::Button },
00095     // Highlight color for three-dimensional display elements.
00096     { "threedhighlight", wmgroup, 0, QPalette::Inactive, QColorGroup::Light },
00097     // Light color for three-dimensional display elements (for edges facing the light source).
00098     { "threedlightshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Midlight },
00099     // Dark shadow for three-dimensional display elements.
00100     { "threedshadow", wmgroup, 0, QPalette::Inactive, QColorGroup::Shadow },
00101 
00102     // Inactive window border.
00103     { "inactiveborder", wmgroup, "background", QPalette::Disabled, QColorGroup::Background },
00104     // Inactive window caption.
00105     { "inactivecaption", wmgroup, "inactiveBackground", QPalette::Disabled, QColorGroup::Background },
00106     // Color of text in an inactive caption.
00107     { "inactivecaptiontext", wmgroup, "inactiveForeground", QPalette::Disabled, QColorGroup::Text },
00108     { "graytext", wmgroup, 0, QPalette::Disabled, QColorGroup::Text },
00109 
00110     // Menu background
00111     { "menu", generalgroup, "background", QPalette::Inactive, QColorGroup::Background },
00112     // Text in menus
00113     { "menutext", generalgroup, "foreground", QPalette::Inactive, QColorGroup::Background },
00114 
00115         // Text of item(s) selected in a control.
00116     { "highlight", generalgroup, "selectBackground", QPalette::Inactive, QColorGroup::Background },
00117 
00118     // Text of item(s) selected in a control.
00119     { "highlighttext", generalgroup, "selectForeground", QPalette::Inactive, QColorGroup::Background },
00120 
00121     // Background color of multiple document interface.
00122     { "appworkspace", generalgroup, "background", QPalette::Inactive, QColorGroup::Text },
00123 
00124     // Scroll bar gray area.
00125     { "scrollbar", generalgroup, "background", QPalette::Inactive, QColorGroup::Background },
00126 
00127     // Window background.
00128     { "window", generalgroup, "windowBackground", QPalette::Inactive, QColorGroup::Background },
00129     // Window frame.
00130     { "windowframe", generalgroup, "windowBackground", QPalette::Inactive, QColorGroup::Background },
00131         // WindowText
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     // ### react to style changes
00145     // see http://www.richinstyle.com for details
00146 
00147     /* Mapping system settings to CSS 2
00148      * Tried hard to get an appropriate mapping - schlpbch
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     // InfoBackground
00170     map["infobackground"] = QToolTip::palette().inactive().background();
00171     // InfoText
00172     map["infotext"] = QToolTip::palette().inactive().foreground();
00173 #endif
00174 
00175     KConfig bckgrConfig("kdesktoprc", true, false); // No multi-screen support
00176     bckgrConfig.setGroup("Desktop0");
00177         // Desktop background.
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     // remove white spaces for those broken websites out there :-(
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     // also recognize "color=ffffff"
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         // recognize #12345 (duplicate the last character)
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     // #fffffff as found on msdn.microsoft.com
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         // CSS like rgb(r, g, b) style
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 QPainter *khtml::printpainter = 0;
00282 
00283 void khtml::setPrintPainter( QPainter *printer )
00284 {
00285     printpainter = printer;
00286 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:36 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001