/**************************************************************************** ** ** Implementation of the TQStyleSheet class ** ** Created : 990101 ** ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. ** ** This file is part of the kernel module of the TQt GUI Toolkit. ** ** This file may be used under the terms of the GNU General ** Public License versions 2.0 or 3.0 as published by the Free ** Software Foundation and appearing in the files LICENSE.GPL2 ** and LICENSE.GPL3 included in the packaging of this file. ** Alternatively you may (at your option) use any later version ** of the GNU General Public License if such license has been ** publicly approved by Trolltech ASA (or its successors, if any) ** and the KDE Free TQt Foundation. ** ** Please review the following information to ensure GNU General ** Public Licensing requirements will be met: ** http://trolltech.com/products/qt/licenses/licensing/opensource/. ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** or contact the sales department at sales@trolltech.com. ** ** This file may be used under the terms of the Q Public License as ** defined by Trolltech ASA and appearing in the file LICENSE.TQPL ** included in the packaging of this file. Licensees holding valid TQt ** Commercial licenses may use this file in accordance with the TQt ** Commercial License Agreement provided with the Software. ** ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted ** herein. ** **********************************************************************/ #include "ntqstylesheet.h" #ifndef TQT_NO_RICHTEXT #include "private/qrichtext_p.h" #include "ntqlayout.h" #include "ntqpainter.h" #include "ntqcleanuphandler.h" #include class TQStyleSheetItemData { public: TQStyleSheetItem::DisplayMode disp; int fontitalic; int fontunderline; int fontstrikeout; int fontweight; int fontsize; int fontsizelog; int fontsizestep; int lineSpacing; TQString fontfamily; TQStyleSheetItem *parentstyle; TQString stylename; int ncolumns; TQColor col; bool anchor; int align; TQStyleSheetItem::VerticalAlignment valign; int margin[5]; TQStyleSheetItem::ListStyle list; TQStyleSheetItem::WhiteSpaceMode whitespacemode; TQString contxt; bool selfnest; TQStyleSheet* sheet; }; /*! \class TQStyleSheetItem ntqstylesheet.h \brief The TQStyleSheetItem class provides an encapsulation of a set of text styles. \ingroup text A style sheet item consists of a name and a set of attributes that specifiy its font, color, etc. When used in a \link TQStyleSheet style sheet\endlink (see styleSheet()), items define the name() of a rich text tag and the display property changes associated with it. The \link TQStyleSheetItem::DisplayMode display mode\endlink attribute indicates whether the item is a block, an inline element or a list element; see setDisplayMode(). The treatment of whitespace is controlled by the \link TQStyleSheetItem::WhiteSpaceMode white space mode\endlink; see setWhiteSpaceMode(). An item's margins are set with setMargin(), In the case of list items, the list style is set with setListStyle(). An item may be a hypertext link anchor; see setAnchor(). Other attributes are set with setAlignment(), setVerticalAlignment(), setFontFamily(), setFontSize(), setFontWeight(), setFontItalic(), setFontUnderline(), setFontStrikeOut and setColor(). */ /*! \enum TQStyleSheetItem::AdditionalStyleValues \internal */ /*! \enum TQStyleSheetItem::WhiteSpaceMode This enum defines the ways in which TQStyleSheet can treat whitespace. \value WhiteSpaceNormal any sequence of whitespace (including line-breaks) is equivalent to a single space. \value WhiteSpacePre whitespace must be output exactly as given in the input. \value WhiteSpaceNoWrap multiple spaces are collapsed as with WhiteSpaceNormal, but no automatic line-breaks occur. To break lines manually, use the \c{
} tag. */ /*! \enum TQStyleSheetItem::Margin \value MarginLeft left margin \value MarginRight right margin \value MarginTop top margin \value MarginBottom bottom margin \value MarginAll all margins (left, right, top and bottom) \value MarginVertical top and bottom margins \value MarginHorizontal left and right margins \value MarginFirstLine margin (indentation) of the first line of a paragarph (in addition to the MarginLeft of the paragraph) */ /*! Constructs a new style called \a name for the stylesheet \a parent. All properties in TQStyleSheetItem are initially in the "do not change" state, except \link TQStyleSheetItem::DisplayMode display mode\endlink, which defaults to \c DisplayInline. */ TQStyleSheetItem::TQStyleSheetItem( TQStyleSheet* parent, const TQString& name ) { d = new TQStyleSheetItemData; d->stylename = name.lower(); d->sheet = parent; init(); if (parent) parent->insert( this ); } /*! Copy constructor. Constructs a copy of \a other that is not bound to any style sheet. */ TQStyleSheetItem::TQStyleSheetItem( const TQStyleSheetItem & other ) { d = new TQStyleSheetItemData; *d = *other.d; } /*! Destroys the style. Note that TQStyleSheetItem objects become owned by TQStyleSheet when they are created. */ TQStyleSheetItem::~TQStyleSheetItem() { delete d; } /*! Assignment. Assings a copy of \a other that is not bound to any style sheet. Unbounds first from previous style sheet. */ TQStyleSheetItem& TQStyleSheetItem::operator=( const TQStyleSheetItem& other ) { if ( &other == this ) return *this; delete d; d = new TQStyleSheetItemData; *d = *other.d; return *this; } /*! Returns the style sheet this item is in. */ TQStyleSheet* TQStyleSheetItem::styleSheet() { return d->sheet; } /*! \overload Returns the style sheet this item is in. */ const TQStyleSheet* TQStyleSheetItem::styleSheet() const { return d->sheet; } /*! \internal Internal initialization */ void TQStyleSheetItem::init() { d->disp = DisplayInline; d->fontitalic = Undefined; d->fontunderline = Undefined; d->fontstrikeout = Undefined; d->fontweight = Undefined; d->fontsize = Undefined; d->fontsizelog = Undefined; d->fontsizestep = 0; d->ncolumns = Undefined; d->col = TQColor(); // !isValid() d->anchor = FALSE; d->align = Undefined; d->valign = VAlignBaseline; d->margin[0] = Undefined; d->margin[1] = Undefined; d->margin[2] = Undefined; d->margin[3] = Undefined; d->margin[4] = Undefined; d->list = ListStyleUndefined; d->whitespacemode = WhiteSpaceModeUndefined; d->selfnest = TRUE; d->lineSpacing = Undefined; } /*! Returns the name of the style item. */ TQString TQStyleSheetItem::name() const { return d->stylename; } /*! Returns the \link TQStyleSheetItem::DisplayMode display mode\endlink of the style. \sa setDisplayMode() */ TQStyleSheetItem::DisplayMode TQStyleSheetItem::displayMode() const { return d->disp; } /*! \enum TQStyleSheetItem::DisplayMode This enum type defines the way adjacent elements are displayed. \value DisplayBlock elements are displayed as a rectangular block (e.g. \c{

...

}). \value DisplayInline elements are displayed in a horizontally flowing sequence (e.g. \c{...}). \value DisplayListItem elements are displayed in a vertical sequence (e.g. \c{
  • ...
  • }). \value DisplayNone elements are not displayed at all. */ /*! Sets the display mode of the style to \a m. \sa displayMode() */ void TQStyleSheetItem::setDisplayMode(DisplayMode m) { d->disp=m; } /*! Returns the alignment of this style. Possible values are \c AlignAuto, \c AlignLeft, \c AlignRight, \c AlignCenter or \c AlignJustify. \sa setAlignment(), TQt::AlignmentFlags */ int TQStyleSheetItem::alignment() const { return d->align; } /*! Sets the alignment to \a f. This only makes sense for styles with a \link TQStyleSheetItem::DisplayMode display mode\endlink of DisplayBlock. Possible values are \c AlignAuto, \c AlignLeft, \c AlignRight, \c AlignCenter or \c AlignJustify. \sa alignment(), displayMode(), TQt::AlignmentFlags */ void TQStyleSheetItem::setAlignment( int f ) { d->align = f; } /*! Returns the vertical alignment of the style. Possible values are \c VAlignBaseline, \c VAlignSub or \c VAlignSuper. \sa setVerticalAlignment() */ TQStyleSheetItem::VerticalAlignment TQStyleSheetItem::verticalAlignment() const { return d->valign; } /*! \enum TQStyleSheetItem::VerticalAlignment This enum type defines the way elements are aligned vertically. This is only supported for text elements. \value VAlignBaseline align the baseline of the element (or the bottom, if the element doesn't have a baseline) with the baseline of the parent \value VAlignSub subscript the element \value VAlignSuper superscript the element */ /*! Sets the vertical alignment to \a valign. Possible values are \c VAlignBaseline, \c VAlignSub or \c VAlignSuper. The vertical alignment property is not inherited. \sa verticalAlignment() */ void TQStyleSheetItem::setVerticalAlignment( VerticalAlignment valign ) { d->valign = valign; } /*! Returns TRUE if the style sets an italic font; otherwise returns FALSE. \sa setFontItalic(), definesFontItalic() */ bool TQStyleSheetItem::fontItalic() const { return d->fontitalic > 0; } /*! If \a italic is TRUE sets italic for the style; otherwise sets upright. \sa fontItalic(), definesFontItalic() */ void TQStyleSheetItem::setFontItalic(bool italic) { d->fontitalic = italic?1:0; } /*! Returns TRUE if the style defines a font shape; otherwise returns FALSE. A style does not define any shape until setFontItalic() is called. \sa setFontItalic(), fontItalic() */ bool TQStyleSheetItem::definesFontItalic() const { return d->fontitalic != Undefined; } /*! Returns TRUE if the style sets an underlined font; otherwise returns FALSE. \sa setFontUnderline(), definesFontUnderline() */ bool TQStyleSheetItem::fontUnderline() const { return d->fontunderline > 0; } /*! If \a underline is TRUE, sets underline for the style; otherwise sets no underline. \sa fontUnderline(), definesFontUnderline() */ void TQStyleSheetItem::setFontUnderline(bool underline) { d->fontunderline = underline?1:0; } /*! Returns TRUE if the style defines a setting for the underline property of the font; otherwise returns FALSE. A style does not define this until setFontUnderline() is called. \sa setFontUnderline(), fontUnderline() */ bool TQStyleSheetItem::definesFontUnderline() const { return d->fontunderline != Undefined; } /*! Returns TRUE if the style sets a strike out font; otherwise returns FALSE. \sa setFontStrikeOut(), definesFontStrikeOut() */ bool TQStyleSheetItem::fontStrikeOut() const { return d->fontstrikeout > 0; } /*! If \a strikeOut is TRUE, sets strike out for the style; otherwise sets no strike out. \sa fontStrikeOut(), definesFontStrikeOut() */ void TQStyleSheetItem::setFontStrikeOut(bool strikeOut) { d->fontstrikeout = strikeOut?1:0; } /*! Returns TRUE if the style defines a setting for the strikeOut property of the font; otherwise returns FALSE. A style does not define this until setFontStrikeOut() is called. \sa setFontStrikeOut(), fontStrikeOut() */ bool TQStyleSheetItem::definesFontStrikeOut() const { return d->fontstrikeout != Undefined; } /*! Returns the font weight setting of the style. This is either a valid \c TQFont::Weight or the value \c TQStyleSheetItem::Undefined. \sa setFontWeight(), TQFont */ int TQStyleSheetItem::fontWeight() const { return d->fontweight; } /*! Sets the font weight setting of the style to \a w. Valid values are those defined by \c TQFont::Weight. \sa TQFont, fontWeight() */ void TQStyleSheetItem::setFontWeight(int w) { d->fontweight = w; } /*! Returns the logical font size setting of the style. This is either a valid size between 1 and 7 or \c TQStyleSheetItem::Undefined. \sa setLogicalFontSize(), setLogicalFontSizeStep(), TQFont::pointSize(), TQFont::setPointSize() */ int TQStyleSheetItem::logicalFontSize() const { return d->fontsizelog; } /*! Sets the logical font size setting of the style to \a s. Valid logical sizes are 1 to 7. \sa logicalFontSize(), TQFont::pointSize(), TQFont::setPointSize() */ void TQStyleSheetItem::setLogicalFontSize(int s) { d->fontsizelog = s; } /*! Returns the logical font size step of this style. The default is 0. Tags such as \c big define \c +1; \c small defines \c -1. \sa setLogicalFontSizeStep() */ int TQStyleSheetItem::logicalFontSizeStep() const { return d->fontsizestep; } /*! Sets the logical font size step of this style to \a s. \sa logicalFontSizeStep() */ void TQStyleSheetItem::setLogicalFontSizeStep( int s ) { d->fontsizestep = s; } /*! Sets the font size setting of the style to \a s points. \sa fontSize(), TQFont::pointSize(), TQFont::setPointSize() */ void TQStyleSheetItem::setFontSize(int s) { d->fontsize = s; } /*! Returns the font size setting of the style. This is either a valid point size or \c TQStyleSheetItem::Undefined. \sa setFontSize(), TQFont::pointSize(), TQFont::setPointSize() */ int TQStyleSheetItem::fontSize() const { return d->fontsize; } /*! Returns the font family setting of the style. This is either a valid font family or TQString::null if no family has been set. \sa setFontFamily(), TQFont::family(), TQFont::setFamily() */ TQString TQStyleSheetItem::fontFamily() const { return d->fontfamily; } /*! Sets the font family setting of the style to \a fam. \sa fontFamily(), TQFont::family(), TQFont::setFamily() */ void TQStyleSheetItem::setFontFamily( const TQString& fam) { d->fontfamily = fam; } /*!\obsolete Returns the number of columns for this style. \sa setNumberOfColumns(), displayMode(), setDisplayMode() */ int TQStyleSheetItem::numberOfColumns() const { return d->ncolumns; } /*!\obsolete Sets the number of columns for this style. Elements in the style are divided into columns. This makes sense only if the style uses a block display mode (see TQStyleSheetItem::DisplayMode). \sa numberOfColumns() */ void TQStyleSheetItem::setNumberOfColumns(int ncols) { if (ncols > 0) d->ncolumns = ncols; } /*! Returns the text color of this style or an invalid color if no color has been set. \sa setColor() TQColor::isValid() */ TQColor TQStyleSheetItem::color() const { return d->col; } /*! Sets the text color of this style to \a c. \sa color() */ void TQStyleSheetItem::setColor( const TQColor &c) { d->col = c; } /*! Returns whether this style is an anchor. \sa setAnchor() */ bool TQStyleSheetItem::isAnchor() const { return d->anchor; } /*! If \a anc is TRUE, sets this style to be an anchor (hypertext link); otherwise sets it to not be an anchor. Elements in this style link to other documents or anchors. \sa isAnchor() */ void TQStyleSheetItem::setAnchor(bool anc) { d->anchor = anc; } /*! Returns the whitespace mode. \sa setWhiteSpaceMode() WhiteSpaceMode */ TQStyleSheetItem::WhiteSpaceMode TQStyleSheetItem::whiteSpaceMode() const { return d->whitespacemode; } /*! Sets the whitespace mode to \a m. \sa WhiteSpaceMode */ void TQStyleSheetItem::setWhiteSpaceMode(WhiteSpaceMode m) { d->whitespacemode = m; } /*! Returns the width of margin \a m in pixels. The margin, \a m, can be \c MarginLeft, \c MarginRight, \c MarginTop, \c MarginBottom, or \c MarginFirstLine. \sa setMargin() Margin */ int TQStyleSheetItem::margin(Margin m) const { if (m == MarginAll ) { return d->margin[MarginLeft]; } else if (m == MarginVertical) { return d->margin[MarginTop]; } else if (m == MarginHorizontal) { return d->margin[MarginLeft]; } else { return d->margin[m]; } } /*! Sets the width of margin \a m to \a v pixels. The margin, \a m, can be \c MarginLeft, \c MarginRight, \c MarginTop, \c MarginBottom, \c MarginFirstLine, \c MarginAll, \c MarginVertical or \c MarginHorizontal. The value \a v must be >= 0. \sa margin() */ void TQStyleSheetItem::setMargin(Margin m, int v) { if (m == MarginAll) { d->margin[MarginLeft] = v; d->margin[MarginRight] = v; d->margin[MarginTop] = v; d->margin[MarginBottom] = v; } else if (m == MarginVertical ) { d->margin[MarginTop] = v; d->margin[MarginBottom] = v; } else if (m == MarginHorizontal ) { d->margin[MarginLeft] = v; d->margin[MarginRight] = v; } else { d->margin[m] = v; } } /*! Returns the list style of the style. \sa setListStyle() ListStyle */ TQStyleSheetItem::ListStyle TQStyleSheetItem::listStyle() const { return d->list; } /*! \enum TQStyleSheetItem::ListStyle This enum type defines how the items in a list are prefixed when displayed. \value ListDisc a filled circle (i.e. a bullet) \value ListCircle an unfilled circle \value ListSquare a filled square \value ListDecimal an integer in base 10: \e 1, \e 2, \e 3, ... \value ListLowerAlpha a lowercase letter: \e a, \e b, \e c, ... \value ListUpperAlpha an uppercase letter: \e A, \e B, \e C, ... */ /*! Sets the list style of the style to \a s. This is used by nested elements that have a display mode of \c DisplayListItem. \sa listStyle() DisplayMode ListStyle */ void TQStyleSheetItem::setListStyle(ListStyle s) { d->list=s; } /*! Returns a space-separated list of names of styles that may contain elements of this style. If nothing has been set, contexts() returns an empty string, which indicates that this style can be nested everywhere. \sa setContexts() */ TQString TQStyleSheetItem::contexts() const { return d->contxt; } /*! Sets a space-separated list of names of styles that may contain elements of this style. If \a c is empty, the style can be nested everywhere. \sa contexts() */ void TQStyleSheetItem::setContexts( const TQString& c) { d->contxt = TQChar(' ') + c + TQChar(' '); } /*! Returns TRUE if this style can be nested into an element of style \a s; otherwise returns FALSE. \sa contexts(), setContexts() */ bool TQStyleSheetItem::allowedInContext( const TQStyleSheetItem* s) const { if ( d->contxt.isEmpty() ) return TRUE; return d->contxt.find( TQChar(' ')+s->name()+TQChar(' ')) != -1; } /*! Returns TRUE if this style has self-nesting enabled; otherwise returns FALSE. \sa setSelfNesting() */ bool TQStyleSheetItem::selfNesting() const { return d->selfnest; } /*! Sets the self-nesting property for this style to \a nesting. In order to support "dirty" HTML, paragraphs \c{

    } and list items \c{

  • } are not self-nesting. This means that starting a new paragraph or list item automatically closes the previous one. \sa selfNesting() */ void TQStyleSheetItem::setSelfNesting( bool nesting ) { d->selfnest = nesting; } /*! \internal Sets the linespacing to be at least \a ls pixels. For compatibility with previous TQt releases, small values get treated differently: If \a ls is smaller than the default font line spacing in pixels at parse time, the resulting line spacing is the sum of the default line spacing plus \a ls. We recommend not relying on this behavior. */ void TQStyleSheetItem::setLineSpacing( int ls ) { d->lineSpacing = ls; } /*! \obsolete Returns the linespacing */ int TQStyleSheetItem::lineSpacing() const { return d->lineSpacing; } //************************************************************************ //************************************************************************ /*! \class TQStyleSheet ntqstylesheet.h \ingroup text \brief The TQStyleSheet class is a collection of styles for rich text rendering and a generator of tags. \ingroup graphics \ingroup helpsystem By creating TQStyleSheetItem objects for a style sheet you build a definition of a set of tags. This definition will be used by the internal rich text rendering system to parse and display text documents to which the style sheet applies. Rich text is normally visualized in a TQTextEdit or a TQTextBrowser. However, TQLabel, TQWhatsThis and TQMessageBox also support it, and other classes are likely to follow. With TQSimpleRichText it is possible to use the rich text renderer for custom widgets as well. The default TQStyleSheet object has the following style bindings, sorted by structuring bindings, anchors, character style bindings (i.e. inline styles), special elements such as horizontal lines or images, and other tags. In addition, rich text supports simple HTML tables. The structuring tags are \table \header \i Structuring tags \i Notes \row \i \c{}...\c{} \i A TQt rich text document. It understands the following attributes: \list \i \c title -- The caption of the document. This attribute is easily accessible with TQTextEdit::documentTitle(). \i \c type -- The type of the document. The default type is \c page. It indicates that the document is displayed in a page of its own. Another style is \c detail, which can be used to explain certain expressions in more detail in a few sentences. For \c detail, TQTextBrowser will then keep the current page and display the new document in a small popup similar to TQWhatsThis. Note that links will not work in documents with \c{...}. \i \c bgcolor -- The background color, for example \c bgcolor="yellow" or \c bgcolor="#0000FF". \i \c background -- The background pixmap, for example \c background="granite.xpm". The pixmap name will be resolved by a TQMimeSourceFactory(). \i \c text -- The default text color, for example \c text="red". \i \c link -- The link color, for example \c link="green". \endlist \row \i \c{

    ...

    } \i A top-level heading. \row \i \c{

    ...

    } \i A sublevel heading. \row \i \c{

    ...

    } \i A sub-sublevel heading. \row \i \c{

    ...

    } \c{
    ...
    } \i Headings of lesser importance. \row \i \c{

    ...

    } \i A left-aligned paragraph. Adjust the alignment with the \c align attribute. Possible values are \c left, \c right and \c center. \row \i \c{
    ...}
    \c{
    } \i A centered paragraph. \row \i \c{
    ...}
    \c{
    } \i An indented paragraph that is useful for quotes. \row \i \c{} \i An unordered list. You can also pass a type argument to define the bullet style. The default is \c type=disc; other types are \c circle and \c square. \row \i \c{
      ...
    } \i An ordered list. You can also pass a type argument to define the enumeration label style. The default is \c type="1"; other types are \c "a" and \c "A". \row \i \c{
  • ...
  • } \i A list item. This tag can be used only within the context of \c{
      } or \c{