diff options
| author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
|---|---|---|
| committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
| commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
| tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /src/kernel | |
| download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip | |
Add Qt3 development HEAD version
Diffstat (limited to 'src/kernel')
227 files changed, 167171 insertions, 0 deletions
diff --git a/src/kernel/makepsheader.pl b/src/kernel/makepsheader.pl new file mode 100755 index 0000000..b842e14 --- /dev/null +++ b/src/kernel/makepsheader.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl + +open(INPUT, 'qpsprinter.ps') + or die "Can't open qpsprinter.ps"; + +$dontcompress = 1; +while(<INPUT>) { + $line = $_; + chomp $line; + if ( /ENDUNCOMPRESS/ ) { + $dontcompress = 0; + } + $line =~ s/%.*$//; + $line = $line; + if ( $dontcompress eq 1 ) { + push(@uncompressed, $line); + } else { + push(@lines, $line); + } +# print "$line\n"; +} + +$uc = join(" ", @uncompressed); +$uc =~ s,\t+, ,g; +$uc=~ s, +, ,g; + +$h = join(" ", @lines); +$h =~ s,\t+, ,g; +$h =~ s, +, ,g; +$h = $h.' '; + +# now compress as much as possible +$h =~ s/ def / d /g; +$h =~ s/ bind def / D /g; +$h =~ s/ dup dup / d2 /g; +$h =~ s/ exch d / ED /g; +$h =~ s/ lineto / LT /g; +$h =~ s/ moveto / MT /g; +$h =~ s/ stroke / S /g; +$h =~ s/ setfont / F /g; +$h =~ s/ setlinewidth / SW /g; +$h =~ s/ closepath / CP /g; +$h =~ s/ rlineto / RL /g; +$h =~ s/ newpath / NP /g; +$h =~ s/ currentmatrix / CM /g; +$h =~ s/ setmatrix / SM /g; +$h =~ s/ translate / TR /g; +$h =~ s/ setdash / SD /g; +$h =~ s/ aload pop setrgbcolor / SC /g; +$h =~ s/ currentfile read pop / CR /g; +$h =~ s/ index / i /g; +$h =~ s/ bitshift / bs /g; +$h =~ s/ setcolorspace / scs /g; +$h =~ s/ dict dup begin / DB /g; +$h =~ s/ end d / DE /g; +$h =~ s/ ifelse / ie /g; +$h =~ s/ astore pop / sp /g; + +# add the uncompressed part of the header before +$h = $uc.' '.$h; + + + +#print $h; + +# wordwrap at col 76 +@head = split(' ', $h); +$line = shift @head; +while( @head ) { + $token = shift @head; + chomp $token; +# print "\nl=$l, len=$len, token=$token."; + $newline = $line.' '.$token; + $newline =~ s, /,/,g; + $newline =~ s, \{,\{,g; + $newline =~ s, \},\},g; + $newline =~ s, \[,\[,g; + $newline =~ s, \],\],g; + $newline =~ s,\{ ,\{,g; + $newline =~ s,\} ,\},g; + $newline =~ s,\[ ,\[,g; + $newline =~ s,\] ,\],g; + if ( length( $newline ) > 76 ) { +# print "\nline=$line\n"; + $header = $header."\n\"".$line."\\n\""; + $newline = $token; + } + $line = $newline; +} +$header = $header."\n\"".$line."\\n\""; + + +print $header; diff --git a/src/kernel/q1xcompatibility.h b/src/kernel/q1xcompatibility.h new file mode 100644 index 0000000..c28f33d --- /dev/null +++ b/src/kernel/q1xcompatibility.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Various macros etc. to ease porting from Qt 1.x to 2.0. THIS FILE +** WILL CHANGE OR DISAPPEAR IN THE NEXT VERSION OF Qt. +** +** Created : 980824 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef Q1XCOMPATIBILITY_H +#define Q1XCOMPATIBILITY_H + +#error "Compatibility with Qt 1.x is no longer guaranteed. Please" +#error "update your code (for example using qt20fix script). We" +#error "apologize for any inconvenience." + +#endif // Q1XCOMPATIBILITY_H diff --git a/src/kernel/qabstractlayout.cpp b/src/kernel/qabstractlayout.cpp new file mode 100644 index 0000000..6921d49 --- /dev/null +++ b/src/kernel/qabstractlayout.cpp @@ -0,0 +1,1945 @@ +/**************************************************************************** +** +** Implementation of the abstract layout base class +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qlayout.h" + +#ifndef QT_NO_LAYOUT +#include "qapplication.h" +#include "qlayoutengine_p.h" +#include "qmenubar.h" +#include "qtoolbar.h" + +static int menuBarHeightForWidth( QMenuBar *menubar, int w ) +{ +#ifndef QT_NO_MENUBAR + if ( menubar && !menubar->isHidden() && !menubar->isTopLevel() ) + return menubar->heightForWidth( QMAX(w, menubar->minimumWidth()) ); + else +#endif + return 0; +} + +/*! + \class QLayoutItem + \ingroup appearance + \ingroup geomanagement + \brief The QLayoutItem class provides an abstract item that a + QLayout manipulates. + + This is used by custom layouts. + + Pure virtual functions are provided to return information about + the layout, including, sizeHint(), minimumSize(), maximumSize() + and expanding(). + + The layout's geometry can be set and retrieved with setGeometry() + and geometry(), and its alignment with setAlignment() and + alignment(). + + isEmpty() returns whether the layout is empty. iterator() returns + an iterator for the layout's children. If the concrete item is a + QWidget, it can be retrieved using widget(). Similarly for + layout() and spacerItem(). + + \sa QLayout +*/ + +/*! + \class QSpacerItem + \ingroup appearance + \ingroup geomanagement + \brief The QSpacerItem class provides blank space in a layout. + + This class is used by custom layouts. + + \sa QLayout QLayout::spacerItem() +*/ + +/*! + \class QWidgetItem + \ingroup appearance + \ingroup geomanagement + \brief The QWidgetItem class is a layout item that represents a widget. + + This is used by custom layouts. + + \sa QLayout QLayout::widget() +*/ + +/*! + \fn QLayoutItem::QLayoutItem( int alignment ) + + Constructs a layout item with an \a alignment that is a bitwise OR + of the \l{Qt::AlignmentFlags}. Not all subclasses support + alignment. +*/ + +/*! + \fn int QLayoutItem::alignment() const + + Returns the alignment of this item. +*/ + +/*! + Sets the alignment of this item to \a a, which is a bitwise OR of + the \l{Qt::AlignmentFlags}. Not all subclasses support alignment. +*/ +void QLayoutItem::setAlignment( int a ) +{ + align = a; +} + +/*! + \fn QSize QLayoutItem::maximumSize() const + + Implemented in subclasses to return the maximum size of this item. +*/ + +/*! + \fn QSize QLayoutItem::minimumSize() const + + Implemented in subclasses to return the minimum size of this item. +*/ + +/*! + \fn QSize QLayoutItem::sizeHint() const + + Implemented in subclasses to return the preferred size of this item. +*/ + +/*! + \fn QSizePolicy::ExpandData QLayoutItem::expanding() const + + Implemented in subclasses to return the direction(s) this item + "wants" to expand in (if any). +*/ + +/*! + \fn void QLayoutItem::setGeometry( const QRect &r ) + + Implemented in subclasses to set this item's geometry to \a r. +*/ + +/*! + \fn QRect QLayoutItem::geometry() const + + Returns the rectangle covered by this layout item. +*/ + +/*! + \fn virtual bool QLayoutItem::isEmpty() const + + Implemented in subclasses to return whether this item is empty, + i.e. whether it contains any widgets. +*/ + +/*! + \fn QSpacerItem::QSpacerItem( int w, int h, QSizePolicy::SizeType hData, QSizePolicy::SizeType vData ) + + Constructs a spacer item with preferred width \a w, preferred + height \a h, horizontal size policy \a hData and vertical size + policy \a vData. + + The default values provide a gap that is able to stretch if + nothing else wants the space. +*/ + +/*! + Changes this spacer item to have preferred width \a w, preferred + height \a h, horizontal size policy \a hData and vertical size + policy \a vData. + + The default values provide a gap that is able to stretch if + nothing else wants the space. +*/ +void QSpacerItem::changeSize( int w, int h, QSizePolicy::SizeType hData, + QSizePolicy::SizeType vData ) +{ + width = w; + height = h; + sizeP = QSizePolicy( hData, vData ); +} + +/*! + \fn QWidgetItem::QWidgetItem (QWidget * w) + + Creates an item containing widget \a w. +*/ + +/*! + Destroys the QLayoutItem. +*/ +QLayoutItem::~QLayoutItem() +{ +} + +/*! + Invalidates any cached information in this layout item. +*/ +void QLayoutItem::invalidate() +{ +} + +/*! + If this item is a QLayout, it is returned as a QLayout; otherwise + 0 is returned. This function provides type-safe casting. +*/ +QLayout * QLayoutItem::layout() +{ + return 0; +} + +/*! + If this item is a QSpacerItem, it is returned as a QSpacerItem; + otherwise 0 is returned. This function provides type-safe casting. +*/ +QSpacerItem * QLayoutItem::spacerItem() +{ + return 0; +} + +/*! + \reimp +*/ +QLayout * QLayout::layout() +{ + return this; +} + +/*! + \reimp +*/ +QSpacerItem * QSpacerItem::spacerItem() +{ + return this; +} + +/*! + If this item is a QWidget, it is returned as a QWidget; otherwise + 0 is returned. This function provides type-safe casting. +*/ +QWidget * QLayoutItem::widget() +{ + return 0; +} + +/*! + Returns the widget managed by this item. +*/ +QWidget * QWidgetItem::widget() +{ + return wid; +} + +/*! + Returns TRUE if this layout's preferred height depends on its + width; otherwise returns FALSE. The default implementation returns + FALSE. + + Reimplement this function in layout managers that support height + for width. + + \sa heightForWidth(), QWidget::heightForWidth() +*/ +bool QLayoutItem::hasHeightForWidth() const +{ + return FALSE; +} + +/*! + Returns an iterator over this item's QLayoutItem children. The + default implementation returns an empty iterator. + + Reimplement this function in subclasses that can have children. +*/ +QLayoutIterator QLayoutItem::iterator() +{ + return QLayoutIterator( 0 ); +} + +/*! + Returns the preferred height for this layout item, given the width + \a w. + + The default implementation returns -1, indicating that the + preferred height is independent of the width of the item. Using + the function hasHeightForWidth() will typically be much faster + than calling this function and testing for -1. + + Reimplement this function in layout managers that support height + for width. A typical implementation will look like this: + \code + int MyLayout::heightForWidth( int w ) const + { + if ( cache_dirty || cached_width != w ) { + // not all C++ compilers support "mutable" + MyLayout *that = (MyLayout*)this; + int h = calculateHeightForWidth( w ); + that->cached_hfw = h; + return h; + } + return cached_hfw; + } + \endcode + + Caching is strongly recommended; without it layout will take + exponential time. + + \sa hasHeightForWidth() +*/ +int QLayoutItem::heightForWidth( int /* w */ ) const +{ + return -1; +} + +/*! + Stores the spacer item's rect \a r so that it can be returned by + geometry(). +*/ +void QSpacerItem::setGeometry( const QRect &r ) +{ + rect = r; +} + +/*! + Sets the geometry of this item's widget to be contained within + rect \a r, taking alignment and maximum size into account. +*/ +void QWidgetItem::setGeometry( const QRect &r ) +{ + QSize s = r.size().boundedTo( qSmartMaxSize( this ) ); + int x = r.x(); + int y = r.y(); + if ( align & (Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask) ) { + QSize pref = wid->sizeHint().expandedTo( wid->minimumSize() ); //### + if ( align & Qt::AlignHorizontal_Mask ) + s.setWidth( QMIN( s.width(), pref.width() ) ); + if ( align & Qt::AlignVertical_Mask ) { + if ( hasHeightForWidth() ) + s.setHeight( QMIN( s.height(), heightForWidth(s.width()) ) ); + else + s.setHeight( QMIN( s.height(), pref.height() ) ); + } + } + int alignHoriz = QApplication::horizontalAlignment( align ); + if ( alignHoriz & Qt::AlignRight ) + x = x + ( r.width() - s.width() ); + else if ( !(alignHoriz & Qt::AlignLeft) ) + x = x + ( r.width() - s.width() ) / 2; + + if ( align & Qt::AlignBottom ) + y = y + ( r.height() - s.height() ); + else if ( !(align & Qt::AlignTop) ) + y = y + ( r.height() - s.height() ) / 2; + + if ( !isEmpty() ) + wid->setGeometry( x, y, s.width(), s.height() ); +} + +/*! + \reimp +*/ +QRect QSpacerItem::geometry() const +{ + return rect; +} + +/*! + \reimp +*/ +QRect QWidgetItem::geometry() const +{ + return wid->geometry(); +} + +/*! + \reimp +*/ +QRect QLayout::geometry() const +{ + return rect; +} + +/*! + \reimp +*/ +bool QWidgetItem::hasHeightForWidth() const +{ + if ( isEmpty() ) + return FALSE; + if ( wid->layout() ) + return wid->layout()->hasHeightForWidth(); + return wid->sizePolicy().hasHeightForWidth(); +} + +/*! + \reimp +*/ +int QWidgetItem::heightForWidth( int w ) const +{ + if ( isEmpty() ) + return -1; + int hfw; + if ( wid->layout() ) + hfw = wid->layout()->totalHeightForWidth( w ); + else + hfw = wid->heightForWidth( w ); + + if ( hfw > wid->maximumHeight() ) + hfw = wid->maximumHeight(); + if ( hfw < wid->minimumHeight() ) + hfw = wid->minimumHeight(); + if ( hfw < 1 ) + hfw = 1; + return hfw; +} + +/*! + Returns the direction in which this spacer item will expand. + + \sa QSizePolicy::ExpandData +*/ +QSizePolicy::ExpandData QSpacerItem::expanding() const +{ + return sizeP.expanding(); +} + +/*! + Returns whether this item's widget can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions and \c NoDirection means that + it doesn't want to grow at all. +*/ +QSizePolicy::ExpandData QWidgetItem::expanding() const +{ + if ( isEmpty() ) + return QSizePolicy::NoDirection; + + int e = wid->sizePolicy().expanding(); + /* + If the layout is expanding, we make the widget expanding, even if + its own size policy isn't expanding. This behavior should be + reconsidered in Qt 4.0. (###) + */ + if ( wid->layout() ) { + if ( wid->sizePolicy().mayGrowHorizontally() + && (wid->layout()->expanding() & QSizePolicy::Horizontally) ) + e |= QSizePolicy::Horizontally; + if ( wid->sizePolicy().mayGrowVertically() + && (wid->layout()->expanding() & QSizePolicy::Vertically) ) + e |= QSizePolicy::Vertically; + } + + if ( align & Qt::AlignHorizontal_Mask ) + e &= ~QSizePolicy::Horizontally; + if ( align & Qt::AlignVertical_Mask) + e &= ~QSizePolicy::Vertically; + return (QSizePolicy::ExpandData)e; +} + +/*! + Returns the minimum size of this spacer item. +*/ +QSize QSpacerItem::minimumSize() const +{ + return QSize( sizeP.mayShrinkHorizontally() ? 0 : width, + sizeP.mayShrinkVertically() ? 0 : height ); +} + +/*! + Returns the minimum size of this item. +*/ +QSize QWidgetItem::minimumSize() const +{ + if ( isEmpty() ) + return QSize( 0, 0 ); + return qSmartMinSize( this ); +} + +/*! + Returns the maximum size of this spacer item. +*/ +QSize QSpacerItem::maximumSize() const +{ + return QSize( sizeP.mayGrowHorizontally() ? QLAYOUTSIZE_MAX : width, + sizeP.mayGrowVertically() ? QLAYOUTSIZE_MAX : height ); +} + +/*! + Returns the maximum size of this item. +*/ +QSize QWidgetItem::maximumSize() const +{ + if ( isEmpty() ) { + return QSize( 0, 0 ); + } else { + return qSmartMaxSize( this, align ); + } +} + +/*! + Returns the preferred size of this spacer item. +*/ +QSize QSpacerItem::sizeHint() const +{ + return QSize( width, height ); +} + +/*! + Returns the preferred size of this item. +*/ +QSize QWidgetItem::sizeHint() const +{ + QSize s; + if ( isEmpty() ) { + s = QSize( 0, 0 ); + } else { + s = wid->sizeHint(); + if ( wid->sizePolicy().horData() == QSizePolicy::Ignored ) + s.setWidth( 1 ); + if ( wid->sizePolicy().verData() == QSizePolicy::Ignored ) + s.setHeight( 1 ); + s = s.boundedTo( wid->maximumSize() ) + .expandedTo( wid->minimumSize() ).expandedTo( QSize(1, 1) ); + } + return s; +} + +/*! + Returns TRUE because a spacer item never contains widgets. +*/ +bool QSpacerItem::isEmpty() const +{ + return TRUE; +} + +/*! + Returns TRUE if the widget has been hidden; otherwise returns + FALSE. +*/ +bool QWidgetItem::isEmpty() const +{ + return wid->isHidden() || wid->isTopLevel(); +} + +/*! + \class QLayout + \brief The QLayout class is the base class of geometry managers. + + \ingroup appearance + \ingroup geomanagement + + This is an abstract base class inherited by the concrete classes, + QBoxLayout and QGridLayout. + + For users of QLayout subclasses or of QMainWindow there is seldom + any need to use the basic functions provided by QLayout, such as + \l setResizeMode() or setMenuBar(). See the \link layout.html layout + overview page \endlink for more information. + + To make your own layout manager, subclass QGLayoutIterator and + implement the functions addItem(), sizeHint(), setGeometry(), and + iterator(). You should also implement minimumSize() to ensure your + layout isn't resized to zero size if there is too little space. To + support children whose heights depend on their widths, implement + hasHeightForWidth() and heightForWidth(). See the \link + customlayout.html custom layout page \endlink for an in-depth + description. + + Geometry management stops when the layout manager is deleted. +*/ + +/*! + Constructs a new top-level QLayout called \a name, with main + widget \a parent. \a parent may not be 0. + + The \a margin is the number of pixels between the edge of the + widget and the managed children. The \a spacing sets the value of + spacing(), which gives the spacing between the managed widgets. If + \a spacing is -1 (the default), spacing is set to the value of \a + margin. + + There can be only one top-level layout for a widget. It is + returned by QWidget::layout() +*/ +QLayout::QLayout( QWidget *parent, int margin, int spacing, const char *name ) + : QObject( parent, name ) +{ + init(); + if ( parent ) { + if ( parent->layout() ) { + qWarning( "QLayout \"%s\" added to %s \"%s\", which already has a" + " layout", QObject::name(), parent->className(), + parent->name() ); + parent->removeChild( this ); + } else { + topLevel = TRUE; + parent->installEventFilter( this ); + setWidgetLayout( parent, this ); + } + } + outsideBorder = margin; + if ( spacing < 0 ) + insideSpacing = margin; + else + insideSpacing = spacing; +} + +void QLayout::init() +{ + insideSpacing = 0; + outsideBorder = 0; + topLevel = FALSE; + enabled = TRUE; + autoNewChild = FALSE; + frozen = FALSE; + activated = FALSE; + marginImpl = FALSE; + autoMinimum = FALSE; + autoResizeMode = TRUE; + extraData = 0; +#ifndef QT_NO_MENUBAR + menubar = 0; +#endif +} + +/*! + Constructs a new child QLayout called \a name, and places it + inside \a parentLayout by using the default placement defined by + addItem(). + + If \a spacing is -1, this QLayout inherits \a parentLayout's + spacing(), otherwise the value of \a spacing is used. +*/ +QLayout::QLayout( QLayout *parentLayout, int spacing, const char *name ) + : QObject( parentLayout, name ) + +{ + init(); + insideSpacing = spacing < 0 ? parentLayout->insideSpacing : spacing; + parentLayout->addItem( this ); +} + +/*! + Constructs a new child QLayout called \a name. If \a spacing is + -1, this QLayout inherits its parent's spacing(); otherwise the + value of \a spacing is used. + + This layout has to be inserted into another layout before geometry + management will work. +*/ +QLayout::QLayout( int spacing, const char *name ) + : QObject( 0, name ) +{ + init(); + insideSpacing = spacing; +} + +/*! + \fn void QLayout::addItem( QLayoutItem *item ) + + Implemented in subclasses to add an \a item. How it is added is + specific to each subclass. + + The ownership of \a item is transferred to the layout, and it's + the layout's responsibility to delete it. +*/ + +/*! + \fn QLayoutIterator QLayout::iterator() + + Implemented in subclasses to return an iterator that iterates over + this layout's children. + + A typical implementation will be: + \code + QLayoutIterator MyLayout::iterator() + { + QGLayoutIterator *i = new MyLayoutIterator( internal_data ); + return QLayoutIterator( i ); + } + \endcode + where MyLayoutIterator is a subclass of QGLayoutIterator. +*/ + +/*! + \fn void QLayout::add( QWidget *w ) + + Adds widget \a w to this layout in a manner specific to the + layout. This function uses addItem(). +*/ + +/*! + \fn QMenuBar* QLayout::menuBar () const + + Returns the menu bar set for this layout, or 0 if no menu bar is + set. +*/ + +/*! + \fn bool QLayout::isTopLevel () const + + Returns TRUE if this layout is a top-level layout, i.e. not a + child of another layout; otherwise returns FALSE. +*/ + +/*! + \property QLayout::margin + \brief the width of the outside border of the layout + + For some layout classes this property has an effect only on + top-level layouts; QBoxLayout and QGridLayout support margins for + child layouts. The default value is 0. + + \sa spacing +*/ + +/*! + \property QLayout::spacing + \brief the spacing between widgets inside the layout + + The default value is -1, which signifies that the layout's spacing + should not override the widget's spacing. + + \sa margin +*/ +void QLayout::setMargin( int margin ) +{ + outsideBorder = margin; + invalidate(); + if ( mainWidget() ) { + QEvent *lh = new QEvent( QEvent::LayoutHint ); + QApplication::postEvent( mainWidget(), lh ); + } +} + +void QLayout::setSpacing( int spacing ) +{ + insideSpacing = spacing; + if ( spacing >= 0 ) + propagateSpacing( this ); + invalidate(); + if ( mainWidget() ) { + QEvent *lh = new QEvent( QEvent::LayoutHint ); + QApplication::postEvent( mainWidget(), lh ); + } +} + +/*! + Returns the main widget (parent widget) of this layout, or 0 if + this layout is a sub-layout that is not yet inserted. +*/ +QWidget *QLayout::mainWidget() +{ + if ( !topLevel ) { + if ( parent() ) { + QLayout *parentLayout = ::qt_cast<QLayout*>(parent()); + Q_ASSERT(parentLayout); + return parentLayout->mainWidget(); + } else { + return 0; + } + } else { + Q_ASSERT(parent() && parent()->isWidgetType()); + return (QWidget*)parent(); + } +} + +/*! + Returns TRUE if this layout is empty. The default implementation + returns FALSE. +*/ +bool QLayout::isEmpty() const +{ + return FALSE; //### should check +} + +/*! + Sets widget \a w's layout to layout \a l. +*/ +void QLayout::setWidgetLayout( QWidget *w, QLayout *l ) +{ + w->setLayout( l ); +} + +/*! + This function is reimplemented in subclasses to perform layout. + + The default implementation maintains the geometry() information + given by rect \a r. Reimplementors must call this function. +*/ +void QLayout::setGeometry( const QRect &r ) +{ + rect = r; +} + +/*! + Invalidates cached information. Reimplementations must call this. +*/ +void QLayout::invalidate() +{ + rect = QRect(); +} + +static bool removeWidgetRecursively( QLayoutItem *lay, QWidget *w ) +{ + bool didSomething = FALSE; + QLayoutIterator it = lay->iterator(); + QLayoutItem *child; + while ( (child = it.current()) != 0 ) { + if ( child->widget() == w ) { + it.deleteCurrent(); + lay->invalidate(); // maybe redundant + didSomething = TRUE; + } else if ( removeWidgetRecursively(child, w) ) { + lay->invalidate(); // maybe redundant + didSomething = TRUE; + } else { + ++it; + } + } + return didSomething; +} + +/*! + \reimp + Performs child widget layout when the parent widget is resized. + Also handles removal of widgets and child layouts. \a e is the + event the occurred on object \a o. +*/ +bool QLayout::eventFilter( QObject *o, QEvent *e ) +{ + if ( !enabled ) + return FALSE; + + if ( !o->isWidgetType() ) + return FALSE; + + switch ( e->type() ) { + case QEvent::Resize: + if ( activated ) { + QResizeEvent *r = (QResizeEvent *)e; + int mbh = 0; +#ifndef QT_NO_MENUBAR + mbh = menuBarHeightForWidth( menubar, r->size().width() ); +#endif + int b = marginImpl ? 0 : outsideBorder; + setGeometry( QRect( b, mbh + b, r->size().width() - 2 * b, + r->size().height() - mbh - 2 * b ) ); + } else { + activate(); + } + break; + case QEvent::ChildRemoved: + { + QChildEvent *c = (QChildEvent *)e; + if ( c->child()->isWidgetType() ) { + QWidget *w = (QWidget *)c->child(); +#ifndef QT_NO_MENUBAR + if ( w == menubar ) + menubar = 0; +#endif + if ( removeWidgetRecursively( this, w ) ) { + QEvent *lh = new QEvent( QEvent::LayoutHint ); + QApplication::postEvent( o, lh ); + } + } + } + break; + case QEvent::ChildInserted: + if ( topLevel && autoNewChild ) { + QChildEvent *c = (QChildEvent *)e; + if ( c->child()->isWidgetType() ) { + QWidget *w = (QWidget *)c->child(); + if ( !w->isTopLevel() ) { +#if !defined(QT_NO_MENUBAR) && !defined(QT_NO_TOOLBAR) + if ( ::qt_cast<QMenuBar*>(w) && !::qt_cast<QToolBar*>(w->parentWidget()) ) + menubar = (QMenuBar *)w; + else +#endif + addItem( new QWidgetItem( w ) ); + QEvent *lh = new QEvent( QEvent::LayoutHint ); + QApplication::postEvent( o, lh ); + } + } + } + break; + case QEvent::LayoutHint: + activate(); + break; + default: + break; + } + return QObject::eventFilter( o, e ); +} + +/*! + \reimp +*/ +void QLayout::childEvent( QChildEvent *e ) +{ + if ( !enabled ) + return; + + if ( e->type() == QEvent::ChildRemoved ) { + QChildEvent *c = (QChildEvent*)e; + QLayoutIterator it = iterator(); + QLayoutItem *item; + while ( (item = it.current() ) ) { + if ( item == (QLayout*)c->child() ) { + it.takeCurrent(); + invalidate(); + break; + } else { + ++it; + } + } + } +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +int QLayout::totalHeightForWidth( int w ) const +{ + if ( topLevel ) { + QWidget *mw = (QWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) { + mw->polish(); + } + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + int h = heightForWidth( w - b ) + b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, w ); +#endif + return h; +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +QSize QLayout::totalMinimumSize() const +{ + if ( topLevel ) { + QWidget *mw = (QWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) + mw->polish(); + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + + QSize s = minimumSize(); + int h = b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, s.width() ); +#endif + return s + QSize( b, h ); +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +QSize QLayout::totalSizeHint() const +{ + if ( topLevel ) { + QWidget *mw = (QWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) + mw->polish(); + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + + QSize s = sizeHint(); + if ( hasHeightForWidth() ) + s.setHeight( heightForWidth(s.width()) ); + int h = b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, s.width() ); +#endif + return s + QSize( b, h ); +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +QSize QLayout::totalMaximumSize() const +{ + if ( topLevel ) { + QWidget *mw = (QWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) { + mw->polish(); + } + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + + QSize s = maximumSize(); + int h = b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, s.width() ); +#endif + + if ( isTopLevel() ) + s = QSize( QMIN( s.width() + b, QLAYOUTSIZE_MAX ), + QMIN( s.height() + h, QLAYOUTSIZE_MAX ) ); + return s; +} + +/*! + \internal + Destroys the layout, deleting all child layouts. + Geometry management stops when a top-level layout is deleted. + + The layout classes will probably be fatally confused if you delete + a sublayout. +*/ +QLayout::~QLayout() +{ + /* + This function may be called during the QObject destructor, + when the parent no longer is a QWidget. + */ + if ( isTopLevel() && parent() && parent()->isWidgetType() && + ((QWidget*)parent())->layout() == this ) + setWidgetLayout( (QWidget*)parent(), 0 ); +} + +/*! + Removes and deletes all items in this layout. +*/ +void QLayout::deleteAllItems() +{ + QLayoutIterator it = iterator(); + QLayoutItem *l; + while ( (l = it.takeCurrent()) ) + delete l; +} + +/*! + This function is called from addLayout() functions in subclasses + to add layout \a l as a sub-layout. +*/ +void QLayout::addChildLayout( QLayout *l ) +{ + if ( l->parent() ) { +#if defined(QT_CHECK_NULL) + qWarning( "QLayout::addChildLayout: layout already has a parent" ); +#endif + return; + } + insertChild( l ); + if ( l->insideSpacing < 0 ) { + l->insideSpacing = insideSpacing; + propagateSpacing( l ); + } +} + +/*! \fn int QLayout::defaultBorder() const + + \internal +*/ + +/*! \fn void QLayout::freeze() + + \internal +*/ + +/*! + \internal + Fixes the size of the main widget and distributes the available + space to the child widgets. For widgets which should not be + resizable, but where a QLayout subclass is used to set up the initial + geometry. + + As a special case, freeze(0, 0) is equivalent to setResizeMode(Fixed). +*/ +void QLayout::freeze( int w, int h ) +{ + if ( w <= 0 || h <= 0 ) { + setResizeMode( Fixed ); + } else { + setResizeMode( FreeResize ); // layout will not change min/max size + mainWidget()->setFixedSize( w, h ); + } +} + +#ifndef QT_NO_MENUBAR + +/*! + Makes the geometry manager take account of the menu bar \a w. All + child widgets are placed below the bottom edge of the menu bar. + + A menu bar does its own geometry management: never do addWidget() + on a QMenuBar. +*/ +void QLayout::setMenuBar( QMenuBar *w ) +{ + menubar = w; +} + +#endif + +/*! + Returns the minimum size of this layout. This is the smallest size + that the layout can have while still respecting the + specifications. Does not include what's needed by margin() or + menuBar(). + + The default implementation allows unlimited resizing. +*/ +QSize QLayout::minimumSize() const +{ + return QSize( 0, 0 ); +} + +/*! + Returns the maximum size of this layout. This is the largest size + that the layout can have while still respecting the + specifications. Does not include what's needed by margin() or + menuBar(). + + The default implementation allows unlimited resizing. +*/ +QSize QLayout::maximumSize() const +{ + return QSize( QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX ); +} + +/*! + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. + + The default implementation returns \c BothDirections. +*/ +QSizePolicy::ExpandData QLayout::expanding() const +{ + return QSizePolicy::BothDirections; +} + +static void invalidateRecursive( QLayoutItem *lay ) +{ + lay->invalidate(); + QLayoutIterator it = lay->iterator(); + QLayoutItem *child; + while ( (child = it.current()) != 0 ) { + invalidateRecursive( child ); + ++it; + } +} + +/*! + Redoes the layout for mainWidget(). You should generally not need + to call this because it is automatically called at the most + appropriate times. + + However, if you set up a QLayout for a visible widget without + resizing that widget, you will need to call this function in order + to lay it out. + + \sa QWidget::updateGeometry() +*/ +bool QLayout::activate() +{ + invalidateRecursive( this ); + if ( !topLevel ) + return FALSE; + + QWidget *mw = mainWidget(); + if (!mw) { +#if defined( QT_CHECK_NULL ) + qWarning( "QLayout::activate: %s \"%s\" does not have a main widget", + QObject::className(), QObject::name() ); +#endif + return FALSE; + } + activated = TRUE; + QSize s = mw->size(); + QSize ms; + int mbh = 0; +#ifndef QT_NO_MENUBAR + mbh = menuBarHeightForWidth( menubar, s.width() ); +#endif + int b = marginImpl ? 0 : outsideBorder; + setGeometry(QRect(b, mbh + b, s.width() - 2 * b, s.height() - mbh - 2 * b)); + if ( frozen ) { + // will trigger resize + mw->setFixedSize( totalSizeHint() ); + } else if ( autoMinimum ) { + ms = totalMinimumSize(); + } else if ( autoResizeMode && topLevel && mw->isTopLevel() ) { + ms = totalMinimumSize(); + if ( hasHeightForWidth() ) { + int h; + int mmbh = menuBarHeightForWidth( menubar, ms.width() ); + // ### 4.0: remove this 'if' when minimumHeightForWidth() is virtual + if ( inherits("QBoxLayout") ) { + h = ((QBoxLayout *) this)->minimumHeightForWidth( ms.width() ); + } else if ( inherits("QGridLayout") ) { + h = ((QGridLayout *) this)->minimumHeightForWidth( ms.width() ); + } else { + h = heightForWidth( ms.width() ); + } + if ( h + mmbh > ms.height() ) +#if 1 + //old behaviour: + ms = QSize( 0, 0 ); +#else + //better, but too big a change for a patch release in a stable branch: + ms.setHeight( 0 ); +#endif + } + } + + if (ms.isValid()) + mw->setMinimumSize( ms ); + + // ideally only if sizeHint() or sizePolicy() has changed + mw->updateGeometry(); + return TRUE; +} + +/*! + \class QSizePolicy + \brief The QSizePolicy class is a layout attribute describing horizontal + and vertical resizing policy. + + \ingroup appearance + \ingroup geomanagement + + The size policy of a widget is an expression of its willingness to + be resized in various ways. + + Widgets that reimplement QWidget::sizePolicy() return a QSizePolicy + that describes the horizontal and vertical resizing policy they + prefer when being laid out. Only \link #interesting one of the + constructors\endlink is of interest in most applications. + + QSizePolicy contains two independent SizeType objects; one describes + the widgets's horizontal size policy, and the other describes its + vertical size policy. It also contains a flag to indicate whether the + height and width of its preferred size are related. + + The horizontal and vertical \l{SizeType}s are set in the usual constructor + and can be queried using a variety of functions. + + The hasHeightForWidth() flag indicates whether the widget's sizeHint() + is width-dependent (such as a word-wrapping label) or not. + + \sa QSizePolicy::SizeType +*/ + +/*! + \enum QSizePolicy::SizeType + + The per-dimension sizing types used when constructing a + QSizePolicy are: + + \value Fixed The QWidget::sizeHint() is the only acceptable + alternative, so the widget can never grow or shrink (e.g. the + vertical direction of a push button). + + \value Minimum The sizeHint() is minimal, and sufficient. The + widget can be expanded, but there is no advantage to it being + larger (e.g. the horizontal direction of a push button). + It cannot be smaller than the size provided by sizeHint(). + + \value Maximum The sizeHint() is a maximum. The widget can be + shrunk any amount without detriment if other widgets need the + space (e.g. a separator line). + It cannot be larger than the size provided by sizeHint(). + + \value Preferred The sizeHint() is best, but the widget can be + shrunk and still be useful. The widget can be expanded, but there + is no advantage to it being larger than sizeHint() (the default + QWidget policy). + + \value Expanding The sizeHint() is a sensible size, but the + widget can be shrunk and still be useful. The widget can make use + of extra space, so it should get as much space as possible (e.g. + the horizontal direction of a slider). + + \value MinimumExpanding The sizeHint() is minimal, and sufficient. + The widget can make use of extra space, so it should get as much + space as possible (e.g. the horizontal direction of a slider). + + \value Ignored the sizeHint() is ignored. The widget will get as + much space as possible. +*/ + +/*! + \enum QSizePolicy::ExpandData + + This enum type describes in which directions a widget can make use + of extra space. There are four possible values: + + \value NoDirection the widget cannot make use of extra space in + any direction. + + \value Horizontally the widget can usefully be wider than the + sizeHint(). + + \value Vertically the widget can usefully be taller than the + sizeHint(). + + \value BothDirections the widget can usefully be both wider and + taller than the sizeHint(). +*/ + +/*! + \fn QSizePolicy::QSizePolicy() + + Constructs a minimally initialized QSizePolicy. +*/ + +/*! + \fn QSizePolicy::QSizePolicy( SizeType hor, SizeType ver, bool hfw ) + + \target interesting + This is the constructor normally used to return a value in the + overridden \l QWidget::sizePolicy() function of a QWidget + subclass. + + It constructs a QSizePolicy with independent horizontal and + vertical sizing types, \a hor and \a ver respectively. These \link + QSizePolicy::SizeType sizing types\endlink affect how the widget + is treated by the \link QLayout layout engine\endlink. + + If \a hfw is TRUE, the preferred height of the widget is dependent + on the width of the widget (for example, a QLabel with line + wrapping). + + \sa horData() verData() hasHeightForWidth() +*/ + +/*! + \fn QSizePolicy::QSizePolicy( SizeType hor, SizeType ver, uchar horStretch, uchar verStretch, bool hfw ) + + Constructs a QSizePolicy with independent horizontal and vertical + sizing types \a hor and \a ver, and stretch factors \a horStretch + and \a verStretch. + + If \a hfw is TRUE, the preferred height of the widget is dependent on the + width of the widget. + + \sa horStretch() verStretch() +*/ + +/*! + \fn QSizePolicy::SizeType QSizePolicy::horData() const + + Returns the horizontal component of the size policy. + + \sa setHorData() verData() horStretch() +*/ + +/*! + \fn QSizePolicy::SizeType QSizePolicy::verData() const + + Returns the vertical component of the size policy. + + \sa setVerData() horData() verStretch() +*/ + +/*! + \fn bool QSizePolicy::mayShrinkHorizontally() const + + Returns TRUE if the widget can sensibly be narrower than its + sizeHint(); otherwise returns FALSE. + + \sa mayShrinkVertically() mayGrowHorizontally() +*/ + +/*! + \fn bool QSizePolicy::mayShrinkVertically() const + + Returns TRUE if the widget can sensibly be shorter than its + sizeHint(); otherwise returns FALSE. + + \sa mayShrinkHorizontally() mayGrowVertically() +*/ + +/*! + \fn bool QSizePolicy::mayGrowHorizontally() const + + Returns TRUE if the widget can sensibly be wider than its + sizeHint(); otherwise returns FALSE. + + \sa mayGrowVertically() mayShrinkHorizontally() +*/ + +/*! + \fn bool QSizePolicy::mayGrowVertically() const + + Returns TRUE if the widget can sensibly be taller than its + sizeHint(); otherwise returns FALSE. + + \sa mayGrowHorizontally() mayShrinkVertically() +*/ + +/*! + \fn QSizePolicy::ExpandData QSizePolicy::expanding() const + + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. + + \sa mayShrinkHorizontally() mayGrowHorizontally() + mayShrinkVertically() mayGrowVertically() +*/ + +/*! + \fn void QSizePolicy::setHorData( SizeType d ) + + Sets the horizontal component of the size policy to size type \a + d. + + \sa horData() setVerData() +*/ + +/*! + \fn void QSizePolicy::setVerData( SizeType d ) + + Sets the vertical component of the size policy to size type \a d. + + \sa verData() setHorData() +*/ + +/*! + \fn bool QSizePolicy::hasHeightForWidth() const + + Returns TRUE if the widget's preferred height depends on its + width; otherwise returns FALSE. + + \sa setHeightForWidth() +*/ + +/*! + \fn void QSizePolicy::setHeightForWidth( bool b ) + + Sets the hasHeightForWidth() flag to \a b. + + \sa hasHeightForWidth() +*/ + +/*! + \fn uint QSizePolicy::horStretch() const + + Returns the horizontal stretch factor of the size policy. + + \sa setHorStretch() verStretch() +*/ + +/*! + \fn uint QSizePolicy::verStretch() const + + Returns the vertical stretch factor of the size policy. + + \sa setVerStretch() horStretch() +*/ + +/*! + \fn void QSizePolicy::setHorStretch( uchar sf ) + + Sets the horizontal stretch factor of the size policy to \a sf. + + \sa horStretch() setVerStretch() +*/ + +/*! + \fn void QSizePolicy::setVerStretch( uchar sf ) + + Sets the vertical stretch factor of the size policy to \a sf. + + \sa verStretch() setHorStretch() +*/ + +/*! + \fn void QSizePolicy::transpose() + + Swaps the horizontal and vertical policies and stretches. +*/ + + +/*! + \fn bool QSizePolicy::operator==( const QSizePolicy &s ) const + + Returns TRUE if this policy is equal to \a s; otherwise returns + FALSE. + + \sa operator!=() +*/ + +/*! + \fn bool QSizePolicy::operator!=( const QSizePolicy &s ) const + + Returns TRUE if this policy is different from \a s; otherwise + returns FALSE. + + \sa operator==() +*/ + +/*! + \class QGLayoutIterator + \brief The QGLayoutIterator class is an abstract base class of internal layout iterators. + + \ingroup appearance + \ingroup geomanagement + + (This class is \e not OpenGL related, it just happens to start with + the letters QGL...) + + Subclass this class to create a custom layout. The functions that + must be implemented are next(), current(), and takeCurrent(). + + The QGLayoutIterator implements the functionality of + QLayoutIterator. Each subclass of QLayout needs a + QGLayoutIterator subclass. +*/ + +/*! + \fn QLayoutItem *QGLayoutIterator::next() + + Implemented in subclasses to move the iterator to the next item + and return that item, or 0 if there is no next item. +*/ + +/*! + \fn QLayoutItem *QGLayoutIterator::current() + + Implemented in subclasses to return the current item, or 0 if + there is no current item. +*/ + +/*! + \fn QLayoutItem *QGLayoutIterator::takeCurrent() + + Implemented in subclasses. The function must remove the current + item from the layout without deleting it, move the iterator to the + next item and return the removed item, or 0 if no item was + removed. +*/ + +/*! + Destroys the iterator +*/ +QGLayoutIterator::~QGLayoutIterator() +{ +} + +/*! + \class QLayoutIterator + \brief The QLayoutIterator class provides iterators over QLayoutItem. + + \ingroup appearance + \ingroup geomanagement + + Use QLayoutItem::iterator() to create an iterator over a layout. + + QLayoutIterator uses \e explicit sharing with a reference count. + If an iterator is copied and one of the copies is modified, both + iterators will be modified. + + A QLayoutIterator is not protected against changes in its layout. If + the layout is modified or deleted the iterator will become invalid. + It is not possible to test for validity. It is safe to delete an + invalid layout; any other access may lead to an illegal memory + reference and the abnormal termination of the program. + + Calling takeCurrent() or deleteCurrent() leaves the iterator in a + valid state, but may invalidate any other iterators that access the + same layout. + + The following code will draw a rectangle for each layout item in + the layout structure of the widget. + \code + static void paintLayout( QPainter *p, QLayoutItem *lay ) + { + QLayoutIterator it = lay->iterator(); + QLayoutItem *child; + while ( (child = it.current()) != 0 ) { + paintLayout( p, child ); + ++it; + } + p->drawRect( lay->geometry() ); + } + void ExampleWidget::paintEvent( QPaintEvent * ) + { + QPainter p( this ); + if ( layout() ) + paintLayout( &p, layout() ); + } + \endcode + + All the functionality of QLayoutIterator is implemented by + subclasses of \l QGLayoutIterator. QLayoutIterator itself is not + designed to be subclassed. +*/ + +/*! + \fn QLayoutIterator::QLayoutIterator( QGLayoutIterator *gi ) + + Constructs an iterator based on \a gi. The constructed iterator + takes ownership of \a gi and will delete it. + + This constructor is provided for layout implementors. Application + programmers should use QLayoutItem::iterator() to create an + iterator over a layout. +*/ + +/*! + \fn QLayoutIterator::QLayoutIterator( const QLayoutIterator &i ) + + Creates a shallow copy of \a i, i.e. if the copy is modified, then + the original will also be modified. +*/ + +/*! + \fn QLayoutIterator::~QLayoutIterator() + + Destroys the iterator. +*/ + +/*! + \fn QLayoutIterator &QLayoutIterator::operator=( const QLayoutIterator &i ) + + Assigns \a i to this iterator and returns a reference to this + iterator. +*/ + +/*! + \fn QLayoutItem *QLayoutIterator::operator++() + + Moves the iterator to the next child item and returns that item, + or 0 if there is no such item. +*/ + +/*! + \fn QLayoutItem *QLayoutIterator::current() + + Returns the current item, or 0 if there is no current item. +*/ + +/*! + \fn QLayoutItem *QLayoutIterator::takeCurrent() + + Removes the current child item from the layout without deleting + it, and moves the iterator to the next item. Returns the removed + item, or 0 if there was no item to be removed. This iterator will + still be valid, but any other iterator over the same layout may + become invalid. +*/ + +/*! + \fn void QLayoutIterator::deleteCurrent() + + Removes and deletes the current child item from the layout and + moves the iterator to the next item. This iterator will still be + valid, but any other iterator over the same layout may become + invalid. +*/ + +/*! + \enum QLayout::ResizeMode + + The possible values are: + + \value Auto If the main widget is a top-level widget with no + height-for-width (hasHeightForWidth()), this is + the same as \c Minimium; otherwise, this is the + same as \c FreeResize. + \value Fixed The main widget's size is set to sizeHint(); it + cannot be resized at all. + \value Minimum The main widget's minimum size is set to + minimumSize(); it cannot be smaller. + \value FreeResize The widget is not constrained. +*/ + +/*! + \property QLayout::resizeMode + \brief the resize mode of the layout + + The default mode is \c Auto. + + \sa QLayout::ResizeMode +*/ + +void QLayout::setResizeMode( ResizeMode mode ) +{ + if ( mode == resizeMode() ) + return; + + switch ( mode ) { + case Auto: + frozen = FALSE; + autoMinimum = FALSE; + autoResizeMode = TRUE; + break; + case Fixed: + frozen = TRUE; + autoMinimum = FALSE; + autoResizeMode = FALSE; + break; + case FreeResize: + frozen = FALSE; + autoMinimum = FALSE; + autoResizeMode = FALSE; + break; + case Minimum: + frozen = FALSE; + autoMinimum = TRUE; + autoResizeMode = FALSE; + } + if ( mainWidget() && mainWidget()->isVisible() ) + activate(); +} + +QLayout::ResizeMode QLayout::resizeMode() const +{ + return ( autoResizeMode ? Auto : + (frozen ? Fixed : (autoMinimum ? Minimum : FreeResize)) ); +} + +/*! + \fn bool QLayout::autoAdd() const + + Returns TRUE if this layout automatically grabs all new + mainWidget()'s new children and adds them as defined by addItem(); + otherwise returns FALSE. This has effect only for top-level + layouts, i.e. layouts that are direct children of their + mainWidget(). + + autoAdd() is disabled by default. + + Note that a top-level layout is not necessarily associated with + the top-level widget. + + \sa setAutoAdd() +*/ + +/*! + If \a b is TRUE, auto-add is enabled; otherwise auto-add is + disabled. + + \warning If auto-add is enabled, you cannot set stretch factors + on the child widgets until the widgets are actually inserted in + the layout (after control returned to the event loop). We + therefore recommend that you avoid the auto-add feature in new + programs. + + \sa autoAdd() +*/ +void QLayout::setAutoAdd( bool b ) +{ + autoNewChild = b; +} + +/*! + \fn bool QLayout::supportsMargin() const + + Returns TRUE if this layout supports \l QLayout::margin on + non-top-level layouts; otherwise returns FALSE. + + \sa margin +*/ + +/*! + Sets the value returned by supportsMargin(). If \a b is TRUE, + margin() handling is implemented by the subclass. If \a b is + FALSE (the default), QLayout will add margin() around top-level + layouts. + + If \a b is TRUE, margin handling needs to be implemented in + setGeometry(), maximumSize(), minimumSize(), sizeHint() and + heightForWidth(). + + \sa supportsMargin() +*/ +void QLayout::setSupportsMargin( bool b ) +{ + marginImpl = b; +} + +/*! + Returns the rectangle that should be covered when the geometry of + this layout is set to \a r, provided that this layout supports + setAlignment(). + + The result is derived from sizeHint() and expanding(). It is never + larger than \a r. +*/ +QRect QLayout::alignmentRect( const QRect &r ) const +{ + QSize s = sizeHint(); + int a = alignment(); + + /* + This is a hack to obtain the real maximum size, not + QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX), the value consistently + returned by QLayoutItems that have an alignment. + */ + QLayout *that = (QLayout *) this; + that->setAlignment( 0 ); + QSize ms = maximumSize(); + that->setAlignment( a ); + + if ( (expanding() & QSizePolicy::Horizontally) || + !(a & Qt::AlignHorizontal_Mask ) ) { + s.setWidth( QMIN(r.width(), ms.width()) ); + } + if ( (expanding() & QSizePolicy::Vertically) || + !(a & Qt::AlignVertical_Mask) ) { + s.setHeight( QMIN(r.height(), ms.height()) ); + } else if ( hasHeightForWidth() ) { + int hfw = heightForWidth( s.width() ); + if ( hfw < s.height() ) + s.setHeight( QMIN(hfw, ms.height()) ); + } + + int x = r.x(); + int y = r.y(); + + if ( a & Qt::AlignBottom ) + y += ( r.height() - s.height() ); + else if ( !(a & Qt::AlignTop) ) + y += ( r.height() - s.height() ) / 2; + + a = QApplication::horizontalAlignment( a ); + if ( a & Qt::AlignRight ) + x += ( r.width() - s.width() ); + else if ( !(a & Qt::AlignLeft) ) + x += ( r.width() - s.width() ) / 2; + + return QRect( x, y, s.width(), s.height() ); +} + +/*! + Removes the widget \a widget from the layout. After this call, it + is the caller's responsibility to give the widget a reasonable + geometry or to put the widget back into a layout. + + \sa removeItem(), QWidget::setGeometry(), add() +*/ +void QLayout::remove( QWidget *widget ) +{ + QLayoutIterator it = iterator(); + QLayoutItem *child; + while ( (child = it.current()) != 0 ) { + if ( child->widget() == widget ) { + it.deleteCurrent(); + invalidate(); // maybe redundant + QApplication::postEvent( mainWidget(), + new QEvent(QEvent::LayoutHint) ); + } else { + ++it; + } + } +} + +/*! + Removes the layout item \a item from the layout. It is the + caller's responsibility to delete the item. + + Notice that \a item can be a layout (since QLayout inherits + QLayoutItem). + + \sa remove(), addItem() +*/ +void QLayout::removeItem( QLayoutItem *item ) +{ + QLayoutIterator it = iterator(); + QLayoutItem *child; + while ( (child = it.current()) != 0 ) { + if ( child == item ) { + it.takeCurrent(); + invalidate(); // maybe redundant + QApplication::postEvent( mainWidget(), + new QEvent(QEvent::LayoutHint) ); + } else { + ++it; + } + } +} + +/*! + Enables this layout if \a enable is TRUE, otherwise disables it. + + An enabled layout adjusts dynamically to changes; a disabled + layout acts as if it did not exist. + + By default all layouts are enabled. + + \sa isEnabled() +*/ +void QLayout::setEnabled( bool enable ) +{ + enabled = enable; +} + +/*! + Returns TRUE if the layout is enabled; otherwise returns FALSE. + + \sa setEnabled() +*/ +bool QLayout::isEnabled() const +{ + return enabled; +} + +void QLayout::propagateSpacing( QLayout *parent ) +{ + QLayoutIterator it = parent->iterator(); + QLayoutItem *child; + while ( (child = it.current()) ) { + QLayout *childLayout = child->layout(); + if ( childLayout && childLayout->insideSpacing < 0 ) { + childLayout->insideSpacing = parent->insideSpacing; + propagateSpacing( childLayout ); + } + ++it; + } +} + +#endif // QT_NO_LAYOUT diff --git a/src/kernel/qabstractlayout.h b/src/kernel/qabstractlayout.h new file mode 100644 index 0000000..a40a94e --- /dev/null +++ b/src/kernel/qabstractlayout.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Definition of layout classes +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QABSTRACTLAYOUT_H +#define QABSTRACTLAYOUT_H + +/* + This header is provided for source compatibility only. +*/ + +#ifndef QT_H +#ifndef QT_NO_COMPAT +#include "qlayout.h" +#endif +#endif // QT_H + +#endif diff --git a/src/kernel/qaccel.cpp b/src/kernel/qaccel.cpp new file mode 100644 index 0000000..53814dc --- /dev/null +++ b/src/kernel/qaccel.cpp @@ -0,0 +1,1089 @@ +/**************************************************************************** +** +** Implementation of QAccel class +** +** Created : 950419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qaccel.h" + +#ifndef QT_NO_ACCEL + +#include "qsignal.h" +#include "qapplication.h" +#include "qwidget.h" +#include "qptrlist.h" +#include "qwhatsthis.h" +#include "qguardedptr.h" +#include "qstatusbar.h" +#include "qdockwindow.h" +#include "qsignalslotimp.h" +/*! + \class QAccel qaccel.h + \brief The QAccel class handles keyboard accelerator and shortcut keys. + + \ingroup misc + + A keyboard accelerator triggers an action when a certain key + combination is pressed. The accelerator handles all keyboard + activity for all the children of one top-level widget, so it is + not affected by the keyboard focus. + + In most cases, you will not need to use this class directly. Use + the QAction class to create actions with accelerators that can be + used in both menus and toolbars. If you're only interested in + menus use QMenuData::insertItem() or QMenuData::setAccel() to make + accelerators for operations that are also available on menus. Many + widgets automatically generate accelerators, such as QButton, + QGroupBox, QLabel (with QLabel::setBuddy()), QMenuBar and QTabBar. + Example: + \code + QPushButton p( "&Exit", parent ); // automatic shortcut ALT+Key_E + QPopupMenu *fileMenu = new fileMenu( parent ); + fileMenu->insertItem( "Undo", parent, SLOT(undo()), CTRL+Key_Z ); + \endcode + + A QAccel contains a list of accelerator items that can be + manipulated using insertItem(), removeItem(), clear(), key() and + findKey(). + + Each accelerator item consists of an identifier and a \l + QKeySequence. A single key sequence consists of a keyboard code + combined with modifiers (\c SHIFT, \c CTRL, \c ALT or \c + UNICODE_ACCEL). For example, \c{CTRL + Key_P} could be a shortcut + for printing a document. The key codes are listed in \c + qnamespace.h. As an alternative, use \c UNICODE_ACCEL with the + unicode code point of the character. For example, \c{UNICODE_ACCEL + + 'A'} gives the same accelerator as \c Key_A. + + When an accelerator key is pressed, the accelerator sends out the + signal activated() with a number that identifies this particular + accelerator item. Accelerator items can also be individually + connected, so that two different keys will activate two different + slots (see connectItem() and disconnectItem()). + + The activated() signal is \e not emitted when two or more + accelerators match the same key. Instead, the first matching + accelerator sends out the activatedAmbiguously() signal. By + pressing the key multiple times, users can navigate between all + matching accelerators. Some standard controls like QPushButton and + QCheckBox connect the activatedAmbiguously() signal to the + harmless setFocus() slot, whereas activated() is connected to a + slot invoking the button's action. Most controls, like QLabel and + QTabBar, treat activated() and activatedAmbiguously() as + equivalent. + + Use setEnabled() to enable or disable all the items in an + accelerator, or setItemEnabled() to enable or disable individual + items. An item is active only when both the QAccel and the item + itself are enabled. + + The function setWhatsThis() specifies a help text that appears + when the user presses an accelerator key in What's This mode. + + The accelerator will be deleted when \e parent is deleted, + and will consume relevant key events until then. + + Please note that the accelerator + \code + accelerator->insertItem( QKeySequence("M") ); + \endcode + can be triggered with both the 'M' key, and with Shift+M, + unless a second accelerator is defined for the Shift+M + combination. + + + Example: + \code + QAccel *a = new QAccel( myWindow ); // create accels for myWindow + a->connectItem( a->insertItem(Key_P+CTRL), // adds Ctrl+P accelerator + myWindow, // connected to myWindow's + SLOT(printDoc()) ); // printDoc() slot + \endcode + + \sa QKeyEvent QWidget::keyPressEvent() QMenuData::setAccel() + QButton::setAccel() QLabel::setBuddy() QKeySequence + \link guibooks.html#fowler GUI Design Handbook: Keyboard Shortcuts \endlink. +*/ + + +struct QAccelItem { // internal accelerator item + QAccelItem( const QKeySequence &k, int i ) + { key=k; id=i; enabled=TRUE; signal=0; } + ~QAccelItem() { delete signal; } + int id; + QKeySequence key; + bool enabled; + QSignal *signal; + QString whatsthis; +}; + + +typedef QPtrList<QAccelItem> QAccelList; // internal accelerator list + +class QAccelPrivate : public Qt { +public: + QAccelPrivate( QAccel* p ); + ~QAccelPrivate(); + QAccelList aitems; + bool enabled; + QGuardedPtr<QWidget> watch; + bool ignorewhatsthis; + QAccel* parent; + + void activate( QAccelItem* item ); + void activateAmbiguously( QAccelItem* item ); +}; + +class QAccelManager : public Qt { +public: + static QAccelManager* self() { return self_ptr ? self_ptr : new QAccelManager; } + void registerAccel( QAccelPrivate* a ) { accels.append( a ); } + void unregisterAccel( QAccelPrivate* a ) { accels.removeRef( a ); if ( accels.isEmpty() ) delete this; } + bool tryAccelEvent( QWidget* w, QKeyEvent* e ); + bool dispatchAccelEvent( QWidget* w, QKeyEvent* e ); + bool tryComposeUnicode( QWidget* w, QKeyEvent* e ); + +private: + QAccelManager():currentState(Qt::NoMatch), clash(-1) { self_ptr = this; } + ~QAccelManager() { self_ptr = 0; } + + bool correctSubWindow( QWidget *w, QAccelPrivate* d ); + SequenceMatch match( QKeyEvent* e, QAccelItem* item, QKeySequence& temp ); + int translateModifiers( ButtonState state ); + + QPtrList<QAccelPrivate> accels; + static QAccelManager* self_ptr; + Qt::SequenceMatch currentState; + QKeySequence intermediate; + int clash; +}; +QAccelManager* QAccelManager::self_ptr = 0; + +bool Q_EXPORT qt_tryAccelEvent( QWidget* w, QKeyEvent* e){ + return QAccelManager::self()->tryAccelEvent( w, e ); +} + +bool Q_EXPORT qt_dispatchAccelEvent( QWidget* w, QKeyEvent* e){ + return QAccelManager::self()->dispatchAccelEvent( w, e ); +} + +bool Q_EXPORT qt_tryComposeUnicode( QWidget* w, QKeyEvent* e){ + return QAccelManager::self()->tryComposeUnicode( w, e ); +} + +#ifdef Q_WS_MAC +static bool qt_accel_no_shortcuts = TRUE; +#else +static bool qt_accel_no_shortcuts = FALSE; +#endif +void Q_EXPORT qt_setAccelAutoShortcuts(bool b) { qt_accel_no_shortcuts = b; } + +/* + \internal + Returns TRUE if the accel is in the current subwindow, else FALSE. +*/ +bool QAccelManager::correctSubWindow( QWidget* w, QAccelPrivate* d ) { +#if !defined ( Q_OS_MACX ) + if ( !d->watch || !d->watch->isVisible() || !d->watch->isEnabled() ) +#else + if ( !d->watch || (!d->watch->isVisible() && !d->watch->inherits( "QMenuBar" )) || !d->watch->isEnabled() ) +#endif + return FALSE; + QWidget* tlw = w->topLevelWidget(); + QWidget* wtlw = d->watch->topLevelWidget(); + + /* if we live in a floating dock window, keep our parent's + * accelerators working */ +#ifndef QT_NO_MAINWINDOW + if ( tlw->isDialog() && tlw->parentWidget() && ::qt_cast<QDockWindow*>(tlw) ) + return tlw->parentWidget()->topLevelWidget() == wtlw; + + if ( wtlw != tlw ) + return FALSE; +#endif + /* if we live in a MDI subwindow, ignore the event if we are + not the active document window */ + QWidget* sw = d->watch; + while ( sw && !sw->testWFlags( WSubWindow ) ) + sw = sw->parentWidget( TRUE ); + if ( sw ) { // we are in a subwindow indeed + QWidget* fw = w; + while ( fw && fw != sw ) + fw = fw->parentWidget( TRUE ); + if ( fw != sw ) // focus widget not in our subwindow + return FALSE; + } + return TRUE; +} + +inline int QAccelManager::translateModifiers( ButtonState state ) +{ + int result = 0; + if ( state & ShiftButton ) + result |= SHIFT; + if ( state & ControlButton ) + result |= CTRL; + if ( state & MetaButton ) + result |= META; + if ( state & AltButton ) + result |= ALT; + return result; +} + +/* + \internal + Matches the current intermediate key sequence + the latest + keyevent, with and AccelItem. Returns Identical, + PartialMatch or NoMatch, and fills \a temp with the + resulting key sequence. +*/ +Qt::SequenceMatch QAccelManager::match( QKeyEvent *e, QAccelItem* item, QKeySequence& temp ) +{ + SequenceMatch result = Qt::NoMatch; + int index = intermediate.count(); + temp = intermediate; + + int modifier = translateModifiers( e->state() ); + + if ( e->key() && e->key() != Key_unknown) { + int key = e->key() | modifier; + if ( e->key() == Key_BackTab ) { + /* + In QApplication, we map shift+tab to shift+backtab. + This code here reverts the mapping in a way that keeps + backtab and shift+tab accelerators working, in that + order, meaning backtab has priority.*/ + key &= ~SHIFT; + + temp.setKey( key, index ); + if ( Qt::NoMatch != (result = temp.matches( item->key )) ) + return result; + if ( e->state() & ShiftButton ) + key |= SHIFT; + key = Key_Tab | ( key & MODIFIER_MASK ); + temp.setKey( key, index ); + if ( Qt::NoMatch != (result = temp.matches( item->key )) ) + return result; + } else { + temp.setKey( key, index ); + if ( Qt::NoMatch != (result = temp.matches( item->key )) ) + return result; + } + + if ( key == Key_BackTab ) { + if ( e->state() & ShiftButton ) + key |= SHIFT; + temp.setKey( key, index ); + if ( Qt::NoMatch != (result = temp.matches( item->key )) ) + return result; + } + } + if ( !e->text().isEmpty() ) { + temp.setKey( (int)e->text()[0].unicode() | UNICODE_ACCEL | modifier, index ); + result = temp.matches( item->key ); + } + return result; +} + +bool QAccelManager::tryAccelEvent( QWidget* w, QKeyEvent* e ) +{ + if ( Qt::NoMatch == currentState ) { + e->t = QEvent::AccelOverride; + e->ignore(); + QApplication::sendSpontaneousEvent( w, e ); + if ( e->isAccepted() ) + return FALSE; + } + e->t = QEvent::Accel; + e->ignore(); + QApplication::sendSpontaneousEvent( w, e ); + return e->isAccepted(); +} + +bool QAccelManager::tryComposeUnicode( QWidget* w, QKeyEvent* e ) +{ + if ( QApplication::metaComposeUnicode ) { + int value = e->key() - Key_0; + // Ignore acceloverrides so we don't trigger + // accels on keypad when Meta compose is on + if ( (e->type() == QEvent::AccelOverride) && + (e->state() == Qt::Keypad + Qt::MetaButton) ) { + e->accept(); + // Meta compose start/continue + } else if ( (e->type() == QEvent::KeyPress) && + (e->state() == Qt::Keypad + Qt::MetaButton) ) { + if ( value >= 0 && value <= 9 ) { + QApplication::composedUnicode *= 10; + QApplication::composedUnicode += value; + return TRUE; + } else { + // Composing interrupted, dispatch! + if ( QApplication::composedUnicode ) { + QChar ch( QApplication::composedUnicode ); + QString s( ch ); + QKeyEvent kep( QEvent::KeyPress, 0, ch.row() ? 0 : ch.cell(), 0, s ); + QKeyEvent ker( QEvent::KeyRelease, 0, ch.row() ? 0 : ch.cell(), 0, s ); + QApplication::sendEvent( w, &kep ); + QApplication::sendEvent( w, &ker ); + } + QApplication::composedUnicode = 0; + return TRUE; + } + // Meta compose end, dispatch + } else if ( (e->type() == QEvent::KeyRelease) && + (e->key() == Key_Meta) && + (QApplication::composedUnicode != 0) ) { + if ( (QApplication::composedUnicode > 0) && + (QApplication::composedUnicode < 0xFFFE) ) { + QChar ch( QApplication::composedUnicode ); + QString s( ch ); + QKeyEvent kep( QEvent::KeyPress, 0, ch.row() ? 0 : ch.cell(), 0, s ); + QKeyEvent ker( QEvent::KeyRelease, 0, ch.row() ? 0 : ch.cell(), 0, s ); + QApplication::sendEvent( w, &kep ); + QApplication::sendEvent( w, &ker ); + } + QApplication::composedUnicode = 0; + return TRUE; + } + } + return FALSE; +} + +/* + \internal + Checks for possible accelerators, if no widget + ate the keypres, or we are in the middle of a + partial key sequence. +*/ +bool QAccelManager::dispatchAccelEvent( QWidget* w, QKeyEvent* e ) +{ +#ifndef QT_NO_STATUSBAR + // Needs to be declared and used here because of "goto doclash" + QStatusBar* mainStatusBar = 0; +#endif + + // Modifiers can NOT be accelerators... + if ( e->key() >= Key_Shift && + e->key() <= Key_Alt ) + return FALSE; + + SequenceMatch result = Qt::NoMatch; + QKeySequence tocheck, partial; + QAccelPrivate* accel = 0; + QAccelItem* item = 0; + QAccelPrivate* firstaccel = 0; + QAccelItem* firstitem = 0; + QAccelPrivate* lastaccel = 0; + QAccelItem* lastitem = 0; + + QKeyEvent pe = *e; + int n = -1; + int hasShift = (e->state()&Qt::ShiftButton)?1:0; + bool identicalDisabled = FALSE; + bool matchFound = FALSE; + do { + accel = accels.first(); + matchFound = FALSE; + while ( accel ) { + if ( correctSubWindow( w, accel ) ) { + if ( accel->enabled ) { + item = accel->aitems.last(); + while( item ) { + if ( Qt::Identical == (result = match( &pe, item, tocheck )) ) { + if ( item->enabled ) { + if ( !firstaccel ) { + firstaccel = accel; + firstitem = item; + } + lastaccel = accel; + lastitem = item; + n++; + matchFound = TRUE; + if ( n > QMAX(clash,0) ) + goto doclash; + } else { + identicalDisabled = TRUE; + } + } + if ( item->enabled && Qt::PartialMatch == result ) { + partial = tocheck; + matchFound = TRUE; + } + item = accel->aitems.prev(); + } + } else { + item = accel->aitems.last(); + while( item ) { + if ( Qt::Identical == match( &pe, item, tocheck ) ) + identicalDisabled = TRUE; + item = accel->aitems.prev(); + } + } + } + accel = accels.next(); + } + pe = QKeyEvent( QEvent::Accel, pe.key(), pe.ascii(), pe.state()&~Qt::ShiftButton, pe.text() ); + } while ( hasShift-- && !matchFound && !identicalDisabled ); + +#ifndef QT_NO_STATUSBAR + mainStatusBar = (QStatusBar*) w->topLevelWidget()->child( 0, "QStatusBar" ); +#endif + if ( n < 0 ) { // no match found + currentState = partial.count() ? PartialMatch : NoMatch; +#ifndef QT_NO_STATUSBAR + // Only display message if we are, or were, in a partial match + if ( mainStatusBar && (PartialMatch == currentState || intermediate.count() ) ) { + if ( currentState == Qt::PartialMatch ) { + mainStatusBar->message( (QString)partial + ", ...", 0 ); + } else if (!identicalDisabled) { + QString message = QAccel::tr("%1, %2 not defined"). + arg( (QString)intermediate ). + arg( QKeySequence::encodeString( e->key() | translateModifiers(e->state()) ) ); + mainStatusBar->message( message, 2000 ); + // Since we're a NoMatch, reset the clash count + clash = -1; + } else { + mainStatusBar->clear(); + } + } +#endif + + bool eatKey = (PartialMatch == currentState || intermediate.count() ); + intermediate = partial; + if ( eatKey ) + e->accept(); + return eatKey; + } else if ( n == 0 ) { // found exactly one match + clash = -1; // reset +#ifndef QT_NO_STATUSBAR + if ( currentState == Qt::PartialMatch && mainStatusBar ) + mainStatusBar->clear(); +#endif + currentState = Qt::NoMatch; // Free sequence keylock + intermediate = QKeySequence(); + lastaccel->activate( lastitem ); + e->accept(); + return TRUE; + } + + doclash: // found more than one match +#ifndef QT_NO_STATUSBAR + if ( !mainStatusBar ) // if "goto doclash", we need to get statusbar again. + mainStatusBar = (QStatusBar*) w->topLevelWidget()->child( 0, "QStatusBar" ); +#endif + + QString message = QAccel::tr( "Ambiguous \"%1\" not handled" ).arg( (QString)tocheck ); + if ( clash >= 0 && n > clash ) { // pick next match + intermediate = QKeySequence(); + currentState = Qt::NoMatch; // Free sequence keylock + clash++; +#ifndef QT_NO_STATUSBAR + if ( mainStatusBar && + !lastitem->signal && + !(lastaccel->parent->receivers( "activatedAmbiguously(int)" )) ) + mainStatusBar->message( message, 2000 ); +#endif + lastaccel->activateAmbiguously( lastitem ); + } else { // start (or wrap) with the first matching + intermediate = QKeySequence(); + currentState = Qt::NoMatch; // Free sequence keylock + clash = 0; +#ifndef QT_NO_STATUSBAR + if ( mainStatusBar && + !firstitem->signal && + !(firstaccel->parent->receivers( "activatedAmbiguously(int)" )) ) + mainStatusBar->message( message, 2000 ); +#endif + firstaccel->activateAmbiguously( firstitem ); + } + e->accept(); + return TRUE; +} + +QAccelPrivate::QAccelPrivate( QAccel* p ) + : parent( p ) +{ + QAccelManager::self()->registerAccel( this ); + aitems.setAutoDelete( TRUE ); + ignorewhatsthis = FALSE; +} + +QAccelPrivate::~QAccelPrivate() +{ + QAccelManager::self()->unregisterAccel( this ); +} + +static QAccelItem *find_id( QAccelList &list, int id ) +{ + register QAccelItem *item = list.first(); + while ( item && item->id != id ) + item = list.next(); + return item; +} + +static QAccelItem *find_key( QAccelList &list, const QKeySequence &key ) +{ + register QAccelItem *item = list.first(); + while ( item && !( item->key == key ) ) + item = list.next(); + return item; +} + +/*! + Constructs a QAccel object called \a name, with parent \a parent. + The accelerator operates on \a parent. +*/ + +QAccel::QAccel( QWidget *parent, const char *name ) + : QObject( parent, name ) +{ + d = new QAccelPrivate( this ); + d->enabled = TRUE; + d->watch = parent; +#if defined(QT_CHECK_NULL) + if ( !d->watch ) + qWarning( "QAccel: An accelerator must have a parent or a watch widget" ); +#endif +} + +/*! + Constructs a QAccel object called \a name, that operates on \a + watch, and is a child of \a parent. + + This constructor is not needed for normal application programming. +*/ +QAccel::QAccel( QWidget* watch, QObject *parent, const char *name ) + : QObject( parent, name ) +{ + d = new QAccelPrivate( this ); + d->enabled = TRUE; + d->watch = watch; +#if defined(QT_CHECK_NULL) + if ( !d->watch ) + qWarning( "QAccel: An accelerator must have a parent or a watch widget" ); +#endif +} + +/*! + Destroys the accelerator object and frees all allocated resources. +*/ + +QAccel::~QAccel() +{ + delete d; +} + + +/*! + \fn void QAccel::activated( int id ) + + This signal is emitted when an accelerator key is pressed. \a id + is a number that identifies this particular accelerator item. + + \sa activatedAmbiguously() +*/ + +/*! + \fn void QAccel::activatedAmbiguously( int id ) + + This signal is emitted when an accelerator key is pressed. \a id + is a number that identifies this particular accelerator item. + + \sa activated() +*/ + + +/*! + Returns TRUE if the accelerator is enabled; otherwise returns + FALSE. + + \sa setEnabled(), isItemEnabled() +*/ + +bool QAccel::isEnabled() const +{ + return d->enabled; +} + + +/*! + Enables the accelerator if \a enable is TRUE, or disables it if \a + enable is FALSE. + + Individual keys can also be enabled or disabled using + setItemEnabled(). To work, a key must be an enabled item in an + enabled QAccel. + + \sa isEnabled(), setItemEnabled() +*/ + +void QAccel::setEnabled( bool enable ) +{ + d->enabled = enable; +} + + +/*! + Returns the number of accelerator items in this accelerator. +*/ + +uint QAccel::count() const +{ + return d->aitems.count(); +} + + +static int get_seq_id() +{ + static int seq_no = -2; // -1 is used as return value in findKey() + return seq_no--; +} + +/*! + Inserts an accelerator item and returns the item's identifier. + + \a key is a key code and an optional combination of SHIFT, CTRL + and ALT. \a id is the accelerator item id. + + If \a id is negative, then the item will be assigned a unique + negative identifier less than -1. + + \code + QAccel *a = new QAccel( myWindow ); // create accels for myWindow + a->insertItem( CTRL + Key_P, 200 ); // Ctrl+P, e.g. to print document + a->insertItem( ALT + Key_X, 201 ); // Alt+X, e.g. to quit + a->insertItem( UNICODE_ACCEL + 'q', 202 ); // Unicode 'q', e.g. to quit + a->insertItem( Key_D ); // gets a unique negative id < -1 + a->insertItem( CTRL + SHIFT + Key_P ); // gets a unique negative id < -1 + \endcode +*/ + +int QAccel::insertItem( const QKeySequence& key, int id ) +{ + if ( id == -1 ) + id = get_seq_id(); + d->aitems.insert( 0, new QAccelItem(key,id) ); + return id; +} + +/*! + Removes the accelerator item with the identifier \a id. +*/ + +void QAccel::removeItem( int id ) +{ + if ( find_id( d->aitems, id) ) + d->aitems.remove(); +} + + +/*! + Removes all accelerator items. +*/ + +void QAccel::clear() +{ + d->aitems.clear(); +} + + +/*! + Returns the key sequence of the accelerator item with identifier + \a id, or an invalid key sequence (0) if the id cannot be found. +*/ + +QKeySequence QAccel::key( int id ) +{ + QAccelItem *item = find_id( d->aitems, id); + return item ? item->key : QKeySequence( 0 ); +} + + +/*! + Returns the identifier of the accelerator item with the key code + \a key, or -1 if the item cannot be found. +*/ + +int QAccel::findKey( const QKeySequence& key ) const +{ + QAccelItem *item = find_key( d->aitems, key ); + return item ? item->id : -1; +} + + +/*! + Returns TRUE if the accelerator item with the identifier \a id is + enabled. Returns FALSE if the item is disabled or cannot be found. + + \sa setItemEnabled(), isEnabled() +*/ + +bool QAccel::isItemEnabled( int id ) const +{ + QAccelItem *item = find_id( d->aitems, id); + return item ? item->enabled : FALSE; +} + + +/*! + Enables the accelerator item with the identifier \a id if \a + enable is TRUE, and disables item \a id if \a enable is FALSE. + + To work, an item must be enabled and be in an enabled QAccel. + + \sa isItemEnabled(), isEnabled() +*/ + +void QAccel::setItemEnabled( int id, bool enable ) +{ + QAccelItem *item = find_id( d->aitems, id); + if ( item ) + item->enabled = enable; +} + + +/*! + Connects the accelerator item \a id to the slot \a member of \a + receiver. + + \code + a->connectItem( 201, mainView, SLOT(quit()) ); + \endcode + + Of course, you can also send a signal as \a member. + + Normally accelerators are connected to slots which then receive + the \c activated(int id) signal with the id of the accelerator + item that was activated. If you choose to connect a specific + accelerator item using this function, the \c activated() signal is + emitted if the associated key sequence is pressed but no \c + activated(int id) signal is emitted. + + \sa disconnectItem() +*/ + +bool QAccel::connectItem( int id, const QObject *receiver, const char *member ) +{ + QAccelItem *item = find_id( d->aitems, id); + if ( item ) { + if ( !item->signal ) { + item->signal = new QSignal; + Q_CHECK_PTR( item->signal ); + } + return item->signal->connect( receiver, member ); + } + return FALSE; +} + +/*! + Disconnects an accelerator item with id \a id from the function + called \a member in the \a receiver object. + + \sa connectItem() +*/ + +bool QAccel::disconnectItem( int id, const QObject *receiver, + const char *member ) +{ + QAccelItem *item = find_id( d->aitems, id); + if ( item && item->signal ) + return item->signal->disconnect( receiver, member ); + return FALSE; +} + +void QAccelPrivate::activate( QAccelItem* item ) +{ +#ifndef QT_NO_WHATSTHIS + if ( QWhatsThis::inWhatsThisMode() && !ignorewhatsthis ) { + QWhatsThis::leaveWhatsThisMode( item->whatsthis ); + return; + } +#endif + if ( item->signal ) + item->signal->activate(); + else + emit parent->activated( item->id ); +} + +void QAccelPrivate::activateAmbiguously( QAccelItem* item ) +{ + if ( item->signal ) + item->signal->activate(); + else + emit parent->activatedAmbiguously( item->id ); +} + + +/*! + Returns the shortcut key sequence for \a str, or an invalid key + sequence (0) if \a str has no shortcut sequence. + + For example, shortcutKey("E&xit") returns ALT+Key_X, + shortcutKey("&Quit") returns ALT+Key_Q and shortcutKey("Quit") + returns 0. (In code that does not inherit the Qt namespace class, + you must write e.g. Qt::ALT+Qt::Key_Q.) + + We provide a \link accelerators.html list of common accelerators + \endlink in English. At the time of writing, Microsoft and Open + Group do not appear to have issued equivalent recommendations for + other languages. +*/ + +QKeySequence QAccel::shortcutKey( const QString &str ) +{ + if(qt_accel_no_shortcuts) + return QKeySequence(); + + int p = 0; + while ( p >= 0 ) { + p = str.find( '&', p ) + 1; + if ( p <= 0 || p >= (int)str.length() ) + return 0; + if ( str[p] != '&' ) { + QChar c = str[p]; + if ( c.isPrint() ) { + char ltr = c.upper().latin1(); + if ( ltr >= (char)Key_A && ltr <= (char)Key_Z ) + c = ltr; + else + c = c.lower(); + return QKeySequence( c.unicode() + ALT + UNICODE_ACCEL ); + } + } + p++; + } + return QKeySequence(); +} + +/*! \obsolete + + Creates an accelerator string for the key \a k. + For instance CTRL+Key_O gives "Ctrl+O". The "Ctrl" etc. + are translated (using QObject::tr()) in the "QAccel" context. + + The function is superfluous. Cast the QKeySequence \a k to a + QString for the same effect. +*/ +QString QAccel::keyToString( QKeySequence k ) +{ + return (QString) k; +} + +/*!\obsolete + + Returns an accelerator code for the string \a s. For example + "Ctrl+O" gives CTRL+UNICODE_ACCEL+'O'. The strings "Ctrl", + "Shift", "Alt" are recognized, as well as their translated + equivalents in the "QAccel" context (using QObject::tr()). Returns 0 + if \a s is not recognized. + + This function is typically used with \link QObject::tr() tr + \endlink(), so that accelerator keys can be replaced in + translations: + + \code + QPopupMenu *file = new QPopupMenu( this ); + file->insertItem( p1, tr("&Open..."), this, SLOT(open()), + QAccel::stringToKey(tr("Ctrl+O", "File|Open")) ); + \endcode + + Notice the \c "File|Open" translator comment. It is by no means + necessary, but it provides some context for the human translator. + + + The function is superfluous. Construct a QKeySequence from the + string \a s for the same effect. + + \sa QObject::tr() + \link i18n.html Internationalization with Qt \endlink +*/ +QKeySequence QAccel::stringToKey( const QString & s ) +{ + return QKeySequence( s ); +} + + +/*! + Sets a What's This help text for the accelerator item \a id to \a + text. + + The text will be shown when the application is in What's This mode + and the user hits the accelerator key. + + To set What's This help on a menu item (with or without an + accelerator key), use QMenuData::setWhatsThis(). + + \sa whatsThis(), QWhatsThis::inWhatsThisMode(), + QMenuData::setWhatsThis(), QAction::setWhatsThis() +*/ +void QAccel::setWhatsThis( int id, const QString& text ) +{ + + QAccelItem *item = find_id( d->aitems, id); + if ( item ) + item->whatsthis = text; +} + +/*! + Returns the What's This help text for the specified item \a id or + QString::null if no text has been specified. + + \sa setWhatsThis() +*/ +QString QAccel::whatsThis( int id ) const +{ + + QAccelItem *item = find_id( d->aitems, id); + return item? item->whatsthis : QString::null; +} + +/*!\internal */ +void QAccel::setIgnoreWhatsThis( bool b) +{ + d->ignorewhatsthis = b; +} + +/*!\internal */ +bool QAccel::ignoreWhatsThis() const +{ + return d->ignorewhatsthis; +} + + +/*! + +\page accelerators.html + +\title Standard Accelerator Keys + +Applications invariably need to define accelerator keys for actions. +Qt fully supports accelerators, for example with \l QAccel::shortcutKey(). + +Here are Microsoft's recommendations for accelerator keys, with +comments about the Open Group's recommendations where they exist +and differ. For most commands, the Open Group either has no advice or +agrees with Microsoft. + +The emboldened letter plus Alt is Microsoft's recommended choice, and +we recommend supporting it. For an Apply button, for example, we +recommend QButton::setText( \link QWidget::tr() tr \endlink("&Apply") ); + +If you have conflicting commands (e.g. About and Apply buttons in the +same dialog), you must decide for yourself. + +\list +\i <b><u>A</u></b>bout +\i Always on <b><u>T</u></b>op +\i <b><u>A</u></b>pply +\i <b><u>B</u></b>ack +\i <b><u>B</u></b>rowse +\i <b><u>C</u></b>lose (CDE: Alt+F4; Alt+F4 is "close window" in Windows) +\i <b><u>C</u></b>opy (CDE: Ctrl+C, Ctrl+Insert) +\i <b><u>C</u></b>opy Here +\i Create <b><u>S</u></b>hortcut +\i Create <b><u>S</u></b>hortcut Here +\i Cu<b><u>t</u></b> +\i <b><u>D</u></b>elete +\i <b><u>E</u></b>dit +\i <b><u>E</u></b>xit (CDE: E<b><u>x</u></b>it) +\i <b><u>E</u></b>xplore +\i <b><u>F</u></b>ile +\i <b><u>F</u></b>ind +\i <b><u>H</u></b>elp +\i Help <b><u>T</u></b>opics +\i <b><u>H</u></b>ide +\i <b><u>I</u></b>nsert +\i Insert <b><u>O</u></b>bject +\i <b><u>L</u></b>ink Here +\i Ma<b><u>x</u></b>imize +\i Mi<b><u>n</u></b>imize +\i <b><u>M</u></b>ove +\i <b><u>M</u></b>ove Here +\i <b><u>N</u></b>ew +\i <b><u>N</u></b>ext +\i <b><u>N</u></b>o +\i <b><u>O</u></b>pen +\i Open <b><u>W</u></b>ith +\i Page Set<b><u>u</u></b>p +\i <b><u>P</u></b>aste +\i Paste <b><u>L</u></b>ink +\i Paste <b><u>S</u></b>hortcut +\i Paste <b><u>S</u></b>pecial +\i <b><u>P</u></b>ause +\i <b><u>P</u></b>lay +\i <b><u>P</u></b>rint +\i <b><u>P</u></b>rint Here +\i P<b><u>r</u></b>operties +\i <b><u>Q</u></b>uick View +\i <b><u>R</u></b>edo (CDE: Ctrl+Y, Shift+Alt+Backspace) +\i <b><u>R</u></b>epeat +\i <b><u>R</u></b>estore +\i <b><u>R</u></b>esume +\i <b><u>R</u></b>etry +\i <b><u>R</u></b>un +\i <b><u>S</u></b>ave +\i Save <b><u>A</u></b>s +\i Select <b><u>A</u></b>ll +\i Se<b><u>n</u></b>d To +\i <b><u>S</u></b>how +\i <b><u>S</u></b>ize +\i S<b><u>p</u></b>lit +\i <b><u>S</u></b>top +\i <b><u>U</u></b>ndo (CDE: Ctrl+Z or Alt+Backspace) +\i <b><u>V</u></b>iew +\i <b><u>W</u></b>hat's This? +\i <b><u>W</u></b>indow +\i <b><u>Y</u></b>es +\endlist + +There are also a lot of other keys and actions (that use other +modifier keys than Alt). See the Microsoft and The Open Group +documentation for details. + +The \link http://www.amazon.com/exec/obidos/ASIN/0735605661/trolltech/t +Microsoft book \endlink has ISBN 0735605661. The corresponding Open Group +book is very hard to find, rather expensive and we cannot recommend +it. However, if you really want it, OGPubs@opengroup.org might be able +to help. Ask them for ISBN 1859121047. + +*/ + +/*! \obsolete serves no purpose anymore */ +void QAccel::repairEventFilter() {} +/*! \obsolete serves no purpose anymore */ +bool QAccel::eventFilter( QObject *, QEvent * ) { return FALSE; } +#endif // QT_NO_ACCEL diff --git a/src/kernel/qaccel.h b/src/kernel/qaccel.h new file mode 100644 index 0000000..97b0930 --- /dev/null +++ b/src/kernel/qaccel.h @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Definition of QAccel class +** +** Created : 950419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QACCEL_H +#define QACCEL_H + +#ifndef QT_H +#include "qobject.h" +#include "qkeysequence.h" +#endif // QT_H + +#ifndef QT_NO_ACCEL + +class QAccelPrivate; + +class Q_EXPORT QAccel : public QObject // accelerator class +{ + Q_OBJECT +public: + QAccel( QWidget *parent, const char *name=0 ); + QAccel( QWidget* watch, QObject *parent, const char *name=0 ); + ~QAccel(); + + bool isEnabled() const; + void setEnabled( bool ); + + uint count() const; + + int insertItem( const QKeySequence& key, int id=-1); + void removeItem( int id ); + void clear(); + + QKeySequence key( int id ); + int findKey( const QKeySequence& key ) const; + + bool isItemEnabled( int id ) const; + void setItemEnabled( int id, bool enable ); + + bool connectItem( int id, const QObject *receiver, const char* member ); + bool disconnectItem( int id, const QObject *receiver, const char* member ); + + void repairEventFilter(); + + void setWhatsThis( int id, const QString& ); + QString whatsThis( int id ) const; + void setIgnoreWhatsThis( bool ); + bool ignoreWhatsThis() const; + + static QKeySequence shortcutKey( const QString & ); + static QString keyToString(QKeySequence k ); + static QKeySequence stringToKey( const QString & ); + +signals: + void activated( int id ); + void activatedAmbiguously( int id ); + +protected: + bool eventFilter( QObject *, QEvent * ); + +private: + QAccelPrivate * d; + +private: +#if defined(Q_DISABLE_COPY) + QAccel( const QAccel & ); + QAccel &operator=( const QAccel & ); +#endif + friend class QAccelPrivate; + friend class QAccelManager; +}; + +#endif // QT_NO_ACCEL +#endif // QACCEL_H diff --git a/src/kernel/qaccessible.cpp b/src/kernel/qaccessible.cpp new file mode 100644 index 0000000..d037124 --- /dev/null +++ b/src/kernel/qaccessible.cpp @@ -0,0 +1,719 @@ +/**************************************************************************** +** +** Implementation of QAccessible and QAccessibleObject classes +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qaccessible.h" + +#if defined(QT_ACCESSIBILITY_SUPPORT) + +#include "qptrdict.h" +#include "qmetaobject.h" +#include <private/qpluginmanager_p.h> +#include "qapplication.h" +#include <stdlib.h> + +/*! + \class QAccessible qaccessible.h + \brief The QAccessible class provides enums and static functions + relating to accessibility. + + \ingroup misc + + Accessibility clients use implementations of the + QAccessibleInterface to read the information an accessible object + exposes, or to call functions to manipulate the accessible object. + +\omit + Qt provides implementations of the QAccessibleInterface for most + widget classes in a plugin. This plugin is located in the \e + accessibility subdirectory of the plugins installation directory. + The default installation directory for plugins is \c INSTALL/plugins, + where \c INSTALL is the directory where Qt was installed. Calling + queryAccessibleInterface( QObject *object, QAccessibleInterface + **iface ) will ask all plugins located in this directory for an + implementation that exposes the information for objects of the + class of \e object. + + To make a Qt application accessible you have to distribute the + accessibility plugin provded with Qt together with your + application. Simply add the plugins created in + INSTALL/plugins/accessibility to your distribution process. Use \l + QApplication::addLibraryPath() to specify a plugin directory for + your application, and copy the files into an \e accessibility + subdirectory of one of those plugin directories. Qt's + accessibility framework will load the plugins upon request and use + the implementations provided to expose an object's accessibility + information. +\endomit + + See the \link plugins-howto.html plugin documentation \endlink for + more details about how to redistribute Qt plugins. +*/ + +/*! + \enum QAccessible::State + + This enum type defines bitflags that can be combined to indicate + the state of the accessible object. The values are: + + \value Normal + \value Unavailable + \value Selected + \value Focused + \value Pressed + \value Checked + \value Mixed + \value ReadOnly + \value HotTracked + \value Default + \value Expanded + \value Collapsed + \value Busy + \value Floating + \value Marqueed + \value Animated + \value Invisible + \value Offscreen + \value Sizeable + \value Moveable + \value SelfVoicing + \value Focusable + \value Selectable + \value Linked + \value Traversed + \value MultiSelectable + \value ExtSelectable + \value AlertLow + \value AlertMedium + \value AlertHigh + \value Protected + \value Valid +*/ + +/*! + \enum QAccessible::Event + + This enum type defines event types when the state of the + accessible object has changed. The event types are: + + \value SoundPlayed + \value Alert + \value ForegroundChanged + \value MenuStart + \value MenuEnd + \value PopupMenuStart + \value PopupMenuEnd + \value ContextHelpStart + \value ContextHelpEnd + \value DragDropStart + \value DragDropEnd + \value DialogStart + \value DialogEnd + \value ScrollingStart + \value ScrollingEnd + \value ObjectCreated + \value ObjectDestroyed + \value ObjectShow + \value ObjectHide + \value ObjectReorder + \value Focus + \value Selection + \value SelectionAdd + \value SelectionRemove + \value SelectionWithin + \value StateChanged + \value LocationChanged + \value NameChanged + \value DescriptionChanged + \value ValueChanged + \value ParentChanged + \value HelpChanged + \value DefaultActionChanged + \value AcceleratorChanged + \value MenuCommand +*/ + +/*! + \enum QAccessible::Role + + This enum defines a number of roles an accessible object can have. + The roles are: + + \value NoRole + \value TitleBar + \value MenuBar + \value ScrollBar + \value Grip + \value Sound + \value Cursor + \value Caret + \value AlertMessage + \value Window + \value Client + \value PopupMenu + \value MenuItem + \value ToolTip + \value Application + \value Document + \value Pane + \value Chart + \value Dialog + \value Border + \value Grouping + \value Separator + \value ToolBar + \value StatusBar + \value Table + \value ColumnHeader + \value RowHeader + \value Column + \value Row + \value Cell + \value Link + \value HelpBalloon + \value Character + \value List + \value ListItem + \value Outline + \value OutlineItem + \value PageTab + \value PropertyPage + \value Indicator + \value Graphic + \value StaticText + \value EditableText + \value PushButton + \value CheckBox + \value RadioButton + \value ComboBox + \value DropLest + \value ProgressBar + \value Dial + \value HotkeyField + \value Slider + \value SpinBox + \value Diagram + \value Animation + \value Equation + \value ButtonDropDown + \value ButtonMenu + \value ButtonDropGrid + \value Whitespace + \value PageTabList + \value Clock +*/ + +/*! + \enum QAccessible::NavDirection + + This enum specifies which item to move to when navigating. + + \value NavUp sibling above + \value NavDown sibling below + \value NavLeft left sibling + \value NavRight right sibling + \value NavNext next sibling + \value NavPrevious previous sibling + \value NavFirstChild first child + \value NavLastChild last child + \value NavFocusChild child with focus +*/ + +/*! + \enum QAccessible::Text + + This enum specifies string information that an accessible object + returns. + + \value Name The name of the object + \value Description A short text describing the object + \value Value The value of the object + \value Help A longer text giving information about how + to use the object + \value DefaultAction The default method to interact with the object + \value Accelerator The keyboard shortcut that executes the + default action +*/ + +/*! + \fn static void QAccessible::updateAccessibility( QObject *object, int control, Event reason ) + + Notifies accessibility clients about a change in \a object's + accessibility information. + + \a reason specifies the cause of the change, for example, + ValueChange when the position of a slider has been changed. \a + control is the ID of the child element that has changed. When \a + control is 0, the object itself has changed. + + Call this function whenever the state of your accessible object or + one of it's sub-elements has been changed either programmatically + (e.g. by calling QLabel::setText()) or by user interaction. + + If there are no accessibility tools listening to this event, the + performance penalty for calling this function is minor, but if determining + the parameters of the call is expensive you can use isActive() to + avoid unnecessary performance penalties if no client is listening. +*/ + +static QPluginManager<QAccessibleFactoryInterface> *qAccessibleManager = 0; + +class AccessibleCache : public QObject, public QPtrDict<QAccessibleInterface> +{ + Q_OBJECT +public: + AccessibleCache() + : QPtrDict<QAccessibleInterface>(73) + { + } + + void addObject(QObject *object, QAccessibleInterface *iface) + { + insert(object, iface); + connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(removeObject(QObject*))); + } + +public slots: + void removeObject(QObject *object); +}; + +#include "qaccessible.moc" + +static AccessibleCache *qAccessibleInterface = 0; +static bool cleanupAdded = FALSE; + +static void qAccessibleCleanup() +{ + if ( qAccessibleInterface && qAccessibleInterface->count() && qAccessibleManager ) + qAccessibleManager->setAutoUnload( FALSE ); + + delete qAccessibleInterface; + qAccessibleInterface = 0; + delete qAccessibleManager; + qAccessibleManager = 0; +} + +#ifdef Q_WS_MAC +QObject *QAccessible::queryAccessibleObject(QAccessibleInterface *o) +{ + if(qAccessibleInterface) { + for(QPtrDictIterator<QAccessibleInterface> it(*qAccessibleInterface); it.current(); ++it) { + if(it.current() == o) + return (QObject*)it.currentKey(); + } + } + return NULL; +} +#endif + +void AccessibleCache::removeObject(QObject *object) +{ + if (!object) + return; + + remove(object); + if (!count()) { + delete this; + qAccessibleInterface = 0; + } +} + + +/*! + Sets \a iface to point to the implementation of the + QAccessibleInterface for \a object, and returns \c QS_OK if + successfull, or sets \a iface to 0 and returns \c QE_NOCOMPONENT if + no accessibility implementation for \a object exists. + + The function uses the \link QObject::className() classname + \endlink of \a object to find a suitable implementation. If no + implementation for the object's class is available the function + tries to find an implementation for the object's parent class. + + This function is called to answer an accessibility client's + request for object information. You should never need to call this + function yourself. +*/ +QRESULT QAccessible::queryAccessibleInterface( QObject *object, QAccessibleInterface **iface ) +{ + *iface = 0; + if ( !object ) + return QE_INVALIDARG; + + if ( qAccessibleInterface ) { + *iface = qAccessibleInterface->find( object ); + if ( *iface ) { + (*iface)->addRef(); + return QS_OK; + } + } + + if ( !qAccessibleManager ) { + qAccessibleManager = new QPluginManager<QAccessibleFactoryInterface>( IID_QAccessibleFactory, QApplication::libraryPaths(), "/accessible" ); + if ( !cleanupAdded ) { + qAddPostRoutine( qAccessibleCleanup ); + cleanupAdded = TRUE; + } + } + + QInterfacePtr<QAccessibleFactoryInterface> factory = 0; + QMetaObject *mo = object->metaObject(); + while ( mo ) { + qAccessibleManager->queryInterface( mo->className(), &factory ); + if ( factory ) + break; + mo = mo->superClass(); + } + if ( factory ) + return factory->createAccessibleInterface( mo->className(), object, iface ); + + return QE_NOCOMPONENT; +} + +/*! + Returns TRUE if an accessibility implementation has been requested, + during the runtime of the application, otherwise returns FALSE. + + Use this function to prevent potentially expensive notifications via + updateAccessibility(). + + \omit + QListView uses this function to prevent index-lookups for item based + notifications. + \endomit +*/ +bool QAccessible::isActive() +{ + return qAccessibleManager != 0; +} + +/*! + \class QAccessibleInterface qaccessible.h + \brief The QAccessibleInterface class defines an interface that exposes information about accessible objects. + + \ingroup misc +*/ + +/*! + \fn bool QAccessibleInterface::isValid() const + + Returns TRUE if all the data necessary to use this interface + implementation is valid (e.g. all pointers are non-null), + otherwise returns FALSE. +*/ + +/*! + \fn int QAccessibleInterface::childCount() const + + Returns the number of children that belong to this object. A child + can provide accessibility information on it's own (e.g. a child + widget), or be a sub-element of this accessible object. + + All objects provide this information. + + \sa queryChild() +*/ + +/*! + \fn QRESULT QAccessibleInterface::queryChild( int control, QAccessibleInterface **iface ) const + + Sets \a iface to point to the implementation of the + QAccessibleInterface for the child specified with \a control. If + the child doesn't provide accessibility information on it's own, + the value of \a iface is set to 0. For those elements, this + object is responsible for exposing the child's properties. + + All objects provide this information. + + \sa childCount(), queryParent() +*/ + +/*! + \fn QRESULT QAccessibleInterface::queryParent( QAccessibleInterface **iface ) const + + Sets \a iface to point to the implementation of the + QAccessibleInterface for the parent object, or to 0 if there is + no such implementation or object. + + All objects provide this information. + + \sa queryChild() +*/ + +/*! + \fn int QAccessibleInterface::controlAt( int x, int y ) const + + Returns the ID of the child that contains the screen coordinates + (\a x, \a y). This function returns 0 if the point is positioned + on the object itself. If the tested point is outside the + boundaries of the object this function returns -1. + + All visual objects provide this information. +*/ + +/*! + \fn QRect QAccessibleInterface::rect( int control ) const + + Returns the location of the child specified with \a control in + screen coordinates. This function returns the location of the + object itself if \a control is 0. + + All visual objects provide this information. +*/ + +/*! + \fn int QAccessibleInterface::navigate( NavDirection direction, int startControl ) const + + This function traverses to another object, or to a sub-element of + the current object. \a direction specifies in which direction to + navigate, and \a startControl specifies the start point of the + navigation, which is either 0 if the navigation starts at the + object itself, or an ID of one of the object's sub-elements. + + The function returns the ID of the sub-element located in the \a + direction specified. If there is nothing in the navigated \a + direction, this function returns -1. + + All objects support navigation. +*/ + +/*! + \fn QString QAccessibleInterface::text( Text t, int control ) const + + Returns a string property \a t of the child object specified by \a + control, or the string property of the object itself if \a control + is 0. + + The \e Name is a string used by clients to identify, find or + announce an accessible object for the user. All objects must have + a name that is unique within their container. + + An accessible object's \e Description provides textual information + about an object's visual appearance. The description is primarily + used to provide greater context for low-vision or blind users, but + is also used for context searching or other applications. Not all + objects have a description. An "OK" button would not need a + description, but a toolbutton that shows a picture of a smiley + would. + + The \e Value of an accessible object represents visual information + contained by the object, e.g. the text in a line edit. Usually, + the value can be modified by the user. Not all objects have a + value, e.g. static text labels don't, and some objects have a + state that already is the value, e.g. toggle buttons. + + The \e Help text provides information about the function and + usage of an accessible object. Not all objects provide this + information. + + An accessible object's \e DefaultAction describes the object's + primary method of manipulation, and should be a verb or a short + phrase, e.g. "Press" for a button. + + The accelerator is a keyboard shortcut that activates the default + action of the object. A keyboard shortcut is the underlined + character in the text of a menu, menu item or control, and is + either the character itself, or a combination of this character + and a modifier key like ALT, CTRL or SHIFT. Command controls like + tool buttons also have shortcut keys and usually display them in + their tooltip. + + \sa role(), state(), selection() +*/ + +/*! + \fn void QAccessibleInterface::setText( Text t, int control, const QString &text ) + + Sets the text property \a t of the child object \a control to \a + text. If \a control is 0, the text property of the object itself + is set. +*/ + +/*! + \fn QAccessible::Role QAccessibleInterface::role( int control ) const + + Returns the role of the object if \a control is 0, or the role of + the object's sub-element with ID \a control. The role of an object + is usually static. All accessible objects have a role. + + \sa text(), state(), selection() +*/ + +/*! + \fn QAccessible::State QAccessibleInterface::state( int control ) const + + Returns the current state of the object if \a control is 0, or the + state of the object's sub-element element with ID \a control. All + objects have a state. + + \sa text(), role(), selection() +*/ + +/*! + \fn QMemArray<int> QAccessibleInterface::selection() const + + Returns the list of all the element IDs that are selected. + + \sa text(), role(), state() +*/ + +/*! + \fn bool QAccessibleInterface::doDefaultAction( int control ) + + Calling this function performs the default action of the child + object specified by \a control, or the default action of the + object itself if \a control is 0. +*/ + +/*! + \fn bool QAccessibleInterface::setFocus( int control ) + + Gives the focus to the child object specified by \a control, or to + the object itself if \a control is 0. + + Returns TRUE if the focus could be set; otherwise returns FALSE. +*/ + +/*! + \fn bool QAccessibleInterface::setSelected( int control, bool on, bool extend ) + + Sets the selection of the child object with ID \a control to \a + on. If \a extend is TRUE, all child elements between the focused + item and the specified child object have their selection set to \a + on. + + Returns TRUE if the selection could be set; otherwise returns + FALSE. + + \sa setFocus(), clearSelection() +*/ + +/*! + \fn void QAccessibleInterface::clearSelection() + + Removes any selection from the object. + + \sa setSelected() +*/ + + + +/*! + \class QAccessibleObject qaccessible.h + \brief The QAccessibleObject class implements parts of the + QAccessibleInterface for QObjects. + + \ingroup misc + + This class is mainly provided for convenience. All subclasses of + the QAccessibleInterface should use this class as the base class. +*/ + +/*! + Creates a QAccessibleObject for \a object. +*/ +QAccessibleObject::QAccessibleObject( QObject *object ) +: object_(object) +{ + if ( !qAccessibleInterface ) { + qAccessibleInterface = new AccessibleCache; + if ( !cleanupAdded ) { + qAddPostRoutine( qAccessibleCleanup ); + cleanupAdded = TRUE; + } + } + + qAccessibleInterface->addObject(object, this); +} + +/*! + Destroys the QAccessibleObject. + + This only happens when a call to release() decrements the internal + reference counter to zero. +*/ +QAccessibleObject::~QAccessibleObject() +{ + if ( qAccessibleInterface ) { + qAccessibleInterface->removeObject(object_); + if ( !qAccessibleInterface->count() ) { + delete qAccessibleInterface; + qAccessibleInterface = 0; + } + } +} + +/*! + \reimp +*/ +QRESULT QAccessibleObject::queryInterface( const QUuid &uuid, QUnknownInterface **iface ) +{ + *iface = 0; + if ( uuid == IID_QAccessible ) + *iface = (QAccessibleInterface*)this; + else if ( uuid == IID_QUnknown ) + *iface = (QUnknownInterface*)this; + else + return QE_NOINTERFACE; + + (*iface)->addRef(); + return QS_OK; +} + +/*! + Returns the QObject for which this QAccessibleInterface + implementation provides information. Use isValid() to make sure + the object pointer is safe to use. +*/ +QObject *QAccessibleObject::object() const +{ +#if defined(QT_CHECK_RANGE) + if ( !isValid() ) + qWarning( "QAccessibleInterface is invalid. Crash pending..." ); +#endif + return object_; +} + +/*! + \reimp +*/ +bool QAccessibleObject::isValid() const +{ + return !object_.isNull(); +} + +#endif diff --git a/src/kernel/qaccessible.h b/src/kernel/qaccessible.h new file mode 100644 index 0000000..956f0bb --- /dev/null +++ b/src/kernel/qaccessible.h @@ -0,0 +1,295 @@ +/**************************************************************************** +** +** Definition of QAccessible and QAccessibleObject classes +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QACCESSIBLE_H +#define QACCESSIBLE_H + +#ifndef QT_H +#include "qobject.h" +#include <private/qcom_p.h> +#include "qrect.h" +#include "qguardedptr.h" +#include "qmemarray.h" +#endif // QT_H + +#if defined(QT_ACCESSIBILITY_SUPPORT) + +struct QAccessibleInterface; + +class Q_EXPORT QAccessible +{ +private: +#ifdef Q_WS_MAC + static QMAC_PASCAL OSStatus globalEventProcessor(EventHandlerCallRef, EventRef, void *); + static QObject *queryAccessibleObject(QAccessibleInterface *); +#endif +public: + enum Event { + SoundPlayed = 0x0001, + Alert = 0x0002, + ForegroundChanged = 0x0003, + MenuStart = 0x0004, + MenuEnd = 0x0005, + PopupMenuStart = 0x0006, + PopupMenuEnd = 0x0007, + ContextHelpStart = 0x000C, + ContextHelpEnd = 0x000D, + DragDropStart = 0x000E, + DragDropEnd = 0x000F, + DialogStart = 0x0010, + DialogEnd = 0x0011, + ScrollingStart = 0x0012, + ScrollingEnd = 0x0013, + + MenuCommand = 0x0018, + + ObjectCreated = 0x8000, + ObjectDestroyed = 0x8001, + ObjectShow = 0x8002, + ObjectHide = 0x8003, + ObjectReorder = 0x8004, + Focus = 0x8005, + Selection = 0x8006, + SelectionAdd = 0x8007, + SelectionRemove = 0x8008, + SelectionWithin = 0x8009, + StateChanged = 0x800A, + LocationChanged = 0x800B, + NameChanged = 0x800C, + DescriptionChanged = 0x800D, + ValueChanged = 0x800E, + ParentChanged = 0x800F, + HelpChanged = 0x80A0, + DefaultActionChanged= 0x80B0, + AcceleratorChanged = 0x80C0 + }; + + enum State { + Normal = 0x00000000, + Unavailable = 0x00000001, + Selected = 0x00000002, + Focused = 0x00000004, + Pressed = 0x00000008, + Checked = 0x00000010, + Mixed = 0x00000020, + ReadOnly = 0x00000040, + HotTracked = 0x00000080, + Default = 0x00000100, + Expanded = 0x00000200, + Collapsed = 0x00000400, + Busy = 0x00000800, + Floating = 0x00001000, + Marqueed = 0x00002000, + Animated = 0x00004000, + Invisible = 0x00008000, + Offscreen = 0x00010000, + Sizeable = 0x00020000, + Moveable = 0x00040000, + SelfVoicing = 0x00080000, + Focusable = 0x00100000, + Selectable = 0x00200000, + Linked = 0x00400000, + Traversed = 0x00800000, + MultiSelectable = 0x01000000, + ExtSelectable = 0x02000000, + AlertLow = 0x04000000, + AlertMedium = 0x08000000, + AlertHigh = 0x10000000, + Protected = 0x20000000, + Valid = 0x3fffffff + }; + + enum Role { + NoRole = 0x00000000, + TitleBar = 0x00000001, + MenuBar = 0x00000002, + ScrollBar = 0x00000003, + Grip = 0x00000004, + Sound = 0x00000005, + Cursor = 0x00000006, + Caret = 0x00000007, + AlertMessage = 0x00000008, + Window = 0x00000009, + Client = 0x0000000A, + PopupMenu = 0x0000000B, + MenuItem = 0x0000000C, + ToolTip = 0x0000000D, + Application = 0x0000000E, + Document = 0x0000000F, + Pane = 0x00000010, + Chart = 0x00000011, + Dialog = 0x00000012, + Border = 0x00000013, + Grouping = 0x00000014, + Separator = 0x00000015, + ToolBar = 0x00000016, + StatusBar = 0x00000017, + Table = 0x00000018, + ColumnHeader = 0x00000019, + RowHeader = 0x0000001A, + Column = 0x0000001B, + Row = 0x0000001C, + Cell = 0x0000001D, + Link = 0x0000001E, + HelpBalloon = 0x0000001F, + Character = 0x00000020, + List = 0x00000021, + ListItem = 0x00000022, + Outline = 0x00000023, + OutlineItem = 0x00000024, + PageTab = 0x00000025, + PropertyPage = 0x00000026, + Indicator = 0x00000027, + Graphic = 0x00000028, + StaticText = 0x00000029, + EditableText = 0x0000002A, // Editable, selectable, etc. + PushButton = 0x0000002B, + CheckBox = 0x0000002C, + RadioButton = 0x0000002D, + ComboBox = 0x0000002E, + DropLest = 0x0000002F, + ProgressBar = 0x00000030, + Dial = 0x00000031, + HotkeyField = 0x00000032, + Slider = 0x00000033, + SpinBox = 0x00000034, + Diagram = 0x00000035, + Animation = 0x00000036, + Equation = 0x00000037, + ButtonDropDown = 0x00000038, + ButtonMenu = 0x00000039, + ButtonDropGrid = 0x0000003A, + Whitespace = 0x0000003B, + PageTabList = 0x0000003C, + Clock = 0x0000003D + }; + + enum NavDirection { + NavUp = 0x00000001, + NavDown = 0x00000002, + NavLeft = 0x00000003, + NavRight = 0x00000004, + NavNext = 0x00000005, + NavPrevious = 0x00000006, + NavFirstChild = 0x00000007, + NavLastChild = 0x00000008, + NavFocusChild = 0x00000009 + }; + + enum Text { + Name = 0, + Description, + Value, + Help, + Accelerator, + DefaultAction + }; + + static QRESULT queryAccessibleInterface( QObject *, QAccessibleInterface ** ); + static void updateAccessibility( QObject *, int who, Event reason ); + static bool isActive(); + + static void initialize(); + static void cleanup(); +}; + +// {EC86CB9C-5DA0-4c43-A739-13EBDF1C6B14} +#define IID_QAccessible QUuid( 0xec86cb9c, 0x5da0, 0x4c43, 0xa7, 0x39, 0x13, 0xeb, 0xdf, 0x1c, 0x6b, 0x14 ) + +struct Q_EXPORT QAccessibleInterface : public QAccessible, public QUnknownInterface +{ + // check for valid pointers + virtual bool isValid() const = 0; + + // hierarchy + virtual int childCount() const = 0; + virtual QRESULT queryChild( int control, QAccessibleInterface** ) const = 0; + virtual QRESULT queryParent( QAccessibleInterface** ) const = 0; + + // navigation + virtual int controlAt( int x, int y ) const = 0; + virtual QRect rect( int control ) const = 0; + virtual int navigate( NavDirection direction, int startControl ) const = 0; + + // properties and state + virtual QString text( Text t, int control ) const = 0; + virtual void setText( Text t, int control, const QString &text ) = 0; + virtual Role role( int control ) const = 0; + virtual State state( int control ) const = 0; + virtual QMemArray<int> selection() const = 0; + + // methods + virtual bool doDefaultAction( int control ) = 0; + virtual bool setFocus( int control ) = 0; + virtual bool setSelected( int control, bool on, bool extend ) = 0; + virtual void clearSelection() = 0; +}; + +// {49F4C6A7-412F-41DE-9E24-648843421FD3} +#ifndef IID_QAccessibleFactory +#define IID_QAccessibleFactory QUuid( 0x49f4c6a7, 0x412f, 0x41de, 0x9e, 0x24, 0x64, 0x88, 0x43, 0x42, 0x1f, 0xd3 ) +#endif + +struct Q_EXPORT QAccessibleFactoryInterface : public QAccessible, public QFeatureListInterface +{ + virtual QRESULT createAccessibleInterface( const QString &, QObject *, QAccessibleInterface** ) = 0; +}; + +class Q_EXPORT QAccessibleObject : public QObject, public QAccessibleInterface +{ +public: + QAccessibleObject( QObject *object ); + virtual ~QAccessibleObject(); + + QRESULT queryInterface( const QUuid &, QUnknownInterface** ); + Q_REFCOUNT + + bool isValid() const; + +protected: + QObject *object() const; + +private: + QGuardedPtr<QObject> object_; +}; + +#define Q_DEFINED_QACCESSIBLE_OBJECT +#include "qwinexport.h" +#endif //QT_ACCESSIBILITY_SUPPORT + +#endif //QACCESSIBLE_H diff --git a/src/kernel/qapplication.cpp b/src/kernel/qapplication.cpp new file mode 100644 index 0000000..7296f4c --- /dev/null +++ b/src/kernel/qapplication.cpp @@ -0,0 +1,4602 @@ +/**************************************************************************** +** +** Implementation of QApplication class +** +** Created : 931107 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qobjectlist.h" +#include "qapplication.h" +#include "qeventloop.h" +#include "qeventloop_p.h" +#include "qwidget.h" +#include "qwidgetlist.h" +#include "qwidgetintdict.h" +#include "qptrdict.h" +#include "qcleanuphandler.h" + +#include "qtranslator.h" +#include "qtextcodec.h" +#include "qsessionmanager.h" +#include "qdragobject.h" +#include "qclipboard.h" +#include "qcursor.h" +#include "qstyle.h" +#include "qstylefactory.h" +#include "qfile.h" +#include "qmessagebox.h" +#include "qdir.h" +#include "qfileinfo.h" +#ifdef Q_WS_WIN +#include "qinputcontext_p.h" +#endif +#include "qfontdata_p.h" + +#if defined(QT_THREAD_SUPPORT) +# include "qmutex.h" +# include "qthread.h" +#endif // QT_THREAD_SUPPORT + +#include <stdlib.h> + +#ifdef truncate +# undef truncate +#endif + +/*! + \class QApplication qapplication.h + \brief The QApplication class manages the GUI application's control + flow and main settings. + + \ingroup application + \mainclass + + It contains the main event loop, where all events from the window + system and other sources are processed and dispatched. It also + handles the application's initialization and finalization, and + provides session management. It also handles most system-wide and + application-wide settings. + + For any GUI application that uses Qt, there is precisely one + QApplication object, no matter whether the application has 0, 1, 2 + or more windows at any time. + + The QApplication object is accessible through the global pointer \c + qApp. Its main areas of responsibility are: + \list + + \i It initializes the application with the user's desktop settings + such as palette(), font() and doubleClickInterval(). It keeps track + of these properties in case the user changes the desktop globally, for + example through some kind of control panel. + + \i It performs event handling, meaning that it receives events + from the underlying window system and dispatches them to the relevant + widgets. By using sendEvent() and postEvent() you can send your own + events to widgets. + + \i It parses common command line arguments and sets its internal + state accordingly. See the \link QApplication::QApplication() + constructor documentation\endlink below for more details about this. + + \i It defines the application's look and feel, which is + encapsulated in a QStyle object. This can be changed at runtime + with setStyle(). + + \i It specifies how the application is to allocate colors. + See setColorSpec() for details. + + \i It provides localization of strings that are visible to the user + via translate(). + + \i It provides some magical objects like the desktop() and the + clipboard(). + + \i It knows about the application's windows. You can ask which + widget is at a certain position using widgetAt(), get a list of + topLevelWidgets() and closeAllWindows(), etc. + + \i It manages the application's mouse cursor handling, + see setOverrideCursor() and setGlobalMouseTracking(). + + \i On the X window system, it provides functions to flush and sync + the communication stream, see flushX() and syncX(). + + \i It provides support for sophisticated \link + session.html session management \endlink. This makes it possible + for applications to terminate gracefully when the user logs out, to + cancel a shutdown process if termination isn't possible and even to + preserve the entire application's state for a future session. See + isSessionRestored(), sessionId() and commitData() and saveState() + for details. + + \endlist + + The <a href="simple-application.html">Application walk-through + example</a> contains a typical complete main() that does the usual + things with QApplication. + + Since the QApplication object does so much initialization, it + <b>must</b> be created before any other objects related to the user + interface are created. + + Since it also deals with common command line arguments, it is + usually a good idea to create it \e before any interpretation or + modification of \c argv is done in the application itself. (Note + also that for X11, setMainWidget() may change the main widget + according to the \c -geometry option. To preserve this + functionality, you must set your defaults before setMainWidget() and + any overrides after.) + + \table + \header \i21 Groups of functions + \row + \i System settings + \i + desktopSettingsAware(), + setDesktopSettingsAware(), + cursorFlashTime(), + setCursorFlashTime(), + doubleClickInterval(), + setDoubleClickInterval(), + wheelScrollLines(), + setWheelScrollLines(), + palette(), + setPalette(), + font(), + setFont(), + fontMetrics(). + + \row + \i Event handling + \i + exec(), + processEvents(), + enter_loop(), + exit_loop(), + exit(), + quit(). + sendEvent(), + postEvent(), + sendPostedEvents(), + removePostedEvents(), + hasPendingEvents(), + notify(), + macEventFilter(), + qwsEventFilter(), + x11EventFilter(), + x11ProcessEvent(), + winEventFilter(). + + \row + \i GUI Styles + \i + style(), + setStyle(), + polish(). + + \row + \i Color usage + \i + colorSpec(), + setColorSpec(), + qwsSetCustomColors(). + + \row + \i Text handling + \i + installTranslator(), + removeTranslator() + translate(). + + \row + \i Widgets + \i + mainWidget(), + setMainWidget(), + allWidgets(), + topLevelWidgets(), + desktop(), + activePopupWidget(), + activeModalWidget(), + clipboard(), + focusWidget(), + winFocus(), + activeWindow(), + widgetAt(). + + \row + \i Advanced cursor handling + \i + hasGlobalMouseTracking(), + setGlobalMouseTracking(), + overrideCursor(), + setOverrideCursor(), + restoreOverrideCursor(). + + \row + \i X Window System synchronization + \i + flushX(), + syncX(). + + \row + \i Session management + \i + isSessionRestored(), + sessionId(), + commitData(), + saveState(). + + \row + \i Threading + \i + lock(), unlock(), locked(), tryLock(), + wakeUpGuiThread() + + \row + \i Miscellaneous + \i + closeAllWindows(), + startingUp(), + closingDown(), + type(). + \endtable + + \e {Non-GUI programs:} While Qt is not optimized or + designed for writing non-GUI programs, it's possible to use + \link tools.html some of its classes \endlink without creating a + QApplication. This can be useful if you wish to share code between + a non-GUI server and a GUI client. + + \headerfile qnamespace.h + \headerfile qwindowdefs.h + \headerfile qglobal.h +*/ + +/*! \enum Qt::HANDLE + \internal +*/ + +/*! + \enum QApplication::Type + + \value Tty a console application + \value GuiClient a GUI client application + \value GuiServer a GUI server application +*/ + +/*! + \enum QApplication::ColorSpec + + \value NormalColor the default color allocation policy + \value CustomColor the same as NormalColor for X11; allocates colors + to a palette on demand under Windows + \value ManyColor the right choice for applications that use thousands of + colors + + See setColorSpec() for full details. +*/ + +/* + The qt_init() and qt_cleanup() functions are implemented in the + qapplication_xyz.cpp file. +*/ + +void qt_init( int *, char **, QApplication::Type ); +void qt_cleanup(); +#if defined(Q_WS_X11) +void qt_init( Display* dpy, Qt::HANDLE, Qt::HANDLE ); +#endif +Q_EXPORT bool qt_tryModalHelper( QWidget *widget, QWidget **rettop ); + +QApplication *qApp = 0; // global application object + +QStyle *QApplication::app_style = 0; // default application style +bool qt_explicit_app_style = FALSE; // style explicitly set by programmer + +int QApplication::app_cspec = QApplication::NormalColor; +#ifndef QT_NO_PALETTE +QPalette *QApplication::app_pal = 0; // default application palette +#endif +QFont *QApplication::app_font = 0; // default application font +bool qt_app_has_font = FALSE; +#ifndef QT_NO_CURSOR +QCursor *QApplication::app_cursor = 0; // default application cursor +#endif +int QApplication::app_tracking = 0; // global mouse tracking +bool QApplication::is_app_running = FALSE; // app starting up if FALSE +bool QApplication::is_app_closing = FALSE; // app closing down if TRUE +int QApplication::loop_level = 0; // event loop level +QWidget *QApplication::main_widget = 0; // main application widget +QWidget *QApplication::focus_widget = 0; // has keyboard input focus +QWidget *QApplication::active_window = 0; // toplevel with keyboard focus +bool QApplication::obey_desktop_settings = TRUE; // use winsys resources +int QApplication::cursor_flash_time = 1000; // text caret flash time +int QApplication::mouse_double_click_time = 400; // mouse dbl click limit +#ifndef QT_NO_WHEELEVENT +int QApplication::wheel_scroll_lines = 3; // number of lines to scroll +#endif +bool qt_is_gui_used; +bool Q_EXPORT qt_resolve_symlinks = TRUE; +bool Q_EXPORT qt_tab_all_widgets = TRUE; +QRect qt_maxWindowRect; +static int drag_time = 500; +static int drag_distance = 4; +static bool reverse_layout = FALSE; +QSize QApplication::app_strut = QSize( 0,0 ); // no default application strut +bool QApplication::animate_ui = TRUE; +bool QApplication::animate_menu = FALSE; +bool QApplication::fade_menu = FALSE; +bool QApplication::animate_combo = FALSE; +bool QApplication::animate_tooltip = FALSE; +bool QApplication::fade_tooltip = FALSE; +bool QApplication::animate_toolbox = FALSE; +bool QApplication::widgetCount = FALSE; +QApplication::Type qt_appType=QApplication::Tty; +#ifndef QT_NO_COMPONENT +QStringList *QApplication::app_libpaths = 0; +#endif +bool QApplication::metaComposeUnicode = FALSE; +int QApplication::composedUnicode = 0; + +#ifdef QT_THREAD_SUPPORT +QMutex *QApplication::qt_mutex = 0; +static QMutex *postevent_mutex = 0; +static Qt::HANDLE qt_application_thread_id = 0; +Q_EXPORT Qt::HANDLE qt_get_application_thread_id() +{ + return qt_application_thread_id; +} +#endif // QT_THREAD_SUPPORT + +QEventLoop *QApplication::eventloop = 0; // application event loop + +#ifndef QT_NO_ACCEL +extern bool qt_dispatchAccelEvent( QWidget*, QKeyEvent* ); // def in qaccel.cpp +extern bool qt_tryComposeUnicode( QWidget*, QKeyEvent* ); // def in qaccel.cpp +#endif + +#if defined(QT_TABLET_SUPPORT) +bool chokeMouse = FALSE; +#endif + +void qt_setMaxWindowRect(const QRect& r) +{ + qt_maxWindowRect = r; + // Re-resize any maximized windows + QWidgetList* l = QApplication::topLevelWidgets(); + if ( l ) { + QWidget *w = l->first(); + while ( w ) { + if ( w->isVisible() && w->isMaximized() ) + { + w->showNormal(); //#### flicker + w->showMaximized(); + } + w = l->next(); + } + delete l; + } +} + +typedef void (*VFPTR)(); +typedef QValueList<VFPTR> QVFuncList; +static QVFuncList *postRList = 0; // list of post routines + +/*! + \relates QApplication + + Adds a global routine that will be called from the QApplication + destructor. This function is normally used to add cleanup routines + for program-wide functionality. + + The function given by \a p should take no arguments and return + nothing, like this: + \code + static int *global_ptr = 0; + + static void cleanup_ptr() + { + delete [] global_ptr; + global_ptr = 0; + } + + void init_ptr() + { + global_ptr = new int[100]; // allocate data + qAddPostRoutine( cleanup_ptr ); // delete later + } + \endcode + + Note that for an application- or module-wide cleanup, + qAddPostRoutine() is often not suitable. People have a tendency to + make such modules dynamically loaded, and then unload those modules + long before the QApplication destructor is called, for example. + + For modules and libraries, using a reference-counted initialization + manager or Qt' parent-child delete mechanism may be better. Here is + an example of a private class which uses the parent-child mechanism + to call a cleanup function at the right time: + + \code + class MyPrivateInitStuff: public QObject { + private: + MyPrivateInitStuff( QObject * parent ): QObject( parent) { + // initialization goes here + } + MyPrivateInitStuff * p; + + public: + static MyPrivateInitStuff * initStuff( QObject * parent ) { + if ( !p ) + p = new MyPrivateInitStuff( parent ); + return p; + } + + ~MyPrivateInitStuff() { + // cleanup (the "post routine") goes here + } + } + \endcode + + By selecting the right parent widget/object, this can often be made + to clean up the module's data at the exact right moment. +*/ + +Q_EXPORT void qAddPostRoutine( QtCleanUpFunction p) +{ + if ( !postRList ) { + postRList = new QVFuncList; + Q_CHECK_PTR( postRList ); + } + postRList->prepend( p ); +} + + +Q_EXPORT void qRemovePostRoutine( QtCleanUpFunction p ) +{ + if ( !postRList ) return; + QVFuncList::Iterator it = postRList->begin(); + while ( it != postRList->end() ) { + if ( *it == p ) { + postRList->remove( it ); + it = postRList->begin(); + } else { + ++it; + } + } +} + +// Default application palettes and fonts (per widget type) +QAsciiDict<QPalette> *QApplication::app_palettes = 0; +QAsciiDict<QFont> *QApplication::app_fonts = 0; + +#ifndef QT_NO_SESSIONMANAGER +QString *QApplication::session_key = 0; // ## session key. Should be a member in 4.0 +#endif +QWidgetList *QApplication::popupWidgets = 0; // has keyboard input focus + +QDesktopWidget *qt_desktopWidget = 0; // root window widgets +#ifndef QT_NO_CLIPBOARD +QClipboard *qt_clipboard = 0; // global clipboard object +#endif +QWidgetList * qt_modal_stack=0; // stack of modal widgets + +// Definitions for posted events +struct QPostEvent { + QPostEvent( QObject *r, QEvent *e ): receiver( r ), event( e ) {} + ~QPostEvent() { delete event; } + QObject *receiver; + QEvent *event; +}; + +class Q_EXPORT QPostEventList : public QPtrList<QPostEvent> +{ +public: + QPostEventList() : QPtrList<QPostEvent>() {} + QPostEventList( const QPostEventList &list ) : QPtrList<QPostEvent>(list) {} + ~QPostEventList() { clear(); } + QPostEventList &operator=(const QPostEventList &list) + { return (QPostEventList&)QPtrList<QPostEvent>::operator=(list); } +}; +class Q_EXPORT QPostEventListIt : public QPtrListIterator<QPostEvent> +{ +public: + QPostEventListIt( const QPostEventList &l ) : QPtrListIterator<QPostEvent>(l) {} + QPostEventListIt &operator=(const QPostEventListIt &i) +{ return (QPostEventListIt&)QPtrListIterator<QPostEvent>::operator=(i); } +}; + +static QPostEventList *globalPostedEvents = 0; // list of posted events + +uint qGlobalPostedEventsCount() +{ + if (!globalPostedEvents) + return 0; + return globalPostedEvents->count(); +} + +static QSingleCleanupHandler<QPostEventList> qapp_cleanup_events; + +#ifndef QT_NO_PALETTE +QPalette *qt_std_pal = 0; + +void qt_create_std_palette() +{ + if ( qt_std_pal ) + delete qt_std_pal; + + QColor standardLightGray( 192, 192, 192 ); + QColor light( 255, 255, 255 ); + QColor dark( standardLightGray.dark( 150 ) ); + QColorGroup std_act( Qt::black, standardLightGray, + light, dark, Qt::gray, + Qt::black, Qt::white ); + QColorGroup std_dis( Qt::darkGray, standardLightGray, + light, dark, Qt::gray, + Qt::darkGray, std_act.background() ); + QColorGroup std_inact( Qt::black, standardLightGray, + light, dark, Qt::gray, + Qt::black, Qt::white ); + qt_std_pal = new QPalette( std_act, std_dis, std_inact ); +} + +static void qt_fix_tooltips() +{ + // No resources for this yet (unlike on Windows). + QColorGroup cg( Qt::black, QColor(255,255,220), + QColor(96,96,96), Qt::black, Qt::black, + Qt::black, QColor(255,255,220) ); + QPalette pal( cg, cg, cg ); + QApplication::setPalette( pal, TRUE, "QTipLabel"); +} +#endif + +void QApplication::process_cmdline( int* argcptr, char ** argv ) +{ + // process platform-indep command line + if ( !qt_is_gui_used || !*argcptr) + return; + + int argc = *argcptr; + int i, j; + + j = 1; + for ( i=1; i<argc; i++ ) { + if ( argv[i] && *argv[i] != '-' ) { + argv[j++] = argv[i]; + continue; + } + QCString arg = argv[i]; + QCString s; + if ( arg == "-qdevel" || arg == "-qdebug") { + // obsolete argument + } else if ( arg.find( "-style=", 0, FALSE ) != -1 ) { + s = arg.right( arg.length() - 7 ); + } else if ( qstrcmp(arg,"-style") == 0 && i < argc-1 ) { + s = argv[++i]; + s = s.lower(); +#ifndef QT_NO_SESSIONMANAGER + } else if ( qstrcmp(arg,"-session") == 0 && i < argc-1 ) { + QCString s = argv[++i]; + if ( !s.isEmpty() ) { + session_id = QString::fromLatin1( s ); + int p = session_id.find( '_' ); + if ( p >= 0 ) { + if ( !session_key ) + session_key = new QString; + *session_key = session_id.mid( p +1 ); + session_id = session_id.left( p ); + } + is_session_restored = TRUE; + } +#endif + } else if ( qstrcmp(arg, "-reverse") == 0 ) { + setReverseLayout( TRUE ); + } else if ( qstrcmp(arg, "-widgetcount") == 0 ) { + widgetCount = TRUE;; + } else { + argv[j++] = argv[i]; + } +#ifndef QT_NO_STYLE + if ( !s.isEmpty() ) { + setStyle( s ); + } +#endif + } + + if(j < argc) { +#ifdef Q_WS_MACX + static char* empty = "\0"; + argv[j] = empty; +#else + argv[j] = 0; +#endif + *argcptr = j; + } +} + +/*! + Initializes the window system and constructs an application object + with \a argc command line arguments in \a argv. + + The global \c qApp pointer refers to this application object. Only + one application object should be created. + + This application object must be constructed before any \link + QPaintDevice paint devices\endlink (including widgets, pixmaps, bitmaps + etc.). + + Note that \a argc and \a argv might be changed. Qt removes command + line arguments that it recognizes. The modified \a argc and \a argv + can also be accessed later with \c qApp->argc() and \c qApp->argv(). + The documentation for argv() contains a detailed description of how + to process command line arguments. + + Qt debugging options (not available if Qt was compiled with the + QT_NO_DEBUG flag defined): + \list + \i -nograb, tells Qt that it must never grab the mouse or the keyboard. + \i -dograb (only under X11), running under a debugger can cause + an implicit -nograb, use -dograb to override. + \i -sync (only under X11), switches to synchronous mode for + debugging. + \endlist + + See \link debug.html Debugging Techniques \endlink for a more + detailed explanation. + + All Qt programs automatically support the following command line options: + \list + \i -reverse causes text to be formatted for right-to-left languages + rather than in the usual left-to-right direction. + \i -style= \e style, sets the application GUI style. Possible values + are \c motif, \c windows, and \c platinum. If you compiled Qt + with additional styles or have additional styles as plugins these + will be available to the \c -style command line option. + \i -style \e style, is the same as listed above. + \i -session= \e session, restores the application from an earlier + \link session.html session \endlink. + \i -session \e session, is the same as listed above. + \i -widgetcount, prints debug message at the end about number of widgets left + undestroyed and maximum number of widgets existed at the same time + \endlist + + The X11 version of Qt also supports some traditional X11 + command line options: + \list + \i -display \e display, sets the X display (default is $DISPLAY). + \i -geometry \e geometry, sets the client geometry of the + \link setMainWidget() main widget\endlink. + \i -fn or \c -font \e font, defines the application font. The + font should be specified using an X logical font description. + \i -bg or \c -background \e color, sets the default background color + and an application palette (light and dark shades are calculated). + \i -fg or \c -foreground \e color, sets the default foreground color. + \i -btn or \c -button \e color, sets the default button color. + \i -name \e name, sets the application name. + \i -title \e title, sets the application title (caption). + \i -visual \c TrueColor, forces the application to use a TrueColor visual + on an 8-bit display. + \i -ncols \e count, limits the number of colors allocated in the + color cube on an 8-bit display, if the application is using the + \c QApplication::ManyColor color specification. If \e count is + 216 then a 6x6x6 color cube is used (i.e. 6 levels of red, 6 of green, + and 6 of blue); for other values, a cube + approximately proportional to a 2x3x1 cube is used. + \i -cmap, causes the application to install a private color map + on an 8-bit display. + \endlist + + \sa argc(), argv() +*/ + +//######### BINARY COMPATIBILITY constructor +QApplication::QApplication( int &argc, char **argv ) +{ + construct( argc, argv, GuiClient ); +} + + +/*! + Constructs an application object with \a argc command line arguments + in \a argv. If \a GUIenabled is TRUE, a GUI application is + constructed, otherwise a non-GUI (console) application is created. + + Set \a GUIenabled to FALSE for programs without a graphical user + interface that should be able to run without a window system. + + On X11, the window system is initialized if \a GUIenabled is TRUE. + If \a GUIenabled is FALSE, the application does not connect to the + X-server. + On Windows and Macintosh, currently the window system is always + initialized, regardless of the value of GUIenabled. This may change in + future versions of Qt. + + The following example shows how to create an application that + uses a graphical interface when available. + \code + int main( int argc, char **argv ) + { +#ifdef Q_WS_X11 + bool useGUI = getenv( "DISPLAY" ) != 0; +#else + bool useGUI = TRUE; +#endif + QApplication app(argc, argv, useGUI); + + if ( useGUI ) { + //start GUI version + ... + } else { + //start non-GUI version + ... + } + return app.exec(); + } +\endcode +*/ + +QApplication::QApplication( int &argc, char **argv, bool GUIenabled ) +{ + construct( argc, argv, GUIenabled ? GuiClient : Tty ); +} + +/*! + Constructs an application object with \a argc command line arguments + in \a argv. + + For Qt/Embedded, passing \c QApplication::GuiServer for \a type + makes this application the server (equivalent to running with the + -qws option). +*/ +QApplication::QApplication( int &argc, char **argv, Type type ) +{ + construct( argc, argv, type ); +} + +Q_EXPORT void qt_ucm_initialize( QApplication *theApp ) +{ + if ( qApp ) + return; + int argc = theApp->argc(); + char **argv = theApp->argv(); + theApp->construct( argc, argv, qApp->type() ); + + Q_ASSERT( qApp == theApp ); +} + +void QApplication::construct( int &argc, char **argv, Type type ) +{ + qt_appType = type; + qt_is_gui_used = (type != Tty); + init_precmdline(); + static const char *empty = ""; + if ( argc == 0 || argv == 0 ) { + argc = 0; + argv = (char **)∅ // ouch! careful with QApplication::argv()! + } + app_argc = argc; + app_argv = argv; + + qt_init( &argc, argv, type ); // Must be called before initialize() + process_cmdline( &argc, argv ); + initialize( argc, argv ); + if ( qt_is_gui_used ) + qt_maxWindowRect = desktop()->rect(); + if ( eventloop ) + eventloop->appStartingUp(); +} + +/*! + Returns the type of application, Tty, GuiClient or GuiServer. +*/ + +QApplication::Type QApplication::type() const +{ + return qt_appType; +} + +#if defined(Q_WS_X11) +/*! + Create an application, given an already open display \a dpy. If \a + visual and \a colormap are non-zero, the application will use those as + the default Visual and Colormap contexts. + + \warning Qt only supports TrueColor visuals at depths higher than 8 + bits-per-pixel. + + This is available only on X11. +*/ + +QApplication::QApplication( Display* dpy, HANDLE visual, HANDLE colormap ) +{ + static int aargc = 1; + // ### a string literal is a cont char* + // ### using it as a char* is wrong and could lead to segfaults + // ### if aargv is modified someday + static char *aargv[] = { (char*)"unknown", 0 }; + + app_argc = aargc; + app_argv = aargv; + + qt_appType = GuiClient; + qt_is_gui_used = TRUE; + qt_appType = GuiClient; + init_precmdline(); + // ... no command line. + + if ( ! dpy ) { +#ifdef QT_CHECK_STATE + qWarning( "QApplication: invalid Display* argument." ); +#endif // QT_CHECK_STATE + + qt_init( &aargc, aargv, GuiClient ); + } else { + qt_init( dpy, visual, colormap ); + } + + initialize( aargc, aargv ); + + if ( qt_is_gui_used ) + qt_maxWindowRect = desktop()->rect(); + if ( eventloop ) + eventloop->appStartingUp(); +} + +/*! + Create an application, given an already open display \a dpy and using + \a argc command line arguments in \a argv. If \a + visual and \a colormap are non-zero, the application will use those as + the default Visual and Colormap contexts. + + \warning Qt only supports TrueColor visuals at depths higher than 8 + bits-per-pixel. + + This is available only on X11. + +*/ +QApplication::QApplication(Display *dpy, int argc, char **argv, + HANDLE visual, HANDLE colormap) +{ + qt_appType = GuiClient; + qt_is_gui_used = TRUE; + qt_appType = GuiClient; + init_precmdline(); + + app_argc = argc; + app_argv = argv; + + if ( ! dpy ) { +#ifdef QT_CHECK_STATE + qWarning( "QApplication: invalid Display* argument." ); +#endif // QT_CHECK_STATE + + qt_init( &argc, argv, GuiClient ); + } else { + qt_init(dpy, visual, colormap); + } + + process_cmdline( &argc, argv ); + initialize(argc, argv); + + if ( qt_is_gui_used ) + qt_maxWindowRect = desktop()->rect(); + if ( eventloop ) + eventloop->appStartingUp(); +} + + +#endif // Q_WS_X11 + + +void QApplication::init_precmdline() +{ + translators = 0; + is_app_closing = FALSE; +#ifndef QT_NO_SESSIONMANAGER + is_session_restored = FALSE; +#endif +#if defined(QT_CHECK_STATE) + if ( qApp ) + qWarning( "QApplication: There should be max one application object" ); +#endif + qApp = (QApplication*)this; +} + +/*! + Initializes the QApplication object, called from the constructors. +*/ + +void QApplication::initialize( int argc, char **argv ) +{ +#ifdef QT_THREAD_SUPPORT + qt_mutex = new QMutex( TRUE ); + postevent_mutex = new QMutex( TRUE ); + qt_application_thread_id = QThread::currentThread(); +#endif // QT_THREAD_SUPPORT + + app_argc = argc; + app_argv = argv; + quit_now = FALSE; + quit_code = 0; + QWidget::createMapper(); // create widget mapper +#ifndef QT_NO_PALETTE + (void) palette(); // trigger creation of application palette +#endif + is_app_running = TRUE; // no longer starting up + +#ifndef QT_NO_SESSIONMANAGER + // connect to the session manager + if ( !session_key ) + session_key = new QString; + session_manager = new QSessionManager( qApp, session_id, *session_key ); +#endif + +} + + +/***************************************************************************** + Functions returning the active popup and modal widgets. + *****************************************************************************/ + +/*! + Returns the active popup widget. + + A popup widget is a special top level widget that sets the \c + WType_Popup widget flag, e.g. the QPopupMenu widget. When the + application opens a popup widget, all events are sent to the popup. + Normal widgets and modal widgets cannot be accessed before the popup + widget is closed. + + Only other popup widgets may be opened when a popup widget is shown. + The popup widgets are organized in a stack. This function returns + the active popup widget at the top of the stack. + + \sa activeModalWidget(), topLevelWidgets() +*/ + +QWidget *QApplication::activePopupWidget() +{ + return popupWidgets ? popupWidgets->getLast() : 0; +} + + +/*! + Returns the active modal widget. + + A modal widget is a special top level widget which is a subclass of + QDialog that specifies the modal parameter of the constructor as + TRUE. A modal widget must be closed before the user can continue + with other parts of the program. + + Modal widgets are organized in a stack. This function returns + the active modal widget at the top of the stack. + + \sa activePopupWidget(), topLevelWidgets() +*/ + +QWidget *QApplication::activeModalWidget() +{ + return qt_modal_stack ? qt_modal_stack->getFirst() : 0; +} + +/*! + Cleans up any window system resources that were allocated by this + application. Sets the global variable \c qApp to 0. +*/ + +QApplication::~QApplication() +{ +#ifndef QT_NO_CLIPBOARD + // flush clipboard contents + if ( qt_clipboard ) { + QCustomEvent event( QEvent::Clipboard ); + QApplication::sendEvent( qt_clipboard, &event ); + } +#endif + + if ( eventloop ) + eventloop->appClosingDown(); + if ( postRList ) { + QVFuncList::Iterator it = postRList->begin(); + while ( it != postRList->end() ) { // call post routines + (**it)(); + postRList->remove( it ); + it = postRList->begin(); + } + delete postRList; + postRList = 0; + } + + QObject *tipmanager = child( "toolTipManager", "QTipManager", FALSE ); + delete tipmanager; + + delete qt_desktopWidget; + qt_desktopWidget = 0; + is_app_closing = TRUE; + +#ifndef QT_NO_CLIPBOARD + delete qt_clipboard; + qt_clipboard = 0; +#endif + QWidget::destroyMapper(); +#ifndef QT_NO_PALETTE + delete qt_std_pal; + qt_std_pal = 0; + delete app_pal; + app_pal = 0; + delete app_palettes; + app_palettes = 0; +#endif + delete app_font; + app_font = 0; + delete app_fonts; + app_fonts = 0; +#ifndef QT_NO_STYLE + delete app_style; + app_style = 0; +#endif +#ifndef QT_NO_CURSOR + delete app_cursor; + app_cursor = 0; +#endif +#ifndef QT_NO_TRANSLATION + delete translators; +#endif + +#ifndef QT_NO_DRAGANDDROP + extern QDragManager *qt_dnd_manager; + delete qt_dnd_manager; +#endif + + qt_cleanup(); + +#ifndef QT_NO_COMPONENT + delete app_libpaths; + app_libpaths = 0; +#endif + +#ifdef QT_THREAD_SUPPORT + delete qt_mutex; + qt_mutex = 0; + delete postevent_mutex; + postevent_mutex = 0; +#endif // QT_THREAD_SUPPORT + + if( qApp == this ) { + if ( postedEvents ) + removePostedEvents( this ); + qApp = 0; + } + is_app_running = FALSE; + + if ( widgetCount ) { + qDebug( "Widgets left: %i Max widgets: %i \n", QWidget::instanceCounter, QWidget::maxInstances ); + } +#ifndef QT_NO_SESSIONMANAGER + delete session_manager; + session_manager = 0; + delete session_key; + session_key = 0; +#endif //QT_NO_SESSIONMANAGER + + qt_explicit_app_style = FALSE; + qt_app_has_font = FALSE; + app_tracking = 0; + obey_desktop_settings = TRUE; + cursor_flash_time = 1000; + mouse_double_click_time = 400; +#ifndef QT_NO_WHEELEVENT + wheel_scroll_lines = 3; +#endif + drag_time = 500; + drag_distance = 4; + reverse_layout = FALSE; + app_strut = QSize( 0, 0 ); + animate_ui = TRUE; + animate_menu = FALSE; + fade_menu = FALSE; + animate_combo = FALSE; + animate_tooltip = FALSE; + fade_tooltip = FALSE; + widgetCount = FALSE; +} + + +/*! + \fn int QApplication::argc() const + + Returns the number of command line arguments. + + The documentation for argv() describes how to process command line + arguments. + + \sa argv(), QApplication::QApplication() +*/ + +/*! + \fn char **QApplication::argv() const + + Returns the command line argument vector. + + \c argv()[0] is the program name, \c argv()[1] is the first + argument and \c argv()[argc()-1] is the last argument. + + A QApplication object is constructed by passing \e argc and \e + argv from the \c main() function. Some of the arguments may be + recognized as Qt options and removed from the argument vector. For + example, the X11 version of Qt knows about \c -display, \c -font + and a few more options. + + Example: + \code + // showargs.cpp - displays program arguments in a list box + + #include <qapplication.h> + #include <qlistbox.h> + + int main( int argc, char **argv ) + { + QApplication a( argc, argv ); + QListBox b; + a.setMainWidget( &b ); + for ( int i = 0; i < a.argc(); i++ ) // a.argc() == argc + b.insertItem( a.argv()[i] ); // a.argv()[i] == argv[i] + b.show(); + return a.exec(); + } + \endcode + + If you run \c{showargs -display unix:0 -font 9x15bold hello world} + under X11, the list box contains the three strings "showargs", + "hello" and "world". + + Qt provides a global pointer, \c qApp, that points to the + QApplication object, and through which you can access argc() and + argv() in functions other than main(). + + \sa argc(), QApplication::QApplication() +*/ + +/*! + \fn void QApplication::setArgs( int argc, char **argv ) + \internal +*/ + + +#ifndef QT_NO_STYLE + +static QString *qt_style_override = 0; + +/*! + Returns the application's style object. + + \sa setStyle(), QStyle +*/ +QStyle& QApplication::style() +{ +#ifndef QT_NO_STYLE + if ( app_style ) + return *app_style; + if ( !qt_is_gui_used ) + qFatal( "No style available in non-gui applications!" ); + +#if defined(Q_WS_X11) + if(!qt_style_override) + x11_initialize_style(); // run-time search for default style +#endif + if ( !app_style ) { + // Compile-time search for default style + // + QString style; + if ( qt_style_override ) { + style = *qt_style_override; + delete qt_style_override; + qt_style_override = 0; + } else { +# if defined(Q_WS_WIN) && defined(Q_OS_TEMP) + style = "PocketPC"; +#elif defined(Q_WS_WIN) + if ( qWinVersion() >= Qt::WV_XP && qWinVersion() < Qt::WV_NT_based ) + style = "WindowsXP"; + else + style = "Windows"; // default styles for Windows +#elif defined(Q_WS_X11) && defined(Q_OS_SOLARIS) + style = "CDE"; // default style for X11 on Solaris +#elif defined(Q_WS_X11) && defined(Q_OS_IRIX) + style = "SGI"; // default style for X11 on IRIX +#elif defined(Q_WS_X11) + style = "Motif"; // default style for X11 +#elif defined(Q_WS_MAC) + style = "Macintosh"; // default style for all Mac's +#elif defined(Q_WS_QWS) + style = "Compact"; // default style for small devices +#endif + } + app_style = QStyleFactory::create( style ); + if ( !app_style && // platform default style not available, try alternatives + !(app_style = QStyleFactory::create( "Windows" ) ) && + !(app_style = QStyleFactory::create( "Platinum" ) ) && + !(app_style = QStyleFactory::create( "MotifPlus" ) ) && + !(app_style = QStyleFactory::create( "Motif" ) ) && + !(app_style = QStyleFactory::create( "CDE" ) ) && + !(app_style = QStyleFactory::create( "Aqua" ) ) && + !(app_style = QStyleFactory::create( "SGI" ) ) && + !(app_style = QStyleFactory::create( "Compact" ) ) +#ifndef QT_NO_STRINGLIST + && !(app_style = QStyleFactory::create( QStyleFactory::keys()[0] ) ) +#endif + ) + qFatal( "No %s style available!", style.latin1() ); + } + + QPalette app_pal_copy ( *app_pal ); + app_style->polish( *app_pal ); + + if ( is_app_running && !is_app_closing && (*app_pal != app_pal_copy) ) { + QEvent e( QEvent::ApplicationPaletteChange ); + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + sendEvent( w, &e ); + } + } + + app_style->polish( qApp ); +#endif + return *app_style; +} + +/*! + Sets the application's GUI style to \a style. Ownership of the style + object is transferred to QApplication, so QApplication will delete + the style object on application exit or when a new style is set. + + Example usage: + \code + QApplication::setStyle( new QWindowsStyle ); + \endcode + + When switching application styles, the color palette is set back to + the initial colors or the system defaults. This is necessary since + certain styles have to adapt the color palette to be fully + style-guide compliant. + + \sa style(), QStyle, setPalette(), desktopSettingsAware() +*/ +void QApplication::setStyle( QStyle *style ) +{ + QStyle* old = app_style; + app_style = style; +#ifdef Q_WS_X11 + qt_explicit_app_style = TRUE; +#endif // Q_WS_X11 + + if ( startingUp() ) { + delete old; + return; + } + + // clean up the old style + if (old) { + if ( is_app_running && !is_app_closing ) { + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( !w->testWFlags(WType_Desktop) && // except desktop + w->testWState(WState_Polished) ) { // has been polished + old->unPolish(w); + } + } + } + old->unPolish( qApp ); + } + + // take care of possible palette requirements of certain gui + // styles. Do it before polishing the application since the style + // might call QApplication::setStyle() itself + if ( !qt_std_pal ) + qt_create_std_palette(); + QPalette tmpPal = *qt_std_pal; + setPalette( tmpPal, TRUE ); + + // initialize the application with the new style + app_style->polish( qApp ); + + // re-polish existing widgets if necessary + if (old) { + if ( is_app_running && !is_app_closing ) { + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( !w->testWFlags(WType_Desktop) ) { // except desktop + if ( w->testWState(WState_Polished) ) + app_style->polish(w); // repolish + w->styleChange( *old ); + if ( w->isVisible() ){ + w->update(); + } + } + } + } + delete old; + } +} + +/*! + \overload + + Requests a QStyle object for \a style from the QStyleFactory. + + The string must be one of the QStyleFactory::keys(), typically one + of "windows", "motif", "cde", "motifplus", "platinum", "sgi" and + "compact". Depending on the platform, "windowsxp", "aqua" or + "macintosh" may be available. + + A later call to the QApplication constructor will override the + requested style when a "-style" option is passed in as a commandline + parameter. + + Returns 0 if an unknown \a style is passed, otherwise the QStyle object + returned is set as the application's GUI style. +*/ +QStyle* QApplication::setStyle( const QString& style ) +{ +#ifdef Q_WS_X11 + qt_explicit_app_style = TRUE; +#endif // Q_WS_X11 + + if ( startingUp() ) { + if(qt_style_override) + *qt_style_override = style; + else + qt_style_override = new QString(style); + return 0; + } + QStyle *s = QStyleFactory::create( style ); + if ( !s ) + return 0; + + setStyle( s ); + return s; +} + +#endif + + +#if 1 /* OBSOLETE */ + +QApplication::ColorMode QApplication::colorMode() +{ + return (QApplication::ColorMode)app_cspec; +} + +void QApplication::setColorMode( QApplication::ColorMode mode ) +{ + app_cspec = mode; +} +#endif + + +/*! + Returns the color specification. + \sa QApplication::setColorSpec() + */ + +int QApplication::colorSpec() +{ + return app_cspec; +} + +/*! + Sets the color specification for the application to \a spec. + + The color specification controls how the application allocates colors + when run on a display with a limited amount of colors, e.g. 8 bit / 256 + color displays. + + The color specification must be set before you create the QApplication + object. + + The options are: + \list + \i QApplication::NormalColor. + This is the default color allocation strategy. Use this option if + your application uses buttons, menus, texts and pixmaps with few + colors. With this option, the application uses system global + colors. This works fine for most applications under X11, but on + Windows machines it may cause dithering of non-standard colors. + \i QApplication::CustomColor. + Use this option if your application needs a small number of custom + colors. On X11, this option is the same as NormalColor. On Windows, Qt + creates a Windows palette, and allocates colors to it on demand. + \i QApplication::ManyColor. + Use this option if your application is very color hungry + (e.g. it requires thousands of colors). + Under X11 the effect is: + \list + \i For 256-color displays which have at best a 256 color true color + visual, the default visual is used, and colors are allocated + from a color cube. The color cube is the 6x6x6 (216 color) "Web + palette"<sup>*</sup>, but the number of colors can be changed + by the \e -ncols option. The user can force the application to + use the true color visual with the \link + QApplication::QApplication() -visual \endlink option. + \i For 256-color displays which have a true color visual with more + than 256 colors, use that visual. Silicon Graphics X servers + have this feature, for example. They provide an 8 bit visual + by default but can deliver true color when asked. + \endlist + On Windows, Qt creates a Windows palette, and fills it with a color cube. + \endlist + + Be aware that the CustomColor and ManyColor choices may lead to colormap + flashing: The foreground application gets (most) of the available + colors, while the background windows will look less attractive. + + Example: + \code + int main( int argc, char **argv ) + { + QApplication::setColorSpec( QApplication::ManyColor ); + QApplication a( argc, argv ); + ... + } + \endcode + + QColor provides more functionality for controlling color allocation and + freeing up certain colors. See QColor::enterAllocContext() for more + information. + + To check what mode you end up with, call QColor::numBitPlanes() once + the QApplication object exists. A value greater than 8 (typically + 16, 24 or 32) means true color. + + <sup>*</sup> The color cube used by Qt has 216 colors whose red, + green, and blue components always have one of the following values: + 0x00, 0x33, 0x66, 0x99, 0xCC, or 0xFF. + + \sa colorSpec(), QColor::numBitPlanes(), QColor::enterAllocContext() */ + +void QApplication::setColorSpec( int spec ) +{ +#if defined(QT_CHECK_STATE) + if ( qApp ) { + qWarning( "QApplication::setColorSpec: This function must be " + "called before the QApplication object is created" ); + } +#endif + app_cspec = spec; +} + +/*! + \fn QSize QApplication::globalStrut() + + Returns the application's global strut. + + The strut is a size object whose dimensions are the minimum that any + GUI element that the user can interact with should have. For example + no button should be resized to be smaller than the global strut size. + + \sa setGlobalStrut() +*/ + +/*! + Sets the application's global strut to \a strut. + + The strut is a size object whose dimensions are the minimum that any + GUI element that the user can interact with should have. For example + no button should be resized to be smaller than the global strut size. + + The strut size should be considered when reimplementing GUI controls + that may be used on touch-screens or similar IO-devices. + + Example: + \code + QSize& WidgetClass::sizeHint() const + { + return QSize( 80, 25 ).expandedTo( QApplication::globalStrut() ); + } + \endcode + + \sa globalStrut() +*/ + +void QApplication::setGlobalStrut( const QSize& strut ) +{ + app_strut = strut; +} + +#if defined( Q_WS_WIN ) || defined( Q_WS_MAC ) +extern const char *qAppFileName(); +#endif + +#ifndef QT_NO_DIR +#ifndef Q_WS_WIN +static QString resolveSymlinks( const QString& path, int depth = 0 ) +{ + bool foundLink = FALSE; + QString linkTarget; + QString part = path; + int slashPos = path.length(); + + // too deep; we give up + if ( depth == 128 ) + return QString::null; + + do { + part = part.left( slashPos ); + QFileInfo fileInfo( part ); + if ( fileInfo.isSymLink() ) { + foundLink = TRUE; + linkTarget = fileInfo.readLink(); + break; + } + } while ( (slashPos = part.findRev('/')) != -1 ); + + if ( foundLink ) { + QString path2; + if ( linkTarget[0] == '/' ) { + path2 = linkTarget; + if ( slashPos < (int) path.length() ) + path2 += "/" + path.right( path.length() - slashPos - 1 ); + } else { + QString relPath; + relPath = part.left( part.findRev('/') + 1 ) + linkTarget; + if ( slashPos < (int) path.length() ) { + if ( !linkTarget.endsWith( "/" ) ) + relPath += "/"; + relPath += path.right( path.length() - slashPos - 1 ); + } + path2 = QDir::current().absFilePath( relPath ); + } + path2 = QDir::cleanDirPath( path2 ); + return resolveSymlinks( path2, depth + 1 ); + } else { + return path; + } +} +#endif // Q_WS_WIN + +/*! + Returns the directory that contains the application executable. + + For example, if you have installed Qt in the \c{C:\Trolltech\Qt} + directory, and you run the \c{demo} example, this function will + return "C:/Trolltech/Qt/examples/demo". + + On Mac OS X this will point to the directory actually containing the + executable, which may be inside of an application bundle (if the + application is bundled). + + \warning On Unix, this function assumes that argv[0] contains the file + name of the executable (which it normally does). It also assumes that + the current directory hasn't been changed by the application. + + \sa applicationFilePath() +*/ +QString QApplication::applicationDirPath() +{ + return QFileInfo( applicationFilePath() ).dirPath(); +} + +/*! + Returns the file path of the application executable. + + For example, if you have installed Qt in the \c{C:\Trolltech\Qt} + directory, and you run the \c{demo} example, this function will + return "C:/Trolltech/Qt/examples/demo/demo.exe". + + \warning On Unix, this function assumes that argv[0] contains the file + name of the executable (which it normally does). It also assumes that + the current directory hasn't been changed by the application. + + \sa applicationDirPath() +*/ +QString QApplication::applicationFilePath() +{ +#if defined( Q_WS_WIN ) + QFileInfo filePath; + QT_WA({ + WCHAR module_name[256]; + GetModuleFileNameW(0, module_name, sizeof(module_name)); + filePath = QString::fromUcs2((const unsigned short *)module_name); + }, { + char module_name[256]; + GetModuleFileNameA(0, module_name, sizeof(module_name)); + filePath = QString::fromLocal8Bit(module_name); + }); + + return filePath.filePath(); +#elif defined( Q_WS_MAC ) + return QDir::cleanDirPath( QFile::decodeName( qAppFileName() ) ); +#else + QString argv0 = QFile::decodeName( argv()[0] ); + QString absPath; + + if ( argv0[0] == '/' ) { + /* + If argv0 starts with a slash, it is already an absolute + file path. + */ + absPath = argv0; + } else if ( argv0.find('/') != -1 ) { + /* + If argv0 contains one or more slashes, it is a file path + relative to the current directory. + */ + absPath = QDir::current().absFilePath( argv0 ); + } else { + /* + Otherwise, the file path has to be determined using the + PATH environment variable. + */ + char *pEnv = getenv( "PATH" ); + QStringList paths( QStringList::split(QChar(':'), pEnv) ); + QStringList::const_iterator p = paths.begin(); + while ( p != paths.end() ) { + QString candidate = QDir::current().absFilePath( *p + "/" + argv0 ); + if ( QFile::exists(candidate) ) { + absPath = candidate; + break; + } + ++p; + } + } + + absPath = QDir::cleanDirPath( absPath ); + if ( QFile::exists(absPath) ) { + return resolveSymlinks( absPath ); + } else { + return QString::null; + } +#endif +} +#endif // QT_NO_DIR + +#ifndef QT_NO_COMPONENT + +/*! + Returns a list of paths that the application will search when + dynamically loading libraries. + The installation directory for plugins is the only entry if no + paths have been set. The default installation directory for plugins + is \c INSTALL/plugins, where \c INSTALL is the directory where Qt was + installed. The directory of the application executable (NOT the + working directory) is also added to the plugin paths. + + If you want to iterate over the list, you should iterate over a + copy, e.g. + \code + QStringList list = app.libraryPaths(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + See the \link plugins-howto.html plugins documentation\endlink for a + description of how the library paths are used. + + \sa setLibraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary +*/ +QStringList QApplication::libraryPaths() +{ + if ( !app_libpaths ) { + app_libpaths = new QStringList; + QString installPathPlugins = QString::fromLocal8Bit(qInstallPathPlugins()); + if ( QFile::exists(installPathPlugins) ) { +#ifdef Q_WS_WIN + installPathPlugins.replace('\\', '/'); +#endif + app_libpaths->append(installPathPlugins); + } + + QString app_location; + if (qApp) + app_location = qApp->applicationFilePath(); +#ifdef Q_WS_WIN + else { + app_location = QString(qAppFileName()); + app_location.replace('\\', '/'); + } +#endif + if (!app_location.isEmpty()) { + app_location.truncate( app_location.findRev( '/' ) ); + if ( app_location != qInstallPathPlugins() && QFile::exists( app_location ) ) + app_libpaths->append( app_location ); + } + } + return *app_libpaths; +} + + +/*! + Sets the list of directories to search when loading libraries to \a paths. + All existing paths will be deleted and the path list will consist of the + paths given in \a paths. + + \sa libraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary + */ +void QApplication::setLibraryPaths( const QStringList &paths ) +{ + delete app_libpaths; + app_libpaths = new QStringList( paths ); +} + +/*! + Append \a path to the end of the library path list. If \a path is + empty or already in the path list, the path list is not changed. + + The default path list consists of a single entry, the installation + directory for plugins. The default installation directory for plugins + is \c INSTALL/plugins, where \c INSTALL is the directory where Qt was + installed. + + \sa removeLibraryPath(), libraryPaths(), setLibraryPaths() + */ +void QApplication::addLibraryPath( const QString &path ) +{ + if ( path.isEmpty() ) + return; + + // make sure that library paths is initialized + libraryPaths(); + + if ( !app_libpaths->contains( path ) ) + app_libpaths->prepend( path ); +} + +/*! + Removes \a path from the library path list. If \a path is empty or not + in the path list, the list is not changed. + + \sa addLibraryPath(), libraryPaths(), setLibraryPaths() +*/ +void QApplication::removeLibraryPath( const QString &path ) +{ + if ( path.isEmpty() ) + return; + + // make sure that library paths is initialized + libraryPaths(); + + if ( app_libpaths->contains( path ) ) + app_libpaths->remove( path ); +} +#endif //QT_NO_COMPONENT + +/*! + Returns the application palette. + + If a widget is passed in \a w, the default palette for the + widget's class is returned. This may or may not be the application + palette. In most cases there isn't a special palette for certain + types of widgets, but one notable exception is the popup menu under + Windows, if the user has defined a special background color for + menus in the display settings. + + \sa setPalette(), QWidget::palette() +*/ +#ifndef QT_NO_PALETTE +QPalette QApplication::palette(const QWidget* w) +{ +#if defined(QT_CHECK_STATE) + if ( !qApp ) + qWarning( "QApplication::palette: This function can only be " + "called after the QApplication object has been created" ); +#endif + if ( !app_pal ) { + if ( !qt_std_pal ) + qt_create_std_palette(); + app_pal = new QPalette( *qt_std_pal ); + qt_fix_tooltips(); + } + + if ( w && app_palettes ) { + QPalette* wp = app_palettes->find( w->className() ); + if ( wp ) + return *wp; + QAsciiDictIterator<QPalette> it( *app_palettes ); + const char* name; + while ( (name=it.currentKey()) != 0 ) { + if ( w->inherits( name ) ) + return *it.current(); + ++it; + } + } + return *app_pal; +} + +/*! + Changes the default application palette to \a palette. If \a + informWidgets is TRUE, then existing widgets are informed about the + change and may adjust themselves to the new application + setting. If \a informWidgets is FALSE, the change only affects newly + created widgets. + + If \a className is passed, the change applies only to widgets that + inherit \a className (as reported by QObject::inherits()). If + \a className is left 0, the change affects all widgets, thus overriding + any previously set class specific palettes. + + The palette may be changed according to the current GUI style in + QStyle::polish(). + + \sa QWidget::setPalette(), palette(), QStyle::polish() +*/ + +void QApplication::setPalette( const QPalette &palette, bool informWidgets, + const char* className ) +{ + QPalette pal = palette; + QPalette *oldpal = 0; +#ifndef QT_NO_STYLE + if ( !startingUp() ) // on startup this has been done already + qApp->style().polish( pal ); // NB: non-const reference +#endif + bool all = FALSE; + if ( !className ) { + if ( !app_pal ) { + app_pal = new QPalette( pal ); + Q_CHECK_PTR( app_pal ); + } else { + *app_pal = pal; + } + all = app_palettes != 0; + delete app_palettes; + app_palettes = 0; + qt_fix_tooltips(); + } else { + if ( !app_palettes ) { + app_palettes = new QAsciiDict<QPalette>; + Q_CHECK_PTR( app_palettes ); + app_palettes->setAutoDelete( TRUE ); + } + oldpal = app_palettes->find( className ); + app_palettes->insert( className, new QPalette( pal ) ); + } + if ( informWidgets && is_app_running && !is_app_closing ) { + if ( !oldpal || ( *oldpal != pal ) ) { + QEvent e( QEvent::ApplicationPaletteChange ); + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( all || (!className && w->isTopLevel() ) || w->inherits(className) ) // matching class + sendEvent( w, &e ); + } + } + } +} + +#endif // QT_NO_PALETTE + +/*! + Returns the default font for the widget \a w, or the default + application font if \a w is 0. + + \sa setFont(), fontMetrics(), QWidget::font() +*/ + +QFont QApplication::font( const QWidget *w ) +{ + if ( w && app_fonts ) { + QFont* wf = app_fonts->find( w->className() ); + if ( wf ) + return *wf; + QAsciiDictIterator<QFont> it( *app_fonts ); + const char* name; + while ( (name=it.currentKey()) != 0 ) { + if ( w->inherits( name ) ) + return *it.current(); + ++it; + } + } + if ( !app_font ) { + app_font = new QFont( "Helvetica" ); + Q_CHECK_PTR( app_font ); + } + return *app_font; +} + +/*! Changes the default application font to \a font. If \a + informWidgets is TRUE, then existing widgets are informed about the + change and may adjust themselves to the new application + setting. If \a informWidgets is FALSE, the change only affects newly + created widgets. If \a className is passed, the change applies only + to classes that inherit \a className (as reported by + QObject::inherits()). + + On application start-up, the default font depends on the window + system. It can vary depending on both the window system version and + the locale. This function lets you override the default font; but + overriding may be a bad idea because, for example, some locales need + extra-large fonts to support their special characters. + + \sa font(), fontMetrics(), QWidget::setFont() +*/ + +void QApplication::setFont( const QFont &font, bool informWidgets, + const char* className ) +{ + bool all = FALSE; + if ( !className ) { + qt_app_has_font = TRUE; + if ( !app_font ) { + app_font = new QFont( font ); + Q_CHECK_PTR( app_font ); + } else { + *app_font = font; + } + + // make sure the application font is complete + app_font->detach(); + app_font->d->mask = QFontPrivate::Complete; + + all = app_fonts != 0; + delete app_fonts; + app_fonts = 0; + } else { + if (!app_fonts){ + app_fonts = new QAsciiDict<QFont>; + Q_CHECK_PTR( app_fonts ); + app_fonts->setAutoDelete( TRUE ); + } + QFont* fnt = new QFont(font); + Q_CHECK_PTR( fnt ); + app_fonts->insert(className, fnt); + } + if ( informWidgets && is_app_running && !is_app_closing ) { + QEvent e( QEvent::ApplicationFontChange ); + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( all || (!className && w->isTopLevel() ) || w->inherits(className) ) // matching class + sendEvent( w, &e ); + } + } +} + + +/*! + Initialization of the appearance of the widget \a w \e before it is first + shown. + + Usually widgets call this automatically when they are polished. It + may be used to do some style-based central customization of widgets. + + Note that you are not limited to the public functions of QWidget. + Instead, based on meta information like QObject::className() you are + able to customize any kind of widget. + + \sa QStyle::polish(), QWidget::polish(), setPalette(), setFont() +*/ + +void QApplication::polish( QWidget *w ) +{ +#ifndef QT_NO_STYLE + w->style().polish( w ); +#endif +} + + +/*! + Returns a list of the top level widgets in the application. + + The list is created using \c new and must be deleted by the caller. + + The list is empty (QPtrList::isEmpty()) if there are no top level + widgets. + + Note that some of the top level widgets may be hidden, for example + the tooltip if no tooltip is currently shown. + + Example: + \code + // Show all hidden top level widgets. + QWidgetList *list = QApplication::topLevelWidgets(); + QWidgetListIt it( *list ); // iterate over the widgets + QWidget * w; + while ( (w=it.current()) != 0 ) { // for each top level widget... + ++it; + if ( !w->isVisible() ) + w->show(); + } + delete list; // delete the list, not the widgets + \endcode + + \warning Delete the list as soon you have finished using it. + The widgets in the list may be deleted by someone else at any time. + + \sa allWidgets(), QWidget::isTopLevel(), QWidget::isVisible(), + QPtrList::isEmpty() +*/ + +QWidgetList *QApplication::topLevelWidgets() +{ + return QWidget::tlwList(); +} + +/*! + Returns a list of all the widgets in the application. + + The list is created using \c new and must be deleted by the caller. + + The list is empty (QPtrList::isEmpty()) if there are no widgets. + + Note that some of the widgets may be hidden. + + Example that updates all widgets: + \code + QWidgetList *list = QApplication::allWidgets(); + QWidgetListIt it( *list ); // iterate over the widgets + QWidget * w; + while ( (w=it.current()) != 0 ) { // for each widget... + ++it; + w->update(); + } + delete list; // delete the list, not the widgets + \endcode + + The QWidgetList class is defined in the \c qwidgetlist.h header + file. + + \warning Delete the list as soon as you have finished using it. + The widgets in the list may be deleted by someone else at any time. + + \sa topLevelWidgets(), QWidget::isVisible(), QPtrList::isEmpty(), +*/ + +QWidgetList *QApplication::allWidgets() +{ + return QWidget::wList(); +} + +/*! + \fn QWidget *QApplication::focusWidget() const + + Returns the application widget that has the keyboard input focus, or + 0 if no widget in this application has the focus. + + \sa QWidget::setFocus(), QWidget::hasFocus(), activeWindow() +*/ + +/*! + \fn QWidget *QApplication::activeWindow() const + + Returns the application top-level window that has the keyboard input + focus, or 0 if no application window has the focus. Note that + there might be an activeWindow() even if there is no focusWidget(), + for example if no widget in that window accepts key events. + + \sa QWidget::setFocus(), QWidget::hasFocus(), focusWidget() +*/ + +/*! + Returns display (screen) font metrics for the application font. + + \sa font(), setFont(), QWidget::fontMetrics(), QPainter::fontMetrics() +*/ + +QFontMetrics QApplication::fontMetrics() +{ + return desktop()->fontMetrics(); +} + + + +/*! + Tells the application to exit with return code 0 (success). + Equivalent to calling QApplication::exit( 0 ). + + It's common to connect the lastWindowClosed() signal to quit(), and + you also often connect e.g. QButton::clicked() or signals in + QAction, QPopupMenu or QMenuBar to it. + + Example: + \code + QPushButton *quitButton = new QPushButton( "Quit" ); + connect( quitButton, SIGNAL(clicked()), qApp, SLOT(quit()) ); + \endcode + + \sa exit() aboutToQuit() lastWindowClosed() QAction +*/ + +void QApplication::quit() +{ + QApplication::exit( 0 ); +} + + +/*! + Closes all top-level windows. + + This function is particularly useful for applications with many + top-level windows. It could, for example, be connected to a "Quit" + entry in the file menu as shown in the following code example: + + \code + // the "Quit" menu entry should try to close all windows + QPopupMenu* file = new QPopupMenu( this ); + file->insertItem( "&Quit", qApp, SLOT(closeAllWindows()), CTRL+Key_Q ); + + // when the last window is closed, the application should quit + connect( qApp, SIGNAL( lastWindowClosed() ), qApp, SLOT( quit() ) ); + \endcode + + The windows are closed in random order, until one window does not + accept the close event. + + \sa QWidget::close(), QWidget::closeEvent(), lastWindowClosed(), + quit(), topLevelWidgets(), QWidget::isTopLevel() + + */ +void QApplication::closeAllWindows() +{ + bool did_close = TRUE; + QWidget *w; + while((w = activeModalWidget()) && did_close) { + if(w->isHidden()) + break; + did_close = w->close(); + } + QWidgetList *list = QApplication::topLevelWidgets(); + for ( w = list->first(); did_close && w; ) { + if ( !w->isHidden() ) { + did_close = w->close(); + delete list; + list = QApplication::topLevelWidgets(); + w = list->first(); + } else { + w = list->next(); + } + } + delete list; +} + +/*! + Displays a simple message box about Qt. The message includes the + version number of Qt being used by the application. + + This is useful for inclusion in the Help menu of an application. + See the examples/menu/menu.cpp example. + + This function is a convenience slot for QMessageBox::aboutQt(). +*/ +void QApplication::aboutQt() +{ +#ifndef QT_NO_MESSAGEBOX + QMessageBox::aboutQt( mainWidget() ); +#endif // QT_NO_MESSAGEBOX +} + + +/*! + \fn void QApplication::lastWindowClosed() + + This signal is emitted when the user has closed the last + top level window. + + The signal is very useful when your application has many top level + widgets but no main widget. You can then connect it to the quit() + slot. + + For convenience, this signal is \e not emitted for transient top level + widgets such as popup menus and dialogs. + + \sa mainWidget(), topLevelWidgets(), QWidget::isTopLevel(), QWidget::close() +*/ + +/*! + \fn void QApplication::aboutToQuit() + + This signal is emitted when the application is about to quit the + main event loop, e.g. when the event loop level drops to zero. + This may happen either after a call to quit() from inside the + application or when the users shuts down the entire desktop session. + + The signal is particularly useful if your application has to do some + last-second cleanup. Note that no user interaction is possible in + this state. + + \sa quit() +*/ + + +/*! + \fn void QApplication::guiThreadAwake() + + This signal is emitted after the event loop returns from a function + that could block. + + \sa wakeUpGuiThread() +*/ + + +/*! + \fn bool QApplication::sendEvent( QObject *receiver, QEvent *event ) + + Sends event \a event directly to receiver \a receiver, using the + notify() function. Returns the value that was returned from the event + handler. + + The event is \e not deleted when the event has been sent. The normal + approach is to create the event on the stack, e.g. + \code + QMouseEvent me( QEvent::MouseButtonPress, pos, 0, 0 ); + QApplication::sendEvent( mainWindow, &me ); + \endcode + If you create the event on the heap you must delete it. + + \sa postEvent(), notify() +*/ + +/*! + Sends event \a e to \a receiver: \a {receiver}->event(\a e). + Returns the value that is returned from the receiver's event handler. + + For certain types of events (e.g. mouse and key events), + the event will be propagated to the receiver's parent and so on up to + the top-level object if the receiver is not interested in the event + (i.e., it returns FALSE). + + There are five different ways that events can be processed; + reimplementing this virtual function is just one of them. All five + approaches are listed below: + \list 1 + \i Reimplementing this function. This is very powerful, providing + complete control; but only one subclass can be qApp. + + \i Installing an event filter on qApp. Such an event filter is able + to process all events for all widgets, so it's just as powerful as + reimplementing notify(); furthermore, it's possible to have more + than one application-global event filter. Global event filters even + see mouse events for \link QWidget::isEnabled() disabled + widgets, \endlink and if \link setGlobalMouseTracking() global mouse + tracking \endlink is enabled, as well as mouse move events for all + widgets. + + \i Reimplementing QObject::event() (as QWidget does). If you do + this you get Tab key presses, and you get to see the events before + any widget-specific event filters. + + \i Installing an event filter on the object. Such an event filter + gets all the events except Tab and Shift-Tab key presses. + + \i Reimplementing paintEvent(), mousePressEvent() and so + on. This is the commonest, easiest and least powerful way. + \endlist + + \sa QObject::event(), installEventFilter() +*/ + +bool QApplication::notify( QObject *receiver, QEvent *e ) +{ + // no events are delivered after ~QApplication() has started + if ( is_app_closing ) + return FALSE; + + if ( receiver == 0 ) { // serious error +#if defined(QT_CHECK_NULL) + qWarning( "QApplication::notify: Unexpected null receiver" ); +#endif + return FALSE; + } + + if ( e->type() == QEvent::ChildRemoved && receiver->postedEvents && globalPostedEvents) { + +#ifdef QT_THREAD_SUPPORT + QMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + // the QObject destructor calls QObject::removeChild, which calls + // QApplication::sendEvent() directly. this can happen while the event + // loop is in the middle of posting events, and when we get here, we may + // not have any more posted events for this object. + if ( receiver->postedEvents ) { + // if this is a child remove event and the child insert + // hasn't been dispatched yet, kill that insert + QPostEventList * l = receiver->postedEvents; + QObject * c = ((QChildEvent*)e)->child(); + QPostEvent * pe; + l->first(); + while( ( pe = l->current()) != 0 ) { + if ( pe->event && pe->receiver == receiver && + pe->event->type() == QEvent::ChildInserted && + ((QChildEvent*)pe->event)->child() == c ) { + pe->event->posted = FALSE; + delete pe->event; + pe->event = 0; + l->remove(); + continue; + } + l->next(); + } + } + } + + bool res = FALSE; + if ( !receiver->isWidgetType() ) + res = internalNotify( receiver, e ); + else switch ( e->type() ) { +#ifndef QT_NO_ACCEL + case QEvent::Accel: + { + QKeyEvent* key = (QKeyEvent*) e; + res = internalNotify( receiver, e ); + + if ( !res && !key->isAccepted() ) + res = qt_dispatchAccelEvent( (QWidget*)receiver, key ); + + // next lines are for compatibility with Qt <= 3.0.x: old + // QAccel was listening on toplevel widgets + if ( !res && !key->isAccepted() && !((QWidget*)receiver)->isTopLevel() ) + res = internalNotify( ((QWidget*)receiver)->topLevelWidget(), e ); + } + break; +#endif //QT_NO_ACCEL + case QEvent::KeyPress: + case QEvent::KeyRelease: + case QEvent::AccelOverride: + { + QWidget* w = (QWidget*)receiver; + QKeyEvent* key = (QKeyEvent*) e; +#ifndef QT_NO_ACCEL + if ( qt_tryComposeUnicode( w, key ) ) + break; +#endif + bool def = key->isAccepted(); + while ( w ) { + if ( def ) + key->accept(); + else + key->ignore(); + res = internalNotify( w, e ); + if ( res || key->isAccepted() ) + break; + w = w->parentWidget( TRUE ); + } + } + break; + case QEvent::MouseButtonPress: + if ( e->spontaneous() ) { + QWidget* fw = (QWidget*)receiver; + while ( fw->focusProxy() ) + fw = fw->focusProxy(); + if ( fw->isEnabled() && fw->focusPolicy() & QWidget::ClickFocus ) { + QFocusEvent::setReason( QFocusEvent::Mouse); + fw->setFocus(); + QFocusEvent::resetReason(); + } + } + // fall through intended + case QEvent::MouseButtonRelease: + case QEvent::MouseButtonDblClick: + case QEvent::MouseMove: + { + QWidget* w = (QWidget*)receiver; + QMouseEvent* mouse = (QMouseEvent*) e; + QPoint relpos = mouse->pos(); + while ( w ) { + QMouseEvent me(mouse->type(), relpos, mouse->globalPos(), mouse->button(), mouse->state()); + me.spont = mouse->spontaneous(); + res = internalNotify( w, w == receiver ? mouse : &me ); + e->spont = FALSE; + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + mouse->accept(); + else + mouse->ignore(); + } + break; +#ifndef QT_NO_WHEELEVENT + case QEvent::Wheel: + { + if ( e->spontaneous() ) { + QWidget* fw = (QWidget*)receiver; + while ( fw->focusProxy() ) + fw = fw->focusProxy(); + if ( fw->isEnabled() && (fw->focusPolicy() & QWidget::WheelFocus) == QWidget::WheelFocus ) { + QFocusEvent::setReason( QFocusEvent::Mouse); + fw->setFocus(); + QFocusEvent::resetReason(); + } + } + + QWidget* w = (QWidget*)receiver; + QWheelEvent* wheel = (QWheelEvent*) e; + QPoint relpos = wheel->pos(); + while ( w ) { + QWheelEvent we(relpos, wheel->globalPos(), wheel->delta(), wheel->state(), wheel->orientation()); + we.spont = wheel->spontaneous(); + res = internalNotify( w, w == receiver ? wheel : &we ); + e->spont = FALSE; + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + wheel->accept(); + else + wheel->ignore(); + } + break; +#endif + case QEvent::ContextMenu: + { + QWidget* w = (QWidget*)receiver; + QContextMenuEvent *context = (QContextMenuEvent*) e; + QPoint relpos = context->pos(); + while ( w ) { + QContextMenuEvent ce(context->reason(), relpos, context->globalPos(), context->state()); + ce.spont = e->spontaneous(); + res = internalNotify( w, w == receiver ? context : &ce ); + e->spont = FALSE; + + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + context->accept(); + else + context->ignore(); + } + break; +#if defined (QT_TABLET_SUPPORT) + case QEvent::TabletMove: + case QEvent::TabletPress: + case QEvent::TabletRelease: + { + QWidget *w = (QWidget*)receiver; + QTabletEvent *tablet = (QTabletEvent*)e; + QPoint relpos = tablet->pos(); + while ( w ) { + QTabletEvent te(tablet->pos(), tablet->globalPos(), tablet->device(), + tablet->pressure(), tablet->xTilt(), tablet->yTilt(), + tablet->uniqueId()); + te.spont = e->spontaneous(); + res = internalNotify( w, w == receiver ? tablet : &te ); + e->spont = FALSE; + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + tablet->accept(); + else + tablet->ignore(); + chokeMouse = tablet->isAccepted(); + } + break; +#endif + default: + res = internalNotify( receiver, e ); + break; + } + + return res; +} + +/*!\reimp + +*/ +bool QApplication::event( QEvent *e ) +{ + if(e->type() == QEvent::Close) { + QCloseEvent *ce = (QCloseEvent*)e; + ce->accept(); + closeAllWindows(); + + QWidgetList *list = topLevelWidgets(); + for(QWidget *w = list->first(); w; w = list->next()) { + if ( !w->isHidden() && !w->isDesktop() && !w->isPopup() && + (!w->isDialog() || !w->parentWidget())) { + ce->ignore(); + break; + } + } + if(ce->isAccepted()) + return TRUE; + } else if (e->type() == QEvent::Quit) { + quit(); + return TRUE; + } + return QObject::event(e); +} + +/*!\internal + + Helper function called by notify() + */ +bool QApplication::internalNotify( QObject *receiver, QEvent * e) +{ + if ( eventFilters ) { + QObjectListIt it( *eventFilters ); + register QObject *obj; + while ( (obj=it.current()) != 0 ) { // send to all filters + ++it; // until one returns TRUE + if ( obj->eventFilter(receiver,e) ) + return TRUE; + } + } + + bool consumed = FALSE; + bool handled = FALSE; + if ( receiver->isWidgetType() ) { + QWidget *widget = (QWidget*)receiver; + + // toggle HasMouse widget state on enter and leave + if ( e->type() == QEvent::Enter || e->type() == QEvent::DragEnter ) + widget->setWState( WState_HasMouse ); + else if ( e->type() == QEvent::Leave || e->type() == QEvent::DragLeave ) + widget->clearWState( WState_HasMouse ); + + // throw away any mouse-tracking-only mouse events + if ( e->type() == QEvent::MouseMove && + (((QMouseEvent*)e)->state()&QMouseEvent::MouseButtonMask) == 0 && + !widget->hasMouseTracking() ) { + handled = TRUE; + consumed = TRUE; + } else if ( !widget->isEnabled() ) { // throw away mouse events to disabled widgets + switch(e->type()) { + case QEvent::MouseButtonPress: + case QEvent::MouseButtonRelease: + case QEvent::MouseButtonDblClick: + case QEvent::MouseMove: + ( (QMouseEvent*) e)->ignore(); + handled = TRUE; + consumed = TRUE; + break; +#ifndef QT_NO_DRAGANDDROP + case QEvent::DragEnter: + case QEvent::DragMove: + ( (QDragMoveEvent*) e)->ignore(); + handled = TRUE; + break; + + case QEvent::DragLeave: + case QEvent::DragResponse: + handled = TRUE; + break; + + case QEvent::Drop: + ( (QDropEvent*) e)->ignore(); + handled = TRUE; + break; +#endif +#ifndef QT_NO_WHEELEVENT + case QEvent::Wheel: + ( (QWheelEvent*) e)->ignore(); + handled = TRUE; + break; +#endif + case QEvent::ContextMenu: + ( (QContextMenuEvent*) e)->ignore(); + handled = TRUE; + break; + default: + break; + } + } + + } + + if (!handled) + consumed = receiver->event( e ); + e->spont = FALSE; + return consumed; +} + +/*! + Returns TRUE if an application object has not been created yet; + otherwise returns FALSE. + + \sa closingDown() +*/ + +bool QApplication::startingUp() +{ + return !is_app_running; +} + +/*! + Returns TRUE if the application objects are being destroyed; + otherwise returns FALSE. + + \sa startingUp() +*/ + +bool QApplication::closingDown() +{ + return is_app_closing; +} + + +/*! + Processes pending events, for 3 seconds or until there are no more + events to process, whichever is shorter. + + You can call this function occasionally when your program is busy + performing a long operation (e.g. copying a file). + + \sa exec(), QTimer, QEventLoop::processEvents() +*/ + +void QApplication::processEvents() +{ + processEvents( 3000 ); +} + +/*! + \overload + + Processes pending events for \a maxtime milliseconds or until + there are no more events to process, whichever is shorter. + + You can call this function occasionally when you program is busy + doing a long operation (e.g. copying a file). + + \sa exec(), QTimer, QEventLoop::processEvents() +*/ +void QApplication::processEvents( int maxtime ) +{ + eventLoop()->processEvents( QEventLoop::AllEvents, maxtime ); +} + +/*! \obsolete + Waits for an event to occur, processes it, then returns. + + This function is useful for adapting Qt to situations where the + event processing must be grafted onto existing program loops. + + Using this function in new applications may be an indication of design + problems. + + \sa processEvents(), exec(), QTimer +*/ + +void QApplication::processOneEvent() +{ + eventLoop()->processEvents( QEventLoop::AllEvents | + QEventLoop::WaitForMore ); +} + +/***************************************************************************** + Main event loop wrappers + *****************************************************************************/ + +/*! + Returns the application event loop. This function will return + zero if called during and after destroying QApplication. + + To create your own instance of QEventLoop or QEventLoop subclass create + it before you create the QApplication object. + + \sa QEventLoop +*/ +QEventLoop *QApplication::eventLoop() +{ + if ( !eventloop && !is_app_closing ) + (void) new QEventLoop( qApp, "default event loop" ); + return eventloop; +} + + +/*! + Enters the main event loop and waits until exit() is called or the + main widget is destroyed, and returns the value that was set to + exit() (which is 0 if exit() is called via quit()). + + It is necessary to call this function to start event handling. The + main event loop receives events from the window system and + dispatches these to the application widgets. + + Generally speaking, no user interaction can take place before + calling exec(). As a special case, modal widgets like QMessageBox + can be used before calling exec(), because modal widgets call + exec() to start a local event loop. + + To make your application perform idle processing, i.e. executing a + special function whenever there are no pending events, use a + QTimer with 0 timeout. More advanced idle processing schemes can + be achieved using processEvents(). + + \sa quit(), exit(), processEvents(), setMainWidget() +*/ +int QApplication::exec() +{ + return eventLoop()->exec(); +} + +/*! + Tells the application to exit with a return code. + + After this function has been called, the application leaves the main + event loop and returns from the call to exec(). The exec() function + returns \a retcode. + + By convention, a \a retcode of 0 means success, and any non-zero + value indicates an error. + + Note that unlike the C library function of the same name, this + function \e does return to the caller -- it is event processing that + stops. + + \sa quit(), exec() +*/ +void QApplication::exit( int retcode ) +{ + qApp->eventLoop()->exit( retcode ); +} + +/*! + \obsolete + + This function enters the main event loop (recursively). Do not call + it unless you really know what you are doing. + + Use QApplication::eventLoop()->enterLoop() instead. + +*/ +int QApplication::enter_loop() +{ + return eventLoop()->enterLoop(); +} + +/*! + \obsolete + + This function exits from a recursive call to the main event loop. + Do not call it unless you are an expert. + + Use QApplication::eventLoop()->exitLoop() instead. + +*/ +void QApplication::exit_loop() +{ + eventLoop()->exitLoop(); +} + +/*! + \obsolete + + Returns the current loop level. + + Use QApplication::eventLoop()->loopLevel() instead. + +*/ +int QApplication::loopLevel() const +{ + return eventLoop()->loopLevel(); +} + +/*! + + Wakes up the GUI thread. + + \sa guiThreadAwake() \link threads.html Thread Support in Qt\endlink +*/ +void QApplication::wakeUpGuiThread() +{ + eventLoop()->wakeUp(); +} + +/*! + This function returns TRUE if there are pending events; otherwise + returns FALSE. Pending events can be either from the window system + or posted events using QApplication::postEvent(). +*/ +bool QApplication::hasPendingEvents() +{ + return eventLoop()->hasPendingEvents(); +} + +#if !defined(Q_WS_X11) + +// The doc and X implementation of these functions is in qapplication_x11.cpp + +void QApplication::flushX() {} // do nothing + +void QApplication::syncX() {} // do nothing + +#endif + +/*! + \fn void QApplication::setWinStyleHighlightColor( const QColor & ) + \obsolete + + Sets the color used to mark selections in windows style for all widgets + in the application. Will repaint all widgets if the color is changed. + + The default color is \c darkBlue. + \sa winStyleHighlightColor() +*/ + +/*! + \fn const QColor& QApplication::winStyleHighlightColor() + \obsolete + + Returns the color used to mark selections in windows style. + + \sa setWinStyleHighlightColor() +*/ + +/*! + Returns the version of the Windows operating system that is running: + + \list + \i Qt::WV_95 - Windows 95 + \i Qt::WV_98 - Windows 98 + \i Qt::WV_Me - Windows Me + \i Qt::WV_NT - Windows NT 4.x + \i Qt::WV_2000 - Windows 2000 (NT5) + \i Qt::WV_XP - Windows XP + \i Qt::WV_2003 - Windows Server 2003 family + \i Qt::WV_CE - Windows CE + \i Qt::WV_CENET - Windows CE.NET + \endlist + + Note that this function is implemented for the Windows version + of Qt only. +*/ + +#if defined(Q_OS_CYGWIN) +Qt::WindowsVersion QApplication::winVersion() +{ + return qt_winver; +} +#endif + +#ifndef QT_NO_TRANSLATION + +bool qt_detectRTLLanguage() +{ + return QApplication::tr( "QT_LAYOUT_DIRECTION", + "Translate this string to the string 'LTR' in left-to-right" + " languages or to 'RTL' in right-to-left languages (such as Hebrew" + " and Arabic) to get proper widget layout." ) == "RTL"; +} + +/*! + Adds the message file \a mf to the list of message files to be used + for translations. + + Multiple message files can be installed. Translations are searched + for in the last installed message file, then the one from last, and + so on, back to the first installed message file. The search stops as + soon as a matching translation is found. + + \sa removeTranslator() translate() QTranslator::load() +*/ + +void QApplication::installTranslator( QTranslator * mf ) +{ + if ( !mf ) + return; + if ( !translators ) + translators = new QValueList<QTranslator*>; + + translators->prepend( mf ); + +#ifndef QT_NO_TRANSLATION_BUILDER + if ( mf->isEmpty() ) + return; +#endif + + // hook to set the layout direction of dialogs + setReverseLayout( qt_detectRTLLanguage() ); + + QWidgetList *list = topLevelWidgets(); + QWidgetListIt it( *list ); + QWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + if (!w->isDesktop()) + postEvent( w, new QEvent( QEvent::LanguageChange ) ); + } + delete list; +} + +/*! + Removes the message file \a mf from the list of message files used by + this application. (It does not delete the message file from the file + system.) + + \sa installTranslator() translate(), QObject::tr() +*/ + +void QApplication::removeTranslator( QTranslator * mf ) +{ + if ( !translators || !mf ) + return; + + if ( translators->remove( mf ) && ! qApp->closingDown() ) { + setReverseLayout( qt_detectRTLLanguage() ); + + QWidgetList *list = topLevelWidgets(); + QWidgetListIt it( *list ); + QWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + postEvent( w, new QEvent( QEvent::LanguageChange ) ); + } + delete list; + } +} + +#ifndef QT_NO_TEXTCODEC +/*! \obsolete + This is the same as QTextCodec::setCodecForTr(). +*/ +void QApplication::setDefaultCodec( QTextCodec* codec ) +{ + QTextCodec::setCodecForTr( codec ); +} + +/*! \obsolete + Returns QTextCodec::codecForTr(). +*/ +QTextCodec* QApplication::defaultCodec() const +{ + return QTextCodec::codecForTr(); +} +#endif //QT_NO_TEXTCODEC + +/*! \enum QApplication::Encoding + + This enum type defines the 8-bit encoding of character string + arguments to translate(): + + \value DefaultCodec - the encoding specified by + QTextCodec::codecForTr() (Latin-1 if none has been set) + \value UnicodeUTF8 - UTF-8 + + \sa QObject::tr(), QObject::trUtf8(), QString::fromUtf8() +*/ + +/*! \reentrant + Returns the translation text for \a sourceText, by querying the + installed messages files. The message files are searched from the most + recently installed message file back to the first installed message + file. + + QObject::tr() and QObject::trUtf8() provide this functionality more + conveniently. + + \a context is typically a class name (e.g., "MyDialog") and + \a sourceText is either English text or a short identifying text, if + the output text will be very long (as for help texts). + + \a comment is a disambiguating comment, for when the same \a + sourceText is used in different roles within the same context. By + default, it is null. \a encoding indicates the 8-bit encoding of + character stings + + See the \l QTranslator documentation for more information about + contexts and comments. + + If none of the message files contain a translation for \a + sourceText in \a context, this function returns a QString + equivalent of \a sourceText. The encoding of \a sourceText is + specified by \e encoding; it defaults to \c DefaultCodec. + + This function is not virtual. You can use alternative translation + techniques by subclassing \l QTranslator. + + \warning This method is reentrant only if all translators are + installed \e before calling this method. Installing or removing + translators while performing translations is not supported. Doing + so will most likely result in crashes or other undesirable behavior. + + \sa QObject::tr() installTranslator() defaultCodec() +*/ + +QString QApplication::translate( const char * context, const char * sourceText, + const char * comment, Encoding encoding ) const +{ + if ( !sourceText ) + return QString::null; + + if ( translators ) { + QValueList<QTranslator*>::iterator it; + QTranslator * mf; + QString result; + for ( it = translators->begin(); it != translators->end(); ++it ) { + mf = *it; + result = mf->findMessage( context, sourceText, comment ).translation(); + if ( !result.isNull() ) + return result; + } + } +#ifndef QT_NO_TEXTCODEC + if ( encoding == UnicodeUTF8 ) + return QString::fromUtf8( sourceText ); + else if ( QTextCodec::codecForTr() != 0 ) + return QTextCodec::codecForTr()->toUnicode( sourceText ); + else +#endif + return QString::fromLatin1( sourceText ); +} + +#endif + +/***************************************************************************** + QApplication management of posted events + *****************************************************************************/ + +//see also notify(), which does the removal of ChildInserted when ChildRemoved. + +/*! + Adds the event \a event with the object \a receiver as the receiver of the + event, to an event queue and returns immediately. + + The event must be allocated on the heap since the post event queue + will take ownership of the event and delete it once it has been posted. + + When control returns to the main event loop, all events that are + stored in the queue will be sent using the notify() function. + + \threadsafe + + \sa sendEvent(), notify() +*/ + +void QApplication::postEvent( QObject *receiver, QEvent *event ) +{ + if ( receiver == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "QApplication::postEvent: Unexpected null receiver" ); +#endif + delete event; + return; + } + +#ifdef QT_THREAD_SUPPORT + QMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + if ( !globalPostedEvents ) { // create list + globalPostedEvents = new QPostEventList; + Q_CHECK_PTR( globalPostedEvents ); + globalPostedEvents->setAutoDelete( TRUE ); + qapp_cleanup_events.set( &globalPostedEvents ); + } + + if ( !receiver->postedEvents ) + receiver->postedEvents = new QPostEventList; + QPostEventList * l = receiver->postedEvents; + + // if this is one of the compressible events, do compression + if ( event->type() == QEvent::Paint || + event->type() == QEvent::LayoutHint || + event->type() == QEvent::Resize || + event->type() == QEvent::Move || + event->type() == QEvent::LanguageChange ) { + l->first(); + QPostEvent * cur = 0; + for ( ;; ) { + while ( (cur=l->current()) != 0 && + ( cur->receiver != receiver || + cur->event == 0 || + cur->event->type() != event->type() ) ) + l->next(); + if ( l->current() != 0 ) { + if ( cur->event->type() == QEvent::Paint ) { + QPaintEvent * p = (QPaintEvent*)(cur->event); + if ( p->erase != ((QPaintEvent*)event)->erase ) { + l->next(); + continue; + } + p->reg = p->reg.unite( ((QPaintEvent *)event)->reg ); + p->rec = p->rec.unite( ((QPaintEvent *)event)->rec ); + delete event; + return; + } else if ( cur->event->type() == QEvent::LayoutHint ) { + delete event; + return; + } else if ( cur->event->type() == QEvent::Resize ) { + ((QResizeEvent *)(cur->event))->s = ((QResizeEvent *)event)->s; + delete event; + return; + } else if ( cur->event->type() == QEvent::Move ) { + ((QMoveEvent *)(cur->event))->p = ((QMoveEvent *)event)->p; + delete event; + return; + } else if ( cur->event->type() == QEvent::LanguageChange ) { + delete event; + return; + } + } + break; + }; + } + +#if !defined(QT_NO_IM) + // if this is one of the compressible IM events, do compression + else if ( event->type() == QEvent::IMCompose ) { + l->last(); + QPostEvent * cur = 0; + for ( ;; ) { + while ( (cur=l->current()) != 0 && + ( cur->receiver != receiver || + cur->event == 0 || + cur->event->type() != event->type() || + cur->event->type() != QEvent::IMStart ) ) + l->prev(); + if ( l->current() != 0 ) { + // IMCompose must not be compressed with another one + // beyond its IMStart boundary + if ( cur->event->type() == QEvent::IMStart ) { + break; + } else if ( cur->event->type() == QEvent::IMCompose ) { + QIMComposeEvent * e = (QIMComposeEvent *)(cur->event); + *e = *(QIMComposeEvent *)event; + delete event; + return; + } + } + break; + }; + } +#endif + + // if no compression could be done, just append something + event->posted = TRUE; + QPostEvent * pe = new QPostEvent( receiver, event ); + l->append( pe ); + globalPostedEvents->append( pe ); + + if (eventloop) + eventloop->wakeUp(); +} + + +/*! \overload + + Dispatches all posted events, i.e. empties the event queue. +*/ +void QApplication::sendPostedEvents() +{ + sendPostedEvents( 0, 0 ); +} + + + +/*! + Immediately dispatches all events which have been previously queued + with QApplication::postEvent() and which are for the object \a receiver + and have the event type \a event_type. + + Note that events from the window system are \e not dispatched by this + function, but by processEvents(). + + If \a receiver is null, the events of \a event_type are sent for all + objects. If \a event_type is 0, all the events are sent for \a receiver. +*/ + +void QApplication::sendPostedEvents( QObject *receiver, int event_type ) +{ + // Make sure the object hierarchy is stable before processing events + // to avoid endless loops + if ( receiver == 0 && event_type == 0 ) + sendPostedEvents( 0, QEvent::ChildInserted ); + + if ( !globalPostedEvents || ( receiver && !receiver->postedEvents ) ) + return; + +#ifdef QT_THREAD_SUPPORT + QMutexLocker locker( postevent_mutex ); +#endif + + bool sent = TRUE; + while ( sent ) { + sent = FALSE; + + if ( !globalPostedEvents || ( receiver && !receiver->postedEvents ) ) + return; + + // if we have a receiver, use the local list. Otherwise, use the + // global list + QPostEventList * l = receiver ? receiver->postedEvents : globalPostedEvents; + + // okay. here is the tricky loop. be careful about optimizing + // this, it looks the way it does for good reasons. + QPostEventListIt it( *l ); + QPostEvent *pe; + while ( (pe=it.current()) != 0 ) { + ++it; + if ( pe->event // hasn't been sent yet + && ( receiver == 0 // we send to all receivers + || receiver == pe->receiver ) // we send to THAT receiver + && ( event_type == 0 // we send all types + || event_type == pe->event->type() ) ) { // we send THAT type + // first, we diddle the event so that we can deliver + // it, and that noone will try to touch it later. + pe->event->posted = FALSE; + QEvent * e = pe->event; + QObject * r = pe->receiver; + pe->event = 0; + + // next, update the data structure so that we're ready + // for the next event. + + // look for the local list, and take whatever we're + // delivering out of it. r->postedEvents maybe *l + if ( r->postedEvents ) { + r->postedEvents->removeRef( pe ); + // if possible, get rid of that list. this is not + // ideal - we will create and delete a list for + // each update() call. it would be better if we'd + // leave the list empty here, and delete it + // somewhere else if it isn't being used. + if ( r->postedEvents->isEmpty() ) { + delete r->postedEvents; + r->postedEvents = 0; + } + } + +#ifdef QT_THREAD_SUPPORT + if ( locker.mutex() ) locker.mutex()->unlock(); +#endif // QT_THREAD_SUPPORT + // after all that work, it's time to deliver the event. + if ( e->type() == QEvent::Paint && r->isWidgetType() ) { + QWidget * w = (QWidget*)r; + QPaintEvent * p = (QPaintEvent*)e; + if ( w->isVisible() ) + w->repaint( p->reg, p->erase ); + } else { + sent = TRUE; + QApplication::sendEvent( r, e ); + } +#ifdef QT_THREAD_SUPPORT + if ( locker.mutex() ) locker.mutex()->lock(); +#endif // QT_THREAD_SUPPORT + + delete e; + // careful when adding anything below this point - the + // sendEvent() call might invalidate any invariants this + // function depends on. + } + } + + // clear the global list, i.e. remove everything that was + // delivered. + if ( l == globalPostedEvents ) { + globalPostedEvents->first(); + while( (pe=globalPostedEvents->current()) != 0 ) { + if ( pe->event ) + globalPostedEvents->next(); + else + globalPostedEvents->remove(); + } + } + } +} + +/*! + Removes all events posted using postEvent() for \a receiver. + + The events are \e not dispatched, instead they are removed from the + queue. You should never need to call this function. If you do call it, + be aware that killing events may cause \a receiver to break one or + more invariants. + + \threadsafe +*/ + +void QApplication::removePostedEvents( QObject *receiver ) +{ + removePostedEvents( receiver, 0 ); +} + +/*! + Removes all events that have the event type \a event_type posted + using postEvent() for \a receiver. + + The events are \e not dispatched, instead they are removed from the + queue. + + If \a event_type is 0, all the events are removed from the queue. + + \threadsafe +*/ + +void QApplication::removePostedEvents( QObject *receiver, int event_type ) +{ + if ( !receiver ) + return; + +#ifdef QT_THREAD_SUPPORT + QMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + // the QObject destructor calls this function directly. this can + // happen while the event loop is in the middle of posting events, + // and when we get here, we may not have any more posted events + // for this object. + if ( !receiver->postedEvents ) + return; + + // iterate over the object-specifc list and delete the events. + // leave the QPostEvent objects; they'll be deleted by + // sendPostedEvents(). + QPostEventList * l = receiver->postedEvents; + l->first(); + QPostEvent * pe; + while( (pe=l->current()) != 0 ) { + if ( !event_type || pe->event->type() == event_type ) { + if ( pe->event ) { + pe->event->posted = FALSE; + delete pe->event; + pe->event = 0; + } + l->remove(); + } else { + l->next(); + } + } + if ( !event_type || !l->count() ) { + receiver->postedEvents = 0; + delete l; + } +} + + +/*! + Removes \a event from the queue of posted events, and emits a + warning message if appropriate. + + \warning This function can be \e really slow. Avoid using it, if + possible. + + \threadsafe +*/ + +void QApplication::removePostedEvent( QEvent * event ) +{ + if ( !event || !event->posted ) + return; + + if ( !globalPostedEvents ) { +#if defined(QT_DEBUG) + qDebug( "QApplication::removePostedEvent: %p %d is posted: impossible", + (void*)event, event->type() ); + return; +#endif + } + +#ifdef QT_THREAD_SUPPORT + QMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + QPostEventListIt it( *globalPostedEvents ); + QPostEvent * pe; + while( (pe = it.current()) != 0 ) { + ++it; + if ( pe->event == event ) { +#if defined(QT_DEBUG) + const char *n; + switch ( event->type() ) { + case QEvent::Timer: + n = "Timer"; + break; + case QEvent::MouseButtonPress: + n = "MouseButtonPress"; + break; + case QEvent::MouseButtonRelease: + n = "MouseButtonRelease"; + break; + case QEvent::MouseButtonDblClick: + n = "MouseButtonDblClick"; + break; + case QEvent::MouseMove: + n = "MouseMove"; + break; +#ifndef QT_NO_WHEELEVENT + case QEvent::Wheel: + n = "Wheel"; + break; +#endif + case QEvent::KeyPress: + n = "KeyPress"; + break; + case QEvent::KeyRelease: + n = "KeyRelease"; + break; + case QEvent::FocusIn: + n = "FocusIn"; + break; + case QEvent::FocusOut: + n = "FocusOut"; + break; + case QEvent::Enter: + n = "Enter"; + break; + case QEvent::Leave: + n = "Leave"; + break; + case QEvent::Paint: + n = "Paint"; + break; + case QEvent::Move: + n = "Move"; + break; + case QEvent::Resize: + n = "Resize"; + break; + case QEvent::Create: + n = "Create"; + break; + case QEvent::Destroy: + n = "Destroy"; + break; + case QEvent::Close: + n = "Close"; + break; + case QEvent::Quit: + n = "Quit"; + break; + default: + n = "<other>"; + break; + } + qWarning("QEvent: Warning: %s event deleted while posted to %s %s", + n, + pe->receiver ? pe->receiver->className() : "null", + pe->receiver ? pe->receiver->name() : "object" ); + // note the beautiful uglehack if !pe->receiver :) +#endif + event->posted = FALSE; + delete pe->event; + pe->event = 0; + return; + } + } +} + +/*!\internal + + Sets the active window in reaction to a system event. Call this + from the platform specific event handlers. + + It sets the activeWindow() and focusWidget() attributes and sends + proper WindowActivate/WindowDeactivate and FocusIn/FocusOut events + to all appropriate widgets. + + \sa activeWindow() + */ +void QApplication::setActiveWindow( QWidget* act ) +{ + QWidget* window = act?act->topLevelWidget():0; + + if ( active_window == window ) + return; + + // first the activation/deactivation events + if ( active_window ) { + QWidgetList deacts; +#ifndef QT_NO_STYLE + if ( style().styleHint(QStyle::SH_Widget_ShareActivation, active_window ) ) { + QWidgetList *list = topLevelWidgets(); + if ( list ) { + for ( QWidget *w = list->first(); w; w = list->next() ) { + if ( w->isVisible() && w->isActiveWindow() ) + deacts.append(w); + } + delete list; + } + } else +#endif + deacts.append(active_window); + active_window = 0; + QEvent e( QEvent::WindowDeactivate ); + for(QWidget *w = deacts.first(); w; w = deacts.next()) + QApplication::sendSpontaneousEvent( w, &e ); + } + + active_window = window; + if ( active_window ) { + QEvent e( QEvent::WindowActivate ); + QWidgetList acts; +#ifndef QT_NO_STYLE + if ( style().styleHint(QStyle::SH_Widget_ShareActivation, active_window ) ) { + QWidgetList *list = topLevelWidgets(); + if ( list ) { + for ( QWidget *w = list->first(); w; w = list->next() ) { + if ( w->isVisible() && w->isActiveWindow() ) + acts.append(w); + } + delete list; + } + } else +#endif + acts.append(active_window); + for(QWidget *w = acts.first(); w; w = acts.next()) + QApplication::sendSpontaneousEvent( w, &e ); + } + + // then focus events + QFocusEvent::setReason( QFocusEvent::ActiveWindow ); + if ( !active_window && focus_widget ) { + QFocusEvent out( QEvent::FocusOut ); + QWidget *tmp = focus_widget; + focus_widget = 0; +#ifdef Q_WS_WIN + QInputContext::accept( tmp ); +#elif defined(Q_WS_X11) + tmp->unfocusInputContext(); +#endif + QApplication::sendSpontaneousEvent( tmp, &out ); + } else if ( active_window ) { + QWidget *w = active_window->focusWidget(); + if ( w && w->focusPolicy() != QWidget::NoFocus ) + w->setFocus(); + else + active_window->focusNextPrevChild( TRUE ); + } + QFocusEvent::resetReason(); +} + + +/*!\internal + + Creates the proper Enter/Leave event when widget \a enter is entered + and widget \a leave is left. + */ +Q_EXPORT void qt_dispatchEnterLeave( QWidget* enter, QWidget* leave ) { +#if 0 + if ( leave ) { + QEvent e( QEvent::Leave ); + QApplication::sendEvent( leave, & e ); + } + if ( enter ) { + QEvent e( QEvent::Enter ); + QApplication::sendEvent( enter, & e ); + } + return; +#endif + + QWidget* w ; + if ( !enter && !leave ) + return; + QWidgetList leaveList; + QWidgetList enterList; + + bool sameWindow = leave && enter && leave->topLevelWidget() == enter->topLevelWidget(); + if ( leave && !sameWindow ) { + w = leave; + do { + leaveList.append( w ); + } while ( (w = w->parentWidget( TRUE ) ) ); + } + if ( enter && !sameWindow ) { + w = enter; + do { + enterList.prepend( w ); + } while ( (w = w->parentWidget(TRUE) ) ); + } + if ( sameWindow ) { + int enterDepth = 0; + int leaveDepth = 0; + w = enter; + while ( ( w = w->parentWidget( TRUE ) ) ) + enterDepth++; + w = leave; + while ( ( w = w->parentWidget( TRUE ) ) ) + leaveDepth++; + QWidget* wenter = enter; + QWidget* wleave = leave; + while ( enterDepth > leaveDepth ) { + wenter = wenter->parentWidget(); + enterDepth--; + } + while ( leaveDepth > enterDepth ) { + wleave = wleave->parentWidget(); + leaveDepth--; + } + while ( !wenter->isTopLevel() && wenter != wleave ) { + wenter = wenter->parentWidget(); + wleave = wleave->parentWidget(); + } + + w = leave; + while ( w != wleave ) { + leaveList.append( w ); + w = w->parentWidget(); + } + w = enter; + while ( w != wenter ) { + enterList.prepend( w ); + w = w->parentWidget(); + } + } + + QEvent leaveEvent( QEvent::Leave ); + for ( w = leaveList.first(); w; w = leaveList.next() ) { + if ( !qApp->activeModalWidget() || qt_tryModalHelper( w, 0 )) + QApplication::sendEvent( w, &leaveEvent ); + } + QEvent enterEvent( QEvent::Enter ); + for ( w = enterList.first(); w; w = enterList.next() ) { + if ( !qApp->activeModalWidget() || qt_tryModalHelper( w, 0 )) + QApplication::sendEvent( w, &enterEvent ); + } +} + + +#ifdef Q_WS_MACX +extern QWidget *qt_tryModalHelperMac( QWidget * top ); //qapplication_mac.cpp +#endif + + +/*!\internal + + Called from qapplication_<platform>.cpp, returns TRUE + if the widget should accept the event. + */ +Q_EXPORT bool qt_tryModalHelper( QWidget *widget, QWidget **rettop ) { + QWidget *modal=0, *top=QApplication::activeModalWidget(); + if ( rettop ) *rettop = top; + + if ( qApp->activePopupWidget() ) + return TRUE; + +#ifdef Q_WS_MACX + top = qt_tryModalHelperMac( top ); + if ( rettop ) *rettop = top; +#endif + + QWidget* groupLeader = widget; + widget = widget->topLevelWidget(); + + if ( widget->testWFlags(Qt::WShowModal) ) // widget is modal + modal = widget; + if ( !top || modal == top ) // don't block event + return TRUE; + + QWidget * p = widget->parentWidget(); // Check if the active modal widget is a parent of our widget + while ( p ) { + if ( p == top ) + return TRUE; + p = p->parentWidget(); + } + + while ( groupLeader && !groupLeader->testWFlags( Qt::WGroupLeader ) ) + groupLeader = groupLeader->parentWidget(); + + if ( groupLeader ) { + // Does groupLeader have a child in qt_modal_stack? + bool unrelated = TRUE; + modal = qt_modal_stack->first(); + while (modal && unrelated) { + QWidget* p = modal->parentWidget(); + while ( p && p != groupLeader && !p->testWFlags( Qt::WGroupLeader) ) { + p = p->parentWidget(); + } + modal = qt_modal_stack->next(); + if ( p == groupLeader ) unrelated = FALSE; + } + + if ( unrelated ) + return TRUE; // don't block event + } + return FALSE; +} + + +/*! + Returns the desktop widget (also called the root window). + + The desktop widget is useful for obtaining the size of the screen. + It may also be possible to draw on the desktop. We recommend against + assuming that it's possible to draw on the desktop, since this does + not work on all operating systems. + + \code + QDesktopWidget *d = QApplication::desktop(); + int w = d->width(); // returns desktop width + int h = d->height(); // returns desktop height + \endcode +*/ + +QDesktopWidget *QApplication::desktop() +{ + if ( !qt_desktopWidget || // not created yet + !qt_desktopWidget->isDesktop() ) { // reparented away + qt_desktopWidget = new QDesktopWidget(); + Q_CHECK_PTR( qt_desktopWidget ); + } + return qt_desktopWidget; +} + +#ifndef QT_NO_CLIPBOARD +/*! + Returns a pointer to the application global clipboard. +*/ +QClipboard *QApplication::clipboard() +{ + if ( qt_clipboard == 0 ) { + qt_clipboard = new QClipboard; + Q_CHECK_PTR( qt_clipboard ); + } + return qt_clipboard; +} +#endif // QT_NO_CLIPBOARD + +/*! + By default, Qt will try to use the current standard colors, fonts + etc., from the underlying window system's desktop settings, + and use them for all relevant widgets. This behavior can be switched off + by calling this function with \a on set to FALSE. + + This static function must be called before creating the QApplication + object, like this: + + \code + int main( int argc, char** argv ) { + QApplication::setDesktopSettingsAware( FALSE ); // I know better than the user + QApplication myApp( argc, argv ); // Use default fonts & colors + ... + } + \endcode + + \sa desktopSettingsAware() +*/ + +void QApplication::setDesktopSettingsAware( bool on ) +{ + obey_desktop_settings = on; +} + +/*! + Returns the value set by setDesktopSettingsAware(); by default TRUE. + + \sa setDesktopSettingsAware() +*/ + +bool QApplication::desktopSettingsAware() +{ + return obey_desktop_settings; +} + +/*! \fn void QApplication::lock() + + Lock the Qt Library Mutex. If another thread has already locked the + mutex, the calling thread will block until the other thread has + unlocked the mutex. + + \sa unlock() locked() \link threads.html Thread Support in Qt\endlink +*/ + + +/*! \fn void QApplication::unlock(bool wakeUpGui) + + Unlock the Qt Library Mutex. If \a wakeUpGui is TRUE (the default), + then the GUI thread will be woken with QApplication::wakeUpGuiThread(). + + \sa lock(), locked() \link threads.html Thread Support in Qt\endlink +*/ + + +/*! \fn bool QApplication::locked() + + Returns TRUE if the Qt Library Mutex is locked by a different thread; + otherwise returns FALSE. + + \warning Due to different implementations of recursive mutexes on + the supported platforms, calling this function from the same thread + that previously locked the mutex will give undefined results. + + \sa lock() unlock() \link threads.html Thread Support in Qt\endlink +*/ + +/*! \fn bool QApplication::tryLock() + + Attempts to lock the Qt Library Mutex, and returns immediately. If + the lock was obtained, this function returns TRUE. If another thread + has locked the mutex, this function returns FALSE, instead of + waiting for the lock to become available. + + The mutex must be unlocked with unlock() before another thread can + successfully lock it. + + \sa lock(), unlock() \link threads.html Thread Support in Qt\endlink +*/ + +#if defined(QT_THREAD_SUPPORT) +void QApplication::lock() +{ + qt_mutex->lock(); +} + +void QApplication::unlock(bool wakeUpGui) +{ + qt_mutex->unlock(); + + if (wakeUpGui) + wakeUpGuiThread(); +} + +bool QApplication::locked() +{ + return qt_mutex->locked(); +} + +bool QApplication::tryLock() +{ + return qt_mutex->tryLock(); +} +#endif + + +/*! + \fn bool QApplication::isSessionRestored() const + + Returns TRUE if the application has been restored from an earlier + \link session.html session\endlink; otherwise returns FALSE. + + \sa sessionId(), commitData(), saveState() +*/ + + +/*! + \fn QString QApplication::sessionId() const + + Returns the current \link session.html session's\endlink identifier. + + If the application has been restored from an earlier session, this + identifier is the same as it was in that previous session. + + The session identifier is guaranteed to be unique both for different + applications and for different instances of the same application. + + \sa isSessionRestored(), sessionKey(), commitData(), saveState() + */ + +/*! + \fn QString QApplication::sessionKey() const + + Returns the session key in the current \link session.html + session\endlink. + + If the application has been restored from an earlier session, this + key is the same as it was when the previous session ended. + + The session key changes with every call of commitData() or + saveState(). + + \sa isSessionRestored(), sessionId(), commitData(), saveState() + */ + + +/*! + \fn void QApplication::commitData( QSessionManager& sm ) + + This function deals with \link session.html session + management\endlink. It is invoked when the QSessionManager wants the + application to commit all its data. + + Usually this means saving all open files, after getting + permission from the user. Furthermore you may want to provide a means + by which the user can cancel the shutdown. + + Note that you should not exit the application within this function. + Instead, the session manager may or may not do this afterwards, + depending on the context. + + \warning Within this function, no user interaction is possible, \e + unless you ask the session manager \a sm for explicit permission. + See QSessionManager::allowsInteraction() and + QSessionManager::allowsErrorInteraction() for details and example + usage. + + The default implementation requests interaction and sends a close + event to all visible top level widgets. If any event was + rejected, the shutdown is canceled. + + \sa isSessionRestored(), sessionId(), saveState(), \link session.html the Session Management overview\endlink +*/ +#ifndef QT_NO_SESSIONMANAGER +void QApplication::commitData( QSessionManager& sm ) +{ + + if ( sm.allowsInteraction() ) { + QWidgetList done; + QWidgetList *list = QApplication::topLevelWidgets(); + bool cancelled = FALSE; + QWidget* w = list->first(); + while ( !cancelled && w ) { + if ( !w->isHidden() ) { + QCloseEvent e; + sendEvent( w, &e ); + cancelled = !e.isAccepted(); + if ( !cancelled ) + done.append( w ); + delete list; // one never knows... + list = QApplication::topLevelWidgets(); + w = list->first(); + } else { + w = list->next(); + } + while ( w && done.containsRef( w ) ) + w = list->next(); + } + delete list; + if ( cancelled ) + sm.cancel(); + } +} + + +/*! + \fn void QApplication::saveState( QSessionManager& sm ) + + This function deals with \link session.html session + management\endlink. It is invoked when the + \link QSessionManager session manager \endlink wants the application + to preserve its state for a future session. + + For example, a text editor would create a temporary file that + includes the current contents of its edit buffers, the location of + the cursor and other aspects of the current editing session. + + Note that you should never exit the application within this + function. Instead, the session manager may or may not do this + afterwards, depending on the context. Futhermore, most session + managers will very likely request a saved state immediately after + the application has been started. This permits the session manager + to learn about the application's restart policy. + + \warning Within this function, no user interaction is possible, \e + unless you ask the session manager \a sm for explicit permission. + See QSessionManager::allowsInteraction() and + QSessionManager::allowsErrorInteraction() for details. + + \sa isSessionRestored(), sessionId(), commitData(), \link session.html the Session Management overview\endlink +*/ + +void QApplication::saveState( QSessionManager& /* sm */ ) +{ +} +#endif //QT_NO_SESSIONMANAGER +/*! + Sets the time after which a drag should start to \a ms ms. + + \sa startDragTime() +*/ + +void QApplication::setStartDragTime( int ms ) +{ + drag_time = ms; +} + +/*! + If you support drag and drop in you application and a drag should + start after a mouse click and after a certain time elapsed, you + should use the value which this method returns as the delay (in ms). + + Qt also uses this delay internally, e.g. in QTextEdit and QLineEdit, + for starting a drag. + + The default value is 500 ms. + + \sa setStartDragTime(), startDragDistance() +*/ + +int QApplication::startDragTime() +{ + return drag_time; +} + +/*! + Sets the distance after which a drag should start to \a l pixels. + + \sa startDragDistance() +*/ + +void QApplication::setStartDragDistance( int l ) +{ + drag_distance = l; +} + +/*! + If you support drag and drop in you application and a drag should + start after a mouse click and after moving the mouse a certain + distance, you should use the value which this method returns as the + distance. + + For example, if the mouse position of the click is stored in \c + startPos and the current position (e.g. in the mouse move event) is + \c currPos, you can find out if a drag should be started with code + like this: + \code + if ( ( startPos - currPos ).manhattanLength() > + QApplication::startDragDistance() ) + startTheDrag(); + \endcode + + Qt uses this value internally, e.g. in QFileDialog. + + The default value is 4 pixels. + + \sa setStartDragDistance(), startDragTime(), QPoint::manhattanLength() +*/ + +int QApplication::startDragDistance() +{ + return drag_distance; +} + +/*! + If \a b is TRUE, all dialogs and widgets will be laid out in a + mirrored fashion, as required by right to left languages such as + Arabic and Hebrew. If \a b is FALSE, dialogs and widgets are laid + out left to right. + + Changing this flag in runtime does not cause a relayout of already + instantiated widgets. + + \sa reverseLayout() +*/ +void QApplication::setReverseLayout( bool b ) +{ + if ( reverse_layout == b ) + return; + + reverse_layout = b; + + QWidgetList *list = topLevelWidgets(); + QWidgetListIt it( *list ); + QWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + postEvent( w, new QEvent( QEvent::LayoutDirectionChange ) ); + } + delete list; +} + +/*! + Returns TRUE if all dialogs and widgets will be laid out in a + mirrored (right to left) fashion. Returns FALSE if dialogs and + widgets will be laid out left to right. + + \sa setReverseLayout() +*/ +bool QApplication::reverseLayout() +{ + return reverse_layout; +} + + +/*! + \class QSessionManager qsessionmanager.h + \brief The QSessionManager class provides access to the session manager. + + \ingroup application + \ingroup environment + + The session manager is responsible for session management, most + importantly for interruption and resumption. A "session" is a kind + of record of the state of the system, e.g. which applications were + run at start up and which applications are currently running. The + session manager is used to save the session, e.g. when the machine + is shut down; and to restore a session, e.g. when the machine is + started up. Use QSettings to save and restore an individual + application's settings, e.g. window positions, recently used files, + etc. + + QSessionManager provides an interface between the application and + the session manager so that the program can work well with the + session manager. In Qt, session management requests for action + are handled by the two virtual functions QApplication::commitData() + and QApplication::saveState(). Both provide a reference to + a session manager object as argument, to allow the application + to communicate with the session manager. + + During a session management action (i.e. within commitData() and + saveState()), no user interaction is possible \e unless the + application got explicit permission from the session manager. You + ask for permission by calling allowsInteraction() or, if it's really + urgent, allowsErrorInteraction(). Qt does not enforce this, but the + session manager may. + + You can try to abort the shutdown process by calling cancel(). The + default commitData() function does this if some top-level window + rejected its closeEvent(). + + For sophisticated session managers provided on Unix/X11, QSessionManager + offers further possibilites to fine-tune an application's session + management behavior: setRestartCommand(), setDiscardCommand(), + setRestartHint(), setProperty(), requestPhase2(). See the respective + function descriptions for further details. +*/ + +/*! \enum QSessionManager::RestartHint + + This enum type defines the circumstances under which this + application wants to be restarted by the session manager. The + current values are + + \value RestartIfRunning if the application is still running when + the session is shut down, it wants to be restarted at the start of + the next session. + + \value RestartAnyway the application wants to be started at the + start of the next session, no matter what. (This is useful for + utilities that run just after startup and then quit.) + + \value RestartImmediately the application wants to be started + immediately whenever it is not running. + + \value RestartNever the application does not want to be restarted + automatically. + + The default hint is \c RestartIfRunning. +*/ + + +/*! + \fn QString QSessionManager::sessionId() const + + Returns the identifier of the current session. + + If the application has been restored from an earlier session, this + identifier is the same as it was in that earlier session. + + \sa sessionKey(), QApplication::sessionId() + */ + +/*! + \fn QString QSessionManager::sessionKey() const + + Returns the session key in the current session. + + If the application has been restored from an earlier session, this + key is the same as it was when the previous session ended. + + The session key changes with every call of commitData() or + saveState(). + + \sa sessionId(), QApplication::sessionKey() + */ + +// ### Note: This function is undocumented, since it is #ifdef'd. + +/*! + \fn void* QSessionManager::handle() const + + X11 only: returns a handle to the current \c SmcConnection. +*/ + + +/*! + \fn bool QSessionManager::allowsInteraction() + + Asks the session manager for permission to interact with the + user. Returns TRUE if interaction is permitted; otherwise + returns FALSE. + + The rationale behind this mechanism is to make it possible to + synchronize user interaction during a shutdown. Advanced session + managers may ask all applications simultaneously to commit their + data, resulting in a much faster shutdown. + + When the interaction is completed we strongly recommend releasing the + user interaction semaphore with a call to release(). This way, other + applications may get the chance to interact with the user while your + application is still busy saving data. (The semaphore is implicitly + released when the application exits.) + + If the user decides to cancel the shutdown process during the + interaction phase, you must tell the session manager that this has + happened by calling cancel(). + + Here's an example of how an application's QApplication::commitData() + might be implemented: + +\code +void MyApplication::commitData( QSessionManager& sm ) { + if ( sm.allowsInteraction() ) { + switch ( QMessageBox::warning( + yourMainWindow, + tr("Application Name"), + tr("Save changes to document Foo?"), + tr("&Yes"), + tr("&No"), + tr("Cancel"), + 0, 2) ) { + case 0: // yes + sm.release(); + // save document here; if saving fails, call sm.cancel() + break; + case 1: // continue without saving + break; + default: // cancel + sm.cancel(); + break; + } + } else { + // we did not get permission to interact, then + // do something reasonable instead. + } +} +\endcode + + If an error occurred within the application while saving its data, + you may want to try allowsErrorInteraction() instead. + + \sa QApplication::commitData(), release(), cancel() +*/ + + +/*! + \fn bool QSessionManager::allowsErrorInteraction() + + This is similar to allowsInteraction(), but also tells the session + manager that an error occurred. Session managers may give error + interaction request higher priority, which means that it is more likely + that an error interaction is permitted. However, you are still not + guaranteed that the session manager will allow interaction. + + \sa allowsInteraction(), release(), cancel() +*/ + +/*! + \fn void QSessionManager::release() + + Releases the session manager's interaction semaphore after an + interaction phase. + + \sa allowsInteraction(), allowsErrorInteraction() +*/ + +/*! + \fn void QSessionManager::cancel() + + Tells the session manager to cancel the shutdown process. Applications + should not call this function without first asking the user. + + \sa allowsInteraction(), allowsErrorInteraction() + +*/ + +/*! + \fn void QSessionManager::setRestartHint( RestartHint hint ) + + Sets the application's restart hint to \a hint. On application + startup the hint is set to \c RestartIfRunning. + + Note that these flags are only hints, a session manager may or may + not respect them. + + We recommend setting the restart hint in QApplication::saveState() + because most session managers perform a checkpoint shortly after an + application's startup. + + \sa restartHint() +*/ + +/*! + \fn QSessionManager::RestartHint QSessionManager::restartHint() const + + Returns the application's current restart hint. The default is + \c RestartIfRunning. + + \sa setRestartHint() +*/ + +/*! + \fn void QSessionManager::setRestartCommand( const QStringList& command ) + + If the session manager is capable of restoring sessions it will + execute \a command in order to restore the application. The command + defaults to + + \code + appname -session id + \endcode + + The \c -session option is mandatory; otherwise QApplication cannot + tell whether it has been restored or what the current session + identifier is. See QApplication::isSessionRestored() and + QApplication::sessionId() for details. + + If your application is very simple, it may be possible to store the + entire application state in additional command line options. This + is usually a very bad idea because command lines are often limited + to a few hundred bytes. Instead, use QSettings, or temporary files + or a database for this purpose. By marking the data with the unique + sessionId(), you will be able to restore the application in a future + session. + + \sa restartCommand(), setDiscardCommand(), setRestartHint() +*/ + +/*! + \fn QStringList QSessionManager::restartCommand() const + + Returns the currently set restart command. + + Note that if you want to iterate over the list, you should + iterate over a copy, e.g. + \code + QStringList list = mySession.restartCommand(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa setRestartCommand(), restartHint() +*/ + +/*! + \fn void QSessionManager::setDiscardCommand( const QStringList& ) + + \sa discardCommand(), setRestartCommand() +*/ + + +/*! + \fn QStringList QSessionManager::discardCommand() const + + Returns the currently set discard command. + + Note that if you want to iterate over the list, you should + iterate over a copy, e.g. + \code + QStringList list = mySession.discardCommand(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa setDiscardCommand(), restartCommand(), setRestartCommand() +*/ + +/*! + \overload void QSessionManager::setManagerProperty( const QString& name, + const QString& value ) + + Low-level write access to the application's identification and state + records are kept in the session manager. + + The property called \a name has its value set to the string \a value. +*/ + +/*! + \fn void QSessionManager::setManagerProperty( const QString& name, + const QStringList& value ) + + Low-level write access to the application's identification and state + record are kept in the session manager. + + The property called \a name has its value set to the string list \a value. +*/ + +/*! + \fn bool QSessionManager::isPhase2() const + + Returns TRUE if the session manager is currently performing a second + session management phase; otherwise returns FALSE. + + \sa requestPhase2() +*/ + +/*! + \fn void QSessionManager::requestPhase2() + + Requests a second session management phase for the application. The + application may then return immediately from the + QApplication::commitData() or QApplication::saveState() function, + and they will be called again once most or all other applications have + finished their session management. + + The two phases are useful for applications such as the X11 window manager + that need to store information about another application's windows + and therefore have to wait until these applications have completed their + respective session management tasks. + + Note that if another application has requested a second phase it + may get called before, simultaneously with, or after your + application's second phase. + + \sa isPhase2() +*/ + +/*! + \fn int QApplication::horizontalAlignment( int align ) + + Strips out vertical alignment flags and transforms an + alignment \a align of AlignAuto into AlignLeft or + AlignRight according to the language used. The other horizontal + alignment flags are left untouched. +*/ + + +/***************************************************************************** + Stubbed session management support + *****************************************************************************/ +#ifndef QT_NO_SESSIONMANAGER +#if defined( QT_NO_SM_SUPPORT ) || defined( Q_WS_WIN ) || defined( Q_WS_MAC ) || defined( Q_WS_QWS ) + +class QSessionManagerData +{ +public: + QStringList restartCommand; + QStringList discardCommand; + QString sessionId; + QString sessionKey; + QSessionManager::RestartHint restartHint; +}; + +QSessionManager* qt_session_manager_self = 0; +QSessionManager::QSessionManager( QApplication * app, QString &id, QString &key ) + : QObject( app, "qt_sessionmanager" ) +{ + qt_session_manager_self = this; + d = new QSessionManagerData; +#if defined(Q_WS_WIN) && !defined(Q_OS_TEMP) + wchar_t guidstr[40]; + GUID guid; + CoCreateGuid( &guid ); + StringFromGUID2(guid, guidstr, 40); + id = QString::fromUcs2((ushort*)guidstr); + CoCreateGuid( &guid ); + StringFromGUID2(guid, guidstr, 40); + key = QString::fromUcs2((ushort*)guidstr); +#endif + d->sessionId = id; + d->sessionKey = key; + d->restartHint = RestartIfRunning; +} + +QSessionManager::~QSessionManager() +{ + delete d; + qt_session_manager_self = 0; +} + +QString QSessionManager::sessionId() const +{ + return d->sessionId; +} + +QString QSessionManager::sessionKey() const +{ + return d->sessionKey; +} + + +#if defined(Q_WS_X11) || defined(Q_WS_MAC) +void* QSessionManager::handle() const +{ + return 0; +} +#endif + +#if !defined(Q_WS_WIN) +bool QSessionManager::allowsInteraction() +{ + return TRUE; +} + +bool QSessionManager::allowsErrorInteraction() +{ + return TRUE; +} +void QSessionManager::release() +{ +} + +void QSessionManager::cancel() +{ +} +#endif + + +void QSessionManager::setRestartHint( QSessionManager::RestartHint hint) +{ + d->restartHint = hint; +} + +QSessionManager::RestartHint QSessionManager::restartHint() const +{ + return d->restartHint; +} + +void QSessionManager::setRestartCommand( const QStringList& command) +{ + d->restartCommand = command; +} + +QStringList QSessionManager::restartCommand() const +{ + return d->restartCommand; +} + +void QSessionManager::setDiscardCommand( const QStringList& command) +{ + d->discardCommand = command; +} + +QStringList QSessionManager::discardCommand() const +{ + return d->discardCommand; +} + +void QSessionManager::setManagerProperty( const QString&, const QString&) +{ +} + +void QSessionManager::setManagerProperty( const QString&, const QStringList& ) +{ +} + +bool QSessionManager::isPhase2() const +{ + return FALSE; +} + +void QSessionManager::requestPhase2() +{ +} + +#endif // QT_NO_SM_SUPPORT +#endif //QT_NO_SESSIONMANAGER diff --git a/src/kernel/qapplication.h b/src/kernel/qapplication.h new file mode 100644 index 0000000..39f7858 --- /dev/null +++ b/src/kernel/qapplication.h @@ -0,0 +1,555 @@ +/**************************************************************************** +** +** Definition of QApplication class +** +** Created : 931107 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QAPPLICATION_H +#define QAPPLICATION_H + +#ifndef QT_H +#include "qdesktopwidget.h" +#include "qasciidict.h" +#include "qpalette.h" +#include "qtranslator.h" +#include "qstrlist.h" +#include "qstringlist.h" +#endif // QT_H + +class QSessionManager; +class QStyle; +class QTranslator; +class QEventLoop; +#if defined(Q_WS_X11) +class QIMEvent; +#endif +#if defined(Q_WS_QWS) +class QWSDecoration; +#endif + +#ifdef QT_THREAD_SUPPORT +class QMutex; +#endif // QT_THREAD_SUPPORT + + +class QApplication; +extern Q_EXPORT QApplication *qApp; // global application object + + +class Q_EXPORT QApplication : public QObject +{ + Q_OBJECT +public: + QApplication( int &argc, char **argv ); + QApplication( int &argc, char **argv, bool GUIenabled ); + enum Type { Tty, GuiClient, GuiServer }; + QApplication( int &argc, char **argv, Type ); +#if defined(Q_WS_X11) + QApplication( Display* dpy, HANDLE visual = 0, HANDLE cmap = 0 ); + QApplication( Display *dpy, int argc, char **argv, + HANDLE visual = 0, HANDLE cmap= 0 ); +#endif + virtual ~QApplication(); + + int argc() const; + char **argv() const; + + Type type() const; + +#ifndef QT_NO_STYLE + static QStyle &style(); + static void setStyle( QStyle* ); + static QStyle* setStyle( const QString& ); +#endif +#ifndef Q_QDOC + enum ColorMode { NormalColors, CustomColors }; + static ColorMode colorMode(); + static void setColorMode( QApplication::ColorMode ); +#endif + + enum ColorSpec { NormalColor=0, CustomColor=1, ManyColor=2 }; + static int colorSpec(); + static void setColorSpec( int ); +#ifndef QT_NO_CURSOR + static QCursor *overrideCursor(); + static void setOverrideCursor( const QCursor &, bool replace=FALSE ); + static void restoreOverrideCursor(); +#endif + static bool hasGlobalMouseTracking(); + static void setGlobalMouseTracking( bool enable ); +#ifndef QT_NO_PALETTE + static QPalette palette( const QWidget* = 0 ); + static void setPalette( const QPalette &, bool informWidgets=FALSE, + const char* className = 0 ); +#endif + static QFont font( const QWidget* = 0 ); + static void setFont( const QFont &, bool informWidgets=FALSE, + const char* className = 0 ); + static QFontMetrics fontMetrics(); + + QWidget *mainWidget() const; + virtual void setMainWidget( QWidget * ); + virtual void polish( QWidget * ); + + static QWidgetList *allWidgets(); + static QWidgetList *topLevelWidgets(); + + static QDesktopWidget *desktop(); + + static QWidget *activePopupWidget(); + static QWidget *activeModalWidget(); +#ifndef QT_NO_CLIPBOARD + static QClipboard *clipboard(); +#endif + QWidget *focusWidget() const; + QWidget *activeWindow() const; + + static QWidget *widgetAt( int x, int y, bool child=FALSE ); + static QWidget *widgetAt( const QPoint &, bool child=FALSE ); + + static QEventLoop *eventLoop(); + + int exec(); + void processEvents(); + void processEvents( int maxtime ); + void processOneEvent(); + bool hasPendingEvents(); + int enter_loop(); + void exit_loop(); + int loopLevel() const; + static void exit( int retcode=0 ); + + static bool sendEvent( QObject *receiver, QEvent *event ); + static void postEvent( QObject *receiver, QEvent *event ); + static void sendPostedEvents( QObject *receiver, int event_type ); + static void sendPostedEvents(); + + static void removePostedEvents( QObject *receiver ); + + virtual bool notify( QObject *, QEvent * ); + + static bool startingUp(); + static bool closingDown(); + + static void flushX(); + static void flush(); + static void syncX(); + + static void beep(); + +#ifndef QT_NO_TRANSLATION +# ifndef QT_NO_TEXTCODEC + void setDefaultCodec( QTextCodec * ); + QTextCodec* defaultCodec() const; +# endif + void installTranslator( QTranslator * ); + void removeTranslator( QTranslator * ); +#endif + enum Encoding { DefaultCodec, UnicodeUTF8 }; + QString translate( const char * context, + const char * key, + const char * comment = 0, + Encoding encoding = DefaultCodec ) const; +#ifndef QT_NO_DIR + QString applicationDirPath(); + QString applicationFilePath(); +#endif +#ifndef QT_NO_PALETTE + // obsolete functions + static void setWinStyleHighlightColor( const QColor &c ) { + QPalette p( palette() ); + p.setColor( QColorGroup::Highlight, c ); + setPalette( p, TRUE); + } + static const QColor &winStyleHighlightColor() { + return palette().active().highlight(); + } +#endif + static void setDesktopSettingsAware( bool ); + static bool desktopSettingsAware(); + + static void setCursorFlashTime( int ); + static int cursorFlashTime(); + + static void setDoubleClickInterval( int ); + static int doubleClickInterval(); +#ifndef QT_NO_WHEELEVENT + static void setWheelScrollLines( int ); + static int wheelScrollLines(); +#endif + static void setGlobalStrut( const QSize & ); + static QSize globalStrut(); + +#ifndef QT_NO_COMPONENT + static void setLibraryPaths( const QStringList & ); + static QStringList libraryPaths(); + static void addLibraryPath( const QString & ); + static void removeLibraryPath( const QString & ); +#endif // QT_NO_COMPONENT + static void setStartDragTime( int ms ); + static int startDragTime(); + static void setStartDragDistance( int l ); + static int startDragDistance(); + + static void setReverseLayout( bool b ); + static bool reverseLayout(); + + static int horizontalAlignment( int align ); + + static bool isEffectEnabled( Qt::UIEffect ); + static void setEffectEnabled( Qt::UIEffect, bool enable = TRUE ); + +#if defined(Q_WS_MAC) + virtual bool macEventFilter( EventHandlerCallRef, EventRef ); +#endif +#if defined(Q_WS_WIN) + virtual bool winEventFilter( MSG * ); +#endif +#if defined(Q_WS_X11) + virtual bool x11EventFilter( XEvent * ); + virtual int x11ClientMessage( QWidget*, XEvent*, bool passive_only); + int x11ProcessEvent( XEvent* ); +#endif +#if defined(Q_WS_QWS) + virtual bool qwsEventFilter( QWSEvent * ); + int qwsProcessEvent( QWSEvent* ); + void qwsSetCustomColors( QRgb *colortable, int start, int numColors ); +/*! + \internal + Returns true if the process is GUI server +*/ + bool qwsIsGUIServer(); +#ifndef QT_NO_QWS_MANAGER + static QWSDecoration &qwsDecoration(); + static void qwsSetDecoration( QWSDecoration *); +#endif +#endif + +#if defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) + static WindowsVersion winVersion(); +#elif defined(Q_OS_MAC) + static MacintoshVersion macVersion(); +#endif +#if defined(Q_WS_WIN) + void winFocus( QWidget *, bool ); + static void winMouseButtonUp(); +#endif + +#ifndef QT_NO_SESSIONMANAGER + // session management + bool isSessionRestored() const; + QString sessionId() const; + QString sessionKey() const; + virtual void commitData( QSessionManager& sm ); + virtual void saveState( QSessionManager& sm ); +#endif +#if defined(Q_WS_X11) +#if !defined(QT_NO_IM_EXTENSIONS) + virtual QWidget *locateICHolderWidget( QWidget *w ); + virtual QWidgetList *icHolderWidgets(); + static void create_im(); + static void close_im(); +#else + QWidget *locateICHolderWidget( QWidget *w ); + QWidgetList *icHolderWidgets(); + static void create_xim(); + static void close_xim(); +#endif + static QString defaultInputMethod(); + void changeAllInputContext( const QString & ); + static bool x11_apply_settings(); +#endif + void wakeUpGuiThread(); +#if defined(QT_THREAD_SUPPORT) + void lock(); + void unlock(bool wakeUpGui = TRUE); + bool locked(); + bool tryLock(); +#endif + +signals: + void lastWindowClosed(); + void aboutToQuit(); + void guiThreadAwake(); + +public slots: + void quit(); + void closeAllWindows(); + void aboutQt(); + +#if defined(Q_WS_QWS) +protected: + void setArgs(int, char **); +#endif + +protected: + bool event(QEvent *); + +private: + void construct( int &argc, char **argv, Type ); + void initialize( int, char ** ); + void init_precmdline(); + void process_cmdline( int* argcptr, char ** argv ); + bool internalNotify( QObject *, QEvent * ); +#if defined(Q_WS_QWS) + static QWidget *findChildWidget( const QWidget *p, const QPoint &pos ); + static QWidget *findWidget( const QObjectList&, const QPoint &, bool rec ); +#endif + +#if defined(Q_WS_MAC) + bool do_mouse_down(Point *, bool *); + static QMAC_PASCAL OSStatus globalEventProcessor(EventHandlerCallRef, EventRef, void *); + static QMAC_PASCAL OSStatus globalAppleEventProcessor(const AppleEvent *, AppleEvent *, long); + static QMAC_PASCAL void qt_context_timer_callbk(EventLoopTimerRef, void *); + static QMAC_PASCAL void qt_select_timer_callbk(EventLoopTimerRef, void *); + static bool qt_mac_apply_settings(); + friend class QMacInputMethod; + friend QMAC_PASCAL OSStatus qt_window_event(EventHandlerCallRef, EventRef, void *); + friend void qt_mac_update_os_settings(); + friend bool qt_set_socket_handler( int, int, QObject *, bool); + friend void qt_mac_destroy_widget(QWidget *); + friend void qt_init(int *, char **, QApplication::Type); +#endif + +#if defined(Q_WS_X11) +private slots: + void postIMEvent( QObject *receiver, QIMEvent *event ); +#endif + +private: +#ifdef QT_THREAD_SUPPORT + static QMutex *qt_mutex; +#endif // QT_THREAD_SUPPORT + + int app_argc; + char **app_argv; + bool quit_now; + int quit_code; + static QStyle *app_style; + static int app_cspec; +#ifndef QT_NO_PALETTE + static QPalette *app_pal; +#endif + static QFont *app_font; +#ifndef QT_NO_CURSOR + static QCursor *app_cursor; +#endif + static QEventLoop* eventloop; + static int app_tracking; + static bool is_app_running; + static bool is_app_closing; + static bool app_exit_loop; + static int loop_level; + static QWidget *main_widget; + static QWidget *focus_widget; + static QWidget *active_window; + static bool obey_desktop_settings; + static int cursor_flash_time; + static int mouse_double_click_time; + static int wheel_scroll_lines; + static int composedUnicode; // Value, meta-composed character + + static bool animate_ui; + static bool animate_menu; + static bool animate_tooltip; + static bool animate_combo; + static bool fade_menu; + static bool fade_tooltip; + static bool animate_toolbox; + static bool widgetCount; // Coupled with -widgetcount switch + static bool metaComposeUnicode; + + QValueList<QTranslator*> *translators; +#ifndef QT_NO_SESSIONMANAGER + QSessionManager *session_manager; + QString session_id; + static QString* session_key; + bool is_session_restored; +#endif +#if defined(Q_WS_X11) +#if !defined (QT_NO_STYLE) + static void x11_initialize_style(); +#endif + static QString defaultIM; // default input method's name in this application. +#endif + + static QSize app_strut; +#ifndef QT_NO_COMPONENT + static QStringList *app_libpaths; +#endif + static QAsciiDict<QPalette> *app_palettes; + static QAsciiDict<QFont> *app_fonts; + + static QWidgetList *popupWidgets; + bool inPopupMode() const; + void closePopup( QWidget *popup ); + void openPopup( QWidget *popup ); + void setActiveWindow( QWidget* act ); + + static bool sendSpontaneousEvent( QObject *receiver, QEvent *event ); + static void removePostedEvent( QEvent * ); + static void removePostedEvents( QObject *receiver, int event_type ); + + friend class QWidget; + friend class QETWidget; + friend class QDialog; + friend class QAccelManager; + friend class QEvent; + friend class QTranslator; + friend class QEventLoop; + friend Q_EXPORT void qt_ucm_initialize( QApplication * ); +#if defined(Q_WS_WIN) + friend bool qt_sendSpontaneousEvent( QObject*, QEvent* ); +#endif +#if defined(Q_WS_QWS) + friend class QInputContext; +#endif +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QApplication( const QApplication & ); + QApplication &operator=( const QApplication & ); +#endif +}; + +inline int QApplication::argc() const +{ + return app_argc; +} + +inline char **QApplication::argv() const +{ + return app_argv; +} + +#if defined(Q_WS_QWS) +inline void QApplication::setArgs(int c, char **v) +{ + app_argc = c; + app_argv = v; +} +#endif + +#ifndef QT_NO_CURSOR +inline QCursor *QApplication::overrideCursor() +{ + return app_cursor; +} +#endif +inline bool QApplication::hasGlobalMouseTracking() +{ + return app_tracking > 0; +} + +inline QWidget *QApplication::mainWidget() const +{ + return main_widget; +} + +inline QWidget *QApplication::focusWidget() const +{ + return focus_widget; +} + +inline QWidget *QApplication::activeWindow() const +{ + return active_window; +} + +inline QWidget *QApplication::widgetAt( const QPoint &p, bool child ) +{ + return widgetAt( p.x(), p.y(), child ); +} + +inline bool QApplication::inPopupMode() const +{ + return popupWidgets != 0; +} +#ifndef QT_NO_SESSIONMANAGER +inline bool QApplication::isSessionRestored() const +{ + return is_session_restored; +} + +inline QString QApplication::sessionId() const +{ + return session_id; +} + +inline QString QApplication::sessionKey() const +{ + return session_key ? *session_key : QString::null; +} +#endif +inline QSize QApplication::globalStrut() +{ + return app_strut; +} + +inline bool QApplication::sendEvent( QObject *receiver, QEvent *event ) +{ if ( event ) event->spont = FALSE; return qApp ? qApp->notify( receiver, event ) : FALSE; } + +inline bool QApplication::sendSpontaneousEvent( QObject *receiver, QEvent *event ) +{ if ( event ) event->spont = TRUE; return qApp ? qApp->notify( receiver, event ) : FALSE; } + +#ifdef QT_NO_TRANSLATION +// Simple versions +inline QString QApplication::translate( const char *, const char *sourceText, + const char *, Encoding encoding ) const +{ +#ifndef QT_NO_TEXTCODEC + if ( encoding == UnicodeUTF8 ) + return QString::fromUtf8( sourceText ); + else +#endif + return QString::fromLatin1( sourceText ); +} +#endif + +inline int QApplication::horizontalAlignment( int align ) +{ + align &= AlignHorizontal_Mask; + if ( align == AlignAuto ) { + if ( reverseLayout() ) + align = AlignRight; + else + align = AlignLeft; + } + return align; +} + +#endif // QAPPLICATION_H + diff --git a/src/kernel/qapplication_p.h b/src/kernel/qapplication_p.h new file mode 100644 index 0000000..9f7382d --- /dev/null +++ b/src/kernel/qapplication_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Definition of some Qt private functions. +** +** Created : 000228 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QAPPLICATION_P_H +#define QAPPLICATION_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qapplication_*.cpp, qwidget*.cpp, qcolor_x11.cpp, qfiledialog.cpp +// and many other. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +class QWidget; +class QObject; +class QClipboard; +class QKeyEvent; +class QMouseEvent; +class QWheelEvent; + +extern Q_EXPORT bool qt_modal_state(); +extern Q_EXPORT void qt_enter_modal( QWidget* ); +extern Q_EXPORT void qt_leave_modal( QWidget* ); + +extern bool qt_is_gui_used; +#ifndef QT_NO_CLIPBOARD +extern QClipboard *qt_clipboard; +#endif + +#if defined (Q_OS_WIN32) || defined (Q_OS_CYGWIN) +extern Qt::WindowsVersion qt_winver; +const int QT_TABLET_NPACKETQSIZE = 128; +# ifdef Q_OS_TEMP + extern DWORD qt_cever; +# endif +#elif defined (Q_OS_MAC) +extern Qt::MacintoshVersion qt_macver; +#endif + +#if defined (Q_WS_X11) +extern int qt_ncols_option; +#endif + + +extern void qt_dispatchEnterLeave( QWidget*, QWidget* ); +extern bool qt_tryModalHelper( QWidget *, QWidget ** = 0 ); + +#endif diff --git a/src/kernel/qapplication_x11.cpp b/src/kernel/qapplication_x11.cpp new file mode 100644 index 0000000..e72bd63 --- /dev/null +++ b/src/kernel/qapplication_x11.cpp @@ -0,0 +1,6653 @@ +/**************************************************************************** +** +** Implementation of X11 startup routines and event handling +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +// ### 4.0: examine Q_EXPORT's below. The respective symbols had all +// been in use (e.g. in the KDE wm ) before the introduction of a version +// map. One might want to turn some of them into propert public API and +// provide a proper alternative for others. See also the exports in +// qapplication_win.cpp which suggest a unification. + +// ### needed for solaris-g++ in beta5 +#define QT_CLEAN_NAMESPACE + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +// Solaris redefines connect -> __xnet_connect with _XOPEN_SOURCE_EXTENDED. +#if defined(connect) +# undef connect +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +#include "qapplication.h" +#include "qapplication_p.h" +#include "qcolor_p.h" +#include "qcursor.h" +#include "qwidget.h" +#include "qwidget_p.h" +#include "qobjectlist.h" +#include "qwidgetlist.h" +#include "qwidgetintdict.h" +#include "qbitarray.h" +#include "qpainter.h" +#include "qpixmapcache.h" +#include "qdatetime.h" +#include "qtextcodec.h" +#include "qdatastream.h" +#include "qbuffer.h" +#include "qsocketnotifier.h" +#include "qsessionmanager.h" +#include "qvaluelist.h" +#include "qdict.h" +#include "qguardedptr.h" +#include "qclipboard.h" +#include "qwhatsthis.h" // ######## dependency +#include "qsettings.h" +#include "qstylefactory.h" +#include "qfileinfo.h" + +// Input method stuff - UNFINISHED +#ifndef QT_NO_IM +#include "qinputcontext.h" +#endif // QT_NO_IM +#include "qinternal_p.h" // shared double buffer cleanup + +#if defined(QT_THREAD_SUPPORT) +# include "qthread.h" +#endif + +#if defined(QT_DEBUG) && defined(Q_OS_LINUX) +# include "qfile.h" +#endif + +#include "qt_x11_p.h" + +#if !defined(QT_NO_XFTFREETYPE) +// XFree86 4.0.3 implementation is missing XftInitFtLibrary forward +extern "C" Bool XftInitFtLibrary(void); +#endif + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include <locale.h> +#include <cstdlib> + +//#define X_NOT_BROKEN +#ifdef X_NOT_BROKEN +// Some X libraries are built with setlocale #defined to _Xsetlocale, +// even though library users are then built WITHOUT such a definition. +// This creates a problem - Qt might setlocale() one value, but then +// X looks and doesn't see the value Qt set. The solution here is to +// implement _Xsetlocale just in case X calls it - redirecting it to +// the real libC version. +// +# ifndef setlocale +extern "C" char *_Xsetlocale(int category, const char *locale); +char *_Xsetlocale(int category, const char *locale) +{ + //qDebug("_Xsetlocale(%d,%s),category,locale"); + return setlocale(category,locale); +} +# endif // setlocale +#endif // X_NOT_BROKEN + + +// resolve the conflict between X11's FocusIn and QEvent::FocusIn +const int XFocusOut = FocusOut; +const int XFocusIn = FocusIn; +#undef FocusOut +#undef FocusIn + +const int XKeyPress = KeyPress; +const int XKeyRelease = KeyRelease; +#undef KeyPress +#undef KeyRelease + + +// Fix old X libraries +#ifndef XK_KP_Home +#define XK_KP_Home 0xFF95 +#endif +#ifndef XK_KP_Left +#define XK_KP_Left 0xFF96 +#endif +#ifndef XK_KP_Up +#define XK_KP_Up 0xFF97 +#endif +#ifndef XK_KP_Right +#define XK_KP_Right 0xFF98 +#endif +#ifndef XK_KP_Down +#define XK_KP_Down 0xFF99 +#endif +#ifndef XK_KP_Prior +#define XK_KP_Prior 0xFF9A +#endif +#ifndef XK_KP_Next +#define XK_KP_Next 0xFF9B +#endif +#ifndef XK_KP_End +#define XK_KP_End 0xFF9C +#endif +#ifndef XK_KP_Insert +#define XK_KP_Insert 0xFF9E +#endif +#ifndef XK_KP_Delete +#define XK_KP_Delete 0xFF9F +#endif + + +/***************************************************************************** + Internal variables and functions + *****************************************************************************/ +static const char *appName; // application name +static const char *appClass; // application class +static const char *appFont = 0; // application font +static const char *appBGCol = 0; // application bg color +static const char *appFGCol = 0; // application fg color +static const char *appBTNCol = 0; // application btn color +static const char *mwGeometry = 0; // main widget geometry +static const char *mwTitle = 0; // main widget title +//Ming-Che 10/10 +char *qt_ximServer = 0; // XIM Server will connect to +static bool mwIconic = FALSE; // main widget iconified +//Ming-Che 10/10 +static Display *appDpy = 0; // X11 application display +static char *appDpyName = 0; // X11 display name +static bool appForeignDpy = FALSE; // we didn't create display +static bool appSync = FALSE; // X11 synchronization +#if defined(QT_DEBUG) +static bool appNoGrab = FALSE; // X11 grabbing enabled +static bool appDoGrab = FALSE; // X11 grabbing override (gdb) +#endif +static int appScreen; // X11 screen number +static int appScreenCount; // X11 screen count +static bool app_save_rootinfo = FALSE; // save root info +static bool app_do_modal = FALSE; // modal mode +static Window curWin = 0; // current window + +static GC* app_gc_ro = 0; // read-only GC +static GC* app_gc_tmp = 0; // temporary GC +static GC* app_gc_ro_m = 0; // read-only GC (monochrome) +static GC* app_gc_tmp_m = 0; // temporary GC (monochrome) +// symbols needed by extern QXEmbed class +Q_EXPORT Atom qt_wm_protocols = 0; // window manager protocols +Q_EXPORT Atom qt_wm_delete_window = 0; // delete window protocol +Q_EXPORT Atom qt_wm_take_focus = 0; // take focus window protocol + +Atom qt_qt_scrolldone = 0; // scroll synchronization +Atom qt_net_wm_context_help = 0; // context help +Atom qt_net_wm_ping = 0; // _NET_WM_PING protocol + +static Atom qt_xsetroot_id = 0; +Atom qt_xa_clipboard = 0; +Atom qt_selection_property = 0; +Atom qt_clipboard_sentinel = 0; +Atom qt_selection_sentinel = 0; +Q_EXPORT Atom qt_wm_state = 0; +Atom qt_wm_change_state = 0; +static Atom qt_settings_timestamp = 0; // Qt >=3 settings timestamp +static Atom qt_input_encoding = 0; // Qt desktop properties +static Atom qt_resource_manager = 0; // X11 Resource manager +Atom qt_sizegrip = 0; // sizegrip +Atom qt_wm_client_leader = 0; +Q_EXPORT Atom qt_window_role = 0; +Q_EXPORT Atom qt_sm_client_id = 0; +Atom qt_xa_motif_wm_hints = 0; +Atom qt_cde_running = 0; +Atom qt_kwin_running = 0; +Atom qt_kwm_running = 0; +Atom qt_gbackground_properties = 0; +Atom qt_x_incr = 0; +Atom qt_utf8_string = 0; + +// detect broken window managers +Atom qt_sgi_desks_manager = 0; +bool qt_broken_wm = FALSE; +static void qt_detect_broken_window_manager(); + +// NET WM support +Atom qt_net_supported = 0; +Atom qt_net_wm_name = 0; +Atom qt_net_wm_icon_name = 0; +Atom qt_net_virtual_roots = 0; +Atom qt_net_workarea = 0; +Atom qt_net_wm_state = 0; +Atom qt_net_wm_state_modal = 0; +Atom qt_net_wm_state_max_v = 0; +Atom qt_net_wm_state_max_h = 0; +Atom qt_net_wm_state_fullscreen = 0; +Atom qt_net_wm_state_above = 0; +Atom qt_net_wm_window_type = 0; +Atom qt_net_wm_window_type_normal = 0; +Atom qt_net_wm_window_type_dialog = 0; +Atom qt_net_wm_window_type_toolbar = 0; +Atom qt_net_wm_window_type_menu = 0; +Atom qt_net_wm_window_type_utility = 0; +Atom qt_net_wm_window_type_splash = 0; +Atom qt_net_wm_window_type_override = 0; // KDE extension +Atom qt_net_wm_window_type_dropdown_menu = 0; +Atom qt_net_wm_window_type_popup_menu = 0; +Atom qt_net_wm_window_type_tooltip = 0; +Atom qt_net_wm_window_type_combo = 0; +Atom qt_net_wm_window_type_dnd = 0; +Atom qt_net_wm_frame_strut = 0; // KDE extension +Atom qt_net_wm_state_stays_on_top = 0; // KDE extension +Atom qt_net_wm_pid = 0; +Atom qt_net_wm_user_time = 0; +Atom qt_net_wm_full_placement = 0; // KDE extension +// Enlightenment support +Atom qt_enlightenment_desktop = 0; + +// window managers list of supported "stuff" +Atom *qt_net_supported_list = 0; +// list of virtual root windows +Window *qt_net_virtual_root_list = 0; + + +// X11 SYNC support +#ifndef QT_NO_XSYNC +Atom qt_net_wm_sync_request_counter = 0; +Atom qt_net_wm_sync_request = 0; +#endif + +// client leader window +Window qt_x11_wm_client_leader = 0; + +// function to update the workarea of the screen - in qdesktopwidget_x11.cpp +extern void qt_desktopwidget_update_workarea(); + +// current focus model +static const int FocusModel_Unknown = -1; +static const int FocusModel_Other = 0; +static const int FocusModel_PointerRoot = 1; +static int qt_focus_model = -1; + +#ifndef QT_NO_XRANDR +// TRUE if Qt is compiled w/ XRandR support and XRandR exists on the connected +// Display +bool qt_use_xrandr = FALSE; +static int xrandr_eventbase; +#endif + +// TRUE if Qt is compiled w/ XRender support and XRender exists on the connected +// Display +Q_EXPORT bool qt_use_xrender = FALSE; + +#ifndef QT_NO_XSYNC +// True if SYNC extension exists on the connected display +bool qt_use_xsync = FALSE; +static int xsync_eventbase; +static int xsync_errorbase; +#endif + +// modifier masks for alt/meta - detected when the application starts +static long qt_alt_mask = 0; +static long qt_meta_mask = 0; +// modifier mask to remove mode switch from modifiers that have alt/meta set +// this problem manifests itself on HP/UX 10.20 at least, and without it +// modifiers do not work at all... +static long qt_mode_switch_remove_mask = 0; + +// flags for extensions for special Languages, currently only for RTL languages +static bool qt_use_rtl_extensions = FALSE; +bool qt_hebrew_keyboard_hack = FALSE; + +static Window mouseActWindow = 0; // window where mouse is +static int mouseButtonPressed = 0; // last mouse button pressed +static int mouseButtonState = 0; // mouse button state +static Time mouseButtonPressTime = 0; // when was a button pressed +static short mouseXPos, mouseYPos; // mouse pres position in act window +static short mouseGlobalXPos, mouseGlobalYPos; // global mouse press position + +extern QWidgetList *qt_modal_stack; // stack of modal widgets +static bool ignoreNextMouseReleaseEvent = FALSE; // ignore the next mouse release + // event if return from a modal + // widget + +static QWidget *popupButtonFocus = 0; +static QWidget *popupOfPopupButtonFocus = 0; +static bool popupCloseDownMode = FALSE; +static bool popupGrabOk; + +static bool sm_blockUserInput = FALSE; // session management + +int qt_xfocusout_grab_counter = 0; + +#if defined (QT_TABLET_SUPPORT) +// since XInput event classes aren't created until we actually open an XInput +// device, here is a static list that we will use later on... +const int INVALID_EVENT = -1; +const int TOTAL_XINPUT_EVENTS = 7; + +XDevice *devStylus = NULL; +XDevice *devEraser = NULL; +XEventClass event_list_stylus[TOTAL_XINPUT_EVENTS]; +XEventClass event_list_eraser[TOTAL_XINPUT_EVENTS]; + +int qt_curr_events_stylus = 0; +int qt_curr_events_eraser = 0; + +// well, luckily we only need to do this once. +static int xinput_motion = INVALID_EVENT; +static int xinput_key_press = INVALID_EVENT; +static int xinput_key_release = INVALID_EVENT; +static int xinput_button_press = INVALID_EVENT; +static int xinput_button_release = INVALID_EVENT; + +// making this assumption on XFree86, since we can only use 1 device, +// the pressure for the eraser and the stylus should be the same, if they aren't +// well, they certainly have a strange pen then... +static int max_pressure; +extern bool chokeMouse; +#endif + +// last timestamp read from QSettings +static uint appliedstamp = 0; + + +typedef int (*QX11EventFilter) (XEvent*); +QX11EventFilter qt_set_x11_event_filter(QX11EventFilter filter); + +static QX11EventFilter qt_x11_event_filter = 0; +Q_EXPORT QX11EventFilter qt_set_x11_event_filter(QX11EventFilter filter) +{ + QX11EventFilter old_filter = qt_x11_event_filter; + qt_x11_event_filter = filter; + return old_filter; +} +static bool qt_x11EventFilter( XEvent* ev ) +{ + if ( qt_x11_event_filter && qt_x11_event_filter( ev ) ) + return TRUE; + return qApp->x11EventFilter( ev ); +} + + + + + +#if !defined(QT_NO_XIM) +//XIM qt_xim = 0; +XIMStyle qt_xim_style = 0; +XIMStyle qt_xim_preferred_style = 0; +static XIMStyle xim_default_style = XIMPreeditCallbacks | XIMStatusNothing; +#endif + +int qt_ximComposingKeycode=0; +QTextCodec * qt_input_mapper = 0; + +Q_EXPORT Time qt_x_time = CurrentTime; +Q_EXPORT Time qt_x_user_time = CurrentTime; +extern bool qt_check_clipboard_sentinel(); //def in qclipboard_x11.cpp +extern bool qt_check_selection_sentinel(); //def in qclipboard_x11.cpp + +static void qt_save_rootinfo(); +bool qt_try_modal( QWidget *, XEvent * ); + +int qt_ncols_option = 216; // used in qcolor_x11.cpp +int qt_visual_option = -1; +bool qt_cmap_option = FALSE; +QWidget *qt_button_down = 0; // widget got last button-down + +extern bool qt_tryAccelEvent( QWidget*, QKeyEvent* ); // def in qaccel.cpp + +struct QScrollInProgress { + static long serial; + QScrollInProgress( QWidget* w, int x, int y ) : + id( serial++ ), scrolled_widget( w ), dx( x ), dy( y ) {} + long id; + QWidget* scrolled_widget; + int dx, dy; +}; +long QScrollInProgress::serial=0; +static QPtrList<QScrollInProgress> *sip_list = 0; + + +// stuff in qt_xdnd.cpp +// setup +extern void qt_xdnd_setup(); +// x event handling +extern void qt_handle_xdnd_enter( QWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_position( QWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_status( QWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_leave( QWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_drop( QWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_finished( QWidget *, const XEvent *, bool ); +extern void qt_xdnd_handle_selection_request( const XSelectionRequestEvent * ); +extern bool qt_xdnd_handle_badwindow(); + +extern void qt_motifdnd_handle_msg( QWidget *, const XEvent *, bool ); +extern void qt_x11_motifdnd_init(); + +// client message atoms +extern Atom qt_xdnd_enter; +extern Atom qt_xdnd_position; +extern Atom qt_xdnd_status; +extern Atom qt_xdnd_leave; +extern Atom qt_xdnd_drop; +extern Atom qt_xdnd_finished; +// xdnd selection atom +extern Atom qt_xdnd_selection; +extern bool qt_xdnd_dragging; + +// gui or non-gui from qapplication.cpp +extern bool qt_is_gui_used; +extern bool qt_app_has_font; + +static bool qt_x11_cmdline_font = false; + + +extern bool qt_resolve_symlinks; // from qapplication.cpp + +// Paint event clipping magic +extern void qt_set_paintevent_clipping( QPaintDevice* dev, const QRegion& region); +extern void qt_clear_paintevent_clipping(); + + +// Palette handling +extern QPalette *qt_std_pal; +extern void qt_create_std_palette(); + +void qt_x11_intern_atom( const char *, Atom * ); + +static QPtrList<QWidget>* deferred_map_list = 0; +static void qt_deferred_map_cleanup() +{ + delete deferred_map_list; + deferred_map_list = 0; +} +void qt_deferred_map_add( QWidget* w) +{ + if ( !deferred_map_list ) { + deferred_map_list = new QPtrList<QWidget>; + qAddPostRoutine( qt_deferred_map_cleanup ); + } + deferred_map_list->append( w ); +} +void qt_deferred_map_take( QWidget* w ) +{ + if (deferred_map_list ) { + deferred_map_list->remove( w ); + } +} +bool qt_deferred_map_contains( QWidget* w ) +{ + if (!deferred_map_list) + return FALSE; + else + return deferred_map_list->contains( w ); +} + + +class QETWidget : public QWidget // event translator widget +{ +public: + void setWState( WFlags f ) { QWidget::setWState(f); } + void clearWState( WFlags f ) { QWidget::clearWState(f); } + void setWFlags( WFlags f ) { QWidget::setWFlags(f); } + void clearWFlags( WFlags f ) { QWidget::clearWFlags(f); } + bool translateMouseEvent( const XEvent * ); + bool translateKeyEventInternal( const XEvent *, int& count, QString& text, int& state, char& ascii, int &code, QEvent::Type &type, bool willRepeat=FALSE, bool statefulTranslation=TRUE ); + bool translateKeyEvent( const XEvent *, bool grab ); + bool translatePaintEvent( const XEvent * ); + bool translateConfigEvent( const XEvent * ); + bool translateCloseEvent( const XEvent * ); + bool translateScrollDoneEvent( const XEvent * ); + bool translateWheelEvent( int global_x, int global_y, int delta, int state, Orientation orient ); +#if defined (QT_TABLET_SUPPORT) + bool translateXinputEvent( const XEvent* ); +#endif + bool translatePropertyEvent(const XEvent *); +}; + + + + +// ************************************************************************ +// Input Method support +// ************************************************************************ + +/*! + An identifier name of the default input method. +*/ +QString QApplication::defaultIM = "imsw-multi"; + + +/*! + This function handles the query about location of the widget + holding the QInputContext instance for widget \a w. + + The input context is used for text input to widget \a w. By + default, it returns the top-level widget of \a w. + + If you want to change the mapping of widget \w to QInputContext + instance, reimplement both this function and + QApplication::icHolderWidgets(). For example, suppose a tabbed web + browser. The browser should allocate a input context per tab + widget because users may switch the tabs and input a new text + during previous input contexts live. + + See also 'Sharing input context between text widgets' and 'Preedit + preservation' section of the class description of QInputContext. + + \sa QInputContext, icHolderWidgets() +*/ +QWidget *QApplication::locateICHolderWidget( QWidget *w ) +{ + return w->topLevelWidget(); +} + + +/*! + This function returns all widgets holding QInputContext. + + By default, This function returns top-level widgets. So if you + want to change the mapping of a widget to QInputContext instance, + you must override this function and locateICHolderWidget(). + + \sa locateICHolderWidget() +*/ +QWidgetList *QApplication::icHolderWidgets() +{ + return QApplication::topLevelWidgets(); +} + + +/*! + This function replaces all QInputContext instances in the + application. The function's argument is the identifier name of + the newly selected input method. +*/ +void QApplication::changeAllInputContext( const QString &identifierName ) +{ + QWidgetList *list = qApp->icHolderWidgets(); + QWidgetListIt it(*list); + while(it.current()) { + it.current()->changeInputContext( identifierName ); + ++it; + } + delete list; + + // defaultIM = identifierName ; // Change of defaultIM -- default input method -- may be enabled. +} + + +/*! + \internal + This is an internal function, you should never call this. + + \sa QInputContext::imEventGenerated() +*/ +void QApplication::postIMEvent( QObject *receiver, QIMEvent *event ) +{ + if ( event->type() == QEvent::IMCompose ) { + // enable event compression to reduce preedit flicker on fast + // typing + postEvent( receiver, event ); + } else { + // cancel queued preedit update + if ( event->type() == QEvent::IMEnd ) + removePostedEvents( receiver, QEvent::IMCompose ); + + // to avoid event receiving order inversion between QKeyEvent + // and QIMEvent, we must send IMStart and IMEnd via + // sendEvent(). + sendEvent( receiver, event ); + delete event; + } +} + + +/*! + This function returns the identifier name of the default input + method in this Application. The value is identical to the value of + QApplication::defaultIM. +*/ +QString QApplication::defaultInputMethod() +{ + return QApplication::defaultIM; +} + + +#if !defined(QT_NO_IM_EXTENSIONS) +/*! \internal + Creates the application input method. +*/ +void QApplication::create_im() +{ +#ifndef QT_NO_XIM + if ( ! qt_xim_preferred_style ) // no configured input style, use the default + qt_xim_preferred_style = xim_default_style; +#endif // QT_NO_XIM +} + + +/*! \internal + Closes the application input method. +*/ +void QApplication::close_im() +{ + QWidgetList *list = qApp->icHolderWidgets(); + QWidgetListIt it(*list); + while(it.current()) { + it.current()->destroyInputContext(); + ++it; + } + delete list; +} + +#else + +/*! \internal + Creates the application input method. +*/ +void QApplication::create_xim() +{ +#ifndef QT_NO_XIM + if ( ! qt_xim_preferred_style ) // no configured input style, use the default + qt_xim_preferred_style = xim_default_style; +#endif // QT_NO_XIM + + QWidgetList *list= qApp->topLevelWidgets(); + QWidgetListIt it(*list); + QWidget * w; + while( (w=it.current()) != 0 ) { + ++it; + w->createTLSysExtra(); + } + delete list; +} + + + /*! \internal + Closes the application input method. + */ +void QApplication::close_xim() +{ +#ifndef QT_NO_XIM + // Calling XCloseIM gives a Purify FMR error + // XCloseIM( qt_xim ); + // We prefer a less serious memory leak + + // if ( qt_xim ) + // qt_xim = 0; + +#endif // QT_NO_XIM + QWidgetList *list = qApp->topLevelWidgets(); + QWidgetListIt it(*list); + while(it.current()) { + it.current()->destroyInputContext(); + ++it; + } + delete list; +} +#endif + +/***************************************************************************** + Default X error handlers + *****************************************************************************/ + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static bool x11_ignore_badwindow; +static bool x11_badwindow; + + // starts to ignore bad window errors from X +void qt_ignore_badwindow() +{ + x11_ignore_badwindow = TRUE; + x11_badwindow = FALSE; +} + + // ends ignoring bad window errors and returns whether an error + // had happen. +bool qt_badwindow() +{ + x11_ignore_badwindow = FALSE; + return x11_badwindow; +} + +static int (*original_x_errhandler)( Display *dpy, XErrorEvent * ); +static int (*original_xio_errhandler)( Display *dpy ); + +static int qt_x_errhandler( Display *dpy, XErrorEvent *err ) +{ + if ( err->error_code == BadWindow ) { + x11_badwindow = TRUE; + if ( err->request_code == 25 /* X_SendEvent */ && + qt_xdnd_handle_badwindow() ) + return 0; + if ( x11_ignore_badwindow ) + return 0; + } else if ( err->error_code == BadMatch && + err->request_code == 42 /* X_SetInputFocus */ ) { + return 0; + } + + char errstr[256]; + XGetErrorText( dpy, err->error_code, errstr, 256 ); + qWarning( "X Error: %s %d\n" + " Major opcode: %d\n" + " Minor opcode: %d\n" + " Resource id: 0x%lx", + errstr, err->error_code, + err->request_code, + err->minor_code, + err->resourceid ); + + // ### we really should distinguish between severe, non-severe and + // ### application specific errors + + return 0; +} + + +static int qt_xio_errhandler( Display * ) +{ + qWarning( "%s: Fatal IO error: client killed", appName ); + qApp = 0; + exit( 1 ); + //### give the application a chance for a proper shutdown instead, + //### exit(1) doesn't help. + return 0; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +// Memory leak: if the app exits before qt_init_internal(), this dict +// isn't released correctly. +static QAsciiDict<Atom> *atoms_to_be_created = 0; +static bool create_atoms_now = 0; + +/***************************************************************************** + qt_x11_intern_atom() - efficiently interns an atom, now or later. + + If the application is being initialized, this function stores the + adddress of the atom and qt_init_internal will do the actual work + quickly. If the application is running, the atom is created here. + + Neither argument may point to temporary variables. + *****************************************************************************/ + +void qt_x11_intern_atom( const char *name, Atom *result) +{ + if ( !name || !result || *result ) + return; + + if ( create_atoms_now ) { + *result = XInternAtom( appDpy, name, False ); + } else { + if ( !atoms_to_be_created ) { + atoms_to_be_created = new QAsciiDict<Atom>; + atoms_to_be_created->setAutoDelete( FALSE ); + } + atoms_to_be_created->insert( name, result ); + *result = 0; + } +} + + +static void qt_x11_process_intern_atoms() +{ + if ( atoms_to_be_created ) { +#if defined(XlibSpecificationRelease) && (XlibSpecificationRelease >= 6) + int i = atoms_to_be_created->count(); + Atom * res = (Atom *)malloc( i * sizeof( Atom ) ); + Atom ** resp = (Atom **)malloc( i * sizeof( Atom* ) ); + char ** names = (char **)malloc( i * sizeof(const char*)); + + i = 0; + QAsciiDictIterator<Atom> it( *atoms_to_be_created ); + while( it.current() ) { + res[i] = 0; + resp[i] = it.current(); + names[i] = qstrdup(it.currentKey()); + i++; + ++it; + } + XInternAtoms( appDpy, names, i, False, res ); + while( i ) { + i--; + delete [] names[i]; + if ( res[i] && resp[i] ) + *(resp[i]) = res[i]; + } + free( res ); + free( resp ); + free( names ); +#else + QAsciiDictIterator<Atom> it( *atoms_to_be_created ); + Atom * result; + const char * name; + while( (result = it.current()) != 0 ) { + name = it.currentKey(); + ++it; + *result = XInternAtom( appDpy, name, False ); + } +#endif + delete atoms_to_be_created; + atoms_to_be_created = 0; + create_atoms_now = TRUE; + } +} + + +/*! \internal + apply the settings to the application +*/ +bool QApplication::x11_apply_settings() +{ + if (! qt_std_pal) + qt_create_std_palette(); + + Atom type; + int format; + long offset = 0; + unsigned long nitems, after = 1; + unsigned char *data = 0; + QDateTime timestamp, settingsstamp; + bool update_timestamp = FALSE; + + if (XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow( 0 ), + qt_settings_timestamp, 0, 0, + False, AnyPropertyType, &type, &format, &nitems, + &after, &data) == Success && format == 8) { + if (data) + XFree(data); + + QBuffer ts; + ts.open(IO_WriteOnly); + + while (after > 0) { + XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow( 0 ), + qt_settings_timestamp, + offset, 1024, False, AnyPropertyType, + &type, &format, &nitems, &after, &data); + if (format == 8) { + ts.writeBlock((const char *) data, nitems); + offset += nitems / 4; + } + + XFree(data); + } + + QDataStream d(ts.buffer(), IO_ReadOnly); + d >> timestamp; + } + + QSettings settings; + settingsstamp = settings.lastModificationTime( "/qt/font" ); + if (! settingsstamp.isValid()) + return FALSE; + + if ( appliedstamp && appliedstamp == settingsstamp.toTime_t() ) + return TRUE; + appliedstamp = settingsstamp.toTime_t(); + + if (! timestamp.isValid() || settingsstamp > timestamp) + update_timestamp = TRUE; + + /* + Qt settings. This is now they are written into the datastream. + + /qt/Palette/ * - QPalette + /qt/font - QFont + /qt/libraryPath - QStringList + /qt/style - QString + /qt/doubleClickInterval - int + /qt/cursorFlashTime - int + /qt/wheelScrollLines - int + /qt/colorSpec - QString + /qt/defaultCodec - QString + /qt/globalStrut - QSize + /qt/GUIEffects - QStringList + /qt/Font Substitutions/ * - QStringList + /qt/Font Substitutions/... - QStringList + */ + + QString str; + QStringList strlist; + int i, num; + QPalette pal(QApplication::palette()); + strlist = settings.readListEntry("/qt/Palette/active"); + if (strlist.count() == QColorGroup::NColorRoles) { + for (i = 0; i < QColorGroup::NColorRoles; i++) + pal.setColor(QPalette::Active, (QColorGroup::ColorRole) i, + QColor(strlist[i])); + } + strlist = settings.readListEntry("/qt/Palette/inactive"); + if (strlist.count() == QColorGroup::NColorRoles) { + for (i = 0; i < QColorGroup::NColorRoles; i++) + pal.setColor(QPalette::Inactive, (QColorGroup::ColorRole) i, + QColor(strlist[i])); + } + strlist = settings.readListEntry("/qt/Palette/disabled"); + if (strlist.count() == QColorGroup::NColorRoles) { + for (i = 0; i < QColorGroup::NColorRoles; i++) + pal.setColor(QPalette::Disabled, (QColorGroup::ColorRole) i, + QColor(strlist[i])); + } + + // workaround for KDE 3.0, which messes up the buttonText value of + // the disabled palette in QSettings + if ( pal.disabled().buttonText() == pal.active().buttonText() ) { + pal.setColor( QPalette::Disabled, QColorGroup::ButtonText, + pal.disabled().foreground() ); + } + + if (pal != *qt_std_pal && pal != QApplication::palette()) { + QApplication::setPalette(pal, TRUE); + *qt_std_pal = pal; + } + + QFont font(QApplication::font()); + if ( !qt_app_has_font && !qt_x11_cmdline_font ) { + // read new font + str = settings.readEntry("/qt/font"); + if (! str.isNull() && ! str.isEmpty()) { + font.fromString(str); + + if (font != QApplication::font()) + QApplication::setFont(font, TRUE); + } + } + + // read library (ie. plugin) path list + QString libpathkey = + QString("/qt/%1.%2/libraryPath").arg( QT_VERSION >> 16 ).arg( (QT_VERSION & 0xff00 ) >> 8 ); + QStringList pathlist = settings.readListEntry(libpathkey, ':'); + if (! pathlist.isEmpty()) { + QStringList::ConstIterator it = pathlist.begin(); + while (it != pathlist.end()) + QApplication::addLibraryPath(*it++); + } + + // read new QStyle + extern bool qt_explicit_app_style; // defined in qapplication.cpp + QString stylename = settings.readEntry( "/qt/style" ); + if ( !stylename.isEmpty() && !qt_explicit_app_style ) { + QApplication::setStyle( stylename ); + // took the style from the user settings, so mark the explicit flag FALSE + qt_explicit_app_style = FALSE; + } + + num = + settings.readNumEntry("/qt/doubleClickInterval", + QApplication::doubleClickInterval()); + QApplication::setDoubleClickInterval(num); + + num = + settings.readNumEntry("/qt/cursorFlashTime", + QApplication::cursorFlashTime()); + QApplication::setCursorFlashTime(num); + + num = + settings.readNumEntry("/qt/wheelScrollLines", + QApplication::wheelScrollLines()); + QApplication::setWheelScrollLines(num); + + QString colorspec = settings.readEntry("/qt/colorSpec", "default"); + if (colorspec == "normal") + QApplication::setColorSpec(QApplication::NormalColor); + else if (colorspec == "custom") + QApplication::setColorSpec(QApplication::CustomColor); + else if (colorspec == "many") + QApplication::setColorSpec(QApplication::ManyColor); + else if (colorspec != "default") + colorspec = "default"; + + QString defaultcodec = settings.readEntry("/qt/defaultCodec", "none"); + if (defaultcodec != "none") { + QTextCodec *codec = QTextCodec::codecForName(defaultcodec); + if (codec) + qApp->setDefaultCodec(codec); + } + + QStringList strut = settings.readListEntry("/qt/globalStrut"); + if (! strut.isEmpty()) { + if (strut.count() == 2) { + QSize sz(strut[0].toUInt(), strut[1].toUInt()); + + if (sz.isValid()) + QApplication::setGlobalStrut(sz); + } + } + + QStringList effects = settings.readListEntry("/qt/GUIEffects"); + + QApplication::setEffectEnabled( Qt::UI_General, effects.contains("general") ); + QApplication::setEffectEnabled( Qt::UI_AnimateMenu, effects.contains("animatemenu") ); + QApplication::setEffectEnabled( Qt::UI_FadeMenu, effects.contains("fademenu") ); + QApplication::setEffectEnabled( Qt::UI_AnimateCombo, effects.contains("animatecombo") ); + QApplication::setEffectEnabled( Qt::UI_AnimateTooltip, effects.contains("animatetooltip") ); + QApplication::setEffectEnabled( Qt::UI_FadeTooltip, effects.contains("fadetooltip") ); + QApplication::setEffectEnabled( Qt::UI_AnimateToolBox, effects.contains("animatetoolbox") ); + + QStringList fontsubs = + settings.entryList("/qt/Font Substitutions"); + if (!fontsubs.isEmpty()) { + QStringList subs; + QString fam, skey; + QStringList::Iterator it = fontsubs.begin(); + while (it != fontsubs.end()) { + fam = (*it++); + skey = "/qt/Font Substitutions/" + fam; + subs = settings.readListEntry(skey); + QFont::insertSubstitutions(fam, subs); + } + } + + qt_broken_wm = + settings.readBoolEntry("/qt/brokenWindowManager", qt_broken_wm); + + qt_resolve_symlinks = + settings.readBoolEntry("/qt/resolveSymlinks", TRUE); + + qt_use_rtl_extensions = + settings.readBoolEntry("/qt/useRtlExtensions", FALSE); + +#ifndef QT_NO_XIM + if (qt_xim_preferred_style == 0) { + QString ximInputStyle = + settings.readEntry( "/qt/XIMInputStyle", + QObject::trUtf8( "On The Spot" ) ).lower(); + if ( ximInputStyle == "on the spot" ) + qt_xim_preferred_style = XIMPreeditCallbacks | XIMStatusNothing; + else if ( ximInputStyle == "over the spot" ) + qt_xim_preferred_style = XIMPreeditPosition | XIMStatusNothing; + else if ( ximInputStyle == "off the spot" ) + qt_xim_preferred_style = XIMPreeditArea | XIMStatusArea; + else if ( ximInputStyle == "root" ) + qt_xim_preferred_style = XIMPreeditNothing | XIMStatusNothing; + } +#endif + +#ifndef QT_NO_IM + /* + The identifier name of an input method is acquired from the + configuration file as a default. If a environment variable + "QT_IM_SWITCHER" is not empty it will overwrite the + configuration file. The "imsw-multi" becomes the default if the entry + is not configured. + */ + if ( getenv( "QT_IM_SWITCHER" ) ) + defaultIM = getenv( "QT_IM_SWITCHER" ); +#ifndef QT_NO_IM_EXTENSIONS + else + defaultIM = settings.readEntry( "/qt/DefaultInputMethodSwitcher", "imsw-multi" ); +#endif + + // defaultIM is restricted to be an IM-switcher. An IM-switcher + // has a 'imsw-' prefix + if ( ! defaultIM.startsWith( "imsw-" ) ) { + defaultIM = "imsw-multi"; + } +#endif + + if (update_timestamp) { + QBuffer stamp; + QDataStream s(stamp.buffer(), IO_WriteOnly); + s << settingsstamp; + + XChangeProperty(appDpy, QPaintDevice::x11AppRootWindow( 0 ), + qt_settings_timestamp, qt_settings_timestamp, 8, + PropModeReplace, (unsigned char *) stamp.buffer().data(), + stamp.buffer().size()); + } + + return TRUE; +} + + +// read the _QT_INPUT_ENCODING property and apply the settings to +// the application +static void qt_set_input_encoding() +{ + Atom type; + int format; + ulong nitems, after = 1; + const char *data; + + int e = XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow(), + qt_input_encoding, 0, 1024, + False, XA_STRING, &type, &format, &nitems, + &after, (unsigned char**)&data ); + if ( e != Success || !nitems || type == None ) { + // Always use the locale codec, since we have no examples of non-local + // XIMs, and since we cannot get a sensible answer about the encoding + // from the XIM. + qt_input_mapper = QTextCodec::codecForLocale(); + + } else { + if ( !qstricmp( data, "locale" ) ) + qt_input_mapper = QTextCodec::codecForLocale(); + else + qt_input_mapper = QTextCodec::codecForName( data ); + // make sure we have an input codec + if( !qt_input_mapper ) + qt_input_mapper = QTextCodec::codecForName( "ISO 8859-1" ); + } + if ( qt_input_mapper->mibEnum() == 11 ) // 8859-8 + qt_input_mapper = QTextCodec::codecForName( "ISO 8859-8-I"); + if( data ) + XFree( (char *)data ); +} + +// set font, foreground and background from x11 resources. The +// arguments may override the resource settings. +static void qt_set_x11_resources( const char* font = 0, const char* fg = 0, + const char* bg = 0, const char* button = 0 ) +{ + if ( !qt_std_pal ) + qt_create_std_palette(); + + QCString resFont, resFG, resBG, resEF, sysFont; + + QApplication::setEffectEnabled( Qt::UI_General, FALSE); + QApplication::setEffectEnabled( Qt::UI_AnimateMenu, FALSE); + QApplication::setEffectEnabled( Qt::UI_FadeMenu, FALSE); + QApplication::setEffectEnabled( Qt::UI_AnimateCombo, FALSE ); + QApplication::setEffectEnabled( Qt::UI_AnimateTooltip, FALSE ); + QApplication::setEffectEnabled( Qt::UI_FadeTooltip, FALSE ); + QApplication::setEffectEnabled( Qt::UI_AnimateToolBox, FALSE ); + + if ( QApplication::desktopSettingsAware() && !QApplication::x11_apply_settings() ) { + int format; + ulong nitems, after = 1; + QCString res; + long offset = 0; + Atom type = None; + + while (after > 0) { + uchar *data; + XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow( 0 ), + qt_resource_manager, + offset, 8192, False, AnyPropertyType, + &type, &format, &nitems, &after, + &data ); + res += (char*)data; + offset += 2048; // offset is in 32bit quantities... 8192/4 == 2048 + if ( data ) + XFree( (char *)data ); + } + + QCString key, value; + int l = 0, r; + QCString apn = appName; + QCString apc = appClass; + int apnl = apn.length(); + int apcl = apc.length(); + int resl = res.length(); + + while (l < resl) { + r = res.find( '\n', l ); + if ( r < 0 ) + r = resl; + while ( isspace((uchar) res[l]) ) + l++; + bool mine = FALSE; + if ( res[l] == '*' && + (res[l+1] == 'f' || res[l+1] == 'b' || res[l+1] == 'g' || + res[l+1] == 'F' || res[l+1] == 'B' || res[l+1] == 'G' || + res[l+1] == 's' || res[l+1] == 'S' ) ) { + // OPTIMIZED, since we only want "*[fbgs].." + + QCString item = res.mid( l, r - l ).simplifyWhiteSpace(); + int i = item.find( ":" ); + key = item.left( i ).stripWhiteSpace().mid(1).lower(); + value = item.right( item.length() - i - 1 ).stripWhiteSpace(); + mine = TRUE; + } else if ( res[l] == appName[0] || (appClass && res[l] == appClass[0]) ) { + if (res.mid(l,apnl) == apn && (res[l+apnl] == '.' || res[l+apnl] == '*')) { + QCString item = res.mid( l, r - l ).simplifyWhiteSpace(); + int i = item.find( ":" ); + key = item.left( i ).stripWhiteSpace().mid(apnl+1).lower(); + value = item.right( item.length() - i - 1 ).stripWhiteSpace(); + mine = TRUE; + } else if (res.mid(l,apcl) == apc && (res[l+apcl] == '.' || res[l+apcl] == '*')) { + QCString item = res.mid( l, r - l ).simplifyWhiteSpace(); + int i = item.find( ":" ); + key = item.left( i ).stripWhiteSpace().mid(apcl+1).lower(); + value = item.right( item.length() - i - 1 ).stripWhiteSpace(); + mine = TRUE; + } + } + + if ( mine ) { + if ( !font && key == "systemfont") + sysFont = value.left( value.findRev(':') ).copy(); + if ( !font && key == "font") + resFont = value.copy(); + else if ( !fg && key == "foreground" ) + resFG = value.copy(); + else if ( !bg && key == "background") + resBG = value.copy(); + else if ( key == "guieffects") + resEF = value.copy(); + // NOTE: if you add more, change the [fbg] stuff above + } + + l = r + 1; + } + } + if ( !sysFont.isEmpty() ) + resFont = sysFont; + if ( resFont.isEmpty() ) + resFont = font; + if ( resFG.isEmpty() ) + resFG = fg; + if ( resBG.isEmpty() ) + resBG = bg; + if ( (!qt_app_has_font || qt_x11_cmdline_font) && !resFont.isEmpty() ) { // set application font + QFont fnt; + fnt.setRawName( resFont ); + + // the font we get may actually be an alias for another font, + // so we reset the application font to the real font info. + if ( ! fnt.exactMatch() ) { + QFontInfo fontinfo( fnt ); + fnt.setFamily( fontinfo.family() ); + fnt.setRawMode( fontinfo.rawMode() ); + + if ( ! fnt.rawMode() ) { + fnt.setItalic( fontinfo.italic() ); + fnt.setWeight( fontinfo.weight() ); + fnt.setUnderline( fontinfo.underline() ); + fnt.setStrikeOut( fontinfo.strikeOut() ); + fnt.setStyleHint( fontinfo.styleHint() ); + + if ( fnt.pointSize() <= 0 && fnt.pixelSize() <= 0 ) + // size is all wrong... fix it + fnt.setPointSize( (int) ( ( fontinfo.pixelSize() * 72. / + (float) QPaintDevice::x11AppDpiY() ) + + 0.5 ) ); + } + } + + if ( fnt != QApplication::font() ) { + QApplication::setFont( fnt, TRUE ); + } + } + + if ( button || !resBG.isEmpty() || !resFG.isEmpty() ) {// set app colors + QColor btn; + QColor bg; + QColor fg; + if ( !resBG.isEmpty() ) + bg = QColor(QString(resBG)); + else + bg = qt_std_pal->active().background(); + if ( !resFG.isEmpty() ) + fg = QColor(QString(resFG)); + else + fg = qt_std_pal->active().foreground(); + if ( button ) + btn = QColor( button ); + else if ( !resBG.isEmpty() ) + btn = bg; + else + btn = qt_std_pal->active().button(); + + int h,s,v; + fg.hsv(&h,&s,&v); + QColor base = Qt::white; + bool bright_mode = FALSE; + if (v >= 255-50) { + base = btn.dark(150); + bright_mode = TRUE; + } + + QColorGroup cg( fg, btn, btn.light(), + btn.dark(), btn.dark(150), fg, Qt::white, base, bg ); + if (bright_mode) { + cg.setColor( QColorGroup::HighlightedText, base ); + cg.setColor( QColorGroup::Highlight, Qt::white ); + } else { + cg.setColor( QColorGroup::HighlightedText, Qt::white ); + cg.setColor( QColorGroup::Highlight, Qt::darkBlue ); + } + QColor disabled( (fg.red()+btn.red())/2, + (fg.green()+btn.green())/2, + (fg.blue()+btn.blue())/2); + QColorGroup dcg( disabled, btn, btn.light( 125 ), btn.dark(), btn.dark(150), + disabled, Qt::white, Qt::white, bg ); + if (bright_mode) { + dcg.setColor( QColorGroup::HighlightedText, base ); + dcg.setColor( QColorGroup::Highlight, Qt::white ); + } else { + dcg.setColor( QColorGroup::HighlightedText, Qt::white ); + dcg.setColor( QColorGroup::Highlight, Qt::darkBlue ); + } + QPalette pal( cg, dcg, cg ); + if ( pal != *qt_std_pal && pal != QApplication::palette() ) + QApplication::setPalette( pal, TRUE ); + *qt_std_pal = pal; + } + + if ( !resEF.isEmpty() ) { + QStringList effects = QStringList::split(" ",resEF); + QApplication::setEffectEnabled( Qt::UI_General, effects.contains("general") ); + QApplication::setEffectEnabled( Qt::UI_AnimateMenu, effects.contains("animatemenu") ); + QApplication::setEffectEnabled( Qt::UI_FadeMenu, effects.contains("fademenu") ); + QApplication::setEffectEnabled( Qt::UI_AnimateCombo, effects.contains("animatecombo") ); + QApplication::setEffectEnabled( Qt::UI_AnimateTooltip, effects.contains("animatetooltip") ); + QApplication::setEffectEnabled( Qt::UI_FadeTooltip, effects.contains("fadetooltip") ); + QApplication::setEffectEnabled( Qt::UI_AnimateToolBox, effects.contains("animatetoolbox") ); + } +} + + +static void qt_detect_broken_window_manager() +{ + Atom type; + int format; + ulong nitems, after; + uchar *data = 0; + + // look for SGI's 4Dwm + int e = XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow(), + qt_sgi_desks_manager, 0, 1, False, XA_WINDOW, + &type, &format, &nitems, &after, &data); + if (data) + XFree(data); + + if (e == Success && type == XA_WINDOW && format == 32 && nitems == 1 && after == 0) { + // detected SGI 4Dwm + qt_broken_wm = TRUE; + } +} + + +// update the supported array +void qt_get_net_supported() +{ + Atom type; + int format; + long offset = 0; + unsigned long nitems, after; + unsigned char *data = 0; + + int e = XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow(), + qt_net_supported, 0, 0, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + if (data) + XFree(data); + + if (qt_net_supported_list) + delete [] qt_net_supported_list; + qt_net_supported_list = 0; + + if (e == Success && type == XA_ATOM && format == 32) { + QBuffer ts; + ts.open(IO_WriteOnly); + + while (after > 0) { + XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow(), + qt_net_supported, offset, 1024, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + + if (type == XA_ATOM && format == 32) { + ts.writeBlock((const char *) data, nitems * sizeof(long)); + offset += nitems; + } else + after = 0; + if (data) + XFree(data); + } + + // compute nitems + QByteArray buffer(ts.buffer()); + nitems = buffer.size() / sizeof(Atom); + qt_net_supported_list = new Atom[nitems + 1]; + Atom *a = (Atom *) buffer.data(); + uint i; + for (i = 0; i < nitems; i++) + qt_net_supported_list[i] = a[i]; + qt_net_supported_list[nitems] = 0; + } +} + + +bool qt_net_supports(Atom atom) +{ + if (! qt_net_supported_list) + return FALSE; + + bool supported = FALSE; + int i = 0; + while (qt_net_supported_list[i] != 0) { + if (qt_net_supported_list[i++] == atom) { + supported = TRUE; + break; + } + } + + return supported; +} + + +// update the virtual roots array +void qt_get_net_virtual_roots() +{ + if (qt_net_virtual_root_list) + delete [] qt_net_virtual_root_list; + qt_net_virtual_root_list = 0; + + if (! qt_net_supports(qt_net_virtual_roots)) + return; + + Atom type; + int format; + long offset = 0; + unsigned long nitems, after; + unsigned char *data; + + int e = XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow(), + qt_net_virtual_roots, 0, 0, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + if (data) + XFree(data); + + if (e == Success && type == XA_ATOM && format == 32) { + QBuffer ts; + ts.open(IO_WriteOnly); + + while (after > 0) { + XGetWindowProperty(appDpy, QPaintDevice::x11AppRootWindow(), + qt_net_virtual_roots, offset, 1024, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + + if (type == XA_ATOM && format == 32) { + ts.writeBlock((const char *) data, nitems * 4); + offset += nitems; + } else + after = 0; + if (data) + XFree(data); + } + + // compute nitems + QByteArray buffer(ts.buffer()); + nitems = buffer.size() / sizeof(Window); + qt_net_virtual_root_list = new Window[nitems + 1]; + Window *a = (Window *) buffer.data(); + uint i; + for (i = 0; i < nitems; i++) + qt_net_virtual_root_list[i] = a[i]; + qt_net_virtual_root_list[nitems] = 0; + } +} + +void qt_x11_create_wm_client_leader() +{ + if ( qt_x11_wm_client_leader ) return; + + qt_x11_wm_client_leader = + XCreateSimpleWindow( QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppRootWindow(), + 0, 0, 1, 1, 0, 0, 0 ); + + // set client leader property to itself + XChangeProperty( QPaintDevice::x11AppDisplay(), + qt_x11_wm_client_leader, qt_wm_client_leader, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&qt_x11_wm_client_leader, 1 ); + + // If we are session managed, inform the window manager about it + QCString session = qApp->sessionId().latin1(); + if ( !session.isEmpty() ) { + XChangeProperty( QPaintDevice::x11AppDisplay(), + qt_x11_wm_client_leader, qt_sm_client_id, + XA_STRING, 8, PropModeReplace, + (unsigned char *)session.data(), session.length() ); + } +} + +static void qt_net_update_user_time(QWidget *tlw) +{ + XChangeProperty(QPaintDevice::x11AppDisplay(), tlw->winId(), qt_net_wm_user_time, XA_CARDINAL, + 32, PropModeReplace, (unsigned char *) &qt_x_user_time, 1); +} + +static void qt_check_focus_model() +{ + Window fw = None; + int unused; + XGetInputFocus( appDpy, &fw, &unused ); + if ( fw == PointerRoot ) + qt_focus_model = FocusModel_PointerRoot; + else + qt_focus_model = FocusModel_Other; +} + + +/* + Returns a truecolor visual (if there is one). 8-bit TrueColor visuals + are ignored, unless the user has explicitly requested -visual TrueColor. + The SGI X server usually has an 8 bit default visual, but the application + can also ask for a truecolor visual. This is what we do if + QApplication::colorSpec() is QApplication::ManyColor. +*/ + +static Visual *find_truecolor_visual( Display *dpy, int scr, int *depth, int *ncols ) +{ + XVisualInfo *vi, rvi; + int best=0, n, i; + rvi.c_class = TrueColor; + rvi.screen = scr; + vi = XGetVisualInfo( dpy, VisualClassMask | VisualScreenMask, + &rvi, &n ); + if ( vi ) { + for ( i=0; i<n; i++ ) { + if ( vi[i].depth > vi[best].depth ) + best = i; + } + } + Visual *v = DefaultVisual(dpy,scr); + if ( !vi || (vi[best].visualid == XVisualIDFromVisual(v)) || + (vi[best].depth <= 8 && qt_visual_option != TrueColor) ) + { + *depth = DefaultDepth(dpy,scr); + *ncols = DisplayCells(dpy,scr); + } else { + v = vi[best].visual; + *depth = vi[best].depth; + *ncols = vi[best].colormap_size; + } + if ( vi ) + XFree( (char *)vi ); + return v; +} + + +/***************************************************************************** + qt_init() - initializes Qt for X11 + *****************************************************************************/ + +#define XK_MISCELLANY +#define XK_LATIN1 +#define XK_KOREAN +#define XK_XKB_KEYS +#include <X11/keysymdef.h> + +// ### This should be static but it isn't because of the friend declaration +// ### in qpaintdevice.h which then should have a static too but can't have +// ### it because "storage class specifiers invalid in friend function +// ### declarations" :-) Ideas anyone? +void qt_init_internal( int *argcptr, char **argv, + Display *display, Qt::HANDLE visual, Qt::HANDLE colormap ) +{ + setlocale( LC_ALL, "" ); // use correct char set mapping + setlocale( LC_NUMERIC, "C" ); // make sprintf()/scanf() work + + if ( display ) { + // Qt part of other application + + appForeignDpy = TRUE; + appDpy = display; + + // Set application name and class + appName = qstrdup( "Qt-subapplication" ); + char *app_class = 0; + if (argv) { + const char* p = strrchr( argv[0], '/' ); + app_class = qstrdup(p ? p + 1 : argv[0]); + if (app_class[0]) + app_class[0] = toupper(app_class[0]); + } + appClass = app_class; + + // Install default error handlers + original_x_errhandler = XSetErrorHandler( qt_x_errhandler ); + original_xio_errhandler = XSetIOErrorHandler( qt_xio_errhandler ); + } else { + // Qt controls everything (default) + + int argc = *argcptr; + int j; + + // Install default error handlers + original_x_errhandler = XSetErrorHandler( qt_x_errhandler ); + original_xio_errhandler = XSetIOErrorHandler( qt_xio_errhandler ); + + // Set application name and class + char *app_class = 0; + if (argv) { + const char *p = strrchr( argv[0], '/' ); + appName = p ? p + 1 : argv[0]; + app_class = qstrdup(appName); + if (app_class[0]) + app_class[0] = toupper(app_class[0]); + } + appClass = app_class; + + // Get command line params + j = argc ? 1 : 0; + for ( int i=1; i<argc; i++ ) { + if ( argv[i] && *argv[i] != '-' ) { + argv[j++] = argv[i]; + continue; + } + QCString arg = argv[i]; + if ( arg == "-display" ) { + if ( ++i < argc ) + appDpyName = argv[i]; + } else if ( arg == "-fn" || arg == "-font" ) { + if ( ++i < argc ) { + appFont = argv[i]; + qt_x11_cmdline_font = true; + } + } else if ( arg == "-bg" || arg == "-background" ) { + if ( ++i < argc ) + appBGCol = argv[i]; + } else if ( arg == "-btn" || arg == "-button" ) { + if ( ++i < argc ) + appBTNCol = argv[i]; + } else if ( arg == "-fg" || arg == "-foreground" ) { + if ( ++i < argc ) + appFGCol = argv[i]; + } else if ( arg == "-name" ) { + if ( ++i < argc ) + appName = argv[i]; + } else if ( arg == "-title" ) { + if ( ++i < argc ) + mwTitle = argv[i]; + } else if ( arg == "-geometry" ) { + if ( ++i < argc ) + mwGeometry = argv[i]; + //Ming-Che 10/10 + } else if ( arg == "-im" ) { + if ( ++i < argc ) + qt_ximServer = argv[i]; + } else if ( arg == "-iconic" ) { + mwIconic = !mwIconic; + } else if ( arg == "-ncols" ) { // xv and netscape use this name + if ( ++i < argc ) + qt_ncols_option = QMAX(0,atoi(argv[i])); + } else if ( arg == "-visual" ) { // xv and netscape use this name + if ( ++i < argc ) { + QCString s = QCString(argv[i]).lower(); + if ( s == "truecolor" ) { + qt_visual_option = TrueColor; + } else { + // ### Should we honor any others? + } + } +#ifndef QT_NO_XIM + } else if ( arg == "-inputstyle" ) { + if ( ++i < argc ) { + QCString s = QCString(argv[i]).lower(); + if ( s == "onthespot" ) + qt_xim_preferred_style = XIMPreeditCallbacks | + XIMStatusNothing; + else if ( s == "overthespot" ) + qt_xim_preferred_style = XIMPreeditPosition | + XIMStatusNothing; + else if ( s == "offthespot" ) + qt_xim_preferred_style = XIMPreeditArea | + XIMStatusArea; + else if ( s == "root" ) + qt_xim_preferred_style = XIMPreeditNothing | + XIMStatusNothing; + } +#endif + } else if ( arg == "-cmap" ) { // xv uses this name + qt_cmap_option = TRUE; + } +#if defined(QT_DEBUG) + else if ( arg == "-sync" ) + appSync = !appSync; + else if ( arg == "-nograb" ) + appNoGrab = !appNoGrab; + else if ( arg == "-dograb" ) + appDoGrab = !appDoGrab; +#endif + else + argv[j++] = argv[i]; + } + + *argcptr = j; + +#if defined(QT_DEBUG) && defined(Q_OS_LINUX) + if ( !appNoGrab && !appDoGrab ) { + QCString s; + s.sprintf( "/proc/%d/cmdline", getppid() ); + QFile f( s ); + if ( f.open( IO_ReadOnly ) ) { + s.truncate( 0 ); + int c; + while ( (c = f.getch()) > 0 ) { + if ( c == '/' ) + s.truncate( 0 ); + else + s += (char)c; + } + if ( s == "gdb" ) { + appNoGrab = TRUE; + qDebug( "Qt: gdb: -nograb added to command-line options.\n" + "\t Use the -dograb option to enforce grabbing." ); + } + f.close(); + } + } +#endif + // Connect to X server + + if( qt_is_gui_used ) { + if ( ( appDpy = XOpenDisplay(appDpyName) ) == 0 ) { + qWarning( "%s: cannot connect to X server %s", appName, + XDisplayName(appDpyName) ); + qApp = 0; + exit( 1 ); + } + + if ( appSync ) // if "-sync" argument + XSynchronize( appDpy, TRUE ); + } + } + // Common code, regardless of whether display is foreign. + + // Get X parameters + + if( qt_is_gui_used ) { + appScreen = DefaultScreen(appDpy); + appScreenCount = ScreenCount(appDpy); + + QPaintDevice::x_appdisplay = appDpy; + QPaintDevice::x_appscreen = appScreen; + + // allocate the arrays for the QPaintDevice data + QPaintDevice::x_appdepth_arr = new int[ appScreenCount ]; + QPaintDevice::x_appcells_arr = new int[ appScreenCount ]; + QPaintDevice::x_approotwindow_arr = new Qt::HANDLE[ appScreenCount ]; + QPaintDevice::x_appcolormap_arr = new Qt::HANDLE[ appScreenCount ]; + QPaintDevice::x_appdefcolormap_arr = new bool[ appScreenCount ]; + QPaintDevice::x_appvisual_arr = new void*[ appScreenCount ]; + QPaintDevice::x_appdefvisual_arr = new bool[ appScreenCount ]; + Q_CHECK_PTR( QPaintDevice::x_appdepth_arr ); + Q_CHECK_PTR( QPaintDevice::x_appcells_arr ); + Q_CHECK_PTR( QPaintDevice::x_approotwindow_arr ); + Q_CHECK_PTR( QPaintDevice::x_appcolormap_arr ); + Q_CHECK_PTR( QPaintDevice::x_appdefcolormap_arr ); + Q_CHECK_PTR( QPaintDevice::x_appvisual_arr ); + Q_CHECK_PTR( QPaintDevice::x_appdefvisual_arr ); + + int screen; + QString serverVendor( ServerVendor( appDpy) ); + if (serverVendor.contains("XFree86") && VendorRelease(appDpy) < 40300000) + qt_hebrew_keyboard_hack = TRUE; + + for ( screen = 0; screen < appScreenCount; ++screen ) { + QPaintDevice::x_appdepth_arr[ screen ] = DefaultDepth(appDpy, screen); + QPaintDevice::x_appcells_arr[ screen ] = DisplayCells(appDpy, screen); + QPaintDevice::x_approotwindow_arr[ screen ] = RootWindow(appDpy, screen); + + // setup the visual and colormap for each screen + Visual *vis = 0; + if ( visual && screen == appScreen ) { + // use the provided visual on the default screen only + vis = (Visual *) visual; + + // figure out the depth of the visual we are using + XVisualInfo *vi, rvi; + int n; + rvi.visualid = XVisualIDFromVisual(vis); + rvi.screen = screen; + vi = XGetVisualInfo( appDpy, VisualIDMask | VisualScreenMask, &rvi, &n ); + if (vi) { + QPaintDevice::x_appdepth_arr[ screen ] = vi->depth; + QPaintDevice::x_appcells_arr[ screen ] = vi->visual->map_entries; + QPaintDevice::x_appvisual_arr[ screen ] = vi->visual; + QPaintDevice::x_appdefvisual_arr[ screen ] = FALSE; + XFree(vi); + } else { + // couldn't get info about the visual, use the default instead + vis = 0; + } + } + + if (!vis) { + // use the default visual + vis = DefaultVisual(appDpy, screen); + QPaintDevice::x_appdefvisual_arr[ screen ] = TRUE; + + if ( qt_visual_option == TrueColor || + QApplication::colorSpec() == QApplication::ManyColor ) { + // find custom visual + + int d, c; + vis = find_truecolor_visual( appDpy, screen, &d, &c ); + QPaintDevice::x_appdepth_arr[ screen ] = d; + QPaintDevice::x_appcells_arr[ screen ] = c; + + QPaintDevice::x_appvisual_arr[ screen ] = vis; + QPaintDevice::x_appdefvisual_arr[ screen ] = + (XVisualIDFromVisual(vis) == + XVisualIDFromVisual(DefaultVisual(appDpy, screen))); + } + + QPaintDevice::x_appvisual_arr[ screen ] = vis; + } + + // we assume that 8bpp == pseudocolor, but this is not + // always the case (according to the X server), so we need + // to make sure that our internal data is setup in a way + // that is compatible with our assumptions + if ( vis->c_class == TrueColor && + QPaintDevice::x_appdepth_arr[ screen ] == 8 && + QPaintDevice::x_appcells_arr[ screen ] == 8 ) + QPaintDevice::x_appcells_arr[ screen ] = 256; + + if ( colormap && screen == appScreen ) { + // use the provided colormap for the default screen only + QPaintDevice::x_appcolormap_arr[ screen ] = colormap; + QPaintDevice::x_appdefcolormap_arr[ screen ] = FALSE; + } else { + if ( vis->c_class == TrueColor ) { + QPaintDevice::x_appdefcolormap_arr[ screen ] = + QPaintDevice::x_appdefvisual_arr[ screen ]; + } else { + QPaintDevice::x_appdefcolormap_arr[ screen ] = + !qt_cmap_option && QPaintDevice::x_appdefvisual_arr[ screen ]; + } + + if ( QPaintDevice::x_appdefcolormap_arr[ screen ] ) { + // use default colormap + XStandardColormap *stdcmap; + VisualID vid = + XVisualIDFromVisual((Visual *) + QPaintDevice::x_appvisual_arr[ screen ]); + int i, count; + + QPaintDevice::x_appcolormap_arr[ screen ] = 0; + + if ( ! serverVendor.contains( "Hewlett-Packard" ) ) { + // on HPUX 10.20 local displays, the RGB_DEFAULT_MAP colormap + // doesn't give us correct colors. Why this happens, I have + // no clue, so we disable this for HPUX + if (XGetRGBColormaps(appDpy, + QPaintDevice::x11AppRootWindow( screen ), + &stdcmap, &count, XA_RGB_DEFAULT_MAP)) { + i = 0; + while (i < count && + QPaintDevice::x_appcolormap_arr[ screen ] == 0) { + if (stdcmap[i].visualid == vid) { + QPaintDevice::x_appcolormap_arr[ screen ] = + stdcmap[i].colormap; + } + i++; + } + + XFree( (char *)stdcmap ); + } + } + + if (QPaintDevice::x_appcolormap_arr[ screen ] == 0) { + QPaintDevice::x_appcolormap_arr[ screen ] = + DefaultColormap(appDpy, screen); + } + } else { + // create a custom colormap + QPaintDevice::x_appcolormap_arr[ screen ] = + XCreateColormap(appDpy, QPaintDevice::x11AppRootWindow( screen ), + vis, AllocNone); + } + } + } + + // Set X paintdevice parameters for the default screen + QPaintDevice::x_appdepth = QPaintDevice::x_appdepth_arr[ appScreen ]; + QPaintDevice::x_appcells = QPaintDevice::x_appcells_arr[ appScreen ]; + QPaintDevice::x_approotwindow = QPaintDevice::x_approotwindow_arr[ appScreen ]; + QPaintDevice::x_appcolormap = QPaintDevice::x_appcolormap_arr[ appScreen ]; + QPaintDevice::x_appdefcolormap = QPaintDevice::x_appdefcolormap_arr[ appScreen ]; + QPaintDevice::x_appvisual = QPaintDevice::x_appvisual_arr[ appScreen ]; + QPaintDevice::x_appdefvisual = QPaintDevice::x_appdefvisual_arr[ appScreen ]; + + // Support protocols + + qt_x11_intern_atom( "WM_PROTOCOLS", &qt_wm_protocols ); + qt_x11_intern_atom( "WM_DELETE_WINDOW", &qt_wm_delete_window ); + qt_x11_intern_atom( "WM_STATE", &qt_wm_state ); + qt_x11_intern_atom( "WM_CHANGE_STATE", &qt_wm_change_state ); + qt_x11_intern_atom( "WM_TAKE_FOCUS", &qt_wm_take_focus ); + qt_x11_intern_atom( "WM_CLIENT_LEADER", &qt_wm_client_leader); + qt_x11_intern_atom( "WM_WINDOW_ROLE", &qt_window_role); + qt_x11_intern_atom( "SM_CLIENT_ID", &qt_sm_client_id); + qt_x11_intern_atom( "CLIPBOARD", &qt_xa_clipboard ); + qt_x11_intern_atom( "RESOURCE_MANAGER", &qt_resource_manager ); + qt_x11_intern_atom( "INCR", &qt_x_incr ); + qt_x11_intern_atom( "_XSETROOT_ID", &qt_xsetroot_id ); + qt_x11_intern_atom( "_QT_SELECTION", &qt_selection_property ); + qt_x11_intern_atom( "_QT_CLIPBOARD_SENTINEL", &qt_clipboard_sentinel ); + qt_x11_intern_atom( "_QT_SELECTION_SENTINEL", &qt_selection_sentinel ); + qt_x11_intern_atom( "_QT_SCROLL_DONE", &qt_qt_scrolldone ); + qt_x11_intern_atom( "_QT_INPUT_ENCODING", &qt_input_encoding ); + qt_x11_intern_atom( "_QT_SIZEGRIP", &qt_sizegrip ); + qt_x11_intern_atom( "_NET_WM_CONTEXT_HELP", &qt_net_wm_context_help ); + qt_x11_intern_atom( "_NET_WM_PING", &qt_net_wm_ping ); + qt_x11_intern_atom( "_MOTIF_WM_HINTS", &qt_xa_motif_wm_hints ); + qt_x11_intern_atom( "DTWM_IS_RUNNING", &qt_cde_running ); + qt_x11_intern_atom( "KWIN_RUNNING", &qt_kwin_running ); + qt_x11_intern_atom( "KWM_RUNNING", &qt_kwm_running ); + qt_x11_intern_atom( "GNOME_BACKGROUND_PROPERTIES", &qt_gbackground_properties ); + + QString atomname("_QT_SETTINGS_TIMESTAMP_"); + atomname += XDisplayName(appDpyName); + qt_x11_intern_atom( atomname.latin1(), &qt_settings_timestamp ); + + qt_x11_intern_atom( "_NET_SUPPORTED", &qt_net_supported ); + qt_x11_intern_atom( "_NET_VIRTUAL_ROOTS", &qt_net_virtual_roots ); + qt_x11_intern_atom( "_NET_WORKAREA", &qt_net_workarea ); + qt_x11_intern_atom( "_NET_WM_STATE", &qt_net_wm_state ); + qt_x11_intern_atom( "_NET_WM_STATE_MODAL", &qt_net_wm_state_modal ); + qt_x11_intern_atom( "_NET_WM_STATE_MAXIMIZED_VERT", &qt_net_wm_state_max_v ); + qt_x11_intern_atom( "_NET_WM_STATE_MAXIMIZED_HORZ", &qt_net_wm_state_max_h ); + qt_x11_intern_atom( "_NET_WM_STATE_FULLSCREEN", &qt_net_wm_state_fullscreen ); + qt_x11_intern_atom( "_NET_WM_STATE_ABOVE", &qt_net_wm_state_above ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE", &qt_net_wm_window_type ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_NORMAL", &qt_net_wm_window_type_normal ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_DIALOG", &qt_net_wm_window_type_dialog ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_TOOLBAR", &qt_net_wm_window_type_toolbar ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_MENU", &qt_net_wm_window_type_menu ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_UTILITY", &qt_net_wm_window_type_utility ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_SPLASH", &qt_net_wm_window_type_splash ); + qt_x11_intern_atom( "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE", &qt_net_wm_window_type_override ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", &qt_net_wm_window_type_dropdown_menu ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_POPUP_MENU", &qt_net_wm_window_type_popup_menu ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_TOOLTIP", &qt_net_wm_window_type_tooltip ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_COMBO", &qt_net_wm_window_type_combo ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_DND", &qt_net_wm_window_type_dnd ); + qt_x11_intern_atom( "_KDE_NET_WM_FRAME_STRUT", &qt_net_wm_frame_strut ); + qt_x11_intern_atom( "_NET_WM_STATE_STAYS_ON_TOP", + &qt_net_wm_state_stays_on_top ); + qt_x11_intern_atom( "_NET_WM_PID", &qt_net_wm_pid ); + qt_x11_intern_atom( "_NET_WM_USER_TIME", &qt_net_wm_user_time ); + qt_x11_intern_atom( "_NET_WM_FULL_PLACEMENT", &qt_net_wm_full_placement ); + qt_x11_intern_atom( "ENLIGHTENMENT_DESKTOP", &qt_enlightenment_desktop ); + qt_x11_intern_atom( "_NET_WM_NAME", &qt_net_wm_name ); + qt_x11_intern_atom( "_NET_WM_ICON_NAME", &qt_net_wm_icon_name ); + qt_x11_intern_atom( "UTF8_STRING", &qt_utf8_string ); + qt_x11_intern_atom( "_SGI_DESKS_MANAGER", &qt_sgi_desks_manager ); + +#ifndef QT_NO_XSYNC + qt_x11_intern_atom( "_NET_WM_SYNC_REQUEST_COUNTER", &qt_net_wm_sync_request_counter ); + qt_x11_intern_atom( "_NET_WM_SYNC_REQUEST", &qt_net_wm_sync_request ); +#endif + + qt_xdnd_setup(); + qt_x11_motifdnd_init(); + + // Finally create all atoms + qt_x11_process_intern_atoms(); + + // look for broken window managers + qt_detect_broken_window_manager(); + + // initialize NET lists + qt_get_net_supported(); + qt_get_net_virtual_roots(); + +#ifndef QT_NO_XRANDR + // See if XRandR is supported on the connected display + int xrandr_errorbase; + Q_UNUSED( xrandr_eventbase ); + if ( XRRQueryExtension( appDpy, &xrandr_eventbase, &xrandr_errorbase ) ) { + // XRandR is supported + qt_use_xrandr = TRUE; + } +#endif // QT_NO_XRANDR + +#ifndef QT_NO_XRENDER + // See if XRender is supported on the connected display + int xrender_eventbase, xrender_errorbase; + if (XRenderQueryExtension(appDpy, &xrender_eventbase, &xrender_errorbase)) { + // XRender is supported, let's see if we have a PictFormat for the + // default visual + XRenderPictFormat *format = + XRenderFindVisualFormat(appDpy, + (Visual *) QPaintDevice::x_appvisual); + qt_use_xrender = (format != 0) && (QPaintDevice::x_appdepth != 8); + } +#endif // QT_NO_XRENDER + +#ifndef QT_NO_XSYNC + // Try to initialize SYNC extension on the connected display + int xsync_major, xsync_minor; + if ( XSyncQueryExtension( appDpy, &xsync_eventbase, &xsync_errorbase ) && + XSyncInitialize( appDpy, &xsync_major, &xsync_minor ) ) { + qt_use_xsync = TRUE; + } +#endif + +#ifndef QT_NO_XKB + // If XKB is detected, set the GrabsUseXKBState option so input method + // compositions continue to work (ie. deadkeys) + unsigned int state = XkbPCF_GrabsUseXKBStateMask; + (void) XkbSetPerClientControls(appDpy, state, &state); +#endif + +#if !defined(QT_NO_XFTFREETYPE) + // defined in qfont_x11.cpp + extern bool qt_has_xft; +#ifndef QT_XFT2 + if (!qt_use_xrender) + qt_has_xft = FALSE; + else +#endif + qt_has_xft = XftInit(0) && XftInitFtLibrary(); + + if (qt_has_xft) { + char *dpi_str = XGetDefault(appDpy, "Xft", "dpi"); + if (dpi_str) { + // use a custom DPI + char *end = 0; + int dpi = strtol(dpi_str, &end, 0); + if (dpi_str != end) { + for (int s = 0; s < ScreenCount(appDpy); ++s) { + QPaintDevice::x11SetAppDpiX(dpi, s); + QPaintDevice::x11SetAppDpiY(dpi, s); + } + } + } + } +#endif // QT_NO_XFTFREETYPE + + // look at the modifier mapping, and get the correct masks for alt/meta + // find the alt/meta masks + XModifierKeymap *map = XGetModifierMapping(appDpy); + if (map) { + int i, maskIndex = 0, mapIndex = 0; + for (maskIndex = 0; maskIndex < 8; maskIndex++) { + for (i = 0; i < map->max_keypermod; i++) { + if (map->modifiermap[mapIndex]) { + KeySym sym = + XKeycodeToKeysym(appDpy, map->modifiermap[mapIndex], 0); + if ( qt_alt_mask == 0 && + ( sym == XK_Alt_L || sym == XK_Alt_R ) ) { + qt_alt_mask = 1 << maskIndex; + } + if ( qt_meta_mask == 0 && + (sym == XK_Meta_L || sym == XK_Meta_R ) ) { + qt_meta_mask = 1 << maskIndex; + } + } + mapIndex++; + } + } + + // not look for mode_switch in qt_alt_mask and qt_meta_mask - if it is + // present in one or both, then we set qt_mode_switch_remove_mask. + // see QETWidget::translateKeyEventInternal for an explanation + // of why this is needed + mapIndex = 0; + for ( maskIndex = 0; maskIndex < 8; maskIndex++ ) { + if ( qt_alt_mask != ( 1 << maskIndex ) && + qt_meta_mask != ( 1 << maskIndex ) ) { + for ( i = 0; i < map->max_keypermod; i++ ) + mapIndex++; + continue; + } + + for ( i = 0; i < map->max_keypermod; i++ ) { + if ( map->modifiermap[ mapIndex ] ) { + KeySym sym = + XKeycodeToKeysym( appDpy, map->modifiermap[ mapIndex ], 0 ); + if ( sym == XK_Mode_switch ) { + qt_mode_switch_remove_mask |= 1 << maskIndex; + } + } + mapIndex++; + } + } + + XFreeModifiermap(map); + } else { + // assume defaults + qt_alt_mask = Mod1Mask; + qt_meta_mask = Mod4Mask; + qt_mode_switch_remove_mask = 0; + } + + // Misc. initialization + + QColor::initialize(); + QFont::initialize(); + QCursor::initialize(); + QPainter::initialize(); + } + +#if defined(QT_THREAD_SUPPORT) + QThread::initialize(); +#endif + + if( qt_is_gui_used ) { + qApp->setName( appName ); + + int screen; + for ( screen = 0; screen < appScreenCount; ++screen ) { + XSelectInput( appDpy, QPaintDevice::x11AppRootWindow( screen ), + KeymapStateMask | EnterWindowMask | LeaveWindowMask | + PropertyChangeMask ); + +#ifndef QT_NO_XRANDR + if (qt_use_xrandr) + XRRSelectInput( appDpy, QPaintDevice::x11AppRootWindow( screen ), True ); +#endif // QT_NO_XRANDR + } + } + + if ( qt_is_gui_used ) { + qt_set_input_encoding(); + + qt_set_x11_resources( appFont, appFGCol, appBGCol, appBTNCol); + + // be smart about the size of the default font. most X servers have helvetica + // 12 point available at 2 resolutions: + // 75dpi (12 pixels) and 100dpi (17 pixels). + // At 95 DPI, a 12 point font should be 16 pixels tall - in which case a 17 + // pixel font is a closer match than a 12 pixel font + int ptsz = + (int) ( ( ( QPaintDevice::x11AppDpiY() >= 95 ? 17. : 12. ) * + 72. / (float) QPaintDevice::x11AppDpiY() ) + 0.5 ); + + if ( !qt_app_has_font && !qt_x11_cmdline_font ) { + QFont f( "Helvetica", ptsz ); + QApplication::setFont( f ); + } + +#if !defined(QT_NO_IM) +#if !defined(QT_NO_IM_EXTENSIONS) + QApplication::create_im(); +#else + QApplication::create_xim(); +#endif +#endif + +#if defined (QT_TABLET_SUPPORT) + int ndev, + i, + j; + bool gotStylus, + gotEraser; + XDeviceInfo *devices, *devs; + XInputClassInfo *ip; + XAnyClassPtr any; + XValuatorInfoPtr v; + XAxisInfoPtr a; + XDevice *dev; + XEventClass *ev_class; + int curr_event_count; + +#if !defined(Q_OS_IRIX) + // XFree86 divides a stylus and eraser into 2 devices, so we must do for both... + const QString XFREENAMESTYLUS = "stylus"; + const QString XFREENAMEPEN = "pen"; + const QString XFREENAMEERASER = "eraser"; +#endif + + devices = XListInputDevices( appDpy, &ndev); + if ( devices == NULL ) { + qWarning( "Failed to get list of devices" ); + ndev = -1; + } + dev = NULL; + for ( devs = devices, i = 0; i < ndev; i++, devs++ ) { + gotEraser = FALSE; +#if defined(Q_OS_IRIX) + + gotStylus = ( !strncmp(devs->name, + WACOM_NAME, sizeof(WACOM_NAME) - 1) ); +#else + QString devName = devs->name; + devName = devName.lower(); + gotStylus = ( devName.startsWith(XFREENAMEPEN) + || devName.startsWith(XFREENAMESTYLUS) ); + if ( !gotStylus ) + gotEraser = devName.startsWith( XFREENAMEERASER ); + +#endif + if ( gotStylus || gotEraser ) { + // I only wanted to do this once, so wrap pointers around these + curr_event_count = 0; + + if ( gotStylus ) { + devStylus = XOpenDevice( appDpy, devs->id ); + dev = devStylus; + ev_class = event_list_stylus; + } else if ( gotEraser ) { + devEraser = XOpenDevice( appDpy, devs->id ); + dev = devEraser; + ev_class = event_list_eraser; + } + if ( dev == NULL ) { + qWarning( "Failed to open device" ); + } else { + if ( dev->num_classes > 0 ) { + for ( ip = dev->classes, j = 0; j < devs->num_classes; + ip++, j++ ) { + switch ( ip->input_class ) { + case KeyClass: + DeviceKeyPress( dev, xinput_key_press, + ev_class[curr_event_count] ); + curr_event_count++; + DeviceKeyRelease( dev, xinput_key_release, + ev_class[curr_event_count] ); + curr_event_count++; + break; + case ButtonClass: + DeviceButtonPress( dev, xinput_button_press, + ev_class[curr_event_count] ); + curr_event_count++; + DeviceButtonRelease( dev, xinput_button_release, + ev_class[curr_event_count] ); + curr_event_count++; + break; + case ValuatorClass: + // I'm only going to be interested in motion when the + // stylus is already down anyway! + DeviceMotionNotify( dev, xinput_motion, + ev_class[curr_event_count] ); + curr_event_count++; + break; + default: + break; + } + } + } + } + // get the min/max value for pressure! + any = (XAnyClassPtr) ( devs->inputclassinfo ); + if ( dev == devStylus ) { + qt_curr_events_stylus = curr_event_count; + for (j = 0; j < devs->num_classes; j++) { + if ( any->c_class == ValuatorClass ) { + v = (XValuatorInfoPtr) any; + a = (XAxisInfoPtr) ((char *) v + + sizeof (XValuatorInfo)); +#if defined (Q_OS_IRIX) + max_pressure = a[WAC_PRESSURE_I].max_value; +#else + max_pressure = a[2].max_value; +#endif + // got the max pressure no need to go further... + break; + } + any = (XAnyClassPtr) ((char *) any + any->length); + } + } else { + qt_curr_events_eraser = curr_event_count; + } + // at this point we are assuming there is only one + // wacom device... +#if defined (Q_OS_IRIX) + if ( devStylus != NULL ) { +#else + if ( devStylus != NULL && devEraser != NULL ) { +#endif + break; + } + } + } // end for loop + XFreeDeviceList( devices ); +#endif // QT_TABLET_SUPPORT + + } else { + // read some non-GUI settings when not using the X server... + + if ( QApplication::desktopSettingsAware() ) { + QSettings settings; + + // read library (ie. plugin) path list + QString libpathkey = QString("/qt/%1.%2/libraryPath") + .arg( QT_VERSION >> 16 ) + .arg( (QT_VERSION & 0xff00 ) >> 8 ); + QStringList pathlist = + settings.readListEntry(libpathkey, ':'); + if (! pathlist.isEmpty()) { + QStringList::ConstIterator it = pathlist.begin(); + while (it != pathlist.end()) + QApplication::addLibraryPath(*it++); + } + + QString defaultcodec = settings.readEntry("/qt/defaultCodec", "none"); + if (defaultcodec != "none") { + QTextCodec *codec = QTextCodec::codecForName(defaultcodec); + if (codec) + qApp->setDefaultCodec(codec); + } + + qt_resolve_symlinks = + settings.readBoolEntry("/qt/resolveSymlinks", TRUE); + } + } + } + + +#ifndef QT_NO_STYLE + // run-time search for default style +void QApplication::x11_initialize_style() +{ + Atom type; + int format; + unsigned long length, after; + uchar *data; + if ( !app_style && + XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow(), qt_kwin_running, + 0, 1, False, AnyPropertyType, &type, &format, &length, + &after, &data ) == Success && length ) { + if ( data ) XFree( (char *)data ); + // kwin is there. check if KDE's styles are available, + // otherwise use windows style + if ( (app_style = QStyleFactory::create("highcolor") ) == 0 ) + app_style = QStyleFactory::create("windows"); + } + if ( !app_style && + XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow(), qt_kwm_running, + 0, 1, False, AnyPropertyType, &type, &format, &length, + &after, &data ) == Success && length ) { + if ( data ) XFree( (char *)data ); + app_style = QStyleFactory::create("windows"); + } + if ( !app_style && + XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow(), qt_cde_running, + 0, 1, False, AnyPropertyType, &type, &format, &length, + &after, &data ) == Success && length ) { + // DTWM is running, meaning most likely CDE is running... + if ( data ) XFree( (char *) data ); + app_style = QStyleFactory::create( "cde" ); + } + // maybe another desktop? + if ( !app_style && + XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow(), + qt_gbackground_properties, 0, 1, False, AnyPropertyType, + &type, &format, &length, &after, &data ) == Success && + length ) { + if ( data ) XFree( (char *)data ); + // default to MotifPlus with hovering + app_style = QStyleFactory::create("motifplus" ); + } +} +#endif + +void qt_init( int *argcptr, char **argv, QApplication::Type ) +{ + qt_init_internal( argcptr, argv, 0, 0, 0 ); +} + +void qt_init( Display *display, Qt::HANDLE visual, Qt::HANDLE colormap ) +{ + qt_init_internal( 0, 0, display, visual, colormap ); +} + + +/***************************************************************************** + qt_cleanup() - cleans up when the application is finished + *****************************************************************************/ + +void qt_cleanup() +{ + appliedstamp = 0; + + if ( app_save_rootinfo ) // root window must keep state + qt_save_rootinfo(); + + if ( qt_is_gui_used ) { + QPixmapCache::clear(); + QPainter::cleanup(); + QCursor::cleanup(); + QFont::cleanup(); + QColor::cleanup(); + QSharedDoubleBuffer::cleanup(); + } +#if defined(QT_THREAD_SUPPORT) + QThread::cleanup(); +#endif + +#if defined (QT_TABLET_SUPPORT) + if ( devStylus != NULL ) + XCloseDevice( appDpy, devStylus ); + if ( devEraser != NULL ) + XCloseDevice( appDpy, devEraser ); +#endif + +#if !defined(QT_NO_IM) +#if !defined(QT_NO_IM_EXTENSIONS) + QApplication::close_im(); +#else + QApplication::close_xim(); +#endif +#endif + + if ( qt_is_gui_used ) { + int screen; + for ( screen = 0; screen < appScreenCount; screen++ ) { + if ( ! QPaintDevice::x11AppDefaultColormap( screen ) ) + XFreeColormap( QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppColormap( screen ) ); + } + } + +#define QT_CLEANUP_GC(g) if (g) { for (int i=0;i<appScreenCount;i++){if(g[i])XFreeGC(appDpy,g[i]);} delete [] g; g = 0; } + QT_CLEANUP_GC(app_gc_ro); + QT_CLEANUP_GC(app_gc_ro_m); + QT_CLEANUP_GC(app_gc_tmp); + QT_CLEANUP_GC(app_gc_tmp_m); +#undef QT_CLEANUP_GC + + delete sip_list; + sip_list = 0; + + // Reset the error handlers + XSetErrorHandler( original_x_errhandler ); + XSetIOErrorHandler( original_xio_errhandler ); + + if ( qt_is_gui_used && !appForeignDpy ) + XCloseDisplay( appDpy ); // close X display + appDpy = 0; + + qt_x11_wm_client_leader = 0; + + if ( QPaintDevice::x_appdepth_arr ) + delete [] QPaintDevice::x_appdepth_arr; + if ( QPaintDevice::x_appcells_arr ) + delete [] QPaintDevice::x_appcells_arr; + if ( QPaintDevice::x_appcolormap_arr ) + delete []QPaintDevice::x_appcolormap_arr; + if ( QPaintDevice::x_appdefcolormap_arr ) + delete [] QPaintDevice::x_appdefcolormap_arr; + if ( QPaintDevice::x_appvisual_arr ) + delete [] QPaintDevice::x_appvisual_arr; + if ( QPaintDevice::x_appdefvisual_arr ) + delete [] QPaintDevice::x_appdefvisual_arr; + + if ( appForeignDpy ) { + delete [] (char *)appName; + appName = 0; + delete [] (char *)appClass; + appClass = 0; + } + + if (qt_net_supported_list) + delete [] qt_net_supported_list; + qt_net_supported_list = 0; + + if (qt_net_virtual_root_list) + delete [] qt_net_virtual_root_list; + qt_net_virtual_root_list = 0; +} + + +/***************************************************************************** + Platform specific global and internal functions + *****************************************************************************/ + +void qt_save_rootinfo() // save new root info +{ + Atom type; + int format; + unsigned long length, after; + uchar *data; + + if ( qt_xsetroot_id ) { // kill old pixmap + if ( XGetWindowProperty( appDpy, QPaintDevice::x11AppRootWindow(), + qt_xsetroot_id, 0, 1, + True, AnyPropertyType, &type, &format, + &length, &after, &data ) == Success ) { + if ( type == XA_PIXMAP && format == 32 && length == 1 && + after == 0 && data ) { + XKillClient( appDpy, *((Pixmap*)data) ); + } + Pixmap dummy = XCreatePixmap( appDpy, QPaintDevice::x11AppRootWindow(), + 1, 1, 1 ); + XChangeProperty( appDpy, QPaintDevice::x11AppRootWindow(), + qt_xsetroot_id, XA_PIXMAP, 32, + PropModeReplace, (uchar *)&dummy, 1 ); + XSetCloseDownMode( appDpy, RetainPermanent ); + } + } + if ( data ) + XFree( (char *)data ); +} + +void qt_updated_rootinfo() +{ + app_save_rootinfo = TRUE; +} + +bool qt_wstate_iconified( WId winid ) +{ + Atom type; + int format; + unsigned long length, after; + uchar *data; + int r = XGetWindowProperty( appDpy, winid, qt_wm_state, 0, 2, + False, AnyPropertyType, &type, &format, + &length, &after, &data ); + bool iconic = FALSE; + if ( r == Success && data && format == 32 ) { + // Q_UINT32 *wstate = (Q_UINT32*)data; + unsigned long *wstate = (unsigned long *) data; + iconic = (*wstate == IconicState ); + XFree( (char *)data ); + } + return iconic; +} + +const char *qAppName() // get application name +{ + return appName; +} + +const char *qAppClass() // get application class +{ + return appClass; +} + +Display *qt_xdisplay() // get current X display +{ + return appDpy; +} + +int qt_xscreen() // get current X screen +{ + return appScreen; +} + +// ### REMOVE 4.0 +WId qt_xrootwin() // get X root window +{ + return QPaintDevice::x11AppRootWindow(); +} + +WId qt_xrootwin( int scrn ) // get X root window for screen +{ + return QPaintDevice::x11AppRootWindow( scrn ); +} + +bool qt_nograb() // application no-grab option +{ +#if defined(QT_DEBUG) + return appNoGrab; +#else + return FALSE; +#endif +} + +static GC create_gc( int scrn, bool monochrome ) +{ + GC gc; + if ( monochrome ) { + Pixmap pm = XCreatePixmap( appDpy, RootWindow( appDpy, scrn ), 8, 8, 1 ); + gc = XCreateGC( appDpy, pm, 0, 0 ); + XFreePixmap( appDpy, pm ); + } else { + if ( QPaintDevice::x11AppDefaultVisual( scrn ) ) { + gc = XCreateGC( appDpy, RootWindow( appDpy, scrn ), 0, 0 ); + } else { + Window w; + XSetWindowAttributes a; + a.background_pixel = Qt::black.pixel( scrn ); + a.border_pixel = Qt::black.pixel( scrn ); + a.colormap = QPaintDevice::x11AppColormap( scrn ); + w = XCreateWindow( appDpy, RootWindow( appDpy, scrn ), 0, 0, 100, 100, + 0, QPaintDevice::x11AppDepth( scrn ), InputOutput, + (Visual*)QPaintDevice::x11AppVisual( scrn ), + CWBackPixel|CWBorderPixel|CWColormap, &a ); + gc = XCreateGC( appDpy, w, 0, 0 ); + XDestroyWindow( appDpy, w ); + } + } + XSetGraphicsExposures( appDpy, gc, False ); + return gc; +} + +GC qt_xget_readonly_gc( int scrn, bool monochrome ) // get read-only GC +{ + if ( scrn < 0 || scrn >= appScreenCount ) { + qDebug("invalid screen %d %d", scrn, appScreenCount ); + QWidget* bla = 0; + bla->setName("hello"); + } + GC gc; + if ( monochrome ) { + if ( !app_gc_ro_m ) // create GC for bitmap + memset( (app_gc_ro_m = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_ro_m[scrn] ) + app_gc_ro_m[scrn] = create_gc( scrn, TRUE ); + gc = app_gc_ro_m[scrn]; + } else { // create standard GC + if ( !app_gc_ro ) + memset( (app_gc_ro = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_ro[scrn] ) + app_gc_ro[scrn] = create_gc( scrn, FALSE ); + gc = app_gc_ro[scrn]; + } + return gc; +} + +GC qt_xget_temp_gc( int scrn, bool monochrome ) // get temporary GC +{ + if ( scrn < 0 || scrn >= appScreenCount ) { + qDebug("invalid screen (tmp) %d %d", scrn, appScreenCount ); + QWidget* bla = 0; + bla->setName("hello"); + } + GC gc; + if ( monochrome ) { + if ( !app_gc_tmp_m ) // create GC for bitmap + memset( (app_gc_tmp_m = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_tmp_m[scrn] ) + app_gc_tmp_m[scrn] = create_gc( scrn, TRUE ); + gc = app_gc_tmp_m[scrn]; + } else { // create standard GC + if ( !app_gc_tmp ) + memset( (app_gc_tmp = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_tmp[scrn] ) + app_gc_tmp[scrn] = create_gc( scrn, FALSE ); + gc = app_gc_tmp[scrn]; + } + return gc; +} + + +/***************************************************************************** + Platform specific QApplication members + *****************************************************************************/ + +/*! + \fn QWidget *QApplication::mainWidget() const + + Returns the main application widget, or 0 if there is no main + widget. + + \sa setMainWidget() +*/ + +/*! + Sets the application's main widget to \a mainWidget. + + In most respects the main widget is like any other widget, except + that if it is closed, the application exits. Note that + QApplication does \e not take ownership of the \a mainWidget, so + if you create your main widget on the heap you must delete it + yourself. + + You need not have a main widget; connecting lastWindowClosed() to + quit() is an alternative. + + For X11, this function also resizes and moves the main widget + according to the \e -geometry command-line option, so you should + set the default geometry (using \l QWidget::setGeometry()) before + calling setMainWidget(). + + \sa mainWidget(), exec(), quit() +*/ + +void QApplication::setMainWidget( QWidget *mainWidget ) +{ +#if defined(QT_CHECK_STATE) + if ( mainWidget && mainWidget->parentWidget() && + ! mainWidget->parentWidget()->isDesktop() ) + qWarning( "QApplication::setMainWidget(): New main widget (%s/%s) " + "has a parent!", + mainWidget->className(), mainWidget->name() ); +#endif + main_widget = mainWidget; + if ( main_widget ) { // give WM command line + XSetWMProperties( main_widget->x11Display(), main_widget->winId(), + 0, 0, app_argv, app_argc, 0, 0, 0 ); + if ( mwTitle ) + XStoreName( main_widget->x11Display(), main_widget->winId(), (char*)mwTitle ); + if ( mwGeometry ) { // parse geometry + int x, y; + int w, h; + int m = XParseGeometry( (char*)mwGeometry, &x, &y, (uint*)&w, (uint*)&h ); + QSize minSize = main_widget->minimumSize(); + QSize maxSize = main_widget->maximumSize(); + if ( (m & XValue) == 0 ) + x = main_widget->geometry().x(); + if ( (m & YValue) == 0 ) + y = main_widget->geometry().y(); + if ( (m & WidthValue) == 0 ) + w = main_widget->width(); + if ( (m & HeightValue) == 0 ) + h = main_widget->height(); + w = QMIN(w,maxSize.width()); + h = QMIN(h,maxSize.height()); + w = QMAX(w,minSize.width()); + h = QMAX(h,minSize.height()); + if ( (m & XNegative) ) { + x = desktop()->width() + x - w; + qt_widget_tlw_gravity = NorthEastGravity; + } + if ( (m & YNegative) ) { + y = desktop()->height() + y - h; + qt_widget_tlw_gravity = (m & XNegative) ? SouthEastGravity : SouthWestGravity; + } + main_widget->setGeometry( x, y, w, h ); + } + } +} + +#ifndef QT_NO_CURSOR + +/***************************************************************************** + QApplication cursor stack + *****************************************************************************/ + +extern void qt_x11_enforce_cursor( QWidget * w ); + +typedef QPtrList<QCursor> QCursorList; + +static QCursorList *cursorStack = 0; + +/*! + \fn QCursor *QApplication::overrideCursor() + + Returns the active application override cursor. + + This function returns 0 if no application cursor has been defined + (i.e. the internal cursor stack is empty). + + \sa setOverrideCursor(), restoreOverrideCursor() +*/ + +/*! + Sets the application override cursor to \a cursor. + + Application override cursors are intended for showing the user + that the application is in a special state, for example during an + operation that might take some time. + + This cursor will be displayed in all the application's widgets + until restoreOverrideCursor() or another setOverrideCursor() is + called. + + Application cursors are stored on an internal stack. + setOverrideCursor() pushes the cursor onto the stack, and + restoreOverrideCursor() pops the active cursor off the stack. + Every setOverrideCursor() must eventually be followed by a + corresponding restoreOverrideCursor(), otherwise the stack will + never be emptied. + + If \a replace is TRUE, the new cursor will replace the last + override cursor (the stack keeps its depth). If \a replace is + FALSE, the new stack is pushed onto the top of the stack. + + Example: + \code + QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) ); + calculateHugeMandelbrot(); // lunch time... + QApplication::restoreOverrideCursor(); + \endcode + + \sa overrideCursor(), restoreOverrideCursor(), QWidget::setCursor() +*/ + +void QApplication::setOverrideCursor( const QCursor &cursor, bool replace ) +{ + if ( !cursorStack ) { + cursorStack = new QCursorList; + Q_CHECK_PTR( cursorStack ); + cursorStack->setAutoDelete( TRUE ); + } + app_cursor = new QCursor( cursor ); + Q_CHECK_PTR( app_cursor ); + if ( replace ) + cursorStack->removeLast(); + cursorStack->append( app_cursor ); + + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // for all widgets that have + if ( w->testWState( WState_OwnCursor ) ) + qt_x11_enforce_cursor( w ); + ++it; + } + XFlush( appDpy ); // make X execute it NOW +} + +/*! + Undoes the last setOverrideCursor(). + + If setOverrideCursor() has been called twice, calling + restoreOverrideCursor() will activate the first cursor set. + Calling this function a second time restores the original widgets' + cursors. + + \sa setOverrideCursor(), overrideCursor(). +*/ + +void QApplication::restoreOverrideCursor() +{ + if ( !cursorStack ) // no cursor stack + return; + cursorStack->removeLast(); + app_cursor = cursorStack->last(); + if ( QWidget::mapper != 0 && !closingDown() ) { + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { // set back to original cursors + if ( w->testWState( WState_OwnCursor ) ) + qt_x11_enforce_cursor( w ); + ++it; + } + XFlush( appDpy ); + } + if ( !app_cursor ) { + delete cursorStack; + cursorStack = 0; + } +} + +#endif + +/*! + \fn bool QApplication::hasGlobalMouseTracking() + + Returns TRUE if global mouse tracking is enabled; otherwise + returns FALSE. + + \sa setGlobalMouseTracking() +*/ + +/*! + Enables global mouse tracking if \a enable is TRUE, or disables it + if \a enable is FALSE. + + Enabling global mouse tracking makes it possible for widget event + filters or application event filters to get all mouse move events, + even when no button is depressed. This is useful for special GUI + elements, e.g. tooltips. + + Global mouse tracking does not affect widgets and their + mouseMoveEvent(). For a widget to get mouse move events when no + button is depressed, it must do QWidget::setMouseTracking(TRUE). + + This function uses an internal counter. Each + setGlobalMouseTracking(TRUE) must have a corresponding + setGlobalMouseTracking(FALSE): + \code + // at this point global mouse tracking is off + QApplication::setGlobalMouseTracking( TRUE ); + QApplication::setGlobalMouseTracking( TRUE ); + QApplication::setGlobalMouseTracking( FALSE ); + // at this point it's still on + QApplication::setGlobalMouseTracking( FALSE ); + // but now it's off + \endcode + + \sa hasGlobalMouseTracking(), QWidget::hasMouseTracking() +*/ + +void QApplication::setGlobalMouseTracking( bool enable ) +{ + bool tellAllWidgets; + if ( enable ) { + tellAllWidgets = (++app_tracking == 1); + } else { + tellAllWidgets = (--app_tracking == 0); + } + if ( tellAllWidgets ) { + QWidgetIntDictIt it( *((QWidgetIntDict*)QWidget::mapper) ); + register QWidget *w; + while ( (w=it.current()) ) { + if ( app_tracking > 0 ) { // switch on + if ( !w->testWState(WState_MouseTracking) ) { + w->setMouseTracking( TRUE ); + w->clearWState( WState_MouseTracking ); + } + } else { // switch off + if ( !w->testWState(WState_MouseTracking) ) { + w->setWState( WState_MouseTracking ); + w->setMouseTracking( FALSE ); + } + } + ++it; + } + } +} + + +/***************************************************************************** + Routines to find a Qt widget from a screen position + *****************************************************************************/ + +Window qt_x11_findClientWindow( Window win, Atom property, bool leaf ) +{ + Atom type = None; + int format, i; + ulong nitems, after; + uchar *data; + Window root, parent, target=0, *children=0; + uint nchildren; + if ( XGetWindowProperty( appDpy, win, property, 0, 0, FALSE, AnyPropertyType, + &type, &format, &nitems, &after, &data ) == Success ) { + if ( data ) + XFree( (char *)data ); + if ( type ) + return win; + } + if ( !XQueryTree(appDpy,win,&root,&parent,&children,&nchildren) ) { + if ( children ) + XFree( (char *)children ); + return 0; + } + for ( i=nchildren-1; !target && i >= 0; i-- ) + target = qt_x11_findClientWindow( children[i], property, leaf ); + if ( children ) + XFree( (char *)children ); + return target; +} + + +/*! + Returns a pointer to the widget at global screen position \a + (x, y), or 0 if there is no Qt widget there. + + If \a child is FALSE and there is a child widget at position \a + (x, y), the top-level widget containing it is returned. If \a child + is TRUE the child widget at position \a (x, y) is returned. + + This function is normally rather slow. + + \sa QCursor::pos(), QWidget::grabMouse(), QWidget::grabKeyboard() +*/ + +QWidget *QApplication::widgetAt( int x, int y, bool child ) +{ + int screen = QCursor::x11Screen(); + int lx, ly; + + Window target; + if ( !XTranslateCoordinates(appDpy, + QPaintDevice::x11AppRootWindow(screen), + QPaintDevice::x11AppRootWindow(screen), + x, y, &lx, &ly, &target) ) { + return 0; + } + if ( !target || target == QPaintDevice::x11AppRootWindow(screen) ) + return 0; + QWidget *w, *c; + w = QWidget::find( (WId)target ); + + if ( !w ) { + qt_ignore_badwindow(); + target = qt_x11_findClientWindow( target, qt_wm_state, TRUE ); + if (qt_badwindow() ) + return 0; + w = QWidget::find( (WId)target ); +#if 0 + if ( !w ) { + // Perhaps the widgets at (x,y) is inside a foreign application? + // Search all toplevel widgets to see if one is within target + QWidgetList *list = topLevelWidgets(); + QWidget *widget = list->first(); + while ( widget && !w ) { + Window ctarget = target; + if ( widget->isVisible() && !widget->isDesktop() ) { + Window wid = widget->winId(); + while ( ctarget && !w ) { + XTranslateCoordinates(appDpy, + QPaintDevice::x11AppRootWindow(screen), + ctarget, x, y, &lx, &ly, &ctarget); + if ( ctarget == wid ) { + // Found + w = widget; + XTranslateCoordinates(appDpy, + QPaintDevice::x11AppRootWindow(screen), + ctarget, x, y, &lx, &ly, &ctarget); + } + } + } + widget = list->next(); + } + delete list; + } +#endif + } + if ( child && w ) { + if ( (c = w->childAt( w->mapFromGlobal(QPoint(x, y ) ) ) ) ) + return c; + } + return w; +} + +/*! + \overload QWidget *QApplication::widgetAt( const QPoint &pos, bool child ) + + Returns a pointer to the widget at global screen position \a pos, + or 0 if there is no Qt widget there. + + If \a child is FALSE and there is a child widget at position \a + pos, the top-level widget containing it is returned. If \a child + is TRUE the child widget at position \a pos is returned. +*/ + + +/*! + Flushes the X event queue in the X11 implementation. This normally + returns almost immediately. Does nothing on other platforms. + + \sa syncX() +*/ + +void QApplication::flushX() +{ + if ( appDpy ) + XFlush( appDpy ); +} + +/*! + Flushes the window system specific event queues. + + If you are doing graphical changes inside a loop that does not + return to the event loop on asynchronous window systems like X11 + or double buffered window systems like MacOS X, and you want to + visualize these changes immediately (e.g. Splash Screens), call + this function. + + \sa flushX() sendPostedEvents() QPainter::flush() +*/ + +void QApplication::flush() +{ + flushX(); +} + +/*! + Synchronizes with the X server in the X11 implementation. This + normally takes some time. Does nothing on other platforms. + + \sa flushX() +*/ + +void QApplication::syncX() +{ + if ( appDpy ) + XSync( appDpy, False ); // don't discard events +} + + +/*! + Sounds the bell, using the default volume and sound. +*/ + +void QApplication::beep() +{ + if ( appDpy ) + XBell( appDpy, 0 ); +} + + + +/***************************************************************************** + Special lookup functions for windows that have been reparented recently + *****************************************************************************/ + +static QWidgetIntDict *wPRmapper = 0; // alternative widget mapper + +void qPRCreate( const QWidget *widget, Window oldwin ) +{ // QWidget::reparent mechanism + if ( !wPRmapper ) { + wPRmapper = new QWidgetIntDict; + Q_CHECK_PTR( wPRmapper ); + } + wPRmapper->insert( (long)oldwin, widget ); // add old window to mapper + QETWidget *w = (QETWidget *)widget; + w->setWState( Qt::WState_Reparented ); // set reparented flag +} + +void qPRCleanup( QWidget *widget ) +{ + QETWidget *etw = (QETWidget *)widget; + if ( !(wPRmapper && etw->testWState(Qt::WState_Reparented)) ) + return; // not a reparented widget + QWidgetIntDictIt it(*wPRmapper); + QWidget *w; + while ( (w=it.current()) ) { + int key = it.currentKey(); + ++it; + if ( w == etw ) { // found widget + etw->clearWState( Qt::WState_Reparented ); // clear flag + wPRmapper->remove( key );// old window no longer needed + if ( wPRmapper->count() == 0 ) { // became empty + delete wPRmapper; // then reset alt mapper + wPRmapper = 0; + return; + } + } + } +} + +static QETWidget *qPRFindWidget( Window oldwin ) +{ + return wPRmapper ? (QETWidget*)wPRmapper->find((long)oldwin) : 0; +} + +/*! + \internal +*/ +int QApplication::x11ClientMessage(QWidget* w, XEvent* event, bool passive_only) +{ + QETWidget *widget = (QETWidget*)w; + if ( event->xclient.format == 32 && event->xclient.message_type ) { + if ( event->xclient.message_type == qt_wm_protocols ) { + Atom a = event->xclient.data.l[0]; + if ( a == qt_wm_delete_window ) { + if ( passive_only ) return 0; + widget->translateCloseEvent(event); + } + else if ( a == qt_wm_take_focus ) { + QWidget * amw = activeModalWidget(); + if ( (ulong) event->xclient.data.l[1] > qt_x_time ) + qt_x_time = event->xclient.data.l[1]; + if ( amw && amw != widget ) { + QWidget* groupLeader = widget; + while ( groupLeader && !groupLeader->testWFlags( Qt::WGroupLeader ) + && groupLeader != amw ) + groupLeader = groupLeader->parentWidget(); + if ( !groupLeader ) { + QWidget *p = amw->parentWidget(); + while (p && p != widget) + p = p->parentWidget(); + if (!p || !qt_net_supported_list) + amw->raise(); // help broken window managers + amw->setActiveWindow(); + } + } +#ifndef QT_NO_WHATSTHIS + } else if ( a == qt_net_wm_context_help ) { + QWhatsThis::enterWhatsThisMode(); +#endif // QT_NO_WHATSTHIS + } else if ( a == qt_net_wm_ping ) { + // avoid send/reply loops + Window root = QPaintDevice::x11AppRootWindow( w->x11Screen() ); + if (event->xclient.window != root) { + event->xclient.window = root; + XSendEvent( event->xclient.display, event->xclient.window, + False, SubstructureNotifyMask|SubstructureRedirectMask, event ); + } +#ifndef QT_NO_XSYNC + } else if (a == qt_net_wm_sync_request ) { + widget->handleSyncRequest( event ); +#endif + } + } else if ( event->xclient.message_type == qt_qt_scrolldone ) { + widget->translateScrollDoneEvent(event); + } else if ( event->xclient.message_type == qt_xdnd_position ) { + qt_handle_xdnd_position( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_enter ) { + qt_handle_xdnd_enter( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_status ) { + qt_handle_xdnd_status( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_leave ) { + qt_handle_xdnd_leave( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_drop ) { + qt_handle_xdnd_drop( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_finished ) { + qt_handle_xdnd_finished( widget, event, passive_only ); + } else { + if ( passive_only ) return 0; + // All other are interactions + } + } else { + qt_motifdnd_handle_msg( widget, event, passive_only ); + } + + return 0; +} + +/*! + This function does the core processing of individual X + \a{event}s, normally by dispatching Qt events to the right + destination. + + It returns 1 if the event was consumed by special handling, 0 if + the \a event was consumed by normal handling, and -1 if the \a + event was for an unrecognized widget. + + \sa x11EventFilter() +*/ +int QApplication::x11ProcessEvent( XEvent* event ) +{ + switch ( event->type ) { + case ButtonPress: + ignoreNextMouseReleaseEvent = FALSE; + qt_x_user_time = event->xbutton.time; + // fallthrough intended + case ButtonRelease: + qt_x_time = event->xbutton.time; + break; + case MotionNotify: + qt_x_time = event->xmotion.time; + break; + case XKeyPress: + qt_x_user_time = event->xkey.time; + // fallthrough intended + case XKeyRelease: + qt_x_time = event->xkey.time; + break; + case PropertyNotify: + qt_x_time = event->xproperty.time; + break; + case EnterNotify: + case LeaveNotify: + qt_x_time = event->xcrossing.time; + break; + case SelectionClear: + qt_x_time = event->xselectionclear.time; + break; + default: + break; + } + + QETWidget *widget = (QETWidget*)QWidget::find( (WId)event->xany.window ); + + if ( wPRmapper ) { // just did a widget reparent? + if ( widget == 0 ) { // not in std widget mapper + switch ( event->type ) { // only for mouse/key events + case ButtonPress: + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + widget = qPRFindWidget( event->xany.window ); + break; + } + } + else if ( widget->testWState(WState_Reparented) ) + qPRCleanup( widget ); // remove from alt mapper + } + + QETWidget *keywidget=0; + bool grabbed=FALSE; + if ( event->type==XKeyPress || event->type==XKeyRelease ) { + keywidget = (QETWidget*)QWidget::keyboardGrabber(); + if ( keywidget ) { + grabbed = TRUE; + } else { + if ( focus_widget ) + keywidget = (QETWidget*)focus_widget; + if ( !keywidget ) { + if ( inPopupMode() ) // no focus widget, see if we have a popup + keywidget = (QETWidget*) activePopupWidget(); + else if ( widget ) + keywidget = (QETWidget*)widget->topLevelWidget(); + } + } + } + +#ifndef QT_NO_IM + // Filtering input events by the input context. It has to be taken + // place before any other key event consumers such as eventfilters + // and accelerators because some input methods require quite + // various key combination and sequences. It often conflicts with + // accelerators and so on, so we must give the input context the + // filtering opportunity first to ensure all input methods work + // properly regardless of application design. + +// #ifndef QT_NO_IM_EXTENSIONS + if( keywidget && keywidget->isEnabled() && keywidget->isInputMethodEnabled() ) { +// #else +// if( keywidget && keywidget->isEnabled() ) { +// #endif + if( ( event->type==XKeyPress || event->type==XKeyRelease ) && + sm_blockUserInput ) // block user interaction during session management + return TRUE; + + // for XIM handling + QInputContext *qic = keywidget->getInputContext(); + if( qic && qic->x11FilterEvent( keywidget, event ) ) + return TRUE; + + // filterEvent() accepts QEvent *event rather than preexpanded key + // event attribute values. This is intended to pass other IM-related + // events in future. The IM-related events are supposed as + // QWheelEvent, QTabletEvent and so on. Other non IM-related events + // should not be forwarded to input contexts to prevent weird event + // handling. + if ( ( event->type == XKeyPress || event->type == XKeyRelease ) ) { + int code = -1; + int count = 0; + int state; + char ascii = 0; + QEvent::Type type; + QString text; + + keywidget->translateKeyEventInternal( event, count, text, + state, ascii, code, type, + FALSE, FALSE ); + + // both key press/release is required for some complex + // input methods. don't eliminate anything. + QKeyEvent keyevent( type, code, ascii, state, text, FALSE, count ); + + if( qic && qic->filterEvent( &keyevent ) ) + return TRUE; + } + } else +#endif // QT_NO_IM + { + if ( XFilterEvent( event, None ) ) + return TRUE; + } + + if ( qt_x11EventFilter(event) ) // send through app filter + return 1; + + if ( event->type == MappingNotify ) { // keyboard mapping changed + XRefreshKeyboardMapping( &event->xmapping ); + return 0; + } + + if ( event->type == PropertyNotify ) { // some properties changed + if ( event->xproperty.window == QPaintDevice::x11AppRootWindow( 0 ) ) { + // root properties for the first screen + if ( event->xproperty.atom == qt_clipboard_sentinel ) { + if (qt_check_clipboard_sentinel() ) + emit clipboard()->dataChanged(); + } else if ( event->xproperty.atom == qt_selection_sentinel ) { + if (qt_check_selection_sentinel() ) + emit clipboard()->selectionChanged(); + } else if ( obey_desktop_settings ) { + if ( event->xproperty.atom == qt_resource_manager ) + qt_set_x11_resources(); + else if ( event->xproperty.atom == qt_settings_timestamp ) + QApplication::x11_apply_settings(); + } + } + if ( event->xproperty.window == QPaintDevice::x11AppRootWindow() ) { + // root properties for the default screen + if ( event->xproperty.atom == qt_input_encoding ) { + qt_set_input_encoding(); + } else if ( event->xproperty.atom == qt_net_supported ) { + qt_get_net_supported(); + } else if ( event->xproperty.atom == qt_net_virtual_roots ) { + qt_get_net_virtual_roots(); + } else if ( event->xproperty.atom == qt_net_workarea ) { + qt_desktopwidget_update_workarea(); + } + } else if ( widget ) { + widget->translatePropertyEvent(event); + } else { + return -1; // don't know this window + } + return 0; + } + + if ( !widget ) { // don't know this windows + QWidget* popup = QApplication::activePopupWidget(); + if ( popup ) { + + /* + That is more than suboptimal. The real solution should + do some keyevent and buttonevent translation, so that + the popup still continues to work as the user expects. + Unfortunately this translation is currently only + possible with a known widget. I'll change that soon + (Matthias). + */ + + // Danger - make sure we don't lock the server + switch ( event->type ) { + case ButtonPress: + case ButtonRelease: + case XKeyPress: + case XKeyRelease: + do { + popup->close(); + } while ( (popup = qApp->activePopupWidget()) ); + return 1; + } + } + return -1; + } + + if ( event->type == XKeyPress || event->type == XKeyRelease ) + widget = keywidget; // send XKeyEvents through keywidget->x11Event() + + if ( app_do_modal ) // modal event handling + if ( !qt_try_modal(widget, event) ) { + if ( event->type == ClientMessage ) + x11ClientMessage( widget, event, TRUE ); + return 1; + } + + + if ( widget->x11Event(event) ) // send through widget filter + return 1; +#if defined (QT_TABLET_SUPPORT) + if ( event->type == xinput_motion || + event->type == xinput_button_release || + event->type == xinput_button_press ) { + widget->translateXinputEvent( event ); + return 0; + } +#endif + +#ifndef QT_NO_XRANDR + if (event->type == xrandr_eventbase + RRScreenChangeNotify + || ( event->type == ConfigureNotify && event->xconfigure.window == QPaintDevice::x11AppRootWindow())) { + // update Xlib internals with the latest screen configuration + XRRUpdateConfiguration(event); + + // update the size for desktop widget + int scr = XRRRootToScreen( appDpy, event->xany.window ); + QWidget *w = desktop()->screen( scr ); + QSize oldSize( w->size() ); + w->crect.setWidth( DisplayWidth( appDpy, scr ) ); + w->crect.setHeight( DisplayHeight( appDpy, scr ) ); + if ( w->size() != oldSize ) { + QResizeEvent e( w->size(), oldSize ); + QApplication::sendEvent( w, &e ); + emit desktop()->resized( scr ); + } + } +#endif // QT_NO_XRANDR + + switch ( event->type ) { + + case ButtonRelease: // mouse event + if ( ignoreNextMouseReleaseEvent ) { + ignoreNextMouseReleaseEvent = FALSE; + break; + } + // fall through intended + case ButtonPress: + if (event->xbutton.root != RootWindow(widget->x11Display(), widget->x11Screen()) + && ! qt_xdnd_dragging) { + while ( activePopupWidget() ) + activePopupWidget()->close(); + return 1; + } + if (event->type == ButtonPress) + qt_net_update_user_time(widget->topLevelWidget()); + // fall through intended + case MotionNotify: +#if defined(QT_TABLET_SUPPORT) + if ( !chokeMouse ) { +#endif + widget->translateMouseEvent( event ); +#if defined(QT_TABLET_SUPPORT) + } else { + chokeMouse = FALSE; + } +#endif + break; + + case XKeyPress: // keyboard event + qt_net_update_user_time(widget->topLevelWidget()); + // fallthrough intended + case XKeyRelease: + { + if ( keywidget && keywidget->isEnabled() ) { // should always exist + // qDebug( "sending key event" ); + keywidget->translateKeyEvent( event, grabbed ); + } + break; + } + + case GraphicsExpose: + case Expose: // paint event + widget->translatePaintEvent( event ); + break; + + case ConfigureNotify: // window move/resize event + if ( event->xconfigure.event == event->xconfigure.window ) + widget->translateConfigEvent( event ); + break; + + case XFocusIn: { // got focus + if ( widget->isDesktop() ) + break; + if ( inPopupMode() ) // some delayed focus event to ignore + break; + if ( !widget->isTopLevel() ) + break; + if ( event->xfocus.detail != NotifyAncestor && + event->xfocus.detail != NotifyInferior && + event->xfocus.detail != NotifyNonlinear ) + break; + widget->createInputContext(); + setActiveWindow( widget ); + if ( qt_focus_model == FocusModel_PointerRoot ) { + // We got real input focus from somewhere, but we were in PointerRoot + // mode, so we don't trust this event. Check the focus model to make + // sure we know what focus mode we are using... + qt_check_focus_model(); + } + } + break; + + case XFocusOut: // lost focus + if ( widget->isDesktop() ) + break; + if ( !widget->isTopLevel() ) + break; + if ( event->xfocus.mode == NotifyGrab ) + qt_xfocusout_grab_counter++; + if ( event->xfocus.mode != NotifyNormal ) + break; + if ( event->xfocus.detail != NotifyAncestor && + event->xfocus.detail != NotifyNonlinearVirtual && + event->xfocus.detail != NotifyNonlinear ) + break; + if ( !inPopupMode() && widget == active_window ) + setActiveWindow( 0 ); + break; + + case EnterNotify: { // enter window + if ( QWidget::mouseGrabber() && widget != QWidget::mouseGrabber() ) + break; + if ( inPopupMode() && widget->topLevelWidget() != activePopupWidget() ) + break; + if ( event->xcrossing.mode != NotifyNormal || + event->xcrossing.detail == NotifyVirtual || + event->xcrossing.detail == NotifyNonlinearVirtual ) + break; + if ( event->xcrossing.focus && + !widget->isDesktop() && !widget->isActiveWindow() ) { + if ( qt_focus_model == FocusModel_Unknown ) // check focus model + qt_check_focus_model(); + if ( qt_focus_model == FocusModel_PointerRoot ) // PointerRoot mode + setActiveWindow( widget ); + } + qt_dispatchEnterLeave( widget, QWidget::find( curWin ) ); + curWin = widget->winId(); + widget->translateMouseEvent( event ); //we don't get MotionNotify, emulate it + } + break; + + case LeaveNotify: { // leave window + if ( QWidget::mouseGrabber() && widget != QWidget::mouseGrabber() ) + break; + if ( curWin && widget->winId() != curWin ) + break; + if ( event->xcrossing.mode != NotifyNormal ) + break; + if ( !widget->isDesktop() ) + widget->translateMouseEvent( event ); //we don't get MotionNotify, emulate it + + QWidget* enter = 0; + XEvent ev; + while ( XCheckMaskEvent( widget->x11Display(), EnterWindowMask | LeaveWindowMask , &ev ) + && !qt_x11EventFilter( &ev )) { + QWidget* event_widget = QWidget::find( ev.xcrossing.window ); + if( event_widget && event_widget->x11Event( &ev ) ) + break; + if ( ev.type == LeaveNotify && ev.xcrossing.mode == NotifyNormal ){ + enter = event_widget; + XPutBackEvent( widget->x11Display(), &ev ); + break; + } + if ( ev.xcrossing.mode != NotifyNormal || + ev.xcrossing.detail == NotifyVirtual || + ev.xcrossing.detail == NotifyNonlinearVirtual ) + continue; + enter = event_widget; + if ( ev.xcrossing.focus && + enter && !enter->isDesktop() && !enter->isActiveWindow() ) { + if ( qt_focus_model == FocusModel_Unknown ) // check focus model + qt_check_focus_model(); + if ( qt_focus_model == FocusModel_PointerRoot ) // PointerRoot mode + setActiveWindow( enter ); + } + break; + } + + if ( ( ! enter || enter->isDesktop() ) && + event->xcrossing.focus && widget == active_window && + qt_focus_model == FocusModel_PointerRoot // PointerRoot mode + ) { + setActiveWindow( 0 ); + } + + if ( !curWin ) + qt_dispatchEnterLeave( widget, 0 ); + + qt_dispatchEnterLeave( enter, widget ); + curWin = enter ? enter->winId() : 0; + } + break; + + case UnmapNotify: // window hidden + if ( widget->isTopLevel() && widget->isShown() ) { + widget->topData()->spont_unmapped = 1; + QHideEvent e; + QApplication::sendSpontaneousEvent( widget, &e ); + widget->hideChildren( TRUE ); + } + break; + + case MapNotify: // window shown + if ( widget->isTopLevel() && + widget->topData()->spont_unmapped ) { + widget->topData()->spont_unmapped = 0; + widget->showChildren( TRUE ); + QShowEvent e; + QApplication::sendSpontaneousEvent( widget, &e ); + } + break; + + case ClientMessage: // client message + return x11ClientMessage(widget,event,False); + + case ReparentNotify: // window manager reparents + while ( XCheckTypedWindowEvent( widget->x11Display(), + widget->winId(), + ReparentNotify, + event ) ) + ; // skip old reparent events + if ( event->xreparent.parent == QPaintDevice::x11AppRootWindow() ) { + if ( widget->isTopLevel() ) { + widget->topData()->parentWinId = event->xreparent.parent; + if ( qt_deferred_map_contains( widget ) ) { + qt_deferred_map_take( widget ); + XMapWindow( appDpy, widget->winId() ); + } + } + } else + // store the parent. Useful for many things, embedding for instance. + widget->topData()->parentWinId = event->xreparent.parent; + if ( widget->isTopLevel() ) { + // the widget frame strut should also be invalidated + widget->topData()->fleft = widget->topData()->fright = + widget->topData()->ftop = widget->topData()->fbottom = 0; + + if ( qt_focus_model != FocusModel_Unknown ) { + // toplevel reparented... + QWidget *newparent = QWidget::find( event->xreparent.parent ); + if ( ! newparent || newparent->isDesktop() ) { + // we dont' know about the new parent (or we've been + // reparented to root), perhaps a window manager + // has been (re)started? reset the focus model to unknown + qt_focus_model = FocusModel_Unknown; + } + } + } + break; + + case SelectionRequest: { + XSelectionRequestEvent *req = &event->xselectionrequest; + if (! req) + break; + + if ( qt_xdnd_selection && req->selection == qt_xdnd_selection ) { + qt_xdnd_handle_selection_request( req ); + + } else if (qt_clipboard) { + QCustomEvent e( QEvent::Clipboard, event ); + QApplication::sendSpontaneousEvent( qt_clipboard, &e ); + } + break; + } + case SelectionClear: { + XSelectionClearEvent *req = &event->xselectionclear; + // don't deliver dnd events to the clipboard, it gets confused + if (! req || qt_xdnd_selection && req->selection == qt_xdnd_selection) + break; + + if (qt_clipboard) { + QCustomEvent e( QEvent::Clipboard, event ); + QApplication::sendSpontaneousEvent( qt_clipboard, &e ); + } + break; + } + + case SelectionNotify: { + XSelectionEvent *req = &event->xselection; + // don't deliver dnd events to the clipboard, it gets confused + if (! req || qt_xdnd_selection && req->selection == qt_xdnd_selection) + break; + + if (qt_clipboard) { + QCustomEvent e( QEvent::Clipboard, event ); + QApplication::sendSpontaneousEvent( qt_clipboard, &e ); + } + break; + } + + default: + break; + } + + return 0; +} + +/*! + This virtual function is only implemented under X11. + + If you create an application that inherits QApplication and + reimplement this function, you get direct access to all X events + that the are received from the X server. + + Return TRUE if you want to stop the event from being processed. + Return FALSE for normal event dispatching. + + \sa x11ProcessEvent() +*/ + +bool QApplication::x11EventFilter( XEvent * ) +{ + return FALSE; +} + + + +/***************************************************************************** + Modal widgets; Since Xlib has little support for this we roll our own + modal widget mechanism. + A modal widget without a parent becomes application-modal. + A modal widget with a parent becomes modal to its parent and grandparents.. + + qt_enter_modal() + Enters modal state + Arguments: + QWidget *widget A modal widget + + qt_leave_modal() + Leaves modal state for a widget + Arguments: + QWidget *widget A modal widget + *****************************************************************************/ + +bool qt_modal_state() +{ + return app_do_modal; +} + +void qt_enter_modal( QWidget *widget ) +{ + if ( !qt_modal_stack ) { // create modal stack + qt_modal_stack = new QWidgetList; + Q_CHECK_PTR( qt_modal_stack ); + } + if (widget->parentWidget()) { + QEvent e(QEvent::WindowBlocked); + QApplication::sendEvent(widget->parentWidget(), &e); + } + + qt_dispatchEnterLeave( 0, QWidget::find((WId)curWin) ); + qt_modal_stack->insert( 0, widget ); + app_do_modal = TRUE; + curWin = 0; + ignoreNextMouseReleaseEvent = FALSE; +} + + +void qt_leave_modal( QWidget *widget ) +{ + if ( qt_modal_stack && qt_modal_stack->removeRef(widget) ) { + if ( qt_modal_stack->isEmpty() ) { + delete qt_modal_stack; + qt_modal_stack = 0; + QPoint p( QCursor::pos() ); + QWidget* w = QApplication::widgetAt( p.x(), p.y(), TRUE ); + qt_dispatchEnterLeave( w, QWidget::find( curWin ) ); // send synthetic enter event + curWin = w? w->winId() : 0; + } + } + app_do_modal = qt_modal_stack != 0; + ignoreNextMouseReleaseEvent = TRUE; + + if (widget->parentWidget()) { + QEvent e(QEvent::WindowUnblocked); + QApplication::sendEvent(widget->parentWidget(), &e); + } +} + + +bool qt_try_modal( QWidget *widget, XEvent *event ) +{ + if (qt_xdnd_dragging) { + // allow mouse events while DnD is active + switch (event->type) { + case ButtonPress: + case ButtonRelease: + case MotionNotify: + return TRUE; + default: + break; + } + } + + if ( qt_tryModalHelper( widget ) ) + return TRUE; + + bool block_event = FALSE; + switch ( event->type ) { + case ButtonPress: // disallow mouse/key events + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + case EnterNotify: + case LeaveNotify: + case ClientMessage: + block_event = TRUE; + break; + default: + break; + } + + return !block_event; +} + + +/***************************************************************************** + Popup widget mechanism + + openPopup() + Adds a widget to the list of popup widgets + Arguments: + QWidget *widget The popup widget to be added + + closePopup() + Removes a widget from the list of popup widgets + Arguments: + QWidget *widget The popup widget to be removed + *****************************************************************************/ + + +static int openPopupCount = 0; +void QApplication::openPopup( QWidget *popup ) +{ + openPopupCount++; + if ( !popupWidgets ) { // create list + popupWidgets = new QWidgetList; + Q_CHECK_PTR( popupWidgets ); + } + popupWidgets->append( popup ); // add to end of list + + if ( popupWidgets->count() == 1 && !qt_nograb() ){ // grab mouse/keyboard + int r = XGrabKeyboard( popup->x11Display(), popup->winId(), FALSE, + GrabModeSync, GrabModeAsync, CurrentTime ); + if ( (popupGrabOk = (r == GrabSuccess)) ) { + r = XGrabPointer( popup->x11Display(), popup->winId(), TRUE, + (uint)(ButtonPressMask | ButtonReleaseMask | + ButtonMotionMask | EnterWindowMask | + LeaveWindowMask | PointerMotionMask), + GrabModeSync, GrabModeAsync, + None, None, CurrentTime ); + + if ( (popupGrabOk = (r == GrabSuccess)) ) + XAllowEvents( popup->x11Display(), SyncPointer, CurrentTime ); + else + XUngrabKeyboard( popup->x11Display(), CurrentTime ); + } + } else if ( popupGrabOk ) { + XAllowEvents( popup->x11Display(), SyncPointer, CurrentTime ); + } + + // popups are not focus-handled by the window system (the first + // popup grabbed the keyboard), so we have to do that manually: A + // new popup gets the focus + QFocusEvent::setReason( QFocusEvent::Popup ); + if ( popup->focusWidget()) + popup->focusWidget()->setFocus(); + else + popup->setFocus(); + QFocusEvent::resetReason(); +} + +void QApplication::closePopup( QWidget *popup ) +{ + if ( !popupWidgets ) + return; + popupWidgets->removeRef( popup ); + if (popup == popupOfPopupButtonFocus) { + popupButtonFocus = 0; + popupOfPopupButtonFocus = 0; + } + if ( popupWidgets->count() == 0 ) { // this was the last popup + popupCloseDownMode = TRUE; // control mouse events + delete popupWidgets; + popupWidgets = 0; + if ( !qt_nograb() && popupGrabOk ) { // grabbing not disabled + if ( mouseButtonState != 0 + || popup->geometry(). contains(QPoint(mouseGlobalXPos, mouseGlobalYPos) ) ) + { // mouse release event or inside + XAllowEvents( popup->x11Display(), AsyncPointer, + CurrentTime ); + } else { // mouse press event + mouseButtonPressTime -= 10000; // avoid double click + XAllowEvents( popup->x11Display(), ReplayPointer,CurrentTime ); + } + XUngrabPointer( popup->x11Display(), CurrentTime ); + XFlush( popup->x11Display() ); + } + if ( active_window ) { + QFocusEvent::setReason( QFocusEvent::Popup ); + if ( active_window->focusWidget() ) + active_window->focusWidget()->setFocus(); + else + active_window->setFocus(); + QFocusEvent::resetReason(); + } + } else { + // popups are not focus-handled by the window system (the + // first popup grabbed the keyboard), so we have to do that + // manually: A popup was closed, so the previous popup gets + // the focus. + QFocusEvent::setReason( QFocusEvent::Popup ); + QWidget* aw = popupWidgets->getLast(); + if (aw->focusWidget()) + aw->focusWidget()->setFocus(); + else + aw->setFocus(); + QFocusEvent::resetReason(); + if ( popupWidgets->count() == 1 && !qt_nograb() ){ // grab mouse/keyboard + int r = XGrabKeyboard( aw->x11Display(), aw->winId(), FALSE, + GrabModeSync, GrabModeAsync, CurrentTime ); + if ( (popupGrabOk = (r == GrabSuccess)) ) { + r = XGrabPointer( aw->x11Display(), aw->winId(), TRUE, + (uint)(ButtonPressMask | ButtonReleaseMask | + ButtonMotionMask | EnterWindowMask | + LeaveWindowMask | PointerMotionMask), + GrabModeSync, GrabModeAsync, + None, None, CurrentTime ); + + if ( (popupGrabOk = (r == GrabSuccess)) ) + XAllowEvents( aw->x11Display(), SyncPointer, CurrentTime ); + } + } + } +} + +/***************************************************************************** + Event translation; translates X11 events to Qt events + *****************************************************************************/ + +// +// Mouse event translation +// +// Xlib doesn't give mouse double click events, so we generate them by +// comparing window, time and position between two mouse press events. +// + +// +// Keyboard event translation +// + +int qt_x11_translateButtonState( int s ) +{ + int bst = 0; + if ( s & Button1Mask ) + bst |= Qt::LeftButton; + if ( s & Button2Mask ) + bst |= Qt::MidButton; + if ( s & Button3Mask ) + bst |= Qt::RightButton; + if ( s & ShiftMask ) + bst |= Qt::ShiftButton; + if ( s & ControlMask ) + bst |= Qt::ControlButton; + if ( s & qt_alt_mask ) + bst |= Qt::AltButton; + if ( s & qt_meta_mask ) + bst |= Qt::MetaButton; + return bst; +} + +bool QETWidget::translateMouseEvent( const XEvent *event ) +{ + static bool manualGrab = FALSE; + QEvent::Type type; // event parameters + QPoint pos; + QPoint globalPos; + int button = 0; + int state; + XEvent nextEvent; + + if ( sm_blockUserInput ) // block user interaction during session management + return TRUE; + + static int x_root_save = -1, y_root_save = -1; + + if ( event->type == MotionNotify ) { // mouse move + if (event->xmotion.root != RootWindow(appDpy, x11Screen()) && + ! qt_xdnd_dragging ) + return FALSE; + + XMotionEvent lastMotion = event->xmotion; + while( XPending( appDpy ) ) { // compres mouse moves + XNextEvent( appDpy, &nextEvent ); + if ( nextEvent.type == ConfigureNotify + || nextEvent.type == PropertyNotify + || nextEvent.type == Expose + || nextEvent.type == NoExpose ) { + qApp->x11ProcessEvent( &nextEvent ); + continue; + } else if ( nextEvent.type != MotionNotify || + nextEvent.xmotion.window != event->xmotion.window || + nextEvent.xmotion.state != event->xmotion.state ) { + XPutBackEvent( appDpy, &nextEvent ); + break; + } + if ( !qt_x11EventFilter(&nextEvent) + && !x11Event( &nextEvent ) ) // send event through filter + lastMotion = nextEvent.xmotion; + else + break; + } + type = QEvent::MouseMove; + pos.rx() = lastMotion.x; + pos.ry() = lastMotion.y; + globalPos.rx() = lastMotion.x_root; + globalPos.ry() = lastMotion.y_root; + state = qt_x11_translateButtonState( lastMotion.state ); + if ( qt_button_down && (state & (LeftButton | + MidButton | + RightButton ) ) == 0 ) + qt_button_down = 0; + + // throw away mouse move events that are sent multiple times to the same + // position + bool throw_away = FALSE; + if ( x_root_save == globalPos.x() && + y_root_save == globalPos.y() ) + throw_away = TRUE; + x_root_save = globalPos.x(); + y_root_save = globalPos.y(); + if ( throw_away ) + return TRUE; + } else if ( event->type == EnterNotify || event->type == LeaveNotify) { + XEvent *xevent = (XEvent *)event; + //unsigned int xstate = event->xcrossing.state; + type = QEvent::MouseMove; + pos.rx() = xevent->xcrossing.x; + pos.ry() = xevent->xcrossing.y; + globalPos.rx() = xevent->xcrossing.x_root; + globalPos.ry() = xevent->xcrossing.y_root; + state = qt_x11_translateButtonState( xevent->xcrossing.state ); + if ( qt_button_down && (state & (LeftButton | + MidButton | + RightButton ) ) == 0 ) + qt_button_down = 0; + if ( !qt_button_down ) + state = state & ~(LeftButton | MidButton | RightButton ); + } else { // button press or release + pos.rx() = event->xbutton.x; + pos.ry() = event->xbutton.y; + globalPos.rx() = event->xbutton.x_root; + globalPos.ry() = event->xbutton.y_root; + state = qt_x11_translateButtonState( event->xbutton.state ); + switch ( event->xbutton.button ) { + case Button1: button = LeftButton; break; + case Button2: button = MidButton; break; + case Button3: button = RightButton; break; + case Button4: + case Button5: + case 6: + case 7: + // the fancy mouse wheel. + + // take care about grabbing. We do this here since it + // is clear that we return anyway + if ( qApp->inPopupMode() && popupGrabOk ) + XAllowEvents( x11Display(), SyncPointer, CurrentTime ); + + // We are only interested in ButtonPress. + if (event->type == ButtonPress ){ + + // compress wheel events (the X Server will simply + // send a button press for each single notch, + // regardless whether the application can catch up + // or not) + int delta = 1; + XEvent xevent; + while ( XCheckTypedWindowEvent(x11Display(),winId(), + ButtonPress,&xevent) ){ + if (xevent.xbutton.button != event->xbutton.button){ + XPutBackEvent(x11Display(), &xevent); + break; + } + delta++; + } + + // the delta is defined as multiples of + // WHEEL_DELTA, which is set to 120. Future wheels + // may offer a finer-resolution. A positive delta + // indicates forward rotation, a negative one + // backward rotation respectively. + int btn = event->xbutton.button; + delta *= 120 * ( (btn == Button4 || btn == 6) ? 1 : -1 ); + bool hor = ( (btn == Button4 || btn == Button5) && (state&AltButton) || + (btn == 6 || btn == 7) ); + translateWheelEvent( globalPos.x(), globalPos.y(), delta, state, (hor)?Horizontal:Vertical ); + } + return TRUE; + } + if ( event->type == ButtonPress ) { // mouse button pressed +#if defined(Q_OS_IRIX) && defined(QT_TABLET_SUPPORT) + XEvent myEv; + if ( XCheckTypedEvent( appDpy, xinput_button_press, &myEv ) ) { + if ( translateXinputEvent( &myEv ) ) { + //Spontaneous event sent. Check if we need to continue. + if ( chokeMouse ) { + chokeMouse = FALSE; + return FALSE; + } + } + } +#endif + qt_button_down = childAt( pos ); //magic for masked widgets + if ( !qt_button_down || !qt_button_down->testWFlags(WMouseNoMask) ) + qt_button_down = this; + if ( mouseActWindow == event->xbutton.window && + mouseButtonPressed == button && + (long)event->xbutton.time -(long)mouseButtonPressTime + < QApplication::doubleClickInterval() && + QABS(event->xbutton.x - mouseXPos) < 5 && + QABS(event->xbutton.y - mouseYPos) < 5 ) { + type = QEvent::MouseButtonDblClick; + mouseButtonPressTime -= 2000; // no double-click next time + } else { + type = QEvent::MouseButtonPress; + mouseButtonPressTime = event->xbutton.time; + } + mouseButtonPressed = button; // save event params for + mouseXPos = pos.x(); // future double click tests + mouseYPos = pos.y(); + mouseGlobalXPos = globalPos.x(); + mouseGlobalYPos = globalPos.y(); + } else { // mouse button released +#if defined(Q_OS_IRIX) && defined(QT_TABLET_SUPPORT) + XEvent myEv; + if ( XCheckTypedEvent( appDpy, xinput_button_release, &myEv ) ) { + if ( translateXinputEvent( &myEv ) ) { + //Spontaneous event sent. Check if we need to continue. + if ( chokeMouse ) { + chokeMouse = FALSE; + return FALSE; + } + } + } +#endif + if ( manualGrab ) { // release manual grab + manualGrab = FALSE; + XUngrabPointer( x11Display(), CurrentTime ); + XFlush( x11Display() ); + } + + type = QEvent::MouseButtonRelease; + } + } + mouseActWindow = winId(); // save some event params + mouseButtonState = state; + if ( type == 0 ) // don't send event + return FALSE; + + if ( qApp->inPopupMode() ) { // in popup mode + QWidget *popup = qApp->activePopupWidget(); + if ( popup != this ) { + if ( testWFlags(WType_Popup) && rect().contains(pos) ) + popup = this; + else // send to last popup + pos = popup->mapFromGlobal( globalPos ); + } + bool releaseAfter = FALSE; + QWidget *popupChild = popup->childAt( pos ); + QWidget *popupTarget = popupChild ? popupChild : popup; + + if (popup != popupOfPopupButtonFocus){ + popupButtonFocus = 0; + popupOfPopupButtonFocus = 0; + } + + if ( !popupTarget->isEnabled() ) { + if ( popupGrabOk ) + XAllowEvents( x11Display(), SyncPointer, CurrentTime ); + } + + switch ( type ) { + case QEvent::MouseButtonPress: + case QEvent::MouseButtonDblClick: + popupButtonFocus = popupChild; + popupOfPopupButtonFocus = popup; + break; + case QEvent::MouseButtonRelease: + releaseAfter = TRUE; + break; + default: + break; // nothing for mouse move + } + + Display* dpy = x11Display(); // store display, send() may destroy us + + + int oldOpenPopupCount = openPopupCount; + + if ( popupButtonFocus ) { + QMouseEvent e( type, popupButtonFocus->mapFromGlobal(globalPos), + globalPos, button, state ); + QApplication::sendSpontaneousEvent( popupButtonFocus, &e ); + if ( releaseAfter ) { + popupButtonFocus = 0; + popupOfPopupButtonFocus = 0; + } + } else if ( popupChild ) { + QMouseEvent e( type, popupChild->mapFromGlobal(globalPos), + globalPos, button, state ); + QApplication::sendSpontaneousEvent( popupChild, &e ); + } else { + QMouseEvent e( type, pos, globalPos, button, state ); + QApplication::sendSpontaneousEvent( popup, &e ); + } + + if ( type == QEvent::MouseButtonPress && button == RightButton && ( openPopupCount == oldOpenPopupCount ) ) { + QWidget *popupEvent = popup; + if(popupButtonFocus) + popupEvent = popupButtonFocus; + else if(popupChild) + popupEvent = popupChild; + QContextMenuEvent e( QContextMenuEvent::Mouse, pos, globalPos, state ); + QApplication::sendSpontaneousEvent( popupEvent, &e ); + } + + if ( releaseAfter ) + qt_button_down = 0; + + if ( qApp->inPopupMode() ) { // still in popup mode + if ( popupGrabOk ) + XAllowEvents( dpy, SyncPointer, CurrentTime ); + } else { + if ( type != QEvent::MouseButtonRelease && state != 0 && + QWidget::find((WId)mouseActWindow) ) { + manualGrab = TRUE; // need to manually grab + XGrabPointer( dpy, mouseActWindow, False, + (uint)(ButtonPressMask | ButtonReleaseMask | + ButtonMotionMask | + EnterWindowMask | LeaveWindowMask), + GrabModeAsync, GrabModeAsync, + None, None, CurrentTime ); + } + } + + } else { + QWidget *widget = this; + QWidget *w = QWidget::mouseGrabber(); + if ( !w ) + w = qt_button_down; + if ( w && w != this ) { + widget = w; + pos = w->mapFromGlobal( globalPos ); + } + + if ( popupCloseDownMode ) { + popupCloseDownMode = FALSE; + if ( testWFlags(WType_Popup) ) // ignore replayed event + return TRUE; + } + + if ( type == QEvent::MouseButtonRelease && + (state & (~button) & ( LeftButton | + MidButton | + RightButton)) == 0 ) { + qt_button_down = 0; + } + + int oldOpenPopupCount = openPopupCount; + + QMouseEvent e( type, pos, globalPos, button, state ); + QApplication::sendSpontaneousEvent( widget, &e ); + + if ( type == QEvent::MouseButtonPress && button == RightButton && ( openPopupCount == oldOpenPopupCount ) ) { + QContextMenuEvent e( QContextMenuEvent::Mouse, pos, globalPos, state ); + QApplication::sendSpontaneousEvent( widget, &e ); + } + } + return TRUE; +} + + +// +// Wheel event translation +// +bool QETWidget::translateWheelEvent( int global_x, int global_y, int delta, int state, Orientation orient ) +{ + // send the event to the widget or its ancestors + { + QWidget* popup = qApp->activePopupWidget(); + if ( popup && topLevelWidget() != popup ) + popup->close(); + QWheelEvent e( mapFromGlobal(QPoint( global_x, global_y)), + QPoint(global_x, global_y), delta, state, orient ); + if ( QApplication::sendSpontaneousEvent( this, &e ) ) + return TRUE; + } + + // send the event to the widget that has the focus or its ancestors, if different + QWidget *w = this; + if ( w != qApp->focusWidget() && ( w = qApp->focusWidget() ) ) { + QWidget* popup = qApp->activePopupWidget(); + if ( popup && w != popup ) + popup->hide(); + QWheelEvent e( mapFromGlobal(QPoint( global_x, global_y)), + QPoint(global_x, global_y), delta, state, orient ); + if ( QApplication::sendSpontaneousEvent( w, &e ) ) + return TRUE; + } + return FALSE; +} + + +// +// XInput Translation Event +// +#if defined (QT_TABLET_SUPPORT) +bool QETWidget::translateXinputEvent( const XEvent *ev ) +{ +#if defined (Q_OS_IRIX) + // Wacom has put defines in their wacom.h file so it would be quite wise + // to use them, need to think of a decent way of not using + // it when it doesn't exist... + XDeviceState *s; + XInputClass *iClass; + XValuatorState *vs; + int j; +#endif + QWidget *w = this; + QPoint global, + curr; + static int pressure = 0; + static int xTilt = 0, + yTilt = 0; + int deviceType = QTabletEvent::NoDevice; + QPair<int, int> tId; + XEvent xinputMotionEvent; + XEvent mouseMotionEvent; + XDevice *dev; + const XDeviceMotionEvent *motion = 0; + XDeviceButtonEvent *button = 0; + QEvent::Type t; + + if ( ev->type == xinput_motion ) { + motion = (const XDeviceMotionEvent*)ev; + for (;;) { + if (!XCheckTypedWindowEvent(x11Display(), winId(), MotionNotify, &mouseMotionEvent)) + break; + if (!XCheckTypedWindowEvent(x11Display(), winId(), xinput_motion, &xinputMotionEvent)) { + XPutBackEvent(x11Display(), &mouseMotionEvent); + break; + } + if (mouseMotionEvent.xmotion.time != motion->time) { + XPutBackEvent(x11Display(), &mouseMotionEvent); + XPutBackEvent(x11Display(), &xinputMotionEvent); + break; + } + motion = ((const XDeviceMotionEvent*)&xinputMotionEvent); + } + t = QEvent::TabletMove; + curr = QPoint( motion->x, motion->y ); + } else { + if ( ev->type == xinput_button_press ) { + t = QEvent::TabletPress; + } else { + t = QEvent::TabletRelease; + } + button = (XDeviceButtonEvent*)ev; +/* + qDebug( "\n\nXInput Button Event" ); + qDebug( "serial:\t%d", button->serial ); + qDebug( "send_event:\t%d", button->send_event ); + qDebug( "display:\t%p", button->display ); + qDebug( "window:\t%d", button->window ); + qDebug( "deviceID:\t%d", button->deviceid ); + qDebug( "root:\t%d", button->root ); + qDebug( "subwindot:\t%d", button->subwindow ); + qDebug( "x:\t%d", button->x ); + qDebug( "y:\t%d", button->y ); + qDebug( "x_root:\t%d", button->x_root ); + qDebug( "y_root:\t%d", button->y_root ); + qDebug( "state:\t%d", button->state ); + qDebug( "button:\t%d", button->button ); + qDebug( "same_screen:\t%d", button->same_screen ); + qDebug( "time:\t%d", button->time ); +*/ + curr = QPoint( button->x, button->y ); + } +#if defined(Q_OS_IRIX) + // default... + dev = devStylus; +#else + if ( ev->type == xinput_motion ) { + if ( motion->deviceid == devStylus->device_id ) { + dev = devStylus; + deviceType = QTabletEvent::Stylus; + } else if ( motion->deviceid == devEraser->device_id ) { + dev = devEraser; + deviceType = QTabletEvent::Eraser; + } + } else { + if ( button->deviceid == devStylus->device_id ) { + dev = devStylus; + deviceType = QTabletEvent::Stylus; + } else if ( button->deviceid == devEraser->device_id ) { + dev = devEraser; + deviceType = QTabletEvent::Eraser; + } + } +#endif + + const int PRESSURE_LEVELS = 255; + // we got the maximum pressure at start time, since various tablets have + // varying levels of distinguishing pressure changes, let's standardize and + // scale everything to 256 different levels... + static int scaleFactor = -1; + if ( scaleFactor == -1 ) { + if ( max_pressure > PRESSURE_LEVELS ) + scaleFactor = max_pressure / PRESSURE_LEVELS; + else + scaleFactor = PRESSURE_LEVELS / max_pressure; + } +#if defined (Q_OS_IRIX) + s = XQueryDeviceState( appDpy, dev ); + if ( s == NULL ) + return FALSE; + iClass = s->data; + for ( j = 0; j < s->num_classes; j++ ) { + if ( iClass->c_class == ValuatorClass ) { + vs = (XValuatorState *)iClass; + // figure out what device we have, based on bitmasking... + if ( vs->valuators[WAC_TRANSDUCER_I] + & WAC_TRANSDUCER_PROX_MSK ) { + switch ( vs->valuators[WAC_TRANSDUCER_I] + & WAC_TRANSDUCER_MSK ) { + case WAC_PUCK_ID: + deviceType = QTabletEvent::Puck; + break; + case WAC_STYLUS_ID: + deviceType = QTabletEvent::Stylus; + break; + case WAC_ERASER_ID: + deviceType = QTabletEvent::Eraser; + break; + } + // Get a Unique Id for the device, Wacom gives us this ability + tId.first = vs->valuators[WAC_TRANSDUCER_I] & WAC_TRANSDUCER_ID_MSK; + tId.second = vs->valuators[WAC_SERIAL_NUM_I]; + } else + deviceType = QTabletEvent::NoDevice; + // apparently Wacom needs a cast for the +/- values to make sense + xTilt = short(vs->valuators[WAC_XTILT_I]); + yTilt = short(vs->valuators[WAC_YTILT_I]); + if ( max_pressure > PRESSURE_LEVELS ) + pressure = vs->valuators[WAC_PRESSURE_I] / scaleFactor; + else + pressure = vs->valuators[WAC_PRESSURE_I] * scaleFactor; + global = QPoint( vs->valuators[WAC_XCOORD_I], + vs->valuators[WAC_YCOORD_I] ); + break; + } + iClass = (XInputClass*)((char*)iClass + iClass->length); + } + XFreeDeviceState( s ); +#else + if ( motion ) { + xTilt = short(motion->axis_data[3]); + yTilt = short(motion->axis_data[4]); + if ( max_pressure > PRESSURE_LEVELS ) + pressure = motion->axis_data[2] / scaleFactor; + else + pressure = motion->axis_data[2] * scaleFactor; + global = QPoint( motion->axis_data[0], motion->axis_data[1] ); + } else { + xTilt = short(button->axis_data[3]); + yTilt = short(button->axis_data[4]); + if ( max_pressure > PRESSURE_LEVELS ) + pressure = button->axis_data[2] / scaleFactor; + else + pressure = button->axis_data[2] * scaleFactor; + global = QPoint( button->axis_data[0], button->axis_data[1] ); + } + // The only way to get these Ids is to scan the XFree86 log, which I'm not going to do. + tId.first = tId.second = -1; +#endif + + QTabletEvent e( t, curr, global, deviceType, pressure, xTilt, yTilt, tId ); + QApplication::sendSpontaneousEvent( w, &e ); + return TRUE; +} +#endif + +bool QETWidget::translatePropertyEvent(const XEvent *event) +{ + if (!isTopLevel()) return TRUE; + + Atom ret; + int format, e; + unsigned char *data = 0; + unsigned long nitems, after; + + if (event->xproperty.atom == qt_net_wm_frame_strut) { + topData()->fleft = topData()->fright = topData()->ftop = topData()->fbottom = 0; + fstrut_dirty = 1; + + if (event->xproperty.state == PropertyNewValue) { + e = XGetWindowProperty(appDpy, event->xproperty.window, qt_net_wm_frame_strut, + 0, 4, // struts are 4 longs + False, XA_CARDINAL, &ret, &format, &nitems, &after, &data); + + if (e == Success && ret == XA_CARDINAL && + format == 32 && nitems == 4) { + long *strut = (long *) data; + topData()->fleft = strut[0]; + topData()->fright = strut[1]; + topData()->ftop = strut[2]; + topData()->fbottom = strut[3]; + fstrut_dirty = 0; + } + } + } else if (event->xproperty.atom == qt_net_wm_state) { + bool max = FALSE; + bool full = FALSE; + + if (event->xproperty.state == PropertyNewValue) { + // using length of 1024 should be safe for all current and + // possible NET states... + e = XGetWindowProperty(appDpy, event->xproperty.window, qt_net_wm_state, 0, 1024, + False, XA_ATOM, &ret, &format, &nitems, &after, &data); + + if (e == Success && ret == XA_ATOM && format == 32 && nitems > 0) { + Atom *states = (Atom *) data; + + unsigned long i; + for (i = 0; i < nitems; i++) { + if (states[i] == qt_net_wm_state_max_v || states[i] == qt_net_wm_state_max_h) + max = TRUE; + else if (states[i] == qt_net_wm_state_fullscreen) + full = TRUE; + } + } + } + + bool send_event = FALSE; + + if (qt_net_supports(qt_net_wm_state_max_v) + && qt_net_supports(qt_net_wm_state_max_h)) { + if (max && !isMaximized()) { + setWState(WState_Maximized); + send_event = TRUE; + } else if (!max && isMaximized()) { + clearWState(WState_Maximized); + send_event = TRUE; + } + } + + if (qt_net_supports(qt_net_wm_state_fullscreen)) { + if (full && !isFullScreen()) { + setWState(WState_FullScreen); + send_event = TRUE; + } else if (!full && isFullScreen()) { + clearWState(WState_FullScreen); + send_event = TRUE; + } + } + + if (send_event) { + QEvent e(QEvent::WindowStateChange); + QApplication::sendSpontaneousEvent(this, &e); + } + } else if (event->xproperty.atom == qt_wm_state) { + // the widget frame strut should also be invalidated + topData()->fleft = topData()->fright = topData()->ftop = topData()->fbottom = 0; + fstrut_dirty = 1; + + if (event->xproperty.state == PropertyDelete) { + // the window manager has removed the WM State property, + // so it is now in the withdrawn state (ICCCM 4.1.3.1) and + // we are free to reuse this window + topData()->parentWinId = 0; + // map the window if we were waiting for a transition to + // withdrawn + if ( qt_deferred_map_contains( this ) ) { + qt_deferred_map_take( this ); + XMapWindow( appDpy, winId() ); + } + } else if (topData()->parentWinId != QPaintDevice::x11AppRootWindow(x11Screen())) { + // the window manager has changed the WM State property... + // we are wanting to see if we are withdrawn so that we + // can reuse this window... we only do this check *IF* we + // haven't been reparented to root - (the parentWinId != + // QPaintDevice::x11AppRootWindow(x11Screen())) check + // above + + e = XGetWindowProperty(appDpy, winId(), qt_wm_state, 0, 2, False, qt_wm_state, + &ret, &format, &nitems, &after, &data ); + + if (e == Success && ret == qt_wm_state && format == 32 && nitems > 0) { + long *state = (long *) data; + switch (state[0]) { + case WithdrawnState: + // if we are in the withdrawn state, we are free + // to reuse this window provided we remove the + // WM_STATE property (ICCCM 4.1.3.1) + XDeleteProperty(appDpy, winId(), qt_wm_state); + + // set the parent id to zero, so that show() will + // work again + topData()->parentWinId = 0; + // map the window if we were waiting for a + // transition to withdrawn + if ( qt_deferred_map_contains( this ) ) { + qt_deferred_map_take( this ); + XMapWindow( appDpy, winId() ); + } + break; + + case IconicState: + if (!isMinimized()) { + // window was minimized + setWState(WState_Minimized); + QEvent e(QEvent::WindowStateChange); + QApplication::sendSpontaneousEvent(this, &e); + } + break; + + default: + if (isMinimized()) { + // window was un-minimized + clearWState(WState_Minimized); + QEvent e(QEvent::WindowStateChange); + QApplication::sendSpontaneousEvent(this, &e); + } + break; + } + } + } + } + + if (data) + XFree(data); + + return TRUE; +} + +#ifndef XK_ISO_Left_Tab +#define XK_ISO_Left_Tab 0xFE20 +#endif + +// the next lines are taken from XFree > 4.0 (X11/XF86keysyms.h), defining some special +// multimedia keys. They are included here as not every system has them. +#define XF86XK_Standby 0x1008FF10 +#define XF86XK_AudioLowerVolume 0x1008FF11 +#define XF86XK_AudioMute 0x1008FF12 +#define XF86XK_AudioRaiseVolume 0x1008FF13 +#define XF86XK_AudioPlay 0x1008FF14 +#define XF86XK_AudioStop 0x1008FF15 +#define XF86XK_AudioPrev 0x1008FF16 +#define XF86XK_AudioNext 0x1008FF17 +#define XF86XK_HomePage 0x1008FF18 +#define XF86XK_Calculator 0x1008FF1D +#define XF86XK_Mail 0x1008FF19 +#define XF86XK_Start 0x1008FF1A +#define XF86XK_Search 0x1008FF1B +#define XF86XK_AudioRecord 0x1008FF1C +#define XF86XK_Back 0x1008FF26 +#define XF86XK_Forward 0x1008FF27 +#define XF86XK_Stop 0x1008FF28 +#define XF86XK_Refresh 0x1008FF29 +#define XF86XK_Favorites 0x1008FF30 +#define XF86XK_AudioPause 0x1008FF31 +#define XF86XK_AudioMedia 0x1008FF32 +#define XF86XK_MyComputer 0x1008FF33 +#define XF86XK_OpenURL 0x1008FF38 +#define XF86XK_Launch0 0x1008FF40 +#define XF86XK_Launch1 0x1008FF41 +#define XF86XK_Launch2 0x1008FF42 +#define XF86XK_Launch3 0x1008FF43 +#define XF86XK_Launch4 0x1008FF44 +#define XF86XK_Launch5 0x1008FF45 +#define XF86XK_Launch6 0x1008FF46 +#define XF86XK_Launch7 0x1008FF47 +#define XF86XK_Launch8 0x1008FF48 +#define XF86XK_Launch9 0x1008FF49 +#define XF86XK_LaunchA 0x1008FF4A +#define XF86XK_LaunchB 0x1008FF4B +#define XF86XK_LaunchC 0x1008FF4C +#define XF86XK_LaunchD 0x1008FF4D +#define XF86XK_LaunchE 0x1008FF4E +#define XF86XK_LaunchF 0x1008FF4F +// end of XF86keysyms.h + + + +static const KeySym KeyTbl[] = { // keyboard mapping table + XK_Escape, Qt::Key_Escape, // misc keys + XK_Tab, Qt::Key_Tab, + XK_ISO_Left_Tab, Qt::Key_Backtab, + XK_BackSpace, Qt::Key_Backspace, + XK_Return, Qt::Key_Return, + XK_Insert, Qt::Key_Insert, + XK_KP_Insert, Qt::Key_Insert, + XK_Delete, Qt::Key_Delete, + XK_KP_Delete, Qt::Key_Delete, + XK_Clear, Qt::Key_Delete, + XK_Pause, Qt::Key_Pause, + XK_Print, Qt::Key_Print, + XK_KP_Begin, Qt::Key_Clear, + 0x1005FF60, Qt::Key_SysReq, // hardcoded Sun SysReq + 0x1007ff00, Qt::Key_SysReq, // hardcoded X386 SysReq + XK_Home, Qt::Key_Home, // cursor movement + XK_End, Qt::Key_End, + XK_Left, Qt::Key_Left, + XK_Up, Qt::Key_Up, + XK_Right, Qt::Key_Right, + XK_Down, Qt::Key_Down, + XK_Prior, Qt::Key_Prior, + XK_Next, Qt::Key_Next, + XK_KP_Home, Qt::Key_Home, + XK_KP_End, Qt::Key_End, + XK_KP_Left, Qt::Key_Left, + XK_KP_Up, Qt::Key_Up, + XK_KP_Right, Qt::Key_Right, + XK_KP_Down, Qt::Key_Down, + XK_KP_Prior, Qt::Key_Prior, + XK_KP_Next, Qt::Key_Next, + XK_Shift_L, Qt::Key_Shift, // modifiers + XK_Shift_R, Qt::Key_Shift, + XK_Shift_Lock, Qt::Key_Shift, + XK_Control_L, Qt::Key_Control, + XK_Control_R, Qt::Key_Control, + XK_Meta_L, Qt::Key_Meta, + XK_Meta_R, Qt::Key_Meta, + XK_Alt_L, Qt::Key_Alt, + XK_Alt_R, Qt::Key_Alt, + XK_Caps_Lock, Qt::Key_CapsLock, + XK_Num_Lock, Qt::Key_NumLock, + XK_Scroll_Lock, Qt::Key_ScrollLock, + XK_KP_Space, Qt::Key_Space, // numeric keypad + XK_KP_Tab, Qt::Key_Tab, + XK_KP_Enter, Qt::Key_Enter, + XK_KP_Equal, Qt::Key_Equal, + XK_KP_Multiply, Qt::Key_Asterisk, + XK_KP_Add, Qt::Key_Plus, + XK_KP_Separator, Qt::Key_Comma, + XK_KP_Subtract, Qt::Key_Minus, + XK_KP_Decimal, Qt::Key_Period, + XK_KP_Divide, Qt::Key_Slash, + XK_Super_L, Qt::Key_Super_L, + XK_Super_R, Qt::Key_Super_R, + XK_Menu, Qt::Key_Menu, + XK_Hyper_L, Qt::Key_Hyper_L, + XK_Hyper_R, Qt::Key_Hyper_R, + XK_Help, Qt::Key_Help, + 0x1000FF74, Qt::Key_BackTab, // hardcoded HP backtab + 0x1005FF10, Qt::Key_F11, // hardcoded Sun F36 (labeled F11) + 0x1005FF11, Qt::Key_F12, // hardcoded Sun F37 (labeled F12) + + // International input method support keys + + // International & multi-key character composition + XK_Multi_key, Qt::Key_Multi_key, + XK_Codeinput, Qt::Key_Codeinput, + XK_SingleCandidate, Qt::Key_SingleCandidate, + XK_MultipleCandidate, Qt::Key_MultipleCandidate, + XK_PreviousCandidate, Qt::Key_PreviousCandidate, + + // Misc Functions + XK_Mode_switch, Qt::Key_Mode_switch, + //XK_script_switch, Qt::Key_script_switch, + XK_script_switch, Qt::Key_Mode_switch, + + // Japanese keyboard support + XK_Kanji, Qt::Key_Kanji, + XK_Muhenkan, Qt::Key_Muhenkan, + //XK_Henkan_Mode, Qt::Key_Henkan_Mode, + XK_Henkan_Mode, Qt::Key_Henkan, + XK_Henkan, Qt::Key_Henkan, + XK_Romaji, Qt::Key_Romaji, + XK_Hiragana, Qt::Key_Hiragana, + XK_Katakana, Qt::Key_Katakana, + XK_Hiragana_Katakana, Qt::Key_Hiragana_Katakana, + XK_Zenkaku, Qt::Key_Zenkaku, + XK_Hankaku, Qt::Key_Hankaku, + XK_Zenkaku_Hankaku, Qt::Key_Zenkaku_Hankaku, + XK_Touroku, Qt::Key_Touroku, + XK_Massyo, Qt::Key_Massyo, + XK_Kana_Lock, Qt::Key_Kana_Lock, + XK_Kana_Shift, Qt::Key_Kana_Shift, + XK_Eisu_Shift, Qt::Key_Eisu_Shift, + XK_Eisu_toggle, Qt::Key_Eisu_toggle, + //XK_Kanji_Bangou, Qt::Key_Kanji_Bangou, + //XK_Zen_Koho, Qt::Key_Zen_Koho, + //XK_Mae_Koho, Qt::Key_Mae_Koho, + XK_Kanji_Bangou, Qt::Key_Codeinput, + XK_Zen_Koho, Qt::Key_MultipleCandidate, + XK_Mae_Koho, Qt::Key_PreviousCandidate, + +#ifdef XK_KOREAN + // Korean keyboard support + XK_Hangul, Qt::Key_Hangul, + XK_Hangul_Start, Qt::Key_Hangul_Start, + XK_Hangul_End, Qt::Key_Hangul_End, + XK_Hangul_Hanja, Qt::Key_Hangul_Hanja, + XK_Hangul_Jamo, Qt::Key_Hangul_Jamo, + XK_Hangul_Romaja, Qt::Key_Hangul_Romaja, + //XK_Hangul_Codeinput, Qt::Key_Hangul_Codeinput, + XK_Hangul_Codeinput, Qt::Key_Codeinput, + XK_Hangul_Jeonja, Qt::Key_Hangul_Jeonja, + XK_Hangul_Banja, Qt::Key_Hangul_Banja, + XK_Hangul_PreHanja, Qt::Key_Hangul_PreHanja, + XK_Hangul_PostHanja, Qt::Key_Hangul_PostHanja, + //XK_Hangul_SingleCandidate, Qt::Key_Hangul_SingleCandidate, + //XK_Hangul_MultipleCandidate, Qt::Key_Hangul_MultipleCandidate, + //XK_Hangul_PreviousCandidate, Qt::Key_Hangul_PreviousCandidate, + XK_Hangul_SingleCandidate, Qt::Key_SingleCandidate, + XK_Hangul_MultipleCandidate, Qt::Key_MultipleCandidate, + XK_Hangul_PreviousCandidate, Qt::Key_PreviousCandidate, + XK_Hangul_Special, Qt::Key_Hangul_Special, + //XK_Hangul_switch, Qt::Key_Hangul_switch, + XK_Hangul_switch, Qt::Key_Mode_switch, +#endif // XK_KOREAN + + // dead keys + XK_dead_grave, Qt::Key_Dead_Grave, + XK_dead_acute, Qt::Key_Dead_Acute, + XK_dead_circumflex, Qt::Key_Dead_Circumflex, + XK_dead_tilde, Qt::Key_Dead_Tilde, + XK_dead_macron, Qt::Key_Dead_Macron, + XK_dead_breve, Qt::Key_Dead_Breve, + XK_dead_abovedot, Qt::Key_Dead_Abovedot, + XK_dead_diaeresis, Qt::Key_Dead_Diaeresis, + XK_dead_abovering, Qt::Key_Dead_Abovering, + XK_dead_doubleacute, Qt::Key_Dead_Doubleacute, + XK_dead_caron, Qt::Key_Dead_Caron, + XK_dead_cedilla, Qt::Key_Dead_Cedilla, + XK_dead_ogonek, Qt::Key_Dead_Ogonek, + XK_dead_iota, Qt::Key_Dead_Iota, + XK_dead_voiced_sound, Qt::Key_Dead_Voiced_Sound, + XK_dead_semivoiced_sound, Qt::Key_Dead_Semivoiced_Sound, + XK_dead_belowdot, Qt::Key_Dead_Belowdot, + XK_dead_hook, Qt::Key_Dead_Hook, + XK_dead_horn, Qt::Key_Dead_Horn, + + // Special multimedia keys + // currently only tested with MS internet keyboard + + // browsing keys + XF86XK_Back, Qt::Key_Back, + XF86XK_Forward, Qt::Key_Forward, + XF86XK_Stop, Qt::Key_Stop, + XF86XK_Refresh, Qt::Key_Refresh, + XF86XK_Favorites, Qt::Key_Favorites, + XF86XK_AudioMedia, Qt::Key_LaunchMedia, + XF86XK_OpenURL, Qt::Key_OpenUrl, + XF86XK_HomePage, Qt::Key_HomePage, + XF86XK_Search, Qt::Key_Search, + + // media keys + XF86XK_AudioLowerVolume, Qt::Key_VolumeDown, + XF86XK_AudioMute, Qt::Key_VolumeMute, + XF86XK_AudioRaiseVolume, Qt::Key_VolumeUp, + XF86XK_AudioPlay, Qt::Key_MediaPlay, + XF86XK_AudioStop, Qt::Key_MediaStop, + XF86XK_AudioPrev, Qt::Key_MediaPrev, + XF86XK_AudioNext, Qt::Key_MediaNext, + XF86XK_AudioRecord, Qt::Key_MediaRecord, + + // launch keys + XF86XK_Mail, Qt::Key_LaunchMail, + XF86XK_MyComputer, Qt::Key_Launch0, + XF86XK_Calculator, Qt::Key_Launch1, + XF86XK_Standby, Qt::Key_Standby, + + XF86XK_Launch0, Qt::Key_Launch2, + XF86XK_Launch1, Qt::Key_Launch3, + XF86XK_Launch2, Qt::Key_Launch4, + XF86XK_Launch3, Qt::Key_Launch5, + XF86XK_Launch4, Qt::Key_Launch6, + XF86XK_Launch5, Qt::Key_Launch7, + XF86XK_Launch6, Qt::Key_Launch8, + XF86XK_Launch7, Qt::Key_Launch9, + XF86XK_Launch8, Qt::Key_LaunchA, + XF86XK_Launch9, Qt::Key_LaunchB, + XF86XK_LaunchA, Qt::Key_LaunchC, + XF86XK_LaunchB, Qt::Key_LaunchD, + XF86XK_LaunchC, Qt::Key_LaunchE, + XF86XK_LaunchD, Qt::Key_LaunchF, + + 0, 0 +}; + + +static QIntDict<void> *keyDict = 0; +static QIntDict<void> *textDict = 0; + +static void deleteKeyDicts() +{ + if ( keyDict ) + delete keyDict; + keyDict = 0; + if ( textDict ) + delete textDict; + textDict = 0; +} + +#if !defined(QT_NO_XIM) +static const unsigned short katakanaKeysymsToUnicode[] = { + 0x0000, 0x3002, 0x300C, 0x300D, 0x3001, 0x30FB, 0x30F2, 0x30A1, + 0x30A3, 0x30A5, 0x30A7, 0x30A9, 0x30E3, 0x30E5, 0x30E7, 0x30C3, + 0x30FC, 0x30A2, 0x30A4, 0x30A6, 0x30A8, 0x30AA, 0x30AB, 0x30AD, + 0x30AF, 0x30B1, 0x30B3, 0x30B5, 0x30B7, 0x30B9, 0x30BB, 0x30BD, + 0x30BF, 0x30C1, 0x30C4, 0x30C6, 0x30C8, 0x30CA, 0x30CB, 0x30CC, + 0x30CD, 0x30CE, 0x30CF, 0x30D2, 0x30D5, 0x30D8, 0x30DB, 0x30DE, + 0x30DF, 0x30E0, 0x30E1, 0x30E2, 0x30E4, 0x30E6, 0x30E8, 0x30E9, + 0x30EA, 0x30EB, 0x30EC, 0x30ED, 0x30EF, 0x30F3, 0x309B, 0x309C +}; + +static const unsigned short cyrillicKeysymsToUnicode[] = { + 0x0000, 0x0452, 0x0453, 0x0451, 0x0454, 0x0455, 0x0456, 0x0457, + 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x0000, 0x045e, 0x045f, + 0x2116, 0x0402, 0x0403, 0x0401, 0x0404, 0x0405, 0x0406, 0x0407, + 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x0000, 0x040e, 0x040f, + 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, + 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, + 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, + 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, + 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, + 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, + 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, + 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a +}; + +static const unsigned short greekKeysymsToUnicode[] = { + 0x0000, 0x0386, 0x0388, 0x0389, 0x038a, 0x03aa, 0x0000, 0x038c, + 0x038e, 0x03ab, 0x0000, 0x038f, 0x0000, 0x0000, 0x0385, 0x2015, + 0x0000, 0x03ac, 0x03ad, 0x03ae, 0x03af, 0x03ca, 0x0390, 0x03cc, + 0x03cd, 0x03cb, 0x03b0, 0x03ce, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, + 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, + 0x03a0, 0x03a1, 0x03a3, 0x0000, 0x03a4, 0x03a5, 0x03a6, 0x03a7, + 0x03a8, 0x03a9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, + 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, + 0x03c0, 0x03c1, 0x03c3, 0x03c2, 0x03c4, 0x03c5, 0x03c6, 0x03c7, + 0x03c8, 0x03c9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +static const unsigned short technicalKeysymsToUnicode[] = { + 0x0000, 0x23B7, 0x250C, 0x2500, 0x2320, 0x2321, 0x2502, 0x23A1, + 0x23A3, 0x23A4, 0x23A6, 0x239B, 0x239D, 0x239E, 0x23A0, 0x23A8, + 0x23AC, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x2264, 0x2260, 0x2265, 0x222B, + 0x2234, 0x221D, 0x221E, 0x0000, 0x0000, 0x2207, 0x0000, 0x0000, + 0x223C, 0x2243, 0x0000, 0x0000, 0x0000, 0x21D4, 0x21D2, 0x2261, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x221A, 0x0000, + 0x0000, 0x0000, 0x2282, 0x2283, 0x2229, 0x222A, 0x2227, 0x2228, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2202, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0192, 0x0000, + 0x0000, 0x0000, 0x0000, 0x2190, 0x2191, 0x2192, 0x2193, 0x0000 +}; + +static const unsigned short specialKeysymsToUnicode[] = { + 0x25C6, 0x2592, 0x2409, 0x240C, 0x240D, 0x240A, 0x0000, 0x0000, + 0x2424, 0x240B, 0x2518, 0x2510, 0x250C, 0x2514, 0x253C, 0x23BA, + 0x23BB, 0x2500, 0x23BC, 0x23BD, 0x251C, 0x2524, 0x2534, 0x252C, + 0x2502, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +static const unsigned short publishingKeysymsToUnicode[] = { + 0x0000, 0x2003, 0x2002, 0x2004, 0x2005, 0x2007, 0x2008, 0x2009, + 0x200a, 0x2014, 0x2013, 0x0000, 0x0000, 0x0000, 0x2026, 0x2025, + 0x2153, 0x2154, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215a, + 0x2105, 0x0000, 0x0000, 0x2012, 0x2329, 0x0000, 0x232a, 0x0000, + 0x0000, 0x0000, 0x0000, 0x215b, 0x215c, 0x215d, 0x215e, 0x0000, + 0x0000, 0x2122, 0x2613, 0x0000, 0x25c1, 0x25b7, 0x25cb, 0x25af, + 0x2018, 0x2019, 0x201c, 0x201d, 0x211e, 0x0000, 0x2032, 0x2033, + 0x0000, 0x271d, 0x0000, 0x25ac, 0x25c0, 0x25b6, 0x25cf, 0x25ae, + 0x25e6, 0x25ab, 0x25ad, 0x25b3, 0x25bd, 0x2606, 0x2022, 0x25aa, + 0x25b2, 0x25bc, 0x261c, 0x261e, 0x2663, 0x2666, 0x2665, 0x0000, + 0x2720, 0x2020, 0x2021, 0x2713, 0x2717, 0x266f, 0x266d, 0x2642, + 0x2640, 0x260e, 0x2315, 0x2117, 0x2038, 0x201a, 0x201e, 0x0000 +}; + +static const unsigned short aplKeysymsToUnicode[] = { + 0x0000, 0x0000, 0x0000, 0x003c, 0x0000, 0x0000, 0x003e, 0x0000, + 0x2228, 0x2227, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x00af, 0x0000, 0x22a5, 0x2229, 0x230a, 0x0000, 0x005f, 0x0000, + 0x0000, 0x0000, 0x2218, 0x0000, 0x2395, 0x0000, 0x22a4, 0x25cb, + 0x0000, 0x0000, 0x0000, 0x2308, 0x0000, 0x0000, 0x222a, 0x0000, + 0x2283, 0x0000, 0x2282, 0x0000, 0x22a2, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x22a3, 0x0000, 0x0000, 0x0000 +}; + +static const unsigned short koreanKeysymsToUnicode[] = { + 0x0000, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, + 0x3138, 0x3139, 0x313a, 0x313b, 0x313c, 0x313d, 0x313e, 0x313f, + 0x3140, 0x3141, 0x3142, 0x3143, 0x3144, 0x3145, 0x3146, 0x3147, + 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e, 0x314f, + 0x3150, 0x3151, 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, + 0x3158, 0x3159, 0x315a, 0x315b, 0x315c, 0x315d, 0x315e, 0x315f, + 0x3160, 0x3161, 0x3162, 0x3163, 0x11a8, 0x11a9, 0x11aa, 0x11ab, + 0x11ac, 0x11ad, 0x11ae, 0x11af, 0x11b0, 0x11b1, 0x11b2, 0x11b3, + 0x11b4, 0x11b5, 0x11b6, 0x11b7, 0x11b8, 0x11b9, 0x11ba, 0x11bb, + 0x11bc, 0x11bd, 0x11be, 0x11bf, 0x11c0, 0x11c1, 0x11c2, 0x316d, + 0x3171, 0x3178, 0x317f, 0x3181, 0x3184, 0x3186, 0x318d, 0x318e, + 0x11eb, 0x11f0, 0x11f9, 0x0000, 0x0000, 0x0000, 0x0000, 0x20a9 +}; + + +static QChar keysymToUnicode(unsigned char byte3, unsigned char byte4) +{ + if ( byte3 == 0x04 ) { + // katakana + if ( byte4 > 0xa0 && byte4 < 0xe0 ) + return QChar( katakanaKeysymsToUnicode[byte4 - 0xa0] ); + else if ( byte4 == 0x7e ) + return QChar( 0x203e ); // Overline + } else if ( byte3 == 0x06 ) { + // russian, use lookup table + if ( byte4 > 0xa0 ) + return QChar( cyrillicKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x07 ) { + // greek + if ( byte4 > 0xa0 ) + return QChar( greekKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x08 ) { + // technical + if ( byte4 > 0xa0 ) + return QChar( technicalKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x09 ) { + // special + if ( byte4 >= 0xe0 ) + return QChar( specialKeysymsToUnicode[byte4 - 0xe0] ); + } else if ( byte3 == 0x0a ) { + // publishing + if ( byte4 > 0xa0 ) + return QChar( publishingKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x0b ) { + // APL + if ( byte4 > 0xa0 ) + return QChar( aplKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x0e ) { + // Korean + if ( byte4 > 0xa0 ) + return QChar( koreanKeysymsToUnicode[byte4 - 0xa0] ); + } + return QChar(0x0); +} +#endif + + +bool QETWidget::translateKeyEventInternal( const XEvent *event, int& count, + QString& text, + int& state, + char& ascii, int& code, QEvent::Type &type, bool willRepeat, bool statefulTranslation ) +{ + QTextCodec *mapper = qt_input_mapper; + // some XmbLookupString implementations don't return buffer overflow correctly, + // so we increase the input buffer to allow for long strings... + // 256 chars * 2 bytes + 1 null-term == 513 bytes + QCString chars(513); + QChar converted; + KeySym key = 0; + + if ( !keyDict ) { + keyDict = new QIntDict<void>( 13 ); + keyDict->setAutoDelete( FALSE ); + textDict = new QIntDict<void>( 13 ); + textDict->setAutoDelete( FALSE ); + qAddPostRoutine( deleteKeyDicts ); + } + + QWidget* tlw = topLevelWidget(); + + XKeyEvent xkeyevent = event->xkey; + + // save the modifier state, we will use the keystate uint later by passing + // it to qt_x11_translateButtonState + uint keystate = event->xkey.state; + // remove the modifiers where mode_switch exists... HPUX machines seem + // to have alt *AND* mode_switch both in Mod1Mask, which causes + // XLookupString to return things like 'å' (aring) for ALT-A. This + // completely breaks modifiers. If we remove the modifier for Mode_switch, + // then things work correctly... + xkeyevent.state &= ~qt_mode_switch_remove_mask; + + type = (event->type == XKeyPress) + ? QEvent::KeyPress : QEvent::KeyRelease; +#if defined(QT_NO_XIM) + + count = XLookupString( &xkeyevent, chars.data(), chars.size(), &key, 0 ); + + if ( count == 1 ) + ascii = chars[0]; + +#else + // Implementation for X11R5 and newer, using XIM + + int keycode = event->xkey.keycode; + Status status; + + if ( type == QEvent::KeyPress ) { + bool mb=FALSE; + // commit string handling is done by + // QXIMInputContext::x11FilterEvent() and are passed to + // widgets via QIMEvent regardless of XIM style, so the + // following code is commented out. +#if 0 + if ( qt_xim ) { + QTLWExtra* xd = tlw->topData(); + QInputContext *qic = (QInputContext *) xd->xic; + if ( qic ) { + mb=TRUE; + count = qic->lookupString(&xkeyevent, chars, &key, &status); + } + } +#endif + if ( !mb ) { + count = XLookupString( &xkeyevent, + chars.data(), chars.size(), &key, 0 ); + } + if ( count && !keycode ) { + keycode = qt_ximComposingKeycode; + qt_ximComposingKeycode = 0; + } + if ( key ) + keyDict->replace( keycode, (void*)key ); + // all keysyms smaller than that are actally keys that can be mapped + // to unicode chars + if ( count == 0 && key < 0xff00 ) { + unsigned char byte3 = (unsigned char )(key >> 8); + int mib = -1; + switch( byte3 ) { + case 0: // Latin 1 + case 1: // Latin 2 + case 2: //latin 3 + case 3: // latin4 + mib = byte3 + 4; break; + case 5: // arabic + mib = 82; break; + case 12: // Hebrew + mib = 85; break; + case 13: // Thai + mib = 2259; break; + case 4: // kana + case 6: // cyrillic + case 7: // greek + case 8: // technical, no mapping here at the moment + case 9: // Special + case 10: // Publishing + case 11: // APL + case 14: // Korean, no mapping + mib = -1; // manual conversion + mapper = 0; + converted = keysymToUnicode( byte3, key & 0xff ); + case 0x20: + // currency symbols + if ( key >= 0x20a0 && key <= 0x20ac ) { + mib = -1; // manual conversion + mapper = 0; + converted = (uint)key; + } + break; + default: + break; + } + if ( mib != -1 ) { + mapper = QTextCodec::codecForMib( mib ); + chars[0] = (unsigned char) (key & 0xff); // get only the fourth bit for conversion later + count++; + } + } else if ( key >= 0x1000000 && key <= 0x100ffff ) { + converted = (ushort) (key - 0x1000000); + mapper = 0; + } + if ( count < (int)chars.size()-1 ) + chars[count] = '\0'; + if ( count == 1 ) { + ascii = chars[0]; + // +256 so we can store all eight-bit codes, including ascii 0, + // and independent of whether char is signed or not. + textDict->replace( keycode, (void*)(long)(256+ascii) ); + } + tlw = 0; + } else { + key = (int)(long)keyDict->find( keycode ); + if ( key ) + if( !willRepeat && statefulTranslation ) // Take out key of dictionary only if this call. + keyDict->take( keycode ); + long s = (long)textDict->find( keycode ); + if ( s ) { + if( statefulTranslation ) + textDict->take( keycode ); + ascii = (char)(s-256); + } + } +#endif // !QT_NO_XIM + + state = qt_x11_translateButtonState( keystate ); + + static int directionKeyEvent = 0; + static unsigned int lastWinId = 0; + if ( qt_use_rtl_extensions && type == QEvent::KeyRelease && statefulTranslation ) { + if (directionKeyEvent == Key_Direction_R || directionKeyEvent == Key_Direction_L ) { + type = QEvent::KeyPress; + code = directionKeyEvent; + chars[0] = 0; + directionKeyEvent = 0; + lastWinId = 0; + return TRUE; + } else { + directionKeyEvent = 0; + lastWinId = 0; + } + } + + // Watch for keypresses and if its a key belonging to the Ctrl-Shift + // direction-changing accel, remember it. + // We keep track of those keys instead of using the event's state + // (to figure out whether the Ctrl modifier is held while Shift is pressed, + // or Shift is held while Ctrl is pressed) since the 'state' doesn't tell + // us whether the modifier held is Left or Right. + if ( qt_use_rtl_extensions && type == QEvent::KeyPress && statefulTranslation ) + if (key == XK_Control_L || key == XK_Control_R || key == XK_Shift_L || key == XK_Shift_R) { + if (!directionKeyEvent) { + directionKeyEvent = key; + // This code exists in order to check that + // the event is occurred in the same widget. + lastWinId = winId(); + } + } else { + // this can no longer be a direction-changing accel. + // if any other key was pressed. + directionKeyEvent = Key_Space; + } + + // Commentary in X11/keysymdef says that X codes match ASCII, so it + // is safe to use the locale functions to process X codes in ISO8859-1. + // + // This is mainly for compatibility - applications should not use the + // Qt keycodes between 128 and 255, but should rather use the + // QKeyEvent::text(). + // + if ( key < 128 || (key < 256 && (!qt_input_mapper || qt_input_mapper->mibEnum()==4)) ) { + code = isprint((int)key) ? toupper((int)key) : 0; // upper-case key, if known + } else if ( key >= XK_F1 && key <= XK_F35 ) { + code = Key_F1 + ((int)key - XK_F1); // function keys + } else if ( key >= XK_KP_0 && key <= XK_KP_9) { + code = Key_0 + ((int)key - XK_KP_0); // numeric keypad keys + state |= Keypad; + } else { + int i = 0; // any other keys + while ( KeyTbl[i] ) { + if ( key == KeyTbl[i] ) { + code = (int)KeyTbl[i+1]; + break; + } + i += 2; + } + switch ( key ) { + case XK_KP_Insert: + case XK_KP_Delete: + case XK_KP_Home: + case XK_KP_End: + case XK_KP_Left: + case XK_KP_Up: + case XK_KP_Right: + case XK_KP_Down: + case XK_KP_Prior: + case XK_KP_Next: + case XK_KP_Space: + case XK_KP_Tab: + case XK_KP_Enter: + case XK_KP_Equal: + case XK_KP_Multiply: + case XK_KP_Add: + case XK_KP_Separator: + case XK_KP_Subtract: + case XK_KP_Decimal: + case XK_KP_Divide: + state |= Keypad; + break; + default: + break; + } + + if ( code == Key_Tab && + (state & ShiftButton) == ShiftButton ) { + // map shift+tab to shift+backtab, QAccel knows about it + // and will handle it. + code = Key_Backtab; + chars[0] = 0; + } + + if ( qt_use_rtl_extensions && type == QEvent::KeyPress && statefulTranslation ) { + if ( directionKeyEvent && lastWinId == winId() ) { + if ( key == XK_Shift_L && directionKeyEvent == XK_Control_L || + key == XK_Control_L && directionKeyEvent == XK_Shift_L ) { + directionKeyEvent = Key_Direction_L; + } else if ( key == XK_Shift_R && directionKeyEvent == XK_Control_R || + key == XK_Control_R && directionKeyEvent == XK_Shift_R ) { + directionKeyEvent = Key_Direction_R; + } + } + else if ( directionKeyEvent == Key_Direction_L || directionKeyEvent == Key_Direction_R ) { + directionKeyEvent = Key_Space; // invalid + } + } + } + +#if 0 +#ifndef Q_EE + static int c = 0; + extern void qt_dialog_default_key(); +#define Q_EE(x) c = (c == x || (!c && x == 0x1000) )? x+1 : 0 + if ( tlw && state == '0' ) { + switch ( code ) { + case 0x4f: Q_EE(Key_Backtab); break; + case 0x52: Q_EE(Key_Tab); break; + case 0x54: Q_EE(Key_Escape); break; + case 0x4c: + if (c == Key_Return ) + qt_dialog_default_key(); + else + Q_EE(Key_Backspace); + break; + } + } +#undef Q_EE +#endif +#endif + + // convert chars (8bit) to text (unicode). + if ( mapper ) + text = mapper->toUnicode(chars,count); + else if ( !mapper && converted.unicode() != 0x0 ) + text = converted; + else + text = chars; + return TRUE; +} + + +struct qt_auto_repeat_data +{ + // match the window and keycode with timestamp delta of 10ms + Window window; + KeyCode keycode; + Time timestamp; + + // queue scanner state + bool release; + bool error; +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static Bool qt_keypress_scanner(Display *, XEvent *event, XPointer arg) +{ + if (event->type != XKeyPress && event->type != XKeyRelease) + return FALSE; + + qt_auto_repeat_data *d = (qt_auto_repeat_data *) arg; + if (d->error || + event->xkey.window != d->window || + event->xkey.keycode != d->keycode) { + d->error = TRUE; + return FALSE; + } + + if (event->type == XKeyPress) { + d->error = (! d->release || event->xkey.time - d->timestamp > 10); + return (! d->error); + } + + // must be XKeyRelease event + if (d->release) { + // found a second release + d->error = TRUE; + return FALSE; + } + + // found a single release + d->release = TRUE; + d->timestamp = event->xkey.time; + + return FALSE; +} + +static Bool qt_keyrelease_scanner(Display *, XEvent *event, XPointer arg) +{ + const qt_auto_repeat_data *d = (const qt_auto_repeat_data *) arg; + return (event->type == XKeyRelease && + event->xkey.window == d->window && + event->xkey.keycode == d->keycode); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +bool QETWidget::translateKeyEvent( const XEvent *event, bool grab ) +{ + int code = -1; + int count = 0; + int state; + char ascii = 0; + + if ( sm_blockUserInput ) // block user interaction during session management + return TRUE; + + Display *dpy = x11Display(); + + if ( !isEnabled() ) + return TRUE; + + QEvent::Type type; + bool autor = FALSE; + QString text; + + translateKeyEventInternal( event, count, text, state, ascii, code, type, + qt_mode_switch_remove_mask != 0 ); + + static uint curr_autorep = 0; + // was this the last auto-repeater? + qt_auto_repeat_data auto_repeat_data; + auto_repeat_data.window = event->xkey.window; + auto_repeat_data.keycode = event->xkey.keycode; + auto_repeat_data.timestamp = event->xkey.time; + + if ( event->type == XKeyPress ) { + if ( curr_autorep == event->xkey.keycode ) { + autor = TRUE; + curr_autorep = 0; + } + } else { + // look ahead for auto-repeat + XEvent nextpress; + + auto_repeat_data.release = TRUE; + auto_repeat_data.error = FALSE; + if (XCheckIfEvent(dpy, &nextpress, &qt_keypress_scanner, + (XPointer) &auto_repeat_data)) { + autor = TRUE; + + // Put it back... we COULD send the event now and not need + // the static curr_autorep variable. + XPutBackEvent(dpy,&nextpress); + } + curr_autorep = autor ? event->xkey.keycode : 0; + } + + // process accelerators before doing key compression + if ( type == QEvent::KeyPress && !grab ) { + // send accel events if the keyboard is not grabbed + QKeyEvent a( type, code, ascii, state, text, autor, + QMAX( QMAX(count,1), int(text.length())) ); + if ( qt_tryAccelEvent( this, &a ) ) + return TRUE; + } + + long save = 0; + if ( qt_mode_switch_remove_mask != 0 ) { + save = qt_mode_switch_remove_mask; + qt_mode_switch_remove_mask = 0; + + // translate the key event again, but this time apply any Mode_switch + // modifiers + translateKeyEventInternal( event, count, text, state, ascii, code, type ); + } + +#ifndef QT_NO_IM + QInputContext *qic = getInputContext(); +#endif + + // compress keys + if ( !text.isEmpty() && testWState(WState_CompressKeys) && +#ifndef QT_NO_IM + // Ordinary input methods require discrete key events to work + // properly, so key compression has to be disabled when input + // context exists. + // + // And further consideration, some complex input method + // require all key press/release events discretely even if + // the input method awares of key compression and compressed + // keys are ordinary alphabets. For example, the uim project + // is planning to implement "combinational shift" feature for + // a Japanese input method, uim-skk. It will work as follows. + // + // 1. press "r" + // 2. press "u" + // 3. release both "r" and "u" in arbitrary order + // 4. above key sequence generates "Ru" + // + // Of course further consideration about other participants + // such as key repeat mechanism is required to implement such + // feature. + ! qic && +#endif // QT_NO_IM + // do not compress keys if the key event we just got above matches + // one of the key ranges used to compute stopCompression + ! ( ( code >= Key_Escape && code <= Key_SysReq ) || + ( code >= Key_Home && code <= Key_Next ) || + ( code >= Key_Super_L && code <= Key_Direction_R ) || + ( ( code == 0 ) && ( ascii == '\n' ) ) ) ) { + // the widget wants key compression so it gets it + int codeIntern = -1; + int countIntern = 0; + int stateIntern; + char asciiIntern = 0; + XEvent evRelease; + XEvent evPress; + + // sync the event queue, this makes key compress work better + XSync( dpy, FALSE ); + + for (;;) { + QString textIntern; + if ( !XCheckTypedWindowEvent(dpy,event->xkey.window, + XKeyRelease,&evRelease) ) + break; + if ( !XCheckTypedWindowEvent(dpy,event->xkey.window, + XKeyPress,&evPress) ) { + XPutBackEvent(dpy, &evRelease); + break; + } + QEvent::Type t; + translateKeyEventInternal( &evPress, countIntern, textIntern, + stateIntern, asciiIntern, codeIntern, t ); + // use stopCompression to stop key compression for the following + // key event ranges: + bool stopCompression = + // 1) misc keys + ( codeIntern >= Key_Escape && codeIntern <= Key_SysReq ) || + // 2) cursor movement + ( codeIntern >= Key_Home && codeIntern <= Key_Next ) || + // 3) extra keys + ( codeIntern >= Key_Super_L && codeIntern <= Key_Direction_R ) || + // 4) something that a) doesn't translate to text or b) translates + // to newline text + ((codeIntern == 0) && (asciiIntern == '\n')); + if (stateIntern == state && !textIntern.isEmpty() && !stopCompression) { + text += textIntern; + count += countIntern; + } else { + XPutBackEvent(dpy, &evPress); + XPutBackEvent(dpy, &evRelease); + break; + } + } + } + + if ( save != 0 ) + qt_mode_switch_remove_mask = save; + + // autorepeat compression makes sense for all widgets (Windows + // does it automatically .... ) + if ( event->type == XKeyPress && text.length() <= 1 +#ifndef QT_NO_IM + // input methods need discrete key events + && ! qic +#endif// QT_NO_IM + ) { + XEvent dummy; + + for (;;) { + auto_repeat_data.release = FALSE; + auto_repeat_data.error = FALSE; + if (! XCheckIfEvent(dpy, &dummy, &qt_keypress_scanner, + (XPointer) &auto_repeat_data)) + break; + if (! XCheckIfEvent(dpy, &dummy, &qt_keyrelease_scanner, + (XPointer) &auto_repeat_data)) + break; + + count++; + if (!text.isEmpty()) + text += text[0]; + } + } + + if (code == 0 && ascii == '\n') { + code = Key_Return; + ascii = '\r'; + text = "\r"; + } + + // try the menukey first + if ( type == QEvent::KeyPress && code == Qt::Key_Menu ) { + QContextMenuEvent e( QContextMenuEvent::Keyboard, QPoint( 5, 5 ), mapToGlobal( QPoint( 5, 5 ) ), 0 ); + QApplication::sendSpontaneousEvent( this, &e ); + if( e.isAccepted() ) + return TRUE; + } + + QKeyEvent e( type, code, ascii, state, text, autor, + QMAX(QMAX(count,1), int(text.length())) ); + return QApplication::sendSpontaneousEvent( this, &e ); +} + + +// +// Paint event translation +// +// When receiving many expose events, we compress them (union of all expose +// rectangles) into one event which is sent to the widget. + +struct PaintEventInfo { + Window window; +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static Bool isPaintOrScrollDoneEvent( Display *, XEvent *ev, XPointer a ) +{ + PaintEventInfo *info = (PaintEventInfo *)a; + if ( ev->type == Expose || ev->type == GraphicsExpose + || ev->type == ClientMessage + && ev->xclient.message_type == qt_qt_scrolldone ) + { + if ( ev->xexpose.window == info->window ) + return True; + } + return False; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +// declared above: static QPtrList<QScrollInProgress> *sip_list = 0; + +void qt_insert_sip( QWidget* scrolled_widget, int dx, int dy ) +{ + if ( !sip_list ) { + sip_list = new QPtrList<QScrollInProgress>; + sip_list->setAutoDelete( TRUE ); + } + + QScrollInProgress* sip = new QScrollInProgress( scrolled_widget, dx, dy ); + sip_list->append( sip ); + + XClientMessageEvent client_message; + client_message.type = ClientMessage; + client_message.window = scrolled_widget->winId(); + client_message.format = 32; + client_message.message_type = qt_qt_scrolldone; + client_message.data.l[0] = sip->id; + + XSendEvent( appDpy, scrolled_widget->winId(), False, NoEventMask, + (XEvent*)&client_message ); +} + +int qt_sip_count( QWidget* scrolled_widget ) +{ + if ( !sip_list ) + return 0; + + int sips=0; + + for (QScrollInProgress* sip = sip_list->first(); + sip; sip=sip_list->next()) + { + if ( sip->scrolled_widget == scrolled_widget ) + sips++; + } + + return sips; +} + +static +bool translateBySips( QWidget* that, QRect& paintRect ) +{ + if ( sip_list ) { + int dx=0, dy=0; + int sips=0; + for (QScrollInProgress* sip = sip_list->first(); + sip; sip=sip_list->next()) + { + if ( sip->scrolled_widget == that ) { + if ( sips ) { + dx += sip->dx; + dy += sip->dy; + } + sips++; + } + } + if ( sips > 1 ) { + paintRect.moveBy( dx, dy ); + return TRUE; + } + } + return FALSE; +} + +bool QETWidget::translatePaintEvent( const XEvent *event ) +{ + setWState( WState_Exposed ); + QRect paintRect( event->xexpose.x, event->xexpose.y, + event->xexpose.width, event->xexpose.height ); + bool merging_okay = !testWFlags(WPaintClever); + XEvent xevent; + PaintEventInfo info; + info.window = winId(); + bool should_clip = translateBySips( this, paintRect ); + + QRegion paintRegion( paintRect ); + + if ( merging_okay ) { + // WARNING: this is O(number_of_events * number_of_matching_events) + while ( XCheckIfEvent(x11Display(),&xevent,isPaintOrScrollDoneEvent, + (XPointer)&info) && + !qt_x11EventFilter(&xevent) && + !x11Event( &xevent ) ) // send event through filter + { + if ( xevent.type == Expose || xevent.type == GraphicsExpose ) { + QRect exposure(xevent.xexpose.x, + xevent.xexpose.y, + xevent.xexpose.width, + xevent.xexpose.height); + if ( translateBySips( this, exposure ) ) + should_clip = TRUE; + paintRegion = paintRegion.unite( exposure ); + } else { + translateScrollDoneEvent( &xevent ); + } + } + } + + if ( should_clip ) { + paintRegion = paintRegion.intersect( rect() ); + if ( paintRegion.isEmpty() ) + return TRUE; + } + + QPaintEvent e( paintRegion ); + setWState( WState_InPaintEvent ); + if ( !isTopLevel() && backgroundOrigin() != WidgetOrigin ) + erase( paintRegion ); + qt_set_paintevent_clipping( this, paintRegion ); + QApplication::sendSpontaneousEvent( this, &e ); + qt_clear_paintevent_clipping(); + clearWState( WState_InPaintEvent ); + return TRUE; +} + +// +// Scroll-done event translation. +// + +bool QETWidget::translateScrollDoneEvent( const XEvent *event ) +{ + if ( !sip_list ) return FALSE; + + long id = event->xclient.data.l[0]; + + // Remove any scroll-in-progress record for the given id. + for (QScrollInProgress* sip = sip_list->first(); sip; sip=sip_list->next()) { + if ( sip->id == id ) { + sip_list->remove( sip_list->current() ); + return TRUE; + } + } + + return FALSE; +} + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif +#ifndef QT_NO_XSYNC +static Bool qt_net_wm_sync_request_scanner(Display*, XEvent* event, XPointer arg) +{ + return (event->type == ClientMessage && event->xclient.window == *(Window*)arg + && event->xclient.message_type == qt_wm_protocols + && event->xclient.data.l[ 0 ] == qt_net_wm_sync_request ); +} +#endif + +#if defined(Q_C_CALLBACKS) +} +#endif + +// +// ConfigureNotify (window move and resize) event translation + +bool QETWidget::translateConfigEvent( const XEvent *event ) +{ + // config pending is only set on resize, see qwidget_x11.cpp, internalSetGeometry() + bool was_resize = testWState( WState_ConfigPending ); + + clearWState(WState_ConfigPending); + + if ( isTopLevel() ) { + QPoint newCPos( geometry().topLeft() ); + QSize newSize( event->xconfigure.width, event->xconfigure.height ); + + bool trust = (topData()->parentWinId == None || + topData()->parentWinId == QPaintDevice::x11AppRootWindow()); + + if (event->xconfigure.send_event || trust ) { + // if a ConfigureNotify comes from a real sendevent request, we can + // trust its values. + newCPos.rx() = event->xconfigure.x + event->xconfigure.border_width; + newCPos.ry() = event->xconfigure.y + event->xconfigure.border_width; + } + + if ( isVisible() ) + QApplication::syncX(); + + if (! extra || extra->compress_events) { + // ConfigureNotify compression for faster opaque resizing + XEvent otherEvent; + int compressed_configs = 0; + while ( XCheckTypedWindowEvent( x11Display(), winId(), ConfigureNotify, + &otherEvent ) ) { + if ( qt_x11EventFilter( &otherEvent ) ) + continue; + + if (x11Event( &otherEvent ) ) + continue; + + if ( otherEvent.xconfigure.event != otherEvent.xconfigure.window ) + continue; + + newSize.setWidth( otherEvent.xconfigure.width ); + newSize.setHeight( otherEvent.xconfigure.height ); + + if ( otherEvent.xconfigure.send_event || trust ) { + newCPos.rx() = otherEvent.xconfigure.x + + otherEvent.xconfigure.border_width; + newCPos.ry() = otherEvent.xconfigure.y + + otherEvent.xconfigure.border_width; + } + ++compressed_configs; + } +#ifndef QT_NO_XSYNC + // _NET_WM_SYNC_REQUEST compression + Window wid = winId(); + while ( compressed_configs && + XCheckIfEvent( x11Display(), &otherEvent, + qt_net_wm_sync_request_scanner, (XPointer)&wid ) ) { + handleSyncRequest( (void*)&otherEvent ); + --compressed_configs; + } +#endif + } + + QRect cr ( geometry() ); + if ( newSize != cr.size() ) { // size changed + was_resize = TRUE; + QSize oldSize = size(); + cr.setSize( newSize ); + crect = cr; + + if ( isVisible() ) { + QResizeEvent e( newSize, oldSize ); + QApplication::sendSpontaneousEvent( this, &e ); + } else { + QResizeEvent * e = new QResizeEvent( newSize, oldSize ); + QApplication::postEvent( this, e ); + } + } + + if ( newCPos != cr.topLeft() ) { // compare with cpos (exluding frame) + QPoint oldPos = geometry().topLeft(); + cr.moveTopLeft( newCPos ); + crect = cr; + if ( isVisible() ) { + QMoveEvent e( newCPos, oldPos ); // pos (including frame), not cpos + QApplication::sendSpontaneousEvent( this, &e ); + } else { + QMoveEvent * e = new QMoveEvent( newCPos, oldPos ); + QApplication::postEvent( this, e ); + } + } + } else { + XEvent xevent; + while ( XCheckTypedWindowEvent(x11Display(),winId(), ConfigureNotify,&xevent) && + !qt_x11EventFilter(&xevent) && + !x11Event( &xevent ) ) // send event through filter + ; + } + + bool transbg = backgroundOrigin() != WidgetOrigin; + // we ignore NorthWestGravity at the moment for reversed layout + if ( transbg || + (!testWFlags( WStaticContents ) && + testWState( WState_Exposed ) && was_resize ) || + QApplication::reverseLayout() ) { + // remove unnecessary paint events from the queue + XEvent xevent; + while ( XCheckTypedWindowEvent( x11Display(), winId(), Expose, &xevent ) && + ! qt_x11EventFilter( &xevent ) && + ! x11Event( &xevent ) ) // send event through filter + ; + repaint( !testWFlags(WResizeNoErase) || transbg ); + } + + incrementSyncCounter(); + + return TRUE; +} + + +// +// Close window event translation. +// +bool QETWidget::translateCloseEvent( const XEvent * ) +{ + return close(FALSE); +} + + +/*! + Sets the text cursor's flash (blink) time to \a msecs + milliseconds. The flash time is the time required to display, + invert and restore the caret display. Usually the text cursor is + displayed for \a msecs/2 milliseconds, then hidden for \a msecs/2 + milliseconds, but this may vary. + + Note that on Microsoft Windows, calling this function sets the + cursor flash time for all windows. + + \sa cursorFlashTime() +*/ +void QApplication::setCursorFlashTime( int msecs ) +{ + cursor_flash_time = msecs; +} + + +/*! + Returns the text cursor's flash (blink) time in milliseconds. The + flash time is the time required to display, invert and restore the + caret display. + + The default value on X11 is 1000 milliseconds. On Windows, the + control panel value is used. + + Widgets should not cache this value since it may be changed at any + time by the user changing the global desktop settings. + + \sa setCursorFlashTime() +*/ +int QApplication::cursorFlashTime() +{ + return cursor_flash_time; +} + +/*! + Sets the time limit that distinguishes a double click from two + consecutive mouse clicks to \a ms milliseconds. + + Note that on Microsoft Windows, calling this function sets the + double click interval for all windows. + + \sa doubleClickInterval() +*/ + +void QApplication::setDoubleClickInterval( int ms ) +{ + mouse_double_click_time = ms; +} + + +/*! + Returns the maximum duration for a double click. + + The default value on X11 is 400 milliseconds. On Windows, the + control panel value is used. + + \sa setDoubleClickInterval() +*/ + +int QApplication::doubleClickInterval() +{ + return mouse_double_click_time; +} + + +/*! + Sets the number of lines to scroll when the mouse wheel is rotated + to \a n. + + If this number exceeds the number of visible lines in a certain + widget, the widget should interpret the scroll operation as a + single page up / page down operation instead. + + \sa wheelScrollLines() +*/ +void QApplication::setWheelScrollLines( int n ) +{ + wheel_scroll_lines = n; +} + +/*! + Returns the number of lines to scroll when the mouse wheel is + rotated. + + \sa setWheelScrollLines() +*/ +int QApplication::wheelScrollLines() +{ + return wheel_scroll_lines; +} + +/*! + Enables the UI effect \a effect if \a enable is TRUE, otherwise + the effect will not be used. + + Note: All effects are disabled on screens running at less than + 16-bit color depth. + + \sa isEffectEnabled(), Qt::UIEffect, setDesktopSettingsAware() +*/ +void QApplication::setEffectEnabled( Qt::UIEffect effect, bool enable ) +{ + switch (effect) { + case UI_AnimateMenu: + if ( enable ) fade_menu = FALSE; + animate_menu = enable; + break; + case UI_FadeMenu: + if ( enable ) + animate_menu = TRUE; + fade_menu = enable; + break; + case UI_AnimateCombo: + animate_combo = enable; + break; + case UI_AnimateTooltip: + if ( enable ) fade_tooltip = FALSE; + animate_tooltip = enable; + break; + case UI_FadeTooltip: + if ( enable ) + animate_tooltip = TRUE; + fade_tooltip = enable; + break; + case UI_AnimateToolBox: + animate_toolbox = enable; + break; + default: + animate_ui = enable; + break; + } +} + +/*! + Returns TRUE if \a effect is enabled; otherwise returns FALSE. + + By default, Qt will try to use the desktop settings. Call + setDesktopSettingsAware(FALSE) to prevent this. + + Note: All effects are disabled on screens running at less than + 16-bit color depth. + + \sa setEffectEnabled(), Qt::UIEffect +*/ +bool QApplication::isEffectEnabled( Qt::UIEffect effect ) +{ + if ( QColor::numBitPlanes() < 16 || !animate_ui ) + return FALSE; + + switch( effect ) { + case UI_AnimateMenu: + return animate_menu; + case UI_FadeMenu: + return fade_menu; + case UI_AnimateCombo: + return animate_combo; + case UI_AnimateTooltip: + return animate_tooltip; + case UI_FadeTooltip: + return fade_tooltip; + case UI_AnimateToolBox: + return animate_toolbox; + default: + return animate_ui; + } +} + +/***************************************************************************** + Session management support + *****************************************************************************/ + +#ifndef QT_NO_SM_SUPPORT + +#include <X11/SM/SMlib.h> + +class QSessionManagerData +{ +public: + QSessionManagerData( QSessionManager* mgr, QString& id, QString& key ) + : sm( mgr ), sessionId( id ), sessionKey( key ) {} + QSessionManager* sm; + QStringList restartCommand; + QStringList discardCommand; + QString& sessionId; + QString& sessionKey; + QSessionManager::RestartHint restartHint; +}; + +class QSmSocketReceiver : public QObject +{ + Q_OBJECT +public: + QSmSocketReceiver( int socket ) + : QObject(0,0) + { + QSocketNotifier* sn = new QSocketNotifier( socket, QSocketNotifier::Read, this ); + connect( sn, SIGNAL( activated(int) ), this, SLOT( socketActivated(int) ) ); + } + +public slots: + void socketActivated(int); +}; + + +static SmcConn smcConnection = 0; +static bool sm_interactionActive; +static bool sm_smActive; +static int sm_interactStyle; +static int sm_saveType; +static bool sm_cancel; +// static bool sm_waitingForPhase2; ### never used?!? +static bool sm_waitingForInteraction; +static bool sm_isshutdown; +// static bool sm_shouldbefast; ### never used?!? +static bool sm_phase2; +static bool sm_in_phase2; + +static QSmSocketReceiver* sm_receiver = 0; + +static void resetSmState(); +static void sm_setProperty( const char* name, const char* type, + int num_vals, SmPropValue* vals); +static void sm_saveYourselfCallback( SmcConn smcConn, SmPointer clientData, + int saveType, Bool shutdown , int interactStyle, Bool fast); +static void sm_saveYourselfPhase2Callback( SmcConn smcConn, SmPointer clientData ) ; +static void sm_dieCallback( SmcConn smcConn, SmPointer clientData ) ; +static void sm_shutdownCancelledCallback( SmcConn smcConn, SmPointer clientData ); +static void sm_saveCompleteCallback( SmcConn smcConn, SmPointer clientData ); +static void sm_interactCallback( SmcConn smcConn, SmPointer clientData ); +static void sm_performSaveYourself( QSessionManagerData* ); + +static void resetSmState() +{ +// sm_waitingForPhase2 = FALSE; ### never used?!? + sm_waitingForInteraction = FALSE; + sm_interactionActive = FALSE; + sm_interactStyle = SmInteractStyleNone; + sm_smActive = FALSE; + sm_blockUserInput = FALSE; + sm_isshutdown = FALSE; +// sm_shouldbefast = FALSE; ### never used?!? + sm_phase2 = FALSE; + sm_in_phase2 = FALSE; +} + + +// theoretically it's possible to set several properties at once. For +// simplicity, however, we do just one property at a time +static void sm_setProperty( const char* name, const char* type, + int num_vals, SmPropValue* vals) +{ + if (num_vals ) { + SmProp prop; + prop.name = (char*)name; + prop.type = (char*)type; + prop.num_vals = num_vals; + prop.vals = vals; + + SmProp* props[1]; + props[0] = ∝ + SmcSetProperties( smcConnection, 1, props ); + } + else { + char* names[1]; + names[0] = (char*) name; + SmcDeleteProperties( smcConnection, 1, names ); + } +} + +static void sm_setProperty( const QString& name, const QString& value) +{ + SmPropValue prop; + prop.length = value.length(); + prop.value = (SmPointer) value.latin1(); + sm_setProperty( name.latin1(), SmARRAY8, 1, &prop ); +} + +static void sm_setProperty( const QString& name, const QStringList& value) +{ + SmPropValue *prop = new SmPropValue[ value.count() ]; + int count = 0; + for ( QStringList::ConstIterator it = value.begin(); it != value.end(); ++it ) { + prop[ count ].length = (*it).length(); + prop[ count ].value = (char*)(*it).latin1(); + ++count; + } + sm_setProperty( name.latin1(), SmLISTofARRAY8, count, prop ); + delete [] prop; +} + + +// workaround for broken libsm, see below +struct QT_smcConn { + unsigned int save_yourself_in_progress : 1; + unsigned int shutdown_in_progress : 1; +}; + +static void sm_saveYourselfCallback( SmcConn smcConn, SmPointer clientData, + int saveType, Bool shutdown , int interactStyle, Bool /*fast*/) +{ + if (smcConn != smcConnection ) + return; + sm_cancel = FALSE; + sm_smActive = TRUE; + sm_isshutdown = shutdown; + sm_saveType = saveType; + sm_interactStyle = interactStyle; +// sm_shouldbefast = fast; ### never used?!? + + // ugly workaround for broken libSM. libSM should do that _before_ + // actually invoking the callback in sm_process.c + ( (QT_smcConn*)smcConn )->save_yourself_in_progress = TRUE; + if ( sm_isshutdown ) + ( (QT_smcConn*)smcConn )->shutdown_in_progress = TRUE; + + sm_performSaveYourself( (QSessionManagerData*) clientData ); + if ( !sm_isshutdown ) // we cannot expect a confirmation message in that case + resetSmState(); +} + +static void sm_performSaveYourself( QSessionManagerData* smd ) +{ + if ( sm_isshutdown ) + sm_blockUserInput = TRUE; + + QSessionManager* sm = smd->sm; + + // generate a new session key + timeval tv; + gettimeofday( &tv, 0 ); + smd->sessionKey = QString::number( tv.tv_sec ) + "_" + QString::number(tv.tv_usec); + + // tell the session manager about our program in best POSIX style + sm_setProperty( SmProgram, QString( qApp->argv()[0] ) ); + // tell the session manager about our user as well. + struct passwd* entry = getpwuid( geteuid() ); + if ( entry ) + sm_setProperty( SmUserID, QString::fromLatin1( entry->pw_name ) ); + + // generate a restart and discard command that makes sense + QStringList restart; + restart << qApp->argv()[0] << "-session" << smd->sessionId + "_" + smd->sessionKey; + if (qstricmp(qAppName(), qAppClass()) != 0) + restart << "-name" << qAppName(); + sm->setRestartCommand( restart ); + QStringList discard; + sm->setDiscardCommand( discard ); + + switch ( sm_saveType ) { + case SmSaveBoth: + qApp->commitData( *sm ); + if ( sm_isshutdown && sm_cancel) + break; // we cancelled the shutdown, no need to save state + // fall through + case SmSaveLocal: + qApp->saveState( *sm ); + break; + case SmSaveGlobal: + qApp->commitData( *sm ); + break; + default: + break; + } + + if ( sm_phase2 && !sm_in_phase2 ) { + SmcRequestSaveYourselfPhase2( smcConnection, sm_saveYourselfPhase2Callback, (SmPointer*) smd ); + sm_blockUserInput = FALSE; + } + else { + // close eventual interaction monitors and cancel the + // shutdown, if required. Note that we can only cancel when + // performing a shutdown, it does not work for checkpoints + if ( sm_interactionActive ) { + SmcInteractDone( smcConnection, sm_isshutdown && sm_cancel); + sm_interactionActive = FALSE; + } + else if ( sm_cancel && sm_isshutdown ) { + if ( sm->allowsErrorInteraction() ) { + SmcInteractDone( smcConnection, True ); + sm_interactionActive = FALSE; + } + } + + // set restart and discard command in session manager + sm_setProperty( SmRestartCommand, sm->restartCommand() ); + sm_setProperty( SmDiscardCommand, sm->discardCommand() ); + + // set the restart hint + SmPropValue prop; + prop.length = sizeof( int ); + int value = sm->restartHint(); + prop.value = (SmPointer) &value; + sm_setProperty( SmRestartStyleHint, SmCARD8, 1, &prop ); + + // we are done + SmcSaveYourselfDone( smcConnection, !sm_cancel ); + } +} + +static void sm_dieCallback( SmcConn smcConn, SmPointer /* clientData */) +{ + if (smcConn != smcConnection ) + return; + resetSmState(); + QEvent quitEvent(QEvent::Quit); + QApplication::sendEvent(qApp, &quitEvent); +} + +static void sm_shutdownCancelledCallback( SmcConn smcConn, SmPointer /* clientData */) +{ + if (smcConn != smcConnection ) + return; + if ( sm_waitingForInteraction ) + qApp->exit_loop(); + resetSmState(); +} + +static void sm_saveCompleteCallback( SmcConn smcConn, SmPointer /*clientData */) +{ + if (smcConn != smcConnection ) + return; + resetSmState(); +} + +static void sm_interactCallback( SmcConn smcConn, SmPointer /* clientData */ ) +{ + if (smcConn != smcConnection ) + return; + if ( sm_waitingForInteraction ) + qApp->exit_loop(); +} + +static void sm_saveYourselfPhase2Callback( SmcConn smcConn, SmPointer clientData ) +{ + if (smcConn != smcConnection ) + return; + sm_in_phase2 = TRUE; + sm_performSaveYourself( (QSessionManagerData*) clientData ); +} + + +void QSmSocketReceiver::socketActivated(int) +{ + IceProcessMessages( SmcGetIceConnection( smcConnection ), 0, 0); +} + + +#undef Bool +#include "qapplication_x11.moc" + +QSessionManager::QSessionManager( QApplication * app, QString &id, QString& key ) + : QObject( app, "session manager" ) +{ + d = new QSessionManagerData( this, id, key ); + d->restartHint = RestartIfRunning; + + resetSmState(); + char cerror[256]; + char* myId = 0; + char* prevId = (char*)id.latin1(); // we know what we are doing + + SmcCallbacks cb; + cb.save_yourself.callback = sm_saveYourselfCallback; + cb.save_yourself.client_data = (SmPointer) d; + cb.die.callback = sm_dieCallback; + cb.die.client_data = (SmPointer) d; + cb.save_complete.callback = sm_saveCompleteCallback; + cb.save_complete.client_data = (SmPointer) d; + cb.shutdown_cancelled.callback = sm_shutdownCancelledCallback; + cb.shutdown_cancelled.client_data = (SmPointer) d; + + // avoid showing a warning message below + const char* session_manager = getenv("SESSION_MANAGER"); + if ( !session_manager || !session_manager[0] ) + return; + + smcConnection = SmcOpenConnection( 0, 0, 1, 0, + SmcSaveYourselfProcMask | + SmcDieProcMask | + SmcSaveCompleteProcMask | + SmcShutdownCancelledProcMask, + &cb, + prevId, + &myId, + 256, cerror ); + + id = QString::fromLatin1( myId ); + ::free( myId ); // it was allocated by C + + QString error = cerror; + if (!smcConnection ) { + qWarning("Session management error: %s", error.latin1() ); + } + else { + sm_receiver = new QSmSocketReceiver( IceConnectionNumber( SmcGetIceConnection( smcConnection ) ) ); + } +} + +QSessionManager::~QSessionManager() +{ + if ( smcConnection ) + SmcCloseConnection( smcConnection, 0, 0 ); + smcConnection = 0; + delete sm_receiver; + delete d; +} + +QString QSessionManager::sessionId() const +{ + return d->sessionId; +} + +QString QSessionManager::sessionKey() const +{ + return d->sessionKey; +} + + +void* QSessionManager::handle() const +{ + return (void*) smcConnection; +} + + +bool QSessionManager::allowsInteraction() +{ + if ( sm_interactionActive ) + return TRUE; + + if ( sm_waitingForInteraction ) + return FALSE; + + if ( sm_interactStyle == SmInteractStyleAny ) { + sm_waitingForInteraction = SmcInteractRequest( smcConnection, SmDialogNormal, + sm_interactCallback, (SmPointer*) this ); + } + if ( sm_waitingForInteraction ) { + qApp->enter_loop(); + sm_waitingForInteraction = FALSE; + if ( sm_smActive ) { // not cancelled + sm_interactionActive = TRUE; + sm_blockUserInput = FALSE; + return TRUE; + } + } + return FALSE; +} + +bool QSessionManager::allowsErrorInteraction() +{ + if ( sm_interactionActive ) + return TRUE; + + if ( sm_waitingForInteraction ) + return FALSE; + + if ( sm_interactStyle == SmInteractStyleAny || sm_interactStyle == SmInteractStyleErrors ) { + sm_waitingForInteraction = SmcInteractRequest( smcConnection, SmDialogError, + sm_interactCallback, (SmPointer*) this ); + } + if ( sm_waitingForInteraction ) { + qApp->enter_loop(); + sm_waitingForInteraction = FALSE; + if ( sm_smActive ) { // not cancelled + sm_interactionActive = TRUE; + sm_blockUserInput = FALSE; + return TRUE; + } + } + return FALSE; +} + +void QSessionManager::release() +{ + if ( sm_interactionActive ) { + SmcInteractDone( smcConnection, False ); + sm_interactionActive = FALSE; + if ( sm_smActive && sm_isshutdown ) + sm_blockUserInput = TRUE; + } +} + +void QSessionManager::cancel() +{ + sm_cancel = TRUE; +} + +void QSessionManager::setRestartHint( QSessionManager::RestartHint hint) +{ + d->restartHint = hint; +} + +QSessionManager::RestartHint QSessionManager::restartHint() const +{ + return d->restartHint; +} + +void QSessionManager::setRestartCommand( const QStringList& command) +{ + d->restartCommand = command; +} + +QStringList QSessionManager::restartCommand() const +{ + return d->restartCommand; +} + +void QSessionManager::setDiscardCommand( const QStringList& command) +{ + d->discardCommand = command; +} + +QStringList QSessionManager::discardCommand() const +{ + return d->discardCommand; +} + +void QSessionManager::setManagerProperty( const QString& name, const QString& value) +{ + SmPropValue prop; + prop.length = value.length(); + prop.value = (SmPointer) value.utf8().data(); + sm_setProperty( name.latin1(), SmARRAY8, 1, &prop ); +} + +void QSessionManager::setManagerProperty( const QString& name, const QStringList& value) +{ + SmPropValue *prop = new SmPropValue[ value.count() ]; + int count = 0; + for ( QStringList::ConstIterator it = value.begin(); it != value.end(); ++it ) { + prop[ count ].length = (*it).length(); + prop[ count ].value = (char*)(*it).utf8().data(); + ++count; + } + sm_setProperty( name.latin1(), SmLISTofARRAY8, count, prop ); + delete [] prop; +} + +bool QSessionManager::isPhase2() const +{ + return sm_in_phase2; +} + +void QSessionManager::requestPhase2() +{ + sm_phase2 = TRUE; +} + + +#endif // QT_NO_SM_SUPPORT diff --git a/src/kernel/qasyncimageio.cpp b/src/kernel/qasyncimageio.cpp new file mode 100644 index 0000000..8ecd1eb --- /dev/null +++ b/src/kernel/qasyncimageio.cpp @@ -0,0 +1,1309 @@ +/**************************************************************************** +** +** Implementation of asynchronous image/movie loading classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qasyncimageio.h" + +#ifndef QT_NO_ASYNC_IMAGE_IO + +#include "qptrlist.h" +#include "qgif.h" +#include <stdlib.h> + +extern void qt_init_image_handlers(); +extern void qt_init_image_plugins(); + +#define Q_TRANSPARENT 0x00ffffff + +/*! + \class QImageConsumer qasyncimageio.h + \brief The QImageConsumer class is an abstraction used by QImageDecoder. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + The QMovie class, or QLabel::setMovie(), are easy to use and for + most situations do what you want with regards animated images. + + A QImageConsumer consumes information about changes to the QImage + maintained by a QImageDecoder. Think of the QImage as the model or + source of the image data, with the QImageConsumer as a view of + that data and the QImageDecoder being the controller that + orchestrates the relationship between the model and the view. + + You'd use the QImageConsumer class, for example, if you were + implementing a web browser with your own image loaders. + + \sa QImageDecoder +*/ + +/*! + \fn void QImageConsumer::changed(const QRect&) + + Called when the given area of the image has changed. +*/ + +/*! + \fn void QImageConsumer::end() + + Called when all the data from all the frames has been decoded and + revealed as changed(). +*/ + +/*! + \fn void QImageConsumer::frameDone() + + One of the two frameDone() functions will be called when a frame + of an animated image has ended and been revealed as changed(). + + When this function is called, the current image should be + displayed. + + The decoder will not make any further changes to the image until + the next call to QImageFormat::decode(). +*/ + +/*! + \overload void QImageConsumer::frameDone( const QPoint& offset, const QRect& rect ) + + One of the two frameDone() functions will be called when a frame + of an animated image has ended and been revealed as changed(). + + When this function is called, the area \a rect in the current + image should be moved by \a offset and displayed. + + The decoder will not make any further changes to the image until + the next call to QImageFormat::decode(). +*/ + +/*! + \fn void QImageConsumer::setLooping(int n) + + Called to indicate that the sequence of frames in the image + should be repeated \a n times, including the sequence during + decoding. + + \list + \i 0 = Forever + \i 1 = Only display frames the first time through + \i 2 = Repeat once after first pass through images + \i etc. + \endlist + + To make the QImageDecoder do this, just delete it and pass the + information to it again for decoding (setLooping() will be called + again, of course, but that can be ignored), or keep copies of the + changed areas at the ends of frames. +*/ + +/*! + \fn void QImageConsumer::setFramePeriod(int milliseconds) + + Notes that the frame about to be decoded should not be displayed + until the given number of \a milliseconds after the time that this + function is called. Of course, the image may not have been + decoded by then, in which case the frame should not be displayed + until it is complete. A value of -1 (the assumed default) + indicates that the image should be displayed even while it is only + partially loaded. +*/ + +/*! + \fn void QImageConsumer::setSize(int, int) + + This function is called as soon as the size of the image has been + determined. +*/ + + +/*! + \class QImageDecoder qasyncimageio.h + \brief The QImageDecoder class is an incremental image decoder for all supported image formats. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + New formats are installed by creating objects of class + QImageFormatType; the QMovie class can be used for all installed + incremental image formats. QImageDecoder is only useful for + creating new ways of feeding data to an QImageConsumer. + + A QImageDecoder is a machine that decodes images. It takes encoded + image data via its decode() method and expresses its decoding by + supplying information to a QImageConsumer. It implements its + decoding by using a QImageFormat created by one of the + currently-existing QImageFormatType factory objects. + + QImageFormatType and QImageFormat are the classes that you might + need to implement support for additional image formats. + + \legalese + + Qt supports GIF reading if it is configured that way during + installation (see qgif.h). If it is, we are required to state that + "The Graphics Interchange Format(c) is the Copyright property of + CompuServe Incorporated. GIF(sm) is a Service Mark property of + CompuServe Incorporated." + + \warning If you are in a country that recognizes software patents + and in which Unisys holds a patent on LZW compression and/or + decompression and you want to use GIF, Unisys may require you to + license that technology. Such countries include Canada, Japan, + the USA, France, Germany, Italy and the UK. + + GIF support may be removed completely in a future version of Qt. + We recommend using the MNG or PNG format. +*/ + +static const int max_header = 32; + + + + + +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 +class QGIFFormat : public QImageFormat { +public: + QGIFFormat(); + virtual ~QGIFFormat(); + + int decode(QImage& img, QImageConsumer* consumer, + const uchar* buffer, int length); + +private: + void fillRect(QImage&, int x, int y, int w, int h, QRgb col); + QRgb color( uchar index ) const; + + // GIF specific stuff + QRgb* globalcmap; + QRgb* localcmap; + QImage backingstore; + unsigned char hold[16]; + bool gif89; + int count; + int ccount; + int expectcount; + enum State { + Header, + LogicalScreenDescriptor, + GlobalColorMap, + LocalColorMap, + Introducer, + ImageDescriptor, + TableImageLZWSize, + ImageDataBlockSize, + ImageDataBlock, + ExtensionLabel, + GraphicControlExtension, + ApplicationExtension, + NetscapeExtensionBlockSize, + NetscapeExtensionBlock, + SkipBlockSize, + SkipBlock, + Done, + Error + } state; + int gncols; + int lncols; + int ncols; + int lzwsize; + bool lcmap; + int swidth, sheight; + int width, height; + int left, top, right, bottom; + enum Disposal { NoDisposal, DoNotChange, RestoreBackground, RestoreImage }; + Disposal disposal; + bool disposed; + int trans_index; + bool gcmap; + int bgcol; + int interlace; + int accum; + int bitcount; + + enum { max_lzw_bits=12 }; // (poor-compiler's static const int) + + int code_size, clear_code, end_code, max_code_size, max_code; + int firstcode, oldcode, incode; + short table[2][1<< max_lzw_bits]; + short stack[(1<<(max_lzw_bits))*2]; + short *sp; + bool needfirst; + int x, y; + int frame; + bool out_of_bounds; + bool digress; + void nextY(QImage& img, QImageConsumer* consumer); + void disposePrevious( QImage& img, QImageConsumer* consumer ); +}; + +class QGIFFormatType : public QImageFormatType +{ + QImageFormat* decoderFor(const uchar* buffer, int length); + const char* formatName() const; +}; + +#endif + + +class QImageDecoderPrivate +{ +public: + QImageDecoderPrivate() + { + count = 0; + } + + static void cleanup(); + + static void ensureFactories() + { + if ( !factories ) { + factories = new QPtrList<QImageFormatType>; +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + gif_decoder_factory = new QGIFFormatType; +#endif + qt_init_image_handlers(); + qAddPostRoutine( cleanup ); + } + } + + static QPtrList<QImageFormatType> * factories; + +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + static QGIFFormatType * gif_decoder_factory; +#endif + + uchar header[max_header]; + int count; +}; + +QPtrList<QImageFormatType> * QImageDecoderPrivate::factories = 0; +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 +QGIFFormatType * QImageDecoderPrivate::gif_decoder_factory = 0; +#endif + + +void QImageDecoderPrivate::cleanup() +{ + delete factories; + factories = 0; +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + delete gif_decoder_factory; + gif_decoder_factory = 0; +#endif +} + + +/*! + Constructs a QImageDecoder that will send change information to + the QImageConsumer \a c. +*/ +QImageDecoder::QImageDecoder(QImageConsumer* c) +{ + qt_init_image_handlers(); + d = new QImageDecoderPrivate; + Q_CHECK_PTR(d); + consumer = c; + actual_decoder = 0; +} + +/*! + Destroys a QImageDecoder. The image it built is destroyed. The + decoder built by the factory for the file format is destroyed. The + consumer for which it decoded the image is \e not destroyed. +*/ +QImageDecoder::~QImageDecoder() +{ + delete d; + delete actual_decoder; +} + +/*! + \fn const QImage& QImageDecoder::image() + + Returns the image currently being decoded. +*/ + +static bool plugins_loaded = FALSE; + +/*! + Call this function to decode some data into image changes. The + data in \a buffer will be decoded, sending change information to + the QImageConsumer of this QImageDecoder until one of the change + functions of the consumer returns FALSE. The length of the data is + given in \a length. + + Returns the number of bytes consumed: 0 if consumption is + complete, and -1 if decoding fails due to invalid data. +*/ +int QImageDecoder::decode(const uchar* buffer, int length) +{ + if (!actual_decoder) { + int i=0; + + while (i < length && d->count < max_header) + d->header[d->count++] = buffer[i++]; + + QImageDecoderPrivate::ensureFactories(); + + for (QImageFormatType* f = QImageDecoderPrivate::factories->first(); + f && !actual_decoder; + f = QImageDecoderPrivate::factories->next()) + { + actual_decoder = f->decoderFor(d->header, d->count); + } + if ( !actual_decoder && !plugins_loaded) { + qt_init_image_plugins(); + plugins_loaded = TRUE; + + for (QImageFormatType* f = QImageDecoderPrivate::factories->first(); + f && !actual_decoder; + f = QImageDecoderPrivate::factories->next()) + { + actual_decoder = f->decoderFor(d->header, d->count); + } + } + + if (!actual_decoder) { + if ( d->count < max_header ) { + // not enough info yet + return i; + } else { + // failure - nothing matches max_header bytes + return -1; + } + } + } + return actual_decoder->decode(img, consumer, buffer, length); +} + +/*! + Returns a QImageFormatType by name. This might be used when the + user needs to force data to be interpreted as being in a certain + format. \a name is one of the formats listed by + QImageDecoder::inputFormats(). Note that you will still need to + supply decodable data to result->decoderFor() before you can begin + decoding the data. +*/ +QImageFormatType* QImageDecoder::format( const char* name ) +{ + QImageDecoderPrivate::ensureFactories(); + qt_init_image_plugins(); + + for (QImageFormatType* f = QImageDecoderPrivate::factories->first(); + f; + f = QImageDecoderPrivate::factories->next()) + { + if ( qstricmp(name,f->formatName())==0 ) + return f; + } + return 0; +} + +/*! + Call this function to find the name of the format of the given + header. The returned string is statically allocated. The function + will look at the first \a length characters in the \a buffer. + + Returns 0 if the format is not recognized. +*/ +const char* QImageDecoder::formatName(const uchar* buffer, int length) +{ + QImageDecoderPrivate::ensureFactories(); + + const char* name = 0; + for (QImageFormatType* f = QImageDecoderPrivate::factories->first(); + f && !name; + f = QImageDecoderPrivate::factories->next()) + { + QImageFormat *decoder = f->decoderFor(buffer, length); + if (decoder) { + name = f->formatName(); + delete decoder; + } + } + if ( !name && !plugins_loaded) { + qt_init_image_plugins(); + plugins_loaded = TRUE; + for (QImageFormatType* f = QImageDecoderPrivate::factories->first(); + f && !name; + f = QImageDecoderPrivate::factories->next()) + { + QImageFormat *decoder = f->decoderFor(buffer, length); + if (decoder) { + name = f->formatName(); + delete decoder; + } + } + } + + return name; +} + +/*! + Returns a sorted list of formats for which asynchronous loading is + supported. +*/ +QStrList QImageDecoder::inputFormats() +{ + QImageDecoderPrivate::ensureFactories(); + qt_init_image_plugins(); + + QStrList result; + + for (QImageFormatType* f = QImageDecoderPrivate::factories->first(); + f; + f = QImageDecoderPrivate::factories->next()) + { + if ( !result.contains( f->formatName() ) ) { + result.inSort( f->formatName() ); + } + } + + return result; +} + +/*! + Registers the new QImageFormatType \a f. This is not needed in + application code because factories call this themselves. +*/ +void QImageDecoder::registerDecoderFactory(QImageFormatType* f) +{ + QImageDecoderPrivate::ensureFactories(); + + QImageDecoderPrivate::factories->insert(0,f); +} + +/*! + Unregisters the QImageFormatType \a f. This is not needed in + application code because factories call this themselves. +*/ +void QImageDecoder::unregisterDecoderFactory(QImageFormatType* f) +{ + if ( !QImageDecoderPrivate::factories ) + return; + + QImageDecoderPrivate::factories->remove(f); +} + +/*! + \class QImageFormat qasyncimageio.h + \brief The QImageFormat class is an incremental image decoder for a specific image format. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + By making a derived class of QImageFormatType, which in turn + creates objects that are a subclass of QImageFormat, you can add + support for more incremental image formats, allowing such formats + to be sources for a QMovie or for the first frame of the image + stream to be loaded as a QImage or QPixmap. + + Your new subclass must reimplement the decode() function in order + to process your new format. + + New QImageFormat objects are generated by new QImageFormatType + factories. +*/ + +/*! + Destroys the object. + + \internal + More importantly, destroys derived classes. +*/ +QImageFormat::~QImageFormat() +{ +} + +/*! + \fn int QImageFormat::decode(QImage& img, QImageConsumer* consumer, const uchar* buffer, int length) + + New subclasses must reimplement this method. + + It should decode some or all of the bytes from \a buffer into \a + img, calling the methods of \a consumer as the decoding proceeds + to inform that consumer of changes to the image. The length of the + data is given in \a length. The consumer may be 0, in which case + the function should just process the data into \a img without + telling any consumer about the changes. Note that the decoder must + store enough state to be able to continue in subsequent calls to + this method - this is the essence of the incremental image + loading. + + The function should return without processing all the data if it + reaches the end of a frame in the input. + + The function must return the number of bytes it has processed. +*/ + +/*! + \class QImageFormatType qasyncimageio.h + \brief The QImageFormatType class is a factory that makes QImageFormat objects. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + Whereas the QImageIO class allows for \e complete loading of + images, QImageFormatType allows for \e incremental loading of + images. + + New image file formats are installed by creating objects of + derived classes of QImageFormatType. They must implement + decoderFor() and formatName(). + + QImageFormatType is a very simple class. Its only task is to + recognize image data in some format and make a new object, + subclassed from QImageFormat, which can decode that format. + + The factories for formats built into Qt are automatically defined + before any other factory is initialized. If two factories would + recognize an image format, the factory created last will override + the earlier one; you can thus override current and future built-in + formats. +*/ + +/*! + \fn virtual QImageFormat* QImageFormatType::decoderFor(const uchar* buffer, int length) + + Returns a decoder for decoding an image that starts with the bytes + in \a buffer. The length of the data is given in \a length. This + function should only return a decoder if it is certain that the + decoder applies to data with the given header. Returns 0 if there + is insufficient data in the header to make a positive + identification or if the data is not recognized. +*/ + +/*! + \fn virtual const char* QImageFormatType::formatName() const + + Returns the name of the format supported by decoders from this + factory. The string is statically allocated. +*/ + +/*! + Constructs a factory. It automatically registers itself with + QImageDecoder. +*/ +QImageFormatType::QImageFormatType() +{ + QImageDecoder::registerDecoderFactory(this); +} + +/*! + Destroys a factory. It automatically unregisters itself from + QImageDecoder. +*/ +QImageFormatType::~QImageFormatType() +{ + QImageDecoder::unregisterDecoderFactory(this); +} + + +/*! + Returns TRUE if Qt was compiled with built-in GIF reading support; + otherwise returns FALSE. +*/ +bool qt_builtin_gif_reader() +{ +#if defined(QT_BUILTIN_GIF_READER) + return QT_BUILTIN_GIF_READER == 1; +#else + return 0; +#endif +} + +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + +/* -- NOTDOC + \class QGIFFormat qasyncimageio.h + \brief Incremental image decoder for GIF image format. + + \ingroup images + \ingroup graphics + + This subclass of QImageFormat decodes GIF format images, + including animated GIFs. Internally in +*/ + +/*! + Constructs a QGIFFormat. +*/ +QGIFFormat::QGIFFormat() +{ + globalcmap = 0; + localcmap = 0; + lncols = 0; + gncols = 0; + disposal = NoDisposal; + out_of_bounds = FALSE; + disposed = TRUE; + frame = -1; + state = Header; + count = 0; + lcmap = FALSE; +} + +/*! + Destroys a QGIFFormat. +*/ +QGIFFormat::~QGIFFormat() +{ + if (globalcmap) delete[] globalcmap; + if ( localcmap ) delete[] localcmap; +} + + +/* -- NOTDOC + \class QGIFFormatType qasyncimageio.h + \brief Incremental image decoder for GIF image format. + + \ingroup images + \ingroup graphics + + This subclass of QImageFormatType recognizes GIF + format images, creating a QGIFFormat when required. An instance + of this class is created automatically before any other factories, + so you should have no need for such objects. +*/ + +QImageFormat* QGIFFormatType::decoderFor( + const uchar* buffer, int length) +{ + if (length < 6) return 0; + if (buffer[0]=='G' + && buffer[1]=='I' + && buffer[2]=='F' + && buffer[3]=='8' + && (buffer[4]=='9' || buffer[4]=='7') + && buffer[5]=='a') + return new QGIFFormat; + return 0; +} + +const char* QGIFFormatType::formatName() const +{ + return "GIF"; +} + + +void QGIFFormat::disposePrevious( QImage& img, QImageConsumer* consumer ) +{ + if ( out_of_bounds ) // flush anything that survived + consumer->changed(QRect(0,0,swidth,sheight)); + + // Handle disposal of previous image before processing next one + + if ( disposed ) return; + + int l = QMIN(swidth-1,left); + int r = QMIN(swidth-1,right); + int t = QMIN(sheight-1,top); + int b = QMIN(sheight-1,bottom); + + switch (disposal) { + case NoDisposal: + break; + case DoNotChange: + break; + case RestoreBackground: + if (trans_index>=0) { + // Easy: we use the transparent color + fillRect(img, l, t, r-l+1, b-t+1, Q_TRANSPARENT); + } else if (bgcol>=0) { + // Easy: we use the bgcol given + fillRect(img, l, t, r-l+1, b-t+1, color(bgcol)); + } else { + // Impossible: We don't know of a bgcol - use pixel 0 + QRgb** line = (QRgb **)img.jumpTable(); + fillRect(img, l, t, r-l+1, b-t+1, line[0][0]); + } + if (consumer) + consumer->changed(QRect(l, t, r-l+1, b-t+1)); + break; + case RestoreImage: { + if ( frame >= 0 ) { + QRgb** line = (QRgb **)img.jumpTable(); + for (int ln=t; ln<=b; ln++) { + memcpy(line[ln]+l, + backingstore.scanLine(ln-t), + (r-l+1)*sizeof(QRgb) ); + } + consumer->changed(QRect(l, t, r-l+1, b-t+1)); + } + } + } + disposal = NoDisposal; // Until an extension says otherwise. + + disposed = TRUE; +} + +/*! + This function decodes some data into image changes. + + Returns the number of bytes consumed. +*/ +int QGIFFormat::decode(QImage& img, QImageConsumer* consumer, + const uchar* buffer, int length) +{ + // We are required to state that + // "The Graphics Interchange Format(c) is the Copyright property of + // CompuServe Incorporated. GIF(sm) is a Service Mark property of + // CompuServe Incorporated." + +#define LM(l, m) (((m)<<8)|l) + digress = FALSE; + int initial = length; + QRgb** line = (QRgb **)img.jumpTable(); + while (!digress && length) { + length--; + unsigned char ch=*buffer++; + switch (state) { + case Header: + hold[count++]=ch; + if (count==6) { + // Header + gif89=(hold[3]!='8' || hold[4]!='7'); + state=LogicalScreenDescriptor; + count=0; + } + break; + case LogicalScreenDescriptor: + hold[count++]=ch; + if (count==7) { + // Logical Screen Descriptor + swidth=LM(hold[0], hold[1]); + sheight=LM(hold[2], hold[3]); + gcmap=!!(hold[4]&0x80); + //UNUSED: bpchan=(((hold[4]&0x70)>>3)+1); + //UNUSED: gcmsortflag=!!(hold[4]&0x08); + gncols=2<<(hold[4]&0x7); + bgcol=(gcmap) ? hold[5] : -1; + //aspect=hold[6] ? double(hold[6]+15)/64.0 : 1.0; + + trans_index = -1; + count=0; + ncols=gncols; + if (gcmap) { + ccount=0; + state=GlobalColorMap; + globalcmap = new QRgb[gncols+1]; // +1 for trans_index + globalcmap[gncols] = Q_TRANSPARENT; + } else { + state=Introducer; + } + } + break; + case GlobalColorMap: case LocalColorMap: + hold[count++]=ch; + if (count==3) { + QRgb rgb = qRgb(hold[0], hold[1], hold[2]); + if ( state == LocalColorMap ) { + if ( ccount < lncols ) + localcmap[ccount] = rgb; + } else { + globalcmap[ccount] = rgb; + } + if (++ccount >= ncols) { + if ( state == LocalColorMap ) + state=TableImageLZWSize; + else + state=Introducer; + } + count=0; + } + break; + case Introducer: + hold[count++]=ch; + switch (ch) { + case ',': + state=ImageDescriptor; + break; + case '!': + state=ExtensionLabel; + break; + case ';': + if (consumer) { + if ( out_of_bounds ) // flush anything that survived + consumer->changed(QRect(0,0,swidth,sheight)); + consumer->end(); + } + state=Done; + break; + default: + digress=TRUE; + // Unexpected Introducer - ignore block + state=Error; + } + break; + case ImageDescriptor: + hold[count++]=ch; + if (count==10) { + int newleft=LM(hold[1], hold[2]); + int newtop=LM(hold[3], hold[4]); + int newwidth=LM(hold[5], hold[6]); + int newheight=LM(hold[7], hold[8]); + + // disbelieve ridiculous logical screen sizes, + // unless the image frames are also large. + if ( swidth/10 > QMAX(newwidth,200) ) + swidth = -1; + if ( sheight/10 > QMAX(newheight,200) ) + sheight = -1; + + if ( swidth <= 0 ) + swidth = newleft + newwidth; + if ( sheight <= 0 ) + sheight = newtop + newheight; + + if (img.isNull()) { + img.create(swidth, sheight, 32); + memset( img.bits(), 0, img.numBytes() ); + if (consumer) consumer->setSize(swidth, sheight); + } + img.setAlphaBuffer(trans_index >= 0); + line = (QRgb **)img.jumpTable(); + + disposePrevious( img, consumer ); + disposed = FALSE; + + left = newleft; + top = newtop; + width = newwidth; + height = newheight; + + right=QMAX( 0, QMIN(left+width, swidth)-1); + bottom=QMAX(0, QMIN(top+height, sheight)-1); + lcmap=!!(hold[9]&0x80); + interlace=!!(hold[9]&0x40); + //bool lcmsortflag=!!(hold[9]&0x20); + lncols=lcmap ? (2<<(hold[9]&0x7)) : 0; + if (lncols) { + if ( localcmap ) + delete [] localcmap; + localcmap = new QRgb[lncols+1]; + localcmap[lncols] = Q_TRANSPARENT; + ncols = lncols; + } else { + ncols = gncols; + } + frame++; + if ( frame == 0 ) { + if ( left || top || width<swidth || height<sheight ) { + // Not full-size image - erase with bg or transparent + if ( trans_index >= 0 ) { + fillRect(img, 0, 0, swidth, sheight, color(trans_index)); + if (consumer) consumer->changed(QRect(0,0,swidth,sheight)); + } else if ( bgcol>=0 ) { + fillRect(img, 0, 0, swidth, sheight, color(bgcol)); + if (consumer) consumer->changed(QRect(0,0,swidth,sheight)); + } + } + } + + if ( disposal == RestoreImage ) { + int l = QMIN(swidth-1,left); + int r = QMIN(swidth-1,right); + int t = QMIN(sheight-1,top); + int b = QMIN(sheight-1,bottom); + int w = r-l+1; + int h = b-t+1; + + if (backingstore.width() < w + || backingstore.height() < h) { + // We just use the backing store as a byte array + backingstore.create( QMAX(backingstore.width(), w), + QMAX(backingstore.height(), h), + 32); + memset( img.bits(), 0, img.numBytes() ); + } + for (int ln=0; ln<h; ln++) { + memcpy(backingstore.scanLine(ln), + line[t+ln]+l, w*sizeof(QRgb)); + } + } + + count=0; + if (lcmap) { + ccount=0; + state=LocalColorMap; + } else { + state=TableImageLZWSize; + } + x = left; + y = top; + accum = 0; + bitcount = 0; + sp = stack; + firstcode = oldcode = 0; + needfirst = FALSE; + out_of_bounds = left>=swidth || y>=sheight; + } + break; + case TableImageLZWSize: { + lzwsize=ch; + if ( lzwsize > max_lzw_bits ) { + state=Error; + } else { + code_size=lzwsize+1; + clear_code=1<<lzwsize; + end_code=clear_code+1; + max_code_size=2*clear_code; + max_code=clear_code+2; + int i; + for (i=0; i<clear_code && i<(1<<max_lzw_bits); i++) { + table[0][i]=0; + table[1][i]=i; + } + for (i=clear_code; i<(1<<max_lzw_bits); i++) { + table[0][i]=table[1][i]=0; + } + state=ImageDataBlockSize; + } + count=0; + break; + } case ImageDataBlockSize: + expectcount=ch; + if (expectcount) { + state=ImageDataBlock; + } else { + if (consumer) { + consumer->frameDone(); + digress = TRUE; + } + + state=Introducer; + } + break; + case ImageDataBlock: + count++; + accum|=(ch<<bitcount); + bitcount+=8; + while (bitcount>=code_size && state==ImageDataBlock) { + int code=accum&((1<<code_size)-1); + bitcount-=code_size; + accum>>=code_size; + + if (code==clear_code) { + if (!needfirst) { + int i; + code_size=lzwsize+1; + max_code_size=2*clear_code; + max_code=clear_code+2; + for (i=0; i<clear_code; i++) { + table[0][i]=0; + table[1][i]=i; + } + for (i=clear_code; i<(1<<max_lzw_bits); i++) { + table[0][i]=table[1][i]=0; + } + } + needfirst=TRUE; + } else if (code==end_code) { + bitcount = -32768; + // Left the block end arrive + } else { + if (needfirst) { + firstcode=oldcode=code; + if (!out_of_bounds && line && firstcode!=trans_index) + line[y][x] = color(firstcode); + x++; + if (x>=swidth) out_of_bounds = TRUE; + needfirst=FALSE; + if (x>=left+width) { + x=left; + out_of_bounds = left>=swidth || y>=sheight; + nextY(img,consumer); + } + } else { + incode=code; + if (code>=max_code) { + *sp++=firstcode; + code=oldcode; + } + while (code>=clear_code) { + *sp++=table[1][code]; + if (code==table[0][code]) { + state=Error; + break; + } + if (sp-stack>=(1<<(max_lzw_bits))*2) { + state=Error; + break; + } + code=table[0][code]; + } + *sp++=firstcode=table[1][code]; + code=max_code; + if (code<(1<<max_lzw_bits)) { + table[0][code]=oldcode; + table[1][code]=firstcode; + max_code++; + if ((max_code>=max_code_size) + && (max_code_size<(1<<max_lzw_bits))) + { + max_code_size*=2; + code_size++; + } + } + oldcode=incode; + while (sp>stack) { + --sp; + if (!out_of_bounds && line && *sp!=trans_index) + line[y][x] = color(*sp); + x++; + if (x>=swidth) out_of_bounds = TRUE; + if (x>=left+width) { + x=left; + out_of_bounds = left>=swidth || y>=sheight; + nextY(img,consumer); + } + } + } + } + } + if (count==expectcount) { + count=0; + state=ImageDataBlockSize; + } + break; + case ExtensionLabel: + switch (ch) { + case 0xf9: + state=GraphicControlExtension; + break; + case 0xff: + state=ApplicationExtension; + break; +#if 0 + case 0xfe: + state=CommentExtension; + break; + case 0x01: + break; +#endif + default: + state=SkipBlockSize; + } + count=0; + break; + case ApplicationExtension: + if (count<11) hold[count]=ch; + count++; + if (count==hold[0]+1) { + if (qstrncmp((char*)(hold+1), "NETSCAPE", 8)==0) { + // Looping extension + state=NetscapeExtensionBlockSize; + } else { + state=SkipBlockSize; + } + count=0; + } + break; + case NetscapeExtensionBlockSize: + expectcount=ch; + count=0; + if (expectcount) state=NetscapeExtensionBlock; + else state=Introducer; + break; + case NetscapeExtensionBlock: + if (count<3) hold[count]=ch; + count++; + if (count==expectcount) { + int loop = hold[0]+hold[1]*256; + if (consumer) consumer->setLooping(loop); + state=SkipBlockSize; // Ignore further blocks + } + break; + case GraphicControlExtension: + if (count<5) hold[count]=ch; + count++; + if (count==hold[0]+1) { + disposePrevious( img, consumer ); + disposal=Disposal((hold[1]>>2)&0x7); + //UNUSED: waitforuser=!!((hold[1]>>1)&0x1); + int delay=count>3 ? LM(hold[2], hold[3]) : 1; + // IE and mozilla use a minimum delay of 10. With the minumum delay of 10 + // we are compatible to them and avoid huge loads on the app and xserver. + if ( delay < 10 ) + delay = 10; + + bool havetrans=hold[1]&0x1; + trans_index = havetrans ? hold[4] : -1; + + if (consumer) consumer->setFramePeriod(delay*10); + count=0; + state=SkipBlockSize; + } + break; + case SkipBlockSize: + expectcount=ch; + count=0; + if (expectcount) state=SkipBlock; + else state=Introducer; + break; + case SkipBlock: + count++; + if (count==expectcount) state=SkipBlockSize; + break; + case Done: + digress=TRUE; + /* Netscape ignores the junk, so we do too. + length++; // Unget + state=Error; // More calls to this is an error + */ + break; + case Error: + return -1; // Called again after done. + } + } + return initial-length; +} + +void QGIFFormat::fillRect(QImage& img, int col, int row, int w, int h, QRgb color) +{ + if (w>0) { + QRgb** line = (QRgb **)img.jumpTable() + row; + for (int j=0; j<h; j++) { + for ( int i=0; i<w; i++ ) { + *(line[j]+col+i) = color; + } + } + } +} + +void QGIFFormat::nextY(QImage& img, QImageConsumer* consumer) +{ + int my; + switch (interlace) { + case 0: + // Non-interlaced + if (consumer && !out_of_bounds) + consumer->changed(QRect(left, y, right-left+1, 1)); + y++; + break; + case 1: + { + int i; + my = QMIN(7, bottom-y); + if ( trans_index < 0 ) // Don't dup with transparency + for (i=1; i<=my; i++) + memcpy(img.scanLine(y+i)+left, img.scanLine(y)+left, + (right-left+1)*sizeof(QRgb)); + if (consumer && !out_of_bounds) + consumer->changed(QRect(left, y, right-left+1, my+1)); + y+=8; + if (y>bottom) { + interlace++; y=top+4; + if (y > bottom) { // for really broken GIFs with bottom < 5 + interlace=2; + y = top + 2; + if (y > bottom) { // for really broken GIF with bottom < 3 + interlace = 0; + y = top + 1; + } + } + } + } break; + case 2: + { + int i; + my = QMIN(3, bottom-y); + if ( trans_index < 0 ) // Don't dup with transparency + for (i=1; i<=my; i++) + memcpy(img.scanLine(y+i)+left, img.scanLine(y)+left, + (right-left+1)*sizeof(QRgb)); + if (consumer && !out_of_bounds) + consumer->changed(QRect(left, y, right-left+1, my+1)); + y+=8; + if (y>bottom) { + interlace++; y=top+2; + if (y > bottom) { // for really broken GIF with bottom < 3 + interlace = 3; + y = top + 1; + } + } + } break; + case 3: + { + int i; + my = QMIN(1, bottom-y); + if ( trans_index < 0 ) // Don't dup with transparency + for (i=1; i<=my; i++) + memcpy(img.scanLine(y+i)+left, img.scanLine(y)+left, + (right-left+1)*sizeof(QRgb)); + if (consumer && !out_of_bounds) + consumer->changed(QRect(left, y, right-left+1, my+1)); + y+=4; + if (y>bottom) { interlace++; y=top+1; } + } break; + case 4: + if (consumer && !out_of_bounds) + consumer->changed(QRect(left, y, right-left+1, 1)); + y+=2; + } + + // Consume bogus extra lines + if (y >= sheight) out_of_bounds=TRUE; //y=bottom; +} + +QRgb QGIFFormat::color( uchar index ) const +{ + if ( index == trans_index || index > ncols ) + return Q_TRANSPARENT; + QRgb *map = lcmap ? localcmap : globalcmap; + return map ? map[index] : 0; +} + + + +#endif // QT_BUILTIN_GIF_READER + +#endif // QT_NO_ASYNC_IMAGE_IO diff --git a/src/kernel/qasyncimageio.h b/src/kernel/qasyncimageio.h new file mode 100644 index 0000000..2d4c37b --- /dev/null +++ b/src/kernel/qasyncimageio.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Definition of asynchronous image/movie loading classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QASYNCIMAGEIO_H +#define QASYNCIMAGEIO_H + +#ifndef QT_H +#include "qimage.h" +#endif // QT_H + +#ifndef QT_NO_ASYNC_IMAGE_IO + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +class Q_EXPORT QImageConsumer { +public: + virtual void end()=0; + + // Change transfer type 1. + virtual void changed( const QRect& ) = 0; + virtual void frameDone() = 0; + + // Change transfer type 2. + virtual void frameDone( const QPoint&, const QRect& ) = 0; + + virtual void setLooping( int ) = 0; + virtual void setFramePeriod( int ) = 0; + virtual void setSize( int, int ) = 0; +}; + +class Q_EXPORT QImageFormat { +public: + virtual ~QImageFormat(); + virtual int decode( QImage& img, QImageConsumer* consumer, + const uchar* buffer, int length ) = 0; +}; + +class Q_EXPORT QImageFormatType { +public: + virtual ~QImageFormatType(); + virtual QImageFormat* decoderFor( const uchar* buffer, int length ) = 0; + virtual const char* formatName() const = 0; +protected: + QImageFormatType(); +}; + +class QImageDecoderPrivate; +class Q_EXPORT QImageDecoder { +public: + QImageDecoder( QImageConsumer* c ); + ~QImageDecoder(); + + const QImage& image() { return img; } + int decode( const uchar* buffer, int length ); + + static const char* formatName( const uchar* buffer, int length ); + static QImageFormatType* format( const char* name ); // direct use - no decode() + + static QStrList inputFormats(); + static void registerDecoderFactory( QImageFormatType* ); + static void unregisterDecoderFactory( QImageFormatType* ); + +private: + QImageFormat* actual_decoder; + QImageConsumer* consumer; + QImage img; + QImageDecoderPrivate *d; +}; + +#endif // QT_NO_ASYNC_IMAGE_IO + +#endif // QASYNCIMAGEIO_H diff --git a/src/kernel/qasyncio.cpp b/src/kernel/qasyncio.cpp new file mode 100644 index 0000000..b29dfa6 --- /dev/null +++ b/src/kernel/qasyncio.cpp @@ -0,0 +1,360 @@ +/**************************************************************************** +** +** Implementation of asynchronous I/O classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qasyncio.h" +#include "qiodevice.h" +#include <stdlib.h> + +#ifndef QT_NO_ASYNC_IO + +/*! + \class QAsyncIO qasyncio.h + \obsolete + \brief The QAsyncIO class encapsulates I/O asynchronicity. + + The Qt classes for asynchronous input/output provide a simple + mechanism to allow large files or slow data sources to be processed + without using large amounts of memory or blocking the user interface. + + This facility is used in Qt to drive animated images. See QImageConsumer. +*/ + + +/*! + Destroys the async IO object. +*/ +QAsyncIO::~QAsyncIO() +{ +} + +/*! + Ensures that only one object, \a obj and function, \a member, can + respond to changes in readiness. +*/ +void QAsyncIO::connect(QObject* obj, const char *member) +{ + signal.disconnect(0, 0); + signal.connect(obj, member); +} + +/*! + Derived classes should call this when they change from being + unready to ready. +*/ +void QAsyncIO::ready() +{ + signal.activate(); +} + + + +/*! + \class QDataSink qasyncio.h + \obsolete + \brief The QDataSink class is an asynchronous consumer of data. + + A data sink is an object which receives data from some source in an + asynchronous manner. This means that at some time not determined by + the data sink, blocks of data are given to it from processing. The + data sink is able to limit the maximum size of such blocks which it + is currently able to process. + + \sa QAsyncIO, QDataSource, QDataPump +*/ + +/*! + \fn int QDataSink::readyToReceive() + + The data sink should return a value indicating how much data it is ready + to consume. This may be 0. +*/ + +/*! + This should be called whenever readyToReceive() might have become non-zero. + It is merely calls QAsyncIO::ready() if readyToReceive() is non-zero. +*/ +void QDataSink::maybeReady() +{ + if (readyToReceive()) ready(); +} + +/*! + \fn void QDataSink::receive(const uchar*, int count) + + This function is called to provide data for the data sink. The \a count + will be no more than the amount indicated by the most recent call to + readyToReceive(). The sink must use all the provided data. +*/ + +/*! + \fn void QDataSink::eof() + + This function will be called when no more data is available for + processing. +*/ + + +/*! + \class QDataSource qasyncio.h + \obsolete + \brief The QDataSource class is an asynchronous producer of data. + + A data source is an object which provides data from some source in an + asynchronous manner. This means that at some time not determined by + the data source, blocks of data will be taken from it for processing. + The data source is able to limit the maximum size of such blocks which + it is currently able to provide. + + \sa QAsyncIO, QDataSink, QDataPump +*/ + +/*! + \fn int QDataSource::readyToSend() + + The data source should return a value indicating how much data it is ready + to provide. This may be 0. If the data source knows it will never be + able to provide any more data (until after a rewind()), it may return -1. +*/ + +/*! + This should be called whenever readyToSend() might have become non-zero. + It is merely calls QAsyncIO::ready() if readyToSend() is non-zero. +*/ +void QDataSource::maybeReady() +{ + if (readyToSend()) ready(); +} + +/*! + \fn void QDataSource::sendTo(QDataSink*, int count) + + This function is called to extract data from the source, by sending + it to the given data sink. The \a count will be no more than the amount + indicated by the most recent call to readyToSend(). The source must + use all the provided data, and the sink will be prepared to accept at + least this much data. +*/ + +/*! + This function should return TRUE if the data source can be rewound. + + The default returns FALSE. +*/ +bool QDataSource::rewindable() const +{ + return FALSE; +} + +/*! + If this function is called with \a on set to TRUE, and rewindable() + is TRUE, then the data source must take measures to allow the rewind() + function to subsequently operate as described. If rewindable() is FALSE, + the function should call QDataSource::enableRewind(), which aborts with + a qFatal() error. + + For example, a network connection may choose to use a disk cache + of input only if rewinding is enabled before the first buffer-full of + data is discarded, returning FALSE in rewindable() if that first buffer + is discarded. +*/ +void QDataSource::enableRewind( bool /* on */ ) +{ + qFatal( "Attempted to make unrewindable QDataSource rewindable" ); +} + +/*! + This function rewinds the data source. This may only be called if + enableRewind(TRUE) has been previously called. +*/ +void QDataSource::rewind() +{ + qFatal("Attempted to rewind unrewindable QDataSource"); +} + +/*! + \class QIODeviceSource qasyncio.h + \obsolete + \brief The QIODeviceSource class is a QDataSource that draws data from a QIODevice. + + This class encapsulates retrieving data from a QIODevice (such as a QFile). +*/ + +/*! + Constructs a QIODeviceSource from the QIODevice \a device. The QIODevice + \e must be dynamically allocated, becomes owned by the QIODeviceSource, + and will be deleted when the QIODeviceSource is destroyed. \a buffer_size + determines the size of buffering to use between asynchronous operations. + The higher the \a buffer_size, the more efficient, but the less interleaved + the operation will be with other processing. +*/ +QIODeviceSource::QIODeviceSource(QIODevice* device, int buffer_size) : + buf_size(buffer_size), + buffer(new uchar[buf_size]), + iod(device), + rew(FALSE) +{ +} + +/*! + Destroys the QIODeviceSource, deleting the QIODevice from which it was + constructed. +*/ +QIODeviceSource::~QIODeviceSource() +{ + delete iod; + delete [] buffer; +} + +/*! + Ready until end-of-file. +*/ +int QIODeviceSource::readyToSend() +{ + if ( iod->status() != IO_Ok || !(iod->state() & IO_Open) ) + return -1; + + int n = QMIN((uint)buf_size, iod->size()-iod->at()); // ### not 64-bit safe + // ### not large file safe + return n ? n : -1; +} + +/*! + Reads a block of data and sends up to \a n bytes to the \a sink. +*/ +void QIODeviceSource::sendTo(QDataSink* sink, int n) +{ + iod->readBlock((char*)buffer, n); + sink->receive(buffer, n); +} + +/*! + All QIODeviceSource's are rewindable. +*/ +bool QIODeviceSource::rewindable() const +{ + return TRUE; +} + +/*! + If \a on is set to TRUE then rewinding is enabled. + No special action is taken. If \a on is set to + FALSE then rewinding is disabled. +*/ +void QIODeviceSource::enableRewind(bool on) +{ + rew = on; +} + +/*! + Calls reset() on the QIODevice. +*/ +void QIODeviceSource::rewind() +{ + if (!rew) { + QDataSource::rewind(); + } else { + iod->reset(); + ready(); + } +} + + +/*! + \class QDataPump qasyncio.h + \obsolete + \brief The QDataPump class moves data from a QDataSource to a QDataSink during event processing. + + For a QDataSource to provide data to a QDataSink, a controller must exist + to examine the QDataSource::readyToSend() and QDataSink::readyToReceive() + methods and respond to the QASyncIO::activate() signal of the source and + sink. One very useful way to do this is interleaved with other event + processing. QDataPump provides this - create a pipe between a source + and a sink, and data will be moved during subsequent event processing. + + Note that each source can only provide data to one sink and each sink + can only receive data from one source (although it is quite possible + to write a multiplexing sink that is multiple sources). +*/ + +/*! + Constructs a QDataPump to move data from a given \a data_source + to a given \a data_sink. +*/ +QDataPump::QDataPump(QDataSource* data_source, QDataSink* data_sink) : + source(data_source), sink(data_sink) +{ + source->connect(this, SLOT(kickStart())); + sink->connect(this, SLOT(kickStart())); + connect(&timer, SIGNAL(timeout()), this, SLOT(tryToPump())); + timer.start(0, TRUE); +} + +void QDataPump::kickStart() +{ + if (!timer.isActive()) { + interval = 0; + timer.start(0, TRUE); + } +} + +void QDataPump::tryToPump() +{ + int supply, demand; + + supply = source->readyToSend(); + demand = sink->readyToReceive(); + if (demand <= 0) { + return; + } + interval = 0; + if (supply < 0) { + // All done (until source signals change in readiness) + sink->eof(); + return; + } + if (!supply) + return; + source->sendTo(sink, QMIN(supply, demand)); + + timer.start(0, TRUE); +} + +#endif // QT_NO_ASYNC_IO + diff --git a/src/kernel/qasyncio.h b/src/kernel/qasyncio.h new file mode 100644 index 0000000..c203d8d --- /dev/null +++ b/src/kernel/qasyncio.h @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Definition of asynchronous I/O classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QASYNCIO_H +#define QASYNCIO_H + +#ifndef QT_H +#include "qobject.h" +#include "qsignal.h" +#include "qtimer.h" +#endif // QT_H + +#ifndef QT_NO_ASYNC_IO + +class QIODevice; + +class Q_EXPORT QAsyncIO { +public: + virtual ~QAsyncIO(); + void connect(QObject*, const char *member); + +protected: + void ready(); + +private: + QSignal signal; +}; + +class Q_EXPORT QDataSink : public QAsyncIO { +public: + // Call this to know how much I can take. + virtual int readyToReceive()=0; + virtual void receive(const uchar*, int count)=0; + virtual void eof()=0; + void maybeReady(); +}; + +class Q_EXPORT QDataSource : public QAsyncIO { +public: + virtual int readyToSend()=0; // returns -1 when never any more ready + virtual void sendTo(QDataSink*, int count)=0; + void maybeReady(); + + virtual bool rewindable() const; + virtual void enableRewind(bool); + virtual void rewind(); +}; + +class Q_EXPORT QIODeviceSource : public QDataSource { + const int buf_size; + uchar *buffer; + QIODevice* iod; + bool rew; + +public: + QIODeviceSource(QIODevice*, int bufsize=4096); + ~QIODeviceSource(); + + int readyToSend(); + void sendTo(QDataSink* sink, int n); + bool rewindable() const; + void enableRewind(bool on); + void rewind(); +}; + +class Q_EXPORT QDataPump : public QObject { + Q_OBJECT + int interval; + QTimer timer; + QDataSource* source; + QDataSink* sink; + +public: + QDataPump(QDataSource*, QDataSink*); + +private slots: + void kickStart(); + void tryToPump(); +}; + +#endif // QT_NO_ASYNC_IO + +#endif diff --git a/src/kernel/qbitmap.cpp b/src/kernel/qbitmap.cpp new file mode 100644 index 0000000..f8b87c8 --- /dev/null +++ b/src/kernel/qbitmap.cpp @@ -0,0 +1,304 @@ +/**************************************************************************** +** +** Implementation of QBitmap class +** +** Created : 941020 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qbitmap.h" +#include "qimage.h" + + +/*! + \class QBitmap qbitmap.h + \brief The QBitmap class provides monochrome (1-bit depth) pixmaps. + + \ingroup graphics + \ingroup images + \ingroup shared + + The QBitmap class is a monochrome off-screen paint device used + mainly for creating custom QCursor and QBrush objects, in + QPixmap::setMask() and for QRegion. + + A QBitmap is a QPixmap with a \link QPixmap::depth() depth\endlink + of 1. If a pixmap with a depth greater than 1 is assigned to a + bitmap, the bitmap will be dithered automatically. A QBitmap is + guaranteed to always have the depth 1, unless it is + QPixmap::isNull() which has depth 0. + + When drawing in a QBitmap (or QPixmap with depth 1), we recommend + using the QColor objects \c Qt::color0 and \c Qt::color1. + Painting with \c color0 sets the bitmap bits to 0, and painting + with \c color1 sets the bits to 1. For a bitmap, 0-bits indicate + background (or transparent) and 1-bits indicate foreground (or + opaque). Using the \c black and \c white QColor objects make no + sense because the QColor::pixel() value is not necessarily 0 for + black and 1 for white. + + The QBitmap can be transformed (translated, scaled, sheared or + rotated) using xForm(). + + Just like the QPixmap class, QBitmap is optimized by the use of + \link shclass.html implicit sharing\endlink, so it is very + efficient to pass QBitmap objects as arguments. + + \sa QPixmap, QPainter::drawPixmap(), bitBlt(), \link shclass.html Shared Classes\endlink +*/ + + +/*! + Constructs a null bitmap. + + \sa QPixmap::isNull() +*/ + +QBitmap::QBitmap() +{ + data->bitmap = TRUE; +} + + +/*! + Constructs a bitmap with width \a w and height \a h. + + The contents of the bitmap is uninitialized if \a clear is FALSE; + otherwise it is filled with pixel value 0 (the QColor \c + Qt::color0). + + The optional \a optimization argument specifies the optimization + setting for the bitmap. The default optimization should be used in + most cases. Games and other pixmap-intensive applications may + benefit from setting this argument; see \l{QPixmap::Optimization}. + + \sa QPixmap::setOptimization(), QPixmap::setDefaultOptimization() +*/ + +QBitmap::QBitmap( int w, int h, bool clear, + QPixmap::Optimization optimization ) + : QPixmap( w, h, 1, optimization ) +{ + data->bitmap = TRUE; + if ( clear ) + fill( Qt::color0 ); +} + + +/*! + \overload + + Constructs a bitmap with the size \a size. + + The contents of the bitmap is uninitialized if \a clear is FALSE; + otherwise it is filled with pixel value 0 (the QColor \c + Qt::color0). + + The optional \a optimization argument specifies the optimization + setting for the bitmap. The default optimization should be used in + most cases. Games and other pixmap-intensive applications may + benefit from setting this argument; see \l{QPixmap::Optimization}. +*/ + +QBitmap::QBitmap( const QSize &size, bool clear, + QPixmap::Optimization optimization ) + : QPixmap( size, 1, optimization ) +{ + data->bitmap = TRUE; + if ( clear ) + fill( Qt::color0 ); +} + + +/*! + Constructs a bitmap with width \a w and height \a h and sets the + contents to \a bits. + + The \a isXbitmap flag should be TRUE if \a bits was generated by + the X11 bitmap program. The X bitmap bit order is little endian. + The QImage documentation discusses bit order of monochrome images. + + Example (creates an arrow bitmap): + \code + uchar arrow_bits[] = { 0x3f, 0x1f, 0x0f, 0x1f, 0x3b, 0x71, 0xe0, 0xc0 }; + QBitmap bm( 8, 8, arrow_bits, TRUE ); + \endcode +*/ + +QBitmap::QBitmap( int w, int h, const uchar *bits, bool isXbitmap ) + : QPixmap( w, h, bits, isXbitmap ) +{ + data->bitmap = TRUE; +} + + +/*! + \overload + + Constructs a bitmap with the size \a size and sets the contents to + \a bits. + + The \a isXbitmap flag should be TRUE if \a bits was generated by + the X11 bitmap program. The X bitmap bit order is little endian. + The QImage documentation discusses bit order of monochrome images. +*/ + +QBitmap::QBitmap( const QSize &size, const uchar *bits, bool isXbitmap ) + : QPixmap( size.width(), size.height(), bits, isXbitmap ) +{ + data->bitmap = TRUE; +} + + +/*! + Constructs a bitmap that is a copy of \a bitmap. +*/ + +QBitmap::QBitmap( const QBitmap &bitmap ) + : QPixmap( bitmap ) +{ +} + +#ifndef QT_NO_IMAGEIO +/*! + Constructs a bitmap from the file \a fileName. If the file does + not exist or is of an unknown format, the bitmap becomes a null + bitmap. + + The parameters \a fileName and \a format are passed on to + QPixmap::load(). Dithering will be performed if the file format + uses more than 1 bit per pixel. + + \sa QPixmap::isNull(), QPixmap::load(), QPixmap::loadFromData(), + QPixmap::save(), QPixmap::imageFormat() +*/ + +QBitmap::QBitmap( const QString& fileName, const char *format ) + : QPixmap() // Will set bitmap to null bitmap, explicit call for clarity +{ + data->bitmap = TRUE; + load( fileName, format, Mono ); +} +#endif + +/*! + Assigns the bitmap \a bitmap to this bitmap and returns a + reference to this bitmap. +*/ + +QBitmap &QBitmap::operator=( const QBitmap &bitmap ) +{ + QPixmap::operator=(bitmap); +#if defined(QT_CHECK_STATE) + Q_ASSERT( data->bitmap ); +#endif + return *this; +} + + +/*! + \overload + + Assigns the pixmap \a pixmap to this bitmap and returns a + reference to this bitmap. + + Dithering will be performed if the pixmap has a QPixmap::depth() + greater than 1. +*/ + +QBitmap &QBitmap::operator=( const QPixmap &pixmap ) +{ + if ( pixmap.isNull() ) { // a null pixmap + QBitmap bm( 0, 0, FALSE, pixmap.optimization() ); + QBitmap::operator=(bm); + } else if ( pixmap.depth() == 1 ) { // 1-bit pixmap + if ( pixmap.isQBitmap() ) { // another QBitmap + QPixmap::operator=(pixmap); // shallow assignment + } else { // not a QBitmap, but 1-bit + QBitmap bm( pixmap.size(), FALSE, pixmap.optimization() ); + bitBlt( &bm, 0,0, &pixmap, 0,0,pixmap.width(),pixmap.height() ); + QBitmap::operator=(bm); + } + } else { // n-bit depth pixmap + QImage image; + image = pixmap; // convert pixmap to image + *this = image; // will dither image + } + return *this; +} + + +/*! + \overload + + Converts the image \a image to a bitmap and assigns the result to + this bitmap. Returns a reference to the bitmap. + + Dithering will be performed if the image has a QImage::depth() + greater than 1. +*/ + +QBitmap &QBitmap::operator=( const QImage &image ) +{ + convertFromImage( image ); + return *this; +} + + +#ifndef QT_NO_PIXMAP_TRANSFORMATION +/*! + Returns a transformed copy of this bitmap by using \a matrix. + + This function does exactly the same as QPixmap::xForm(), except + that it returns a QBitmap instead of a QPixmap. + + \sa QPixmap::xForm() +*/ + +QBitmap QBitmap::xForm( const QWMatrix &matrix ) const +{ + QPixmap pm = QPixmap::xForm( matrix ); + QBitmap bm; + // Here we fake the pixmap to think it's a QBitmap. With this trick, + // the QBitmap::operator=(const QPixmap&) will just refer the + // pm.data and we do not need to perform a bitBlt. + pm.data->bitmap = TRUE; + bm = pm; + return bm; +} +#endif // QT_NO_TRANSFORMATIONS + + + diff --git a/src/kernel/qbitmap.h b/src/kernel/qbitmap.h new file mode 100644 index 0000000..40fbf40 --- /dev/null +++ b/src/kernel/qbitmap.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Definition of QBitmap class +** +** Created : 941020 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QBITMAP_H +#define QBITMAP_H + +#ifndef QT_H +#include "qpixmap.h" +#endif // QT_H + + +class Q_EXPORT QBitmap : public QPixmap +{ +public: + QBitmap(); + QBitmap( int w, int h, bool clear = FALSE, + QPixmap::Optimization = QPixmap::DefaultOptim ); + QBitmap( const QSize &, bool clear = FALSE, + QPixmap::Optimization = QPixmap::DefaultOptim ); + QBitmap( int w, int h, const uchar *bits, bool isXbitmap=FALSE ); + QBitmap( const QSize &, const uchar *bits, bool isXbitmap=FALSE ); + QBitmap( const QBitmap & ); +#ifndef QT_NO_IMAGEIO + QBitmap( const QString &fileName, const char *format=0 ); +#endif + QBitmap &operator=( const QBitmap & ); + QBitmap &operator=( const QPixmap & ); + QBitmap &operator=( const QImage & ); + +#ifndef QT_NO_PIXMAP_TRANSFORMATION + QBitmap xForm( const QWMatrix & ) const; +#endif +}; + + +#endif // QBITMAP_H diff --git a/src/kernel/qbrush.h b/src/kernel/qbrush.h new file mode 100644 index 0000000..7b6bade --- /dev/null +++ b/src/kernel/qbrush.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Definition of QBrush class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QBRUSH_H +#define QBRUSH_H + +#ifndef QT_H +#include "qcolor.h" +#include "qshared.h" +#endif // QT_H + + +class Q_EXPORT QBrush: public Qt +{ +friend class QPainter; +public: + QBrush(); + QBrush( BrushStyle ); + QBrush( const QColor &, BrushStyle=SolidPattern ); + QBrush( const QColor &, const QPixmap & ); + QBrush( const QBrush & ); + ~QBrush(); + QBrush &operator=( const QBrush & ); + + BrushStyle style() const { return data->style; } + void setStyle( BrushStyle ); + const QColor &color()const { return data->color; } + void setColor( const QColor & ); + QPixmap *pixmap() const { return data->pixmap; } + void setPixmap( const QPixmap & ); + + bool operator==( const QBrush &p ) const; + bool operator!=( const QBrush &b ) const + { return !(operator==(b)); } + +private: + QBrush copy() const; + void detach(); + void init( const QColor &, BrushStyle ); + struct QBrushData : public QShared { // brush data + BrushStyle style; + QColor color; + QPixmap *pixmap; + } *data; +}; + + +/***************************************************************************** + QBrush stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QBrush & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QBrush & ); +#endif + +#endif // QBRUSH_H diff --git a/src/kernel/qclipboard.cpp b/src/kernel/qclipboard.cpp new file mode 100644 index 0000000..b02d1ec --- /dev/null +++ b/src/kernel/qclipboard.cpp @@ -0,0 +1,567 @@ +/**************************************************************************** +** +** Implementation of QClipboard class +** +** Created : 960430 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qclipboard.h" + +#ifndef QT_NO_CLIPBOARD + +#include "qapplication.h" +#include "qapplication_p.h" +#include "qdragobject.h" +#include "qpixmap.h" + +/*! + \class QClipboard qclipboard.h + \brief The QClipboard class provides access to the window system clipboard. + + \ingroup io + \ingroup environment + \mainclass + + The clipboard offers a simple mechanism to copy and paste data + between applications. + + QClipboard supports the same data types that QDragObject does, and + uses similar mechanisms. For advanced clipboard usage + read \link dnd.html the drag-and-drop documentation\endlink. + + There is a single QClipboard object in an application, and you can + access it using QApplication::clipboard(). + + Example: + \code + QClipboard *cb = QApplication::clipboard(); + + // Copy text from the clipboard (paste) + QString text = cb->text(QClipboard::Clipboard); + if ( !text.isNull() ) + qDebug( "The clipboard contains: " + text ); + + // Copy text into the clipboard + cb->setText( "This text can be pasted by other programs", + QClipboard::Clipboard ); + \endcode + + QClipboard features some convenience functions to access common data + types: setText() allows the exchange of Unicode text and + setPixmap() and setImage() allows the exchange of QPixmaps + and QImages between applications. The setData() function is the + ultimate in flexibility: it allows you to add any QMimeSource into the + clipboard. There are corresponding getters for each of these, e.g. + text(), image() and pixmap(). + + You can clear the clipboard by calling clear(). + + + \section1 Platform Specific Information + + \section2 X11 + + \list + + \i The X11 Window System has the concept of a separate selection + and clipboard. When text is selected, it is immediately available + as the global mouse selection. The global mouse selection may + later be copied to the clipboard. By convention, the middle mouse + button is used to paste the global mouse selection. + + \i X11 also has the concept of ownership; if you change the + selection within a window, X11 will only notify the owner and the + previous owner of the change, i.e. it will not notify all + applications that the selection or clipboard data changed. + + \i Lastly, the X11 clipboard is event driven, i.e. the clipboard + will not function properly if the event loop is not running. + Similarly, it is recommended that the contents of the clipboard + are stored or retrieved in direct response to user-input events, + e.g. mouse button or key presses and releases. You should not + store or retrieve the clipboard contents in response to timer or + non-user-input events. + + \endlist + + \section2 Windows + + \list + + \i Microsoft Windows does not support the global mouse selection; + it only supports the global clipboard, e.g. Windows only adds text + to the clipboard when an explicit copy or cut is made. + + \i Windows does not have the concept of ownership; the clipboard + is a fully global resource so all applications are notified of + changes. + + \endlist + + See the multiclip example in the \e{Qt Designer} examples + directory for an example of a multiplatform clipboard application + that also demonstrates selection handling. +*/ + + +/*! + \internal + + Constructs a clipboard object. + + Do not call this function. + + Call QApplication::clipboard() instead to get a pointer to the + application's global clipboard object. + + There is only one clipboard in the window system, and creating + more than one object to represent it is almost certainly an error. +*/ + +QClipboard::QClipboard( QObject *parent, const char *name ) + : QObject( parent, name ) +{ + // nothing +} + +#ifndef Q_WS_WIN32 +/*! + \internal + + Destroys the clipboard. + + You should never delete the clipboard. QApplication will do this + when the application terminates. +*/ +QClipboard::~QClipboard() +{ +} +#endif + +/*! + \fn void QClipboard::dataChanged() + + This signal is emitted when the clipboard data is changed. +*/ + +/*! + \fn void QClipboard::selectionChanged() + + This signal is emitted when the selection is changed. This only + applies to windowing systems that support selections, e.g. X11. + Windows doesn't support selections. +*/ + +/*! \enum QClipboard::Mode + \keyword clipboard mode + + This enum type is used to control which part of the system clipboard is + used by QClipboard::data(), QClipboard::setData() and related functions. + + \value Clipboard indicates that data should be stored and retrieved from + the global clipboard. + + \value Selection indicates that data should be stored and retrieved from + the global mouse selection. + + \e Note: Support for \c Selection is provided only on systems with a + global mouse selection (e.g. X11). + + \sa QClipboard::supportsSelection() +*/ + + +/***************************************************************************** + QApplication member functions related to QClipboard. + *****************************************************************************/ + +#ifndef QT_NO_MIMECLIPBOARD +// text handling is done directly in qclipboard_qws, for now + +/*! + \overload + + Returns the clipboard text in subtype \a subtype, or a null string + if the clipboard does not contain any text. If \a subtype is null, + any subtype is acceptable, and \a subtype is set to the chosen + subtype. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + text is retrieved from the global clipboard. If \a mode is + QClipboard::Selection, the text is retrieved from the global + mouse selection. + + Common values for \a subtype are "plain" and "html". + + \sa setText(), data(), QString::operator!() +*/ +QString QClipboard::text( QCString &subtype, Mode mode ) const +{ + QString r; + QTextDrag::decode( data( mode ) ,r, subtype ); + return r; +} + +/*! + \overload + + Returns the clipboard text in subtype \a subtype, or a null string + if the clipboard does not contain any text. This function uses the + QClipboard::text() function which takes a QClipboard::Mode + argument. The value of the mode argument is determined by the + return value of selectionModeEnabled(). If selectionModeEnabled() + returns TRUE, the mode argument is QClipboard::Selection, + otherwise the mode argument is QClipboard::Clipboard. +*/ +// ### remove 4.0 +QString QClipboard::text( QCString& subtype ) const +{ + return text( subtype, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Returns the clipboard text as plain text, or a null string if the + clipboard does not contain any text. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + text is retrieved from the global clipboard. If \a mode is + QClipboard::Selection, the text is retrieved from the global + mouse selection. + + \sa setText(), data(), QString::operator!() +*/ +QString QClipboard::text( Mode mode ) const +{ + QCString subtype = "plain"; + return text( subtype, mode ); +} + +/*! + \overload + + This function uses the QClipboard::text() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ + +QString QClipboard::text() const +{ + return text( selectionModeEnabled() ? Selection : Clipboard ); +} + + /*! + Copies \a text into the clipboard as plain text. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + text is stored in the global clipboard. If \a mode is + QClipboard::Selection, the text is stored in the global + mouse selection. + + \sa text(), setData() +*/ + +void QClipboard::setText( const QString &text, Mode mode ) +{ + setData( new QTextDrag(text), mode ); +} + +/*! + \overload + + This function uses the QClipboard::setText() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +void QClipboard::setText( const QString &text ) +{ + setText( text, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Returns the clipboard image, or returns a null image if the + clipboard does not contain an image or if it contains an image in + an unsupported image format. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + image is retrieved from the global clipboard. If \a mode is + QClipboard::Selection, the image is retrieved from the global + mouse selection. + + \sa setImage() pixmap() data(), QImage::isNull() +*/ +QImage QClipboard::image( Mode mode ) const +{ + QImage r; + QImageDrag::decode( data( mode ), r ); + return r; +} + +/*! + \overload + + This function uses the QClipboard::image() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +QImage QClipboard::image() const +{ + return image( selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Copies \a image into the clipboard. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + image is stored in the global clipboard. If \a mode is + QClipboard::Selection, the data is stored in the global + mouse selection. + + This is shorthand for: + \code + setData( new QImageDrag(image), mode ) + \endcode + + \sa image(), setPixmap() setData() +*/ +void QClipboard::setImage( const QImage &image, Mode mode ) +{ + setData( new QImageDrag( image ), mode ); +} + +/*! + \overload + + This function uses the QClipboard::setImage() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +void QClipboard::setImage( const QImage &image ) +{ + setImage( image, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Returns the clipboard pixmap, or null if the clipboard does not + contain a pixmap. Note that this can lose information. For + example, if the image is 24-bit and the display is 8-bit, the + result is converted to 8 bits, and if the image has an alpha + channel, the result just has a mask. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + pixmap is retrieved from the global clipboard. If \a mode is + QClipboard::Selection, the pixmap is retrieved from the global + mouse selection. + + \sa setPixmap() image() data() QPixmap::convertFromImage(). +*/ +QPixmap QClipboard::pixmap( Mode mode ) const +{ + QPixmap r; + QImageDrag::decode( data( mode ), r ); + return r; +} + +/*! + \overload + + This function uses the QClipboard::pixmap() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +QPixmap QClipboard::pixmap() const +{ + return pixmap( selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Copies \a pixmap into the clipboard. Note that this is slower + than setImage() because it needs to convert the QPixmap to a + QImage first. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + pixmap is stored in the global clipboard. If \a mode is + QClipboard::Selection, the pixmap is stored in the global + mouse selection. + + \sa pixmap() setImage() setData() +*/ +void QClipboard::setPixmap( const QPixmap &pixmap, Mode mode ) +{ + // *could* just use the handle, but that is X hackery, MIME is better. + setData( new QImageDrag( pixmap.convertToImage() ), mode ); +} + +/*! + \overload + + This function uses the QClipboard::setPixmap() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +void QClipboard::setPixmap( const QPixmap &pixmap ) +{ + setPixmap( pixmap, selectionModeEnabled() ? Selection : Clipboard ); +} + + +/*! \fn QMimeSource *QClipboard::data( Mode mode ) const + Returns a reference to a QMimeSource representation of the current + clipboard data. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + data is retrieved from the global clipboard. If \a mode is + QClipboard::Selection, the data is retrieved from the global + mouse selection. + + \sa setData() +*/ + +/*! + \overload + + This function uses the QClipboard::data() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +QMimeSource *QClipboard::data() const +{ + return data( selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! \fn void QClipboard::setData( QMimeSource *src, Mode mode ) + Sets the clipboard data to \a src. Ownership of the data is + transferred to the clipboard. If you want to remove the data + either call clear() or call setData() again with new data. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, the + data is retrieved from the global clipboard. If \a mode is + QClipboard::Selection, the data is retrieved from the global + mouse selection. + + The QDragObject subclasses are reasonable objects to put into the + clipboard (but do not try to call QDragObject::drag() on the same + object). Any QDragObject placed in the clipboard should have a + parent of 0. Do not put QDragMoveEvent or QDropEvent subclasses in + the clipboard, as they do not belong to the event handler which + receives them. + + The setText(), setImage() and setPixmap() functions are simpler + wrappers for setting text, image and pixmap data respectively. + + \sa data() +*/ + +/*! + \overload + + This function uses the QClipboard::setData() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is + QClipboard::Clipboard. +*/ +// ### remove 4.0 +void QClipboard::setData( QMimeSource *src ) +{ + setData( src, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! \fn void QClipboard::clear( Mode mode ) + Clear the clipboard contents. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is QClipboard::Clipboard, this + function clears the the global clipboard contents. If \a mode is + QClipboard::Selection, this function clears the global mouse + selection contents. + + \sa QClipboard::Mode, supportsSelection() +*/ + +/*! + \overload + + This function uses the QClipboard::clear() function which takes + a QClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + QClipboard::Selection, otherwise the mode argument is QClipboard::Clipboard. +*/ +// ### remove 4.0 +void QClipboard::clear() +{ + clear( selectionModeEnabled() ? Selection : Clipboard ); +} + +#endif // QT_NO_MIMECLIPBOARD +#endif // QT_NO_CLIPBOARD diff --git a/src/kernel/qclipboard.h b/src/kernel/qclipboard.h new file mode 100644 index 0000000..ef84412 --- /dev/null +++ b/src/kernel/qclipboard.h @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Definition of QClipboard class +** +** Created : 960430 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QCLIPBOARD_H +#define QCLIPBOARD_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_CLIPBOARD + +class QMimeSource; + +class Q_EXPORT QClipboard : public QObject +{ + Q_OBJECT +private: + QClipboard( QObject *parent=0, const char *name=0 ); + ~QClipboard(); + +public: + enum Mode { Clipboard, Selection }; + + void clear( Mode mode ); // ### default arg = Clipboard in 4.0 + void clear(); // ### remove 4.0 + + bool supportsSelection() const; + bool ownsSelection() const; + bool ownsClipboard() const; + + void setSelectionMode(bool enable); // ### remove 4.0 + bool selectionModeEnabled() const; // ### remove 4.0 + + // ### default arg mode = Clipboard in 4.0 for all of these + QString text( Mode mode ) const; + QString text( QCString& subtype, Mode mode ) const; + void setText( const QString &, Mode mode ); + +#ifndef QT_NO_MIMECLIPBOARD + QMimeSource *data( Mode mode ) const; + void setData( QMimeSource*, Mode mode ); + + QImage image( Mode mode ) const; + QPixmap pixmap( Mode mode ) const; + void setImage( const QImage &, Mode mode ); + void setPixmap( const QPixmap &, Mode mode ); +#endif + + // ### remove all of these in 4.0 + QString text() const; + QString text(QCString& subtype) const; + void setText( const QString &); + +#ifndef QT_NO_MIMECLIPBOARD + QMimeSource *data() const; + void setData( QMimeSource* ); + + QImage image() const; + QPixmap pixmap() const; + void setImage( const QImage & ); + void setPixmap( const QPixmap & ); +#endif + +signals: + void selectionChanged(); + void dataChanged(); + +private slots: + void ownerDestroyed(); + +protected: + void connectNotify( const char * ); + bool event( QEvent * ); + + friend class QApplication; + friend class QBaseApplication; + friend class QDragManager; + friend class QMimeSource; + +private: +#if defined(Q_WS_MAC) + void loadScrap(bool convert); + void saveScrap(); +#endif + + // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QClipboard( const QClipboard & ); + QClipboard &operator=( const QClipboard & ); +#endif +}; + +#endif // QT_NO_CLIPBOARD + +#endif // QCLIPBOARD_H diff --git a/src/kernel/qclipboard_x11.cpp b/src/kernel/qclipboard_x11.cpp new file mode 100644 index 0000000..73cdad4 --- /dev/null +++ b/src/kernel/qclipboard_x11.cpp @@ -0,0 +1,1674 @@ +/**************************************************************************** +** +** Implementation of QClipboard class for X11 +** +** Created : 960430 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +// #define QCLIPBOARD_DEBUG +// #define QCLIPBOARD_DEBUG_VERBOSE + +#ifdef QCLIPBOARD_DEBUG +# define QDEBUG qDebug +#else +# define QDEBUG if (FALSE) qDebug +#endif + +#ifdef QCLIPBOARD_DEBUG_VERBOSE +# define VQDEBUG qDebug +#else +# define VQDEBUG if (FALSE) qDebug +#endif + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +#include "qclipboard.h" + +#ifndef QT_NO_CLIPBOARD + +#include "qapplication.h" +#include "qeventloop.h" +#include "qbitmap.h" +#include "qdatetime.h" +#include "qdragobject.h" +#include "qbuffer.h" +#include "qtextcodec.h" +#include "qvaluelist.h" +#include "qmap.h" +#include "qt_x11_p.h" +#include "qapplication_p.h" + + +// REVISED: arnt + +/***************************************************************************** + Internal QClipboard functions for X11. + *****************************************************************************/ + +// from qapplication_x11.cpp +typedef int (*QX11EventFilter) (XEvent*); +extern QX11EventFilter qt_set_x11_event_filter (QX11EventFilter filter); + +extern Time qt_x_time; // def. in qapplication_x11.cpp +extern Time qt_x_incr; // def. in qapplication_x11.cpp +extern Atom qt_xa_clipboard; +extern Atom qt_selection_property; +extern Atom qt_clipboard_sentinel; +extern Atom qt_selection_sentinel; +extern Atom qt_utf8_string; + +// from qdnd_x11.cpp +extern Atom* qt_xdnd_str_to_atom( const char *mimeType ); +extern const char* qt_xdnd_atom_to_str( Atom ); + + +static int clipboard_timeout = 5000; // 5s timeout on clipboard operations + +static QWidget * owner = 0; +static QWidget *requestor = 0; +static bool inSelectionMode_obsolete = FALSE; // ### remove 4.0 +static bool timer_event_clear = FALSE; +static int timer_id = 0; + +static int pending_timer_id = 0; +static bool pending_clipboard_changed = FALSE; +static bool pending_selection_changed = FALSE; + +Q_EXPORT bool qt_qclipboard_bailout_hack = false; + +// event capture mechanism for qt_xclb_wait_for_event +static bool waiting_for_data = FALSE; +static bool has_captured_event = FALSE; +static Window capture_event_win = None; +static int capture_event_type = -1; +static XEvent captured_event; + +class QClipboardWatcher; // forward decl +static QClipboardWatcher *selection_watcher = 0; +static QClipboardWatcher *clipboard_watcher = 0; + +static void cleanup() +{ + delete owner; + delete requestor; + owner = 0; + requestor = 0; +} + +static +void setupOwner() +{ + if ( owner ) + return; + owner = new QWidget( 0, "internal clipboard owner" ); + requestor = new QWidget(0, "internal clipboard requestor"); + qAddPostRoutine( cleanup ); +} + +static +int sizeof_format(int format) +{ + int sz; + switch (format) { + default: + case 8: sz = sizeof( char); break; + case 16: sz = sizeof(short); break; + case 32: sz = sizeof( long); break; + } + return sz; +} + +class QClipboardWatcher : public QMimeSource { +public: + QClipboardWatcher( QClipboard::Mode mode ); + ~QClipboardWatcher(); + bool empty() const; + const char* format( int n ) const; + QByteArray encodedData( const char* fmt ) const; + QByteArray getDataInFormat(Atom fmtatom) const; + + Atom atom; + QValueList<const char *> formatList; +}; + + + +class QClipboardData +{ +public: + QClipboardData(); + ~QClipboardData(); + + void setSource(QMimeSource* s) + { + clear(TRUE); + src = s; + } + + QMimeSource *source() const { return src; } + + void addTransferredPixmap(QPixmap pm) + { + /* TODO: queue them */ + transferred[tindex] = pm; + tindex=(tindex+1)%2; + } + void clearTransfers() + { + transferred[0] = QPixmap(); + transferred[1] = QPixmap(); + } + + void clear(bool destruct=TRUE); + + QMimeSource *src; + Time timestamp; + + QPixmap transferred[2]; + int tindex; +}; + +QClipboardData::QClipboardData() +{ + src = 0; + timestamp = CurrentTime; + tindex=0; +} + +QClipboardData::~QClipboardData() +{ clear(); } + +void QClipboardData::clear(bool destruct) +{ + if(destruct) + delete src; + src = 0; + timestamp = CurrentTime; +} + + +static QClipboardData *internalCbData = 0; +static QClipboardData *internalSelData = 0; + +static void cleanupClipboardData() +{ + delete internalCbData; + internalCbData = 0; +} + +static QClipboardData *clipboardData() +{ + if ( internalCbData == 0 ) { + internalCbData = new QClipboardData; + Q_CHECK_PTR( internalCbData ); + qAddPostRoutine( cleanupClipboardData ); + } + return internalCbData; +} + +void qt_clipboard_cleanup_mime_source(QMimeSource *src) +{ + if(internalCbData && internalCbData->source() == src) + internalCbData->clear(FALSE); +} + +static void cleanupSelectionData() +{ + delete internalSelData; + internalSelData = 0; +} + +static QClipboardData *selectionData() +{ + if (internalSelData == 0) { + internalSelData = new QClipboardData; + Q_CHECK_PTR(internalSelData); + qAddPostRoutine(cleanupSelectionData); + } + return internalSelData; +} + +class QClipboardINCRTransaction +{ +public: + QClipboardINCRTransaction(Window w, Atom p, Atom t, int f, QByteArray d, unsigned int i); + ~QClipboardINCRTransaction(void); + + int x11Event(XEvent *event); + + Window window; + Atom property, target; + int format; + QByteArray data; + unsigned int increment; + unsigned int offset; +}; + +typedef QMap<Window,QClipboardINCRTransaction*> TransactionMap; +static TransactionMap *transactions = 0; +static QX11EventFilter prev_x11_event_filter = 0; +static int incr_timer_id = 0; + +static int qt_xclb_transation_event_handler(XEvent *event) +{ + TransactionMap::Iterator it = transactions->find(event->xany.window); + if (it != transactions->end()) { + if ((*it)->x11Event(event) != 0) + return 1; + } + if (prev_x11_event_filter) + return prev_x11_event_filter(event); + return 0; +} + +/* + called when no INCR activity has happened for 'clipboard_timeout' + milliseconds... we assume that all unfinished transactions have + timed out and remove everything from the transaction map +*/ +static void qt_xclb_incr_timeout(void) +{ + qWarning("QClipboard: timed out while sending data"); + + while (transactions) + delete *transactions->begin(); +} + +QClipboardINCRTransaction::QClipboardINCRTransaction(Window w, Atom p, Atom t, int f, + QByteArray d, unsigned int i) + : window(w), property(p), target(t), format(f), data(d), increment(i), offset(0u) +{ + QDEBUG("QClipboard: sending %d bytes (INCR transaction %p)", d.size(), this); + + XSelectInput(QPaintDevice::x11AppDisplay(), window, PropertyChangeMask); + + if (! transactions) { + VQDEBUG("QClipboard: created INCR transaction map"); + transactions = new TransactionMap; + prev_x11_event_filter = qt_set_x11_event_filter(qt_xclb_transation_event_handler); + + incr_timer_id = QApplication::clipboard()->startTimer(clipboard_timeout); + } + transactions->insert(window, this); +} + +QClipboardINCRTransaction::~QClipboardINCRTransaction(void) +{ + VQDEBUG("QClipboard: destroyed INCR transacton %p", this); + + XSelectInput(QPaintDevice::x11AppDisplay(), window, NoEventMask); + + transactions->remove(window); + if (transactions->isEmpty()) { + VQDEBUG("QClipboard: no more INCR transations"); + delete transactions; + transactions = 0; + (void)qt_set_x11_event_filter(prev_x11_event_filter); + + if (incr_timer_id != 0) { + QApplication::clipboard()->killTimer(incr_timer_id); + incr_timer_id = 0; + } + } +} + +int QClipboardINCRTransaction::x11Event(XEvent *event) +{ + if (event->type != PropertyNotify + || (event->xproperty.state != PropertyDelete + || event->xproperty.atom != property)) + return 0; + + // restart the INCR timer + if (incr_timer_id) QApplication::clipboard()->killTimer(incr_timer_id); + incr_timer_id = QApplication::clipboard()->startTimer(clipboard_timeout); + + unsigned int bytes_left = data.size() - offset; + if (bytes_left > 0) { + unsigned int xfer = QMIN(increment, bytes_left); + VQDEBUG("QClipboard: sending %d bytes, %d remaining (INCR transaction %p)", + xfer, bytes_left - xfer, this); + + XChangeProperty(QPaintDevice::x11AppDisplay(), window, property, target, format, + PropModeReplace, (uchar *) data.data() + offset, xfer); + offset += xfer; + } else { + // INCR transaction finished... + XChangeProperty(QPaintDevice::x11AppDisplay(), window, property, target, format, + PropModeReplace, (uchar *) data.data(), 0); + delete this; + } + + return 1; +} + + +/***************************************************************************** + QClipboard member functions for X11. + *****************************************************************************/ + + +void QClipboard::clear( Mode mode ) +{ setData(0, mode); } + + +/*! + Returns TRUE if the clipboard supports mouse selection; otherwise + returns FALSE. +*/ +bool QClipboard::supportsSelection() const +{ return TRUE; } + + +/*! + Returns TRUE if this clipboard object owns the mouse selection + data; otherwise returns FALSE. +*/ +bool QClipboard::ownsSelection() const +{ return selectionData()->timestamp != CurrentTime; } + +/*! + Returns TRUE if this clipboard object owns the clipboard data; + otherwise returns FALSE. +*/ +bool QClipboard::ownsClipboard() const +{ return clipboardData()->timestamp != CurrentTime; } + + +/*! \obsolete + + Use the QClipboard::data(), QClipboard::setData() and related functions + which take a QClipboard::Mode argument. + + Sets the clipboard selection mode. If \a enable is TRUE, then + subsequent calls to QClipboard::setData() and other functions + which put data into the clipboard will put the data into the mouse + selection, otherwise the data will be put into the clipboard. + + \sa supportsSelection(), selectionModeEnabled() +*/ +void QClipboard::setSelectionMode(bool enable) +{ inSelectionMode_obsolete = enable; } + + +/*! \obsolete + + Use the QClipboard::data(), QClipboard::setData() and related functions + which take a QClipboard::Mode argument. + + Returns the selection mode. + + \sa setSelectionMode(), supportsSelection() +*/ +bool QClipboard::selectionModeEnabled() const +{ return inSelectionMode_obsolete; } + + +// event filter function... captures interesting events while +// qt_xclb_wait_for_event is running the event loop +static int qt_xclb_event_filter(XEvent *event) +{ + if (event->xany.type == capture_event_type && + event->xany.window == capture_event_win) { + VQDEBUG( "QClipboard: event_filter(): caught event type %d", event->type ); + has_captured_event = TRUE; + captured_event = *event; + return 1; + } + + return 0; +} + +static Bool checkForClipboardEvents(Display *, XEvent *e, XPointer) +{ + return ((e->type == SelectionRequest && (e->xselectionrequest.selection == XA_PRIMARY + || e->xselectionrequest.selection == qt_xa_clipboard)) + || (e->type == SelectionClear && (e->xselectionclear.selection == XA_PRIMARY + || e->xselectionclear.selection == qt_xa_clipboard))); +} + +static bool selection_request_pending = false; + +static Bool check_selection_request_pending( Display*, XEvent* e, XPointer ) + { + if( e->type == SelectionRequest && e->xselectionrequest.owner == owner->winId()) + selection_request_pending = true; + return False; + } + +bool qt_xclb_wait_for_event( Display *dpy, Window win, int type, XEvent *event, + int timeout ) +{ + QTime started = QTime::currentTime(); + QTime now = started; + + if (qApp->eventLoop()->inherits("QMotif")) { // yes yes, evil hack, we know + if ( waiting_for_data ) + qFatal( "QClipboard: internal error, qt_xclb_wait_for_event recursed" ); + + waiting_for_data = TRUE; + has_captured_event = FALSE; + capture_event_win = win; + capture_event_type = type; + + QX11EventFilter old_event_filter = qt_set_x11_event_filter(qt_xclb_event_filter); + + do { + if ( XCheckTypedWindowEvent(dpy,win,type,event) ) { + waiting_for_data = FALSE; + qt_set_x11_event_filter(old_event_filter); + return TRUE; + } + + now = QTime::currentTime(); + if ( started > now ) // crossed midnight + started = now; + + // 0x08 == ExcludeTimers for X11 only + qApp->eventLoop()->processEvents( QEventLoop::ExcludeUserInput | + QEventLoop::ExcludeSocketNotifiers | + QEventLoop::WaitForMore | 0x08 ); + + if ( has_captured_event ) { + waiting_for_data = FALSE; + *event = captured_event; + qt_set_x11_event_filter(old_event_filter); + return TRUE; + } + } while ( started.msecsTo(now) < timeout ); + + waiting_for_data = FALSE; + qt_set_x11_event_filter(old_event_filter); + + return FALSE; + } + + bool flushed = FALSE; + do { + if ( XCheckTypedWindowEvent(dpy,win,type,event) ) + return TRUE; + if( qt_qclipboard_bailout_hack ) { + XEvent dummy; + selection_request_pending = false; + if ( owner != NULL ) + XCheckIfEvent(dpy,&dummy,check_selection_request_pending,NULL); + if( selection_request_pending ) + return TRUE; + } + + // process other clipboard events, since someone is probably requesting data from us + XEvent e; + if (XCheckIfEvent(dpy, &e, checkForClipboardEvents, 0)) + qApp->x11ProcessEvent(&e); + + now = QTime::currentTime(); + if ( started > now ) // crossed midnight + started = now; + + if(!flushed) { + XFlush( dpy ); + flushed = TRUE; + } + + // sleep 50ms, so we don't use up CPU cycles all the time. + struct timeval usleep_tv; + usleep_tv.tv_sec = 0; + usleep_tv.tv_usec = 50000; + select(0, 0, 0, 0, &usleep_tv); + } while ( started.msecsTo(now) < timeout ); + + return FALSE; +} + + +static inline int maxSelectionIncr( Display *dpy ) +{ return XMaxRequestSize(dpy) > 65536 ? 65536*4 : XMaxRequestSize(dpy)*4 - 100; } + +// uglyhack: externed into qt_xdnd.cpp. qt is really not designed for +// single-platform, multi-purpose blocks of code... +bool qt_xclb_read_property( Display *dpy, Window win, Atom property, + bool deleteProperty, + QByteArray *buffer, int *size, Atom *type, + int *format, bool nullterm ) +{ + int maxsize = maxSelectionIncr(dpy); + ulong bytes_left; // bytes_after + ulong length; // nitems + uchar *data; + Atom dummy_type; + int dummy_format; + int r; + + if ( !type ) // allow null args + type = &dummy_type; + if ( !format ) + format = &dummy_format; + + // Don't read anything, just get the size of the property data + r = XGetWindowProperty( dpy, win, property, 0, 0, False, + AnyPropertyType, type, format, + &length, &bytes_left, &data ); + if (r != Success || (type && *type == None)) { + buffer->resize( 0 ); + return FALSE; + } + XFree( (char*)data ); + + int offset = 0, buffer_offset = 0, format_inc = 1, proplen = bytes_left; + + VQDEBUG("QClipboard: read_property(): initial property length: %d", proplen); + + switch (*format) { + case 8: + default: + format_inc = sizeof(char) / 1; + break; + + case 16: + format_inc = sizeof(short) / 2; + proplen *= sizeof(short) / 2; + break; + + case 32: + format_inc = sizeof(long) / 4; + proplen *= sizeof(long) / 4; + break; + } + + bool ok = buffer->resize( proplen + (nullterm ? 1 : 0) ); + + VQDEBUG("QClipboard: read_property(): buffer resized to %d", buffer->size()); + + if ( ok ) { + // could allocate buffer + + while ( bytes_left ) { + // more to read... + + r = XGetWindowProperty( dpy, win, property, offset, maxsize/4, + False, AnyPropertyType, type, format, + &length, &bytes_left, &data ); + if (r != Success || (type && *type == None)) + break; + + offset += length / (32 / *format); + length *= format_inc * (*format) / 8; + + // Here we check if we get a buffer overflow and tries to + // recover -- this shouldn't normally happen, but it doesn't + // hurt to be defensive + if (buffer_offset + length > buffer->size()) { + length = buffer->size() - buffer_offset; + + // escape loop + bytes_left = 0; + } + + memcpy(buffer->data() + buffer_offset, data, length); + buffer_offset += length; + + XFree( (char*)data ); + } + + static Atom xa_compound_text = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + if ( *format == 8 && *type == xa_compound_text ) { + // convert COMPOUND_TEXT to a multibyte string + XTextProperty textprop; + textprop.encoding = *type; + textprop.format = *format; + textprop.nitems = length; + textprop.value = (unsigned char *) buffer->data(); + + char **list_ret = 0; + int count; + if ( XmbTextPropertyToTextList( dpy, &textprop, &list_ret, + &count ) == Success && + count && list_ret ) { + offset = strlen( list_ret[0] ); + buffer->resize( offset + ( nullterm ? 1 : 0 ) ); + memcpy( buffer->data(), list_ret[0], offset ); + } + if (list_ret) XFreeStringList(list_ret); + } + + // zero-terminate (for text) + if (nullterm) + buffer->at(buffer_offset) = '\0'; + } + + // correct size, not 0-term. + if ( size ) + *size = buffer_offset; + + VQDEBUG("QClipboard: read_property(): buffer size %d, buffer offset %d, offset %d", + buffer->size(), buffer_offset, offset); + + if ( deleteProperty ) + XDeleteProperty( dpy, win, property ); + + XFlush( dpy ); + + return ok; +} + + +// this is externed into qt_xdnd.cpp too. +QByteArray qt_xclb_read_incremental_property( Display *dpy, Window win, + Atom property, int nbytes, + bool nullterm ) +{ + XEvent event; + + QByteArray buf; + QByteArray tmp_buf; + bool alloc_error = FALSE; + int length; + int offset = 0; + + if ( nbytes > 0 ) { + // Reserve buffer + zero-terminator (for text data) + // We want to complete the INCR transfer even if we cannot + // allocate more memory + alloc_error = !buf.resize(nbytes+1); + } + + for (;;) { + XFlush( dpy ); + if ( !qt_xclb_wait_for_event(dpy,win,PropertyNotify,&event,clipboard_timeout) ) + break; + if ( event.xproperty.atom != property || + event.xproperty.state != PropertyNewValue ) + continue; + if ( qt_xclb_read_property(dpy, win, property, TRUE, &tmp_buf, + &length,0, 0, FALSE) ) { + if ( length == 0 ) { // no more data, we're done + if ( nullterm ) { + buf.resize( offset+1 ); + buf.at( offset ) = '\0'; + } else { + buf.resize(offset); + } + return buf; + } else if ( !alloc_error ) { + if ( offset+length > (int)buf.size() ) { + if ( !buf.resize(offset+length+65535) ) { + alloc_error = TRUE; + length = buf.size() - offset; + } + } + memcpy( buf.data()+offset, tmp_buf.data(), length ); + tmp_buf.resize( 0 ); + offset += length; + } + } else { + break; + } + } + + // timed out ... create a new requestor window, otherwise the requestor + // could consider next request to be still part of this timed out request + delete requestor; + requestor = new QWidget( 0, "internal clipboard requestor" ); + + return QByteArray(); +} + +static Atom send_selection(QClipboardData *d, Atom target, Window window, Atom property, + int format = 0, QByteArray data = QByteArray()); + +static Atom send_targets_selection(QClipboardData *d, Window window, Atom property) +{ + int atoms = 0; + while (d->source()->format(atoms)) atoms++; + if (d->source()->provides("image/ppm")) atoms++; + if (d->source()->provides("image/pbm")) atoms++; + if (d->source()->provides("text/plain")) atoms+=4; + + VQDEBUG("QClipboard: send_targets_selection(): %d provided types", atoms); + + // for 64 bit cleanness... XChangeProperty expects long* for data with format == 32 + QByteArray data((atoms+3) * sizeof(long)); // plus TARGETS, MULTIPLE and TIMESTAMP + long *atarget = (long *) data.data(); + + const char *fmt; + int n = 0; + while ((fmt=d->source()->format(n)) && n < atoms) + atarget[n++] = *qt_xdnd_str_to_atom(fmt); + + static Atom xa_text = *qt_xdnd_str_to_atom("TEXT"); + static Atom xa_compound_text = *qt_xdnd_str_to_atom("COMPOUND_TEXT"); + static Atom xa_targets = *qt_xdnd_str_to_atom("TARGETS"); + static Atom xa_multiple = *qt_xdnd_str_to_atom("MULTIPLE"); + static Atom xa_timestamp = *qt_xdnd_str_to_atom("TIMESTAMP"); + + if (d->source()->provides("image/ppm")) + atarget[n++] = XA_PIXMAP; + if (d->source()->provides("image/pbm")) + atarget[n++] = XA_BITMAP; + if (d->source()->provides("text/plain")) { + atarget[n++] = qt_utf8_string; + atarget[n++] = xa_text; + atarget[n++] = xa_compound_text; + atarget[n++] = XA_STRING; + } + + atarget[n++] = xa_targets; + atarget[n++] = xa_multiple; + atarget[n++] = xa_timestamp; + +#if defined(QCLIPBOARD_DEBUG_VERBOSE) + for (int index = 0; index < n; index++) { + VQDEBUG(" atom %d: 0x%lx (%s)", index, atarget[index], + qt_xdnd_atom_to_str(atarget[index])); + } +#endif + + XChangeProperty(QPaintDevice::x11AppDisplay(), window, property, XA_ATOM, 32, + PropModeReplace, (uchar *) data.data(), n); + return property; +} + +static Atom send_string_selection(QClipboardData *d, Atom target, Window window, Atom property) +{ + static Atom xa_text = *qt_xdnd_str_to_atom("TEXT"); + static Atom xa_compound_text = *qt_xdnd_str_to_atom("COMPOUND_TEXT"); + + QDEBUG("QClipboard: send_string_selection():\n" + " property type %lx\n" + " property name '%s'", + target, qt_xdnd_atom_to_str(target)); + + if (target == xa_text || target == xa_compound_text) { + // the ICCCM states that TEXT and COMPOUND_TEXT are in the + // encoding of choice, so we choose the encoding of the locale + QByteArray data = d->source()->encodedData("text/plain"); + if(data.resize(data.size() + 1)) + data[int(data.size() - 1)] = '\0'; + char *list[] = { data.data(), NULL }; + + XICCEncodingStyle style = + (target == xa_compound_text) ? XCompoundTextStyle : XStdICCTextStyle; + XTextProperty textprop; + if (list[0] != NULL + && XmbTextListToTextProperty(QPaintDevice::x11AppDisplay(), + list, 1, style, &textprop) == Success) { + QDEBUG(" textprop type %lx\n" + " textprop name '%s'\n" + " format %d\n" + " %ld items", + textprop.encoding, qt_xdnd_atom_to_str(textprop.encoding), + textprop.format, textprop.nitems); + + int sz = sizeof_format(textprop.format); + data.duplicate((const char *) textprop.value, textprop.nitems * sz); + XFree(textprop.value); + + return send_selection(d, textprop.encoding, window, property, textprop.format, data); + } + + return None; + } + + Atom xtarget = None; + const char *fmt = 0; + if (target == XA_STRING + || (target == xa_text && QTextCodec::codecForLocale()->mibEnum() == 4)) { + // the ICCCM states that STRING is latin1 plus newline and tab + // see section 2.6.2 + fmt = "text/plain;charset=ISO-8859-1"; + xtarget = XA_STRING; + } else if (target == qt_utf8_string) { + // proposed UTF8_STRING conversion type + fmt = "text/plain;charset=UTF-8"; + xtarget = qt_utf8_string; + } + + if (xtarget == None) // should not happen + return None; + + QByteArray data = d->source()->encodedData(fmt); + + QDEBUG(" format 8\n %d bytes", data.size()); + + return send_selection(d, xtarget, window, property, 8, data); +} + +static Atom send_pixmap_selection(QClipboardData *d, Atom target, Window window, Atom property) +{ + QPixmap pm; + + if ( target == XA_PIXMAP ) { + QByteArray data = d->source()->encodedData("image/ppm"); + pm.loadFromData(data); + } else if ( target == XA_BITMAP ) { + QByteArray data = d->source()->encodedData("image/pbm"); + QImage img; + img.loadFromData(data); + if ( img.depth() != 1 ) + img = img.convertDepth(1); + } + + if (pm.isNull()) // should never happen + return None; + + Pixmap handle = pm.handle(); + XChangeProperty(QPaintDevice::x11AppDisplay(), window, property, + target, 32, PropModeReplace, (uchar *) &handle, 1); + d->addTransferredPixmap(pm); + return property; + +} + +static Atom send_selection(QClipboardData *d, Atom target, Window window, Atom property, + int format, QByteArray data) +{ + if (format == 0) format = 8; + + if (data.isEmpty()) { + const char *fmt = qt_xdnd_atom_to_str(target); + QDEBUG("QClipboard: send_selection(): converting to type '%s'", fmt); + if (fmt && !d->source()->provides(fmt)) // Not a MIME type we can produce + return None; + data = d->source()->encodedData(fmt); + } + + QDEBUG("QClipboard: send_selection():\n" + " property type %lx\n" + " property name '%s'\n" + " format %d\n" + " %d bytes", + target, qt_xdnd_atom_to_str(target), format, data.size()); + + // don't allow INCR transfers when using MULTIPLE or to + // Motif clients (since Motif doesn't support INCR) + static Atom motif_clip_temporary = *qt_xdnd_str_to_atom("CLIP_TEMPORARY"); + bool allow_incr = property != motif_clip_temporary; + + // X_ChangeProperty protocol request is 24 bytes + const unsigned int increment = (XMaxRequestSize(QPaintDevice::x11AppDisplay()) * 4) - 24; + if (data.size() > increment && allow_incr) { + long bytes = data.size(); + XChangeProperty(QPaintDevice::x11AppDisplay(), window, property, + qt_x_incr, 32, PropModeReplace, (uchar *) &bytes, 1); + + (void)new QClipboardINCRTransaction(window, property, target, format, data, increment); + return qt_x_incr; + } + + // make sure we can perform the XChangeProperty in a single request + if (data.size() > increment) + return None; // ### perhaps use several XChangeProperty calls w/ PropModeAppend? + + // use a single request to transfer data + XChangeProperty(QPaintDevice::x11AppDisplay(), window, property, target, + format, PropModeReplace, (uchar *) data.data(), + data.size() / sizeof_format(format)); + return property; +} + +/*! \internal + Internal cleanup for Windows. +*/ +void QClipboard::ownerDestroyed() +{ } + + +/*! \internal + Internal optimization for Windows. +*/ +void QClipboard::connectNotify( const char * ) +{ } + + +/*! \reimp + */ +bool QClipboard::event( QEvent *e ) +{ + if ( e->type() == QEvent::Timer ) { + QTimerEvent *te = (QTimerEvent *) e; + + if ( waiting_for_data ) // should never happen + return FALSE; + + if (te->timerId() == timer_id) { + killTimer(timer_id); + timer_id = 0; + + timer_event_clear = TRUE; + if ( selection_watcher ) // clear selection + selectionData()->clear(); + if ( clipboard_watcher ) // clear clipboard + clipboardData()->clear(); + timer_event_clear = FALSE; + + return TRUE; + } else if ( te->timerId() == pending_timer_id ) { + // I hate klipper + killTimer( pending_timer_id ); + pending_timer_id = 0; + + if ( pending_clipboard_changed ) { + pending_clipboard_changed = FALSE; + clipboardData()->clear(); + emit dataChanged(); + } + if ( pending_selection_changed ) { + pending_selection_changed = FALSE; + selectionData()->clear(); + emit selectionChanged(); + } + + return TRUE; + } else if (te->timerId() == incr_timer_id) { + killTimer(incr_timer_id); + incr_timer_id = 0; + + qt_xclb_incr_timeout(); + + return TRUE; + } else { + return QObject::event( e ); + } + } else if ( e->type() != QEvent::Clipboard ) { + return QObject::event( e ); + } + + XEvent *xevent = (XEvent *)(((QCustomEvent *)e)->data()); + Display *dpy = QPaintDevice::x11AppDisplay(); + + if ( !xevent ) + return TRUE; + + switch ( xevent->type ) { + + case SelectionClear: + // new selection owner + if (xevent->xselectionclear.selection == XA_PRIMARY) { + QClipboardData *d = selectionData(); + + // ignore the event if it was generated before we gained selection ownership + if (d->timestamp != CurrentTime && xevent->xselectionclear.time < d->timestamp) + break; + + QDEBUG("QClipboard: new selection owner 0x%lx at time %lx (ours %lx)", + XGetSelectionOwner(dpy, XA_PRIMARY), + xevent->xselectionclear.time, d->timestamp); + + if ( ! waiting_for_data ) { + d->clear(); + emit selectionChanged(); + } else { + pending_selection_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = QApplication::clipboard()->startTimer( 0 ); + } + } else if (xevent->xselectionclear.selection == qt_xa_clipboard) { + QClipboardData *d = clipboardData(); + + // ignore the event if it was generated before we gained selection ownership + if (d->timestamp != CurrentTime && xevent->xselectionclear.time < d->timestamp) + break; + + QDEBUG("QClipboard: new clipboard owner 0x%lx at time %lx (%lx)", + XGetSelectionOwner(dpy, qt_xa_clipboard), + xevent->xselectionclear.time, d->timestamp); + + if ( ! waiting_for_data ) { + d->clear(); + emit dataChanged(); + } else { + pending_clipboard_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = QApplication::clipboard()->startTimer( 0 ); + } + } else { +#ifdef QT_CHECK_STATE + qWarning("QClipboard: Unknown SelectionClear event received."); +#endif + return FALSE; + } + break; + + case SelectionNotify: + /* + Something has delivered data to us, but this was not caught + by QClipboardWatcher::getDataInFormat() + + Just skip the event to prevent Bad Things (tm) from + happening later on... + */ + break; + + case SelectionRequest: + { + // someone wants our data + XSelectionRequestEvent *req = &xevent->xselectionrequest; + + if (requestor && req->requestor == requestor->winId()) + break; + + XEvent event; + event.xselection.type = SelectionNotify; + event.xselection.display = req->display; + event.xselection.requestor = req->requestor; + event.xselection.selection = req->selection; + event.xselection.target = req->target; + event.xselection.property = None; + event.xselection.time = req->time; + + QDEBUG("QClipboard: SelectionRequest from %lx\n" + " selection 0x%lx (%s) target 0x%lx (%s)", + req->requestor, + req->selection, + qt_xdnd_atom_to_str(req->selection), + req->target, + qt_xdnd_atom_to_str(req->target)); + + QClipboardData *d; + + if ( req->selection == XA_PRIMARY ) { + d = selectionData(); + } else if ( req->selection == qt_xa_clipboard ) { + d = clipboardData(); + } else { +#ifdef QT_CHECK_RANGE + qWarning("QClipboard: unknown selection '%lx'", req->selection); +#endif // QT_CHECK_RANGE + + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + + if (! d->source()) { +#ifdef QT_CHECK_STATE + qWarning("QClipboard: cannot transfer data, no data available"); +#endif // QT_CHECK_STATE + + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + + QDEBUG("QClipboard: SelectionRequest at time %lx (ours %lx)", + req->time, d->timestamp); + + if (d->timestamp == CurrentTime // we don't own the selection anymore + || (req->time != CurrentTime && req->time < d->timestamp)) { + QDEBUG("QClipboard: SelectionRequest too old"); + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + + static Atom xa_text = *qt_xdnd_str_to_atom("TEXT"); + static Atom xa_compound_text = *qt_xdnd_str_to_atom("COMPOUND_TEXT"); + static Atom xa_targets = *qt_xdnd_str_to_atom("TARGETS"); + static Atom xa_multiple = *qt_xdnd_str_to_atom("MULTIPLE"); + static Atom xa_timestamp = *qt_xdnd_str_to_atom("TIMESTAMP"); + + struct AtomPair { Atom target; Atom property; } *multi = 0; + Atom multi_type = None; + int multi_format = 0; + int nmulti = 0; + int imulti = -1; + bool multi_writeback = FALSE; + + if ( req->target == xa_multiple ) { + QByteArray multi_data; + if (req->property == None + || !qt_xclb_read_property(dpy, req->requestor, req->property, + FALSE, &multi_data, 0, &multi_type, &multi_format, 0) + || multi_format != 32) { + // MULTIPLE property not formatted correctly + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + nmulti = multi_data.size()/sizeof(*multi); + multi = new AtomPair[nmulti]; + memcpy(multi,multi_data.data(),multi_data.size()); + imulti = 0; + } + + for (; imulti < nmulti; ++imulti) { + Atom target; + Atom property; + + if ( multi ) { + target = multi[imulti].target; + property = multi[imulti].property; + } else { + target = req->target; + property = req->property; + if (property == None) // obsolete client + property = target; + } + + Atom ret = None; + if (target == None || property == None) { + ; + } else if (target == xa_timestamp) { + if (d->timestamp != CurrentTime) { + XChangeProperty(dpy, req->requestor, property, xa_timestamp, 32, + PropModeReplace, (uchar *) &d->timestamp, 1); + ret = property; + } else { + +#ifdef QT_CHECK_STATE + qWarning("QClipboard: invalid data timestamp"); +#endif // QT_CHECK_STATE + } + } else if (target == xa_targets) { + ret = send_targets_selection(d, req->requestor, property); + } else if (target == XA_STRING + || target == xa_text + || target == xa_compound_text + || target == qt_utf8_string) { + ret = send_string_selection(d, target, req->requestor, property); + } else if (target == XA_PIXMAP + || target == XA_BITMAP) { + ret = send_pixmap_selection(d, target, req->requestor, property); + } else { + ret = send_selection(d, target, req->requestor, property); + } + + if (nmulti > 0) { + if (ret == None) { + multi[imulti].property = None; + multi_writeback = TRUE; + } + } else { + event.xselection.property = ret; + break; + } + } + + if (nmulti > 0) { + if (multi_writeback) { + // according to ICCCM 2.6.2 says to put None back + // into the original property on the requestor window + XChangeProperty(dpy, req->requestor, req->property, multi_type, 32, + PropModeReplace, (uchar *) multi, nmulti * 2); + } + + delete [] multi; + event.xselection.property = req->property; + } + + // send selection notify to requestor + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + + QDEBUG("QClipboard: SelectionNotify to 0x%lx\n" + " property 0x%lx (%s)", + req->requestor, event.xselection.property, + qt_xdnd_atom_to_str(event.xselection.property)); + } + break; + } + + return TRUE; +} + + + + + + +QClipboardWatcher::QClipboardWatcher( QClipboard::Mode mode ) +{ + switch ( mode ) { + case QClipboard::Selection: + atom = XA_PRIMARY; + break; + + case QClipboard::Clipboard: + atom = qt_xa_clipboard; + break; + +#ifdef QT_CHECK_RANGE + default: + qWarning( "QClipboardWatcher: internal error, unknown clipboard mode" ); + break; +#endif // QT_CHECK_RANGE + } + + setupOwner(); +} + +QClipboardWatcher::~QClipboardWatcher() +{ + if( selection_watcher == this ) + selection_watcher = 0; + if( clipboard_watcher == this ) + clipboard_watcher = 0; +} + +bool QClipboardWatcher::empty() const +{ + Display *dpy = QPaintDevice::x11AppDisplay(); + Window win = XGetSelectionOwner( dpy, atom ); + +#ifdef QT_CHECK_STATE + if( win == requestor->winId()) { + qWarning( "QClipboardWatcher::empty: internal error, app owns the selection" ); + return TRUE; + } +#endif // QT_CHECK_STATE + + return win == None; +} + +const char* QClipboardWatcher::format( int n ) const +{ + if ( empty() ) + return 0; + + if (! formatList.count()) { + // get the list of targets from the current clipboard owner - we do this + // once so that multiple calls to this function don't require multiple + // server round trips... + static Atom xa_targets = *qt_xdnd_str_to_atom( "TARGETS" ); + + QClipboardWatcher *that = (QClipboardWatcher *) this; + QByteArray ba = getDataInFormat(xa_targets); + if (ba.size() > 0) { + Atom *unsorted_target = (Atom *) ba.data(); + static Atom xa_text = *qt_xdnd_str_to_atom( "TEXT" ); + static Atom xa_compound_text = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + int i, size = ba.size() / sizeof(Atom); + + // sort TARGETS to prefer some types over others. some apps + // will report XA_STRING before COMPOUND_TEXT, and we want the + // latter, not the former (if it is present). + Atom* target = new Atom[size+4]; + memset( target, 0, (size+4) * sizeof(Atom) ); + + for ( i = 0; i < size; ++i ) { + if ( unsorted_target[i] == qt_utf8_string ) + target[0] = unsorted_target[i]; + else if ( unsorted_target[i] == xa_compound_text ) + target[1] = unsorted_target[i]; + else if ( unsorted_target[i] == xa_text ) + target[2] = unsorted_target[i]; + else if ( unsorted_target[i] == XA_STRING ) + target[3] = unsorted_target[i]; + else + target[i + 4] = unsorted_target[i]; + } + + for (i = 0; i < size + 4; ++i) { + if ( target[i] == 0 ) continue; + + VQDEBUG(" format: %s", qt_xdnd_atom_to_str(target[i])); + + if ( target[i] == XA_PIXMAP ) + that->formatList.append("image/ppm"); + else if ( target[i] == XA_STRING ) + that->formatList.append( "text/plain;charset=ISO-8859-1" ); + else if ( target[i] == qt_utf8_string ) + that->formatList.append( "text/plain;charset=UTF-8" ); + else if ( target[i] == xa_text || + target[i] == xa_compound_text ) + that->formatList.append( "text/plain" ); + else + that->formatList.append(qt_xdnd_atom_to_str(target[i])); + } + delete []target; + + QDEBUG("QClipboardWatcher::format: %d formats available", + int(that->formatList.count())); + } + } + + if (n >= 0 && n < (signed) formatList.count()) + return formatList[n]; + if (n == 0) + return "text/plain"; + return 0; +} + +QByteArray QClipboardWatcher::encodedData( const char* fmt ) const +{ + if ( !fmt || empty() ) + return QByteArray( 0 ); + + QDEBUG("QClipboardWatcher::encodedData: fetching format '%s'", fmt); + + Atom fmtatom = 0; + + if ( 0==qstricmp(fmt,"text/plain;charset=iso-8859-1") ) { + // ICCCM section 2.6.2 says STRING is latin1 text + fmtatom = XA_STRING; + } else if ( 0==qstricmp(fmt,"text/plain;charset=utf-8") ) { + // proprosed UTF8_STRING conversion type + fmtatom = *qt_xdnd_str_to_atom( "UTF8_STRING" ); + } else if ( 0==qstrcmp(fmt,"text/plain") ) { + fmtatom = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + } else if ( 0==qstrcmp(fmt,"image/ppm") ) { + fmtatom = XA_PIXMAP; + QByteArray pmd = getDataInFormat(fmtatom); + if ( pmd.size() == sizeof(Pixmap) ) { + Pixmap xpm = *((Pixmap*)pmd.data()); + Display *dpy = QPaintDevice::x11AppDisplay(); + Window r; + int x,y; + uint w,h,bw,d; + if ( ! xpm ) + return QByteArray( 0 ); + XGetGeometry(dpy,xpm, &r,&x,&y,&w,&h,&bw,&d); + QImageIO iio; + GC gc = XCreateGC( dpy, xpm, 0, 0 ); + if ( d == 1 ) { + QBitmap qbm(w,h); + XCopyArea(dpy,xpm,qbm.handle(),gc,0,0,w,h,0,0); + iio.setFormat("PBMRAW"); + iio.setImage(qbm.convertToImage()); + } else { + QPixmap qpm(w,h); + XCopyArea(dpy,xpm,qpm.handle(),gc,0,0,w,h,0,0); + iio.setFormat("PPMRAW"); + iio.setImage(qpm.convertToImage()); + } + XFreeGC(dpy,gc); + QBuffer buf; + buf.open(IO_WriteOnly); + iio.setIODevice(&buf); + iio.write(); + return buf.buffer(); + } else { + fmtatom = *qt_xdnd_str_to_atom(fmt); + } + } else { + fmtatom = *qt_xdnd_str_to_atom(fmt); + } + return getDataInFormat(fmtatom); +} + +QByteArray QClipboardWatcher::getDataInFormat(Atom fmtatom) const +{ + QByteArray buf; + + Display *dpy = QPaintDevice::x11AppDisplay(); + Window win = requestor->winId(); + + QDEBUG("QClipboardWatcher::getDataInFormat: selection '%s' format '%s'", + qt_xdnd_atom_to_str(atom), qt_xdnd_atom_to_str(fmtatom)); + + XSelectInput(dpy, win, NoEventMask); // don't listen for any events + + XDeleteProperty(dpy, win, qt_selection_property); + XConvertSelection(dpy, atom, fmtatom, qt_selection_property, win, qt_x_time); + XSync(dpy, FALSE); + + VQDEBUG("QClipboardWatcher::getDataInFormat: waiting for SelectionNotify event"); + + XEvent xevent; + if ( !qt_xclb_wait_for_event(dpy,win,SelectionNotify,&xevent,clipboard_timeout) || + xevent.xselection.property == None ) { + QDEBUG("QClipboardWatcher::getDataInFormat: format not available"); + return buf; + } + + VQDEBUG("QClipboardWatcher::getDataInFormat: fetching data..."); + + Atom type; + XSelectInput(dpy, win, PropertyChangeMask); + + if ( qt_xclb_read_property(dpy,win,qt_selection_property,TRUE, + &buf,0,&type,0,FALSE) ) { + if ( type == qt_x_incr ) { + int nbytes = buf.size() >= 4 ? *((int*)buf.data()) : 0; + buf = qt_xclb_read_incremental_property( dpy, win, + qt_selection_property, + nbytes, FALSE ); + } + } + + XSelectInput(dpy, win, NoEventMask); + + QDEBUG("QClipboardWatcher::getDataInFormat: %d bytes received", buf.size()); + + return buf; +} + + +QMimeSource* QClipboard::data( Mode mode ) const +{ + QClipboardData *d; + switch ( mode ) { + case Selection: d = selectionData(); break; + case Clipboard: d = clipboardData(); break; + + default: +#ifdef QT_CHECK_RANGE + qWarning( "QClipboard::data: invalid mode '%d'", mode ); +#endif // QT_CHECK_RANGE + return 0; + } + + if ( ! d->source() && ! timer_event_clear ) { + if ( mode == Selection ) { + if ( ! selection_watcher ) + selection_watcher = new QClipboardWatcher( mode ); + d->setSource( selection_watcher ); + } else { + if ( ! clipboard_watcher ) + clipboard_watcher = new QClipboardWatcher( mode ); + d->setSource( clipboard_watcher ); + } + + if (! timer_id) { + // start a zero timer - we will clear cached data when the timer + // times out, which will be the next time we hit the event loop... + // that way, the data is cached long enough for calls within a single + // loop/function, but the data doesn't linger around in case the selection + // changes + QClipboard *that = ((QClipboard *) this); + timer_id = that->startTimer(0); + } + } + + return d->source(); +} + + +void QClipboard::setData( QMimeSource* src, Mode mode ) +{ + Atom atom, sentinel_atom; + QClipboardData *d; + switch ( mode ) { + case Selection: + atom = XA_PRIMARY; + sentinel_atom = qt_selection_sentinel; + d = selectionData(); + break; + + case Clipboard: + atom = qt_xa_clipboard; + sentinel_atom = qt_clipboard_sentinel; + d = clipboardData(); + break; + + default: +#ifdef QT_CHECK_RANGE + qWarning( "QClipboard::data: invalid mode '%d'", mode ); +#endif // QT_CHECK_RANGE + return; + } + + Display *dpy = QPaintDevice::x11AppDisplay(); + Window newOwner; + + if (! src) { // no data, clear clipboard contents + newOwner = None; + d->clear(); + } else { + setupOwner(); + + newOwner = owner->winId(); + + d->setSource(src); + d->timestamp = qt_x_time; + } + + Window prevOwner = XGetSelectionOwner( dpy, atom ); + // use qt_x_time, since d->timestamp == CurrentTime when clearing + XSetSelectionOwner(dpy, atom, newOwner, qt_x_time); + + if ( mode == Selection ) + emit selectionChanged(); + else + emit dataChanged(); + + if (XGetSelectionOwner(dpy, atom) != newOwner) { +#ifdef QT_CHECK_STATE + qWarning("QClipboard::setData: Cannot set X11 selection owner for %s", + qt_xdnd_atom_to_str(atom)); +#endif // QT_CHECK_STATE + + d->clear(); + return; + } + + // Signal to other Qt processes that the selection has changed + Window owners[2]; + owners[0] = newOwner; + owners[1] = prevOwner; + XChangeProperty( dpy, QApplication::desktop()->screen(0)->winId(), + sentinel_atom, XA_WINDOW, 32, PropModeReplace, + (unsigned char*)&owners, 2 ); +} + + +/* + Called by the main event loop in qapplication_x11.cpp when the + _QT_SELECTION_SENTINEL property has been changed (i.e. when some Qt + process has performed QClipboard::setData(). If it returns TRUE, the + QClipBoard dataChanged() signal should be emitted. +*/ + +bool qt_check_selection_sentinel() +{ + bool doIt = TRUE; + if ( owner ) { + /* + Since the X selection mechanism cannot give any signal when + the selection has changed, we emulate it (for Qt processes) here. + The notification should be ignored in case of either + a) This is the process that did setData (because setData() + then has already emitted dataChanged()) + b) This is the process that owned the selection when dataChanged() + was called (because we have then received a SelectionClear event, + and have already emitted dataChanged() as a result of that) + */ + + Window* owners; + Atom actualType; + int actualFormat; + ulong nitems; + ulong bytesLeft; + + if (XGetWindowProperty(QPaintDevice::x11AppDisplay(), + QApplication::desktop()->screen(0)->winId(), + qt_selection_sentinel, 0, 2, False, XA_WINDOW, + &actualType, &actualFormat, &nitems, + &bytesLeft, (unsigned char**)&owners) == Success) { + if ( actualType == XA_WINDOW && actualFormat == 32 && nitems == 2 ) { + Window win = owner->winId(); + if ( owners[0] == win || owners[1] == win ) + doIt = FALSE; + } + + XFree( owners ); + } + } + + if (doIt) { + if ( waiting_for_data ) { + pending_selection_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = QApplication::clipboard()->startTimer( 0 ); + doIt = FALSE; + } else { + selectionData()->clear(); + } + } + + return doIt; +} + + +bool qt_check_clipboard_sentinel() +{ + bool doIt = TRUE; + if (owner) { + Window *owners; + Atom actualType; + int actualFormat; + unsigned long nitems, bytesLeft; + + if (XGetWindowProperty(QPaintDevice::x11AppDisplay(), + QApplication::desktop()->screen(0)->winId(), + qt_clipboard_sentinel, 0, 2, False, XA_WINDOW, + &actualType, &actualFormat, &nitems, &bytesLeft, + (unsigned char **) &owners) == Success) { + if (actualType == XA_WINDOW && actualFormat == 32 && nitems == 2) { + Window win = owner->winId(); + if (owners[0] == win || owners[1] == win) + doIt = FALSE; + } + + XFree(owners); + } + } + + if (doIt) { + if ( waiting_for_data ) { + pending_clipboard_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = QApplication::clipboard()->startTimer( 0 ); + doIt = FALSE; + } else { + clipboardData()->clear(); + } + } + + return doIt; +} + +#endif // QT_NO_CLIPBOARD diff --git a/src/kernel/qcolor.cpp b/src/kernel/qcolor.cpp new file mode 100644 index 0000000..309915e --- /dev/null +++ b/src/kernel/qcolor.cpp @@ -0,0 +1,1030 @@ +/**************************************************************************** +** +** Implementation of QColor class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qcolor.h" +#include "qnamespace.h" +#include "qdatastream.h" + +#include <stdio.h> + + +/*! + \class QColor qcolor.h + \brief The QColor class provides colors based on RGB or HSV values. + + \ingroup images + \ingroup graphics + \ingroup appearance + + A color is normally specified in terms of RGB (red, green and blue) + components, but it is also possible to specify HSV (hue, saturation + and value) or set a color name (the names are copied from from the + X11 color database). + + In addition to the RGB value, a QColor also has a pixel value and a + validity. The pixel value is used by the underlying window system + to refer to a color. It can be thought of as an index into the + display hardware's color table. + + The validity (isValid()) indicates whether the color is legal at + all. For example, a RGB color with RGB values out of range is + illegal. For performance reasons, QColor mostly disregards illegal + colors. The result of using an invalid color is unspecified and + will usually be surprising. + + There are 19 predefined QColor objects: \c white, \c black, \c + red, \c darkRed, \c green, \c darkGreen, \c blue, \c darkBlue, \c + cyan, \c darkCyan, \c magenta, \c darkMagenta, \c yellow, \c + darkYellow, \c gray, \c darkGray, \c lightGray, \c color0 and \c + color1, accessible as members of the Qt namespace (ie. \c Qt::red). + + \img qt-colors.png Qt Colors + + The colors \c color0 (zero pixel value) and \c color1 (non-zero + pixel value) are special colors for drawing in \link QBitmap + bitmaps\endlink. Painting with \c color0 sets the bitmap bits to 0 + (transparent, i.e. background), and painting with \c color1 sets the + bits to 1 (opaque, i.e. foreground). + + The QColor class has an efficient, dynamic color allocation + strategy. A color is normally allocated the first time it is used + (lazy allocation), that is, whenever the pixel() function is called. + The following steps are taken to allocate a color. If, at any point, + a suitable color is found then the appropriate pixel value is + returned and the subsequent steps are not taken: + + \list 1 + \i Is the pixel value valid? If it is, just return it; otherwise, + allocate a pixel value. + \i Check an internal hash table to see if we allocated an equal RGB + value earlier. If we did, set the corresponding pixel value for the + color and return it. + \i Try to allocate the RGB value. If we succeed, we get a pixel value + that we save in the internal table with the RGB value. + Return the pixel value. + \i The color could not be allocated. Find the closest matching + color, save it in the internal table, and return it. + \endlist + + A color can be set by passing setNamedColor() an RGB string like + "#112233", or a color name, e.g. "blue". The names are taken from + X11's rgb.txt database but can also be used under Windows. To get + a lighter or darker color use light() and dark() respectively. + Colors can also be set using setRgb() and setHsv(). The color + components can be accessed in one go with rgb() and hsv(), or + individually with red(), green() and blue(). + + Use maxColors() and numBitPlanes() to determine the maximum number + of colors and the number of bit planes supported by the underlying + window system, + + If you need to allocate many colors temporarily, for example in an + image viewer application, enterAllocContext(), leaveAllocContext() and + destroyAllocContext() will prove useful. + + \section1 HSV Colors + + Because many people don't know the HSV color model very well, we'll + cover it briefly here. + + The RGB model is hardware-oriented. Its representation is close to + what most monitors show. In contrast, HSV represents color in a way + more suited to the human perception of color. For example, the + relationships "stronger than", "darker than" and "the opposite of" + are easily expressed in HSV but are much harder to express in RGB. + + HSV, like RGB, has three components: + + \list + + \i H, for hue, is either 0-359 if the color is chromatic (not + gray), or meaningless if it is gray. It represents degrees on the + color wheel familiar to most people. Red is 0 (degrees), green is + 120 and blue is 240. + + \i S, for saturation, is 0-255, and the bigger it is, the + stronger the color is. Grayish colors have saturation near 0; very + strong colors have saturation near 255. + + \i V, for value, is 0-255 and represents lightness or brightness + of the color. 0 is black; 255 is as far from black as possible. + + \endlist + + Here are some examples: Pure red is H=0, S=255, V=255. A dark red, + moving slightly towards the magenta, could be H=350 (equivalent to + -10), S=255, V=180. A grayish light red could have H about 0 (say + 350-359 or 0-10), S about 50-100, and S=255. + + Qt returns a hue value of -1 for achromatic colors. If you pass a + too-big hue value, Qt forces it into range. Hue 360 or 720 is + treated as 0; hue 540 is treated as 180. + + \sa QPalette, QColorGroup, QApplication::setColorSpec(), + \link http://www.poynton.com/ColorFAQ.html Color FAQ\endlink +*/ + +/***************************************************************************** + Global colors + *****************************************************************************/ + +#if defined(Q_WS_WIN) +#define COLOR0_PIX 0x00ffffff +#define COLOR1_PIX 0 +#else +#define COLOR0_PIX 0 +#define COLOR1_PIX 1 +#endif + +#if (defined(Q_CC_GNU) && defined(Q_OS_WIN)) +// workaround - bug in mingw +static QColor stdcol[19] = { + QColor( 255, 255, 255 ), + QColor( 0, 0, 0 ), + QColor( 0, 0, 0 ), + QColor( 255, 255, 255 ), + QColor( 128, 128, 128 ), + QColor( 160, 160, 164 ), + QColor( 192, 192, 192 ), + QColor( 255, 0, 0 ), + QColor( 0, 255, 0 ), + QColor( 0, 0, 255 ), + QColor( 0, 255, 255 ), + QColor( 255, 0, 255 ), + QColor( 255, 255, 0 ), + QColor( 128, 0, 0 ), + QColor( 0, 128, 0 ), + QColor( 0, 0, 128 ), + QColor( 0, 128, 128 ), + QColor( 128, 0, 128 ), + QColor( 128, 128, 0 ) }; +#else + static QColor stdcol[19]; +#endif + +QT_STATIC_CONST_IMPL QColor & Qt::color0 = stdcol[0]; +QT_STATIC_CONST_IMPL QColor & Qt::color1 = stdcol[1]; +QT_STATIC_CONST_IMPL QColor & Qt::black = stdcol[2]; +QT_STATIC_CONST_IMPL QColor & Qt::white = stdcol[3]; +QT_STATIC_CONST_IMPL QColor & Qt::darkGray = stdcol[4]; +QT_STATIC_CONST_IMPL QColor & Qt::gray = stdcol[5]; +QT_STATIC_CONST_IMPL QColor & Qt::lightGray = stdcol[6]; +QT_STATIC_CONST_IMPL QColor & Qt::red = stdcol[7]; +QT_STATIC_CONST_IMPL QColor & Qt::green = stdcol[8]; +QT_STATIC_CONST_IMPL QColor & Qt::blue = stdcol[9]; +QT_STATIC_CONST_IMPL QColor & Qt::cyan = stdcol[10]; +QT_STATIC_CONST_IMPL QColor & Qt::magenta = stdcol[11]; +QT_STATIC_CONST_IMPL QColor & Qt::yellow = stdcol[12]; +QT_STATIC_CONST_IMPL QColor & Qt::darkRed = stdcol[13]; +QT_STATIC_CONST_IMPL QColor & Qt::darkGreen = stdcol[14]; +QT_STATIC_CONST_IMPL QColor & Qt::darkBlue = stdcol[15]; +QT_STATIC_CONST_IMPL QColor & Qt::darkCyan = stdcol[16]; +QT_STATIC_CONST_IMPL QColor & Qt::darkMagenta = stdcol[17]; +QT_STATIC_CONST_IMPL QColor & Qt::darkYellow = stdcol[18]; + + +/***************************************************************************** + QColor member functions + *****************************************************************************/ + +bool QColor::color_init = FALSE; // color system not initialized +bool QColor::globals_init = FALSE; // global color not initialized +QColor::ColorModel QColor::colormodel = d32; + + +QColor* QColor::globalColors() +{ + return stdcol; +} + + +/*! + Initializes the global colors. This function is called if a global + color variable is initialized before the constructors for our + global color objects are executed. Without this mechanism, + assigning a color might assign an uninitialized value. + + Example: + \code + QColor myColor = red; // will initialize red etc. + + int main( int argc, char **argc ) + { + } + \endcode +*/ + +void QColor::initGlobalColors() +{ + globals_init = TRUE; + + #ifdef Q_WS_X11 + // HACK: we need a way to recognize color0 and color1 uniquely, so + // that we can use color0 and color1 with fixed pixel values on + // all screens + stdcol[ 0].d.argb = qRgba(255, 255, 255, 1); + stdcol[ 1].d.argb = qRgba( 0, 0, 0, 1); + #else + stdcol[ 0].d.argb = qRgb(255,255,255); + stdcol[ 1].d.argb = 0; + #endif // Q_WS_X11 + stdcol[ 0].setPixel( COLOR0_PIX ); + stdcol[ 1].setPixel( COLOR1_PIX ); + + // From the "The Palette Manager: How and Why" by Ron Gery, March 23, + // 1992, archived on MSDN: + // The Windows system palette is broken up into two sections, + // one with fixed colors and one with colors that can be changed + // by applications. The system palette predefines 20 entries; + // these colors are known as the static or reserved colors and + // consist of the 16 colors found in the Windows version 3.0 VGA + // driver and 4 additional colors chosen for their visual appeal. + // The DEFAULT_PALETTE stock object is, as the name implies, the + // default palette selected into a device context (DC) and consists + // of these static colors. Applications can set the remaining 236 + // colors using the Palette Manager. + // The 20 reserved entries have indices in [0,9] and [246,255]. We + // reuse 17 of them. + stdcol[ 2].setRgb( 0, 0, 0 ); // index 0 black + stdcol[ 3].setRgb( 255, 255, 255 ); // index 255 white + stdcol[ 4].setRgb( 128, 128, 128 ); // index 248 medium gray + stdcol[ 5].setRgb( 160, 160, 164 ); // index 247 light gray + stdcol[ 6].setRgb( 192, 192, 192 ); // index 7 light gray + stdcol[ 7].setRgb( 255, 0, 0 ); // index 249 red + stdcol[ 8].setRgb( 0, 255, 0 ); // index 250 green + stdcol[ 9].setRgb( 0, 0, 255 ); // index 252 blue + stdcol[10].setRgb( 0, 255, 255 ); // index 254 cyan + stdcol[11].setRgb( 255, 0, 255 ); // index 253 magenta + stdcol[12].setRgb( 255, 255, 0 ); // index 251 yellow + stdcol[13].setRgb( 128, 0, 0 ); // index 1 dark red + stdcol[14].setRgb( 0, 128, 0 ); // index 2 dark green + stdcol[15].setRgb( 0, 0, 128 ); // index 4 dark blue + stdcol[16].setRgb( 0, 128, 128 ); // index 6 dark cyan + stdcol[17].setRgb( 128, 0, 128 ); // index 5 dark magenta + stdcol[18].setRgb( 128, 128, 0 ); // index 3 dark yellow +} + +/*! + \enum QColor::Spec + + The type of color specified, either RGB or HSV, e.g. in the + \c{QColor::QColor( x, y, z, colorSpec)} constructor. + + \value Rgb + \value Hsv +*/ + + +/*! + \fn QColor::QColor() + + Constructs an invalid color with the RGB value (0, 0, 0). An + invalid color is a color that is not properly set up for the + underlying window system. + + The alpha value of an invalid color is unspecified. + + \sa isValid() +*/ + + +/*! + \fn QColor::QColor( int r, int g, int b ) + + Constructs a color with the RGB value \a r, \a g, \a b, in the + same way as setRgb(). + + The color is left invalid if any or the arguments are illegal. + + \sa setRgb() +*/ + + +/*! + Constructs a color with the RGB value \a rgb and a custom pixel + value \a pixel. + + If \a pixel == 0xffffffff (the default), then the color uses the + RGB value in a standard way. If \a pixel is something else, then + the pixel value is set directly to \a pixel, skipping the normal + allocation procedure. +*/ + +QColor::QColor( QRgb rgb, uint pixel ) +{ + if ( pixel == 0xffffffff ) { + setRgb( rgb ); + } else { + d.argb = rgb; + setPixel( pixel ); + } +} + +void QColor::setPixel( uint pixel ) +{ + switch ( colormodel ) { + case d8: + d.d8.direct = TRUE; + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pixel; + break; + case d32: + d.d32.pix = pixel; + break; + } +} + + +/*! + Constructs a color with the RGB or HSV value \a x, \a y, \a z. + + The arguments are an RGB value if \a colorSpec is QColor::Rgb. \a + x (red), \a y (green), and \a z (blue). All of them must be in the + range 0-255. + + The arguments are an HSV value if \a colorSpec is QColor::Hsv. \a + x (hue) must be -1 for achromatic colors and 0-359 for chromatic + colors; \a y (saturation) and \a z (value) must both be in the + range 0-255. + + \sa setRgb(), setHsv() +*/ + +QColor::QColor( int x, int y, int z, Spec colorSpec ) +{ + d.d32.argb = Invalid; + d.d32.pix = Dirt; + if ( colorSpec == Hsv ) + setHsv( x, y, z ); + else + setRgb( x, y, z ); +} + + +/*! + Constructs a named color in the same way as setNamedColor() using + name \a name. + + The color is left invalid if \a name cannot be parsed. + + \sa setNamedColor() +*/ + +QColor::QColor( const QString& name ) +{ + setNamedColor( name ); +} + + +/*! + Constructs a named color in the same way as setNamedColor() using + name \a name. + + The color is left invalid if \a name cannot be parsed. + + \sa setNamedColor() +*/ + +QColor::QColor( const char *name ) +{ + setNamedColor( QString(name) ); +} + + + +/*! + Constructs a color that is a copy of \a c. +*/ + +QColor::QColor( const QColor &c ) +{ + if ( !globals_init ) + initGlobalColors(); + d.argb = c.d.argb; + d.d32.pix = c.d.d32.pix; +} + + +/*! + Assigns a copy of the color \a c and returns a reference to this + color. +*/ + +QColor &QColor::operator=( const QColor &c ) +{ + if ( !globals_init ) + initGlobalColors(); + d.argb = c.d.argb; + d.d32.pix = c.d.d32.pix; + return *this; +} + + +/*! + \fn bool QColor::isValid() const + + Returns FALSE if the color is invalid, i.e. it was constructed using the + default constructor; otherwise returns TRUE. +*/ + +/*! + \internal +*/ +bool QColor::isDirty() const +{ + if ( colormodel == d8 ) { + return d.d8.dirty; + } else { + return d.d32.probablyDirty(); + } +} + +/*! + Returns the name of the color in the format "#RRGGBB", i.e. a "#" + character followed by three two-digit hexadecimal numbers. + + \sa setNamedColor() +*/ + +QString QColor::name() const +{ +#ifndef QT_NO_SPRINTF + QString s; + s.sprintf( "#%02x%02x%02x", red(), green(), blue() ); + return s; +#else + char s[20]; + sprintf( s, "#%02x%02x%02x", red(), green(), blue() ); + return QString(s); +#endif +} + +static int hex2int( QChar hexchar ) +{ + int v; + if ( hexchar.isDigit() ) + v = hexchar.digitValue(); + else if ( hexchar >= 'A' && hexchar <= 'F' ) + v = hexchar.cell() - 'A' + 10; + else if ( hexchar >= 'a' && hexchar <= 'f' ) + v = hexchar.cell() - 'a' + 10; + else + v = -1; + return v; +} + + +/*! + Sets the RGB value to \a name, which may be in one of these + formats: + \list + \i #RGB (each of R, G and B is a single hex digit) + \i #RRGGBB + \i #RRRGGGBBB + \i #RRRRGGGGBBBB + \i A name from the X color database (rgb.txt) (e.g. + "steelblue" or "gainsboro"). These color names also work + under Windows. + \endlist + + The color is invalid if \a name cannot be parsed. +*/ + +void QColor::setNamedColor( const QString &name ) +{ + if ( name.isEmpty() ) { + d.argb = 0; + if ( colormodel == d8 ) { + d.d8.invalid = TRUE; + } else { + d.d32.argb = Invalid; + } + } else if ( name[0] == '#' ) { + const QChar *p = name.unicode()+1; + int len = name.length()-1; + int r, g, b; + if ( len == 12 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[1]); + g = (hex2int(p[4]) << 4) + hex2int(p[5]); + b = (hex2int(p[8]) << 4) + hex2int(p[9]); + } else if ( len == 9 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[1]); + g = (hex2int(p[3]) << 4) + hex2int(p[4]); + b = (hex2int(p[6]) << 4) + hex2int(p[7]); + } else if ( len == 6 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[1]); + g = (hex2int(p[2]) << 4) + hex2int(p[3]); + b = (hex2int(p[4]) << 4) + hex2int(p[5]); + } else if ( len == 3 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[0]); + g = (hex2int(p[1]) << 4) + hex2int(p[1]); + b = (hex2int(p[2]) << 4) + hex2int(p[2]); + } else { + r = g = b = -1; + } + if ( (uint)r > 255 || (uint)g > 255 || (uint)b > 255 ) { + d.d32.argb = Invalid; + d.d32.pix = Dirt; +#if defined(QT_CHECK_RANGE) + qWarning( "QColor::setNamedColor: could not parse color '%s'", + name.local8Bit().data() ); +#endif + } else { + setRgb( r, g, b ); + } + } else { + setSystemNamedColor( name ); + } +} + + +#undef max +#undef min + +/*! + \fn void QColor::getHsv( int &h, int &s, int &v ) const + \obsolete +*/ + +/*! \fn void QColor::getHsv( int *h, int *s, int *v ) const + + Returns the current RGB value as HSV. The contents of the \a h, \a + s and \a v pointers are set to the HSV values. If any of the three + pointers are null, the function does nothing. + + The hue (which \a h points to) is set to -1 if the color is + achromatic. + + \warning Colors are stored internally as RGB values, so getHSv() + may return slightly different values to those set by setHsv(). + + \sa setHsv(), rgb() +*/ + +/*! \obsolete Use getHsv() instead. + */ +void QColor::hsv( int *h, int *s, int *v ) const +{ + if ( !h || !s || !v ) + return; + int r = qRed(d.argb); + int g = qGreen(d.argb); + int b = qBlue(d.argb); + uint max = r; // maximum RGB component + int whatmax = 0; // r=>0, g=>1, b=>2 + if ( (uint)g > max ) { + max = g; + whatmax = 1; + } + if ( (uint)b > max ) { + max = b; + whatmax = 2; + } + uint min = r; // find minimum value + if ( (uint)g < min ) min = g; + if ( (uint)b < min ) min = b; + int delta = max-min; + *v = max; // calc value + *s = max ? (510*delta+max)/(2*max) : 0; + if ( *s == 0 ) { + *h = -1; // undefined hue + } else { + switch ( whatmax ) { + case 0: // red is max component + if ( g >= b ) + *h = (120*(g-b)+delta)/(2*delta); + else + *h = (120*(g-b+delta)+delta)/(2*delta) + 300; + break; + case 1: // green is max component + if ( b > r ) + *h = 120 + (120*(b-r)+delta)/(2*delta); + else + *h = 60 + (120*(b-r+delta)+delta)/(2*delta); + break; + case 2: // blue is max component + if ( r > g ) + *h = 240 + (120*(r-g)+delta)/(2*delta); + else + *h = 180 + (120*(r-g+delta)+delta)/(2*delta); + break; + } + } +} + + +/*! + Sets a HSV color value. \a h is the hue, \a s is the saturation + and \a v is the value of the HSV color. + + If \a s or \a v are not in the range 0-255, or \a h is < -1, the + color is not changed. + + \warning Colors are stored internally as RGB values, so getHSv() + may return slightly different values to those set by setHsv(). + + \sa hsv(), setRgb() +*/ + +void QColor::setHsv( int h, int s, int v ) +{ + if ( h < -1 || (uint)s > 255 || (uint)v > 255 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QColor::setHsv: HSV parameters out of range" ); +#endif + return; + } + int r=v, g=v, b=v; + if ( s == 0 || h == -1 ) { // achromatic case + // Ignore + } else { // chromatic case + if ( (uint)h >= 360 ) + h %= 360; + uint f = h%60; + h /= 60; + uint p = (uint)(2*v*(255-s)+255)/510; + uint q, t; + if ( h&1 ) { + q = (uint)(2*v*(15300-s*f)+15300)/30600; + switch( h ) { + case 1: r=(int)q; g=(int)v, b=(int)p; break; + case 3: r=(int)p; g=(int)q, b=(int)v; break; + case 5: r=(int)v; g=(int)p, b=(int)q; break; + } + } else { + t = (uint)(2*v*(15300-(s*(60-f)))+15300)/30600; + switch( h ) { + case 0: r=(int)v; g=(int)t, b=(int)p; break; + case 2: r=(int)p; g=(int)v, b=(int)t; break; + case 4: r=(int)t; g=(int)p, b=(int)v; break; + } + } + } + setRgb( r, g, b ); +} + + +/*! + \fn QRgb QColor::rgb() const + + Returns the RGB value. + + The return type \e QRgb is equivalent to \c unsigned \c int. + + For an invalid color, the alpha value of the returned color is + unspecified. + + \sa setRgb(), hsv(), qRed(), qBlue(), qGreen(), isValid() +*/ + +/*! \fn void QColor::getRgb( int *r, int *g, int *b ) const + + Sets the contents pointed to by \a r, \a g and \a b to the red, + green and blue components of the RGB value respectively. The value + range for a component is 0..255. + + \sa rgb(), setRgb(), getHsv() +*/ + +/*! \obsolete Use getRgb() instead */ +void QColor::rgb( int *r, int *g, int *b ) const +{ + *r = qRed(d.argb); + *g = qGreen(d.argb); + *b = qBlue(d.argb); +} + + +/*! + Sets the RGB value to \a r, \a g, \a b. The arguments, \a r, \a g + and \a b must all be in the range 0..255. If any of them are + outside the legal range, the color is not changed. + + \sa rgb(), setHsv() +*/ + +void QColor::setRgb( int r, int g, int b ) +{ + if ( (uint)r > 255 || (uint)g > 255 || (uint)b > 255 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QColor::setRgb: RGB parameter(s) out of range" ); +#endif + return; + } + d.argb = qRgb( r, g, b ); + if ( colormodel == d8 ) { + d.d8.invalid = FALSE; + d.d8.direct = FALSE; + d.d8.dirty = TRUE; + } else { + d.d32.pix = Dirt; + } +} + + +/*! + \overload + Sets the RGB value to \a rgb. + + The type \e QRgb is equivalent to \c unsigned \c int. + + \sa rgb(), setHsv() +*/ + +void QColor::setRgb( QRgb rgb ) +{ + d.argb = rgb; + if ( colormodel == d8 ) { + d.d8.invalid = FALSE; + d.d8.direct = FALSE; + d.d8.dirty = TRUE; + } else { + d.d32.pix = Dirt; + } +} + +/*! + \fn int QColor::red() const + + Returns the R (red) component of the RGB value. +*/ + + +/*! + \fn int QColor::green() const + + Returns the G (green) component of the RGB value. +*/ + +/*! + \fn int QColor::blue() const + + Returns the B (blue) component of the RGB value. +*/ + + +/*! + Returns a lighter (or darker) color, but does not change this + object. + + Returns a lighter color if \a factor is greater than 100. Setting + \a factor to 150 returns a color that is 50% brighter. + + Returns a darker color if \a factor is less than 100. We recommend + using dark() for this purpose. If \a factor is 0 or negative, the + return value is unspecified. + + (This function converts the current RGB color to HSV, multiplies V + by \a factor, and converts the result back to RGB.) + + \sa dark() +*/ + +QColor QColor::light( int factor ) const +{ + if ( factor <= 0 ) // invalid lightness factor + return *this; + else if ( factor < 100 ) // makes color darker + return dark( 10000/factor ); + + int h, s, v; + hsv( &h, &s, &v ); + v = (factor*v)/100; + if ( v > 255 ) { // overflow + s -= v-255; // adjust saturation + if ( s < 0 ) + s = 0; + v = 255; + } + QColor c; + c.setHsv( h, s, v ); + return c; +} + + +/*! + Returns a darker (or lighter) color, but does not change this + object. + + Returns a darker color if \a factor is greater than 100. Setting + \a factor to 300 returns a color that has one-third the + brightness. + + Returns a lighter color if \a factor is less than 100. We + recommend using lighter() for this purpose. If \a factor is 0 or + negative, the return value is unspecified. + + (This function converts the current RGB color to HSV, divides V by + \a factor and converts back to RGB.) + + \sa light() +*/ + +QColor QColor::dark( int factor ) const +{ + if ( factor <= 0 ) // invalid darkness factor + return *this; + else if ( factor < 100 ) // makes color lighter + return light( 10000/factor ); + int h, s, v; + hsv( &h, &s, &v ); + v = (v*100)/factor; + QColor c; + c.setHsv( h, s, v ); + return c; +} + + +/*! + \fn bool QColor::operator==( const QColor &c ) const + + Returns TRUE if this color has the same RGB value as \a c; + otherwise returns FALSE. +*/ + +/*! + \fn bool QColor::operator!=( const QColor &c ) const + Returns TRUE if this color has a different RGB value from \a c; + otherwise returns FALSE. +*/ + +/*! + Returns the pixel value. + + This value is used by the underlying window system to refer to a + color. It can be thought of as an index into the display + hardware's color table, but the value is an arbitrary 32-bit + value. + + \sa alloc() +*/ +uint QColor::pixel() const +{ + if ( isDirty() ) + return ((QColor*)this)->alloc(); + else if ( colormodel == d8 ) +#ifdef Q_WS_WIN + // since d.d8.pix is uchar we have to use the PALETTEINDEX + // macro to get the respective palette entry index. + return (0x01000000 | (int)(short)(d.d8.pix)); +#else + return d.d8.pix; +#endif + else + return d.d32.pix; +} + +/*! + \fn QStringList QColor::colorNames() + Returns a QStringList containing the color names Qt knows about. +*/ + +/***************************************************************************** + QColor stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QColor + Writes a color object, \a c to the stream, \a s. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QColor &c ) +{ + Q_UINT32 p = (Q_UINT32)c.rgb(); + if ( s.version() == 1 ) // Swap red and blue + p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00); + return s << p; +} + +/*! + \relates QColor + Reads a color object, \a c, from the stream, \a s. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QColor &c ) +{ + Q_UINT32 p; + s >> p; + if ( s.version() == 1 ) // Swap red and blue + p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00); + c.setRgb( p ); + return s; +} +#endif + +/***************************************************************************** + QColor global functions (documentation only) + *****************************************************************************/ + +/*! + \fn int qRed( QRgb rgb ) + \relates QColor + + Returns the red component of the RGB triplet \a rgb. + \sa qRgb(), QColor::red() +*/ + +/*! + \fn int qGreen( QRgb rgb ) + \relates QColor + + Returns the green component of the RGB triplet \a rgb. + \sa qRgb(), QColor::green() +*/ + +/*! + \fn int qBlue( QRgb rgb ) + \relates QColor + + Returns the blue component of the RGB triplet \a rgb. + \sa qRgb(), QColor::blue() +*/ + +/*! + \fn int qAlpha( QRgb rgba ) + \relates QColor + + Returns the alpha component of the RGBA quadruplet \a rgba. + */ + +/*! + \fn QRgb qRgb( int r, int g, int b ) + \relates QColor + + Returns the RGB triplet \a (r,g,b). + + The return type QRgb is equivalent to \c unsigned \c int. + + \sa qRgba(), qRed(), qGreen(), qBlue() +*/ + +/*! + \fn QRgb qRgba( int r, int g, int b, int a ) + \relates QColor + + Returns the RGBA quadruplet \a (r,g,b,a). + + The return type QRgba is equivalent to \c unsigned \c int. + + \sa qRgb(), qRed(), qGreen(), qBlue() +*/ + +/*! + \fn int qGray( int r, int g, int b ) + \relates QColor + + Returns a gray value 0..255 from the (\a r, \a g, \a b) triplet. + + The gray value is calculated using the formula (r*11 + g*16 + + b*5)/32. +*/ + +/*! + \overload int qGray( qRgb rgb ) + \relates QColor + + Returns a gray value 0..255 from the given \a rgb colour. +*/ + diff --git a/src/kernel/qcolor.h b/src/kernel/qcolor.h new file mode 100644 index 0000000..96662cd --- /dev/null +++ b/src/kernel/qcolor.h @@ -0,0 +1,229 @@ +/**************************************************************************** +** +** Definition of QColor class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QCOLOR_H +#define QCOLOR_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qstringlist.h" +#endif // QT_H + +const QRgb RGB_MASK = 0x00ffffff; // masks RGB values + +Q_EXPORT inline int qRed( QRgb rgb ) // get red part of RGB +{ return (int)((rgb >> 16) & 0xff); } + +Q_EXPORT inline int qGreen( QRgb rgb ) // get green part of RGB +{ return (int)((rgb >> 8) & 0xff); } + +Q_EXPORT inline int qBlue( QRgb rgb ) // get blue part of RGB +{ return (int)(rgb & 0xff); } + +Q_EXPORT inline int qAlpha( QRgb rgb ) // get alpha part of RGBA +{ return (int)((rgb >> 24) & 0xff); } + +Q_EXPORT inline QRgb qRgb( int r, int g, int b )// set RGB value +{ return (0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); } + +Q_EXPORT inline QRgb qRgba( int r, int g, int b, int a )// set RGBA value +{ return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); } + +Q_EXPORT inline int qGray( int r, int g, int b )// convert R,G,B to gray 0..255 +{ return (r*11+g*16+b*5)/32; } + +Q_EXPORT inline int qGray( QRgb rgb ) // convert RGB to gray 0..255 +{ return qGray( qRed(rgb), qGreen(rgb), qBlue(rgb) ); } + + +class Q_EXPORT QColor +{ +public: + enum Spec { Rgb, Hsv }; + + QColor(); + QColor( int r, int g, int b ); + QColor( int x, int y, int z, Spec ); + QColor( QRgb rgb, uint pixel=0xffffffff); + QColor( const QString& name ); + QColor( const char *name ); + QColor( const QColor & ); + QColor &operator=( const QColor & ); + + bool isValid() const; + bool isDirty() const; + QString name() const; + void setNamedColor( const QString& name ); + + QRgb rgb() const; + void setRgb( int r, int g, int b ); + void setRgb( QRgb rgb ); + void getRgb( int *r, int *g, int *b ) const { rgb( r, g, b ); } + void rgb( int *r, int *g, int *b ) const; // obsolete + + int red() const; + int green() const; + int blue() const; + + void setHsv( int h, int s, int v ); + void getHsv( int *h, int *s, int *v ) const { hsv( h, s, v ); } + void hsv( int *h, int *s, int *v ) const; // obsolete + void getHsv( int &h, int &s, int &v ) const { hsv( &h, &s, &v ); } // obsolete + + QColor light( int f = 150 ) const; + QColor dark( int f = 200 ) const; + + bool operator==( const QColor &c ) const; + bool operator!=( const QColor &c ) const; + + uint alloc(); + uint pixel() const; + +#if defined(Q_WS_X11) + // ### in 4.0, make this take a default argument of -1 for default screen? + uint alloc( int screen ); + uint pixel( int screen ) const; +#endif + + static int maxColors(); + static int numBitPlanes(); + + static int enterAllocContext(); + static void leaveAllocContext(); + static int currentAllocContext(); + static void destroyAllocContext( int ); + +#if defined(Q_WS_WIN) + static const QRgb* palette( int* numEntries = 0 ); + static int setPaletteEntries( const QRgb* entries, int numEntries, + int base = -1 ); + static HPALETTE hPal() { return hpal; } + static uint realizePal( QWidget * ); +#endif + + static void initialize(); + static void cleanup(); +#ifndef QT_NO_STRINGLIST + static QStringList colorNames(); +#endif + enum { Dirt = 0x44495254, Invalid = 0x49000000 }; + +private: + void setSystemNamedColor( const QString& name ); + void setPixel( uint pixel ); + static void initGlobalColors(); + static uint argbToPix32(QRgb); + static QColor* globalColors(); + static bool color_init; + static bool globals_init; +#if defined(Q_WS_WIN) + static HPALETTE hpal; +#endif + static enum ColorModel { d8, d32 } colormodel; + union { + QRgb argb; + struct D8 { + QRgb argb; + uchar pix; + uchar invalid; + uchar dirty; + uchar direct; + } d8; + struct D32 { + QRgb argb; + uint pix; + bool invalid() const { return argb == QColor::Invalid && pix == QColor::Dirt; } + bool probablyDirty() const { return pix == QColor::Dirt; } + } d32; + } d; +}; + + +inline QColor::QColor() +{ d.d32.argb = Invalid; d.d32.pix = Dirt; } + +inline QColor::QColor( int r, int g, int b ) +{ + d.d32.argb = Invalid; + d.d32.pix = Dirt; + setRgb( r, g, b ); +} + +inline QRgb QColor::rgb() const +{ return d.argb; } + +inline int QColor::red() const +{ return qRed(d.argb); } + +inline int QColor::green() const +{ return qGreen(d.argb); } + +inline int QColor::blue() const +{ return qBlue(d.argb); } + +inline bool QColor::isValid() const +{ + if ( colormodel == d8 ) + return !d.d8.invalid; + else + return !d.d32.invalid(); +} + +inline bool QColor::operator==( const QColor &c ) const +{ + return d.argb == c.d.argb && isValid() == c.isValid(); +} + +inline bool QColor::operator!=( const QColor &c ) const +{ + return !operator==(c); +} + + +/***************************************************************************** + QColor stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QColor & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QColor & ); +#endif + +#endif // QCOLOR_H diff --git a/src/kernel/qcolor_p.cpp b/src/kernel/qcolor_p.cpp new file mode 100644 index 0000000..266f553 --- /dev/null +++ b/src/kernel/qcolor_p.cpp @@ -0,0 +1,797 @@ +/**************************************************************************** +** +** Named color support for non-X platforms. +** The color names have been borrowed from X. +** +** Created : 000228 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qglobal.h" +#if defined(Q_CC_BOR) +// needed for qsort() because of a std namespace problem on Borland +#include "qplatformdefs.h" +#endif + +#include "qcolor.h" + +#ifndef QT_NO_COLORNAMES + +#include <stdlib.h> + +#undef QRGB +#define QRGB(r,g,b) (r*65536 + g*256 + b) + +const int rgbTblSize = 657; + +static const struct RGBData { + uint value; + const char *name; +} rgbTbl[] = { + { QRGB(240,248,255), "aliceblue" }, + { QRGB(250,235,215), "antiquewhite" }, + { QRGB(255,239,219), "antiquewhite1" }, + { QRGB(238,223,204), "antiquewhite2" }, + { QRGB(205,192,176), "antiquewhite3" }, + { QRGB(139,131,120), "antiquewhite4" }, + { QRGB(127,255,212), "aquamarine" }, + { QRGB(127,255,212), "aquamarine1" }, + { QRGB(118,238,198), "aquamarine2" }, + { QRGB(102,205,170), "aquamarine3" }, + { QRGB( 69,139,116), "aquamarine4" }, + { QRGB(240,255,255), "azure" }, + { QRGB(240,255,255), "azure1" }, + { QRGB(224,238,238), "azure2" }, + { QRGB(193,205,205), "azure3" }, + { QRGB(131,139,139), "azure4" }, + { QRGB(245,245,220), "beige" }, + { QRGB(255,228,196), "bisque" }, + { QRGB(255,228,196), "bisque1" }, + { QRGB(238,213,183), "bisque2" }, + { QRGB(205,183,158), "bisque3" }, + { QRGB(139,125,107), "bisque4" }, + { QRGB( 0, 0, 0), "black" }, + { QRGB(255,235,205), "blanchedalmond" }, + { QRGB( 0, 0,255), "blue" }, + { QRGB( 0, 0,255), "blue1" }, + { QRGB( 0, 0,238), "blue2" }, + { QRGB( 0, 0,205), "blue3" }, + { QRGB( 0, 0,139), "blue4" }, + { QRGB(138, 43,226), "blueviolet" }, + { QRGB(165, 42, 42), "brown" }, + { QRGB(255, 64, 64), "brown1" }, + { QRGB(238, 59, 59), "brown2" }, + { QRGB(205, 51, 51), "brown3" }, + { QRGB(139, 35, 35), "brown4" }, + { QRGB(222,184,135), "burlywood" }, + { QRGB(255,211,155), "burlywood1" }, + { QRGB(238,197,145), "burlywood2" }, + { QRGB(205,170,125), "burlywood3" }, + { QRGB(139,115, 85), "burlywood4" }, + { QRGB( 95,158,160), "cadetblue" }, + { QRGB(152,245,255), "cadetblue1" }, + { QRGB(142,229,238), "cadetblue2" }, + { QRGB(122,197,205), "cadetblue3" }, + { QRGB( 83,134,139), "cadetblue4" }, + { QRGB(127,255, 0), "chartreuse" }, + { QRGB(127,255, 0), "chartreuse1" }, + { QRGB(118,238, 0), "chartreuse2" }, + { QRGB(102,205, 0), "chartreuse3" }, + { QRGB( 69,139, 0), "chartreuse4" }, + { QRGB(210,105, 30), "chocolate" }, + { QRGB(255,127, 36), "chocolate1" }, + { QRGB(238,118, 33), "chocolate2" }, + { QRGB(205,102, 29), "chocolate3" }, + { QRGB(139, 69, 19), "chocolate4" }, + { QRGB(255,127, 80), "coral" }, + { QRGB(255,114, 86), "coral1" }, + { QRGB(238,106, 80), "coral2" }, + { QRGB(205, 91, 69), "coral3" }, + { QRGB(139, 62, 47), "coral4" }, + { QRGB(100,149,237), "cornflowerblue" }, + { QRGB(255,248,220), "cornsilk" }, + { QRGB(255,248,220), "cornsilk1" }, + { QRGB(238,232,205), "cornsilk2" }, + { QRGB(205,200,177), "cornsilk3" }, + { QRGB(139,136,120), "cornsilk4" }, + { QRGB( 0,255,255), "cyan" }, + { QRGB( 0,255,255), "cyan1" }, + { QRGB( 0,238,238), "cyan2" }, + { QRGB( 0,205,205), "cyan3" }, + { QRGB( 0,139,139), "cyan4" }, + { QRGB( 0, 0,139), "darkblue" }, + { QRGB( 0,139,139), "darkcyan" }, + { QRGB(184,134, 11), "darkgoldenrod" }, + { QRGB(255,185, 15), "darkgoldenrod1" }, + { QRGB(238,173, 14), "darkgoldenrod2" }, + { QRGB(205,149, 12), "darkgoldenrod3" }, + { QRGB(139,101, 8), "darkgoldenrod4" }, + { QRGB(169,169,169), "darkgray" }, + { QRGB( 0,100, 0), "darkgreen" }, + { QRGB(169,169,169), "darkgrey" }, + { QRGB(189,183,107), "darkkhaki" }, + { QRGB(139, 0,139), "darkmagenta" }, + { QRGB( 85,107, 47), "darkolivegreen" }, + { QRGB(202,255,112), "darkolivegreen1" }, + { QRGB(188,238,104), "darkolivegreen2" }, + { QRGB(162,205, 90), "darkolivegreen3" }, + { QRGB(110,139, 61), "darkolivegreen4" }, + { QRGB(255,140, 0), "darkorange" }, + { QRGB(255,127, 0), "darkorange1" }, + { QRGB(238,118, 0), "darkorange2" }, + { QRGB(205,102, 0), "darkorange3" }, + { QRGB(139, 69, 0), "darkorange4" }, + { QRGB(153, 50,204), "darkorchid" }, + { QRGB(191, 62,255), "darkorchid1" }, + { QRGB(178, 58,238), "darkorchid2" }, + { QRGB(154, 50,205), "darkorchid3" }, + { QRGB(104, 34,139), "darkorchid4" }, + { QRGB(139, 0, 0), "darkred" }, + { QRGB(233,150,122), "darksalmon" }, + { QRGB(143,188,143), "darkseagreen" }, + { QRGB(193,255,193), "darkseagreen1" }, + { QRGB(180,238,180), "darkseagreen2" }, + { QRGB(155,205,155), "darkseagreen3" }, + { QRGB(105,139,105), "darkseagreen4" }, + { QRGB( 72, 61,139), "darkslateblue" }, + { QRGB( 47, 79, 79), "darkslategray" }, + { QRGB(151,255,255), "darkslategray1" }, + { QRGB(141,238,238), "darkslategray2" }, + { QRGB(121,205,205), "darkslategray3" }, + { QRGB( 82,139,139), "darkslategray4" }, + { QRGB( 47, 79, 79), "darkslategrey" }, + { QRGB( 0,206,209), "darkturquoise" }, + { QRGB(148, 0,211), "darkviolet" }, + { QRGB(255, 20,147), "deeppink" }, + { QRGB(255, 20,147), "deeppink1" }, + { QRGB(238, 18,137), "deeppink2" }, + { QRGB(205, 16,118), "deeppink3" }, + { QRGB(139, 10, 80), "deeppink4" }, + { QRGB( 0,191,255), "deepskyblue" }, + { QRGB( 0,191,255), "deepskyblue1" }, + { QRGB( 0,178,238), "deepskyblue2" }, + { QRGB( 0,154,205), "deepskyblue3" }, + { QRGB( 0,104,139), "deepskyblue4" }, + { QRGB(105,105,105), "dimgray" }, + { QRGB(105,105,105), "dimgrey" }, + { QRGB( 30,144,255), "dodgerblue" }, + { QRGB( 30,144,255), "dodgerblue1" }, + { QRGB( 28,134,238), "dodgerblue2" }, + { QRGB( 24,116,205), "dodgerblue3" }, + { QRGB( 16, 78,139), "dodgerblue4" }, + { QRGB(178, 34, 34), "firebrick" }, + { QRGB(255, 48, 48), "firebrick1" }, + { QRGB(238, 44, 44), "firebrick2" }, + { QRGB(205, 38, 38), "firebrick3" }, + { QRGB(139, 26, 26), "firebrick4" }, + { QRGB(255,250,240), "floralwhite" }, + { QRGB( 34,139, 34), "forestgreen" }, + { QRGB(220,220,220), "gainsboro" }, + { QRGB(248,248,255), "ghostwhite" }, + { QRGB(255,215, 0), "gold" }, + { QRGB(255,215, 0), "gold1" }, + { QRGB(238,201, 0), "gold2" }, + { QRGB(205,173, 0), "gold3" }, + { QRGB(139,117, 0), "gold4" }, + { QRGB(218,165, 32), "goldenrod" }, + { QRGB(255,193, 37), "goldenrod1" }, + { QRGB(238,180, 34), "goldenrod2" }, + { QRGB(205,155, 29), "goldenrod3" }, + { QRGB(139,105, 20), "goldenrod4" }, + { QRGB(190,190,190), "gray" }, + { QRGB( 0, 0, 0), "gray0" }, + { QRGB( 3, 3, 3), "gray1" }, + { QRGB( 26, 26, 26), "gray10" }, + { QRGB(255,255,255), "gray100" }, + { QRGB( 28, 28, 28), "gray11" }, + { QRGB( 31, 31, 31), "gray12" }, + { QRGB( 33, 33, 33), "gray13" }, + { QRGB( 36, 36, 36), "gray14" }, + { QRGB( 38, 38, 38), "gray15" }, + { QRGB( 41, 41, 41), "gray16" }, + { QRGB( 43, 43, 43), "gray17" }, + { QRGB( 46, 46, 46), "gray18" }, + { QRGB( 48, 48, 48), "gray19" }, + { QRGB( 5, 5, 5), "gray2" }, + { QRGB( 51, 51, 51), "gray20" }, + { QRGB( 54, 54, 54), "gray21" }, + { QRGB( 56, 56, 56), "gray22" }, + { QRGB( 59, 59, 59), "gray23" }, + { QRGB( 61, 61, 61), "gray24" }, + { QRGB( 64, 64, 64), "gray25" }, + { QRGB( 66, 66, 66), "gray26" }, + { QRGB( 69, 69, 69), "gray27" }, + { QRGB( 71, 71, 71), "gray28" }, + { QRGB( 74, 74, 74), "gray29" }, + { QRGB( 8, 8, 8), "gray3" }, + { QRGB( 77, 77, 77), "gray30" }, + { QRGB( 79, 79, 79), "gray31" }, + { QRGB( 82, 82, 82), "gray32" }, + { QRGB( 84, 84, 84), "gray33" }, + { QRGB( 87, 87, 87), "gray34" }, + { QRGB( 89, 89, 89), "gray35" }, + { QRGB( 92, 92, 92), "gray36" }, + { QRGB( 94, 94, 94), "gray37" }, + { QRGB( 97, 97, 97), "gray38" }, + { QRGB( 99, 99, 99), "gray39" }, + { QRGB( 10, 10, 10), "gray4" }, + { QRGB(102,102,102), "gray40" }, + { QRGB(105,105,105), "gray41" }, + { QRGB(107,107,107), "gray42" }, + { QRGB(110,110,110), "gray43" }, + { QRGB(112,112,112), "gray44" }, + { QRGB(115,115,115), "gray45" }, + { QRGB(117,117,117), "gray46" }, + { QRGB(120,120,120), "gray47" }, + { QRGB(122,122,122), "gray48" }, + { QRGB(125,125,125), "gray49" }, + { QRGB( 13, 13, 13), "gray5" }, + { QRGB(127,127,127), "gray50" }, + { QRGB(130,130,130), "gray51" }, + { QRGB(133,133,133), "gray52" }, + { QRGB(135,135,135), "gray53" }, + { QRGB(138,138,138), "gray54" }, + { QRGB(140,140,140), "gray55" }, + { QRGB(143,143,143), "gray56" }, + { QRGB(145,145,145), "gray57" }, + { QRGB(148,148,148), "gray58" }, + { QRGB(150,150,150), "gray59" }, + { QRGB( 15, 15, 15), "gray6" }, + { QRGB(153,153,153), "gray60" }, + { QRGB(156,156,156), "gray61" }, + { QRGB(158,158,158), "gray62" }, + { QRGB(161,161,161), "gray63" }, + { QRGB(163,163,163), "gray64" }, + { QRGB(166,166,166), "gray65" }, + { QRGB(168,168,168), "gray66" }, + { QRGB(171,171,171), "gray67" }, + { QRGB(173,173,173), "gray68" }, + { QRGB(176,176,176), "gray69" }, + { QRGB( 18, 18, 18), "gray7" }, + { QRGB(179,179,179), "gray70" }, + { QRGB(181,181,181), "gray71" }, + { QRGB(184,184,184), "gray72" }, + { QRGB(186,186,186), "gray73" }, + { QRGB(189,189,189), "gray74" }, + { QRGB(191,191,191), "gray75" }, + { QRGB(194,194,194), "gray76" }, + { QRGB(196,196,196), "gray77" }, + { QRGB(199,199,199), "gray78" }, + { QRGB(201,201,201), "gray79" }, + { QRGB( 20, 20, 20), "gray8" }, + { QRGB(204,204,204), "gray80" }, + { QRGB(207,207,207), "gray81" }, + { QRGB(209,209,209), "gray82" }, + { QRGB(212,212,212), "gray83" }, + { QRGB(214,214,214), "gray84" }, + { QRGB(217,217,217), "gray85" }, + { QRGB(219,219,219), "gray86" }, + { QRGB(222,222,222), "gray87" }, + { QRGB(224,224,224), "gray88" }, + { QRGB(227,227,227), "gray89" }, + { QRGB( 23, 23, 23), "gray9" }, + { QRGB(229,229,229), "gray90" }, + { QRGB(232,232,232), "gray91" }, + { QRGB(235,235,235), "gray92" }, + { QRGB(237,237,237), "gray93" }, + { QRGB(240,240,240), "gray94" }, + { QRGB(242,242,242), "gray95" }, + { QRGB(245,245,245), "gray96" }, + { QRGB(247,247,247), "gray97" }, + { QRGB(250,250,250), "gray98" }, + { QRGB(252,252,252), "gray99" }, + { QRGB( 0,255, 0), "green" }, + { QRGB( 0,255, 0), "green1" }, + { QRGB( 0,238, 0), "green2" }, + { QRGB( 0,205, 0), "green3" }, + { QRGB( 0,139, 0), "green4" }, + { QRGB(173,255, 47), "greenyellow" }, + { QRGB(190,190,190), "grey" }, + { QRGB( 0, 0, 0), "grey0" }, + { QRGB( 3, 3, 3), "grey1" }, + { QRGB( 26, 26, 26), "grey10" }, + { QRGB(255,255,255), "grey100" }, + { QRGB( 28, 28, 28), "grey11" }, + { QRGB( 31, 31, 31), "grey12" }, + { QRGB( 33, 33, 33), "grey13" }, + { QRGB( 36, 36, 36), "grey14" }, + { QRGB( 38, 38, 38), "grey15" }, + { QRGB( 41, 41, 41), "grey16" }, + { QRGB( 43, 43, 43), "grey17" }, + { QRGB( 46, 46, 46), "grey18" }, + { QRGB( 48, 48, 48), "grey19" }, + { QRGB( 5, 5, 5), "grey2" }, + { QRGB( 51, 51, 51), "grey20" }, + { QRGB( 54, 54, 54), "grey21" }, + { QRGB( 56, 56, 56), "grey22" }, + { QRGB( 59, 59, 59), "grey23" }, + { QRGB( 61, 61, 61), "grey24" }, + { QRGB( 64, 64, 64), "grey25" }, + { QRGB( 66, 66, 66), "grey26" }, + { QRGB( 69, 69, 69), "grey27" }, + { QRGB( 71, 71, 71), "grey28" }, + { QRGB( 74, 74, 74), "grey29" }, + { QRGB( 8, 8, 8), "grey3" }, + { QRGB( 77, 77, 77), "grey30" }, + { QRGB( 79, 79, 79), "grey31" }, + { QRGB( 82, 82, 82), "grey32" }, + { QRGB( 84, 84, 84), "grey33" }, + { QRGB( 87, 87, 87), "grey34" }, + { QRGB( 89, 89, 89), "grey35" }, + { QRGB( 92, 92, 92), "grey36" }, + { QRGB( 94, 94, 94), "grey37" }, + { QRGB( 97, 97, 97), "grey38" }, + { QRGB( 99, 99, 99), "grey39" }, + { QRGB( 10, 10, 10), "grey4" }, + { QRGB(102,102,102), "grey40" }, + { QRGB(105,105,105), "grey41" }, + { QRGB(107,107,107), "grey42" }, + { QRGB(110,110,110), "grey43" }, + { QRGB(112,112,112), "grey44" }, + { QRGB(115,115,115), "grey45" }, + { QRGB(117,117,117), "grey46" }, + { QRGB(120,120,120), "grey47" }, + { QRGB(122,122,122), "grey48" }, + { QRGB(125,125,125), "grey49" }, + { QRGB( 13, 13, 13), "grey5" }, + { QRGB(127,127,127), "grey50" }, + { QRGB(130,130,130), "grey51" }, + { QRGB(133,133,133), "grey52" }, + { QRGB(135,135,135), "grey53" }, + { QRGB(138,138,138), "grey54" }, + { QRGB(140,140,140), "grey55" }, + { QRGB(143,143,143), "grey56" }, + { QRGB(145,145,145), "grey57" }, + { QRGB(148,148,148), "grey58" }, + { QRGB(150,150,150), "grey59" }, + { QRGB( 15, 15, 15), "grey6" }, + { QRGB(153,153,153), "grey60" }, + { QRGB(156,156,156), "grey61" }, + { QRGB(158,158,158), "grey62" }, + { QRGB(161,161,161), "grey63" }, + { QRGB(163,163,163), "grey64" }, + { QRGB(166,166,166), "grey65" }, + { QRGB(168,168,168), "grey66" }, + { QRGB(171,171,171), "grey67" }, + { QRGB(173,173,173), "grey68" }, + { QRGB(176,176,176), "grey69" }, + { QRGB( 18, 18, 18), "grey7" }, + { QRGB(179,179,179), "grey70" }, + { QRGB(181,181,181), "grey71" }, + { QRGB(184,184,184), "grey72" }, + { QRGB(186,186,186), "grey73" }, + { QRGB(189,189,189), "grey74" }, + { QRGB(191,191,191), "grey75" }, + { QRGB(194,194,194), "grey76" }, + { QRGB(196,196,196), "grey77" }, + { QRGB(199,199,199), "grey78" }, + { QRGB(201,201,201), "grey79" }, + { QRGB( 20, 20, 20), "grey8" }, + { QRGB(204,204,204), "grey80" }, + { QRGB(207,207,207), "grey81" }, + { QRGB(209,209,209), "grey82" }, + { QRGB(212,212,212), "grey83" }, + { QRGB(214,214,214), "grey84" }, + { QRGB(217,217,217), "grey85" }, + { QRGB(219,219,219), "grey86" }, + { QRGB(222,222,222), "grey87" }, + { QRGB(224,224,224), "grey88" }, + { QRGB(227,227,227), "grey89" }, + { QRGB( 23, 23, 23), "grey9" }, + { QRGB(229,229,229), "grey90" }, + { QRGB(232,232,232), "grey91" }, + { QRGB(235,235,235), "grey92" }, + { QRGB(237,237,237), "grey93" }, + { QRGB(240,240,240), "grey94" }, + { QRGB(242,242,242), "grey95" }, + { QRGB(245,245,245), "grey96" }, + { QRGB(247,247,247), "grey97" }, + { QRGB(250,250,250), "grey98" }, + { QRGB(252,252,252), "grey99" }, + { QRGB(240,255,240), "honeydew" }, + { QRGB(240,255,240), "honeydew1" }, + { QRGB(224,238,224), "honeydew2" }, + { QRGB(193,205,193), "honeydew3" }, + { QRGB(131,139,131), "honeydew4" }, + { QRGB(255,105,180), "hotpink" }, + { QRGB(255,110,180), "hotpink1" }, + { QRGB(238,106,167), "hotpink2" }, + { QRGB(205, 96,144), "hotpink3" }, + { QRGB(139, 58, 98), "hotpink4" }, + { QRGB(205, 92, 92), "indianred" }, + { QRGB(255,106,106), "indianred1" }, + { QRGB(238, 99, 99), "indianred2" }, + { QRGB(205, 85, 85), "indianred3" }, + { QRGB(139, 58, 58), "indianred4" }, + { QRGB(255,255,240), "ivory" }, + { QRGB(255,255,240), "ivory1" }, + { QRGB(238,238,224), "ivory2" }, + { QRGB(205,205,193), "ivory3" }, + { QRGB(139,139,131), "ivory4" }, + { QRGB(240,230,140), "khaki" }, + { QRGB(255,246,143), "khaki1" }, + { QRGB(238,230,133), "khaki2" }, + { QRGB(205,198,115), "khaki3" }, + { QRGB(139,134, 78), "khaki4" }, + { QRGB(230,230,250), "lavender" }, + { QRGB(255,240,245), "lavenderblush" }, + { QRGB(255,240,245), "lavenderblush1" }, + { QRGB(238,224,229), "lavenderblush2" }, + { QRGB(205,193,197), "lavenderblush3" }, + { QRGB(139,131,134), "lavenderblush4" }, + { QRGB(124,252, 0), "lawngreen" }, + { QRGB(255,250,205), "lemonchiffon" }, + { QRGB(255,250,205), "lemonchiffon1" }, + { QRGB(238,233,191), "lemonchiffon2" }, + { QRGB(205,201,165), "lemonchiffon3" }, + { QRGB(139,137,112), "lemonchiffon4" }, + { QRGB(173,216,230), "lightblue" }, + { QRGB(191,239,255), "lightblue1" }, + { QRGB(178,223,238), "lightblue2" }, + { QRGB(154,192,205), "lightblue3" }, + { QRGB(104,131,139), "lightblue4" }, + { QRGB(240,128,128), "lightcoral" }, + { QRGB(224,255,255), "lightcyan" }, + { QRGB(224,255,255), "lightcyan1" }, + { QRGB(209,238,238), "lightcyan2" }, + { QRGB(180,205,205), "lightcyan3" }, + { QRGB(122,139,139), "lightcyan4" }, + { QRGB(238,221,130), "lightgoldenrod" }, + { QRGB(255,236,139), "lightgoldenrod1" }, + { QRGB(238,220,130), "lightgoldenrod2" }, + { QRGB(205,190,112), "lightgoldenrod3" }, + { QRGB(139,129, 76), "lightgoldenrod4" }, + { QRGB(250,250,210), "lightgoldenrodyellow" }, + { QRGB(211,211,211), "lightgray" }, + { QRGB(144,238,144), "lightgreen" }, + { QRGB(211,211,211), "lightgrey" }, + { QRGB(255,182,193), "lightpink" }, + { QRGB(255,174,185), "lightpink1" }, + { QRGB(238,162,173), "lightpink2" }, + { QRGB(205,140,149), "lightpink3" }, + { QRGB(139, 95,101), "lightpink4" }, + { QRGB(255,160,122), "lightsalmon" }, + { QRGB(255,160,122), "lightsalmon1" }, + { QRGB(238,149,114), "lightsalmon2" }, + { QRGB(205,129, 98), "lightsalmon3" }, + { QRGB(139, 87, 66), "lightsalmon4" }, + { QRGB( 32,178,170), "lightseagreen" }, + { QRGB(135,206,250), "lightskyblue" }, + { QRGB(176,226,255), "lightskyblue1" }, + { QRGB(164,211,238), "lightskyblue2" }, + { QRGB(141,182,205), "lightskyblue3" }, + { QRGB( 96,123,139), "lightskyblue4" }, + { QRGB(132,112,255), "lightslateblue" }, + { QRGB(119,136,153), "lightslategray" }, + { QRGB(119,136,153), "lightslategrey" }, + { QRGB(176,196,222), "lightsteelblue" }, + { QRGB(202,225,255), "lightsteelblue1" }, + { QRGB(188,210,238), "lightsteelblue2" }, + { QRGB(162,181,205), "lightsteelblue3" }, + { QRGB(110,123,139), "lightsteelblue4" }, + { QRGB(255,255,224), "lightyellow" }, + { QRGB(255,255,224), "lightyellow1" }, + { QRGB(238,238,209), "lightyellow2" }, + { QRGB(205,205,180), "lightyellow3" }, + { QRGB(139,139,122), "lightyellow4" }, + { QRGB( 50,205, 50), "limegreen" }, + { QRGB(250,240,230), "linen" }, + { QRGB(255, 0,255), "magenta" }, + { QRGB(255, 0,255), "magenta1" }, + { QRGB(238, 0,238), "magenta2" }, + { QRGB(205, 0,205), "magenta3" }, + { QRGB(139, 0,139), "magenta4" }, + { QRGB(176, 48, 96), "maroon" }, + { QRGB(255, 52,179), "maroon1" }, + { QRGB(238, 48,167), "maroon2" }, + { QRGB(205, 41,144), "maroon3" }, + { QRGB(139, 28, 98), "maroon4" }, + { QRGB(102,205,170), "mediumaquamarine" }, + { QRGB( 0, 0,205), "mediumblue" }, + { QRGB(186, 85,211), "mediumorchid" }, + { QRGB(224,102,255), "mediumorchid1" }, + { QRGB(209, 95,238), "mediumorchid2" }, + { QRGB(180, 82,205), "mediumorchid3" }, + { QRGB(122, 55,139), "mediumorchid4" }, + { QRGB(147,112,219), "mediumpurple" }, + { QRGB(171,130,255), "mediumpurple1" }, + { QRGB(159,121,238), "mediumpurple2" }, + { QRGB(137,104,205), "mediumpurple3" }, + { QRGB( 93, 71,139), "mediumpurple4" }, + { QRGB( 60,179,113), "mediumseagreen" }, + { QRGB(123,104,238), "mediumslateblue" }, + { QRGB( 0,250,154), "mediumspringgreen" }, + { QRGB( 72,209,204), "mediumturquoise" }, + { QRGB(199, 21,133), "mediumvioletred" }, + { QRGB( 25, 25,112), "midnightblue" }, + { QRGB(245,255,250), "mintcream" }, + { QRGB(255,228,225), "mistyrose" }, + { QRGB(255,228,225), "mistyrose1" }, + { QRGB(238,213,210), "mistyrose2" }, + { QRGB(205,183,181), "mistyrose3" }, + { QRGB(139,125,123), "mistyrose4" }, + { QRGB(255,228,181), "moccasin" }, + { QRGB(255,222,173), "navajowhite" }, + { QRGB(255,222,173), "navajowhite1" }, + { QRGB(238,207,161), "navajowhite2" }, + { QRGB(205,179,139), "navajowhite3" }, + { QRGB(139,121, 94), "navajowhite4" }, + { QRGB( 0, 0,128), "navy" }, + { QRGB( 0, 0,128), "navyblue" }, + { QRGB(253,245,230), "oldlace" }, + { QRGB(107,142, 35), "olivedrab" }, + { QRGB(192,255, 62), "olivedrab1" }, + { QRGB(179,238, 58), "olivedrab2" }, + { QRGB(154,205, 50), "olivedrab3" }, + { QRGB(105,139, 34), "olivedrab4" }, + { QRGB(255,165, 0), "orange" }, + { QRGB(255,165, 0), "orange1" }, + { QRGB(238,154, 0), "orange2" }, + { QRGB(205,133, 0), "orange3" }, + { QRGB(139, 90, 0), "orange4" }, + { QRGB(255, 69, 0), "orangered" }, + { QRGB(255, 69, 0), "orangered1" }, + { QRGB(238, 64, 0), "orangered2" }, + { QRGB(205, 55, 0), "orangered3" }, + { QRGB(139, 37, 0), "orangered4" }, + { QRGB(218,112,214), "orchid" }, + { QRGB(255,131,250), "orchid1" }, + { QRGB(238,122,233), "orchid2" }, + { QRGB(205,105,201), "orchid3" }, + { QRGB(139, 71,137), "orchid4" }, + { QRGB(238,232,170), "palegoldenrod" }, + { QRGB(152,251,152), "palegreen" }, + { QRGB(154,255,154), "palegreen1" }, + { QRGB(144,238,144), "palegreen2" }, + { QRGB(124,205,124), "palegreen3" }, + { QRGB( 84,139, 84), "palegreen4" }, + { QRGB(175,238,238), "paleturquoise" }, + { QRGB(187,255,255), "paleturquoise1" }, + { QRGB(174,238,238), "paleturquoise2" }, + { QRGB(150,205,205), "paleturquoise3" }, + { QRGB(102,139,139), "paleturquoise4" }, + { QRGB(219,112,147), "palevioletred" }, + { QRGB(255,130,171), "palevioletred1" }, + { QRGB(238,121,159), "palevioletred2" }, + { QRGB(205,104,137), "palevioletred3" }, + { QRGB(139, 71, 93), "palevioletred4" }, + { QRGB(255,239,213), "papayawhip" }, + { QRGB(255,218,185), "peachpuff" }, + { QRGB(255,218,185), "peachpuff1" }, + { QRGB(238,203,173), "peachpuff2" }, + { QRGB(205,175,149), "peachpuff3" }, + { QRGB(139,119,101), "peachpuff4" }, + { QRGB(205,133, 63), "peru" }, + { QRGB(255,192,203), "pink" }, + { QRGB(255,181,197), "pink1" }, + { QRGB(238,169,184), "pink2" }, + { QRGB(205,145,158), "pink3" }, + { QRGB(139, 99,108), "pink4" }, + { QRGB(221,160,221), "plum" }, + { QRGB(255,187,255), "plum1" }, + { QRGB(238,174,238), "plum2" }, + { QRGB(205,150,205), "plum3" }, + { QRGB(139,102,139), "plum4" }, + { QRGB(176,224,230), "powderblue" }, + { QRGB(160, 32,240), "purple" }, + { QRGB(155, 48,255), "purple1" }, + { QRGB(145, 44,238), "purple2" }, + { QRGB(125, 38,205), "purple3" }, + { QRGB( 85, 26,139), "purple4" }, + { QRGB(255, 0, 0), "red" }, + { QRGB(255, 0, 0), "red1" }, + { QRGB(238, 0, 0), "red2" }, + { QRGB(205, 0, 0), "red3" }, + { QRGB(139, 0, 0), "red4" }, + { QRGB(188,143,143), "rosybrown" }, + { QRGB(255,193,193), "rosybrown1" }, + { QRGB(238,180,180), "rosybrown2" }, + { QRGB(205,155,155), "rosybrown3" }, + { QRGB(139,105,105), "rosybrown4" }, + { QRGB( 65,105,225), "royalblue" }, + { QRGB( 72,118,255), "royalblue1" }, + { QRGB( 67,110,238), "royalblue2" }, + { QRGB( 58, 95,205), "royalblue3" }, + { QRGB( 39, 64,139), "royalblue4" }, + { QRGB(139, 69, 19), "saddlebrown" }, + { QRGB(250,128,114), "salmon" }, + { QRGB(255,140,105), "salmon1" }, + { QRGB(238,130, 98), "salmon2" }, + { QRGB(205,112, 84), "salmon3" }, + { QRGB(139, 76, 57), "salmon4" }, + { QRGB(244,164, 96), "sandybrown" }, + { QRGB( 46,139, 87), "seagreen" }, + { QRGB( 84,255,159), "seagreen1" }, + { QRGB( 78,238,148), "seagreen2" }, + { QRGB( 67,205,128), "seagreen3" }, + { QRGB( 46,139, 87), "seagreen4" }, + { QRGB(255,245,238), "seashell" }, + { QRGB(255,245,238), "seashell1" }, + { QRGB(238,229,222), "seashell2" }, + { QRGB(205,197,191), "seashell3" }, + { QRGB(139,134,130), "seashell4" }, + { QRGB(160, 82, 45), "sienna" }, + { QRGB(255,130, 71), "sienna1" }, + { QRGB(238,121, 66), "sienna2" }, + { QRGB(205,104, 57), "sienna3" }, + { QRGB(139, 71, 38), "sienna4" }, + { QRGB(135,206,235), "skyblue" }, + { QRGB(135,206,255), "skyblue1" }, + { QRGB(126,192,238), "skyblue2" }, + { QRGB(108,166,205), "skyblue3" }, + { QRGB( 74,112,139), "skyblue4" }, + { QRGB(106, 90,205), "slateblue" }, + { QRGB(131,111,255), "slateblue1" }, + { QRGB(122,103,238), "slateblue2" }, + { QRGB(105, 89,205), "slateblue3" }, + { QRGB( 71, 60,139), "slateblue4" }, + { QRGB(112,128,144), "slategray" }, + { QRGB(198,226,255), "slategray1" }, + { QRGB(185,211,238), "slategray2" }, + { QRGB(159,182,205), "slategray3" }, + { QRGB(108,123,139), "slategray4" }, + { QRGB(112,128,144), "slategrey" }, + { QRGB(255,250,250), "snow" }, + { QRGB(255,250,250), "snow1" }, + { QRGB(238,233,233), "snow2" }, + { QRGB(205,201,201), "snow3" }, + { QRGB(139,137,137), "snow4" }, + { QRGB( 0,255,127), "springgreen" }, + { QRGB( 0,255,127), "springgreen1" }, + { QRGB( 0,238,118), "springgreen2" }, + { QRGB( 0,205,102), "springgreen3" }, + { QRGB( 0,139, 69), "springgreen4" }, + { QRGB( 70,130,180), "steelblue" }, + { QRGB( 99,184,255), "steelblue1" }, + { QRGB( 92,172,238), "steelblue2" }, + { QRGB( 79,148,205), "steelblue3" }, + { QRGB( 54,100,139), "steelblue4" }, + { QRGB(210,180,140), "tan" }, + { QRGB(255,165, 79), "tan1" }, + { QRGB(238,154, 73), "tan2" }, + { QRGB(205,133, 63), "tan3" }, + { QRGB(139, 90, 43), "tan4" }, + { QRGB(216,191,216), "thistle" }, + { QRGB(255,225,255), "thistle1" }, + { QRGB(238,210,238), "thistle2" }, + { QRGB(205,181,205), "thistle3" }, + { QRGB(139,123,139), "thistle4" }, + { QRGB(255, 99, 71), "tomato" }, + { QRGB(255, 99, 71), "tomato1" }, + { QRGB(238, 92, 66), "tomato2" }, + { QRGB(205, 79, 57), "tomato3" }, + { QRGB(139, 54, 38), "tomato4" }, + { QRGB( 64,224,208), "turquoise" }, + { QRGB( 0,245,255), "turquoise1" }, + { QRGB( 0,229,238), "turquoise2" }, + { QRGB( 0,197,205), "turquoise3" }, + { QRGB( 0,134,139), "turquoise4" }, + { QRGB(238,130,238), "violet" }, + { QRGB(208, 32,144), "violetred" }, + { QRGB(255, 62,150), "violetred1" }, + { QRGB(238, 58,140), "violetred2" }, + { QRGB(205, 50,120), "violetred3" }, + { QRGB(139, 34, 82), "violetred4" }, + { QRGB(245,222,179), "wheat" }, + { QRGB(255,231,186), "wheat1" }, + { QRGB(238,216,174), "wheat2" }, + { QRGB(205,186,150), "wheat3" }, + { QRGB(139,126,102), "wheat4" }, + { QRGB(255,255,255), "white" }, + { QRGB(245,245,245), "whitesmoke" }, + { QRGB(255,255, 0), "yellow" }, + { QRGB(255,255, 0), "yellow1" }, + { QRGB(238,238, 0), "yellow2" }, + { QRGB(205,205, 0), "yellow3" }, + { QRGB(139,139, 0), "yellow4" }, + { QRGB(154,205, 50), "yellowgreen" } }; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +#ifdef Q_OS_TEMP +static int __cdecl rgb_cmp( const void *d1, const void *d2 ) +#else +static int rgb_cmp( const void *d1, const void *d2 ) +#endif +{ + return qstricmp( ((RGBData *)d1)->name, ((RGBData *)d2)->name ); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +bool qt_get_named_rgb( const char *name, QRgb* rgb ) +{ + Q_LONG len = strlen(name)+1; + char *name_no_space = (char *)malloc(len); + for(Q_LONG o=0,i=0; i < len; i++) { + if(name[i] != '\t' && name[i] != ' ') + name_no_space[o++] = name[i]; + } + + RGBData x; + x.name = name_no_space; + // Funtion bsearch() is supposed to be + // void *bsearch(const void *key, const void *base, ... + // So why (char*)? Are there broken bsearch() declarations out there? + RGBData *r = (RGBData*)bsearch((char*)&x, (char*)rgbTbl, rgbTblSize, + sizeof(RGBData), rgb_cmp); + free(name_no_space); + if ( r ) { + *rgb = r->value; + return TRUE; + } else { + return FALSE; + } +} + +uint qt_get_rgb_val( const char *name ) +{ + QRgb r = 0; + qt_get_named_rgb(name,&r); + return r; +} +#ifndef QT_NO_STRINGLIST +QStringList QColor::colorNames() +{ + int i = 0; + QStringList lst; + for ( i = 0; i < rgbTblSize; i++ ) + lst << rgbTbl[i].name; + + return lst; +} +#endif +#else + +bool qt_get_named_rgb( const char *, QRgb* ) +{ + return FALSE; +} + +uint qt_get_rgb_val( const char * ) +{ + return 0; +} +#ifndef QT_NO_STRINGLIST +QStringList QColor::colorNames() +{ + return QStringList(); +} +#endif +#endif // QT_NO_COLORNAMES diff --git a/src/kernel/qcolor_p.h b/src/kernel/qcolor_p.h new file mode 100644 index 0000000..72ae2b0 --- /dev/null +++ b/src/kernel/qcolor_p.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Named color support for non-X platforms. +** The color names have been borrowed from X. +** +** Created : 000228 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QCOLOR_P_H +#define QCOLOR_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qmenudata.cpp, qmenubar.cpp, qmenubar.cpp, qpopupmenu.cpp, +// qmotifstyle.cpp and qwindowssstyle.cpp. This header file may change +// from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +extern uint qt_get_rgb_val( const char *name ); +extern bool qt_get_named_rgb( const char *, QRgb* ); +extern void qt_reset_color_avail(); + +#endif diff --git a/src/kernel/qcolor_x11.cpp b/src/kernel/qcolor_x11.cpp new file mode 100644 index 0000000..eeaee5e --- /dev/null +++ b/src/kernel/qcolor_x11.cpp @@ -0,0 +1,835 @@ +/**************************************************************************** +** +** Implementation of QColor class for X11 +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qcolor.h" +#include "qcolor_p.h" +#include "string.h" +#include "qpaintdevice.h" +#include "qapplication.h" +#include "qapplication_p.h" +#include "qt_x11_p.h" + +// NOT REVISED + +/***************************************************************************** + The color dictionary speeds up color allocation significantly for X11. + When there are no more colors, QColor::alloc() will set the colors_avail + flag to FALSE and try to find the nearest color. + NOTE: From deep within the event loop, the colors_avail flag is reset to + TRUE (calls the function qt_reset_color_avail()), because some other + application might free its colors, thereby making them available for + this Qt application. + *****************************************************************************/ + +#include "qintdict.h" + +struct QColorData { + uint pix; // allocated pixel value + int context; // allocation context +}; + +typedef QIntDict<QColorData> QColorDict; +typedef QIntDictIterator<QColorData> QColorDictIt; +static int current_alloc_context = 0; // current color alloc context +static const uint col_std_dict = 419; +static const uint col_large_dict = 18397; + +class QColorScreenData { +public: + QColorScreenData() + { + colorDict = 0; + colors_avail = TRUE; + g_vis = 0; + g_carr = 0; + g_carr_fetch = TRUE; + g_cells = 0; + g_our_alloc = 0; + color_reduce = FALSE; + } + + QColorDict *colorDict; // dict of allocated colors + bool colors_avail; // X colors available + bool g_truecolor; // truecolor visual + Visual *g_vis; // visual + XColor *g_carr; // color array + bool g_carr_fetch; // perform XQueryColors? + int g_cells; // number of entries in g_carr + bool *g_our_alloc; // our allocated colors + uint red_mask , green_mask , blue_mask; + int red_shift, green_shift, blue_shift; + bool color_reduce; + int col_div_r; + int col_div_g; + int col_div_b; +}; + +static int screencount = 0; +static QColorScreenData **screendata = 0; // array of screendata pointers + + +/* + This function is called from the event loop. It resets the colors_avail + flag so that the application can retry to allocate read-only colors + that other applications may have deallocated lately. + + The g_our_alloc and g_carr are global arrays that optimize color + approximation when there are no more colors left to allocate. +*/ + +void qt_reset_color_avail() +{ + int i; + for ( i = 0; i < screencount; i++ ) { + screendata[i]->colors_avail = TRUE; + screendata[i]->g_carr_fetch = TRUE; // do XQueryColors if !colors_avail + } +} + + +/* + Finds the nearest color. +*/ + +static int find_nearest_color( int r, int g, int b, int* mindist_out, + QColorScreenData *sd ) +{ + int mincol = -1; + int mindist = 200000; + int rx, gx, bx, dist; + XColor *xc = &sd->g_carr[0]; + for ( int i=0; i<sd->g_cells; i++ ) { + rx = r - (xc->red >> 8); + gx = g - (xc->green >> 8); + bx = b - (xc->blue>> 8); + dist = rx*rx + gx*gx + bx*bx; // calculate distance + if ( dist < mindist ) { // minimal? + mindist = dist; + mincol = i; + } + xc++; + } + *mindist_out = mindist; + return mincol; +} + + +/***************************************************************************** + QColor misc internal functions + *****************************************************************************/ + +static int highest_bit( uint v ) +{ + int i; + uint b = (uint)1 << 31; // get pos of highest bit in v + for ( i=31; ((b & v) == 0) && i>=0; i-- ) + b >>= 1; + return i; +} + + +/***************************************************************************** + QColor static member functions + *****************************************************************************/ + +/*! + Returns the maximum number of colors supported by the underlying + window system if the window system uses a palette. + + Otherwise returns -1. Use numBitPlanes() to calculate the available + colors in that case. +*/ + +int QColor::maxColors() +{ + Visual *visual = (Visual *) QPaintDevice::x11AppVisual(); + if (visual->c_class & 1) + return QPaintDevice::x11AppCells(); + return -1; +} + +/*! + Returns the number of color bit planes for the underlying window + system. + + The returned value is equal to the default pixmap depth. + + \sa QPixmap::defaultDepth() +*/ + +int QColor::numBitPlanes() +{ + return QPaintDevice::x11AppDepth(); +} + + +/*! + Internal initialization required for QColor. + This function is called from the QApplication constructor. + + \sa cleanup() +*/ + +void QColor::initialize() +{ + static const int blackIdx = 2; + static const int whiteIdx = 3; + + if ( color_init ) // already initialized + return; + color_init = TRUE; + + Display *dpy = QPaintDevice::x11AppDisplay(); + int spec = QApplication::colorSpec(); + + screencount = ScreenCount( dpy ); + screendata = new QColorScreenData*[ screencount ]; + + int scr; + for ( scr = 0; scr < screencount; ++scr ) { + screendata[scr] = new QColorScreenData; + screendata[scr]->g_vis = (Visual *) QPaintDevice::x11AppVisual( scr ); + screendata[scr]->g_truecolor = screendata[scr]->g_vis->c_class == TrueColor + || screendata[scr]->g_vis->c_class == DirectColor; + + int ncols = QPaintDevice::x11AppCells( scr ); + + if ( screendata[scr]->g_truecolor ) { + if (scr == DefaultScreen(dpy)) + colormodel = d32; + } else { + if (scr == DefaultScreen(dpy)) + colormodel = d8; + + // Create the g_our_alloc array, which remembers which color pixels + // we allocated. + screendata[scr]->g_cells = QMIN(ncols,256); + screendata[scr]->g_carr = new XColor[screendata[scr]->g_cells]; + Q_CHECK_PTR( screendata[scr]->g_carr ); + memset( screendata[scr]->g_carr, 0, + screendata[scr]->g_cells*sizeof(XColor) ); + screendata[scr]->g_carr_fetch = TRUE; // run XQueryColors on demand + screendata[scr]->g_our_alloc = new bool[screendata[scr]->g_cells]; + Q_CHECK_PTR( screendata[scr]->g_our_alloc ); + memset( screendata[scr]->g_our_alloc, FALSE, + screendata[scr]->g_cells*sizeof(bool) ); + XColor *xc = &screendata[scr]->g_carr[0]; + for ( int i=0; i<screendata[scr]->g_cells; i++ ) { + xc->pixel = i; // g_carr[i] = color i + xc++; + } + } + + int dictsize; + if ( screendata[scr]->g_truecolor ) { // truecolor + dictsize = 1; // will not need color dict + screendata[scr]->red_mask = (uint)screendata[scr]->g_vis->red_mask; + screendata[scr]->green_mask = (uint)screendata[scr]->g_vis->green_mask; + screendata[scr]->blue_mask = (uint)screendata[scr]->g_vis->blue_mask; + screendata[scr]->red_shift = + highest_bit( screendata[scr]->red_mask ) - 7; + screendata[scr]->green_shift = + highest_bit( screendata[scr]->green_mask ) - 7; + screendata[scr]->blue_shift = + highest_bit( screendata[scr]->blue_mask ) - 7; + } else { + dictsize = col_std_dict; + } + screendata[scr]->colorDict = new QColorDict(dictsize); // create dictionary + Q_CHECK_PTR( screendata[scr]->colorDict ); + + if ( spec == (int)QApplication::ManyColor ) { + screendata[scr]->color_reduce = TRUE; + + switch ( qt_ncols_option ) { + case 216: + // 6:6:6 + screendata[scr]->col_div_r = screendata[scr]->col_div_g = + screendata[scr]->col_div_b = (255/(6-1)); + break; + default: { + // 2:3:1 proportions, solved numerically + if ( qt_ncols_option > 255 ) qt_ncols_option = 255; + if ( qt_ncols_option < 1 ) qt_ncols_option = 1; + int nr = 2; + int ng = 2; + int nb = 2; + for (;;) { + if ( nb*2 < nr && (nb+1)*nr*ng < qt_ncols_option ) + nb++; + else if ( nr*3 < ng*2 && nb*(nr+1)*ng < qt_ncols_option ) + nr++; + else if ( nb*nr*(ng+1) < qt_ncols_option ) + ng++; + else break; + } + qt_ncols_option = nr*ng*nb; + screendata[scr]->col_div_r = (255/(nr-1)); + screendata[scr]->col_div_g = (255/(ng-1)); + screendata[scr]->col_div_b = (255/(nb-1)); + } + } + } + } + + scr = QPaintDevice::x11AppScreen(); + + // Initialize global color objects + if ( QPaintDevice::x11AppDefaultVisual(scr) && + QPaintDevice::x11AppDefaultColormap(scr) ) { + globalColors()[blackIdx].setPixel((uint) BlackPixel(dpy, scr)); + globalColors()[whiteIdx].setPixel((uint) WhitePixel(dpy, scr)); + } else { + globalColors()[blackIdx].alloc(scr); + globalColors()[whiteIdx].alloc(scr); + } + +#if 0 /* 0 == allocate colors on demand */ + setLazyAlloc( FALSE ); // allocate global colors + ((QColor*)(&darkGray))-> alloc(); + ((QColor*)(&gray))-> alloc(); + ((QColor*)(&lightGray))-> alloc(); + ((QColor*)(&::red))-> alloc(); + ((QColor*)(&::green))-> alloc(); + ((QColor*)(&::blue))-> alloc(); + ((QColor*)(&cyan))-> alloc(); + ((QColor*)(&magenta))-> alloc(); + ((QColor*)(&yellow))-> alloc(); + ((QColor*)(&darkRed))-> alloc(); + ((QColor*)(&darkGreen))-> alloc(); + ((QColor*)(&darkBlue))-> alloc(); + ((QColor*)(&darkCyan))-> alloc(); + ((QColor*)(&darkMagenta))-> alloc(); + ((QColor*)(&darkYellow))-> alloc(); + setLazyAlloc( TRUE ); +#endif +} + +/*! + Internal clean up required for QColor. + This function is called from the QApplication destructor. + + \sa initialize() +*/ + +void QColor::cleanup() +{ + if ( !color_init ) + return; + color_init = FALSE; + int scr; + for ( scr = 0; scr < screencount; scr++ ) { + if ( screendata[scr]->g_carr ) { + delete [] screendata[scr]->g_carr; + screendata[scr]->g_carr = 0; + } + if ( screendata[scr]->g_our_alloc ) { + delete [] screendata[scr]->g_our_alloc; + screendata[scr]->g_our_alloc = 0; + } + if ( screendata[scr]->colorDict ) { + screendata[scr]->colorDict->setAutoDelete( TRUE ); + screendata[scr]->colorDict->clear(); + delete screendata[scr]->colorDict; + screendata[scr]->colorDict = 0; + } + delete screendata[scr]; + screendata[scr] = 0; + } + delete [] screendata; + screendata = 0; + screencount = 0; +} + + +/***************************************************************************** + QColor member functions + *****************************************************************************/ + +/*! + \internal + Allocates the color on screen \a screen. Only used in X11. + + \sa alloc(), pixel() +*/ +uint QColor::alloc( int screen ) +{ + Display *dpy = QPaintDevice::x11AppDisplay(); + if ( screen < 0 ) + screen = QPaintDevice::x11AppScreen(); + if ( !color_init ) + return dpy ? (uint)BlackPixel(dpy, screen) : 0; + int r = qRed(d.argb); + int g = qGreen(d.argb); + int b = qBlue(d.argb); + uint pix = 0; + QColorScreenData *sd = screendata[screen]; + if ( sd->g_truecolor ) { // truecolor: map to pixel + r = sd->red_shift > 0 ? r << sd->red_shift : r >> -sd->red_shift; + g = sd->green_shift > 0 ? g << sd->green_shift : g >> -sd->green_shift; + b = sd->blue_shift > 0 ? b << sd->blue_shift : b >> -sd->blue_shift; + pix = (b & sd->blue_mask) | (g & sd->green_mask) | (r & sd->red_mask) + | ~(sd->blue_mask | sd->green_mask | sd->red_mask); + if ( screen == QPaintDevice::x11AppScreen() ) + d.d32.pix = pix; + return pix; + } + QColorData *c = sd->colorDict->find( (long)(d.argb) ); + if ( c ) { // found color in dictionary + pix = c->pix; + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; // color ok + d.d8.dirty = FALSE; + d.d8.pix = pix; // use same pixel value + if ( c->context != current_alloc_context ) { + c->context = 0; // convert to default context + sd->g_our_alloc[pix] = TRUE; // reuse without XAllocColor + } + } + return pix; + } + + XColor col; + col.red = r << 8; + col.green = g << 8; + col.blue = b << 8; + + bool try_again = FALSE; + bool try_alloc = !sd->color_reduce; + int try_count = 0; + + do { + // This loop is run until we manage to either allocate or + // find an approximate color, it stops after a few iterations. + + try_again = FALSE; + + if ( try_alloc && sd->colors_avail && + XAllocColor(dpy, QPaintDevice::x11AppColormap( screen ),&col) ) { + // We could allocate the color + pix = (uint) col.pixel; + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.pix = pix; + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + sd->g_carr[d.d8.pix] = col; // update color array + if ( current_alloc_context == 0 ) + sd->g_our_alloc[d.d8.pix] = TRUE; // reuse without XAllocColor + } + } else { + // No available colors, or we did not want to allocate one + int i; + sd->colors_avail = FALSE; // no more available colors + if ( sd->g_carr_fetch ) { // refetch color array + sd->g_carr_fetch = FALSE; + XQueryColors( dpy, QPaintDevice::x11AppColormap( screen ), sd->g_carr, + sd->g_cells ); + } + int mindist; + i = find_nearest_color( r, g, b, &mindist, sd ); + + if ( mindist != 0 && !try_alloc ) { + // Not an exact match with an existing color + int rr = ((r+sd->col_div_r/2)/sd->col_div_r)*sd->col_div_r; + int rg = ((g+sd->col_div_g/2)/sd->col_div_g)*sd->col_div_g; + int rb = ((b+sd->col_div_b/2)/sd->col_div_b)*sd->col_div_b; + int rx = rr - r; + int gx = rg - g; + int bx = rb - b; + int dist = rx*rx + gx*gx + bx*bx; // calculate distance + if ( dist < mindist ) { + // reduced color is closer - try to alloc it + r = rr; + g = rg; + b = rb; + col.red = r << 8; + col.green = g << 8; + col.blue = b << 8; + try_alloc = TRUE; + try_again = TRUE; + sd->colors_avail = TRUE; + continue; // Try alloc reduced color + } + } + + if ( i == -1 ) { // no nearest color?! + int unused, value; + hsv(&unused, &unused, &value); + if (value < 128) { // dark, use black + d.argb = qRgb(0,0,0); + pix = (uint)BlackPixel( dpy, screen ); + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } else { // light, use white + d.argb = qRgb(0xff,0xff,0xff); + pix = (uint)WhitePixel( dpy, screen ); + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } + return pix; + } + if ( sd->g_our_alloc[i] ) { // we've already allocated it + ; // i == g_carr[i].pixel + } else { + // Try to allocate existing color + col = sd->g_carr[i]; + if ( XAllocColor(dpy, QPaintDevice::x11AppColormap( screen ), &col) ) { + i = (uint)col.pixel; + sd->g_carr[i] = col; // update color array + if ( screen == QPaintDevice::x11AppScreen() ) { + if ( current_alloc_context == 0 ) + sd->g_our_alloc[i] = TRUE; // only in the default context + } + } else { + // Oops, it's gone again + try_count++; + try_again = TRUE; + sd->colors_avail = TRUE; + sd->g_carr_fetch = TRUE; + } + } + if ( !try_again ) { // got it + pix = (uint)sd->g_carr[i].pixel; + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; // allocated X11 color + } + } + } + + } while ( try_again && try_count < 2 ); + + if ( try_again ) { // no hope of allocating color + int unused, value; + hsv(&unused, &unused, &value); + if (value < 128) { // dark, use black + d.argb = qRgb(0,0,0); + pix = (uint)BlackPixel( dpy, screen ); + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } else { // light, use white + d.argb = qRgb(0xff,0xff,0xff); + pix = (uint)WhitePixel( dpy, screen ); + if ( screen == QPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } + return pix; + } + // All colors outside context 0 must go into the dictionary + bool many = sd->colorDict->count() >= sd->colorDict->size() * 8; + if ( many && sd->colorDict->size() == col_std_dict ) { + sd->colorDict->resize( col_large_dict ); + } + if ( !many || current_alloc_context != 0 ) { + c = new QColorData; // insert into color dict + Q_CHECK_PTR( c ); + c->pix = pix; + c->context = current_alloc_context; + sd->colorDict->insert( (long)d.argb, c ); // store color in dict + } + return pix; +} + +/*! + Allocates the RGB color and returns the pixel value. + + Allocating a color means to obtain a pixel value from the RGB + specification. The pixel value is an index into the global color + table, but should be considered an arbitrary platform-dependent value. + + The pixel() function calls alloc() if necessary, so in general you + don't need to call this function. + + \sa enterAllocContext() +*/ +// ### 4.0 - remove me? +uint QColor::alloc() +{ + return alloc( -1 ); +} + +/*! + \overload + + Returns the pixel value for screen \a screen. + + This value is used by the underlying window system to refer to a color. + It can be thought of as an index into the display hardware's color table, + but the value is an arbitrary 32-bit value. + + \sa alloc() +*/ +uint QColor::pixel( int screen ) const +{ + if (screen != QPaintDevice::x11AppScreen() && + // don't allocate color0 or color1, they have fixed pixel + // values for all screens + d.argb != qRgba(255, 255, 255, 1) && d.argb != qRgba(0, 0, 0, 1)) + return ((QColor*)this)->alloc( screen ); + return pixel(); +} + + +void QColor::setSystemNamedColor( const QString& name ) +{ + // setSystemNamedColor should look up rgb values from the built in + // color tables first (see qcolor_p.cpp), and failing that, use + // the window system's interface for translating names to rgb values... + // we do this so that things like uic can load an XPM file with named colors + // and convert it to a png without having to use window system functions... + d.argb = qt_get_rgb_val( name.latin1() ); + QRgb rgb; + if ( qt_get_named_rgb( name.latin1(), &rgb ) ) { + setRgb( qRed(rgb), qGreen(rgb), qBlue(rgb) ); + if ( colormodel == d8 ) { + d.d8.invalid = FALSE; + d.d8.dirty = TRUE; + d.d8.pix = 0; + } else { + alloc(); + } + } else if ( !color_init ) { +#if defined(QT_CHECK_STATE) + qWarning( "QColor::setSystemNamedColor: Cannot perform this operation " + "because QApplication does not exist" ); +#endif + // set color to invalid + *this = QColor(); + } else { + XColor col, hw_col; + if ( XLookupColor(QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppColormap(), name.latin1(), + &col, &hw_col) ) { + setRgb( col.red>>8, col.green>>8, col.blue>>8 ); + } else { + // set color to invalid + *this = QColor(); + } + } +} + + +#define MAX_CONTEXTS 16 +static int context_stack[MAX_CONTEXTS]; +static int context_ptr = 0; + +static void init_context_stack() +{ + static bool did_init = FALSE; + if ( !did_init ) { + did_init = TRUE; + context_stack[0] = current_alloc_context = 0; + } +} + + +/*! + Enters a color allocation context and returns a non-zero unique + identifier. + + Color allocation contexts are useful for programs that need to + allocate many colors and throw them away later, like image + viewers. The allocation context functions work for true color + displays as well as for colormap displays, except that + QColor::destroyAllocContext() does nothing for true color. + + Example: + \code + QPixmap loadPixmap( QString fileName ) + { + static int alloc_context = 0; + if ( alloc_context ) + QColor::destroyAllocContext( alloc_context ); + alloc_context = QColor::enterAllocContext(); + QPixmap pm( fileName ); + QColor::leaveAllocContext(); + return pm; + } + \endcode + + The example code loads a pixmap from file. It frees up all colors + that were allocated the last time loadPixmap() was called. + + The initial/default context is 0. Qt keeps a list of colors + associated with their allocation contexts. You can call + destroyAllocContext() to get rid of all colors that were allocated + in a specific context. + + Calling enterAllocContext() enters an allocation context. The + allocation context lasts until you call leaveAllocContext(). + QColor has an internal stack of allocation contexts. Each call to + enterAllocContex() must have a corresponding leaveAllocContext(). + + \code + // context 0 active + int c1 = QColor::enterAllocContext(); // enter context c1 + // context c1 active + int c2 = QColor::enterAllocContext(); // enter context c2 + // context c2 active + QColor::leaveAllocContext(); // leave context c2 + // context c1 active + QColor::leaveAllocContext(); // leave context c1 + // context 0 active + // Now, free all colors that were allocated in context c2 + QColor::destroyAllocContext( c2 ); + \endcode + + You may also want to set the application's color specification. + See QApplication::setColorSpec() for more information. + + \sa leaveAllocContext(), currentAllocContext(), destroyAllocContext(), + QApplication::setColorSpec() +*/ + +int QColor::enterAllocContext() +{ + static int context_seq_no = 0; + init_context_stack(); + if ( context_ptr+1 == MAX_CONTEXTS ) { +#if defined(QT_CHECK_STATE) + qWarning( "QColor::enterAllocContext: Context stack overflow" ); +#endif + return 0; + } + current_alloc_context = context_stack[++context_ptr] = ++context_seq_no; + return current_alloc_context; +} + + +/*! + Leaves a color allocation context. + + See enterAllocContext() for a detailed explanation. + + \sa enterAllocContext(), currentAllocContext() +*/ + +void QColor::leaveAllocContext() +{ + init_context_stack(); + if ( context_ptr == 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "QColor::leaveAllocContext: Context stack underflow" ); +#endif + return; + } + current_alloc_context = context_stack[--context_ptr]; +} + + +/*! + Returns the current color allocation context. + + The default context is 0. + + \sa enterAllocContext(), leaveAllocContext() +*/ + +int QColor::currentAllocContext() +{ + return current_alloc_context; +} + + +/*! + Destroys a color allocation context, \e context. + + This function deallocates all colors that were allocated in the + specified \a context. If \a context == -1, it frees up all colors + that the application has allocated. If \a context == -2, it frees + up all colors that the application has allocated, except those in + the default context. + + The function does nothing for true color displays. + + \sa enterAllocContext(), alloc() +*/ + +void QColor::destroyAllocContext( int context ) +{ + init_context_stack(); + if ( !color_init ) + return; + + int screen; + for ( screen = 0; screen < screencount; ++screen ) { + if ( screendata[screen]->g_truecolor ) + continue; + + ulong pixels[256]; + bool freeing[256]; + memset( freeing, FALSE, screendata[screen]->g_cells*sizeof(bool) ); + QColorData *d; + QColorDictIt it( *screendata[screen]->colorDict ); + int i = 0; + uint rgbv; + while ( (d=it.current()) ) { + rgbv = (uint)it.currentKey(); + if ( (d->context || context==-1) && + (d->context == context || context < 0) ) { + if ( !screendata[screen]->g_our_alloc[d->pix] && !freeing[d->pix] ) { + // will free this color + pixels[i++] = d->pix; + freeing[d->pix] = TRUE; + } + // remove from dict + screendata[screen]->colorDict->remove( (long)rgbv ); + } + ++it; + } + if ( i ) + XFreeColors( QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppColormap( screen ), + pixels, i, 0 ); + } +} diff --git a/src/kernel/qconnection.cpp b/src/kernel/qconnection.cpp new file mode 100644 index 0000000..7528382 --- /dev/null +++ b/src/kernel/qconnection.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Implementation of QConnection class +** +** Created : 930417 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qconnection.h" + +/*! \class QConnection qconnection.h + \brief The QConnection class is an internal class, used in the signal/slot mechanism. + + \internal + + Do not use this class directly in application programs. + + QObject has a list of QConnection for each signal that is connected to the + outside world. +*/ + +QConnection::QConnection( const QObject *object, int member, + const char *memberName, int memberType ) +{ + obj = (QObject *)object; + mbr = member; + mbr_name = memberName; + mbr_type = memberType; + nargs = 0; + if ( strstr(memberName,"()") == 0 ) { + const char *p = memberName; + nargs++; + while ( *p ) { + if ( *p++ == ',' ) + nargs++; + } + } +} + +/*! + \fn QConnection::~QConnection() +*/ + +/*! + \fn bool QConnection::isConnected() const +*/ + +/*! + \fn QObject *QConnection::object() const +*/ + +/*! + \fn int QConnection::member() const +*/ + +/*! + \fn const char *QConnection::memberName() const +*/ + +/*! + \fn int QConnection::numArgs() const +*/ diff --git a/src/kernel/qconnection.h b/src/kernel/qconnection.h new file mode 100644 index 0000000..a7d8755 --- /dev/null +++ b/src/kernel/qconnection.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Definition of QConnection class +** +** Created : 930417 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QCONNECTION_H +#define QCONNECTION_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +class Q_EXPORT QConnection +{ +public: + QConnection( const QObject *, int, const char *memberName, int memberType ); + ~QConnection() {} + + bool isConnected() const { return obj != 0; } + + QObject *object() const { return obj; } // get object/member pointer + int member() const { return mbr; } + const char *memberName() const { return mbr_name; } + int memberType() const { return mbr_type; } + int numArgs() const { return nargs; } + +private: + QObject *obj; // object connected to + int mbr; // member connected to + const char *mbr_name; + int mbr_type; + int nargs; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QConnection( const QConnection & ); + QConnection &operator=( const QConnection & ); +#endif +}; + +#define Q_DEFINED_QCONNECTION +#include "qwinexport.h" +#endif // QCONNECTION_H diff --git a/src/kernel/qcursor.cpp b/src/kernel/qcursor.cpp new file mode 100644 index 0000000..7956a94 --- /dev/null +++ b/src/kernel/qcursor.cpp @@ -0,0 +1,287 @@ +/**************************************************************************** +** +** Implementation of QCursor class +** +** Created : 940220 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qcursor.h" + +#ifndef QT_NO_CURSOR + +#include "qbitmap.h" +#include "qimage.h" +#include "qdatastream.h" + + +/*! + \class QCursor qcursor.h + + \brief The QCursor class provides a mouse cursor with an arbitrary + shape. + + \ingroup appearance + \ingroup shared + + This class is mainly used to create mouse cursors that are + associated with particular widgets and to get and set the position + of the mouse cursor. + + Qt has a number of standard cursor shapes, but you can also make + custom cursor shapes based on a QBitmap, a mask and a hotspot. + + To associate a cursor with a widget, use QWidget::setCursor(). To + associate a cursor with all widgets (normally for a short period + of time), use QApplication::setOverrideCursor(). + + To set a cursor shape use QCursor::setShape() or use the QCursor + constructor which takes the shape as argument, or you can use one + of the predefined cursors defined in the \l CursorShape enum. + + If you want to create a cursor with your own bitmap, either use + the QCursor constructor which takes a bitmap and a mask or the + constructor which takes a pixmap as arguments. + + To set or get the position of the mouse cursor use the static + methods QCursor::pos() and QCursor::setPos(). + + \img cursors.png Cursor Shapes + + \sa QWidget \link guibooks.html#fowler GUI Design Handbook: + Cursors\endlink + + On X11, Qt supports the \link + http://www.xfree86.org/4.3.0/Xcursor.3.html Xcursor\endlink + library, which allows for full color icon themes. The table below + shows the cursor name used for each Qt::CursorShape value. If a + cursor cannot be found using the name shown below, a standard X11 + cursor will be used instead. Note: X11 does not provide + appropriate cursors for all possible Qt::CursorShape values. It + is possible that some cursors will be taken from the Xcursor + theme, while others will use an internal bitmap cursor. + + \table + \header \i Qt::CursorShape Values \i Cursor Names + \row \i Qt::ArrowCursor \i left_ptr + \row \i Qt::UpArrowCursor \i up_arrow + \row \i Qt::CrossCursor \i cross + \row \i Qt::WaitCursor \i wait + \row \i Qt::BusyCursor \i left_ptr_watch + \row \i Qt::IbeamCursor \i ibeam + \row \i Qt::SizeVerCursor \i size_ver + \row \i Qt::SizeHorCursor \i size_hor + \row \i Qt::SizeBDiagCursor \i size_bdiag + \row \i Qt::SizeFDiagCursor \i size_fdiag + \row \i Qt::SizeAllCursor \i size_all + \row \i Qt::SplitVCursor \i split_v + \row \i Qt::SplitHCursor \i split_h + \row \i Qt::PointingHandCursor \i pointing_hand + \row \i Qt::ForbiddenCursor \i forbidden + \row \i Qt::WhatsThisCursor \i whats_this + \endtable +*/ + +/*! + \enum Qt::CursorShape + + This enum type defines the various cursors that can be used. + + \value ArrowCursor standard arrow cursor + \value UpArrowCursor upwards arrow + \value CrossCursor crosshair + \value WaitCursor hourglass/watch + \value BusyCursor standard arrow with hourglass/watch + \value IbeamCursor ibeam/text entry + \value SizeVerCursor vertical resize + \value SizeHorCursor horizontal resize + \value SizeFDiagCursor diagonal resize (\) + \value SizeBDiagCursor diagonal resize (/) + \value SizeAllCursor all directions resize + \value BlankCursor blank/invisible cursor + \value SplitVCursor vertical splitting + \value SplitHCursor horizontal splitting + \value PointingHandCursor a pointing hand + \value ForbiddenCursor a slashed circle + \value WhatsThisCursor an arrow with a question mark + \value BitmapCursor + + ArrowCursor is the default for widgets in a normal state. + + \img cursors.png Cursor Shapes +*/ + +/***************************************************************************** + QCursor stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM + + +/*! + \relates QCursor + Writes the cursor \a c to the stream \a s. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QCursor &c ) +{ + s << (Q_INT16)c.shape(); // write shape id to stream + if ( c.shape() == Qt::BitmapCursor ) { // bitmap cursor +#if !defined(QT_NO_IMAGEIO) + s << *c.bitmap() << *c.mask(); + s << c.hotSpot(); +#else + qWarning("No Image Cursor I/O"); +#endif + } + return s; +} + +/*! + \relates QCursor + Reads a cursor from the stream \a s and sets \a c to the read data. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QCursor &c ) +{ + Q_INT16 shape; + s >> shape; // read shape id from stream + if ( shape == Qt::BitmapCursor ) { // read bitmap cursor +#if !defined(QT_NO_IMAGEIO) + QBitmap bm, bmm; + QPoint hot; + s >> bm >> bmm >> hot; + c = QCursor( bm, bmm, hot.x(), hot.y() ); +#else + qWarning("No Image Cursor I/O"); +#endif + } else { + c.setShape( (int)shape ); // create cursor with shape + } + return s; +} +#endif // QT_NO_DATASTREAM + + +/*! + Constructs a custom pixmap cursor. + + \a pixmap is the image. It is usual to give it a mask (set using + QPixmap::setMask()). \a hotX and \a hotY define the cursor's hot + spot. + + If \a hotX is negative, it is set to the \c{pixmap().width()/2}. + If \a hotY is negative, it is set to the \c{pixmap().height()/2}. + + Valid cursor sizes depend on the display hardware (or the + underlying window system). We recommend using 32x32 cursors, + because this size is supported on all platforms. Some platforms + also support 16x16, 48x48 and 64x64 cursors. + + Currently, only black-and-white pixmaps can be used. + + \sa QPixmap::QPixmap(), QPixmap::setMask() +*/ + +QCursor::QCursor( const QPixmap &pixmap, int hotX, int hotY ) +{ + QImage img = pixmap.convertToImage(). + convertDepth( 8, Qt::ThresholdDither|Qt::AvoidDither ); + QBitmap bm; + bm.convertFromImage( img, Qt::ThresholdDither|Qt::AvoidDither ); + QBitmap bmm; + if ( bm.mask() ) { + bmm = *bm.mask(); + QBitmap nullBm; + bm.setMask( nullBm ); + } + else if ( pixmap.mask() ) { + QImage mimg = pixmap.mask()->convertToImage(). + convertDepth( 8, Qt::ThresholdDither|Qt::AvoidDither ); + bmm.convertFromImage( mimg, Qt::ThresholdDither|Qt::AvoidDither ); + } + else { + bmm.resize( bm.size() ); + bmm.fill( Qt::color1 ); + } + + setBitmap(bm,bmm,hotX,hotY); +} + + + +/*! + Constructs a custom bitmap cursor. + + \a bitmap and + \a mask make up the bitmap. + \a hotX and + \a hotY define the cursor's hot spot. + + If \a hotX is negative, it is set to the \c{bitmap().width()/2}. + If \a hotY is negative, it is set to the \c{bitmap().height()/2}. + + The cursor \a bitmap (B) and \a mask (M) bits are combined like this: + \list + \i B=1 and M=1 gives black. + \i B=0 and M=1 gives white. + \i B=0 and M=0 gives transparent. + \i B=1 and M=0 gives an undefined result. + \endlist + + Use the global Qt color \c color0 to draw 0-pixels and \c color1 to + draw 1-pixels in the bitmaps. + + Valid cursor sizes depend on the display hardware (or the + underlying window system). We recommend using 32x32 cursors, + because this size is supported on all platforms. Some platforms + also support 16x16, 48x48 and 64x64 cursors. + + \sa QBitmap::QBitmap(), QBitmap::setMask() +*/ + +QCursor::QCursor( const QBitmap &bitmap, const QBitmap &mask, + int hotX, int hotY ) +{ + setBitmap(bitmap,mask,hotX,hotY); +} + +#endif // QT_NO_CURSOR + + diff --git a/src/kernel/qcursor.h b/src/kernel/qcursor.h new file mode 100644 index 0000000..9b99edb --- /dev/null +++ b/src/kernel/qcursor.h @@ -0,0 +1,153 @@ +/**************************************************************************** +** +** Definition of QCursor class +** +** Created : 940219 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QCURSOR_H +#define QCURSOR_H + +#ifndef QT_H +#include "qpoint.h" +#include "qshared.h" +#endif // QT_H + +/* + ### The fake cursor has to go first with old qdoc. +*/ +#ifdef QT_NO_CURSOR + +class Q_EXPORT QCursor : public Qt +{ +public: + static QPoint pos(); + static void setPos( int x, int y ); + static void setPos( const QPoint & ); +private: + QCursor(); +}; + +#endif // QT_NO_CURSOR + +#ifndef QT_NO_CURSOR + +struct QCursorData; + + +class Q_EXPORT QCursor : public Qt +{ +public: + QCursor(); // create default arrow cursor + QCursor( int shape ); + QCursor( const QBitmap &bitmap, const QBitmap &mask, + int hotX=-1, int hotY=-1 ); + QCursor( const QPixmap &pixmap, + int hotX=-1, int hotY=-1 ); + QCursor( const QCursor & ); + ~QCursor(); + QCursor &operator=( const QCursor & ); + + int shape() const; + void setShape( int ); + + const QBitmap *bitmap() const; + const QBitmap *mask() const; + QPoint hotSpot() const; + +#if defined(Q_WS_WIN) + HCURSOR handle() const; + QCursor( HCURSOR ); +#elif defined(Q_WS_X11) + HANDLE handle() const; + QCursor( HANDLE ); +#elif defined(Q_WS_MAC) + HANDLE handle() const; +#elif defined(Q_WS_QWS) + HANDLE handle() const; +#endif + + static QPoint pos(); + static void setPos( int x, int y ); + static void setPos( const QPoint & ); + + static void initialize(); + static void cleanup(); + +#if defined(Q_WS_X11) + static int x11Screen(); +#endif +private: + void setBitmap( const QBitmap &bitmap, const QBitmap &mask, + int hotX, int hotY ); + void update() const; + QCursorData *data; + QCursor *find_cur(int); +#if defined(Q_WS_MAC) + friend void qt_mac_set_cursor(const QCursor *c, const Point *p); +#endif +}; + + +#if !defined(QT_CLEAN_NAMESPACE) +// CursorShape is defined in X11/X.h +#ifdef CursorShape +#define X_CursorShape CursorShape +#undef CursorShape +#endif +typedef Qt::CursorShape QCursorShape; +#ifdef X_CursorShape +#define CursorShape X_CursorShape +#endif +#endif + + +/***************************************************************************** + QCursor stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QCursor & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QCursor & ); +#endif +#endif // QT_NO_CURSOR + + +inline void QCursor::setPos( const QPoint &p ) +{ + setPos( p.x(), p.y() ); +} + +#endif // QCURSOR_H diff --git a/src/kernel/qcursor_x11.cpp b/src/kernel/qcursor_x11.cpp new file mode 100644 index 0000000..7d359a3 --- /dev/null +++ b/src/kernel/qcursor_x11.cpp @@ -0,0 +1,833 @@ +/**************************************************************************** +** +** Implementation of QCursor class for X11 +** +** Created : 940219 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qcursor.h" +#include "qbitmap.h" +#include "qimage.h" +#include "qapplication.h" +#include "qdatastream.h" +#include "qnamespace.h" +#include "qt_x11_p.h" +#include <X11/cursorfont.h> + +#ifndef QT_NO_XCURSOR +# include <X11/Xcursor/Xcursor.h> +#endif // QT_NO_XCURSOR + +// Define QT_USE_APPROXIMATE_CURSORS when compiling if you REALLY want to +// use the ugly X11 cursors. + +/***************************************************************************** + Internal QCursorData class + *****************************************************************************/ + +struct QCursorData : public QShared +{ + QCursorData( int s = 0 ); + ~QCursorData(); + int cshape; + QBitmap *bm, *bmm; + short hx, hy; + XColor fg,bg; + Cursor hcurs; + Pixmap pm, pmm; +}; + +QCursorData::QCursorData( int s ) +{ + cshape = s; + hcurs = 0; + bm = bmm = 0; + hx = hy = 0; + pm = pmm = 0; +} + +QCursorData::~QCursorData() +{ + Display *dpy = QPaintDevice::x11AppDisplay(); + + // Add in checking for the display too as on HP-UX + // we seem to get a core dump as the cursor data is + // deleted again from main() on exit... + if ( hcurs && dpy ) + XFreeCursor( dpy, hcurs ); + if ( pm && dpy ) + XFreePixmap( dpy, pm ); + if ( pmm && dpy ) + XFreePixmap( dpy, pmm ); + delete bm; + delete bmm; +} + + +/***************************************************************************** + Global cursors + *****************************************************************************/ + +static QCursor cursorTable[Qt::LastCursor+1]; + +static const int arrowCursorIdx = 0; + +QT_STATIC_CONST_IMPL QCursor & Qt::arrowCursor = cursorTable[0]; +QT_STATIC_CONST_IMPL QCursor & Qt::upArrowCursor = cursorTable[1]; +QT_STATIC_CONST_IMPL QCursor & Qt::crossCursor = cursorTable[2]; +QT_STATIC_CONST_IMPL QCursor & Qt::waitCursor = cursorTable[3]; +QT_STATIC_CONST_IMPL QCursor & Qt::ibeamCursor = cursorTable[4]; +QT_STATIC_CONST_IMPL QCursor & Qt::sizeVerCursor = cursorTable[5]; +QT_STATIC_CONST_IMPL QCursor & Qt::sizeHorCursor = cursorTable[6]; +QT_STATIC_CONST_IMPL QCursor & Qt::sizeBDiagCursor = cursorTable[7]; +QT_STATIC_CONST_IMPL QCursor & Qt::sizeFDiagCursor = cursorTable[8]; +QT_STATIC_CONST_IMPL QCursor & Qt::sizeAllCursor = cursorTable[9]; +QT_STATIC_CONST_IMPL QCursor & Qt::blankCursor = cursorTable[10]; +QT_STATIC_CONST_IMPL QCursor & Qt::splitVCursor = cursorTable[11]; +QT_STATIC_CONST_IMPL QCursor & Qt::splitHCursor = cursorTable[12]; +QT_STATIC_CONST_IMPL QCursor & Qt::pointingHandCursor = cursorTable[13]; +QT_STATIC_CONST_IMPL QCursor & Qt::forbiddenCursor = cursorTable[14]; +QT_STATIC_CONST_IMPL QCursor & Qt::whatsThisCursor = cursorTable[15]; +QT_STATIC_CONST_IMPL QCursor & Qt::busyCursor = cursorTable[16]; + + +QCursor *QCursor::find_cur( int shape ) // find predefined cursor +{ + return (uint)shape <= LastCursor ? &cursorTable[shape] : 0; +} + + +static bool initialized = FALSE; + +/*! + Internal function that deinitializes the predefined cursors. + This function is called from the QApplication destructor. + + \sa initialize() +*/ +void QCursor::cleanup() +{ + if ( !initialized ) + return; + + int shape; + for( shape = 0; shape <= LastCursor; shape++ ) { + if ( cursorTable[shape].data && cursorTable[shape].data->deref() ) + delete cursorTable[shape].data; + cursorTable[shape].data = 0; + } + initialized = FALSE; +} + + +/*! + Internal function that initializes the predefined cursors. + This function is called from the QApplication constructor. + + \sa cleanup() +*/ + +void QCursor::initialize() +{ + int shape; + for( shape = 0; shape <= LastCursor; shape++ ) + cursorTable[shape].data = new QCursorData( shape ); + initialized = TRUE; + qAddPostRoutine( cleanup ); +} + + +/*! + Constructs a cursor with the default arrow shape. +*/ +QCursor::QCursor() +{ + if ( !initialized ) { + if ( qApp->startingUp() ) { + data = 0; + return; + } + initialize(); + } + QCursor* c = &cursorTable[arrowCursorIdx]; + c->data->ref(); + data = c->data; +} + + + +/*! + Constructs a cursor with the specified \a shape. + + See \l CursorShape for a list of shapes. + + \sa setShape() +*/ + +QCursor::QCursor( int shape ) +{ + if ( !initialized ) + initialize(); + QCursor *c = find_cur( shape ); + if ( !c ) // not found + c = &cursorTable[arrowCursorIdx]; // then use arrowCursor + c->data->ref(); + data = c->data; +} + +/*! + Constructs a cursor from the window system cursor \a cursor. + + \warning Using this function is not portable. This function is only + available on X11 and Windows. +*/ +QCursor::QCursor( HANDLE cursor ) +{ + if ( !initialized ) + initialize(); + + data = new QCursorData; + Q_CHECK_PTR( data ); + data->hcurs = cursor; +} + + + +void QCursor::setBitmap( const QBitmap &bitmap, const QBitmap &mask, + int hotX, int hotY ) +{ + if ( !initialized ) + initialize(); + if ( bitmap.depth() != 1 || mask.depth() != 1 || + bitmap.size() != mask.size() ) { +#if defined(QT_CHECK_NULL) + qWarning( "QCursor: Cannot create bitmap cursor; invalid bitmap(s)" ); +#endif + QCursor *c = &cursorTable[arrowCursorIdx]; + c->data->ref(); + data = c->data; + return; + } + data = new QCursorData; + Q_CHECK_PTR( data ); + data->bm = new QBitmap( bitmap ); + data->bmm = new QBitmap( mask ); + data->hcurs = 0; + data->cshape = BitmapCursor; + data->hx = hotX >= 0 ? hotX : bitmap.width()/2; + data->hy = hotY >= 0 ? hotY : bitmap.height()/2; + data->fg.red = 0 << 8; + data->fg.green = 0 << 8; + data->fg.blue = 0 << 8; + data->bg.red = 255 << 8; + data->bg.green = 255 << 8; + data->bg.blue = 255 << 8; + update(); // Xcursor's backward compatibility hack needs the cursor to be created + // right after the bitmaps are created and filled with data +} + + +/*! + Constructs a copy of the cursor \a c. +*/ + +QCursor::QCursor( const QCursor &c ) +{ + if ( !initialized ) + initialize(); + data = c.data; // shallow copy + data->ref(); +} + +/*! + Destroys the cursor. +*/ + +QCursor::~QCursor() +{ + if ( data && data->deref() ) + delete data; +} + + +/*! + Assigns \a c to this cursor and returns a reference to this + cursor. +*/ + +QCursor &QCursor::operator=( const QCursor &c ) +{ + if ( !initialized ) + initialize(); + c.data->ref(); // avoid c = c + if ( data->deref() ) + delete data; + data = c.data; + return *this; +} + + +/*! + Returns the cursor shape identifier. The return value is one of + the \l CursorShape enum values (cast to an int). + + \sa setShape() +*/ + +int QCursor::shape() const +{ + if ( !initialized ) + initialize(); + return data->cshape; +} + +/*! + Sets the cursor to the shape identified by \a shape. + + See \l CursorShape for the list of cursor shapes. + + \sa shape() +*/ + +void QCursor::setShape( int shape ) +{ + if ( !initialized ) + initialize(); + QCursor *c = find_cur( shape ); // find one of the global ones + if ( !c ) // not found + c = &cursorTable[arrowCursorIdx]; // then use arrowCursor + c->data->ref(); + if ( data->deref() ) // make shallow copy + delete data; + data = c->data; +} + + +/*! + Returns the cursor bitmap, or 0 if it is one of the standard + cursors. +*/ +const QBitmap *QCursor::bitmap() const +{ + if ( !initialized ) + initialize(); + return data->bm; +} + +/*! + Returns the cursor bitmap mask, or 0 if it is one of the standard + cursors. +*/ + +const QBitmap *QCursor::mask() const +{ + if ( !initialized ) + initialize(); + return data->bmm; +} + +/*! + Returns the cursor hot spot, or (0, 0) if it is one of the + standard cursors. +*/ + +QPoint QCursor::hotSpot() const +{ + if ( !initialized ) + initialize(); + return QPoint( data->hx, data->hy ); +} + + +/*! + Returns the window system cursor handle. + + \warning + Portable in principle, but if you use it you are probably about to + do something non-portable. Be careful. +*/ + +Qt::HANDLE QCursor::handle() const +{ + if ( !initialized ) + initialize(); + if ( !data->hcurs ) + update(); + return data->hcurs; +} + +/*! + \fn QCursor::QCursor( HCURSOR handle ) + + Creates a cursor with the specified window system handle \a + handle. + + \warning + Portable in principle, but if you use it you are probably about to + do something non-portable. Be careful. +*/ + +/*! + Returns the position of the cursor (hot spot) in global screen + coordinates. + + You can call QWidget::mapFromGlobal() to translate it to widget + coordinates. + + \sa setPos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal() +*/ +QPoint QCursor::pos() +{ + Window root; + Window child; + int root_x, root_y, win_x, win_y; + uint buttons; + Display* dpy = QPaintDevice::x11AppDisplay(); + for ( int i = 0; i < ScreenCount( dpy ); i++ ) { + if ( XQueryPointer( dpy, QPaintDevice::x11AppRootWindow( i ), &root, &child, + &root_x, &root_y, &win_x, &win_y, &buttons ) ) + + return QPoint( root_x, root_y ); + } + return QPoint(); +} + +/*! \internal +*/ +int QCursor::x11Screen() +{ + Window root; + Window child; + int root_x, root_y, win_x, win_y; + uint buttons; + Display* dpy = QPaintDevice::x11AppDisplay(); + for ( int i = 0; i < ScreenCount( dpy ); i++ ) { + if ( XQueryPointer( dpy, QPaintDevice::x11AppRootWindow( i ), &root, &child, + &root_x, &root_y, &win_x, &win_y, &buttons ) ) + return i; + } + return -1; +} + +/*! + Moves the cursor (hot spot) to the global screen position (\a x, + \a y). + + You can call QWidget::mapToGlobal() to translate widget + coordinates to global screen coordinates. + + \sa pos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal() +*/ + +void QCursor::setPos( int x, int y ) +{ + QPoint current, target(x, y); + + // this is copied from pos(), since we need the screen number for the correct + // root window in the XWarpPointer call + Window root; + Window child; + int root_x, root_y, win_x, win_y; + uint buttons; + Display* dpy = QPaintDevice::x11AppDisplay(); + int screen; + for ( screen = 0; screen < ScreenCount( dpy ); screen++ ) { + if ( XQueryPointer( dpy, QPaintDevice::x11AppRootWindow( screen ), &root, &child, + &root_x, &root_y, &win_x, &win_y, &buttons ) ) { + current = QPoint( root_x, root_y ); + break; + } + } + + if ( screen >= ScreenCount( dpy ) ) + return; + + // Need to check, since some X servers generate null mouse move + // events, causing looping in applications which call setPos() on + // every mouse move event. + // + if ( current == target ) + return; + + XWarpPointer( QPaintDevice::x11AppDisplay(), None, + QPaintDevice::x11AppRootWindow( screen ), + 0, 0, 0, 0, x, y ); +} + +/*! + \overload void QCursor::setPos ( const QPoint & ) +*/ + + +/*! + \internal + + Creates the cursor. +*/ + +void QCursor::update() const +{ + if ( !initialized ) + initialize(); + register QCursorData *d = data; // cheat const! + if ( d->hcurs ) // already loaded + return; + + Display *dpy = QPaintDevice::x11AppDisplay(); + Window rootwin = QPaintDevice::x11AppRootWindow(); + + if ( d->cshape == BitmapCursor ) { + d->hcurs = XCreatePixmapCursor( dpy, d->bm->handle(), d->bmm->handle(), + &d->fg, &d->bg, d->hx, d->hy ); + return; + } + +#ifndef QT_NO_XCURSOR + static const char *cursorNames[] = { + "left_ptr", + "up_arrow", + "cross", + "wait", + "ibeam", + "size_ver", + "size_hor", + "size_bdiag", + "size_fdiag", + "size_all", + "blank", + "split_v", + "split_h", + "pointing_hand", + "forbidden", + "whats_this", + "left_ptr_watch" + }; + + d->hcurs = XcursorLibraryLoadCursor( dpy, cursorNames[d->cshape] ); + if ( d->hcurs ) + return; +#endif // QT_NO_XCURSOR + + static uchar cur_blank_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + + // Non-standard X11 cursors are created from bitmaps + +#ifndef QT_USE_APPROXIMATE_CURSORS + static const uchar cur_ver_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f, + 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0xf0, 0x0f, + 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01, 0x00, 0x00 }; + static const uchar mcur_ver_bits[] = { + 0x00, 0x00, 0x80, 0x03, 0xc0, 0x07, 0xe0, 0x0f, 0xf0, 0x1f, 0xf8, 0x3f, + 0xfc, 0x7f, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xfc, 0x7f, 0xf8, 0x3f, + 0xf0, 0x1f, 0xe0, 0x0f, 0xc0, 0x07, 0x80, 0x03 }; + static const uchar cur_hor_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x30, 0x18, + 0x38, 0x38, 0xfc, 0x7f, 0xfc, 0x7f, 0x38, 0x38, 0x30, 0x18, 0x20, 0x08, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar mcur_hor_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x40, 0x04, 0x60, 0x0c, 0x70, 0x1c, 0x78, 0x3c, + 0xfc, 0x7f, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfc, 0x7f, 0x78, 0x3c, + 0x70, 0x1c, 0x60, 0x0c, 0x40, 0x04, 0x00, 0x00 }; + static const uchar cur_bdiag_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x3e, 0x00, 0x3c, 0x00, 0x3e, + 0x00, 0x37, 0x88, 0x23, 0xd8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0xf8, 0x00, + 0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar mcur_bdiag_bits[] = { + 0x00, 0x00, 0xc0, 0x7f, 0x80, 0x7f, 0x00, 0x7f, 0x00, 0x7e, 0x04, 0x7f, + 0x8c, 0x7f, 0xdc, 0x77, 0xfc, 0x63, 0xfc, 0x41, 0xfc, 0x00, 0xfc, 0x01, + 0xfc, 0x03, 0xfc, 0x07, 0x00, 0x00, 0x00, 0x00 }; + static const uchar cur_fdiag_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, + 0xf8, 0x00, 0xd8, 0x01, 0x88, 0x23, 0x00, 0x37, 0x00, 0x3e, 0x00, 0x3c, + 0x00, 0x3e, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00 }; + static const uchar mcur_fdiag_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xfc, 0x03, 0xfc, 0x01, 0xfc, 0x00, + 0xfc, 0x41, 0xfc, 0x63, 0xdc, 0x77, 0x8c, 0x7f, 0x04, 0x7f, 0x00, 0x7e, + 0x00, 0x7f, 0x80, 0x7f, 0xc0, 0x7f, 0x00, 0x00 }; + static const uchar *cursor_bits16[] = { + cur_ver_bits, mcur_ver_bits, cur_hor_bits, mcur_hor_bits, + cur_bdiag_bits, mcur_bdiag_bits, cur_fdiag_bits, mcur_fdiag_bits, + 0, 0, cur_blank_bits, cur_blank_bits }; + + static const uchar vsplit_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xe0, 0x03, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar vsplitm_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0xf0, 0x07, 0x00, + 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, + 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, + 0x80, 0xff, 0xff, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xf0, 0x07, 0x00, + 0x00, 0xe0, 0x03, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar hsplit_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x41, 0x82, 0x00, 0x80, 0x41, 0x82, 0x01, 0xc0, 0x7f, 0xfe, 0x03, + 0x80, 0x41, 0x82, 0x01, 0x00, 0x41, 0x82, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar hsplitm_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, + 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe2, 0x47, 0x00, 0x00, 0xe3, 0xc7, 0x00, + 0x80, 0xe3, 0xc7, 0x01, 0xc0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x07, + 0xc0, 0xff, 0xff, 0x03, 0x80, 0xe3, 0xc7, 0x01, 0x00, 0xe3, 0xc7, 0x00, + 0x00, 0xe2, 0x47, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, + 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar whatsthis_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, 0xf0, 0x07, 0x00, + 0x09, 0x18, 0x0e, 0x00, 0x11, 0x1c, 0x0e, 0x00, 0x21, 0x1c, 0x0e, 0x00, + 0x41, 0x1c, 0x0e, 0x00, 0x81, 0x1c, 0x0e, 0x00, 0x01, 0x01, 0x07, 0x00, + 0x01, 0x82, 0x03, 0x00, 0xc1, 0xc7, 0x01, 0x00, 0x49, 0xc0, 0x01, 0x00, + 0x95, 0xc0, 0x01, 0x00, 0x93, 0xc0, 0x01, 0x00, 0x21, 0x01, 0x00, 0x00, + 0x20, 0xc1, 0x01, 0x00, 0x40, 0xc2, 0x01, 0x00, 0x40, 0x02, 0x00, 0x00, + 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; + static const uchar whatsthism_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x07, 0x00, 0x07, 0xf8, 0x0f, 0x00, + 0x0f, 0xfc, 0x1f, 0x00, 0x1f, 0x3e, 0x1f, 0x00, 0x3f, 0x3e, 0x1f, 0x00, + 0x7f, 0x3e, 0x1f, 0x00, 0xff, 0x3e, 0x1f, 0x00, 0xff, 0x9d, 0x0f, 0x00, + 0xff, 0xc3, 0x07, 0x00, 0xff, 0xe7, 0x03, 0x00, 0x7f, 0xe0, 0x03, 0x00, + 0xf7, 0xe0, 0x03, 0x00, 0xf3, 0xe0, 0x03, 0x00, 0xe1, 0xe1, 0x03, 0x00, + 0xe0, 0xe1, 0x03, 0x00, 0xc0, 0xe3, 0x03, 0x00, 0xc0, 0xe3, 0x03, 0x00, + 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; + static const uchar busy_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, + 0x41, 0xe0, 0xff, 0x00, 0x81, 0x20, 0x80, 0x00, 0x01, 0xe1, 0xff, 0x00, + 0x01, 0x42, 0x40, 0x00, 0xc1, 0x47, 0x40, 0x00, 0x49, 0x40, 0x55, 0x00, + 0x95, 0x80, 0x2a, 0x00, 0x93, 0x00, 0x15, 0x00, 0x21, 0x01, 0x0a, 0x00, + 0x20, 0x01, 0x11, 0x00, 0x40, 0x82, 0x20, 0x00, 0x40, 0x42, 0x44, 0x00, + 0x80, 0x41, 0x4a, 0x00, 0x00, 0x40, 0x55, 0x00, 0x00, 0xe0, 0xff, 0x00, + 0x00, 0x20, 0x80, 0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + static const uchar busym_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x0f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, + 0x7f, 0xe0, 0xff, 0x00, 0xff, 0xe0, 0xff, 0x00, 0xff, 0xe1, 0xff, 0x00, + 0xff, 0xc3, 0x7f, 0x00, 0xff, 0xc7, 0x7f, 0x00, 0x7f, 0xc0, 0x7f, 0x00, + 0xf7, 0x80, 0x3f, 0x00, 0xf3, 0x00, 0x1f, 0x00, 0xe1, 0x01, 0x0e, 0x00, + 0xe0, 0x01, 0x1f, 0x00, 0xc0, 0x83, 0x3f, 0x00, 0xc0, 0xc3, 0x7f, 0x00, + 0x80, 0xc1, 0x7f, 0x00, 0x00, 0xc0, 0x7f, 0x00, 0x00, 0xe0, 0xff, 0x00, + 0x00, 0xe0, 0xff, 0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + static const uchar * const cursor_bits32[] = { + vsplit_bits, vsplitm_bits, hsplit_bits, hsplitm_bits, + 0, 0, 0, 0, whatsthis_bits, whatsthism_bits, busy_bits, busym_bits + }; + + static const uchar forbidden_bits[] = { + 0x00,0x00,0x00,0x80,0x1f,0x00,0xe0,0x7f,0x00,0xf0,0xf0,0x00,0x38,0xc0,0x01, + 0x7c,0x80,0x03,0xec,0x00,0x03,0xce,0x01,0x07,0x86,0x03,0x06,0x06,0x07,0x06, + 0x06,0x0e,0x06,0x06,0x1c,0x06,0x0e,0x38,0x07,0x0c,0x70,0x03,0x1c,0xe0,0x03, + 0x38,0xc0,0x01,0xf0,0xe0,0x00,0xe0,0x7f,0x00,0x80,0x1f,0x00,0x00,0x00,0x00 }; + + static const unsigned char forbiddenm_bits[] = { + 0x80,0x1f,0x00,0xe0,0x7f,0x00,0xf0,0xff,0x00,0xf8,0xff,0x01,0xfc,0xf0,0x03, + 0xfe,0xc0,0x07,0xfe,0x81,0x07,0xff,0x83,0x0f,0xcf,0x07,0x0f,0x8f,0x0f,0x0f, + 0x0f,0x1f,0x0f,0x0f,0x3e,0x0f,0x1f,0xfc,0x0f,0x1e,0xf8,0x07,0x3e,0xf0,0x07, + 0xfc,0xe0,0x03,0xf8,0xff,0x01,0xf0,0xff,0x00,0xe0,0x7f,0x00,0x80,0x1f,0x00}; + + static const uchar * const cursor_bits20[] = { + forbidden_bits, forbiddenm_bits + }; + + if ( d->cshape >= SizeVerCursor && d->cshape < SizeAllCursor || + d->cshape == BlankCursor ) { + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + int i = (d->cshape - SizeVerCursor)*2; + d->pm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits16[i], + 16, 16 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits16[i+1], + 16,16); + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, &bg, 8, 8 ); + return; + } + if ( ( d->cshape >= SplitVCursor && d->cshape <= SplitHCursor ) || + d->cshape == WhatsThisCursor || d->cshape == BusyCursor ) { + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + int i = (d->cshape - SplitVCursor)*2; + d->pm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits32[i], + 32, 32 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits32[i+1], + 32, 32); + int hs = ( d->cshape == PointingHandCursor || + d->cshape == WhatsThisCursor || + d->cshape == BusyCursor ) ? 0 : 16; + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, &bg, hs, hs ); + return; + } + if ( d->cshape == ForbiddenCursor ) { + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + int i = (d->cshape - ForbiddenCursor)*2; + d->pm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits20[i], + 20, 20 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits20[i+1], + 20, 20); + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, &bg, 10, 10 ); + return; + } +#endif /* ! QT_USE_APPROXIMATE_CURSORS */ + + uint sh; + switch ( d->cshape ) { // map Q cursor to X cursor + case ArrowCursor: + sh = XC_left_ptr; + break; + case UpArrowCursor: + sh = XC_center_ptr; + break; + case CrossCursor: + sh = XC_crosshair; + break; + case WaitCursor: + sh = XC_watch; + break; + case IbeamCursor: + sh = XC_xterm; + break; + case SizeAllCursor: + sh = XC_fleur; + break; + case PointingHandCursor: + sh = XC_hand2; + break; +#ifdef QT_USE_APPROXIMATE_CURSORS + case SizeBDiagCursor: + sh = XC_top_right_corner; + break; + case SizeFDiagCursor: + sh = XC_bottom_right_corner; + break; + case BlankCursor: + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + d->pm = XCreateBitmapFromData( dpy, rootwin, + (char *)cur_blank_bits, 16, 16 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, + (char *)cur_blank_bits, 16,16); + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, + &bg, 8, 8 ); + return; + break; + case SizeVerCursor: + case SplitVCursor: + sh = XC_sb_v_double_arrow; + break; + case SizeHorCursor: + case SplitHCursor: + sh = XC_sb_h_double_arrow; + break; + case WhatsThisCursor: + sh = XC_question_arrow; + break; + case ForbiddenCursor: + sh = XC_circle; + break; + case BusyCursor: + sh = XC_watch; + break; +#endif /* QT_USE_APPROXIMATE_CURSORS */ + default: +#if defined(QT_CHECK_RANGE) + qWarning( "QCursor::update: Invalid cursor shape %d", d->cshape ); +#endif + return; + } + d->hcurs = XCreateFontCursor( dpy, sh ); +} diff --git a/src/kernel/qdesktopwidget.h b/src/kernel/qdesktopwidget.h new file mode 100644 index 0000000..f8c1f0d --- /dev/null +++ b/src/kernel/qdesktopwidget.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Definition of QDesktopWidget class. +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QDESKTOPWIDGET_H +#define QDESKTOPWIDGET_H + +#ifndef QT_H +#include "qwidget.h" +#endif // QT_H + +class QApplication; +class QDesktopWidgetPrivate; /* Don't touch! */ + +class Q_EXPORT QDesktopWidget : public QWidget +{ + Q_OBJECT +public: + QDesktopWidget(); + ~QDesktopWidget(); + + bool isVirtualDesktop() const; + + int numScreens() const; + int primaryScreen() const; + + int screenNumber( QWidget *widget = 0 ) const; // ### 4.0: const QWidget* + int screenNumber( const QPoint & ) const; + + QWidget *screen( int screen = -1 ); + + const QRect& screenGeometry( int screen = -1 ) const; + const QRect& screenGeometry( QWidget *widget ) const + { return screenGeometry( screenNumber( widget ) ); } + const QRect& screenGeometry( const QPoint &point ) const + { return screenGeometry( screenNumber( point ) ); } + + const QRect& availableGeometry( int screen = -1 ) const; + const QRect& availableGeometry( QWidget *widget ) const + { return availableGeometry( screenNumber( widget ) ); } + const QRect& availableGeometry( const QPoint &point ) const + { return availableGeometry( screenNumber( point ) ); } + + void insertChild( QObject * ); + +signals: + void resized( int ); + void workAreaResized( int ); + +protected: + void resizeEvent( QResizeEvent *e ); + +private: + QDesktopWidgetPrivate *d; + +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QDesktopWidget( const QDesktopWidget & ); + QDesktopWidget &operator=( const QDesktopWidget & ); +#endif + + friend class QApplication; +#ifdef Q_WS_QWS + friend class QWSDisplay; +#endif +}; + +#endif //QDESKTOPWIDGET_H diff --git a/src/kernel/qdesktopwidget_x11.cpp b/src/kernel/qdesktopwidget_x11.cpp new file mode 100644 index 0000000..1ab32a6 --- /dev/null +++ b/src/kernel/qdesktopwidget_x11.cpp @@ -0,0 +1,355 @@ +/**************************************************************************** +** +** Implementation of QDesktopWidget class. +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qdesktopwidget.h" +#include "qapplication.h" +#include "qobjectlist.h" +#include "qt_x11_p.h" + +// defined in qwidget_x11.cpp +extern int qt_x11_create_desktop_on_screen; + +// defined in qapplication_x11.cpp +extern Atom qt_net_workarea; +extern bool qt_net_supports(Atom atom); + +// function to update the workarea of the screen +static bool qt_desktopwidget_workarea_dirty = TRUE; +void qt_desktopwidget_update_workarea() +{ + qt_desktopwidget_workarea_dirty = TRUE; +} + + +class QSingleDesktopWidget : public QWidget +{ +public: + QSingleDesktopWidget(); + ~QSingleDesktopWidget(); +}; + +QSingleDesktopWidget::QSingleDesktopWidget() + : QWidget( 0, "desktop", WType_Desktop ) +{ +} + +QSingleDesktopWidget::~QSingleDesktopWidget() +{ + while ( children() ) + removeChild( children()->getFirst() ); +} + + +class QDesktopWidgetPrivate +{ +public: + QDesktopWidgetPrivate(); + ~QDesktopWidgetPrivate(); + + void init(); + + bool use_xinerama; + int defaultScreen; + int screenCount; + + QWidget **screens; + QRect *rects; + QRect *workareas; +}; + +QDesktopWidgetPrivate::QDesktopWidgetPrivate() + : use_xinerama(FALSE), defaultScreen(0), screenCount(1), + screens( 0 ), rects( 0 ), workareas( 0 ) +{ +} + +QDesktopWidgetPrivate::~QDesktopWidgetPrivate() +{ + if ( screens ) { + for ( int i = 0; i < screenCount; ++i ) { + if (i == defaultScreen) continue; + delete screens[i]; + screens[i] = 0; + } + + free(screens); + } + + if ( rects ) delete [] rects; + if ( workareas ) delete [] workareas; +} + +void QDesktopWidgetPrivate::init() +{ + // get the screen count + int newScreenCount; + +#ifndef QT_NO_XINERAMA + XineramaScreenInfo *xinerama_screeninfo = 0; + int unused; + use_xinerama = (XineramaQueryExtension(QPaintDevice::x11AppDisplay(), + &unused, &unused) && + XineramaIsActive(QPaintDevice::x11AppDisplay())); + + if (use_xinerama) { + xinerama_screeninfo = + XineramaQueryScreens(QPaintDevice::x11AppDisplay(), &newScreenCount); + + if (xinerama_screeninfo) + defaultScreen = 0; + } else +#endif // QT_NO_XINERAMA + { + defaultScreen = DefaultScreen(QPaintDevice::x11AppDisplay()); + newScreenCount = ScreenCount(QPaintDevice::x11AppDisplay()); + use_xinerama = false; + } + + delete [] rects; + rects = new QRect[ newScreenCount ]; + delete [] workareas; + workareas = new QRect[ newScreenCount ]; + + // get the geometry of each screen + int i, j, x, y, w, h; + for ( i = 0, j = 0; i < newScreenCount; i++ ) { + +#ifndef QT_NO_XINERAMA + if (use_xinerama) { + x = xinerama_screeninfo[i].x_org; + y = xinerama_screeninfo[i].y_org; + w = xinerama_screeninfo[i].width; + h = xinerama_screeninfo[i].height; + } else +#endif // QT_NO_XINERAMA + { + x = 0; + y = 0; + w = WidthOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), i)); + h = HeightOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), i)); + } + + workareas[i] = QRect(); + rects[j].setRect(x, y, w, h); + + // overlapping? + if (j > 0 && rects[j-1].intersects(rects[j])) { + // pick the bigger one, ignore the other + if ((rects[j].width()*rects[j].height()) > + (rects[j-1].width()*rects[j-1].height())) + rects[j-1] = rects[j]; + } + else + j++; + } + + if (screens) { + // leaks QWidget* pointers on purpose, can't delete them as pointer escapes + screens = (QWidget**) realloc(screens, j * sizeof(QWidget*)); + if (j > screenCount) + memset(&screens[screenCount], 0, (j-screenCount) * sizeof(QWidget*)); + } + + screenCount = j; + +#ifndef QT_NO_XINERAMA + if (use_xinerama && screenCount == 1) + use_xinerama = false; + + if (xinerama_screeninfo) + XFree(xinerama_screeninfo); +#endif // QT_NO_XINERAMA + +} + +// the QDesktopWidget itself will be created on the default screen +// as qt_x11_create_desktop_on_screen defaults to -1 +QDesktopWidget::QDesktopWidget() + : QWidget( 0, "desktop", WType_Desktop ) +{ + d = new QDesktopWidgetPrivate(); + + /* + we don't call d->init() here, since the initial resize event + will end up calling init() a second time, which is inefficient. + instead, for the sending of all posted event to the desktop + widget (including the initial resize event, which calls + d->init()). + */ + QApplication::sendPostedEvents( this, 0 ); +} + +QDesktopWidget::~QDesktopWidget() +{ + delete d; +} + +bool QDesktopWidget::isVirtualDesktop() const +{ + return d->use_xinerama; +} + +int QDesktopWidget::primaryScreen() const +{ + return d->defaultScreen; +} + +int QDesktopWidget::numScreens() const +{ + return d->screenCount; +} + +QWidget *QDesktopWidget::screen( int screen ) +{ + if (d->use_xinerama) + return this; + + if ( screen < 0 || screen >= d->screenCount ) + screen = d->defaultScreen; + + if ( ! d->screens ) { + d->screens = (QWidget**) calloc( d->screenCount, sizeof(QWidget*)); + d->screens[ d->defaultScreen ] = this; + } + + if ( ! d->screens[screen] || // not created yet + ! d->screens[screen]->isDesktop() ) { // reparented away + qt_x11_create_desktop_on_screen = screen; + d->screens[screen] = new QSingleDesktopWidget; + qt_x11_create_desktop_on_screen = -1; + } + + return d->screens[screen]; +} + +const QRect& QDesktopWidget::availableGeometry( int screen ) const +{ + if ( qt_desktopwidget_workarea_dirty ) { + // the workareas are dirty, invalidate them + for ( int i = 0; i < d->screenCount; ++i ) + d->workareas[i] = QRect(); + qt_desktopwidget_workarea_dirty = FALSE; + } + + if ( screen < 0 || screen >= d->screenCount ) + screen = d->defaultScreen; + + if ( d->workareas[screen].isValid() ) + return d->workareas[screen]; + + if ( ! isVirtualDesktop() && qt_net_supports( qt_net_workarea ) ) { + Atom ret; + int format, e; + unsigned char *data = 0; + unsigned long nitems, after; + + e = XGetWindowProperty( QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppRootWindow( screen ), + qt_net_workarea, 0, 4, False, XA_CARDINAL, + &ret, &format, &nitems, &after, &data ); + + if (e == Success && ret == XA_CARDINAL && + format == 32 && nitems == 4) { + long *workarea = (long *) data; + d->workareas[screen].setRect( workarea[0], workarea[1], + workarea[2], workarea[3] ); + } else { + d->workareas[screen] = screenGeometry(screen); + } + if ( data ) + XFree( data ); + } else { + d->workareas[screen] = screenGeometry(screen); + } + + return d->workareas[screen]; +} + +const QRect& QDesktopWidget::screenGeometry( int screen ) const +{ + if ( screen < 0 || screen >= d->screenCount ) + screen = d->defaultScreen; + + return d->rects[ screen ]; +} + +int QDesktopWidget::screenNumber( QWidget *widget ) const +{ + if ( !widget ) + return d->defaultScreen; + +#ifndef QT_NO_XINERAMA + if (d->use_xinerama) { + // this is how we do it for xinerama + QRect frame = widget->frameGeometry(); + if ( !widget->isTopLevel() ) + frame.moveTopLeft( widget->mapToGlobal( QPoint( 0, 0 ) ) ); + + int maxSize = -1; + int maxScreen = -1; + + for ( int i = 0; i < d->screenCount; ++i ) { + QRect sect = d->rects[i].intersect( frame ); + int size = sect.width() * sect.height(); + if ( size > maxSize && sect.width() > 0 && sect.height() > 0 ) { + maxSize = size; + maxScreen = i; + } + } + return maxScreen; + } +#endif // QT_NO_XINERAMA + + return widget->x11Screen(); +} + +int QDesktopWidget::screenNumber( const QPoint &point ) const +{ + for ( int i = 0; i < d->screenCount; ++i ) { + if ( d->rects[i].contains( point ) ) + return i; + } + return -1; +} + +void QDesktopWidget::resizeEvent( QResizeEvent *event ) +{ + d->init(); + qt_desktopwidget_workarea_dirty = TRUE; + QWidget::resizeEvent( event ); +} diff --git a/src/kernel/qdnd_x11.cpp b/src/kernel/qdnd_x11.cpp new file mode 100644 index 0000000..607a358 --- /dev/null +++ b/src/kernel/qdnd_x11.cpp @@ -0,0 +1,1859 @@ +/**************************************************************************** +** +** XDND implementation for Qt. See http://www.cco.caltech.edu/~jafl/xdnd/ +** +** Created : 980320 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +#include "qapplication.h" + +#ifndef QT_NO_DRAGANDDROP + +#include "qwidget.h" +#include "qintdict.h" +#include "qdatetime.h" +#include "qdict.h" +#include "qguardedptr.h" +#include "qdragobject.h" +#include "qobjectlist.h" +#include "qcursor.h" +#include "qbitmap.h" +#include "qpainter.h" + +#include "qt_x11_p.h" + +// conflict resolution + +const int XKeyPress = KeyPress; +const int XKeyRelease = KeyRelease; +#undef KeyPress +#undef KeyRelease + +// this stuff is copied from qapp_x11.cpp + +extern void qt_x11_intern_atom( const char *, Atom * ); + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +extern void qt_ignore_badwindow(); +extern bool qt_badwindow(); +extern void qt_enter_modal( QWidget *widget ); +extern void qt_leave_modal( QWidget *widget ); + +#if defined(Q_C_CALLBACKS) +} +#endif + +extern Window qt_x11_findClientWindow( Window, Atom, bool ); +extern Atom qt_wm_state; +extern Time qt_x_time; +extern Time qt_x_user_time; + +// this stuff is copied from qclb_x11.cpp + +extern bool qt_xclb_wait_for_event( Display *dpy, Window win, int type, + XEvent *event, int timeout ); +extern bool qt_xclb_read_property( Display *dpy, Window win, Atom property, + bool deleteProperty, + QByteArray *buffer, int *size, Atom *type, + int *format, bool nullterm ); +extern QByteArray qt_xclb_read_incremental_property( Display *dpy, Window win, + Atom property, + int nbytes, bool nullterm ); +// and all this stuff is copied -into- qapp_x11.cpp + +void qt_xdnd_setup(); +void qt_handle_xdnd_enter( QWidget *, const XEvent *, bool ); +void qt_handle_xdnd_position( QWidget *, const XEvent *, bool ); +void qt_handle_xdnd_status( QWidget *, const XEvent *, bool ); +void qt_handle_xdnd_leave( QWidget *, const XEvent *, bool ); +void qt_handle_xdnd_drop( QWidget *, const XEvent *, bool ); +void qt_handle_xdnd_finished( QWidget *, const XEvent *, bool ); +void qt_xdnd_handle_selection_request( const XSelectionRequestEvent * ); +bool qt_xdnd_handle_badwindow(); +// client messages +Atom qt_xdnd_enter; +Atom qt_xdnd_position; +Atom qt_xdnd_status; +Atom qt_xdnd_leave; +Atom qt_xdnd_drop; +Atom qt_xdnd_finished; +Atom qt_xdnd_type_list; +const int qt_xdnd_version = 4; + +extern int qt_x11_translateButtonState( int s ); + +// Actions +// +// The Xdnd spec allows for user-defined actions. This could be implemented +// with a registration process in Qt. WE SHOULD do that later. +// +Atom qt_xdnd_action_copy; +Atom qt_xdnd_action_link; +Atom qt_xdnd_action_move; +Atom qt_xdnd_action_private; +static +QDropEvent::Action xdndaction_to_qtaction(Atom atom) +{ + if ( atom == qt_xdnd_action_copy || atom == 0 ) + return QDropEvent::Copy; + if ( atom == qt_xdnd_action_link ) + return QDropEvent::Link; + if ( atom == qt_xdnd_action_move ) + return QDropEvent::Move; + return QDropEvent::Private; +} +static +int qtaction_to_xdndaction(QDropEvent::Action a) +{ + switch ( a ) { + case QDropEvent::Copy: + return qt_xdnd_action_copy; + case QDropEvent::Link: + return qt_xdnd_action_link; + case QDropEvent::Move: + return qt_xdnd_action_move; + case QDropEvent::Private: + return qt_xdnd_action_private; + default: + return qt_xdnd_action_copy; + } +} + +// clean up the stuff used. +static void qt_xdnd_cleanup(); + +static void qt_xdnd_send_leave(); + +// XDND selection +Atom qt_xdnd_selection; +// other selection +static Atom qt_selection_property; +// INCR +static Atom qt_incr_atom; + +// properties for XDND drop sites +Atom qt_xdnd_aware; +Atom qt_xdnd_proxy; + +// real variables: +// xid of current drag source +static Atom qt_xdnd_dragsource_xid = 0; + +// the types in this drop. 100 is no good, but at least it's big. +const int qt_xdnd_max_type = 100; +static Atom qt_xdnd_types[qt_xdnd_max_type]; + +static QIntDict<QCString> * qt_xdnd_drag_types = 0; +static QDict<Atom> * qt_xdnd_atom_numbers = 0; + +// timer used when target wants "continuous" move messages (eg. scroll) +static int heartbeat = -1; +// rectangle in which the answer will be the same +static QRect qt_xdnd_source_sameanswer; +//static QRect qt_xdnd_target_sameanswer; +static bool qt_xdnd_target_answerwas; +// top-level window we sent position to last. +static Window qt_xdnd_current_target; +// window to send events to (always valid if qt_xdnd_current_target) +static Window qt_xdnd_current_proxy_target; +// widget we forwarded position to last, and local position +static QGuardedPtr<QWidget> qt_xdnd_current_widget; +static QPoint qt_xdnd_current_position; +// time of this drop, as type Atom to save on casts +static Atom qt_xdnd_source_current_time; +// timestamp from the XdndPosition and XdndDrop +static Time qt_xdnd_target_current_time; +// screen number containing the pointer... -1 means default +static int qt_xdnd_current_screen = -1; +// state of dragging... true if dragging, false if not +bool qt_xdnd_dragging = FALSE; +// need to check state of keyboard modifiers +static bool need_modifiers_check = FALSE; + +// dict of payload data, sorted by type atom +static QIntDict<QByteArray> * qt_xdnd_target_data = 0; + +// first drag object, or 0 +static QDragObject * qt_xdnd_source_object = 0; + +// Motif dnd +extern void qt_motifdnd_enable( QWidget *, bool ); +extern QByteArray qt_motifdnd_obtain_data( const char *format ); +extern const char *qt_motifdnd_format( int n ); + +bool qt_motifdnd_active = FALSE; +static bool dndCancelled = FALSE; + +// Shift/Ctrl handling, and final drop status +static QDragObject::DragMode drag_mode; +static QDropEvent::Action global_requested_action = QDropEvent::Copy; +static QDropEvent::Action global_accepted_action = QDropEvent::Copy; + +// for embedding only +static QWidget* current_embedding_widget = 0; +static XEvent last_enter_event; + +// cursors +static QCursor *noDropCursor = 0; +static QCursor *moveCursor = 0; +static QCursor *copyCursor = 0; +static QCursor *linkCursor = 0; + +static QPixmap *defaultPm = 0; + +static const int default_pm_hotx = -2; +static const int default_pm_hoty = -16; +static const char* const default_pm[] = { +"13 9 3 1", +". c None", +" c #000000", +"X c #FFFFFF", +"X X X X X X X", +" X X X X X X ", +"X ......... X", +" X.........X ", +"X ......... X", +" X.........X ", +"X ......... X", +" X X X X X X ", +"X X X X X X X" +}; + +class QShapedPixmapWidget : public QWidget { + +public: + QShapedPixmapWidget(int screen = -1) : + QWidget(QApplication::desktop()->screen( screen ), + 0, WStyle_Customize | WStyle_Tool | WStyle_NoBorder | WX11BypassWM ), oldpmser( 0 ), oldbmser( 0 ) + { + x11SetWindowType( X11WindowTypeDND ); + } + + void setPixmap(QPixmap pm, QPoint hot) + { + int bmser = pm.mask() ? pm.mask()->serialNumber() : 0; + if( oldpmser == pm.serialNumber() && oldbmser == bmser + && oldhot == hot ) + return; + oldpmser = pm.serialNumber(); + oldbmser = bmser; + oldhot = hot; + bool hotspot_in = !(hot.x() < 0 || hot.y() < 0 || hot.x() >= pm.width() || hot.y() >= pm.height()); +// if the pixmap has hotspot in its area, make a "hole" in it at that position +// this will allow XTranslateCoordinates() to find directly the window below the cursor instead +// of finding this pixmap, and therefore there won't be needed any (slow) search for the window +// using findRealWindow() + if( hotspot_in ) { + QBitmap mask = pm.mask() ? *pm.mask() : QBitmap( pm.width(), pm.height()); + if( !pm.mask()) + mask.fill( Qt::color1 ); + QPainter p( &mask ); + p.setPen( Qt::color0 ); + p.drawPoint( hot.x(), hot.y()); + p.end(); + pm.setMask( mask ); + setMask( mask ); + } else if ( pm.mask() ) { + setMask( *pm.mask() ); + } else { + clearMask(); + } + resize(pm.width(),pm.height()); + setErasePixmap(pm); + erase(); + } +private: + int oldpmser; + int oldbmser; + QPoint oldhot; +}; + +static QShapedPixmapWidget * qt_xdnd_deco = 0; + +static QWidget* desktop_proxy = 0; + +class QExtraWidget : public QWidget +{ +public: + QWExtra* extraData() { return QWidget::extraData(); } + QTLWExtra* topData() { return QWidget::topData(); } +}; + + +static bool qt_xdnd_enable( QWidget* w, bool on ) +{ + if ( on ) { + QWidget * xdnd_widget = 0; + if ( w->isDesktop() ) { + if ( desktop_proxy ) // *WE* already have one. + return FALSE; + + // As per Xdnd4, use XdndProxy + XGrabServer( w->x11Display() ); + Atom type = None; + int f; + unsigned long n, a; + WId *proxy_id_ptr; + XGetWindowProperty( w->x11Display(), w->winId(), + qt_xdnd_proxy, 0, 1, False, + XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id_ptr ); + WId proxy_id = 0; + if ( type == XA_WINDOW && proxy_id_ptr ) { + proxy_id = *proxy_id_ptr; + XFree(proxy_id_ptr); + proxy_id_ptr = 0; + // Already exists. Real? + qt_ignore_badwindow(); + XGetWindowProperty( w->x11Display(), proxy_id, + qt_xdnd_proxy, 0, 1, False, + XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id_ptr ); + if ( qt_badwindow() || type != XA_WINDOW || !proxy_id_ptr || *proxy_id_ptr != proxy_id ) { + // Bogus - we will overwrite. + proxy_id = 0; + } + } + if ( proxy_id_ptr ) + XFree(proxy_id_ptr); + + if ( !proxy_id ) { + xdnd_widget = desktop_proxy = new QWidget; + proxy_id = desktop_proxy->winId(); + XChangeProperty ( w->x11Display(), + w->winId(), qt_xdnd_proxy, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&proxy_id, 1 ); + XChangeProperty ( w->x11Display(), + proxy_id, qt_xdnd_proxy, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&proxy_id, 1 ); + } + + XUngrabServer( w->x11Display() ); + } else { + xdnd_widget = w->topLevelWidget(); + } + if ( xdnd_widget ) { + Atom atm = (Atom)qt_xdnd_version; + XChangeProperty ( xdnd_widget->x11Display(), xdnd_widget->winId(), + qt_xdnd_aware, XA_ATOM, 32, PropModeReplace, + (unsigned char *)&atm, 1 ); + return TRUE; + } else { + return FALSE; + } + } else { + if ( w->isDesktop() ) { + XDeleteProperty( w->x11Display(), w->winId(), + qt_xdnd_proxy ); + delete desktop_proxy; + desktop_proxy = 0; + } + return TRUE; + } +} + +const char* qt_xdnd_atom_to_str( Atom a ) +{ + if ( !a ) return 0; + + if ( a == XA_STRING ) + return "text/plain"; // some Xdnd clients are dumb + + if ( !qt_xdnd_drag_types ) { + qt_xdnd_drag_types = new QIntDict<QCString>( 17 ); + qt_xdnd_drag_types->setAutoDelete( TRUE ); + } + QCString* result; + if ( !(result=qt_xdnd_drag_types->find( a )) ) { + const char* mimeType = XGetAtomName( QPaintDevice::x11AppDisplay(), a ); + if ( !mimeType ) + return 0; // only happens on protocol error + result = new QCString( mimeType ); + qt_xdnd_drag_types->insert( (long)a, result ); + XFree((void*)mimeType); + } + return *result; +} + +Atom* qt_xdnd_str_to_atom( const char *mimeType ) +{ + if ( !mimeType || !*mimeType ) + return 0; + if ( !qt_xdnd_atom_numbers ) { + qt_xdnd_atom_numbers = new QDict<Atom>( 17 ); + qt_xdnd_atom_numbers->setAutoDelete( TRUE ); + } + + Atom * result; + if ( (result = qt_xdnd_atom_numbers->find( mimeType )) ) + return result; + + result = new Atom; + *result = 0; + qt_x11_intern_atom( mimeType, result ); + qt_xdnd_atom_numbers->insert( mimeType, result ); + qt_xdnd_atom_to_str( *result ); + + return result; +} + + +void qt_xdnd_setup() { + // set up protocol atoms + qt_x11_intern_atom( "XdndEnter", &qt_xdnd_enter ); + qt_x11_intern_atom( "XdndPosition", &qt_xdnd_position ); + qt_x11_intern_atom( "XdndStatus", &qt_xdnd_status ); + qt_x11_intern_atom( "XdndLeave", &qt_xdnd_leave ); + qt_x11_intern_atom( "XdndDrop", &qt_xdnd_drop ); + qt_x11_intern_atom( "XdndFinished", &qt_xdnd_finished ); + qt_x11_intern_atom( "XdndTypeList", &qt_xdnd_type_list ); + + qt_x11_intern_atom( "XdndSelection", &qt_xdnd_selection ); + + qt_x11_intern_atom( "XdndAware", &qt_xdnd_aware ); + qt_x11_intern_atom( "XdndProxy", &qt_xdnd_proxy ); + + + qt_x11_intern_atom( "XdndActionCopy", &qt_xdnd_action_copy ); + qt_x11_intern_atom( "XdndActionLink", &qt_xdnd_action_link ); + qt_x11_intern_atom( "XdndActionMove", &qt_xdnd_action_move ); + qt_x11_intern_atom( "XdndActionPrivate", &qt_xdnd_action_private ); + + qt_x11_intern_atom( "QT_SELECTION", &qt_selection_property ); + qt_x11_intern_atom( "INCR", &qt_incr_atom ); + + qAddPostRoutine( qt_xdnd_cleanup ); +} + + +void qt_xdnd_cleanup() +{ + delete qt_xdnd_drag_types; + qt_xdnd_drag_types = 0; + delete qt_xdnd_atom_numbers; + qt_xdnd_atom_numbers = 0; + delete qt_xdnd_target_data; + qt_xdnd_target_data = 0; + delete noDropCursor; + noDropCursor = 0; + delete copyCursor; + copyCursor = 0; + delete moveCursor; + moveCursor = 0; + delete linkCursor; + linkCursor = 0; + delete defaultPm; + defaultPm = 0; + delete desktop_proxy; + desktop_proxy = 0; +} + + +static QWidget * find_child( QWidget * tlw, QPoint & p ) +{ + QWidget * w = tlw; + + p = w->mapFromGlobal( p ); + bool done = FALSE; + while ( !done ) { + done = TRUE; + if ( ((QExtraWidget*)w)->extraData() && + ((QExtraWidget*)w)->extraData()->xDndProxy != 0 ) + break; // stop searching for widgets under the mouse cursor if found widget is a proxy. + if ( w->children() ) { + QObjectListIt it( *w->children() ); + it.toLast(); + QObject * o; + while( (o=it.current()) ) { + --it; + if ( o->isWidgetType() && + ((QWidget*)o)->isVisible() && + ((QWidget*)o)->geometry().contains( p ) && + !((QWidget*)o)->isTopLevel()) { + w = (QWidget *)o; + done = FALSE; + p = w->mapFromParent( p ); + break; + } + } + } + } + return w; +} + + +static bool checkEmbedded(QWidget* w, const XEvent* xe) +{ + if (!w) + return FALSE; + + if (current_embedding_widget != 0 && current_embedding_widget != w) { + qt_xdnd_current_target = ((QExtraWidget*)current_embedding_widget)->extraData()->xDndProxy; + qt_xdnd_current_proxy_target = qt_xdnd_current_target; + qt_xdnd_send_leave(); + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; + current_embedding_widget = 0; + } + + QWExtra* extra = ((QExtraWidget*)w)->extraData(); + if ( extra && extra->xDndProxy != 0 ) { + + if (current_embedding_widget != w) { + + last_enter_event.xany.window = extra->xDndProxy; + XSendEvent( QPaintDevice::x11AppDisplay(), extra->xDndProxy, False, NoEventMask, + &last_enter_event ); + current_embedding_widget = w; + } + + ((XEvent*)xe)->xany.window = extra->xDndProxy; + XSendEvent( QPaintDevice::x11AppDisplay(), extra->xDndProxy, False, NoEventMask, + (XEvent*)xe ); + qt_xdnd_current_widget = w; + return TRUE; + } + current_embedding_widget = 0; + return FALSE; +} + +void qt_handle_xdnd_enter( QWidget *, const XEvent * xe, bool /*passive*/ ) +{ + //if ( !w->neveHadAChildWithDropEventsOn() ) + //return; // haven't been set up for dnd + + qt_motifdnd_active = FALSE; + + last_enter_event.xclient = xe->xclient; + + qt_xdnd_target_answerwas = FALSE; + + const long *l = xe->xclient.data.l; + int version = (int)(((unsigned long)(l[1])) >> 24); + + if ( version > qt_xdnd_version ) + return; + + qt_xdnd_dragsource_xid = l[0]; + + int j = 0; + if ( l[1] & 1 ) { + // get the types from XdndTypeList + Atom type = None; + int f; + unsigned long n, a; + Atom *data; + XGetWindowProperty( QPaintDevice::x11AppDisplay(), qt_xdnd_dragsource_xid, + qt_xdnd_type_list, 0, + qt_xdnd_max_type, False, XA_ATOM, &type, &f,&n,&a,(uchar**)&data ); + for ( ; j<qt_xdnd_max_type && j < (int)n; j++ ) { + qt_xdnd_types[j] = data[j]; + } + if ( data ) + XFree( (uchar*)data ); + } else { + // get the types from the message + int i; + for( i=2; i < 5; i++ ) { + qt_xdnd_types[j++] = l[i]; + } + } + qt_xdnd_types[j] = 0; +} + + + +void qt_handle_xdnd_position( QWidget *w, const XEvent * xe, bool passive ) +{ + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + QPoint p( (l[2] & 0xffff0000) >> 16, l[2] & 0x0000ffff ); + QWidget * c = find_child( w, p ); // changes p to to c-local coordinates + + if (!passive && checkEmbedded(c, xe)) + return; + + if ( !c || !c->acceptDrops() && c->isDesktop() ) { + return; + } + + if ( l[0] != qt_xdnd_dragsource_xid ) { + //qDebug( "xdnd drag position from unexpected source (%08lx not %08lx)", + // l[0], qt_xdnd_dragsource_xid ); + return; + } + + if (l[3] != 0) { + // timestamp from the source + qt_xdnd_target_current_time = qt_x_user_time = l[3]; + } + + XClientMessageEvent response; + response.type = ClientMessage; + response.window = qt_xdnd_dragsource_xid; + response.format = 32; + response.message_type = qt_xdnd_status; + response.data.l[0] = w->winId(); + response.data.l[1] = 0; // flags + response.data.l[2] = 0; // x, y + response.data.l[3] = 0; // w, h + response.data.l[4] = 0; // action + + if ( !passive ) { // otherwise just reject + while ( c && !c->acceptDrops() && !c->isTopLevel() ) { + p = c->mapToParent( p ); + c = c->parentWidget(); + } + + QRect answerRect( c->mapToGlobal( p ), QSize( 1,1 ) ); + + QDragMoveEvent me( p ); + QDropEvent::Action accepted_action = xdndaction_to_qtaction(l[4]); + me.setAction(accepted_action); + + if ( c != qt_xdnd_current_widget ) { + qt_xdnd_target_answerwas = FALSE; + if ( qt_xdnd_current_widget ) { + QDragLeaveEvent e; + QApplication::sendEvent( qt_xdnd_current_widget, &e ); + } + if ( c->acceptDrops() ) { + qt_xdnd_current_widget = c; + qt_xdnd_current_position = p; + + QDragEnterEvent de( p ); + de.setAction(accepted_action); + QApplication::sendEvent( c, &de ); + if ( de.isAccepted() ) { + me.accept( de.answerRect() ); + if ( !de.isActionAccepted() ) // only as a copy (move if we del) + accepted_action = QDropEvent::Copy; + else + me.acceptAction(TRUE); + } else { + me.ignore( de.answerRect() ); + } + } + } else { + if ( qt_xdnd_target_answerwas ) { + me.accept(); + me.acceptAction(global_requested_action == global_accepted_action); + } + } + + if ( !c->acceptDrops() ) { + qt_xdnd_current_widget = 0; + answerRect = QRect( p, QSize( 1, 1 ) ); + } else if ( xdndaction_to_qtaction(l[4]) < QDropEvent::Private ) { + qt_xdnd_current_widget = c; + qt_xdnd_current_position = p; + + QApplication::sendEvent( c, &me ); + qt_xdnd_target_answerwas = me.isAccepted(); + if ( me.isAccepted() ) { + response.data.l[1] = 1; // yes + if ( !me.isActionAccepted() ) // only as a copy (move if we del) + accepted_action = QDropEvent::Copy; + } else { + response.data.l[0] = 0; + } + answerRect = me.answerRect().intersect( c->rect() ); + } else { + response.data.l[0] = 0; + answerRect = QRect( p, QSize( 1, 1 ) ); + } + answerRect = QRect( c->mapToGlobal( answerRect.topLeft() ), + answerRect.size() ); + + if ( answerRect.left() < 0 ) + answerRect.setLeft( 0 ); + if ( answerRect.right() > 4096 ) + answerRect.setRight( 4096 ); + if ( answerRect.top() < 0 ) + answerRect.setTop( 0 ); + if ( answerRect.bottom() > 4096 ) + answerRect.setBottom( 4096 ); + if ( answerRect.width() < 0 ) + answerRect.setWidth( 0 ); + if ( answerRect.height() < 0 ) + answerRect.setHeight( 0 ); + + response.data.l[2] = (answerRect.x() << 16) + answerRect.y(); + response.data.l[3] = (answerRect.width() << 16) + answerRect.height(); + response.data.l[4] = qtaction_to_xdndaction(accepted_action); + global_accepted_action = accepted_action; + } + + // reset + qt_xdnd_target_current_time = CurrentTime; + + QWidget * source = QWidget::find( qt_xdnd_dragsource_xid ); + + if ( source && source->isDesktop() && !source->acceptDrops() ) + source = 0; + + if ( source ) + qt_handle_xdnd_status( source, (const XEvent *)&response, passive ); + else + XSendEvent( QPaintDevice::x11AppDisplay(), qt_xdnd_dragsource_xid, False, + NoEventMask, (XEvent*)&response ); +} + + +void qt_handle_xdnd_status( QWidget * w, const XEvent * xe, bool /*passive*/ ) +{ + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + // Messy: QDragResponseEvent is just a call to QDragManager function + global_accepted_action = xdndaction_to_qtaction(l[4]); + QDragResponseEvent e( (int)(l[1] & 1) ); + QApplication::sendEvent( w, &e ); + + if ( (int)(l[1] & 2) == 0 ) { + QPoint p( (l[2] & 0xffff0000) >> 16, l[2] & 0x0000ffff ); + QSize s( (l[3] & 0xffff0000) >> 16, l[3] & 0x0000ffff ); + qt_xdnd_source_sameanswer = QRect( p, s ); + if ( qt_xdnd_source_sameanswer.isNull() ) { + // Application wants "coninutous" move events + } + } else { + qt_xdnd_source_sameanswer = QRect(); + } +} + + +void qt_handle_xdnd_leave( QWidget *w, const XEvent * xe, bool /*passive*/ ) +{ + //qDebug( "xdnd leave" ); + if ( !qt_xdnd_current_widget || + w->topLevelWidget() != qt_xdnd_current_widget->topLevelWidget() ) { + return; // sanity + } + + if (checkEmbedded(current_embedding_widget, xe)) { + current_embedding_widget = 0; + qt_xdnd_current_widget = 0; + return; + } + + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + QDragLeaveEvent e; + QApplication::sendEvent( qt_xdnd_current_widget, &e ); + + if ( l[0] != qt_xdnd_dragsource_xid ) { + // This often happens - leave other-process window quickly + //qDebug( "xdnd drag leave from unexpected source (%08lx not %08lx", + //l[0], qt_xdnd_dragsource_xid ); + qt_xdnd_current_widget = 0; + return; + } + + qt_xdnd_dragsource_xid = 0; + qt_xdnd_types[0] = 0; + qt_xdnd_current_widget = 0; +} + + +void qt_xdnd_send_leave() +{ + if ( !qt_xdnd_current_target ) + return; + + XClientMessageEvent leave; + leave.type = ClientMessage; + leave.window = qt_xdnd_current_target; + leave.format = 32; + leave.message_type = qt_xdnd_leave; + leave.data.l[0] = qt_xdnd_dragsource_xid; + leave.data.l[1] = 0; // flags + leave.data.l[2] = 0; // x, y + leave.data.l[3] = 0; // w, h + leave.data.l[4] = 0; // just null + + QWidget * w = QWidget::find( qt_xdnd_current_proxy_target ); + + if ( w && w->isDesktop() && !w->acceptDrops() ) + w = 0; + + if ( w ) + qt_handle_xdnd_leave( w, (const XEvent *)&leave, FALSE ); + else + XSendEvent( QPaintDevice::x11AppDisplay(), qt_xdnd_current_proxy_target, False, + NoEventMask, (XEvent*)&leave ); + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; +} + + + +void qt_handle_xdnd_drop( QWidget *, const XEvent * xe, bool passive ) +{ + if ( !qt_xdnd_current_widget ) { + qt_xdnd_dragsource_xid = 0; + return; // sanity + } + + if (!passive && checkEmbedded(qt_xdnd_current_widget, xe)){ + current_embedding_widget = 0; + qt_xdnd_dragsource_xid = 0; + qt_xdnd_current_widget = 0; + return; + } + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + //qDebug( "xdnd drop" ); + + if ( l[0] != qt_xdnd_dragsource_xid ) { + //qDebug( "xdnd drop from unexpected source (%08lx not %08lx", + // l[0], qt_xdnd_dragsource_xid ); + return; + } + + if (l[2] != 0) { + // update the "user time" from the timestamp in the event. + qt_xdnd_target_current_time = qt_x_user_time = l[2]; + } + + if ( qt_xdnd_source_object ) + qt_xdnd_source_object->setTarget( qt_xdnd_current_widget ); + + if ( !passive ) { + QDropEvent de( qt_xdnd_current_position ); + de.setAction( global_accepted_action ); + QApplication::sendEvent( qt_xdnd_current_widget, &de ); + if ( !de.isAccepted() ) { + // Ignore a failed drag + global_accepted_action = QDropEvent::Copy; + dndCancelled = TRUE; + } + XClientMessageEvent finished; + finished.type = ClientMessage; + finished.window = qt_xdnd_dragsource_xid; + finished.format = 32; + finished.message_type = qt_xdnd_finished; + finished.data.l[0] = qt_xdnd_current_widget?qt_xdnd_current_widget->topLevelWidget()->winId():0; + finished.data.l[1] = 0; // flags + XSendEvent( QPaintDevice::x11AppDisplay(), qt_xdnd_dragsource_xid, False, + NoEventMask, (XEvent*)&finished ); + } else { + QDragLeaveEvent e; + QApplication::sendEvent( qt_xdnd_current_widget, &e ); + } + qt_xdnd_dragsource_xid = 0; + qt_xdnd_current_widget = 0; + + // reset + qt_xdnd_target_current_time = CurrentTime; +} + + +void qt_handle_xdnd_finished( QWidget *, const XEvent * xe, bool passive ) +{ + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + if ( l[0] && (l[0] == qt_xdnd_current_target + || l[0] == qt_xdnd_current_proxy_target) ) { + // + if ( !passive ) + (void ) checkEmbedded( qt_xdnd_current_widget, xe); + current_embedding_widget = 0; + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; + delete qt_xdnd_source_object; + qt_xdnd_source_object = 0; + } +} + + +void QDragManager::timerEvent( QTimerEvent* e ) +{ + if ( e->timerId() == heartbeat ) { + if( need_modifiers_check ) { + Window root, child; + int root_x, root_y, win_x, win_y; + unsigned int mask; + XQueryPointer( qt_xdisplay(), qt_xrootwin( qt_xdnd_current_screen ), + &root, &child, &root_x, &root_y, &win_x, &win_y, &mask ); + if( updateMode( (ButtonState)qt_x11_translateButtonState( mask ))) + qt_xdnd_source_sameanswer = QRect(); // force move + } + need_modifiers_check = TRUE; + if( qt_xdnd_source_sameanswer.isNull() ) + move( QCursor::pos() ); + } +} + +static bool qt_xdnd_was_move = false; +static bool qt_xdnd_found = false; +// check whole incoming X queue for move events +// checking whole queue is done by always returning False in the predicate +// if there's another move event in the queue, and there's not a mouse button +// or keyboard or ClientMessage event before it, the current move event +// may be safely discarded +// this helps avoiding being overloaded by being flooded from many events +// from the XServer +static +Bool qt_xdnd_predicate( Display*, XEvent* ev, XPointer ) +{ + if( qt_xdnd_found ) + return False; + if( ev->type == MotionNotify ) + { + qt_xdnd_was_move = true; + qt_xdnd_found = true; + } + if( ev->type == ButtonPress || ev->type == ButtonRelease + || ev->type == XKeyPress || ev->type == XKeyRelease + || ev->type == ClientMessage ) + { + qt_xdnd_was_move = false; + qt_xdnd_found = true; + } + return False; +} + +static +bool qt_xdnd_another_movement() +{ + qt_xdnd_was_move = false; + qt_xdnd_found = false; + XEvent dummy; + XCheckIfEvent( qt_xdisplay(), &dummy, qt_xdnd_predicate, NULL ); + return qt_xdnd_was_move; +} + +bool QDragManager::eventFilter( QObject * o, QEvent * e) +{ + if ( beingCancelled ) { + if ( e->type() == QEvent::KeyRelease && + ((QKeyEvent*)e)->key() == Key_Escape ) { + qApp->removeEventFilter( this ); + object = 0; + dragSource = 0; + beingCancelled = FALSE; + qApp->exit_loop(); + return TRUE; // block the key release + } + return FALSE; + } + + Q_ASSERT( object != 0 ); + + if ( !o->isWidgetType() ) + return FALSE; + + if ( e->type() == QEvent::MouseMove ) { + QMouseEvent* me = (QMouseEvent *)e; + if( !qt_xdnd_another_movement()) { + updateMode(me->stateAfter()); + move( me->globalPos() ); + } + need_modifiers_check = FALSE; + return TRUE; + } else if ( e->type() == QEvent::MouseButtonRelease ) { + qApp->removeEventFilter( this ); + if ( willDrop ) + drop(); + else + cancel(); + object = 0; + dragSource = 0; + beingCancelled = FALSE; + qApp->exit_loop(); + return TRUE; + } else if ( e->type() == QEvent::DragResponse ) { + if ( ((QDragResponseEvent *)e)->dragAccepted() ) { + if ( !willDrop ) { + willDrop = TRUE; + } + } else { + if ( willDrop ) { + willDrop = FALSE; + } + } + updateCursor(); + return TRUE; + } + + if ( e->type() == QEvent::KeyPress + || e->type() == QEvent::KeyRelease ) + { + QKeyEvent *ke = ((QKeyEvent*)e); + if ( ke->key() == Key_Escape && e->type() == QEvent::KeyPress ) { + cancel(); + qApp->removeEventFilter( this ); + object = 0; + dragSource = 0; + beingCancelled = FALSE; + qApp->exit_loop(); + } else { + if( updateMode(ke->stateAfter())) { + qt_xdnd_source_sameanswer = QRect(); // force move + move( QCursor::pos() ); + } + need_modifiers_check = FALSE; + } + return TRUE; // Eat all key events + } + + // ### We bind modality to widgets, so we have to do this + // ### "manually". + // DnD is modal - eat all other interactive events + switch ( e->type() ) { + case QEvent::MouseButtonPress: + case QEvent::MouseButtonRelease: + case QEvent::MouseButtonDblClick: + case QEvent::MouseMove: + case QEvent::KeyPress: + case QEvent::KeyRelease: + case QEvent::Wheel: + case QEvent::Accel: + case QEvent::AccelAvailable: + case QEvent::AccelOverride: + return TRUE; + default: + return FALSE; + } +} + + +static Qt::ButtonState oldstate; +bool QDragManager::updateMode( ButtonState newstate ) +{ + if ( newstate == oldstate ) + return false; + const int both = ShiftButton|ControlButton; + if ( (newstate & both) == both ) { + global_requested_action = QDropEvent::Link; + } else { + bool local = qt_xdnd_source_object != 0; + if ( drag_mode == QDragObject::DragMove ) + global_requested_action = QDropEvent::Move; + else if ( drag_mode == QDragObject::DragCopy ) + global_requested_action = QDropEvent::Copy; + else if ( drag_mode == QDragObject::DragLink ) + global_requested_action = QDropEvent::Link; + else { + if ( drag_mode == QDragObject::DragDefault && local ) + global_requested_action = QDropEvent::Move; + else + global_requested_action = QDropEvent::Copy; + if ( newstate & ShiftButton ) + global_requested_action = QDropEvent::Move; + else if ( newstate & ControlButton ) + global_requested_action = QDropEvent::Copy; + } + } + oldstate = newstate; + return true; +} + + +void QDragManager::createCursors() +{ + if ( !noDropCursor ) { + noDropCursor = new QCursor( ForbiddenCursor ); + if ( !pm_cursor[0].isNull() ) + moveCursor = new QCursor(pm_cursor[0], 0,0); + if ( !pm_cursor[1].isNull() ) + copyCursor = new QCursor(pm_cursor[1], 0,0); + if ( !pm_cursor[2].isNull() ) + linkCursor = new QCursor(pm_cursor[2], 0,0); + } +} + +void QDragManager::updateCursor() +{ + QCursor *c; + if ( willDrop ) { + if ( global_accepted_action == QDropEvent::Copy ) { + if ( global_requested_action == QDropEvent::Move ) + c = moveCursor; // (source can delete) + else + c = copyCursor; + } else if ( global_accepted_action == QDropEvent::Link ) { + c = linkCursor; + } else { + c = moveCursor; + } + if ( qt_xdnd_deco ) { + qt_xdnd_deco->show(); + qt_xdnd_deco->raise(); + } + } else { + c = noDropCursor; + //if ( qt_xdnd_deco ) + // qt_xdnd_deco->hide(); + } +#ifndef QT_NO_CURSOR + if ( c ) + qApp->setOverrideCursor( *c, TRUE ); +#endif +} + + +void QDragManager::cancel( bool deleteSource ) +{ + killTimer( heartbeat ); + heartbeat = -1; + if ( object ) { + beingCancelled = TRUE; + object = 0; + } + + if ( qt_xdnd_current_target ) { + qt_xdnd_send_leave(); + } + +#ifndef QT_NO_CURSOR + if ( restoreCursor ) { + QApplication::restoreOverrideCursor(); + restoreCursor = FALSE; + } +#endif + + if ( deleteSource ) + delete qt_xdnd_source_object; + qt_xdnd_source_object = 0; + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + + dndCancelled = TRUE; +} + +static +Window findRealWindow( const QPoint & pos, Window w, int md ) +{ + if ( qt_xdnd_deco && w == qt_xdnd_deco->winId() ) + return 0; + + if ( md ) { + qt_ignore_badwindow(); + XWindowAttributes attr; + XGetWindowAttributes( QPaintDevice::x11AppDisplay(), w, &attr ); + if (qt_badwindow()) + return 0; + + if ( attr.map_state == IsViewable + && QRect(attr.x,attr.y,attr.width,attr.height) + .contains(pos) ) + { + { + Atom type = None; + int f; + unsigned long n, a; + unsigned char *data; + + XGetWindowProperty( QPaintDevice::x11AppDisplay(), w, qt_xdnd_aware, 0, + 0, False, AnyPropertyType, &type, &f,&n,&a,&data ); + if ( data ) XFree(data); + if ( type ) return w; + } + + Window r, p; + Window* c; + uint nc; + if ( XQueryTree( QPaintDevice::x11AppDisplay(), w, &r, &p, &c, &nc ) ) { + r=0; + for (uint i=nc; !r && i--; ) { + r = findRealWindow( pos-QPoint(attr.x,attr.y), + c[i], md-1 ); + } + XFree(c); + if ( r ) + return r; + + // We didn't find a client window! Just use the + // innermost window. + } + + // No children! + return w; + } + } + return 0; +} + +void QDragManager::move( const QPoint & globalPos ) +{ + if (!object) { + // perhaps the target crashed? + return; + } + + int screen = QCursor::x11Screen(); + if ( ( qt_xdnd_current_screen == -1 && screen != QPaintDevice::x11AppScreen() ) || + ( screen != qt_xdnd_current_screen ) ) { + // recreate the pixmap on the new screen... + delete qt_xdnd_deco; + qt_xdnd_deco = new QShapedPixmapWidget( screen ); + qt_xdnd_deco->x11SetWindowTransient( dragSource->topLevelWidget()); + if (!QWidget::mouseGrabber()) { + updatePixmap(); + qt_xdnd_deco->grabMouse(); + } + } + updatePixmap( globalPos ); + + if ( qt_xdnd_source_sameanswer.contains( globalPos ) && + qt_xdnd_source_sameanswer.isValid() ) { + return; + } + + qt_xdnd_current_screen = screen; + Window rootwin = QPaintDevice::x11AppRootWindow( qt_xdnd_current_screen ); + Window target = 0; + int lx = 0, ly = 0; + if ( !XTranslateCoordinates( QPaintDevice::x11AppDisplay(), rootwin, rootwin, + globalPos.x(), globalPos.y(), + &lx, &ly, &target) ) + // some wierd error... + return; + + if ( target == rootwin ) { + // Ok. + } else if ( target ) { + //me + Window src = rootwin; + while (target != 0) { + int lx2, ly2; + Window t; + // translate coordinates + if (!XTranslateCoordinates(QPaintDevice::x11AppDisplay(), src, target, + lx, ly, &lx2, &ly2, &t)) { + target = 0; + break; + } + lx = lx2; + ly = ly2; + src = target; + + // check if it has XdndAware + Atom type = None; + int f; + unsigned long n, a; + unsigned char *data = 0; + XGetWindowProperty(QPaintDevice::x11AppDisplay(), target, qt_xdnd_aware, 0, 0, False, + AnyPropertyType, &type, &f,&n,&a,&data); + if (data) + XFree(data); + if (type) + break; + + // find child at the coordinates + if (!XTranslateCoordinates( QPaintDevice::x11AppDisplay(), src, src, + lx, ly, &lx2, &ly2, &target)) { + target = 0; + break; + } + } + if ( qt_xdnd_deco && (!target || target == qt_xdnd_deco->winId()) ) { + target = findRealWindow(globalPos,rootwin,6); + } + } + + QWidget* w; + if ( target ) { + w = QWidget::find( (WId)target ); + if ( w && w->isDesktop() && !w->acceptDrops() ) + w = 0; + } else { + w = 0; + target = rootwin; + } + + WId proxy_target = target; + int target_version = 1; + + { + Atom type = None; + int r, f; + unsigned long n, a; + WId *proxy_id; + qt_ignore_badwindow(); + r = XGetWindowProperty( qt_xdisplay(), target, qt_xdnd_proxy, 0, + 1, False, XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id ); + if ( ( r != Success ) || qt_badwindow() ) { + proxy_target = target = 0; + } else if ( type == XA_WINDOW && proxy_id ) { + proxy_target = *proxy_id; + XFree(proxy_id); + proxy_id = 0; + r = XGetWindowProperty( qt_xdisplay(), proxy_target, qt_xdnd_proxy, 0, + 1, False, XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id ); + if ( ( r != Success ) || qt_badwindow() || !type || !proxy_id || *proxy_id != proxy_target ) { + // Bogus + proxy_target = 0; + target = 0; + } + if ( proxy_id ) + XFree(proxy_id); + } + if ( proxy_target ) { + int *tv; + qt_ignore_badwindow(); + r = XGetWindowProperty( qt_xdisplay(), proxy_target, qt_xdnd_aware, 0, + 1, False, AnyPropertyType, &type, &f,&n,&a,(uchar**)&tv ); + if ( r != Success ) { + target = 0; + } else { + target_version = QMIN(qt_xdnd_version,tv ? *tv : 1); + if ( tv ) + XFree( tv ); + if (!(!qt_badwindow() && type)) + target = 0; + } + } + } + + if ( target != qt_xdnd_current_target ) { + if ( qt_xdnd_current_target ) + qt_xdnd_send_leave(); + + qt_xdnd_current_target = target; + qt_xdnd_current_proxy_target = proxy_target; + if ( target ) { + QMemArray<Atom> type; + int flags = target_version << 24; + const char* fmt; + int nfmt=0; + for (nfmt=0; (fmt=object->format(nfmt)); nfmt++) { + type.resize(nfmt+1); + type[nfmt] = *qt_xdnd_str_to_atom( fmt ); + } + if ( nfmt >= 3 ) { + XChangeProperty( QPaintDevice::x11AppDisplay(), + object->source()->winId(), qt_xdnd_type_list, + XA_ATOM, 32, PropModeReplace, + (unsigned char *)type.data(), + type.size() ); + flags |= 0x0001; + } + XClientMessageEvent enter; + enter.type = ClientMessage; + enter.window = target; + enter.format = 32; + enter.message_type = qt_xdnd_enter; + enter.data.l[0] = object->source()->winId(); + enter.data.l[1] = flags; + enter.data.l[2] = type.size()>0 ? type[0] : 0; + enter.data.l[3] = type.size()>1 ? type[1] : 0; + enter.data.l[4] = type.size()>2 ? type[2] : 0; + // provisionally set the rectangle to 5x5 pixels... + qt_xdnd_source_sameanswer = QRect( globalPos.x() - 2, + globalPos.y() -2 , 5, 5 ); + + if ( w ) { + qt_handle_xdnd_enter( w, (const XEvent *)&enter, FALSE ); + } else if ( target ) { + XSendEvent( QPaintDevice::x11AppDisplay(), proxy_target, False, NoEventMask, + (XEvent*)&enter ); + } + } + } + + if ( target ) { + XClientMessageEvent move; + move.type = ClientMessage; + move.window = target; + move.format = 32; + move.message_type = qt_xdnd_position; + move.window = target; + move.data.l[0] = object->source()->winId(); + move.data.l[1] = 0; // flags + move.data.l[2] = (globalPos.x() << 16) + globalPos.y(); + move.data.l[3] = qt_x_time; + move.data.l[4] = qtaction_to_xdndaction( global_requested_action ); + + if ( w ) + qt_handle_xdnd_position( w, (const XEvent *)&move, FALSE ); + else + XSendEvent( QPaintDevice::x11AppDisplay(), proxy_target, False, NoEventMask, + (XEvent*)&move ); + } else { + if ( willDrop ) { + willDrop = FALSE; + updateCursor(); + } + } +} + + +void QDragManager::drop() +{ + killTimer( heartbeat ); + heartbeat = -1; + if ( !qt_xdnd_current_target ) + return; + + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + + XClientMessageEvent drop; + drop.type = ClientMessage; + drop.window = qt_xdnd_current_target; + drop.format = 32; + drop.message_type = qt_xdnd_drop; + drop.data.l[0] = object->source()->winId(); + drop.data.l[1] = 0; // flags + drop.data.l[2] = qt_x_time; + drop.data.l[3] = 0; + drop.data.l[4] = 0; + + QWidget * w = QWidget::find( qt_xdnd_current_proxy_target ); + + if ( w && w->isDesktop() && !w->acceptDrops() ) + w = 0; + + if ( w ) + qt_handle_xdnd_drop( w, (const XEvent *)&drop, FALSE ); + else + XSendEvent( QPaintDevice::x11AppDisplay(), qt_xdnd_current_proxy_target, False, + NoEventMask, (XEvent*)&drop ); + +#ifndef QT_NO_CURSOR + if ( restoreCursor ) { + QApplication::restoreOverrideCursor(); + restoreCursor = FALSE; + } +#endif +} + + + +bool qt_xdnd_handle_badwindow() +{ + if ( qt_xdnd_source_object && qt_xdnd_current_target ) { + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; + delete qt_xdnd_source_object; + qt_xdnd_source_object = 0; + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + return TRUE; + } + if ( qt_xdnd_dragsource_xid ) { + qt_xdnd_dragsource_xid = 0; + if ( qt_xdnd_current_widget ) { + QDragLeaveEvent e; + QApplication::sendEvent( qt_xdnd_current_widget, &e ); + qt_xdnd_current_widget = 0; + } + return TRUE; + } + return FALSE; +} + + +/*! + \class QDragMoveEvent qevent.h + \ingroup events + \ingroup draganddrop + \brief The QDragMoveEvent class provides an event which is sent while a drag and drop is in progress. + + When a widget \link QWidget::setAcceptDrops() accepts drop + events\endlink, it will receive this event repeatedly while the + drag is within the widget's boundaries. The widget should examine + the event to see what data it \link QDragMoveEvent::provides() + provides\endlink, and accept() the drop if appropriate. + + Note that this class inherits most of its functionality from + QDropEvent. +*/ + + +/*! + Returns TRUE if this event provides format \a mimeType; otherwise + returns FALSE. + + \sa data() +*/ + +bool QDropEvent::provides( const char *mimeType ) const +{ + if ( qt_motifdnd_active && qstrnicmp( mimeType, "text/", 5 ) == 0 ) + return TRUE; + + int n=0; + const char* f; + do { + f = format( n ); + if ( !f ) + return FALSE; + n++; + } while( qstricmp( mimeType, f ) ); + return TRUE; +} + +void qt_xdnd_handle_selection_request( const XSelectionRequestEvent * req ) +{ + if ( !req ) + return; + XEvent evt; + evt.xselection.type = SelectionNotify; + evt.xselection.display = req->display; + evt.xselection.requestor = req->requestor; + evt.xselection.selection = req->selection; + evt.xselection.target = req->target; + evt.xselection.property = None; + evt.xselection.time = req->time; + const char* format = qt_xdnd_atom_to_str( req->target ); + if ( format && qt_xdnd_source_object && + qt_xdnd_source_object->provides( format ) ) { + QByteArray a = qt_xdnd_source_object->encodedData(format); + XChangeProperty ( QPaintDevice::x11AppDisplay(), req->requestor, req->property, + req->target, 8, PropModeReplace, + (unsigned char *)a.data(), a.size() ); + evt.xselection.property = req->property; + } + // ### this can die if req->requestor crashes at the wrong + // ### moment + XSendEvent( QPaintDevice::x11AppDisplay(), req->requestor, False, 0, &evt ); +} + +/* + XChangeProperty ( QPaintDevice::x11AppDisplay(), req->requestor, req->property, + XA_STRING, 8, + PropModeReplace, + (uchar *)d->text(), strlen(d->text()) ); + evt.xselection.property = req->property; +*/ + +static QByteArray qt_xdnd_obtain_data( const char *format ) +{ + QByteArray result; + + QWidget* w; + if ( qt_xdnd_dragsource_xid && qt_xdnd_source_object && + (w=QWidget::find( qt_xdnd_dragsource_xid )) + && (!w->isDesktop() || w->acceptDrops()) ) + { + QDragObject * o = qt_xdnd_source_object; + if ( o->provides( format ) ) + result = o->encodedData(format); + return result; + } + + Atom * a = qt_xdnd_str_to_atom( format ); + if ( !a || !*a ) + return result; + + if ( !qt_xdnd_target_data ) + qt_xdnd_target_data = new QIntDict<QByteArray>( 17 ); + + if ( qt_xdnd_target_data->find( (int)*a ) ) { + result = *(qt_xdnd_target_data->find( (int)*a )); + } else { + if ( XGetSelectionOwner( QPaintDevice::x11AppDisplay(), + qt_xdnd_selection ) == None ) + return result; // should never happen? + + QWidget* tw = qt_xdnd_current_widget; + if ( !qt_xdnd_current_widget || + qt_xdnd_current_widget->isDesktop() ) { + tw = new QWidget; + } + XConvertSelection( QPaintDevice::x11AppDisplay(), + qt_xdnd_selection, + *a, + qt_xdnd_selection, + tw->winId(), + qt_xdnd_target_current_time ); + XFlush( QPaintDevice::x11AppDisplay() ); + + XEvent xevent; + bool got=qt_xclb_wait_for_event( QPaintDevice::x11AppDisplay(), + tw->winId(), + SelectionNotify, &xevent, 5000); + if ( got ) { + Atom type; + + if ( qt_xclb_read_property( QPaintDevice::x11AppDisplay(), + tw->winId(), + qt_xdnd_selection, TRUE, + &result, 0, &type, 0, FALSE ) ) { + if ( type == qt_incr_atom ) { + int nbytes = result.size() >= 4 ? *((int*)result.data()) : 0; + result = qt_xclb_read_incremental_property( QPaintDevice::x11AppDisplay(), + tw->winId(), + qt_xdnd_selection, + nbytes, FALSE ); + } else if ( type != *a ) { + // (includes None) qDebug( "Qt clipboard: unknown atom %ld", type); + } +#if 0 + // this needs to be matched by a qt_xdnd_target_data->clear() + // when each drag is finished. for 2.0, we do the safe thing + // and disable the entire caching. + if ( type != None ) + qt_xdnd_target_data->insert( (int)((long)a), new QByteArray(result) ); +#endif + } + } + if ( !qt_xdnd_current_widget || + qt_xdnd_current_widget->isDesktop() ) { + delete tw; + } + } + + return result; +} + + +/* + Enable drag and drop for widget w by installing the proper + properties on w's toplevel widget. +*/ +bool qt_dnd_enable( QWidget* w, bool on ) +{ + w = w->topLevelWidget(); + + if ( on ) { + if ( ( (QExtraWidget*)w)->topData()->dnd ) + return TRUE; // been there, done that + ((QExtraWidget*)w)->topData()->dnd = 1; + } + + qt_motifdnd_enable( w, on ); + return qt_xdnd_enable( w, on ); +} + + +/*! + \class QDropEvent qevent.h + \ingroup events + \ingroup draganddrop + + \brief The QDropEvent class provides an event which is sent when a drag and drop is completed. + + When a widget \link QWidget::setAcceptDrops() accepts drop + events\endlink, it will receive this event if it has accepted the + most recent QDragEnterEvent or QDragMoveEvent sent to it. + + The widget should use data() to extract the data in an appropriate + format. +*/ + + +/*! + \fn QDropEvent::QDropEvent (const QPoint & pos, Type typ) + + Constructs a drop event that drops a drop of type \a typ on point + \a pos. +*/ // ### pos is in which coordinate system? + + +/*! + Returns a byte array containing the drag's data, in \a format. + + data() normally needs to get the data from the drag source, which + is potentially very slow, so it's advisable to call this function + only if you're sure that you will need the data in \a format. + + The resulting data will have a size of 0 if the format was not + available. + + \sa format() QByteArray::size() +*/ + +QByteArray QDropEvent::encodedData( const char *format ) const +{ + if ( qt_motifdnd_active ) + return qt_motifdnd_obtain_data( format ); + return qt_xdnd_obtain_data( format ); +} + +/*! + Returns a string describing one of the available data types for + this drag. Common examples are "text/plain" and "image/gif". If \a + n is less than zero or greater than the number of available data + types, format() returns 0. + + This function is provided mainly for debugging. Most drop targets + will use provides(). + + \sa data() provides() +*/ + +const char* QDropEvent::format( int n ) const +{ + if ( qt_motifdnd_active ) + return qt_motifdnd_format( n ); + + int i = 0; + while( i<n && qt_xdnd_types[i] ) + i++; + if ( i < n ) + return 0; + + const char* name = qt_xdnd_atom_to_str( qt_xdnd_types[i] ); + if ( !name ) + return 0; // should never happen + + return name; +} + +bool QDragManager::drag( QDragObject * o, QDragObject::DragMode mode ) +{ + if ( object == o || !o || !o->parent() ) + return FALSE; + + if ( object ) { + cancel(); + qApp->removeEventFilter( this ); + beingCancelled = FALSE; + } + + if ( qt_xdnd_source_object ) { + // the last drag and drop operation hasn't finished, so we are going to wait + // for one second to see if it does... if the finish message comes after this, + // then we could still have problems, but this is highly unlikely + QApplication::flushX(); + + QTime started = QTime::currentTime(); + QTime now = started; + do { + XEvent event; + if ( XCheckTypedEvent( QPaintDevice::x11AppDisplay(), + ClientMessage, &event ) ) + qApp->x11ProcessEvent( &event ); + + now = QTime::currentTime(); + if ( started > now ) // crossed midnight + started = now; + + // sleep 50ms, so we don't use up CPU cycles all the time. + struct timeval usleep_tv; + usleep_tv.tv_sec = 0; + usleep_tv.tv_usec = 50000; + select(0, 0, 0, 0, &usleep_tv); + } while ( qt_xdnd_source_object && started.msecsTo(now) < 1000 ); + } + + qt_xdnd_source_object = o; + qt_xdnd_source_object->setTarget( 0 ); + qt_xdnd_deco = new QShapedPixmapWidget(); + + willDrop = FALSE; + + object = o; + updatePixmap(); + + dragSource = (QWidget *)(object->parent()); + + qt_xdnd_deco->x11SetWindowTransient( dragSource->topLevelWidget()); + qApp->installEventFilter( this ); + qt_xdnd_source_current_time = qt_x_time; + XSetSelectionOwner( QPaintDevice::x11AppDisplay(), qt_xdnd_selection, + dragSource->topLevelWidget()->winId(), + qt_xdnd_source_current_time ); + oldstate = ButtonState(-1); // #### Should use state that caused the drag + drag_mode = mode; + global_accepted_action = QDropEvent::Copy; + updateMode(ButtonState(0)); + qt_xdnd_source_sameanswer = QRect(); + move(QCursor::pos()); + heartbeat = startTimer(200); + need_modifiers_check = FALSE; + +#ifndef QT_NO_CURSOR + qApp->setOverrideCursor( arrowCursor ); + restoreCursor = TRUE; + updateCursor(); +#endif + + dndCancelled = FALSE; + qt_xdnd_dragging = TRUE; + + if (!QWidget::mouseGrabber()) + qt_xdnd_deco->grabMouse(); + + qApp->enter_loop(); // Do the DND. + +#ifndef QT_NO_CURSOR + qApp->restoreOverrideCursor(); +#endif + + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + killTimer( heartbeat ); + heartbeat = -1; + qt_xdnd_current_screen = -1; + qt_xdnd_dragging = FALSE; + + return ((! dndCancelled) && // source del? + (global_accepted_action == QDropEvent::Copy && + global_requested_action == QDropEvent::Move)); + + // qt_xdnd_source_object persists until we get an xdnd_finish message +} + +void QDragManager::updatePixmap( const QPoint& cursorPos ) +{ + if ( qt_xdnd_deco ) { + QPixmap pm; + QPoint pm_hot(default_pm_hotx,default_pm_hoty); + if ( object ) { + pm = object->pixmap(); + if ( !pm.isNull() ) + pm_hot = object->pixmapHotSpot(); + } + if ( pm.isNull() ) { + if ( !defaultPm ) + defaultPm = new QPixmap(default_pm); + pm = *defaultPm; + } + qt_xdnd_deco->setPixmap(pm, pm_hot); + qt_xdnd_deco->move(cursorPos-pm_hot); + //if ( willDrop ) { + qt_xdnd_deco->show(); + //} else { + // qt_xdnd_deco->hide(); + //} + } +} + +void QDragManager::updatePixmap() +{ + updatePixmap( QCursor::pos()); +} + +#endif // QT_NO_DRAGANDDROP diff --git a/src/kernel/qdragobject.cpp b/src/kernel/qdragobject.cpp new file mode 100644 index 0000000..44e340f --- /dev/null +++ b/src/kernel/qdragobject.cpp @@ -0,0 +1,1811 @@ +/**************************************************************************** +** +** Implementation of Drag and Drop support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +#ifndef QT_NO_MIME + +#include "qdragobject.h" +#include "qtextcodec.h" +#include "qapplication.h" +#include "qpoint.h" +#include "qwidget.h" +#include "qbuffer.h" +#include "qgif.h" +#include "qregexp.h" +#include "qdir.h" +#include <ctype.h> + +// both a struct for storing stuff in and a wrapper to avoid polluting +// the name space + +class QDragObjectData +{ +public: + QDragObjectData(): hot(0,0) {} + QPixmap pixmap; + QPoint hot; + // store default cursors + QPixmap *pm_cursor; +}; + +static QWidget* last_target; + +/*! + After the drag completes, this function will return the QWidget + which received the drop, or 0 if the data was dropped on another + application. + + This can be useful for detecting the case where drag and drop is + to and from the same widget. +*/ +QWidget * QDragObject::target() +{ + return last_target; +} + +/*! + \internal + Sets the target. +*/ +void QDragObject::setTarget(QWidget* t) +{ + last_target = t; +} + +class QStoredDragData +{ +public: + QStoredDragData() {} + const char* fmt; + QByteArray enc; +}; + + +// These pixmaps approximate the images in the Windows User Interface Guidelines. + +// XPM + +static const char * const move_xpm[] = { +"11 20 3 1", +". c None", +#if defined(Q_WS_WIN) +"a c #000000", +"X c #FFFFFF", // Windows cursor is traditionally white +#else +"a c #FFFFFF", +"X c #000000", // X11 cursor is traditionally black +#endif +"aa.........", +"aXa........", +"aXXa.......", +"aXXXa......", +"aXXXXa.....", +"aXXXXXa....", +"aXXXXXXa...", +"aXXXXXXXa..", +"aXXXXXXXXa.", +"aXXXXXXXXXa", +"aXXXXXXaaaa", +"aXXXaXXa...", +"aXXaaXXa...", +"aXa..aXXa..", +"aa...aXXa..", +"a.....aXXa.", +"......aXXa.", +".......aXXa", +".......aXXa", +"........aa."}; + +/* XPM */ +static const char * const copy_xpm[] = { +"24 30 3 1", +". c None", +"a c #000000", +"X c #FFFFFF", +#if defined(Q_WS_WIN) // Windows cursor is traditionally white +"aa......................", +"aXa.....................", +"aXXa....................", +"aXXXa...................", +"aXXXXa..................", +"aXXXXXa.................", +"aXXXXXXa................", +"aXXXXXXXa...............", +"aXXXXXXXXa..............", +"aXXXXXXXXXa.............", +"aXXXXXXaaaa.............", +"aXXXaXXa................", +"aXXaaXXa................", +"aXa..aXXa...............", +"aa...aXXa...............", +"a.....aXXa..............", +"......aXXa..............", +".......aXXa.............", +".......aXXa.............", +"........aa...aaaaaaaaaaa", +#else +"XX......................", +"XaX.....................", +"XaaX....................", +"XaaaX...................", +"XaaaaX..................", +"XaaaaaX.................", +"XaaaaaaX................", +"XaaaaaaaX...............", +"XaaaaaaaaX..............", +"XaaaaaaaaaX.............", +"XaaaaaaXXXX.............", +"XaaaXaaX................", +"XaaXXaaX................", +"XaX..XaaX...............", +"XX...XaaX...............", +"X.....XaaX..............", +"......XaaX..............", +".......XaaX.............", +".......XaaX.............", +"........XX...aaaaaaaaaaa", +#endif +".............aXXXXXXXXXa", +".............aXXXXXXXXXa", +".............aXXXXaXXXXa", +".............aXXXXaXXXXa", +".............aXXaaaaaXXa", +".............aXXXXaXXXXa", +".............aXXXXaXXXXa", +".............aXXXXXXXXXa", +".............aXXXXXXXXXa", +".............aaaaaaaaaaa"}; + +/* XPM */ +static const char * const link_xpm[] = { +"24 30 3 1", +". c None", +"a c #000000", +"X c #FFFFFF", +#if defined(Q_WS_WIN) // Windows cursor is traditionally white +"aa......................", +"aXa.....................", +"aXXa....................", +"aXXXa...................", +"aXXXXa..................", +"aXXXXXa.................", +"aXXXXXXa................", +"aXXXXXXXa...............", +"aXXXXXXXXa..............", +"aXXXXXXXXXa.............", +"aXXXXXXaaaa.............", +"aXXXaXXa................", +"aXXaaXXa................", +"aXa..aXXa...............", +"aa...aXXa...............", +"a.....aXXa..............", +"......aXXa..............", +".......aXXa.............", +".......aXXa.............", +"........aa...aaaaaaaaaaa", +#else +"XX......................", +"XaX.....................", +"XaaX....................", +"XaaaX...................", +"XaaaaX..................", +"XaaaaaX.................", +"XaaaaaaX................", +"XaaaaaaaX...............", +"XaaaaaaaaX..............", +"XaaaaaaaaaX.............", +"XaaaaaaXXXX.............", +"XaaaXaaX................", +"XaaXXaaX................", +"XaX..XaaX...............", +"XX...XaaX...............", +"X.....XaaX..............", +"......XaaX..............", +".......XaaX.............", +".......XaaX.............", +"........XX...aaaaaaaaaaa", +#endif +".............aXXXXXXXXXa", +".............aXXXaaaaXXa", +".............aXXXXaaaXXa", +".............aXXXaaaaXXa", +".............aXXaaaXaXXa", +".............aXXaaXXXXXa", +".............aXXaXXXXXXa", +".............aXXXaXXXXXa", +".............aXXXXXXXXXa", +".............aaaaaaaaaaa"}; + +#ifndef QT_NO_DRAGANDDROP + +// the universe's only drag manager +QDragManager * qt_dnd_manager = 0; + + +QDragManager::QDragManager() + : QObject( qApp, "global drag manager" ) +{ + n_cursor = 3; + pm_cursor = new QPixmap[n_cursor]; + pm_cursor[0] = QPixmap((const char **)move_xpm); + pm_cursor[1] = QPixmap((const char **)copy_xpm); + pm_cursor[2] = QPixmap((const char **)link_xpm); +#if defined(Q_WS_X11) + createCursors(); // Xcursors cache can hold only 8 bitmaps (4 cursors) +#endif + object = 0; + dragSource = 0; + dropWidget = 0; + if ( !qt_dnd_manager ) + qt_dnd_manager = this; + beingCancelled = FALSE; + restoreCursor = FALSE; + willDrop = FALSE; +} + + +QDragManager::~QDragManager() +{ +#ifndef QT_NO_CURSOR + if ( restoreCursor ) + QApplication::restoreOverrideCursor(); +#endif + qt_dnd_manager = 0; + delete [] pm_cursor; +} + +#endif + + +/*! + Constructs a drag object called \a name, which is a child of \a + dragSource. + + Note that the drag object will be deleted when \a dragSource is + deleted. +*/ + +QDragObject::QDragObject( QWidget * dragSource, const char * name ) + : QObject( dragSource, name ) +{ + d = new QDragObjectData(); + d->pm_cursor = 0; +#ifndef QT_NO_DRAGANDDROP + if ( !qt_dnd_manager && qApp ) + (void)new QDragManager(); +#endif +} + + +/*! + Destroys the drag object, canceling any drag and drop operation in + which it is involved, and frees up the storage used. +*/ + +QDragObject::~QDragObject() +{ +#ifndef QT_NO_DRAGANDDROP + if ( qt_dnd_manager && qt_dnd_manager->object == this ) + qt_dnd_manager->cancel( FALSE ); + if ( d->pm_cursor ) { + for ( int i = 0; i < qt_dnd_manager->n_cursor; i++ ) + qt_dnd_manager->pm_cursor[i] = d->pm_cursor[i]; + delete [] d->pm_cursor; + } +#endif + delete d; +} + +#ifndef QT_NO_DRAGANDDROP +/*! + Set the pixmap \a pm to display while dragging the object. The + platform-specific implementation will use this where it can - so + provide a small masked pixmap, and do not assume that the user + will actually see it. For example, cursors on Windows 95 are of + limited size. + + The \a hotspot is the point on (or off) the pixmap that should be + under the cursor as it is dragged. It is relative to the top-left + pixel of the pixmap. + + \warning We have seen problems with drag cursors on different + graphics hardware and driver software on Windows. Setting the + graphics acceleration in the display settings down one tick solved + the problems in all cases. +*/ +void QDragObject::setPixmap(QPixmap pm, const QPoint& hotspot) +{ + d->pixmap = pm; + d->hot = hotspot; + if ( qt_dnd_manager && qt_dnd_manager->object == this ) + qt_dnd_manager->updatePixmap(); +} + +/*! + \overload + Uses a hotspot that positions the pixmap below and to the right of + the mouse pointer. This allows the user to clearly see the point + on the window which they are dragging the data onto. +*/ +void QDragObject::setPixmap(QPixmap pm) +{ + setPixmap(pm,QPoint(-10, -10)); +} + +/*! + Returns the currently set pixmap (which \link QPixmap::isNull() + isNull()\endlink if none is set). +*/ +QPixmap QDragObject::pixmap() const +{ + return d->pixmap; +} + +/*! + Returns the currently set pixmap hotspot. +*/ +QPoint QDragObject::pixmapHotSpot() const +{ + return d->hot; +} + +#if 0 + +// ## reevaluate for Qt 4 +/*! + Set the \a cursor used when dragging in mode \a m. + Note: X11 only allow bitmaps for cursors. +*/ +void QDragObject::setCursor( DragMode m, const QPixmap &cursor ) +{ + if ( d->pm_cursor == 0 ) { + // safe default cursors + d->pm_cursor = new QPixmap[qt_dnd_manager->n_cursor]; + for ( int i = 0; i < qt_dnd_manager->n_cursor; i++ ) + d->pm_cursor[i] = qt_dnd_manager->pm_cursor[i]; + } + + int index; + switch ( m ) { + case DragCopy: + index = 1; + break; + case DragLink: + index = 2; + break; + default: + index = 0; + break; + } + + // override default cursor + for ( index = 0; index < qt_dnd_manager->n_cursor; index++ ) + qt_dnd_manager->pm_cursor[index] = cursor; +} + +/*! + Returns the cursor used when dragging in mode \a m, or null if no cursor + has been set for that mode. +*/ +QPixmap *QDragObject::cursor( DragMode m ) const +{ + if ( !d->pm_cursor ) + return 0; + + int index; + switch ( m ) { + case DragCopy: + index = 1; + break; + case DragLink: + index = 2; + break; + default: + index = 0; + break; + } + + return qt_dnd_manager->pm_cursor+index; +} + +#endif // 0 + +/*! + Starts a drag operation using the contents of this object, using + DragDefault mode. + + The function returns TRUE if the caller should delete the original + copy of the dragged data (but see target()); otherwise returns + FALSE. + + If the drag contains \e references to information (e.g. file names + in a QUriDrag are references) then the return value should always + be ignored, as the target is expected to manipulate the + referred-to content directly. On X11 the return value should + always be correct anyway, but on Windows this is not necessarily + the case (e.g. the file manager starts a background process to + move files, so the source \e{must not} delete the files!) +*/ +bool QDragObject::drag() +{ + return drag( DragDefault ); +} + + +/*! + Starts a drag operation using the contents of this object, using + \c DragMove mode. Be sure to read the constraints described in + drag(). + + \sa drag() dragCopy() dragLink() +*/ +bool QDragObject::dragMove() +{ + return drag( DragMove ); +} + + +/*! + Starts a drag operation using the contents of this object, using + \c DragCopy mode. Be sure to read the constraints described in + drag(). + + \sa drag() dragMove() dragLink() +*/ +void QDragObject::dragCopy() +{ + (void)drag( DragCopy ); +} + +/*! + Starts a drag operation using the contents of this object, using + \c DragLink mode. Be sure to read the constraints described in + drag(). + + \sa drag() dragCopy() dragMove() +*/ +void QDragObject::dragLink() +{ + (void)drag( DragLink ); +} + + +/*! + \enum QDragObject::DragMode + + This enum describes the possible drag modes. + + \value DragDefault The mode is determined heuristically. + \value DragCopy The data is copied, never moved. + \value DragMove The data is moved, if dragged at all. + \value DragLink The data is linked, if dragged at all. + \value DragCopyOrMove The user chooses the mode by using a + control key to switch from the default. +*/ + + +/*! + \overload + Starts a drag operation using the contents of this object. + + At this point, the object becomes owned by Qt, not the + application. You should not delete the drag object or anything it + references. The actual transfer of data to the target application + will be done during future event processing - after that time the + drag object will be deleted. + + Returns TRUE if the dragged data was dragged as a \e move, + indicating that the caller should remove the original source of + the data (the drag object must continue to have a copy); otherwise + returns FALSE. + + The \a mode specifies the drag mode (see + \l{QDragObject::DragMode}.) Normally one of the simpler drag(), + dragMove(), or dragCopy() functions would be used instead. +*/ +bool QDragObject::drag( DragMode mode ) +{ + if ( qt_dnd_manager ) + return qt_dnd_manager->drag( this, mode ); + else + return FALSE; +} + +#endif + + +/*! + Returns a pointer to the drag source where this object originated. +*/ + +QWidget * QDragObject::source() +{ + if ( parent() && parent()->isWidgetType() ) + return (QWidget *)parent(); + else + return 0; +} + + +/*! + \class QDragObject qdragobject.h + + \brief The QDragObject class encapsulates MIME-based data + transfer. + + \ingroup draganddrop + + QDragObject is the base class for all data that needs to be + transferred between and within applications, both for drag and + drop and for the \link qclipboard.html clipboard\endlink. + + See the \link dnd.html Drag-and-drop documentation\endlink for an + overview of how to provide drag and drop in your application. + + See the QClipboard documentation for an overview of how to provide + cut-and-paste in your application. + + The drag() function is used to start a drag operation. You can + specify the \l DragMode in the call or use one of the convenience + functions dragCopy(), dragMove() or dragLink(). The drag source + where the data originated is retrieved with source(). If the data + was dropped on a widget within the application, target() will + return a pointer to that widget. Specify the pixmap to display + during the drag with setPixmap(). +*/ + +static +void stripws(QCString& s) +{ + int f; + while ( (f=s.find(' ')) >= 0 ) + s.remove(f,1); +} + +static +const char * staticCharset(int i) +{ + static QCString localcharset; + + switch ( i ) { + case 0: + return "UTF-8"; + case 1: + return "ISO-10646-UCS-2"; + case 2: + return ""; // in the 3rd place - some Xdnd targets might only look at 3 + case 3: + if ( localcharset.isNull() ) { + QTextCodec *localCodec = QTextCodec::codecForLocale(); + if ( localCodec ) { + localcharset = localCodec->name(); + localcharset = localcharset.lower(); + stripws(localcharset); + } else { + localcharset = ""; + } + } + return localcharset; + } + return 0; +} + + +class QTextDragPrivate { +public: + QTextDragPrivate(); + + enum { nfmt=4 }; + + QString txt; + QCString fmt[nfmt]; + QCString subtype; + + void setSubType(const QCString & st) + { + subtype = st.lower(); + for ( int i=0; i<nfmt; i++ ) { + fmt[i] = "text/"; + fmt[i].append(subtype); + QCString cs = staticCharset(i); + if ( !cs.isEmpty() ) { + fmt[i].append(";charset="); + fmt[i].append(cs); + } + } + } +}; + +inline QTextDragPrivate::QTextDragPrivate() +{ + setSubType("plain"); +} + +/*! + Sets the MIME subtype of the text being dragged to \a st. The + default subtype is "plain", so the default MIME type of the text + is "text/plain". You might use this to declare that the text is + "text/html" by calling setSubtype("html"). +*/ +void QTextDrag::setSubtype( const QCString & st) +{ + d->setSubType(st); +} + +/*! + \class QTextDrag qdragobject.h + + \brief The QTextDrag class is a drag and drop object for + transferring plain and Unicode text. + + \ingroup draganddrop + + Plain text is passed in a QString which may contain multiple lines + (i.e. may contain newline characters). The drag target will receive + the newlines according to the runtime environment, e.g. LF on Unix, + and CRLF on Windows. + + Qt provides no built-in mechanism for delivering only a single-line. + + For more information about drag and drop, see the QDragObject class + and the \link dnd.html drag and drop documentation\endlink. +*/ + + +/*! + Constructs a text drag object and sets its data to \a text. \a + dragSource must be the drag source; \a name is the object name. +*/ + +QTextDrag::QTextDrag( const QString &text, + QWidget * dragSource, const char * name ) + : QDragObject( dragSource, name ) +{ + d = new QTextDragPrivate; + setText( text ); +} + + +/*! + Constructs a default text drag object. \a dragSource must be the + drag source; \a name is the object name. +*/ + +QTextDrag::QTextDrag( QWidget * dragSource, const char * name ) + : QDragObject( dragSource, name ) +{ + d = new QTextDragPrivate; +} + + +/*! + Destroys the text drag object and frees up all allocated + resources. +*/ +QTextDrag::~QTextDrag() +{ + delete d; +} + + +/*! + Sets the text to be dragged to \a text. You will need to call this + if you did not pass the text during construction. +*/ +void QTextDrag::setText( const QString &text ) +{ + d->txt = text; +} + + +/*! + \reimp +*/ +const char * QTextDrag::format(int i) const +{ + if ( i >= d->nfmt ) + return 0; + return d->fmt[i]; +} + +QTextCodec* qt_findcharset(const QCString& mimetype) +{ + int i=mimetype.find("charset="); + if ( i >= 0 ) { + QCString cs = mimetype.mid(i+8); + stripws(cs); + i = cs.find(';'); + if ( i >= 0 ) + cs = cs.left(i); + // win98 often has charset=utf16, and we need to get the correct codec for + // it to be able to get Unicode text drops. + if ( cs == "utf16" ) + cs = "ISO-10646-UCS-2"; + // May return 0 if unknown charset + return QTextCodec::codecForName(cs); + } + // no charset=, use locale + return QTextCodec::codecForLocale(); +} + +static QTextCodec *codecForHTML(const QCString &ba) +{ + // determine charset + int mib = 0; + int pos; + QTextCodec *c = 0; + + if (ba.size() > 1 && (((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff) + || ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe))) { + mib = 1000; // utf16 + } else if (ba.size() > 2 + && (uchar)ba[0] == 0xef + && (uchar)ba[1] == 0xbb + && (uchar)ba[2] == 0xbf) { + mib = 106; // utf-8 + } else { + pos = 0; + while ((pos = ba.find("<meta http-equiv=", pos, FALSE)) != -1) { + int end = ba.find('>', pos+1); + if (end == -1) + break; + pos = ba.find("charset=", pos, FALSE) + (int)strlen("charset="); + if (pos != -1 && pos < end) { + int pos2 = ba.find('\"', pos+1); + QCString cs = ba.mid(pos, pos2-pos); + c = QTextCodec::codecForName(cs); + if (c) + return c; + } + pos = end; + } + } + if (mib) + c = QTextCodec::codecForMib(mib); + + return c; +} + +static +QTextCodec* findcodec(const QMimeSource* e) +{ + QTextCodec* r = 0; + const char* f; + int i; + for ( i=0; (f=e->format(i)); i++ ) { + bool html = !qstrnicmp(f, "text/html", 9); + if (html) + r = codecForHTML(QCString(e->encodedData(f))); + if (!r) + r = qt_findcharset(QCString(f).lower()); + if (r) + return r; + } + return 0; +} + + + +/*! + \reimp +*/ +QByteArray QTextDrag::encodedData(const char* mime) const +{ + QCString r; + if ( 0==qstrnicmp(mime,"text/",5) ) { + QCString m(mime); + m = m.lower(); + QTextCodec *codec = qt_findcharset(m); + if ( !codec ) + return r; + QString text( d->txt ); +#if defined(Q_WS_WIN) + int index = text.find( QString::fromLatin1("\r\n"), 0 ); + while ( index != -1 ) { + text.replace( index, 2, QChar('\n') ); + index = text.find( "\r\n", index ); + } +#endif + r = codec->fromUnicode(text); + if (!codec || codec->mibEnum() != 1000) { + // Don't include NUL in size (QCString::resize() adds NUL) +#if defined(Q_WS_WIN) + // This is needed to ensure the \0 isn't lost on Windows 95 + if ( qWinVersion() & Qt::WV_DOS_based ) + ((QByteArray&)r).resize(r.length()+1); + else +#endif + ((QByteArray&)r).resize(r.length()); + } + } + return r; +} + +/*! + Returns TRUE if the information in \a e can be decoded into a + QString; otherwise returns FALSE. + + \sa decode() +*/ +bool QTextDrag::canDecode( const QMimeSource* e ) +{ + const char* f; + for (int i=0; (f=e->format(i)); i++) { + if ( 0==qstrnicmp(f,"text/",5) ) { + return findcodec(e) != 0; + } + } + return 0; +} + +/*! + \overload + + Attempts to decode the dropped information in \a e into \a str. + Returns TRUE if successful; otherwise returns FALSE. If \a subtype + is null, any text subtype is accepted; otherwise only the + specified \a subtype is accepted. + + \sa canDecode() +*/ +bool QTextDrag::decode( const QMimeSource* e, QString& str, QCString& subtype ) +{ + if(!e) + return FALSE; + + // when subtype is not specified, try text/plain first, otherwise this may read + // things like text/x-moz-url even though better targets are available + if( subtype.isNull()) { + QCString subtmp = "plain"; + if( decode( e, str, subtmp )) { + subtype = subtmp; + return true; + } + } + + if ( e->cacheType == QMimeSource::Text ) { + str = *e->cache.txt.str; + subtype = *e->cache.txt.subtype; + return TRUE; + } + + const char* mime; + for (int i=0; (mime = e->format(i)); i++) { + if ( 0==qstrnicmp(mime,"text/",5) ) { + QCString m(mime); + m = m.lower(); + int semi = m.find(';'); + if ( semi < 0 ) + semi = m.length(); + QCString foundst = m.mid(5,semi-5); + if ( subtype.isNull() || foundst == subtype ) { + bool html = !qstrnicmp(mime, "text/html", 9); + QTextCodec* codec = 0; + if (html) { + QByteArray bytes = e->encodedData(mime); + // search for the charset tag in the HTML + codec = codecForHTML(QCString(bytes.data(), bytes.size())); + } + if (!codec) + codec = qt_findcharset(m); + if ( codec ) { + QByteArray payload; + + payload = e->encodedData(mime); + if ( payload.size() ) { + int l; + if ( codec->mibEnum() != 1000) { + // length is at NUL or payload.size() + l = 0; + while ( l < (int)payload.size() && payload[l] ) + l++; + } else { + l = payload.size(); + } + + str = codec->toUnicode(payload,l); + + if ( subtype.isNull() ) + subtype = foundst; + + QMimeSource *m = (QMimeSource*)e; + m->clearCache(); + m->cacheType = QMimeSource::Text; + m->cache.txt.str = new QString( str ); + m->cache.txt.subtype = new QCString( subtype ); + + return TRUE; + } + } + } + } + } + return FALSE; +} + +/*! + Attempts to decode the dropped information in \a e into \a str. + Returns TRUE if successful; otherwise returns FALSE. + + \sa canDecode() +*/ +bool QTextDrag::decode( const QMimeSource* e, QString& str ) +{ + QCString st; + return decode(e,str,st); +} + + +/* + QImageDrag could use an internal MIME type for communicating QPixmaps + and QImages rather than always converting to raw data. This is available + for that purpose and others. It is not currently used. +*/ +class QImageDragData +{ +public: +}; + + +/*! + \class QImageDrag qdragobject.h + + \brief The QImageDrag class provides a drag and drop object for + transferring images. + + \ingroup draganddrop + + Images are offered to the receiving application in multiple + formats, determined by Qt's \link QImage::outputFormats() output + formats\endlink. + + For more information about drag and drop, see the QDragObject + class and the \link dnd.html drag and drop documentation\endlink. +*/ + +/*! + Constructs an image drag object and sets its data to \a image. \a + dragSource must be the drag source; \a name is the object name. +*/ + +QImageDrag::QImageDrag( QImage image, + QWidget * dragSource, const char * name ) + : QDragObject( dragSource, name ), + d(0) +{ + setImage( image ); +} + +/*! + Constructs a default image drag object. \a dragSource must be the + drag source; \a name is the object name. +*/ + +QImageDrag::QImageDrag( QWidget * dragSource, const char * name ) + : QDragObject( dragSource, name ), + d(0) +{ +} + + +/*! + Destroys the image drag object and frees up all allocated + resources. +*/ + +QImageDrag::~QImageDrag() +{ + // nothing +} + + +/*! + Sets the image to be dragged to \a image. You will need to call + this if you did not pass the image during construction. +*/ +void QImageDrag::setImage( QImage image ) +{ + img = image; // ### detach? + ofmts = QImage::outputFormats(); + ofmts.remove("PBM"); // remove non-raw PPM + if ( image.depth()!=32 ) { + // BMP better than PPM for paletted images + if ( ofmts.remove("BMP") ) // move to front + ofmts.insert(0,"BMP"); + } + // PNG is best of all + if ( ofmts.remove("PNG") ) // move to front + ofmts.insert(0,"PNG"); + + if(cacheType == QMimeSource::NoCache) { //cache it + cacheType = QMimeSource::Graphics; + cache.gfx.img = new QImage( img ); + cache.gfx.pix = 0; + } +} + +/*! + \reimp +*/ +const char * QImageDrag::format(int i) const +{ + if ( i < (int)ofmts.count() ) { + static QCString str; + str.sprintf("image/%s",(((QImageDrag*)this)->ofmts).at(i)); + str = str.lower(); + if ( str == "image/pbmraw" ) + str = "image/ppm"; + return str; + } else { + return 0; + } +} + +/*! + \reimp +*/ +QByteArray QImageDrag::encodedData(const char* fmt) const +{ + if ( qstrnicmp( fmt, "image/", 6 )==0 ) { + QCString f = fmt+6; + QByteArray data; + QBuffer w( data ); + w.open( IO_WriteOnly ); + QImageIO io( &w, f.upper() ); + io.setImage( img ); + if ( !io.write() ) + return QByteArray(); + w.close(); + return data; + } else { + return QByteArray(); + } +} + +/*! + Returns TRUE if the information in mime source \a e can be decoded + into an image; otherwise returns FALSE. + + \sa decode() +*/ +bool QImageDrag::canDecode( const QMimeSource* e ) { + QStrList fileFormats = QImageIO::inputFormats(); + + fileFormats.first(); + while ( fileFormats.current()) { + QCString format = fileFormats.current(); + QCString type = "image/" + format.lower(); + if ( e->provides(type.data())) + return TRUE; + fileFormats.next(); + } + + return FALSE; +} + +/*! + Attempts to decode the dropped information in mime source \a e + into \a img. Returns TRUE if successful; otherwise returns FALSE. + + \sa canDecode() +*/ +bool QImageDrag::decode( const QMimeSource* e, QImage& img ) +{ + if ( !e ) + return FALSE; + if ( e->cacheType == QMimeSource::Graphics ) { + img = *e->cache.gfx.img; + return TRUE; + } + + QByteArray payload; + QStrList fileFormats = QImageIO::inputFormats(); + // PNG is best of all + if ( fileFormats.remove("PNG") ) // move to front + fileFormats.insert(0,"PNG"); + fileFormats.first(); + while ( fileFormats.current() ) { + QCString format = fileFormats.current(); + fileFormats.next(); + + QCString type = "image/" + format.lower(); + if ( ! e->provides( type.data() ) ) continue; + payload = e->encodedData( type.data() ); + if ( !payload.isEmpty() ) + break; + } + + if ( payload.isEmpty() ) + return FALSE; + + img.loadFromData(payload); + if ( img.isNull() ) + return FALSE; + QMimeSource *m = (QMimeSource*)e; + m->clearCache(); + m->cacheType = QMimeSource::Graphics; + m->cache.gfx.img = new QImage( img ); + m->cache.gfx.pix = 0; + return TRUE; +} + +/*! + \overload + + Attempts to decode the dropped information in mime source \a e + into pixmap \a pm. Returns TRUE if successful; otherwise returns + FALSE. + + This is a convenience function that converts to a QPixmap via a + QImage. + + \sa canDecode() +*/ +bool QImageDrag::decode( const QMimeSource* e, QPixmap& pm ) +{ + if ( !e ) + return FALSE; + + if ( e->cacheType == QMimeSource::Graphics && e->cache.gfx.pix) { + pm = *e->cache.gfx.pix; + return TRUE; + } + + QImage img; + // We avoid dither, since the image probably came from this display + if ( decode( e, img ) ) { + if ( !pm.convertFromImage( img, AvoidDither ) ) + return FALSE; + // decode initialized the cache for us + + QMimeSource *m = (QMimeSource*)e; + m->cache.gfx.pix = new QPixmap( pm ); + return TRUE; + } + return FALSE; +} + + + + +/*! + \class QStoredDrag qdragobject.h + \brief The QStoredDrag class provides a simple stored-value drag object for arbitrary MIME data. + + \ingroup draganddrop + + When a block of data has only one representation, you can use a + QStoredDrag to hold it. + + For more information about drag and drop, see the QDragObject + class and the \link dnd.html drag and drop documentation\endlink. +*/ + +/*! + Constructs a QStoredDrag. The \a dragSource and \a name are passed + to the QDragObject constructor, and the format is set to \a + mimeType. + + The data will be unset. Use setEncodedData() to set it. +*/ +QStoredDrag::QStoredDrag( const char* mimeType, QWidget * dragSource, const char * name ) : + QDragObject(dragSource,name) +{ + d = new QStoredDragData(); + d->fmt = qstrdup(mimeType); +} + +/*! + Destroys the drag object and frees up all allocated resources. +*/ +QStoredDrag::~QStoredDrag() +{ + delete [] (char*)d->fmt; + delete d; +} + +/*! + \reimp +*/ +const char * QStoredDrag::format(int i) const +{ + if ( i==0 ) + return d->fmt; + else + return 0; +} + + +/*! + Sets the encoded data of this drag object to \a encodedData. The + encoded data is what's delivered to the drop sites. It must be in + a strictly defined and portable format. + + The drag object can't be dropped (by the user) until this function + has been called. +*/ + +void QStoredDrag::setEncodedData( const QByteArray & encodedData ) +{ + d->enc = encodedData.copy(); +} + +/*! + Returns the stored data. \a m contains the data's format. + + \sa setEncodedData() +*/ +QByteArray QStoredDrag::encodedData(const char* m) const +{ + if ( !qstricmp(m,d->fmt) ) + return d->enc; + else + return QByteArray(); +} + + +/*! + \class QUriDrag qdragobject.h + \brief The QUriDrag class provides a drag object for a list of URI references. + + \ingroup draganddrop + + URIs are a useful way to refer to files that may be distributed + across multiple machines. A URI will often refer to a file on a + machine local to both the drag source and the drop target, so the + URI can be equivalent to passing a file name but is more + extensible. + + Use URIs in Unicode form so that the user can comfortably edit and + view them. For use in HTTP or other protocols, use the correctly + escaped ASCII form. + + You can convert a list of file names to file URIs using + setFileNames(), or into human-readble form with setUnicodeUris(). + + Static functions are provided to convert between filenames and + URIs, e.g. uriToLocalFile() and localFileToUri(), and to and from + human-readable form, e.g. uriToUnicodeUri(), unicodeUriToUri(). + You can also decode URIs from a mimesource into a list with + decodeLocalFiles() and decodeToUnicodeUris(). +*/ + +/*! + Constructs an object to drag the list of URIs in \a uris. The \a + dragSource and \a name arguments are passed on to QStoredDrag. + Note that URIs are always in escaped UTF8 encoding. +*/ +QUriDrag::QUriDrag( QStrList uris, + QWidget * dragSource, const char * name ) : + QStoredDrag( "text/uri-list", dragSource, name ) +{ + setUris(uris); +} + +/*! + Constructs an object to drag. You must call setUris() before you + start the drag(). Passes \a dragSource and \a name to the + QStoredDrag constructor. +*/ +QUriDrag::QUriDrag( QWidget * dragSource, const char * name ) : + QStoredDrag( "text/uri-list", dragSource, name ) +{ +} + +/*! + Destroys the object. +*/ +QUriDrag::~QUriDrag() +{ +} + +/*! + Changes the list of \a uris to be dragged. + + Note that URIs are always in escaped UTF8 encoding. +*/ +void QUriDrag::setUris( QStrList uris ) +{ + QByteArray a; + int c=0; + for ( const char* s = uris.first(); s; s = uris.next() ) { + int l = qstrlen(s); + a.resize(c+l+2); + memcpy(a.data()+c,s,l); + memcpy(a.data()+c+l,"\r\n",2); + c+=l+2; + } + a.resize(c+1); + a[c] = 0; + setEncodedData(a); +} + + +/*! + Returns TRUE if decode() would be able to decode \a e; otherwise + returns FALSE. +*/ +bool QUriDrag::canDecode( const QMimeSource* e ) +{ + return e->provides( "text/uri-list" ); +} + +/*! + Decodes URIs from \a e, placing the result in \a l (which is first + cleared). + + Returns TRUE if \a e contained a valid list of URIs; otherwise + returns FALSE. +*/ +bool QUriDrag::decode( const QMimeSource* e, QStrList& l ) +{ + QByteArray payload = e->encodedData( "text/uri-list" ); + if ( payload.size() ) { + l.clear(); + l.setAutoDelete(TRUE); + uint c=0; + const char* d = payload.data(); + while (c < payload.size() && d[c]) { + uint f = c; + // Find line end + while (c < payload.size() && d[c] && d[c]!='\r' + && d[c] != '\n') + c++; + QCString s(d+f,c-f+1); + if ( s[0] != '#' ) // non-comment? + l.append( s ); + // Skip junk + while (c < payload.size() && d[c] && + (d[c]=='\n' || d[c]=='\r')) + c++; + } + return TRUE; + } + return FALSE; +} + +static uint htod( int h ) +{ + if ( isdigit(h) ) + return h - '0'; + return tolower( h ) - 'a' + 10; +} + +/*! + \fn QUriDrag::setFilenames( const QStringList & ) + \obsolete + + Use setFileNames() instead (notice the N). +*/ + +/*! + Sets the URIs to be the local-file URIs equivalent to \a fnames. + + \sa localFileToUri(), setUris() +*/ +void QUriDrag::setFileNames( const QStringList & fnames ) +{ + QStrList uris; + for ( QStringList::ConstIterator i = fnames.begin(); + i != fnames.end(); ++i ) { + QCString fileUri = localFileToUri(*i); + if (!fileUri.isEmpty()) + uris.append(fileUri); + } + setUris( uris ); +} + +/*! + Sets the URIs in \a uuris to be the Unicode URIs (only useful for + displaying to humans). + + \sa localFileToUri(), setUris() +*/ +void QUriDrag::setUnicodeUris( const QStringList & uuris ) +{ + QStrList uris; + for ( QStringList::ConstIterator i = uuris.begin(); + i != uuris.end(); ++i ) + uris.append( unicodeUriToUri(*i) ); + setUris( uris ); +} + +/*! + Returns the URI equivalent of the Unicode URI given in \a uuri + (only useful for displaying to humans). + + \sa uriToLocalFile() +*/ +QCString QUriDrag::unicodeUriToUri(const QString& uuri) +{ + QCString utf8 = uuri.utf8(); + QCString escutf8; + int n = utf8.length(); + bool isFile = uuri.startsWith("file://"); + for (int i=0; i<n; i++) { + if ( utf8[i] >= 'a' && utf8[i] <= 'z' + || utf8[i] == '/' + || utf8[i] >= '0' && utf8[i] <= '9' + || utf8[i] >= 'A' && utf8[i] <= 'Z' + + || utf8[i] == '-' || utf8[i] == '_' + || utf8[i] == '.' || utf8[i] == '!' + || utf8[i] == '~' || utf8[i] == '*' + || utf8[i] == '(' || utf8[i] == ')' + || utf8[i] == '\'' + + // Allow this through, so that all URI-references work. + || (!isFile && utf8[i] == '#') + + || utf8[i] == ';' + || utf8[i] == '?' || utf8[i] == ':' + || utf8[i] == '@' || utf8[i] == '&' + || utf8[i] == '=' || utf8[i] == '+' + || utf8[i] == '$' || utf8[i] == ',' ) + { + escutf8 += utf8[i]; + } else { + // Everything else is escaped as %HH + QCString s(4); + s.sprintf("%%%02x",(uchar)utf8[i]); + escutf8 += s; + } + } + return escutf8; +} + +/*! + Returns the URI equivalent to the absolute local file \a filename. + + \sa uriToLocalFile() +*/ +QCString QUriDrag::localFileToUri(const QString& filename) +{ + QString r = filename; + + //check that it is an absolute file + if (QDir::isRelativePath(r)) + return QCString(); + +#ifdef Q_WS_WIN + + + bool hasHost = FALSE; + // convert form network path + if (r.left(2) == "\\\\" || r.left(2) == "//") { + r.remove(0, 2); + hasHost = TRUE; + } + + // Slosh -> Slash + int slosh; + while ( (slosh=r.find('\\')) >= 0 ) { + r[slosh] = '/'; + } + + // Drive + if ( r[0] != '/' && !hasHost) + r.insert(0,'/'); + +#endif +#if defined ( Q_WS_X11 ) && 0 + // URL without the hostname is considered to be errorneous by XDnD. + // See: http://www.newplanetsoftware.com/xdnd/dragging_files.html + // This feature is not active because this would break dnd between old and new qt apps. + char hostname[257]; + if ( gethostname( hostname, 255 ) == 0 ) { + hostname[256] = '\0'; + r.prepend( QString::fromLatin1( hostname ) ); + } +#endif + return unicodeUriToUri(QString("file://" + r)); +} + +/*! + Returns the Unicode URI (only useful for displaying to humans) + equivalent of \a uri. + + Note that URIs are always in escaped UTF8 encoding. + + \sa localFileToUri() +*/ +QString QUriDrag::uriToUnicodeUri(const char* uri) +{ + QCString utf8; + + while (*uri) { + switch (*uri) { + case '%': { + uint ch = (uchar) uri[1]; + if ( ch && uri[2] ) { + ch = htod( ch ) * 16 + htod( (uchar) uri[2] ); + utf8 += (char) ch; + uri += 2; + } + } + break; + default: + utf8 += *uri; + } + ++uri; + } + + return QString::fromUtf8(utf8); +} + +/*! + Returns the name of a local file equivalent to \a uri or a null + string if \a uri is not a local file. + + Note that URIs are always in escaped UTF8 encoding. + + \sa localFileToUri() +*/ +QString QUriDrag::uriToLocalFile(const char* uri) +{ + QString file; + + if (!uri) + return file; + if (0==qstrnicmp(uri,"file:/",6)) // It is a local file uri + uri += 6; + else if (QString(uri).find(":/") != -1) // It is a different scheme uri + return file; + + bool local = uri[0] != '/' || ( uri[0] != '\0' && uri[1] == '/' ); +#ifdef Q_WS_X11 + // do we have a hostname? + if ( !local && uri[0] == '/' && uri[2] != '/' ) { + // then move the pointer to after the 'hostname/' part of the uri + const char* hostname_end = strchr( uri+1, '/' ); + if ( hostname_end != NULL ) { + char hostname[ 257 ]; + if ( gethostname( hostname, 255 ) == 0 ) { + hostname[ 256 ] = '\0'; + if ( qstrncmp( uri+1, hostname, hostname_end - ( uri+1 )) == 0 ) { + uri = hostname_end + 1; // point after the slash + local = TRUE; + } + } + } + } +#endif + if ( local ) { + file = uriToUnicodeUri(uri); + if ( uri[1] == '/' ) { + file.remove((uint)0,1); + } else { + file.insert(0,'/'); + } +#ifdef Q_WS_WIN + if ( file.length() > 2 && file[0] == '/' && file[2] == '|' ) { + file[2] = ':'; + file.remove(0,1); + } else if (file.length() > 2 && file[0] == '/' && file[1].isLetter() && file[2] == ':') { + file.remove(0, 1); + } + // Leave slash as slashes. +#endif + } +#ifdef Q_WS_WIN + else { + file = uriToUnicodeUri(uri); + // convert to network path + file.insert(1, '/'); // leave as forward slashes + } +#endif + + return file; +} + +/*! + Decodes URIs from the mime source event \a e, converts them to + local files if they refer to local files, and places them in \a l + (which is first cleared). + + Returns TRUE if \e contained a valid list of URIs; otherwise + returns FALSE. The list will be empty if no URIs were local files. +*/ +bool QUriDrag::decodeLocalFiles( const QMimeSource* e, QStringList& l ) +{ + QStrList u; + if ( !decode( e, u ) ) + return FALSE; + + l.clear(); + for (const char* s=u.first(); s; s=u.next()) { + QString lf = uriToLocalFile(s); + if ( !lf.isNull() ) + l.append( lf ); + } + return TRUE; +} + +/*! + Decodes URIs from the mime source event \a e, converts them to + Unicode URIs (only useful for displaying to humans), placing them + in \a l (which is first cleared). + + Returns TRUE if \e contained a valid list of URIs; otherwise + returns FALSE. +*/ +bool QUriDrag::decodeToUnicodeUris( const QMimeSource* e, QStringList& l ) +{ + QStrList u; + if ( !decode( e, u ) ) + return FALSE; + + l.clear(); + for (const char* s=u.first(); s; s=u.next()) + l.append( uriToUnicodeUri(s) ); + + return TRUE; +} + + +#ifndef QT_NO_DRAGANDDROP +/*! + If the source of the drag operation is a widget in this + application, this function returns that source, otherwise it + returns 0. The source of the operation is the first parameter to + drag object subclasses. + + This is useful if your widget needs special behavior when dragging + to itself, etc. + + See QDragObject::QDragObject() and subclasses. +*/ +QWidget* QDropEvent::source() const +{ + return qt_dnd_manager ? qt_dnd_manager->dragSource : 0; +} +#endif + +/*! + \class QColorDrag qdragobject.h + + \brief The QColorDrag class provides a drag and drop object for + transferring colors. + + \ingroup draganddrop + + This class provides a drag object which can be used to transfer data + about colors for drag and drop and in the clipboard. For example, it + is used in QColorDialog. + + The color is set in the constructor but can be changed with + setColor(). + + For more information about drag and drop, see the QDragObject class + and the \link dnd.html drag and drop documentation\endlink. +*/ + +/*! + Constructs a color drag object with the color \a col. Passes \a + dragsource and \a name to the QStoredDrag constructor. +*/ + +QColorDrag::QColorDrag( const QColor &col, QWidget *dragsource, const char *name ) + : QStoredDrag( "application/x-color", dragsource, name ) +{ + setColor( col ); +} + +/*! + Constructs a color drag object with a white color. Passes \a + dragsource and \a name to the QStoredDrag constructor. +*/ + +QColorDrag::QColorDrag( QWidget *dragsource, const char *name ) + : QStoredDrag( "application/x-color", dragsource, name ) +{ + setColor( Qt::white ); +} + +/*! + Sets the color of the color drag to \a col. +*/ + +void QColorDrag::setColor( const QColor &col ) +{ + short r = (col.red() << 8) | col.red(); + short g = (col.green() << 8) | col.green(); + short b = (col.blue() << 8) | col.blue(); + + // make sure we transmit data in network order + r = htons(r); + g = htons(g); + b = htons(b); + + ushort rgba[4] = { + r, g, b, + 0xffff // Alpha not supported yet. + }; + QByteArray data(sizeof(rgba)); + memcpy(data.data(), rgba, sizeof(rgba)); + setEncodedData(data); +} + +/*! + Returns TRUE if the color drag object can decode the mime source + \a e; otherwise returns FALSE. +*/ + +bool QColorDrag::canDecode( QMimeSource *e ) +{ + return e->provides( "application/x-color" ); +} + +/*! + Decodes the mime source \a e and sets the decoded values to \a + col. +*/ + +bool QColorDrag::decode( QMimeSource *e, QColor &col ) +{ + QByteArray data = e->encodedData("application/x-color"); + ushort rgba[4]; + if (data.size() != sizeof(rgba)) + return FALSE; + + memcpy(rgba, data.data(), sizeof(rgba)); + + short r = rgba[0]; + short g = rgba[1]; + short b = rgba[2]; + + // data is in network order + r = ntohs(r); + g = ntohs(g); + b = ntohs(b); + + r = (r >> 8) & 0xff; + g = (g >> 8) & 0xff; + b = (b >> 8) & 0xff; + + col.setRgb(r, g, b); + return TRUE; +} + +#endif // QT_NO_MIME diff --git a/src/kernel/qdragobject.h b/src/kernel/qdragobject.h new file mode 100644 index 0000000..d04a045 --- /dev/null +++ b/src/kernel/qdragobject.h @@ -0,0 +1,279 @@ +/**************************************************************************** +** +** Definition of QDragObject +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QDRAGOBJECT_H +#define QDRAGOBJECT_H + +class QWidget; +class QTextDragPrivate; +class QDragObjectData; +class QStoredDragData; +class QImageDragData; + +#ifndef QT_H +#include "qobject.h" +#include "qimage.h" +#include "qstrlist.h" +#include "qcolor.h" +#endif // QT_H + +#ifndef QT_NO_MIME + +class Q_EXPORT QDragObject: public QObject, public QMimeSource { + Q_OBJECT +public: + QDragObject( QWidget * dragSource = 0, const char * name = 0 ); + virtual ~QDragObject(); + +#ifndef QT_NO_DRAGANDDROP + bool drag(); + bool dragMove(); + void dragCopy(); + void dragLink(); + + virtual void setPixmap(QPixmap); + virtual void setPixmap(QPixmap, const QPoint& hotspot); + QPixmap pixmap() const; + QPoint pixmapHotSpot() const; +#endif + + QWidget * source(); + static QWidget * target(); + + static void setTarget(QWidget*); + +#ifndef QT_NO_DRAGANDDROP + enum DragMode { DragDefault, DragCopy, DragMove, DragLink, DragCopyOrMove }; + +protected: + virtual bool drag(DragMode); +#endif + +private: + QDragObjectData * d; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QDragObject( const QDragObject & ); + QDragObject &operator=( const QDragObject & ); +#endif +}; + +class Q_EXPORT QStoredDrag: public QDragObject { + Q_OBJECT + QStoredDragData * d; + +public: + QStoredDrag( const char * mimeType, + QWidget * dragSource = 0, const char * name = 0 ); + ~QStoredDrag(); + + virtual void setEncodedData( const QByteArray & ); + + const char * format(int i) const; + virtual QByteArray encodedData(const char*) const; + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QStoredDrag( const QStoredDrag & ); + QStoredDrag &operator=( const QStoredDrag & ); +#endif +}; + +class Q_EXPORT QTextDrag: public QDragObject { + Q_OBJECT + QTextDragPrivate* d; +public: + QTextDrag( const QString &, + QWidget * dragSource = 0, const char * name = 0 ); + QTextDrag( QWidget * dragSource = 0, const char * name = 0 ); + ~QTextDrag(); + + virtual void setText( const QString &); + virtual void setSubtype( const QCString &); + + const char * format(int i) const; + virtual QByteArray encodedData(const char*) const; + + static bool canDecode( const QMimeSource* e ); + static bool decode( const QMimeSource* e, QString& s ); + static bool decode( const QMimeSource* e, QString& s, QCString& subtype ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QTextDrag( const QTextDrag & ); + QTextDrag &operator=( const QTextDrag & ); +#endif +}; + +class Q_EXPORT QImageDrag: public QDragObject { + Q_OBJECT + QImage img; + QStrList ofmts; + QImageDragData* d; + +public: + QImageDrag( QImage image, QWidget * dragSource = 0, const char * name = 0 ); + QImageDrag( QWidget * dragSource = 0, const char * name = 0 ); + ~QImageDrag(); + + virtual void setImage( QImage image ); + + const char * format(int i) const; + virtual QByteArray encodedData(const char*) const; + + static bool canDecode( const QMimeSource* e ); + static bool decode( const QMimeSource* e, QImage& i ); + static bool decode( const QMimeSource* e, QPixmap& i ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QImageDrag( const QImageDrag & ); + QImageDrag &operator=( const QImageDrag & ); +#endif +}; + + +class Q_EXPORT QUriDrag: public QStoredDrag { + Q_OBJECT + +public: + QUriDrag( QStrList uris, QWidget * dragSource = 0, const char * name = 0 ); + QUriDrag( QWidget * dragSource = 0, const char * name = 0 ); + ~QUriDrag(); + + void setFilenames( const QStringList & fnames ) { setFileNames( fnames ); } + void setFileNames( const QStringList & fnames ); + void setUnicodeUris( const QStringList & uuris ); + virtual void setUris( QStrList uris ); + + static QString uriToLocalFile(const char*); + static QCString localFileToUri(const QString&); + static QString uriToUnicodeUri(const char*); + static QCString unicodeUriToUri(const QString&); + static bool canDecode( const QMimeSource* e ); + static bool decode( const QMimeSource* e, QStrList& i ); + static bool decodeToUnicodeUris( const QMimeSource* e, QStringList& i ); + static bool decodeLocalFiles( const QMimeSource* e, QStringList& i ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QUriDrag( const QUriDrag & ); + QUriDrag &operator=( const QUriDrag & ); +#endif +}; + +class Q_EXPORT QColorDrag : public QStoredDrag +{ + Q_OBJECT + QColor color; + +public: + QColorDrag( const QColor &col, QWidget *dragsource = 0, const char *name = 0 ); + QColorDrag( QWidget * dragSource = 0, const char * name = 0 ); + void setColor( const QColor &col ); + + static bool canDecode( QMimeSource * ); + static bool decode( QMimeSource *, QColor &col ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QColorDrag( const QColorDrag & ); + QColorDrag &operator=( const QColorDrag & ); +#endif +}; + +#ifndef QT_NO_COMPAT +typedef QUriDrag QUrlDrag; +#endif + +#ifndef QT_NO_DRAGANDDROP + +// QDragManager is not part of the public API. It is defined in a +// header file simply so different .cpp files can implement different +// member functions. +// + +class Q_EXPORT QDragManager: public QObject { + Q_OBJECT + +private: + QDragManager(); + ~QDragManager(); + // only friend classes can use QDragManager. + friend class QDragObject; + friend class QDragMoveEvent; + friend class QDropEvent; + friend class QApplication; + + bool eventFilter( QObject *, QEvent * ); + void timerEvent( QTimerEvent* ); + + bool drag( QDragObject *, QDragObject::DragMode ); + + void cancel( bool deleteSource = TRUE ); + void move( const QPoint & ); + void drop(); + void updatePixmap(); + void updatePixmap( const QPoint& cursorPos ); + +private: + QDragObject * object; + bool updateMode( ButtonState newstate ); + void updateCursor(); +#if defined(Q_WS_X11) + void createCursors(); +#endif + + QWidget * dragSource; + QWidget * dropWidget; + bool beingCancelled; + bool restoreCursor; + bool willDrop; + + QPixmap *pm_cursor; + int n_cursor; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QDragManager( const QDragManager & ); + QDragManager &operator=( const QDragManager & ); +#endif +}; + +#endif + +#endif // QT_NO_MIME + +#endif // QDRAGOBJECT_H diff --git a/src/kernel/qdrawutil.cpp b/src/kernel/qdrawutil.cpp new file mode 100644 index 0000000..bcac422 --- /dev/null +++ b/src/kernel/qdrawutil.cpp @@ -0,0 +1,957 @@ +/**************************************************************************** +** +** Implementation of draw utilities +** +** Created : 950920 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qdrawutil.h" +#ifndef QT_NO_DRAWUTIL +#include "qbitmap.h" +#include "qpixmapcache.h" +#include "qapplication.h" +#include "qpainter.h" + +/*! + \relates QPainter + + \c{#include <qdrawutil.h>} + + Draws a horizontal (\a y1 == \a y2) or vertical (\a x1 == \a x2) + shaded line using the painter \a p. + + Nothing is drawn if \a y1 != \a y2 and \a x1 != \a x2 (i.e. the + line is neither horizontal nor vertical). + + The color group argument \a g specifies the shading colors (\link + QColorGroup::light() light\endlink, \link QColorGroup::dark() + dark\endlink and \link QColorGroup::mid() middle\endlink colors). + + The line appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The \a lineWidth argument specifies the line width for each of the + lines. It is not the total line width. + + The \a midLineWidth argument specifies the width of a middle line + drawn in the QColorGroup::mid() color. + + If you want to use a QFrame widget instead, you can make it + display a shaded line, for example \c{QFrame::setFrameStyle( + QFrame::HLine | QFrame::Sunken )}. + + \warning This function does not look at QWidget::style() or + QApplication::style(). Use the drawing functions in QStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadeRect(), qDrawShadePanel(), QStyle::drawPrimitive() +*/ + +void qDrawShadeLine( QPainter *p, int x1, int y1, int x2, int y2, + const QColorGroup &g, bool sunken, + int lineWidth, int midLineWidth ) +{ + if (!( p && lineWidth >= 0 && midLineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawShadeLine invalid parameters." ); +#endif + return; + } + int tlw = lineWidth*2 + midLineWidth; // total line width + QPen oldPen = p->pen(); // save pen + if ( sunken ) + p->setPen( g.dark() ); + else + p->setPen( g.light() ); + QPointArray a; + int i; + if ( y1 == y2 ) { // horizontal line + int y = y1 - tlw/2; + if ( x1 > x2 ) { // swap x1 and x2 + int t = x1; + x1 = x2; + x2 = t; + } + x2--; + for ( i=0; i<lineWidth; i++ ) { // draw top shadow + a.setPoints( 3, x1+i, y+tlw-1-i, + x1+i, y+i, + x2-i, y+i ); + p->drawPolyline( a ); + } + if ( midLineWidth > 0 ) { + p->setPen( g.mid() ); + for ( i=0; i<midLineWidth; i++ ) // draw lines in the middle + p->drawLine( x1+lineWidth, y+lineWidth+i, + x2-lineWidth, y+lineWidth+i ); + } + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + for ( i=0; i<lineWidth; i++ ) { // draw bottom shadow + a.setPoints( 3, x1+i, y+tlw-i-1, + x2-i, y+tlw-i-1, + x2-i, y+i+1 ); + p->drawPolyline( a ); + } + } + else if ( x1 == x2 ) { // vertical line + int x = x1 - tlw/2; + if ( y1 > y2 ) { // swap y1 and y2 + int t = y1; + y1 = y2; + y2 = t; + } + y2--; + for ( i=0; i<lineWidth; i++ ) { // draw left shadow + a.setPoints( 3, x+i, y2, + x+i, y1+i, + x+tlw-1, y1+i ); + p->drawPolyline( a ); + } + if ( midLineWidth > 0 ) { + p->setPen( g.mid() ); + for ( i=0; i<midLineWidth; i++ ) // draw lines in the middle + p->drawLine( x+lineWidth+i, y1+lineWidth, x+lineWidth+i, y2 ); + } + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + for ( i=0; i<lineWidth; i++ ) { // draw right shadow + a.setPoints( 3, x+lineWidth, y2-i, + x+tlw-i-1, y2-i, + x+tlw-i-1, y1+lineWidth ); + p->drawPolyline( a ); + } + } + p->setPen( oldPen ); +} + + +/*! + \relates QPainter + + \c{#include <qdrawutil.h>} + + Draws the shaded rectangle specified by (\a x, \a y, \a w, \a h) + using the painter \a p. + + The color group argument \a g specifies the shading colors (\link + QColorGroup::light() light\endlink, \link QColorGroup::dark() + dark\endlink and \link QColorGroup::mid() middle\endlink colors). + + The rectangle appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The \a lineWidth argument specifies the line width for each of the + lines. It is not the total line width. + + The \a midLineWidth argument specifies the width of a middle line + drawn in the QColorGroup::mid() color. + + The rectangle's interior is filled with the \a fill brush unless + \a fill is 0. + + If you want to use a QFrame widget instead, you can make it + display a shaded rectangle, for example \c{QFrame::setFrameStyle( + QFrame::Box | QFrame::Raised )}. + + \warning This function does not look at QWidget::style() or + QApplication::style(). Use the drawing functions in QStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadeLine(), qDrawShadePanel(), qDrawPlainRect(), + QStyle::drawItem(), QStyle::drawControl() + QStyle::drawComplexControl() +*/ + +void qDrawShadeRect( QPainter *p, int x, int y, int w, int h, + const QColorGroup &g, bool sunken, + int lineWidth, int midLineWidth, + const QBrush *fill ) +{ + if ( w == 0 || h == 0 ) + return; + if ( ! ( w > 0 && h > 0 && lineWidth >= 0 && midLineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawShadeRect(): Invalid parameters" ); +#endif + return; + } + QPen oldPen = p->pen(); + if ( sunken ) + p->setPen( g.dark() ); + else + p->setPen( g.light() ); + int x1=x, y1=y, x2=x+w-1, y2=y+h-1; + QPointArray a; + + if ( lineWidth == 1 && midLineWidth == 0 ) {// standard shade rectangle + p->drawRect( x1, y1, w-1, h-1 ); + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + a.setPoints( 8, x1+1,y1+1, x2-2,y1+1, x1+1,y1+2, x1+1,y2-2, + x1,y2, x2,y2, x2,y1, x2,y2-1 ); + p->drawLineSegments( a ); // draw bottom/right lines + } else { // more complicated + int m = lineWidth+midLineWidth; + int i, j=0, k=m; + for ( i=0; i<lineWidth; i++ ) { // draw top shadow + a.setPoints( 8, x1+i, y2-i, x1+i, y1+i, x1+i, y1+i, x2-i, y1+i, + x1+k, y2-k, x2-k, y2-k, x2-k, y2-k, x2-k, y1+k ); + p->drawLineSegments( a ); + k++; + } + p->setPen( g.mid() ); + j = lineWidth*2; + for ( i=0; i<midLineWidth; i++ ) { // draw lines in the middle + p->drawRect( x1+lineWidth+i, y1+lineWidth+i, w-j, h-j ); + j += 2; + } + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + k = m; + for ( i=0; i<lineWidth; i++ ) { // draw bottom shadow + a.setPoints( 8, x1+1+i,y2-i, x2-i, y2-i, x2-i, y2-i, x2-i, y1+i+1, + x1+k, y2-k, x1+k, y1+k, x1+k, y1+k, x2-k, y1+k ); + p->drawLineSegments( a ); + k++; + } + } + if ( fill ) { + QBrush oldBrush = p->brush(); + int tlw = lineWidth + midLineWidth; + p->setPen( Qt::NoPen ); + p->setBrush( *fill ); + p->drawRect( x+tlw, y+tlw, w-2*tlw, h-2*tlw ); + p->setBrush( oldBrush ); + } + p->setPen( oldPen ); // restore pen +} + + +/*! + \relates QPainter + + \c{#include <qdrawutil.h>} + + Draws the shaded panel specified by (\a x, \a y, \a w, \a h) using + the painter \a p. + + The color group argument \a g specifies the shading colors (\link + QColorGroup::light() light\endlink, \link QColorGroup::dark() + dark\endlink and \link QColorGroup::mid() middle\endlink colors). + + The panel appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The \a lineWidth argument specifies the line width. + + The panel's interior is filled with the \a fill brush unless \a + fill is 0. + + If you want to use a QFrame widget instead, you can make it + display a shaded panel, for example \c{QFrame::setFrameStyle( + QFrame::Panel | QFrame::Sunken )}. + + \warning This function does not look at QWidget::style() or + QApplication::style(). Use the drawing functions in QStyle to make + widgets that follow the current GUI style. + + \sa qDrawWinPanel(), qDrawShadeLine(), qDrawShadeRect(), + QStyle::drawPrimitive() +*/ + +void qDrawShadePanel( QPainter *p, int x, int y, int w, int h, + const QColorGroup &g, bool sunken, + int lineWidth, const QBrush *fill ) +{ + if ( w == 0 || h == 0 ) + return; + if ( !( w > 0 && h > 0 && lineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawShadePanel() Invalid parameters." ); +#endif + } + QColor shade = g.dark(); + QColor light = g.light(); + if ( fill ) { + if ( fill->color() == shade ) + shade = g.shadow(); + if ( fill->color() == light ) + light = g.midlight(); + } + QPen oldPen = p->pen(); // save pen + QPointArray a( 4*lineWidth ); + if ( sunken ) + p->setPen( shade ); + else + p->setPen( light ); + int x1, y1, x2, y2; + int i; + int n = 0; + x1 = x; + y1 = y2 = y; + x2 = x+w-2; + for ( i=0; i<lineWidth; i++ ) { // top shadow + a.setPoint( n++, x1, y1++ ); + a.setPoint( n++, x2--, y2++ ); + } + x2 = x1; + y1 = y+h-2; + for ( i=0; i<lineWidth; i++ ) { // left shadow + a.setPoint( n++, x1++, y1 ); + a.setPoint( n++, x2++, y2-- ); + } + p->drawLineSegments( a ); + n = 0; + if ( sunken ) + p->setPen( light ); + else + p->setPen( shade ); + x1 = x; + y1 = y2 = y+h-1; + x2 = x+w-1; + for ( i=0; i<lineWidth; i++ ) { // bottom shadow + a.setPoint( n++, x1++, y1-- ); + a.setPoint( n++, x2, y2-- ); + } + x1 = x2; + y1 = y; + y2 = y+h-lineWidth-1; + for ( i=0; i<lineWidth; i++ ) { // right shadow + a.setPoint( n++, x1--, y1++ ); + a.setPoint( n++, x2--, y2 ); + } + p->drawLineSegments( a ); + if ( fill ) { // fill with fill color + QBrush oldBrush = p->brush(); + p->setPen( Qt::NoPen ); + p->setBrush( *fill ); + p->drawRect( x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2 ); + p->setBrush( oldBrush ); + } + p->setPen( oldPen ); // restore pen +} + + +/*! + \internal + This function draws a rectangle with two pixel line width. + It is called from qDrawWinButton() and qDrawWinPanel(). + + c1..c4 and fill are used: + + 1 1 1 1 1 2 + 1 3 3 3 4 2 + 1 3 F F 4 2 + 1 3 F F 4 2 + 1 4 4 4 4 2 + 2 2 2 2 2 2 +*/ + +static void qDrawWinShades( QPainter *p, + int x, int y, int w, int h, + const QColor &c1, const QColor &c2, + const QColor &c3, const QColor &c4, + const QBrush *fill ) +{ + if ( w < 2 || h < 2 ) // can't do anything with that + return; + QPen oldPen = p->pen(); + QPointArray a( 3 ); + a.setPoints( 3, x, y+h-2, x, y, x+w-2, y ); + p->setPen( c1 ); + p->drawPolyline( a ); + a.setPoints( 3, x, y+h-1, x+w-1, y+h-1, x+w-1, y ); + p->setPen( c2 ); + p->drawPolyline( a ); + if ( w > 4 && h > 4 ) { + a.setPoints( 3, x+1, y+h-3, x+1, y+1, x+w-3, y+1 ); + p->setPen( c3 ); + p->drawPolyline( a ); + a.setPoints( 3, x+1, y+h-2, x+w-2, y+h-2, x+w-2, y+1 ); + p->setPen( c4 ); + p->drawPolyline( a ); + if ( fill ) { + QBrush oldBrush = p->brush(); + p->setBrush( *fill ); + p->setPen( Qt::NoPen ); + p->drawRect( x+2, y+2, w-4, h-4 ); + p->setBrush( oldBrush ); + } + } + p->setPen( oldPen ); +} + + +/*! + \relates QPainter + + \c{#include <qdrawutil.h>} + + Draws the Windows-style button specified by (\a x, \a y, \a w, \a + h) using the painter \a p. + + The color group argument \a g specifies the shading colors (\link + QColorGroup::light() light\endlink, \link QColorGroup::dark() + dark\endlink and \link QColorGroup::mid() middle\endlink colors). + + The button appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The line width is 2 pixels. + + The button's interior is filled with the \a *fill brush unless \a + fill is 0. + + \warning This function does not look at QWidget::style() or + QApplication::style(). Use the drawing functions in QStyle to make + widgets that follow the current GUI style. + + \sa qDrawWinPanel(), QStyle::drawControl() +*/ + +void qDrawWinButton( QPainter *p, int x, int y, int w, int h, + const QColorGroup &g, bool sunken, + const QBrush *fill ) +{ + if ( sunken ) + qDrawWinShades( p, x, y, w, h, + g.shadow(), g.light(), g.dark(), g.button(), fill ); + else + qDrawWinShades( p, x, y, w, h, + g.light(), g.shadow(), g.button(), g.dark(), fill ); +} + +/*! + \relates QPainter + + \c{#include <qdrawutil.h>} + + Draws the Windows-style panel specified by (\a x, \a y, \a w, \a + h) using the painter \a p. + + The color group argument \a g specifies the shading colors. + + The panel appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The line width is 2 pixels. + + The button's interior is filled with the \a fill brush unless \a + fill is 0. + + If you want to use a QFrame widget instead, you can make it + display a shaded panel, for example \c{QFrame::setFrameStyle( + QFrame::WinPanel | QFrame::Raised )}. + + \warning This function does not look at QWidget::style() or + QApplication::style(). Use the drawing functions in QStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadePanel(), qDrawWinButton(), QStyle::drawPrimitive() +*/ + +void qDrawWinPanel( QPainter *p, int x, int y, int w, int h, + const QColorGroup &g, bool sunken, + const QBrush *fill ) +{ + if ( sunken ) + qDrawWinShades( p, x, y, w, h, + g.dark(), g.light(), g.shadow(), g.midlight(), fill ); + else + qDrawWinShades( p, x, y, w, h, + g.light(), g.shadow(), g.midlight(), g.dark(), fill ); +} + + +/*! + \relates QPainter + + \c{#include <qdrawutil.h>} + + Draws the plain rectangle specified by (\a x, \a y, \a w, \a h) + using the painter \a p. + + The color argument \a c specifies the line color. + + The \a lineWidth argument specifies the line width. + + The rectangle's interior is filled with the \a fill brush unless + \a fill is 0. + + If you want to use a QFrame widget instead, you can make it + display a plain rectangle, for example \c{QFrame::setFrameStyle( + QFrame::Box | QFrame::Plain )}. + + \warning This function does not look at QWidget::style() or + QApplication::style(). Use the drawing functions in QStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadeRect(), QStyle::drawPrimitive() +*/ + +void qDrawPlainRect( QPainter *p, int x, int y, int w, int h, const QColor &c, + int lineWidth, const QBrush *fill ) +{ + if ( w == 0 || h == 0 ) + return; + if ( !( w > 0 && h > 0 && lineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawPlainRect() Invalid parameters." ); +#endif + } + QPen oldPen = p->pen(); + QBrush oldBrush = p->brush(); + p->setPen( c ); + p->setBrush( Qt::NoBrush ); + for ( int i=0; i<lineWidth; i++ ) + p->drawRect( x+i, y+i, w-i*2, h-i*2 ); + if ( fill ) { // fill with fill color + p->setPen( Qt::NoPen ); + p->setBrush( *fill ); + p->drawRect( x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2 ); + } + p->setPen( oldPen ); + p->setBrush( oldBrush ); +} + + +QRect qItemRect( QPainter *p, Qt::GUIStyle gs, + int x, int y, int w, int h, + int flags, + bool enabled, + const QPixmap *pixmap, + const QString& text, int len ) +{ + QRect result; + + if ( pixmap ) { + if ( (flags & Qt::AlignVCenter) == Qt::AlignVCenter ) + y += h/2 - pixmap->height()/2; + else if ( (flags & Qt::AlignBottom) == Qt::AlignBottom) + y += h - pixmap->height(); + if ( (flags & Qt::AlignRight) == Qt::AlignRight ) + x += w - pixmap->width(); + else if ( (flags & Qt::AlignHCenter) == Qt::AlignHCenter ) + x += w/2 - pixmap->width()/2; + else if ( (flags & Qt::AlignLeft) != Qt::AlignLeft && QApplication::reverseLayout() ) + x += w - pixmap->width(); + result = QRect(x, y, pixmap->width(), pixmap->height()); + } else if ( !text.isNull() && p ) { + result = p->boundingRect( x, y, w, h, flags, text, len ); + if ( gs == Qt::WindowsStyle && !enabled ) { + result.setWidth(result.width()+1); + result.setHeight(result.height()+1); + } + } else { + result = QRect(x, y, w, h); + } + + return result; +} + + +void qDrawItem( QPainter *p, Qt::GUIStyle gs, + int x, int y, int w, int h, + int flags, + const QColorGroup &g, bool enabled, + const QPixmap *pixmap, + const QString& text, int len , const QColor* penColor ) +{ + p->setPen( penColor?*penColor:g.foreground() ); + if ( pixmap ) { + QPixmap pm( *pixmap ); + bool clip = (flags & Qt::DontClip) == 0; + if ( clip ) { + if ( pm.width() < w && pm.height() < h ) + clip = FALSE; + else + p->setClipRect( x, y, w, h ); + } + if ( (flags & Qt::AlignVCenter) == Qt::AlignVCenter ) + y += h/2 - pm.height()/2; + else if ( (flags & Qt::AlignBottom) == Qt::AlignBottom) + y += h - pm.height(); + if ( (flags & Qt::AlignRight) == Qt::AlignRight ) + x += w - pm.width(); + else if ( (flags & Qt::AlignHCenter) == Qt::AlignHCenter ) + x += w/2 - pm.width()/2; + else if ( ((flags & Qt::AlignLeft) != Qt::AlignLeft) && QApplication::reverseLayout() ) // AlignAuto && rightToLeft + x += w - pm.width(); + + if ( !enabled ) { + if ( pm.mask() ) { // pixmap with a mask + if ( !pm.selfMask() ) { // mask is not pixmap itself + QPixmap pmm( *pm.mask() ); + pmm.setMask( *((QBitmap *)&pmm) ); + pm = pmm; + } + } else if ( pm.depth() == 1 ) { // monochrome pixmap, no mask + pm.setMask( *((QBitmap *)&pm) ); +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + } else { // color pixmap, no mask + QString k; + k.sprintf( "$qt-drawitem-%x", pm.serialNumber() ); + QPixmap *mask = QPixmapCache::find(k); + bool del=FALSE; + if ( !mask ) { + mask = new QPixmap( pm.createHeuristicMask() ); + mask->setMask( *((QBitmap*)mask) ); + del = !QPixmapCache::insert( k, mask ); + } + pm = *mask; + if (del) delete mask; +#endif + } + if ( gs == Qt::WindowsStyle ) { + p->setPen( g.light() ); + p->drawPixmap( x+1, y+1, pm ); + p->setPen( g.text() ); + } + } + p->drawPixmap( x, y, pm ); + if ( clip ) + p->setClipping( FALSE ); + } else if ( !text.isNull() ) { + if ( gs == Qt::WindowsStyle && !enabled ) { + p->setPen( g.light() ); + p->drawText( x+1, y+1, w, h, flags, text, len ); + p->setPen( g.text() ); + } + p->drawText( x, y, w, h, flags, text, len ); + } +} + + +/***************************************************************************** + Overloaded functions. + *****************************************************************************/ + +/*! + \overload void qDrawShadeLine( QPainter *p, const QPoint &p1, const QPoint &p2, const QColorGroup &g, bool sunken, int lineWidth, int midLineWidth ) +*/ + +void qDrawShadeLine( QPainter *p, const QPoint &p1, const QPoint &p2, + const QColorGroup &g, bool sunken, + int lineWidth, int midLineWidth ) +{ + qDrawShadeLine( p, p1.x(), p1.y(), p2.x(), p2.y(), g, sunken, + lineWidth, midLineWidth ); +} + +/*! + \overload void qDrawShadeRect( QPainter *p, const QRect &r, const QColorGroup &g, bool sunken, int lineWidth, int midLineWidth, const QBrush *fill ) +*/ + +void qDrawShadeRect( QPainter *p, const QRect &r, + const QColorGroup &g, bool sunken, + int lineWidth, int midLineWidth, + const QBrush *fill ) +{ + qDrawShadeRect( p, r.x(), r.y(), r.width(), r.height(), g, sunken, + lineWidth, midLineWidth, fill ); +} + +/*! + \overload void qDrawShadePanel( QPainter *p, const QRect &r, const QColorGroup &g, bool sunken, int lineWidth, const QBrush *fill ) +*/ + +void qDrawShadePanel( QPainter *p, const QRect &r, + const QColorGroup &g, bool sunken, + int lineWidth, const QBrush *fill ) +{ + qDrawShadePanel( p, r.x(), r.y(), r.width(), r.height(), g, sunken, + lineWidth, fill ); +} + +/*! + \overload void qDrawWinButton( QPainter *p, const QRect &r, const QColorGroup &g, bool sunken, const QBrush *fill ) +*/ + +void qDrawWinButton( QPainter *p, const QRect &r, + const QColorGroup &g, bool sunken, + const QBrush *fill ) +{ + qDrawWinButton( p, r.x(), r.y(), r.width(), r.height(), g, sunken, fill ); +} + +/*! + \overload void qDrawWinPanel( QPainter *p, const QRect &r, const QColorGroup &g, bool sunken, const QBrush *fill ) +*/ + +void qDrawWinPanel( QPainter *p, const QRect &r, + const QColorGroup &g, bool sunken, + const QBrush *fill ) +{ + qDrawWinPanel( p, r.x(), r.y(), r.width(), r.height(), g, sunken, fill ); +} + +/*! + \overload void qDrawPlainRect( QPainter *p, const QRect &r, const QColor &c, int lineWidth, const QBrush *fill ) +*/ + +void qDrawPlainRect( QPainter *p, const QRect &r, const QColor &c, + int lineWidth, const QBrush *fill ) +{ + qDrawPlainRect( p, r.x(), r.y(), r.width(), r.height(), c, + lineWidth, fill ); +} + + +static void qDrawWinArrow( QPainter *p, Qt::ArrowType type, bool down, + int x, int y, int w, int h, + const QColorGroup &g, bool enabled ) +{ + QPointArray a; // arrow polygon + switch ( type ) { + case Qt::UpArrow: + a.setPoints( 7, -3,1, 3,1, -2,0, 2,0, -1,-1, 1,-1, 0,-2 ); + break; + case Qt::DownArrow: + a.setPoints( 7, -3,-1, 3,-1, -2,0, 2,0, -1,1, 1,1, 0,2 ); + break; + case Qt::LeftArrow: + a.setPoints( 7, 1,-3, 1,3, 0,-2, 0,2, -1,-1, -1,1, -2,0 ); + break; + case Qt::RightArrow: + a.setPoints( 7, -1,-3, -1,3, 0,-2, 0,2, 1,-1, 1,1, 2,0 ); + break; + } + if ( a.isNull() ) + return; + + if ( down ) { + x++; + y++; + } + + QPen savePen = p->pen(); // save current pen + if (down) + p->setBrushOrigin(p->brushOrigin() + QPoint(1,1)); + p->fillRect( x, y, w, h, g.brush( QColorGroup::Button ) ); + if (down) + p->setBrushOrigin(p->brushOrigin() - QPoint(1,1)); + if ( enabled ) { + a.translate( x+w/2, y+h/2 ); + p->setPen( g.foreground() ); + p->drawLineSegments( a, 0, 3 ); // draw arrow + p->drawPoint( a[6] ); + } else { + a.translate( x+w/2+1, y+h/2+1 ); + p->setPen( g.light() ); + p->drawLineSegments( a, 0, 3 ); // draw arrow + p->drawPoint( a[6] ); + a.translate( -1, -1 ); + p->setPen( g.mid() ); + p->drawLineSegments( a, 0, 3 ); // draw arrow + p->drawPoint( a[6] ); + } + p->setPen( savePen ); // restore pen +} + + +#if defined(Q_CC_MSVC) +#pragma warning(disable: 4244) +#endif + +#ifndef QT_NO_STYLE_MOTIF +// motif arrows look the same whether they are used or not +// is this correct? +static void qDrawMotifArrow( QPainter *p, Qt::ArrowType type, bool down, + int x, int y, int w, int h, + const QColorGroup &g, bool ) +{ + QPointArray bFill; // fill polygon + QPointArray bTop; // top shadow. + QPointArray bBot; // bottom shadow. + QPointArray bLeft; // left shadow. +#ifndef QT_NO_TRANSFORMATIONS + QWMatrix matrix; // xform matrix +#endif + bool vertical = type == Qt::UpArrow || type == Qt::DownArrow; + bool horizontal = !vertical; + int dim = w < h ? w : h; + int colspec = 0x0000; // color specification array + + if ( dim < 2 ) // too small arrow + return; + + if ( dim > 3 ) { + if ( dim > 6 ) + bFill.resize( dim & 1 ? 3 : 4 ); + bTop.resize( (dim/2)*2 ); + bBot.resize( dim & 1 ? dim + 1 : dim ); + bLeft.resize( dim > 4 ? 4 : 2 ); + bLeft.putPoints( 0, 2, 0,0, 0,dim-1 ); + if ( dim > 4 ) + bLeft.putPoints( 2, 2, 1,2, 1,dim-3 ); + bTop.putPoints( 0, 4, 1,0, 1,1, 2,1, 3,1 ); + bBot.putPoints( 0, 4, 1,dim-1, 1,dim-2, 2,dim-2, 3,dim-2 ); + + for( int i=0; i<dim/2-2 ; i++ ) { + bTop.putPoints( i*2+4, 2, 2+i*2,2+i, 5+i*2, 2+i ); + bBot.putPoints( i*2+4, 2, 2+i*2,dim-3-i, 5+i*2,dim-3-i ); + } + if ( dim & 1 ) // odd number size: extra line + bBot.putPoints( dim-1, 2, dim-3,dim/2, dim-1,dim/2 ); + if ( dim > 6 ) { // dim>6: must fill interior + bFill.putPoints( 0, 2, 1,dim-3, 1,2 ); + if ( dim & 1 ) // if size is an odd number + bFill.setPoint( 2, dim - 3, dim / 2 ); + else + bFill.putPoints( 2, 2, dim-4,dim/2-1, dim-4,dim/2 ); + } + } + else { + if ( dim == 3 ) { // 3x3 arrow pattern + bLeft.setPoints( 4, 0,0, 0,2, 1,1, 1,1 ); + bTop .setPoints( 2, 1,0, 1,0 ); + bBot .setPoints( 2, 1,2, 2,1 ); + } + else { // 2x2 arrow pattern + bLeft.setPoints( 2, 0,0, 0,1 ); + bTop .setPoints( 2, 1,0, 1,0 ); + bBot .setPoints( 2, 1,1, 1,1 ); + } + } + + if ( type == Qt::UpArrow || type == Qt::LeftArrow ) { +#ifndef QT_NO_TRANSFORMATIONS // #### fix me! + matrix.translate( x, y ); + if ( vertical ) { + matrix.translate( 0, h - 1 ); + matrix.rotate( -90 ); + } else { + matrix.translate( w - 1, h - 1 ); + matrix.rotate( 180 ); + } +#endif + if ( down ) + colspec = horizontal ? 0x2334 : 0x2343; + else + colspec = horizontal ? 0x1443 : 0x1434; + } + else if ( type == Qt::DownArrow || type == Qt::RightArrow ) { +#ifndef QT_NO_TRANSFORMATIONS // #### fix me! + matrix.translate( x, y ); + if ( vertical ) { + matrix.translate( w-1, 0 ); + matrix.rotate( 90 ); + } +#endif + if ( down ) + colspec = horizontal ? 0x2443 : 0x2434; + else + colspec = horizontal ? 0x1334 : 0x1343; + } + + QColor *cols[5]; + cols[0] = 0; + cols[1] = (QColor *)&g.button(); + cols[2] = (QColor *)&g.mid(); + cols[3] = (QColor *)&g.light(); + cols[4] = (QColor *)&g.dark(); +#define CMID *cols[ (colspec>>12) & 0xf ] +#define CLEFT *cols[ (colspec>>8) & 0xf ] +#define CTOP *cols[ (colspec>>4) & 0xf ] +#define CBOT *cols[ colspec & 0xf ] + + QPen savePen = p->pen(); // save current pen + QBrush saveBrush = p->brush(); // save current brush +#ifndef QT_NO_TRANSFORMATIONS + QWMatrix wxm = p->worldMatrix(); +#endif + QPen pen( Qt::NoPen ); + const QBrush &brush = g.brush( QColorGroup::Button ); + + p->setPen( pen ); + p->setBrush( brush ); +#ifndef QT_NO_TRANSFORMATIONS + p->setWorldMatrix( matrix, TRUE ); // set transformation matrix +#endif + p->drawPolygon( bFill ); // fill arrow + p->setBrush( Qt::NoBrush ); // don't fill + + p->setPen( CLEFT ); + p->drawLineSegments( bLeft ); + p->setPen( CTOP ); + p->drawLineSegments( bTop ); + p->setPen( CBOT ); + p->drawLineSegments( bBot ); + +#ifndef QT_NO_TRANSFORMATIONS + p->setWorldMatrix( wxm ); +#endif + p->setBrush( saveBrush ); // restore brush + p->setPen( savePen ); // restore pen + +#undef CMID +#undef CLEFT +#undef CTOP +#undef CBOT +} +#endif + +void qDrawArrow( QPainter *p, Qt::ArrowType type, Qt::GUIStyle style, bool down, + int x, int y, int w, int h, + const QColorGroup &g, bool enabled ) +{ + switch ( style ) { + case Qt::WindowsStyle: + qDrawWinArrow( p, type, down, x, y, w, h, g, enabled ); + break; +#ifndef QT_NO_STYLE_MOTIF + case Qt::MotifStyle: + qDrawMotifArrow( p, type, down, x, y, w, h, g, enabled ); + break; +#endif + default: +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawArrow: Requested GUI style not supported" ); +#else + ; +#endif + } +} +#endif //QT_NO_DRAWUTIL diff --git a/src/kernel/qdrawutil.h b/src/kernel/qdrawutil.h new file mode 100644 index 0000000..a78527c --- /dev/null +++ b/src/kernel/qdrawutil.h @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Definition of draw utilities +** +** Created : 950920 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QDRAWUTIL_H +#define QDRAWUTIL_H + +#ifndef QT_H +#include "qnamespace.h" +#include "qstring.h" // char*->QString conversion +#endif // QT_H + +class QPainter; +class QColorGroup; +class QPoint; +class QBrush; +class QRect; +class QPixmap; + +#ifndef QT_NO_DRAWUTIL +// +// Standard shade drawing +// + +Q_EXPORT void qDrawShadeLine( QPainter *p, int x1, int y1, int x2, int y2, + const QColorGroup &g, bool sunken = TRUE, + int lineWidth = 1, int midLineWidth = 0 ); + +Q_EXPORT void qDrawShadeLine( QPainter *p, const QPoint &p1, const QPoint &p2, + const QColorGroup &g, bool sunken = TRUE, + int lineWidth = 1, int midLineWidth = 0 ); + +Q_EXPORT void qDrawShadeRect( QPainter *p, int x, int y, int w, int h, + const QColorGroup &, bool sunken=FALSE, + int lineWidth = 1, int midLineWidth = 0, + const QBrush *fill = 0 ); + +Q_EXPORT void qDrawShadeRect( QPainter *p, const QRect &r, + const QColorGroup &, bool sunken=FALSE, + int lineWidth = 1, int midLineWidth = 0, + const QBrush *fill = 0 ); + +Q_EXPORT void qDrawShadePanel( QPainter *p, int x, int y, int w, int h, + const QColorGroup &, bool sunken=FALSE, + int lineWidth = 1, const QBrush *fill = 0 ); + +Q_EXPORT void qDrawShadePanel( QPainter *p, const QRect &r, + const QColorGroup &, bool sunken=FALSE, + int lineWidth = 1, const QBrush *fill = 0 ); + +Q_EXPORT void qDrawWinButton( QPainter *p, int x, int y, int w, int h, + const QColorGroup &g, bool sunken = FALSE, + const QBrush *fill = 0 ); + +Q_EXPORT void qDrawWinButton( QPainter *p, const QRect &r, + const QColorGroup &g, bool sunken = FALSE, + const QBrush *fill = 0 ); + +Q_EXPORT void qDrawWinPanel( QPainter *p, int x, int y, int w, int h, + const QColorGroup &, bool sunken=FALSE, + const QBrush *fill = 0 ); + +Q_EXPORT void qDrawWinPanel( QPainter *p, const QRect &r, + const QColorGroup &, bool sunken=FALSE, + const QBrush *fill = 0 ); + +Q_EXPORT void qDrawPlainRect( QPainter *p, int x, int y, int w, int h, const QColor &, + int lineWidth = 1, const QBrush *fill = 0 ); + +Q_EXPORT void qDrawPlainRect( QPainter *p, const QRect &r, const QColor &, + int lineWidth = 1, const QBrush *fill = 0 ); + + +// +// Other obsolete drawing functions. +// Use QStyle::itemRect(), QStyle::drawItem() and QStyle::drawArrow() instead. +// +Q_EXPORT QRect qItemRect( QPainter *p, Qt::GUIStyle gs, int x, int y, int w, int h, + int flags, bool enabled, + const QPixmap *pixmap, const QString& text, int len=-1 ); + +Q_EXPORT void qDrawItem( QPainter *p, Qt::GUIStyle gs, int x, int y, int w, int h, + int flags, const QColorGroup &g, bool enabled, + const QPixmap *pixmap, const QString& text, + int len=-1, const QColor* penColor = 0 ); + +Q_EXPORT void qDrawArrow( QPainter *p, Qt::ArrowType type, Qt::GUIStyle style, bool down, + int x, int y, int w, int h, + const QColorGroup &g, bool enabled ); + +#endif // QT_NO_DRAWUTIL +#endif // QDRAWUTIL_H diff --git a/src/kernel/qdropsite.cpp b/src/kernel/qdropsite.cpp new file mode 100644 index 0000000..b1234ef --- /dev/null +++ b/src/kernel/qdropsite.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Implementation of Drag and Drop support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qdropsite.h" + +#ifndef QT_NO_DRAGANDDROP + +#include "qwidget.h" + + +// NOT REVISED +/*! + \class QDropSite qdropsite.h + \brief The QDropSite class provides nothing and does nothing. + + \obsolete + + If your code uses it, you can safely delete it. + + It was used in Qt 1.x to do some drag and drop; that has since been + folded into QWidget. + + For detailed information about drag-and-drop, see the QDragObject class. + + \sa QDragObject, QTextDrag, QImageDrag +*/ + +/*! + Constructs a QDropSite to handle events for the widget \a self. + + Pass \c this as the \a self parameter. + This enables dropping by calling QWidget::setAcceptDrops(TRUE). +*/ +QDropSite::QDropSite( QWidget* self ) +{ + self->setAcceptDrops( TRUE ); +} + +/*! + Destroys the drop site. +*/ +QDropSite::~QDropSite() +{ +} + +#endif // QT_NO_DRAGANDDROP diff --git a/src/kernel/qdropsite.h b/src/kernel/qdropsite.h new file mode 100644 index 0000000..4f629c0 --- /dev/null +++ b/src/kernel/qdropsite.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Definitation of Drag and Drop support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QDROPSITE_H +#define QDROPSITE_H + +#ifndef QT_H +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H +#endif + + +class QWidget; + + +class Q_EXPORT QDropSite { +public: + QDropSite( QWidget* parent ); + virtual ~QDropSite(); +}; + + +#endif // QDROPSITE_H diff --git a/src/kernel/qevent.cpp b/src/kernel/qevent.cpp new file mode 100644 index 0000000..c428966 --- /dev/null +++ b/src/kernel/qevent.cpp @@ -0,0 +1,2571 @@ +/**************************************************************************** +** +** Implementation of event classes +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qevent.h" +#include "qcursor.h" +#include "qapplication.h" + + +/*! + \class QEvent qevent.h + \brief The QEvent class is the base class of all + event classes. Event objects contain event parameters. + + \ingroup events + \ingroup environment + + Qt's main event loop (QApplication::exec()) fetches native window + system events from the event queue, translates them into QEvents + and sends the translated events to QObjects. + + In general, events come from the underlying window system + (spontaneous() returns TRUE) but it is also possible to manually + send events using QApplication::sendEvent() and + QApplication::postEvent() (spontaneous() returns FALSE). + + QObjects receive events by having their QObject::event() function + called. The function can be reimplemented in subclasses to + customize event handling and add additional event types; + QWidget::event() is a notable example. By default, events are + dispatched to event handlers like QObject::timerEvent() and + QWidget::mouseMoveEvent(). QObject::installEventFilter() allows an + object to intercept events destined for another object. + + The basic QEvent contains only an event type parameter. + Subclasses of QEvent contain additional parameters that describe + the particular event. + + \sa QObject::event() QObject::installEventFilter() + QWidget::event() QApplication::sendEvent() + QApplication::postEvent() QApplication::processEvents() +*/ + + +/*! + \enum Qt::ButtonState + + This enum type describes the state of the mouse and the modifier + buttons. + + \value NoButton used when the button state does not refer to any + button (see QMouseEvent::button()). + \value LeftButton set if the left button is pressed, or if this + event refers to the left button. (The left button may be + the right button on left-handed mice.) + \value RightButton the right button. + \value MidButton the middle button. + \value ShiftButton a Shift key on the keyboard is also pressed. + \value ControlButton a Ctrl key on the keyboard is also pressed. + \value AltButton an Alt key on the keyboard is also pressed. + \value MetaButton a Meta key on the keyboard is also pressed. + \value Keypad a keypad button is pressed. + \value KeyButtonMask a mask for ShiftButton, ControlButton, + AltButton and MetaButton. + \value MouseButtonMask a mask for LeftButton, RightButton and MidButton. +*/ + +/*! + \enum QEvent::Type + + This enum type defines the valid event types in Qt. The event + types and the specialized classes for each type are these: + + \value None Not an event. + \value Accessibility Accessibility information is requested + \value Timer Regular timer events, \l{QTimerEvent}. + \value MouseButtonPress Mouse press, \l{QMouseEvent}. + \value MouseButtonRelease Mouse release, \l{QMouseEvent}. + \value MouseButtonDblClick Mouse press again, \l{QMouseEvent}. + \value MouseMove Mouse move, \l{QMouseEvent}. + \value KeyPress Key press (including Shift, for example), \l{QKeyEvent}. + \value KeyRelease Key release, \l{QKeyEvent}. + \value IMStart The start of input method composition, \l{QIMEvent}. + \value IMCompose Input method composition is taking place, \l{QIMEvent}. + \value IMEnd The end of input method composition, \l{QIMEvent}. + \value FocusIn Widget gains keyboard focus, \l{QFocusEvent}. + \value FocusOut Widget loses keyboard focus, \l{QFocusEvent}. + \value Enter Mouse enters widget's boundaries. + \value Leave Mouse leaves widget's boundaries. + \value Paint Screen update necessary, \l{QPaintEvent}. + \value Move Widget's position changed, \l{QMoveEvent}. + \value Resize Widget's size changed, \l{QResizeEvent}. + \value Show Widget was shown on screen, \l{QShowEvent}. + \value Hide Widget was hidden, \l{QHideEvent}. + \value ShowToParent A child widget has been shown. + \value HideToParent A child widget has been hidden. + \value Close Widget was closed (permanently), \l{QCloseEvent}. + \value ShowNormal Widget should be shown normally (obsolete). + \value ShowMaximized Widget should be shown maximized (obsolete). + \value ShowMinimized Widget should be shown minimized (obsolete). + \value ShowFullScreen Widget should be shown full-screen (obsolete). + \value ShowWindowRequest Widget's window should be shown (obsolete). + \value DeferredDelete The object will be deleted after it has + cleaned up. + \value Accel Key press in child for shortcut key handling, \l{QKeyEvent}. + \value Wheel Mouse wheel rolled, \l{QWheelEvent}. + \value ContextMenu Context popup menu, \l{QContextMenuEvent} + \value AccelOverride Key press in child, for overriding shortcut key handling, \l{QKeyEvent}. + \value AccelAvailable internal. + \value WindowActivate Window was activated. + \value WindowDeactivate Window was deactivated. + \value CaptionChange Widget's caption changed. + \value IconChange Widget's icon changed. + \value ParentFontChange Font of the parent widget changed. + \value ApplicationFontChange Default application font changed. + \value PaletteChange Palette of the widget changed. + \value ParentPaletteChange Palette of the parent widget changed. + \value ApplicationPaletteChange Default application palette changed. + \value Clipboard Clipboard contents have changed. + \value SockAct Socket activated, used to implement \l{QSocketNotifier}. + \value DragEnter A drag-and-drop enters widget, \l{QDragEnterEvent}. + \value DragMove A drag-and-drop is in progress, \l{QDragMoveEvent}. + \value DragLeave A drag-and-drop leaves widget, \l{QDragLeaveEvent}. + \value Drop A drag-and-drop is completed, \l{QDropEvent}. + \value DragResponse Internal event used by Qt on some platforms. + \value ChildInserted Object gets a child, \l{QChildEvent}. + \value ChildRemoved Object loses a child, \l{QChildEvent}. + \value LayoutHint Widget child has changed layout properties. + \value ActivateControl Internal event used by Qt on some platforms. + \value DeactivateControl Internal event used by Qt on some platforms. + \value LanguageChange The application translation changed, \l{QTranslator} + \value LayoutDirectionChange The direction of layouts changed + \value LocaleChange The system locale changed + \value Quit Reserved. + \value Create Reserved. + \value Destroy Reserved. + \value Reparent Reserved. + \value Speech Reserved for speech input. + \value TabletMove A Wacom Tablet Move Event. + \value Style Internal use only + \value TabletPress A Wacom Tablet Press Event + \value TabletRelease A Wacom Tablet Release Event + \value OkRequest Internal event used by Qt on some platforms. + \value HelpRequest Internal event used by Qt on some platforms. + \value IconDrag Internal event used by Qt on some platforms when proxy icon is dragged. + \value WindowStateChange The window's state, i.e. minimized, + maximized or full-screen, has changed. See \l{QWidget::windowState()}. + \value WindowBlocked The window is modally blocked + \value WindowUnblocked The window leaves modal blocking + + \value User User defined event. + \value MaxUser Last user event id. + + User events should have values between User and MaxUser inclusive. +*/ +/*! + \fn QEvent::QEvent( Type type ) + + Contructs an event object of type \a type. +*/ + +/*! + \fn QEvent::Type QEvent::type() const + + Returns the event type. +*/ + +/*! + \fn bool QEvent::spontaneous() const + + Returns TRUE if the event originated outside the application, i.e. + it is a system event; otherwise returns FALSE. +*/ + + +/*! + \class QTimerEvent qevent.h + \brief The QTimerEvent class contains parameters that describe a + timer event. + + \ingroup events + + Timer events are sent at regular intervals to objects that have + started one or more timers. Each timer has a unique identifier. A + timer is started with QObject::startTimer(). + + The QTimer class provides a high-level programming interface that + uses signals instead of events. It also provides one-shot timers. + + The event handler QObject::timerEvent() receives timer events. + + \sa QTimer, QObject::timerEvent(), QObject::startTimer(), + QObject::killTimer(), QObject::killTimers() +*/ + +/*! + \fn QTimerEvent::QTimerEvent( int timerId ) + + Constructs a timer event object with the timer identifier set to + \a timerId. +*/ + +/*! + \fn int QTimerEvent::timerId() const + + Returns the unique timer identifier, which is the same identifier + as returned from QObject::startTimer(). +*/ + + +/*! + \class QMouseEvent qevent.h + \ingroup events + + \brief The QMouseEvent class contains parameters that describe a mouse event. + + Mouse events occur when a mouse button is pressed or released + inside a widget or when the mouse cursor is moved. + + Mouse move events will occur only when a mouse button is pressed + down, unless mouse tracking has been enabled with + QWidget::setMouseTracking(). + + Qt automatically grabs the mouse when a mouse button is pressed + inside a widget; the widget will continue to receive mouse events + until the last mouse button is released. + + A mouse event contains a special accept flag that indicates + whether the receiver wants the event. You should call + QMouseEvent::ignore() if the mouse event is not handled by your + widget. A mouse event is propagated up the parent widget chain + until a widget accepts it with QMouseEvent::accept() or an event + filter consumes it. + + The functions pos(), x() and y() give the cursor position relative + to the widget that receives the mouse event. If you move the + widget as a result of the mouse event, use the global position + returned by globalPos() to avoid a shaking motion. + + The QWidget::setEnabled() function can be used to enable or + disable mouse and keyboard events for a widget. + + The event handlers QWidget::mousePressEvent(), + QWidget::mouseReleaseEvent(), QWidget::mouseDoubleClickEvent() and + QWidget::mouseMoveEvent() receive mouse events. + + \sa QWidget::setMouseTracking(), QWidget::grabMouse(), + QCursor::pos() +*/ + +/*! + \fn QMouseEvent::QMouseEvent( Type type, const QPoint &pos, int button, int state ) + + Constructs a mouse event object. + + The \a type parameter must be one of \c QEvent::MouseButtonPress, + \c QEvent::MouseButtonRelease, \c QEvent::MouseButtonDblClick or + \c QEvent::MouseMove. + + The \a pos parameter specifies the position relative to the + receiving widget. \a button specifies the \link Qt::ButtonState + button\endlink that caused the event, which should be \c + Qt::NoButton (0), if \a type is \c MouseMove. \a state is the + \link Qt::ButtonState ButtonState\endlink at the time of the + event. + + The globalPos() is initialized to QCursor::pos(), which may not be + appropriate. Use the other constructor to specify the global + position explicitly. +*/ + +QMouseEvent::QMouseEvent( Type type, const QPoint &pos, int button, int state ) + : QEvent(type), p(pos), b(button),s((ushort)state), accpt(TRUE){ + g = QCursor::pos(); +} + + +/*! + \fn QMouseEvent::QMouseEvent( Type type, const QPoint &pos, const QPoint &globalPos, int button, int state ) + + Constructs a mouse event object. + + The \a type parameter must be \c QEvent::MouseButtonPress, \c + QEvent::MouseButtonRelease, \c QEvent::MouseButtonDblClick or \c + QEvent::MouseMove. + + The \a pos parameter specifies the position relative to the + receiving widget. \a globalPos is the position in absolute + coordinates. \a button specifies the \link Qt::ButtonState + button\endlink that caused the event, which should be \c + Qt::NoButton (0), if \a type is \c MouseMove. \a state is the + \link Qt::ButtonState ButtonState\endlink at the time of the + event. + +*/ + +/*! + \fn const QPoint &QMouseEvent::pos() const + + Returns the position of the mouse pointer relative to the widget + that received the event. + + If you move the widget as a result of the mouse event, use the + global position returned by globalPos() to avoid a shaking motion. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn const QPoint &QMouseEvent::globalPos() const + + Returns the global position of the mouse pointer \e{at the time + of the event}. This is important on asynchronous window systems + like X11. Whenever you move your widgets around in response to + mouse events, globalPos() may differ a lot from the current + pointer position QCursor::pos(), and from QWidget::mapToGlobal( + pos() ). + + \sa globalX(), globalY() +*/ + +/*! + \fn int QMouseEvent::x() const + + Returns the x-position of the mouse pointer, relative to the + widget that received the event. + + \sa y(), pos() +*/ + +/*! + \fn int QMouseEvent::y() const + + Returns the y-position of the mouse pointer, relative to the + widget that received the event. + + \sa x(), pos() +*/ + +/*! + \fn int QMouseEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int QMouseEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + +/*! + \fn ButtonState QMouseEvent::button() const + + Returns the button that caused the event. + + Possible return values are \c LeftButton, \c RightButton, \c + MidButton and \c NoButton. + + Note that the returned value is always \c NoButton for mouse move + events. + + \sa state() Qt::ButtonState +*/ + + +/*! + \fn ButtonState QMouseEvent::state() const + + Returns the button state (a combination of mouse buttons and + keyboard modifiers), i.e. what buttons and keys were being pressed + immediately before the event was generated. + + This means that if you have a \c QEvent::MouseButtonPress or a \c + QEvent::MouseButtonDblClick state() will \e not include the mouse + button that's pressed. But once the mouse button has been + released, the \c QEvent::MouseButtonRelease event will have the + button() that was pressed. + + This value is mainly interesting for \c QEvent::MouseMove; for the + other cases, button() is more useful. + + The returned value is \c LeftButton, \c RightButton, \c MidButton, + \c ShiftButton, \c ControlButton and \c AltButton OR'ed together. + + \sa button() stateAfter() Qt::ButtonState +*/ + +/*! + \fn ButtonState QMouseEvent::stateAfter() const + + Returns the state of buttons after the event. + + \sa state() Qt::ButtonState +*/ +Qt::ButtonState QMouseEvent::stateAfter() const +{ + return Qt::ButtonState(state()^button()); +} + + + +/*! + \fn bool QMouseEvent::isAccepted() const + + Returns TRUE if the receiver of the event wants to keep the key; + otherwise returns FALSE. +*/ + +/*! + \fn void QMouseEvent::accept() + + Sets the accept flag of the mouse event object. + + Setting the accept parameter indicates that the receiver of the + event wants the mouse event. Unwanted mouse events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + + +/*! + \fn void QMouseEvent::ignore() + + Clears the accept flag parameter of the mouse event object. + + Clearing the accept parameter indicates that the event receiver + does not want the mouse event. Unwanted mouse events are sent to + the parent widget. + + The accept flag is set by default. + + \sa accept() +*/ + + +/*! + \class QWheelEvent qevent.h + \brief The QWheelEvent class contains parameters that describe a wheel event. + + \ingroup events + + Wheel events are sent to the widget under the mouse, and if that widget + does not handle the event they are sent to the focus widget. The rotation + distance is provided by delta(). The functions pos() and globalPos() return + the mouse pointer location at the time of the event. + + A wheel event contains a special accept flag that indicates + whether the receiver wants the event. You should call + QWheelEvent::accept() if you handle the wheel event; otherwise it + will be sent to the parent widget. + + The QWidget::setEnable() function can be used to enable or disable + mouse and keyboard events for a widget. + + The event handler QWidget::wheelEvent() receives wheel events. + + \sa QMouseEvent, QWidget::grabMouse() +*/ + +/*! + \fn Orientation QWheelEvent::orientation() const + + Returns the wheel's orientation. +*/ + +/*! + \fn QWheelEvent::QWheelEvent( const QPoint &pos, int delta, int state, Orientation orient = Vertical ); + + Constructs a wheel event object. + + The globalPos() is initialized to QCursor::pos(), i.e. \a pos, + which is usually (but not always) right. Use the other constructor + if you need to specify the global position explicitly. \a delta + contains the rotation distance, \a state holds the keyboard + modifier flags at the time of the event and \a orient holds the + wheel's orientation. + + \sa pos(), delta(), state() +*/ +#ifndef QT_NO_WHEELEVENT +QWheelEvent::QWheelEvent( const QPoint &pos, int delta, int state, Orientation orient ) + : QEvent(Wheel), p(pos), d(delta), s((ushort)state), + accpt(TRUE), o(orient) +{ + g = QCursor::pos(); +} +#endif +/*! + \fn QWheelEvent::QWheelEvent( const QPoint &pos, const QPoint& globalPos, int delta, int state, Orientation orient = Vertical ) + + Constructs a wheel event object. The position when the event + occurred is given in \a pos and \a globalPos. \a delta contains + the rotation distance, \a state holds the keyboard modifier flags + at the time of the event and \a orient holds the wheel's + orientation. + + \sa pos(), globalPos(), delta(), state() +*/ + +/*! + \fn int QWheelEvent::delta() const + + Returns the distance that the wheel is rotated expressed in + multiples or divisions of the \e{wheel delta}, which is currently + defined to be 120. A positive value indicates that the wheel was + rotated forwards away from the user; a negative value indicates + that the wheel was rotated backwards toward the user. + + The \e{wheel delta} constant was defined to be 120 by wheel mouse + vendors to allow building finer-resolution wheels in the future, + including perhaps a freely rotating wheel with no notches. The + expectation is that such a device would send more messages per + rotation but with a smaller value in each message. +*/ + +/*! + \fn const QPoint &QWheelEvent::pos() const + + Returns the position of the mouse pointer, relative to the widget + that received the event. + + If you move your widgets around in response to mouse + events, use globalPos() instead of this function. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn int QWheelEvent::x() const + + Returns the x-position of the mouse pointer, relative to the + widget that received the event. + + \sa y(), pos() +*/ + +/*! + \fn int QWheelEvent::y() const + + Returns the y-position of the mouse pointer, relative to the + widget that received the event. + + \sa x(), pos() +*/ + + +/*! + \fn const QPoint &QWheelEvent::globalPos() const + + Returns the global position of the mouse pointer \e{at the time + of the event}. This is important on asynchronous window systems + such as X11; whenever you move your widgets around in response to + mouse events, globalPos() can differ a lot from the current + pointer position QCursor::pos(). + + \sa globalX(), globalY() +*/ + +/*! + \fn int QWheelEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int QWheelEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + + +/*! + \fn ButtonState QWheelEvent::state() const + + Returns the keyboard modifier flags of the event. + + The returned value is \c ShiftButton, \c ControlButton, and \c + AltButton OR'ed together. +*/ + +/*! + \fn bool QWheelEvent::isAccepted() const + + Returns TRUE if the receiver of the event handles the wheel event; + otherwise returns FALSE. +*/ + +/*! + \fn void QWheelEvent::accept() + + Sets the accept flag of the wheel event object. + + Setting the accept parameter indicates that the receiver of the + event wants the wheel event. Unwanted wheel events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + +/*! + \fn void QWheelEvent::ignore() + + Clears the accept flag parameter of the wheel event object. + + Clearing the accept parameter indicates that the event receiver + does not want the wheel event. Unwanted wheel events are sent to + the parent widget. The accept flag is set by default. + + \sa accept() +*/ + + +/*! + \enum Qt::Modifier + + This enum type describes the keyboard modifier keys supported by + Qt. + + \value SHIFT the Shift keys provided on all standard keyboards. + \value META the Meta keys. + \value CTRL the Ctrl keys. + \value ALT the normal Alt keys, but not e.g. AltGr. + \value MODIFIER_MASK is a mask of Shift, Ctrl, Alt and Meta. + \value UNICODE_ACCEL the accelerator is specified as a Unicode code + point, not as a Qt Key. +*/ + +/*! + \class QKeyEvent qevent.h + \brief The QKeyEvent class contains describes a key event. + + \ingroup events + + Key events occur when a key is pressed or released when a widget + has keyboard input focus. + + A key event contains a special accept flag that indicates whether the + receiver wants the key event. You should call QKeyEvent::ignore() if the + key press or release event is not handled by your widget. A key event is + propagated up the parent widget chain until a widget accepts it with + QKeyEvent::accept() or an event filter consumes it. + Key events for multi media keys are ignored by default. You should call + QKeyEvent::accept() if your widget handles those events. + + The QWidget::setEnable() function can be used to enable or disable + mouse and keyboard events for a widget. + + The event handlers QWidget::keyPressEvent() and + QWidget::keyReleaseEvent() receive key events. + + \sa QFocusEvent, QWidget::grabKeyboard() +*/ + +/*! + \fn QKeyEvent::QKeyEvent( Type type, int key, int ascii, int state, + const QString& text, bool autorep, ushort count ) + + Constructs a key event object. + + The \a type parameter must be \c QEvent::KeyPress or \c + QEvent::KeyRelease. If \a key is 0 the event is not a result of a + known key (e.g. it may be the result of a compose sequence or + keyboard macro). \a ascii is the ASCII code of the key that was + pressed or released. \a state holds the keyboard modifiers. \a + text is the Unicode text that the key generated. If \a autorep is + TRUE, isAutoRepeat() will be TRUE. \a count is the number of + single keys. + + The accept flag is set to TRUE. +*/ + +/*! + \fn int QKeyEvent::key() const + + Returns the code of the key that was pressed or released. + + See \l Qt::Key for the list of keyboard codes. These codes are + independent of the underlying window system. + + A value of either 0 or Key_unknown means that the event is not + the result of a known key (e.g. it may be the result of a compose + sequence or a keyboard macro, or due to key event compression). + + Applications should not use the Qt latin 1 keycodes between 128 + and 255, but should rather use the QKeyEvent::text(). This is + mainly for compatibility. + + \sa QWidget::setKeyCompression() +*/ + +/*! + \fn int QKeyEvent::ascii() const + + Returns the ASCII code of the key that was pressed or released. We + recommend using text() instead. + + \sa text() +*/ + +/*! + \fn QString QKeyEvent::text() const + + Returns the Unicode text that this key generated. The text returned + migth be empty, which is the case when pressing or + releasing modifying keys as Shift, Control, Alt and Meta. In these + cases key() will contain a valid value. + + \sa QWidget::setKeyCompression() +*/ + +/*! + \fn ButtonState QKeyEvent::state() const + + Returns the keyboard modifier flags that existed immediately + before the event occurred. + + The returned value is \c ShiftButton, \c ControlButton, \c AltButton + and \c MetaButton OR'ed together. + + \sa stateAfter() +*/ + +/*! + \fn ButtonState QKeyEvent::stateAfter() const + + Returns the keyboard modifier flags that existed immediately after + the event occurred. + + \warning This function cannot be trusted. + + \sa state() +*/ +//###### We must check with XGetModifierMapping +Qt::ButtonState QKeyEvent::stateAfter() const +{ + if ( key() == Key_Shift ) + return Qt::ButtonState(state()^ShiftButton); + if ( key() == Key_Control ) + return Qt::ButtonState(state()^ControlButton); + if ( key() == Key_Alt ) + return Qt::ButtonState(state()^AltButton); + if ( key() == Key_Meta ) + return Qt::ButtonState(state()^MetaButton); + return state(); +} + +/*! + \fn bool QKeyEvent::isAccepted() const + + Returns TRUE if the receiver of the event wants to keep the key; + otherwise returns FALSE +*/ + +/*! + \fn void QKeyEvent::accept() + + Sets the accept flag of the key event object. + + Setting the accept parameter indicates that the receiver of the + event wants the key event. Unwanted key events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + +/*! + \fn bool QKeyEvent::isAutoRepeat() const + + Returns TRUE if this event comes from an auto-repeating key and + FALSE if it comes from an initial key press. + + Note that if the event is a multiple-key compressed event that is + partly due to auto-repeat, this function could return either TRUE + or FALSE indeterminately. +*/ + +/*! + \fn int QKeyEvent::count() const + + Returns the number of single keys for this event. If text() is not + empty, this is simply the length of the string. + + \sa QWidget::setKeyCompression() +*/ + +/*! + \fn void QKeyEvent::ignore() + + Clears the accept flag parameter of the key event object. + + Clearing the accept parameter indicates that the event receiver + does not want the key event. Unwanted key events are sent to the + parent widget. + + The accept flag is set by default. + + \sa accept() +*/ + +/*! + \enum Qt::Key + + The key names used by Qt. + + \value Key_Escape + \value Key_Tab + \value Key_Backtab + \value Key_Backspace + \value Key_Return + \value Key_Enter + \value Key_Insert + \value Key_Delete + \value Key_Pause + \value Key_Print + \value Key_SysReq + \value Key_Home + \value Key_End + \value Key_Left + \value Key_Up + \value Key_Right + \value Key_Down + \value Key_Prior + \value Key_Next + \value Key_Shift + \value Key_Control + \value Key_Meta + \value Key_Alt + \value Key_CapsLock + \value Key_NumLock + \value Key_ScrollLock + \value Key_Clear + \value Key_F1 + \value Key_F2 + \value Key_F3 + \value Key_F4 + \value Key_F5 + \value Key_F6 + \value Key_F7 + \value Key_F8 + \value Key_F9 + \value Key_F10 + \value Key_F11 + \value Key_F12 + \value Key_F13 + \value Key_F14 + \value Key_F15 + \value Key_F16 + \value Key_F17 + \value Key_F18 + \value Key_F19 + \value Key_F20 + \value Key_F21 + \value Key_F22 + \value Key_F23 + \value Key_F24 + \value Key_F25 + \value Key_F26 + \value Key_F27 + \value Key_F28 + \value Key_F29 + \value Key_F30 + \value Key_F31 + \value Key_F32 + \value Key_F33 + \value Key_F34 + \value Key_F35 + \value Key_Super_L + \value Key_Super_R + \value Key_Menu + \value Key_Hyper_L + \value Key_Hyper_R + \value Key_Help + \value Key_Space + \value Key_Any + \value Key_Exclam + \value Key_QuoteDbl + \value Key_NumberSign + \value Key_Dollar + \value Key_Percent + \value Key_Ampersand + \value Key_Apostrophe + \value Key_ParenLeft + \value Key_ParenRight + \value Key_Asterisk + \value Key_Plus + \value Key_Comma + \value Key_Minus + \value Key_Period + \value Key_Slash + \value Key_0 + \value Key_1 + \value Key_2 + \value Key_3 + \value Key_4 + \value Key_5 + \value Key_6 + \value Key_7 + \value Key_8 + \value Key_9 + \value Key_Colon + \value Key_Semicolon + \value Key_Less + \value Key_Equal + \value Key_Greater + \value Key_Question + \value Key_At + \value Key_A + \value Key_B + \value Key_C + \value Key_D + \value Key_E + \value Key_F + \value Key_G + \value Key_H + \value Key_I + \value Key_J + \value Key_K + \value Key_L + \value Key_M + \value Key_N + \value Key_O + \value Key_P + \value Key_Q + \value Key_R + \value Key_S + \value Key_T + \value Key_U + \value Key_V + \value Key_W + \value Key_X + \value Key_Y + \value Key_Z + \value Key_BracketLeft + \value Key_Backslash + \value Key_BracketRight + \value Key_AsciiCircum + \value Key_Underscore + \value Key_QuoteLeft + \value Key_BraceLeft + \value Key_Bar + \value Key_BraceRight + \value Key_AsciiTilde + + \value Key_nobreakspace + \value Key_exclamdown + \value Key_cent + \value Key_sterling + \value Key_currency + \value Key_yen + \value Key_brokenbar + \value Key_section + \value Key_diaeresis + \value Key_copyright + \value Key_ordfeminine + \value Key_guillemotleft + \value Key_notsign + \value Key_hyphen + \value Key_registered + \value Key_macron + \value Key_degree + \value Key_plusminus + \value Key_twosuperior + \value Key_threesuperior + \value Key_acute + \value Key_mu + \value Key_paragraph + \value Key_periodcentered + \value Key_cedilla + \value Key_onesuperior + \value Key_masculine + \value Key_guillemotright + \value Key_onequarter + \value Key_onehalf + \value Key_threequarters + \value Key_questiondown + \value Key_Agrave + \value Key_Aacute + \value Key_Acircumflex + \value Key_Atilde + \value Key_Adiaeresis + \value Key_Aring + \value Key_AE + \value Key_Ccedilla + \value Key_Egrave + \value Key_Eacute + \value Key_Ecircumflex + \value Key_Ediaeresis + \value Key_Igrave + \value Key_Iacute + \value Key_Icircumflex + \value Key_Idiaeresis + \value Key_ETH + \value Key_Ntilde + \value Key_Ograve + \value Key_Oacute + \value Key_Ocircumflex + \value Key_Otilde + \value Key_Odiaeresis + \value Key_multiply + \value Key_Ooblique + \value Key_Ugrave + \value Key_Uacute + \value Key_Ucircumflex + \value Key_Udiaeresis + \value Key_Yacute + \value Key_THORN + \value Key_ssharp + \value Key_agrave + \value Key_aacute + \value Key_acircumflex + \value Key_atilde + \value Key_adiaeresis + \value Key_aring + \value Key_ae + \value Key_ccedilla + \value Key_egrave + \value Key_eacute + \value Key_ecircumflex + \value Key_ediaeresis + \value Key_igrave + \value Key_iacute + \value Key_icircumflex + \value Key_idiaeresis + \value Key_eth + \value Key_ntilde + \value Key_ograve + \value Key_oacute + \value Key_ocircumflex + \value Key_otilde + \value Key_odiaeresis + \value Key_division + \value Key_oslash + \value Key_ugrave + \value Key_uacute + \value Key_ucircumflex + \value Key_udiaeresis + \value Key_yacute + \value Key_thorn + \value Key_ydiaeresis + + Multimedia keys + + \value Key_Back + \value Key_Forward + \value Key_Stop + \value Key_Refresh + + \value Key_VolumeDown + \value Key_VolumeMute + \value Key_VolumeUp + \value Key_BassBoost + \value Key_BassUp + \value Key_BassDown + \value Key_TrebleUp + \value Key_TrebleDown + + \value Key_MediaPlay + \value Key_MediaStop + \value Key_MediaPrev + \value Key_MediaNext + \value Key_MediaRecord + + \value Key_HomePage + \value Key_Favorites + \value Key_Search + \value Key_Standby + \value Key_OpenUrl + + \value Key_LaunchMail + \value Key_LaunchMedia + \value Key_Launch0 + \value Key_Launch1 + \value Key_Launch2 + \value Key_Launch3 + \value Key_Launch4 + \value Key_Launch5 + \value Key_Launch6 + \value Key_Launch7 + \value Key_Launch8 + \value Key_Launch9 + \value Key_LaunchA + \value Key_LaunchB + \value Key_LaunchC + \value Key_LaunchD + \value Key_LaunchE + \value Key_LaunchF + + \value Key_MediaLast + + \value Key_unknown + + \value Key_Direction_L internal use only + \value Key_Direction_R internal use only + +*/ + + +/*! + \class QFocusEvent qevent.h + \brief The QFocusEvent class contains event parameters for widget focus + events. + + \ingroup events + + Focus events are sent to widgets when the keyboard input focus + changes. Focus events occur due to mouse actions, keypresses (e.g. + Tab or Backtab), the window system, popup menus, keyboard + shortcuts or other application specific reasons. The reason for a + particular focus event is returned by reason() in the appropriate + event handler. + + The event handlers QWidget::focusInEvent() and + QWidget::focusOutEvent() receive focus events. + + Use setReason() to set the reason for all focus events, and + resetReason() to set the reason for all focus events to the reason + in force before the last setReason() call. + + \sa QWidget::setFocus(), QWidget::setFocusPolicy() +*/ + +/*! + \fn QFocusEvent::QFocusEvent( Type type ) + + Constructs a focus event object. + + The \a type parameter must be either \c QEvent::FocusIn or \c + QEvent::FocusOut. +*/ + + + +QFocusEvent::Reason QFocusEvent::m_reason = QFocusEvent::Other; +QFocusEvent::Reason QFocusEvent::prev_reason = QFocusEvent::Other; + + +/*! + \enum QFocusEvent::Reason + + This enum specifies why the focus changed. + + \value Mouse because of a mouse action. + \value Tab because of a Tab press. + \value Backtab because of a Backtab press + (possibly including Shift/Control, e.g. Shift+Tab). + \value ActiveWindow because the window system made this window (in)active. + \value Popup because the application opened/closed a popup that grabbed/released focus. + \value Shortcut because of a keyboard shortcut. + \value Other any other reason, usually application-specific. + + See the \link focus.html keyboard focus overview\endlink for more + about focus. +*/ + +/*! + Returns the reason for this focus event. + + \sa setReason() + */ +QFocusEvent::Reason QFocusEvent::reason() +{ + return m_reason; +} + +/*! + Sets the reason for all future focus events to \a reason. + + \sa reason(), resetReason() + */ +void QFocusEvent::setReason( Reason reason ) +{ + prev_reason = m_reason; + m_reason = reason; +} + +/*! + Resets the reason for all future focus events to the value before + the last setReason() call. + + \sa reason(), setReason() + */ +void QFocusEvent::resetReason() +{ + m_reason = prev_reason; +} + +/*! + \fn bool QFocusEvent::gotFocus() const + + Returns TRUE if the widget received the text input focus; + otherwise returns FALSE. +*/ + +/*! + \fn bool QFocusEvent::lostFocus() const + + Returns TRUE if the widget lost the text input focus; otherwise + returns FALSE. +*/ + + +/*! + \class QPaintEvent qevent.h + \brief The QPaintEvent class contains event parameters for paint events. + + \ingroup events + + Paint events are sent to widgets that need to update themselves, + for instance when part of a widget is exposed because a covering + widget is moved. + + The event contains a region() that needs to be updated, and a + rect() that is the bounding rectangle of that region. Both are + provided because many widgets can't make much use of region(), and + rect() can be much faster than region().boundingRect(). Painting + is clipped to region() during processing of a paint event. + + The erased() function returns TRUE if the region() has been + cleared to the widget's background (see + QWidget::backgroundMode()), and FALSE if the region's contents are + arbitrary. + + \sa QPainter QWidget::update() QWidget::repaint() + QWidget::paintEvent() QWidget::backgroundMode() QRegion +*/ + +/*! + \fn QPaintEvent::QPaintEvent( const QRegion &paintRegion, bool erased=TRUE ) + + Constructs a paint event object with the region that should be + updated. The region is given by \a paintRegion. If \a erased is + TRUE the region will be cleared before repainting. +*/ + +/*! + \fn QPaintEvent::QPaintEvent( const QRect &paintRect, bool erased=TRUE ) + + Constructs a paint event object with the rectangle that should be + updated. The region is also given by \a paintRect. If \a erased is + TRUE the region will be cleared before repainting. +*/ + +/*! + \fn QPaintEvent::QPaintEvent( const QRegion &paintRegion, const QRect &paintRect, bool erased=TRUE ) + + Constructs a paint event object with the rectangle \a paintRect + that should be updated. The region is given by \a paintRegion. If + \a erased is TRUE the region will be cleared before repainting. +*/ + +/*! + \fn const QRect &QPaintEvent::rect() const + + Returns the rectangle that should be updated. + + \sa region(), QPainter::setClipRect() +*/ + +/*! + \fn const QRegion &QPaintEvent::region() const + + Returns the region that should be updated. + + \sa rect(), QPainter::setClipRegion() +*/ + +/*! + \fn bool QPaintEvent::erased() const + + Returns TRUE if the paint event region (or rectangle) has been + erased with the widget's background; otherwise returns FALSE. +*/ + +/*! + \class QMoveEvent qevent.h + \brief The QMoveEvent class contains event parameters for move events. + + \ingroup events + + Move events are sent to widgets that have been moved to a new position + relative to their parent. + + The event handler QWidget::moveEvent() receives move events. + + \sa QWidget::move(), QWidget::setGeometry() +*/ + +/*! + \fn QMoveEvent::QMoveEvent( const QPoint &pos, const QPoint &oldPos ) + + Constructs a move event with the new and old widget positions, \a + pos and \a oldPos respectively. +*/ + +/*! + \fn const QPoint &QMoveEvent::pos() const + + Returns the new position of the widget. This excludes the window + frame for top level widgets. +*/ + +/*! + \fn const QPoint &QMoveEvent::oldPos() const + + Returns the old position of the widget. +*/ + + +/*! + \class QResizeEvent qevent.h + \brief The QResizeEvent class contains event parameters for resize events. + + \ingroup events + + Resize events are sent to widgets that have been resized. + + The event handler QWidget::resizeEvent() receives resize events. + + \sa QWidget::resize(), QWidget::setGeometry() +*/ + +/*! + \fn QResizeEvent::QResizeEvent( const QSize &size, const QSize &oldSize ) + + Constructs a resize event with the new and old widget sizes, \a + size and \a oldSize respectively. +*/ + +/*! + \fn const QSize &QResizeEvent::size() const + + Returns the new size of the widget, which is the same as + QWidget::size(). +*/ + +/*! + \fn const QSize &QResizeEvent::oldSize() const + + Returns the old size of the widget. +*/ + + +/*! + \class QCloseEvent qevent.h + \brief The QCloseEvent class contains parameters that describe a close event. + + \ingroup events + + Close events are sent to widgets that the user wants to close, + usually by choosing "Close" from the window menu, or by clicking + the `X' titlebar button. They are also sent when you call + QWidget::close() to close a widget programmatically. + + Close events contain a flag that indicates whether the receiver + wants the widget to be closed or not. When a widget accepts the + close event, it is hidden (and destroyed if it was created with + the \c WDestructiveClose flag). If it refuses to accept the close + event nothing happens. (Under X11 it is possible that the window + manager will forcibly close the window; but at the time of writing + we are not aware of any window manager that does this.) + + The application's main widget -- QApplication::mainWidget() -- + is a special case. When it accepts the close event, Qt leaves the + main event loop and the application is immediately terminated + (i.e. it returns from the call to QApplication::exec() in the + main() function). + + The event handler QWidget::closeEvent() receives close events. The + default implementation of this event handler accepts the close + event. If you do not want your widget to be hidden, or want some + special handing, you should reimplement the event handler. + + The \link simple-application.html#closeEvent closeEvent() in the + Application Walkthrough\endlink shows a close event handler that + asks whether to save a document before closing. + + If you want the widget to be deleted when it is closed, create it + with the \c WDestructiveClose widget flag. This is very useful for + independent top-level windows in a multi-window application. + + \l{QObject}s emits the \link QObject::destroyed() + destroyed()\endlink signal when they are deleted. + + If the last top-level window is closed, the + QApplication::lastWindowClosed() signal is emitted. + + The isAccepted() function returns TRUE if the event's receiver has + agreed to close the widget; call accept() to agree to close the + widget and call ignore() if the receiver of this event does not + want the widget to be closed. + + \sa QWidget::close(), QWidget::hide(), QObject::destroyed(), + QApplication::setMainWidget(), QApplication::lastWindowClosed(), + QApplication::exec(), QApplication::quit() +*/ + +/*! + \fn QCloseEvent::QCloseEvent() + + Constructs a close event object with the accept parameter flag set + to FALSE. + + \sa accept() +*/ + +/*! + \fn bool QCloseEvent::isAccepted() const + + Returns TRUE if the receiver of the event has agreed to close the + widget; otherwise returns FALSE. + + \sa accept(), ignore() +*/ + +/*! + \fn void QCloseEvent::accept() + + Sets the accept flag of the close event object. + + Setting the accept flag indicates that the receiver of this event + agrees to close the widget. + + The accept flag is \e not set by default. + + If you choose to accept in QWidget::closeEvent(), the widget will + be hidden. If the widget's \c WDestructiveClose flag is set, it + will also be destroyed. + + \sa ignore(), QWidget::hide() +*/ + +/*! + \fn void QCloseEvent::ignore() + + Clears the accept flag of the close event object. + + Clearing the accept flag indicates that the receiver of this event + does not want the widget to be closed. + + The close event is constructed with the accept flag cleared. + + \sa accept() +*/ + +/*! + \class QIconDragEvent qevent.h + \brief The QIconDragEvent class signals that a main icon drag has begun. + + \ingroup events + + Icon drag events are sent to widgets when the main icon of a window has been dragged away. + On Mac OS X this is fired when the proxy icon of a window is dragged off titlebar, in response to + this event is is normal to begin using drag and drop. +*/ + +/*! + \fn QIconDragEvent::QIconDragEvent() + + Constructs an icon drag event object with the accept parameter + flag set to FALSE. + + \sa accept() +*/ + +/*! + \fn bool QIconDragEvent::isAccepted() const + + Returns TRUE if the receiver of the event has started a drag and + drop operation; otherwise returns FALSE. + + \sa accept(), ignore() +*/ + +/*! + \fn void QIconDragEvent::accept() + + Sets the accept flag of the icon drag event object. + + Setting the accept flag indicates that the receiver of this event + has started a drag and drop oeration. + + The accept flag is \e not set by default. + + \sa ignore(), QWidget::hide() +*/ + +/*! + \fn void QIconDragEvent::ignore() + + Clears the accept flag of the icon drag object. + + Clearing the accept flag indicates that the receiver of this event + has not handled the icon drag as a result other events can be sent. + + The icon drag event is constructed with the accept flag cleared. + + \sa accept() +*/ + +/*! + \class QContextMenuEvent qevent.h + \brief The QContextMenuEvent class contains parameters that describe a context menu event. + + \ingroup events + + Context menu events are sent to widgets when a user triggers a + context menu. What triggers this is platform dependent. For + example, on Windows, pressing the menu button or releasing the + right mouse button will cause this event to be sent. + + When this event occurs it is customary to show a QPopupMenu with a + context menu, if this is relevant to the context. + + Context menu events contain a special accept flag that indicates + whether the receiver accepted the event. If the event handler does + not accept the event, then whatever triggered the event will be + handled as a regular input event if possible. + + \sa QPopupMenu +*/ + +/*! + \fn QContextMenuEvent::QContextMenuEvent( Reason reason, const QPoint &pos, const QPoint &globalPos, int state ) + + Constructs a context menu event object with the accept parameter + flag set to FALSE. + + The \a reason parameter must be \c QContextMenuEvent::Mouse or \c + QContextMenuEvent::Keyboard. + + The \a pos parameter specifies the mouse position relative to the + receiving widget. \a globalPos is the mouse position in absolute + coordinates. \a state is the ButtonState at the time of the event. +*/ + + +/*! + \fn QContextMenuEvent::QContextMenuEvent( Reason reason, const QPoint &pos, int state ) + + Constructs a context menu event object with the accept parameter + flag set to FALSE. + + The \a reason parameter must be \c QContextMenuEvent::Mouse or \c + QContextMenuEvent::Keyboard. + + The \a pos parameter specifies the mouse position relative to the + receiving widget. \a state is the ButtonState at the time of the + event. + + The globalPos() is initialized to QCursor::pos(), which may not be + appropriate. Use the other constructor to specify the global + position explicitly. +*/ + +QContextMenuEvent::QContextMenuEvent( Reason reason, const QPoint &pos, int state ) + : QEvent( ContextMenu ), p( pos ), accpt(TRUE), consum(TRUE), + reas( reason ), s((ushort)state) +{ + gp = QCursor::pos(); +} + +/*! + \fn const QPoint &QContextMenuEvent::pos() const + + Returns the position of the mouse pointer relative to the widget + that received the event. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn int QContextMenuEvent::x() const + + Returns the x-position of the mouse pointer, relative to the + widget that received the event. + + \sa y(), pos() +*/ + +/*! + \fn int QContextMenuEvent::y() const + + Returns the y-position of the mouse pointer, relative to the + widget that received the event. + + \sa x(), pos() +*/ + +/*! + \fn const QPoint &QContextMenuEvent::globalPos() const + + Returns the global position of the mouse pointer at the time of + the event. + + \sa x(), y(), pos() +*/ + +/*! + \fn int QContextMenuEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int QContextMenuEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + +/*! + \fn ButtonState QContextMenuEvent::state() const + + Returns the button state (a combination of mouse buttons and + keyboard modifiers), i.e. what buttons and keys were being + pressed immediately before the event was generated. + + The returned value is \c LeftButton, \c RightButton, \c MidButton, + \c ShiftButton, \c ControlButton and \c AltButton OR'ed together. +*/ + +/*! + \fn bool QContextMenuEvent::isConsumed() const + + Returns TRUE (which stops propagation of the event) if the + receiver has blocked the event; otherwise returns FALSE. + + \sa accept(), ignore(), consume() +*/ + +/*! + \fn void QContextMenuEvent::consume() + + Sets the consume flag of the context event object. + + Setting the consume flag indicates that the receiver of this event + does not want the event to be propagated further (i.e. not sent to + parent classes.) + + The consumed flag is not set by default. + + \sa ignore() accept() +*/ + +/*! + \fn bool QContextMenuEvent::isAccepted() const + + Returns TRUE if the receiver has processed the event; otherwise + returns FALSE. + + \sa accept(), ignore(), consume() +*/ + +/*! + \fn void QContextMenuEvent::accept() + + Sets the accept flag of the context event object. + + Setting the accept flag indicates that the receiver of this event + has processed the event. Processing the event means you did + something with it and it will be implicitly consumed. + + The accept flag is not set by default. + + \sa ignore() consume() +*/ + +/*! + \fn void QContextMenuEvent::ignore() + + Clears the accept flag of the context event object. + + Clearing the accept flag indicates that the receiver of this event + does not need to show a context menu. This will implicitly remove + the consumed flag as well. + + The accept flag is not set by default. + + \sa accept() consume() +*/ + +/*! + \enum QContextMenuEvent::Reason + + This enum describes the reason the ContextMenuEvent was sent. The + values are: + + \value Mouse The mouse caused the event to be sent. Normally this + means the right mouse button was clicked, but this is platform + specific. + + \value Keyboard The keyboard caused this event to be sent. On + Windows this means the menu button was pressed. + + \value Other The event was sent by some other means (i.e. not by + the mouse or keyboard). +*/ + + +/*! + \fn QContextMenuEvent::Reason QContextMenuEvent::reason() const + + Returns the reason for this context event. +*/ + + +/*! + \class QIMEvent qevent.h + \brief The QIMEvent class provides parameters for input method events. + + \ingroup events + + Input method events are sent to widgets when an input method is + used to enter text into a widget. Input methods are widely used to + enter text in Asian and other complex languages. + + The events are of interest to widgets that accept keyboard input + and want to be able to correctly handle complex languages. Text + input in such languages is usually a three step process. + + \list 1 + \i <b>Starting to Compose</b><br> + When the user presses the first key on a keyboard an input context + is created. This input context will contain a string with the + typed characters. + + \i <b>Composing</b><br> + With every new key pressed, the input method will try to create a + matching string for the text typed so far. While the input context + is active, the user can only move the cursor inside the string + belonging to this input context. + + \i <b>Completing</b><br> + At some point, e.g. when the user presses the Spacebar, they get + to this stage, where they can choose from a number of strings that + match the text they have typed so far. The user can press Enter to + confirm their choice or Escape to cancel the input; in either case + the input context will be closed. + \endlist + + Note that the particular key presses used for a given input + context may differ from those we've mentioned here, i.e. they may + not be Spacebar, Enter and Escape. + + These three stages are represented by three different types of + events. The IMStartEvent, IMComposeEvent and IMEndEvent. When a + new input context is created, an IMStartEvent will be sent to the + widget and delivered to the \l QWidget::imStartEvent() function. + The widget can then update internal data structures to reflect + this. + + After this, an IMComposeEvent will be sent to the widget for + every key the user presses. It will contain the current + composition string the widget has to show and the current cursor + position within the composition string. This string is temporary + and can change with every key the user types, so the widget will + need to store the state before the composition started (the state + it had when it received the IMStartEvent). IMComposeEvents will be + delivered to the \l QWidget::imComposeEvent() function. + + Usually, widgets try to mark the part of the text that is part of + the current composition in a way that is visible to the user. A + commonly used visual cue is to use a dotted underline. + + After the user has selected the final string, an IMEndEvent will + be sent to the widget. The event contains the final string the + user selected, and could be empty if they canceled the + composition. This string should be accepted as the final text the + user entered, and the intermediate composition string should be + cleared. These events are delivered to \l QWidget::imEndEvent(). + + If the user clicks another widget, taking the focus out of the + widget where the composition is taking place the IMEndEvent will + be sent and the string it holds will be the result of the + composition up to that point (which may be an empty string). +*/ + +/*! + \fn QIMEvent::QIMEvent( Type type, const QString &text, int cursorPosition ) + + Constructs a new QIMEvent with the accept flag set to FALSE. \a + type can be one of QEvent::IMStartEvent, QEvent::IMComposeEvent + or QEvent::IMEndEvent. \a text contains the current compostion + string and \a cursorPosition the current position of the cursor + inside \a text. +*/ + +/*! + \fn const QString &QIMEvent::text() const + + Returns the composition text. This is a null string for an + IMStartEvent, and contains the final accepted string (which may be + empty) in the IMEndEvent. +*/ + +/*! + \fn int QIMEvent::cursorPos() const + + Returns the current cursor position inside the composition string. + Will return -1 for IMStartEvent and IMEndEvent. +*/ + +/*! + \fn int QIMEvent::selectionLength() const + + Returns the number of characters in the composition string ( + starting at cursorPos() ) that should be marked as selected by the + input widget receiving the event. + Will return 0 for IMStartEvent and IMEndEvent. +*/ + +/*! + \fn bool QIMEvent::isAccepted() const + + Returns TRUE if the receiver of the event processed the event; + otherwise returns FALSE. +*/ + +/*! + \fn void QIMEvent::accept() + + Sets the accept flag of the input method event object. + + Setting the accept parameter indicates that the receiver of the + event processed the input method event. + + The accept flag is not set by default. + + \sa ignore() +*/ + + +/*! + \fn void QIMEvent::ignore() + + Clears the accept flag parameter of the input method event object. + + Clearing the accept parameter indicates that the event receiver + does not want the input method event. + + The accept flag is cleared by default. + + \sa accept() +*/ + +/*! + \class QTabletEvent qevent.h + \brief The QTabletEvent class contains parameters that describe a Tablet + event. + + \ingroup events + + Tablet Events are generated from a Wacom© tablet. Most of + the time you will want to deal with events from the tablet as if + they were events from a mouse, for example retrieving the position + with x(), y(), pos(), globalX(), globalY() and globalPos(). In + some situations you may wish to retrieve the extra information + provided by the tablet device driver, for example, you might want + to adjust color brightness based on pressure. QTabletEvent allows + you to get the pressure(), the xTilt() and yTilt(), as well as the + type of device being used with device() (see \l{TabletDevice}). + + A tablet event contains a special accept flag that indicates + whether the receiver wants the event. You should call + QTabletEvent::accept() if you handle the tablet event; otherwise + it will be sent to the parent widget. + + The QWidget::setEnabled() function can be used to enable or + disable mouse and keyboard events for a widget. + + The event handler QWidget::tabletEvent() receives all three types of tablet + events. Qt will first send a tabletEvent and then, if it is not accepted, + it will send a mouse event. This allows applications that don't utilize + tablets to use a tablet like a mouse while also enabling those who want to + use both tablets and mouses differently. + +*/ + +/*! + \enum QTabletEvent::TabletDevice + + This enum defines what type of device is generating the event. + + \value NoDevice No device, or an unknown device. + \value Puck A Puck (a device that is similar to a flat mouse with + a transparent circle with cross-hairs). + \value Stylus A Stylus (the narrow end of the pen). + \value Eraser An Eraser (the broad end of the pen). + \omit + \value Menu A menu button was pressed (currently unimplemented). +*/ + +/*! + \fn QTabletEvent::QTabletEvent( Type t, const QPoint &pos, + const QPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, + const QPair<int,int> &uId ) + Construct a tablet event of type \a t. The position of when the event occurred is given + int \a pos and \a globalPos. \a device contains the \link TabletDevice device type\endlink, + \a pressure contains the pressure exerted on the \a device, \a xTilt and \a yTilt contain + \a device's degree of tilt from the X and Y axis respectively. The \a uId contains an + event id. + + On Irix, \a globalPos will contain the high-resolution coordinates received from the + tablet device driver, instead of from the windowing system. + + \sa pos(), globalPos(), device(), pressure(), xTilt(), yTilt() +*/ + +QTabletEvent::QTabletEvent( Type t, const QPoint &pos, const QPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, + const QPair<int, int> &uId ) + : QEvent( t ), + mPos( pos ), + mGPos( globalPos ), + mDev( device ), + mPress( pressure ), + mXT( xTilt ), + mYT( yTilt ), + mType( uId.first ), + mPhy( uId.second ), + mbAcc(TRUE) +{} + +/*! + \obsolete + \fn QTabletEvent::QTabletEvent( const QPoint &pos, const QPoint &globalPos, int device, int pressure, int xTilt, int yTilt, const QPair<int,int> &uId ) + + Constructs a tablet event object. The position when the event + occurred is is given in \a pos and \a globalPos. \a device + contains the \link TabletDevice device type\endlink, \a pressure + contains the pressure exerted on the \a device, \a xTilt and \a + yTilt contain the \a device's degrees of tilt from the X and Y + axis respectively. The \a uId contains an event id. + + On Irix, \a globalPos will contain the high-resolution coordinates + received from the tablet device driver, instead of from the + windowing system. + + \sa pos(), globalPos(), device(), pressure(), xTilt(), yTilt() +*/ + +/*! + \fn TabletDevices QTabletEvent::device() const + + Returns the type of device that generated the event. Useful if you + want one end of the pen to do something different than the other. + + \sa TabletDevice +*/ + +/*! + \fn int QTabletEvent::pressure() const + + Returns the pressure that is exerted on the device. This number is + a value from 0 (no pressure) to 255 (maximum pressure). The + pressure is always scaled to be within this range no matter how + many pressure levels the underlying hardware supports. +*/ + +/*! + \fn int QTabletEvent::xTilt() const + + Returns the difference from the perpendicular in the X Axis. + Positive values are towards the tablet's physical right. The angle + is in the range -60 to +60 degrees. + + \sa yTilt() +*/ + +/*! + \fn int QTabletEvent::yTilt() const + + Returns the difference from the perpendicular in the Y Axis. + Positive values are towards the bottom of the tablet. The angle is + within the range -60 to +60 degrees. + + \sa xTilt() +*/ + +/*! + \fn const QPoint &QTabletEvent::pos() const + + Returns the position of the device, relative to the widget that + received the event. + + If you move widgets around in response to mouse events, use + globalPos() instead of this function. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn int QTabletEvent::x() const + + Returns the x-position of the device, relative to the widget that + received the event. + + \sa y(), pos() +*/ + +/*! + \fn int QTabletEvent::y() const + + Returns the y-position of the device, relative to the widget that + received the event. + + \sa x(), pos() +*/ + +/*! + \fn const QPoint &QTabletEvent::globalPos() const + + Returns the global position of the device \e{at the time of the + event}. This is important on asynchronous windows systems like X11; + whenever you move your widgets around in response to mouse events, + globalPos() can differ significantly from the current position + QCursor::pos(). + + \sa globalX(), globalY() +*/ + +/*! + \fn int QTabletEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int QTabletEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + +/*! + \fn bool QTabletEvent::isAccepted() const + + Returns TRUE if the receiver of the event handles the tablet + event; otherwise returns FALSE. +*/ + +/*! + \fn void QTabletEvent::accept() + + Sets the accept flag of the tablet event object. + + Setting the accept flag indicates that the receiver of the event + wants the tablet event. Unwanted tablet events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + +/*! + \fn void QTabletEvent::ignore() + + Clears the accept flag parameter of the tablet event object. + + Clearing the accept flag indicates that the event receiver does + not want the tablet event. Unwanted tablet events are sent to the + parent widget. + + The accept flag is set by default. + + \sa accept() +*/ + +/*! + \fn QPair<int, int> QTabletEvent::uniqueId() + + Returns a unique ID for the current device. It is possible to + generate a unique ID for any Wacom© device. This makes it + possible to differentiate between multiple devices being used at + the same time on the tablet. The \c first member contains a value + for the type, the \c second member contains a physical ID obtained + from the device. Each combination of these values is unique. Note: + for different platforms, the \c first value is different due to + different driver implementations. +*/ + +/*! + \class QChildEvent qevent.h + \brief The QChildEvent class contains event parameters for child object + events. + + \ingroup events + + Child events are sent to objects when children are inserted or + removed. + + A \c ChildRemoved event is sent immediately, but a \c + ChildInserted event is \e posted (with QApplication::postEvent()). + + Note that if a child is removed immediately after it is inserted, + the \c ChildInserted event may be suppressed, but the \c + ChildRemoved event will always be sent. In this case there will be + a \c ChildRemoved event without a corresponding \c ChildInserted + event. + + The handler for these events is QObject::childEvent(). +*/ + +/*! + \fn QChildEvent::QChildEvent( Type type, QObject *child ) + + Constructs a child event object. The \a child is the object that + is to be removed or inserted. + + The \a type parameter must be either \c QEvent::ChildInserted or + \c QEvent::ChildRemoved. +*/ + +/*! + \fn QObject *QChildEvent::child() const + + Returns the child widget that was inserted or removed. +*/ + +/*! + \fn bool QChildEvent::inserted() const + + Returns TRUE if the widget received a new child; otherwise returns + FALSE. +*/ + +/*! + \fn bool QChildEvent::removed() const + + Returns TRUE if the object lost a child; otherwise returns FALSE. +*/ + + + + +/*! + \class QCustomEvent qevent.h + \brief The QCustomEvent class provides support for custom events. + + \ingroup events + + QCustomEvent is a generic event class for user-defined events. + User defined events can be sent to widgets or other QObject + instances using QApplication::postEvent() or + QApplication::sendEvent(). Subclasses of QObject can easily + receive custom events by implementing the QObject::customEvent() + event handler function. + + QCustomEvent objects should be created with a type ID that + uniquely identifies the event type. To avoid clashes with the + Qt-defined events types, the value should be at least as large as + the value of the "User" entry in the QEvent::Type enum. + + QCustomEvent contains a generic void* data member that may be used + for transferring event-specific data to the receiver. Note that + since events are normally delivered asynchronously, the data + pointer, if used, must remain valid until the event has been + received and processed. + + QCustomEvent can be used as-is for simple user-defined event + types, but normally you will want to make a subclass of it for + your event types. In a subclass, you can add data members that are + suitable for your event type. + + Example: + \code + class ColorChangeEvent : public QCustomEvent + { + public: + ColorChangeEvent( QColor color ) + : QCustomEvent( 65432 ), c( color ) {} + QColor color() const { return c; } + private: + QColor c; + }; + + // To send an event of this custom event type: + + ColorChangeEvent* ce = new ColorChangeEvent( blue ); + QApplication::postEvent( receiver, ce ); // Qt will delete it when done + + // To receive an event of this custom event type: + + void MyWidget::customEvent( QCustomEvent * e ) + { + if ( e->type() == 65432 ) { // It must be a ColorChangeEvent + ColorChangeEvent* ce = (ColorChangeEvent*)e; + newColor = ce->color(); + } + } + \endcode + + \sa QWidget::customEvent(), QApplication::notify() +*/ + + +/*! + Constructs a custom event object with event type \a type. The + value of \a type must be at least as large as QEvent::User. The + data pointer is set to 0. +*/ + +QCustomEvent::QCustomEvent( int type ) + : QEvent( (QEvent::Type)type ), d( 0 ) +{ +} + + +/*! + \fn QCustomEvent::QCustomEvent( Type type, void *data ) + + Constructs a custom event object with the event type \a type and a + pointer to \a data. (Note that any int value may safely be cast to + QEvent::Type). +*/ + + +/*! + \fn void QCustomEvent::setData( void* data ) + + Sets the generic data pointer to \a data. + + \sa data() +*/ + +/*! + \fn void *QCustomEvent::data() const + + Returns a pointer to the generic event data. + + \sa setData() +*/ + + + +/*! + \fn QDragMoveEvent::QDragMoveEvent( const QPoint& pos, Type type ) + + Creates a QDragMoveEvent for which the mouse is at point \a pos, + and the event is of type \a type. + + \warning Do not create a QDragMoveEvent yourself since these + objects rely on Qt's internal state. +*/ + +/*! + \fn void QDragMoveEvent::accept( const QRect & r ) + + The same as accept(), but also notifies that future moves will + also be acceptable if they remain within the rectangle \a r on the + widget: this can improve performance, but may also be ignored by + the underlying system. + + If the rectangle is \link QRect::isEmpty() empty\endlink, then + drag move events will be sent continuously. This is useful if the + source is scrolling in a timer event. +*/ + +/*! + \fn void QDragMoveEvent::ignore( const QRect & r) + + The opposite of accept(const QRect&), i.e. says that moves within + rectangle \a r are not acceptable (will be ignored). +*/ + +/*! + \fn QRect QDragMoveEvent::answerRect() const + + Returns the rectangle for which the acceptance of the move event + applies. +*/ + + + +/*! + \fn const QPoint& QDropEvent::pos() const + + Returns the position where the drop was made. +*/ + +/*! + \fn bool QDropEvent::isAccepted () const + + Returns TRUE if the drop target accepts the event; otherwise + returns FALSE. +*/ + +/*! + \fn void QDropEvent::accept(bool y=TRUE) + + Call this function to indicate whether the event provided data + which your widget processed. Set \a y to TRUE (the default) if + your widget could process the data, otherwise set \a y to FALSE. + To get the data, use encodedData(), or preferably, the decode() + methods of existing QDragObject subclasses, such as + QTextDrag::decode(), or your own subclasses. + + \sa acceptAction() +*/ + +/*! + \fn void QDropEvent::acceptAction(bool y=TRUE) + + Call this to indicate that the action described by action() is + accepted (i.e. if \a y is TRUE, which is the default), not merely + the default copy action. If you call acceptAction(TRUE), there is + no need to also call accept(TRUE). +*/ + +/*! + \fn void QDragMoveEvent::accept( bool y ) + \reimp + \internal + Remove in 3.0 +*/ + +/*! + \fn void QDragMoveEvent::ignore() + \reimp + \internal + Remove in 3.0 +*/ + + +/*! + \enum QDropEvent::Action + + This enum describes the action which a source requests that a + target perform with dropped data. + + \value Copy The default action. The source simply uses the data + provided in the operation. + \value Link The source should somehow create a link to the + location specified by the data. + \value Move The source should somehow move the object from the + location specified by the data to a new location. + \value Private The target has special knowledge of the MIME type, + which the source should respond to in a similar way to + a Copy. + \value UserAction The source and target can co-operate using + special actions. This feature is not currently + supported. + + The Link and Move actions only makes sense if the data is a + reference, for example, text/uri-list file lists (see QUriDrag). +*/ + +/*! + \fn void QDropEvent::setAction( Action a ) + + Sets the action to \a a. This is used internally, you should not + need to call this in your code: the \e source decides the action, + not the target. +*/ + +/*! + \fn Action QDropEvent::action() const + + Returns the Action which the target is requesting to be performed + with the data. If your application understands the action and can + process the supplied data, call acceptAction(); if your + application can process the supplied data but can only perform the + Copy action, call accept(). +*/ + +/*! + \fn void QDropEvent::ignore() + + The opposite of accept(), i.e. you have ignored the drop event. +*/ + +/*! + \fn bool QDropEvent::isActionAccepted () const + + Returns TRUE if the drop action was accepted by the drop site; + otherwise returns FALSE. +*/ + + +/*! + \fn void QDropEvent::setPoint (const QPoint & np) + + Sets the drop to happen at point \a np. You do not normally need + to use this as it will be set internally before your widget + receives the drop event. +*/ // ### here too - what coordinate system? + + +/*! + \class QDragEnterEvent qevent.h + \brief The QDragEnterEvent class provides an event which is sent to the widget when a drag and drop first drags onto the widget. + + \ingroup events + \ingroup draganddrop + + This event is always immediately followed by a QDragMoveEvent, so + you only need to respond to one or the other event. This class + inherits most of its functionality from QDragMoveEvent, which in + turn inherits most of its functionality from QDropEvent. + + \sa QDragLeaveEvent, QDragMoveEvent, QDropEvent +*/ + +/*! + \fn QDragEnterEvent::QDragEnterEvent (const QPoint & pos) + + Constructs a QDragEnterEvent entering at the given point, \a pos. + + \warning Do not create a QDragEnterEvent yourself since these + objects rely on Qt's internal state. +*/ + +/*! + \class QDragLeaveEvent qevent.h + \brief The QDragLeaveEvent class provides an event which is sent to the widget when a drag and drop leaves the widget. + + \ingroup events + \ingroup draganddrop + + This event is always preceded by a QDragEnterEvent and a series of + \l{QDragMoveEvent}s. It is not sent if a QDropEvent is sent + instead. + + \sa QDragEnterEvent, QDragMoveEvent, QDropEvent +*/ + +/*! + \fn QDragLeaveEvent::QDragLeaveEvent() + + Constructs a QDragLeaveEvent. + + \warning Do not create a QDragLeaveEvent yourself since these + objects rely on Qt's internal state. +*/ + +/*! + \class QHideEvent qevent.h + \brief The QHideEvent class provides an event which is sent after a widget is hidden. + + \ingroup events + + This event is sent just before QWidget::hide() returns, and also + when a top-level window has been hidden (iconified) by the user. + + If spontaneous() is TRUE the event originated outside the + application, i.e. the user hid the window using the window manager + controls, either by iconifying the window or by switching to + another virtual desktop where the window isn't visible. The window + will become hidden but not withdrawn. If the window was iconified, + QWidget::isMinimized() returns TRUE. + + \sa QShowEvent +*/ + +/*! + \fn QHideEvent::QHideEvent() + + Constructs a QHideEvent. +*/ + +/*! + \class QShowEvent qevent.h + \brief The QShowEvent class provides an event which is sent when a widget is shown. + + \ingroup events + + There are two kinds of show events: show events caused by the + window system (spontaneous) and internal show events. Spontaneous + show events are sent just after the window system shows the + window, including after a top-level window has been shown + (un-iconified) by the user. Internal show events are delivered + just before the widget becomes visible. + + \sa QHideEvent +*/ + +/*! + \fn QShowEvent::QShowEvent() + + Constructs a QShowEvent. +*/ + + +/*! + \fn QByteArray QDropEvent::data(const char* f) const + + \obsolete + + Use QDropEvent::encodedData(). +*/ + + +/*! + Destroys the event. If it was \link + QApplication::postEvent() posted \endlink, + it will be removed from the list of events to be posted. +*/ + +QEvent::~QEvent() +{ + if ( posted && qApp ) + QApplication::removePostedEvent( this ); +} diff --git a/src/kernel/qevent.h b/src/kernel/qevent.h new file mode 100644 index 0000000..6512b9a --- /dev/null +++ b/src/kernel/qevent.h @@ -0,0 +1,617 @@ +/**************************************************************************** +** +** Definition of event classes +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QEVENT_H +#define QEVENT_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qregion.h" +#include "qnamespace.h" +#include "qmime.h" +#include "qpair.h" +#endif // QT_H + +class Q_EXPORT QEvent: public Qt // event base class +{ +public: + enum Type { + + /* + If you get a strange compiler error on the line with None, + it's probably because you're also including X11 headers, + which #define the symbol None. Put the X11 includes after + the Qt includes to solve this problem. + */ + + None = 0, // invalid event + + + Timer = 1, // timer event + MouseButtonPress = 2, // mouse button pressed + MouseButtonRelease = 3, // mouse button released + MouseButtonDblClick = 4, // mouse button double click + MouseMove = 5, // mouse move + KeyPress = 6, // key pressed + KeyRelease = 7, // key released + FocusIn = 8, // keyboard focus received + FocusOut = 9, // keyboard focus lost + Enter = 10, // mouse enters widget + Leave = 11, // mouse leaves widget + Paint = 12, // paint widget + Move = 13, // move widget + Resize = 14, // resize widget + Create = 15, // after object creation + Destroy = 16, // during object destruction + Show = 17, // widget is shown + Hide = 18, // widget is hidden + Close = 19, // request to close widget + Quit = 20, // request to quit application + Reparent = 21, // widget has been reparented + ShowMinimized = 22, // widget is shown minimized + ShowNormal = 23, // widget is shown normal + WindowActivate = 24, // window was activated + WindowDeactivate = 25, // window was deactivated + ShowToParent = 26, // widget is shown to parent + HideToParent = 27, // widget is hidden to parent + ShowMaximized = 28, // widget is shown maximized + ShowFullScreen = 29, // widget is shown full-screen + Accel = 30, // accelerator event + Wheel = 31, // wheel event + AccelAvailable = 32, // accelerator available event + CaptionChange = 33, // caption changed + IconChange = 34, // icon changed + ParentFontChange = 35, // parent font changed + ApplicationFontChange = 36, // application font changed + ParentPaletteChange = 37, // parent palette changed + ApplicationPaletteChange = 38, // application palette changed + PaletteChange = 39, // widget palette changed + Clipboard = 40, // internal clipboard event + Speech = 42, // reserved for speech input + SockAct = 50, // socket activation + AccelOverride = 51, // accelerator override event + DeferredDelete = 52, // deferred delete event + DragEnter = 60, // drag moves into widget + DragMove = 61, // drag moves in widget + DragLeave = 62, // drag leaves or is cancelled + Drop = 63, // actual drop + DragResponse = 64, // drag accepted/rejected + ChildInserted = 70, // new child widget + ChildRemoved = 71, // deleted child widget + LayoutHint = 72, // child min/max size changed + ShowWindowRequest = 73, // widget's window should be mapped + WindowBlocked = 74, // window is about to be blocked modally + WindowUnblocked = 75, // windows modal blocking has ended + ActivateControl = 80, // ActiveX activation + DeactivateControl = 81, // ActiveX deactivation + ContextMenu = 82, // context popup menu + IMStart = 83, // input method composition start + IMCompose = 84, // input method composition + IMEnd = 85, // input method composition end + Accessibility = 86, // accessibility information is requested + TabletMove = 87, // Wacom tablet event + LocaleChange = 88, // the system locale changed + LanguageChange = 89, // the application language changed + LayoutDirectionChange = 90, // the layout direction changed + Style = 91, // internal style event + TabletPress = 92, // tablet press + TabletRelease = 93, // tablet release + OkRequest = 94, // CE (Ok) button pressed + HelpRequest = 95, // CE (?) button pressed + WindowStateChange = 96, // window state has changed + IconDrag = 97, // proxy icon dragged + User = 1000, // first user event id + MaxUser = 65535 // last user event id + }; + + + QEvent( Type type ) : t(type), posted(FALSE), spont(FALSE) {} + virtual ~QEvent(); + Type type() const { return t; } + bool spontaneous() const { return spont; } +protected: + Type t; +private: + uint posted : 1; + uint spont : 1; + + + friend class QApplication; + friend class QAccelManager; + friend class QBaseApplication; + friend class QETWidget; +}; + + +class Q_EXPORT QTimerEvent : public QEvent +{ +public: + QTimerEvent( int timerId ) + : QEvent(Timer), id(timerId) {} + int timerId() const { return id; } +protected: + int id; +}; + + +class Q_EXPORT QMouseEvent : public QEvent +{ +public: + QMouseEvent( Type type, const QPoint &pos, int button, int state ); + + QMouseEvent( Type type, const QPoint &pos, const QPoint&globalPos, + int button, int state ) + : QEvent(type), p(pos), g(globalPos), b((ushort)button),s((ushort)state),accpt(TRUE) {}; + + const QPoint &pos() const { return p; } + const QPoint &globalPos() const { return g; } + int x() const { return p.x(); } + int y() const { return p.y(); } + int globalX() const { return g.x(); } + int globalY() const { return g.y(); } + ButtonState button() const { return (ButtonState) b; } + ButtonState state() const { return (ButtonState) s; } + ButtonState stateAfter() const; + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + QPoint p; + QPoint g; + ushort b; + ushort s; + uint accpt:1; +}; + + +#ifndef QT_NO_WHEELEVENT +class Q_EXPORT QWheelEvent : public QEvent +{ +public: + QWheelEvent( const QPoint &pos, int delta, int state, Orientation orient = Vertical ); + QWheelEvent( const QPoint &pos, const QPoint& globalPos, int delta, int state, Orientation orient = Vertical ) + : QEvent(Wheel), p(pos), g(globalPos), d(delta), s((ushort)state), + accpt(TRUE), o(orient) {} + int delta() const { return d; } + const QPoint &pos() const { return p; } + const QPoint &globalPos() const { return g; } + int x() const { return p.x(); } + int y() const { return p.y(); } + int globalX() const { return g.x(); } + int globalY() const { return g.y(); } + ButtonState state() const { return ButtonState(s); } + Orientation orientation() const { return o; } + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + QPoint p; + QPoint g; + int d; + ushort s; + bool accpt; + Orientation o; +}; +#endif + +class Q_EXPORT QTabletEvent : public QEvent +{ +public: + enum TabletDevice { NoDevice = -1, Puck, Stylus, Eraser }; + QTabletEvent( Type t, const QPoint &pos, const QPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, const QPair<int,int> &uId ); + QTabletEvent( const QPoint &pos, const QPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, const QPair<int,int> &uId ) + : QEvent( TabletMove ), mPos( pos ), mGPos( globalPos ), mDev( device ), + mPress( pressure ), mXT( xTilt ), mYT( yTilt ), mType( uId.first ), + mPhy( uId.second ), mbAcc(TRUE) + {} + int pressure() const { return mPress; } + int xTilt() const { return mXT; } + int yTilt() const { return mYT; } + const QPoint &pos() const { return mPos; } + const QPoint &globalPos() const { return mGPos; } + int x() const { return mPos.x(); } + int y() const { return mPos.y(); } + int globalX() const { return mGPos.x(); } + int globalY() const { return mGPos.y(); } + TabletDevice device() const { return TabletDevice(mDev); } + int isAccepted() const { return mbAcc; } + void accept() { mbAcc = TRUE; } + void ignore() { mbAcc = FALSE; } + QPair<int,int> uniqueId() { return QPair<int,int>( mType, mPhy); } +protected: + QPoint mPos; + QPoint mGPos; + int mDev, + mPress, + mXT, + mYT, + mType, + mPhy; + bool mbAcc; + +}; + +class Q_EXPORT QKeyEvent : public QEvent +{ +public: + QKeyEvent( Type type, int key, int ascii, int state, + const QString& text=QString::null, bool autorep=FALSE, ushort count=1 ) + : QEvent(type), txt(text), k((ushort)key), s((ushort)state), + a((uchar)ascii), accpt(TRUE), autor(autorep), c(count) + { + if ( key >= Key_Back && key <= Key_MediaLast ) + accpt = FALSE; + } + int key() const { return k; } + int ascii() const { return a; } + ButtonState state() const { return ButtonState(s); } + ButtonState stateAfter() const; + bool isAccepted() const { return accpt; } + QString text() const { return txt; } + bool isAutoRepeat() const { return autor; } + int count() const { return int(c); } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } + +protected: + QString txt; + ushort k, s; + uchar a; + uint accpt:1; + uint autor:1; + ushort c; +}; + + +class Q_EXPORT QFocusEvent : public QEvent +{ +public: + + QFocusEvent( Type type ) + : QEvent(type) {} + + bool gotFocus() const { return type() == FocusIn; } + bool lostFocus() const { return type() == FocusOut; } + + enum Reason { Mouse, Tab, Backtab, ActiveWindow, Popup, Shortcut, Other }; + static Reason reason(); + static void setReason( Reason reason ); + static void resetReason(); + +private: + static Reason m_reason; + static Reason prev_reason; +}; + + +class Q_EXPORT QPaintEvent : public QEvent +{ +public: + QPaintEvent( const QRegion& paintRegion, bool erased = TRUE) + : QEvent(Paint), + rec(paintRegion.boundingRect()), + reg(paintRegion), + erase(erased){} + QPaintEvent( const QRect &paintRect, bool erased = TRUE ) + : QEvent(Paint), + rec(paintRect), + reg(paintRect), + erase(erased){} + QPaintEvent( const QRegion &paintRegion, const QRect &paintRect, bool erased = TRUE ) + : QEvent(Paint), + rec(paintRect), + reg(paintRegion), + erase(erased){} + + const QRect &rect() const { return rec; } + const QRegion ®ion() const { return reg; } + bool erased() const { return erase; } +protected: + friend class QApplication; + friend class QBaseApplication; + QRect rec; + QRegion reg; + bool erase; +}; + + +class Q_EXPORT QMoveEvent : public QEvent +{ +public: + QMoveEvent( const QPoint &pos, const QPoint &oldPos ) + : QEvent(Move), p(pos), oldp(oldPos) {} + const QPoint &pos() const { return p; } + const QPoint &oldPos()const { return oldp;} +protected: + QPoint p, oldp; + friend class QApplication; + friend class QBaseApplication; +}; + + +class Q_EXPORT QResizeEvent : public QEvent +{ +public: + QResizeEvent( const QSize &size, const QSize &oldSize ) + : QEvent(Resize), s(size), olds(oldSize) {} + const QSize &size() const { return s; } + const QSize &oldSize()const { return olds;} +protected: + QSize s, olds; + friend class QApplication; + friend class QBaseApplication; +}; + + +class Q_EXPORT QCloseEvent : public QEvent +{ +public: + QCloseEvent() + : QEvent(Close), accpt(FALSE) {} + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + bool accpt; +}; + + +class Q_EXPORT QIconDragEvent : public QEvent +{ +public: + QIconDragEvent() + : QEvent(IconDrag), accpt(FALSE) {} + + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + bool accpt; +}; + +class Q_EXPORT QShowEvent : public QEvent +{ +public: + QShowEvent() + : QEvent(Show) {} +}; + + +class Q_EXPORT QHideEvent : public QEvent +{ +public: + QHideEvent() + : QEvent(Hide) {} +}; + +class Q_EXPORT QContextMenuEvent : public QEvent +{ +public: + enum Reason { Mouse, Keyboard, Other }; + QContextMenuEvent( Reason reason, const QPoint &pos, const QPoint &globalPos, int state ) + : QEvent( ContextMenu ), p( pos ), gp( globalPos ), accpt( TRUE ), consum( TRUE ), + reas( reason ), s((ushort)state) {} + QContextMenuEvent( Reason reason, const QPoint &pos, int state ); + + int x() const { return p.x(); } + int y() const { return p.y(); } + int globalX() const { return gp.x(); } + int globalY() const { return gp.y(); } + + const QPoint& pos() const { return p; } + const QPoint& globalPos() const { return gp; } + + ButtonState state() const { return (ButtonState) s; } + bool isAccepted() const { return accpt; } + bool isConsumed() const { return consum; } + void consume() { accpt = FALSE; consum = TRUE; } + void accept() { accpt = TRUE; consum = TRUE; } + void ignore() { accpt = FALSE; consum = FALSE; } + + Reason reason() const { return Reason( reas ); } + +protected: + QPoint p; + QPoint gp; + bool accpt; + bool consum; + uint reas:8; + ushort s; +}; + + +class Q_EXPORT QIMEvent : public QEvent +{ +public: + QIMEvent( Type type, const QString &text, int cursorPosition ) + : QEvent(type), txt(text), cpos(cursorPosition), a(TRUE) {} + const QString &text() const { return txt; } + int cursorPos() const { return cpos; } + bool isAccepted() const { return a; } + void accept() { a = TRUE; } + void ignore() { a = FALSE; } + int selectionLength() const; + +private: + QString txt; + int cpos; + bool a; +}; + +class Q_EXPORT QIMComposeEvent : public QIMEvent +{ +public: + QIMComposeEvent( Type type, const QString &text, int cursorPosition, + int selLength ) + : QIMEvent( type, text, cursorPosition ), selLen( selLength ) { } + +private: + int selLen; + + friend class QIMEvent; +}; + +inline int QIMEvent::selectionLength() const +{ + if ( type() != IMCompose ) return 0; + QIMComposeEvent *that = (QIMComposeEvent *) this; + return that->selLen; +} + + +#ifndef QT_NO_DRAGANDDROP + +// This class is rather closed at the moment. If you need to create your +// own DND event objects, write to qt-bugs@trolltech.com and we'll try to +// find a way to extend it so it covers your needs. + +class Q_EXPORT QDropEvent : public QEvent, public QMimeSource +{ +public: + QDropEvent( const QPoint& pos, Type typ=Drop ) + : QEvent(typ), p(pos), + act(0), accpt(0), accptact(0), resv(0), + d(0) + {} + const QPoint &pos() const { return p; } + bool isAccepted() const { return accpt || accptact; } + void accept(bool y=TRUE) { accpt = y; } + void ignore() { accpt = FALSE; } + + bool isActionAccepted() const { return accptact; } + void acceptAction(bool y=TRUE) { accptact = y; } + enum Action { Copy, Link, Move, Private, UserAction=100 }; + void setAction( Action a ) { act = (uint)a; } + Action action() const { return Action(act); } + + QWidget* source() const; + const char* format( int n = 0 ) const; + QByteArray encodedData( const char* ) const; + bool provides( const char* ) const; + + QByteArray data(const char* f) const { return encodedData(f); } + + void setPoint( const QPoint& np ) { p = np; } + +protected: + QPoint p; + uint act:8; + uint accpt:1; + uint accptact:1; + uint resv:5; + void * d; +}; + + + +class Q_EXPORT QDragMoveEvent : public QDropEvent +{ +public: + QDragMoveEvent( const QPoint& pos, Type typ=DragMove ) + : QDropEvent(pos,typ), + rect( pos, QSize( 1, 1 ) ) {} + QRect answerRect() const { return rect; } + void accept( bool y=TRUE ) { QDropEvent::accept(y); } + void accept( const QRect & r) { accpt = TRUE; rect = r; } + void ignore( const QRect & r) { accpt =FALSE; rect = r; } + void ignore() { QDropEvent::ignore(); } + +protected: + QRect rect; +}; + + +class Q_EXPORT QDragEnterEvent : public QDragMoveEvent +{ +public: + QDragEnterEvent( const QPoint& pos ) : + QDragMoveEvent(pos, DragEnter) { } +}; + + +/* An internal class */ +class Q_EXPORT QDragResponseEvent : public QEvent +{ +public: + QDragResponseEvent( bool accepted ) + : QEvent(DragResponse), a(accepted) {} + bool dragAccepted() const { return a; } +protected: + bool a; +}; + + +class Q_EXPORT QDragLeaveEvent : public QEvent +{ +public: + QDragLeaveEvent() + : QEvent(DragLeave) {} +}; + +#endif // QT_NO_DRAGANDDROP + +class Q_EXPORT QChildEvent : public QEvent +{ +public: + QChildEvent( Type type, QObject *child ) + : QEvent(type), c(child) {} + QObject *child() const { return c; } + bool inserted() const { return t == ChildInserted; } + bool removed() const { return t == ChildRemoved; } +protected: + QObject *c; +}; + + +class Q_EXPORT QCustomEvent : public QEvent +{ +public: + QCustomEvent( int type ); + QCustomEvent( Type type, void *data ) + : QEvent(type), d(data) {}; + void *data() const { return d; } + void setData( void* data ) { d = data; } +private: + void *d; +}; + +#endif // QEVENT_H diff --git a/src/kernel/qeventloop.cpp b/src/kernel/qeventloop.cpp new file mode 100644 index 0000000..1f6a130 --- /dev/null +++ b/src/kernel/qeventloop.cpp @@ -0,0 +1,394 @@ +/**************************************************************************** +** +** Implementation of QEventLoop class +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qeventloop_p.h" // includes qplatformdefs.h +#include "qeventloop.h" +#include "qapplication.h" +#include "qdatetime.h" + +/*! + \class QEventLoop + \brief The QEventLoop class manages the event queue. + + \ingroup application + \ingroup events + + It receives events from the window system and other sources. It + then sends them to QApplication for processing and delivery. + + QEventLoop allows the application programmer to have more control + over event delivery. Programs that perform long operations can + call either processOneEvent() or processEvents() with various + ProcessEvent values OR'ed together to control which events should + be delivered. + + QEventLoop also allows the integration of an external event loop + with the Qt event loop. The Motif Extension included with Qt + includes a reimplementation of QEventLoop for merging Qt and Motif + events together. + + To use your own instance of QEventLoop or QEventLoop subclass create + it before you create the QApplication object. +*/ + +/*! \enum QEventLoop::ProcessEvents + + This enum controls the types of events processed by the + processEvents() functions. + + \value AllEvents - All events are processed + \value ExcludeUserInput - Do not process user input events. + ( ButtonPress, KeyPress, etc. ) + \value ExcludeSocketNotifiers - Do not process socket notifier + events. + \value WaitForMore - Wait for events if no pending events + are available. + + \sa processEvents() +*/ + +/*! \enum QEventLoop::ProcessEventsFlags + A \c typedef to allow various ProcessEvents values to be OR'ed together. + + \sa ProcessEvents + */ + +/*! + Creates a QEventLoop object, this object becomes the global event loop object. + There can only be one event loop object. The QEventLoop is usually constructed + by calling QApplication::eventLoop(). To create your own event loop object create + it before you instantiate the QApplication object. + + The \a parent and \a name arguments are passed on to the QObject constructor. +*/ +QEventLoop::QEventLoop( QObject *parent, const char *name ) + : QObject( parent, name ) +{ +#if defined(QT_CHECK_STATE) + if ( QApplication::eventloop ) + qFatal( "QEventLoop: there must be only one event loop object. \nConstruct it before QApplication." ); + // for now ;) +#endif // QT_CHECK_STATE + + d = new QEventLoopPrivate; + + init(); + QApplication::eventloop = this; +} + +/*! + Destructs the QEventLoop object. +*/ +QEventLoop::~QEventLoop() +{ + cleanup(); + delete d; + QApplication::eventloop = 0; +} + +/*! + Enters the main event loop and waits until exit() is called, and + returns the value that was set to exit(). + + It is necessary to call this function to start event handling. The + main event loop receives events from the window system and + dispatches these to the application widgets. + + Generally speaking, no user interaction can take place before + calling exec(). As a special case, modal widgets like QMessageBox + can be used before calling exec(), because modal widgets call + exec() to start a local event loop. + + To make your application perform idle processing, i.e. executing a + special function whenever there are no pending events, use a + QTimer with 0 timeout. More advanced idle processing schemes can + be achieved using processEvents(). + + \sa QApplication::quit(), exit(), processEvents() +*/ +int QEventLoop::exec() +{ + d->reset(); + + enterLoop(); + + // cleanup + d->looplevel = 0; + d->quitnow = FALSE; + d->exitloop = FALSE; + d->shortcut = FALSE; + // don't reset quitcode! + + return d->quitcode; +} + +/*! \fn void QEventLoop::exit( int retcode = 0 ) + + Tells the event loop to exit with a return code. + + After this function has been called, the event loop returns from + the call to exec(). The exec() function returns \a retcode. + + By convention, a \a retcode of 0 means success, and any non-zero + value indicates an error. + + Note that unlike the C library function of the same name, this + function \e does return to the caller -- it is event processing that + stops. + + \sa QApplication::quit(), exec() +*/ +void QEventLoop::exit( int retcode ) +{ + if ( d->quitnow ) // preserve existing quitcode + return; + d->quitcode = retcode; + d->quitnow = TRUE; + d->exitloop = TRUE; + d->shortcut = TRUE; +} + + +/*! \fn int QEventLoop::enterLoop() + + This function enters the main event loop (recursively). Do not call + it unless you really know what you are doing. + */ +int QEventLoop::enterLoop() +{ + // save the current exitloop state + bool old_exitloop = d->exitloop; + d->exitloop = FALSE; + d->shortcut = FALSE; + + d->looplevel++; + while ( ! d->exitloop ) + processEvents( AllEvents | WaitForMore ); + d->looplevel--; + + // restore the exitloop state, but if quitnow is TRUE, we need to keep + // exitloop set so that all other event loops drop out. + d->exitloop = old_exitloop || d->quitnow; + d->shortcut = d->quitnow; + + if ( d->looplevel < 1 ) { + d->quitnow = FALSE; + d->exitloop = FALSE; + d->shortcut = FALSE; + emit qApp->aboutToQuit(); + + // send deferred deletes + QApplication::sendPostedEvents( 0, QEvent::DeferredDelete ); + } + + return d->looplevel; +} + +/*! \fn void QEventLoop::exitLoop() + + This function exits from a recursive call to the main event loop. + Do not call it unless you really know what you are doing. +*/ +void QEventLoop::exitLoop() +{ + d->exitloop = TRUE; + d->shortcut = TRUE; +} + +/*! \fn void QEventLoop::loopLevel() const + + Returns the current loop level. +*/ +int QEventLoop::loopLevel() const +{ + return d->looplevel; +} + +/*! + Process pending events that match \a flags for a maximum of \a + maxTime milliseconds, or until there are no more events to + process, which ever is shorter. + + This function is especially useful if you have a long running + operation and want to show its progress without allowing user + input, i.e. by using the \c ExcludeUserInput flag. + + NOTE: This function will not process events continuously; it + returns after all available events are processed. + + NOTE: Specifying the \c WaitForMore flag makes no sense and will + be ignored. +*/ +void QEventLoop::processEvents( ProcessEventsFlags flags, int maxTime ) +{ + QTime start = QTime::currentTime(); + QTime now; + while ( ! d->quitnow && processEvents( flags & ~WaitForMore ) ) { + now = QTime::currentTime(); + if ( start.msecsTo( now ) > maxTime ) + break; + } +} + +/*! + \fn bool QEventLoop::processEvents( ProcessEventsFlags flags ) + \overload + + Processes pending events that match \a flags until there are no + more events to process. + + This function is especially useful if you have a long running + operation and want to show its progress without allowing user + input, i.e. by using the \c ExcludeUserInput flag. + + If the \c WaitForMore flag is set in \a flags, the behavior of + this function is as follows: + + \list + + \i If events are available, this function returns after processing + them. + + \i If no events are available, this function will wait until more + are available and return after processing newly available events. + + \endlist + + If the \c WaitForMore flag is \e not set in \a flags, and no + events are available, this function will return immediately. + + NOTE: This function will not process events continuously; it + returns after all available events are processed. + + This function returns TRUE if an event was processed; otherwise it + returns FALSE. + + \sa ProcessEvents hasPendingEvents() +*/ + +/*! \fn bool QEventLoop::hasPendingEvents() const + + Returns TRUE if there is an event waiting, otherwise it returns FALSE. +*/ + +/*! \fn void QEventLoop::registerSocketNotifier( QSocketNotifier *notifier ) + + Registers \a notifier with the event loop. Subclasses need to + reimplement this method to tie a socket notifier into another + event loop. Reimplementations \e MUST call the base + implementation. +*/ + +/*! \fn void QEventLoop::unregisterSocketNotifier( QSocketNotifier *notifier ) + + Unregisters \a notifier from the event loop. Subclasses need to + reimplement this method to tie a socket notifier into another + event loop. Reimplementations \e MUST call the base + implementation. +*/ + +/*! \fn void QEventLoop::setSocketNotifierPending( QSocketNotifier *notifier ) + + Marks \a notifier as pending. The socket notifier will be + activated the next time activateSocketNotifiers() is called. +*/ + +/*! \fn int QEventLoop::activateSocketNotifiers() + + Activates all pending socket notifiers and returns the number of + socket notifiers that were activated. +*/ + +/*! \fn int QEventLoop::activateTimers() + + Activates all Qt timers and returns the number of timers that were + activated. + + QEventLoop subclasses that do their own timer handling need to + call this after the time returned by timeToWait() has elapsed. + + Note: This function is only useful on systems where \c select() is + used to block the eventloop. On Windows, this function always + returns 0. On MacOS X, this function always returns 0 when the + GUI is enabled. On MacOS X, this function returns the documented + value when the GUI is disabled. +*/ + +/*! \fn int QEventLoop::timeToWait() const + + Returns the number of milliseconds that Qt needs to handle its + timers or -1 if there are no timers running. + + QEventLoop subclasses that do their own timer handling need to use + this to make sure that Qt's timers continue to work. + + Note: This function is only useful on systems where \c select() is + used to block the eventloop. On Windows, this function always + returns -1. On MacOS X, this function always returns -1 when the + GUI is enabled. On MacOS X, this function returns the documented + value when the GUI is disabled. +*/ + +/*! \fn void QEventLoop::wakeUp() + \threadsafe + + Wakes up the event loop. + + \sa awake() +*/ + +/*! \fn void QEventLoop::awake() + + This signal is emitted after the event loop returns from a + function that could block. + + \sa wakeUp() aboutToBlock() +*/ + +/*! \fn void QEventLoop::aboutToBlock() + + This signal is emitted before the event loop calls a function that + could block. + + \sa awake() +*/ + +#if !defined(Q_WS_X11) +void QEventLoop::appStartingUp(){} +void QEventLoop::appClosingDown(){} +#endif // Q_WS_X11 diff --git a/src/kernel/qeventloop.h b/src/kernel/qeventloop.h new file mode 100644 index 0000000..8d45c43 --- /dev/null +++ b/src/kernel/qeventloop.h @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Declaration of QEventLoop class +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QEVENTLOOP_H +#define QEVENTLOOP_H + +#ifndef QT_H +#include "qobject.h" +#include "qsocketnotifier.h" +#endif // QT_H + +class QEventLoopPrivate; +class QSocketNotifier; +class QTimer; +#ifdef Q_WS_MAC +struct timeval; //stdc struct +struct TimerInfo; //internal structure (qeventloop_mac.cpp) +#endif + +#if defined(QT_THREAD_SUPPORT) +class QMutex; +#endif // QT_THREAD_SUPPORT + + +class Q_EXPORT QEventLoop : public QObject +{ + Q_OBJECT + +public: + QEventLoop( QObject *parent = 0, const char *name = 0 ); + ~QEventLoop(); + + enum ProcessEvents { + AllEvents = 0x00, + ExcludeUserInput = 0x01, + ExcludeSocketNotifiers = 0x02, + WaitForMore = 0x04 + }; + typedef uint ProcessEventsFlags; + + void processEvents( ProcessEventsFlags flags, int maxtime ); + virtual bool processEvents( ProcessEventsFlags flags ); + + virtual bool hasPendingEvents() const; + + virtual void registerSocketNotifier( QSocketNotifier * ); + virtual void unregisterSocketNotifier( QSocketNotifier * ); + void setSocketNotifierPending( QSocketNotifier * ); + int activateSocketNotifiers(); + + int activateTimers(); + int timeToWait() const; + + virtual int exec(); + virtual void exit( int retcode = 0 ); + + virtual int enterLoop(); + virtual void exitLoop(); + virtual int loopLevel() const; + + virtual void wakeUp(); + +signals: + void awake(); + void aboutToBlock(); + +private: +#if defined(Q_WS_MAC) + friend QMAC_PASCAL void qt_mac_select_timer_callbk(EventLoopTimerRef, void *); + int macHandleSelect(timeval *); + void macHandleTimer(TimerInfo *); +#endif // Q_WS_MAC + + // internal initialization/cleanup - implemented in various platform specific files + void init(); + void cleanup(); + virtual void appStartingUp(); + virtual void appClosingDown(); + + // data for the default implementation - other implementations should not + // use/need this data + QEventLoopPrivate *d; + + friend class QApplication; +}; + +#endif // QEVENTLOOP_H diff --git a/src/kernel/qeventloop_p.h b/src/kernel/qeventloop_p.h new file mode 100644 index 0000000..4e7e365 --- /dev/null +++ b/src/kernel/qeventloop_p.h @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** Definition of QEventLoop class +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QEVENTLOOP_P_H +#define QEVENTLOOP_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. This header file may +// change from version to version without notice, or even be +// removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qplatformdefs.h" +#endif // QT_H + +// SCO OpenServer redefines raise -> kill +#if defined(raise) +# undef raise +#endif + +#include "qwindowdefs.h" + +class QSocketNotifier; +#ifdef Q_OS_MAC +class QMacSockNotPrivate; +#endif + +#if defined(Q_OS_UNIX) || defined (Q_WS_WIN) +#include "qptrlist.h" +#endif // Q_OS_UNIX || Q_WS_WIN + +#if defined(Q_OS_UNIX) +struct QSockNot +{ + QSocketNotifier *obj; + int fd; + fd_set *queue; +}; + +class QSockNotType +{ +public: + QSockNotType(); + ~QSockNotType(); + + QPtrList<QSockNot> *list; + fd_set select_fds; + fd_set enabled_fds; + fd_set pending_fds; + +}; +#endif // Q_OS_UNIX + +#if defined(Q_WS_WIN) +struct QSockNot { + QSocketNotifier *obj; + int fd; +}; +#endif // Q_WS_WIN + +class QEventLoopPrivate +{ +public: + QEventLoopPrivate() + { + reset(); + } + + void reset() { + looplevel = 0; + quitcode = 0; + quitnow = FALSE; + exitloop = FALSE; + shortcut = FALSE; + } + + int looplevel; + int quitcode; + unsigned int quitnow : 1; + unsigned int exitloop : 1; + unsigned int shortcut : 1; + +#if defined(Q_WS_MAC) + uchar next_select_timer; + EventLoopTimerRef select_timer; +#endif + +#if defined(Q_WS_X11) + int xfd; +#endif // Q_WS_X11 + +#if defined(Q_OS_UNIX) + int thread_pipe[2]; + + // pending socket notifiers list + QPtrList<QSockNot> sn_pending_list; + // highest fd for all socket notifiers + int sn_highest; + // 3 socket notifier types - read, write and exception + QSockNotType sn_vec[3]; +#endif + +#ifdef Q_WS_WIN + // pending socket notifiers list + QPtrList<QSockNot> sn_pending_list; +#endif // Q_WS_WIN + +}; + +#endif // QEVENTLOOP_P_H diff --git a/src/kernel/qeventloop_unix.cpp b/src/kernel/qeventloop_unix.cpp new file mode 100644 index 0000000..743eb1d --- /dev/null +++ b/src/kernel/qeventloop_unix.cpp @@ -0,0 +1,587 @@ +/**************************************************************************** +** +** Implementation of QEventLoop class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qeventloop_p.h" // includes qplatformdefs.h +#include "qeventloop.h" +#include "qapplication.h" +#include "qbitarray.h" +#include <stdlib.h> +#include <sys/types.h> + + +/***************************************************************************** + Timer handling; UNIX has no application timer support so we'll have to + make our own from scratch. + + NOTE: These functions are for internal use. QObject::startTimer() and + QObject::killTimer() are for public use. + The QTimer class provides a high-level interface which translates + timer events into signals. + + qStartTimer( interval, obj ) + Starts a timer which will run until it is killed with qKillTimer() + Arguments: + int interval timer interval in milliseconds + QObject *obj where to send the timer event + Returns: + int timer identifier, or zero if not successful + + qKillTimer( timerId ) + Stops a timer specified by a timer identifier. + Arguments: + int timerId timer identifier + Returns: + bool TRUE if successful + + qKillTimer( obj ) + Stops all timers that are sent to the specified object. + Arguments: + QObject *obj object receiving timer events + Returns: + bool TRUE if successful + *****************************************************************************/ + +// +// Internal data structure for timers +// + +struct TimerInfo { // internal timer info + int id; // - timer identifier + timeval interval; // - timer interval + timeval timeout; // - when to sent event + QObject *obj; // - object to receive event +}; + +typedef QPtrList<TimerInfo> TimerList; // list of TimerInfo structs + +static QBitArray *timerBitVec; // timer bit vector +static TimerList *timerList = 0; // timer list + +static void initTimers(); +void cleanupTimers(); +static timeval watchtime; // watch if time is turned back +timeval *qt_wait_timer(); +timeval *qt_wait_timer_max = 0; + +// +// Internal operator functions for timevals +// + +static inline bool operator<( const timeval &t1, const timeval &t2 ) +{ + return t1.tv_sec < t2.tv_sec || + (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec); +} + +static inline bool operator==( const timeval &t1, const timeval &t2 ) +{ + return t1.tv_sec == t2.tv_sec && t1.tv_usec == t2.tv_usec; +} + +static inline timeval &operator+=( timeval &t1, const timeval &t2 ) +{ + t1.tv_sec += t2.tv_sec; + if ( (t1.tv_usec += t2.tv_usec) >= 1000000 ) { + t1.tv_sec++; + t1.tv_usec -= 1000000; + } + return t1; +} + +static inline timeval operator+( const timeval &t1, const timeval &t2 ) +{ + timeval tmp; + tmp.tv_sec = t1.tv_sec + t2.tv_sec; + if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 ) { + tmp.tv_sec++; + tmp.tv_usec -= 1000000; + } + return tmp; +} + +static inline timeval operator-( const timeval &t1, const timeval &t2 ) +{ + timeval tmp; + tmp.tv_sec = t1.tv_sec - t2.tv_sec; + if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 ) { + tmp.tv_sec--; + tmp.tv_usec += 1000000; + } + return tmp; +} + + +// +// Internal functions for manipulating timer data structures. +// The timerBitVec array is used for keeping track of timer identifiers. +// + +static int allocTimerId() // find avail timer identifier +{ + int i = timerBitVec->size()-1; + while ( i >= 0 && (*timerBitVec)[i] ) + i--; + if ( i < 0 ) { + i = timerBitVec->size(); + timerBitVec->resize( 4 * i ); + for( int j=timerBitVec->size()-1; j > i; j-- ) + timerBitVec->clearBit( j ); + } + timerBitVec->setBit( i ); + return i+1; +} + +static void insertTimer( const TimerInfo *ti ) // insert timer info into list +{ + TimerInfo *t = timerList->first(); + int index = 0; +#if defined(QT_DEBUG) + int dangerCount = 0; +#endif + while ( t && t->timeout < ti->timeout ) { // list is sorted by timeout +#if defined(QT_DEBUG) + if ( t->obj == ti->obj ) + dangerCount++; +#endif + t = timerList->next(); + index++; + } + timerList->insert( index, ti ); // inserts sorted +#if defined(QT_DEBUG) + if ( dangerCount > 16 ) + qDebug( "QObject: %d timers now exist for object %s::%s", + dangerCount, ti->obj->className(), ti->obj->name() ); +#endif +} + +static inline void getTime( timeval &t ) // get time of day +{ + gettimeofday( &t, 0 ); + while ( t.tv_usec >= 1000000 ) { // NTP-related fix + t.tv_usec -= 1000000; + t.tv_sec++; + } + while ( t.tv_usec < 0 ) { + if ( t.tv_sec > 0 ) { + t.tv_usec += 1000000; + t.tv_sec--; + } else { + t.tv_usec = 0; + break; + } + } +} + +static void repairTimer( const timeval &time ) // repair broken timer +{ + timeval diff = watchtime - time; + register TimerInfo *t = timerList->first(); + while ( t ) { // repair all timers + t->timeout = t->timeout - diff; + t = timerList->next(); + } +} + +// +// Timer activation functions (called from the event loop) +// + +/* + Returns the time to wait for the next timer, or null if no timers are + waiting. + + The result is bounded to qt_wait_timer_max if this exists. +*/ + +timeval *qt_wait_timer() +{ + static timeval tm; + bool first = TRUE; + timeval currentTime; + if ( timerList && timerList->count() ) { // there are waiting timers + getTime( currentTime ); + if ( first ) { + if ( currentTime < watchtime ) // clock was turned back + repairTimer( currentTime ); + first = FALSE; + watchtime = currentTime; + } + TimerInfo *t = timerList->first(); // first waiting timer + if ( currentTime < t->timeout ) { // time to wait + tm = t->timeout - currentTime; + } else { + tm.tv_sec = 0; // no time to wait + tm.tv_usec = 0; + } + if ( qt_wait_timer_max && *qt_wait_timer_max < tm ) + tm = *qt_wait_timer_max; + return &tm; + } + if ( qt_wait_timer_max ) { + tm = *qt_wait_timer_max; + return &tm; + } + return 0; // no timers +} + +// Timer initialization +static void initTimers() // initialize timers +{ + timerBitVec = new QBitArray( 128 ); + Q_CHECK_PTR( timerBitVec ); + int i = timerBitVec->size(); + while( i-- > 0 ) + timerBitVec->clearBit( i ); + timerList = new TimerList; + Q_CHECK_PTR( timerList ); + timerList->setAutoDelete( TRUE ); + gettimeofday( &watchtime, 0 ); +} + +// Timer cleanup +void cleanupTimers() +{ + delete timerList; + timerList = 0; + delete timerBitVec; + timerBitVec = 0; +} + +// Main timer functions for starting and killing timers +int qStartTimer( int interval, QObject *obj ) +{ + if ( !timerList ) // initialize timer data + initTimers(); + int id = allocTimerId(); // get free timer id + if ( id <= 0 || + id > (int)timerBitVec->size() || !obj )// cannot create timer + return 0; + timerBitVec->setBit( id-1 ); // set timer active + TimerInfo *t = new TimerInfo; // create timer + Q_CHECK_PTR( t ); + t->id = id; + t->interval.tv_sec = interval/1000; + t->interval.tv_usec = (interval%1000)*1000; + timeval currentTime; + getTime( currentTime ); + t->timeout = currentTime + t->interval; + t->obj = obj; + insertTimer( t ); // put timer in list + return id; +} + +bool qKillTimer( int id ) +{ + register TimerInfo *t; + if ( !timerList || id <= 0 || + id > (int)timerBitVec->size() || !timerBitVec->testBit( id-1 ) ) + return FALSE; // not init'd or invalid timer + t = timerList->first(); + while ( t && t->id != id ) // find timer info in list + t = timerList->next(); + if ( t ) { // id found + timerBitVec->clearBit( id-1 ); // set timer inactive + return timerList->remove(); + } + else // id not found + return FALSE; +} + +bool qKillTimer( QObject *obj ) +{ + register TimerInfo *t; + if ( !timerList ) // not initialized + return FALSE; + t = timerList->first(); + while ( t ) { // check all timers + if ( t->obj == obj ) { // object found + timerBitVec->clearBit( t->id-1 ); + timerList->remove(); + t = timerList->current(); + } else { + t = timerList->next(); + } + } + return TRUE; +} + +/***************************************************************************** + Socket notifier type + *****************************************************************************/ +QSockNotType::QSockNotType() + : list( 0 ) +{ + FD_ZERO( &select_fds ); + FD_ZERO( &enabled_fds ); + FD_ZERO( &pending_fds ); +} + +QSockNotType::~QSockNotType() +{ + if ( list ) + delete list; + list = 0; +} + +/***************************************************************************** + QEventLoop implementations for UNIX + *****************************************************************************/ +void QEventLoop::registerSocketNotifier( QSocketNotifier *notifier ) +{ + int sockfd = notifier->socket(); + int type = notifier->type(); + if ( sockfd < 0 || sockfd >= FD_SETSIZE || type < 0 || type > 2 || notifier == 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QSocketNotifier: Internal error" ); +#endif + return; + } + + QPtrList<QSockNot> *list = d->sn_vec[type].list; + fd_set *fds = &d->sn_vec[type].enabled_fds; + QSockNot *sn; + + if ( ! list ) { + // create new list, the QSockNotType destructor will delete it for us + list = new QPtrList<QSockNot>; + Q_CHECK_PTR( list ); + list->setAutoDelete( TRUE ); + d->sn_vec[type].list = list; + } + + sn = new QSockNot; + Q_CHECK_PTR( sn ); + sn->obj = notifier; + sn->fd = sockfd; + sn->queue = &d->sn_vec[type].pending_fds; + + if ( list->isEmpty() ) { + list->insert( 0, sn ); + } else { // sort list by fd, decreasing + QSockNot *p = list->first(); + while ( p && p->fd > sockfd ) + p = list->next(); +#if defined(QT_CHECK_STATE) + if ( p && p->fd == sockfd ) { + static const char *t[] = { "read", "write", "exception" }; + qWarning( "QSocketNotifier: Multiple socket notifiers for " + "same socket %d and type %s", sockfd, t[type] ); + } +#endif + if ( p ) + list->insert( list->at(), sn ); + else + list->append( sn ); + } + + FD_SET( sockfd, fds ); + d->sn_highest = QMAX( d->sn_highest, sockfd ); +} + +void QEventLoop::unregisterSocketNotifier( QSocketNotifier *notifier ) +{ + int sockfd = notifier->socket(); + int type = notifier->type(); + if ( sockfd < 0 || type < 0 || type > 2 || notifier == 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QSocketNotifier: Internal error" ); +#endif + return; + } + + QPtrList<QSockNot> *list = d->sn_vec[type].list; + fd_set *fds = &d->sn_vec[type].enabled_fds; + QSockNot *sn; + if ( ! list ) + return; + sn = list->first(); + while ( sn && !(sn->obj == notifier && sn->fd == sockfd) ) + sn = list->next(); + if ( !sn ) // not found + return; + + FD_CLR( sockfd, fds ); // clear fd bit + FD_CLR( sockfd, sn->queue ); + d->sn_pending_list.removeRef( sn ); // remove from activation list + list->remove(); // remove notifier found above + + if ( d->sn_highest == sockfd ) { // find highest fd + d->sn_highest = -1; + for ( int i=0; i<3; i++ ) { + if ( d->sn_vec[i].list && ! d->sn_vec[i].list->isEmpty() ) + d->sn_highest = QMAX( d->sn_highest, // list is fd-sorted + d->sn_vec[i].list->getFirst()->fd ); + } + } +} + +void QEventLoop::setSocketNotifierPending( QSocketNotifier *notifier ) +{ + int sockfd = notifier->socket(); + int type = notifier->type(); + if ( sockfd < 0 || type < 0 || type > 2 || notifier == 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QSocketNotifier: Internal error" ); +#endif + return; + } + + QPtrList<QSockNot> *list = d->sn_vec[type].list; + QSockNot *sn; + if ( ! list ) + return; + sn = list->first(); + while ( sn && !(sn->obj == notifier && sn->fd == sockfd) ) + sn = list->next(); + if ( ! sn ) { // not found + return; + } + + // We choose a random activation order to be more fair under high load. + // If a constant order is used and a peer early in the list can + // saturate the IO, it might grab our attention completely. + // Also, if we're using a straight list, the callback routines may + // delete other entries from the list before those other entries are + // processed. + if ( ! FD_ISSET( sn->fd, sn->queue ) ) { + d->sn_pending_list.insert( (rand() & 0xff) % + (d->sn_pending_list.count()+1), sn ); + FD_SET( sn->fd, sn->queue ); + } +} + +void QEventLoop::wakeUp() +{ + /* + Apparently, there is not consistency among different operating + systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd argument to + ioctl() to be an int, which is normally 32-bit even on 64-bit + machines. + + IRIX, on the other hand, expects a size_t, which is 64-bit on + 64-bit machines. + + So, the solution is to use size_t initialized to zero to make + sure all bits are set to zero, preventing underflow with the + FreeBSD/Linux/Solaris ioctls. + */ + size_t nbytes = 0; + char c = 0; + if ( ::ioctl( d->thread_pipe[0], FIONREAD, (char*)&nbytes ) >= 0 && nbytes == 0 ) { + ::write( d->thread_pipe[1], &c, 1 ); + } +} + +int QEventLoop::timeToWait() const +{ + timeval *tm = qt_wait_timer(); + if ( ! tm ) // no active timers + return -1; + return (tm->tv_sec*1000) + (tm->tv_usec/1000); +} + +int QEventLoop::activateTimers() +{ + if ( !timerList || !timerList->count() ) // no timers + return 0; + bool first = TRUE; + timeval currentTime; + int n_act = 0, maxCount = timerList->count(); + TimerInfo *begin = 0; + register TimerInfo *t; + + for ( ;; ) { + if ( ! maxCount-- ) + break; + getTime( currentTime ); // get current time + if ( first ) { + if ( currentTime < watchtime ) // clock was turned back + repairTimer( currentTime ); + first = FALSE; + watchtime = currentTime; + } + t = timerList->first(); + if ( !t || currentTime < t->timeout ) // no timer has expired + break; + if ( ! begin ) { + begin = t; + } else if ( begin == t ) { + // avoid sending the same timer multiple times + break; + } else if ( t->interval < begin->interval || t->interval == begin->interval ) { + begin = t; + } + timerList->take(); // unlink from list + t->timeout += t->interval; + if ( t->timeout < currentTime ) + t->timeout = currentTime + t->interval; + insertTimer( t ); // relink timer + if ( t->interval.tv_usec > 0 || t->interval.tv_sec > 0 ) + n_act++; + QTimerEvent e( t->id ); + QApplication::sendEvent( t->obj, &e ); // send event + if ( timerList->findRef( begin ) == -1 ) + begin = 0; + } + return n_act; +} + +int QEventLoop::activateSocketNotifiers() +{ + if ( d->sn_pending_list.isEmpty() ) + return 0; + + // activate entries + int n_act = 0; + QEvent event( QEvent::SockAct ); + QPtrListIterator<QSockNot> it( d->sn_pending_list ); + QSockNot *sn; + while ( (sn=it.current()) ) { + ++it; + d->sn_pending_list.removeRef( sn ); + if ( FD_ISSET(sn->fd, sn->queue) ) { + FD_CLR( sn->fd, sn->queue ); + QApplication::sendEvent( sn->obj, &event ); + n_act++; + } + } + + return n_act; +} diff --git a/src/kernel/qeventloop_x11.cpp b/src/kernel/qeventloop_x11.cpp new file mode 100644 index 0000000..e3b43a6 --- /dev/null +++ b/src/kernel/qeventloop_x11.cpp @@ -0,0 +1,417 @@ +/**************************************************************************** +** +** Implementation of QEventLoop class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qeventloop_p.h" // includes qplatformdefs.h +#include "qeventloop.h" +#include "qapplication.h" +#include "qbitarray.h" +#include "qcolor_p.h" +#include "qt_x11_p.h" + +#if defined(QT_THREAD_SUPPORT) +# include "qmutex.h" +#endif // QT_THREAD_SUPPORT + +#include <errno.h> + + +// resolve the conflict between X11's FocusIn and QEvent::FocusIn +#undef FocusOut +#undef FocusIn + +static const int XKeyPress = KeyPress; +static const int XKeyRelease = KeyRelease; +#undef KeyPress +#undef KeyRelease + +// from qapplication.cpp +extern bool qt_is_gui_used; + +// from qeventloop_unix.cpp +extern timeval *qt_wait_timer(); +extern void cleanupTimers(); + +// ### this needs to go away at some point... +typedef void (*VFPTR)(); +typedef QValueList<VFPTR> QVFuncList; +void qt_install_preselect_handler( VFPTR ); +void qt_remove_preselect_handler( VFPTR ); +static QVFuncList *qt_preselect_handler = 0; +void qt_install_postselect_handler( VFPTR ); +void qt_remove_postselect_handler( VFPTR ); +static QVFuncList *qt_postselect_handler = 0; + +void qt_install_preselect_handler( VFPTR handler ) +{ + if ( !qt_preselect_handler ) + qt_preselect_handler = new QVFuncList; + qt_preselect_handler->append( handler ); +} +void qt_remove_preselect_handler( VFPTR handler ) +{ + if ( qt_preselect_handler ) { + QVFuncList::Iterator it = qt_preselect_handler->find( handler ); + if ( it != qt_preselect_handler->end() ) + qt_preselect_handler->remove( it ); + } +} +void qt_install_postselect_handler( VFPTR handler ) +{ + if ( !qt_postselect_handler ) + qt_postselect_handler = new QVFuncList; + qt_postselect_handler->prepend( handler ); +} +void qt_remove_postselect_handler( VFPTR handler ) +{ + if ( qt_postselect_handler ) { + QVFuncList::Iterator it = qt_postselect_handler->find( handler ); + if ( it != qt_postselect_handler->end() ) + qt_postselect_handler->remove( it ); + } +} + + +void QEventLoop::init() +{ + // initialize the common parts of the event loop + pipe( d->thread_pipe ); + fcntl(d->thread_pipe[0], F_SETFD, FD_CLOEXEC); + fcntl(d->thread_pipe[1], F_SETFD, FD_CLOEXEC); + + d->sn_highest = -1; + + // intitialize the X11 parts of the event loop + d->xfd = -1; + if ( qt_is_gui_used ) + d->xfd = XConnectionNumber( QPaintDevice::x11AppDisplay() ); +} + +void QEventLoop::cleanup() +{ + // cleanup the common parts of the event loop + close( d->thread_pipe[0] ); + close( d->thread_pipe[1] ); + cleanupTimers(); + + // cleanup the X11 parts of the event loop + d->xfd = -1; +} + +bool QEventLoop::processEvents( ProcessEventsFlags flags ) +{ + // process events from the X server + XEvent event; + int nevents = 0; + +#if defined(QT_THREAD_SUPPORT) + QMutexLocker locker( QApplication::qt_mutex ); +#endif + + // handle gui and posted events + if ( qt_is_gui_used ) { + QApplication::sendPostedEvents(); + + // Two loops so that posted events accumulate + while ( XPending( QPaintDevice::x11AppDisplay() ) ) { + // also flushes output buffer + while ( XPending( QPaintDevice::x11AppDisplay() ) ) { + if ( d->shortcut ) { + return FALSE; + } + + XNextEvent( QPaintDevice::x11AppDisplay(), &event ); + + if ( flags & ExcludeUserInput ) { + switch ( event.type ) { + case ButtonPress: + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + case EnterNotify: + case LeaveNotify: + continue; + + case ClientMessage: + { + // from qapplication_x11.cpp + extern Atom qt_wm_protocols; + extern Atom qt_wm_take_focus; + extern Atom qt_qt_scrolldone; + + // only keep the wm_take_focus and + // qt_qt_scrolldone protocols, discard all + // other client messages + if ( event.xclient.format != 32 ) + continue; + + if ( event.xclient.message_type == qt_wm_protocols || + (Atom) event.xclient.data.l[0] == qt_wm_take_focus ) + break; + if ( event.xclient.message_type == qt_qt_scrolldone ) + break; + } + + default: break; + } + } + + nevents++; + if ( qApp->x11ProcessEvent( &event ) == 1 ) + return TRUE; + } + } + } + + if ( d->shortcut ) { + return FALSE; + } + + QApplication::sendPostedEvents(); + + const uint exclude_all = ExcludeSocketNotifiers | 0x08; + // 0x08 == ExcludeTimers for X11 only + if ( nevents > 0 && ( flags & exclude_all ) == exclude_all && + ( flags & WaitForMore ) ) { + return TRUE; + } + + // don't block if exitLoop() or exit()/quit() has been called. + bool canWait = d->exitloop || d->quitnow ? FALSE : (flags & WaitForMore); + + // Process timers and socket notifiers - the common UNIX stuff + + // return the maximum time we can wait for an event. + static timeval zerotm; + timeval *tm = 0; + if ( ! ( flags & 0x08 ) ) { // 0x08 == ExcludeTimers for X11 only + tm = qt_wait_timer(); // wait for timer or X event + if ( !canWait ) { + if ( !tm ) + tm = &zerotm; + tm->tv_sec = 0; // no time to wait + tm->tv_usec = 0; + } + } + + int highest = 0; + if ( ! ( flags & ExcludeSocketNotifiers ) ) { + // return the highest fd we can wait for input on + if ( d->sn_highest >= 0 ) { // has socket notifier(s) + if ( d->sn_vec[0].list && ! d->sn_vec[0].list->isEmpty() ) + d->sn_vec[0].select_fds = d->sn_vec[0].enabled_fds; + else + FD_ZERO( &d->sn_vec[0].select_fds ); + + if ( d->sn_vec[1].list && ! d->sn_vec[1].list->isEmpty() ) + d->sn_vec[1].select_fds = d->sn_vec[1].enabled_fds; + else + FD_ZERO( &d->sn_vec[1].select_fds ); + + if ( d->sn_vec[2].list && ! d->sn_vec[2].list->isEmpty() ) + d->sn_vec[2].select_fds = d->sn_vec[2].enabled_fds; + else + FD_ZERO( &d->sn_vec[2].select_fds ); + } else { + FD_ZERO( &d->sn_vec[0].select_fds ); + + FD_ZERO( &d->sn_vec[1].select_fds ); + FD_ZERO( &d->sn_vec[2].select_fds ); + } + + highest = d->sn_highest; + } else { + FD_ZERO( &d->sn_vec[0].select_fds ); + FD_ZERO( &d->sn_vec[1].select_fds ); + FD_ZERO( &d->sn_vec[2].select_fds ); + } + + if ( qt_is_gui_used ) { + // select for events on the event socket - only on X11 + FD_SET( d->xfd, &d->sn_vec[0].select_fds ); + highest = QMAX( highest, d->xfd ); + } + + FD_SET( d->thread_pipe[0], &d->sn_vec[0].select_fds ); + highest = QMAX( highest, d->thread_pipe[0] ); + + if ( canWait ) + emit aboutToBlock(); + + if ( qt_preselect_handler ) { + QVFuncList::Iterator it, end = qt_preselect_handler->end(); + for ( it = qt_preselect_handler->begin(); it != end; ++it ) + (**it)(); + } + + // unlock the GUI mutex and select. when we return from this function, there is + // something for us to do +#if defined(QT_THREAD_SUPPORT) + locker.mutex()->unlock(); +#endif + + int nsel; + do { + nsel = select( highest + 1, + &d->sn_vec[0].select_fds, + &d->sn_vec[1].select_fds, + &d->sn_vec[2].select_fds, + tm ); + } while (nsel == -1 && (errno == EINTR || errno == EAGAIN)); + + // relock the GUI mutex before processing any pending events +#if defined(QT_THREAD_SUPPORT) + locker.mutex()->lock(); +#endif + + // we are awake, broadcast it + emit awake(); + emit qApp->guiThreadAwake(); + + if (nsel == -1) { + if (errno == EBADF) { + // it seems a socket notifier has a bad fd... find out + // which one it is and disable it + fd_set fdset; + zerotm.tv_sec = zerotm.tv_usec = 0l; + + for (int type = 0; type < 3; ++type) { + QPtrList<QSockNot> *list = d->sn_vec[type].list; + if (!list) continue; + + QSockNot *sn = list->first(); + while (sn) { + FD_ZERO(&fdset); + FD_SET(sn->fd, &fdset); + + int ret = -1; + do { + switch (type) { + case 0: // read + ret = select(sn->fd + 1, &fdset, 0, 0, &zerotm); + break; + case 1: // write + ret = select(sn->fd + 1, 0, &fdset, 0, &zerotm); + break; + case 2: // except + ret = select(sn->fd + 1, 0, 0, &fdset, &zerotm); + break; + } + } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); + + if (ret == -1 && errno == EBADF) { + // disable the invalid socket notifier + static const char *t[] = { "Read", "Write", "Exception" }; + qWarning("QSocketNotifier: invalid socket %d and type '%s', disabling...", + sn->fd, t[type]); + sn->obj->setEnabled(FALSE); + } + + sn = list->next(); + } + } + } else { + // EINVAL... shouldn't happen, so let's complain to stderr + // and hope someone sends us a bug report + perror( "select" ); + } + } + + // some other thread woke us up... consume the data on the thread pipe so that + // select doesn't immediately return next time + if ( nsel > 0 && FD_ISSET( d->thread_pipe[0], &d->sn_vec[0].select_fds ) ) { + char c; + ::read( d->thread_pipe[0], &c, 1 ); + } + + if ( qt_postselect_handler ) { + QVFuncList::Iterator it, end = qt_postselect_handler->end(); + for ( it = qt_postselect_handler->begin(); it != end; ++it ) + (**it)(); + } + + // activate socket notifiers + if ( ! ( flags & ExcludeSocketNotifiers ) && nsel > 0 && d->sn_highest >= 0 ) { + // if select says data is ready on any socket, then set the socket notifier + // to pending + int i; + for ( i=0; i<3; i++ ) { + if ( ! d->sn_vec[i].list ) + continue; + + QPtrList<QSockNot> *list = d->sn_vec[i].list; + QSockNot *sn = list->first(); + while ( sn ) { + if ( FD_ISSET( sn->fd, &d->sn_vec[i].select_fds ) ) + setSocketNotifierPending( sn->obj ); + sn = list->next(); + } + } + + nevents += activateSocketNotifiers(); + } + + // activate timers + if ( ! ( flags & 0x08 ) ) { + // 0x08 == ExcludeTimers for X11 only + nevents += activateTimers(); + } + + // color approx. optimization - only on X11 + qt_reset_color_avail(); + + // return true if we handled events, false otherwise + return (nevents > 0); +} + +bool QEventLoop::hasPendingEvents() const +{ + extern uint qGlobalPostedEventsCount(); // from qapplication.cpp + return ( qGlobalPostedEventsCount() || XPending( QPaintDevice::x11AppDisplay() ) ); +} + +void QEventLoop::appStartingUp() +{ + if ( qt_is_gui_used ) + d->xfd = XConnectionNumber( QPaintDevice::x11AppDisplay() ); +} + +void QEventLoop::appClosingDown() +{ + d->xfd = -1; +} diff --git a/src/kernel/qfocusdata.cpp b/src/kernel/qfocusdata.cpp new file mode 100644 index 0000000..4645447 --- /dev/null +++ b/src/kernel/qfocusdata.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Implementation of QFocusData class +** +** Created : 980622 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qfocusdata.h" + +/*! + \class QFocusData qfocusdata.h + \brief The QFocusData class maintains the list of widgets in the focus + chain. + + \ingroup misc + + This read-only list always contains at least one widget (i.e. the + top-level widget). It provides a simple cursor which can be reset + to the current focus widget using home(), or moved to its + neighboring widgets using next() and prev(). You can also retrieve + the count() of the number of widgets in the list. The list is a + loop, so if you keep iterating, for example using next(), you will + never come to the end. + + Some widgets in the list may not accept focus. Widgets are added + to the list as necessary, but not removed from it. This lets + widgets change focus policy dynamically without disrupting the + focus chain the user experiences. When a widget disables and + re-enables tab focus, its position in the focus chain does not + change. + + When reimplementing QWidget::focusNextPrevChild() to provide + special focus flow, you will usually call QWidget::focusData() to + retrieve the focus data stored at the top-level widget. A + top-level widget's focus data contains the focus list for its + hierarchy of widgets. + + The cursor may change at any time. + + This class is \e not thread-safe. + + \sa QWidget::focusNextPrevChild() QWidget::setTabOrder() + QWidget::setFocusPolicy() +*/ + +/*! + \fn QWidget* QFocusData::focusWidget() const + + Returns the widgets in the hierarchy that are in the focus chain. +*/ + +/*! + \fn int QFocusData::count() const + + Returns the number of widgets in the focus chain. +*/ + +/*! + Moves the cursor to the focusWidget() and returns that widget. You + must call this before next() or prev() to iterate meaningfully. +*/ +QWidget* QFocusData::home() +{ + focusWidgets.find(it.current()); + return focusWidgets.current(); +} + +/*! + Moves the cursor to the next widget in the focus chain. There is + \e always a next widget because the list is a loop. +*/ +QWidget* QFocusData::next() +{ + QWidget* r = focusWidgets.next(); + if ( !r ) + r = focusWidgets.first(); + return r; +} + +/*! + Moves the cursor to the previous widget in the focus chain. There + is \e always a previous widget because the list is a loop. +*/ +QWidget* QFocusData::prev() +{ + QWidget* r = focusWidgets.prev(); + if ( !r ) + r = focusWidgets.last(); + return r; +} + +/*! + Returns the last widget in the focus chain. + The cursor is not modified. +*/ +QWidget *QFocusData::last() const +{ + return focusWidgets.getLast(); +} + +/*! + Returns the first widget in the focus chain. + The cursor is not modified. +*/ +QWidget *QFocusData::first() const +{ + return focusWidgets.getFirst(); +} diff --git a/src/kernel/qfocusdata.h b/src/kernel/qfocusdata.h new file mode 100644 index 0000000..4b610a9 --- /dev/null +++ b/src/kernel/qfocusdata.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Definition of internal QFocusData class +** +** Created : 980405 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QFOCUSDATA_H +#define QFOCUSDATA_H + +#ifndef QT_H +#include "qwidgetlist.h" +#endif // QT_H + + +class Q_EXPORT QFocusData { +public: + QWidget* focusWidget() const { return it.current(); } + + QWidget* home(); + QWidget* next(); + QWidget* prev(); + QWidget* first() const; + QWidget* last() const; + int count() const { return focusWidgets.count(); } + +private: + friend class QWidget; + + QFocusData() + : it(focusWidgets) {} + QWidgetList focusWidgets; + QWidgetListIt it; +}; + + +#endif // QFOCUSDATA_H diff --git a/src/kernel/qfont.cpp b/src/kernel/qfont.cpp new file mode 100644 index 0000000..66ba453 --- /dev/null +++ b/src/kernel/qfont.cpp @@ -0,0 +1,3356 @@ +/**************************************************************************** +** +** Implementation of QFont, QFontMetrics and QFontInfo classes +** +** Created : 941207 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#define QT_FATAL_ASSERT + +#include "qfont.h" +#include "qfontdatabase.h" +#include "qfontmetrics.h" +#include "qfontinfo.h" +#include "qpainter.h" +#include "qdict.h" +#include "qcache.h" +#include "qdatastream.h" +#include "qapplication.h" +#include "qcleanuphandler.h" +#include "qstringlist.h" +#ifdef Q_WS_MAC +#include "qpaintdevicemetrics.h" +#endif + +#include <private/qunicodetables_p.h> +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qpainter_p.h" +#include "qtextengine_p.h" + +// #define QFONTCACHE_DEBUG +#ifdef QFONTCACHE_DEBUG +# define FC_DEBUG qDebug +#else +# define FC_DEBUG if (FALSE) qDebug +#endif + + + + +bool QFontDef::operator==( const QFontDef &other ) const +{ + /* + QFontDef comparison is more complicated than just simple + per-member comparisons. + + When comparing point/pixel sizes, either point or pixelsize + could be -1. in This case we have to compare the non negative + size value. + + This test will fail if the point-sizes differ by 1/2 point or + more or they do not round to the same value. We have to do this + since our API still uses 'int' point-sizes in the API, but store + deci-point-sizes internally. + + To compare the family members, we need to parse the font names + and compare the family/foundry strings separately. This allows + us to compare e.g. "Helvetica" and "Helvetica [Adobe]" with + positive results. + */ + if (pixelSize != -1 && other.pixelSize != -1) { + if (pixelSize != other.pixelSize) + return FALSE; + } else if (pointSize != -1 && other.pointSize != -1) { + if (pointSize != other.pointSize + && (QABS(pointSize - other.pointSize) >= 5 + || qRound(pointSize/10.) != qRound(other.pointSize/10.))) + return FALSE; + } else { + return FALSE; + } + + if (!ignorePitch && !other.ignorePitch && fixedPitch != other.fixedPitch) + return FALSE; + + if (stretch != 0 && other.stretch != 0 && stretch != other.stretch) + return FALSE; + + QString this_family, this_foundry, other_family, other_foundry; + QFontDatabase::parseFontName(family, this_foundry, this_family); + QFontDatabase::parseFontName(other.family, other_foundry, other_family); + + return ( styleHint == other.styleHint + && styleStrategy == other.styleStrategy + && weight == other.weight + && italic == other.italic + && this_family == other_family + && (this_foundry.isEmpty() + || other_foundry.isEmpty() + || this_foundry == other_foundry) +#ifdef Q_WS_X11 + && addStyle == other.addStyle +#endif // Q_WS_X11 + ); +} + + + + +QFontPrivate::QFontPrivate() + : engineData( 0 ), paintdevice( 0 ), + rawMode( FALSE ), underline( FALSE ), overline( FALSE ), strikeOut( FALSE ), + mask( 0 ) +{ +#ifdef Q_WS_X11 + screen = QPaintDevice::x11AppScreen(); +#else + screen = 0; +#endif // Q_WS_X11 +} + +QFontPrivate::QFontPrivate( const QFontPrivate &other ) + : QShared(), request( other.request ), engineData( 0 ), + paintdevice( other.paintdevice ), screen( other.screen ), + rawMode( other.rawMode ), underline( other.underline ), overline( other.overline ), + strikeOut( other.strikeOut ), mask( other.mask ) +{ +} + +QFontPrivate::~QFontPrivate() +{ + if ( engineData ) + engineData->deref(); + engineData = 0; +} + +void QFontPrivate::resolve( const QFontPrivate *other ) +{ +#ifdef QT_CHECK_STATE + Q_ASSERT( other != 0 ); +#endif + + if ( ( mask & Complete ) == Complete ) return; + + // assign the unset-bits with the set-bits of the other font def + if ( ! ( mask & Family ) ) + request.family = other->request.family; + + if ( ! ( mask & Size ) ) { + request.pointSize = other->request.pointSize; + request.pixelSize = other->request.pixelSize; + } + + if ( ! ( mask & StyleHint ) ) + request.styleHint = other->request.styleHint; + + if ( ! ( mask & StyleStrategy ) ) + request.styleStrategy = other->request.styleStrategy; + + if ( ! ( mask & Weight ) ) + request.weight = other->request.weight; + + if ( ! ( mask & Italic ) ) + request.italic = other->request.italic; + + if ( ! ( mask & FixedPitch ) ) + request.fixedPitch = other->request.fixedPitch; + + if ( ! ( mask & Stretch ) ) + request.stretch = other->request.stretch; + + if ( ! ( mask & Underline ) ) + underline = other->underline; + + if ( ! ( mask & Overline ) ) + overline = other->overline; + + if ( ! ( mask & StrikeOut ) ) + strikeOut = other->strikeOut; +} + + + + +QFontEngineData::QFontEngineData() + : lineWidth( 1 ) +{ +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + memset( engines, 0, QFont::LastPrivateScript * sizeof( QFontEngine * ) ); +#else + engine = 0; +#endif // Q_WS_X11 || Q_WS_WIN +#ifndef Q_WS_MAC + memset( widthCache, 0, widthCacheSize*sizeof( uchar ) ); +#endif +} + +QFontEngineData::~QFontEngineData() +{ +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + for ( int i = 0; i < QFont::LastPrivateScript; i++ ) { + if ( engines[i] ) + engines[i]->deref(); + engines[i] = 0; + } +#else + if ( engine ) + engine->deref(); + engine = 0; +#endif // Q_WS_X11 || Q_WS_WIN +} + + + + +/*! + \class QFont qfont.h + \brief The QFont class specifies a font used for drawing text. + + \ingroup graphics + \ingroup appearance + \ingroup shared + \mainclass + + When you create a QFont object you specify various attributes that + you want the font to have. Qt will use the font with the specified + attributes, or if no matching font exists, Qt will use the closest + matching installed font. The attributes of the font that is + actually used are retrievable from a QFontInfo object. If the + window system provides an exact match exactMatch() returns TRUE. + Use QFontMetrics to get measurements, e.g. the pixel length of a + string using QFontMetrics::width(). + + Use QApplication::setFont() to set the application's default font. + + If a choosen X11 font does not include all the characters that + need to be displayed, QFont will try to find the characters in the + nearest equivalent fonts. When a QPainter draws a character from a + font the QFont will report whether or not it has the character; if + it does not, QPainter will draw an unfilled square. + + Create QFonts like this: + \code + QFont serifFont( "Times", 10, Bold ); + QFont sansFont( "Helvetica [Cronyx]", 12 ); + \endcode + + The attributes set in the constructor can also be set later, e.g. + setFamily(), setPointSize(), setPointSizeFloat(), setWeight() and + setItalic(). The remaining attributes must be set after + contstruction, e.g. setBold(), setUnderline(), setOverline(), + setStrikeOut() and setFixedPitch(). QFontInfo objects should be + created \e after the font's attributes have been set. A QFontInfo + object will not change, even if you change the font's + attributes. The corresponding "get" functions, e.g. family(), + pointSize(), etc., return the values that were set, even though + the values used may differ. The actual values are available from a + QFontInfo object. + + If the requested font family is unavailable you can influence the + \link #fontmatching font matching algorithm\endlink by choosing a + particular \l{QFont::StyleHint} and \l{QFont::StyleStrategy} with + setStyleHint(). The default family (corresponding to the current + style hint) is returned by defaultFamily(). + + The font-matching algorithm has a lastResortFamily() and + lastResortFont() in cases where a suitable match cannot be found. + You can provide substitutions for font family names using + insertSubstitution() and insertSubstitutions(). Substitutions can + be removed with removeSubstitution(). Use substitute() to retrieve + a family's first substitute, or the family name itself if it has + no substitutes. Use substitutes() to retrieve a list of a family's + substitutes (which may be empty). + + Every QFont has a key() which you can use, for example, as the key + in a cache or dictionary. If you want to store a user's font + preferences you could use QSettings, writing the font information + with toString() and reading it back with fromString(). The + operator<<() and operator>>() functions are also available, but + they work on a data stream. + + It is possible to set the height of characters shown on the screen + to a specified number of pixels with setPixelSize(); however using + setPointSize() has a similar effect and provides device + independence. + + Under the X Window System you can set a font using its system + specific name with setRawName(). + + Loading fonts can be expensive, especially on X11. QFont contains + extensive optimizations to make the copying of QFont objects fast, + and to cache the results of the slow window system functions it + depends upon. + + \target fontmatching + The font matching algorithm works as follows: + \list 1 + \i The specified font family is searched for. + \i If not found, the styleHint() is used to select a replacement + family. + \i Each replacement font family is searched for. + \i If none of these are found or there was no styleHint(), "helvetica" + will be searched for. + \i If "helvetica" isn't found Qt will try the lastResortFamily(). + \i If the lastResortFamily() isn't found Qt will try the + lastResortFont() which will always return a name of some kind. + \endlist + + Once a font is found, the remaining attributes are matched in order of + priority: + \list 1 + \i fixedPitch() + \i pointSize() (see below) + \i weight() + \i italic() + \endlist + + If you have a font which matches on family, even if none of the + other attributes match, this font will be chosen in preference to + a font which doesn't match on family but which does match on the + other attributes. This is because font family is the dominant + search criteria. + + The point size is defined to match if it is within 20% of the + requested point size. When several fonts match and are only + distinguished by point size, the font with the closest point size + to the one requested will be chosen. + + The actual family, font size, weight and other font attributes + used for drawing text will depend on what's available for the + chosen family under the window system. A QFontInfo object can be + used to determine the actual values used for drawing the text. + + Examples: + + \code + QFont f("Helvetica"); + \endcode + If you had both an Adobe and a Cronyx Helvetica, you might get + either. + + \code + QFont f1( "Helvetica [Cronyx]" ); // Qt 3.x + QFont f2( "Cronyx-Helvetica" ); // Qt 2.x compatibility + \endcode + You can specify the foundry you want in the family name. Both fonts, + f1 and f2, in the above example will be set to "Helvetica + [Cronyx]". + + To determine the attributes of the font actually used in the window + system, use a QFontInfo object, e.g. + \code + QFontInfo info( f1 ); + QString family = info.family(); + \endcode + + To find out font metrics use a QFontMetrics object, e.g. + \code + QFontMetrics fm( f1 ); + int pixelWidth = fm.width( "How many pixels wide is this text?" ); + int pixelHeight = fm.height(); + \endcode + + For more general information on fonts, see the + \link http://www.nwalsh.com/comp.fonts/FAQ/ comp.fonts FAQ.\endlink + Information on encodings can be found from + \link http://czyborra.com/ Roman Czyborra's\endlink page. + + \sa QFontMetrics QFontInfo QFontDatabase QApplication::setFont() + QWidget::setFont() QPainter::setFont() QFont::StyleHint + QFont::Weight +*/ + +/*! + \enum QFont::Script + + This enum represents \link unicode.html Unicode \endlink allocated + scripts. For exhaustive coverage see \link + http://www.amazon.com/exec/obidos/ASIN/0201616335/trolltech/t The + Unicode Standard Version 3.0 \endlink. The following scripts are + supported: + + Modern European alphabetic scripts (left to right): + + \value Latin consists of most alphabets based on the original Latin alphabet. + \value Greek covers ancient and modern Greek and Coptic. + \value Cyrillic covers the Slavic and non-Slavic languages using + cyrillic alphabets. + \value Armenian contains the Armenian alphabet used with the + Armenian language. + \value Georgian covers at least the language Georgian. + \value Runic covers the known constituents of the Runic alphabets used + by the early and medieval societies in the Germanic, + Scandinavian, and Anglo-Saxon areas. + \value Ogham is an alphabetical script used to write a very early + form of Irish. + \value SpacingModifiers are small signs indicating modifications + to the preceeding letter. + \value CombiningMarks consist of diacritical marks not specific to + a particular alphabet, diacritical marks used in + combination with mathematical and technical symbols, and + glyph encodings applied to multiple letterforms. + + Middle Eastern scripts (right to left): + + \value Hebrew is used for writing Hebrew, Yiddish, and some other languages. + \value Arabic covers the Arabic language as well as Persian, Urdu, + Kurdish and some others. + \value Syriac is used to write the active liturgical languages and + dialects of several Middle Eastern and Southeast Indian + communities. + \value Thaana is used to write the Maledivian Dhivehi language. + + South and Southeast Asian scripts (left to right with few historical exceptions): + + \value Devanagari covers classical Sanskrit and modern Hindi as + well as several other languages. + \value Bengali is a relative to Devanagari employed to write the + Bengali language used in West Bengal/India and Bangladesh + as well as several minority languages. + \value Gurmukhi is another Devanagari relative used to write Punjabi. + \value Gujarati is closely related to Devanagari and used to write + the Gujarati language of the Gujarat state in India. + \value Oriya is used to write the Oriya language of Orissa state/India. + \value Tamil is used to write the Tamil language of Tamil Nadu state/India, + Sri Lanka, Singapore and parts of Malaysia as well as some + minority languages. + \value Telugu is used to write the Telugu language of Andhra + Pradesh state/India and some minority languages. + \value Kannada is another South Indian script used to write the + Kannada language of Karnataka state/India and some minority + languages. + \value Malayalam is used to write the Malayalam language of Kerala + state/India. + \value Sinhala is used for Sri Lanka's majority language Sinhala + and is also employed to write Pali, Sanskrit, and Tamil. + \value Thai is used to write Thai and other Southeast Asian languages. + \value Lao is a language and script quite similar to Thai. + \value Tibetan is the script used to write Tibetan in several + countries like Tibet, the bordering Indian regions and + Nepal. It is also used in the Buddist philosophy and + liturgy of the Mongolian cultural area. + \value Myanmar is mainly used to write the Burmese language of + Myanmar (former Burma). + \value Khmer is the official language of Kampuchea. + + East Asian scripts (traditionally top-down, right to left, modern + often horizontal left to right): + + \value Han consists of the CJK (Chinese, Japanese, Korean) + idiographic characters. + \value Hiragana is a cursive syllabary used to indicate phonetics + and pronounciation of Japanese words. + \value Katakana is a non-cursive syllabic script used to write + Japanese words with visual emphasis and non-Japanese words + in a phonetical manner. + \value Hangul is a Korean script consisting of alphabetic components. + \value Bopomofo is a phonetic alphabet for Chinese (mainly Mandarin). + \value Yi (also called Cuan or Wei) is a syllabary used to write + the Yi language of Southwestern China, Myanmar, Laos, and Vietnam. + + Additional scripts that do not fit well into the script categories above: + + \value Ethiopic is a syllabary used by several Central East African languages. + \value Cherokee is a left-to-right syllabic script used to write + the Cherokee language. + \value CanadianAboriginal consists of the syllabics used by some + Canadian aboriginal societies. + \value Mongolian is the traditional (and recently reintroduced) + script used to write Mongolian. + + Symbols: + + \value CurrencySymbols contains currency symbols not encoded in other scripts. + \value LetterlikeSymbols consists of symbols derived from + ordinary letters of an alphabetical script. + \value NumberForms are provided for compatibility with other + existing character sets. + \value MathematicalOperators consists of encodings for operators, + relations and other symbols like arrows used in a mathematical context. + \value TechnicalSymbols contains representations for control + codes, the space symbol, APL symbols and other symbols + mainly used in the context of electronic data processing. + \value GeometricSymbols covers block elements and geometric shapes. + \value MiscellaneousSymbols consists of a heterogeneous collection + of symbols that do not fit any other Unicode character + block, e.g. Dingbats. + \value EnclosedAndSquare is provided for compatibility with some + East Asian standards. + \value Braille is an international writing system used by blind + people. This script encodes the 256 eight-dot patterns with + the 64 six-dot patterns as a subset. + + \value Tagalog + \value Hanunoo + \value Buhid + \value Tagbanwa + + \value KatakanaHalfWidth + + \value Limbu (Unicode 4.0) + \value TaiLe (Unicode 4.0) + + \value Unicode includes all the above scripts. +*/ + +/*! \internal + + Constructs a font for use on the paint device \a pd using the + specified font \a data. +*/ +QFont::QFont( QFontPrivate *data, QPaintDevice *pd ) +{ + d = new QFontPrivate( *data ); + Q_CHECK_PTR( d ); + d->paintdevice = pd; + + // now a single reference + d->count = 1; +} + +/*! \internal + Detaches the font object from common font data. +*/ +void QFont::detach() +{ + if (d->count == 1) { + if ( d->engineData ) + d->engineData->deref(); + d->engineData = 0; + + return; + } + + QFontPrivate *old_d = d; + d = new QFontPrivate( *old_d ); + + /* + if this font is a copy of the application default font, set the + fontdef mask to zero to indicate that *nothing* has been + explicitly set by the programmer. + */ + const QFont appfont = QApplication::font(); + if ( old_d == appfont.d ) + d->mask = 0; + + if ( old_d->deref() ) + delete old_d; +} + +/*! + Constructs a font object that uses the application's default font. + + \sa QApplication::setFont(), QApplication::font() +*/ +QFont::QFont() +{ + const QFont appfont = QApplication::font(); + d = appfont.d; + d->ref(); +} + +/*! + Constructs a font object with the specified \a family, \a + pointSize, \a weight and \a italic settings. + + If \a pointSize is <= 0 it is set to 1. + + The \a family name may optionally also include a foundry name, + e.g. "Helvetica [Cronyx]". (The Qt 2.x syntax, i.e. + "Cronyx-Helvetica", is also supported.) If the \a family is + available from more than one foundry and the foundry isn't + specified, an arbitrary foundry is chosen. If the family isn't + available a family will be set using the \link #fontmatching font + matching\endlink algorithm. + + \sa Weight, setFamily(), setPointSize(), setWeight(), setItalic(), + setStyleHint() QApplication::font() +*/ +QFont::QFont( const QString &family, int pointSize, int weight, bool italic ) +{ + + d = new QFontPrivate; + Q_CHECK_PTR( d ); + + d->mask = QFontPrivate::Family; + + if (pointSize <= 0) { + pointSize = 12; + } else { + d->mask |= QFontPrivate::Size; + } + + if (weight < 0) { + weight = Normal; + } else { + d->mask |= QFontPrivate::Weight | QFontPrivate::Italic; + } + + d->request.family = family; + d->request.pointSize = pointSize * 10; + d->request.pixelSize = -1; + d->request.weight = weight; + d->request.italic = italic; +} + +/*! + Constructs a font that is a copy of \a font. +*/ +QFont::QFont( const QFont &font ) +{ + d = font.d; + d->ref(); +} + +/*! + Destroys the font object and frees all allocated resources. +*/ +QFont::~QFont() +{ + if ( d->deref() ) + delete d; + d = 0; +} + +/*! + Assigns \a font to this font and returns a reference to it. +*/ +QFont &QFont::operator=( const QFont &font ) +{ + if ( font.d != d ) { + if ( d->deref() ) + delete d; + d = font.d; + d->ref(); + } + + return *this; +} + +/*! + Returns the requested font family name, i.e. the name set in the + constructor or the last setFont() call. + + \sa setFamily() substitutes() substitute() +*/ +QString QFont::family() const +{ + return d->request.family; +} + +/*! + Sets the family name of the font. The name is case insensitive and + may include a foundry name. + + The \a family name may optionally also include a foundry name, + e.g. "Helvetica [Cronyx]". (The Qt 2.x syntax, i.e. + "Cronyx-Helvetica", is also supported.) If the \a family is + available from more than one foundry and the foundry isn't + specified, an arbitrary foundry is chosen. If the family isn't + available a family will be set using the \link #fontmatching font + matching\endlink algorithm. + + \sa family(), setStyleHint(), QFontInfo +*/ +void QFont::setFamily( const QString &family ) +{ + detach(); + + d->request.family = family; +#if defined(Q_WS_X11) + d->request.addStyle = QString::null; +#endif // Q_WS_X11 + + d->mask |= QFontPrivate::Family; +} + +/*! + Returns the point size in 1/10ths of a point. + + The returned value will be -1 if the font size has been specified + in pixels. + + \sa pointSize() pointSizeFloat() + */ +int QFont::deciPointSize() const +{ + return d->request.pointSize; +} + +/*! + Returns the point size of the font. Returns -1 if the font size + was specified in pixels. + + \sa setPointSize() deciPointSize() pointSizeFloat() +*/ +int QFont::pointSize() const +{ + return d->request.pointSize == -1 ? -1 : (d->request.pointSize + 5) / 10; +} + +/*! + Sets the point size to \a pointSize. The point size must be + greater than zero. + + \sa pointSize() setPointSizeFloat() +*/ +void QFont::setPointSize( int pointSize ) +{ + if ( pointSize <= 0 ) { + +#if defined(QT_CHECK_RANGE) + qWarning( "QFont::setPointSize: Point size <= 0 (%d)", pointSize ); +#endif + + return; + } + + detach(); + + d->request.pointSize = pointSize * 10; + d->request.pixelSize = -1; + + d->mask |= QFontPrivate::Size; +} + +/*! + Sets the point size to \a pointSize. The point size must be + greater than zero. The requested precision may not be achieved on + all platforms. + + \sa pointSizeFloat() setPointSize() setPixelSize() +*/ +void QFont::setPointSizeFloat( float pointSize ) +{ + if ( pointSize <= 0.0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QFont::setPointSize: Point size <= 0 (%f)", pointSize ); +#endif + return; + } + + detach(); + + d->request.pointSize = qRound(pointSize * 10.0); + d->request.pixelSize = -1; + + d->mask |= QFontPrivate::Size; +} + +/*! + Returns the point size of the font. Returns -1 if the font size was + specified in pixels. + + \sa pointSize() setPointSizeFloat() pixelSize() QFontInfo::pointSize() QFontInfo::pixelSize() +*/ +float QFont::pointSizeFloat() const +{ + return float( d->request.pointSize == -1 ? -10 : d->request.pointSize ) / 10.0; +} + +/*! + Sets the font size to \a pixelSize pixels. + + Using this function makes the font device dependent. Use + setPointSize() or setPointSizeFloat() to set the size of the font + in a device independent manner. + + \sa pixelSize() +*/ +void QFont::setPixelSize( int pixelSize ) +{ + if ( pixelSize <= 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QFont::setPixelSize: Pixel size <= 0 (%d)", pixelSize ); +#endif + return; + } + + detach(); + + d->request.pixelSize = pixelSize; + d->request.pointSize = -1; + + d->mask |= QFontPrivate::Size; +} + +/*! + Returns the pixel size of the font if it was set with + setPixelSize(). Returns -1 if the size was set with setPointSize() + or setPointSizeFloat(). + + \sa setPixelSize() pointSize() QFontInfo::pointSize() QFontInfo::pixelSize() +*/ +int QFont::pixelSize() const +{ + return d->request.pixelSize; +} + +/*! \obsolete + + Sets the logical pixel height of font characters when shown on + the screen to \a pixelSize. +*/ +void QFont::setPixelSizeFloat( float pixelSize ) +{ + setPixelSize( (int)pixelSize ); +} + +/*! + Returns TRUE if italic has been set; otherwise returns FALSE. + + \sa setItalic() +*/ +bool QFont::italic() const +{ + return d->request.italic; +} + +/*! + If \a enable is TRUE, italic is set on; otherwise italic is set + off. + + \sa italic(), QFontInfo +*/ +void QFont::setItalic( bool enable ) +{ + detach(); + + d->request.italic = enable; + d->mask |= QFontPrivate::Italic; +} + +/*! + Returns the weight of the font which is one of the enumerated + values from \l{QFont::Weight}. + + \sa setWeight(), Weight, QFontInfo +*/ +int QFont::weight() const +{ + return d->request.weight; +} + +/*! + \enum QFont::Weight + + Qt uses a weighting scale from 0 to 99 similar to, but not the + same as, the scales used in Windows or CSS. A weight of 0 is + ultralight, whilst 99 will be an extremely black. + + This enum contains the predefined font weights: + + \value Light 25 + \value Normal 50 + \value DemiBold 63 + \value Bold 75 + \value Black 87 +*/ + +/*! + Sets the weight the font to \a weight, which should be a value + from the \l QFont::Weight enumeration. + + \sa weight(), QFontInfo +*/ +void QFont::setWeight( int weight ) +{ + if ( weight < 0 || weight > 99 ) { + +#if defined(QT_CHECK_RANGE) + qWarning( "QFont::setWeight: Value out of range (%d)", weight ); +#endif + + return; + } + + detach(); + + d->request.weight = weight; + d->mask |= QFontPrivate::Weight; +} + +/*! + \fn bool QFont::bold() const + + Returns TRUE if weight() is a value greater than \link Weight + QFont::Normal \endlink; otherwise returns FALSE. + + \sa weight(), setBold(), QFontInfo::bold() +*/ + +/*! + \fn void QFont::setBold( bool enable ) + + If \a enable is true sets the font's weight to \link Weight + QFont::Bold \endlink; otherwise sets the weight to \link Weight + QFont::Normal\endlink. + + For finer boldness control use setWeight(). + + \sa bold(), setWeight() +*/ + +/*! + Returns TRUE if underline has been set; otherwise returns FALSE. + + \sa setUnderline() +*/ +bool QFont::underline() const +{ + return d->underline; +} + +/*! + If \a enable is TRUE, sets underline on; otherwise sets underline + off. + + \sa underline(), QFontInfo +*/ +void QFont::setUnderline( bool enable ) +{ + detach(); + + d->underline = enable; + d->mask |= QFontPrivate::Underline; +} + +/*! + Returns TRUE if overline has been set; otherwise returns FALSE. + + \sa setOverline() +*/ +bool QFont::overline() const +{ + return d->overline; +} + +/*! + If \a enable is TRUE, sets overline on; otherwise sets overline off. + + \sa overline(), QFontInfo +*/ +void QFont::setOverline( bool enable ) +{ + detach(); + + d->overline = enable; + d->mask |= QFontPrivate::Overline; +} + +/*! + Returns TRUE if strikeout has been set; otherwise returns FALSE. + + \sa setStrikeOut() +*/ +bool QFont::strikeOut() const +{ + return d->strikeOut; +} + +/*! + If \a enable is TRUE, sets strikeout on; otherwise sets strikeout + off. + + \sa strikeOut(), QFontInfo +*/ +void QFont::setStrikeOut( bool enable ) +{ + detach(); + + d->strikeOut = enable; + d->mask |= QFontPrivate::StrikeOut; +} + +/*! + Returns TRUE if fixed pitch has been set; otherwise returns FALSE. + + \sa setFixedPitch(), QFontInfo::fixedPitch() +*/ +bool QFont::fixedPitch() const +{ + return d->request.fixedPitch; +} + +/*! + If \a enable is TRUE, sets fixed pitch on; otherwise sets fixed + pitch off. + + \sa fixedPitch(), QFontInfo +*/ +void QFont::setFixedPitch( bool enable ) +{ + detach(); + + d->request.fixedPitch = enable; + d->request.ignorePitch = FALSE; + d->mask |= QFontPrivate::FixedPitch; +} + +/*! + Returns the StyleStrategy. + + The style strategy affects the \link #fontmatching font + matching\endlink algorithm. See \l QFont::StyleStrategy for the + list of strategies. + + \sa setStyleHint() QFont::StyleHint +*/ +QFont::StyleStrategy QFont::styleStrategy() const +{ + return (StyleStrategy) d->request.styleStrategy; +} + +/*! + Returns the StyleHint. + + The style hint affects the \link #fontmatching font + matching\endlink algorithm. See \l QFont::StyleHint for the list + of strategies. + + \sa setStyleHint(), QFont::StyleStrategy QFontInfo::styleHint() +*/ +QFont::StyleHint QFont::styleHint() const +{ + return (StyleHint) d->request.styleHint; +} + +/*! + \enum QFont::StyleHint + + Style hints are used by the \link #fontmatching font + matching\endlink algorithm to find an appropriate default family + if a selected font family is not available. + + \value AnyStyle leaves the font matching algorithm to choose the + family. This is the default. + + \value SansSerif the font matcher prefer sans serif fonts. + \value Helvetica is a synonym for \c SansSerif. + + \value Serif the font matcher prefers serif fonts. + \value Times is a synonym for \c Serif. + + \value TypeWriter the font matcher prefers fixed pitch fonts. + \value Courier a synonym for \c TypeWriter. + + \value OldEnglish the font matcher prefers decorative fonts. + \value Decorative is a synonym for \c OldEnglish. + + \value System the font matcher prefers system fonts. +*/ + +/*! + \enum QFont::StyleStrategy + + The style strategy tells the \link #fontmatching font + matching\endlink algorithm what type of fonts should be used to + find an appropriate default family. + + The following strategies are available: + + \value PreferDefault the default style strategy. It does not prefer + any type of font. + \value PreferBitmap prefers bitmap fonts (as opposed to outline + fonts). + \value PreferDevice prefers device fonts. + \value PreferOutline prefers outline fonts (as opposed to bitmap fonts). + \value ForceOutline forces the use of outline fonts. + \value NoAntialias don't antialias the fonts. + \value PreferAntialias antialias if possible. + \value OpenGLCompatible forces the use of OpenGL compatible + fonts. + + Any of these may be OR-ed with one of these flags: + + \value PreferMatch prefer an exact match. The font matcher will try to + use the exact font size that has been specified. + \value PreferQuality prefer the best quality font. The font matcher + will use the nearest standard point size that the font + supports. +*/ + +/*! + Sets the style hint and strategy to \a hint and \a strategy, + respectively. + + If these aren't set explicitly the style hint will default to + \c AnyStyle and the style strategy to \c PreferDefault. + + Qt does not support style hints on X11 since this information + is not provided by the window system. + + \sa StyleHint, styleHint(), StyleStrategy, styleStrategy(), QFontInfo +*/ +void QFont::setStyleHint( StyleHint hint, StyleStrategy strategy ) +{ + detach(); + + if ( ( d->mask & ( QFontPrivate::StyleHint | QFontPrivate::StyleStrategy ) ) && + (StyleHint) d->request.styleHint == hint && + (StyleStrategy) d->request.styleStrategy == strategy ) + return; + + d->request.styleHint = hint; + d->request.styleStrategy = strategy; + d->mask |= QFontPrivate::StyleHint; + d->mask |= QFontPrivate::StyleStrategy; + +#if defined(Q_WS_X11) + d->request.addStyle = QString::null; +#endif // Q_WS_X11 +} + +/*! + Sets the style strategy for the font to \a s. + + \sa QFont::StyleStrategy +*/ +void QFont::setStyleStrategy( StyleStrategy s ) +{ + detach(); + + if ( ( d->mask & QFontPrivate::StyleStrategy ) && + s == (StyleStrategy)d->request.styleStrategy ) + return; + + d->request.styleStrategy = s; + d->mask |= QFontPrivate::StyleStrategy; +} + + +/*! + \enum QFont::Stretch + + Predefined stretch values that follow the CSS naming convention. + + \value UltraCondensed 50 + \value ExtraCondensed 62 + \value Condensed 75 + \value SemiCondensed 87 + \value Unstretched 100 + \value SemiExpanded 112 + \value Expanded 125 + \value ExtraExpanded 150 + \value UltraExpanded 200 + + \sa setStretch() stretch() +*/ + +/*! + Returns the stretch factor for the font. + + \sa setStretch() + */ +int QFont::stretch() const +{ + return d->request.stretch; +} + +/*! + Sets the stretch factor for the font. + + The stretch factor changes the width of all characters in the font + by \a factor percent. For example, setting \a factor to 150 + results in all characters in the font being 1.5 times ( ie. 150% ) + wider. The default stretch factor is 100. The minimum stretch + factor is 1, and the maximum stretch factor is 4000. + + The stretch factor is only applied to outline fonts. The stretch + factor is ignored for bitmap fonts. + + NOTE: QFont cannot stretch XLFD fonts. When loading XLFD fonts on + X11, the stretch factor is matched against a predefined set of + values for the SETWIDTH_NAME field of the XLFD. + + \sa stretch() QFont::StyleStrategy +*/ +void QFont::setStretch( int factor ) +{ + if ( factor < 1 || factor > 4000 ) { +#ifdef QT_CHECK_RANGE + qWarning( "QFont::setStretch(): parameter '%d' out of range", factor ); +#endif // QT_CHECK_RANGE + + return; + } + + detach(); + + if ( ( d->mask & QFontPrivate::Stretch ) && + d->request.stretch == (uint)factor ) + return; + + d->request.stretch = (uint)factor; + d->mask |= QFontPrivate::Stretch; +} + +/*! + If \a enable is TRUE, turns raw mode on; otherwise turns raw mode + off. This function only has an effect under X11. + + If raw mode is enabled, Qt will search for an X font with a + complete font name matching the family name, ignoring all other + values set for the QFont. If the font name matches several fonts, + Qt will use the first font returned by X. QFontInfo \e cannot be + used to fetch information about a QFont using raw mode (it will + return the values set in the QFont for all parameters, including + the family name). + + \warning Do not use raw mode unless you really, really need it! In + most (if not all) cases, setRawName() is a much better choice. + + \sa rawMode(), setRawName() +*/ +void QFont::setRawMode( bool enable ) +{ + detach(); + + if ( (bool) d->rawMode == enable ) return; + + d->rawMode = enable; +} + +/*! + Returns TRUE if a window system font exactly matching the settings + of this font is available. + + \sa QFontInfo +*/ +bool QFont::exactMatch() const +{ + QFontEngine *engine = d->engineForScript( QFont::NoScript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return d->rawMode ? engine->type() != QFontEngine::Box + : d->request == engine->fontDef; +} + +/*! + Returns TRUE if this font is equal to \a f; otherwise returns + FALSE. + + Two QFonts are considered equal if their font attributes are + equal. If rawMode() is enabled for both fonts, only the family + fields are compared. + + \sa operator!=() isCopyOf() +*/ +bool QFont::operator==( const QFont &f ) const +{ + return f.d == d || ( f.d->request == d->request && + f.d->underline == d->underline && + f.d->overline == d->overline && + f.d->strikeOut == d->strikeOut ); +} + +/*! + Returns TRUE if this font is different from \a f; otherwise + returns FALSE. + + Two QFonts are considered to be different if their font attributes + are different. If rawMode() is enabled for both fonts, only the + family fields are compared. + + \sa operator==() +*/ +bool QFont::operator!=( const QFont &f ) const +{ + return !(operator==( f )); +} + +/*! + Returns TRUE if this font and \a f are copies of each other, i.e. + one of them was created as a copy of the other and neither has + been modified since. This is much stricter than equality. + + \sa operator=() operator==() +*/ +bool QFont::isCopyOf( const QFont & f ) const +{ + return d == f.d; +} + +/*! + Returns TRUE if raw mode is used for font name matching; otherwise + returns FALSE. + + \sa setRawMode() rawName() +*/ +bool QFont::rawMode() const +{ + return d->rawMode; +} + +/*! + Returns a new QFont that has attributes copied from \a other. +*/ +QFont QFont::resolve( const QFont &other ) const +{ + if ( *this == other && d->mask == other.d->mask ) + return *this; + + QFont font( *this ); + font.detach(); + + /* + if this font is a copy of the application default font, set the + fontdef mask to zero to indicate that *nothing* has been + explicitly set by the programmer. + */ + const QFont appfont = QApplication::font(); + if ( d == appfont.d ) + font.d->mask = 0; + + font.d->resolve( other.d ); + + return font; +} + +#ifndef QT_NO_COMPAT + +/*! \obsolete + + Please use QApplication::font() instead. +*/ +QFont QFont::defaultFont() +{ + return QApplication::font(); +} + +/*! \obsolete + + Please use QApplication::setFont() instead. +*/ +void QFont::setDefaultFont( const QFont &f ) +{ + QApplication::setFont( f ); +} + + +#endif + + + + +#ifndef QT_NO_STRINGLIST + +/***************************************************************************** + QFont substitution management + *****************************************************************************/ + +typedef QDict<QStringList> QFontSubst; +static QFontSubst *fontSubst = 0; +static QSingleCleanupHandler<QFontSubst> qfont_cleanup_fontsubst; + + +// create substitution dict +static void initFontSubst() +{ + // default substitutions + static const char *initTbl[] = { + +#if defined(Q_WS_X11) + "arial", "helvetica", + "helv", "helvetica", + "tms rmn", "times", +#elif defined(Q_WS_WIN) + "times", "Times New Roman", + "courier", "Courier New", + "helvetica", "Arial", +#endif + + 0, 0 + }; + + if (fontSubst) + return; + + fontSubst = new QFontSubst(17, FALSE); + Q_CHECK_PTR( fontSubst ); + fontSubst->setAutoDelete( TRUE ); + qfont_cleanup_fontsubst.set(&fontSubst); + + for ( int i=0; initTbl[i] != 0; i += 2 ) + QFont::insertSubstitution(QString::fromLatin1(initTbl[i]), + QString::fromLatin1(initTbl[i+1])); +} + + +/*! + Returns the first family name to be used whenever \a familyName is + specified. The lookup is case insensitive. + + If there is no substitution for \a familyName, \a familyName is + returned. + + To obtain a list of substitutions use substitutes(). + + \sa setFamily() insertSubstitutions() insertSubstitution() removeSubstitution() +*/ +QString QFont::substitute( const QString &familyName ) +{ + initFontSubst(); + + QStringList *list = fontSubst->find(familyName); + if (list && list->count() > 0) + return *(list->at(0)); + + return familyName; +} + + +/*! + Returns a list of family names to be used whenever \a familyName + is specified. The lookup is case insensitive. + + If there is no substitution for \a familyName, an empty list is + returned. + + \sa substitute() insertSubstitutions() insertSubstitution() removeSubstitution() + */ +QStringList QFont::substitutes(const QString &familyName) +{ + initFontSubst(); + + QStringList ret, *list = fontSubst->find(familyName); + if (list) + ret += *list; + return ret; +} + + +/*! + Inserts the family name \a substituteName into the substitution + table for \a familyName. + + \sa insertSubstitutions() removeSubstitution() substitutions() substitute() substitutes() +*/ +void QFont::insertSubstitution(const QString &familyName, + const QString &substituteName) +{ + initFontSubst(); + + QStringList *list = fontSubst->find(familyName); + if (! list) { + list = new QStringList; + fontSubst->insert(familyName, list); + } + + if (! list->contains(substituteName)) + list->append(substituteName); +} + + +/*! + Inserts the list of families \a substituteNames into the + substitution list for \a familyName. + + \sa insertSubstitution(), removeSubstitution(), substitutions(), substitute() +*/ +void QFont::insertSubstitutions(const QString &familyName, + const QStringList &substituteNames) +{ + initFontSubst(); + + QStringList *list = fontSubst->find(familyName); + if (! list) { + list = new QStringList; + fontSubst->insert(familyName, list); + } + + QStringList::ConstIterator it = substituteNames.begin(); + while (it != substituteNames.end()) { + if (! list->contains(*it)) + list->append(*it); + it++; + } +} + +// ### mark: should be called removeSubstitutions() +/*! + Removes all the substitutions for \a familyName. + + \sa insertSubstitutions(), insertSubstitution(), substitutions(), substitute() +*/ +void QFont::removeSubstitution( const QString &familyName ) +{ // ### function name should be removeSubstitutions() or + // ### removeSubstitutionList() + initFontSubst(); + + fontSubst->remove(familyName); +} + + +/*! + Returns a sorted list of substituted family names. + + \sa insertSubstitution(), removeSubstitution(), substitute() +*/ +QStringList QFont::substitutions() +{ + initFontSubst(); + + QStringList ret; + QDictIterator<QStringList> it(*fontSubst); + + while (it.current()) { + ret.append(it.currentKey()); + ++it; + } + + ret.sort(); + + return ret; +} + +#endif // QT_NO_STRINGLIST + + +/* \internal + Internal function. Converts boolean font settings to an unsigned + 8-bit number. Used for serialization etc. +*/ +static Q_UINT8 get_font_bits( const QFontPrivate *f ) +{ +#ifdef QT_CHECK_STATE + Q_ASSERT( f != 0 ); +#endif + + Q_UINT8 bits = 0; + if ( f->request.italic ) + bits |= 0x01; + if ( f->underline ) + bits |= 0x02; + if ( f->overline ) + bits |= 0x40; + if ( f->strikeOut ) + bits |= 0x04; + if ( f->request.fixedPitch ) + bits |= 0x08; + // if ( f.hintSetByUser ) + // bits |= 0x10; + if ( f->rawMode ) + bits |= 0x20; + return bits; +} + + +#ifndef QT_NO_DATASTREAM + +/* \internal + Internal function. Sets boolean font settings from an unsigned + 8-bit number. Used for serialization etc. +*/ +static void set_font_bits( Q_UINT8 bits, QFontPrivate *f ) +{ +#ifdef QT_CHECK_STATE + Q_ASSERT( f != 0 ); +#endif + + f->request.italic = (bits & 0x01) != 0; + f->underline = (bits & 0x02) != 0; + f->overline = (bits & 0x40) != 0; + f->strikeOut = (bits & 0x04) != 0; + f->request.fixedPitch = (bits & 0x08) != 0; + // f->hintSetByUser = (bits & 0x10) != 0; + f->rawMode = (bits & 0x20) != 0; +} + +#endif + + +/*! + Returns the font's key, a textual representation of a font. It is + typically used as the key for a cache or dictionary of fonts. + + \sa QMap +*/ +QString QFont::key() const +{ + return toString(); +} + +/*! + Returns a description of the font. The description is a + comma-separated list of the attributes, perfectly suited for use + in QSettings. + + \sa fromString() operator<<() + */ +QString QFont::toString() const +{ + const QChar comma( ',' ); + return family() + comma + + QString::number( pointSizeFloat() ) + comma + + QString::number( pixelSize() ) + comma + + QString::number( (int) styleHint() ) + comma + + QString::number( weight() ) + comma + + QString::number( (int) italic() ) + comma + + QString::number( (int) underline() ) + comma + + QString::number( (int) strikeOut() ) + comma + + QString::number( (int)fixedPitch() ) + comma + + QString::number( (int) rawMode() ); +} + + +/*! + Sets this font to match the description \a descrip. The description + is a comma-separated list of the font attributes, as returned by + toString(). + + \sa toString() operator>>() + */ +bool QFont::fromString(const QString &descrip) +{ +#ifndef QT_NO_STRINGLIST + QStringList l(QStringList::split(',', descrip)); + + int count = (int)l.count(); +#else + int count = 0; + QString l[11]; + int from = 0; + int to = descrip.find( ',' ); + while ( to > 0 && count < 11 ) { + l[count] = descrip.mid( from, to-from ); + count++; + from = to+1; + to = descrip.find( ',', from ); + } +#endif // QT_NO_STRINGLIST + if ( !count || ( count > 2 && count < 9 ) || count > 11 ) { + +#ifdef QT_CHECK_STATE + qWarning("QFont::fromString: invalid description '%s'", + descrip.isEmpty() ? "(empty)" : descrip.latin1()); +#endif + + return FALSE; + } + + setFamily(l[0]); + if ( count > 1 && l[1].toDouble() > 0.0 ) + setPointSizeFloat(l[1].toDouble()); + if ( count == 9 ) { + setStyleHint((StyleHint) l[2].toInt()); + setWeight(l[3].toInt()); + setItalic(l[4].toInt()); + setUnderline(l[5].toInt()); + setStrikeOut(l[6].toInt()); + setFixedPitch(l[7].toInt()); + setRawMode(l[8].toInt()); + } else if ( count == 10 ) { + if ( l[2].toInt() > 0 ) + setPixelSize( l[2].toInt() ); + setStyleHint((StyleHint) l[3].toInt()); + setWeight(l[4].toInt()); + setItalic(l[5].toInt()); + setUnderline(l[6].toInt()); + setStrikeOut(l[7].toInt()); + setFixedPitch(l[8].toInt()); + setRawMode(l[9].toInt()); + } + + return TRUE; +} + +#if !defined( Q_WS_QWS ) +/*! \internal + + Internal function that dumps font cache statistics. +*/ +void QFont::cacheStatistics() +{ + + +} +#endif // !Q_WS_QWS + + + +/***************************************************************************** + QFont stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM + +/*! + \relates QFont + + Writes the font \a font to the data stream \a s. (toString() + writes to a text stream.) + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ +QDataStream &operator<<( QDataStream &s, const QFont &font ) +{ + if ( s.version() == 1 ) { + QCString fam( font.d->request.family.latin1() ); + s << fam; + } else { + s << font.d->request.family; + } + + if ( s.version() <= 3 ) { + Q_INT16 pointSize = (Q_INT16) font.d->request.pointSize; + if ( pointSize == -1 ) { +#ifdef Q_WS_X11 + pointSize = (Q_INT16)(font.d->request.pixelSize*720/QPaintDevice::x11AppDpiY()); +#else + pointSize = (Q_INT16)QFontInfo( font ).pointSize() * 10; +#endif + } + s << pointSize; + } else { + s << (Q_INT16) font.d->request.pointSize; + s << (Q_INT16) font.d->request.pixelSize; + } + + s << (Q_UINT8) font.d->request.styleHint; + if ( s.version() >= 5 ) + s << (Q_UINT8 ) font.d->request.styleStrategy; + return s << (Q_UINT8) 0 + << (Q_UINT8) font.d->request.weight + << get_font_bits(font.d); +} + + +/*! + \relates QFont + + Reads the font \a font from the data stream \a s. (fromString() + reads from a text stream.) + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ +QDataStream &operator>>( QDataStream &s, QFont &font ) +{ + if (font.d->deref()) delete font.d; + + font.d = new QFontPrivate; + font.d->mask = QFontPrivate::Complete; + + Q_INT16 pointSize, pixelSize = -1; + Q_UINT8 styleHint, styleStrategy = QFont::PreferDefault, charSet, weight, bits; + + if ( s.version() == 1 ) { + QCString fam; + s >> fam; + font.d->request.family = QString( fam ); + } else { + s >> font.d->request.family; + } + + s >> pointSize; + if ( s.version() >= 4 ) + s >> pixelSize; + s >> styleHint; + if ( s.version() >= 5 ) + s >> styleStrategy; + s >> charSet; + s >> weight; + s >> bits; + + font.d->request.pointSize = pointSize; + font.d->request.pixelSize = pixelSize; + font.d->request.styleHint = styleHint; + font.d->request.styleStrategy = styleStrategy; + font.d->request.weight = weight; + + set_font_bits( bits, font.d ); + + return s; +} + +#endif // QT_NO_DATASTREAM + + + + +/***************************************************************************** + QFontMetrics member functions + *****************************************************************************/ + +/*! + \class QFontMetrics qfontmetrics.h + \brief The QFontMetrics class provides font metrics information. + + \ingroup graphics + \ingroup shared + + QFontMetrics functions calculate the size of characters and + strings for a given font. There are three ways you can create a + QFontMetrics object: + + \list 1 + \i Calling the QFontMetrics constructor with a QFont creates a + font metrics object for a screen-compatible font, i.e. the font + cannot be a printer font<sup>*</sup>. If the font is changed + later, the font metrics object is \e not updated. + + \i QWidget::fontMetrics() returns the font metrics for a widget's + font. This is equivalent to QFontMetrics(widget->font()). If the + widget's font is changed later, the font metrics object is \e not + updated. + + \i QPainter::fontMetrics() returns the font metrics for a + painter's current font. If the painter's font is changed later, the + font metrics object is \e not updated. + \endlist + + <sup>*</sup> If you use a printer font the values returned may be + inaccurate. Printer fonts are not always accessible so the nearest + screen font is used if a printer font is supplied. + + Once created, the object provides functions to access the + individual metrics of the font, its characters, and for strings + rendered in the font. + + There are several functions that operate on the font: ascent(), + descent(), height(), leading() and lineSpacing() return the basic + size properties of the font. The underlinePos(), overlinePos(), + strikeOutPos() and lineWidth() functions, return the properties of + the line that underlines, overlines or strikes out the + characters. These functions are all fast. + + There are also some functions that operate on the set of glyphs in + the font: minLeftBearing(), minRightBearing() and maxWidth(). + These are by necessity slow, and we recommend avoiding them if + possible. + + For each character, you can get its width(), leftBearing() and + rightBearing() and find out whether it is in the font using + inFont(). You can also treat the character as a string, and use + the string functions on it. + + The string functions include width(), to return the width of a + string in pixels (or points, for a printer), boundingRect(), to + return a rectangle large enough to contain the rendered string, + and size(), to return the size of that rectangle. + + Example: + \code + QFont font( "times", 24 ); + QFontMetrics fm( font ); + int pixelsWide = fm.width( "What's the width of this text?" ); + int pixelsHigh = fm.height(); + \endcode + + \sa QFont QFontInfo QFontDatabase +*/ + +/*! + Constructs a font metrics object for \a font. + + The font must be screen-compatible, i.e. a font you use when + drawing text in \link QWidget widgets\endlink or \link QPixmap + pixmaps\endlink, not QPicture or QPrinter. + + The font metrics object holds the information for the font that is + passed in the constructor at the time it is created, and is not + updated if the font's attributes are changed later. + + Use QPainter::fontMetrics() to get the font metrics when painting. + This will give correct results also when painting on paint device + that is not screen-compatible. +*/ +QFontMetrics::QFontMetrics( const QFont &font ) + : d( font.d ), painter( 0 ), fscript( QFont::NoScript ) +{ + d->ref(); +} + +/*! + \overload + + Constructs a font metrics object for \a font using the given \a + script. +*/ +QFontMetrics::QFontMetrics( const QFont &font, QFont::Script script ) + : d( font.d ), painter( 0 ), fscript( script ) +{ + d->ref(); +} + +/*! \internal + + Constructs a font metrics object for the painter's font \a p. +*/ +QFontMetrics::QFontMetrics( const QPainter *p ) + : painter ( (QPainter *) p ), fscript( QFont::NoScript ) +{ +#if defined(CHECK_STATE) + if ( !painter->isActive() ) + qWarning( "QFontMetrics: Get font metrics between QPainter::begin() " + "and QPainter::end()" ); +#endif + + if ( painter->testf(QPainter::DirtyFont) ) + painter->updateFont(); + + d = painter->pfont ? painter->pfont->d : painter->cfont.d; + +#if defined(Q_WS_X11) + if ( d->screen != p->scrn ) { + QFontPrivate *new_d = new QFontPrivate( *d ); + Q_CHECK_PTR( new_d ); + d = new_d; + d->screen = p->scrn; + d->count = 1; + } else +#endif // Q_WS_X11 + d->ref(); +} + +/*! + Constructs a copy of \a fm. +*/ +QFontMetrics::QFontMetrics( const QFontMetrics &fm ) + : d( fm.d ), painter( 0 ), fscript( fm.fscript ) +{ + d->ref(); +} + +/*! + Destroys the font metrics object and frees all allocated + resources. +*/ +QFontMetrics::~QFontMetrics() +{ + if ( d->deref() ) + delete d; +} + +/*! + Assigns the font metrics \a fm. +*/ +QFontMetrics &QFontMetrics::operator=( const QFontMetrics &fm ) +{ + if ( d != fm.d ) { + if ( d->deref() ) + delete d; + d = fm.d; + d->ref(); + } + painter = fm.painter; + return *this; +} + +/*! + Returns the ascent of the font. + + The ascent of a font is the distance from the baseline to the + highest position characters extend to. In practice, some font + designers break this rule, e.g. when they put more than one accent + on top of a character, or to accommodate an unusual character in + an exotic language, so it is possible (though rare) that this + value will be too small. + + \sa descent() +*/ +int QFontMetrics::ascent() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return QMAX(engine->ascent(), latin_engine->ascent()); +} + + +/*! + Returns the descent of the font. + + The descent is the distance from the base line to the lowest point + characters extend to. (Note that this is different from X, which + adds 1 pixel.) In practice, some font designers break this rule, + e.g. to accommodate an unusual character in an exotic language, so + it is possible (though rare) that this value will be too small. + + \sa ascent() +*/ +int QFontMetrics::descent() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return QMAX(engine->descent(), latin_engine->descent()); +} + +/*! + Returns the height of the font. + + This is always equal to ascent()+descent()+1 (the 1 is for the + base line). + + \sa leading(), lineSpacing() +*/ +int QFontMetrics::height() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return (QMAX(engine->ascent(), latin_engine->ascent()) + + QMAX(engine->descent(), latin_engine->descent()) + 1); +} + +/*! + Returns the leading of the font. + + This is the natural inter-line spacing. + + \sa height(), lineSpacing() +*/ +int QFontMetrics::leading() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return QMAX(engine->leading(), latin_engine->leading()); +} + +/*! + Returns the distance from one base line to the next. + + This value is always equal to leading()+height(). + + \sa height(), leading() +*/ +int QFontMetrics::lineSpacing() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return (QMAX(engine->leading(), latin_engine->leading()) + + QMAX(engine->ascent(), latin_engine->ascent()) + + QMAX(engine->descent(), latin_engine->descent()) + 1); +} + +/*! + Returns the minimum left bearing of the font. + + This is the smallest leftBearing(char) of all characters in the + font. + + Note that this function can be very slow if the font is large. + + \sa minRightBearing(), leftBearing() +*/ +int QFontMetrics::minLeftBearing() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return QMIN(engine->minLeftBearing(), latin_engine->minLeftBearing()); +} + +/*! + Returns the minimum right bearing of the font. + + This is the smallest rightBearing(char) of all characters in the + font. + + Note that this function can be very slow if the font is large. + + \sa minLeftBearing(), rightBearing() +*/ +int QFontMetrics::minRightBearing() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *latin_engine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return QMIN(engine->minRightBearing(), latin_engine->minRightBearing()); +} + +/*! + Returns the width of the widest character in the font. +*/ +int QFontMetrics::maxWidth() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); + QFontEngine *lengine = d->engineForScript( QFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( lengine != 0 ); +#endif // QT_CHECK_STATE + + return QMAX(engine->maxCharWidth(), lengine->maxCharWidth()); +} + +/*! + Returns TRUE if character \a ch is a valid character in the font; + otherwise returns FALSE. +*/ +bool QFontMetrics::inFont(QChar ch) const +{ + QFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + if ( engine->type() == QFontEngine::Box ) return FALSE; + return engine->canRender( &ch, 1 ); +} + +/*! \fn int QFontMetrics::leftBearing( QChar ch ) const + Returns the left bearing of character \a ch in the font. + + The left bearing is the right-ward distance of the left-most pixel + of the character from the logical origin of the character. This + value is negative if the pixels of the character extend to the + left of the logical origin. + + See width(QChar) for a graphical description of this metric. + + \sa rightBearing(), minLeftBearing(), width() +*/ +#if !defined(Q_WS_WIN) && !defined(Q_WS_QWS) +int QFontMetrics::leftBearing(QChar ch) const +{ + QFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + if ( engine->type() == QFontEngine::Box ) return 0; + + glyph_t glyphs[10]; + int nglyphs = 9; + engine->stringToCMap( &ch, 1, glyphs, 0, &nglyphs, FALSE ); + // ### can nglyphs != 1 happen at all? Not currently I think + glyph_metrics_t gi = engine->boundingBox( glyphs[0] ); + return gi.x; +} +#endif // !Q_WS_WIN + +/*! \fn int QFontMetrics::rightBearing(QChar ch) const + Returns the right bearing of character \a ch in the font. + + The right bearing is the left-ward distance of the right-most + pixel of the character from the logical origin of a subsequent + character. This value is negative if the pixels of the character + extend to the right of the width() of the character. + + See width() for a graphical description of this metric. + + \sa leftBearing(), minRightBearing(), width() +*/ +#if !defined(Q_WS_WIN) && !defined(Q_WS_QWS) +int QFontMetrics::rightBearing(QChar ch) const +{ + QFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + if ( engine->type() == QFontEngine::Box ) return 0; + + glyph_t glyphs[10]; + int nglyphs = 9; + engine->stringToCMap( &ch, 1, glyphs, 0, &nglyphs, FALSE ); + // ### can nglyphs != 1 happen at all? Not currently I think + glyph_metrics_t gi = engine->boundingBox( glyphs[0] ); + return gi.xoff - gi.x - gi.width; +} +#endif // !Q_WS_WIN + + +#ifndef Q_WS_QWS +/*! + Returns the width in pixels of the first \a len characters of \a + str. If \a len is negative (the default), the entire string is + used. + + Note that this value is \e not equal to boundingRect().width(); + boundingRect() returns a rectangle describing the pixels this + string will cover whereas width() returns the distance to where + the next string should be drawn. + + \sa boundingRect() +*/ +int QFontMetrics::width( const QString &str, int len ) const +{ + if (len < 0) + len = str.length(); + if (len == 0) + return 0; + + int pos = 0; + int width = 0; +#ifndef Q_WS_MAC + const QChar *ch = str.unicode(); + + while (pos < len) { + unsigned short uc = ch->unicode(); + if (uc < QFontEngineData::widthCacheSize && d->engineData && d->engineData->widthCache[uc]) + width += d->engineData->widthCache[uc]; + else { + QFont::Script script; + SCRIPT_FOR_CHAR( script, *ch ); + + if (script >= QFont::Arabic && script <= QFont::Khmer) + break; + if ( ::category( *ch ) != QChar::Mark_NonSpacing && !qIsZeroWidthChar(ch->unicode())) { + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[8]; + advance_t advances[8]; + int nglyphs = 7; + engine->stringToCMap( ch, 1, glyphs, advances, &nglyphs, FALSE ); + + // ### can nglyphs != 1 happen at all? Not currently I think + if ( uc < QFontEngineData::widthCacheSize && advances[0] > 0 && advances[0] < 0x100 ) + d->engineData->widthCache[ uc ] = advances[0]; + width += advances[0]; + } + } + ++pos; + ++ch; + } + if ( pos < len ) { +#endif + QTextEngine layout( str, d ); + layout.itemize( QTextEngine::WidthOnly ); + width += layout.width( pos, len-pos ); +#ifndef Q_WS_MAC + } +#endif + return width; +} +#endif + +/*! \fn int QFontMetrics::width( QChar ch ) const + + <img src="bearings.png" align=right> + + Returns the logical width of character \a ch in pixels. This is a + distance appropriate for drawing a subsequent character after \a + ch. + + Some of the metrics are described in the image to the right. The + central dark rectangles cover the logical width() of each + character. The outer pale rectangles cover the leftBearing() and + rightBearing() of each character. Notice that the bearings of "f" + in this particular font are both negative, while the bearings of + "o" are both positive. + + \warning This function will produce incorrect results for Arabic + characters or non spacing marks in the middle of a string, as the + glyph shaping and positioning of marks that happens when + processing strings cannot be taken into account. Use charWidth() + instead if you aren't looking for the width of isolated + characters. + + \sa boundingRect(), charWidth() +*/ + +/*! \fn int QFontMetrics::width( char c ) const + + \overload + \obsolete + + Provided to aid porting from Qt 1.x. +*/ + +/*! \fn int QFontMetrics::charWidth( const QString &str, int pos ) const + Returns the width of the character at position \a pos in the + string \a str. + + The whole string is needed, as the glyph drawn may change + depending on the context (the letter before and after the current + one) for some languages (e.g. Arabic). + + This function also takes non spacing marks and ligatures into + account. +*/ + +#ifndef Q_WS_QWS +/*! + Returns the bounding rectangle of the first \a len characters of + \a str, which is the set of pixels the text would cover if drawn + at (0, 0). + + If \a len is negative (the default), the entire string is used. + + Note that the bounding rectangle may extend to the left of (0, 0), + e.g. for italicized fonts, and that the text output may cover \e + all pixels in the bounding rectangle. + + Newline characters are processed as normal characters, \e not as + linebreaks. + + Due to the different actual character heights, the height of the + bounding rectangle of e.g. "Yes" and "yes" may be different. + + \sa width(), QPainter::boundingRect() +*/ +QRect QFontMetrics::boundingRect( const QString &str, int len ) const +{ + if (len < 0) + len = str.length(); + if (len == 0) + return QRect(); + + QTextEngine layout( str, d ); + layout.itemize( QTextEngine::NoBidi|QTextEngine::SingleLine ); + glyph_metrics_t gm = layout.boundingBox( 0, len ); + return QRect( gm.x, gm.y, gm.width, gm.height ); +} +#endif + +/*! + Returns the rectangle that is covered by ink if the character + specified by \a ch were to be drawn at the origin of the coordinate + system. + + Note that the bounding rectangle may extend to the left of (0, 0), + e.g. for italicized fonts, and that the text output may cover \e + all pixels in the bounding rectangle. For a space character the rectangle + will usually be empty. + + Note that the rectangle usually extends both above and below the + base line. + + \warning The width of the returned rectangle is not the advance width + of the character. Use boundingRect(const QString &) or width() instead. + + \sa width() +*/ +QRect QFontMetrics::boundingRect( QChar ch ) const +{ + QFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[10]; + int nglyphs = 9; + engine->stringToCMap( &ch, 1, glyphs, 0, &nglyphs, FALSE ); + glyph_metrics_t gi = engine->boundingBox( glyphs[0] ); + return QRect( gi.x, gi.y, gi.width, gi.height ); +} + +/*! + \overload + + Returns the bounding rectangle of the first \a len characters of + \a str, which is the set of pixels the text would cover if drawn + at (0, 0). The drawing, and hence the bounding rectangle, is + constrained to the rectangle (\a x, \a y, \a w, \a h). + + If \a len is negative (which is the default), the entire string is + used. + + The \a flgs argument is the bitwise OR of the following flags: + \list + \i \c AlignAuto aligns to the left border for all languages except + Arabic and Hebrew where it aligns to the right. + \i \c AlignLeft aligns to the left border. + \i \c AlignRight aligns to the right border. + \i \c AlignJustify produces justified text. + \i \c AlignHCenter aligns horizontally centered. + \i \c AlignTop aligns to the top border. + \i \c AlignBottom aligns to the bottom border. + \i \c AlignVCenter aligns vertically centered + \i \c AlignCenter (== \c{AlignHCenter | AlignVCenter}) + \i \c SingleLine ignores newline characters in the text. + \i \c ExpandTabs expands tabs (see below) + \i \c ShowPrefix interprets "&x" as "<u>x</u>", i.e. underlined. + \i \c WordBreak breaks the text to fit the rectangle. + \endlist + + Horizontal alignment defaults to \c AlignAuto and vertical + alignment defaults to \c AlignTop. + + If several of the horizontal or several of the vertical alignment + flags are set, the resulting alignment is undefined. + + These flags are defined in \c qnamespace.h. + + If \c ExpandTabs is set in \a flgs, then: if \a tabarray is + non-null, it specifies a 0-terminated sequence of pixel-positions + for tabs; otherwise if \a tabstops is non-zero, it is used as the + tab spacing (in pixels). + + Note that the bounding rectangle may extend to the left of (0, 0), + e.g. for italicized fonts, and that the text output may cover \e + all pixels in the bounding rectangle. + + Newline characters are processed as linebreaks. + + Despite the different actual character heights, the heights of the + bounding rectangles of "Yes" and "yes" are the same. + + The bounding rectangle given by this function is somewhat larger + than that calculated by the simpler boundingRect() function. This + function uses the \link minLeftBearing() maximum left \endlink and + \link minRightBearing() right \endlink font bearings as is + necessary for multi-line text to align correctly. Also, + fontHeight() and lineSpacing() are used to calculate the height, + rather than individual character heights. + + The \a intern argument should not be used. + + \sa width(), QPainter::boundingRect(), Qt::AlignmentFlags +*/ +QRect QFontMetrics::boundingRect( int x, int y, int w, int h, int flgs, + const QString& str, int len, int tabstops, + int *tabarray, QTextParag **intern ) const +{ + if ( len < 0 ) + len = str.length(); + + int tabarraylen=0; + if (tabarray) + while (tabarray[tabarraylen]) + tabarraylen++; + + QRect rb; + QRect r(x, y, w, h); + qt_format_text( QFont( d, d->paintdevice ), r, flgs|Qt::DontPrint, str, len, &rb, + tabstops, tabarray, tabarraylen, intern, 0 ); + + return rb; +} + +/*! + Returns the size in pixels of the first \a len characters of \a + str. + + If \a len is negative (the default), the entire string is used. + + The \a flgs argument is the bitwise OR of the following flags: + \list + \i \c SingleLine ignores newline characters. + \i \c ExpandTabs expands tabs (see below) + \i \c ShowPrefix interprets "&x" as "<u>x</u>", i.e. underlined. + \i \c WordBreak breaks the text to fit the rectangle. + \endlist + + These flags are defined in \c qnamespace.h. + + If \c ExpandTabs is set in \a flgs, then: if \a tabarray is + non-null, it specifies a 0-terminated sequence of pixel-positions + for tabs; otherwise if \a tabstops is non-zero, it is used as the + tab spacing (in pixels). + + Newline characters are processed as linebreaks. + + Despite the different actual character heights, the heights of the + bounding rectangles of "Yes" and "yes" are the same. + + The \a intern argument should not be used. + + \sa boundingRect() +*/ +QSize QFontMetrics::size( int flgs, const QString &str, int len, int tabstops, + int *tabarray, QTextParag **intern ) const +{ + return boundingRect(0,0,0,0,flgs,str,len,tabstops,tabarray,intern).size(); +} + +/*! + Returns the distance from the base line to where an underscore + should be drawn. + + \sa overlinePos(), strikeOutPos(), lineWidth() +*/ +int QFontMetrics::underlinePos() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return engine->underlinePosition(); +} + +/*! + Returns the distance from the base line to where an overline + should be drawn. + + \sa underlinePos(), strikeOutPos(), lineWidth() +*/ +int QFontMetrics::overlinePos() const +{ + int pos = ascent() + 1; + return pos > 0 ? pos : 1; +} + +/*! + Returns the distance from the base line to where the strikeout + line should be drawn. + + \sa underlinePos(), overlinePos(), lineWidth() +*/ +int QFontMetrics::strikeOutPos() const +{ + int pos = ascent() / 3; + return pos > 0 ? pos : 1; +} + +/*! + Returns the width of the underline and strikeout lines, adjusted + for the point size of the font. + + \sa underlinePos(), overlinePos(), strikeOutPos() +*/ +int QFontMetrics::lineWidth() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return engine->lineThickness(); +} + + + + +/***************************************************************************** + QFontInfo member functions + *****************************************************************************/ + +/*! + \class QFontInfo qfontinfo.h + + \brief The QFontInfo class provides general information about fonts. + + \ingroup graphics + \ingroup shared + + The QFontInfo class provides the same access functions as QFont, + e.g. family(), pointSize(), italic(), weight(), fixedPitch(), + styleHint() etc. But whilst the QFont access functions return the + values that were set, a QFontInfo object returns the values that + apply to the font that will actually be used to draw the text. + + For example, when the program asks for a 25pt Courier font on a + machine that has a non-scalable 24pt Courier font, QFont will + (normally) use the 24pt Courier for rendering. In this case, + QFont::pointSize() returns 25 and QFontInfo::pointSize() returns + 24. + + There are three ways to create a QFontInfo object. + \list 1 + \i Calling the QFontInfo constructor with a QFont creates a font + info object for a screen-compatible font, i.e. the font cannot be + a printer font<sup>*</sup>. If the font is changed later, the font + info object is \e not updated. + + \i QWidget::fontInfo() returns the font info for a widget's font. + This is equivalent to calling QFontInfo(widget->font()). If the + widget's font is changed later, the font info object is \e not + updated. + + \i QPainter::fontInfo() returns the font info for a painter's + current font. If the painter's font is changed later, the font + info object is \e not updated. + \endlist + + <sup>*</sup> If you use a printer font the values returned may be + inaccurate. Printer fonts are not always accessible so the nearest + screen font is used if a printer font is supplied. + + \sa QFont QFontMetrics QFontDatabase +*/ + +/*! + Constructs a font info object for \a font. + + The font must be screen-compatible, i.e. a font you use when + drawing text in \link QWidget widgets\endlink or \link QPixmap + pixmaps\endlink, not QPicture or QPrinter. + + The font info object holds the information for the font that is + passed in the constructor at the time it is created, and is not + updated if the font's attributes are changed later. + + Use QPainter::fontInfo() to get the font info when painting. + This will give correct results also when painting on paint device + that is not screen-compatible. +*/ +QFontInfo::QFontInfo( const QFont &font ) + : d( font.d ), painter( 0 ), fscript( QFont::NoScript ) +{ + d->ref(); +} + +/*! + Constructs a font info object for \a font using the specified \a + script. +*/ +QFontInfo::QFontInfo( const QFont &font, QFont::Script script ) + : d( font.d ), painter( 0 ), fscript( script ) +{ + d->ref(); +} + +/*! \internal + + Constructs a font info object from the painter's font \a p. +*/ +QFontInfo::QFontInfo( const QPainter *p ) + : painter( 0 ), fscript( QFont::NoScript ) +{ + QPainter *painter = (QPainter *) p; + +#if defined(CHECK_STATE) + if ( !painter->isActive() ) + qWarning( "QFontInfo: Get font info between QPainter::begin() " + "and QPainter::end()" ); +#endif + + painter->setf( QPainter::FontInf ); + if ( painter->testf(QPainter::DirtyFont) ) + painter->updateFont(); + if ( painter->pfont ) + d = painter->pfont->d; + else + d = painter->cfont.d; + d->ref(); +} + +/*! + Constructs a copy of \a fi. +*/ +QFontInfo::QFontInfo( const QFontInfo &fi ) + : d(fi.d), painter(0), fscript( fi.fscript ) +{ + d->ref(); +} + +/*! + Destroys the font info object. +*/ +QFontInfo::~QFontInfo() +{ + if ( d->deref() ) + delete d; +} + +/*! + Assigns the font info in \a fi. +*/ +QFontInfo &QFontInfo::operator=( const QFontInfo &fi ) +{ + if ( d != fi.d ) { + if ( d->deref() ) + delete d; + d = fi.d; + d->ref(); + } + painter = 0; + fscript = fi.fscript; + return *this; +} + +/*! + Returns the family name of the matched window system font. + + \sa QFont::family() +*/ +QString QFontInfo::family() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.family; +} + +/*! + Returns the point size of the matched window system font. + + \sa QFont::pointSize() +*/ +int QFontInfo::pointSize() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return ( engine->fontDef.pointSize + 5 ) / 10; +} + +/*! + Returns the pixel size of the matched window system font. + + \sa QFont::pointSize() +*/ +int QFontInfo::pixelSize() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.pixelSize; +} + +/*! + Returns the italic value of the matched window system font. + + \sa QFont::italic() +*/ +bool QFontInfo::italic() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.italic; +} + +/*! + Returns the weight of the matched window system font. + + \sa QFont::weight(), bold() +*/ +int QFontInfo::weight() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.weight; + +} + +/*! + \fn bool QFontInfo::bold() const + + Returns TRUE if weight() would return a value greater than \c + QFont::Normal; otherwise returns FALSE. + + \sa weight(), QFont::bold() +*/ + +/*! + Returns the underline value of the matched window system font. + + \sa QFont::underline() + + \internal + + Here we read the underline flag directly from the QFont. + This is OK for X11 and for Windows because we always get what we want. +*/ +bool QFontInfo::underline() const +{ + return d->underline; +} + +/*! + Returns the overline value of the matched window system font. + + \sa QFont::overline() + + \internal + + Here we read the overline flag directly from the QFont. + This is OK for X11 and for Windows because we always get what we want. +*/ +bool QFontInfo::overline() const +{ + return d->overline; +} + +/*! + Returns the strikeout value of the matched window system font. + + \sa QFont::strikeOut() + + \internal Here we read the strikeOut flag directly from the QFont. + This is OK for X11 and for Windows because we always get what we want. +*/ +bool QFontInfo::strikeOut() const +{ + return d->strikeOut; +} + +/*! + Returns the fixed pitch value of the matched window system font. + + \sa QFont::fixedPitch() +*/ +bool QFontInfo::fixedPitch() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE +#ifdef Q_OS_MAC + if (!engine->fontDef.fixedPitchComputed) { + QChar ch[2] = { QChar('i'), QChar('m') }; + glyph_t g[2]; + int l = 2; + advance_t a[2]; + engine->stringToCMap(ch, 2, g, a, &l, FALSE); + engine->fontDef.fixedPitch = a[0] == a[1]; + engine->fontDef.fixedPitchComputed = TRUE; + } +#endif + return engine->fontDef.fixedPitch; +} + +/*! + Returns the style of the matched window system font. + + Currently only returns the style hint set in QFont. + + \sa QFont::styleHint() QFont::StyleHint +*/ +QFont::StyleHint QFontInfo::styleHint() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return (QFont::StyleHint) engine->fontDef.styleHint; +} + +/*! + Returns TRUE if the font is a raw mode font; otherwise returns + FALSE. + + If it is a raw mode font, all other functions in QFontInfo will + return the same values set in the QFont, regardless of the font + actually used. + + \sa QFont::rawMode() +*/ +bool QFontInfo::rawMode() const +{ + return d->rawMode; +} + +/*! + Returns TRUE if the matched window system font is exactly the same + as the one specified by the font; otherwise returns FALSE. + + \sa QFont::exactMatch() +*/ +bool QFontInfo::exactMatch() const +{ + QFontEngine *engine = d->engineForScript( (QFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return d->rawMode ? engine->type() != QFontEngine::Box + : d->request == engine->fontDef; +} + + + + +// ********************************************************************** +// QFontCache +// ********************************************************************** + +#ifdef QFONTCACHE_DEBUG +// fast timeouts for debugging +static const int fast_timeout = 1000; // 1s +static const int slow_timeout = 5000; // 5s +#else +static const int fast_timeout = 10000; // 10s +static const int slow_timeout = 300000; // 5m +#endif // QFONTCACHE_DEBUG + +QFontCache *QFontCache::instance = 0; +const uint QFontCache::min_cost = 4*1024; // 4mb + +static QSingleCleanupHandler<QFontCache> cleanup_fontcache; + + +QFontCache::QFontCache() + : QObject( qApp, "global font cache" ), total_cost( 0 ), max_cost( min_cost ), + current_timestamp( 0 ), fast( FALSE ), timer_id( -1 ) +{ + Q_ASSERT( instance == 0 ); + instance = this; + cleanup_fontcache.set( &instance ); +} + +QFontCache::~QFontCache() +{ + { + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + if ( it.data()->count == 0 ) + delete it.data(); + else + FC_DEBUG("QFontCache::~QFontCache: engineData %p still has refcount %d", + it.data(), it.data()->count); + ++it; + } + } + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + while ( it != end ) { + if ( it.data().data->count == 0 ) { + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG("QFontCache::~QFontCache: deleting engine %p key=(%d / %d %d %d %d %d)", + it.data().data, it.key().script, it.key().def.pointSize, + it.key().def.pixelSize, it.key().def.weight, it.key().def.italic, + it.key().def.fixedPitch); + + delete it.data().data; + } + } else { + FC_DEBUG("QFontCache::~QFontCache: engine = %p still has refcount %d", + it.data().data, it.data().data->count); + } + ++it; + } + instance = 0; +} + +#ifdef Q_WS_QWS +void QFontCache::clear() +{ + { + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + QFontEngineData *data = it.data(); + if ( data->engine ) + data->engine->deref(); + data->engine = 0; + ++it; + } + } + + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + while ( it != end ) { + if ( it.data().data->count == 0 ) { + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG("QFontCache::~QFontCache: deleting engine %p key=(%d / %d %d %d %d %d)", + it.data().data, it.key().script, it.key().def.pointSize, + it.key().def.pixelSize, it.key().def.weight, it.key().def.italic, + it.key().def.fixedPitch); + delete it.data().data; + } + } else { + FC_DEBUG("QFontCache::~QFontCache: engine = %p still has refcount %d", + it.data().data, it.data().data->count); + } + ++it; + } +} +#endif + +QFontEngineData *QFontCache::findEngineData( const Key &key ) const +{ + EngineDataCache::ConstIterator it = engineDataCache.find( key ), + end = engineDataCache.end(); + if ( it == end ) return 0; + + // found + return it.data(); +} + +void QFontCache::insertEngineData( const Key &key, QFontEngineData *engineData ) +{ + FC_DEBUG( "QFontCache: inserting new engine data %p", engineData ); + + engineDataCache.insert( key, engineData ); + increaseCost( sizeof( QFontEngineData ) ); +} + +QFontEngine *QFontCache::findEngine( const Key &key ) +{ + EngineCache::Iterator it = engineCache.find( key ), + end = engineCache.end(); + if ( it == end ) return 0; + + // found... update the hitcount and timestamp + it.data().hits++; + it.data().timestamp = ++current_timestamp; + + FC_DEBUG( "QFontCache: found font engine\n" + " %p: timestamp %4u hits %3u ref %2d/%2d, type '%s'", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->name() ); + + return it.data().data; +} + +void QFontCache::insertEngine( const Key &key, QFontEngine *engine ) +{ + FC_DEBUG( "QFontCache: inserting new engine %p", engine ); + + Engine data( engine ); + data.timestamp = ++current_timestamp; + + engineCache.insert( key, data ); + + // only increase the cost if this is the first time we insert the engine + if ( engine->cache_count == 0 ) + increaseCost( engine->cache_cost ); + + ++engine->cache_count; +} + +void QFontCache::increaseCost( uint cost ) +{ + cost = ( cost + 512 ) / 1024; // store cost in kb + cost = cost > 0 ? cost : 1; + total_cost += cost; + + FC_DEBUG( " COST: increased %u kb, total_cost %u kb, max_cost %u kb", + cost, total_cost, max_cost ); + + if ( total_cost > max_cost) { + max_cost = total_cost; + + if ( timer_id == -1 || ! fast ) { + FC_DEBUG( " TIMER: starting fast timer (%d ms)", fast_timeout ); + + if (timer_id != -1) killTimer( timer_id ); + timer_id = startTimer( fast_timeout ); + fast = TRUE; + } + } +} + +void QFontCache::decreaseCost( uint cost ) +{ + cost = ( cost + 512 ) / 1024; // cost is stored in kb + cost = cost > 0 ? cost : 1; + Q_ASSERT( cost <= total_cost ); + total_cost -= cost; + + FC_DEBUG( " COST: decreased %u kb, total_cost %u kb, max_cost %u kb", + cost, total_cost, max_cost ); +} + +#if defined(Q_WS_WIN ) || defined (Q_WS_QWS) +void QFontCache::cleanupPrinterFonts() +{ + FC_DEBUG( "QFontCache::cleanupPrinterFonts" ); + + { + FC_DEBUG( " CLEAN engine data:" ); + + // clean out all unused engine datas + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + if ( it.key().screen == 0 ) { + ++it; + continue; + } + + if( it.data()->count > 0 ) { +#ifdef Q_WS_WIN + for(int i = 0; i < QFont::LastPrivateScript; ++i) { + if( it.data()->engines[i] ) { + it.data()->engines[i]->deref(); + it.data()->engines[i] = 0; + } + } +#else + if ( it.data()->engine ) { + it.data()->engine->deref(); + it.data()->engine = 0; + } +#endif + ++it; + } else { + + EngineDataCache::Iterator rem = it++; + + decreaseCost( sizeof( QFontEngineData ) ); + + FC_DEBUG( " %p", rem.data() ); + + delete rem.data(); + engineDataCache.remove( rem ); + } + } + } + + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + while( it != end ) { + if ( it.data().data->count > 0 || it.key().screen == 0) { + ++it; + continue; + } + + FC_DEBUG( " %p: timestamp %4u hits %2u ref %2d/%2d, type '%s'", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->name() ); + + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG( " DELETE: last occurence in cache" ); + + decreaseCost( it.data().data->cache_cost ); + delete it.data().data; + } + + engineCache.remove( it++ ); + } +} +#endif + +void QFontCache::timerEvent( QTimerEvent * ) +{ + FC_DEBUG( "QFontCache::timerEvent: performing cache maintenance (timestamp %u)", + current_timestamp ); + + if ( total_cost <= max_cost && max_cost <= min_cost ) { + FC_DEBUG( " cache redused sufficiently, stopping timer" ); + + killTimer( timer_id ); + timer_id = -1; + fast = FALSE; + + return; + } + + // go through the cache and count up everything in use + uint in_use_cost = 0; + + { + FC_DEBUG( " SWEEP engine data:" ); + + // make sure the cost of each engine data is at least 1kb + const uint engine_data_cost = + sizeof( QFontEngineData ) > 1024 ? sizeof( QFontEngineData ) : 1024; + + EngineDataCache::ConstIterator it = engineDataCache.begin(), + end = engineDataCache.end(); + for ( ; it != end; ++it ) { +#ifdef QFONTCACHE_DEBUG + FC_DEBUG( " %p: ref %2d", it.data(), it.data()->count ); + +# if defined(Q_WS_X11) || defined(Q_WS_WIN) + // print out all engines + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) { + if ( ! it.data()->engines[i] ) continue; + FC_DEBUG( " contains %p", it.data()->engines[i] ); + } +# endif // Q_WS_X11 || Q_WS_WIN +#endif // QFONTCACHE_DEBUG + + if ( it.data()->count > 0 ) + in_use_cost += engine_data_cost; + } + } + + { + FC_DEBUG( " SWEEP engine:" ); + + EngineCache::ConstIterator it = engineCache.begin(), + end = engineCache.end(); + for ( ; it != end; ++it ) { + FC_DEBUG( " %p: timestamp %4u hits %2u ref %2d/%2d, cost %u bytes", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->cache_cost ); + + if ( it.data().data->count > 0 ) + in_use_cost += it.data().data->cache_cost / it.data().data->cache_count; + } + + // attempt to make up for rounding errors + in_use_cost += (uint)engineCache.count(); + } + + in_use_cost = ( in_use_cost + 512 ) / 1024; // cost is stored in kb + + /* + calculate the new maximum cost for the cache + + NOTE: in_use_cost is *not* correct due to rounding errors in the + above algorithm. instead of worrying about getting the + calculation correct, we are more interested in speed, and use + in_use_cost as a floor for new_max_cost + */ + uint new_max_cost = QMAX( QMAX( max_cost / 2, in_use_cost ), min_cost ); + + FC_DEBUG( " after sweep, in use %u kb, total %u kb, max %u kb, new max %u kb", + in_use_cost, total_cost, max_cost, new_max_cost ); + + if ( new_max_cost == max_cost ) { + if ( fast ) { + FC_DEBUG( " cannot shrink cache, slowing timer" ); + + killTimer( timer_id ); + timer_id = startTimer( slow_timeout ); + fast = FALSE; + } + + return; + } else if ( ! fast ) { + FC_DEBUG( " dropping into passing gear" ); + + killTimer( timer_id ); + timer_id = startTimer( fast_timeout ); + fast = TRUE; + } + + max_cost = new_max_cost; + + { + FC_DEBUG( " CLEAN engine data:" ); + + // clean out all unused engine datas + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + if ( it.data()->count > 0 ) { + ++it; + continue; + } + + EngineDataCache::Iterator rem = it++; + + decreaseCost( sizeof( QFontEngineData ) ); + + FC_DEBUG( " %p", rem.data() ); + + delete rem.data(); + engineDataCache.remove( rem ); + } + } + + // clean out the engine cache just enough to get below our new max cost + uint current_cost; + do { + current_cost = total_cost; + + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + // determine the oldest and least popular of the unused engines + uint oldest = ~0; + uint least_popular = ~0; + + for ( ; it != end; ++it ) { + if ( it.data().data->count > 0 ) continue; + + if ( it.data().timestamp < oldest && + it.data().hits <= least_popular ) { + oldest = it.data().timestamp; + least_popular = it.data().hits; + } + } + + FC_DEBUG( " oldest %u least popular %u", oldest, least_popular ); + + for ( it = engineCache.begin(); it != end; ++it ) { + if ( it.data().data->count == 0 && + it.data().timestamp == oldest && + it.data().hits == least_popular) + break; + } + + if ( it != end ) { + FC_DEBUG( " %p: timestamp %4u hits %2u ref %2d/%2d, type '%s'", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->name() ); + + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG( " DELETE: last occurence in cache" ); + + decreaseCost( it.data().data->cache_cost ); + delete it.data().data; + } else { + /* + this particular font engine is in the cache multiple + times... set current_cost to zero, so that we can + keep looping to get rid of all occurences + */ + current_cost = 0; + } + + engineCache.remove( it ); + } + } while ( current_cost != total_cost && total_cost > max_cost ); +} diff --git a/src/kernel/qfont.h b/src/kernel/qfont.h new file mode 100644 index 0000000..935f04a --- /dev/null +++ b/src/kernel/qfont.h @@ -0,0 +1,372 @@ +/**************************************************************************** +** +** Definition of QFont class +** +** Created : 940514 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QFONT_H +#define QFONT_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qstring.h" +#endif // QT_H + + +class QFontPrivate; /* don't touch */ +class QStringList; +class QTextFormatCollection; + +class Q_EXPORT QFont +{ +public: + enum StyleHint { + Helvetica, SansSerif = Helvetica, + Times, Serif = Times, + Courier, TypeWriter = Courier, + OldEnglish, Decorative = OldEnglish, + System, + AnyStyle + }; + + enum StyleStrategy { + PreferDefault = 0x0001, + PreferBitmap = 0x0002, + PreferDevice = 0x0004, + PreferOutline = 0x0008, + ForceOutline = 0x0010, + PreferMatch = 0x0020, + PreferQuality = 0x0040, + PreferAntialias = 0x0080, + NoAntialias = 0x0100, + OpenGLCompatible = 0x0200 + }; + + enum Weight { + Light = 25, + Normal = 50, + DemiBold = 63, + Bold = 75, + Black = 87 + }; + + enum Stretch { + UltraCondensed = 50, + ExtraCondensed = 62, + Condensed = 75, + SemiCondensed = 87, + Unstretched = 100, + SemiExpanded = 112, + Expanded = 125, + ExtraExpanded = 150, + UltraExpanded = 200 + }; + + // default font + QFont(); + // specific font +#ifdef Q_QDOC + QFont( const QString &family, int pointSize = 12, int weight = Normal, + bool italic = FALSE ); +#else + QFont( const QString &family, int pointSize = -1, int weight = -1, + bool italic = FALSE ); +#endif + // copy constructor + QFont( const QFont & ); + + ~QFont(); + + QString family() const; + void setFamily( const QString &); + + int pointSize() const; + float pointSizeFloat() const; + void setPointSize( int ); + void setPointSizeFloat( float ); + + int pixelSize() const; + void setPixelSize( int ); + void setPixelSizeFloat( float ); + + int weight() const; + void setWeight( int ); + + bool bold() const; + void setBold( bool ); + + bool italic() const; + void setItalic( bool ); + + bool underline() const; + void setUnderline( bool ); + + bool overline() const; + void setOverline( bool ); + + bool strikeOut() const; + void setStrikeOut( bool ); + + bool fixedPitch() const; + void setFixedPitch( bool ); + + StyleHint styleHint() const; + StyleStrategy styleStrategy() const; + void setStyleHint( StyleHint, StyleStrategy = PreferDefault ); + void setStyleStrategy( StyleStrategy s ); + + int stretch() const; + void setStretch( int ); + + // is raw mode still needed? + bool rawMode() const; + void setRawMode( bool ); + + // dupicated from QFontInfo + bool exactMatch() const; + + QFont &operator=( const QFont & ); + bool operator==( const QFont & ) const; + bool operator!=( const QFont & ) const; + bool isCopyOf( const QFont & ) const; + + +#ifdef Q_WS_WIN + HFONT handle() const; +#else // !Q_WS_WIN + Qt::HANDLE handle() const; +#endif // Q_WS_WIN + + + // needed for X11 + void setRawName( const QString & ); + QString rawName() const; + + QString key() const; + + QString toString() const; + bool fromString(const QString &); + +#ifndef QT_NO_STRINGLIST + static QString substitute(const QString &); + static QStringList substitutes(const QString &); + static QStringList substitutions(); + static void insertSubstitution(const QString&, const QString &); + static void insertSubstitutions(const QString&, const QStringList &); + static void removeSubstitution(const QString &); +#endif //QT_NO_STRINGLIST + static void initialize(); + static void cleanup(); +#ifndef Q_WS_QWS + static void cacheStatistics(); +#endif + +#if defined(Q_WS_QWS) + void qwsRenderToDisk(bool all=TRUE); +#endif + + + // a copy of this lives in qunicodetables.cpp, as we can't include + // qfont.h it in tools/. Do not modify without changing the script + // enum in qunicodetable_p.h aswell. + enum Script { + // European Alphabetic Scripts + Latin, + Greek, + Cyrillic, + Armenian, + Georgian, + Runic, + Ogham, + SpacingModifiers, + CombiningMarks, + + // Middle Eastern Scripts + Hebrew, + Arabic, + Syriac, + Thaana, + + // South and Southeast Asian Scripts + Devanagari, + Bengali, + Gurmukhi, + Gujarati, + Oriya, + Tamil, + Telugu, + Kannada, + Malayalam, + Sinhala, + Thai, + Lao, + Tibetan, + Myanmar, + Khmer, + + // East Asian Scripts + Han, + Hiragana, + Katakana, + Hangul, + Bopomofo, + Yi, + + // Additional Scripts + Ethiopic, + Cherokee, + CanadianAboriginal, + Mongolian, + + // Symbols + CurrencySymbols, + LetterlikeSymbols, + NumberForms, + MathematicalOperators, + TechnicalSymbols, + GeometricSymbols, + MiscellaneousSymbols, + EnclosedAndSquare, + Braille, + + Unicode, + + // some scripts added in Unicode 3.2 + Tagalog, + Hanunoo, + Buhid, + Tagbanwa, + + KatakanaHalfWidth, + + // from Unicode 4.0 + Limbu, + TaiLe, + + // End +#if !defined(Q_QDOC) + NScripts, + UnknownScript = NScripts, + + NoScript, + + // ---------------------------------------- + // Dear User, you can see values > NScript, + // but they are internal - do not touch. + + Han_Japanese, + Han_SimplifiedChinese, + Han_TraditionalChinese, + Han_Korean, + + LastPrivateScript +#endif + }; + + QString defaultFamily() const; + QString lastResortFamily() const; + QString lastResortFont() const; + +#ifndef QT_NO_COMPAT + + static QFont defaultFont(); + static void setDefaultFont( const QFont & ); + +#endif // QT_NO_COMPAT + + QFont resolve( const QFont & ) const; + +protected: + // why protected? + bool dirty() const; + int deciPointSize() const; + +private: + QFont( QFontPrivate *, QPaintDevice *pd ); + + void detach(); + +#if defined(Q_WS_MAC) + void macSetFont(QPaintDevice *); +#elif defined(Q_WS_X11) + void x11SetScreen( int screen = -1 ); + int x11Screen() const; +#endif + + friend class QFontMetrics; + friend class QFontInfo; + friend class QPainter; + friend class QPSPrinterFont; + friend class QApplication; + friend class QWidget; + friend class QTextFormatCollection; + friend class QTextLayout; + friend class QTextItem; + friend class QGLContext; +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + friend Qt::HANDLE qt_xft_handle(const QFont &font); +#endif +#ifndef QT_NO_DATASTREAM + friend Q_EXPORT QDataStream &operator<<( QDataStream &, const QFont & ); + friend Q_EXPORT QDataStream &operator>>( QDataStream &, QFont & ); +#endif + + QFontPrivate *d; +}; + + +inline bool QFont::bold() const +{ return weight() > Normal; } + + +inline void QFont::setBold( bool enable ) +{ setWeight( enable ? Bold : Normal ); } + + + + +/***************************************************************************** + QFont stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QFont & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QFont & ); +#endif + + +#endif // QFONT_H diff --git a/src/kernel/qfont_x11.cpp b/src/kernel/qfont_x11.cpp new file mode 100644 index 0000000..fea0b58 --- /dev/null +++ b/src/kernel/qfont_x11.cpp @@ -0,0 +1,737 @@ +/**************************************************************************** +** +** Implementation of QFont, QFontMetrics and QFontInfo classes for X11 +** +** Created : 940515 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#define QT_FATAL_ASSERT + +// REVISED: brad + +#include "qplatformdefs.h" + +#include "qfont.h" +#include "qapplication.h" +#include "qcleanuphandler.h" +#include "qfontinfo.h" +#include "qfontdatabase.h" +#include "qfontmetrics.h" +#include "qpaintdevice.h" +#include "qpaintdevicemetrics.h" +#include "qtextcodec.h" + +#include <private/qfontcodecs_p.h> +#include <private/qunicodetables_p.h> +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qtextengine_p.h" + +#include "qt_x11_p.h" + +#include <time.h> +#include <stdlib.h> +#include <ctype.h> + +#define QFONTLOADER_DEBUG +#define QFONTLOADER_DEBUG_VERBOSE + +Q_EXPORT bool qt_has_xft = FALSE; + +#ifndef QT_NO_XFTFREETYPE +Qt::HANDLE qt_xft_handle(const QFont &font) +{ + QFontEngine *engine = font.d->engineForScript( QFontPrivate::defaultScript ); + if (!engine->type() == QFontEngine::Xft) + return 0; + return (long)static_cast<QFontEngineXft *>(engine)->font(); +} +#endif + +double qt_pixelSize(double pointSize, QPaintDevice *paintdevice, int scr) +{ + if (pointSize < 0) return -1.; + + double result = pointSize; + if (paintdevice && QPaintDeviceMetrics( paintdevice ).logicalDpiY() != 75) + result *= QPaintDeviceMetrics( paintdevice ).logicalDpiY() / 72.; + else if (QPaintDevice::x11AppDpiY( scr ) != 75) + result *= QPaintDevice::x11AppDpiY( scr ) / 72.; + + return result; +} + +double qt_pointSize(double pixelSize, QPaintDevice *paintdevice, int scr) +{ + if (pixelSize < 0) return -1.; + + double result = pixelSize; + if ( paintdevice && QPaintDeviceMetrics( paintdevice ).logicalDpiY() != 75) + result *= 72. / QPaintDeviceMetrics( paintdevice ).logicalDpiY(); + else if (QPaintDevice::x11AppDpiY(scr) != 75) + result *= 72. / QPaintDevice::x11AppDpiY( scr ); + + return result; +} + +static inline double pixelSize( const QFontDef &request, QPaintDevice *paintdevice, + int scr ) +{ + return ((request.pointSize != -1) ? + qt_pixelSize(request.pointSize / 10., paintdevice, scr) : + (double)request.pixelSize); +} + +static inline double pointSize( const QFontDef &request, QPaintDevice *paintdevice, + int scr ) +{ + return ((request.pixelSize != -1) ? + qt_pointSize(request.pixelSize, paintdevice, scr) * 10.: + (double)request.pointSize); +} + + +/* + Removes wildcards from an XLFD. + + Returns \a xlfd with all wildcards removed if a match for \a xlfd is + found, otherwise it returns \a xlfd. +*/ +static QCString qt_fixXLFD( const QCString &xlfd ) +{ + QCString ret = xlfd; + int count = 0; + char **fontNames = + XListFonts( QPaintDevice::x11AppDisplay(), xlfd, 32768, &count ); + if ( count > 0 ) + ret = fontNames[0]; + XFreeFontNames( fontNames ); + return ret ; +} + +typedef QMap<QFont::Script,QString> FallbackMap; +static FallbackMap *fallbackMap = 0; +static QSingleCleanupHandler<FallbackMap> qt_fallback_font_family_cleanup; + +static void ensure_fallback_map() +{ + if ( fallbackMap ) return; + fallbackMap = new FallbackMap; + qt_fallback_font_family_cleanup.set( &fallbackMap ); +} + +// Returns the user-configured fallback family for the specified script. +QString qt_fallback_font_family( QFont::Script script ) +{ + QString ret; + + if ( fallbackMap ) { + FallbackMap::ConstIterator it, end = fallbackMap->end(); + it = fallbackMap->find( script ); + if ( it != end ) + ret = it.data(); + } + + return ret; +} + +// Sets the fallback family for the specified script. +void qt_set_fallback_font_family( QFont::Script script, const QString &family ) +{ + ensure_fallback_map(); + + if ( ! family.isEmpty() ) + fallbackMap->insert( script, family ); + else + fallbackMap->remove( script ); +} + + +QFont::Script QFontPrivate::defaultScript = QFont::UnknownScript; +int QFontPrivate::defaultEncodingID = -1; + +/*! + Internal function that initializes the font system. + + \internal + The font cache and font dict do not alloc the keys. The key is a QString + which is shared between QFontPrivate and QXFontName. +*/ +void QFont::initialize() +{ + // create global font cache + if ( ! QFontCache::instance ) (void) new QFontCache; + +#ifndef QT_NO_CODECS +#ifndef QT_NO_BIG_CODECS + static bool codecs_once = FALSE; + if ( ! codecs_once ) { + (void) new QFontJis0201Codec; + (void) new QFontJis0208Codec; + (void) new QFontKsc5601Codec; + (void) new QFontGb2312Codec; + (void) new QFontGbkCodec; + (void) new QFontGb18030_0Codec; + (void) new QFontBig5Codec; + (void) new QFontBig5hkscsCodec; + (void) new QFontLaoCodec; + codecs_once = TRUE; + } +#endif // QT_NO_BIG_CODECS +#endif // QT_NO_CODECS + + extern int qt_encoding_id_for_mib( int mib ); // from qfontdatabase_x11.cpp + QTextCodec *codec = QTextCodec::codecForLocale(); + // determine the default encoding id using the locale, otherwise + // fallback to latin1 ( mib == 4 ) + int mib = codec ? codec->mibEnum() : 4; + + // for asian locales, use the mib for the font codec instead of the locale codec + switch (mib) { + case 38: // eucKR + mib = 36; + break; + + case 2025: // GB2312 + mib = 57; + break; + + case 113: // GBK + mib = -113; + break; + + case 114: // GB18030 + mib = -114; + break; + + case 2026: // Big5 + mib = -2026; + break; + + case 2101: // Big5-HKSCS + mib = -2101; + break; + + case 16: // JIS7 + mib = 15; + break; + + case 17: // SJIS + case 18: // eucJP + mib = 63; + break; + } + + // get the default encoding id for the locale encoding... + QFontPrivate::defaultEncodingID = qt_encoding_id_for_mib( mib ); + + // get some sample text based on the users locale. we use this to determine the + // default script for the font system + QCString oldlctime = setlocale(LC_TIME, 0); + QCString lctime = setlocale(LC_TIME, ""); + + time_t ttmp = time(0); + struct tm *tt = 0; + char samp[64]; + QString sample; + + if ( ttmp != -1 ) { +#if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) + // use the reentrant versions of localtime() where available + tm res; + tt = localtime_r( &ttmp, &res ); +#else + tt = localtime( &ttmp ); +#endif // QT_THREAD_SUPPORT && _POSIX_THREAD_SAFE_FUNCTIONS + + if ( tt != 0 && strftime( samp, 64, "%A%B", tt ) > 0 ) + if ( codec ) + sample = codec->toUnicode( samp ); + } + + if ( ! sample.isNull() && ! sample.isEmpty() ) { + QFont::Script cs = QFont::NoScript, tmp; + const QChar *uc = sample.unicode(); + QFontPrivate *priv = new QFontPrivate; + + for ( uint i = 0; i < sample.length(); i++ ) { + SCRIPT_FOR_CHAR( tmp, *uc ); + uc++; + if ( tmp != cs && tmp != QFont::UnknownScript ) { + cs = tmp; + break; + } + } + delete priv; + + if ( cs != QFont::UnknownScript ) + QFontPrivate::defaultScript = cs; + } + + setlocale( LC_TIME, oldlctime.data() ); +} + +/*! \internal + + Internal function that cleans up the font system. +*/ +void QFont::cleanup() +{ + // delete the global font cache + delete QFontCache::instance; +} + +/*! + \internal + X11 Only: Returns the screen with which this font is associated. +*/ +int QFont::x11Screen() const +{ + return d->screen; +} + +/*! \internal + X11 Only: Associate the font with the specified \a screen. +*/ +void QFont::x11SetScreen( int screen ) +{ + if ( screen < 0 ) // assume default + screen = QPaintDevice::x11AppScreen(); + + if ( screen == d->screen ) + return; // nothing to do + + detach(); + d->screen = screen; +} + +/*! \internal + Returns a QFontEngine for the specified \a script that matches the + QFontDef \e request member variable. +*/ +void QFontPrivate::load( QFont::Script script ) +{ + // NOTE: the X11 and Windows implementations of this function are + // identical... if you change one, change both. + +#ifdef QT_CHECK_STATE + // sanity checks + if (!QFontCache::instance) + qWarning("Must construct a QApplication before a QFont"); + Q_ASSERT( script >= 0 && script < QFont::LastPrivateScript ); +#endif // QT_CHECK_STATE + + QFontDef req = request; + req.pixelSize = qRound(pixelSize(req, paintdevice, screen)); + req.pointSize = 0; + + if ( ! engineData ) { + QFontCache::Key key( req, QFont::NoScript, screen, paintdevice ); + + // look for the requested font in the engine data cache + engineData = QFontCache::instance->findEngineData( key ); + + if ( ! engineData ) { + // create a new one + engineData = new QFontEngineData; + QFontCache::instance->insertEngineData( key, engineData ); + } else { + engineData->ref(); + } + } + + // the cached engineData could have already loaded the engine we want + if ( engineData->engines[script] ) return; + + // load the font + QFontEngine *engine = 0; + // double scale = 1.0; // ### TODO: fix the scale calculations + + // list of families to try + QStringList family_list; + + if (!req.family.isEmpty()) { + family_list = QStringList::split( ',', req.family ); + + // append the substitute list for each family in family_list + QStringList subs_list; + QStringList::ConstIterator it = family_list.begin(), end = family_list.end(); + for ( ; it != end; ++it ) + subs_list += QFont::substitutes( *it ); + family_list += subs_list; + +#ifndef QT_XFT2 + // with Xft2, we want to use fontconfig to determine better fallbacks, + // otherwise we might run into trouble with default fonts as "serif" + + // append the default fallback font for the specified script + QString fallback = qt_fallback_font_family( script ); + if ( ! fallback.isEmpty() && ! family_list.contains( fallback ) ) + family_list << fallback; + + // add the default family + QString defaultFamily = QApplication::font().family(); + if ( ! family_list.contains( defaultFamily ) ) + family_list << defaultFamily; + + // add QFont::defaultFamily() to the list, for compatibility with + // previous versions + family_list << QApplication::font().defaultFamily(); +#endif // QT_XFT2 + } + + // null family means find the first font matching the specified script + family_list << QString::null; + + QStringList::ConstIterator it = family_list.begin(), end = family_list.end(); + for ( ; ! engine && it != end; ++it ) { + req.family = *it; + + engine = QFontDatabase::findFont( script, this, req ); + if ( engine ) { + if ( engine->type() != QFontEngine::Box ) + break; + + if ( ! req.family.isEmpty() ) + engine = 0; + + continue; + } + } + + engine->ref(); + engineData->engines[script] = engine; +} + +/*! + Returns TRUE if the font attributes have been changed and the font + has to be (re)loaded; otherwise returns FALSE. +*/ +bool QFont::dirty() const +{ + return d->engineData == 0; +} + +/*! + Returns the window system handle to the font, for low-level + access. Using this function is \e not portable. +*/ +Qt::HANDLE QFont::handle() const +{ + QFontEngine *engine = d->engineForScript( QFontPrivate::defaultScript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + switch ( engine->type() ) { + case QFontEngine::XLFD: + return ((QFontEngineXLFD *) engine)->handle(); + case QFontEngine::LatinXLFD: + return ((QFontEngineLatinXLFD *) engine)->handle(); + + default: break; + } + return 0; +} + +/*! + Returns the name of the font within the underlying window system. + + On Windows, this is usually just the family name of a TrueType + font. + + On X11, it is an XLFD (X Logical Font Description). When Qt is + build with Xft support on X11, the return value can be an Xft + pattern or an XLFD. + + Using the return value of this function is usually \e not \e + portable. + + \sa setRawName() +*/ +QString QFont::rawName() const +{ + QFontEngine *engine = d->engineForScript( QFontPrivate::defaultScript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return QString::fromLatin1( engine->name() ); +} + +/*! + Sets a font by its system specific name. The function is + particularly useful under X, where system font settings (for + example X resources) are usually available in XLFD (X Logical Font + Description) form only. You can pass an XLFD as \a name to this + function. + + A font set with setRawName() is still a full-featured QFont. It can + be queried (for example with italic()) or modified (for example with + setItalic()) and is therefore also suitable for rendering rich text. + + If Qt's internal font database cannot resolve the raw name, the + font becomes a raw font with \a name as its family. + + Note that the present implementation does not handle wildcards in + XLFDs well, and that font aliases (file \c fonts.alias in the font + directory on X11) are not supported. + + \sa rawName(), setRawMode(), setFamily() +*/ +void QFont::setRawName( const QString &name ) +{ + detach(); + + // from qfontdatabase_x11.cpp + extern bool qt_fillFontDef( const QCString &xlfd, QFontDef *fd, int screen ); + + if ( ! qt_fillFontDef( qt_fixXLFD( name.latin1() ), &d->request, d->screen ) ) { +#ifdef QT_CHECK_STATE + qWarning("QFont::setRawName(): Invalid XLFD: \"%s\"", name.latin1()); +#endif // QT_CHECK_STATE + + setFamily( name ); + setRawMode( TRUE ); + } else { + d->mask = QFontPrivate::Complete; + } +} + +/*! + Returns the "last resort" font family name. + + The current implementation tries a wide variety of common fonts, + returning the first one it finds. Is is possible that no family is + found in which case a null string is returned. + + \sa lastResortFont() +*/ +QString QFont::lastResortFamily() const +{ + return QString::fromLatin1( "Helvetica" ); +} + +/*! + Returns the family name that corresponds to the current style + hint. + + \sa StyleHint styleHint() setStyleHint() +*/ +QString QFont::defaultFamily() const +{ + switch ( d->request.styleHint ) { + case QFont::Times: + return QString::fromLatin1( "Times" ); + + case QFont::Courier: + return QString::fromLatin1( "Courier" ); + + case QFont::Decorative: + return QString::fromLatin1( "Old English" ); + + case QFont::Helvetica: + case QFont::System: + default: + return QString::fromLatin1( "Helvetica" ); + } +} + +/* + Returns a last resort raw font name for the font matching algorithm. + This is used if even the last resort family is not available. It + returns \e something, almost no matter what. The current + implementation tries a wide variety of common fonts, returning the + first one it finds. The implementation may change at any time. +*/ +static const char * const tryFonts[] = { + "-*-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-times-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-lucida-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-helvetica-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-courier-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-times-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-lucida-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-helvetica-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-courier-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-times-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-lucida-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*", + "6x13", + "7x13", + "8x13", + "9x15", + "fixed", + 0 +}; + +// Returns TRUE if the font exists, FALSE otherwise +static bool fontExists( const QString &fontName ) +{ + int count; + char **fontNames = XListFonts( QPaintDevice::x11AppDisplay(), + (char*)fontName.latin1(), 32768, &count ); + if ( fontNames ) XFreeFontNames( fontNames ); + + return count != 0; +} + +/*! + Returns a "last resort" font name for the font matching algorithm. + This is used if the last resort family is not available. It will + always return a name, if necessary returning something like + "fixed" or "system". + + The current implementation tries a wide variety of common fonts, + returning the first one it finds. The implementation may change + at any time, but this function will always return a string + containing something. + + It is theoretically possible that there really isn't a + lastResortFont() in which case Qt will abort with an error + message. We have not been able to identify a case where this + happens. Please \link bughowto.html report it as a bug\endlink if + it does, preferably with a list of the fonts you have installed. + + \sa lastResortFamily() rawName() +*/ +QString QFont::lastResortFont() const +{ + static QString last; + + // already found + if ( ! last.isNull() ) + return last; + + int i = 0; + const char* f; + + while ( ( f = tryFonts[i] ) ) { + last = QString::fromLatin1( f ); + + if ( fontExists( last ) ) + return last; + + i++; + } + +#if defined(CHECK_NULL) + qFatal( "QFontPrivate::lastResortFont: Cannot find any reasonable font" ); +#endif + + return last; +} + + + + +// ********************************************************************** +// QFontMetrics member methods +// ********************************************************************** + +int QFontMetrics::width( QChar ch ) const +{ + unsigned short uc = ch.unicode(); + if ( uc < QFontEngineData::widthCacheSize && + d->engineData && d->engineData->widthCache[ uc ] ) + return d->engineData->widthCache[ uc ]; + + if ( ::category( ch ) == QChar::Mark_NonSpacing || qIsZeroWidthChar(ch.unicode())) + return 0; + + QFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[8]; + advance_t advances[8]; + int nglyphs = 7; + engine->stringToCMap( &ch, 1, glyphs, advances, &nglyphs, FALSE ); + + // ### can nglyphs != 1 happen at all? Not currently I think + if ( uc < QFontEngineData::widthCacheSize && advances[0] > 0 && advances[0] < 0x100 ) + d->engineData->widthCache[ uc ] = advances[0]; + + return advances[0]; +} + + +int QFontMetrics::charWidth( const QString &str, int pos ) const +{ + if ( pos < 0 || pos > (int)str.length() ) + return 0; + + const QChar &ch = str.unicode()[ pos ]; + if ( ch.unicode() < QFontEngineData::widthCacheSize && + d->engineData && d->engineData->widthCache[ ch.unicode() ] ) + return d->engineData->widthCache[ ch.unicode() ]; + + QFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + int width; + + if ( script >= QFont::Arabic && script <= QFont::Khmer ) { + // complex script shaping. Have to do some hard work + int from = QMAX( 0, pos - 8 ); + int to = QMIN( (int)str.length(), pos + 8 ); + QConstString cstr( str.unicode()+from, to-from); + QTextEngine layout( cstr.string(), d ); + layout.itemize( QTextEngine::WidthOnly ); + width = layout.width( pos-from, 1 ); + } else if ( ::category( ch ) == QChar::Mark_NonSpacing || qIsZeroWidthChar(ch.unicode())) { + width = 0; + } else { + QFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[8]; + advance_t advances[8]; + int nglyphs = 7; + engine->stringToCMap( &ch, 1, glyphs, advances, &nglyphs, FALSE ); + width = advances[0]; + } + if ( ch.unicode() < QFontEngineData::widthCacheSize && width > 0 && width < 0x100 ) + d->engineData->widthCache[ ch.unicode() ] = width; + return width; +} diff --git a/src/kernel/qfontdata_p.h b/src/kernel/qfontdata_p.h new file mode 100644 index 0000000..ab44d8f --- /dev/null +++ b/src/kernel/qfontdata_p.h @@ -0,0 +1,278 @@ +/**************************************************************************** +** +** Definition of internal QFontData struct +** +** Created : 941229 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QFONTDATA_P_H +#define QFONTDATA_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#include "qobject.h" +#include "qfont.h" +#include "qpaintdevicemetrics.h" + +// forwards +class QFontEngine; +class QPaintDevice; + + +struct QFontDef +{ + inline QFontDef() + : pointSize( -1 ), pixelSize( -1 ), + styleHint( QFont::AnyStyle ), styleStrategy( QFont::PreferDefault ), + weight( 50 ), italic( FALSE ), fixedPitch( FALSE ), stretch( 100 ), + ignorePitch(TRUE) +#ifdef Q_WS_MAC + ,fixedPitchComputed(FALSE) +#endif + { + } + + QString family; + +#ifdef Q_WS_X11 + QString addStyle; +#endif // Q_WS_X11 + + int pointSize; + int pixelSize; + + uint styleHint : 8; + uint styleStrategy : 16; + + uint weight : 7; // 0-99 + uint italic : 1; + uint fixedPitch : 1; + uint stretch : 12; // 0-400 + + uint ignorePitch : 1; + uint fixedPitchComputed : 1; // for Mac OS X only + uint reserved : 14; // for future extensions + + bool operator==( const QFontDef &other ) const; + inline bool operator<( const QFontDef &other ) const + { + if ( pixelSize != other.pixelSize ) return pixelSize < other.pixelSize; + if ( weight != other.weight ) return weight < other.weight; + if ( italic != other.italic ) return italic < other.italic; + if ( stretch != other.stretch ) return stretch < other.stretch; + if ( styleHint != other.styleHint ) return styleHint < other.styleHint; + if ( styleStrategy != other.styleStrategy ) return styleStrategy < other.styleStrategy; + if ( family != other.family ) return family < other.family; + +#ifdef Q_WS_X11 + if ( addStyle != other.addStyle ) return addStyle < other.addStyle; +#endif // Q_WS_X11 + + return FALSE; + } +}; + +class QFontEngineData : public QShared +{ +public: + QFontEngineData(); + ~QFontEngineData(); + + uint lineWidth; + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + QFontEngine *engines[QFont::LastPrivateScript]; +#else + QFontEngine *engine; +#endif // Q_WS_X11 || Q_WS_WIN +#ifndef Q_WS_MAC + enum { widthCacheSize = 0x500 }; + uchar widthCache[widthCacheSize]; +#endif +}; + + +class QFontPrivate : public QShared +{ +public: + static QFont::Script defaultScript; +#ifdef Q_WS_X11 + static int defaultEncodingID; +#endif // Q_WS_X11 + + QFontPrivate(); + QFontPrivate( const QFontPrivate &other ); + ~QFontPrivate(); + + void load( QFont::Script script ); + QFontEngine *engineForScript( QFont::Script script ) const { + if ( script == QFont::NoScript ) + script = QFontPrivate::defaultScript; +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + if ( ! engineData || ! engineData->engines[script] ) + ((QFontPrivate *) this)->load( script ); + return engineData->engines[script]; +#else + if ( ! engineData || ! engineData->engine ) + ((QFontPrivate *) this)->load( script ); + return engineData->engine; +#endif // Q_WS_X11 || Q_WS_WIN + } + + QFontDef request; + QFontEngineData *engineData; + QPaintDevice *paintdevice; + int screen; + + uint rawMode : 1; + uint underline : 1; + uint overline : 1; + uint strikeOut : 1; + + enum { + Family = 0x0001, + Size = 0x0002, + StyleHint = 0x0004, + StyleStrategy = 0x0008, + Weight = 0x0010, + Italic = 0x0020, + Underline = 0x0040, + Overline = 0x0080, + StrikeOut = 0x0100, + FixedPitch = 0x0200, + Stretch = 0x0400, + Complete = 0x07ff + }; + + uint mask; + + void resolve( const QFontPrivate *other ); +}; + + +class QFontCache : public QObject +{ +public: + static QFontCache *instance; + + QFontCache(); + ~QFontCache(); + +#ifdef Q_WS_QWS + void clear(); +#endif + // universal key structure. QFontEngineDatas and QFontEngines are cached using + // the same keys + struct Key { + Key() : script(0), screen( 0 ), dpi(0) { } + Key( const QFontDef &d, QFont::Script c, int s, QPaintDevice *pdev ) + : script(c), screen(s) { + def = d; +#ifdef Q_WS_X11 + dpi = pdev ? QPaintDeviceMetrics(pdev).logicalDpiY() : 0; +#else + Q_UNUSED(pdev); + dpi = 0; +#endif + } + + QFontDef def; + int script; + int screen; + int dpi; + + inline bool operator<( const Key &other ) const + { + if ( script != other.script ) return script < other.script; + if ( screen != other.screen ) return screen < other.screen; + if ( dpi != other.dpi ) return dpi < other.dpi; + return def < other.def; + } + inline bool operator==( const Key &other ) const + { return def == other.def && script == other.script && + screen == other.screen && dpi == other.dpi; } + }; + + // QFontEngineData cache + typedef QMap<Key,QFontEngineData*> EngineDataCache; + EngineDataCache engineDataCache; + + QFontEngineData *findEngineData( const Key &key ) const; + void insertEngineData( const Key &key, QFontEngineData *engineData ); + + // QFontEngine cache + struct Engine { + Engine() : data( 0 ), timestamp( 0 ), hits( 0 ) { } + Engine( QFontEngine *d ) : data( d ), timestamp( 0 ), hits( 0 ) { } + + QFontEngine *data; + uint timestamp; + uint hits; + }; + + typedef QMap<Key,Engine> EngineCache; + EngineCache engineCache; + + QFontEngine *findEngine( const Key &key ); + void insertEngine( const Key &key, QFontEngine *engine ); + +#if defined(Q_WS_WIN) || defined(Q_WS_QWS) + void cleanupPrinterFonts(); +#endif + + private: + void increaseCost( uint cost ); + void decreaseCost( uint cost ); + void timerEvent( QTimerEvent *event ); + + static const uint min_cost; + uint total_cost, max_cost; + uint current_timestamp; + bool fast; + int timer_id; +}; + +#endif // QFONTDATA_P_H diff --git a/src/kernel/qfontdatabase.cpp b/src/kernel/qfontdatabase.cpp new file mode 100644 index 0000000..e326657 --- /dev/null +++ b/src/kernel/qfontdatabase.cpp @@ -0,0 +1,2491 @@ +/**************************************************************************** +** +** Implementation of font database class. +** +** Created : 990603 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qfontdatabase.h" + +#ifndef QT_NO_FONTDATABASE + +#include <qtl.h> +#include <qapplication.h> + +#include <private/qunicodetables_p.h> +#include "qfontengine_p.h" + +#include <qcleanuphandler.h> + +#ifdef Q_WS_X11 +#include <locale.h> +#endif +#include <stdlib.h> + +//#define QFONTDATABASE_DEBUG +#ifdef QFONTDATABASE_DEBUG +# define FD_DEBUG qDebug +#else +# define FD_DEBUG if (FALSE) qDebug +#endif + +//#define FONT_MATCH_DEBUG +#ifdef FONT_MATCH_DEBUG +# define FM_DEBUG qDebug +#else +# define FM_DEBUG if (FALSE) qDebug +#endif + +#if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET) +# define for if(0){}else for +#endif + +static int ucstricmp( const QString &as, const QString &bs ) +{ + const QChar *a = as.unicode(); + const QChar *b = bs.unicode(); + if ( a == b ) + return 0; + if ( a == 0 ) + return 1; + if ( b == 0 ) + return -1; + int l=QMIN(as.length(),bs.length()); + while ( l-- && ::lower( *a ) == ::lower( *b ) ) + a++,b++; + if ( l==-1 ) + return ( as.length()-bs.length() ); + return ::lower( *a ).unicode() - ::lower( *b ).unicode(); +} + +static int getFontWeight( const QString &weightString ) +{ + QString s = weightString.lower(); + + // Test in decreasing order of commonness + if (s == "medium" || + s == "normal") + return QFont::Normal; + if (s == "bold") + return QFont::Bold; + if (s == "demibold" || s == "demi bold") + return QFont::DemiBold; + if (s == "black") + return QFont::Black; + if (s == "light") + return QFont::Light; + + if (s.contains("bold")) { + if (s.contains("demi")) + return (int) QFont::DemiBold; + return (int) QFont::Bold; + } + + if (s.contains("light")) + return (int) QFont::Light; + + if (s.contains("black")) + return (int) QFont::Black; + + return (int) QFont::Normal; +} + +#ifdef Q_WS_X11 +struct QtFontEncoding +{ + signed int encoding : 16; + + uint xpoint : 16; + uint xres : 8; + uint yres : 8; + uint avgwidth : 16; + uchar pitch : 8; +}; +#endif // Q_WS_X11 + +struct QtFontSize +{ + unsigned short pixelSize; + +#ifdef Q_WS_X11 + int count; + QtFontEncoding *encodings; + QtFontEncoding *encodingID( int id, uint xpoint = 0, uint xres = 0, + uint yres = 0, uint avgwidth = 0, bool add = FALSE); +#endif // Q_WS_X11 +}; + + +#ifdef Q_WS_X11 +QtFontEncoding *QtFontSize::encodingID( int id, uint xpoint, uint xres, + uint yres, uint avgwidth, bool add ) +{ + // we don't match using the xpoint, xres and yres parameters, only the id + for ( int i = 0; i < count; ++i ) { + if ( encodings[i].encoding == id ) + return encodings + i; + } + + if ( !add ) return 0; + + if ( !(count % 4) ) + encodings = ( QtFontEncoding * ) + realloc( encodings, + (((count+4) >> 2 ) << 2 ) * sizeof( QtFontEncoding ) ); + encodings[count].encoding = id; + encodings[count].xpoint = xpoint; + encodings[count].xres = xres; + encodings[count].yres = yres; + encodings[count].avgwidth = avgwidth; + encodings[count].pitch = '*'; + return encodings + count++; +} +#endif // Q_WS_X11 + +struct QtFontStyle +{ + struct Key { + Key( const QString &styleString ); + Key() : italic( FALSE ), oblique( FALSE ), + weight( QFont::Normal ), stretch( 0 ) { } + Key( const Key &o ) : italic( o.italic ), oblique( o.oblique ), + weight( o.weight ), stretch( o.stretch ) { } + uint italic : 1; + uint oblique : 1; + signed int weight : 8; + signed int stretch : 12; + + bool operator==( const Key & other ) { + return ( italic == other.italic && + oblique == other.oblique && + weight == other.weight && + (stretch == 0 || other.stretch == 0 || stretch == other.stretch) ); + } + bool operator!=( const Key &other ) { + return !operator==(other); + } + bool operator <( const Key &o ) { + int x = (italic << 13) + (oblique << 12) + (weight << 14) + stretch; + int y = (o.italic << 13) + (o.oblique << 12) + (o.weight << 14) + o.stretch; + return ( x < y ); + } + }; + + QtFontStyle( const Key &k ) + : key( k ), bitmapScalable( FALSE ), smoothScalable( FALSE ), + fakeOblique( FALSE ), count( 0 ), pixelSizes( 0 ) + { +#if defined(Q_WS_X11) + weightName = setwidthName = 0; +#endif // Q_WS_X11 + } + + ~QtFontStyle() { +#ifdef Q_WS_X11 + delete [] weightName; + delete [] setwidthName; + while ( count-- ) + free(pixelSizes[count].encodings); +#endif + free( pixelSizes ); + } + + Key key; + bool bitmapScalable : 1; + bool smoothScalable : 1; + bool fakeOblique : 1; + int count : 29; + QtFontSize *pixelSizes; + +#ifdef Q_WS_X11 + const char *weightName; + const char *setwidthName; +#endif // Q_WS_X11 + + QtFontSize *pixelSize( unsigned short size, bool = FALSE ); +}; + +QtFontStyle::Key::Key( const QString &styleString ) + : italic( FALSE ), oblique( FALSE ), weight( QFont::Normal ), stretch( 0 ) +{ + weight = getFontWeight( styleString ); + + if ( styleString.contains( "Italic" ) ) + italic = TRUE; + else if ( styleString.contains( "Oblique" ) ) + oblique = TRUE; +} + +QtFontSize *QtFontStyle::pixelSize( unsigned short size, bool add ) +{ + for ( int i = 0; i < count; i++ ) { + if ( pixelSizes[i].pixelSize == size ) + return pixelSizes + i; + } + if ( !add ) + return 0; + + if ( !(count % 8) ) + pixelSizes = (QtFontSize *) + realloc( pixelSizes, + (((count+8) >> 3 ) << 3) * sizeof(QtFontSize) ); + pixelSizes[count].pixelSize = size; +#ifdef Q_WS_X11 + pixelSizes[count].count = 0; + pixelSizes[count].encodings = 0; +#endif + return pixelSizes + (count++); +} + +struct QtFontFoundry +{ + QtFontFoundry( const QString &n ) : name( n ), count( 0 ), styles( 0 ) {} + ~QtFontFoundry() { + while ( count-- ) + delete styles[count]; + free( styles ); + } + + QString name; + + int count; + QtFontStyle **styles; + QtFontStyle *style( const QtFontStyle::Key &, bool = FALSE ); +}; + +QtFontStyle *QtFontFoundry::style( const QtFontStyle::Key &key, bool create ) +{ + int pos = 0; + if ( count ) { + int low = 0; + int high = count; + pos = count / 2; + while ( high > low ) { + if ( styles[pos]->key == key ) + return styles[pos]; + if ( styles[pos]->key < key ) + low = pos + 1; + else + high = pos; + pos = (high + low) / 2; + }; + pos = low; + } + if ( !create ) + return 0; + +// qDebug("adding key (weight=%d, italic=%d, oblique=%d stretch=%d) at %d", key.weight, key.italic, key.oblique, key.stretch, pos ); + if ( !(count % 8) ) + styles = (QtFontStyle **) + realloc( styles, (((count+8) >> 3 ) << 3) * sizeof( QtFontStyle * ) ); + + memmove( styles + pos + 1, styles + pos, (count-pos)*sizeof(QtFontStyle *) ); + styles[pos] = new QtFontStyle( key ); + count++; + return styles[pos]; +} + + +struct QtFontFamily +{ + enum ScriptStatus { Unknown = 0, Supported = 1, + UnSupported_Xft= 2, UnSupported_Xlfd = 4, UnSupported = 6 }; + + QtFontFamily(const QString &n ) + : +#ifdef Q_WS_X11 + fixedPitch( TRUE ), hasXft( FALSE ), xftScriptCheck( FALSE ), xlfdLoaded( FALSE ), synthetic(FALSE), +#else + fixedPitch( FALSE ), +#endif +#ifdef Q_WS_WIN + scriptCheck( FALSE ), +#endif +#if defined(Q_OS_MAC) && !defined(QWS) + fixedPitchComputed(FALSE), +#endif + fullyLoaded( FALSE ), + name( n ), count( 0 ), foundries( 0 ) { + memset( scripts, 0, sizeof( scripts ) ); + } + ~QtFontFamily() { + while ( count-- ) + delete foundries[count]; + free( foundries ); + } + + bool fixedPitch : 1; +#ifdef Q_WS_X11 + bool hasXft : 1; + bool xftScriptCheck : 1; + bool xlfdLoaded : 1; + bool synthetic : 1; +#endif +#ifdef Q_WS_WIN + bool scriptCheck : 1; +#endif +#if defined(Q_OS_MAC) && !defined(QWS) + bool fixedPitchComputed : 1; +#endif + bool fullyLoaded : 1; + QString name; + QString rawName; +#ifdef Q_WS_X11 + QCString fontFilename; + int fontFileIndex; +#endif +#ifdef Q_WS_MAC + FMFontFamily macFamily; +#endif +#ifdef Q_WS_WIN + QString english_name; +#endif + int count; + QtFontFoundry **foundries; + + unsigned char scripts[QFont::LastPrivateScript]; + + QtFontFoundry *foundry( const QString &f, bool = FALSE ); +}; + +QtFontFoundry *QtFontFamily::foundry( const QString &f, bool create ) +{ + if ( f.isNull() && count == 1 ) + return foundries[0]; + + for ( int i = 0; i < count; i++ ) { + if ( ucstricmp( foundries[i]->name, f ) == 0 ) + return foundries[i]; + } + if ( !create ) + return 0; + + if ( !(count % 8) ) + foundries = (QtFontFoundry **) + realloc( foundries, + (((count+8) >> 3 ) << 3) * sizeof( QtFontFoundry * ) ); + + foundries[count] = new QtFontFoundry( f ); + return foundries[count++]; +} + +class QFontDatabasePrivate { +public: + QFontDatabasePrivate() : count( 0 ), families( 0 ) { } + ~QFontDatabasePrivate() { + while ( count-- ) + delete families[count]; + free( families ); + } + QtFontFamily *family( const QString &f, bool = FALSE ); + + int count; + QtFontFamily **families; +}; + +QtFontFamily *QFontDatabasePrivate::family( const QString &f, bool create ) +{ + int low = 0; + int high = count; + int pos = count / 2; + int res = 1; + if ( count ) { + while ( (res = ucstricmp( families[pos]->name, f )) && pos != low ) { + if ( res > 0 ) + high = pos; + else + low = pos; + pos = (high + low) / 2; + }; + if ( !res ) + return families[pos]; + } + if ( !create ) + return 0; + + if ( res < 0 ) + pos++; + + // qDebug("adding family %s at %d total=%d", f.latin1(), pos, count); + if ( !(count % 8) ) + families = (QtFontFamily **) + realloc( families, + (((count+8) >> 3 ) << 3) * sizeof( QtFontFamily * ) ); + + memmove( families + pos + 1, families + pos, (count-pos)*sizeof(QtFontFamily *) ); + families[pos] = new QtFontFamily( f ); + count++; + return families[pos]; +} + + + + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) +static const unsigned short sample_chars[QFont::LastPrivateScript][14] = +{ + // European Alphabetic Scripts + // Latin, + { 0x0041, 0x0 }, + // Greek, + { 0x0391, 0x0 }, + // Cyrillic, + { 0x0410, 0x0 }, + // Armenian, + { 0x0540, 0x0 }, + // Georgian, + { 0x10d0, 0x0 }, + // Runic, + { 0x16a0, 0x0 }, + // Ogham, + { 0x1680, 0x0 }, + // SpacingModifiers, + { 0x02c6, 0x0 }, + // CombiningMarks, + { 0x0300, 0x0 }, + + // Middle Eastern Scripts + // Hebrew, + { 0x05d0, 0x0 }, + // Arabic, + { 0x0630, 0x0 }, + // Syriac, + { 0x0710, 0x0 }, + // Thaana, + { 0x0780, 0x0 }, + + // South and Southeast Asian Scripts + // Devanagari, + { 0x0910, 0x0 }, + // Bengali, + { 0x0990, 0x0 }, + // Gurmukhi, + { 0x0a10, 0x0 }, + // Gujarati, + { 0x0a90, 0x0 }, + // Oriya, + { 0x0b10, 0x0 }, + // Tamil, + { 0x0b90, 0x0 }, + // Telugu, + { 0x0c10, 0x0 }, + // Kannada, + { 0x0c90, 0x0 }, + // Malayalam, + { 0x0d10, 0x0 }, + // Sinhala, + { 0x0d90, 0x0 }, + // Thai, + { 0x0e10, 0x0 }, + // Lao, + { 0x0e81, 0x0 }, + // Tibetan, + { 0x0f00, 0x0 }, + // Myanmar, + { 0x1000, 0x0 }, + // Khmer, + { 0x1780, 0x0 }, + + // East Asian Scripts + // Han, + { 0x4e00, 0x0 }, + // Hiragana, + { 0x3050, 0x4e00, 0x25EF, 0x3012, 0x3013, 0x30FB, 0x30FC, 0x5CE0, 0 }, + // Katakana, + { 0x30b0, 0x4e00, 0x25EF, 0x3012, 0x3013, 0x30FB, 0x30FC, 0x5CE0, 0 }, + // Hangul, + { 0xac00, 0x0 }, + // Bopomofo, + { 0x3110, 0x0 }, + // Yi, + { 0xa000, 0x0 }, + + // Additional Scripts + // Ethiopic, + { 0x1200, 0x0 }, + // Cherokee, + { 0x13a0, 0x0 }, + // CanadianAboriginal, + { 0x1410, 0x0 }, + // Mongolian, + { 0x1800, 0x0 }, + + // Symbols + // CurrencySymbols, + { 0x20aa, 0x0 }, + // LetterlikeSymbols, + { 0x2103, 0x0 }, + // NumberForms, + { 0x2160, 0x0 }, + // MathematicalOperators, + { 0x222b, 0x0 }, + // TechnicalSymbols, + { 0x2312, 0x0 }, + // GeometricSymbols, + { 0x2500, 0x0 }, + // MiscellaneousSymbols, + { 0x2640, 0x2714, 0x0 }, + // EnclosedAndSquare, + { 0x2460, 0x0 }, + // Braille, + { 0x2800, 0x0 }, + + // Unicode, + { 0xfffd, 0x0 }, + + // some scripts added in Unicode 3.2 + // Tagalog, + { 0x1700, 0x0 }, + // Hanunoo, + { 0x1720, 0x0 }, + // Buhid, + { 0x1740, 0x0 }, + // Tagbanwa, + { 0x1770, 0x0 }, + + // KatakanaHalfWidth + { 0xff65, 0x0 }, + + // Limbu + { 0x1901, 0x0 }, + // TaiLe + { 0x1950, 0x0 }, + + // NScripts + { 0x0000, 0x0 }, + // NoScript + { 0x0000, 0x0 }, + + // Han_Japanese + { 0x4e00, 0x25EF, 0x3012, 0x3013, 0x30FB, 0x5CE0, 0 }, + // Han_SimplifiedChinese, 0x3400 is optional + { 0x4e00, 0x201C, 0x3002, 0x6237, 0x9555, 0xFFE5, 0 }, + // Han_TraditionalChinese, 0xF6B1 is optional + // OR Han_HongkongChinese, 0x3435, 0xE000, 0xF6B1 are optional + { 0x4e00, 0x201C, 0x3002, 0x6236, 0x9F98, 0xFFE5, 0 }, + // Han_Korean + { 0x4e00, 0 } + // Taiwan would be 0x201C, 0x3002, 0x4E00, 0x9F98, 0xFFE5 +}; + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) +static inline bool requiresOpenType(QFont::Script s) +{ + return (s >= QFont::Syriac && s <= QFont::Sinhala) + || (s >= QFont::Myanmar && s <= QFont::Khmer); +} +#endif + +static inline bool canRender( QFontEngine *fe, QFont::Script script ) +{ + if ( !fe ) return FALSE; + + bool hasChar = true; + + if (!sample_chars[script][0]) + hasChar = false; + + int i = 0; + while (hasChar && sample_chars[script][i]){ + QChar sample(sample_chars[script][i]); + if ( !fe->canRender( &sample, 1 ) ) { + hasChar = false; +#ifdef FONT_MATCH_DEBUG + FM_DEBUG(" font has NOT char 0x%04x", sample.unicode() ); + } else { + FM_DEBUG(" font has char 0x%04x", sample.unicode() ); +#endif + } + ++i; + } +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + if (hasChar && requiresOpenType(script)) { + QOpenType *ot = fe->openType(); + if (!ot || !ot->supportsScript(script)) + return FALSE; + } +#endif + + return hasChar; +} +#endif // Q_WS_X11 || Q_WS_WIN + + +static QSingleCleanupHandler<QFontDatabasePrivate> qfontdatabase_cleanup; +static QFontDatabasePrivate *db=0; +#define SMOOTH_SCALABLE 0xffff + +#if defined( Q_WS_X11 ) +# include "qfontdatabase_x11.cpp" +#elif defined( Q_WS_MAC ) +# include "qfontdatabase_mac.cpp" +#elif defined( Q_WS_WIN ) +# include "qfontdatabase_win.cpp" +#elif defined( Q_WS_QWS ) +# include "qfontdatabase_qws.cpp" +#endif + +static QtFontStyle *bestStyle(QtFontFoundry *foundry, const QtFontStyle::Key &styleKey) +{ + int best = 0; + int dist = 0xffff; + + for ( int i = 0; i < foundry->count; i++ ) { + QtFontStyle *style = foundry->styles[i]; + + int d = QABS( styleKey.weight - style->key.weight ); + + if ( styleKey.stretch != 0 && style->key.stretch != 0 ) { + d += QABS( styleKey.stretch - style->key.stretch ); + } + + if ( styleKey.italic ) { + if ( !style->key.italic ) + d += style->key.oblique ? 0x0001 : 0x1000; + } else if ( styleKey.oblique ) { + if (!style->key.oblique ) + d += style->key.italic ? 0x0001 : 0x1000; + } else if ( style->key.italic || style->key.oblique ) { + d += 0x1000; + } + + if ( d < dist ) { + best = i; + dist = d; + } + } + + FM_DEBUG( " best style has distance 0x%x", dist ); + if (!foundry->count) { + QtFontStyle *temp = NULL; + return temp; + } + return foundry->styles[best]; +} + +#if defined(Q_WS_X11) +static QtFontEncoding *findEncoding(QFont::Script script, int styleStrategy, + QtFontSize *size, int force_encoding_id) +{ + QtFontEncoding *encoding = 0; + + if (force_encoding_id >= 0) { + encoding = size->encodingID(force_encoding_id); + if (!encoding) + FM_DEBUG(" required encoding_id not available"); + return encoding; + } + + if (styleStrategy & (QFont::OpenGLCompatible | QFont::PreferBitmap)) { + FM_DEBUG(" PreferBitmap and/or OpenGL set, skipping Xft"); + } else { + encoding = size->encodingID(-1); // -1 == prefer Xft + if (encoding) return encoding; + } + + // Xft not available, find an XLFD font, trying the default encoding first + encoding = size->encodingID(QFontPrivate::defaultEncodingID); + + if (!encoding || !scripts_for_xlfd_encoding[encoding->encoding][script]) { + // find the first encoding that supports the requested script + encoding = 0; + for (int x = 0; !encoding && x < size->count; ++x) { + const int enc = size->encodings[x].encoding; + if (scripts_for_xlfd_encoding[enc][script]) { + encoding = size->encodings + x; + break; + } + } + } + + return encoding; +} +#endif // Q_WS_X11 + + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) +static +unsigned int bestFoundry( QFont::Script script, unsigned int score, int styleStrategy, + const QtFontFamily *family, const QString &foundry_name, + QtFontStyle::Key styleKey, int pixelSize, char pitch, + QtFontFoundry **best_foundry, QtFontStyle **best_style, + QtFontSize **best_size +#ifdef Q_WS_X11 + , QtFontEncoding **best_encoding, int force_encoding_id +#endif + ) +{ + Q_UNUSED( script ); + Q_UNUSED( pitch ); + + FM_DEBUG( " REMARK: looking for best foundry for family '%s'", family->name.latin1() ); + + for ( int x = 0; x < family->count; ++x ) { + QtFontFoundry *foundry = family->foundries[x]; + if ( ! foundry_name.isEmpty() && + ucstricmp( foundry->name, foundry_name ) != 0 ) + continue; + + FM_DEBUG( " looking for matching style in foundry '%s'", + foundry->name.isEmpty() ? "-- none --" : foundry->name.latin1() ); + + QtFontStyle *style = bestStyle(foundry, styleKey); + + if ( ! style->smoothScalable && ( styleStrategy & QFont::ForceOutline ) ) { + FM_DEBUG( " ForceOutline set, but not smoothly scalable" ); + continue; + } + + int px = -1; + QtFontSize *size = 0; + + // 1. see if we have an exact matching size + if (! (styleStrategy & QFont::ForceOutline)) { + size = style->pixelSize(pixelSize); + if (size) { + FM_DEBUG(" found exact size match (%d pixels)", size->pixelSize); + px = size->pixelSize; + } + } + + // 2. see if we have a smoothly scalable font + if (! size && style->smoothScalable && ! (styleStrategy & QFont::PreferBitmap)) { + size = style->pixelSize(SMOOTH_SCALABLE); + if (size) { + FM_DEBUG(" found smoothly scalable font (%d pixels)", pixelSize); + px = pixelSize; + } + } + + // 3. see if we have a bitmap scalable font + if (! size && style->bitmapScalable && (styleStrategy & QFont::PreferMatch)) { + size = style->pixelSize(0); + if (size) { + FM_DEBUG(" found bitmap scalable font (%d pixels)", pixelSize); + px = pixelSize; + } + } + +#ifdef Q_WS_X11 + QtFontEncoding *encoding = 0; +#endif + + // 4. find closest size match + if (! size) { + unsigned int distance = ~0u; + for (int x = 0; x < style->count; ++x) { +#ifdef Q_WS_X11 + encoding = + findEncoding(script, styleStrategy, style->pixelSizes + x, force_encoding_id); + if (!encoding) { + FM_DEBUG(" size %3d does not support the script we want", + style->pixelSizes[x].pixelSize); + continue; + } +#endif + + unsigned int d = QABS(style->pixelSizes[x].pixelSize - pixelSize); + if (d < distance) { + distance = d; + size = style->pixelSizes + x; + FM_DEBUG(" best size so far: %3d (%d)", size->pixelSize, pixelSize); + } + } + + if (!size) { + FM_DEBUG(" no size supports the script we want"); + continue; + } + + if (style->bitmapScalable && ! (styleStrategy & QFont::PreferQuality) && + (distance * 10 / pixelSize) >= 2) { + // the closest size is not close enough, go ahead and + // use a bitmap scaled font + size = style->pixelSize(0); + px = pixelSize; + } else { + px = size->pixelSize; + } + } + +#ifdef Q_WS_X11 + if (size) { + encoding = findEncoding(script, styleStrategy, size, force_encoding_id); + if (!encoding) size = 0; + } + if ( ! encoding ) { + FM_DEBUG( " foundry doesn't support the script we want" ); + continue; + } +#endif // Q_WS_X11 + + unsigned int this_score = 0x0000; + enum { + PitchMismatch = 0x4000, + StyleMismatch = 0x2000, + BitmapScaledPenalty = 0x1000, + EncodingMismatch = 0x0002, + XLFDPenalty = 0x0001 + }; + +#ifdef Q_WS_X11 + if ( encoding->encoding != -1 ) { + this_score += XLFDPenalty; + if ( encoding->encoding != QFontPrivate::defaultEncodingID ) + this_score += EncodingMismatch; + } + if (pitch != '*') { + if ( !( pitch == 'm' && encoding->pitch == 'c' ) && pitch != encoding->pitch ) + this_score += PitchMismatch; + } +#else + // ignore pitch for asian fonts, some of them misreport it, and they are all + // fixed pitch anyway. + if (pitch != '*' && (script <= QFont::NScripts && script != QFont::KatakanaHalfWidth + && (script < QFont::Han || script > QFont::Yi))) { + if ((pitch == 'm' && !family->fixedPitch) + || (pitch == 'p' && family->fixedPitch)) + this_score += PitchMismatch; + } +#endif + if ( styleKey != style->key ) + this_score += StyleMismatch; + if ( !style->smoothScalable && px != size->pixelSize ) // bitmap scaled + this_score += BitmapScaledPenalty; + if (px != pixelSize) // close, but not exact, size match + this_score += QABS(px - pixelSize); + + if ( this_score < score ) { + FM_DEBUG( " found a match: score %x best score so far %x", + this_score, score ); + + score = this_score; + *best_foundry = foundry; + *best_style = style; + *best_size = size; +#ifdef Q_WS_X11 + *best_encoding = encoding; +#endif // Q_WS_X11 + } else { + FM_DEBUG( " score %x no better than best %x", this_score, score); + } + } + + return score; +} + +/*! + \internal +*/ +QFontEngine * +QFontDatabase::findFont( QFont::Script script, const QFontPrivate *fp, + const QFontDef &request, int force_encoding_id ) +{ +#ifndef Q_WS_X11 + Q_UNUSED( force_encoding_id ); +#endif + + if ( !db ) + initializeDb(); + + QFontEngine *fe = 0; + if ( fp ) { + if ( fp->rawMode ) { + fe = loadEngine( script, fp, request, 0, 0, 0 +#ifdef Q_WS_X11 + , 0, 0, FALSE +#endif + ); + + // if we fail to load the rawmode font, use a 12pixel box engine instead + if (! fe) fe = new QFontEngineBox( 12 ); + return fe; + } + + QFontCache::Key key( request, script, +#ifdef Q_WS_WIN + (int)fp->paintdevice, +#else + fp->screen, +#endif + fp->paintdevice + ); + fe = QFontCache::instance->findEngine( key ); + if ( fe ) return fe; + } + +#ifdef Q_WS_WIN + if (request.styleStrategy & QFont::PreferDevice) { + QFontEngine *fe = loadEngine(script, fp, request, 0, 0, 0); + if(fe) + return fe; + } +#endif + + QString family_name, foundry_name; + QtFontStyle::Key styleKey; + styleKey.italic = request.italic; + styleKey.weight = request.weight; + styleKey.stretch = request.stretch; + char pitch = request.ignorePitch ? '*' : request.fixedPitch ? 'm' : 'p'; + + parseFontName( request.family, foundry_name, family_name ); + +#ifdef Q_WS_X11 + if (script == QFont::Han) { + // modify script according to locale + static QFont::Script defaultHan; + QCString locale = setlocale(LC_ALL, NULL); + + if (locale.contains("ko")) + defaultHan = QFont::Han_Korean; + else if (locale.contains("zh_TW") || locale.contains("zh_HK")) + defaultHan = QFont::Han_TraditionalChinese; + else if (locale.contains("zh")) + defaultHan = QFont::Han_SimplifiedChinese; + else if (locale.contains("ja")) + defaultHan = QFont::Han_Japanese; + else + defaultHan = QFont::Han; // don't change + + script = defaultHan; + } +#endif + + FM_DEBUG( "QFontDatabase::findFont\n" + " request:\n" + " family: %s [%s], script: %d (%s)\n" + " weight: %d, italic: %d\n" + " stretch: %d\n" + " pixelSize: %d\n" + " pitch: %c", + family_name.isEmpty() ? "-- first in script --" : family_name.latin1(), + foundry_name.isEmpty() ? "-- any --" : foundry_name.latin1(), + script, scriptName( script ).latin1(), + request.weight, request.italic, request.stretch, request.pixelSize, pitch ); + + bool usesFontConfig = FALSE; +#ifdef QT_XFT2 + if (family_name.isEmpty() + || family_name == "Sans Serif" + || family_name == "Serif" + || family_name == "Monospace") { + fe = loadFontConfigFont(fp, request, script); + usesFontConfig = (fe != 0); + } + if (!fe) +#endif + { + QtFontFamily *best_family = 0; + QtFontFoundry *best_foundry = 0; + QtFontStyle *best_style = 0; + QtFontSize *best_size = 0; +#ifdef Q_WS_X11 + QtFontEncoding *best_encoding = 0; +#endif // Q_WS_X11 + + unsigned int score = ~0; + + load( family_name, script ); + + for ( int x = 0; x < db->count; ++x ) { + QtFontFamily *try_family = db->families[x]; +#ifdef Q_WS_X11 + if (try_family->synthetic) // skip generated fontconfig fonts + continue; +#endif + + if ( !family_name.isEmpty() && + ucstricmp( try_family->name, family_name ) != 0 +#ifdef Q_WS_WIN + && ucstricmp( try_family->english_name, family_name ) != 0 +#endif + ) + continue; + + if ( family_name.isEmpty() ) + load( try_family->name, script ); + + uint score_adjust = 0; + QFont::Script override_script = script; + if ( ! ( try_family->scripts[script] & QtFontFamily::Supported ) + && script != QFont::Unicode) { + // family not supported in the script we want +#ifdef Q_WS_X11 + if (script >= QFont::Han_Japanese && script <= QFont::Han_Korean + && try_family->scripts[QFont::Han] == QtFontFamily::Supported) { + // try with the han script instead, give it a penalty + if (override_script == QFont::Han_TraditionalChinese + && (try_family->scripts[QFont::Han_SimplifiedChinese] & QtFontFamily::Supported)) { + override_script = QFont::Han_SimplifiedChinese; + score_adjust = 200; + } else if (override_script == QFont::Han_SimplifiedChinese + && (try_family->scripts[QFont::Han_TraditionalChinese] & QtFontFamily::Supported)) { + override_script = QFont::Han_TraditionalChinese; + score_adjust = 200; + } else { + override_script = QFont::Han; + score_adjust = 400; + } + } else +#endif + if (family_name.isEmpty()) { + continue; + } else if (try_family->scripts[QFont::UnknownScript] & QtFontFamily::Supported) { + // try with the unknown script (for a symbol font) + override_script = QFont::UnknownScript; +#ifndef QT_XFT2 + } else if (try_family->scripts[QFont::Unicode] & QtFontFamily::Supported) { + // try with the unicode script instead + override_script = QFont::Unicode; +#endif + } else { + // family not supported by unicode/unknown scripts + continue; + } + } + + QtFontFoundry *try_foundry = 0; + QtFontStyle *try_style = 0; + QtFontSize *try_size = 0; +#ifdef Q_WS_X11 + QtFontEncoding *try_encoding = 0; +#endif // Q_WS_X11 + + // as we know the script is supported, we can be sure + // to find a matching font here. + unsigned int newscore = + bestFoundry( override_script, score, request.styleStrategy, + try_family, foundry_name, styleKey, request.pixelSize, pitch, + &try_foundry, &try_style, &try_size +#ifdef Q_WS_X11 + , &try_encoding, force_encoding_id +#endif + ); + if ( try_foundry == 0 ) { + // the specific foundry was not found, so look for + // any foundry matching our requirements + newscore = bestFoundry( override_script, score, request.styleStrategy, try_family, + QString::null, styleKey, request.pixelSize, + pitch, &try_foundry, &try_style, &try_size +#ifdef Q_WS_X11 + , &try_encoding, force_encoding_id +#endif + ); + } + newscore += score_adjust; + + if ( newscore < score ) { + score = newscore; + best_family = try_family; + best_foundry = try_foundry; + best_style = try_style; + best_size = try_size; +#ifdef Q_WS_X11 + best_encoding = try_encoding; +#endif // Q_WS_X11 + } + if ( newscore < 10 ) // xlfd instead of xft... just accept it + break; + } + + if ( best_family != 0 && best_foundry != 0 && best_style != 0 +#ifdef Q_WS_X11 + && best_size != 0 && best_encoding != 0 +#endif + ) { + FM_DEBUG( " BEST:\n" + " family: %s [%s]\n" + " weight: %d, italic: %d, oblique: %d\n" + " stretch: %d\n" + " pixelSize: %d\n" + " pitch: %c\n" + " encoding: %d\n", + best_family->name.latin1(), + best_foundry->name.isEmpty() ? "-- none --" : best_foundry->name.latin1(), + best_style->key.weight, best_style->key.italic, best_style->key.oblique, + best_style->key.stretch, best_size ? best_size->pixelSize : 0xffff, +#ifdef Q_WS_X11 + best_encoding->pitch, best_encoding->encoding +#else + 'p', 0 +#endif + ); + + fe = loadEngine( script, fp, request, best_family, best_foundry, best_style +#ifdef Q_WS_X11 + , best_size, best_encoding, ( force_encoding_id >= 0 ) +#endif + ); + } + if (fe) { + fe->fontDef.family = best_family->name; + if ( ! best_foundry->name.isEmpty() ) { + fe->fontDef.family += QString::fromLatin1( " [" ); + fe->fontDef.family += best_foundry->name; + fe->fontDef.family += QString::fromLatin1( "]" ); + } + + if ( best_style->smoothScalable ) + fe->fontDef.pixelSize = request.pixelSize; + else if ( best_style->bitmapScalable && + ( request.styleStrategy & QFont::PreferMatch ) ) + fe->fontDef.pixelSize = request.pixelSize; + else + fe->fontDef.pixelSize = best_size->pixelSize; + + fe->fontDef.styleHint = request.styleHint; + fe->fontDef.styleStrategy = request.styleStrategy; + + fe->fontDef.weight = best_style->key.weight; + fe->fontDef.italic = best_style->key.italic || best_style->key.oblique; + fe->fontDef.fixedPitch = best_family->fixedPitch; + fe->fontDef.stretch = best_style->key.stretch; + fe->fontDef.ignorePitch = FALSE; + } + } + + if ( fe ) { + if ( script != QFont::Unicode && !canRender( fe, script ) ) { + FM_DEBUG( " WARN: font loaded cannot render a sample char" ); + + delete fe; + fe = 0; + } else if ( fp ) { + QFontDef def = request; + if (def.family.isEmpty()) { + def.family = fp->request.family; + def.family = def.family.left(def.family.find(',')); + } + QFontCache::Key key( def, script, +#ifdef Q_WS_WIN + (int)fp->paintdevice, +#else + fp->screen, +#endif + fp->paintdevice + ); + QFontCache::instance->insertEngine( key, fe ); + if (!usesFontConfig) { + for ( int i = 0; i < QFont::NScripts; ++i ) { + if ( i == script ) continue; + + if (!canRender(fe, (QFont::Script) i)) + continue; + + key.script = i; + QFontCache::instance->insertEngine( key, fe ); + } + } + } + } + + if (!fe) { + if ( !request.family.isEmpty() ) + return 0; + + FM_DEBUG( "returning box engine" ); + + fe = new QFontEngineBox( request.pixelSize ); + fe->fontDef = request; + + if ( fp ) { + QFontCache::Key key( request, script, +#ifdef Q_WS_WIN + (int)fp->paintdevice, +#else + fp->screen, +#endif + fp->paintdevice + ); + QFontCache::instance->insertEngine( key, fe ); + } + } + + if ( fp ) { +#if defined(Q_WS_X11) + fe->fontDef.pointSize = + qRound(10. * qt_pointSize(fe->fontDef.pixelSize, fp->paintdevice, fp->screen)); +#elif defined(Q_WS_WIN) + fe->fontDef.pointSize = int( double( fe->fontDef.pixelSize ) * 720.0 / + GetDeviceCaps(shared_dc,LOGPIXELSY) ); +#else + fe->fontDef.pointSize = int( double( fe->fontDef.pixelSize ) * 720.0 / + 96.0 ); +#endif + } else { + fe->fontDef.pointSize = request.pointSize; + } + + return fe; +} +#endif // Q_WS_X11 || Q_WS_WIN + + + + +static QString styleString( int weight, bool italic, bool oblique ) +{ + QString result; + if ( weight >= QFont::Black ) + result = "Black"; + else if ( weight >= QFont::Bold ) + result = "Bold"; + else if ( weight >= QFont::DemiBold ) + result = "Demi Bold"; + else if ( weight < QFont::Normal ) + result = "Light"; + + if ( italic ) + result += " Italic"; + else if ( oblique ) + result += " Oblique"; + + if ( result.isEmpty() ) + result = "Normal"; + + return result.simplifyWhiteSpace(); +} + +/*! + Returns a string that describes the style of the font \a f. For + example, "Bold Italic", "Bold", "Italic" or "Normal". An empty + string may be returned. +*/ +QString QFontDatabase::styleString( const QFont &f ) +{ + // ### fix oblique here + return ::styleString( f.weight(), f.italic(), FALSE ); +} + + +/*! + \class QFontDatabase qfontdatabase.h + \brief The QFontDatabase class provides information about the fonts available in the underlying window system. + + \ingroup environment + \ingroup graphics + + The most common uses of this class are to query the database for + the list of font families() and for the pointSizes() and styles() + that are available for each family. An alternative to pointSizes() + is smoothSizes() which returns the sizes at which a given family + and style will look attractive. + + If the font family is available from two or more foundries the + foundry name is included in the family name, e.g. "Helvetica + [Adobe]" and "Helvetica [Cronyx]". When you specify a family you + can either use the old hyphenated Qt 2.x "foundry-family" format, + e.g. "Cronyx-Helvetica", or the new bracketed Qt 3.x "family + [foundry]" format e.g. "Helvetica [Cronyx]". If the family has a + foundry it is always returned, e.g. by families(), using the + bracketed format. + + The font() function returns a QFont given a family, style and + point size. + + A family and style combination can be checked to see if it is + italic() or bold(), and to retrieve its weight(). Similarly we can + call isBitmapScalable(), isSmoothlyScalable(), isScalable() and + isFixedPitch(). + + A text version of a style is given by styleString(). + + The QFontDatabase class also supports some static functions, for + example, standardSizes(). You can retrieve the Unicode 3.0 + description of a \link QFont::Script script\endlink using + scriptName(), and a sample of characters in a script with + scriptSample(). + + Example: +\code +#include <qapplication.h> +#include <qfontdatabase.h> +#include <else.h> + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QFontDatabase fdb; + QStringList families = fdb.families(); + for ( QStringList::Iterator f = families.begin(); f != families.end(); ++f ) { + QString family = *f; + qDebug( family ); + QStringList styles = fdb.styles( family ); + for ( QStringList::Iterator s = styles.begin(); s != styles.end(); ++s ) { + QString style = *s; + QString dstyle = "\t" + style + " ("; + QValueList<int> smoothies = fdb.smoothSizes( family, style ); + for ( QValueList<int>::Iterator points = smoothies.begin(); + points != smoothies.end(); ++points ) { + dstyle += QString::number( *points ) + " "; + } + dstyle = dstyle.left( dstyle.length() - 1 ) + ")"; + qDebug( dstyle ); + } + } + return 0; +} +\endcode + This example gets the list of font families, then the list of + styles for each family and the point sizes that are available for + each family/style combination. +*/ +/*! + \obsolete + \fn inline QStringList QFontDatabase::families( bool ) const +*/ +/*! + \obsolete + \fn inline QStringList QFontDatabase::styles( const QString &family, + const QString & ) const +*/ +/*! + \obsolete + \fn inline QValueList<int> QFontDatabase::pointSizes( const QString &family, + const QString &style , + const QString & ) +*/ + +/*! + \obsolete + \fn inline QValueList<int> QFontDatabase::smoothSizes( const QString &family, + const QString &style, + const QString & ) +*/ +/*! + \obsolete + \fn inline QFont QFontDatabase::font( const QString &familyName, + const QString &style, + int pointSize, + const QString &) +*/ +/*! + \obsolete + \fn inline bool QFontDatabase::isBitmapScalable( const QString &family, + const QString &style, + const QString & ) const +*/ + +/*! + \obsolete + \fn inline bool QFontDatabase::isSmoothlyScalable( const QString &family, + const QString &style, + const QString & ) const +*/ + +/*! + \obsolete + \fn inline bool QFontDatabase::isScalable( const QString &family, + const QString &style, + const QString & ) const +*/ + +/*! + \obsolete + \fn inline bool QFontDatabase::isFixedPitch( const QString &family, + const QString &style, + const QString & ) const +*/ + +/*! + \obsolete + \fn inline bool QFontDatabase::italic( const QString &family, + const QString &style, + const QString & ) const +*/ + +/*! + \obsolete + \fn inline bool QFontDatabase::bold( const QString &family, + const QString &style, + const QString & ) const +*/ + +/*! + \obsolete + \fn inline int QFontDatabase::weight( const QString &family, + const QString &style, + const QString & ) const +*/ + + +/*! + Creates a font database object. +*/ +QFontDatabase::QFontDatabase() +{ + createDatabase(); + + d = db; +} + + +/*! Returns a sorted list of the names of the available font families. + + If a family exists in several foundries, the returned name for + that font is in the form "family [foundry]". Examples: "Times + [Adobe]", "Times [Cronyx]", "Palatino". +*/ +QStringList QFontDatabase::families() const +{ + load(); + + QStringList flist; + for ( int i = 0; i < d->count; i++ ) { + QtFontFamily *f = d->families[i]; + if ( f->count == 0 ) + continue; + if ( f->count == 1 ) { + flist.append( f->name ); + } else { + for ( int j = 0; j < f->count; j++ ) { + QString str = f->name; + QString foundry = f->foundries[j]->name; + if ( !foundry.isEmpty() ) { + str += " ["; + str += foundry; + str += "]"; + } + flist.append( str ); + } + } + } + return flist; +} + +/*! + \overload + + Returns a sorted list of the available font families which support + the Unicode script \a script. + + If a family exists in several foundries, the returned name for + that font is in the form "family [foundry]". Examples: "Times + [Adobe]", "Times [Cronyx]", "Palatino". +*/ +QStringList QFontDatabase::families( QFont::Script script ) const +{ + load(); + + QStringList flist; + for ( int i = 0; i < d->count; i++ ) { + QtFontFamily *f = d->families[i]; + if ( f->count == 0 ) + continue; + if (!(f->scripts[script] & QtFontFamily::Supported)) + continue; + if ( f->count == 1 ) { + flist.append( f->name ); + } else { + for ( int j = 0; j < f->count; j++ ) { + QString str = f->name; + QString foundry = f->foundries[j]->name; + if ( !foundry.isEmpty() ) { + str += " ["; + str += foundry; + str += "]"; + } + flist.append( str ); + } + } + } + return flist; +} + +/*! + Returns a list of the styles available for the font family \a + family. Some example styles: "Light", "Light Italic", "Bold", + "Oblique", "Demi". The list may be empty. +*/ +QStringList QFontDatabase::styles( const QString &family ) const +{ + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QStringList l; + QtFontFamily *f = d->family( familyName ); + if ( !f ) + return l; + + QtFontFoundry allStyles( foundryName ); + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) { + QtFontStyle::Key ke( foundry->styles[k]->key ); + ke.stretch = 0; + allStyles.style( ke, TRUE ); + } + } + } + + for ( int i = 0; i < allStyles.count; i++ ) + l.append( ::styleString( allStyles.styles[i]->key.weight, + allStyles.styles[i]->key.italic, + allStyles.styles[i]->key.oblique ) ); + return l; +} + +/*! + Returns TRUE if the font that has family \a family and style \a + style is fixed pitch; otherwise returns FALSE. +*/ + +bool QFontDatabase::isFixedPitch(const QString &family, + const QString &style) const +{ + Q_UNUSED(style); + + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontFamily *f = d->family( familyName ); +#if defined(Q_OS_MAC) && !defined(QWS) + if (f) { + if (!f->fixedPitchComputed) { + QFontMetrics fm(familyName); + f->fixedPitch = fm.width('i') == fm.width('m'); + f->fixedPitchComputed = TRUE; + } + } +#endif + + return ( f && f->fixedPitch ); +} + +/*! + Returns TRUE if the font that has family \a family and style \a + style is a scalable bitmap font; otherwise returns FALSE. Scaling + a bitmap font usually produces an unattractive hardly readable + result, because the pixels of the font are scaled. If you need to + scale a bitmap font it is better to scale it to one of the fixed + sizes returned by smoothSizes(). + + \sa isScalable(), isSmoothlyScalable() +*/ +bool QFontDatabase::isBitmapScalable( const QString &family, + const QString &style) const +{ + bool bitmapScalable = FALSE; + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontStyle::Key styleKey( style ); + + QtFontFamily *f = d->family( familyName ); + if ( !f ) return bitmapScalable; + + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + if ((style.isEmpty() || foundry->styles[k]->key == styleKey) && + foundry->styles[k]->bitmapScalable && !foundry->styles[k]->smoothScalable) { + bitmapScalable = TRUE; + goto end; + } + } + } + end: + return bitmapScalable; +} + + +/*! + Returns TRUE if the font that has family \a family and style \a + style is smoothly scalable; otherwise returns FALSE. If this + function returns TRUE, it's safe to scale this font to any size, + and the result will always look attractive. + + \sa isScalable(), isBitmapScalable() +*/ +bool QFontDatabase::isSmoothlyScalable( const QString &family, + const QString &style) const +{ + bool smoothScalable = FALSE; + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontStyle::Key styleKey( style ); + + QtFontFamily *f = d->family( familyName ); + if ( !f ) return smoothScalable; + + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + if ((style.isEmpty() || foundry->styles[k]->key == styleKey) && foundry->styles[k]->smoothScalable) { + smoothScalable = TRUE; + goto end; + } + } + } + end: + return smoothScalable; +} + +/*! + Returns TRUE if the font that has family \a family and style \a + style is scalable; otherwise returns FALSE. + + \sa isBitmapScalable(), isSmoothlyScalable() +*/ +bool QFontDatabase::isScalable( const QString &family, + const QString &style) const +{ + if ( isSmoothlyScalable( family, style) ) + return TRUE; + + return isBitmapScalable( family, style); +} + + +/*! + Returns a list of the point sizes available for the font that has + family \a family and style \a style. The list may be empty. + + \sa smoothSizes(), standardSizes() +*/ +QValueList<int> QFontDatabase::pointSizes( const QString &family, + const QString &style) +{ +#if defined(Q_WS_MAC) + // windows and macosx are always smoothly scalable + Q_UNUSED( family ); + Q_UNUSED( style ); + return standardSizes(); +#else + bool smoothScalable = FALSE; + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontStyle::Key styleKey( style ); + + QValueList<int> sizes; + + QtFontFamily *fam = d->family( familyName ); + if ( !fam ) return sizes; + + for ( int j = 0; j < fam->count; j++ ) { + QtFontFoundry *foundry = fam->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + QtFontStyle *style = foundry->style( styleKey ); + if ( !style ) continue; + + if ( style->smoothScalable ) { + smoothScalable = TRUE; + goto end; + } + for ( int l = 0; l < style->count; l++ ) { + const QtFontSize *size = style->pixelSizes + l; + + if (size->pixelSize != 0 && size->pixelSize != USHRT_MAX) { +#ifdef Q_WS_X11 + const uint pointSize = qRound(qt_pointSize(size->pixelSize, 0, -1)); +#else + const uint pointSize = size->pixelSize; // embedded uses 72dpi +#endif + if (! sizes.contains(pointSize)) + sizes.append(pointSize); + } + } + } + } + end: + if ( smoothScalable ) + return standardSizes(); + + qHeapSort( sizes ); + return sizes; +#endif +} + +/*! + Returns a QFont object that has family \a family, style \a style + and point size \a pointSize. If no matching font could be created, + a QFont object that uses the application's default font is + returned. +*/ +QFont QFontDatabase::font( const QString &family, const QString &style, + int pointSize) +{ + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontFoundry allStyles( foundryName ); + QtFontFamily *f = d->family( familyName ); + if ( !f ) return QApplication::font(); + + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + QtFontStyle::Key styleKey( style ); + QtFontStyle *s = bestStyle(&allStyles, styleKey); + + if ( !s ) // no styles found? + return QApplication::font(); + return QFont( family, pointSize, s->key.weight, + s->key.italic ? TRUE : s->key.oblique ? TRUE : FALSE ); +} + + +/*! + Returns the point sizes of a font that has family \a family and + style \a style that will look attractive. The list may be empty. + For non-scalable fonts and bitmap scalable fonts, this function + is equivalent to pointSizes(). + + \sa pointSizes(), standardSizes() +*/ +QValueList<int> QFontDatabase::smoothSizes( const QString &family, + const QString &style) +{ +#ifdef Q_WS_WIN + Q_UNUSED( family ); + Q_UNUSED( style ); + return QFontDatabase::standardSizes(); +#else + bool smoothScalable = FALSE; + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontStyle::Key styleKey( style ); + + QValueList<int> sizes; + + QtFontFamily *fam = d->family( familyName ); + if ( !fam ) + return sizes; + + for ( int j = 0; j < fam->count; j++ ) { + QtFontFoundry *foundry = fam->foundries[j]; + if ( foundryName.isEmpty() || + ucstricmp( foundry->name, foundryName ) == 0 ) { + QtFontStyle *style = foundry->style( styleKey ); + if ( !style ) continue; + + if ( style->smoothScalable ) { + smoothScalable = TRUE; + goto end; + } + for ( int l = 0; l < style->count; l++ ) { + const QtFontSize *size = style->pixelSizes + l; + + if ( size->pixelSize != 0 && size->pixelSize != USHRT_MAX ) { +#ifdef Q_WS_X11 + const uint pointSize = qRound(qt_pointSize(size->pixelSize, 0, -1)); +#else + const uint pointSize = size->pixelSize; // embedded uses 72dpi +#endif + if (! sizes.contains(pointSize)) + sizes.append( pointSize ); + } + } + } + } + end: + if ( smoothScalable ) + return QFontDatabase::standardSizes(); + + qHeapSort( sizes ); + return sizes; +#endif +} + + +/*! + Returns a list of standard font sizes. + + \sa smoothSizes(), pointSizes() +*/ +QValueList<int> QFontDatabase::standardSizes() +{ + QValueList<int> ret; + static const unsigned short standard[] = + { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 0 }; + const unsigned short *sizes = standard; + while ( *sizes ) ret << *sizes++; + return ret; +} + + +/*! + Returns TRUE if the font that has family \a family and style \a + style is italic; otherwise returns FALSE. + + \sa weight(), bold() +*/ +bool QFontDatabase::italic( const QString &family, + const QString &style) const +{ + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontFoundry allStyles( foundryName ); + QtFontFamily *f = d->family( familyName ); + if ( !f ) return FALSE; + + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + QtFontStyle::Key styleKey( style ); + QtFontStyle *s = allStyles.style( styleKey ); + return s && s->key.italic; +} + + +/*! + Returns TRUE if the font that has family \a family and style \a + style is bold; otherwise returns FALSE. + + \sa italic(), weight() +*/ +bool QFontDatabase::bold( const QString &family, + const QString &style) const +{ + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontFoundry allStyles( foundryName ); + QtFontFamily *f = d->family( familyName ); + if ( !f ) return FALSE; + + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || + ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + QtFontStyle::Key styleKey( style ); + QtFontStyle *s = allStyles.style( styleKey ); + return s && s->key.weight >= QFont::Bold; +} + + +/*! + Returns the weight of the font that has family \a family and style + \a style. If there is no such family and style combination, + returns -1. + + \sa italic(), bold() +*/ +int QFontDatabase::weight( const QString &family, + const QString &style) const +{ + QString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + QtFontFoundry allStyles( foundryName ); + QtFontFamily *f = d->family( familyName ); + if ( !f ) return -1; + + for ( int j = 0; j < f->count; j++ ) { + QtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || + ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + QtFontStyle::Key styleKey( style ); + QtFontStyle *s = allStyles.style( styleKey ); + return s ? s->key.weight : -1; +} + + +/*! + Returns a string that gives a default description of the \a script + (e.g. for displaying to the user in a dialog). The name matches + the name of the script as defined by the Unicode 3.0 standard. + + \sa QFont::Script +*/ +QString QFontDatabase::scriptName(QFont::Script script) +{ + const char *name = 0; + + switch (script) { + case QFont::Latin: + name = QT_TRANSLATE_NOOP("QFont", "Latin"); + break; + case QFont::Greek: + name = QT_TRANSLATE_NOOP("QFont", "Greek" ); + break; + case QFont::Cyrillic: + name = QT_TRANSLATE_NOOP("QFont", "Cyrillic" ); + break; + case QFont::Armenian: + name = QT_TRANSLATE_NOOP("QFont", "Armenian" ); + break; + case QFont::Georgian: + name = QT_TRANSLATE_NOOP("QFont", "Georgian" ); + break; + case QFont::Runic: + name = QT_TRANSLATE_NOOP("QFont", "Runic" ); + break; + case QFont::Ogham: + name = QT_TRANSLATE_NOOP("QFont", "Ogham" ); + break; + case QFont::SpacingModifiers: + name = QT_TRANSLATE_NOOP("QFont", "SpacingModifiers" ); + break; + case QFont::CombiningMarks: + name = QT_TRANSLATE_NOOP("QFont", "CombiningMarks" ); + break; + case QFont::Hebrew: + name = QT_TRANSLATE_NOOP("QFont", "Hebrew" ); + break; + case QFont::Arabic: + name = QT_TRANSLATE_NOOP("QFont", "Arabic" ); + break; + case QFont::Syriac: + name = QT_TRANSLATE_NOOP("QFont", "Syriac" ); + break; + case QFont::Thaana: + name = QT_TRANSLATE_NOOP("QFont", "Thaana" ); + break; + case QFont::Devanagari: + name = QT_TRANSLATE_NOOP("QFont", "Devanagari" ); + break; + case QFont::Bengali: + name = QT_TRANSLATE_NOOP("QFont", "Bengali" ); + break; + case QFont::Gurmukhi: + name = QT_TRANSLATE_NOOP("QFont", "Gurmukhi" ); + break; + case QFont::Gujarati: + name = QT_TRANSLATE_NOOP("QFont", "Gujarati" ); + break; + case QFont::Oriya: + name = QT_TRANSLATE_NOOP("QFont", "Oriya" ); + break; + case QFont::Tamil: + name = QT_TRANSLATE_NOOP("QFont", "Tamil" ); + break; + case QFont::Telugu: + name = QT_TRANSLATE_NOOP("QFont", "Telugu" ); + break; + case QFont::Kannada: + name = QT_TRANSLATE_NOOP("QFont", "Kannada" ); + break; + case QFont::Malayalam: + name = QT_TRANSLATE_NOOP("QFont", "Malayalam" ); + break; + case QFont::Sinhala: + name = QT_TRANSLATE_NOOP("QFont", "Sinhala" ); + break; + case QFont::Thai: + name = QT_TRANSLATE_NOOP("QFont", "Thai" ); + break; + case QFont::Lao: + name = QT_TRANSLATE_NOOP("QFont", "Lao" ); + break; + case QFont::Tibetan: + name = QT_TRANSLATE_NOOP("QFont", "Tibetan" ); + break; + case QFont::Myanmar: + name = QT_TRANSLATE_NOOP("QFont", "Myanmar" ); + break; + case QFont::Khmer: + name = QT_TRANSLATE_NOOP("QFont", "Khmer" ); + break; + case QFont::Han: + name = QT_TRANSLATE_NOOP("QFont", "Han" ); + break; + case QFont::Hiragana: + name = QT_TRANSLATE_NOOP("QFont", "Hiragana" ); + break; + case QFont::Katakana: + name = QT_TRANSLATE_NOOP("QFont", "Katakana" ); + break; + case QFont::Hangul: + name = QT_TRANSLATE_NOOP("QFont", "Hangul" ); + break; + case QFont::Bopomofo: + name = QT_TRANSLATE_NOOP("QFont", "Bopomofo" ); + break; + case QFont::Yi: + name = QT_TRANSLATE_NOOP("QFont", "Yi" ); + break; + case QFont::Ethiopic: + name = QT_TRANSLATE_NOOP("QFont", "Ethiopic" ); + break; + case QFont::Cherokee: + name = QT_TRANSLATE_NOOP("QFont", "Cherokee" ); + break; + case QFont::CanadianAboriginal: + name = QT_TRANSLATE_NOOP("QFont", "Canadian Aboriginal" ); + break; + case QFont::Mongolian: + name = QT_TRANSLATE_NOOP("QFont", "Mongolian" ); + break; + + case QFont::CurrencySymbols: + name = QT_TRANSLATE_NOOP("QFont", "Currency Symbols" ); + break; + + case QFont::LetterlikeSymbols: + name = QT_TRANSLATE_NOOP("QFont", "Letterlike Symbols" ); + break; + + case QFont::NumberForms: + name = QT_TRANSLATE_NOOP("QFont", "Number Forms" ); + break; + + case QFont::MathematicalOperators: + name = QT_TRANSLATE_NOOP("QFont", "Mathematical Operators" ); + break; + + case QFont::TechnicalSymbols: + name = QT_TRANSLATE_NOOP("QFont", "Technical Symbols" ); + break; + + case QFont::GeometricSymbols: + name = QT_TRANSLATE_NOOP("QFont", "Geometric Symbols" ); + break; + + case QFont::MiscellaneousSymbols: + name = QT_TRANSLATE_NOOP("QFont", "Miscellaneous Symbols" ); + break; + + case QFont::EnclosedAndSquare: + name = QT_TRANSLATE_NOOP("QFont", "Enclosed and Square" ); + break; + + case QFont::Braille: + name = QT_TRANSLATE_NOOP("QFont", "Braille" ); + break; + + case QFont::Unicode: + name = QT_TRANSLATE_NOOP("QFont", "Unicode" ); + break; + + case QFont::Tagalog: + name = QT_TRANSLATE_NOOP( "QFont", "Tagalog" ); + break; + + case QFont::Hanunoo: + name = QT_TRANSLATE_NOOP( "QFont", "Hanunoo" ); + break; + + case QFont::Buhid: + name = QT_TRANSLATE_NOOP( "QFont", "Buhid" ); + break; + + case QFont::Tagbanwa: + name = QT_TRANSLATE_NOOP( "QFont", "Tagbanwa" ); + break; + + case QFont::KatakanaHalfWidth: + name = QT_TRANSLATE_NOOP( "QFont", "Katakana Half-Width Forms" ); + break; + + case QFont::Han_Japanese: + name = QT_TRANSLATE_NOOP( "QFont", "Han (Japanese)" ); + break; + + case QFont::Han_SimplifiedChinese: + name = QT_TRANSLATE_NOOP( "QFont", "Han (Simplified Chinese)" ); + break; + + case QFont::Han_TraditionalChinese: + name = QT_TRANSLATE_NOOP( "QFont", "Han (Traditional Chinese)" ); + break; + + case QFont::Han_Korean: + name = QT_TRANSLATE_NOOP( "QFont", "Han (Korean)" ); + break; + + default: + name = QT_TRANSLATE_NOOP( "QFont", "Unknown Script" ); + break; + } + + return qApp ? qApp->translate("QFont", name) : QString::fromLatin1(name); +} + + +/*! + Returns a string with sample characters from \a script. + + \sa QFont::Script +*/ +QString QFontDatabase::scriptSample(QFont::Script script) +{ + QString sample = "AaBb"; + + switch (script) { + case QFont::Latin: + // This is cheating... we only show latin-1 characters so that we don't + // end up loading lots of fonts - at least on X11... + sample += QChar(0x00C3); + sample += QChar(0x00E1); + sample += "Zz"; + break; + case QFont::Greek: + sample += QChar(0x0393); + sample += QChar(0x03B1); + sample += QChar(0x03A9); + sample += QChar(0x03C9); + break; + case QFont::Cyrillic: + sample += QChar(0x0414); + sample += QChar(0x0434); + sample += QChar(0x0436); + sample += QChar(0x0402); + break; + case QFont::Armenian: + sample += QChar(0x053f); + sample += QChar(0x054f); + sample += QChar(0x056f); + sample += QChar(0x057f); + break; + case QFont::Georgian: + sample += QChar(0x10a0); + sample += QChar(0x10b0); + sample += QChar(0x10c0); + sample += QChar(0x10d0); + break; + case QFont::Runic: + sample += QChar(0x16a0); + sample += QChar(0x16b0); + sample += QChar(0x16c0); + sample += QChar(0x16d0); + break; + case QFont::Ogham: + sample += QChar(0x1681); + sample += QChar(0x1687); + sample += QChar(0x1693); + sample += QChar(0x168d); + break; + + + + case QFont::Hebrew: + sample += QChar(0x05D0); + sample += QChar(0x05D1); + sample += QChar(0x05D2); + sample += QChar(0x05D3); + break; + case QFont::Arabic: + sample += QChar(0x0628); + sample += QChar(0x0629); + sample += QChar(0x062A); + sample += QChar(0x063A); + break; + case QFont::Syriac: + sample += QChar(0x0715); + sample += QChar(0x0725); + sample += QChar(0x0716); + sample += QChar(0x0726); + break; + case QFont::Thaana: + sample += QChar(0x0784); + sample += QChar(0x0794); + sample += QChar(0x078c); + sample += QChar(0x078d); + break; + + + + case QFont::Devanagari: + sample += QChar(0x0905); + sample += QChar(0x0915); + sample += QChar(0x0925); + sample += QChar(0x0935); + break; + case QFont::Bengali: + sample += QChar(0x0986); + sample += QChar(0x0996); + sample += QChar(0x09a6); + sample += QChar(0x09b6); + break; + case QFont::Gurmukhi: + sample += QChar(0x0a05); + sample += QChar(0x0a15); + sample += QChar(0x0a25); + sample += QChar(0x0a35); + break; + case QFont::Gujarati: + sample += QChar(0x0a85); + sample += QChar(0x0a95); + sample += QChar(0x0aa5); + sample += QChar(0x0ab5); + break; + case QFont::Oriya: + sample += QChar(0x0b06); + sample += QChar(0x0b16); + sample += QChar(0x0b2b); + sample += QChar(0x0b36); + break; + case QFont::Tamil: + sample += QChar(0x0b89); + sample += QChar(0x0b99); + sample += QChar(0x0ba9); + sample += QChar(0x0bb9); + break; + case QFont::Telugu: + sample += QChar(0x0c05); + sample += QChar(0x0c15); + sample += QChar(0x0c25); + sample += QChar(0x0c35); + break; + case QFont::Kannada: + sample += QChar(0x0c85); + sample += QChar(0x0c95); + sample += QChar(0x0ca5); + sample += QChar(0x0cb5); + break; + case QFont::Malayalam: + sample += QChar(0x0d05); + sample += QChar(0x0d15); + sample += QChar(0x0d25); + sample += QChar(0x0d35); + break; + case QFont::Sinhala: + sample += QChar(0x0d90); + sample += QChar(0x0da0); + sample += QChar(0x0db0); + sample += QChar(0x0dc0); + break; + case QFont::Thai: + sample += QChar(0x0e02); + sample += QChar(0x0e12); + sample += QChar(0x0e22); + sample += QChar(0x0e32); + break; + case QFont::Lao: + sample += QChar(0x0e8d); + sample += QChar(0x0e9d); + sample += QChar(0x0ead); + sample += QChar(0x0ebd); + break; + case QFont::Tibetan: + sample += QChar(0x0f00); + sample += QChar(0x0f01); + sample += QChar(0x0f02); + sample += QChar(0x0f03); + break; + case QFont::Myanmar: + sample += QChar(0x1000); + sample += QChar(0x1001); + sample += QChar(0x1002); + sample += QChar(0x1003); + break; + case QFont::Khmer: + sample += QChar(0x1780); + sample += QChar(0x1790); + sample += QChar(0x17b0); + sample += QChar(0x17c0); + break; + + + + case QFont::Han: + sample += QChar(0x6f84); + sample += QChar(0x820a); + sample += QChar(0x61a9); + sample += QChar(0x9781); + break; + case QFont::Hiragana: + sample += QChar(0x3050); + sample += QChar(0x3060); + sample += QChar(0x3070); + sample += QChar(0x3080); + break; + case QFont::Katakana: + sample += QChar(0x30b0); + sample += QChar(0x30c0); + sample += QChar(0x30d0); + sample += QChar(0x30e0); + break; + case QFont::Hangul: + sample += QChar(0xac00); + sample += QChar(0xac11); + sample += QChar(0xac1a); + sample += QChar(0xac2f); + break; + case QFont::Bopomofo: + sample += QChar(0x3105); + sample += QChar(0x3115); + sample += QChar(0x3125); + sample += QChar(0x3129); + break; + case QFont::Yi: + sample += QChar(0xa1a8); + sample += QChar(0xa1a6); + sample += QChar(0xa200); + sample += QChar(0xa280); + break; + + + + case QFont::Ethiopic: + sample += QChar(0x1200); + sample += QChar(0x1240); + sample += QChar(0x1280); + sample += QChar(0x12c0); + break; + case QFont::Cherokee: + sample += QChar(0x13a0); + sample += QChar(0x13b0); + sample += QChar(0x13c0); + sample += QChar(0x13d0); + break; + case QFont::CanadianAboriginal: + sample += QChar(0x1410); + sample += QChar(0x1500); + sample += QChar(0x15f0); + sample += QChar(0x1650); + break; + case QFont::Mongolian: + sample += QChar(0x1820); + sample += QChar(0x1840); + sample += QChar(0x1860); + sample += QChar(0x1880); + break; + + + case QFont::CurrencySymbols: + case QFont::LetterlikeSymbols: + case QFont::NumberForms: + case QFont::MathematicalOperators: + case QFont::TechnicalSymbols: + case QFont::GeometricSymbols: + case QFont::MiscellaneousSymbols: + case QFont::EnclosedAndSquare: + case QFont::Braille: + break; + + + case QFont::Unicode: + sample += QChar(0x0174); + sample += QChar(0x0628); + sample += QChar(0x0e02); + sample += QChar(0x263A); + sample += QChar(0x3129); + sample += QChar(0x61a9); + sample += QChar(0xac2f); + break; + + + + default: + sample += QChar(0xfffd); + sample += QChar(0xfffd); + sample += QChar(0xfffd); + sample += QChar(0xfffd); + break; + } + + return sample; +} + + + + +/*! + \internal + + This makes sense of the font family name: + + 1) if the family name contains a '-' (ie. "Adobe-Courier"), then we + split at the '-', and use the string as the foundry, and the string to + the right as the family + + 2) if the family name contains a '[' and a ']', then we take the text + between the square brackets as the foundry, and the text before the + square brackets as the family (ie. "Arial [Monotype]") +*/ +void QFontDatabase::parseFontName(const QString &name, QString &foundry, QString &family) +{ + if ( name.contains('-') ) { + int i = name.find('-'); + foundry = name.left( i ); + family = name.right( name.length() - i - 1 ); + } else if ( name.contains('[') && name.contains(']')) { + int i = name.find('['); + int li = name.findRev(']'); + + if (i < li) { + foundry = name.mid(i + 1, li - i - 1); + if (name[i - 1] == ' ') + i--; + family = name.left(i); + } + } else { + foundry = QString::null; + family = name; + } +} + +#endif // QT_NO_FONTDATABASE diff --git a/src/kernel/qfontdatabase.h b/src/kernel/qfontdatabase.h new file mode 100644 index 0000000..2ccb314 --- /dev/null +++ b/src/kernel/qfontdatabase.h @@ -0,0 +1,227 @@ +/**************************************************************************** +** +** Definition of the QFontDatabase class +** +** Created : 981126 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QFONTDATABASE_H +#define QFONTDATABASE_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qstring.h" +#include "qstringlist.h" +#include "qfont.h" +#include "qvaluelist.h" +#endif // QT_H + + +#ifndef QT_NO_FONTDATABASE + +class QFontStylePrivate; /* Don't touch! */ +struct QtFontStyle; +struct QtFontFamily; +struct QtFontFoundry; +struct QFontDef; +class QFontEngine; +#ifdef Q_WS_QWS +class QDiskFont; +#endif + +class QFontDatabasePrivate; + +class Q_EXPORT QFontDatabase +{ +public: + static QValueList<int> standardSizes(); + + QFontDatabase(); + + QStringList families() const; + QStringList families( QFont::Script ) const; + QStringList styles( const QString & ) const; + QValueList<int> pointSizes( const QString &, const QString & = QString::null); + QValueList<int> smoothSizes( const QString &, const QString &); + QString styleString( const QFont &); + + QFont font( const QString &, const QString &, int); + + bool isBitmapScalable( const QString &, const QString & = QString::null) const; + bool isSmoothlyScalable( const QString &, const QString & = QString::null) const; + bool isScalable( const QString &, const QString & = QString::null) const; + bool isFixedPitch( const QString &, const QString & = QString::null) const; + + bool italic( const QString &, const QString &) const; + bool bold( const QString &, const QString &) const; + int weight( const QString &, const QString &) const; + + static QString scriptName(QFont::Script); + static QString scriptSample(QFont::Script); + +#ifdef Q_WS_QWS + static void qwsAddDiskFont( QDiskFont *qdf ); +#endif + + // For source compatibility with < 3.0 +#ifndef QT_NO_COMPAT + + QStringList families(bool) const; + QStringList styles( const QString &, const QString & ) const; + QValueList<int> pointSizes( const QString &, const QString &, const QString & ); + QValueList<int> smoothSizes( const QString &, const QString &, const QString & ); + + QFont font( const QString &, const QString &, int, const QString &); + + bool isBitmapScalable( const QString &, const QString &, const QString & ) const; + bool isSmoothlyScalable( const QString &, const QString &, const QString & ) const; + bool isScalable( const QString &, const QString &, const QString & ) const; + bool isFixedPitch( const QString &, const QString &, const QString & ) const; + + bool italic( const QString &, const QString &, const QString & ) const; + bool bold( const QString &, const QString &, const QString & ) const; + int weight( const QString &, const QString &, const QString & ) const; + +#endif // QT_NO_COMPAT + +private: +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + static QFontEngine *findFont( QFont::Script script, const QFontPrivate *fp, + const QFontDef &request, int force_encoding_id = -1 ); +#endif // Q_WS_X11 + + static void createDatabase(); + + static void parseFontName(const QString &name, QString &foundry, QString &family); + + friend struct QFontDef; + friend class QFontPrivate; + friend class QFontDialog; + friend class QFontEngineLatinXLFD; + + QFontDatabasePrivate *d; +}; + + +#ifndef QT_NO_COMPAT + +inline QStringList QFontDatabase::families( bool ) const +{ + return families(); +} + +inline QStringList QFontDatabase::styles( const QString &family, + const QString & ) const +{ + return styles(family); +} + +inline QValueList<int> QFontDatabase::pointSizes( const QString &family, + const QString &style , + const QString & ) +{ + return pointSizes(family, style); +} + +inline QValueList<int> QFontDatabase::smoothSizes( const QString &family, + const QString &style, + const QString & ) +{ + return smoothSizes(family, style); +} + +inline QFont QFontDatabase::font( const QString &familyName, + const QString &style, + int pointSize, + const QString &) +{ + return font(familyName, style, pointSize); +} + +inline bool QFontDatabase::isBitmapScalable( const QString &family, + const QString &style, + const QString & ) const +{ + return isBitmapScalable(family, style); +} + +inline bool QFontDatabase::isSmoothlyScalable( const QString &family, + const QString &style, + const QString & ) const +{ + return isSmoothlyScalable(family, style); +} + +inline bool QFontDatabase::isScalable( const QString &family, + const QString &style, + const QString & ) const +{ + return isScalable(family, style); +} + +inline bool QFontDatabase::isFixedPitch( const QString &family, + const QString &style, + const QString & ) const +{ + return isFixedPitch(family, style); +} + +inline bool QFontDatabase::italic( const QString &family, + const QString &style, + const QString & ) const +{ + return italic(family, style); +} + +inline bool QFontDatabase::bold( const QString &family, + const QString &style, + const QString & ) const +{ + return bold(family, style); +} + +inline int QFontDatabase::weight( const QString &family, + const QString &style, + const QString & ) const +{ + return weight(family, style); +} + +#endif // QT_NO_COMPAT + +#endif // QT_NO_FONTDATABASE + +#endif // QFONTDATABASE_H diff --git a/src/kernel/qfontdatabase_x11.cpp b/src/kernel/qfontdatabase_x11.cpp new file mode 100644 index 0000000..05bdbb8 --- /dev/null +++ b/src/kernel/qfontdatabase_x11.cpp @@ -0,0 +1,2014 @@ +/**************************************************************************** +** +** Implementation of platform specific QFontDatabase +** +** Created : 970521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 <qplatformdefs.h> + +#include <qdatetime.h> +#include <qpaintdevicemetrics.h> + +#include "qt_x11_p.h" + +#include <ctype.h> +#include <stdlib.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/mman.h> + +#ifndef QT_NO_XFTFREETYPE +#include <ft2build.h> +#include FT_FREETYPE_H +#endif + +#ifndef QT_XFT2 +#define FcBool Bool +#define FcTrue True +#define FcFalse False +#endif + +#ifdef QFONTDATABASE_DEBUG +# define FD_DEBUG qDebug +#else +# define FD_DEBUG if (FALSE) qDebug +#endif // QFONTDATABASE_DEBUG + +// from qfont_x11.cpp +extern double qt_pointSize(double pixelSize, QPaintDevice *paintdevice, int screen); +extern double qt_pixelSize(double pointSize, QPaintDevice *paintdevice, int screen); + + +static inline void capitalize ( char *s ) +{ + bool space = TRUE; + while( *s ) { + if ( space ) + *s = toupper( *s ); + space = ( *s == ' ' ); + ++s; + } +} + + +// ----- begin of generated code ----- + +#define make_tag( c1, c2, c3, c4 ) \ +( (((unsigned int)c1)<<24) | (((unsigned int)c2)<<16) | \ +(((unsigned int)c3)<<8) | ((unsigned int)c4) ) + +struct XlfdEncoding { + const char *name; + int id; + int mib; + unsigned int hash1; + unsigned int hash2; +}; + +static const XlfdEncoding xlfd_encoding[] = { + { "iso8859-1", 0, 4, make_tag('i','s','o','8'), make_tag('5','9','-','1') }, + { "iso8859-2", 1, 5, make_tag('i','s','o','8'), make_tag('5','9','-','2') }, + { "iso8859-3", 2, 6, make_tag('i','s','o','8'), make_tag('5','9','-','3') }, + { "iso8859-4", 3, 7, make_tag('i','s','o','8'), make_tag('5','9','-','4') }, + { "iso8859-9", 4, 12, make_tag('i','s','o','8'), make_tag('5','9','-','9') }, + { "iso8859-10", 5, 13, make_tag('i','s','o','8'), make_tag('9','-','1','0') }, + { "iso8859-13", 6, 109, make_tag('i','s','o','8'), make_tag('9','-','1','3') }, + { "iso8859-14", 7, 110, make_tag('i','s','o','8'), make_tag('9','-','1','4') }, + { "iso8859-15", 8, 111, make_tag('i','s','o','8'), make_tag('9','-','1','5') }, + { "hp-roman8", 9, 2004, make_tag('h','p','-','r'), make_tag('m','a','n','8') }, + { "jisx0208*-0", 10, 63, make_tag('j','i','s','x'), 0 }, +#define LAST_LATIN_ENCODING 10 + { "iso8859-5", 11, 8, make_tag('i','s','o','8'), make_tag('5','9','-','5') }, + { "*-cp1251", 12, 2251, 0, make_tag('1','2','5','1') }, + { "koi8-ru", 13, 2084, make_tag('k','o','i','8'), make_tag('8','-','r','u') }, + { "koi8-u", 14, 2088, make_tag('k','o','i','8'), make_tag('i','8','-','u') }, + { "koi8-r", 15, 2084, make_tag('k','o','i','8'), make_tag('i','8','-','r') }, + { "iso8859-7", 16, 10, make_tag('i','s','o','8'), make_tag('5','9','-','7') }, + { "iso10646-1", 17, 0, make_tag('i','s','o','1'), make_tag('4','6','-','1') }, + { "iso8859-8", 18, 85, make_tag('i','s','o','8'), make_tag('5','9','-','8') }, + { "gb18030-0", 19, -114, make_tag('g','b','1','8'), make_tag('3','0','-','0') }, + { "gb18030.2000-0", 20, -113, make_tag('g','b','1','8'), make_tag('0','0','-','0') }, + { "gbk-0", 21, -113, make_tag('g','b','k','-'), make_tag('b','k','-','0') }, + { "gb2312.*-0", 22, 57, make_tag('g','b','2','3'), 0 }, + { "jisx0201*-0", 23, 15, make_tag('j','i','s','x'), 0 }, + { "ksc5601*-*", 24, 36, make_tag('k','s','c','5'), 0 }, + { "big5hkscs-0", 25, -2101, make_tag('b','i','g','5'), make_tag('c','s','-','0') }, + { "hkscs-1", 26, -2101, make_tag('h','k','s','c'), make_tag('c','s','-','1') }, + { "big5*-*", 27, -2026, make_tag('b','i','g','5'), 0 }, + { "tscii-*", 28, 2028, make_tag('t','s','c','i'), 0 }, + { "tis620*-*", 29, 2259, make_tag('t','i','s','6'), 0 }, + { "iso8859-11", 30, 2259, make_tag('i','s','o','8'), make_tag('9','-','1','1') }, + { "mulelao-1", 31, -4242, make_tag('m','u','l','e'), make_tag('a','o','-','1') }, + { "ethiopic-unicode", 32, 0, make_tag('e','t','h','i'), make_tag('c','o','d','e') }, + { "unicode-*", 33, 0, make_tag('u','n','i','c'), 0 }, + { "*-symbol", 34, 0, 0, make_tag('m','b','o','l') }, + { "*-fontspecific", 35, 0, 0, make_tag('i','f','i','c') }, + { "fontspecific-*", 36, 0, make_tag('f','o','n','t'), 0 }, + { 0, 0, 0, 0, 0 } +}; + +static const char scripts_for_xlfd_encoding[37][61] = { + // iso8859-1 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-2 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-3 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-4 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-9 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-10 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-13 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-14 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-15 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // hp-roman8 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // jisx0208*-0 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0 }, + // iso8859-5 + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // *-cp1251 + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // koi8-ru + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // koi8-u + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // koi8-r + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-7 + { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso10646-1 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-8 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // gb18030-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // gb18030.2000-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // gbk-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // gb2312.*-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // jisx0201*-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // ksc5601*-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1 }, + // big5hkscs-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0 }, + // hkscs-1 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0 }, + // big5*-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0 }, + // tscii-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // tis620*-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-11 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // mulelao-1 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // ethiopic-unicode + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // unicode-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // *-symbol + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0 }, + // *-fontspecific + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0 }, + // fontspecific-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0 } + +}; + +// ----- end of generated code ----- + + +const int numEncodings = sizeof( xlfd_encoding ) / sizeof( XlfdEncoding ) - 1; + +int qt_xlfd_encoding_id( const char *encoding ) +{ + // qDebug("looking for encoding id for '%s'", encoding ); + int len = strlen( encoding ); + if ( len < 4 ) + return -1; + unsigned int hash1 = make_tag( encoding[0], encoding[1], encoding[2], encoding[3] ); + const char *ch = encoding + len - 4; + unsigned int hash2 = make_tag( ch[0], ch[1], ch[2], ch[3] ); + + const XlfdEncoding *enc = xlfd_encoding; + for ( ; enc->name; ++enc ) { + if ( (enc->hash1 && enc->hash1 != hash1) || + (enc->hash2 && enc->hash2 != hash2) ) + continue; + // hashes match, do a compare if strings match + // the enc->name can contain '*'s we have to interpret correctly + const char *n = enc->name; + const char *e = encoding; + while ( 1 ) { + // qDebug("bol: *e='%c', *n='%c'", *e, *n ); + if ( *e == '\0' ) { + if ( *n ) + break; + // qDebug( "found encoding id %d", enc->id ); + return enc->id; + } + if ( *e == *n ) { + ++e; + ++n; + continue; + } + if ( *n != '*' ) + break; + ++n; + // qDebug("skip: *e='%c', *n='%c'", *e, *n ); + while ( *e && *e != *n ) + ++e; + } + } + // qDebug( "couldn't find encoding %s", encoding ); + return -1; +} + +int qt_mib_for_xlfd_encoding( const char *encoding ) +{ + int id = qt_xlfd_encoding_id( encoding ); + if ( id != -1 ) return xlfd_encoding[id].mib; + return 0; +} + +int qt_encoding_id_for_mib( int mib ) +{ + const XlfdEncoding *enc = xlfd_encoding; + for ( ; enc->name; ++enc ) { + if ( enc->mib == mib ) + return enc->id; + } + return -1; +} + +static const char * xlfd_for_id( int id ) +{ + // special case: -1 returns the "*-*" encoding, allowing us to do full + // database population in a single X server round trip. + if ( id < 0 || id > numEncodings ) + return "*-*"; + return xlfd_encoding[id].name; +} + +enum XLFDFieldNames { + Foundry, + Family, + Weight, + Slant, + Width, + AddStyle, + PixelSize, + PointSize, + ResolutionX, + ResolutionY, + Spacing, + AverageWidth, + CharsetRegistry, + CharsetEncoding, + NFontFields +}; + +// Splits an X font name into fields separated by '-' +static bool parseXFontName( char *fontName, char **tokens ) +{ + if ( ! fontName || fontName[0] == '0' || fontName[0] != '-' ) { + tokens[0] = 0; + return FALSE; + } + + int i; + ++fontName; + for ( i = 0; i < NFontFields && fontName && fontName[0]; ++i ) { + tokens[i] = fontName; + for ( ;; ++fontName ) { + if ( *fontName == '-' ) + break; + if ( ! *fontName ) { + fontName = 0; + break; + } + } + + if ( fontName ) *fontName++ = '\0'; + } + + if ( i < NFontFields ) { + for ( int j = i ; j < NFontFields; ++j ) + tokens[j] = 0; + return FALSE; + } + + return TRUE; +} + +static inline bool isZero(char *x) +{ + return (x[0] == '0' && x[1] == 0); +} + +static inline bool isScalable( char **tokens ) +{ + return (isZero(tokens[PixelSize]) && + isZero(tokens[PointSize]) && + isZero(tokens[AverageWidth])); +} + +static inline bool isSmoothlyScalable( char **tokens ) +{ + return (isZero(tokens[ResolutionX]) && + isZero(tokens[ResolutionY])); +} + +static inline bool isFixedPitch( char **tokens ) +{ + return (tokens[Spacing][0] == 'm' || + tokens[Spacing][0] == 'c' || + tokens[Spacing][0] == 'M' || + tokens[Spacing][0] == 'C'); +} + +/* + Fills in a font definition (QFontDef) from an XLFD (X Logical Font + Description). + + Returns TRUE if the the given xlfd is valid. The fields lbearing + and rbearing are not given any values. +*/ +bool qt_fillFontDef( const QCString &xlfd, QFontDef *fd, int screen ) +{ + char *tokens[NFontFields]; + QCString buffer = xlfd.copy(); + if ( ! parseXFontName(buffer.data(), tokens) ) + return FALSE; + + capitalize(tokens[Family]); + capitalize(tokens[Foundry]); + + fd->family = QString::fromLatin1(tokens[Family]); + QString foundry = QString::fromLatin1(tokens[Foundry]); + if ( ! foundry.isEmpty() && foundry != QString::fromLatin1("*") ) + fd->family += + QString::fromLatin1(" [") + foundry + QString::fromLatin1("]"); + + if ( qstrlen( tokens[AddStyle] ) > 0 ) + fd->addStyle = QString::fromLatin1(tokens[AddStyle]); + else + fd->addStyle = QString::null; + + fd->pointSize = atoi(tokens[PointSize]); + fd->styleHint = QFont::AnyStyle; // ### any until we match families + + char slant = tolower( (uchar) tokens[Slant][0] ); + fd->italic = ( slant == 'o' || slant == 'i' ); + char fixed = tolower( (uchar) tokens[Spacing][0] ); + fd->fixedPitch = ( fixed == 'm' || fixed == 'c' ); + fd->weight = getFontWeight( tokens[Weight] ); + + int r = atoi(tokens[ResolutionY]); + fd->pixelSize = atoi(tokens[PixelSize]); + // not "0" or "*", or required DPI + if ( r && fd->pixelSize && QPaintDevice::x11AppDpiY( screen ) && + r != QPaintDevice::x11AppDpiY( screen ) ) { + // calculate actual pointsize for display DPI + fd->pointSize = qRound(qt_pointSize(fd->pixelSize, 0, screen) * 10.); + } else if ( fd->pixelSize == 0 && fd->pointSize ) { + // calculate pixel size from pointsize/dpi + fd->pixelSize = qRound(qt_pixelSize(fd->pointSize / 10., 0, screen)); + } + + return TRUE; +} + +/* + Fills in a font definition (QFontDef) from the font properties in an + XFontStruct. + + Returns TRUE if the QFontDef could be filled with properties from + the XFontStruct. The fields lbearing and rbearing are not given any + values. +*/ +static bool qt_fillFontDef( XFontStruct *fs, QFontDef *fd, int screen ) +{ + unsigned long value; + if ( fs && !XGetFontProperty( fs, XA_FONT, &value ) ) + return FALSE; + + char *n = XGetAtomName( QPaintDevice::x11AppDisplay(), value ); + QCString xlfd( n ); + if ( n ) + XFree( n ); + return qt_fillFontDef( xlfd.lower(), fd, screen ); +} + + +static QtFontStyle::Key getStyle( char ** tokens ) +{ + QtFontStyle::Key key; + + char slant0 = tolower( (uchar) tokens[Slant][0] ); + + if ( slant0 == 'r' ) { + if ( tokens[Slant][1]) { + char slant1 = tolower( (uchar) tokens[Slant][1] ); + + if ( slant1 == 'o' ) + key.oblique = TRUE; + else if ( slant1 == 'i' ) + key.italic = TRUE; + } + } else if ( slant0 == 'o' ) + key.oblique = TRUE; + else if ( slant0 == 'i' ) + key.italic = TRUE; + + key.weight = getFontWeight( tokens[Weight] ); + + if ( qstrcmp( tokens[Width], "normal" ) == 0 ) { + key.stretch = 100; + } else if ( qstrcmp( tokens[Width], "semi condensed" ) == 0 || + qstrcmp( tokens[Width], "semicondensed" ) == 0 ) { + key.stretch = 90; + } else if ( qstrcmp( tokens[Width], "condensed" ) == 0 ) { + key.stretch = 80; + } else if ( qstrcmp( tokens[Width], "narrow" ) == 0 ) { + key.stretch = 60; + } + + return key; +} + + +extern bool qt_has_xft; // defined in qfont_x11.cpp + +static bool xlfdsFullyLoaded = FALSE; +static unsigned char encodingLoaded[numEncodings]; + +static void loadXlfds( const char *reqFamily, int encoding_id ) +{ + QtFontFamily *fontFamily = reqFamily ? db->family( reqFamily ) : 0; + + // make sure we don't load twice + if ( (encoding_id == -1 && xlfdsFullyLoaded) || (encoding_id != -1 && encodingLoaded[encoding_id]) ) + return; + if ( fontFamily && fontFamily->xlfdLoaded ) + return; + +#ifdef QT_XFT2 + if ( !qt_has_xft ) { +#endif // QT_XFT2 + int fontCount; + // force the X server to give us XLFDs + QCString xlfd_pattern = "-*-"; + xlfd_pattern += reqFamily ? reqFamily : "*"; + xlfd_pattern += "-*-*-*-*-*-*-*-*-*-*-"; + xlfd_pattern += xlfd_for_id( encoding_id ); + + char **fontList = XListFonts( QPaintDevice::x11AppDisplay(), + xlfd_pattern.data(), + 0xffff, &fontCount ); + // qDebug("requesting xlfd='%s', got %d fonts", xlfd_pattern.data(), fontCount ); + + + char *tokens[NFontFields]; + + for( int i = 0 ; i < fontCount ; i++ ) { + if ( ! parseXFontName( fontList[i], tokens ) ) continue; + + // get the encoding_id for this xlfd. we need to do this + // here, since we can pass -1 to this function to do full + // database population + *(tokens[CharsetEncoding]-1) = '-'; + int encoding_id = qt_xlfd_encoding_id( tokens[CharsetRegistry] ); + if ( encoding_id == -1 ) + continue; + + char *familyName = tokens[Family]; + capitalize( familyName ); + char *foundryName = tokens[Foundry]; + capitalize( foundryName ); + QtFontStyle::Key styleKey = getStyle( tokens ); + + bool smooth_scalable = FALSE; + bool bitmap_scalable = FALSE; + if ( isScalable(tokens) ) { + if ( isSmoothlyScalable( tokens ) ) + smooth_scalable = TRUE; + else + bitmap_scalable = TRUE; + } + uint pixelSize = atoi( tokens[PixelSize] ); + uint xpointSize = atoi( tokens[PointSize] ); + uint xres = atoi( tokens[ResolutionX] ); + uint yres = atoi( tokens[ResolutionY] ); + uint avgwidth = atoi( tokens[AverageWidth] ); + bool fixedPitch = isFixedPitch( tokens ); + + if (avgwidth == 0 && pixelSize != 0) { + /* + Ignore bitmap scalable fonts that are automatically + generated by some X servers. We know they are bitmap + scalable because even though they have a specified pixel + size, the average width is zero. + */ + continue; + } + + QtFontFamily *family = fontFamily ? fontFamily : db->family( familyName, TRUE ); + family->fontFileIndex = -1; + QtFontFoundry *foundry = family->foundry( foundryName, TRUE ); + QtFontStyle *style = foundry->style( styleKey, TRUE ); + + delete [] style->weightName; + style->weightName = qstrdup( tokens[Weight] ); + delete [] style->setwidthName; + style->setwidthName = qstrdup( tokens[Width] ); + + if ( smooth_scalable ) { + style->smoothScalable = TRUE; + style->bitmapScalable = FALSE; + pixelSize = SMOOTH_SCALABLE; + } + if ( !style->smoothScalable && bitmap_scalable ) + style->bitmapScalable = TRUE; + if ( !fixedPitch ) + family->fixedPitch = FALSE; + + QtFontSize *size = style->pixelSize( pixelSize, TRUE ); + QtFontEncoding *enc = + size->encodingID( encoding_id, xpointSize, xres, yres, avgwidth, TRUE ); + enc->pitch = *tokens[Spacing]; + if ( !enc->pitch ) enc->pitch = '*'; + + for ( int script = 0; script < QFont::LastPrivateScript; ++script ) { + if ( scripts_for_xlfd_encoding[encoding_id][script] ) + family->scripts[script] = QtFontFamily::Supported; + else + family->scripts[script] |= QtFontFamily::UnSupported_Xlfd; + } + if ( encoding_id == -1 ) + family->xlfdLoaded = TRUE; + } + if ( !reqFamily ) { + // mark encoding as loaded + if ( encoding_id == -1 ) + xlfdsFullyLoaded = TRUE; + else + encodingLoaded[encoding_id] = TRUE; + } + + XFreeFontNames( fontList ); + +#ifdef QT_XFT2 + } +#endif // QT_XFT2 +} + +#ifndef QT_NO_XFTFREETYPE +static int getXftWeight(int xftweight) +{ + int qtweight = QFont::Black; + if (xftweight <= (XFT_WEIGHT_LIGHT + XFT_WEIGHT_MEDIUM) / 2) + qtweight = QFont::Light; + else if (xftweight <= (XFT_WEIGHT_MEDIUM + XFT_WEIGHT_DEMIBOLD) / 2) + qtweight = QFont::Normal; + else if (xftweight <= (XFT_WEIGHT_DEMIBOLD + XFT_WEIGHT_BOLD) / 2) + qtweight = QFont::DemiBold; + else if (xftweight <= (XFT_WEIGHT_BOLD + XFT_WEIGHT_BLACK) / 2) + qtweight = QFont::Bold; + + return qtweight; +} + +static void loadXft() +{ + if (!qt_has_xft) + return; + +#ifdef QT_XFT2 + struct XftDefaultFont { + const char *qtname; + const char *rawname; + bool fixed; + }; + const XftDefaultFont defaults[] = { + { "Serif", "serif", FALSE }, + { "Sans Serif", "sans-serif", FALSE }, + { "Monospace", "monospace", TRUE }, + { 0, 0, FALSE } + }; + const XftDefaultFont *f = defaults; + while (f->qtname) { + QtFontFamily *family = db->family( f->qtname, TRUE ); + family->fixedPitch = f->fixed; + family->rawName = f->rawname; + family->hasXft = TRUE; + family->synthetic = TRUE; + QtFontFoundry *foundry + = family->foundry( QString::null, TRUE ); + + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) { + if (i == QFont::UnknownScript) + continue; + family->scripts[i] = QtFontFamily::Supported; + } + + QtFontStyle::Key styleKey; + styleKey.oblique = FALSE; + for (int i = 0; i < 4; ++i) { + styleKey.italic = (i%2); + styleKey.weight = (i > 1) ? QFont::Bold : QFont::Normal; + QtFontStyle *style = foundry->style( styleKey, TRUE ); + style->smoothScalable = TRUE; + QtFontSize *size = style->pixelSize( SMOOTH_SCALABLE, TRUE ); + QtFontEncoding *enc = size->encodingID( -1, 0, 0, 0, 0, TRUE ); + enc->pitch = (f->fixed ? 'm' : 'p'); + } + ++f; + } +#endif +} + +#ifdef XFT_MATRIX +static void checkXftMatrix( QtFontFamily* family ) { + for ( int j = 0; j < family->count; ++j ) { // each foundry + QtFontFoundry *foundry = family->foundries[j]; + for ( int k = 0; k < foundry->count; ++k ) { + QtFontStyle *style = foundry->styles[k]; + if ( style->key.italic || style->key.oblique ) continue; + + QtFontSize *size = style->pixelSize( SMOOTH_SCALABLE ); + if ( ! size ) continue; + QtFontEncoding *enc = size->encodingID( -1, 0, 0, 0, 0, TRUE ); + if ( ! enc ) continue; + + QtFontStyle::Key key = style->key; + + // does this style have an italic equivalent? + key.italic = TRUE; + QtFontStyle *equiv = foundry->style( key ); + if ( equiv ) continue; + + // does this style have an oblique equivalent? + key.italic = FALSE; + key.oblique = TRUE; + equiv = foundry->style( key ); + if ( equiv ) continue; + + // let's fake one... + equiv = foundry->style( key, TRUE ); + equiv->fakeOblique = TRUE; + equiv->smoothScalable = TRUE; + + QtFontSize *equiv_size = equiv->pixelSize( SMOOTH_SCALABLE, TRUE ); + QtFontEncoding *equiv_enc = equiv_size->encodingID( -1, 0, 0, 0, 0, TRUE ); + + // keep the same pitch + equiv_enc->pitch = enc->pitch; + } + } +} +#endif // XFT_MATRIX + +static bool loadXftFont( FcPattern* font ) +{ + QString familyName; + QString rawName; + char *value; + int weight_value; + int slant_value; + int spacing_value; + char *file_value; + int index_value; + char *foundry_value = 0; + FcBool scalable = FcTrue; + + if (XftPatternGetString( font, + XFT_FAMILY, 0, &value) != XftResultMatch ) + return false; + // capitalize( value ); + rawName = familyName = QString::fromUtf8(value); + familyName.replace('-', ' '); + familyName.replace("/", ""); + + slant_value = XFT_SLANT_ROMAN; + weight_value = XFT_WEIGHT_MEDIUM; + spacing_value = XFT_PROPORTIONAL; + file_value = 0; + index_value = 0; + XftPatternGetInteger (font, XFT_SLANT, 0, &slant_value); + XftPatternGetInteger (font, XFT_WEIGHT, 0, &weight_value); + XftPatternGetInteger (font, XFT_SPACING, 0, &spacing_value); + XftPatternGetString (font, XFT_FILE, 0, &file_value); + XftPatternGetInteger (font, XFT_INDEX, 0, &index_value); +#ifdef QT_XFT2 + FcPatternGetBool(font, FC_SCALABLE, 0, &scalable); + foundry_value = 0; + XftPatternGetString(font, FC_FOUNDRY, 0, &foundry_value); +#endif + QtFontFamily *family = db->family( familyName, TRUE ); + family->rawName = rawName; + family->hasXft = TRUE; + +#ifdef QT_XFT2 + FcCharSet *charset = 0; + FcResult res = FcPatternGetCharSet(font, FC_CHARSET, 0, &charset); + if (res == FcResultMatch && FcCharSetCount(charset) > 1) { + for (int i = 0; i < QFont::LastPrivateScript; ++i) { + bool supported = sample_chars[i][0]; + for (int j = 0; sample_chars[i][j]; ++j){ + if (!FcCharSetHasChar(charset, sample_chars[i][j])) { + supported = false; + break; + } + } + if ( supported ){ + family->scripts[i] = QtFontFamily::Supported; + } else { + family->scripts[i] |= QtFontFamily::UnSupported_Xft; + } + } + family->xftScriptCheck = TRUE; + } else { + // we set UnknownScript to supported for symbol fonts. It makes no sense to merge these + // with other ones, as they are special in a way. + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) + family->scripts[i] |= QtFontFamily::UnSupported_Xft; + family->scripts[QFont::UnknownScript] = QtFontFamily::Supported; + } +#endif // QT_XFT2 + + QCString file = (file_value ? file_value : ""); + family->fontFilename = file; + family->fontFileIndex = index_value; + + QtFontStyle::Key styleKey; + styleKey.italic = (slant_value == XFT_SLANT_ITALIC); + styleKey.oblique = (slant_value == XFT_SLANT_OBLIQUE); + styleKey.weight = getXftWeight( weight_value ); +#ifdef QT_XFT2 + if (!scalable) { + int width = 100; +#if FC_VERSION >= 20193 + XftPatternGetInteger (font, FC_WIDTH, 0, &width); +#endif + styleKey.stretch = width; + } +#endif + + QtFontFoundry *foundry + = family->foundry( foundry_value ? QString::fromUtf8(foundry_value) : QString::null, TRUE ); + QtFontStyle *style = foundry->style( styleKey, TRUE ); + + if (spacing_value < XFT_MONO ) + family->fixedPitch = FALSE; + + QtFontSize *size; + if (scalable) { + style->smoothScalable = TRUE; + size = style->pixelSize( SMOOTH_SCALABLE, TRUE ); + } +#ifdef QT_XFT2 + else { + double pixel_size = 0; + XftPatternGetDouble (font, FC_PIXEL_SIZE, 0, &pixel_size); + size = style->pixelSize( (int)pixel_size, TRUE ); + } +#endif + QtFontEncoding *enc = size->encodingID( -1, 0, 0, 0, 0, TRUE ); + enc->pitch = ( spacing_value >= XFT_CHARCELL ? 'c' : + ( spacing_value >= XFT_MONO ? 'm' : 'p' ) ); + + checkXftMatrix( family ); + + return true; +} + +#ifndef QT_XFT2 + +#define MAKE_TAG( _x1, _x2, _x3, _x4 ) \ + ( ( (Q_UINT32)_x1 << 24 ) | \ + ( (Q_UINT32)_x2 << 16 ) | \ + ( (Q_UINT32)_x3 << 8 ) | \ + (Q_UINT32)_x4 ) + +#ifdef _POSIX_MAPPED_FILES +static inline Q_UINT32 getUInt(unsigned char *p) +{ + Q_UINT32 val; + val = *p++ << 24; + val |= *p++ << 16; + val |= *p++ << 8; + val |= *p; + + return val; +} + +static inline Q_UINT16 getUShort(unsigned char *p) +{ + Q_UINT16 val; + val = *p++ << 8; + val |= *p; + + return val; +} + +static inline void tag_to_string( char *string, Q_UINT32 tag ) +{ + string[0] = (tag >> 24)&0xff; + string[1] = (tag >> 16)&0xff; + string[2] = (tag >> 8)&0xff; + string[3] = tag&0xff; + string[4] = 0; +} + +static Q_UINT16 getGlyphIndex( unsigned char *table, Q_UINT16 format, unsigned short unicode ) +{ + if ( format == 0 ) { + if ( unicode < 256 ) + return (int) *(table+6+unicode); + } else if ( format == 2 ) { + qWarning("format 2 encoding table for Unicode, not implemented!"); + } else if ( format == 4 ) { + Q_UINT16 segCountX2 = getUShort( table + 6 ); + unsigned char *ends = table + 14; + Q_UINT16 endIndex = 0; + int i = 0; + for ( ; i < segCountX2/2 && (endIndex = getUShort( ends + 2*i )) < unicode; i++ ); + + unsigned char *idx = ends + segCountX2 + 2 + 2*i; + Q_UINT16 startIndex = getUShort( idx ); + + if ( startIndex > unicode ) + return 0; + + idx += segCountX2; + Q_INT16 idDelta = (Q_INT16)getUShort( idx ); + idx += segCountX2; + Q_UINT16 idRangeoffset_t = (Q_UINT16)getUShort( idx ); + + Q_UINT16 glyphIndex; + if ( idRangeoffset_t ) { + Q_UINT16 id = getUShort( idRangeoffset_t + 2*(unicode - startIndex) + idx); + if ( id ) + glyphIndex = ( idDelta + id ) % 0x10000; + else + glyphIndex = 0; + } else { + glyphIndex = (idDelta + unicode) % 0x10000; + } + return glyphIndex; + } + + return 0; +} +#endif // _POSIX_MAPPED_FILES + +static inline void checkXftCoverage( QtFontFamily *family ) +{ +#ifdef _POSIX_MAPPED_FILES + QCString ext = family->fontFilename.mid( family->fontFilename.findRev( '.' ) ).lower(); + if ( family->fontFileIndex == 0 && ( ext == ".ttf" || ext == ".otf" ) ) { + void *map; + // qDebug("using own ttf code coverage checking of '%s'!", family->name.latin1() ); + int fd = open( family->fontFilename.data(), O_RDONLY ); + size_t pagesize = getpagesize(); + off_t offset = 0; + size_t length = (8192 / pagesize + 1) * pagesize; + + if ( fd == -1 ) + goto xftCheck; + { + if ( (map = mmap( 0, length, PROT_READ, MAP_SHARED, fd, offset ) ) == MAP_FAILED ) + goto error; + + unsigned char *ttf = (unsigned char *)map; + Q_UINT32 version = getUInt( ttf ); + if ( version != 0x00010000 ) { + // qDebug("file has wrong version %x", version ); + goto error1; + } + Q_UINT16 numTables = getUShort( ttf+4 ); + + unsigned char *table_dir = ttf + 12; + Q_UINT32 cmap_offset = 0; + Q_UINT32 cmap_length = 0; + for ( int n = 0; n < numTables; n++ ) { + Q_UINT32 tag = getUInt( table_dir + 16*n ); + if ( tag == MAKE_TAG( 'c', 'm', 'a', 'p' ) ) { + cmap_offset = getUInt( table_dir + 16*n + 8 ); + cmap_length = getUInt( table_dir + 16*n + 12 ); + break; + } + } + if ( !cmap_offset ) { + // qDebug("no cmap found" ); + goto error1; + } + + if ( cmap_offset + cmap_length > length ) { + munmap( map, length ); + offset = cmap_offset / pagesize * pagesize; + cmap_offset -= offset; + length = (cmap_offset + cmap_length); + if ( (map = mmap( 0, length, PROT_READ, MAP_SHARED, fd, offset ) ) == MAP_FAILED ) + goto error; + } + + unsigned char *cmap = ((unsigned char *)map) + cmap_offset; + + version = getUShort( cmap ); + if ( version != 0 ) { + // qDebug("wrong cmap version" ); + goto error1; + } + numTables = getUShort( cmap + 2 ); + unsigned char *unicode_table = 0; + bool symbol_table = TRUE; + for ( int n = 0; n < numTables; n++ ) { + Q_UINT32 version = getUInt( cmap + 4 + 8*n ); + // accept both symbol and Unicode encodings. prefer unicode. + if ( version == 0x00030001 || version == 0x00030000 ) { + unicode_table = cmap + getUInt( cmap + 4 + 8*n + 4 ); + if ( version == 0x00030001 ) { + symbol_table = FALSE; + break; + } + } + } + + if ( !unicode_table ) { + // qDebug("no unicode table found" ); + goto error1; + } + + Q_UINT16 format = getUShort( unicode_table ); + if ( format != 4 ) + goto error1; + + if (symbol_table) { + // we set UnknownScript to supported for symbol fonts. It makes no sense to merge these + // with other ones, as they are special in a way. + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) + family->scripts[i] |= QtFontFamily::UnSupported_Xft; + family->scripts[QFont::UnknownScript] = QtFontFamily::Supported; + } else { + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) { + + bool supported = sample_chars[i][0]; + for (int j = 0; sample_chars[i][j]; ++j) { + if (!getGlyphIndex(unicode_table, format, sample_chars[i][j])) { + supported=false; + break; + } + } + if ( supported ){ + // qDebug("font can render script %d", i ); + family->scripts[i] = QtFontFamily::Supported; + } else { + family->scripts[i] |= QtFontFamily::UnSupported_Xft; + } + } + } + family->xftScriptCheck = TRUE; + } + error1: + munmap( map, length ); + error: + close( fd ); + if ( family->xftScriptCheck ) + return; + } + xftCheck: +#endif // _POSIX_MAPPED_FILES + + FD_DEBUG("using Freetype for checking of '%s'", family->name.latin1() ); + + FT_Library ft_lib; + FT_Error error = FT_Init_FreeType( &ft_lib ); + if ( error ) return; + FT_Face face; + error = FT_New_Face( ft_lib, family->fontFilename, family->fontFileIndex, &face ); + if ( error ) return; + + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) { + bool supported = sample_chars[i][j]; + for (int j = 0; sample_chars[i][j]; ++j){ + if (!FT_Get_Char_Index(face, sample_chars[i][j])) { + supported=false; + break; + } + } + if ( supported ){ + FD_DEBUG("font can render char %04x, %04x script %d '%s'", + ch.unicode(), FT_Get_Char_Index ( face, ch.unicode() ), + i, QFontDatabase::scriptName( (QFont::Script)i ).latin1() ); + + family->scripts[i] = QtFontFamily::Supported; + } else { + family->scripts[i] |= QtFontFamily::UnSupported_Xft; + } + } + FT_Done_Face( face ); + FT_Done_FreeType( ft_lib ); + family->xftScriptCheck = TRUE; +} +#endif // QT_XFT2 +#endif // QT_NO_XFTFREETYPE + +static void load( const QString &family = QString::null, int script = -1 ) +{ +#ifdef QFONTDATABASE_DEBUG + QTime t; + t.start(); +#endif + + if ( family.isNull() ) { +#ifndef QT_NO_XFTFREETYPE + static bool xft_readall_done = false; + if (qt_has_xft && !xft_readall_done) { + xft_readall_done = true; + XftFontSet *fonts = + XftListFonts(QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppScreen(), + (const char *)0, + XFT_FAMILY, XFT_WEIGHT, XFT_SLANT, + XFT_SPACING, XFT_FILE, XFT_INDEX, +#ifdef QT_XFT2 + FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, +#if FC_VERSION >= 20193 + FC_WIDTH, +#endif +#endif // QT_XFT2 + (const char *)0); + for (int i = 0; i < fonts->nfont; i++) + loadXftFont( fonts->fonts[i] ); + XftFontSetDestroy (fonts); + } +#ifdef QT_XFT2 + if (qt_has_xft) + return; +#endif +#endif // QT_NO_XFTFREETYPE + if ( script == -1 ) + loadXlfds( 0, -1 ); + else { + for ( int i = 0; i < numEncodings; i++ ) { + if ( scripts_for_xlfd_encoding[i][script] ) + loadXlfds( 0, i ); + } + } + } else { + QtFontFamily *f = db->family( family, TRUE ); + if ( !f->fullyLoaded ) { + +#ifndef QT_NO_XFTFREETYPE + if (qt_has_xft) { + QString mfamily = family; + redo: + XftFontSet *fonts = + XftListFonts(QPaintDevice::x11AppDisplay(), + QPaintDevice::x11AppScreen(), + XFT_FAMILY, XftTypeString, mfamily.utf8().data(), + (const char *)0, + XFT_FAMILY, XFT_WEIGHT, XFT_SLANT, + XFT_SPACING, XFT_FILE, XFT_INDEX, +#ifdef QT_XFT2 + FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, +#if FC_VERSION >= 20193 + FC_WIDTH, +#endif +#endif // QT_XFT2 + (const char *)0); + for (int i = 0; i < fonts->nfont; i++) + loadXftFont( fonts->fonts[i] ); + XftFontSetDestroy (fonts); + if (mfamily.contains(' ')) { + mfamily.replace(QChar(' '), QChar('-')); + goto redo; + } + f->fullyLoaded = TRUE; +#ifdef QT_XFT2 + return; +#endif + } +#ifndef QT_XFT2 + // need to check Xft coverage + if ( f->hasXft && !f->xftScriptCheck ) { + checkXftCoverage( f ); + } +#endif +#endif // QT_NO_XFTFREETYPE + // could reduce this further with some more magic: + // would need to remember the encodings loaded for the family. + if ( ( script == -1 && !f->xlfdLoaded ) || + ( !f->hasXft && !(f->scripts[script] & QtFontFamily::Supported) && + !(f->scripts[script] & QtFontFamily::UnSupported_Xlfd) ) ) { + loadXlfds( family, -1 ); + f->fullyLoaded = TRUE; + } + } + } + +#ifdef QFONTDATABASE_DEBUG + FD_DEBUG("QFontDatabase: load( %s, %d) took %d ms", family.latin1(), script, t.elapsed() ); +#endif +} + + +static void initializeDb() +{ + if ( db ) return; + db = new QFontDatabasePrivate; + qfontdatabase_cleanup.set(&db); + +#ifndef QT_XFT2 + memset( encodingLoaded, FALSE, sizeof( encodingLoaded ) ); +#endif + + QTime t; + t.start(); + +#ifndef QT_NO_XFTFREETYPE + loadXft(); + FD_DEBUG("QFontDatabase: loaded Xft: %d ms", t.elapsed() ); +#endif + + t.start(); + +#ifndef QT_NO_XFTFREETYPE + for ( int i = 0; i < db->count; i++ ) { +#ifndef QT_XFT2 + checkXftCoverage( db->families[i] ); + FD_DEBUG("QFontDatabase: xft coverage check: %d ms", t.elapsed() ); +#endif // QT_XFT2 + +#ifdef XFT_MATRIX + checkXftMatrix( db->families[i] ); +#endif // XFT_MATRIX + } +#endif + + +#ifdef QFONTDATABASE_DEBUG +#ifdef QT_XFT2 + if (!qt_has_xft) +#endif + // load everything at startup in debug mode. + loadXlfds( 0, -1 ); + + // print the database + for ( int f = 0; f < db->count; f++ ) { + QtFontFamily *family = db->families[f]; + FD_DEBUG("'%s' %s hasXft=%s", family->name.latin1(), (family->fixedPitch ? "fixed" : ""), + (family->hasXft ? "yes" : "no") ); + for ( int i = 0; i < QFont::LastPrivateScript; ++i ) { + FD_DEBUG("\t%s: %s", QFontDatabase::scriptName((QFont::Script) i).latin1(), + ((family->scripts[i] & QtFontFamily::Supported) ? "Supported" : + (family->scripts[i] & QtFontFamily::UnSupported) == QtFontFamily::UnSupported ? + "UnSupported" : "Unknown")); + } + + for ( int fd = 0; fd < family->count; fd++ ) { + QtFontFoundry *foundry = family->foundries[fd]; + FD_DEBUG("\t\t'%s'", foundry->name.latin1() ); + for ( int s = 0; s < foundry->count; s++ ) { + QtFontStyle *style = foundry->styles[s]; + FD_DEBUG("\t\t\tstyle: italic=%d oblique=%d (fake=%d) weight=%d (%s)\n" + "\t\t\tstretch=%d (%s)", + style->key.italic, style->key.oblique, style->fakeOblique, style->key.weight, + style->weightName, style->key.stretch, + style->setwidthName ? style->setwidthName : "nil" ); + if ( style->smoothScalable ) + FD_DEBUG("\t\t\t\tsmooth scalable" ); + else if ( style->bitmapScalable ) + FD_DEBUG("\t\t\t\tbitmap scalable" ); + if ( style->pixelSizes ) { + qDebug("\t\t\t\t%d pixel sizes", style->count ); + for ( int z = 0; z < style->count; ++z ) { + QtFontSize *size = style->pixelSizes + z; + for ( int e = 0; e < size->count; ++e ) { + FD_DEBUG( "\t\t\t\t size %5d pitch %c encoding %s", + size->pixelSize, + size->encodings[e].pitch, + xlfd_for_id( size->encodings[e].encoding ) ); + } + } + } + } + } + } +#endif // QFONTDATABASE_DEBUG +} + +void QFontDatabase::createDatabase() +{ + initializeDb(); +} + + +// -------------------------------------------------------------------------------------- +// font loader +// -------------------------------------------------------------------------------------- +#define MAXFONTSIZE_XFT 256 +#define MAXFONTSIZE_XLFD 128 +#ifndef QT_NO_XFTFREETYPE +static double addPatternProps(XftPattern *pattern, const QtFontStyle::Key &key, bool fakeOblique, + bool smoothScalable, const QFontPrivate *fp, const QFontDef &request) +{ + int weight_value = XFT_WEIGHT_BLACK; + if ( key.weight == 0 ) + weight_value = XFT_WEIGHT_MEDIUM; + else if ( key.weight < (QFont::Light + QFont::Normal) / 2 ) + weight_value = XFT_WEIGHT_LIGHT; + else if ( key.weight < (QFont::Normal + QFont::DemiBold) / 2 ) + weight_value = XFT_WEIGHT_MEDIUM; + else if ( key.weight < (QFont::DemiBold + QFont::Bold) / 2 ) + weight_value = XFT_WEIGHT_DEMIBOLD; + else if ( key.weight < (QFont::Bold + QFont::Black) / 2 ) + weight_value = XFT_WEIGHT_BOLD; + XftPatternAddInteger( pattern, XFT_WEIGHT, weight_value ); + + int slant_value = XFT_SLANT_ROMAN; + if ( key.italic ) + slant_value = XFT_SLANT_ITALIC; + else if ( key.oblique && !fakeOblique ) + slant_value = XFT_SLANT_OBLIQUE; + XftPatternAddInteger( pattern, XFT_SLANT, slant_value ); + + /* + Xft1 doesn't obey user settings for turning off anti-aliasing using + the following: + + match any size > 6 size < 12 edit antialias = false; + + ... if we request pixel sizes. so, work around this limitiation and + convert the pixel size to a point size and request that. + */ + double size_value = request.pixelSize; + double scale = 1.; + if ( size_value > MAXFONTSIZE_XFT ) { + scale = (double)size_value/(double)MAXFONTSIZE_XFT; + size_value = MAXFONTSIZE_XFT; + } + + size_value = size_value*72./QPaintDevice::x11AppDpiY(fp->screen); + XftPatternAddDouble( pattern, XFT_SIZE, size_value ); + +#ifdef XFT_MATRIX +# ifdef QT_XFT2 + if (!smoothScalable) { +# if FC_VERSION >= 20193 + int stretch = request.stretch; + if (!stretch) + stretch = 100; + XftPatternAddInteger(pattern, FC_WIDTH, stretch); +# endif + } else +# endif + if ( ( request.stretch > 0 && request.stretch != 100 ) || + ( key.oblique && fakeOblique ) ) { + XftMatrix matrix; + XftMatrixInit( &matrix ); + + if ( request.stretch > 0 && request.stretch != 100 ) + XftMatrixScale( &matrix, double( request.stretch ) / 100.0, 1.0 ); + if ( key.oblique && fakeOblique ) + XftMatrixShear( &matrix, 0.20, 0.0 ); + + XftPatternAddMatrix( pattern, XFT_MATRIX, &matrix ); + } +#endif // XFT_MATRIX + if (request.styleStrategy & (QFont::PreferAntialias|QFont::NoAntialias)) { + XftPatternDel(pattern, XFT_ANTIALIAS); + XftPatternAddBool(pattern, XFT_ANTIALIAS, + !(request.styleStrategy & QFont::NoAntialias)); + } + + return scale; +} +#endif // QT_NO_XFTFREETYPE + +static +QFontEngine *loadEngine( QFont::Script script, + const QFontPrivate *fp, const QFontDef &request, + QtFontFamily *family, QtFontFoundry *foundry, + QtFontStyle *style, QtFontSize *size, + QtFontEncoding *encoding, bool forced_encoding ) +{ + Q_UNUSED(script); + + if ( fp && fp->rawMode ) { + QCString xlfd = request.family.latin1(); + FM_DEBUG( "Loading XLFD (rawmode) '%s'", xlfd.data() ); + + XFontStruct *xfs; + if (! (xfs = XLoadQueryFont(QPaintDevice::x11AppDisplay(), xlfd.data() ) ) ) + return 0; + + QFontEngine *fe = new QFontEngineXLFD( xfs, xlfd.data(), 0 ); + if ( ! qt_fillFontDef( xfs, &fe->fontDef, QPaintDevice::x11AppScreen() ) && + ! qt_fillFontDef( xlfd, &fe->fontDef, QPaintDevice::x11AppScreen() ) ) + fe->fontDef = QFontDef(); + + return fe; + } + +#ifndef QT_NO_XFTFREETYPE + if ( encoding->encoding == -1 ) { + + FM_DEBUG( " using Xft" ); + + XftPattern *pattern = XftPatternCreate(); + if ( !pattern ) return 0; + + bool symbol = (family->scripts[QFont::UnknownScript] == QtFontFamily::Supported); +# ifdef QT_XFT2 + if (!symbol && script != QFont::Unicode) { + FcCharSet *cs = FcCharSetCreate(); + for ( int j=0; sample_chars[script][j]; j++ ) + FcCharSetAddChar(cs, sample_chars[script][j]); + if (script == QFont::Latin) + // add Euro character + FcCharSetAddChar(cs, 0x20ac); + FcPatternAddCharSet(pattern, FC_CHARSET, cs); + FcCharSetDestroy(cs); + } +# else + XftPatternAddString( pattern, XFT_ENCODING, symbol ? "adobe-fontspecific" : "iso10646-1"); +# endif // QT_XFT2 + + if ( !foundry->name.isEmpty() ) + XftPatternAddString( pattern, XFT_FOUNDRY, + foundry->name.utf8().data() ); + + if ( !family->rawName.isEmpty() ) + XftPatternAddString( pattern, XFT_FAMILY, + family->rawName.utf8().data() ); + + + char pitch_value = ( encoding->pitch == 'c' ? XFT_CHARCELL : + ( encoding->pitch == 'm' ? XFT_MONO : XFT_PROPORTIONAL ) ); + XftPatternAddInteger( pattern, XFT_SPACING, pitch_value ); + + double scale = addPatternProps(pattern, style->key, style->fakeOblique, + style->smoothScalable, fp, request); + + XftResult res; + XftPattern *result = + XftFontMatch( QPaintDevice::x11AppDisplay(), fp->screen, pattern, &res ); +#ifdef QT_XFT2 + if (result && script == QFont::Latin) { + // since we added the Euro char on top, check we actually got the family + // we requested. If we didn't get it correctly, remove the Euro from the pattern + // and try again. + FcChar8 *f; + res = FcPatternGetString(result, FC_FAMILY, 0, &f); + if (res == FcResultMatch && QString::fromUtf8((char *)f) != family->rawName) { + FcPatternDel(pattern, FC_CHARSET); + FcCharSet *cs = FcCharSetCreate(); + for ( int j=0; sample_chars[script][j]; j++ ) + FcCharSetAddChar(cs, sample_chars[script][j]); + FcPatternAddCharSet(pattern, FC_CHARSET, cs); + FcCharSetDestroy(cs); + result = XftFontMatch( QPaintDevice::x11AppDisplay(), fp->screen, pattern, &res ); + } + } +#endif + XftPatternDestroy(pattern); + if (!result) + return 0; + + // somehow this gets lost in the XftMatch call, reset the anitaliasing property correctly. + if (request.styleStrategy & (QFont::PreferAntialias|QFont::NoAntialias)) { + XftPatternDel(result, XFT_ANTIALIAS); + XftPatternAddBool(result, XFT_ANTIALIAS, + !(request.styleStrategy & QFont::NoAntialias)); + } + // We pass a duplicate to XftFontOpenPattern because either xft font + // will own the pattern after the call or the pattern will be + // destroyed. + XftPattern *dup = XftPatternDuplicate( result ); + XftFont *xftfs = XftFontOpenPattern( QPaintDevice::x11AppDisplay(), dup ); + + if ( ! xftfs ) // Xft couldn't find a font? + return 0; + + QFontEngine *fe = new QFontEngineXft( xftfs, result, symbol ? 1 : 0 ); + if (fp->paintdevice + && QPaintDeviceMetrics(fp->paintdevice).logicalDpiY() != QPaintDevice::x11AppDpiY()) { + double px; + XftPatternGetDouble(result, XFT_PIXEL_SIZE, 0, &px); + scale = (double)request.pixelSize/px; + } + fe->setScale( scale ); + return fe; + } +#endif // QT_NO_XFTFREETYPE + + FM_DEBUG( " using XLFD" ); + + QCString xlfd = "-"; + xlfd += foundry->name.isEmpty() ? "*" : foundry->name.latin1(); + xlfd += "-"; + xlfd += family->name.isEmpty() ? "*" : family->name.latin1(); + + xlfd += "-"; + xlfd += style->weightName ? style->weightName : "*"; + xlfd += "-"; + xlfd += ( style->key.italic ? "i" : ( style->key.oblique ? "o" : "r" ) ); + + xlfd += "-"; + xlfd += style->setwidthName ? style->setwidthName : "*"; + // ### handle add-style + xlfd += "-*-"; + + int px = size->pixelSize; + if ( style->smoothScalable && px == SMOOTH_SCALABLE ) + px = request.pixelSize; + else if ( style->bitmapScalable && px == 0 ) + px = request.pixelSize; + double scale = 1.; + if ( px > MAXFONTSIZE_XLFD ) { + scale = (double)px/(double)MAXFONTSIZE_XLFD; + px = MAXFONTSIZE_XLFD; + } + if (fp && fp->paintdevice + && QPaintDeviceMetrics(fp->paintdevice).logicalDpiY() != QPaintDevice::x11AppDpiY()) + scale = (double)request.pixelSize/(double)px; + + xlfd += QString::number( px ).latin1(); + xlfd += "-"; + xlfd += QString::number( encoding->xpoint ); + xlfd += "-"; + xlfd += QString::number( encoding->xres ); + xlfd += "-"; + xlfd += QString::number( encoding->yres ); + xlfd += "-"; + + // ### handle cell spaced fonts + xlfd += encoding->pitch; + xlfd += "-"; + xlfd += QString::number( encoding->avgwidth ); + xlfd += "-"; + xlfd += xlfd_for_id( encoding->encoding ); + + FM_DEBUG( " xlfd: '%s'", xlfd.data() ); + + XFontStruct *xfs; + if (! (xfs = XLoadQueryFont(QPaintDevice::x11AppDisplay(), xlfd.data() ) ) ) + return 0; + + QFontEngine *fe = 0; + const int mib = xlfd_encoding[ encoding->encoding ].mib; + if (script == QFont::Latin && encoding->encoding <= LAST_LATIN_ENCODING && !forced_encoding) { + fe = new QFontEngineLatinXLFD( xfs, xlfd.data(), mib ); + } else { + fe = new QFontEngineXLFD( xfs, xlfd.data(), mib ); + } + + fe->setScale( scale ); + + return fe; +} + + +#ifdef QT_XFT2 + +static void parseFontName(const QString &name, QString &foundry, QString &family) +{ + if ( name.contains('[') && name.contains(']')) { + int i = name.find('['); + int li = name.findRev(']'); + + if (i < li) { + foundry = name.mid(i + 1, li - i - 1); + if (name[i - 1] == ' ') + i--; + family = name.left(i); + } + } else { + foundry = QString::null; + family = name; + } +} + + +static QFontEngine *loadFontConfigFont(const QFontPrivate *fp, const QFontDef &request, QFont::Script script) +{ + if (!qt_has_xft) + return 0; + + QStringList family_list; + if (request.family.isEmpty()) { + family_list = QStringList::split(QChar(','), fp->request.family); + + QString stylehint; + switch ( request.styleHint ) { + case QFont::SansSerif: + stylehint = "sans-serif"; + break; + case QFont::Serif: + stylehint = "serif"; + break; + case QFont::TypeWriter: + stylehint = "monospace"; + break; + default: + if (request.fixedPitch) + stylehint = "monospace"; + break; + } + if (!stylehint.isEmpty()) + family_list << stylehint; + } else { + family_list << request.family; + } + + FcPattern *pattern = FcPatternCreate(); + + { + QString family, foundry; + for (QStringList::ConstIterator it = family_list.begin(); it != family_list.end(); ++it) { + parseFontName(*it, foundry, family); + XftPatternAddString(pattern, XFT_FAMILY, family.utf8().data()); + } + } + + QtFontStyle::Key key; + key.italic = request.italic; + key.weight = request.weight; + key.stretch = request.stretch; + + double scale = addPatternProps(pattern, key, FALSE, TRUE, fp, request); +#ifdef FONT_MATCH_DEBUG + qDebug("original pattern contains:"); + FcPatternPrint(pattern); +#endif + + // XftFontMatch calls the right ConfigSubstitute variants, but as we use + // FcFontMatch/Sort here we have to do it manually. + FcConfigSubstitute(0, pattern, FcMatchPattern); + XftDefaultSubstitute(QPaintDevice::x11AppDisplay(), QPaintDevice::x11AppScreen(), pattern); + +// qDebug("1: pattern contains:"); +// FcPatternPrint(pattern); + + { + FcValue value; + value.type = FcTypeString; + + // these should only get added to the pattern _after_ substitution + // append the default fallback font for the specified script + extern QString qt_fallback_font_family( QFont::Script ); + QString fallback = qt_fallback_font_family( script ); + if ( ! fallback.isEmpty() && ! family_list.contains( fallback ) ) { + QCString cs = fallback.utf8(); + value.u.s = (const FcChar8 *)cs.data(); + FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); + } + + // add the default family + QString defaultFamily = QApplication::font().family(); + if ( ! family_list.contains( defaultFamily ) ) { + QCString cs = defaultFamily.utf8(); + value.u.s = (const FcChar8 *)cs.data(); + FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); + } + + // add QFont::defaultFamily() to the list, for compatibility with + // previous versions + defaultFamily = QApplication::font().defaultFamily(); + if ( ! family_list.contains( defaultFamily ) ) { + QCString cs = defaultFamily.utf8(); + value.u.s = (const FcChar8 *)cs.data(); + FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); + } + } + + if (script != QFont::Unicode) { + FcCharSet *cs = FcCharSetCreate(); + for ( int j=0; sample_chars[script][j]; j++ ) + FcCharSetAddChar(cs, sample_chars[script][j]); + if (script == QFont::Latin) + // add Euro character + FcCharSetAddChar(cs, 0x20ac); + FcPatternAddCharSet(pattern, FC_CHARSET, cs); + FcCharSetDestroy(cs); + } + +#ifdef FONT_MATCH_DEBUG + printf("final pattern contains:\n"); + FcPatternPrint(pattern); +#endif + + QFontEngine *fe = 0; + + for( int jj = (FcGetVersion() >= 20392 ? 0 : 1); jj < 2; ++jj ) { + bool use_fontsort = ( jj == 1 ); + + FcResult result; + FcFontSet *fs = 0; + FcPattern *fsp = 0; + if( use_fontsort ) { + fs = FcFontSort(0, pattern, FcFalse, 0, &result); + if (!fs) + continue; + } else { + fsp = FcFontMatch(0, pattern, &result); + if (!fsp) + continue; + } + +#ifdef FONT_MATCH_DEBUG + if( use_fontsort ) { + printf("fontset contains:\n"); + for (int i = 0; i < fs->nfont; ++i) { + FcPattern *test = fs->fonts[i]; + FcChar8 *fam; + FcPatternGetString(test, FC_FAMILY, 0, &fam); + printf(" %s\n", fam); + } + } else { + printf("fontmatch:"); + FcChar8 *fam; + FcPatternGetString(fsp, FC_FAMILY, 0, &fam); + printf(" %s\n", fam); + } +#endif + + double size_value = request.pixelSize; + if ( size_value > MAXFONTSIZE_XFT ) + size_value = MAXFONTSIZE_XFT; + + int cnt = use_fontsort ? fs->nfont : 1; + + for (int i = 0; i < cnt; ++i) { + FcPattern *font = use_fontsort ? fs->fonts[i] : fsp; + FcCharSet *cs; + FcResult res = FcPatternGetCharSet(font, FC_CHARSET, 0, &cs); + if (res != FcResultMatch) + continue; + bool do_break=true; + for ( int j=0; sample_chars[script][j]; j++ ){ + do_break=false; + if (!FcCharSetHasChar(cs, sample_chars[script][j])) { + do_break=true; + break; + } + } + if ( do_break ) + continue; + FcBool scalable; + res = FcPatternGetBool(font, FC_SCALABLE, 0, &scalable); + if (res != FcResultMatch || !scalable) { + int pixelSize; + res = FcPatternGetInteger(font, FC_PIXEL_SIZE, 0, &pixelSize); + if (res != FcResultMatch || QABS((size_value-pixelSize)/size_value) > 0.2) + continue; + } + + XftPattern *pattern = XftPatternDuplicate(font); + // add properties back in as the font selected from the list doesn't contain them. + addPatternProps(pattern, key, FALSE, TRUE, fp, request); + + XftPattern *result = + XftFontMatch( QPaintDevice::x11AppDisplay(), fp->screen, pattern, &res ); + XftPatternDestroy(pattern); + + // We pass a duplicate to XftFontOpenPattern because either xft font + // will own the pattern after the call or the pattern will be + // destroyed. + XftPattern *dup = XftPatternDuplicate( result ); + XftFont *xftfs = XftFontOpenPattern( QPaintDevice::x11AppDisplay(), dup ); + + if ( !xftfs ) { + // Xft couldn't find a font? + qDebug("couldn't open fontconfigs chosen font with Xft!!!"); + } else { + fe = new QFontEngineXft( xftfs, result, 0 ); + if (fp->paintdevice + && QPaintDeviceMetrics(fp->paintdevice).logicalDpiY() != QPaintDevice::x11AppDpiY()) { + double px; + XftPatternGetDouble(result, XFT_PIXEL_SIZE, 0, &px); + scale = request.pixelSize/px; + } + fe->setScale( scale ); + fe->fontDef = request; + if ( script != QFont::Unicode && !canRender(fe, script) ) { + FM_DEBUG( " WARN: font loaded cannot render samples" ); + delete fe; + fe = 0; + }else + FM_DEBUG( " USE: %s", fe->fontDef.family.latin1() ); + } + if (fe) { + QFontEngineXft *xft = (QFontEngineXft *)fe; + char *family; + if (XftPatternGetString(xft->pattern(), XFT_FAMILY, 0, &family) == XftResultMatch) + xft->fontDef.family = QString::fromUtf8(family); + + double px; + if (XftPatternGetDouble(xft->pattern(), XFT_PIXEL_SIZE, 0, &px) == XftResultMatch) + xft->fontDef.pixelSize = qRound(px); + + int weight = XFT_WEIGHT_MEDIUM; + XftPatternGetInteger(xft->pattern(), XFT_WEIGHT, 0, &weight); + xft->fontDef.weight = getXftWeight(weight); + + int slant = XFT_SLANT_ROMAN; + XftPatternGetInteger(xft->pattern(), XFT_SLANT, 0, &slant); + xft->fontDef.italic = (slant != XFT_SLANT_ROMAN); + + int spacing = XFT_PROPORTIONAL; + XftPatternGetInteger(xft->pattern(), XFT_SPACING, 0, &spacing); + xft->fontDef.fixedPitch = spacing != XFT_PROPORTIONAL; + + xft->fontDef.ignorePitch = FALSE; + break; + } + } + + if( use_fontsort ) + FcFontSetDestroy(fs); + else + FcPatternDestroy(fsp); + + if( fe ) + break; + + } // for( jj ) + + FcPatternDestroy(pattern); + + return fe; +} + +#endif diff --git a/src/kernel/qfontengine_p.h b/src/kernel/qfontengine_p.h new file mode 100644 index 0000000..e714903 --- /dev/null +++ b/src/kernel/qfontengine_p.h @@ -0,0 +1,632 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QFONTENGINE_P_H +#define QFONTENGINE_P_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +#ifdef Q_WS_WIN +#include "qt_windows.h" +#include "qptrdict.h" +#endif + +#include "qtextengine_p.h" + +class QPaintDevice; + +struct glyph_metrics_t; +class QChar; +typedef unsigned short glyph_t; +struct qoffset_t; +typedef int advance_t; +class QOpenType; +struct TransformedFont; + +#if defined( Q_WS_X11 ) || defined( Q_WS_WIN) || defined( Q_WS_MAC ) +class QFontEngine : public QShared +{ +public: + enum Error { + NoError, + OutOfMemory + }; + + enum Type { + // X11 types + Box, + XLFD, + LatinXLFD, + Xft, + + // MS Windows types + Win, + Uniscribe, + + // Apple MacOS types + Mac, + + // Trolltech QWS types + QWS + }; + + QFontEngine() { + count = 0; cache_count = 0; +#ifdef Q_WS_X11 + transformed_fonts = 0; +#endif + } + virtual ~QFontEngine(); + + /* returns 0 as glyph index for non existant glyphs */ + virtual Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, + advance_t *advances, int *nglyphs, bool mirrored ) const = 0; + +#ifdef Q_WS_X11 + virtual int cmap() const { return -1; } + virtual QOpenType *openType() const { return 0; } +#endif + + virtual void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ) = 0; + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, + const qoffset_t *offsets, int numGlyphs ) = 0; + virtual glyph_metrics_t boundingBox( glyph_t glyph ) = 0; + + virtual int ascent() const = 0; + virtual int descent() const = 0; + virtual int leading() const = 0; + + virtual int lineThickness() const; + virtual int underlinePosition() const; + + virtual int maxCharWidth() const = 0; + virtual int minLeftBearing() const { return 0; } + virtual int minRightBearing() const { return 0; } + + virtual const char *name() const = 0; + + virtual bool canRender( const QChar *string, int len ) = 0; + + virtual void setScale( double ) {} + virtual double scale() const { return 1.; } + + virtual Type type() const = 0; + + QFontDef fontDef; + uint cache_cost; // amount of mem used in kb by the font + int cache_count; + +#ifdef Q_WS_WIN + HDC dc() const; + void getGlyphIndexes( const QChar *ch, int numChars, glyph_t *glyphs, bool mirrored ) const; + void getCMap(); + + QCString _name; + HDC hdc; + HFONT hfont; + LOGFONT logfont; + uint stockFont : 1; + uint paintDevice : 1; + uint useTextOutA : 1; + uint ttf : 1; + uint symbol : 1; + union { + TEXTMETRICW w; + TEXTMETRICA a; + } tm; + int lw; + unsigned char *cmap; + void *script_cache; + static QPtrDict<QFontEngine> cacheDict; + short lbearing; + short rbearing; +#endif // Q_WS_WIN +#ifdef Q_WS_X11 + TransformedFont *transformed_fonts; +#endif +}; +#elif defined( Q_WS_QWS ) +class QGfx; + +class QFontEngine : public QShared +{ +public: + QFontEngine( const QFontDef&, const QPaintDevice * = 0 ); + ~QFontEngine(); + /*QMemoryManager::FontID*/ void *handle() const; + + enum Type { + // X11 types + Box, + XLFD, + Xft, + + // MS Windows types + Win, + Uniscribe, + + // Apple MacOS types + Mac, + + // Trolltech QWS types + Qws + }; + + enum TextFlags { + Underline = 0x01, + Overline = 0x02, + StrikeOut = 0x04 + }; + + enum Error { + NoError, + OutOfMemory + }; + /* returns 0 as glyph index for non existant glyphs */ + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + + glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + int underlinePosition() const; + int lineThickness() const; + + Type type() { return Qws; } + + bool canRender( const QChar *string, int len ); + inline const char *name() const { return 0; } + QFontDef fontDef; + /*QMemoryManager::FontID*/ void *id; + int cache_cost; + int cache_count; + int scale; +}; +#endif // WIN || X11 || MAC + + + +enum IndicFeatures { + CcmpFeature, + InitFeature, + NuktaFeature, + AkhantFeature, + RephFeature, + BelowFormFeature, + HalfFormFeature, + PostFormFeature, + VattuFeature, + PreSubstFeature, + AboveSubstFeature, + BelowSubstFeature, + PostSubstFeature, + HalantFeature +}; + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) +class QFontEngineBox : public QFontEngine +{ +public: + QFontEngineBox( int size ); + ~QFontEngineBox(); + + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const { return 0; } + int minRightBearing() const { return 0; } + +#ifdef Q_WS_X11 + int cmap() const; +#endif + const char *name() const; + + bool canRender( const QChar *string, int len ); + + Type type() const; + inline int size() const { return _size; } + +private: + friend class QFontPrivate; + int _size; +}; +#endif + +#ifdef Q_WS_X11 +#include "qt_x11_p.h" + + +struct TransformedFont +{ + float xx; + float xy; + float yx; + float yy; + union { + Font xlfd_font; +#ifndef QT_NO_XFTFREETYPE + XftFont *xft_font; +#endif + }; + TransformedFont *next; +}; + +#ifndef QT_NO_XFTFREETYPE +#include <ft2build.h> +#include FT_FREETYPE_H +#include "ftxopen.h" + +class QTextCodec; + +class QFontEngineXft : public QFontEngine +{ +public: + QFontEngineXft( XftFont *font, XftPattern *pattern, int cmap ); + ~QFontEngineXft(); + + QOpenType *openType() const; + + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int lineThickness() const; + int underlinePosition() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + int cmap() const; + const char *name() const; + + void setScale( double scale ); + double scale() const { return _scale; } + + bool canRender( const QChar *string, int len ); + + Type type() const; + XftPattern *pattern() const { return _pattern; } + FT_Face face() const { return _face; } + XftFont *font() const { return _font; } + + void recalcAdvances( int len, glyph_t *glyphs, advance_t *advances ); + +private: + friend class QFontPrivate; + friend class QOpenType; + XftFont *_font; + XftPattern *_pattern; + FT_Face _face; + QOpenType *_openType; + int _cmap; + short lbearing; + short rbearing; + float _scale; + enum { widthCacheSize = 0x800, cmapCacheSize = 0x500 }; + unsigned char widthCache[widthCacheSize]; + glyph_t cmapCache[cmapCacheSize]; +}; +#endif + +class QFontEngineLatinXLFD; + +class QFontEngineXLFD : public QFontEngine +{ +public: + QFontEngineXLFD( XFontStruct *fs, const char *name, int cmap ); + ~QFontEngineXLFD(); + + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + int cmap() const; + const char *name() const; + + bool canRender( const QChar *string, int len ); + + void setScale( double scale ); + double scale() const { return _scale; } + Type type() const; + + Qt::HANDLE handle() const { return (Qt::HANDLE) _fs->fid; } + +private: + friend class QFontPrivate; + XFontStruct *_fs; + QCString _name; + QTextCodec *_codec; + float _scale; // needed for printing, to correctly scale font metrics for bitmap fonts + int _cmap; + short lbearing; + short rbearing; + enum XlfdTransformations { + XlfdTrUnknown, + XlfdTrSupported, + XlfdTrUnsupported + }; + XlfdTransformations xlfd_transformations; + + friend class QFontEngineLatinXLFD; +}; + +class QFontEngineLatinXLFD : public QFontEngine +{ +public: + QFontEngineLatinXLFD( XFontStruct *xfs, const char *name, int cmap ); + ~QFontEngineLatinXLFD(); + + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, + advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, + const QScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, + const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + int cmap() const { return -1; } // ### + const char *name() const; + + bool canRender( const QChar *string, int len ); + + void setScale( double scale ); + double scale() const { return _engines[0]->scale(); } + Type type() const { return LatinXLFD; } + + Qt::HANDLE handle() const { return ((QFontEngineXLFD *) _engines[0])->handle(); } + +private: + void findEngine( const QChar &ch ); + + QFontEngine **_engines; + int _count; + + glyph_t glyphIndices [0x200]; + advance_t glyphAdvances[0x200]; + glyph_t euroIndex; + advance_t euroAdvance; +}; + +class QScriptItem; +class QTextEngine; + +#ifndef QT_NO_XFTFREETYPE + +#include "qscriptengine_p.h" +#include "qtextengine_p.h" +#include <ft2build.h> +#include FT_FREETYPE_H +#include "ftxopen.h" + +enum { PositioningProperties = 0x80000000 }; + +class QOpenType +{ +public: + QOpenType(QFontEngineXft *fe); + ~QOpenType(); + + struct Features { + uint tag; + uint property; + }; + + bool supportsScript(unsigned int script) { + Q_ASSERT(script < QFont::NScripts); + return supported_scripts[script]; + } + void selectScript(unsigned int script, const Features *features = 0); + + bool shape(QShaperItem *item, const unsigned int *properties = 0); + bool positionAndAdd(QShaperItem *item, bool doLogClusters = TRUE); + + OTL_GlyphItem glyphs() const { return otl_buffer->in_string; } + int len() const { return otl_buffer->in_length; } + void setProperty(int index, uint property) { otl_buffer->in_string[index].properties = property; } + + +private: + bool checkScript(unsigned int script); + QFontEngine *fontEngine; + FT_Face face; + TTO_GDEF gdef; + TTO_GSUB gsub; + TTO_GPOS gpos; + bool supported_scripts[QFont::NScripts]; + FT_ULong current_script; + bool positioned : 1; + OTL_Buffer otl_buffer; + GlyphAttributes *tmpAttributes; + unsigned int *tmpLogClusters; + int length; + int orig_nglyphs; + int loadFlags; +}; + +#endif // QT_NO_XFTFREETYPE + +#elif defined( Q_WS_MAC ) +#include "qt_mac.h" +#include <qmap.h> +#include <qcache.h> + +class QFontEngineMac : public QFontEngine +{ +#if 0 + ATSFontMetrics *info; +#else + FontInfo *info; +#endif + int psize; + FMFontFamily fmfam; + QMacFontInfo *internal_fi; + mutable ATSUTextLayout mTextLayout; + enum { widthCacheSize = 0x500 }; + mutable unsigned char widthCache[widthCacheSize]; + friend class QFont; + friend class QGLContext; + friend class QFontPrivate; + friend class QMacSetFontInfo; + +public: + QFontEngineMac(); + ~QFontEngineMac(); + + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + + glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const { return (int)info->ascent; } + int descent() const { return (int)info->descent; } + int leading() const { return (int)info->leading; } +#if 0 + int maxCharWidth() const { return (int)info->maxAdvanceWidth; } +#else + int maxCharWidth() const { return info->widMax; } +#endif + + const char *name() const { return "ATSUI"; } + + bool canRender( const QChar *string, int len ); + + Type type() const { return QFontEngine::Mac; } + + void calculateCost(); + + enum { WIDTH=0x01, DRAW=0x02, EXISTS=0x04 }; + int doTextTask(const QChar *s, int pos, int use_len, int len, uchar task, int =-1, int y=-1, + QPaintDevice *dev=NULL, const QRegion *rgn=NULL) const; +}; + +#elif defined( Q_WS_WIN ) + +class QFontEngineWin : public QFontEngine +{ +public: + QFontEngineWin( const char *name, HDC, HFONT, bool, LOGFONT ); + + Error stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + + glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + const char *name() const; + + bool canRender( const QChar *string, int len ); + + Type type() const; + + enum { widthCacheSize = 0x800, cmapCacheSize = 0x500 }; + unsigned char widthCache[widthCacheSize]; +}; + +#if 0 +class QFontEngineUniscribe : public QFontEngineWin +{ +public: + void draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ); + bool canRender( const QChar *string, int len ); + + Type type() const; +}; +#endif + +#endif // Q_WS_WIN + +#endif diff --git a/src/kernel/qfontengine_x11.cpp b/src/kernel/qfontengine_x11.cpp new file mode 100644 index 0000000..ae27f9a --- /dev/null +++ b/src/kernel/qfontengine_x11.cpp @@ -0,0 +1,2724 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qfontengine_p.h" + +// #define FONTENGINE_DEBUG + +#include <qwidget.h> +#include <qcstring.h> +#include <qtextcodec.h> + +#include "qbitmap.h" +#include "qfontdatabase.h" +#include "qpaintdevice.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qimage.h" + +#include "qt_x11_p.h" + +#include "qfont.h" +#include "qtextengine_p.h" + +#include <private/qunicodetables_p.h> + +#include <limits.h> + +// defined in qfontdatbase_x11.cpp +extern int qt_mib_for_xlfd_encoding( const char *encoding ); +extern int qt_xlfd_encoding_id( const char *encoding ); + +extern void qt_draw_transformed_rect( QPainter *p, int x, int y, int w, int h, bool fill ); + +static void drawLines( QPainter *p, QFontEngine *fe, int baseline, int x1, int w, int textFlags ) +{ + int lw = fe->lineThickness(); + if ( textFlags & Qt::Underline ) { + int pos = fe->underlinePosition(); + qt_draw_transformed_rect( p, x1, baseline+pos, w, lw, TRUE ); + } + if ( textFlags & Qt::Overline ) { + int pos = fe->ascent()+1; + if ( !pos ) pos = 1; + qt_draw_transformed_rect( p, x1, baseline-pos, w, lw, TRUE ); + } + if ( textFlags & Qt::StrikeOut ) { + int pos = fe->ascent()/3; + if ( !pos ) pos = 1; + qt_draw_transformed_rect( p, x1, baseline-pos, w, lw, TRUE ); + } +} + + +inline static void qSafeXDestroyImage( XImage *x ) +{ + if ( x->data ) { + free( x->data ); + x->data = 0; + } + XDestroyImage( x ); +} + +extern bool qt_xForm_helper( const QWMatrix &trueMat, int xoffset, + int type, int depth, + uchar *dptr, int dbpl, int p_inc, int dHeight, + uchar *sptr, int sbpl, int sWidth, int sHeight + ); + +static QBitmap transform(Display *dpy, const QBitmap &source, int xoff, int yoff, int w, int h, const QWMatrix &matrix) +{ + int ws = source.width(); + int hs = source.height(); + + bool invertible; + QWMatrix mat = matrix.invert( &invertible ); // invert matrix + + if (!invertible ) + return QBitmap(); + mat.translate(xoff, yoff); + + XImage *xi = XGetImage(dpy, source.handle(), 0, 0, ws, hs, AllPlanes, XYPixmap); + + if ( !xi ) + return QBitmap(); + + int sbpl = xi->bytes_per_line; + uchar *sptr = (uchar *)xi->data; + + int dbpl = (w+7)/8; + int dbytes = dbpl*h; + + uchar *dptr = (uchar *)malloc( dbytes ); // create buffer for bits + memset( dptr, 0, dbytes ); + + int type = xi->bitmap_bit_order == MSBFirst ? QT_XFORM_TYPE_MSBFIRST : QT_XFORM_TYPE_LSBFIRST; + int xbpl, p_inc; + xbpl = (w+7)/8; + p_inc = dbpl - xbpl; + + bool ok = qt_xForm_helper( mat, xi->xoffset, type, 1, dptr, xbpl, p_inc, h, sptr, sbpl, ws, hs ); + qSafeXDestroyImage(xi); + QBitmap bm; + if (ok) { + bm = QBitmap( w, h, dptr, QImage::systemBitOrder() != QImage::BigEndian ); + } else { +#if defined(QT_CHECK_RANGE) + qWarning( "QFontEngineXft::tranform: xform failed"); +#endif + } + + free( dptr ); + return bm; +} + + +static void drawScaled(int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags, + Display *dpy, GC gc, QPaintDevice *pdev, QFontEngine *fe, + const QWMatrix &xmat, float scale) +{ + // font doesn't support transformations, need to do it by hand + int w = qRound(si->width/scale); + int h = qRound((si->ascent + si->descent + 1)/scale); + if (w == 0 || h == 0) + return; + QWMatrix mat1 = xmat; + mat1.scale(scale, scale); + + w += h; // add some pixels to width because of italic correction + QBitmap bm( w, h, TRUE ); // create bitmap + QPainter paint; + paint.begin( &bm ); // draw text in bitmap + fe->draw( &paint, 0, si->ascent/scale, engine, si, textFlags ); + paint.end(); + + QRect pdevRect; + if (pdev->devType() == QInternal::Widget) + pdevRect = ((QWidget *)pdev)->rect(); + else if (pdev->devType() == QInternal::Pixmap) + pdevRect = ((QPixmap *)pdev)->rect(); + else + return; + + + QRect br = mat1.mapRect(QRect(x, y - si->ascent, w, h)); + QRect br2 = br & pdevRect; + if (br2.width() <= 0 || br2.height() <= 0 + || br2.width() >= 32768 || br2.height() >= 32768) + return; + QWMatrix mat = QPixmap::trueMatrix( mat1, w, h ); + QBitmap wx_bm = ::transform(dpy, bm, br2.x() - br.x(), br2.y() - br.y(), br2.width(), br2.height(), mat); + if ( wx_bm.isNull() ) + return; + + x = br2.x(); + y = br2.y(); + + Qt::HANDLE hd = pdev->handle(); + XSetFillStyle( dpy, gc, FillStippled ); + XSetStipple( dpy, gc, wx_bm.handle() ); + XSetTSOrigin( dpy, gc, x, y ); + XFillRectangle( dpy, hd, gc, x, y, wx_bm.width(), wx_bm.height() ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); +} + + +QFontEngine::~QFontEngine() +{ +} + +int QFontEngine::lineThickness() const +{ + // ad hoc algorithm + int score = fontDef.weight * fontDef.pixelSize; + int lw = score / 700; + + // looks better with thicker line for small pointsizes + if ( lw < 2 && score >= 1050 ) lw = 2; + if ( lw == 0 ) lw = 1; + + return lw; +} + +int QFontEngine::underlinePosition() const +{ + int pos = ( ( lineThickness() * 2 ) + 3 ) / 6; + return pos ? pos : 1; +} + +// ------------------------------------------------------------------ +// The box font engine +// ------------------------------------------------------------------ + + +QFontEngineBox::QFontEngineBox( int size ) + : _size( size ) +{ + cache_cost = sizeof( QFontEngineBox ); +} + +QFontEngineBox::~QFontEngineBox() +{ +} + +QFontEngine::Error QFontEngineBox::stringToCMap( const QChar *, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + + memset( glyphs, 0, len * sizeof( glyph_t ) ); + *nglyphs = len; + + if ( advances ) { + for ( int i = 0; i < len; i++ ) + *(advances++) = _size; + } + return NoError; +} + +void QFontEngineBox::draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ) +{ + Display *dpy = QPaintDevice::x11AppDisplay(); + Qt::HANDLE hd = p->device()->handle(); + GC gc = p->gc; + +#ifdef FONTENGINE_DEBUG + p->save(); + p->setBrush( Qt::white ); + glyph_metrics_t ci = boundingBox( glyphs, offsets, numGlyphs ); + p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height ); + p->drawRect( x + ci.x, y + 50 + ci.y, ci.width, ci.height ); + qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height ); + p->restore(); + int xp = x; + int yp = y; +#endif + + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + + if ( p->txop > QPainter::TxTranslate ) { + int xp = x; + int yp = _size + 2; + int s = _size - 3; + for (int k = 0; k < si->num_glyphs; k++) { + if (!glyphAttributes[k].zeroWidth) + qt_draw_transformed_rect( p, xp, yp, s, s, FALSE ); + xp += _size; + } + } else { + if ( p->txop == QPainter::TxTranslate ) + p->map( x, y, &x, &y ); + + XRectangle rects[64]; + + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = QMIN(64, si->num_glyphs-gl); + int adv = toDraw*_size; + if (x + adv < SHRT_MAX && x > SHRT_MIN) { + int ng = 0; + for (int k = 0; k < toDraw; k++) { + if (!glyphAttributes[gl + k].zeroWidth) { + rects[ng].x = x + (k * _size); + rects[ng].y = y - _size + 2; + rects[ng].width = rects[k].height = _size - 3; + ++ng; + } + } + XDrawRectangles(dpy, hd, gc, rects, ng); + } + gl += toDraw; + x += adv; + } + } + + if ( textFlags != 0 ) + drawLines( p, this, y, x, si->num_glyphs*_size, textFlags ); + +#ifdef FONTENGINE_DEBUG + x = xp; + y = yp; + p->save(); + p->setPen( Qt::red ); + for ( int i = 0; i < numGlyphs; i++ ) { + glyph_metrics_t ci = boundingBox( glyphs[i] ); + x += offsets[i].x; + y += offsets[i].y; + p->drawRect( x + ci.x, y + 50 + ci.y, ci.width, ci.height ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offset=(%d/%d)", i, ci.x, ci.y, ci.width, ci.height, + ci.xoff, ci.yoff, offsets[i].x, offsets[i].y ); + x += ci.xoff; + y += ci.yoff; + } + p->restore(); +#endif +} + +glyph_metrics_t QFontEngineBox::boundingBox( const glyph_t *, const advance_t *, const qoffset_t *, int numGlyphs ) +{ + glyph_metrics_t overall; + overall.x = overall.y = 0; + overall.width = _size*numGlyphs; + overall.height = _size; + overall.xoff = overall.width; + overall.yoff = 0; + return overall; +} + +glyph_metrics_t QFontEngineBox::boundingBox( glyph_t ) +{ + return glyph_metrics_t( 0, _size, _size, _size, _size, 0 ); +} + + + +int QFontEngineBox::ascent() const +{ + return _size; +} + +int QFontEngineBox::descent() const +{ + return 0; +} + +int QFontEngineBox::leading() const +{ + int l = qRound( _size * 0.15 ); + return (l > 0) ? l : 1; +} + +int QFontEngineBox::maxCharWidth() const +{ + return _size; +} + +int QFontEngineBox::cmap() const +{ + return -1; +} + +const char *QFontEngineBox::name() const +{ + return "null"; +} + +bool QFontEngineBox::canRender( const QChar *, int ) +{ + return TRUE; +} + +QFontEngine::Type QFontEngineBox::type() const +{ + return Box; +} + + + + +// ------------------------------------------------------------------ +// Xlfd cont engine +// ------------------------------------------------------------------ + +static inline XCharStruct *charStruct( XFontStruct *xfs, uint ch ) +{ + XCharStruct *xcs = 0; + unsigned char r = ch>>8; + unsigned char c = ch&0xff; + if ( r >= xfs->min_byte1 && + r <= xfs->max_byte1 && + c >= xfs->min_char_or_byte2 && + c <= xfs->max_char_or_byte2) { + if ( !xfs->per_char ) + xcs = &(xfs->min_bounds); + else { + xcs = xfs->per_char + ((r - xfs->min_byte1) * + (xfs->max_char_or_byte2 - + xfs->min_char_or_byte2 + 1)) + + (c - xfs->min_char_or_byte2); + if (xcs->width == 0 && xcs->ascent == 0 && xcs->descent == 0) + xcs = 0; + } + } + return xcs; +} + +QFontEngineXLFD::QFontEngineXLFD( XFontStruct *fs, const char *name, int mib ) + : _fs( fs ), _name( name ), _codec( 0 ), _scale( 1. ), _cmap( mib ) +{ + if ( _cmap ) _codec = QTextCodec::codecForMib( _cmap ); + + cache_cost = (((fs->max_byte1 - fs->min_byte1) * + (fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1)) + + fs->max_char_or_byte2 - fs->min_char_or_byte2); + cache_cost = ((fs->max_bounds.ascent + fs->max_bounds.descent) * + (fs->max_bounds.width * cache_cost / 8)); + lbearing = SHRT_MIN; + rbearing = SHRT_MIN; + +#if 1 + // Server side transformations do not seem to work correctly for + // all types of fonts (for example, it works for bdf/pcf fonts, + // but not for ttf). It also seems to be extermely server + // dependent. The best thing is to just disable server side + // transformations until either server support matures or we + // figure out a better way to do it. + xlfd_transformations = XlfdTrUnsupported; +#else + xlfd_transformations = XlfdTrUnknown; + + // Hummingbird's Exceed X server will substitute 'fixed' for any + // known fonts, and it doesn't seem to support transformations, so + // we should never try to use xlfd transformations with it + if (strstr(ServerVendor(QPaintDevice::x11AppDisplay()), "Hummingbird")) + xlfd_transformations = XlfdTrUnsupported; +#endif +} + +QFontEngineXLFD::~QFontEngineXLFD() +{ + XFreeFont( QPaintDevice::x11AppDisplay(), _fs ); + _fs = 0; + TransformedFont *trf = transformed_fonts; + while ( trf ) { + XUnloadFont( QPaintDevice::x11AppDisplay(), trf->xlfd_font ); + TransformedFont *tmp = trf; + trf = trf->next; + delete tmp; + } +} + +QFontEngine::Error QFontEngineXLFD::stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + + if ( _codec ) { + bool haveNbsp = FALSE; + for ( int i = 0; i < len; i++ ) + if ( str[i].unicode() == 0xa0 ) { + haveNbsp = TRUE; + break; + } + + QChar *chars = (QChar *)str; + if ( haveNbsp || mirrored ) { + chars = (QChar *)malloc( len*sizeof(QChar) ); + for ( int i = 0; i < len; i++ ) + chars[i] = (str[i].unicode() == 0xa0 ? 0x20 : + (mirrored ? ::mirroredChar(str[i]).unicode() : str[i].unicode())); + } + _codec->fromUnicodeInternal( chars, glyphs, len ); + if (chars != str) + free( chars ); + } else { + glyph_t *g = glyphs + len; + const QChar *c = str + len; + if ( mirrored ) { + while ( c != str ) + *(--g) = (--c)->unicode() == 0xa0 ? 0x20 : ::mirroredChar(*c).unicode(); + } else { + while ( c != str ) + *(--g) = (--c)->unicode() == 0xa0 ? 0x20 : c->unicode(); + } + } + *nglyphs = len; + + if ( advances ) { + glyph_t *g = glyphs + len; + advance_t *a = advances + len; + XCharStruct *xcs; + // inlined for better perfomance + if ( !_fs->per_char ) { + xcs = &_fs->min_bounds; + while ( a != advances ) + *(--a) = xcs->width; + } + else if ( !_fs->max_byte1 ) { + XCharStruct *base = _fs->per_char - _fs->min_char_or_byte2; + while ( g-- != glyphs ) { + unsigned int gl = *g; + xcs = (gl >= _fs->min_char_or_byte2 && gl <= _fs->max_char_or_byte2) ? + base + gl : 0; + *(--a) = (!xcs || (!xcs->width && !xcs->ascent && !xcs->descent)) ? _fs->ascent : xcs->width; + } + } + else { + while ( g != glyphs ) { + xcs = charStruct( _fs, *(--g) ); + *(--a) = (xcs ? xcs->width : _fs->ascent); + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } + return NoError; +} + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static bool x_font_load_error = FALSE; +static int x_font_errorhandler(Display *, XErrorEvent *) +{ + x_font_load_error = TRUE; + return 0; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +void QFontEngineXLFD::draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ) +{ + if ( !si->num_glyphs ) + return; + +// qDebug("QFontEngineXLFD::draw( %d, %d, numglyphs=%d", x, y, si->num_glyphs ); + + Display *dpy = QPaintDevice::x11AppDisplay(); + Qt::HANDLE hd = p->device()->handle(); + GC gc = p->gc; + + bool transform = FALSE; + int xorig = x; + int yorig = y; + + Qt::HANDLE font_id = _fs->fid; + if ( p->txop > QPainter::TxTranslate || _scale < 0.9999 || _scale > 1.0001 ) { + bool degenerate = QABS( p->m11()*p->m22() - p->m12()*p->m21() ) < 0.01; + if ( !degenerate && xlfd_transformations != XlfdTrUnsupported ) { + // need a transformed font from the server + QCString xlfd_transformed = _name; + int field = 0; + char *data = xlfd_transformed.data(); + int pos = 0; + while ( field < 7 ) { + if ( data[pos] == '-' ) + field++; + pos++; + } + int endPos = pos; + while ( data[endPos] != '-' ) + endPos++; + float size = xlfd_transformed.mid( pos, endPos-pos ).toInt(); + float mat[4]; + mat[0] = p->m11()*size*_scale; + mat[1] = -p->m12()*size*_scale; + mat[2] = -p->m21()*size*_scale; + mat[3] = p->m22()*size*_scale; + + // check if we have it cached + TransformedFont *trf = transformed_fonts; + TransformedFont *prev = 0; + int i = 0; + while ( trf ) { + if ( trf->xx == mat[0] && + trf->xy == mat[1] && + trf->yx == mat[2] && + trf->yy == mat[3] ) + break; + TransformedFont *tmp = trf; + trf = trf->next; + if (i > 10) { + XUnloadFont( QPaintDevice::x11AppDisplay(), tmp->xlfd_font ); + delete tmp; + prev->next = trf; + } else { + prev = tmp; + } + ++i; + } + if ( trf ) { + if ( prev ) { + // move to beginning of list + prev->next = trf->next; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + font_id = trf->xlfd_font; + } else { + QCString matrix="["; + for ( int i = 0; i < 4; i++ ) { + float f = mat[i]; + if ( f < 0 ) { + matrix += '~'; + f = -f; + } + matrix += QString::number( f, 'f', 5 ).latin1(); + matrix += ' '; + } + matrix += ']'; + //qDebug("m: %2.2f %2.2f %2.2f %2.2f, matrix=%s", p->m11(), p->m12(), p->m21(), p->m22(), matrix.data()); + xlfd_transformed.replace( pos, endPos-pos, matrix ); + + x_font_load_error = FALSE; + XErrorHandler old_handler = XSetErrorHandler( x_font_errorhandler ); + font_id = XLoadFont( dpy, xlfd_transformed.data() ); + XSync( dpy, FALSE ); + XSetErrorHandler( old_handler ); + if ( x_font_load_error ) { + //qDebug( "couldn't load transformed font" ); + font_id = _fs->fid; + xlfd_transformations = XlfdTrUnsupported; + } else { + TransformedFont *trf = new TransformedFont; + trf->xx = mat[0]; + trf->xy = mat[1]; + trf->yx = mat[2]; + trf->yy = mat[3]; + trf->xlfd_font = font_id; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + } + } + if ( degenerate || xlfd_transformations == XlfdTrUnsupported ) { + // XServer or font don't support server side transformations, need to do it by hand + float tmp = _scale; + _scale = 1.; + drawScaled(x, y, engine, si, textFlags, dpy, p->gc, p->device(), this, p->xmat, tmp); + _scale = tmp; + return; + } + transform = TRUE; + } else if ( p->txop == QPainter::TxTranslate ) { + p->map( x, y, &x, &y ); + } + + XSetFont(dpy, gc, font_id); + +#ifdef FONTENGINE_DEBUG + p->save(); + p->setBrush( Qt::white ); + glyph_metrics_t ci = boundingBox( glyphs, advances, offsets, si->num_glyphs ); + p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height ); + p->drawRect( x + ci.x, y + 100 + ci.y, ci.width, ci.height ); + qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height ); + p->restore(); + int xp = x; + int yp = y; +#endif + + glyph_t *glyphs = engine->glyphs( si ); + advance_t *advances = engine->advances( si ); + qoffset_t *offsets = engine->offsets( si ); + + XChar2b ch[256]; + XChar2b *chars = ch; + if ( si->num_glyphs > 255 ) + chars = (XChar2b *)malloc( si->num_glyphs*sizeof(XChar2b) ); + + for (int i = 0; i < si->num_glyphs; i++) { + chars[i].byte1 = glyphs[i] >> 8; + chars[i].byte2 = glyphs[i] & 0xff; + } + + int xpos = x; + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + + if ( si->analysis.bidiLevel % 2 ) { + int i = si->num_glyphs; + while( i-- ) { + advance_t adv = advances[i]; + // qDebug("advance = %d/%d", adv.x, adv.y ); + x += adv; + glyph_metrics_t gi = boundingBox( glyphs[i] ); + int xp = x-offsets[i].x-gi.xoff; + int yp = y+offsets[i].y-gi.yoff; + if ( transform ) + p->map( xp, yp, &xp, &yp ); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XDrawString16(dpy, hd, gc, xp, yp, chars+i, 1 ); + } + } else { + if ( transform || si->hasPositioning ) { + int i = 0; + while( i < si->num_glyphs ) { + int xp = x+offsets[i].x; + int yp = y+offsets[i].y; + if ( transform ) + p->map( xp, yp, &xp, &yp ); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XDrawString16(dpy, hd, gc, xp, yp, chars+i, 1 ); + advance_t adv = advances[i]; + // qDebug("advance = %d/%d", adv.x, adv.y ); + x += adv; + i++; + } + } else { + // we can take a shortcut + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = QMIN(64, si->num_glyphs-gl); + int adv = 0; + for (int i = gl; i < gl+toDraw; ++i) + adv += advances[i]; + if (x + adv < SHRT_MAX && x > SHRT_MIN) + XDrawString16(dpy, hd, gc, x, y, chars+gl, toDraw); + gl += toDraw; + x += adv; + } + } + } + + if ( chars != ch ) + free( chars ); + + if ( textFlags != 0 ) + drawLines( p, this, yorig, xorig, x-xpos, textFlags ); + +#ifdef FONTENGINE_DEBUG + x = xp; + y = yp; + p->save(); + p->setPen( Qt::red ); + for ( int i = 0; i < si->num_glyphs; i++ ) { + glyph_metrics_t ci = boundingBox( glyphs[i] ); + p->drawRect( x + ci.x + offsets[i].x, y + 100 + ci.y + offsets[i].y, ci.width, ci.height ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=(%d/%d)", i, ci.x, ci.y, ci.width, ci.height, + ci.xoff, ci.yoff, offsets[i].x, offsets[i].y, + advances[i].x, advances[i].y); + x += advances[i].x; + y += advances[i].y; + } + p->restore(); +#endif +} + +glyph_metrics_t QFontEngineXLFD::boundingBox( const glyph_t *glyphs, const advance_t *advances, const qoffset_t *offsets, int numGlyphs ) +{ + int i; + + glyph_metrics_t overall; + int ymax = 0; + int xmax = 0; + for (i = 0; i < numGlyphs; i++) { + XCharStruct *xcs = charStruct( _fs, glyphs[i] ); + if (xcs) { + int x = overall.xoff + offsets[i].x - xcs->lbearing; + int y = overall.yoff + offsets[i].y - xcs->ascent; + overall.x = QMIN( overall.x, x ); + overall.y = QMIN( overall.y, y ); + xmax = QMAX( xmax, overall.xoff + offsets[i].x + xcs->rbearing ); + ymax = QMAX( ymax, y + xcs->ascent + xcs->descent ); + overall.xoff += qRound(advances[i]/_scale); + } else { + int size = _fs->ascent; + overall.x = QMIN(overall.x, overall.xoff ); + overall.y = QMIN(overall.y, overall.yoff - size ); + ymax = QMAX( ymax, overall.yoff ); + overall.xoff += size; + xmax = QMAX( xmax, overall.xoff ); + } + } + overall.height = ymax - overall.y; + overall.width = xmax - overall.x; + + if ( _scale != 1. ) { + overall.x = qRound(overall.x * _scale); + overall.y = qRound(overall.y * _scale); + overall.height = qRound(overall.height * _scale); + overall.width = qRound(overall.width * _scale); + overall.xoff = qRound(overall.xoff * _scale); + overall.yoff = qRound(overall.yoff * _scale); + } + return overall; +} + +glyph_metrics_t QFontEngineXLFD::boundingBox( glyph_t glyph ) +{ + glyph_metrics_t gm; + // ### scale missing! + XCharStruct *xcs = charStruct( _fs, glyph ); + if (xcs) { + gm = glyph_metrics_t( xcs->lbearing, -xcs->ascent, xcs->rbearing- xcs->lbearing, xcs->ascent + xcs->descent, xcs->width, 0 ); + } else { + int size = _fs->ascent; + gm = glyph_metrics_t( 0, size, size, size, size, 0 ); + } + if ( _scale != 1. ) { + gm.x = qRound(gm.x * _scale); + gm.y = qRound(gm.y * _scale); + gm.height = qRound(gm.height * _scale); + gm.width = qRound(gm.width * _scale); + gm.xoff = qRound(gm.xoff * _scale); + gm.yoff = qRound(gm.yoff * _scale); + } + return gm; +} + + +int QFontEngineXLFD::ascent() const +{ + return qRound(_fs->ascent*_scale); +} + +int QFontEngineXLFD::descent() const +{ + return qRound((_fs->descent-1)*_scale); +} + +int QFontEngineXLFD::leading() const +{ + int l = qRound((QMIN(_fs->ascent, _fs->max_bounds.ascent) + + QMIN(_fs->descent, _fs->max_bounds.descent)) * _scale * 0.15 ); + return (l > 0) ? l : 1; +} + +int QFontEngineXLFD::maxCharWidth() const +{ + return qRound(_fs->max_bounds.width*_scale); +} + + +// Loads the font for the specified script +static inline int maxIndex(XFontStruct *f) { + return (((f->max_byte1 - f->min_byte1) * + (f->max_char_or_byte2 - f->min_char_or_byte2 + 1)) + + f->max_char_or_byte2 - f->min_char_or_byte2); +} + +int QFontEngineXLFD::minLeftBearing() const +{ + if ( lbearing == SHRT_MIN ) { + if ( _fs->per_char ) { + XCharStruct *cs = _fs->per_char; + int nc = maxIndex(_fs) + 1; + int mx = cs->lbearing; + + for (int c = 1; c < nc; c++) { + // ignore the bearings for characters whose ink is + // completely outside the normal bounding box + if ((cs[c].lbearing <= 0 && cs[c].rbearing <= 0) || + (cs[c].lbearing >= cs[c].width && cs[c].rbearing >= cs[c].width)) + continue; + + int nmx = cs[c].lbearing; + + if (nmx < mx) + mx = nmx; + } + + ((QFontEngineXLFD *)this)->lbearing = mx; + } else + ((QFontEngineXLFD *)this)->lbearing = _fs->min_bounds.lbearing; + } + return qRound (lbearing*_scale); +} + +int QFontEngineXLFD::minRightBearing() const +{ + if ( rbearing == SHRT_MIN ) { + if ( _fs->per_char ) { + XCharStruct *cs = _fs->per_char; + int nc = maxIndex(_fs) + 1; + int mx = cs->rbearing; + + for (int c = 1; c < nc; c++) { + // ignore the bearings for characters whose ink is + // completely outside the normal bounding box + if ((cs[c].lbearing <= 0 && cs[c].rbearing <= 0) || + (cs[c].lbearing >= cs[c].width && cs[c].rbearing >= cs[c].width)) + continue; + + int nmx = cs[c].rbearing; + + if (nmx < mx) + mx = nmx; + } + + ((QFontEngineXLFD *)this)->rbearing = mx; + } else + ((QFontEngineXLFD *)this)->rbearing = _fs->min_bounds.rbearing; + } + return qRound (rbearing*_scale); +} + +int QFontEngineXLFD::cmap() const +{ + return _cmap; +} + +const char *QFontEngineXLFD::name() const +{ + return _name; +} + +bool QFontEngineXLFD::canRender( const QChar *string, int len ) +{ + glyph_t glyphs[256]; + int nglyphs = 255; + glyph_t *g = glyphs; + if ( stringToCMap( string, len, g, 0, &nglyphs, FALSE ) == OutOfMemory ) { + g = (glyph_t *)malloc( nglyphs*sizeof(glyph_t) ); + stringToCMap( string, len, g, 0, &nglyphs, FALSE ); + } + + bool allExist = TRUE; + for ( int i = 0; i < nglyphs; i++ ) { + if ( !g[i] || !charStruct( _fs, g[i] ) ) { + allExist = FALSE; + break; + } + } + + if ( g != glyphs ) + free( g ); + + return allExist; +} + + +void QFontEngineXLFD::setScale( double scale ) +{ + _scale = scale; +} + + +QFontEngine::Type QFontEngineXLFD::type() const +{ + return XLFD; +} + + +// ------------------------------------------------------------------ +// LatinXLFD engine +// ------------------------------------------------------------------ + +static const int engine_array_inc = 4; + +QFontEngineLatinXLFD::QFontEngineLatinXLFD( XFontStruct *xfs, const char *name, + int mib ) +{ + _engines = new QFontEngine*[ engine_array_inc ]; + _engines[0] = new QFontEngineXLFD( xfs, name, mib ); + _count = 1; + + cache_cost = _engines[0]->cache_cost; + + memset( glyphIndices, 0, sizeof( glyphIndices ) ); + memset( glyphAdvances, 0, sizeof( glyphAdvances ) ); + euroIndex = 0; + euroAdvance = 0; +} + +QFontEngineLatinXLFD::~QFontEngineLatinXLFD() +{ + for ( int i = 0; i < _count; ++i ) { + delete _engines[i]; + _engines[i] = 0; + } + delete [] _engines; + _engines = 0; +} + +void QFontEngineLatinXLFD::findEngine( const QChar &ch ) +{ + if ( ch.unicode() == 0 ) return; + + static const char *alternate_encodings[] = { + "iso8859-1", + "iso8859-2", + "iso8859-3", + "iso8859-4", + "iso8859-9", + "iso8859-10", + "iso8859-13", + "iso8859-14", + "iso8859-15", + "hp-roman8" + }; + static const int mib_count = sizeof( alternate_encodings ) / sizeof( const char * ); + + // see if one of the above mibs can map the char we want + QTextCodec *codec = 0; + int which = -1; + int i; + for ( i = 0; i < mib_count; ++i ) { + const int mib = qt_mib_for_xlfd_encoding( alternate_encodings[i] ); + bool skip = FALSE; + for ( int e = 0; e < _count; ++e ) { + if ( _engines[e]->cmap() == mib ) { + skip = TRUE; + break; + } + } + if ( skip ) continue; + + codec = QTextCodec::codecForMib( mib ); + if ( codec && codec->canEncode( ch ) ) { + which = i; + break; + } + } + + if ( ! codec || which == -1 ) + return; + + const int enc_id = qt_xlfd_encoding_id( alternate_encodings[which] ); + QFontDef req = fontDef; + QFontEngine *engine = QFontDatabase::findFont( QFont::Latin, 0, req, enc_id ); + if ( ! engine ) { + req.family = QString::null; + engine = QFontDatabase::findFont( QFont::Latin, 0, req, enc_id ); + if ( ! engine ) return; + } + engine->setScale( scale() ); + + if ( ! ( _count % engine_array_inc ) ) { + // grow the engines array + QFontEngine **old = _engines; + int new_size = + ( ( ( _count+engine_array_inc ) / engine_array_inc ) * engine_array_inc ); + _engines = new QFontEngine*[new_size]; + for ( i = 0; i < _count; ++i ) + _engines[i] = old[i]; + delete [] old; + } + + _engines[_count] = engine; + const int hi = _count << 8; + ++_count; + + unsigned short chars[0x201]; + glyph_t glyphs[0x201]; + advance_t advances[0x201]; + for ( i = 0; i < 0x200; ++i ) + chars[i] = i; + chars[0x200] = 0x20ac; + int glyphCount = 0x201; + engine->stringToCMap( (const QChar *) chars, 0x201, glyphs, advances, &glyphCount, FALSE ); + + // merge member data with the above + for ( i = 0; i < 0x200; ++i ) { + if ( glyphIndices[i] != 0 || glyphs[i] == 0 ) continue; + glyphIndices[i] = glyphs[i] >= 0x2100 ? glyphs[i] : hi | glyphs[i]; + glyphAdvances[i] = advances[i]; + } + if (!euroIndex && glyphs[0x200]) { + euroIndex = hi | glyphs[0x200]; + euroAdvance = advances[0x200]; + } +} + +QFontEngine::Error +QFontEngineLatinXLFD::stringToCMap( const QChar *str, int len, glyph_t *glyphs, + advance_t *advances, int *nglyphs, bool mirrored ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + + int i; + bool missing = FALSE; + const QChar *c = str+len; + glyph_t *g = glyphs+len; + if ( advances ) { + int asc = ascent(); + advance_t *a = advances+len; + if ( mirrored ) { + while ( c != str ) { + --c; + --g; + --a; + if ( c->unicode() < 0x200 ) { + unsigned short ch = ::mirroredChar(*c).unicode(); + *g = glyphIndices[ch]; + *a = glyphAdvances[ch]; + } else { + if ( c->unicode() == 0x20ac ) { + *g = euroIndex; + *a = euroAdvance; + } else { + *g = 0; + *a = asc; + } + } + missing = ( missing || ( *g == 0 ) ); + } + } else { + while ( c != str ) { + --c; + --g; + --a; + if ( c->unicode() < 0x200 ) { + *g = glyphIndices[c->unicode()]; + *a = glyphAdvances[c->unicode()]; + } else { + if ( c->unicode() == 0x20ac ) { + *g = euroIndex; + *a = euroAdvance; + } else { + *g = 0; + *a = asc; + } + } + missing = ( missing || ( *g == 0 ) ); + } + } + } else { + if ( mirrored ) { + while ( c != str ) { + --c; + --g; + *g = ( ( c->unicode() < 0x200 ) ? glyphIndices[::mirroredChar(*c).unicode()] + : (c->unicode() == 0x20ac) ? euroIndex : 0 ); + missing = ( missing || ( *g == 0 ) ); + } + } else { + while ( c != str ) { + --c; + --g; + *g = ( ( c->unicode() < 0x200 ) ? glyphIndices[c->unicode()] + : (c->unicode() == 0x20ac) ? euroIndex : 0 ); + missing = ( missing || ( *g == 0 ) ); + } + } + } + + if ( missing ) { + for ( i = 0; i < len; ++i ) { + unsigned short uc = str[i].unicode(); + if ( glyphs[i] != 0 || (uc >= 0x200 && uc != 0x20ac) ) + continue; + + QFontEngineLatinXLFD *that = (QFontEngineLatinXLFD *) this; + that->findEngine( str[i] ); + glyphs[i] = (uc == 0x20ac ? euroIndex : that->glyphIndices[uc]); + if ( advances ) + advances[i] = (uc == 0x20ac ? euroAdvance : glyphAdvances[uc]); + } + } + + *nglyphs = len; + return NoError; +} + +void QFontEngineLatinXLFD::draw( QPainter *p, int x, int y, const QTextEngine *engine, + const QScriptItem *si, int textFlags ) +{ + if ( !si->num_glyphs ) return; + + glyph_t *glyphs = engine->glyphs( si ); + advance_t *advances = engine->advances( si ); + int which = glyphs[0] >> 8; + if (which > 0x20) + which = 0; + + int start = 0; + int end, i; + for ( end = 0; end < si->num_glyphs; ++end ) { + int e = glyphs[end] >> 8; + if (e > 0x20) + e = 0; + if ( e == which ) continue; + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + + // draw the text + QScriptItem si2 = *si; + si2.glyph_data_offset = si->glyph_data_offset + start; + si2.num_glyphs = end - start; + _engines[which]->draw( p, x, y, engine, &si2, textFlags ); + + // reset the high byte for all glyphs and advance to the next sub-string + const int hi = which << 8; + for ( i = start; i < end; ++i ) { + glyphs[i] = hi | glyphs[i]; + x += advances[i]; + } + + // change engine + start = end; + which = e; + } + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + // draw the text + QScriptItem si2 = *si; + si2.glyph_data_offset = si->glyph_data_offset + start; + si2.num_glyphs = end - start; + _engines[which]->draw( p, x, y, engine, &si2, textFlags ); + + // reset the high byte for all glyphs + if (which != 0) { + const int hi = which << 8; + for ( i = start; i < end; ++i ) + glyphs[i] = hi | glyphs[i]; + } +} + +glyph_metrics_t QFontEngineLatinXLFD::boundingBox( const glyph_t *glyphs_const, + const advance_t *advances, + const qoffset_t *offsets, + int numGlyphs ) +{ + if ( numGlyphs <= 0 ) return glyph_metrics_t(); + + glyph_metrics_t overall; + + glyph_t *glyphs = (glyph_t *) glyphs_const; + int which = glyphs[0] >> 8; + if (which > 0x20) + which = 0; + + int start = 0; + int end, i; + for ( end = 0; end < numGlyphs; ++end ) { + int e = glyphs[end] >> 8; + if (e > 0x20) + e = 0; + if ( e == which ) continue; + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + + // merge the bounding box for this run + const glyph_metrics_t gm = + _engines[which]->boundingBox( glyphs + start, + advances + start, + offsets + start, + end - start ); + + overall.x = QMIN( overall.x, gm.x ); + overall.y = QMIN( overall.y, gm.y ); + overall.width = overall.xoff + gm.width; + overall.height = QMAX( overall.height + overall.y, gm.height + gm.y ) - + QMIN( overall.y, gm.y ); + overall.xoff += gm.xoff; + overall.yoff += gm.yoff; + + // reset the high byte for all glyphs + if (which != 0) { + const int hi = which << 8; + for ( i = start; i < end; ++i ) + glyphs[i] = hi | glyphs[i]; + } + + // change engine + start = end; + which = e; + } + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + + // merge the bounding box for this run + const glyph_metrics_t gm = + _engines[which]->boundingBox( glyphs + start, + advances + start, + offsets + start, + end - start ); + + overall.x = QMIN( overall.x, gm.x ); + overall.y = QMIN( overall.y, gm.y ); + overall.width = overall.xoff + gm.width; + overall.height = QMAX( overall.height + overall.y, gm.height + gm.y ) - + QMIN( overall.y, gm.y ); + overall.xoff += gm.xoff; + overall.yoff += gm.yoff; + + // reset the high byte for all glyphs + if (which != 0) { + const int hi = which << 8; + for ( i = start; i < end; ++i ) + glyphs[i] = hi | glyphs[i]; + } + + return overall; +} + +glyph_metrics_t QFontEngineLatinXLFD::boundingBox( glyph_t glyph ) +{ + int engine = glyph >> 8; + if (engine > 0x20) + engine = 0; + Q_ASSERT( engine < _count ); + return _engines[engine]->boundingBox( engine > 0 ? glyph & 0xff : glyph ); +} + +int QFontEngineLatinXLFD::ascent() const +{ + return _engines[0]->ascent(); +} + +int QFontEngineLatinXLFD::descent() const +{ + return _engines[0]->descent(); +} + +int QFontEngineLatinXLFD::leading() const +{ + return _engines[0]->leading(); +} + +int QFontEngineLatinXLFD::maxCharWidth() const +{ + return _engines[0]->maxCharWidth(); +} + +int QFontEngineLatinXLFD::minLeftBearing() const +{ + return _engines[0]->minLeftBearing(); +} + +int QFontEngineLatinXLFD::minRightBearing() const +{ + return _engines[0]->minRightBearing(); +} + +const char *QFontEngineLatinXLFD::name() const +{ + return _engines[0]->name(); +} + +bool QFontEngineLatinXLFD::canRender( const QChar *string, int len ) +{ + bool all = TRUE; + int i; + for ( i = 0; i < len; ++i ) { + if ( string[i].unicode() >= 0x200 || + glyphIndices[string[i].unicode()] == 0 ) { + if (string[i].unicode() != 0x20ac || euroIndex == 0) + all = FALSE; + break; + } + } + + if ( all ) + return TRUE; + + all = TRUE; + for ( i = 0; i < len; ++i ) { + if ( string[i].unicode() >= 0x200 ) { + if (string[i].unicode() == 0x20ac) { + if (euroIndex) + continue; + + findEngine(string[i]); + if (euroIndex) + continue; + } + all = FALSE; + break; + } + if ( glyphIndices[string[i].unicode()] != 0 ) continue; + + findEngine( string[i] ); + if ( glyphIndices[string[i].unicode()] == 0 ) { + all = FALSE; + break; + } + } + + return all; +} + +void QFontEngineLatinXLFD::setScale( double scale ) +{ + int i; + for ( i = 0; i < _count; ++i ) + _engines[i]->setScale( scale ); + unsigned short chars[0x200]; + for ( i = 0; i < 0x200; ++i ) + chars[i] = i; + int glyphCount = 0x200; + _engines[0]->stringToCMap( (const QChar *)chars, 0x200, + glyphIndices, glyphAdvances, &glyphCount, FALSE ); +} + + +// ------------------------------------------------------------------ +// Xft cont engine +// ------------------------------------------------------------------ +// #define FONTENGINE_DEBUG + +#ifndef QT_NO_XFTFREETYPE +class Q_HackPaintDevice : public QPaintDevice +{ +public: + inline Q_HackPaintDevice() : QPaintDevice( 0 ) {} + inline XftDraw *xftDrawHandle() const { + return (XftDraw *)rendhd; + } + +}; + +#ifdef QT_XFT2 +static inline void getGlyphInfo( XGlyphInfo *xgi, XftFont *font, int glyph ) +{ + FT_UInt x = glyph; + XftGlyphExtents( QPaintDevice::x11AppDisplay(), font, &x, 1, xgi ); +} +#else +static inline XftFontStruct *getFontStruct( XftFont *font ) +{ + if (font->core) + return 0; + return font->u.ft.font; +} + +static inline void getGlyphInfo(XGlyphInfo *xgi, XftFont *font, int glyph) +{ + + XftTextExtents32(QPaintDevice::x11AppDisplay(), font, (XftChar32 *) &glyph, 1, xgi); +} +#endif // QT_XFT2 + +static inline FT_Face lockFTFace( XftFont *font ) +{ +#ifdef QT_XFT2 + return XftLockFace( font ); +#else + if (font->core) return 0; + return font->u.ft.font->face; +#endif // QT_XFT2 +} + +static inline void unlockFTFace( XftFont *font ) +{ +#ifdef QT_XFT2 + XftUnlockFace( font ); +#else + Q_UNUSED( font ); +#endif // QT_XFT2 +} + + + +QFontEngineXft::QFontEngineXft( XftFont *font, XftPattern *pattern, int cmap ) + : _font( font ), _pattern( pattern ), _openType( 0 ), _cmap( cmap ) +{ + _face = lockFTFace( _font ); + +#ifndef QT_XFT2 + XftFontStruct *xftfs = getFontStruct( _font ); + if ( xftfs ) { + // dirty hack: we set the charmap in the Xftfreetype to -1, so + // XftFreetype assumes no encoding and really draws glyph + // indices. The FT_Face still has the Unicode encoding to we + // can convert from Unicode to glyph index + xftfs->charmap = -1; + } +#else + _cmap = -1; + // Xft maps Unicode and adobe roman for us. + for (int i = 0; i < _face->num_charmaps; ++i) { + FT_CharMap cm = _face->charmaps[i]; +// qDebug("font has charmap %x", cm->encoding); + if (cm->encoding == ft_encoding_adobe_custom + || cm->encoding == ft_encoding_symbol) { +// qDebug("font has adobe custom or ms symbol charmap"); + _cmap = i; + break; + } + } +#endif // QT_XFT2 + + + cache_cost = _font->height * _font->max_advance_width * + ( _face ? _face->num_glyphs : 1024 ); + + // if the Xft font is not antialiased, it uses bitmaps instead of + // 8-bit alpha maps... adjust the cache_cost to reflect this + Bool antialiased = TRUE; + if ( XftPatternGetBool( pattern, XFT_ANTIALIAS, + 0, &antialiased ) == XftResultMatch && + ! antialiased ) { + cache_cost /= 8; + } + lbearing = SHRT_MIN; + rbearing = SHRT_MIN; + + memset( widthCache, 0, sizeof(widthCache) ); + memset( cmapCache, 0, sizeof(cmapCache) ); +} + +QFontEngineXft::~QFontEngineXft() +{ + delete _openType; + unlockFTFace( _font ); + + XftFontClose( QPaintDevice::x11AppDisplay(),_font ); + XftPatternDestroy( _pattern ); + _font = 0; + _pattern = 0; + TransformedFont *trf = transformed_fonts; + while ( trf ) { + XftFontClose( QPaintDevice::x11AppDisplay(), trf->xft_font ); + TransformedFont *tmp = trf; + trf = trf->next; + delete tmp; + } +} + +#ifdef QT_XFT2 +static glyph_t getAdobeCharIndex(XftFont *font, int cmap, uint ucs4) +{ + FT_Face _face = XftLockFace( font ); + FT_Set_Charmap(_face, _face->charmaps[cmap]); + glyph_t g = FT_Get_Char_Index(_face, ucs4); + XftUnlockFace(font); + return g; +} +#endif + +QFontEngine::Error QFontEngineXft::stringToCMap( const QChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + +#ifdef QT_XFT2 + if (_cmap != -1) { + for ( int i = 0; i < len; ++i ) { + unsigned short uc = str[i].unicode(); + if (mirrored) + uc = ::mirroredChar(str[i]).unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = XftCharIndex(0, _font, uc); + if (!glyph) + glyph = getAdobeCharIndex(_font, _cmap, uc); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((QFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else if ( mirrored ) { + for ( int i = 0; i < len; ++i ) { + unsigned short uc = ::mirroredChar(str[i]).unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + if (uc == 0xa0) + uc = 0x20; + glyph_t glyph = XftCharIndex(0, _font, uc); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((QFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else { + for ( int i = 0; i < len; ++i ) { + unsigned short uc = str[i].unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + if (uc == 0xa0) + uc = 0x20; + glyph_t glyph = XftCharIndex(0, _font, uc); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((QFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } + + if ( advances ) { + for ( int i = 0; i < len; i++ ) { + FT_UInt glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftGlyphExtents( QPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi ); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((QFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } +#else + if ( !_face ) { + if ( mirrored ) { + for ( int i = 0; i < len; i++ ) + glyphs[i] = ::mirroredChar(str[i]).unicode(); + } else { + for ( int i = 0; i < len; i++ ) + glyphs[i] = str[i].unicode(); + } + } else { + if ( _cmap == 1 ) { + // symbol font + for ( int i = 0; i < len; i++ ) { + unsigned short uc = str[i].unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = FT_Get_Char_Index( _face, uc ); + if(!glyph && uc < 0x100) + glyph = FT_Get_Char_Index( _face, uc+0xf000 ); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((QFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else if ( mirrored ) { + for ( int i = 0; i < len; i++ ) { + unsigned short uc = ::mirroredChar(str[i]).unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = FT_Get_Char_Index( _face, uc ); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((QFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else { + for ( int i = 0; i < len; i++ ) { + unsigned short uc = str[i].unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = FT_Get_Char_Index( _face, uc ); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((QFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } + } + + if ( advances ) { + for ( int i = 0; i < len; i++ ) { + XftChar16 glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftTextExtents16(QPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((QFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } +#endif // QT_XFT2 + + *nglyphs = len; + return NoError; +} + + +void QFontEngineXft::recalcAdvances( int len, glyph_t *glyphs, advance_t *advances ) +{ + +#ifdef QT_XFT2 + for ( int i = 0; i < len; i++ ) { + FT_UInt glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftGlyphExtents( QPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi ); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((QFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } +#else + for ( int i = 0; i < len; i++ ) { + XftChar16 glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftTextExtents16(QPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((QFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } +#endif // QT_XFT2 +} + +//#define FONTENGINE_DEBUG +void QFontEngineXft::draw( QPainter *p, int x, int y, const QTextEngine *engine, const QScriptItem *si, int textFlags ) +{ + if ( !si->num_glyphs ) + return; + + Display *dpy = QPaintDevice::x11AppDisplay(); + + int xorig = x; + int yorig = y; + + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + + XftFont *fnt = _font; + bool transform = FALSE; + if ( p->txop >= QPainter::TxScale || p->rop != Qt::CopyROP || _scale < 0.9999 || _scale > 1.001) { + bool can_scale = (_face->face_flags & FT_FACE_FLAG_SCALABLE) && p->rop == Qt::CopyROP; + double size = (p->m11()*p->m22() - p->m12()*p->m21())*_scale*_scale*fontDef.pixelSize*fontDef.pixelSize; + if (size > 256*256 || _scale < .9999 || _scale > 1.001) + can_scale = FALSE; + if (!can_scale) { + // font doesn't support transformations, need to do it by hand + float tmp = _scale; + _scale = 1.; + drawScaled(x, y, engine, si, textFlags, dpy, p->gc, p->device(), this, p->xmat, tmp); + _scale = tmp; + return; + } + + XftMatrix *mat = 0; + XftPatternGetMatrix( _pattern, XFT_MATRIX, 0, &mat ); + XftMatrix m2; + m2.xx = p->m11()*_scale; + m2.xy = -p->m21()*_scale; + m2.yx = -p->m12()*_scale; + m2.yy = p->m22()*_scale; + + // check if we have it cached + TransformedFont *trf = transformed_fonts; + TransformedFont *prev = 0; + int i = 0; + while ( trf ) { + if ( trf->xx == (float)m2.xx && + trf->xy == (float)m2.xy && + trf->yx == (float)m2.yx && + trf->yy == (float)m2.yy ) + break; + TransformedFont *tmp = trf; + trf = trf->next; + if (i > 10) { + XftFontClose( QPaintDevice::x11AppDisplay(), tmp->xft_font ); + delete tmp; + prev->next = trf; + } else { + prev = tmp; + } + ++i; + } + if ( trf ) { + if ( prev ) { + // move to beginning of list + prev->next = trf->next; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + fnt = trf->xft_font; + } else { + if ( mat ) + XftMatrixMultiply( &m2, &m2, mat ); + + XftPattern *pattern = XftPatternDuplicate( _pattern ); + XftPatternDel( pattern, XFT_MATRIX ); + XftPatternAddMatrix( pattern, XFT_MATRIX, &m2 ); + + fnt = XftFontOpenPattern( dpy, pattern ); +#ifndef QT_XFT2 + XftFontStruct *xftfs = getFontStruct( fnt ); + if ( xftfs ) { + // dirty hack: we set the charmap in the Xftfreetype to -1, so + // XftFreetype assumes no encoding and really draws glyph + // indices. The FT_Face still has the Unicode encoding to we + // can convert from Unicode to glyph index + xftfs->charmap = -1; + } +#endif // QT_XFT2 + TransformedFont *trf = new TransformedFont; + trf->xx = (float)m2.xx; + trf->xy = (float)m2.xy; + trf->yx = (float)m2.yx; + trf->yy = (float)m2.yy; + trf->xft_font = fnt; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + transform = TRUE; + } else if ( p->txop == QPainter::TxTranslate ) { + p->map( x, y, &x, &y ); + } + + glyph_t *glyphs = engine->glyphs( si ); + advance_t *advances = engine->advances( si ); + qoffset_t *offsets = engine->offsets( si ); + + const QColor &pen = p->cpen.color(); + XftDraw *draw = ((Q_HackPaintDevice *)p->pdev)->xftDrawHandle(); + + XftColor col; + col.color.red = pen.red () | pen.red() << 8; + col.color.green = pen.green () | pen.green() << 8; + col.color.blue = pen.blue () | pen.blue() << 8; + col.color.alpha = 0xffff; + col.pixel = pen.pixel(); +#ifdef FONTENGINE_DEBUG + qDebug("===== drawing %d glyphs reverse=%s ======", si->num_glyphs, si->analysis.bidiLevel % 2?"TRUE":"FALSE" ); + p->save(); + p->setBrush( Qt::white ); + glyph_metrics_t ci = boundingBox( glyphs, advances, offsets, si->num_glyphs ); + p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height ); + p->drawRect( x + ci.x, y + 100 + ci.y, ci.width, ci.height ); + qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height ); + p->restore(); + int yp = y; + int xp = x; +#endif + + if ( textFlags != 0 ) + drawLines( p, this, yorig, xorig, si->width, textFlags ); + + + if ( si->isSpace ) + return; + + if ( transform || si->hasPositioning ) { + if ( si->analysis.bidiLevel % 2 ) { + int i = si->num_glyphs; + while( i-- ) { + int xp = x + offsets[i].x; + int yp = y + offsets[i].y; + if ( transform ) + p->map( xp, yp, &xp, &yp ); +#ifdef QT_XFT2 + FT_UInt glyph = *(glyphs + i); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawGlyphs( draw, &col, fnt, xp, yp, &glyph, 1 ); +#else + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawString16( draw, &col, fnt, xp, yp, (XftChar16 *) (glyphs+i), 1); +#endif // QT_XFT2 +#ifdef FONTENGINE_DEBUG + glyph_metrics_t gi = boundingBox( glyphs[i] ); + p->drawRect( x+offsets[i].x+gi.x, y+offsets[i].y+100+gi.y, gi.width, gi.height ); + p->drawLine( x+offsets[i].x, y + 150 + 5*i , x+offsets[i].x+advances[i], y + 150 + 5*i ); + p->drawLine( x+offsets[i].x, y + 152 + 5*i , x+offsets[i].x+gi.xoff, y + 152 + 5*i ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=%d", i, gi.x, gi.y, gi.width, gi.height, + gi.xoff, gi.yoff, offsets[i].x, offsets[i].y, advances[i]); +#endif + x += advances[i]; + } + } else { + int i = 0; + while ( i < si->num_glyphs ) { + int xp = x + offsets[i].x; + int yp = y + offsets[i].y; + if ( transform ) + p->map( xp, yp, &xp, &yp ); +#ifdef QT_XFT2 + FT_UInt glyph = *(glyphs + i); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawGlyphs( draw, &col, fnt, xp, yp, &glyph, 1 ); +#else + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawString16( draw, &col, fnt, xp, yp, (XftChar16 *) (glyphs+i), 1 ); +#endif // QT_XFT2 + // qDebug("advance = %d/%d", adv.x, adv.y ); + x += advances[i]; + i++; + } + } + } else { + // Xft has real trouble drawing the glyphs on their own. + // Drawing them as one string increases performance significantly. +#ifdef QT_XFT2 + // #### we should use a different method anyways on Xft2 + FT_UInt g[64]; + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = QMIN(64, si->num_glyphs-gl); + int adv = 0; + if ( si->analysis.bidiLevel % 2 ) { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[si->num_glyphs-1-(gl+i)]; + adv += advances[si->num_glyphs-1-(gl+i)]; + } + } else { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[gl+i]; + adv += advances[gl+i]; + } + } + if (x + adv < SHRT_MAX && x > SHRT_MIN) + XftDrawGlyphs( draw, &col, fnt, x, y, g, toDraw ); + gl += toDraw; + x += adv; + } +#else + XftChar16 g[64]; + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = QMIN(64, si->num_glyphs-gl); + int adv = 0; + if ( si->analysis.bidiLevel % 2 ) { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[si->num_glyphs-1-(gl+i)]; + adv += advances[si->num_glyphs-1-(gl+i)]; + } + } else { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[gl+i]; + adv += advances[gl+i]; + } + } + if (x + adv < SHRT_MAX && x > SHRT_MIN) + XftDrawString16( draw, &col, fnt, x, y, g, toDraw ); + gl += toDraw; + x += adv; + } +#endif // QT_XFT2 + } + +#ifdef FONTENGINE_DEBUG + if ( !si->analysis.bidiLevel % 2 ) { + x = xp; + y = yp; + p->save(); + p->setPen( Qt::red ); + for ( int i = 0; i < si->num_glyphs; i++ ) { + glyph_metrics_t ci = boundingBox( glyphs[i] ); + p->drawRect( x + ci.x + offsets[i].x, y + 100 + ci.y + offsets[i].y, ci.width, ci.height ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=%d", i, ci.x, ci.y, ci.width, ci.height, + ci.xoff, ci.yoff, offsets[i].x, offsets[i].y, advances[i]); + x += advances[i]; + } + p->restore(); + } +#endif +} + +glyph_metrics_t QFontEngineXft::boundingBox( const glyph_t *glyphs, const advance_t *advances, const qoffset_t *offsets, int numGlyphs ) +{ + XGlyphInfo xgi; + + glyph_metrics_t overall; + int ymax = 0; + int xmax = 0; + if (_scale != 1) { + for (int i = 0; i < numGlyphs; i++) { + getGlyphInfo( &xgi, _font, glyphs[i] ); + int x = overall.xoff + offsets[i].x - xgi.x; + int y = overall.yoff + offsets[i].y - xgi.y; + overall.x = QMIN( overall.x, x ); + overall.y = QMIN( overall.y, y ); + xmax = QMAX( xmax, x + xgi.width ); + ymax = QMAX( ymax, y + xgi.height ); + overall.xoff += qRound(advances[i]/_scale); + } + overall.x = qRound(overall.x * _scale); + overall.y = qRound(overall.y * _scale); + overall.xoff = qRound(overall.xoff * _scale); + overall.yoff = qRound(overall.yoff * _scale); + xmax = qRound(xmax * _scale); + ymax = qRound(ymax * _scale); + } else { + for (int i = 0; i < numGlyphs; i++) { + getGlyphInfo( &xgi, _font, glyphs[i] ); + int x = overall.xoff + offsets[i].x - xgi.x; + int y = overall.yoff + offsets[i].y - xgi.y; + overall.x = QMIN( overall.x, x ); + overall.y = QMIN( overall.y, y ); + xmax = QMAX( xmax, x + xgi.width ); + ymax = QMAX( ymax, y + xgi.height ); + overall.xoff += advances[i]; + } + } + overall.height = ymax - overall.y; + overall.width = xmax - overall.x; + return overall; +} + +glyph_metrics_t QFontEngineXft::boundingBox( glyph_t glyph ) +{ + XGlyphInfo xgi; + getGlyphInfo( &xgi, _font, glyph ); + glyph_metrics_t gm = glyph_metrics_t( -xgi.x, -xgi.y, xgi.width, xgi.height, xgi.xOff, -xgi.yOff ); + if ( _scale != 1. ) { + gm.x = qRound(gm.x * _scale); + gm.y = qRound(gm.y * _scale); + gm.height = qRound(gm.height * _scale); + gm.width = qRound(gm.width * _scale); + gm.xoff = qRound(gm.xoff * _scale); + gm.yoff = qRound(gm.yoff * _scale); + } + return gm; +} + + + +int QFontEngineXft::ascent() const +{ + return qRound(_font->ascent*_scale); +} + +int QFontEngineXft::descent() const +{ + return qRound((_font->descent-1)*_scale); +} + +// #### use Freetype to determine this +int QFontEngineXft::leading() const +{ + int l = qRound(QMIN( _font->height - (_font->ascent + _font->descent), + ((_font->ascent + _font->descent) >> 4)*_scale )); + return (l > 0) ? l : 1; +} + +// #### use Freetype to determine this +int QFontEngineXft::lineThickness() const +{ + // ad hoc algorithm + int score = fontDef.weight * fontDef.pixelSize; + int lw = score / 700; + + // looks better with thicker line for small pointsizes + if ( lw < 2 && score >= 1050 ) lw = 2; + if ( lw == 0 ) lw = 1; + + return lw; +} + +// #### use Freetype to determine this +int QFontEngineXft::underlinePosition() const +{ + int pos = ( ( lineThickness() * 2 ) + 3 ) / 6; + return pos ? pos : 1; +} + +int QFontEngineXft::maxCharWidth() const +{ + return qRound(_font->max_advance_width*_scale); +} + +static const ushort char_table[] = { + 40, + 67, + 70, + 75, + 86, + 88, + 89, + 91, + 102, + 114, + 124, + 127, + 205, + 645, + 884, + 922, + 1070, + 12386 +}; + +static const int char_table_entries = sizeof(char_table)/sizeof(ushort); + + +int QFontEngineXft::minLeftBearing() const +{ + if ( lbearing == SHRT_MIN ) + minRightBearing(); // calculates both + + return lbearing; +} + +int QFontEngineXft::minRightBearing() const +{ + if ( rbearing == SHRT_MIN ) { + QFontEngineXft *that = (QFontEngineXft *)this; + that->lbearing = that->rbearing = 0; + QChar *ch = (QChar *)char_table; + glyph_t glyphs[char_table_entries]; + int ng = char_table_entries; + stringToCMap(ch, char_table_entries, glyphs, 0, &ng, FALSE); + while (--ng) { + if (glyphs[ng]) { + glyph_metrics_t gi = that->boundingBox( glyphs[ng] ); + if (gi.xoff) { + that->lbearing = QMIN(lbearing, gi.x); + that->rbearing = QMIN(rbearing, gi.xoff - gi.x - gi.width); + } + } + } + } + + return rbearing; +} + +int QFontEngineXft::cmap() const +{ + return _cmap; +} + +const char *QFontEngineXft::name() const +{ + return "xft"; +} + +void QFontEngineXft::setScale( double scale ) +{ + _scale = scale; +} + +bool QFontEngineXft::canRender( const QChar *string, int len ) +{ + bool allExist = TRUE; + +#ifdef QT_XFT2 + if (_cmap != -1) { + for ( int i = 0; i < len; i++ ) { + if (!XftCharExists(0, _font, string[i].unicode()) + && getAdobeCharIndex(_font, _cmap, string[i].unicode()) == 0) { + allExist = FALSE; + break; + } + } + } else { + for ( int i = 0; i < len; i++ ) { + if (!XftCharExists(0, _font, string[i].unicode())) { + allExist = FALSE; + break; + } + } + } +#else + glyph_t glyphs[256]; + int nglyphs = 255; + glyph_t *g = glyphs; + if ( stringToCMap( string, len, g, 0, &nglyphs, FALSE ) == OutOfMemory ) { + g = (glyph_t *)malloc( nglyphs*sizeof(glyph_t) ); + stringToCMap( string, len, g, 0, &nglyphs, FALSE ); + } + + for ( int i = 0; i < nglyphs; i++ ) { + if ( !XftGlyphExists(QPaintDevice::x11AppDisplay(), _font, g[i]) ) { + allExist = FALSE; + break; + } + } + + if ( g != glyphs ) + free( g ); +#endif // QT_XFT2 + + return allExist; +} + +QOpenType *QFontEngineXft::openType() const +{ +// qDebug("openTypeIface requested!"); + if ( _openType ) + return _openType; + + if ( !_face || ! FT_IS_SFNT( _face ) ) + return 0; + + QFontEngineXft *that = (QFontEngineXft *)this; + that->_openType = new QOpenType(that); + return _openType; +} + + +QFontEngine::Type QFontEngineXft::type() const +{ + return Xft; +} +#endif + + +// -------------------------------------------------------------------------------------------------------------------- +// Open type support +// -------------------------------------------------------------------------------------------------------------------- + +#ifndef QT_NO_XFTFREETYPE + +#include "qscriptengine_p.h" + +//#define OT_DEBUG + +#ifdef OT_DEBUG +static inline char *tag_to_string(FT_ULong tag) +{ + static char string[5]; + string[0] = (tag >> 24)&0xff; + string[1] = (tag >> 16)&0xff; + string[2] = (tag >> 8)&0xff; + string[3] = tag&0xff; + string[4] = 0; + return string; +} +#endif + +#define DefaultLangSys 0xffff +#define DefaultScript FT_MAKE_TAG('D', 'F', 'L', 'T') + +enum { + RequiresGsub = 1, + RequiresGpos = 2 +}; + +struct OTScripts { + unsigned int tag; + int flags; +}; + +static const OTScripts ot_scripts [] = { +// // European Alphabetic Scripts +// Latin, + { FT_MAKE_TAG( 'l', 'a', 't', 'n' ), 0 }, +// Greek, + { FT_MAKE_TAG( 'g', 'r', 'e', 'k' ), 0 }, +// Cyrillic, + { FT_MAKE_TAG( 'c', 'y', 'r', 'l' ), 0 }, +// Armenian, + { FT_MAKE_TAG( 'a', 'r', 'm', 'n' ), 0 }, +// Georgian, + { FT_MAKE_TAG( 'g', 'e', 'o', 'r' ), 0 }, +// Runic, + { FT_MAKE_TAG( 'r', 'u', 'n', 'r' ), 0 }, +// Ogham, + { FT_MAKE_TAG( 'o', 'g', 'a', 'm' ), 0 }, +// SpacingModifiers, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// CombiningMarks, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, + +// // Middle Eastern Scripts +// Hebrew, + { FT_MAKE_TAG( 'h', 'e', 'b', 'r' ), 1 }, +// Arabic, + { FT_MAKE_TAG( 'a', 'r', 'a', 'b' ), 1 }, +// Syriac, + { FT_MAKE_TAG( 's', 'y', 'r', 'c' ), 1 }, +// Thaana, + { FT_MAKE_TAG( 't', 'h', 'a', 'a' ), 1 }, + +// // South and Southeast Asian Scripts +// Devanagari, + { FT_MAKE_TAG( 'd', 'e', 'v', 'a' ), 1 }, +// Bengali, + { FT_MAKE_TAG( 'b', 'e', 'n', 'g' ), 1 }, +// Gurmukhi, + { FT_MAKE_TAG( 'g', 'u', 'r', 'u' ), 1 }, +// Gujarati, + { FT_MAKE_TAG( 'g', 'u', 'j', 'r' ), 1 }, +// Oriya, + { FT_MAKE_TAG( 'o', 'r', 'y', 'a' ), 1 }, +// Tamil, + { FT_MAKE_TAG( 't', 'a', 'm', 'l' ), 1 }, +// Telugu, + { FT_MAKE_TAG( 't', 'e', 'l', 'u' ), 1 }, +// Kannada, + { FT_MAKE_TAG( 'k', 'n', 'd', 'a' ), 1 }, +// Malayalam, + { FT_MAKE_TAG( 'm', 'l', 'y', 'm' ), 1 }, +// Sinhala, + // ### could not find any OT specs on this + { FT_MAKE_TAG( 's', 'i', 'n', 'h' ), 1 }, +// Thai, + { FT_MAKE_TAG( 't', 'h', 'a', 'i' ), 1 }, +// Lao, + { FT_MAKE_TAG( 'l', 'a', 'o', ' ' ), 1 }, +// Tibetan, + { FT_MAKE_TAG( 't', 'i', 'b', 't' ), 1 }, +// Myanmar, + { FT_MAKE_TAG( 'm', 'y', 'm', 'r' ), 1 }, +// Khmer, + { FT_MAKE_TAG( 'k', 'h', 'm', 'r' ), 1 }, + +// // East Asian Scripts +// Han, + { FT_MAKE_TAG( 'h', 'a', 'n', 'i' ), 0 }, +// Hiragana, + { FT_MAKE_TAG( 'k', 'a', 'n', 'a' ), 0 }, +// Katakana, + { FT_MAKE_TAG( 'k', 'a', 'n', 'a' ), 0 }, +// Hangul, + { FT_MAKE_TAG( 'h', 'a', 'n', 'g' ), 1 }, +// Bopomofo, + { FT_MAKE_TAG( 'b', 'o', 'p', 'o' ), 0 }, +// Yi, + { FT_MAKE_TAG( 'y', 'i', ' ', ' ' ), 0 }, + +// // Additional Scripts +// Ethiopic, + { FT_MAKE_TAG( 'e', 't', 'h', 'i' ), 0 }, +// Cherokee, + { FT_MAKE_TAG( 'c', 'h', 'e', 'r' ), 0 }, +// CanadianAboriginal, + { FT_MAKE_TAG( 'c', 'a', 'n', 's' ), 0 }, +// Mongolian, + { FT_MAKE_TAG( 'm', 'o', 'n', 'g' ), 0 }, +// // Symbols +// CurrencySymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// LetterlikeSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// NumberForms, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// MathematicalOperators, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// TechnicalSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// GeometricSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// MiscellaneousSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// EnclosedAndSquare, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// Braille, + { FT_MAKE_TAG( 'b', 'r', 'a', 'i' ), 0 }, +// Unicode, should be used + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 } + // ### where are these? +// { FT_MAKE_TAG( 'b', 'y', 'z', 'm' ), 0 }, +// { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, + // ### Hangul Jamo +// { FT_MAKE_TAG( 'j', 'a', 'm', 'o' ), 0 }, +}; + +QOpenType::QOpenType(QFontEngineXft *fe) + : fontEngine(fe), gdef(0), gsub(0), gpos(0), current_script(0) +{ + face = fe->face(); + otl_buffer_new(face->memory, &otl_buffer); + tmpAttributes = 0; + tmpLogClusters = 0; + + FT_Error error; + if ((error = TT_Load_GDEF_Table(face, &gdef))) { +#ifdef OT_DEBUG + qDebug("error loading gdef table: %d", error); +#endif + gdef = 0; + } + + if ((error = TT_Load_GSUB_Table(face, &gsub, gdef))) { + gsub = 0; +#ifdef OT_DEBUG + if (error != FT_Err_Table_Missing) { + qDebug("error loading gsub table: %d", error); + } else { + qDebug("face doesn't have a gsub table"); + } +#endif + } + + if ((error = TT_Load_GPOS_Table(face, &gpos, gdef))) { + gpos = 0; +#ifdef OT_DEBUG + qDebug("error loading gpos table: %d", error); +#endif + } + + for (uint i = 0; i < QFont::NScripts; ++i) + supported_scripts[i] = checkScript(i); +} + +QOpenType::~QOpenType() +{ + if (gpos) + TT_Done_GPOS_Table(gpos); + if (gsub) + TT_Done_GSUB_Table(gsub); + if (gdef) + TT_Done_GDEF_Table(gdef); + if (otl_buffer) + otl_buffer_free(otl_buffer); + if (tmpAttributes) + free(tmpAttributes); + if (tmpLogClusters) + free(tmpLogClusters); +} + +bool QOpenType::checkScript(unsigned int script) +{ + assert(script < QFont::NScripts); + + uint tag = ot_scripts[script].tag; + int requirements = ot_scripts[script].flags; + + if (requirements & RequiresGsub) { + if (!gsub) + return FALSE; + + FT_UShort script_index; + FT_Error error = TT_GSUB_Select_Script(gsub, tag, &script_index); + if (error) { +#ifdef OT_DEBUG + qDebug("could not select script %d in GSub table: %d", (int)script, error); +#endif + return FALSE; + } + } + + if (requirements & RequiresGpos) { + if (!gpos) + return FALSE; + + FT_UShort script_index; + FT_Error error = TT_GPOS_Select_Script(gpos, script, &script_index); + if (error) { +#ifdef OT_DEBUG + qDebug("could not select script in gpos table: %d", error); +#endif + return FALSE; + } + + } + return TRUE; +} + + +void QOpenType::selectScript(unsigned int script, const Features *features) +{ + if (current_script == script) + return; + + assert(script < QFont::NScripts); + // find script in our list of supported scripts. + uint tag = ot_scripts[script].tag; + + if (gsub && features) { +#ifdef OT_DEBUG + { + TTO_FeatureList featurelist = gsub->FeatureList; + int numfeatures = featurelist.FeatureCount; + qDebug("gsub table has %d features", numfeatures); + for(int i = 0; i < numfeatures; i++) { + TTO_FeatureRecord *r = featurelist.FeatureRecord + i; + qDebug(" feature '%s'", tag_to_string(r->FeatureTag)); + } + } +#endif + TT_GSUB_Clear_Features(gsub); + FT_UShort script_index; + FT_Error error = TT_GSUB_Select_Script(gsub, tag, &script_index); + if (!error) { +#ifdef OT_DEBUG + qDebug("script %s has script index %d", tag_to_string(script), script_index); +#endif + while (features->tag) { + FT_UShort feature_index; + error = TT_GSUB_Select_Feature(gsub, features->tag, script_index, 0xffff, &feature_index); + if (!error) { +#ifdef OT_DEBUG + qDebug(" adding feature %s", tag_to_string(features->tag)); +#endif + TT_GSUB_Add_Feature(gsub, feature_index, features->property); + } + ++features; + } + } + } + + if (gpos) { + TT_GPOS_Clear_Features(gpos); + FT_UShort script_index; + FT_Error error = TT_GPOS_Select_Script(gpos, tag, &script_index); + if (!error) { +#ifdef OT_DEBUG + { + TTO_FeatureList featurelist = gpos->FeatureList; + int numfeatures = featurelist.FeatureCount; + qDebug("gpos table has %d features", numfeatures); + for(int i = 0; i < numfeatures; i++) { + TTO_FeatureRecord *r = featurelist.FeatureRecord + i; + FT_UShort feature_index; + TT_GPOS_Select_Feature(gpos, r->FeatureTag, script_index, 0xffff, &feature_index); + qDebug(" feature '%s'", tag_to_string(r->FeatureTag)); + } + } +#endif + FT_ULong *feature_tag_list; + error = TT_GPOS_Query_Features(gpos, script_index, 0xffff, &feature_tag_list); + if (!error) { + while (*feature_tag_list) { + FT_UShort feature_index; + error = TT_GPOS_Select_Feature(gpos, *feature_tag_list, script_index, 0xffff, &feature_index); + if (!error) + TT_GPOS_Add_Feature(gpos, feature_index, PositioningProperties); + ++feature_tag_list; + } + } + } + } + + current_script = script; +} + +#ifdef OT_DEBUG +static void dump_string(OTL_Buffer buffer) +{ + for (uint i = 0; i < buffer->in_length; ++i) { + qDebug(" %x: cluster=%d", buffer->in_string[i].gindex, buffer->in_string[i].cluster); + } +} +#endif + +extern void qt_heuristicPosition(QShaperItem *item); + +bool QOpenType::shape(QShaperItem *item, const unsigned int *properties) +{ + length = item->num_glyphs; + + otl_buffer_clear(otl_buffer); + + tmpAttributes = (GlyphAttributes *) realloc(tmpAttributes, length*sizeof(GlyphAttributes)); + tmpLogClusters = (unsigned int *) realloc(tmpLogClusters, length*sizeof(unsigned int)); + for (int i = 0; i < length; ++i) { + otl_buffer_add_glyph(otl_buffer, item->glyphs[i], properties ? properties[i] : 0, i); + tmpAttributes[i] = item->attributes[i]; + tmpLogClusters[i] = item->log_clusters[i]; + } + +#ifdef OT_DEBUG + qDebug("-----------------------------------------"); +// qDebug("log clusters before shaping:"); +// for (int j = 0; j < length; j++) +// qDebug(" log[%d] = %d", j, item->log_clusters[j]); + qDebug("original glyphs: %p", item->glyphs); + for (int i = 0; i < length; ++i) + qDebug(" glyph=%4x", otl_buffer->in_string[i].gindex); +// dump_string(otl_buffer); +#endif + + loadFlags = FT_LOAD_DEFAULT; + + if (gsub) { + uint error = TT_GSUB_Apply_String(gsub, otl_buffer); + if (error && error != TTO_Err_Not_Covered) + return false; + } + +#ifdef OT_DEBUG +// qDebug("log clusters before shaping:"); +// for (int j = 0; j < length; j++) +// qDebug(" log[%d] = %d", j, item->log_clusters[j]); + qDebug("shaped glyphs:"); + for (int i = 0; i < length; ++i) + qDebug(" glyph=%4x", otl_buffer->in_string[i].gindex); + qDebug("-----------------------------------------"); +// dump_string(otl_buffer); +#endif + + return true; +} + +bool QOpenType::positionAndAdd(QShaperItem *item, bool doLogClusters) +{ + if (gpos) { +#ifdef Q_WS_X11 + Q_ASSERT(fontEngine->type() == QFontEngine::Xft); + face = lockFTFace(static_cast<QFontEngineXft *>(fontEngine)->font()); +#endif + memset(otl_buffer->positions, 0, otl_buffer->in_length*sizeof(OTL_PositionRec)); + // #### check that passing "FALSE,FALSE" is correct + TT_GPOS_Apply_String(face, gpos, loadFlags, otl_buffer, FALSE, FALSE); +#ifdef Q_WS_X11 + unlockFTFace(static_cast<QFontEngineXft *>(fontEngine)->font()); +#endif + } + + // make sure we have enough space to write everything back + if (item->num_glyphs < (int)otl_buffer->in_length) { + item->num_glyphs = otl_buffer->in_length; + return FALSE; + } + + for (unsigned int i = 0; i < otl_buffer->in_length; ++i) { + item->glyphs[i] = otl_buffer->in_string[i].gindex; + item->attributes[i] = tmpAttributes[otl_buffer->in_string[i].cluster]; + if (i && otl_buffer->in_string[i].cluster == otl_buffer->in_string[i-1].cluster) + item->attributes[i].clusterStart = FALSE; + } + item->num_glyphs = otl_buffer->in_length; + + if (doLogClusters) { + // we can't do this for indic, as we pass the stuf in syllables and it's easier to do it in the shaper. + unsigned short *logClusters = item->log_clusters; + int clusterStart = 0; + int oldCi = 0; + for (unsigned int i = 0; i < otl_buffer->in_length; ++i) { + int ci = otl_buffer->in_string[i].cluster; + // qDebug(" ci[%d] = %d mark=%d, cmb=%d, cs=%d", + // i, ci, glyphAttributes[i].mark, glyphAttributes[i].combiningClass, glyphAttributes[i].clusterStart); + if (!item->attributes[i].mark && item->attributes[i].clusterStart && ci != oldCi) { + for (int j = oldCi; j < ci; j++) + logClusters[j] = clusterStart; + clusterStart = i; + oldCi = ci; + } + } + for (int j = oldCi; j < length; j++) + logClusters[j] = clusterStart; + } + + // calulate the advances for the shaped glyphs +// qDebug("unpositioned: "); + static_cast<QFontEngineXft *>(item->font)->recalcAdvances(item->num_glyphs, item->glyphs, item->advances); + + // positioning code: + if (gpos) { + float scale = item->font->scale(); + OTL_Position positions = otl_buffer->positions; + +// qDebug("positioned glyphs:"); + for (unsigned int i = 0; i < otl_buffer->in_length; i++) { +// qDebug(" %d:\t orig advance: (%d/%d)\tadv=(%d/%d)\tpos=(%d/%d)\tback=%d\tnew_advance=%d", i, +// glyphs[i].advance.x.toInt(), glyphs[i].advance.y.toInt(), +// (int)(positions[i].x_advance >> 6), (int)(positions[i].y_advance >> 6), +// (int)(positions[i].x_pos >> 6), (int)(positions[i].y_pos >> 6), +// positions[i].back, positions[i].new_advance); + // ###### fix the case where we have y advances. How do we handle this in Uniscribe????? + if (positions[i].new_advance) { + item->advances[i] = item->flags & QTextEngine::RightToLeft + ? -qRound((positions[i].x_advance >> 6)*scale) + : qRound((positions[i].x_advance >> 6)*scale); + } else { + item->advances[i] += item->flags & QTextEngine::RightToLeft + ? -qRound((positions[i].x_advance >> 6)*scale) + : qRound((positions[i].x_advance >> 6)*scale); + } + item->offsets[i].x = qRound((positions[i].x_pos >> 6)*scale); + item->offsets[i].y = -qRound((positions[i].y_pos >> 6)*scale); + int back = positions[i].back; + if (item->flags & QTextEngine::RightToLeft) { + while (back--) { + item->offsets[i].x -= item->advances[i-back]; + } + } else { + while (back) { + item->offsets[i].x -= item->advances[i-back]; + --back; + } + } +// qDebug(" ->\tadv=%d\tpos=(%d/%d)", +// glyphs[i].advance.x.toInt(), glyphs[i].offset.x.toInt(), glyphs[i].offset.y.toInt()); + } + item->has_positioning = TRUE; + } else { + qt_heuristicPosition(item); + } + +#ifdef OT_DEBUG +// if (doLogClusters) { +// qDebug("log clusters after shaping:"); +// for (int j = 0; j < length; j++) +// qDebug(" log[%d] = %d", j, item->log_clusters[j]); +// } + qDebug("final glyphs:"); + for (int i = 0; i < (int)otl_buffer->in_length; ++i) + qDebug(" glyph=%4x char_index=%d mark: %d cmp: %d, clusterStart: %d advance=%d offset=%d/%d", + item->glyphs[i], otl_buffer->in_string[i].cluster, item->attributes[i].mark, + item->attributes[i].combiningClass, item->attributes[i].clusterStart, + item->advances[i], + item->offsets[i].x, item->offsets[i].y); + qDebug("-----------------------------------------"); +#endif + return TRUE; +} + +#endif diff --git a/src/kernel/qfontinfo.h b/src/kernel/qfontinfo.h new file mode 100644 index 0000000..e51547f --- /dev/null +++ b/src/kernel/qfontinfo.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Definition of QFontInfo class +** +** Created : 950131 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QFONTINFO_H +#define QFONTINFO_H + +#ifndef QT_H +#include "qfont.h" +#endif // QT_H + + +class Q_EXPORT QFontInfo +{ +public: + QFontInfo( const QFont & ); + QFontInfo( const QFont &, QFont::Script ); + QFontInfo( const QFontInfo & ); + ~QFontInfo(); + + QFontInfo &operator=( const QFontInfo & ); + + QString family() const; + int pixelSize() const; + int pointSize() const; + bool italic() const; + int weight() const; + bool bold() const; + bool underline() const; + bool overline() const; + bool strikeOut() const; + bool fixedPitch() const; + QFont::StyleHint styleHint() const; + bool rawMode() const; + + bool exactMatch() const; + + +private: + QFontInfo( const QPainter * ); + + QFontPrivate *d; + QPainter *painter; + int fscript; + + friend class QWidget; + friend class QPainter; +}; + + +inline bool QFontInfo::bold() const +{ return weight() > QFont::Normal; } + + +#endif // QFONTINFO_H diff --git a/src/kernel/qfontmetrics.h b/src/kernel/qfontmetrics.h new file mode 100644 index 0000000..c86fc2b --- /dev/null +++ b/src/kernel/qfontmetrics.h @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Definition of QFontMetrics class +** +** Created : 940514 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QFONTMETRICS_H +#define QFONTMETRICS_H + +#ifndef QT_H +#include "qfont.h" +#include "qrect.h" +#endif // QT_H + +#ifdef Q_WS_QWS +class QFontEngine; +#endif + +class QTextCodec; +class QTextParag; + +class Q_EXPORT QFontMetrics +{ +public: + QFontMetrics( const QFont & ); + QFontMetrics( const QFont &, QFont::Script ); + QFontMetrics( const QFontMetrics & ); + ~QFontMetrics(); + + QFontMetrics &operator=( const QFontMetrics & ); + + int ascent() const; + int descent() const; + int height() const; + int leading() const; + int lineSpacing() const; + int minLeftBearing() const; + int minRightBearing() const; + int maxWidth() const; + + bool inFont(QChar) const; + + int leftBearing(QChar) const; + int rightBearing(QChar) const; + int width( const QString &, int len = -1 ) const; + + int width( QChar ) const; +#ifndef QT_NO_COMPAT + int width( char c ) const { return width( (QChar) c ); } +#endif + + int charWidth( const QString &str, int pos ) const; + QRect boundingRect( const QString &, int len = -1 ) const; + QRect boundingRect( QChar ) const; + QRect boundingRect( int x, int y, int w, int h, int flags, + const QString& str, int len=-1, int tabstops=0, + int *tabarray=0, QTextParag **intern=0 ) const; + QSize size( int flags, + const QString& str, int len=-1, int tabstops=0, + int *tabarray=0, QTextParag **intern=0 ) const; + + int underlinePos() const; + int overlinePos() const; + int strikeOutPos() const; + int lineWidth() const; + +private: + QFontMetrics( const QPainter * ); + + friend class QWidget; + friend class QPainter; + friend class QTextFormat; +#if defined( Q_WS_MAC ) + friend class QFontPrivate; +#endif + + QFontPrivate *d; + QPainter *painter; + int fscript; +}; + + +#endif // QFONTMETRICS_H diff --git a/src/kernel/qgif.h b/src/kernel/qgif.h new file mode 100644 index 0000000..4ce6f10 --- /dev/null +++ b/src/kernel/qgif.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** To enable built-in reading of GIF images in Qt, change the definition +** below to "#define QT_BUILTIN_GIF_READER 1". +** +** To disable built-in reading of GIF images in Qt, change the definition +** below to "#define QT_BUILTIN_GIF_READER 0". +** +** WARNING: +** A separate license from Unisys may be required to use the gif +** reader. See http://www.unisys.com/about__unisys/lzw/ +** for information from Unisys +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QGIF_H +#define QGIF_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +#ifndef QT_BUILTIN_GIF_READER +#define QT_BUILTIN_GIF_READER 0 +#endif + +bool qt_builtin_gif_reader(); + +#endif // QGIF_H diff --git a/src/kernel/qgplugin.cpp b/src/kernel/qgplugin.cpp new file mode 100644 index 0000000..710cdde --- /dev/null +++ b/src/kernel/qgplugin.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 2001-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qgplugin.h" + +#ifndef QT_NO_COMPONENT + +#include <private/qcom_p.h> + +QGPlugin::QGPlugin() + : _iface( 0 ) +{ +} + +QGPlugin::QGPlugin( QUnknownInterface *i ) + : _iface( i ) +{ +} + +QGPlugin::~QGPlugin() +{ +} + +QUnknownInterface* QGPlugin::iface() +{ + Q_ASSERT( _iface ); + QUnknownInterface *i; + _iface->queryInterface( IID_QUnknown, &i ); + return i; +} + +void QGPlugin::setIface( QUnknownInterface *iface ) +{ + _iface = iface; +} + +#endif // QT_NO_COMPONENT diff --git a/src/kernel/qgplugin.h b/src/kernel/qgplugin.h new file mode 100644 index 0000000..3fa4493 --- /dev/null +++ b/src/kernel/qgplugin.h @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 2001-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QGPLUGIN_H +#define QGPLUGIN_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of a number of Qt sources files. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_COMPONENT + +#ifndef Q_EXTERN_C +#ifdef __cplusplus +#define Q_EXTERN_C extern "C" +#else +#define Q_EXTERN_C extern +#endif +#endif + +#ifndef Q_EXPORT_PLUGIN +#if defined(QT_THREAD_SUPPORT) +#define QT_THREADED_BUILD 1 +#define Q_PLUGIN_FLAGS_STRING "11" +#else +#define QT_THREADED_BUILD 0 +#define Q_PLUGIN_FLAGS_STRING "01" +#endif + +// this is duplicated at Q_UCM_VERIFICATION_DATA in qcom_p.h +// NOTE: if you change pattern, you MUST change the pattern in +// qcomlibrary.cpp as well. changing the pattern will break all +// backwards compatibility as well (no old plugins will be loaded). +#ifndef Q_PLUGIN_VERIFICATION_DATA +# define Q_PLUGIN_VERIFICATION_DATA \ + static const char *qt_ucm_verification_data = \ + "pattern=""QT_UCM_VERIFICATION_DATA""\n" \ + "version="QT_VERSION_STR"\n" \ + "flags="Q_PLUGIN_FLAGS_STRING"\n" \ + "buildkey="QT_BUILD_KEY"\0"; +#endif // Q_PLUGIN_VERIFICATION_DATA + +#define Q_PLUGIN_INSTANTIATE( IMPLEMENTATION ) \ + { \ + IMPLEMENTATION *i = new IMPLEMENTATION; \ + return i->iface(); \ + } + +# ifdef Q_WS_WIN +# ifdef Q_CC_BOR +# define Q_EXPORT_PLUGIN(PLUGIN) \ + Q_PLUGIN_VERIFICATION_DATA \ + Q_EXTERN_C __declspec(dllexport) \ + const char * __stdcall qt_ucm_query_verification_data() \ + { return qt_ucm_verification_data; } \ + Q_EXTERN_C __declspec(dllexport) QUnknownInterface* \ + __stdcall ucm_instantiate() \ + Q_PLUGIN_INSTANTIATE( PLUGIN ) +# else +# define Q_EXPORT_PLUGIN(PLUGIN) \ + Q_PLUGIN_VERIFICATION_DATA \ + Q_EXTERN_C __declspec(dllexport) \ + const char *qt_ucm_query_verification_data() \ + { return qt_ucm_verification_data; } \ + Q_EXTERN_C __declspec(dllexport) QUnknownInterface* ucm_instantiate() \ + Q_PLUGIN_INSTANTIATE( PLUGIN ) +# endif +# else +# define Q_EXPORT_PLUGIN(PLUGIN) \ + Q_PLUGIN_VERIFICATION_DATA \ + Q_EXTERN_C \ + const char *qt_ucm_query_verification_data() \ + { return qt_ucm_verification_data; } \ + Q_EXTERN_C QUnknownInterface* ucm_instantiate() \ + Q_PLUGIN_INSTANTIATE( PLUGIN ) +# endif + +#endif + +struct QUnknownInterface; + +class Q_EXPORT QGPlugin : public QObject +{ + Q_OBJECT +public: + QGPlugin( QUnknownInterface *i ); + ~QGPlugin(); + + QUnknownInterface* iface(); + void setIface( QUnknownInterface *iface ); + +private: + QGPlugin(); + QUnknownInterface* _iface; +}; + +#endif // QT_NO_COMPONENT + +#endif // QGPLUGIN_H diff --git a/src/kernel/qguardedptr.cpp b/src/kernel/qguardedptr.cpp new file mode 100644 index 0000000..0d51e96 --- /dev/null +++ b/src/kernel/qguardedptr.cpp @@ -0,0 +1,226 @@ +/**************************************************************************** +** +** Implementation of QGuardedPtr class +** +** Created : 990929 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qguardedptr.h" + +/*! + \class QGuardedPtr qguardedptr.h + \brief The QGuardedPtr class is a template class that provides guarded pointers to QObjects. + + \ingroup objectmodel + \mainclass + + A guarded pointer, \c{QGuardedPtr<X>}, behaves like a normal C++ + pointer \c{X*}, except that it is automatically set to 0 when + the referenced object is destroyed (unlike normal C++ pointers, + which become "dangling pointers" in such cases). \c X must be a + subclass of QObject. + + Guarded pointers are useful whenever you need to store a pointer + to a QObject that is owned by someone else and therefore might be + destroyed while you still hold a reference to it. You can safely + test the pointer for validity. + + Example: + \code + QGuardedPtr<QLabel> label = new QLabel( 0, "label" ); + label->setText( "I like guarded pointers" ); + + delete (QLabel*) label; // simulate somebody destroying the label + + if ( label) + label->show(); + else + qDebug("The label has been destroyed"); + \endcode + + The program will output \c{The label has been destroyed} rather + than dereferencing an invalid address in \c label->show(). + + The functions and operators available with a QGuardedPtr are the + same as those available with a normal unguarded pointer, except + the pointer arithmetic operators (++, --, -, and +), which are + normally used only with arrays of objects. Use them like normal + pointers and you will not need to read this class documentation. + + For creating guarded pointers, you can construct or assign to them + from an X* or from another guarded pointer of the same type. You + can compare them with each other using operator==() and + operator!=(), or test for 0 with isNull(). And you can dereference + them using either the \c *x or the \c x->member notation. + + A guarded pointer will automatically cast to an X*, so you can + freely mix guarded and unguarded pointers. This means that if you + have a QGuardedPtr<QWidget>, you can pass it to a function that + requires a QWidget*. For this reason, it is of little value to + declare functions to take a QGuardedPtr as a parameter; just use + normal pointers. Use a QGuardedPtr when you are storing a pointer + over time. + + Note again that class \e X must inherit QObject, or a compilation + or link error will result. +*/ + +/*! + \fn QGuardedPtr::QGuardedPtr() + + Constructs a 0 guarded pointer. + + \sa isNull() +*/ + +/*! + \fn QGuardedPtr::QGuardedPtr( T* p ) + + Constructs a guarded pointer that points to same object as \a p + points to. +*/ + +/*! + \fn QGuardedPtr::QGuardedPtr(const QGuardedPtr<T> &p) + + Copy one guarded pointer from another. The constructed guarded + pointer points to the same object that \a p points to (which may + be 0). +*/ + +/*! + \fn QGuardedPtr::~QGuardedPtr() + + Destroys the guarded pointer. Just like a normal pointer, + destroying a guarded pointer does \e not destroy the object being + pointed to. +*/ + +/*! + \fn QGuardedPtr<T>& QGuardedPtr::operator=(const QGuardedPtr<T> &p) + + Assignment operator. This guarded pointer then points to the same + object as \a p points to. +*/ + +/*! + \overload QGuardedPtr<T> & QGuardedPtr::operator=(T* p) + + Assignment operator. This guarded pointer then points to the same + object as \a p points to. +*/ + +/*! + \fn bool QGuardedPtr::operator==( const QGuardedPtr<T> &p ) const + + Equality operator; implements traditional pointer semantics. + Returns TRUE if both \a p and this guarded pointer are 0, or if + both \a p and this pointer point to the same object; otherwise + returns FALSE. + + \sa operator!=() +*/ + +/*! + \fn bool QGuardedPtr::operator!= ( const QGuardedPtr<T>& p ) const + + Inequality operator; implements pointer semantics, the negation of + operator==(). Returns TRUE if \a p and this guarded pointer are + not pointing to the same object; otherwise returns FALSE. +*/ + +/*! + \fn bool QGuardedPtr::isNull() const + + Returns \c TRUE if the referenced object has been destroyed or if + there is no referenced object; otherwise returns FALSE. +*/ + +/*! + \fn T* QGuardedPtr::operator->() const + + Overloaded arrow operator; implements pointer semantics. Just use + this operator as you would with a normal C++ pointer. +*/ + +/*! + \fn T& QGuardedPtr::operator*() const + + Dereference operator; implements pointer semantics. Just use this + operator as you would with a normal C++ pointer. +*/ + +/*! + \fn QGuardedPtr::operator T*() const + + Cast operator; implements pointer semantics. Because of this + function you can pass a QGuardedPtr\<X\> to a function where an X* + is required. +*/ + + +/* Internal classes */ + + +QGuardedPtrPrivate::QGuardedPtrPrivate( QObject* o) + : QObject(0, "_ptrpriv" ), obj( o ) +{ + if ( obj ) + connect( obj, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) ); +} + + +QGuardedPtrPrivate::~QGuardedPtrPrivate() +{ +} + +void QGuardedPtrPrivate::reconnect( QObject *o ) +{ + if ( obj == o ) + return; + if ( obj ) + disconnect( obj, SIGNAL( destroyed() ), + this, SLOT( objectDestroyed() ) ); + obj = o; + if ( obj ) + connect( obj, SIGNAL( destroyed() ), + this, SLOT( objectDestroyed() ) ); +} + +void QGuardedPtrPrivate::objectDestroyed() +{ + obj = 0; +} diff --git a/src/kernel/qguardedptr.h b/src/kernel/qguardedptr.h new file mode 100644 index 0000000..b66a280 --- /dev/null +++ b/src/kernel/qguardedptr.h @@ -0,0 +1,144 @@ +/**************************************************************************** +** +** Definition of QGuardedPtr class +** +** Created : 990929 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QGUARDEDPTR_H +#define QGUARDEDPTR_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +// ### 4.0: rename to something without Private in it. Not really internal. +class Q_EXPORT QGuardedPtrPrivate : public QObject, public QShared +{ + Q_OBJECT +public: + QGuardedPtrPrivate( QObject* ); + ~QGuardedPtrPrivate(); + + QObject* object() const; + void reconnect( QObject* ); + +private slots: + void objectDestroyed(); + +private: + QObject* obj; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QGuardedPtrPrivate( const QGuardedPtrPrivate & ); + QGuardedPtrPrivate &operator=( const QGuardedPtrPrivate & ); +#endif +}; + +template <class T> +class QGuardedPtr +{ +public: + QGuardedPtr() : priv( new QGuardedPtrPrivate( 0 ) ) {} + + QGuardedPtr( T* o) { + priv = new QGuardedPtrPrivate( (QObject*)o ); + } + + QGuardedPtr(const QGuardedPtr<T> &p) { + priv = p.priv; + ref(); + } + + ~QGuardedPtr() { deref(); } + + QGuardedPtr<T> &operator=(const QGuardedPtr<T> &p) { + if ( priv != p.priv ) { + deref(); + priv = p.priv; + ref(); + } + return *this; + } + + QGuardedPtr<T> &operator=(T* o) { + if ( priv && priv->count == 1 ) { + priv->reconnect( (QObject*)o ); + } else { + deref(); + priv = new QGuardedPtrPrivate( (QObject*)o ); + } + return *this; + } + + bool operator==( const QGuardedPtr<T> &p ) const { + return (T*)(*this) == (T*) p; + } + + bool operator!= ( const QGuardedPtr<T>& p ) const { + return !( *this == p ); + } + + bool isNull() const { return !priv || !priv->object(); } + + T* operator->() const { return (T*)(priv?priv->object():0); } + + T& operator*() const { return *((T*)(priv?priv->object():0)); } + + operator T*() const { return (T*)(priv?priv->object():0); } + +private: + + void ref() { if (priv) priv->ref(); } + + void deref() { + if ( priv && priv->deref() ) + delete priv; + } + + QGuardedPtrPrivate* priv; +}; + + + + +inline QObject* QGuardedPtrPrivate::object() const +{ + return obj; +} + +#define Q_DEFINED_QGUARDEDPTR +#include "qwinexport.h" +#endif diff --git a/src/kernel/qiconset.cpp b/src/kernel/qiconset.cpp new file mode 100644 index 0000000..223b0d7 --- /dev/null +++ b/src/kernel/qiconset.cpp @@ -0,0 +1,949 @@ +/**************************************************************************** +** +** Implementation of QIconSet class +** +** Created : 980318 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qiconset.h" + +#ifndef QT_NO_ICONSET + +#include "qapplication.h" +#include "qbitmap.h" +#include "qcleanuphandler.h" +#include "qimage.h" +#include "qpainter.h" + +enum { NumSizes = 2, NumModes = 3, NumStates = 2 }; + +static QIconFactory *defaultFac = 0; +static QSingleCleanupHandler<QIconFactory> q_cleanup_icon_factory; + +static short widths[2] = { 22, 32 }; +static short heights[2] = { 22, 32 }; + +enum QIconSetIconOrigin { + SuppliedFileName, // 'fileName' contains the name of the file + SuppliedPixmap, // 'pixmap' is a pointer to the user-supplied pixmap + Manufactured, // 'pixmap' is a factory-generated pixmap (or 0) + Generated // 'pixmap' is a QIconSet-generated pixmap (or 0) +}; + +struct QIconSetIcon +{ + QIconSetIconOrigin origin; + union { + QString *fileName; + QPixmap *pixmap; + }; + + QIconSetIcon() : origin( Generated ) { pixmap = 0; } + QIconSetIcon( const QIconSetIcon& other ) + : origin( Generated ) { + pixmap = 0; + operator=( other ); + } + ~QIconSetIcon() { + if ( origin == SuppliedFileName ) { + delete fileName; + } else { + delete pixmap; + } + } + + QIconSetIcon& operator=( const QIconSetIcon& other ); + + void clearCached() { + if ( pixmap && (origin == Manufactured || origin == Generated) ) { + origin = Generated; + delete pixmap; + pixmap = 0; + } + } +}; + +QIconSetIcon& QIconSetIcon::operator=( const QIconSetIcon& other ) +{ + QPixmap *oldPixmap = 0; + QString *oldFileName = 0; + if ( origin == SuppliedFileName ) { + oldFileName = fileName; + } else { + oldPixmap = pixmap; + } + + origin = other.origin; + if ( other.origin == SuppliedFileName ) { + fileName = new QString( *other.fileName ); + } else { + if ( other.pixmap ) { + pixmap = new QPixmap( *other.pixmap ); + } else { + pixmap = 0; + } + } + delete oldPixmap; + delete oldFileName; + return *this; +} + +class QIconSetPrivate : public QShared +{ +public: + QIconSetIcon icons[NumSizes][NumModes][NumStates]; + QPixmap defaultPix; + QIconFactory *factory; + + QIconSetPrivate() : factory( 0 ) { } + QIconSetPrivate( const QIconSetPrivate& other ) : QShared() { + count = 1; + for ( int i = 0; i < NumSizes; i++ ) { + for ( int j = 0; j < NumModes; j++ ) { + for ( int k = 0; k < NumStates; k++ ) { + icons[i][j][k] = other.icons[i][j][k]; + } + } + } + defaultPix = other.defaultPix; + factory = other.factory; + if ( factory ) + factory->ref(); + } + ~QIconSetPrivate() { + setFactory( 0 ); + } + + QIconSetIcon *icon( const QIconSet *iconSet, QIconSet::Size size, + QIconSet::Mode mode, QIconSet::State state ); + void setFactory( QIconFactory *newFactory ) { + if ( newFactory ) + newFactory->ref(); + if ( factory && factory->deref() && factory->autoDelete() ) + delete factory; + factory = newFactory; + } + + Q_DUMMY_COMPARISON_OPERATOR( QIconSetPrivate ) +}; + +QIconSetIcon *QIconSetPrivate::icon( const QIconSet *iconSet, + QIconSet::Size size, QIconSet::Mode mode, + QIconSet::State state ) +{ + QIconSetIcon *ik = &icons[(int) size - 1][(int) mode][(int) state]; + + if ( iconSet ) { + if ( ik->origin == SuppliedFileName ) { + QPixmap *newPixmap = new QPixmap( *ik->fileName ); + delete ik->fileName; + + if ( newPixmap->isNull() ) { + delete newPixmap; + ik->origin = Generated; + ik->pixmap = 0; + } else { + ik->origin = SuppliedPixmap; + ik->pixmap = newPixmap; + } + } + + if ( !ik->pixmap && ik->origin == Generated ) { + QIconFactory *f = factory; + if ( !f ) + f = defaultFac; + + if ( f ) { + /* + We set 'origin' to Manufactured half a second too + early to prevent recursive calls to this function. + (This can happen if createPixmap() calls + QIconSet::pixmap(), which in turn calls this + function.) + */ + ik->origin = Manufactured; + ik->pixmap = f->createPixmap( *iconSet, size, mode, state ); + if ( !ik->pixmap ) + ik->origin = Generated; + } + } + } + return ik; +} + +/*! \class QIconSet + + \brief The QIconSet class provides a set of icons with different + styles and sizes. + + \ingroup graphics + \ingroup images + \ingroup shared + \mainclass + + A QIconSet can generate smaller, larger, active, and disabled pixmaps + from the set of icons it is given. Such pixmaps are used by + QToolButton, QHeader, QPopupMenu, etc. to show an icon representing a + particular action. + + The simplest use of QIconSet is to create one from a QPixmap and then + use it, allowing Qt to work out all the required icon styles and + sizes. For example: + + \code + QToolButton *but = new QToolButton( QIconSet( QPixmap("open.xpm") ), ... ); + \endcode + + Using whichever pixmaps you specify as a base, QIconSet provides a + set of six icons, each with a \l Size and a \l Mode: Small Normal, + Small Disabled, Small Active, Large Normal, Large Disabled, and + Large Active. + + An additional set of six icons can be provided for widgets that have + an "On" or "Off" state, like checkable menu items or toggleable + toolbuttons. If you provide pixmaps for the "On" state, but not for + the "Off" state, the QIconSet will provide the "Off" pixmaps. You may + specify icons for both states in you wish. + + You can set any of the icons using setPixmap(). + + When you retrieve a pixmap using pixmap(Size, Mode, State), + QIconSet will return the icon that has been set or previously + generated for that size, mode and state combination. If none is + available, QIconSet will ask the icon factory. If the icon factory + cannot provide any (the default), QIconSet generates a pixmap based + on the pixmaps it has been given and returns it. + + The \c Disabled appearance is computed using an algorithm that + produces results very similar to those used in Microsoft Windows + 95. The \c Active appearance is identical to the \c Normal + appearance unless you use setPixmap() to set it to something + special. + + When scaling icons, QIconSet uses \link QImage::smoothScale() + smooth scaling\endlink, which can partially blend the color component + of pixmaps. If the results look poor, the best solution + is to supply pixmaps in both large and small sizes. + + You can use the static function setIconSize() to set the preferred + size of the generated large/small icons. The default small size is + 22 x 22, while the default large size is 32 x 32. These sizes only + affect generated icons. + + The isGenerated() function returns TRUE if an icon was generated by + QIconSet or by a factory; clearGenerated() clears all cached + pixmaps. + + \section1 Making Classes that Use QIconSet + + If you write your own widgets that have an option to set a small + pixmap, consider allowing a QIconSet to be set for that pixmap. The + Qt class QToolButton is an example of such a widget. + + Provide a method to set a QIconSet, and when you draw the icon, choose + whichever icon is appropriate for the current state of your widget. + For example: + \code + void MyWidget::drawIcon( QPainter* p, QPoint pos ) + { + p->drawPixmap( pos, icons->pixmap( + QIconSet::Small, + isEnabled() ? QIconSet::Normal : + QIconSet::Disabled, + isEnabled() ? QIconSet::On : + QIconSet::Off)); + } + \endcode + + You might also make use of the \c Active mode, perhaps making your + widget \c Active when the mouse is over the widget (see \l + QWidget::enterEvent()), while the mouse is pressed pending the + release that will activate the function, or when it is the currently + selected item. If the widget can be toggled, the "On" mode might be + used to draw a different icon. + + \img iconset.png QIconSet + + \sa QIconFactory QPixmap QMainWindow::setUsesBigPixmaps() + \link guibooks.html#fowler GUI Design Handbook: Iconic Label \endlink +*/ + + +/*! + \enum QIconSet::Size + + This enum type describes the size at which a pixmap is intended to be + used. + The currently defined sizes are: + + \value Automatic The size of the pixmap is determined from its + pixel size. This is a useful default. + \value Small The pixmap is the smaller of two. + \value Large The pixmap is the larger of two. + + If a Small pixmap is not set by QIconSet::setPixmap(), the Large + pixmap will be automatically scaled down to the size of a small pixmap + to generate the Small pixmap when required. Similarly, a Small pixmap + will be automatically scaled up to generate a Large pixmap. The + preferred sizes for large/small generated icons can be set using + setIconSize(). + + \sa setIconSize() iconSize() setPixmap() pixmap() QMainWindow::setUsesBigPixmaps() +*/ + +/*! + \enum QIconSet::Mode + + This enum type describes the mode for which a pixmap is intended to be + used. + The currently defined modes are: + + \value Normal + Display the pixmap when the user is + not interacting with the icon, but the + functionality represented by the icon is available. + \value Disabled + Display the pixmap when the + functionality represented by the icon is not available. + \value Active + Display the pixmap when the + functionality represented by the icon is available and + the user is interacting with the icon, for example, moving the + mouse over it or clicking it. +*/ + +/*! + \enum QIconSet::State + + This enum describes the state for which a pixmap is intended to be + used. The \e state can be: + + \value Off Display the pixmap when the widget is in an "off" state + \value On Display the pixmap when the widget is in an "on" state + + \sa setPixmap() pixmap() +*/ + +/*! + Constructs a null icon set. + + \sa setPixmap(), reset() +*/ +QIconSet::QIconSet() + : d( 0 ) +{ +} + +/*! + Constructs an icon set for which the Normal pixmap is \a pixmap, + which is assumed to be of size \a size. + + The default for \a size is \c Automatic, which means that QIconSet + will determine whether the pixmap is Small or Large from its pixel + size. Pixmaps less than the width of a small generated icon are + considered to be Small. You can use setIconSize() to set the + preferred size of a generated icon. + + \sa setIconSize() reset() +*/ +QIconSet::QIconSet( const QPixmap& pixmap, Size size ) + : d( 0 ) +{ + reset( pixmap, size ); +} + +/*! Creates an iconset which uses the pixmap \a smallPix for for + displaying a small icon, and the pixmap \a largePix for displaying a + large icon. +*/ +QIconSet::QIconSet( const QPixmap& smallPix, const QPixmap& largePix ) + : d( 0 ) +{ + reset( smallPix, Small ); + reset( largePix, Large ); +} + +/*! + Constructs a copy of \a other. This is very fast. +*/ +QIconSet::QIconSet( const QIconSet& other ) + : d( other.d ) +{ + if ( d ) + d->ref(); +} + +/*! + Destroys the icon set and frees any allocated resources. +*/ +QIconSet::~QIconSet() +{ + if ( d && d->deref() ) + delete d; +} + +/*! + Sets this icon set to use pixmap \a pixmap for the Normal pixmap, + assuming it to be of size \a size. + + This is equivalent to assigning QIconSet(\a pixmap, \a size) to this + icon set. + + This function does nothing if \a pixmap is a null pixmap. +*/ +void QIconSet::reset( const QPixmap& pixmap, Size size ) +{ + if ( pixmap.isNull() ) + return; + + detach(); + normalize( size, pixmap.size() ); + setPixmap( pixmap, size, Normal ); + d->defaultPix = pixmap; + d->setFactory( 0 ); +} + +/*! + Sets this icon set to provide pixmap \a pixmap for size \a size, mode \a + mode and state \a state. The icon set may also use \a pixmap for + generating other pixmaps if they are not explicitly set. + + The \a size can be one of Automatic, Large or Small. If Automatic is + used, QIconSet will determine if the pixmap is Small or Large from its + pixel size. + + Pixmaps less than the width of a small generated icon are + considered to be Small. You can use setIconSize() to set the preferred + size of a generated icon. + + This function does nothing if \a pixmap is a null pixmap. + + \sa reset() +*/ +void QIconSet::setPixmap( const QPixmap& pixmap, Size size, Mode mode, + State state ) +{ + if ( pixmap.isNull() ) + return; + + normalize( size, pixmap.size() ); + + detach(); + clearGenerated(); + + QIconSetIcon *icon = d->icon( 0, size, mode, state ); + if ( icon->origin == SuppliedFileName ) { + delete icon->fileName; + icon->pixmap = 0; + } + icon->origin = SuppliedPixmap; + if ( icon->pixmap == 0 ) { + icon->pixmap = new QPixmap( pixmap ); + } else { + *icon->pixmap = pixmap; + } +} + +/*! + \overload + + The pixmap is loaded from \a fileName when it becomes necessary. +*/ +void QIconSet::setPixmap( const QString& fileName, Size size, Mode mode, + State state ) +{ + if ( size == Automatic ) { + setPixmap( QPixmap(fileName), size, mode, state ); + } else { + detach(); + clearGenerated(); + + QIconSetIcon *icon = d->icon( 0, size, mode, state ); + if ( icon->origin == SuppliedFileName ) { + *icon->fileName = fileName; + } else { + delete icon->pixmap; + icon->fileName = new QString( fileName ); + icon->origin = SuppliedFileName; + } + } +} + +/*! + Returns a pixmap with size \a size, mode \a mode and state \a + state, generating one if necessary. Generated pixmaps are cached. +*/ +QPixmap QIconSet::pixmap( Size size, Mode mode, State state ) const +{ + if ( !d ) { + if ( defaultFac ) { + QIconSet *that = (QIconSet *) this; + that->detach(); + } else { + return QPixmap(); + } + } + + if ( size == Automatic ) + size = Small; + + QIconSetIcon *icon = d->icon( this, size, mode, state ); + if ( icon->pixmap ) + return *icon->pixmap; + if ( icon->origin == Manufactured ) { + /* + This can only occur during the half a second's time when + the icon is being manufactured. If QIconFactory somehow + tries to access the pixmap it's supposed to be creating, it + will get a null pixmap. + */ + return QPixmap(); + } + + if ( mode == Active ) + return pixmap( size, Normal, state ); + + Size otherSize = ( size == Large ) ? Small : Large; + QIconSetIcon *otherSizeIcon = d->icon( this, otherSize, mode, state ); + + if ( state == Off ) { + if ( mode == Disabled && + d->icon(this, size, Normal, Off)->origin != Generated ) { + icon->pixmap = createDisabled( size, Off ); + } else if ( otherSizeIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeIcon->pixmap ); + } else if ( mode == Disabled ) { + icon->pixmap = createDisabled( size, Off ); + } else if ( !d->defaultPix.isNull() ) { + icon->pixmap = new QPixmap( d->defaultPix ); + } else { + /* + No icons are available for { TRUE, Normal, Off } and + { FALSE, Normal, Off }. Try the other 10 combinaisons, + best ones first. + */ + const int N = 10; + static const struct { + bool sameSize; + Mode mode; + State state; + } tryList[N] = { + { TRUE, Active, Off }, + { TRUE, Normal, On }, + { TRUE, Active, On }, + { FALSE, Active, Off }, + { FALSE, Normal, On }, + { FALSE, Active, On }, + { TRUE, Disabled, Off }, + { TRUE, Disabled, On }, + { FALSE, Disabled, Off }, + { FALSE, Disabled, On } + }; + + for ( int i = 0; i < N; i++ ) { + bool sameSize = tryList[i].sameSize; + QIconSetIcon *tryIcon = + d->icon( this, sameSize ? size : otherSize, + tryList[i].mode, tryList[i].state ); + if ( tryIcon->origin != Generated ) { + if ( sameSize ) { + if ( tryIcon->pixmap ) + icon->pixmap = new QPixmap( *tryIcon->pixmap ); + } else { + icon->pixmap = createScaled( size, tryIcon->pixmap ); + } + break; + } + } + } + } else { /* ( state == On ) */ + if ( mode == Normal ) { + if ( otherSizeIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeIcon->pixmap ); + } else { + icon->pixmap = new QPixmap( pixmap(size, mode, Off) ); + } + } else { /* ( mode == Disabled ) */ + QIconSetIcon *offIcon = d->icon( this, size, mode, Off ); + QIconSetIcon *otherSizeOffIcon = d->icon( this, otherSize, mode, + Off ); + + if ( offIcon->origin != Generated ) { + if ( offIcon->pixmap ) + icon->pixmap = new QPixmap( *offIcon->pixmap ); + } else if ( d->icon(this, size, Normal, On)->origin != Generated ) { + icon->pixmap = createDisabled( size, On ); + } else if ( otherSizeIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeIcon->pixmap ); + } else if ( otherSizeOffIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeOffIcon->pixmap ); + } else { + icon->pixmap = createDisabled( size, On ); + } + } + } + if ( icon->pixmap ) { + return *icon->pixmap; + } else { + return QPixmap(); + } +} + +/*! \overload + \obsolete + + This is the same as pixmap(\a size, \a enabled, \a state). +*/ +QPixmap QIconSet::pixmap( Size size, bool enabled, State state ) const +{ + return pixmap( size, enabled ? Normal : Disabled, state ); +} + +/*! + \overload + + Returns the pixmap originally provided to the constructor or to + reset(). This is the Normal pixmap of unspecified Size. + + \sa reset() +*/ +QPixmap QIconSet::pixmap() const +{ + if ( !d ) + return QPixmap(); + return d->defaultPix; +} + +/*! + Returns TRUE if the pixmap with size \a size, mode \a mode and + state \a state is generated from other pixmaps; otherwise returns + FALSE. + + A pixmap obtained from a QIconFactory is considered non-generated. +*/ +bool QIconSet::isGenerated( Size size, Mode mode, State state ) const +{ + if ( !d ) + return TRUE; + return d->icon( this, size, mode, state )->origin == Generated; +} + +/*! + Clears all cached pixmaps, including those obtained from an + eventual QIconFactory. +*/ +void QIconSet::clearGenerated() +{ + if ( !d ) + return; + + for ( int i = 0; i < NumSizes; i++ ) { + for ( int j = 0; j < NumModes; j++ ) { + for ( int k = 0; k < NumStates; k++ ) { + d->icons[i][j][k].clearCached(); + } + } + } +} + +/*! + Installs \a factory as the icon factory for this iconset. The + icon factory is used to generates pixmaps not set by the user. + + If no icon factory is installed, QIconFactory::defaultFactory() + is used. +*/ +void QIconSet::installIconFactory( QIconFactory *factory ) +{ + detach(); + d->setFactory( factory ); +} + +/*! + Returns TRUE if the icon set is empty; otherwise returns FALSE. +*/ +bool QIconSet::isNull() const +{ + return !d; +} + +/*! + Detaches this icon set from others with which it may share data. + + You will never need to call this function; other QIconSet functions + call it as necessary. +*/ +void QIconSet::detach() +{ + if ( !d ) { + d = new QIconSetPrivate; + return; + } + if ( d->count != 1 ) { + d->deref(); + d = new QIconSetPrivate( *d ); + } +} + +/*! + Assigns \a other to this icon set and returns a reference to this + icon set. + + \sa detach() +*/ +QIconSet& QIconSet::operator=( const QIconSet& other ) +{ + if ( other.d ) + other.d->ref(); + + if ( d && d->deref() ) + delete d; + d = other.d; + return *this; +} + +/*! + Set the preferred size for all small or large icons that are + generated after this call. If \a which is Small, sets the preferred + size of small generated icons to \a size. Similarly, if \a which is + Large, sets the preferred size of large generated icons to \a size. + + Note that cached icons will not be regenerated, so it is recommended + that you set the preferred icon sizes before generating any icon sets. + Also note that the preferred icon sizes will be ignored for icon sets + that have been created using both small and large pixmaps. + + \sa iconSize() +*/ +void QIconSet::setIconSize( Size which, const QSize& size ) +{ + widths[(int) which - 1] = size.width(); + heights[(int) which - 1] = size.height(); +} + +/*! + If \a which is Small, returns the preferred size of a small + generated icon; if \a which is Large, returns the preferred size + of a large generated icon. + + \sa setIconSize() +*/ +const QSize& QIconSet::iconSize( Size which ) +{ + // ### replace 'const QSize&' with QSize in Qt 4 and simply this code + static QSize size; + size = QSize( widths[(int) which - 1], heights[(int) which - 1] ); + return size; +} + +void QIconSet::normalize( Size& which, const QSize& pixSize ) +{ + if ( which == Automatic ) + which = pixSize.width() > iconSize( Small ).width() ? Large : Small; +} + +/*! + Returns a new pixmap that is a copy of \a suppliedPix, scaled to + the icon size \a size. +*/ +QPixmap *QIconSet::createScaled( Size size, const QPixmap *suppliedPix ) const +{ + if ( !suppliedPix || suppliedPix->isNull() ) + return 0; + + QImage img = suppliedPix->convertToImage(); + QSize imgSize = iconSize( size ); + if ( size == Small ) { + imgSize = imgSize.boundedTo( img.size() ); + } else { + imgSize = imgSize.expandedTo( img.size() ); + } + img = img.smoothScale( imgSize ); + + QPixmap *pixmap = new QPixmap( img ); + if ( !pixmap->mask() ) { + QBitmap mask; + mask.convertFromImage( img.createHeuristicMask(), + Qt::MonoOnly | Qt::ThresholdDither ); + pixmap->setMask( mask ); + } + return pixmap; +} + +/*! + Returns a new pixmap that has a 'disabled' look, taking as its + base the iconset's icon with size \a size and state \a state. +*/ +QPixmap *QIconSet::createDisabled( Size size, State state ) const +{ + QPixmap normalPix = pixmap( size, Normal, state ); + if ( normalPix.isNull() ) + return 0; + + QImage img; + QPixmap *pixmap = 0; + QBitmap normalMask; + if ( normalPix.mask() ) { + normalMask = *normalPix.mask(); + } else { + img = normalPix.convertToImage(); + normalMask.convertFromImage( img.createHeuristicMask(), + Qt::MonoOnly | Qt::ThresholdDither ); + } + + pixmap = new QPixmap( normalPix.width() + 1, + normalPix.height() + 1 ); + const QColorGroup &dis = QApplication::palette().disabled(); + pixmap->fill( dis.background() ); + + QPainter painter; + painter.begin( pixmap ); + painter.setPen( dis.base() ); + painter.drawPixmap( 1, 1, normalMask ); + painter.setPen( dis.foreground() ); + painter.drawPixmap( 0, 0, normalMask ); + painter.end(); + + if ( !normalMask.mask() ) + normalMask.setMask( normalMask ); + + QBitmap mask( pixmap->size() ); + mask.fill( Qt::color0 ); + painter.begin( &mask ); + painter.drawPixmap( 0, 0, normalMask ); + painter.drawPixmap( 1, 1, normalMask ); + painter.end(); + pixmap->setMask( mask ); + return pixmap; +} + +/*! \class QIconFactory + \ingroup advanced + \brief The QIconFactory class is used to create pixmaps for a QIconSet. + + By reimplementing createPixmap(), you can override QIconSet's + default algorithm for computing pixmaps not supplied by the user. + + Call setAutoDelete(TRUE) if you want the factory to automatically + delete itself when it is no longer needed by QIconSet. + + \sa QIconSet +*/ + +/*! + Constructs an icon factory. +*/ +QIconFactory::QIconFactory() + : autoDel( 0 ) +{ + count = 0; +} + +/*! + Destroys the object and frees any allocated resources. +*/ +QIconFactory::~QIconFactory() +{ +} + +/*! + Ceates a pixmap for \a iconSet with a certain \a size, \a mode, and + \a state. Returns 0 if the default QIconSet algorithm should be + used to create a pixmap that wasn't supplied by the user. + + It is the caller's responsibility to delete the returned pixmap. + + The default implementation always returns 0. +*/ +QPixmap *QIconFactory::createPixmap( const QIconSet& /* iconSet */, + QIconSet::Size /* size */, + QIconSet::Mode /* mode */, + QIconSet::State /* state */ ) +{ + return 0; +} + +/*! + \fn void QIconFactory::setAutoDelete( bool autoDelete ) + + If \a autoDelete is TRUE, sets the icon factory to automatically + delete itself when it is no longer referenced by any QIconSet and + isn't the default factory. If \a autoDelete is FALSE (the default) + auto-deletion is disabled. + + \sa autoDelete(), defaultFactory() +*/ + +/*! + \fn bool QIconFactory::autoDelete() const + + Returns TRUE if auto-deletion is enabled; otherwise returns FALSE. + + \sa setAutoDelete() +*/ + +/*! + Returns the default icon factory. + + \sa installDefaultFactory() +*/ +QIconFactory *QIconFactory::defaultFactory() +{ + if ( !defaultFac ) { + defaultFac = new QIconFactory; + defaultFac->setAutoDelete( TRUE ); + defaultFac->ref(); + q_cleanup_icon_factory.set( &defaultFac ); + } + return defaultFac; +} + +/*! + Replaces the default icon factory with \a factory. +*/ +void QIconFactory::installDefaultFactory( QIconFactory *factory ) +{ + if ( !factory ) + return; + + factory->ref(); + if ( defaultFac && defaultFac->deref() && defaultFac->autoDelete() ) + delete defaultFac; + defaultFac = factory; + q_cleanup_icon_factory.set( &defaultFac ); +} + +#endif // QT_NO_ICONSET diff --git a/src/kernel/qiconset.h b/src/kernel/qiconset.h new file mode 100644 index 0000000..e02ffbd --- /dev/null +++ b/src/kernel/qiconset.h @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Definition of QIconSet class +** +** Created : 980318 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QICONSET_H +#define QICONSET_H + +#ifndef QT_H +#include "qobject.h" +#include "qpixmap.h" +#endif // QT_H + +#ifndef QT_NO_ICONSET + +class QIconFactory; +class QIconSetPrivate; + +// ### Remove all 'virtual' functions in QIconSet (but not QIconFactory) in Qt 4.0 +class Q_EXPORT QIconSet +{ +public: + // the implementation makes assumptions about the value of these + enum Size { Automatic, Small, Large }; + enum Mode { Normal, Disabled, Active }; + enum State { On, Off }; + + QIconSet(); + QIconSet( const QPixmap& pixmap, Size size = Automatic ); + QIconSet( const QPixmap& smallPix, const QPixmap& largePix ); + QIconSet( const QIconSet& other ); + virtual ~QIconSet(); + + void reset( const QPixmap& pixmap, Size size ); + + virtual void setPixmap( const QPixmap& pixmap, Size size, + Mode mode = Normal, State state = Off ); + virtual void setPixmap( const QString& fileName, Size size, + Mode mode = Normal, State state = Off ); + QPixmap pixmap( Size size, Mode mode, State state = Off ) const; + QPixmap pixmap( Size size, bool enabled, State state = Off ) const; + QPixmap pixmap() const; + bool isGenerated( Size size, Mode mode, State state = Off ) const; + void clearGenerated(); + void installIconFactory( QIconFactory *factory ); + + bool isNull() const; + + void detach(); + + QIconSet& operator=( const QIconSet& other ); + + // static functions + static void setIconSize( Size which, const QSize& size ); + static const QSize& iconSize( Size which ); + +#ifndef Q_QDOC + Q_DUMMY_COMPARISON_OPERATOR(QIconSet) +#endif + +private: + void normalize( Size& which, const QSize& pixSize ); + QPixmap *createScaled( Size size, const QPixmap *suppliedPix ) const; + QPixmap *createDisabled( Size size, State state ) const; + + QIconSetPrivate *d; +}; + +class Q_EXPORT QIconFactory : private QShared +{ +public: + QIconFactory(); + virtual ~QIconFactory(); + + virtual QPixmap *createPixmap( const QIconSet& iconSet, QIconSet::Size size, + QIconSet::Mode mode, QIconSet::State state ); + void setAutoDelete( bool autoDelete ) { autoDel = autoDelete; } + bool autoDelete() const { return autoDel; } + + static QIconFactory *defaultFactory(); + static void installDefaultFactory( QIconFactory *factory ); + +private: +#if defined(Q_DISABLE_COPY) + QIconFactory( const QIconFactory & ); + QIconFactory &operator=( const QIconFactory & ); +#endif + + friend class QIconSet; + friend class QIconSetPrivate; + + uint autoDel : 1; + uint unused : 31; +}; + +#endif // QT_NO_ICONSET +#endif diff --git a/src/kernel/qimage.cpp b/src/kernel/qimage.cpp new file mode 100644 index 0000000..d5a18b9 --- /dev/null +++ b/src/kernel/qimage.cpp @@ -0,0 +1,6497 @@ +/**************************************************************************** +** +** Implementation of QImage and QImageIO classes +** +** Created : 950207 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qimage.h" +#include "qregexp.h" +#include "qfile.h" +#include "qdatastream.h" +#include "qtextstream.h" +#include "qbuffer.h" +#include "qptrlist.h" +#include "qasyncimageio.h" +#include "qpngio.h" +#include "qmngio.h" +#include "qjpegio.h" +#include "qmap.h" +#include <private/qpluginmanager_p.h> +#include "qimageformatinterface_p.h" +#include "qwmatrix.h" +#include "qapplication.h" +#include "qmime.h" +#include "qdragobject.h" +#include <ctype.h> +#include <stdlib.h> + +#ifdef Q_WS_QWS +#include "qgfx_qws.h" +#endif + +// 16bpp images on supported on Qt/Embedded +#if !defined( Q_WS_QWS ) && !defined(QT_NO_IMAGE_16_BIT) +#define QT_NO_IMAGE_16_BIT +#endif + + +/*! + \class QImage + \brief The QImage class provides a hardware-independent pixmap + representation with direct access to the pixel data. + + \ingroup images + \ingroup graphics + \ingroup shared + \mainclass + + It is one of the two classes Qt provides for dealing with images, + the other being QPixmap. QImage is designed and optimized for I/O + and for direct pixel access/manipulation. QPixmap is designed and + optimized for drawing. There are (slow) functions to convert + between QImage and QPixmap: QPixmap::convertToImage() and + QPixmap::convertFromImage(). + + An image has the parameters \link width() width\endlink, \link + height() height\endlink and \link depth() depth\endlink (bits per + pixel, bpp), a color table and the actual \link bits() + pixels\endlink. QImage supports 1-bpp, 8-bpp and 32-bpp image + data. 1-bpp and 8-bpp images use a color lookup table; the pixel + value is a color table index. + + 32-bpp images encode an RGB value in 24 bits and ignore the color + table. The most significant byte is used for the \link + setAlphaBuffer() alpha buffer\endlink. + + An entry in the color table is an RGB triplet encoded as a \c + uint. Use the \link ::qRed() qRed()\endlink, \link ::qGreen() + qGreen()\endlink and \link ::qBlue() qBlue()\endlink functions (\c + qcolor.h) to access the components, and \link ::qRgb() + qRgb\endlink to make an RGB triplet (see the QColor class + documentation). + + 1-bpp (monochrome) images have a color table with a most two + colors. There are two different formats: big endian (MSB first) or + little endian (LSB first) bit order. To access a single bit you + will must do some bit shifts: + + \code + QImage image; + // sets bit at (x,y) to 1 + if ( image.bitOrder() == QImage::LittleEndian ) + *(image.scanLine(y) + (x >> 3)) |= 1 << (x & 7); + else + *(image.scanLine(y) + (x >> 3)) |= 1 << (7 - (x & 7)); + \endcode + + If this looks complicated, it might be a good idea to convert the + 1-bpp image to an 8-bpp image using convertDepth(). + + 8-bpp images are much easier to work with than 1-bpp images + because they have a single byte per pixel: + + \code + QImage image; + // set entry 19 in the color table to yellow + image.setColor( 19, qRgb(255,255,0) ); + // set 8 bit pixel at (x,y) to value yellow (in color table) + *(image.scanLine(y) + x) = 19; + \endcode + + 32-bpp images ignore the color table; instead, each pixel contains + the RGB triplet. 24 bits contain the RGB value; the most + significant byte is reserved for the alpha buffer. + + \code + QImage image; + // sets 32 bit pixel at (x,y) to yellow. + uint *p = (uint *)image.scanLine(y) + x; + *p = qRgb(255,255,0); + \endcode + + On Qt/Embedded, scanlines are aligned to the pixel depth and may + be padded to any degree, while on all other platforms, the + scanlines are 32-bit aligned for all depths. The constructor + taking a \c{uchar*} argument always expects 32-bit aligned data. + On Qt/Embedded, an additional constructor allows the number of + bytes-per-line to be specified. + + QImage supports a variety of methods for getting information about + the image, for example, colorTable(), allGray(), isGrayscale(), + bitOrder(), bytesPerLine(), depth(), dotsPerMeterX() and + dotsPerMeterY(), hasAlphaBuffer(), numBytes(), numColors(), and + width() and height(). + + Pixel colors are retrieved with pixel() and set with setPixel(). + + QImage also supports a number of functions for creating a new + image that is a transformed version of the original. For example, + copy(), convertBitOrder(), convertDepth(), createAlphaMask(), + createHeuristicMask(), mirror(), scale(), smoothScale(), swapRGB() + and xForm(). There are also functions for changing attributes of + an image in-place, for example, setAlphaBuffer(), setColor(), + setDotsPerMeterX() and setDotsPerMeterY() and setNumColors(). + + Images can be loaded and saved in the supported formats. Images + are saved to a file with save(). Images are loaded from a file + with load() (or in the constructor) or from an array of data with + loadFromData(). The lists of supported formats are available from + inputFormatList() and outputFormatList(). + + Strings of text may be added to images using setText(). + + The QImage class uses explicit \link shclass.html sharing\endlink, + similar to that used by QMemArray. + + New image formats can be added as \link plugins-howto.html + plugins\endlink. + + \sa QImageIO QPixmap \link shclass.html Shared Classes\endlink +*/ + + +/*! + \enum QImage::Endian + + This enum type is used to describe the endianness of the CPU and + graphics hardware. + + \value IgnoreEndian Endianness does not matter. Useful for some + operations that are independent of endianness. + \value BigEndian Network byte order, as on SPARC and Motorola CPUs. + \value LittleEndian PC/Alpha byte order. +*/ + +/*! + \enum Qt::ImageConversionFlags + + The conversion flag is a bitwise-OR of the following values. The + options marked "(default)" are set if no other values from the + list are included (since the defaults are zero): + + Color/Mono preference (ignored for QBitmap) + \value AutoColor (default) - If the image has \link + QImage::depth() depth\endlink 1 and contains only + black and white pixels, the pixmap becomes monochrome. + \value ColorOnly The pixmap is dithered/converted to the + \link QPixmap::defaultDepth() native display depth\endlink. + \value MonoOnly The pixmap becomes monochrome. If necessary, + it is dithered using the chosen dithering algorithm. + + Dithering mode preference for RGB channels + \value DiffuseDither (default) - A high-quality dither. + \value OrderedDither A faster, more ordered dither. + \value ThresholdDither No dithering; closest color is used. + + Dithering mode preference for alpha channel + \value ThresholdAlphaDither (default) - No dithering. + \value OrderedAlphaDither A faster, more ordered dither. + \value DiffuseAlphaDither A high-quality dither. + \value NoAlpha Not supported. + + Color matching versus dithering preference + \value PreferDither (default when converting to a pixmap) - Always dither + 32-bit images when the image is converted to 8 bits. + \value AvoidDither (default when converting for the purpose of saving to + file) - Dither 32-bit images only if the image has more than 256 + colors and it is being converted to 8 bits. + \value AutoDither Not supported. + + The following are not values that are used directly, but masks for + the above classes: + \value ColorMode_Mask Mask for the color mode. + \value Dither_Mask Mask for the dithering mode for RGB channels. + \value AlphaDither_Mask Mask for the dithering mode for the alpha channel. + \value DitherMode_Mask Mask for the mode that determines the preference of + color matching versus dithering. + + Using 0 as the conversion flag sets all the default options. +*/ + +#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001) +#pragma message disable narrowptr +#endif + +#ifndef QT_NO_IMAGE_TEXT +class QImageDataMisc { +public: + QImageDataMisc() { } + QImageDataMisc( const QImageDataMisc& o ) : + text_lang(o.text_lang) { } + + QImageDataMisc& operator=(const QImageDataMisc& o) + { + text_lang = o.text_lang; + return *this; + } + QValueList<QImageTextKeyLang> list() + { + return text_lang.keys(); + } + + QStringList languages() + { + QStringList r; + QMap<QImageTextKeyLang,QString>::Iterator it = text_lang.begin(); + for ( ; it != text_lang.end(); ++it ) { + r.remove( it.key().lang ); + r.append( it.key().lang ); + } + return r; + } + QStringList keys() + { + QStringList r; + QMap<QImageTextKeyLang,QString>::Iterator it = text_lang.begin(); + for ( ; it != text_lang.end(); ++it ) { + r.remove( it.key().key ); + r.append( it.key().key ); + } + return r; + } + + QMap<QImageTextKeyLang,QString> text_lang; +}; +#endif // QT_NO_IMAGE_TEXT + + + +/***************************************************************************** + QImage member functions + *****************************************************************************/ + +// table to flip bits +static const uchar bitflip[256] = { + /* + open OUT, "| fmt"; + for $i (0..255) { + print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) | + (($i >> 3) & 0x04) | (($i >> 1) & 0x08) | + (($i << 7) & 0x80) | (($i << 5) & 0x40) | + (($i << 3) & 0x20) | (($i << 1) & 0x10), ", "; + } + close OUT; + */ + 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, + 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, + 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, + 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, + 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, + 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, + 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, + 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, + 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, + 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, + 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, + 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, + 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, + 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, + 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, + 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255 +}; + +const uchar *qt_get_bitflip_array() // called from QPixmap code +{ + return bitflip; +} + + +/*! + Constructs a null image. + + \sa isNull() +*/ + +QImage::QImage() +{ + init(); +} + +/*! + Constructs an image with \a w width, \a h height, \a depth bits + per pixel, \a numColors colors and bit order \a bitOrder. + + Using this constructor is the same as first constructing a null + image and then calling the create() function. + + \sa create() +*/ + +QImage::QImage( int w, int h, int depth, int numColors, Endian bitOrder ) +{ + init(); + create( w, h, depth, numColors, bitOrder ); +} + +/*! + Constructs an image with size \a size pixels, depth \a depth bits, + \a numColors and \a bitOrder endianness. + + Using this constructor is the same as first constructing a null + image and then calling the create() function. + + \sa create() +*/ +QImage::QImage( const QSize& size, int depth, int numColors, Endian bitOrder ) +{ + init(); + create( size, depth, numColors, bitOrder ); +} + +#ifndef QT_NO_IMAGEIO +/*! + Constructs an image and tries to load the image from the file \a + fileName. + + If \a format is specified, the loader attempts to read the image + using the specified format. If \a format is not specified (which + is the default), the loader reads a few bytes from the header to + guess the file format. + + If the loading of the image failed, this object is a \link + isNull() null\endlink image. + + The QImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa load() isNull() QImageIO +*/ + +QImage::QImage( const QString &fileName, const char* format ) +{ + init(); + load( fileName, format ); +} + +#ifndef QT_NO_IMAGEIO_XPM +// helper +static void read_xpm_image_or_array( QImageIO *, const char * const *, QImage & ); +#endif +/*! + Constructs an image from \a xpm, which must be a valid XPM image. + + Errors are silently ignored. + + Note that it's possible to squeeze the XPM variable a little bit + by using an unusual declaration: + + \code + static const char * const start_xpm[]={ + "16 15 8 1", + "a c #cec6bd", + .... + \endcode + + The extra \c const makes the entire definition read-only, which is + slightly more efficient (e.g. when the code is in a shared + library) and ROMable when the application is to be stored in ROM. +*/ + +QImage::QImage( const char * const xpm[] ) +{ + init(); +#ifndef QT_NO_IMAGEIO_XPM + read_xpm_image_or_array( 0, xpm, *this ); +#else + // We use a qFatal rather than disabling the whole function, as this + // constructor may be ambiguous. + qFatal("XPM not supported"); +#endif +} + +/*! + Constructs an image from the binary data \a array. It tries to + guess the file format. + + If the loading of the image failed, this object is a \link + isNull() null\endlink image. + + \sa loadFromData() isNull() imageFormat() +*/ +QImage::QImage( const QByteArray &array ) +{ + init(); + loadFromData(array); +} +#endif //QT_NO_IMAGEIO + + +/*! + Constructs a \link shclass.html shallow copy\endlink of \a image. +*/ + +QImage::QImage( const QImage &image ) +{ + data = image.data; + data->ref(); +} + +/*! + Constructs an image \a w pixels wide, \a h pixels high with a + color depth of \a depth, that uses an existing memory buffer, \a + yourdata. The buffer must remain valid throughout the life of the + QImage. The image does not delete the buffer at destruction. + + If \a colortable is 0, a color table sufficient for \a numColors + will be allocated (and destructed later). + + Note that \a yourdata must be 32-bit aligned. + + The endianness is given in \a bitOrder. +*/ +QImage::QImage( uchar* yourdata, int w, int h, int depth, + QRgb* colortable, int numColors, + Endian bitOrder ) +{ + init(); + int bpl = ((w*depth+31)/32)*4; // bytes per scanline + if ( w <= 0 || h <= 0 || depth <= 0 || numColors < 0 + || INT_MAX / sizeof(uchar *) < uint(h) + || INT_MAX / uint(depth) < uint(w) + || bpl <= 0 + || INT_MAX / uint(bpl) < uint(h) ) + return; // invalid parameter(s) + data->w = w; + data->h = h; + data->d = depth; + data->ncols = depth != 32 ? numColors : 0; + if ( !yourdata ) + return; // Image header info can be saved without needing to allocate memory. + data->nbytes = bpl*h; + if ( colortable || !data->ncols ) { + data->ctbl = colortable; + data->ctbl_mine = FALSE; + } else { + // calloc since we realloc, etc. later (ick) + data->ctbl = (QRgb*)calloc( data->ncols*sizeof(QRgb), data->ncols ); + Q_CHECK_PTR(data->ctbl); + data->ctbl_mine = TRUE; + } + uchar** jt = (uchar**)malloc(h*sizeof(uchar*)); + Q_CHECK_PTR(jt); + for (int j=0; j<h; j++) { + jt[j] = yourdata+j*bpl; + } + data->bits = jt; + data->bitordr = bitOrder; +} + +#ifdef Q_WS_QWS + +/*! + Constructs an image that uses an existing memory buffer. The + buffer must remain valid for the life of the QImage. The image + does not delete the buffer at destruction. The buffer is passed as + \a yourdata. The image's width is \a w and its height is \a h. The + color depth is \a depth. \a bpl specifies the number of bytes per + line. + + If \a colortable is 0, a color table sufficient for \a numColors + will be allocated (and destructed later). + + The endianness is specified by \a bitOrder. + + \warning This constructor is only available on Qt/Embedded. +*/ +QImage::QImage( uchar* yourdata, int w, int h, int depth, + int bpl, QRgb* colortable, int numColors, + Endian bitOrder ) +{ + init(); + if ( !yourdata || w <= 0 || h <= 0 || depth <= 0 || numColors < 0 + || INT_MAX / sizeof(uchar *) < uint(h) + || INT_MAX / uint(bpl) < uint(h) + ) + return; // invalid parameter(s) + data->w = w; + data->h = h; + data->d = depth; + data->ncols = numColors; + data->nbytes = bpl * h; + if ( colortable || !numColors ) { + data->ctbl = colortable; + data->ctbl_mine = FALSE; + } else { + // calloc since we realloc, etc. later (ick) + data->ctbl = (QRgb*)calloc( numColors*sizeof(QRgb), numColors ); + Q_CHECK_PTR(data->ctbl); + data->ctbl_mine = TRUE; + } + uchar** jt = (uchar**)malloc(h*sizeof(uchar*)); + Q_CHECK_PTR(jt); + for (int j=0; j<h; j++) { + jt[j] = yourdata+j*bpl; + } + data->bits = jt; + data->bitordr = bitOrder; +} +#endif // Q_WS_QWS + +/*! + Destroys the image and cleans up. +*/ + +QImage::~QImage() +{ + if ( data && data->deref() ) { + reset(); + delete data; + } +} + + + + +/*! Convenience function. Gets the data associated with the absolute + name \a abs_name from the default mime source factory and decodes it + to an image. + + \sa QMimeSourceFactory, QImage::fromMimeSource(), QImageDrag::decode() +*/ +#ifndef QT_NO_MIME +QImage QImage::fromMimeSource( const QString &abs_name ) +{ + const QMimeSource *m = QMimeSourceFactory::defaultFactory()->data( abs_name ); + if ( !m ) { +#if defined(QT_CHECK_STATE) + qWarning("QImage::fromMimeSource: Cannot find image \"%s\" in the mime source factory", abs_name.latin1() ); +#endif + return QImage(); + } + QImage img; + QImageDrag::decode( m, img ); + return img; +} +#endif + + +/*! + Assigns a \link shclass.html shallow copy\endlink of \a image to + this image and returns a reference to this image. + + \sa copy() +*/ + +QImage &QImage::operator=( const QImage &image ) +{ + image.data->ref(); // avoid 'x = x' + if ( data->deref() ) { + reset(); + delete data; + } + data = image.data; + return *this; +} + +/*! + \overload + + Sets the image bits to the \a pixmap contents and returns a + reference to the image. + + If the image shares data with other images, it will first + dereference the shared data. + + Makes a call to QPixmap::convertToImage(). +*/ + +QImage &QImage::operator=( const QPixmap &pixmap ) +{ + *this = pixmap.convertToImage(); + return *this; +} + +/*! + Detaches from shared image data and makes sure that this image is + the only one referring to the data. + + If multiple images share common data, this image makes a copy of + the data and detaches itself from the sharing mechanism. + Nothing is done if there is just a single reference. + + \sa copy() +*/ + +void QImage::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + +/*! + Returns a \link shclass.html deep copy\endlink of the image. + + \sa detach() +*/ + +QImage QImage::copy() const +{ + if ( isNull() ) { + // maintain the fields of invalid QImages when copied + return QImage( 0, width(), height(), depth(), colorTable(), numColors(), bitOrder() ); + } else { + QImage image; + image.create( width(), height(), depth(), numColors(), bitOrder() ); +#ifdef Q_WS_QWS + // Qt/Embedded can create images with non-default bpl + // make sure we don't crash. + if ( image.numBytes() != numBytes() ) + for ( int i = 0; i < height(); i++ ) + memcpy( image.scanLine(i), scanLine(i), image.bytesPerLine() ); + else +#endif + memcpy( image.bits(), bits(), numBytes() ); + memcpy( image.colorTable(), colorTable(), numColors() * sizeof(QRgb) ); + image.setAlphaBuffer( hasAlphaBuffer() ); + image.data->dpmx = dotsPerMeterX(); + image.data->dpmy = dotsPerMeterY(); + image.data->offset = offset(); +#ifndef QT_NO_IMAGE_TEXT + if ( data->misc ) { + image.data->misc = new QImageDataMisc; + *image.data->misc = misc(); + } +#endif + return image; + } +} + +/*! + \overload + + Returns a \link shclass.html deep copy\endlink of a sub-area of + the image. + + The returned image is always \a w by \a h pixels in size, and is + copied from position \a x, \a y in this image. In areas beyond + this image pixels are filled with pixel 0. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa bitBlt() Qt::ImageConversionFlags +*/ + +QImage QImage::copy(int x, int y, int w, int h, int conversion_flags) const +{ + int dx = 0; + int dy = 0; + if ( w <= 0 || h <= 0 ) return QImage(); // Nothing to copy + + QImage image( w, h, depth(), numColors(), bitOrder() ); + + if ( x < 0 || y < 0 || x + w > width() || y + h > height() ) { + // bitBlt will not cover entire image - clear it. + // ### should deal with each side separately for efficiency + image.fill(0); + if ( x < 0 ) { + dx = -x; + x = 0; + } + if ( y < 0 ) { + dy = -y; + y = 0; + } + } + + bool has_alpha = hasAlphaBuffer(); + if ( has_alpha ) { + // alpha channel should be only copied, not used by bitBlt(), and + // this is mutable, we will restore the image state before returning + QImage *that = (QImage *) this; + that->setAlphaBuffer( FALSE ); + } + memcpy( image.colorTable(), colorTable(), numColors()*sizeof(QRgb) ); + bitBlt( &image, dx, dy, this, x, y, -1, -1, conversion_flags ); + if ( has_alpha ) { + // restore image state + QImage *that = (QImage *) this; + that->setAlphaBuffer( TRUE ); + } + image.setAlphaBuffer(hasAlphaBuffer()); + image.data->dpmx = dotsPerMeterX(); + image.data->dpmy = dotsPerMeterY(); + image.data->offset = offset(); +#ifndef QT_NO_IMAGE_TEXT + if ( data->misc ) { + image.data->misc = new QImageDataMisc; + *image.data->misc = misc(); + } +#endif + return image; +} + +/*! + \overload QImage QImage::copy(const QRect& r) const + + Returns a \link shclass.html deep copy\endlink of a sub-area of + the image. + + The returned image always has the size of the rectangle \a r. In + areas beyond this image pixels are filled with pixel 0. +*/ + +/*! + \fn bool QImage::isNull() const + + Returns TRUE if it is a null image; otherwise returns FALSE. + + A null image has all parameters set to zero and no allocated data. +*/ + + +/*! + \fn int QImage::width() const + + Returns the width of the image. + + \sa height() size() rect() +*/ + +/*! + \fn int QImage::height() const + + Returns the height of the image. + + \sa width() size() rect() +*/ + +/*! + \fn QSize QImage::size() const + + Returns the size of the image, i.e. its width and height. + + \sa width() height() rect() +*/ + +/*! + \fn QRect QImage::rect() const + + Returns the enclosing rectangle (0, 0, width(), height()) of the + image. + + \sa width() height() size() +*/ + +/*! + \fn int QImage::depth() const + + Returns the depth of the image. + + The image depth is the number of bits used to encode a single + pixel, also called bits per pixel (bpp) or bit planes of an image. + + The supported depths are 1, 8, 16 (Qt/Embedded only) and 32. + + \sa convertDepth() +*/ + +/*! + \fn int QImage::numColors() const + + Returns the size of the color table for the image. + + Notice that numColors() returns 0 for 16-bpp (Qt/Embedded only) + and 32-bpp images because these images do not use color tables, + but instead encode pixel values as RGB triplets. + + \sa setNumColors() colorTable() +*/ + +/*! + \fn QImage::Endian QImage::bitOrder() const + + Returns the bit order for the image. + + If it is a 1-bpp image, this function returns either + QImage::BigEndian or QImage::LittleEndian. + + If it is not a 1-bpp image, this function returns + QImage::IgnoreEndian. + + \sa depth() +*/ + +/*! + \fn uchar **QImage::jumpTable() const + + Returns a pointer to the scanline pointer table. + + This is the beginning of the data block for the image. + + \sa bits() scanLine() +*/ + +/*! + \fn QRgb *QImage::colorTable() const + + Returns a pointer to the color table. + + \sa numColors() +*/ + +/*! + \fn int QImage::numBytes() const + + Returns the number of bytes occupied by the image data. + + \sa bytesPerLine() bits() +*/ + +/*! + \fn int QImage::bytesPerLine() const + + Returns the number of bytes per image scanline. This is equivalent + to numBytes()/height(). + + \sa numBytes() scanLine() +*/ + +/*! + \fn QRgb QImage::color( int i ) const + + Returns the color in the color table at index \a i. The first + color is at index 0. + + A color value is an RGB triplet. Use the \link ::qRed() + qRed()\endlink, \link ::qGreen() qGreen()\endlink and \link + ::qBlue() qBlue()\endlink functions (defined in \c qcolor.h) to + get the color value components. + + \sa setColor() numColors() QColor +*/ + +/*! + \fn void QImage::setColor( int i, QRgb c ) + + Sets a color in the color table at index \a i to \a c. + + A color value is an RGB triplet. Use the \link ::qRgb() + qRgb()\endlink function (defined in \c qcolor.h) to make RGB + triplets. + + \sa color() setNumColors() numColors() +*/ + +/*! + \fn uchar *QImage::scanLine( int i ) const + + Returns a pointer to the pixel data at the scanline with index \a + i. The first scanline is at index 0. + + The scanline data is aligned on a 32-bit boundary. + + \warning If you are accessing 32-bpp image data, cast the returned + pointer to \c{QRgb*} (QRgb has a 32-bit size) and use it to + read/write the pixel value. You cannot use the \c{uchar*} pointer + directly, because the pixel format depends on the byte order on + the underlying platform. Hint: use \link ::qRed() qRed()\endlink, + \link ::qGreen() qGreen()\endlink and \link ::qBlue() + qBlue()\endlink, etc. (qcolor.h) to access the pixels. + + \warning If you are accessing 16-bpp image data, you must handle + endianness yourself. (Qt/Embedded only) + + \sa bytesPerLine() bits() jumpTable() +*/ + +/*! + \fn uchar *QImage::bits() const + + Returns a pointer to the first pixel data. This is equivalent to + scanLine(0). + + \sa numBytes() scanLine() jumpTable() +*/ + + + +void QImage::warningIndexRange( const char *func, int i ) +{ +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::%s: Index %d out of range", func, i ); +#else + Q_UNUSED( func ) + Q_UNUSED( i ) +#endif +} + + +/*! + Resets all image parameters and deallocates the image data. +*/ + +void QImage::reset() +{ + freeBits(); + setNumColors( 0 ); +#ifndef QT_NO_IMAGE_TEXT + delete data->misc; +#endif + reinit(); +} + + +/*! + Fills the entire image with the pixel value \a pixel. + + If the \link depth() depth\endlink of this image is 1, only the + lowest bit is used. If you say fill(0), fill(2), etc., the image + is filled with 0s. If you say fill(1), fill(3), etc., the image is + filled with 1s. If the depth is 8, the lowest 8 bits are used. + + If the depth is 32 and the image has no alpha buffer, the \a pixel + value is written to each pixel in the image. If the image has an + alpha buffer, only the 24 RGB bits are set and the upper 8 bits + (alpha value) are left unchanged. + + Note: QImage::pixel() returns the color of the pixel at the given + coordinates; QColor::pixel() returns the pixel value of the + underlying window system (essentially an index value), so normally + you will want to use QImage::pixel() to use a color from an + existing image or QColor::rgb() to use a specific color. + + \sa invertPixels() depth() hasAlphaBuffer() create() +*/ + +void QImage::fill( uint pixel ) +{ + if ( depth() == 1 || depth() == 8 ) { + if ( depth() == 1 ) { + if ( pixel & 1 ) + pixel = 0xffffffff; + else + pixel = 0; + } else { + uint c = pixel & 0xff; + pixel = c | ((c << 8) & 0xff00) | ((c << 16) & 0xff0000) | + ((c << 24) & 0xff000000); + } + int bpl = bytesPerLine(); + for ( int i=0; i<height(); i++ ) + memset( scanLine(i), pixel, bpl ); +#ifndef QT_NO_IMAGE_16_BIT + } else if ( depth() == 16 ) { + for ( int i=0; i<height(); i++ ) { + //optimize with 32-bit writes, since image is always aligned + uint *p = (uint *)scanLine(i); + uint *end = (uint*)(((ushort*)p) + width()); + uint fill; + ushort *f = (ushort*)&fill; + f[0]=pixel; + f[1]=pixel; + while ( p < end ) + *p++ = fill; + } +#endif // QT_NO_IMAGE_16_BIT +#ifndef QT_NO_IMAGE_TRUECOLOR + } else if ( depth() == 32 ) { + if ( hasAlphaBuffer() ) { + pixel &= 0x00ffffff; + for ( int i=0; i<height(); i++ ) { + uint *p = (uint *)scanLine(i); + uint *end = p + width(); + while ( p < end ) { + *p = (*p & 0xff000000) | pixel; + p++; + } + } + } else { + for ( int i=0; i<height(); i++ ) { + uint *p = (uint *)scanLine(i); + uint *end = p + width(); + while ( p < end ) + *p++ = pixel; + } + } +#endif // QT_NO_IMAGE_TRUECOLOR + } +} + + +/*! + Inverts all pixel values in the image. + + If the depth is 32: if \a invertAlpha is TRUE, the alpha bits are + also inverted, otherwise they are left unchanged. + + If the depth is not 32, the argument \a invertAlpha has no + meaning. + + Note that inverting an 8-bit image means to replace all pixels + using color index \e i with a pixel using color index 255 minus \e + i. Similarly for a 1-bit image. The color table is not changed. + + \sa fill() depth() hasAlphaBuffer() +*/ + +void QImage::invertPixels( bool invertAlpha ) +{ + Q_UINT32 n = numBytes(); + if ( n % 4 ) { + Q_UINT8 *p = (Q_UINT8*)bits(); + Q_UINT8 *end = p + n; + while ( p < end ) + *p++ ^= 0xff; + } else { + Q_UINT32 *p = (Q_UINT32*)bits(); + Q_UINT32 *end = p + n/4; + uint xorbits = invertAlpha && depth() == 32 ? 0x00ffffff : 0xffffffff; + while ( p < end ) + *p++ ^= xorbits; + } +} + + +/*! + Determines the host computer byte order. Returns + QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first). + + \sa systemBitOrder() +*/ + +QImage::Endian QImage::systemByteOrder() +{ + static Endian sbo = IgnoreEndian; + if ( sbo == IgnoreEndian ) { // initialize + int ws; + bool be; + qSysInfo( &ws, &be ); + sbo = be ? BigEndian : LittleEndian; + } + return sbo; +} + + +#if defined(Q_WS_X11) +#include <X11/Xlib.h> // needed for systemBitOrder +#include <X11/Xutil.h> +#include <X11/Xos.h> +#if defined(Q_OS_WIN32) +#undef open +#undef close +#undef read +#undef write +#endif +#endif + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +/*! + Determines the bit order of the display hardware. Returns + QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first). + + \sa systemByteOrder() +*/ + +QImage::Endian QImage::systemBitOrder() +{ +#if defined(Q_WS_X11) + return BitmapBitOrder(qt_xdisplay()) == MSBFirst ? BigEndian :LittleEndian; +#else + return BigEndian; +#endif +} + + +/*! + Resizes the color table to \a numColors colors. + + If the color table is expanded all the extra colors will be set to + black (RGB 0,0,0). + + \sa numColors() color() setColor() colorTable() +*/ + +void QImage::setNumColors( int numColors ) +{ + if ( numColors == data->ncols ) + return; + if ( numColors == 0 ) { // use no color table + if ( data->ctbl ) { + if ( data->ctbl_mine ) + free( data->ctbl ); + else + data->ctbl_mine = TRUE; + data->ctbl = 0; + } + data->ncols = 0; + return; + } + if ( data->ctbl && data->ctbl_mine ) { // already has color table + data->ctbl = (QRgb*)realloc( data->ctbl, numColors*sizeof(QRgb) ); + if ( data->ctbl && numColors > data->ncols ) + memset( (char *)&data->ctbl[data->ncols], 0, + (numColors-data->ncols)*sizeof(QRgb) ); + } else { // create new color table + data->ctbl = (QRgb*)calloc( numColors*sizeof(QRgb), 1 ); + Q_CHECK_PTR(data->ctbl); + data->ctbl_mine = TRUE; + } + data->ncols = data->ctbl == 0 ? 0 : numColors; +} + + +/*! + \fn bool QImage::hasAlphaBuffer() const + + Returns TRUE if alpha buffer mode is enabled; otherwise returns + FALSE. + + \sa setAlphaBuffer() +*/ + +/*! + Enables alpha buffer mode if \a enable is TRUE, otherwise disables + it. The default setting is disabled. + + An 8-bpp image has 8-bit pixels. A pixel is an index into the + \link color() color table\endlink, which contains 32-bit color + values. In a 32-bpp image, the 32-bit pixels are the color values. + + This 32-bit value is encoded as follows: The lower 24 bits are + used for the red, green, and blue components. The upper 8 bits + contain the alpha component. + + The alpha component specifies the transparency of a pixel. 0 means + completely transparent and 255 means opaque. The alpha component + is ignored if you do not enable alpha buffer mode. + + The alpha buffer is used to set a mask when a QImage is translated + to a QPixmap. + + \sa hasAlphaBuffer() createAlphaMask() +*/ + +void QImage::setAlphaBuffer( bool enable ) +{ + data->alpha = enable; +} + + +/*! + Sets the image \a width, \a height, \a depth, its number of colors + (in \a numColors), and bit order. Returns TRUE if successful, or + FALSE if the parameters are incorrect or if memory cannot be + allocated. + + The \a width and \a height is limited to 32767. \a depth must be + 1, 8, or 32. If \a depth is 1, \a bitOrder must be set to + either QImage::LittleEndian or QImage::BigEndian. For other depths + \a bitOrder must be QImage::IgnoreEndian. + + This function allocates a color table and a buffer for the image + data. The image data is not initialized. + + The image buffer is allocated as a single block that consists of a + table of \link scanLine() scanline\endlink pointers (jumpTable()) + and the image data (bits()). + + \sa fill() width() height() depth() numColors() bitOrder() + jumpTable() scanLine() bits() bytesPerLine() numBytes() +*/ + +bool QImage::create( int width, int height, int depth, int numColors, + Endian bitOrder ) +{ + reset(); // reset old data + if ( width <= 0 || height <= 0 || depth <= 0 || numColors < 0 ) + return FALSE; // invalid parameter(s) + if ( depth == 1 && bitOrder == IgnoreEndian ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::create: Bit order is required for 1 bpp images" ); +#endif + return FALSE; + } + if ( depth != 1 ) + bitOrder = IgnoreEndian; + +#if defined(QT_CHECK_RANGE) + if ( depth == 24 ) + qWarning( "QImage::create: 24-bpp images no longer supported, " + "use 32-bpp instead" ); +#endif + switch ( depth ) { + case 1: + case 8: +#ifndef QT_NO_IMAGE_16_BIT + case 16: +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: +#endif + break; + default: // invalid depth + return FALSE; + } + + if ( depth == 32 ) + numColors = 0; + setNumColors( numColors ); + if ( data->ncols != numColors ) // could not alloc color table + return FALSE; + + if ( INT_MAX / uint(depth) < uint(width) ) { // sanity check for potential overflow + setNumColors( 0 ); + return FALSE; + } +// Qt/Embedded doesn't waste memory on unnecessary padding. +#ifdef Q_WS_QWS + const int bpl = (width*depth+7)/8; // bytes per scanline + const int pad = 0; +#else + const int bpl = ((width*depth+31)/32)*4; // bytes per scanline + // #### WWA: shouldn't this be (width*depth+7)/8: + const int pad = bpl - (width*depth)/8; // pad with zeros +#endif + if ( INT_MAX / uint(bpl) < uint(height) + || bpl < 0 + || INT_MAX / sizeof(uchar *) < uint(height) ) { // sanity check for potential overflow + setNumColors( 0 ); + return FALSE; + } + int nbytes = bpl*height; // image size + int ptbl = height*sizeof(uchar*); // pointer table size + int size = nbytes + ptbl; // total size of data block + uchar **p = (uchar **)malloc( size ); // alloc image bits + Q_CHECK_PTR(p); + if ( !p ) { // no memory + setNumColors( 0 ); + return FALSE; + } + data->w = width; + data->h = height; + data->d = depth; + data->nbytes = nbytes; + data->bitordr = bitOrder; + data->bits = p; // set image pointer + //uchar *d = (uchar*)p + ptbl; // setup scanline pointers + uchar *d = (uchar*)(p + height); // setup scanline pointers + while ( height-- ) { + *p++ = d; + if ( pad ) + memset( d+bpl-pad, 0, pad ); + d += bpl; + } + return TRUE; +} + +/*! + \overload bool QImage::create( const QSize&, int depth, int numColors, Endian bitOrder ) +*/ +bool QImage::create( const QSize& size, int depth, int numColors, + QImage::Endian bitOrder ) +{ + return create(size.width(), size.height(), depth, numColors, bitOrder); +} + +/*! + \internal + Initializes the image data structure. +*/ + +void QImage::init() +{ + data = new QImageData; + Q_CHECK_PTR( data ); + reinit(); +} + +void QImage::reinit() +{ + data->w = data->h = data->d = data->ncols = 0; + data->nbytes = 0; + data->ctbl = 0; + data->bits = 0; + data->bitordr = QImage::IgnoreEndian; + data->alpha = FALSE; +#ifndef QT_NO_IMAGE_TEXT + data->misc = 0; +#endif + data->dpmx = 0; + data->dpmy = 0; + data->offset = QPoint(0,0); +} + +/*! + \internal + Deallocates the image data and sets the bits pointer to 0. +*/ + +void QImage::freeBits() +{ + if ( data->bits ) { // dealloc image bits + free( data->bits ); + data->bits = 0; + } +} + + +/***************************************************************************** + Internal routines for converting image depth. + *****************************************************************************/ + +// +// convert_32_to_8: Converts a 32 bits depth (true color) to an 8 bit +// image with a colormap. If the 32 bit image has more than 256 colors, +// we convert the red,green and blue bytes into a single byte encoded +// as 6 shades of each of red, green and blue. +// +// if dithering is needed, only 1 color at most is available for alpha. +// +#ifndef QT_NO_IMAGE_TRUECOLOR +struct QRgbMap { + QRgbMap() : rgb(0xffffffff) { } + bool used() const { return rgb!=0xffffffff; } + uchar pix; + QRgb rgb; +}; + +static bool convert_32_to_8( const QImage *src, QImage *dst, int conversion_flags, QRgb* palette=0, int palette_count=0 ) +{ + register QRgb *p; + uchar *b; + bool do_quant = FALSE; + int y, x; + + if ( !dst->create(src->width(), src->height(), 8, 256) ) + return FALSE; + + const int tablesize = 997; // prime + QRgbMap table[tablesize]; + int pix=0; + QRgb amask = src->hasAlphaBuffer() ? 0xffffffff : 0x00ffffff; + if ( src->hasAlphaBuffer() ) + dst->setAlphaBuffer(TRUE); + + if ( palette ) { + // Preload palette into table. + + p = palette; + // Almost same code as pixel insertion below + while ( palette_count-- > 0 ) { + // Find in table... + int hash = (*p & amask) % tablesize; + for (;;) { + if ( table[hash].used() ) { + if ( table[hash].rgb == (*p & amask) ) { + // Found previous insertion - use it + break; + } else { + // Keep searching... + if (++hash == tablesize) hash = 0; + } + } else { + // Cannot be in table + Q_ASSERT ( pix != 256 ); // too many colors + // Insert into table at this unused position + dst->setColor( pix, (*p & amask) ); + table[hash].pix = pix++; + table[hash].rgb = *p & amask; + break; + } + } + p++; + } + } + + if ( (conversion_flags & Qt::DitherMode_Mask) == Qt::PreferDither ) { + do_quant = TRUE; + } else { + for ( y=0; y<src->height(); y++ ) { // check if <= 256 colors + p = (QRgb *)src->scanLine(y); + b = dst->scanLine(y); + x = src->width(); + while ( x-- ) { + // Find in table... + int hash = (*p & amask) % tablesize; + for (;;) { + if ( table[hash].used() ) { + if ( table[hash].rgb == (*p & amask) ) { + // Found previous insertion - use it + break; + } else { + // Keep searching... + if (++hash == tablesize) hash = 0; + } + } else { + // Cannot be in table + if ( pix == 256 ) { // too many colors + do_quant = TRUE; + // Break right out + x = 0; + y = src->height(); + } else { + // Insert into table at this unused position + dst->setColor( pix, (*p & amask) ); + table[hash].pix = pix++; + table[hash].rgb = (*p & amask); + } + break; + } + } + *b++ = table[hash].pix; // May occur once incorrectly + p++; + } + } + } + int ncols = do_quant ? 256 : pix; + + static uint bm[16][16]; + static int init=0; + if (!init) { + // Build a Bayer Matrix for dithering + + init = 1; + int n, i, j; + + bm[0][0]=0; + + for (n=1; n<16; n*=2) { + for (i=0; i<n; i++) { + for (j=0; j<n; j++) { + bm[i][j]*=4; + bm[i+n][j]=bm[i][j]+2; + bm[i][j+n]=bm[i][j]+3; + bm[i+n][j+n]=bm[i][j]+1; + } + } + } + + for (i=0; i<16; i++) + for (j=0; j<16; j++) + bm[i][j]<<=8; + } + + dst->setNumColors( ncols ); + + if ( do_quant ) { // quantization needed + +#define MAX_R 5 +#define MAX_G 5 +#define MAX_B 5 +#define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b)) + + int rc, gc, bc; + + for ( rc=0; rc<=MAX_R; rc++ ) // build 6x6x6 color cube + for ( gc=0; gc<=MAX_G; gc++ ) + for ( bc=0; bc<=MAX_B; bc++ ) { + dst->setColor( INDEXOF(rc,gc,bc), + (amask&0xff000000) + | qRgb( rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B ) ); + } + + int sw = src->width(); + + int* line1[3]; + int* line2[3]; + int* pv[3]; + if ( ( conversion_flags & Qt::Dither_Mask ) == Qt::DiffuseDither ) { + line1[0] = new int[src->width()]; + line2[0] = new int[src->width()]; + line1[1] = new int[src->width()]; + line2[1] = new int[src->width()]; + line1[2] = new int[src->width()]; + line2[2] = new int[src->width()]; + pv[0] = new int[sw]; + pv[1] = new int[sw]; + pv[2] = new int[sw]; + } + + for ( y=0; y < src->height(); y++ ) { + p = (QRgb *)src->scanLine(y); + b = dst->scanLine(y); + QRgb *end = p + sw; + + // perform quantization + if ( ( conversion_flags & Qt::Dither_Mask ) == Qt::ThresholdDither ) { +#define DITHER(p,m) ((uchar) ((p * (m) + 127) / 255)) + while ( p < end ) { + rc = qRed( *p ); + gc = qGreen( *p ); + bc = qBlue( *p ); + + *b++ = + INDEXOF( + DITHER(rc, MAX_R), + DITHER(gc, MAX_G), + DITHER(bc, MAX_B) + ); + + p++; + } +#undef DITHER + } else if ( ( conversion_flags & Qt::Dither_Mask ) == Qt::OrderedDither ) { +#define DITHER(p,d,m) ((uchar) ((((256 * (m) + (m) + 1)) * (p) + (d)) / 65536 )) + + int x = 0; + while ( p < end ) { + uint d = bm[y&15][x&15]; + + rc = qRed( *p ); + gc = qGreen( *p ); + bc = qBlue( *p ); + + *b++ = + INDEXOF( + DITHER(rc, d, MAX_R), + DITHER(gc, d, MAX_G), + DITHER(bc, d, MAX_B) + ); + + p++; + x++; + } +#undef DITHER + } else { // Diffuse + int endian = (QImage::systemByteOrder() == QImage::BigEndian); + int x; + uchar* q = src->scanLine(y); + uchar* q2 = src->scanLine(y+1 < src->height() ? y + 1 : 0); + for (int chan = 0; chan < 3; chan++) { + b = dst->scanLine(y); + int *l1 = (y&1) ? line2[chan] : line1[chan]; + int *l2 = (y&1) ? line1[chan] : line2[chan]; + if ( y == 0 ) { + for (int i=0; i<sw; i++) + l1[i] = q[i*4+chan+endian]; + } + if ( y+1 < src->height() ) { + for (int i=0; i<sw; i++) + l2[i] = q2[i*4+chan+endian]; + } + // Bi-directional error diffusion + if ( y&1 ) { + for (x=0; x<sw; x++) { + int pix = QMAX(QMIN(5, (l1[x] * 5 + 128)/ 255), 0); + int err = l1[x] - pix * 255 / 5; + pv[chan][x] = pix; + + // Spread the error around... + if ( x+1<sw ) { + l1[x+1] += (err*7)>>4; + l2[x+1] += err>>4; + } + l2[x]+=(err*5)>>4; + if (x>1) + l2[x-1]+=(err*3)>>4; + } + } else { + for (x=sw; x-->0; ) { + int pix = QMAX(QMIN(5, (l1[x] * 5 + 128)/ 255), 0); + int err = l1[x] - pix * 255 / 5; + pv[chan][x] = pix; + + // Spread the error around... + if ( x > 0 ) { + l1[x-1] += (err*7)>>4; + l2[x-1] += err>>4; + } + l2[x]+=(err*5)>>4; + if (x+1 < sw) + l2[x+1]+=(err*3)>>4; + } + } + } + if (endian) { + for (x=0; x<sw; x++) { + *b++ = INDEXOF(pv[0][x],pv[1][x],pv[2][x]); + } + } else { + for (x=0; x<sw; x++) { + *b++ = INDEXOF(pv[2][x],pv[1][x],pv[0][x]); + } + } + } + } + +#ifndef QT_NO_IMAGE_DITHER_TO_1 + if ( src->hasAlphaBuffer() ) { + const int trans = 216; + dst->setColor(trans, 0x00000000); // transparent + QImage mask = src->createAlphaMask(conversion_flags); + uchar* m; + for ( y=0; y < src->height(); y++ ) { + uchar bit = 0x80; + m = mask.scanLine(y); + b = dst->scanLine(y); + int w = src->width(); + for ( x = 0; x<w; x++ ) { + if ( !(*m&bit) ) + b[x] = trans; + if (!(bit >>= 1)) { + bit = 0x80; + while ( x<w-1 && *++m == 0xff ) // skip chunks + x+=8; + } + } + } + } +#endif + + if ( ( conversion_flags & Qt::Dither_Mask ) == Qt::DiffuseDither ) { + delete [] line1[0]; + delete [] line2[0]; + delete [] line1[1]; + delete [] line2[1]; + delete [] line1[2]; + delete [] line2[2]; + delete [] pv[0]; + delete [] pv[1]; + delete [] pv[2]; + } + +#undef MAX_R +#undef MAX_G +#undef MAX_B +#undef INDEXOF + + } + + return TRUE; +} + + +static bool convert_8_to_32( const QImage *src, QImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 32) ) + return FALSE; // create failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; y<dst->height(); y++ ) { // for each scan line... + register uint *p = (uint *)dst->scanLine(y); + uchar *b = src->scanLine(y); + uint *end = p + dst->width(); + while ( p < end ) + *p++ = src->color(*b++); + } + return TRUE; +} + + +static bool convert_1_to_32( const QImage *src, QImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 32) ) + return FALSE; // could not create + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; y<dst->height(); y++ ) { // for each scan line... + register uint *p = (uint *)dst->scanLine(y); + uchar *b = src->scanLine(y); + int x; + if ( src->bitOrder() == QImage::BigEndian ) { + for ( x=0; x<dst->width(); x++ ) { + *p++ = src->color( (*b >> (7 - (x & 7))) & 1 ); + if ( (x & 7) == 7 ) + b++; + } + } else { + for ( x=0; x<dst->width(); x++ ) { + *p++ = src->color( (*b >> (x & 7)) & 1 ); + if ( (x & 7) == 7 ) + b++; + } + } + } + return TRUE; +} +#endif // QT_NO_IMAGE_TRUECOLOR + +static bool convert_1_to_8( const QImage *src, QImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 8, 2) ) + return FALSE; // something failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + if (src->numColors() >= 2) { + dst->setColor( 0, src->color(0) ); // copy color table + dst->setColor( 1, src->color(1) ); + } else { + // Unlikely, but they do exist + if (src->numColors() >= 1) + dst->setColor( 0, src->color(0) ); + else + dst->setColor( 0, 0xffffffff ); + dst->setColor( 1, 0xff000000 ); + } + for ( int y=0; y<dst->height(); y++ ) { // for each scan line... + register uchar *p = dst->scanLine(y); + uchar *b = src->scanLine(y); + int x; + if ( src->bitOrder() == QImage::BigEndian ) { + for ( x=0; x<dst->width(); x++ ) { + *p++ = (*b >> (7 - (x & 7))) & 1; + if ( (x & 7) == 7 ) + b++; + } + } else { + for ( x=0; x<dst->width(); x++ ) { + *p++ = (*b >> (x & 7)) & 1; + if ( (x & 7) == 7 ) + b++; + } + } + } + return TRUE; +} + +#ifndef QT_NO_IMAGE_DITHER_TO_1 +// +// dither_to_1: Uses selected dithering algorithm. +// + +static bool dither_to_1( const QImage *src, QImage *dst, + int conversion_flags, bool fromalpha ) +{ + if ( !dst->create(src->width(), src->height(), 1, 2, QImage::BigEndian) ) + return FALSE; // something failed + + enum { Threshold, Ordered, Diffuse } dithermode; + + if ( fromalpha ) { + if ( ( conversion_flags & Qt::AlphaDither_Mask ) == Qt::DiffuseAlphaDither ) + dithermode = Diffuse; + else if ( ( conversion_flags & Qt::AlphaDither_Mask ) == Qt::OrderedAlphaDither ) + dithermode = Ordered; + else + dithermode = Threshold; + } else { + if ( ( conversion_flags & Qt::Dither_Mask ) == Qt::ThresholdDither ) + dithermode = Threshold; + else if ( ( conversion_flags & Qt::Dither_Mask ) == Qt::OrderedDither ) + dithermode = Ordered; + else + dithermode = Diffuse; + } + + dst->setColor( 0, qRgb(255, 255, 255) ); + dst->setColor( 1, qRgb( 0, 0, 0) ); + int w = src->width(); + int h = src->height(); + int d = src->depth(); + uchar gray[256]; // gray map for 8 bit images + bool use_gray = d == 8; + if ( use_gray ) { // make gray map + if ( fromalpha ) { + // Alpha 0x00 -> 0 pixels (white) + // Alpha 0xFF -> 1 pixels (black) + for ( int i=0; i<src->numColors(); i++ ) + gray[i] = (255 - (src->color(i) >> 24)); + } else { + // Pixel 0x00 -> 1 pixels (black) + // Pixel 0xFF -> 0 pixels (white) + for ( int i=0; i<src->numColors(); i++ ) + gray[i] = qGray( src->color(i) & 0x00ffffff ); + } + } + + switch ( dithermode ) { + case Diffuse: { + int *line1 = new int[w]; + int *line2 = new int[w]; + int bmwidth = (w+7)/8; + if ( !(line1 && line2) ) + return FALSE; + register uchar *p; + uchar *end; + int *b1, *b2; + int wbytes = w * (d/8); + p = src->bits(); + end = p + wbytes; + b2 = line2; + if ( use_gray ) { // 8 bit image + while ( p < end ) + *b2++ = gray[*p++]; +#ifndef QT_NO_IMAGE_TRUECOLOR + } else { // 32 bit image + if ( fromalpha ) { + while ( p < end ) { + *b2++ = 255 - (*(uint*)p >> 24); + p += 4; + } + } else { + while ( p < end ) { + *b2++ = qGray(*(uint*)p); + p += 4; + } + } +#endif + } + int x, y; + for ( y=0; y<h; y++ ) { // for each scan line... + int *tmp = line1; line1 = line2; line2 = tmp; + bool not_last_line = y < h - 1; + if ( not_last_line ) { // calc. grayvals for next line + p = src->scanLine(y+1); + end = p + wbytes; + b2 = line2; + if ( use_gray ) { // 8 bit image + while ( p < end ) + *b2++ = gray[*p++]; +#ifndef QT_NO_IMAGE_TRUECOLOR + } else { // 24 bit image + if ( fromalpha ) { + while ( p < end ) { + *b2++ = 255 - (*(uint*)p >> 24); + p += 4; + } + } else { + while ( p < end ) { + *b2++ = qGray(*(uint*)p); + p += 4; + } + } +#endif + } + } + + int err; + p = dst->scanLine( y ); + memset( p, 0, bmwidth ); + b1 = line1; + b2 = line2; + int bit = 7; + for ( x=1; x<=w; x++ ) { + if ( *b1 < 128 ) { // black pixel + err = *b1++; + *p |= 1 << bit; + } else { // white pixel + err = *b1++ - 255; + } + if ( bit == 0 ) { + p++; + bit = 7; + } else { + bit--; + } + if ( x < w ) + *b1 += (err*7)>>4; // spread error to right pixel + if ( not_last_line ) { + b2[0] += (err*5)>>4; // pixel below + if ( x > 1 ) + b2[-1] += (err*3)>>4; // pixel below left + if ( x < w ) + b2[1] += err>>4; // pixel below right + } + b2++; + } + } + delete [] line1; + delete [] line2; + } break; + case Ordered: { + static uint bm[16][16]; + static int init=0; + if (!init) { + // Build a Bayer Matrix for dithering + + init = 1; + int n, i, j; + + bm[0][0]=0; + + for (n=1; n<16; n*=2) { + for (i=0; i<n; i++) { + for (j=0; j<n; j++) { + bm[i][j]*=4; + bm[i+n][j]=bm[i][j]+2; + bm[i][j+n]=bm[i][j]+3; + bm[i+n][j+n]=bm[i][j]+1; + } + } + } + + // Force black to black + bm[0][0]=1; + } + + dst->fill( 0 ); + uchar** mline = dst->jumpTable(); +#ifndef QT_NO_IMAGE_TRUECOLOR + if ( d == 32 ) { + uint** line = (uint**)src->jumpTable(); + for ( int i=0; i<h; i++ ) { + uint *p = line[i]; + uint *end = p + w; + uchar *m = mline[i]; + int bit = 7; + int j = 0; + if ( fromalpha ) { + while ( p < end ) { + if ( (*p++ >> 24) >= bm[j++&15][i&15] ) + *m |= 1 << bit; + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } else { + while ( p < end ) { + if ( (uint)qGray(*p++) < bm[j++&15][i&15] ) + *m |= 1 << bit; + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } + } + } else +#endif // QT_NO_IMAGE_TRUECOLOR + /* ( d == 8 ) */ { + uchar** line = src->jumpTable(); + for ( int i=0; i<h; i++ ) { + uchar *p = line[i]; + uchar *end = p + w; + uchar *m = mline[i]; + int bit = 7; + int j = 0; + while ( p < end ) { + if ( (uint)gray[*p++] < bm[j++&15][i&15] ) + *m |= 1 << bit; + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } + } + } break; + default: { // Threshold: + dst->fill( 0 ); + uchar** mline = dst->jumpTable(); +#ifndef QT_NO_IMAGE_TRUECOLOR + if ( d == 32 ) { + uint** line = (uint**)src->jumpTable(); + for ( int i=0; i<h; i++ ) { + uint *p = line[i]; + uint *end = p + w; + uchar *m = mline[i]; + int bit = 7; + if ( fromalpha ) { + while ( p < end ) { + if ( (*p++ >> 24) >= 128 ) + *m |= 1 << bit; // Set mask "on" + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } else { + while ( p < end ) { + if ( qGray(*p++) < 128 ) + *m |= 1 << bit; // Set pixel "black" + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } + } + } else +#endif //QT_NO_IMAGE_TRUECOLOR + if ( d == 8 ) { + uchar** line = src->jumpTable(); + for ( int i=0; i<h; i++ ) { + uchar *p = line[i]; + uchar *end = p + w; + uchar *m = mline[i]; + int bit = 7; + while ( p < end ) { + if ( gray[*p++] < 128 ) + *m |= 1 << bit; // Set mask "on"/ pixel "black" + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } + } + } + } + return TRUE; +} +#endif + +#ifndef QT_NO_IMAGE_16_BIT +//###### Endianness issues! +static inline bool is16BitGray( ushort c ) +{ + int r=(c & 0xf800) >> 11; + int g=(c & 0x07e0) >> 6; //green/2 + int b=(c & 0x001f); + return r == g && g == b; +} + + +static bool convert_16_to_32( const QImage *src, QImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 32) ) + return FALSE; // create failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; y<dst->height(); y++ ) { // for each scan line... + register uint *p = (uint *)dst->scanLine(y); + ushort *s = (ushort*)src->scanLine(y); + uint *end = p + dst->width(); + while ( p < end ) + *p++ = qt_conv16ToRgb( *s++ ); + } + return TRUE; +} + + +static bool convert_32_to_16( const QImage *src, QImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 16) ) + return FALSE; // create failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; y<dst->height(); y++ ) { // for each scan line... + register ushort *p = (ushort *)dst->scanLine(y); + uint *s = (uint*)src->scanLine(y); + ushort *end = p + dst->width(); + while ( p < end ) + *p++ = qt_convRgbTo16( *s++ ); + } + return TRUE; +} + + +#endif + +/*! + Converts the depth (bpp) of the image to \a depth and returns the + converted image. The original image is not changed. + + The \a depth argument must be 1, 8, 16 (Qt/Embedded only) or 32. + + Returns \c *this if \a depth is equal to the image depth, or a + \link isNull() null\endlink image if this image cannot be + converted. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa Qt::ImageConversionFlags depth() isNull() +*/ + +QImage QImage::convertDepth( int depth, int conversion_flags ) const +{ + QImage image; + if ( data->d == depth ) + image = *this; // no conversion +#ifndef QT_NO_IMAGE_DITHER_TO_1 + else if ( (data->d == 8 || data->d == 32) && depth == 1 ) // dither + dither_to_1( this, &image, conversion_flags, FALSE ); +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + else if ( data->d == 32 && depth == 8 ) // 32 -> 8 + convert_32_to_8( this, &image, conversion_flags ); + else if ( data->d == 8 && depth == 32 ) // 8 -> 32 + convert_8_to_32( this, &image ); +#endif + else if ( data->d == 1 && depth == 8 ) // 1 -> 8 + convert_1_to_8( this, &image ); +#ifndef QT_NO_IMAGE_TRUECOLOR + else if ( data->d == 1 && depth == 32 ) // 1 -> 32 + convert_1_to_32( this, &image ); +#endif +#ifndef QT_NO_IMAGE_16_BIT + else if ( data->d == 16 && depth != 16 ) { + QImage tmp; + convert_16_to_32( this, &tmp ); + image = tmp.convertDepth( depth, conversion_flags ); + } else if ( data->d != 16 && depth == 16 ) { + QImage tmp = convertDepth( 32, conversion_flags ); + convert_32_to_16( &tmp, &image ); + } +#endif + else { +#if defined(QT_CHECK_RANGE) + if ( isNull() ) + qWarning( "QImage::convertDepth: Image is a null image" ); + else + qWarning( "QImage::convertDepth: Depth %d not supported", depth ); +#endif + } + return image; +} + +/*! + \overload +*/ + +QImage QImage::convertDepth( int depth ) const +{ + return convertDepth( depth, 0 ); +} + +/*! + Returns TRUE if ( \a x, \a y ) is a valid coordinate in the image; + otherwise returns FALSE. + + \sa width() height() pixelIndex() +*/ + +bool QImage::valid( int x, int y ) const +{ + return x >= 0 && x < width() + && y >= 0 && y < height(); +} + +/*! + Returns the pixel index at the given coordinates. + + If (\a x, \a y) is not \link valid() valid\endlink, or if the + image is not a paletted image (depth() \> 8), the results are + undefined. + + \sa valid() depth() +*/ + +int QImage::pixelIndex( int x, int y ) const +{ +#if defined(QT_CHECK_RANGE) + if ( x < 0 || x >= width() ) { + qWarning( "QImage::pixel: x=%d out of range", x ); + return -12345; + } +#endif + uchar * s = scanLine( y ); + switch( depth() ) { + case 1: + if ( bitOrder() == QImage::LittleEndian ) + return (*(s + (x >> 3)) >> (x & 7)) & 1; + else + return (*(s + (x >> 3)) >> (7- (x & 7))) & 1; + case 8: + return (int)s[x]; +#ifndef QT_NO_IMAGE_TRUECOLOR +#ifndef QT_NO_IMAGE_16_BIT + case 16: +#endif + case 32: +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::pixelIndex: Not applicable for %d-bpp images " + "(no palette)", depth() ); +#endif + return 0; +#endif //QT_NO_IMAGE_TRUECOLOR + } + return 0; +} + + +/*! + Returns the color of the pixel at the coordinates (\a x, \a y). + + If (\a x, \a y) is not \link valid() on the image\endlink, the + results are undefined. + + \sa setPixel() qRed() qGreen() qBlue() valid() +*/ + +QRgb QImage::pixel( int x, int y ) const +{ +#if defined(QT_CHECK_RANGE) + if ( x < 0 || x >= width() ) { + qWarning( "QImage::pixel: x=%d out of range", x ); + return 12345; + } +#endif + uchar * s = scanLine( y ); + switch( depth() ) { + case 1: + if ( bitOrder() == QImage::LittleEndian ) + return color( (*(s + (x >> 3)) >> (x & 7)) & 1 ); + else + return color( (*(s + (x >> 3)) >> (7- (x & 7))) & 1 ); + case 8: + return color( (int)s[x] ); +#ifndef QT_NO_IMAGE_16_BIT + case 16: + return qt_conv16ToRgb(((ushort*)s)[x]); +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: + return ((QRgb*)s)[x]; +#endif + default: + return 100367; + } +} + + +/*! + Sets the pixel index or color at the coordinates (\a x, \a y) to + \a index_or_rgb. + + If (\a x, \a y) is not \link valid() valid\endlink, the result is + undefined. + + If the image is a paletted image (depth() \<= 8) and \a + index_or_rgb \>= numColors(), the result is undefined. + + \sa pixelIndex() pixel() qRgb() qRgba() valid() +*/ + +void QImage::setPixel( int x, int y, uint index_or_rgb ) +{ + if ( x < 0 || x >= width() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::setPixel: x=%d out of range", x ); +#endif + return; + } + if ( depth() == 1 ) { + uchar * s = scanLine( y ); + if ( index_or_rgb > 1) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::setPixel: index=%d out of range", + index_or_rgb ); +#endif + } else if ( bitOrder() == QImage::LittleEndian ) { + if (index_or_rgb==0) + *(s + (x >> 3)) &= ~(1 << (x & 7)); + else + *(s + (x >> 3)) |= (1 << (x & 7)); + } else { + if (index_or_rgb==0) + *(s + (x >> 3)) &= ~(1 << (7-(x & 7))); + else + *(s + (x >> 3)) |= (1 << (7-(x & 7))); + } + } else if ( depth() == 8 ) { + if (index_or_rgb > (uint)numColors()) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::setPixel: index=%d out of range", + index_or_rgb ); +#endif + return; + } + uchar * s = scanLine( y ); + s[x] = index_or_rgb; +#ifndef QT_NO_IMAGE_16_BIT + } else if ( depth() == 16 ) { + ushort * s = (ushort*)scanLine( y ); + s[x] = qt_convRgbTo16(index_or_rgb); +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + } else if ( depth() == 32 ) { + QRgb * s = (QRgb*)scanLine( y ); + s[x] = index_or_rgb; +#endif + } +} + + +/*! + Converts the bit order of the image to \a bitOrder and returns the + converted image. The original image is not changed. + + Returns \c *this if the \a bitOrder is equal to the image bit + order, or a \link isNull() null\endlink image if this image cannot + be converted. + + \sa bitOrder() systemBitOrder() isNull() +*/ + +QImage QImage::convertBitOrder( Endian bitOrder ) const +{ + if ( isNull() || data->d != 1 || // invalid argument(s) + !(bitOrder == BigEndian || bitOrder == LittleEndian) ) { + QImage nullImage; + return nullImage; + } + if ( data->bitordr == bitOrder ) // nothing to do + return copy(); + + QImage image( data->w, data->h, 1, data->ncols, bitOrder ); + + int bpl = (width() + 7) / 8; + for ( int y = 0; y < data->h; y++ ) { + register uchar *p = jumpTable()[y]; + uchar *end = p + bpl; + uchar *b = image.jumpTable()[y]; + while ( p < end ) + *b++ = bitflip[*p++]; + } + memcpy( image.colorTable(), colorTable(), numColors()*sizeof(QRgb) ); + return image; +} + +// ### Candidate (renamed) for qcolor.h +static +bool isGray(QRgb c) +{ + return qRed(c) == qGreen(c) + && qRed(c) == qBlue(c); +} + +/*! + Returns TRUE if all the colors in the image are shades of gray + (i.e. their red, green and blue components are equal); otherwise + returns FALSE. + + This function is slow for large 16-bit (Qt/Embedded only) and 32-bit images. + + \sa isGrayscale() +*/ +bool QImage::allGray() const +{ +#ifndef QT_NO_IMAGE_TRUECOLOR + if (depth()==32) { + int p = width()*height(); + QRgb* b = (QRgb*)bits(); + while (p--) + if (!isGray(*b++)) + return FALSE; +#ifndef QT_NO_IMAGE_16_BIT + } else if (depth()==16) { + int p = width()*height(); + ushort* b = (ushort*)bits(); + while (p--) + if (!is16BitGray(*b++)) + return FALSE; +#endif + } else +#endif //QT_NO_IMAGE_TRUECOLOR + { + if (!data->ctbl) return TRUE; + for (int i=0; i<numColors(); i++) + if (!isGray(data->ctbl[i])) + return FALSE; + } + return TRUE; +} + +/*! + For 16-bit (Qt/Embedded only) and 32-bit images, this function is + equivalent to allGray(). + + For 8-bpp images, this function returns TRUE if color(i) is + QRgb(i,i,i) for all indices of the color table; otherwise returns + FALSE. + + \sa allGray() depth() +*/ +bool QImage::isGrayscale() const +{ + switch (depth()) { +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: +#ifndef QT_NO_IMAGE_16_BIT + case 16: +#endif + return allGray(); +#endif //QT_NO_IMAGE_TRUECOLOR + case 8: { + for (int i=0; i<numColors(); i++) + if (data->ctbl[i] != qRgb(i,i,i)) + return FALSE; + return TRUE; + } + } + return FALSE; +} + +#ifndef QT_NO_IMAGE_SMOOTHSCALE +static +void pnmscale(const QImage& src, QImage& dst) +{ + QRgb* xelrow = 0; + QRgb* tempxelrow = 0; + register QRgb* xP; + register QRgb* nxP; + int rows, cols, rowsread, newrows, newcols; + register int row, col, needtoreadrow; + const uchar maxval = 255; + double xscale, yscale; + long sxscale, syscale; + register long fracrowtofill, fracrowleft; + long* as; + long* rs; + long* gs; + long* bs; + int rowswritten = 0; + + cols = src.width(); + rows = src.height(); + newcols = dst.width(); + newrows = dst.height(); + + long SCALE; + long HALFSCALE; + + if (cols > 4096) + { + SCALE = 4096; + HALFSCALE = 2048; + } + else + { + int fac = 4096; + + while (cols * fac > 4096) + { + fac /= 2; + } + + SCALE = fac * cols; + HALFSCALE = fac * cols / 2; + } + + xscale = (double) newcols / (double) cols; + yscale = (double) newrows / (double) rows; + + sxscale = (long)(xscale * SCALE); + syscale = (long)(yscale * SCALE); + + if ( newrows != rows ) /* shortcut Y scaling if possible */ + tempxelrow = new QRgb[cols]; + + if ( src.hasAlphaBuffer() ) { + dst.setAlphaBuffer(TRUE); + as = new long[cols]; + for ( col = 0; col < cols; ++col ) + as[col] = HALFSCALE; + } else { + as = 0; + } + rs = new long[cols]; + gs = new long[cols]; + bs = new long[cols]; + rowsread = 0; + fracrowleft = syscale; + needtoreadrow = 1; + for ( col = 0; col < cols; ++col ) + rs[col] = gs[col] = bs[col] = HALFSCALE; + fracrowtofill = SCALE; + + for ( row = 0; row < newrows; ++row ) { + /* First scale Y from xelrow into tempxelrow. */ + if ( newrows == rows ) { + /* shortcut Y scaling if possible */ + tempxelrow = xelrow = (QRgb*)src.scanLine(rowsread++); + } else { + while ( fracrowleft < fracrowtofill ) { + if ( needtoreadrow && rowsread < rows ) + xelrow = (QRgb*)src.scanLine(rowsread++); + for ( col = 0, xP = xelrow; col < cols; ++col, ++xP ) { + if (as) { + as[col] += fracrowleft * qAlpha( *xP ); + rs[col] += fracrowleft * qRed( *xP ) * qAlpha( *xP ) / 255; + gs[col] += fracrowleft * qGreen( *xP ) * qAlpha( *xP ) / 255; + bs[col] += fracrowleft * qBlue( *xP ) * qAlpha( *xP ) / 255; + } else { + rs[col] += fracrowleft * qRed( *xP ); + gs[col] += fracrowleft * qGreen( *xP ); + bs[col] += fracrowleft * qBlue( *xP ); + } + } + fracrowtofill -= fracrowleft; + fracrowleft = syscale; + needtoreadrow = 1; + } + /* Now fracrowleft is >= fracrowtofill, so we can produce a row. */ + if ( needtoreadrow && rowsread < rows ) { + xelrow = (QRgb*)src.scanLine(rowsread++); + needtoreadrow = 0; + } + register long a=0; + for ( col = 0, xP = xelrow, nxP = tempxelrow; + col < cols; ++col, ++xP, ++nxP ) + { + register long r, g, b; + + if ( as ) { + r = rs[col] + fracrowtofill * qRed( *xP ) * qAlpha( *xP ) / 255; + g = gs[col] + fracrowtofill * qGreen( *xP ) * qAlpha( *xP ) / 255; + b = bs[col] + fracrowtofill * qBlue( *xP ) * qAlpha( *xP ) / 255; + a = as[col] + fracrowtofill * qAlpha( *xP ); + if ( a ) { + r = r * 255 / a * SCALE; + g = g * 255 / a * SCALE; + b = b * 255 / a * SCALE; + } + } else { + r = rs[col] + fracrowtofill * qRed( *xP ); + g = gs[col] + fracrowtofill * qGreen( *xP ); + b = bs[col] + fracrowtofill * qBlue( *xP ); + } + r /= SCALE; + if ( r > maxval ) r = maxval; + g /= SCALE; + if ( g > maxval ) g = maxval; + b /= SCALE; + if ( b > maxval ) b = maxval; + if ( as ) { + a /= SCALE; + if ( a > maxval ) a = maxval; + *nxP = qRgba( (int)r, (int)g, (int)b, (int)a ); + as[col] = HALFSCALE; + } else { + *nxP = qRgb( (int)r, (int)g, (int)b ); + } + rs[col] = gs[col] = bs[col] = HALFSCALE; + } + fracrowleft -= fracrowtofill; + if ( fracrowleft == 0 ) { + fracrowleft = syscale; + needtoreadrow = 1; + } + fracrowtofill = SCALE; + } + + /* Now scale X from tempxelrow into dst and write it out. */ + if ( newcols == cols ) { + /* shortcut X scaling if possible */ + memcpy(dst.scanLine(rowswritten++), tempxelrow, newcols*4); + } else { + register long a, r, g, b; + register long fraccoltofill, fraccolleft = 0; + register int needcol; + + nxP = (QRgb*)dst.scanLine(rowswritten++); + fraccoltofill = SCALE; + a = r = g = b = HALFSCALE; + needcol = 0; + for ( col = 0, xP = tempxelrow; col < cols; ++col, ++xP ) { + fraccolleft = sxscale; + while ( fraccolleft >= fraccoltofill ) { + if ( needcol ) { + ++nxP; + a = r = g = b = HALFSCALE; + } + if ( as ) { + r += fraccoltofill * qRed( *xP ) * qAlpha( *xP ) / 255; + g += fraccoltofill * qGreen( *xP ) * qAlpha( *xP ) / 255; + b += fraccoltofill * qBlue( *xP ) * qAlpha( *xP ) / 255; + a += fraccoltofill * qAlpha( *xP ); + if ( a ) { + r = r * 255 / a * SCALE; + g = g * 255 / a * SCALE; + b = b * 255 / a * SCALE; + } + } else { + r += fraccoltofill * qRed( *xP ); + g += fraccoltofill * qGreen( *xP ); + b += fraccoltofill * qBlue( *xP ); + } + r /= SCALE; + if ( r > maxval ) r = maxval; + g /= SCALE; + if ( g > maxval ) g = maxval; + b /= SCALE; + if ( b > maxval ) b = maxval; + if (as) { + a /= SCALE; + if ( a > maxval ) a = maxval; + *nxP = qRgba( (int)r, (int)g, (int)b, (int)a ); + } else { + *nxP = qRgb( (int)r, (int)g, (int)b ); + } + fraccolleft -= fraccoltofill; + fraccoltofill = SCALE; + needcol = 1; + } + if ( fraccolleft > 0 ) { + if ( needcol ) { + ++nxP; + a = r = g = b = HALFSCALE; + needcol = 0; + } + if (as) { + a += fraccolleft * qAlpha( *xP ); + r += fraccolleft * qRed( *xP ) * qAlpha( *xP ) / 255; + g += fraccolleft * qGreen( *xP ) * qAlpha( *xP ) / 255; + b += fraccolleft * qBlue( *xP ) * qAlpha( *xP ) / 255; + } else { + r += fraccolleft * qRed( *xP ); + g += fraccolleft * qGreen( *xP ); + b += fraccolleft * qBlue( *xP ); + } + fraccoltofill -= fraccolleft; + } + } + if ( fraccoltofill > 0 ) { + --xP; + if (as) { + a += fraccolleft * qAlpha( *xP ); + r += fraccoltofill * qRed( *xP ) * qAlpha( *xP ) / 255; + g += fraccoltofill * qGreen( *xP ) * qAlpha( *xP ) / 255; + b += fraccoltofill * qBlue( *xP ) * qAlpha( *xP ) / 255; + if ( a ) { + r = r * 255 / a * SCALE; + g = g * 255 / a * SCALE; + b = b * 255 / a * SCALE; + } + } else { + r += fraccoltofill * qRed( *xP ); + g += fraccoltofill * qGreen( *xP ); + b += fraccoltofill * qBlue( *xP ); + } + } + if ( ! needcol ) { + r /= SCALE; + if ( r > maxval ) r = maxval; + g /= SCALE; + if ( g > maxval ) g = maxval; + b /= SCALE; + if ( b > maxval ) b = maxval; + if (as) { + a /= SCALE; + if ( a > maxval ) a = maxval; + *nxP = qRgba( (int)r, (int)g, (int)b, (int)a ); + } else { + *nxP = qRgb( (int)r, (int)g, (int)b ); + } + } + } + } + + if ( newrows != rows && tempxelrow )// Robust, tempxelrow might be 0 1 day + delete [] tempxelrow; + if ( as ) // Avoid purify complaint + delete [] as; + if ( rs ) // Robust, rs might be 0 one day + delete [] rs; + if ( gs ) // Robust, gs might be 0 one day + delete [] gs; + if ( bs ) // Robust, bs might be 0 one day + delete [] bs; +} +#endif + +/*! + \enum QImage::ScaleMode + + The functions scale() and smoothScale() use different modes for + scaling the image. The purpose of these modes is to retain the + ratio of the image if this is required. + + \img scaling.png + + \value ScaleFree The image is scaled freely: the resulting image + fits exactly into the specified size; the ratio will not + necessarily be preserved. + \value ScaleMin The ratio of the image is preserved and the + resulting image is guaranteed to fit into the specified size + (it is as large as possible within these constraints) - the + image might be smaller than the requested size. + \value ScaleMax The ratio of the image is preserved and the + resulting image fills the whole specified rectangle (it is as + small as possible within these constraints) - the image might + be larger than the requested size. +*/ + +#ifndef QT_NO_IMAGE_SMOOTHSCALE +/*! + Returns a smoothly scaled copy of the image. The returned image + has a size of width \a w by height \a h pixels if \a mode is \c + ScaleFree. The modes \c ScaleMin and \c ScaleMax may be used to + preserve the ratio of the image: if \a mode is \c ScaleMin, the + returned image is guaranteed to fit into the rectangle specified + by \a w and \a h (it is as large as possible within the + constraints); if \a mode is \c ScaleMax, the returned image fits + at least into the specified rectangle (it is a small as possible + within the constraints). + + For 32-bpp images and 1-bpp/8-bpp color images the result will be + 32-bpp, whereas \link allGray() all-gray \endlink images + (including black-and-white 1-bpp) will produce 8-bit \link + isGrayscale() grayscale \endlink images with the palette spanning + 256 grays from black to white. + + This function uses code based on pnmscale.c by Jef Poskanzer. + + pnmscale.c - read a portable anymap and scale it + + \legalese + + Copyright (C) 1989, 1991 by Jef Poskanzer. + + Permission to use, copy, modify, and distribute this software and + its documentation for any purpose and without fee is hereby + granted, provided that the above copyright notice appear in all + copies and that both that copyright notice and this permission + notice appear in supporting documentation. This software is + provided "as is" without express or implied warranty. + + \sa scale() mirror() +*/ +QImage QImage::smoothScale( int w, int h, ScaleMode mode ) const +{ + return smoothScale( QSize( w, h ), mode ); +} +#endif + +#ifndef QT_NO_IMAGE_SMOOTHSCALE +/*! + \overload + + The requested size of the image is \a s. +*/ +QImage QImage::smoothScale( const QSize& s, ScaleMode mode ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::smoothScale: Image is a null image" ); +#endif + return copy(); + } + + QSize newSize = size(); + newSize.scale( s, (QSize::ScaleMode)mode ); // ### remove cast in Qt 4.0 + if ( newSize == size() ) + return copy(); + + if ( depth() == 32 ) { + QImage img( newSize, 32 ); + // 32-bpp to 32-bpp + pnmscale( *this, img ); + return img; + } else if ( depth() != 16 && allGray() && !hasAlphaBuffer() ) { + // Inefficient + return convertDepth(32).smoothScale(newSize, mode).convertDepth(8); + } else { + // Inefficient + return convertDepth(32).smoothScale(newSize, mode); + } +} +#endif + +/*! + Returns a copy of the image scaled to a rectangle of width \a w + and height \a h according to the ScaleMode \a mode. + + \list + \i If \a mode is \c ScaleFree, the image is scaled to (\a w, + \a h). + \i If \a mode is \c ScaleMin, the image is scaled to a rectangle + as large as possible inside (\a w, \a h), preserving the aspect + ratio. + \i If \a mode is \c ScaleMax, the image is scaled to a rectangle + as small as possible outside (\a w, \a h), preserving the aspect + ratio. + \endlist + + If either the width \a w or the height \a h is 0 or negative, this + function returns a \link isNull() null\endlink image. + + This function uses a simple, fast algorithm. If you need better + quality, use smoothScale() instead. + + \sa scaleWidth() scaleHeight() smoothScale() xForm() +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +QImage QImage::scale( int w, int h, ScaleMode mode ) const +{ + return scale( QSize( w, h ), mode ); +} +#endif + +/*! + \overload + + The requested size of the image is \a s. +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +QImage QImage::scale( const QSize& s, ScaleMode mode ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::scale: Image is a null image" ); +#endif + return copy(); + } + if ( s.isEmpty() ) + return QImage(); + + QSize newSize = size(); + newSize.scale( s, (QSize::ScaleMode)mode ); // ### remove cast in Qt 4.0 + if ( newSize == size() ) + return copy(); + + QImage img; + QWMatrix wm; + wm.scale( (double)newSize.width() / width(), (double)newSize.height() / height() ); + img = xForm( wm ); + // ### I should test and resize the image if it has not the right size +// if ( img.width() != newSize.width() || img.height() != newSize.height() ) +// img.resize( newSize.width(), newSize.height() ); + return img; +} +#endif + +/*! + Returns a scaled copy of the image. The returned image has a width + of \a w pixels. This function automatically calculates the height + of the image so that the ratio of the image is preserved. + + If \a w is 0 or negative a \link isNull() null\endlink image is + returned. + + \sa scale() scaleHeight() smoothScale() xForm() +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +QImage QImage::scaleWidth( int w ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::scaleWidth: Image is a null image" ); +#endif + return copy(); + } + if ( w <= 0 ) + return QImage(); + + QWMatrix wm; + double factor = (double) w / width(); + wm.scale( factor, factor ); + return xForm( wm ); +} +#endif + +/*! + Returns a scaled copy of the image. The returned image has a + height of \a h pixels. This function automatically calculates the + width of the image so that the ratio of the image is preserved. + + If \a h is 0 or negative a \link isNull() null\endlink image is + returned. + + \sa scale() scaleWidth() smoothScale() xForm() +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +QImage QImage::scaleHeight( int h ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage::scaleHeight: Image is a null image" ); +#endif + return copy(); + } + if ( h <= 0 ) + return QImage(); + + QWMatrix wm; + double factor = (double) h / height(); + wm.scale( factor, factor ); + return xForm( wm ); +} +#endif + + +/*! + Returns a copy of the image that is transformed using the + transformation matrix, \a matrix. + + The transformation \a matrix is internally adjusted to compensate + for unwanted translation, i.e. xForm() returns the smallest image + that contains all the transformed points of the original image. + + \sa scale() QPixmap::xForm() QPixmap::trueMatrix() QWMatrix +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +QImage QImage::xForm( const QWMatrix &matrix ) const +{ + // This function uses the same algorithm as (and steals quite some + // code from) QPixmap::xForm(). + + if ( isNull() ) + return copy(); + + if ( depth() == 16 ) { + // inefficient + return convertDepth( 32 ).xForm( matrix ); + } + + // source image data + int ws = width(); + int hs = height(); + int sbpl = bytesPerLine(); + uchar *sptr = bits(); + + // target image data + int wd; + int hd; + + int bpp = depth(); + + // compute size of target image + QWMatrix mat = QPixmap::trueMatrix( matrix, ws, hs ); + if ( mat.m12() == 0.0F && mat.m21() == 0.0F ) { + if ( mat.m11() == 1.0F && mat.m22() == 1.0F ) // identity matrix + return copy(); + hd = qRound( mat.m22() * hs ); + wd = qRound( mat.m11() * ws ); + hd = QABS( hd ); + wd = QABS( wd ); + } else { // rotation or shearing + QPointArray a( QRect(0, 0, ws, hs) ); + a = mat.map( a ); + QRect r = a.boundingRect().normalize(); + wd = r.width(); + hd = r.height(); + } + + bool invertible; + mat = mat.invert( &invertible ); // invert matrix + if ( hd == 0 || wd == 0 || !invertible ) // error, return null image + return QImage(); + + // create target image (some of the code is from QImage::copy()) + QImage dImage( wd, hd, depth(), numColors(), bitOrder() ); + + // If the image allocation failed, we need to gracefully abort. + if (dImage.isNull()) + return dImage; + + memcpy( dImage.colorTable(), colorTable(), numColors()*sizeof(QRgb) ); + dImage.setAlphaBuffer( hasAlphaBuffer() ); + dImage.data->dpmx = dotsPerMeterX(); + dImage.data->dpmy = dotsPerMeterY(); + + switch ( bpp ) { + // initizialize the data + case 1: + memset( dImage.bits(), 0, dImage.numBytes() ); + break; + case 8: + if ( dImage.data->ncols < 256 ) { + // colors are left in the color table, so pick that one as transparent + dImage.setNumColors( dImage.data->ncols+1 ); + dImage.setColor( dImage.data->ncols-1, 0x00 ); + memset( dImage.bits(), dImage.data->ncols-1, dImage.numBytes() ); + } else { + memset( dImage.bits(), 0, dImage.numBytes() ); + } + break; + case 16: + memset( dImage.bits(), 0xff, dImage.numBytes() ); + break; + case 32: + memset( dImage.bits(), 0x00, dImage.numBytes() ); + break; + } + + int type; + if ( bitOrder() == BigEndian ) + type = QT_XFORM_TYPE_MSBFIRST; + else + type = QT_XFORM_TYPE_LSBFIRST; + int dbpl = dImage.bytesPerLine(); + qt_xForm_helper( mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, + ws, hs ); + return dImage; +} +#endif + +/*! + Builds and returns a 1-bpp mask from the alpha buffer in this + image. Returns a \link isNull() null\endlink image if \link + setAlphaBuffer() alpha buffer mode\endlink is disabled. + + See QPixmap::convertFromImage() for a description of the \a + conversion_flags argument. + + The returned image has little-endian bit order, which you can + convert to big-endianness using convertBitOrder(). + + \sa createHeuristicMask() hasAlphaBuffer() setAlphaBuffer() +*/ +#ifndef QT_NO_IMAGE_DITHER_TO_1 +QImage QImage::createAlphaMask( int conversion_flags ) const +{ + if ( conversion_flags == 1 ) { + // Old code is passing "TRUE". + conversion_flags = Qt::DiffuseAlphaDither; + } + + if ( isNull() || !hasAlphaBuffer() ) + return QImage(); + + if ( depth() == 1 ) { + // A monochrome pixmap, with alpha channels on those two colors. + // Pretty unlikely, so use less efficient solution. + return convertDepth(8, conversion_flags) + .createAlphaMask( conversion_flags ); + } + + QImage mask1; + dither_to_1( this, &mask1, conversion_flags, TRUE ); + return mask1; +} +#endif + +#ifndef QT_NO_IMAGE_HEURISTIC_MASK +/*! + Creates and returns a 1-bpp heuristic mask for this image. It + works by selecting a color from one of the corners, then chipping + away pixels of that color starting at all the edges. + + The four corners vote for which color is to be masked away. In + case of a draw (this generally means that this function is not + applicable to the image), the result is arbitrary. + + The returned image has little-endian bit order, which you can + convert to big-endianness using convertBitOrder(). + + If \a clipTight is TRUE the mask is just large enough to cover the + pixels; otherwise, the mask is larger than the data pixels. + + This function disregards the \link hasAlphaBuffer() alpha buffer + \endlink. + + \sa createAlphaMask() +*/ + +QImage QImage::createHeuristicMask( bool clipTight ) const +{ + if ( isNull() ) { + QImage nullImage; + return nullImage; + } + if ( depth() != 32 ) { + QImage img32 = convertDepth(32); + return img32.createHeuristicMask(clipTight); + } + +#define PIX(x,y) (*((QRgb*)scanLine(y)+x) & 0x00ffffff) + + int w = width(); + int h = height(); + QImage m(w, h, 1, 2, QImage::LittleEndian); + m.setColor( 0, 0xffffff ); + m.setColor( 1, 0 ); + m.fill( 0xff ); + + QRgb background = PIX(0,0); + if ( background != PIX(w-1,0) && + background != PIX(0,h-1) && + background != PIX(w-1,h-1) ) { + background = PIX(w-1,0); + if ( background != PIX(w-1,h-1) && + background != PIX(0,h-1) && + PIX(0,h-1) == PIX(w-1,h-1) ) { + background = PIX(w-1,h-1); + } + } + + int x,y; + bool done = FALSE; + uchar *ypp, *ypc, *ypn; + while( !done ) { + done = TRUE; + ypn = m.scanLine(0); + ypc = 0; + for ( y = 0; y < h; y++ ) { + ypp = ypc; + ypc = ypn; + ypn = (y == h-1) ? 0 : m.scanLine(y+1); + QRgb *p = (QRgb *)scanLine(y); + for ( x = 0; x < w; x++ ) { + // slowness here - it's possible to do six of these tests + // together in one go. oh well. + if ( ( x == 0 || y == 0 || x == w-1 || y == h-1 || + !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) || + !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) || + !(*(ypp + (x >> 3)) & (1 << (x & 7))) || + !(*(ypn + (x >> 3)) & (1 << (x & 7))) ) && + ( (*(ypc + (x >> 3)) & (1 << (x & 7))) ) && + ( (*p & 0x00ffffff) == background ) ) { + done = FALSE; + *(ypc + (x >> 3)) &= ~(1 << (x & 7)); + } + p++; + } + } + } + + if ( !clipTight ) { + ypn = m.scanLine(0); + ypc = 0; + for ( y = 0; y < h; y++ ) { + ypp = ypc; + ypc = ypn; + ypn = (y == h-1) ? 0 : m.scanLine(y+1); + QRgb *p = (QRgb *)scanLine(y); + for ( x = 0; x < w; x++ ) { + if ( (*p & 0x00ffffff) != background ) { + if ( x > 0 ) + *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7)); + if ( x < w-1 ) + *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7)); + if ( y > 0 ) + *(ypp + (x >> 3)) |= (1 << (x & 7)); + if ( y < h-1 ) + *(ypn + (x >> 3)) |= (1 << (x & 7)); + } + p++; + } + } + } + +#undef PIX + + return m; +} +#endif //QT_NO_IMAGE_HEURISTIC_MASK + +#ifndef QT_NO_IMAGE_MIRROR +/* + This code is contributed by Philipp Lang, + GeneriCom Software Germany (www.generi.com) + under the terms of the QPL, Version 1.0 +*/ + +/*! + \overload + + Returns a mirror of the image, mirrored in the horizontal and/or + the vertical direction depending on whether \a horizontal and \a + vertical are set to TRUE or FALSE. The original image is not + changed. + + \sa smoothScale() +*/ +QImage QImage::mirror(bool horizontal, bool vertical) const +{ + int w = width(); + int h = height(); + if ( (w <= 1 && h <= 1) || (!horizontal && !vertical) ) + return copy(); + + // Create result image, copy colormap + QImage result(w, h, depth(), numColors(), bitOrder()); + memcpy(result.colorTable(), colorTable(), numColors()*sizeof(QRgb)); + result.setAlphaBuffer(hasAlphaBuffer()); + + if (depth() == 1) + w = (w+7)/8; + int dxi = horizontal ? -1 : 1; + int dxs = horizontal ? w-1 : 0; + int dyi = vertical ? -1 : 1; + int dy = vertical ? h-1: 0; + + // 1 bit, 8 bit + if (depth() == 1 || depth() == 8) { + for (int sy = 0; sy < h; sy++, dy += dyi) { + Q_UINT8* ssl = (Q_UINT8*)(data->bits[sy]); + Q_UINT8* dsl = (Q_UINT8*)(result.data->bits[dy]); + int dx = dxs; + for (int sx = 0; sx < w; sx++, dx += dxi) + dsl[dx] = ssl[sx]; + } + } +#ifndef QT_NO_IMAGE_TRUECOLOR +#ifndef QT_NO_IMAGE_16_BIT + // 16 bit + else if (depth() == 16) { + for (int sy = 0; sy < h; sy++, dy += dyi) { + Q_UINT16* ssl = (Q_UINT16*)(data->bits[sy]); + Q_UINT16* dsl = (Q_UINT16*)(result.data->bits[dy]); + int dx = dxs; + for (int sx = 0; sx < w; sx++, dx += dxi) + dsl[dx] = ssl[sx]; + } + } +#endif + // 32 bit + else if (depth() == 32) { + for (int sy = 0; sy < h; sy++, dy += dyi) { + Q_UINT32* ssl = (Q_UINT32*)(data->bits[sy]); + Q_UINT32* dsl = (Q_UINT32*)(result.data->bits[dy]); + int dx = dxs; + for (int sx = 0; sx < w; sx++, dx += dxi) + dsl[dx] = ssl[sx]; + } + } +#endif + + // special handling of 1 bit images for horizontal mirroring + if (horizontal && depth() == 1) { + int shift = width() % 8; + for (int y = h-1; y >= 0; y--) { + Q_UINT8* a0 = (Q_UINT8*)(result.data->bits[y]); + // Swap bytes + Q_UINT8* a = a0+dxs; + while (a >= a0) { + *a = bitflip[*a]; + a--; + } + // Shift bits if unaligned + if (shift != 0) { + a = a0+dxs; + Q_UINT8 c = 0; + if (bitOrder() == QImage::LittleEndian) { + while (a >= a0) { + Q_UINT8 nc = *a << shift; + *a = (*a >> (8-shift)) | c; + --a; + c = nc; + } + } else { + while (a >= a0) { + Q_UINT8 nc = *a >> shift; + *a = (*a << (8-shift)) | c; + --a; + c = nc; + } + } + } + } + } + + return result; +} + +/*! + Returns a QImage which is a vertically mirrored copy of this + image. The original QImage is not changed. +*/ + +QImage QImage::mirror() const +{ + return mirror(FALSE,TRUE); +} +#endif //QT_NO_IMAGE_MIRROR + +/*! + Returns a QImage in which the values of the red and blue + components of all pixels have been swapped, effectively converting + an RGB image to a BGR image. The original QImage is not changed. +*/ + +QImage QImage::swapRGB() const +{ + QImage res = copy(); + if ( !isNull() ) { +#ifndef QT_NO_IMAGE_TRUECOLOR + if ( depth() == 32 ) { + for ( int i=0; i < height(); i++ ) { + uint *p = (uint*)scanLine( i ); + uint *q = (uint*)res.scanLine( i ); + uint *end = p + width(); + while ( p < end ) { + *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | + (*p & 0xff00ff00); + p++; + q++; + } + } +#ifndef QT_NO_IMAGE_16_BIT + } else if ( depth() == 16 ) { + qWarning( "QImage::swapRGB not implemented for 16bpp" ); +#endif + } else +#endif //QT_NO_IMAGE_TRUECOLOR + { + uint* p = (uint*)colorTable(); + uint* q = (uint*)res.colorTable(); + if ( p && q ) { + for ( int i=0; i < numColors(); i++ ) { + *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | + (*p & 0xff00ff00); + p++; + q++; + } + } + } + } + return res; +} + +#ifndef QT_NO_IMAGEIO +/*! + Returns a string that specifies the image format of the file \a + fileName, or 0 if the file cannot be read or if the format is not + recognized. + + The QImageIO documentation lists the guaranteed supported image + formats, or use QImage::inputFormats() and QImage::outputFormats() + to get lists that include the installed formats. + + \sa load() save() +*/ + +const char* QImage::imageFormat( const QString &fileName ) +{ + return QImageIO::imageFormat( fileName ); +} + +/*! + Returns a list of image formats that are supported for image + input. + + \sa outputFormats() inputFormatList() QImageIO +*/ +QStrList QImage::inputFormats() +{ + return QImageIO::inputFormats(); +} +#ifndef QT_NO_STRINGLIST +/*! + Returns a list of image formats that are supported for image + input. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QStringList list = myImage.inputFormatList(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa outputFormatList() inputFormats() QImageIO +*/ +QStringList QImage::inputFormatList() +{ + return QStringList::fromStrList(QImageIO::inputFormats()); +} + + +/*! + Returns a list of image formats that are supported for image + output. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QStringList list = myImage.outputFormatList(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa inputFormatList() outputFormats() QImageIO +*/ +QStringList QImage::outputFormatList() +{ + return QStringList::fromStrList(QImageIO::outputFormats()); +} +#endif //QT_NO_STRINGLIST + +/*! + Returns a list of image formats that are supported for image + output. + + \sa inputFormats() outputFormatList() QImageIO +*/ +QStrList QImage::outputFormats() +{ + return QImageIO::outputFormats(); +} + + +/*! + Loads an image from the file \a fileName. Returns TRUE if the + image was successfully loaded; otherwise returns FALSE. + + If \a format is specified, the loader attempts to read the image + using the specified format. If \a format is not specified (which + is the default), the loader reads a few bytes from the header to + guess the file format. + + The QImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa loadFromData() save() imageFormat() QPixmap::load() QImageIO +*/ + +bool QImage::load( const QString &fileName, const char* format ) +{ + QImageIO io( fileName, format ); + bool result = io.read(); + if ( result ) + operator=( io.image() ); + return result; +} + +/*! + Loads an image from the first \a len bytes of binary data in \a + buf. Returns TRUE if the image was successfully loaded; otherwise + returns FALSE. + + If \a format is specified, the loader attempts to read the image + using the specified format. If \a format is not specified (which + is the default), the loader reads a few bytes from the header to + guess the file format. + + The QImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa load() save() imageFormat() QPixmap::loadFromData() QImageIO +*/ + +bool QImage::loadFromData( const uchar *buf, uint len, const char *format ) +{ + QByteArray a; + a.setRawData( (char *)buf, len ); + QBuffer b( a ); + b.open( IO_ReadOnly ); + QImageIO io( &b, format ); + bool result = io.read(); + b.close(); + a.resetRawData( (char *)buf, len ); + if ( result ) + operator=( io.image() ); + return result; +} + +/*! + \overload + + Loads an image from the QByteArray \a buf. +*/ +bool QImage::loadFromData( QByteArray buf, const char *format ) +{ + return loadFromData( (const uchar *)(buf.data()), buf.size(), format ); +} + +/*! + Saves the image to the file \a fileName, using the image file + format \a format and a quality factor of \a quality. \a quality + must be in the range 0..100 or -1. Specify 0 to obtain small + compressed files, 100 for large uncompressed files, and -1 (the + default) to use the default settings. + + Returns TRUE if the image was successfully saved; otherwise + returns FALSE. + + \sa load() loadFromData() imageFormat() QPixmap::save() QImageIO +*/ + +bool QImage::save( const QString &fileName, const char* format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + QImageIO io( fileName, format ); + return doImageIO( &io, quality ); +} + +/*! + \overload + + This function writes a QImage to the QIODevice, \a device. This + can be used, for example, to save an image directly into a + QByteArray: + \code + QImage image; + QByteArray ba; + QBuffer buffer( ba ); + buffer.open( IO_WriteOnly ); + image.save( &buffer, "PNG" ); // writes image into ba in PNG format + \endcode +*/ + +bool QImage::save( QIODevice* device, const char* format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + QImageIO io( device, format ); + return doImageIO( &io, quality ); +} + +/* \internal +*/ + +bool QImage::doImageIO( QImageIO* io, int quality ) const +{ + if ( !io ) + return FALSE; + io->setImage( *this ); +#if defined(QT_CHECK_RANGE) + if ( quality > 100 || quality < -1 ) + qWarning( "QPixmap::save: quality out of range [-1,100]" ); +#endif + if ( quality >= 0 ) + io->setQuality( QMIN(quality,100) ); + return io->write(); +} +#endif //QT_NO_IMAGEIO + +/***************************************************************************** + QImage stream functions + *****************************************************************************/ +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +/*! + \relates QImage + + Writes the image \a image to the stream \a s as a PNG image, or as a + BMP image if the stream's version is 1. + + Note that writing the stream to a file will not produce a valid image file. + + \sa QImage::save() + \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QImage &image ) +{ + if ( s.version() >= 5 ) { + if ( image.isNull() ) { + s << (Q_INT32) 0; // null image marker + return s; + } else { + s << (Q_INT32) 1; + // continue ... + } + } + QImageIO io; + io.setIODevice( s.device() ); + if ( s.version() == 1 ) + io.setFormat( "BMP" ); + else + io.setFormat( "PNG" ); + + io.setImage( image ); + io.write(); + return s; +} + +/*! + \relates QImage + + Reads an image from the stream \a s and stores it in \a image. + + \sa QImage::load() + \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QImage &image ) +{ + if ( s.version() >= 5 ) { + Q_INT32 nullMarker; + s >> nullMarker; + if ( !nullMarker ) { + image = QImage(); // null image + return s; + } + } + QImageIO io( s.device(), 0 ); + if ( io.read() ) + image = io.image(); + return s; +} +#endif + +/***************************************************************************** + Standard image io handlers (defined below) + *****************************************************************************/ + +// standard image io handlers (defined below) +#ifndef QT_NO_IMAGEIO_BMP +static void read_bmp_image( QImageIO * ); +static void write_bmp_image( QImageIO * ); +#endif +#ifndef QT_NO_IMAGEIO_PPM +static void read_pbm_image( QImageIO * ); +static void write_pbm_image( QImageIO * ); +#endif +#ifndef QT_NO_IMAGEIO_XBM +static void read_xbm_image( QImageIO * ); +static void write_xbm_image( QImageIO * ); +#endif +#ifndef QT_NO_IMAGEIO_XPM +static void read_xpm_image( QImageIO * ); +static void write_xpm_image( QImageIO * ); +#endif + +#ifndef QT_NO_ASYNC_IMAGE_IO +static void read_async_image( QImageIO * ); // Not in table of handlers +#endif + +/***************************************************************************** + Misc. utility functions + *****************************************************************************/ +#if !defined(QT_NO_IMAGEIO_XPM) || !defined(QT_NO_IMAGEIO_XBM) +static QString fbname( const QString &fileName ) // get file basename (sort of) +{ + QString s = fileName; + if ( !s.isEmpty() ) { + int i; + if ( (i = s.findRev('/')) >= 0 ) + s = s.mid( i ); + if ( (i = s.findRev('\\')) >= 0 ) + s = s.mid( i ); + QRegExp r( QString::fromLatin1("[a-zA-Z][a-zA-Z0-9_]*") ); + int p = r.search( s ); + if ( p == -1 ) + s.truncate( 0 ); + else + s = s.mid( p, r.matchedLength() ); + } + if ( s.isEmpty() ) + s = QString::fromLatin1( "dummy" ); + return s; +} +#endif + +#ifndef QT_NO_IMAGEIO_BMP +static void swapPixel01( QImage *image ) // 1-bpp: swap 0 and 1 pixels +{ + int i; + if ( image->depth() == 1 && image->numColors() == 2 ) { + register uint *p = (uint *)image->bits(); + int nbytes = image->numBytes(); + for ( i=0; i<nbytes/4; i++ ) { + *p = ~*p; + p++; + } + uchar *p2 = (uchar *)p; + for ( i=0; i<(nbytes&3); i++ ) { + *p2 = ~*p2; + p2++; + } + QRgb t = image->color(0); // swap color 0 and 1 + image->setColor( 0, image->color(1) ); + image->setColor( 1, t ); + } +} +#endif + + +/***************************************************************************** + QImageIO member functions + *****************************************************************************/ + +/*! + \class QImageIO qimage.h + + \brief The QImageIO class contains parameters for loading and + saving images. + + \ingroup images + \ingroup graphics + \ingroup io + + QImageIO contains a QIODevice object that is used for image data + I/O. The programmer can install new image file formats in addition + to those that Qt provides. + + Qt currently supports the following image file formats: PNG, BMP, + XBM, XPM and PNM. It may also support JPEG, MNG and GIF, if + specially configured during compilation. The different PNM formats + are: PBM (P1 or P4), PGM (P2 or P5), and PPM (P3 or P6). + + You don't normally need to use this class; QPixmap::load(), + QPixmap::save(), and QImage contain sufficient functionality. + + For image files that contain sequences of images, only the first + is read. See QMovie for loading multiple images. + + PBM, PGM, and PPM format \e output is always in the more condensed + raw format. PPM and PGM files with more than 256 levels of + intensity are scaled down when reading. + + \warning If you are in a country which recognizes software patents + and in which Unisys holds a patent on LZW compression and/or + decompression and you want to use GIF, Unisys may require you to + license the technology. Such countries include Canada, Japan, the + USA, France, Germany, Italy and the UK. + + GIF support may be removed completely in a future version of Qt. + We recommend using the PNG format. + + \sa QImage QPixmap QFile QMovie +*/ + +#ifndef QT_NO_IMAGEIO +struct QImageIOData +{ + const char *parameters; + int quality; + float gamma; +}; + +/*! + Constructs a QImageIO object with all parameters set to zero. +*/ + +QImageIO::QImageIO() +{ + init(); +} + +/*! + Constructs a QImageIO object with the I/O device \a ioDevice and a + \a format tag. +*/ + +QImageIO::QImageIO( QIODevice *ioDevice, const char *format ) + : frmt(format) +{ + init(); + iodev = ioDevice; +} + +/*! + Constructs a QImageIO object with the file name \a fileName and a + \a format tag. +*/ + +QImageIO::QImageIO( const QString &fileName, const char* format ) + : frmt(format), fname(fileName) +{ + init(); +} + +/*! + Contains initialization common to all QImageIO constructors. +*/ + +void QImageIO::init() +{ + d = new QImageIOData(); + d->parameters = 0; + d->quality = -1; // default quality of the current format + d->gamma=0.0f; + iostat = 0; + iodev = 0; +} + +/*! + Destroys the object and all related data. +*/ + +QImageIO::~QImageIO() +{ + if ( d->parameters ) + delete [] (char*)d->parameters; + delete d; +} + + +/***************************************************************************** + QImageIO image handler functions + *****************************************************************************/ + +class QImageHandler +{ +public: + QImageHandler( const char *f, const char *h, const QCString& fl, + image_io_handler r, image_io_handler w ); + QCString format; // image format + QRegExp header; // image header pattern + enum TMode { Untranslated=0, TranslateIn, TranslateInOut } text_mode; + image_io_handler read_image; // image read function + image_io_handler write_image; // image write function + bool obsolete; // support not "published" +}; + +QImageHandler::QImageHandler( const char *f, const char *h, const QCString& fl, + image_io_handler r, image_io_handler w ) + : format(f), header(QString::fromLatin1(h)) +{ + text_mode = Untranslated; + if ( fl.contains('t') ) + text_mode = TranslateIn; + else if ( fl.contains('T') ) + text_mode = TranslateInOut; + obsolete = fl.contains('O'); + read_image = r; + write_image = w; +} + +typedef QPtrList<QImageHandler> QIHList;// list of image handlers +static QIHList *imageHandlers = 0; +#ifndef QT_NO_COMPONENT +static QPluginManager<QImageFormatInterface> *plugin_manager = 0; +#else +static void *plugin_manager = 0; +#endif + +void qt_init_image_plugins() +{ +#ifndef QT_NO_COMPONENT + if ( plugin_manager ) + return; + + plugin_manager = new QPluginManager<QImageFormatInterface>( IID_QImageFormat, QApplication::libraryPaths(), "/imageformats" ); + + QStringList features = plugin_manager->featureList(); + QStringList::Iterator it = features.begin(); + while ( it != features.end() ) { + QString str = *it; + ++it; + QInterfacePtr<QImageFormatInterface> iface; + plugin_manager->queryInterface( str, &iface ); + if ( iface ) + iface->installIOHandler( str ); + } +#endif +} + +static void cleanup() +{ + // make sure that image handlers are delete before plugin manager + delete imageHandlers; + imageHandlers = 0; +#ifndef QT_NO_COMPONENT + delete plugin_manager; + plugin_manager = 0; +#endif +} + +void qt_init_image_handlers() // initialize image handlers +{ + if ( !imageHandlers ) { + imageHandlers = new QIHList; + Q_CHECK_PTR( imageHandlers ); + imageHandlers->setAutoDelete( TRUE ); + qAddPostRoutine( cleanup ); +#ifndef QT_NO_IMAGEIO_BMP + QImageIO::defineIOHandler( "BMP", "^BM", 0, + read_bmp_image, write_bmp_image ); +#endif +#ifndef QT_NO_IMAGEIO_PPM + QImageIO::defineIOHandler( "PBM", "^P1", "t", + read_pbm_image, write_pbm_image ); + QImageIO::defineIOHandler( "PBMRAW", "^P4", "O", + read_pbm_image, write_pbm_image ); + QImageIO::defineIOHandler( "PGM", "^P2", "t", + read_pbm_image, write_pbm_image ); + QImageIO::defineIOHandler( "PGMRAW", "^P5", "O", + read_pbm_image, write_pbm_image ); + QImageIO::defineIOHandler( "PPM", "^P3", "t", + read_pbm_image, write_pbm_image ); + QImageIO::defineIOHandler( "PPMRAW", "^P6", "O", + read_pbm_image, write_pbm_image ); +#endif +#ifndef QT_NO_IMAGEIO_XBM + QImageIO::defineIOHandler( "XBM", "^((/\\*(?!.XPM.\\*/))|#define)", "T", + read_xbm_image, write_xbm_image ); +#endif +#ifndef QT_NO_IMAGEIO_XPM + QImageIO::defineIOHandler( "XPM", "/\\*.XPM.\\*/", "T", + read_xpm_image, write_xpm_image ); +#endif +#ifndef QT_NO_IMAGEIO_MNG + qInitMngIO(); +#endif +#ifndef QT_NO_IMAGEIO_PNG + qInitPngIO(); +#endif +#ifndef QT_NO_IMAGEIO_JPEG + qInitJpegIO(); +#endif + } +} + +static QImageHandler *get_image_handler( const char *format ) +{ // get pointer to handler + qt_init_image_handlers(); + qt_init_image_plugins(); + register QImageHandler *p = imageHandlers->first(); + while ( p ) { // traverse list + if ( p->format == format ) + return p; + p = imageHandlers->next(); + } + return 0; // no such handler +} + + +/*! + Defines an image I/O handler for the image format called \a + format, which is recognized using the \link qregexp.html#details + regular expression\endlink \a header, read using \a readImage and + written using \a writeImage. + + \a flags is a string of single-character flags for this format. + The only flag defined currently is T (upper case), so the only + legal value for \a flags are "T" and the empty string. The "T" + flag means that the image file is a text file, and Qt should treat + all newline conventions as equivalent. (XPM files and some PPM + files are text files for example.) + + \a format is used to select a handler to write a QImage; \a header + is used to select a handler to read an image file. + + If \a readImage is a null pointer, the QImageIO will not be able + to read images in \a format. If \a writeImage is a null pointer, + the QImageIO will not be able to write images in \a format. If + both are null, the QImageIO object is valid but useless. + + Example: + \code + void readGIF( QImageIO *image ) + { + // read the image using the image->ioDevice() + } + + void writeGIF( QImageIO *image ) + { + // write the image using the image->ioDevice() + } + + // add the GIF image handler + + QImageIO::defineIOHandler( "GIF", + "^GIF[0-9][0-9][a-z]", + 0, + readGIF, + writeGIF ); + \endcode + + Before the regex test, all the 0 bytes in the file header are + converted to 1 bytes. This is done because when Qt was + ASCII-based, QRegExp could not handle 0 bytes in strings. + + The regexp is only applied on the first 14 bytes of the file. + + Note that Qt assumes that there is only one handler per format; if + two handlers support the same format, Qt will choose one + arbitrarily. It is not possible to have one handler support + reading, and another support writing. +*/ + +void QImageIO::defineIOHandler( const char *format, + const char *header, + const char *flags, + image_io_handler readImage, + image_io_handler writeImage ) +{ + qt_init_image_handlers(); + QImageHandler *p; + p = new QImageHandler( format, header, flags, + readImage, writeImage ); + Q_CHECK_PTR( p ); + imageHandlers->insert( 0, p ); +} + + +/***************************************************************************** + QImageIO normal member functions + *****************************************************************************/ + +/*! + \fn const QImage &QImageIO::image() const + + Returns the image currently set. + + \sa setImage() +*/ + +/*! + \fn int QImageIO::status() const + + Returns the image's IO status. A non-zero value indicates an + error, whereas 0 means that the IO operation was successful. + + \sa setStatus() +*/ + +/*! + \fn const char *QImageIO::format() const + + Returns the image format string or 0 if no format has been + explicitly set. +*/ + +/*! + \fn QIODevice *QImageIO::ioDevice() const + + Returns the IO device currently set. + + \sa setIODevice() +*/ + +/*! + \fn QString QImageIO::fileName() const + + Returns the file name currently set. + + \sa setFileName() +*/ + +/*! + \fn QString QImageIO::description() const + + Returns the image description string. + + \sa setDescription() +*/ + + +/*! + Sets the image to \a image. + + \sa image() +*/ + +void QImageIO::setImage( const QImage &image ) +{ + im = image; +} + +/*! + Sets the image IO status to \a status. A non-zero value indicates + an error, whereas 0 means that the IO operation was successful. + + \sa status() +*/ + +void QImageIO::setStatus( int status ) +{ + iostat = status; +} + +/*! + Sets the image format to \a format for the image to be read or + written. + + It is necessary to specify a format before writing an image, but + it is not necessary to specify a format before reading an image. + + If no format has been set, Qt guesses the image format before + reading it. If a format is set the image will only be read if it + has that format. + + \sa read() write() format() +*/ + +void QImageIO::setFormat( const char *format ) +{ + frmt = format; +} + +/*! + Sets the IO device to be used for reading or writing an image. + + Setting the IO device allows images to be read/written to any + block-oriented QIODevice. + + If \a ioDevice is not null, this IO device will override file name + settings. + + \sa setFileName() +*/ + +void QImageIO::setIODevice( QIODevice *ioDevice ) +{ + iodev = ioDevice; +} + +/*! + Sets the name of the file to read or write an image from to \a + fileName. + + \sa setIODevice() +*/ + +void QImageIO::setFileName( const QString &fileName ) +{ + fname = fileName; +} + +/*! + Returns the quality of the written image, related to the + compression ratio. + + \sa setQuality() QImage::save() +*/ + +int QImageIO::quality() const +{ + return d->quality; +} + +/*! + Sets the quality of the written image to \a q, related to the + compression ratio. + + \a q must be in the range -1..100. Specify 0 to obtain small + compressed files, 100 for large uncompressed files. (-1 signifies + the default compression.) + + \sa quality() QImage::save() +*/ + +void QImageIO::setQuality( int q ) +{ + d->quality = q; +} + +/*! + Returns the image's parameters string. + + \sa setParameters() +*/ + +const char *QImageIO::parameters() const +{ + return d->parameters; +} + +/*! + Sets the image's parameter string to \a parameters. This is for + image handlers that require special parameters. + + Although the current image formats supported by Qt ignore the + parameters string, it may be used in future extensions or by + contributions (for example, JPEG). + + \sa parameters() +*/ + +void QImageIO::setParameters( const char *parameters ) +{ + if ( d && d->parameters ) + delete [] (char*)d->parameters; + d->parameters = qstrdup( parameters ); +} + +/*! + Sets the gamma value at which the image will be viewed to \a + gamma. If the image format stores a gamma value for which the + image is intended to be used, then this setting will be used to + modify the image. Setting to 0.0 will disable gamma correction + (i.e. any specification in the file will be ignored). + + The default value is 0.0. + + \sa gamma() +*/ +void QImageIO::setGamma( float gamma ) +{ + d->gamma=gamma; +} + +/*! + Returns the gamma value at which the image will be viewed. + + \sa setGamma() +*/ +float QImageIO::gamma() const +{ + return d->gamma; +} + +/*! + Sets the image description string for image handlers that support + image descriptions to \a description. + + Currently, no image format supported by Qt uses the description + string. +*/ + +void QImageIO::setDescription( const QString &description ) +{ + descr = description; +} + + +/*! + Returns a string that specifies the image format of the file \a + fileName, or null if the file cannot be read or if the format is + not recognized. +*/ + +const char* QImageIO::imageFormat( const QString &fileName ) +{ + QFile file( fileName ); + if ( !file.open(IO_ReadOnly) ) + return 0; + const char* format = imageFormat( &file ); + file.close(); + return format; +} + +/*! + \overload + + Returns a string that specifies the image format of the image read + from IO device \a d, or 0 if the device cannot be read or if the + format is not recognized. + + Make sure that \a d is at the right position in the device (for + example, at the beginning of the file). + + \sa QIODevice::at() +*/ + +const char *QImageIO::imageFormat( QIODevice *d ) +{ + // if you change this change the documentation for defineIOHandler() + const int buflen = 14; + + char buf[buflen]; + char buf2[buflen]; + qt_init_image_handlers(); + qt_init_image_plugins(); + int pos = d->at(); // save position + int rdlen = d->readBlock( buf, buflen ); // read a few bytes + + if ( rdlen != buflen ) + return 0; + + memcpy( buf2, buf, buflen ); + + const char* format = 0; + for ( int n = 0; n < rdlen; n++ ) + if ( buf[n] == '\0' ) + buf[n] = '\001'; + if ( d->status() == IO_Ok && rdlen > 0 ) { + buf[rdlen - 1] = '\0'; + QString bufStr = QString::fromLatin1(buf); + QImageHandler *p = imageHandlers->first(); + int bestMatch = -1; + while ( p ) { + if ( p->read_image && p->header.search(bufStr) != -1 ) { + // try match with header if a read function is available + if (p->header.matchedLength() > bestMatch) { + // keep looking for best match + format = p->format; + bestMatch = p->header.matchedLength(); + } + } + p = imageHandlers->next(); + } + } + d->at( pos ); // restore position +#ifndef QT_NO_ASYNC_IMAGE_IO + if ( !format ) + format = QImageDecoder::formatName( (uchar*)buf2, rdlen ); +#endif + + return format; +} + +/*! + Returns a sorted list of image formats that are supported for + image input. +*/ +QStrList QImageIO::inputFormats() +{ + QStrList result; + + qt_init_image_handlers(); + qt_init_image_plugins(); + +#ifndef QT_NO_ASYNC_IMAGE_IO + // Include asynchronous loaders first. + result = QImageDecoder::inputFormats(); +#endif + + QImageHandler *p = imageHandlers->first(); + while ( p ) { + if ( p->read_image + && !p->obsolete + && !result.contains(p->format) ) + { + result.inSort(p->format); + } + p = imageHandlers->next(); + } + + return result; +} + +/*! + Returns a sorted list of image formats that are supported for + image output. +*/ +QStrList QImageIO::outputFormats() +{ + QStrList result; + + qt_init_image_handlers(); + qt_init_image_plugins(); + + // Include asynchronous writers (!) first. + // (None) + + QImageHandler *p = imageHandlers->first(); + while ( p ) { + if ( p->write_image + && !p->obsolete + && !result.contains(p->format) ) + { + result.inSort(p->format); + } + p = imageHandlers->next(); + } + + return result; +} + + + +/*! + Reads an image into memory and returns TRUE if the image was + successfully read; otherwise returns FALSE. + + Before reading an image you must set an IO device or a file name. + If both an IO device and a file name have been set, the IO device + will be used. + + Setting the image file format string is optional. + + Note that this function does \e not set the \link format() + format\endlink used to read the image. If you need that + information, use the imageFormat() static functions. + + Example: + + \code + QImageIO iio; + QPixmap pixmap; + iio.setFileName( "vegeburger.bmp" ); + if ( image.read() ) // ok + pixmap = iio.image(); // convert to pixmap + \endcode + + \sa setIODevice() setFileName() setFormat() write() QPixmap::load() +*/ + +bool QImageIO::read() +{ + QFile file; + const char *image_format; + QImageHandler *h; + + if ( iodev ) { // read from io device + // ok, already open + } else if ( !fname.isEmpty() ) { // read from file + file.setName( fname ); + if ( !file.open(IO_ReadOnly) ) + return FALSE; // cannot open file + iodev = &file; + } else { // no file name or io device + return FALSE; + } + if (frmt.isEmpty()) { + // Try to guess format + image_format = imageFormat( iodev ); // get image format + if ( !image_format ) { + if ( file.isOpen() ) { // unknown format + file.close(); + iodev = 0; + } + return FALSE; + } + } else { + image_format = frmt; + } + + h = get_image_handler( image_format ); + if ( file.isOpen() ) { +#if !defined(Q_OS_UNIX) + if ( h && h->text_mode ) { // reopen in translated mode + file.close(); + file.open( IO_ReadOnly | IO_Translate ); + } + else +#endif + file.at( 0 ); // position to start + } + iostat = 1; // assume error + + if ( h && h->read_image ) { + (*h->read_image)( this ); + } +#ifndef QT_NO_ASYNC_IMAGE_IO + else { + // Format name, but no handler - must be an asychronous reader + read_async_image( this ); + } +#endif + + if ( file.isOpen() ) { // image was read using file + file.close(); + iodev = 0; + } + return iostat == 0; // image successfully read? +} + + +/*! + Writes an image to an IO device and returns TRUE if the image was + successfully written; otherwise returns FALSE. + + Before writing an image you must set an IO device or a file name. + If both an IO device and a file name have been set, the IO device + will be used. + + The image will be written using the specified image format. + + Example: + \code + QImageIO iio; + QImage im; + im = pixmap; // convert to image + iio.setImage( im ); + iio.setFileName( "vegeburger.bmp" ); + iio.setFormat( "BMP" ); + if ( iio.write() ) + // returned TRUE if written successfully + \endcode + + \sa setIODevice() setFileName() setFormat() read() QPixmap::save() +*/ + +bool QImageIO::write() +{ + if ( frmt.isEmpty() ) + return FALSE; + QImageHandler *h = get_image_handler( frmt ); + if ( !h && !plugin_manager) { + qt_init_image_plugins(); + h = get_image_handler( frmt ); + } + if ( !h || !h->write_image ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImageIO::write: No such image format handler: %s", + format() ); +#endif + return FALSE; + } + QFile file; + if ( !iodev && !fname.isEmpty() ) { + file.setName( fname ); + bool translate = h->text_mode==QImageHandler::TranslateInOut; + int fmode = translate ? IO_WriteOnly|IO_Translate : IO_WriteOnly; + if ( !file.open(fmode) ) // couldn't create file + return FALSE; + iodev = &file; + } + iostat = 1; + (*h->write_image)( this ); + if ( file.isOpen() ) { // image was written using file + file.close(); + iodev = 0; + } + return iostat == 0; // image successfully written? +} +#endif //QT_NO_IMAGEIO + +#ifndef QT_NO_IMAGEIO_BMP + +/***************************************************************************** + BMP (DIB) image read/write functions + *****************************************************************************/ + +const int BMP_FILEHDR_SIZE = 14; // size of BMP_FILEHDR data + +struct BMP_FILEHDR { // BMP file header + char bfType[2]; // "BM" + Q_INT32 bfSize; // size of file + Q_INT16 bfReserved1; + Q_INT16 bfReserved2; + Q_INT32 bfOffBits; // pointer to the pixmap bits +}; + +QDataStream &operator>>( QDataStream &s, BMP_FILEHDR &bf ) +{ // read file header + s.readRawBytes( bf.bfType, 2 ); + s >> bf.bfSize >> bf.bfReserved1 >> bf.bfReserved2 >> bf.bfOffBits; + return s; +} + +QDataStream &operator<<( QDataStream &s, const BMP_FILEHDR &bf ) +{ // write file header + s.writeRawBytes( bf.bfType, 2 ); + s << bf.bfSize << bf.bfReserved1 << bf.bfReserved2 << bf.bfOffBits; + return s; +} + + +const int BMP_OLD = 12; // old Windows/OS2 BMP size +const int BMP_WIN = 40; // new Windows BMP size +const int BMP_OS2 = 64; // new OS/2 BMP size + +const int BMP_RGB = 0; // no compression +const int BMP_RLE8 = 1; // run-length encoded, 8 bits +const int BMP_RLE4 = 2; // run-length encoded, 4 bits +const int BMP_BITFIELDS = 3; // RGB values encoded in data as bit-fields + +struct BMP_INFOHDR { // BMP information header + Q_INT32 biSize; // size of this struct + Q_INT32 biWidth; // pixmap width + Q_INT32 biHeight; // pixmap height + Q_INT16 biPlanes; // should be 1 + Q_INT16 biBitCount; // number of bits per pixel + Q_INT32 biCompression; // compression method + Q_INT32 biSizeImage; // size of image + Q_INT32 biXPelsPerMeter; // horizontal resolution + Q_INT32 biYPelsPerMeter; // vertical resolution + Q_INT32 biClrUsed; // number of colors used + Q_INT32 biClrImportant; // number of important colors +}; + + +QDataStream &operator>>( QDataStream &s, BMP_INFOHDR &bi ) +{ + s >> bi.biSize; + if ( bi.biSize == BMP_WIN || bi.biSize == BMP_OS2 ) { + s >> bi.biWidth >> bi.biHeight >> bi.biPlanes >> bi.biBitCount; + s >> bi.biCompression >> bi.biSizeImage; + s >> bi.biXPelsPerMeter >> bi.biYPelsPerMeter; + s >> bi.biClrUsed >> bi.biClrImportant; + } + else { // probably old Windows format + Q_INT16 w, h; + s >> w >> h >> bi.biPlanes >> bi.biBitCount; + bi.biWidth = w; + bi.biHeight = h; + bi.biCompression = BMP_RGB; // no compression + bi.biSizeImage = 0; + bi.biXPelsPerMeter = bi.biYPelsPerMeter = 0; + bi.biClrUsed = bi.biClrImportant = 0; + } + return s; +} + +QDataStream &operator<<( QDataStream &s, const BMP_INFOHDR &bi ) +{ + s << bi.biSize; + s << bi.biWidth << bi.biHeight; + s << bi.biPlanes; + s << bi.biBitCount; + s << bi.biCompression; + s << bi.biSizeImage; + s << bi.biXPelsPerMeter << bi.biYPelsPerMeter; + s << bi.biClrUsed << bi.biClrImportant; + return s; +} + +static +int calc_shift(int mask) +{ + int result = 0; + while (!(mask & 1)) { + result++; + mask >>= 1; + } + return result; +} + +static +bool read_dib( QDataStream& s, int offset, int startpos, QImage& image ) +{ + BMP_INFOHDR bi; + QIODevice* d = s.device(); + + s >> bi; // read BMP info header + if ( d->atEnd() ) // end of stream/file + return FALSE; +#if 0 + qDebug( "offset...........%d", offset ); + qDebug( "startpos.........%d", startpos ); + qDebug( "biSize...........%d", bi.biSize ); + qDebug( "biWidth..........%d", bi.biWidth ); + qDebug( "biHeight.........%d", bi.biHeight ); + qDebug( "biPlanes.........%d", bi.biPlanes ); + qDebug( "biBitCount.......%d", bi.biBitCount ); + qDebug( "biCompression....%d", bi.biCompression ); + qDebug( "biSizeImage......%d", bi.biSizeImage ); + qDebug( "biXPelsPerMeter..%d", bi.biXPelsPerMeter ); + qDebug( "biYPelsPerMeter..%d", bi.biYPelsPerMeter ); + qDebug( "biClrUsed........%d", bi.biClrUsed ); + qDebug( "biClrImportant...%d", bi.biClrImportant ); +#endif + int w = bi.biWidth, h = bi.biHeight, nbits = bi.biBitCount; + int t = bi.biSize, comp = bi.biCompression; + int red_mask, green_mask, blue_mask; + int red_shift = 0; + int green_shift = 0; + int blue_shift = 0; + int red_scale = 0; + int green_scale = 0; + int blue_scale = 0; + + if ( !(nbits == 1 || nbits == 4 || nbits == 8 || nbits == 16 || nbits == 24 || nbits == 32) || + bi.biPlanes != 1 || comp > BMP_BITFIELDS ) + return FALSE; // weird BMP image + if ( !(comp == BMP_RGB || (nbits == 4 && comp == BMP_RLE4) || + (nbits == 8 && comp == BMP_RLE8) || ((nbits == 16 || nbits == 32) && comp == BMP_BITFIELDS)) ) + return FALSE; // weird compression type + + int ncols; + int depth; + switch ( nbits ) { + case 32: + case 24: + case 16: + depth = 32; + break; + case 8: + case 4: + depth = 8; + break; + default: + depth = 1; + } + if ( depth == 32 ) // there's no colormap + ncols = 0; + else // # colors used + ncols = bi.biClrUsed ? bi.biClrUsed : 1 << nbits; + + image.create( w, h, depth, ncols, nbits == 1 ? + QImage::BigEndian : QImage::IgnoreEndian ); + if ( image.isNull() ) // could not create image + return FALSE; + + image.setDotsPerMeterX( bi.biXPelsPerMeter ); + image.setDotsPerMeterY( bi.biYPelsPerMeter ); + + d->at( startpos + BMP_FILEHDR_SIZE + bi.biSize ); // goto start of colormap + + if ( ncols > 0 ) { // read color table + uchar rgb[4]; + int rgb_len = t == BMP_OLD ? 3 : 4; + for ( int i=0; i<ncols; i++ ) { + if ( d->readBlock( (char *)rgb, rgb_len ) != rgb_len ) + return FALSE; + image.setColor( i, qRgb(rgb[2],rgb[1],rgb[0]) ); + if ( d->atEnd() ) // truncated file + return FALSE; + } + } else if (comp == BMP_BITFIELDS && (nbits == 16 || nbits == 32)) { + if ( (Q_ULONG)d->readBlock( (char *)&red_mask, sizeof(red_mask) ) != sizeof(red_mask) ) + return FALSE; + if ( (Q_ULONG)d->readBlock( (char *)&green_mask, sizeof(green_mask) ) != sizeof(green_mask) ) + return FALSE; + if ( (Q_ULONG)d->readBlock( (char *)&blue_mask, sizeof(blue_mask) ) != sizeof(blue_mask) ) + return FALSE; + red_shift = calc_shift(red_mask); + red_scale = 256 / ((red_mask >> red_shift) + 1); + green_shift = calc_shift(green_mask); + green_scale = 256 / ((green_mask >> green_shift) + 1); + blue_shift = calc_shift(blue_mask); + blue_scale = 256 / ((blue_mask >> blue_shift) + 1); + } else if (comp == BMP_RGB && (nbits == 24 || nbits == 32)) { + blue_mask = 0x000000ff; + green_mask = 0x0000ff00; + red_mask = 0x00ff0000; + blue_shift = 0; + green_shift = 8; + red_shift = 16; + blue_scale = green_scale = red_scale = 1; + } else if (comp == BMP_RGB && nbits == 16) // don't support RGB values for 15/16 bpp + return FALSE; + + // offset can be bogus, be careful + if (offset>=0 && startpos + offset > (Q_LONG)d->at() ) + d->at( startpos + offset ); // start of image data + + int bpl = image.bytesPerLine(); +#ifdef Q_WS_QWS + // + // Guess the number of bytes-per-line if we don't know how much + // image data is in the file (bogus image ?). + // + int bmpbpl = bi.biSizeImage > 0 ? + bi.biSizeImage / bi.biHeight : + (d->size() - offset) / bi.biHeight; + int pad = bmpbpl-bpl; +#endif + uchar **line = image.jumpTable(); + + if ( nbits == 1 ) { // 1 bit BMP image + while ( --h >= 0 ) { + if ( d->readBlock((char*)line[h],bpl) != bpl ) + break; +#ifdef Q_WS_QWS + if ( pad > 0 ) + d->at(d->at()+pad); +#endif + } + if ( ncols == 2 && qGray(image.color(0)) < qGray(image.color(1)) ) + swapPixel01( &image ); // pixel 0 is white! + } + + else if ( nbits == 4 ) { // 4 bit BMP image + int buflen = ((w+7)/8)*4; + uchar *buf = new uchar[buflen]; + Q_CHECK_PTR( buf ); + if ( comp == BMP_RLE4 ) { // run length compression + int x=0, y=0, b, c, i; + register uchar *p = line[h-1]; + uchar *endp = line[h-1]+w; + while ( y < h ) { + if ( (b=d->getch()) == EOF ) + break; + if ( b == 0 ) { // escape code + switch ( (b=d->getch()) ) { + case 0: // end of line + x = 0; + y++; + p = line[h-y-1]; + break; + case 1: // end of image + case EOF: // end of file + y = h; // exit loop + break; + case 2: // delta (jump) + x += d->getch(); + y += d->getch(); + + // Protection + if ( (uint)x >= (uint)w ) + x = w-1; + if ( (uint)y >= (uint)h ) + y = h-1; + + p = line[h-y-1] + x; + break; + default: // absolute mode + // Protection + if ( p + b > endp ) + b = endp-p; + + i = (c = b)/2; + while ( i-- ) { + b = d->getch(); + *p++ = b >> 4; + *p++ = b & 0x0f; + } + if ( c & 1 ) + *p++ = d->getch() >> 4; + if ( (((c & 3) + 1) & 2) == 2 ) + d->getch(); // align on word boundary + x += c; + } + } else { // encoded mode + // Protection + if ( p + b > endp ) + b = endp-p; + + i = (c = b)/2; + b = d->getch(); // 2 pixels to be repeated + while ( i-- ) { + *p++ = b >> 4; + *p++ = b & 0x0f; + } + if ( c & 1 ) + *p++ = b >> 4; + x += c; + } + } + } else if ( comp == BMP_RGB ) { // no compression + while ( --h >= 0 ) { + if ( d->readBlock((char*)buf,buflen) != buflen ) + break; + register uchar *p = line[h]; + uchar *b = buf; + for ( int i=0; i<w/2; i++ ) { // convert nibbles to bytes + *p++ = *b >> 4; + *p++ = *b++ & 0x0f; + } + if ( w & 1 ) // the last nibble + *p = *b >> 4; + } + } + delete [] buf; + } + + else if ( nbits == 8 ) { // 8 bit BMP image + if ( comp == BMP_RLE8 ) { // run length compression + int x=0, y=0, b; + register uchar *p = line[h-1]; + const uchar *endp = line[h-1]+w; + while ( y < h ) { + if ( (b=d->getch()) == EOF ) + break; + if ( b == 0 ) { // escape code + switch ( (b=d->getch()) ) { + case 0: // end of line + x = 0; + y++; + p = line[h-y-1]; + break; + case 1: // end of image + case EOF: // end of file + y = h; // exit loop + break; + case 2: // delta (jump) + x += d->getch(); + y += d->getch(); + + // Protection + if ( (uint)x >= (uint)w ) + x = w-1; + if ( (uint)y >= (uint)h ) + y = h-1; + + p = line[h-y-1] + x; + break; + default: // absolute mode + // Protection + if ( p + b > endp ) + b = endp-p; + + if ( d->readBlock( (char *)p, b ) != b ) + return FALSE; + if ( (b & 1) == 1 ) + d->getch(); // align on word boundary + x += b; + p += b; + } + } else { // encoded mode + // Protection + if ( p + b > endp ) + b = endp-p; + + memset( p, d->getch(), b ); // repeat pixel + x += b; + p += b; + } + } + } else if ( comp == BMP_RGB ) { // uncompressed + while ( --h >= 0 ) { + if ( d->readBlock((char *)line[h],bpl) != bpl ) + break; +#ifdef Q_WS_QWS + if ( pad > 0 ) + d->at(d->at()+pad); +#endif + } + } + } + + else if ( nbits == 16 || nbits == 24 || nbits == 32 ) { // 16,24,32 bit BMP image + register QRgb *p; + QRgb *end; + uchar *buf24 = new uchar[bpl]; + int bpl24 = ((w*nbits+31)/32)*4; + uchar *b; + int c; + + while ( --h >= 0 ) { + p = (QRgb *)line[h]; + end = p + w; + if ( d->readBlock( (char *)buf24,bpl24) != bpl24 ) + break; + b = buf24; + while ( p < end ) { + c = *(uchar*)b | (*(uchar*)(b+1)<<8); + if (nbits != 16) + c |= *(uchar*)(b+2)<<16; + *p++ = qRgb(((c & red_mask) >> red_shift) * red_scale, + ((c & green_mask) >> green_shift) * green_scale, + ((c & blue_mask) >> blue_shift) * blue_scale); + b += nbits/8; + } + } + delete[] buf24; + } + + return TRUE; +} + +bool qt_read_dib( QDataStream& s, QImage& image ) +{ + return read_dib(s,-1,-BMP_FILEHDR_SIZE,image); +} + + +static void read_bmp_image( QImageIO *iio ) +{ + QIODevice *d = iio->ioDevice(); + QDataStream s( d ); + BMP_FILEHDR bf; + int startpos = d->at(); + + s.setByteOrder( QDataStream::LittleEndian );// Intel byte order + s >> bf; // read BMP file header + + if ( qstrncmp(bf.bfType,"BM",2) != 0 ) // not a BMP image + return; + + QImage image; + if (read_dib( s, bf.bfOffBits, startpos, image )) { + iio->setImage( image ); + iio->setStatus( 0 ); // image ok + } +} + +bool qt_write_dib( QDataStream& s, QImage image ) +{ + int nbits; + int bpl_bmp; + int bpl = image.bytesPerLine(); + + QIODevice* d = s.device(); + + if ( image.depth() == 8 && image.numColors() <= 16 ) { + bpl_bmp = (((bpl+1)/2+3)/4)*4; + nbits = 4; + } else if ( image.depth() == 32 ) { + bpl_bmp = ((image.width()*24+31)/32)*4; + nbits = 24; +#ifdef Q_WS_QWS + } else if ( image.depth() == 1 || image.depth() == 8 ) { + // Qt/E doesn't word align. + bpl_bmp = ((image.width()*image.depth()+31)/32)*4; + nbits = image.depth(); +#endif + } else { + bpl_bmp = bpl; + nbits = image.depth(); + } + + BMP_INFOHDR bi; + bi.biSize = BMP_WIN; // build info header + bi.biWidth = image.width(); + bi.biHeight = image.height(); + bi.biPlanes = 1; + bi.biBitCount = nbits; + bi.biCompression = BMP_RGB; + bi.biSizeImage = bpl_bmp*image.height(); + bi.biXPelsPerMeter = image.dotsPerMeterX() ? image.dotsPerMeterX() + : 2834; // 72 dpi default + bi.biYPelsPerMeter = image.dotsPerMeterY() ? image.dotsPerMeterY() : 2834; + bi.biClrUsed = image.numColors(); + bi.biClrImportant = image.numColors(); + s << bi; // write info header + + if ( image.depth() != 32 ) { // write color table + uchar *color_table = new uchar[4*image.numColors()]; + uchar *rgb = color_table; + QRgb *c = image.colorTable(); + for ( int i=0; i<image.numColors(); i++ ) { + *rgb++ = qBlue ( c[i] ); + *rgb++ = qGreen( c[i] ); + *rgb++ = qRed ( c[i] ); + *rgb++ = 0; + } + d->writeBlock( (char *)color_table, 4*image.numColors() ); + delete [] color_table; + } + + if ( image.depth() == 1 && image.bitOrder() != QImage::BigEndian ) + image = image.convertBitOrder( QImage::BigEndian ); + + int y; + + if ( nbits == 1 || nbits == 8 ) { // direct output +#ifdef Q_WS_QWS + // Qt/E doesn't word align. + int pad = bpl_bmp - bpl; + char padding[4]; +#endif + for ( y=image.height()-1; y>=0; y-- ) { + d->writeBlock( (char*)image.scanLine(y), bpl ); +#ifdef Q_WS_QWS + d->writeBlock( padding, pad ); +#endif + } + return TRUE; + } + + uchar *buf = new uchar[bpl_bmp]; + uchar *b, *end; + register uchar *p; + + memset( buf, 0, bpl_bmp ); + for ( y=image.height()-1; y>=0; y-- ) { // write the image bits + if ( nbits == 4 ) { // convert 8 -> 4 bits + p = image.scanLine(y); + b = buf; + end = b + image.width()/2; + while ( b < end ) { + *b++ = (*p << 4) | (*(p+1) & 0x0f); + p += 2; + } + if ( image.width() & 1 ) + *b = *p << 4; + } else { // 32 bits + QRgb *p = (QRgb *)image.scanLine( y ); + QRgb *end = p + image.width(); + b = buf; + while ( p < end ) { + *b++ = qBlue(*p); + *b++ = qGreen(*p); + *b++ = qRed(*p); + p++; + } + } + if ( bpl_bmp != d->writeBlock( (char*)buf, bpl_bmp ) ) { + delete[] buf; + return FALSE; + } + } + delete[] buf; + return TRUE; +} + + +static void write_bmp_image( QImageIO *iio ) +{ + QIODevice *d = iio->ioDevice(); + QImage image = iio->image(); + QDataStream s( d ); + BMP_FILEHDR bf; + int bpl_bmp; + int bpl = image.bytesPerLine(); + + // Code partially repeated in qt_write_dib + if ( image.depth() == 8 && image.numColors() <= 16 ) { + bpl_bmp = (((bpl+1)/2+3)/4)*4; + } else if ( image.depth() == 32 ) { + bpl_bmp = ((image.width()*24+31)/32)*4; + } else { + bpl_bmp = bpl; + } + + iio->setStatus( 0 ); + s.setByteOrder( QDataStream::LittleEndian );// Intel byte order + strncpy( bf.bfType, "BM", 2 ); // build file header + bf.bfReserved1 = bf.bfReserved2 = 0; // reserved, should be zero + bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.numColors()*4; + bf.bfSize = bf.bfOffBits + bpl_bmp*image.height(); + s << bf; // write file header + + if ( !qt_write_dib( s, image ) ) + iio->setStatus( 1 ); + +} + +#endif // QT_NO_IMAGEIO_BMP + +#ifndef QT_NO_IMAGEIO_PPM + +/***************************************************************************** + PBM/PGM/PPM (ASCII and RAW) image read/write functions + *****************************************************************************/ + +static int read_pbm_int( QIODevice *d ) +{ + int c; + int val = -1; + bool digit; + const int buflen = 100; + char buf[buflen]; + for ( ;; ) { + if ( (c=d->getch()) == EOF ) // end of file + break; + digit = isdigit( (uchar) c ); + if ( val != -1 ) { + if ( digit ) { + val = 10*val + c - '0'; + continue; + } else { + if ( c == '#' ) // comment + d->readLine( buf, buflen ); + break; + } + } + if ( digit ) // first digit + val = c - '0'; + else if ( isspace((uchar) c) ) + continue; + else if ( c == '#' ) + d->readLine( buf, buflen ); + else + break; + } + return val; +} + +static void read_pbm_image( QImageIO *iio ) // read PBM image data +{ + const int buflen = 300; + char buf[buflen]; + QIODevice *d = iio->ioDevice(); + int w, h, nbits, mcc, y; + int pbm_bpl; + char type; + bool raw; + QImage image; + + if ( d->readBlock( buf, 3 ) != 3 ) // read P[1-6]<white-space> + return; + if ( !(buf[0] == 'P' && isdigit((uchar) buf[1]) && isspace((uchar) buf[2])) ) + return; + switch ( (type=buf[1]) ) { + case '1': // ascii PBM + case '4': // raw PBM + nbits = 1; + break; + case '2': // ascii PGM + case '5': // raw PGM + nbits = 8; + break; + case '3': // ascii PPM + case '6': // raw PPM + nbits = 32; + break; + default: + return; + } + raw = type >= '4'; + w = read_pbm_int( d ); // get image width + h = read_pbm_int( d ); // get image height + if ( nbits == 1 ) + mcc = 1; // ignore max color component + else + mcc = read_pbm_int( d ); // get max color component + if ( w <= 0 || w > 32767 || h <= 0 || h > 32767 || mcc <= 0 ) + return; // weird P.M image + + int maxc = mcc; + if ( maxc > 255 ) + maxc = 255; + image.create( w, h, nbits, 0, + nbits == 1 ? QImage::BigEndian : QImage::IgnoreEndian ); + if ( image.isNull() ) + return; + + pbm_bpl = (nbits*w+7)/8; // bytes per scanline in PBM + + if ( raw ) { // read raw data + if ( nbits == 32 ) { // type 6 + pbm_bpl = 3*w; + uchar *buf24 = new uchar[pbm_bpl], *b; + QRgb *p; + QRgb *end; + for ( y=0; y<h; y++ ) { + if ( d->readBlock( (char *)buf24, pbm_bpl ) != pbm_bpl ) { + delete[] buf24; + return; + } + p = (QRgb *)image.scanLine( y ); + end = p + w; + b = buf24; + while ( p < end ) { + *p++ = qRgb(b[0],b[1],b[2]); + b += 3; + } + } + delete[] buf24; + } else { // type 4,5 + for ( y=0; y<h; y++ ) { + if ( d->readBlock( (char *)image.scanLine(y), pbm_bpl ) + != pbm_bpl ) + return; + } + } + } else { // read ascii data + register uchar *p; + int n; + for ( y=0; y<h; y++ ) { + p = image.scanLine( y ); + n = pbm_bpl; + if ( nbits == 1 ) { + int b; + while ( n-- ) { + b = 0; + for ( int i=0; i<8; i++ ) + b = (b << 1) | (read_pbm_int(d) & 1); + *p++ = b; + } + } else if ( nbits == 8 ) { + if ( mcc == maxc ) { + while ( n-- ) { + *p++ = read_pbm_int( d ); + } + } else { + while ( n-- ) { + *p++ = read_pbm_int( d ) * maxc / mcc; + } + } + } else { // 32 bits + n /= 4; + int r, g, b; + if ( mcc == maxc ) { + while ( n-- ) { + r = read_pbm_int( d ); + g = read_pbm_int( d ); + b = read_pbm_int( d ); + *((QRgb*)p) = qRgb( r, g, b ); + p += 4; + } + } else { + while ( n-- ) { + r = read_pbm_int( d ) * maxc / mcc; + g = read_pbm_int( d ) * maxc / mcc; + b = read_pbm_int( d ) * maxc / mcc; + *((QRgb*)p) = qRgb( r, g, b ); + p += 4; + } + } + } + } + } + + if ( nbits == 1 ) { // bitmap + image.setNumColors( 2 ); + image.setColor( 0, qRgb(255,255,255) ); // white + image.setColor( 1, qRgb(0,0,0) ); // black + } else if ( nbits == 8 ) { // graymap + image.setNumColors( maxc+1 ); + for ( int i=0; i<=maxc; i++ ) + image.setColor( i, qRgb(i*255/maxc,i*255/maxc,i*255/maxc) ); + } + + iio->setImage( image ); + iio->setStatus( 0 ); // image ok +} + + +static void write_pbm_image( QImageIO *iio ) +{ + QIODevice* out = iio->ioDevice(); + QCString str; + + QImage image = iio->image(); + QCString format = iio->format(); + format = format.left(3); // ignore RAW part + bool gray = format == "PGM"; + + if ( format == "PBM" ) { + image = image.convertDepth(1); + } else if ( image.depth() == 1 ) { + image = image.convertDepth(8); + } + + if ( image.depth() == 1 && image.numColors() == 2 ) { + if ( qGray(image.color(0)) < qGray(image.color(1)) ) { + // 0=dark/black, 1=light/white - invert + image.detach(); + for ( int y=0; y<image.height(); y++ ) { + uchar *p = image.scanLine(y); + uchar *end = p + image.bytesPerLine(); + while ( p < end ) + *p++ ^= 0xff; + } + } + } + + uint w = image.width(); + uint h = image.height(); + + str.sprintf("P\n%d %d\n", w, h); + + switch (image.depth()) { + case 1: { + str.insert(1, '4'); + if ((uint)out->writeBlock(str, str.length()) != str.length()) { + iio->setStatus(1); + return; + } + w = (w+7)/8; + for (uint y=0; y<h; y++) { + uchar* line = image.scanLine(y); + if ( w != (uint)out->writeBlock((char*)line, w) ) { + iio->setStatus(1); + return; + } + } + } + break; + + case 8: { + str.insert(1, gray ? '5' : '6'); + str.append("255\n"); + if ((uint)out->writeBlock(str, str.length()) != str.length()) { + iio->setStatus(1); + return; + } + QRgb *color = image.colorTable(); + uint bpl = w*(gray ? 1 : 3); + uchar *buf = new uchar[bpl]; + for (uint y=0; y<h; y++) { + uchar *b = image.scanLine(y); + uchar *p = buf; + uchar *end = buf+bpl; + if ( gray ) { + while ( p < end ) { + uchar g = (uchar)qGray(color[*b++]); + *p++ = g; + } + } else { + while ( p < end ) { + QRgb rgb = color[*b++]; + *p++ = qRed(rgb); + *p++ = qGreen(rgb); + *p++ = qBlue(rgb); + } + } + if ( bpl != (uint)out->writeBlock((char*)buf, bpl) ) { + iio->setStatus(1); + return; + } + } + delete [] buf; + } + break; + + case 32: { + str.insert(1, gray ? '5' : '6'); + str.append("255\n"); + if ((uint)out->writeBlock(str, str.length()) != str.length()) { + iio->setStatus(1); + return; + } + uint bpl = w*(gray ? 1 : 3); + uchar *buf = new uchar[bpl]; + for (uint y=0; y<h; y++) { + QRgb *b = (QRgb*)image.scanLine(y); + uchar *p = buf; + uchar *end = buf+bpl; + if ( gray ) { + while ( p < end ) { + uchar g = (uchar)qGray(*b++); + *p++ = g; + } + } else { + while ( p < end ) { + QRgb rgb = *b++; + *p++ = qRed(rgb); + *p++ = qGreen(rgb); + *p++ = qBlue(rgb); + } + } + if ( bpl != (uint)out->writeBlock((char*)buf, bpl) ) { + iio->setStatus(1); + return; + } + } + delete [] buf; + } + } + + iio->setStatus(0); +} + +#endif // QT_NO_IMAGEIO_PPM + +#ifndef QT_NO_ASYNC_IMAGE_IO + +class QImageIOFrameGrabber : public QImageConsumer { +public: + QImageIOFrameGrabber() : framecount(0) { } + + QImageDecoder *decoder; + int framecount; + + void changed(const QRect&) { } + void end() { } + void frameDone(const QPoint&, const QRect&) { framecount++; } + void frameDone() { framecount++; } + void setLooping(int) { } + void setFramePeriod(int) { } + void setSize(int, int) { } +}; + +static void read_async_image( QImageIO *iio ) +{ + const int buf_len = 2048; + uchar buffer[buf_len]; + QIODevice *d = iio->ioDevice(); + QImageIOFrameGrabber* consumer = new QImageIOFrameGrabber(); + QImageDecoder *decoder = new QImageDecoder(consumer); + consumer->decoder = decoder; + int startAt = d->at(); + int totLen = 0; + + for (;;) { + int length = d->readBlock((char*)buffer, buf_len); + if ( length <= 0 ) { + iio->setStatus(length); + break; + } + uchar* b = buffer; + int r = -1; + while (length > 0 && consumer->framecount==0) { + r = decoder->decode(b, length); + if ( r <= 0 ) break; + b += r; + totLen += r; + length -= r; + } + if ( consumer->framecount ) { + // Stopped after first frame + if ( d->isDirectAccess() ) + d->at( startAt + totLen ); + else { + // ### We have (probably) read too much from the stream into + // the buffer, and there is no way to put it back! + } + iio->setImage(decoder->image()); + iio->setStatus(0); + break; + } + if ( r <= 0 ) { + iio->setStatus(r); + break; + } + } + + consumer->decoder = 0; + delete decoder; + delete consumer; +} + +#endif // QT_NO_ASYNC_IMAGE_IO + +#ifndef QT_NO_IMAGEIO_XBM + +/***************************************************************************** + X bitmap image read/write functions + *****************************************************************************/ + +static inline int hex2byte( register char *p ) +{ + return ( (isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4 ) | + ( isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10 ); +} + +static void read_xbm_image( QImageIO *iio ) +{ + const int buflen = 300; + char buf[buflen]; + QRegExp r1, r2; + QIODevice *d = iio->ioDevice(); + int w=-1, h=-1; + QImage image; + + r1 = QString::fromLatin1("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+"); + r2 = QString::fromLatin1("[0-9]+"); + d->readLine( buf, buflen ); // "#define .._width <num>" + + while (!d->atEnd() && buf[0] != '#') //skip leading comment, if any + d->readLine( buf, buflen ); + + QString sbuf; + sbuf = QString::fromLatin1(buf); + + if ( r1.search(sbuf) == 0 && + r2.search(sbuf, r1.matchedLength()) == r1.matchedLength() ) + w = atoi( &buf[r1.matchedLength()] ); + + d->readLine( buf, buflen ); // "#define .._height <num>" + sbuf = QString::fromLatin1(buf); + + if ( r1.search(sbuf) == 0 && + r2.search(sbuf, r1.matchedLength()) == r1.matchedLength() ) + h = atoi( &buf[r1.matchedLength()] ); + + if ( w <= 0 || w > 32767 || h <= 0 || h > 32767 ) + return; // format error + + for ( ;; ) { // scan for data + if ( d->readLine(buf, buflen) <= 0 ) // end of file + return; + if ( strstr(buf,"0x") != 0 ) // does line contain data? + break; + } + + image.create( w, h, 1, 2, QImage::LittleEndian ); + if ( image.isNull() ) + return; + + image.setColor( 0, qRgb(255,255,255) ); // white + image.setColor( 1, qRgb(0,0,0) ); // black + + int x = 0, y = 0; + uchar *b = image.scanLine(0); + char *p = strstr( buf, "0x" ); + w = (w+7)/8; // byte width + + while ( y < h ) { // for all encoded bytes... + if ( p ) { // p = "0x.." + *b++ = hex2byte(p+2); + p += 2; + if ( ++x == w && ++y < h ) { + b = image.scanLine(y); + x = 0; + } + p = strstr( p, "0x" ); + } else { // read another line + if ( d->readLine(buf,buflen) <= 0 ) // EOF ==> truncated image + break; + p = strstr( buf, "0x" ); + } + } + + iio->setImage( image ); + iio->setStatus( 0 ); // image ok +} + + +static void write_xbm_image( QImageIO *iio ) +{ + QIODevice *d = iio->ioDevice(); + QImage image = iio->image(); + int w = image.width(); + int h = image.height(); + int i; + QString s = fbname(iio->fileName()); // get file base name + char *buf = new char[s.length() + 100]; + + sprintf( buf, "#define %s_width %d\n", s.ascii(), w ); + d->writeBlock( buf, qstrlen(buf) ); + sprintf( buf, "#define %s_height %d\n", s.ascii(), h ); + d->writeBlock( buf, qstrlen(buf) ); + sprintf( buf, "static char %s_bits[] = {\n ", s.ascii() ); + d->writeBlock( buf, qstrlen(buf) ); + + iio->setStatus( 0 ); + + if ( image.depth() != 1 ) + image = image.convertDepth( 1 ); // dither + if ( image.bitOrder() != QImage::LittleEndian ) + image = image.convertBitOrder( QImage::LittleEndian ); + + bool invert = qGray(image.color(0)) < qGray(image.color(1)); + char hexrep[16]; + for ( i=0; i<10; i++ ) + hexrep[i] = '0' + i; + for ( i=10; i<16; i++ ) + hexrep[i] = 'a' -10 + i; + if ( invert ) { + char t; + for ( i=0; i<8; i++ ) { + t = hexrep[15-i]; + hexrep[15-i] = hexrep[i]; + hexrep[i] = t; + } + } + int bcnt = 0; + register char *p = buf; + int bpl = (w+7)/8; + for (int y = 0; y < h; ++y) { + uchar *b = image.scanLine(y); + for (i = 0; i < bpl; ++i) { + *p++ = '0'; *p++ = 'x'; + *p++ = hexrep[*b >> 4]; + *p++ = hexrep[*b++ & 0xf]; + + if ( i < bpl - 1 || y < h - 1 ) { + *p++ = ','; + if ( ++bcnt > 14 ) { + *p++ = '\n'; + *p++ = ' '; + *p = '\0'; + if ( (int)qstrlen(buf) != d->writeBlock( buf, qstrlen(buf) ) ) { + iio->setStatus( 1 ); + delete [] buf; + return; + } + p = buf; + bcnt = 0; + } + } + } + } + strcpy( p, " };\n" ); + if ( (int)qstrlen(buf) != d->writeBlock( buf, qstrlen(buf) ) ) + iio->setStatus( 1 ); + delete [] buf; +} + +#endif // QT_NO_IMAGEIO_XBM + + +#ifndef QT_NO_IMAGEIO_XPM + +/***************************************************************************** + XPM image read/write functions + *****************************************************************************/ + + +// Skip until ", read until the next ", return the rest in *buf +// Returns FALSE on error, TRUE on success + +static bool read_xpm_string( QCString &buf, QIODevice *d, + const char * const *source, int &index ) +{ + if ( source ) { + buf = source[index++]; + return TRUE; + } + + if ( buf.size() < 69 ) //# just an approximation + buf.resize( 123 ); + + buf[0] = '\0'; + int c; + int i; + while ( (c=d->getch()) != EOF && c != '"' ) { } + if ( c == EOF ) { + return FALSE; + } + i = 0; + while ( (c=d->getch()) != EOF && c != '"' ) { + if ( i == (int)buf.size() ) + buf.resize( i*2+42 ); + buf[i++] = c; + } + if ( c == EOF ) { + return FALSE; + } + + if ( i == (int)buf.size() ) // always use a 0 terminator + buf.resize( i+1 ); + buf[i] = '\0'; + return TRUE; +} + + + +static int nextColorSpec(const QCString & buf) +{ + int i = buf.find(" c "); + if (i < 0) + i = buf.find(" g "); + if (i < 0) + i = buf.find(" g4 "); + if (i < 0) + i = buf.find(" m "); + if (i < 0) + i = buf.find(" s "); + return i; +} + +// +// INTERNAL +// +// Reads an .xpm from either the QImageIO or from the QString *. +// One of the two HAS to be 0, the other one is used. +// + +static void read_xpm_image_or_array( QImageIO * iio, const char * const * source, + QImage & image) +{ + QCString buf; + QIODevice *d = 0; + buf.resize( 200 ); + + int i, cpp, ncols, w, h, index = 0; + + if ( iio ) { + iio->setStatus( 1 ); + d = iio ? iio->ioDevice() : 0; + d->readLine( buf.data(), buf.size() ); // "/* XPM */" + QRegExp r( QString::fromLatin1("/\\*.XPM.\\*/") ); + if ( buf.find(r) == -1 ) + return; // bad magic + } else if ( !source ) { + return; + } + + if ( !read_xpm_string( buf, d, source, index ) ) + return; + + if ( sscanf( buf, "%d %d %d %d", &w, &h, &ncols, &cpp ) < 4 ) + return; // < 4 numbers parsed + + if ( cpp > 15 ) + return; + + if ( ncols > 256 ) { + image.create( w, h, 32 ); + } else { + image.create( w, h, 8, ncols ); + } + + if (image.isNull()) + return; + + QMap<QString, int> colorMap; + int currentColor; + + for( currentColor=0; currentColor < ncols; ++currentColor ) { + if ( !read_xpm_string( buf, d, source, index ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage: XPM color specification missing"); +#endif + return; + } + QString index; + index = buf.left( cpp ); + buf = buf.mid( cpp ).simplifyWhiteSpace().lower(); + buf.prepend( " " ); + i = nextColorSpec(buf); + if ( i < 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage: XPM color specification is missing: %s", buf.data()); +#endif + return; // no c/g/g4/m/s specification at all + } + buf = buf.mid( i+3 ); + // Strip any other colorspec + int end = nextColorSpec(buf); + if (end != -1) + buf.truncate(end); + buf = buf.stripWhiteSpace(); + if ( buf == "none" ) { + image.setAlphaBuffer( TRUE ); + int transparentColor = currentColor; + if ( image.depth() == 8 ) { + image.setColor( transparentColor, + RGB_MASK & qRgb(198,198,198) ); + colorMap.insert( index, transparentColor ); + } else { + QRgb rgb = RGB_MASK & qRgb(198,198,198); + colorMap.insert( index, rgb ); + } + } else { + if ( ((buf.length()-1) % 3) && (buf[0] == '#') ) { + buf.truncate (((buf.length()-1) / 4 * 3) + 1); // remove alpha channel left by imagemagick + } + QColor c( buf.data() ); + if ( image.depth() == 8 ) { + image.setColor( currentColor, 0xff000000 | c.rgb() ); + colorMap.insert( index, currentColor ); + } else { + QRgb rgb = 0xff000000 | c.rgb(); + colorMap.insert( index, rgb ); + } + } + } + + // Read pixels + for( int y=0; y<h; y++ ) { + if ( !read_xpm_string( buf, d, source, index ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QImage: XPM pixels missing on image line %d", y); +#endif + return; + } + if ( image.depth() == 8 ) { + uchar *p = image.scanLine(y); + uchar *d = (uchar *)buf.data(); + uchar *end = d + buf.length(); + int x; + if ( cpp == 1 ) { + char b[2]; + b[1] = '\0'; + for ( x=0; x<w && d<end; x++ ) { + b[0] = *d++; + *p++ = (uchar)colorMap[b]; + } + } else { + char b[16]; + b[cpp] = '\0'; + for ( x=0; x<w && d<end; x++ ) { + strncpy( b, (char *)d, cpp ); + *p++ = (uchar)colorMap[b]; + d += cpp; + } + } + } else { + QRgb *p = (QRgb*)image.scanLine(y); + uchar *d = (uchar *)buf.data(); + uchar *end = d + buf.length(); + int x; + char b[16]; + b[cpp] = '\0'; + for ( x=0; x<w && d<end; x++ ) { + strncpy( b, (char *)d, cpp ); + *p++ = (QRgb)colorMap[b]; + d += cpp; + } + } + } + if ( iio ) { + iio->setImage( image ); + iio->setStatus( 0 ); // image ok + } +} + + +static void read_xpm_image( QImageIO * iio ) +{ + QImage i; + (void)read_xpm_image_or_array( iio, 0, i ); + return; +} + + +static const char* xpm_color_name( int cpp, int index ) +{ + static char returnable[5]; + static const char code[] = ".#abcdefghijklmnopqrstuvwxyzABCD" + "EFGHIJKLMNOPQRSTUVWXYZ0123456789"; + // cpp is limited to 4 and index is limited to 64^cpp + if ( cpp > 1 ) { + if ( cpp > 2 ) { + if ( cpp > 3 ) { + returnable[3] = code[index % 64]; + index /= 64; + } else + returnable[3] = '\0'; + returnable[2] = code[index % 64]; + index /= 64; + } else + returnable[2] = '\0'; + // the following 4 lines are a joke! + if ( index == 0 ) + index = 64*44+21; + else if ( index == 64*44+21 ) + index = 0; + returnable[1] = code[index % 64]; + index /= 64; + } else + returnable[1] = '\0'; + returnable[0] = code[index]; + + return returnable; +} + + +// write XPM image data +static void write_xpm_image( QImageIO * iio ) +{ + if ( iio ) + iio->setStatus( 1 ); + else + return; + + // ### 8-bit case could be made faster + QImage image; + if ( iio->image().depth() != 32 ) + image = iio->image().convertDepth( 32 ); + else + image = iio->image(); + + QMap<QRgb, int> colorMap; + + int w = image.width(), h = image.height(), ncolors = 0; + int x, y; + + // build color table + for( y=0; y<h; y++ ) { + QRgb * yp = (QRgb *)image.scanLine( y ); + for( x=0; x<w; x++ ) { + QRgb color = *(yp + x); + if ( !colorMap.contains(color) ) + colorMap.insert( color, ncolors++ ); + } + } + + // number of 64-bit characters per pixel needed to encode all colors + int cpp = 1; + for ( int k = 64; ncolors > k; k *= 64 ) { + ++cpp; + // limit to 4 characters per pixel + // 64^4 colors is enough for a 4096x4096 image + if ( cpp > 4) + break; + } + + QString line; + + // write header + QTextStream s( iio->ioDevice() ); + s << "/* XPM */" << endl + << "static char *" << fbname(iio->fileName()) << "[]={" << endl + << "\"" << w << " " << h << " " << ncolors << " " << cpp << "\""; + + // write palette + QMap<QRgb, int>::Iterator c = colorMap.begin(); + while ( c != colorMap.end() ) { + QRgb color = c.key(); + if ( image.hasAlphaBuffer() && color == (color & RGB_MASK) ) + line.sprintf( "\"%s c None\"", + xpm_color_name(cpp, *c) ); + else + line.sprintf( "\"%s c #%02x%02x%02x\"", + xpm_color_name(cpp, *c), + qRed(color), + qGreen(color), + qBlue(color) ); + ++c; + s << "," << endl << line; + } + + // write pixels, limit to 4 characters per pixel + line.truncate( cpp*w ); + for( y=0; y<h; y++ ) { + QRgb * yp = (QRgb *) image.scanLine( y ); + int cc = 0; + for( x=0; x<w; x++ ) { + int color = (int)(*(yp + x)); + QCString chars = xpm_color_name( cpp, colorMap[color] ); + line[cc++] = chars[0]; + if ( cpp > 1 ) { + line[cc++] = chars[1]; + if ( cpp > 2 ) { + line[cc++] = chars[2]; + if ( cpp > 3 ) { + line[cc++] = chars[3]; + } + } + } + } + s << "," << endl << "\"" << line << "\""; + } + s << "};" << endl; + + iio->setStatus( 0 ); +} + +#endif // QT_NO_IMAGEIO_XPM + +/*! + Returns an image with depth \a d, using the \a palette_count + colors pointed to by \a palette. If \a d is 1 or 8, the returned + image will have its color table ordered the same as \a palette. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + Note: currently no closest-color search is made. If colors are + found that are not in the palette, the palette may not be used at + all. This result should not be considered valid because it may + change in future implementations. + + Currently inefficient for non-32-bit images. + + \sa Qt::ImageConversionFlags +*/ +#ifndef QT_NO_IMAGE_TRUECOLOR +QImage QImage::convertDepthWithPalette( int d, QRgb* palette, int palette_count, int conversion_flags ) const +{ + if ( depth() == 1 ) { + return convertDepth( 8, conversion_flags ) + .convertDepthWithPalette( d, palette, palette_count, conversion_flags ); + } else if ( depth() == 8 ) { + // ### this could be easily made more efficient + return convertDepth( 32, conversion_flags ) + .convertDepthWithPalette( d, palette, palette_count, conversion_flags ); + } else { + QImage result; + convert_32_to_8( this, &result, + (conversion_flags&~Qt::DitherMode_Mask) | Qt::AvoidDither, + palette, palette_count ); + return result.convertDepth( d ); + } +} +#endif +static +bool +haveSamePalette(const QImage& a, const QImage& b) +{ + if (a.depth() != b.depth()) return FALSE; + if (a.numColors() != b.numColors()) return FALSE; + QRgb* ca = a.colorTable(); + QRgb* cb = b.colorTable(); + for (int i=a.numColors(); i--; ) { + if (*ca++ != *cb++) return FALSE; + } + return TRUE; +} + +/*! + \relates QImage + + Copies a block of pixels from \a src to \a dst. The pixels + copied from source (src) are converted according to + \a conversion_flags if it is incompatible with the destination + (\a dst). + + \a sx, \a sy is the top-left pixel in \a src, \a dx, \a dy + is the top-left position in \a dst and \a sw, \a sh is the + size of the copied block. + + The copying is clipped if areas outside \a src or \a dst are + specified. + + If \a sw is -1, it is adjusted to src->width(). Similarly, if \a + sh is -1, it is adjusted to src->height(). + + Currently inefficient for non 32-bit images. +*/ +void bitBlt( QImage* dst, int dx, int dy, const QImage* src, + int sx, int sy, int sw, int sh, int conversion_flags ) +{ + // Parameter correction + if ( sw < 0 ) sw = src->width(); + if ( sh < 0 ) sh = src->height(); + if ( sx < 0 ) { dx -= sx; sw += sx; sx = 0; } + if ( sy < 0 ) { dy -= sy; sh += sy; sy = 0; } + if ( dx < 0 ) { sx -= dx; sw += dx; dx = 0; } + if ( dy < 0 ) { sy -= dy; sh += dy; dy = 0; } + if ( sx + sw > src->width() ) sw = src->width() - sx; + if ( sy + sh > src->height() ) sh = src->height() - sy; + if ( dx + sw > dst->width() ) sw = dst->width() - dx; + if ( dy + sh > dst->height() ) sh = dst->height() - dy; + if ( sw <= 0 || sh <= 0 ) return; // Nothing left to copy + if ( (dst->data == src->data) && dx==sx && dy==sy ) return; // Same pixels + + // "Easy" to copy if both same depth and one of: + // - 32 bit + // - 8 bit, identical palette + // - 1 bit, identical palette and byte-aligned area + // + if ( haveSamePalette(*dst,*src) + && ( dst->depth() != 1 || + !( (dx&7) || (sx&7) || + ((sw&7) && (sx+sw < src->width()) || + (dx+sw < dst->width()) ) ) ) ) + { + // easy to copy + } else if ( dst->depth() != 32 ) { +#ifndef QT_NO_IMAGE_TRUECOLOR + + QImage dstconv = dst->convertDepth( 32 ); + bitBlt( &dstconv, dx, dy, src, sx, sy, sw, sh, + (conversion_flags&~Qt::DitherMode_Mask) | Qt::AvoidDither ); + *dst = dstconv.convertDepthWithPalette( dst->depth(), + dst->colorTable(), dst->numColors() ); +#endif + return; + } + + // Now assume palette can be ignored + + if ( dst->depth() != src->depth() ) { + if ( sw == src->width() && sh == src->height() || dst->depth()==32 ) { + QImage srcconv = src->convertDepth( dst->depth(), conversion_flags ); + bitBlt( dst, dx, dy, &srcconv, sx, sy, sw, sh, conversion_flags ); + } else { + QImage srcconv = src->copy( sx, sy, sw, sh ); // ie. bitBlt + bitBlt( dst, dx, dy, &srcconv, 0, 0, sw, sh, conversion_flags ); + } + return; + } + + // Now assume both are the same depth. + + // Now assume both are 32-bit or 8-bit with compatible palettes. + + // "Easy" + + switch ( dst->depth() ) { + case 1: + { + uchar* d = dst->scanLine(dy) + dx/8; + uchar* s = src->scanLine(sy) + sx/8; + const int bw = (sw+7)/8; + if ( bw < 64 ) { + // Trust ourselves + const int dd = dst->bytesPerLine() - bw; + const int ds = src->bytesPerLine() - bw; + while ( sh-- ) { + for ( int t=bw; t--; ) + *d++ = *s++; + d += dd; + s += ds; + } + } else { + // Trust libc + const int dd = dst->bytesPerLine(); + const int ds = src->bytesPerLine(); + while ( sh-- ) { + memcpy( d, s, bw ); + d += dd; + s += ds; + } + } + } + break; + case 8: + { + uchar* d = dst->scanLine(dy) + dx; + uchar* s = src->scanLine(sy) + sx; + if ( sw < 64 ) { + // Trust ourselves + const int dd = dst->bytesPerLine() - sw; + const int ds = src->bytesPerLine() - sw; + while ( sh-- ) { + for ( int t=sw; t--; ) + *d++ = *s++; + d += dd; + s += ds; + } + } else { + // Trust libc + const int dd = dst->bytesPerLine(); + const int ds = src->bytesPerLine(); + while ( sh-- ) { + memcpy( d, s, sw ); + d += dd; + s += ds; + } + } + } + break; +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: + if ( src->hasAlphaBuffer() ) { + QRgb* d = (QRgb*)dst->scanLine(dy) + dx; + QRgb* s = (QRgb*)src->scanLine(sy) + sx; + const int dd = dst->width() - sw; + const int ds = src->width() - sw; + while ( sh-- ) { + for ( int t=sw; t--; ) { + unsigned char a = qAlpha(*s); + if ( a == 255 ) + *d++ = *s++; + else if ( a == 0 ) + ++d,++s; // nothing + else { + unsigned char r = ((qRed(*s)-qRed(*d)) * a) / 256 + qRed(*d); + unsigned char g = ((qGreen(*s)-qGreen(*d)) * a) / 256 + qGreen(*d); + unsigned char b = ((qBlue(*s)-qBlue(*d)) * a) / 256 + qBlue(*d); + a = QMAX(qAlpha(*d),a); // alternatives... + *d++ = qRgba(r,g,b,a); + ++s; + } + } + d += dd; + s += ds; + } + } else { + QRgb* d = (QRgb*)dst->scanLine(dy) + dx; + QRgb* s = (QRgb*)src->scanLine(sy) + sx; + if ( sw < 64 ) { + // Trust ourselves + const int dd = dst->width() - sw; + const int ds = src->width() - sw; + while ( sh-- ) { + for ( int t=sw; t--; ) + *d++ = *s++; + d += dd; + s += ds; + } + } else { + // Trust libc + const int dd = dst->width(); + const int ds = src->width(); + const int b = sw*sizeof(QRgb); + while ( sh-- ) { + memcpy( d, s, b ); + d += dd; + s += ds; + } + } + } + break; +#endif // QT_NO_IMAGE_TRUECOLOR + } +} + + +/*! + Returns TRUE if this image and image \a i have the same contents; + otherwise returns FALSE. The comparison can be slow, unless there + is some obvious difference, such as different widths, in which + case the function will return quickly. + + \sa operator=() +*/ + +bool QImage::operator==( const QImage & i ) const +{ + // same object, or shared? + if ( i.data == data ) + return TRUE; + // obviously different stuff? + if ( i.data->h != data->h || + i.data->w != data->w ) + return FALSE; + // not equal if one has alphabuffer and the other does not + if ( i.hasAlphaBuffer() != hasAlphaBuffer() ) + return FALSE; + // that was the fast bit... + QImage i1 = convertDepth( 32 ); + QImage i2 = i.convertDepth( 32 ); + int l; + // if no alpha buffer used, there might still be junk in the + // alpha bits; thus, we can't do memcmp-style comparison of scanlines + if ( !hasAlphaBuffer() ) { + int m; + QRgb *i1line; + QRgb *i2line; + for( l=0; l < data->h; l++ ) { + i1line = (uint *)i1.scanLine( l ); + i2line = (uint *)i2.scanLine( l ); + // compare pixels of scanline individually + for ( m=0; m < data->w; m++ ) + if ( (i1line[m] ^ i2line[m]) & 0x00FFFFFF ) + return FALSE; + } + } else { + // yay, we can do fast binary comparison on entire scanlines + for( l=0; l < data->h; l++ ) + if ( memcmp( i1.scanLine( l ), i2.scanLine( l ), 4*data->w ) ) + return FALSE; + } + return TRUE; +} + + +/*! + Returns TRUE if this image and image \a i have different contents; + otherwise returns FALSE. The comparison can be slow, unless there + is some obvious difference, such as different widths, in which + case the function will return quickly. + + \sa operator=() +*/ + +bool QImage::operator!=( const QImage & i ) const +{ + return !(*this == i); +} + + + + +/*! + \fn int QImage::dotsPerMeterX() const + + Returns the number of pixels that fit horizontally in a physical + meter. This and dotsPerMeterY() define the intended scale and + aspect ratio of the image. + + \sa setDotsPerMeterX() +*/ + +/*! + \fn int QImage::dotsPerMeterY() const + + Returns the number of pixels that fit vertically in a physical + meter. This and dotsPerMeterX() define the intended scale and + aspect ratio of the image. + + \sa setDotsPerMeterY() +*/ + +/*! + Sets the value returned by dotsPerMeterX() to \a x. +*/ +void QImage::setDotsPerMeterX(int x) +{ + data->dpmx = x; +} + +/*! + Sets the value returned by dotsPerMeterY() to \a y. +*/ +void QImage::setDotsPerMeterY(int y) +{ + data->dpmy = y; +} + +/*! + \fn QPoint QImage::offset() const + + Returns the number of pixels by which the image is intended to be + offset by when positioning relative to other images. +*/ + +/*! + Sets the value returned by offset() to \a p. +*/ +void QImage::setOffset(const QPoint& p) +{ + data->offset = p; +} +#ifndef QT_NO_IMAGE_TEXT +/*! + \internal + + Returns the internal QImageDataMisc object. This object will be + created if it doesn't already exist. +*/ +QImageDataMisc& QImage::misc() const +{ + if ( !data->misc ) + data->misc = new QImageDataMisc; + return *data->misc; +} + +/*! + Returns the string recorded for the keyword \a key in language \a + lang, or in a default language if \a lang is 0. +*/ +QString QImage::text(const char* key, const char* lang) const +{ + QImageTextKeyLang x(key,lang); + return misc().text_lang[x]; +} + +/*! + \overload + + Returns the string recorded for the keyword and language \a kl. +*/ +QString QImage::text(const QImageTextKeyLang& kl) const +{ + return misc().text_lang[kl]; +} + +/*! + Returns the language identifiers for which some texts are + recorded. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QStringList list = myImage.textLanguages(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa textList() text() setText() textKeys() +*/ +QStringList QImage::textLanguages() const +{ + if ( !data->misc ) + return QStringList(); + return misc().languages(); +} + +/*! + Returns the keywords for which some texts are recorded. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QStringList list = myImage.textKeys(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa textList() text() setText() textLanguages() +*/ +QStringList QImage::textKeys() const +{ + if ( !data->misc ) + return QStringList(); + return misc().keys(); +} + +/*! + Returns a list of QImageTextKeyLang objects that enumerate all the + texts key/language pairs set by setText() for this image. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QValueList<QImageTextKeyLang> list = myImage.textList(); + QValueList<QImageTextKeyLang>::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode +*/ +QValueList<QImageTextKeyLang> QImage::textList() const +{ + if ( !data->misc ) + return QValueList<QImageTextKeyLang>(); + return misc().list(); +} + +/*! + Records string \a s for the keyword \a key. The \a key should be a + portable keyword recognizable by other software - some suggested + values can be found in \link + http://www.libpng.org/pub/png/spec/1.2/png-1.2-pdg.html#C.Anc-text + the PNG specification \endlink. \a s can be any text. \a lang + should specify the language code (see + \link http://www.rfc-editor.org/rfc/rfc1766.txt RFC 1766 \endlink) or 0. +*/ +void QImage::setText(const char* key, const char* lang, const QString& s) +{ + QImageTextKeyLang x(key,lang); + misc().text_lang.replace(x,s); +} + +#endif // QT_NO_IMAGE_TEXT + +#ifdef Q_WS_QWS +/*! + \internal +*/ +QGfx * QImage::graphicsContext() +{ + QGfx * ret=0; + if(depth()) { + int w = qt_screen->mapToDevice( QSize(width(),height()) ).width(); + int h = qt_screen->mapToDevice( QSize(width(),height()) ).height(); + ret=QGfx::createGfx(depth(),bits(),w,h,bytesPerLine()); + } else { + qDebug("Trying to create image for null depth"); + return 0; + } + if(depth()<=8) { + QRgb * tmp=colorTable(); + int nc=numColors(); + if(tmp==0) { + static QRgb table[2] = { qRgb(255,255,255), qRgb(0,0,0) }; + tmp=table; + nc=2; + } + ret->setClut(tmp,nc); + } + return ret; +} + +#endif diff --git a/src/kernel/qimage.h b/src/kernel/qimage.h new file mode 100644 index 0000000..1ade054 --- /dev/null +++ b/src/kernel/qimage.h @@ -0,0 +1,425 @@ +/**************************************************************************** +** +** Definition of QImage and QImageIO classes +** +** Created : 950207 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QIMAGE_H +#define QIMAGE_H + +#ifndef QT_H +#include "qpixmap.h" +#include "qstrlist.h" +#include "qstringlist.h" +#endif // QT_H + +class QImageDataMisc; // internal +#ifndef QT_NO_IMAGE_TEXT +class Q_EXPORT QImageTextKeyLang { +public: + QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { } + QImageTextKeyLang() { } + + QCString key; + QCString lang; + + bool operator< (const QImageTextKeyLang& other) const + { return key < other.key || key==other.key && lang < other.lang; } + bool operator== (const QImageTextKeyLang& other) const + { return key==other.key && lang==other.lang; } +}; +#endif //QT_NO_IMAGE_TEXT + + +class Q_EXPORT QImage +{ +public: + enum Endian { IgnoreEndian, BigEndian, LittleEndian }; + + QImage(); + QImage( int width, int height, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); + QImage( const QSize&, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); +#ifndef QT_NO_IMAGEIO + QImage( const QString &fileName, const char* format=0 ); + QImage( const char * const xpm[] ); + QImage( const QByteArray &data ); +#endif + QImage( uchar* data, int w, int h, int depth, + QRgb* colortable, int numColors, + Endian bitOrder ); +#ifdef Q_WS_QWS + QImage( uchar* data, int w, int h, int depth, int pbl, + QRgb* colortable, int numColors, + Endian bitOrder ); +#endif + QImage( const QImage & ); + ~QImage(); + + QImage &operator=( const QImage & ); + QImage &operator=( const QPixmap & ); + bool operator==( const QImage & ) const; + bool operator!=( const QImage & ) const; + void detach(); + QImage copy() const; + QImage copy(int x, int y, int w, int h, int conversion_flags=0) const; + QImage copy(const QRect&) const; +#ifndef QT_NO_MIME + static QImage fromMimeSource( const QString& abs_name ); +#endif + bool isNull() const { return data->bits == 0; } + + int width() const { return data->w; } + int height() const { return data->h; } + QSize size() const { return QSize(data->w,data->h); } + QRect rect() const { return QRect(0,0,data->w,data->h); } + int depth() const { return data->d; } + int numColors() const { return data->ncols; } + Endian bitOrder() const { return (Endian) data->bitordr; } + + QRgb color( int i ) const; + void setColor( int i, QRgb c ); + void setNumColors( int ); + + bool hasAlphaBuffer() const; + void setAlphaBuffer( bool ); + + bool allGray() const; + bool isGrayscale() const; + + uchar *bits() const; + uchar *scanLine( int ) const; + uchar **jumpTable() const; + QRgb *colorTable() const; + int numBytes() const; + int bytesPerLine() const; + +#ifdef Q_WS_QWS + QGfx * graphicsContext(); +#endif + + bool create( int width, int height, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); + bool create( const QSize&, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); + void reset(); + + void fill( uint pixel ); + void invertPixels( bool invertAlpha = TRUE ); + + QImage convertDepth( int ) const; +#ifndef QT_NO_IMAGE_TRUECOLOR + QImage convertDepthWithPalette( int, QRgb* p, int pc, int cf=0 ) const; +#endif + QImage convertDepth( int, int conversion_flags ) const; + QImage convertBitOrder( Endian ) const; + + enum ScaleMode { + ScaleFree, + ScaleMin, + ScaleMax + }; +#ifndef QT_NO_IMAGE_SMOOTHSCALE + QImage smoothScale( int w, int h, ScaleMode mode=ScaleFree ) const; + QImage smoothScale( const QSize& s, ScaleMode mode=ScaleFree ) const; +#endif +#ifndef QT_NO_IMAGE_TRANSFORMATION + QImage scale( int w, int h, ScaleMode mode=ScaleFree ) const; + QImage scale( const QSize& s, ScaleMode mode=ScaleFree ) const; + QImage scaleWidth( int w ) const; + QImage scaleHeight( int h ) const; + QImage xForm( const QWMatrix &matrix ) const; +#endif + +#ifndef QT_NO_IMAGE_DITHER_TO_1 + QImage createAlphaMask( int conversion_flags=0 ) const; +#endif +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + QImage createHeuristicMask( bool clipTight=TRUE ) const; +#endif +#ifndef QT_NO_IMAGE_MIRROR + QImage mirror() const; + QImage mirror(bool horizontally, bool vertically) const; +#endif + QImage swapRGB() const; + + static Endian systemBitOrder(); + static Endian systemByteOrder(); + +#ifndef QT_NO_IMAGEIO + static const char* imageFormat( const QString &fileName ); + static QStrList inputFormats(); + static QStrList outputFormats(); +#ifndef QT_NO_STRINGLIST + static QStringList inputFormatList(); + static QStringList outputFormatList(); +#endif + bool load( const QString &fileName, const char* format=0 ); + bool loadFromData( const uchar *buf, uint len, + const char *format=0 ); + bool loadFromData( QByteArray data, const char* format=0 ); + bool save( const QString &fileName, const char* format, + int quality=-1 ) const; + bool save( QIODevice * device, const char* format, + int quality=-1 ) const; +#endif //QT_NO_IMAGEIO + + bool valid( int x, int y ) const; + int pixelIndex( int x, int y ) const; + QRgb pixel( int x, int y ) const; + void setPixel( int x, int y, uint index_or_rgb ); + + // Auxiliary data + int dotsPerMeterX() const; + int dotsPerMeterY() const; + void setDotsPerMeterX(int); + void setDotsPerMeterY(int); + QPoint offset() const; + void setOffset(const QPoint&); +#ifndef QT_NO_IMAGE_TEXT + QValueList<QImageTextKeyLang> textList() const; + QStringList textLanguages() const; + QStringList textKeys() const; + QString text(const char* key, const char* lang=0) const; + QString text(const QImageTextKeyLang&) const; + void setText(const char* key, const char* lang, const QString&); +#endif +private: + void init(); + void reinit(); + void freeBits(); + static void warningIndexRange( const char *, int ); + + struct QImageData : public QShared { // internal image data + int w; // image width + int h; // image height + int d; // image depth + int ncols; // number of colors + int nbytes; // number of bytes data + int bitordr; // bit order (1 bit depth) + QRgb *ctbl; // color table + uchar **bits; // image data + bool alpha; // alpha buffer + int dpmx; // dots per meter X (or 0) + int dpmy; // dots per meter Y (or 0) + QPoint offset; // offset in pixels +#ifndef QT_NO_IMAGE_TEXT + QImageDataMisc* misc; // less common stuff +#endif + bool ctbl_mine; // this allocated ctbl + } *data; +#ifndef QT_NO_IMAGE_TEXT + QImageDataMisc& misc() const; +#endif +#ifndef QT_NO_IMAGEIO + bool doImageIO( QImageIO* io, int quality ) const; +#endif + friend Q_EXPORT void bitBlt( QImage* dst, int dx, int dy, + const QImage* src, int sx, int sy, + int sw, int sh, int conversion_flags ); +}; + + +// QImage stream functions + +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +Q_EXPORT QDataStream &operator<<( QDataStream &, const QImage & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QImage & ); +#endif + +#ifndef QT_NO_IMAGEIO +class QIODevice; +typedef void (*image_io_handler)( QImageIO * ); // image IO handler + + +struct QImageIOData; + + +class Q_EXPORT QImageIO +{ +public: + QImageIO(); + QImageIO( QIODevice *ioDevice, const char *format ); + QImageIO( const QString &fileName, const char* format ); + ~QImageIO(); + + + const QImage &image() const { return im; } + int status() const { return iostat; } + const char *format() const { return frmt; } + QIODevice *ioDevice() const { return iodev; } + QString fileName() const { return fname; } + int quality() const; + QString description() const { return descr; } + const char *parameters() const; + float gamma() const; + + void setImage( const QImage & ); + void setStatus( int ); + void setFormat( const char * ); + void setIODevice( QIODevice * ); + void setFileName( const QString & ); + void setQuality( int ); + void setDescription( const QString & ); + void setParameters( const char * ); + void setGamma( float ); + + bool read(); + bool write(); + + static const char* imageFormat( const QString &fileName ); + static const char *imageFormat( QIODevice * ); + static QStrList inputFormats(); + static QStrList outputFormats(); + + static void defineIOHandler( const char *format, + const char *header, + const char *flags, + image_io_handler read_image, + image_io_handler write_image ); + +private: + void init(); + + QImage im; // image + int iostat; // IO status + QCString frmt; // image format + QIODevice *iodev; // IO device + QString fname; // file name + char *params; // image parameters //### change to QImageIOData *d in 3.0 + QString descr; // image description + QImageIOData *d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QImageIO( const QImageIO & ); + QImageIO &operator=( const QImageIO & ); +#endif +}; + +#endif //QT_NO_IMAGEIO + +Q_EXPORT void bitBlt( QImage* dst, int dx, int dy, const QImage* src, + int sx=0, int sy=0, int sw=-1, int sh=-1, + int conversion_flags=0 ); + + +/***************************************************************************** + QImage member functions + *****************************************************************************/ + +inline bool QImage::hasAlphaBuffer() const +{ + return data->alpha; +} + +inline uchar *QImage::bits() const +{ + return data->bits ? data->bits[0] : 0; +} + +inline uchar **QImage::jumpTable() const +{ + return data->bits; +} + +inline QRgb *QImage::colorTable() const +{ + return data->ctbl; +} + +inline int QImage::numBytes() const +{ + return data->nbytes; +} + +inline int QImage::bytesPerLine() const +{ + return data->h ? data->nbytes/data->h : 0; +} + +inline QImage QImage::copy(const QRect& r) const +{ + return copy(r.x(), r.y(), r.width(), r.height()); +} + +inline QRgb QImage::color( int i ) const +{ +#if defined(QT_CHECK_RANGE) + if ( i >= data->ncols ) + warningIndexRange( "color", i ); +#endif + return data->ctbl ? data->ctbl[i] : (QRgb)-1; +} + +inline void QImage::setColor( int i, QRgb c ) +{ +#if defined(QT_CHECK_RANGE) + if ( i >= data->ncols ) + warningIndexRange( "setColor", i ); +#endif + if ( data->ctbl ) + data->ctbl[i] = c; +} + +inline uchar *QImage::scanLine( int i ) const +{ +#if defined(QT_CHECK_RANGE) + if ( i >= data->h ) + warningIndexRange( "scanLine", i ); +#endif + return data->bits ? data->bits[i] : 0; +} + +inline int QImage::dotsPerMeterX() const +{ + return data->dpmx; +} + +inline int QImage::dotsPerMeterY() const +{ + return data->dpmy; +} + +inline QPoint QImage::offset() const +{ + return data->offset; +} + + +#endif // QIMAGE_H diff --git a/src/kernel/qimageformatinterface_p.h b/src/kernel/qimageformatinterface_p.h new file mode 100644 index 0000000..3b7fe17 --- /dev/null +++ b/src/kernel/qimageformatinterface_p.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QIMAGEFORMATINTERFACE_P_H +#define QIMAGEFORMATINTERFACE_P_H + +#ifndef QT_H +#include <private/qcom_p.h> +#endif // QT_H + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_NO_COMPONENT + +// {04903F05-54B1-4726-A849-FB5CB097CA87} +#ifndef IID_QImageFormat +#define IID_QImageFormat QUuid( 0x04903f05, 0x54b1, 0x4726, 0xa8, 0x49, 0xfb, 0x5c, 0xb0, 0x97, 0xca, 0x87 ) +#endif + +class QImage; + +struct Q_EXPORT QImageFormatInterface : public QFeatureListInterface +{ + virtual QRESULT loadImage( const QString &format, const QString &filename, QImage * ) = 0; + virtual QRESULT saveImage( const QString &format, const QString &filename, const QImage & ) = 0; + + virtual QRESULT installIOHandler( const QString & ) = 0; +}; + +#endif // QT_NO_COMPONENT + +#endif // QIMAGEFORMATINTERFACE_P_H diff --git a/src/kernel/qimageformatplugin.cpp b/src/kernel/qimageformatplugin.cpp new file mode 100644 index 0000000..ec586d4 --- /dev/null +++ b/src/kernel/qimageformatplugin.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 2001-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qimageformatplugin.h" +#ifndef QT_NO_IMAGEFORMATPLUGIN +#include "qimageformatinterface_p.h" +#include "qimage.h" + +/*! + \class QImageFormatPlugin qimageformatplugin.h + \brief The QImageFormatPlugin class provides an abstract base for custom image format plugins. + + \ingroup plugins + + The image format plugin is a simple plugin interface that makes + it easy to create custom image formats that can be used + transparently by applications. + + Writing an image format plugin is achieved by subclassing this + base class, reimplementing the pure virtual functions keys() and + installIOHandler(), and exporting the class with the + Q_EXPORT_PLUGIN macro. See the \link plugins-howto.html Plugins + documentation\endlink for details. +*/ + +/*! + \fn QStringList QImageFormatPlugin::keys() const + + Returns the list of image formats this plugin supports. + + \sa installIOHandler() +*/ + + +/*! + \fn bool QImageFormatPlugin::installIOHandler( const QString &format ) + + Installs a QImageIO image I/O handler for the image format \a + format. + + \sa keys() +*/ + +class QImageFormatPluginPrivate : public QImageFormatInterface +{ +public: + QImageFormatPluginPrivate( QImageFormatPlugin *p ) + : plugin( p ) + { + } + virtual ~QImageFormatPluginPrivate(); + + QRESULT queryInterface( const QUuid &iid, QUnknownInterface **iface ); + Q_REFCOUNT; + + QStringList featureList() const; + + QRESULT loadImage( const QString &format, const QString &filename, QImage * ); + QRESULT saveImage( const QString &format, const QString &filename, const QImage & ); + + QRESULT installIOHandler( const QString & ); + +private: + QImageFormatPlugin *plugin; +}; + +QImageFormatPluginPrivate::~QImageFormatPluginPrivate() +{ + delete plugin; +} + +QRESULT QImageFormatPluginPrivate::queryInterface( const QUuid &iid, QUnknownInterface **iface ) +{ + *iface = 0; + + if ( iid == IID_QUnknown ) + *iface = this; + else if ( iid == IID_QFeatureList ) + *iface = this; + else if ( iid == IID_QImageFormat ) + *iface = this; + else + return QE_NOINTERFACE; + + (*iface)->addRef(); + return QS_OK; +} + +QStringList QImageFormatPluginPrivate::featureList() const +{ + return plugin->keys(); +} + +QRESULT QImageFormatPluginPrivate::loadImage( const QString &format, const QString &filename, QImage *image ) +{ + return plugin->loadImage( format, filename, image ) ? QS_FALSE : QS_OK; +} + +QRESULT QImageFormatPluginPrivate::saveImage( const QString &format, const QString &filename, const QImage &image ) +{ + return plugin->saveImage( format, filename, image ) ? QS_FALSE : QS_OK; +} + +QRESULT QImageFormatPluginPrivate::installIOHandler( const QString &format ) +{ + return plugin->installIOHandler( format ) ? QS_FALSE : QS_OK; +} + +/*! + Constructs an image format plugin. This is invoked automatically + by the Q_EXPORT_PLUGIN macro. +*/ +QImageFormatPlugin::QImageFormatPlugin() + : QGPlugin( d = new QImageFormatPluginPrivate( this ) ) +{ +} + +/*! + Destroys the image format plugin. + + You never have to call this explicitly. Qt destroys a plugin + automatically when it is no longer used. +*/ +QImageFormatPlugin::~QImageFormatPlugin() +{ +} + + +/*!\internal + */ +bool QImageFormatPlugin::loadImage( const QString &format, const QString &filename, QImage *image ) +{ + Q_UNUSED( format ) + Q_UNUSED( filename ) + Q_UNUSED( image ) + return FALSE; +} + +/*! \internal + */ +bool QImageFormatPlugin::saveImage( const QString &format, const QString &filename, const QImage &image ) +{ + Q_UNUSED( format ) + Q_UNUSED( filename ) + Q_UNUSED( image ) + return FALSE; +} + +#endif // QT_NO_IMAGEFORMATPLUGIN diff --git a/src/kernel/qimageformatplugin.h b/src/kernel/qimageformatplugin.h new file mode 100644 index 0000000..ea11e3d --- /dev/null +++ b/src/kernel/qimageformatplugin.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Definition of ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QIMAGEFORMATPLUGIN_H +#define QIMAGEFORMATPLUGIN_H + +#ifndef QT_H +#include "qgplugin.h" +#include "qstringlist.h" +#endif // QT_H + +#ifndef QT_NO_IMAGEFORMATPLUGIN +class QImageFormat; +class QImageFormatPluginPrivate; + +class Q_EXPORT QImageFormatPlugin : public QGPlugin +{ + Q_OBJECT +public: + QImageFormatPlugin(); + ~QImageFormatPlugin(); + + virtual QStringList keys() const = 0; + virtual bool loadImage( const QString &format, const QString &filename, QImage *image ); + virtual bool saveImage( const QString &format, const QString &filename, const QImage &image ); + virtual bool installIOHandler( const QString &format ) = 0; + +private: + QImageFormatPluginPrivate *d; +}; +#endif // QT_NO_IMAGEFORMATPLUGIN +#endif // QIMAGEFORMATPLUGIN_H diff --git a/src/kernel/qinputcontext.cpp b/src/kernel/qinputcontext.cpp new file mode 100644 index 0000000..5433ae4 --- /dev/null +++ b/src/kernel/qinputcontext.cpp @@ -0,0 +1,856 @@ +/**************************************************************************** +** $Id: qinputcontext.cpp,v 1.6 2004/06/22 06:47:30 daisuke Exp $ +** +** Implementation of QInputContext class +** +** Copyright (C) 2000-2003 Trolltech AS. All rights reserved. +** +** This file is part of the kernel module of the Qt GUI Toolkit. +** +** This file may be distributed under the terms of the Q Public License +** as defined by Trolltech AS of Norway and appearing in the file +** LICENSE.QPL included in the packaging of this file. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition +** licenses for Unix/X11 may use this file in accordance with the Qt Commercial +** License Agreement provided with the Software. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for +** information about Qt Commercial License Agreements. +** See http://www.trolltech.com/qpl/ for QPL licensing information. +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +//#define QT_NO_IM_PREEDIT_RELOCATION + +#include "qinputcontext.h" + +#ifndef QT_NO_IM + +#include "qplatformdefs.h" + +#include "qapplication.h" +#include "qwidget.h" +#include "qpopupmenu.h" + +#include <stdlib.h> +#include <limits.h> + +class QInputContextPrivate +{ +public: + QInputContextPrivate() + : holderWidget( 0 ), composingWidget( 0 ), hasFocus( FALSE ), + isComposing( FALSE ) +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + , preeditString( QString::null ), + cursorPosition( -1 ), selLength ( 0 ) +#endif + {} + + QWidget *holderWidget; // widget to which QInputContext instance belongs. + QWidget *composingWidget; + bool hasFocus; + bool isComposing; + + void updateComposingState( const QString &text, + int newCursorPosition, int newSelLength ) { +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + preeditString = text; + cursorPosition = newCursorPosition; + selLength = newSelLength; +#endif + } + + void resetComposingState() { + isComposing = FALSE; +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + preeditString = QString::null; + cursorPosition = -1; + selLength = 0; +#endif + } + +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + QString preeditString; + int cursorPosition; + int selLength; +#endif +}; + + +// UPDATED COMMENT REQUIRED -- 2004-07-08 YamaKen +/*! + \class QInputContext qinputcontext.h + \brief The QInputContext class abstracts the input method dependent data and composing state. + + \ingroup i18n + + An input method is responsible to input complex text that cannot + be inputted via simple keymap. It converts a sequence of input + events (typically key events) into a text string through the input + method specific converting process. The class of the processes are + widely ranging from simple finite state machine to complex text + translator that pools a whole paragraph of a text with text + editing capability to perform grammar and semantic analysis. + + To abstract such different input method specific intermediate + information, Qt offers the QInputContext as base class. The + concept is well known as 'input context' in the input method + domain. an input context is created for a text widget in response + to a demand. It is ensured that an input context is prepared for + an input method before input to a text widget. + + Multiple input contexts that is belonging to a single input method + may concurrently coexist. Suppose multi-window text editor. Each + text widget of window A and B holds different QInputContext + instance which contains different state information such as + partially composed text. + + \section1 Groups of functions: + + \table + \header \i Context \i Functions + + \row \i Receiving information \i + x11FilterEvent(), + filterEvent(), + setMicroFocus(), + mouseHandler() + + \row \i Sending back composed text \i + sendIMEvent(), + + \row \i State change notification \i + setFocus(), + unsetFocus(), + reset() + + \row \i Context information \i + identifierName(), + language(), + font(), + isComposing(), + + \endtable + + + \section1 Sharing input context between text widgets + + Any input context can be shared between several text widgets to + reduce resource consumption. In ideal case, each text widgets + should be allocated dedicated input context. But some complex + input contexts require slightly heavy resource such as 100 + kilobytes of memory. It prevents quite many text widgets from + being used concurrently. + + To resolve such problem, we can share an input context. There is + one 'input context holder widget' per text widgets that shares + identical input context. In this model, the holder widget owns the + shared input context. Other text widgets access the input context + via QApplication::locateICHolderWidget(). But the access + convention is transparently hidden into QWidget, so developers are + not required to aware of it. + + What developer should know is only the mapping function + QApplication::locateICHolderWidget(). It accepts a widget as + argument and returns its holder widget. Default implementation + returns the top-level widget of the widget as reasonable + assumption. But some applications should reimplement the function + to fit application specific usability. See + QApplication::locateICHolderWidget() for further information. + + + \section1 Preedit preservation + + As described above, input contexts have wide variety of amount of + the state information in accordance with belonging input + method. It is ranging from 2-3 keystrokes of sequence in + deterministic input methods to hundreds of keystrokes with + semantic text refinement in complex input methods such as ordinary + Japanese input method. The difference requires the different reset + policies in losing input focus. + + The former simple input method case, users will prefer resetting + the context to back to the neutral state when something + happened. Suppose a web browsing. The user scroll the page by + scrollbar after he or she has typed a half of the valid key + sequence into a text widget. In the case, the input context should + be reset in losing focus when he or she has dragged the + scrollbar. He or she will be confused if the input context is + still preserved until focused back to the text widget because he + or she will restart typing with first key of the sequence as a + habitual operation. + + On the other hand, we should choose completely different policy + for the latter complex input method case. Suppose same situation + as above but he or she is using a complex input method. In the + case, he or she will be angry if the input context has been lost + when he or she has dragged the scrollbar because the input context + contained a valuably composed text made up by considerable input + cost. So we should not reset the input context in the case. And + the input context should be preserved until focused back to the + text widget. This behavior is named as 'preedit preservation'. + + The two policies can be switched by calling or not calling reset() + in unsetFocus(). Default implementation of unsetFocus() calls + reset() to fit the simple input methods. The implementation is + expressed as 'preedit preservation is disabled'. + + + \section1 Preedit relocation + + Although the most case of the preedit preservation problem for + complex input methods is resolved as described above, there is a + special case. Suppose the case that matches all of the following + conditions. + + \list + + \i a input focus has been moved from a text widget to another text + widget directly + + \i the input context is shared between the two text widgets + + \i preedit preservation is enabled for the input context + + \endlist + + In the case, there are the following two requirements that + contradicts each other. The input context sharing causes it. + + \list + + \i the input context has to be reset to prepare to input to the + newly focused text widget + + \i the input context has to be preserved until focused back to the + previous text widget + + \endlist + + A intrinsic feature named 'preedit relocation' is available to + compromise the requirements. If the feature is enabled for the + input context, it is simply moved to the new text widget with the + preedit string. The user continues the input on the new text + widget, or relocate it to another text widget. The preedit of + previous text widget is automatically cleared to back to the + neutral state of the widget. + + This strange behavior is just a compromise. As described in + previous section, complex input method user should not be exposed + to the risk losing the input context because it contains valuable + long text made up with considerable input cost. The user will + immediately focus back to the previous text widget to continue the + input in the correct text widget if the preedit relocation + occurred. The feature is mainly existing as safety. + + The feature properly works even if the focus is moved as + following. Input method developers are not required to be aware of + the relocation protocol since QInputContext transparently handles + it. + + a text widget -> a non-text widget -> another text widget + + To enable the preedit relocation feature, the input context class + have to reimplement isPreeditRelocationEnabled() as returns TRUE. + The implementation requires that the preedit preservation is also + enabled since preedit relocation is a special case of the preedit + preservation. If the preedit relocation is disabled, the input + context is simply reset in the relocation case. + + + \section1 Input context instanciation + \section1 Input method switching + + \section1 Text widget implementor's guide + + Add following code fragment into createPopupMenu() to add input + method dependent submenus. + + \code + #ifndef QT_NO_IM + QInputContext *qic = getInputContext(); + if ( qic ) + qic->addMenusTo( popup ); + #endif + \endcode + + \sa QInputContextPlugin, QInputContextFactory, QApplication::locateICHolderWidget(), QApplication::defaultInputMethod() +*/ + + +/*! + Constructs an input context. + + holderWidget is set immediately after this constructor has been + returned on the X11 platform. +*/ +QInputContext::QInputContext( QObject *parent ) + : QObject( parent ) +{ + d = new QInputContextPrivate; +} + + +/*! + Destroys the input context. +*/ +QInputContext::~QInputContext() +{ + delete d; +} + +#if defined(Q_WS_X11) +/*! + \internal + Returns the owner of this input context. Ordinary input methods + should not call this function directly to keep platform + independence and flexible configuration possibility. + + The return value may differ from focusWidget() if the input + context is shared between several text widgets. + + \sa setHolderWidget(), focusWidget() +*/ +QWidget *QInputContext::holderWidget() const +{ + return d->holderWidget; +} + +/*! + \internal + Sets the owner of this input context. Ordinary input methods + must not call this function directly. + + \sa holderWidget() +*/ +void QInputContext::setHolderWidget( QWidget *w ) +{ + d->holderWidget = w; +} + +/*! + \internal + Returns the widget that has an input focus for this input + context. Ordinary input methods should not call this function + directly to keep platform independence and flexible configuration + possibility. + + The return value may differ from holderWidget() if the input + context is shared between several text widgets. + + \sa setFocusWidget(), holderWidget() +*/ +QWidget *QInputContext::focusWidget() const +{ + return d->hasFocus ? d->composingWidget : 0; +} + + +/*! + \internal + Sets the widget that has an input focus for this input + context. Ordinary input methods must not call this function + directly. + + \sa focusWidget() +*/ +void QInputContext::setFocusWidget( QWidget *w ) +{ + if ( w ) { + bool isFocusingBack = ( w == d->composingWidget ); + bool isPreeditRelocation = ( ! isFocusingBack && isComposing() && + d->composingWidget ); + // invoke sendIMEventInternal() rather than sendIMEvent() to + // avoid altering the composing state + if ( isPreeditRelocation == TRUE ) { + // clear preedit of previously focused text + // widget. preserved preedit may be exist even if + // isPreeditRelocationEnabled() == FALSE. + sendIMEventInternal( QEvent::IMEnd ); + } + d->composingWidget = w; // changes recipient of QIMEvent + if ( isPreeditRelocation == TRUE ) { +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + if ( isPreeditRelocationEnabled() ) { + // copy preedit state to the widget that gaining focus + sendIMEventInternal( QEvent::IMStart ); + sendIMEventInternal( QEvent::IMCompose, d->preeditString, + d->cursorPosition, d->selLength ); + } else +#endif + { + // reset input context when the shared context has + // focused on another text widget + reset(); + } + } + } + d->hasFocus = w ? TRUE : FALSE; +} + + +/*! + \internal + This function is called from QWidget to keep input state + consistency. Ordinary input method must not call this function + directly. +*/ +void QInputContext::releaseComposingWidget( QWidget *w ) +{ + if ( d->composingWidget == w ) { + d->composingWidget = 0; + d->hasFocus = FALSE; + } +} +#endif // Q_WS_X11 + +/*! + \internal + This function can be reimplemented in a subclass as returning TRUE + if you want making your input method enable the preedit + relocation. See the description for preedit relocation of + QInputContext. + + /sa QInputContext +*/ +bool QInputContext::isPreeditRelocationEnabled() +{ + return FALSE; +} + +/*! + This function indicates whether IMStart event had been sent to the + text widget. It is ensured that an input context can send IMCompose + or IMEnd event safely if this function returned TRUE. + + The state is automatically being tracked through sendIMEvent(). + + \sa sendIMEvent() +*/ +bool QInputContext::isComposing() const +{ + return d->isComposing; +} + + +/*! + This function can be reimplemented in a subclass to filter input + events. + + Return TRUE if the \a event has been consumed. Otherwise, the + unfiltered \a event will be forwarded to widgets as ordinary + way. Although the input events have accept() and ignore() + methods, leave it untouched. + + \a event is currently restricted to QKeyEvent. But some input + method related events such as QWheelEvent or QTabletEvent may be + added in future. + + The filtering opportunity is always given to the input context as + soon as possible. It has to be taken place before any other key + event consumers such as eventfilters and accelerators because some + input methods require quite various key combination and + sequences. It often conflicts with accelerators and so on, so we + must give the input context the filtering opportunity first to + ensure all input methods work properly regardless of application + design. + + Ordinary input methods require discrete key events to work + properly, so Qt's key compression is always disabled for any input + contexts. + + \sa QKeyEvent, x11FilterEvent() +*/ +bool QInputContext::filterEvent( const QEvent *event ) +{ + return FALSE; +} + + +/*! + \fn void QInputContext::deletionRequested() + + Emit this signal when a fatal error has been caused in the input + context. The input context will be deleted by the owner which is + usually the holder widget. +*/ + +/*! + \fn void QInputContext::imEventGenerated( QObject *receiver, QIMEvent *e ) + + \internal + This signal is emitted when the user has sent a QIMEvent through + sendIMEvent(). Ordinary input methods should not emit this signal + directly. + + \a receiver is a platform dependent destination of the \a e. + + \sa QIMEvent, sendIMEvent(), sendIMEventInternal(), +*/ + +/*! + \internal + Sends a QIMEvent to the client via imEventGenerated() + signal. Ordinary input method should not call this function + directly. + + \sa QIMEvent, QIMComposeEvent, sendIMEvent(), imEventGenerated() +*/ +void QInputContext::sendIMEventInternal( QEvent::Type type, + const QString &text, + int cursorPosition, int selLength ) +{ + QObject *receiver = 0; + QIMEvent *event = 0; + +#if defined(Q_WS_X11) + receiver = d->composingWidget; +#elif defined(Q_WS_QWS) + // just a placeholder +#endif + if ( ! receiver ) + return; + + if ( type == QEvent::IMStart ) { + qDebug( "sending IMStart with %d chars to %p", + text.length(), receiver ); + event = new QIMEvent( type, text, cursorPosition ); + } else if ( type == QEvent::IMEnd ) { + qDebug( "sending IMEnd with %d chars to %p, text=%s", + text.length(), receiver, (const char*)text.local8Bit() ); + event = new QIMEvent( type, text, cursorPosition ); + } else if ( type == QEvent::IMCompose ) { + qDebug( "sending IMCompose to %p with %d chars, cpos=%d, sellen=%d, text=%s", + receiver, text.length(), cursorPosition, selLength, + (const char*)text.local8Bit() ); + event = new QIMComposeEvent( type, text, cursorPosition, selLength ); + } + + if ( event ) + emit imEventGenerated( receiver, event ); +} + + +/*! + Call this function to send QIMEvent to the text widget. This + function constructs a QIMEvent based on the arguments and send it + to the appropriate widget. Ordinary input method should not + reimplement this function. + + \a type is either \c QEvent::IMStart or \c QEvent::IMCompose or \c + QEvent::IMEnd. You have to send a \c QEvent::IMStart to start + composing, then send several \c QEvent::IMCompose to update the + preedit of the widget, and finalize the composition with sending + \c QEvent::IMEnd. + + \c QEvent::IMStart should always be sent without arguments as: + \code + sendIMEvent( QEvent::IMStart ) + \endcode + + And \c QEvent::IMCompose can be sent without cursor: + \code + sendIMEvent( QEvent::IMCompose, QString( "a text" ) ) + \endcode + + Or optionally with cursor with \a cursorPosition: + \code + sendIMEvent( QEvent::IMCompose, QString( "a text with cursor" ), 12 ) + \endcode + Note that \a cursorPosition also specifies microfocus position. + + Or optionally with selection text: + \code + sendIMEvent( QEvent::IMCompose, QString( "a text with selection" ), 12, 9 ) + \endcode + \a cursorPosition and \a selLength must be within the \a text. The + \a cursorPosition also specifies microfocus position in the case: + + \c QEvent::IMEnd can be sent without arguments to terminate the + composition with null string: + \code + sendIMEvent( QEvent::IMEnd ) + \endcode + + Or optionally accepts \a text to commit a string: + \code + sendIMEvent( QEvent::IMEnd, QString( "a text" ) ) + \endcode + + \sa QIMEvent, QIMComposeEvent, setMicroFocus() +*/ +void QInputContext::sendIMEvent( QEvent::Type type, const QString &text, + int cursorPosition, int selLength ) +{ +#if defined(Q_WS_X11) + if ( !focusWidget() ) + return; +#endif + + if ( type == QEvent::IMStart ) { + sendIMEventInternal( type, text, cursorPosition, selLength ); + d->isComposing = TRUE; + } else if ( type == QEvent::IMEnd ) { + d->resetComposingState(); + sendIMEventInternal( type, text, cursorPosition, selLength ); + } else if ( type == QEvent::IMCompose ) { + d->updateComposingState( text, cursorPosition, selLength ); + sendIMEventInternal( type, text, cursorPosition, selLength ); + } +} + + +/*! + This function can be reimplemented in a subclass to detect + that the input context has been focused on. + + The input context will receive input events through + x11FilterEvent() and filterEvent() after setFocus() until + unsetFocus() has been called. + + an input context is ensured that setFocus() is called exactly once + until unsetFocus() has been called even if preedit relocation has + occurred. This means that an input focus will survive between + several widgets that sharing the input context. + + On the X11 platform, focusWidget is already set before this + function has been called. + + \sa unsetFocus() +*/ +void QInputContext::setFocus() +{ +} + + +/*! + This function can be reimplemented in a subclass to detect + that the input context has lost the focus. + + an input context is ensured that unsetFocus() is not called during + preedit relocation. This means that an input focus will survive + between several widgets that sharing the input context. + + Default implementation that calls reset() is sufficient for simple + input methods. You can override this function to alter the + behavior. For example, most Japanese input contexts should not be + reset on losing focus. The context sometimes contains a whole + paragraph and has minutes of lifetime different to ephemeral one + in other languages. The piled input context should be survived + until focused again since Japanese user naturally expects so. + + On the X11 platform, focusWidget is valid until this function has + been returned. + + \sa setFocus() +*/ +void QInputContext::unsetFocus() +{ + reset(); +} + + +/*! + This function can be implemented in a subclass to handle + microfocus changes. + + 'microfocus' stands for the input method focus point in the + preedit (XIM "spot" point) for complex language input handling. It + can be used to place auxiliary GUI widgets such as candidate + selection window. + + \a x, \a y, \a w and \a h represents the position and size of the + cursor in the preedit string. \a f is the font on the location of + the cursor. +*/ +void QInputContext::setMicroFocus( int x, int y, int w, int h, QFont *f ) +{ +} + + +/*! + This function can be reimplemented in a subclass to handle mouse + presses/releases/doubleclicks/moves within the preedit text. You + can use the function to implement mouse-oriented user interface + such as text selection or popup menu for candidate selection. + + The parameter \a x is the offset within the string that was sent + with the IMCompose event. The alteration boundary of \a x is + ensured as character boundary of preedit string accurately. + + \a type is either \c QEvent::MouseButtonPress or \c + QEvent::MouseButtonRelease or \c QEvent::MouseButtonDblClick or \c + QEvent::MouseButtonMove. Refer \a button and \a state to determine + what operation has performed. + + The method interface is imported from + QWSInputMethod::mouseHandler() of Qt/Embedded 2.3.7 and extended + for desktop system. + */ +void QInputContext::mouseHandler( int x, QEvent::Type type, + Qt::ButtonState button, + Qt::ButtonState state ) +{ + // Default behavior for simple ephemeral input contexts. Some + // complex input contexts should not be reset here. + if ( type == QEvent::MouseButtonPress || + type == QEvent::MouseButtonDblClick ) + reset(); +} + + +/*! + Returns the font of the current input widget + */ +QFont QInputContext::font() const +{ + if ( !focusWidget() ) + return QApplication::font(); //### absolutely last resort + + return focusWidget()->font(); +} + + +/*! + This function can be reimplemented in a subclass to reset the + state of the input method. + + This function is called by several widgets to reset input + state. For example, a text widget call this function before + inserting a text to make widget ready to accept a text. + + Default implementation is sufficient for simple input method. You + can override this function to reset external input method engines + in complex input method. In the case, call QInputContext::reset() + to ensure proper termination of inputting. + + You must not send any QIMEvent except empty IMEnd event using + QInputContext::reset() at reimplemented reset(). It will break + input state consistency. +*/ +void QInputContext::reset() +{ + if ( isComposing() ) + sendIMEvent( QEvent::IMEnd ); +} + + +/*! + This function must be implemented in any subclasses to return the + identifier name of the input method. + + Return value is the name to identify and specify input methods for + the input method switching mechanism and so on. The name has to be + consistent with QInputContextPlugin::keys(). The name has to + consist of ASCII characters only. + + There are two different names with different responsibility in the + input method domain. This function returns one of them. Another + name is called 'display name' that stands for the name for + endusers appeared in a menu and so on. + + \sa QInputContextPlugin::keys(), QInputContextPlugin::displayName() +*/ +QString QInputContext::identifierName() +{ + return ""; +} + + +/*! + This function must be implemented in any subclasses to return a + language code (e.g. "zh_CN", "zh_TW", "zh_HK", "ja", "ko", ...) + of the input context. If the input context can handle multiple + languages, return the currently used one. The name has to be + consistent with QInputContextPlugin::language(). + + This information will be used by language tagging feature in + QIMEvent. It is required to distinguish unified han characters + correctly. It enables proper font and character code + handling. Suppose CJK-awared multilingual web browser + (that automatically modifies fonts in CJK-mixed text) and XML editor + (that automatically inserts lang attr). + + \sa QInputContextPlugin::language() +*/ +QString QInputContext::language() +{ + return ""; +} + + +#if (QT_VERSION-0 >= 0x040000) +/*! + This is a preliminary interface for Qt4 + */ +QList<QAction *> QInputContext::actions() +{ +} +#else +/*! + This function can be reimplemented in a subclass to provide input + method dependent popup menus. Return 0 if the menus are + unnecessary. + + Ownership of the object and children are transferred to the + caller, and the result must not be called + setAutoDelete(). QInputContextMenu::title is used for label text + of the popup menu as submenu. + + \sa addMenusTo() +*/ +QPtrList<QInputContextMenu> *QInputContext::menus() +{ + return 0; +} +#endif + +/*! + Appends input method dependent submenus into \a popup. A separator + is also inserted into \a popup if \a action is InsertSeparator. + + This is an utility function only for convenience in limited + situation. This function is used by input context owner such as + text widgets to add the submenus to its own context menu. If you + want to insert the submenus in more flexible way, use + QInputContext::menus() manually. \a popup is not restricted to + context menu of a text widget. For example, the owner may be a + input method menu of Qtopia taskbar in Qt/Embedded platform. + + \sa menus(), QInputContextMenu::Action +*/ +void QInputContext::addMenusTo( QPopupMenu *popup, QInputContextMenu::Action action ) +{ + if ( ! popup ) + return; + + QPtrList<QInputContextMenu> *imMenus = menus(); + if ( imMenus ) { + if ( action == QInputContextMenu::InsertSeparator ) + popup->insertSeparator(); + for ( QPtrList<QInputContextMenu>::Iterator it = imMenus->begin(); + it != imMenus->end(); + ++it ) { + QInputContextMenu *imMenu = *it; + popup->insertItem( imMenu->title, imMenu->popup ); + } + imMenus->clear(); + delete imMenus; + } +} + +#endif //Q_NO_IM diff --git a/src/kernel/qinputcontext.h b/src/kernel/qinputcontext.h new file mode 100644 index 0000000..99e5d37 --- /dev/null +++ b/src/kernel/qinputcontext.h @@ -0,0 +1,143 @@ +/**************************************************************************** +** $Id: qinputcontext.h,v 1.8 2004/06/22 06:47:30 daisuke Exp $ +** +** Definition of QInputContext +** +** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. +** +** This file is part of the kernel module of the Qt GUI Toolkit. +** +** This file may be distributed under the terms of the Q Public License +** as defined by Trolltech AS of Norway and appearing in the file +** LICENSE.QPL included in the packaging of this file. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition +** licenses may use this file in accordance with the Qt Commercial License +** Agreement provided with the Software. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for +** information about Qt Commercial License Agreements. +** See http://www.trolltech.com/qpl/ for QPL licensing information. +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#ifndef QINPUTCONTEXT_H +#define QINPUTCONTEXT_H + +#ifndef QT_NO_IM + +#ifndef QT_H +#include "qobject.h" +#include "qglobal.h" +#include "qevent.h" +#include "qstring.h" +#if (QT_VERSION-0 >= 0x040000) +#include "qlist.h" +#include "qaction.h" +#else +#include "qptrlist.h" +#endif +#endif + +class QWidget; +class QFont; +class QPopupMenu; +class QInputContextPrivate; + + +struct QInputContextMenu { + enum Action { + NoSeparator, + InsertSeparator + }; +#if !(QT_VERSION-0 >= 0x040000) + QString title; + QPopupMenu *popup; +#endif +}; + + +class QInputContext : public QObject +{ + Q_OBJECT +public: + QInputContext( QObject *parent = 0 ); + virtual ~QInputContext(); + + virtual QString identifierName(); + virtual QString language(); + +#if defined(Q_WS_X11) + virtual bool x11FilterEvent( QWidget *keywidget, XEvent *event ); +#endif // Q_WS_X11 + virtual bool filterEvent( const QEvent *event ); + virtual void reset(); + + virtual void setFocus(); + virtual void unsetFocus(); + virtual void setMicroFocus( int x, int y, int w, int h, QFont *f = 0 ); + virtual void mouseHandler( int x, QEvent::Type type, + Qt::ButtonState button, Qt::ButtonState state ); + virtual QFont font() const; + virtual bool isComposing() const; + virtual bool isPreeditRelocationEnabled(); + +#if (QT_VERSION-0 >= 0x040000) + virtual QList<QAction *> actions(); + void addActionsTo( QMenu *menu, QInputContextMenu::Action action = QInputContextMenu::InsertSeparator ); +#else + virtual QPtrList<QInputContextMenu> *menus(); + void addMenusTo( QPopupMenu *popup, QInputContextMenu::Action action = QInputContextMenu::InsertSeparator ); +#endif + +#if defined(Q_WS_X11) + // these functions are not recommended for ordinary use + virtual QWidget *focusWidget() const; + virtual QWidget *holderWidget() const; + + // these functions must not be used by ordinary input method + virtual void setFocusWidget( QWidget *w ); + virtual void setHolderWidget( QWidget *w ); + virtual void releaseComposingWidget( QWidget *w ); +#endif + +signals: + void deletionRequested(); + void imEventGenerated( QObject *receiver, QIMEvent *e ); + +protected: + virtual void sendIMEvent( QEvent::Type type, + const QString &text = QString::null, + int cursorPosition = -1, int selLength = 0 ); + +private: + void sendIMEventInternal( QEvent::Type type, + const QString &text = QString::null, + int cursorPosition = -1, int selLength = 0 ); + + QInputContextPrivate *d; + + friend class QWidget; + friend class QInputContextFactory; + +private: // Disabled copy constructor and operator= + QInputContext( const QInputContext & ); + QInputContext &operator=( const QInputContext & ); + +}; + +#endif //Q_NO_IM + +#endif // QINPUTCONTEXT_H diff --git a/src/kernel/qinputcontext_p.h b/src/kernel/qinputcontext_p.h new file mode 100644 index 0000000..2debb63 --- /dev/null +++ b/src/kernel/qinputcontext_p.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Definition of ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QINPUTCONTEXT_P_H +#define QINPUTCONTEXT_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#include "qglobal.h" + +class QKeyEvent; +class QWidget; +class QFont; +class QString; + + +#ifdef Q_WS_X11 +#include "qarray.h" +#include "qwindowdefs.h" +#include "qt_x11_p.h" +#endif + +#ifdef Q_WS_WIN +#include "qt_windows.h" +#endif + +#ifdef Q_WS_QWS +class QWSIMEvent; +#endif + +class QInputContext +{ +public: +#ifdef Q_WS_X11 + QInputContext(QWidget *); // should be a toplevel widget + ~QInputContext(); + + void setFocus(); + void setComposePosition(int, int); + void setComposeArea(int, int, int, int); + void reset(); + + int lookupString(XKeyEvent *, QCString &, KeySym *, Status *) const; + void setXFontSet(const QFont &); + + void *ic; + QString text; + QWidget *focusWidget; + bool composing; + QFont font; + XFontSet fontset; + QMemArray<bool> selectedChars; +#endif // Q_WS_X11 + +#ifdef Q_WS_QWS + static void translateIMEvent( QWSIMEvent *, QWidget * ); + static void reset(); +private: + static QWidget* focusWidget; + static QString* composition; +#endif //Q_WS_QWS + +#ifdef Q_WS_WIN + static void init(); + static void shutdown(); + + static void TranslateMessage( const MSG *msg); + static LRESULT DefWindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); + + static void setFont( const QWidget *w, const QFont & ); + static void setFocusHint( int x, int y, int w, int h, const QWidget *widget ); + static bool startComposition(); + static bool endComposition( QWidget *fw = 0 ); + static bool composition( LPARAM lparam ); + + static void accept( QWidget *fw = 0 ); + static void enable( QWidget *w, bool b ); +#endif +}; + +#endif // QINPUTCONTEXT_P_H diff --git a/src/kernel/qinputcontext_x11.cpp b/src/kernel/qinputcontext_x11.cpp new file mode 100644 index 0000000..8bdfa5e --- /dev/null +++ b/src/kernel/qinputcontext_x11.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Implementation of QInputContext class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qinputcontext.h" + +#ifndef QT_NO_IM + +#include "qplatformdefs.h" + +#include "qapplication.h" +#include "qwidget.h" + +#include "qt_x11_p.h" + +/*! + This function may be overridden only if input method is depending + on X11 and you need raw XEvent. Otherwise, this function must not. + + This function is designed to filter raw key events for XIM, but + other input methods may use this to implement some special + features such as distinguishing Shift_L and Shift_R. + + Return TRUE if the \a event has been consumed. Otherwise, the + unfiltered \a event will be translated into QEvent and forwarded + to filterEvent(). Filtering at both x11FilterEvent() and + filterEvent() in single input method is allowed. + + \a keywidget is a client widget into which a text is inputted. \a + event is inputted XEvent. + + \sa filterEvent() +*/ +bool QInputContext::x11FilterEvent( QWidget *keywidget, XEvent *event ) +{ + return FALSE; +} + +#endif //Q_NO_IM diff --git a/src/kernel/qinternal.cpp b/src/kernel/qinternal.cpp new file mode 100644 index 0000000..4289ecd --- /dev/null +++ b/src/kernel/qinternal.cpp @@ -0,0 +1,771 @@ +/**************************************************************************** +** +** Implementation of some internal classes +** +** Created : 010427 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "private/qinternal_p.h" +#include "qwidget.h" +#include "qpixmap.h" +#include "qpainter.h" +#include "qcleanuphandler.h" + +static QPixmap* qdb_shared_pixmap = 0; +static QPixmap *qdb_force_pixmap = 0; +static QSharedDoubleBuffer* qdb_owner = 0; + +QCleanupHandler<QPixmap> qdb_pixmap_cleanup; + +#ifdef Q_WS_MACX +bool QSharedDoubleBuffer::dblbufr = FALSE; +#else +bool QSharedDoubleBuffer::dblbufr = TRUE; +#endif + + +/* + hardLimitWidth/Height: if >= 0, the maximum number of pixels that + get double buffered. + + sharedLimitWidth/Height: if >= 0, the maximum number of pixels the + shared double buffer can keep. + + For x with sharedLimitSize < x <= hardLimitSize, temporary buffers + are constructed. + */ +static const int hardLimitWidth = -1; +static const int hardLimitHeight = -1; +#if defined( Q_WS_QWS ) || defined( Q_WS_MAC9 ) +// Small in Qt/Embedded / Mac9 - 5K on 32bpp +static const int sharedLimitWidth = 64; +static const int sharedLimitHeight = 20; +#else +// 240K on 32bpp +static const int sharedLimitWidth = 640; +static const int sharedLimitHeight = 100; +#endif + +// ******************************************************************* +// QSharedDoubleBufferCleaner declaration and implementation +// ******************************************************************* + +/* \internal + This class is responsible for cleaning up the pixmaps created by the + QSharedDoubleBuffer class. When QSharedDoubleBuffer creates a + pixmap larger than the shared limits, this class deletes it after a + specified amount of time. + + When the large pixmap is created/used, you must call start(). If the + large pixmap is ever deleted, you must call stop(). The start() + method always restarts the timer, so if the large pixmap is + constantly in use, the timer will never fire, and the pixmap will + not be constantly created and destroyed. +*/ + +static const int shared_double_buffer_cleanup_timeout = 30000; // 30 seconds + +// declaration + +class QSharedDoubleBufferCleaner : public QObject +{ +public: + QSharedDoubleBufferCleaner( void ); + + void start( void ); + void stop( void ); + + void doCleanup( void ); + + bool event( QEvent *e ); + +private: + int timer_id; +}; + +// implementation + +/* \internal + Creates a QSharedDoubleBufferCleaner object. The timer is not + started when creating the object. +*/ +QSharedDoubleBufferCleaner::QSharedDoubleBufferCleaner( void ) + : QObject( 0, "internal shared double buffer cleanup object" ), + timer_id( -1 ) +{ +} + +/* \internal + Starts the cleanup timer. Any previously running timer is stopped. +*/ +void QSharedDoubleBufferCleaner::start( void ) +{ + stop(); + timer_id = startTimer( shared_double_buffer_cleanup_timeout ); +} + +/* \internal + Stops the cleanup timer, if it is running. +*/ +void QSharedDoubleBufferCleaner::stop( void ) +{ + if ( timer_id != -1 ) + killTimer( timer_id ); + timer_id = -1; +} + +/* \internal + */ +void QSharedDoubleBufferCleaner::doCleanup( void ) +{ + qdb_pixmap_cleanup.remove( &qdb_force_pixmap ); + delete qdb_force_pixmap; + qdb_force_pixmap = 0; +} + +/* \internal + Event handler reimplementation. Calls doCleanup() when the timer + fires. +*/ +bool QSharedDoubleBufferCleaner::event( QEvent *e ) +{ + if ( e->type() != QEvent::Timer ) + return FALSE; + + QTimerEvent *event = (QTimerEvent *) e; + if ( event->timerId() == timer_id ) { + doCleanup(); + stop(); + } +#ifdef QT_CHECK_STATE + else { + qWarning( "QSharedDoubleBufferCleaner::event: invalid timer event received." ); + return FALSE; + } +#endif // QT_CHECK_STATE + + return TRUE; +} + +// static instance +static QSharedDoubleBufferCleaner *static_cleaner = 0; +QSingleCleanupHandler<QSharedDoubleBufferCleaner> cleanup_static_cleaner; + +inline static QSharedDoubleBufferCleaner *staticCleaner() +{ + if ( ! static_cleaner ) { + static_cleaner = new QSharedDoubleBufferCleaner(); + cleanup_static_cleaner.set( &static_cleaner ); + } + return static_cleaner; +} + + +// ******************************************************************* +// QSharedDoubleBuffer implementation +// ******************************************************************* + +/* \internal + \enum DoubleBufferFlags + + \value InitBG initialize the background of the double buffer. + + \value Force disable shared buffer size limits. + + \value Default InitBG and Force are used by default. +*/ + +/* \internal + \enum DoubleBufferState + + \value Active indicates that the buffer may be used. + + \value BufferActive indicates that painting with painter() will be + double buffered. + + \value ExternalPainter indicates that painter() will return a + painter that was not created by QSharedDoubleBuffer. +*/ + +/* \internal + \class QSharedDoubleBuffer + + This class provides a single, reusable double buffer. This class + is used internally by Qt widgets that need double buffering, which + prevents each individual widget form creating a double buffering + pixmap. + + Using a single pixmap double buffer and sharing it across all + widgets is nicer on window system resources. +*/ + +/* \internal + Creates a QSharedDoubleBuffer with flags \f. + + \sa DoubleBufferFlags +*/ +QSharedDoubleBuffer::QSharedDoubleBuffer( DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ +} + +/* \internal + Creates a QSharedDoubleBuffer with flags \f. The \a widget, \a x, + \a y, \a w and \a h arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +QSharedDoubleBuffer::QSharedDoubleBuffer( QWidget* widget, + int x, int y, int w, int h, + DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( widget, x, y, w, h ); +} + +/* \internal + Creates a QSharedDoubleBuffer with flags \f. The \a painter, \a x, + \a y, \a w and \a h arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +QSharedDoubleBuffer::QSharedDoubleBuffer( QPainter* painter, + int x, int y, int w, int h, + DBFlags f) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( painter, x, y, w, h ); +} + +/* \internal + Creates a QSharedDoubleBuffer with flags \f. The \a widget and + \a r arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +QSharedDoubleBuffer::QSharedDoubleBuffer( QWidget *widget, const QRect &r, DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( widget, r ); +} + +/* \internal + Creates a QSharedDoubleBuffer with flags \f. The \a painter and + \a r arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +QSharedDoubleBuffer::QSharedDoubleBuffer( QPainter *painter, const QRect &r, DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( painter, r ); +} + +/* \internal + Destructs the QSharedDoubleBuffer and calls end() if the buffer is + active. + + \sa isActive() end() +*/ +QSharedDoubleBuffer::~QSharedDoubleBuffer() +{ + if ( isActive() ) + end(); +} + +/* \internal + Starts double buffered painting in the area specified by \a x, + \a y, \a w and \a h on \a painter. Painting should be done using the + QPainter returned by QSharedDoubleBuffer::painter(). + + The double buffered area will be updated when calling end(). + + \sa painter() isActive() end() +*/ +bool QSharedDoubleBuffer::begin( QPainter* painter, int x, int y, int w, int h ) +{ + if ( isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QSharedDoubleBuffer::begin: Buffer is already active." + "\n\tYou must end() the buffer before a second begin()" ); +#endif // QT_CHECK_STATE + return FALSE; + } + + external_p = painter; + + if ( painter->device()->devType() == QInternal::Widget ) + return begin( (QWidget *) painter->device(), x, y, w, h ); + + state = Active; + + rx = x; + ry = y; + rw = w; + rh = h; + + if ( ( pix = getPixmap() ) ) { +#ifdef Q_WS_X11 + if ( painter->device()->x11Screen() != pix->x11Screen() ) + pix->x11SetScreen( painter->device()->x11Screen() ); + QPixmap::x11SetDefaultScreen( pix->x11Screen() ); +#endif // Q_WS_X11 + + state |= BufferActive; + p = new QPainter( pix ); + if ( p->isActive() ) { + p->setPen( external_p->pen() ); + p->setBackgroundColor( external_p->backgroundColor() ); + p->setFont( external_p->font() ); + } + } else { + state |= ExternalPainter; + p = external_p; + } + + return TRUE; +} + +/* \internal + + + Starts double buffered painting in the area specified by \a x, + \a y, \a w and \a h on \a widget. Painting should be done using the + QPainter returned by QSharedDoubleBuffer::painter(). + + The double buffered area will be updated when calling end(). + + \sa painter() isActive() end() +*/ +bool QSharedDoubleBuffer::begin( QWidget* widget, int x, int y, int w, int h ) +{ + if ( isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QSharedDoubleBuffer::begin: Buffer is already active." + "\n\tYou must end() the buffer before a second begin()" ); +#endif // QT_CHECK_STATE + return FALSE; + } + + state = Active; + + wid = widget; + rx = x; + ry = y; + rw = w <= 0 ? wid->width() : w; + rh = h <= 0 ? wid->height() : h; + + if ( ( pix = getPixmap() ) ) { +#ifdef Q_WS_X11 + if ( wid->x11Screen() != pix->x11Screen() ) + pix->x11SetScreen( wid->x11Screen() ); + QPixmap::x11SetDefaultScreen( pix->x11Screen() ); +#endif // Q_WS_X11 + + state |= BufferActive; + if ( flags & InitBG ) { + pix->fill( wid, rx, ry ); + } + p = new QPainter( pix, wid ); + // newly created painters should be translated to the origin + // of the widget, so that paint methods can draw onto the double + // buffered painter in widget coordinates. + p->setBrushOrigin( -rx, -ry ); + p->translate( -rx, -ry ); + } else { + if ( external_p ) { + state |= ExternalPainter; + p = external_p; + } else { + p = new QPainter( wid ); + } + + if ( flags & InitBG ) { + wid->erase( rx, ry, rw, rh ); + } + } + return TRUE; +} + +/* \internal + Ends double buffered painting. The contents of the shared double + buffer pixmap are drawn onto the destination by calling flush(), + and ownership of the shared double buffer pixmap is released. + + \sa begin() flush() +*/ +bool QSharedDoubleBuffer::end() +{ + if ( ! isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QSharedDoubleBuffer::end: Buffer is not active." + "\n\tYou must call begin() before calling end()." ); +#endif // QT_CHECK_STATE + return FALSE; + } + + if ( ! ( state & ExternalPainter ) ) { + p->end(); + delete p; + } + + flush(); + + if ( pix ) { + releasePixmap(); + } + + wid = 0; + rx = ry = rw = rh = 0; + // do not reset flags! + state = 0; + + p = external_p = 0; + pix = 0; + + return TRUE; +} + +/* \internal + Paints the contents of the shared double buffer pixmap onto the + destination. The destination is determined from the arguments + based to begin(). + + Note: You should not need to call this function, since it is called + from end(). + + \sa begin() end() +*/ +void QSharedDoubleBuffer::flush() +{ + if ( ! isActive() || ! ( state & BufferActive ) ) + return; + + if ( external_p ) + external_p->drawPixmap( rx, ry, *pix, 0, 0, rw, rh ); + else if ( wid && wid->isVisible() ) + bitBlt( wid, rx, ry, pix, 0, 0, rw, rh ); +} + +/* \internal + Aquire ownership of the shared double buffer pixmap, subject to the + following conditions: + + \list 1 + \i double buffering is enabled globally. + \i the shared double buffer pixmap is not in use. + \i the size specified in begin() is valid, and within limits. + \endlist + + If all of these conditions are met, then this QSharedDoubleBuffer + object becomes the owner of the shared double buffer pixmap. The + shared double buffer pixmap is resize if necessary, and this + function returns a pointer to the pixmap. Ownership must later be + relinquished by calling releasePixmap(). + + If none of the above conditions are met, this function returns + zero. + + \sa releasePixmap() +*/ +QPixmap *QSharedDoubleBuffer::getPixmap() +{ + if ( isDisabled() ) { + // double buffering disabled globally + return 0; + } + + if ( qdb_owner ) { + // shared pixmap already in use + return 0; + } + + if ( rw <= 0 || rh <= 0 || + ( hardLimitWidth > 0 && rw >= hardLimitWidth ) || + ( hardLimitHeight > 0 && rh >= hardLimitHeight ) ) { + // invalid size, or hard limit reached + return 0; + } + + if ( rw >= sharedLimitWidth || rh >= sharedLimitHeight ) { + if ( flags & Force ) { + rw = QMIN(rw, 8000); + rh = QMIN(rh, 8000); + // need to create a big pixmap and start the cleaner + if ( ! qdb_force_pixmap ) { + qdb_force_pixmap = new QPixmap( rw, rh ); + qdb_pixmap_cleanup.add( &qdb_force_pixmap ); + } else if ( qdb_force_pixmap->width () < rw || + qdb_force_pixmap->height() < rh ) { + qdb_force_pixmap->resize( rw, rh ); + } + qdb_owner = this; + staticCleaner()->start(); + return qdb_force_pixmap; + } + + // size is outside shared limit + return 0; + } + + if ( ! qdb_shared_pixmap ) { + qdb_shared_pixmap = new QPixmap( rw, rh ); + qdb_pixmap_cleanup.add( &qdb_shared_pixmap ); + } else if ( qdb_shared_pixmap->width() < rw || + qdb_shared_pixmap->height() < rh ) { + qdb_shared_pixmap->resize( rw, rh ); + } + qdb_owner = this; + return qdb_shared_pixmap; +} + +/* \internal + Releases ownership of the shared double buffer pixmap. + + \sa getPixmap() +*/ +void QSharedDoubleBuffer::releasePixmap() +{ + if ( qdb_owner != this ) { + // sanity check + +#ifdef QT_CHECK_STATE + qWarning( "QSharedDoubleBuffer::releasePixmap: internal error." + "\n\t%p does not own shared pixmap, %p does.", + (void*)this, (void*)qdb_owner ); +#endif // QT_CHECK_STATE + + return; + } + + qdb_owner = 0; +} + +/* \internal + \fn bool QSharedDoubleBuffer::isDisabled() + + Returns TRUE if double buffering is disabled globally, FALSE otherwise. +*/ + +/* \internal + \fn void QSharedDoubleBuffer::setDisabled( bool off ) + + Disables global double buffering \a off is TRUE, otherwise global + double buffering is enabled. +*/ + +/* \internal + Deletes the shared double buffer pixmap. You should not need to + call this function, since it is called from the QApplication + destructor. +*/ +void QSharedDoubleBuffer::cleanup() +{ + qdb_pixmap_cleanup.remove( &qdb_shared_pixmap ); + qdb_pixmap_cleanup.remove( &qdb_force_pixmap ); + delete qdb_shared_pixmap; + delete qdb_force_pixmap; + qdb_shared_pixmap = 0; + qdb_force_pixmap = 0; + qdb_owner = 0; +} + +/* \internal + \fn bool QSharedDoubleBuffer::begin( QWidget *widget, const QRect &r ) + \overload +*/ + +/* \internal + \fn bool QSharedDoubleBuffer::begin( QPainter *painter, const QRect &r ) + \overload +*/ + +/* \internal + \fn QPainter *QSharedDoubleBuffer::painter() const + + Returns the active painter on the double buffered area, + or zero if double buffered painting is not active. +*/ + +/* \internal + \fn bool QSharedDoubleBuffer::isActive() const + + Returns TRUE if double buffered painting is active, FALSE otherwise. +*/ + +/* \internal + \fn bool QSharedDoubleBuffer::isBuffered() const + + Returns TRUE if painting is double buffered, FALSE otherwise. +*/ + + +// ******************************************************************* +// QMembuf declaration and implementation +// ******************************************************************* + +/* \internal + This class implements an efficient buffering of data that is often used by + asynchronous IO classes like QSocket, QHttp and QProcess. +*/ + +QMembuf::QMembuf() : _size(0), _index(0) +{ + buf = new QPtrList<QByteArray>; + buf->setAutoDelete( TRUE ); +} + +QMembuf::~QMembuf() +{ + delete buf; +} + +/*! \internal + This function consumes \a nbytes bytes of data from the + buffer and copies it into \a sink. If \a sink is a 0 pointer + the data goes into the nirvana. +*/ +bool QMembuf::consumeBytes( Q_ULONG nbytes, char *sink ) +{ + if ( nbytes <= 0 || nbytes > _size ) + return FALSE; + _size -= nbytes; + for ( ;; ) { + QByteArray *a = buf->first(); + if ( _index + nbytes >= a->size() ) { + // Here we skip the whole byte array and get the next later + int len = a->size() - _index; + if ( sink ) { + memcpy( sink, a->data()+_index, len ); + sink += len; + } + nbytes -= len; + buf->remove(); + _index = 0; + if ( nbytes == 0 ) + break; + } else { + // Here we skip only a part of the first byte array + if ( sink ) + memcpy( sink, a->data()+_index, nbytes ); + _index += nbytes; + break; + } + } + return TRUE; +} + +/*! \internal + Scans for any occurrence of '\n' in the buffer. If \a store + is not 0 the text up to the first '\n' (or terminating 0) is + written to \a store, and a terminating 0 is appended to \a store + if necessary. Returns TRUE if a '\n' was found; otherwise returns + FALSE. +*/ +bool QMembuf::scanNewline( QByteArray *store ) +{ + if ( _size == 0 ) + return FALSE; + int i = 0; // index into 'store' + QByteArray *a = 0; + char *p; + int n; + for ( ;; ) { + if ( !a ) { + a = buf->first(); + if ( !a || a->size() == 0 ) + return FALSE; + p = a->data() + _index; + n = a->size() - _index; + } else { + a = buf->next(); + if ( !a || a->size() == 0 ) + return FALSE; + p = a->data(); + n = a->size(); + } + if ( store ) { + while ( n-- > 0 ) { + *(store->data()+i) = *p; + if ( ++i == (int)store->size() ) + store->resize( store->size() < 256 + ? 1024 : store->size()*4 ); + switch ( *p ) { + case '\0': + store->resize( i ); + return FALSE; + case '\n': + *(store->data()+i) = '\0'; + store->resize( i ); + return TRUE; + } + p++; + } + } else { + while ( n-- > 0 ) { + switch ( *p++ ) { + case '\0': + return FALSE; + case '\n': + return TRUE; + } + } + } + } +} + +int QMembuf::ungetch( int ch ) +{ + if ( buf->isEmpty() || _index==0 ) { + // we need a new QByteArray + QByteArray *ba = new QByteArray( 1 ); + buf->insert( 0, ba ); + _size++; + ba->at( 0 ) = ch; + } else { + // we can reuse a place in the buffer + QByteArray *ba = buf->first(); + _index--; + _size++; + ba->at( _index ) = ch; + } + return ch; +} diff --git a/src/kernel/qinternal_p.h b/src/kernel/qinternal_p.h new file mode 100644 index 0000000..dd85421 --- /dev/null +++ b/src/kernel/qinternal_p.h @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Definition of some shared interal classes +** +** Created : 010427 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QINTERNAL_P_H +#define QINTERNAL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of a number of Qt sources files. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// +// +#ifndef QT_H +#include "qnamespace.h" +#include "qrect.h" +#include "qptrlist.h" +#include "qcstring.h" +#include "qiodevice.h" +#endif // QT_H + +class QWidget; +class QPainter; +class QPixmap; + +class Q_EXPORT QSharedDoubleBuffer +{ +public: + enum DoubleBufferFlags { + NoFlags = 0x00, + InitBG = 0x01, + Force = 0x02, + Default = InitBG | Force + }; + typedef uint DBFlags; + + QSharedDoubleBuffer( DBFlags f = Default ); + QSharedDoubleBuffer( QWidget* widget, + int x = 0, int y = 0, int w = -1, int h = -1, + DBFlags f = Default ); + QSharedDoubleBuffer( QPainter* painter, + int x = 0, int y = 0, int w = -1, int h = -1, + DBFlags f = Default ); + QSharedDoubleBuffer( QWidget *widget, const QRect &r, DBFlags f = Default ); + QSharedDoubleBuffer( QPainter *painter, const QRect &r, DBFlags f = Default ); + ~QSharedDoubleBuffer(); + + bool begin( QWidget* widget, int x = 0, int y = 0, int w = -1, int h = -1 ); + bool begin( QPainter* painter, int x = 0, int y = 0, int w = -1, int h = -1); + bool begin( QWidget* widget, const QRect &r ); + bool begin( QPainter* painter, const QRect &r ); + bool end(); + + QPainter* painter() const; + + bool isActive() const; + bool isBuffered() const; + void flush(); + + static bool isDisabled() { return !dblbufr; } + static void setDisabled( bool off ) { dblbufr = !off; } + + static void cleanup(); + +private: + enum DoubleBufferState { + Active = 0x0100, + BufferActive = 0x0200, + ExternalPainter = 0x0400 + }; + typedef uint DBState; + + QPixmap *getPixmap(); + void releasePixmap(); + + QWidget *wid; + int rx, ry, rw, rh; + DBFlags flags; + DBState state; + + QPainter *p, *external_p; + QPixmap *pix; + + static bool dblbufr; +}; + +inline bool QSharedDoubleBuffer::begin( QWidget* widget, const QRect &r ) +{ return begin( widget, r.x(), r.y(), r.width(), r.height() ); } + +inline bool QSharedDoubleBuffer::begin( QPainter *painter, const QRect &r ) +{ return begin( painter, r.x(), r.y(), r.width(), r.height() ); } + +inline QPainter* QSharedDoubleBuffer::painter() const +{ return p; } + +inline bool QSharedDoubleBuffer::isActive() const +{ return ( state & Active ); } + +inline bool QSharedDoubleBuffer::isBuffered() const +{ return ( state & BufferActive ); } + + +class QVirtualDestructor { +public: + virtual ~QVirtualDestructor() {} +}; + +template <class T> +class QAutoDeleter : public QVirtualDestructor { +public: + QAutoDeleter( T* p ) : ptr( p ) {} + ~QAutoDeleter() { delete ptr; } + T* data() const { return ptr; } +private: + T* ptr; +}; + +template <class T> +T* qAutoDeleterData( QAutoDeleter<T>* ad ) +{ + if ( !ad ) + return 0; + return ad->data(); +} + +template <class T> +QAutoDeleter<T>* qAutoDeleter( T* p ) +{ + return new QAutoDeleter<T>( p ); +} + +class Q_EXPORT QMembuf +{ +public: + QMembuf(); + ~QMembuf(); + + void append( QByteArray *ba ); + void clear(); + + bool consumeBytes( Q_ULONG nbytes, char *sink ); + QByteArray readAll(); + bool scanNewline( QByteArray *store ); + bool canReadLine() const; + + int ungetch( int ch ); + + QIODevice::Offset size() const; + +private: + + QPtrList<QByteArray> *buf; + QIODevice::Offset _size; + QIODevice::Offset _index; +}; + +inline void QMembuf::append( QByteArray *ba ) +{ buf->append( ba ); _size += ba->size(); } + +inline void QMembuf::clear() +{ buf->clear(); _size=0; _index=0; } + +inline QByteArray QMembuf::readAll() +{ QByteArray ba(_size); consumeBytes(_size,ba.data()); return ba; } + +inline bool QMembuf::canReadLine() const +{ return ((QMembuf*)this)->scanNewline( 0 ); } + +inline QIODevice::Offset QMembuf::size() const +{ return _size; } + +#endif // QINTERNAL_P_H diff --git a/src/kernel/qjpegio.cpp b/src/kernel/qjpegio.cpp new file mode 100644 index 0000000..2c7556e --- /dev/null +++ b/src/kernel/qjpegio.cpp @@ -0,0 +1,599 @@ +/**************************************************************************** +** +** Implementation of JPEG QImage IOHandler +** +** Created : 990521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QT_CLEAN_NAMESPACE +#define QT_CLEAN_NAMESPACE +#endif + +#include "qimage.h" + +#ifndef QT_NO_IMAGEIO_JPEG + +#include "qiodevice.h" +#include "qjpegio.h" + +#include <stdio.h> // jpeglib needs this to be pre-included +#include <setjmp.h> + + +// including jpeglib.h seems to be a little messy +extern "C" { +#define XMD_H // shut JPEGlib up +#if defined(Q_OS_UNIXWARE) +# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this +#endif +#include <jpeglib.h> +#ifdef const +# undef const // remove crazy C hackery in jconfig.h +#endif +} + + +struct my_error_mgr : public jpeg_error_mgr { + jmp_buf setjmp_buffer; +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void my_error_exit (j_common_ptr cinfo) +{ + my_error_mgr* myerr = (my_error_mgr*) cinfo->err; + char buffer[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, buffer); + qWarning(buffer); + longjmp(myerr->setjmp_buffer, 1); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +static const int max_buf = 4096; + +struct my_jpeg_source_mgr : public jpeg_source_mgr { + // Nothing dynamic - cannot rely on destruction over longjump + QImageIO* iio; + JOCTET buffer[max_buf]; + +public: + my_jpeg_source_mgr(QImageIO* iio); +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void qt_init_source(j_decompress_ptr) +{ +} + +static +boolean qt_fill_input_buffer(j_decompress_ptr cinfo) +{ + int num_read; + my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; + QIODevice* dev = src->iio->ioDevice(); + src->next_input_byte = src->buffer; + num_read = dev->readBlock((char*)src->buffer, max_buf); + if ( num_read <= 0 ) { + // Insert a fake EOI marker - as per jpeglib recommendation + src->buffer[0] = (JOCTET) 0xFF; + src->buffer[1] = (JOCTET) JPEG_EOI; + src->bytes_in_buffer = 2; + } else { + src->bytes_in_buffer = num_read; + } +#if defined(Q_OS_UNIXWARE) + return B_TRUE; +#else + return TRUE; +#endif +} + +static +void qt_skip_input_data(j_decompress_ptr cinfo, long num_bytes) +{ + my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; + + // `dumb' implementation from jpeglib + + /* Just a dumb implementation for now. Could use fseek() except + * it doesn't work on pipes. Not clear that being smart is worth + * any trouble anyway --- large skips are infrequent. + */ + if (num_bytes > 0) { + while (num_bytes > (long) src->bytes_in_buffer) { + num_bytes -= (long) src->bytes_in_buffer; + (void) qt_fill_input_buffer(cinfo); + /* note we assume that qt_fill_input_buffer will never return FALSE, + * so suspension need not be handled. + */ + } + src->next_input_byte += (size_t) num_bytes; + src->bytes_in_buffer -= (size_t) num_bytes; + } +} + +static +void qt_term_source(j_decompress_ptr) +{ +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +inline my_jpeg_source_mgr::my_jpeg_source_mgr(QImageIO* iioptr) +{ + jpeg_source_mgr::init_source = qt_init_source; + jpeg_source_mgr::fill_input_buffer = qt_fill_input_buffer; + jpeg_source_mgr::skip_input_data = qt_skip_input_data; + jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart; + jpeg_source_mgr::term_source = qt_term_source; + iio = iioptr; + bytes_in_buffer = 0; + next_input_byte = buffer; +} + + +static +void scaleSize( int &reqW, int &reqH, int imgW, int imgH, QImage::ScaleMode mode ) +{ + if ( mode == QImage::ScaleFree ) + return; + int t1 = imgW * reqH; + int t2 = reqW * imgH; + if (( mode == QImage::ScaleMin && (t1 > t2) ) || ( mode == QImage::ScaleMax && (t1 < t2) )) + reqH = t2 / imgW; + else + reqW = t1 / imgH; +} + + +static +void read_jpeg_image(QImageIO* iio) +{ + QImage image; + + struct jpeg_decompress_struct cinfo; + + struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(iio); + struct my_error_mgr jerr; + + jpeg_create_decompress(&cinfo); + + cinfo.src = iod_src; + + cinfo.err = jpeg_std_error(&jerr); + jerr.error_exit = my_error_exit; + + if (!setjmp(jerr.setjmp_buffer)) { +#if defined(Q_OS_UNIXWARE) + (void) jpeg_read_header(&cinfo, B_TRUE); +#else + (void) jpeg_read_header(&cinfo, TRUE); +#endif + + (void) jpeg_start_decompress(&cinfo); + + QString params = iio->parameters(); + params.simplifyWhiteSpace(); + int sWidth = 0, sHeight = 0; + char sModeStr[1024] = ""; + QImage::ScaleMode sMode; + + if ( params.contains( "GetHeaderInformation" ) ) { + + // Create QImage's without allocating the data + if ( cinfo.output_components == 3 || cinfo.output_components == 4) { + image = QImage( NULL, cinfo.output_width, cinfo.output_height, 32, NULL, 0, QImage::IgnoreEndian ); + } else if ( cinfo.output_components == 1 ) { + image = QImage( NULL, cinfo.output_width, cinfo.output_height, 8, NULL, 0, QImage::IgnoreEndian ); + } else { + // Unsupported format + } + + + } else if ( params.contains( "Scale" ) ) { + sscanf( params.latin1(), "Scale( %i, %i, %1023s )", + &sWidth, &sHeight, sModeStr ); + + QString sModeQStr( sModeStr ); + if ( sModeQStr == "ScaleFree" ) { + sMode = QImage::ScaleFree; + } else if ( sModeQStr == "ScaleMin" ) { + sMode = QImage::ScaleMin; + } else if ( sModeQStr == "ScaleMax" ) { + sMode = QImage::ScaleMax; + } else { + qDebug("read_jpeg_image: invalid scale mode \"%s\", see QImage::ScaleMode documentation", sModeStr); + sMode = QImage::ScaleFree; + } + +// qDebug( "Parameters ask to scale the image to %i x %i ScaleMode: %s", sWidth, sHeight, sModeStr ); + scaleSize( sWidth, sHeight, cinfo.output_width, cinfo.output_height, sMode ); +// qDebug( "Scaling the jpeg to %i x %i", sWidth, sHeight, sModeStr ); + + bool created = FALSE; + if ( cinfo.output_components == 3 || cinfo.output_components == 4) { + created = image.create( sWidth, sHeight, 32 ); + } else if ( cinfo.output_components == 1 ) { + created = image.create( sWidth, sHeight, 8, 256 ); + for (int i=0; i<256; i++) + image.setColor(i, qRgb(i,i,i)); + } else { + // Unsupported format + } + if (!created) + image = QImage(); + + if (!image.isNull()) { + QImage tmpImage( cinfo.output_width, 1, 32 ); + uchar** inLines = tmpImage.jumpTable(); + uchar** outLines = image.jumpTable(); + while (cinfo.output_scanline < cinfo.output_height) { + int outputLine = sHeight * cinfo.output_scanline / cinfo.output_height; + (void) jpeg_read_scanlines(&cinfo, inLines, 1); + if ( cinfo.output_components == 3 ) { + uchar *in = inLines[0]; + QRgb *out = (QRgb*)outLines[outputLine]; + for (uint i=0; i<cinfo.output_width; i++ ) { +// ### Only scaling down an image works, I don't think scaling up will work at the moment +// ### An idea I have to make this a smooth scale is to progressively add the pixel values up +// When scaling down, multiple values are being over drawn in to the output buffer. +// Instead, a weighting based on the distance the line or pixel is from the output pixel determines +// the weight of it when added to the output buffer. At present it is a non-smooth scale which is +// inefficently implemented, it still uncompresses all the jpeg, an optimization for progressive +// jpegs could be made if scaling by say 50% or some other special cases + out[sWidth * i / cinfo.output_width] = qRgb( in[0], in[1], in[2] ); + in += 3; + } + } else { +// ### Need to test the case where the jpeg is grayscale, need some black and white jpegs to test +// this code. (also only scales down and probably won't scale to a larger size) + uchar *in = inLines[0]; + uchar *out = outLines[outputLine]; + for (uint i=0; i<cinfo.output_width; i++ ) { + out[sWidth * i / cinfo.output_width] = in[i]; + } + } + } + (void) jpeg_finish_decompress(&cinfo); + } + + } else { + + bool created = FALSE; + if ( cinfo.output_components == 3 || cinfo.output_components == 4) { + created = image.create( cinfo.output_width, cinfo.output_height, 32 ); + } else if ( cinfo.output_components == 1 ) { + created = image.create( cinfo.output_width, cinfo.output_height, 8, 256 ); + for (int i=0; i<256; i++) + image.setColor(i, qRgb(i,i,i)); + } else { + // Unsupported format + } + if (!created) + image = QImage(); + + if (!image.isNull()) { + uchar** lines = image.jumpTable(); + while (cinfo.output_scanline < cinfo.output_height) + (void) jpeg_read_scanlines(&cinfo, + lines + cinfo.output_scanline, + cinfo.output_height); + (void) jpeg_finish_decompress(&cinfo); + + if ( cinfo.output_components == 3 ) { + // Expand 24->32 bpp. + for (uint j=0; j<cinfo.output_height; j++) { + uchar *in = image.scanLine(j) + cinfo.output_width * 3; + QRgb *out = (QRgb*)image.scanLine(j); + + for (uint i=cinfo.output_width; i--; ) { + in-=3; + out[i] = qRgb(in[0], in[1], in[2]); + } + } + } + } + } + + if (!image.isNull()) { + if ( cinfo.density_unit == 1 ) { + image.setDotsPerMeterX( int(100. * cinfo.X_density / 2.54) ); + image.setDotsPerMeterY( int(100. * cinfo.Y_density / 2.54) ); + } else if ( cinfo.density_unit == 2 ) { + image.setDotsPerMeterX( int(100. * cinfo.X_density) ); + image.setDotsPerMeterY( int(100. * cinfo.Y_density) ); + } + } + + iio->setImage(image); + iio->setStatus(image.isNull()); + } + + jpeg_destroy_decompress(&cinfo); + delete iod_src; +} + + +struct my_jpeg_destination_mgr : public jpeg_destination_mgr { + // Nothing dynamic - cannot rely on destruction over longjump + QImageIO* iio; + JOCTET buffer[max_buf]; + +public: + my_jpeg_destination_mgr(QImageIO*); +}; + + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void qt_init_destination(j_compress_ptr) +{ +} + +static +void qt_exit_on_error(j_compress_ptr cinfo, QIODevice* dev) +{ + if (dev->status() == IO_Ok) { + return; + } else { + // cinfo->err->msg_code = JERR_FILE_WRITE; + (*cinfo->err->error_exit)((j_common_ptr)cinfo); + } +} + +static +boolean qt_empty_output_buffer(j_compress_ptr cinfo) +{ + my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; + QIODevice* dev = dest->iio->ioDevice(); + + if ( dev->writeBlock( (char*)dest->buffer, max_buf ) != max_buf ) + qt_exit_on_error(cinfo, dev); + + dest->next_output_byte = dest->buffer; + dest->free_in_buffer = max_buf; + +#if defined(Q_OS_UNIXWARE) + return B_TRUE; +#else + return TRUE; +#endif +} + +static +void qt_term_destination(j_compress_ptr cinfo) +{ + my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; + QIODevice* dev = dest->iio->ioDevice(); + Q_LONG n = max_buf - dest->free_in_buffer; + + if ( dev->writeBlock( (char*)dest->buffer, n ) != n ) + qt_exit_on_error(cinfo, dev); + + dev->flush(); + + qt_exit_on_error(cinfo, dev); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +inline +my_jpeg_destination_mgr::my_jpeg_destination_mgr(QImageIO* iioptr) +{ + jpeg_destination_mgr::init_destination = qt_init_destination; + jpeg_destination_mgr::empty_output_buffer = qt_empty_output_buffer; + jpeg_destination_mgr::term_destination = qt_term_destination; + iio = iioptr; + next_output_byte = buffer; + free_in_buffer = max_buf; +} + + +static +void write_jpeg_image(QImageIO* iio) +{ + QImage image = iio->image(); + + struct jpeg_compress_struct cinfo; + JSAMPROW row_pointer[1]; + row_pointer[0] = 0; + + struct my_jpeg_destination_mgr *iod_dest = new my_jpeg_destination_mgr(iio); + struct my_error_mgr jerr; + + cinfo.err = jpeg_std_error(&jerr); + + jerr.error_exit = my_error_exit; + + if (!setjmp(jerr.setjmp_buffer)) { + jpeg_create_compress(&cinfo); + + cinfo.dest = iod_dest; + + cinfo.image_width = image.width(); + cinfo.image_height = image.height(); + + QRgb* cmap=0; + bool gray=FALSE; + switch ( image.depth() ) { + case 1: + case 8: + cmap = image.colorTable(); + gray = TRUE; + int i; + for (i=image.numColors(); gray && i--; ) { + gray = gray & ( qRed(cmap[i]) == qGreen(cmap[i]) && + qRed(cmap[i]) == qBlue(cmap[i]) ); + } + cinfo.input_components = gray ? 1 : 3; + cinfo.in_color_space = gray ? JCS_GRAYSCALE : JCS_RGB; + break; + case 32: + cinfo.input_components = 3; + cinfo.in_color_space = JCS_RGB; + } + + jpeg_set_defaults(&cinfo); + + float diffInch = QABS(image.dotsPerMeterX()*2.54/100. - qRound(image.dotsPerMeterX()*2.54/100.)) + + QABS(image.dotsPerMeterY()*2.54/100. - qRound(image.dotsPerMeterY()*2.54/100.)); + float diffCm = (QABS(image.dotsPerMeterX()/100. - qRound(image.dotsPerMeterX()/100.)) + + QABS(image.dotsPerMeterY()/100. - qRound(image.dotsPerMeterY()/100.)))*2.54; + if (diffInch < diffCm) { + cinfo.density_unit = 1; // dots/inch + cinfo.X_density = qRound(image.dotsPerMeterX()*2.54/100.); + cinfo.Y_density = qRound(image.dotsPerMeterY()*2.54/100.); + } else { + cinfo.density_unit = 2; // dots/cm + cinfo.X_density = (image.dotsPerMeterX()+50) / 100; + cinfo.Y_density = (image.dotsPerMeterY()+50) / 100; + } + + int quality = iio->quality() >= 0 ? QMIN(iio->quality(),100) : 75; +#if defined(Q_OS_UNIXWARE) + jpeg_set_quality(&cinfo, quality, B_TRUE /* limit to baseline-JPEG values */); + jpeg_start_compress(&cinfo, B_TRUE); +#else + jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); + jpeg_start_compress(&cinfo, TRUE); +#endif + + row_pointer[0] = new uchar[cinfo.image_width*cinfo.input_components]; + int w = cinfo.image_width; + while (cinfo.next_scanline < cinfo.image_height) { + uchar *row = row_pointer[0]; + switch ( image.depth() ) { + case 1: + if (gray) { + uchar* data = image.scanLine(cinfo.next_scanline); + if ( image.bitOrder() == QImage::LittleEndian ) { + for (int i=0; i<w; i++) { + bool bit = !!(*(data + (i >> 3)) & (1 << (i & 7))); + row[i] = qRed(cmap[bit]); + } + } else { + for (int i=0; i<w; i++) { + bool bit = !!(*(data + (i >> 3)) & (1 << (7 -(i & 7)))); + row[i] = qRed(cmap[bit]); + } + } + } else { + uchar* data = image.scanLine(cinfo.next_scanline); + if ( image.bitOrder() == QImage::LittleEndian ) { + for (int i=0; i<w; i++) { + bool bit = !!(*(data + (i >> 3)) & (1 << (i & 7))); + *row++ = qRed(cmap[bit]); + *row++ = qGreen(cmap[bit]); + *row++ = qBlue(cmap[bit]); + } + } else { + for (int i=0; i<w; i++) { + bool bit = !!(*(data + (i >> 3)) & (1 << (7 -(i & 7)))); + *row++ = qRed(cmap[bit]); + *row++ = qGreen(cmap[bit]); + *row++ = qBlue(cmap[bit]); + } + } + } + break; + case 8: + if (gray) { + uchar* pix = image.scanLine(cinfo.next_scanline); + for (int i=0; i<w; i++) { + *row = qRed(cmap[*pix]); + ++row; ++pix; + } + } else { + uchar* pix = image.scanLine(cinfo.next_scanline); + for (int i=0; i<w; i++) { + *row++ = qRed(cmap[*pix]); + *row++ = qGreen(cmap[*pix]); + *row++ = qBlue(cmap[*pix]); + ++pix; + } + } + break; + case 32: { + QRgb* rgb = (QRgb*)image.scanLine(cinfo.next_scanline); + for (int i=0; i<w; i++) { + *row++ = qRed(*rgb); + *row++ = qGreen(*rgb); + *row++ = qBlue(*rgb); + ++rgb; + } + } + } + jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); + + iio->setStatus(0); + } + + delete iod_dest; + delete [] row_pointer[0]; +} + +void qInitJpegIO() +{ + // Not much to go on - just 3 bytes: 0xFF, M_SOI, 0xFF + // Even the third is not strictly specified as required. + QImageIO::defineIOHandler("JPEG", "^\377\330\377", 0, read_jpeg_image, write_jpeg_image); +} + +#endif diff --git a/src/kernel/qjpegio.h b/src/kernel/qjpegio.h new file mode 100644 index 0000000..ff59410 --- /dev/null +++ b/src/kernel/qjpegio.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Definition of JPEG QImage IOHandler +** +** Created : 970621 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QJPEGIO_H +#define QJPEGIO_H + +#include "qglobal.h" + +#ifndef QT_NO_IMAGEIO_JPEG + +void qInitJpegIO(); + +#endif // QT_NO_IMAGEIO_JPEG + +#endif // QJPEGIO_H diff --git a/src/kernel/qkeycode.h b/src/kernel/qkeycode.h new file mode 100644 index 0000000..54a41d9 --- /dev/null +++ b/src/kernel/qkeycode.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Definition of keyboard codes +** +** Created : 931030 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QKEYCODE_H +#define QKEYCODE_H + +#ifndef QT_H +#include "qnamespace.h" +#endif // QT_H + +// all key codes are now in the Qt namespace class + +#endif // QKEYCODE_H diff --git a/src/kernel/qkeysequence.cpp b/src/kernel/qkeysequence.cpp new file mode 100644 index 0000000..7263d4a --- /dev/null +++ b/src/kernel/qkeysequence.cpp @@ -0,0 +1,731 @@ +/**************************************************************************** +** +** Implementation of QKeySequence class +** +** Created : 0108007 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qkeysequence.h" + +#ifndef QT_NO_ACCEL + +#include "qaccel.h" +#include "qshared.h" +#include "qvaluelist.h" +#ifndef QT_NO_REGEXP +# include "qregexp.h" +#endif + +#ifdef Q_WS_MAC +#define QMAC_CTRL (QString(QChar(0x2318))) +#define QMAC_META (QString(QChar(0x2303))) +#define QMAC_ALT (QString(QChar(0x2325))) +#define QMAC_SHIFT (QString(QChar(0x21E7))) +#endif + +/*! + \class QKeySequence qkeysequence.h + \brief The QKeySequence class encapsulates a key sequence as used + by accelerators. + + \ingroup misc + + A key sequence consists of up to four keyboard codes, each + optionally combined with modifiers, e.g. \c SHIFT, \c CTRL, \c + ALT, \c META, or \c UNICODE_ACCEL. For example, \c{CTRL + Key_P} + might be a sequence used as a shortcut for printing a document. + The key codes are listed in \c{qnamespace.h}. As an alternative, + use \c UNICODE_ACCEL with the unicode code point of the character. + For example, \c{UNICODE_ACCEL + 'A'} gives the same key sequence + as \c Key_A. + + Key sequences can be constructed either from an integer key code, + or from a human readable translatable string such as + "Ctrl+X,Alt+Space". A key sequence can be cast to a QString to + obtain a human readable translated version of the sequence. + Translations are done in the "QAccel" context. + + \sa QAccel +*/ + +/*! + \enum Qt::SequenceMatch + + \value NoMatch Sequences have nothing in common + \value PartialMatch Sequences match partially, but are not complete + \value Identical Sequences do not differ +*/ + +static struct { + int key; + const char* name; +} keyname[] = { + { Qt::Key_Space, QT_TRANSLATE_NOOP( "QAccel", "Space" ) }, + { Qt::Key_Escape, QT_TRANSLATE_NOOP( "QAccel", "Esc" ) }, + { Qt::Key_Tab, QT_TRANSLATE_NOOP( "QAccel", "Tab" ) }, + { Qt::Key_Backtab, QT_TRANSLATE_NOOP( "QAccel", "Backtab" ) }, + { Qt::Key_Backspace, QT_TRANSLATE_NOOP( "QAccel", "Backspace" ) }, + { Qt::Key_Return, QT_TRANSLATE_NOOP( "QAccel", "Return" ) }, + { Qt::Key_Enter, QT_TRANSLATE_NOOP( "QAccel", "Enter" ) }, + { Qt::Key_Insert, QT_TRANSLATE_NOOP( "QAccel", "Ins" ) }, + { Qt::Key_Delete, QT_TRANSLATE_NOOP( "QAccel", "Del" ) }, + { Qt::Key_Pause, QT_TRANSLATE_NOOP( "QAccel", "Pause" ) }, + { Qt::Key_Print, QT_TRANSLATE_NOOP( "QAccel", "Print" ) }, + { Qt::Key_SysReq, QT_TRANSLATE_NOOP( "QAccel", "SysReq" ) }, + { Qt::Key_Home, QT_TRANSLATE_NOOP( "QAccel", "Home" ) }, + { Qt::Key_End, QT_TRANSLATE_NOOP( "QAccel", "End" ) }, + { Qt::Key_Left, QT_TRANSLATE_NOOP( "QAccel", "Left" ) }, + { Qt::Key_Up, QT_TRANSLATE_NOOP( "QAccel", "Up" ) }, + { Qt::Key_Right, QT_TRANSLATE_NOOP( "QAccel", "Right" ) }, + { Qt::Key_Down, QT_TRANSLATE_NOOP( "QAccel", "Down" ) }, + { Qt::Key_Prior, QT_TRANSLATE_NOOP( "QAccel", "PgUp" ) }, + { Qt::Key_Next, QT_TRANSLATE_NOOP( "QAccel", "PgDown" ) }, + { Qt::Key_CapsLock, QT_TRANSLATE_NOOP( "QAccel", "CapsLock" ) }, + { Qt::Key_NumLock, QT_TRANSLATE_NOOP( "QAccel", "NumLock" ) }, + { Qt::Key_ScrollLock, QT_TRANSLATE_NOOP( "QAccel", "ScrollLock" ) }, + { Qt::Key_Menu, QT_TRANSLATE_NOOP( "QAccel", "Menu" ) }, + { Qt::Key_Help, QT_TRANSLATE_NOOP( "QAccel", "Help" ) }, + + // Multimedia keys + { Qt::Key_Back, QT_TRANSLATE_NOOP( "QAccel", "Back" ) }, + { Qt::Key_Forward, QT_TRANSLATE_NOOP( "QAccel", "Forward" ) }, + { Qt::Key_Stop, QT_TRANSLATE_NOOP( "QAccel", "Stop" ) }, + { Qt::Key_Refresh, QT_TRANSLATE_NOOP( "QAccel", "Refresh" ) }, + { Qt::Key_VolumeDown, QT_TRANSLATE_NOOP( "QAccel", "Volume Down" ) }, + { Qt::Key_VolumeMute, QT_TRANSLATE_NOOP( "QAccel", "Volume Mute" ) }, + { Qt::Key_VolumeUp, QT_TRANSLATE_NOOP( "QAccel", "Volume Up" ) }, + { Qt::Key_BassBoost, QT_TRANSLATE_NOOP( "QAccel", "Bass Boost" ) }, + { Qt::Key_BassUp, QT_TRANSLATE_NOOP( "QAccel", "Bass Up" ) }, + { Qt::Key_BassDown, QT_TRANSLATE_NOOP( "QAccel", "Bass Down" ) }, + { Qt::Key_TrebleUp, QT_TRANSLATE_NOOP( "QAccel", "Treble Up" ) }, + { Qt::Key_TrebleDown, QT_TRANSLATE_NOOP( "QAccel", "Treble Down" ) }, + { Qt::Key_MediaPlay, QT_TRANSLATE_NOOP( "QAccel", "Media Play" ) }, + { Qt::Key_MediaStop, QT_TRANSLATE_NOOP( "QAccel", "Media Stop" ) }, + { Qt::Key_MediaPrev, QT_TRANSLATE_NOOP( "QAccel", "Media Previous" ) }, + { Qt::Key_MediaNext, QT_TRANSLATE_NOOP( "QAccel", "Media Next" ) }, + { Qt::Key_MediaRecord, QT_TRANSLATE_NOOP( "QAccel", "Media Record" ) }, + { Qt::Key_HomePage, QT_TRANSLATE_NOOP( "QAccel", "Home" ) }, + { Qt::Key_Favorites, QT_TRANSLATE_NOOP( "QAccel", "Favorites" ) }, + { Qt::Key_Search, QT_TRANSLATE_NOOP( "QAccel", "Search" ) }, + { Qt::Key_Standby, QT_TRANSLATE_NOOP( "QAccel", "Standby" ) }, + { Qt::Key_OpenUrl, QT_TRANSLATE_NOOP( "QAccel", "Open URL" ) }, + { Qt::Key_LaunchMail, QT_TRANSLATE_NOOP( "QAccel", "Launch Mail" ) }, + { Qt::Key_LaunchMedia, QT_TRANSLATE_NOOP( "QAccel", "Launch Media" ) }, + { Qt::Key_Launch0, QT_TRANSLATE_NOOP( "QAccel", "Launch (0)" ) }, + { Qt::Key_Launch1, QT_TRANSLATE_NOOP( "QAccel", "Launch (1)" ) }, + { Qt::Key_Launch2, QT_TRANSLATE_NOOP( "QAccel", "Launch (2)" ) }, + { Qt::Key_Launch3, QT_TRANSLATE_NOOP( "QAccel", "Launch (3)" ) }, + { Qt::Key_Launch4, QT_TRANSLATE_NOOP( "QAccel", "Launch (4)" ) }, + { Qt::Key_Launch5, QT_TRANSLATE_NOOP( "QAccel", "Launch (5)" ) }, + { Qt::Key_Launch6, QT_TRANSLATE_NOOP( "QAccel", "Launch (6)" ) }, + { Qt::Key_Launch7, QT_TRANSLATE_NOOP( "QAccel", "Launch (7)" ) }, + { Qt::Key_Launch8, QT_TRANSLATE_NOOP( "QAccel", "Launch (8)" ) }, + { Qt::Key_Launch9, QT_TRANSLATE_NOOP( "QAccel", "Launch (9)" ) }, + { Qt::Key_LaunchA, QT_TRANSLATE_NOOP( "QAccel", "Launch (A)" ) }, + { Qt::Key_LaunchB, QT_TRANSLATE_NOOP( "QAccel", "Launch (B)" ) }, + { Qt::Key_LaunchC, QT_TRANSLATE_NOOP( "QAccel", "Launch (C)" ) }, + { Qt::Key_LaunchD, QT_TRANSLATE_NOOP( "QAccel", "Launch (D)" ) }, + { Qt::Key_LaunchE, QT_TRANSLATE_NOOP( "QAccel", "Launch (E)" ) }, + { Qt::Key_LaunchF, QT_TRANSLATE_NOOP( "QAccel", "Launch (F)" ) }, + + // -------------------------------------------------------------- + // More consistent namings + { Qt::Key_Print, QT_TRANSLATE_NOOP( "QAccel", "Print Screen" ) }, + { Qt::Key_Prior, QT_TRANSLATE_NOOP( "QAccel", "Page Up" ) }, + { Qt::Key_Next, QT_TRANSLATE_NOOP( "QAccel", "Page Down" ) }, + { Qt::Key_CapsLock, QT_TRANSLATE_NOOP( "QAccel", "Caps Lock" ) }, + { Qt::Key_NumLock, QT_TRANSLATE_NOOP( "QAccel", "Num Lock" ) }, + { Qt::Key_NumLock, QT_TRANSLATE_NOOP( "QAccel", "Number Lock" ) }, + { Qt::Key_ScrollLock, QT_TRANSLATE_NOOP( "QAccel", "Scroll Lock" ) }, + { Qt::Key_Insert, QT_TRANSLATE_NOOP( "QAccel", "Insert" ) }, + { Qt::Key_Delete, QT_TRANSLATE_NOOP( "QAccel", "Delete" ) }, + { Qt::Key_Escape, QT_TRANSLATE_NOOP( "QAccel", "Escape" ) }, + { Qt::Key_SysReq, QT_TRANSLATE_NOOP( "QAccel", "System Request" ) }, + + { 0, 0 } +}; + + +class QKeySequencePrivate : public QShared +{ +public: + inline QKeySequencePrivate() + { + key[0] = key[1] = key[2] = key[3] = 0; + } + inline QKeySequencePrivate( QKeySequencePrivate *copy ) + { + key[0] = copy->key[0]; + key[1] = copy->key[1]; + key[2] = copy->key[2]; + key[3] = copy->key[3]; + } + int key[4]; +}; + + +/*! + Constructs an empty key sequence. +*/ +QKeySequence::QKeySequence() +{ + d = new QKeySequencePrivate(); + Q_CHECK_PTR( d ); +} + +/*! + Creates a key sequence from the string \a key. For example + "Ctrl+O" gives CTRL+UNICODE_ACCEL+'O'. The strings "Ctrl", + "Shift", "Alt" and "Meta" are recognized, as well as their + translated equivalents in the "QAccel" context (using + QObject::tr()). + + Multiple key codes (up to four) may be entered by separating them + with commas, e.g. "Alt+X,Ctrl+S,Q". + + This contructor is typically used with \link QObject::tr() tr + \endlink(), so that accelerator keys can be replaced in + translations: + + \code + QPopupMenu *file = new QPopupMenu( this ); + file->insertItem( tr("&Open..."), this, SLOT(open()), + QKeySequence( tr("Ctrl+O", "File|Open") ) ); + \endcode + + Note the \c "File|Open" translator comment. It is by no means + necessary, but it provides some context for the human translator. +*/ +QKeySequence::QKeySequence( const QString& key ) +{ + d = new QKeySequencePrivate(); + Q_CHECK_PTR( d ); + assign( key ); +} + + +// ### BCI: Merge with constructor below for 4.0 +/*! + Constructs a key sequence that has a single \a key. + + The key codes are listed in \c{qnamespace.h} and can be + combined with modifiers, e.g. with \c SHIFT, \c CTRL, \c + ALT, \c META or \c UNICODE_ACCEL. +*/ +QKeySequence::QKeySequence( int key ) +{ + d = new QKeySequencePrivate(); + Q_CHECK_PTR( d ); + d->key[0] = key; +} + +/*! + Constructs a key sequence with up to 4 keys \a k1, \a k2, + \a k3 and \a k4. + + The key codes are listed in \c{qnamespace.h} and can be + combined with modifiers, e.g. with \c SHIFT, \c CTRL, \c + ALT, \c META or \c UNICODE_ACCEL. +*/ +QKeySequence::QKeySequence( int k1, int k2, int k3, int k4 ) +{ + d = new QKeySequencePrivate(); + Q_CHECK_PTR( d ); + d->key[0] = k1; + d->key[1] = k2; + d->key[2] = k3; + d->key[3] = k4; +} + +/*! + Copy constructor. Makes a copy of \a keysequence. + */ +QKeySequence::QKeySequence( const QKeySequence& keysequence ) + : d( keysequence.d ) +{ + d->ref(); +} + + +/*! + Destroys the key sequence. + */ +QKeySequence::~QKeySequence() +{ + if ( d->deref() ) + delete d; +} + +/*! + \internal + KeySequences should never be modified, but rather just created. + Internally though we do need to modify to keep pace in event + delivery. +*/ + +void QKeySequence::setKey( int key, int index ) +{ +#ifdef QT_CHECK_STATE + if ( 0 > index && 4 < index ) { + qWarning( "QKeySequence::setKey: index %u out of range", index ); + return; + } +#endif // QT_CHECK_STATE + + if ( 1 < d->count ) { + QKeySequencePrivate *newd = new QKeySequencePrivate( d ); + d->deref(); + d = newd; + } + d->key[index] = key; +} + +/*! + Returns the number of keys in the key sequence. + The maximum is 4. + */ +uint QKeySequence::count() const +{ + if ( ! d->key[0] ) + return 0; + if ( ! d->key[1] ) + return 1; + if ( ! d->key[2] ) + return 2; + if ( ! d->key[3] ) + return 3; + return 4; +} + + +/*! + Returns TRUE if the key sequence is empty; otherwise returns + FALSE. +*/ +bool QKeySequence::isEmpty() const +{ + return !d->key[0]; +} + + +/*! + Adds the string \a keyseq to the key sequence. \a keyseq may + contain up to four key codes, provided they are seperated by a + comma, e.g. "Alt+X,Ctrl+S,Z"). Returns the number of key codes + added. +*/ +int QKeySequence::assign( QString keyseq ) +{ + QString part; + int n = 0; + int p = 0, diff = 0; + + // Run through the whole string, but stop + // if we have 4 keys before the end. + while ( keyseq.length() && n < 4 ) { + // We MUST use something to seperate each sequence, and space + // does not cut it, since some of the key names have space + // in them.. (Let's hope no one translate with a comma in it:) + p = keyseq.find( ',' ); + if ( -1 != p ) { + if ( ',' == keyseq[p+1] ) // e.g. 'Ctrl+,, Shift+,,' + p++; + if ( ' ' == keyseq[p+1] ) { // Space after comma + diff = 1; + p++; + } else if ( '\0' == keyseq[p+1] ) { // Last comma 'Ctrl+,' + p = -1; + } else { + diff = 0; + } + } + part = keyseq.left( -1==p?keyseq.length():p-diff ); + keyseq = keyseq.right( -1==p?0:keyseq.length() - ( p + 1 ) ); + d->key[n] = decodeString( part ); + n++; + } + return n; +} + +struct ModifKeyName { + ModifKeyName() { } + ModifKeyName(int q, QString n) : qt_key(q), name(n) { } + int qt_key; + QString name; +}; + +/*! + Constructs a single key from the string \str. + */ +int QKeySequence::decodeString( const QString& str ) +{ + int ret = 0; + QString accel = str; + + QValueList<ModifKeyName> modifs; +#ifdef QMAC_CTRL + modifs << ModifKeyName( CTRL, QMAC_CTRL ); +#endif +#ifdef QMAC_ALT + modifs << ModifKeyName( ALT, QMAC_ALT ); +#endif +#ifdef QMAC_META + modifs << ModifKeyName( META, QMAC_META ); +#endif +#ifdef QMAC_SHIFT + modifs << ModifKeyName( SHIFT, QMAC_SHIFT ); +#endif + modifs << ModifKeyName( CTRL, "ctrl+" ) << ModifKeyName( CTRL, QAccel::tr("Ctrl").lower().append('+') ); + modifs << ModifKeyName( SHIFT, "shift+" ) << ModifKeyName( SHIFT, QAccel::tr("Shift").lower().append('+') ); + modifs << ModifKeyName( ALT, "alt+" ) << ModifKeyName( ALT, QAccel::tr("Alt").lower().append('+') ); + modifs << ModifKeyName( META, "meta+" ) << ModifKeyName( ALT, QAccel::tr("Meta").lower().append('+') ); + QString sl = accel.lower(); + for( QValueList<ModifKeyName>::iterator it = modifs.begin(); it != modifs.end(); ++it ) { + if ( sl.contains( (*it).name ) ) { + ret |= (*it).qt_key; +#ifndef QT_NO_REGEXP + accel.remove( QRegExp(QRegExp::escape((*it).name), FALSE) ); +#else + accel.remove( (*it).name ); +#endif + sl = accel.lower(); + } + } + + int p = accel.findRev( '+', str.length() - 2 ); // -2 so that Ctrl++ works + if( p > 0 ) + accel = accel.mid( p + 1 ); + + int fnum = 0; + if ( accel.length() == 1 ) { + char ltr = accel[0].upper().latin1(); + // We can only upper A-Z without problems. + if ( ltr < (char)Key_A || ltr > (char)Key_Z ) + ret |= accel[0].unicode(); + else + ret |= accel[0].upper().unicode(); + ret |= UNICODE_ACCEL; + } else if ( accel[0] == 'F' && (fnum = accel.mid(1).toInt()) && (fnum >= 1) && (fnum <= 35) ) { + ret |= Key_F1 + fnum - 1; + } else { + // Check through translation table for the correct key name + // ...or fall back on english table. + bool found = FALSE; + for ( int tran = 0; tran < 2; tran++ ) { + for ( int i = 0; keyname[i].name; i++ ) { + if ( tran ? accel == QAccel::tr(keyname[i].name) + : accel == keyname[i].name ) { + ret |= keyname[i].key; + found = TRUE; + break; + } + } + if(found) + break; + } + } + return ret; +} + + +/*! + Creates an accelerator string for \a key. For example, + CTRL+Key_O gives "Ctrl+O". The strings, "Ctrl", "Shift", etc. are + translated (using QObject::tr()) in the "QAccel" context. + */ +QString QKeySequence::encodeString( int key ) +{ + QString s; +#if defined(Q_OS_MAC) && !defined(QWS) + // On MAC the order is Meta, Alt, Shift, Control. + if ( (key & META) == META ) + s += QMAC_META; + if ( (key & ALT) == ALT ) + s += QMAC_ALT; + if ( (key & SHIFT) == SHIFT ) + s += QMAC_SHIFT; + if ( (key & CTRL) == CTRL ) + s += QMAC_CTRL; +#else + // On other systems the order is Meta, Control, Alt, Shift + if ( (key & META) == META ) + s += QAccel::tr( "Meta" ); + if ( (key & CTRL) == CTRL ) { + if ( !s.isEmpty() ) + s += QAccel::tr( "+" ); + s += QAccel::tr( "Ctrl" ); + } + if ( (key & ALT) == ALT ) { + if ( !s.isEmpty() ) + s += QAccel::tr( "+" ); + s += QAccel::tr( "Alt" ); + } + if ( (key & SHIFT) == SHIFT ) { + if ( !s.isEmpty() ) + s += QAccel::tr( "+" ); + s += QAccel::tr( "Shift" ); + } +#endif + + + key &= ~(SHIFT | CTRL | ALT | META ); + QString p; + + if ( (key & UNICODE_ACCEL) == UNICODE_ACCEL ) { + // Note: This character should NOT be upper()'ed, since + // the encoded string should indicate EXACTLY what the + // key represents! Hence a 'Ctrl+Shift+c' is posible to + // represent, but is clearly impossible to trigger... + p = QChar(key & 0xffff); + } else if ( key >= Key_F1 && key <= Key_F35 ) { + p = QAccel::tr( "F%1" ).arg(key - Key_F1 + 1); + } else if ( key > Key_Space && key <= Key_AsciiTilde ) { + p.sprintf( "%c", key ); + } else { + int i=0; + while (keyname[i].name) { + if ( key == keyname[i].key ) { + p = QAccel::tr(keyname[i].name); + break; + } + ++i; + } + // If we can't find the actual translatable keyname, + // fall back on the unicode representation of it... + // Or else characters like Key_aring may not get displayed + // ( Really depends on you locale ) + if ( !keyname[i].name ) + // Note: This character should NOT be upper()'ed, see above! + p = QChar(key & 0xffff); + } + +#ifndef Q_OS_MAC + if ( !s.isEmpty() ) + s += QAccel::tr( "+" ); +#endif + + s += p; + return s; +} + +/*! + Matches the sequence with \a seq. Returns \c Qt::Identical if + successful, \c Qt::PartialMatch for matching but incomplete \a seq, + and \c Qt::NoMatch if the sequences have nothing in common. + Returns \c Qt::NoMatch if \a seq is shorter. +*/ +Qt::SequenceMatch QKeySequence::matches( const QKeySequence& seq ) const +{ + uint userN = count(), + seqN = seq.count(); + + if ( userN > seqN ) + return NoMatch; + + // If equal in length, we have a potential Identical sequence, + // else we already know it can only be partial. + SequenceMatch match = ( userN == seqN ? Identical : PartialMatch ); + + for ( uint i = 0; i < userN; i++ ) { + int userKey = (*this)[i], + sequenceKey = seq[i]; + + if ( (userKey & ~Qt::UNICODE_ACCEL) != + (sequenceKey & ~Qt::UNICODE_ACCEL) ) + return NoMatch; + } + return match; +} + + +/*! + Creates an accelerator string for the key sequence. + For instance CTRL+Key_O gives "Ctrl+O". If the key sequence has + multiple key codes they are returned comma-separated, e.g. + "Alt+X, Ctrl+Y, Z". The strings, "Ctrl", "Shift", etc. are + translated (using QObject::tr()) in the "QAccel" scope. If the key + sequence has no keys, QString::null is returned. + + On Mac OS X, the string returned resembles the sequence that is shown in + the menubar. +*/ +QKeySequence::operator QString() const +{ + int end = count(); + if ( !end ) return QString::null; + + QString complete; + int i = 0; + while ( i < end ) { + complete += encodeString( d->key[i] ); + i++; + if ( i != end) + complete += ", "; + } + return complete; +} + + +/*! + \obsolete + For backward compatibility: returns the first keycode + as integer. If the key sequence is empty, 0 is returned. + */ +QKeySequence::operator int () const +{ + if ( 1 <= count() ) + return d->key[0]; + return 0; +} + + +/*! + Returns a reference to the element at position \a index in the key + sequence. This can only be used to read an element. + */ +int QKeySequence::operator[]( uint index ) const +{ +#ifdef QT_CHECK_STATE + if ( index > 4 ) { + qWarning( "QKeySequence::operator[]: index %u out of range", index ); + return 0; + } +#endif // QT_CHECK_STATE + return d->key[index]; +} + + +/*! + Assignment operator. Assigns \a keysequence to this + object. + */ +QKeySequence &QKeySequence::operator=( const QKeySequence & keysequence ) +{ + keysequence.d->ref(); + if ( d->deref() ) + delete d; + d = keysequence.d; + return *this; +} + + +/*! + Returns TRUE if \a keysequence is equal to this key + sequence; otherwise returns FALSE. + */ + + +bool QKeySequence::operator==( const QKeySequence& keysequence ) const +{ + return ( (d->key[0]&~UNICODE_ACCEL) == (keysequence.d->key[0]&~UNICODE_ACCEL) && + (d->key[1]&~UNICODE_ACCEL) == (keysequence.d->key[1]&~UNICODE_ACCEL) && + (d->key[2]&~UNICODE_ACCEL) == (keysequence.d->key[2]&~UNICODE_ACCEL) && + (d->key[3]&~UNICODE_ACCEL) == (keysequence.d->key[3]&~UNICODE_ACCEL) ); +} + + +/*! + Returns TRUE if \a keysequence is not equal to this key sequence; + otherwise returns FALSE. +*/ +bool QKeySequence::operator!= ( const QKeySequence& keysequence ) const +{ + QKeySequence *that = (QKeySequence*)this; + return !( (*that) == keysequence ); +} + + +/***************************************************************************** + QKeySequence stream functions + *****************************************************************************/ +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +/*! + \relates QKeySequence + + Writes the key sequence \a keysequence to the stream \a s. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ +QDataStream &operator<<( QDataStream &s, const QKeySequence &keysequence ) +{ + QValueList<int> list; + list += keysequence.d->key[0]; + list += keysequence.d->key[1]; + list += keysequence.d->key[2]; + list += keysequence.d->key[3]; + s << list; + + return s; +} + + +/*! + \relates QKeySequence + + Reads a key sequence from the stream \a s into the key sequence \a + keysequence. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ +QDataStream &operator>>( QDataStream &s, QKeySequence &keysequence ) +{ + QValueList<int> list; + s >> list; + +#ifdef QT_CHECK_STATE + if ( 1 != list.count() && 4 != list.count() ) { + qWarning( "Invalid QKeySequence data in the datastream." ); + return s; + } +#endif + + if ( 1 == list.count() ) { + keysequence.d->key[0] = *list.at( 0 ); + keysequence.d->key[1] = + keysequence.d->key[2] = + keysequence.d->key[3] = 0; + } else { + keysequence.d->key[0] = *list.at( 0 ); + keysequence.d->key[1] = *list.at( 1 ); + keysequence.d->key[2] = *list.at( 2 ); + keysequence.d->key[3] = *list.at( 3 ); + } + return s; +} + +#endif //QT_NO_DATASTREAM + +#endif //QT_NO_ACCEL diff --git a/src/kernel/qkeysequence.h b/src/kernel/qkeysequence.h new file mode 100644 index 0000000..c00477b --- /dev/null +++ b/src/kernel/qkeysequence.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Definition of QKeySequence class +** +** Created : 0108007 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QKEYSEQUENCE_H +#define QKEYSEQUENCE_H + +#ifndef QT_H +#ifndef QT_H +#include "qnamespace.h" +#include "qstring.h" +#endif // QT_H +#endif + +#ifndef QT_NO_ACCEL + +/***************************************************************************** + QKeySequence stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +class QKeySequence; +Q_EXPORT QDataStream &operator<<( QDataStream &, const QKeySequence & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QKeySequence & ); +#endif + +class QKeySequencePrivate; + +class Q_EXPORT QKeySequence : public Qt +{ +public: + QKeySequence(); + QKeySequence( const QString& key ); + QKeySequence( int key ); + QKeySequence( int k1, int k2, int k3 = 0, int k4 = 0 ); + QKeySequence( const QKeySequence & ); + ~QKeySequence(); + + uint count() const; + bool isEmpty() const; + Qt::SequenceMatch matches( const QKeySequence & ) const; + + operator QString() const; + operator int () const; + int operator[]( uint ) const; + QKeySequence &operator=( const QKeySequence & ); + bool operator==( const QKeySequence& ) const; + bool operator!= ( const QKeySequence& ) const; + +private: + static int decodeString( const QString & ); + static QString encodeString( int ); + int assign( QString ); + void setKey( int key, int index ); + + QKeySequencePrivate* d; + + friend Q_EXPORT QDataStream &operator<<( QDataStream &, const QKeySequence & ); + friend Q_EXPORT QDataStream &operator>>( QDataStream &, QKeySequence & ); + friend class QAccelManager; +}; + +#else + +class Q_EXPORT QKeySequence : public Qt +{ +public: + QKeySequence() {} + QKeySequence( int ) {} +}; + +#endif //QT_NO_ACCEL + +#endif diff --git a/src/kernel/qlayout.cpp b/src/kernel/qlayout.cpp new file mode 100644 index 0000000..c2418b6 --- /dev/null +++ b/src/kernel/qlayout.cpp @@ -0,0 +1,2605 @@ +/**************************************************************************** +** +** Implementation of layout classes +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qlayout.h" + +#ifndef QT_NO_LAYOUT + +#include "qapplication.h" +#include "qwidget.h" +#include "qptrlist.h" +#include "qsizepolicy.h" + +#include "qlayoutengine_p.h" + +/* + Three internal classes related to QGridLayout: (1) QGridBox is a + QLayoutItem with (row, column) information; (2) QGridMultiBox is a + QGridBox with (torow, tocolumn) information; (3) QGridLayoutData is + the internal representation of a QGridLayout. +*/ + +class QGridBox +{ +public: + QGridBox( QLayoutItem *lit ) { item_ = lit; } + + QGridBox( QWidget *wid ) { item_ = new QWidgetItem( wid ); } + QGridBox( int w, int h, QSizePolicy::SizeType hData = QSizePolicy::Minimum, + QSizePolicy::SizeType vData = QSizePolicy::Minimum ) + { item_ = new QSpacerItem( w, h, hData, vData ); } + ~QGridBox() { delete item_; } + + QSize sizeHint() const { return item_->sizeHint(); } + QSize minimumSize() const { return item_->minimumSize(); } + QSize maximumSize() const { return item_->maximumSize(); } + QSizePolicy::ExpandData expanding() const { return item_->expanding(); } + bool isEmpty() const { return item_->isEmpty(); } + + bool hasHeightForWidth() const { return item_->hasHeightForWidth(); } + int heightForWidth( int w ) const { return item_->heightForWidth(w); } + + void setAlignment( int a ) { item_->setAlignment( a ); } + void setGeometry( const QRect &r ) { item_->setGeometry( r ); } + int alignment() const { return item_->alignment(); } + QLayoutItem *item() { return item_; } + QLayoutItem *takeItem() { QLayoutItem *i = item_; item_ = 0; return i; } + + int hStretch() { return item_->widget() ? + item_->widget()->sizePolicy().horStretch() : 0; } + int vStretch() { return item_->widget() ? + item_->widget()->sizePolicy().verStretch() : 0; } + +private: + friend class QGridLayoutData; + friend class QGridLayoutDataIterator; + + QLayoutItem *item_; + int row, col; +}; + +class QGridMultiBox +{ +public: + QGridMultiBox( QGridBox *box, int toRow, int toCol ) + : box_( box ), torow( toRow ), tocol( toCol ) { } + ~QGridMultiBox() { delete box_; } + QGridBox *box() { return box_; } + QLayoutItem *takeItem() { return box_->takeItem(); } + +private: + friend class QGridLayoutData; + friend class QGridLayoutDataIterator; + + QGridBox *box_; + int torow, tocol; +}; + +class QGridLayoutData +{ +public: + QGridLayoutData(); + QGridLayoutData( int nRows, int nCols ); + ~QGridLayoutData(); + + void add( QGridBox*, int row, int col ); + void add( QGridBox*, int row1, int row2, int col1, int col2 ); + QSize sizeHint( int ) const; + QSize minimumSize( int ) const; + QSize maximumSize( int ) const; + + QSizePolicy::ExpandData expanding( int spacing ); + + void distribute( QRect, int ); + inline int numRows() const { return rr; } + inline int numCols() const { return cc; } + inline void expand( int rows, int cols ) + { setSize( QMAX(rows, rr), QMAX(cols, cc) ); } + inline void setRowStretch( int r, int s ) + { expand( r + 1, 0 ); rStretch[r] = s; setDirty(); } + inline void setColStretch( int c, int s ) + { expand( 0, c + 1 ); cStretch[c] = s; setDirty(); } + inline int rowStretch( int r ) const { return rStretch[r]; } + inline int colStretch( int c ) const { return cStretch[c]; } + inline void setRowSpacing( int r, int s ) + { expand( r + 1, 0 ); rSpacing[r] = s; setDirty(); } + inline void setColSpacing( int c, int s ) + { expand( 0, c + 1 ); cSpacing[c] = s; setDirty(); } + inline int rowSpacing( int r ) const { return rSpacing[r]; } + inline int colSpacing( int c ) const { return cSpacing[c]; } + + inline void setReversed( bool r, bool c ) { hReversed = c; vReversed = r; } + inline bool horReversed() const { return hReversed; } + inline bool verReversed() const { return vReversed; } + inline void setDirty() { needRecalc = TRUE; hfw_width = -1; } + inline bool isDirty() const { return needRecalc; } + bool hasHeightForWidth( int space ); + int heightForWidth( int, int, int ); + int minimumHeightForWidth( int, int, int ); + + bool findWidget( QWidget* w, int *row, int *col ); + + inline void getNextPos( int &row, int &col ) { row = nextR; col = nextC; } + inline uint count() const + { return things.count() + ( multi ? multi->count() : 0 ); } + QRect cellGeometry( int row, int col ) const; + +private: + void setNextPosAfter( int r, int c ); + void recalcHFW( int w, int s ); + void addHfwData ( QGridBox *box, int width ); + void init(); + QSize findSize( QCOORD QLayoutStruct::*, int ) const; + void addData( QGridBox *b, bool r = TRUE, bool c = TRUE ); + void setSize( int rows, int cols ); + void setupLayoutData( int space ); + void setupHfwLayoutData( int space ); + + int rr; + int cc; + QMemArray<QLayoutStruct> rowData; + QMemArray<QLayoutStruct> colData; + QMemArray<QLayoutStruct> *hfwData; + QMemArray<int> rStretch; + QMemArray<int> cStretch; + QMemArray<int> rSpacing; + QMemArray<int> cSpacing; + QPtrList<QGridBox> things; + QPtrList<QGridMultiBox> *multi; + + int hfw_width; + int hfw_height; + int hfw_minheight; + int nextR; + int nextC; + + uint hReversed : 1; + uint vReversed : 1; + uint needRecalc : 1; + uint has_hfw : 1; + uint addVertical : 1; + + friend class QGridLayoutDataIterator; +}; + +QGridLayoutData::QGridLayoutData() +{ + init(); +} + +QGridLayoutData::QGridLayoutData( int nRows, int nCols ) + : rowData( 0 ), colData( 0 ) +{ + init(); + if ( nRows < 0 ) { + nRows = 1; + addVertical = FALSE; + } + if ( nCols < 0 ) { + nCols = 1; + addVertical = TRUE; + } + setSize( nRows, nCols ); +} + +QGridLayoutData::~QGridLayoutData() +{ + // must be cleared while the data is still in a stable state + things.clear(); + + delete multi; + delete hfwData; +} + +void QGridLayoutData::init() +{ + addVertical = FALSE; + setDirty(); + multi = 0; + rr = cc = 0; + nextR = nextC = 0; + hfwData = 0; + things.setAutoDelete( TRUE ); + hReversed = FALSE; + vReversed = FALSE; +} + +bool QGridLayoutData::hasHeightForWidth( int spacing ) +{ + setupLayoutData( spacing ); + return has_hfw; +} + +/* + Assumes that setupLayoutData() has been called, and that + qGeomCalc() has filled in colData with appropriate values. +*/ +void QGridLayoutData::recalcHFW( int w, int spacing ) +{ + /* + Go through all children, using colData and heightForWidth() + and put the results in hfw_rowData. + */ + if ( !hfwData ) + hfwData = new QMemArray<QLayoutStruct>( rr ); + setupHfwLayoutData( spacing ); + QMemArray<QLayoutStruct> &rData = *hfwData; + + int h = 0; + int mh = 0; + int n = 0; + for ( int r = 0; r < rr; r++ ) { + h += rData[r].sizeHint; + mh += rData[r].minimumSize; + if ( !rData[r].empty ) + n++; + } + if ( n ) { + h += ( n - 1 ) * spacing; + mh += ( n - 1 ) * spacing; + } + + hfw_width = w; + hfw_height = QMIN( QLAYOUTSIZE_MAX, h ); + hfw_minheight = QMIN( QLAYOUTSIZE_MAX, mh ); +} + +int QGridLayoutData::heightForWidth( int w, int margin, int spacing ) +{ + setupLayoutData( spacing ); + if ( !has_hfw ) + return -1; + if ( w + 2*margin != hfw_width ) { + qGeomCalc( colData, 0, cc, 0, w+2*margin, spacing ); + recalcHFW( w+2*margin, spacing ); + } + return hfw_height + 2*margin; +} + +int QGridLayoutData::minimumHeightForWidth( int w, int margin, int spacing ) +{ + (void) heightForWidth( w, margin, spacing ); + return has_hfw ? (hfw_minheight + 2*margin) : -1; +} + +bool QGridLayoutData::findWidget( QWidget* w, int *row, int *col ) +{ + QPtrListIterator<QGridBox> it( things ); + QGridBox * box; + while ( (box = it.current()) != 0 ) { + ++it; + if ( box->item()->widget() == w ) { + if ( row ) + *row = box->row; + if ( col ) + *col = box->col; + return TRUE; + } + } + if ( multi ) { + QPtrListIterator<QGridMultiBox> it( *multi ); + QGridMultiBox * mbox; + while ( (mbox = it.current()) != 0 ) { + ++it; + box = mbox->box(); + if ( box->item()->widget() == w ) { + if ( row ) + *row = box->row; + if ( col ) + *col = box->col; + return TRUE; + } + + } + } + return FALSE; +} + +QSize QGridLayoutData::findSize( QCOORD QLayoutStruct::*size, int spacer ) const +{ + QGridLayoutData *that = (QGridLayoutData *)this; + that->setupLayoutData( spacer ); + + int w = 0; + int h = 0; + int n = 0; + for ( int r = 0; r < rr; r++ ) { + h = h + rowData[r].*size; + if ( !rowData[r].empty ) + n++; + } + if ( n ) + h += ( n - 1 ) * spacer; + n = 0; + for ( int c = 0; c < cc; c++ ) { + w = w + colData[c].*size; + if ( !colData[c].empty ) + n++; + } + if ( n ) + w += ( n - 1 ) * spacer; + w = QMIN( QLAYOUTSIZE_MAX, w ); + h = QMIN( QLAYOUTSIZE_MAX, h ); + + return QSize( w, h ); +} + +QSizePolicy::ExpandData QGridLayoutData::expanding( int spacing ) +{ + setupLayoutData( spacing ); + int ret = 0; + + for ( int r = 0; r < rr; r++ ) { + if ( rowData[r].expansive ) { + ret |= (int) QSizePolicy::Vertically; + break; + } + } + for ( int c = 0; c < cc; c++ ) { + if ( colData[c].expansive ) { + ret |= (int) QSizePolicy::Horizontally; + break; + } + } + return (QSizePolicy::ExpandData) ret; +} + +QSize QGridLayoutData::sizeHint( int spacer ) const +{ + return findSize( &QLayoutStruct::sizeHint, spacer ); +} + +QSize QGridLayoutData::maximumSize( int spacer ) const +{ + return findSize( &QLayoutStruct::maximumSize, spacer ); +} + +QSize QGridLayoutData::minimumSize( int spacer ) const +{ + return findSize( &QLayoutStruct::minimumSize, spacer ); +} + +void QGridLayoutData::setSize( int r, int c ) +{ + if ( (int)rowData.size() < r ) { + int newR = QMAX( r, rr * 2 ); + rowData.resize( newR ); + rStretch.resize( newR ); + rSpacing.resize( newR ); + for ( int i = rr; i < newR; i++ ) { + rowData[i].init(); + rStretch[i] = 0; + rSpacing[i] = 0; + } + } + if ( (int)colData.size() < c ) { + int newC = QMAX( c, cc * 2 ); + colData.resize( newC ); + cStretch.resize( newC ); + cSpacing.resize( newC ); + for ( int i = cc; i < newC; i++ ) { + colData[i].init(); + cStretch[i] = 0; + cSpacing[i] = 0; + } + } + + if ( hfwData && (int)hfwData->size() < r ) { + delete hfwData; + hfwData = 0; + hfw_width = -1; + } + rr = r; + cc = c; +} + +void QGridLayoutData::setNextPosAfter( int row, int col ) +{ + if ( addVertical ) { + if ( col > nextC || col == nextC && row >= nextR ) { + nextR = row + 1; + nextC = col; + if ( nextR >= rr ) { + nextR = 0; + nextC++; + } + } + } else { + if ( row > nextR || row == nextR && col >= nextC ) { + nextR = row; + nextC = col + 1; + if ( nextC >= cc ) { + nextC = 0; + nextR++; + } + } + } +} + +void QGridLayoutData::add( QGridBox *box, int row, int col ) +{ + expand( row+1, col+1 ); + box->row = row; + box->col = col; + things.append( box ); + setDirty(); + setNextPosAfter( row, col ); +} + +void QGridLayoutData::add( QGridBox *box, int row1, int row2, int col1, + int col2 ) +{ +#ifdef QT_CHECK_RANGE + if ( row2 >= 0 && row2 < row1 ) + qWarning( "QGridLayout: Multi-cell fromRow greater than toRow" ); + if ( col2 >= 0 && col2 < col1 ) + qWarning( "QGridLayout: Multi-cell fromCol greater than toCol" ); +#endif + if ( row1 == row2 && col1 == col2 ) { + add( box, row1, col1 ); + return; + } + expand( row2+1, col2+1 ); + box->row = row1; + box->col = col1; + QGridMultiBox *mbox = new QGridMultiBox( box, row2, col2 ); + if ( !multi ) { + multi = new QPtrList<QGridMultiBox>; + multi->setAutoDelete( TRUE ); + } + multi->append( mbox ); + setDirty(); + if ( col2 < 0 ) + col2 = cc - 1; + + setNextPosAfter( row2, col2 ); +} + +void QGridLayoutData::addData( QGridBox *box, bool r, bool c ) +{ + QSize hint = box->sizeHint(); + QSize minS = box->minimumSize(); + QSize maxS = box->maximumSize(); + + if ( c ) { + if ( !cStretch[box->col] ) + colData[box->col].stretch = QMAX( colData[box->col].stretch, + box->hStretch() ); + colData[box->col].sizeHint = QMAX( hint.width(), + colData[box->col].sizeHint ); + colData[box->col].minimumSize = QMAX( minS.width(), + colData[box->col].minimumSize ); + + qMaxExpCalc( colData[box->col].maximumSize, colData[box->col].expansive, + maxS.width(), + box->expanding() & QSizePolicy::Horizontally ); + } + if ( r ) { + if ( !rStretch[box->row] ) + rowData[box->row].stretch = QMAX( rowData[box->row].stretch, + box->vStretch() ); + rowData[box->row].sizeHint = QMAX( hint.height(), + rowData[box->row].sizeHint ); + rowData[box->row].minimumSize = QMAX( minS.height(), + rowData[box->row].minimumSize ); + + qMaxExpCalc( rowData[box->row].maximumSize, rowData[box->row].expansive, + maxS.height(), + box->expanding() & QSizePolicy::Vertically ); + } + if ( box->isEmpty() ) { + if ( box->item()->widget() != 0 ) { + /* + Hidden widgets should behave exactly the same as if + there were no widgets at all in the cell. We could have + QWidgetItem::maximumSize() to return + QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX). However, that + value is not suitable for QBoxLayouts. So let's fix it + here. + */ + if ( c ) + colData[box->col].maximumSize = QLAYOUTSIZE_MAX; + if ( r ) + rowData[box->row].maximumSize = QLAYOUTSIZE_MAX; + } + } else { + // Empty boxes (i.e. spacers) do not get borders. This is + // hacky, but compatible. + if ( c ) + colData[box->col].empty = FALSE; + if ( r ) + rowData[box->row].empty = FALSE; + } +} + +static void distributeMultiBox( QMemArray<QLayoutStruct> &chain, int spacing, + int start, int end, + int minSize, int sizeHint, + QMemArray<int> &stretchArray, int stretch ) +{ + int i; + int w = 0; + int wh = 0; + int max = 0; + for ( i = start; i <= end; i++ ) { + w += chain[i].minimumSize; + wh += chain[i].sizeHint; + max += chain[i].maximumSize; + chain[i].empty = FALSE; + if ( stretchArray[i] == 0 ) + chain[i].stretch = QMAX(chain[i].stretch,stretch); + } + w += spacing * ( end - start ); + wh += spacing * ( end - start ); + max += spacing * ( end - start ); + + if ( max < minSize ) { // implies w < minSize + /* + We must increase the maximum size of at least one of the + items. qGeomCalc() will put the extra space in between the + items. We must recover that extra space and put it + somewhere. It does not really matter where, since the user + can always specify stretch factors and avoid this code. + */ + qGeomCalc( chain, start, end - start + 1, 0, minSize, spacing ); + int pos = 0; + for ( i = start; i <= end; i++ ) { + int nextPos = ( i == end ) ? minSize - 1 : chain[i + 1].pos; + int realSize = nextPos - pos; + if ( i != end ) + realSize -= spacing; + if ( chain[i].minimumSize < realSize ) + chain[i].minimumSize = realSize; + if ( chain[i].maximumSize < chain[i].minimumSize ) + chain[i].maximumSize = chain[i].minimumSize; + pos = nextPos; + } + } else if ( w < minSize ) { + qGeomCalc( chain, start, end - start + 1, 0, minSize, spacing ); + for ( i = start; i <= end; i++ ) { + if ( chain[i].minimumSize < chain[i].size ) + chain[i].minimumSize = chain[i].size; + } + } + + if ( wh < sizeHint ) { + qGeomCalc( chain, start, end - start + 1, 0, sizeHint, spacing ); + for ( i = start; i <= end; i++ ) { + if ( chain[i].sizeHint < chain[i].size ) + chain[i].sizeHint = chain[i].size; + } + } +} + +//#define QT_LAYOUT_DISABLE_CACHING + +void QGridLayoutData::setupLayoutData( int spacing ) +{ +#ifndef QT_LAYOUT_DISABLE_CACHING + if ( !needRecalc ) + return; +#endif + has_hfw = FALSE; + int i; + + for ( i = 0; i < rr; i++ ) + rowData[i].init( rStretch[i], rSpacing[i] ); + for ( i = 0; i < cc; i++ ) + colData[i].init( cStretch[i], cSpacing[i] ); + + QPtrListIterator<QGridBox> it( things ); + QGridBox * box; + while ( (box = it.current()) != 0 ) { + ++it; + addData( box ); + has_hfw = has_hfw || box->item()->hasHeightForWidth(); + } + + if ( multi ) { + QPtrListIterator<QGridMultiBox> it( *multi ); + QGridMultiBox * mbox; + while ( (mbox = it.current()) != 0 ) { + ++it; + QGridBox *box = mbox->box(); + int r1 = box->row; + int c1 = box->col; + int r2 = mbox->torow; + int c2 = mbox->tocol; + if ( r2 < 0 ) + r2 = rr - 1; + if ( c2 < 0 ) + c2 = cc - 1; + + QSize hint = box->sizeHint(); + QSize min = box->minimumSize(); + if ( box->hasHeightForWidth() ) + has_hfw = TRUE; + + if ( r1 == r2 ) { + addData( box, TRUE, FALSE ); + } else { + distributeMultiBox( rowData, spacing, r1, r2, + min.height(), hint.height(), + rStretch, box->vStretch() ); + } + if ( c1 == c2 ) { + addData( box, FALSE, TRUE ); + } else { + distributeMultiBox( colData, spacing, c1, c2, + min.width(), hint.width(), + cStretch, box->hStretch() ); + } + } + } + for ( i = 0; i < rr; i++ ) + rowData[i].expansive = rowData[i].expansive || rowData[i].stretch > 0; + for ( i = 0; i < cc; i++ ) + colData[i].expansive = colData[i].expansive || colData[i].stretch > 0; + + needRecalc = FALSE; +} + +void QGridLayoutData::addHfwData( QGridBox *box, int width ) +{ + QMemArray<QLayoutStruct> &rData = *hfwData; + if ( box->hasHeightForWidth() ) { + int hint = box->heightForWidth( width ); + rData[box->row].sizeHint = QMAX( hint, rData[box->row].sizeHint ); + rData[box->row].minimumSize = QMAX( hint, rData[box->row].minimumSize ); + } else { + QSize hint = box->sizeHint(); + QSize minS = box->minimumSize(); + rData[box->row].sizeHint = QMAX( hint.height(), + rData[box->row].sizeHint ); + rData[box->row].minimumSize = QMAX( minS.height(), + rData[box->row].minimumSize ); + } +} + +/* + Similar to setupLayoutData(), but uses heightForWidth(colData) + instead of sizeHint(). Assumes that setupLayoutData() and + qGeomCalc(colData) has been called. +*/ +void QGridLayoutData::setupHfwLayoutData( int spacing ) +{ + QMemArray<QLayoutStruct> &rData = *hfwData; + int i; + for ( i = 0; i < rr; i++ ) { + rData[i] = rowData[i]; + rData[i].minimumSize = rData[i].sizeHint = 0; + } + QPtrListIterator<QGridBox> it( things ); + QGridBox * box; + while ( (box=it.current()) != 0 ) { + ++it; + addHfwData( box, colData[box->col].size ); + } + if ( multi ) { + QPtrListIterator<QGridMultiBox> it( *multi ); + QGridMultiBox * mbox; + while ( (mbox=it.current()) != 0 ) { + ++it; + QGridBox *box = mbox->box(); + int r1 = box->row; + int c1 = box->col; + int r2 = mbox->torow; + int c2 = mbox->tocol; + if ( r2 < 0 ) + r2 = rr-1; + if ( c2 < 0 ) + c2 = cc-1; + int w = colData[c2].pos + colData[c2].size - colData[c1].pos; + if ( r1 == r2 ) { + addHfwData( box, w ); + } else { + QSize hint = box->sizeHint(); + QSize min = box->minimumSize(); + if ( box->hasHeightForWidth() ) { + int hfwh = box->heightForWidth( w ); + if ( hfwh > hint.height() ) + hint.setHeight( hfwh ); + if ( hfwh > min.height() ) + min.setHeight( hfwh ); + } + distributeMultiBox( rData, spacing, r1, r2, + min.height(), hint.height(), + rStretch, box->vStretch() ); + } + } + } + for ( i = 0; i < rr; i++ ) + rData[i].expansive = rData[i].expansive || rData[i].stretch > 0; +} + +void QGridLayoutData::distribute( QRect r, int spacing ) +{ + bool visualHReversed = hReversed; + if ( QApplication::reverseLayout() ) + visualHReversed = !visualHReversed; + + setupLayoutData( spacing ); + + qGeomCalc( colData, 0, cc, r.x(), r.width(), spacing ); + QMemArray<QLayoutStruct> *rDataPtr; + if ( has_hfw ) { + recalcHFW( r.width(), spacing ); + qGeomCalc( *hfwData, 0, rr, r.y(), r.height(), spacing ); + rDataPtr = hfwData; + } else { + qGeomCalc( rowData, 0, rr, r.y(), r.height(), spacing ); + rDataPtr = &rowData; + } + QMemArray<QLayoutStruct> &rData = *rDataPtr; + + QPtrListIterator<QGridBox> it( things ); + QGridBox * box; + while ( (box=it.current()) != 0 ) { + ++it; + int x = colData[box->col].pos; + int y = rData[box->row].pos; + int w = colData[box->col].size; + int h = rData[box->row].size; + if ( visualHReversed ) + x = r.left() + r.right() - x - w + 1; + if ( vReversed ) + y = r.top() + r.bottom() - y - h + 1; + box->setGeometry( QRect( x, y, w, h ) ); + } + if ( multi ) { + QPtrListIterator<QGridMultiBox> it( *multi ); + QGridMultiBox * mbox; + while ( (mbox=it.current()) != 0 ) { + ++it; + QGridBox *box = mbox->box(); + int r2 = mbox->torow; + int c2 = mbox->tocol; + if ( r2 < 0 ) + r2 = rr-1; + if ( c2 < 0 ) + c2 = cc-1; + + int x = colData[box->col].pos; + int y = rData[box->row].pos; + int x2p = colData[c2].pos + colData[c2].size; // x2+1 + int y2p = rData[r2].pos + rData[r2].size; // y2+1 + int w = x2p - x; + int h = y2p - y; + // this code is copied from above: + if ( visualHReversed ) + x = r.left() + r.right() - x - w + 1; + if ( vReversed ) + y = r.top() + r.bottom() - y - h + 1; + box->setGeometry( QRect( x, y, w, h ) ); + } + } +} + +QRect QGridLayoutData::cellGeometry( int row, int col ) const +{ + if ( row < 0 || row >= rr || col < 0 || col >= cc ) + return QRect(); + + const QMemArray<QLayoutStruct> *rDataPtr; + if ( has_hfw ) + rDataPtr = hfwData; + else + rDataPtr = &rowData; + return QRect( colData[col].pos, (*rDataPtr)[row].pos, + colData[col].size, (*rDataPtr)[row].size ); +} + +class QGridLayoutDataIterator : public QGLayoutIterator +{ +public: + QGridLayoutDataIterator( QGridLayoutData *d ); + uint count() const { return data->count(); } + QLayoutItem *current() { + if ( multi ) { + if ( !data->multi || idx >= (int)data->multi->count() ) + return 0; + return data->multi->at( idx )->box()->item(); + } else { + if ( idx >= (int)data->things.count() ) + return 0; + return data->things.at( idx )->item(); + } + } + void toFirst() { + multi = data->things.isEmpty(); + idx = 0; + } + QLayoutItem *next() { + idx++; + if ( !multi && idx >= (int)data->things.count() ) { + multi = TRUE; + idx = 0; + } + return current(); + } + QLayoutItem *takeCurrent() { + QLayoutItem *item = 0; + if ( multi ) { + if ( !data->multi || idx >= (int)data->multi->count() ) + return 0; + QGridMultiBox *b = data->multi->take( idx ); + item = b->takeItem(); + delete b; + } else { + if ( idx >= (int)data->things.count() ) + return 0; + QGridBox *b = data->things.take( idx ); + item = b->takeItem(); + delete b; + } + return item; + } + +private: + QGridLayoutData *data; + bool multi; + int idx; +}; + +inline QGridLayoutDataIterator::QGridLayoutDataIterator( QGridLayoutData *d ) + : data( d ) +{ + toFirst(); +} + +/*! + \class QGridLayout + + \brief The QGridLayout class lays out widgets in a grid. + + \ingroup geomanagement + \ingroup appearance + \mainclass + + QGridLayout takes the space made available to it (by its parent + layout or by the mainWidget()), divides it up into rows and + columns, and puts each widget it manages into the correct cell. + + Columns and rows behave identically; we will discuss columns, but + there are equivalent functions for rows. + + Each column has a minimum width and a stretch factor. The minimum + width is the greatest of that set using addColSpacing() and the + minimum width of each widget in that column. The stretch factor is + set using setColStretch() and determines how much of the available + space the column will get over and above its necessary minimum. + + Normally, each managed widget or layout is put into a cell of its + own using addWidget(), addLayout() or by the \link + QLayout::setAutoAdd() auto-add facility\endlink. It is also + possible for a widget to occupy multiple cells using + addMultiCellWidget(). If you do this, QGridLayout will guess how + to distribute the size over the columns/rows (based on the stretch + factors). + + To remove a widget from a layout, call remove(). Calling + QWidget::hide() on a widget also effectively removes the widget + from the layout until QWidget::show() is called. + + This illustration shows a fragment of a dialog with a five-column, + three-row grid (the grid is shown overlaid in magenta): + + \img gridlayout.png + + Columns 0, 2 and 4 in this dialog fragment are made up of a + QLabel, a QLineEdit, and a QListBox. Columns 1 and 3 are + placeholders made with addColSpacing(). Row 0 consists of three + QLabel objects, row 1 of three QLineEdit objects and row 2 of + three QListBox objects. We used placeholder columns (1 and 3) to + get the right amount of space between the columns. + + Note that the columns and rows are not equally wide or tall. If + you want two columns to have the same width, you must set their + minimum widths and stretch factors to be the same yourself. You do + this using addColSpacing() and setColStretch(). + + If the QGridLayout is not the top-level layout (i.e. does not + manage all of the widget's area and children), you must add it to + its parent layout when you create it, but before you do anything + with it. The normal way to add a layout is by calling + parentLayout-\>addLayout(). + + Once you have added your layout you can start putting widgets and + other layouts into the cells of your grid layout using + addWidget(), addLayout() and addMultiCellWidget(). + + QGridLayout also includes two margin widths: the border and the + spacing. The border is the width of the reserved space along each + of the QGridLayout's four sides. The spacing is the width of the + automatically allocated spacing between neighboring boxes. + + Both the border and the spacing are parameters of the constructor + and default to 0. + + \sa QGrid, \link layout.html Layout Overview \endlink +*/ + +/*! + \enum QGridLayout::Corner + + This enum identifies which corner is the origin (0, 0) of the + layout. + + \value TopLeft the top-left corner + \value TopRight the top-right corner + \value BottomLeft the bottom-left corner + \value BottomRight the bottom-right corner +*/ + +/*! + Constructs a new QGridLayout with \a nRows rows, \a nCols columns + and parent widget, \a parent. \a parent may not be 0. The grid + layout is called \a name. + + \a margin is the number of pixels between the edge of the widget + and its managed children. \a space is the default number of pixels + between cells. If \a space is -1, the value of \a margin is used. +*/ +QGridLayout::QGridLayout( QWidget *parent, int nRows, int nCols, int margin, + int space, const char *name ) + : QLayout( parent, margin, space, name ) +{ + init( nRows, nCols ); +} + +/*! + Constructs a new grid that is placed inside \a parentLayout with + \a nRows rows and \a nCols columns. If \a spacing is -1, this + QGridLayout inherits its parent's spacing(); otherwise \a spacing + is used. The grid layout is called \a name. + + This grid is placed according to \a parentLayout's default + placement rules. +*/ +QGridLayout::QGridLayout( QLayout *parentLayout, int nRows, int nCols, + int spacing, const char *name ) + : QLayout( parentLayout, spacing, name ) +{ + init( nRows, nCols ); +} + +/*! + Constructs a new grid with \a nRows rows and \a nCols columns. If + \a spacing is -1, this QGridLayout inherits its parent's + spacing(); otherwise \a spacing is used. The grid layout is called + \a name. + + You must insert this grid into another layout. You can insert + widgets and layouts into this layout at any time, but laying out + will not be performed before this is inserted into another layout. +*/ +QGridLayout::QGridLayout( int nRows, int nCols, + int spacing, const char *name ) + : QLayout( spacing, name ) +{ + init( nRows, nCols ); +} + +/*! + Destroys the grid layout. Geometry management is terminated if + this is a top-level grid. + + The layout's widgets aren't destroyed. +*/ +QGridLayout::~QGridLayout() +{ + delete data; +} + +/*! + Returns the number of rows in this grid. +*/ +int QGridLayout::numRows() const +{ + return data->numRows(); +} + +/*! + Returns the number of columns in this grid. +*/ +int QGridLayout::numCols() const +{ + return data->numCols(); +} + +/*! + Returns the preferred size of this grid. +*/ +QSize QGridLayout::sizeHint() const +{ + return data->sizeHint( spacing() ) + QSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the minimum size needed by this grid. +*/ +QSize QGridLayout::minimumSize() const +{ + return data->minimumSize( spacing() ) + QSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the maximum size needed by this grid. +*/ +QSize QGridLayout::maximumSize() const +{ + QSize s = data->maximumSize( spacing() ) + + QSize( 2 * margin(), 2 * margin() ); + s = s.boundedTo( QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX) ); + if ( alignment() & Qt::AlignHorizontal_Mask ) + s.setWidth( QLAYOUTSIZE_MAX ); + if ( alignment() & Qt::AlignVertical_Mask ) + s.setHeight( QLAYOUTSIZE_MAX ); + return s; +} + +/*! + Returns TRUE if this layout's preferred height depends on its + width; otherwise returns FALSE. +*/ +bool QGridLayout::hasHeightForWidth() const +{ + return ((QGridLayout*)this)->data->hasHeightForWidth( spacing() ); +} + +/*! + Returns the layout's preferred height when it is \a w pixels wide. +*/ +int QGridLayout::heightForWidth( int w ) const +{ + QGridLayout *that = (QGridLayout*)this; + return that->data->heightForWidth( w, margin(), spacing() ); +} + +/*! \internal */ +int QGridLayout::minimumHeightForWidth( int w ) const +{ + QGridLayout *that = (QGridLayout*)this; + return that->data->minimumHeightForWidth( w, margin(), spacing() ); +} + +/*! + Searches for widget \a w in this layout (not including child + layouts). If \a w is found, it sets \c \a row and \c \a col to + the row and column and returns TRUE; otherwise returns FALSE. + + Note: if a widget spans multiple rows/columns, the top-left cell + is returned. +*/ +bool QGridLayout::findWidget( QWidget* w, int *row, int *col ) +{ + return data->findWidget( w, row, col ); +} + +/*! + Resizes managed widgets within the rectangle \a r. +*/ +void QGridLayout::setGeometry( const QRect &r ) +{ + if ( data->isDirty() || r != geometry() ) { + QLayout::setGeometry( r ); + QRect cr = alignment() ? alignmentRect( r ) : r; + QRect s( cr.x() + margin(), cr.y() + margin(), + cr.width() - 2 * margin(), cr.height() - 2 * margin() ); + data->distribute( s, spacing() ); + } +} + +/*! + Returns the geometry of the cell with row \a row and column \a col + in the grid. Returns an invalid rectangle if \a row or \a col is + outside the grid. + + \warning in the current version of Qt this function does not + return valid results until setGeometry() has been called, i.e. + after the mainWidget() is visible. +*/ +QRect QGridLayout::cellGeometry( int row, int col ) const +{ + return data->cellGeometry( row, col ); +} + +/*! + Expands this grid so that it will have \a nRows rows and \a nCols + columns. Will not shrink the grid. You should not need to call + this function because QGridLayout expands automatically as new + items are inserted. +*/ +void QGridLayout::expand( int nRows, int nCols ) +{ + data->expand( nRows, nCols ); +} + +/*! + Sets up the grid. +*/ +void QGridLayout::init( int nRows, int nCols ) +{ + setSupportsMargin( TRUE ); + data = new QGridLayoutData( nRows, nCols ); +} + +/*! + \overload + + Adds \a item to the next free position of this layout. +*/ +void QGridLayout::addItem( QLayoutItem *item ) +{ + int r, c; + data->getNextPos( r, c ); + add( item, r, c ); +} + +/*! + Adds \a item at position \a row, \a col. The layout takes + ownership of the \a item. +*/ +void QGridLayout::addItem( QLayoutItem *item, int row, int col ) +{ + add( item, row, col ); +} + +/*! + Adds \a item at position \a row, \a col. The layout takes + ownership of the \a item. +*/ +void QGridLayout::add( QLayoutItem *item, int row, int col ) +{ + QGridBox *box = new QGridBox( item ); + data->add( box, row, col ); +} + +/*! + Adds the \a item to the cell grid, spanning multiple rows/columns. + + The cell will span from \a fromRow, \a fromCol to \a toRow, \a + toCol. Alignment is specified by \a alignment, which is a bitwise + OR of \l Qt::AlignmentFlags values. The default alignment is 0, + which means that the widget fills the entire cell. +*/ +void QGridLayout::addMultiCell( QLayoutItem *item, int fromRow, int toRow, + int fromCol, int toCol, int alignment ) +{ + QGridBox *b = new QGridBox( item ); + b->setAlignment( alignment ); + data->add( b, fromRow, toRow, fromCol, toCol ); +} + +/* + Returns TRUE if the widget \a w can be added to the layout \a l; + otherwise returns FALSE. +*/ +static bool checkWidget( QLayout *l, QWidget *w ) +{ + if ( !w ) { +#if defined(QT_CHECK_STATE) + qWarning( "QLayout: Cannot add null widget to %s/%s", l->className(), + l->name() ); +#endif + return FALSE; + } + if ( w->parentWidget() != l->mainWidget() && l->mainWidget() ) { +#if defined(QT_CHECK_STATE) + if ( w->parentWidget() ) + qWarning( "QLayout: Adding %s/%s (child of %s/%s) to layout for " + "%s/%s", w->className(), w->name(), + w->parentWidget()->className(), w->parentWidget()->name(), + l->mainWidget()->className(), l->mainWidget()->name() ); + else + qWarning( "QLayout: Adding %s/%s (top-level widget) to layout for" + " %s/%s", w->className(), w->name(), + l->mainWidget()->className(), l->mainWidget()->name() ); +#endif + return FALSE; + } + return TRUE; +} + +/*! + Adds the widget \a w to the cell grid at \a row, \a col. The + top-left position is (0, 0) by default. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l Qt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + \list + \i You should not call this if you have enabled the + \link QLayout::setAutoAdd() auto-add facility of the layout\endlink. + + \i From Qt 3.0, the \a alignment parameter is interpreted more + aggressively than in previous versions of Qt. A non-default + alignment now indicates that the widget should not grow to fill + the available space, but should be sized according to sizeHint(). + \endlist + + \sa addMultiCellWidget() +*/ +void QGridLayout::addWidget( QWidget *w, int row, int col, int alignment ) +{ + if ( !checkWidget( this, w ) ) + return; + if ( row < 0 || col < 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "QGridLayout: Cannot add %s/%s to %s/%s at row %d col %d", + w->className(), w->name(), className(), name(), row, col ); +#endif + return; + } + QWidgetItem *b = new QWidgetItem( w ); + b->setAlignment( alignment ); + add( b, row, col ); +} + +/*! + Adds the widget \a w to the cell grid, spanning multiple + rows/columns. The cell will span from \a fromRow, \a fromCol to \a + toRow, \a toCol. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l Qt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + A non-zero alignment indicates that the widget should not grow to + fill the available space but should be sized according to + sizeHint(). + + \sa addWidget() +*/ +void QGridLayout::addMultiCellWidget( QWidget *w, int fromRow, int toRow, + int fromCol, int toCol, int alignment ) +{ + QGridBox *b = new QGridBox( w ); + b->setAlignment( alignment ); + data->add( b, fromRow, toRow, fromCol, toCol ); +} + +/*! + Places the \a layout at position (\a row, \a col) in the grid. The + top-left position is (0, 0). + + \a layout becomes a child of the grid layout. + + When a layout is constructed with another layout as its parent, + you don't need to call addLayout(); the child layout is + automatically added to the parent layout as it is constructed. + + \sa addMultiCellLayout() +*/ +void QGridLayout::addLayout( QLayout *layout, int row, int col ) +{ + addChildLayout( layout ); + add( layout, row, col ); +} + +/*! + Adds the layout \a layout to the cell grid, spanning multiple + rows/columns. The cell will span from \a fromRow, \a fromCol to \a + toRow, \a toCol. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l Qt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + A non-zero alignment indicates that the layout should not grow to + fill the available space but should be sized according to + sizeHint(). + + \a layout becomes a child of the grid layout. + + \sa addLayout() +*/ +void QGridLayout::addMultiCellLayout( QLayout *layout, int fromRow, int toRow, + int fromCol, int toCol, int alignment ) +{ + addChildLayout( layout ); + QGridBox *b = new QGridBox( layout ); + b->setAlignment( alignment ); + data->add( b, fromRow, toRow, fromCol, toCol ); +} + +/*! + Sets the stretch factor of row \a row to \a stretch. The first row + is number 0. + + The stretch factor is relative to the other rows in this grid. + Rows with a higher stretch factor take more of the available + space. + + The default stretch factor is 0. If the stretch factor is 0 and no + other row in this table can grow at all, the row may still grow. + + \sa rowStretch(), setRowSpacing(), setColStretch() +*/ +void QGridLayout::setRowStretch( int row, int stretch ) +{ + data->setRowStretch( row, stretch ); +} + +/*! + Returns the stretch factor for row \a row. + + \sa setRowStretch() +*/ +int QGridLayout::rowStretch( int row ) const +{ + return data->rowStretch( row ); +} + +/*! + Returns the stretch factor for column \a col. + + \sa setColStretch() +*/ +int QGridLayout::colStretch( int col ) const +{ + return data->colStretch( col ); +} + +/*! + Sets the stretch factor of column \a col to \a stretch. The first + column is number 0. + + The stretch factor is relative to the other columns in this grid. + Columns with a higher stretch factor take more of the available + space. + + The default stretch factor is 0. If the stretch factor is 0 and no + other column in this table can grow at all, the column may still + grow. + + \sa colStretch(), addColSpacing(), setRowStretch() +*/ +void QGridLayout::setColStretch( int col, int stretch ) +{ + data->setColStretch( col, stretch ); +} + +#if QT_VERSION >= 0x040000 +#error "Make add{Row,Col}Spacing() inline QT_NO_COMPAT functions defined in terms of set{Row,Col}Spacing()" +#endif + +/*! + \obsolete + + Sets the minimum height of row \a row to \a minsize pixels. + + Use setRowSpacing() instead. +*/ +void QGridLayout::addRowSpacing( int row, int minsize ) +{ + QLayoutItem *b = new QSpacerItem( 0, minsize ); + add( b, row, 0 ); +} + +/*! + \obsolete + + Sets the minimum width of column \a col to \a minsize pixels. + + Use setColSpacing() instead. +*/ +void QGridLayout::addColSpacing( int col, int minsize ) +{ + QLayoutItem *b = new QSpacerItem( minsize, 0 ); + add( b, 0, col ); +} + +/*! + Sets the minimum height of row \a row to \a minSize pixels. + + \sa rowSpacing(), setColSpacing() +*/ +void QGridLayout::setRowSpacing( int row, int minSize ) +{ + data->setRowSpacing( row, minSize ); +} + +/*! + Returns the row spacing for row \a row. + + \sa setRowSpacing() +*/ +int QGridLayout::rowSpacing( int row ) const +{ + return data->rowSpacing( row ); +} + +/*! + Sets the minimum width of column \a col to \a minSize pixels. + + \sa colSpacing(), setRowSpacing() +*/ +void QGridLayout::setColSpacing( int col, int minSize ) +{ + data->setColSpacing( col, minSize ); +} + +/*! + Returns the column spacing for column \a col. + + \sa setColSpacing() +*/ +int QGridLayout::colSpacing( int col ) const +{ + return data->colSpacing( col ); +} + +/*! + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. +*/ +QSizePolicy::ExpandData QGridLayout::expanding() const +{ + return data->expanding( spacing() ); +} + +/*! + Sets the grid's origin corner, i.e. position (0, 0), to \a c. +*/ +void QGridLayout::setOrigin( Corner c ) +{ + data->setReversed( c == BottomLeft || c == BottomRight, + c == TopRight || c == BottomRight ); +} + +/*! + Returns the corner that's used for the grid's origin, i.e. for + position (0, 0). +*/ +QGridLayout::Corner QGridLayout::origin() const +{ + if ( data->horReversed() ) { + return data->verReversed() ? BottomRight : TopRight; + } else { + return data->verReversed() ? BottomLeft : TopLeft; + } +} + +/*! + Resets cached information. +*/ +void QGridLayout::invalidate() +{ + QLayout::invalidate(); + QLayout::setGeometry( QRect() ); // for binary compatibility (?) + data->setDirty(); +} + +/*! \reimp */ +QLayoutIterator QGridLayout::iterator() +{ + return QLayoutIterator( new QGridLayoutDataIterator(data) ); +} + +struct QBoxLayoutItem +{ + QBoxLayoutItem( QLayoutItem *it, int stretch_ = 0 ) + : item( it ), stretch( stretch_ ), magic( FALSE ) { } + ~QBoxLayoutItem() { delete item; } + + int hfw( int w ) { + if ( item->hasHeightForWidth() ) { + return item->heightForWidth( w ); + } else { + return item->sizeHint().height(); + } + } + int mhfw( int w ) { + if ( item->hasHeightForWidth() ) { + return item->heightForWidth( w ); + } else { + return item->minimumSize().height(); + } + } + int hStretch() { + if ( stretch == 0 && item->widget() ) { + return item->widget()->sizePolicy().horStretch(); + } else { + return stretch; + } + } + int vStretch() { + if ( stretch == 0 && item->widget() ) { + return item->widget()->sizePolicy().verStretch(); + } else { + return stretch; + } + } + + QLayoutItem *item; + int stretch; + bool magic; +}; + +class QBoxLayoutData +{ +public: + QBoxLayoutData() : geomArray( 0 ), hfwWidth( -1 ), dirty( TRUE ) + { list.setAutoDelete( TRUE ); } + + ~QBoxLayoutData() { delete geomArray; } + void setDirty() { + delete geomArray; + geomArray = 0; + hfwWidth = -1; + hfwHeight = -1; + dirty = TRUE; + } + + QPtrList<QBoxLayoutItem> list; + QMemArray<QLayoutStruct> *geomArray; + int hfwWidth; + int hfwHeight; + int hfwMinHeight; + QSize sizeHint; + QSize minSize; + QSize maxSize; + QSizePolicy::ExpandData expanding; + uint hasHfw : 1; + uint dirty : 1; +}; + +class QBoxLayoutIterator : public QGLayoutIterator +{ +public: + QBoxLayoutIterator( QBoxLayoutData *d ) : data( d ), idx( 0 ) {} + QLayoutItem *current() { + if ( idx >= int(data->list.count()) ) + return 0; + return data->list.at(idx)->item; + } + QLayoutItem *next() { + idx++; + return current(); + } + QLayoutItem *takeCurrent() { + QLayoutItem *item = 0; + + QBoxLayoutItem *b = data->list.take( idx ); + if ( b ) { + item = b->item; + b->item = 0; + delete b; + } + data->setDirty(); + return item; + } + +private: + QBoxLayoutData *data; + int idx; +}; + +/*! + \class QBoxLayout + + \brief The QBoxLayout class lines up child widgets horizontally or + vertically. + + \ingroup geomanagement + \ingroup appearance + + QBoxLayout takes the space it gets (from its parent layout or from + the mainWidget()), divides it up into a row of boxes, and makes + each managed widget fill one box. + + \img qhbox-m.png Horizontal box with five child widgets + + If the QBoxLayout's orientation is \c Horizontal the boxes are + placed in a row, with suitable sizes. Each widget (or other box) + will get at least its minimum size and at most its maximum size. + Any excess space is shared according to the stretch factors (more + about that below). + + \img qvbox-m.png Vertical box with five child widgets + + If the QBoxLayout's orientation is \c Vertical, the boxes are + placed in a column, again with suitable sizes. + + The easiest way to create a QBoxLayout is to use one of the + convenience classes, e.g. QHBoxLayout (for \c Horizontal boxes) or + QVBoxLayout (for \c Vertical boxes). You can also use the + QBoxLayout constructor directly, specifying its direction as \c + LeftToRight, \c Down, \c RightToLeft or \c Up. + + If the QBoxLayout is not the top-level layout (i.e. it is not + managing all of the widget's area and children), you must add it + to its parent layout before you can do anything with it. The + normal way to add a layout is by calling + parentLayout-\>addLayout(). + + Once you have done this, you can add boxes to the QBoxLayout using + one of four functions: + + \list + \i addWidget() to add a widget to the QBoxLayout and set the + widget's stretch factor. (The stretch factor is along the row of + boxes.) + + \i addSpacing() to create an empty box; this is one of the + functions you use to create nice and spacious dialogs. See below + for ways to set margins. + + \i addStretch() to create an empty, stretchable box. + + \i addLayout() to add a box containing another QLayout to the row + and set that layout's stretch factor. + \endlist + + Use insertWidget(), insertSpacing(), insertStretch() or + insertLayout() to insert a box at a specified position in the + layout. + + QBoxLayout also includes two margin widths: + + \list + \i setMargin() sets the width of the outer border. This is the width + of the reserved space along each of the QBoxLayout's four sides. + \i setSpacing() sets the width between neighboring boxes. (You + can use addSpacing() to get more space at a particular spot.) + \endlist + + The margin defaults to 0. The spacing defaults to the same as the + margin width for a top-level layout, or to the same as the parent + layout. Both are parameters to the constructor. + + To remove a widget from a layout, call remove(). Calling + QWidget::hide() on a widget also effectively removes the widget + from the layout until QWidget::show() is called. + + You will almost always want to use QVBoxLayout and QHBoxLayout + rather than QBoxLayout because of their convenient constructors. + + \sa QGrid \link layout.html Layout Overview \endlink +*/ + +/*! + \enum QBoxLayout::Direction + + This type is used to determine the direction of a box layout. + + \value LeftToRight Horizontal, from left to right + \value RightToLeft Horizontal, from right to left + \value TopToBottom Vertical, from top to bottom + \value Down The same as \c TopToBottom + \value BottomToTop Vertical, from bottom to top + \value Up The same as \c BottomToTop +*/ + +static inline bool horz( QBoxLayout::Direction dir ) +{ + return dir == QBoxLayout::RightToLeft || dir == QBoxLayout::LeftToRight; +} + +/*! + Constructs a new QBoxLayout with direction \a d and main widget \a + parent. \a parent may not be 0. + + The \a margin is the number of pixels between the edge of the + widget and its managed children. The \a spacing is the default + number of pixels between neighboring children. If \a spacing is -1 + the value of \a margin is used for \a spacing. + + \a name is the internal object name. + + \sa direction() +*/ +QBoxLayout::QBoxLayout( QWidget *parent, Direction d, + int margin, int spacing, const char *name ) + : QLayout( parent, margin, spacing, name ) +{ + data = new QBoxLayoutData; + dir = d; + setSupportsMargin( TRUE ); +} + +/*! + Constructs a new QBoxLayout called \a name, with direction \a d, + and inserts it into \a parentLayout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, the layout will inherit its + parent's spacing(). +*/ +QBoxLayout::QBoxLayout( QLayout *parentLayout, Direction d, int spacing, + const char *name ) + : QLayout( parentLayout, spacing, name ) +{ + data = new QBoxLayoutData; + dir = d; + setSupportsMargin( TRUE ); +} + +/*! + Constructs a new QBoxLayout called \a name, with direction \a d. + + If \a spacing is -1, the layout will inherit its parent's + spacing(); otherwise \a spacing is used. + + You must insert this box into another layout. +*/ +QBoxLayout::QBoxLayout( Direction d, int spacing, const char *name ) + : QLayout( spacing, name ) +{ + data = new QBoxLayoutData; + dir = d; + setSupportsMargin( TRUE ); +} + +/*! + Destroys this box layout. + + The layout's widgets aren't destroyed. +*/ +QBoxLayout::~QBoxLayout() +{ + delete data; +} + +/*! + Returns the preferred size of this box layout. +*/ +QSize QBoxLayout::sizeHint() const +{ + if ( data->dirty ) { + QBoxLayout *that = (QBoxLayout*)this; + that->setupGeom(); + } + return data->sizeHint + QSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the minimum size needed by this box layout. +*/ +QSize QBoxLayout::minimumSize() const +{ + if ( data->dirty ) { + QBoxLayout *that = (QBoxLayout*)this; + that->setupGeom(); + } + return data->minSize + QSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the maximum size needed by this box layout. +*/ +QSize QBoxLayout::maximumSize() const +{ + if ( data->dirty ) { + QBoxLayout *that = (QBoxLayout*)this; + that->setupGeom(); + } + QSize s = ( data->maxSize + QSize(2 * margin(), 2 * margin()) ) + .boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX)); + if ( alignment() & Qt::AlignHorizontal_Mask ) + s.setWidth( QLAYOUTSIZE_MAX ); + if ( alignment() & Qt::AlignVertical_Mask ) + s.setHeight( QLAYOUTSIZE_MAX ); + return s; +} + +/*! + Returns TRUE if this layout's preferred height depends on its width; + otherwise returns FALSE. +*/ +bool QBoxLayout::hasHeightForWidth() const +{ + if ( data->dirty ) { + QBoxLayout *that = (QBoxLayout*)this; + that->setupGeom(); + } + return data->hasHfw; +} + +/*! + Returns the layout's preferred height when it is \a w pixels wide. +*/ +int QBoxLayout::heightForWidth( int w ) const +{ + if ( !hasHeightForWidth() ) + return -1; + w -= 2 * margin(); + if ( w != data->hfwWidth ) { + QBoxLayout *that = (QBoxLayout*)this; + that->calcHfw( w ); + } + return data->hfwHeight + 2 * margin(); +} + +/*! \internal */ +int QBoxLayout::minimumHeightForWidth( int w ) const +{ + (void) heightForWidth( w ); + return data->hasHfw ? (data->hfwMinHeight + 2 * margin() ) : -1; +} + +/*! + Resets cached information. +*/ +void QBoxLayout::invalidate() +{ + QLayout::invalidate(); + data->setDirty(); +} + +/*! + \reimp +*/ +QLayoutIterator QBoxLayout::iterator() +{ + return QLayoutIterator( new QBoxLayoutIterator(data) ); +} + +/*! + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. +*/ +QSizePolicy::ExpandData QBoxLayout::expanding() const +{ + if ( data->dirty ) { + QBoxLayout *that = (QBoxLayout*)this; + that->setupGeom(); + } + return data->expanding; +} + +/*! + Resizes managed widgets within the rectangle \a r. +*/ +void QBoxLayout::setGeometry( const QRect &r ) +{ + if ( !data->geomArray || r != geometry() ) { + QLayout::setGeometry( r ); + if ( !data->geomArray ) + setupGeom(); + QRect cr = alignment() ? alignmentRect( r ) : r; + QRect s( cr.x() + margin(), cr.y() + margin(), + cr.width() - 2 * margin(), cr.height() - 2 * margin() ); + + QMemArray<QLayoutStruct> a = *data->geomArray; + int pos = horz( dir ) ? s.x() : s.y(); + int space = horz( dir ) ? s.width() : s.height(); + int n = a.count(); + if ( data->hasHfw && !horz(dir) ) { + for ( int i = 0; i < n; i++ ) { + QBoxLayoutItem *box = data->list.at( i ); + if ( box->item->hasHeightForWidth() ) + a[i].sizeHint = a[i].minimumSize = + box->item->heightForWidth( s.width() ); + } + } + + Direction visualDir = dir; + if ( QApplication::reverseLayout() ) { + if ( dir == LeftToRight ) + visualDir = RightToLeft; + else if ( dir == RightToLeft ) + visualDir = LeftToRight; + } + + qGeomCalc( a, 0, n, pos, space, spacing() ); + for ( int i = 0; i < n; i++ ) { + QBoxLayoutItem *box = data->list.at( i ); + + switch ( visualDir ) { + case LeftToRight: + box->item->setGeometry( QRect(a[i].pos, s.y(), + a[i].size, s.height()) ); + break; + case RightToLeft: + box->item->setGeometry( QRect(s.left() + s.right() + - a[i].pos - a[i].size + 1, s.y(), + a[i].size, s.height()) ); + break; + case TopToBottom: + box->item->setGeometry( QRect(s.x(), a[i].pos, + s.width(), a[i].size) ); + break; + case BottomToTop: + box->item->setGeometry( QRect(s.x(), s.top() + s.bottom() + - a[i].pos - a[i].size + 1, + s.width(), a[i].size) ); + } + } + } +} + +/*! + Adds \a item to the end of this box layout. +*/ +void QBoxLayout::addItem( QLayoutItem *item ) +{ + QBoxLayoutItem *it = new QBoxLayoutItem( item ); + data->list.append( it ); + invalidate(); +} + +/*! + Inserts \a item into this box layout at position \a index. If \a + index is negative, the item is added at the end. + + \warning Does not call QLayout::insertChildLayout() if \a item is + a QLayout. + + \sa addItem(), findWidget() +*/ +void QBoxLayout::insertItem( int index, QLayoutItem *item ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + QBoxLayoutItem *it = new QBoxLayoutItem( item ); + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts a non-stretchable space at position \a index, with size \a + size. If \a index is negative the space is added at the end. + + The box layout has default margin and spacing. This function adds + additional space. + + \sa insertStretch() +*/ +void QBoxLayout::insertSpacing( int index, int size ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + // hack in QGridLayoutData: spacers do not get insideSpacing + QLayoutItem *b; + if ( horz( dir ) ) + b = new QSpacerItem( size, 0, QSizePolicy::Fixed, + QSizePolicy::Minimum ); + else + b = new QSpacerItem( 0, size, QSizePolicy::Minimum, + QSizePolicy::Fixed ); + + QBoxLayoutItem *it = new QBoxLayoutItem( b ); + it->magic = TRUE; + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts a stretchable space at position \a index, with zero + minimum size and stretch factor \a stretch. If \a index is + negative the space is added at the end. + + \sa insertSpacing() +*/ +void QBoxLayout::insertStretch( int index, int stretch ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + // hack in QGridLayoutData: spacers do not get insideSpacing + QLayoutItem *b; + if ( horz( dir ) ) + b = new QSpacerItem( 0, 0, QSizePolicy::Expanding, + QSizePolicy::Minimum ); + else + b = new QSpacerItem( 0, 0, QSizePolicy::Minimum, + QSizePolicy::Expanding ); + + QBoxLayoutItem *it = new QBoxLayoutItem( b, stretch ); + it->magic = TRUE; + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts \a layout at position \a index, with stretch factor \a + stretch. If \a index is negative, the layout is added at the end. + + \a layout becomes a child of the box layout. + + \sa setAutoAdd(), insertWidget(), insertSpacing() +*/ +void QBoxLayout::insertLayout( int index, QLayout *layout, int stretch ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + addChildLayout( layout ); + QBoxLayoutItem *it = new QBoxLayoutItem( layout, stretch ); + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts \a widget at position \a index, with stretch factor \a + stretch and alignment \a alignment. If \a index is negative, the + widget is added at the end. + + The stretch factor applies only in the \link direction() direction + \endlink of the QBoxLayout, and is relative to the other boxes and + widgets in this QBoxLayout. Widgets and boxes with higher stretch + factors grow more. + + If the stretch factor is 0 and nothing else in the QBoxLayout has + a stretch factor greater than zero, the space is distributed + according to the QWidget:sizePolicy() of each widget that's + involved. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l Qt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + From Qt 3.0, the \a alignment parameter is interpreted more + aggressively than in previous versions of Qt. A non-default + alignment now indicates that the widget should not grow to fill + the available space, but should be sized according to sizeHint(). + + \sa setAutoAdd(), insertLayout(), insertSpacing() +*/ +void QBoxLayout::insertWidget( int index, QWidget *widget, int stretch, + int alignment ) +{ + if ( !checkWidget(this, widget) ) + return; + + if ( index < 0 ) // append + index = data->list.count(); + + QWidgetItem *b = new QWidgetItem( widget ); + b->setAlignment( alignment ); + QBoxLayoutItem *it = new QBoxLayoutItem( b, stretch ); + data->list.insert( index, it ); + invalidate(); +} + +/*! + Adds a non-stretchable space with size \a size to the end of this + box layout. QBoxLayout provides default margin and spacing. This + function adds additional space. + + \sa insertSpacing(), addStretch() +*/ +void QBoxLayout::addSpacing( int size ) +{ + insertSpacing( -1, size ); +} + +/*! + Adds a stretchable space with zero minimum size and stretch factor + \a stretch to the end of this box layout. + + \sa addSpacing() +*/ +void QBoxLayout::addStretch( int stretch ) +{ + insertStretch( -1, stretch ); +} + +/*! + Adds \a widget to the end of this box layout, with a stretch + factor of \a stretch and alignment \a alignment. + + The stretch factor applies only in the \link direction() direction + \endlink of the QBoxLayout, and is relative to the other boxes and + widgets in this QBoxLayout. Widgets and boxes with higher stretch + factors grow more. + + If the stretch factor is 0 and nothing else in the QBoxLayout has + a stretch factor greater than zero, the space is distributed + according to the QWidget:sizePolicy() of each widget that's + involved. + + Alignment is specified by \a alignment which is a bitwise OR of \l + Qt::AlignmentFlags values. The default alignment is 0, which means + that the widget fills the entire cell. + + From Qt 3.0, the \a alignment parameter is interpreted more + aggressively than in previous versions of Qt. A non-default + alignment now indicates that the widget should not grow to fill + the available space, but should be sized according to sizeHint(). + + \sa insertWidget(), setAutoAdd(), addLayout(), addSpacing() +*/ +void QBoxLayout::addWidget( QWidget *widget, int stretch, + int alignment ) +{ + insertWidget( -1, widget, stretch, alignment ); +} + +/*! + Adds \a layout to the end of the box, with serial stretch factor + \a stretch. + + When a layout is constructed with another layout as its parent, + you don't need to call addLayout(); the child layout is + automatically added to the parent layout as it is constructed. + + \sa insertLayout(), setAutoAdd(), addWidget(), addSpacing() +*/ +void QBoxLayout::addLayout( QLayout *layout, int stretch ) +{ + insertLayout( -1, layout, stretch ); +} + +/*! + Limits the perpendicular dimension of the box (e.g. height if the + box is LeftToRight) to a minimum of \a size. Other constraints may + increase the limit. +*/ +void QBoxLayout::addStrut( int size ) +{ + QLayoutItem *b; + if ( horz( dir ) ) + b = new QSpacerItem( 0, size, QSizePolicy::Fixed, + QSizePolicy::Minimum ); + else + b = new QSpacerItem( size, 0, QSizePolicy::Minimum, + QSizePolicy::Fixed ); + + QBoxLayoutItem *it = new QBoxLayoutItem( b ); + it->magic = TRUE; + data->list.append( it ); + invalidate(); +} + +/*! + Searches for widget \a w in this layout (not including child + layouts). + + Returns the index of \a w, or -1 if \a w is not found. +*/ +int QBoxLayout::findWidget( QWidget* w ) +{ + const int n = data->list.count(); + for ( int i = 0; i < n; i++ ) { + if ( data->list.at(i)->item->widget() == w ) + return i; + } + return -1; +} + +/*! + Sets the stretch factor for widget \a w to \a stretch and returns + TRUE if \a w is found in this layout (not including child + layouts); otherwise returns FALSE. +*/ +bool QBoxLayout::setStretchFactor( QWidget *w, int stretch ) +{ + QPtrListIterator<QBoxLayoutItem> it( data->list ); + QBoxLayoutItem *box; + while ( (box=it.current()) != 0 ) { + ++it; + if ( box->item->widget() == w ) { + box->stretch = stretch; + invalidate(); + return TRUE; + } + } + return FALSE; +} + +/*! + \overload + + Sets the stretch factor for the layout \a l to \a stretch and + returns TRUE if \a l is found in this layout (not including child + layouts); otherwise returns FALSE. +*/ +bool QBoxLayout::setStretchFactor( QLayout *l, int stretch ) +{ + QPtrListIterator<QBoxLayoutItem> it( data->list ); + QBoxLayoutItem *box; + while ( (box=it.current()) != 0 ) { + ++it; + if ( box->item->layout() == l ) { + box->stretch = stretch; + invalidate(); + return TRUE; + } + } + return FALSE; +} + +/*! + Sets the direction of this layout to \a direction. +*/ +void QBoxLayout::setDirection( Direction direction ) +{ + if ( dir == direction ) + return; + if ( horz(dir) != horz(direction) ) { + //swap around the spacers (the "magic" bits) + //#### a bit yucky, knows too much. + //#### probably best to add access functions to spacerItem + //#### or even a QSpacerItem::flip() + QPtrListIterator<QBoxLayoutItem> it( data->list ); + QBoxLayoutItem *box; + while ( (box=it.current()) != 0 ) { + ++it; + if ( box->magic ) { + QSpacerItem *sp = box->item->spacerItem(); + if ( sp ) { + if ( sp->expanding() == QSizePolicy::NoDirection ) { + //spacing or strut + QSize s = sp->sizeHint(); + sp->changeSize( s.height(), s.width(), + horz(direction) ? QSizePolicy::Fixed:QSizePolicy::Minimum, + horz(direction) ? QSizePolicy::Minimum:QSizePolicy::Fixed ); + + } else { + //stretch + if ( horz(direction) ) + sp->changeSize( 0, 0, QSizePolicy::Expanding, + QSizePolicy::Minimum ); + else + sp->changeSize( 0, 0, QSizePolicy::Minimum, + QSizePolicy::Expanding ); + } + } + } + } + } + dir = direction; + invalidate(); + if ( mainWidget() ) { + QEvent *lh = new QEvent( QEvent::LayoutHint ); + QApplication::postEvent( mainWidget(), lh ); + } + +} + +/* + Initializes the data structure needed by qGeomCalc and + recalculates max/min and size hint. +*/ +void QBoxLayout::setupGeom() +{ + if ( !data->dirty ) + return; + + int maxw = horz( dir ) ? 0 : QLAYOUTSIZE_MAX; + int maxh = horz( dir ) ? QLAYOUTSIZE_MAX : 0; + int minw = 0; + int minh = 0; + int hintw = 0; + int hinth = 0; + + bool horexp = FALSE; + bool verexp = FALSE; + + data->hasHfw = FALSE; + + delete data->geomArray; + int n = data->list.count(); + data->geomArray = new QMemArray<QLayoutStruct>( n ); + QMemArray<QLayoutStruct>& a = *data->geomArray; + + bool first = TRUE; + for ( int i = 0; i < n; i++ ) { + QBoxLayoutItem *box = data->list.at( i ); + QSize max = box->item->maximumSize(); + QSize min = box->item->minimumSize(); + QSize hint = box->item->sizeHint(); + QSizePolicy::ExpandData exp = box->item->expanding(); + bool empty = box->item->isEmpty(); + // space before non-empties, except the first: + int space = ( empty || first ) ? 0 : spacing(); + bool ignore = empty && box->item->widget(); // ignore hidden widgets + + if ( horz(dir) ) { + bool expand = exp & QSizePolicy::Horizontally || box->stretch > 0; + horexp = horexp || expand; + maxw += max.width() + space; + minw += min.width() + space; + hintw += hint.width() + space; + if ( !ignore ) + qMaxExpCalc( maxh, verexp, + max.height(), exp & QSizePolicy::Vertically ); + minh = QMAX( minh, min.height() ); + hinth = QMAX( hinth, hint.height() ); + + a[i].sizeHint = hint.width(); + a[i].maximumSize = max.width(); + a[i].minimumSize = min.width(); + a[i].expansive = expand; + a[i].stretch = box->stretch ? box->stretch : box->hStretch(); + } else { + bool expand = ( exp & QSizePolicy::Vertically || box->stretch > 0 ); + verexp = verexp || expand; + maxh += max.height() + space; + minh += min.height() + space; + hinth += hint.height() + space; + if ( !ignore ) + qMaxExpCalc( maxw, horexp, + max.width(), exp & QSizePolicy::Horizontally ); + minw = QMAX( minw, min.width() ); + hintw = QMAX( hintw, hint.width() ); + + a[i].sizeHint = hint.height(); + a[i].maximumSize = max.height(); + a[i].minimumSize = min.height(); + a[i].expansive = expand; + a[i].stretch = box->stretch ? box->stretch : box->vStretch(); + } + + a[i].empty = empty; + data->hasHfw = data->hasHfw || box->item->hasHeightForWidth(); + first = first && empty; + } + + data->minSize = QSize( minw, minh ); + data->maxSize = QSize( maxw, maxh ).expandedTo( data->minSize ); + + data->expanding = (QSizePolicy::ExpandData) + ( (horexp ? QSizePolicy::Horizontally : 0) + | (verexp ? QSizePolicy::Vertically : 0) ); + + data->sizeHint = QSize( hintw, hinth ) + .expandedTo( data->minSize ) + .boundedTo( data->maxSize ); + + data->dirty = FALSE; +} + +/* + Calculates and stores the preferred height given the width \a w. +*/ +void QBoxLayout::calcHfw( int w ) +{ + int h = 0; + int mh = 0; + + if ( horz(dir) ) { + QMemArray<QLayoutStruct> &a = *data->geomArray; + int n = a.count(); + qGeomCalc( a, 0, n, 0, w, spacing() ); + for ( int i = 0; i < n; i++ ) { + QBoxLayoutItem *box = data->list.at(i); + h = QMAX( h, box->hfw(a[i].size) ); + mh = QMAX( mh, box->mhfw(a[i].size) ); + } + } else { + QPtrListIterator<QBoxLayoutItem> it( data->list ); + QBoxLayoutItem *box; + bool first = TRUE; + while ( (box = it.current()) != 0 ) { + ++it; + bool empty = box->item->isEmpty(); + h += box->hfw( w ); + mh += box->mhfw( w ); + if ( !first && !empty ) { + h += spacing(); + mh += spacing(); + } + first = first && empty; + } + } + data->hfwWidth = w; + data->hfwHeight = h; + data->hfwMinHeight = mh; +} + +/*! + \fn QBoxLayout::Direction QBoxLayout::direction() const + + Returns the direction of the box. addWidget() and addSpacing() + work in this direction; the stretch stretches in this direction. + + \sa QBoxLayout::Direction addWidget() addSpacing() +*/ + +/*! + \class QHBoxLayout + \brief The QHBoxLayout class lines up widgets horizontally. + + \ingroup geomanagement + \ingroup appearance + \mainclass + + This class is used to construct horizontal box layout objects. See + \l QBoxLayout for more details. + + The simplest use of the class is like this: + \code + QBoxLayout * l = new QHBoxLayout( widget ); + l->setAutoAdd( TRUE ); + new QSomeWidget( widget ); + new QSomeOtherWidget( widget ); + new QAnotherWidget( widget ); + \endcode + + or like this: + \code + QBoxLayout * l = new QHBoxLayout( widget ); + l->addWidget( existingChildOfWidget ); + l->addWidget( anotherChildOfWidget ); + \endcode + + \img qhboxlayout.png QHBox + + \sa QVBoxLayout QGridLayout + \link layout.html the Layout overview \endlink +*/ + +/*! + Constructs a new top-level horizontal box called \a name, with + parent \a parent. + + The \a margin is the number of pixels between the edge of the + widget and its managed children. The \a spacing is the default + number of pixels between neighboring children. If \a spacing is -1 + the value of \a margin is used for \a spacing. +*/ +QHBoxLayout::QHBoxLayout( QWidget *parent, int margin, + int spacing, const char *name ) + : QBoxLayout( parent, LeftToRight, margin, spacing, name ) +{ +} + +/*! + Constructs a new horizontal box called name \a name and adds it to + \a parentLayout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this QHBoxLayout will inherit its + parent's spacing(). +*/ +QHBoxLayout::QHBoxLayout( QLayout *parentLayout, int spacing, + const char *name ) + : QBoxLayout( parentLayout, LeftToRight, spacing, name ) +{ +} + +/*! + Constructs a new horizontal box called name \a name. You must add + it to another layout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this QHBoxLayout will inherit its + parent's spacing(). +*/ +QHBoxLayout::QHBoxLayout( int spacing, const char *name ) + : QBoxLayout( LeftToRight, spacing, name ) +{ +} + +/*! + Destroys this box layout. + + The layout's widgets aren't destroyed. +*/ +QHBoxLayout::~QHBoxLayout() +{ +} + +/*! + \class QVBoxLayout + + \brief The QVBoxLayout class lines up widgets vertically. + + \ingroup geomanagement + \ingroup appearance + \mainclass + + This class is used to construct vertical box layout objects. See + QBoxLayout for more details. + + The simplest use of the class is like this: + \code + QBoxLayout * l = new QVBoxLayout( widget ); + l->addWidget( aWidget ); + l->addWidget( anotherWidget ); + \endcode + + \img qvboxlayout.png QVBox + + \sa QHBoxLayout QGridLayout \link layout.html the Layout overview \endlink +*/ + +/*! + Constructs a new top-level vertical box called \a name, with + parent \a parent. + + The \a margin is the number of pixels between the edge of the + widget and its managed children. The \a spacing is the default + number of pixels between neighboring children. If \a spacing is -1 + the value of \a margin is used for \a spacing. +*/ +QVBoxLayout::QVBoxLayout( QWidget *parent, int margin, int spacing, + const char *name ) + : QBoxLayout( parent, TopToBottom, margin, spacing, name ) +{ + +} + +/*! + Constructs a new vertical box called name \a name and adds it to + \a parentLayout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this QVBoxLayout will inherit its + parent's spacing(). +*/ +QVBoxLayout::QVBoxLayout( QLayout *parentLayout, int spacing, + const char *name ) + : QBoxLayout( parentLayout, TopToBottom, spacing, name ) +{ +} + +/*! + Constructs a new vertical box called name \a name. You must add + it to another layout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this QVBoxLayout will inherit its + parent's spacing(). +*/ +QVBoxLayout::QVBoxLayout( int spacing, const char *name ) + : QBoxLayout( TopToBottom, spacing, name ) +{ +} + +/*! + Destroys this box layout. + + The layout's widgets aren't destroyed. +*/ +QVBoxLayout::~QVBoxLayout() +{ +} + +QBoxLayout *QBoxLayout::createTmpCopy() +{ + QBoxLayout *bl = new QBoxLayout( direction() ); + delete bl->data; + bl->data = data; + return bl; +} + +#endif // QT_NO_LAYOUT diff --git a/src/kernel/qlayout.h b/src/kernel/qlayout.h new file mode 100644 index 0000000..7e96eda --- /dev/null +++ b/src/kernel/qlayout.h @@ -0,0 +1,473 @@ +/**************************************************************************** +** +** Definition of layout classes +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QLAYOUT_H +#define QLAYOUT_H + +#ifndef QT_H +#include "qobject.h" +#include "qsizepolicy.h" +#include "qwidget.h" +#endif // QT_H + +#include <limits.h> + +#ifndef QT_NO_LAYOUT + +#if 0 +Q_OBJECT +#endif + +static const int QLAYOUTSIZE_MAX = INT_MAX/256/16; + +class QGridLayoutBox; +class QGridLayoutData; +class QLayout; +class QLayoutItem; +struct QLayoutData; +class QMenuBar; +class QSpacerItem; +class QWidget; + +class Q_EXPORT QGLayoutIterator : public QShared +{ +public: + virtual ~QGLayoutIterator(); + virtual QLayoutItem *next() = 0; + virtual QLayoutItem *current() = 0; + virtual QLayoutItem *takeCurrent() = 0; +}; + +class Q_EXPORT QLayoutIterator +{ +public: + QLayoutIterator( QGLayoutIterator *i ) : it( i ) { } + QLayoutIterator( const QLayoutIterator &i ) : it( i.it ) { + if ( it ) + it->ref(); + } + ~QLayoutIterator() { if ( it && it->deref() ) delete it; } + QLayoutIterator &operator=( const QLayoutIterator &i ) { + if ( i.it ) + i.it->ref(); + if ( it && it->deref() ) + delete it; + it = i.it; + return *this; + } + QLayoutItem *operator++() { return it ? it->next() : 0; } + QLayoutItem *current() { return it ? it->current() : 0; } + QLayoutItem *takeCurrent() { return it ? it->takeCurrent() : 0; } + void deleteCurrent(); + +private: + QGLayoutIterator *it; +}; + +class Q_EXPORT QLayoutItem +{ +public: + QLayoutItem( int alignment = 0 ) : align( alignment ) { } + virtual ~QLayoutItem(); + virtual QSize sizeHint() const = 0; + virtual QSize minimumSize() const = 0; + virtual QSize maximumSize() const = 0; + virtual QSizePolicy::ExpandData expanding() const = 0; + virtual void setGeometry( const QRect& ) = 0; + virtual QRect geometry() const = 0; + virtual bool isEmpty() const = 0; + virtual bool hasHeightForWidth() const; + virtual int heightForWidth( int ) const; + // ### add minimumHeightForWidth( int ) in Qt 4.0 + virtual void invalidate(); + + virtual QWidget *widget(); + virtual QLayoutIterator iterator(); + virtual QLayout *layout(); + virtual QSpacerItem *spacerItem(); + + int alignment() const { return align; } + virtual void setAlignment( int a ); + +protected: + int align; +}; + +class Q_EXPORT QSpacerItem : public QLayoutItem +{ +public: + QSpacerItem( int w, int h, + QSizePolicy::SizeType hData = QSizePolicy::Minimum, + QSizePolicy::SizeType vData = QSizePolicy::Minimum ) + : width( w ), height( h ), sizeP( hData, vData ) { } + void changeSize( int w, int h, + QSizePolicy::SizeType hData = QSizePolicy::Minimum, + QSizePolicy::SizeType vData = QSizePolicy::Minimum ); + QSize sizeHint() const; + QSize minimumSize() const; + QSize maximumSize() const; + QSizePolicy::ExpandData expanding() const; + bool isEmpty() const; + void setGeometry( const QRect& ); + QRect geometry() const; + QSpacerItem *spacerItem(); + +private: + int width; + int height; + QSizePolicy sizeP; + QRect rect; +}; + +class Q_EXPORT QWidgetItem : public QLayoutItem +{ +public: + QWidgetItem( QWidget *w ) : wid( w ) { } + QSize sizeHint() const; + QSize minimumSize() const; + QSize maximumSize() const; + QSizePolicy::ExpandData expanding() const; + bool isEmpty() const; + void setGeometry( const QRect& ); + QRect geometry() const; + virtual QWidget *widget(); + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + +private: + QWidget *wid; +}; + +class Q_EXPORT QLayout : public QObject, public QLayoutItem +{ + Q_OBJECT + Q_ENUMS( ResizeMode ) + Q_PROPERTY( int margin READ margin WRITE setMargin ) + Q_PROPERTY( int spacing READ spacing WRITE setSpacing ) + Q_PROPERTY( ResizeMode resizeMode READ resizeMode WRITE setResizeMode ) + +public: + // ### Qt 4.0: put 'Auto' first in enum + enum ResizeMode { FreeResize, Minimum, Fixed, Auto }; + + QLayout( QWidget *parent, int margin = 0, int spacing = -1, + const char *name = 0 ); + QLayout( QLayout *parentLayout, int spacing = -1, const char *name = 0 ); + QLayout( int spacing = -1, const char *name = 0 ); + ~QLayout(); + + int margin() const { return outsideBorder; } + int spacing() const { return insideSpacing; } + + virtual void setMargin( int ); + virtual void setSpacing( int ); + + int defaultBorder() const { return insideSpacing; } + void freeze( int w, int h ); + void freeze() { setResizeMode( Fixed ); } + + void setResizeMode( ResizeMode ); + ResizeMode resizeMode() const; + +#ifndef QT_NO_MENUBAR + virtual void setMenuBar( QMenuBar *w ); + QMenuBar *menuBar() const { return menubar; } +#endif + + QWidget *mainWidget(); + bool isTopLevel() const { return topLevel; } + + virtual void setAutoAdd( bool ); + bool autoAdd() const { return autoNewChild; } + + void invalidate(); + QRect geometry() const; + bool activate(); + + void add( QWidget *w ) { addItem( new QWidgetItem(w) ); } + virtual void addItem( QLayoutItem * ) = 0; + + void remove( QWidget *w ); + void removeItem( QLayoutItem * ); + + QSizePolicy::ExpandData expanding() const; + QSize minimumSize() const; + QSize maximumSize() const; + void setGeometry( const QRect& ) = 0; + QLayoutIterator iterator() = 0; + bool isEmpty() const; + + int totalHeightForWidth( int w ) const; + QSize totalMinimumSize() const; + QSize totalMaximumSize() const; + QSize totalSizeHint() const; + QLayout *layout(); + + bool supportsMargin() const { return marginImpl; } + + void setEnabled( bool ); + bool isEnabled() const; + +protected: + bool eventFilter( QObject *, QEvent * ); + void childEvent( QChildEvent *e ); + void addChildLayout( QLayout *l ); + void deleteAllItems(); + + void setSupportsMargin( bool ); + QRect alignmentRect( const QRect& ) const; + +private: + void setWidgetLayout( QWidget *, QLayout * ); + void init(); + int insideSpacing; + int outsideBorder; + uint topLevel : 1; + uint enabled : 1; + uint autoNewChild : 1; + uint frozen : 1; + uint activated : 1; + uint marginImpl : 1; + uint autoMinimum : 1; + uint autoResizeMode : 1; + QRect rect; + QLayoutData *extraData; +#ifndef QT_NO_MENUBAR + QMenuBar *menubar; +#endif + +private: +#if defined(Q_DISABLE_COPY) + QLayout( const QLayout & ); + QLayout &operator=( const QLayout & ); +#endif + + static void propagateSpacing( QLayout *layout ); +}; + +inline void QLayoutIterator::deleteCurrent() +{ + delete takeCurrent(); +} + +class Q_EXPORT QGridLayout : public QLayout +{ + Q_OBJECT +public: + QGridLayout( QWidget *parent, int nRows = 1, int nCols = 1, int border = 0, + int spacing = -1, const char *name = 0 ); + QGridLayout( int nRows = 1, int nCols = 1, int spacing = -1, + const char *name = 0 ); + QGridLayout( QLayout *parentLayout, int nRows = 1, int nCols = 1, + int spacing = -1, const char *name = 0 ); + ~QGridLayout(); + + QSize sizeHint() const; + QSize minimumSize() const; + QSize maximumSize() const; + + // ### remove 'virtual' in 4.0 (or add 'virtual' to set{Row,Col}Spacing()) + virtual void setRowStretch( int row, int stretch ); + virtual void setColStretch( int col, int stretch ); + int rowStretch( int row ) const; + int colStretch( int col ) const; + + void setRowSpacing( int row, int minSize ); + void setColSpacing( int col, int minSize ); + int rowSpacing( int row ) const; + int colSpacing( int col ) const; + + int numRows() const; + int numCols() const; + QRect cellGeometry( int row, int col ) const; + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + int minimumHeightForWidth( int ) const; + + QSizePolicy::ExpandData expanding() const; + void invalidate(); + + void addItem( QLayoutItem * ); + void addItem( QLayoutItem *item, int row, int col ); + void addMultiCell( QLayoutItem *, int fromRow, int toRow, + int fromCol, int toCol, int align = 0 ); + + void addWidget( QWidget *, int row, int col, int align = 0 ); + void addMultiCellWidget( QWidget *, int fromRow, int toRow, + int fromCol, int toCol, int align = 0 ); + void addLayout( QLayout *layout, int row, int col); + void addMultiCellLayout( QLayout *layout, int fromRow, int toRow, + int fromCol, int toCol, int align = 0 ); + void addRowSpacing( int row, int minsize ); + void addColSpacing( int col, int minsize ); + + void expand( int rows, int cols ); + + enum Corner { TopLeft, TopRight, BottomLeft, BottomRight }; + void setOrigin( Corner ); + Corner origin() const; + QLayoutIterator iterator(); + void setGeometry( const QRect& ); + +protected: + bool findWidget( QWidget* w, int *r, int *c ); + void add( QLayoutItem*, int row, int col ); + +private: +#if defined(Q_DISABLE_COPY) + QGridLayout( const QGridLayout & ); + QGridLayout &operator=( const QGridLayout & ); +#endif + + void init( int rows, int cols ); + QGridLayoutData *data; +}; + +class QBoxLayoutData; +class QDockWindow; + +class Q_EXPORT QBoxLayout : public QLayout +{ + Q_OBJECT +public: + enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop, + Down = TopToBottom, Up = BottomToTop }; + + QBoxLayout( QWidget *parent, Direction, int border = 0, int spacing = -1, + const char *name = 0 ); + QBoxLayout( QLayout *parentLayout, Direction, int spacing = -1, + const char *name = 0 ); + QBoxLayout( Direction, int spacing = -1, const char *name = 0 ); + ~QBoxLayout(); + + void addItem( QLayoutItem * ); + + Direction direction() const { return dir; } + void setDirection( Direction ); + + void addSpacing( int size ); + void addStretch( int stretch = 0 ); + void addWidget( QWidget *, int stretch = 0, int alignment = 0 ); + void addLayout( QLayout *layout, int stretch = 0 ); + void addStrut( int ); + + void insertSpacing( int index, int size ); + void insertStretch( int index, int stretch = 0 ); + void insertWidget( int index, QWidget *widget, int stretch = 0, + int alignment = 0 ); + void insertLayout( int index, QLayout *layout, int stretch = 0 ); + + bool setStretchFactor( QWidget*, int stretch ); + bool setStretchFactor( QLayout *l, int stretch ); + + QSize sizeHint() const; + QSize minimumSize() const; + QSize maximumSize() const; + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + int minimumHeightForWidth( int ) const; + + QSizePolicy::ExpandData expanding() const; + void invalidate(); + QLayoutIterator iterator(); + void setGeometry( const QRect& ); + + int findWidget( QWidget* w ); + +protected: + void insertItem( int index, QLayoutItem * ); + +private: + friend class QDockWindow; +#if defined(Q_DISABLE_COPY) + QBoxLayout( const QBoxLayout & ); + QBoxLayout &operator=( const QBoxLayout & ); +#endif + + void setupGeom(); + void calcHfw( int ); + QBoxLayoutData *data; + Direction dir; + QBoxLayout *createTmpCopy(); +}; + +class Q_EXPORT QHBoxLayout : public QBoxLayout +{ + Q_OBJECT +public: + QHBoxLayout( QWidget *parent, int border = 0, + int spacing = -1, const char *name = 0 ); + QHBoxLayout( QLayout *parentLayout, + int spacing = -1, const char *name = 0 ); + QHBoxLayout( int spacing = -1, const char *name = 0 ); + + ~QHBoxLayout(); + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QHBoxLayout( const QHBoxLayout & ); + QHBoxLayout &operator=( const QHBoxLayout & ); +#endif +}; + +class Q_EXPORT QVBoxLayout : public QBoxLayout +{ + Q_OBJECT +public: + QVBoxLayout( QWidget *parent, int border = 0, + int spacing = -1, const char *name = 0 ); + QVBoxLayout( QLayout *parentLayout, + int spacing = -1, const char *name = 0 ); + QVBoxLayout( int spacing = -1, const char *name = 0 ); + + ~QVBoxLayout(); + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QVBoxLayout( const QVBoxLayout & ); + QVBoxLayout &operator=( const QVBoxLayout & ); +#endif +}; + +#endif // QT_NO_LAYOUT +#endif // QLAYOUT_H diff --git a/src/kernel/qlayoutengine.cpp b/src/kernel/qlayoutengine.cpp new file mode 100644 index 0000000..d96c920 --- /dev/null +++ b/src/kernel/qlayoutengine.cpp @@ -0,0 +1,322 @@ +/**************************************************************************** +** +** Implementation of QLayout functionality +** +** Created : 981231 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qlayout.h" +#include "private/qlayoutengine_p.h" + +#ifndef QT_NO_LAYOUT + +static inline int toFixed( int i ) { return i * 256; } +static inline int fRound( int i ) { + return ( i % 256 < 128 ) ? i / 256 : 1 + i / 256; +} + +/* + This is the main workhorse of the QGridLayout. It portions out + available space to the chain's children. + + The calculation is done in fixed point: "fixed" variables are + scaled by a factor of 256. + + If the layout runs "backwards" (i.e. RightToLeft or Up) the layout + is computed mirror-reversed, and it's the caller's responsibility + do reverse the values before use. + + chain contains input and output parameters describing the geometry. + count is the count of items in the chain; pos and space give the + interval (relative to parentWidget topLeft). +*/ +Q_EXPORT void qGeomCalc( QMemArray<QLayoutStruct> &chain, int start, int count, + int pos, int space, int spacer ) +{ + typedef int fixed; + int cHint = 0; + int cMin = 0; + int cMax = 0; + int sumStretch = 0; + int spacerCount = 0; + + bool wannaGrow = FALSE; // anyone who really wants to grow? + // bool canShrink = FALSE; // anyone who could be persuaded to shrink? + + int i; + for ( i = start; i < start + count; i++ ) { + chain[i].done = FALSE; + cHint += chain[i].smartSizeHint(); + cMin += chain[i].minimumSize; + cMax += chain[i].maximumSize; + sumStretch += chain[i].stretch; + if ( !chain[i].empty ) + spacerCount++; + wannaGrow = wannaGrow || chain[i].expansive || chain[i].stretch > 0; + } + + int extraspace = 0; + if ( spacerCount ) + spacerCount--; // only spacers between things + if ( space < cMin + spacerCount * spacer ) { + for ( i = start; i < start+count; i++ ) { + chain[i].size = chain[i].minimumSize; + chain[i].done = TRUE; + } + } else if ( space < cHint + spacerCount*spacer ) { + /* + Less space than smartSizeHint(), but more than minimumSize. + Currently take space equally from each, as in Qt 2.x. + Commented-out lines will give more space to stretchier + items. + */ + int n = count; + int space_left = space - spacerCount*spacer; + int overdraft = cHint - space_left; + + // first give to the fixed ones: + for ( i = start; i < start + count; i++ ) { + if ( !chain[i].done + && chain[i].minimumSize >= chain[i].smartSizeHint() ) { + chain[i].size = chain[i].smartSizeHint(); + chain[i].done = TRUE; + space_left -= chain[i].smartSizeHint(); + // sumStretch -= chain[i].stretch; + n--; + } + } + bool finished = n == 0; + while ( !finished ) { + finished = TRUE; + fixed fp_over = toFixed( overdraft ); + fixed fp_w = 0; + + for ( i = start; i < start+count; i++ ) { + if ( chain[i].done ) + continue; + // if ( sumStretch <= 0 ) + fp_w += fp_over / n; + // else + // fp_w += (fp_over * chain[i].stretch) / sumStretch; + int w = fRound( fp_w ); + chain[i].size = chain[i].smartSizeHint() - w; + fp_w -= toFixed( w ); // give the difference to the next + if ( chain[i].size < chain[i].minimumSize ) { + chain[i].done = TRUE; + chain[i].size = chain[i].minimumSize; + finished = FALSE; + overdraft -= ( chain[i].smartSizeHint() + - chain[i].minimumSize ); + // sumStretch -= chain[i].stretch; + n--; + break; + } + } + } + } else { // extra space + int n = count; + int space_left = space - spacerCount*spacer; + // first give to the fixed ones, and handle non-expansiveness + for ( i = start; i < start + count; i++ ) { + if ( !chain[i].done + && (chain[i].maximumSize <= chain[i].smartSizeHint() + || (wannaGrow && !chain[i].expansive && chain[i].stretch == 0)) ) { + chain[i].size = chain[i].smartSizeHint(); + chain[i].done = TRUE; + space_left -= chain[i].smartSizeHint(); + sumStretch -= chain[i].stretch; + n--; + } + } + extraspace = space_left; + + /* + Do a trial distribution and calculate how much it is off. + If there are more deficit pixels than surplus pixels, give + the minimum size items what they need, and repeat. + Otherwise give to the maximum size items, and repeat. + + Paul Olav Tvete has a wonderful mathematical proof of the + correctness of this principle, but unfortunately this + comment is too small to contain it. + */ + int surplus, deficit; + do { + surplus = deficit = 0; + fixed fp_space = toFixed( space_left ); + fixed fp_w = 0; + for ( i = start; i < start+count; i++ ) { + if ( chain[i].done ) + continue; + extraspace = 0; + if ( sumStretch <= 0 ) + fp_w += fp_space / n; + else + fp_w += (fp_space * chain[i].stretch) / sumStretch; + int w = fRound( fp_w ); + chain[i].size = w; + fp_w -= toFixed( w ); // give the difference to the next + if ( w < chain[i].smartSizeHint() ) { + deficit += chain[i].smartSizeHint() - w; + } else if ( w > chain[i].maximumSize ) { + surplus += w - chain[i].maximumSize; + } + } + if ( deficit > 0 && surplus <= deficit ) { + // give to the ones that have too little + for ( i = start; i < start+count; i++ ) { + if ( !chain[i].done && + chain[i].size < chain[i].smartSizeHint() ) { + chain[i].size = chain[i].smartSizeHint(); + chain[i].done = TRUE; + space_left -= chain[i].smartSizeHint(); + sumStretch -= chain[i].stretch; + n--; + } + } + } + if ( surplus > 0 && surplus >= deficit ) { + // take from the ones that have too much + for ( i = start; i < start+count; i++ ) { + if ( !chain[i].done && + chain[i].size > chain[i].maximumSize ) { + chain[i].size = chain[i].maximumSize; + chain[i].done = TRUE; + space_left -= chain[i].maximumSize; + sumStretch -= chain[i].stretch; + n--; + } + } + } + } while ( n > 0 && surplus != deficit ); + if ( n == 0 ) + extraspace = space_left; + } + + /* + As a last resort, we distribute the unwanted space equally + among the spacers (counting the start and end of the chain). We + could, but don't, attempt a sub-pixel allocation of the extra + space. + */ + int extra = extraspace / ( spacerCount + 2 ); + int p = pos + extra; + for ( i = start; i < start+count; i++ ) { + chain[i].pos = p; + p = p + chain[i].size; + if ( !chain[i].empty ) + p += spacer+extra; + } +} + +Q_EXPORT QSize qSmartMinSize( const QWidgetItem *i ) +{ + QWidget *w = ((QWidgetItem *)i)->widget(); + + QSize s( 0, 0 ); + if ( w->layout() ) { + s = w->layout()->totalMinimumSize(); + } else { + QSize sh; + + if ( w->sizePolicy().horData() != QSizePolicy::Ignored ) { + if ( w->sizePolicy().mayShrinkHorizontally() ) { + s.setWidth( w->minimumSizeHint().width() ); + } else { + sh = w->sizeHint(); + s.setWidth( sh.width() ); + } + } + + if ( w->sizePolicy().verData() != QSizePolicy::Ignored ) { + if ( w->sizePolicy().mayShrinkVertically() ) { + s.setHeight( w->minimumSizeHint().height() ); + } else { + s.setHeight( sh.isValid() ? sh.height() + : w->sizeHint().height() ); + } + } + } + s = s.boundedTo( w->maximumSize() ); + QSize min = w->minimumSize(); + if ( min.width() > 0 ) + s.setWidth( min.width() ); + if ( min.height() > 0 ) + s.setHeight( min.height() ); + + if ( i->hasHeightForWidth() && min.height() == 0 && min.width() > 0 ) + s.setHeight( i->heightForWidth(s.width()) ); + + s = s.expandedTo( QSize(1, 1) ); + return s; +} + +Q_EXPORT QSize qSmartMinSize( QWidget *w ) +{ + QWidgetItem item( w ); + return qSmartMinSize( &item ); +} + +Q_EXPORT QSize qSmartMaxSize( const QWidgetItem *i, int align ) +{ + QWidget *w = ( (QWidgetItem*)i )->widget(); + if ( align & Qt::AlignHorizontal_Mask && align & Qt::AlignVertical_Mask ) + return QSize( QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX ); + QSize s = w->maximumSize(); + if ( s.width() == QWIDGETSIZE_MAX && !(align & Qt::AlignHorizontal_Mask) ) + if ( !w->sizePolicy().mayGrowHorizontally() ) + s.setWidth( w->sizeHint().width() ); + + if ( s.height() == QWIDGETSIZE_MAX && !(align & Qt::AlignVertical_Mask) ) + if ( !w->sizePolicy().mayGrowVertically() ) + s.setHeight( w->sizeHint().height() ); + + s = s.expandedTo( w->minimumSize() ); + + if ( align & Qt::AlignHorizontal_Mask ) + s.setWidth( QLAYOUTSIZE_MAX ); + if ( align & Qt::AlignVertical_Mask ) + s.setHeight( QLAYOUTSIZE_MAX ); + return s; +} + +Q_EXPORT QSize qSmartMaxSize( QWidget *w, int align ) +{ + QWidgetItem item( w ); + return qSmartMaxSize( &item, align ); +} + +#endif // QT_NO_LAYOUT diff --git a/src/kernel/qlayoutengine_p.h b/src/kernel/qlayoutengine_p.h new file mode 100644 index 0000000..5e8608d --- /dev/null +++ b/src/kernel/qlayoutengine_p.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Internal header file. +** +** Created : 981027 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QLAYOUTENGINE_P_H +#define QLAYOUTENGINE_P_H + +#ifndef QLAYOUT_H + #error "Need to include qlayout.h before including qlayoutengine_p.h" +#endif + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qlayout.cpp, qlayoutengine.cpp, qmainwindow.cpp and qsplitter.cpp. +// This header file may change from version to version without notice, +// or even be removed. +// +// We mean it. +// +// + + +#ifndef QT_H +#include "qabstractlayout.h" +#endif // QT_H + +#ifndef QT_NO_LAYOUT + +struct QLayoutStruct +{ + inline void init( int stretchFactor = 0, int spacing = 0 ) { + stretch = stretchFactor; + minimumSize = sizeHint = spacing; + maximumSize = QLAYOUTSIZE_MAX; + expansive = FALSE; + empty = TRUE; + } + + QCOORD smartSizeHint() { + return ( stretch > 0 ) ? minimumSize : sizeHint; + } + + // parameters + int stretch; + QCOORD sizeHint; + QCOORD maximumSize; + QCOORD minimumSize; + bool expansive; + bool empty; + + // temporary storage + bool done; + + // result + int pos; + int size; +}; + + +Q_EXPORT void qGeomCalc( QMemArray<QLayoutStruct> &chain, int start, int count, + int pos, int space, int spacer ); +Q_EXPORT QSize qSmartMinSize( const QWidgetItem *i ); +Q_EXPORT QSize qSmartMinSize( QWidget *w ); +Q_EXPORT QSize qSmartMaxSize( const QWidgetItem *i, int align = 0 ); +Q_EXPORT QSize qSmartMaxSize( QWidget *w, int align = 0 ); + + +/* + Modify total maximum (max) and total expansion (exp) + when adding boxmax/boxexp. + + Expansive boxes win over non-expansive boxes. +*/ +static inline void qMaxExpCalc( QCOORD & max, bool &exp, + QCOORD boxmax, bool boxexp ) +{ + if ( exp ) { + if ( boxexp ) + max = QMAX( max, boxmax ); + } else { + if ( boxexp ) + max = boxmax; + else + max = QMIN( max, boxmax ); + } + exp = exp || boxexp; +} + +#endif //QT_NO_LAYOUT +#endif diff --git a/src/kernel/qlocalfs.cpp b/src/kernel/qlocalfs.cpp new file mode 100644 index 0000000..e6017f6 --- /dev/null +++ b/src/kernel/qlocalfs.cpp @@ -0,0 +1,407 @@ +/**************************************************************************** +** +** Implementation of QLocalFs class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qlocalfs.h" + +#ifndef QT_NO_NETWORKPROTOCOL + +#include "qfileinfo.h" +#include "qfile.h" +#include "qurlinfo.h" +#include "qapplication.h" +#include "qurloperator.h" +#include "qguardedptr.h" + +//#define QLOCALFS_DEBUG + + +/*! + \class QLocalFs qlocalfs.h + \brief The QLocalFs class is an implementation of a + QNetworkProtocol that works on the local file system. +\if defined(commercial) + It is part of the <a href="commercialeditions.html">Qt Enterprise Edition</a>. +\endif + + \module network + + \ingroup io + + This class is derived from QNetworkProtocol. QLocalFs is not + normally used directly, but rather through a QUrlOperator, for + example: + \code + QUrlOperator op( "file:///tmp" ); + op.listChildren(); // Asks the server to provide a directory listing + \endcode + + This code will only work if the QLocalFs class is registered; to + register the class, you must call qInitNetworkProtocols() before + using a QUrlOperator with QLocalFs. + + If you really need to use QLocalFs directly, don't forget + to set its QUrlOperator with setUrl(). + + \sa \link network.html Qt Network Documentation \endlink QNetworkProtocol, QUrlOperator +*/ + +/*! + Constructor. +*/ + +QLocalFs::QLocalFs() + : QNetworkProtocol() +{ +} + +static int convertPermissions(QFileInfo *fi) +{ + int p = 0; + if ( fi->permission( QFileInfo::ReadOwner ) ) + p |= QUrlInfo::ReadOwner; + if ( fi->permission( QFileInfo::WriteOwner ) ) + p |= QUrlInfo::WriteOwner; + if ( fi->permission( QFileInfo::ExeOwner ) ) + p |= QUrlInfo::ExeOwner; + if ( fi->permission( QFileInfo::ReadGroup ) ) + p |= QUrlInfo::ReadGroup; + if ( fi->permission( QFileInfo::WriteGroup ) ) + p |= QUrlInfo::WriteGroup; + if ( fi->permission( QFileInfo::ExeGroup ) ) + p |= QUrlInfo::ExeGroup; + if ( fi->permission( QFileInfo::ReadOther ) ) + p |= QUrlInfo::ReadOther; + if ( fi->permission( QFileInfo::WriteOther ) ) + p |= QUrlInfo::WriteOther; + if ( fi->permission( QFileInfo::ExeOther ) ) + p |= QUrlInfo::ExeOther; + return p; +} + +/*! + \reimp +*/ + +void QLocalFs::operationListChildren( QNetworkOperation *op ) +{ +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: operationListChildren" ); +#endif + op->setState( StInProgress ); + + dir = QDir( url()->path() ); + dir.setNameFilter( url()->nameFilter() ); + dir.setMatchAllDirs( TRUE ); + if ( !dir.isReadable() ) { + QString msg = tr( "Could not read directory\n%1" ).arg( url()->path() ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrListChildren ); + emit finished( op ); + return; + } + + const QFileInfoList *filist = dir.entryInfoList( QDir::All | QDir::Hidden | QDir::System ); + if ( !filist ) { + QString msg = tr( "Could not read directory\n%1" ).arg( url()->path() ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrListChildren ); + emit finished( op ); + return; + } + + emit start( op ); + + QFileInfoListIterator it( *filist ); + QFileInfo *fi; + QValueList<QUrlInfo> infos; + while ( ( fi = it.current() ) != 0 ) { + ++it; + infos << QUrlInfo( fi->fileName(), convertPermissions(fi), fi->owner(), fi->group(), + fi->size(), fi->lastModified(), fi->lastRead(), fi->isDir(), fi->isFile(), + fi->isSymLink(), fi->isWritable(), fi->isReadable(), fi->isExecutable() ); + } + emit newChildren( infos, op ); + op->setState( StDone ); + emit finished( op ); +} + +/*! + \reimp +*/ + +void QLocalFs::operationMkDir( QNetworkOperation *op ) +{ +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: operationMkDir" ); +#endif + op->setState( StInProgress ); + QString dirname = op->arg( 0 ); + + dir = QDir( url()->path() ); + if ( dir.mkdir( dirname ) ) { + QFileInfo fi( dir, dirname ); + QUrlInfo inf( fi.fileName(), convertPermissions(&fi), fi.owner(), fi.group(), + fi.size(), fi.lastModified(), fi.lastRead(), fi.isDir(), fi.isFile(), + fi.isSymLink(), fi.isWritable(), fi.isReadable(), fi.isExecutable() ); + emit newChild( inf, op ); + op->setState( StDone ); + emit createdDirectory( inf, op ); + emit finished( op ); + } else { + QString msg = tr( "Could not create directory\n%1" ).arg( dirname ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrMkDir ); + emit finished( op ); + } +} + +/*! + \reimp +*/ + +void QLocalFs::operationRemove( QNetworkOperation *op ) +{ +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: operationRemove" ); +#endif + op->setState( StInProgress ); + QString name = QUrl( op->arg( 0 ) ).path(); + bool deleted = FALSE; + + dir = QDir( url()->path() ); + + QFileInfo fi( dir, name ); + if ( fi.isDir() ) { + if ( dir.rmdir( name ) ) + deleted = TRUE; + } + + if ( deleted || dir.remove( name ) ) { + op->setState( StDone ); + emit removed( op ); + emit finished( op ); + } else { + QString msg = tr( "Could not remove file or directory\n%1" ).arg( name ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrRemove ); + emit finished( op ); + } +} + +/*! + \reimp +*/ + +void QLocalFs::operationRename( QNetworkOperation *op ) +{ +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: operationRename" ); +#endif + op->setState( StInProgress ); + QString oldname = op->arg( 0 ); + QString newname = op->arg( 1 ); + + dir = QDir( url()->path() ); + if ( dir.rename( oldname, newname ) ) { + op->setState( StDone ); + emit itemChanged( op ); + emit finished( op ); + } else { + QString msg = tr( "Could not rename\n%1\nto\n%2" ).arg( oldname ).arg( newname ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrRename ); + emit finished( op ); + } +} + +/*! + \reimp +*/ + +void QLocalFs::operationGet( QNetworkOperation *op ) +{ +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: operationGet" ); +#endif + op->setState( StInProgress ); + QString from = QUrl( op->arg( 0 ) ).path(); + + QFile f( from ); + if ( !f.open( IO_ReadOnly ) ) { +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: could not open %s", from.latin1() ); +#endif + QString msg = tr( "Could not open\n%1" ).arg( from ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrGet ); + emit finished( op ); + return; + } + + QByteArray s; + emit dataTransferProgress( 0, f.size(), op ); + if ( f.size() != 0 ) { + int blockSize = calcBlockSize( f.size() ); + if ( (int)f.size() < blockSize ) { + s.resize( f.size() ); + f.readBlock( s.data(), f.size() ); + emit data( s, op ); + emit dataTransferProgress( f.size(), f.size(), op ); +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: got all %d bytes at once", f.size() ); +#endif + } else { + s.resize( blockSize ); + int remaining = f.size(); + QGuardedPtr<QObject> that = this; + while ( that && remaining > 0 ) { + if ( operationInProgress() != op ) + return; + if ( remaining >= blockSize ) { + f.readBlock( s.data(), blockSize ); + emit data( s, op ); + emit dataTransferProgress( f.size() - remaining, f.size(), op ); + remaining -= blockSize; + } else { + s.resize( remaining ); + f.readBlock( s.data(), remaining ); + emit data( s, op ); + emit dataTransferProgress( f.size() - remaining, f.size(), op ); + remaining -= remaining; + } + qApp->processEvents(); + } + if (!that) + return; +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: got all %d bytes step by step", f.size() ); +#endif + emit dataTransferProgress( f.size(), f.size(), op ); + } + } + op->setState( StDone ); + f.close(); + emit finished( op ); +} + +/*! + \reimp +*/ + +void QLocalFs::operationPut( QNetworkOperation *op ) +{ +#ifdef QLOCALFS_DEBUG + qDebug( "QLocalFs: operationPut" ); +#endif + op->setState( StInProgress ); + QString to = QUrl( op->arg( 0 ) ).path(); + + QFile f( to ); + if ( !f.open( IO_WriteOnly ) ) { + QString msg = tr( "Could not write\n%1" ).arg( to ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrPut ); + emit finished( op ); + return; + } + + QByteArray ba( op->rawArg( 1 ) ); + emit dataTransferProgress( 0, ba.size(), op ); + int blockSize = calcBlockSize( ba.size() ); + if ( (int)ba.size() < blockSize ) { + f.writeBlock( ba.data(), ba.size() ); + emit dataTransferProgress( ba.size(), ba.size(), op ); + } else { + int i = 0; + while ( i + blockSize < (int)ba.size() - 1 ) { + if ( operationInProgress() != op ) + return; + f.writeBlock( &ba.data()[ i ], blockSize ); + f.flush(); + emit dataTransferProgress( i + blockSize, ba.size(), op ); + i += blockSize; + QGuardedPtr<QObject> that = this; + qApp->processEvents(); + if (!that) + return; + } + if ( i < (int)ba.size() - 1 ) + f.writeBlock( &ba.data()[ i ], ba.size() - i ); + emit dataTransferProgress( ba.size(), ba.size(), op ); + } + op->setState( StDone ); + f.close(); + emit finished( op ); +} + +/*! + \reimp +*/ + +int QLocalFs::supportedOperations() const +{ + return OpListChildren | OpMkDir | OpRemove | OpRename | OpGet | OpPut; +} + +/*! + \internal +*/ + +int QLocalFs::calcBlockSize( int totalSize ) const +{ + if ( totalSize == 0 ) + return 1024; + int s = totalSize / 100; + // we want a block size between 1KB and 1MB + if ( s < 1024 ) + s = 1024; + if ( s > 1048576 ) + s = 1048576; + return s; +} + +#endif // QT_NO_NETWORKPROTOCOL diff --git a/src/kernel/qlocalfs.h b/src/kernel/qlocalfs.h new file mode 100644 index 0000000..7c2dd0d --- /dev/null +++ b/src/kernel/qlocalfs.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Definition of QLocalFs class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QLOCALFS_H +#define QLOCALFS_H + +#ifndef QT_H +#include "qnetworkprotocol.h" +#include "qdir.h" +#endif // QT_H + +#ifndef QT_NO_NETWORKPROTOCOL + +class Q_EXPORT QLocalFs : public QNetworkProtocol +{ + Q_OBJECT + +public: + QLocalFs(); + virtual int supportedOperations() const; + +protected: + virtual void operationListChildren( QNetworkOperation *op ); + virtual void operationMkDir( QNetworkOperation *op ); + virtual void operationRemove( QNetworkOperation *op ); + virtual void operationRename( QNetworkOperation *op ); + virtual void operationGet( QNetworkOperation *op ); + virtual void operationPut( QNetworkOperation *op ); + +private: + int calcBlockSize( int totalSize ) const; + QDir dir; + +}; + +#endif // QT_NO_NETWORKPROTOCOL + +#endif // QLOCALFS_H diff --git a/src/kernel/qlock.cpp b/src/kernel/qlock.cpp new file mode 100644 index 0000000..56faa1d --- /dev/null +++ b/src/kernel/qlock.cpp @@ -0,0 +1,297 @@ +/**************************************************************************** +** +** Definition of QLock class. This manages interprocess locking +** +** Created : 20000406 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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 "qlock_p.h" + +#ifndef QT_NO_QWS_MULTIPROCESS + +#include <unistd.h> +#include <sys/types.h> +#if defined(Q_OS_MACX) +#define Q_NO_SEMAPHORE +#include <sys/stat.h> +#include <sys/file.h> +#else +#include <sys/sem.h> +#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) \ + || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD) || defined(Q_OS_BSDI) +/* union semun is defined by including <sys/sem.h> */ +#else +/* according to X/OPEN we have to define it ourselves */ +union semun { + int val; /* value for SETVAL */ + struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ + unsigned short *array; /* array for GETALL, SETALL */ +}; +#endif +#endif +#include <sys/ipc.h> +#include <string.h> +#include <errno.h> + +#define MAX_LOCKS 200 // maximum simultaneous read locks + +class QLockData +{ +public: +#ifdef Q_NO_SEMAPHORE + QCString file; +#endif + int id; + int count; + bool owned; +}; + +#endif + +/*! + \class QLock qlock_p.h + \brief The QLock class is a wrapper for a System V shared semaphore. + + \ingroup qws + \ingroup io + + \internal + + It is used by Qt/Embedded for synchronizing access to the graphics + card and shared memory region between processes. +*/ + +/*! + \enum QLock::Type + + \value Read + \value Write +*/ + +/*! + \fn QLock::QLock( const QString &filename, char id, bool create ) + + Creates a lock. \a filename is the file path of the Unix-domain + socket the Qt/Embedded client is using. \a id is the name of the + particular lock to be created on that socket. If \a create is TRUE + the lock is to be created (as the Qt/Embedded server does); if \a + create is FALSE the lock should exist already (as the Qt/Embedded + client expects). +*/ + +QLock::QLock( const QString &filename, char id, bool create ) +{ +#ifndef QT_NO_QWS_MULTIPROCESS + data = new QLockData; + data->count = 0; +#ifdef Q_NO_SEMAPHORE + data->file = QString(filename+id).local8Bit(); + for(int x = 0; x < 2; x++) { + data->id = open(data->file, O_RDWR | (x ? O_CREAT : 0), S_IRWXU); + if(data->id != -1 || !create) { + data->owned = x; + break; + } + } +#else + key_t semkey = ftok(filename, id); + data->id = semget(semkey,0,0); + data->owned = create; + if ( create ) { + semun arg; arg.val = 0; + if ( data->id != -1 ) + semctl(data->id,0,IPC_RMID,arg); + data->id = semget(semkey,1,IPC_CREAT|0600); + arg.val = MAX_LOCKS; + semctl(data->id,0,SETVAL,arg); + } +#endif + if ( data->id == -1 ) { + qWarning( "Cannot %s semaphore %s \'%c\'", + create ? "create" : "get", filename.latin1(), id ); + qDebug("Error %d %s\n",errno,strerror(errno)); + } +#endif +} + +/*! + \fn QLock::~QLock() + + Destroys a lock +*/ + +QLock::~QLock() +{ +#ifndef QT_NO_QWS_MULTIPROCESS + if ( locked() ) + unlock(); +#ifdef Q_NO_SEMAPHORE + if(isValid()) { + close(data->id); + if( data->owned ) + unlink( data->file ); + } +#else + if(data->owned) { + semun arg; arg.val = 0; + semctl( data->id, 0, IPC_RMID, arg ); + } +#endif + delete data; +#endif +} + +/*! + \fn bool QLock::isValid() const + + Returns TRUE if the lock constructor was succesful; returns FALSE if + the lock could not be created or was not available to connect to. +*/ + +bool QLock::isValid() const +{ +#ifndef QT_NO_QWS_MULTIPROCESS + return (data->id != -1); +#else + return TRUE; +#endif +} + +/*! + Locks the semaphore with a lock of type \a t. Locks can either be + \c Read or \c Write. If a lock is \c Read, attempts by other + processes to obtain \c Read locks will succeed, and \c Write + attempts will block until the lock is unlocked. If locked as \c + Write, all attempts to lock by other processes will block until + the lock is unlocked. Locks are stacked: i.e. a given QLock can be + locked multiple times by the same process without blocking, and + will only be unlocked after a corresponding number of unlock() + calls. +*/ + +void QLock::lock( Type t ) +{ +#ifndef QT_NO_QWS_MULTIPROCESS + if ( !data->count ) { +#ifdef Q_NO_SEMAPHORE + int op = LOCK_SH; + if(t == Write) + op = LOCK_EX; + for( int rv=1; rv; ) { + rv = flock(data->id, op); + if (rv == -1 && errno != EINTR) + qDebug("Semop lock failure %s",strerror(errno)); + } +#else + sembuf sops; + sops.sem_num = 0; + sops.sem_flg = SEM_UNDO; + + if ( t == Write ) { + sops.sem_op = -MAX_LOCKS; + type = Write; + } else { + sops.sem_op = -1; + type = Read; + } + + int rv; + do { + rv = semop(data->id,&sops,1); + if (rv == -1 && errno != EINTR) + qDebug("Semop lock failure %s",strerror(errno)); + } while ( rv == -1 && errno == EINTR ); +#endif + } + data->count++; +#endif +} + +/*! + \fn void QLock::unlock() + + Unlocks the semaphore. If other processes were blocking waiting to + lock() the semaphore, one of them will wake up and succeed in + lock()ing. +*/ + +void QLock::unlock() +{ +#ifndef QT_NO_QWS_MULTIPROCESS + if( data->count ) { + data->count--; + if( !data->count ) { +#ifdef Q_NO_SEMAPHORE + for( int rv=1; rv; ) { + rv = flock(data->id, LOCK_UN); + if (rv == -1 && errno != EINTR) + qDebug("Semop lock failure %s",strerror(errno)); + } +#else + sembuf sops; + sops.sem_num = 0; + sops.sem_op = 1; + sops.sem_flg = SEM_UNDO; + if ( type == Write ) + sops.sem_op = MAX_LOCKS; + + int rv; + do { + rv = semop(data->id,&sops,1); + if (rv == -1 && errno != EINTR) + qDebug("Semop unlock failure %s",strerror(errno)); + } while ( rv == -1 && errno == EINTR ); +#endif + } + } else { + qDebug("Unlock without corresponding lock"); + } +#endif +} + +/*! + \fn bool QLock::locked() const + + Returns TRUE if the lock is currently held by the current process; + otherwise returns FALSE. +*/ + +bool QLock::locked() const +{ +#ifndef QT_NO_QWS_MULTIPROCESS + return (data->count > 0); +#else + return FALSE; +#endif +} diff --git a/src/kernel/qlock_p.h b/src/kernel/qlock_p.h new file mode 100644 index 0000000..de07ba4 --- /dev/null +++ b/src/kernel/qlock_p.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Definition of QLock class. This manages interprocess locking +** +** Created : 20000406 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QLOCK_P_H +#define QLOCK_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. This header file may +// change from version to version without notice, or even be +// removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qstring.h" +#endif // QT_H + +class QLockData; + +class QLock +{ +public: + QLock( const QString &filename, char id, bool create = FALSE ); + ~QLock(); + + enum Type { Read, Write }; + + bool isValid() const; + void lock( Type type ); + void unlock(); + bool locked() const; + +private: + Type type; + QLockData *data; +}; + + +// Nice class for ensuring the lock is released. +// Just create one on the stack and the lock is automatically released +// when QLockHolder is destructed. +class QLockHolder +{ +public: + QLockHolder( QLock *l, QLock::Type type ) : qlock(l) { + qlock->lock( type ); + } + ~QLockHolder() { if ( locked() ) qlock->unlock(); } + + void lock( QLock::Type type ) { qlock->lock( type ); } + void unlock() { qlock->unlock(); } + bool locked() const { return qlock->locked(); } + +private: + QLock *qlock; +}; + +#endif + diff --git a/src/kernel/qmetaobject.cpp b/src/kernel/qmetaobject.cpp new file mode 100644 index 0000000..9a86686 --- /dev/null +++ b/src/kernel/qmetaobject.cpp @@ -0,0 +1,1251 @@ +/**************************************************************************** +** +** Implementation of QMetaObject class +** +** Created : 930419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qmetaobject.h" +#include "qasciidict.h" + +#ifdef QT_THREAD_SUPPORT +#include <private/qmutexpool_p.h> +#endif // QT_THREAD_SUPPORT + +/*! + \class QMetaData qmetaobject.h + \reentrant + + \brief The QMetaData class provides information about a member function that is known to the meta object system. + + \internal + + The struct consists of three members, \e name, \e method and \e access: + + \code + const char *name; // - member name + const QUMethod* method; // - detailed method description + enum Access { Private, Protected, Public }; + Access access; // - access permission + \endcode + */ + +/*! + \class QClassInfo qmetaobject.h + + \brief The QClassInfo class provides a struct that stores some basic information about a single class. + + \internal + + The class information is a simple \e name - \e value pair: + + \code + const char* name; + const char* value; + \endcode + + */ + + +/*! + \class QMetaObject qmetaobject.h + \brief The QMetaObject class contains meta information about Qt objects. + + \ingroup objectmodel + + The Meta Object System in Qt is responsible for the signals and + slots inter-object communication mechanism, runtime type + information and the property system. All meta information in Qt is + kept in a single instance of QMetaObject per class. + + This class is not normally required for application programming. + But if you write meta applications, such as scripting engines or + GUI builders, you might find these functions useful: + \list + \i className() to get the name of a class. + \i superClassName() to get the name of the superclass. + \i inherits(), the function called by QObject::inherits(). + \i superClass() to access the superclass's meta object. + \i numSlots(), numSignals(), slotNames(), and signalNames() to get + information about a class's signals and slots. + \i property() and propertyNames() to obtain information about a + class's properties. + \endlist + + Classes may have a list of name-value pairs of class information. + The number of pairs is returned by numClassInfo(), and values are + returned by classInfo(). + + \sa \link moc.html moc (Meta Object Compiler)\endlink + +*/ + + +/***************************************************************************** + The private object. + *****************************************************************************/ + +// extra flags from moc.y +enum Flags { + Invalid = 0x00000000, + Readable = 0x00000001, + Writable = 0x00000002, + EnumOrSet = 0x00000004, + UnresolvedEnum = 0x00000008, + StdSet = 0x00000100, + Override = 0x00000200, + NotDesignable = 0x00001000, + DesignableOverride = 0x00002000, + NotScriptable = 0x00004000, + ScriptableOverride = 0x00008000, + NotStored = 0x00010000, + StoredOverride = 0x00020000 +}; + +static QAsciiDict<void> *qt_metaobjects = 0; +static int qt_metaobjects_count = 0; + +class QMetaObjectPrivate +{ +public: + QMetaObjectPrivate() : +#ifndef QT_NO_PROPERTIES + enumData(0), numEnumData(0), + propData(0),numPropData(0), + qt_static_property(0), +#endif + classInfo(0), numClassInfo(0) {} +#ifndef QT_NO_PROPERTIES + const QMetaEnum *enumData; + int numEnumData; + const QMetaProperty *propData; + int numPropData; + bool (*qt_static_property)(QObject*, int, int, QVariant*); +#endif + const QClassInfo *classInfo; + int numClassInfo; +}; + + +/***************************************************************************** + Internal dictionary for fast access to class members + *****************************************************************************/ + +#if defined(Q_CANNOT_DELETE_CONSTANT) +typedef QMetaData QConstMetaData; +#else +typedef const QMetaData QConstMetaData; +#endif + +class Q_EXPORT QMemberDict : public QAsciiDict<QConstMetaData> +{ +public: + QMemberDict( int size = 17, bool cs = TRUE, bool ck = TRUE ) : + QAsciiDict<QConstMetaData>(size,cs,ck) {} + QMemberDict( const QMemberDict &dict ) : QAsciiDict<QConstMetaData>(dict) {} + ~QMemberDict() { clear(); } + QMemberDict &operator=(const QMemberDict &dict) + { return (QMemberDict&)QAsciiDict<QConstMetaData>::operator=(dict); } +}; + + +/* + Calculate optimal dictionary size for n entries using prime numbers, + and assuming there are no more than 40 entries. +*/ + +static int optDictSize( int n ) +{ + if ( n < 6 ) + n = 5; + else if ( n < 10 ) + n = 11; + else if ( n < 14 ) + n = 17; + else + n = 23; + return n; +} + + +/***************************************************************************** + QMetaObject member functions + *****************************************************************************/ + +/*!\internal + */ +QMetaObject::QMetaObject( const char *const class_name, QMetaObject *super_class, + const QMetaData *const slot_data, int n_slots, + const QMetaData *const signal_data, int n_signals, +#ifndef QT_NO_PROPERTIES + const QMetaProperty *const prop_data, int n_props, + const QMetaEnum *const enum_data, int n_enums, +#endif + const QClassInfo *const class_info, int n_info ) +{ + classname = class_name; // set meta data + superclass = super_class; + superclassname = superclass ? superclass->className() : 0; + slotDict = init( slotData = slot_data, n_slots ); + signalDict = init( signalData = signal_data, n_signals ); + + d = new QMetaObjectPrivate; + reserved = 0; + +#ifndef QT_NO_PROPERTIES + d->propData = prop_data; + d->numPropData = n_props; + d->enumData = enum_data; + d->numEnumData = n_enums; +#endif + d->classInfo = class_info; + d->numClassInfo = n_info; + + signaloffset = superclass ? ( superclass->signalOffset() + superclass->numSignals() ) : 0; + slotoffset = superclass ? ( superclass->slotOffset() + superclass->numSlots() ) : 0; +#ifndef QT_NO_PROPERTIES + propertyoffset = superclass ? ( superclass->propertyOffset() + superclass->numProperties() ) : 0; +#endif +} + +#ifndef QT_NO_PROPERTIES +/*!\internal + */ +QMetaObject::QMetaObject( const char *const class_name, QMetaObject *super_class, + const QMetaData *const slot_data, int n_slots, + const QMetaData *const signal_data, int n_signals, + const QMetaProperty *const prop_data, int n_props, + const QMetaEnum *const enum_data, int n_enums, + bool (*qt_static_property)(QObject*, int, int, QVariant*), + const QClassInfo *const class_info, int n_info ) +{ + classname = class_name; // set meta data + superclass = super_class; + superclassname = superclass ? superclass->className() : 0; + slotDict = init( slotData = slot_data, n_slots ); + signalDict = init( signalData = signal_data, n_signals ); + + d = new QMetaObjectPrivate; + reserved = 0; + + d->propData = prop_data; + d->numPropData = n_props; + d->enumData = enum_data; + d->numEnumData = n_enums; + d->qt_static_property = qt_static_property; + d->classInfo = class_info; + d->numClassInfo = n_info; + + signaloffset = superclass ? ( superclass->signalOffset() + superclass->numSignals() ) : 0; + slotoffset = superclass ? ( superclass->slotOffset() + superclass->numSlots() ) : 0; + propertyoffset = superclass ? ( superclass->propertyOffset() + superclass->numProperties() ) : 0; +} +#endif + +/*!\internal + */ +QMetaObject::~QMetaObject() +{ + delete slotDict; // delete dicts + delete signalDict; + delete d; +#ifdef QT_THREAD_SUPPORT + QMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + if ( qt_metaobjects ) { + qt_metaobjects->remove( classname ); + if ( qt_metaobjects->isEmpty() ) { + delete qt_metaobjects; + qt_metaobjects = 0; + } + } + + // delete reserved; // Unused void* +} + + +/*! + \fn const char *QMetaObject::className() const + + Returns the class name. + + \sa QObject::className(), superClassName() +*/ + +/*! + \fn const char *QMetaObject::superClassName() const + + Returns the class name of the superclass or 0 if there is no + superclass in the QObject hierachy. + + \sa className() +*/ + +/*! + \fn QMetaObject *QMetaObject::superClass() const + + Returns the meta object of the super class or 0 if there is no + such object. +*/ + +/*! + Returns the number of slots for this class. + + If \a super is TRUE, inherited slots are included. + + \sa slotNames() +*/ +int QMetaObject::numSlots( bool super ) const // number of slots +{ + int n = slotDict ? slotDict->count() : 0; + if ( !super || !superclass ) + return n; + return n + superclass->numSlots( super ); +} + +/*! + Returns the number of signals for this class. + + If \a super is TRUE, inherited signals are included. + + \sa signalNames() +*/ +int QMetaObject::numSignals( bool super ) const // number of signals +{ + int n = signalDict ? signalDict->count() : 0; + if ( !super || !superclass ) + return n; + return n + superclass->numSignals( super ); +} + + +/*! \internal + + Returns the meta data of the slot with the name \a n or 0 if no + such slot exists. + + If \a super is TRUE, inherited slots are included. + */ +const QMetaData* QMetaObject::slot( int index, bool super ) const +{ + int idx = index - ( super ? slotOffset() : 0 ); + if ( slotDict && idx >= 0 && idx < (int) slotDict->count() ) { + return slotData + idx; + } + if ( !super || !superclass ) + return 0; + return superclass->slot( index, super ); +} + +/*! \internal + + Returns the meta data of the signal with the name \a n or 0 if no + such signal exists. + + If \a super is TRUE, inherited signals are included. + */ +const QMetaData* QMetaObject::signal( int index, bool super ) const +{ + int idx = index - ( super ? signalOffset() : 0 ); + if ( signalDict && idx >= 0 && idx < (int) signalDict->count() ) { + return signalData + idx; + } + if ( !super || !superclass ) + return 0; + return superclass->signal( index, super ); +} + + +/*! + \fn int QMetaObject::signalOffset() const + + \internal + + Returns the signal offset for this metaobject. + +*/ + +/*! + \fn int QMetaObject::propertyOffset() const + + \internal + + Returns the property offset for this metaobject. + +*/ + +/*! \internal + Returns the index of the signal with name \n or -1 if no such signal exists. + + If \a super is TRUE, inherited signals are included. +*/ +int QMetaObject::findSignal( const char* n, bool super ) const +{ + const QMetaObject *mo = this; + int offset = -1; + + do { + const QMetaData *md = mo->signalDict ? mo->signalDict->find( n ) : 0; + if ( md ) { +#if defined(QT_CHECK_RANGE) + if ( offset != -1 ) { + qWarning( "QMetaObject::findSignal:%s: Conflict with %s::%s", + className(), mo->className(), n ); + return offset; + } +#endif + offset = mo->signalOffset() + ( md - mo->signalData ); +#if !defined(QT_CHECK_RANGE) + return offset; +#endif + } + } while ( super && (mo = mo->superclass) ); + + return offset; +} + +/*! + \fn int QMetaObject::slotOffset() const + + \internal + + Returns the slot offset for this metaobject. + +*/ + +/*! \internal + Returns the index of the slot with name \n or -1 if no such slot exists. + + If \a super is TRUE, inherited slots are included. + */ +int QMetaObject::findSlot( const char* n, bool super ) const +{ + const QMetaData *md = slotDict ? slotDict->find( n ) : 0; + if ( md ) + return slotOffset() + ( md - slotData ); + if ( !super || !superclass) + return -1; + return superclass->findSlot( n, super ); +} + +/*!\internal + */ +QMetaObject *QMetaObject::new_metaobject( const char *classname, + QMetaObject *superclassobject, + const QMetaData * const slot_data, int n_slots, + const QMetaData * const signal_data, int n_signals, +#ifndef QT_NO_PROPERTIES + const QMetaProperty * const prop_data, int n_props, + const QMetaEnum * const enum_data, int n_enums, +#endif + const QClassInfo * const class_info, int n_info ) +{ + return new QMetaObject( classname, superclassobject, slot_data, n_slots, + signal_data, n_signals, +#ifndef QT_NO_PROPERTIES + prop_data, n_props, + enum_data, n_enums, +#endif + class_info, n_info ); +} + +#ifndef QT_NO_PROPERTIES +/*!\internal + */ +QMetaObject *QMetaObject::new_metaobject( const char *classname, + QMetaObject *superclassobject, + const QMetaData * const slot_data, int n_slots, + const QMetaData * const signal_data, int n_signals, + const QMetaProperty * const prop_data, int n_props, + const QMetaEnum * const enum_data, int n_enums, + bool (*qt_static_property)(QObject*, int, int, QVariant*), + const QClassInfo * const class_info, int n_info ) +{ + return new QMetaObject( classname, superclassobject, slot_data, n_slots, + signal_data, n_signals, + prop_data, n_props, + enum_data, n_enums, + qt_static_property, + class_info, n_info ); +} +#endif + +/*!\internal + */ +QMemberDict *QMetaObject::init( const QMetaData * data, int n ) +{ + if ( n == 0 ) // nothing, then make no dict + return 0; + QMemberDict *dict = new QMemberDict( optDictSize(n), TRUE, FALSE ); + Q_CHECK_PTR( dict ); + while ( n-- ) { // put all members into dict + dict->insert( data->name, data ); + data++; + } + return dict; +} + +/*! + Returns the number of items of class information available for + this class. + + If \a super is TRUE, inherited class information is included. +*/ +int QMetaObject::numClassInfo( bool super ) const +{ + return d->numClassInfo + ((super && superclass)?superclass->numClassInfo(super):0); +} + +/*! + Returns the class information with index \a index or 0 if no such + information exists. + + If \a super is TRUE, inherited class information is included. +*/ +const QClassInfo* QMetaObject::classInfo( int index, bool super ) const +{ + if ( index < 0 ) + return 0; + if ( index < d->numClassInfo ) + return &(d->classInfo[ index ]); + if ( !super || !superclass ) + return 0; + return superclass->classInfo( index - d->numClassInfo, super ); +} + +/*! + \overload + Returns the class information with name \a name or 0 if no such + information exists. + + If \a super is TRUE, inherited class information is included. +*/ +const char* QMetaObject::classInfo( const char* name, bool super ) const +{ + for( int i = 0; i < d->numClassInfo; ++i ) { + if ( qstrcmp( d->classInfo[i].name, name ) == 0 ) + return d->classInfo[i].value; + } + if ( !super || !superclass ) + return 0; + return superclass->classInfo( name, super ); +} + +#ifndef QT_NO_PROPERTIES + +/*! + Returns the number of properties for this class. + + If \a super is TRUE, inherited properties are included. + + \sa propertyNames() + */ +int QMetaObject::numProperties( bool super ) const // number of signals +{ + int n = d->numPropData; + if ( !super || !superclass ) + return n; + return n + superclass->numProperties( super ); +} + +/*! + Returns the property meta data for the property at index \a index + or 0 if no such property exists. + + If \a super is TRUE, inherited properties are included. + + \sa propertyNames() + */ +const QMetaProperty* QMetaObject::property( int index, bool super ) const +{ + int idx = index - ( super ? propertyOffset() : 0 ); + if ( d->propData && idx >= 0 && idx < (int)d->numPropData ) + return d->propData + idx; + if ( !super || !superclass ) + return 0; + return superclass->property( index, super ); +} + + +/*! + Returns the index for the property with name \a name or -1 if no + such property exists. + + If \a super is TRUE, inherited properties are included. + + \sa property(), propertyNames() +*/ + +int QMetaObject::findProperty( const char *name, bool super ) const +{ + for( int i = 0; i < d->numPropData; ++i ) { + if ( d->propData[i].isValid() && qstrcmp( d->propData[i].name(), name ) == 0 ) { + return ( super ? propertyOffset() : 0 ) + i; + } + } + if ( !super || !superclass ) + return -1; + return superclass->findProperty( name, super ); +} + +/*! \internal + + Returns the index for the property \a prop + or -1 if the property can not be found. + + If \a super is TRUE, inherited properties are included. + + \sa property(), propertyNames() +*/ + +int QMetaObject::indexOfProperty( const QMetaProperty* prop, bool super ) const +{ + if ( *prop->meta == this ) + return ( super ? propertyOffset() : 0 ) + ( prop - d->propData); + if ( !super || !superclass ) + return -1; + return superclass->indexOfProperty( prop, super ); +} + +/*!\internal + + Returns the parent property of property \a p or 0, if the property + cannot be resolved. + + \a p has to be contained in this meta object +*/ + +const QMetaProperty* QMetaObject::resolveProperty( const QMetaProperty* p ) const +{ + if ( !superclass ) + return 0; + return superclass->property( superclass->findProperty( p->n, TRUE ), TRUE ); +} + +/*!\internal + + \overload + + The version of resolveProperty that is used by moc generated code +*/ + +int QMetaObject::resolveProperty( int index ) const +{ + if ( !superclass ) + return -1; + const QMetaProperty* p = d->propData + ( index - propertyOffset() ); + return superclass->findProperty( p->n, TRUE ); +} + + +/*! + Returns a list with the names of all this class's properties. + + If \a super is TRUE, inherited properties are included. + + \sa property() +*/ +QStrList QMetaObject::propertyNames( bool super ) const +{ + QStrList l( FALSE ); + + if ( superclass && super ) { + QStrList sl = superclass->propertyNames( super ); + for ( QStrListIterator slit( sl ); slit.current(); ++slit ) + l.append( slit.current() ); + } + + for( int i = 0; i < d->numPropData; ++i ) { + if ( d->propData[i].isValid() ) + l.append( d->propData[i].name() ); + } + + return l; +} + +/*! + Returns a list with the names of all this class's signals. + + If \a super is TRUE, inherited signals are included. +*/ +QStrList QMetaObject::signalNames( bool super ) const +{ + QStrList l( FALSE ); + int n = numSignals( super ); + for( int i = 0; i < n; ++i ) { + l.append( signal(i, super)->name ); + } + return l; +} + +/*! + Returns a list with the names of all this class's slots. + + If \a super is TRUE, inherited slots are included. + + \sa numSlots() +*/ +QStrList QMetaObject::slotNames( bool super ) const +{ + QStrList l( FALSE ); + int n = numSlots( super ); + for( int i = 0; i < n; ++i ) + l.append( slot( i, super)->name ); + return l; +} + +/*!\internal + + */ + +int QMetaObject::numEnumerators( bool super ) const +{ + int n = 0; + if ( superclass && super ) + n += superclass->numEnumerators( super ); + return n + d->numEnumData; +} + +/*!\internal + + */ +QStrList QMetaObject::enumeratorNames( bool super ) const +{ + QStrList l( FALSE ); + + if ( superclass && super ) { + QStrList sl = superclass->enumeratorNames( super ); + for ( QStrListIterator slit( sl ); slit.current(); ++slit ) + l.append( slit.current() ); + } + + for( int i = 0; i < d->numEnumData; ++i ) { + if ( d->enumData[i].items ) + l.append( d->enumData[i].name ); + } + + return l; +} + +/*!\internal + */ +const QMetaEnum* QMetaObject::enumerator( const char* name, bool super ) const +{ + for( int i = 0; i < d->numEnumData; ++i ) + if ( qstrcmp( d->enumData[i].name, name ) == 0 ) + return &(d->enumData[i]); + if ( !super || !superclass ) + return 0; + return superclass->enumerator( name, super ); +} + +#endif // QT_NO_PROPERTIES + + +/*! + Returns TRUE if this class inherits \a clname within the meta + object inheritance chain; otherwise returns FALSE. + + (A class is considered to inherit itself.) +*/ +bool QMetaObject::inherits( const char* clname ) const +{ + const QMetaObject *meta = this; + while ( meta ) { + if ( qstrcmp(clname, meta->className()) == 0 ) + return TRUE; + meta = meta->superclass; + } + return FALSE; +} + +/*! \internal */ + +QMetaObject *QMetaObject::metaObject( const char *class_name ) +{ + if ( !qt_metaobjects ) + return 0; +#ifdef QT_THREAD_SUPPORT + QMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + QtStaticMetaObjectFunction func = (QtStaticMetaObjectFunction)qt_metaobjects->find( class_name ); + if ( func ) + return func(); + return 0; +} + +/*! \internal */ +bool QMetaObject::hasMetaObject( const char *class_name ) +{ + if ( !qt_metaobjects ) + return FALSE; +#ifdef QT_THREAD_SUPPORT + QMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + return !!qt_metaobjects->find( class_name ); +} + +#ifndef QT_NO_PROPERTIES +/*! \internal + +### this functions will go away. It exists purely for the sake of meta +### object code generated with Qt 3.1.0 +*/ +bool QMetaObject::qt_static_property( QObject* o, int id, int f, QVariant* v) +{ + if ( d->qt_static_property ) + return d->qt_static_property( o, id, f, v ); + else if ( o ) // compatibility + return o->qt_property( id, f, v ); + else if ( superclass ) + return superclass->qt_static_property( o, id, f, v ); + switch ( f ) { + case 3: case 4: case 5: + return TRUE; + default: + return FALSE; + } +} + + +/*! + \class QMetaProperty qmetaobject.h + + \brief The QMetaProperty class stores meta data about a property. + + \ingroup objectmodel + + Property meta data includes type(), name(), and whether a property + is writable(), designable() and stored(). + + The functions isSetType(), isEnumType() and enumKeys() provide + further information about a property's type. The conversion + functions keyToValue(), valueToKey(), keysToValue() and + valueToKeys() allow conversion between the integer representation + of an enumeration or set value and its literal representation. + + Actual property values are set and received through QObject's set + and get functions. See QObject::setProperty() and + QObject::property() for details. + + You receive meta property data through an object's meta object. + See QMetaObject::property() and QMetaObject::propertyNames() for + details. +*/ + +/*! + Returns the possible enumeration keys if this property is an + enumeration type (or a set type). + + \sa isEnumType() +*/ +QStrList QMetaProperty::enumKeys() const +{ + QStrList l( FALSE ); + const QMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return l; + if ( ed != 0 ) { + for( uint i = 0; i < ed->count; ++i ) { + uint j = 0; + while ( j < i && + ed->items[j].value != ed->items[i].value ) + ++j; + if ( i == j ) + l.append( ed->items[i].key ); + } + } + return l; +} + +/*! + Converts the enumeration key \a key to its integer value. + + For set types, use keysToValue(). + + \sa valueToKey(), isSetType(), keysToValue() +*/ +int QMetaProperty::keyToValue( const char* key ) const +{ + const QMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return -1; + for ( uint i = 0; i < ed->count; ++i ) { + if ( !qstrcmp( key, ed->items[i].key) ) + return ed->items[i].value; + } + return -1; +} + +/*! + Converts the enumeration value \a value to its literal key. + + For set types, use valueToKeys(). + + \sa valueToKey(), isSetType(), valueToKeys() +*/ +const char* QMetaProperty::valueToKey( int value ) const +{ + const QMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return 0; + for ( uint i = 0; i < ed->count; ++i ) { + if ( value == ed->items[i].value ) + return ed->items[i].key ; + } + return 0; +} + +/*! + Converts the list of keys \a keys to their combined (OR-ed) + integer value. + + \sa isSetType(), valueToKey(), keysToValue() +*/ +int QMetaProperty::keysToValue( const QStrList& keys ) const +{ + const QMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return -1; + int value = 0; + for ( QStrListIterator it( keys ); it.current(); ++it ) { + uint i; + for( i = ed->count; i > 0; --i ) { + if ( !qstrcmp( it.current(), ed->items[i-1].key) ) { + value |= ed->items[i-1].value; + break; + } + } + if ( i == 0 ) + value |= -1; + } + return value; +} + +/*! + Converts the set value \a value to a list of keys. + + \sa isSetType(), valueToKey(), valueToKeys() +*/ +QStrList QMetaProperty::valueToKeys( int value ) const +{ + QStrList keys; + const QMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return keys; + + int v = value; + for( uint i = ed->count; i > 0; --i ) { + int k = ed->items[i-1].value; + if ( ( k != 0 && (v & k) == k ) || ( k == value) ) { + v = v & ~k; + keys.append( ed->items[i-1].key ); + } + } + return keys; +} + +bool QMetaProperty::writable() const +{ + if ( !testFlags( Override ) || testFlags( Writable ) ) + return testFlags( Writable ); + const QMetaObject* mo = (*meta); + const QMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->writable() : FALSE; +} + +/*!\internal + */ +bool QMetaProperty::stdSet() const +{ + if ( !testFlags( Override ) || testFlags( Writable ) ) + return testFlags( StdSet ); + const QMetaObject* mo = (*meta); + const QMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->stdSet() : FALSE; +} + +/*!\internal + */ +int QMetaProperty::id() const +{ + return _id < 0 ? (*meta)->indexOfProperty( this, TRUE ) : _id; +} + +/*! \internal +*/ +void QMetaProperty::clear() +{ + t = n = 0; + meta = 0; + enumData = 0; + _id = -1; + flags = 0; +} + +bool QMetaProperty::isValid() const +{ + if ( testFlags( UnresolvedEnum ) ) { + if ( !enumData && (!meta || !(*meta)->enumerator( t, TRUE ) ) ) + return FALSE; + } + if ( !testFlags( Override ) || testFlags( Readable ) ) + return testFlags( Readable ); + const QMetaObject* mo = (*meta); + const QMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->isValid() : FALSE; +} + +bool QMetaProperty::isSetType() const +{ + const QMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + return ( ed != 0 && ed->set ); +} + +bool QMetaProperty::isEnumType() const +{ + return testFlags( EnumOrSet ); +} + + + +/*! + \fn const char* QMetaProperty::type() const + + Returns the type of the property. +*/ + +/*! + \fn const char* QMetaProperty::name() const + + Returns the name of the property. +*/ + +/*! + \fn bool QMetaProperty::writable() const + + Returns TRUE if the property is writable; otherwise returns FALSE. + +*/ + +/*! \fn bool QMetaProperty::isValid() const + + \internal + + Returns whether the property is valid. +*/ + +/*! + \fn bool QMetaProperty::isEnumType() const + + Returns TRUE if the property's type is an enumeration value; + otherwise returns FALSE. + + \sa isSetType(), enumKeys() +*/ + +/*! + \fn bool QMetaProperty::isSetType() const + + Returns TRUE if the property's type is an enumeration value that + is used as set, i.e. if the enumeration values can be OR-ed + together; otherwise returns FALSE. A set type is implicitly also + an enum type. + + \sa isEnumType(), enumKeys() +*/ + + +/*! Returns TRUE if the property is designable for object \a o; + otherwise returns FALSE. + + If no object \a o is given, the function returns a static + approximation. + */ +bool QMetaProperty::designable( QObject* o ) const +{ + if ( !isValid() || !writable() ) + return FALSE; + if ( o ) { + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + return idx >= 0 && o->qt_property( idx, 3, 0 ); + } + if ( testFlags( DesignableOverride ) ) { + const QMetaObject* mo = (*meta); + const QMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->designable() : FALSE; + } + return !testFlags( NotDesignable ); +} + +/*! + Returns TRUE if the property is scriptable for object \a o; + otherwise returns FALSE. + + If no object \a o is given, the function returns a static + approximation. + */ +bool QMetaProperty::scriptable( QObject* o ) const +{ + if ( o ) { + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + return idx >= 0 && o->qt_property( idx, 4, 0 ); + } + if ( testFlags( ScriptableOverride ) ) { + const QMetaObject* mo = (*meta); + const QMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->scriptable() : FALSE; + } + return !testFlags( NotScriptable ); +} + +/*! + Returns TRUE if the property shall be stored for object \a o; + otherwise returns FALSE. + + If no object \a o is given, the function returns a static + approximation. + */ +bool QMetaProperty::stored( QObject* o ) const +{ + if ( !isValid() || !writable() ) + return FALSE; + if ( o ) { + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + return idx >= 0 && o->qt_property( idx, 5, 0 ); + } + if ( testFlags( StoredOverride ) ) { + const QMetaObject* mo = (*meta); + const QMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->stored() : FALSE; + } + return !testFlags( NotStored ); +} + + +/*! + Tries to reset the property for object \a o with a reset method. + On success, returns TRUE; otherwise returns FALSE. + + Reset methods are optional, usually only a few properties support + them. +*/ +bool QMetaProperty::reset( QObject* o ) const +{ + if ( !o ) + return FALSE; + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + if ( idx < 0 ) + return 0; + return o->qt_property( idx, 2, 0 ); +} + + +/*! \enum QMetaProperty::Flags + + \internal +*/ + +#endif // QT_NO_PROPERTIES + +/* + * QMetaObjectCleanUp is used as static global object in the moc-generated cpp + * files and deletes the QMetaObject provided with setMetaObject. It sets the + * QObject reference to the metaObj to NULL when it is destroyed. + */ +QMetaObjectCleanUp::QMetaObjectCleanUp( const char *mo_name, QtStaticMetaObjectFunction func ) + : metaObject( 0 ) +{ +#ifdef QT_THREAD_SUPPORT + QMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + if ( !qt_metaobjects ) + qt_metaobjects = new QAsciiDict<void>( 257 ); + qt_metaobjects->insert( mo_name, (void*)func ); + + qt_metaobjects_count++; +} + +QMetaObjectCleanUp::QMetaObjectCleanUp() + : metaObject( 0 ) +{ +} + +/*! \fn bool QMetaProperty::testFlags( uint f ) const + \internal +*/ + +QMetaObjectCleanUp::~QMetaObjectCleanUp() +{ +#ifdef QT_THREAD_SUPPORT + QMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + if ( !--qt_metaobjects_count ) { + delete qt_metaobjects; + qt_metaobjects = 0; + } + if ( metaObject ) { + delete *metaObject; + *metaObject = 0; + metaObject = 0; + } +} + +void QMetaObjectCleanUp::setMetaObject( QMetaObject *&mo ) +{ +#if defined(QT_CHECK_RANGE) + if ( metaObject ) + qWarning( "QMetaObjectCleanUp::setMetaObject: Double use of QMetaObjectCleanUp!" ); +#endif + metaObject = &mo; +} diff --git a/src/kernel/qmetaobject.h b/src/kernel/qmetaobject.h new file mode 100644 index 0000000..9813231 --- /dev/null +++ b/src/kernel/qmetaobject.h @@ -0,0 +1,286 @@ +/**************************************************************************** +** +** Definition of QMetaObject class +** +** Created : 930419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QMETAOBJECT_H +#define QMETAOBJECT_H + +#ifndef QT_H +#include "qconnection.h" +#include "qstrlist.h" +#endif // QT_H + +#ifndef Q_MOC_OUTPUT_REVISION +#define Q_MOC_OUTPUT_REVISION 26 +#endif + +class QObject; +struct QUMethod; +class QMetaObjectPrivate; + +struct QMetaData // - member function meta data +{ // for signal and slots + const char *name; // - member name + const QUMethod* method; // - detailed method description + enum Access { Private, Protected, Public }; + Access access; // - access permission +}; + +#ifndef QT_NO_PROPERTIES +struct QMetaEnum // enumerator meta data +{ // for properties + const char *name; // - enumerator name + uint count; // - number of values + struct Item // - a name/value pair + { + const char *key; + int value; + }; + const Item *items; // - the name/value pairs + bool set; // whether enum has to be treated as a set +}; +#endif + +#ifndef QT_NO_PROPERTIES + +class Q_EXPORT QMetaProperty // property meta data +{ +public: + const char* type() const { return t; } // type of the property + const char* name() const { return n; } // name of the property + + bool writable() const; + bool isValid() const; + + bool isSetType() const; + bool isEnumType() const; + QStrList enumKeys() const; // enumeration names + + int keyToValue( const char* key ) const; // enum and set conversion functions + const char* valueToKey( int value ) const; + int keysToValue( const QStrList& keys ) const; + QStrList valueToKeys( int value ) const; + + bool designable( QObject* = 0 ) const; + bool scriptable( QObject* = 0 ) const; + bool stored( QObject* = 0 ) const; + + bool reset( QObject* ) const; + + const char* t; // internal + const char* n; // internal + + enum Flags { + Invalid = 0x00000000, + Readable = 0x00000001, + Writable = 0x00000002, + EnumOrSet = 0x00000004, + UnresolvedEnum = 0x00000008, + StdSet = 0x00000100, + Override = 0x00000200 + }; + + uint flags; // internal + bool testFlags( uint f ) const; // internal + bool stdSet() const; // internal + int id() const; // internal + + QMetaObject** meta; // internal + + const QMetaEnum* enumData; // internal + int _id; // internal + void clear(); // internal +}; + +inline bool QMetaProperty::testFlags( uint f ) const +{ return (flags & (uint)f) != (uint)0; } + +#endif // QT_NO_PROPERTIES + +struct QClassInfo // class info meta data +{ + const char* name; // - name of the info + const char* value; // - value of the info +}; + +class Q_EXPORT QMetaObject // meta object class +{ +public: + QMetaObject( const char * const class_name, QMetaObject *superclass, + const QMetaData * const slot_data, int n_slots, + const QMetaData * const signal_data, int n_signals, +#ifndef QT_NO_PROPERTIES + const QMetaProperty *const prop_data, int n_props, + const QMetaEnum *const enum_data, int n_enums, +#endif + const QClassInfo *const class_info, int n_info ); + +#ifndef QT_NO_PROPERTIES + QMetaObject( const char * const class_name, QMetaObject *superclass, + const QMetaData * const slot_data, int n_slots, + const QMetaData * const signal_data, int n_signals, + const QMetaProperty *const prop_data, int n_props, + const QMetaEnum *const enum_data, int n_enums, + bool (*qt_static_property)(QObject*, int, int, QVariant*), + const QClassInfo *const class_info, int n_info ); +#endif + + + virtual ~QMetaObject(); + + const char *className() const { return classname; } + const char *superClassName() const { return superclassname; } + + QMetaObject *superClass() const { return superclass; } + + bool inherits( const char* clname ) const; + + int numSlots( bool super = FALSE ) const; + int numSignals( bool super = FALSE ) const; + + int findSlot( const char *, bool super = FALSE ) const; + int findSignal( const char *, bool super = FALSE ) const; + + const QMetaData *slot( int index, bool super = FALSE ) const; + const QMetaData *signal( int index, bool super = FALSE ) const; + + QStrList slotNames( bool super = FALSE ) const; + QStrList signalNames( bool super = FALSE ) const; + + int slotOffset() const; + int signalOffset() const; + int propertyOffset() const; + + int numClassInfo( bool super = FALSE ) const; + const QClassInfo *classInfo( int index, bool super = FALSE ) const; + const char *classInfo( const char* name, bool super = FALSE ) const; + +#ifndef QT_NO_PROPERTIES + const QMetaProperty *property( int index, bool super = FALSE ) const; + int findProperty( const char *name, bool super = FALSE ) const; + int indexOfProperty( const QMetaProperty*, bool super = FALSE ) const; + const QMetaProperty* resolveProperty( const QMetaProperty* ) const; + int resolveProperty( int ) const; + QStrList propertyNames( bool super = FALSE ) const; + int numProperties( bool super = FALSE ) const; +#endif + + // static wrappers around constructors, necessary to work around a + // Windows-DLL limitation: objects can only be deleted within a + // DLL if they were actually created within that DLL. + static QMetaObject *new_metaobject( const char *, QMetaObject *, + const QMetaData *const, int, + const QMetaData *const, int, +#ifndef QT_NO_PROPERTIES + const QMetaProperty *const prop_data, int n_props, + const QMetaEnum *const enum_data, int n_enums, +#endif + const QClassInfo *const class_info, int n_info ); +#ifndef QT_NO_PROPERTIES + static QMetaObject *new_metaobject( const char *, QMetaObject *, + const QMetaData *const, int, + const QMetaData *const, int, + const QMetaProperty *const prop_data, int n_props, + const QMetaEnum *const enum_data, int n_enums, + bool (*qt_static_property)(QObject*, int, int, QVariant*), + const QClassInfo *const class_info, int n_info ); + QStrList enumeratorNames( bool super = FALSE ) const; + int numEnumerators( bool super = FALSE ) const; + const QMetaEnum *enumerator( const char* name, bool super = FALSE ) const; +#endif + + static QMetaObject *metaObject( const char *class_name ); + static bool hasMetaObject( const char *class_name ); + +private: + QMemberDict *init( const QMetaData *, int ); + + const char *classname; // class name + const char *superclassname; // super class name + QMetaObject *superclass; // super class meta object + QMetaObjectPrivate *d; // private data for... + void *reserved; // ...binary compatibility + const QMetaData *slotData; // slot meta data + QMemberDict *slotDict; // slot dictionary + const QMetaData *signalData; // signal meta data + QMemberDict *signalDict; // signal dictionary + int signaloffset; + int slotoffset; +#ifndef QT_NO_PROPERTIES + int propertyoffset; +public: + bool qt_static_property( QObject* o, int id, int f, QVariant* v); +private: + friend class QMetaProperty; +#endif + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QMetaObject( const QMetaObject & ); + QMetaObject &operator=( const QMetaObject & ); +#endif +}; + +inline int QMetaObject::slotOffset() const +{ return slotoffset; } + +inline int QMetaObject::signalOffset() const +{ return signaloffset; } + +#ifndef QT_NO_PROPERTIES +inline int QMetaObject::propertyOffset() const +{ return propertyoffset; } +#endif + +typedef QMetaObject *(*QtStaticMetaObjectFunction)(); + +class Q_EXPORT QMetaObjectCleanUp +{ +public: + QMetaObjectCleanUp( const char *mo_name, QtStaticMetaObjectFunction ); + QMetaObjectCleanUp(); + ~QMetaObjectCleanUp(); + + void setMetaObject( QMetaObject *&mo ); + +private: + QMetaObject **metaObject; +}; + +#endif // QMETAOBJECT_H diff --git a/src/kernel/qmime.cpp b/src/kernel/qmime.cpp new file mode 100644 index 0000000..e50757d --- /dev/null +++ b/src/kernel/qmime.cpp @@ -0,0 +1,618 @@ +/**************************************************************************** +** +** Implementation of MIME support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qmime.h" + +#ifndef QT_NO_MIME + +#include "qmap.h" +#include "qstringlist.h" +#include "qfileinfo.h" +#include "qdir.h" +#include "qdragobject.h" +#include "qcleanuphandler.h" +#include "qapplication.h" // ### for now +#include "qclipboard.h" // ### for now + +/*! + \class QMimeSource qmime.h + \brief The QMimeSource class is an abstraction of objects which provide formatted data of a certain MIME type. + + \ingroup io + \ingroup draganddrop + \ingroup misc + + \link dnd.html Drag-and-drop\endlink and + \link QClipboard clipboard\endlink use this abstraction. + + \sa \link http://www.isi.edu/in-notes/iana/assignments/media-types/ + IANA list of MIME media types\endlink +*/ + +static int qt_mime_serial_number = 0; +static QMimeSourceFactory* defaultfactory = 0; +static QSingleCleanupHandler<QMimeSourceFactory> qmime_cleanup_factory; + +/*! + Constructs a mime source and assigns a globally unique serial + number to it. + + \sa serialNumber() +*/ + +QMimeSource::QMimeSource() +{ + ser_no = qt_mime_serial_number++; + cacheType = NoCache; +} + +/*! + \fn int QMimeSource::serialNumber() const + + Returns the mime source's globally unique serial number. +*/ + + +void QMimeSource::clearCache() +{ + if ( cacheType == Text ) { + delete cache.txt.str; + delete cache.txt.subtype; + cache.txt.str = 0; + cache.txt.subtype = 0; + } else if ( cacheType == Graphics ) { + delete cache.gfx.img; + delete cache.gfx.pix; + cache.gfx.img = 0; + cache.gfx.pix = 0; + } + cacheType = NoCache; +} + +/*! + Provided to ensure that subclasses destroy themselves correctly. +*/ +QMimeSource::~QMimeSource() +{ +#ifndef QT_NO_CLIPBOARD + extern void qt_clipboard_cleanup_mime_source(QMimeSource *); + qt_clipboard_cleanup_mime_source(this); +#endif + clearCache(); +} + +/*! + \fn QByteArray QMimeSource::encodedData(const char*) const + + Returns the encoded data of this object in the specified MIME + format. + + Subclasses must reimplement this function. +*/ + + + +/*! + Returns TRUE if the object can provide the data in format \a + mimeType; otherwise returns FALSE. + + If you inherit from QMimeSource, for consistency reasons it is + better to implement the more abstract canDecode() functions such + as QTextDrag::canDecode() and QImageDrag::canDecode(). +*/ +bool QMimeSource::provides(const char* mimeType) const +{ + const char* fmt; + for (int i=0; (fmt = format(i)); i++) { + if ( !qstricmp(mimeType,fmt) ) + return TRUE; + } + return FALSE; +} + + +/*! + \fn const char * QMimeSource::format(int i) const + + Returns the \a{i}-th supported MIME format, or 0. +*/ + + + +class QMimeSourceFactoryData { +public: + QMimeSourceFactoryData() : + last(0) + { + } + + ~QMimeSourceFactoryData() + { + QMap<QString, QMimeSource*>::Iterator it = stored.begin(); + while ( it != stored.end() ) { + delete *it; + ++it; + } + delete last; + } + + QMap<QString, QMimeSource*> stored; + QMap<QString, QString> extensions; + QStringList path; + QMimeSource* last; + QPtrList<QMimeSourceFactory> factories; +}; + + +/*! + \class QMimeSourceFactory qmime.h + \brief The QMimeSourceFactory class is an extensible provider of mime-typed data. + + \ingroup io + \ingroup environment + + A QMimeSourceFactory provides an abstract interface to a + collection of information. Each piece of information is + represented by a QMimeSource object which can be examined and + converted to concrete data types by functions such as + QImageDrag::canDecode() and QImageDrag::decode(). + + The base QMimeSourceFactory can be used in two ways: as an + abstraction of a collection of files or as specifically stored + data. For it to access files, call setFilePath() before accessing + data. For stored data, call setData() for each item (there are + also convenience functions, e.g. setText(), setImage() and + setPixmap(), that simply call setData() with appropriate + parameters). + + The rich text widgets, QTextEdit and QTextBrowser, use + QMimeSourceFactory to resolve references such as images or links + within rich text documents. They either access the default factory + (see \l{defaultFactory()}) or their own (see + \l{QTextEdit::setMimeSourceFactory()}). Other classes that are + capable of displaying rich text (such as QLabel, QWhatsThis or + QMessageBox) always use the default factory. + + A factory can also be used as a container to store data associated + with a name. This technique is useful whenever rich text contains + images that are stored in the program itself, not loaded from the + hard disk. Your program may, for example, define some image data + as: + \code + static const char* myimage_data[]={ + "...", + ... + "..."}; + \endcode + + To be able to use this image within some rich text, for example + inside a QLabel, you must create a QImage from the raw data and + insert it into the factory with a unique name: + \code + QMimeSourceFactory::defaultFactory()->setImage( "myimage", QImage(myimage_data) ); + \endcode + + Now you can create a rich text QLabel with + + \code + QLabel* label = new QLabel( + "Rich text with embedded image:<img source=\"myimage\">" + "Isn't that <em>cute</em>?" ); + \endcode + + When no longer needed, you can clear the data from the factory: + + \code + delete label; + QMimeSourceFactory::defaultFactory()->setData( "myimage", 0 ); + \endcode +*/ + + +/*! + Constructs a QMimeSourceFactory that has no file path and no + stored content. +*/ +QMimeSourceFactory::QMimeSourceFactory() : + d(new QMimeSourceFactoryData) +{ + // add some reasonable defaults + setExtensionType("htm", "text/html;charset=iso8859-1"); + setExtensionType("html", "text/html;charset=iso8859-1"); + setExtensionType("txt", "text/plain"); + setExtensionType("xml", "text/xml;charset=UTF-8"); + setExtensionType("jpg", "image/jpeg"); // support misspelled jpeg files +} + +/*! + Destroys the QMimeSourceFactory, deleting all stored content. +*/ +QMimeSourceFactory::~QMimeSourceFactory() +{ + if ( defaultFactory() == this ) + defaultfactory = 0; + delete d; +} + +QMimeSource* QMimeSourceFactory::dataInternal(const QString& abs_name, const QMap<QString, QString> &extensions ) const +{ + QMimeSource* r = 0; + QFileInfo fi(abs_name); + if ( fi.isReadable() ) { + + // get the right mimetype + QString e = fi.extension(FALSE); + QCString mimetype = "application/octet-stream"; + const char* imgfmt; + if ( extensions.contains(e) ) + mimetype = extensions[e].latin1(); + else if ( ( imgfmt = QImage::imageFormat( abs_name ) ) ) + mimetype = QCString("image/")+QCString(imgfmt).lower(); + + QFile f(abs_name); + if ( f.open(IO_ReadOnly) && f.size() ) { + QByteArray ba(f.size()); + f.readBlock(ba.data(), ba.size()); + QStoredDrag* sr = new QStoredDrag( mimetype ); + sr->setEncodedData( ba ); + delete d->last; + d->last = r = sr; + } + } + + // we didn't find the mime-source, so ask the default factory for + // the mime-source (this one will iterate over all installed ones) + // + // this looks dangerous, as this dataInternal() function will be + // called again when the default factory loops over all installed + // factories (including this), but the static bool looping in + // data() avoids endless recursions + if ( !r && this != defaultFactory() ) + r = (QMimeSource*)defaultFactory()->data( abs_name ); + + return r; +} + + +/*! + Returns a reference to the data associated with \a abs_name. The + return value remains valid only until the next data() or setData() + call, so you should immediately decode the result. + + If there is no data associated with \a abs_name in the factory's + store, the factory tries to access the local filesystem. If \a + abs_name isn't an absolute file name, the factory will search for + it in all defined paths (see \l{setFilePath()}). + + The factory understands all the image formats supported by + QImageIO. Any other mime types are determined by the file name + extension. The default settings are + \code + setExtensionType("html", "text/html;charset=iso8859-1"); + setExtensionType("htm", "text/html;charset=iso8859-1"); + setExtensionType("txt", "text/plain"); + setExtensionType("xml", "text/xml;charset=UTF-8"); + \endcode + The effect of these is that file names ending in "txt" will be + treated as text encoded in the local encoding; those ending in + "xml" will be treated as text encoded in Unicode UTF-8 encoding. + The text/html type is treated specially, since the encoding can be + specified in the html file itself. "html" or "htm" will be treated + as text encoded in the encoding specified by the html meta tag, if + none could be found, the charset of the mime type will be used. + The text subtype ("html", "plain", or "xml") does not affect the + factory, but users of the factory may behave differently. We + recommend creating "xml" files where practical. These files can be + viewed regardless of the runtime encoding and can encode any + Unicode characters without resorting to encoding definitions + inside the file. + + Any file data that is not recognized will be retrieved as a + QMimeSource providing the "application/octet-stream" mime type, + meaning uninterpreted binary data. + + You can add further extensions or change existing ones with + subsequent calls to setExtensionType(). If the extension mechanism + is not sufficient for your problem domain, you can inherit + QMimeSourceFactory and reimplement this function to perform some + more specialized mime-type detection. The same applies if you want + to use the mime source factory to access URL referenced data over + a network. +*/ +const QMimeSource* QMimeSourceFactory::data(const QString& abs_name) const +{ + if ( d->stored.contains(abs_name) ) + return d->stored[abs_name]; + + QMimeSource* r = 0; + QStringList::Iterator it; + if ( abs_name[0] == '/' +#ifdef Q_WS_WIN + || ( abs_name[0] && abs_name[1] == ':' ) || abs_name.startsWith("\\\\") +#endif + ) + { + // handle absolute file names directly + r = dataInternal( abs_name, d->extensions); + } + else { // check list of paths + for ( it = d->path.begin(); !r && it != d->path.end(); ++it ) { + QString filename = *it; + if ( filename[(int)filename.length()-1] != '/' ) + filename += '/'; + filename += abs_name; + r = dataInternal( filename, d->extensions ); + } + } + + static bool looping = FALSE; + if ( !r && this == defaultFactory() ) { + // we found no mime-source and we are the default factory, so + // we know all the other installed mime-source factories, so + // ask them + if ( !looping ) { + // to avoid endless recustions, don't enter the loop below + // if data() got called from within the loop below + looping = TRUE; + QPtrListIterator<QMimeSourceFactory> it( d->factories ); + QMimeSourceFactory *f; + while ( ( f = it.current() ) ) { + ++it; + if ( f == this ) + continue; + r = (QMimeSource*)f->data( abs_name ); + if ( r ) { + looping = FALSE; + return r; + } + } + looping = FALSE; + } + } else if ( !r ) { + // we are not the default mime-source factory, so ask the + // default one for the mime-source, as this one will loop over + // all installed mime-source factories and ask these + r = (QMimeSource*)defaultFactory()->data( abs_name ); + } + + return r; +} + +/*! + Sets the list of directories that will be searched when named data + is requested to the those given in the string list \a path. + + \sa filePath() +*/ +void QMimeSourceFactory::setFilePath( const QStringList& path ) +{ + d->path = path; +} + +/*! + Returns the currently set search paths. +*/ +QStringList QMimeSourceFactory::filePath() const +{ + return d->path; +} + +/*! + Adds another search path, \a p to the existing search paths. + + \sa setFilePath() +*/ +void QMimeSourceFactory::addFilePath( const QString& p ) +{ + d->path += p; +} + +/*! + Sets the mime-type to be associated with the file name extension, + \a ext to \a mimetype. This determines the mime-type for files + found via the paths set by setFilePath(). +*/ +void QMimeSourceFactory::setExtensionType( const QString& ext, const char* mimetype ) +{ + d->extensions.replace(ext, mimetype); +} + +/*! + Converts the absolute or relative data item name \a + abs_or_rel_name to an absolute name, interpreted within the + context (path) of the data item named \a context (this must be an + absolute name). +*/ +QString QMimeSourceFactory::makeAbsolute(const QString& abs_or_rel_name, const QString& context) const +{ + if ( context.isNull() || + !(context[0] == '/' +#ifdef Q_WS_WIN + || ( context[0] && context[1] == ':') +#endif + )) + return abs_or_rel_name; + if ( abs_or_rel_name.isEmpty() ) + return context; + QFileInfo c( context ); + if (!c.isDir()) { + QFileInfo r( c.dir(TRUE), abs_or_rel_name ); + return r.absFilePath(); + } else { + QDir d(context); + QFileInfo r(d, abs_or_rel_name); + return r.absFilePath(); + } +} + +/*! + \overload + A convenience function. See data(const QString& abs_name). The + file name is given in \a abs_or_rel_name and the path is in \a + context. +*/ +const QMimeSource* QMimeSourceFactory::data(const QString& abs_or_rel_name, const QString& context) const +{ + const QMimeSource* r = data(makeAbsolute(abs_or_rel_name,context)); + if ( !r && !d->path.isEmpty() ) + r = data(abs_or_rel_name); + return r; +} + + +/*! + Sets \a text to be the data item associated with the absolute name + \a abs_name. + + Equivalent to setData(abs_name, new QTextDrag(text)). +*/ +void QMimeSourceFactory::setText( const QString& abs_name, const QString& text ) +{ + setData(abs_name, new QTextDrag(text)); +} + +/*! + Sets \a image to be the data item associated with the absolute + name \a abs_name. + + Equivalent to setData(abs_name, new QImageDrag(image)). +*/ +void QMimeSourceFactory::setImage( const QString& abs_name, const QImage& image ) +{ + setData(abs_name, new QImageDrag(image)); +} + +/*! + Sets \a pixmap to be the data item associated with the absolute + name \a abs_name. +*/ +void QMimeSourceFactory::setPixmap( const QString& abs_name, const QPixmap& pixmap ) +{ + setData(abs_name, new QImageDrag(pixmap.convertToImage())); +} + +/*! + Sets \a data to be the data item associated with + the absolute name \a abs_name. Note that the ownership of \a data is + transferred to the factory: do not delete or access the pointer after + passing it to this function. + + Passing 0 for data removes previously stored data. +*/ +void QMimeSourceFactory::setData( const QString& abs_name, QMimeSource* data ) +{ + if ( d->stored.contains(abs_name) ) + delete d->stored[abs_name]; + d->stored.replace(abs_name,data); +} + + +/*! + Returns the application-wide default mime source factory. This + factory is used by rich text rendering classes such as + QSimpleRichText, QWhatsThis and QMessageBox to resolve named + references within rich text documents. It serves also as the + initial factory for the more complex render widgets, QTextEdit and + QTextBrowser. + + \sa setDefaultFactory() +*/ +QMimeSourceFactory* QMimeSourceFactory::defaultFactory() +{ + if (!defaultfactory) + { + defaultfactory = new QMimeSourceFactory(); + qmime_cleanup_factory.set( &defaultfactory ); + } + return defaultfactory; +} + +/*! + Sets the default \a factory, destroying any previously set mime + source provider. The ownership of the factory is transferred to + Qt. + + \sa defaultFactory() +*/ +void QMimeSourceFactory::setDefaultFactory( QMimeSourceFactory* factory) +{ + if ( !defaultfactory ) + qmime_cleanup_factory.set( &defaultfactory ); + else if ( defaultfactory != factory ) + delete defaultfactory; + defaultfactory = factory; +} + +/*! + Sets the defaultFactory() to 0 and returns the previous one. +*/ + +QMimeSourceFactory* QMimeSourceFactory::takeDefaultFactory() +{ + QMimeSourceFactory *f = defaultfactory; + defaultfactory = 0; + return f; +} + +/*! + Adds the QMimeSourceFactory \a f to the list of available + mimesource factories. If the defaultFactory() can't resolve a + data() it iterates over the list of installed mimesource factories + until the data can be resolved. + + \sa removeFactory(); +*/ + +void QMimeSourceFactory::addFactory( QMimeSourceFactory *f ) +{ + QMimeSourceFactory::defaultFactory()->d->factories.append( f ); +} + +/*! + Removes the mimesource factory \a f from the list of available + mimesource factories. + + \sa addFactory(); +*/ + +void QMimeSourceFactory::removeFactory( QMimeSourceFactory *f ) +{ + QMimeSourceFactory::defaultFactory()->d->factories.removeRef( f ); +} + +#endif // QT_NO_MIME diff --git a/src/kernel/qmime.h b/src/kernel/qmime.h new file mode 100644 index 0000000..a34489e --- /dev/null +++ b/src/kernel/qmime.h @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Definition of mime classes +** +** Created : 981204 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QMIME_H +#define QMIME_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qmap.h" +#endif // QT_H + +#ifndef QT_NO_MIME + +class QImageDrag; +class QTextDrag; + +class Q_EXPORT QMimeSource +{ + friend class QClipboardData; + +public: + QMimeSource(); + virtual ~QMimeSource(); + virtual const char* format( int n = 0 ) const = 0; + virtual bool provides( const char* ) const; + virtual QByteArray encodedData( const char* ) const = 0; + int serialNumber() const; + +private: + int ser_no; + enum { NoCache, Text, Graphics } cacheType; + union + { + struct + { + QString *str; + QCString *subtype; + } txt; + struct + { + QImage *img; + QPixmap *pix; + } gfx; + } cache; + void clearCache(); + + // friends for caching + friend class QImageDrag; + friend class QTextDrag; + +}; + +inline int QMimeSource::serialNumber() const +{ return ser_no; } + +class QStringList; +class QMimeSourceFactoryData; + +class Q_EXPORT QMimeSourceFactory { +public: + QMimeSourceFactory(); + virtual ~QMimeSourceFactory(); + + static QMimeSourceFactory* defaultFactory(); + static void setDefaultFactory( QMimeSourceFactory* ); + static QMimeSourceFactory* takeDefaultFactory(); + static void addFactory( QMimeSourceFactory *f ); + static void removeFactory( QMimeSourceFactory *f ); + + virtual const QMimeSource* data(const QString& abs_name) const; + virtual QString makeAbsolute(const QString& abs_or_rel_name, const QString& context) const; + const QMimeSource* data(const QString& abs_or_rel_name, const QString& context) const; + + virtual void setText( const QString& abs_name, const QString& text ); + virtual void setImage( const QString& abs_name, const QImage& im ); + virtual void setPixmap( const QString& abs_name, const QPixmap& pm ); + virtual void setData( const QString& abs_name, QMimeSource* data ); + virtual void setFilePath( const QStringList& ); + virtual QStringList filePath() const; + void addFilePath( const QString& ); + virtual void setExtensionType( const QString& ext, const char* mimetype ); + +private: + QMimeSource *dataInternal(const QString& abs_name, const QMap<QString, QString> &extensions ) const; + QMimeSourceFactoryData* d; +}; + +#if defined(Q_WS_WIN) + +#ifndef QT_H +#include "qptrlist.h" // down here for GCC 2.7.* compatibility +#endif // QT_H + +/* + Encapsulation of conversion between MIME and Windows CLIPFORMAT. + Not need on X11, as the underlying protocol uses the MIME standard + directly. +*/ + +class Q_EXPORT QWindowsMime { +public: + QWindowsMime(); + virtual ~QWindowsMime(); + + static void initialize(); + + static QPtrList<QWindowsMime> all(); + static QWindowsMime* convertor( const char* mime, int cf ); + static const char* cfToMime(int cf); + + static int registerMimeType(const char *mime); + + virtual const char* convertorName()=0; + virtual int countCf()=0; + virtual int cf(int index)=0; + virtual bool canConvert( const char* mime, int cf )=0; + virtual const char* mimeFor(int cf)=0; + virtual int cfFor(const char* )=0; + virtual QByteArray convertToMime( QByteArray data, const char* mime, int cf )=0; + virtual QByteArray convertFromMime( QByteArray data, const char* mime, int cf )=0; +}; + +#endif +#if defined(Q_WS_MAC) + +#ifndef QT_H +#include "qptrlist.h" // down here for GCC 2.7.* compatibility +#endif // QT_H + +/* + Encapsulation of conversion between MIME and Mac flavor. + Not need on X11, as the underlying protocol uses the MIME standard + directly. +*/ + +class Q_EXPORT QMacMime { + char type; +public: + enum QMacMimeType { MIME_DND=0x01, MIME_CLIP=0x02, MIME_QT_CONVERTOR=0x04, MIME_ALL=MIME_DND|MIME_CLIP }; + QMacMime(char); + virtual ~QMacMime(); + + static void initialize(); + + static QPtrList<QMacMime> all(QMacMimeType); + static QMacMime* convertor(QMacMimeType, const char* mime, int flav); + static const char* flavorToMime(QMacMimeType, int flav); + + virtual const char* convertorName()=0; + virtual int countFlavors()=0; + virtual int flavor(int index)=0; + virtual bool canConvert(const char* mime, int flav)=0; + virtual const char* mimeFor(int flav)=0; + virtual int flavorFor(const char*)=0; + virtual QByteArray convertToMime(QValueList<QByteArray> data, const char* mime, int flav)=0; + virtual QValueList<QByteArray> convertFromMime(QByteArray data, const char* mime, int flav)=0; +}; + +#endif // Q_WS_MAC + +#endif // QT_NO_MIME + +#endif // QMIME_H diff --git a/src/kernel/qmngio.cpp b/src/kernel/qmngio.cpp new file mode 100644 index 0000000..2644270 --- /dev/null +++ b/src/kernel/qmngio.cpp @@ -0,0 +1,464 @@ +/**************************************************************************** +** +** Implementation of MNG QImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QT_CLEAN_NAMESPACE +#define QT_CLEAN_NAMESPACE +#endif + +#include "qdatetime.h" + +#ifndef QT_NO_IMAGEIO_MNG + +#include "qimage.h" +#include "qasyncimageio.h" +#include "qiodevice.h" +#include "qmngio.h" + +// Define XMD_H prohibits the included headers of libmng.h to typedef INT32. +// This is needed for Borland with STL support, since in that case, INT32 is +// already defined by some Borland header. +#define XMD_H +#if defined(Q_OS_UNIXWARE) +# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this +#endif +#include <libmng.h> +#include <stdlib.h> + + +#ifndef QT_NO_ASYNC_IMAGE_IO + +class QMNGFormat : public QImageFormat { +public: + QMNGFormat(); + virtual ~QMNGFormat(); + + int decode(QImage& img, QImageConsumer* consumer, + const uchar* buffer, int length); + + bool openstream() + { + // ### We should figure out how many loops an MNG has, but for now always assume infinite. + if (consumer) + consumer->setLooping(0); + return TRUE; + } + bool closestream( ) + { + if (consumer) + consumer->end(); + return TRUE; + } + bool readdata( mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pRead ) + { + uint m = ndata + nbuffer - ubuffer; + if ( iBuflen > m ) { + iBuflen = m; + } + *pRead = iBuflen; + uint n = nbuffer-ubuffer; + if ( iBuflen < n ) { + // enough in buffer + memcpy(pBuf, buffer+ubuffer, iBuflen); + ubuffer += iBuflen; + return TRUE; + } + if ( n ) { + // consume buffer + memcpy(pBuf, buffer+ubuffer, n ); + pBuf = (mng_ptr)((char*)pBuf + n); + iBuflen -= n; + ubuffer = nbuffer; + } + if ( iBuflen ) { + // fill from incoming data + memcpy(pBuf, data, iBuflen); + data += iBuflen; + ndata -= iBuflen; + } + return TRUE; + } + bool errorproc( mng_int32 iErrorcode, + mng_int8 /*iSeverity*/, + mng_chunkid iChunkname, + mng_uint32 /*iChunkseq*/, + mng_int32 iExtra1, + mng_int32 iExtra2, + mng_pchar zErrortext ) + { + qWarning("MNG error %d: %s; chunk %c%c%c%c; subcode %d:%d", + iErrorcode, zErrortext ? zErrortext : "", + (iChunkname>>24)&0xff, + (iChunkname>>16)&0xff, + (iChunkname>>8)&0xff, + (iChunkname>>0)&0xff, + iExtra1,iExtra2); + return TRUE; + } + bool processheader( mng_uint32 iWidth, mng_uint32 iHeight ) + { + image->create(iWidth,iHeight,32); + image->setAlphaBuffer(TRUE); + memset(image->bits(),0,iWidth*iHeight*4); + consumer->setSize(iWidth,iHeight); + mng_set_canvasstyle(handle, + QImage::systemByteOrder() == QImage::LittleEndian + ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8 ); + return TRUE; + } + mng_ptr getcanvasline( mng_uint32 iLinenr ) + { + return image->scanLine(iLinenr); + } + mng_bool refresh( mng_uint32 x, mng_uint32 y, mng_uint32 w, mng_uint32 h ) + { + QRect r(x,y,w,h); + consumer->changed(r); + consumer->setFramePeriod(0); + consumer->frameDone(); + return TRUE; + } + mng_uint32 gettickcount( ) + { + return timer.elapsed() - losttime; + } + bool settimer( mng_uint32 iMsecs ) + { + consumer->setFramePeriod(iMsecs); + consumer->frameDone(); + state = Time; + losingtimer.start(); + losttime -= iMsecs; + return TRUE; + } + +private: + // Animation-level information + enum { MovieStart, Time, Data, Data2 } state; + + // Image-level information + mng_handle handle; + + // For storing unused data + uchar *buffer; + uint maxbuffer; + uint nbuffer; + + // Timing + QTime timer; + QTime losingtimer; + int losttime; + + void enlargeBuffer(uint n) + { + if ( n > maxbuffer ) { + maxbuffer = n; + buffer = (uchar*)realloc(buffer,n); + } + } + + // Temporary locals during single data-chunk processing + const uchar* data; + uint ndata; + uint ubuffer; + QImageConsumer* consumer; + QImage* image; +}; + +class QMNGFormatType : public QImageFormatType +{ + QImageFormat* decoderFor(const uchar* buffer, int length); + const char* formatName() const; +}; + + +/* + \class QMNGFormat qmngio.h + \brief Incremental image decoder for MNG image format. + + \ingroup images + \ingroup graphics + + This subclass of QImageFormat decodes MNG format images, + including animated MNGs. + + Animated MNG images are standard MNG images. The MNG standard + defines two extension chunks that are useful for animations: + + <dl> + <dt>gIFg - GIF-like Graphic Control Extension + <dd>Includes frame disposal, user input flag (we ignore this), + and inter-frame delay. + <dt>gIFx - GIF-like Application Extension + <dd>Multi-purpose, but we just use the Netscape extension + which specifies looping. + </dl> + + The subimages usually contain a offset chunk (oFFs) but need not. + + The first image defines the "screen" size. Any subsequent image that + doesn't fit is clipped. + +TODO: decide on this point. gIFg gives disposal types, so it can be done. + All images paste (\e not composite, just place all-channel copying) + over the previous image to produce a subsequent frame. +*/ + +/* + \class QMNGFormatType qasyncimageio.h + \brief Incremental image decoder for MNG image format. + + \ingroup images + \ingroup graphics + \ingroup io + + This subclass of QImageFormatType recognizes MNG + format images, creating a QMNGFormat when required. An instance + of this class is created automatically before any other factories, + so you should have no need for such objects. +*/ + +QImageFormat* QMNGFormatType::decoderFor( const uchar* buffer, int length ) +{ + if (length < 8) return 0; + + if (buffer[0]==138 // MNG signature + && buffer[1]=='M' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10 + || buffer[0]==139 // JNG signature + && buffer[1]=='J' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10 +#ifdef QT_NO_IMAGEIO_PNG // if we don't have native PNG support use libmng + || buffer[0]==137 // PNG signature + && buffer[1]=='P' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10 +#endif + ) + return new QMNGFormat; + return 0; +} + +const char* QMNGFormatType::formatName() const +{ + return "MNG"; +} + + +/*! + Constructs a QMNGFormat. +*/ +QMNGFormat::QMNGFormat() +{ + state = MovieStart; + handle = 0; + nbuffer = 0; + maxbuffer = 0; + buffer = 0; + losttime = 0; +} + +/* + Destroys a QMNGFormat. +*/ +QMNGFormat::~QMNGFormat() +{ + // We're setting the consumer to 0 since it may have been + // deleted by read_async_image in qimage.cpp + consumer = 0; + if (handle) mng_cleanup(&handle); +} + + +// C-callback to C++-member-function conversion +// +static mng_bool openstream( mng_handle handle ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->openstream(); +} +static mng_bool closestream( mng_handle handle ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->closestream(); +} +static mng_bool readdata( mng_handle handle, mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pRead ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->readdata(pBuf,iBuflen,pRead); +} +static mng_bool errorproc( mng_handle handle, + mng_int32 iErrorcode, + mng_int8 iSeverity, + mng_chunkid iChunkname, + mng_uint32 iChunkseq, + mng_int32 iExtra1, + mng_int32 iExtra2, + mng_pchar zErrortext ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->errorproc(iErrorcode, + iSeverity,iChunkname,iChunkseq,iExtra1,iExtra2,zErrortext); +} +static mng_bool processheader( mng_handle handle, + mng_uint32 iWidth, mng_uint32 iHeight ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->processheader(iWidth,iHeight); +} +static mng_ptr getcanvasline( mng_handle handle, mng_uint32 iLinenr ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->getcanvasline(iLinenr); +} +static mng_bool refresh( mng_handle handle, + mng_uint32 iTop, + mng_uint32 iLeft, + mng_uint32 iBottom, + mng_uint32 iRight ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->refresh(iTop,iLeft,iBottom,iRight); +} +static mng_uint32 gettickcount( mng_handle handle ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->gettickcount(); +} +static mng_bool settimer( mng_handle handle, mng_uint32 iMsecs ) +{ + return ((QMNGFormat*)mng_get_userdata(handle))->settimer(iMsecs); +} + +static mng_ptr memalloc( mng_size_t iLen ) +{ + return calloc(1,iLen); +} +static void memfree( mng_ptr iPtr, mng_size_t /*iLen*/ ) +{ + free(iPtr); +} + +/*! + This function decodes some data into image changes. + + Returns the number of bytes consumed. +*/ +int QMNGFormat::decode( QImage& img, QImageConsumer* cons, + const uchar* buf, int length ) +{ + consumer = cons; + image = &img; + + data = buf; + ndata = length; + ubuffer = 0; + + if ( state == MovieStart ) { + handle = mng_initialize( (mng_ptr)this, ::memalloc, ::memfree, 0 ); + mng_set_suspensionmode( handle, MNG_TRUE ); + mng_setcb_openstream( handle, ::openstream ); + mng_setcb_closestream( handle, ::closestream ); + mng_setcb_readdata( handle, ::readdata ); + mng_setcb_errorproc( handle, ::errorproc ); + mng_setcb_processheader( handle, ::processheader ); + mng_setcb_getcanvasline( handle, ::getcanvasline ); + mng_setcb_refresh( handle, ::refresh ); + mng_setcb_gettickcount( handle, ::gettickcount ); + mng_setcb_settimer( handle, ::settimer ); + state = Data; + mng_readdisplay(handle); + losingtimer.start(); + } + + losttime += losingtimer.elapsed(); + if ( ndata || !length ) + mng_display_resume(handle); + losingtimer.start(); + + image = 0; + + nbuffer -= ubuffer; + if ( nbuffer ) { + // Move back unused tail + memcpy(buffer,buffer+ubuffer,nbuffer); + } + if ( ndata ) { + // Not all used. + enlargeBuffer(nbuffer+ndata); + memcpy(buffer+nbuffer,data,ndata); + nbuffer += ndata; + } + + return length; +} + +static QMNGFormatType* globalMngFormatTypeObject = 0; + +#endif // QT_NO_ASYNC_IMAGE_IO + +#ifndef QT_NO_ASYNC_IMAGE_IO +void qCleanupMngIO() +{ + if ( globalMngFormatTypeObject ) { + delete globalMngFormatTypeObject; + globalMngFormatTypeObject = 0; + } +} +#endif + +void qInitMngIO() +{ + static bool done = FALSE; + if ( !done ) { + done = TRUE; +#ifndef QT_NO_ASYNC_IMAGE_IO + globalMngFormatTypeObject = new QMNGFormatType; + qAddPostRoutine( qCleanupMngIO ); +#endif + } +} + +#endif // QT_NO_IMAGEIO_MNG diff --git a/src/kernel/qmngio.h b/src/kernel/qmngio.h new file mode 100644 index 0000000..f2c20d8 --- /dev/null +++ b/src/kernel/qmngio.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Definition of MNG QImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QMNGIO_H +#define QMNGIO_H + +#ifndef QT_H +#endif // QT_H + +#ifndef QT_NO_IMAGEIO_MNG + +void qInitMngIO(); + +#endif // QT_NO_IMAGEIO_MNG + +#endif // QMNGIO_H diff --git a/src/kernel/qmotifdnd_x11.cpp b/src/kernel/qmotifdnd_x11.cpp new file mode 100644 index 0000000..c9399f9 --- /dev/null +++ b/src/kernel/qmotifdnd_x11.cpp @@ -0,0 +1,978 @@ +/**************************************************************************** +** +** Implementation of Motif Dynamic Drag and Drop class +** +** Created : 950419 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +/* The following copyright notice pertains to the code as contributed +to Trolltech, not to Trolltech's modifications. It is replicated +in doc/dnd.doc, where the documentation system can see it. */ + +/* Copyright 1996 Daniel Dardailler. + + Permission to use, copy, modify, distribute, and sell this software + for any purpose is hereby granted without fee, provided that the above + copyright notice appear in all copies and that both that copyright + notice and this permission notice appear in supporting documentation, + and that the name of Daniel Dardailler not be used in advertising or + publicity pertaining to distribution of the software without specific, + written prior permission. Daniel Dardailler makes no representations + about the suitability of this software for any purpose. It is + provided "as is" without express or implied warranty. + + Modifications Copyright 1999 Matt Koss, under the same license as + above. +************************************************************/ + +/***********************************************************/ +/* Motif Drag&Drop Dynamic Protocol messaging API code */ +/* Only requires Xlib layer - not MT safe */ +/* Author: Daniel Dardailler, daniel@x.org */ +/* Adapted by : Matt Koss, koss@napri.sk */ +/* Further adaptions by : Trolltech ASA */ +/***********************************************************/ + +#include "qplatformdefs.h" + +#include "qapplication.h" + +#ifndef QT_NO_DRAGANDDROP + +#include "qwidget.h" +#include "qt_x11_p.h" + +#include <stdlib.h> + + +static Atom atom_message_type, atom_receiver_info, atom_src_property_type; +static Atom atom_motif_window, atom_target_list ; + +static bool in_drop_site = FALSE; +static Window cur_window = 0; +static QWidget *drop_widget = 0L; + +static Atom Dnd_transfer_success, Dnd_transfer_failure; + +static Atom Dnd_selection; +static Time Dnd_selection_time; + +static Atom * src_targets ; +static ushort num_src_targets ; + +extern bool qt_motifdnd_active; + +// this stuff is copied from qclipboard_x11.cpp + +extern bool qt_xclb_wait_for_event( Display *dpy, Window win, int type, + XEvent *event, int timeout ); +extern bool qt_xclb_read_property( Display *dpy, Window win, Atom property, + bool deleteProperty, + QByteArray *buffer, int *size, Atom *type, + int *format, bool nullterm ); + +extern Atom* qt_xdnd_str_to_atom( const char *mimeType ); +extern const char* qt_xdnd_atom_to_str( Atom ); + + +// Motif definitions +#define DndVersion 1 +#define DndRevision 0 +#define DndIncludeVersion (DndVersion * 10 + DndRevision) + +/* The following values are used in the DndData structure */ + +/* protocol style */ +#define DND_DRAG_NONE 0 +#define DND_DRAG_DROP_ONLY 1 +#define DND_DRAG_DYNAMIC 5 + +/* message type */ +#define DND_TOP_LEVEL_ENTER 0 +#define DND_TOP_LEVEL_LEAVE 1 +#define DND_DRAG_MOTION 2 +#define DND_DROP_SITE_ENTER 3 +#define DND_DROP_SITE_LEAVE 4 +#define DND_DROP_START 5 +#define DND_OPERATION_CHANGED 8 + +/* operation(s) */ +#define DND_NOOP 0L +#define DND_MOVE (1L << 0) +#define DND_COPY (1L << 1) +#define DND_LINK (1L << 2) + +/* status */ +#define DND_NO_DROP_SITE 1 +#define DND_INVALID_DROP_SITE 2 +#define DND_VALID_DROP_SITE 3 + +/* completion */ +#define DND_DROP 0 +#define DND_DROP_HELP 1 +#define DND_DROP_CANCEL 2 + +#define BYTE unsigned char +#define CARD32 unsigned int +#define CARD16 unsigned short +#define INT16 signed short + +/* Client side structure used in the API */ +typedef struct { + unsigned char reason; /* message type: DND_TOP_LEVEL_ENTER, etc */ + Time time ; + unsigned char operation; + unsigned char operations; + unsigned char status; + unsigned char completion; + short x ; + short y ; + Window src_window ; + Atom property ; +} DndData ; + + +typedef struct _DndSrcProp { + BYTE byte_order ; + BYTE protocol_version ; + CARD16 target_index ; + CARD32 selection ; +} DndSrcProp ; + +typedef struct _DndReceiverProp { + BYTE byte_order ; + BYTE protocol_version ; + BYTE protocol_style ; + BYTE pad1; + CARD32 proxy_window; + CARD16 num_drop_sites ; + CARD16 pad2; + CARD32 total_size; +} DndReceiverProp ; + +/* need to use some union hack since window and property are in + different order depending on the message ... */ +typedef struct _DndTop { + CARD32 src_window; + CARD32 property; +} DndTop ; + +typedef struct _DndPot { + INT16 x; + INT16 y; + CARD32 property; + CARD32 src_window; +} DndPot ; + +typedef struct _DndMessage { + BYTE reason; + BYTE byte_order; + CARD16 flags; + CARD32 time; + union { + DndTop top ; + DndPot pot ; + } data ; +} DndMessage ; + +typedef struct { + BYTE byte_order; + BYTE protocol_version; + CARD16 num_target_lists; + CARD32 data_size; + /* then come series of CARD16,CARD32,CARD32,CARD32... */ +} DndTargets; + + +/* protocol version */ +#define DND_PROTOCOL_VERSION 0 + + +#define DND_EVENT_TYPE_MASK ((BYTE)0x80) +#define DND_EVENT_TYPE_SHIFT 7 +#define DND_CLEAR_EVENT_TYPE ((BYTE)0x7F) + +/* message_type is data[0] of the client_message + this return 1 (receiver bit up) or 0 (initiator) */ +#define DND_GET_EVENT_TYPE(message_type) \ +((char) (((message_type) & DND_EVENT_TYPE_MASK) >> DND_EVENT_TYPE_SHIFT)) + +/* event_type can be 0 (initiator) or 1 (receiver) */ +#define DND_SET_EVENT_TYPE(event_type) \ +(((BYTE)(event_type) << DND_EVENT_TYPE_SHIFT) & DND_EVENT_TYPE_MASK) + + +#define DND_OPERATION_MASK ((CARD16) 0x000F) +#define DND_OPERATION_SHIFT 0 +#define DND_STATUS_MASK ((CARD16) 0x00F0) +#define DND_STATUS_SHIFT 4 +#define DND_OPERATIONS_MASK ((CARD16) 0x0F00) +#define DND_OPERATIONS_SHIFT 8 +#define DND_COMPLETION_MASK ((CARD16) 0xF000) +#define DND_COMPLETION_SHIFT 12 + +#define DND_GET_OPERATION(flags) \ +((unsigned char) \ +(((flags) & DND_OPERATION_MASK) >> DND_OPERATION_SHIFT)) + +#define DND_SET_OPERATION(operation) \ +(((CARD16)(operation) << DND_OPERATION_SHIFT)\ +& DND_OPERATION_MASK) + +#define DND_GET_STATUS(flags) \ +((unsigned char) \ +(((flags) & DND_STATUS_MASK) >> DND_STATUS_SHIFT)) + +#define DND_SET_STATUS(status) \ +(((CARD16)(status) << DND_STATUS_SHIFT)\ +& DND_STATUS_MASK) + +#define DND_GET_OPERATIONS(flags) \ +((unsigned char) \ +(((flags) & DND_OPERATIONS_MASK) >> DND_OPERATIONS_SHIFT)) + +#define DND_SET_OPERATIONS(operation) \ +(((CARD16)(operation) << DND_OPERATIONS_SHIFT)\ +& DND_OPERATIONS_MASK) + +#define DND_GET_COMPLETION(flags) \ +((unsigned char) \ +(((flags) & DND_COMPLETION_MASK) >> DND_COMPLETION_SHIFT)) + +#define DND_SET_COMPLETION(completion) \ +(((CARD16)(completion) << DND_COMPLETION_SHIFT)\ +& DND_COMPLETION_MASK) + + +#define SWAP4BYTES(l) {\ +struct { unsigned t :32;} bit32;\ +char n, *tp = (char *) &bit32;\ +bit32.t = l;\ +n = tp[0]; tp[0] = tp[3]; tp[3] = n;\ +n = tp[1]; tp[1] = tp[2]; tp[2] = n;\ +l = bit32.t;\ +} + +#define SWAP2BYTES(s) {\ +struct { unsigned t :16; } bit16;\ +char n, *tp = (char *) &bit16;\ +bit16.t = s;\ +n = tp[0]; tp[0] = tp[1]; tp[1] = n;\ +s = bit16.t;\ +} + + +/** Private extern functions */ + +static unsigned char DndByteOrder (); + + +/***** Targets/Index stuff */ + +typedef struct { + int num_targets; + Atom *targets; +} DndTargetsTableEntryRec, * DndTargetsTableEntry; + +typedef struct { + int num_entries; + DndTargetsTableEntry entries; +} DndTargetsTableRec, * DndTargetsTable; + + +static int _DndIndexToTargets(Display * display, + int index, + Atom ** targets); + +extern void qt_x11_intern_atom( const char *, Atom * ); + +///////////////////////////////////////////////////////////////// + +void qt_x11_motifdnd_init() +{ + /* Init atoms used in the com */ + + qt_x11_intern_atom( "_MOTIF_DRAG_AND_DROP_MESSAGE", &atom_message_type ); + qt_x11_intern_atom( "_MOTIF_DRAG_INITIATOR_INFO", &atom_src_property_type ); + qt_x11_intern_atom( "_MOTIF_DRAG_RECEIVER_INFO", &atom_receiver_info ); + qt_x11_intern_atom( "_MOTIF_DRAG_WINDOW", &atom_motif_window ); + qt_x11_intern_atom( "_MOTIF_DRAG_TARGETS", &atom_target_list ); + + qt_x11_intern_atom( "XmTRANSFER_SUCCESS", &Dnd_transfer_success ); + qt_x11_intern_atom( "XmTRANSFER_FAILURE", &Dnd_transfer_failure ); + + char my_dnd_selection_name[30]; // 11-digit number should be enough + sprintf(my_dnd_selection_name, "_MY_DND_SELECTION_%d", (int)getpid()); + qt_x11_intern_atom( my_dnd_selection_name, &Dnd_selection ); +} + +static unsigned char DndByteOrder () +{ + static unsigned char byte_order = 0; + + if (!byte_order) { + unsigned int endian = 1; + byte_order = (*((char *)&endian))?'l':'B'; + } + return byte_order ; +} + + + +static void DndReadSourceProperty(Display * dpy, + Window window, Atom dnd_selection, + Atom ** targets, unsigned short * num_targets) +{ + DndSrcProp * src_prop = 0; + Atom type ; + int format ; + unsigned long bytesafter, lengthRtn; + + if ((XGetWindowProperty (dpy, window, dnd_selection, 0L, 100000L, + False, atom_src_property_type, &type, + &format, &lengthRtn, &bytesafter, + (unsigned char **) &src_prop) != Success) + || (type == None)) { + *num_targets = 0; + return ; + } + + if (src_prop->byte_order != DndByteOrder()) { + SWAP2BYTES(src_prop->target_index); + SWAP4BYTES(src_prop->selection); + } + + *num_targets = _DndIndexToTargets(dpy, src_prop->target_index, targets); + + XFree((char*)src_prop); +} + + +/* Position the _MOTIF_DRAG_RECEIVER_INFO property on the dropsite window. + Called by the receiver of the drop to indicate the + supported protocol style : dynamic, drop_only or none */ +static void DndWriteReceiverProperty(Display * dpy, Window window, + unsigned char protocol_style) +{ + DndReceiverProp receiver_prop ; + + receiver_prop.byte_order = DndByteOrder() ; + receiver_prop.protocol_version = DND_PROTOCOL_VERSION; + receiver_prop.protocol_style = protocol_style ; + receiver_prop.proxy_window = None ; + receiver_prop.num_drop_sites = 0 ; + receiver_prop.total_size = sizeof(DndReceiverProp); + + /* write the buffer to the property */ + XChangeProperty (dpy, window, atom_receiver_info, atom_receiver_info, + 8, PropModeReplace, + (unsigned char *)&receiver_prop, + sizeof(DndReceiverProp)); +} + + +/* protocol style equiv (preregister stuff really) */ +#define DND_DRAG_DROP_ONLY_EQUIV 3 +#define DND_DRAG_DYNAMIC_EQUIV1 2 +#define DND_DRAG_DYNAMIC_EQUIV2 4 + + +/* Produce a client message to be sent by the caller */ +static void DndFillClientMessage(Display * dpy, Window window, + XClientMessageEvent *cm, + DndData * dnd_data, + char receiver) +{ + DndMessage * dnd_message = (DndMessage*)&cm->data.b[0] ; + + cm->display = dpy; + cm->type = ClientMessage; + cm->serial = LastKnownRequestProcessed(dpy); + cm->send_event = True; + cm->window = window; + cm->format = 8; + cm->message_type = atom_message_type ;/* _MOTIF_DRAG_AND_DROP_MESSAGE */ + + dnd_message->reason = dnd_data->reason | DND_SET_EVENT_TYPE(receiver); + + dnd_message->byte_order = DndByteOrder(); + + /* we're filling in flags with more stuff that necessary, + depending on the reason, but it doesn't matter */ + dnd_message->flags = 0 ; + dnd_message->flags |= DND_SET_STATUS(dnd_data->status) ; + dnd_message->flags |= DND_SET_OPERATION(dnd_data->operation) ; + dnd_message->flags |= DND_SET_OPERATIONS(dnd_data->operations) ; + dnd_message->flags |= DND_SET_COMPLETION(dnd_data->completion) ; + + dnd_message->time = dnd_data->time ; + + switch(dnd_data->reason) { + case DND_DROP_SITE_LEAVE: break ; + case DND_TOP_LEVEL_ENTER: + case DND_TOP_LEVEL_LEAVE: + dnd_message->data.top.src_window = dnd_data->src_window ; + dnd_message->data.top.property = dnd_data->property ; + break ; /* cannot fall thru since the byte layout is different in + both set of messages, see top and pot union stuff */ + + case DND_DRAG_MOTION: + case DND_OPERATION_CHANGED: + case DND_DROP_SITE_ENTER: + case DND_DROP_START: + dnd_message->data.pot.x = dnd_data->x ; /* mouse position */ + dnd_message->data.pot.y = dnd_data->y ; + dnd_message->data.pot.src_window = dnd_data->src_window ; + dnd_message->data.pot.property = dnd_data->property ; + break ; + default: + break ; + } + +} + +static Bool DndParseClientMessage(XClientMessageEvent *cm, DndData * dnd_data, + char * receiver) +{ + DndMessage * dnd_message = (DndMessage*)&cm->data.b[0] ; + + if (cm->message_type != atom_message_type) { + return False ; + } + + if (dnd_message->byte_order != DndByteOrder()) { + SWAP2BYTES(dnd_message->flags); + SWAP4BYTES(dnd_message->time); + } /* do the rest in the switch */ + + dnd_data->reason = dnd_message->reason ; + if (DND_GET_EVENT_TYPE(dnd_data->reason)) + *receiver = 1 ; + else + *receiver = 0 ; + dnd_data->reason &= DND_CLEAR_EVENT_TYPE ; + + dnd_data->time = dnd_message->time ; + + /* we're reading in more stuff that necessary. but who cares */ + dnd_data->status = DND_GET_STATUS(dnd_message->flags) ; + dnd_data->operation = DND_GET_OPERATION(dnd_message->flags) ; + dnd_data->operations = DND_GET_OPERATIONS(dnd_message->flags) ; + dnd_data->completion = DND_GET_COMPLETION(dnd_message->flags) ; + + switch(dnd_data->reason) { + case DND_TOP_LEVEL_ENTER: + case DND_TOP_LEVEL_LEAVE: + if (dnd_message->byte_order != DndByteOrder()) { + SWAP4BYTES(dnd_message->data.top.src_window); + SWAP4BYTES(dnd_message->data.top.property); + } + dnd_data->src_window = dnd_message->data.top.src_window ; + dnd_data->property = dnd_message->data.top.property ; + break ; /* cannot fall thru, see above comment in write msg */ + + case DND_DRAG_MOTION: + case DND_OPERATION_CHANGED: + case DND_DROP_SITE_ENTER: + case DND_DROP_START: + if (dnd_message->byte_order != DndByteOrder()) { + SWAP2BYTES(dnd_message->data.pot.x); + SWAP2BYTES(dnd_message->data.pot.y); + SWAP4BYTES(dnd_message->data.pot.property); + SWAP4BYTES(dnd_message->data.pot.src_window); + } + dnd_data->x = dnd_message->data.pot.x ; + dnd_data->y = dnd_message->data.pot.y ; + dnd_data->property = dnd_message->data.pot.property ; + dnd_data->src_window = dnd_message->data.pot.src_window ; + break ; + + case DND_DROP_SITE_LEAVE: + break; + default: + break ; + } + + return True ; +} + + +static Window MotifWindow(Display *display ) +{ + Atom type; + int format; + unsigned long size; + unsigned long bytes_after; + Window *property = 0; + Window motif_window ; + + /* this version does no caching, so it's slow: round trip each time */ + + if ((XGetWindowProperty (display, DefaultRootWindow(display), + atom_motif_window, + 0L, 100000L, False, AnyPropertyType, + &type, &format, &size, &bytes_after, + (unsigned char **) &property) == Success) && + (type != None)) { + motif_window = *property; + } else { + XSetWindowAttributes sAttributes; + + /* really, this should be done on a separate connection, + with XSetCloseDownMode (RetainPermanent), so that + others don't have to recreate it; hopefully, some real + Motif application will be around to do it */ + + sAttributes.override_redirect = True; + sAttributes.event_mask = PropertyChangeMask; + motif_window = XCreateWindow (display, + DefaultRootWindow (display), + -170, -560, 1, 1, 0, 0, + InputOnly, CopyFromParent, + (CWOverrideRedirect |CWEventMask), + &sAttributes); + XMapWindow (display, motif_window); + } + + if (property) { + XFree ((char *)property); + } + + return (motif_window); +} + + +static DndTargetsTable TargetsTable(Display *display) +{ + Atom type; + int format; + unsigned long size; + unsigned long bytes_after; + Window motif_window = MotifWindow(display) ; + DndTargets * target_prop; + DndTargetsTable targets_table ; + int i,j ; + char * target_data ; + + /* this version does no caching, so it's slow: round trip each time */ + /* ideally, register for property notify on this target_list + atom and update when necessary only */ + + if ((XGetWindowProperty (display, motif_window, + atom_target_list, 0L, 100000L, + False, atom_target_list, + &type, &format, &size, &bytes_after, + (unsigned char **) &target_prop) != Success) || + type == None) { + qWarning("QMotifDND: cannot get property on motif window"); + return 0; + } + + if (target_prop->protocol_version != DND_PROTOCOL_VERSION) { + qWarning("QMotifDND: protocol mismatch"); + } + + if (target_prop->byte_order != DndByteOrder()) { + /* need to swap num_target_lists and size */ + SWAP2BYTES(target_prop->num_target_lists); + SWAP4BYTES(target_prop->data_size); + } + + /* now parse DndTarget prop data in a TargetsTable */ + + targets_table = (DndTargetsTable)malloc(sizeof(DndTargetsTableRec)); + targets_table->num_entries = target_prop->num_target_lists ; + targets_table->entries = (DndTargetsTableEntry) + malloc(sizeof(DndTargetsTableEntryRec) * target_prop->num_target_lists); + + target_data = (char*)target_prop + sizeof(*target_prop) ; + + for (i = 0 ; i < targets_table->num_entries; i++) { + CARD16 num_targets ; + CARD32 atom ; + + memcpy(&num_targets, target_data, 2); + target_data += 2; + + /* potential swap needed here */ + if (target_prop->byte_order != DndByteOrder()) + SWAP2BYTES(num_targets); + + targets_table->entries[i].num_targets = num_targets ; + targets_table->entries[i].targets = (Atom *) + malloc(sizeof(Atom) * targets_table->entries[i].num_targets); + + + for (j = 0; j < num_targets; j++) { + memcpy(&atom, target_data, 4 ); + target_data += 4; + + /* another potential swap needed here */ + if (target_prop->byte_order != DndByteOrder()) + SWAP4BYTES(atom); + + targets_table->entries[i].targets[j] = (Atom) atom ; + } + } + + if (target_prop) { + XFree((char *)target_prop); + } + + return targets_table ; +} + + +static int _DndIndexToTargets(Display * display, + int index, + Atom ** targets) +{ + DndTargetsTable targets_table; + int i ; + + /* again, slow: no caching here, alloc/free each time */ + + if (!(targets_table = TargetsTable (display)) || + (index >= targets_table->num_entries)) { + return -1; + } + + /* transfer the correct target list index */ + *targets = (Atom*)malloc(sizeof(Atom)*targets_table-> + entries[index].num_targets); + memcpy((char*)*targets, + (char*)targets_table->entries[index].targets, + sizeof(Atom)*targets_table->entries[index].num_targets); + + /* free the target table and its guts */ + for (i=0 ; i < targets_table->num_entries; i++) + XFree((char*)targets_table->entries[i].targets); + + int tmp = targets_table->entries[index].num_targets; + XFree((char*)targets_table); + + return tmp; // targets_table->entries[index].num_targets; +} + + +const char *qt_motifdnd_format( int n ) +{ + if ( ! qt_motifdnd_active ) + return 0; // should not happen + + if ( n == 0 ) + return "text/plain"; + if ( n == 1 ) + return "text/uri-list"; + n -= 2; + + if ( n >= num_src_targets ) + return 0; + + Atom target = src_targets[n]; + + // duplicated from qclipboard_x11.cpp - not the best solution + static Atom xa_utf8_string = *qt_xdnd_str_to_atom( "UTF8_STRING" ); + static Atom xa_text = *qt_xdnd_str_to_atom( "TEXT" ); + static Atom xa_compound_text = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + + if ( target == XA_STRING ) + return "text/plain;charset=ISO-8859-1"; + if ( target == xa_utf8_string ) + return "text/plain;charset=UTF-8"; + if ( target == xa_text || + target == xa_compound_text ) + return "text/plain"; + + return qt_xdnd_atom_to_str( target ); +} + + +QByteArray qt_motifdnd_obtain_data( const char *mimeType ) +{ + QByteArray result; + + // try to convert the selection to the requested property + // qDebug( "trying to convert to '%s'", mimeType ); + + int n=0; + const char* f; + do { + f = qt_motifdnd_format( n ); + if ( !f ) + return result; + n++; + } while( qstricmp( mimeType, f ) ); + + // found one + Atom conversion_type; + + if ( qstrnicmp( f, "text/", 5 ) == 0 ) { + // always convert text to XA_STRING for compatibility with + // prior Qt versions + conversion_type = XA_STRING; + } else { + conversion_type = *qt_xdnd_str_to_atom( f ); + // qDebug( "found format '%s' 0x%lx '%s'", f, conversion_type, + // qt_xdnd_atom_to_str( conversion_type ) ); + } + + if ( XGetSelectionOwner( qt_xdisplay(), + Dnd_selection ) == None ) { + return result; // should never happen? + } + + QWidget* tw = drop_widget; + if ( drop_widget->isDesktop() ) { + tw = new QWidget; + } + + // convert selection to the appropriate type + XConvertSelection (qt_xdisplay(), Dnd_selection, conversion_type, + Dnd_selection, tw->winId(), Dnd_selection_time); + + XFlush( qt_xdisplay() ); + + XEvent xevent; + bool got=qt_xclb_wait_for_event( qt_xdisplay(), + tw->winId(), + SelectionNotify, &xevent, 5000); + if ( got ) { + Atom type; + + if ( qt_xclb_read_property( qt_xdisplay(), + tw->winId(), + Dnd_selection, TRUE, + &result, 0, &type, 0, TRUE ) ) { + } + } + + // we have to convert selection in order to indicate success to the initiator + XConvertSelection (qt_xdisplay(), Dnd_selection, Dnd_transfer_success, + Dnd_selection, tw->winId(), Dnd_selection_time); + + // wait again for SelectionNotify event + qt_xclb_wait_for_event( qt_xdisplay(), + tw->winId(), + SelectionNotify, &xevent, 5000); + + if ( drop_widget->isDesktop() ) { + delete tw; + } + + return result; +} + + +void qt_motifdnd_enable( QWidget *widget, bool ) +{ + DndWriteReceiverProperty( widget->x11Display(), widget->winId(), + DND_DRAG_DYNAMIC); +} + + +void qt_motifdnd_handle_msg( QWidget * /* w */ , const XEvent * xe, bool /* passive */ ) +{ + + XEvent event = *xe; + XClientMessageEvent cm ; + DndData dnd_data ; + char receiver ; + + if (!(DndParseClientMessage ((XClientMessageEvent*)&event, + &dnd_data, &receiver))) { + return; + } + + switch ( dnd_data.reason ) { + + case DND_DRAG_MOTION: + + { + /* check if in drop site, and depending on the state, + send a drop site enter or drop site leave or echo */ + + QPoint p( dnd_data.x, dnd_data.y ); + QWidget *c = QApplication::widgetAt( p, TRUE ); + if (c) + p = c->mapFromGlobal(p); + + while ( c && !c->acceptDrops() && !c->isTopLevel() ) { + p = c->mapToParent( p ); + c = c->parentWidget(); + } + + QDragMoveEvent me( p ); + QDropEvent::Action accepted_action = QDropEvent::Copy; + me.setAction(accepted_action); + + if ( c != 0L && c->acceptDrops() ) { + + if ( drop_widget != 0L && drop_widget->acceptDrops() && + drop_widget != c ) { + QDragLeaveEvent e; + QApplication::sendEvent( drop_widget, &e ); + QDragEnterEvent de( p ); + QApplication::sendEvent( c, &de ); + } + + drop_widget = c; + + if (!in_drop_site) { + in_drop_site = True ; + + dnd_data.reason = DND_DROP_SITE_ENTER ; + dnd_data.time = CurrentTime ; + dnd_data.operation = DND_MOVE|DND_COPY; + dnd_data.operations = DND_MOVE|DND_COPY; + + DndFillClientMessage (event.xclient.display, + cur_window, + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + QDragEnterEvent de( p ); + QApplication::sendEvent( drop_widget, &de ); + if ( de.isAccepted() ) { + me.accept( de.answerRect() ); + } else { + me.ignore( de.answerRect() ); + } + + } else { + dnd_data.reason = DND_DRAG_MOTION ; + dnd_data.time = CurrentTime ; + dnd_data.operation = DND_MOVE|DND_COPY; + dnd_data.operations = DND_MOVE|DND_COPY; + + DndFillClientMessage (event.xclient.display, + cur_window, + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + QApplication::sendEvent( drop_widget, &me ); + } + } else { + if (in_drop_site) { + in_drop_site = False ; + + dnd_data.reason = DND_DROP_SITE_LEAVE ; + dnd_data.time = CurrentTime ; + + DndFillClientMessage (event.xclient.display, + cur_window, + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + QDragLeaveEvent e; + QApplication::sendEvent( drop_widget, &e ); + } + } + } + break; + + case DND_TOP_LEVEL_ENTER: + + /* get the size of our drop site for later use */ + + cur_window = dnd_data.src_window ; + qt_motifdnd_active = TRUE; + + /* no answer needed, just read source property */ + DndReadSourceProperty (event.xclient.display, + cur_window, + dnd_data.property, + &src_targets, &num_src_targets); + break; + + case DND_TOP_LEVEL_LEAVE: + /* no need to do anything */ + break; + + case DND_OPERATION_CHANGED: + /* need to echo */ + break; + + case DND_DROP_START: + if (!in_drop_site) { + // we have to convert selection in order to indicate failure to the initiator + XConvertSelection (qt_xdisplay(), dnd_data.property, Dnd_transfer_failure, + dnd_data.property, cur_window, dnd_data.time); + QDragLeaveEvent e; + QApplication::sendEvent( drop_widget, &e ); + drop_widget = 0; + return; + } + + /* need to echo and then request a convert */ + dnd_data.reason = DND_DROP_START ; + + DndFillClientMessage (event.xclient.display, + drop_widget->winId(), + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + // store selection and its time + Dnd_selection = dnd_data.property; + Dnd_selection_time = dnd_data.time; + + QPoint p( dnd_data.x, dnd_data.y ); + QDropEvent de( drop_widget->mapFromGlobal(p) ); + de.setAction( QDropEvent::Copy ); + QApplication::sendEvent( drop_widget, &de ); + + if (in_drop_site) + in_drop_site = False ; + + drop_widget = 0; + cur_window = 0; + break; + } // end of switch ( dnd_data.reason ) +} + +#endif // QT_NO_DRAGANDDROP diff --git a/src/kernel/qmovie.cpp b/src/kernel/qmovie.cpp new file mode 100644 index 0000000..3379fba --- /dev/null +++ b/src/kernel/qmovie.cpp @@ -0,0 +1,1081 @@ +/**************************************************************************** +** +** Implementation of movie classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +// #define QT_SAVE_MOVIE_HACK + +#include "qtimer.h" +#include "qpainter.h" +#include "qptrlist.h" +#include "qbitmap.h" +#include "qmovie.h" +#include "qfile.h" +#include "qbuffer.h" +#include "qobject.h" +#include "qpixmapcache.h" + +#ifndef QT_NO_MOVIE + +#ifdef Q_WS_QWS +#include "qgfx_qws.h" +#endif + +#include "qasyncio.h" +#include "qasyncimageio.h" + +#include <stdlib.h> + +/*! + \class QMovie qmovie.h + \brief The QMovie class provides incremental loading of animations or images, signalling as it progresses. + + \ingroup images + \ingroup graphics + \ingroup multimedia + \mainclass + + The simplest way to display a QMovie is to use a QLabel and + QLabel::setMovie(). + + A QMovie provides a QPixmap as the framePixmap(); connections can + be made via connectResize() and connectUpdate() to receive + notification of size and pixmap changes. All decoding is driven + by the normal event-processing mechanisms. + + The movie begins playing as soon as the QMovie is created + (actually, once control returns to the event loop). When the last + frame in the movie has been played, it may loop back to the start + if such looping is defined in the input source. + + QMovie objects are explicitly shared. This means that a QMovie + copied from another QMovie will be displaying the same frame at + all times. If one shared movie pauses, all pause. To make \e + independent movies, they must be constructed separately. + + The set of data formats supported by QMovie is determined by the + decoder factories that have been installed; the format of the + input is determined as the input is decoded. + + The supported formats are MNG (if Qt is configured with MNG + support enabled) and GIF (if Qt is configured with GIF support + enabled, see qgif.h). + + If Qt is configured to support GIF reading, we are required to + state that "The Graphics Interchange Format(c) is the Copyright + property of CompuServe Incorporated. GIF(sm) is a Service Mark + property of CompuServe Incorporated. + + \warning If you are in a country that recognizes software patents + and in which Unisys holds a patent on LZW compression and/or + decompression and you want to use GIF, Unisys may require you to + license that technology. Such countries include Canada, Japan, + the USA, France, Germany, Italy and the UK. + + GIF support may be removed completely in a future version of Qt. + We recommend using the MNG or PNG format. + + \img qmovie.png QMovie + + \sa QLabel::setMovie() +*/ + +/*! + \enum QMovie::Status + + \value SourceEmpty + \value UnrecognizedFormat + \value Paused + \value EndOfFrame + \value EndOfLoop + \value EndOfMovie + \value SpeedChanged +*/ + +class QMoviePrivate : public QObject, public QShared, + private QDataSink, private QImageConsumer +{ + Q_OBJECT + +public: // for QMovie + + // Creates a null Private + QMoviePrivate(); + + // NOTE: The ownership of the QDataSource is transferred to the Private + QMoviePrivate(QDataSource* src, QMovie* movie, int bufsize); + + virtual ~QMoviePrivate(); + + bool isNull() const; + + // Initialize, possibly to the null state + void init(bool fully); + void flushBuffer(); + void updatePixmapFromImage(); + void updatePixmapFromImage(const QPoint& off, const QRect& area); + void showChanges(); + + // This as QImageConsumer + void changed(const QRect& rect); + void end(); + void preFrameDone(); //util func + void frameDone(); + void frameDone(const QPoint&, const QRect& rect); + void restartTimer(); + void setLooping(int l); + void setFramePeriod(int milliseconds); + void setSize(int w, int h); + + // This as QDataSink + int readyToReceive(); + void receive(const uchar* b, int bytecount); + void eof(); + void pause(); + +signals: + void sizeChanged(const QSize&); + void areaChanged(const QRect&); + void dataStatus(int); + +public slots: + void refresh(); + +public: + QMovie *that; + QWidget * display_widget; + + QImageDecoder *decoder; + + // Cyclic buffer + int buf_size; + uchar *buffer; + int buf_r, buf_w, buf_usage; + + int framenumber; + int frameperiod; + int speed; + QTimer *frametimer; + int lasttimerinterval; + int loop; + bool movie_ended; + bool dirty_cache; + bool waitingForFrameTick; + int stepping; + QRect changed_area; + QRect valid_area; + QDataPump *pump; + QDataSource *source; + QPixmap mypixmap; + QBitmap mymask; + QColor bg; + + int error; + bool empty; + +#ifdef QT_SAVE_MOVIE_HACK + bool save_image; + int image_number; +#endif +}; + + +QMoviePrivate::QMoviePrivate() +{ + dirty_cache = FALSE; + buffer = 0; + pump = 0; + source = 0; + decoder = 0; + display_widget=0; + buf_size = 0; + init(FALSE); +} + +// NOTE: The ownership of the QDataSource is transferred to the Private +QMoviePrivate::QMoviePrivate(QDataSource* src, QMovie* movie, int bufsize) : + that(movie), + buf_size(bufsize) +{ + frametimer = new QTimer(this); + pump = src ? new QDataPump(src, this) : 0; + QObject::connect(frametimer, SIGNAL(timeout()), this, SLOT(refresh())); + dirty_cache = FALSE; + source = src; + buffer = 0; + decoder = 0; + speed = 100; + display_widget=0; + init(TRUE); +} + +QMoviePrivate::~QMoviePrivate() +{ + if ( buffer ) // Avoid purify complaint + delete [] buffer; + delete pump; + delete decoder; + delete source; + + // Too bad.. but better be safe than sorry + if ( dirty_cache ) + QPixmapCache::clear(); +} + +bool QMoviePrivate::isNull() const +{ + return !buf_size; +} + +// Initialize. Only actually allocate any space if \a fully is TRUE, +// otherwise, just enough to be a valid null Private. +void QMoviePrivate::init(bool fully) +{ +#ifdef QT_SAVE_MOVIE_HACK + save_image = TRUE; + image_number = 0; +#endif + + buf_usage = buf_r = buf_w = 0; + if ( buffer ) // Avoid purify complaint + delete [] buffer; + buffer = fully ? new uchar[buf_size] : 0; + if ( buffer ) + memset( buffer, 0, buf_size ); + + delete decoder; + decoder = fully ? new QImageDecoder(this) : 0; + +#ifdef AVOID_OPEN_FDS + if ( source && !source->isOpen() ) + source->open(IO_ReadOnly); +#endif + + waitingForFrameTick = FALSE; + stepping = -1; + framenumber = 0; + frameperiod = -1; + if (fully) frametimer->stop(); + lasttimerinterval = -1; + changed_area.setRect(0,0,-1,-1); + valid_area = changed_area; + loop = -1; + movie_ended = FALSE; + error = 0; + empty = TRUE; +} + +void QMoviePrivate::flushBuffer() +{ + int used; + while (buf_usage && !waitingForFrameTick && stepping != 0 && !error) { + used = decoder->decode(buffer + buf_r, QMIN(buf_usage, buf_size - buf_r)); + if (used <= 0) { + if ( used < 0 ) { + error = 1; + emit dataStatus(QMovie::UnrecognizedFormat); + } + break; + } + buf_r = (buf_r + used) % buf_size; + buf_usage -= used; + } + + // Some formats, like MNG, can make stuff happen without any extra data. + // Only do this if the movie hasn't ended, however or we'll never get the end of loop signal. + if (!movie_ended) { + used = decoder->decode(buffer + buf_r, 0); + if (used <= 0) { + if ( used < 0 ) { + error = 1; + emit dataStatus(QMovie::UnrecognizedFormat); + } + } + } + + if (error) + frametimer->stop(); + maybeReady(); +} + +void QMoviePrivate::updatePixmapFromImage() +{ + if (changed_area.isEmpty()) return; + updatePixmapFromImage(QPoint(0,0),changed_area); +} + +void QMoviePrivate::updatePixmapFromImage(const QPoint& off, + const QRect& area) +{ + // Create temporary QImage to hold the part we want + const QImage& gimg = decoder->image(); + QImage img = gimg.copy(area); + +#ifdef QT_SAVE_MOVIE_HACK + if ( save_image ) { + QString name; + name.sprintf("movie%i.ppm",image_number++); + gimg.save( name, "PPM" ); + } +#endif + + // Resize to size of image + if (mypixmap.width() != gimg.width() || mypixmap.height() != gimg.height()) + mypixmap.resize(gimg.width(), gimg.height()); + + // Convert to pixmap and paste that onto myself + QPixmap lines; + +#ifndef QT_NO_SPRINTF + if (!(frameperiod < 0 && loop == -1)) { + // its an animation, lets see if we converted + // this frame already. + QString key; + key.sprintf( "%08lx:%04d", ( long )this, framenumber ); + if ( !QPixmapCache::find( key, lines ) ) { + lines.convertFromImage(img, Qt::ColorOnly); + QPixmapCache::insert( key, lines ); + dirty_cache = TRUE; + } + } else +#endif + { + lines.convertFromImage(img, Qt::ColorOnly); + } + + if (bg.isValid()) { + QPainter p; + p.begin(&mypixmap); + p.fillRect(area, bg); + p.drawPixmap(area, lines); + p.end(); + } else { + if (gimg.hasAlphaBuffer()) { + // Resize to size of image + if (mymask.isNull()) { + mymask.resize(gimg.width(), gimg.height()); + mymask.fill( Qt::color1 ); + } + } + mypixmap.setMask(QBitmap()); // Remove reference to my mask + copyBlt( &mypixmap, area.left(), area.top(), + &lines, off.x(), off.y(), area.width(), area.height() ); + } + +#ifdef Q_WS_QWS + if(display_widget) { + QGfx * mygfx=display_widget->graphicsContext(); + if(mygfx) { + double xscale,yscale; + xscale=display_widget->width(); + yscale=display_widget->height(); + xscale=xscale/((double)mypixmap.width()); + yscale=yscale/((double)mypixmap.height()); + double xh,yh; + xh=xscale*((double)area.left()); + yh=yscale*((double)area.top()); + mygfx->setSource(&mypixmap); + mygfx->setAlphaType(QGfx::IgnoreAlpha); + mygfx->stretchBlt(0,0,display_widget->width(), + display_widget->height(),mypixmap.width(), + mypixmap.height()); + delete mygfx; + } + } +#endif +} + +void QMoviePrivate::showChanges() +{ + if (changed_area.isValid()) { + updatePixmapFromImage(); + + valid_area = valid_area.unite(changed_area); + emit areaChanged(changed_area); + + changed_area.setWidth(-1); // make empty + } +} + +// Private as QImageConsumer +void QMoviePrivate::changed(const QRect& rect) +{ + if (!frametimer->isActive()) + frametimer->start(0); + changed_area = changed_area.unite(rect); +} + +void QMoviePrivate::end() +{ + movie_ended = TRUE; +} + +void QMoviePrivate::preFrameDone() +{ + if (stepping > 0) { + stepping--; + if (!stepping) { + frametimer->stop(); + emit dataStatus( QMovie::Paused ); + } + } else { + waitingForFrameTick = TRUE; + restartTimer(); + } +} +void QMoviePrivate::frameDone() +{ + preFrameDone(); + showChanges(); + emit dataStatus(QMovie::EndOfFrame); + framenumber++; +} +void QMoviePrivate::frameDone(const QPoint& p, + const QRect& rect) +{ + preFrameDone(); + const QImage& gimg = decoder->image(); + QPoint point = p - gimg.offset(); + if (framenumber==0) + emit sizeChanged(gimg.size()); + valid_area = valid_area.unite(QRect(point,rect.size())); + updatePixmapFromImage(point,rect); + emit areaChanged(QRect(point,rect.size())); + emit dataStatus(QMovie::EndOfFrame); + framenumber++; +} + +void QMoviePrivate::restartTimer() +{ + if (speed > 0) { + int i = frameperiod >= 0 ? frameperiod * 100/speed : 0; + if ( i != lasttimerinterval || !frametimer->isActive() ) { + lasttimerinterval = i; + frametimer->start( i ); + } + } else { + frametimer->stop(); + } +} + +void QMoviePrivate::setLooping(int nloops) +{ + if (loop == -1) { // Only if we don't already know how many loops! + if (source && source->rewindable()) { + source->enableRewind(TRUE); + loop = nloops; + } else { + // Cannot loop from this source + loop = -2; + } + } +} + +void QMoviePrivate::setFramePeriod(int milliseconds) +{ + // Animation: only show complete frame + frameperiod = milliseconds; + if (stepping<0 && frameperiod >= 0) restartTimer(); +} + +void QMoviePrivate::setSize(int w, int h) +{ + if (mypixmap.width() != w || mypixmap.height() != h) { + mypixmap.resize(w, h); + emit sizeChanged(QSize(w, h)); + } +} + + +// Private as QDataSink + +int QMoviePrivate::readyToReceive() +{ + // Could pre-fill buffer, but more efficient to just leave the + // data back at the source. + return (waitingForFrameTick || !stepping || buf_usage || error) + ? 0 : buf_size; +} + +void QMoviePrivate::receive(const uchar* b, int bytecount) +{ + if ( bytecount ) empty = FALSE; + + while (bytecount && !waitingForFrameTick && stepping != 0) { + int used = decoder->decode(b, bytecount); + if (used<=0) { + if ( used < 0 ) { + error = 1; + emit dataStatus(QMovie::UnrecognizedFormat); + } + break; + } + b+=used; + bytecount-=used; + } + + // Append unused to buffer + while (bytecount--) { + buffer[buf_w] = *b++; + buf_w = (buf_w+1)%buf_size; + buf_usage++; + } +} + +void QMoviePrivate::eof() +{ + if ( !movie_ended ) + return; + + if ( empty ) + emit dataStatus(QMovie::SourceEmpty); + +#ifdef QT_SAVE_MOVIE_HACK + save_image = FALSE; +#endif + + emit dataStatus(QMovie::EndOfLoop); + + if (loop >= 0) { + if (loop) { + loop--; + if (!loop) return; + } + delete decoder; + decoder = new QImageDecoder(this); + source->rewind(); + framenumber = 0; + movie_ended = FALSE; + } else { + delete decoder; + decoder = 0; + if ( buffer ) // Avoid purify complaint + delete [] buffer; + buffer = 0; + emit dataStatus(QMovie::EndOfMovie); +#ifdef AVOID_OPEN_FDS + if ( source ) + source->close(); +#endif + } +} + +void QMoviePrivate::pause() +{ + if ( stepping ) { + stepping = 0; + frametimer->stop(); + emit dataStatus( QMovie::Paused ); + } +} + +void QMoviePrivate::refresh() +{ + if (!decoder) { + frametimer->stop(); + return; + } + + if (frameperiod < 0 && loop == -1) { + // Only show changes if probably not an animation + showChanges(); + } + + if (!buf_usage) { + frametimer->stop(); + } + + waitingForFrameTick = FALSE; + flushBuffer(); +} + +///////////////// End of Private ///////////////// + + + + + +/*! + Constructs a null QMovie. The only interesting thing to do with + such a movie is to assign another movie to it. + + \sa isNull() +*/ +QMovie::QMovie() +{ + d = new QMoviePrivate(); +} + +/*! + Constructs a QMovie with an external data source. You should later + call pushData() to send incoming animation data to the movie. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. + + \sa pushData() +*/ +QMovie::QMovie(int bufsize) +{ + d = new QMoviePrivate(0, this, bufsize); +} + +/*! + Returns the maximum amount of data that can currently be pushed + into the movie by a call to pushData(). This is affected by the + initial buffer size, but varies as the movie plays and data is + consumed. +*/ +int QMovie::pushSpace() const +{ + return d->readyToReceive(); +} + +/*! + Pushes \a length bytes from \a data into the movie. \a length must + be no more than the amount returned by pushSpace() since the + previous call to pushData(). +*/ +void QMovie::pushData(const uchar* data, int length) +{ + d->receive(data,length); +} + +#ifdef Q_WS_QWS // ##### Temporary performance experiment +/*! + \internal +*/ +void QMovie::setDisplayWidget(QWidget * w) +{ + d->display_widget=w; +} +#endif + +/*! + Constructs a QMovie that reads an image sequence from the given + data source, \a src. The source must be allocated dynamically, + because QMovie will take ownership of it and will destroy it when + the movie is destroyed. The movie starts playing as soon as event + processing continues. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. +*/ +QMovie::QMovie(QDataSource* src, int bufsize) +{ + d = new QMoviePrivate(src, this, bufsize); +} + +/*! + Constructs a QMovie that reads an image sequence from the file, \a + fileName. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. +*/ +QMovie::QMovie(const QString &fileName, int bufsize) +{ + QFile* file = new QFile(fileName); + if ( !fileName.isEmpty() ) + file->open(IO_ReadOnly); + d = new QMoviePrivate(new QIODeviceSource(file, bufsize), this, bufsize); +} + +/*! + Constructs a QMovie that reads an image sequence from the byte + array, \a data. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. +*/ +QMovie::QMovie(QByteArray data, int bufsize) +{ + QBuffer* buffer = new QBuffer(data); + buffer->open(IO_ReadOnly); + d = new QMoviePrivate(new QIODeviceSource(buffer, bufsize), this, bufsize); +} + +/*! + Constructs a movie that uses the same data as movie \a movie. + QMovies use explicit sharing, so operations on the copy will + affect both. +*/ +QMovie::QMovie(const QMovie& movie) +{ + d = movie.d; + d->ref(); +} + +/*! + Destroys the QMovie. If this is the last reference to the data of + the movie, the data is deallocated. +*/ +QMovie::~QMovie() +{ + if (d->deref()) delete d; +} + +/*! + Returns TRUE if the movie is null; otherwise returns FALSE. +*/ +bool QMovie::isNull() const +{ + return d->isNull(); +} + +/*! + Makes this movie use the same data as movie \a movie. QMovies use + explicit sharing. +*/ +QMovie& QMovie::operator=(const QMovie& movie) +{ + movie.d->ref(); + if (d->deref()) delete d; + d = movie.d; + return *this; +} + + +/*! + Sets the background color of the pixmap to \a c. If the background + color isValid(), the pixmap will never have a mask because the + background color will be used in transparent regions of the image. + + \sa backgroundColor() +*/ +void QMovie::setBackgroundColor(const QColor& c) +{ + d->bg = c; +} + +/*! + Returns the background color of the movie set by + setBackgroundColor(). +*/ +const QColor& QMovie::backgroundColor() const +{ + return d->bg; +} + +/*! + Returns the area of the pixmap for which pixels have been + generated. +*/ +const QRect& QMovie::getValidRect() const +{ + return d->valid_area; +} + +/*! + Returns the current frame of the movie, as a QPixmap. It is not + generally useful to keep a copy of this pixmap. It is better to + keep a copy of the QMovie and get the framePixmap() only when + needed for drawing. + + \sa frameImage() +*/ +const QPixmap& QMovie::framePixmap() const +{ + return d->mypixmap; +} + +/*! + Returns the current frame of the movie, as a QImage. It is not + generally useful to keep a copy of this image. Also note that you + must not call this function if the movie is finished(), since by + then the image will not be available. + + \sa framePixmap() +*/ +const QImage& QMovie::frameImage() const +{ + return d->decoder->image(); +} + +/*! + Returns the number of steps remaining after a call to step(). If + the movie is paused, steps() returns 0. If it's running normally + or is finished, steps() returns a negative number. +*/ +int QMovie::steps() const +{ + return d->stepping; +} + +/*! + Returns the number of times EndOfFrame has been emitted since the + start of the current loop of the movie. Thus, before any + EndOfFrame has been emitted the value will be 0; within slots + processing the first signal, frameNumber() will be 1, and so on. +*/ +int QMovie::frameNumber() const { return d->framenumber; } + +/*! + Returns TRUE if the image is paused; otherwise returns FALSE. +*/ +bool QMovie::paused() const +{ + return d->stepping == 0; +} + +/*! + Returns TRUE if the image is no longer playing: this happens when + all loops of all frames are complete; otherwise returns FALSE. +*/ +bool QMovie::finished() const +{ + return !d->decoder; +} + +/*! + Returns TRUE if the image is not single-stepping, not paused, and + not finished; otherwise returns FALSE. +*/ +bool QMovie::running() const +{ + return d->stepping<0 && d->decoder; +} + +/*! + Pauses the progress of the animation. + + \sa unpause() +*/ +void QMovie::pause() +{ + d->pause(); +} + +/*! + Unpauses the progress of the animation. + + \sa pause() +*/ +void QMovie::unpause() +{ + if ( d->stepping >= 0 ) { + if (d->isNull()) + return; + d->stepping = -1; + d->restartTimer(); + } +} + +/*! + \overload + + Steps forward, showing \a steps frames, and then pauses. +*/ +void QMovie::step(int steps) +{ + if (d->isNull()) + return; + d->stepping = steps; + d->frametimer->start(0); + d->waitingForFrameTick = FALSE; // Full speed ahead! +} + +/*! + Steps forward 1 frame and then pauses. +*/ +void QMovie::step() +{ + step(1); +} + +/*! + Rewinds the movie to the beginning. If the movie has not been + paused, it begins playing again. +*/ +void QMovie::restart() +{ + if (d->isNull()) + return; + if (d->source->rewindable()) { + d->source->enableRewind(TRUE); + d->source->rewind(); + int s = d->stepping; + d->init(TRUE); + if ( s>0 ) + step(s); + else if ( s==0 ) + pause(); + } +} + +/*! + Returns the movie's play speed as a percentage. The default is 100 + percent. + + \sa setSpeed() +*/ +int QMovie::speed() const +{ + return d->speed; +} + +/*! + Sets the movie's play speed as a percentage, to \a percent. This + is a percentage of the speed dictated by the input data format. + The default is 100 percent. +*/ +void QMovie::setSpeed(int percent) +{ + int oldspeed = d->speed; + if ( oldspeed != percent && percent >= 0 ) { + d->speed = percent; + // Restart timer only if really needed + if (d->stepping < 0) { + if ( !percent || !oldspeed // To or from zero + || oldspeed*4 / percent > 4 // More than 20% slower + || percent*4 / oldspeed > 4 // More than 20% faster + ) + d->restartTimer(); + } + } +} + +/*! + Connects the \a{receiver}'s \a member of type \c{void member(const + QSize&)} so that it is signalled when the movie changes size. + + Note that due to the explicit sharing of QMovie objects, these + connections persist until they are explicitly disconnected with + disconnectResize() or until \e every shared copy of the movie is + deleted. +*/ +void QMovie::connectResize(QObject* receiver, const char *member) +{ + QObject::connect(d, SIGNAL(sizeChanged(const QSize&)), receiver, member); +} + +/*! + Disconnects the \a{receiver}'s \a member (or all members if \a + member is zero) that were previously connected by connectResize(). +*/ +void QMovie::disconnectResize(QObject* receiver, const char *member) +{ + QObject::disconnect(d, SIGNAL(sizeChanged(const QSize&)), receiver, member); +} + +/*! + Connects the \a{receiver}'s \a member of type \c{void member(const + QRect&)} so that it is signalled when an area of the framePixmap() + has changed since the previous frame. + + Note that due to the explicit sharing of QMovie objects, these + connections persist until they are explicitly disconnected with + disconnectUpdate() or until \e every shared copy of the movie is + deleted. +*/ +void QMovie::connectUpdate(QObject* receiver, const char *member) +{ + QObject::connect(d, SIGNAL(areaChanged(const QRect&)), receiver, member); +} + +/*! + Disconnects the \a{receiver}'s \a member (or all members if \q + member is zero) that were previously connected by connectUpdate(). +*/ +void QMovie::disconnectUpdate(QObject* receiver, const char *member) +{ + QObject::disconnect(d, SIGNAL(areaChanged(const QRect&)), receiver, member); +} + +/*! + Connects the \a{receiver}'s \a member, of type \c{void + member(int)} so that it is signalled when the movie changes + status. The status codes are negative for errors and positive for + information. + + \table + \header \i Status Code \i Meaning + \row \i QMovie::SourceEmpty + \i signalled if the input cannot be read. + \row \i QMovie::UnrecognizedFormat + \i signalled if the input data is unrecognized. + \row \i QMovie::Paused + \i signalled when the movie is paused by a call to paused() + or by after \link step() stepping \endlink pauses. + \row \i QMovie::EndOfFrame + \i signalled at end-of-frame after any update and Paused signals. + \row \i QMovie::EndOfLoop + \i signalled at end-of-loop, after any update signals, + EndOfFrame - but before EndOfMovie. + \row \i QMovie::EndOfMovie + \i signalled when the movie completes and is not about to loop. + \endtable + + More status messages may be added in the future, so a general test + for errors would test for negative. + + Note that due to the explicit sharing of QMovie objects, these + connections persist until they are explicitly disconnected with + disconnectStatus() or until \e every shared copy of the movie is + deleted. +*/ +void QMovie::connectStatus(QObject* receiver, const char *member) +{ + QObject::connect(d, SIGNAL(dataStatus(int)), receiver, member); +} + +/*! + Disconnects the \a{receiver}'s \a member (or all members if \a + member is zero) that were previously connected by connectStatus(). +*/ +void QMovie::disconnectStatus(QObject* receiver, const char *member) +{ + QObject::disconnect(d, SIGNAL(dataStatus(int)), receiver, member); +} + + +#include "qmovie.moc" + +#endif // QT_NO_MOVIE diff --git a/src/kernel/qmovie.h b/src/kernel/qmovie.h new file mode 100644 index 0000000..c77fd00 --- /dev/null +++ b/src/kernel/qmovie.h @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Definition of movie classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QMOVIE_H +#define QMOVIE_H + +#ifndef QT_H +#include "qpixmap.h" // ### remove or keep for users' convenience? +#endif // QT_H + +#ifndef QT_NO_MOVIE + +class QDataSource; +class QObject; +class QMoviePrivate; + +class Q_EXPORT QMovie { +public: + QMovie(); + QMovie(int bufsize); + QMovie(QDataSource*, int bufsize=1024); + QMovie(const QString &fileName, int bufsize=1024); + QMovie(QByteArray data, int bufsize=1024); + QMovie(const QMovie&); + ~QMovie(); + + QMovie& operator=(const QMovie&); + + int pushSpace() const; + void pushData(const uchar* data, int length); + + const QColor& backgroundColor() const; + void setBackgroundColor(const QColor&); + + const QRect& getValidRect() const; + const QPixmap& framePixmap() const; + const QImage& frameImage() const; + + bool isNull() const; + + int frameNumber() const; + int steps() const; + bool paused() const; + bool finished() const; + bool running() const; + + void unpause(); + void pause(); + void step(); + void step(int); + void restart(); + + int speed() const; + void setSpeed(int); + + void connectResize(QObject* receiver, const char *member); + void disconnectResize(QObject* receiver, const char *member=0); + + void connectUpdate(QObject* receiver, const char *member); + void disconnectUpdate(QObject* receiver, const char *member=0); + +#ifdef Q_WS_QWS + // Temporary hack + void setDisplayWidget(QWidget * w); +#endif + + enum Status { SourceEmpty=-2, + UnrecognizedFormat=-1, + Paused=1, + EndOfFrame=2, + EndOfLoop=3, + EndOfMovie=4, + SpeedChanged=5 }; + void connectStatus(QObject* receiver, const char *member); + void disconnectStatus(QObject* receiver, const char *member=0); + +private: + QMoviePrivate *d; +}; + +#endif // QT_NO_MOVIE + +#endif diff --git a/src/kernel/qnamespace.h b/src/kernel/qnamespace.h new file mode 100644 index 0000000..1598785 --- /dev/null +++ b/src/kernel/qnamespace.h @@ -0,0 +1,1008 @@ +/**************************************************************************** +** +** Definition of Qt namespace (as class for compiler compatibility) +** +** Created : 980927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QNAMESPACE_H +#define QNAMESPACE_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + + +class QColor; +class QCursor; + + +class Q_EXPORT Qt { +public: + QT_STATIC_CONST QColor & color0; + QT_STATIC_CONST QColor & color1; + QT_STATIC_CONST QColor & black; + QT_STATIC_CONST QColor & white; + QT_STATIC_CONST QColor & darkGray; + QT_STATIC_CONST QColor & gray; + QT_STATIC_CONST QColor & lightGray; + QT_STATIC_CONST QColor & red; + QT_STATIC_CONST QColor & green; + QT_STATIC_CONST QColor & blue; + QT_STATIC_CONST QColor & cyan; + QT_STATIC_CONST QColor & magenta; + QT_STATIC_CONST QColor & yellow; + QT_STATIC_CONST QColor & darkRed; + QT_STATIC_CONST QColor & darkGreen; + QT_STATIC_CONST QColor & darkBlue; + QT_STATIC_CONST QColor & darkCyan; + QT_STATIC_CONST QColor & darkMagenta; + QT_STATIC_CONST QColor & darkYellow; + + // documented in qevent.cpp + enum ButtonState { // mouse/keyboard state values + NoButton = 0x0000, + LeftButton = 0x0001, + RightButton = 0x0002, + MidButton = 0x0004, + MouseButtonMask = 0x0007, + ShiftButton = 0x0100, + ControlButton = 0x0200, + AltButton = 0x0400, + MetaButton = 0x0800, + KeyButtonMask = 0x0f00, + Keypad = 0x4000 + }; + + // documented in qobject.cpp + // ideally would start at 1, as in QSizePolicy, but that breaks other things + enum Orientation { + Horizontal = 0, + Vertical + }; + + // documented in qlistview.cpp + enum SortOrder { + Ascending, + Descending + }; + + // Text formatting flags for QPainter::drawText and QLabel + // the following four enums can be combined to one integer which + // is passed as textflag to drawText and qt_format_text. + + // documented in qpainter.cpp + enum AlignmentFlags { + AlignAuto = 0x0000, // text alignment + AlignLeft = 0x0001, + AlignRight = 0x0002, + AlignHCenter = 0x0004, + AlignJustify = 0x0008, + AlignHorizontal_Mask = AlignLeft | AlignRight | AlignHCenter | AlignJustify, + AlignTop = 0x0010, + AlignBottom = 0x0020, + AlignVCenter = 0x0040, + AlignVertical_Mask = AlignTop | AlignBottom | AlignVCenter, + AlignCenter = AlignVCenter | AlignHCenter + }; + + // documented in qpainter.cpp + enum TextFlags { + SingleLine = 0x0080, // misc. flags + DontClip = 0x0100, + ExpandTabs = 0x0200, + ShowPrefix = 0x0400, + WordBreak = 0x0800, + BreakAnywhere = 0x1000, +#ifndef Q_QDOC + DontPrint = 0x2000, + Underline = 0x01000000, + Overline = 0x02000000, + StrikeOut = 0x04000000, + IncludeTrailingSpaces = 0x08000000, +#endif + NoAccel = 0x4000 + }; + + // Widget flags; documented in qwidget.cpp + typedef uint WState; + + // QWidget state flags (internal, barely documented in qwidget.cpp) + enum WidgetState { + WState_Created = 0x00000001, + WState_Disabled = 0x00000002, + WState_Visible = 0x00000004, + WState_ForceHide = 0x00000008, + WState_OwnCursor = 0x00000010, + WState_MouseTracking = 0x00000020, + WState_CompressKeys = 0x00000040, + WState_BlockUpdates = 0x00000080, + WState_InPaintEvent = 0x00000100, + WState_Reparented = 0x00000200, + WState_ConfigPending = 0x00000400, + WState_Resized = 0x00000800, + WState_AutoMask = 0x00001000, + WState_Polished = 0x00002000, + WState_DND = 0x00004000, + WState_Reserved0 = 0x00008000, + WState_FullScreen = 0x00010000, + WState_OwnSizePolicy = 0x00020000, + WState_CreatedHidden = 0x00040000, + WState_Maximized = 0x00080000, + WState_Minimized = 0x00100000, + WState_ForceDisabled = 0x00200000, + WState_Exposed = 0x00400000, + WState_HasMouse = 0x00800000 + }; + + // Widget flags2; documented in qwidget.cpp + typedef uint WFlags; + + // documented in qwidget.cpp + enum WidgetFlags { + WType_TopLevel = 0x00000001, // widget type flags + WType_Dialog = 0x00000002, + WType_Popup = 0x00000004, + WType_Desktop = 0x00000008, + WType_Mask = 0x0000000f, + + WStyle_Customize = 0x00000010, // window style flags + WStyle_NormalBorder = 0x00000020, + WStyle_DialogBorder = 0x00000040, // MS-Windows only + WStyle_NoBorder = 0x00002000, + WStyle_Title = 0x00000080, + WStyle_SysMenu = 0x00000100, + WStyle_Minimize = 0x00000200, + WStyle_Maximize = 0x00000400, + WStyle_MinMax = WStyle_Minimize | WStyle_Maximize, + WStyle_Tool = 0x00000800, + WStyle_StaysOnTop = 0x00001000, + WStyle_ContextHelp = 0x00004000, + WStyle_Reserved = 0x00008000, + WStyle_Mask = 0x0000fff0, + + WDestructiveClose = 0x00010000, // misc flags + WPaintDesktop = 0x00020000, + WPaintUnclipped = 0x00040000, + WPaintClever = 0x00080000, + WResizeNoErase = 0x00100000, // OBSOLETE + WMouseNoMask = 0x00200000, + WStaticContents = 0x00400000, + WRepaintNoErase = 0x00800000, // OBSOLETE +#if defined(Q_WS_X11) + WX11BypassWM = 0x01000000, + WWinOwnDC = 0x00000000, + WMacNoSheet = 0x00000000, + WMacDrawer = 0x00000000, +#elif defined(Q_WS_MAC) + WX11BypassWM = 0x00000000, + WWinOwnDC = 0x00000000, + WMacNoSheet = 0x01000000, + WMacDrawer = 0x20000000, +#else + WX11BypassWM = 0x00000000, + WWinOwnDC = 0x01000000, + WMacNoSheet = 0x00000000, + WMacDrawer = 0x00000000, +#endif + WGroupLeader = 0x02000000, + WShowModal = 0x04000000, + WNoMousePropagation = 0x08000000, + WSubWindow = 0x10000000, +#if defined(Q_WS_X11) + WStyle_Splash = 0x20000000, +#else + WStyle_Splash = WStyle_NoBorder | WMacNoSheet | WStyle_Tool | WWinOwnDC, +#endif + WNoAutoErase = WRepaintNoErase | WResizeNoErase +#ifndef QT_NO_COMPAT + , + WNorthWestGravity = WStaticContents, + WType_Modal = WType_Dialog | WShowModal, + WStyle_Dialog = WType_Dialog, + WStyle_NoBorderEx = WStyle_NoBorder +#endif + }; + + enum WindowState { + WindowNoState = 0x00000000, + WindowMinimized = 0x00000001, + WindowMaximized = 0x00000002, + WindowFullScreen = 0x00000004, + WindowActive = 0x00000008 + }; + + + // Image conversion flags. The unusual ordering is caused by + // compatibility and default requirements. + // Documented in qimage.cpp + + enum ImageConversionFlags { + ColorMode_Mask = 0x00000003, + AutoColor = 0x00000000, + ColorOnly = 0x00000003, + MonoOnly = 0x00000002, + // Reserved = 0x00000001, + + AlphaDither_Mask = 0x0000000c, + ThresholdAlphaDither = 0x00000000, + OrderedAlphaDither = 0x00000004, + DiffuseAlphaDither = 0x00000008, + NoAlpha = 0x0000000c, // Not supported + + Dither_Mask = 0x00000030, + DiffuseDither = 0x00000000, + OrderedDither = 0x00000010, + ThresholdDither = 0x00000020, + // ReservedDither= 0x00000030, + + DitherMode_Mask = 0x000000c0, + AutoDither = 0x00000000, + PreferDither = 0x00000040, + AvoidDither = 0x00000080 + }; + + // documented in qpainter.cpp + enum BGMode { // background mode + TransparentMode, + OpaqueMode + }; + +#ifndef QT_NO_COMPAT + // documented in qpainter.cpp + enum PaintUnit { // paint unit + PixelUnit, + LoMetricUnit, // OBSOLETE + HiMetricUnit, // OBSOLETE + LoEnglishUnit, // OBSOLETE + HiEnglishUnit, // OBSOLETE + TwipsUnit // OBSOLETE + }; +#endif + + // documented in qstyle.cpp +#ifdef QT_NO_COMPAT + enum GUIStyle { + WindowsStyle = 1, // ### Qt 4.0: either remove the obsolete enums or clean up compat vs. + MotifStyle = 4, // ### QT_NO_COMPAT by reordering or combination into one enum. + GtkStyle = 6 // Gtk compability mode + }; +#else + enum GUIStyle { + MacStyle, // OBSOLETE + WindowsStyle, + Win3Style, // OBSOLETE + PMStyle, // OBSOLETE + MotifStyle, + GtkStyle = 6 // Gtk compability mode + }; +#endif + + // documented in qkeysequence.cpp + enum SequenceMatch { + NoMatch, + PartialMatch, + Identical + }; + + // documented in qevent.cpp + enum Modifier { // accelerator modifiers + META = 0x00100000, + SHIFT = 0x00200000, + CTRL = 0x00400000, + ALT = 0x00800000, + MODIFIER_MASK = 0x00f00000, + UNICODE_ACCEL = 0x10000000, + + ASCII_ACCEL = UNICODE_ACCEL // 1.x compat + }; + + // documented in qevent.cpp + enum Key { + Key_Escape = 0x1000, // misc keys + Key_Tab = 0x1001, + Key_Backtab = 0x1002, Key_BackTab = Key_Backtab, + Key_Backspace = 0x1003, Key_BackSpace = Key_Backspace, + Key_Return = 0x1004, + Key_Enter = 0x1005, + Key_Insert = 0x1006, + Key_Delete = 0x1007, + Key_Pause = 0x1008, + Key_Print = 0x1009, + Key_SysReq = 0x100a, + Key_Clear = 0x100b, + Key_Home = 0x1010, // cursor movement + Key_End = 0x1011, + Key_Left = 0x1012, + Key_Up = 0x1013, + Key_Right = 0x1014, + Key_Down = 0x1015, + Key_Prior = 0x1016, Key_PageUp = Key_Prior, + Key_Next = 0x1017, Key_PageDown = Key_Next, + Key_Shift = 0x1020, // modifiers + Key_Control = 0x1021, + Key_Meta = 0x1022, + Key_Alt = 0x1023, + Key_CapsLock = 0x1024, + Key_NumLock = 0x1025, + Key_ScrollLock = 0x1026, + Key_F1 = 0x1030, // function keys + Key_F2 = 0x1031, + Key_F3 = 0x1032, + Key_F4 = 0x1033, + Key_F5 = 0x1034, + Key_F6 = 0x1035, + Key_F7 = 0x1036, + Key_F8 = 0x1037, + Key_F9 = 0x1038, + Key_F10 = 0x1039, + Key_F11 = 0x103a, + Key_F12 = 0x103b, + Key_F13 = 0x103c, + Key_F14 = 0x103d, + Key_F15 = 0x103e, + Key_F16 = 0x103f, + Key_F17 = 0x1040, + Key_F18 = 0x1041, + Key_F19 = 0x1042, + Key_F20 = 0x1043, + Key_F21 = 0x1044, + Key_F22 = 0x1045, + Key_F23 = 0x1046, + Key_F24 = 0x1047, + Key_F25 = 0x1048, // F25 .. F35 only on X11 + Key_F26 = 0x1049, + Key_F27 = 0x104a, + Key_F28 = 0x104b, + Key_F29 = 0x104c, + Key_F30 = 0x104d, + Key_F31 = 0x104e, + Key_F32 = 0x104f, + Key_F33 = 0x1050, + Key_F34 = 0x1051, + Key_F35 = 0x1052, + Key_Super_L = 0x1053, // extra keys + Key_Super_R = 0x1054, + Key_Menu = 0x1055, + Key_Hyper_L = 0x1056, + Key_Hyper_R = 0x1057, + Key_Help = 0x1058, + Key_Direction_L = 0x1059, + Key_Direction_R = 0x1060, + + // International input method support (X keycode - 0xEE00, the + // definition follows Qt/Embedded 2.3.7) Only interesting if + // you are writing your own input method + + // International & multi-key character composition + Key_Multi_key = 0x1120, // Multi-key character compose + Key_Codeinput = 0x1137, + Key_SingleCandidate = 0x113c, + Key_MultipleCandidate = 0x113d, + Key_PreviousCandidate = 0x113e, + + // Misc Functions + Key_Mode_switch = 0x117e, // Character set switch + //Key_script_switch = 0x117e, // Alias for mode_switch + + // Japanese keyboard support + Key_Kanji = 0x1121, // Kanji, Kanji convert + Key_Muhenkan = 0x1122, // Cancel Conversion + //Key_Henkan_Mode = 0x1123, // Start/Stop Conversion + Key_Henkan = 0x1123, // Alias for Henkan_Mode + Key_Romaji = 0x1124, // to Romaji + Key_Hiragana = 0x1125, // to Hiragana + Key_Katakana = 0x1126, // to Katakana + Key_Hiragana_Katakana = 0x1127, // Hiragana/Katakana toggle + Key_Zenkaku = 0x1128, // to Zenkaku + Key_Hankaku = 0x1129, // to Hankaku + Key_Zenkaku_Hankaku = 0x112a, // Zenkaku/Hankaku toggle + Key_Touroku = 0x112b, // Add to Dictionary + Key_Massyo = 0x112c, // Delete from Dictionary + Key_Kana_Lock = 0x112d, // Kana Lock + Key_Kana_Shift = 0x112e, // Kana Shift + Key_Eisu_Shift = 0x112f, // Alphanumeric Shift + Key_Eisu_toggle = 0x1130, // Alphanumeric toggle + //Key_Kanji_Bangou = 0x1137, // Codeinput + //Key_Zen_Koho = 0x113d, // Multiple/All Candidate(s) + //Key_Mae_Koho = 0x113e, // Previous Candidate + + // Korean keyboard support + // + // In fact, many Korean users need only 2 keys, Key_Hangul and + // Key_Hangul_Hanja. But rest of the keys are good for future. + + Key_Hangul = 0x1131, // Hangul start/stop(toggle) + Key_Hangul_Start = 0x1132, // Hangul start + Key_Hangul_End = 0x1133, // Hangul end, English start + Key_Hangul_Hanja = 0x1134, // Start Hangul->Hanja Conversion + Key_Hangul_Jamo = 0x1135, // Hangul Jamo mode + Key_Hangul_Romaja = 0x1136, // Hangul Romaja mode + //Key_Hangul_Codeinput = 0x1137, // Hangul code input mode + Key_Hangul_Jeonja = 0x1138, // Jeonja mode + Key_Hangul_Banja = 0x1139, // Banja mode + Key_Hangul_PreHanja = 0x113a, // Pre Hanja conversion + Key_Hangul_PostHanja = 0x113b, // Post Hanja conversion + //Key_Hangul_SingleCandidate = 0x113c, // Single candidate + //Key_Hangul_MultipleCandidate = 0x113d, // Multiple candidate + //Key_Hangul_PreviousCandidate = 0x113e, // Previous candidate + Key_Hangul_Special = 0x113f, // Special symbols + //Key_Hangul_switch = 0x117e, // Alias for mode_switch + + // dead keys (X keycode - 0xED00 to avoid the conflict) + Key_Dead_Grave = 0x1250, + Key_Dead_Acute = 0x1251, + Key_Dead_Circumflex = 0x1252, + Key_Dead_Tilde = 0x1253, + Key_Dead_Macron = 0x1254, + Key_Dead_Breve = 0x1255, + Key_Dead_Abovedot = 0x1256, + Key_Dead_Diaeresis = 0x1257, + Key_Dead_Abovering = 0x1258, + Key_Dead_Doubleacute = 0x1259, + Key_Dead_Caron = 0x125a, + Key_Dead_Cedilla = 0x125b, + Key_Dead_Ogonek = 0x125c, + Key_Dead_Iota = 0x125d, + Key_Dead_Voiced_Sound = 0x125e, + Key_Dead_Semivoiced_Sound = 0x125f, + Key_Dead_Belowdot = 0x1260, + Key_Dead_Hook = 0x1261, + Key_Dead_Horn = 0x1262, + + Key_Space = 0x20, // 7 bit printable ASCII + Key_Any = Key_Space, + Key_Exclam = 0x21, + Key_QuoteDbl = 0x22, + Key_NumberSign = 0x23, + Key_Dollar = 0x24, + Key_Percent = 0x25, + Key_Ampersand = 0x26, + Key_Apostrophe = 0x27, + Key_ParenLeft = 0x28, + Key_ParenRight = 0x29, + Key_Asterisk = 0x2a, + Key_Plus = 0x2b, + Key_Comma = 0x2c, + Key_Minus = 0x2d, + Key_Period = 0x2e, + Key_Slash = 0x2f, + Key_0 = 0x30, + Key_1 = 0x31, + Key_2 = 0x32, + Key_3 = 0x33, + Key_4 = 0x34, + Key_5 = 0x35, + Key_6 = 0x36, + Key_7 = 0x37, + Key_8 = 0x38, + Key_9 = 0x39, + Key_Colon = 0x3a, + Key_Semicolon = 0x3b, + Key_Less = 0x3c, + Key_Equal = 0x3d, + Key_Greater = 0x3e, + Key_Question = 0x3f, + Key_At = 0x40, + Key_A = 0x41, + Key_B = 0x42, + Key_C = 0x43, + Key_D = 0x44, + Key_E = 0x45, + Key_F = 0x46, + Key_G = 0x47, + Key_H = 0x48, + Key_I = 0x49, + Key_J = 0x4a, + Key_K = 0x4b, + Key_L = 0x4c, + Key_M = 0x4d, + Key_N = 0x4e, + Key_O = 0x4f, + Key_P = 0x50, + Key_Q = 0x51, + Key_R = 0x52, + Key_S = 0x53, + Key_T = 0x54, + Key_U = 0x55, + Key_V = 0x56, + Key_W = 0x57, + Key_X = 0x58, + Key_Y = 0x59, + Key_Z = 0x5a, + Key_BracketLeft = 0x5b, + Key_Backslash = 0x5c, + Key_BracketRight = 0x5d, + Key_AsciiCircum = 0x5e, + Key_Underscore = 0x5f, + Key_QuoteLeft = 0x60, + Key_BraceLeft = 0x7b, + Key_Bar = 0x7c, + Key_BraceRight = 0x7d, + Key_AsciiTilde = 0x7e, + + // Latin 1 codes adapted from X: keysymdef.h,v 1.21 94/08/28 16:17:06 + // + // This is mainly for compatibility - applications and input + // methods should not use the Qt keycodes between 128 and 255, + // but should rather use the QKeyEvent::text(). See + // QETWidget::translateKeyEventInternal() for more details. + + Key_nobreakspace = 0x0a0, + Key_exclamdown = 0x0a1, + Key_cent = 0x0a2, + Key_sterling = 0x0a3, + Key_currency = 0x0a4, + Key_yen = 0x0a5, + Key_brokenbar = 0x0a6, + Key_section = 0x0a7, + Key_diaeresis = 0x0a8, + Key_copyright = 0x0a9, + Key_ordfeminine = 0x0aa, + Key_guillemotleft = 0x0ab, // left angle quotation mark + Key_notsign = 0x0ac, + Key_hyphen = 0x0ad, + Key_registered = 0x0ae, + Key_macron = 0x0af, + Key_degree = 0x0b0, + Key_plusminus = 0x0b1, + Key_twosuperior = 0x0b2, + Key_threesuperior = 0x0b3, + Key_acute = 0x0b4, + Key_mu = 0x0b5, + Key_paragraph = 0x0b6, + Key_periodcentered = 0x0b7, + Key_cedilla = 0x0b8, + Key_onesuperior = 0x0b9, + Key_masculine = 0x0ba, + Key_guillemotright = 0x0bb, // right angle quotation mark + Key_onequarter = 0x0bc, + Key_onehalf = 0x0bd, + Key_threequarters = 0x0be, + Key_questiondown = 0x0bf, + Key_Agrave = 0x0c0, + Key_Aacute = 0x0c1, + Key_Acircumflex = 0x0c2, + Key_Atilde = 0x0c3, + Key_Adiaeresis = 0x0c4, + Key_Aring = 0x0c5, + Key_AE = 0x0c6, + Key_Ccedilla = 0x0c7, + Key_Egrave = 0x0c8, + Key_Eacute = 0x0c9, + Key_Ecircumflex = 0x0ca, + Key_Ediaeresis = 0x0cb, + Key_Igrave = 0x0cc, + Key_Iacute = 0x0cd, + Key_Icircumflex = 0x0ce, + Key_Idiaeresis = 0x0cf, + Key_ETH = 0x0d0, + Key_Ntilde = 0x0d1, + Key_Ograve = 0x0d2, + Key_Oacute = 0x0d3, + Key_Ocircumflex = 0x0d4, + Key_Otilde = 0x0d5, + Key_Odiaeresis = 0x0d6, + Key_multiply = 0x0d7, + Key_Ooblique = 0x0d8, + Key_Ugrave = 0x0d9, + Key_Uacute = 0x0da, + Key_Ucircumflex = 0x0db, + Key_Udiaeresis = 0x0dc, + Key_Yacute = 0x0dd, + Key_THORN = 0x0de, + Key_ssharp = 0x0df, + Key_agrave = 0x0e0, + Key_aacute = 0x0e1, + Key_acircumflex = 0x0e2, + Key_atilde = 0x0e3, + Key_adiaeresis = 0x0e4, + Key_aring = 0x0e5, + Key_ae = 0x0e6, + Key_ccedilla = 0x0e7, + Key_egrave = 0x0e8, + Key_eacute = 0x0e9, + Key_ecircumflex = 0x0ea, + Key_ediaeresis = 0x0eb, + Key_igrave = 0x0ec, + Key_iacute = 0x0ed, + Key_icircumflex = 0x0ee, + Key_idiaeresis = 0x0ef, + Key_eth = 0x0f0, + Key_ntilde = 0x0f1, + Key_ograve = 0x0f2, + Key_oacute = 0x0f3, + Key_ocircumflex = 0x0f4, + Key_otilde = 0x0f5, + Key_odiaeresis = 0x0f6, + Key_division = 0x0f7, + Key_oslash = 0x0f8, + Key_ugrave = 0x0f9, + Key_uacute = 0x0fa, + Key_ucircumflex = 0x0fb, + Key_udiaeresis = 0x0fc, + Key_yacute = 0x0fd, + Key_thorn = 0x0fe, + Key_ydiaeresis = 0x0ff, + + // multimedia/internet keys - ignored by default - see QKeyEvent c'tor + + Key_Back = 0x1061, + Key_Forward = 0x1062, + Key_Stop = 0x1063, + Key_Refresh = 0x1064, + + Key_VolumeDown = 0x1070, + Key_VolumeMute = 0x1071, + Key_VolumeUp = 0x1072, + Key_BassBoost = 0x1073, + Key_BassUp = 0x1074, + Key_BassDown = 0x1075, + Key_TrebleUp = 0x1076, + Key_TrebleDown = 0x1077, + + Key_MediaPlay = 0x1080, + Key_MediaStop = 0x1081, + Key_MediaPrev = 0x1082, + Key_MediaNext = 0x1083, + Key_MediaRecord = 0x1084, + + Key_HomePage = 0x1090, + Key_Favorites = 0x1091, + Key_Search = 0x1092, + Key_Standby = 0x1093, + Key_OpenUrl = 0x1094, + + Key_LaunchMail = 0x10a0, + Key_LaunchMedia = 0x10a1, + Key_Launch0 = 0x10a2, + Key_Launch1 = 0x10a3, + Key_Launch2 = 0x10a4, + Key_Launch3 = 0x10a5, + Key_Launch4 = 0x10a6, + Key_Launch5 = 0x10a7, + Key_Launch6 = 0x10a8, + Key_Launch7 = 0x10a9, + Key_Launch8 = 0x10aa, + Key_Launch9 = 0x10ab, + Key_LaunchA = 0x10ac, + Key_LaunchB = 0x10ad, + Key_LaunchC = 0x10ae, + Key_LaunchD = 0x10af, + Key_LaunchE = 0x10b0, + Key_LaunchF = 0x10b1, + + Key_MediaLast = 0x1fff, + + Key_unknown = 0xffff + }; + + // documented in qcommonstyle.cpp + enum ArrowType { + UpArrow, + DownArrow, + LeftArrow, + RightArrow + }; + + // documented in qpainter.cpp + enum RasterOp { // raster op mode + CopyROP, + OrROP, + XorROP, + NotAndROP, EraseROP=NotAndROP, + NotCopyROP, + NotOrROP, + NotXorROP, + AndROP, NotEraseROP=AndROP, + NotROP, + ClearROP, + SetROP, + NopROP, + AndNotROP, + OrNotROP, + NandROP, + NorROP, LastROP=NorROP + }; + + // documented in qpainter.cpp + enum PenStyle { // pen style + NoPen, + SolidLine, + DashLine, + DotLine, + DashDotLine, + DashDotDotLine, + MPenStyle = 0x0f + }; + + // documented in qpainter.cpp + enum PenCapStyle { // line endcap style + FlatCap = 0x00, + SquareCap = 0x10, + RoundCap = 0x20, + MPenCapStyle = 0x30 + }; + + // documented in qpainter.cpp + enum PenJoinStyle { // line join style + MiterJoin = 0x00, + BevelJoin = 0x40, + RoundJoin = 0x80, + MPenJoinStyle = 0xc0 + }; + + // documented in qpainter.cpp + enum BrushStyle { // brush style + NoBrush, + SolidPattern, + Dense1Pattern, + Dense2Pattern, + Dense3Pattern, + Dense4Pattern, + Dense5Pattern, + Dense6Pattern, + Dense7Pattern, + HorPattern, + VerPattern, + CrossPattern, + BDiagPattern, + FDiagPattern, + DiagCrossPattern, + CustomPattern=24 + }; + + // documented in qapplication_mac.cpp + enum MacintoshVersion { + //Unknown + MV_Unknown = 0x0000, + + //Version numbers + MV_9 = 0x0001, + MV_10_DOT_0 = 0x0002, + MV_10_DOT_1 = 0x0003, + MV_10_DOT_2 = 0x0004, + MV_10_DOT_3 = 0x0005, + MV_10_DOT_4 = 0x0006, + + //Code names + MV_CHEETAH = MV_10_DOT_0, + MV_PUMA = MV_10_DOT_1, + MV_JAGUAR = MV_10_DOT_2, + MV_PANTHER = MV_10_DOT_3, + MV_TIGER = MV_10_DOT_4 + }; + + // documented in qapplication_win.cpp + enum WindowsVersion { + WV_32s = 0x0001, + WV_95 = 0x0002, + WV_98 = 0x0003, + WV_Me = 0x0004, + WV_DOS_based = 0x000f, + + WV_NT = 0x0010, + WV_2000 = 0x0020, + WV_XP = 0x0030, + WV_2003 = 0x0040, + WV_VISTA = 0x0080, + WV_NT_based = 0x00f0, + + WV_CE = 0x0100, + WV_CENET = 0x0200, + WV_CE_based = 0x0f00 + }; + + // documented in qstyle.cpp + enum UIEffect { + UI_General, + UI_AnimateMenu, + UI_FadeMenu, + UI_AnimateCombo, + UI_AnimateTooltip, + UI_FadeTooltip, + UI_AnimateToolBox + }; + + // documented in qcursor.cpp + enum CursorShape { + ArrowCursor, + UpArrowCursor, + CrossCursor, + WaitCursor, + IbeamCursor, + SizeVerCursor, + SizeHorCursor, + SizeBDiagCursor, + SizeFDiagCursor, + SizeAllCursor, + BlankCursor, + SplitVCursor, + SplitHCursor, + PointingHandCursor, + ForbiddenCursor, + WhatsThisCursor, + BusyCursor, + LastCursor = BusyCursor, + BitmapCursor = 24 + }; + + // Global cursors + + QT_STATIC_CONST QCursor & arrowCursor; // standard arrow cursor + QT_STATIC_CONST QCursor & upArrowCursor; // upwards arrow + QT_STATIC_CONST QCursor & crossCursor; // crosshair + QT_STATIC_CONST QCursor & waitCursor; // hourglass/watch + QT_STATIC_CONST QCursor & ibeamCursor; // ibeam/text entry + QT_STATIC_CONST QCursor & sizeVerCursor; // vertical resize + QT_STATIC_CONST QCursor & sizeHorCursor; // horizontal resize + QT_STATIC_CONST QCursor & sizeBDiagCursor; // diagonal resize (/) + QT_STATIC_CONST QCursor & sizeFDiagCursor; // diagonal resize (\) + QT_STATIC_CONST QCursor & sizeAllCursor; // all directions resize + QT_STATIC_CONST QCursor & blankCursor; // blank/invisible cursor + QT_STATIC_CONST QCursor & splitVCursor; // vertical bar with left-right + // arrows + QT_STATIC_CONST QCursor & splitHCursor; // horizontal bar with up-down + // arrows + QT_STATIC_CONST QCursor & pointingHandCursor; // pointing hand + QT_STATIC_CONST QCursor & forbiddenCursor; // forbidden cursor (slashed circle) + QT_STATIC_CONST QCursor & whatsThisCursor; // arrow with a question mark + QT_STATIC_CONST QCursor & busyCursor; // arrow with hourglass + + + enum TextFormat { + PlainText, + RichText, + AutoText, + LogText + }; + + // Documented in qtextedit.cpp + enum AnchorAttribute { + AnchorName, + AnchorHref + }; + + // Documented in qmainwindow.cpp + enum Dock { + DockUnmanaged, + DockTornOff, + DockTop, + DockBottom, + DockRight, + DockLeft, + DockMinimized +#ifndef QT_NO_COMPAT + , + Unmanaged = DockUnmanaged, + TornOff = DockTornOff, + Top = DockTop, + Bottom = DockBottom, + Right = DockRight, + Left = DockLeft, + Minimized = DockMinimized +#endif + }; + // compatibility + typedef Dock ToolBarDock; + + // documented in qdatetime.cpp + enum DateFormat { + TextDate, // default Qt + ISODate, // ISO 8601 + LocalDate // locale dependent + }; + + // documented in qdatetime.cpp + enum TimeSpec { + LocalTime, + UTC + }; + + // documented in qwidget.cpp + enum BackgroundMode { + FixedColor, + FixedPixmap, + NoBackground, + PaletteForeground, + PaletteButton, + PaletteLight, + PaletteMidlight, + PaletteDark, + PaletteMid, + PaletteText, + PaletteBrightText, + PaletteBase, + PaletteBackground, + PaletteShadow, + PaletteHighlight, + PaletteHighlightedText, + PaletteButtonText, + PaletteLink, + PaletteLinkVisited, + X11ParentRelative + }; + + typedef uint ComparisonFlags; + + // Documented in qstring.cpp + enum StringComparisonMode { + CaseSensitive = 0x00001, // 0 0001 + BeginsWith = 0x00002, // 0 0010 + EndsWith = 0x00004, // 0 0100 + Contains = 0x00008, // 0 1000 + ExactMatch = 0x00010 // 1 0000 + }; + + // Documented in qtabwidget.cpp + enum Corner { + TopLeft = 0x00000, + TopRight = 0x00001, + BottomLeft = 0x00002, + BottomRight = 0x00003 + }; + + // "handle" type for system objects. Documented as \internal in + // qapplication.cpp +#if defined(Q_WS_MAC) + typedef void * HANDLE; +#elif defined(Q_WS_WIN) + typedef void *HANDLE; +#elif defined(Q_WS_X11) + typedef unsigned long HANDLE; +#elif defined(Q_WS_QWS) + typedef void * HANDLE; +#endif +}; + + +class Q_EXPORT QInternal { +public: + enum PaintDeviceFlags { + UndefinedDevice = 0x00, + Widget = 0x01, + Pixmap = 0x02, + Printer = 0x03, + Picture = 0x04, + System = 0x05, + DeviceTypeMask = 0x0f, + ExternalDevice = 0x10, + // used to emulate some of the behaviour different between Qt2 and Qt3 (mainly for printing) + CompatibilityMode = 0x20 + }; +}; + +#endif // QNAMESPACE_H diff --git a/src/kernel/qnetworkprotocol.cpp b/src/kernel/qnetworkprotocol.cpp new file mode 100644 index 0000000..4173584 --- /dev/null +++ b/src/kernel/qnetworkprotocol.cpp @@ -0,0 +1,1265 @@ +/**************************************************************************** +** +** Implementation of QNetworkProtocol class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qnetworkprotocol.h" + +#ifndef QT_NO_NETWORKPROTOCOL + +#include "qlocalfs.h" +#include "qurloperator.h" +#include "qtimer.h" +#include "qmap.h" +#include "qptrqueue.h" + +//#define QNETWORKPROTOCOL_DEBUG +#define NETWORK_OP_DELAY 1000 + +extern Q_EXPORT QNetworkProtocolDict *qNetworkProtocolRegister; + +QNetworkProtocolDict *qNetworkProtocolRegister = 0; + +class QNetworkProtocolPrivate +{ +public: + QNetworkProtocolPrivate( QNetworkProtocol *p ) + { + url = 0; + opInProgress = 0; + opStartTimer = new QTimer( p ); + removeTimer = new QTimer( p ); + operationQueue.setAutoDelete( FALSE ); + autoDelete = FALSE; + removeInterval = 10000; + oldOps.setAutoDelete( FALSE ); + } + + ~QNetworkProtocolPrivate() + { + removeTimer->stop(); + if ( opInProgress ) { + if ( opInProgress == operationQueue.head() ) + operationQueue.dequeue(); + opInProgress->free(); + } + while ( operationQueue.head() ) { + operationQueue.head()->free(); + operationQueue.dequeue(); + } + while ( oldOps.first() ) { + oldOps.first()->free(); + oldOps.removeFirst(); + } + delete opStartTimer; + } + + QUrlOperator *url; + QPtrQueue< QNetworkOperation > operationQueue; + QNetworkOperation *opInProgress; + QTimer *opStartTimer, *removeTimer; + int removeInterval; + bool autoDelete; + QPtrList< QNetworkOperation > oldOps; +}; + +/*! + \class QNetworkProtocol qnetworkprotocol.h + \brief The QNetworkProtocol class provides a common API for network protocols. +\if defined(commercial) + It is part of the <a href="commercialeditions.html">Qt Enterprise Edition</a>. +\endif + + \module network + \ingroup io + \module network + \mainclass + + This is a base class which should be used for network protocols + implementations that can then be used in Qt (e.g. in the file + dialog) together with the QUrlOperator. + + The easiest way to implement a new network protocol is to + reimplement the operation*() methods, e.g. operationGet(), etc. + Only the supported operations should be reimplemented. To specify + which operations are supported, also reimplement + supportedOperations() and return an int that is OR'd together + using the supported operations from the \l + QNetworkProtocol::Operation enum. + + When you implement a network protocol this way, it is important to + emit the correct signals. Also, always emit the finished() signal + when an operation is done (on success \e and on failure). Qt + relies on correctly emitted finished() signals. + + For a detailed description of the Qt Network Architecture and how + to implement and use network protocols in Qt, see the \link + network.html Qt Network Documentation\endlink. +*/ + +/*! + \fn void QNetworkProtocol::newChildren( const QValueList<QUrlInfo> &i, QNetworkOperation *op ) + + This signal is emitted after listChildren() was called and new + children (files) have been read from the list of files. \a i holds + the information about the new children. \a op is the pointer to + the operation object which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. + + When implementing your own network protocol and reading children, + you usually don't read one child at once, but rather a list of + them. That's why this signal takes a list of QUrlInfo objects. If + you prefer to read just one child at a time you can use the + convenience signal newChild(), which takes a single QUrlInfo + object. +*/ + +/*! + \fn void QNetworkProtocol::newChild( const QUrlInfo &i, QNetworkOperation *op ) + + This signal is emitted if a new child (file) has been read. + QNetworkProtocol automatically connects it to a slot which creates + a list of QUrlInfo objects (with just one QUrlInfo \a i) and emits + the newChildren() signal with this list. \a op is the pointer to + the operation object which contains all the information about the + operation that has finished, including the state, etc. + + This is just a convenience signal useful for implementing your own + network protocol. In all other cases connect to the newChildren() + signal with its list of QUrlInfo objects. +*/ + +/*! + \fn void QNetworkProtocol::finished( QNetworkOperation *op ) + + This signal is emitted when an operation finishes. This signal is + always emitted, for both success and failure. \a op is the pointer + to the operation object which contains all the information about + the operation, including the state, etc. Check the state and error + code of the operation object to determine whether or not the + operation was successful. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::start( QNetworkOperation *op ) + + Some operations (such as listChildren()) emit this signal when + they start processing the operation. \a op is the pointer to the + operation object which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::createdDirectory( const QUrlInfo &i, QNetworkOperation *op ) + + This signal is emitted when mkdir() has been succesful and the + directory has been created. \a i holds the information about the + new directory. \a op is the pointer to the operation object which + contains all the information about the operation, including the + state, etc. Using op->arg( 0 ), you can get the file name of the + new directory. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::removed( QNetworkOperation *op ) + + This signal is emitted when remove() has been succesful and the + file has been removed. \a op holds the file name of the removed + file in the first argument, accessible with op->arg( 0 ). \a op is + the pointer to the operation object which contains all the + information about the operation, including the state, etc. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::itemChanged( QNetworkOperation *op ) + + This signal is emitted whenever a file which is a child of this + URL has been changed, e.g. by successfully calling rename(). \a op + holds the original and the new file names in the first and second + arguments, accessible with op->arg( 0 ) and op->arg( 1 ) + respectively. \a op is the pointer to the operation object which + contains all the information about the operation, including the + state, etc. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::data( const QByteArray &data, + QNetworkOperation *op ) + + This signal is emitted when new \a data has been received after + calling get() or put(). \a op holds the name of the file from + which data is retrieved or uploaded in its first argument, and the + (raw) data in its second argument. You can get them with + op->arg( 0 ) and op->rawArg( 1 ). \a op is the pointer to the + operation object, which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator (which is used by the network + protocol) emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::dataTransferProgress( int bytesDone, int bytesTotal, QNetworkOperation *op ) + + This signal is emitted during the transfer of data (using put() or + get()). \a bytesDone is how many bytes of \a bytesTotal have been + transferred. \a bytesTotal may be -1, which means that the total + number of bytes is not known. \a op is the pointer to the + operation object which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, QNetworkProtocol is smart + enough to let the QUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void QNetworkProtocol::connectionStateChanged( int state, const QString &data ) + + This signal is emitted whenever the state of the connection of the + network protocol is changed. \a state describes the new state, + which is one of, \c ConHostFound, \c ConConnected or \c ConClosed. + \a data is a message text. +*/ + +/*! + \enum QNetworkProtocol::State + + This enum contains the state that a QNetworkOperation can have. + + \value StWaiting The operation is in the QNetworkProtocol's queue + waiting to be prcessed. + + \value StInProgress The operation is being processed. + + \value StDone The operation has been processed succesfully. + + \value StFailed The operation has been processed but an error occurred. + + \value StStopped The operation has been processed but has been + stopped before it finished, and is waiting to be processed. + +*/ + +/*! + \enum QNetworkProtocol::Operation + + This enum lists the possible operations that a network protocol + can support. supportedOperations() returns an int of these that is + OR'd together. Also, the type() of a QNetworkOperation is always + one of these values. + + \value OpListChildren List the children of a URL, e.g. of a directory. + \value OpMkDir Create a directory. + \value OpRemove Remove a child (e.g. a file). + \value OpRename Rename a child (e.g. a file). + \value OpGet Get data from a location. + \value OpPut Put data to a location. +*/ + +/*! + \enum QNetworkProtocol::ConnectionState + + When the connection state of a network protocol changes it emits + the signal connectionStateChanged(). The first argument is one of + the following values: + + \value ConHostFound Host has been found. + \value ConConnected Connection to the host has been established. + \value ConClosed Connection has been closed. +*/ + +/*! + \enum QNetworkProtocol::Error + + When an operation fails (finishes unsuccessfully), the + QNetworkOperation of the operation returns an error code which has + one of the following values: + + \value NoError No error occurred. + + \value ErrValid The URL you are operating on is not valid. + + \value ErrUnknownProtocol There is no protocol implementation + available for the protocol of the URL you are operating on (e.g. + if the protocol is http and no http implementation has been + registered). + + \value ErrUnsupported The operation is not supported by the + protocol. + + \value ErrParse The URL could not be parsed correctly. + + \value ErrLoginIncorrect You needed to login but the username + or password is wrong. + + \value ErrHostNotFound The specified host (in the URL) couldn't + be found. + + \value ErrListChildren An error occurred while listing the + children (files). + + \value ErrMkDir An error occurred when creating a directory. + + \value ErrRemove An error occurred when removing a child (file). + + \value ErrRename An error occurred when renaming a child (file). + + \value ErrGet An error occurred while getting (retrieving) data. + + \value ErrPut An error occurred while putting (uploading) data. + + \value ErrFileNotExisting A file which is needed by the operation + doesn't exist. + + \value ErrPermissionDenied Permission for doing the operation has + been denied. + + You should also use these error codes when implementing custom + network protocols. If this is not possible, you can define your own + error codes by using integer values that don't conflict with any + of these values. +*/ + +/*! + Constructor of the network protocol base class. Does some + initialization and connecting of signals and slots. +*/ + +QNetworkProtocol::QNetworkProtocol() + : QObject() +{ + d = new QNetworkProtocolPrivate( this ); + + connect( d->opStartTimer, SIGNAL( timeout() ), + this, SLOT( startOps() ) ); + connect( d->removeTimer, SIGNAL( timeout() ), + this, SLOT( removeMe() ) ); + + if ( url() ) { + connect( this, SIGNAL( data(const QByteArray&,QNetworkOperation*) ), + url(), SIGNAL( data(const QByteArray&,QNetworkOperation*) ) ); + connect( this, SIGNAL( finished(QNetworkOperation*) ), + url(), SIGNAL( finished(QNetworkOperation*) ) ); + connect( this, SIGNAL( start(QNetworkOperation*) ), + url(), SIGNAL( start(QNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ), + url(), SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ), + url(), SLOT( addEntry(const QValueList<QUrlInfo>&) ) ); + connect( this, SIGNAL( createdDirectory(const QUrlInfo&,QNetworkOperation*) ), + url(), SIGNAL( createdDirectory(const QUrlInfo&,QNetworkOperation*) ) ); + connect( this, SIGNAL( removed(QNetworkOperation*) ), + url(), SIGNAL( removed(QNetworkOperation*) ) ); + connect( this, SIGNAL( itemChanged(QNetworkOperation*) ), + url(), SIGNAL( itemChanged(QNetworkOperation*) ) ); + connect( this, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ), + url(), SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ) ); + connect( this, SIGNAL( connectionStateChanged(int,const QString&) ), + url(), SIGNAL( connectionStateChanged(int,const QString&) ) ); + } + + connect( this, SIGNAL( finished(QNetworkOperation*) ), + this, SLOT( processNextOperation(QNetworkOperation*) ) ); + connect( this, SIGNAL( newChild(const QUrlInfo&,QNetworkOperation*) ), + this, SLOT( emitNewChildren(const QUrlInfo&,QNetworkOperation*) ) ); + +} + +/*! + Destructor. +*/ + +QNetworkProtocol::~QNetworkProtocol() +{ + delete d; +} + +/*! + Sets the QUrlOperator, on which the protocol works, to \a u. + + \sa QUrlOperator +*/ + +void QNetworkProtocol::setUrl( QUrlOperator *u ) +{ + if ( url() ) { + disconnect( this, SIGNAL( data(const QByteArray&,QNetworkOperation*) ), + url(), SIGNAL( data(const QByteArray&,QNetworkOperation*) ) ); + disconnect( this, SIGNAL( finished(QNetworkOperation*) ), + url(), SIGNAL( finished(QNetworkOperation*) ) ); + disconnect( this, SIGNAL( start(QNetworkOperation*) ), + url(), SIGNAL( start(QNetworkOperation*) ) ); + disconnect( this, SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ), + url(), SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ) ); + disconnect( this, SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ), + url(), SLOT( addEntry(const QValueList<QUrlInfo>&) ) ); + disconnect( this, SIGNAL( createdDirectory(const QUrlInfo&,QNetworkOperation*) ), + url(), SIGNAL( createdDirectory(const QUrlInfo&,QNetworkOperation*) ) ); + disconnect( this, SIGNAL( removed(QNetworkOperation*) ), + url(), SIGNAL( removed(QNetworkOperation*) ) ); + disconnect( this, SIGNAL( itemChanged(QNetworkOperation*) ), + url(), SIGNAL( itemChanged(QNetworkOperation*) ) ); + disconnect( this, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ), + url(), SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ) ); + disconnect( this, SIGNAL( connectionStateChanged(int,const QString&) ), + url(), SIGNAL( connectionStateChanged(int,const QString&) ) ); + } + + + // ### if autoDelete is TRUE, we should delete the QUrlOperator (something + // like below; but that is not possible since it would delete this, too). + //if ( d->autoDelete && (d->url!=u) ) { + // delete d->url; // destructor deletes the network protocol + //} + d->url = u; + + if ( url() ) { + connect( this, SIGNAL( data(const QByteArray&,QNetworkOperation*) ), + url(), SIGNAL( data(const QByteArray&,QNetworkOperation*) ) ); + connect( this, SIGNAL( finished(QNetworkOperation*) ), + url(), SIGNAL( finished(QNetworkOperation*) ) ); + connect( this, SIGNAL( start(QNetworkOperation*) ), + url(), SIGNAL( start(QNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ), + url(), SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const QValueList<QUrlInfo>&,QNetworkOperation*) ), + url(), SLOT( addEntry(const QValueList<QUrlInfo>&) ) ); + connect( this, SIGNAL( createdDirectory(const QUrlInfo&,QNetworkOperation*) ), + url(), SIGNAL( createdDirectory(const QUrlInfo&,QNetworkOperation*) ) ); + connect( this, SIGNAL( removed(QNetworkOperation*) ), + url(), SIGNAL( removed(QNetworkOperation*) ) ); + connect( this, SIGNAL( itemChanged(QNetworkOperation*) ), + url(), SIGNAL( itemChanged(QNetworkOperation*) ) ); + connect( this, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ), + url(), SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ) ); + connect( this, SIGNAL( connectionStateChanged(int,const QString&) ), + url(), SIGNAL( connectionStateChanged(int,const QString&) ) ); + } + + if ( !d->opInProgress && !d->operationQueue.isEmpty() ) + d->opStartTimer->start( 0, TRUE ); +} + +/*! + For processing operations the network protocol base class calls + this method quite often. This should be reimplemented by new + network protocols. It should return TRUE if the connection is OK + (open); otherwise it should return FALSE. If the connection is not + open the protocol should open it. + + If the connection can't be opened (e.g. because you already tried + but the host couldn't be found), set the state of \a op to + QNetworkProtocol::StFailed and emit the finished() signal with + this QNetworkOperation as argument. + + \a op is the operation that needs an open connection. +*/ + +bool QNetworkProtocol::checkConnection( QNetworkOperation * ) +{ + return TRUE; +} + +/*! + Returns an int that is OR'd together using the enum values of + \l{QNetworkProtocol::Operation}, which describes which operations + are supported by the network protocol. Should be reimplemented by + new network protocols. +*/ + +int QNetworkProtocol::supportedOperations() const +{ + return 0; +} + +/*! + Adds the operation \a op to the operation queue. The operation + will be processed as soon as possible. This method returns + immediately. +*/ + +void QNetworkProtocol::addOperation( QNetworkOperation *op ) +{ +#ifdef QNETWORKPROTOCOL_DEBUG + qDebug( "QNetworkOperation: addOperation: %p %d", op, op->operation() ); +#endif + d->operationQueue.enqueue( op ); + if ( !d->opInProgress ) + d->opStartTimer->start( 0, TRUE ); +} + +/*! + Static method to register a network protocol for Qt. For example, + if you have an implementation of NNTP (called Nntp) which is + derived from QNetworkProtocol, call: + \code + QNetworkProtocol::registerNetworkProtocol( "nntp", new QNetworkProtocolFactory<Nntp> ); + \endcode + after which your implementation is registered for future nntp + operations. + + The name of the protocol is given in \a protocol and a pointer to + the protocol factory is given in \a protocolFactory. +*/ + +void QNetworkProtocol::registerNetworkProtocol( const QString &protocol, + QNetworkProtocolFactoryBase *protocolFactory ) +{ + if ( !qNetworkProtocolRegister ) { + qNetworkProtocolRegister = new QNetworkProtocolDict; + QNetworkProtocol::registerNetworkProtocol( "file", new QNetworkProtocolFactory< QLocalFs > ); + } + + qNetworkProtocolRegister->insert( protocol, protocolFactory ); +} + +/*! + Static method to get a new instance of the network protocol \a + protocol. For example, if you need to do some FTP operations, do + the following: + \code + QFtp *ftp = QNetworkProtocol::getNetworkProtocol( "ftp" ); + \endcode + This returns a pointer to a new instance of an ftp implementation + or null if no protocol for ftp was registered. The ownership of + the pointer is transferred to you, so you must delete it if you + don't need it anymore. + + Normally you should not work directly with network protocols, so + you will not need to call this method yourself. Instead, use + QUrlOperator, which makes working with network protocols much more + convenient. + + \sa QUrlOperator +*/ + +QNetworkProtocol *QNetworkProtocol::getNetworkProtocol( const QString &protocol ) +{ + if ( !qNetworkProtocolRegister ) { + qNetworkProtocolRegister = new QNetworkProtocolDict; + QNetworkProtocol::registerNetworkProtocol( "file", new QNetworkProtocolFactory< QLocalFs > ); + } + + if ( protocol.isNull() ) + return 0; + + QNetworkProtocolFactoryBase *factory = qNetworkProtocolRegister->find( protocol ); + if ( factory ) + return factory->createObject(); + + return 0; +} + +/*! + Returns TRUE if the only protocol registered is for working on the + local filesystem; returns FALSE if other network protocols are + also registered. +*/ + +bool QNetworkProtocol::hasOnlyLocalFileSystem() +{ + if ( !qNetworkProtocolRegister ) + return FALSE; + + QDictIterator< QNetworkProtocolFactoryBase > it( *qNetworkProtocolRegister ); + for ( ; it.current(); ++it ) + if ( it.currentKey() != "file" ) + return FALSE; + return TRUE; +} + +/*! + \internal + Starts processing network operations. +*/ + +void QNetworkProtocol::startOps() +{ +#ifdef QNETWORKPROTOCOL_DEBUG + qDebug( "QNetworkOperation: start processing operations" ); +#endif + processNextOperation( 0 ); +} + +/*! + \internal + Processes the operation \a op. It calls the + corresponding operation[something]( QNetworkOperation * ) + methods. +*/ + +void QNetworkProtocol::processOperation( QNetworkOperation *op ) +{ + if ( !op ) + return; + + switch ( op->operation() ) { + case OpListChildren: + operationListChildren( op ); + break; + case OpMkDir: + operationMkDir( op ); + break; + case OpRemove: + operationRemove( op ); + break; + case OpRename: + operationRename( op ); + break; + case OpGet: + operationGet( op ); + break; + case OpPut: + operationPut( op ); + break; + } +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports listing children (files); + this method should then process this QNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html Qt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void QNetworkProtocol::operationListChildren( QNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports making directories; this + method should then process this QNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html Qt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void QNetworkProtocol::operationMkDir( QNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports removing children (files); + this method should then process this QNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html Qt Network Documentation\endlink which is describes + in detail how to reimplement this method. You may also want to + look at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void QNetworkProtocol::operationRemove( QNetworkOperation * ) +{ +} + +/*! + When implementing a new newtork protocol, this method should be + reimplemented if the protocol supports renaming children (files); + this method should then process this QNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html Qt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void QNetworkProtocol::operationRename( QNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports getting data; this method + should then process the QNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html Qt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void QNetworkProtocol::operationGet( QNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports putting (uploading) data; + this method should then process the QNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html Qt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void QNetworkProtocol::operationPut( QNetworkOperation * ) +{ +} + +/*! \internal +*/ + +void QNetworkProtocol::operationPutChunk( QNetworkOperation * ) +{ +} + +/*! + \internal + Handles operations. Deletes the previous operation object and + tries to process the next operation. It also checks the connection state + and only processes the next operation, if the connection of the protocol + is open. Otherwise it waits until the protocol opens the connection. +*/ + +void QNetworkProtocol::processNextOperation( QNetworkOperation *old ) +{ +#ifdef QNETWORKPROTOCOL_DEBUG + qDebug( "QNetworkOperation: process next operation, old: %p", old ); +#endif + d->removeTimer->stop(); + + if ( old ) + d->oldOps.append( old ); + if ( d->opInProgress && d->opInProgress!=old ) + d->oldOps.append( d->opInProgress ); + + if ( d->operationQueue.isEmpty() ) { + d->opInProgress = 0; + if ( d->autoDelete ) + d->removeTimer->start( d->removeInterval, TRUE ); + return; + } + + QNetworkOperation *op = d->operationQueue.head(); + + d->opInProgress = op; + + if ( !checkConnection( op ) ) { + if ( op->state() != QNetworkProtocol::StFailed ) { + d->opStartTimer->start( 0, TRUE ); + } else { + d->operationQueue.dequeue(); + clearOperationQueue(); + emit finished( op ); + } + + return; + } + + d->opInProgress = op; + d->operationQueue.dequeue(); + processOperation( op ); +} + +/*! + Returns the QUrlOperator on which the protocol works. +*/ + +QUrlOperator *QNetworkProtocol::url() const +{ + return d->url; +} + +/*! + Returns the operation, which is being processed, or 0 of no + operation is being processed at the moment. +*/ + +QNetworkOperation *QNetworkProtocol::operationInProgress() const +{ + return d->opInProgress; +} + +/*! + Clears the operation queue. +*/ + +void QNetworkProtocol::clearOperationQueue() +{ + d->operationQueue.dequeue(); + d->operationQueue.setAutoDelete( TRUE ); + d->operationQueue.clear(); +} + +/*! + Stops the current operation that is being processed and clears all + waiting operations. +*/ + +void QNetworkProtocol::stop() +{ + QNetworkOperation *op = d->opInProgress; + clearOperationQueue(); + if ( op ) { + op->setState( StStopped ); + op->setProtocolDetail( tr( "Operation stopped by the user" ) ); + emit finished( op ); + setUrl( 0 ); + op->free(); + } +} + +/*! + Because it's sometimes hard to take care of removing network + protocol instances, QNetworkProtocol provides an auto-delete + mechanism. If you set \a b to TRUE, the network protocol instance + is removed after it has been inactive for \a i milliseconds (i.e. + \a i milliseconds after the last operation has been processed). + If you set \a b to FALSE the auto-delete mechanism is switched + off. + + If you switch on auto-delete, the QNetworkProtocol also deletes + its QUrlOperator. +*/ + +void QNetworkProtocol::setAutoDelete( bool b, int i ) +{ + d->autoDelete = b; + d->removeInterval = i; +} + +/*! + Returns TRUE if auto-deleting is enabled; otherwise returns FALSE. + + \sa QNetworkProtocol::setAutoDelete() +*/ + +bool QNetworkProtocol::autoDelete() const +{ + return d->autoDelete; +} + +/*! + \internal +*/ + +void QNetworkProtocol::removeMe() +{ + if ( d->autoDelete ) { +#ifdef QNETWORKPROTOCOL_DEBUG + qDebug( "QNetworkOperation: autodelete of QNetworkProtocol %p", this ); +#endif + delete d->url; // destructor deletes the network protocol + } +} + +void QNetworkProtocol::emitNewChildren( const QUrlInfo &i, QNetworkOperation *op ) +{ + QValueList<QUrlInfo> lst; + lst << i; + emit newChildren( lst, op ); +} + +class QNetworkOperationPrivate +{ +public: + QNetworkProtocol::Operation operation; + QNetworkProtocol::State state; + QMap<int, QString> args; + QMap<int, QByteArray> rawArgs; + QString protocolDetail; + int errorCode; + QTimer *deleteTimer; +}; + +/*! + \class QNetworkOperation + + \brief The QNetworkOperation class provides common operations for network protocols. +\if defined(commercial) + It is part of the <a href="commercialeditions.html">Qt Enterprise Edition</a>. +\endif + + \module network + \ingroup io + + An object is created to describe the operation and the current + state for each operation that a network protocol should process. + + For a detailed description of the Qt Network Architecture and how + to implement and use network protocols in Qt, see the \link + network.html Qt Network Documentation\endlink. + + \sa QNetworkProtocol +*/ + +/*! + Constructs a network operation object. \a operation is the type of + the operation, and \a arg0, \a arg1 and \a arg2 are the first + three arguments of the operation. The state is initialized to + QNetworkProtocol::StWaiting. + + \sa QNetworkProtocol::Operation QNetworkProtocol::State +*/ + +QNetworkOperation::QNetworkOperation( QNetworkProtocol::Operation operation, + const QString &arg0, const QString &arg1, + const QString &arg2 ) +{ + d = new QNetworkOperationPrivate; + d->deleteTimer = new QTimer( this ); + connect( d->deleteTimer, SIGNAL( timeout() ), + this, SLOT( deleteMe() ) ); + d->operation = operation; + d->state = QNetworkProtocol::StWaiting; + d->args[ 0 ] = arg0; + d->args[ 1 ] = arg1; + d->args[ 2 ] = arg2; + d->rawArgs[ 0 ] = QByteArray( 0 ); + d->rawArgs[ 1 ] = QByteArray( 0 ); + d->rawArgs[ 2 ] = QByteArray( 0 ); + d->protocolDetail = QString::null; + d->errorCode = (int)QNetworkProtocol::NoError; +} + +/*! + Constructs a network operation object. \a operation is the type of + the operation, and \a arg0, \a arg1 and \a arg2 are the first + three raw data arguments of the operation. The state is + initialized to QNetworkProtocol::StWaiting. + + \sa QNetworkProtocol::Operation QNetworkProtocol::State +*/ + +QNetworkOperation::QNetworkOperation( QNetworkProtocol::Operation operation, + const QByteArray &arg0, const QByteArray &arg1, + const QByteArray &arg2 ) +{ + d = new QNetworkOperationPrivate; + d->deleteTimer = new QTimer( this ); + connect( d->deleteTimer, SIGNAL( timeout() ), + this, SLOT( deleteMe() ) ); + d->operation = operation; + d->state = QNetworkProtocol::StWaiting; + d->args[ 0 ] = QString::null; + d->args[ 1 ] = QString::null; + d->args[ 2 ] = QString::null; + d->rawArgs[ 0 ] = arg0; + d->rawArgs[ 1 ] = arg1; + d->rawArgs[ 2 ] = arg2; + d->protocolDetail = QString::null; + d->errorCode = (int)QNetworkProtocol::NoError; +} + +/*! + Destructor. +*/ + +QNetworkOperation::~QNetworkOperation() +{ + delete d; +} + +/*! + Sets the \a state of the operation object. This should be done by + the network protocol during processing; at the end it should be + set to QNetworkProtocol::StDone or QNetworkProtocol::StFailed, + depending on success or failure. + + \sa QNetworkProtocol::State +*/ + +void QNetworkOperation::setState( QNetworkProtocol::State state ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->state = state; +} + +/*! + If the operation failed, the error message can be specified as \a + detail. +*/ + +void QNetworkOperation::setProtocolDetail( const QString &detail ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->protocolDetail = detail; +} + +/*! + Sets the error code to \a ec. + + If the operation failed, the protocol should set an error code to + describe the error in more detail. If possible, one of the error + codes defined in QNetworkProtocol should be used. + + \sa setProtocolDetail() QNetworkProtocol::Error +*/ + +void QNetworkOperation::setErrorCode( int ec ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->errorCode = ec; +} + +/*! + Sets the network operation's \a{num}-th argument to \a arg. +*/ + +void QNetworkOperation::setArg( int num, const QString &arg ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->args[ num ] = arg; +} + +/*! + Sets the network operation's \a{num}-th raw data argument to \a arg. +*/ + +void QNetworkOperation::setRawArg( int num, const QByteArray &arg ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->rawArgs[ num ] = arg; +} + +/*! + Returns the type of the operation. +*/ + +QNetworkProtocol::Operation QNetworkOperation::operation() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->operation; +} + +/*! + Returns the state of the operation. You can determine whether an + operation is still waiting to be processed, is being processed, + has been processed successfully, or failed. +*/ + +QNetworkProtocol::State QNetworkOperation::state() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->state; +} + +/*! + Returns the operation's \a{num}-th argument. If this argument was + not already set, an empty string is returned. +*/ + +QString QNetworkOperation::arg( int num ) const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->args[ num ]; +} + +/*! + Returns the operation's \a{num}-th raw data argument. If this + argument was not already set, an empty bytearray is returned. +*/ + +QByteArray QNetworkOperation::rawArg( int num ) const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->rawArgs[ num ]; +} + +/*! + Returns a detailed error message for the last error. This must + have been set using setProtocolDetail(). +*/ + +QString QNetworkOperation::protocolDetail() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->protocolDetail; +} + +/*! + Returns the error code for the last error that occurred. +*/ + +int QNetworkOperation::errorCode() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->errorCode; +} + +/*! + \internal +*/ + +QByteArray& QNetworkOperation::raw( int num ) const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->rawArgs[ num ]; +} + +/*! + Sets this object to delete itself when it hasn't been used for one + second. + + Because QNetworkOperation pointers are passed around a lot the + QNetworkProtocol generally does not have enough knowledge to + delete these at the correct time. If a QNetworkProtocol doesn't + need an operation any more it will call this function instead. + + Note: you should never need to call the method yourself. +*/ + +void QNetworkOperation::free() +{ + d->deleteTimer->start( NETWORK_OP_DELAY ); +} + +/*! + \internal + Internal slot for auto-deletion. +*/ + +void QNetworkOperation::deleteMe() +{ + delete this; +} + +#endif diff --git a/src/kernel/qnetworkprotocol.h b/src/kernel/qnetworkprotocol.h new file mode 100644 index 0000000..096a9ad --- /dev/null +++ b/src/kernel/qnetworkprotocol.h @@ -0,0 +1,244 @@ +/**************************************************************************** +** +** Definition of QNetworkProtocol class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QNETWORKPROTOCOL_H +#define QNETWORKPROTOCOL_H + +#ifndef QT_H +#include "qurlinfo.h" +#include "qstring.h" +#include "qdict.h" +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_NETWORKPROTOCOL + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +class QNetworkProtocol; +class QNetworkOperation; +class QTimer; +class QUrlOperator; +class QNetworkProtocolPrivate; +template <class T> class QValueList; + +class Q_EXPORT QNetworkProtocolFactoryBase +{ +public: + virtual QNetworkProtocol *createObject() = 0; + +}; + +template< class T > +class QNetworkProtocolFactory : public QNetworkProtocolFactoryBase +{ +public: + QNetworkProtocol *createObject() { + return new T; + } + +}; + +typedef QDict< QNetworkProtocolFactoryBase > QNetworkProtocolDict; + +class Q_EXPORT QNetworkProtocol : public QObject +{ + Q_OBJECT + +public: + enum State { + StWaiting = 0, + StInProgress, + StDone, + StFailed, + StStopped + }; + + enum Operation { + OpListChildren = 1, + OpMkDir = 2, + OpMkdir = OpMkDir, // ### remove in 4.0 + OpRemove = 4, + OpRename = 8, + OpGet = 32, + OpPut = 64 + }; + + enum ConnectionState { + ConHostFound, + ConConnected, + ConClosed + }; + + enum Error { + // no error + NoError = 0, + // general errors + ErrValid, + ErrUnknownProtocol, + ErrUnsupported, + ErrParse, + // errors on connect + ErrLoginIncorrect, + ErrHostNotFound, + // protocol errors + ErrListChildren, + ErrListChlidren = ErrListChildren, // ### remove in 4.0 + ErrMkDir, + ErrMkdir = ErrMkDir, // ### remove in 4.0 + ErrRemove, + ErrRename, + ErrGet, + ErrPut, + ErrFileNotExisting, + ErrPermissionDenied + }; + + QNetworkProtocol(); + virtual ~QNetworkProtocol(); + + virtual void setUrl( QUrlOperator *u ); + + virtual void setAutoDelete( bool b, int i = 10000 ); + bool autoDelete() const; + + static void registerNetworkProtocol( const QString &protocol, + QNetworkProtocolFactoryBase *protocolFactory ); + static QNetworkProtocol *getNetworkProtocol( const QString &protocol ); + static bool hasOnlyLocalFileSystem(); + + virtual int supportedOperations() const; + virtual void addOperation( QNetworkOperation *op ); + + QUrlOperator *url() const; + QNetworkOperation *operationInProgress() const; + virtual void clearOperationQueue(); + virtual void stop(); + +signals: + void data( const QByteArray &, QNetworkOperation *res ); + void connectionStateChanged( int state, const QString &data ); + void finished( QNetworkOperation *res ); + void start( QNetworkOperation *res ); + void newChildren( const QValueList<QUrlInfo> &, QNetworkOperation *res ); + void newChild( const QUrlInfo &, QNetworkOperation *res ); + void createdDirectory( const QUrlInfo &, QNetworkOperation *res ); + void removed( QNetworkOperation *res ); + void itemChanged( QNetworkOperation *res ); + void dataTransferProgress( int bytesDone, int bytesTotal, QNetworkOperation *res ); + +protected: + virtual void processOperation( QNetworkOperation *op ); + virtual void operationListChildren( QNetworkOperation *op ); + virtual void operationMkDir( QNetworkOperation *op ); + virtual void operationRemove( QNetworkOperation *op ); + virtual void operationRename( QNetworkOperation *op ); + virtual void operationGet( QNetworkOperation *op ); + virtual void operationPut( QNetworkOperation *op ); + virtual void operationPutChunk( QNetworkOperation *op ); + virtual bool checkConnection( QNetworkOperation *op ); + +private: + QNetworkProtocolPrivate *d; + +private slots: + void processNextOperation( QNetworkOperation *old ); + void startOps(); + void emitNewChildren( const QUrlInfo &i, QNetworkOperation *op ); + + void removeMe(); + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QNetworkProtocol( const QNetworkProtocol & ); + QNetworkProtocol &operator=( const QNetworkProtocol & ); +#endif +}; + +class QNetworkOperationPrivate; + +class Q_EXPORT QNetworkOperation : public QObject +{ + Q_OBJECT + friend class QUrlOperator; + +public: + QNetworkOperation( QNetworkProtocol::Operation operation, + const QString &arg0, const QString &arg1, + const QString &arg2 ); + QNetworkOperation( QNetworkProtocol::Operation operation, + const QByteArray &arg0, const QByteArray &arg1, + const QByteArray &arg2 ); + ~QNetworkOperation(); + + void setState( QNetworkProtocol::State state ); + void setProtocolDetail( const QString &detail ); + void setErrorCode( int ec ); + void setArg( int num, const QString &arg ); + void setRawArg( int num, const QByteArray &arg ); + + QNetworkProtocol::Operation operation() const; + QNetworkProtocol::State state() const; + QString arg( int num ) const; + QByteArray rawArg( int num ) const; + QString protocolDetail() const; + int errorCode() const; + + void free(); + +private slots: + void deleteMe(); + +private: + QByteArray &raw( int num ) const; + QNetworkOperationPrivate *d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QNetworkOperation( const QNetworkOperation & ); + QNetworkOperation &operator=( const QNetworkOperation & ); +#endif +}; + +#endif // QT_NO_NETWORKPROTOCOL + +#endif // QNETWORKPROTOCOL_H diff --git a/src/kernel/qobject.cpp b/src/kernel/qobject.cpp new file mode 100644 index 0000000..7790676 --- /dev/null +++ b/src/kernel/qobject.cpp @@ -0,0 +1,2711 @@ +/**************************************************************************** +** +** Implementation of QObject class +** +** Created : 930418 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qvariant.h" +#include "qapplication.h" +#include "qobject.h" +#include "qobjectlist.h" +#include "qsignalslotimp.h" +#include "qregexp.h" +#include "qmetaobject.h" +#include <private/qucom_p.h> +#include "qucomextra_p.h" +#include "qptrvector.h" + +#ifdef QT_THREAD_SUPPORT +#include <qmutex.h> +#include <private/qmutexpool_p.h> +#endif + +#include <ctype.h> + + +#ifndef QT_NO_USERDATA +class QObjectPrivate : public QPtrVector<QObjectUserData> +{ +public: + QObjectPrivate( uint s ) : QPtrVector<QObjectUserData>(s){ setAutoDelete( TRUE ); } +}; +#else +class QObjectPrivate { +} +#endif + +class QSenderObjectList : public QObjectList, public QShared +{ +public: + QSenderObjectList() : currentSender( 0 ) { } + QObject *currentSender; +}; + +/*! + \class Qt qnamespace.h + + \brief The Qt class is a namespace for miscellaneous identifiers + that need to be global-like. + + \ingroup misc + + Normally, you can ignore this class. QObject and a few other + classes inherit it, so all the identifiers in the Qt namespace are + normally usable without qualification. + + However, you may occasionally need to say \c Qt::black instead of + just \c black, particularly in static utility functions (such as + many class factories). + +*/ + +/*! + \enum Qt::Orientation + + This type is used to signify an object's orientation. + + \value Horizontal + \value Vertical + + Orientation is used with QScrollBar for example. +*/ + + +/*! + \class QObject qobject.h + \brief The QObject class is the base class of all Qt objects. + + \ingroup objectmodel + \mainclass + \reentrant + + QObject is the heart of the \link object.html Qt object model. + \endlink The central feature in this model is a very powerful + mechanism for seamless object communication called \link + signalsandslots.html signals and slots \endlink. You can + connect a signal to a slot with connect() and destroy the + connection with disconnect(). To avoid never ending notification + loops you can temporarily block signals with blockSignals(). The + protected functions connectNotify() and disconnectNotify() make it + possible to track connections. + + QObjects organize themselves in object trees. When you create a + QObject with another object as parent, the object will + automatically do an insertChild() on the parent and thus show up + in the parent's children() list. The parent takes ownership of the + object i.e. it will automatically delete its children in its + destructor. You can look for an object by name and optionally type + using child() or queryList(), and get the list of tree roots using + objectTrees(). + + Every object has an object name() and can report its className() + and whether it inherits() another class in the QObject inheritance + hierarchy. + + When an object is deleted, it emits a destroyed() signal. You can + catch this signal to avoid dangling references to QObjects. The + QGuardedPtr class provides an elegant way to use this feature. + + QObjects can receive events through event() and filter the events + of other objects. See installEventFilter() and eventFilter() for + details. A convenience handler, childEvent(), can be reimplemented + to catch child events. + + Last but not least, QObject provides the basic timer support in + Qt; see QTimer for high-level support for timers. + + Notice that the Q_OBJECT macro is mandatory for any object that + implements signals, slots or properties. You also need to run the + \link moc.html moc program (Meta Object Compiler) \endlink on the + source file. We strongly recommend the use of this macro in \e all + subclasses of QObject regardless of whether or not they actually + use signals, slots and properties, since failure to do so may lead + certain functions to exhibit undefined behaviour. + + All Qt widgets inherit QObject. The convenience function + isWidgetType() returns whether an object is actually a widget. It + is much faster than inherits( "QWidget" ). + + Some QObject functions, e.g. children(), objectTrees() and + queryList() return a QObjectList. A QObjectList is a QPtrList of + QObjects. QObjectLists support the same operations as QPtrLists + and have an iterator class, QObjectListIt. +*/ + + +// +// Remove white space from SIGNAL and SLOT names. +// Internal for QObject::connect() and QObject::disconnect() +// + +static inline bool isIdentChar( char x ) +{ // Avoid bug in isalnum + return x == '_' || (x >= '0' && x <= '9') || + (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); +} + +static inline bool isSpace( char x ) +{ +#if defined(Q_CC_BOR) + /* + Borland C++ 4.5 has a weird isspace() bug. + isspace() usually works, but not here. + This implementation is sufficient for our internal use: rmWS() + */ + return (uchar) x <= 32; +#else + return isspace( (uchar) x ); +#endif +} + +static QCString qt_rmWS( const char *s ) +{ + QCString result( qstrlen(s)+1 ); + char *d = result.data(); + char last = 0; + while( *s && isSpace(*s) ) // skip leading space + s++; + while ( *s ) { + while ( *s && !isSpace(*s) ) + last = *d++ = *s++; + while ( *s && isSpace(*s) ) + s++; + if ( *s && isIdentChar(*s) && isIdentChar(last) ) + last = *d++ = ' '; + } + *d = '\0'; + result.truncate( (int)(d - result.data()) ); + int void_pos = result.find("(void)"); + if ( void_pos >= 0 ) + result.remove( void_pos+1, (uint)strlen("void") ); + return result; +} + + +// Event functions, implemented in qapplication_xxx.cpp + +int qStartTimer( int interval, QObject *obj ); +bool qKillTimer( int id ); +bool qKillTimer( QObject *obj ); + +static void removeObjFromList( QObjectList *objList, const QObject *obj, + bool single=FALSE ) +{ + if ( !objList ) + return; + int index = objList->findRef( obj ); + while ( index >= 0 ) { + objList->remove(); + if ( single ) + return; + index = objList->findNextRef( obj ); + } +} + + +/*! + \relates QObject + + Returns a pointer to the object named \a name that inherits \a + type and with a given \a parent. + + Returns 0 if there is no such child. + + \code + QListBox *c = (QListBox *) qt_find_obj_child( myWidget, "QListBox", + "my list box" ); + if ( c ) + c->insertItem( "another string" ); + \endcode +*/ + +void *qt_find_obj_child( QObject *parent, const char *type, const char *name ) +{ + const QObjectList *list = parent->children(); + if ( list ) { + QObjectListIt it( *list ); + QObject *obj; + while ( (obj = it.current()) ) { + ++it; + if ( qstrcmp(name,obj->name()) == 0 && + obj->inherits(type) ) + return obj; + } + } + return 0; +} + + + +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY +/* + Preliminary signal spy + */ +Q_EXPORT QObject* qt_preliminary_signal_spy = 0; +static QObject* qt_spy_signal_sender = 0; + +static void qt_spy_signal( QObject* sender, int signal, QUObject* o ) +{ + QMetaObject* mo = sender->metaObject(); + while ( mo && signal - mo->signalOffset() < 0 ) + mo = mo->superClass(); + if ( !mo ) + return; + const QMetaData* sigData = mo->signal( signal - mo->signalOffset() ); + if ( !sigData ) + return; + QCString s; + mo = sender->metaObject(); + while ( mo ) { + s.sprintf( "%s_%s", mo->className(), sigData->name ); + int slot = qt_preliminary_signal_spy->metaObject()->findSlot( s, TRUE ); + if ( slot >= 0 ) { +#ifdef QT_THREAD_SUPPORT + // protect access to qt_spy_signal_sender + void * const address = &qt_spy_signal_sender; + QMutexLocker locker( qt_global_mutexpool ? + qt_global_mutexpool->get( address ) : 0 ); +#endif // QT_THREAD_SUPPORT + + QObject* old_sender = qt_spy_signal_sender; + qt_spy_signal_sender = sender; + qt_preliminary_signal_spy->qt_invoke( slot, o ); + qt_spy_signal_sender = old_sender; + break; + } + mo = mo->superClass(); + } +} + +/* + End Preliminary signal spy + */ +#endif // QT_NO_PRELIMINARY_SIGNAL_SPY + +static QObjectList* object_trees = 0; + +#ifdef QT_THREAD_SUPPORT +static QMutex *obj_trees_mutex = 0; +#endif + +static void cleanup_object_trees() +{ + delete object_trees; + object_trees = 0; +#ifdef QT_THREAD_SUPPORT + delete obj_trees_mutex; + obj_trees_mutex = 0; +#endif +} + +static void ensure_object_trees() +{ + object_trees = new QObjectList; + qAddPostRoutine( cleanup_object_trees ); +} + +static void insert_tree( QObject* obj ) +{ +#ifdef QT_THREAD_SUPPORT + if ( !obj_trees_mutex ) + obj_trees_mutex = new QMutex(); + QMutexLocker locker( obj_trees_mutex ); +#endif + if ( !object_trees ) + ensure_object_trees(); + object_trees->insert(0, obj ); +} + +static void remove_tree( QObject* obj ) +{ + if ( object_trees ) { +#ifdef QT_THREAD_SUPPORT + QMutexLocker locker( obj_trees_mutex ); +#endif + object_trees->removeRef( obj ); + } +} + +/*! \internal + TQt compatibility function +*/ +QObjectList QObject::childrenListObject() { + if (children()) return *(children()); + else return QObjectList(); +} + +/*! \internal + TQt compatibility function +*/ +const QObjectList QObject::childrenListObject() const { + if (children()) return *(children()); + else return QObjectList(); +} + +/*! \internal + TQt compatibility function +*/ +const QObjectList QObject::objectTreesListObject() { + if (objectTrees()) return *(objectTrees()); + else return QObjectList(); +} + + +/***************************************************************************** + QObject member functions + *****************************************************************************/ + +/*! + Constructs an object called \a name with parent object, \a parent. + + The parent of an object may be viewed as the object's owner. For + instance, a \link QDialog dialog box\endlink is the parent of the + "OK" and "Cancel" buttons it contains. + + The destructor of a parent object destroys all child objects. + + Setting \a parent to 0 constructs an object with no parent. If the + object is a widget, it will become a top-level window. + + The object name is some text that can be used to identify a + QObject. It's particularly useful in conjunction with \link + designer-manual.book <i>Qt Designer</i>\endlink. You can find an + object by name (and type) using child(). To find several objects + use queryList(). + + \sa parent(), name(), child(), queryList() +*/ + +QObject::QObject( QObject *parent, const char *name ) + : + isSignal( FALSE ), // assume not a signal object + isWidget( FALSE ), // assume not a widget object + pendTimer( FALSE ), // no timers yet + blockSig( FALSE ), // not blocking signals + wasDeleted( FALSE ), // double-delete catcher + isTree( FALSE ), // no tree yet + objname( name ? qstrdup(name) : 0 ), // set object name + parentObj( 0 ), // no parent yet. It is set by insertChild() + childObjects( 0 ), // no children yet + connections( 0 ), // no connections yet + senderObjects( 0 ), // no signals connected yet + eventFilters( 0 ), // no filters installed + postedEvents( 0 ), // no events posted + d( 0 ) +{ + if ( !metaObj ) // will create object dict + (void) staticMetaObject(); + + if ( parent ) { // add object to parent + parent->insertChild( this ); + } else { + insert_tree( this ); + isTree = TRUE; + } +} + + +/*! + Destroys the object, deleting all its child objects. + + All signals to and from the object are automatically disconnected. + + \warning All child objects are deleted. If any of these objects + are on the stack or global, sooner or later your program will + crash. We do not recommend holding pointers to child objects from + outside the parent. If you still do, the QObject::destroyed() + signal gives you an opportunity to detect when an object is + destroyed. + + \warning Deleting a QObject while pending events are waiting to be + delivered can cause a crash. You must not delete the QObject + directly from a thread that is not the GUI thread. Use the + QObject::deleteLater() method instead, which will cause the event + loop to delete the object after all pending events have been + delivered to the object. +*/ + +QObject::~QObject() +{ + if ( wasDeleted ) { +#if defined(QT_DEBUG) + qWarning( "Double QObject deletion detected." ); +#endif + return; + } + wasDeleted = 1; + blockSig = 0; // unblock signals to keep QGuardedPtr happy + emit destroyed( this ); + emit destroyed(); + if ( objname ) + delete [] (char*)objname; + objname = 0; + if ( pendTimer ) // might be pending timers + qKillTimer( this ); + QApplication::removePostedEvents( this ); + if ( isTree ) { + remove_tree( this ); // remove from global root list + isTree = FALSE; + } + if ( parentObj ) // remove it from parent object + parentObj->removeChild( this ); + register QObject *obj; + if ( senderObjects ) { // disconnect from senders + QSenderObjectList *tmp = senderObjects; + senderObjects = 0; + obj = tmp->first(); + while ( obj ) { // for all senders... + obj->disconnect( this ); + obj = tmp->next(); + } + if ( tmp->deref() ) + delete tmp; + } + if ( connections ) { // disconnect receivers + for ( int i = 0; i < (int) connections->size(); i++ ) { + QConnectionList* clist = (*connections)[i]; // for each signal... + if ( !clist ) + continue; + register QConnection *c; + QConnectionListIt cit(*clist); + while( (c=cit.current()) ) { // for each connected slot... + ++cit; + if ( (obj=c->object()) ) + removeObjFromList( obj->senderObjects, this ); + } + } + delete connections; + connections = 0; + } + if ( eventFilters ) { + delete eventFilters; + eventFilters = 0; + } + if ( childObjects ) { // delete children objects + QObjectListIt it(*childObjects); + while ( (obj=it.current()) ) { + ++it; + obj->parentObj = 0; + childObjects->removeRef( obj ); + delete obj; + } + delete childObjects; + } + + delete d; +} + + +/*! + \fn QMetaObject *QObject::metaObject() const + + Returns a pointer to the meta object of this object. + + A meta object contains information about a class that inherits + QObject, e.g. class name, superclass name, properties, signals and + slots. Every class that contains the Q_OBJECT macro will also have + a meta object. + + The meta object information is required by the signal/slot + connection mechanism and the property system. The functions isA() + and inherits() also make use of the meta object. +*/ + +/*! + \fn const char *QObject::className() const + + Returns the class name of this object. + + This function is generated by the \link metaobjects.html Meta + Object Compiler. \endlink + + \warning This function will return the wrong name if the class + definition lacks the Q_OBJECT macro. + + \sa name(), inherits(), isA(), isWidgetType() +*/ + +/*! + Returns TRUE if this object is an instance of the class \a clname; + otherwise returns FALSE. + + Example: + \code + QTimer *t = new QTimer; // QTimer inherits QObject + t->isA( "QTimer" ); // returns TRUE + t->isA( "QObject" ); // returns FALSE + \endcode + + \sa inherits() metaObject() +*/ + +bool QObject::isA( const char *clname ) const +{ + return qstrcmp( clname, className() ) == 0; +} + +/*! + Returns TRUE if this object is an instance of a class that + inherits \a clname, and \a clname inherits QObject; otherwise + returns FALSE. + + A class is considered to inherit itself. + + Example: + \code + QTimer *t = new QTimer; // QTimer inherits QObject + t->inherits( "QTimer" ); // returns TRUE + t->inherits( "QObject" ); // returns TRUE + t->inherits( "QButton" ); // returns FALSE + + // QScrollBar inherits QWidget and QRangeControl + QScrollBar *s = new QScrollBar( 0 ); + s->inherits( "QWidget" ); // returns TRUE + s->inherits( "QRangeControl" ); // returns FALSE + \endcode + + (\l QRangeControl is not a QObject.) + + \sa isA(), metaObject() +*/ + +bool QObject::inherits( const char *clname ) const +{ + return metaObject()->inherits( clname ); +} + +/*! + \internal + + Returns TRUE if \a object inherits \a superClass within + the meta object inheritance chain; otherwise returns FALSE. + + \sa inherits() +*/ +void *qt_inheritedBy( QMetaObject *superClass, const QObject *object ) +{ + if (!object) + return 0; + register QMetaObject *mo = object->metaObject(); + while (mo) { + if (mo == superClass) + return (void*)object; + mo = mo->superClass(); + } + return 0; +} + +/*! + \property QObject::name + + \brief the name of this object + + You can find an object by name (and type) using child(). You can + find a set of objects with queryList(). + + The object name is set by the constructor or by the setName() + function. The object name is not very useful in the current + version of Qt, but will become increasingly important in the + future. + + If the object does not have a name, the name() function returns + "unnamed", so printf() (used in qDebug()) will not be asked to + output a null pointer. If you want a null pointer to be returned + for unnamed objects, you can call name( 0 ). + + \code + qDebug( "MyClass::setPrecision(): (%s) invalid precision %f", + name(), newPrecision ); + \endcode + + \sa className(), child(), queryList() +*/ + +const char * QObject::name() const +{ + // If you change the name here, the builder will be broken + return objname ? objname : "unnamed"; +} + +/*! + Sets the object's name to \a name. +*/ +void QObject::setName( const char *name ) +{ + if ( objname ) + delete [] (char*) objname; + objname = name ? qstrdup(name) : 0; +} + +/*! + \overload + + Returns the name of this object, or \a defaultName if the object + does not have a name. +*/ + +const char * QObject::name( const char * defaultName ) const +{ + return objname ? objname : defaultName; +} + + +/*! + Searches the children and optionally grandchildren of this object, + and returns a child that is called \a objName that inherits \a + inheritsClass. If \a inheritsClass is 0 (the default), any class + matches. + + If \a recursiveSearch is TRUE (the default), child() performs a + depth-first search of the object's children. + + If there is no such object, this function returns 0. If there are + more than one, the first one found is retured; if you need all of + them, use queryList(). +*/ +QObject* QObject::child( const char *objName, const char *inheritsClass, + bool recursiveSearch ) +{ + const QObjectList *list = children(); + if ( !list ) + return 0; + + bool onlyWidgets = ( inheritsClass && qstrcmp( inheritsClass, "QWidget" ) == 0 ); + QObjectListIt it( *list ); + QObject *obj; + while ( ( obj = it.current() ) ) { + ++it; + if ( onlyWidgets ) { + if ( obj->isWidgetType() && ( !objName || qstrcmp( objName, obj->name() ) == 0 ) ) + break; + } else if ( ( !inheritsClass || obj->inherits(inheritsClass) ) && ( !objName || qstrcmp( objName, obj->name() ) == 0 ) ) + break; + if ( recursiveSearch && (obj = obj->child( objName, inheritsClass, recursiveSearch ) ) ) + break; + } + return obj; +} + +/*! + \fn bool QObject::isWidgetType() const + + Returns TRUE if the object is a widget; otherwise returns FALSE. + + Calling this function is equivalent to calling + inherits("QWidget"), except that it is much faster. +*/ + +/*! + \fn bool QObject::highPriority() const + + Returns TRUE if the object is a high-priority object, or FALSE if + it is a standard-priority object. + + High-priority objects are placed first in QObject's list of + children on the assumption that they will be referenced very + often. +*/ + + +/*! + This virtual function receives events to an object and should + return TRUE if the event \a e was recognized and processed. + + The event() function can be reimplemented to customize the + behavior of an object. + + \sa installEventFilter(), timerEvent(), QApplication::sendEvent(), + QApplication::postEvent(), QWidget::event() +*/ + +bool QObject::event( QEvent *e ) +{ +#if defined(QT_CHECK_NULL) + if ( e == 0 ) + qWarning( "QObject::event: Null events are not permitted" ); +#endif + if ( eventFilters ) { // try filters + if ( activate_filters(e) ) // stopped by a filter + return TRUE; + } + + switch ( e->type() ) { + case QEvent::Timer: + timerEvent( (QTimerEvent*)e ); + return TRUE; + + case QEvent::ChildInserted: + case QEvent::ChildRemoved: + childEvent( (QChildEvent*)e ); + return TRUE; + + case QEvent::DeferredDelete: + delete this; + return TRUE; + + default: + if ( e->type() >= QEvent::User ) { + customEvent( (QCustomEvent*) e ); + return TRUE; + } + break; + } + return FALSE; +} + +/*! + This event handler can be reimplemented in a subclass to receive + timer events for the object. + + QTimer provides a higher-level interface to the timer + functionality, and also more general information about timers. + + \sa startTimer(), killTimer(), killTimers(), event() +*/ + +void QObject::timerEvent( QTimerEvent * ) +{ +} + + +/*! + This event handler can be reimplemented in a subclass to receive + child events. + + Child events are sent to objects when children are inserted or + removed. + + Note that events with QEvent::type() \c QEvent::ChildInserted are + posted (with \l{QApplication::postEvent()}) to make sure that the + child's construction is completed before this function is called. + + If a child is removed immediately after it is inserted, the \c + ChildInserted event may be suppressed, but the \c ChildRemoved + event will always be sent. In such cases it is possible that there + will be a \c ChildRemoved event without a corresponding \c + ChildInserted event. + + If you change state based on \c ChildInserted events, call + QWidget::constPolish(), or do + \code + QApplication::sendPostedEvents( this, QEvent::ChildInserted ); + \endcode + in functions that depend on the state. One notable example is + QWidget::sizeHint(). + + \sa event(), QChildEvent +*/ + +void QObject::childEvent( QChildEvent * ) +{ +} + +/*! + This event handler can be reimplemented in a subclass to receive + custom events. Custom events are user-defined events with a type + value at least as large as the "User" item of the \l QEvent::Type + enum, and is typically a QCustomEvent or QCustomEvent subclass. + + \sa event(), QCustomEvent +*/ +void QObject::customEvent( QCustomEvent * ) +{ +} + + + +/*! + Filters events if this object has been installed as an event + filter for the \a watched object. + + In your reimplementation of this function, if you want to filter + the event \a e, out, i.e. stop it being handled further, return + TRUE; otherwise return FALSE. + + Example: + \code + class MyMainWindow : public QMainWindow + { + public: + MyMainWindow( QWidget *parent = 0, const char *name = 0 ); + + protected: + bool eventFilter( QObject *obj, QEvent *ev ); + + private: + QTextEdit *textEdit; + }; + + MyMainWindow::MyMainWindow( QWidget *parent, const char *name ) + : QMainWindow( parent, name ) + { + textEdit = new QTextEdit( this ); + setCentralWidget( textEdit ); + textEdit->installEventFilter( this ); + } + + bool MyMainWindow::eventFilter( QObject *obj, QEvent *ev ) + { + if ( obj == textEdit ) { + if ( e->type() == QEvent::KeyPress ) { + QKeyEvent *k = (QKeyEvent*)ev; + qDebug( "Ate key press %d", k->key() ); + return TRUE; + } else { + return FALSE; + } + } else { + // pass the event on to the parent class + return QMainWindow::eventFilter( obj, ev ); + } + } + \endcode + + Notice in the example above that unhandled events are passed to + the base class's eventFilter() function, since the base class + might have reimplemented eventFilter() for its own internal + purposes. + + \warning If you delete the receiver object in this function, be + sure to return TRUE. Otherwise, Qt will forward the event to the + deleted object and the program might crash. + + \sa installEventFilter() +*/ + +bool QObject::eventFilter( QObject * /* watched */, QEvent * /* e */ ) +{ + return FALSE; +} + + +/*! + \internal + Activates all event filters for this object. + This function is normally called from QObject::event() or QWidget::event(). +*/ + +bool QObject::activate_filters( QEvent *e ) +{ + if ( !eventFilters ) // no event filter + return FALSE; + QObjectListIt it( *eventFilters ); + register QObject *obj = it.current(); + while ( obj ) { // send to all filters + ++it; // until one returns TRUE + if ( obj->eventFilter(this,e) ) { + return TRUE; + } + obj = it.current(); + } + return FALSE; // don't do anything with it +} + + +/*! + \fn bool QObject::signalsBlocked() const + + Returns TRUE if signals are blocked; otherwise returns FALSE. + + Signals are not blocked by default. + + \sa blockSignals() +*/ + +/*! + Blocks signals if \a block is TRUE, or unblocks signals if \a + block is FALSE. + + Emitted signals disappear into hyperspace if signals are blocked. + Note that the destroyed() signals will be emitted even if the signals + for this object have been blocked. +*/ + +void QObject::blockSignals( bool block ) +{ + blockSig = block; +} + + +// +// The timer flag hasTimer is set when startTimer is called. +// It is not reset when killing the timer because more than +// one timer might be active. +// + +/*! + Starts a timer and returns a timer identifier, or returns zero if + it could not start a timer. + + A timer event will occur every \a interval milliseconds until + killTimer() or killTimers() is called. If \a interval is 0, then + the timer event occurs once every time there are no more window + system events to process. + + The virtual timerEvent() function is called with the QTimerEvent + event parameter class when a timer event occurs. Reimplement this + function to get timer events. + + If multiple timers are running, the QTimerEvent::timerId() can be + used to find out which timer was activated. + + Example: + \code + class MyObject : public QObject + { + Q_OBJECT + public: + MyObject( QObject *parent = 0, const char *name = 0 ); + + protected: + void timerEvent( QTimerEvent * ); + }; + + MyObject::MyObject( QObject *parent, const char *name ) + : QObject( parent, name ) + { + startTimer( 50 ); // 50-millisecond timer + startTimer( 1000 ); // 1-second timer + startTimer( 60000 ); // 1-minute timer + } + + void MyObject::timerEvent( QTimerEvent *e ) + { + qDebug( "timer event, id %d", e->timerId() ); + } + \endcode + + Note that QTimer's accuracy depends on the underlying operating + system and hardware. Most platforms support an accuracy of 20 ms; + some provide more. If Qt is unable to deliver the requested + number of timer clicks, it will silently discard some. + + The QTimer class provides a high-level programming interface with + one-shot timers and timer signals instead of events. + + \sa timerEvent(), killTimer(), killTimers(), QEventLoop::awake(), + QEventLoop::aboutToBlock() +*/ + +int QObject::startTimer( int interval ) +{ + pendTimer = TRUE; // set timer flag + return qStartTimer( interval, (QObject *)this ); +} + +/*! + Kills the timer with timer identifier, \a id. + + The timer identifier is returned by startTimer() when a timer + event is started. + + \sa timerEvent(), startTimer(), killTimers() +*/ + +void QObject::killTimer( int id ) +{ + qKillTimer( id ); +} + +/*! + Kills all timers that this object has started. + + \warning Using this function can cause hard-to-find bugs: it kills + timers started by sub- and superclasses as well as those started + by you, which is often not what you want. We recommend using a + QTimer or perhaps killTimer(). + + \sa timerEvent(), startTimer(), killTimer() +*/ + +void QObject::killTimers() +{ + qKillTimer( this ); +} + +static void objSearch( QObjectList *result, + QObjectList *list, + const char *inheritsClass, + bool onlyWidgets, + const char *objName, + QRegExp *rx, + bool recurse ) +{ + if ( !list || list->isEmpty() ) // nothing to search + return; + QObject *obj = list->first(); + while ( obj ) { + bool ok = TRUE; + if ( onlyWidgets ) + ok = obj->isWidgetType(); + else if ( inheritsClass && !obj->inherits(inheritsClass) ) + ok = FALSE; + if ( ok ) { + if ( objName ) + ok = ( qstrcmp(objName,obj->name()) == 0 ); +#ifndef QT_NO_REGEXP + else if ( rx ) + ok = ( rx->search(QString::fromLatin1(obj->name())) != -1 ); +#endif + } + if ( ok ) // match! + result->append( obj ); + if ( recurse && obj->children() ) + objSearch( result, (QObjectList *)obj->children(), inheritsClass, + onlyWidgets, objName, rx, recurse ); + obj = list->next(); + } +} + +/*! + \fn QObject *QObject::parent() const + + Returns a pointer to the parent object. + + \sa children() +*/ + +/*! + \fn const QObjectList *QObject::children() const + + Returns a list of child objects, or 0 if this object has no + children. + + The QObjectList class is defined in the \c qobjectlist.h header + file. + + The first child added is the \link QPtrList::first() first\endlink + object in the list and the last child added is the \link + QPtrList::last() last\endlink object in the list, i.e. new + children are appended at the end. + + Note that the list order changes when QWidget children are \link + QWidget::raise() raised\endlink or \link QWidget::lower() + lowered.\endlink A widget that is raised becomes the last object + in the list, and a widget that is lowered becomes the first object + in the list. + + \sa child(), queryList(), parent(), insertChild(), removeChild() +*/ + + +/*! + Returns a pointer to the list of all object trees (their root + objects), or 0 if there are no objects. + + The QObjectList class is defined in the \c qobjectlist.h header + file. + + The most recent root object created is the \link QPtrList::first() + first\endlink object in the list and the first root object added + is the \link QPtrList::last() last\endlink object in the list. + + \sa children(), parent(), insertChild(), removeChild() +*/ +const QObjectList *QObject::objectTrees() +{ + return object_trees; +} + + +/*! + Searches the children and optionally grandchildren of this object, + and returns a list of those objects that are named or that match + \a objName and inherit \a inheritsClass. If \a inheritsClass is 0 + (the default), all classes match. If \a objName is 0 (the + default), all object names match. + + If \a regexpMatch is TRUE (the default), \a objName is a regular + expression that the objects's names must match. The syntax is that + of a QRegExp. If \a regexpMatch is FALSE, \a objName is a string + and object names must match it exactly. + + Note that \a inheritsClass uses single inheritance from QObject, + the way inherits() does. According to inherits(), QMenuBar + inherits QWidget but not QMenuData. This does not quite match + reality, but is the best that can be done on the wide variety of + compilers Qt supports. + + Finally, if \a recursiveSearch is TRUE (the default), queryList() + searches \e{n}th-generation as well as first-generation children. + + If all this seems a bit complex for your needs, the simpler + child() function may be what you want. + + This somewhat contrived example disables all the buttons in this + window: + \code + QObjectList *l = topLevelWidget()->queryList( "QButton" ); + QObjectListIt it( *l ); // iterate over the buttons + QObject *obj; + + while ( (obj = it.current()) != 0 ) { + // for each found object... + ++it; + ((QButton*)obj)->setEnabled( FALSE ); + } + delete l; // delete the list, not the objects + \endcode + + The QObjectList class is defined in the \c qobjectlist.h header + file. + + \warning Delete the list as soon you have finished using it. The + list contains pointers that may become invalid at almost any time + without notice (as soon as the user closes a window you may have + dangling pointers, for example). + + \sa child() children(), parent(), inherits(), name(), QRegExp +*/ + +QObjectList *QObject::queryList( const char *inheritsClass, + const char *objName, + bool regexpMatch, + bool recursiveSearch ) const +{ + QObjectList *list = new QObjectList; + Q_CHECK_PTR( list ); + bool onlyWidgets = ( inheritsClass && qstrcmp(inheritsClass, "QWidget") == 0 ); +#ifndef QT_NO_REGEXP + if ( regexpMatch && objName ) { // regexp matching + QRegExp rx(QString::fromLatin1(objName)); + objSearch( list, (QObjectList *)children(), inheritsClass, onlyWidgets, + 0, &rx, recursiveSearch ); + } else +#endif + { + objSearch( list, (QObjectList *)children(), inheritsClass, onlyWidgets, + objName, 0, recursiveSearch ); + } + return list; +} + +/*! \internal + + Returns a list of objects/slot pairs that are connected to the + \a signal, or 0 if nothing is connected to it. +*/ + +QConnectionList *QObject::receivers( const char* signal ) const +{ + if ( connections && signal ) { + if ( *signal == '2' ) { // tag == 2, i.e. signal + QCString s = qt_rmWS( signal+1 ); + return receivers( metaObject()->findSignal( (const char*)s, TRUE ) ); + } else { + return receivers( metaObject()->findSignal(signal, TRUE ) ); + } + } + return 0; +} + +/*! \internal + + Returns a list of objects/slot pairs that are connected to the + signal, or 0 if nothing is connected to it. +*/ + +QConnectionList *QObject::receivers( int signal ) const +{ +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( qt_preliminary_signal_spy && signal >= 0 ) { + if ( !connections ) { + QObject* that = (QObject*) this; + that->connections = new QSignalVec( signal+1 ); + that->connections->setAutoDelete( TRUE ); + } + if ( !connections->at( signal ) ) { + QConnectionList* clist = new QConnectionList; + clist->setAutoDelete( TRUE ); + connections->insert( signal, clist ); + return clist; + } + } +#endif + if ( connections && signal >= 0 ) + return connections->at( signal ); + return 0; +} + + +/*! + Inserts an object \a obj into the list of child objects. + + \warning This function cannot be used to make one widget the child + widget of another widget. Child widgets can only be created by + setting the parent widget in the constructor or by calling + QWidget::reparent(). + + \sa removeChild(), QWidget::reparent() +*/ + +void QObject::insertChild( QObject *obj ) +{ + if ( obj->isTree ) { + remove_tree( obj ); + obj->isTree = FALSE; + } + if ( obj->parentObj && obj->parentObj != this ) { +#if defined(QT_CHECK_STATE) + if ( obj->parentObj != this && obj->isWidgetType() ) + qWarning( "QObject::insertChild: Cannot reparent a widget, " + "use QWidget::reparent() instead" ); +#endif + obj->parentObj->removeChild( obj ); + } + + if ( !childObjects ) { + childObjects = new QObjectList; + Q_CHECK_PTR( childObjects ); + } else if ( obj->parentObj == this ) { +#if defined(QT_CHECK_STATE) + qWarning( "QObject::insertChild: Object %s::%s already in list", + obj->className(), obj->name( "unnamed" ) ); +#endif + return; + } + obj->parentObj = this; + childObjects->append( obj ); + + QChildEvent *e = new QChildEvent( QEvent::ChildInserted, obj ); + QApplication::postEvent( this, e ); +} + +/*! + Removes the child object \a obj from the list of children. + + \warning This function will not remove a child widget from the + screen. It will only remove it from the parent widget's list of + children. + + \sa insertChild(), QWidget::reparent() +*/ + +void QObject::removeChild( QObject *obj ) +{ + if ( childObjects && childObjects->removeRef(obj) ) { + obj->parentObj = 0; + if ( !obj->wasDeleted ) { + insert_tree( obj ); // it's a root object now + obj->isTree = TRUE; + } + if ( childObjects->isEmpty() ) { + delete childObjects; // last child removed + childObjects = 0; // reset children list + } + + // remove events must be sent, not posted!!! + QChildEvent ce( QEvent::ChildRemoved, obj ); + QApplication::sendEvent( this, &ce ); + } +} + + +/*! + \fn void QObject::installEventFilter( const QObject *filterObj ) + + Installs an event filter \a filterObj on this object. For example: + \code + monitoredObj->installEventFilter( filterObj ); + \endcode + + An event filter is an object that receives all events that are + sent to this object. The filter can either stop the event or + forward it to this object. The event filter \a filterObj receives + events via its eventFilter() function. The eventFilter() function + must return TRUE if the event should be filtered, (i.e. stopped); + otherwise it must return FALSE. + + If multiple event filters are installed on a single object, the + filter that was installed last is activated first. + + Here's a \c KeyPressEater class that eats the key presses of its + monitored objects: + \code + class KeyPressEater : public QObject + { + ... + protected: + bool eventFilter( QObject *o, QEvent *e ); + }; + + bool KeyPressEater::eventFilter( QObject *o, QEvent *e ) + { + if ( e->type() == QEvent::KeyPress ) { + // special processing for key press + QKeyEvent *k = (QKeyEvent *)e; + qDebug( "Ate key press %d", k->key() ); + return TRUE; // eat event + } else { + // standard event processing + return FALSE; + } + } + \endcode + + And here's how to install it on two widgets: + \code + KeyPressEater *keyPressEater = new KeyPressEater( this ); + QPushButton *pushButton = new QPushButton( this ); + QListView *listView = new QListView( this ); + + pushButton->installEventFilter( keyPressEater ); + listView->installEventFilter( keyPressEater ); + \endcode + + The QAccel class, for example, uses this technique to intercept + accelerator key presses. + + \warning If you delete the receiver object in your eventFilter() + function, be sure to return TRUE. If you return FALSE, Qt sends + the event to the deleted object and the program will crash. + + \sa removeEventFilter(), eventFilter(), event() +*/ + +void QObject::installEventFilter( const QObject *obj ) +{ + if ( !obj ) + return; + if ( eventFilters ) { + int c = eventFilters->findRef( obj ); + if ( c >= 0 ) + eventFilters->take( c ); + disconnect( obj, SIGNAL(destroyed(QObject*)), + this, SLOT(cleanupEventFilter(QObject*)) ); + } else { + eventFilters = new QObjectList; + Q_CHECK_PTR( eventFilters ); + } + eventFilters->insert( 0, obj ); + connect( obj, SIGNAL(destroyed(QObject*)), this, SLOT(cleanupEventFilter(QObject*)) ); +} + +/*! + Removes an event filter object \a obj from this object. The + request is ignored if such an event filter has not been installed. + + All event filters for this object are automatically removed when + this object is destroyed. + + It is always safe to remove an event filter, even during event + filter activation (i.e. from the eventFilter() function). + + \sa installEventFilter(), eventFilter(), event() +*/ + +void QObject::removeEventFilter( const QObject *obj ) +{ + if ( eventFilters && eventFilters->removeRef(obj) ) { + if ( eventFilters->isEmpty() ) { // last event filter removed + delete eventFilters; + eventFilters = 0; // reset event filter list + } + disconnect( obj, SIGNAL(destroyed(QObject*)), + this, SLOT(cleanupEventFilter(QObject*)) ); + } +} + + +/***************************************************************************** + Signal connection management + *****************************************************************************/ + +#if defined(QT_CHECK_RANGE) + +static bool check_signal_macro( const QObject *sender, const char *signal, + const char *func, const char *op ) +{ + int sigcode = (int)(*signal) - '0'; + if ( sigcode != QSIGNAL_CODE ) { + if ( sigcode == QSLOT_CODE ) + qWarning( "QObject::%s: Attempt to %s non-signal %s::%s", + func, op, sender->className(), signal+1 ); + else + qWarning( "QObject::%s: Use the SIGNAL macro to %s %s::%s", + func, op, sender->className(), signal ); + return FALSE; + } + return TRUE; +} + +static bool check_member_code( int code, const QObject *object, + const char *member, const char *func ) +{ + if ( code != QSLOT_CODE && code != QSIGNAL_CODE ) { + qWarning( "QObject::%s: Use the SLOT or SIGNAL macro to " + "%s %s::%s", func, func, object->className(), member ); + return FALSE; + } + return TRUE; +} + +static void err_member_notfound( int code, const QObject *object, + const char *member, const char *func ) +{ + const char *type = 0; + switch ( code ) { + case QSLOT_CODE: type = "slot"; break; + case QSIGNAL_CODE: type = "signal"; break; + } + if ( strchr(member,')') == 0 ) // common typing mistake + qWarning( "QObject::%s: Parentheses expected, %s %s::%s", + func, type, object->className(), member ); + else + qWarning( "QObject::%s: No such %s %s::%s", + func, type, object->className(), member ); +} + + +static void err_info_about_objects( const char * func, + const QObject * sender, + const QObject * receiver ) +{ + const char * a = sender->name(), * b = receiver->name(); + if ( a ) + qWarning( "QObject::%s: (sender name: '%s')", func, a ); + if ( b ) + qWarning( "QObject::%s: (receiver name: '%s')", func, b ); +} + +static void err_info_about_candidates( int code, + const QMetaObject* mo, + const char* member, + const char *func ) +{ + if ( strstr(member,"const char*") ) { + // porting help + QCString newname = member; + int p; + while ( (p=newname.find("const char*")) >= 0 ) { + newname.replace(p, 11, "const QString&"); + } + const QMetaData *rm = 0; + switch ( code ) { + case QSLOT_CODE: + rm = mo->slot( mo->findSlot( newname, TRUE ), TRUE ); + break; + case QSIGNAL_CODE: + rm = mo->signal( mo->findSignal( newname, TRUE ), TRUE ); + break; + } + if ( rm ) { + qWarning("QObject::%s: Candidate: %s", func, newname.data()); + } + } +} + + +#endif // QT_CHECK_RANGE + + +/*! + Returns a pointer to the object that sent the signal, if called in + a slot activated by a signal; otherwise it returns 0. The pointer + is valid only during the execution of the slot that calls this + function. + + The pointer returned by this function becomes invalid if the + sender is destroyed, or if the slot is disconnected from the + sender's signal. + + \warning This function violates the object-oriented principle of + modularity. However, getting access to the sender might be useful + when many signals are connected to a single slot. The sender is + undefined if the slot is called as a normal C++ function. +*/ + +const QObject *QObject::sender() +{ +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( this == qt_preliminary_signal_spy ) { +# ifdef QT_THREAD_SUPPORT + // protect access to qt_spy_signal_sender + void * const address = &qt_spy_signal_sender; + QMutexLocker locker( qt_global_mutexpool ? + qt_global_mutexpool->get( address ) : 0 ); +# endif // QT_THREAD_SUPPORT + return qt_spy_signal_sender; + } +#endif + if ( senderObjects && + senderObjects->currentSender && + /* + * currentSender may be a dangling pointer in case the object + * it was pointing to was destructed from inside a slot. Thus + * verify it still is contained inside the senderObjects list + * which gets cleaned on both destruction and disconnect. + */ + + senderObjects->findRef( senderObjects->currentSender ) != -1 ) + return senderObjects->currentSender; + return 0; +} + + +/*! + \fn void QObject::connectNotify( const char *signal ) + + This virtual function is called when something has been connected + to \a signal in this object. + + \warning This function violates the object-oriented principle of + modularity. However, it might be useful when you need to perform + expensive initialization only if something is connected to a + signal. + + \sa connect(), disconnectNotify() +*/ + +void QObject::connectNotify( const char * ) +{ +} + +/*! + \fn void QObject::disconnectNotify( const char *signal ) + + This virtual function is called when something has been + disconnected from \a signal in this object. + + \warning This function violates the object-oriented principle of + modularity. However, it might be useful for optimizing access to + expensive resources. + + \sa disconnect(), connectNotify() +*/ + +void QObject::disconnectNotify( const char * ) +{ +} + + +/*! + \fn bool QObject::checkConnectArgs( const char *signal, const QObject *receiver, const char *member ) + + Returns TRUE if the \a signal and the \a member arguments are + compatible; otherwise returns FALSE. (The \a receiver argument is + currently ignored.) + + \warning We recommend that you use the default implementation and + do not reimplement this function. + + \omit + TRUE: "signal(<anything>)", "member()" + TRUE: "signal(a,b,c)", "member(a,b,c)" + TRUE: "signal(a,b,c)", "member(a,b)", "member(a)" etc. + FALSE: "signal(const a)", "member(a)" + FALSE: "signal(a)", "member(const a)" + FALSE: "signal(a)", "member(b)" + FALSE: "signal(a)", "member(a,b)" + \endomit +*/ + +bool QObject::checkConnectArgs( const char *signal, + const QObject *, + const char *member ) +{ + const char *s1 = signal; + const char *s2 = member; + while ( *s1++ != '(' ) { } // scan to first '(' + while ( *s2++ != '(' ) { } + if ( *s2 == ')' || qstrcmp(s1,s2) == 0 ) // member has no args or + return TRUE; // exact match + int s1len = qstrlen(s1); + int s2len = qstrlen(s2); + if ( s2len < s1len && qstrncmp(s1,s2,s2len-1)==0 && s1[s2len-1]==',' ) + return TRUE; // member has less args + return FALSE; +} + +/*! + Normlizes the signal or slot definition \a signalSlot by removing + unnecessary whitespace. +*/ + +QCString QObject::normalizeSignalSlot( const char *signalSlot ) +{ + if ( !signalSlot ) + return QCString(); + return qt_rmWS( signalSlot ); +} + + + +/*! + \overload bool QObject::connect( const QObject *sender, const char *signal, const char *member ) const + + Connects \a signal from the \a sender object to this object's \a + member. + + Equivalent to: \c{QObject::connect(sender, signal, this, member)}. + + \sa disconnect() +*/ + +/*! + Connects \a signal from the \a sender object to \a member in object + \a receiver, and returns TRUE if the connection succeeds; otherwise + returns FALSE. + + You must use the SIGNAL() and SLOT() macros when specifying the \a signal + and the \a member, for example: + \code + QLabel *label = new QLabel; + QScrollBar *scroll = new QScrollBar; + QObject::connect( scroll, SIGNAL(valueChanged(int)), + label, SLOT(setNum(int)) ); + \endcode + + This example ensures that the label always displays the current + scroll bar value. Note that the signal and slots parameters must not + contain any variable names, only the type. E.g. the following would + not work and return FALSE: + QObject::connect( scroll, SIGNAL(valueChanged(int v)), + label, SLOT(setNum(int v)) ); + + A signal can also be connected to another signal: + + \code + class MyWidget : public QWidget + { + Q_OBJECT + public: + MyWidget(); + + signals: + void myUsefulSignal(); + + private: + QPushButton *aButton; + }; + + MyWidget::MyWidget() + { + aButton = new QPushButton( this ); + connect( aButton, SIGNAL(clicked()), SIGNAL(myUsefulSignal()) ); + } + \endcode + + In this example, the MyWidget constructor relays a signal from a + private member variable, and makes it available under a name that + relates to MyWidget. + + A signal can be connected to many slots and signals. Many signals + can be connected to one slot. + + If a signal is connected to several slots, the slots are activated + in an arbitrary order when the signal is emitted. + + The function returns TRUE if it successfully connects the signal + to the slot. It will return FALSE if it cannot create the + connection, for example, if QObject is unable to verify the + existence of either \a signal or \a member, or if their signatures + aren't compatible. + + A signal is emitted for \e{every} connection you make, so if you + duplicate a connection, two signals will be emitted. You can + always break a connection using \c{disconnect()}. + + \sa disconnect() +*/ + +bool QObject::connect( const QObject *sender, const char *signal, + const QObject *receiver, const char *member ) +{ +#if defined(QT_CHECK_NULL) + if ( sender == 0 || receiver == 0 || signal == 0 || member == 0 ) { + qWarning( "QObject::connect: Cannot connect %s::%s to %s::%s", + sender ? sender->className() : "(null)", + signal ? signal+1 : "(null)", + receiver ? receiver->className() : "(null)", + member ? member+1 : "(null)" ); + return FALSE; + } +#endif + QMetaObject *smeta = sender->metaObject(); + +#if defined(QT_CHECK_RANGE) + if ( !check_signal_macro( sender, signal, "connect", "bind" ) ) + return FALSE; +#endif + QCString nw_signal(signal); // Assume already normalized + ++signal; // skip member type code + + int signal_index = smeta->findSignal( signal, TRUE ); + if ( signal_index < 0 ) { // normalize and retry + nw_signal = qt_rmWS( signal-1 ); // remove whitespace + signal = nw_signal.data()+1; // skip member type code + signal_index = smeta->findSignal( signal, TRUE ); + } + + if ( signal_index < 0 ) { // no such signal +#if defined(QT_CHECK_RANGE) + err_member_notfound( QSIGNAL_CODE, sender, signal, "connect" ); + err_info_about_candidates( QSIGNAL_CODE, smeta, signal, "connect" ); + err_info_about_objects( "connect", sender, receiver ); +#endif + return FALSE; + } + const QMetaData *sm = smeta->signal( signal_index, TRUE ); + signal = sm->name; // use name from meta object + + int membcode = member[0] - '0'; // get member code + + QObject *s = (QObject *)sender; // we need to change them + QObject *r = (QObject *)receiver; // internally + +#if defined(QT_CHECK_RANGE) + if ( !check_member_code( membcode, r, member, "connect" ) ) + return FALSE; +#endif + member++; // skip code + + QCString nw_member ; + QMetaObject *rmeta = r->metaObject(); + int member_index = -1; + switch ( membcode ) { // get receiver member + case QSLOT_CODE: + member_index = rmeta->findSlot( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSlot( member, TRUE ); + } + break; + case QSIGNAL_CODE: + member_index = rmeta->findSignal( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSignal( member, TRUE ); + } + break; + } + if ( member_index < 0 ) { +#if defined(QT_CHECK_RANGE) + err_member_notfound( membcode, r, member, "connect" ); + err_info_about_candidates( membcode, rmeta, member, "connect" ); + err_info_about_objects( "connect", sender, receiver ); +#endif + return FALSE; + } +#if defined(QT_CHECK_RANGE) + if ( !s->checkConnectArgs(signal,receiver,member) ) { + qWarning( "QObject::connect: Incompatible sender/receiver arguments" + "\n\t%s::%s --> %s::%s", + s->className(), signal, + r->className(), member ); + return FALSE; + } else { + const QMetaData *rm = membcode == QSLOT_CODE ? + rmeta->slot( member_index, TRUE ) : + rmeta->signal( member_index, TRUE ); + if ( rm ) { + int si = 0; + int ri = 0; + while ( si < sm->method->count && ri < rm->method->count ) { + if ( sm->method->parameters[si].inOut == QUParameter::Out ) + si++; + else if ( rm->method->parameters[ri].inOut == QUParameter::Out ) + ri++; + else if ( !QUType::isEqual( sm->method->parameters[si++].type, + rm->method->parameters[ri++].type ) ) { + if ( ( QUType::isEqual( sm->method->parameters[si-1].type, &static_QUType_ptr ) + && QUType::isEqual( rm->method->parameters[ri-1].type, &static_QUType_varptr ) ) + || ( QUType::isEqual( sm->method->parameters[si-1].type, &static_QUType_varptr ) + && QUType::isEqual( rm->method->parameters[ri-1].type, &static_QUType_ptr ) ) ) + continue; // varptr got introduced in 3.1 and is binary compatible with ptr + qWarning( "QObject::connect: Incompatible sender/receiver marshalling" + "\n\t%s::%s --> %s::%s", + s->className(), signal, + r->className(), member ); + return FALSE; + } + } + } + } +#endif + connectInternal( sender, signal_index, receiver, membcode, member_index ); + s->connectNotify( nw_signal ); + return TRUE; +} + +/*! \internal */ + +void QObject::connectInternal( const QObject *sender, int signal_index, const QObject *receiver, + int membcode, int member_index ) +{ + QObject *s = (QObject*)sender; + QObject *r = (QObject*)receiver; + + if ( !s->connections ) { // create connections lookup table + s->connections = new QSignalVec( signal_index+1 ); + Q_CHECK_PTR( s->connections ); + s->connections->setAutoDelete( TRUE ); + } + + QConnectionList *clist = s->connections->at( signal_index ); + if ( !clist ) { // create receiver list + clist = new QConnectionList; + Q_CHECK_PTR( clist ); + clist->setAutoDelete( TRUE ); + s->connections->insert( signal_index, clist ); + } + + QMetaObject *rmeta = r->metaObject(); + const QMetaData *rm = 0; + + switch ( membcode ) { // get receiver member + case QSLOT_CODE: + rm = rmeta->slot( member_index, TRUE ); + break; + case QSIGNAL_CODE: + rm = rmeta->signal( member_index, TRUE ); + break; + } + + QConnection *c = new QConnection( r, member_index, rm ? rm->name : "qt_invoke", membcode ); + Q_CHECK_PTR( c ); + clist->append( c ); + if ( !r->senderObjects ) // create list of senders + r->senderObjects = new QSenderObjectList; + r->senderObjects->append( s ); // add sender to list +} + + +/*! + \overload bool QObject::disconnect( const char *signal, const QObject *receiver, const char *member ) + + Disconnects \a signal from \a member of \a receiver. + + A signal-slot connection is removed when either of the objects + involved are destroyed. +*/ + +/*! + \overload bool QObject::disconnect( const QObject *receiver, const char *member ) + + Disconnects all signals in this object from \a receiver's \a + member. + + A signal-slot connection is removed when either of the objects + involved are destroyed. +*/ + +/*! + Disconnects \a signal in object \a sender from \a member in object + \a receiver. + + A signal-slot connection is removed when either of the objects + involved are destroyed. + + disconnect() is typically used in three ways, as the following + examples demonstrate. + \list 1 + \i Disconnect everything connected to an object's signals: + \code + disconnect( myObject, 0, 0, 0 ); + \endcode + equivalent to the non-static overloaded function + \code + myObject->disconnect(); + \endcode + \i Disconnect everything connected to a specific signal: + \code + disconnect( myObject, SIGNAL(mySignal()), 0, 0 ); + \endcode + equivalent to the non-static overloaded function + \code + myObject->disconnect( SIGNAL(mySignal()) ); + \endcode + \i Disconnect a specific receiver: + \code + disconnect( myObject, 0, myReceiver, 0 ); + \endcode + equivalent to the non-static overloaded function + \code + myObject->disconnect( myReceiver ); + \endcode + \endlist + + 0 may be used as a wildcard, meaning "any signal", "any receiving + object", or "any slot in the receiving object", respectively. + + The \a sender may never be 0. (You cannot disconnect signals from + more than one object in a single call.) + + If \a signal is 0, it disconnects \a receiver and \a member from + any signal. If not, only the specified signal is disconnected. + + If \a receiver is 0, it disconnects anything connected to \a + signal. If not, slots in objects other than \a receiver are not + disconnected. + + If \a member is 0, it disconnects anything that is connected to \a + receiver. If not, only slots named \a member will be disconnected, + and all other slots are left alone. The \a member must be 0 if \a + receiver is left out, so you cannot disconnect a + specifically-named slot on all objects. + + \sa connect() +*/ + +bool QObject::disconnect( const QObject *sender, const char *signal, + const QObject *receiver, const char *member ) +{ +#if defined(QT_CHECK_NULL) + if ( sender == 0 || (receiver == 0 && member != 0) ) { + qWarning( "QObject::disconnect: Unexpected null parameter" ); + return FALSE; + } +#endif + if ( !sender->connections ) // no connected signals + return FALSE; + QObject *s = (QObject *)sender; + QObject *r = (QObject *)receiver; + int member_index = -1; + int membcode = -1; + QCString nw_member; + if ( member ) { + membcode = member[0] - '0'; +#if defined(QT_CHECK_RANGE) + if ( !check_member_code( membcode, r, member, "disconnect" ) ) + return FALSE; +#endif + ++member; + QMetaObject *rmeta = r->metaObject(); + + switch ( membcode ) { // get receiver member + case QSLOT_CODE: + member_index = rmeta->findSlot( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSlot( member, TRUE ); + } + break; + case QSIGNAL_CODE: + member_index = rmeta->findSignal( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSignal( member, TRUE ); + } + break; + } + if ( member_index < 0 ) { // no such member +#if defined(QT_CHECK_RANGE) + err_member_notfound( membcode, r, member, "disconnect" ); + err_info_about_candidates( membcode, rmeta, member, "connect" ); + err_info_about_objects( "disconnect", sender, receiver ); +#endif + return FALSE; + } + } + + if ( signal == 0 ) { // any/all signals + if ( disconnectInternal( s, -1, r, membcode, member_index ) ) + s->disconnectNotify( 0 ); + else + return FALSE; + } else { // specific signal +#if defined(QT_CHECK_RANGE) + if ( !check_signal_macro( s, signal, "disconnect", "unbind" ) ) + return FALSE; +#endif + QCString nw_signal(signal); // Assume already normalized + ++signal; // skip member type code + + QMetaObject *smeta = s->metaObject(); + if ( !smeta ) // no meta object + return FALSE; + int signal_index = smeta->findSignal( signal, TRUE ); + if ( signal_index < 0 ) { // normalize and retry + nw_signal = qt_rmWS( signal-1 ); // remove whitespace + signal = nw_signal.data()+1; // skip member type code + signal_index = smeta->findSignal( signal, TRUE ); + } + if ( signal_index < 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QObject::disconnect: No such signal %s::%s", + s->className(), signal ); +#endif + return FALSE; + } + + /* compatibility and safety: If a receiver has several slots + * with the same name, disconnect them all*/ + bool res = FALSE; + if ( membcode == QSLOT_CODE && r ) { + QMetaObject * rmeta = r->metaObject(); + do { + int mi = rmeta->findSlot( member ); + if ( mi != -1 ) + res |= disconnectInternal( s, signal_index, r, membcode, mi ); + } while ( (rmeta = rmeta->superClass()) ); + } else { + res = disconnectInternal( s, signal_index, r, membcode, member_index ); + } + if ( res ) + s->disconnectNotify( nw_signal ); + return res; + } + return TRUE; +} + +/*! \internal */ + +bool QObject::disconnectInternal( const QObject *sender, int signal_index, + const QObject *receiver, int membcode, int member_index ) +{ + QObject *s = (QObject*)sender; + QObject *r = (QObject*)receiver; + + if ( !s->connections ) + return FALSE; + + bool success = FALSE; + QConnectionList *clist; + register QConnection *c; + if ( signal_index == -1 ) { + for ( int i = 0; i < (int) s->connections->size(); i++ ) { + clist = (*s->connections)[i]; // for all signals... + if ( !clist ) + continue; + c = clist->first(); + while ( c ) { // for all receivers... + if ( r == 0 ) { // remove all receivers + removeObjFromList( c->object()->senderObjects, s ); + success = TRUE; + c = clist->next(); + } else if ( r == c->object() && + ( member_index == -1 || + member_index == c->member() && c->memberType() == membcode ) ) { + removeObjFromList( c->object()->senderObjects, s, TRUE ); + success = TRUE; + clist->remove(); + c = clist->current(); + } else { + c = clist->next(); + } + } + if ( r == 0 ) // disconnect all receivers + s->connections->insert( i, 0 ); + } + } else { + clist = s->connections->at( signal_index ); + if ( !clist ) + return FALSE; + + c = clist->first(); + while ( c ) { // for all receivers... + if ( r == 0 ) { // remove all receivers + removeObjFromList( c->object()->senderObjects, s, TRUE ); + success = TRUE; + c = clist->next(); + } else if ( r == c->object() && + ( member_index == -1 || + member_index == c->member() && c->memberType() == membcode ) ) { + removeObjFromList( c->object()->senderObjects, s, TRUE ); + success = TRUE; + clist->remove(); + c = clist->current(); + } else { + c = clist->next(); + } + } + if ( r == 0 ) // disconnect all receivers + s->connections->insert( signal_index, 0 ); + } + return success; +} + +/*! + \fn QObject::destroyed() + + This signal is emitted when the object is being destroyed. + + Note that the signal is emitted by the QObject destructor, so + the object's virtual table is already degenerated at this point, + and it is not safe to call any functions on the object emitting + the signal. This signal can not be blocked. + + All the objects's children are destroyed immediately after this + signal is emitted. +*/ + +/*! + \overload QObject::destroyed( QObject* obj) + + This signal is emitted immediately before the object \a obj is + destroyed, and can not be blocked. + + All the objects's children are destroyed immediately after this + signal is emitted. +*/ + +/*! + Performs a deferred deletion of this object. + + Instead of an immediate deletion this function schedules a + deferred delete event for processing when Qt returns to the main + event loop. +*/ +void QObject::deleteLater() +{ + QApplication::postEvent( this, new QEvent( QEvent::DeferredDelete) ); +} + +/*! + This slot is connected to the destroyed() signal of other objects + that have installed event filters on this object. When the other + object, \a obj, is destroyed, we want to remove its event filter. +*/ + +void QObject::cleanupEventFilter(QObject* obj) +{ + removeEventFilter( obj ); +} + + +/*! + \fn QString QObject::tr( const char *sourceText, const char * comment ) + \reentrant + + Returns a translated version of \a sourceText, or \a sourceText + itself if there is no appropriate translated version. The + translation context is QObject with \a comment (0 by default). + All QObject subclasses using the Q_OBJECT macro automatically have + a reimplementation of this function with the subclass name as + context. + + \warning This method is reentrant only if all translators are + installed \e before calling this method. Installing or removing + translators while performing translations is not supported. Doing + so will probably result in crashes or other undesirable behavior. + + \sa trUtf8() QApplication::translate() + \link i18n.html Internationalization with Qt\endlink +*/ + +/*! + \fn QString QObject::trUtf8( const char *sourceText, + const char *comment ) + \reentrant + + Returns a translated version of \a sourceText, or + QString::fromUtf8(\a sourceText) if there is no appropriate + version. It is otherwise identical to tr(\a sourceText, \a + comment). + + \warning This method is reentrant only if all translators are + installed \e before calling this method. Installing or removing + translators while performing translations is not supported. Doing + so will probably result in crashes or other undesirable behavior. + + \sa tr() QApplication::translate() +*/ + +static QMetaObjectCleanUp cleanUp_Qt = QMetaObjectCleanUp( "QObject", &QObject::staticMetaObject ); + +QMetaObject* QObject::staticQtMetaObject() +{ + static QMetaObject* qtMetaObject = 0; + if ( qtMetaObject ) + return qtMetaObject; + +#ifndef QT_NO_PROPERTIES + static const QMetaEnum::Item enum_0[] = { + { "AlignLeft", (int) Qt::AlignLeft }, + { "AlignRight", (int) Qt::AlignRight }, + { "AlignHCenter", (int) Qt::AlignHCenter }, + { "AlignTop", (int) Qt::AlignTop }, + { "AlignBottom", (int) Qt::AlignBottom }, + { "AlignVCenter", (int) Qt::AlignVCenter }, + { "AlignCenter", (int) Qt::AlignCenter }, + { "AlignAuto", (int) Qt::AlignAuto }, + { "AlignJustify", (int) Qt::AlignJustify }, + { "WordBreak", (int) Qt::WordBreak } + }; + + static const QMetaEnum::Item enum_1[] = { + { "Horizontal", (int) Qt::Horizontal }, + { "Vertical", (int) Qt::Vertical } + }; + + static const QMetaEnum::Item enum_2[] = { + { "PlainText", (int) Qt::PlainText }, + { "RichText", (int) Qt::RichText }, + { "AutoText", (int) Qt::AutoText }, + { "LogText", (int) Qt::LogText } + }; + + static const QMetaEnum::Item enum_3[] = { + { "NoBackground", (int) Qt::NoBackground }, + { "PaletteForeground", (int) Qt::PaletteForeground }, + { "PaletteButton", (int) Qt::PaletteButton }, + { "PaletteLight", (int) Qt::PaletteLight }, + { "PaletteMidlight", (int) Qt::PaletteMidlight }, + { "PaletteDark", (int) Qt::PaletteDark }, + { "PaletteMid", (int) Qt::PaletteMid }, + { "PaletteText", (int) Qt::PaletteText }, + { "PaletteBrightText", (int) Qt::PaletteBrightText }, + { "PaletteBase", (int) Qt::PaletteBase }, + { "PaletteBackground", (int) Qt::PaletteBackground }, + { "PaletteShadow", (int) Qt::PaletteShadow }, + { "PaletteHighlight", (int) Qt::PaletteHighlight }, + { "PaletteHighlightedText", (int) Qt::PaletteHighlightedText }, + { "PaletteButtonText", (int) Qt::PaletteButtonText }, + { "PaletteLink", (int) Qt::PaletteLink }, + { "PaletteLinkVisited", (int) Qt::PaletteLinkVisited } + }; + + static const QMetaEnum::Item enum_4[] = { + { "TextDate", (int) Qt::TextDate }, + { "ISODate", (int) Qt::ISODate }, + { "LocalDate", (int) Qt::LocalDate } + }; + + + static const QMetaEnum enum_tbl[] = { + { "Alignment", 10, enum_0, TRUE }, + { "Orientation", 2, enum_1, FALSE }, + { "TextFormat", 4, enum_2, FALSE }, + { "BackgroundMode", 17, enum_3, FALSE }, + { "DateFormat", 3, enum_4, FALSE } + }; +#endif + + qtMetaObject = new QMetaObject( "Qt", 0, + 0, 0, + 0, 0, +#ifndef QT_NO_PROPERTIES + 0, 0, + enum_tbl, 5, +#endif + 0, 0 ); + cleanUp_Qt.setMetaObject( qtMetaObject ); + + return qtMetaObject; +} + +/*! + \internal + + Signal activation with the most frequently used parameter/argument + types. All other combinations are generated by the meta object + compiler. + */ +void QObject::activate_signal( int signal ) +{ +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( qt_preliminary_signal_spy ) { + if ( !signalsBlocked() && signal >= 0 && + ( !connections || !connections->at( signal ) ) ) { + QUObject o[1]; + qt_spy_signal( this, signal, o ); + return; + } + } +#endif + + if ( !connections || signalsBlocked() || signal < 0 ) + return; + QConnectionList *clist = connections->at( signal ); + if ( !clist ) + return; + QUObject o[1]; + activate_signal( clist, o ); +} + +/*! \internal */ + +void QObject::activate_signal( QConnectionList *clist, QUObject *o ) +{ + if ( !clist ) + return; + +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( qt_preliminary_signal_spy ) + qt_spy_signal( this, connections->findRef( clist), o ); +#endif + + QObject *object; + QSenderObjectList* sol; + QObject* oldSender = 0; + QConnection *c; + if ( clist->count() == 1 ) { // save iterator + c = clist->first(); + object = c->object(); + sol = object->senderObjects; + if ( sol ) { + oldSender = sol->currentSender; + sol->ref(); + sol->currentSender = this; + } + if ( c->memberType() == QSIGNAL_CODE ) + object->qt_emit( c->member(), o ); + else + object->qt_invoke( c->member(), o ); + if ( sol ) { + sol->currentSender = oldSender; + if ( sol->deref() ) + delete sol; + } + } else { + QConnection *cd = 0; + QConnectionListIt it(*clist); + while ( (c=it.current()) ) { + ++it; + if ( c == cd ) + continue; + cd = c; + object = c->object(); + sol = object->senderObjects; + if ( sol ) { + oldSender = sol->currentSender; + sol->ref(); + sol->currentSender = this; + } + if ( c->memberType() == QSIGNAL_CODE ) + object->qt_emit( c->member(), o ); + else + object->qt_invoke( c->member(), o ); + if (sol ) { + sol->currentSender = oldSender; + if ( sol->deref() ) + delete sol; + } + } + } +} + +/*! + \overload void QObject::activate_signal( int signal, int ) +*/ + +/*! + \overload void QObject::activate_signal( int signal, double ) +*/ + +/*! + \overload void QObject::activate_signal( int signal, QString ) +*/ + +/*! + \fn void QObject::activate_signal_bool( int signal, bool ) + \internal + + Like the above functions, but since bool is sometimes + only a typedef it cannot be a simple overload. +*/ + +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY +#define ACTIVATE_SIGNAL_WITH_PARAM(FNAME,TYPE) \ +void QObject::FNAME( int signal, TYPE param ) \ +{ \ + if ( qt_preliminary_signal_spy ) { \ + if ( !signalsBlocked() && signal >= 0 && \ + ( !connections || !connections->at( signal ) ) ) { \ + QUObject o[2]; \ + static_QUType_##TYPE.set( o+1, param ); \ + qt_spy_signal( this, signal, o ); \ + return; \ + } \ + } \ + if ( !connections || signalsBlocked() || signal < 0 ) \ + return; \ + QConnectionList *clist = connections->at( signal ); \ + if ( !clist ) \ + return; \ + QUObject o[2]; \ + static_QUType_##TYPE.set( o+1, param ); \ + activate_signal( clist, o ); \ +} +#else +#define ACTIVATE_SIGNAL_WITH_PARAM(FNAME,TYPE) \ +void QObject::FNAME( int signal, TYPE param ) \ +{ \ + if ( !connections || signalsBlocked() || signal < 0 ) \ + return; \ + QConnectionList *clist = connections->at( signal ); \ + if ( !clist ) \ + return; \ + QUObject o[2]; \ + static_QUType_##TYPE.set( o+1, param ); \ + activate_signal( clist, o ); \ +} + +#endif +// We don't want to duplicate too much text so... + +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal, int ) +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal, double ) +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal, QString ) +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal_bool, bool ) + + +/***************************************************************************** + QObject debugging output routines. + *****************************************************************************/ + +static void dumpRecursive( int level, QObject *object ) +{ +#if defined(QT_DEBUG) + if ( object ) { + QString buf; + buf.fill( '\t', level/2 ); + if ( level % 2 ) + buf += " "; + const char *name = object->name(); + QString flags=""; + if ( qApp->focusWidget() == object ) + flags += 'F'; + if ( object->isWidgetType() ) { + QWidget * w = (QWidget *)object; + if ( w->isVisible() ) { + QString t( "<%1,%2,%3,%4>" ); + flags += t.arg(w->x()).arg(w->y()).arg(w->width()).arg(w->height()); + } else { + flags += 'I'; + } + } + qDebug( "%s%s::%s %s", (const char*)buf, object->className(), name, + flags.latin1() ); + if ( object->children() ) { + QObjectListIt it(*object->children()); + QObject * c; + while ( (c=it.current()) != 0 ) { + ++it; + dumpRecursive( level+1, c ); + } + } + } +#else + Q_UNUSED( level ) + Q_UNUSED( object ) +#endif +} + +/*! + Dumps a tree of children to the debug output. + + This function is useful for debugging, but does nothing if the + library has been compiled in release mode (i.e. without debugging + information). +*/ + +void QObject::dumpObjectTree() +{ + dumpRecursive( 0, this ); +} + +/*! + Dumps information about signal connections, etc. for this object + to the debug output. + + This function is useful for debugging, but does nothing if the + library has been compiled in release mode (i.e. without debugging + information). +*/ + +void QObject::dumpObjectInfo() +{ +#if defined(QT_DEBUG) + qDebug( "OBJECT %s::%s", className(), name( "unnamed" ) ); + int n = 0; + qDebug( " SIGNALS OUT" ); + if ( connections ) { + QConnectionList *clist; + for ( uint i = 0; i < connections->size(); i++ ) { + if ( ( clist = connections->at( i ) ) ) { + qDebug( "\t%s", metaObject()->signal( i, TRUE )->name ); + n++; + register QConnection *c; + QConnectionListIt cit(*clist); + while ( (c=cit.current()) ) { + ++cit; + qDebug( "\t --> %s::%s %s", c->object()->className(), + c->object()->name( "unnamed" ), c->memberName() ); + } + } + } + } + if ( n == 0 ) + qDebug( "\t<None>" ); + + qDebug( " SIGNALS IN" ); + n = 0; + if ( senderObjects ) { + QObject *sender = senderObjects->first(); + while ( sender ) { + qDebug( "\t%s::%s", + sender->className(), sender->name( "unnamed" ) ); + n++; + sender = senderObjects->next(); + } + } + if ( n == 0 ) + qDebug( "\t<None>" ); +#endif +} + +#ifndef QT_NO_PROPERTIES + +/*! + Sets the value of the object's \a name property to \a value. + + Returns TRUE if the operation was successful; otherwise returns + FALSE. + + Information about all available properties is provided through the + metaObject(). + + \sa property(), metaObject(), QMetaObject::propertyNames(), QMetaObject::property() +*/ +bool QObject::setProperty( const char *name, const QVariant& value ) +{ + if ( !value.isValid() ) + return FALSE; + + QVariant v = value; + + QMetaObject* meta = metaObject(); + if ( !meta ) + return FALSE; + int id = meta->findProperty( name, TRUE ); + const QMetaProperty* p = meta->property( id, TRUE ); + if ( !p || !p->isValid() || !p->writable() ) { + qWarning( "%s::setProperty( \"%s\", value ) failed: property invalid, read-only or does not exist", + className(), name ); + return FALSE; + } + + if ( p->isEnumType() ) { + if ( v.type() == QVariant::String || v.type() == QVariant::CString ) { + if ( p->isSetType() ) { + QString s = value.toString(); + // QStrList does not support split, use QStringList for that. + QStringList l = QStringList::split( '|', s ); + QStrList keys; + for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it ) + keys.append( (*it).stripWhiteSpace().latin1() ); + v = QVariant( p->keysToValue( keys ) ); + } else { + v = QVariant( p->keyToValue( value.toCString().data() ) ); + } + } else if ( v.type() != QVariant::Int && v.type() != QVariant::UInt ) { + return FALSE; + } + return qt_property( id, 0, &v ); + } + + QVariant::Type type = (QVariant::Type)(p->flags >> 24); + if ( type == QVariant::Invalid ) + type = QVariant::nameToType( p->type() ); + if ( type != QVariant::Invalid && !v.canCast( type ) ) + return FALSE; + return qt_property( id, 0, &v ); +} + +/*! + Returns the value of the object's \a name property. + + If no such property exists, the returned variant is invalid. + + Information about all available properties are provided through + the metaObject(). + + \sa setProperty(), QVariant::isValid(), metaObject(), + QMetaObject::propertyNames(), QMetaObject::property() +*/ +QVariant QObject::property( const char *name ) const +{ + QVariant v; + QMetaObject* meta = metaObject(); + if ( !meta ) + return v; + int id = meta->findProperty( name, TRUE ); + const QMetaProperty* p = meta->property( id, TRUE ); + if ( !p || !p->isValid() ) { + qWarning( "%s::property( \"%s\" ) failed: property invalid or does not exist", + className(), name ); + return v; + } + QObject* that = (QObject*) this; // moc ensures constness for the qt_property call + that->qt_property( id, 1, &v ); + return v; +} + +#endif // QT_NO_PROPERTIES + +#ifndef QT_NO_USERDATA +/*!\internal + */ +uint QObject::registerUserData() +{ + static int user_data_registration = 0; + return user_data_registration++; +} + +/*!\internal + */ +QObjectUserData::~QObjectUserData() +{ +} + +/*!\internal + */ +void QObject::setUserData( uint id, QObjectUserData* data) +{ + if ( !d ) + d = new QObjectPrivate( id+1 ); + if ( id >= d->size() ) + d->resize( id+1 ); + d->insert( id, data ); +} + +/*!\internal + */ +QObjectUserData* QObject::userData( uint id ) const +{ + if ( d && id < d->size() ) + return d->at( id ); + return 0; +} + +#endif // QT_NO_USERDATA diff --git a/src/kernel/qobject.h b/src/kernel/qobject.h new file mode 100644 index 0000000..6de28db --- /dev/null +++ b/src/kernel/qobject.h @@ -0,0 +1,265 @@ +/**************************************************************************** +** +** Definition of QObject class +** +** Created : 930418 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QOBJECT_H +#define QOBJECT_H + +#ifndef QT_H +#include "qobjectdefs.h" +#include "qwindowdefs.h" +#include "qstring.h" +#include "qevent.h" +#include "qnamespace.h" +#endif // QT_H + +#define QT_TR_NOOP(x) (x) +#define QT_TRANSLATE_NOOP(scope,x) (x) + +class QMetaObject; +class QVariant; +class QMetaProperty; +class QPostEventList; +class QSenderObjectList; +class QObjectPrivate; +#ifndef QT_NO_USERDATA +class QObjectUserData; +#endif +struct QUObject; + +class Q_EXPORT QObject: public Qt +{ + Q_OBJECT + Q_PROPERTY( QCString name READ name WRITE setName ) + +public: + QObject( QObject *parent=0, const char *name=0 ); + virtual ~QObject(); + +#ifdef Q_QDOC + virtual const char *className() const; + static QString tr( const char *, const char * ); + static QString trUtf8( const char *, const char * ); + virtual QMetaObject *metaObject() const; +#endif + + virtual bool event( QEvent * ); + virtual bool eventFilter( QObject *, QEvent * ); + + bool isA( const char * ) const; + bool inherits( const char * ) const; + + const char *name() const; + const char *name( const char * defaultName ) const; + + virtual void setName( const char *name ); + bool isWidgetType() const { return isWidget; } + bool highPriority() const { return FALSE; } + + bool signalsBlocked() const { return blockSig; } + void blockSignals( bool b ); + + int startTimer( int interval ); + void killTimer( int id ); + void killTimers(); + + QObject *child( const char *objName, const char *inheritsClass = 0, bool recursiveSearch = TRUE ); //### const in 4.0 + const QObjectList *children() const { return childObjects; } + QObjectList childrenListObject(); + const QObjectList childrenListObject() const; + + static const QObjectList *objectTrees(); + static const QObjectList objectTreesListObject(); + + QObjectList *queryList( const char *inheritsClass = 0, + const char *objName = 0, + bool regexpMatch = TRUE, + bool recursiveSearch = TRUE ) const; + + virtual void insertChild( QObject * ); + virtual void removeChild( QObject * ); + + void installEventFilter( const QObject * ); + void removeEventFilter( const QObject * ); + + static bool connect( const QObject *sender, const char *signal, + const QObject *receiver, const char *member ); + bool connect( const QObject *sender, const char *signal, + const char *member ) const; + static bool disconnect( const QObject *sender, const char *signal, + const QObject *receiver, const char *member ); + bool disconnect( const char *signal=0, + const QObject *receiver=0, const char *member=0 ); + bool disconnect( const QObject *receiver, const char *member=0 ); + static void connectInternal( const QObject *sender, int signal_index, + const QObject *receiver, int membcode, int member_index ); + static bool disconnectInternal( const QObject *sender, int signal_index, + const QObject *receiver, int membcode, int member_index ); + + void dumpObjectTree(); + void dumpObjectInfo(); + +#ifndef QT_NO_PROPERTIES + virtual bool setProperty( const char *name, const QVariant& value ); + virtual QVariant property( const char *name ) const; +#endif // QT_NO_PROPERTIES +#ifdef QT_NO_TRANSLATION + static QString tr( const char *sourceText, const char * = 0); +#ifndef QT_NO_TEXTCODEC + static QString trUtf8( const char *sourceText, const char * = 0); +#endif +#endif //QT_NO_TRANSLATION + +#ifndef QT_NO_USERDATA + static uint registerUserData(); + void setUserData( uint id, QObjectUserData* data); + QObjectUserData* userData( uint id ) const; +#endif // QT_NO_USERDATA + +signals: + void destroyed(); + void destroyed( QObject* obj ); + +public: + QObject *parent() const { return parentObj; } + +public slots: + void deleteLater(); + +private slots: + void cleanupEventFilter( QObject* ); + +protected: + bool activate_filters( QEvent * ); + QConnectionList *receivers( const char* signal ) const; + QConnectionList *receivers( int signal ) const; + void activate_signal( int signal ); + void activate_signal( int signal, int ); + void activate_signal( int signal, double ); + void activate_signal( int signal, QString ); + void activate_signal_bool( int signal, bool ); + void activate_signal( QConnectionList *clist, QUObject *o ); + + const QObject *sender(); + + virtual void timerEvent( QTimerEvent * ); + virtual void childEvent( QChildEvent * ); + virtual void customEvent( QCustomEvent * ); + + virtual void connectNotify( const char *signal ); + virtual void disconnectNotify( const char *signal ); + virtual bool checkConnectArgs( const char *signal, const QObject *receiver, + const char *member ); + static QCString normalizeSignalSlot( const char *signalSlot ); + +private: + uint isSignal : 1; + uint isWidget : 1; + uint pendTimer : 1; + uint blockSig : 1; + uint wasDeleted : 1; + uint isTree : 1; + + const char *objname; + QObject *parentObj; + QObjectList *childObjects; + QSignalVec *connections; + QSenderObjectList *senderObjects; + QObjectList *eventFilters; + QPostEventList *postedEvents; + QObjectPrivate* d; + + static QMetaObject* staticQtMetaObject(); + + friend class QApplication; + friend class QBaseApplication; + friend class QWidget; + friend class QSignal; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QObject( const QObject & ); + QObject &operator=( const QObject & ); +#endif +}; + + +#ifndef QT_NO_USERDATA +class Q_EXPORT QObjectUserData { +public: + virtual ~QObjectUserData(); +}; +#endif + + +inline bool QObject::connect( const QObject *sender, const char *signal, + const char *member ) const +{ + return connect( sender, signal, this, member ); +} + + +inline bool QObject::disconnect( const char *signal, + const QObject *receiver, const char *member ) +{ + return disconnect( this, signal, receiver, member ); +} + + +inline bool QObject::disconnect( const QObject *receiver, const char *member ) +{ + return disconnect( this, 0, receiver, member ); +} + + +#ifdef QT_NO_TRANSLATION +inline QString QObject::tr( const char *sourceText, const char * ) { + return QString::fromLatin1( sourceText ); +} +#ifndef QT_NO_TEXTCODEC +inline QString QObject::trUtf8( const char *sourceText, const char * ) { + return QString::fromUtf8( sourceText ); +} +#endif +#endif //QT_NO_TRANSLATION + + +#define Q_DEFINED_QOBJECT +#include "qwinexport.h" +#endif // QOBJECT_H diff --git a/src/kernel/qobjectcleanuphandler.cpp b/src/kernel/qobjectcleanuphandler.cpp new file mode 100644 index 0000000..98b0309 --- /dev/null +++ b/src/kernel/qobjectcleanuphandler.cpp @@ -0,0 +1,172 @@ +/**************************************************************************** +** +** Implementation of QObjectCleanupHandler class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qobjectcleanuphandler.h" +#include "qobjectlist.h" + +/*! + \class QObjectCleanupHandler qobjectcleanuphandler.h + \brief The QObjectCleanupHandler class watches the lifetime of multiple QObjects. + + \ingroup objectmodel + + A QObjectCleanupHandler is useful whenever you need to know when a + number of \l{QObject}s that are owned by someone else have been + deleted. This is important, for example, when referencing memory + in an application that has been allocated in a shared library. + + Example: + + \code + class FactoryComponent : public FactoryInterface, public QLibraryInterface + { + public: + ... + + QObject *createObject(); + + bool init(); + void cleanup(); + bool canUnload() const; + + private: + QObjectCleanupHandler objects; + }; + + // allocate a new object, and add it to the cleanup handler + QObject *FactoryComponent::createObject() + { + return objects.add( new QObject() ); + } + + // QLibraryInterface implementation + bool FactoryComponent::init() + { + return TRUE; + } + + void FactoryComponent::cleanup() + { + } + + // it is only safe to unload the library when all QObject's have been destroyed + bool FactoryComponent::canUnload() const + { + return objects.isEmpty(); + } + \endcode +*/ + +/*! + Constructs an empty QObjectCleanupHandler. +*/ +QObjectCleanupHandler::QObjectCleanupHandler() +: QObject(), cleanupObjects( 0 ) +{ +} + +/*! + Destroys the cleanup handler. All objects in this cleanup handler + will be deleted. +*/ +QObjectCleanupHandler::~QObjectCleanupHandler() +{ + clear(); +} + +/*! + Adds \a object to this cleanup handler and returns the pointer to + the object. +*/ +QObject* QObjectCleanupHandler::add( QObject* object ) +{ + if ( !object ) + return 0; + + if ( !cleanupObjects ) { + cleanupObjects = new QObjectList; + cleanupObjects->setAutoDelete( TRUE ); + } + connect( object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)) ); + cleanupObjects->insert( 0, object ); + return object; +} + +/*! + Removes the \a object from this cleanup handler. The object will + not be destroyed. +*/ +void QObjectCleanupHandler::remove( QObject *object ) +{ + if ( !cleanupObjects ) + return; + if ( cleanupObjects->findRef( object ) >= 0 ) { + (void) cleanupObjects->take(); + disconnect( object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)) ); + } +} + +/*! + Returns TRUE if this cleanup handler is empty or if all objects in + this cleanup handler have been destroyed; otherwise return FALSE. +*/ +bool QObjectCleanupHandler::isEmpty() const +{ + return cleanupObjects ? cleanupObjects->isEmpty() : TRUE; +} + +/*! + Deletes all objects in this cleanup handler. The cleanup handler + becomes empty. +*/ +void QObjectCleanupHandler::clear() +{ + delete cleanupObjects; + cleanupObjects = 0; +} + +void QObjectCleanupHandler::objectDestroyed( QObject*object ) +{ + if ( cleanupObjects ) + cleanupObjects->setAutoDelete( FALSE ); + + remove( object ); + + if ( cleanupObjects ) + cleanupObjects->setAutoDelete( TRUE ); +} diff --git a/src/kernel/qobjectcleanuphandler.h b/src/kernel/qobjectcleanuphandler.h new file mode 100644 index 0000000..348217c --- /dev/null +++ b/src/kernel/qobjectcleanuphandler.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Definition of ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QOBJECTCLEANUPHANDLER_H +#define QOBJECTCLEANUPHANDLER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +class QObjectList; + +class Q_EXPORT QObjectCleanupHandler : public QObject +{ + Q_OBJECT + +public: + QObjectCleanupHandler(); + ~QObjectCleanupHandler(); + + QObject* add( QObject* object ); + void remove( QObject *object ); + bool isEmpty() const; + void clear(); + +private: + QObjectList *cleanupObjects; + +private slots: + void objectDestroyed( QObject * ); +}; + +#endif // QOBJECTCLEANUPHANDLER_H diff --git a/src/kernel/qobjectdefs.h b/src/kernel/qobjectdefs.h new file mode 100644 index 0000000..37605bd --- /dev/null +++ b/src/kernel/qobjectdefs.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Macros and definitions related to QObject +** +** Created : 930419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QOBJECTDEFS_H +#define QOBJECTDEFS_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + + +#ifndef QT_NO_TRANSLATION +# ifndef QT_NO_TEXTCODEC +// full set of tr functions +# define QT_TR_FUNCTIONS \ + static QString tr( const char *, const char * = 0 ); \ + static QString trUtf8( const char *, const char * = 0 ); +# else +// no QTextCodec, no utf8 +# define QT_TR_FUNCTIONS \ + static QString tr( const char *, const char * = 0 ); +# endif +#else +// inherit the ones from QObject +# define QT_TR_FUNCTIONS +#endif + +#ifndef QT_NO_PROPERTIES +# define QT_PROP_FUNCTIONS \ + virtual bool qt_property( int id, int f, QVariant* v); \ + static bool qt_static_property( QObject* , int, int, QVariant* ); +#else +# define QT_PROP_FUNCTIONS +#endif + +// The following macros are our "extensions" to C++ +// They are used, strictly speaking, only by the moc. +struct QUObject; + +#ifdef QT_MOC_CPP +#define slots slots +#define signals signals +#define Q_CLASSINFO( name, value ) Q_CLASSINFO( name, value ) +#define Q_PROPERTY( text ) Q_PROPERTY( text ) +#define Q_OVERRIDE( text ) Q_OVERRIDE( text ) +#define Q_ENUMS( x ) Q_ENUMS( x ) +#define Q_SETS( x ) Q_SETS( x ) + /* tmake ignore Q_OBJECT */ +#define Q_OBJECT Q_OBJECT + /* tmake ignore Q_OBJECT */ +#define Q_OBJECT_FAKE Q_OBJECT_FAKE + +#else +#define slots // slots: in class +#define signals protected // signals: in class +#ifndef QT_NO_EMIT +#define emit // emit signal +#endif +#define Q_CLASSINFO( name, value ) // class info +#define Q_PROPERTY( text ) // property +#define Q_OVERRIDE( text ) // override property +#define Q_ENUMS( x ) +#define Q_SETS( x ) + +/* tmake ignore Q_OBJECT */ +#define Q_OBJECT \ +public: \ + virtual QMetaObject *metaObject() const { \ + return staticMetaObject(); \ + } \ + virtual const char *className() const; \ + virtual void* qt_cast( const char* ); \ + virtual bool qt_invoke( int, QUObject* ); \ + virtual bool qt_emit( int, QUObject* ); \ + QT_PROP_FUNCTIONS \ + static QMetaObject* staticMetaObject(); \ + QObject* qObject() { return (QObject*)this; } \ + QT_TR_FUNCTIONS \ +private: \ + static QMetaObject *metaObj; + +/* tmake ignore Q_OBJECT */ +#define Q_OBJECT_FAKE Q_OBJECT + +#endif + +// macro for naming members +#ifdef METHOD +#undef METHOD +#endif +#ifdef SLOT +#undef SLOT +#endif +#ifdef SIGNAL +#undef SIGNAL +#endif + +#if defined(_OLD_CPP_) +#define METHOD(a) "0""a" +#define SLOT(a) "1""a" +#define SIGNAL(a) "2""a" +#else +#define METHOD(a) "0"#a +#define SLOT(a) "1"#a +#define SIGNAL(a) "2"#a +#endif + +#ifndef QT_CLEAN_NAMESPACE +#define METHOD_CODE 0 // member type codes +#define SLOT_CODE 1 +#define SIGNAL_CODE 2 +#endif + +#define QMETHOD_CODE 0 // member type codes +#define QSLOT_CODE 1 +#define QSIGNAL_CODE 2 + +class QObject; +class QMetaObject; +class QSignal; +class QConnection; +class QEvent; +struct QMetaData; +class QConnectionList; +class QConnectionListIt; +class QSignalVec; +class QObjectList; +class QObjectListIt; +class QMemberDict; + +Q_EXPORT void *qt_find_obj_child( QObject *, const char *, const char * ); +#define Q_CHILD(parent,type,name) \ + ((type*)qt_find_obj_child(parent,#type,name)) + +Q_EXPORT void *qt_inheritedBy( QMetaObject *super, const QObject *cls ); + +template <typename T> +Q_INLINE_TEMPLATES T qt_cast(const QObject *object) +{ return (T)qt_inheritedBy( ((T)0)->staticMetaObject(), object ); } +#endif // QOBJECTDEFS_H diff --git a/src/kernel/qobjectdict.h b/src/kernel/qobjectdict.h new file mode 100644 index 0000000..ed6afc0 --- /dev/null +++ b/src/kernel/qobjectdict.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Definition of QObjectDictionary +** +** Created : 940807 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QOBJECTDICT_H +#define QOBJECTDICT_H + +#ifndef QT_H +#include "qmetaobject.h" +#include "qasciidict.h" +#endif // QT_H + + +// +// The object dictionary is a collection of QMetaObjects +// + +class Q_EXPORT QObjectDictionary : public QAsciiDict<QMetaObject> +{ +public: + QObjectDictionary(int size=17,bool cs=TRUE,bool ck=TRUE) + : QAsciiDict<QMetaObject>(size,cs,ck) {} + QObjectDictionary( const QObjectDictionary &dict ) + : QAsciiDict<QMetaObject>(dict) {} + ~QObjectDictionary() { clear(); } + QObjectDictionary &operator=(const QObjectDictionary &dict) + { return (QObjectDictionary&)QAsciiDict<QMetaObject>::operator=(dict);} +}; + +#endif // QOBJECTDICT_H diff --git a/src/kernel/qobjectlist.h b/src/kernel/qobjectlist.h new file mode 100644 index 0000000..57e146b --- /dev/null +++ b/src/kernel/qobjectlist.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Definition of QObjectList +** +** Created : 940807 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QOBJECTLIST_H +#define QOBJECTLIST_H + +#ifndef QT_H +#include "qobject.h" +#include "qptrlist.h" +#endif // QT_H + + +#if defined(Q_TEMPLATEDLL) +//Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QObject>; +//Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrListIterator<QObject>; +#endif + + +class Q_EXPORT QObjectList : public QPtrList<QObject> +{ +public: + QObjectList() : QPtrList<QObject>() {} + QObjectList( const QObjectList &list ) : QPtrList<QObject>(list) {} + ~QObjectList() { clear(); } + QObjectList &operator=(const QObjectList &list) + { return (QObjectList&)QPtrList<QObject>::operator=(list); } +}; + +class Q_EXPORT QObjectListIterator : public QPtrListIterator<QObject> +{ +public: + QObjectListIterator( const QObjectList &l ) + : QPtrListIterator<QObject>( l ) { } + QObjectListIterator &operator=( const QObjectListIterator &i ) + { return (QObjectListIterator&) + QPtrListIterator<QObject>::operator=( i ); } +}; + +#if (QT_VERSION-0 >= 0x040000) +#if defined(Q_CC_GNU) +#warning "remove the QObjectListIt class" +#warning "remove the typedef too, maybe" +#endif +typedef QObjectListIterator QObjectListIt; +#else +class Q_EXPORT QObjectListIt : public QPtrListIterator<QObject> +{ +public: + QObjectListIt( const QObjectList &l ) : QPtrListIterator<QObject>(l) {} + QObjectListIt &operator=(const QObjectListIt &i) + { return (QObjectListIt&)QPtrListIterator<QObject>::operator=(i); } +}; +#endif + +#endif // QOBJECTLIST_H diff --git a/src/kernel/qpaintdevice.h b/src/kernel/qpaintdevice.h new file mode 100644 index 0000000..75d7de1 --- /dev/null +++ b/src/kernel/qpaintdevice.h @@ -0,0 +1,421 @@ +/**************************************************************************** +** +** Definition of QPaintDevice class +** +** Created : 940721 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPAINTDEVICE_H +#define QPAINTDEVICE_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qrect.h" +#endif // QT_H + +#if defined(Q_WS_QWS) +class QWSDisplay; +class QGfx; +#endif + +class QIODevice; +class QString; +class QTextItem; + + +#if defined(Q_WS_X11) +struct QPaintDeviceX11Data; +#endif + +union QPDevCmdParam { + int ival; + int *ivec; + QString *str; + const QPoint *point; + const QRect *rect; + const QPointArray *ptarr; + const QPixmap *pixmap; + const QImage *image; + const QColor *color; + const QFont *font; + const QPen *pen; + const QBrush *brush; + const QRegion *rgn; + const QWMatrix *matrix; + const QTextItem *textItem; + QIODevice *device; +}; + + + +class Q_EXPORT QPaintDevice // device for QPainter +{ +public: + virtual ~QPaintDevice(); + + int devType() const; + bool isExtDev() const; + bool paintingActive() const; + + virtual void setResolution( int ); + virtual int resolution() const; + + // Windows: get device context + // X-Windows: get drawable +#if defined(Q_WS_WIN) + virtual HDC handle() const; +#elif defined(Q_WS_X11) + virtual Qt::HANDLE handle() const; + virtual Qt::HANDLE x11RenderHandle() const; +#elif defined(Q_WS_MAC) + virtual Qt::HANDLE handle() const; +#elif defined(Q_WS_QWS) + virtual Qt::HANDLE handle() const; +#endif + +#if defined(Q_WS_X11) + Display *x11Display() const; + int x11Screen() const; + int x11Depth() const; + int x11Cells() const; + Qt::HANDLE x11Colormap() const; + bool x11DefaultColormap() const; + void *x11Visual() const; + bool x11DefaultVisual() const; + + static Display *x11AppDisplay(); + static int x11AppScreen(); + + static int x11AppDpiX(); + static int x11AppDpiY(); + static void x11SetAppDpiX(int); + static void x11SetAppDpiY(int); + static int x11AppDepth(); + static int x11AppCells(); + static Qt::HANDLE x11AppRootWindow(); + static Qt::HANDLE x11AppColormap(); + static bool x11AppDefaultColormap(); + static void *x11AppVisual(); + static bool x11AppDefaultVisual(); + + // ### in 4.0, the above need to go away, the below needs to take a -1 default + // argument, signifying the default screen... + static int x11AppDepth( int screen ); + static int x11AppCells( int screen ); + static Qt::HANDLE x11AppRootWindow( int screen ); + static Qt::HANDLE x11AppColormap( int screen ); + static void *x11AppVisual( int screen ); + static bool x11AppDefaultColormap( int screen ); + static bool x11AppDefaultVisual( int screen ); + static int x11AppDpiX( int ); + static int x11AppDpiY( int ); + static void x11SetAppDpiX( int, int ); + static void x11SetAppDpiY( int, int ); +#endif + +#if defined(Q_WS_QWS) + static QWSDisplay *qwsDisplay(); + virtual unsigned char * scanLine(int) const; + virtual int bytesPerLine() const; + virtual QGfx * graphicsContext(bool clip_children=TRUE) const; +#endif + + enum PDevCmd { + PdcNOP = 0, // <void> + PdcDrawPoint = 1, // point + PdcDrawFirst = PdcDrawPoint, + PdcMoveTo = 2, // point + PdcLineTo = 3, // point + PdcDrawLine = 4, // point,point + PdcDrawRect = 5, // rect + PdcDrawRoundRect = 6, // rect,ival,ival + PdcDrawEllipse = 7, // rect + PdcDrawArc = 8, // rect,ival,ival + PdcDrawPie = 9, // rect,ival,ival + PdcDrawChord = 10, // rect,ival,ival + PdcDrawLineSegments = 11, // ptarr + PdcDrawPolyline = 12, // ptarr + PdcDrawPolygon = 13, // ptarr,ival + PdcDrawCubicBezier = 14, // ptarr + PdcDrawText = 15, // point,str + PdcDrawTextFormatted = 16, // rect,ival,str + PdcDrawPixmap = 17, // rect,pixmap + PdcDrawImage = 18, // rect,image + PdcDrawText2 = 19, // point,str + PdcDrawText2Formatted = 20, // rect,ival,str + PdcDrawTextItem = 21, + PdcDrawLast = PdcDrawTextItem, + + // no painting commands below PdcDrawLast. + + PdcBegin = 30, // <void> + PdcEnd = 31, // <void> + PdcSave = 32, // <void> + PdcRestore = 33, // <void> + PdcSetdev = 34, // device - PRIVATE + PdcSetBkColor = 40, // color + PdcSetBkMode = 41, // ival + PdcSetROP = 42, // ival + PdcSetBrushOrigin = 43, // point + PdcSetFont = 45, // font + PdcSetPen = 46, // pen + PdcSetBrush = 47, // brush + PdcSetTabStops = 48, // ival + PdcSetTabArray = 49, // ival,ivec + PdcSetUnit = 50, // ival + PdcSetVXform = 51, // ival + PdcSetWindow = 52, // rect + PdcSetViewport = 53, // rect + PdcSetWXform = 54, // ival + PdcSetWMatrix = 55, // matrix,ival + PdcSaveWMatrix = 56, + PdcRestoreWMatrix = 57, + PdcSetClip = 60, // ival + PdcSetClipRegion = 61, // rgn + + PdcReservedStart = 0, // codes 0-199 are reserved + PdcReservedStop = 199 // for Qt + }; + +protected: + QPaintDevice( uint devflags ); + +#if defined(Q_WS_WIN) + HDC hdc; // device context +#elif defined(Q_WS_X11) + Qt::HANDLE hd; // handle to drawable + Qt::HANDLE rendhd; // handle to RENDER pict + + void copyX11Data( const QPaintDevice * ); + void cloneX11Data( const QPaintDevice * ); + virtual void setX11Data( const QPaintDeviceX11Data* ); + QPaintDeviceX11Data* getX11Data( bool def=FALSE ) const; +#elif defined(Q_WS_MAC) +#if !defined( QMAC_NO_QUARTZ ) + CGContextRef ctx; +#endif + void * hd; +#elif defined(Q_WS_QWS) + Qt::HANDLE hd; +#endif + + virtual bool cmd( int, QPainter *, QPDevCmdParam * ); + virtual int metric( int ) const; + virtual int fontMet( QFont *, int, const char * = 0, int = 0 ) const; + virtual int fontInf( QFont *, int ) const; + + ushort devFlags; // device flags + ushort painters; // refcount + + friend class QPainter; + friend class QPaintDeviceMetrics; +#if defined(Q_WS_MAC) +#ifndef QMAC_NO_QUARTZ + virtual CGContextRef macCGContext(bool clipped=TRUE) const; +#endif + friend Q_EXPORT void unclippedScaledBitBlt( QPaintDevice *, int, int, int, int, + const QPaintDevice *, int, int, int, int, Qt::RasterOp, bool, bool ); +#else + friend Q_EXPORT void bitBlt( QPaintDevice *, int, int, + const QPaintDevice *, + int, int, int, int, Qt::RasterOp, bool ); +#endif +#if defined(Q_WS_X11) + friend void qt_init_internal( int *, char **, Display *, Qt::HANDLE, Qt::HANDLE ); + friend void qt_cleanup(); +#endif + +private: +#if defined(Q_WS_X11) + static Display *x_appdisplay; + static int x_appscreen; + + static int x_appdepth; + static int x_appcells; + static Qt::HANDLE x_approotwindow; + static Qt::HANDLE x_appcolormap; + static bool x_appdefcolormap; + static void *x_appvisual; + static bool x_appdefvisual; + + // ### in 4.0, remove the above, and replace with the below + static int *x_appdepth_arr; + static int *x_appcells_arr; + static Qt::HANDLE *x_approotwindow_arr; + static Qt::HANDLE *x_appcolormap_arr; + static bool *x_appdefcolormap_arr; + static void **x_appvisual_arr; + static bool *x_appdefvisual_arr; + + QPaintDeviceX11Data* x11Data; +#endif + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QPaintDevice( const QPaintDevice & ); + QPaintDevice &operator=( const QPaintDevice & ); +#endif +}; + + +Q_EXPORT +void bitBlt( QPaintDevice *dst, int dx, int dy, + const QPaintDevice *src, int sx=0, int sy=0, int sw=-1, int sh=-1, + Qt::RasterOp = Qt::CopyROP, bool ignoreMask=FALSE ); + +Q_EXPORT +void bitBlt( QPaintDevice *dst, int dx, int dy, + const QImage *src, int sx=0, int sy=0, int sw=-1, int sh=-1, + int conversion_flags=0 ); + + +#if defined(Q_WS_X11) + +struct Q_EXPORT QPaintDeviceX11Data : public QShared { + Display* x_display; + int x_screen; + int x_depth; + int x_cells; + Qt::HANDLE x_colormap; + bool x_defcolormap; + void* x_visual; + bool x_defvisual; +}; + +#endif + +/***************************************************************************** + Inline functions + *****************************************************************************/ + +inline int QPaintDevice::devType() const +{ return devFlags & QInternal::DeviceTypeMask; } + +inline bool QPaintDevice::isExtDev() const +{ return (devFlags & QInternal::ExternalDevice) != 0; } + +inline bool QPaintDevice::paintingActive() const +{ return painters != 0; } + +#if defined(Q_WS_X11) +inline Display *QPaintDevice::x11Display() const +{ return x11Data ? x11Data->x_display : x_appdisplay; } + +inline int QPaintDevice::x11Screen() const +{ return x11Data ? x11Data->x_screen : x_appscreen; } + +inline int QPaintDevice::x11Depth() const +{ return x11Data ? x11Data->x_depth : x_appdepth; } + +inline int QPaintDevice::x11Cells() const +{ return x11Data ? x11Data->x_cells : x_appcells; } + +inline Qt::HANDLE QPaintDevice::x11Colormap() const +{ return x11Data ? x11Data->x_colormap : x_appcolormap; } + +inline bool QPaintDevice::x11DefaultColormap() const +{ return x11Data ? x11Data->x_defcolormap : x_appdefcolormap; } + +inline void *QPaintDevice::x11Visual() const +{ return x11Data ? x11Data->x_visual : x_appvisual; } + +inline bool QPaintDevice::x11DefaultVisual() const +{ return x11Data ? x11Data->x_defvisual : x_appdefvisual; } + +inline Display *QPaintDevice::x11AppDisplay() +{ return x_appdisplay; } + +inline int QPaintDevice::x11AppScreen() +{ return x_appscreen; } + +inline int QPaintDevice::x11AppDepth( int screen ) +{ return x_appdepth_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline int QPaintDevice::x11AppCells( int screen ) +{ return x_appcells_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline Qt::HANDLE QPaintDevice::x11AppRootWindow( int screen ) +{ return x_approotwindow_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline Qt::HANDLE QPaintDevice::x11AppColormap( int screen ) +{ return x_appcolormap_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline bool QPaintDevice::x11AppDefaultColormap( int screen ) +{ return x_appdefcolormap_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline void *QPaintDevice::x11AppVisual( int screen ) +{ return x_appvisual_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline bool QPaintDevice::x11AppDefaultVisual( int screen ) +{ return x_appdefvisual_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline int QPaintDevice::x11AppDepth() +{ return x_appdepth; } + +inline int QPaintDevice::x11AppCells() +{ return x_appcells; } + +inline Qt::HANDLE QPaintDevice::x11AppRootWindow() +{ return x_approotwindow; } + +inline Qt::HANDLE QPaintDevice::x11AppColormap() +{ return x_appcolormap; } + +inline bool QPaintDevice::x11AppDefaultColormap() +{ return x_appdefcolormap; } + +inline void *QPaintDevice::x11AppVisual() +{ return x_appvisual; } + +inline bool QPaintDevice::x11AppDefaultVisual() +{ return x_appdefvisual; } + +#endif // Q_WS_X11 + + +Q_EXPORT +inline void bitBlt( QPaintDevice *dst, const QPoint &dp, + const QPaintDevice *src, const QRect &sr =QRect(0,0,-1,-1), + Qt::RasterOp rop=Qt::CopyROP, bool ignoreMask=FALSE ) +{ + bitBlt( dst, dp.x(), dp.y(), src, sr.x(), sr.y(), sr.width(), sr.height(), + rop, ignoreMask ); +} + + + + +#endif // QPAINTDEVICE_H diff --git a/src/kernel/qpaintdevice_x11.cpp b/src/kernel/qpaintdevice_x11.cpp new file mode 100644 index 0000000..a755c01 --- /dev/null +++ b/src/kernel/qpaintdevice_x11.cpp @@ -0,0 +1,1168 @@ +/**************************************************************************** +** +** Implementation of QPaintDevice class for X11 +** +** Created : 940721 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpaintdevice.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qwidget.h" +#include "qbitmap.h" +#include "qapplication.h" +#include "qt_x11_p.h" + + +/*! + \class QPaintDevice qpaintdevice.h + \brief The QPaintDevice class is the base class of objects that + can be painted. + + \ingroup graphics + \ingroup images + + A paint device is an abstraction of a two-dimensional space that + can be drawn using a QPainter. The drawing capabilities are + implemented by the subclasses QWidget, QPixmap, QPicture and + QPrinter. + + The default coordinate system of a paint device has its origin + located at the top-left position. X increases to the right and Y + increases downward. The unit is one pixel. There are several ways + to set up a user-defined coordinate system using the painter, for + example, using QPainter::setWorldMatrix(). + + Example (draw on a paint device): + \code + void MyWidget::paintEvent( QPaintEvent * ) + { + QPainter p; // our painter + p.begin( this ); // start painting the widget + p.setPen( red ); // red outline + p.setBrush( yellow ); // yellow fill + p.drawEllipse( 10, 20, 100,100 ); // 100x100 ellipse at position (10, 20) + p.end(); // painting done + } + \endcode + + The bit block transfer is an extremely useful operation for + copying pixels from one paint device to another (or to itself). It + is implemented as the global function bitBlt(). + + Example (scroll widget contents 10 pixels to the right): + \code + bitBlt( myWidget, 10, 0, myWidget ); + \endcode + + \warning Qt requires that a QApplication object exists before + any paint devices can be created. Paint devices access window + system resources, and these resources are not initialized before + an application object is created. +*/ + + +// +// Some global variables - these are initialized by QColor::initialize() +// + +Display *QPaintDevice::x_appdisplay = 0; +int QPaintDevice::x_appscreen; + +int QPaintDevice::x_appdepth; +int QPaintDevice::x_appcells; +Qt::HANDLE QPaintDevice::x_approotwindow; +Qt::HANDLE QPaintDevice::x_appcolormap; +bool QPaintDevice::x_appdefcolormap; +void *QPaintDevice::x_appvisual; +bool QPaintDevice::x_appdefvisual; + +// ### in 4.0, remove the above, and use the below +int *QPaintDevice::x_appdepth_arr; +int *QPaintDevice::x_appcells_arr; +Qt::HANDLE *QPaintDevice::x_approotwindow_arr; +Qt::HANDLE *QPaintDevice::x_appcolormap_arr; +bool *QPaintDevice::x_appdefcolormap_arr; +void **QPaintDevice::x_appvisual_arr; +bool *QPaintDevice::x_appdefvisual_arr; + +/*! + \enum QPaintDevice::PDevCmd + \internal +*/ + +/*! + Constructs a paint device with internal flags \a devflags. This + constructor can be invoked only from QPaintDevice subclasses. +*/ + +QPaintDevice::QPaintDevice( uint devflags ) +{ + if ( !qApp ) { // global constructor +#if defined(QT_CHECK_STATE) + qFatal( "QPaintDevice: Must construct a QApplication before a " + "QPaintDevice" ); +#endif + return; + } + devFlags = devflags; + painters = 0; + hd = 0; + rendhd = 0; + x11Data = 0; +} + +/*! + Destroys the paint device and frees window system resources. +*/ + +QPaintDevice::~QPaintDevice() +{ +#if defined(QT_CHECK_STATE) + if ( paintingActive() ) + qWarning( "QPaintDevice: Cannot destroy paint device that is being " + "painted" ); +#endif + if ( x11Data && x11Data->deref() ) { + delete x11Data; + x11Data = 0; + } +} + + +/* + \internal + Makes a shallow copy of the X11-specific data of \a fromDevice, if it is not + null. Otherwise this function sets it to null. +*/ + +void QPaintDevice::copyX11Data( const QPaintDevice *fromDevice ) +{ + setX11Data( fromDevice ? fromDevice->x11Data : 0 ); +} + +/* + \internal + Makes a deep copy of the X11-specific data of \a fromDevice, if it is not + null. Otherwise this function sets it to null. +*/ + +void QPaintDevice::cloneX11Data( const QPaintDevice *fromDevice ) +{ + if ( fromDevice && fromDevice->x11Data ) { + QPaintDeviceX11Data *d = new QPaintDeviceX11Data; + *d = *fromDevice->x11Data; + d->count = 0; + setX11Data( d ); + } else { + setX11Data( 0 ); + } +} + +/* + \internal + Makes a shallow copy of the X11-specific data \a d and assigns it to this + class. This function increments the reference code of \a d. +*/ + +void QPaintDevice::setX11Data( const QPaintDeviceX11Data* d ) +{ + if ( x11Data && x11Data->deref() ) + delete x11Data; + x11Data = (QPaintDeviceX11Data*)d; + if ( x11Data ) + x11Data->ref(); +} + + +/* + \internal + If \a def is FALSE, returns a deep copy of the x11Data, or 0 if x11Data is 0. + If \a def is TRUE, makes a QPaintDeviceX11Data struct filled with the default + values. + + In either case the caller is responsible for deleting the returned + struct. But notice that the struct is a shared class, so other + classes might also have a reference to it. The reference count of + the returned QPaintDeviceX11Data* is 0. +*/ + +QPaintDeviceX11Data* QPaintDevice::getX11Data( bool def ) const +{ + QPaintDeviceX11Data* res = 0; + if ( def ) { + res = new QPaintDeviceX11Data; + res->x_display = x11AppDisplay(); + res->x_screen = x11AppScreen(); + res->x_depth = x11AppDepth(); + res->x_cells = x11AppCells(); + res->x_colormap = x11Colormap(); + res->x_defcolormap = x11AppDefaultColormap(); + res->x_visual = x11AppVisual(); + res->x_defvisual = x11AppDefaultVisual(); + res->deref(); + } else if ( x11Data ) { + res = new QPaintDeviceX11Data; + *res = *x11Data; + res->count = 0; + } + return res; +} + + +/*! + \fn int QPaintDevice::devType() const + + \internal + + Returns the device type identifier, which is \c QInternal::Widget + if the device is a QWidget, \c QInternal::Pixmap if it's a + QPixmap, \c QInternal::Printer if it's a QPrinter, \c + QInternal::Picture if it's a QPicture or \c + QInternal::UndefinedDevice in other cases (which should never + happen). +*/ + +/*! + \fn bool QPaintDevice::isExtDev() const + + Returns TRUE if the device is an external paint device; otherwise + returns FALSE. + + External paint devices cannot be bitBlt()'ed from. QPicture and + QPrinter are external paint devices. +*/ + +/*! + Returns the window system handle of the paint device, for + low-level access. Using this function is not portable. + + The HANDLE type varies with platform; see \c qpaintdevice.h and + \c qwindowdefs.h for details. + + \sa x11Display() +*/ +Qt::HANDLE QPaintDevice::handle() const +{ + return hd; +} + +/*! + Returns the window system handle of the paint device for XRender + support. Use of this function is not portable. This function will + return 0 if XRender support is not compiled into Qt, if the + XRender extension is not supported on the X11 display, or if the + handle could not be created. +*/ +Qt::HANDLE QPaintDevice::x11RenderHandle() const +{ +#ifndef QT_NO_XFTFREETYPE + return rendhd ? XftDrawPicture( (XftDraw *) rendhd ) : 0; +#else + return 0; +#endif // QT_NO_XFTFREETYPE +} + + +/*! + \fn Display *QPaintDevice::x11AppDisplay() + + Returns a pointer to the X display global to the application (X11 + only). Using this function is not portable. + + \sa handle() +*/ + +/*! + \fn int QPaintDevice::x11AppScreen() + + Returns the screen number on the X display global to the + application (X11 only). Using this function is not portable. +*/ + +/*! + \overload + \fn int QPaintDevice::x11AppDepth() + + Returns the depth for the default screen of the X display global + to the application (X11 only). Using this function is not + portable. + + \sa QPixmap::defaultDepth() +*/ + +/*! + \fn int QPaintDevice::x11AppCells() + + Returns the number of entries in the colormap for the default + screen of the X display global to the application (X11 + only). Using this function is not portable. + + \sa x11Colormap() +*/ + +/*! + \fn HANDLE QPaintDevice::x11AppRootWindow() + + Returns the root window for the default screen of the X display + global to the applicatoin (X11 only). Using this function is not + portable. +*/ + +/*! + \fn HANDLE QPaintDevice::x11AppColormap() + + Returns the colormap for the default screen of the X display + global to the application (X11 only). Using this function is not + portable. + + \sa x11Cells() +*/ + +/*! + \fn bool QPaintDevice::x11AppDefaultColormap () + + Returns the default colormap for the default screen of the X + display global to the application (X11 only). Using this function + is not portable. + + \sa x11Cells() +*/ + +/*! + \fn void* QPaintDevice::x11AppVisual () + + Returns the Visual for the default screen of the X display global + to the application (X11 only). Using this function is not + portable. +*/ + +/*! + \fn bool QPaintDevice::x11AppDefaultVisual () + + Returns TRUE if the Visual used is the default for the default + screen of the X display global to the application (X11 only); + otherwise returns FALSE. Using this function is not portable. +*/ + +/*! + \fn int QPaintDevice::x11AppDepth( int screen ) + + Returns the depth for screen \a screen of the X display global to + the application (X11 only). Using this function is not portable. + + \sa QPixmap::defaultDepth() +*/ + +/*! + \overload + \fn int QPaintDevice::x11AppCells( int screen ) + + Returns the number of entries in the colormap for screen \a screen + of the X display global to the application (X11 only). Using this + function is not portable. + + \sa x11Colormap() +*/ + +/*! + \overload + \fn HANDLE QPaintDevice::x11AppRootWindow( int screen ) + + Returns the root window for screen \a screen of the X display + global to the applicatoin (X11 only). Using this function is not + portable. +*/ + +/*! + \overload + \fn HANDLE QPaintDevice::x11AppColormap( int screen ) + + Returns the colormap for screen \a screen of the X display global + to the application (X11 only). Using this function is not + portable. + + \sa x11Cells() +*/ + +/*! + \overload + \fn bool QPaintDevice::x11AppDefaultColormap( int screen ) + + Returns the default colormap for screen \a screen of the X display + global to the application (X11 only). Using this function is not + portable. + + \sa x11Cells() +*/ + +/*! + \overload + \fn void* QPaintDevice::x11AppVisual( int screen ) + + Returns the Visual for screen \a screen of the X display global to + the application (X11 only). Using this function is not portable. +*/ + +/*! + \overload + \fn bool QPaintDevice::x11AppDefaultVisual( int screen ) + + Returns TRUE if the Visual used is the default for screen + \a screen of the X display global to the application (X11 only); + otherwise returns FALSE. Using this function is not portable. +*/ + + +/*! + \fn Display *QPaintDevice::x11Display() const + + Returns a pointer to the X display for the paint device (X11 + only). Using this function is not portable. + + \sa handle() +*/ + +/*! + \fn int QPaintDevice::x11Screen () const + + Returns the screen number on the X display for the paint device + (X11 only). Using this function is not portable. +*/ + +/*! + \fn int QPaintDevice::x11Depth () const + + Returns the depth of the X display for the paint device (X11 + only). Using this function is not portable. + + \sa QPixmap::defaultDepth() +*/ + +/*! + \fn int QPaintDevice::x11Cells () const + + Returns the number of entries in the colormap of the X display for + the paint device (X11 only). Using this function is not portable. + + \sa x11Colormap() +*/ + +/*! + \fn HANDLE QPaintDevice::x11Colormap () const + + Returns the colormap of the X display for the paint device (X11 + only). Using this function is not portable. + + \sa x11Cells() +*/ + +/*! + \fn bool QPaintDevice::x11DefaultColormap () const + + Returns the default colormap of the X display for the paint device + (X11 only). Using this function is not portable. + + \sa x11Cells() +*/ + +/*! + \fn void* QPaintDevice::x11Visual () const + + Returns the Visual of the X display for the paint device (X11 + only). Using this function is not portable. +*/ + +/*! + \fn bool QPaintDevice::x11DefaultVisual () const + + Returns the default Visual of the X display for the paint device + (X11 only). Using this function is not portable. +*/ + +static int *dpisX=0, *dpisY=0; +static void create_dpis() +{ + if ( dpisX ) + return; + + Display *dpy = QPaintDevice::x11AppDisplay(); + if ( ! dpy ) + return; + + int i, screens = ScreenCount( dpy ); + dpisX = new int[ screens ]; + dpisY = new int[ screens ]; + Q_CHECK_PTR( dpisX ); + Q_CHECK_PTR( dpisY ); + for ( i = 0; i < screens; i++ ) { + dpisX[ i ] = (DisplayWidth(dpy,i) * 254 + DisplayWidthMM(dpy,i)*5) + + / (DisplayWidthMM(dpy,i)*10); + dpisY[ i ] = (DisplayHeight(dpy,i) * 254 + DisplayHeightMM(dpy,i)*5) + / (DisplayHeightMM(dpy,i)*10); + } +} + +/*! + Sets the value returned by x11AppDpiX() to \a dpi for screen + \a screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. + + \sa x11SetAppDpiY() +*/ +void QPaintDevice::x11SetAppDpiX(int dpi, int screen) +{ + create_dpis(); + if ( ! dpisX ) + return; + if ( screen < 0 ) + screen = QPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( QPaintDevice::x11AppDisplay() ) ) + return; + dpisX[ screen ] = dpi; +} + +/*! + \overload + + Sets the value returned by x11AppDpiX() to \a dpi for the default + screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. + +*/ +// ### REMOVE 4.0 +void QPaintDevice::x11SetAppDpiX( int dpi ) +{ + QPaintDevice::x11SetAppDpiX( dpi, -1 ); +} + +/*! + Sets the value returned by x11AppDpiY() to \a dpi for screen + \a screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. + + \sa x11SetAppDpiX() +*/ +void QPaintDevice::x11SetAppDpiY(int dpi, int screen) +{ + create_dpis(); + if ( ! dpisY ) + return; + if ( screen < 0 ) + screen = QPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( QPaintDevice::x11AppDisplay() ) ) + return; + dpisY[ screen ] = dpi; +} + +/*! + \overload + + Sets the value returned by x11AppDpiY() to \a dpi for the default + screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. +*/ +// ### REMOVE 4.0 +void QPaintDevice::x11SetAppDpiY( int dpi ) +{ + QPaintDevice::x11SetAppDpiY( dpi, -1 ); +} + +/*! + Returns the horizontal DPI of the X display (X11 only) for screen + \a screen. Using this function is not portable. See + QPaintDeviceMetrics for portable access to related information. + Using this function is not portable. + + \sa x11AppDpiY(), x11SetAppDpiX(), QPaintDeviceMetrics::logicalDpiX() +*/ +int QPaintDevice::x11AppDpiX(int screen) +{ + create_dpis(); + if ( ! dpisX ) + return 0; + if ( screen < 0 ) + screen = QPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( QPaintDevice::x11AppDisplay() ) ) + return 0; + return dpisX[ screen ]; +} + +/*! + \overload + + Returns the horizontal DPI of the X display (X11 only) for the + default screen. Using this function is not portable. See + QPaintDeviceMetrics for portable access to related information. + Using this function is not portable. +*/ +int QPaintDevice::x11AppDpiX() +{ + return QPaintDevice::x11AppDpiX( -1 ); +} + +/*! + Returns the vertical DPI of the X11 display (X11 only) for screen + \a screen. Using this function is not portable. See + QPaintDeviceMetrics for portable access to related information. + Using this function is not portable. + + \sa x11AppDpiX(), x11SetAppDpiY(), QPaintDeviceMetrics::logicalDpiY() +*/ +int QPaintDevice::x11AppDpiY( int screen ) +{ + create_dpis(); + if ( ! dpisY ) + return 0; + if ( screen < 0 ) + screen = QPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( QPaintDevice::x11AppDisplay() ) ) + return 0; + return dpisY[ screen ]; +} + +/*! + \overload + + Returns the vertical DPI of the X11 display (X11 only) for the + default screen. Using this function is not portable. See + QPaintDeviceMetrics for portable access to related information. + Using this function is not portable. + + \sa x11AppDpiX(), x11SetAppDpiY(), QPaintDeviceMetrics::logicalDpiY() +*/ +int QPaintDevice::x11AppDpiY() +{ + return QPaintDevice::x11AppDpiY( -1 ); +} + +/*! + \fn bool QPaintDevice::paintingActive() const + + Returns TRUE if the device is being painted, i.e. someone has + called QPainter::begin() but not yet called QPainter::end() for + this device; otherwise returns FALSE. + + \sa QPainter::isActive() +*/ + +/*! + Internal virtual function that interprets drawing commands from + the painter. + + Implemented by subclasses that have no direct support for drawing + graphics (external paint devices, for example, QPicture). +*/ + +bool QPaintDevice::cmd( int, QPainter *, QPDevCmdParam * ) +{ +#if defined(QT_CHECK_STATE) + qWarning( "QPaintDevice::cmd: Device has no command interface" ); +#endif + return FALSE; +} + +/*! + \internal + + Internal virtual function that returns paint device metrics. + + Please use the QPaintDeviceMetrics class instead. +*/ + +int QPaintDevice::metric( int ) const +{ +#if defined(QT_CHECK_STATE) + qWarning( "QPaintDevice::metrics: Device has no metric information" ); +#endif + return 0; +} + +/*! + \internal + + Internal virtual function. Reserved for future use. + + Please use the QFontMetrics class instead. +*/ + +int QPaintDevice::fontMet( QFont *, int, const char *, int ) const +{ + return 0; +} + +/*! + \internal + + Internal virtual function. Reserved for future use. + + Please use the QFontInfo class instead. +*/ + +int QPaintDevice::fontInf( QFont *, int ) const +{ + return 0; +} + + +// +// Internal functions for simple GC caching for blt'ing masked pixmaps. +// This cache is used when the pixmap optimization is set to Normal +// and the pixmap size doesn't exceed 128x128. +// + +static bool init_mask_gc = FALSE; +static const int max_mask_gcs = 11; // suitable for hashing + +struct mask_gc { + GC gc; + int mask_no; +}; + +static mask_gc gc_vec[max_mask_gcs]; + + +static void cleanup_mask_gc() +{ + Display *dpy = QPaintDevice::x11AppDisplay(); + init_mask_gc = FALSE; + for ( int i=0; i<max_mask_gcs; i++ ) { + if ( gc_vec[i].gc ) + XFreeGC( dpy, gc_vec[i].gc ); + } +} + +static GC cache_mask_gc( Display *dpy, Drawable hd, int mask_no, Pixmap mask ) +{ + if ( !init_mask_gc ) { // first time initialization + init_mask_gc = TRUE; + qAddPostRoutine( cleanup_mask_gc ); + for ( int i=0; i<max_mask_gcs; i++ ) + gc_vec[i].gc = 0; + } + mask_gc *p = &gc_vec[mask_no % max_mask_gcs]; + if ( !p->gc || p->mask_no != mask_no ) { // not a perfect match + if ( !p->gc ) { // no GC + p->gc = XCreateGC( dpy, hd, 0, 0 ); + XSetGraphicsExposures( dpy, p->gc, False ); + } + XSetClipMask( dpy, p->gc, mask ); + p->mask_no = mask_no; + } + return p->gc; +} + + +/*! + \relates QPaintDevice + + Copies a block of pixels from \a src to \a dst, perhaps merging + each pixel according to the \link Qt::RasterOp raster operation \endlink + \a rop. \a sx, \a sy + is the top-left pixel in \a src (0, 0) by default, \a dx, \a dy is + the top-left position in \a dst and \a sw, \a sh is the size of + the copied block (all of \a src by default). + + The most common values for \a rop are CopyROP and XorROP; the \l + Qt::RasterOp documentation defines all the possible values. + + If \a ignoreMask is FALSE (the default) and \a src is a + masked QPixmap, the entire blit is masked by \a{src}->mask(). + + If \a src, \a dst, \a sw or \a sh is 0, bitBlt() does nothing. If + \a sw or \a sh is negative bitBlt() copies starting at \a sx (and + respectively, \a sy) and ending at the right end (respectively, + bottom) of \a src. + + \a src must be a QWidget or QPixmap. You cannot blit from a + QPrinter, for example. bitBlt() does nothing if you attempt to + blit from an unsupported device. + + bitBlt() does nothing if \a src has a greater depth than \e dst. + If you need to for example, draw a 24-bit pixmap on an 8-bit + widget, you must use drawPixmap(). +*/ + +void bitBlt( QPaintDevice *dst, int dx, int dy, + const QPaintDevice *src, int sx, int sy, int sw, int sh, + Qt::RasterOp rop, bool ignoreMask ) +{ + if ( !src || !dst ) { +#if defined(QT_CHECK_NULL) + Q_ASSERT( src != 0 ); + Q_ASSERT( dst != 0 ); +#endif + return; + } + if ( !src->handle() || src->isExtDev() ) + return; + + QPaintDevice *pdev = QPainter::redirect( dst ); + if ( pdev ) + dst = pdev; + + int ts = src->devType(); // from device type + int td = dst->devType(); // to device type + Display *dpy = src->x11Display(); + + if ( sw <= 0 ) { // special width + if ( sw < 0 ) + sw = src->metric( QPaintDeviceMetrics::PdmWidth ) - sx; + else + return; + } + if ( sh <= 0 ) { // special height + if ( sh < 0 ) + sh = src->metric( QPaintDeviceMetrics::PdmHeight ) - sy; + else + return; + } + + if ( dst->paintingActive() && dst->isExtDev() ) { + QPixmap *pm; // output to picture/printer + bool tmp_pm = TRUE; + if ( ts == QInternal::Pixmap ) { + pm = (QPixmap*)src; + if ( sx != 0 || sy != 0 || + sw != pm->width() || sh != pm->height() || ignoreMask ) { + QPixmap *tmp = new QPixmap( sw, sh, pm->depth() ); + bitBlt( tmp, 0, 0, pm, sx, sy, sw, sh, Qt::CopyROP, TRUE ); + if ( pm->mask() && !ignoreMask ) { + QBitmap mask( sw, sh ); + bitBlt( &mask, 0, 0, pm->mask(), sx, sy, sw, sh, + Qt::CopyROP, TRUE ); + tmp->setMask( mask ); + } + pm = tmp; + } else { + tmp_pm = FALSE; + } + } else if ( ts == QInternal::Widget ) {// bitBlt to temp pixmap + pm = new QPixmap( sw, sh ); + Q_CHECK_PTR( pm ); + bitBlt( pm, 0, 0, src, sx, sy, sw, sh ); + } else { +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Cannot bitBlt from device" ); +#endif + return; + } + QPDevCmdParam param[3]; + QRect r(dx, dy, pm->width(), pm->height()); + param[0].rect = &r; + param[1].pixmap = pm; + dst->cmd( QPaintDevice::PdcDrawPixmap, 0, param ); + if ( tmp_pm ) + delete pm; + return; + } + + switch ( ts ) { + case QInternal::Widget: + case QInternal::Pixmap: + case QInternal::System: // OK, can blt from these + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Cannot bitBlt from device type %x", ts ); +#endif + return; + } + switch ( td ) { + case QInternal::Widget: + case QInternal::Pixmap: + case QInternal::System: // OK, can blt to these + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Cannot bitBlt to device type %x", td ); +#endif + return; + } + + static const short ropCodes[] = { // ROP translation table + GXcopy, GXor, GXxor, GXandInverted, + GXcopyInverted, GXorInverted, GXequiv, GXand, + GXinvert, GXclear, GXset, GXnoop, + GXandReverse, GXorReverse, GXnand, GXnor + }; + if ( rop > Qt::LastROP ) { +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Invalid ROP code" ); +#endif + return; + } + + if ( dst->handle() == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "bitBlt: Cannot bitBlt to device" ); +#endif + return; + } + + bool mono_src; + bool mono_dst; + bool include_inferiors = FALSE; + bool graphics_exposure = FALSE; + QPixmap *src_pm; + QBitmap *mask; + + if ( ts == QInternal::Pixmap ) { + src_pm = (QPixmap*)src; + if ( src_pm->x11Screen() != dst->x11Screen() ) + src_pm->x11SetScreen( dst->x11Screen() ); + mono_src = src_pm->depth() == 1; + mask = ignoreMask ? 0 : src_pm->data->mask; + } else { + src_pm = 0; + mono_src = FALSE; + mask = 0; + include_inferiors = ((QWidget*)src)->testWFlags(Qt::WPaintUnclipped); + graphics_exposure = td == QInternal::Widget; + } + if ( td == QInternal::Pixmap ) { + if ( dst->x11Screen() != src->x11Screen() ) + ((QPixmap*)dst)->x11SetScreen( src->x11Screen() ); + mono_dst = ((QPixmap*)dst)->depth() == 1; + ((QPixmap*)dst)->detach(); // changes shared pixmap + } else { + mono_dst = FALSE; + include_inferiors = include_inferiors || + ((QWidget*)dst)->testWFlags(Qt::WPaintUnclipped); + } + + if ( mono_dst && !mono_src ) { // dest is 1-bit pixmap, source is not +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Incompatible destination pixmap" ); +#endif + return; + } + +#ifndef QT_NO_XRENDER + if (src_pm && !mono_src && src_pm->data->alphapm && !ignoreMask ) { + // use RENDER to do the blit + QPixmap *alpha = src_pm->data->alphapm; + if (src->x11RenderHandle() && + alpha->x11RenderHandle() && + dst->x11RenderHandle()) { + XRenderPictureAttributes pattr; + ulong picmask = 0; + if (include_inferiors) { + pattr.subwindow_mode = IncludeInferiors; + picmask |= CPSubwindowMode; + } + if (graphics_exposure) { + pattr.graphics_exposures = TRUE; + picmask |= CPGraphicsExposure; + } + if (picmask) + XRenderChangePicture(dpy, dst->x11RenderHandle(), picmask, &pattr); + XRenderComposite(dpy, PictOpOver, src->x11RenderHandle(), + alpha->x11RenderHandle(), dst->x11RenderHandle(), + sx, sy, sx, sy, dx, dy, sw, sh); + // restore attributes + pattr.subwindow_mode = ClipByChildren; + pattr.graphics_exposures = FALSE; + if (picmask) + XRenderChangePicture(dpy, dst->x11RenderHandle(), picmask, &pattr); + return; + } + } +#endif + + GC gc; + + if ( mask && !mono_src ) { // fast masked blt + bool temp_gc = FALSE; + if ( mask->data->maskgc ) { + gc = (GC)mask->data->maskgc; // we have a premade mask GC + } else { + if ( FALSE && src_pm->optimization() == QPixmap::NormalOptim ) { // #### cache disabled + // Compete for the global cache + gc = cache_mask_gc( dpy, dst->handle(), + mask->data->ser_no, + mask->handle() ); + } else { + // Create a new mask GC. If BestOptim, we store the mask GC + // with the mask (not at the pixmap). This way, many pixmaps + // which have a common mask will be optimized at no extra cost. + gc = XCreateGC( dpy, dst->handle(), 0, 0 ); + XSetGraphicsExposures( dpy, gc, False ); + XSetClipMask( dpy, gc, mask->handle() ); + if ( src_pm->optimization() == QPixmap::BestOptim ) { + mask->data->maskgc = gc; + } else { + temp_gc = TRUE; + } + } + } + XSetClipOrigin( dpy, gc, dx-sx, dy-sy ); + if ( rop != Qt::CopyROP ) // use non-default ROP code + XSetFunction( dpy, gc, ropCodes[rop] ); + if ( include_inferiors ) { + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + XSetSubwindowMode( dpy, gc, ClipByChildren ); + } else { + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + } + + if ( temp_gc ) // delete temporary GC + XFreeGC( dpy, gc ); + else if ( rop != Qt::CopyROP ) // restore ROP + XSetFunction( dpy, gc, GXcopy ); + return; + } + + gc = qt_xget_temp_gc( dst->x11Screen(), mono_dst ); // get a reusable GC + + if ( rop != Qt::CopyROP ) // use non-default ROP code + XSetFunction( dpy, gc, ropCodes[rop] ); + + if ( mono_src && mono_dst && src == dst ) { // dst and src are the same bitmap + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, dx, dy ); + } else if ( mono_src ) { // src is bitmap + XGCValues gcvals; + ulong valmask = GCBackground | GCForeground | GCFillStyle | + GCStipple | GCTileStipXOrigin | GCTileStipYOrigin; + if ( td == QInternal::Widget ) { // set GC colors + QWidget *w = (QWidget *)dst; + gcvals.background = w->backgroundColor().pixel( dst->x11Screen() ); + gcvals.foreground = w->foregroundColor().pixel( dst->x11Screen() ); + if ( include_inferiors ) { + valmask |= GCSubwindowMode; + gcvals.subwindow_mode = IncludeInferiors; + } + } else if ( mono_dst ) { + gcvals.background = 0; + gcvals.foreground = 1; + } else { + gcvals.background = Qt::white.pixel( dst->x11Screen() ); + gcvals.foreground = Qt::black.pixel( dst->x11Screen() ); + } + + gcvals.fill_style = FillOpaqueStippled; + gcvals.stipple = src->handle(); + gcvals.ts_x_origin = dx - sx; + gcvals.ts_y_origin = dy - sy; + + bool clipmask = FALSE; + if ( mask ) { + if ( ((QPixmap*)src)->data->selfmask ) { + gcvals.fill_style = FillStippled; + } else { + XSetClipMask( dpy, gc, mask->handle() ); + XSetClipOrigin( dpy, gc, dx-sx, dy-sy ); + clipmask = TRUE; + } + } + + XChangeGC( dpy, gc, valmask, &gcvals ); + XFillRectangle( dpy,dst->handle(), gc, dx, dy, sw, sh ); + + valmask = GCFillStyle | GCTileStipXOrigin | GCTileStipYOrigin; + gcvals.fill_style = FillSolid; + gcvals.ts_x_origin = 0; + gcvals.ts_y_origin = 0; + if ( include_inferiors ) { + valmask |= GCSubwindowMode; + gcvals.subwindow_mode = ClipByChildren; + } + XChangeGC( dpy, gc, valmask, &gcvals ); + + if ( clipmask ) { + XSetClipOrigin( dpy, gc, 0, 0 ); + XSetClipMask( dpy, gc, None ); + } + + } else { // src is pixmap/widget + + if ( graphics_exposure ) // widget to widget + XSetGraphicsExposures( dpy, gc, True ); + if ( include_inferiors ) { + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + XSetSubwindowMode( dpy, gc, ClipByChildren ); + } else { + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + } + if ( graphics_exposure ) // reset graphics exposure + XSetGraphicsExposures( dpy, gc, False ); + } + + if ( rop != Qt::CopyROP ) // restore ROP + XSetFunction( dpy, gc, GXcopy ); +} + + +/*! + \relates QPaintDevice + + \overload void bitBlt( QPaintDevice *dst, const QPoint &dp, const QPaintDevice *src, const QRect &sr, RasterOp rop ) + + Overloaded bitBlt() with the destination point \a dp and source + rectangle \a sr. +*/ + + +/*! + \internal +*/ +// makes it possible to add a setResolution as we have in QPrinter for all +// paintdevices without breaking bin compatibility. +void QPaintDevice::setResolution( int ) +{ +} + +/*!\internal +*/ +int QPaintDevice::resolution() const +{ + return metric( QPaintDeviceMetrics::PdmDpiY ); +} diff --git a/src/kernel/qpaintdevicedefs.h b/src/kernel/qpaintdevicedefs.h new file mode 100644 index 0000000..63027dc --- /dev/null +++ b/src/kernel/qpaintdevicedefs.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Definition of QPaintDevice constants and flags +** +** Created : 940721 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPAINTDEVICEDEFS_H +#define QPAINTDEVICEDEFS_H + +#error "this file is gone. the #defines it contained are in" +#error "q1xcompatibility.h; the functionality is in QPaintDevice" +#error "and QPaintDeviceMetrics." + +#endif // QPAINTDEVICEDEFS_H diff --git a/src/kernel/qpaintdevicemetrics.cpp b/src/kernel/qpaintdevicemetrics.cpp new file mode 100644 index 0000000..daa3a9d --- /dev/null +++ b/src/kernel/qpaintdevicemetrics.cpp @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Implementation of QPaintDeviceMetrics class +** +** Created : 941109 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpaintdevicemetrics.h" + +/*! + \class QPaintDeviceMetrics qpaintdevicemetrics.h + \brief The QPaintDeviceMetrics class provides information about a + paint device. + + \ingroup graphics + \ingroup images + + Sometimes when drawing graphics it is necessary to obtain + information about the physical characteristics of a paint device. + This class provides the information. For example, to compute the + aspect ratio of a paint device: + + \code + QPaintDeviceMetrics pdm( myWidget ); + double aspect = (double)pdm.widthMM() / (double)pdm.heightMM(); + \endcode + + QPaintDeviceMetrics contains methods to provide the width and + height of a device in both pixels (width() and height()) and + millimeters (widthMM() and heightMM()), the number of colors the + device supports (numColors()), the number of bit planes (depth()), + and the resolution of the device (logicalDpiX() and + logicalDpiY()). + + It is not always possible for QPaintDeviceMetrics to compute the + values you ask for, particularly for external devices. The + ultimate example is asking for the resolution of of a QPrinter + that is set to "print to file": who knows what printer that file + will end up on? +*/ + +/*! + Constructs a metric for the paint device \a pd. +*/ +QPaintDeviceMetrics::QPaintDeviceMetrics( const QPaintDevice *pd ) +{ + pdev = (QPaintDevice *)pd; +} + + +/*! + \fn int QPaintDeviceMetrics::width() const + + Returns the width of the paint device in default coordinate system + units (e.g. pixels for QPixmap and QWidget). +*/ + +/*! + \fn int QPaintDeviceMetrics::height() const + + Returns the height of the paint device in default coordinate + system units (e.g. pixels for QPixmap and QWidget). +*/ + +/*! + \fn int QPaintDeviceMetrics::widthMM() const + + Returns the width of the paint device, measured in millimeters. +*/ + +/*! + \fn int QPaintDeviceMetrics::heightMM() const + + Returns the height of the paint device, measured in millimeters. +*/ + +/*! + \fn int QPaintDeviceMetrics::numColors() const + + Returns the number of different colors available for the paint + device. Since this value is an int will not be sufficient to represent + the number of colors on 32 bit displays, in which case INT_MAX is + returned instead. +*/ + +/*! + \fn int QPaintDeviceMetrics::depth() const + + Returns the bit depth (number of bit planes) of the paint device. +*/ + +/*! + \fn int QPaintDeviceMetrics::logicalDpiX() const + + Returns the horizontal resolution of the device in dots per inch, + which is used when computing font sizes. For X, this is usually + the same as could be computed from widthMM(), but it varies on + Windows. +*/ + +/*! + \fn int QPaintDeviceMetrics::logicalDpiY() const + + Returns the vertical resolution of the device in dots per inch, + which is used when computing font sizes. For X, this is usually + the same as could be computed from heightMM(), but it varies on + Windows. +*/ + +/*! + \fn int QPaintDeviceMetrics::physicalDpiX() const + \internal +*/ +/*! + \fn int QPaintDeviceMetrics::physicalDpiY() const + \internal +*/ + diff --git a/src/kernel/qpaintdevicemetrics.h b/src/kernel/qpaintdevicemetrics.h new file mode 100644 index 0000000..0acf89f --- /dev/null +++ b/src/kernel/qpaintdevicemetrics.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Definition of QPaintDeviceMetrics class +** +** Created : 941109 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPAINTDEVICEMETRICS_H +#define QPAINTDEVICEMETRICS_H + +#ifndef QT_H +#include "qpaintdevice.h" +#endif // QT_H + + +class Q_EXPORT QPaintDeviceMetrics // paint device metrics +{ +public: + QPaintDeviceMetrics( const QPaintDevice * ); + + enum { + PdmWidth = 1, + PdmHeight, + PdmWidthMM, + PdmHeightMM, + PdmNumColors, + PdmDepth, + PdmDpiX, + PdmDpiY, + PdmPhysicalDpiX, + PdmPhysicalDpiY + }; + + int width() const { return (int)pdev->metric(PdmWidth); } + int height() const { return (int)pdev->metric(PdmHeight); } + int widthMM() const { return (int)pdev->metric(PdmWidthMM); } + int heightMM() const { return (int)pdev->metric(PdmHeightMM); } + int logicalDpiX() const { return (int)pdev->metric(PdmDpiX); } + int logicalDpiY() const { return (int)pdev->metric(PdmDpiY); } + int physicalDpiX()const { return (int)pdev->metric(PdmPhysicalDpiX); } + int physicalDpiY()const { return (int)pdev->metric(PdmPhysicalDpiY); } + int numColors() const { return (int)pdev->metric(PdmNumColors); } + int depth() const { return (int)pdev->metric(PdmDepth); } + +private: + QPaintDevice *pdev; +}; + + +#endif // QPAINTDEVICEMETRICS_H diff --git a/src/kernel/qpainter.cpp b/src/kernel/qpainter.cpp new file mode 100644 index 0000000..e5e9026 --- /dev/null +++ b/src/kernel/qpainter.cpp @@ -0,0 +1,3966 @@ +/**************************************************************************** +** +** Implementation of QPainter, QPen and QBrush classes +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpainter.h" +#include "qpainter_p.h" +#include "qbitmap.h" +#include "qptrstack.h" +#include "qptrdict.h" +#include "qdatastream.h" +#include "qwidget.h" +#include "qimage.h" +#include "qpaintdevicemetrics.h" +#include "qapplication.h" +#include "qrichtext_p.h" +#include "qregexp.h" +#include "qcleanuphandler.h" +#ifdef Q_WS_QWS +#include "qgfx_qws.h" +#endif +#include <string.h> + +#include "qtextlayout_p.h" +#include "qfontengine_p.h" + +#ifndef QT_NO_TRANSFORMATIONS +typedef QPtrStack<QWMatrix> QWMatrixStack; +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +/*! + \class QPainter qpainter.h + \brief The QPainter class does low-level painting e.g. on widgets. + + \ingroup graphics + \ingroup images + \mainclass + + The painter provides highly optimized functions to do most of the + drawing GUI programs require. QPainter can draw everything from + simple lines to complex shapes like pies and chords. It can also + draw aligned text and pixmaps. Normally, it draws in a "natural" + coordinate system, but it can also do view and world + transformation. + + The typical use of a painter is: + + \list + \i Construct a painter. + \i Set a pen, a brush etc. + \i Draw. + \i Destroy the painter. + \endlist + + Mostly, all this is done inside a paint event. (In fact, 99% of + all QPainter use is in a reimplementation of + QWidget::paintEvent(), and the painter is heavily optimized for + such use.) Here's one very simple example: + + \code + void SimpleExampleWidget::paintEvent() + { + QPainter paint( this ); + paint.setPen( Qt::blue ); + paint.drawText( rect(), AlignCenter, "The Text" ); + } + \endcode + + Usage is simple, and there are many settings you can use: + + \list + + \i font() is the currently set font. If you set a font that isn't + available, Qt finds a close match. In fact font() returns what + you set using setFont() and fontInfo() returns the font actually + being used (which may be the same). + + \i brush() is the currently set brush; the color or pattern that's + used for filling e.g. circles. + + \i pen() is the currently set pen; the color or stipple that's + used for drawing lines or boundaries. + + \i backgroundMode() is \c Opaque or \c Transparent, i.e. whether + backgroundColor() is used or not. + + \i backgroundColor() only applies when backgroundMode() is Opaque + and pen() is a stipple. In that case, it describes the color of + the background pixels in the stipple. + + \i rasterOp() is how pixels drawn interact with the pixels already + there. + + \i brushOrigin() is the origin of the tiled brushes, normally the + origin of the window. + + \i viewport(), window(), worldMatrix() and many more make up the + painter's coordinate transformation system. See \link + coordsys.html The Coordinate System \endlink for an explanation of + this, or see below for a very brief overview of the functions. + + \i hasClipping() is whether the painter clips at all. (The paint + device clips, too.) If the painter clips, it clips to clipRegion(). + + \i pos() is the current position, set by moveTo() and used by + lineTo(). + + \endlist + + Note that some of these settings mirror settings in some paint + devices, e.g. QWidget::font(). QPainter::begin() (or the QPainter + constructor) copies these attributes from the paint device. + Calling, for example, QWidget::setFont() doesn't take effect until + the next time a painter begins painting on it. + + save() saves all of these settings on an internal stack, restore() + pops them back. + + The core functionality of QPainter is drawing, and there are + functions to draw most primitives: drawPoint(), drawPoints(), + drawLine(), drawRect(), drawWinFocusRect(), drawRoundRect(), + drawEllipse(), drawArc(), drawPie(), drawChord(), + drawLineSegments(), drawPolyline(), drawPolygon(), + drawConvexPolygon() and drawCubicBezier(). All of these functions + take integer coordinates; there are no floating-point versions + since we want drawing to be as fast as possible. + + There are functions to draw pixmaps/images, namely drawPixmap(), + drawImage() and drawTiledPixmap(). drawPixmap() and drawImage() + produce the same result, except that drawPixmap() is faster + on-screen and drawImage() faster and sometimes better on QPrinter + and QPicture. + + Text drawing is done using drawText(), and when you need + fine-grained positioning, boundingRect() tells you where a given + drawText() command would draw. + + There is a drawPicture() function that draws the contents of an + entire QPicture using this painter. drawPicture() is the only + function that disregards all the painter's settings: the QPicture + has its own settings. + + Normally, the QPainter operates on the device's own coordinate + system (usually pixels), but QPainter has good support for + coordinate transformation. See \link coordsys.html The Coordinate + System \endlink for a more general overview and a simple example. + + The most common functions used are scale(), rotate(), translate() + and shear(), all of which operate on the worldMatrix(). + setWorldMatrix() can replace or add to the currently set + worldMatrix(). + + setViewport() sets the rectangle on which QPainter operates. The + default is the entire device, which is usually fine, except on + printers. setWindow() sets the coordinate system, that is, the + rectangle that maps to viewport(). What's drawn inside the + window() ends up being inside the viewport(). The window's + default is the same as the viewport, and if you don't use the + transformations, they are optimized away, gaining another little + bit of speed. + + After all the coordinate transformation is done, QPainter can clip + the drawing to an arbitrary rectangle or region. hasClipping() is + TRUE if QPainter clips, and clipRegion() returns the clip region. + You can set it using either setClipRegion() or setClipRect(). + Note that the clipping can be slow. It's all system-dependent, + but as a rule of thumb, you can assume that drawing speed is + inversely proportional to the number of rectangles in the clip + region. + + After QPainter's clipping, the paint device may also clip. For + example, most widgets clip away the pixels used by child widgets, + and most printers clip away an area near the edges of the paper. + This additional clipping is not reflected by the return value of + clipRegion() or hasClipping(). + + QPainter also includes some less-used functions that are very + useful on those occasions when they're needed. + + isActive() indicates whether the painter is active. begin() (and + the most usual constructor) makes it active. end() (and the + destructor) deactivates it. If the painter is active, device() + returns the paint device on which the painter paints. + + Sometimes it is desirable to make someone else paint on an unusual + QPaintDevice. QPainter supports a static function to do this, + redirect(). We recommend not using it, but for some hacks it's + perfect. + + setTabStops() and setTabArray() can change where the tab stops + are, but these are very seldomly used. + + \warning Note that QPainter does not attempt to work around + coordinate limitations in the underlying window system. Some + platforms may behave incorrectly with coordinates as small as + +/-4000. + + \headerfile qdrawutil.h + + \sa QPaintDevice QWidget QPixmap QPrinter QPicture + \link simple-application.html Application Walkthrough \endlink + \link coordsys.html Coordinate System Overview \endlink +*/ + +/*! + \fn QGfx * QPainter::internalGfx() + + \internal +*/ + +/*! + \enum QPainter::CoordinateMode + \value CoordDevice + \value CoordPainter + + \sa clipRegion() +*/ +/*! + \enum QPainter::TextDirection + \value Auto + \value RTL right to left + \value LTR left to right + + \sa drawText() +*/ + +/*! + \enum Qt::PaintUnit + \value PixelUnit + \value LoMetricUnit \e obsolete + \value HiMetricUnit \e obsolete + \value LoEnglishUnit \e obsolete + \value HiEnglishUnit \e obsolete + \value TwipsUnit \e obsolete +*/ + +/*! + \enum Qt::BrushStyle + + \value NoBrush + \value SolidPattern + \value Dense1Pattern + \value Dense2Pattern + \value Dense3Pattern + \value Dense4Pattern + \value Dense5Pattern + \value Dense6Pattern + \value Dense7Pattern + \value HorPattern + \value VerPattern + \value CrossPattern + \value BDiagPattern + \value FDiagPattern + \value DiagCrossPattern + \value CustomPattern + + \img brush-styles.png Brush Styles + +*/ + +/*! + \enum Qt::RasterOp + + This enum type is used to describe the way things are written to + the paint device. Each bit of the \e src (what you write) + interacts with the corresponding bit of the \e dst pixel. + + \value CopyROP dst = src + \value OrROP dst = src OR dst + \value XorROP dst = src XOR dst + \value NotAndROP dst = (NOT src) AND dst + \value EraseROP an alias for \c NotAndROP + \value NotCopyROP dst = NOT src + \value NotOrROP dst = (NOT src) OR dst + \value NotXorROP dst = (NOT src) XOR dst + \value AndROP dst = src AND dst + \value NotEraseROP an alias for \c AndROP + \value NotROP dst = NOT dst + \value ClearROP dst = 0 + \value SetROP dst = 1 + \value NopROP dst = dst + \value AndNotROP dst = src AND (NOT dst) + \value OrNotROP dst = src OR (NOT dst) + \value NandROP dst = NOT (src AND dst) + \value NorROP dst = NOT (src OR dst) + + By far the most useful ones are \c CopyROP and \c XorROP. + + On Qt/Embedded, only \c CopyROP, \c XorROP, and \c NotROP are supported. +*/ + +/*! + \enum Qt::AlignmentFlags + + This enum type is used to describe alignment. It contains + horizontal and vertical flags. + + The horizontal flags are: + + \value AlignAuto Aligns according to the language. Left for most, + right for Arabic and Hebrew. + \value AlignLeft Aligns with the left edge. + \value AlignRight Aligns with the right edge. + \value AlignHCenter Centers horizontally in the available space. + \value AlignJustify Justifies the text in the available space. + Does not work for everything and may be interpreted as + AlignAuto in some cases. + + The vertical flags are: + + \value AlignTop Aligns with the top. + \value AlignBottom Aligns with the bottom. + \value AlignVCenter Centers vertically in the available space. + + You can use only one of the horizontal flags at a time. There is + one two-dimensional flag: + + \value AlignCenter Centers in both dimensions. + + You can use at most one horizontal and one vertical flag at a time. \c + AlignCenter counts as both horizontal and vertical. + + Masks: + + \value AlignHorizontal_Mask + \value AlignVertical_Mask + + Conflicting combinations of flags have undefined meanings. +*/ + +/*! + \enum Qt::TextFlags + + This enum type is used to define some modifier flags. Some of + these flags only make sense in the context of printing: + + \value SingleLine Treats all whitespace as spaces and prints just + one line. + \value DontClip If it's impossible to stay within the given bounds, + it prints outside. + \value ExpandTabs Makes the U+0009 (ASCII tab) character move to + the next tab stop. + \value ShowPrefix Displays the string "\&P" as <u>P</u> + (see QButton for an example). For an ampersand, use "\&\&". + \value WordBreak Breaks lines at appropriate points, e.g. at word + boundaries. + \value BreakAnywhere Breaks lines anywhere, even within words. + \value NoAccel Same as ShowPrefix but doesn't draw the underlines. + + You can use as many modifier flags as you want, except that \c + SingleLine and \c WordBreak cannot be combined. + + Flags that are inappropriate for a given use (e.g. ShowPrefix to + QGridLayout::addWidget()) are generally ignored. + +*/ + +/*! + \enum Qt::PenStyle + + This enum type defines the pen styles that can be drawn using + QPainter. The styles are + + \value NoPen no line at all. For example, QPainter::drawRect() + fills but does not draw any boundary line. + + \value SolidLine a simple line. + + \value DashLine dashes separated by a few pixels. + + \value DotLine dots separated by a few pixels. + + \value DashDotLine alternate dots and dashes. + + \value DashDotDotLine one dash, two dots, one dash, two dots. + + \value MPenStyle mask of the pen styles. + + \img pen-styles.png Pen Styles +*/ + +/*! + \enum Qt::PenCapStyle + + This enum type defines the pen cap styles supported by Qt, i.e. + the line end caps that can be drawn using QPainter. + + \value FlatCap a square line end that does not cover the end + point of the line. + \value SquareCap a square line end that covers the end point and + extends beyond it with half the line width. + \value RoundCap a rounded line end. + \value MPenCapStyle mask of the pen cap styles. + + \img pen-cap-styles.png Pen Cap Styles +*/ + +/*! + \enum Qt::PenJoinStyle + + This enum type defines the pen join styles supported by Qt, i.e. + which joins between two connected lines can be drawn using + QPainter. + + \value MiterJoin The outer edges of the lines are extended to + meet at an angle, and this area is filled. + \value BevelJoin The triangular notch between the two lines is filled. + \value RoundJoin A circular arc between the two lines is filled. + \value MPenJoinStyle mask of the pen join styles. + + \img pen-join-styles.png Pen Join Styles +*/ + +/*! + \enum Qt::BGMode + + Background mode + + \value TransparentMode + \value OpaqueMode +*/ + +/*! + Constructs a painter. + + Notice that all painter settings (setPen, setBrush etc.) are reset + to default values when begin() is called. + + \sa begin(), end() +*/ + +QPainter::QPainter() +{ + init(); +} + + +/*! + Constructs a painter that begins painting the paint device \a pd + immediately. Depending on the underlying graphic system the + painter will paint over children of the paintdevice if \a + unclipped is TRUE. + + This constructor is convenient for short-lived painters, e.g. in a + \link QWidget::paintEvent() paint event\endlink and should be used + only once. The constructor calls begin() for you and the QPainter + destructor automatically calls end(). + + Here's an example using begin() and end(): + \code + void MyWidget::paintEvent( QPaintEvent * ) + { + QPainter p; + p.begin( this ); + p.drawLine( ... ); // drawing code + p.end(); + } + \endcode + + The same example using this constructor: + \code + void MyWidget::paintEvent( QPaintEvent * ) + { + QPainter p( this ); + p.drawLine( ... ); // drawing code + } + \endcode + + Since the constructor cannot provide feedback when the initialization + of the painter failed you should rather use begin() and end() to paint + on external devices, e.g. printers. + + \sa begin(), end() +*/ + +QPainter::QPainter( const QPaintDevice *pd, bool unclipped ) +{ + init(); + if ( begin( pd, unclipped ) ) + flags |= CtorBegin; +} + + +/*! + Constructs a painter that begins painting the paint device \a pd + immediately, with the default arguments taken from \a + copyAttributes. The painter will paint over children of the paint + device if \a unclipped is TRUE (although this is not supported on + all platforms). + + \sa begin() +*/ + +QPainter::QPainter( const QPaintDevice *pd, + const QWidget *copyAttributes, bool unclipped ) +{ + init(); + if ( begin( pd, copyAttributes, unclipped ) ) + flags |= CtorBegin; +} + + +/*! + Destroys the painter. +*/ + +QPainter::~QPainter() +{ + if ( isActive() ) + end(); + else + killPStack(); + if ( tabarray ) // delete tab array + delete [] tabarray; +#ifndef QT_NO_TRANSFORMATIONS + if ( wm_stack ) + delete (QWMatrixStack *)wm_stack; +#endif + destroy(); +} + + +/*! + \overload bool QPainter::begin( const QPaintDevice *pd, const QWidget *copyAttributes, bool unclipped ) + + This version opens the painter on a paint device \a pd and sets + the initial pen, background color and font from \a copyAttributes, + painting over the paint device's children when \a unclipped is + TRUE. This is equivalent to: + + \code + QPainter p; + p.begin( pd ); + p.setPen( copyAttributes->foregroundColor() ); + p.setBackgroundColor( copyAttributes->backgroundColor() ); + p.setFont( copyAttributes->font() ); + \endcode + + This begin function is convenient for double buffering. When you + draw in a pixmap instead of directly in a widget (to later bitBlt + the pixmap into the widget) you will need to set the widget's + font etc. This function does exactly that. + + Example: + \code + void MyWidget::paintEvent( QPaintEvent * ) + { + QPixmap pm(size()); + QPainter p; + p.begin(&pm, this); + // ... potentially flickering paint operation ... + p.end(); + bitBlt(this, 0, 0, &pm); + } + \endcode + + \sa end() +*/ + +bool QPainter::begin( const QPaintDevice *pd, const QWidget *copyAttributes, bool unclipped ) +{ + if ( copyAttributes == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "QPainter::begin: The widget to copy attributes from cannot " + "be null" ); +#endif + return FALSE; + } + if ( begin( pd, unclipped ) ) { + setPen( copyAttributes->foregroundColor() ); + setBackgroundColor( copyAttributes->backgroundColor() ); + setFont( copyAttributes->font() ); + return TRUE; + } + return FALSE; +} + + +/*! + \internal + Sets or clears a pointer flag. +*/ + +void QPainter::setf( uint b, bool v ) +{ + if ( v ) + setf( b ); + else + clearf( b ); +} + + +/*! + \fn bool QPainter::isActive() const + + Returns TRUE if the painter is active painting, i.e. begin() has + been called and end() has not yet been called; otherwise returns + FALSE. + + \sa QPaintDevice::paintingActive() +*/ + +/*! + \fn QPaintDevice *QPainter::device() const + + Returns the paint device on which this painter is currently + painting, or 0 if the painter is not active. + + \sa QPaintDevice::paintingActive() +*/ + + +struct QPState { // painter state + QFont font; + QPen pen; + QPoint curPt; + QBrush brush; + QColor bgc; + uchar bgm; + uchar rop; + QPoint bro; + QRect wr, vr; +#ifndef QT_NO_TRANSFORMATIONS + QWMatrix wm; +#else + int xlatex; + int xlatey; +#endif + bool vxf; + bool wxf; + QRegion rgn; + bool clip; + int ts; + int *ta; + void* wm_stack; +}; + +//TODO lose the worldmatrix stack + +typedef QPtrStack<QPState> QPStateStack; + + +void QPainter::killPStack() +{ +#if defined(QT_CHECK_STATE) + if ( ps_stack && !((QPStateStack *)ps_stack)->isEmpty() ) + qWarning( "QPainter::killPStack: non-empty save/restore stack when " + "end() was called" ); +#endif + delete (QPStateStack *)ps_stack; + ps_stack = 0; +} + +/*! + Saves the current painter state (pushes the state onto a stack). A + save() must be followed by a corresponding restore(). end() + unwinds the stack. + + \sa restore() +*/ + +void QPainter::save() +{ + if ( testf(ExtDev) ) { + if ( testf(DirtyFont) ) + updateFont(); + if ( testf(DirtyPen) ) + updatePen(); + if ( testf(DirtyBrush) ) + updateBrush(); + pdev->cmd( QPaintDevice::PdcSave, this, 0 ); + } + QPStateStack *pss = (QPStateStack *)ps_stack; + if ( pss == 0 ) { + pss = new QPtrStack<QPState>; + Q_CHECK_PTR( pss ); + pss->setAutoDelete( TRUE ); + ps_stack = pss; + } + QPState *ps = new QPState; + Q_CHECK_PTR( ps ); + ps->font = cfont; + ps->pen = cpen; + ps->curPt = pos(); + ps->brush = cbrush; + ps->bgc = bg_col; + ps->bgm = bg_mode; + ps->rop = rop; + ps->bro = bro; +#ifndef QT_NO_TRANSFORMATIONS + ps->wr = QRect( wx, wy, ww, wh ); + ps->vr = QRect( vx, vy, vw, vh ); + ps->wm = wxmat; + ps->vxf = testf(VxF); + ps->wxf = testf(WxF); +#else + ps->xlatex = xlatex; + ps->xlatey = xlatey; +#endif + ps->rgn = crgn; + ps->clip = testf(ClipOn); + ps->ts = tabstops; + ps->ta = tabarray; + ps->wm_stack = wm_stack; + wm_stack = 0; + pss->push( ps ); +} + +/*! + Restores the current painter state (pops a saved state off the + stack). + + \sa save() +*/ + +void QPainter::restore() +{ + if ( testf(ExtDev) ) { + pdev->cmd( QPaintDevice::PdcRestore, this, 0 ); + if ( pdev->devType() == QInternal::Picture ) + block_ext = TRUE; + } + QPStateStack *pss = (QPStateStack *)ps_stack; + if ( pss == 0 || pss->isEmpty() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::restore: Empty stack error" ); +#endif + return; + } + QPState *ps = pss->pop(); + bool hardRestore = testf(VolatileDC); + + if ( ps->font != cfont || hardRestore ) + setFont( ps->font ); + if ( ps->pen != cpen || hardRestore ) + setPen( ps->pen ); + if ( ps->brush != cbrush || hardRestore ) + setBrush( ps->brush ); + if ( ps->bgc != bg_col || hardRestore ) + setBackgroundColor( ps->bgc ); + if ( ps->bgm != bg_mode || hardRestore ) + setBackgroundMode( (BGMode)ps->bgm ); + if ( ps->rop != rop || hardRestore ) + setRasterOp( (RasterOp)ps->rop ); + if ( ps->bro != bro || hardRestore ) + setBrushOrigin( ps->bro ); +#ifndef QT_NO_TRANSFORMATIONS + QRect wr( wx, wy, ww, wh ); + QRect vr( vx, vy, vw, vh ); + if ( ps->wr != wr || hardRestore ) + setWindow( ps->wr ); + if ( ps->vr != vr || hardRestore ) + setViewport( ps->vr ); + if ( ps->wm != wxmat || hardRestore ) + setWorldMatrix( ps->wm ); + if ( ps->vxf != testf(VxF) || hardRestore ) + setViewXForm( ps->vxf ); + if ( ps->wxf != testf(WxF) || hardRestore ) + setWorldXForm( ps->wxf ); +#else + xlatex = ps->xlatex; + xlatey = ps->xlatey; + setf( VxF, xlatex || xlatey ); +#endif + if ( ps->curPt != pos() || hardRestore ) + moveTo( ps->curPt ); + if ( ps->rgn != crgn || hardRestore ) + setClipRegion( ps->rgn ); + if ( ps->clip != testf(ClipOn) || hardRestore ) + setClipping( ps->clip ); + tabstops = ps->ts; + tabarray = ps->ta; + +#ifndef QT_NO_TRANSFORMATIONS + if ( wm_stack ) + delete (QWMatrixStack *)wm_stack; + wm_stack = ps->wm_stack; +#endif + delete ps; + block_ext = FALSE; +} + +typedef QPtrDict<QPaintDevice> QPaintDeviceDict; +static QPaintDeviceDict *pdev_dict = 0; + +/*! + Redirects all paint commands for a paint device, \a pdev, to + another paint device, \a replacement, unless \a replacement is 0. + If \a replacement is 0, the redirection for \a pdev is removed. + + In general, you'll probably find calling QPixmap::grabWidget() or + QPixmap::grabWindow() is an easier solution. +*/ + +void QPainter::redirect( QPaintDevice *pdev, QPaintDevice *replacement ) +{ + if ( pdev_dict == 0 ) { + if ( replacement == 0 ) + return; + pdev_dict = new QPaintDeviceDict; + Q_CHECK_PTR( pdev_dict ); + } +#if defined(QT_CHECK_NULL) + if ( pdev == 0 ) + qWarning( "QPainter::redirect: The pdev argument cannot be 0" ); +#endif + if ( replacement ) { + pdev_dict->insert( pdev, replacement ); + } else { + pdev_dict->remove( pdev ); + if ( pdev_dict->count() == 0 ) { + delete pdev_dict; + pdev_dict = 0; + } + } +} + +/*! + \internal + Returns the replacement for \a pdev, or 0 if there is no replacement. +*/ +QPaintDevice *QPainter::redirect( QPaintDevice *pdev ) +{ + return pdev_dict ? pdev_dict->find( pdev ) : 0; +} + +/*! + Returns the font metrics for the painter, if the painter is + active. It is not possible to obtain metrics for an inactive + painter, so the return value is undefined if the painter is not + active. + + \sa fontInfo(), isActive() +*/ + +QFontMetrics QPainter::fontMetrics() const +{ + if ( pdev && pdev->devType() == QInternal::Picture ) + return QFontMetrics( cfont ); + + return QFontMetrics(this); +} + +/*! + Returns the font info for the painter, if the painter is active. + It is not possible to obtain font information for an inactive + painter, so the return value is undefined if the painter is not + active. + + \sa fontMetrics(), isActive() +*/ + +QFontInfo QPainter::fontInfo() const +{ + if ( pdev && pdev->devType() == QInternal::Picture ) + return QFontInfo( cfont ); + + return QFontInfo(this); +} + + +/*! + \fn const QPen &QPainter::pen() const + + Returns the painter's current pen. + + \sa setPen() +*/ + +/*! + Sets a new painter pen. + + The \a pen defines how to draw lines and outlines, and it also + defines the text color. + + \sa pen() +*/ + +void QPainter::setPen( const QPen &pen ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setPen: Will be reset by begin()" ); +#endif + if ( cpen == pen ) + return; + cpen = pen; + updatePen(); +} + +/*! + \overload + + Sets the painter's pen to have style \a style, width 0 and black + color. + + \sa pen(), QPen +*/ + +void QPainter::setPen( PenStyle style ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setPen: Will be reset by begin()" ); +#endif + QPen::QPenData *d = cpen.data; // low level access + if ( d->style == style && d->linest == style && !d->width && d->color == Qt::black ) + return; + if ( d->count != 1 ) { + cpen.detach(); + d = cpen.data; + } + d->style = style; + d->width = 0; + d->color = Qt::black; + d->linest = style; + updatePen(); +} + +/*! + \overload + + Sets the painter's pen to have style \c SolidLine, width 0 and the + specified \a color. + + \sa pen(), QPen +*/ + +void QPainter::setPen( const QColor &color ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setPen: Will be reset by begin()" ); +#endif + QPen::QPenData *d = cpen.data; // low level access + if ( d->color == color && !d->width && d->style == SolidLine && d->linest == SolidLine ) + return; + if ( d->count != 1 ) { + cpen.detach(); + d = cpen.data; + } + d->style = SolidLine; + d->width = 0; + d->color = color; + d->linest = SolidLine; + updatePen(); +} + +/*! + \fn const QBrush &QPainter::brush() const + + Returns the painter's current brush. + + \sa QPainter::setBrush() +*/ + +/*! + \overload + + Sets the painter's brush to \a brush. + + The \a brush defines how shapes are filled. + + \sa brush() +*/ + +void QPainter::setBrush( const QBrush &brush ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setBrush: Will be reset by begin()" ); +#endif + if ( cbrush == brush ) + return; + cbrush = brush; + updateBrush(); +} + +/*! + Sets the painter's brush to black color and the specified \a + style. + + \sa brush(), QBrush +*/ + +void QPainter::setBrush( BrushStyle style ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setBrush: Will be reset by begin()" ); +#endif + QBrush::QBrushData *d = cbrush.data; // low level access + if ( d->style == style && d->color == Qt::black && !d->pixmap ) + return; + if ( d->count != 1 ) { + cbrush.detach(); + d = cbrush.data; + } + d->style = style; + d->color = Qt::black; + if ( d->pixmap ) { + delete d->pixmap; + d->pixmap = 0; + } + updateBrush(); +} + +/*! + \overload + + Sets the painter's brush to have style \c SolidPattern and the + specified \a color. + + \sa brush(), QBrush +*/ + +void QPainter::setBrush( const QColor &color ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setBrush: Will be reset by begin()" ); +#endif + QBrush::QBrushData *d = cbrush.data; // low level access + if ( d->color == color && d->style == SolidPattern && !d->pixmap ) + return; + if ( d->count != 1 ) { + cbrush.detach(); + d = cbrush.data; + } + d->style = SolidPattern; + d->color = color; + if ( d->pixmap ) { + delete d->pixmap; + d->pixmap = 0; + } + updateBrush(); +} + + +/*! + \fn const QColor &QPainter::backgroundColor() const + + Returns the current background color. + + \sa setBackgroundColor() QColor +*/ + +/*! + \fn BGMode QPainter::backgroundMode() const + + Returns the current background mode. + + \sa setBackgroundMode() BGMode +*/ + +/*! + \fn RasterOp QPainter::rasterOp() const + + Returns the current \link Qt::RasterOp raster operation \endlink. + + \sa setRasterOp() RasterOp +*/ + +/*! + \fn const QPoint &QPainter::brushOrigin() const + + Returns the brush origin currently set. + + \sa setBrushOrigin() +*/ + + +/*! + \fn int QPainter::tabStops() const + + Returns the tab stop setting. + + \sa setTabStops() +*/ + +/*! + Set the tab stop width to \a ts, i.e. locates tab stops at \a ts, + 2*\a ts, 3*\a ts and so on. + + Tab stops are used when drawing formatted text with \c ExpandTabs + set. This fixed tab stop value is used only if no tab array is set + (which is the default case). + + A value of 0 (the default) implies a tabstop setting of 8 times the width of the + character 'x' in the font currently set on the painter. + + \sa tabStops(), setTabArray(), drawText(), fontMetrics() +*/ + +void QPainter::setTabStops( int ts ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setTabStops: Will be reset by begin()" ); +#endif + tabstops = ts; + if ( isActive() && testf(ExtDev) ) { // tell extended device + QPDevCmdParam param[1]; + param[0].ival = ts; + pdev->cmd( QPaintDevice::PdcSetTabStops, this, param ); + } +} + +/*! + \fn int *QPainter::tabArray() const + + Returns the currently set tab stop array. + + \sa setTabArray() +*/ + +/*! + Sets the tab stop array to \a ta. This puts tab stops at \a ta[0], + \a ta[1] and so on. The array is null-terminated. + + If both a tab array and a tab top size is set, the tab array wins. + + \sa tabArray(), setTabStops(), drawText(), fontMetrics() +*/ + +void QPainter::setTabArray( int *ta ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setTabArray: Will be reset by begin()" ); +#endif + if ( ta != tabarray ) { + tabarraylen = 0; + if ( tabarray ) // Avoid purify complaint + delete [] tabarray; // delete old array + if ( ta ) { // tabarray = copy of 'ta' + while ( ta[tabarraylen] ) + tabarraylen++; + tabarraylen++; // and 0 terminator + tabarray = new int[tabarraylen]; // duplicate ta + memcpy( tabarray, ta, sizeof(int)*tabarraylen ); + } else { + tabarray = 0; + } + } + if ( isActive() && testf(ExtDev) ) { // tell extended device + QPDevCmdParam param[2]; + param[0].ival = tabarraylen; + param[1].ivec = tabarray; + pdev->cmd( QPaintDevice::PdcSetTabArray, this, param ); + } +} + + +/*! + \fn HANDLE QPainter::handle() const + + Returns the platform-dependent handle used for drawing. Using this + function is not portable. +*/ + + +/***************************************************************************** + QPainter xform settings + *****************************************************************************/ + +#ifndef QT_NO_TRANSFORMATIONS + +/*! + Enables view transformations if \a enable is TRUE, or disables + view transformations if \a enable is FALSE. + + \sa hasViewXForm(), setWindow(), setViewport(), setWorldMatrix(), + setWorldXForm(), xForm() +*/ + +void QPainter::setViewXForm( bool enable ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setViewXForm: Will be reset by begin()" ); +#endif + if ( !isActive() || enable == testf(VxF) ) + return; + setf( VxF, enable ); + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].ival = enable; + pdev->cmd( QPaintDevice::PdcSetVXform, this, param ); + } + updateXForm(); +} + +/*! + \fn bool QPainter::hasViewXForm() const + + Returns TRUE if view transformation is enabled; otherwise returns + FALSE. + + \sa setViewXForm(), xForm() +*/ + +/*! + Returns the window rectangle. + + \sa setWindow(), setViewXForm() +*/ + +QRect QPainter::window() const +{ + return QRect( wx, wy, ww, wh ); +} + +/*! + Sets the window rectangle view transformation for the painter and + enables view transformation. + + The window rectangle is part of the view transformation. The + window specifies the logical coordinate system and is specified by + the \a x, \a y, \a w width and \a h height parameters. Its sister, + the viewport(), specifies the device coordinate system. + + The default window rectangle is the same as the device's + rectangle. See the \link coordsys.html Coordinate System Overview + \endlink for an overview of coordinate transformation. + + \sa window(), setViewport(), setViewXForm(), setWorldMatrix(), + setWorldXForm() +*/ + +void QPainter::setWindow( int x, int y, int w, int h ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setWindow: Will be reset by begin()" ); +#endif + wx = x; + wy = y; + ww = w; + wh = h; + if ( testf(ExtDev) ) { + QRect r( x, y, w, h ); + QPDevCmdParam param[1]; + param[0].rect = (QRect*)&r; + pdev->cmd( QPaintDevice::PdcSetWindow, this, param ); + } + if ( testf(VxF) ) + updateXForm(); + else + setViewXForm( TRUE ); +} + +/*! + Returns the viewport rectangle. + + \sa setViewport(), setViewXForm() +*/ + +QRect QPainter::viewport() const // get viewport +{ + return QRect( vx, vy, vw, vh ); +} + +/*! + Sets the viewport rectangle view transformation for the painter + and enables view transformation. + + The viewport rectangle is part of the view transformation. The + viewport specifies the device coordinate system and is specified + by the \a x, \a y, \a w width and \a h height parameters. Its + sister, the window(), specifies the logical coordinate system. + + The default viewport rectangle is the same as the device's + rectangle. See the \link coordsys.html Coordinate System Overview + \endlink for an overview of coordinate transformation. + + \sa viewport(), setWindow(), setViewXForm(), setWorldMatrix(), + setWorldXForm(), xForm() +*/ + +void QPainter::setViewport( int x, int y, int w, int h ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setViewport: Will be reset by begin()" ); +#endif + vx = x; + vy = y; + vw = w; + vh = h; + if ( testf(ExtDev) ) { + QRect r( x, y, w, h ); + QPDevCmdParam param[1]; + param[0].rect = (QRect*)&r; + pdev->cmd( QPaintDevice::PdcSetViewport, this, param ); + } + if ( testf(VxF) ) + updateXForm(); + else + setViewXForm( TRUE ); +} + + +/*! + Enables world transformations if \a enable is TRUE, or disables + world transformations if \a enable is FALSE. The world + transformation matrix is not changed. + + \sa setWorldMatrix(), setWindow(), setViewport(), setViewXForm(), + xForm() +*/ + +void QPainter::setWorldXForm( bool enable ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setWorldXForm: Will be reset by begin()" ); +#endif + if ( !isActive() || enable == testf(WxF) ) + return; + setf( WxF, enable ); + if ( testf(ExtDev) && !block_ext ) { + QPDevCmdParam param[1]; + param[0].ival = enable; + pdev->cmd( QPaintDevice::PdcSetWXform, this, param ); + } + updateXForm(); +} + +/*! + \fn bool QPainter::hasWorldXForm() const + + Returns TRUE if world transformation is enabled; otherwise returns + FALSE. + + \sa setWorldXForm() +*/ + +/*! + Returns the world transformation matrix. + + \sa setWorldMatrix() +*/ + +const QWMatrix &QPainter::worldMatrix() const +{ + return wxmat; +} + +/*! + Sets the world transformation matrix to \a m and enables world + transformation. + + If \a combine is TRUE, then \a m is combined with the current + transformation matrix, otherwise \a m replaces the current + transformation matrix. + + If \a m is the identity matrix and \a combine is FALSE, this + function calls setWorldXForm(FALSE). (The identity matrix is the + matrix where QWMatrix::m11() and QWMatrix::m22() are 1.0 and the + rest are 0.0.) + + World transformations are applied after the view transformations + (i.e. \link setWindow() window\endlink and \link setViewport() + viewport\endlink). + + The following functions can transform the coordinate system without using + a QWMatrix: + \list + \i translate() + \i scale() + \i shear() + \i rotate() + \endlist + + They operate on the painter's worldMatrix() and are implemented like this: + + \code + void QPainter::rotate( double a ) + { + QWMatrix m; + m.rotate( a ); + setWorldMatrix( m, TRUE ); + } + \endcode + + Note that you should always use \a combine when you are drawing + into a QPicture. Otherwise it may not be possible to replay the + picture with additional transformations. Using translate(), + scale(), etc., is safe. + + For a brief overview of coordinate transformation, see the \link + coordsys.html Coordinate System Overview. \endlink + + \sa worldMatrix() setWorldXForm() setWindow() setViewport() + setViewXForm() xForm() QWMatrix +*/ + +void QPainter::setWorldMatrix( const QWMatrix &m, bool combine ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::setWorldMatrix: Will be reset by begin()" ); +#endif + return; + } + if ( combine ) + wxmat = m * wxmat; // combines + else + wxmat = m; // set new matrix + bool identity = wxmat.m11() == 1.0F && wxmat.m22() == 1.0F && + wxmat.m12() == 0.0F && wxmat.m21() == 0.0F && + wxmat.dx() == 0.0F && wxmat.dy() == 0.0F; + if ( testf(ExtDev) && !block_ext ) { + QPDevCmdParam param[2]; + param[0].matrix = &m; + param[1].ival = combine; + pdev->cmd( QPaintDevice::PdcSetWMatrix, this, param ); + } + if ( identity && pdev->devType() != QInternal::Picture ) + setWorldXForm( FALSE ); + else if ( !testf(WxF) ) + setWorldXForm( TRUE ); + else + updateXForm(); +} + +/*! \obsolete + + We recommend using save() instead. +*/ + +void QPainter::saveWorldMatrix() +{ + QWMatrixStack *stack = (QWMatrixStack *)wm_stack; + if ( stack == 0 ) { + stack = new QPtrStack<QWMatrix>; + Q_CHECK_PTR( stack ); + stack->setAutoDelete( TRUE ); + wm_stack = stack; + } + + stack->push( new QWMatrix( wxmat ) ); + +} + +/*! \obsolete + We recommend using restore() instead. +*/ + +void QPainter::restoreWorldMatrix() +{ + QWMatrixStack *stack = (QWMatrixStack *)wm_stack; + if ( stack == 0 || stack->isEmpty() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::restoreWorldMatrix: Empty stack error" ); +#endif + return; + } + QWMatrix* m = stack->pop(); + setWorldMatrix( *m ); + delete m; +} + +#endif // QT_NO_TRANSFORMATIONS + +/*! + Translates the coordinate system by \a (dx, dy). After this call, + \a (dx, dy) is added to points. + + For example, the following code draws the same point twice: + \code + void MyWidget::paintEvent() + { + QPainter paint( this ); + + paint.drawPoint( 0, 0 ); + + paint.translate( 100.0, 40.0 ); + paint.drawPoint( -100, -40 ); + } + \endcode + + \sa scale(), shear(), rotate(), resetXForm(), setWorldMatrix(), xForm() +*/ + +void QPainter::translate( double dx, double dy ) +{ +#ifndef QT_NO_TRANSFORMATIONS + QWMatrix m; + m.translate( dx, dy ); + setWorldMatrix( m, TRUE ); +#else + xlatex += (int)dx; + xlatey += (int)dy; + setf( VxF, xlatex || xlatey ); +#endif +} + + +#ifndef QT_NO_TRANSFORMATIONS +/*! + Scales the coordinate system by \a (sx, sy). + + \sa translate(), shear(), rotate(), resetXForm(), setWorldMatrix(), + xForm() +*/ + +void QPainter::scale( double sx, double sy ) +{ + QWMatrix m; + m.scale( sx, sy ); + setWorldMatrix( m, TRUE ); +} + +/*! + Shears the coordinate system by \a (sh, sv). + + \sa translate(), scale(), rotate(), resetXForm(), setWorldMatrix(), + xForm() +*/ + +void QPainter::shear( double sh, double sv ) +{ + QWMatrix m; + m.shear( sv, sh ); + setWorldMatrix( m, TRUE ); +} + +/*! + Rotates the coordinate system \a a degrees counterclockwise. + + \sa translate(), scale(), shear(), resetXForm(), setWorldMatrix(), + xForm() +*/ + +void QPainter::rotate( double a ) +{ + QWMatrix m; + m.rotate( a ); + setWorldMatrix( m, TRUE ); +} + + +/*! + Resets any transformations that were made using translate(), scale(), + shear(), rotate(), setWorldMatrix(), setViewport() and + setWindow(). + + \sa worldMatrix(), viewport(), window() +*/ + +void QPainter::resetXForm() +{ + if ( !isActive() ) + return; + wx = wy = vx = vy = 0; // default view origins + ww = vw = pdev->metric( QPaintDeviceMetrics::PdmWidth ); + wh = vh = pdev->metric( QPaintDeviceMetrics::PdmHeight ); + wxmat = QWMatrix(); + setWorldXForm( FALSE ); + setViewXForm( FALSE ); +} + +/*! + \internal + Updates an internal integer transformation matrix. +*/ + +void QPainter::updateXForm() +{ + QWMatrix m; + if ( testf(VxF) ) { + double scaleW = (double)vw/(double)ww; + double scaleH = (double)vh/(double)wh; + m.setMatrix( scaleW, 0, 0, scaleH, vx - wx*scaleW, vy - wy*scaleH ); + } + if ( testf(WxF) ) { + if ( testf(VxF) ) + m = wxmat * m; + else + m = wxmat; + } + xmat = m; + + txinv = FALSE; // no inverted matrix + txop = TxNone; + if ( m12()==0.0 && m21()==0.0 && m11() >= 0.0 && m22() >= 0.0 ) { + if ( m11()==1.0 && m22()==1.0 ) { + if ( dx()!=0.0 || dy()!=0.0 ) + txop = TxTranslate; + } else { + txop = TxScale; +#if defined(Q_WS_WIN) + setf(DirtyFont); +#endif + } + } else { + txop = TxRotShear; +#if defined(Q_WS_WIN) + setf(DirtyFont); +#endif + } +} + + +/*! + \internal + Updates an internal integer inverse transformation matrix. +*/ + +void QPainter::updateInvXForm() +{ +#if defined(QT_CHECK_STATE) + Q_ASSERT( txinv == FALSE ); +#endif + txinv = TRUE; // creating inverted matrix + bool invertible; + QWMatrix m; + if ( testf(VxF) ) { + m.translate( vx, vy ); + m.scale( 1.0*vw/ww, 1.0*vh/wh ); + m.translate( -wx, -wy ); + } + if ( testf(WxF) ) { + if ( testf(VxF) ) + m = wxmat * m; + else + m = wxmat; + } + ixmat = m.invert( &invertible ); // invert matrix +} + +#else +void QPainter::resetXForm() +{ + xlatex = 0; + xlatey = 0; + clearf( VxF ); +} +#endif // QT_NO_TRANSFORMATIONS + + +extern bool qt_old_transformations; + +/*! + \internal + Maps a point from logical coordinates to device coordinates. +*/ + +void QPainter::map( int x, int y, int *rx, int *ry ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( qt_old_transformations ) { + switch ( txop ) { + case TxNone: + *rx = x; *ry = y; + break; + case TxTranslate: + // #### "Why no rounding here?", Warwick asked of Haavard. + *rx = int(x + dx()); + *ry = int(y + dy()); + break; + case TxScale: { + double tx = m11()*x + dx(); + double ty = m22()*y + dy(); + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + } break; + default: { + double tx = m11()*x + m21()*y+dx(); + double ty = m12()*x + m22()*y+dy(); + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + } break; + } + } else { + switch ( txop ) { + case TxNone: + *rx = x; + *ry = y; + break; + case TxTranslate: + *rx = qRound( x + dx() ); + *ry = qRound( y + dy() ); + break; + case TxScale: + *rx = qRound( m11()*x + dx() ); + *ry = qRound( m22()*y + dy() ); + break; + default: + *rx = qRound( m11()*x + m21()*y+dx() ); + *ry = qRound( m12()*x + m22()*y+dy() ); + break; + } + } +#else + *rx = x + xlatex; + *ry = y + xlatey; +#endif +} + +/*! + \internal + Maps a rectangle from logical coordinates to device coordinates. + This internal function does not handle rotation and/or shear. +*/ + +void QPainter::map( int x, int y, int w, int h, + int *rx, int *ry, int *rw, int *rh ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( qt_old_transformations ) { + switch ( txop ) { + case TxNone: + *rx = x; *ry = y; + *rw = w; *rh = h; + break; + case TxTranslate: + // #### "Why no rounding here?", Warwick asked of Haavard. + *rx = int(x + dx()); + *ry = int(y + dy()); + *rw = w; *rh = h; + break; + case TxScale: { + double tx1 = m11()*x + dx(); + double ty1 = m22()*y + dy(); + double tx2 = m11()*(x + w - 1) + dx(); + double ty2 = m22()*(y + h - 1) + dy(); + *rx = qRound( tx1 ); + *ry = qRound( ty1 ); + *rw = qRound( tx2 ) - *rx + 1; + *rh = qRound( ty2 ) - *ry + 1; + } break; + default: +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::map: Internal error" ); +#endif + break; + } + } else { + switch ( txop ) { + case TxNone: + *rx = x; *ry = y; + *rw = w; *rh = h; + break; + case TxTranslate: + *rx = qRound(x + dx() ); + *ry = qRound(y + dy() ); + *rw = w; *rh = h; + break; + case TxScale: + *rx = qRound( m11()*x + dx() ); + *ry = qRound( m22()*y + dy() ); + *rw = qRound( m11()*w ); + *rh = qRound( m22()*h ); + break; + default: +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::map: Internal error" ); +#endif + break; + } + } +#else + *rx = x + xlatex; + *ry = y + xlatey; + *rw = w; *rh = h; +#endif +} + +/*! + \internal + Maps a point from device coordinates to logical coordinates. +*/ + +void QPainter::mapInv( int x, int y, int *rx, int *ry ) const +{ +#ifndef QT_NO_TRANSFORMATIONS +#if defined(QT_CHECK_STATE) + if ( !txinv ) + qWarning( "QPainter::mapInv: Internal error" ); +#endif + if ( qt_old_transformations ) { + double tx = im11()*x + im21()*y+idx(); + double ty = im12()*x + im22()*y+idy(); + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + } else { + *rx = qRound( im11()*x + im21()*y + idx() ); + *ry = qRound( im12()*x + im22()*y + idy() ); + } +#else + *rx = x - xlatex; + *ry = y - xlatey; +#endif +} + +/*! + \internal + Maps a rectangle from device coordinates to logical coordinates. + Cannot handle rotation and/or shear. +*/ + +void QPainter::mapInv( int x, int y, int w, int h, + int *rx, int *ry, int *rw, int *rh ) const +{ +#ifndef QT_NO_TRANSFORMATIONS +#if defined(QT_CHECK_STATE) + if ( !txinv || txop == TxRotShear ) + qWarning( "QPainter::mapInv: Internal error" ); +#endif + if ( qt_old_transformations ) { + double tx = im11()*x + idx(); + double ty = im22()*y + idy(); + double tw = im11()*w; + double th = im22()*h; + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + *rw = tw >= 0 ? int(tw + 0.5) : int(tw - 0.5); + *rh = th >= 0 ? int(th + 0.5) : int(th - 0.5); + } else { + *rx = qRound( im11()*x + idx() ); + *ry = qRound( im22()*y + idy() ); + *rw = qRound( im11()*w ); + *rh = qRound( im22()*h ); + } +#else + *rx = x - xlatex; + *ry = y - xlatey; + *rw = w; + *rh = h; +#endif +} + + +/*! + Returns the point \a pv transformed from model coordinates to + device coordinates. + + \sa xFormDev(), QWMatrix::map() +*/ + +QPoint QPainter::xForm( const QPoint &pv ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return pv; + int x=pv.x(), y=pv.y(); + map( x, y, &x, &y ); + return QPoint( x, y ); +#else + return QPoint( pv.x()+xlatex, pv.y()+xlatey ); +#endif +} + +/*! + \overload + + Returns the rectangle \a rv transformed from model coordinates to + device coordinates. + + If world transformation is enabled and rotation or shearing has + been specified, then the bounding rectangle is returned. + + \sa xFormDev(), QWMatrix::map() +*/ + +QRect QPainter::xForm( const QRect &rv ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return rv; + if ( txop == TxRotShear ) { // rotation/shear + return xmat.mapRect( rv ); + } + // Just translation/scale + int x, y, w, h; + rv.rect( &x, &y, &w, &h ); + map( x, y, w, h, &x, &y, &w, &h ); + return QRect( x, y, w, h ); +#else + return QRect( rv.x()+xlatex, rv.y()+xlatey, rv.width(), rv.height() ); +#endif +} + +/*! + \overload + + Returns the point array \a av transformed from model coordinates + to device coordinates. + + \sa xFormDev(), QWMatrix::map() +*/ + +QPointArray QPainter::xForm( const QPointArray &av ) const +{ + QPointArray a = av; +#ifndef QT_NO_TRANSFORMATIONS + if ( txop != TxNone ) + { + return xmat * av; + } +#else + a.translate( xlatex, xlatey ); +#endif + return a; +} + +/*! + \overload + + Returns the point array \a av transformed from model coordinates + to device coordinates. The \a index is the first point in the + array and \a npoints denotes the number of points to be + transformed. If \a npoints is negative, all points from \a + av[index] until the last point in the array are transformed. + + The returned point array consists of the number of points that + were transformed. + + Example: + \code + QPointArray a(10); + QPointArray b; + b = painter.xForm(a, 2, 4); // b.size() == 4 + b = painter.xForm(a, 2, -1); // b.size() == 8 + \endcode + + \sa xFormDev(), QWMatrix::map() +*/ + +QPointArray QPainter::xForm( const QPointArray &av, int index, + int npoints ) const +{ + int lastPoint = npoints < 0 ? av.size() : index+npoints; + QPointArray a( lastPoint-index ); + memcpy( a.data(), av.data()+index, (lastPoint-index)*sizeof( QPoint ) ); +#ifndef QT_NO_TRANSFORMATIONS + return xmat*a; +#else + a.translate( xlatex, xlatey ); + return a; +#endif +} + +/*! + \overload + + Returns the point \a pd transformed from device coordinates to + model coordinates. + + \sa xForm(), QWMatrix::map() +*/ + +QPoint QPainter::xFormDev( const QPoint &pd ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return pd; + if ( !txinv ) { + QPainter *that = (QPainter*)this; // mutable + that->updateInvXForm(); + } +#endif + int x=pd.x(), y=pd.y(); + mapInv( x, y, &x, &y ); + return QPoint( x, y ); +} + +/*! + Returns the rectangle \a rd transformed from device coordinates to + model coordinates. + + If world transformation is enabled and rotation or shearing is + used, then the bounding rectangle is returned. + + \sa xForm(), QWMatrix::map() +*/ + +QRect QPainter::xFormDev( const QRect &rd ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return rd; + if ( !txinv ) { + QPainter *that = (QPainter*)this; // mutable + that->updateInvXForm(); + } + if ( txop == TxRotShear ) { // rotation/shear + return ixmat.mapRect( rd ); + } +#endif + // Just translation/scale + int x, y, w, h; + rd.rect( &x, &y, &w, &h ); + mapInv( x, y, w, h, &x, &y, &w, &h ); + return QRect( x, y, w, h ); +} + +/*! + \overload + + Returns the point array \a ad transformed from device coordinates + to model coordinates. + + \sa xForm(), QWMatrix::map() +*/ + +QPointArray QPainter::xFormDev( const QPointArray &ad ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return ad; + if ( !txinv ) { + QPainter *that = (QPainter*)this; // mutable + that->updateInvXForm(); + } + return ixmat * ad; +#else + // ### + return ad; +#endif +} + +/*! + \overload + + Returns the point array \a ad transformed from device coordinates + to model coordinates. The \a index is the first point in the array + and \a npoints denotes the number of points to be transformed. If + \a npoints is negative, all points from \a ad[index] until the + last point in the array are transformed. + + The returned point array consists of the number of points that + were transformed. + + Example: + \code + QPointArray a(10); + QPointArray b; + b = painter.xFormDev(a, 1, 3); // b.size() == 3 + b = painter.xFormDev(a, 1, -1); // b.size() == 9 + \endcode + + \sa xForm(), QWMatrix::map() +*/ + +QPointArray QPainter::xFormDev( const QPointArray &ad, int index, + int npoints ) const +{ + int lastPoint = npoints < 0 ? ad.size() : index+npoints; + QPointArray a( lastPoint-index ); + memcpy( a.data(), ad.data()+index, (lastPoint-index)*sizeof( QPoint ) ); +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return a; + if ( !txinv ) { + QPainter *that = (QPainter*)this; // mutable + that->updateInvXForm(); + } + return ixmat * a; +#else + // ### + return a; +#endif +} + + +/*! + Fills the rectangle \a (x, y, w, h) with the \a brush. + + You can specify a QColor as \a brush, since there is a QBrush + constructor that takes a QColor argument and creates a solid + pattern brush. + + \sa drawRect() +*/ + +void QPainter::fillRect( int x, int y, int w, int h, const QBrush &brush ) +{ + QPen oldPen = pen(); // save pen + QBrush oldBrush = this->brush(); // save brush + setPen( NoPen ); + setBrush( brush ); + drawRect( x, y, w, h ); // draw filled rect + setBrush( oldBrush ); // restore brush + setPen( oldPen ); // restore pen +} + + +/*! + \overload void QPainter::setBrushOrigin( const QPoint &p ) + + Sets the brush origin to point \a p. +*/ + +/*! + \overload void QPainter::setWindow( const QRect &r ) + + Sets the painter's window to rectangle \a r. +*/ + + +/*! + \overload void QPainter::setViewport( const QRect &r ) + + Sets the painter's viewport to rectangle \a r. +*/ + + +/*! + \fn bool QPainter::hasClipping() const + + Returns TRUE if clipping has been set; otherwise returns FALSE. + + \sa setClipping() +*/ + +/*! + Returns the currently set clip region. Note that the clip region + is given in physical device coordinates and \e not subject to any + \link coordsys.html coordinate transformation \endlink if \a m is + equal to \c CoordDevice (the default). If \a m equals \c + CoordPainter the returned region is in model coordinates. + + \sa setClipRegion(), setClipRect(), setClipping() QPainter::CoordinateMode +*/ +QRegion QPainter::clipRegion( CoordinateMode m ) const +{ + // ### FIXME in 4.0: + // If the transformation mode is CoordPainter, we should transform the + // clip region with painter transformations. + +#ifndef QT_NO_TRANSFORMATIONS + QRegion r; + if ( m == CoordDevice ) { + r = crgn; + } else { + if ( !txinv ) { + QPainter *that = (QPainter*)this; // mutable + that->updateInvXForm(); + } + + r = ixmat * crgn; + } + return r; +#else + return crgn; +#endif +} + +/*! + \fn void QPainter::setClipRect( int x, int y, int w, int h, CoordinateMode m) + + Sets the clip region to the rectangle \a x, \a y, \a w, \a h and + enables clipping. The clip mode is set to \a m. + + If \a m is \c CoordDevice (the default), the coordinates given for + the clip region are taken to be physical device coordinates and + are \e not subject to any \link coordsys.html coordinate + transformations\endlink. If \a m is \c CoordPainter, the + coordinates given for the clip region are taken to be model + coordinates. + + \sa setClipRegion(), clipRegion(), setClipping() QPainter::CoordinateMode +*/ + +/*! + \overload void QPainter::drawPoint( const QPoint &p ) + + Draws the point \a p. +*/ + + +/*! + \overload void QPainter::moveTo( const QPoint &p ) + + Moves to the point \a p. +*/ + +/*! + \overload void QPainter::lineTo( const QPoint &p ) + + Draws a line to the point \a p. +*/ + +/*! + \overload void QPainter::drawLine( const QPoint &p1, const QPoint &p2 ) + + Draws a line from point \a p1 to point \a p2. +*/ + +/*! + \overload void QPainter::drawRect( const QRect &r ) + + Draws the rectangle \a r. +*/ + +/*! + \overload void QPainter::drawWinFocusRect( const QRect &r ) + + Draws rectangle \a r as a window focus rectangle. +*/ + +/*! + \overload void QPainter::drawWinFocusRect( const QRect &r, const QColor &bgColor ) + + Draws rectangle \a r as a window focus rectangle using background + color \a bgColor. +*/ + + +#if !defined(Q_WS_X11) && !defined(Q_WS_QWS) && !defined(Q_WS_MAC) +// The doc and X implementation of this functions is in qpainter_x11.cpp +void QPainter::drawWinFocusRect( int, int, int, int, + bool, const QColor & ) +{ + // do nothing, only called from X11 specific functions +} +#endif + + +/*! + \overload void QPainter::drawRoundRect( const QRect &r, int xRnd, int yRnd ) + + Draws a rounded rectangle \a r, rounding to the x position \a xRnd + and the y position \a yRnd on each corner. +*/ + +/*! + \overload void QPainter::drawEllipse( const QRect &r ) + + Draws the ellipse that fits inside rectangle \a r. +*/ + +/*! + \overload void QPainter::drawArc( const QRect &r, int a, int alen ) + + Draws the arc that fits inside the rectangle \a r with start angle + \a a and arc length \a alen. +*/ + +/*! + \overload void QPainter::drawPie( const QRect &r, int a, int alen ) + + Draws a pie segment that fits inside the rectangle \a r with start + angle \a a and arc length \a alen. +*/ + +/*! + \overload void QPainter::drawChord( const QRect &r, int a, int alen ) + + Draws a chord that fits inside the rectangle \a r with start angle + \a a and arc length \a alen. +*/ + +/*! + \overload void QPainter::drawPixmap( const QPoint &p, const QPixmap &pm, const QRect &sr ) + + Draws the rectangle \a sr of pixmap \a pm with its origin at point + \a p. +*/ + +/*! + \overload void QPainter::drawPixmap( const QPoint &p, const QPixmap &pm ) + + Draws the pixmap \a pm with its origin at point \a p. +*/ + +void QPainter::drawPixmap( const QPoint &p, const QPixmap &pm ) +{ + drawPixmap( p.x(), p.y(), pm, 0, 0, pm.width(), pm.height() ); +} + +#if !defined(QT_NO_IMAGE_SMOOTHSCALE) || !defined(QT_NO_PIXMAP_TRANSFORMATION) + +/*! + \overload + + Draws the pixmap \a pm into the rectangle \a r. The pixmap is + scaled to fit the rectangle, if image and rectangle size disagree. +*/ +void QPainter::drawPixmap( const QRect &r, const QPixmap &pm ) +{ + int rw = r.width(); + int rh = r.height(); + int iw= pm.width(); + int ih = pm.height(); + if ( rw <= 0 || rh <= 0 || iw <= 0 || ih <= 0 ) + return; + bool scale = ( rw != iw || rh != ih ); + float scaleX = (float)rw/(float)iw; + float scaleY = (float)rh/(float)ih; + bool smooth = ( scaleX < 1.5 || scaleY < 1.5 ); + + if ( testf(ExtDev) ) { + QPDevCmdParam param[2]; + param[0].rect = &r; + param[1].pixmap = ± +#if defined(Q_WS_WIN) + if ( !pdev->cmd( QPaintDevice::PdcDrawPixmap, this, param ) || !hdc ) + return; +#elif defined(Q_WS_QWS) + pdev->cmd( QPaintDevice::PdcDrawPixmap, this, param ); + return; +#elif defined(Q_WS_MAC) + if ( !pdev->cmd( QPaintDevice::PdcDrawPixmap, this, param ) || !pdev->handle()) + return; +#else + if ( !pdev->cmd( QPaintDevice::PdcDrawPixmap, this, param ) || !hd ) + return; +#endif + } + + QPixmap pixmap = pm; + + if ( scale ) { +#ifndef QT_NO_IMAGE_SMOOTHSCALE +# ifndef QT_NO_PIXMAP_TRANSFORMATION + if ( smooth ) +# endif + { + QImage i = pm.convertToImage(); + pixmap = QPixmap( i.smoothScale( rw, rh ) ); + } +# ifndef QT_NO_PIXMAP_TRANSFORMATION + else +# endif +#endif +#ifndef QT_NO_PIXMAP_TRANSFORMATION + { + pixmap = pm.xForm( QWMatrix( scaleX, 0, 0, scaleY, 0, 0 ) ); + } +#endif + } + drawPixmap( r.x(), r.y(), pixmap ); +} + +#endif + +/*! + \overload void QPainter::drawImage( const QPoint &, const QImage &, const QRect &sr, int conversionFlags = 0 ); + + Draws the rectangle \a sr from the image at the given point. +*/ + +/* + Draws at point \a p the \sr rect from image \a pm, using \a + conversionFlags if the image needs to be converted to a pixmap. + The default value for \a conversionFlags is 0; see + convertFromImage() for information about what other values do. + + This function may convert \a image to a pixmap and then draw it, if + device() is a QPixmap or a QWidget, or else draw it directly, if + device() is a QPrinter or QPicture. +*/ + +/*! + Draws at (\a x, \a y) the \a sw by \a sh area of pixels from (\a + sx, \a sy) in \a image, using \a conversionFlags if the image + needs to be converted to a pixmap. The default value for \a + conversionFlags is 0; see convertFromImage() for information about + what other values do. + + This function may convert \a image to a pixmap and then draw it, + if device() is a QPixmap or a QWidget, or else draw it directly, + if device() is a QPrinter or QPicture. + + Currently alpha masks of the image are ignored when painting on a QPrinter. + + \sa drawPixmap() QPixmap::convertFromImage() +*/ +void QPainter::drawImage( int x, int y, const QImage & image, + int sx, int sy, int sw, int sh, + int conversionFlags ) +{ +#ifdef Q_WS_QWS + //### Hackish +# ifndef QT_NO_TRANSFORMATIONS + if ( !image.isNull() && gfx && + (txop==TxNone||txop==TxTranslate) && !testf(ExtDev) ) +# else + if ( !image.isNull() && gfx && !testf(ExtDev) ) +# endif + { + if(sw<0) + sw=image.width(); + if(sh<0) + sh=image.height(); + + QImage image2 = qt_screen->mapToDevice( image ); + + // This is a bit dubious + if(image2.depth()==1) { + image2.setNumColors( 2 ); + image2.setColor( 0, qRgb(255,255,255) ); + image2.setColor( 1, qRgb(0,0,0) ); + } + if ( image2.hasAlphaBuffer() ) + gfx->setAlphaType(QGfx::InlineAlpha); + else + gfx->setAlphaType(QGfx::IgnoreAlpha); + gfx->setSource(&image2); + if ( testf(VxF|WxF) ) { + map( x, y, &x, &y ); + } + gfx->blt(x,y,sw,sh,sx,sy); + return; + } +#endif + + if ( !isActive() || image.isNull() ) + return; + + // right/bottom + if ( sw < 0 ) + sw = image.width() - sx; + if ( sh < 0 ) + sh = image.height() - sy; + + // Sanity-check clipping + if ( sx < 0 ) { + x -= sx; + sw += sx; + sx = 0; + } + if ( sw + sx > image.width() ) + sw = image.width() - sx; + if ( sy < 0 ) { + y -= sy; + sh += sy; + sy = 0; + } + if ( sh + sy > image.height() ) + sh = image.height() - sy; + + if ( sw <= 0 || sh <= 0 ) + return; + + bool all = image.rect().intersect(QRect(sx,sy,sw,sh)) == image.rect(); + QImage subimage = all ? image : image.copy(sx,sy,sw,sh); + + if ( testf(ExtDev) ) { + QPDevCmdParam param[2]; + QRect r( x, y, subimage.width(), subimage.height() ); + param[0].rect = &r; + param[1].image = &subimage; +#if defined(Q_WS_WIN) + if ( !pdev->cmd( QPaintDevice::PdcDrawImage, this, param ) || !hdc ) + return; +#elif defined (Q_WS_QWS) + pdev->cmd( QPaintDevice::PdcDrawImage, this, param ); + return; +#elif defined(Q_WS_MAC) + if(!pdev->cmd( QPaintDevice::PdcDrawImage, this, param ) || !pdev->handle() ) + return; +#else + if ( !pdev->cmd( QPaintDevice::PdcDrawImage, this, param ) || !hd ) + return; +#endif + } + + QPixmap pm; + pm.convertFromImage( subimage, conversionFlags ); + drawPixmap( x, y, pm ); +} + +/*! + \overload void QPainter::drawImage( const QPoint &p, const QImage &i, int conversion_flags ) + + Draws the image \a i at point \a p. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa Qt::ImageConversionFlags +*/ +void QPainter::drawImage( const QPoint & p, const QImage & i, + int conversion_flags ) +{ + drawImage(p, i, i.rect(), conversion_flags); +} + +#if !defined(QT_NO_IMAGE_TRANSFORMATION) || !defined(QT_NO_IMAGE_SMOOTHSCALE) + +/*! + \overload + + Draws the image \a i into the rectangle \a r. The image will be + scaled to fit the rectangle if image and rectangle dimensions + differ. +*/ +void QPainter::drawImage( const QRect &r, const QImage &i ) +{ + int rw = r.width(); + int rh = r.height(); + int iw= i.width(); + int ih = i.height(); + if ( rw <= 0 || rh <= 0 || iw <= 0 || ih <= 0 ) + return; + + if ( testf(ExtDev) ) { + QPDevCmdParam param[2]; + param[0].rect = &r; + param[1].image = &i; +#if defined(Q_WS_WIN) + if ( !pdev->cmd( QPaintDevice::PdcDrawImage, this, param ) || !hdc ) + return; +#elif defined(Q_WS_QWS) + pdev->cmd( QPaintDevice::PdcDrawImage, this, param ); + return; +#elif defined(Q_WS_MAC) + if ( !pdev->cmd( QPaintDevice::PdcDrawImage, this, param ) || !pdev->handle() ) + return; +#else + if ( !pdev->cmd( QPaintDevice::PdcDrawImage, this, param ) || !hd ) + return; +#endif + } + + + bool scale = ( rw != iw || rh != ih ); + float scaleX = (float)rw/(float)iw; + float scaleY = (float)rh/(float)ih; + bool smooth = ( scaleX < 1.5 || scaleY < 1.5 ); + + QImage img = scale + ? ( +#if defined(QT_NO_IMAGE_TRANSFORMATION) + i.smoothScale( rw, rh ) +#elif defined(QT_NO_IMAGE_SMOOTHSCALE) + i.scale( rw, rh ) +#else + smooth ? i.smoothScale( rw, rh ) : i.scale( rw, rh ) +#endif + ) + : i; + + drawImage( r.x(), r.y(), img ); +} + +#endif + + +void bitBlt( QPaintDevice *dst, int dx, int dy, + const QImage *src, int sx, int sy, int sw, int sh, + int conversion_flags ) +{ + QPixmap tmp; + if ( sx == 0 && sy == 0 + && (sw<0 || sw==src->width()) && (sh<0 || sh==src->height()) ) + { + tmp.convertFromImage( *src, conversion_flags ); + } else { + tmp.convertFromImage( src->copy( sx, sy, sw, sh, conversion_flags), + conversion_flags ); + } + bitBlt( dst, dx, dy, &tmp ); +} + + +/*! + \overload void QPainter::drawTiledPixmap( const QRect &r, const QPixmap &pm, const QPoint &sp ) + + Draws a tiled pixmap, \a pm, inside rectangle \a r with its origin + at point \a sp. +*/ + +/*! + \overload void QPainter::drawTiledPixmap( const QRect &r, const QPixmap &pm ) + + Draws a tiled pixmap, \a pm, inside rectangle \a r. +*/ + +/*! + \overload void QPainter::fillRect( const QRect &r, const QBrush &brush ) + + Fills the rectangle \a r using brush \a brush. +*/ + +/*! + \fn void QPainter::eraseRect( int x, int y, int w, int h ) + + Erases the area inside \a x, \a y, \a w, \a h. Equivalent to + \c{fillRect( x, y, w, h, backgroundColor() )}. +*/ + +/*! + \overload void QPainter::eraseRect( const QRect &r ) + + Erases the area inside the rectangle \a r. +*/ + +/*! + \fn QPainter::drawText( int x, int y, const QString &, int len = -1, TextDirection dir = Auto ) + + \overload + + Draws the given text at position \a x, \a y. If \a len is -1 (the + default) all the text is drawn, otherwise the first \a len + characters are drawn. The text's direction is given by \a dir. + + \sa QPainter::TextDirection +*/ + +/*! + \fn void QPainter::drawText( int x, int y, int w, int h, int flags, + const QString&, int len = -1, QRect *br=0, + QTextParag **internal=0 ) + + \overload + + Draws the given text within the rectangle starting at \a x, \a y, + with width \a w and height \a h. If \a len is -1 (the default) all + the text is drawn, otherwise the first \a len characters are + drawn. The text's flags that are given in the \a flags parameter + are \l{Qt::AlignmentFlags} and \l{Qt::TextFlags} OR'd together. \a + br (if not null) is set to the actual bounding rectangle of the + output. The \a internal parameter is for internal use only. +*/ + +/*! + \fn void QPainter::drawText( const QPoint &, const QString &, int len = -1, TextDirection dir = Auto ); + + \overload + + Draws the text at the given point. + + \sa QPainter::TextDirection +*/ + +/* + Draws the text in \a s at point \a p. If \a len is -1 the entire + string is drawn, otherwise just the first \a len characters. The + text's direction is specified by \a dir. +*/ + + +/*! + \fn void QPainter::drawText( int x, int y, const QString &, int pos, int len, TextDirection dir = Auto ); + + \overload + + Draws the text from position \a pos, at point \a (x, y). If \a len is + -1 the entire string is drawn, otherwise just the first \a len + characters. The text's direction is specified by \a dir. +*/ + +/*! + \fn void QPainter::drawText( const QPoint &p, const QString &, int pos, int len, TextDirection dir = Auto ); + + Draws the text from position \a pos, at point \a p. If \a len is + -1 the entire string is drawn, otherwise just the first \a len + characters. The text's direction is specified by \a dir. + + Note that the meaning of \e y is not the same for the two + drawText() varieties. For overloads that take a simple \e x, \e y + pair (or a point), the \e y value is the text's baseline; for + overloads that take a rectangle, \e rect.y() is the top of the + rectangle and the text is aligned within that rectangle in + accordance with the alignment flags. + + \sa QPainter::TextDirection +*/ + +/*! + \fn void QPainter::drawTextItem(const QPoint &, const QTextItem &, int) + \internal +*/ + +static inline void fix_neg_rect( int *x, int *y, int *w, int *h ) +{ + if ( *w < 0 ) { + *w = -*w + 2; + *x -= *w - 1; + } + if ( *h < 0 ) { + *h = -*h + 2; + *y -= *h - 1; + } +} +void QPainter::fix_neg_rect( int *x, int *y, int *w, int *h ) +{ + ::fix_neg_rect(x,y,w,h); +} + +// +// The drawText function takes two special parameters; 'internal' and 'brect'. +// +// The 'internal' parameter contains a pointer to an array of encoded +// information that keeps internal geometry data. +// If the drawText function is called repeatedly to display the same text, +// it makes sense to calculate text width and linebreaks the first time, +// and use these parameters later to print the text because we save a lot of +// CPU time. +// The 'internal' parameter will not be used if it is a null pointer. +// The 'internal' parameter will be generated if it is not null, but points +// to a null pointer, i.e. internal != 0 && *internal == 0. +// The 'internal' parameter will be used if it contains a non-null pointer. +// +// If the 'brect parameter is a non-null pointer, then the bounding rectangle +// of the text will be returned in 'brect'. +// + +/*! + \overload + + Draws at most \a len characters from \a str in the rectangle \a r. + + This function draws formatted text. The \a tf text format is + really of type \l Qt::AlignmentFlags and \l Qt::TextFlags OR'd + together. + + Horizontal alignment defaults to AlignAuto and vertical alignment + defaults to AlignTop. + + \a brect (if not null) is set to the actual bounding rectangle of + the output. \a internal is, yes, internal. + + \sa boundingRect() +*/ + +void QPainter::drawText( const QRect &r, int tf, + const QString& str, int len, QRect *brect, + QTextParag **internal ) +{ + if ( !isActive() ) + return; + if ( len < 0 ) + len = str.length(); + if ( len == 0 ) // empty string + return; + + if ( testf(DirtyFont|ExtDev) ) { + if ( testf(DirtyFont) ) + updateFont(); + if ( testf(ExtDev) && (tf & DontPrint) == 0 ) { + QPDevCmdParam param[3]; + QString newstr = str; + newstr.truncate( len ); + param[0].rect = &r; + param[1].ival = tf; + param[2].str = &newstr; + if ( pdev->devType() != QInternal::Printer ) { +#if defined(Q_WS_WIN) + if ( !pdev->cmd( QPaintDevice::PdcDrawText2Formatted, + this, param) || + !hdc ) + return; // QPrinter wants PdcDrawText2 +#elif defined(Q_WS_QWS) + pdev->cmd( QPaintDevice::PdcDrawText2Formatted, this, param); + return; +#elif defined(Q_WS_MAC) + if ( !pdev->cmd( QPaintDevice::PdcDrawText2Formatted, this, param) || + !pdev->handle()) + return; // QPrinter wants PdcDrawText2 +#else + if ( !pdev->cmd( QPaintDevice::PdcDrawText2Formatted, + this, param) || + !hd ) + return; // QPrinter wants PdcDrawText2 +#endif + } + } + } + + qt_format_text(font(), r, tf, str, len, brect, + tabstops, tabarray, tabarraylen, internal, this); +} + +//#define QT_FORMAT_TEXT_DEBUG + +#define QChar_linesep QChar(0x2028U) + +void qt_format_text( const QFont& font, const QRect &_r, + int tf, const QString& str, int len, QRect *brect, + int tabstops, int* tabarray, int tabarraylen, + QTextParag **, QPainter* painter ) +{ + // we need to copy r here to protect against the case (&r == brect). + QRect r( _r ); + + bool dontclip = (tf & Qt::DontClip) == Qt::DontClip; + bool wordbreak = (tf & Qt::WordBreak) == Qt::WordBreak; + bool singleline = (tf & Qt::SingleLine) == Qt::SingleLine; + bool showprefix = (tf & Qt::ShowPrefix) == Qt::ShowPrefix; + bool noaccel = ( tf & Qt::NoAccel ) == Qt::NoAccel; + + bool isRightToLeft = str.isRightToLeft(); + if ( ( tf & Qt::AlignHorizontal_Mask ) == Qt::AlignAuto ) + tf |= isRightToLeft ? Qt::AlignRight : Qt::AlignLeft; + + bool expandtabs = ( (tf & Qt::ExpandTabs) && + ( ( (tf & Qt::AlignLeft) && !isRightToLeft ) || + ( (tf & Qt::AlignRight) && isRightToLeft ) ) ); + + if ( !painter ) + tf |= Qt::DontPrint; + + int maxUnderlines = 0; + int numUnderlines = 0; + int underlinePositionStack[32]; + int *underlinePositions = underlinePositionStack; + + QFont fnt(painter ? (painter->pfont ? *painter->pfont : painter->cfont) : font); + QFontMetrics fm( fnt ); + + QString text = str; + // str.setLength() always does a deep copy, so the replacement + // code below is safe. + text.setLength( len ); + // compatible behaviour to the old implementation. Replace + // tabs by spaces + QChar *chr = (QChar*)text.unicode(); + const QChar *end = chr + len; + bool haveLineSep = FALSE; + while ( chr != end ) { + if ( *chr == '\r' || ( singleline && *chr == '\n' ) ) { + *chr = ' '; + } else if ( *chr == '\n' ) { + *chr = QChar_linesep; + haveLineSep = TRUE; + } else if ( *chr == '&' ) { + ++maxUnderlines; + } + ++chr; + } + if ( !expandtabs ) { + chr = (QChar*)text.unicode(); + while ( chr != end ) { + if ( *chr == '\t' ) + *chr = ' '; + ++chr; + } + } else if (!tabarraylen && !tabstops) { + tabstops = fm.width('x')*8; + } + + if ( noaccel || showprefix ) { + if ( maxUnderlines > 32 ) + underlinePositions = new int[maxUnderlines]; + QChar *cout = (QChar*)text.unicode(); + QChar *cin = cout; + int l = len; + while ( l ) { + if ( *cin == '&' ) { + ++cin; + --l; + if ( !l ) + break; + if ( *cin != '&' ) + underlinePositions[numUnderlines++] = cout - text.unicode(); + } + *cout = *cin; + ++cout; + ++cin; + --l; + } + uint newlen = cout - text.unicode(); + if ( newlen != text.length()) + text.setLength( newlen ); + } + + // no need to do extra work for underlines if we don't paint + if ( tf & Qt::DontPrint ) + numUnderlines = 0; + + int height = 0; + int left = r.width(); + int right = 0; + + QTextLayout textLayout( text, fnt ); + int rb = QMAX( 0, -fm.minRightBearing() ); + int lb = QMAX( 0, -fm.minLeftBearing() ); + + if ( text.isEmpty() ) { + height = fm.height(); + left = right = 0; + tf |= QPainter::DontPrint; + } else { + textLayout.beginLayout((haveLineSep || expandtabs || wordbreak) ? + QTextLayout::MultiLine : + (tf & Qt::DontPrint) ? QTextLayout::NoBidi : QTextLayout::SingleLine ); + + // break underline chars into items of their own + for( int i = 0; i < numUnderlines; i++ ) { + textLayout.setBoundary( underlinePositions[i] ); + textLayout.setBoundary( underlinePositions[i]+1 ); + } + + int lineWidth = wordbreak ? QMAX(0, r.width()-rb-lb) : INT_MAX; + if(!wordbreak) + tf |= Qt::IncludeTrailingSpaces; + + int leading = fm.leading(); + int asc = fm.ascent(); + int desc = fm.descent(); + height = -leading; + + //qDebug("\n\nbeginLayout: lw = %d, rectwidth=%d", lineWidth , r.width()); + while ( !textLayout.atEnd() ) { + height += leading; + textLayout.beginLine( lineWidth == INT_MAX ? lineWidth : lineWidth ); + //qDebug("-----beginLine( %d )-----", lineWidth ); + bool linesep = FALSE; + while ( 1 ) { + QTextItem ti = textLayout.currentItem(); + //qDebug("item: from=%d, ch=%x", ti.from(), text.unicode()[ti.from()].unicode() ); + if ( expandtabs && ti.isTab() ) { + int tw = 0; + int x = textLayout.widthUsed(); + if ( tabarraylen ) { +// qDebug("tabarraylen=%d", tabarraylen ); + int tab = 0; + while ( tab < tabarraylen ) { + if ( tabarray[tab] > x ) { + tw = tabarray[tab] - x; + break; + } + ++tab; + } + } else { + tw = tabstops - (x % tabstops); + } + //qDebug("tw = %d", tw ); + if ( tw ) + ti.setWidth( tw ); + } + if ( ti.isObject() && text.unicode()[ti.from()] == QChar_linesep ) + linesep = TRUE; + + if ( linesep || textLayout.addCurrentItem() != QTextLayout::Ok || textLayout.atEnd() ) + break; + } + + int ascent = asc, descent = desc, lineLeft, lineRight; + textLayout.setLineWidth( r.width()-rb-lb ); + textLayout.endLine( 0, height, tf, &ascent, &descent, + &lineLeft, &lineRight ); + //qDebug("finalizing line: lw=%d ascent = %d, descent=%d lineleft=%d lineright=%d", lineWidth, ascent, descent,lineLeft, lineRight ); + left = QMIN( left, lineLeft ); + right = QMAX( right, lineRight ); + height += ascent + descent + 1; + if ( linesep ) + textLayout.nextItem(); + } + } + + int yoff = 0; + if ( tf & Qt::AlignBottom ) + yoff = r.height() - height; + else if ( tf & Qt::AlignVCenter ) + yoff = (r.height() - height)/2; + + if ( brect ) { + *brect = QRect( r.x() + left, r.y() + yoff, right-left + lb+rb, height ); + //qDebug("br = %d %d %d/%d, left=%d, right=%d", brect->x(), brect->y(), brect->width(), brect->height(), left, right); + } + + if (!(tf & QPainter::DontPrint)) { + bool restoreClipping = FALSE; + bool painterHasClip = FALSE; + QRegion painterClipRegion; + if ( !dontclip ) { +#ifndef QT_NO_TRANSFORMATIONS + QRegion reg = painter->xmat * r; +#else + QRegion reg = r; + reg.translate( painter->xlatex, painter->xlatey ); +#endif + if ( painter->hasClipping() ) + reg &= painter->clipRegion(); + + painterHasClip = painter->hasClipping(); + painterClipRegion = painter->clipRegion(); + restoreClipping = TRUE; + painter->setClipRegion( reg ); + } else { + if ( painter->hasClipping() ){ + painterHasClip = painter->hasClipping(); + painterClipRegion = painter->clipRegion(); + restoreClipping = TRUE; + painter->setClipping( FALSE ); + } + } + + int cUlChar = 0; + int _tf = 0; + if (fnt.underline()) _tf |= Qt::Underline; + if (fnt.overline()) _tf |= Qt::Overline; + if (fnt.strikeOut()) _tf |= Qt::StrikeOut; + + //qDebug("have %d items",textLayout.numItems()); + for ( int i = 0; i < textLayout.numItems(); i++ ) { + QTextItem ti = textLayout.itemAt( i ); + //qDebug("Item %d: from=%d, length=%d, space=%d x=%d", i, ti.from(), ti.length(), ti.isSpace(), ti.x() ); + if ( ti.isTab() || ti.isObject() ) + continue; + int textFlags = _tf; + if ( !noaccel && numUnderlines > cUlChar && ti.from() == underlinePositions[cUlChar] ) { + textFlags |= Qt::Underline; + cUlChar++; + } +#if defined(Q_WS_X11) || defined(Q_WS_QWS) + if ( painter->bg_mode == Qt::OpaqueMode ) { + int h = ti.ascent() + ti.descent() + 1; + if (ti.y() + h < height) + // don't add leading to last line + h += fm.leading(); + qt_draw_background( painter, r.x()+lb + ti.x(), r.y() + yoff + ti.y() - ti.ascent(), + ti.width(), h); + } +#endif + painter->drawTextItem( r.x()+lb, r.y() + yoff, ti, textFlags ); + } + + if ( restoreClipping ) { + painter->setClipRegion( painterClipRegion ); + painter->setClipping( painterHasClip ); + } + } + + if ( underlinePositions != underlinePositionStack ) + delete [] underlinePositions; +} + +/*! + \overload + + Returns the bounding rectangle of the aligned text that would be + printed with the corresponding drawText() function using the first + \a len characters from \a str if \a len is > -1, or the whole of + \a str if \a len is -1. The drawing, and hence the bounding + rectangle, is constrained to the rectangle \a r, or to the + rectangle required to draw the text, whichever is the larger. + + The \a internal parameter should not be used. + + \sa drawText(), fontMetrics(), QFontMetrics::boundingRect(), Qt::TextFlags +*/ + +QRect QPainter::boundingRect( const QRect &r, int flags, + const QString& str, int len, QTextParag **internal ) +{ + QRect brect; + if ( str.isEmpty() ) + brect.setRect( r.x(),r.y(), 0,0 ); + else + drawText( r, flags | DontPrint, str, len, &brect, internal ); + return brect; +} + +/*! + \fn QRect QPainter::boundingRect( int x, int y, int w, int h, int flags, const QString&, int len = -1, QTextParag **intern=0 ); + + Returns the bounding rectangle of the aligned text that would be + printed with the corresponding drawText() function using the first + \a len characters of the string if \a len is > -1, or the whole of + the string if \a len is -1. The drawing, and hence the bounding + rectangle, is constrained to the rectangle that begins at point \a + (x, y) with width \a w and hight \a h, or to the + rectangle required to draw the text, whichever is the larger. + + The \a flags argument is + the bitwise OR of the following flags: + \table + \header \i Flag \i Meaning + \row \i \c AlignAuto \i aligns according to the language, usually left. + \row \i \c AlignLeft \i aligns to the left border. + \row \i \c AlignRight \i aligns to the right border. + \row \i \c AlignHCenter \i aligns horizontally centered. + \row \i \c AlignTop \i aligns to the top border. + \row \i \c AlignBottom \i aligns to the bottom border. + \row \i \c AlignVCenter \i aligns vertically centered. + \row \i \c AlignCenter \i (== \c AlignHCenter | \c AlignVCenter). + \row \i \c SingleLine \i ignores newline characters in the text. + \row \i \c ExpandTabs \i expands tabs. + \row \i \c ShowPrefix \i interprets "&x" as "<u>x</u>". + \row \i \c WordBreak \i breaks the text to fit the rectangle. + \endtable + + Horizontal alignment defaults to \c AlignLeft and vertical + alignment defaults to \c AlignTop. + + If several of the horizontal or several of the vertical alignment flags + are set, the resulting alignment is undefined. + + The \a intern parameter should not be used. + + \sa Qt::TextFlags +*/ + + + +/***************************************************************************** + QPen member functions + *****************************************************************************/ + +/*! + \class QPen qpen.h + \brief The QPen class defines how a QPainter should draw lines and outlines + of shapes. + + \ingroup graphics + \ingroup images + \ingroup shared + \mainclass + + A pen has a style, width, color, cap style and join style. + + The pen style defines the line type. The default pen style is \c + Qt::SolidLine. Setting the style to \c NoPen tells the painter to + not draw lines or outlines. + + When drawing 1 pixel wide diagonal lines you can either use a very + fast algorithm (specified by a line width of 0, which is the + default), or a slower but more accurate algorithm (specified by a + line width of 1). For horizontal and vertical lines a line width + of 0 is the same as a line width of 1. The cap and join style have + no effect on 0-width lines. + + The pen color defines the color of lines and text. The default + line color is black. The QColor documentation lists predefined + colors. + + The cap style defines how the end points of lines are drawn. The + join style defines how the joins between two lines are drawn when + multiple connected lines are drawn (QPainter::drawPolyline() + etc.). The cap and join styles only apply to wide lines, i.e. when + the width is 1 or greater. + + Use the QBrush class to specify fill styles. + + Example: + \code + QPainter painter; + QPen pen( red, 2 ); // red solid line, 2 pixels wide + painter.begin( &anyPaintDevice ); // paint something + painter.setPen( pen ); // set the red, wide pen + painter.drawRect( 40,30, 200,100 ); // draw a rectangle + painter.setPen( blue ); // set blue pen, 0 pixel width + painter.drawLine( 40,30, 240,130 ); // draw a diagonal in rectangle + painter.end(); // painting done + \endcode + + See the \l Qt::PenStyle enum type for a complete list of pen + styles. + + With reference to the end points of lines, for wide (non-0-width) + pens it depends on the cap style whether the end point is drawn or + not. QPainter will try to make sure that the end point is drawn + for 0-width pens, but this cannot be absolutely guaranteed because + the underlying drawing engine is free to use any (typically + accelerated) algorithm for drawing 0-width lines. On all tested + systems, however, the end point of at least all non-diagonal lines + are drawn. + + A pen's color(), width(), style(), capStyle() and joinStyle() can + be set in the constructor or later with setColor(), setWidth(), + setStyle(), setCapStyle() and setJoinStyle(). Pens may also be + compared and streamed. + + \img pen-styles.png Pen styles + + \sa QPainter, QPainter::setPen() +*/ + + +/*! + \internal + Initializes the pen. +*/ + +void QPen::init( const QColor &color, uint width, uint linestyle ) +{ + data = new QPenData; + Q_CHECK_PTR( data ); + data->style = (PenStyle)(linestyle & MPenStyle); + data->width = width; + data->color = color; + data->linest = linestyle; +} + +/*! + Constructs a default black solid line pen with 0 width, which + renders lines 1 pixel wide (fast diagonals). +*/ + +QPen::QPen() +{ + init( Qt::black, 0, SolidLine ); // default pen +} + +/*! + Constructs a black pen with 0 width (fast diagonals) and style \a + style. + + \sa setStyle() +*/ + +QPen::QPen( PenStyle style ) +{ + init( Qt::black, 0, style ); +} + +/*! + Constructs a pen with the specified \a color, \a width and \a + style. + + \sa setWidth(), setStyle(), setColor() +*/ + +QPen::QPen( const QColor &color, uint width, PenStyle style ) +{ + init( color, width, style ); +} + +/*! + Constructs a pen with the specified color \a cl and width \a w. + The pen style is set to \a s, the pen cap style to \a c and the + pen join style to \a j. + + A line width of 0 will produce a 1 pixel wide line using a fast + algorithm for diagonals. A line width of 1 will also produce a 1 + pixel wide line, but uses a slower more accurate algorithm for + diagonals. For horizontal and vertical lines a line width of 0 is + the same as a line width of 1. The cap and join style have no + effect on 0-width lines. + + \sa setWidth(), setStyle(), setColor() +*/ + +QPen::QPen( const QColor &cl, uint w, PenStyle s, PenCapStyle c, + PenJoinStyle j ) +{ + init( cl, w, s | c | j ); +} + +/*! + Constructs a pen that is a copy of \a p. +*/ + +QPen::QPen( const QPen &p ) +{ + data = p.data; + data->ref(); +} + +/*! + Destroys the pen. +*/ + +QPen::~QPen() +{ + if ( data->deref() ) + delete data; +} + + +/*! + Detaches from shared pen data to make sure that this pen is the + only one referring the data. + + If multiple pens share common data, this pen dereferences the data + and gets a copy of the data. Nothing is done if there is just a + single reference. +*/ + +void QPen::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + + +/*! + Assigns \a p to this pen and returns a reference to this pen. +*/ + +QPen &QPen::operator=( const QPen &p ) +{ + p.data->ref(); + if ( data->deref() ) + delete data; + data = p.data; + return *this; +} + + +/*! + Returns a \link shclass.html deep copy\endlink of the pen. +*/ + +QPen QPen::copy() const +{ + QPen p( data->color, data->width, data->style, capStyle(), joinStyle() ); + return p; +} + + +/*! + \fn PenStyle QPen::style() const + + Returns the pen style. + + \sa setStyle() +*/ + +/*! + Sets the pen style to \a s. + + See the \l Qt::PenStyle documentation for a list of all the + styles. + + \warning On Mac OS X the style setting (other than \c NoPen and \c + SolidLine) have no effect as they are not implemented by the + underlying system. + + \warning On Windows 95/98, the style setting (other than \c NoPen + and \c SolidLine) has no effect for lines with width greater than + 1. + + \sa style() +*/ + +void QPen::setStyle( PenStyle s ) +{ + if ( data->style == s ) + return; + detach(); + data->style = s; + data->linest = (data->linest & ~MPenStyle) | s; +} + + +/*! + \fn uint QPen::width() const + + Returns the pen width. + + \sa setWidth() +*/ + +/*! + Sets the pen width to \a w. + + A line width of 0 will produce a 1 pixel wide line using a fast + algorithm for diagonals. A line width of 1 will also produce a 1 + pixel wide line, but uses a slower more accurate algorithm for + diagonals. For horizontal and vertical lines a line width of 0 is + the same as a line width of 1. The cap and join style have no + effect on 0-width lines. + + \sa width() +*/ + +void QPen::setWidth( uint w ) +{ + if ( data->width == w ) + return; + detach(); + data->width = w; +} + + +/*! + Returns the pen's cap style. + + \sa setCapStyle() +*/ +Qt::PenCapStyle QPen::capStyle() const +{ + return (PenCapStyle)(data->linest & MPenCapStyle); +} + +/*! + Sets the pen's cap style to \a c. + + The default value is \c FlatCap. The cap style has no effect on + 0-width pens. + + \img pen-cap-styles.png Pen Cap Styles + + \warning On Windows 95/98 and Macintosh, the cap style setting has + no effect. Wide lines are rendered as if the cap style was \c + SquareCap. + + \sa capStyle() +*/ + +void QPen::setCapStyle( PenCapStyle c ) +{ + if ( (data->linest & MPenCapStyle) == c ) + return; + detach(); + data->linest = (data->linest & ~MPenCapStyle) | c; +} + +/*! + Returns the pen's join style. + + \sa setJoinStyle() +*/ +Qt::PenJoinStyle QPen::joinStyle() const +{ + return (PenJoinStyle)(data->linest & MPenJoinStyle); +} + +/*! + Sets the pen's join style to \a j. + + The default value is \c MiterJoin. The join style has no effect on + 0-width pens. + + \img pen-join-styles.png Pen Join Styles + + \warning On Windows 95/98 and Macintosh, the join style setting + has no effect. Wide lines are rendered as if the join style was \c + BevelJoin. + + \sa joinStyle() +*/ + +void QPen::setJoinStyle( PenJoinStyle j ) +{ + if ( (data->linest & MPenJoinStyle) == j ) + return; + detach(); + data->linest = (data->linest & ~MPenJoinStyle) | j; +} + +/*! + \fn const QColor &QPen::color() const + + Returns the pen color. + + \sa setColor() +*/ + +/*! + Sets the pen color to \a c. + + \sa color() +*/ + +void QPen::setColor( const QColor &c ) +{ + detach(); + data->color = c; +} + + +/*! + \fn bool QPen::operator!=( const QPen &p ) const + + Returns TRUE if the pen is different from \a p; otherwise returns + FALSE. + + Two pens are different if they have different styles, widths or + colors. + + \sa operator==() +*/ + +/*! + Returns TRUE if the pen is equal to \a p; otherwise returns FALSE. + + Two pens are equal if they have equal styles, widths and colors. + + \sa operator!=() +*/ + +bool QPen::operator==( const QPen &p ) const +{ + return (p.data == data) || (p.data->linest == data->linest && + p.data->width == data->width && p.data->color == data->color); +} + + +/***************************************************************************** + QPen stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QPen + + Writes the pen \a p to the stream \a s and returns a reference to + the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QPen &p ) +{ + // ### width() should not be restricted to 8-bit values + if ( s.version() < 3 ) + return s << (Q_UINT8)p.style() << (Q_UINT8)p.width() << p.color(); + else + return s << (Q_UINT8)( p.style() | p.capStyle() | p.joinStyle() ) + << (Q_UINT8)p.width() << p.color(); +} + +/*! + \relates QPen + + Reads a pen from the stream \a s into \a p and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QPen &p ) +{ + Q_UINT8 style, width; + QColor color; + s >> style; + s >> width; + s >> color; + p = QPen( color, (uint)width, (Qt::PenStyle)style ); // owl + return s; +} +#endif //QT_NO_DATASTREAM + +/***************************************************************************** + QBrush member functions + *****************************************************************************/ + +/*! + \class QBrush qbrush.h + + \brief The QBrush class defines the fill pattern of shapes drawn by a QPainter. + + \ingroup graphics + \ingroup images + \ingroup shared + + A brush has a style and a color. One of the brush styles is a + custom pattern, which is defined by a QPixmap. + + The brush style defines the fill pattern. The default brush style + is \c NoBrush (depending on how you construct a brush). This style + tells the painter to not fill shapes. The standard style for + filling is \c SolidPattern. + + The brush color defines the color of the fill pattern. The QColor + documentation lists the predefined colors. + + Use the QPen class for specifying line/outline styles. + + Example: + \code + QPainter painter; + QBrush brush( yellow ); // yellow solid pattern + painter.begin( &anyPaintDevice ); // paint something + painter.setBrush( brush ); // set the yellow brush + painter.setPen( NoPen ); // do not draw outline + painter.drawRect( 40,30, 200,100 ); // draw filled rectangle + painter.setBrush( NoBrush ); // do not fill + painter.setPen( black ); // set black pen, 0 pixel width + painter.drawRect( 10,10, 30,20 ); // draw rectangle outline + painter.end(); // painting done + \endcode + + See the setStyle() function for a complete list of brush styles. + + \img brush-styles.png Brush Styles + + \sa QPainter, QPainter::setBrush(), QPainter::setBrushOrigin() +*/ + + +/*! + \internal + Initializes the brush. +*/ + +void QBrush::init( const QColor &color, BrushStyle style ) +{ + data = new QBrushData; + Q_CHECK_PTR( data ); + data->style = style; + data->color = color; + data->pixmap = 0; +} + +/*! + Constructs a default black brush with the style \c NoBrush (will + not fill shapes). +*/ + +QBrush::QBrush() +{ + static QBrushData* defBrushData = 0; + if ( !defBrushData ) { + static QSharedCleanupHandler<QBrushData> defBrushCleanup; + defBrushData = new QBrushData; + defBrushData->style = NoBrush; + defBrushData->color = Qt::black; + defBrushData->pixmap = 0; + defBrushCleanup.set( &defBrushData ); + } + data = defBrushData; + data->ref(); +} + +/*! + Constructs a black brush with the style \a style. + + \sa setStyle() +*/ + +QBrush::QBrush( BrushStyle style ) +{ + init( Qt::black, style ); +} + +/*! + Constructs a brush with the color \a color and the style \a style. + + \sa setColor(), setStyle() +*/ + +QBrush::QBrush( const QColor &color, BrushStyle style ) +{ + init( color, style ); +} + +/*! + Constructs a brush with the color \a color and a custom pattern + stored in \a pixmap. + + The color will only have an effect for monochrome pixmaps, i.e. + for QPixmap::depth() == 1. + + Pixmap brushes are currently not supported when printing on X11. + + \sa setColor(), setPixmap() +*/ + +QBrush::QBrush( const QColor &color, const QPixmap &pixmap ) +{ + init( color, CustomPattern ); + setPixmap( pixmap ); +} + +/*! + Constructs a brush that is a \link shclass.html shallow + copy\endlink of \a b. +*/ + +QBrush::QBrush( const QBrush &b ) +{ + data = b.data; + data->ref(); +} + +/*! + Destroys the brush. +*/ + +QBrush::~QBrush() +{ + if ( data->deref() ) { + delete data->pixmap; + delete data; + } +} + + +/*! + Detaches from shared brush data to make sure that this brush is + the only one referring the data. + + If multiple brushes share common data, this brush dereferences the + data and gets a copy of the data. Nothing is done if there is just + a single reference. +*/ + +void QBrush::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + + +/*! + Assigns \a b to this brush and returns a reference to this brush. +*/ + +QBrush &QBrush::operator=( const QBrush &b ) +{ + b.data->ref(); // beware of b = b + if ( data->deref() ) { + delete data->pixmap; + delete data; + } + data = b.data; + return *this; +} + + +/*! + Returns a \link shclass.html deep copy\endlink of the brush. +*/ + +QBrush QBrush::copy() const +{ + if ( data->style == CustomPattern ) { // brush has pixmap + QBrush b( data->color, *data->pixmap ); + return b; + } else { // brush has std pattern + QBrush b( data->color, data->style ); + return b; + } +} + + +/*! + \fn BrushStyle QBrush::style() const + + Returns the brush style. + + \sa setStyle() +*/ + +/*! + Sets the brush style to \a s. + + The brush styles are: + \table + \header \i Pattern \i Meaning + \row \i NoBrush \i will not fill shapes (default). + \row \i SolidPattern \i solid (100%) fill pattern. + \row \i Dense1Pattern \i11 94% fill pattern. + \row \i Dense2Pattern \i11 88% fill pattern. + \row \i Dense3Pattern \i11 63% fill pattern. + \row \i Dense4Pattern \i11 50% fill pattern. + \row \i Dense5Pattern \i11 37% fill pattern. + \row \i Dense6Pattern \i11 12% fill pattern. + \row \i Dense7Pattern \i11 6% fill pattern. + \row \i HorPattern \i horizontal lines pattern. + \row \i VerPattern \i vertical lines pattern. + \row \i CrossPattern \i crossing lines pattern. + \row \i BDiagPattern \i diagonal lines (directed /) pattern. + \row \i FDiagPattern \i diagonal lines (directed \) pattern. + \row \i DiagCrossPattern \i diagonal crossing lines pattern. + \row \i CustomPattern \i set when a pixmap pattern is being used. + \endtable + + On Windows, dense and custom patterns cannot be transparent. + + See the \link #details Detailed Description\endlink for a picture + of all the styles. + + \sa style() +*/ + +void QBrush::setStyle( BrushStyle s ) // set brush style +{ + if ( data->style == s ) + return; +#if defined(QT_CHECK_RANGE) + if ( s == CustomPattern ) + qWarning( "QBrush::setStyle: CustomPattern is for internal use" ); +#endif + detach(); + data->style = s; +} + + +/*! + \fn const QColor &QBrush::color() const + + Returns the brush color. + + \sa setColor() +*/ + +/*! + Sets the brush color to \a c. + + \sa color(), setStyle() +*/ + +void QBrush::setColor( const QColor &c ) +{ + detach(); + data->color = c; +} + + +/*! + \fn QPixmap *QBrush::pixmap() const + + Returns a pointer to the custom brush pattern, or 0 if no custom + brush pattern has been set. + + \sa setPixmap() +*/ + +/*! + Sets the brush pixmap to \a pixmap. The style is set to \c + CustomPattern. + + The current brush color will only have an effect for monochrome + pixmaps, i.e. for QPixmap::depth() == 1. + + Pixmap brushes are currently not supported when printing on X11. + + \sa pixmap(), color() +*/ + +void QBrush::setPixmap( const QPixmap &pixmap ) +{ + detach(); + if ( data->pixmap ) + delete data->pixmap; + if ( pixmap.isNull() ) { + data->style = NoBrush; + data->pixmap = 0; + } else { + data->style = CustomPattern; + data->pixmap = new QPixmap( pixmap ); + if ( data->pixmap->optimization() == QPixmap::MemoryOptim ) + data->pixmap->setOptimization( QPixmap::NormalOptim ); + } +} + + +/*! + \fn bool QBrush::operator!=( const QBrush &b ) const + + Returns TRUE if the brush is different from \a b; otherwise + returns FALSE. + + Two brushes are different if they have different styles, colors or + pixmaps. + + \sa operator==() +*/ + +/*! + Returns TRUE if the brush is equal to \a b; otherwise returns + FALSE. + + Two brushes are equal if they have equal styles, colors and + pixmaps. + + \sa operator!=() +*/ + +bool QBrush::operator==( const QBrush &b ) const +{ + return (b.data == data) || (b.data->style == data->style && + b.data->color == data->color && + b.data->pixmap == data->pixmap); +} + + +/*! + \fn inline double QPainter::translationX() const + \internal +*/ + +/*! + \fn inline double QPainter::translationY() const + \internal +*/ + + +/***************************************************************************** + QBrush stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QBrush + + Writes the brush \a b to the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QBrush &b ) +{ + s << (Q_UINT8)b.style() << b.color(); + if ( b.style() == Qt::CustomPattern ) +#ifndef QT_NO_IMAGEIO + s << *b.pixmap(); +#else + qWarning("No Image Brush I/O"); +#endif + return s; +} + +/*! + \relates QBrush + + Reads the brush \a b from the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QBrush &b ) +{ + Q_UINT8 style; + QColor color; + s >> style; + s >> color; + if ( style == Qt::CustomPattern ) { +#ifndef QT_NO_IMAGEIO + QPixmap pm; + s >> pm; + b = QBrush( color, pm ); +#else + qWarning("No Image Brush I/O"); +#endif + } + else + b = QBrush( color, (Qt::BrushStyle)style ); + return s; +} +#endif // QT_NO_DATASTREAM diff --git a/src/kernel/qpainter.h b/src/kernel/qpainter.h new file mode 100644 index 0000000..454a380 --- /dev/null +++ b/src/kernel/qpainter.h @@ -0,0 +1,721 @@ +/**************************************************************************** +** +** Definition of QPainter class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPAINTER_H +#define QPAINTER_H + + +#ifndef QT_H +#include "qcolor.h" +#include "qfontmetrics.h" +#include "qfontinfo.h" +#include "qregion.h" +#include "qpen.h" +#include "qbrush.h" +#include "qpointarray.h" +#include "qwmatrix.h" +#endif // QT_H + +class QGfx; +class QTextCodec; +class QTextParag; +class QPaintDevice; +class QTextItem; +#if defined( Q_WS_MAC ) +class QMacSavedPortInfo; +#endif +class QPainterPrivate; + +#if defined(Q_WS_QWS) +class QScreen; +#endif + +class Q_EXPORT QPainter : public Qt +{ +public: + enum CoordinateMode { CoordDevice, CoordPainter }; + + QPainter(); + QPainter( const QPaintDevice *, bool unclipped = FALSE ); + QPainter( const QPaintDevice *, const QWidget *, bool unclipped = FALSE ); + ~QPainter(); + + bool begin( const QPaintDevice *, bool unclipped = FALSE ); + bool begin( const QPaintDevice *, const QWidget *, bool unclipped = FALSE ); + bool end(); + QPaintDevice *device() const; + +#ifdef Q_WS_QWS + QGfx * internalGfx(); +#ifdef QT_QWS_EXPERIMENTAL_SCREENPAINTER + bool begin(QScreen *screen); +#endif +#endif + + static void redirect( QPaintDevice *pdev, QPaintDevice *replacement ); + static QPaintDevice *redirect( QPaintDevice *pdev ); + + bool isActive() const; + + void flush( const QRegion ®ion, CoordinateMode cm = CoordDevice ); + void flush(); + void save(); + void restore(); + + // Drawing tools + + QFontMetrics fontMetrics() const; + QFontInfo fontInfo() const; + + const QFont &font() const; + void setFont( const QFont & ); + const QPen &pen() const; + void setPen( const QPen & ); + void setPen( PenStyle ); + void setPen( const QColor & ); + const QBrush &brush() const; + void setBrush( const QBrush & ); + void setBrush( BrushStyle ); + void setBrush( const QColor & ); + QPoint pos() const; + + // Drawing attributes/modes + + const QColor &backgroundColor() const; + void setBackgroundColor( const QColor & ); + BGMode backgroundMode() const; + void setBackgroundMode( BGMode ); + RasterOp rasterOp() const; + void setRasterOp( RasterOp ); + const QPoint &brushOrigin() const; + void setBrushOrigin( int x, int y ); + void setBrushOrigin( const QPoint & ); + + // Scaling and transformations + +// PaintUnit unit() const; // get set painter unit +// void setUnit( PaintUnit ); // NOT IMPLEMENTED!!! + + bool hasViewXForm() const; + bool hasWorldXForm() const; + +#ifndef QT_NO_TRANSFORMATIONS + void setViewXForm( bool ); // set xform on/off + QRect window() const; // get window + void setWindow( const QRect & ); // set window + void setWindow( int x, int y, int w, int h ); + QRect viewport() const; // get viewport + void setViewport( const QRect & ); // set viewport + void setViewport( int x, int y, int w, int h ); + + void setWorldXForm( bool ); // set world xform on/off + const QWMatrix &worldMatrix() const; // get/set world xform matrix + void setWorldMatrix( const QWMatrix &, bool combine=FALSE ); + + void saveWorldMatrix(); + void restoreWorldMatrix(); + + void scale( double sx, double sy ); + void shear( double sh, double sv ); + void rotate( double a ); +#endif + void translate( double dx, double dy ); + void resetXForm(); + double translationX() const; + double translationY() const; + + QPoint xForm( const QPoint & ) const; // map virtual -> device + QRect xForm( const QRect & ) const; + QPointArray xForm( const QPointArray & ) const; + QPointArray xForm( const QPointArray &, int index, int npoints ) const; + QPoint xFormDev( const QPoint & ) const; // map device -> virtual + QRect xFormDev( const QRect & ) const; + QPointArray xFormDev( const QPointArray & ) const; + QPointArray xFormDev( const QPointArray &, int index, int npoints ) const; + + // Clipping + + void setClipping( bool ); // set clipping on/off + bool hasClipping() const; + QRegion clipRegion( CoordinateMode = CoordDevice ) const; + void setClipRect( const QRect &, CoordinateMode = CoordDevice ); // set clip rectangle + void setClipRect( int x, int y, int w, int h, CoordinateMode = CoordDevice ); + void setClipRegion( const QRegion &, CoordinateMode = CoordDevice );// set clip region + + // Graphics drawing functions + + void drawPoint( int x, int y ); + void drawPoint( const QPoint & ); + void drawPoints( const QPointArray& a, + int index=0, int npoints=-1 ); + void moveTo( int x, int y ); + void moveTo( const QPoint & ); + void lineTo( int x, int y ); + void lineTo( const QPoint & ); + void drawLine( int x1, int y1, int x2, int y2 ); + void drawLine( const QPoint &, const QPoint & ); + void drawRect( int x, int y, int w, int h ); + void drawRect( const QRect & ); + void drawWinFocusRect( int x, int y, int w, int h ); + void drawWinFocusRect( int x, int y, int w, int h, + const QColor &bgColor ); + void drawWinFocusRect( const QRect & ); + void drawWinFocusRect( const QRect &, + const QColor &bgColor ); + void drawRoundRect( int x, int y, int w, int h, int = 25, int = 25 ); + void drawRoundRect( const QRect &, int = 25, int = 25 ); + void drawEllipse( int x, int y, int w, int h ); + void drawEllipse( const QRect & ); + void drawArc( int x, int y, int w, int h, int a, int alen ); + void drawArc( const QRect &, int a, int alen ); + void drawPie( int x, int y, int w, int h, int a, int alen ); + void drawPie( const QRect &, int a, int alen ); + void drawChord( int x, int y, int w, int h, int a, int alen ); + void drawChord( const QRect &, int a, int alen ); + void drawLineSegments( const QPointArray &, + int index=0, int nlines=-1 ); + void drawPolyline( const QPointArray &, + int index=0, int npoints=-1 ); + void drawPolygon( const QPointArray &, bool winding=FALSE, + int index=0, int npoints=-1 ); + void drawConvexPolygon( const QPointArray &, + int index=0, int npoints=-1 ); +#ifndef QT_NO_BEZIER + void drawCubicBezier( const QPointArray &, int index=0 ); +#endif + void drawPixmap( int x, int y, const QPixmap &, + int sx=0, int sy=0, int sw=-1, int sh=-1 ); + void drawPixmap( const QPoint &, const QPixmap &, + const QRect &sr ); + void drawPixmap( const QPoint &, const QPixmap & ); + void drawPixmap( const QRect &, const QPixmap & ); + void drawImage( int x, int y, const QImage &, + int sx = 0, int sy = 0, int sw = -1, int sh = -1, + int conversionFlags = 0 ); + void drawImage( const QPoint &, const QImage &, + const QRect &sr, int conversionFlags = 0 ); + void drawImage( const QPoint &, const QImage &, + int conversion_flags = 0 ); + void drawImage( const QRect &, const QImage & ); + void drawTiledPixmap( int x, int y, int w, int h, const QPixmap &, + int sx=0, int sy=0 ); + void drawTiledPixmap( const QRect &, const QPixmap &, + const QPoint & ); + void drawTiledPixmap( const QRect &, const QPixmap & ); +#ifndef QT_NO_PICTURE + void drawPicture( const QPicture & ); + void drawPicture( int x, int y, const QPicture & ); + void drawPicture( const QPoint &, const QPicture & ); +#endif + + void fillRect( int x, int y, int w, int h, const QBrush & ); + void fillRect( const QRect &, const QBrush & ); + void eraseRect( int x, int y, int w, int h ); + void eraseRect( const QRect & ); + + // Text drawing functions + + enum TextDirection { + Auto, + RTL, + LTR + }; + + void drawText( int x, int y, const QString &, int len = -1, TextDirection dir = Auto ); + void drawText( const QPoint &, const QString &, int len = -1, TextDirection dir = Auto ); + + void drawText( int x, int y, const QString &, int pos, int len, TextDirection dir = Auto ); + void drawText( const QPoint &p, const QString &, int pos, int len, TextDirection dir = Auto ); + + void drawText( int x, int y, int w, int h, int flags, + const QString&, int len = -1, QRect *br=0, + QTextParag **intern=0 ); + void drawText( const QRect &, int flags, + const QString&, int len = -1, QRect *br=0, + QTextParag **intern=0 ); + + void drawTextItem( int x, int y, const QTextItem &ti, int textflags = 0 ); + void drawTextItem( const QPoint& p, const QTextItem &ti, int textflags = 0 ); + + QRect boundingRect( int x, int y, int w, int h, int flags, + const QString&, int len = -1, QTextParag **intern=0 ); + QRect boundingRect( const QRect &, int flags, + const QString&, int len = -1, QTextParag **intern=0 ); + + int tabStops() const; + void setTabStops( int ); + int *tabArray() const; + void setTabArray( int * ); + + // Other functions + +#if defined(Q_WS_WIN) + HDC handle() const; +#elif defined(Q_WS_X11) || defined(Q_WS_MAC) + HANDLE handle() const; +#endif + + + static void initialize(); + static void cleanup(); + +private: + void init(); + void destroy(); + void updateFont(); + void updatePen(); + void updateBrush(); +#ifndef QT_NO_TRANSFORMATIONS + void updateXForm(); + void updateInvXForm(); +#endif + void map( int, int, int *rx, int *ry ) const; + void map( int, int, int, int, int *, int *, int *, int * ) const; + void mapInv( int, int, int *, int * ) const; + void mapInv( int, int, int, int, int *, int *, int *, int * ) const; + void drawPolyInternal( const QPointArray &, bool close=TRUE ); + void drawWinFocusRect( int x, int y, int w, int h, bool xorPaint, + const QColor &penColor ); + + enum { IsActive=0x01, ExtDev=0x02, IsStartingUp=0x04, NoCache=0x08, + VxF=0x10, WxF=0x20, ClipOn=0x40, SafePolygon=0x80, MonoDev=0x100, + DirtyFont=0x200, DirtyPen=0x400, DirtyBrush=0x800, + RGBColor=0x1000, FontMet=0x2000, FontInf=0x4000, CtorBegin=0x8000, + UsePrivateCx = 0x10000, VolatileDC = 0x20000, Qt2Compat = 0x40000 }; + uint flags; + bool testf( uint b ) const { return (flags&b)!=0; } + void setf( uint b ) { flags |= b; } + void setf( uint b, bool v ); + void clearf( uint b ) { flags &= (uint)(~b); } + void fix_neg_rect( int *x, int *y, int *w, int *h ); + + QPainterPrivate *d; + QPaintDevice *pdev; + QColor bg_col; + uchar bg_mode; + uchar rop; + uchar pu; + QPoint bro; + QFont cfont; + QFont *pfont; // font used for metrics (might be different for printers) + QPen cpen; + QBrush cbrush; + QRegion crgn; + int tabstops; + int *tabarray; + int tabarraylen; + bool block_ext; // for temporary blocking of external devices + + // Transformations +#ifndef QT_NO_TRANSFORMATIONS + QCOORD wx, wy, ww, wh; + QCOORD vx, vy, vw, vh; + QWMatrix wxmat; + + // Cached composition (and inverse) of transformations + QWMatrix xmat; + QWMatrix ixmat; + + + + double m11() const { return xmat.m11(); } + double m12() const { return xmat.m12(); } + double m21() const { return xmat.m21(); } + double m22() const { return xmat.m22(); } + double dx() const { return xmat.dx(); } + double dy() const { return xmat.dy(); } + double im11() const { return ixmat.m11(); } + double im12() const { return ixmat.m12(); } + double im21() const { return ixmat.m21(); } + double im22() const { return ixmat.m22(); } + double idx() const { return ixmat.dx(); } + double idy() const { return ixmat.dy(); } + + int txop; + bool txinv; + +#else + // even without transformations we still have translations + int xlatex; + int xlatey; +#endif + + void *penRef; // pen cache ref + void *brushRef; // brush cache ref + void *ps_stack; + void *wm_stack; + void killPStack(); + +protected: +#ifdef Q_OS_TEMP + QPoint internalCurrentPos; + uint old_pix; // ### All win platforms in 4.0 +#endif +#if defined(Q_WS_WIN) + friend class QFontEngineWin; + friend class QFontEngineBox; + QT_WIN_PAINTER_MEMBERS +#elif defined(Q_WS_X11) + friend class QFontEngineXLFD; + friend class QFontEngineXft; + friend class QFontEngineBox; + Display *dpy; // current display + int scrn; // current screen + Qt::HANDLE hd; // handle to drawable + Qt::HANDLE rendhd; // handle to Xft draw + GC gc; // graphics context (standard) + GC gc_brush; // graphics contect for brush + QPoint curPt; // current point + uint clip_serial; // clipping serial number +#elif defined(Q_WS_MAC) + Qt::HANDLE hd; // handle to drawable + void initPaintDevice(bool force=FALSE, QPoint *off=NULL, QRegion *rgn=NULL); + friend const QRegion &qt_mac_update_painter(QPainter *, bool); + friend class QFontEngineMac; + friend class QMacPainter; +#elif defined(Q_WS_QWS) + friend class QFontEngine; + QGfx * gfx; + friend void qwsUpdateActivePainters(); +#endif + friend class QFontMetrics; + friend class QFontInfo; + friend class QTextLayout; + friend void qt_format_text( const QFont &, const QRect &r, + int tf, const QString& str, int len, QRect *brect, + int tabstops, int* tabarray, int tabarraylen, + QTextParag **internal, QPainter* painter ); + friend void qt_draw_background( QPainter *p, int x, int y, int w, int h ); + friend void qt_draw_transformed_rect( QPainter *p, int x, int y, int w, int h, bool fill ); + friend class QPrinter; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QPainter( const QPainter & ); + QPainter &operator=( const QPainter & ); +#endif + + enum TransformationCodes { + TxNone = 0, // transformation codes + TxTranslate = 1, // copy in qpainter_*.cpp + TxScale = 2, + TxRotShear = 3 + }; +}; + + +/***************************************************************************** + QPainter member functions + *****************************************************************************/ + +inline QPaintDevice *QPainter::device() const +{ + return pdev; +} + +inline bool QPainter::isActive() const +{ + return testf(IsActive); +} + +inline const QFont &QPainter::font() const +{ + return cfont; +} + +inline const QPen &QPainter::pen() const +{ + return cpen; +} + +inline const QBrush &QPainter::brush() const +{ + return cbrush; +} + +/* +inline PaintUnit QPainter::unit() const +{ + return (PaintUnit)pu; +} +*/ + +inline const QColor &QPainter::backgroundColor() const +{ + return bg_col; +} + +inline Qt::BGMode QPainter::backgroundMode() const +{ + return (BGMode)bg_mode; +} + +inline Qt::RasterOp QPainter::rasterOp() const +{ + return (RasterOp)rop; +} + +inline const QPoint &QPainter::brushOrigin() const +{ + return bro; +} + +inline bool QPainter::hasViewXForm() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return testf(VxF); +#else + return xlatex || xlatey; +#endif +} + +inline bool QPainter::hasWorldXForm() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return testf(WxF); +#else + return xlatex || xlatey; +#endif +} + +inline double QPainter::translationX() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return worldMatrix().dx(); +#else + return xlatex; +#endif +} + +inline double QPainter::translationY() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return worldMatrix().dy(); +#else + return xlatey; +#endif +} + + +inline bool QPainter::hasClipping() const +{ + return testf(ClipOn); +} + +inline int QPainter::tabStops() const +{ + return tabstops; +} + +inline int *QPainter::tabArray() const +{ + return tabarray; +} + +#if defined(Q_WS_WIN) +inline HDC QPainter::handle() const +{ + return hdc; +} +#elif defined(Q_WS_X11) || defined(Q_WS_MAC) +inline Qt::HANDLE QPainter::handle() const +{ + return hd; +} +#endif + +inline void QPainter::setBrushOrigin( const QPoint &p ) +{ + setBrushOrigin( p.x(), p.y() ); +} + +#ifndef QT_NO_TRANSFORMATIONS +inline void QPainter::setWindow( const QRect &r ) +{ + setWindow( r.x(), r.y(), r.width(), r.height() ); +} + +inline void QPainter::setViewport( const QRect &r ) +{ + setViewport( r.x(), r.y(), r.width(), r.height() ); +} +#endif + +inline void QPainter::setClipRect( int x, int y, int w, int h, CoordinateMode m ) +{ + setClipRect( QRect(x,y,w,h), m ); +} + +inline void QPainter::drawPoint( const QPoint &p ) +{ + drawPoint( p.x(), p.y() ); +} + +inline void QPainter::moveTo( const QPoint &p ) +{ + moveTo( p.x(), p.y() ); +} + +inline void QPainter::lineTo( const QPoint &p ) +{ + lineTo( p.x(), p.y() ); +} + +inline void QPainter::drawLine( const QPoint &p1, const QPoint &p2 ) +{ + drawLine( p1.x(), p1.y(), p2.x(), p2.y() ); +} + +inline void QPainter::drawRect( const QRect &r ) +{ + drawRect( r.x(), r.y(), r.width(), r.height() ); +} + +inline void QPainter::drawWinFocusRect( const QRect &r ) +{ + drawWinFocusRect( r.x(), r.y(), r.width(), r.height() ); +} + +inline void QPainter::drawWinFocusRect( const QRect &r,const QColor &penColor ) +{ + drawWinFocusRect( r.x(), r.y(), r.width(), r.height(), penColor ); +} + +inline void QPainter::drawRoundRect( const QRect &r, int xRnd, int yRnd ) +{ + drawRoundRect( r.x(), r.y(), r.width(), r.height(), xRnd, yRnd ); +} + +inline void QPainter::drawEllipse( const QRect &r ) +{ + drawEllipse( r.x(), r.y(), r.width(), r.height() ); +} + +inline void QPainter::drawArc( const QRect &r, int a, int alen ) +{ + drawArc( r.x(), r.y(), r.width(), r.height(), a, alen ); +} + +inline void QPainter::drawPie( const QRect &r, int a, int alen ) +{ + drawPie( r.x(), r.y(), r.width(), r.height(), a, alen ); +} + +inline void QPainter::drawChord( const QRect &r, int a, int alen ) +{ + drawChord( r.x(), r.y(), r.width(), r.height(), a, alen ); +} + +inline void QPainter::drawPixmap( const QPoint &p, const QPixmap &pm, + const QRect &sr ) +{ + drawPixmap( p.x(), p.y(), pm, sr.x(), sr.y(), sr.width(), sr.height() ); +} + +inline void QPainter::drawImage( const QPoint &p, const QImage &pm, + const QRect &sr, int conversionFlags ) +{ + drawImage( p.x(), p.y(), pm, + sr.x(), sr.y(), sr.width(), sr.height(), conversionFlags ); +} + +inline void QPainter::drawTiledPixmap( const QRect &r, const QPixmap &pm, + const QPoint &sp ) +{ + drawTiledPixmap( r.x(), r.y(), r.width(), r.height(), pm, sp.x(), sp.y() ); +} + +inline void QPainter::drawTiledPixmap( const QRect &r, const QPixmap &pm ) +{ + drawTiledPixmap( r.x(), r.y(), r.width(), r.height(), pm, 0, 0 ); +} + +inline void QPainter::fillRect( const QRect &r, const QBrush &brush ) +{ + fillRect( r.x(), r.y(), r.width(), r.height(), brush ); +} + +inline void QPainter::eraseRect( int x, int y, int w, int h ) +{ + fillRect( x, y, w, h, backgroundColor() ); +} + +inline void QPainter::eraseRect( const QRect &r ) +{ + fillRect( r.x(), r.y(), r.width(), r.height(), backgroundColor() ); +} + +inline void QPainter::drawText( const QPoint &p, const QString &s, int len, TextDirection dir ) +{ + drawText( p.x(), p.y(), s, 0, len, dir ); +} + +inline void QPainter::drawText( const QPoint &p, const QString &s, int pos, int len, TextDirection dir ) +{ + drawText( p.x(), p.y(), s, pos, len, dir ); +} + +inline void QPainter::drawText( int x, int y, int w, int h, int tf, + const QString& str, int len, QRect *br, QTextParag **i ) +{ + QRect r(x, y, w, h); + drawText( r, tf, str, len, br, i ); +} + +inline void QPainter::drawTextItem( const QPoint& p, const QTextItem &ti, int textflags ) +{ + drawTextItem( p.x(), p.y(), ti, textflags ); +} + +inline QRect QPainter::boundingRect( int x, int y, int w, int h, int tf, + const QString& str, int len, QTextParag **i ) +{ + QRect r(x, y, w, h); + return boundingRect( r, tf, str, len, i ); +} + +#if defined(Q_WS_QWS) +inline QGfx * QPainter::internalGfx() +{ + return gfx; +} +#endif + +#endif // QPAINTER_H diff --git a/src/kernel/qpainter_p.h b/src/kernel/qpainter_p.h new file mode 100644 index 0000000..caec428 --- /dev/null +++ b/src/kernel/qpainter_p.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Definition of some Qt private functions. +** +** Created : 000909 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPAINTER_P_H +#define QPAINTER_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qpainter.cpp and qfont.cpp. This header file may change +// from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +extern void qt_format_text( const QFont& f, const QRect &r, + int tf, const QString& str, int len, QRect *brect, + int tabstops, int* tabarray, int tabarraylen, + QTextParag **internal, QPainter* painter ); + + +#endif diff --git a/src/kernel/qpainter_x11.cpp b/src/kernel/qpainter_x11.cpp new file mode 100644 index 0000000..206bffc --- /dev/null +++ b/src/kernel/qpainter_x11.cpp @@ -0,0 +1,3153 @@ +/**************************************************************************** +** +** Implementation of QPainter class for X11 +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +#include "qfont.h" +#include "qpainter.h" +#include "qwidget.h" +#include "qbitmap.h" +#include "qpixmapcache.h" +#include "qtextcodec.h" +#include "qpaintdevicemetrics.h" + +#include "qt_x11_p.h" + +#include "qtextlayout_p.h" +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qtextengine_p.h" + +#include <math.h> + +// paintevent magic to provide Windows semantics on X11 +static QRegion* paintEventClipRegion = 0; +static QPaintDevice* paintEventDevice = 0; + +void qt_set_paintevent_clipping( QPaintDevice* dev, const QRegion& region) +{ + if ( !paintEventClipRegion ) + paintEventClipRegion = new QRegion( region ); + else + *paintEventClipRegion = region; + paintEventDevice = dev; +} + +void qt_clear_paintevent_clipping() +{ + delete paintEventClipRegion; + paintEventClipRegion = 0; + paintEventDevice = 0; +} + +class QWFlagWidget : public QWidget +{ +public: + void setWState( WFlags f ) { QWidget::setWState(f); } + void clearWState( WFlags f ) { QWidget::clearWState(f); } + void setWFlags( WFlags f ) { QWidget::setWFlags(f); } + void clearWFlags( WFlags f ) { QWidget::clearWFlags(f); } +}; + +void qt_erase_region( QWidget* w, const QRegion& region) +{ + QRegion reg = region; + + if ( QPainter::redirect(w) || (!w->isTopLevel() && w->backgroundPixmap() + && w->backgroundOrigin() != QWidget::WidgetOrigin) ) { + QPoint offset = w->backgroundOffset(); + int ox = offset.x(); + int oy = offset.y(); + + bool unclipped = w->testWFlags( Qt::WPaintUnclipped ); + if ( unclipped ) + ((QWFlagWidget*)w)->clearWFlags( Qt::WPaintUnclipped ); + QPainter p( w ); + p.setClipRegion( region ); // automatically includes paintEventDevice if required + if ( w->backgroundPixmap() ) + p.drawTiledPixmap( 0, 0, w->width(), w->height(), + *w->backgroundPixmap(), ox, oy ); + else + p.fillRect( w->rect(), w->eraseColor() ); + if ( unclipped ) + ((QWFlagWidget*)w)->setWFlags( Qt::WPaintUnclipped ); + return; + } + + if ( w == paintEventDevice && paintEventClipRegion ) + reg = paintEventClipRegion->intersect( reg ); + + QMemArray<QRect> r = reg.rects(); + for (uint i=0; i<r.size(); i++) { + const QRect& rr = r[(int)i]; + XClearArea( w->x11Display(), w->winId(), + rr.x(), rr.y(), rr.width(), rr.height(), False ); + } +} + +void qt_erase_rect( QWidget* w, const QRect& r) +{ + if ( QPainter::redirect(w) || w == paintEventDevice + || w->backgroundOrigin() != QWidget::WidgetOrigin ) + qt_erase_region( w, r ); + else + XClearArea( w->x11Display(), w->winId(), r.x(), r.y(), r.width(), r.height(), False ); + +} + +#ifdef QT_NO_XFTFREETYPE +static const Qt::HANDLE rendhd = 0; +#endif + +// hack, so we don't have to make QRegion::clipRectangles() public or include +// X11 headers in qregion.h +inline void *qt_getClipRects( const QRegion &r, int &num ) +{ + return r.clipRectangles( num ); +} + +static inline void x11SetClipRegion(Display *dpy, GC gc, GC gc2, Qt::HANDLE draw, const QRegion &r) +{ + int num; + XRectangle *rects = (XRectangle *)qt_getClipRects( r, num ); + + if (gc) + XSetClipRectangles( dpy, gc, 0, 0, rects, num, YXBanded ); + if (gc2) + XSetClipRectangles( dpy, gc2, 0, 0, rects, num, YXBanded ); + +#ifndef QT_NO_XFTFREETYPE + if (draw) + XftDrawSetClipRectangles((XftDraw *) draw, 0, 0, rects, num); +#else + Q_UNUSED(draw); +#endif // QT_NO_XFTFREETYPE +} + +static inline void x11ClearClipRegion(Display *dpy, GC gc, GC gc2, Qt::HANDLE draw) +{ + if (gc) + XSetClipMask(dpy, gc, None); + if (gc2) + XSetClipMask(dpy, gc2, None); + +#ifndef QT_NO_XFTFREETYPE + if (draw) { +# ifdef QT_XFT2 + XftDrawSetClip((XftDraw *) draw, None); +# else + // stupid Xft1 + Picture pict = XftDrawPicture((XftDraw *) draw); + XRenderPictureAttributes pattr; + pattr.clip_mask = None; + XRenderChangePicture(dpy, pict, CPClipMask, &pattr); +# endif // QT_XFT2 + } +#else + Q_UNUSED(draw); +#endif // QT_NO_XFTFREETYPE +} + + +/***************************************************************************** + Trigonometric function for QPainter + + We have implemented simple sine and cosine function that are called from + QPainter::drawPie() and QPainter::drawChord() when drawing the outline of + pies and chords. + These functions are slower and less accurate than math.h sin() and cos(), + but with still around 1/70000th sec. execution time (on a 486DX2-66) and + 8 digits accuracy, it should not be the bottleneck in drawing these shapes. + The advantage is that you don't have to link in the math library. + *****************************************************************************/ + +const double Q_PI = 3.14159265358979323846; // pi +const double Q_2PI = 6.28318530717958647693; // 2*pi +const double Q_PI2 = 1.57079632679489661923; // pi/2 + + +#if defined(Q_CC_GNU) && defined(Q_OS_AIX) +// AIX 4.2 gcc 2.7.2.3 gets internal error. +static int qRoundAIX( double d ) +{ + return qRound(d); +} +#define qRound qRoundAIX +#endif + + +#if defined(Q_CC_GNU) && defined(__i386__) + +inline double qcos( double a ) +{ + double r; + __asm__ ( + "fcos" + : "=t" (r) : "0" (a) ); + return(r); +} + +inline double qsin( double a ) +{ + double r; + __asm__ ( + "fsin" + : "=t" (r) : "0" (a) ); + return(r); +} + +double qsincos( double a, bool calcCos=FALSE ) +{ + return calcCos ? qcos(a) : qsin(a); +} + +#else + +double qsincos( double a, bool calcCos=FALSE ) +{ + if ( calcCos ) // calculate cosine + a -= Q_PI2; + if ( a >= Q_2PI || a <= -Q_2PI ) { // fix range: -2*pi < a < 2*pi + int m = (int)(a/Q_2PI); + a -= Q_2PI*m; + } + if ( a < 0.0 ) // 0 <= a < 2*pi + a += Q_2PI; + int sign = a > Q_PI ? -1 : 1; + if ( a >= Q_PI ) + a = Q_2PI - a; + if ( a >= Q_PI2 ) + a = Q_PI - a; + if ( calcCos ) + sign = -sign; + double a2 = a*a; // here: 0 <= a < pi/4 + double a3 = a2*a; // make taylor sin sum + double a5 = a3*a2; + double a7 = a5*a2; + double a9 = a7*a2; + double a11 = a9*a2; + return (a-a3/6+a5/120-a7/5040+a9/362880-a11/39916800)*sign; +} + +inline double qsin( double a ) { return qsincos(a, FALSE); } +inline double qcos( double a ) { return qsincos(a, TRUE); } + +#endif + + +/***************************************************************************** + QPainter internal GC (Graphics Context) allocator. + + The GC allocator offers two functions; alloc_gc() and free_gc() that + reuse GC objects instead of calling XCreateGC() and XFreeGC(), which + are a whole lot slower. + *****************************************************************************/ + +struct QGC +{ + GC gc; + char in_use; + bool mono; + int scrn; +}; + +const int gc_array_size = 256; +static QGC gc_array[gc_array_size]; // array of GCs +static bool gc_array_init = FALSE; + + +static void init_gc_array() +{ + if ( !gc_array_init ) { + memset( gc_array, 0, gc_array_size*sizeof(QGC) ); + gc_array_init = TRUE; + } +} + +static void cleanup_gc_array( Display *dpy ) +{ + register QGC *p = gc_array; + int i = gc_array_size; + if ( gc_array_init ) { + while ( i-- ) { + if ( p->gc ) // destroy GC + XFreeGC( dpy, p->gc ); + p++; + } + gc_array_init = FALSE; + } +} + +// #define DONT_USE_GC_ARRAY + +static GC alloc_gc( Display *dpy, int scrn, Drawable hd, bool monochrome=FALSE, + bool privateGC = FALSE ) +{ +#if defined(DONT_USE_GC_ARRAY) + privateGC = TRUE; // will be slower +#endif + if ( privateGC ) { + GC gc = XCreateGC( dpy, hd, 0, 0 ); + XSetGraphicsExposures( dpy, gc, False ); + return gc; + } + register QGC *p = gc_array; + int i = gc_array_size; + if ( !gc_array_init ) // not initialized + init_gc_array(); + while ( i-- ) { + if ( !p->gc ) { // create GC (once) + p->gc = XCreateGC( dpy, hd, 0, 0 ); + p->scrn = scrn; + XSetGraphicsExposures( dpy, p->gc, False ); + p->in_use = FALSE; + p->mono = monochrome; + } + if ( !p->in_use && p->mono == monochrome && p->scrn == scrn ) { + p->in_use = TRUE; // available/compatible GC + return p->gc; + } + p++; + } +#if defined(QT_CHECK_NULL) + qWarning( "QPainter: Internal error; no available GC" ); +#endif + GC gc = XCreateGC( dpy, hd, 0, 0 ); + XSetGraphicsExposures( dpy, gc, False ); + return gc; +} + +static void free_gc( Display *dpy, GC gc, bool privateGC = FALSE ) +{ +#if defined(DONT_USE_GC_ARRAY) + privateGC = TRUE; // will be slower +#endif + if ( privateGC ) { + Q_ASSERT( dpy != 0 ); + XFreeGC( dpy, gc ); + return; + } + register QGC *p = gc_array; + int i = gc_array_size; + if ( gc_array_init ) { + while ( i-- ) { + if ( p->gc == gc ) { + p->in_use = FALSE; // set available + XSetClipMask( dpy, gc, None ); // make it reusable + XSetFunction( dpy, gc, GXcopy ); + XSetFillStyle( dpy, gc, FillSolid ); + XSetTSOrigin( dpy, gc, 0, 0 ); + return; + } + p++; + } + } + + // not found in gc_array + XFreeGC(dpy, gc); +} + + +/***************************************************************************** + QPainter internal GC (Graphics Context) cache for solid pens and + brushes. + + The GC cache makes a significant contribution to speeding up + drawing. Setting new pen and brush colors will make the painter + look for another GC with the same color instead of changing the + color value of the GC currently in use. The cache structure is + optimized for fast lookup. Only solid line pens with line width 0 + and solid brushes are cached. + + In addition, stored GCs may have an implicit clipping region + set. This prevents any drawing outside paint events. Both + updatePen() and updateBrush() keep track of the validity of this + clipping region by storing the clip_serial number in the cache. + +*****************************************************************************/ + +struct QGCC // cached GC +{ + GC gc; + uint pix; + int count; + int hits; + uint clip_serial; + int scrn; +}; + +const int gc_cache_size = 29; // multiply by 4 +static QGCC *gc_cache_buf; +static QGCC *gc_cache[4*gc_cache_size]; +static bool gc_cache_init = FALSE; +static uint gc_cache_clip_serial = 0; + + +static void init_gc_cache() +{ + if ( !gc_cache_init ) { + gc_cache_init = TRUE; + gc_cache_clip_serial = 0; + QGCC *g = gc_cache_buf = new QGCC[4*gc_cache_size]; + memset( g, 0, 4*gc_cache_size*sizeof(QGCC) ); + for ( int i=0; i<4*gc_cache_size; i++ ) + gc_cache[i] = g++; + } +} + + +// #define GC_CACHE_STAT +#if defined(GC_CACHE_STAT) +#include "qtextstream.h" +#include "qbuffer.h" + +static int g_numhits = 0; +static int g_numcreates = 0; +static int g_numfaults = 0; +#endif + + +static void cleanup_gc_cache() +{ + if ( !gc_cache_init ) + return; +#if defined(GC_CACHE_STAT) + qDebug( "Number of cache hits = %d", g_numhits ); + qDebug( "Number of cache creates = %d", g_numcreates ); + qDebug( "Number of cache faults = %d", g_numfaults ); + for ( int i=0; i<gc_cache_size; i++ ) { + QCString str; + QBuffer buf( str ); + buf.open(IO_ReadWrite); + QTextStream s(&buf); + s << i << ": "; + for ( int j=0; j<4; j++ ) { + QGCC *g = gc_cache[i*4+j]; + s << (g->gc ? 'X' : '-') << ',' << g->hits << ',' + << g->count << '\t'; + } + s << '\0'; + qDebug( str ); + buf.close(); + } +#endif + delete [] gc_cache_buf; + gc_cache_init = FALSE; +} + + +static bool obtain_gc( void **ref, GC *gc, uint pix, Display *dpy, int scrn, + Qt::HANDLE hd, uint painter_clip_serial ) +{ + if ( !gc_cache_init ) + init_gc_cache(); + + int k = (pix % gc_cache_size) * 4; + QGCC *g = gc_cache[k]; + QGCC *prev = 0; + +#define NOMATCH (g->gc && (g->pix != pix || g->scrn != scrn || \ + (g->clip_serial > 0 && g->clip_serial != painter_clip_serial))) + + if ( NOMATCH ) { + prev = g; + g = gc_cache[++k]; + if ( NOMATCH ) { + prev = g; + g = gc_cache[++k]; + if ( NOMATCH ) { + prev = g; + g = gc_cache[++k]; + if ( NOMATCH ) { + if ( g->count == 0 && g->scrn == scrn) { // steal this GC + g->pix = pix; + g->count = 1; + g->hits = 1; + g->clip_serial = 0; + XSetForeground( dpy, g->gc, pix ); + XSetClipMask(dpy, g->gc, None); + gc_cache[k] = prev; + gc_cache[k-1] = g; + *ref = (void *)g; + *gc = g->gc; + return TRUE; + } else { // all GCs in use +#if defined(GC_CACHE_STAT) + g_numfaults++; +#endif + *ref = 0; + return FALSE; + } + } + } + } + } + +#undef NOMATCH + + *ref = (void *)g; + + if ( g->gc ) { // reuse existing GC +#if defined(GC_CACHE_STAT) + g_numhits++; +#endif + *gc = g->gc; + g->count++; + g->hits++; + if ( prev && g->hits > prev->hits ) { // maintain LRU order + gc_cache[k] = prev; + gc_cache[k-1] = g; + } + return TRUE; + } else { // create new GC +#if defined(GC_CACHE_STAT) + g_numcreates++; +#endif + g->gc = alloc_gc( dpy, scrn, hd, FALSE ); + g->scrn = scrn; + g->pix = pix; + g->count = 1; + g->hits = 1; + g->clip_serial = 0; + *gc = g->gc; + return FALSE; + } +} + +static inline void release_gc( void *ref ) +{ + ((QGCC*)ref)->count--; +} + +/***************************************************************************** + QPainter member functions + *****************************************************************************/ + +/*! + \internal + + Internal function that initializes the painter. +*/ + +void QPainter::initialize() +{ + init_gc_array(); + init_gc_cache(); +} + +/*! + \internal + + Internal function that cleans up the painter. +*/ + +void QPainter::cleanup() +{ + cleanup_gc_cache(); + cleanup_gc_array( QPaintDevice::x11AppDisplay() ); + QPointArray::cleanBuffers(); +} + +/*! + \internal + + Internal function that destroys up the painter. +*/ + +void QPainter::destroy() +{ + +} + +void QPainter::init() +{ + d = 0; + flags = IsStartingUp; + bg_col = white; // default background color + bg_mode = TransparentMode; // default background mode + rop = CopyROP; // default ROP + tabstops = 0; // default tabbing + tabarray = 0; + tabarraylen = 0; + ps_stack = 0; + wm_stack = 0; + gc = gc_brush = 0; + pdev = 0; + dpy = 0; + txop = txinv = 0; + penRef = brushRef = 0; + clip_serial = 0; + pfont = 0; + block_ext = FALSE; +} + + +/*! + \fn const QFont &QPainter::font() const + + Returns the currently set painter font. + + \sa setFont(), QFont +*/ + +/*! + Sets the painter's font to \a font. + + This font is used by subsequent drawText() functions. The text + color is the same as the pen color. + + \sa font(), drawText() +*/ + +void QPainter::setFont( const QFont &font ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setFont: Will be reset by begin()" ); +#endif + if ( cfont.d != font.d ) { + cfont = font; + cfont.x11SetScreen( scrn ); + setf(DirtyFont); + } +} + + +void QPainter::updateFont() +{ + if (!isActive()) + return; + + clearf(DirtyFont); + if ( testf(ExtDev) ) { + if (pdev->devType() == QInternal::Printer) { + if ( pfont ) delete pfont; + pfont = new QFont( cfont.d, pdev ); + } + QPDevCmdParam param[1]; + param[0].font = &cfont; + if ( !pdev->cmd( QPaintDevice::PdcSetFont, this, param ) || !hd ) + return; + } + setf(NoCache); + if ( penRef ) + updatePen(); // force a non-cached GC +} + + +void QPainter::updatePen() +{ + if (!isActive()) + return; + + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].pen = &cpen; + if ( !pdev->cmd( QPaintDevice::PdcSetPen, this, param ) || !hd ) + return; + } + + int ps = cpen.style(); + bool cacheIt = !testf(ClipOn|MonoDev|NoCache) && + (ps == NoPen || ps == SolidLine) && + cpen.width() == 0 && rop == CopyROP; + + bool obtained = FALSE; + bool internclipok = hasClipping(); + if ( cacheIt ) { + if ( gc ) { + if ( penRef ) + release_gc( penRef ); + else + free_gc( dpy, gc ); + } + obtained = obtain_gc(&penRef, &gc, cpen.color().pixel(scrn), dpy, scrn, + hd, clip_serial); + if ( !obtained && !penRef ) + gc = alloc_gc( dpy, scrn, hd, FALSE ); + } else { + if ( gc ) { + if ( penRef ) { + release_gc( penRef ); + penRef = 0; + gc = alloc_gc( dpy, scrn, hd, testf(MonoDev) ); + } else { + internclipok = TRUE; + } + } else { + gc = alloc_gc( dpy, scrn, hd, testf(MonoDev), testf(UsePrivateCx) ); + } + } + + if ( !internclipok ) { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + if ( penRef &&((QGCC*)penRef)->clip_serial < gc_cache_clip_serial ) { + x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion ); + ((QGCC*)penRef)->clip_serial = gc_cache_clip_serial; + } else if ( !penRef ) { + x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion ); + } + } else if (penRef && ((QGCC*)penRef)->clip_serial ) { + x11ClearClipRegion(dpy, gc, 0, rendhd); + ((QGCC*)penRef)->clip_serial = 0; + } + } + + if ( obtained ) + return; + + char dashes[10]; // custom pen dashes + int dash_len = 0; // length of dash list + int s = LineSolid; + int cp = CapButt; + int jn = JoinMiter; + + /* + We are emulating Windows here. Windows treats cpen.width() == 1 + (or 0) as a very special case. The fudge variable unifies this + case with the general case. + */ + int dot = cpen.width(); // width of a dot + int fudge = 1; + bool allow_zero_lw = TRUE; + if ( dot <= 1 ) { + dot = 3; + fudge = 2; + } + + switch( ps ) { + case NoPen: + case SolidLine: + s = LineSolid; + break; + case DashLine: + dashes[0] = fudge * 3 * dot; + dashes[1] = fudge * dot; + dash_len = 2; + allow_zero_lw = FALSE; + break; + case DotLine: + dashes[0] = dot; + dashes[1] = dot; + dash_len = 2; + allow_zero_lw = FALSE; + break; + case DashDotLine: + dashes[0] = 3 * dot; + dashes[1] = fudge * dot; + dashes[2] = dot; + dashes[3] = fudge * dot; + dash_len = 4; + allow_zero_lw = FALSE; + break; + case DashDotDotLine: + dashes[0] = 3 * dot; + dashes[1] = dot; + dashes[2] = dot; + dashes[3] = dot; + dashes[4] = dot; + dashes[5] = dot; + dash_len = 6; + allow_zero_lw = FALSE; + } + Q_ASSERT( dash_len <= (int) sizeof(dashes) ); + + switch ( cpen.capStyle() ) { + case SquareCap: + cp = CapProjecting; + break; + case RoundCap: + cp = CapRound; + break; + case FlatCap: + default: + cp = CapButt; + break; + } + switch ( cpen.joinStyle() ) { + case BevelJoin: + jn = JoinBevel; + break; + case RoundJoin: + jn = JoinRound; + break; + case MiterJoin: + default: + jn = JoinMiter; + break; + } + + XSetForeground( dpy, gc, cpen.color().pixel(scrn) ); + XSetBackground( dpy, gc, bg_col.pixel(scrn) ); + + if ( dash_len ) { // make dash list + XSetDashes( dpy, gc, 0, dashes, dash_len ); + s = bg_mode == TransparentMode ? LineOnOffDash : LineDoubleDash; + } + XSetLineAttributes( dpy, gc, + (! allow_zero_lw && cpen.width() == 0) ? 1 : cpen.width(), + s, cp, jn ); +} + + +void QPainter::updateBrush() +{ + if (!isActive()) + return; + + static const uchar dense1_pat[] = { 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff }; + static const uchar dense2_pat[] = { 0x77, 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff }; + static const uchar dense3_pat[] = { 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55, 0xee }; + static const uchar dense4_pat[] = { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa }; + static const uchar dense5_pat[] = { 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11 }; + static const uchar dense6_pat[] = { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 }; + static const uchar dense7_pat[] = { 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00 }; + static const uchar hor_pat[] = { // horizontal pattern + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar ver_pat[] = { // vertical pattern + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20 }; + static const uchar cross_pat[] = { // cross pattern + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20 }; + static const uchar bdiag_pat[] = { // backward diagonal pattern + 0x20, 0x20, 0x10, 0x10, 0x08, 0x08, 0x04, 0x04, 0x02, 0x02, 0x01, 0x01, + 0x80, 0x80, 0x40, 0x40, 0x20, 0x20, 0x10, 0x10, 0x08, 0x08, 0x04, 0x04, + 0x02, 0x02, 0x01, 0x01, 0x80, 0x80, 0x40, 0x40 }; + static const uchar fdiag_pat[] = { // forward diagonal pattern + 0x02, 0x02, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, 0x20, 0x20, 0x40, 0x40, + 0x80, 0x80, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, + 0x20, 0x20, 0x40, 0x40, 0x80, 0x80, 0x01, 0x01 }; + static const uchar dcross_pat[] = { // diagonal cross pattern + 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x14, 0x14, 0x22, 0x22, 0x41, 0x41, + 0x80, 0x80, 0x41, 0x41, 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x14, 0x14, + 0x22, 0x22, 0x41, 0x41, 0x80, 0x80, 0x41, 0x41 }; + static const uchar * const pat_tbl[] = { + dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat, + dense6_pat, dense7_pat, + hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat }; + + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].brush = &cbrush; + if ( !pdev->cmd( QPaintDevice::PdcSetBrush, this, param ) || !hd ) + return; + } + + int bs = cbrush.style(); + bool cacheIt = !testf(ClipOn|MonoDev|NoCache) && + (bs == NoBrush || bs == SolidPattern) && + bro.x() == 0 && bro.y() == 0 && rop == CopyROP; + + bool obtained = FALSE; + bool internclipok = hasClipping(); + if ( cacheIt ) { + if ( gc_brush ) { + if ( brushRef ) + release_gc( brushRef ); + else + free_gc( dpy, gc_brush ); + } + obtained = obtain_gc(&brushRef, &gc_brush, cbrush.color().pixel(scrn), dpy, + scrn, hd, clip_serial); + if ( !obtained && !brushRef ) + gc_brush = alloc_gc( dpy, scrn, hd, FALSE ); + } else { + if ( gc_brush ) { + if ( brushRef ) { + release_gc( brushRef ); + brushRef = 0; + gc_brush = alloc_gc( dpy, scrn, hd, testf(MonoDev) ); + } else { + internclipok = TRUE; + } + } else { + gc_brush = alloc_gc( dpy, scrn, hd, testf(MonoDev), testf(UsePrivateCx)); + } + } + + if ( !internclipok ) { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + if ( brushRef &&((QGCC*)brushRef)->clip_serial < gc_cache_clip_serial ) { + x11SetClipRegion( dpy, gc_brush, 0, rendhd, *paintEventClipRegion ); + ((QGCC*)brushRef)->clip_serial = gc_cache_clip_serial; + } else if ( !brushRef ){ + x11SetClipRegion( dpy, gc_brush, 0, rendhd, *paintEventClipRegion ); + } + } else if (brushRef && ((QGCC*)brushRef)->clip_serial ) { + x11ClearClipRegion(dpy, gc_brush, 0, rendhd); + ((QGCC*)brushRef)->clip_serial = 0; + } + } + + if ( obtained ) + return; + + const uchar *pat = 0; // pattern + int d = 0; // defalt pattern size: d*d + int s = FillSolid; + if ( bs >= Dense1Pattern && bs <= DiagCrossPattern ) { + pat = pat_tbl[ bs-Dense1Pattern ]; + if ( bs <= Dense7Pattern ) + d = 8; + else if ( bs <= CrossPattern ) + d = 24; + else + d = 16; + } + + XSetLineAttributes( dpy, gc_brush, 0, LineSolid, CapButt, JoinMiter ); + XSetForeground( dpy, gc_brush, cbrush.color().pixel(scrn) ); + XSetBackground( dpy, gc_brush, bg_col.pixel(scrn) ); + + if ( bs == CustomPattern || pat ) { + QPixmap *pm; + if ( pat ) { + QString key; + key.sprintf( "$qt-brush$%d", bs ); + pm = QPixmapCache::find( key ); + bool del = FALSE; + if ( !pm ) { // not already in pm dict + pm = new QBitmap( d, d, pat, TRUE ); + Q_CHECK_PTR( pm ); + del = !QPixmapCache::insert( key, pm ); + } + if ( cbrush.data->pixmap ) + delete cbrush.data->pixmap; + cbrush.data->pixmap = new QPixmap( *pm ); + if (del) delete pm; + } + pm = cbrush.data->pixmap; + pm->x11SetScreen( scrn ); + if ( pm->depth() == 1 ) { + XSetStipple( dpy, gc_brush, pm->handle() ); + s = bg_mode == TransparentMode ? FillStippled : FillOpaqueStippled; + } else { + XSetTile( dpy, gc_brush, pm->handle() ); + s = FillTiled; + } + } + XSetFillStyle( dpy, gc_brush, s ); +} + + +/*! + Begins painting the paint device \a pd and returns TRUE if + successful; otherwise returns FALSE. If \a unclipped is TRUE, the + painting will not be clipped at the paint device's boundaries, + (although this is not supported by all platforms). + + The errors that can occur are serious problems, such as these: + + \code + p->begin( 0 ); // impossible - paint device cannot be 0 + + QPixmap pm( 0, 0 ); + p->begin( pm ); // impossible - pm.isNull(); + + p->begin( myWidget ); + p2->begin( myWidget ); // impossible - only one painter at a time + \endcode + + Note that most of the time, you can use one of the constructors + instead of begin(), and that end() is automatically done at + destruction. + + \warning A paint device can only be painted by one painter at a + time. + + \sa end(), flush() +*/ + +bool QPainter::begin( const QPaintDevice *pd, bool unclipped ) +{ + if ( isActive() ) { // already active painting +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::begin: Painter is already active." + "\n\tYou must end() the painter before a second begin()" ); +#endif + return FALSE; + } + if ( pd == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "QPainter::begin: Paint device cannot be null" ); +#endif + return FALSE; + } + + QPixmap::x11SetDefaultScreen( pd->x11Screen() ); + + const QWidget *copyFrom = 0; + pdev = redirect( (QPaintDevice*)pd ); + if ( pdev ) { // redirected paint device? + if ( pd->devType() == QInternal::Widget ) + copyFrom = (const QWidget *)pd; // copy widget settings + } else { + pdev = (QPaintDevice*)pd; + } + + if ( pdev->isExtDev() && pdev->paintingActive() ) { + // somebody else is already painting +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::begin: Another QPainter is already painting " + "this device;\n\tAn extended paint device can only be " + "painted by one QPainter at a time." ); +#endif + return FALSE; + } + + bool reinit = flags != IsStartingUp; // 2nd or 3rd etc. time called + flags = IsActive | DirtyFont; // init flags + int dt = pdev->devType(); // get the device type + + if ( (pdev->devFlags & QInternal::ExternalDevice) != 0 ) + setf(ExtDev); + else if ( dt == QInternal::Pixmap ) // device is a pixmap + ((QPixmap*)pdev)->detach(); // will modify it + + dpy = pdev->x11Display(); // get display variable + scrn = pdev->x11Screen(); // get screen variable + hd = pdev->handle(); // get handle to drawable + rendhd = pdev->rendhd; + + if ( testf(ExtDev) ) { // external device + if ( !pdev->cmd( QPaintDevice::PdcBegin, this, 0 ) ) { + // could not begin painting + if ( reinit ) + clearf( IsActive | DirtyFont ); + else + flags = IsStartingUp; + pdev = 0; + return FALSE; + } + if ( tabstops ) // update tabstops for device + setTabStops( tabstops ); + if ( tabarray ) // update tabarray for device + setTabArray( tabarray ); + } + + if ( pdev->x11Depth() != pdev->x11AppDepth( scrn ) ) { // non-standard depth + setf(NoCache); + setf(UsePrivateCx); + } + + pdev->painters++; // also tell paint device + bro = curPt = QPoint( 0, 0 ); + if ( reinit ) { + bg_mode = TransparentMode; // default background mode + rop = CopyROP; // default ROP + wxmat.reset(); // reset world xform matrix + xmat.reset(); + ixmat.reset(); + txop = txinv = 0; + if ( dt != QInternal::Widget ) { + QFont defaultFont; // default drawing tools + QPen defaultPen; + QBrush defaultBrush; + cfont = defaultFont; // set these drawing tools + cpen = defaultPen; + cbrush = defaultBrush; + bg_col = white; // default background color + } + } + wx = wy = vx = vy = 0; // default view origins + + if ( dt == QInternal::Widget ) { // device is a widget + QWidget *w = (QWidget*)pdev; + cfont = w->font(); // use widget font + cpen = QPen( w->foregroundColor() ); // use widget fg color + if ( reinit ) { + QBrush defaultBrush; + cbrush = defaultBrush; + } + bg_col = w->backgroundColor(); // use widget bg color + ww = vw = w->width(); // default view size + wh = vh = w->height(); + if ( unclipped || w->testWFlags( WPaintUnclipped ) ) { // paint direct on device + setf( NoCache ); + setf(UsePrivateCx); + updatePen(); + updateBrush(); + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XSetSubwindowMode( dpy, gc_brush, IncludeInferiors ); +#ifndef QT_NO_XFTFREETYPE + if (rendhd) + XftDrawSetSubwindowMode((XftDraw *) rendhd, IncludeInferiors); +#endif + } + } else if ( dt == QInternal::Pixmap ) { // device is a pixmap + QPixmap *pm = (QPixmap*)pdev; + if ( pm->isNull() ) { +#if defined(QT_CHECK_NULL) + qWarning( "QPainter::begin: Cannot paint null pixmap" ); +#endif + end(); + return FALSE; + } + bool mono = pm->depth() == 1; // monochrome bitmap + if ( mono ) { + setf( MonoDev ); + bg_col = color0; + cpen.setColor( color1 ); + } + ww = vw = pm->width(); // default view size + wh = vh = pm->height(); + } else if ( testf(ExtDev) ) { // external device + ww = vw = pdev->metric( QPaintDeviceMetrics::PdmWidth ); + wh = vh = pdev->metric( QPaintDeviceMetrics::PdmHeight ); + } + if ( ww == 0 ) + ww = wh = vw = vh = 1024; + if ( copyFrom ) { // copy redirected widget + cfont = copyFrom->font(); + cpen = QPen( copyFrom->foregroundColor() ); + bg_col = copyFrom->backgroundColor(); + } + if ( testf(ExtDev) ) { // external device + setBackgroundColor( bg_col ); // default background color + setBackgroundMode( TransparentMode ); // default background mode + setRasterOp( CopyROP ); // default raster operation + } + clip_serial = gc_cache_clip_serial++; + updateBrush(); + updatePen(); + return TRUE; +} + +/*! + Ends painting. Any resources used while painting are released. + + Note that while you mostly don't need to call end(), the + destructor will do it, there is at least one common case when it + is needed, namely double buffering. + + \code + QPainter p( myPixmap, this ) + // ... + p.end(); // stops drawing on myPixmap + p.begin( this ); + p.drawPixmap( 0, 0, myPixmap ); + \endcode + + Since you can't draw a QPixmap while it is being painted, it is + necessary to close the active painter. + + \sa begin(), isActive() +*/ + +bool QPainter::end() // end painting +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::end: Missing begin() or begin() failed" ); +#endif + return FALSE; + } + killPStack(); + + //#### This should not be necessary: + if ( pdev->devType() == QInternal::Widget && // ##### + ((QWidget*)pdev)->testWFlags(WPaintUnclipped) ) { + if ( gc ) + XSetSubwindowMode( dpy, gc, ClipByChildren ); + if ( gc_brush ) + XSetSubwindowMode( dpy, gc_brush, ClipByChildren ); + } + + if ( gc_brush ) { // restore brush gc + if ( brushRef ) { + release_gc( brushRef ); + brushRef = 0; + } else { + free_gc( dpy, gc_brush, testf(UsePrivateCx) ); + } + gc_brush = 0; + + } + if ( gc ) { // restore pen gc + if ( penRef ) { + release_gc( penRef ); + penRef = 0; + } else { + free_gc( dpy, gc, testf(UsePrivateCx) ); + } + gc = 0; + } + + if ( testf(ExtDev) ) + pdev->cmd( QPaintDevice::PdcEnd, this, 0 ); + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + // reset clipping/subwindow mode on our render picture + XftDrawSetClip((XftDraw *) rendhd, None); + XftDrawSetSubwindowMode((XftDraw *) rendhd, ClipByChildren); + } +#endif // QT_NO_XFTFREETYPE + + if ( pfont ) { + delete pfont; + pfont = 0; + } + + flags = 0; + pdev->painters--; + pdev = 0; + dpy = 0; + return TRUE; +} + +/*! + Flushes any buffered drawing operations inside the region \a + region using clipping mode \a cm. + + The flush may update the whole device if the platform does not + support flushing to a specified region. + + \sa flush() CoordinateMode +*/ + +void QPainter::flush(const QRegion &, CoordinateMode) +{ + flush(); +} + + +/*! + \overload + + Flushes any buffered drawing operations. +*/ + +void QPainter::flush() +{ + if ( isActive() && dpy ) + XFlush( dpy ); +} + + +/*! + Sets the background color of the painter to \a c. + + The background color is the color that is filled in when drawing + opaque text, stippled lines and bitmaps. The background color has + no effect in transparent background mode (which is the default). + + \sa backgroundColor() setBackgroundMode() BackgroundMode +*/ + +void QPainter::setBackgroundColor( const QColor &c ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::setBackgroundColor: Call begin() first" ); +#endif + return; + } + bg_col = c; + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].color = &bg_col; + if ( !pdev->cmd( QPaintDevice::PdcSetBkColor, this, param ) || !hd ) + return; + } + if ( !penRef ) + updatePen(); // update pen setting + if ( !brushRef ) + updateBrush(); // update brush setting +} + +/*! + Sets the background mode of the painter to \a m, which must be + either \c TransparentMode (the default) or \c OpaqueMode. + + Transparent mode draws stippled lines and text without setting the + background pixels. Opaque mode fills these space with the current + background color. + + Note that in order to draw a bitmap or pixmap transparently, you + must use QPixmap::setMask(). + + \sa backgroundMode(), setBackgroundColor() +*/ + +void QPainter::setBackgroundMode( BGMode m ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::setBackgroundMode: Call begin() first" ); +#endif + return; + } + if ( m != TransparentMode && m != OpaqueMode ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPainter::setBackgroundMode: Invalid mode" ); +#endif + return; + } + bg_mode = m; + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].ival = m; + if ( !pdev->cmd( QPaintDevice::PdcSetBkMode, this, param ) || !hd ) + return; + } + if ( !penRef ) + updatePen(); // update pen setting + if ( !brushRef ) + updateBrush(); // update brush setting +} + +static const short ropCodes[] = { // ROP translation table + GXcopy, // CopyROP + GXor, // OrROP + GXxor, // XorROP + GXandInverted, // NotAndROP EraseROP + GXcopyInverted, // NotCopyROP + GXorInverted, // NotOrROP + GXequiv, // NotXorROP + GXand, // AndROP + GXinvert, // NotROP + GXclear, // ClearROP + GXset, // SetROP + GXnoop, // NopROP + GXandReverse, // AndNotROP + GXorReverse, // OrNotROP + GXnand, // NandROP + GXnor // NorROP +}; + + +/*! + Sets the \link Qt::RasterOp raster operation \endlink to \a r. + The default is \c CopyROP. + + \sa rasterOp() Qt::RasterOp +*/ + +void QPainter::setRasterOp( RasterOp r ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::setRasterOp: Call begin() first" ); +#endif + return; + } + if ( (uint)r > LastROP ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPainter::setRasterOp: Invalid ROP code" ); +#endif + return; + } + rop = r; + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].ival = r; + if ( !pdev->cmd( QPaintDevice::PdcSetROP, this, param ) || !hd ) + return; + } + if ( penRef ) + updatePen(); // get non-cached pen GC + if ( brushRef ) + updateBrush(); // get non-cached brush GC + XSetFunction( dpy, gc, ropCodes[rop] ); + XSetFunction( dpy, gc_brush, ropCodes[rop] ); +} + +// ### matthias - true? + +/*! + Sets the brush origin to \a (x, y). + + The brush origin specifies the (0, 0) coordinate of the painter's + brush. This setting only applies to pattern brushes and pixmap + brushes. + + \sa brushOrigin() +*/ + +void QPainter::setBrushOrigin( int x, int y ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::setBrushOrigin: Call begin() first" ); +#endif + return; + } + bro = QPoint(x, y); + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].point = &bro; + if ( !pdev->cmd( QPaintDevice::PdcSetBrushOrigin, this, param ) || + !hd ) + return; + } + if ( brushRef ) + updateBrush(); // get non-cached brush GC + XSetTSOrigin( dpy, gc_brush, x, y ); +} + + +/*! + Enables clipping if \a enable is TRUE, or disables clipping if \a + enable is FALSE. + + \sa hasClipping(), setClipRect(), setClipRegion() +*/ + +void QPainter::setClipping( bool enable ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPainter::setClipping: Will be reset by begin()" ); +#endif + return; + } + + if ( enable == testf(ClipOn) ) + return; + + setf( ClipOn, enable ); + if ( testf(ExtDev) ) { + if ( block_ext ) + return; + QPDevCmdParam param[1]; + param[0].ival = enable; + if ( !pdev->cmd( QPaintDevice::PdcSetClip, this, param ) || !hd ) + return; + } + if ( enable ) { + QRegion rgn = crgn; + if ( pdev == paintEventDevice && paintEventClipRegion ) + rgn = rgn.intersect( *paintEventClipRegion ); + if ( penRef ) + updatePen(); + if ( brushRef ) + updateBrush(); + x11SetClipRegion( dpy, gc, gc_brush, rendhd, rgn ); + } else { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + x11SetClipRegion( dpy, gc, gc_brush , rendhd, *paintEventClipRegion ); + } else { + x11ClearClipRegion(dpy, gc, gc_brush, rendhd); + } + } +} + + +/*! + \overload + + Sets the clip region to the rectangle \a r and enables clipping. + The clip mode is set to \a m. + + \sa CoordinateMode +*/ + +void QPainter::setClipRect( const QRect &r, CoordinateMode m ) +{ + setClipRegion( QRegion( r ), m ); +} + +/*! + Sets the clip region to \a rgn and enables clipping. The clip mode + is set to \a m. + + Note that the clip region is given in physical device coordinates + and \e not subject to any \link coordsys.html coordinate + transformation.\endlink + + \sa setClipRect(), clipRegion(), setClipping() CoordinateMode +*/ + +void QPainter::setClipRegion( const QRegion &rgn, CoordinateMode m ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "QPainter::setClipRegion: Will be reset by begin()" ); +#endif + if ( m == CoordDevice ) + crgn = rgn; + else + crgn = xmat * rgn; + + if ( testf(ExtDev) ) { + if ( block_ext ) + return; + QPDevCmdParam param[2]; + param[0].rgn = &rgn; + param[1].ival = m; + if ( !pdev->cmd( QPaintDevice::PdcSetClipRegion, this, param ) ) + return; // device cannot clip + } + clearf( ClipOn ); // be sure to update clip rgn + setClipping( TRUE ); +} + + +/*! + \internal + + Internal function for drawing a polygon. +*/ + +void QPainter::drawPolyInternal( const QPointArray &a, bool close ) +{ + if ( a.size() < 2 ) + return; + + int x1, y1, x2, y2; // connect last to first point + a.point( a.size()-1, &x1, &y1 ); + a.point( 0, &x2, &y2 ); + bool do_close = close && !(x1 == x2 && y1 == y2); + + if ( close && cbrush.style() != NoBrush ) { // draw filled polygon + XFillPolygon( dpy, hd, gc_brush, (XPoint*)a.shortPoints(), a.size(), + Nonconvex, CoordModeOrigin ); + if ( cpen.style() == NoPen ) { // draw fake outline + XDrawLines( dpy, hd, gc_brush, (XPoint*)a.shortPoints(), a.size(), + CoordModeOrigin ); + if ( do_close ) + XDrawLine( dpy, hd, gc_brush, x1, y1, x2, y2 ); + } + } + if ( cpen.style() != NoPen ) { // draw outline + XDrawLines( dpy, hd, gc, (XPoint*)a.shortPoints(), a.size(), + CoordModeOrigin); + if ( do_close ) + XDrawLine( dpy, hd, gc, x1, y1, x2, y2 ); + } +} + + +/*! + Draws/plots a single point at \a (x, y) using the current pen. + + \sa QPen +*/ + +void QPainter::drawPoint( int x, int y ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + QPoint p( x, y ); + param[0].point = &p; + if ( !pdev->cmd( QPaintDevice::PdcDrawPoint, this, param ) || + !hd ) + return; + } + map( x, y, &x, &y ); + } + if ( cpen.style() != NoPen ) + XDrawPoint( dpy, hd, gc, x, y ); +} + + +/*! + Draws/plots an array of points, \a a, using the current pen. + + If \a index is non-zero (the default is zero) only points from \a + index are drawn. If \a npoints is negative (the default) the rest + of the points from \a index are drawn. If \a npoints is zero or + greater, \a npoints points are drawn. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + Qt 4. +*/ + +void QPainter::drawPoints( const QPointArray& a, int index, int npoints ) +{ + if ( npoints < 0 ) + npoints = a.size() - index; + if ( index + npoints > (int)a.size() ) + npoints = a.size() - index; + if ( !isActive() || npoints < 1 || index < 0 ) + return; + QPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + for (int i=0; i<npoints; i++) { + QPoint p( pa[index+i].x(), pa[index+i].y() ); + param[0].point = &p; + if ( !pdev->cmd( QPaintDevice::PdcDrawPoint, this, param )) + return; + } + if ( !hd ) return; + } + if ( txop != TxNone ) { + pa = xForm( a, index, npoints ); + if ( pa.size() != a.size() ) { + index = 0; + npoints = pa.size(); + } + } + } + if ( cpen.style() != NoPen ) + XDrawPoints( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )), + npoints, CoordModeOrigin ); +} + + +/*! \obsolete + Sets the current pen position to \a (x, y) + + \sa lineTo(), pos() +*/ + +void QPainter::moveTo( int x, int y ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + QPoint p( x, y ); + param[0].point = &p; + if ( !pdev->cmd( QPaintDevice::PdcMoveTo, this, param ) || !hd ) + return; + } + } + curPt = QPoint( x, y ); +} + +/*! \obsolete + Use drawLine() instead. + + Draws a line from the current pen position to \a (x, y) and sets + \a (x, y) to be the new current pen position. + + \sa QPen moveTo(), drawLine(), pos() +*/ + +void QPainter::lineTo( int x, int y ) +{ + if ( !isActive() ) + return; + int cx = curPt.x(), cy = curPt.y(); + curPt = QPoint( x, y ); + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + QPoint p( x, y ); + param[0].point = &p; + if ( !pdev->cmd( QPaintDevice::PdcLineTo, this, param ) || !hd ) + return; + } + map( x, y, &x, &y ); + map( cx, cy, &cx, &cy ); + } + if ( cpen.style() != NoPen ) + XDrawLine( dpy, hd, gc, cx, cy, x, y ); +} + +/*! + Draws a line from (\a x1, \a y1) to (\a x2, \a y2) and sets the + current pen position to (\a x2, \a y2). + + \sa pen() +*/ + +void QPainter::drawLine( int x1, int y1, int x2, int y2 ) +{ + if ( !isActive() ) + return; + curPt = QPoint( x2, y2 ); + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[2]; + QPoint p1(x1, y1), p2(x2, y2); + param[0].point = &p1; + param[1].point = &p2; + if ( !pdev->cmd( QPaintDevice::PdcDrawLine, this, param ) || !hd ) + return; + } + map( x1, y1, &x1, &y1 ); + map( x2, y2, &x2, &y2 ); + } + if ( cpen.style() != NoPen ) + XDrawLine( dpy, hd, gc, x1, y1, x2, y2 ); +} + + + +/*! + Draws a rectangle with upper left corner at \a (x, y) and with + width \a w and height \a h. + + \sa QPen, drawRoundRect() +*/ + +void QPainter::drawRect( int x, int y, int w, int h ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + QRect r( x, y, w, h ); + param[0].rect = &r; + if ( !pdev->cmd( QPaintDevice::PdcDrawRect, this, param ) || !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear polygon + QPointArray pa = xmat.mapToPolygon( QRect(x, y, w, h) ); + pa.resize( 5 ); + pa.setPoint( 4, pa.point( 0 ) ); + drawPolyInternal( pa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + if ( cbrush.style() != NoBrush ) { + if ( cpen.style() == NoPen ) { + XFillRectangle( dpy, hd, gc_brush, x, y, w, h ); + return; + } + int lw = cpen.width(); + int lw2 = (lw+1)/2; + if ( w > lw && h > lw ) + XFillRectangle( dpy, hd, gc_brush, x+lw2, y+lw2, w-lw-1, h-lw-1 ); + } + if ( cpen.style() != NoPen ) + XDrawRectangle( dpy, hd, gc, x, y, w-1, h-1 ); +} + +/*! + \overload + + Draws a Windows focus rectangle with upper left corner at (\a x, + \a y) and with width \a w and height \a h. + + This function draws a stippled XOR rectangle that is used to + indicate keyboard focus (when QApplication::style() is \c + WindowStyle). + + \warning This function draws nothing if the coordinate system has + been \link rotate() rotated\endlink or \link shear() + sheared\endlink. + + \sa drawRect(), QApplication::style() +*/ + +void QPainter::drawWinFocusRect( int x, int y, int w, int h ) +{ + drawWinFocusRect( x, y, w, h, TRUE, color0 ); +} + +/*! + Draws a Windows focus rectangle with upper left corner at (\a x, + \a y) and with width \a w and height \a h using a pen color that + contrasts with \a bgColor. + + This function draws a stippled rectangle (XOR is not used) that is + used to indicate keyboard focus (when the QApplication::style() is + \c WindowStyle). + + The pen color used to draw the rectangle is either white or black + depending on the color of \a bgColor (see QColor::gray()). + + \warning This function draws nothing if the coordinate system has + been \link rotate() rotated\endlink or \link shear() + sheared\endlink. + + \sa drawRect(), QApplication::style() +*/ + +void QPainter::drawWinFocusRect( int x, int y, int w, int h, + const QColor &bgColor ) +{ + drawWinFocusRect( x, y, w, h, FALSE, bgColor ); +} + + +/*! + \internal +*/ + +void QPainter::drawWinFocusRect( int x, int y, int w, int h, + bool xorPaint, const QColor &bgColor ) +{ + if ( !isActive() || txop == TxRotShear ) + return; + static char winfocus_line[] = { 1, 1 }; + + QPen old_pen = cpen; + RasterOp old_rop = (RasterOp)rop; + + if ( xorPaint ) { + if ( QColor::numBitPlanes() <= 8 ) + setPen( color1 ); + else + setPen( white ); + setRasterOp( XorROP ); + } else { + if ( qGray( bgColor.rgb() ) < 128 ) + setPen( white ); + else + setPen( black ); + } + + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + QRect r( x, y, w, h ); + param[0].rect = &r; + if ( !pdev->cmd( QPaintDevice::PdcDrawRect, this, param ) || !hd) { + setRasterOp( old_rop ); + setPen( old_pen ); + return; + } + } + map( x, y, w, h, &x, &y, &w, &h ); + } + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + XSetDashes( dpy, gc, 0, winfocus_line, 2 ); + XSetLineAttributes( dpy, gc, 1, LineOnOffDash, CapButt, JoinMiter ); + + XDrawRectangle( dpy, hd, gc, x, y, w-1, h-1 ); + XSetLineAttributes( dpy, gc, 0, LineSolid, CapButt, JoinMiter ); + setRasterOp( old_rop ); + setPen( old_pen ); +} + + +/*! + Draws a rectangle with rounded corners at \a (x, y), with width \a + w and height \a h. + + The \a xRnd and \a yRnd arguments specify how rounded the corners + should be. 0 is angled corners, 99 is maximum roundedness. + + The width and height include all of the drawn lines. + + \sa drawRect(), QPen +*/ + +void QPainter::drawRoundRect( int x, int y, int w, int h, int xRnd, int yRnd ) +{ + if ( !isActive() ) + return; + if ( xRnd <= 0 || yRnd <= 0 ) { + drawRect( x, y, w, h ); // draw normal rectangle + return; + } + if ( xRnd >= 100 ) // fix ranges + xRnd = 99; + if ( yRnd >= 100 ) + yRnd = 99; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[3]; + QRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = xRnd; + param[2].ival = yRnd; + if ( !pdev->cmd( QPaintDevice::PdcDrawRoundRect, this, param ) || + !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear polygon + if ( w <= 0 || h <= 0 ) + fix_neg_rect( &x, &y, &w, &h ); + w--; + h--; + int rxx = w*xRnd/200; + int ryy = h*yRnd/200; + // were there overflows? + if ( rxx < 0 ) + rxx = w/200*xRnd; + if ( ryy < 0 ) + ryy = h/200*yRnd; + int rxx2 = 2*rxx; + int ryy2 = 2*ryy; + QPointArray a[4]; + a[0].makeArc( x, y, rxx2, ryy2, 1*16*90, 16*90, xmat ); + a[1].makeArc( x, y+h-ryy2, rxx2, ryy2, 2*16*90, 16*90, xmat ); + a[2].makeArc( x+w-rxx2, y+h-ryy2, rxx2, ryy2, 3*16*90, 16*90, xmat ); + a[3].makeArc( x+w-rxx2, y, rxx2, ryy2, 0*16*90, 16*90, xmat ); + // ### is there a better way to join QPointArrays? + QPointArray aa; + aa.resize( a[0].size() + a[1].size() + a[2].size() + a[3].size() ); + uint j = 0; + for ( int k=0; k<4; k++ ) { + for ( uint i=0; i<a[k].size(); i++ ) { + aa.setPoint( j, a[k].point(i) ); + j++; + } + } + drawPolyInternal( aa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + int rx = (w*xRnd)/200; + int ry = (h*yRnd)/200; + int rx2 = 2*rx; + int ry2 = 2*ry; + if ( cbrush.style() != NoBrush ) { // draw filled round rect + int dp, ds; + if ( cpen.style() == NoPen ) { + dp = 0; + ds = 1; + } + else { + dp = 1; + ds = 0; + } +#define SET_ARC(px, py, w, h, a1, a2) \ + a->x=px; a->y=py; a->width=w; a->height=h; a->angle1=a1; a->angle2=a2; a++ + XArc arcs[4]; + XArc *a = arcs; + SET_ARC( x+w-rx2, y, rx2, ry2, 0, 90*64 ); + SET_ARC( x, y, rx2, ry2, 90*64, 90*64 ); + SET_ARC( x, y+h-ry2, rx2, ry2, 180*64, 90*64 ); + SET_ARC( x+w-rx2, y+h-ry2, rx2, ry2, 270*64, 90*64 ); + XFillArcs( dpy, hd, gc_brush, arcs, 4 ); +#undef SET_ARC +#define SET_RCT(px, py, w, h) \ + r->x=px; r->y=py; r->width=w; r->height=h; r++ + XRectangle rects[3]; + XRectangle *r = rects; + SET_RCT( x+rx, y+dp, w-rx2, ry ); + SET_RCT( x+dp, y+ry, w+ds, h-ry2 ); + SET_RCT( x+rx, y+h-ry, w-rx2, ry+ds ); + XFillRectangles( dpy, hd, gc_brush, rects, 3 ); +#undef SET_RCT + } + if ( cpen.style() != NoPen ) { // draw outline +#define SET_ARC(px, py, w, h, a1, a2) \ + a->x=px; a->y=py; a->width=w; a->height=h; a->angle1=a1; a->angle2=a2; a++ + XArc arcs[4]; + XArc *a = arcs; + SET_ARC( x+w-rx2, y, rx2, ry2, 0, 90*64 ); + SET_ARC( x, y, rx2, ry2, 90*64, 90*64 ); + SET_ARC( x, y+h-ry2, rx2, ry2, 180*64, 90*64 ); + SET_ARC( x+w-rx2, y+h-ry2, rx2, ry2, 270*64, 90*64 ); + XDrawArcs( dpy, hd, gc, arcs, 4 ); +#undef SET_ARC +#define SET_SEG(xp1, yp1, xp2, yp2) \ + s->x1=xp1; s->y1=yp1; s->x2=xp2; s->y2=yp2; s++ + XSegment segs[4]; + XSegment *s = segs; + SET_SEG( x+rx, y, x+w-rx, y ); + SET_SEG( x+rx, y+h, x+w-rx, y+h ); + SET_SEG( x, y+ry, x, y+h-ry ); + SET_SEG( x+w, y+ry, x+w, y+h-ry ); + XDrawSegments( dpy, hd, gc, segs, 4 ); +#undef SET_SET + } +} + +/*! + Draws an ellipse with center at \a (x + w/2, y + h/2) and size \a + (w, h). +*/ + +void QPainter::drawEllipse( int x, int y, int w, int h ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + QRect r( x, y, w, h ); + param[0].rect = &r; + if ( !pdev->cmd( QPaintDevice::PdcDrawEllipse, this, param ) || + !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear polygon + QPointArray a; + a.makeArc( x, y, w, h, 0, 360*16, xmat ); + drawPolyInternal( a ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + if ( w == 1 && h == 1 ) { + XDrawPoint( dpy, hd, (cpen.style() == NoPen)?gc_brush:gc, x, y ); + return; + } + w--; + h--; + if ( cbrush.style() != NoBrush ) { // draw filled ellipse + XFillArc( dpy, hd, gc_brush, x, y, w, h, 0, 360*64 ); + if ( cpen.style() == NoPen ) { + XDrawArc( dpy, hd, gc_brush, x, y, w, h, 0, 360*64 ); + return; + } + } + if ( cpen.style() != NoPen ) // draw outline + XDrawArc( dpy, hd, gc, x, y, w, h, 0, 360*64 ); +} + + +/*! + Draws an arc defined by the rectangle \a (x, y, w, h), the start + angle \a a and the arc length \a alen. + + The angles \a a and \a alen are 1/16th of a degree, i.e. a full + circle equals 5760 (16*360). Positive values of \a a and \a alen + mean counter-clockwise while negative values mean the clockwise + direction. Zero degrees is at the 3 o'clock position. + + Example: + \code + QPainter p( myWidget ); + p.drawArc( 10,10, 70,100, 100*16, 160*16 ); // draws a "(" arc + \endcode + + \sa drawPie(), drawChord() +*/ + +void QPainter::drawArc( int x, int y, int w, int h, int a, int alen ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[3]; + QRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = a; + param[2].ival = alen; + if ( !pdev->cmd( QPaintDevice::PdcDrawArc, this, param ) || + !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear + QPointArray pa; + pa.makeArc( x, y, w, h, a, alen, xmat ); // arc polyline + drawPolyInternal( pa, FALSE ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + if ( cpen.style() != NoPen ) + XDrawArc( dpy, hd, gc, x, y, w, h, a*4, alen*4 ); +} + + +/*! + Draws a pie defined by the rectangle \a (x, y, w, h), the start + angle \a a and the arc length \a alen. + + The pie is filled with the current brush(). + + The angles \a a and \a alen are 1/16th of a degree, i.e. a full + circle equals 5760 (16*360). Positive values of \a a and \a alen + mean counter-clockwise while negative values mean the clockwise + direction. Zero degrees is at the 3 o'clock position. + + \sa drawArc(), drawChord() +*/ + +void QPainter::drawPie( int x, int y, int w, int h, int a, int alen ) +{ + // Make sure "a" is 0..360*16, as otherwise a*4 may overflow 16 bits. + if ( a > (360*16) ) { + a = a % (360*16); + } else if ( a < 0 ) { + a = a % (360*16); + if ( a < 0 ) a += (360*16); + } + + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[3]; + QRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = a; + param[2].ival = alen; + if ( !pdev->cmd( QPaintDevice::PdcDrawPie, this, param ) || !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear + QPointArray pa; + pa.makeArc( x, y, w, h, a, alen, xmat ); // arc polyline + int n = pa.size(); + int cx, cy; + xmat.map(x+w/2, y+h/2, &cx, &cy); + pa.resize( n+2 ); + pa.setPoint( n, cx, cy ); // add legs + pa.setPoint( n+1, pa.at(0) ); + drawPolyInternal( pa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + XSetArcMode( dpy, gc_brush, ArcPieSlice ); + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + + GC g = gc; + bool nopen = cpen.style() == NoPen; + + if ( cbrush.style() != NoBrush ) { // draw filled pie + XFillArc( dpy, hd, gc_brush, x, y, w, h, a*4, alen*4 ); + if ( nopen ) { + g = gc_brush; + nopen = FALSE; + } + } + if ( !nopen ) { // draw pie outline + double w2 = 0.5*w; // with, height in ellipsis + double h2 = 0.5*h; + double xc = (double)x+w2; + double yc = (double)y+h2; + double ra1 = Q_PI/2880.0*a; // convert a, alen to radians + double ra2 = ra1 + Q_PI/2880.0*alen; + int xic = qRound(xc); + int yic = qRound(yc); + XDrawLine( dpy, hd, g, xic, yic, + qRound(xc + qcos(ra1)*w2), qRound(yc - qsin(ra1)*h2)); + XDrawLine( dpy, hd, g, xic, yic, + qRound(xc + qcos(ra2)*w2), qRound(yc - qsin(ra2)*h2)); + XDrawArc( dpy, hd, g, x, y, w, h, a*4, alen*4 ); + } +} + + +/*! + Draws a chord defined by the rectangle \a (x, y, w, h), the start + angle \a a and the arc length \a alen. + + The chord is filled with the current brush(). + + The angles \a a and \a alen are 1/16th of a degree, i.e. a full + circle equals 5760 (16*360). Positive values of \a a and \a alen + mean counter-clockwise while negative values mean the clockwise + direction. Zero degrees is at the 3 o'clock position. + + \sa drawArc(), drawPie() +*/ + +void QPainter::drawChord( int x, int y, int w, int h, int a, int alen ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[3]; + QRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = a; + param[2].ival = alen; + if ( !pdev->cmd(QPaintDevice::PdcDrawChord, this, param) || !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear + QPointArray pa; + pa.makeArc( x, y, w-1, h-1, a, alen, xmat ); // arc polygon + int n = pa.size(); + pa.resize( n+1 ); + pa.setPoint( n, pa.at(0) ); // connect endpoints + drawPolyInternal( pa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + XSetArcMode( dpy, gc_brush, ArcChord ); + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + + GC g = gc; + bool nopen = cpen.style() == NoPen; + + if ( cbrush.style() != NoBrush ) { // draw filled chord + XFillArc( dpy, hd, gc_brush, x, y, w, h, a*4, alen*4 ); + if ( nopen ) { + g = gc_brush; + nopen = FALSE; + } + } + if ( !nopen ) { // draw chord outline + double w2 = 0.5*w; // with, height in ellipsis + double h2 = 0.5*h; + double xc = (double)x+w2; + double yc = (double)y+h2; + double ra1 = Q_PI/2880.0*a; // convert a, alen to radians + double ra2 = ra1 + Q_PI/2880.0*alen; + XDrawLine( dpy, hd, g, + qRound(xc + qcos(ra1)*w2), qRound(yc - qsin(ra1)*h2), + qRound(xc + qcos(ra2)*w2), qRound(yc - qsin(ra2)*h2)); + XDrawArc( dpy, hd, g, x, y, w, h, a*4, alen*4 ); + } + XSetArcMode( dpy, gc_brush, ArcPieSlice ); +} + + +/*! + Draws \a nlines separate lines from points defined in \a a, + starting at \a a[index] (\a index defaults to 0). If \a nlines is + -1 (the default) all points until the end of the array are used + (i.e. (a.size()-index)/2 lines are drawn). + + Draws the 1st line from \a a[index] to \a a[index+1]. Draws the + 2nd line from \a a[index+2] to \a a[index+3] etc. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + Qt 4. + + \sa drawPolyline(), drawPolygon(), QPen +*/ + +void QPainter::drawLineSegments( const QPointArray &a, int index, int nlines ) +{ + if ( nlines < 0 ) + nlines = a.size()/2 - index/2; + if ( index + nlines*2 > (int)a.size() ) + nlines = (a.size() - index)/2; + if ( !isActive() || nlines < 1 || index < 0 ) + return; + QPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + if ( 2*nlines != (int)pa.size() ) { + pa = QPointArray( nlines*2 ); + for ( int i=0; i<nlines*2; i++ ) + pa.setPoint( i, a.point(index+i) ); + index = 0; + } + QPDevCmdParam param[1]; + param[0].ptarr = (QPointArray*)&pa; + if ( !pdev->cmd(QPaintDevice::PdcDrawLineSegments, this, param) || + !hd ) + return; + } + if ( txop != TxNone ) { + pa = xForm( a, index, nlines*2 ); + if ( pa.size() != a.size() ) { + index = 0; + nlines = pa.size()/2; + } + } + } + if ( cpen.style() != NoPen ) + XDrawSegments( dpy, hd, gc, + (XSegment*)(pa.shortPoints( index, nlines*2 )), nlines ); +} + + +/*! + Draws the polyline defined by the \a npoints points in \a a + starting at \a a[index]. (\a index defaults to 0.) + + If \a npoints is -1 (the default) all points until the end of the + array are used (i.e. a.size()-index-1 line segments are drawn). + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + Qt 4. + + \sa drawLineSegments(), drawPolygon(), QPen +*/ + +void QPainter::drawPolyline( const QPointArray &a, int index, int npoints ) +{ + if ( npoints < 0 ) + npoints = a.size() - index; + if ( index + npoints > (int)a.size() ) + npoints = a.size() - index; + if ( !isActive() || npoints < 2 || index < 0 ) + return; + QPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + if ( npoints != (int)pa.size() ) { + pa = QPointArray( npoints ); + for ( int i=0; i<npoints; i++ ) + pa.setPoint( i, a.point(index+i) ); + index = 0; + } + QPDevCmdParam param[1]; + param[0].ptarr = (QPointArray*)&pa; + if ( !pdev->cmd(QPaintDevice::PdcDrawPolyline, this, param) || !hd ) + return; + } + if ( txop != TxNone ) { + pa = xForm( pa, index, npoints ); + if ( pa.size() != a.size() ) { + index = 0; + npoints = pa.size(); + } + } + } + if ( cpen.style() != NoPen ) { + while(npoints>65535) { + XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, 65535 )), + 65535, CoordModeOrigin ); + npoints-=65535; + index+=65535; + } + XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )), + npoints, CoordModeOrigin ); + } +} + +static int global_polygon_shape = Complex; + +/*! + Draws the polygon defined by the \a npoints points in \a a + starting at \a a[index]. (\a index defaults to 0.) + + If \a npoints is -1 (the default) all points until the end of the + array are used (i.e. a.size()-index line segments define the + polygon). + + The first point is always connected to the last point. + + The polygon is filled with the current brush(). If \a winding is + TRUE, the polygon is filled using the winding fill algorithm. If + \a winding is FALSE, the polygon is filled using the even-odd + (alternative) fill algorithm. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + Qt 4. + + \sa drawLineSegments(), drawPolyline(), QPen +*/ + +void QPainter::drawPolygon( const QPointArray &a, bool winding, + int index, int npoints ) +{ + if ( npoints < 0 ) + npoints = a.size() - index; + if ( index + npoints > (int)a.size() ) + npoints = a.size() - index; + if ( !isActive() || npoints < 2 || index < 0 ) + return; + QPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + if ( npoints != (int)a.size() ) { + pa = QPointArray( npoints ); + for ( int i=0; i<npoints; i++ ) + pa.setPoint( i, a.point(index+i) ); + index = 0; + } + QPDevCmdParam param[2]; + param[0].ptarr = (QPointArray*)&pa; + param[1].ival = winding; + if ( !pdev->cmd(QPaintDevice::PdcDrawPolygon, this, param) || !hd ) + return; + } + if ( txop != TxNone ) { + pa = xForm( a, index, npoints ); + if ( pa.size() != a.size() ) { + index = 0; + npoints = pa.size(); + } + } + } + if ( winding ) // set to winding fill rule + XSetFillRule( dpy, gc_brush, WindingRule ); + + if ( pa[index] != pa[index+npoints-1] ){ // close open pointarray + pa.detach(); + pa.resize( index+npoints+1 ); + pa.setPoint( index+npoints, pa[index] ); + npoints++; + } + + if ( cbrush.style() != NoBrush ) { // draw filled polygon + XFillPolygon( dpy, hd, gc_brush, + (XPoint*)(pa.shortPoints( index, npoints )), + npoints, global_polygon_shape, CoordModeOrigin ); + } + if ( cpen.style() != NoPen ) { // draw outline + XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )), + npoints, CoordModeOrigin ); + } + if ( winding ) // set to normal fill rule + XSetFillRule( dpy, gc_brush, EvenOddRule ); +} + +/*! + Draws the convex polygon defined by the \a npoints points in \a pa + starting at \a pa[index] (\a index defaults to 0). + + If the supplied polygon is not convex, the results are undefined. + + On some platforms (e.g. X Window), this is faster than + drawPolygon(). + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + Qt 4. +*/ +void QPainter::drawConvexPolygon( const QPointArray &pa, + int index, int npoints ) +{ + global_polygon_shape = Convex; + drawPolygon(pa, FALSE, index, npoints); + global_polygon_shape = Complex; +} + + + +/*! + Draws a cubic Bezier curve defined by the control points in \a a, + starting at \a a[index] (\a index defaults to 0). + + Control points after \a a[index + 3] are ignored. Nothing happens + if there aren't enough control points. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + Qt 4. +*/ + +void QPainter::drawCubicBezier( const QPointArray &a, int index ) +{ + if ( !isActive() ) + return; + if ( a.size() - index < 4 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPainter::drawCubicBezier: Cubic Bezier needs 4 control " + "points" ); +#endif + return; + } + QPointArray pa( a ); + if ( index != 0 || a.size() > 4 ) { + pa = QPointArray( 4 ); + for ( int i=0; i<4; i++ ) + pa.setPoint( i, a.point(index+i) ); + } + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + QPDevCmdParam param[1]; + param[0].ptarr = (QPointArray*)&pa; + if ( !pdev->cmd(QPaintDevice::PdcDrawCubicBezier, this, param) || + !hd ) + return; + } + if ( txop != TxNone ) + pa = xForm( pa ); + } + if ( cpen.style() != NoPen ) { + pa = pa.cubicBezier(); + XDrawLines( dpy, hd, gc, (XPoint*)pa.shortPoints(), pa.size(), + CoordModeOrigin ); + } +} + + +/*! + Draws a pixmap at \a (x, y) by copying a part of \a pixmap into + the paint device. + + \a (x, y) specifies the top-left point in the paint device that is + to be drawn onto. \a (sx, sy) specifies the top-left point in \a + pixmap that is to be drawn. The default is (0, 0). + + \a (sw, sh) specifies the size of the pixmap that is to be drawn. + The default, (-1, -1), means all the way to the bottom right of + the pixmap. + + Currently the mask of the pixmap or it's alpha channel are ignored + when painting on a QPrinter. + + \sa bitBlt(), QPixmap::setMask() +*/ + +void QPainter::drawPixmap( int x, int y, const QPixmap &pixmap, + int sx, int sy, int sw, int sh ) +{ + if ( !isActive() || pixmap.isNull() ) + return; + + // right/bottom + if ( sw < 0 ) + sw = pixmap.width() - sx; + if ( sh < 0 ) + sh = pixmap.height() - sy; + + // Sanity-check clipping + if ( sx < 0 ) { + x -= sx; + sw += sx; + sx = 0; + } + if ( sw + sx > pixmap.width() ) + sw = pixmap.width() - sx; + if ( sy < 0 ) { + y -= sy; + sh += sy; + sy = 0; + } + if ( sh + sy > pixmap.height() ) + sh = pixmap.height() - sy; + + if ( sw <= 0 || sh <= 0 ) + return; + + if ( pdev->x11Screen() != pixmap.x11Screen() ) { + QPixmap* p = (QPixmap*) &pixmap; + p->x11SetScreen( pdev->x11Screen() ); + } + + QPixmap::x11SetDefaultScreen( pixmap.x11Screen() ); + + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) || txop == TxScale || txop == TxRotShear ) { + if ( sx != 0 || sy != 0 || + sw != pixmap.width() || sh != pixmap.height() ) { + QPixmap tmp( sw, sh, pixmap.depth() ); + bitBlt( &tmp, 0, 0, &pixmap, sx, sy, sw, sh, CopyROP, TRUE ); + if ( pixmap.mask() ) { + QBitmap mask( sw, sh ); + bitBlt( &mask, 0, 0, pixmap.mask(), sx, sy, sw, sh, + CopyROP, TRUE ); + tmp.setMask( mask ); + } + drawPixmap( x, y, tmp ); + return; + } + if ( testf(ExtDev) ) { + QPDevCmdParam param[2]; + QRect r(x, y, pixmap.width(), pixmap.height()); + param[0].rect = &r; + param[1].pixmap = &pixmap; + if ( !pdev->cmd(QPaintDevice::PdcDrawPixmap, this, param) || !hd ) + return; + } + if ( txop == TxScale || txop == TxRotShear ) { + QWMatrix mat( m11(), m12(), + m21(), m22(), + dx(), dy() ); + mat = QPixmap::trueMatrix( mat, sw, sh ); + QPixmap pm = pixmap.xForm( mat ); + if ( !pm.mask() && txop == TxRotShear ) { + QBitmap bm_clip( sw, sh, 1 ); + bm_clip.fill( color1 ); + pm.setMask( bm_clip.xForm(mat) ); + } + map( x, y, &x, &y ); // compute position of pixmap + int dx, dy; + mat.map( 0, 0, &dx, &dy ); + uint save_flags = flags; + flags = IsActive | (save_flags & ClipOn); + drawPixmap( x-dx, y-dy, pm ); + flags = save_flags; + return; + } + } + map( x, y, &x, &y ); + } + + QBitmap *mask = (QBitmap *)pixmap.mask(); + bool mono = pixmap.depth() == 1; + + if ( mask && !hasClipping() && pdev != paintEventDevice ) { + if ( mono ) { // needs GCs pen color + bool selfmask = pixmap.data->selfmask; + if ( selfmask ) { + XSetFillStyle( dpy, gc, FillStippled ); + XSetStipple( dpy, gc, pixmap.handle() ); + } else { + XSetFillStyle( dpy, gc, FillOpaqueStippled ); + XSetStipple( dpy, gc, pixmap.handle() ); + XSetClipMask( dpy, gc, mask->handle() ); + XSetClipOrigin( dpy, gc, x-sx, y-sy ); + } + XSetTSOrigin( dpy, gc, x-sx, y-sy ); + XFillRectangle( dpy, hd, gc, x, y, sw, sh ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); + if ( !selfmask ) { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion ); + } else { + x11ClearClipRegion(dpy, gc, 0, rendhd); + } + } + } else { + bitBlt( pdev, x, y, &pixmap, sx, sy, sw, sh, (RasterOp)rop ); + } + return; + } + + QRegion rgn = crgn; + + if ( mask ) { // pixmap has clip mask + // Implies that clipping is on, either explicit or implicit + // Create a new mask that combines the mask with the clip region + + if ( pdev == paintEventDevice && paintEventClipRegion ) { + if ( hasClipping() ) + rgn = rgn.intersect( *paintEventClipRegion ); + else + rgn = *paintEventClipRegion; + } + + QBitmap *comb = new QBitmap( sw, sh ); + comb->detach(); + GC cgc = qt_xget_temp_gc( pixmap.x11Screen(), TRUE ); // get temporary mono GC + XSetForeground( dpy, cgc, 0 ); + XFillRectangle( dpy, comb->handle(), cgc, 0, 0, sw, sh ); + XSetBackground( dpy, cgc, 0 ); + XSetForeground( dpy, cgc, 1 ); + int num; + XRectangle *rects = (XRectangle *)qt_getClipRects( rgn, num ); + XSetClipRectangles( dpy, cgc, -x, -y, rects, num, YXBanded ); + XSetFillStyle( dpy, cgc, FillOpaqueStippled ); + XSetStipple( dpy, cgc, mask->handle() ); + XSetTSOrigin( dpy, cgc, -sx, -sy ); + XFillRectangle( dpy, comb->handle(), cgc, 0, 0, sw, sh ); + XSetTSOrigin( dpy, cgc, 0, 0 ); // restore cgc + XSetFillStyle( dpy, cgc, FillSolid ); + XSetClipMask( dpy, cgc, None ); + mask = comb; // it's deleted below + + XSetClipMask( dpy, gc, mask->handle() ); + XSetClipOrigin( dpy, gc, x, y ); + } + + if ( mono ) { + XSetBackground( dpy, gc, bg_col.pixel(scrn) ); + XSetFillStyle( dpy, gc, FillOpaqueStippled ); + XSetStipple( dpy, gc, pixmap.handle() ); + XSetTSOrigin( dpy, gc, x-sx, y-sy ); + XFillRectangle( dpy, hd, gc, x, y, sw, sh ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); + } else { +#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_NO_XRENDER) + Picture pict = rendhd ? XftDrawPicture((XftDraw *) rendhd) : None; + QPixmap *alpha = pixmap.data->alphapm; + + if ( pict && pixmap.x11RenderHandle() && + alpha && alpha->x11RenderHandle()) { + XRenderComposite(dpy, PictOpOver, pixmap.x11RenderHandle(), + alpha->x11RenderHandle(), pict, + sx, sy, sx, sy, x, y, sw, sh); + } else +#endif // !QT_NO_XFTFREETYPE && !QT_NO_XRENDER + { + XCopyArea( dpy, pixmap.handle(), hd, gc, sx, sy, sw, sh, x, y ); + } + } + + if ( mask ) { // restore clipping + XSetClipOrigin( dpy, gc, 0, 0 ); + XSetRegion( dpy, gc, rgn.handle() ); + delete mask; // delete comb, created above + } +} + + +/* Internal, used by drawTiledPixmap */ + +static void drawTile( QPainter *p, int x, int y, int w, int h, + const QPixmap &pixmap, int xOffset, int yOffset ) +{ + int yPos, xPos, drawH, drawW, yOff, xOff; + yPos = y; + yOff = yOffset; + while( yPos < y + h ) { + drawH = pixmap.height() - yOff; // Cropping first row + if ( yPos + drawH > y + h ) // Cropping last row + drawH = y + h - yPos; + xPos = x; + xOff = xOffset; + while( xPos < x + w ) { + drawW = pixmap.width() - xOff; // Cropping first column + if ( xPos + drawW > x + w ) // Cropping last column + drawW = x + w - xPos; + p->drawPixmap( xPos, yPos, pixmap, xOff, yOff, drawW, drawH ); + xPos += drawW; + xOff = 0; + } + yPos += drawH; + yOff = 0; + } +} + +#if 0 // see comment in drawTiledPixmap +/* Internal, used by drawTiledPixmap */ + +static void fillTile( QPixmap *tile, const QPixmap &pixmap ) +{ + bitBlt( tile, 0, 0, &pixmap, 0, 0, -1, -1, Qt::CopyROP, TRUE ); + int x = pixmap.width(); + while ( x < tile->width() ) { + bitBlt( tile, x,0, tile, 0,0, x,pixmap.height(), Qt::CopyROP, TRUE ); + x *= 2; + } + int y = pixmap.height(); + while ( y < tile->height() ) { + bitBlt( tile, 0,y, tile, 0,0, tile->width(),y, Qt::CopyROP, TRUE ); + y *= 2; + } +} +#endif + +/*! + Draws a tiled \a pixmap in the specified rectangle. + + \a (x, y) specifies the top-left point in the paint device that is + to be drawn onto; with the width and height given by \a w and \a + h. \a (sx, sy) specifies the top-left point in \a pixmap that is + to be drawn. The default is (0, 0). + + Calling drawTiledPixmap() is similar to calling drawPixmap() + several times to fill (tile) an area with a pixmap, but is + potentially much more efficient depending on the underlying window + system. + + \sa drawPixmap() +*/ + +void QPainter::drawTiledPixmap( int x, int y, int w, int h, + const QPixmap &pixmap, int sx, int sy ) +{ + int sw = pixmap.width(); + int sh = pixmap.height(); + if (!sw || !sh ) + return; + if ( sx < 0 ) + sx = sw - -sx % sw; + else + sx = sx % sw; + if ( sy < 0 ) + sy = sh - -sy % sh; + else + sy = sy % sh; + /* + Requirements for optimizing tiled pixmaps: + - not an external device + - not scale or rotshear + - not mono pixmap + - no mask + */ + QBitmap *mask = (QBitmap *)pixmap.mask(); + if ( !testf(ExtDev) && txop <= TxTranslate && pixmap.depth() > 1 && + mask == 0 ) { + if ( txop == TxTranslate ) + map( x, y, &x, &y ); + +#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_NO_XRENDER) + Picture pict = rendhd ? XftDrawPicture((XftDraw *) rendhd) : None; + QPixmap *alpha = pixmap.data->alphapm; + + if (pict && pixmap.x11RenderHandle() && alpha && alpha->x11RenderHandle()) { + // this is essentially drawTile() from above, inlined for + // the XRenderComposite call + int yPos, xPos, drawH, drawW, yOff, xOff; + yPos = y; + yOff = sy; + while( yPos < y + h ) { + drawH = pixmap.height() - yOff; // Cropping first row + if ( yPos + drawH > y + h ) // Cropping last row + drawH = y + h - yPos; + xPos = x; + xOff = sx; + while( xPos < x + w ) { + drawW = pixmap.width() - xOff; // Cropping first column + if ( xPos + drawW > x + w ) // Cropping last column + drawW = x + w - xPos; + XRenderComposite(dpy, PictOpOver, pixmap.x11RenderHandle(), + alpha->x11RenderHandle(), pict, + xOff, yOff, xOff, yOff, xPos, yPos, drawW, drawH); + xPos += drawW; + xOff = 0; + } + yPos += drawH; + yOff = 0; + } + return; + } +#endif // !QT_NO_XFTFREETYPE && !QT_NO_XRENDER + + XSetTile( dpy, gc, pixmap.handle() ); + XSetFillStyle( dpy, gc, FillTiled ); + XSetTSOrigin( dpy, gc, x-sx, y-sy ); + XFillRectangle( dpy, hd, gc, x, y, w, h ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); + return; + } + +#if 0 + // maybe there'll be point in this again, but for the time all it + // does is make trouble for the postscript code. + if ( sw*sh < 8192 && sw*sh < 16*w*h ) { + int tw = sw; + int th = sh; + while( th * tw < 4096 && ( th < h || tw < w ) ) { + if ( h/th > w/tw ) + th *= 2; + else + tw *= 2; + } + QPixmap tile( tw, th, pixmap.depth(), QPixmap::NormalOptim ); + fillTile( &tile, pixmap ); + if ( mask ) { + QBitmap tilemask( tw, th, QPixmap::NormalOptim ); + fillTile( &tilemask, *mask ); + tile.setMask( tilemask ); + } + drawTile( this, x, y, w, h, tile, sx, sy ); + } else { + drawTile( this, x, y, w, h, pixmap, sx, sy ); + } +#else + // for now we'll just output the original and let the postscript + // code make what it can of it. qpicture will be unhappy. + drawTile( this, x, y, w, h, pixmap, sx, sy ); +#endif +} + +#if 0 +// +// Generate a string that describes a transformed bitmap. This string is used +// to insert and find bitmaps in the global pixmap cache. +// + +static QString gen_text_bitmap_key( const QWMatrix &m, const QFont &font, + const QString &str, int pos, int len ) +{ + QString fk = font.key(); + int sz = 4*2 + len*2 + fk.length()*2 + sizeof(double)*6; + QByteArray buf(sz); + uchar *p = (uchar *)buf.data(); + *((double*)p)=m.m11(); p+=sizeof(double); + *((double*)p)=m.m12(); p+=sizeof(double); + *((double*)p)=m.m21(); p+=sizeof(double); + *((double*)p)=m.m22(); p+=sizeof(double); + *((double*)p)=m.dx(); p+=sizeof(double); + *((double*)p)=m.dy(); p+=sizeof(double); + QChar h1( '$' ); + QChar h2( 'q' ); + QChar h3( 't' ); + QChar h4( '$' ); + *((QChar*)p)=h1; p+=2; + *((QChar*)p)=h2; p+=2; + *((QChar*)p)=h3; p+=2; + *((QChar*)p)=h4; p+=2; + memcpy( (char*)p, (char*)(str.unicode()+pos), len*2 ); p += len*2; + memcpy( (char*)p, (char*)fk.unicode(), fk.length()*2 ); p += fk.length()*2; + return QString( (QChar*)buf.data(), buf.size()/2 ); +} + +static QBitmap *get_text_bitmap( const QString &key ) +{ + return (QBitmap*)QPixmapCache::find( key ); +} + +static void ins_text_bitmap( const QString &key, QBitmap *bm ) +{ + if ( !QPixmapCache::insert(key, bm) ) // cannot insert pixmap + delete bm; +} +#endif + +void qt_draw_transformed_rect( QPainter *p, int x, int y, int w, int h, bool fill ) +{ + XPoint points[5]; + int xp = x, yp = y; + p->map( xp, yp, &xp, &yp ); + points[0].x = xp; + points[0].y = yp; + xp = x + w; yp = y; + p->map( xp, yp, &xp, &yp ); + points[1].x = xp; + points[1].y = yp; + xp = x + w; yp = y + h; + p->map( xp, yp, &xp, &yp ); + points[2].x = xp; + points[2].y = yp; + xp = x; yp = y + h; + p->map( xp, yp, &xp, &yp ); + points[3].x = xp; + points[3].y = yp; + points[4] = points[0]; + + if ( fill ) + XFillPolygon( p->dpy, p->hd, p->gc, points, 4, Convex, CoordModeOrigin ); + else + XDrawLines( p->dpy, p->hd, p->gc, points, 5, CoordModeOrigin ); +} + +void qt_draw_background( QPainter *p, int x, int y, int w, int h ) +{ + if (p->testf(QPainter::ExtDev)) { + if (p->pdev->devType() == QInternal::Printer) + p->fillRect(x, y, w, h, p->bg_col); + return; + } + XSetForeground( p->dpy, p->gc, p->bg_col.pixel(p->scrn) ); + qt_draw_transformed_rect( p, x, y, w, h, TRUE); + XSetForeground( p->dpy, p->gc, p->cpen.color().pixel(p->scrn) ); +} + +/*! + Draws at most \a len characters of the string \a str at position + \a (x, y). + + \a (x, y) is the base line position. Note that the meaning of \a y + is not the same for the two drawText() varieties. +*/ +void QPainter::drawText( int x, int y, const QString &str, int len, QPainter::TextDirection dir ) +{ + drawText( x, y, str, 0, len, dir ); +} + +/*! + Draws at most \a len characters starting at position \a pos from the + string \a str to position \a (x, y). + + \a (x, y) is the base line position. Note that the meaning of \a y + is not the same for the two drawText() varieties. +*/ +void QPainter::drawText( int x, int y, const QString &str, int pos, int len, QPainter::TextDirection dir ) +{ + if ( !isActive() ) + return; + if (len < 0) + len = str.length() - pos; + if ( len <= 0 || pos >= (int)str.length() ) // empty string + return; + if ( pos + len > (int)str.length() ) + len = str.length() - pos; + + if ( testf(DirtyFont) ) { + updateFont(); + } + + if ( testf(ExtDev) && pdev->devType() != QInternal::Printer ) { + QPDevCmdParam param[3]; + QPoint p(x, y); + QString string = str.mid( pos, len ); + param[0].point = &p; + param[1].str = &string; + param[2].ival = QFont::Latin; + if ( !pdev->cmd(QPaintDevice::PdcDrawText2, this, param) || !hd ) + return; + } + + bool simple = (dir == QPainter::Auto) && str.simpleText(); + // we can't take the complete string here as we would otherwise + // get quadratic behaviour when drawing long strings in parts. + // we do however need some chars around the part we paint to get arabic shaping correct. + // ### maybe possible to remove after cursor restrictions work in QRT + int start; + int end; + if ( simple ) { + start = pos; + end = pos+len; + } else { + start = QMAX( 0, pos - 8 ); + end = QMIN( (int)str.length(), pos + len + 8 ); + } + QConstString cstr( str.unicode() + start, end - start ); + pos -= start; + + QTextEngine engine( cstr.string(), pfont ? pfont->d : cfont.d ); + QTextLayout layout( &engine ); + + // this is actually what beginLayout does. Inlined here, so we can + // avoid the bidi algorithm if we don't need it. + engine.itemize( simple ? QTextEngine::NoBidi|QTextEngine::SingleLine : QTextEngine::Full|QTextEngine::SingleLine ); + engine.currentItem = 0; + engine.firstItemInLine = -1; + + if ( dir != Auto ) { + int level = dir == RTL ? 1 : 0; + for ( int i = engine.items.size(); i >= 0; i-- ) + engine.items[i].analysis.bidiLevel = level; + } + + if ( !simple ) { + layout.setBoundary( pos ); + layout.setBoundary( pos + len ); + } + + // small hack to force skipping of unneeded items + start = 0; + while ( engine.items[start].position < pos ) + ++start; + engine.currentItem = start; + layout.beginLine( 0xfffffff ); + end = start; + while ( !layout.atEnd() && layout.currentItem().from() < pos + len ) { + layout.addCurrentItem(); + end++; + } + QFontMetrics fm(fontMetrics()); + int ascent = fm.ascent(), descent = fm.descent(); + int left, right; + layout.endLine( 0, 0, Qt::SingleLine|Qt::AlignLeft, &ascent, &descent, &left, &right ); + + // do _not_ call endLayout() here, as it would clean up the shaped items and we would do shaping another time + // for painting. + + int textFlags = 0; + if ( cfont.d->underline ) textFlags |= Qt::Underline; + if ( cfont.d->overline ) textFlags |= Qt::Overline; + if ( cfont.d->strikeOut ) textFlags |= Qt::StrikeOut; + + if ( bg_mode == OpaqueMode ) + qt_draw_background( this, x, y-ascent, right-left, ascent+descent+1); + + for ( int i = start; i < end; i++ ) { + QTextItem ti; + ti.item = i; + ti.engine = &engine; + + drawTextItem( x, y - ascent, ti, textFlags ); + } + layout.d = 0; +} + + +/*! \internal + Draws the text item \a ti at position \a (x, y ). + + This method ignores the painters background mode and + color. drawText and qt_format_text have to do it themselves, as + only they know the extents of the complete string. + + It ignores the font set on the painter as the text item has one of its own. + + The underline and strikeout parameters of the text items font are + ignored aswell. You'll need to pass in the correct flags to get + underlining and strikeout. +*/ +void QPainter::drawTextItem( int x, int y, const QTextItem &ti, int textFlags ) +{ + if ( testf(ExtDev) ) { + QPDevCmdParam param[2]; + QPoint p(x, y); + param[0].point = &p; + param[1].textItem = &ti; + bool retval = pdev->cmd(QPaintDevice::PdcDrawTextItem, this, param); + if ( !retval || !hd ) + return; + } + + QTextEngine *engine = ti.engine; + QScriptItem *si = &engine->items[ti.item]; + + engine->shape( ti.item ); + QFontEngine *fe = si->fontEngine; + assert( fe != 0 ); + + x += si->x; + y += si->y; + + fe->draw( this, x, y, engine, si, textFlags ); +} + +#if QT_VERSION >= 0x040000 +#error "remove current position and associated methods" +#endif +/*! + \obsolete + Returns the current position of the pen. + + \sa moveTo() + */ +QPoint QPainter::pos() const +{ + return curPt; +} diff --git a/src/kernel/qpalette.cpp b/src/kernel/qpalette.cpp new file mode 100644 index 0000000..a9e07f7 --- /dev/null +++ b/src/kernel/qpalette.cpp @@ -0,0 +1,1224 @@ +/**************************************************************************** +** +** Implementation of QColorGroup and QPalette classes +** +** Created : 950323 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpalette.h" + +#ifndef QT_NO_PALETTE +#include "qdatastream.h" +#include "qcleanuphandler.h" + +/***************************************************************************** + QColorGroup member functions + *****************************************************************************/ + +/*! + \class QColorGroup qpalette.h + \brief The QColorGroup class contains a group of widget colors. + + \ingroup appearance + \ingroup graphics + \ingroup images + + A color group contains a group of colors used by widgets for + drawing themselves. We recommend that widgets use color group + roles such as "foreground" and "base" rather than literal colors + like "red" or "turquoise". The color roles are enumerated and + defined in the \l ColorRole documentation. + + The most common use of QColorGroup is like this: + + \code + QPainter p; + ... + p.setPen( colorGroup().foreground() ); + p.drawLine( ... ) + \endcode + + It is also possible to modify color groups or create new color + groups from scratch. + + The color group class can be created using three different + constructors or by modifying one supplied by Qt. The default + constructor creates an all-black color group, which can then be + modified using set functions; there's also a constructor for + specifying all the color group colors. And there is also a copy + constructor. + + We strongly recommend using a system-supplied color group and + modifying that as necessary. + + You modify a color group by calling the access functions + setColor() and setBrush(), depending on whether you want a pure + color or a pixmap pattern. + + There are also corresponding color() and brush() getters, and a + commonly used convenience function to get each ColorRole: + background(), foreground(), base(), etc. + + \sa QColor QPalette QWidget::colorGroup() +*/ + + +/*! + \enum QColorGroup::ColorRole + + The ColorRole enum defines the different symbolic color roles used + in current GUIs. + + The central roles are: + + \value Background general background color. + + \value Foreground general foreground color. + + \value Base used as background color for text entry widgets, for example; + usually white or another light color. + + \value Text the foreground color used with \c Base. Usually this + is the same as the \c Foreground, in which case it must provide good + contrast with \c Background and \c Base. + + \value Button general button background color in which buttons need a + background different from \c Background, as in the Macintosh style. + + \value ButtonText a foreground color used with the \c Button color. + + There are some color roles used mostly for 3D bevel and shadow + effects: + + \value Light lighter than \c Button color. + + \value Midlight between \c Button and \c Light. + + \value Dark darker than \c Button. + + \value Mid between \c Button and \c Dark. + + \value Shadow a very dark color. + By default, the shadow color is \c Qt::black. + + All of these are normally derived from \c Background and used in + ways that depend on that relationship. For example, buttons depend + on it to make the bevels look attractive, and Motif scroll bars + depend on \c Mid to be slightly different from \c Background. + + Selected (marked) items have two roles: + + \value Highlight a color to indicate a selected item or the + current item. By default, the highlight color is \c Qt::darkBlue. + + \value HighlightedText a text color that contrasts with \c Highlight. + By default, the highlighted text color is \c Qt::white. + + Finally, there is a special role for text that needs to be + drawn where \c Text or \c Foreground would give poor contrast, + such as on pressed push buttons: + + \value BrightText a text color that is very different from \c + Foreground and contrasts well with e.g. \c Dark. + + \value Link a text color used for unvisited hyperlinks. + By default, the link color is \c Qt::blue. + + \value LinkVisited a text color used for already visited hyperlinks. + By default, the linkvisited color is \c Qt::magenta. + + \value NColorRoles Internal. + + Note that text colors can be used for things other than just + words; text colors are \e usually used for text, but it's quite + common to use the text color roles for lines, icons, etc. + + This image shows most of the color roles in use: + \img palette.png Color Roles +*/ + + +class QColorGroupPrivate : public QShared +{ +public: + QBrush br[QColorGroup::NColorRoles]; + QColorGroupPrivate* detach() { + if ( count > 1 ) { + deref(); + QColorGroupPrivate* d = new QColorGroupPrivate; + for (int i=0; i<QColorGroup::NColorRoles; i++) + d->br[i] = br[i]; + return d; + } + return this; + } +}; + +/*! + Constructs a color group with all colors set to black. +*/ + +QColorGroup::QColorGroup() +{ + static QColorGroupPrivate* defColorGroupData = 0; + if ( !defColorGroupData ) { + static QSharedCleanupHandler<QColorGroupPrivate> defColorGroupCleanup; + defColorGroupData = new QColorGroupPrivate; + defColorGroupCleanup.set( &defColorGroupData ); + } + d = defColorGroupData; + br = d->br; + d->ref(); +} + +/*! + Constructs a color group that is an independent copy of \a other. +*/ +QColorGroup::QColorGroup( const QColorGroup& other ) +{ + d = other.d; + d->ref(); + br = d->br; +} + +/*! + Copies the colors of \a other to this color group. +*/ +QColorGroup& QColorGroup::operator =(const QColorGroup& other) +{ + if ( d != other.d ) { + if ( d->deref() ) + delete d; + d = other.d; + br = d->br; + d->ref(); + } + return *this; +} + +static QColor qt_mix_colors( QColor a, QColor b) +{ + return QColor( (a.red() + b.red()) / 2, (a.green() + b.green()) / 2, (a.blue() + b.blue()) / 2 ); +} + + +/*! + Constructs a color group. You can pass either brushes, pixmaps or + plain colors for \a foreground, \a button, \a light, \a dark, \a + mid, \a text, \a bright_text, \a base and \a background. + + \sa QBrush +*/ + QColorGroup::QColorGroup( const QBrush &foreground, const QBrush &button, + const QBrush &light, const QBrush &dark, + const QBrush &mid, const QBrush &text, + const QBrush &bright_text, const QBrush &base, + const QBrush &background) +{ + d = new QColorGroupPrivate; + br = d->br; + br[Foreground] = foreground; + br[Button] = button; + br[Light] = light; + br[Dark] = dark; + br[Mid] = mid; + br[Text] = text; + br[BrightText] = bright_text; + br[ButtonText] = text; + br[Base] = base; + br[Background] = background; + br[Midlight] = qt_mix_colors( br[Button].color(), br[Light].color() ); + br[Shadow] = Qt::black; + br[Highlight] = Qt::darkBlue; + br[HighlightedText] = Qt::white; + br[Link] = Qt::blue; + br[LinkVisited] = Qt::magenta; +} + + +/*!\obsolete + + Constructs a color group with the specified colors. The button + color will be set to the background color. +*/ + +QColorGroup::QColorGroup( const QColor &foreground, const QColor &background, + const QColor &light, const QColor &dark, + const QColor &mid, + const QColor &text, const QColor &base ) +{ + d = new QColorGroupPrivate; + br = d->br; + br[Foreground] = QBrush(foreground); + br[Button] = QBrush(background); + br[Light] = QBrush(light); + br[Dark] = QBrush(dark); + br[Mid] = QBrush(mid); + br[Text] = QBrush(text); + br[BrightText] = br[Light]; + br[ButtonText] = br[Text]; + br[Base] = QBrush(base); + br[Background] = QBrush(background); + br[Midlight] = qt_mix_colors( br[Button].color(), br[Light].color() ); + br[Shadow] = Qt::black; + br[Highlight] = Qt::darkBlue; + br[HighlightedText] = Qt::white; + br[Link] = Qt::blue; + br[LinkVisited] = Qt::magenta; +} + +/*! + Destroys the color group. +*/ + +QColorGroup::~QColorGroup() +{ + if ( d->deref() ) + delete d; +} + +/*! + Returns the color that has been set for color role \a r. + + \sa brush() ColorRole + */ +const QColor &QColorGroup::color( ColorRole r ) const +{ + return br[r].color(); +} + +/*! + Returns the brush that has been set for color role \a r. + + \sa color() setBrush() ColorRole +*/ +const QBrush &QColorGroup::brush( ColorRole r ) const +{ + return br[r]; +} + +/*! + Sets the brush used for color role \a r to a solid color \a c. + + \sa brush() setColor() ColorRole +*/ +void QColorGroup::setColor( ColorRole r, const QColor &c ) +{ + setBrush( r, QBrush(c) ); +} + +/*! + Sets the brush used for color role \a r to \a b. + + \sa brush() setColor() ColorRole +*/ +void QColorGroup::setBrush( ColorRole r, const QBrush &b ) +{ + d = d->detach(); + br = d->br; + br[r] = b; +} + + +/*! + \fn const QColor & QColorGroup::foreground() const + + Returns the foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::button() const + + Returns the button color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::light() const + + Returns the light color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor& QColorGroup::midlight() const + + Returns the midlight color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::dark() const + + Returns the dark color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::mid() const + + Returns the mid color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::text() const + + Returns the text foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::brightText() const + + Returns the bright text foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::buttonText() const + + Returns the button text foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::base() const + + Returns the base color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::background() const + + Returns the background color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::shadow() const + + Returns the shadow color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::highlight() const + + Returns the highlight color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::highlightedText() const + + Returns the highlighted text color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::link() const + + Returns the unvisited link text color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const QColor & QColorGroup::linkVisited() const + + Returns the visited link text color of the color group. + + \sa ColorRole +*/ + +/*! + \fn bool QColorGroup::operator!=( const QColorGroup &g ) const + + Returns TRUE if this color group is different from \a g; otherwise + returns FALSE. + + \sa operator!=() +*/ + +/*! + Returns TRUE if this color group is equal to \a g; otherwise + returns FALSE. + + \sa operator==() +*/ + +bool QColorGroup::operator==( const QColorGroup &g ) const +{ + if ( d == g.d ) + return TRUE; + for( int r = 0 ; r < NColorRoles ; r++ ) + if ( br[r] != g.br[r] ) + return FALSE; + return TRUE; +} + + +/***************************************************************************** + QPalette member functions + *****************************************************************************/ + +/*! + \class QPalette qpalette.h + + \brief The QPalette class contains color groups for each widget state. + + \ingroup appearance + \ingroup shared + \ingroup graphics + \ingroup images + \mainclass + + A palette consists of three color groups: \e active, \e disabled, + and \e inactive. All widgets contain a palette, and all widgets in + Qt use their palette to draw themselves. This makes the user + interface easily configurable and easier to keep consistent. + + If you create a new widget we strongly recommend that you use the + colors in the palette rather than hard-coding specific colors. + + The color groups: + \list + \i The active() group is used for the window that has keyboard focus. + \i The inactive() group is used for other windows. + \i The disabled() group is used for widgets (not windows) that are + disabled for some reason. + \endlist + + Both active and inactive windows can contain disabled widgets. + (Disabled widgets are often called \e inaccessible or \e{grayed + out}.) + + In Motif style, active() and inactive() look the same. In Windows + 2000 style and Macintosh Platinum style, the two styles look + slightly different. + + There are setActive(), setInactive(), and setDisabled() functions + to modify the palette. (Qt also supports a normal() group; this is + an obsolete alias for active(), supported for backwards + compatibility.) + + Colors and brushes can be set for particular roles in any of a + palette's color groups with setColor() and setBrush(). + + You can copy a palette using the copy constructor and test to see + if two palettes are \e identical using isCopyOf(). + + \sa QApplication::setPalette(), QWidget::setPalette(), QColorGroup, QColor +*/ + +/*! + \enum QPalette::ColorGroup + + \value Disabled + \value Active + \value Inactive + \value NColorGroups + \value Normal synonym for Active +*/ + +/*! + \obsolete + + \fn const QColorGroup &QPalette::normal() const + + Returns the active color group. Use active() instead. + + \sa setActive() active() +*/ + +/*! + \obsolete + + \fn void QPalette::setNormal( const QColorGroup & cg ) + + Sets the active color group to \a cg. Use setActive() instead. + + \sa setActive() active() +*/ + + +static int palette_count = 1; + +/*! + Constructs a palette that consists of color groups with only black + colors. +*/ + +QPalette::QPalette() +{ + static QPalData *defPalData = 0; + if ( !defPalData ) { // create common palette data + defPalData = new QPalData; // for the default palette + static QSharedCleanupHandler<QPalData> defPalCleanup; + defPalCleanup.set( &defPalData ); + defPalData->ser_no = palette_count++; + } + data = defPalData; + data->ref(); +} + +/*!\obsolete + Constructs a palette from the \a button color. The other colors are + automatically calculated, based on this color. Background will be + the button color as well. +*/ + +QPalette::QPalette( const QColor &button ) +{ + data = new QPalData; + Q_CHECK_PTR( data ); + data->ser_no = palette_count++; + QColor bg = button, btn = button, fg, base, disfg; + int h, s, v; + bg.hsv( &h, &s, &v ); + if ( v > 128 ) { // light background + fg = Qt::black; + base = Qt::white; + disfg = Qt::darkGray; + } else { // dark background + fg = Qt::white; + base = Qt::black; + disfg = Qt::darkGray; + } + data->active = QColorGroup( fg, btn, btn.light(150), btn.dark(), + btn.dark(150), fg, Qt::white, base, bg ); + data->disabled = QColorGroup( disfg, btn, btn.light(150), btn.dark(), + btn.dark(150), disfg, Qt::white, base, bg ); + data->inactive = data->active; +} + +/*! + Constructs a palette from a \a button color and a \a background. + The other colors are automatically calculated, based on these + colors. +*/ + +QPalette::QPalette( const QColor &button, const QColor &background ) +{ + data = new QPalData; + Q_CHECK_PTR( data ); + data->ser_no = palette_count++; + QColor bg = background, btn = button, fg, base, disfg; + int h, s, v; + bg.hsv( &h, &s, &v ); + if ( v > 128 ) { // light background + fg = Qt::black; + base = Qt::white; + disfg = Qt::darkGray; + } else { // dark background + fg = Qt::white; + base = Qt::black; + disfg = Qt::darkGray; + } + data->active = QColorGroup( fg, btn, btn.light(150), btn.dark(), + btn.dark(150), fg, Qt::white, base, bg ); + data->disabled = QColorGroup( disfg, btn, btn.light(150), btn.dark(), + btn.dark(150), disfg, Qt::white, base, bg ); + data->inactive = data->active; +} + +/*! + Constructs a palette that consists of the three color groups \a + active, \a disabled and \a inactive. See the \link #details + Detailed Description\endlink for definitions of the color groups + and \l QColorGroup::ColorRole for definitions of each color role + in the three groups. + + \sa QColorGroup QColorGroup::ColorRole QPalette +*/ + +QPalette::QPalette( const QColorGroup &active, const QColorGroup &disabled, + const QColorGroup &inactive ) +{ + data = new QPalData; + Q_CHECK_PTR( data ); + data->ser_no = palette_count++; + data->active = active; + data->disabled = disabled; + data->inactive = inactive; +} + +/*! + Constructs a copy of \a p. + + This constructor is fast (it uses copy-on-write). +*/ + +QPalette::QPalette( const QPalette &p ) +{ + data = p.data; + data->ref(); +} + +/*! + Destroys the palette. +*/ + +QPalette::~QPalette() +{ + if ( data->deref() ) + delete data; +} + +/*! + Assigns \a p to this palette and returns a reference to this + palette. + + This is fast (it uses copy-on-write). + + \sa copy() +*/ + +QPalette &QPalette::operator=( const QPalette &p ) +{ + p.data->ref(); + if ( data->deref() ) + delete data; + data = p.data; + return *this; +} + + +/*! + Returns the color in color group \a gr, used for color role \a r. + + \sa brush() setColor() QColorGroup::ColorRole +*/ +const QColor &QPalette::color( ColorGroup gr, QColorGroup::ColorRole r ) const +{ + return directBrush( gr, r ).color(); +} + +/*! + Returns the brush in color group \a gr, used for color role \a r. + + \sa color() setBrush() QColorGroup::ColorRole +*/ +const QBrush &QPalette::brush( ColorGroup gr, QColorGroup::ColorRole r ) const +{ + return directBrush( gr, r ); +} + +/*! + Sets the brush in color group \a gr, used for color role \a r, to + the solid color \a c. + + \sa setBrush() color() QColorGroup::ColorRole +*/ +void QPalette::setColor( ColorGroup gr, QColorGroup::ColorRole r, + const QColor &c) +{ + setBrush( gr, r, QBrush(c) ); +} + +/*! + Sets the brush in color group \a gr, used for color role \a r, to + \a b. + + \sa brush() setColor() QColorGroup::ColorRole +*/ +void QPalette::setBrush( ColorGroup gr, QColorGroup::ColorRole r, + const QBrush &b) +{ + detach(); + data->ser_no = palette_count++; + directSetBrush( gr, r, b); +} + +/*! + \overload + + Sets the brush color used for color role \a r to color \a c in all + three color groups. + + \sa color() setBrush() QColorGroup::ColorRole +*/ +void QPalette::setColor( QColorGroup::ColorRole r, const QColor &c ) +{ + setBrush( r, QBrush(c) ); +} + +/*! + \overload + + Sets the brush in for color role \a r in all three color groups to + \a b. + + \sa brush() setColor() QColorGroup::ColorRole active() inactive() disabled() +*/ +void QPalette::setBrush( QColorGroup::ColorRole r, const QBrush &b ) +{ + detach(); + data->ser_no = palette_count++; + directSetBrush( Active, r, b ); + directSetBrush( Disabled, r, b ); + directSetBrush( Inactive, r, b ); +} + + +/*! + Returns a deep copy of this palette. + + \warning This is slower than the copy constructor and assignment + operator and offers no benefits. +*/ + +QPalette QPalette::copy() const +{ + QPalette p( data->active, data->disabled, data->inactive ); + return p; +} + + +/*! + Detaches this palette from any other QPalette objects with which + it might implicitly share QColorGroup objects. In essence, does + the copying part of copy-on-write. + + Calling this should generally not be necessary; QPalette calls it + itself when necessary. +*/ + +void QPalette::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + +/*! + \fn const QColorGroup & QPalette::disabled() const + + Returns the disabled color group of this palette. + + \sa QColorGroup, setDisabled(), active(), inactive() +*/ + +/*! + Sets the \c Disabled color group to \a g. + + \sa disabled() setActive() setInactive() +*/ + +void QPalette::setDisabled( const QColorGroup &g ) +{ + detach(); + data->ser_no = palette_count++; + data->disabled = g; +} + +/*! + \fn const QColorGroup & QPalette::active() const + + Returns the active color group of this palette. + + \sa QColorGroup, setActive(), inactive(), disabled() +*/ + +/*! + Sets the \c Active color group to \a g. + + \sa active() setDisabled() setInactive() QColorGroup +*/ + +void QPalette::setActive( const QColorGroup &g ) +{ + detach(); + data->ser_no = palette_count++; + data->active = g; +} + +/*! + \fn const QColorGroup & QPalette::inactive() const + + Returns the inactive color group of this palette. + + \sa QColorGroup, setInactive(), active(), disabled() +*/ + +/*! + Sets the \c Inactive color group to \a g. + + \sa active() setDisabled() setActive() QColorGroup +*/ + +void QPalette::setInactive( const QColorGroup &g ) +{ + detach(); + data->ser_no = palette_count++; + data->inactive = g; +} + + +/*! + \fn bool QPalette::operator!=( const QPalette &p ) const + + Returns TRUE (slowly) if this palette is different from \a p; + otherwise returns FALSE (usually quickly). +*/ + +/*! + Returns TRUE (usually quickly) if this palette is equal to \a p; + otherwise returns FALSE (slowly). +*/ + +bool QPalette::operator==( const QPalette &p ) const +{ + return data->active == p.data->active && + data->disabled == p.data->disabled && + data->inactive == p.data->inactive; +} + + +/*! + \fn int QPalette::serialNumber() const + + Returns a number that uniquely identifies this QPalette object. + The serial number is intended for caching. Its value may not be + used for anything other than equality testing. + + Note that QPalette uses copy-on-write, and the serial number + changes during the lazy copy operation (detach()), not during a + shallow copy (copy constructor or assignment). + + \sa QPixmap QPixmapCache QCache +*/ + + +/***************************************************************************** + QColorGroup/QPalette stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +/*! + \relates QColorGroup + + Writes color group, \a g to the stream \a s. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QColorGroup &g ) +{ + if ( s.version() == 1 ) { + // Qt 1.x + s << g.foreground() + << g.background() + << g.light() + << g.dark() + << g.mid() + << g.text() + << g.base(); + } else { + int max = QColorGroup::NColorRoles; + if ( s.version() <= 3) // Qt 2.x + max = 14; + + for( int r = 0 ; r < max ; r++ ) + s << g.brush( (QColorGroup::ColorRole)r); + } + return s; +} + +/*! + \related QColorGroup + + Reads a color group from the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QColorGroup &g ) +{ + if ( s.version() == 1 ) { + // Qt 1.x + QColor fg, bg, light, dark, mid, text, base; + s >> fg >> bg >> light >> dark >> mid >> text >> base; + QPalette p( bg ); + QColorGroup n( p.active() ); + n.setColor( QColorGroup::Foreground, fg ); + n.setColor( QColorGroup::Light, light ); + n.setColor( QColorGroup::Dark, dark ); + n.setColor( QColorGroup::Mid, mid ); + n.setColor( QColorGroup::Text, text ); + n.setColor( QColorGroup::Base, base ); + g = n; + } else { + int max = QColorGroup::NColorRoles; + if (s.version() <= 3) // Qt 2.x + max = 14; + + QBrush tmp; + for( int r = 0 ; r < max; r++ ) { + s >> tmp; + g.setBrush( (QColorGroup::ColorRole)r, tmp); + } + } + return s; +} + + +/*! + \relates QPalette + + Writes the palette, \a p to the stream \a s and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QPalette &p ) +{ + return s << p.active() + << p.disabled() + << p.inactive(); +} + + +static void readV1ColorGroup( QDataStream &s, QColorGroup &g, + QPalette::ColorGroup r ) +{ + QColor fg, bg, light, dark, mid, text, base; + s >> fg >> bg >> light >> dark >> mid >> text >> base; + QPalette p( bg ); + QColorGroup n; + switch ( r ) { + case QPalette::Disabled: + n = p.disabled(); + break; + case QPalette::Inactive: + n = p.inactive(); + break; + default: + n = p.active(); + break; + } + n.setColor( QColorGroup::Foreground, fg ); + n.setColor( QColorGroup::Light, light ); + n.setColor( QColorGroup::Dark, dark ); + n.setColor( QColorGroup::Mid, mid ); + n.setColor( QColorGroup::Text, text ); + n.setColor( QColorGroup::Base, base ); + g = n; +} + + +/*! + \relates QPalette + + Reads a palette from the stream, \a s into the palette \a p, and + returns a reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QPalette &p ) +{ + QColorGroup active, disabled, inactive; + if ( s.version() == 1 ) { + readV1ColorGroup( s, active, QPalette::Active ); + readV1ColorGroup( s, disabled, QPalette::Disabled ); + readV1ColorGroup( s, inactive, QPalette::Inactive ); + } else { + s >> active >> disabled >> inactive; + } + QPalette newpal( active, disabled, inactive ); + p = newpal; + return s; +} +#endif //QT_NO_DATASTREAM + +/*! + Returns TRUE if this palette and \a p are copies of each other, + i.e. one of them was created as a copy of the other and neither + was subsequently modified; otherwise returns FALSE. This is much + stricter than equality. + + \sa operator=() operator==() +*/ + +bool QPalette::isCopyOf( const QPalette & p ) +{ + return data && data == p.data; +} + +const QBrush &QPalette::directBrush( ColorGroup gr, QColorGroup::ColorRole r ) const +{ + if ( (uint)gr > (uint)QPalette::NColorGroups ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPalette::directBrush: colorGroup(%i) out of range", gr ); +#endif + return data->active.br[QColorGroup::Foreground]; + } + if ( (uint)r >= (uint)QColorGroup::NColorRoles ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPalette::directBrush: colorRole(%i) out of range", r ); +#endif + return data->active.br[QColorGroup::Foreground]; + } + switch( gr ) { + case Active: + return data->active.br[r]; + //break; + case Disabled: + return data->disabled.br[r]; + //break; + case Inactive: + return data->inactive.br[r]; + //break; + default: + break; + } +#if defined(QT_CHECK_RANGE) + qWarning( "QPalette::directBrush: colorGroup(%i) internal error", gr ); +#endif + return data->active.br[QColorGroup::Foreground]; // Satisfy compiler +} + +void QPalette::directSetBrush( ColorGroup gr, QColorGroup::ColorRole r, const QBrush& b) +{ + if ( (uint)gr > (uint)QPalette::NColorGroups ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPalette::directBrush: colorGroup(%i) out of range", gr ); +#endif + return; + } + if ( (uint)r >= (uint)QColorGroup::NColorRoles ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPalette::directBrush: colorRole(%i) out of range", r ); +#endif + return; + } + switch( gr ) { + case Active: + data->active.setBrush(r,b); + break; + case Disabled: + data->disabled.setBrush(r,b); + break; + case Inactive: + data->inactive.setBrush(r,b); + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "QPalette::directBrush: colorGroup(%i) internal error", gr ); +#endif + break; + } +} + + +/*!\internal*/ +QColorGroup::ColorRole QPalette::foregroundRoleFromMode( Qt::BackgroundMode mode ) +{ + switch (mode) { + case Qt::PaletteButton: + return QColorGroup::ButtonText; + case Qt::PaletteBase: + return QColorGroup::Text; + case Qt::PaletteDark: + case Qt::PaletteShadow: + return QColorGroup::Light; + case Qt::PaletteHighlight: + return QColorGroup::HighlightedText; + case Qt::PaletteBackground: + default: + return QColorGroup::Foreground; + } +} + +/*!\internal*/ +QColorGroup::ColorRole QPalette::backgroundRoleFromMode( Qt::BackgroundMode mode) +{ + switch (mode) { + case Qt::PaletteForeground: + return QColorGroup::Foreground; + case Qt::PaletteButton: + return QColorGroup::Button; + case Qt::PaletteLight: + return QColorGroup::Light; + case Qt::PaletteMidlight: + return QColorGroup::Midlight; + case Qt::PaletteDark: + return QColorGroup::Dark; + case Qt::PaletteMid: + return QColorGroup::Mid; + case Qt::PaletteText: + return QColorGroup::Text; + case Qt::PaletteBrightText: + return QColorGroup::BrightText; + case Qt::PaletteButtonText: + return QColorGroup::ButtonText; + case Qt::PaletteBase: + return QColorGroup::Base; + case Qt::PaletteShadow: + return QColorGroup::Shadow; + case Qt::PaletteHighlight: + return QColorGroup::Highlight; + case Qt::PaletteHighlightedText: + return QColorGroup::HighlightedText; + case Qt::PaletteLink: + return QColorGroup::Link; + case Qt::PaletteLinkVisited: + return QColorGroup::LinkVisited; + case Qt::PaletteBackground: + default: + return QColorGroup::Background; + } +} + +#endif // QT_NO_PALETTE diff --git a/src/kernel/qpalette.h b/src/kernel/qpalette.h new file mode 100644 index 0000000..90399cd --- /dev/null +++ b/src/kernel/qpalette.h @@ -0,0 +1,189 @@ +/**************************************************************************** +** +** Definition of QColorGroup and QPalette classes +** +** Created : 950323 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPALETTE_H +#define QPALETTE_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qcolor.h" +#include "qshared.h" +#include "qbrush.h" // QColor->QBrush conversion +#endif // QT_H + +#ifndef QT_NO_PALETTE + +class QColorGroupPrivate; + +class Q_EXPORT QColorGroup +{ +public: + QColorGroup(); + QColorGroup( const QColor &foreground, const QColor &button, + const QColor &light, const QColor &dark, const QColor &mid, + const QColor &text, const QColor &base ); + QColorGroup( const QBrush &foreground, const QBrush &button, + const QBrush &light, const QBrush &dark, const QBrush &mid, + const QBrush &text, const QBrush &bright_text, + const QBrush &base, const QBrush &background); + QColorGroup( const QColorGroup & ); + + ~QColorGroup(); + + QColorGroup& operator =(const QColorGroup&); + + // Do not change the order, the serialization format depends on it + enum ColorRole { Foreground, Button, Light, Midlight, Dark, Mid, + Text, BrightText, ButtonText, Base, Background, Shadow, + Highlight, HighlightedText, Link, LinkVisited, + NColorRoles }; + + const QColor &color( ColorRole ) const; + const QBrush &brush( ColorRole ) const; + void setColor( ColorRole, const QColor & ); + void setBrush( ColorRole, const QBrush & ); + + const QColor &foreground() const { return br[Foreground].color(); } + const QColor &button() const { return br[Button].color(); } + const QColor &light() const { return br[Light].color(); } + const QColor &dark() const { return br[Dark].color(); } + const QColor &mid() const { return br[Mid].color(); } + const QColor &text() const { return br[Text].color(); } + const QColor &base() const { return br[Base].color(); } + const QColor &background() const { return br[Background].color(); } + + const QColor &midlight() const { return br[Midlight].color(); } + const QColor &brightText() const { return br[BrightText].color(); } + const QColor &buttonText() const { return br[ButtonText].color(); } + const QColor &shadow() const { return br[Shadow].color(); } + const QColor &highlight() const { return br[Highlight].color(); } + const QColor &highlightedText() const{return br[HighlightedText].color(); } + const QColor &link() const { return br[Link].color(); } + const QColor &linkVisited() const { return br[LinkVisited].color(); } + + bool operator==( const QColorGroup &g ) const; + bool operator!=( const QColorGroup &g ) const + { return !(operator==(g)); } + +private: + QBrush *br; + QColorGroupPrivate * d; + + friend class QPalette; +}; + + +class Q_EXPORT QPalette +{ +public: + QPalette(); + QPalette( const QColor &button ); + QPalette( const QColor &button, const QColor &background ); + QPalette( const QColorGroup &active, const QColorGroup &disabled, + const QColorGroup &inactive ); + QPalette( const QPalette & ); + ~QPalette(); + QPalette &operator=( const QPalette & ); + + enum ColorGroup { Disabled, Active, Inactive, NColorGroups, Normal=Active }; + + const QColor &color( ColorGroup, QColorGroup::ColorRole ) const; + const QBrush &brush( ColorGroup, QColorGroup::ColorRole ) const; + void setColor( ColorGroup, QColorGroup::ColorRole, const QColor & ); + void setBrush( ColorGroup, QColorGroup::ColorRole, const QBrush & ); + + void setColor( QColorGroup::ColorRole, const QColor & ); + void setBrush( QColorGroup::ColorRole, const QBrush & ); + + QPalette copy() const; + + const QColorGroup &active() const { return data->active; } + const QColorGroup &disabled() const { return data->disabled; } + const QColorGroup &inactive() const { return data->inactive; } +#ifndef QT_NO_COMPAT + const QColorGroup &normal() const { return active(); } +#endif + + void setActive( const QColorGroup & ); + void setDisabled( const QColorGroup & ); + void setInactive( const QColorGroup & ); +#ifndef QT_NO_COMPAT + void setNormal( const QColorGroup & cg ) { setActive(cg); } +#endif + + bool operator==( const QPalette &p ) const; + bool operator!=( const QPalette &p ) const + { return !(operator==(p)); } + bool isCopyOf( const QPalette & ); + + int serialNumber() const { return data->ser_no; } + + + static QColorGroup::ColorRole foregroundRoleFromMode( Qt::BackgroundMode mode ); + static QColorGroup::ColorRole backgroundRoleFromMode( Qt::BackgroundMode mode); + +private: + void detach(); + const QBrush &directBrush( ColorGroup, QColorGroup::ColorRole ) const; + void directSetBrush( ColorGroup, QColorGroup::ColorRole, const QBrush& ); + + struct QPalData : public QShared { + QColorGroup disabled; + QColorGroup active; + int ser_no; + QColorGroup inactive; + } *data; +}; + + +/***************************************************************************** + QColorGroup/QPalette stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QColorGroup & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QColorGroup & ); + +Q_EXPORT QDataStream &operator<<( QDataStream &, const QPalette & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QPalette & ); +#endif // QT_NO_DATASTREAM + +#endif // QT_NO_PALETTE +#endif // QPALETTE_H diff --git a/src/kernel/qpen.h b/src/kernel/qpen.h new file mode 100644 index 0000000..ba77943 --- /dev/null +++ b/src/kernel/qpen.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Definition of QPen class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPEN_H +#define QPEN_H + +#ifndef QT_H +#include "qcolor.h" +#include "qshared.h" +#endif // QT_H + + +class Q_EXPORT QPen: public Qt +{ +public: + QPen(); + QPen( PenStyle ); + QPen( const QColor &color, uint width=0, PenStyle style=SolidLine ); + QPen( const QColor &cl, uint w, PenStyle s, PenCapStyle c, PenJoinStyle j); + QPen( const QPen & ); + ~QPen(); + QPen &operator=( const QPen & ); + + PenStyle style() const { return data->style; } + void setStyle( PenStyle ); + uint width() const { return data->width; } + void setWidth( uint ); + const QColor &color() const { return data->color; } + void setColor( const QColor & ); + PenCapStyle capStyle() const; + void setCapStyle( PenCapStyle ); + PenJoinStyle joinStyle() const; + void setJoinStyle( PenJoinStyle ); + + bool operator==( const QPen &p ) const; + bool operator!=( const QPen &p ) const + { return !(operator==(p)); } + +private: + friend class QPainter; +#ifdef Q_WS_WIN + friend class QFontEngineWin; +#endif + + QPen copy() const; + void detach(); + void init( const QColor &, uint, uint ); + struct QPenData : public QShared { // pen data + PenStyle style; + uint width; + QColor color; + Q_UINT16 linest; + } *data; +}; + + +/***************************************************************************** + QPen stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QPen & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QPen & ); +#endif + +#endif // QPEN_H diff --git a/src/kernel/qpicture.cpp b/src/kernel/qpicture.cpp new file mode 100644 index 0000000..d434516 --- /dev/null +++ b/src/kernel/qpicture.cpp @@ -0,0 +1,1229 @@ +/**************************************************************************** +** +** Implementation of QPicture class +** +** Created : 940802 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpicture.h" + +#ifndef QT_NO_PICTURE + +#include "qpainter.h" +#include "qpixmap.h" +#include "qimage.h" +#include "qfile.h" +#include "qdatastream.h" +#include "qpaintdevicemetrics.h" + +#ifndef QT_NO_SVG +#include "private/qsvgdevice_p.h" +#endif + +/*! + \class QPicture qpicture.h + \brief The QPicture class is a paint device that records and + replays QPainter commands. + + \ingroup graphics + \ingroup images + \ingroup shared + + A picture serializes painter commands to an IO device in a + platform-independent format. For example, a picture created under + Windows can be read on a Sun SPARC. + + Pictures are called meta-files on some platforms. + + Qt pictures use a proprietary binary format. Unlike native picture + (meta-file) formats on many window systems, Qt pictures have no + limitations regarding their contents. Everything that can be + painted can also be stored in a picture, e.g. fonts, pixmaps, + regions, transformed graphics, etc. + + QPicture is an \link shclass.html implicitly shared\endlink class. + + Example of how to record a picture: + \code + QPicture pic; + QPainter p; + p.begin( &pic ); // paint in picture + p.drawEllipse( 10,20, 80,70 ); // draw an ellipse + p.end(); // painting done + pic.save( "drawing.pic" ); // save picture + \endcode + + Example of how to replay a picture: + \code + QPicture pic; + pic.load( "drawing.pic" ); // load picture + QPainter p; + p.begin( &myWidget ); // paint in myWidget + p.drawPicture( pic ); // draw the picture + p.end(); // painting done + \endcode + + Pictures can also be drawn using play(). Some basic data about a + picture is available, for example, size(), isNull() and + boundingRect(). + +*/ + + +static const char *mfhdr_tag = "QPIC"; // header tag +static const Q_UINT16 mfhdr_maj = 5; // major version # +static const Q_UINT16 mfhdr_min = 0; // minor version # + + +/*! + Constructs an empty picture. + + The \a formatVersion parameter may be used to \e create a QPicture + that can be read by applications that are compiled with earlier + versions of Qt. + \list + \i \a formatVersion == 1 is binary compatible with Qt 1.x and later. + \i \a formatVersion == 2 is binary compatible with Qt 2.0.x and later. + \i \a formatVersion == 3 is binary compatible with Qt 2.1.x and later. + \i \a formatVersion == 4 is binary compatible with Qt 3.0.x and later. + \i \a formatVersion == 5 is binary compatible with Qt 3.1. + \endlist + + Note that the default formatVersion is -1 which signifies the + current release, i.e. for Qt 3.1 a formatVersion of 5 is the same + as the default formatVersion of -1. + + Reading pictures generated by earlier versions of Qt is supported + and needs no special coding; the format is automatically detected. +*/ + +QPicture::QPicture( int formatVersion ) + : QPaintDevice( QInternal::Picture | QInternal::ExternalDevice ) + // set device type +{ + d = new QPicturePrivate; + +#if defined(QT_CHECK_RANGE) + if ( formatVersion == 0 ) + qWarning( "QPicture: invalid format version 0" ); +#endif + + // still accept the 0 default from before Qt 3.0. + if ( formatVersion > 0 && formatVersion != (int)mfhdr_maj ) { + d->formatMajor = formatVersion; + d->formatMinor = 0; + d->formatOk = FALSE; + } + else { + d->resetFormat(); + } +} + +/*! + Constructs a \link shclass.html shallow copy\endlink of \a pic. +*/ + +QPicture::QPicture( const QPicture &pic ) + : QPaintDevice( QInternal::Picture | QInternal::ExternalDevice ) +{ + d = pic.d; + d->ref(); +} + +/*! + Destroys the picture. +*/ +QPicture::~QPicture() +{ + if ( d->deref() ) + delete d; +} + + +/*! + \fn bool QPicture::isNull() const + + Returns TRUE if the picture contains no data; otherwise returns + FALSE. +*/ + +/*! + \fn uint QPicture::size() const + + Returns the size of the picture data. + + \sa data() +*/ + +/*! + \fn const char* QPicture::data() const + + Returns a pointer to the picture data. The pointer is only valid + until the next non-const function is called on this picture. The + returned pointer is 0 if the picture contains no data. + + \sa size(), isNull() +*/ + +/*! + Sets the picture data directly from \a data and \a size. This + function copies the input data. + + \sa data(), size() +*/ + +void QPicture::setData( const char* data, uint size ) +{ + detach(); + QByteArray a( size ); + memcpy( a.data(), data, size ); + d->pictb.setBuffer( a ); // set byte array in buffer + d->resetFormat(); // we'll have to check +} + + +/*! + Loads a picture from the file specified by \a fileName and returns + TRUE if successful; otherwise returns FALSE. + + By default, the file will be interpreted as being in the native + QPicture format. Specifying the \a format string is optional and + is only needed for importing picture data stored in a different + format. + + Currently, the only external format supported is the \link + http://www.w3.org/Graphics/SVG/ W3C SVG \endlink format which + requires the \link xml.html Qt XML module \endlink. The + corresponding \a format string is "svg". + + \sa save() +*/ + +bool QPicture::load( const QString &fileName, const char *format ) +{ + QFile f( fileName ); + if ( !f.open(IO_ReadOnly) ) + return FALSE; + return load( &f, format ); +} + +/*! + \overload + + \a dev is the device to use for loading. +*/ + +bool QPicture::load( QIODevice *dev, const char *format ) +{ +#ifndef QT_NO_SVG + if ( qstrcmp( format, "svg" ) == 0 ) { + QSvgDevice svg; + if ( !svg.load( dev ) ) + return FALSE; + QPainter p( this ); + bool b = svg.play( &p ); + d->brect = svg.boundingRect(); + return b; + } +#endif + if ( format ) { + qWarning( "QPicture::load: No such picture format: %s", format ); + return FALSE; + } + + detach(); + QByteArray a = dev->readAll(); + d->pictb.setBuffer( a ); // set byte array in buffer + return d->checkFormat(); +} + +/*! + Saves a picture to the file specified by \a fileName and returns + TRUE if successful; otherwise returns FALSE. + + Specifying the file \a format string is optional. It's not + recommended unless you intend to export the picture data for + use by a third party reader. By default the data will be saved in + the native QPicture file format. + + Currently, the only external format supported is the \link + http://www.w3.org/Graphics/SVG/ W3C SVG \endlink format which + requires the \link xml.html Qt XML module \endlink. The + corresponding \a format string is "svg". + + \sa load() +*/ + +bool QPicture::save( const QString &fileName, const char *format ) +{ + if ( paintingActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPicture::save: still being painted on. " + "Call QPainter::end() first" ); +#endif + return FALSE; + } + +#ifndef QT_NO_SVG + // identical to QIODevice* code below but the file name + // makes a difference when it comes to saving pixmaps + if ( qstricmp( format, "svg" ) == 0 ) { + QSvgDevice svg; + QPainter p( &svg ); + if ( !play( &p ) ) + return FALSE; + svg.setBoundingRect( boundingRect() ); + return svg.save( fileName ); + } +#endif + + QFile f( fileName ); + if ( !f.open(IO_WriteOnly) ) + return FALSE; + return save( &f, format ); +} + +/*! + \overload + + \a dev is the device to use for saving. +*/ + +bool QPicture::save( QIODevice *dev, const char *format ) +{ + if ( paintingActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPicture::save: still being painted on. " + "Call QPainter::end() first" ); +#endif + return FALSE; + } + +#ifndef QT_NO_SVG + if ( qstricmp( format, "svg" ) == 0 ) { + QSvgDevice svg; + QPainter p( &svg ); + if ( !play( &p ) ) + return FALSE; + svg.setBoundingRect( boundingRect() ); + return svg.save( dev ); + } +#endif + if ( format ) { + qWarning( "QPicture::save: No such picture format: %s", format ); + return FALSE; + } + + dev->writeBlock( d->pictb.buffer().data(), d->pictb.buffer().size() ); + return TRUE; +} + +/*! + Returns the picture's bounding rectangle or an invalid rectangle + if the picture contains no data. +*/ + +QRect QPicture::boundingRect() const +{ + if ( !d->formatOk ) + d->checkFormat(); + return d->brect; +} + +/*! + Sets the picture's bounding rectangle to \a r. The automatically + calculated value is overriden. +*/ + +void QPicture::setBoundingRect( const QRect &r ) +{ + if ( !d->formatOk ) + d->checkFormat(); + d->brect = r; +} + +/*! + Replays the picture using \a painter, and returns TRUE if + successful; otherwise returns FALSE. + + This function does exactly the same as QPainter::drawPicture() + with (x, y) = (0, 0). +*/ + +bool QPicture::play( QPainter *painter ) +{ + if ( d->pictb.size() == 0 ) // nothing recorded + return TRUE; + + if ( !d->formatOk && !d->checkFormat() ) + return FALSE; + + d->pictb.open( IO_ReadOnly ); // open buffer device + QDataStream s; + s.setDevice( &d->pictb ); // attach data stream to buffer + s.device()->at( 10 ); // go directly to the data + s.setVersion( d->formatMajor == 4 ? 3 : d->formatMajor ); + + Q_UINT8 c, clen; + Q_UINT32 nrecords; + s >> c >> clen; + Q_ASSERT( c == PdcBegin ); + // bounding rect was introduced in ver 4. Read in checkFormat(). + if ( d->formatMajor >= 4 ) { + Q_INT32 dummy; + s >> dummy >> dummy >> dummy >> dummy; + } + s >> nrecords; + if ( !exec( painter, s, nrecords ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPicture::play: Format error" ); +#endif + d->pictb.close(); + return FALSE; + } + d->pictb.close(); + return TRUE; // no end-command +} + + +/*! + \internal + Iterates over the internal picture data and draws the picture using + \a painter. +*/ + +bool QPicture::exec( QPainter *painter, QDataStream &s, int nrecords ) +{ +#if defined(QT_DEBUG) + int strm_pos; +#endif + Q_UINT8 c; // command id + Q_UINT8 tiny_len; // 8-bit length descriptor + Q_INT32 len; // 32-bit length descriptor + Q_INT16 i_16, i1_16, i2_16; // parameters... + Q_INT8 i_8; + Q_UINT32 ul; + QCString str1; + QString str; + QPoint p, p1, p2; + QRect r; + QPointArray a; + QColor color; + QFont font; + QPen pen; + QBrush brush; + QRegion rgn; +#ifndef QT_NO_TRANSFORMATIONS + QWMatrix matrix; +#endif + + while ( nrecords-- && !s.eof() ) { + s >> c; // read cmd + s >> tiny_len; // read param length + if ( tiny_len == 255 ) // longer than 254 bytes + s >> len; + else + len = tiny_len; +#if defined(QT_DEBUG) + strm_pos = s.device()->at(); +#endif + switch ( c ) { // exec cmd + case PdcNOP: + break; + case PdcDrawPoint: + s >> p; + painter->drawPoint( p ); + break; + case PdcMoveTo: + s >> p; + painter->moveTo( p ); + break; + case PdcLineTo: + s >> p; + painter->lineTo( p ); + break; + case PdcDrawLine: + s >> p1 >> p2; + painter->drawLine( p1, p2 ); + break; + case PdcDrawRect: + s >> r; + painter->drawRect( r ); + break; + case PdcDrawRoundRect: + s >> r >> i1_16 >> i2_16; + painter->drawRoundRect( r, i1_16, i2_16 ); + break; + case PdcDrawEllipse: + s >> r; + painter->drawEllipse( r ); + break; + case PdcDrawArc: + s >> r >> i1_16 >> i2_16; + painter->drawArc( r, i1_16, i2_16 ); + break; + case PdcDrawPie: + s >> r >> i1_16 >> i2_16; + painter->drawPie( r, i1_16, i2_16 ); + break; + case PdcDrawChord: + s >> r >> i1_16 >> i2_16; + painter->drawChord( r, i1_16, i2_16 ); + break; + case PdcDrawLineSegments: + s >> a; + painter->drawLineSegments( a ); + break; + case PdcDrawPolyline: + s >> a; + painter->drawPolyline( a ); + break; + case PdcDrawPolygon: + s >> a >> i_8; + painter->drawPolygon( a, i_8 ); + break; + case PdcDrawCubicBezier: + s >> a; +#ifndef QT_NO_BEZIER + painter->drawCubicBezier( a ); +#endif + break; + case PdcDrawText: + s >> p >> str1; + painter->drawText( p, str1 ); + break; + case PdcDrawTextFormatted: + s >> r >> i_16 >> str1; + painter->drawText( r, i_16, str1 ); + break; + case PdcDrawText2: + s >> p >> str; + painter->drawText( p, str ); + break; + case PdcDrawText2Formatted: + s >> r >> i_16 >> str; + painter->drawText( r, i_16, str ); + break; + case PdcDrawPixmap: { + QPixmap pixmap; + if ( d->formatMajor < 4 ) { + s >> p >> pixmap; + painter->drawPixmap( p, pixmap ); + } else { + s >> r >> pixmap; + painter->drawPixmap( r, pixmap ); + } + } + break; + case PdcDrawImage: { + QImage image; + if ( d->formatMajor < 4 ) { + s >> p >> image; + painter->drawImage( p, image ); + } else { + s >> r >> image; + painter->drawImage( r, image ); + } + } + break; + case PdcBegin: + s >> ul; // number of records + if ( !exec( painter, s, ul ) ) + return FALSE; + break; + case PdcEnd: + if ( nrecords == 0 ) + return TRUE; + break; + case PdcSave: + painter->save(); + break; + case PdcRestore: + painter->restore(); + break; + case PdcSetBkColor: + s >> color; + painter->setBackgroundColor( color ); + break; + case PdcSetBkMode: + s >> i_8; + painter->setBackgroundMode( (Qt::BGMode)i_8 ); + break; + case PdcSetROP: + s >> i_8; + painter->setRasterOp( (Qt::RasterOp)i_8 ); + break; + case PdcSetBrushOrigin: + s >> p; + painter->setBrushOrigin( p ); + break; + case PdcSetFont: + s >> font; + painter->setFont( font ); + break; + case PdcSetPen: + s >> pen; + painter->setPen( pen ); + break; + case PdcSetBrush: + s >> brush; + painter->setBrush( brush ); + break; + case PdcSetTabStops: + s >> i_16; + painter->setTabStops( i_16 ); + break; + case PdcSetTabArray: + s >> i_16; + if ( i_16 == 0 ) { + painter->setTabArray( 0 ); + } else { + int *ta = new int[i_16]; + Q_CHECK_PTR( ta ); + for ( int i=0; i<i_16; i++ ) { + s >> i1_16; + ta[i] = i1_16; + } + painter->setTabArray( ta ); + delete [] ta; + } + break; + case PdcSetVXform: + s >> i_8; +#ifndef QT_NO_TRANSFORMATIONS + painter->setViewXForm( i_8 ); +#endif + break; + case PdcSetWindow: + s >> r; +#ifndef QT_NO_TRANSFORMATIONS + painter->setWindow( r ); +#endif + break; + case PdcSetViewport: + s >> r; +#ifndef QT_NO_TRANSFORMATIONS + painter->setViewport( r ); +#endif + break; + case PdcSetWXform: + s >> i_8; +#ifndef QT_NO_TRANSFORMATIONS + painter->setWorldXForm( i_8 ); +#endif + break; + case PdcSetWMatrix: +#ifndef QT_NO_TRANSFORMATIONS // #### fix me! + s >> matrix >> i_8; + painter->setWorldMatrix( matrix, i_8 ); +#endif + break; +#ifndef QT_NO_TRANSFORMATIONS + case PdcSaveWMatrix: + painter->saveWorldMatrix(); + break; + case PdcRestoreWMatrix: + painter->restoreWorldMatrix(); + break; +#endif + case PdcSetClip: + s >> i_8; + painter->setClipping( i_8 ); + break; + case PdcSetClipRegion: + s >> rgn >> i_8; + painter->setClipRegion( rgn, (QPainter::CoordinateMode)i_8 ); + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "QPicture::play: Invalid command %d", c ); +#endif + if ( len ) // skip unknown command + s.device()->at( s.device()->at()+len ); + } +#if defined(QT_DEBUG) + //qDebug( "device->at(): %i, strm_pos: %i len: %i", s.device()->at(), strm_pos, len ); + Q_ASSERT( Q_INT32(s.device()->at() - strm_pos) == len ); +#endif + } + return FALSE; +} + + +/*! + \internal + Records painter commands and stores them in the pictb buffer. +*/ + +bool QPicture::cmd( int c, QPainter *pt, QPDevCmdParam *p ) +{ + detach(); + return d->cmd( c, pt, p ); +} + +/*! + \internal + Implementation of the function forwarded above to the internal data struct. +*/ + +bool QPicture::QPicturePrivate::cmd( int c, QPainter *pt, QPDevCmdParam *p ) +{ + QDataStream s; + s.setDevice( &pictb ); + // when moving up to 4 the QDataStream version remained at 3 + s.setVersion( formatMajor != 4 ? formatMajor : 3 ); + if ( c == PdcBegin ) { // begin; write header + QByteArray empty( 0 ); + pictb.setBuffer( empty ); // reset byte array in buffer + pictb.open( IO_WriteOnly ); + s.writeRawBytes( mfhdr_tag, 4 ); + s << (Q_UINT16)0 << (Q_UINT16)formatMajor << (Q_UINT16)formatMinor; + s << (Q_UINT8)c << (Q_UINT8)sizeof(Q_INT32); + brect = QRect(); + if ( formatMajor >= 4 ) { + s << (Q_INT32)brect.left() << (Q_INT32)brect.top() + << (Q_INT32)brect.width() << (Q_INT32)brect.height(); + } + trecs = 0; + s << (Q_UINT32)trecs; // total number of records + formatOk = FALSE; + return TRUE; + } else if ( c == PdcEnd ) { // end; calc checksum and close + trecs++; + s << (Q_UINT8)c << (Q_UINT8)0; + QByteArray buf = pictb.buffer(); + int cs_start = sizeof(Q_UINT32); // pos of checksum word + int data_start = cs_start + sizeof(Q_UINT16); + int brect_start = data_start + 2*sizeof(Q_INT16) + 2*sizeof(Q_UINT8); + int pos = pictb.at(); + pictb.at( brect_start ); + if ( formatMajor >= 4 ) { // bounding rectangle + s << (Q_INT32)brect.left() << (Q_INT32)brect.top() + << (Q_INT32)brect.width() << (Q_INT32)brect.height(); + } + s << (Q_UINT32)trecs; // write number of records + pictb.at( cs_start ); + Q_UINT16 cs = (Q_UINT16)qChecksum( buf.data()+data_start, pos-data_start ); + s << cs; // write checksum + pictb.close(); + return TRUE; + } + trecs++; + s << (Q_UINT8)c; // write cmd to stream + s << (Q_UINT8)0; // write dummy length info + int pos = (int)pictb.at(); // save position + QRect br; // bounding rect addition + bool corr = FALSE; // correction for pen width + + switch ( c ) { + case PdcDrawPoint: + case PdcMoveTo: + case PdcLineTo: + case PdcSetBrushOrigin: + s << *p[0].point; + br = QRect( *p[0].point, QSize( 1, 1 ) ); + corr = TRUE; + break; + case PdcDrawLine: + s << *p[0].point << *p[1].point; + br = QRect( *p[0].point, *p[1].point ).normalize(); + corr = TRUE; + break; + case PdcDrawRect: + case PdcDrawEllipse: + s << *p[0].rect; + br = *p[0].rect; + corr = TRUE; + break; + case PdcDrawRoundRect: + case PdcDrawArc: + case PdcDrawPie: + case PdcDrawChord: + s << *p[0].rect << (Q_INT16)p[1].ival << (Q_INT16)p[2].ival; + br = *p[0].rect; + corr = TRUE; + break; + case PdcDrawLineSegments: + case PdcDrawPolyline: + s << *p[0].ptarr; + br = p[0].ptarr->boundingRect(); + corr = TRUE; + break; +#ifndef QT_NO_BEZIER + case PdcDrawCubicBezier: + s << *p[0].ptarr; + br = p[0].ptarr->cubicBezier().boundingRect(); + corr = TRUE; + break; +#endif + case PdcDrawPolygon: + s << *p[0].ptarr << (Q_INT8)p[1].ival; + br = p[0].ptarr->boundingRect(); + corr = TRUE; + break; + case PdcDrawText2: + if ( formatMajor == 1 ) { + pictb.at( pos - 2 ); + s << (Q_UINT8)PdcDrawText << (Q_UINT8)0; + QCString str1( (*p[1].str).latin1() ); + s << *p[0].point << str1; + } + else { + s << *p[0].point << *p[1].str; + } + br = pt->fontMetrics().boundingRect( *p[1].str ); + br.moveBy( p[0].point->x(), p[0].point->y() ); + break; + case PdcDrawText2Formatted: + if ( formatMajor == 1 ) { + pictb.at( pos - 2 ); + s << (Q_UINT8)PdcDrawTextFormatted << (Q_UINT8)0; + QCString str1( (*p[2].str).latin1() ); + s << *p[0].rect << (Q_INT16)p[1].ival << str1; + } + else { + s << *p[0].rect << (Q_INT16)p[1].ival << *p[2].str; + } + br = *p[0].rect; + break; + case PdcDrawPixmap: + if ( formatMajor < 4 ) { + s << *p[0].point; + s << *p[1].pixmap; + br = QRect( *p[0].point, p[1].pixmap->size() ); + } else { + s << *p[0].rect; + s << *p[1].pixmap; + br = *p[0].rect; + } + break; + case PdcDrawImage: + if ( formatMajor < 4 ) { + QPoint pt( p[0].point->x(), p[0].point->y() ); + s << pt; + s << *p[1].image; + br = QRect( *p[0].point, p[1].image->size() ); + } else { + s << *p[0].rect; + s << *p[1].image; + br = *p[0].rect; + } + break; + case PdcSave: + case PdcRestore: + break; + case PdcSetBkColor: + s << *p[0].color; + break; + case PdcSetBkMode: + case PdcSetROP: + s << (Q_INT8)p[0].ival; + break; + case PdcSetFont: { + QFont fnt = *p[0].font; + if (fnt.pointSize() > 0) + // we have to store pixels to get correct replay. + // the resolution is 72 dpi, so points == pixels + fnt.setPixelSize(QFontInfo(fnt).pixelSize()); + s << fnt; + } + break; + case PdcSetPen: + s << *p[0].pen; + break; + case PdcSetBrush: + s << *p[0].brush; + break; + case PdcSetTabStops: + s << (Q_INT16)p[0].ival; + break; + case PdcSetTabArray: + s << (Q_INT16)p[0].ival; + if ( p[0].ival ) { + int *ta = p[1].ivec; + for ( int i=0; i<p[0].ival; i++ ) + s << (Q_INT16)ta[i]; + } + break; + case PdcSetUnit: + case PdcSetVXform: + case PdcSetWXform: + case PdcSetClip: + s << (Q_INT8)p[0].ival; + break; +#ifndef QT_NO_TRANSFORMATIONS + case PdcSetWindow: + case PdcSetViewport: + s << *p[0].rect; + break; + case PdcSetWMatrix: + s << *p[0].matrix << (Q_INT8)p[1].ival; + break; +#endif + case PdcSetClipRegion: + s << *p[0].rgn; + s << (Q_INT8)p[1].ival; + break; +#if defined(QT_CHECK_RANGE) + default: + qWarning( "QPicture::cmd: Command %d not recognized", c ); +#endif + } + int newpos = (int)pictb.at(); // new position + int length = newpos - pos; + if ( length < 255 ) { // write 8-bit length + pictb.at(pos - 1); // position to right index + s << (Q_UINT8)length; + } else { // write 32-bit length + s << (Q_UINT32)0; // extend the buffer + pictb.at(pos - 1); // position to right index + s << (Q_UINT8)255; // indicate 32-bit length + char *p = pictb.buffer().data(); + memmove( p+pos+4, p+pos, length ); // make room for 4 byte + s << (Q_UINT32)length; + newpos += 4; + } + pictb.at( newpos ); // set to new position + + if ( br.isValid() ) { + if ( corr ) { // widen bounding rect + int w2 = pt->pen().width() / 2; + br.setCoords( br.left() - w2, br.top() - w2, + br.right() + w2, br.bottom() + w2 ); + } +#ifndef QT_NO_TRANSFORMATIONS + br = pt->worldMatrix().map( br ); +#endif + if ( pt->hasClipping() ) { + QRect cr = pt->clipRegion().boundingRect(); + br &= cr; + } + if ( br.isValid() ) + brect |= br; // merge with existing rect + } + + return TRUE; +} + + +/*! + Internal implementation of the virtual QPaintDevice::metric() + function. + + Use the QPaintDeviceMetrics class instead. + + A picture has the following hard-coded values: dpi=72, + numcolors=16777216 and depth=24. + + \a m is the metric to get. +*/ + +int QPicture::metric( int m ) const +{ + int val; + switch ( m ) { + // ### hard coded dpi and color depth values ! + case QPaintDeviceMetrics::PdmWidth: + val = d->brect.width(); + break; + case QPaintDeviceMetrics::PdmHeight: + val = d->brect.height(); + break; + case QPaintDeviceMetrics::PdmWidthMM: + val = int(25.4/72.0*d->brect.width()); + break; + case QPaintDeviceMetrics::PdmHeightMM: + val = int(25.4/72.0*d->brect.height()); + break; + case QPaintDeviceMetrics::PdmDpiX: + case QPaintDeviceMetrics::PdmPhysicalDpiX: + val = 72; + break; + case QPaintDeviceMetrics::PdmDpiY: + case QPaintDeviceMetrics::PdmPhysicalDpiY: + val = 72; + break; + case QPaintDeviceMetrics::PdmNumColors: + val = 16777216; + break; + case QPaintDeviceMetrics::PdmDepth: + val = 24; + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "QPicture::metric: Invalid metric command" ); +#endif + } + return val; +} + +/*! + Detaches from shared picture data and makes sure that this picture + is the only one referring to the data. + + If multiple pictures share common data, this picture makes a copy + of the data and detaches itself from the sharing mechanism. + Nothing is done if there is just a single reference. +*/ + +void QPicture::detach() +{ + if ( d->count != 1 ) + *this = copy(); +} + +/*! + Returns a \link shclass.html deep copy\endlink of the picture. +*/ + +QPicture QPicture::copy() const +{ + QPicture p; + QByteArray a( size() ); + memcpy( a.data(), data(), size() ); + p.d->pictb.setBuffer( a ); // set byte array in buffer + if ( d->pictb.isOpen() ) { // copy buffer state + p.d->pictb.open( d->pictb.mode() ); + p.d->pictb.at( d->pictb.at() ); + } + p.d->trecs = d->trecs; + p.d->formatOk = d->formatOk; + p.d->formatMinor = d->formatMajor; + p.d->brect = boundingRect(); + return p; +} + +/***************************************************************************** + QPainter member functions + *****************************************************************************/ + +/*! + Replays the picture \a pic translated by (\a x, \a y). + + This function does exactly the same as QPicture::play() when + called with (\a x, \a y) = (0, 0). +*/ + +void QPainter::drawPicture( int x, int y, const QPicture &pic ) +{ + save(); + translate( x, y ); + ((QPicture*)&pic)->play( (QPainter*)this ); + restore(); +} + +/*! + \overload void QPainter::drawPicture( const QPoint &p, const QPicture &pic ) + + Draws picture \a pic at point \a p. +*/ + +void QPainter::drawPicture( const QPoint &p, const QPicture &pic ) +{ + drawPicture( p.x(), p.y(), pic ); +} + +/*! + \obsolete + + Use one of the other QPainter::drawPicture() functions with a (0, 0) + offset instead. +*/ + +void QPainter::drawPicture( const QPicture &pic ) +{ + drawPicture( 0, 0, pic ); +} + +/*! + Assigns a \link shclass.html shallow copy\endlink of \a p to this + picture and returns a reference to this picture. +*/ + +QPicture& QPicture::operator= (const QPicture& p) +{ + p.d->ref(); // avoid 'x = x' + if ( d->deref() ) + delete d; + d = p.d; + return *this; +} + + +/*! + \internal + + Sets formatOk to FALSE and resets the format version numbers to default +*/ + +void QPicture::QPicturePrivate::resetFormat() +{ + formatOk = FALSE; + formatMajor = mfhdr_maj; + formatMinor = mfhdr_min; +} + +/*! + \internal + + Checks data integrity and format version number. Set formatOk to TRUE + on success, to FALSE otherwise. Returns the resulting formatOk value. +*/ + +bool QPicture::QPicturePrivate::checkFormat() +{ + resetFormat(); + + // can't check anything in an empty buffer + if ( pictb.size() == 0 ) + return FALSE; + + pictb.open( IO_ReadOnly ); // open buffer device + QDataStream s; + s.setDevice( &pictb ); // attach data stream to buffer + + char mf_id[4]; // picture header tag + s.readRawBytes( mf_id, 4 ); // read actual tag + if ( memcmp(mf_id, mfhdr_tag, 4) != 0 ) { // wrong header id +#if defined(QT_CHECK_RANGE) + qWarning( "QPicture::checkFormat: Incorrect header" ); +#endif + pictb.close(); + return FALSE; + } + + int cs_start = sizeof(Q_UINT32); // pos of checksum word + int data_start = cs_start + sizeof(Q_UINT16); + Q_UINT16 cs,ccs; + QByteArray buf = pictb.buffer(); // pointer to data + s >> cs; // read checksum + ccs = qChecksum( buf.data() + data_start, buf.size() - data_start ); + if ( ccs != cs ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPicture::checkFormat: Invalid checksum %x, %x expected", + ccs, cs ); +#endif + pictb.close(); + return FALSE; + } + + Q_UINT16 major, minor; + s >> major >> minor; // read version number + if ( major > mfhdr_maj ) { // new, incompatible version +#if defined(QT_CHECK_RANGE) + qWarning( "QPicture::checkFormat: Incompatible version %d.%d", + major, minor); +#endif + pictb.close(); + return FALSE; + } + s.setVersion( major != 4 ? major : 3 ); + + Q_UINT8 c, clen; + s >> c >> clen; + if ( c == PdcBegin ) { + if ( !( major >= 1 && major <= 3 )) { + Q_INT32 l, t, w, h; + s >> l >> t >> w >> h; + brect = QRect( l, t, w, h ); + } + } else { +#if defined(QT_CHECK_RANGE) + qWarning( "QPicture::checkFormat: Format error" ); +#endif + pictb.close(); + return FALSE; + } + pictb.close(); + + formatOk = TRUE; // picture seems to be ok + formatMajor = major; + formatMinor = minor; + return TRUE; +} + +/***************************************************************************** + QPicture stream functions + *****************************************************************************/ + +/*! + \relates QPicture + + Writes picture \a r to the stream \a s and returns a reference to + the stream. +*/ + +QDataStream &operator<<( QDataStream &s, const QPicture &r ) +{ + Q_UINT32 size = r.d->pictb.buffer().size(); + s << size; + // null picture ? + if ( size == 0 ) + return s; + // just write the whole buffer to the stream + return s.writeRawBytes ( r.d->pictb.buffer().data(), + r.d->pictb.buffer().size() ); +} + +/*! + \relates QPicture + + Reads a picture from the stream \a s into picture \a r and returns + a reference to the stream. +*/ + +QDataStream &operator>>( QDataStream &s, QPicture &r ) +{ + QDataStream sr; + + // "init"; this code is similar to the beginning of QPicture::cmd() + sr.setDevice( &r.d->pictb ); + sr.setVersion( r.d->formatMajor ); + Q_UINT32 len; + s >> len; + QByteArray data( len ); + if ( len > 0 ) + s.readRawBytes( data.data(), len ); + + r.d->pictb.setBuffer( data ); + r.d->resetFormat(); + + return s; +} + +#endif // QT_NO_PICTURE + diff --git a/src/kernel/qpicture.h b/src/kernel/qpicture.h new file mode 100644 index 0000000..23a374f --- /dev/null +++ b/src/kernel/qpicture.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Definition of QPicture class +** +** Created : 940729 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPICTURE_H +#define QPICTURE_H + +#ifndef QT_H +#include "qpaintdevice.h" +#include "qbuffer.h" +#endif // QT_H + +#ifndef QT_NO_PICTURE + +class Q_EXPORT QPicture : public QPaintDevice // picture class +{ +public: + QPicture( int formatVersion = -1 ); + QPicture( const QPicture & ); + ~QPicture(); + + bool isNull() const; + + uint size() const; + const char* data() const; + virtual void setData( const char* data, uint size ); + + bool play( QPainter * ); + + bool load( QIODevice *dev, const char *format = 0 ); + bool load( const QString &fileName, const char *format = 0 ); + bool save( QIODevice *dev, const char *format = 0 ); + bool save( const QString &fileName, const char *format = 0 ); + + QRect boundingRect() const; + void setBoundingRect( const QRect &r ); + + QPicture& operator= (const QPicture&); + + friend Q_EXPORT QDataStream &operator<<( QDataStream &, const QPicture & ); + friend Q_EXPORT QDataStream &operator>>( QDataStream &, QPicture & ); + +protected: + bool cmd( int, QPainter *, QPDevCmdParam * ); + int metric( int ) const; + void detach(); + QPicture copy() const; + +private: + bool exec( QPainter *, QDataStream &, int ); + + struct QPicturePrivate : public QShared { + bool cmd( int, QPainter *, QPDevCmdParam * ); + bool checkFormat(); + void resetFormat(); + + QBuffer pictb; + int trecs; + bool formatOk; + int formatMajor; + int formatMinor; + QRect brect; + } *d; +}; + + +inline bool QPicture::isNull() const +{ + return d->pictb.buffer().isNull(); +} + +inline uint QPicture::size() const +{ + return d->pictb.buffer().size(); +} + +inline const char* QPicture::data() const +{ + return d->pictb.buffer().data(); +} + +/***************************************************************************** + QPicture stream functions + *****************************************************************************/ + +Q_EXPORT QDataStream &operator<<( QDataStream &, const QPicture & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QPicture & ); + +#endif // QT_NO_PICTURE + +#endif // QPICTURE_H diff --git a/src/kernel/qpixmap.cpp b/src/kernel/qpixmap.cpp new file mode 100644 index 0000000..ecc9b46 --- /dev/null +++ b/src/kernel/qpixmap.cpp @@ -0,0 +1,1510 @@ +/**************************************************************************** +** +** Implementation of QPixmap class +** +** Created : 950301 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpixmap.h" + +#include "qbitmap.h" +#include "qimage.h" +#include "qwidget.h" +#include "qpainter.h" +#include "qdatastream.h" +#include "qbuffer.h" +#include "qobjectlist.h" +#include "qapplication.h" +#include <private/qinternal_p.h> +#include "qmime.h" +#include "qdragobject.h" +#include "qfile.h" + +/*! + \class QPixmap qpixmap.h + \brief The QPixmap class is an off-screen, pixel-based paint device. + + \ingroup graphics + \ingroup images + \ingroup shared + \mainclass + + QPixmap is one of the two classes Qt provides for dealing with + images; the other is QImage. QPixmap is designed and optimized + for drawing; QImage is designed and optimized for I/O and for + direct pixel access/manipulation. There are (slow) functions to + convert between QImage and QPixmap: convertToImage() and + convertFromImage(). + + One common use of the QPixmap class is to enable smooth updating + of widgets. Whenever something complex needs to be drawn, you can + use a pixmap to obtain flicker-free drawing, like this: + + \list 1 + \i Create a pixmap with the same size as the widget. + \i Fill the pixmap with the widget background color. + \i Paint the pixmap. + \i bitBlt() the pixmap contents onto the widget. + \endlist + + Pixel data in a pixmap is internal and is managed by the + underlying window system. Pixels can be accessed only through + QPainter functions, through bitBlt(), and by converting the + QPixmap to a QImage. + + You can easily display a QPixmap on the screen using + QLabel::setPixmap(). For example, all the QButton subclasses + support pixmap use. + + The QPixmap class uses \link shclass.html copy-on-write\endlink, + so it is practical to pass QPixmap objects by value. + + You can retrieve the width(), height(), depth() and size() of a + pixmap. The enclosing rectangle is given by rect(). Pixmaps can be + filled with fill() and resized with resize(). You can create and + set a mask with createHeuristicMask() and setMask(). Use + selfMask() to see if the pixmap is identical to its mask. + + In addition to loading a pixmap from file using load() you can + also loadFromData(). You can control optimization with + setOptimization() and obtain a transformed version of the pixmap + using xForm() + + Note regarding Windows 95 and 98: on Windows 9x the system crashes + if you create more than about 1000 pixmaps, independent of the + size of the pixmaps or installed RAM. Windows NT-systems (including + 2000, XP and following versions) do not have the same limitation, + but depending on the graphics equipment the system will fail to + allocate pixmap objects at some point (due to system running out of + GDI resources). + + Qt tries to work around the resource limitation. If you set the + pixmap optimization to \c QPixmap::MemoryOptim and the width of + your pixmap is less than or equal to 128 pixels, Qt stores the + pixmap in a way that is very memory-efficient when there are many + pixmaps. + + If your application uses dozens or hundreds of pixmaps (for + example on tool bar buttons and in popup menus), and you plan to + run it on Windows 95 or Windows 98, we recommend using code like + this: + + \code + QPixmap::setDefaultOptimization( QPixmap::MemoryOptim ); + while ( ... ) { + // load tool bar pixmaps etc. + QPixmap *pixmap = new QPixmap(fileName); + } + QPixmap::setDefaultOptimization( QPixmap::NormalOptim ); + \endcode + + In general it is recommended to make as much use of QPixmap's + implicit sharing and the QPixmapCache as possible. + + \sa QBitmap, QImage, QImageIO, \link shclass.html Shared Classes\endlink +*/ + +/*! + \enum QPixmap::ColorMode + + This enum type defines the color modes that exist for converting + QImage objects to QPixmap. + + \value Auto Select \c Color or \c Mono on a case-by-case basis. + \value Color Always create colored pixmaps. + \value Mono Always create bitmaps. +*/ + +/*! + \enum QPixmap::Optimization + + QPixmap has the choice of optimizing for speed or memory in a few + places; the best choice varies from pixmap to pixmap but can + generally be derived heuristically. This enum type defines a + number of optimization modes that you can set for any pixmap to + tweak the speed/memory tradeoffs: + + \value DefaultOptim Whatever QPixmap::defaultOptimization() + returns. A pixmap with this optimization will have whatever + the current default optimization is. If the default + optimization is changed using setDefaultOptimization(), then + this will not effect any pixmaps that have already been + created. + + \value NoOptim No optimization (currently the same as \c + MemoryOptim). + + \value MemoryOptim Optimize for minimal memory use on Windows + 9x and X11 systems. + + \value NormalOptim Optimize for typical usage. Often uses more + memory than \c MemoryOptim, and is often faster. + + \value BestOptim Optimize for pixmaps that are drawn very often + and where performance is critical. Generally uses more memory + than \c NormalOptim and may provide a little more speed. + + We recommend using \c DefaultOptim. + +*/ + + +QPixmap::Optimization QPixmap::defOptim = QPixmap::NormalOptim; + + +/*! + \internal + Private constructor which takes the bitmap flag, the optimization.and a screen. +*/ + +QPixmap::QPixmap( int w, int h, int depth, bool bitmap, + Optimization optimization ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( w, h, depth, bitmap, optimization ); +} + + +/*! + Constructs a null pixmap. + + \sa isNull() +*/ + +QPixmap::QPixmap() + : QPaintDevice( QInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); +} + +/*! + Constructs a pixmap from the QImage \a image. + + \sa convertFromImage() +*/ + +QPixmap::QPixmap( const QImage& image ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + convertFromImage( image ); +} + +/*! + Constructs a pixmap with \a w width, \a h height and \a depth bits + per pixel. The pixmap is optimized in accordance with the \a + optimization value. + + The contents of the pixmap is uninitialized. + + The \a depth can be either 1 (monochrome) or the depth of the + current video mode. If \a depth is negative, then the hardware + depth of the current video mode will be used. + + If either \a w or \a h is zero, a null pixmap is constructed. + + \sa isNull() QPixmap::Optimization +*/ + +QPixmap::QPixmap( int w, int h, int depth, Optimization optimization ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( w, h, depth, FALSE, optimization ); +} + +/*! + \overload QPixmap::QPixmap( const QSize &size, int depth, Optimization optimization ) + + Constructs a pixmap of size \a size, \a depth bits per pixel, + optimized in accordance with the \a optimization value. +*/ + +QPixmap::QPixmap( const QSize &size, int depth, Optimization optimization ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( size.width(), size.height(), depth, FALSE, optimization ); +} + +#ifndef QT_NO_IMAGEIO +/*! + Constructs a pixmap from the file \a fileName. If the file does + not exist or is of an unknown format, the pixmap becomes a null + pixmap. + + The \a fileName, \a format and \a conversion_flags parameters are + passed on to load(). This means that the data in \a fileName is + not compiled into the binary. If \a fileName contains a relative + path (e.g. the filename only) the relevant file must be found + relative to the runtime working directory. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa Qt::ImageConversionFlags isNull(), load(), loadFromData(), save(), imageFormat() +*/ + +QPixmap::QPixmap( const QString& fileName, const char *format, + int conversion_flags ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + load( fileName, format, conversion_flags ); +} + +/*! + Constructs a pixmap from the file \a fileName. If the file does + not exist or is of an unknown format, the pixmap becomes a null + pixmap. + + The \a fileName, \a format and \a mode parameters are passed on to + load(). This means that the data in \a fileName is not compiled + into the binary. If \a fileName contains a relative path (e.g. the + filename only) the relevant file must be found relative to the + runtime working directory. + + \sa QPixmap::ColorMode isNull(), load(), loadFromData(), save(), imageFormat() +*/ + +QPixmap::QPixmap( const QString& fileName, const char *format, ColorMode mode ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + load( fileName, format, mode ); +} + +/*! + Constructs a pixmap from \a xpm, which must be a valid XPM image. + + Errors are silently ignored. + + Note that it's possible to squeeze the XPM variable a little bit + by using an unusual declaration: + + \code + static const char * const start_xpm[]={ + "16 15 8 1", + "a c #cec6bd", + .... + \endcode + + The extra \c const makes the entire definition read-only, which is + slightly more efficient (for example, when the code is in a shared + library) and ROMable when the application is to be stored in ROM. + + In order to use that sort of declaration you must cast the + variable back to \c{const char **} when you create the QPixmap. +*/ + +QPixmap::QPixmap( const char *xpm[] ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + QImage image( xpm ); + if ( !image.isNull() ) + convertFromImage( image ); +} + +/*! + Constructs a pixmaps by loading from \a img_data. The data can be + in any image format supported by Qt. + + \sa loadFromData() +*/ + +QPixmap::QPixmap( const QByteArray & img_data ) + : QPaintDevice( QInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + loadFromData( img_data ); +} +#endif //QT_NO_IMAGEIO + +/*! + Constructs a pixmap that is a copy of \a pixmap. +*/ + +QPixmap::QPixmap( const QPixmap &pixmap ) + : QPaintDevice( QInternal::Pixmap ) +{ + if ( pixmap.paintingActive() ) { // make a deep copy + data = 0; + operator=( pixmap.copy() ); + } else { + data = pixmap.data; + data->ref(); + devFlags = pixmap.devFlags; // copy QPaintDevice flags +#if defined(Q_WS_WIN) + hdc = pixmap.hdc; // copy Windows device context +#elif defined(Q_WS_X11) + hd = pixmap.hd; // copy X11 drawable + rendhd = pixmap.rendhd; + copyX11Data( &pixmap ); // copy x11Data +#elif defined(Q_WS_MAC) + hd = pixmap.hd; +#endif + } +} + + +/*! + Destroys the pixmap. +*/ + +QPixmap::~QPixmap() +{ + deref(); +} + +/*! Convenience function. Gets the data associated with the absolute + name \a abs_name from the default mime source factory and decodes it + to a pixmap. + + \sa QMimeSourceFactory, QImage::fromMimeSource(), QImageDrag::decode() +*/ + +#ifndef QT_NO_MIME +QPixmap QPixmap::fromMimeSource( const QString &abs_name ) +{ + const QMimeSource *m = QMimeSourceFactory::defaultFactory()->data( abs_name ); + if ( !m ) { + if ( QFile::exists( abs_name ) ) + return QPixmap( abs_name ); +#if defined(QT_CHECK_STATE) + if ( !abs_name.isEmpty() ) + qWarning( "QPixmap::fromMimeSource: Cannot find pixmap \"%s\" in the mime source factory", + abs_name.latin1() ); +#endif + return QPixmap(); + } + QPixmap pix; + QImageDrag::decode( m, pix ); + return pix; +} +#endif + +/*! + Returns a \link shclass.html deep copy\endlink of the pixmap using + the bitBlt() function to copy the pixels. + + \sa operator=() +*/ + +QPixmap QPixmap::copy( bool ignoreMask ) const +{ +#if defined(Q_WS_X11) + int old = x11SetDefaultScreen( x11Screen() ); +#endif // Q_WS_X11 + + QPixmap pm( data->w, data->h, data->d, data->bitmap, data->optim ); + + if ( !pm.isNull() ) { // copy the bitmap +#if defined(Q_WS_X11) + pm.cloneX11Data( this ); +#endif // Q_WS_X11 + + if ( ignoreMask ) + bitBlt( &pm, 0, 0, this, 0, 0, data->w, data->h, Qt::CopyROP, TRUE ); + else + copyBlt( &pm, 0, 0, this, 0, 0, data->w, data->h ); + } + +#if defined(Q_WS_X11) + x11SetDefaultScreen( old ); +#endif // Q_WS_X11 + + return pm; +} + + +/*! + Assigns the pixmap \a pixmap to this pixmap and returns a + reference to this pixmap. +*/ + +QPixmap &QPixmap::operator=( const QPixmap &pixmap ) +{ + if ( paintingActive() ) { +#if defined(QT_CHECK_STATE) + qWarning("QPixmap::operator=: Cannot assign to pixmap during painting"); +#endif + return *this; + } + pixmap.data->ref(); // avoid 'x = x' + deref(); + if ( pixmap.paintingActive() ) { // make a deep copy + init( pixmap.width(), pixmap.height(), pixmap.depth(), + pixmap.data->bitmap, pixmap.data->optim ); + data->uninit = FALSE; + if ( !isNull() ) + copyBlt( this, 0, 0, &pixmap, 0, 0, pixmap.width(), pixmap.height() ); + pixmap.data->deref(); + } else { + data = pixmap.data; + devFlags = pixmap.devFlags; // copy QPaintDevice flags +#if defined(Q_WS_WIN) + hdc = pixmap.hdc; +#elif defined(Q_WS_X11) + hd = pixmap.hd; // copy QPaintDevice drawable + rendhd = pixmap.rendhd; + copyX11Data( &pixmap ); // copy x11Data +#elif defined(Q_WS_MACX) || defined(Q_OS_MAC9) + hd = pixmap.hd; +#endif + } + return *this; +} + + +/*! + \overload + + Converts the image \a image to a pixmap that is assigned to this + pixmap. Returns a reference to the pixmap. + + \sa convertFromImage(). +*/ + +QPixmap &QPixmap::operator=( const QImage &image ) +{ + convertFromImage( image ); + return *this; +} + + +/*! + \fn bool QPixmap::isQBitmap() const + + Returns TRUE if this is a QBitmap; otherwise returns FALSE. +*/ + +/*! + \fn bool QPixmap::isNull() const + + Returns TRUE if this is a null pixmap; otherwise returns FALSE. + + A null pixmap has zero width, zero height and no contents. You + cannot draw in a null pixmap or bitBlt() anything to it. + + Resizing an existing pixmap to (0, 0) makes a pixmap into a null + pixmap. + + \sa resize() +*/ + +/*! + \fn int QPixmap::width() const + + Returns the width of the pixmap. + + \sa height(), size(), rect() +*/ + +/*! + \fn int QPixmap::height() const + + Returns the height of the pixmap. + + \sa width(), size(), rect() +*/ + +/*! + \fn QSize QPixmap::size() const + + Returns the size of the pixmap. + + \sa width(), height(), rect() +*/ + +/*! + \fn QRect QPixmap::rect() const + + Returns the enclosing rectangle (0,0,width(),height()) of the pixmap. + + \sa width(), height(), size() +*/ + +/*! + \fn int QPixmap::depth() const + + Returns the depth of the pixmap. + + The pixmap depth is also called bits per pixel (bpp) or bit planes + of a pixmap. A null pixmap has depth 0. + + \sa defaultDepth(), isNull(), QImage::convertDepth() +*/ + + +/*! + \overload void QPixmap::fill( const QWidget *widget, const QPoint &ofs ) + + Fills the pixmap with the \a widget's background color or pixmap. + If the background is empty, nothing is done. + + The \a ofs point is an offset in the widget. + + The point \a ofs is a point in the widget's coordinate system. The + pixmap's top-left pixel will be mapped to the point \a ofs in the + widget. This is significant if the widget has a background pixmap; + otherwise the pixmap will simply be filled with the background + color of the widget. + + Example: + \code + void CuteWidget::paintEvent( QPaintEvent *e ) + { + QRect ur = e->rect(); // rectangle to update + QPixmap pix( ur.size() ); // Pixmap for double-buffering + pix.fill( this, ur.topLeft() ); // fill with widget background + + QPainter p( &pix ); + p.translate( -ur.x(), -ur.y() ); // use widget coordinate system + // when drawing on pixmap + // ... draw on pixmap ... + + p.end(); + + bitBlt( this, ur.topLeft(), &pix ); + } + \endcode +*/ + +/*! + \overload void QPixmap::fill( const QWidget *widget, int xofs, int yofs ) + + Fills the pixmap with the \a widget's background color or pixmap. + If the background is empty, nothing is done. \a xofs, \a yofs is + an offset in the widget. +*/ + +void QPixmap::fill( const QWidget *widget, int xofs, int yofs ) +{ + const QPixmap* bgpm = widget->backgroundPixmap(); + fill( widget->backgroundColor() ); + if ( bgpm ) { + if ( !bgpm->isNull() ) { + QPoint ofs = widget->backgroundOffset(); + xofs += ofs.x(); + yofs += ofs.y(); + + QPainter p; + p.begin( this ); + p.setPen( NoPen ); + p.drawTiledPixmap( 0, 0, width(), height(), *widget->backgroundPixmap(), xofs, yofs ); + p.end(); + } + } +} + + +/*! + \overload void QPixmap::resize( const QSize &size ) + + Resizes the pixmap to size \a size. +*/ + +/*! + Resizes the pixmap to \a w width and \a h height. If either \a w + or \a h is 0, the pixmap becomes a null pixmap. + + If both \a w and \a h are greater than 0, a valid pixmap is + created. New pixels will be uninitialized (random) if the pixmap + is expanded. +*/ + +void QPixmap::resize( int w, int h ) +{ + if ( w < 1 || h < 1 ) { // becomes null + QPixmap pm( 0, 0, 0, data->bitmap, data->optim ); + *this = pm; + return; + } + int d; + if ( depth() > 0 ) + d = depth(); + else + d = isQBitmap() ? 1 : -1; + // Create new pixmap + QPixmap pm( w, h, d, data->bitmap, data->optim ); +#ifdef Q_WS_X11 + pm.x11SetScreen( x11Screen() ); +#endif // Q_WS_X11 + if ( !data->uninit && !isNull() ) // has existing pixmap + bitBlt( &pm, 0, 0, this, 0, 0, // copy old pixmap + QMIN(width(), w), + QMIN(height(),h), CopyROP, TRUE ); +#if defined(Q_WS_MAC) + if(data->alphapm) { + data->alphapm->resize(w, h); + } else +#elif defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + if (data->alphapm) + qWarning("QPixmap::resize: TODO: resize alpha data"); + else +#endif // Q_WS_X11 + if ( data->mask ) { // resize mask as well + if ( data->selfmask ) { // preserve self-mask + pm.setMask( *((QBitmap*)&pm) ); + } else { // independent mask + QBitmap m = *data->mask; + m.resize( w, h ); + pm.setMask( m ); + } + } + *this = pm; +} + + +/*! + \fn const QBitmap *QPixmap::mask() const + + Returns the mask bitmap, or 0 if no mask has been set. + + \sa setMask(), QBitmap, hasAlpha() +*/ + +/*! + Sets a mask bitmap. + + The \a newmask bitmap defines the clip mask for this pixmap. Every + pixel in \a newmask corresponds to a pixel in this pixmap. Pixel + value 1 means opaque and pixel value 0 means transparent. The mask + must have the same size as this pixmap. + + \warning Setting the mask on a pixmap will cause any alpha channel + data to be cleared. For example: + \code + QPixmap alpha( "image-with-alpha.png" ); + QPixmap alphacopy = alpha; + alphacopy.setMask( *alphacopy.mask() ); + \endcode + Now, alpha and alphacopy are visually different. + + Setting a \link isNull() null\endlink mask resets the mask. + + \sa mask(), createHeuristicMask(), QBitmap +*/ + +void QPixmap::setMask( const QBitmap &newmask ) +{ + const QPixmap *tmp = &newmask; // dec cxx bug + if ( (data == tmp->data) || + ( newmask.handle() && newmask.handle() == handle() ) ) { + QPixmap m = tmp->copy( TRUE ); + setMask( *((QBitmap*)&m) ); + data->selfmask = TRUE; // mask == pixmap + return; + } + + if ( newmask.isNull() ) { // reset the mask + if (data->mask) { + detach(); + data->selfmask = FALSE; + + delete data->mask; + data->mask = 0; + } + return; + } + + detach(); + data->selfmask = FALSE; + + if ( newmask.width() != width() || newmask.height() != height() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::setMask: The pixmap and the mask must have " + "the same size" ); +#endif + return; + } +#if defined(Q_WS_MAC) || (defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE)) + // when setting the mask, we get rid of the alpha channel completely + delete data->alphapm; + data->alphapm = 0; +#endif // Q_WS_X11 && !QT_NO_XFTFREETYPE + + delete data->mask; + QBitmap* newmaskcopy; + if ( newmask.mask() ) + newmaskcopy = (QBitmap*)new QPixmap( tmp->copy( TRUE ) ); + else + newmaskcopy = new QBitmap( newmask ); +#ifdef Q_WS_X11 + newmaskcopy->x11SetScreen( x11Screen() ); +#endif + data->mask = newmaskcopy; +} + + +/*! + \fn bool QPixmap::selfMask() const + + Returns TRUE if the pixmap's mask is identical to the pixmap + itself; otherwise returns FALSE. + + \sa mask() +*/ + +#ifndef QT_NO_IMAGE_HEURISTIC_MASK +/*! + Creates and returns a heuristic mask for this pixmap. It works by + selecting a color from one of the corners and then chipping away + pixels of that color, starting at all the edges. + + The mask may not be perfect but it should be reasonable, so you + can do things such as the following: + \code + pm->setMask( pm->createHeuristicMask() ); + \endcode + + This function is slow because it involves transformation to a + QImage, non-trivial computations and a transformation back to a + QBitmap. + + If \a clipTight is TRUE the mask is just large enough to cover the + pixels; otherwise, the mask is larger than the data pixels. + + \sa QImage::createHeuristicMask() +*/ + +QBitmap QPixmap::createHeuristicMask( bool clipTight ) const +{ + QBitmap m; + m.convertFromImage( convertToImage().createHeuristicMask(clipTight) ); + return m; +} +#endif +#ifndef QT_NO_IMAGEIO +/*! + Returns a string that specifies the image format of the file \a + fileName, or 0 if the file cannot be read or if the format cannot + be recognized. + + The QImageIO documentation lists the supported image formats. + + \sa load(), save() +*/ + +const char* QPixmap::imageFormat( const QString &fileName ) +{ + return QImageIO::imageFormat(fileName); +} + +/*! + Loads a pixmap from the file \a fileName at runtime. Returns TRUE + if successful; otherwise returns FALSE. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + See the convertFromImage() documentation for a description of the + \a conversion_flags argument. + + The QImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa loadFromData(), save(), imageFormat(), QImage::load(), + QImageIO +*/ + +bool QPixmap::load( const QString &fileName, const char *format, + int conversion_flags ) +{ + QImageIO io( fileName, format ); + bool result = io.read(); + if ( result ) { + detach(); // ###hanord: Why detach here, convertFromImage does it + result = convertFromImage( io.image(), conversion_flags ); + } + return result; +} + +/*! + \overload + + Loads a pixmap from the file \a fileName at runtime. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + The \a mode is used to specify the color mode of the pixmap. + + \sa QPixmap::ColorMode +*/ + +bool QPixmap::load( const QString &fileName, const char *format, + ColorMode mode ) +{ + int conversion_flags = 0; + switch (mode) { + case Color: + conversion_flags |= ColorOnly; + break; + case Mono: + conversion_flags |= MonoOnly; + break; + default: + break;// Nothing. + } + return load( fileName, format, conversion_flags ); +} +#endif //QT_NO_IMAGEIO + +/*! + \overload + + Converts \a image and sets this pixmap using color mode \a mode. + Returns TRUE if successful; otherwise returns FALSE. + + \sa QPixmap::ColorMode +*/ + +bool QPixmap::convertFromImage( const QImage &image, ColorMode mode ) +{ + if ( image.isNull() ) { + // convert null image to null pixmap + *this = QPixmap(); + return TRUE; + } + + int conversion_flags = 0; + switch (mode) { + case Color: + conversion_flags |= ColorOnly; + break; + case Mono: + conversion_flags |= MonoOnly; + break; + default: + break;// Nothing. + } + return convertFromImage( image, conversion_flags ); +} + +#ifndef QT_NO_IMAGEIO +/*! + Loads a pixmap from the binary data in \a buf (\a len bytes). + Returns TRUE if successful; otherwise returns FALSE. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + See the convertFromImage() documentation for a description of the + \a conversion_flags argument. + + The QImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa load(), save(), imageFormat(), QImage::loadFromData(), + QImageIO +*/ + +bool QPixmap::loadFromData( const uchar *buf, uint len, const char *format, + int conversion_flags ) +{ + QByteArray a; + a.setRawData( (char *)buf, len ); + QBuffer b( a ); + b.open( IO_ReadOnly ); + QImageIO io( &b, format ); + bool result = io.read(); + b.close(); + a.resetRawData( (char *)buf, len ); + if ( result ) { + detach(); + result = convertFromImage( io.image(), conversion_flags ); + } + return result; +} + +/*! + \overload + + Loads a pixmap from the binary data in \a buf (\a len bytes) using + color mode \a mode. Returns TRUE if successful; otherwise returns + FALSE. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + \sa QPixmap::ColorMode +*/ + +bool QPixmap::loadFromData( const uchar *buf, uint len, const char *format, + ColorMode mode ) +{ + int conversion_flags = 0; + switch (mode) { + case Color: + conversion_flags |= ColorOnly; + break; + case Mono: + conversion_flags |= MonoOnly; + break; + default: + break;// Nothing. + } + return loadFromData( buf, len, format, conversion_flags ); +} + +/*! + \overload +*/ + +bool QPixmap::loadFromData( const QByteArray &buf, const char *format, + int conversion_flags ) +{ + return loadFromData( (const uchar *)(buf.data()), buf.size(), + format, conversion_flags ); +} + + +/*! + Saves the pixmap to the file \a fileName using the image file + format \a format and a quality factor \a quality. \a quality must + be in the range [0,100] or -1. Specify 0 to obtain small + compressed files, 100 for large uncompressed files, and -1 to use + the default settings. Returns TRUE if successful; otherwise + returns FALSE. + + \sa load(), loadFromData(), imageFormat(), QImage::save(), + QImageIO +*/ + +bool QPixmap::save( const QString &fileName, const char *format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + QImageIO io( fileName, format ); + return doImageIO( &io, quality ); +} + +/*! + \overload + + This function writes a QPixmap to the QIODevice, \a device. This + can be used, for example, to save a pixmap directly into a + QByteArray: + \code + QPixmap pixmap; + QByteArray ba; + QBuffer buffer( ba ); + buffer.open( IO_WriteOnly ); + pixmap.save( &buffer, "PNG" ); // writes pixmap into ba in PNG format + \endcode +*/ + +bool QPixmap::save( QIODevice* device, const char* format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + QImageIO io( device, format ); + return doImageIO( &io, quality ); +} + +/*! \internal +*/ + +bool QPixmap::doImageIO( QImageIO* io, int quality ) const +{ + if ( !io ) + return FALSE; + io->setImage( convertToImage() ); +#if defined(QT_CHECK_RANGE) + if ( quality > 100 || quality < -1 ) + qWarning( "QPixmap::save: quality out of range [-1,100]" ); +#endif + if ( quality >= 0 ) + io->setQuality( QMIN(quality,100) ); + return io->write(); +} + +#endif //QT_NO_IMAGEIO + +/*! + \fn int QPixmap::serialNumber() const + + Returns a number that uniquely identifies the contents of this + QPixmap object. This means that multiple QPixmap objects can have + the same serial number as long as they refer to the same contents. + + An example of where this is useful is for caching QPixmaps. + + \sa QPixmapCache +*/ + + +/*! + Returns the default pixmap optimization setting. + + \sa setDefaultOptimization(), setOptimization(), optimization() +*/ + +QPixmap::Optimization QPixmap::defaultOptimization() +{ + return defOptim; +} + +/*! + Sets the default pixmap optimization. + + All \e new pixmaps that are created will use this default + optimization. You may also set optimization for individual pixmaps + using the setOptimization() function. + + The initial default \a optimization setting is \c QPixmap::Normal. + + \sa defaultOptimization(), setOptimization(), optimization() +*/ + +void QPixmap::setDefaultOptimization( Optimization optimization ) +{ + if ( optimization != DefaultOptim ) + defOptim = optimization; +} + + +// helper for next function. +static QPixmap grabChildWidgets( QWidget * w ) +{ + QPixmap res( w->width(), w->height() ); + if ( res.isNull() && w->width() ) + return res; + res.fill( w, QPoint( 0, 0 ) ); + QPaintDevice *oldRedirect = QPainter::redirect( w ); + QPainter::redirect( w, &res ); + bool dblbfr = QSharedDoubleBuffer::isDisabled(); + QSharedDoubleBuffer::setDisabled( TRUE ); + QPaintEvent e( w->rect(), FALSE ); + QApplication::sendEvent( w, &e ); + QSharedDoubleBuffer::setDisabled( dblbfr ); + QPainter::redirect( w, oldRedirect ); + + const QObjectList * children = w->children(); + if ( children ) { + QPainter p( &res ); + QObjectListIt it( *children ); + QObject * child; + while( (child=it.current()) != 0 ) { + ++it; + if ( child->isWidgetType() && + !((QWidget *)child)->isHidden() && + !((QWidget *)child)->isTopLevel() && + ((QWidget *)child)->geometry().intersects( w->rect() ) ) { + // those conditions aren't quite right, it's possible + // to have a grandchild completely outside its + // grandparent, but partially inside its parent. no + // point in optimizing for that. + + // make sure to evaluate pos() first - who knows what + // the paint event(s) inside grabChildWidgets() will do. + QPoint childpos = ((QWidget *)child)->pos(); + QPixmap cpm = grabChildWidgets( (QWidget *)child ); + if ( cpm.isNull() ) { + // Some child pixmap failed - abort and reset + res.resize( 0, 0 ); + break; + } + p.drawPixmap( childpos, cpm); + } + } + } + return res; +} + + +/*! + Creates a pixmap and paints \a widget in it. + + If the \a widget has any children, then they are also painted in + the appropriate positions. + + If you specify \a x, \a y, \a w or \a h, only the rectangle you + specify is painted. The defaults are 0, 0 (top-left corner) and + -1,-1 (which means the entire widget). + + (If \a w is negative, the function copies everything to the right + border of the window. If \a h is negative, the function copies + everything to the bottom of the window.) + + If \a widget is 0, or if the rectangle defined by \a x, \a y, the + modified \a w and the modified \a h does not overlap the \a + {widget}->rect(), this function will return a null QPixmap. + + This function actually asks \a widget to paint itself (and its + children to paint themselves). QPixmap::grabWindow() grabs pixels + off the screen, which is a bit faster and picks up \e exactly + what's on-screen. This function works by calling paintEvent() with + painter redirection turned on. If there are overlaying windows, + grabWindow() will see them, but not this function. + + If there is overlap, it returns a pixmap of the size you want, + containing a rendering of \a widget. If the rectangle you ask for + is a superset of \a widget, the areas outside \a widget are + covered with the widget's background. + + If an error occurs when trying to grab the widget, such as the + size of the widget being too large to fit in memory, an isNull() + pixmap is returned. + + \sa grabWindow() QPainter::redirect() QWidget::paintEvent() +*/ + +QPixmap QPixmap::grabWidget( QWidget * widget, int x, int y, int w, int h ) +{ + QPixmap res; + if ( !widget ) + return res; + + if ( w < 0 ) + w = widget->width() - x; + if ( h < 0 ) + h = widget->height() - y; + + QRect wr( x, y, w, h ); + if ( wr == widget->rect() ) + return grabChildWidgets( widget ); + if ( !wr.intersects( widget->rect() ) ) + return res; + + res.resize( w, h ); + if( res.isNull() ) + return res; + res.fill( widget, QPoint( w,h ) ); + QPixmap tmp( grabChildWidgets( widget ) ); + if( tmp.isNull() ) + return tmp; + ::bitBlt( &res, 0, 0, &tmp, x, y, w, h ); + return res; +} + +/*! + Returns the actual matrix used for transforming a pixmap with \a w + width and \a h height and matrix \a matrix. + + When transforming a pixmap with xForm(), the transformation matrix + is internally adjusted to compensate for unwanted translation, + i.e. xForm() returns the smallest pixmap containing all + transformed points of the original pixmap. + + This function returns the modified matrix, which maps points + correctly from the original pixmap into the new pixmap. + + \sa xForm(), QWMatrix +*/ +#ifndef QT_NO_PIXMAP_TRANSFORMATION +QWMatrix QPixmap::trueMatrix( const QWMatrix &matrix, int w, int h ) +{ + const double dt = (double)0.; + double x1,y1, x2,y2, x3,y3, x4,y4; // get corners + double xx = (double)w; + double yy = (double)h; + + QWMatrix mat( matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), 0., 0. ); + + mat.map( dt, dt, &x1, &y1 ); + mat.map( xx, dt, &x2, &y2 ); + mat.map( xx, yy, &x3, &y3 ); + mat.map( dt, yy, &x4, &y4 ); + + double ymin = y1; // lowest y value + if ( y2 < ymin ) ymin = y2; + if ( y3 < ymin ) ymin = y3; + if ( y4 < ymin ) ymin = y4; + double xmin = x1; // lowest x value + if ( x2 < xmin ) xmin = x2; + if ( x3 < xmin ) xmin = x3; + if ( x4 < xmin ) xmin = x4; + + double ymax = y1; // lowest y value + if ( y2 > ymax ) ymax = y2; + if ( y3 > ymax ) ymax = y3; + if ( y4 > ymax ) ymax = y4; + double xmax = x1; // lowest x value + if ( x2 > xmax ) xmax = x2; + if ( x3 > xmax ) xmax = x3; + if ( x4 > xmax ) xmax = x4; + + if ( xmax-xmin > 1.0 ) + xmin -= xmin/(xmax-xmin); + if ( ymax-ymin > 1.0 ) + ymin -= ymin/(ymax-ymin); + + mat.setMatrix( matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), -xmin, -ymin ); + return mat; +} +#endif // QT_NO_WMATRIX + + + + + +/***************************************************************************** + QPixmap stream functions + *****************************************************************************/ +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +/*! + \relates QPixmap + + Writes the pixmap \a pixmap to the stream \a s as a PNG image. + + Note that writing the stream to a file will not produce a valid image file. + + \sa QPixmap::save() + \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QPixmap &pixmap ) +{ + s << pixmap.convertToImage(); + return s; +} + +/*! + \relates QPixmap + + Reads a pixmap from the stream \a s into the pixmap \a pixmap. + + \sa QPixmap::load() + \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QPixmap &pixmap ) +{ + QImage img; + s >> img; + pixmap.convertFromImage( img ); + return s; +} + +#endif //QT_NO_DATASTREAM + + + + +/***************************************************************************** + QPixmap (and QImage) helper functions + *****************************************************************************/ +/* + This internal function contains the common (i.e. platform independent) code + to do a transformation of pixel data. It is used by QPixmap::xForm() and by + QImage::xForm(). + + \a trueMat is the true transformation matrix (see QPixmap::trueMatrix()) and + \a xoffset is an offset to the matrix. + + \a msbfirst specifies for 1bpp images, if the MSB or LSB comes first and \a + depth specifies the colordepth of the data. + + \a dptr is a pointer to the destination data, \a dbpl specifies the bits per + line for the destination data, \a p_inc is the offset that we advance for + every scanline and \a dHeight is the height of the destination image. + + \a sprt is the pointer to the source data, \a sbpl specifies the bits per + line of the source data, \a sWidth and \a sHeight are the width and height of + the source data. +*/ +#ifndef QT_NO_PIXMAP_TRANSFORMATION +#undef IWX_MSB +#define IWX_MSB(b) if ( trigx < maxws && trigy < maxhs ) { \ + if ( *(sptr+sbpl*(trigy>>16)+(trigx>>19)) & \ + (1 << (7-((trigx>>16)&7))) ) \ + *dptr |= b; \ + } \ + trigx += m11; \ + trigy += m12; + // END OF MACRO +#undef IWX_LSB +#define IWX_LSB(b) if ( trigx < maxws && trigy < maxhs ) { \ + if ( *(sptr+sbpl*(trigy>>16)+(trigx>>19)) & \ + (1 << ((trigx>>16)&7)) ) \ + *dptr |= b; \ + } \ + trigx += m11; \ + trigy += m12; + // END OF MACRO +#undef IWX_PIX +#define IWX_PIX(b) if ( trigx < maxws && trigy < maxhs ) { \ + if ( (*(sptr+sbpl*(trigy>>16)+(trigx>>19)) & \ + (1 << (7-((trigx>>16)&7)))) == 0 ) \ + *dptr &= ~b; \ + } \ + trigx += m11; \ + trigy += m12; + // END OF MACRO +bool qt_xForm_helper( const QWMatrix &trueMat, int xoffset, + int type, int depth, + uchar *dptr, int dbpl, int p_inc, int dHeight, + uchar *sptr, int sbpl, int sWidth, int sHeight + ) +{ + int m11 = int(trueMat.m11()*65536.0 + 1.); + int m12 = int(trueMat.m12()*65536.0 + 1.); + int m21 = int(trueMat.m21()*65536.0 + 1.); + int m22 = int(trueMat.m22()*65536.0 + 1.); + int dx = qRound(trueMat.dx() *65536.0); + int dy = qRound(trueMat.dy() *65536.0); + + int m21ydx = dx + (xoffset<<16); + int m22ydy = dy; + uint trigx; + uint trigy; + uint maxws = sWidth<<16; + uint maxhs = sHeight<<16; + + for ( int y=0; y<dHeight; y++ ) { // for each target scanline + trigx = m21ydx; + trigy = m22ydy; + uchar *maxp = dptr + dbpl; + if ( depth != 1 ) { + switch ( depth ) { + case 8: // 8 bpp transform + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) + *dptr = *(sptr+sbpl*(trigy>>16)+(trigx>>16)); + trigx += m11; + trigy += m12; + dptr++; + } + break; + + case 16: // 16 bpp transform + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) + *((ushort*)dptr) = *((ushort *)(sptr+sbpl*(trigy>>16) + + ((trigx>>16)<<1))); + trigx += m11; + trigy += m12; + dptr++; + dptr++; + } + break; + + case 24: { // 24 bpp transform + uchar *p2; + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) { + p2 = sptr+sbpl*(trigy>>16) + ((trigx>>16)*3); + dptr[0] = p2[0]; + dptr[1] = p2[1]; + dptr[2] = p2[2]; + } + trigx += m11; + trigy += m12; + dptr += 3; + } + } + break; + + case 32: // 32 bpp transform + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) + *((uint*)dptr) = *((uint *)(sptr+sbpl*(trigy>>16) + + ((trigx>>16)<<2))); + trigx += m11; + trigy += m12; + dptr += 4; + } + break; + + default: { + return FALSE; + } + } + } else { + switch ( type ) { + case QT_XFORM_TYPE_MSBFIRST: + while ( dptr < maxp ) { + IWX_MSB(128); + IWX_MSB(64); + IWX_MSB(32); + IWX_MSB(16); + IWX_MSB(8); + IWX_MSB(4); + IWX_MSB(2); + IWX_MSB(1); + dptr++; + } + break; + case QT_XFORM_TYPE_LSBFIRST: + while ( dptr < maxp ) { + IWX_LSB(1); + IWX_LSB(2); + IWX_LSB(4); + IWX_LSB(8); + IWX_LSB(16); + IWX_LSB(32); + IWX_LSB(64); + IWX_LSB(128); + dptr++; + } + break; +# if defined(Q_WS_WIN) + case QT_XFORM_TYPE_WINDOWSPIXMAP: + while ( dptr < maxp ) { + IWX_PIX(128); + IWX_PIX(64); + IWX_PIX(32); + IWX_PIX(16); + IWX_PIX(8); + IWX_PIX(4); + IWX_PIX(2); + IWX_PIX(1); + dptr++; + } + break; +# endif + } + } + m21ydx += m21; + m22ydy += m22; + dptr += p_inc; + } + return TRUE; +} +#undef IWX_MSB +#undef IWX_LSB +#undef IWX_PIX +#endif // QT_NO_PIXMAP_TRANSFORMATION diff --git a/src/kernel/qpixmap.h b/src/kernel/qpixmap.h new file mode 100644 index 0000000..cb3e72f --- /dev/null +++ b/src/kernel/qpixmap.h @@ -0,0 +1,354 @@ +/**************************************************************************** +** +** Definition of QPixmap class +** +** Created : 940501 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPIXMAP_H +#define QPIXMAP_H + +#ifndef QT_H +#include "qpaintdevice.h" +#include "qcolor.h" // char*->QColor conversion +#include "qstring.h" // char*->QString conversion +#include "qnamespace.h" +#endif // QT_H + +class QGfx; +class QPixmapPrivate; + +#if defined(Q_WS_WIN) +// Internal pixmap memory optimization class for Windows 9x +class QMultiCellPixmap; +#endif + + +class Q_EXPORT QPixmap : public QPaintDevice, public Qt +{ +public: + enum ColorMode { Auto, Color, Mono }; + enum Optimization { DefaultOptim, NoOptim, MemoryOptim=NoOptim, + NormalOptim, BestOptim }; + + QPixmap(); + QPixmap( const QImage& image ); + QPixmap( int w, int h, int depth = -1, Optimization = DefaultOptim ); + QPixmap( const QSize &, int depth = -1, Optimization = DefaultOptim ); +#ifndef QT_NO_IMAGEIO + QPixmap( const QString& fileName, const char *format=0, + ColorMode mode=Auto ); + QPixmap( const QString& fileName, const char *format, + int conversion_flags ); + QPixmap( const char *xpm[] ); // ### in 4.0, 'const char * const xpm[]'? + QPixmap( const QByteArray &data ); +#endif + QPixmap( const QPixmap & ); + ~QPixmap(); + + QPixmap &operator=( const QPixmap & ); + QPixmap &operator=( const QImage & ); + + bool isNull() const; + + int width() const { return data->w; } + int height() const { return data->h; } + QSize size() const { return QSize(data->w,data->h); } + QRect rect() const { return QRect(0,0,data->w,data->h); } + int depth() const { return data->d; } + static int defaultDepth(); + + void fill( const QColor &fillColor = Qt::white ); + void fill( const QWidget *, int xofs, int yofs ); + void fill( const QWidget *, const QPoint &ofs ); + void resize( int width, int height ); + void resize( const QSize & ); + + const QBitmap *mask() const; + void setMask( const QBitmap & ); + bool selfMask() const; + bool hasAlpha() const; + bool hasAlphaChannel() const; +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + QBitmap createHeuristicMask( bool clipTight = TRUE ) const; +#endif +#ifndef QT_NO_MIME + static QPixmap fromMimeSource( const QString& abs_name ); +#endif + static QPixmap grabWindow( WId, int x=0, int y=0, int w=-1, int h=-1 ); + static QPixmap grabWidget( QWidget * widget, + int x=0, int y=0, int w=-1, int h=-1 ); + +#ifndef QT_NO_PIXMAP_TRANSFORMATION + QPixmap xForm( const QWMatrix & ) const; + static QWMatrix trueMatrix( const QWMatrix &, int w, int h ); +#endif + + QImage convertToImage() const; + bool convertFromImage( const QImage &, ColorMode mode=Auto ); + bool convertFromImage( const QImage &, int conversion_flags ); +#ifndef QT_NO_IMAGEIO + static const char* imageFormat( const QString &fileName ); + bool load( const QString& fileName, const char *format=0, + ColorMode mode=Auto ); + bool load( const QString& fileName, const char *format, + int conversion_flags ); + bool loadFromData( const uchar *buf, uint len, + const char* format=0, + ColorMode mode=Auto ); + bool loadFromData( const uchar *buf, uint len, + const char* format, + int conversion_flags ); + bool loadFromData( const QByteArray &data, + const char* format=0, + int conversion_flags=0 ); + bool save( const QString& fileName, const char* format, int quality = -1 ) const; + bool save( QIODevice* device, const char* format, int quality = -1 ) const; +#endif + +#if defined(Q_WS_WIN) + HBITMAP hbm() const; +#endif + + int serialNumber() const; + + Optimization optimization() const; + void setOptimization( Optimization ); + static Optimization defaultOptimization(); + static void setDefaultOptimization( Optimization ); + + virtual void detach(); + + bool isQBitmap() const; + +#if defined(Q_WS_WIN) + // These functions are internal and used by Windows 9x only + bool isMultiCellPixmap() const; + HDC multiCellHandle() const; + HBITMAP multiCellBitmap() const; + int multiCellOffset() const; + int allocCell(); + void freeCell( bool = FALSE ); +#endif + +#if defined(Q_WS_QWS) + virtual QGfx * graphicsContext(bool clip_children=TRUE) const; + virtual unsigned char * scanLine(int) const; + virtual int bytesPerLine() const; + QRgb * clut() const; + int numCols() const; +#elif defined(Q_WS_X11) + static int x11SetDefaultScreen( int screen ); + void x11SetScreen( int screen ); +#endif + +#ifndef Q_QDOC + Q_DUMMY_COMPARISON_OPERATOR(QPixmap) +#endif + +protected: + QPixmap( int w, int h, const uchar *data, bool isXbitmap ); + int metric( int ) const; + +#if defined(Q_WS_WIN) + struct QMCPI { // mem optim for win9x + QMultiCellPixmap *mcp; + int offset; + }; +#endif + + struct QPixmapData : public QShared { // internal pixmap data + QCOORD w, h; + short d; + uint uninit : 1; + uint bitmap : 1; + uint selfmask : 1; +#if defined(Q_WS_WIN) + uint mcp : 1; +#endif + int ser_no; + QBitmap *mask; +#if defined(Q_WS_WIN) + QPixmap *maskpm; + union { + HBITMAP hbm; // if mcp == FALSE + QMCPI *mcpi; // if mcp == TRUE + } hbm_or_mcpi; + uchar *realAlphaBits; +#ifdef Q_OS_TEMP + uchar* ppvBits; // Pointer to DIBSection bits +#endif +#elif defined(Q_WS_X11) + void *ximage; + void *maskgc; + QPixmap *alphapm; +#elif defined(Q_WS_MAC) + ColorTable *clut; + QPixmap *alphapm; +#elif defined(Q_WS_QWS) + int id; // ### should use QPaintDevice::hd, since it is there + QRgb * clut; + int numcols; + int rw; + int rh; + bool hasAlpha; +#endif + Optimization optim; +#if defined(Q_WS_WIN) + HBITMAP old_hbm; +#endif + } *data; +private: +#ifndef QT_NO_IMAGEIO + bool doImageIO( QImageIO* io, int quality ) const; +#endif + QPixmap( int w, int h, int depth, bool, Optimization ); + void init( int, int, int, bool, Optimization ); + void deref(); + QPixmap copy( bool ignoreMask = FALSE ) const; +#if defined(Q_WS_WIN) + void initAlphaPixmap( uchar *bytes, int length, struct tagBITMAPINFO *bmi ); + void convertToAlphaPixmap( bool initAlpha=TRUE ); + static void bitBltAlphaPixmap( QPixmap *dst, int dx, int dy, + const QPixmap *src, int sx, int sy, + int sw, int sh, bool useDstAlpha ); +#endif + static Optimization defOptim; + friend Q_EXPORT void bitBlt( QPaintDevice *, int, int, + const QPaintDevice *, + int, int, int, int, RasterOp, bool ); + friend Q_EXPORT void bitBlt( QPaintDevice *, int, int, + const QImage* src, + int, int, int, int, int conversion_flags ); + friend Q_EXPORT void copyBlt( QPixmap *dst, int dx, int dy, + const QPixmap *src, int sx, int sy, + int sw, int sh ); + +#if defined(Q_WS_MAC) + friend void unclippedScaledBitBlt(QPaintDevice *, int, int, int, int, + const QPaintDevice *, int, int, int, int, + Qt::RasterOp, bool, bool); +#endif + + friend class QBitmap; + friend class QPaintDevice; + friend class QPainter; + friend class QGLWidget; +}; + + +inline bool QPixmap::isNull() const +{ + return data->w == 0; +} + +inline void QPixmap::fill( const QWidget *w, const QPoint &ofs ) +{ + fill( w, ofs.x(), ofs.y() ); +} + +inline void QPixmap::resize( const QSize &s ) +{ + resize( s.width(), s.height() ); +} + +inline const QBitmap *QPixmap::mask() const +{ + return data->mask; +} + +inline bool QPixmap::selfMask() const +{ + return data->selfmask; +} + +#if defined(Q_WS_WIN) +inline HBITMAP QPixmap::hbm() const +{ + return data->mcp ? 0 : data->hbm_or_mcpi.hbm; +} +#endif + +inline int QPixmap::serialNumber() const +{ + return data->ser_no; +} + +inline QPixmap::Optimization QPixmap::optimization() const +{ + return data->optim; +} + +inline bool QPixmap::isQBitmap() const +{ + return data->bitmap; +} + +#if defined(Q_WS_WIN) +inline bool QPixmap::isMultiCellPixmap() const +{ + return data->mcp; +} +#endif + + +/***************************************************************************** + QPixmap stream functions + *****************************************************************************/ + +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +Q_EXPORT QDataStream &operator<<( QDataStream &, const QPixmap & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QPixmap & ); +#endif + +/***************************************************************************** + QPixmap (and QImage) helper functions + *****************************************************************************/ + +#ifndef QT_NO_PIXMAP_TRANSFORMATION +# define QT_XFORM_TYPE_MSBFIRST 0 +# define QT_XFORM_TYPE_LSBFIRST 1 +# if defined(Q_WS_WIN) +# define QT_XFORM_TYPE_WINDOWSPIXMAP 2 +# endif +bool qt_xForm_helper( const QWMatrix&, int, int, int, uchar*, int, int, int, uchar*, int, int, int ); +#endif + +Q_EXPORT void copyBlt( QPixmap *dst, int dx, int dy, + const QPixmap *src, int sx = 0, int sy = 0, + int sw = -1, int sh = -1 ); + +#endif // QPIXMAP_H diff --git a/src/kernel/qpixmap_x11.cpp b/src/kernel/qpixmap_x11.cpp new file mode 100644 index 0000000..d0a16c5 --- /dev/null +++ b/src/kernel/qpixmap_x11.cpp @@ -0,0 +1,2476 @@ +/**************************************************************************** +** +** Implementation of QPixmap class for X11 +** +** Created : 940501 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +// NOT REVISED + +#include "qplatformdefs.h" + +#if defined(Q_OS_WIN32) && defined(QT_MITSHM) +#undef QT_MITSHM +#endif + +#ifdef QT_MITSHM + +// Use the MIT Shared Memory extension for pixmap<->image conversions +#define QT_MITSHM_CONVERSIONS + +// Uncomment the next line to enable the MIT Shared Memory extension +// for QPixmap::xForm() +// +// WARNING: This has some problems: +// +// 1. Consumes a 800x600 pixmap +// 2. Qt does not handle the ShmCompletion message, so you will +// get strange effects if you xForm() repeatedly. +// +// #define QT_MITSHM_XFORM + +#else +#undef QT_MITSHM_CONVERSIONS +#undef QT_MITSHM_XFORM +#endif + +#include "qbitmap.h" +#include "qpaintdevicemetrics.h" +#include "qimage.h" +#include "qwmatrix.h" +#include "qapplication.h" +#include "qt_x11_p.h" + +#include <stdlib.h> + +#if defined(Q_CC_MIPS) +# define for if(0){}else for +#endif + + +/*! + \class QPixmap::QPixmapData + \brief The QPixmap::QPixmapData class is an internal class. + \internal +*/ + + +// For thread-safety: +// image->data does not belong to X11, so we must free it ourselves. + +inline static void qSafeXDestroyImage( XImage *x ) +{ + if ( x->data ) { + free( x->data ); + x->data = 0; + } + XDestroyImage( x ); +} + + +/***************************************************************************** + MIT Shared Memory Extension support: makes xForm noticeably (~20%) faster. + *****************************************************************************/ + +#if defined(QT_MITSHM_XFORM) + +static bool xshminit = FALSE; +static XShmSegmentInfo xshminfo; +static XImage *xshmimg = 0; +static Pixmap xshmpm = 0; + +static void qt_cleanup_mitshm() +{ + if ( xshmimg == 0 ) + return; + Display *dpy = QPaintDevice::x11AppDisplay(); + if ( xshmpm ) { + XFreePixmap( dpy, xshmpm ); + xshmpm = 0; + } + XShmDetach( dpy, &xshminfo ); xshmimg->data = 0; + qSafeXDestroyImage( xshmimg ); xshmimg = 0; + shmdt( xshminfo.shmaddr ); + shmctl( xshminfo.shmid, IPC_RMID, 0 ); +} + + +static bool qt_create_mitshm_buffer( const QPaintDevice* dev, int w, int h ) +{ + static int major, minor; + static Bool pixmaps_ok; + Display *dpy = dev->x11Display(); + int dd = dev->x11Depth(); + Visual *vis = (Visual*)dev->x11Visual(); + + if ( xshminit ) { + qt_cleanup_mitshm(); + } else { + if ( !XShmQueryVersion(dpy, &major, &minor, &pixmaps_ok) ) + return FALSE; // MIT Shm not supported + qAddPostRoutine( qt_cleanup_mitshm ); + xshminit = TRUE; + } + + xshmimg = XShmCreateImage( dpy, vis, dd, ZPixmap, 0, &xshminfo, w, h ); + if ( !xshmimg ) + return FALSE; + + bool ok; + xshminfo.shmid = shmget( IPC_PRIVATE, + xshmimg->bytes_per_line * xshmimg->height, + IPC_CREAT | 0777 ); + ok = xshminfo.shmid != -1; + if ( ok ) { + xshmimg->data = (char*)shmat( xshminfo.shmid, 0, 0 ); + xshminfo.shmaddr = xshmimg->data; + ok = ( xshminfo.shmaddr != (char*)-1 ); + } + xshminfo.readOnly = FALSE; + if ( ok ) + ok = XShmAttach( dpy, &xshminfo ); + if ( !ok ) { + qSafeXDestroyImage( xshmimg ); + xshmimg = 0; + if ( xshminfo.shmaddr ) + shmdt( xshminfo.shmaddr ); + if ( xshminfo.shmid != -1 ) + shmctl( xshminfo.shmid, IPC_RMID, 0 ); + return FALSE; + } + if ( pixmaps_ok ) + xshmpm = XShmCreatePixmap( dpy, DefaultRootWindow(dpy), xshmimg->data, + &xshminfo, w, h, dd ); + + return TRUE; +} + +#else + +// If extern, need a dummy. +// +// static bool qt_create_mitshm_buffer( QPaintDevice*, int, int ) +// { +// return FALSE; +// } + +#endif // QT_MITSHM_XFORM + +#ifdef QT_MITSHM_CONVERSIONS + +static bool qt_mitshm_error = false; +static int qt_mitshm_errorhandler( Display*, XErrorEvent* ) +{ + qt_mitshm_error = true; + return 0; +} + +static XImage* qt_XShmCreateImage( Display* dpy, Visual* visual, unsigned int depth, + int format, int /*offset*/, char* /*data*/, unsigned int width, unsigned int height, + int /*bitmap_pad*/, int /*bytes_per_line*/, XShmSegmentInfo* shminfo ) +{ + if( width * height * depth < 100*100*32 ) + return NULL; + static int shm_inited = -1; + if( shm_inited == -1 ) { + if( XShmQueryExtension( dpy )) + shm_inited = 1; + else + shm_inited = 0; + } + if( shm_inited == 0 ) + return NULL; + XImage* xi = XShmCreateImage( dpy, visual, depth, format, NULL, shminfo, width, + height ); + if( xi == NULL ) + return NULL; + shminfo->shmid = shmget( IPC_PRIVATE, xi->bytes_per_line * xi->height, + IPC_CREAT|0600); + if( shminfo->shmid < 0 ) { + XDestroyImage( xi ); + return NULL; + } + shminfo->readOnly = False; + shminfo->shmaddr = (char*)shmat( shminfo->shmid, 0, 0 ); + if( shminfo->shmaddr == (char*)-1 ) { + XDestroyImage( xi ); + shmctl( shminfo->shmid, IPC_RMID, 0 ); + return NULL; + } + xi->data = shminfo->shmaddr; +#ifndef QT_MITSHM_RMID_IGNORES_REFCOUNT + // mark as deleted to automatically free the memory in case + // of a crash (but this doesn't work e.g. on Solaris) + shmctl( shminfo->shmid, IPC_RMID, 0 ); +#endif + if( shm_inited == 1 ) { // first time + XErrorHandler old_h = XSetErrorHandler( qt_mitshm_errorhandler ); + XShmAttach( dpy, shminfo ); + shm_inited = 2; + XSync( dpy, False ); + XSetErrorHandler( old_h ); + if( qt_mitshm_error ) { // oops ... perhaps we are remote? + shm_inited = 0; + XDestroyImage( xi ); + shmdt( shminfo->shmaddr ); +#ifdef QT_MITSHM_RMID_IGNORES_REFCOUNT + shmctl( shminfo->shmid, IPC_RMID, 0 ); +#endif + return NULL; + } + } else + XShmAttach( dpy, shminfo ); + return xi; +} + +static void qt_XShmDestroyImage( XImage* xi, XShmSegmentInfo* shminfo ) +{ + XShmDetach( QPaintDevice::x11AppDisplay(), shminfo ); + XDestroyImage( xi ); + shmdt( shminfo->shmaddr ); +#ifdef QT_MITSHM_RMID_IGNORES_REFCOUNT + shmctl( shminfo->shmid, IPC_RMID, 0 ); +#endif +} + +static XImage* qt_XShmGetImage( const QPixmap* pix, int format, + XShmSegmentInfo* shminfo ) +{ + XImage* xi = qt_XShmCreateImage( pix->x11Display(), (Visual*)pix->x11Visual(), + pix->depth(), format, 0, 0, pix->width(), pix->height(), 32, 0, shminfo ); + if( xi == NULL ) + return NULL; + if( XShmGetImage( pix->x11Display(), pix->handle(), xi, 0, 0, AllPlanes ) == False ) { + qt_XShmDestroyImage( xi, shminfo ); + return NULL; + } + return xi; +} + +#endif // QT_MITSHM_CONVERSIONS + +/***************************************************************************** + Internal functions + *****************************************************************************/ + +extern const uchar *qt_get_bitflip_array(); // defined in qimage.cpp + +static uchar *flip_bits( const uchar *bits, int len ) +{ + register const uchar *p = bits; + const uchar *end = p + len; + uchar *newdata = new uchar[len]; + uchar *b = newdata; + const uchar *f = qt_get_bitflip_array(); + while ( p < end ) + *b++ = f[*p++]; + return newdata; +} + +// Returns position of highest bit set or -1 if none +static int highest_bit( uint v ) +{ + int i; + uint b = (uint)1 << 31; + for ( i=31; ((b & v) == 0) && i>=0; i-- ) + b >>= 1; + return i; +} + +// Returns position of lowest set bit in 'v' as an integer (0-31), or -1 +static int lowest_bit( uint v ) +{ + int i; + ulong lb; + lb = 1; + for (i=0; ((v & lb) == 0) && i<32; i++, lb<<=1); + return i==32 ? -1 : i; +} + +// Counts the number of bits set in 'v' +static uint n_bits( uint v ) +{ + int i = 0; + while ( v ) { + v = v & (v - 1); + i++; + } + return i; +} + +static uint *red_scale_table = 0; +static uint *green_scale_table = 0; +static uint *blue_scale_table = 0; + +static void cleanup_scale_tables() +{ + delete[] red_scale_table; + delete[] green_scale_table; + delete[] blue_scale_table; +} + +/* + Could do smart bitshifting, but the "obvious" algorithm only works for + nBits >= 4. This is more robust. +*/ +static void build_scale_table( uint **table, uint nBits ) +{ + if ( nBits > 7 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "build_scale_table: internal error, nBits = %i", nBits ); +#endif + return; + } + if (!*table) { + static bool firstTable = TRUE; + if ( firstTable ) { + qAddPostRoutine( cleanup_scale_tables ); + firstTable = FALSE; + } + *table = new uint[256]; + } + int maxVal = (1 << nBits) - 1; + int valShift = 8 - nBits; + int i; + for( i = 0 ; i < maxVal + 1 ; i++ ) + (*table)[i << valShift] = i*255/maxVal; +} + +static int defaultScreen = -1; + +extern bool qt_use_xrender; // defined in qapplication_x11.cpp +extern bool qt_has_xft; // defined in qfont_x11.cpp + +#ifndef QT_NO_XFTFREETYPE +#ifndef QT_XFT2 +// Xft1 doesn't have XftDrawCreateAlpha, so we fake it in qtaddons_x11.cpp +extern "C" XftDraw *XftDrawCreateAlpha( Display *, Qt::HANDLE, int ); +#endif // QT_XFT2 +#endif // QT_NO_XFTFREETYPE + +/***************************************************************************** + QPixmap member functions + *****************************************************************************/ + +/*! + \internal + Initializes the pixmap data. +*/ + +void QPixmap::init( int w, int h, int d, bool bitmap, Optimization optim ) +{ +#if defined(QT_CHECK_STATE) + if ( qApp->type() == QApplication::Tty ) { + qWarning( "QPixmap: Cannot create a QPixmap when no GUI " + "is being used" ); + } +#endif + + static int serial = 0; + + if ( defaultScreen >= 0 && defaultScreen != x11Screen() ) { + QPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = defaultScreen; + xd->x_depth = QPaintDevice::x11AppDepth( xd->x_screen ); + xd->x_cells = QPaintDevice::x11AppCells( xd->x_screen ); + xd->x_colormap = QPaintDevice::x11AppColormap( xd->x_screen ); + xd->x_defcolormap = QPaintDevice::x11AppDefaultColormap( xd->x_screen ); + xd->x_visual = QPaintDevice::x11AppVisual( xd->x_screen ); + xd->x_defvisual = QPaintDevice::x11AppDefaultVisual( xd->x_screen ); + setX11Data( xd ); + } + + int dd = x11Depth(); + + if ( d != -1 ) + dd = d; + + if ( optim == DefaultOptim ) // use default optimization + optim = defOptim; + + data = new QPixmapData; + Q_CHECK_PTR( data ); + + memset( data, 0, sizeof(QPixmapData) ); + data->count = 1; + data->uninit = TRUE; + data->bitmap = bitmap; + data->ser_no = ++serial; + data->optim = optim; + + bool make_null = w == 0 || h == 0; // create null pixmap + if ( d == 1 ) // monocrome pixmap + data->d = 1; + else if ( d < 0 || d == dd ) // def depth pixmap + data->d = dd; + if ( make_null || w < 0 || h < 0 || data->d == 0 ) { + hd = 0; + rendhd = 0; +#if defined(QT_CHECK_RANGE) + if ( !make_null ) + qWarning( "QPixmap: Invalid pixmap parameters" ); +#endif + return; + } + data->w = w; + data->h = h; + hd = (HANDLE)XCreatePixmap( x11Display(), RootWindow(x11Display(), x11Screen() ), + w, h, data->d ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) { + if ( data->d == 1 ) { + rendhd = (HANDLE) XftDrawCreateBitmap( x11Display(), hd ); + } else { + rendhd = (HANDLE) XftDrawCreate( x11Display(), hd, + (Visual *) x11Visual(), + x11Colormap() ); + } + } +#endif // QT_NO_XFTFREETYPE + +} + + +void QPixmap::deref() +{ + if ( data && data->deref() ) { // last reference lost + delete data->mask; + delete data->alphapm; + if ( data->ximage ) + qSafeXDestroyImage( (XImage*)data->ximage ); + if ( data->maskgc ) + XFreeGC( x11Display(), (GC)data->maskgc ); + if ( qApp && hd) { + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + XFreePixmap( x11Display(), hd ); + hd = 0; + } + delete data; + } +} + + +/*! + Constructs a monochrome pixmap, with width \a w and height \a h, + that is initialized with the data in \a bits. The \a isXbitmap + indicates whether the data is an X bitmap and defaults to FALSE. + This constructor is protected and used by the QBitmap class. +*/ + +QPixmap::QPixmap( int w, int h, const uchar *bits, bool isXbitmap) + : QPaintDevice( QInternal::Pixmap ) +{ // for bitmaps only + init( 0, 0, 0, FALSE, defOptim ); + if ( w <= 0 || h <= 0 ) // create null pixmap + return; + + data->uninit = FALSE; + data->w = w; + data->h = h; + data->d = 1; + uchar *flipped_bits; + if ( isXbitmap ) { + flipped_bits = 0; + } else { // not X bitmap -> flip bits + flipped_bits = flip_bits( bits, ((w+7)/8)*h ); + bits = flipped_bits; + } + hd = (HANDLE)XCreateBitmapFromData( x11Display(), + RootWindow(x11Display(), x11Screen() ), + (char *)bits, w, h ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) + rendhd = (HANDLE) XftDrawCreateBitmap (x11Display (), hd); +#endif // QT_NO_XFTFREETYPE + + if ( flipped_bits ) // Avoid purify complaint + delete [] flipped_bits; +} + + +/*! + This is a special-purpose function that detaches the pixmap from + shared pixmap data. + + A pixmap is automatically detached by Qt whenever its contents is + about to change. This is done in all QPixmap member functions + that modify the pixmap (fill(), resize(), convertFromImage(), + load(), etc.), in bitBlt() for the destination pixmap and in + QPainter::begin() on a pixmap. + + It is possible to modify a pixmap without letting Qt know. You can + first obtain the system-dependent handle() and then call + system-specific functions (for instance, BitBlt under Windows) + that modify the pixmap contents. In such cases, you can call + detach() to cut the pixmap loose from other pixmaps that share + data with this one. + + detach() returns immediately if there is just a single reference + or if the pixmap has not been initialized yet. +*/ + +void QPixmap::detach() +{ + if ( data->count != 1 ) + *this = copy(); + data->uninit = FALSE; + + // reset cached data + if ( data->ximage ) { + qSafeXDestroyImage( (XImage*)data->ximage ); + data->ximage = 0; + } + if ( data->maskgc ) { + XFreeGC( x11Display(), (GC)data->maskgc ); + data->maskgc = 0; + } +} + + +/*! + Returns the default pixmap depth, i.e. the depth a pixmap gets if + -1 is specified. + + \sa depth() +*/ + +int QPixmap::defaultDepth() +{ + return x11AppDepth(); +} + + +/*! + \fn QPixmap::Optimization QPixmap::optimization() const + + Returns the optimization setting for this pixmap. + + The default optimization setting is \c QPixmap::NormalOptim. You + can change this setting in two ways: + \list + \i Call setDefaultOptimization() to set the default optimization + for all new pixmaps. + \i Call setOptimization() to set the optimization for individual + pixmaps. + \endlist + + \sa setOptimization(), setDefaultOptimization(), defaultOptimization() +*/ + +/*! + Sets pixmap drawing optimization for this pixmap. + + The \a optimization setting affects pixmap operations, in + particular drawing of transparent pixmaps (bitBlt() a pixmap with + a mask set) and pixmap transformations (the xForm() function). + + Pixmap optimization involves keeping intermediate results in a + cache buffer and using the cache to speed up bitBlt() and xForm(). + The cost is more memory consumption, up to twice as much as an + unoptimized pixmap. + + Use the setDefaultOptimization() to change the default + optimization for all new pixmaps. + + \sa optimization(), setDefaultOptimization(), defaultOptimization() +*/ + +void QPixmap::setOptimization( Optimization optimization ) +{ + if ( optimization == data->optim ) + return; + detach(); + data->optim = optimization == DefaultOptim ? + defOptim : optimization; + if ( data->optim == MemoryOptim && data->ximage ) { + qSafeXDestroyImage( (XImage*)data->ximage ); + data->ximage = 0; + } +} + + +/*! + Fills the pixmap with the color \a fillColor. +*/ + +void QPixmap::fill( const QColor &fillColor ) +{ + if ( isNull() ) + return; + detach(); // detach other references + GC gc = qt_xget_temp_gc( x11Screen(), depth()==1 ); + XSetForeground( x11Display(), gc, fillColor.pixel(x11Screen()) ); + XFillRectangle( x11Display(), hd, gc, 0, 0, width(), height() ); +} + + +/*! + Internal implementation of the virtual QPaintDevice::metric() function. + + Use the QPaintDeviceMetrics class instead. + + \a m is the metric to get. +*/ + +int QPixmap::metric( int m ) const +{ + int val; + if ( m == QPaintDeviceMetrics::PdmWidth ) + val = width(); + else if ( m == QPaintDeviceMetrics::PdmHeight ) { + val = height(); + } else { + Display *dpy = x11Display(); + int scr = x11Screen(); + switch ( m ) { + case QPaintDeviceMetrics::PdmDpiX: + case QPaintDeviceMetrics::PdmPhysicalDpiX: + val = QPaintDevice::x11AppDpiX( scr ); + break; + case QPaintDeviceMetrics::PdmDpiY: + case QPaintDeviceMetrics::PdmPhysicalDpiY: + val = QPaintDevice::x11AppDpiY( scr ); + break; + case QPaintDeviceMetrics::PdmWidthMM: + val = (DisplayWidthMM(dpy,scr)*width())/ + DisplayWidth(dpy,scr); + break; + case QPaintDeviceMetrics::PdmHeightMM: + val = (DisplayHeightMM(dpy,scr)*height())/ + DisplayHeight(dpy,scr); + break; + case QPaintDeviceMetrics::PdmNumColors: + val = 1 << depth(); + break; + case QPaintDeviceMetrics::PdmDepth: + val = depth(); + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::metric: Invalid metric command" ); +#endif + } + } + return val; +} + +/*! + Converts the pixmap to a QImage. Returns a null image if it fails. + + If the pixmap has 1-bit depth, the returned image will also be 1 + bit deep. If the pixmap has 2- to 8-bit depth, the returned image + has 8-bit depth. If the pixmap has greater than 8-bit depth, the + returned image has 32-bit depth. + + Note that for the moment, alpha masks on monochrome images are + ignored. + + \sa convertFromImage() +*/ + +QImage QPixmap::convertToImage() const +{ + QImage image; + if ( isNull() ) + return image; // null image + + int w = width(); + int h = height(); + int d = depth(); + bool mono = d == 1; + Visual *visual = (Visual *)x11Visual(); + bool trucol = (visual->c_class == TrueColor || visual->c_class == DirectColor) && !mono && d > 8; + + if ( d > 1 && d <= 8 ) // set to nearest valid depth + d = 8; // 2..8 ==> 8 + // we could run into the situation where d == 8 AND trucol is true, which can + // cause problems when converting to and from images. in this case, always treat + // the depth as 32... from Klaus Schmidinger and qt-bugs/arc-15/31333. + if ( d > 8 || trucol ) + d = 32; // > 8 ==> 32 + + XImage *xi = (XImage *)data->ximage; // any cached ximage? +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_ximage = false; + XShmSegmentInfo shminfo; +#endif + if ( !xi ) { // fetch data from X server +#ifdef QT_MITSHM_CONVERSIONS + xi = qt_XShmGetImage( this, mono ? XYPixmap : ZPixmap, &shminfo ); + if( xi ) { + mitshm_ximage = true; + } else +#endif + xi = XGetImage( x11Display(), hd, 0, 0, w, h, AllPlanes, + mono ? XYPixmap : ZPixmap ); + } + Q_CHECK_PTR( xi ); + if (!xi) + return image; // null image + + QImage::Endian bitOrder = QImage::IgnoreEndian; + if ( mono ) { + bitOrder = xi->bitmap_bit_order == LSBFirst ? + QImage::LittleEndian : QImage::BigEndian; + } + image.create( w, h, d, 0, bitOrder ); + if ( image.isNull() ) { // could not create image +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + return image; + } + + const QPixmap* msk = mask(); + const QPixmap *alf = data->alphapm; + + QImage alpha; + if (alf) { + XImage* axi; +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_aximage = false; + XShmSegmentInfo ashminfo; + axi = qt_XShmGetImage( alf, ZPixmap, &ashminfo ); + if( axi ) { + mitshm_aximage = true; + } else +#endif + axi = XGetImage(x11Display(), alf->hd, 0, 0, w, h, AllPlanes, ZPixmap); + + if (axi) { + image.setAlphaBuffer( TRUE ); + alpha.create(w, h, 8); + + // copy each scanline + char *src = axi->data; + int bpl = QMIN(alpha.bytesPerLine(), axi->bytes_per_line); + for (int y = 0; y < h; y++ ) { + memcpy( alpha.scanLine(y), src, bpl ); + src += axi->bytes_per_line; + } + +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_aximage ) + qt_XShmDestroyImage( axi, &ashminfo ); + else +#endif + qSafeXDestroyImage( axi ); + } + } else if (msk) { + image.setAlphaBuffer( TRUE ); + alpha = msk->convertToImage(); + } + bool ale = alpha.bitOrder() == QImage::LittleEndian; + + if ( trucol ) { // truecolor + const uint red_mask = (uint)visual->red_mask; + const uint green_mask = (uint)visual->green_mask; + const uint blue_mask = (uint)visual->blue_mask; + const int red_shift = highest_bit( red_mask ) - 7; + const int green_shift = highest_bit( green_mask ) - 7; + const int blue_shift = highest_bit( blue_mask ) - 7; + + const uint red_bits = n_bits( red_mask ); + const uint green_bits = n_bits( green_mask ); + const uint blue_bits = n_bits( blue_mask ); + + static uint red_table_bits = 0; + static uint green_table_bits = 0; + static uint blue_table_bits = 0; + + if ( red_bits < 8 && red_table_bits != red_bits) { + build_scale_table( &red_scale_table, red_bits ); + red_table_bits = red_bits; + } + if ( blue_bits < 8 && blue_table_bits != blue_bits) { + build_scale_table( &blue_scale_table, blue_bits ); + blue_table_bits = blue_bits; + } + if ( green_bits < 8 && green_table_bits != green_bits) { + build_scale_table( &green_scale_table, green_bits ); + green_table_bits = green_bits; + } + + int r, g, b; + + QRgb *dst; + uchar *src; + uint pixel; + int bppc = xi->bits_per_pixel; + + if ( bppc > 8 && xi->byte_order == LSBFirst ) + bppc++; + + for ( int y=0; y<h; y++ ) { + uchar* asrc = alf || msk ? alpha.scanLine( y ) : 0; + dst = (QRgb *)image.scanLine( y ); + src = (uchar *)xi->data + xi->bytes_per_line*y; + for ( int x=0; x<w; x++ ) { + switch ( bppc ) { + case 8: + pixel = *src++; + break; + case 16: // 16 bit MSB + pixel = src[1] | (ushort)src[0] << 8; + src += 2; + break; + case 17: // 16 bit LSB + pixel = src[0] | (ushort)src[1] << 8; + src += 2; + break; + case 24: // 24 bit MSB + pixel = src[2] | (ushort)src[1] << 8 | + (uint)src[0] << 16; + src += 3; + break; + case 25: // 24 bit LSB + pixel = src[0] | (ushort)src[1] << 8 | + (uint)src[2] << 16; + src += 3; + break; + case 32: // 32 bit MSB + pixel = src[3] | (ushort)src[2] << 8 | + (uint)src[1] << 16 | (uint)src[0] << 24; + src += 4; + break; + case 33: // 32 bit LSB + pixel = src[0] | (ushort)src[1] << 8 | + (uint)src[2] << 16 | (uint)src[3] << 24; + src += 4; + break; + default: // should not really happen + x = w; // leave loop + y = h; + pixel = 0; // eliminate compiler warning +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::convertToImage: Invalid depth %d", + bppc ); +#endif + } + if ( red_shift > 0 ) + r = (pixel & red_mask) >> red_shift; + else + r = (pixel & red_mask) << -red_shift; + if ( green_shift > 0 ) + g = (pixel & green_mask) >> green_shift; + else + g = (pixel & green_mask) << -green_shift; + if ( blue_shift > 0 ) + b = (pixel & blue_mask) >> blue_shift; + else + b = (pixel & blue_mask) << -blue_shift; + + if ( red_bits < 8 ) + r = red_scale_table[r]; + if ( green_bits < 8 ) + g = green_scale_table[g]; + if ( blue_bits < 8 ) + b = blue_scale_table[b]; + + if (alf) { + *dst++ = qRgba(r, g, b, asrc[x]); + } else if (msk) { + if ( ale ) { + *dst++ = (asrc[x >> 3] & (1 << (x & 7))) + ? qRgba(r, g, b, 0xff) : qRgba(r, g, b, 0x00); + } else { + *dst++ = (asrc[x >> 3] & (1 << (7 -(x & 7)))) + ? qRgba(r, g, b, 0xff) : qRgba(r, g, b, 0x00); + } + } else { + *dst++ = qRgb(r, g, b); + } + } + } + } else if ( xi->bits_per_pixel == d ) { // compatible depth + char *xidata = xi->data; // copy each scanline + int bpl = QMIN(image.bytesPerLine(),xi->bytes_per_line); + for ( int y=0; y<h; y++ ) { + memcpy( image.scanLine(y), xidata, bpl ); + xidata += xi->bytes_per_line; + } + } else { + /* Typically 2 or 4 bits display depth */ +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::convertToImage: Display not supported (bpp=%d)", + xi->bits_per_pixel ); +#endif + image.reset(); +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + return image; + } + + if ( mono ) { // bitmap + image.setNumColors( 2 ); + image.setColor( 0, qRgb(255,255,255) ); + image.setColor( 1, qRgb(0,0,0) ); + } else if ( !trucol ) { // pixmap with colormap + register uchar *p; + uchar *end; + uchar use[256]; // pixel-in-use table + uchar pix[256]; // pixel translation table + int ncols, i, bpl; + memset( use, 0, 256 ); + memset( pix, 0, 256 ); + bpl = image.bytesPerLine(); + + if (msk) { // which pixels are used? + for ( i=0; i<h; i++ ) { + uchar* asrc = alpha.scanLine( i ); + p = image.scanLine( i ); + for ( int x = 0; x < w; x++ ) { + if ( ale ) { + if (asrc[x >> 3] & (1 << (x & 7))) + use[*p] = 1; + } else { + if (asrc[x >> 3] & (1 << (7 -(x & 7)))) + use[*p] = 1; + } + ++p; + } + } + } else { + for ( i=0; i<h; i++ ) { + p = image.scanLine( i ); + end = p + bpl; + while ( p < end ) + use[*p++] = 1; + } + } + ncols = 0; + for ( i=0; i<256; i++ ) { // build translation table + if ( use[i] ) + pix[i] = ncols++; + } + for ( i=0; i<h; i++ ) { // translate pixels + p = image.scanLine( i ); + end = p + bpl; + while ( p < end ) { + *p = pix[*p]; + p++; + } + } + + Colormap cmap = x11Colormap(); + int ncells = x11Cells(); + XColor *carr = new XColor[ncells]; + for ( i=0; i<ncells; i++ ) + carr[i].pixel = i; + // Get default colormap + XQueryColors( x11Display(), cmap, carr, ncells ); + + if (msk) { + int trans; + if (ncols < 256) { + trans = ncols++; + image.setNumColors( ncols ); // create color table + image.setColor( trans, 0x00000000 ); + } else { + image.setNumColors( ncols ); // create color table + // oh dear... no spare "transparent" pixel. + // use first pixel in image (as good as any). + trans = image.scanLine( i )[0]; + } + for ( i=0; i<h; i++ ) { + uchar* asrc = alpha.scanLine( i ); + p = image.scanLine( i ); + for ( int x = 0; x < w; x++ ) { + if ( ale ) { + if (!(asrc[x >> 3] & (1 << (x & 7)))) + *p = trans; + } else { + if (!(asrc[x >> 3] & (1 << (7 -(x & 7))))) + *p = trans; + } + ++p; + } + } + } else { + image.setNumColors( ncols ); // create color table + } + int j = 0; + for ( i=0; i<256; i++ ) { // translate pixels + if ( use[i] ) { + image.setColor( j++, + ( msk ? 0xff000000 : 0 ) + | qRgb( (carr[i].red >> 8) & 255, + (carr[i].green >> 8) & 255, + (carr[i].blue >> 8) & 255 ) ); + } + } + + delete [] carr; + } + if ( data->optim != BestOptim ) { // throw away image data +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + ((QPixmap*)this)->data->ximage = 0; + } else { // keep ximage data +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) { // copy the XImage? + qt_XShmDestroyImage( xi, &shminfo ); + xi = 0; + } +#endif + ((QPixmap*)this)->data->ximage = xi; + } + + return image; +} + + +/*! + Converts image \a img and sets this pixmap. Returns TRUE if + successful; otherwise returns FALSE. + + The \a conversion_flags argument is a bitwise-OR of the + \l{Qt::ImageConversionFlags}. Passing 0 for \a conversion_flags + sets all the default options. + + Note that even though a QPixmap with depth 1 behaves much like a + QBitmap, isQBitmap() returns FALSE. + + If a pixmap with depth 1 is painted with color0 and color1 and + converted to an image, the pixels painted with color0 will produce + pixel index 0 in the image and those painted with color1 will + produce pixel index 1. + + \sa convertToImage(), isQBitmap(), QImage::convertDepth(), + defaultDepth(), QImage::hasAlphaBuffer() +*/ + +bool QPixmap::convertFromImage( const QImage &img, int conversion_flags ) +{ + if ( img.isNull() ) { +#if defined(QT_CHECK_NULL) + qWarning( "QPixmap::convertFromImage: Cannot convert a null image" ); +#endif + return FALSE; + } + detach(); // detach other references + QImage image = img; + const uint w = image.width(); + const uint h = image.height(); + int d = image.depth(); + const int dd = x11Depth(); + bool force_mono = (dd == 1 || isQBitmap() || + (conversion_flags & ColorMode_Mask)==MonoOnly ); + + if ( w >= 32768 || h >= 32768 ) + return FALSE; + + // get rid of the mask + delete data->mask; + data->mask = 0; + + // get rid of alpha pixmap + delete data->alphapm; + data->alphapm = 0; + + // must be monochrome + if ( force_mono ) { + if ( d != 1 ) { + // dither + image = image.convertDepth( 1, conversion_flags ); + d = 1; + } + } else { // can be both + bool conv8 = FALSE; + if ( d > 8 && dd <= 8 ) { // convert to 8 bit + if ( (conversion_flags & DitherMode_Mask) == AutoDither ) + conversion_flags = (conversion_flags & ~DitherMode_Mask) + | PreferDither; + conv8 = TRUE; + } else if ( (conversion_flags & ColorMode_Mask) == ColorOnly ) { + conv8 = d == 1; // native depth wanted + } else if ( d == 1 ) { + if ( image.numColors() == 2 ) { + QRgb c0 = image.color(0); // Auto: convert to best + QRgb c1 = image.color(1); + conv8 = QMIN(c0,c1) != qRgb(0,0,0) || QMAX(c0,c1) != qRgb(255,255,255); + } else { + // eg. 1-color monochrome images (they do exist). + conv8 = TRUE; + } + } + if ( conv8 ) { + image = image.convertDepth( 8, conversion_flags ); + d = 8; + } + } + + if ( d == 1 ) { // 1 bit pixmap (bitmap) + if ( hd ) { // delete old X pixmap + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + XFreePixmap( x11Display(), hd ); + } + + // make sure image.color(0) == color0 (white) and image.color(1) == color1 (black) + if (image.color(0) == Qt::black.rgb() && image.color(1) == Qt::white.rgb()) { + image.invertPixels(); + image.setColor(0, Qt::white.rgb()); + image.setColor(1, Qt::black.rgb()); + } + + char *bits; + uchar *tmp_bits; + int bpl = (w+7)/8; + int ibpl = image.bytesPerLine(); + if ( image.bitOrder() == QImage::BigEndian || bpl != ibpl ) { + tmp_bits = new uchar[bpl*h]; + Q_CHECK_PTR( tmp_bits ); + bits = (char *)tmp_bits; + uchar *p, *b, *end; + uint y, count; + if ( image.bitOrder() == QImage::BigEndian ) { + const uchar *f = qt_get_bitflip_array(); + b = tmp_bits; + for ( y=0; y<h; y++ ) { + p = image.scanLine( y ); + end = p + bpl; + count = bpl; + while ( count > 4 ) { + *b++ = f[*p++]; + *b++ = f[*p++]; + *b++ = f[*p++]; + *b++ = f[*p++]; + count -= 4; + } + while ( p < end ) + *b++ = f[*p++]; + } + } else { // just copy + b = tmp_bits; + p = image.scanLine( 0 ); + for ( y=0; y<h; y++ ) { + memcpy( b, p, bpl ); + b += bpl; + p += ibpl; + } + } + } else { + bits = (char *)image.bits(); + tmp_bits = 0; + } + hd = (HANDLE)XCreateBitmapFromData( x11Display(), + RootWindow(x11Display(), x11Screen() ), + bits, w, h ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) + rendhd = (HANDLE) XftDrawCreateBitmap( x11Display(), hd ); +#endif // QT_NO_XFTFREETYPE + + if ( tmp_bits ) // Avoid purify complaint + delete [] tmp_bits; + data->w = w; data->h = h; data->d = 1; + + if ( image.hasAlphaBuffer() ) { + QBitmap m; + m = image.createAlphaMask( conversion_flags ); + setMask( m ); + } + return TRUE; + } + + Display *dpy = x11Display(); + Visual *visual = (Visual *)x11Visual(); + XImage *xi = 0; + bool trucol = (visual->c_class == TrueColor || visual->c_class == DirectColor); + int nbytes = image.numBytes(); + uchar *newbits= 0; + int newbits_size = 0; +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_ximage = false; + XShmSegmentInfo shminfo; +#endif + + if ( trucol ) { // truecolor display + QRgb pix[256]; // pixel translation table + const bool d8 = d == 8; + const uint red_mask = (uint)visual->red_mask; + const uint green_mask = (uint)visual->green_mask; + const uint blue_mask = (uint)visual->blue_mask; + const int red_shift = highest_bit( red_mask ) - 7; + const int green_shift = highest_bit( green_mask ) - 7; + const int blue_shift = highest_bit( blue_mask ) - 7; + const uint rbits = highest_bit(red_mask) - lowest_bit(red_mask) + 1; + const uint gbits = highest_bit(green_mask) - lowest_bit(green_mask) + 1; + const uint bbits = highest_bit(blue_mask) - lowest_bit(blue_mask) + 1; + + if ( d8 ) { // setup pixel translation + QRgb *ctable = image.colorTable(); + for ( int i=0; i<image.numColors(); i++ ) { + int r = qRed (ctable[i]); + int g = qGreen(ctable[i]); + int b = qBlue (ctable[i]); + r = red_shift > 0 ? r << red_shift : r >> -red_shift; + g = green_shift > 0 ? g << green_shift : g >> -green_shift; + b = blue_shift > 0 ? b << blue_shift : b >> -blue_shift; + pix[i] = (b & blue_mask) | (g & green_mask) | (r & red_mask) + | ~(blue_mask | green_mask | red_mask); + } + } + +#ifdef QT_MITSHM_CONVERSIONS + xi = qt_XShmCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0, &shminfo ); + if( xi != NULL ) { + mitshm_ximage = true; + newbits = (uchar*)xi->data; + } + else +#endif + xi = XCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0 ); + if (!xi) + return false; + if( newbits == NULL ) + newbits = (uchar *)malloc( xi->bytes_per_line*h ); + Q_CHECK_PTR( newbits ); + if ( !newbits ) // no memory + return FALSE; + int bppc = xi->bits_per_pixel; + + bool contig_bits = n_bits(red_mask) == rbits && + n_bits(green_mask) == gbits && + n_bits(blue_mask) == bbits; + bool dither_tc = + // Want it? + (conversion_flags & Dither_Mask) != ThresholdDither && + (conversion_flags & DitherMode_Mask) != AvoidDither && + // Need it? + bppc < 24 && !d8 && + // Can do it? (Contiguous bits?) + contig_bits; + + static bool init=FALSE; + static int D[16][16]; + if ( dither_tc && !init ) { + // I also contributed this code to XV - WWA. + /* + The dither matrix, D, is obtained with this formula: + + D2 = [ 0 2 ] + [ 3 1 ] + + + D2*n = [ 4*Dn 4*Dn+2*Un ] + [ 4*Dn+3*Un 4*Dn+1*Un ] + */ + int n,i,j; + init=1; + + /* Set D2 */ + D[0][0]=0; + D[1][0]=2; + D[0][1]=3; + D[1][1]=1; + + /* Expand using recursive definition given above */ + for (n=2; n<16; n*=2) { + for (i=0; i<n; i++) { + for (j=0; j<n; j++) { + D[i][j]*=4; + D[i+n][j]=D[i][j]+2; + D[i][j+n]=D[i][j]+3; + D[i+n][j+n]=D[i][j]+1; + } + } + } + init=TRUE; + } + + enum { BPP8, + BPP16_8_3_M3, BPP16_7_2_M3, BPP16_MSB, BPP16_LSB, + BPP24_MSB, BPP24_LSB, + BPP32_16_8_0, BPP32_MSB, BPP32_LSB + } mode = BPP8; + + if ( bppc > 8 && xi->byte_order == LSBFirst ) + bppc++; + + int wordsize; + bool bigendian; + qSysInfo( &wordsize, &bigendian ); + bool same_msb_lsb = ( xi->byte_order == MSBFirst ) == ( bigendian ); + + if( bppc == 8 ) // 8 bit + mode = BPP8; + else if( bppc == 16 || bppc == 17 ) { // 16 bit MSB/LSB + if( red_shift == 8 && green_shift == 3 && blue_shift == -3 + && !d8 && same_msb_lsb ) + mode = BPP16_8_3_M3; + else if( red_shift == 7 && green_shift == 2 && blue_shift == -3 + && !d8 && same_msb_lsb ) + mode = BPP16_7_2_M3; + else + mode = bppc == 17 ? BPP16_LSB : BPP16_MSB; + } else if( bppc == 24 || bppc == 25 ) { // 24 bit MSB/LSB + mode = bppc == 25 ? BPP24_LSB : BPP24_MSB; + } else if( bppc == 32 || bppc == 33 ) { // 32 bit MSB/LSB + if( red_shift == 16 && green_shift == 8 && blue_shift == 0 + && !d8 && same_msb_lsb ) + mode = BPP32_16_8_0; + else + mode = bppc == 33 ? BPP32_LSB : BPP32_MSB; + } else + qFatal("Logic error 3"); + +#define GET_PIXEL \ + int pixel; \ + if ( d8 ) pixel = pix[*src++]; \ + else { \ + int r = qRed ( *p ); \ + int g = qGreen( *p ); \ + int b = qBlue ( *p++ ); \ + r = red_shift > 0 \ + ? r << red_shift : r >> -red_shift; \ + g = green_shift > 0 \ + ? g << green_shift : g >> -green_shift; \ + b = blue_shift > 0 \ + ? b << blue_shift : b >> -blue_shift; \ + pixel = (r & red_mask)|(g & green_mask) | (b & blue_mask) \ + | ~(blue_mask | green_mask | red_mask); \ + } + +// optimized case - no d8 case, shift only once instead of twice, mask only once instead of twice, +// use direct values instead of variables, and use only one statement +// (*p >> 16), (*p >> 8 ) and (*p) are qRed(),qGreen() and qBlue() without masking +// shifts have to be passed including the shift operator (e.g. '>>3'), because of the direction +#define GET_PIXEL_OPT(red_shift,green_shift,blue_shift,red_mask,green_mask,blue_mask) \ + int pixel = ((( *p >> 16 ) red_shift ) & red_mask ) \ + | ((( *p >> 8 ) green_shift ) & green_mask ) \ + | ((( *p ) blue_shift ) & blue_mask ); \ + ++p; + +#define GET_PIXEL_DITHER_TC \ + int r = qRed ( *p ); \ + int g = qGreen( *p ); \ + int b = qBlue ( *p++ ); \ + const int thres = D[x%16][y%16]; \ + if ( r <= (255-(1<<(8-rbits))) && ((r<<rbits) & 255) \ + > thres) \ + r += (1<<(8-rbits)); \ + if ( g <= (255-(1<<(8-gbits))) && ((g<<gbits) & 255) \ + > thres) \ + g += (1<<(8-gbits)); \ + if ( b <= (255-(1<<(8-bbits))) && ((b<<bbits) & 255) \ + > thres) \ + b += (1<<(8-bbits)); \ + r = red_shift > 0 \ + ? r << red_shift : r >> -red_shift; \ + g = green_shift > 0 \ + ? g << green_shift : g >> -green_shift; \ + b = blue_shift > 0 \ + ? b << blue_shift : b >> -blue_shift; \ + int pixel = (r & red_mask)|(g & green_mask) | (b & blue_mask); + +// again, optimized case +// can't be optimized that much :( +#define GET_PIXEL_DITHER_TC_OPT(red_shift,green_shift,blue_shift,red_mask,green_mask,blue_mask, \ + rbits,gbits,bbits) \ + const int thres = D[x%16][y%16]; \ + int r = qRed ( *p ); \ + if ( r <= (255-(1<<(8-rbits))) && ((r<<rbits) & 255) \ + > thres) \ + r += (1<<(8-rbits)); \ + int g = qGreen( *p ); \ + if ( g <= (255-(1<<(8-gbits))) && ((g<<gbits) & 255) \ + > thres) \ + g += (1<<(8-gbits)); \ + int b = qBlue ( *p++ ); \ + if ( b <= (255-(1<<(8-bbits))) && ((b<<bbits) & 255) \ + > thres) \ + b += (1<<(8-bbits)); \ + int pixel = (( r red_shift ) & red_mask ) \ + | (( g green_shift ) & green_mask ) \ + | (( b blue_shift ) & blue_mask ); + +#define CYCLE(body) \ + for ( uint y=0; y<h; y++ ) { \ + uchar* src = image.scanLine( y ); \ + uchar* dst = newbits + xi->bytes_per_line*y; \ + QRgb* p = (QRgb *)src; \ + body \ + } + + if ( dither_tc ) { + switch ( mode ) { + case BPP16_8_3_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x<w; x++ ) { + GET_PIXEL_DITHER_TC_OPT(<<8,<<3,>>3,0xf800,0x7e0,0x1f,5,6,5) + *dst16++ = pixel; + } + ) + break; + case BPP16_7_2_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x<w; x++ ) { + GET_PIXEL_DITHER_TC_OPT(<<7,<<2,>>3,0x7c00,0x3e0,0x1f,5,5,5) + *dst16++ = pixel; + } + ) + break; + case BPP16_MSB: // 16 bit MSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL_DITHER_TC + *dst++ = (pixel >> 8); + *dst++ = pixel; + } + ) + break; + case BPP16_LSB: // 16 bit LSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL_DITHER_TC + *dst++ = pixel; + *dst++ = pixel >> 8; + } + ) + break; + default: + qFatal("Logic error"); + } + } else { + switch ( mode ) { + case BPP8: // 8 bit + CYCLE( + Q_UNUSED(p); + for ( uint x=0; x<w; x++ ) { + int pixel = pix[*src++]; + *dst++ = pixel; + } + ) + break; + case BPP16_8_3_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x<w; x++ ) { + GET_PIXEL_OPT(<<8,<<3,>>3,0xf800,0x7e0,0x1f) + *dst16++ = pixel; + } + ) + break; + case BPP16_7_2_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x<w; x++ ) { + GET_PIXEL_OPT(<<7,<<2,>>3,0x7c00,0x3e0,0x1f) + *dst16++ = pixel; + } + ) + break; + case BPP16_MSB: // 16 bit MSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL + *dst++ = (pixel >> 8); + *dst++ = pixel; + } + ) + break; + case BPP16_LSB: // 16 bit LSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL + *dst++ = pixel; + *dst++ = pixel >> 8; + } + ) + break; + case BPP24_MSB: // 24 bit MSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL + *dst++ = pixel >> 16; + *dst++ = pixel >> 8; + *dst++ = pixel; + } + ) + break; + case BPP24_LSB: // 24 bit LSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL + *dst++ = pixel; + *dst++ = pixel >> 8; + *dst++ = pixel >> 16; + } + ) + break; + case BPP32_16_8_0: + CYCLE( + memcpy( dst, p, w * 4 ); + ) + break; + case BPP32_MSB: // 32 bit MSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL + *dst++ = pixel >> 24; + *dst++ = pixel >> 16; + *dst++ = pixel >> 8; + *dst++ = pixel; + } + ) + break; + case BPP32_LSB: // 32 bit LSB + CYCLE( + for ( uint x=0; x<w; x++ ) { + GET_PIXEL + *dst++ = pixel; + *dst++ = pixel >> 8; + *dst++ = pixel >> 16; + *dst++ = pixel >> 24; + } + ) + break; + default: + qFatal("Logic error 2"); + } + } + xi->data = (char *)newbits; + } + + if ( d == 8 && !trucol ) { // 8 bit pixmap + int pop[256]; // pixel popularity + + if ( image.numColors() == 0 ) + image.setNumColors( 1 ); + + memset( pop, 0, sizeof(int)*256 ); // reset popularity array + uint i; + for ( i=0; i<h; i++ ) { // for each scanline... + uchar* p = image.scanLine( i ); + uchar *end = p + w; + while ( p < end ) // compute popularity + pop[*p++]++; + } + + newbits = (uchar *)malloc( nbytes ); // copy image into newbits + newbits_size = nbytes; + Q_CHECK_PTR( newbits ); + if ( !newbits ) // no memory + return FALSE; + uchar* p = newbits; + memcpy( p, image.bits(), nbytes ); // copy image data into newbits + + /* + * The code below picks the most important colors. It is based on the + * diversity algorithm, implemented in XV 3.10. XV is (C) by John Bradley. + */ + + struct PIX { // pixel sort element + uchar r,g,b,n; // color + pad + int use; // popularity + int index; // index in colormap + int mindist; + }; + int ncols = 0; + for ( i=0; i< (uint) image.numColors(); i++ ) { // compute number of colors + if ( pop[i] > 0 ) + ncols++; + } + for ( i=image.numColors(); i<256; i++ ) // ignore out-of-range pixels + pop[i] = 0; + + // works since we make sure above to have at least + // one color in the image + if ( ncols == 0 ) + ncols = 1; + + PIX pixarr[256]; // pixel array + PIX pixarr_sorted[256]; // pixel array (sorted) + memset( pixarr, 0, ncols*sizeof(PIX) ); + PIX *px = &pixarr[0]; + int maxpop = 0; + int maxpix = 0; + Q_CHECK_PTR( pixarr ); + uint j = 0; + QRgb* ctable = image.colorTable(); + for ( i=0; i<256; i++ ) { // init pixel array + if ( pop[i] > 0 ) { + px->r = qRed ( ctable[i] ); + px->g = qGreen( ctable[i] ); + px->b = qBlue ( ctable[i] ); + px->n = 0; + px->use = pop[i]; + if ( pop[i] > maxpop ) { // select most popular entry + maxpop = pop[i]; + maxpix = j; + } + px->index = i; + px->mindist = 1000000; + px++; + j++; + } + } + pixarr_sorted[0] = pixarr[maxpix]; + pixarr[maxpix].use = 0; + + for ( i=1; i< (uint) ncols; i++ ) { // sort pixels + int minpix = -1, mindist = -1; + px = &pixarr_sorted[i-1]; + int r = px->r; + int g = px->g; + int b = px->b; + int dist; + if ( (i & 1) || i<10 ) { // sort on max distance + for ( int j=0; j<ncols; j++ ) { + px = &pixarr[j]; + if ( px->use ) { + dist = (px->r - r)*(px->r - r) + + (px->g - g)*(px->g - g) + + (px->b - b)*(px->b - b); + if ( px->mindist > dist ) + px->mindist = dist; + if ( px->mindist > mindist ) { + mindist = px->mindist; + minpix = j; + } + } + } + } else { // sort on max popularity + for ( int j=0; j<ncols; j++ ) { + px = &pixarr[j]; + if ( px->use ) { + dist = (px->r - r)*(px->r - r) + + (px->g - g)*(px->g - g) + + (px->b - b)*(px->b - b); + if ( px->mindist > dist ) + px->mindist = dist; + if ( px->use > mindist ) { + mindist = px->use; + minpix = j; + } + } + } + } + pixarr_sorted[i] = pixarr[minpix]; + pixarr[minpix].use = 0; + } + + uint pix[256]; // pixel translation table + px = &pixarr_sorted[0]; + for ( i=0; i< (uint) ncols; i++ ) { // allocate colors + QColor c( px->r, px->g, px->b ); + pix[px->index] = c.pixel(x11Screen()); + px++; + } + + p = newbits; + for ( i=0; i< (uint) nbytes; i++ ) { // translate pixels + *p = pix[*p]; + p++; + } + } + + if ( !xi ) { // X image not created +#ifdef QT_MITSHM_CONVERSIONS + xi = qt_XShmCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0, &shminfo ); + if( xi != NULL ) + mitshm_ximage = true; + else +#endif + xi = XCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0 ); + if ( xi->bits_per_pixel == 16 ) { // convert 8 bpp ==> 16 bpp + ushort *p2; + int p2inc = xi->bytes_per_line/sizeof(ushort); + ushort *newerbits = (ushort *)malloc( xi->bytes_per_line * h ); + newbits_size = xi->bytes_per_line * h; + Q_CHECK_PTR( newerbits ); + if ( !newerbits ) // no memory + return FALSE; + uchar* p = newbits; + for ( uint y=0; y<h; y++ ) { // OOPS: Do right byte order!! + p2 = newerbits + p2inc*y; + for ( uint x=0; x<w; x++ ) + *p2++ = *p++; + } + free( newbits ); + newbits = (uchar *)newerbits; + } else if ( xi->bits_per_pixel != 8 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::convertFromImage: Display not supported " + "(bpp=%d)", xi->bits_per_pixel ); +#endif + } +#ifdef QT_MITSHM_CONVERSIONS + if( newbits_size > 0 && mitshm_ximage ) { // need to copy to shared memory + memcpy( xi->data, newbits, newbits_size ); + free( newbits ); + newbits = (uchar*)xi->data; + } + else +#endif + xi->data = (char *)newbits; + } + + if ( hd && (width() != (int)w || height() != (int)h || this->depth() != dd) ) { + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + XFreePixmap( dpy, hd ); // don't reuse old pixmap + hd = 0; + } + if ( !hd ) { // create new pixmap + hd = (HANDLE)XCreatePixmap( x11Display(), + RootWindow(x11Display(), x11Screen() ), + w, h, dd ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) { + if ( data->d == 1 ) { + rendhd = (HANDLE) XftDrawCreateBitmap( x11Display (), hd ); + } else { + rendhd = (HANDLE) XftDrawCreate( x11Display (), hd, + (Visual *) x11Visual(), x11Colormap() ); + } + } +#endif // QT_NO_XFTFREETYPE + + } + +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + XShmPutImage( dpy, hd, qt_xget_readonly_gc( x11Screen(), FALSE ), + xi, 0, 0, 0, 0, w, h, False ); + else +#endif + XPutImage( dpy, hd, qt_xget_readonly_gc( x11Screen(), FALSE ), + xi, 0, 0, 0, 0, w, h ); + + data->w = w; + data->h = h; + data->d = dd; + + XImage* axi = NULL; +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_aximage = false; + XShmSegmentInfo ashminfo; +#endif + if ( image.hasAlphaBuffer() ) { + QBitmap m; + m = image.createAlphaMask( conversion_flags ); + setMask( m ); + +#ifndef QT_NO_XFTFREETYPE + // does this image have an alphamap (and not just a 1bpp mask)? + bool alphamap = image.depth() == 32; + if (image.depth() == 8) { + const QRgb * const rgb = image.colorTable(); + for (int i = 0, count = image.numColors(); i < count; ++i) { + const int alpha = qAlpha(rgb[i]); + if (alpha != 0 && alpha != 0xff) { + alphamap = TRUE; + break; + } + } + } + + if (qt_use_xrender && qt_has_xft && alphamap) { + data->alphapm = new QPixmap; // create a null pixmap + + // setup pixmap data + data->alphapm->data->w = w; + data->alphapm->data->h = h; + data->alphapm->data->d = 8; + + // create 8bpp pixmap and render picture + data->alphapm->hd = + XCreatePixmap(x11Display(), RootWindow(x11Display(), x11Screen()), + w, h, 8); + + data->alphapm->rendhd = + (HANDLE) XftDrawCreateAlpha( x11Display(), data->alphapm->hd, 8 ); + +#ifdef QT_MITSHM_CONVERSIONS + axi = qt_XShmCreateImage( x11Display(), (Visual*)x11Visual(), + 8, ZPixmap, 0, 0, w, h, 8, 0, &ashminfo ); + if( axi != NULL ) + mitshm_aximage = true; + else +#endif + axi = XCreateImage(x11Display(), (Visual *) x11Visual(), + 8, ZPixmap, 0, 0, w, h, 8, 0); + + if (axi) { + if( axi->data==NULL ) { + // the data is deleted by qSafeXDestroyImage + axi->data = (char *) malloc(h * axi->bytes_per_line); + Q_CHECK_PTR( axi->data ); + } + char *aptr = axi->data; + + if (image.depth() == 32) { + const int *iptr = (const int *) image.bits(); + if( axi->bytes_per_line == (int)w ) { + int max = w * h; + while (max--) + *aptr++ = *iptr++ >> 24; // squirt + } else { + for (uint i = 0; i < h; ++i ) { + for (uint j = 0; j < w; ++j ) + *aptr++ = *iptr++ >> 24; // squirt + aptr += ( axi->bytes_per_line - w ); + } + } + } else if (image.depth() == 8) { + const QRgb * const rgb = image.colorTable(); + for (uint y = 0; y < h; ++y) { + const uchar *iptr = image.scanLine(y); + for (uint x = 0; x < w; ++x) + *aptr++ = qAlpha(rgb[*iptr++]); + aptr += ( axi->bytes_per_line - w ); + } + } + + GC gc = XCreateGC(x11Display(), data->alphapm->hd, 0, 0); + #ifdef QT_MITSHM_CONVERSIONS + if( mitshm_aximage ) + XShmPutImage( dpy, data->alphapm->hd, gc, axi, 0, 0, 0, 0, w, h, False ); + else +#endif + XPutImage(dpy, data->alphapm->hd, gc, axi, 0, 0, 0, 0, w, h); + XFreeGC(x11Display(), gc); + } + } +#endif // QT_NO_XFTFREETYPE + } + +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage || mitshm_aximage ) + XSync( x11Display(), False ); // wait until processed +#endif + + if ( data->optim != BestOptim ) { // throw away image +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + data->ximage = 0; + } else { // keep ximage that we created +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) { // copy the XImage? + qt_XShmDestroyImage( xi, &shminfo ); + xi = 0; + } +#endif + data->ximage = xi; + } + if( axi ) { +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_aximage ) + qt_XShmDestroyImage( axi, &ashminfo ); + else +#endif + qSafeXDestroyImage(axi); + } + return TRUE; +} + + +/*! + Grabs the contents of the window \a window and makes a pixmap out + of it. Returns the pixmap. + + The arguments \a (x, y) specify the offset in the window, whereas + \a (w, h) specify the width and height of the area to be copied. + + If \a w is negative, the function copies everything to the right + border of the window. If \a h is negative, the function copies + everything to the bottom of the window. + + Note that grabWindow() grabs pixels from the screen, not from the + window. If there is another window partially or entirely over the + one you grab, you get pixels from the overlying window, too. + + Note also that the mouse cursor is generally not grabbed. + + The reason we use a window identifier and not a QWidget is to + enable grabbing of windows that are not part of the application, + window system frames, and so on. + + \warning Grabbing an area outside the screen is not safe in + general. This depends on the underlying window system. + + \warning X11 only: If \a window is not the same depth as the root + window and another window partially or entirely obscures the one + you grab, you will \e not get pixels from the overlying window. + The contests of the obscured areas in the pixmap are undefined and + uninitialized. + + \sa grabWidget() +*/ + +QPixmap QPixmap::grabWindow( WId window, int x, int y, int w, int h ) +{ + if ( w == 0 || h == 0 ) + return QPixmap(); + + Display *dpy = x11AppDisplay(); + XWindowAttributes window_attr; + if ( ! XGetWindowAttributes( dpy, window, &window_attr ) ) + return QPixmap(); + + if ( w < 0 ) + w = window_attr.width - x; + if ( h < 0 ) + h = window_attr.height - y; + + // determine the screen + int scr; + for ( scr = 0; scr < ScreenCount( dpy ); ++scr ) { + if ( window_attr.root == RootWindow( dpy, scr ) ) // found it + break; + } + if ( scr >= ScreenCount( dpy ) ) // sanity check + return QPixmap(); + + + // get the depth of the root window + XWindowAttributes root_attr; + if ( ! XGetWindowAttributes( dpy, window_attr.root, &root_attr ) ) + return QPixmap(); + + if ( window_attr.depth == root_attr.depth ) { + // if the depth of the specified window and the root window are the + // same, grab pixels from the root window (so that we get the any + // overlapping windows and window manager frames) + + // map x and y to the root window + WId unused; + if ( ! XTranslateCoordinates( dpy, window, window_attr.root, x, y, + &x, &y, &unused ) ) + return QPixmap(); + + window = window_attr.root; + } + + QPixmap pm( w, h ); + pm.data->uninit = FALSE; + pm.x11SetScreen( scr ); + + GC gc = qt_xget_temp_gc( scr, FALSE ); + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XCopyArea( dpy, window, pm.handle(), gc, x, y, w, h, 0, 0 ); + XSetSubwindowMode( dpy, gc, ClipByChildren ); + + return pm; +} + +/*! + Returns a copy of the pixmap that is transformed using \a matrix. + The original pixmap is not changed. + + The transformation \a matrix is internally adjusted to compensate + for unwanted translation, i.e. xForm() returns the smallest image + that contains all the transformed points of the original image. + + This function is slow because it involves transformation to a + QImage, non-trivial computations and a transformation back to a + QPixmap. + + \sa trueMatrix(), QWMatrix, QPainter::setWorldMatrix() QImage::xForm() +*/ + +QPixmap QPixmap::xForm( const QWMatrix &matrix ) const +{ + uint w = 0; + uint h = 0; // size of target pixmap + uint ws, hs; // size of source pixmap + uchar *dptr; // data in target pixmap + uint dbpl, dbytes; // bytes per line/bytes total + uchar *sptr; // data in original pixmap + int sbpl; // bytes per line in original + int bpp; // bits per pixel + bool depth1 = depth() == 1; + Display *dpy = x11Display(); + + if ( isNull() ) // this is a null pixmap + return copy(); + + ws = width(); + hs = height(); + + QWMatrix mat( matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), 0., 0. ); + + double scaledWidth; + double scaledHeight; + + if ( matrix.m12() == 0.0F && matrix.m21() == 0.0F ) { + if ( matrix.m11() == 1.0F && matrix.m22() == 1.0F ) + return *this; // identity matrix + scaledHeight = matrix.m22()*hs; + scaledWidth = matrix.m11()*ws; + h = QABS( qRound( scaledHeight ) ); + w = QABS( qRound( scaledWidth ) ); + } else { // rotation or shearing + QPointArray a( QRect(0,0,ws+1,hs+1) ); + a = mat.map( a ); + QRect r = a.boundingRect().normalize(); + w = r.width()-1; + h = r.height()-1; + scaledWidth = w; + scaledHeight = h; + } + + mat = trueMatrix( mat, ws, hs ); // true matrix + + + bool invertible; + mat = mat.invert( &invertible ); // invert matrix + + if ( h == 0 || w == 0 || !invertible + || QABS(scaledWidth) >= 32768 || QABS(scaledHeight) >= 32768 ) { // error, return null pixmap + QPixmap pm; + pm.data->bitmap = data->bitmap; + return pm; + } + +#if defined(QT_MITSHM_XFORM) + static bool try_once = TRUE; + if (try_once) { + try_once = FALSE; + if ( !xshminit ) + qt_create_mitshm_buffer( this, 800, 600 ); + } + + bool use_mitshm = xshmimg && !depth1 && + xshmimg->width >= w && xshmimg->height >= h; +#endif + XImage *xi = (XImage*)data->ximage; // any cached ximage? + if ( !xi ) + xi = XGetImage( x11Display(), handle(), 0, 0, ws, hs, AllPlanes, + depth1 ? XYPixmap : ZPixmap ); + + if ( !xi ) { // error, return null pixmap + QPixmap pm; + pm.data->bitmap = data->bitmap; + pm.data->alphapm = data->alphapm; + return pm; + } + + sbpl = xi->bytes_per_line; + sptr = (uchar *)xi->data; + bpp = xi->bits_per_pixel; + + if ( depth1 ) + dbpl = (w+7)/8; + else + dbpl = ((w*bpp+31)/32)*4; + dbytes = dbpl*h; + +#if defined(QT_MITSHM_XFORM) + if ( use_mitshm ) { + dptr = (uchar *)xshmimg->data; + uchar fillbyte = bpp == 8 ? white.pixel() : 0xff; + for ( int y=0; y<h; y++ ) + memset( dptr + y*xshmimg->bytes_per_line, fillbyte, dbpl ); + } else { +#endif + dptr = (uchar *)malloc( dbytes ); // create buffer for bits + Q_CHECK_PTR( dptr ); + if ( depth1 ) // fill with zeros + memset( dptr, 0, dbytes ); + else if ( bpp == 8 ) // fill with background color + memset( dptr, Qt::white.pixel( x11Screen() ), dbytes ); + else + memset( dptr, 0xff, dbytes ); +#if defined(QT_MITSHM_XFORM) + } +#endif + + // #define QT_DEBUG_XIMAGE +#if defined(QT_DEBUG_XIMAGE) + qDebug( "----IMAGE--INFO--------------" ); + qDebug( "width............. %d", xi->width ); + qDebug( "height............ %d", xi->height ); + qDebug( "xoffset........... %d", xi->xoffset ); + qDebug( "format............ %d", xi->format ); + qDebug( "byte order........ %d", xi->byte_order ); + qDebug( "bitmap unit....... %d", xi->bitmap_unit ); + qDebug( "bitmap bit order.. %d", xi->bitmap_bit_order ); + qDebug( "depth............. %d", xi->depth ); + qDebug( "bytes per line.... %d", xi->bytes_per_line ); + qDebug( "bits per pixel.... %d", xi->bits_per_pixel ); +#endif + + int type; + if ( xi->bitmap_bit_order == MSBFirst ) + type = QT_XFORM_TYPE_MSBFIRST; + else + type = QT_XFORM_TYPE_LSBFIRST; + int xbpl, p_inc; + if ( depth1 ) { + xbpl = (w+7)/8; + p_inc = dbpl - xbpl; + } else { + xbpl = (w*bpp)/8; + p_inc = dbpl - xbpl; +#if defined(QT_MITSHM_XFORM) + if ( use_mitshm ) + p_inc = xshmimg->bytes_per_line - xbpl; +#endif + } + + if ( !qt_xForm_helper( mat, xi->xoffset, type, bpp, dptr, xbpl, p_inc, h, sptr, sbpl, ws, hs ) ){ +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::xForm: display not supported (bpp=%d)",bpp); +#endif + QPixmap pm; + return pm; + } + + if ( data->optim == NoOptim ) { // throw away ximage + qSafeXDestroyImage( xi ); + data->ximage = 0; + } else { // keep ximage that we fetched + data->ximage = xi; + } + + if ( depth1 ) { // mono bitmap + QPixmap pm( w, h, dptr, QImage::systemBitOrder() != QImage::BigEndian ); + pm.data->bitmap = data->bitmap; + free( dptr ); + if ( data->mask ) { + if ( data->selfmask ) // pixmap == mask + pm.setMask( *((QBitmap*)(&pm)) ); + else + pm.setMask( data->mask->xForm(matrix) ); + } + return pm; + } else { // color pixmap + GC gc = qt_xget_readonly_gc( x11Screen(), FALSE ); + QPixmap pm( w, h ); + pm.data->uninit = FALSE; + pm.x11SetScreen( x11Screen() ); +#if defined(QT_MITSHM_XFORM) + if ( use_mitshm ) { + XCopyArea( dpy, xshmpm, pm.handle(), gc, 0, 0, w, h, 0, 0 ); + } else { +#endif + xi = XCreateImage( dpy, (Visual *)x11Visual(), x11Depth(), + ZPixmap, 0, (char *)dptr, w, h, 32, 0 ); + XPutImage( dpy, pm.handle(), gc, xi, 0, 0, 0, 0, w, h); + qSafeXDestroyImage( xi ); +#if defined(QT_MITSHM_XFORM) + } +#endif + + if ( data->mask ) // xform mask, too + pm.setMask( data->mask->xForm(matrix) ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_use_xrender && qt_has_xft && data->alphapm ) { // xform the alpha channel + XImage *axi = 0; + if ((axi = XGetImage(x11Display(), data->alphapm->handle(), + 0, 0, ws, hs, AllPlanes, ZPixmap))) { + sbpl = axi->bytes_per_line; + sptr = (uchar *) axi->data; + bpp = axi->bits_per_pixel; + dbytes = dbpl * h; + dptr = (uchar *) malloc(dbytes); + Q_CHECK_PTR( dptr ); + memset(dptr, 0, dbytes); + if ( axi->bitmap_bit_order == MSBFirst ) + type = QT_XFORM_TYPE_MSBFIRST; + else + type = QT_XFORM_TYPE_LSBFIRST; + + if (qt_xForm_helper( mat, axi->xoffset, type, bpp, dptr, w, + 0, h, sptr, sbpl, ws, hs )) { + delete pm.data->alphapm; + pm.data->alphapm = new QPixmap; // create a null pixmap + + // setup pixmap data + pm.data->alphapm->data->w = w; + pm.data->alphapm->data->h = h; + pm.data->alphapm->data->d = 8; + + // create 8bpp pixmap and render picture + pm.data->alphapm->hd = + XCreatePixmap(x11Display(), + RootWindow(x11Display(), x11Screen()), + w, h, 8); + + pm.data->alphapm->rendhd = + (HANDLE) XftDrawCreateAlpha( x11Display(), + pm.data->alphapm->hd, 8 ); + + XImage *axi2 = XCreateImage(x11Display(), (Visual *) x11Visual(), + 8, ZPixmap, 0, (char *)dptr, w, h, 8, 0); + + if (axi2) { + // the data is deleted by qSafeXDestroyImage + GC gc = XCreateGC(x11Display(), pm.data->alphapm->hd, 0, 0); + XPutImage(dpy, pm.data->alphapm->hd, gc, axi2, 0, 0, 0, 0, w, h); + XFreeGC(x11Display(), gc); + qSafeXDestroyImage(axi2); + } + } + qSafeXDestroyImage(axi); + } + } +#endif // QT_NO_XFTFREETYPE + + return pm; + } +} + + +/*! + \internal +*/ +int QPixmap::x11SetDefaultScreen( int screen ) +{ + int old = defaultScreen; + defaultScreen = screen; + return old; +} + +/*! + \internal +*/ +void QPixmap::x11SetScreen( int screen ) +{ + if ( screen < 0 ) + screen = x11AppScreen(); + + if ( screen == x11Screen() ) + return; // nothing to do + + if ( isNull() ) { + QPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = screen; + xd->x_depth = QPaintDevice::x11AppDepth( screen ); + xd->x_cells = QPaintDevice::x11AppCells( screen ); + xd->x_colormap = QPaintDevice::x11AppColormap( screen ); + xd->x_defcolormap = QPaintDevice::x11AppDefaultColormap( screen ); + xd->x_visual = QPaintDevice::x11AppVisual( screen ); + xd->x_defvisual = QPaintDevice::x11AppDefaultVisual( screen ); + setX11Data( xd ); + return; + } +#if 0 + qDebug("QPixmap::x11SetScreen for %p from %d to %d. Size is %d/%d", data, x11Screen(), screen, width(), height() ); +#endif + + QImage img = convertToImage(); + resize(0,0); + QPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = screen; + xd->x_depth = QPaintDevice::x11AppDepth( screen ); + xd->x_cells = QPaintDevice::x11AppCells( screen ); + xd->x_colormap = QPaintDevice::x11AppColormap( screen ); + xd->x_defcolormap = QPaintDevice::x11AppDefaultColormap( screen ); + xd->x_visual = QPaintDevice::x11AppVisual( screen ); + xd->x_defvisual = QPaintDevice::x11AppDefaultVisual( screen ); + setX11Data( xd ); + convertFromImage( img ); +} + +/*! + Returns TRUE this pixmap has an alpha channel or a mask. + + \sa hasAlphaChannel() mask() +*/ +bool QPixmap::hasAlpha() const +{ + return data->alphapm || data->mask; +} + +/*! + Returns TRUE if the pixmap has an alpha channel; otherwise it + returns FALSE. + + NOTE: If the pixmap has a mask but not alpha channel, this + function returns FALSE. + + \sa hasAlpha() mask() +*/ +bool QPixmap::hasAlphaChannel() const +{ + return data->alphapm != 0; +} + +/*! + \relates QPixmap + + Copies a block of pixels from \a src to \a dst. The alpha channel + and mask data (if any) is also copied from \a src. NOTE: \a src + is \e not alpha blended or masked when copied to \a dst. Use + bitBlt() or QPainter::drawPixmap() to perform alpha blending or + masked drawing. + + \a sx, \a sy is the top-left pixel in \a src (0, 0 by default), \a + dx, \a dy is the top-left position in \a dst and \a sw, \sh is the + size of the copied block (all of \a src by default). + + If \a src, \a dst, \a sw or \a sh is 0 (zero), copyBlt() does + nothing. If \a sw or \a sh is negative, copyBlt() copies starting + at \a sx (and respectively, \a sy) and ending at the right edge + (and respectively, the bottom edge) of \a src. + + copyBlt() does nothing if \a src and \a dst have different depths. +*/ +Q_EXPORT void copyBlt( QPixmap *dst, int dx, int dy, + const QPixmap *src, int sx, int sy, int sw, int sh ) +{ + if ( ! dst || ! src || sw == 0 || sh == 0 || dst->depth() != src->depth() ) { +#ifdef QT_CHECK_NULL + Q_ASSERT( dst != 0 ); + Q_ASSERT( src != 0 ); +#endif + return; + } + + // copy pixel data + bitBlt( dst, dx, dy, src, sx, sy, sw, sh, Qt::CopyROP, TRUE ); + + // copy mask data + if ( src->data->mask ) { + if ( ! dst->data->mask ) { + dst->data->mask = new QBitmap( dst->width(), dst->height() ); + + // new masks are fully opaque by default + dst->data->mask->fill( Qt::color1 ); + } + + bitBlt( dst->data->mask, dx, dy, + src->data->mask, sx, sy, sw, sh, Qt::CopyROP, TRUE ); + } + +#ifndef QT_NO_XFTFREETYPE + // copy alpha data + extern bool qt_use_xrender; // from qapplication_x11.cpp + if ( ! qt_use_xrender || ! src->data->alphapm ) + return; + + if ( sw < 0 ) + sw = src->width() - sx; + else + sw = QMIN( src->width()-sx, sw ); + sw = QMIN( dst->width()-dx, sw ); + + if ( sh < 0 ) + sh = src->height() - sy ; + else + sh = QMIN( src->height()-sy, sh ); + sh = QMIN( dst->height()-dy, sh ); + + if ( sw <= 0 || sh <= 0 ) + return; + + // create an alpha pixmap for dst if it doesn't exist + bool do_init = FALSE; + if ( ! dst->data->alphapm ) { + dst->data->alphapm = new QPixmap; + + // setup pixmap d + dst->data->alphapm->data->w = dst->width(); + dst->data->alphapm->data->h = dst->height(); + dst->data->alphapm->data->d = 8; + + // create 8bpp pixmap and render picture + dst->data->alphapm->hd = + XCreatePixmap(dst->x11Display(), + RootWindow(dst->x11Display(), dst->x11Screen()), + dst->width(), dst->height(), 8); + + // new alpha pixmaps should be fully opaque by default + do_init = TRUE; + + dst->data->alphapm->rendhd = + (Qt::HANDLE) XftDrawCreateAlpha( dst->x11Display(), + dst->data->alphapm->hd, 8 ); + } + + GC gc = XCreateGC(dst->x11Display(), dst->data->alphapm->hd, 0, 0); + + if ( do_init ) { + // the alphapm was just created, make it fully opaque + XSetForeground( dst->x11Display(), gc, 255 ); + XSetBackground( dst->x11Display(), gc, 255 ); + XFillRectangle( dst->x11Display(), dst->data->alphapm->hd, gc, + 0, 0, dst->data->alphapm->data->w, + dst->data->alphapm->data->h ); + } + + XCopyArea(dst->x11Display(), src->data->alphapm->hd, dst->data->alphapm->hd, gc, + sx, sy, sw, sh, dx, dy); + XFreeGC(dst->x11Display(), gc); +#endif // QT_NO_XFTFREETYPE +} diff --git a/src/kernel/qpixmapcache.cpp b/src/kernel/qpixmapcache.cpp new file mode 100644 index 0000000..ee70e19 --- /dev/null +++ b/src/kernel/qpixmapcache.cpp @@ -0,0 +1,336 @@ +/**************************************************************************** +** +** Implementation of QPixmapCache class +** +** Created : 950504 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpixmapcache.h" +#include "qcache.h" +#include "qobject.h" +#include "qcleanuphandler.h" + + +// REVISED: paul +/*! + \class QPixmapCache qpixmapcache.h + + \brief The QPixmapCache class provides an application-global cache for + pixmaps. + + \ingroup environment + \ingroup graphics + \ingroup images + + This class is a tool for optimized drawing with QPixmap. You can + use it to store temporary pixmaps that are expensive to generate + without using more storage space than cacheLimit(). Use insert() + to insert pixmaps, find() to find them and clear() to empty the + cache. + + For example, QRadioButton has a non-trivial visual representation + so we don't want to regenerate a pixmap whenever a radio button is + displayed or changes state. In the function + QRadioButton::drawButton(), we do not draw the radio button + directly. Instead, we first check the global pixmap cache for a + pixmap with the key "$qt_radio_nnn_", where \c nnn is a numerical + value that specifies the the radio button state. If a pixmap is + found, we bitBlt() it onto the widget and return. Otherwise, we + create a new pixmap, draw the radio button in the pixmap, and + finally insert the pixmap in the global pixmap cache, using the + key above. The bitBlt() is ten times faster than drawing the + radio button. All radio buttons in the program share the cached + pixmap since QPixmapCache is application-global. + + QPixmapCache contains no member data, only static functions to + access the global pixmap cache. It creates an internal QCache for + caching the pixmaps. + + The cache associates a pixmap with a string (key). If two pixmaps + are inserted into the cache using equal keys, then the last pixmap + will hide the first pixmap. The QDict and QCache classes do + exactly the same. + + The cache becomes full when the total size of all pixmaps in the + cache exceeds cacheLimit(). The initial cache limit is 1024 KByte + (1 MByte); it is changed with setCacheLimit(). A pixmap takes + roughly width*height*depth/8 bytes of memory. + + See the \l QCache documentation for more details about the cache + mechanism. +*/ + + +static const int cache_size = 149; // size of internal hash array +#ifdef Q_WS_MAC9 +static int cache_limit = 256; // 256 KB cache limit +#else +static int cache_limit = 1024; // 1024 KB cache limit +#endif + +class QPMCache: public QObject, public QCache<QPixmap> +{ +public: + QPMCache(): + QObject( 0, "global pixmap cache" ), + QCache<QPixmap>( cache_limit * 1024, cache_size ), + id( 0 ), ps( 0 ), t( FALSE ) + { + setAutoDelete( TRUE ); + } + ~QPMCache() {} + void timerEvent( QTimerEvent * ); + bool insert( const QString& k, const QPixmap *d, int c, int p = 0 ); +private: + int id; + int ps; + bool t; +}; + + +/* + This is supposed to cut the cache size down by about 80-90% in a + minute once the application becomes idle, to let any inserted pixmap + remain in the cache for some time before it becomes a candidate for + cleaning-up, and to not cut down the size of the cache while the + cache is in active use. + + When the last pixmap has been deleted from the cache, kill the + timer so Qt won't keep the CPU from going into sleep mode. +*/ + +void QPMCache::timerEvent( QTimerEvent * ) +{ + int mc = maxCost(); + bool nt = totalCost() == ps; + setMaxCost( nt ? totalCost() * 3 / 4 : totalCost() -1 ); + setMaxCost( mc ); + ps = totalCost(); + + if ( !count() ) { + killTimer( id ); + id = 0; + } else if ( nt != t ) { + killTimer( id ); + id = startTimer( nt ? 10000 : 30000 ); + t = nt; + } +} + +bool QPMCache::insert( const QString& k, const QPixmap *d, int c, int p ) +{ + bool r = QCache<QPixmap>::insert( k, d, c, p ); + if ( r && !id ) { + id = startTimer( 30000 ); + t = FALSE; + } + return r; +} + +static QPMCache *pm_cache = 0; // global pixmap cache + +static QSingleCleanupHandler<QPMCache> qpm_cleanup_cache; + +/*! + Returns the pixmap associated with the \a key in the cache, or + null if there is no such pixmap. + + \warning If valid, you should copy the pixmap immediately (this is + fast). Subsequent insertions into the cache could cause the + pointer to become invalid. For this reason, we recommend you use + find(const QString&, QPixmap&) instead. + + Example: + \code + QPixmap* pp; + QPixmap p; + if ( (pp=QPixmapCache::find("my_big_image", pm)) ) { + p = *pp; + } else { + p.load("bigimage.png"); + QPixmapCache::insert("my_big_image", new QPixmap(p)); + } + painter->drawPixmap(0, 0, p); + \endcode +*/ + +QPixmap *QPixmapCache::find( const QString &key ) +{ + return pm_cache ? pm_cache->find(key) : 0; +} + + +/*! + \overload + + Looks for a cached pixmap associated with the \a key in the cache. + If a pixmap is found, the function sets \a pm to that pixmap and + returns TRUE; otherwise leaves \a pm alone and returns FALSE. + + Example: + \code + QPixmap p; + if ( !QPixmapCache::find("my_big_image", pm) ) { + pm.load("bigimage.png"); + QPixmapCache::insert("my_big_image", pm); + } + painter->drawPixmap(0, 0, p); + \endcode +*/ + +bool QPixmapCache::find( const QString &key, QPixmap& pm ) +{ + QPixmap* p = pm_cache ? pm_cache->find(key) : 0; + if ( p ) pm = *p; + return !!p; +} + + +/*! + \obsolete + Inserts the pixmap \a pm associated with \a key into the cache. + Returns TRUE if successful, or FALSE if the pixmap is too big for the cache. + + <strong> + Note: \a pm must be allocated on the heap (using \c new). + + If this function returns FALSE, you must delete \a pm yourself. + + If this function returns TRUE, do not use \a pm afterwards or + keep references to it because any other insertions into the cache, + whether from anywhere in the application or within Qt itself, could cause + the pixmap to be discarded from the cache and the pointer to + become invalid. + + Due to these dangers, we strongly recommend that you use + insert(const QString&, const QPixmap&) instead. + </strong> +*/ + +bool QPixmapCache::insert( const QString &key, QPixmap *pm ) +{ + if ( !pm_cache ) { // create pixmap cache + pm_cache = new QPMCache; + Q_CHECK_PTR( pm_cache ); + qpm_cleanup_cache.set( &pm_cache ); + } + return pm_cache->insert( key, pm, pm->width()*pm->height()*pm->depth()/8 ); +} + +/*! + Inserts a copy of the pixmap \a pm associated with the \a key into + the cache. + + All pixmaps inserted by the Qt library have a key starting with + "$qt", so your own pixmap keys should never begin "$qt". + + When a pixmap is inserted and the cache is about to exceed its + limit, it removes pixmaps until there is enough room for the + pixmap to be inserted. + + The oldest pixmaps (least recently accessed in the cache) are + deleted when more space is needed. + + \sa setCacheLimit(). +*/ + +bool QPixmapCache::insert( const QString &key, const QPixmap& pm ) +{ + if ( !pm_cache ) { // create pixmap cache + pm_cache = new QPMCache; + Q_CHECK_PTR( pm_cache ); + qpm_cleanup_cache.set( &pm_cache ); + } + QPixmap *p = new QPixmap(pm); + bool rt = pm_cache->insert( key, p, p->width()*p->height()*p->depth()/8 ); + if ( !rt ) + delete p; + + return rt; +} + +/*! + Returns the cache limit (in kilobytes). + + The default setting is 1024 kilobytes. + + \sa setCacheLimit(). +*/ + +int QPixmapCache::cacheLimit() +{ + return cache_limit; +} + +/*! + Sets the cache limit to \a n kilobytes. + + The default setting is 1024 kilobytes. + + \sa cacheLimit() +*/ + +void QPixmapCache::setCacheLimit( int n ) +{ +#ifdef Q_WS_MAC9 + if(n > 256) + qWarning("QPixmapCache::setCacheLimit: Setting cache limits high is harmfull to mac9's health"); +#endif + cache_limit = n; + if ( pm_cache ) + pm_cache->setMaxCost( 1024*cache_limit ); +} + + +/*! + Removes the pixmap associated with \a key from the cache. +*/ +void QPixmapCache::remove( const QString &key ) +{ + if ( pm_cache ) + pm_cache->remove( key ); +} + + +/*! + Removes all pixmaps from the cache. +*/ + +void QPixmapCache::clear() +{ + if ( pm_cache ) + pm_cache->clear(); +} diff --git a/src/kernel/qpixmapcache.h b/src/kernel/qpixmapcache.h new file mode 100644 index 0000000..fbbd0bd --- /dev/null +++ b/src/kernel/qpixmapcache.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Definition of QPixmapCache class +** +** Created : 950501 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPIXMAPCACHE_H +#define QPIXMAPCACHE_H + +#ifndef QT_H +#include "qpixmap.h" +#endif // QT_H + + +class Q_EXPORT QPixmapCache // global pixmap cache +{ +public: + static int cacheLimit(); + static void setCacheLimit( int ); + static QPixmap *find( const QString &key ); + static bool find( const QString &key, QPixmap& ); + static bool insert( const QString &key, QPixmap * ); + static bool insert( const QString &key, const QPixmap& ); + static void remove( const QString &key ); + static void clear(); +}; + + +#endif // QPIXMAPCACHE_H diff --git a/src/kernel/qpngio.cpp b/src/kernel/qpngio.cpp new file mode 100644 index 0000000..95036e8 --- /dev/null +++ b/src/kernel/qpngio.cpp @@ -0,0 +1,1242 @@ +/**************************************************************************** +** +** Implementation of PNG QImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpngio.h" + +#ifndef QT_NO_IMAGEIO_PNG + +#include "qasyncimageio.h" +#include "qiodevice.h" + +#include <png.h> + + +#ifdef Q_OS_TEMP +#define CALLBACK_CALL_TYPE __cdecl +#else +#define CALLBACK_CALL_TYPE +#endif + + +/* + All PNG files load to the minimal QImage equivalent. + + All QImage formats output to reasonably efficient PNG equivalents. + Never to grayscale. +*/ + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) +{ + QImageIO* iio = (QImageIO*)png_get_io_ptr(png_ptr); + QIODevice* in = iio->ioDevice(); + + while (length) { + int nr = in->readBlock((char*)data, length); + if (nr <= 0) { + png_error(png_ptr, "Read Error"); + return; + } + length -= nr; + } +} + + +static +void CALLBACK_CALL_TYPE qpiw_write_fn( png_structp png_ptr, png_bytep data, png_size_t length ) +{ + QPNGImageWriter* qpiw = (QPNGImageWriter*)png_get_io_ptr( png_ptr ); + QIODevice* out = qpiw->device(); + + uint nr = out->writeBlock( (char*)data, length ); + if ( nr != length ) { + png_error( png_ptr, "Write Error" ); + return; + } +} + + +static +void CALLBACK_CALL_TYPE qpiw_flush_fn( png_structp png_ptr ) +{ + QPNGImageWriter* qpiw = (QPNGImageWriter*)png_get_io_ptr( png_ptr ); + QIODevice* out = qpiw->device(); + + out->flush(); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +static +void setup_qt( QImage& image, png_structp png_ptr, png_infop info_ptr, float screen_gamma=0.0 ) +{ + if ( screen_gamma != 0.0 && png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) ) { + double file_gamma; + png_get_gAMA(png_ptr, info_ptr, &file_gamma); + png_set_gamma( png_ptr, screen_gamma, file_gamma ); + } + + png_uint_32 width; + png_uint_32 height; + int bit_depth; + int color_type; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, + 0, 0, 0); + + if ( color_type == PNG_COLOR_TYPE_GRAY ) { + // Black & White or 8-bit grayscale + if ( bit_depth == 1 && info_ptr->channels == 1 ) { + png_set_invert_mono( png_ptr ); + png_read_update_info( png_ptr, info_ptr ); + if (!image.create( width, height, 1, 2, QImage::BigEndian )) + return; + image.setColor( 1, qRgb(0,0,0) ); + image.setColor( 0, qRgb(255,255,255) ); + } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { + png_set_expand(png_ptr); + png_set_strip_16(png_ptr); + png_set_gray_to_rgb(png_ptr); + + if (!image.create(width, height, 32)) + return; + image.setAlphaBuffer(TRUE); + + if (QImage::systemByteOrder() == QImage::BigEndian) + png_set_swap_alpha(png_ptr); + + png_read_update_info(png_ptr, info_ptr); + } else { + if ( bit_depth == 16 ) + png_set_strip_16(png_ptr); + else if ( bit_depth < 8 ) + png_set_packing(png_ptr); + int ncols = bit_depth < 8 ? 1 << bit_depth : 256; + png_read_update_info(png_ptr, info_ptr); + if (!image.create(width, height, 8, ncols)) + return; + for (int i=0; i<ncols; i++) { + int c = i*255/(ncols-1); + image.setColor( i, qRgba(c,c,c,0xff) ); + } + if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) { + const int g = info_ptr->trans_values.gray; + if (g < ncols) { + image.setAlphaBuffer(TRUE); + image.setColor(g, image.color(g) & RGB_MASK); + } + } + } + } else if ( color_type == PNG_COLOR_TYPE_PALETTE + && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE) + && info_ptr->num_palette <= 256 ) + { + // 1-bit and 8-bit color + if ( bit_depth != 1 ) + png_set_packing( png_ptr ); + png_read_update_info( png_ptr, info_ptr ); + png_get_IHDR(png_ptr, info_ptr, + &width, &height, &bit_depth, &color_type, 0, 0, 0); + if (!image.create(width, height, bit_depth, info_ptr->num_palette, + QImage::BigEndian)) + return; + int i = 0; + if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) { + image.setAlphaBuffer( TRUE ); + while ( i < info_ptr->num_trans ) { + image.setColor(i, qRgba( + info_ptr->palette[i].red, + info_ptr->palette[i].green, + info_ptr->palette[i].blue, + info_ptr->trans[i] + ) + ); + i++; + } + } + while ( i < info_ptr->num_palette ) { + image.setColor(i, qRgba( + info_ptr->palette[i].red, + info_ptr->palette[i].green, + info_ptr->palette[i].blue, + 0xff + ) + ); + i++; + } + } else { + // 32-bit + if ( bit_depth == 16 ) + png_set_strip_16(png_ptr); + + png_set_expand(png_ptr); + + if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) + png_set_gray_to_rgb(png_ptr); + + if (!image.create(width, height, 32)) + return; + + // Only add filler if no alpha, or we can get 5 channel data. + if (!(color_type & PNG_COLOR_MASK_ALPHA) + && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { + png_set_filler(png_ptr, 0xff, + QImage::systemByteOrder() == QImage::BigEndian ? + PNG_FILLER_BEFORE : PNG_FILLER_AFTER); + // We want 4 bytes, but it isn't an alpha channel + } else { + image.setAlphaBuffer(TRUE); + } + + if ( QImage::systemByteOrder() == QImage::BigEndian ) { + png_set_swap_alpha(png_ptr); + } + + png_read_update_info(png_ptr, info_ptr); + } + + // Qt==ARGB==Big(ARGB)==Little(BGRA) + if ( QImage::systemByteOrder() == QImage::LittleEndian ) { + png_set_bgr(png_ptr); + } +} + + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif +static void CALLBACK_CALL_TYPE qt_png_warning(png_structp /*png_ptr*/, png_const_charp message) +{ + qWarning("libpng warning: %s", message); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +static +void read_png_image(QImageIO* iio) +{ + png_structp png_ptr; + png_infop info_ptr; + png_infop end_info; + png_bytep* row_pointers; + + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0); + if (!png_ptr) { + iio->setStatus(-1); + return; + } + + png_set_error_fn(png_ptr, 0, 0, qt_png_warning); + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_read_struct(&png_ptr, 0, 0); + iio->setStatus(-2); + return; + } + + end_info = png_create_info_struct(png_ptr); + if (!end_info) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + iio->setStatus(-3); + return; + } + + if (setjmp(png_ptr->jmpbuf)) { + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + iio->setStatus(-4); + return; + } + + png_set_read_fn(png_ptr, (void*)iio, iod_read_fn); + png_read_info(png_ptr, info_ptr); + + QImage image; + setup_qt(image, png_ptr, info_ptr, iio->gamma()); + if (image.isNull()) { + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + iio->setStatus(-5); + return; + } + + png_uint_32 width; + png_uint_32 height; + int bit_depth; + int color_type; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, + 0, 0, 0); + + uchar** jt = image.jumpTable(); + row_pointers=new png_bytep[height]; + + for (uint y=0; y<height; y++) { + row_pointers[y]=jt[y]; + } + + png_read_image(png_ptr, row_pointers); + +#if 0 // libpng takes care of this. +png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) + if (image.depth()==32 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { + QRgb trans = 0xFF000000 | qRgb( + (info_ptr->trans_values.red << 8 >> bit_depth)&0xff, + (info_ptr->trans_values.green << 8 >> bit_depth)&0xff, + (info_ptr->trans_values.blue << 8 >> bit_depth)&0xff); + for (uint y=0; y<height; y++) { + for (uint x=0; x<info_ptr->width; x++) { + if (((uint**)jt)[y][x] == trans) { + ((uint**)jt)[y][x] &= 0x00FFFFFF; + } else { + } + } + } + } +#endif + + image.setDotsPerMeterX(png_get_x_pixels_per_meter(png_ptr,info_ptr)); + image.setDotsPerMeterY(png_get_y_pixels_per_meter(png_ptr,info_ptr)); + +#ifndef QT_NO_IMAGE_TEXT + png_textp text_ptr; + int num_text=0; + png_get_text(png_ptr,info_ptr,&text_ptr,&num_text); + while (num_text--) { + image.setText(text_ptr->key,0,text_ptr->text); + text_ptr++; + } +#endif + + delete [] row_pointers; + + if ( image.hasAlphaBuffer() ) { + // Many PNG files lie (eg. from PhotoShop). Fortunately this loop will + // usually be quick to find those that tell the truth. + QRgb* c; + int n; + if (image.depth()==32) { + c = (QRgb*)image.bits(); + n = image.bytesPerLine() * image.height() / 4; + } else { + c = image.colorTable(); + n = image.numColors(); + } + while ( n-- && qAlpha(*c++)==0xff ) + ; + if ( n<0 ) // LIAR! + image.setAlphaBuffer(FALSE); + } + + iio->setImage(image); + + png_read_end(png_ptr, end_info); + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + + iio->setStatus(0); +} + +QPNGImageWriter::QPNGImageWriter(QIODevice* iod) : + dev(iod), + frames_written(0), + disposal(Unspecified), + looping(-1), + ms_delay(-1), + gamma(0.0) +{ +} + +QPNGImageWriter::~QPNGImageWriter() +{ +} + +void QPNGImageWriter::setDisposalMethod(DisposalMethod dm) +{ + disposal = dm; +} + +void QPNGImageWriter::setLooping(int loops) +{ + looping = loops; +} + +void QPNGImageWriter::setFrameDelay(int msecs) +{ + ms_delay = msecs; +} + +void QPNGImageWriter::setGamma(float g) +{ + gamma = g; +} + + +#ifndef QT_NO_IMAGE_TEXT +static void set_text(const QImage& image, png_structp png_ptr, png_infop info_ptr, bool short_not_long) +{ + QValueList<QImageTextKeyLang> keys = image.textList(); + if ( keys.count() ) { + png_textp text_ptr = new png_text[keys.count()]; + int i=0; + for (QValueList<QImageTextKeyLang>::Iterator it=keys.begin(); + it != keys.end(); ++it) + { + QString t = image.text(*it); + if ( (t.length() <= 200) == short_not_long ) { + if ( t.length() < 40 ) + text_ptr[i].compression = PNG_TEXT_COMPRESSION_NONE; + else + text_ptr[i].compression = PNG_TEXT_COMPRESSION_zTXt; + text_ptr[i].key = (png_charp)(*it).key.data(); + text_ptr[i].text = (png_charp)t.latin1(); + //text_ptr[i].text = qstrdup(t.latin1()); + i++; + } + } + png_set_text(png_ptr, info_ptr, text_ptr, i); + //for (int j=0; j<i; j++) + //free(text_ptr[i].text); + delete [] text_ptr; + } +} +#endif + +bool QPNGImageWriter::writeImage(const QImage& image, int off_x, int off_y) +{ + return writeImage(image, -1, off_x, off_y); +} + +bool QPNGImageWriter::writeImage(const QImage& image, int quality_in, int off_x_in, int off_y_in) +{ + QPoint offset = image.offset(); + int off_x = off_x_in + offset.x(); + int off_y = off_y_in + offset.y(); + + png_structp png_ptr; + png_infop info_ptr; + png_bytep* row_pointers; + + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0); + if (!png_ptr) { + return FALSE; + } + + png_set_error_fn(png_ptr, 0, 0, qt_png_warning); + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_write_struct(&png_ptr, 0); + return FALSE; + } + + if (setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr, &info_ptr); + return FALSE; + } + + int quality = quality_in; + if (quality >= 0) { + if (quality > 9) { +#if defined(QT_CHECK_RANGE) + qWarning( "PNG: Quality %d out of range", quality ); +#endif + quality = 9; + } + png_set_compression_level(png_ptr, quality); + } + + if (gamma != 0.0) { + png_set_gAMA(png_ptr, info_ptr, 1.0/gamma); + } + + png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn); + + info_ptr->channels = + (image.depth() == 32) + ? (image.hasAlphaBuffer() ? 4 : 3) + : 1; + + png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(), + image.depth() == 1 ? 1 : 8 /* per channel */, + image.depth() == 32 + ? image.hasAlphaBuffer() + ? PNG_COLOR_TYPE_RGB_ALPHA + : PNG_COLOR_TYPE_RGB + : PNG_COLOR_TYPE_PALETTE, 0, 0, 0); + + + //png_set_sBIT(png_ptr, info_ptr, 8); + info_ptr->sig_bit.red = 8; + info_ptr->sig_bit.green = 8; + info_ptr->sig_bit.blue = 8; + + if (image.depth() == 1 && image.bitOrder() == QImage::LittleEndian) + png_set_packswap(png_ptr); + + png_colorp palette = 0; + png_bytep copy_trans = 0; + if (image.numColors()) { + // Paletted + int num_palette = image.numColors(); + palette = new png_color[num_palette]; + png_set_PLTE(png_ptr, info_ptr, palette, num_palette); + int* trans = new int[num_palette]; + int num_trans = 0; + for (int i=0; i<num_palette; i++) { + QRgb rgb=image.color(i); + info_ptr->palette[i].red = qRed(rgb); + info_ptr->palette[i].green = qGreen(rgb); + info_ptr->palette[i].blue = qBlue(rgb); + if (image.hasAlphaBuffer()) { + trans[i] = rgb >> 24; + if (trans[i] < 255) { + num_trans = i+1; + } + } + } + if (num_trans) { + copy_trans = new png_byte[num_trans]; + for (int i=0; i<num_trans; i++) + copy_trans[i] = trans[i]; + png_set_tRNS(png_ptr, info_ptr, copy_trans, num_trans, 0); + } + delete [] trans; + } + + if ( image.hasAlphaBuffer() ) { + info_ptr->sig_bit.alpha = 8; + } + + // Swap ARGB to RGBA (normal PNG format) before saving on + // BigEndian machines + if ( QImage::systemByteOrder() == QImage::BigEndian ) { + png_set_swap_alpha(png_ptr); + } + + // Qt==ARGB==Big(ARGB)==Little(BGRA) + if ( QImage::systemByteOrder() == QImage::LittleEndian ) { + png_set_bgr(png_ptr); + } + + if (off_x || off_y) { + png_set_oFFs(png_ptr, info_ptr, off_x, off_y, PNG_OFFSET_PIXEL); + } + + if ( frames_written > 0 ) + png_set_sig_bytes(png_ptr, 8); + + if ( image.dotsPerMeterX() > 0 || image.dotsPerMeterY() > 0 ) { + png_set_pHYs(png_ptr, info_ptr, + image.dotsPerMeterX(), image.dotsPerMeterY(), + PNG_RESOLUTION_METER); + } + +#ifndef QT_NO_IMAGE_TEXT + // Write short texts early. + set_text(image,png_ptr,info_ptr,TRUE); +#endif + + png_write_info(png_ptr, info_ptr); + +#ifndef QT_NO_IMAGE_TEXT + // Write long texts later. + set_text(image,png_ptr,info_ptr,FALSE); +#endif + + if ( image.depth() != 1 ) + png_set_packing(png_ptr); + + if ( image.depth() == 32 && !image.hasAlphaBuffer() ) + png_set_filler(png_ptr, 0, + QImage::systemByteOrder() == QImage::BigEndian ? + PNG_FILLER_BEFORE : PNG_FILLER_AFTER); + + if ( looping >= 0 && frames_written == 0 ) { + uchar data[13] = "NETSCAPE2.0"; + // 0123456789aBC + data[0xB] = looping%0x100; + data[0xC] = looping/0x100; + png_write_chunk(png_ptr, (png_byte*)"gIFx", data, 13); + } + if ( ms_delay >= 0 || disposal!=Unspecified ) { + uchar data[4]; + data[0] = disposal; + data[1] = 0; + data[2] = (ms_delay/10)/0x100; // hundredths + data[3] = (ms_delay/10)%0x100; + png_write_chunk(png_ptr, (png_byte*)"gIFg", data, 4); + } + + png_uint_32 width; + png_uint_32 height; + int bit_depth; + int color_type; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, + 0, 0, 0); + + uchar** jt = image.jumpTable(); + row_pointers=new png_bytep[height]; + uint y; + for (y=0; y<height; y++) { + row_pointers[y]=jt[y]; + } + png_write_image(png_ptr, row_pointers); + delete [] row_pointers; + + png_write_end(png_ptr, info_ptr); + frames_written++; + + if ( palette ) + delete [] palette; + if ( copy_trans ) + delete [] copy_trans; + + png_destroy_write_struct(&png_ptr, &info_ptr); + + return TRUE; +} + +static +void write_png_image(QImageIO* iio) +{ + QPNGImageWriter writer(iio->ioDevice()); + int quality = iio->quality(); + if ( quality >= 0 ) { + quality = QMIN( quality, 100 ); + quality = (100-quality) * 9 / 91; // map [0,100] -> [9,0] + } + writer.setGamma(iio->gamma()); + bool ok = writer.writeImage( iio->image(), quality ); + iio->setStatus( ok ? 0 : -1 ); +} + +/*! + \class QPNGImagePacker qpngio.h + \brief The QPNGImagePacker class creates well-compressed PNG animations. + + \ingroup images + \ingroup graphics + + By using transparency, QPNGImagePacker allows you to build a PNG + image from a sequence of QImages. + + Images are added using packImage(). +*/ + + +/*! + Creates an image packer that writes PNG data to IO device \a iod + using a \a storage_depth bit encoding (use 8 or 32, depending on + the desired quality and compression requirements). + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversionflags to specify how you'd prefer this to happen. + + \sa Qt::ImageConversionFlags +*/ +QPNGImagePacker::QPNGImagePacker(QIODevice* iod, int storage_depth, + int conversionflags) : + QPNGImageWriter(iod), + depth(storage_depth), + convflags(conversionflags), + alignx(1) +{ +} + +/*! + Aligns pixel differences to \a x pixels. For example, using 8 can + improve playback on certain hardware. Normally the default of + 1-pixel alignment (i.e. no alignment) gives better compression and + performance. +*/ +void QPNGImagePacker::setPixelAlignment(int x) +{ + alignx = x; +} + +/*! + Adds the image \a img to the PNG animation, analyzing the + differences between this and the previous image to improve + compression. +*/ +bool QPNGImagePacker::packImage(const QImage& img) +{ + QImage image = img.convertDepth(32); + if ( previous.isNull() ) { + // First image + writeImage(image.convertDepth(depth,convflags)); + } else { + bool done; + int minx, maxx, miny, maxy; + int w = image.width(); + int h = image.height(); + + QRgb** jt = (QRgb**)image.jumpTable(); + QRgb** pjt = (QRgb**)previous.jumpTable(); + + // Find left edge of change + done = FALSE; + for (minx = 0; minx < w && !done; minx++) { + for (int ty = 0; ty < h; ty++) { + if ( jt[ty][minx] != pjt[ty][minx] ) { + done = TRUE; + break; + } + } + } + minx--; + + // Find right edge of change + done = FALSE; + for (maxx = w-1; maxx >= 0 && !done; maxx--) { + for (int ty = 0; ty < h; ty++) { + if ( jt[ty][maxx] != pjt[ty][maxx] ) { + done = TRUE; + break; + } + } + } + maxx++; + + // Find top edge of change + done = FALSE; + for (miny = 0; miny < h && !done; miny++) { + for (int tx = 0; tx < w; tx++) { + if ( jt[miny][tx] != pjt[miny][tx] ) { + done = TRUE; + break; + } + } + } + miny--; + + // Find right edge of change + done = FALSE; + for (maxy = h-1; maxy >= 0 && !done; maxy--) { + for (int tx = 0; tx < w; tx++) { + if ( jt[maxy][tx] != pjt[maxy][tx] ) { + done = TRUE; + break; + } + } + } + maxy++; + + if ( minx > maxx ) minx=maxx=0; + if ( miny > maxy ) miny=maxy=0; + + if ( alignx > 1 ) { + minx -= minx % alignx; + maxx = maxx - maxx % alignx + alignx - 1; + } + + int dw = maxx-minx+1; + int dh = maxy-miny+1; + + QImage diff(dw, dh, 32); + + diff.setAlphaBuffer(TRUE); + int x, y; + if ( alignx < 1 ) + alignx = 1; + for (y = 0; y < dh; y++) { + QRgb* li = (QRgb*)image.scanLine(y+miny)+minx; + QRgb* lp = (QRgb*)previous.scanLine(y+miny)+minx; + QRgb* ld = (QRgb*)diff.scanLine(y); + if ( alignx ) { + for (x = 0; x < dw; x+=alignx) { + int i; + for (i=0; i<alignx; i++) { + if ( li[x+i] != lp[x+i] ) + break; + } + if ( i == alignx ) { + // All the same + for (i=0; i<alignx; i++) { + ld[x+i] = qRgba(0,0,0,0); + } + } else { + // Some different + for (i=0; i<alignx; i++) { + ld[x+i] = 0xff000000 | li[x+i]; + } + } + } + } else { + for (x = 0; x < dw; x++) { + if ( li[x] != lp[x] ) + ld[x] = 0xff000000 | li[x]; + else + ld[x] = qRgba(0,0,0,0); + } + } + } + + diff = diff.convertDepth(depth,convflags); + if ( !writeImage(diff, minx, miny) ) + return FALSE; + } + previous = image; + return TRUE; +} + + +#ifndef QT_NO_ASYNC_IMAGE_IO + +class QPNGFormat : public QImageFormat { +public: + QPNGFormat(); + virtual ~QPNGFormat(); + + int decode(QImage& img, QImageConsumer* consumer, + const uchar* buffer, int length); + + void info(png_structp png_ptr, png_infop info); + void row(png_structp png_ptr, png_bytep new_row, + png_uint_32 row_num, int pass); + void end(png_structp png_ptr, png_infop info); +#ifdef PNG_USER_CHUNKS_SUPPORTED + int user_chunk(png_structp png_ptr, + png_bytep data, png_uint_32 length); +#endif + +private: + // Animation-level information + enum { MovieStart, FrameStart, Inside, End } state; + int first_frame; + int base_offx; + int base_offy; + + // Image-level information + png_structp png_ptr; + png_infop info_ptr; + + // Temporary locals during single data-chunk processing + QImageConsumer* consumer; + QImage* image; + int unused_data; +}; + +class QPNGFormatType : public QImageFormatType +{ + QImageFormat* decoderFor(const uchar* buffer, int length); + const char* formatName() const; +}; + + +/* + \class QPNGFormat qpngio.h + \brief The QPNGFormat class provides an incremental image decoder for PNG + image format. + + \ingroup images + \ingroup graphics + + This subclass of QImageFormat decodes PNG format images, + including animated PNGs. + + Animated PNG images are standard PNG images. The PNG standard + defines two extension chunks that are useful for animations: + + \list + \i gIFg - GIF-like Graphic Control Extension. + This includes frame disposal, user input flag (we ignore this), + and inter-frame delay. + \i gIFx - GIF-like Application Extension. + This is multi-purpose, but we just use the Netscape extension + which specifies looping. + \endlist + + The subimages usually contain a offset chunk (oFFs) but need not. + + The first image defines the "screen" size. Any subsequent image that + doesn't fit is clipped. +*/ +/* ###TODO: decide on this point. gIFg gives disposal types, so it can be done. + All images paste (\e not composite, just place all-channel copying) + over the previous image to produce a subsequent frame. +*/ + +/* + \class QPNGFormatType qasyncimageio.h + \brief The QPNGFormatType class provides an incremental image decoder + for PNG image format. + + \ingroup images + \ingroup graphics + \ingroup io + + This subclass of QImageFormatType recognizes PNG format images, creating + a QPNGFormat when required. An instance of this class is created + automatically before any other factories, so you should have no need for + such objects. +*/ + +QImageFormat* QPNGFormatType::decoderFor( + const uchar* buffer, int length) +{ + if (length < 8) return 0; + if (buffer[0]==137 + && buffer[1]=='P' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10) + return new QPNGFormat; + return 0; +} + +const char* QPNGFormatType::formatName() const +{ + return "PNG"; +} + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static void +CALLBACK_CALL_TYPE info_callback(png_structp png_ptr, png_infop info) +{ + QPNGFormat* that = (QPNGFormat*)png_get_progressive_ptr(png_ptr); + that->info(png_ptr,info); +} + +static void +CALLBACK_CALL_TYPE row_callback(png_structp png_ptr, png_bytep new_row, + png_uint_32 row_num, int pass) +{ + QPNGFormat* that = (QPNGFormat*)png_get_progressive_ptr(png_ptr); + that->row(png_ptr,new_row,row_num,pass); +} + +static void +CALLBACK_CALL_TYPE end_callback(png_structp png_ptr, png_infop info) +{ + QPNGFormat* that = (QPNGFormat*)png_get_progressive_ptr(png_ptr); + that->end(png_ptr,info); +} + +#if 0 +#ifdef PNG_USER_CHUNKS_SUPPORTED +static int +CALLBACK_CALL_TYPE user_chunk_callback(png_structp png_ptr, + png_unknown_chunkp chunk) +{ + QPNGFormat* that = (QPNGFormat*)png_get_progressive_ptr(png_ptr); + return that->user_chunk(png_ptr,chunk->data,chunk->size); +} +#endif +#endif + +#if defined(Q_C_CALLBACKS) +} +#endif + + +/*! + Constructs a QPNGFormat object. +*/ +QPNGFormat::QPNGFormat() +{ + state = MovieStart; + first_frame = 1; + base_offx = 0; + base_offy = 0; + png_ptr = 0; + info_ptr = 0; +} + + +/*! + Destroys a QPNGFormat object. +*/ +QPNGFormat::~QPNGFormat() +{ + if ( png_ptr ) + png_destroy_read_struct(&png_ptr, &info_ptr, 0); +} + + +/*! + This function decodes some data into image changes. + + Returns the number of bytes consumed. +*/ +int QPNGFormat::decode(QImage& img, QImageConsumer* cons, + const uchar* buffer, int length) +{ + consumer = cons; + image = &img; + + if ( state != Inside ) { + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); + if (!png_ptr) { + info_ptr = 0; + image = 0; + return -1; + } + + png_set_error_fn(png_ptr, 0, 0, qt_png_warning); + png_set_compression_level(png_ptr, 9); + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + image = 0; + return -1; + } + + if (setjmp((png_ptr)->jmpbuf)) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + image = 0; + return -1; + } + + png_set_progressive_read_fn(png_ptr, (void *)this, + info_callback, row_callback, end_callback); + +#ifdef PNG_USER_CHUNKS_SUPPORTED + // Can't do this yet. libpng has a crash bug with unknown (user) chunks. + // Warwick has sent them a patch. + // png_set_read_user_chunk_fn(png_ptr, 0, user_chunk_callback); + // png_set_keep_unknown_chunks(png_ptr, 2/*HANDLE_CHUNK_IF_SAFE*/, 0, 0); +#endif + + if ( state != MovieStart && *buffer != 0211 ) { + // Good, no signature - the preferred way to concat PNG images. + // Skip them. + png_set_sig_bytes(png_ptr, 8); + } + + state = Inside; + } + + if ( !png_ptr ) return 0; + + if (setjmp(png_ptr->jmpbuf)) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + image = 0; + state = MovieStart; + return -1; + } + unused_data = 0; + png_process_data(png_ptr, info_ptr, (png_bytep)buffer, length); + int l = length - unused_data; + + // TODO: send incremental stuff to consumer (optional) + + if ( state != Inside ) { + if ( png_ptr ) + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + } + + image = 0; + return l; +} + +void QPNGFormat::info(png_structp png, png_infop) +{ + png_set_interlace_handling(png); + setup_qt(*image, png, info_ptr); +} + +void QPNGFormat::row(png_structp png, png_bytep new_row, + png_uint_32 row_num, int) +{ + uchar* old_row = image->scanLine(row_num); + png_progressive_combine_row(png, old_row, new_row); +} + + +void QPNGFormat::end(png_structp png, png_infop info) +{ + int offx = png_get_x_offset_pixels(png,info) - base_offx; + int offy = png_get_y_offset_pixels(png,info) - base_offy; + if ( first_frame ) { + base_offx = offx; + base_offy = offy; + first_frame = 0; + } + image->setOffset(QPoint(offx,offy)); + image->setDotsPerMeterX(png_get_x_pixels_per_meter(png,info)); + image->setDotsPerMeterY(png_get_y_pixels_per_meter(png,info)); +#ifndef QT_NO_IMAGE_TEXT + png_textp text_ptr; + int num_text=0; + png_get_text(png,info,&text_ptr,&num_text); + while (num_text--) { + image->setText(text_ptr->key,0,text_ptr->text); + text_ptr++; + } +#endif + QRect r(0,0,image->width(),image->height()); + consumer->frameDone(QPoint(offx,offy),r); + consumer->end(); + state = FrameStart; + unused_data = (int)png->buffer_size; // Since libpng doesn't tell us +} + +#ifdef PNG_USER_CHUNKS_SUPPORTED + +/* +#ifndef QT_NO_IMAGE_TEXT +static bool skip(png_uint_32& max, png_bytep& data) +{ + while (*data) { + if ( !max ) return FALSE; + max--; + data++; + } + if ( !max ) return FALSE; + max--; + data++; // skip to after NUL + return TRUE; +} +#endif +*/ + +int QPNGFormat::user_chunk(png_structp png, + png_bytep data, png_uint_32 length) +{ +#if 0 // NOT SUPPORTED: experimental PNG animation. + // qDebug("Got %ld-byte %s chunk", length, png->chunk_name); + if ( 0==qstrcmp((char*)png->chunk_name, "gIFg") + && length == 4 ) { + + //QPNGImageWriter::DisposalMethod disposal = + // (QPNGImageWriter::DisposalMethod)data[0]; + // ### TODO: use the disposal method + int ms_delay = ((data[2] << 8) | data[3])*10; + consumer->setFramePeriod(ms_delay); + return 1; + } else if ( 0==qstrcmp((char*)png->chunk_name, "gIFx") + && length == 13 ) { + if ( qstrncmp((char*)data,"NETSCAPE2.0",11)==0 ) { + int looping = (data[0xC]<<8)|data[0xB]; + consumer->setLooping(looping); + return 1; + } + } +#else + Q_UNUSED( png ) + Q_UNUSED( data ) + Q_UNUSED( length ) +#endif + +#ifndef QT_NO_IMAGE_TEXT + /* + + libpng now supports this chunk. + + + if ( 0==qstrcmp((char*)png->chunk_name, "iTXt") && length>=6 ) { + const char* keyword = (const char*)data; + if ( !skip(length,data) ) return 0; + if ( length >= 4 ) { + char compression_flag = *data++; + char compression_method = *data++; + if ( compression_flag == compression_method ) { + // fool the compiler into thinking they're used + } + const char* lang = (const char*)data; + if ( !skip(length,data) ) return 0; + // const char* keyword_utf8 = (const char*)data; + if ( !skip(length,data) ) return 0; + const char* text_utf8 = (const char*)data; + if ( !skip(length,data) ) return 0; + QString text = QString::fromUtf8(text_utf8); + image->setText(keyword,lang[0] ? lang : 0,text); + return 1; + } + } + */ +#endif + + return 0; +} +#endif + + +static QPNGFormatType* globalPngFormatTypeObject = 0; + +#endif // QT_NO_ASYNC_IMAGE_IO + +static bool done = FALSE; +void qCleanupPngIO() +{ +#ifndef QT_NO_ASYNC_IMAGE_IO + if ( globalPngFormatTypeObject ) { + delete globalPngFormatTypeObject; + globalPngFormatTypeObject = 0; + } +#endif + done = FALSE; +} + +void qInitPngIO() +{ + if ( !done ) { + done = TRUE; + QImageIO::defineIOHandler( "PNG", "^.PNG\r", 0, read_png_image, + write_png_image); +#ifndef QT_NO_ASYNC_IMAGE_IO + globalPngFormatTypeObject = new QPNGFormatType; +#endif + qAddPostRoutine( qCleanupPngIO ); + } +} + +void qt_zlib_compression_hack() +{ + compress(0,0,0,0); + uncompress(0,0,0,0); +} + +#endif // QT_NO_IMAGEIO_PNG diff --git a/src/kernel/qpngio.h b/src/kernel/qpngio.h new file mode 100644 index 0000000..ccfd224 --- /dev/null +++ b/src/kernel/qpngio.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Definition of PNG QImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPNGIO_H +#define QPNGIO_H + +#ifndef QT_H +#include "qimage.h" +#endif // QT_H + +#ifndef QT_NO_IMAGEIO_PNG + +void qInitPngIO(); + +class QIODevice; + +#ifndef Q_PNGEXPORT +#if !defined(QT_PLUGIN) +#define Q_PNGEXPORT Q_EXPORT +#else +#define Q_PNGEXPORT +#endif +#endif + +class Q_PNGEXPORT QPNGImageWriter { +public: + QPNGImageWriter(QIODevice*); + ~QPNGImageWriter(); + + enum DisposalMethod { Unspecified, NoDisposal, RestoreBackground, RestoreImage }; + void setDisposalMethod(DisposalMethod); + void setLooping(int loops=0); // 0 == infinity + void setFrameDelay(int msecs); + void setGamma(float); + + bool writeImage(const QImage& img, int x, int y); + bool writeImage(const QImage& img, int quality, int x, int y); + bool writeImage(const QImage& img) + { return writeImage(img, 0, 0); } + bool writeImage(const QImage& img, int quality) + { return writeImage(img, quality, 0, 0); } + + QIODevice* device() { return dev; } + +private: + QIODevice* dev; + int frames_written; + DisposalMethod disposal; + int looping; + int ms_delay; + float gamma; +}; + +class Q_PNGEXPORT QPNGImagePacker : public QPNGImageWriter { +public: + QPNGImagePacker(QIODevice*, int depth, int convflags); + + void setPixelAlignment(int x); + bool packImage(const QImage& img); + +private: + QImage previous; + int depth; + int convflags; + int alignx; +}; + +#endif // QT_NO_IMAGEIO_PNG + +#endif // QPNGIO_H diff --git a/src/kernel/qpoint.cpp b/src/kernel/qpoint.cpp new file mode 100644 index 0000000..5313307 --- /dev/null +++ b/src/kernel/qpoint.cpp @@ -0,0 +1,443 @@ +/**************************************************************************** +** +** Implementation of QPoint class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpoint.h" +#include "qdatastream.h" + + +/*! + \class QPoint qpoint.h + \brief The QPoint class defines a point in the plane. + + \ingroup images + \ingroup graphics + \mainclass + + A point is specified by an x coordinate and a y coordinate. + + The coordinate type is \c QCOORD (a 32-bit integer). The minimum + value of \c QCOORD is \c QCOORD_MIN (-2147483648) and the maximum + value is \c QCOORD_MAX (2147483647). + + The coordinates are accessed by the functions x() and y(); they + can be set by setX() and setY() or by the reference functions rx() + and ry(). + + Given a point \e p, the following statements are all equivalent: + \code + p.setX( p.x() + 1 ); + p += QPoint( 1, 0 ); + p.rx()++; + \endcode + + A QPoint can also be used as a vector. Addition and subtraction + of QPoints are defined as for vectors (each component is added + separately). You can divide or multiply a QPoint by an \c int or a + \c double. The function manhattanLength() gives an inexpensive + approximation of the length of the QPoint interpreted as a vector. + + Example: + \code + //QPoint oldPos is defined somewhere else + MyWidget::mouseMoveEvent( QMouseEvent *e ) + { + QPoint vector = e->pos() - oldPos; + if ( vector.manhattanLength() > 3 ) + ... //mouse has moved more than 3 pixels since oldPos + } + \endcode + + QPoints can be compared for equality or inequality, and they can + be written to and read from a QStream. + + \sa QPointArray QSize, QRect +*/ + + +/***************************************************************************** + QPoint member functions + *****************************************************************************/ + +/*! + \fn QPoint::QPoint() + + Constructs a point with coordinates (0, 0) (isNull() returns TRUE). +*/ + +/*! + \fn QPoint::QPoint( int xpos, int ypos ) + + Constructs a point with x value \a xpos and y value \a ypos. +*/ + +/*! + \fn bool QPoint::isNull() const + + Returns TRUE if both the x value and the y value are 0; otherwise + returns FALSE. +*/ + +/*! + \fn int QPoint::x() const + + Returns the x coordinate of the point. + + \sa setX() y() +*/ + +/*! + \fn int QPoint::y() const + + Returns the y coordinate of the point. + + \sa setY() x() +*/ + +/*! + \fn void QPoint::setX( int x ) + + Sets the x coordinate of the point to \a x. + + \sa x() setY() +*/ + +/*! + \fn void QPoint::setY( int y ) + + Sets the y coordinate of the point to \a y. + + \sa y() setX() +*/ + + +/*! + \fn QCOORD &QPoint::rx() + + Returns a reference to the x coordinate of the point. + + Using a reference makes it possible to directly manipulate x. + + Example: + \code + QPoint p( 1, 2 ); + p.rx()--; // p becomes (0, 2) + \endcode + + \sa ry() +*/ + +/*! + \fn QCOORD &QPoint::ry() + + Returns a reference to the y coordinate of the point. + + Using a reference makes it possible to directly manipulate y. + + Example: + \code + QPoint p( 1, 2 ); + p.ry()++; // p becomes (1, 3) + \endcode + + \sa rx() +*/ + + +/*! + \fn QPoint &QPoint::operator+=( const QPoint &p ) + + Adds point \a p to this point and returns a reference to this + point. + + Example: + \code + QPoint p( 3, 7 ); + QPoint q( -1, 4 ); + p += q; // p becomes (2,11) + \endcode +*/ + +/*! + \fn QPoint &QPoint::operator-=( const QPoint &p ) + + Subtracts point \a p from this point and returns a reference to + this point. + + Example: + \code + QPoint p( 3, 7 ); + QPoint q( -1, 4 ); + p -= q; // p becomes (4,3) + \endcode +*/ + +/*! + \fn QPoint &QPoint::operator*=( int c ) + + Multiplies this point's x and y by \a c, and returns a reference + to this point. + + Example: + \code + QPoint p( -1, 4 ); + p *= 2; // p becomes (-2,8) + \endcode +*/ + +/*! + \overload QPoint &QPoint::operator*=( double c ) + + Multiplies this point's x and y by \a c, and returns a reference + to this point. + + Example: + \code + QPoint p( -1, 4 ); + p *= 2.5; // p becomes (-3,10) + \endcode + + Note that the result is truncated because points are held as + integers. +*/ + + +/*! + \fn bool operator==( const QPoint &p1, const QPoint &p2 ) + + \relates QPoint + + Returns TRUE if \a p1 and \a p2 are equal; otherwise returns FALSE. +*/ + +/*! + \fn bool operator!=( const QPoint &p1, const QPoint &p2 ) + + \relates QPoint + + Returns TRUE if \a p1 and \a p2 are not equal; otherwise returns FALSE. +*/ + +/*! + \fn const QPoint operator+( const QPoint &p1, const QPoint &p2 ) + + \relates QPoint + + Returns the sum of \a p1 and \a p2; each component is added separately. +*/ + +/*! + \fn const QPoint operator-( const QPoint &p1, const QPoint &p2 ) + + \relates QPoint + + Returns \a p2 subtracted from \a p1; each component is subtracted + separately. +*/ + +/*! + \fn const QPoint operator*( const QPoint &p, int c ) + + \relates QPoint + + Returns the QPoint formed by multiplying both components of \a p + by \a c. +*/ + +/*! + \overload const QPoint operator*( int c, const QPoint &p ) + + \relates QPoint + + Returns the QPoint formed by multiplying both components of \a p + by \a c. +*/ + +/*! + \overload const QPoint operator*( const QPoint &p, double c ) + + \relates QPoint + + Returns the QPoint formed by multiplying both components of \a p + by \a c. + + Note that the result is truncated because points are held as + integers. +*/ + +/*! + \overload const QPoint operator*( double c, const QPoint &p ) + + \relates QPoint + + Returns the QPoint formed by multiplying both components of \a p + by \a c. + + Note that the result is truncated because points are held as + integers. +*/ + +/*! + \overload const QPoint operator-( const QPoint &p ) + + \relates QPoint + + Returns the QPoint formed by changing the sign of both components + of \a p, equivalent to \c{QPoint(0,0) - p}. +*/ + +/*! + \fn QPoint &QPoint::operator/=( int c ) + + Divides both x and y by \a c, and returns a reference to this + point. + + Example: + \code + QPoint p( -2, 8 ); + p /= 2; // p becomes (-1,4) + \endcode +*/ + +/*! + \overload QPoint &QPoint::operator/=( double c ) + + Divides both x and y by \a c, and returns a reference to this + point. + + Example: + \code + QPoint p( -3, 10 ); + p /= 2.5; // p becomes (-1,4) + \endcode + + Note that the result is truncated because points are held as + integers. +*/ + +/*! + \fn const QPoint operator/( const QPoint &p, int c ) + + \relates QPoint + + Returns the QPoint formed by dividing both components of \a p by + \a c. +*/ + +/*! + \overload const QPoint operator/( const QPoint &p, double c ) + + \relates QPoint + + Returns the QPoint formed by dividing both components of \a p + by \a c. + + Note that the result is truncated because points are held as + integers. +*/ + + +void QPoint::warningDivByZero() +{ +#if defined(QT_CHECK_MATH) + qWarning( "QPoint: Division by zero error" ); +#endif +} + + +/***************************************************************************** + QPoint stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QPoint + + Writes point \a p to the stream \a s and returns a reference to + the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QPoint &p ) +{ + if ( s.version() == 1 ) + s << (Q_INT16)p.x() << (Q_INT16)p.y(); + else + s << (Q_INT32)p.x() << (Q_INT32)p.y(); + return s; +} + +/*! + \relates QPoint + + Reads a QPoint from the stream \a s into point \a p and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QPoint &p ) +{ + if ( s.version() == 1 ) { + Q_INT16 x, y; + s >> x; p.rx() = x; + s >> y; p.ry() = y; + } + else { + Q_INT32 x, y; + s >> x; p.rx() = x; + s >> y; p.ry() = y; + } + return s; +} +#endif // QT_NO_DATASTREAM +/*! + Returns the sum of the absolute values of x() and y(), + traditionally known as the "Manhattan length" of the vector from + the origin to the point. The tradition arises because such + distances apply to travelers who can only travel on a rectangular + grid, like the streets of Manhattan. + + This is a useful, and quick to calculate, approximation to the + true length: sqrt(pow(x(),2)+pow(y(),2)). +*/ +int QPoint::manhattanLength() const +{ + return QABS(x())+QABS(y()); +} diff --git a/src/kernel/qpoint.h b/src/kernel/qpoint.h new file mode 100644 index 0000000..f2894cc --- /dev/null +++ b/src/kernel/qpoint.h @@ -0,0 +1,219 @@ +/**************************************************************************** +** +** Definition of QPoint class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPOINT_H +#define QPOINT_H + +#ifndef QT_H +#include "qwindowdefs.h" +#endif // QT_H + + +class Q_EXPORT QPoint +{ +public: + QPoint(); + QPoint( int xpos, int ypos ); + + bool isNull() const; + + int x() const; + int y() const; + void setX( int x ); + void setY( int y ); + + int manhattanLength() const; + + QCOORD &rx(); + QCOORD &ry(); + + QPoint &operator+=( const QPoint &p ); + QPoint &operator-=( const QPoint &p ); + QPoint &operator*=( int c ); + QPoint &operator*=( double c ); + QPoint &operator/=( int c ); + QPoint &operator/=( double c ); + + friend inline bool operator==( const QPoint &, const QPoint & ); + friend inline bool operator!=( const QPoint &, const QPoint & ); + friend inline const QPoint operator+( const QPoint &, const QPoint & ); + friend inline const QPoint operator-( const QPoint &, const QPoint & ); + friend inline const QPoint operator*( const QPoint &, int ); + friend inline const QPoint operator*( int, const QPoint & ); + friend inline const QPoint operator*( const QPoint &, double ); + friend inline const QPoint operator*( double, const QPoint & ); + friend inline const QPoint operator-( const QPoint & ); + friend inline const QPoint operator/( const QPoint &, int ); + friend inline const QPoint operator/( const QPoint &, double ); + +private: + static void warningDivByZero(); + +#if defined(Q_OS_MAC) + QCOORD yp; + QCOORD xp; +#else + QCOORD xp; + QCOORD yp; +#endif +}; + + +/***************************************************************************** + QPoint stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QPoint & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QPoint & ); +#endif + +/***************************************************************************** + QPoint inline functions + *****************************************************************************/ + +inline QPoint::QPoint() +{ xp=0; yp=0; } + +inline QPoint::QPoint( int xpos, int ypos ) +{ xp=(QCOORD)xpos; yp=(QCOORD)ypos; } + +inline bool QPoint::isNull() const +{ return xp == 0 && yp == 0; } + +inline int QPoint::x() const +{ return xp; } + +inline int QPoint::y() const +{ return yp; } + +inline void QPoint::setX( int x ) +{ xp = (QCOORD)x; } + +inline void QPoint::setY( int y ) +{ yp = (QCOORD)y; } + +inline QCOORD &QPoint::rx() +{ return xp; } + +inline QCOORD &QPoint::ry() +{ return yp; } + +inline QPoint &QPoint::operator+=( const QPoint &p ) +{ xp+=p.xp; yp+=p.yp; return *this; } + +inline QPoint &QPoint::operator-=( const QPoint &p ) +{ xp-=p.xp; yp-=p.yp; return *this; } + +inline QPoint &QPoint::operator*=( int c ) +{ xp*=(QCOORD)c; yp*=(QCOORD)c; return *this; } + +inline QPoint &QPoint::operator*=( double c ) +{ xp=(QCOORD)(xp*c); yp=(QCOORD)(yp*c); return *this; } + +inline bool operator==( const QPoint &p1, const QPoint &p2 ) +{ return p1.xp == p2.xp && p1.yp == p2.yp; } + +inline bool operator!=( const QPoint &p1, const QPoint &p2 ) +{ return p1.xp != p2.xp || p1.yp != p2.yp; } + +inline const QPoint operator+( const QPoint &p1, const QPoint &p2 ) +{ return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); } + +inline const QPoint operator-( const QPoint &p1, const QPoint &p2 ) +{ return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); } + +inline const QPoint operator*( const QPoint &p, int c ) +{ return QPoint(p.xp*c, p.yp*c); } + +inline const QPoint operator*( int c, const QPoint &p ) +{ return QPoint(p.xp*c, p.yp*c); } + +inline const QPoint operator*( const QPoint &p, double c ) +{ return QPoint((QCOORD)(p.xp*c), (QCOORD)(p.yp*c)); } + +inline const QPoint operator*( double c, const QPoint &p ) +{ return QPoint((QCOORD)(p.xp*c), (QCOORD)(p.yp*c)); } + +inline const QPoint operator-( const QPoint &p ) +{ return QPoint(-p.xp, -p.yp); } + +inline QPoint &QPoint::operator/=( int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + warningDivByZero(); +#endif + xp/=(QCOORD)c; + yp/=(QCOORD)c; + return *this; +} + +inline QPoint &QPoint::operator/=( double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + warningDivByZero(); +#endif + xp=(QCOORD)(xp/c); + yp=(QCOORD)(yp/c); + return *this; +} + +inline const QPoint operator/( const QPoint &p, int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + QPoint::warningDivByZero(); +#endif + return QPoint(p.xp/c, p.yp/c); +} + +inline const QPoint operator/( const QPoint &p, double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + QPoint::warningDivByZero(); +#endif + return QPoint((QCOORD)(p.xp/c), (QCOORD)(p.yp/c)); +} + +#define Q_DEFINED_QPOINT +#include "qwinexport.h" +#endif // QPOINT_H diff --git a/src/kernel/qpointarray.cpp b/src/kernel/qpointarray.cpp new file mode 100644 index 0000000..d1c7569 --- /dev/null +++ b/src/kernel/qpointarray.cpp @@ -0,0 +1,1109 @@ +/**************************************************************************** +** +** Implementation of QPointArray class +** +** Created : 940213 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpointarray.h" +#include "qrect.h" +#include "qdatastream.h" +#include "qwmatrix.h" +#include <stdarg.h> + +const double Q_PI = 3.14159265358979323846; // pi // one more useful comment + + +/*! + \class QPointArray qpointarray.h + \brief The QPointArray class provides an array of points. + + \ingroup images + \ingroup graphics + \ingroup shared + + A QPointArray is an array of QPoint objects. In addition to the + functions provided by QMemArray, QPointArray provides some + point-specific functions. + + For convenient reading and writing of the point data use + setPoints(), putPoints(), point(), and setPoint(). + + For geometry operations use boundingRect() and translate(). There + is also the QWMatrix::map() function for more general + transformations of QPointArrays. You can also create arcs and + ellipses with makeArc() and makeEllipse(). + + Among others, QPointArray is used by QPainter::drawLineSegments(), + QPainter::drawPolyline(), QPainter::drawPolygon() and + QPainter::drawCubicBezier(). + + Note that because this class is a QMemArray, copying an array and + modifying the copy modifies the original as well, i.e. a shallow + copy. If you need a deep copy use copy() or detach(), for example: + + \code + void drawGiraffe( const QPointArray & r, QPainter * p ) + { + QPointArray tmp = r; + tmp.detach(); + // some code that modifies tmp + p->drawPoints( tmp ); + } + \endcode + + If you forget the tmp.detach(), the const array will be modified. + + \sa QPainter QWMatrix QMemArray +*/ + + +/***************************************************************************** + QPointArray member functions + *****************************************************************************/ + +/*! + \fn QPointArray::QPointArray() + + Constructs a null point array. + + \sa isNull() +*/ + +/*! + \fn QPointArray::QPointArray( int size ) + + Constructs a point array with room for \a size points. Makes a + null array if \a size == 0. + + \sa resize(), isNull() +*/ + +/*! + \fn QPointArray::QPointArray( const QPointArray &a ) + + Constructs a shallow copy of the point array \a a. + + \sa copy() detach() +*/ + +/*! + Constructs a point array from the rectangle \a r. + + If \a closed is FALSE, then the point array just contains the + following four points in the listed order: r.topLeft(), + r.topRight(), r.bottomRight() and r.bottomLeft(). + + If \a closed is TRUE, then a fifth point is set to r.topLeft(). +*/ + +QPointArray::QPointArray( const QRect &r, bool closed ) +{ + setPoints( 4, r.left(), r.top(), + r.right(), r.top(), + r.right(), r.bottom(), + r.left(), r.bottom() ); + if ( closed ) { + resize( 5 ); + setPoint( 4, r.left(), r.top() ); + } +} + +/*! + \internal + Constructs a point array with \a nPoints points, taken from the + \a points array. + + Equivalent to setPoints(nPoints, points). +*/ + +QPointArray::QPointArray( int nPoints, const QCOORD *points ) +{ + setPoints( nPoints, points ); +} + + +/*! + \fn QPointArray::~QPointArray() + + Destroys the point array. +*/ + + +/*! + \fn QPointArray &QPointArray::operator=( const QPointArray &a ) + + Assigns a shallow copy of \a a to this point array and returns a + reference to this point array. + + Equivalent to assign(a). + + \sa copy() detach() +*/ + +/*! + \fn QPointArray QPointArray::copy() const + + Creates a deep copy of the array. + + \sa detach() +*/ + + + +/*! + Translates all points in the array by \a (dx, dy). +*/ + +void QPointArray::translate( int dx, int dy ) +{ + register QPoint *p = data(); + register int i = size(); + QPoint pt( dx, dy ); + while ( i-- ) { + *p += pt; + p++; + } +} + + +/*! + Reads the coordinates of the point at position \a index within the + array and writes them into \a *x and \a *y. +*/ + +void QPointArray::point( uint index, int *x, int *y ) const +{ + QPoint p = QMemArray<QPoint>::at( index ); + if ( x ) + *x = (int)p.x(); + if ( y ) + *y = (int)p.y(); +} + +/*! + \overload + + Returns the point at position \a index within the array. +*/ + +QPoint QPointArray::point( uint index ) const +{ // #### index out of bounds + return QMemArray<QPoint>::at( index ); +} + +/*! + \fn void QPointArray::setPoint( uint i, const QPoint &p ) + + \overload + + Sets the point at array index \a i to \a p. +*/ + +/*! + Sets the point at position \a index in the array to \a (x, y). +*/ + +void QPointArray::setPoint( uint index, int x, int y ) +{ // #### index out of bounds + QMemArray<QPoint>::at( index ) = QPoint( x, y ); +} + +/*! + \internal + Resizes the array to \a nPoints and sets the points in the array to + the values taken from \a points. + + Returns TRUE if successful, or FALSE if the array could not be + resized (normally due to lack of memory). + + The example code creates an array with two points (1,2) and (3,4): + \code + static QCOORD points[] = { 1,2, 3,4 }; + QPointArray a; + a.setPoints( 2, points ); + \endcode + + \sa resize(), putPoints() +*/ + +bool QPointArray::setPoints( int nPoints, const QCOORD *points ) +{ + if ( !resize(nPoints) ) + return FALSE; + int i = 0; + while ( nPoints-- ) { // make array of points + setPoint( i++, *points, *(points+1) ); + points++; + points++; + } + return TRUE; +} + +/*! + \overload + + Resizes the array to \a nPoints and sets the points in the array + to the values taken from the variable argument list. + + Returns TRUE if successful, or FALSE if the array could not be + resized (typically due to lack of memory). + + The example code creates an array with two points (1,2) and (3,4): + + \code + QPointArray a; + a.setPoints( 2, 1,2, 3,4 ); + \endcode + + The points are given as a sequence of integers, starting with \a + firstx then \a firsty, and so on. + + \sa resize(), putPoints() +*/ + +bool QPointArray::setPoints( int nPoints, int firstx, int firsty, ... ) +{ + va_list ap; + if ( !resize(nPoints) ) + return FALSE; + setPoint( 0, firstx, firsty ); // set first point + int i = 1, x, y; + nPoints--; + va_start( ap, firsty ); + while ( nPoints-- ) { + x = va_arg( ap, int ); + y = va_arg( ap, int ); + setPoint( i++, x, y ); + } + va_end( ap ); + return TRUE; +} + +/*! \overload + \internal + Copies \a nPoints points from the \a points coord array into + this point array, and resizes the point array if + \c{index+nPoints} exceeds the size of the array. + + Returns TRUE if successful, or FALSE if the array could not be + resized (typically due to lack of memory). + +*/ + +bool QPointArray::putPoints( int index, int nPoints, const QCOORD *points ) +{ + if ( index + nPoints > (int)size() ) { // extend array + if ( !resize( index + nPoints ) ) + return FALSE; + } + int i = index; + while ( nPoints-- ) { // make array of points + setPoint( i++, *points, *(points+1) ); + points++; + points++; + } + return TRUE; +} + +/*! + Copies \a nPoints points from the variable argument list into this + point array from position \a index, and resizes the point array if + \c{index+nPoints} exceeds the size of the array. + + Returns TRUE if successful, or FALSE if the array could not be + resized (typically due to lack of memory). + + The example code creates an array with three points (4,5), (6,7) + and (8,9), by expanding the array from 1 to 3 points: + + \code + QPointArray a( 1 ); + a[0] = QPoint( 4, 5 ); + a.putPoints( 1, 2, 6,7, 8,9 ); // index == 1, points == 2 + \endcode + + This has the same result, but here putPoints overwrites rather + than extends: + \code + QPointArray a( 3 ); + a.putPoints( 0, 3, 4,5, 0,0, 8,9 ); + a.putPoints( 1, 1, 6,7 ); + \endcode + + The points are given as a sequence of integers, starting with \a + firstx then \a firsty, and so on. + + \sa resize() +*/ + +bool QPointArray::putPoints( int index, int nPoints, int firstx, int firsty, + ... ) +{ + va_list ap; + if ( index + nPoints > (int)size() ) { // extend array + if ( !resize(index + nPoints) ) + return FALSE; + } + if ( nPoints <= 0 ) + return TRUE; + setPoint( index, firstx, firsty ); // set first point + int i = index + 1, x, y; + nPoints--; + va_start( ap, firsty ); + while ( nPoints-- ) { + x = va_arg( ap, int ); + y = va_arg( ap, int ); + setPoint( i++, x, y ); + } + va_end( ap ); + return TRUE; +} + + +/*! + \overload + + This version of the function copies \a nPoints from \a from into + this array, starting at \a index in this array and \a fromIndex in + \a from. \a fromIndex is 0 by default. + + \code + QPointArray a; + a.putPoints( 0, 3, 1,2, 0,0, 5,6 ); + // a is now the three-point array ( 1,2, 0,0, 5,6 ); + QPointArray b; + b.putPoints( 0, 3, 4,4, 5,5, 6,6 ); + // b is now ( 4,4, 5,5, 6,6 ); + a.putPoints( 2, 3, b ); + // a is now ( 1,2, 0,0, 4,4, 5,5, 6,6 ); + \endcode +*/ + +bool QPointArray::putPoints( int index, int nPoints, + const QPointArray & from, int fromIndex ) +{ + if ( index + nPoints > (int)size() ) { // extend array + if ( !resize(index + nPoints) ) + return FALSE; + } + if ( nPoints <= 0 ) + return TRUE; + int n = 0; + while( n < nPoints ) { + setPoint( index+n, from[fromIndex+n] ); + n++; + } + return TRUE; +} + + +/*! + Returns the bounding rectangle of the points in the array, or + QRect(0,0,0,0) if the array is empty. +*/ + +QRect QPointArray::boundingRect() const +{ + if ( isEmpty() ) + return QRect( 0, 0, 0, 0 ); // null rectangle + register QPoint *pd = data(); + int minx, maxx, miny, maxy; + minx = maxx = pd->x(); + miny = maxy = pd->y(); + pd++; + for ( int i=1; i<(int)size(); i++ ) { // find min+max x and y + if ( pd->x() < minx ) + minx = pd->x(); + else if ( pd->x() > maxx ) + maxx = pd->x(); + if ( pd->y() < miny ) + miny = pd->y(); + else if ( pd->y() > maxy ) + maxy = pd->y(); + pd++; + } + return QRect( QPoint(minx,miny), QPoint(maxx,maxy) ); +} + + +static inline int fix_angle( int a ) +{ + if ( a > 16*360 ) + a %= 16*360; + else if ( a < -16*360 ) { + a = -( (-a) % (16*360) ); + } + return a; +} + +/*! + Sets the points of the array to those describing an arc of an + ellipse with size, width \a w by height \a h, and position (\a x, + \a y), starting from angle \a a1 and spanning by angle \a a2. The + resulting array has sufficient resolution for pixel accuracy (see + the overloaded function which takes an additional QWMatrix + parameter). + + Angles are specified in 16ths of a degree, i.e. a full circle + equals 5760 (16*360). Positive values mean counter-clockwise, + whereas negative values mean the clockwise direction. Zero degrees + is at the 3 o'clock position. + + See the \link qcanvasellipse.html#anglediagram angle diagram\endlink. +*/ + +void QPointArray::makeArc( int x, int y, int w, int h, int a1, int a2 ) +{ +#if !defined(QT_OLD_MAKEELLIPSE) && !defined(QT_NO_TRANSFORMATIONS) + QWMatrix unit; + makeArc(x,y,w,h,a1,a2,unit); +#else + a1 = fix_angle( a1 ); + if ( a1 < 0 ) + a1 += 16*360; + a2 = fix_angle( a2 ); + int a3 = a2 > 0 ? a2 : -a2; // abs angle + makeEllipse( x, y, w, h ); + int npts = a3*size()/(16*360); // # points in arc array + QPointArray a(npts); + int i = a1*size()/(16*360); + int j = 0; + if ( a2 > 0 ) { + while ( npts-- ) { + if ( i >= (int)size() ) // wrap index + i = 0; + a.QMemArray<QPoint>::at( j++ ) = QMemArray<QPoint>::at( i++ ); + } + } else { + while ( npts-- ) { + if ( i < 0 ) // wrap index + i = (int)size()-1; + a.QMemArray<QPoint>::at( j++ ) = QMemArray<QPoint>::at( i-- ); + } + } + *this = a; + return; +#endif +} + +#ifndef QT_NO_TRANSFORMATIONS +// Based upon: +// parelarc.c from Graphics Gems III +// VanAken / Simar, "A Parametric Elliptical Arc Algorithm" +// +static void +qtr_elips(QPointArray& a, int off, double dxP, double dyP, double dxQ, double dyQ, double dxK, double dyK, int m) +{ +#define PIV2 102944 /* fixed point PI/2 */ +#define TWOPI 411775 /* fixed point 2*PI */ +#define HALF 32768 /* fixed point 1/2 */ + + int xP, yP, xQ, yQ, xK, yK; + xP = int(dxP * 65536.0); yP = int(dyP * 65536.0); + xQ = int(dxQ * 65536.0); yQ = int(dyQ * 65536.0); + xK = int(dxK * 65536.0); yK = int(dyK * 65536.0); + + int i; + int vx, ux, vy, uy, xJ, yJ; + + vx = xK - xQ; /* displacements from center */ + ux = xK - xP; + vy = yK - yQ; + uy = yK - yP; + xJ = xP - vx + HALF; /* center of ellipse J */ + yJ = yP - vy + HALF; + + int r; + ux -= (r = ux >> (2*m + 3)); /* cancel 2nd-order error */ + ux -= (r >>= (2*m + 4)); /* cancel 4th-order error */ + ux -= r >> (2*m + 3); /* cancel 6th-order error */ + ux += vx >> (m + 1); /* cancel 1st-order error */ + uy -= (r = uy >> (2*m + 3)); /* cancel 2nd-order error */ + uy -= (r >>= (2*m + 4)); /* cancel 4th-order error */ + uy -= r >> (2*m + 3); /* cancel 6th-order error */ + uy += vy >> (m + 1); /* cancel 1st-order error */ + + const int qn = a.size()/4; + for (i = 0; i < qn; i++) { + a[off+i] = QPoint((xJ + vx) >> 16, (yJ + vy) >> 16); + ux -= vx >> m; + vx += ux >> m; + uy -= vy >> m; + vy += uy >> m; + } + +#undef PIV2 +#undef TWOPI +#undef HALF +} + + +/*! + \overload + + Sets the points of the array to those describing an arc of an + ellipse with width \a w and height \a h and position (\a x, \a y), + starting from angle \a a1, and spanning angle by \a a2, and + transformed by the matrix \a xf. The resulting array has + sufficient resolution for pixel accuracy. + + Angles are specified in 16ths of a degree, i.e. a full circle + equals 5760 (16*360). Positive values mean counter-clockwise, + whereas negative values mean the clockwise direction. Zero degrees + is at the 3 o'clock position. + + See the \link qcanvasellipse.html#anglediagram angle diagram\endlink. +*/ +void QPointArray::makeArc( int x, int y, int w, int h, + int a1, int a2, + const QWMatrix& xf ) +{ +#define PIV2 102944 /* fixed point PI/2 */ + if ( --w < 0 || --h < 0 || !a2 ) { + resize( 0 ); + return; + } + + bool rev = a2 < 0; + if ( rev ) { + a1 += a2; + a2 = -a2; + } + a1 = fix_angle( a1 ); + if ( a1 < 0 ) + a1 += 16*360; + a2 = fix_angle( a2 ); + + bool arc = a1 != 0 || a2 != 360*16 || rev; + + double xP, yP, xQ, yQ, xK, yK; + + xf.map(x+w, y+h/2.0, &xP, &yP); + xf.map(x+w/2.0, y, &xQ, &yQ); + xf.map(x+w, y, &xK, &yK); + + int m = 3; + int max; + int q = int(QMAX(QABS(xP-xQ),QABS(yP-yQ))); + if ( arc ) + q *= 2; + do { + m++; + max = 4*(1 + (PIV2 >> (16 - m)) ); + } while (max < q && m < 16); // 16 limits memory usage on HUGE arcs + + double inc = 1.0/(1<<m); + + const int qn = (PIV2 >> (16 - m)); + resize(qn*4); + + qtr_elips(*this, 0, xP, yP, xQ, yQ, xK, yK, m); + xP = xQ; yP = yQ; + xf.map(x, y+h/2.0, &xQ, &yQ); + xf.map(x, y, &xK, &yK); + qtr_elips(*this, qn, xP, yP, xQ, yQ, xK, yK, m); + xP = xQ; yP = yQ; + xf.map(x+w/2.0, y+h, &xQ, &yQ); + xf.map(x, y+h, &xK, &yK); + qtr_elips(*this, qn*2, xP, yP, xQ, yQ, xK, yK, m); + xP = xQ; yP = yQ; + xf.map(x+w, y+h/2.0, &xQ, &yQ); + xf.map(x+w, y+h, &xK, &yK); + qtr_elips(*this, qn*3, xP, yP, xQ, yQ, xK, yK, m); + + int n = size(); + + if ( arc ) { + double da1 = double(a1)*Q_PI / (360*8); + double da3 = double(a2+a1)*Q_PI / (360*8); + int i = int(da1/inc+0.5); + int l = int(da3/inc+0.5); + int k = (l-i)+1; + QPointArray r(k); + int j = 0; + + if ( rev ) { + while ( k-- ) + r[j++] = at((i+k)%n); + } else { + while ( j < k ) { + r[j] = at((i+j)%n); + j++; + } + } + *this = r; + } +#undef PIV2 +} + +#endif // QT_NO_TRANSFORMATIONS + +/*! + Sets the points of the array to those describing an ellipse with + size, width \a w by height \a h, and position (\a x, \a y). + + The returned array has sufficient resolution for use as pixels. +*/ +void QPointArray::makeEllipse( int x, int y, int w, int h ) +{ // midpoint, 1/4 ellipse +#if !defined(QT_OLD_MAKEELLIPSE) && !defined(QT_NO_TRANSFORMATIONS) + QWMatrix unit; + makeArc(x,y,w,h,0,360*16,unit); + return; +#else + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) { + resize( 0 ); + return; + } + if ( w < 0 ) { // negative width + w = -w; + x -= w; + } + if ( h < 0 ) { // negative height + h = -h; + y -= h; + } + } + int s = (w+h+2)/2; // max size of xx,yy array + int *px = new int[s]; // 1/4th of ellipse + int *py = new int[s]; + int xx, yy, i=0; + double d1, d2; + double a2=(w/2)*(w/2), b2=(h/2)*(h/2); + xx = 0; + yy = int(h/2); + d1 = b2 - a2*(h/2) + 0.25*a2; + px[i] = xx; + py[i] = yy; + i++; + while ( a2*(yy-0.5) > b2*(xx+0.5) ) { // region 1 + if ( d1 < 0 ) { + d1 = d1 + b2*(3.0+2*xx); + xx++; + } else { + d1 = d1 + b2*(3.0+2*xx) + 2.0*a2*(1-yy); + xx++; + yy--; + } + px[i] = xx; + py[i] = yy; + i++; + } + d2 = b2*(xx+0.5)*(xx+0.5) + a2*(yy-1)*(yy-1) - a2*b2; + while ( yy > 0 ) { // region 2 + if ( d2 < 0 ) { + d2 = d2 + 2.0*b2*(xx+1) + a2*(3-2*yy); + xx++; + yy--; + } else { + d2 = d2 + a2*(3-2*yy); + yy--; + } + px[i] = xx; + py[i] = yy; + i++; + } + s = i; + resize( 4*s ); // make full point array + x += w/2; + y += h/2; + for ( i=0; i<s; i++ ) { // mirror + xx = px[i]; + yy = py[i]; + setPoint( s-i-1, x+xx, y-yy ); + setPoint( s+i, x-xx, y-yy ); + setPoint( 3*s-i-1, x-xx, y+yy ); + setPoint( 3*s+i, x+xx, y+yy ); + } + delete[] px; + delete[] py; +#endif +} + +#ifndef QT_NO_BEZIER +// Work functions for QPointArray::cubicBezier() +static +void split(const double *p, double *l, double *r) +{ + double tmpx; + double tmpy; + + l[0] = p[0]; + l[1] = p[1]; + r[6] = p[6]; + r[7] = p[7]; + + l[2] = (p[0]+ p[2])/2; + l[3] = (p[1]+ p[3])/2; + tmpx = (p[2]+ p[4])/2; + tmpy = (p[3]+ p[5])/2; + r[4] = (p[4]+ p[6])/2; + r[5] = (p[5]+ p[7])/2; + + l[4] = (l[2]+ tmpx)/2; + l[5] = (l[3]+ tmpy)/2; + r[2] = (tmpx + r[4])/2; + r[3] = (tmpy + r[5])/2; + + l[6] = (l[4]+ r[2])/2; + l[7] = (l[5]+ r[3])/2; + r[0] = l[6]; + r[1] = l[7]; +} + +// Based on: +// +// A Fast 2D Point-On-Line Test +// by Alan Paeth +// from "Graphics Gems", Academic Press, 1990 +static +int pnt_on_line( const int* p, const int* q, const int* t ) +{ +/* + * given a line through P:(px,py) Q:(qx,qy) and T:(tx,ty) + * return 0 if T is not on the line through <--P--Q--> + * 1 if T is on the open ray ending at P: <--P + * 2 if T is on the closed interior along: P--Q + * 3 if T is on the open ray beginning at Q: Q--> + * + * Example: consider the line P = (3,2), Q = (17,7). A plot + * of the test points T(x,y) (with 0 mapped onto '.') yields: + * + * 8| . . . . . . . . . . . . . . . . . 3 3 + * Y 7| . . . . . . . . . . . . . . 2 2 Q 3 3 Q = 2 + * 6| . . . . . . . . . . . 2 2 2 2 2 . . . + * a 5| . . . . . . . . 2 2 2 2 2 2 . . . . . + * x 4| . . . . . 2 2 2 2 2 2 . . . . . . . . + * i 3| . . . 2 2 2 2 2 . . . . . . . . . . . + * s 2| 1 1 P 2 2 . . . . . . . . . . . . . . P = 2 + * 1| 1 1 . . . . . . . . . . . . . . . . . + * +-------------------------------------- + * 1 2 3 4 5 X-axis 10 15 19 + * + * Point-Line distance is normalized with the Infinity Norm + * avoiding square-root code and tightening the test vs the + * Manhattan Norm. All math is done on the field of integers. + * The latter replaces the initial ">= MAX(...)" test with + * "> (ABS(qx-px) + ABS(qy-py))" loosening both inequality + * and norm, yielding a broader target line for selection. + * The tightest test is employed here for best discrimination + * in merging collinear (to grid coordinates) vertex chains + * into a larger, spanning vectors within the Lemming editor. + */ + + // if all points are coincident, return condition 2 (on line) + if(q[0]==p[0] && q[1]==p[1] && q[0]==t[0] && q[1]==t[1]) { + return 2; + } + + if ( QABS((q[1]-p[1])*(t[0]-p[0])-(t[1]-p[1])*(q[0]-p[0])) >= + (QMAX(QABS(q[0]-p[0]), QABS(q[1]-p[1])))) return 0; + + if (((q[0]<p[0])&&(p[0]<t[0])) || ((q[1]<p[1])&&(p[1]<t[1]))) + return 1 ; + if (((t[0]<p[0])&&(p[0]<q[0])) || ((t[1]<p[1])&&(p[1]<q[1]))) + return 1 ; + if (((p[0]<q[0])&&(q[0]<t[0])) || ((p[1]<q[1])&&(q[1]<t[1]))) + return 3 ; + if (((t[0]<q[0])&&(q[0]<p[0])) || ((t[1]<q[1])&&(q[1]<p[1]))) + return 3 ; + + return 2 ; +} +static +void polygonizeQBezier( double* acc, int& accsize, const double ctrl[], + int maxsize ) +{ + if ( accsize > maxsize / 2 ) + { + // This never happens in practice. + + if ( accsize >= maxsize-4 ) + return; + // Running out of space - approximate by a line. + acc[accsize++] = ctrl[0]; + acc[accsize++] = ctrl[1]; + acc[accsize++] = ctrl[6]; + acc[accsize++] = ctrl[7]; + return; + } + + //intersects: + double l[8]; + double r[8]; + split( ctrl, l, r); + + // convert to integers for line condition check + int c0[2]; c0[0] = int(ctrl[0]); c0[1] = int(ctrl[1]); + int c1[2]; c1[0] = int(ctrl[2]); c1[1] = int(ctrl[3]); + int c2[2]; c2[0] = int(ctrl[4]); c2[1] = int(ctrl[5]); + int c3[2]; c3[0] = int(ctrl[6]); c3[1] = int(ctrl[7]); + + // #### Duplication needed? + if ( QABS(c1[0]-c0[0]) <= 1 && QABS(c1[1]-c0[1]) <= 1 + && QABS(c2[0]-c0[0]) <= 1 && QABS(c2[1]-c0[1]) <= 1 + && QABS(c3[0]-c1[0]) <= 1 && QABS(c3[1]-c0[1]) <= 1 ) + { + // Approximate by one line. + // Dont need to write last pt as it is the same as first pt + // on the next segment + acc[accsize++] = l[0]; + acc[accsize++] = l[1]; + return; + } + + if ( ( pnt_on_line( c0, c3, c1 ) == 2 && pnt_on_line( c0, c3, c2 ) == 2 ) + || ( QABS(c1[0]-c0[0]) <= 1 && QABS(c1[1]-c0[1]) <= 1 + && QABS(c2[0]-c0[0]) <= 1 && QABS(c2[1]-c0[1]) <= 1 + && QABS(c3[0]-c1[0]) <= 1 && QABS(c3[1]-c0[1]) <= 1 ) ) + { + // Approximate by one line. + // Dont need to write last pt as it is the same as first pt + // on the next segment + acc[accsize++] = l[0]; + acc[accsize++] = l[1]; + return; + } + + // Too big and too curved - recusively subdivide. + polygonizeQBezier( acc, accsize, l, maxsize ); + polygonizeQBezier( acc, accsize, r, maxsize ); +} + +/*! + Returns the Bezier points for the four control points in this + array. +*/ + +QPointArray QPointArray::cubicBezier() const +{ +#ifdef USE_SIMPLE_QBEZIER_CODE + if ( size() != 4 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPointArray::bezier: The array must have 4 control points" ); +#endif + QPointArray p; + return p; + } + + int v; + float xvec[4]; + float yvec[4]; + for ( v=0; v<4; v++ ) { // store all x,y in xvec,yvec + int x, y; + point( v, &x, &y ); + xvec[v] = (float)x; + yvec[v] = (float)y; + } + + QRect r = boundingRect(); + int m = QMAX(r.width(),r.height())/2; + m = QMIN(m,30); // m = number of result points + if ( m < 2 ) // at least two points + m = 2; + QPointArray p( m ); // p = Bezier point array + register QPointData *pd = p.data(); + + float x0 = xvec[0], y0 = yvec[0]; + float dt = 1.0F/m; + float cx = 3.0F * (xvec[1] - x0); + float bx = 3.0F * (xvec[2] - xvec[1]) - cx; + float ax = xvec[3] - (x0 + cx + bx); + float cy = 3.0F * (yvec[1] - y0); + float by = 3.0F * (yvec[2] - yvec[1]) - cy; + float ay = yvec[3] - (y0 + cy + by); + float t = dt; + + pd->rx() = (QCOORD)xvec[0]; + pd->ry() = (QCOORD)yvec[0]; + pd++; + m -= 2; + + while ( m-- ) { + pd->rx() = (QCOORD)qRound( ((ax * t + bx) * t + cx) * t + x0 ); + pd->ry() = (QCOORD)qRound( ((ay * t + by) * t + cy) * t + y0 ); + pd++; + t += dt; + } + + pd->rx() = (QCOORD)xvec[3]; + pd->ry() = (QCOORD)yvec[3]; + + return p; +#else + + if ( size() != 4 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QPointArray::bezier: The array must have 4 control points" ); +#endif + QPointArray pa; + return pa; + } else { + QRect r = boundingRect(); + int m = 4+2*QMAX(r.width(),r.height()); + double *p = new double[m]; + double ctrl[8]; + int i; + for (i=0; i<4; i++) { + ctrl[i*2] = at(i).x(); + ctrl[i*2+1] = at(i).y(); + } + int len=0; + polygonizeQBezier( p, len, ctrl, m ); + QPointArray pa((len/2)+1); // one extra point for last point on line + int j=0; + for (i=0; j<len; i++) { + int x = qRound(p[j++]); + int y = qRound(p[j++]); + pa[i] = QPoint(x,y); + } + // add last pt on the line, which will be at the last control pt + pa[(int)pa.size()-1] = at(3); + delete[] p; + + return pa; + } + +#endif +} +#endif //QT_NO_BEZIER + +/***************************************************************************** + QPointArray stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QPointArray + + Writes the point array, \a a to the stream \a s and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QPointArray &a ) +{ + register uint i; + uint len = a.size(); + s << len; // write size of array + for ( i=0; i<len; i++ ) // write each point + s << a.point( i ); + return s; +} + +/*! + \relates QPointArray + + Reads a point array, \a a from the stream \a s and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QPointArray &a ) +{ + register uint i; + uint len; + s >> len; // read size of array + if ( !a.resize( len ) ) // no memory + return s; + QPoint p; + for ( i=0; i<len; i++ ) { // read each point + s >> p; + a.setPoint( i, p ); + } + return s; +} +#endif //QT_NO_DATASTREAM + + + +struct QShortPoint { // Binary compatible with XPoint + short x, y; +}; + +uint QPointArray::splen = 0; +void* QPointArray::sp = 0; // Really a QShortPoint* + +/*! + \internal + + Converts the point coords to short (16bit) size, compatible with + X11's XPoint structure. The pointer returned points to a static + array, so its contents will be overwritten the next time this + function is called. +*/ + +void* QPointArray::shortPoints( int index, int nPoints ) const +{ + + if ( isNull() || !nPoints ) + return 0; + QPoint* p = data(); + p += index; + uint i = nPoints < 0 ? size() : nPoints; + if ( splen < i ) { + if ( sp ) + delete[] ((QShortPoint*)sp); + sp = new QShortPoint[i]; + splen = i; + } + QShortPoint* ps = (QShortPoint*)sp; + while ( i-- ) { + ps->x = (short)p->x(); + ps->y = (short)p->y(); + p++; + ps++; + } + return sp; +} + + +/*! + \internal + + Deallocates the internal buffer used by shortPoints(). +*/ + +void QPointArray::cleanBuffers() +{ + if ( sp ) + delete[] ((QShortPoint*)sp); + sp = 0; + splen = 0; +} diff --git a/src/kernel/qpointarray.h b/src/kernel/qpointarray.h new file mode 100644 index 0000000..aa798ea --- /dev/null +++ b/src/kernel/qpointarray.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Definition of QPointArray class +** +** Created : 940213 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPOINTARRAY_H +#define QPOINTARRAY_H + +#ifndef QT_H +#include "qmemarray.h" +#include "qpoint.h" +#endif // QT_H + + +#if defined(Q_TEMPLATEDLL) +//Q_TEMPLATE_EXTERN template class Q_EXPORT QMemArray<QPoint>; +#endif + +class Q_EXPORT QPointArray : public QMemArray<QPoint> +{ +public: + QPointArray() {} + ~QPointArray() {} + QPointArray( int size ) : QMemArray<QPoint>( size ) {} + QPointArray( const QPointArray &a ) : QMemArray<QPoint>( a ) {} + QPointArray( const QRect &r, bool closed=FALSE ); + QPointArray( int nPoints, const QCOORD *points ); + + QPointArray &operator=( const QPointArray &a ) + { return (QPointArray&)assign( a ); } + + QPointArray copy() const + { QPointArray tmp; return *((QPointArray*)&tmp.duplicate(*this)); } + + void translate( int dx, int dy ); + QRect boundingRect() const; + + void point( uint i, int *x, int *y ) const; + QPoint point( uint i ) const; + void setPoint( uint i, int x, int y ); + void setPoint( uint i, const QPoint &p ); + bool setPoints( int nPoints, const QCOORD *points ); + bool setPoints( int nPoints, int firstx, int firsty, ... ); + bool putPoints( int index, int nPoints, const QCOORD *points ); + bool putPoints( int index, int nPoints, int firstx, int firsty, ... ); + bool putPoints( int index, int nPoints, + const QPointArray & from, int fromIndex=0 ); + + void makeArc( int x, int y, int w, int h, int a1, int a2 ); + void makeEllipse( int x, int y, int w, int h ); + void makeArc( int x, int y, int w, int h, int a1, int a2, + const QWMatrix& ); +#ifndef QT_NO_BEZIER + QPointArray cubicBezier() const; +#endif + void* shortPoints( int index = 0, int nPoints = -1 ) const; + static void cleanBuffers(); + +protected: + static uint splen; + static void* sp; +}; + + +/***************************************************************************** + QPointArray stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QPointArray & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QPointArray & ); +#endif + +/***************************************************************************** + Misc. QPointArray functions + *****************************************************************************/ + +inline void QPointArray::setPoint( uint i, const QPoint &p ) +{ + setPoint( i, p.x(), p.y() ); +} + + +#endif // QPOINTARRAY_H diff --git a/src/kernel/qpolygonscanner.cpp b/src/kernel/qpolygonscanner.cpp new file mode 100644 index 0000000..86f9ca0 --- /dev/null +++ b/src/kernel/qpolygonscanner.cpp @@ -0,0 +1,937 @@ +/**************************************************************************** +** +** Implementation of QPolygonScanner class +** +** Created : 000120 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qpolygonscanner.h" +#include "qpointarray.h" +#include <stdlib.h> + + +// Based on Xserver code miFillGeneralPoly... +/* + * + * Written by Brian Kelleher; Oct. 1985 + * + * Routine to fill a polygon. Two fill rules are + * supported: frWINDING and frEVENODD. + * + * See fillpoly.h for a complete description of the algorithm. + */ + +/* + * These are the data structures needed to scan + * convert regions. Two different scan conversion + * methods are available -- the even-odd method, and + * the winding number method. + * The even-odd rule states that a point is inside + * the polygon if a ray drawn from that point in any + * direction will pass through an odd number of + * path segments. + * By the winding number rule, a point is decided + * to be inside the polygon if a ray drawn from that + * point in any direction passes through a different + * number of clockwise and counterclockwise path + * segments. + * + * These data structures are adapted somewhat from + * the algorithm in (Foley/Van Dam) for scan converting + * polygons. + * The basic algorithm is to start at the top (smallest y) + * of the polygon, stepping down to the bottom of + * the polygon by incrementing the y coordinate. We + * keep a list of edges which the current scanline crosses, + * sorted by x. This list is called the Active Edge Table (AET) + * As we change the y-coordinate, we update each entry in + * in the active edge table to reflect the edges new xcoord. + * This list must be sorted at each scanline in case + * two edges intersect. + * We also keep a data structure known as the Edge Table (ET), + * which keeps track of all the edges which the current + * scanline has not yet reached. The ET is basically a + * list of ScanLineList structures containing a list of + * edges which are entered at a given scanline. There is one + * ScanLineList per scanline at which an edge is entered. + * When we enter a new edge, we move it from the ET to the AET. + * + * From the AET, we can implement the even-odd rule as in + * (Foley/Van Dam). + * The winding number rule is a little trickier. We also + * keep the EdgeTableEntries in the AET linked by the + * nextWETE (winding EdgeTableEntry) link. This allows + * the edges to be linked just as before for updating + * purposes, but only uses the edges linked by the nextWETE + * link as edges representing spans of the polygon to + * drawn (as with the even-odd rule). + */ + +/* $XConsortium: miscanfill.h,v 1.5 94/04/17 20:27:50 dpw Exp $ */ +/* + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall +not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization +from the X Consortium. + +*/ + + +/* + * scanfill.h + * + * Written by Brian Kelleher; Jan 1985 + * + * This file contains a few macros to help track + * the edge of a filled object. The object is assumed + * to be filled in scanline order, and thus the + * algorithm used is an extension of Bresenham's line + * drawing algorithm which assumes that y is always the + * major axis. + * Since these pieces of code are the same for any filled shape, + * it is more convenient to gather the library in one + * place, but since these pieces of code are also in + * the inner loops of output primitives, procedure call + * overhead is out of the question. + * See the author for a derivation if needed. + */ + +/* + * In scan converting polygons, we want to choose those pixels + * which are inside the polygon. Thus, we add .5 to the starting + * x coordinate for both left and right edges. Now we choose the + * first pixel which is inside the pgon for the left edge and the + * first pixel which is outside the pgon for the right edge. + * Draw the left pixel, but not the right. + * + * How to add .5 to the starting x coordinate: + * If the edge is moving to the right, then subtract dy from the + * error term from the general form of the algorithm. + * If the edge is moving to the left, then add dy to the error term. + * + * The reason for the difference between edges moving to the left + * and edges moving to the right is simple: If an edge is moving + * to the right, then we want the algorithm to flip immediately. + * If it is moving to the left, then we don't want it to flip until + * we traverse an entire pixel. + */ +#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \ + int dx; /* local storage */ \ +\ + /* \ + * if the edge is horizontal, then it is ignored \ + * and assumed not to be processed. Otherwise, do this stuff. \ + */ \ + if ((dy) != 0) { \ + xStart = (x1); \ + dx = (x2) - xStart; \ + if (dx < 0) { \ + m = dx / (dy); \ + m1 = m - 1; \ + incr1 = -2 * dx + 2 * (dy) * m1; \ + incr2 = -2 * dx + 2 * (dy) * m; \ + d = 2 * m * (dy) - 2 * dx - 2 * (dy); \ + } else { \ + m = dx / (dy); \ + m1 = m + 1; \ + incr1 = 2 * dx - 2 * (dy) * m1; \ + incr2 = 2 * dx - 2 * (dy) * m; \ + d = -2 * m * (dy) + 2 * dx; \ + } \ + } \ +} + +#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \ + if (m1 > 0) { \ + if (d > 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } else {\ + if (d >= 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } \ +} + + +/* + * This structure contains all of the information needed + * to run the bresenham algorithm. + * The variables may be hardcoded into the declarations + * instead of using this structure to make use of + * register declarations. + */ +typedef struct { + int minor; /* minor axis */ + int d; /* decision variable */ + int m, m1; /* slope and slope+1 */ + int incr1, incr2; /* error increments */ +} BRESINFO; + + +#define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \ + BRESINITPGON(dmaj, min1, min2, bres.minor, bres.d, \ + bres.m, bres.m1, bres.incr1, bres.incr2) + +#define BRESINCRPGONSTRUCT(bres) \ + BRESINCRPGON(bres.d, bres.minor, bres.m, bres.m1, bres.incr1, bres.incr2) + + +typedef struct _EdgeTableEntry { + int ymax; /* ycoord at which we exit this edge. */ + BRESINFO bres; /* Bresenham info to run the edge */ + struct _EdgeTableEntry *next; /* next in the list */ + struct _EdgeTableEntry *back; /* for insertion sort */ + struct _EdgeTableEntry *nextWETE; /* for winding num rule */ + int ClockWise; /* flag for winding number rule */ +} EdgeTableEntry; + + +typedef struct _ScanLineList{ + int scanline; /* the scanline represented */ + EdgeTableEntry *edgelist; /* header node */ + struct _ScanLineList *next; /* next in the list */ +} ScanLineList; + + +typedef struct { + int ymax; /* ymax for the polygon */ + int ymin; /* ymin for the polygon */ + ScanLineList scanlines; /* header node */ +} EdgeTable; + + +/* + * Here is a struct to help with storage allocation + * so we can allocate a big chunk at a time, and then take + * pieces from this heap when we need to. + */ +#define SLLSPERBLOCK 25 + +typedef struct _ScanLineListBlock { + ScanLineList SLLs[SLLSPERBLOCK]; + struct _ScanLineListBlock *next; +} ScanLineListBlock; + +/* + * number of points to buffer before sending them off + * to scanlines() : Must be an even number + */ +#define NUMPTSTOBUFFER 200 + +/* + * + * a few macros for the inner loops of the fill code where + * performance considerations don't allow a procedure call. + * + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The winding number rule is in effect, so we must notify + * the caller when the edge has been removed so he + * can reorder the Winding Active Edge Table. + */ +#define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + fixWAET = 1; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres); \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} + + +/* + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The even-odd rule is in effect. + */ +#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres) \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} + +/*********************************************************** + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +******************************************************************/ + +#define MAXINT 0x7fffffff +#define MININT -MAXINT + +/* + * fillUtils.c + * + * Written by Brian Kelleher; Oct. 1985 + * + * This module contains all of the utility functions + * needed to scan convert a polygon. + * + */ +/* + * InsertEdgeInET + * + * Insert the given edge into the edge table. + * First we must find the correct bucket in the + * Edge table, then find the right slot in the + * bucket. Finally, we can insert it. + * + */ +static bool +miInsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE, + int scanline, ScanLineListBlock **SLLBlock, int *iSLLBlock) +{ + register EdgeTableEntry *start, *prev; + register ScanLineList *pSLL, *pPrevSLL; + ScanLineListBlock *tmpSLLBlock; + + /* + * find the right bucket to put the edge into + */ + pPrevSLL = &ET->scanlines; + pSLL = pPrevSLL->next; + while (pSLL && (pSLL->scanline < scanline)) + { + pPrevSLL = pSLL; + pSLL = pSLL->next; + } + + /* + * reassign pSLL (pointer to ScanLineList) if necessary + */ + if ((!pSLL) || (pSLL->scanline > scanline)) + { + if (*iSLLBlock > SLLSPERBLOCK-1) + { + tmpSLLBlock = + (ScanLineListBlock *)malloc(sizeof(ScanLineListBlock)); + if (!tmpSLLBlock) + return FALSE; + (*SLLBlock)->next = tmpSLLBlock; + tmpSLLBlock->next = 0; + *SLLBlock = tmpSLLBlock; + *iSLLBlock = 0; + } + pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]); + + pSLL->next = pPrevSLL->next; + pSLL->edgelist = 0; + pPrevSLL->next = pSLL; + } + pSLL->scanline = scanline; + + /* + * now insert the edge in the right bucket + */ + prev = 0; + start = pSLL->edgelist; + while (start && (start->bres.minor < ETE->bres.minor)) + { + prev = start; + start = start->next; + } + ETE->next = start; + + if (prev) + prev->next = ETE; + else + pSLL->edgelist = ETE; + return TRUE; +} + +/* + * CreateEdgeTable + * + * This routine creates the edge table for + * scan converting polygons. + * The Edge Table (ET) looks like: + * + * EdgeTable + * -------- + * | ymax | ScanLineLists + * |scanline|-->------------>-------------->... + * -------- |scanline| |scanline| + * |edgelist| |edgelist| + * --------- --------- + * | | + * | | + * V V + * list of ETEs list of ETEs + * + * where ETE is an EdgeTableEntry data structure, + * and there is one ScanLineList per scanline at + * which an edge is initially entered. + * + */ + +typedef struct { +#if defined(Q_OS_MAC) + int y, x; +#else + int x, y; +#endif + +} DDXPointRec, *DDXPointPtr; + +/* + * Clean up our act. + */ +static void +miFreeStorage(ScanLineListBlock *pSLLBlock) +{ + register ScanLineListBlock *tmpSLLBlock; + + while (pSLLBlock) + { + tmpSLLBlock = pSLLBlock->next; + free(pSLLBlock); + pSLLBlock = tmpSLLBlock; + } +} + +static bool +miCreateETandAET(int count, DDXPointPtr pts, EdgeTable *ET, + EdgeTableEntry *AET, EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock) +{ + register DDXPointPtr top, bottom; + register DDXPointPtr PrevPt, CurrPt; + int iSLLBlock = 0; + + int dy; + + if (count < 2) return TRUE; + + /* + * initialize the Active Edge Table + */ + AET->next = 0; + AET->back = 0; + AET->nextWETE = 0; + AET->bres.minor = MININT; + + /* + * initialize the Edge Table. + */ + ET->scanlines.next = 0; + ET->ymax = MININT; + ET->ymin = MAXINT; + pSLLBlock->next = 0; + + PrevPt = &pts[count-1]; + + /* + * for each vertex in the array of points. + * In this loop we are dealing with two vertices at + * a time -- these make up one edge of the polygon. + */ + while (count--) + { + CurrPt = pts++; + + /* + * find out which point is above and which is below. + */ + if (PrevPt->y > CurrPt->y) + { + bottom = PrevPt, top = CurrPt; + pETEs->ClockWise = 0; + } + else + { + bottom = CurrPt, top = PrevPt; + pETEs->ClockWise = 1; + } + + /* + * don't add horizontal edges to the Edge table. + */ + if (bottom->y != top->y) + { + pETEs->ymax = bottom->y-1; /* -1 so we don't get last scanline */ + + /* + * initialize integer edge algorithm + */ + dy = bottom->y - top->y; + BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres) + + if (!miInsertEdgeInET(ET, pETEs, top->y, &pSLLBlock, &iSLLBlock)) + { + miFreeStorage(pSLLBlock->next); + return FALSE; + } + + ET->ymax = QMAX(ET->ymax, PrevPt->y); + ET->ymin = QMIN(ET->ymin, PrevPt->y); + pETEs++; + } + + PrevPt = CurrPt; + } + return TRUE; +} + +/* + * loadAET + * + * This routine moves EdgeTableEntries from the + * EdgeTable into the Active Edge Table, + * leaving them sorted by smaller x coordinate. + * + */ + +static void +miloadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs) +{ + register EdgeTableEntry *pPrevAET; + register EdgeTableEntry *tmp; + + pPrevAET = AET; + AET = AET->next; + while (ETEs) + { + while (AET && (AET->bres.minor < ETEs->bres.minor)) + { + pPrevAET = AET; + AET = AET->next; + } + tmp = ETEs->next; + ETEs->next = AET; + if (AET) + AET->back = ETEs; + ETEs->back = pPrevAET; + pPrevAET->next = ETEs; + pPrevAET = ETEs; + + ETEs = tmp; + } +} + +/* + * computeWAET + * + * This routine links the AET by the + * nextWETE (winding EdgeTableEntry) link for + * use by the winding number rule. The final + * Active Edge Table (AET) might look something + * like: + * + * AET + * ---------- --------- --------- + * |ymax | |ymax | |ymax | + * | ... | |... | |... | + * |next |->|next |->|next |->... + * |nextWETE| |nextWETE| |nextWETE| + * --------- --------- ^-------- + * | | | + * V-------------------> V---> ... + * + */ +static void +micomputeWAET(EdgeTableEntry *AET) +{ + register EdgeTableEntry *pWETE; + register int inside = 1; + register int isInside = 0; + + AET->nextWETE = 0; + pWETE = AET; + AET = AET->next; + while (AET) + { + if (AET->ClockWise) + isInside++; + else + isInside--; + + if ((!inside && !isInside) || + ( inside && isInside)) + { + pWETE->nextWETE = AET; + pWETE = AET; + inside = !inside; + } + AET = AET->next; + } + pWETE->nextWETE = 0; +} + +/* + * InsertionSort + * + * Just a simple insertion sort using + * pointers and back pointers to sort the Active + * Edge Table. + * + */ + +static int +miInsertionSort(EdgeTableEntry *AET) +{ + register EdgeTableEntry *pETEchase; + register EdgeTableEntry *pETEinsert; + register EdgeTableEntry *pETEchaseBackTMP; + register int changed = 0; + + AET = AET->next; + while (AET) + { + pETEinsert = AET; + pETEchase = AET; + while (pETEchase->back->bres.minor > AET->bres.minor) + pETEchase = pETEchase->back; + + AET = AET->next; + if (pETEchase != pETEinsert) + { + pETEchaseBackTMP = pETEchase->back; + pETEinsert->back->next = AET; + if (AET) + AET->back = pETEinsert->back; + pETEinsert->next = pETEchase; + pETEchase->back->next = pETEinsert; + pETEchase->back = pETEinsert; + pETEinsert->back = pETEchaseBackTMP; + changed = 1; + } + } + return(changed); +} + +/*! + \overload +*/ +void QPolygonScanner::scan(const QPointArray& pa, bool winding, int index, int npoints) +{ + scan( pa, winding, index, npoints, TRUE ); +} + +/*! + \overload + + If \a stitchable is FALSE, the right and bottom edges of the + polygon are included. This causes adjacent polygons to overlap. +*/ +void QPolygonScanner::scan(const QPointArray& pa, bool winding, int index, int npoints, bool stitchable) +{ + scan( pa, winding, index, npoints, + stitchable ? Edge(Left+Top) : Edge(Left+Right+Top+Bottom) ); +} + +/*! + Calls processSpans() for all scanlines of the polygon defined by + \a npoints starting at \a index in \a pa. + + If \a winding is TRUE, the Winding algorithm rather than the + Odd-Even rule is used. + + The \a edges is any bitwise combination of: + \list + \i \c QPolygonScanner::Left + \i \c QPolygonScanner::Right + \i \c QPolygonScanner::Top + \i \c QPolygonScanner::Bottom + \endlist + \a edges determines which edges are included. + + \warning The edges feature does not work properly. + +*/ +void QPolygonScanner::scan( const QPointArray& pa, bool winding, int index, int npoints, Edge edges ) +{ + + + DDXPointPtr ptsIn = (DDXPointPtr)pa.data(); + ptsIn += index; + register EdgeTableEntry *pAET; /* the Active Edge Table */ + register int y; /* the current scanline */ + register int nPts = 0; /* number of pts in buffer */ + register EdgeTableEntry *pWETE; /* Winding Edge Table */ + register ScanLineList *pSLL; /* Current ScanLineList */ + register DDXPointPtr ptsOut; /* ptr to output buffers */ + int *width; + DDXPointRec FirstPoint[NUMPTSTOBUFFER]; /* the output buffers */ + int FirstWidth[NUMPTSTOBUFFER]; + EdgeTableEntry *pPrevAET; /* previous AET entry */ + EdgeTable ET; /* Edge Table header node */ + EdgeTableEntry AET; /* Active ET header node */ + EdgeTableEntry *pETEs; /* Edge Table Entries buff */ + ScanLineListBlock SLLBlock; /* header for ScanLineList */ + int fixWAET = 0; + int edge_l = (edges & Left) ? 1 : 0; + int edge_r = (edges & Right) ? 1 : 0; + int edge_t = 1; //#### (edges & Top) ? 1 : 0; + int edge_b = (edges & Bottom) ? 1 : 0; + + if (npoints == -1) + npoints = pa.size(); + + if (npoints < 3) + return; + + if(!(pETEs = (EdgeTableEntry *) + malloc(sizeof(EdgeTableEntry) * npoints))) + return; + ptsOut = FirstPoint; + width = FirstWidth; + if (!miCreateETandAET(npoints, ptsIn, &ET, &AET, pETEs, &SLLBlock)) + { + free(pETEs); + return; + } + pSLL = ET.scanlines.next; + + if (!winding) + { + /* + * for each scanline + */ + for (y = ET.ymin+1-edge_t; y < ET.ymax+edge_b; y++) + { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL && y == pSLL->scanline) + { + miloadAET(&AET, pSLL->edgelist); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + + /* + * for each active edge + */ + while (pAET) + { + ptsOut->x = pAET->bres.minor + 1 - edge_l; + ptsOut++->y = y; + *width++ = pAET->next->bres.minor - pAET->bres.minor + - 1 + edge_l + edge_r; + nPts++; + + /* + * send out the buffer when its full + */ + if (nPts == NUMPTSTOBUFFER) + { + processSpans( nPts, (QPoint*)FirstPoint, FirstWidth ); + ptsOut = FirstPoint; + width = FirstWidth; + nPts = 0; + } + EVALUATEEDGEEVENODD(pAET, pPrevAET, y) + EVALUATEEDGEEVENODD(pAET, pPrevAET, y) + } + miInsertionSort(&AET); + } + } + else /* default to WindingNumber */ + { + /* + * for each scanline + */ + for (y = ET.ymin+1-edge_t; y < ET.ymax+edge_b; y++) + { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL && y == pSLL->scanline) + { + miloadAET(&AET, pSLL->edgelist); + micomputeWAET(&AET); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + pWETE = pAET; + + /* + * for each active edge + */ + while (pAET) + { + /* + * if the next edge in the active edge table is + * also the next edge in the winding active edge + * table. + */ + if (pWETE == pAET) + { + ptsOut->x = pAET->bres.minor + 1 - edge_l; + ptsOut++->y = y; + *width++ = pAET->nextWETE->bres.minor - pAET->bres.minor - 1 + edge_l + edge_r; + nPts++; + + /* + * send out the buffer + */ + if (nPts == NUMPTSTOBUFFER) + { + processSpans( nPts, (QPoint*)FirstPoint, FirstWidth ); + ptsOut = FirstPoint; + width = FirstWidth; + nPts = 0; + } + + pWETE = pWETE->nextWETE; + while (pWETE != pAET) { + EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) + } + pWETE = pWETE->nextWETE; + } + EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) + } + + /* + * reevaluate the Winding active edge table if we + * just had to resort it or if we just exited an edge. + */ + if (miInsertionSort(&AET) || fixWAET) + { + micomputeWAET(&AET); + fixWAET = 0; + } + } + } + + /* + * Get any spans that we missed by buffering + */ + + + processSpans( nPts, (QPoint*)FirstPoint, FirstWidth ); + free(pETEs); + miFreeStorage(SLLBlock.next); +} +/***** END OF X11-based CODE *****/ + + diff --git a/src/kernel/qpolygonscanner.h b/src/kernel/qpolygonscanner.h new file mode 100644 index 0000000..423e2e1 --- /dev/null +++ b/src/kernel/qpolygonscanner.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Definition of QPolygonScanner class +** +** Created : 000120 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPOLYGONSCANNER_H +#define QPOLYGONSCANNER_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +class QPointArray; +class QPoint; + +class Q_EXPORT QPolygonScanner { +public: + // BIC: fix for 3.0 + void scan( const QPointArray& pa, bool winding, int index=0, int npoints=-1 ); + void scan( const QPointArray& pa, bool winding, int index, int npoints, bool stitchable ); + enum Edge { Left=1, Right=2, Top=4, Bottom=8 }; + void scan( const QPointArray& pa, bool winding, int index, int npoints, Edge edges ); + virtual void processSpans( int n, QPoint* point, int* width )=0; +}; + +#endif // QPOLYGONSCANNER_H diff --git a/src/kernel/qprinter.cpp b/src/kernel/qprinter.cpp new file mode 100644 index 0000000..31e1ec4 --- /dev/null +++ b/src/kernel/qprinter.cpp @@ -0,0 +1,1025 @@ +/********************************************************************** +** +** Implementation of QPrinter class +** +** Created : 941003 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qprinter.h" +#include "qprinter_p.h" + +#ifndef QT_NO_PRINTER + +/*! + \class QPrinter qprinter.h + \brief The QPrinter class is a paint device that paints on a printer. + + \ingroup images + \ingroup graphics + \mainclass + + On Windows it uses the built-in printer drivers. On X11 it + generates postscript and sends that to lpr, lp, or another print + command. + + QPrinter is used in much the same way as QWidget and QPixmap are + used. The big difference is that you must keep track of the pages. + + QPrinter supports a number of settable parameters, most of which + can be changed by the end user when the application calls + QPrinter::setup(). + + The most important parameters are: + \list + \i setOrientation() tells QPrinter which page orientation to use (virtual). + \i setPageSize() tells QPrinter what page size to expect from the + printer. + \i setResolution() tells QPrinter what resolution you wish the + printer to provide (in dpi). + \i setFullPage() tells QPrinter whether you want to deal with the + full page or just with the part the printer can draw on. The + default is FALSE, so that by default you should be able to paint + on (0,0). If TRUE the origin of the coordinate system will be in + the top left corner of the paper and most probably the printer + will not be able to paint something there due to it's physical + margins. + \i setNumCopies() tells QPrinter how many copies of the document + it should print. + \i setMinMax() tells QPrinter and QPrintDialog what the allowed + range for fromPage() and toPage() are. + \endlist + + Except where noted, you can only call the set functions before + setup(), or between QPainter::end() and setup(). (Some may take + effect between setup() and begin(), or between begin() and end(), + but that's strictly undocumented and such behaviour may differ + depending on platform.) + + There are also some settings that the user sets (through the + printer dialog) and that applications are expected to obey: + + \list + + \i pageOrder() tells the application program whether to print + first-page-first or last-page-first. + + \i colorMode() tells the application program whether to print in + color or grayscale. (If you print in color and the printer does + not support color, Qt will try to approximate. The document may + take longer to print, but the quality should not be made visibly + poorer.) + + \i fromPage() and toPage() indicate what pages the application + program should print. + + \i paperSource() tells the application progam which paper source + to print from. + + \endlist + + You can of course call these functions to establish defaults + before you ask the user through QPrinter::setup(). + + Once you start printing, calling newPage() is essential. You will + probably also need to look at the QPaintDeviceMetrics for the + printer (see the \link simple-application.html#printersimple print + function\endlink in the Application walk-through). In previous versions, + paint device metrics were valid only after the QPrinter has been set + up, i.e. after setup() has returned successfully. This is no longer + the case and paint device metrics can be requested safely before set up. + + If you want to abort the print job, abort() will try its best to + stop printing. It may cancel the entire job or just some of it. + + \omit Need a function to setup() without a dialog (i.e. use defaults). + \endomit + + The TrueType font embedding for Qt's postscript driver uses code + by David Chappell of Trinity College Computing Center. + + \legalese + + Copyright 1995, Trinity College Computing Center. + Written by David Chappell. + + Permission to use, copy, modify, and distribute this software and + its documentation for any purpose and without fee is hereby + granted, provided that the above copyright notice appear in all + copies and that both that copyright notice and this permission + notice appear in supporting documentation. This software is + provided "as is" without express or implied warranty. + + TrueType font support. These functions allow PPR to generate + PostScript fonts from Microsoft compatible TrueType font files. + + The functions in this file do most of the work to convert a + TrueType font to a type 3 PostScript font. + + Most of the material in this file is derived from a program called + "ttf2ps" which L. S. Ng posted to the usenet news group + "comp.sources.postscript". The author did not provide a copyright + notice or indicate any restrictions on use. + + Last revised 11 July 1995. + +*/ + +/*! + \enum QPrinter::PrinterMode + + This enum describes the mode the printer should work in. It + basically presets a certain resolution and working mode. + + \value ScreenResolution Sets the resolution of the print device to + the screen resolution. This has the big advantage that the results + obtained when painting on the printer will match more or less + exactly the visible output on the screen. It is the easiest to + use, as font metrics on the screen and on the printer are the + same. This is the default value. ScreenResolution will produce a + lower quality output than HighResolution and should only be used + for drafts. + + \value PrinterResolution Use the physical resolution of the + printer on Windows. On Unix, set the postscript resolution to 72 + dpi. + + \value HighResolution Use printer resolution on windows, set the + resolution of the postscript driver to 600dpi. + + \value Compatible Almost the same as PrinterResolution, but keeps + some peculiarities of the Qt 2.x printer driver. This is useful + for applications ported from Qt 2.x to Qt 3.x. +*/ + +/*! + \enum QPrinter::Orientation + + This enum type (not to be confused with Qt::Orientation) is used + to specify each page's orientation. + + \value Portrait the page's height is greater than its width (the + default). + + \value Landscape the page's width is greater than its height. + + This type interacts with \l QPrinter::PageSize and + QPrinter::setFullPage() to determine the final size of the page + available to the application. +*/ + + +/*! + \enum QPrinter::PageSize + + This enum type specifies what paper size QPrinter should use. + QPrinter does not check that the paper size is available; it just + uses this information, together with QPrinter::Orientation and + QPrinter::setFullPage(), to determine the printable area (see + QPaintDeviceMetrics). + + The defined sizes (with setFullPage(TRUE)) are: + + \value A0 841 x 1189 mm This value is not supported on windows. + \value A1 594 x 841 mm This value is not supported on windows. + \value A2 420 x 594 mm + \value A3 297 x 420 mm + \value A4 210 x 297 mm, 8.26 x 11.7 inches + \value A5 148 x 210 mm + \value A6 105 x 148 mm + \value A7 74 x 105 mm + \value A8 52 x 74 mm + \value A9 37 x 52 mm + \value B0 1030 x 1456 mm + \value B1 728 x 1030 mm + \value B10 32 x 45 mm + \value B2 515 x 728 mm + \value B3 364 x 515 mm + \value B4 257 x 364 mm + \value B5 182 x 257 mm, 7.17 x 10.13 inches + \value B6 128 x 182 mm + \value B7 91 x 128 mm + \value B8 64 x 91 mm + \value B9 45 x 64 mm + \value C5E 163 x 229 mm + \value Comm10E 105 x 241 mm, US Common #10 Envelope + \value DLE 110 x 220 mm + \value Executive 7.5 x 10 inches, 191 x 254 mm + \value Folio 210 x 330 mm + \value Ledger 432 x 279 mm + \value Legal 8.5 x 14 inches, 216 x 356 mm + \value Letter 8.5 x 11 inches, 216 x 279 mm + \value Tabloid 279 x 432 mm + \value Custom + \value NPageSize (internal) + + With setFullPage(FALSE) (the default), the metrics will be a bit + smaller; how much depends on the printer in use. +*/ + + +/*! + \enum QPrinter::PageOrder + + This enum type is used by QPrinter to tell the application program + how to print. + + \value FirstPageFirst the lowest-numbered page should be printed + first. + + \value LastPageFirst the highest-numbered page should be printed + first. +*/ + +/*! + \enum QPrinter::ColorMode + + This enum type is used to indicate whether QPrinter should print + in color or not. + + \value Color print in color if available, otherwise in grayscale. + + \value GrayScale print in grayscale, even on color printers. + Might be a little faster than \c Color. This is the default. +*/ + +/*! + \enum QPrinter::PaperSource + + This enum type specifies what paper source QPrinter is to use. + QPrinter does not check that the paper source is available; it + just uses this information to try and set the paper source. + Whether it will set the paper source depends on whether the + printer has that particular source. + + Note: this is currently only implemented for Windows. + + \value OnlyOne + \value Lower + \value Middle + \value Manual + \value Envelope + \value EnvelopeManual + \value Auto + \value Tractor + \value SmallFormat + \value LargeFormat + \value LargeCapacity + \value Cassette + \value FormSource +*/ + +/*! + \enum QPrinter::PrintRange + + This enum is used to specify which print range the application + should use to print. + + \value AllPages All pages should be printed + \value Selection Only the selection should be printed. + \value PageRange From page, to page option. + + \sa setPrintRange(), printRange() +*/ + +/*! + \enum QPrinter::PrinterOption + + This enum describes various printer options that appear in the + printer setup dialog. It is used to enable and disable these + options in the setup dialog. + + \value PrintToFile Describes if print to file should be enabled. + \value PrintSelection Describes if printing selections should be enabled. + \value PrintPageRange Describes if printing page ranges (from, to) should + be enabled + + \sa setOptionEnabled(), isOptionEnabled() +*/ + + +/*! + \fn QString QPrinter::printerName() const + + Returns the printer name. This value is initially set to the name + of the default printer. + + \sa setPrinterName() +*/ + +/*! + \fn bool QPrinter::outputToFile() const + + Returns TRUE if the output should be written to a file, or FALSE + if the output should be sent directly to the printer. The default + setting is FALSE. + + This function is currently only supported under X11 and Mac OS X. + + \sa setOutputToFile(), setOutputFileName() +*/ + +/*! + Specifies whether the output should be written to a file or sent + directly to the printer. + + Will output to a file if \a enable is TRUE, or will output + directly to the printer if \a enable is FALSE. + + This function is currently only supported under X11 and Mac OS X. + + \sa outputToFile(), setOutputFileName() +*/ + +void QPrinter::setOutputToFile( bool enable ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPrinter::setOutputToFile: Cannot do this during printing" ); +#endif + return; + } + output_file = enable; +} + + +/*! + \fn QString QPrinter::outputFileName() const + + Returns the name of the output file. There is no default file + name. + + \sa setOutputFileName(), setOutputToFile() +*/ + +/*! + Sets the name of the output file to \a fileName. + + Setting a null or empty name (0 or "") disables output to a file, + i.e. calls setOutputToFile(FALSE). Setting a non-empty name + enables output to a file, i.e. calls setOutputToFile(TRUE). + + This function is currently only supported under X11. + + \sa outputFileName(), setOutputToFile() +*/ + +void QPrinter::setOutputFileName( const QString &fileName ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning("QPrinter::setOutputFileName: Cannot do this during printing"); +#endif + return; + } + output_filename = fileName; + output_file = !output_filename.isEmpty(); +} + + +/*! + \fn QString QPrinter::printProgram() const + + Returns the name of the program that sends the print output to the + printer. + + The default is to return a null string; meaning that QPrinter will + try to be smart in a system-dependent way. On X11 only, you can + set it to something different to use a specific print program. + + On Windows, this function returns the name of the printer device + driver. + + \sa setPrintProgram() setPrinterSelectionOption() +*/ + +/*! + Sets the name of the program that should do the print job to \a + printProg. + + On X11, this function sets the program to call with the PostScript + output. On other platforms, it has no effect. + + \sa printProgram() +*/ + +void QPrinter::setPrintProgram( const QString &printProg ) +{ + print_prog = printProg; +} + + +/*! + \fn QString QPrinter::docName() const + + Returns the document name. + + \sa setDocName() +*/ + +/*! + Sets the document name to \a name. +*/ + +void QPrinter::setDocName( const QString &name ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPrinter::setDocName: Cannot do this during printing" ); +#endif + return; + } + doc_name = name; +} + + +/*! + \fn QString QPrinter::creator() const + + Returns the name of the application that created the document. + + \sa setCreator() +*/ + +/*! + Sets the name of the application that created the document to \a + creator. + + This function is only applicable to the X11 version of Qt. If no + creator name is specified, the creator will be set to "Qt" + followed by some version number. + + \sa creator() +*/ + +void QPrinter::setCreator( const QString &creator ) +{ + creator_name = creator; +} + + +/*! + \fn Orientation QPrinter::orientation() const + + Returns the orientation setting. The default value is \c + QPrinter::Portrait. + + \sa setOrientation() +*/ + +/*! + Sets the print orientation to \a orientation. + + The orientation can be either \c QPrinter::Portrait or \c + QPrinter::Landscape. + + The printer driver reads this setting and prints using the + specified orientation. On Windows this setting won't take effect + until the printer dialog is shown (using QPrinter::setup()). + + Windows only! This option can be changed while printing and will + take effect from the next call to newPage() + + \sa orientation() +*/ + +void QPrinter::setOrientation( Orientation orientation ) +{ + orient = orientation; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + \fn PageSize QPrinter::pageSize() const + + Returns the printer page size. The default value is system-dependent. + + \sa setPageSize() +*/ + + +/*! + Sets the printer page size to \a newPageSize if that size is + supported. The result if undefined if \a newPageSize is not + supported. + + The default page size is system-dependent. + + This function is useful mostly for setting a default value that + the user can override in the print dialog when you call setup(). + + \sa pageSize() PageSize setFullPage() setResolution() +*/ + +void QPrinter::setPageSize( PageSize newPageSize ) +{ + if ( newPageSize > NPageSize ) { +#if defined(QT_CHECK_STATE) + qWarning("QPrinter::SetPageSize: illegal page size %d", newPageSize ); +#endif + return; + } + page_size = newPageSize; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + +/*! + Sets the page order to \a newPageOrder. + + The page order can be \c QPrinter::FirstPageFirst or \c + QPrinter::LastPageFirst. The application programmer is responsible + for reading the page order and printing accordingly. + + This function is useful mostly for setting a default value that + the user can override in the print dialog when you call setup(). + + \bug This value is not kept in sync with the Windows or Mac OS X printer + dialogs. +*/ + +void QPrinter::setPageOrder( PageOrder newPageOrder ) +{ + page_order = newPageOrder; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + Returns the current page order. + + The default page order is \c FirstPageFirst. + + \bug This value is not kept in sync with the Windows or Mac OS X printer + dialogs. +*/ + +QPrinter::PageOrder QPrinter::pageOrder() const +{ + return page_order; +} + + +/*! + Sets the printer's color mode to \a newColorMode, which can be + either \c Color or \c GrayScale (the default). + + \sa colorMode() +*/ + +void QPrinter::setColorMode( ColorMode newColorMode ) +{ + color_mode = newColorMode; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + Returns the current color mode. The default color mode is \c + Color. + + \sa setColorMode() +*/ + +QPrinter::ColorMode QPrinter::colorMode() const +{ + return color_mode; +} + + +/*! + \fn int QPrinter::fromPage() const + + Returns the from-page setting. The default value is 0. + + If fromPage() and toPage() both return 0 this signifies 'print the + whole document'. + + The programmer is responsible for reading this setting and + printing accordingly. + + \sa setFromTo(), toPage() +*/ + +/*! + \fn int QPrinter::toPage() const + + Returns the to-page setting. The default value is 0. + + If fromPage() and toPage() both return 0 this signifies 'print the + whole document'. + + The programmer is responsible for reading this setting and + printing accordingly. + + \sa setFromTo(), fromPage() +*/ + +/*! + Sets the from-page and to-page settings to \a fromPage and \a + toPage respectively. + + The from-page and to-page settings specify what pages to print. + + If fromPage() and toPage() both return 0 this signifies 'print the + whole document'. + + This function is useful mostly to set a default value that the + user can override in the print dialog when you call setup(). + + \sa fromPage(), toPage(), setMinMax(), setup() +*/ + +void QPrinter::setFromTo( int fromPage, int toPage ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPrinter::setFromTo: Cannot do this during printing" ); +#endif + return; + } + from_pg = fromPage; + to_pg = toPage; +} + + +/*! + \fn int QPrinter::minPage() const + + Returns the min-page setting, i.e. the lowest page number a user + is allowed to choose. The default value is 0. + + \sa maxPage(), setMinMax() setFromTo() +*/ + +/*! + \fn int QPrinter::maxPage() const + + Returns the max-page setting. A user can't choose a higher page + number than maxPage() when they select a print range. The default + value is 0. + + \sa minPage(), setMinMax() setFromTo() +*/ + +/*! + Sets the min-page and max-page settings to \a minPage and \a + maxPage respectively. + + The min-page and max-page restrict the from-page and to-page + settings. When the printer setup dialog appears, the user cannot + select a from page or a to page that are outside the range + specified by min and max pages. + + \sa minPage(), maxPage(), setFromTo(), setup() +*/ + +void QPrinter::setMinMax( int minPage, int maxPage ) +{ + min_pg = minPage; + max_pg = maxPage; + if ( from_pg == 0 || from_pg < minPage ) + from_pg = minPage; + if ( to_pg == 0 || to_pg > maxPage ) + to_pg = maxPage; +} + + +/*! + \fn int QPrinter::numCopies() const + + Returns the number of copies to be printed. The default value is 1. + + This value will return the number of times the application is + required to print in order to match the number specified in the + printer setup dialog. This has been done since some printer + drivers are not capable of buffering up the copies and the + application in those cases have to make an explicit call to the + print code for each copy. + + \sa setNumCopies() +*/ + +/*! + \fn bool QPrinter::collateCopiesEnabled() const + + \internal + + Returns TRUE if the application should provide the user with the + option of choosing a collated printout; otherwise returns FALSE. + + Collation means that each page is printed in order, i.e. print the + first page, then the second page, then the third page and so on, and + then repeat this sequence for as many copies as have been requested. + If you don't collate you get several copies of the first page, then + several copies of the second page, then several copies of the third + page, and so on. + + \sa setCollateCopiesEnabled() setCollateCopies() collateCopies() +*/ + +/*! + \fn void QPrinter::setCollateCopiesEnabled(bool enable) + + \internal + + If \a enable is TRUE (the default) the user is given the choice of + whether to print out multiple copies collated in the print dialog. + If \a enable is FALSE, then collateCopies() will be ignored. + + Collation means that each page is printed in order, i.e. print the + first page, then the second page, then the third page and so on, and + then repeat this sequence for as many copies as have been requested. + If you don't collate you get several copies of the first page, then + several copies of the second page, then several copies of the third + page, and so on. + + \sa collateCopiesEnabled() setCollateCopies() collateCopies() +*/ + +/*! + \fn bool QPrinter::collateCopies() const + + \internal + + Returns TRUE if collation is turned on when multiple copies is selected. + Returns FALSE if it is turned off when multiple copies is selected. + + \sa collateCopiesEnabled() setCollateCopiesEnabled() setCollateCopies() +*/ + +/*! + \internal + + Sets the default value for collation checkbox when the print dialog appears. + If \a on is TRUE, it will enable setCollateCopiesEnabled(). + The default value is FALSE. This value will be changed by what the + user presses in the print dialog. + + \sa collateCopiesEnabled() setCollateCopiesEnabled() collateCopies() +*/ + +void QPrinter::setCollateCopies(bool on) +{ + if (!collateCopiesEnabled() && on) + setCollateCopiesEnabled(on); + usercolcopies = on; +} + +/*! + Sets the number of copies to be printed to \a numCopies. + + The printer driver reads this setting and prints the specified + number of copies. + + \sa numCopies(), setup() +*/ + +void QPrinter::setNumCopies( int numCopies ) +{ + ncopies = numCopies; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + Returns the printer options selection string. This is useful only + if the print command has been explicitly set. + + The default value (a null string) implies that the printer should + be selected in a system-dependent manner. + + Any other value implies that the given value should be used. + + \sa setPrinterSelectionOption() +*/ + +QString QPrinter::printerSelectionOption() const +{ + return option_string; +} + + +/*! + Sets the printer to use \a option to select the printer. \a option + is null by default (which implies that Qt should be smart enough + to guess correctly), but it can be set to other values to use a + specific printer selection option. + + If the printer selection option is changed while the printer is + active, the current print job may or may not be affected. + + \sa printerSelectionOption() +*/ + +void QPrinter::setPrinterSelectionOption( const QString & option ) +{ + option_string = option; +} + + +/*! + Sets QPrinter to have the origin of the coordinate system at the + top-left corner of the paper if \a fp is TRUE, or where it thinks + the top-left corner of the printable area is if \a fp is FALSE. + + The default is FALSE. You can (probably) print on (0,0), and + QPaintDeviceMetrics will report something smaller than the size + indicated by PageSize. (Note that QPrinter may be wrong on Unix + systems - it does not have perfect knowledge of the physical + printer.) + + If you set \a fp to TRUE, QPaintDeviceMetrics will report the + exact same size as indicated by \c PageSize, but you cannot print + on all of that - you must take care of the output margins + yourself. + + \sa PageSize setPageSize() QPaintDeviceMetrics fullPage() +*/ + +void QPrinter::setFullPage( bool fp ) +{ + to_edge = fp; +} + + +/*! + Returns TRUE if the origin of the printer's coordinate system is + at the corner of the sheet and FALSE if it is at the edge of the + printable area. + + See setFullPage() for details and caveats. + + \sa setFullPage() PageSize QPaintDeviceMetrics +*/ + +bool QPrinter::fullPage() const +{ + return to_edge; +} + + +/*! + Requests that the printer prints at \a dpi or as near to \a dpi as + possible. + + This setting affects the coordinate system as returned by, for + example, QPaintDeviceMetrics and QPainter::viewport(). + + The value depends on the \c PrintingMode used in the QPrinter + constructor. By default, the dpi value of the screen is used. + + This function must be called before setup() to have an effect on + all platforms. + + \sa resolution() setPageSize() +*/ + +void QPrinter::setResolution( int dpi ) +{ + res = dpi; + res_set = TRUE; +} + + +/*! + Returns the current assumed resolution of the printer, as set by + setResolution() or by the printer subsystem. + + \sa setResolution() +*/ + +int QPrinter::resolution() const +{ + return res; +} + +/*! + Sets the paper source setting to \a source. + + Windows only! This option can be changed while printing and will + take effect from the next call to newPage() + + \sa paperSource() +*/ + +void QPrinter::setPaperSource( PaperSource source ) +{ + paper_source = source; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + +/*! + Returns the currently set paper source of the printer. + + \sa setPaperSource() +*/ + +QPrinter::PaperSource QPrinter::paperSource() const +{ + return paper_source; +} + +/*! + Sets the default selected page range to be used when the print setup + dialog is opened to \a range. If the PageRange specified by \a range is + currently disabled the function does nothing. + + \sa printRange() +*/ +void QPrinter::setPrintRange( PrintRange range ) +{ + if( range != AllPages ) + if( range == Selection + && !isOptionEnabled( PrintSelection ) ) + setOptionEnabled( PrintSelection, TRUE ); + else if( range == PageRange + && !isOptionEnabled( PrintPageRange ) ) + setOptionEnabled( PrintPageRange, TRUE ); + d->printRange = range; +} + +/*! + Returns the PageRange of the QPrinter. After the print setup dialog + has been opened, this function returns the value selected by the user. + + \sa setPrintRange() +*/ +QPrinter::PrintRange QPrinter::printRange() const +{ + return d->printRange; +} + +/*! + Enables the printer option with the identifier \a option if \a + enable is TRUE, and disables option \a option if \a enable is FALSE. + + \sa isOptionEnabled() +*/ +void QPrinter::setOptionEnabled( PrinterOption option, bool enable ) +{ + if( enable ) { + d->printerOptions |= ( 1 << option ); + if( ( option == PrintPageRange ) && min_pg==0 && max_pg==0 ) + max_pg = 9999; + } else { + d->printerOptions &= ( ~( 1 << option ) ); + } +} + +/*! + Returns TRUE if the printer option with identifier \a option is enabled; + otherwise returns FALSE. + + \sa setOptionEnabled() + */ +bool QPrinter::isOptionEnabled( PrinterOption option ) +{ + return d->printerOptions & ( 1 << option ); +} +#endif // QT_NO_PRINTER + diff --git a/src/kernel/qprinter.h b/src/kernel/qprinter.h new file mode 100644 index 0000000..ef171e3 --- /dev/null +++ b/src/kernel/qprinter.h @@ -0,0 +1,284 @@ +/********************************************************************** +** +** Definition of QPrinter class +** +** Created : 940927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPRINTER_H +#define QPRINTER_H + +#ifndef QT_H +#include "qpaintdevice.h" +#include "qstring.h" +#include "qstringlist.h" +#endif // QT_H + +#ifndef QT_NO_PRINTER + +#if defined(B0) +#undef B0 // Terminal hang-up. We assume that you do not want that. +#endif + +class QPrinterPrivate; + +class Q_EXPORT QPrinter : public QPaintDevice +{ +public: + enum PrinterMode { ScreenResolution, PrinterResolution, HighResolution, Compatible }; + + QPrinter( PrinterMode mode = ScreenResolution ); + ~QPrinter(); + + enum Orientation { Portrait, Landscape }; + + enum PageSize { A4, B5, Letter, Legal, Executive, + A0, A1, A2, A3, A5, A6, A7, A8, A9, B0, B1, + B10, B2, B3, B4, B6, B7, B8, B9, C5E, Comm10E, + DLE, Folio, Ledger, Tabloid, Custom, NPageSize = Custom }; + + enum PageOrder { FirstPageFirst, LastPageFirst }; + + enum ColorMode { GrayScale, Color }; + + enum PaperSource { OnlyOne, Lower, Middle, Manual, Envelope, + EnvelopeManual, Auto, Tractor, SmallFormat, + LargeFormat, LargeCapacity, Cassette, FormSource }; + + enum PrintRange { AllPages, + Selection, + PageRange }; + + enum PrinterOption { PrintToFile, + PrintSelection, + PrintPageRange }; + + QString printerName() const; + virtual void setPrinterName( const QString &); + bool outputToFile() const; + virtual void setOutputToFile( bool ); + QString outputFileName()const; + virtual void setOutputFileName( const QString &); + + QString printProgram() const; + virtual void setPrintProgram( const QString &); + + QString printerSelectionOption() const; + virtual void setPrinterSelectionOption( const QString & ); + + QString docName() const; + virtual void setDocName( const QString &); + QString creator() const; + virtual void setCreator( const QString &); + + Orientation orientation() const; + virtual void setOrientation( Orientation ); + PageSize pageSize() const; + virtual void setPageSize( PageSize ); +#ifdef Q_WS_WIN + void setWinPageSize( short winPageSize ); + short winPageSize() const; +#endif +#ifdef Q_WS_MAC + bool printSetup(); + bool pageSetup(); +#endif + virtual void setPageOrder( PageOrder ); + PageOrder pageOrder() const; + + void setResolution( int ); + int resolution() const; + + virtual void setColorMode( ColorMode ); + ColorMode colorMode() const; + + virtual void setFullPage( bool ); + bool fullPage() const; + QSize margins() const; + void setMargins( uint top, uint left, uint bottom, uint right ); + void margins( uint *top, uint *left, uint *bottom, uint *right ) const; + + int fromPage() const; + int toPage() const; + virtual void setFromTo( int fromPage, int toPage ); + int minPage() const; + int maxPage() const; + virtual void setMinMax( int minPage, int maxPage ); + int numCopies() const; + virtual void setNumCopies( int ); + + bool collateCopiesEnabled() const; + void setCollateCopiesEnabled(bool ); + + bool collateCopies() const; + void setCollateCopies( bool ); + + PrintRange printRange() const; + void setPrintRange( PrintRange range ); + + bool newPage(); + bool abort(); + bool aborted() const; + + bool setup( QWidget *parent = 0 ); + + PaperSource paperSource() const; + virtual void setPaperSource( PaperSource ); + + void setOptionEnabled( PrinterOption, bool enable ); + bool isOptionEnabled( PrinterOption ); + +protected: + bool cmd( int, QPainter *, QPDevCmdParam * ); + int metric( int ) const; + +#if defined(Q_WS_WIN) + virtual void setActive(); + virtual void setIdle(); +#endif + +private: +#if defined(Q_WS_X11) || defined(Q_WS_QWS) + QPaintDevice *pdrv; + int pid; +#endif +#if defined(Q_WS_MAC) + friend class QPrinterPrivate; + PMPageFormat pformat; + PMPrintSettings psettings; + PMPrintSession psession; + bool prepare(PMPrintSettings *); + bool prepare(PMPageFormat *); + void interpret(PMPrintSettings *); + void interpret(PMPageFormat *); +#endif +#if defined(Q_WS_WIN) + void readPdlg( void* ); + void readPdlgA( void* ); + void writeDevmode( Qt::HANDLE ); + void writeDevmodeA( Qt::HANDLE ); + void reinit(); + + bool viewOffsetDone; + QPainter* painter; + Qt::HANDLE hdevmode; + Qt::HANDLE hdevnames; +#endif + + int state; + QString printer_name; + QString option_string; + QString output_filename; + bool output_file; + QString print_prog; + QString doc_name; + QString creator_name; + + PageSize page_size; + PaperSource paper_source; + PageOrder page_order; + ColorMode color_mode; + Orientation orient; + uint to_edge : 1; + uint appcolcopies : 1; + uint usercolcopies : 1; + uint res_set : 1; + short from_pg, to_pg; + short min_pg, max_pg; + short ncopies; + int res; + QPrinterPrivate *d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QPrinter( const QPrinter & ); + QPrinter &operator=( const QPrinter & ); +#endif +}; + + +inline QString QPrinter::printerName() const +{ return printer_name; } + +inline bool QPrinter::outputToFile() const +{ return output_file; } + +inline QString QPrinter::outputFileName() const +{ return output_filename; } + +inline QString QPrinter::printProgram() const +{ return print_prog; } + +inline QString QPrinter::docName() const +{ return doc_name; } + +inline QString QPrinter::creator() const +{ return creator_name; } + +inline QPrinter::PageSize QPrinter::pageSize() const +{ return page_size; } + +inline QPrinter::Orientation QPrinter::orientation() const +{ return orient; } + +inline int QPrinter::fromPage() const +{ return from_pg; } + +inline int QPrinter::toPage() const +{ return to_pg; } + +inline int QPrinter::minPage() const +{ return min_pg; } + +inline int QPrinter::maxPage() const +{ return max_pg; } + +inline int QPrinter::numCopies() const +{ return ncopies; } + +inline bool QPrinter::collateCopiesEnabled() const +{ return appcolcopies; } + +inline void QPrinter::setCollateCopiesEnabled(bool v) +{ appcolcopies = v; } + +inline bool QPrinter::collateCopies() const +{ return usercolcopies; } + + +#endif // QT_NO_PRINTER + +#endif // QPRINTER_H diff --git a/src/kernel/qprinter_p.h b/src/kernel/qprinter_p.h new file mode 100644 index 0000000..8a16e47 --- /dev/null +++ b/src/kernel/qprinter_p.h @@ -0,0 +1,59 @@ +/********************************************************************** +** +** Definition of QPrinter class +** +** Created : 940927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPRINTER_P_H +#define QPRINTER_P_H +#ifndef QT_NO_PRINTER + +#ifndef QT_H +#include <qshared.h> +#include <qstring.h> +#include <qsize.h> +#endif // QT_H + +class QPrinterPrivate +{ +public: + Q_UINT32 printerOptions; + QPrinter::PrintRange printRange; +}; + +#endif +#endif diff --git a/src/kernel/qprinter_unix.cpp b/src/kernel/qprinter_unix.cpp new file mode 100644 index 0000000..5dcfa93 --- /dev/null +++ b/src/kernel/qprinter_unix.cpp @@ -0,0 +1,672 @@ +/**************************************************************************** +** +** Implementation of QPrinter class for Unix +** +** Created : 950810 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +static inline int qt_open(const char *pathname, int flags, mode_t mode) +{ return ::open(pathname, flags, mode); } +#if defined(open) +# undef open +#endif + +#include "qprinter.h" + +#ifndef QT_NO_PRINTER + +#include "qpaintdevicemetrics.h" +#include "qpsprinter_p.h" +#include "qprintdialog.h" +#include "qapplication.h" +#include "qprinter_p.h" + +#include <unistd.h> // For ::sleep() +#include <stdlib.h> + + +// NOT REVISED + + +class QPrinterUnixPrivate : public QPrinterPrivate +{ +public: + bool marginsSpecified; + uint topMargin; + uint leftMargin; + uint bottomMargin; + uint rightMargin; +}; + +#define D ( (QPrinterUnixPrivate*) d ) + +/***************************************************************************** + QPrinter member functions + *****************************************************************************/ + +// QPrinter states + +#define PST_IDLE 0 +#define PST_ACTIVE 1 +#define PST_ERROR 2 +#define PST_ABORTED 3 + +// Default values for QPrinter members + +struct PrinterDefaults { + QString printerName; + bool outputToFile; + QString outputFileName; + QPrinter::Orientation orientation; + QPrinter::PageSize pageSize; + QPrinter::PageOrder pageOrder; + QPrinter::ColorMode colorMode; + int numCopies; +}; + +static PrinterDefaults * globalPrinterDefaults = 0; + +/*! + Constructs a printer paint device with mode \a m. + + \sa QPrinter::PrinterMode +*/ + +QPrinter::QPrinter( PrinterMode m ) + : QPaintDevice( QInternal::Printer | QInternal::ExternalDevice ) +{ + pdrv = 0; + pid = 0; + orient = Portrait; + page_size = A4; + page_order = FirstPageFirst; + color_mode = GrayScale; + ncopies = 1; + printer_name = getenv("PRINTER"); + from_pg = to_pg = min_pg = max_pg = 0; + state = PST_IDLE; + output_file = FALSE; + to_edge = FALSE; + paper_source = OnlyOne; + switch ( m ) { + case ScreenResolution: +#ifdef Q_WS_QWS + res = 72; +#else + res = QPaintDevice::x11AppDpiY(); +#endif + break; + case Compatible: + case PrinterResolution: + res = 72; + break; + case HighResolution: + res = 600; + } + + d = new QPrinterUnixPrivate; + D->marginsSpecified = FALSE; + d->printerOptions = 0; + setOptionEnabled( PrintToFile, TRUE ); + setOptionEnabled( PrintPageRange, TRUE ); + setPrintRange( AllPages ); +} + +/*! + Destroys the printer paint device and cleans up. +*/ + +QPrinter::~QPrinter() +{ + delete pdrv; + if ( pid ) { + (void)::kill( pid, 6 ); + (void)::wait( 0 ); + pid = 0; + } + delete d; +} + + +/*! + Advances to a new page on the printer. Returns TRUE if successful; + otherwise returns FALSE. +*/ + +bool QPrinter::newPage() +{ + if ( state == PST_ACTIVE && pdrv ) + return ((QPSPrinter*)pdrv)->cmd( QPSPrinter::NewPage, 0, 0 ); + return FALSE; +} + + +/*! + Aborts the print job. Returns TRUE if successful; otherwise + returns FALSE. + + \sa aborted() +*/ + +bool QPrinter::abort() +{ + if ( state == PST_ACTIVE && pdrv ) { + ((QPSPrinter*)pdrv)->cmd( QPSPrinter::AbortPrinting, 0, 0 ); + state = PST_ABORTED; + if ( pid ) { + (void)::kill( pid, 6 ); + (void)::wait( 0 ); + pid = 0; + } + } + return state == PST_ABORTED; +} + +/*! + Returns TRUE if the print job was aborted; otherwise returns + FALSE. + + \sa abort() +*/ + +bool QPrinter::aborted() const +{ + return state == PST_ABORTED; +} + +/*! + Sets the printer name to \a name. + + The default printer will be used if no printer name is set. + + Under X11, the \c PRINTER environment variable defines the default + printer. Under any other window system, the window system defines + the default printer. + + \sa printerName() +*/ + +void QPrinter::setPrinterName( const QString &name ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "QPrinter::setPrinterName: Cannot do this during printing" ); +#endif + return; + } + printer_name = name; +} + +static void deleteGlobalPrinterDefaults() +{ + delete globalPrinterDefaults; + globalPrinterDefaults = 0; +} + +/*! + Opens a printer setup dialog, with parent \a parent, and asks the + user to specify which printer they wish to use and what settings + it should have. + + Returns TRUE if the user pressed "OK" to print, or FALSE if the + user canceled the operation. +*/ + +bool QPrinter::setup( QWidget * parent ) +{ +#ifndef QT_NO_PRINTDIALOG + bool result = QPrintDialog::getPrinterSetup( this, parent ); +#else + bool result = FALSE; +#endif + if ( result ) { + if ( !globalPrinterDefaults ) { + globalPrinterDefaults = new PrinterDefaults; + qAddPostRoutine( deleteGlobalPrinterDefaults ); + } + globalPrinterDefaults->printerName = printerName(); + globalPrinterDefaults->outputToFile = outputToFile(); + globalPrinterDefaults->outputFileName = outputFileName(); + globalPrinterDefaults->orientation = orientation(); + globalPrinterDefaults->pageSize = pageSize(); + globalPrinterDefaults->pageOrder = pageOrder(); + globalPrinterDefaults->colorMode = colorMode(); + } + return result; +} + +static void closeAllOpenFds() +{ + // hack time... getting the maximum number of open + // files, if possible. if not we assume it's the + // larger of 256 and the fd we got + int i; +#if defined(Q_OS_OS2EMX) + LONG req_count = 0; + ULONG rc, handle_count; + rc = DosSetRelMaxFH (&req_count, &handle_count); + /* if (rc != NO_ERROR) ... */ + i = (int)handle_count; +#elif defined(_SC_OPEN_MAX) + i = (int)sysconf( _SC_OPEN_MAX ); +#elif defined(_POSIX_OPEN_MAX) + i = (int)_POSIX_OPEN_MAX; +#elif defined(OPEN_MAX) + i = (int)OPEN_MAX; +#else + i = QMAX( 256, fds[0] ); +#endif // Q_OS_OS2EMX // ways-to-set i + while( --i > 0 ) + ::close( i ); +} + + + +static const char * const psToStr[QPrinter::NPageSize+1] = +{ "A4", "B5", "Letter", "Legal", "Executive", + "A0", "A1", "A2", "A3", "A5", "A6", "A7", "A8", "A9", "B0", "B1", + "B10", "B2", "B3", "B4", "B6", "B7", "B8", "B9", "C5E", "Comm10E", + "DLE", "Folio", "Ledger", "Tabloid", 0 +}; + + +/*! + \internal + Handles painter commands to the printer. +*/ + +bool QPrinter::cmd( int c, QPainter *paint, QPDevCmdParam *p ) +{ + if ( c == PdcBegin ) { + if ( state == PST_IDLE ) { + if ( output_file ) { + int fd = 0; + fd = qt_open( output_filename.local8Bit(), + O_CREAT | O_NOCTTY | O_TRUNC | O_WRONLY, + 0666 ); + if ( fd >= 0 ) { + pdrv = new QPSPrinter( this, fd ); + state = PST_ACTIVE; + } + } else { + QString pr; + if ( printer_name ) + pr = printer_name; + QApplication::flushX(); + int fds[2]; + if ( pipe( fds ) != 0 ) { + qWarning( "QPSPrinter: could not open pipe to print" ); + state = PST_ERROR; + return FALSE; + } + +// ### shouldn't we use QProcess here???? +#if 0 && defined(Q_OS_OS2EMX) + // this code is still not used, and maybe it's not + // usable either, any more. if you want to use it, + // you may need to fix it first. + + // old comment: + + // this code is usable but not in use. spawn() is + // preferable to fork()/exec() for very large + // programs. if fork()/exec() is a problem and you + // use OS/2, remove '0 && ' from the #if. + int tmp; + tmp = dup(0); + dup2( fds[0], 0 ); + ::close( fds[0] ); + fcntl(tmp, F_SETFD, FD_CLOEXEC); + fcntl(fds[1], F_SETFD, FD_CLOEXEC); + if ( option_string ) + pr.prepend( option_string ); + else + pr.prepend( "-P" ); // ### + if ( spawnlp(P_NOWAIT,print_prog.data(), print_prog.data(), + pr.data(), output->name(), 0) == -1 ) { + ; // couldn't exec, ignored + } + dup2( tmp, 0 ); + ::close( tmp ); + pdrv = new QPSPrinter( this, fds[1] ); + state = PST_ACTIVE; +#else + pid = fork(); + if ( pid == 0 ) { // child process + // if possible, exit quickly, so the actual lp/lpr + // becomes a child of init, and ::waitpid() is + // guaranteed not to wait. + if ( fork() > 0 ) { + closeAllOpenFds(); + + // try to replace this process with "true" - this prevents + // global destructors from being called (that could possibly + // do wrong things to the parent process) + (void)execlp("true", "true", (char *)0); + (void)execl("/bin/true", "true", (char *)0); + (void)execl("/usr/bin/true", "true", (char *)0); + ::exit( 0 ); + } + dup2( fds[0], 0 ); + + closeAllOpenFds(); + + if ( print_prog ) { + if ( option_string ) + pr.prepend( option_string ); + else + pr.prepend( QString::fromLatin1( "-P" ) ); + (void)execlp( print_prog.ascii(), print_prog.ascii(), + pr.ascii(), (char *)0 ); + } else { + // if no print program has been specified, be smart + // about the option string too. + QStringList lprhack; + QStringList lphack; + QString media; + if ( pr || option_string ) { + if ( option_string ) { + lprhack = QStringList::split(QChar(' '), option_string); + lphack = lprhack; + } else { + lprhack.append( QString::fromLatin1( "-P" ) ); + lphack.append( QString::fromLatin1( "-d" ) ); + } + lprhack.append(pr); + lphack.append(pr); + } + char ** lpargs = new char *[lphack.size()+6]; + lpargs[0] = "lp"; + uint i; + for (i = 0; i < lphack.size(); ++i) + lpargs[i+1] = (char *)lphack[i].ascii(); +#ifndef Q_OS_OSF + if (psToStr[page_size]) { + lpargs[++i] = "-o"; + lpargs[++i] = (char *)psToStr[page_size]; + lpargs[++i] = "-o"; + media = "media="; + media += psToStr[page_size]; + lpargs[++i] = (char *)media.ascii(); + } +#endif + lpargs[++i] = 0; + char **lprargs = new char *[lprhack.size()+1]; + lprargs[0] = "lpr"; + for (uint x = 0; x < lprhack.size(); ++x) + lprargs[x+1] = (char *)lprhack[x].ascii(); + lprargs[lprhack.size() + 1] = 0; + (void)execvp( "lp", lpargs ); + (void)execvp( "lpr", lprargs ); + (void)execv( "/bin/lp", lpargs); + (void)execv( "/bin/lpr", lprargs); + (void)execv( "/usr/bin/lp", lpargs); + (void)execv( "/usr/bin/lpr", lprargs); + } + // if we couldn't exec anything, close the fd, + // wait for a second so the parent process (the + // child of the GUI process) has exited. then + // exit. + ::close( 0 ); + (void)::sleep( 1 ); + ::exit( 0 ); + } else { // parent process + ::close( fds[0] ); + pdrv = new QPSPrinter( this, fds[1] ); + state = PST_ACTIVE; + } +#endif // else part of Q_OS_OS2EMX + } + if ( state == PST_ACTIVE && pdrv ) + return ((QPSPrinter*)pdrv)->cmd( c, paint, p ); + } else { + // ignore it? I don't know + } + } else { + bool r = FALSE; + if ( state == PST_ACTIVE && pdrv ) { + r = ((QPSPrinter*)pdrv)->cmd( c, paint, p ); + if ( c == PdcEnd ) { + state = PST_IDLE; + delete pdrv; + pdrv = 0; + if ( pid ) { + (void)::waitpid( pid, 0, 0 ); + pid = 0; + } + } + } else if ( state == PST_ABORTED && c == PdcEnd ) + state = PST_IDLE; + return r; + } + return TRUE; +} + + +#define MM(n) int((n * 720 + 127) / 254) +#define IN(n) int(n * 72) + +struct PaperSize { + int width, height; +}; + +static const PaperSize paperSizes[QPrinter::NPageSize] = +{ + { MM(210), MM(297) }, // A4 + { MM(176), MM(250) }, // B5 + { IN(8.5), IN(11) }, // Letter + { IN(8.5), IN(14) }, // Legal + { IN(7.5), IN(10) }, // Executive + { MM(841), MM(1189) }, // A0 + { MM(594), MM(841) }, // A1 + { MM(420), MM(594) }, // A2 + { MM(297), MM(420) }, // A3 + { MM(148), MM(210) }, // A5 + { MM(105), MM(148) }, // A6 + { MM(74), MM(105)}, // A7 + { MM(52), MM(74) }, // A8 + { MM(37), MM(52) }, // A9 + { MM(1000), MM(1414) }, // B0 + { MM(707), MM(1000) }, // B1 + { MM(31), MM(44) }, // B10 + { MM(500), MM(707) }, // B2 + { MM(353), MM(500) }, // B3 + { MM(250), MM(353) }, // B4 + { MM(125), MM(176) }, // B6 + { MM(88), MM(125) }, // B7 + { MM(62), MM(88) }, // B8 + { MM(44), MM(62) }, // B9 + { MM(162), MM(229) }, // C5E + { IN(4.125), IN(9.5) }, // Comm10E + { MM(110), MM(220) }, // DLE + { IN(8.5), IN(13) }, // Folio + { IN(17), IN(11) }, // Ledger + { IN(11), IN(17) } // Tabloid +}; + +/*! + Internal implementation of the virtual QPaintDevice::metric() function. + + Use the QPaintDeviceMetrics class instead. + + \internal + Hard coded return values for PostScript under X. +*/ + +int QPrinter::metric( int m ) const +{ + int val; + PageSize s = pageSize(); +#if defined(QT_CHECK_RANGE) + Q_ASSERT( (uint)s < (uint)NPageSize ); +#endif + switch ( m ) { + case QPaintDeviceMetrics::PdmWidth: + val = orient == Portrait ? paperSizes[s].width : paperSizes[s].height; + if ( res != 72 ) + val = (val * res + 36) / 72; + if ( !fullPage() ) { + if ( D->marginsSpecified ) + val -= D->leftMargin + D->rightMargin; + else + val -= 2*margins().width(); + } + break; + case QPaintDeviceMetrics::PdmHeight: + val = orient == Portrait ? paperSizes[s].height : paperSizes[s].width; + if ( res != 72 ) + val = (val * res + 36) / 72; + if ( !fullPage() ) { + if ( D->marginsSpecified ) + val -= D->topMargin + D->bottomMargin; + else + val -= 2*margins().height(); + } + break; + case QPaintDeviceMetrics::PdmDpiX: + val = res; + break; + case QPaintDeviceMetrics::PdmDpiY: + val = res; + break; + case QPaintDeviceMetrics::PdmPhysicalDpiX: + case QPaintDeviceMetrics::PdmPhysicalDpiY: + val = 72; + break; + case QPaintDeviceMetrics::PdmWidthMM: + // double rounding error here. hooray. + val = metric( QPaintDeviceMetrics::PdmWidth ); + val = (val * 254 + 5*res) / (10*res); + break; + case QPaintDeviceMetrics::PdmHeightMM: + val = metric( QPaintDeviceMetrics::PdmHeight ); + val = (val * 254 + 5*res) / (10*res); + break; + case QPaintDeviceMetrics::PdmNumColors: + val = 16777216; + break; + case QPaintDeviceMetrics::PdmDepth: + val = 24; + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "QPixmap::metric: Invalid metric command" ); +#endif + } + return val; +} + + +/*! + Returns the width of the left margin and the height of the top + margin of the printer. On Unix, this is a best-effort guess, not + based on perfect knowledge. + + If you have called setFullPage( TRUE ), margins().width() may be + treated as the smallest sane left margin you can use, and + margins().height() as the smallest sane top margin you can + use. + + If you have called setFullPage( FALSE ) (this is the default), + margins() is automatically subtracted from the pageSize() by + QPrinter. + + \sa setFullPage() QPaintDeviceMetrics PageSize +*/ +QSize QPrinter::margins() const +{ + if ( D->marginsSpecified ) + return QSize( D->leftMargin, D->topMargin ); + + if (orient == Portrait) + return QSize( res/2, res/3 ); + + return QSize( res/3, res/2 ); +} + +/*! + \overload + + Sets \a top, \a left, \a bottom and \a right to the margins of the + printer. On Unix, this is a best-effort guess, not based on + perfect knowledge. + + If you have called setFullPage( TRUE ), the four values specify + the smallest sane margins you can use. + + If you have called setFullPage( FALSE ) (this is the default), + the margins are automatically subtracted from the pageSize() by + QPrinter. + + \sa setFullPage() QPaintDeviceMetrics PageSize +*/ +void QPrinter::margins( uint *top, uint *left, uint *bottom, uint *right ) const +{ + if ( !D->marginsSpecified ) { + int x = orient == Portrait ? res/2 : res/3; + int y = orient == Portrait ? res/3 : res/2; + *top = *bottom = y; + *left = *right = x; + } else { + *top = D->topMargin; + *left = D->leftMargin; + *bottom = D->bottomMargin; + *right = D->rightMargin; + } +} + +/*! + Sets the printer margins to the sizes specified in \a top, \a left, + \a bottom and \a right. + + This function currently only has an effect on Unix systems. + + \sa margins() +*/ +void QPrinter::setMargins( uint top, uint left, uint bottom, uint right ) +{ + D->topMargin = top; + D->leftMargin = left; + D->bottomMargin = bottom; + D->rightMargin = right; + D->marginsSpecified = TRUE; +} + +#endif diff --git a/src/kernel/qprocess.cpp b/src/kernel/qprocess.cpp new file mode 100644 index 0000000..653327d --- /dev/null +++ b/src/kernel/qprocess.cpp @@ -0,0 +1,806 @@ +/**************************************************************************** +** +** Implementation of QProcess class +** +** Created : 20000905 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 <stdio.h> +#include <stdlib.h> + +#include "qprocess.h" + +#ifndef QT_NO_PROCESS + +#include "qapplication.h" +#include "private/qinternal_p.h" + + +//#define QT_QPROCESS_DEBUG + + +/*! + \class QProcess qprocess.h + + \brief The QProcess class is used to start external programs and + to communicate with them. + + \ingroup io + \ingroup misc + \mainclass + + You can write to the started program's standard input, and can + read the program's standard output and standard error. You can + pass command line arguments to the program either in the + constructor or with setArguments() or addArgument(). The program's + working directory can be set with setWorkingDirectory(). If you + need to set up environment variables pass them to the start() or + launch() functions (see below). The processExited() signal is + emitted if the program exits. The program's exit status is + available from exitStatus(), although you could simply call + normalExit() to see if the program terminated normally. + + There are two different ways to start a process. If you just want + to run a program, optionally passing data to its standard input at + the beginning, use one of the launch() functions. If you want full + control of the program's standard input (especially if you don't + know all the data you want to send to standard input at the + beginning), use the start() function. + + If you use start() you can write to the program's standard input + using writeToStdin() and you can close the standard input with + closeStdin(). The wroteToStdin() signal is emitted if the data + sent to standard input has been written. You can read from the + program's standard output using readStdout() or readLineStdout(). + These functions return an empty QByteArray if there is no data to + read. The readyReadStdout() signal is emitted when there is data + available to be read from standard output. Standard error has a + set of functions that correspond to the standard output functions, + i.e. readStderr(), readLineStderr() and readyReadStderr(). + + If you use one of the launch() functions the data you pass will be + sent to the program's standard input which will be closed once all + the data has been written. You should \e not use writeToStdin() or + closeStdin() if you use launch(). If you need to send data to the + program's standard input after it has started running use start() + instead of launch(). + + Both start() and launch() can accept a string list of strings each + of which has the format, key=value, where the keys are the names + of environment variables. + + You can test to see if a program is running with isRunning(). The + program's process identifier is available from + processIdentifier(). If you want to terminate a running program + use tryTerminate(), but note that the program may ignore this. If + you \e really want to terminate the program, without it having any + chance to clean up, you can use kill(). + + As an example, suppose we want to start the \c uic command (a Qt + command line tool used with \e{Qt Designer}) and perform some + operations on the output (the \c uic outputs the code it generates + to standard output by default). Suppose further that we want to + run the program on the file "small_dialog.ui" with the command + line options "-tr i18n". On the command line we would write: + \code + uic -tr i18n small_dialog.ui + \endcode + + \quotefile process/process.cpp + + A code snippet for this with the QProcess class might look like + this: + + \skipto UicManager::UicManager() + \printline UicManager::UicManager() + \printline { + \skipto proc = new QProcess( this ); + \printline proc = new QProcess( this ); + \skipto proc->addArgument( "uic" ); + \printuntil this, SLOT(readFromStdout()) ); + \skipto if ( !proc->start() ) { + \printuntil // error handling + \skipto } + \printline } + \printline } + + \skipto void UicManager::readFromStdout() + \printuntil // Bear in mind that the data might be output in chunks. + \skipto } + \printline } + + Although you may need quotes for a file named on the command line + (e.g. if it contains spaces) you shouldn't use extra quotes for + arguments passed to addArgument() or setArguments(). + + The readyReadStdout() signal is emitted when there is new data on + standard output. This happens asynchronously: you don't know if + more data will arrive later. + + In the above example you could connect the processExited() signal + to the slot UicManager::readFromStdout() instead. If you do so, + you will be certain that all the data is available when the slot + is called. On the other hand, you must wait until the process has + finished before doing any processing. + + Note that if you are expecting a lot of output from the process, + you may hit platform-dependent limits to the pipe buffer size. The + solution is to make sure you connect to the output, e.g. the + readyReadStdout() and readyReadStderr() signals and read the data + as soon as it becomes available. + + Please note that QProcess does not emulate a shell. This means that + QProcess does not do any expansion of arguments: a '*' is passed as a '*' + to the program and is \e not replaced by all the files, a '$HOME' is also + passed literally and is \e not replaced by the environment variable HOME + and the special characters for IO redirection ('>', '|', etc.) are also + passed literally and do \e not have the special meaning as they have in a + shell. + + Also note that QProcess does not emulate a terminal. This means that + certain programs which need direct terminal control, do not work as + expected with QProcess. Such programs include console email programs (like + pine and mutt) but also programs which require the user to enter a password + (like su and ssh). + + \section1 Notes for Windows users + + Some Windows commands, for example, \c dir, are not provided by + separate applications, but by the command interpreter. + If you attempt to use QProcess to execute these commands directly + it won't work. One possible solution is to execute the command + interpreter itself (\c cmd.exe on some Windows systems), and ask + the interpreter to execute the desired command. + + Under Windows there are certain problems starting 16-bit applications + and capturing their output. Microsoft recommends using an intermediate + application to start 16-bit applications. + + \sa QSocket +*/ + +/*! + \enum QProcess::Communication + + This enum type defines the communication channels connected to the + process. + + \value Stdin Data can be written to the process's standard input. + + \value Stdout Data can be read from the process's standard + output. + + \value Stderr Data can be read from the process's standard error. + + \value DupStderr Both the process's standard error output \e and + its standard output are written to its standard output. (Like + Unix's dup2().) This means that nothing is sent to the standard + error output. This is especially useful if your application + requires that the output on standard output and on standard error + must be read in the same order that they are produced. This is a + flag, so to activate it you must pass \c{Stdout|Stderr|DupStderr}, + or \c{Stdin|Stdout|Stderr|DupStderr} if you want to provide input, + to the setCommunication() call. + + \sa setCommunication() communication() +*/ + +/*! + Constructs a QProcess object. The \a parent and \a name parameters + are passed to the QObject constructor. + + \sa setArguments() addArgument() start() +*/ +QProcess::QProcess( QObject *parent, const char *name ) + : QObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ), + wroteToStdinConnected( FALSE ), + readStdoutCalled( FALSE ), readStderrCalled( FALSE ), + comms( Stdin|Stdout|Stderr ) +{ + init(); +} + +/*! + Constructs a QProcess with \a arg0 as the command to be executed. + The \a parent and \a name parameters are passed to the QObject + constructor. + + The process is not started. You must call start() or launch() to + start the process. + + \sa setArguments() addArgument() start() +*/ +QProcess::QProcess( const QString& arg0, QObject *parent, const char *name ) + : QObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ), + wroteToStdinConnected( FALSE ), + readStdoutCalled( FALSE ), readStderrCalled( FALSE ), + comms( Stdin|Stdout|Stderr ) +{ + init(); + addArgument( arg0 ); +} + +/*! + Constructs a QProcess with \a args as the arguments of the + process. The first element in the list is the command to be + executed. The other elements in the list are the arguments to this + command. The \a parent and \a name parameters are passed to the + QObject constructor. + + The process is not started. You must call start() or launch() to + start the process. + + \sa setArguments() addArgument() start() +*/ +QProcess::QProcess( const QStringList& args, QObject *parent, const char *name ) + : QObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ), + wroteToStdinConnected( FALSE ), + readStdoutCalled( FALSE ), readStderrCalled( FALSE ), + comms( Stdin|Stdout|Stderr ) +{ + init(); + setArguments( args ); +} + + +/*! + Returns the list of arguments that are set for the process. + Arguments can be specified with the constructor or with the + functions setArguments() and addArgument(). + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QStringList list = myProcess.arguments(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa setArguments() addArgument() +*/ +QStringList QProcess::arguments() const +{ + return _arguments; +} + +/*! + Clears the list of arguments that are set for the process. + + \sa setArguments() addArgument() +*/ +void QProcess::clearArguments() +{ + _arguments.clear(); +} + +/*! + Sets \a args as the arguments for the process. The first element + in the list is the command to be executed. The other elements in + the list are the arguments to the command. Any previous arguments + are deleted. + + QProcess does not perform argument substitutions; for example, if you + specify "*" or "$DISPLAY", these values are passed to the process + literally. If you want to have the same behavior as the shell + provides, you must do the substitutions yourself; i.e. instead of + specifying a "*" you must specify the list of all the filenames in + the current directory, and instead of "$DISPLAY" you must specify + the value of the environment variable \c DISPLAY. + + Note for Windows users. The standard Windows shells, e.g. \c + command.com and \c cmd.exe, do not perform file globbing, i.e. + they do not convert a "*" on the command line into a list of files + in the current directory. For this reason most Windows + applications implement their own file globbing, and as a result of + this, specifying an argument of "*" for a Windows application is + likely to result in the application performing a file glob and + ending up with a list of filenames. + + \sa arguments() addArgument() +*/ +void QProcess::setArguments( const QStringList& args ) +{ + _arguments = args; +} + +/*! + Adds \a arg to the end of the list of arguments. + + The first element in the list of arguments is the command to be + executed; the following elements are the command's arguments. + + \sa arguments() setArguments() +*/ +void QProcess::addArgument( const QString& arg ) +{ + _arguments.append( arg ); +} + +#ifndef QT_NO_DIR +/*! + Returns the working directory that was set with + setWorkingDirectory(), or the current directory if none has been + explicitly set. + + \sa setWorkingDirectory() QDir::current() +*/ +QDir QProcess::workingDirectory() const +{ + return workingDir; +} + +/*! + Sets \a dir as the working directory for processes. This does not + affect running processes; only processes that are started + afterwards are affected. + + Setting the working directory is especially useful for processes + that try to access files with relative paths. + + \sa workingDirectory() start() +*/ +void QProcess::setWorkingDirectory( const QDir& dir ) +{ + workingDir = dir; +} +#endif //QT_NO_DIR + +/*! + Returns the communication required with the process, i.e. some + combination of the \c Communication flags. + + \sa setCommunication() +*/ +int QProcess::communication() const +{ + return comms; +} + +/*! + Sets \a commFlags as the communication required with the process. + + \a commFlags is a bitwise OR of the flags defined by the \c + Communication enum. + + The default is \c{Stdin|Stdout|Stderr}. + + \sa communication() +*/ +void QProcess::setCommunication( int commFlags ) +{ + comms = commFlags; +} + +/*! + Returns TRUE if the process has exited normally; otherwise returns + FALSE. This implies that this function returns FALSE if the + process is still running. + + \sa isRunning() exitStatus() processExited() +*/ +bool QProcess::normalExit() const +{ + // isRunning() has the side effect that it determines the exit status! + if ( isRunning() ) + return FALSE; + else + return exitNormal; +} + +/*! + Returns the exit status of the process or 0 if the process is + still running. This function returns immediately and does not wait + until the process is finished. + + If normalExit() is FALSE (e.g. if the program was killed or + crashed), this function returns 0, so you should check the return + value of normalExit() before relying on this value. + + \sa normalExit() processExited() +*/ +int QProcess::exitStatus() const +{ + // isRunning() has the side effect that it determines the exit status! + if ( isRunning() ) + return 0; + else + return exitStat; +} + + +/*! + Reads the data that the process has written to standard output. + When new data is written to standard output, the class emits the + signal readyReadStdout(). + + If there is no data to read, this function returns a QByteArray of + size 0: it does not wait until there is something to read. + + \sa readyReadStdout() readLineStdout() readStderr() writeToStdin() +*/ +QByteArray QProcess::readStdout() +{ + if ( readStdoutCalled ) { + return QByteArray(); + } + readStdoutCalled = TRUE; + QMembuf *buf = membufStdout(); + readStdoutCalled = FALSE; + + return buf->readAll(); +} + +/*! + Reads the data that the process has written to standard error. + When new data is written to standard error, the class emits the + signal readyReadStderr(). + + If there is no data to read, this function returns a QByteArray of + size 0: it does not wait until there is something to read. + + \sa readyReadStderr() readLineStderr() readStdout() writeToStdin() +*/ +QByteArray QProcess::readStderr() +{ + if ( readStderrCalled ) { + return QByteArray(); + } + readStderrCalled = TRUE; + QMembuf *buf = membufStderr(); + readStderrCalled = FALSE; + + return buf->readAll(); +} + +/*! + Reads a line of text from standard output, excluding any trailing + newline or carriage return characters, and returns it. Returns + QString::null if canReadLineStdout() returns FALSE. + + By default, the text is interpreted to be in Latin-1 encoding. If you need + other codecs, you can set a different codec with + QTextCodec::setCodecForCStrings(). + + \sa canReadLineStdout() readyReadStdout() readStdout() readLineStderr() +*/ +QString QProcess::readLineStdout() +{ + QByteArray a( 256 ); + QMembuf *buf = membufStdout(); + if ( !buf->scanNewline( &a ) ) { + if ( !canReadLineStdout() ) + return QString::null; + + if ( !buf->scanNewline( &a ) ) + return QString( buf->readAll() ); + } + + uint size = a.size(); + buf->consumeBytes( size, 0 ); + + // get rid of terminating \n or \r\n + if ( size>0 && a.at( size - 1 ) == '\n' ) { + if ( size>1 && a.at( size - 2 ) == '\r' ) + a.at( size - 2 ) = '\0'; + else + a.at( size - 1 ) = '\0'; + } + return QString( a ); +} + +/*! + Reads a line of text from standard error, excluding any trailing + newline or carriage return characters and returns it. Returns + QString::null if canReadLineStderr() returns FALSE. + + By default, the text is interpreted to be in Latin-1 encoding. If you need + other codecs, you can set a different codec with + QTextCodec::setCodecForCStrings(). + + \sa canReadLineStderr() readyReadStderr() readStderr() readLineStdout() +*/ +QString QProcess::readLineStderr() +{ + QByteArray a( 256 ); + QMembuf *buf = membufStderr(); + if ( !buf->scanNewline( &a ) ) { + if ( !canReadLineStderr() ) + return QString::null; + + if ( !buf->scanNewline( &a ) ) + return QString( buf->readAll() ); + } + + uint size = a.size(); + buf->consumeBytes( size, 0 ); + + // get rid of terminating \n or \r\n + if ( size>0 && a.at( size - 1 ) == '\n' ) { + if ( size>1 && a.at( size - 2 ) == '\r' ) + a.at( size - 2 ) = '\0'; + else + a.at( size - 1 ) = '\0'; + } + return QString( a ); +} + +/*! + \fn void QProcess::launchFinished() + + This signal is emitted when the process was started with launch(). + If the start was successful, this signal is emitted after all the + data has been written to standard input. If the start failed, then + this signal is emitted immediately. + + This signal is especially useful if you want to know when you can + safely delete the QProcess object when you are not interested in + reading from standard output or standard error. + + \sa launch() QObject::deleteLater() +*/ + +/*! + Runs the process and writes the data \a buf to the process's + standard input. If all the data is written to standard input, + standard input is closed. The command is searched for in the path + for executable programs; you can also use an absolute path in the + command itself. + + If \a env is null, then the process is started with the same + environment as the starting process. If \a env is non-null, then + the values in the string list are interpreted as environment + setttings of the form \c {key=value} and the process is started + with these environment settings. For convenience, there is a small + exception to this rule under Unix: if \a env does not contain any + settings for the environment variable \c LD_LIBRARY_PATH, then + this variable is inherited from the starting process. + + Returns TRUE if the process could be started; otherwise returns + FALSE. + + Note that you should not use the slots writeToStdin() and + closeStdin() on processes started with launch(), since the result + is not well-defined. If you need these slots, use start() instead. + + The process may or may not read the \a buf data sent to its + standard input. + + You can call this function even when a process that was started + with this instance is still running. Be aware that if you do this + the standard input of the process that was launched first will be + closed, with any pending data being deleted, and the process will + be left to run out of your control. Similarly, if the process + could not be started the standard input will be closed and the + pending data deleted. (On operating systems that have zombie + processes, Qt will also wait() on the old process.) + + The object emits the signal launchFinished() when this function + call is finished. If the start was successful, this signal is + emitted after all the data has been written to standard input. If + the start failed, then this signal is emitted immediately. + + \sa start() launchFinished(); +*/ +bool QProcess::launch( const QByteArray& buf, QStringList *env ) +{ + if ( start( env ) ) { + if ( !buf.isEmpty() ) { + connect( this, SIGNAL(wroteToStdin()), + this, SLOT(closeStdinLaunch()) ); + writeToStdin( buf ); + } else { + closeStdin(); + emit launchFinished(); + } + return TRUE; + } else { + emit launchFinished(); + return FALSE; + } +} + +/*! + \overload + + The data \a buf is written to standard input with writeToStdin() + using the QString::local8Bit() representation of the strings. +*/ +bool QProcess::launch( const QString& buf, QStringList *env ) +{ + if ( start( env ) ) { + if ( !buf.isEmpty() ) { + connect( this, SIGNAL(wroteToStdin()), + this, SLOT(closeStdinLaunch()) ); + writeToStdin( buf ); + } else { + closeStdin(); + emit launchFinished(); + } + return TRUE; + } else { + emit launchFinished(); + return FALSE; + } +} + +/* + This private slot is used by the launch() functions to close standard input. +*/ +void QProcess::closeStdinLaunch() +{ + disconnect( this, SIGNAL(wroteToStdin()), + this, SLOT(closeStdinLaunch()) ); + closeStdin(); + emit launchFinished(); +} + + +/*! + \fn void QProcess::readyReadStdout() + + This signal is emitted when the process has written data to + standard output. You can read the data with readStdout(). + + Note that this signal is only emitted when there is new data and + not when there is old, but unread data. In the slot connected to + this signal, you should always read everything that is available + at that moment to make sure that you don't lose any data. + + \sa readStdout() readLineStdout() readyReadStderr() +*/ + +/*! + \fn void QProcess::readyReadStderr() + + This signal is emitted when the process has written data to + standard error. You can read the data with readStderr(). + + Note that this signal is only emitted when there is new data and + not when there is old, but unread data. In the slot connected to + this signal, you should always read everything that is available + at that moment to make sure that you don't lose any data. + + \sa readStderr() readLineStderr() readyReadStdout() +*/ + +/*! + \fn void QProcess::processExited() + + This signal is emitted when the process has exited. + + \sa isRunning() normalExit() exitStatus() start() launch() +*/ + +/*! + \fn void QProcess::wroteToStdin() + + This signal is emitted if the data sent to standard input (via + writeToStdin()) was actually written to the process. This does not + imply that the process really read the data, since this class only + detects when it was able to write the data to the operating + system. But it is now safe to close standard input without losing + pending data. + + \sa writeToStdin() closeStdin() +*/ + + +/*! + \overload + + The string \a buf is handled as text using the + QString::local8Bit() representation. +*/ +void QProcess::writeToStdin( const QString& buf ) +{ + QByteArray tmp = buf.local8Bit(); + tmp.resize( qstrlen( tmp.data() ) ); + writeToStdin( tmp ); +} + + +/* + * Under Windows the implementation is not so nice: it is not that easy to + * detect when one of the signals should be emitted; therefore there are some + * timers that query the information. + * To keep it a little efficient, use the timers only when they are needed. + * They are needed, if you are interested in the signals. So use + * connectNotify() and disconnectNotify() to keep track of your interest. + */ +/*! \reimp +*/ +void QProcess::connectNotify( const char * signal ) +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::connectNotify(): signal %s has been connected", signal ); +#endif + if ( !ioRedirection ) + if ( qstrcmp( signal, SIGNAL(readyReadStdout()) )==0 || + qstrcmp( signal, SIGNAL(readyReadStderr()) )==0 + ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::connectNotify(): set ioRedirection to TRUE" ); +#endif + setIoRedirection( TRUE ); + return; + } + if ( !notifyOnExit && qstrcmp( signal, SIGNAL(processExited()) )==0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::connectNotify(): set notifyOnExit to TRUE" ); +#endif + setNotifyOnExit( TRUE ); + return; + } + if ( !wroteToStdinConnected && qstrcmp( signal, SIGNAL(wroteToStdin()) )==0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::connectNotify(): set wroteToStdinConnected to TRUE" ); +#endif + setWroteStdinConnected( TRUE ); + return; + } +} + +/*! \reimp +*/ +void QProcess::disconnectNotify( const char * ) +{ + if ( ioRedirection && + receivers( SIGNAL(readyReadStdout()) ) ==0 && + receivers( SIGNAL(readyReadStderr()) ) ==0 + ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::disconnectNotify(): set ioRedirection to FALSE" ); +#endif + setIoRedirection( FALSE ); + } + if ( notifyOnExit && receivers( SIGNAL(processExited()) ) == 0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::disconnectNotify(): set notifyOnExit to FALSE" ); +#endif + setNotifyOnExit( FALSE ); + } + if ( wroteToStdinConnected && receivers( SIGNAL(wroteToStdin()) ) == 0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::disconnectNotify(): set wroteToStdinConnected to FALSE" ); +#endif + setWroteStdinConnected( FALSE ); + } +} + +#endif // QT_NO_PROCESS diff --git a/src/kernel/qprocess.h b/src/kernel/qprocess.h new file mode 100644 index 0000000..c2d6026 --- /dev/null +++ b/src/kernel/qprocess.h @@ -0,0 +1,178 @@ +/**************************************************************************** +** +** Implementation of QProcess class +** +** Created : 20000905 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPROCESS_H +#define QPROCESS_H + +#ifndef QT_H +#include "qobject.h" +#include "qstringlist.h" +#include "qdir.h" +#endif // QT_H + +#ifndef QT_NO_PROCESS + +class QProcessPrivate; +class QMembuf; + + +class Q_EXPORT QProcess : public QObject +{ + Q_OBJECT +public: + QProcess( QObject *parent=0, const char *name=0 ); + QProcess( const QString& arg0, QObject *parent=0, const char *name=0 ); + QProcess( const QStringList& args, QObject *parent=0, const char *name=0 ); + ~QProcess(); + + // set and get the arguments and working directory + QStringList arguments() const; + void clearArguments(); + virtual void setArguments( const QStringList& args ); + virtual void addArgument( const QString& arg ); +#ifndef QT_NO_DIR + QDir workingDirectory() const; + virtual void setWorkingDirectory( const QDir& dir ); +#endif + + // set and get the comms wanted + enum Communication { Stdin=0x01, Stdout=0x02, Stderr=0x04, DupStderr=0x08 }; + void setCommunication( int c ); + int communication() const; + + // start the execution + virtual bool start( QStringList *env=0 ); + virtual bool launch( const QString& buf, QStringList *env=0 ); + virtual bool launch( const QByteArray& buf, QStringList *env=0 ); + + // inquire the status + bool isRunning() const; + bool normalExit() const; + int exitStatus() const; + + // reading + virtual QByteArray readStdout(); + virtual QByteArray readStderr(); + bool canReadLineStdout() const; + bool canReadLineStderr() const; + virtual QString readLineStdout(); + virtual QString readLineStderr(); + + // get platform dependent process information +#if defined(Q_OS_WIN32) + typedef void* PID; +#else + typedef Q_LONG PID; +#endif + PID processIdentifier(); + + void flushStdin(); + +signals: + void readyReadStdout(); + void readyReadStderr(); + void processExited(); + void wroteToStdin(); + void launchFinished(); + +public slots: + // end the execution + void tryTerminate() const; + void kill() const; + + // input + virtual void writeToStdin( const QByteArray& buf ); + virtual void writeToStdin( const QString& buf ); + virtual void closeStdin(); + +protected: // ### or private? + void connectNotify( const char * signal ); + void disconnectNotify( const char * signal ); +private: + void setIoRedirection( bool value ); + void setNotifyOnExit( bool value ); + void setWroteStdinConnected( bool value ); + + void init(); + void reset(); +#if defined(Q_OS_WIN32) + uint readStddev( HANDLE dev, char *buf, uint bytes ); +#endif + QMembuf* membufStdout(); + QMembuf* membufStderr(); + +private slots: + void socketRead( int fd ); + void socketWrite( int fd ); + void timeout(); + void closeStdinLaunch(); + +private: + QProcessPrivate *d; +#ifndef QT_NO_DIR + QDir workingDir; +#endif + QStringList _arguments; + + int exitStat; // exit status + bool exitNormal; // normal exit? + bool ioRedirection; // automatically set be (dis)connectNotify + bool notifyOnExit; // automatically set be (dis)connectNotify + bool wroteToStdinConnected; // automatically set be (dis)connectNotify + + bool readStdoutCalled; + bool readStderrCalled; + int comms; + + friend class QProcessPrivate; +#if defined(Q_OS_UNIX) + friend class QProcessManager; + friend class QProc; +#endif + +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QProcess( const QProcess & ); + QProcess &operator=( const QProcess & ); +#endif +}; + +#endif // QT_NO_PROCESS + +#endif // QPROCESS_H diff --git a/src/kernel/qprocess_unix.cpp b/src/kernel/qprocess_unix.cpp new file mode 100644 index 0000000..40476d3 --- /dev/null +++ b/src/kernel/qprocess_unix.cpp @@ -0,0 +1,1409 @@ +/**************************************************************************** +** +** Implementation of QProcess class for Unix +** +** Created : 20000905 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +// Solaris redefines connect -> __xnet_connect with _XOPEN_SOURCE_EXTENDED. +#if defined(connect) +#undef connect +#endif + +#include "qprocess.h" + +#ifndef QT_NO_PROCESS + +#include "qapplication.h" +#include "qptrqueue.h" +#include "qptrlist.h" +#include "qsocketnotifier.h" +#include "qtimer.h" +#include "qcleanuphandler.h" +#include "qregexp.h" +#include "private/qinternal_p.h" + +#include <stdlib.h> +#include <errno.h> +#include <sys/types.h> + +#ifdef __MIPSEL__ +# ifndef SOCK_DGRAM +# define SOCK_DGRAM 1 +# endif +# ifndef SOCK_STREAM +# define SOCK_STREAM 2 +# endif +#endif + +//#define QT_QPROCESS_DEBUG + + +#ifdef Q_C_CALLBACKS +extern "C" { +#endif // Q_C_CALLBACKS + + QT_SIGNAL_RETTYPE qt_C_sigchldHnd(QT_SIGNAL_ARGS); + +#ifdef Q_C_CALLBACKS +} +#endif // Q_C_CALLBACKS + + +class QProc; +class QProcessManager; +class QProcessPrivate +{ +public: + QProcessPrivate(); + ~QProcessPrivate(); + + void closeOpenSocketsForChild(); + void newProc( pid_t pid, QProcess *process ); + + QMembuf bufStdout; + QMembuf bufStderr; + + QPtrQueue<QByteArray> stdinBuf; + + QSocketNotifier *notifierStdin; + QSocketNotifier *notifierStdout; + QSocketNotifier *notifierStderr; + + ssize_t stdinBufRead; + QProc *proc; + + bool exitValuesCalculated; + bool socketReadCalled; + + static QProcessManager *procManager; +}; + + +/*********************************************************************** + * + * QProc + * + **********************************************************************/ +/* + The class QProcess does not necessarily map exactly to the running + child processes: if the process is finished, the QProcess class may still be + there; furthermore a user can use QProcess to start more than one process. + + The helper-class QProc has the semantics that one instance of this class maps + directly to a running child process. +*/ +class QProc +{ +public: + QProc( pid_t p, QProcess *proc=0 ) : pid(p), process(proc) + { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProc: Constructor for pid %d and QProcess %p", pid, process ); +#endif + socketStdin = 0; + socketStdout = 0; + socketStderr = 0; + } + ~QProc() + { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProc: Destructor for pid %d and QProcess %p", pid, process ); +#endif + if ( process ) { + if ( process->d->notifierStdin ) + process->d->notifierStdin->setEnabled( FALSE ); + if ( process->d->notifierStdout ) + process->d->notifierStdout->setEnabled( FALSE ); + if ( process->d->notifierStderr ) + process->d->notifierStderr->setEnabled( FALSE ); + process->d->proc = 0; + } + if( socketStdin ) + ::close( socketStdin ); + if( socketStdout ) + ::close( socketStdout ); + if( socketStderr ) + ::close( socketStderr ); + } + + pid_t pid; + int socketStdin; + int socketStdout; + int socketStderr; + QProcess *process; +}; + +/*********************************************************************** + * + * QProcessManager + * + **********************************************************************/ +class QProcessManager : public QObject +{ + Q_OBJECT + +public: + QProcessManager(); + ~QProcessManager(); + + void append( QProc *p ); + void remove( QProc *p ); + + void cleanup(); + +public slots: + void removeMe(); + void sigchldHnd( int ); + +public: + struct sigaction oldactChld; + struct sigaction oldactPipe; + QPtrList<QProc> *procList; + int sigchldFd[2]; + +private: + QSocketNotifier *sn; +}; + +static void qprocess_cleanup() +{ + delete QProcessPrivate::procManager; + QProcessPrivate::procManager = 0; +} + +#ifdef Q_OS_QNX6 +#define BAILOUT close(tmpSocket);close(socketFD[1]);return -1; +int qnx6SocketPairReplacement (int socketFD[2]) { + int tmpSocket; + tmpSocket = socket (AF_INET, SOCK_STREAM, 0); + if (tmpSocket == -1) + return -1; + socketFD[1] = socket(AF_INET, SOCK_STREAM, 0); + if (socketFD[1] == -1) { BAILOUT }; + + sockaddr_in ipAddr; + memset(&ipAddr, 0, sizeof(ipAddr)); + ipAddr.sin_family = AF_INET; + ipAddr.sin_addr.s_addr = INADDR_ANY; + + int socketOptions = 1; + setsockopt(tmpSocket, SOL_SOCKET, SO_REUSEADDR, &socketOptions, sizeof(int)); + + bool found = FALSE; + for (int socketIP = 2000; (socketIP < 2500) && !(found); socketIP++) { + ipAddr.sin_port = htons(socketIP); + if (bind(tmpSocket, (struct sockaddr *)&ipAddr, sizeof(ipAddr))) + found = TRUE; + } + + if (listen(tmpSocket, 5)) { BAILOUT }; + + // Select non-blocking mode + int originalFlags = fcntl(socketFD[1], F_GETFL, 0); + fcntl(socketFD[1], F_SETFL, originalFlags | O_NONBLOCK); + + // Request connection + if (connect(socketFD[1], (struct sockaddr*)&ipAddr, sizeof(ipAddr))) + if (errno != EINPROGRESS) { BAILOUT }; + + // Accept connection + socketFD[0] = accept(tmpSocket, (struct sockaddr *)NULL, (size_t *)NULL); + if(socketFD[0] == -1) { BAILOUT }; + + // We're done + close(tmpSocket); + + // Restore original flags , ie return to blocking + fcntl(socketFD[1], F_SETFL, originalFlags); + return 0; +} +#undef BAILOUT +#endif + +QProcessManager::QProcessManager() : sn(0) +{ + procList = new QPtrList<QProc>; + procList->setAutoDelete( TRUE ); + + // The SIGCHLD handler writes to a socket to tell the manager that + // something happened. This is done to get the processing in sync with the + // event reporting. +#ifndef Q_OS_QNX6 + if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, sigchldFd ) ) { +#else + if ( qnx6SocketPairReplacement (sigchldFd) ) { +#endif + sigchldFd[0] = 0; + sigchldFd[1] = 0; + } else { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: install socket notifier (%d)", sigchldFd[1] ); +#endif + sn = new QSocketNotifier( sigchldFd[1], + QSocketNotifier::Read, this ); + connect( sn, SIGNAL(activated(int)), + this, SLOT(sigchldHnd(int)) ); + sn->setEnabled( TRUE ); + } + + // install a SIGCHLD handler and ignore SIGPIPE + struct sigaction act; + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: install a SIGCHLD handler" ); +#endif + act.sa_handler = qt_C_sigchldHnd; + sigemptyset( &(act.sa_mask) ); + sigaddset( &(act.sa_mask), SIGCHLD ); + act.sa_flags = SA_NOCLDSTOP; +#if defined(SA_RESTART) + act.sa_flags |= SA_RESTART; +#endif + if ( sigaction( SIGCHLD, &act, &oldactChld ) != 0 ) + qWarning( "Error installing SIGCHLD handler" ); + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: install a SIGPIPE handler (SIG_IGN)" ); +#endif + act.sa_handler = QT_SIGNAL_IGNORE; + sigemptyset( &(act.sa_mask) ); + sigaddset( &(act.sa_mask), SIGPIPE ); + act.sa_flags = 0; + if ( sigaction( SIGPIPE, &act, &oldactPipe ) != 0 ) + qWarning( "Error installing SIGPIPE handler" ); +} + +QProcessManager::~QProcessManager() +{ + delete procList; + + if ( sigchldFd[0] != 0 ) + ::close( sigchldFd[0] ); + if ( sigchldFd[1] != 0 ) + ::close( sigchldFd[1] ); + + // restore SIGCHLD handler +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: restore old sigchild handler" ); +#endif + if ( sigaction( SIGCHLD, &oldactChld, 0 ) != 0 ) + qWarning( "Error restoring SIGCHLD handler" ); + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: restore old sigpipe handler" ); +#endif + if ( sigaction( SIGPIPE, &oldactPipe, 0 ) != 0 ) + qWarning( "Error restoring SIGPIPE handler" ); +} + +void QProcessManager::append( QProc *p ) +{ + procList->append( p ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: append process (procList.count(): %d)", procList->count() ); +#endif +} + +void QProcessManager::remove( QProc *p ) +{ + procList->remove( p ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager: remove process (procList.count(): %d)", procList->count() ); +#endif + cleanup(); +} + +void QProcessManager::cleanup() +{ + if ( procList->count() == 0 ) { + QTimer::singleShot( 0, this, SLOT(removeMe()) ); + } +} + +void QProcessManager::removeMe() +{ + if ( procList->count() == 0 ) { + qRemovePostRoutine(qprocess_cleanup); + QProcessPrivate::procManager = 0; + delete this; + } +} + +void QProcessManager::sigchldHnd( int fd ) +{ + // Disable the socket notifier to make sure that this function is not + // called recursively -- this can happen, if you enter the event loop in + // the slot connected to the processExited() signal (e.g. by showing a + // modal dialog) and there are more than one process which exited in the + // meantime. + if ( sn ) { + if ( !sn->isEnabled() ) + return; + sn->setEnabled( FALSE ); + } + + char tmp; + ::read( fd, &tmp, sizeof(tmp) ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager::sigchldHnd()" ); +#endif + QProc *proc; + QProcess *process; + bool removeProc; + proc = procList->first(); + while ( proc != 0 ) { + removeProc = FALSE; + process = proc->process; + if ( process != 0 ) { + if ( !process->isRunning() ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager::sigchldHnd() (PID: %d): process exited (QProcess available)", proc->pid ); +#endif + /* + Apparently, there is not consistency among different + operating systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd + argument to ioctl() to be an int, which is normally + 32-bit even on 64-bit machines. + + IRIX, on the other hand, expects a size_t, which is + 64-bit on 64-bit machines. + + So, the solution is to use size_t initialized to + zero to make sure all bits are set to zero, + preventing underflow with the FreeBSD/Linux/Solaris + ioctls. + */ + size_t nbytes = 0; + // read pending data + if ( proc->socketStdout && ::ioctl(proc->socketStdout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager::sigchldHnd() (PID: %d): reading %d bytes of pending data on stdout", proc->pid, nbytes ); +#endif + process->socketRead( proc->socketStdout ); + } + nbytes = 0; + if ( proc->socketStderr && ::ioctl(proc->socketStderr, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager::sigchldHnd() (PID: %d): reading %d bytes of pending data on stderr", proc->pid, nbytes ); +#endif + process->socketRead( proc->socketStderr ); + } + // close filedescriptors if open, and disable the + // socket notifiers + if ( proc->socketStdout ) { + ::close( proc->socketStdout ); + proc->socketStdout = 0; + if (process->d->notifierStdout) + process->d->notifierStdout->setEnabled(FALSE); + } + if ( proc->socketStderr ) { + ::close( proc->socketStderr ); + proc->socketStderr = 0; + if (process->d->notifierStderr) + process->d->notifierStderr->setEnabled(FALSE); + } + + if ( process->notifyOnExit ) + emit process->processExited(); + + removeProc = TRUE; + } + } else { + int status; + if ( ::waitpid( proc->pid, &status, WNOHANG ) == proc->pid ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessManager::sigchldHnd() (PID: %d): process exited (QProcess not available)", proc->pid ); +#endif + removeProc = TRUE; + } + } + if ( removeProc ) { + QProc *oldproc = proc; + proc = procList->next(); + remove( oldproc ); + } else { + proc = procList->next(); + } + } + if ( sn ) + sn->setEnabled( TRUE ); +} + +#include "qprocess_unix.moc" + + +/*********************************************************************** + * + * QProcessPrivate + * + **********************************************************************/ +QProcessManager *QProcessPrivate::procManager = 0; + +QProcessPrivate::QProcessPrivate() +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessPrivate: Constructor" ); +#endif + stdinBufRead = 0; + + notifierStdin = 0; + notifierStdout = 0; + notifierStderr = 0; + + exitValuesCalculated = FALSE; + socketReadCalled = FALSE; + + proc = 0; +} + +QProcessPrivate::~QProcessPrivate() +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcessPrivate: Destructor" ); +#endif + + if ( proc != 0 ) { + if ( proc->socketStdin != 0 ) { + ::close( proc->socketStdin ); + proc->socketStdin = 0; + } + proc->process = 0; + } + + while ( !stdinBuf.isEmpty() ) { + delete stdinBuf.dequeue(); + } + delete notifierStdin; + delete notifierStdout; + delete notifierStderr; +} + +/* + Closes all open sockets in the child process that are not needed by the child + process. Otherwise one child may have an open socket on standard input, etc. + of another child. +*/ +void QProcessPrivate::closeOpenSocketsForChild() +{ + if ( procManager != 0 ) { + if ( procManager->sigchldFd[0] != 0 ) + ::close( procManager->sigchldFd[0] ); + if ( procManager->sigchldFd[1] != 0 ) + ::close( procManager->sigchldFd[1] ); + + // close also the sockets from other QProcess instances + for ( QProc *p=procManager->procList->first(); p!=0; p=procManager->procList->next() ) { + ::close( p->socketStdin ); + ::close( p->socketStdout ); + ::close( p->socketStderr ); + } + } +} + +void QProcessPrivate::newProc( pid_t pid, QProcess *process ) +{ + proc = new QProc( pid, process ); + if ( procManager == 0 ) { + procManager = new QProcessManager; + qAddPostRoutine(qprocess_cleanup); + } + // the QProcessManager takes care of deleting the QProc instances + procManager->append( proc ); +} + +/*********************************************************************** + * + * sigchld handler callback + * + **********************************************************************/ +QT_SIGNAL_RETTYPE qt_C_sigchldHnd( QT_SIGNAL_ARGS ) +{ + if ( QProcessPrivate::procManager == 0 ) + return; + if ( QProcessPrivate::procManager->sigchldFd[0] == 0 ) + return; + + char a = 1; + ::write( QProcessPrivate::procManager->sigchldFd[0], &a, sizeof(a) ); +} + + +/*********************************************************************** + * + * QProcess + * + **********************************************************************/ +/* + This private class does basic initialization. +*/ +void QProcess::init() +{ + d = new QProcessPrivate(); + exitStat = 0; + exitNormal = FALSE; +} + +/* + This private class resets the process variables, etc. so that it can be used + for another process to start. +*/ +void QProcess::reset() +{ + delete d; + d = new QProcessPrivate(); + exitStat = 0; + exitNormal = FALSE; + d->bufStdout.clear(); + d->bufStderr.clear(); +} + +QMembuf* QProcess::membufStdout() +{ + if ( d->proc && d->proc->socketStdout ) { + /* + Apparently, there is not consistency among different + operating systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd argument to + ioctl() to be an int, which is normally 32-bit even on + 64-bit machines. + + IRIX, on the other hand, expects a size_t, which is 64-bit + on 64-bit machines. + + So, the solution is to use size_t initialized to zero to + make sure all bits are set to zero, preventing underflow + with the FreeBSD/Linux/Solaris ioctls. + */ + size_t nbytes = 0; + if ( ::ioctl(d->proc->socketStdout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) + socketRead( d->proc->socketStdout ); + } + return &d->bufStdout; +} + +QMembuf* QProcess::membufStderr() +{ + if ( d->proc && d->proc->socketStderr ) { + /* + Apparently, there is not consistency among different + operating systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd argument to + ioctl() to be an int, which is normally 32-bit even on + 64-bit machines. + + IRIX, on the other hand, expects a size_t, which is 64-bit + on 64-bit machines. + + So, the solution is to use size_t initialized to zero to + make sure all bits are set to zero, preventing underflow + with the FreeBSD/Linux/Solaris ioctls. + */ + size_t nbytes = 0; + if ( ::ioctl(d->proc->socketStderr, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) + socketRead( d->proc->socketStderr ); + } + return &d->bufStderr; +} + +/*! + Destroys the instance. + + If the process is running, it is <b>not</b> terminated! The + standard input, standard output and standard error of the process + are closed. + + You can connect the destroyed() signal to the kill() slot, if you + want the process to be terminated automatically when the instance + is destroyed. + + \sa tryTerminate() kill() +*/ +QProcess::~QProcess() +{ + delete d; +} + +/*! + Tries to run a process for the command and arguments that were + specified with setArguments(), addArgument() or that were + specified in the constructor. The command is searched for in the + path for executable programs; you can also use an absolute path in + the command itself. + + If \a env is null, then the process is started with the same + environment as the starting process. If \a env is non-null, then + the values in the stringlist are interpreted as environment + setttings of the form \c {key=value} and the process is started in + these environment settings. For convenience, there is a small + exception to this rule: under Unix, if \a env does not contain any + settings for the environment variable \c LD_LIBRARY_PATH, then + this variable is inherited from the starting process; under + Windows the same applies for the environment variable \c PATH. + + Returns TRUE if the process could be started; otherwise returns + FALSE. + + You can write data to the process's standard input with + writeToStdin(). You can close standard input with closeStdin() and + you can terminate the process with tryTerminate(), or with kill(). + + You can call this function even if you've used this instance to + create a another process which is still running. In such cases, + QProcess closes the old process's standard input and deletes + pending data, i.e., you lose all control over the old process, but + the old process is not terminated. This applies also if the + process could not be started. (On operating systems that have + zombie processes, Qt will also wait() on the old process.) + + \sa launch() closeStdin() +*/ +bool QProcess::start( QStringList *env ) +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::start()" ); +#endif + reset(); + + int sStdin[2]; + int sStdout[2]; + int sStderr[2]; + + // open sockets for piping +#ifndef Q_OS_QNX6 + if ( (comms & Stdin) && ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStdin ) == -1 ) { +#else + if ( (comms & Stdin) && qnx6SocketPairReplacement(sStdin) == -1 ) { +#endif + return FALSE; + } +#ifndef Q_OS_QNX6 + if ( (comms & Stderr) && ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStderr ) == -1 ) { +#else + if ( (comms & Stderr) && qnx6SocketPairReplacement(sStderr) == -1 ) { +#endif + if ( comms & Stdin ) { + ::close( sStdin[0] ); + ::close( sStdin[1] ); + } + return FALSE; + } +#ifndef Q_OS_QNX6 + if ( (comms & Stdout) && ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStdout ) == -1 ) { +#else + if ( (comms & Stdout) && qnx6SocketPairReplacement(sStdout) == -1 ) { +#endif + if ( comms & Stdin ) { + ::close( sStdin[0] ); + ::close( sStdin[1] ); + } + if ( comms & Stderr ) { + ::close( sStderr[0] ); + ::close( sStderr[1] ); + } + return FALSE; + } + + // the following pipe is only used to determine if the process could be + // started + int fd[2]; + if ( pipe( fd ) < 0 ) { + // non critical error, go on + fd[0] = 0; + fd[1] = 0; + } + + // construct the arguments for exec + QCString *arglistQ = new QCString[ _arguments.count() + 1 ]; + const char** arglist = new const char*[ _arguments.count() + 1 ]; + int i = 0; + for ( QStringList::Iterator it = _arguments.begin(); it != _arguments.end(); ++it ) { + arglistQ[i] = (*it).local8Bit(); + arglist[i] = arglistQ[i]; +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::start(): arg %d = %s", i, arglist[i] ); +#endif + i++; + } +#ifdef Q_OS_MACX + if(i) { + QCString arg_bundle = arglistQ[0]; + QFileInfo fi(arg_bundle); + if(fi.exists() && fi.isDir() && arg_bundle.right(4) == ".app") { + QCString exe = arg_bundle; + int lslash = exe.findRev('/'); + if(lslash != -1) + exe = exe.mid(lslash+1); + exe = QCString(arg_bundle + "/Contents/MacOS/" + exe); + exe = exe.left(exe.length() - 4); //chop off the .app + if(QFile::exists(exe)) { + arglistQ[0] = exe; + arglist[0] = arglistQ[0]; + } + } + } +#endif + arglist[i] = 0; + + // Must make sure signal handlers are installed before exec'ing + // in case the process exits quickly. + if ( d->procManager == 0 ) { + d->procManager = new QProcessManager; + qAddPostRoutine(qprocess_cleanup); + } + + // fork and exec + QApplication::flushX(); + pid_t pid = fork(); + if ( pid == 0 ) { + // child + d->closeOpenSocketsForChild(); + if ( comms & Stdin ) { + ::close( sStdin[1] ); + ::dup2( sStdin[0], STDIN_FILENO ); + } + if ( comms & Stdout ) { + ::close( sStdout[0] ); + ::dup2( sStdout[1], STDOUT_FILENO ); + } + if ( comms & Stderr ) { + ::close( sStderr[0] ); + ::dup2( sStderr[1], STDERR_FILENO ); + } + if ( comms & DupStderr ) { + ::dup2( STDOUT_FILENO, STDERR_FILENO ); + } +#ifndef QT_NO_DIR + ::chdir( workingDir.absPath().latin1() ); +#endif + if ( fd[0] ) + ::close( fd[0] ); + if ( fd[1] ) + ::fcntl( fd[1], F_SETFD, FD_CLOEXEC ); // close on exec shows sucess + + if ( env == 0 ) { // inherit environment and start process +#ifndef Q_OS_QNX4 + ::execvp( arglist[0], (char*const*)arglist ); // ### cast not nice +#else + ::execvp( arglist[0], (char const*const*)arglist ); // ### cast not nice +#endif + } else { // start process with environment settins as specified in env + // construct the environment for exec + int numEntries = env->count(); +#if defined(Q_OS_MACX) + QString ld_library_path("DYLD_LIBRARY_PATH"); +#else + QString ld_library_path("LD_LIBRARY_PATH"); +#endif + bool setLibraryPath = + env->grep( QRegExp( "^" + ld_library_path + "=" ) ).empty() && + getenv( ld_library_path ) != 0; + if ( setLibraryPath ) + numEntries++; + QCString *envlistQ = new QCString[ numEntries + 1 ]; + const char** envlist = new const char*[ numEntries + 1 ]; + int i = 0; + if ( setLibraryPath ) { + envlistQ[i] = QString( ld_library_path + "=%1" ).arg( getenv( ld_library_path ) ).local8Bit(); + envlist[i] = envlistQ[i]; + i++; + } + for ( QStringList::Iterator it = env->begin(); it != env->end(); ++it ) { + envlistQ[i] = (*it).local8Bit(); + envlist[i] = envlistQ[i]; + i++; + } + envlist[i] = 0; + + // look for the executable in the search path + if ( _arguments.count()>0 && getenv("PATH")!=0 ) { + QString command = _arguments[0]; + if ( !command.contains( '/' ) ) { + QStringList pathList = QStringList::split( ':', getenv( "PATH" ) ); + for (QStringList::Iterator it = pathList.begin(); it != pathList.end(); ++it ) { + QString dir = *it; +#if defined(Q_OS_MACX) //look in a bundle + if(!QFile::exists(dir + "/" + command) && QFile::exists(dir + "/" + command + ".app")) + dir += "/" + command + ".app/Contents/MacOS"; +#endif +#ifndef QT_NO_DIR + QFileInfo fileInfo( dir, command ); +#else + QFileInfo fileInfo( dir + "/" + command ); +#endif + if ( fileInfo.isExecutable() ) { +#if defined(Q_OS_MACX) + arglistQ[0] = fileInfo.absFilePath().local8Bit(); +#else + arglistQ[0] = fileInfo.filePath().local8Bit(); +#endif + arglist[0] = arglistQ[0]; + break; + } + } + } + } +#ifndef Q_OS_QNX4 + ::execve( arglist[0], (char*const*)arglist, (char*const*)envlist ); // ### casts not nice +#else + ::execve( arglist[0], (char const*const*)arglist,(char const*const*)envlist ); // ### casts not nice +#endif + } + if ( fd[1] ) { + char buf = 0; + ::write( fd[1], &buf, 1 ); + ::close( fd[1] ); + } + ::_exit( -1 ); + } else if ( pid == -1 ) { + // error forking + goto error; + } + + // test if exec was successful + if ( fd[1] ) + ::close( fd[1] ); + if ( fd[0] ) { + char buf; + for ( ;; ) { + int n = ::read( fd[0], &buf, 1 ); + if ( n==1 ) { + // socket was not closed => error + if ( ::waitpid( pid, 0, WNOHANG ) != pid ) { + // The wait did not succeed yet, so try again when we get + // the sigchild (to avoid zombies). + d->newProc( pid, 0 ); + } + d->proc = 0; + goto error; + } else if ( n==-1 ) { + if ( errno==EAGAIN || errno==EINTR ) + // try it again + continue; + } + break; + } + ::close( fd[0] ); + } + + d->newProc( pid, this ); + + if ( comms & Stdin ) { + ::close( sStdin[0] ); + d->proc->socketStdin = sStdin[1]; + + // Select non-blocking mode + int originalFlags = fcntl(d->proc->socketStdin, F_GETFL, 0); + fcntl(d->proc->socketStdin, F_SETFL, originalFlags | O_NONBLOCK); + + d->notifierStdin = new QSocketNotifier( sStdin[1], QSocketNotifier::Write ); + connect( d->notifierStdin, SIGNAL(activated(int)), + this, SLOT(socketWrite(int)) ); + // setup notifiers for the sockets + if ( !d->stdinBuf.isEmpty() ) { + d->notifierStdin->setEnabled( TRUE ); + } + } + if ( comms & Stdout ) { + ::close( sStdout[1] ); + d->proc->socketStdout = sStdout[0]; + d->notifierStdout = new QSocketNotifier( sStdout[0], QSocketNotifier::Read ); + connect( d->notifierStdout, SIGNAL(activated(int)), + this, SLOT(socketRead(int)) ); + if ( ioRedirection ) + d->notifierStdout->setEnabled( TRUE ); + } + if ( comms & Stderr ) { + ::close( sStderr[1] ); + d->proc->socketStderr = sStderr[0]; + d->notifierStderr = new QSocketNotifier( sStderr[0], QSocketNotifier::Read ); + connect( d->notifierStderr, SIGNAL(activated(int)), + this, SLOT(socketRead(int)) ); + if ( ioRedirection ) + d->notifierStderr->setEnabled( TRUE ); + } + + // cleanup and return + delete[] arglistQ; + delete[] arglist; + return TRUE; + +error: +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::start(): error starting process" ); +#endif + if ( d->procManager ) + d->procManager->cleanup(); + if ( comms & Stdin ) { + ::close( sStdin[1] ); + ::close( sStdin[0] ); + } + if ( comms & Stdout ) { + ::close( sStdout[0] ); + ::close( sStdout[1] ); + } + if ( comms & Stderr ) { + ::close( sStderr[0] ); + ::close( sStderr[1] ); + } + ::close( fd[0] ); + ::close( fd[1] ); + delete[] arglistQ; + delete[] arglist; + return FALSE; +} + + +/*! + Asks the process to terminate. Processes can ignore this if they + wish. If you want to be certain that the process really + terminates, you can use kill() instead. + + The slot returns immediately: it does not wait until the process + has finished. When the process terminates, the processExited() + signal is emitted. + + \sa kill() processExited() +*/ +void QProcess::tryTerminate() const +{ + if ( d->proc != 0 ) + ::kill( d->proc->pid, SIGTERM ); +} + +/*! + Terminates the process. This is not a safe way to end a process + since the process will not be able to do any cleanup. + tryTerminate() is safer, but processes can ignore a + tryTerminate(). + + The nice way to end a process and to be sure that it is finished, + is to do something like this: + \code + process->tryTerminate(); + QTimer::singleShot( 5000, process, SLOT( kill() ) ); + \endcode + + This tries to terminate the process the nice way. If the process + is still running after 5 seconds, it terminates the process the + hard way. The timeout should be chosen depending on the time the + process needs to do all its cleanup: use a higher value if the + process is likely to do a lot of computation or I/O on cleanup. + + The slot returns immediately: it does not wait until the process + has finished. When the process terminates, the processExited() + signal is emitted. + + \sa tryTerminate() processExited() +*/ +void QProcess::kill() const +{ + if ( d->proc != 0 ) + ::kill( d->proc->pid, SIGKILL ); +} + +/*! + Returns TRUE if the process is running; otherwise returns FALSE. + + \sa normalExit() exitStatus() processExited() +*/ +bool QProcess::isRunning() const +{ + if ( d->exitValuesCalculated ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::isRunning(): FALSE (already computed)" ); +#endif + return FALSE; + } + if ( d->proc == 0 ) + return FALSE; + int status; + if ( ::waitpid( d->proc->pid, &status, WNOHANG ) == d->proc->pid ) { + // compute the exit values + QProcess *that = (QProcess*)this; // mutable + that->exitNormal = WIFEXITED( status ) != 0; + if ( exitNormal ) { + that->exitStat = (char)WEXITSTATUS( status ); + } + d->exitValuesCalculated = TRUE; + + // On heavy processing, the socket notifier for the sigchild might not + // have found time to fire yet. + if ( d->procManager && d->procManager->sigchldFd[1] < FD_SETSIZE ) { + fd_set fds; + struct timeval tv; + FD_ZERO( &fds ); + FD_SET( d->procManager->sigchldFd[1], &fds ); + tv.tv_sec = 0; + tv.tv_usec = 0; + if ( ::select( d->procManager->sigchldFd[1]+1, &fds, 0, 0, &tv ) > 0 ) + d->procManager->sigchldHnd( d->procManager->sigchldFd[1] ); + } + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::isRunning() (PID: %d): FALSE", d->proc->pid ); +#endif + return FALSE; + } +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::isRunning() (PID: %d): TRUE", d->proc->pid ); +#endif + return TRUE; +} + +/*! + Returns TRUE if it's possible to read an entire line of text from + standard output at this time; otherwise returns FALSE. + + \sa readLineStdout() canReadLineStderr() +*/ +bool QProcess::canReadLineStdout() const +{ + if ( !d->proc || !d->proc->socketStdout ) + return d->bufStdout.size() != 0; + + QProcess *that = (QProcess*)this; + return that->membufStdout()->scanNewline( 0 ); +} + +/*! + Returns TRUE if it's possible to read an entire line of text from + standard error at this time; otherwise returns FALSE. + + \sa readLineStderr() canReadLineStdout() +*/ +bool QProcess::canReadLineStderr() const +{ + if ( !d->proc || !d->proc->socketStderr ) + return d->bufStderr.size() != 0; + + QProcess *that = (QProcess*)this; + return that->membufStderr()->scanNewline( 0 ); +} + +/*! + Writes the data \a buf to the process's standard input. The + process may or may not read this data. + + This function always returns immediately. The data you + pass to writeToStdin() is copied into an internal memory buffer in + QProcess, and when control goes back to the event loop, QProcess will + starting transferring data from this buffer to the running process. + Sometimes the data will be transferred in several payloads, depending on + how much data is read at a time by the process itself. When QProcess has + transferred all the data from its memory buffer to the running process, it + emits wroteToStdin(). + + Note that some operating systems use a buffer to transfer + the data. As a result, wroteToStdin() may be emitted before the + running process has actually read all the data. + + \sa wroteToStdin() closeStdin() readStdout() readStderr() +*/ +void QProcess::writeToStdin( const QByteArray& buf ) +{ +#if defined(QT_QPROCESS_DEBUG) +// qDebug( "QProcess::writeToStdin(): write to stdin (%d)", d->socketStdin ); +#endif + d->stdinBuf.enqueue( new QByteArray(buf) ); + if ( d->notifierStdin != 0 ) + d->notifierStdin->setEnabled( TRUE ); +} + + +/*! + Closes the process's standard input. + + This function also deletes any pending data that has not been + written to standard input. + + \sa wroteToStdin() +*/ +void QProcess::closeStdin() +{ + if ( d->proc == 0 ) + return; + if ( d->proc->socketStdin !=0 ) { + while ( !d->stdinBuf.isEmpty() ) { + delete d->stdinBuf.dequeue(); + } + delete d->notifierStdin; + d->notifierStdin = 0; + if ( ::close( d->proc->socketStdin ) != 0 ) { + qWarning( "Could not close stdin of child process" ); + } +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::closeStdin(): stdin (%d) closed", d->proc->socketStdin ); +#endif + d->proc->socketStdin = 0; + } +} + + +/* + This private slot is called when the process has outputted data to either + standard output or standard error. +*/ +void QProcess::socketRead( int fd ) +{ + if ( d->socketReadCalled ) { + // the slots that are connected to the readyRead...() signals might + // trigger a recursive call of socketRead(). Avoid this since you get a + // blocking read otherwise. + return; + } + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::socketRead(): %d", fd ); +#endif + if ( fd == 0 ) + return; + if ( !d->proc ) + return; + QMembuf *buffer = 0; + int n; + if ( fd == d->proc->socketStdout ) { + buffer = &d->bufStdout; + } else if ( fd == d->proc->socketStderr ) { + buffer = &d->bufStderr; + } else { + // this case should never happen, but just to be safe + return; + } +#if defined(QT_QPROCESS_DEBUG) + uint oldSize = buffer->size(); +#endif + + // try to read data first (if it fails, the filedescriptor was closed) + const int basize = 4096; + QByteArray *ba = new QByteArray( basize ); + n = ::read( fd, ba->data(), basize ); + if ( n > 0 ) { + ba->resize( n ); + buffer->append( ba ); + ba = 0; + } else { + delete ba; + ba = 0; + } + // eof or error? + if ( n == 0 || n == -1 ) { + if ( fd == d->proc->socketStdout ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::socketRead(): stdout (%d) closed", fd ); +#endif + d->notifierStdout->setEnabled( FALSE ); + delete d->notifierStdout; + d->notifierStdout = 0; + ::close( d->proc->socketStdout ); + d->proc->socketStdout = 0; + return; + } else if ( fd == d->proc->socketStderr ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::socketRead(): stderr (%d) closed", fd ); +#endif + d->notifierStderr->setEnabled( FALSE ); + delete d->notifierStderr; + d->notifierStderr = 0; + ::close( d->proc->socketStderr ); + d->proc->socketStderr = 0; + return; + } + } + + if ( fd < FD_SETSIZE ) { + fd_set fds; + struct timeval tv; + FD_ZERO( &fds ); + FD_SET( fd, &fds ); + tv.tv_sec = 0; + tv.tv_usec = 0; + while ( ::select( fd+1, &fds, 0, 0, &tv ) > 0 ) { + // prepare for the next round + FD_ZERO( &fds ); + FD_SET( fd, &fds ); + // read data + ba = new QByteArray( basize ); + n = ::read( fd, ba->data(), basize ); + if ( n > 0 ) { + ba->resize( n ); + buffer->append( ba ); + ba = 0; + } else { + delete ba; + ba = 0; + break; + } + } + } + + d->socketReadCalled = TRUE; + if ( fd == d->proc->socketStdout ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::socketRead(): %d bytes read from stdout (%d)", + buffer->size()-oldSize, fd ); +#endif + emit readyReadStdout(); + } else if ( fd == d->proc->socketStderr ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::socketRead(): %d bytes read from stderr (%d)", + buffer->size()-oldSize, fd ); +#endif + emit readyReadStderr(); + } + d->socketReadCalled = FALSE; +} + + +/* + This private slot is called when the process tries to read data from standard + input. +*/ +void QProcess::socketWrite( int fd ) +{ + while ( fd == d->proc->socketStdin && d->proc->socketStdin != 0 ) { + if ( d->stdinBuf.isEmpty() ) { + d->notifierStdin->setEnabled( FALSE ); + return; + } + ssize_t ret = ::write( fd, + d->stdinBuf.head()->data() + d->stdinBufRead, + d->stdinBuf.head()->size() - d->stdinBufRead ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "QProcess::socketWrite(): wrote %d bytes to stdin (%d)", ret, fd ); +#endif + if ( ret == -1 ) + return; + d->stdinBufRead += ret; + if ( d->stdinBufRead == (ssize_t)d->stdinBuf.head()->size() ) { + d->stdinBufRead = 0; + delete d->stdinBuf.dequeue(); + if ( wroteToStdinConnected && d->stdinBuf.isEmpty() ) + emit wroteToStdin(); + } + } +} + +/*! + \internal + Flushes standard input. This is useful if you want to use QProcess in a + synchronous manner. + + This function should probably go into the public API. +*/ +void QProcess::flushStdin() +{ + if (d->proc) + socketWrite(d->proc->socketStdin); +} + +/* + This private slot is only used under Windows (but moc does not know about #if + defined()). +*/ +void QProcess::timeout() +{ +} + + +/* + This private function is used by connectNotify() and disconnectNotify() to + change the value of ioRedirection (and related behaviour) +*/ +void QProcess::setIoRedirection( bool value ) +{ + ioRedirection = value; + if ( ioRedirection ) { + if ( d->notifierStdout ) + d->notifierStdout->setEnabled( TRUE ); + if ( d->notifierStderr ) + d->notifierStderr->setEnabled( TRUE ); + } else { + if ( d->notifierStdout ) + d->notifierStdout->setEnabled( FALSE ); + if ( d->notifierStderr ) + d->notifierStderr->setEnabled( FALSE ); + } +} + +/* + This private function is used by connectNotify() and + disconnectNotify() to change the value of notifyOnExit (and related + behaviour) +*/ +void QProcess::setNotifyOnExit( bool value ) +{ + notifyOnExit = value; +} + +/* + This private function is used by connectNotify() and disconnectNotify() to + change the value of wroteToStdinConnected (and related behaviour) +*/ +void QProcess::setWroteStdinConnected( bool value ) +{ + wroteToStdinConnected = value; +} + +/*! \enum QProcess::PID + \internal +*/ +/*! + Returns platform dependent information about the process. This can + be used together with platform specific system calls. + + Under Unix the return value is the PID of the process, or -1 if no + process belongs to this object. + + Under Windows it is a pointer to the \c PROCESS_INFORMATION + struct, or 0 if no process is belongs to this object. + + Use of this function's return value is likely to be non-portable. +*/ +QProcess::PID QProcess::processIdentifier() +{ + if ( d->proc == 0 ) + return -1; + return d->proc->pid; +} + +#endif // QT_NO_PROCESS diff --git a/src/kernel/qpsprinter.cpp b/src/kernel/qpsprinter.cpp new file mode 100644 index 0000000..039b587 --- /dev/null +++ b/src/kernel/qpsprinter.cpp @@ -0,0 +1,6582 @@ +/********************************************************************** +** +** Implementation of QPSPrinter class +** +** Created : 941003 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +#include "qpsprinter_p.h" + +#ifndef QT_NO_PRINTER + +#undef Q_PRINTER_USE_TYPE42 + +#include "qpainter.h" +#include "qapplication.h" +#include "qpaintdevicemetrics.h" +#include "qimage.h" +#include "qdatetime.h" +#include "qstring.h" +#include "qdict.h" +#include "qmemarray.h" +#include "qfile.h" +#include "qbuffer.h" +#include "qintdict.h" +#include "qtextcodec.h" +#include "qsettings.h" +#include "qmap.h" +#include "qfontdatabase.h" +#include "qregexp.h" +#include "qbitmap.h" +#include <private/qunicodetables_p.h> + +#if defined(Q_OS_WIN32) +#include <io.h> +#ifdef Q_PRINTER_USE_TYPE42 +#include <stdlib.h> +#endif +#else +#include <unistd.h> +#include <stdlib.h> +#endif + +#ifdef Q_WS_X11 +#include "qt_x11_p.h" +#ifdef None +#undef None +#endif +#ifdef GrayScale +#undef GrayScale +#endif +#endif + +#if defined( Q_WS_X11 ) || defined (Q_WS_QWS) +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qtextlayout_p.h" +#include "qtextengine_p.h" +extern bool qt_has_xft; +#endif + +static bool qt_gen_epsf = FALSE; +static bool embedFonts = TRUE; + +Q_EXPORT void qt_generate_epsf( bool b ) +{ + qt_gen_epsf = b; +} + +static const char *const ps_header = +"/d/def load def/D{bind d}bind d/d2{dup dup}D/B{0 d2}D/W{255 d2}D/ED{exch d}D\n" +"/D0{0 ED}D/LT{lineto}D/MT{moveto}D/S{stroke}D/F{setfont}D/SW{setlinewidth}D\n" +"/CP{closepath}D/RL{rlineto}D/NP{newpath}D/CM{currentmatrix}D/SM{setmatrix}D\n" +"/TR{translate}D/SD{setdash}D/SC{aload pop setrgbcolor}D/CR{currentfile read\n" +"pop}D/i{index}D/bs{bitshift}D/scs{setcolorspace}D/DB{dict dup begin}D/DE{end\n" +"d}D/ie{ifelse}D/sp{astore pop}D/BSt 0 d/LWi 1 d/PSt 1 d/Cx 0 d/Cy 0 d/WFi\n" +"false d/OMo false d/BCol[1 1 1]d/PCol[0 0 0]d/BkCol[1 1 1]d/BDArr[0.94 0.88\n" +"0.63 0.50 0.37 0.12 0.06]d/defM matrix d/nS 0 d/GPS{PSt 1 ge PSt 5 le and{{\n" +"LArr PSt 1 sub 2 mul get}{LArr PSt 2 mul 1 sub get}ie}{[]}ie}D/QS{PSt 0 ne{\n" +"gsave LWi SW true GPS 0 SD S OMo PSt 1 ne and{BkCol SC false GPS dup 0 get\n" +"SD S}if grestore}if}D/r28{{CR dup 32 gt{exit}if pop}loop 3{CR}repeat 0 4{7\n" +"bs exch dup 128 gt{84 sub}if 42 sub 127 and add}repeat}D/rA 0 d/rL 0 d/rB{rL\n" +"0 eq{/rA r28 d/rL 28 d}if dup rL gt{rA exch rL sub rL exch/rA 0 d/rL 0 d rB\n" +"exch bs add}{dup rA 16#fffffff 3 -1 roll bs not and exch dup rL exch sub/rL\n" +"ED neg rA exch bs/rA ED}ie}D/uc{/rL 0 d 0{dup 2 i length ge{exit}if 1 rB 1\n" +"eq{3 rB dup 3 ge{1 add dup rB 1 i 5 ge{1 i 6 ge{1 i 7 ge{1 i 8 ge{128 add}if\n" +"64 add}if 32 add}if 16 add}if 3 add exch pop}if 3 add exch 10 rB 1 add{dup 3\n" +"i lt{dup}{2 i}ie 4 i 3 i 3 i sub 2 i getinterval 5 i 4 i 3 -1 roll\n" +"putinterval dup 4 -1 roll add 3 1 roll 4 -1 roll exch sub dup 0 eq{exit}if 3\n" +"1 roll}loop pop pop}{3 rB 1 add{2 copy 8 rB put 1 add}repeat}ie}loop pop}D\n" +"/sl D0/QCIgray D0/QCIcolor D0/QCIindex D0/QCI{/colorimage where{pop false 3\n" +"colorimage}{exec/QCIcolor ED/QCIgray QCIcolor length 3 idiv string d 0 1\n" +"QCIcolor length 3 idiv 1 sub{/QCIindex ED/x QCIindex 3 mul d QCIgray\n" +"QCIindex QCIcolor x get 0.30 mul QCIcolor x 1 add get 0.59 mul QCIcolor x 2\n" +"add get 0.11 mul add add cvi put}for QCIgray image}ie}D/di{gsave TR 1 i 1 eq\n" +"{false eq{pop true 3 1 roll 4 i 4 i false 4 i 4 i imagemask BkCol SC\n" +"imagemask}{pop false 3 1 roll imagemask}ie}{dup false ne{/languagelevel\n" +"where{pop languagelevel 3 ge}{false}ie}{false}ie{/ma ED 8 eq{/dc[0 1]d\n" +"/DeviceGray}{/dc[0 1 0 1 0 1]d/DeviceRGB}ie scs/im ED/mt ED/h ED/w ED/id 7\n" +"DB/ImageType 1 d/Width w d/Height h d/ImageMatrix mt d/DataSource im d\n" +"/BitsPerComponent 8 d/Decode dc d DE/md 7 DB/ImageType 1 d/Width w d/Height\n" +"h d/ImageMatrix mt d/DataSource ma d/BitsPerComponent 1 d/Decode[0 1]d DE 4\n" +"DB/ImageType 3 d/DataDict id d/MaskDict md d/InterleaveType 3 d end image}{\n" +"pop 8 4 1 roll 8 eq{image}{QCI}ie}ie}ie grestore}d/BF{gsave BSt 1 eq{BCol SC\n" +"WFi{fill}{eofill}ie}if BSt 2 ge BSt 8 le and{BDArr BSt 2 sub get/sc ED BCol{\n" +"1. exch sub sc mul 1. exch sub}forall 3 array astore SC WFi{fill}{eofill}ie}\n" +"if BSt 9 ge BSt 14 le and{WFi{clip}{eoclip}ie defM SM pathbbox 3 i 3 i TR 4\n" +"2 roll 3 2 roll exch sub/h ED sub/w ED OMo{NP 0 0 MT 0 h RL w 0 RL 0 h neg\n" +"RL CP BkCol SC fill}if BCol SC 0.3 SW NP BSt 9 eq BSt 11 eq or{0 4 h{dup 0\n" +"exch MT w exch LT}for}if BSt 10 eq BSt 11 eq or{0 4 w{dup 0 MT h LT}for}if\n" +"BSt 12 eq BSt 14 eq or{w h gt{0 6 w h add{dup 0 MT h sub h LT}for}{0 6 w h\n" +"add{dup 0 exch MT w sub w exch LT}for}ie}if BSt 13 eq BSt 14 eq or{w h gt{0\n" +"6 w h add{dup h MT h sub 0 LT}for}{0 6 w h add{dup w exch MT w sub 0 exch LT\n" +"}for}ie}if S}if BSt 24 eq{}if grestore}D/mat matrix d/ang1 D0/ang2 D0/w D0/h\n" +"D0/x D0/y D0/ARC{/ang2 ED/ang1 ED/h ED/w ED/y ED/x ED mat CM pop x w 2 div\n" +"add y h 2 div add TR 1 h w div neg scale ang2 0 ge{0 0 w 2 div ang1 ang1\n" +"ang2 add arc}{0 0 w 2 div ang1 ang1 ang2 add arcn}ie mat SM}D/C D0/P{NP MT\n" +"0.5 0.5 rmoveto 0 -1 RL -1 0 RL 0 1 RL CP fill}D/M{/Cy ED/Cx ED}D/L{NP Cx Cy\n" +"MT/Cy ED/Cx ED Cx Cy LT QS}D/DL{NP MT LT QS}D/HL{1 i DL}D/VL{2 i exch DL}D/R\n" +"{/h ED/w ED/y ED/x ED NP x y MT 0 h RL w 0 RL 0 h neg RL CP BF QS}D/ACR{/h\n" +"ED/w ED/y ED/x ED x y MT 0 h RL w 0 RL 0 h neg RL CP}D/xr D0/yr D0/rx D0/ry\n" +"D0/rx2 D0/ry2 D0/RR{/yr ED/xr ED/h ED/w ED/y ED/x ED xr 0 le yr 0 le or{x y\n" +"w h R}{xr 100 ge yr 100 ge or{x y w h E}{/rx xr w mul 200 div d/ry yr h mul\n" +"200 div d/rx2 rx 2 mul d/ry2 ry 2 mul d NP x rx add y MT x y rx2 ry2 180 -90\n" +"x y h add ry2 sub rx2 ry2 270 -90 x w add rx2 sub y h add ry2 sub rx2 ry2 0\n" +"-90 x w add rx2 sub y rx2 ry2 90 -90 ARC ARC ARC ARC CP BF QS}ie}ie}D/E{/h\n" +"ED/w ED/y ED/x ED mat CM pop x w 2 div add y h 2 div add TR 1 h w div scale\n" +"NP 0 0 w 2 div 0 360 arc mat SM BF QS}D/A{16 div exch 16 div exch NP ARC QS}\n" +"D/PIE{/ang2 ED/ang1 ED/h ED/w ED/y ED/x ED NP x w 2 div add y h 2 div add MT\n" +"x y w h ang1 16 div ang2 16 div ARC CP BF QS}D/CH{16 div exch 16 div exch NP\n" +"ARC CP BF QS}D/BZ{curveto QS}D/CRGB{255 div 3 1 roll 255 div 3 1 roll 255\n" +"div 3 1 roll}D/BC{CRGB BkCol sp}D/BR{CRGB BCol sp/BSt ED}D/WB{1 W BR}D/NB{0\n" +"B BR}D/PE{setlinejoin setlinecap CRGB PCol sp/LWi ED/PSt ED LWi 0 eq{0.25\n" +"/LWi ED}if PCol SC}D/P1{1 0 5 2 roll 0 0 PE}D/ST{defM SM concat}D/MF{true\n" +"exch true exch{exch pop exch pop dup 0 get dup findfont dup/FontName get 3\n" +"-1 roll eq{exit}if}forall exch dup 1 get/fxscale ED 2 get/fslant ED exch\n" +"/fencoding ED[fxscale 0 fslant 1 0 0]makefont fencoding false eq{}{dup\n" +"maxlength dict begin{1 i/FID ne{def}{pop pop}ifelse}forall/Encoding\n" +"fencoding d currentdict end}ie definefont pop}D/MFEmb{findfont dup length\n" +"dict begin{1 i/FID ne{d}{pop pop}ifelse}forall/Encoding ED currentdict end\n" +"definefont pop}D/DF{findfont/fs 3 -1 roll d[fs 0 0 fs -1 mul 0 0]makefont d}\n" +"D/ty 0 d/Y{/ty ED}D/Tl{gsave SW NP 1 i exch MT 1 i 0 RL S grestore}D/XYT{ty\n" +"MT/xyshow where{pop pop xyshow}{exch pop 1 i dup length 2 div exch\n" +"stringwidth pop 3 -1 roll exch sub exch div exch 0 exch ashow}ie}D/AT{ty MT\n" +"1 i dup length 2 div exch stringwidth pop 3 -1 roll exch sub exch div exch 0\n" +"exch ashow}D/QI{/C save d pageinit/Cx 0 d/Cy 0 d/OMo false d}D/QP{C restore\n" +"showpage}D/SPD{/setpagedevice where{1 DB 3 1 roll d end setpagedevice}{pop\n" +"pop}ie}D/SV{BSt LWi PSt Cx Cy WFi OMo BCol PCol BkCol/nS nS 1 add d gsave}D\n" +"/RS{nS 0 gt{grestore/BkCol ED/PCol ED/BCol ED/OMo ED/WFi ED/Cy ED/Cx ED/PSt\n" +"ED/LWi ED/BSt ED/nS nS 1 sub d}if}D/CLSTART{/clipTmp matrix CM d defM SM NP}\n" +"D/CLEND{clip NP clipTmp SM}D/CLO{grestore gsave defM SM}D\n"; + +// the next table is derived from a list provided by Adobe on its web +// server: http://partners.adobe.com/asn/developer/typeforum/glyphlist.txt + +// the start of the header comment: +// +// Name: Adobe Glyph List +// Table version: 1.2 +// Date: 22 Oct 1998 +// +// Description: +// +// The Adobe Glyph List (AGL) list relates Unicode values (UVs) to glyph +// names, and should be used only as described in the document "Unicode and +// Glyph Names," at +// http://partners.adobe.com:80/asn/developer/type/unicodegn.html +// +// IMPORTANT NOTE: +// the list contains glyphs in the private use area of unicode. These should get removed when regenerating the glyphlist. +// also 0 shout be mapped to .notdef +static const struct { + Q_UINT16 u; + const char * g; +} unicodetoglyph[] = { + // grep '^[0-9A-F][0-9A-F][0-9A-F][0-9A-F];' < /tmp/glyphlist.txt | sed -e 's/;/, "/' -e 's-;-" }, // -' -e 's/^/ { 0x/' | sort + { 0x0000, ".notdef" }, + { 0x0020, "space" }, // SPACE + { 0x0021, "exclam" }, // EXCLAMATION MARK + { 0x0022, "quotedbl" }, // QUOTATION MARK + { 0x0023, "numbersign" }, // NUMBER SIGN + { 0x0024, "dollar" }, // DOLLAR SIGN + { 0x0025, "percent" }, // PERCENT SIGN + { 0x0026, "ampersand" }, // AMPERSAND + { 0x0027, "quotesingle" }, // APOSTROPHE + { 0x0028, "parenleft" }, // LEFT PARENTHESIS + { 0x0029, "parenright" }, // RIGHT PARENTHESIS + { 0x002A, "asterisk" }, // ASTERISK + { 0x002B, "plus" }, // PLUS SIGN + { 0x002C, "comma" }, // COMMA + { 0x002D, "hyphen" }, // HYPHEN-MINUS + { 0x002E, "period" }, // FULL STOP + { 0x002F, "slash" }, // SOLIDUS + { 0x0030, "zero" }, // DIGIT ZERO + { 0x0031, "one" }, // DIGIT ONE + { 0x0032, "two" }, // DIGIT TWO + { 0x0033, "three" }, // DIGIT THREE + { 0x0034, "four" }, // DIGIT FOUR + { 0x0035, "five" }, // DIGIT FIVE + { 0x0036, "six" }, // DIGIT SIX + { 0x0037, "seven" }, // DIGIT SEVEN + { 0x0038, "eight" }, // DIGIT EIGHT + { 0x0039, "nine" }, // DIGIT NINE + { 0x003A, "colon" }, // COLON + { 0x003B, "semicolon" }, // SEMICOLON + { 0x003C, "less" }, // LESS-THAN SIGN + { 0x003D, "equal" }, // EQUALS SIGN + { 0x003E, "greater" }, // GREATER-THAN SIGN + { 0x003F, "question" }, // QUESTION MARK + { 0x0040, "at" }, // COMMERCIAL AT + { 0x0041, "A" }, // LATIN CAPITAL LETTER A + { 0x0042, "B" }, // LATIN CAPITAL LETTER B + { 0x0043, "C" }, // LATIN CAPITAL LETTER C + { 0x0044, "D" }, // LATIN CAPITAL LETTER D + { 0x0045, "E" }, // LATIN CAPITAL LETTER E + { 0x0046, "F" }, // LATIN CAPITAL LETTER F + { 0x0047, "G" }, // LATIN CAPITAL LETTER G + { 0x0048, "H" }, // LATIN CAPITAL LETTER H + { 0x0049, "I" }, // LATIN CAPITAL LETTER I + { 0x004A, "J" }, // LATIN CAPITAL LETTER J + { 0x004B, "K" }, // LATIN CAPITAL LETTER K + { 0x004C, "L" }, // LATIN CAPITAL LETTER L + { 0x004D, "M" }, // LATIN CAPITAL LETTER M + { 0x004E, "N" }, // LATIN CAPITAL LETTER N + { 0x004F, "O" }, // LATIN CAPITAL LETTER O + { 0x0050, "P" }, // LATIN CAPITAL LETTER P + { 0x0051, "Q" }, // LATIN CAPITAL LETTER Q + { 0x0052, "R" }, // LATIN CAPITAL LETTER R + { 0x0053, "S" }, // LATIN CAPITAL LETTER S + { 0x0054, "T" }, // LATIN CAPITAL LETTER T + { 0x0055, "U" }, // LATIN CAPITAL LETTER U + { 0x0056, "V" }, // LATIN CAPITAL LETTER V + { 0x0057, "W" }, // LATIN CAPITAL LETTER W + { 0x0058, "X" }, // LATIN CAPITAL LETTER X + { 0x0059, "Y" }, // LATIN CAPITAL LETTER Y + { 0x005A, "Z" }, // LATIN CAPITAL LETTER Z + { 0x005B, "bracketleft" }, // LEFT SQUARE BRACKET + { 0x005C, "backslash" }, // REVERSE SOLIDUS + { 0x005D, "bracketright" }, // RIGHT SQUARE BRACKET + { 0x005E, "asciicircum" }, // CIRCUMFLEX ACCENT + { 0x005F, "underscore" }, // LOW LINE + { 0x0060, "grave" }, // GRAVE ACCENT + { 0x0061, "a" }, // LATIN SMALL LETTER A + { 0x0062, "b" }, // LATIN SMALL LETTER B + { 0x0063, "c" }, // LATIN SMALL LETTER C + { 0x0064, "d" }, // LATIN SMALL LETTER D + { 0x0065, "e" }, // LATIN SMALL LETTER E + { 0x0066, "f" }, // LATIN SMALL LETTER F + { 0x0067, "g" }, // LATIN SMALL LETTER G + { 0x0068, "h" }, // LATIN SMALL LETTER H + { 0x0069, "i" }, // LATIN SMALL LETTER I + { 0x006A, "j" }, // LATIN SMALL LETTER J + { 0x006B, "k" }, // LATIN SMALL LETTER K + { 0x006C, "l" }, // LATIN SMALL LETTER L + { 0x006D, "m" }, // LATIN SMALL LETTER M + { 0x006E, "n" }, // LATIN SMALL LETTER N + { 0x006F, "o" }, // LATIN SMALL LETTER O + { 0x0070, "p" }, // LATIN SMALL LETTER P + { 0x0071, "q" }, // LATIN SMALL LETTER Q + { 0x0072, "r" }, // LATIN SMALL LETTER R + { 0x0073, "s" }, // LATIN SMALL LETTER S + { 0x0074, "t" }, // LATIN SMALL LETTER T + { 0x0075, "u" }, // LATIN SMALL LETTER U + { 0x0076, "v" }, // LATIN SMALL LETTER V + { 0x0077, "w" }, // LATIN SMALL LETTER W + { 0x0078, "x" }, // LATIN SMALL LETTER X + { 0x0079, "y" }, // LATIN SMALL LETTER Y + { 0x007A, "z" }, // LATIN SMALL LETTER Z + { 0x007B, "braceleft" }, // LEFT CURLY BRACKET + { 0x007C, "bar" }, // VERTICAL LINE + { 0x007D, "braceright" }, // RIGHT CURLY BRACKET + { 0x007E, "asciitilde" }, // TILDE + { 0x00A0, "space" }, // NO-BREAK SPACE;Duplicate + { 0x00A1, "exclamdown" }, // INVERTED EXCLAMATION MARK + { 0x00A2, "cent" }, // CENT SIGN + { 0x00A3, "sterling" }, // POUND SIGN + { 0x00A4, "currency" }, // CURRENCY SIGN + { 0x00A5, "yen" }, // YEN SIGN + { 0x00A6, "brokenbar" }, // BROKEN BAR + { 0x00A7, "section" }, // SECTION SIGN + { 0x00A8, "dieresis" }, // DIAERESIS + { 0x00A9, "copyright" }, // COPYRIGHT SIGN + { 0x00AA, "ordfeminine" }, // FEMININE ORDINAL INDICATOR + { 0x00AB, "guillemotleft" }, // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK + { 0x00AC, "logicalnot" }, // NOT SIGN + { 0x00AD, "hyphen" }, // SOFT HYPHEN;Duplicate + { 0x00AE, "registered" }, // REGISTERED SIGN + { 0x00AF, "macron" }, // MACRON + { 0x00B0, "degree" }, // DEGREE SIGN + { 0x00B1, "plusminus" }, // PLUS-MINUS SIGN + { 0x00B2, "twosuperior" }, // SUPERSCRIPT TWO + { 0x00B3, "threesuperior" }, // SUPERSCRIPT THREE + { 0x00B4, "acute" }, // ACUTE ACCENT + { 0x00B5, "mu" }, // MICRO SIGN + { 0x00B6, "paragraph" }, // PILCROW SIGN + { 0x00B7, "periodcentered" }, // MIDDLE DOT + { 0x00B8, "cedilla" }, // CEDILLA + { 0x00B9, "onesuperior" }, // SUPERSCRIPT ONE + { 0x00BA, "ordmasculine" }, // MASCULINE ORDINAL INDICATOR + { 0x00BB, "guillemotright" }, // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK + { 0x00BC, "onequarter" }, // VULGAR FRACTION ONE QUARTER + { 0x00BD, "onehalf" }, // VULGAR FRACTION ONE HALF + { 0x00BE, "threequarters" }, // VULGAR FRACTION THREE QUARTERS + { 0x00BF, "questiondown" }, // INVERTED QUESTION MARK + { 0x00C0, "Agrave" }, // LATIN CAPITAL LETTER A WITH GRAVE + { 0x00C1, "Aacute" }, // LATIN CAPITAL LETTER A WITH ACUTE + { 0x00C2, "Acircumflex" }, // LATIN CAPITAL LETTER A WITH CIRCUMFLEX + { 0x00C3, "Atilde" }, // LATIN CAPITAL LETTER A WITH TILDE + { 0x00C4, "Adieresis" }, // LATIN CAPITAL LETTER A WITH DIAERESIS + { 0x00C5, "Aring" }, // LATIN CAPITAL LETTER A WITH RING ABOVE + { 0x00C6, "AE" }, // LATIN CAPITAL LETTER AE + { 0x00C7, "Ccedilla" }, // LATIN CAPITAL LETTER C WITH CEDILLA + { 0x00C8, "Egrave" }, // LATIN CAPITAL LETTER E WITH GRAVE + { 0x00C9, "Eacute" }, // LATIN CAPITAL LETTER E WITH ACUTE + { 0x00CA, "Ecircumflex" }, // LATIN CAPITAL LETTER E WITH CIRCUMFLEX + { 0x00CB, "Edieresis" }, // LATIN CAPITAL LETTER E WITH DIAERESIS + { 0x00CC, "Igrave" }, // LATIN CAPITAL LETTER I WITH GRAVE + { 0x00CD, "Iacute" }, // LATIN CAPITAL LETTER I WITH ACUTE + { 0x00CE, "Icircumflex" }, // LATIN CAPITAL LETTER I WITH CIRCUMFLEX + { 0x00CF, "Idieresis" }, // LATIN CAPITAL LETTER I WITH DIAERESIS + { 0x00D0, "Eth" }, // LATIN CAPITAL LETTER ETH + { 0x00D1, "Ntilde" }, // LATIN CAPITAL LETTER N WITH TILDE + { 0x00D2, "Ograve" }, // LATIN CAPITAL LETTER O WITH GRAVE + { 0x00D3, "Oacute" }, // LATIN CAPITAL LETTER O WITH ACUTE + { 0x00D4, "Ocircumflex" }, // LATIN CAPITAL LETTER O WITH CIRCUMFLEX + { 0x00D5, "Otilde" }, // LATIN CAPITAL LETTER O WITH TILDE + { 0x00D6, "Odieresis" }, // LATIN CAPITAL LETTER O WITH DIAERESIS + { 0x00D7, "multiply" }, // MULTIPLICATION SIGN + { 0x00D8, "Oslash" }, // LATIN CAPITAL LETTER O WITH STROKE + { 0x00D9, "Ugrave" }, // LATIN CAPITAL LETTER U WITH GRAVE + { 0x00DA, "Uacute" }, // LATIN CAPITAL LETTER U WITH ACUTE + { 0x00DB, "Ucircumflex" }, // LATIN CAPITAL LETTER U WITH CIRCUMFLEX + { 0x00DC, "Udieresis" }, // LATIN CAPITAL LETTER U WITH DIAERESIS + { 0x00DD, "Yacute" }, // LATIN CAPITAL LETTER Y WITH ACUTE + { 0x00DE, "Thorn" }, // LATIN CAPITAL LETTER THORN + { 0x00DF, "germandbls" }, // LATIN SMALL LETTER SHARP S + { 0x00E0, "agrave" }, // LATIN SMALL LETTER A WITH GRAVE + { 0x00E1, "aacute" }, // LATIN SMALL LETTER A WITH ACUTE + { 0x00E2, "acircumflex" }, // LATIN SMALL LETTER A WITH CIRCUMFLEX + { 0x00E3, "atilde" }, // LATIN SMALL LETTER A WITH TILDE + { 0x00E4, "adieresis" }, // LATIN SMALL LETTER A WITH DIAERESIS + { 0x00E5, "aring" }, // LATIN SMALL LETTER A WITH RING ABOVE + { 0x00E6, "ae" }, // LATIN SMALL LETTER AE + { 0x00E7, "ccedilla" }, // LATIN SMALL LETTER C WITH CEDILLA + { 0x00E8, "egrave" }, // LATIN SMALL LETTER E WITH GRAVE + { 0x00E9, "eacute" }, // LATIN SMALL LETTER E WITH ACUTE + { 0x00EA, "ecircumflex" }, // LATIN SMALL LETTER E WITH CIRCUMFLEX + { 0x00EB, "edieresis" }, // LATIN SMALL LETTER E WITH DIAERESIS + { 0x00EC, "igrave" }, // LATIN SMALL LETTER I WITH GRAVE + { 0x00ED, "iacute" }, // LATIN SMALL LETTER I WITH ACUTE + { 0x00EE, "icircumflex" }, // LATIN SMALL LETTER I WITH CIRCUMFLEX + { 0x00EF, "idieresis" }, // LATIN SMALL LETTER I WITH DIAERESIS + { 0x00F0, "eth" }, // LATIN SMALL LETTER ETH + { 0x00F1, "ntilde" }, // LATIN SMALL LETTER N WITH TILDE + { 0x00F2, "ograve" }, // LATIN SMALL LETTER O WITH GRAVE + { 0x00F3, "oacute" }, // LATIN SMALL LETTER O WITH ACUTE + { 0x00F4, "ocircumflex" }, // LATIN SMALL LETTER O WITH CIRCUMFLEX + { 0x00F5, "otilde" }, // LATIN SMALL LETTER O WITH TILDE + { 0x00F6, "odieresis" }, // LATIN SMALL LETTER O WITH DIAERESIS + { 0x00F7, "divide" }, // DIVISION SIGN + { 0x00F8, "oslash" }, // LATIN SMALL LETTER O WITH STROKE + { 0x00F9, "ugrave" }, // LATIN SMALL LETTER U WITH GRAVE + { 0x00FA, "uacute" }, // LATIN SMALL LETTER U WITH ACUTE + { 0x00FB, "ucircumflex" }, // LATIN SMALL LETTER U WITH CIRCUMFLEX + { 0x00FC, "udieresis" }, // LATIN SMALL LETTER U WITH DIAERESIS + { 0x00FD, "yacute" }, // LATIN SMALL LETTER Y WITH ACUTE + { 0x00FE, "thorn" }, // LATIN SMALL LETTER THORN + { 0x00FF, "ydieresis" }, // LATIN SMALL LETTER Y WITH DIAERESIS + { 0x0100, "Amacron" }, // LATIN CAPITAL LETTER A WITH MACRON + { 0x0101, "amacron" }, // LATIN SMALL LETTER A WITH MACRON + { 0x0102, "Abreve" }, // LATIN CAPITAL LETTER A WITH BREVE + { 0x0103, "abreve" }, // LATIN SMALL LETTER A WITH BREVE + { 0x0104, "Aogonek" }, // LATIN CAPITAL LETTER A WITH OGONEK + { 0x0105, "aogonek" }, // LATIN SMALL LETTER A WITH OGONEK + { 0x0106, "Cacute" }, // LATIN CAPITAL LETTER C WITH ACUTE + { 0x0107, "cacute" }, // LATIN SMALL LETTER C WITH ACUTE + { 0x0108, "Ccircumflex" }, // LATIN CAPITAL LETTER C WITH CIRCUMFLEX + { 0x0109, "ccircumflex" }, // LATIN SMALL LETTER C WITH CIRCUMFLEX + { 0x010A, "Cdotaccent" }, // LATIN CAPITAL LETTER C WITH DOT ABOVE + { 0x010B, "cdotaccent" }, // LATIN SMALL LETTER C WITH DOT ABOVE + { 0x010C, "Ccaron" }, // LATIN CAPITAL LETTER C WITH CARON + { 0x010D, "ccaron" }, // LATIN SMALL LETTER C WITH CARON + { 0x010E, "Dcaron" }, // LATIN CAPITAL LETTER D WITH CARON + { 0x010F, "dcaron" }, // LATIN SMALL LETTER D WITH CARON + { 0x0110, "Dcroat" }, // LATIN CAPITAL LETTER D WITH STROKE + { 0x0111, "dcroat" }, // LATIN SMALL LETTER D WITH STROKE + { 0x0112, "Emacron" }, // LATIN CAPITAL LETTER E WITH MACRON + { 0x0113, "emacron" }, // LATIN SMALL LETTER E WITH MACRON + { 0x0114, "Ebreve" }, // LATIN CAPITAL LETTER E WITH BREVE + { 0x0115, "ebreve" }, // LATIN SMALL LETTER E WITH BREVE + { 0x0116, "Edotaccent" }, // LATIN CAPITAL LETTER E WITH DOT ABOVE + { 0x0117, "edotaccent" }, // LATIN SMALL LETTER E WITH DOT ABOVE + { 0x0118, "Eogonek" }, // LATIN CAPITAL LETTER E WITH OGONEK + { 0x0119, "eogonek" }, // LATIN SMALL LETTER E WITH OGONEK + { 0x011A, "Ecaron" }, // LATIN CAPITAL LETTER E WITH CARON + { 0x011B, "ecaron" }, // LATIN SMALL LETTER E WITH CARON + { 0x011C, "Gcircumflex" }, // LATIN CAPITAL LETTER G WITH CIRCUMFLEX + { 0x011D, "gcircumflex" }, // LATIN SMALL LETTER G WITH CIRCUMFLEX + { 0x011E, "Gbreve" }, // LATIN CAPITAL LETTER G WITH BREVE + { 0x011F, "gbreve" }, // LATIN SMALL LETTER G WITH BREVE + { 0x0120, "Gdotaccent" }, // LATIN CAPITAL LETTER G WITH DOT ABOVE + { 0x0121, "gdotaccent" }, // LATIN SMALL LETTER G WITH DOT ABOVE + { 0x0122, "Gcommaaccent" }, // LATIN CAPITAL LETTER G WITH CEDILLA + { 0x0123, "gcommaaccent" }, // LATIN SMALL LETTER G WITH CEDILLA + { 0x0124, "Hcircumflex" }, // LATIN CAPITAL LETTER H WITH CIRCUMFLEX + { 0x0125, "hcircumflex" }, // LATIN SMALL LETTER H WITH CIRCUMFLEX + { 0x0126, "Hbar" }, // LATIN CAPITAL LETTER H WITH STROKE + { 0x0127, "hbar" }, // LATIN SMALL LETTER H WITH STROKE + { 0x0128, "Itilde" }, // LATIN CAPITAL LETTER I WITH TILDE + { 0x0129, "itilde" }, // LATIN SMALL LETTER I WITH TILDE + { 0x012A, "Imacron" }, // LATIN CAPITAL LETTER I WITH MACRON + { 0x012B, "imacron" }, // LATIN SMALL LETTER I WITH MACRON + { 0x012C, "Ibreve" }, // LATIN CAPITAL LETTER I WITH BREVE + { 0x012D, "ibreve" }, // LATIN SMALL LETTER I WITH BREVE + { 0x012E, "Iogonek" }, // LATIN CAPITAL LETTER I WITH OGONEK + { 0x012F, "iogonek" }, // LATIN SMALL LETTER I WITH OGONEK + { 0x0130, "Idotaccent" }, // LATIN CAPITAL LETTER I WITH DOT ABOVE + { 0x0131, "dotlessi" }, // LATIN SMALL LETTER DOTLESS I + { 0x0132, "IJ" }, // LATIN CAPITAL LIGATURE IJ + { 0x0133, "ij" }, // LATIN SMALL LIGATURE IJ + { 0x0134, "Jcircumflex" }, // LATIN CAPITAL LETTER J WITH CIRCUMFLEX + { 0x0135, "jcircumflex" }, // LATIN SMALL LETTER J WITH CIRCUMFLEX + { 0x0136, "Kcommaaccent" }, // LATIN CAPITAL LETTER K WITH CEDILLA + { 0x0137, "kcommaaccent" }, // LATIN SMALL LETTER K WITH CEDILLA + { 0x0138, "kgreenlandic" }, // LATIN SMALL LETTER KRA + { 0x0139, "Lacute" }, // LATIN CAPITAL LETTER L WITH ACUTE + { 0x013A, "lacute" }, // LATIN SMALL LETTER L WITH ACUTE + { 0x013B, "Lcommaaccent" }, // LATIN CAPITAL LETTER L WITH CEDILLA + { 0x013C, "lcommaaccent" }, // LATIN SMALL LETTER L WITH CEDILLA + { 0x013D, "Lcaron" }, // LATIN CAPITAL LETTER L WITH CARON + { 0x013E, "lcaron" }, // LATIN SMALL LETTER L WITH CARON + { 0x013F, "Ldot" }, // LATIN CAPITAL LETTER L WITH MIDDLE DOT + { 0x0140, "ldot" }, // LATIN SMALL LETTER L WITH MIDDLE DOT + { 0x0141, "Lslash" }, // LATIN CAPITAL LETTER L WITH STROKE + { 0x0142, "lslash" }, // LATIN SMALL LETTER L WITH STROKE + { 0x0143, "Nacute" }, // LATIN CAPITAL LETTER N WITH ACUTE + { 0x0144, "nacute" }, // LATIN SMALL LETTER N WITH ACUTE + { 0x0145, "Ncommaaccent" }, // LATIN CAPITAL LETTER N WITH CEDILLA + { 0x0146, "ncommaaccent" }, // LATIN SMALL LETTER N WITH CEDILLA + { 0x0147, "Ncaron" }, // LATIN CAPITAL LETTER N WITH CARON + { 0x0148, "ncaron" }, // LATIN SMALL LETTER N WITH CARON + { 0x0149, "napostrophe" }, // LATIN SMALL LETTER N PRECEDED BY APOSTROPHE + { 0x014A, "Eng" }, // LATIN CAPITAL LETTER ENG + { 0x014B, "eng" }, // LATIN SMALL LETTER ENG + { 0x014C, "Omacron" }, // LATIN CAPITAL LETTER O WITH MACRON + { 0x014D, "omacron" }, // LATIN SMALL LETTER O WITH MACRON + { 0x014E, "Obreve" }, // LATIN CAPITAL LETTER O WITH BREVE + { 0x014F, "obreve" }, // LATIN SMALL LETTER O WITH BREVE + { 0x0150, "Ohungarumlaut" }, // LATIN CAPITAL LETTER O WITH DOUBLE ACUTE + { 0x0151, "ohungarumlaut" }, // LATIN SMALL LETTER O WITH DOUBLE ACUTE + { 0x0152, "OE" }, // LATIN CAPITAL LIGATURE OE + { 0x0153, "oe" }, // LATIN SMALL LIGATURE OE + { 0x0154, "Racute" }, // LATIN CAPITAL LETTER R WITH ACUTE + { 0x0155, "racute" }, // LATIN SMALL LETTER R WITH ACUTE + { 0x0156, "Rcommaaccent" }, // LATIN CAPITAL LETTER R WITH CEDILLA + { 0x0157, "rcommaaccent" }, // LATIN SMALL LETTER R WITH CEDILLA + { 0x0158, "Rcaron" }, // LATIN CAPITAL LETTER R WITH CARON + { 0x0159, "rcaron" }, // LATIN SMALL LETTER R WITH CARON + { 0x015A, "Sacute" }, // LATIN CAPITAL LETTER S WITH ACUTE + { 0x015B, "sacute" }, // LATIN SMALL LETTER S WITH ACUTE + { 0x015C, "Scircumflex" }, // LATIN CAPITAL LETTER S WITH CIRCUMFLEX + { 0x015D, "scircumflex" }, // LATIN SMALL LETTER S WITH CIRCUMFLEX + { 0x015E, "Scedilla" }, // LATIN CAPITAL LETTER S WITH CEDILLA + { 0x015F, "scedilla" }, // LATIN SMALL LETTER S WITH CEDILLA + { 0x0160, "Scaron" }, // LATIN CAPITAL LETTER S WITH CARON + { 0x0161, "scaron" }, // LATIN SMALL LETTER S WITH CARON + { 0x0162, "Tcommaaccent" }, // LATIN CAPITAL LETTER T WITH CEDILLA + { 0x0163, "tcommaaccent" }, // LATIN SMALL LETTER T WITH CEDILLA + { 0x0164, "Tcaron" }, // LATIN CAPITAL LETTER T WITH CARON + { 0x0165, "tcaron" }, // LATIN SMALL LETTER T WITH CARON + { 0x0166, "Tbar" }, // LATIN CAPITAL LETTER T WITH STROKE + { 0x0167, "tbar" }, // LATIN SMALL LETTER T WITH STROKE + { 0x0168, "Utilde" }, // LATIN CAPITAL LETTER U WITH TILDE + { 0x0169, "utilde" }, // LATIN SMALL LETTER U WITH TILDE + { 0x016A, "Umacron" }, // LATIN CAPITAL LETTER U WITH MACRON + { 0x016B, "umacron" }, // LATIN SMALL LETTER U WITH MACRON + { 0x016C, "Ubreve" }, // LATIN CAPITAL LETTER U WITH BREVE + { 0x016D, "ubreve" }, // LATIN SMALL LETTER U WITH BREVE + { 0x016E, "Uring" }, // LATIN CAPITAL LETTER U WITH RING ABOVE + { 0x016F, "uring" }, // LATIN SMALL LETTER U WITH RING ABOVE + { 0x0170, "Uhungarumlaut" }, // LATIN CAPITAL LETTER U WITH DOUBLE ACUTE + { 0x0171, "uhungarumlaut" }, // LATIN SMALL LETTER U WITH DOUBLE ACUTE + { 0x0172, "Uogonek" }, // LATIN CAPITAL LETTER U WITH OGONEK + { 0x0173, "uogonek" }, // LATIN SMALL LETTER U WITH OGONEK + { 0x0174, "Wcircumflex" }, // LATIN CAPITAL LETTER W WITH CIRCUMFLEX + { 0x0175, "wcircumflex" }, // LATIN SMALL LETTER W WITH CIRCUMFLEX + { 0x0176, "Ycircumflex" }, // LATIN CAPITAL LETTER Y WITH CIRCUMFLEX + { 0x0177, "ycircumflex" }, // LATIN SMALL LETTER Y WITH CIRCUMFLEX + { 0x0178, "Ydieresis" }, // LATIN CAPITAL LETTER Y WITH DIAERESIS + { 0x0179, "Zacute" }, // LATIN CAPITAL LETTER Z WITH ACUTE + { 0x017A, "zacute" }, // LATIN SMALL LETTER Z WITH ACUTE + { 0x017B, "Zdotaccent" }, // LATIN CAPITAL LETTER Z WITH DOT ABOVE + { 0x017C, "zdotaccent" }, // LATIN SMALL LETTER Z WITH DOT ABOVE + { 0x017D, "Zcaron" }, // LATIN CAPITAL LETTER Z WITH CARON + { 0x017E, "zcaron" }, // LATIN SMALL LETTER Z WITH CARON + { 0x017F, "longs" }, // LATIN SMALL LETTER LONG S + { 0x0192, "florin" }, // LATIN SMALL LETTER F WITH HOOK + { 0x01A0, "Ohorn" }, // LATIN CAPITAL LETTER O WITH HORN + { 0x01A1, "ohorn" }, // LATIN SMALL LETTER O WITH HORN + { 0x01AF, "Uhorn" }, // LATIN CAPITAL LETTER U WITH HORN + { 0x01B0, "uhorn" }, // LATIN SMALL LETTER U WITH HORN + { 0x01E6, "Gcaron" }, // LATIN CAPITAL LETTER G WITH CARON + { 0x01E7, "gcaron" }, // LATIN SMALL LETTER G WITH CARON + { 0x01FA, "Aringacute" }, // LATIN CAPITAL LETTER A WITH RING ABOVE AND ACUTE + { 0x01FB, "aringacute" }, // LATIN SMALL LETTER A WITH RING ABOVE AND ACUTE + { 0x01FC, "AEacute" }, // LATIN CAPITAL LETTER AE WITH ACUTE + { 0x01FD, "aeacute" }, // LATIN SMALL LETTER AE WITH ACUTE + { 0x01FE, "Oslashacute" }, // LATIN CAPITAL LETTER O WITH STROKE AND ACUTE + { 0x01FF, "oslashacute" }, // LATIN SMALL LETTER O WITH STROKE AND ACUTE + { 0x0218, "Scommaaccent" }, // LATIN CAPITAL LETTER S WITH COMMA BELOW + { 0x0219, "scommaaccent" }, // LATIN SMALL LETTER S WITH COMMA BELOW + { 0x021A, "Tcommaaccent" }, // LATIN CAPITAL LETTER T WITH COMMA BELOW;Duplicate + { 0x021B, "tcommaaccent" }, // LATIN SMALL LETTER T WITH COMMA BELOW;Duplicate + { 0x02BC, "afii57929" }, // MODIFIER LETTER APOSTROPHE + { 0x02BD, "afii64937" }, // MODIFIER LETTER REVERSED COMMA + { 0x02C6, "circumflex" }, // MODIFIER LETTER CIRCUMFLEX ACCENT + { 0x02C7, "caron" }, // CARON + { 0x02C9, "macron" }, // MODIFIER LETTER MACRON;Duplicate + { 0x02D8, "breve" }, // BREVE + { 0x02D9, "dotaccent" }, // DOT ABOVE + { 0x02DA, "ring" }, // RING ABOVE + { 0x02DB, "ogonek" }, // OGONEK + { 0x02DC, "tilde" }, // SMALL TILDE + { 0x02DD, "hungarumlaut" }, // DOUBLE ACUTE ACCENT + { 0x0300, "gravecomb" }, // COMBINING GRAVE ACCENT + { 0x0301, "acutecomb" }, // COMBINING ACUTE ACCENT + { 0x0303, "tildecomb" }, // COMBINING TILDE + { 0x0309, "hookabovecomb" }, // COMBINING HOOK ABOVE + { 0x0323, "dotbelowcomb" }, // COMBINING DOT BELOW + { 0x0384, "tonos" }, // GREEK TONOS + { 0x0385, "dieresistonos" }, // GREEK DIALYTIKA TONOS + { 0x0386, "Alphatonos" }, // GREEK CAPITAL LETTER ALPHA WITH TONOS + { 0x0387, "anoteleia" }, // GREEK ANO TELEIA + { 0x0388, "Epsilontonos" }, // GREEK CAPITAL LETTER EPSILON WITH TONOS + { 0x0389, "Etatonos" }, // GREEK CAPITAL LETTER ETA WITH TONOS + { 0x038A, "Iotatonos" }, // GREEK CAPITAL LETTER IOTA WITH TONOS + { 0x038C, "Omicrontonos" }, // GREEK CAPITAL LETTER OMICRON WITH TONOS + { 0x038E, "Upsilontonos" }, // GREEK CAPITAL LETTER UPSILON WITH TONOS + { 0x038F, "Omegatonos" }, // GREEK CAPITAL LETTER OMEGA WITH TONOS + { 0x0390, "iotadieresistonos" }, // GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS + { 0x0391, "Alpha" }, // GREEK CAPITAL LETTER ALPHA + { 0x0392, "Beta" }, // GREEK CAPITAL LETTER BETA + { 0x0393, "Gamma" }, // GREEK CAPITAL LETTER GAMMA + { 0x0394, "Delta" }, // GREEK CAPITAL LETTER DELTA;Duplicate + { 0x0395, "Epsilon" }, // GREEK CAPITAL LETTER EPSILON + { 0x0396, "Zeta" }, // GREEK CAPITAL LETTER ZETA + { 0x0397, "Eta" }, // GREEK CAPITAL LETTER ETA + { 0x0398, "Theta" }, // GREEK CAPITAL LETTER THETA + { 0x0399, "Iota" }, // GREEK CAPITAL LETTER IOTA + { 0x039A, "Kappa" }, // GREEK CAPITAL LETTER KAPPA + { 0x039B, "Lambda" }, // GREEK CAPITAL LETTER LAMDA + { 0x039C, "Mu" }, // GREEK CAPITAL LETTER MU + { 0x039D, "Nu" }, // GREEK CAPITAL LETTER NU + { 0x039E, "Xi" }, // GREEK CAPITAL LETTER XI + { 0x039F, "Omicron" }, // GREEK CAPITAL LETTER OMICRON + { 0x03A0, "Pi" }, // GREEK CAPITAL LETTER PI + { 0x03A1, "Rho" }, // GREEK CAPITAL LETTER RHO + { 0x03A3, "Sigma" }, // GREEK CAPITAL LETTER SIGMA + { 0x03A4, "Tau" }, // GREEK CAPITAL LETTER TAU + { 0x03A5, "Upsilon" }, // GREEK CAPITAL LETTER UPSILON + { 0x03A6, "Phi" }, // GREEK CAPITAL LETTER PHI + { 0x03A7, "Chi" }, // GREEK CAPITAL LETTER CHI + { 0x03A8, "Psi" }, // GREEK CAPITAL LETTER PSI + { 0x03A9, "Omega" }, // GREEK CAPITAL LETTER OMEGA;Duplicate + { 0x03AA, "Iotadieresis" }, // GREEK CAPITAL LETTER IOTA WITH DIALYTIKA + { 0x03AB, "Upsilondieresis" }, // GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA + { 0x03AC, "alphatonos" }, // GREEK SMALL LETTER ALPHA WITH TONOS + { 0x03AD, "epsilontonos" }, // GREEK SMALL LETTER EPSILON WITH TONOS + { 0x03AE, "etatonos" }, // GREEK SMALL LETTER ETA WITH TONOS + { 0x03AF, "iotatonos" }, // GREEK SMALL LETTER IOTA WITH TONOS + { 0x03B0, "upsilondieresistonos" }, // GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS + { 0x03B1, "alpha" }, // GREEK SMALL LETTER ALPHA + { 0x03B2, "beta" }, // GREEK SMALL LETTER BETA + { 0x03B3, "gamma" }, // GREEK SMALL LETTER GAMMA + { 0x03B4, "delta" }, // GREEK SMALL LETTER DELTA + { 0x03B5, "epsilon" }, // GREEK SMALL LETTER EPSILON + { 0x03B6, "zeta" }, // GREEK SMALL LETTER ZETA + { 0x03B7, "eta" }, // GREEK SMALL LETTER ETA + { 0x03B8, "theta" }, // GREEK SMALL LETTER THETA + { 0x03B9, "iota" }, // GREEK SMALL LETTER IOTA + { 0x03BA, "kappa" }, // GREEK SMALL LETTER KAPPA + { 0x03BB, "lambda" }, // GREEK SMALL LETTER LAMDA + { 0x03BC, "mu" }, // GREEK SMALL LETTER MU;Duplicate + { 0x03BD, "nu" }, // GREEK SMALL LETTER NU + { 0x03BE, "xi" }, // GREEK SMALL LETTER XI + { 0x03BF, "omicron" }, // GREEK SMALL LETTER OMICRON + { 0x03C0, "pi" }, // GREEK SMALL LETTER PI + { 0x03C1, "rho" }, // GREEK SMALL LETTER RHO + { 0x03C2, "sigma1" }, // GREEK SMALL LETTER FINAL SIGMA + { 0x03C3, "sigma" }, // GREEK SMALL LETTER SIGMA + { 0x03C4, "tau" }, // GREEK SMALL LETTER TAU + { 0x03C5, "upsilon" }, // GREEK SMALL LETTER UPSILON + { 0x03C6, "phi" }, // GREEK SMALL LETTER PHI + { 0x03C7, "chi" }, // GREEK SMALL LETTER CHI + { 0x03C8, "psi" }, // GREEK SMALL LETTER PSI + { 0x03C9, "omega" }, // GREEK SMALL LETTER OMEGA + { 0x03CA, "iotadieresis" }, // GREEK SMALL LETTER IOTA WITH DIALYTIKA + { 0x03CB, "upsilondieresis" }, // GREEK SMALL LETTER UPSILON WITH DIALYTIKA + { 0x03CC, "omicrontonos" }, // GREEK SMALL LETTER OMICRON WITH TONOS + { 0x03CD, "upsilontonos" }, // GREEK SMALL LETTER UPSILON WITH TONOS + { 0x03CE, "omegatonos" }, // GREEK SMALL LETTER OMEGA WITH TONOS + { 0x03D1, "theta1" }, // GREEK THETA SYMBOL + { 0x03D2, "Upsilon1" }, // GREEK UPSILON WITH HOOK SYMBOL + { 0x03D5, "phi1" }, // GREEK PHI SYMBOL + { 0x03D6, "omega1" }, // GREEK PI SYMBOL + { 0x0401, "afii10023" }, // CYRILLIC CAPITAL LETTER IO + { 0x0402, "afii10051" }, // CYRILLIC CAPITAL LETTER DJE + { 0x0403, "afii10052" }, // CYRILLIC CAPITAL LETTER GJE + { 0x0404, "afii10053" }, // CYRILLIC CAPITAL LETTER UKRAINIAN IE + { 0x0405, "afii10054" }, // CYRILLIC CAPITAL LETTER DZE + { 0x0406, "afii10055" }, // CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I + { 0x0407, "afii10056" }, // CYRILLIC CAPITAL LETTER YI + { 0x0408, "afii10057" }, // CYRILLIC CAPITAL LETTER JE + { 0x0409, "afii10058" }, // CYRILLIC CAPITAL LETTER LJE + { 0x040A, "afii10059" }, // CYRILLIC CAPITAL LETTER NJE + { 0x040B, "afii10060" }, // CYRILLIC CAPITAL LETTER TSHE + { 0x040C, "afii10061" }, // CYRILLIC CAPITAL LETTER KJE + { 0x040E, "afii10062" }, // CYRILLIC CAPITAL LETTER SHORT U + { 0x040F, "afii10145" }, // CYRILLIC CAPITAL LETTER DZHE + { 0x0410, "afii10017" }, // CYRILLIC CAPITAL LETTER A + { 0x0411, "afii10018" }, // CYRILLIC CAPITAL LETTER BE + { 0x0412, "afii10019" }, // CYRILLIC CAPITAL LETTER VE + { 0x0413, "afii10020" }, // CYRILLIC CAPITAL LETTER GHE + { 0x0414, "afii10021" }, // CYRILLIC CAPITAL LETTER DE + { 0x0415, "afii10022" }, // CYRILLIC CAPITAL LETTER IE + { 0x0416, "afii10024" }, // CYRILLIC CAPITAL LETTER ZHE + { 0x0417, "afii10025" }, // CYRILLIC CAPITAL LETTER ZE + { 0x0418, "afii10026" }, // CYRILLIC CAPITAL LETTER I + { 0x0419, "afii10027" }, // CYRILLIC CAPITAL LETTER SHORT I + { 0x041A, "afii10028" }, // CYRILLIC CAPITAL LETTER KA + { 0x041B, "afii10029" }, // CYRILLIC CAPITAL LETTER EL + { 0x041C, "afii10030" }, // CYRILLIC CAPITAL LETTER EM + { 0x041D, "afii10031" }, // CYRILLIC CAPITAL LETTER EN + { 0x041E, "afii10032" }, // CYRILLIC CAPITAL LETTER O + { 0x041F, "afii10033" }, // CYRILLIC CAPITAL LETTER PE + { 0x0420, "afii10034" }, // CYRILLIC CAPITAL LETTER ER + { 0x0421, "afii10035" }, // CYRILLIC CAPITAL LETTER ES + { 0x0422, "afii10036" }, // CYRILLIC CAPITAL LETTER TE + { 0x0423, "afii10037" }, // CYRILLIC CAPITAL LETTER U + { 0x0424, "afii10038" }, // CYRILLIC CAPITAL LETTER EF + { 0x0425, "afii10039" }, // CYRILLIC CAPITAL LETTER HA + { 0x0426, "afii10040" }, // CYRILLIC CAPITAL LETTER TSE + { 0x0427, "afii10041" }, // CYRILLIC CAPITAL LETTER CHE + { 0x0428, "afii10042" }, // CYRILLIC CAPITAL LETTER SHA + { 0x0429, "afii10043" }, // CYRILLIC CAPITAL LETTER SHCHA + { 0x042A, "afii10044" }, // CYRILLIC CAPITAL LETTER HARD SIGN + { 0x042B, "afii10045" }, // CYRILLIC CAPITAL LETTER YERU + { 0x042C, "afii10046" }, // CYRILLIC CAPITAL LETTER SOFT SIGN + { 0x042D, "afii10047" }, // CYRILLIC CAPITAL LETTER E + { 0x042E, "afii10048" }, // CYRILLIC CAPITAL LETTER YU + { 0x042F, "afii10049" }, // CYRILLIC CAPITAL LETTER YA + { 0x0430, "afii10065" }, // CYRILLIC SMALL LETTER A + { 0x0431, "afii10066" }, // CYRILLIC SMALL LETTER BE + { 0x0432, "afii10067" }, // CYRILLIC SMALL LETTER VE + { 0x0433, "afii10068" }, // CYRILLIC SMALL LETTER GHE + { 0x0434, "afii10069" }, // CYRILLIC SMALL LETTER DE + { 0x0435, "afii10070" }, // CYRILLIC SMALL LETTER IE + { 0x0436, "afii10072" }, // CYRILLIC SMALL LETTER ZHE + { 0x0437, "afii10073" }, // CYRILLIC SMALL LETTER ZE + { 0x0438, "afii10074" }, // CYRILLIC SMALL LETTER I + { 0x0439, "afii10075" }, // CYRILLIC SMALL LETTER SHORT I + { 0x043A, "afii10076" }, // CYRILLIC SMALL LETTER KA + { 0x043B, "afii10077" }, // CYRILLIC SMALL LETTER EL + { 0x043C, "afii10078" }, // CYRILLIC SMALL LETTER EM + { 0x043D, "afii10079" }, // CYRILLIC SMALL LETTER EN + { 0x043E, "afii10080" }, // CYRILLIC SMALL LETTER O + { 0x043F, "afii10081" }, // CYRILLIC SMALL LETTER PE + { 0x0440, "afii10082" }, // CYRILLIC SMALL LETTER ER + { 0x0441, "afii10083" }, // CYRILLIC SMALL LETTER ES + { 0x0442, "afii10084" }, // CYRILLIC SMALL LETTER TE + { 0x0443, "afii10085" }, // CYRILLIC SMALL LETTER U + { 0x0444, "afii10086" }, // CYRILLIC SMALL LETTER EF + { 0x0445, "afii10087" }, // CYRILLIC SMALL LETTER HA + { 0x0446, "afii10088" }, // CYRILLIC SMALL LETTER TSE + { 0x0447, "afii10089" }, // CYRILLIC SMALL LETTER CHE + { 0x0448, "afii10090" }, // CYRILLIC SMALL LETTER SHA + { 0x0449, "afii10091" }, // CYRILLIC SMALL LETTER SHCHA + { 0x044A, "afii10092" }, // CYRILLIC SMALL LETTER HARD SIGN + { 0x044B, "afii10093" }, // CYRILLIC SMALL LETTER YERU + { 0x044C, "afii10094" }, // CYRILLIC SMALL LETTER SOFT SIGN + { 0x044D, "afii10095" }, // CYRILLIC SMALL LETTER E + { 0x044E, "afii10096" }, // CYRILLIC SMALL LETTER YU + { 0x044F, "afii10097" }, // CYRILLIC SMALL LETTER YA + { 0x0451, "afii10071" }, // CYRILLIC SMALL LETTER IO + { 0x0452, "afii10099" }, // CYRILLIC SMALL LETTER DJE + { 0x0453, "afii10100" }, // CYRILLIC SMALL LETTER GJE + { 0x0454, "afii10101" }, // CYRILLIC SMALL LETTER UKRAINIAN IE + { 0x0455, "afii10102" }, // CYRILLIC SMALL LETTER DZE + { 0x0456, "afii10103" }, // CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I + { 0x0457, "afii10104" }, // CYRILLIC SMALL LETTER YI + { 0x0458, "afii10105" }, // CYRILLIC SMALL LETTER JE + { 0x0459, "afii10106" }, // CYRILLIC SMALL LETTER LJE + { 0x045A, "afii10107" }, // CYRILLIC SMALL LETTER NJE + { 0x045B, "afii10108" }, // CYRILLIC SMALL LETTER TSHE + { 0x045C, "afii10109" }, // CYRILLIC SMALL LETTER KJE + { 0x045E, "afii10110" }, // CYRILLIC SMALL LETTER SHORT U + { 0x045F, "afii10193" }, // CYRILLIC SMALL LETTER DZHE + { 0x0462, "afii10146" }, // CYRILLIC CAPITAL LETTER YAT + { 0x0463, "afii10194" }, // CYRILLIC SMALL LETTER YAT + { 0x0472, "afii10147" }, // CYRILLIC CAPITAL LETTER FITA + { 0x0473, "afii10195" }, // CYRILLIC SMALL LETTER FITA + { 0x0474, "afii10148" }, // CYRILLIC CAPITAL LETTER IZHITSA + { 0x0475, "afii10196" }, // CYRILLIC SMALL LETTER IZHITSA + { 0x0490, "afii10050" }, // CYRILLIC CAPITAL LETTER GHE WITH UPTURN + { 0x0491, "afii10098" }, // CYRILLIC SMALL LETTER GHE WITH UPTURN + { 0x04D9, "afii10846" }, // CYRILLIC SMALL LETTER SCHWA + { 0x05B0, "afii57799" }, // HEBREW POINT SHEVA + { 0x05B1, "afii57801" }, // HEBREW POINT HATAF SEGOL + { 0x05B2, "afii57800" }, // HEBREW POINT HATAF PATAH + { 0x05B3, "afii57802" }, // HEBREW POINT HATAF QAMATS + { 0x05B4, "afii57793" }, // HEBREW POINT HIRIQ + { 0x05B5, "afii57794" }, // HEBREW POINT TSERE + { 0x05B6, "afii57795" }, // HEBREW POINT SEGOL + { 0x05B7, "afii57798" }, // HEBREW POINT PATAH + { 0x05B8, "afii57797" }, // HEBREW POINT QAMATS + { 0x05B9, "afii57806" }, // HEBREW POINT HOLAM + { 0x05BB, "afii57796" }, // HEBREW POINT QUBUTS + { 0x05BC, "afii57807" }, // HEBREW POINT DAGESH OR MAPIQ + { 0x05BD, "afii57839" }, // HEBREW POINT METEG + { 0x05BE, "afii57645" }, // HEBREW PUNCTUATION MAQAF + { 0x05BF, "afii57841" }, // HEBREW POINT RAFE + { 0x05C0, "afii57842" }, // HEBREW PUNCTUATION PASEQ + { 0x05C1, "afii57804" }, // HEBREW POINT SHIN DOT + { 0x05C2, "afii57803" }, // HEBREW POINT SIN DOT + { 0x05C3, "afii57658" }, // HEBREW PUNCTUATION SOF PASUQ + { 0x05D0, "afii57664" }, // HEBREW LETTER ALEF + { 0x05D1, "afii57665" }, // HEBREW LETTER BET + { 0x05D2, "afii57666" }, // HEBREW LETTER GIMEL + { 0x05D3, "afii57667" }, // HEBREW LETTER DALET + { 0x05D4, "afii57668" }, // HEBREW LETTER HE + { 0x05D5, "afii57669" }, // HEBREW LETTER VAV + { 0x05D6, "afii57670" }, // HEBREW LETTER ZAYIN + { 0x05D7, "afii57671" }, // HEBREW LETTER HET + { 0x05D8, "afii57672" }, // HEBREW LETTER TET + { 0x05D9, "afii57673" }, // HEBREW LETTER YOD + { 0x05DA, "afii57674" }, // HEBREW LETTER FINAL KAF + { 0x05DB, "afii57675" }, // HEBREW LETTER KAF + { 0x05DC, "afii57676" }, // HEBREW LETTER LAMED + { 0x05DD, "afii57677" }, // HEBREW LETTER FINAL MEM + { 0x05DE, "afii57678" }, // HEBREW LETTER MEM + { 0x05DF, "afii57679" }, // HEBREW LETTER FINAL NUN + { 0x05E0, "afii57680" }, // HEBREW LETTER NUN + { 0x05E1, "afii57681" }, // HEBREW LETTER SAMEKH + { 0x05E2, "afii57682" }, // HEBREW LETTER AYIN + { 0x05E3, "afii57683" }, // HEBREW LETTER FINAL PE + { 0x05E4, "afii57684" }, // HEBREW LETTER PE + { 0x05E5, "afii57685" }, // HEBREW LETTER FINAL TSADI + { 0x05E6, "afii57686" }, // HEBREW LETTER TSADI + { 0x05E7, "afii57687" }, // HEBREW LETTER QOF + { 0x05E8, "afii57688" }, // HEBREW LETTER RESH + { 0x05E9, "afii57689" }, // HEBREW LETTER SHIN + { 0x05EA, "afii57690" }, // HEBREW LETTER TAV + { 0x05F0, "afii57716" }, // HEBREW LIGATURE YIDDISH DOUBLE VAV + { 0x05F1, "afii57717" }, // HEBREW LIGATURE YIDDISH VAV YOD + { 0x05F2, "afii57718" }, // HEBREW LIGATURE YIDDISH DOUBLE YOD + { 0x060C, "afii57388" }, // ARABIC COMMA + { 0x061B, "afii57403" }, // ARABIC SEMICOLON + { 0x061F, "afii57407" }, // ARABIC QUESTION MARK + { 0x0621, "afii57409" }, // ARABIC LETTER HAMZA + { 0x0622, "afii57410" }, // ARABIC LETTER ALEF WITH MADDA ABOVE + { 0x0623, "afii57411" }, // ARABIC LETTER ALEF WITH HAMZA ABOVE + { 0x0624, "afii57412" }, // ARABIC LETTER WAW WITH HAMZA ABOVE + { 0x0625, "afii57413" }, // ARABIC LETTER ALEF WITH HAMZA BELOW + { 0x0626, "afii57414" }, // ARABIC LETTER YEH WITH HAMZA ABOVE + { 0x0627, "afii57415" }, // ARABIC LETTER ALEF + { 0x0628, "afii57416" }, // ARABIC LETTER BEH + { 0x0629, "afii57417" }, // ARABIC LETTER TEH MARBUTA + { 0x062A, "afii57418" }, // ARABIC LETTER TEH + { 0x062B, "afii57419" }, // ARABIC LETTER THEH + { 0x062C, "afii57420" }, // ARABIC LETTER JEEM + { 0x062D, "afii57421" }, // ARABIC LETTER HAH + { 0x062E, "afii57422" }, // ARABIC LETTER KHAH + { 0x062F, "afii57423" }, // ARABIC LETTER DAL + { 0x0630, "afii57424" }, // ARABIC LETTER THAL + { 0x0631, "afii57425" }, // ARABIC LETTER REH + { 0x0632, "afii57426" }, // ARABIC LETTER ZAIN + { 0x0633, "afii57427" }, // ARABIC LETTER SEEN + { 0x0634, "afii57428" }, // ARABIC LETTER SHEEN + { 0x0635, "afii57429" }, // ARABIC LETTER SAD + { 0x0636, "afii57430" }, // ARABIC LETTER DAD + { 0x0637, "afii57431" }, // ARABIC LETTER TAH + { 0x0638, "afii57432" }, // ARABIC LETTER ZAH + { 0x0639, "afii57433" }, // ARABIC LETTER AIN + { 0x063A, "afii57434" }, // ARABIC LETTER GHAIN + { 0x0640, "afii57440" }, // ARABIC TATWEEL + { 0x0641, "afii57441" }, // ARABIC LETTER FEH + { 0x0642, "afii57442" }, // ARABIC LETTER QAF + { 0x0643, "afii57443" }, // ARABIC LETTER KAF + { 0x0644, "afii57444" }, // ARABIC LETTER LAM + { 0x0645, "afii57445" }, // ARABIC LETTER MEEM + { 0x0646, "afii57446" }, // ARABIC LETTER NOON + { 0x0647, "afii57470" }, // ARABIC LETTER HEH + { 0x0648, "afii57448" }, // ARABIC LETTER WAW + { 0x0649, "afii57449" }, // ARABIC LETTER ALEF MAKSURA + { 0x064A, "afii57450" }, // ARABIC LETTER YEH + { 0x064B, "afii57451" }, // ARABIC FATHATAN + { 0x064C, "afii57452" }, // ARABIC DAMMATAN + { 0x064D, "afii57453" }, // ARABIC KASRATAN + { 0x064E, "afii57454" }, // ARABIC FATHA + { 0x064F, "afii57455" }, // ARABIC DAMMA + { 0x0650, "afii57456" }, // ARABIC KASRA + { 0x0651, "afii57457" }, // ARABIC SHADDA + { 0x0652, "afii57458" }, // ARABIC SUKUN + { 0x0660, "afii57392" }, // ARABIC-INDIC DIGIT ZERO + { 0x0661, "afii57393" }, // ARABIC-INDIC DIGIT ONE + { 0x0662, "afii57394" }, // ARABIC-INDIC DIGIT TWO + { 0x0663, "afii57395" }, // ARABIC-INDIC DIGIT THREE + { 0x0664, "afii57396" }, // ARABIC-INDIC DIGIT FOUR + { 0x0665, "afii57397" }, // ARABIC-INDIC DIGIT FIVE + { 0x0666, "afii57398" }, // ARABIC-INDIC DIGIT SIX + { 0x0667, "afii57399" }, // ARABIC-INDIC DIGIT SEVEN + { 0x0668, "afii57400" }, // ARABIC-INDIC DIGIT EIGHT + { 0x0669, "afii57401" }, // ARABIC-INDIC DIGIT NINE + { 0x066A, "afii57381" }, // ARABIC PERCENT SIGN + { 0x066D, "afii63167" }, // ARABIC FIVE POINTED STAR + { 0x0679, "afii57511" }, // ARABIC LETTER TTEH + { 0x067E, "afii57506" }, // ARABIC LETTER PEH + { 0x0686, "afii57507" }, // ARABIC LETTER TCHEH + { 0x0688, "afii57512" }, // ARABIC LETTER DDAL + { 0x0691, "afii57513" }, // ARABIC LETTER RREH + { 0x0698, "afii57508" }, // ARABIC LETTER JEH + { 0x06A4, "afii57505" }, // ARABIC LETTER VEH + { 0x06AF, "afii57509" }, // ARABIC LETTER GAF + { 0x06BA, "afii57514" }, // ARABIC LETTER NOON GHUNNA + { 0x06D2, "afii57519" }, // ARABIC LETTER YEH BARREE + { 0x06D5, "afii57534" }, // ARABIC LETTER AE + { 0x1E80, "Wgrave" }, // LATIN CAPITAL LETTER W WITH GRAVE + { 0x1E81, "wgrave" }, // LATIN SMALL LETTER W WITH GRAVE + { 0x1E82, "Wacute" }, // LATIN CAPITAL LETTER W WITH ACUTE + { 0x1E83, "wacute" }, // LATIN SMALL LETTER W WITH ACUTE + { 0x1E84, "Wdieresis" }, // LATIN CAPITAL LETTER W WITH DIAERESIS + { 0x1E85, "wdieresis" }, // LATIN SMALL LETTER W WITH DIAERESIS + { 0x1EF2, "Ygrave" }, // LATIN CAPITAL LETTER Y WITH GRAVE + { 0x1EF3, "ygrave" }, // LATIN SMALL LETTER Y WITH GRAVE + { 0x200C, "afii61664" }, // ZERO WIDTH NON-JOINER + { 0x200D, "afii301" }, // ZERO WIDTH JOINER + { 0x200E, "afii299" }, // LEFT-TO-RIGHT MARK + { 0x200F, "afii300" }, // RIGHT-TO-LEFT MARK + { 0x2012, "figuredash" }, // FIGURE DASH + { 0x2013, "endash" }, // EN DASH + { 0x2014, "emdash" }, // EM DASH + { 0x2015, "afii00208" }, // HORIZONTAL BAR + { 0x2017, "underscoredbl" }, // DOUBLE LOW LINE + { 0x2018, "quoteleft" }, // LEFT SINGLE QUOTATION MARK + { 0x2019, "quoteright" }, // RIGHT SINGLE QUOTATION MARK + { 0x201A, "quotesinglbase" }, // SINGLE LOW-9 QUOTATION MARK + { 0x201B, "quotereversed" }, // SINGLE HIGH-REVERSED-9 QUOTATION MARK + { 0x201C, "quotedblleft" }, // LEFT DOUBLE QUOTATION MARK + { 0x201D, "quotedblright" }, // RIGHT DOUBLE QUOTATION MARK + { 0x201E, "quotedblbase" }, // DOUBLE LOW-9 QUOTATION MARK + { 0x2020, "dagger" }, // DAGGER + { 0x2021, "daggerdbl" }, // DOUBLE DAGGER + { 0x2022, "bullet" }, // BULLET + { 0x2024, "onedotenleader" }, // ONE DOT LEADER + { 0x2025, "twodotenleader" }, // TWO DOT LEADER + { 0x2026, "ellipsis" }, // HORIZONTAL ELLIPSIS + { 0x202C, "afii61573" }, // POP DIRECTIONAL FORMATTING + { 0x202D, "afii61574" }, // LEFT-TO-RIGHT OVERRIDE + { 0x202E, "afii61575" }, // RIGHT-TO-LEFT OVERRIDE + { 0x2030, "perthousand" }, // PER MILLE SIGN + { 0x2032, "minute" }, // PRIME + { 0x2033, "second" }, // DOUBLE PRIME + { 0x2039, "guilsinglleft" }, // SINGLE LEFT-POINTING ANGLE QUOTATION MARK + { 0x203A, "guilsinglright" }, // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK + { 0x203C, "exclamdbl" }, // DOUBLE EXCLAMATION MARK + { 0x2044, "fraction" }, // FRACTION SLASH + { 0x2070, "zerosuperior" }, // SUPERSCRIPT ZERO + { 0x2074, "foursuperior" }, // SUPERSCRIPT FOUR + { 0x2075, "fivesuperior" }, // SUPERSCRIPT FIVE + { 0x2076, "sixsuperior" }, // SUPERSCRIPT SIX + { 0x2077, "sevensuperior" }, // SUPERSCRIPT SEVEN + { 0x2078, "eightsuperior" }, // SUPERSCRIPT EIGHT + { 0x2079, "ninesuperior" }, // SUPERSCRIPT NINE + { 0x207D, "parenleftsuperior" }, // SUPERSCRIPT LEFT PARENTHESIS + { 0x207E, "parenrightsuperior" }, // SUPERSCRIPT RIGHT PARENTHESIS + { 0x207F, "nsuperior" }, // SUPERSCRIPT LATIN SMALL LETTER N + { 0x2080, "zeroinferior" }, // SUBSCRIPT ZERO + { 0x2081, "oneinferior" }, // SUBSCRIPT ONE + { 0x2082, "twoinferior" }, // SUBSCRIPT TWO + { 0x2083, "threeinferior" }, // SUBSCRIPT THREE + { 0x2084, "fourinferior" }, // SUBSCRIPT FOUR + { 0x2085, "fiveinferior" }, // SUBSCRIPT FIVE + { 0x2086, "sixinferior" }, // SUBSCRIPT SIX + { 0x2087, "seveninferior" }, // SUBSCRIPT SEVEN + { 0x2088, "eightinferior" }, // SUBSCRIPT EIGHT + { 0x2089, "nineinferior" }, // SUBSCRIPT NINE + { 0x208D, "parenleftinferior" }, // SUBSCRIPT LEFT PARENTHESIS + { 0x208E, "parenrightinferior" }, // SUBSCRIPT RIGHT PARENTHESIS + { 0x20A1, "colonmonetary" }, // COLON SIGN + { 0x20A3, "franc" }, // FRENCH FRANC SIGN + { 0x20A4, "lira" }, // LIRA SIGN + { 0x20A7, "peseta" }, // PESETA SIGN + { 0x20AA, "afii57636" }, // NEW SHEQEL SIGN + { 0x20AB, "dong" }, // DONG SIGN + { 0x20AC, "Euro" }, // EURO SIGN + { 0x2105, "afii61248" }, // CARE OF + { 0x2111, "Ifraktur" }, // BLACK-LETTER CAPITAL I + { 0x2113, "afii61289" }, // SCRIPT SMALL L + { 0x2116, "afii61352" }, // NUMERO SIGN + { 0x2118, "weierstrass" }, // SCRIPT CAPITAL P + { 0x211C, "Rfraktur" }, // BLACK-LETTER CAPITAL R + { 0x211E, "prescription" }, // PRESCRIPTION TAKE + { 0x2122, "trademark" }, // TRADE MARK SIGN + { 0x2126, "Omega" }, // OHM SIGN + { 0x212E, "estimated" }, // ESTIMATED SYMBOL + { 0x2135, "aleph" }, // ALEF SYMBOL + { 0x2153, "onethird" }, // VULGAR FRACTION ONE THIRD + { 0x2154, "twothirds" }, // VULGAR FRACTION TWO THIRDS + { 0x215B, "oneeighth" }, // VULGAR FRACTION ONE EIGHTH + { 0x215C, "threeeighths" }, // VULGAR FRACTION THREE EIGHTHS + { 0x215D, "fiveeighths" }, // VULGAR FRACTION FIVE EIGHTHS + { 0x215E, "seveneighths" }, // VULGAR FRACTION SEVEN EIGHTHS + { 0x2190, "arrowleft" }, // LEFTWARDS ARROW + { 0x2191, "arrowup" }, // UPWARDS ARROW + { 0x2192, "arrowright" }, // RIGHTWARDS ARROW + { 0x2193, "arrowdown" }, // DOWNWARDS ARROW + { 0x2194, "arrowboth" }, // LEFT RIGHT ARROW + { 0x2195, "arrowupdn" }, // UP DOWN ARROW + { 0x21A8, "arrowupdnbse" }, // UP DOWN ARROW WITH BASE + { 0x21B5, "carriagereturn" }, // DOWNWARDS ARROW WITH CORNER LEFTWARDS + { 0x21D0, "arrowdblleft" }, // LEFTWARDS DOUBLE ARROW + { 0x21D1, "arrowdblup" }, // UPWARDS DOUBLE ARROW + { 0x21D2, "arrowdblright" }, // RIGHTWARDS DOUBLE ARROW + { 0x21D3, "arrowdbldown" }, // DOWNWARDS DOUBLE ARROW + { 0x21D4, "arrowdblboth" }, // LEFT RIGHT DOUBLE ARROW + { 0x2200, "universal" }, // FOR ALL + { 0x2202, "partialdiff" }, // PARTIAL DIFFERENTIAL + { 0x2203, "existential" }, // THERE EXISTS + { 0x2205, "emptyset" }, // EMPTY SET + { 0x2206, "Delta" }, // INCREMENT + { 0x2207, "gradient" }, // NABLA + { 0x2208, "element" }, // ELEMENT OF + { 0x2209, "notelement" }, // NOT AN ELEMENT OF + { 0x220B, "suchthat" }, // CONTAINS AS MEMBER + { 0x220F, "product" }, // N-ARY PRODUCT + { 0x2211, "summation" }, // N-ARY SUMMATION + { 0x2212, "minus" }, // MINUS SIGN + { 0x2215, "fraction" }, // DIVISION SLASH;Duplicate + { 0x2217, "asteriskmath" }, // ASTERISK OPERATOR + { 0x2219, "periodcentered" }, // BULLET OPERATOR;Duplicate + { 0x221A, "radical" }, // SQUARE ROOT + { 0x221D, "proportional" }, // PROPORTIONAL TO + { 0x221E, "infinity" }, // INFINITY + { 0x221F, "orthogonal" }, // RIGHT ANGLE + { 0x2220, "angle" }, // ANGLE + { 0x2227, "logicaland" }, // LOGICAL AND + { 0x2228, "logicalor" }, // LOGICAL OR + { 0x2229, "intersection" }, // INTERSECTION + { 0x222A, "union" }, // UNION + { 0x222B, "integral" }, // INTEGRAL + { 0x2234, "therefore" }, // THEREFORE + { 0x223C, "similar" }, // TILDE OPERATOR + { 0x2245, "congruent" }, // APPROXIMATELY EQUAL TO + { 0x2248, "approxequal" }, // ALMOST EQUAL TO + { 0x2260, "notequal" }, // NOT EQUAL TO + { 0x2261, "equivalence" }, // IDENTICAL TO + { 0x2264, "lessequal" }, // LESS-THAN OR EQUAL TO + { 0x2265, "greaterequal" }, // GREATER-THAN OR EQUAL TO + { 0x2282, "propersubset" }, // SUBSET OF + { 0x2283, "propersuperset" }, // SUPERSET OF + { 0x2284, "notsubset" }, // NOT A SUBSET OF + { 0x2286, "reflexsubset" }, // SUBSET OF OR EQUAL TO + { 0x2287, "reflexsuperset" }, // SUPERSET OF OR EQUAL TO + { 0x2295, "circleplus" }, // CIRCLED PLUS + { 0x2297, "circlemultiply" }, // CIRCLED TIMES + { 0x22A5, "perpendicular" }, // UP TACK + { 0x22C5, "dotmath" }, // DOT OPERATOR + { 0x2302, "house" }, // HOUSE + { 0x2310, "revlogicalnot" }, // REVERSED NOT SIGN + { 0x2320, "integraltp" }, // TOP HALF INTEGRAL + { 0x2321, "integralbt" }, // BOTTOM HALF INTEGRAL + { 0x2329, "angleleft" }, // LEFT-POINTING ANGLE BRACKET + { 0x232A, "angleright" }, // RIGHT-POINTING ANGLE BRACKET + { 0x2500, "SF100000" }, // BOX DRAWINGS LIGHT HORIZONTAL + { 0x2502, "SF110000" }, // BOX DRAWINGS LIGHT VERTICAL + { 0x250C, "SF010000" }, // BOX DRAWINGS LIGHT DOWN AND RIGHT + { 0x2510, "SF030000" }, // BOX DRAWINGS LIGHT DOWN AND LEFT + { 0x2514, "SF020000" }, // BOX DRAWINGS LIGHT UP AND RIGHT + { 0x2518, "SF040000" }, // BOX DRAWINGS LIGHT UP AND LEFT + { 0x251C, "SF080000" }, // BOX DRAWINGS LIGHT VERTICAL AND RIGHT + { 0x2524, "SF090000" }, // BOX DRAWINGS LIGHT VERTICAL AND LEFT + { 0x252C, "SF060000" }, // BOX DRAWINGS LIGHT DOWN AND HORIZONTAL + { 0x2534, "SF070000" }, // BOX DRAWINGS LIGHT UP AND HORIZONTAL + { 0x253C, "SF050000" }, // BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL + { 0x2550, "SF430000" }, // BOX DRAWINGS DOUBLE HORIZONTAL + { 0x2551, "SF240000" }, // BOX DRAWINGS DOUBLE VERTICAL + { 0x2552, "SF510000" }, // BOX DRAWINGS DOWN SINGLE AND RIGHT DOUBLE + { 0x2553, "SF520000" }, // BOX DRAWINGS DOWN DOUBLE AND RIGHT SINGLE + { 0x2554, "SF390000" }, // BOX DRAWINGS DOUBLE DOWN AND RIGHT + { 0x2555, "SF220000" }, // BOX DRAWINGS DOWN SINGLE AND LEFT DOUBLE + { 0x2556, "SF210000" }, // BOX DRAWINGS DOWN DOUBLE AND LEFT SINGLE + { 0x2557, "SF250000" }, // BOX DRAWINGS DOUBLE DOWN AND LEFT + { 0x2558, "SF500000" }, // BOX DRAWINGS UP SINGLE AND RIGHT DOUBLE + { 0x2559, "SF490000" }, // BOX DRAWINGS UP DOUBLE AND RIGHT SINGLE + { 0x255A, "SF380000" }, // BOX DRAWINGS DOUBLE UP AND RIGHT + { 0x255B, "SF280000" }, // BOX DRAWINGS UP SINGLE AND LEFT DOUBLE + { 0x255C, "SF270000" }, // BOX DRAWINGS UP DOUBLE AND LEFT SINGLE + { 0x255D, "SF260000" }, // BOX DRAWINGS DOUBLE UP AND LEFT + { 0x255E, "SF360000" }, // BOX DRAWINGS VERTICAL SINGLE AND RIGHT DOUBLE + { 0x255F, "SF370000" }, // BOX DRAWINGS VERTICAL DOUBLE AND RIGHT SINGLE + { 0x2560, "SF420000" }, // BOX DRAWINGS DOUBLE VERTICAL AND RIGHT + { 0x2561, "SF190000" }, // BOX DRAWINGS VERTICAL SINGLE AND LEFT DOUBLE + { 0x2562, "SF200000" }, // BOX DRAWINGS VERTICAL DOUBLE AND LEFT SINGLE + { 0x2563, "SF230000" }, // BOX DRAWINGS DOUBLE VERTICAL AND LEFT + { 0x2564, "SF470000" }, // BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE + { 0x2565, "SF480000" }, // BOX DRAWINGS DOWN DOUBLE AND HORIZONTAL SINGLE + { 0x2566, "SF410000" }, // BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL + { 0x2567, "SF450000" }, // BOX DRAWINGS UP SINGLE AND HORIZONTAL DOUBLE + { 0x2568, "SF460000" }, // BOX DRAWINGS UP DOUBLE AND HORIZONTAL SINGLE + { 0x2569, "SF400000" }, // BOX DRAWINGS DOUBLE UP AND HORIZONTAL + { 0x256A, "SF540000" }, // BOX DRAWINGS VERTICAL SINGLE AND HORIZONTAL DOUBLE + { 0x256B, "SF530000" }, // BOX DRAWINGS VERTICAL DOUBLE AND HORIZONTAL SINGLE + { 0x256C, "SF440000" }, // BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL + { 0x2580, "upblock" }, // UPPER HALF BLOCK + { 0x2584, "dnblock" }, // LOWER HALF BLOCK + { 0x2588, "block" }, // FULL BLOCK + { 0x258C, "lfblock" }, // LEFT HALF BLOCK + { 0x2590, "rtblock" }, // RIGHT HALF BLOCK + { 0x2591, "ltshade" }, // LIGHT SHADE + { 0x2592, "shade" }, // MEDIUM SHADE + { 0x2593, "dkshade" }, // DARK SHADE + { 0x25A0, "filledbox" }, // BLACK SQUARE + { 0x25A1, "H22073" }, // WHITE SQUARE + { 0x25AA, "H18543" }, // BLACK SMALL SQUARE + { 0x25AB, "H18551" }, // WHITE SMALL SQUARE + { 0x25AC, "filledrect" }, // BLACK RECTANGLE + { 0x25B2, "triagup" }, // BLACK UP-POINTING TRIANGLE + { 0x25BA, "triagrt" }, // BLACK RIGHT-POINTING POINTER + { 0x25BC, "triagdn" }, // BLACK DOWN-POINTING TRIANGLE + { 0x25C4, "triaglf" }, // BLACK LEFT-POINTING POINTER + { 0x25CA, "lozenge" }, // LOZENGE + { 0x25CB, "circle" }, // WHITE CIRCLE + { 0x25CF, "H18533" }, // BLACK CIRCLE + { 0x25D8, "invbullet" }, // INVERSE BULLET + { 0x25D9, "invcircle" }, // INVERSE WHITE CIRCLE + { 0x25E6, "openbullet" }, // WHITE BULLET + { 0x263A, "smileface" }, // WHITE SMILING FACE + { 0x263B, "invsmileface" }, // BLACK SMILING FACE + { 0x263C, "sun" }, // WHITE SUN WITH RAYS + { 0x2640, "female" }, // FEMALE SIGN + { 0x2642, "male" }, // MALE SIGN + { 0x2660, "spade" }, // BLACK SPADE SUIT + { 0x2663, "club" }, // BLACK CLUB SUIT + { 0x2665, "heart" }, // BLACK HEART SUIT + { 0x2666, "diamond" }, // BLACK DIAMOND SUIT + { 0x266A, "musicalnote" }, // EIGHTH NOTE + { 0x266B, "musicalnotedbl" }, // BEAMED EIGHTH NOTES + // The names below are in the PU area of Unicode, but needed to get a correct mapping of the symbol font + { 0xF6D9, "copyrightserif" }, + { 0xF6DA, "registerserif" }, + { 0xF6DB, "trademarkserif" }, + { 0xF8E5, "radicalex" }, + { 0xF8E6, "arrowvertex" }, + { 0xF8E7, "arrowhorizex" }, + { 0xF8E8, "registersans" }, + { 0xF8E9, "copyrightsans" }, + { 0xF8EA, "trademarksans" }, + { 0xF8EB, "parenlefttp" }, + { 0xF8EC, "parenleftex" }, + { 0xF8ED, "parenleftbt" }, + { 0xF8EE, "bracketlefttp" }, + { 0xF8EF, "bracketleftex" }, + { 0xF8F0, "bracketleftbt" }, + { 0xF8F1, "bracelefttp" }, + { 0xF8F2, "braceleftmid" }, + { 0xF8F3, "braceleftbt" }, + { 0xF8F4, "braceex" }, + { 0xF8F5, "integralex" }, + { 0xF8F6, "parenrighttp" }, + { 0xF8F7, "parenrightex" }, + { 0xF8F8, "parenrightbt" }, + { 0xF8F9, "bracketrighttp" }, + { 0xF8FA, "bracketrightex" }, + { 0xF8FB, "bracketrightbt" }, + { 0xF8FC, "bracerighttp" }, + { 0xF8FD, "bracerightmid" }, + { 0xF8FE, "bracerightbt" }, + // End of extensions needed for symbols + { 0xFB00, "ff" }, // LATIN SMALL LIGATURE FF + { 0xFB01, "fi" }, // LATIN SMALL LIGATURE FI + { 0xFB02, "fl" }, // LATIN SMALL LIGATURE FL + { 0xFB03, "ffi" }, // LATIN SMALL LIGATURE FFI + { 0xFB04, "ffl" }, // LATIN SMALL LIGATURE FFL + { 0xFB1F, "afii57705" }, // HEBREW LIGATURE YIDDISH YOD YOD PATAH + { 0xFB2A, "afii57694" }, // HEBREW LETTER SHIN WITH SHIN DOT + { 0xFB2B, "afii57695" }, // HEBREW LETTER SHIN WITH SIN DOT + { 0xFB35, "afii57723" }, // HEBREW LETTER VAV WITH DAGESH + { 0xFB4B, "afii57700" }, // HEBREW LETTER VAV WITH HOLAM + // end of stuff from glyphlist.txt + { 0xFFFF, 0 } +}; + +// --------------------------------------------------------------------- +// postscript font substitution dictionary. We assume every postscript printer has at least +// Helvetica, Times, Courier and Symbol + +struct psfont { + const char *psname; + float slant; + float xscale; +}; + +static const psfont Arial[] = { + {"Arial", 0, 84.04 }, + { "Arial-Italic", 0, 84.04 }, + { "Arial-Bold", 0, 88.65 }, + { "Arial-BoldItalic", 0, 88.65 } +}; + +static const psfont AvantGarde[] = { + { "AvantGarde-Book", 0, 87.43 }, + { "AvantGarde-BookOblique", 0, 88.09 }, + { "AvantGarde-Demi", 0, 88.09 }, + { "AvantGarde-DemiOblique", 0, 87.43 }, +}; + +static const psfont Bookman [] = { + { "Bookman-Light", 0, 93.78 }, + { "Bookman-LightItalic", 0, 91.42 }, + { "Bookman-Demi", 0, 99.86 }, + { "Bookman-DemiItalic", 0, 101.54 } +}; + +static const psfont Charter [] = { + { "CharterBT-Roman", 0, 84.04 }, + { "CharterBT-Italic", 0.0, 81.92 }, + { "CharterBT-Bold", 0, 88.99 }, + { "CharterBT-BoldItalic", 0.0, 88.20 } +}; + +static const psfont Courier [] = { + { "Courier", 0, 100. }, + { "Courier-Oblique", 0, 100. }, + { "Courier-Bold", 0, 100. }, + { "Courier-BoldOblique", 0, 100. } +}; + +static const psfont Garamond [] = { + { "Garamond-Antiqua", 0, 78.13 }, + { "Garamond-Kursiv", 0, 78.13 }, + { "Garamond-Halbfett", 0, 78.13 }, + { "Garamond-KursivHalbfett", 0, 78.13 } +}; + +static const psfont GillSans [] = { // ### some estimated value for xstretch + { "GillSans", 0, 82 }, + { "GillSans-Italic", 0, 82 }, + { "GillSans-Bold", 0, 82 }, + { "GillSans-BoldItalic", 0, 82 } +}; + +static const psfont Helvetica [] = { + { "Helvetica", 0, 84.04 }, + { "Helvetica-Oblique", 0, 84.04 }, + { "Helvetica-Bold", 0, 88.65 }, + { "Helvetica-BoldOblique", 0, 88.65 } +}; + +static const psfont Letter [] = { + { "LetterGothic", 0, 83.32 }, + { "LetterGothic-Italic", 0, 83.32 }, + { "LetterGothic-Bold", 0, 83.32 }, + { "LetterGothic-Bold", 0.2, 83.32 } +}; + +static const psfont LucidaSans [] = { + { "LucidaSans", 0, 94.36 }, + { "LucidaSans-Oblique", 0, 94.36 }, + { "LucidaSans-Demi", 0, 98.10 }, + { "LucidaSans-DemiOblique", 0, 98.08 } +}; + +static const psfont LucidaSansTT [] = { + { "LucidaSans-Typewriter", 0, 100.50 }, + { "LucidaSans-TypewriterOblique", 0, 100.50 }, + { "LucidaSans-TypewriterBold", 0, 100.50 }, + { "LucidaSans-TypewriterBoldOblique", 0, 100.50 } +}; + +static const psfont LucidaBright [] = { + { "LucidaBright", 0, 93.45 }, + { "LucidaBright-Italic", 0, 91.98 }, + { "LucidaBright-Demi", 0, 96.22 }, + { "LucidaBright-DemiItalic", 0, 96.98 } +}; + +static const psfont Palatino [] = { + { "Palatino-Roman", 0, 82.45 }, + { "Palatino-Italic", 0, 76.56 }, + { "Palatino-Bold", 0, 83.49 }, + { "Palatino-BoldItalic", 0, 81.51 } +}; + +static const psfont Symbol [] = { + { "Symbol", 0, 82.56 }, + { "Symbol", 0.2, 82.56 }, + { "Symbol", 0, 82.56 }, + { "Symbol", 0.2, 82.56 } +}; + +static const psfont Tahoma [] = { + { "Tahoma", 0, 83.45 }, + { "Tahoma", 0.2, 83.45 }, + { "Tahoma-Bold", 0, 95.59 }, + { "Tahoma-Bold", 0.2, 95.59 } +}; + +static const psfont Times [] = { + { "Times-Roman", 0, 82.45 }, + { "Times-Italic", 0, 82.45 }, + { "Times-Bold", 0, 82.45 }, + { "Times-BoldItalic", 0, 82.45 } +}; + +static const psfont Verdana [] = { + { "Verdana", 0, 96.06 }, + { "Verdana-Italic", 0, 96.06 }, + { "Verdana-Bold", 0, 107.12 }, + { "Verdana-BoldItalic", 0, 107.10 } +}; + +static const psfont Utopia [] = { // ### + { "Utopia-Regular", 0, 84.70 }, + { "Utopia-Regular", 0.2, 84.70 }, + { "Utopia-Bold", 0, 88.01 }, + { "Utopia-Bold", 0.2, 88.01 } +}; + +static const psfont * const SansSerifReplacements[] = { + Helvetica, 0 + }; +static const psfont * const SerifReplacements[] = { + Times, 0 + }; +static const psfont * const FixedReplacements[] = { + Courier, 0 + }; +static const psfont * const TahomaReplacements[] = { + Verdana, AvantGarde, Helvetica, 0 + }; +static const psfont * const VerdanaReplacements[] = { + Tahoma, AvantGarde, Helvetica, 0 + }; + +static const struct { + const char * input; // spaces are stripped in here, and everything lowercase + const psfont * ps; + const psfont *const * replacements; +} postscriptFonts [] = { + { "arial", Arial, SansSerifReplacements }, + { "arialmt", Arial, SansSerifReplacements }, + { "arialunicodems", Arial, SansSerifReplacements }, + { "avantgarde", AvantGarde, SansSerifReplacements }, + { "bookman", Bookman, SerifReplacements }, + { "charter", Charter, SansSerifReplacements }, + { "bitstreamcharter", Charter, SansSerifReplacements }, + { "bitstreamcyberbit", Times, SerifReplacements }, // ### + { "courier", Courier, 0 }, + { "couriernew", Courier, 0 }, + { "fixed", Courier, 0 }, + { "garamond", Garamond, SerifReplacements }, + { "gillsans", GillSans, SansSerifReplacements }, + { "helvetica", Helvetica, 0 }, + { "letter", Letter, FixedReplacements }, + { "lucida", LucidaSans, SansSerifReplacements }, + { "lucidasans", LucidaSans, SansSerifReplacements }, + { "lucidabright", LucidaBright, SerifReplacements }, + { "lucidasanstypewriter", LucidaSansTT, FixedReplacements }, + { "luciduxsans", LucidaSans, SansSerifReplacements }, + { "luciduxserif", LucidaBright, SerifReplacements }, + { "luciduxmono", LucidaSansTT, FixedReplacements }, + { "palatino", Palatino, SerifReplacements }, + { "symbol", Symbol, 0 }, + { "tahoma", Tahoma, TahomaReplacements }, + { "terminal", Courier, 0 }, + { "times", Times, 0 }, + { "timesnewroman", Times, 0 }, + { "verdana", Verdana, VerdanaReplacements }, + { "utopia", Utopia, SerifReplacements }, + { 0, 0, 0 } +}; + + +// ------------------------------End of static data ---------------------------------- + +// make sure DSC comments are not longer than 255 chars per line. +static QString wrapDSC( const QString &str ) +{ + QString dsc = str.simplifyWhiteSpace(); + const uint wrapAt = 254; + QString wrapped; + if ( dsc.length() < wrapAt ) + wrapped = dsc; + else { + wrapped = dsc.left( wrapAt ); + QString tmp = dsc.mid( wrapAt ); + while ( tmp.length() > wrapAt-3 ) { + wrapped += "\n%%+" + tmp.left( wrapAt-3 ); + tmp = tmp.mid( wrapAt-3 ); + } + wrapped += "\n%%+" + tmp; + } + return wrapped + "\n"; +} + +static QString toString( const float num ) +{ + return QString::number( num, 'f', 3 ); +} + +// ----------------------------- Internal class declarations ----------------------------- + +class QPSPrinterFontPrivate; + +class QPSPrinterPrivate { +public: + QPSPrinterPrivate( QPrinter *prt, int filedes ); + ~QPSPrinterPrivate(); + + void matrixSetup( QPainter * ); + void clippingSetup( QPainter * ); + void setClippingOff( QPainter * ); + void orientationSetup(); + void resetDrawingTools( QPainter * ); + void emitHeader( bool finished ); + void setFont( const QFont &, int script ); + void drawImage( QPainter *, float x, float y, float w, float h, const QImage &img, const QImage &mask ); + void initPage( QPainter *paint ); + void flushPage( bool last = FALSE ); + + QPrinter *printer; + int pageCount; + bool dirtyMatrix; + bool dirtyNewPage; + bool epsf; + QString fontsUsed; + + // outstream is the stream the build up pages are copied to. It points to buffer + // at the start, and is reset to use the outDevice after emitHeader has been called. + QTextStream outStream; + + // stores the descriptions of the first pages. outStream operates on this buffer + // until we call emitHeader + QBuffer *buffer; + int pagesInBuffer; + + // the device the output is in the end streamed to. + QIODevice * outDevice; + int fd; + + // buffer for the current page. Needed becaus we might have page fonts. + QBuffer *pageBuffer; + QTextStream pageStream; + + QDict<QString> headerFontNames; + QDict<QString> pageFontNames; + QDict<QPSPrinterFontPrivate> fonts; + QPSPrinterFontPrivate *currentFontFile; + int headerFontNumber; + int pageFontNumber; + QBuffer * fontBuffer; + QTextStream fontStream; + bool dirtyClipping; + bool firstClipOnPage; + QRect boundingBox; + QImage * savedImage; + QPen cpen; + QBrush cbrush; + bool dirtypen; + bool dirtybrush; + QColor bkColor; + bool dirtyBkColor; + Qt::BGMode bkMode; + bool dirtyBkMode; +#ifndef QT_NO_TEXTCODEC + QTextCodec * currentFontCodec; +#endif + QString currentFont; + QFontMetrics fm; + int textY; + QFont currentUsed; + int scriptUsed; + QFont currentSet; + float scale; + + QStringList fontpath; +}; + + +class QPSPrinterFontPrivate { +public: + QPSPrinterFontPrivate(); + virtual ~QPSPrinterFontPrivate() {} + virtual QString postScriptFontName() { return psname; } + virtual QString defineFont( QTextStream &stream, const QString &ps, const QFont &f, const QString &key, + QPSPrinterPrivate *d ); + virtual void download(QTextStream& s, bool global); + virtual void drawText( QTextStream &stream, const QPoint &p, QTextEngine *engine, int item, + const QString &text, QPSPrinterPrivate *d, QPainter *paint); + virtual unsigned short mapUnicode( unsigned short unicode ); + void downloadMapping( QTextStream &s, bool global ); + QString glyphName( unsigned short glyphindex, bool *glyphSet = 0 ); + virtual void restore(); + + virtual unsigned short unicode_for_glyph(int glyphindex) { return glyphindex; } + virtual unsigned short glyph_for_unicode(unsigned short unicode) { return unicode; } + unsigned short insertIntoSubset( unsigned short unicode ); + virtual bool embedded() { return FALSE; } + + bool operator == ( const QPSPrinterFontPrivate &other ) { + return other.psname == psname; + } + inline void setSymbol() { symbol = TRUE; } + +protected: + QString psname; + QStringList replacementList; + + QMap<unsigned short, unsigned short> subset; // unicode subset in the global font + QMap<unsigned short, unsigned short> page_subset; // subset added in this page + int subsetCount; + int pageSubsetCount; + bool global_dict; + bool downloaded; + bool symbol; +}; + +// ------------------- end of class declarations --------------------------- + +// -------------------------------------------------------------- +// beginning of font related methods +// -------------------------------------------------------------- + + +static int getPsFontType( const QFontEngine *fe ) +{ + int weight = fe->fontDef.weight; + bool italic = fe->fontDef.italic; + + int type = 0; // used to look up in the psname array + // get the right modification, or build something + if ( weight > QFont::Normal && italic ) + type = 3; + else if ( weight > QFont::Normal ) + type = 2; + else if ( italic ) + type = 1; + return type; +} + +static int addPsFontNameExtension( const QFontEngine *fe, QString &ps, const psfont *psf = 0 ) +{ + int type = getPsFontType( fe ); + + if ( psf ) { + ps = QString::fromLatin1( psf[type].psname ); + } else { + switch ( type ) { + case 1: + ps.append( QString::fromLatin1("-Italic") ); + break; + case 2: + ps.append( QString::fromLatin1("-Bold") ); + break; + case 3: + ps.append( QString::fromLatin1("-BoldItalic") ); + break; + case 0: + default: + break; + } + } + return type; +} + +static QString makePSFontName( const QFontEngine *fe, int *listpos = 0, int *ftype = 0 ) +{ + QString ps; + int i; + + QString family = fe->fontDef.family.lower(); + + // try to make a "good" postscript name + ps = family.simplifyWhiteSpace(); + i = 0; + while( (unsigned int)i < ps.length() ) { + if ( i != 0 && ps[i] == '[') { + if ( ps[i-1] == ' ' ) + ps.truncate (i-1); + else + ps.truncate (i); + break; + } + if ( i == 0 || ps[i-1] == ' ' ) { + ps[i] = ps[i].upper(); + if ( i ) + ps.remove( i-1, 1 ); + else + i++; + } else { + i++; + } + } + + if ( ps.isEmpty() ) + ps = "Helvetica"; + + // see if the table has a better name + i = 0; + QString lowerName = ps.lower(); + while( postscriptFonts[i].input && + postscriptFonts[i].input != lowerName ) + i++; + const psfont *psf = postscriptFonts[i].ps; + + int type = addPsFontNameExtension( fe, ps, psf ); + + if ( listpos ) + *listpos = i; + if ( ftype ) + *ftype = type; + return ps; +} + +static void appendReplacements( QStringList &list, const psfont * const * replacements, int type, float xscale = 100. ) +{ + // iterate through the replacement fonts + while ( *replacements ) { + const psfont *psf = *replacements; + QString ps = "[ /" + QString::fromLatin1( psf[type].psname ) + " " + + toString( xscale / psf[type].xscale ) + " " + + toString( psf[type].slant ) + " ]"; + list.append( ps ); + ++replacements; + } +} + +static QStringList makePSFontNameList( const QFontEngine *fe, const QString &psname = QString::null, bool useNameForLookup = FALSE ) +{ + int i; + int type; + QStringList list; + QString ps = psname; + + if ( !ps.isEmpty() && !useNameForLookup ) { + QString best = "[ /" + ps + " 1.0 0.0 ]"; + list.append( best ); + } + + ps = makePSFontName( fe, &i, &type ); + + const psfont *psf = postscriptFonts[i].ps; + const psfont * const * replacements = postscriptFonts[i].replacements; + float xscale = 100; + if ( psf ) { + // xscale for the "right" font is always 1. We scale the replacements... + xscale = psf->xscale; + ps = "[ /" + QString::fromLatin1( psf[type].psname ) + " 1.0 " + + toString( psf[type].slant ) + " ]"; + } else { + ps = "[ /" + ps + " 1.0 0.0 ]"; + // only add default replacement fonts in case this font was unknown. + if ( fe->fontDef.fixedPitch ) { + replacements = FixedReplacements; + } else { + replacements = SansSerifReplacements; + // 100 is courier, but most fonts are not as wide as courier. Using 100 + // here would make letters overlap for some fonts. This value is empirical. + xscale = 83; + } + } + list.append( ps ); + + if ( replacements ) + appendReplacements( list, replacements, type, xscale); + return list; +} + +static void emitPSFontNameList( QTextStream &s, const QString &psname, const QStringList &list ) +{ + s << "/" << psname << "List [\n"; + s << list.join("\n "); + s << "\n] d\n"; +} + +static inline float pointSize( const QFont &f, float scale ) +{ + float psize; + if ( f.pointSize() != -1 ) + psize = f.pointSize()/scale; + else + psize = f.pixelSize(); + return psize; +} + + +// ========================== FONT CLASSES =============== + + +QPSPrinterFontPrivate::QPSPrinterFontPrivate() +{ + global_dict = FALSE; + downloaded = FALSE; + symbol = FALSE; + // map 0 to .notdef + subset.insert( 0, 0 ); + subsetCount = 1; + pageSubsetCount = 0; +} + +unsigned short QPSPrinterFontPrivate::insertIntoSubset( unsigned short u ) +{ + unsigned short retval = 0; + if ( subset.find(u) == subset.end() ) { + if ( !downloaded ) { // we need to add to the page subset + subset.insert( u, subsetCount ); // mark it as used + //printf("GLOBAL SUBSET ADDED %04x = %04x\n",u, subsetCount); + retval = subsetCount; + subsetCount++; + } else if ( page_subset.find(u) == page_subset.end() ) { + page_subset.insert( u, pageSubsetCount ); // mark it as used + //printf("PAGE SUBSET ADDED %04x = %04x\n",u, pageSubsetCount); + retval = pageSubsetCount + (subsetCount/256 + 1) * 256; + pageSubsetCount++; + } + } else { + qWarning("QPSPrinterFont::internal error"); + } + return retval; +} + +void QPSPrinterFontPrivate::restore() +{ + page_subset.clear(); + pageSubsetCount = 0; + //qDebug("restore for font %s\n",psname.latin1()); +} + +static inline const char *toHex( uchar u ) +{ + static char hexVal[3]; + int i = 1; + while ( i >= 0 ) { + ushort hex = (u & 0x000f); + if ( hex < 0x0a ) + hexVal[i] = '0'+hex; + else + hexVal[i] = 'A'+(hex-0x0a); + u = u >> 4; + i--; + } + hexVal[2] = '\0'; + return hexVal; +} + +static inline const char *toHex( ushort u ) +{ + static char hexVal[5]; + int i = 3; + while ( i >= 0 ) { + ushort hex = (u & 0x000f); + if ( hex < 0x0a ) + hexVal[i] = '0'+hex; + else + hexVal[i] = 'A'+(hex-0x0a); + u = u >> 4; + i--; + } + hexVal[4] = '\0'; + return hexVal; +} + +static inline const char * toInt( int i ) +{ + static char intVal[20]; + intVal[19] = 0; + int pos = 19; + if ( i == 0 ) { + intVal[--pos] = '0'; + } else { + bool neg = FALSE; + if ( i < 0 ) { + neg = TRUE; + i = -i; + } + while ( i ) { + int dec = i%10; + intVal[--pos] = '0'+dec; + i /= 10; + } + if ( neg ) + intVal[--pos] = '-'; + } + return intVal+pos; +} + +void QPSPrinterFontPrivate::drawText( QTextStream &stream, const QPoint &p, QTextEngine *engine, int item, + const QString &text, QPSPrinterPrivate *d, QPainter *paint) +{ + int len = engine->length( item ); + QScriptItem &si = engine->items[item]; + + int x = p.x() + si.x; + int y = p.y() + si.y; + if ( y != d->textY || d->textY == 0 ) + stream << y << " Y"; + d->textY = y; + + stream << "<"; + if ( si.analysis.bidiLevel % 2 ) { + for ( int i = len-1; i >=0; i-- ) + stream << toHex( mapUnicode(text.unicode()[i].unicode()) ); + } else { + for ( int i = 0; i < len; i++ ) + stream << toHex( mapUnicode(text.unicode()[i].unicode()) ); + } + stream << ">"; + + stream << si.width << " " << x; + + if ( paint->font().underline() ) + stream << ' ' << y + d->fm.underlinePos() + d->fm.lineWidth() + << " " << d->fm.lineWidth() << " Tl"; + if ( paint->font().strikeOut() ) + stream << ' ' << y + d->fm.strikeOutPos() + << " " << d->fm.lineWidth() << " Tl"; + stream << " AT\n"; + +} + + +QString QPSPrinterFontPrivate::defineFont( QTextStream &stream, const QString &ps, const QFont &f, const QString &key, + QPSPrinterPrivate *d ) +{ + QString fontName; + fontName.sprintf( "/%s-Uni", ps.latin1()); + + if ( d->buffer ) { + ++d->headerFontNumber; + d->fontStream << "/F" << d->headerFontNumber << " " + << pointSize( f, d->scale ) << fontName << " DF\n"; + fontName.sprintf( "F%d", d->headerFontNumber ); + d->headerFontNames.insert( key, new QString( fontName ) ); + } else { + ++d->pageFontNumber; + stream << "/F" << d->pageFontNumber << " " + << pointSize( f, d->scale ) << fontName << " DF\n"; + fontName.sprintf( "F%d", d->pageFontNumber ); + d->pageFontNames.insert( key, new QString( fontName ) ); + } + return fontName; +} + +unsigned short QPSPrinterFontPrivate::mapUnicode( unsigned short unicode ) +{ + QMap<unsigned short, unsigned short>::iterator res; + res = subset.find( unicode ); + unsigned short offset = 0; + bool found = FALSE; + if ( res != subset.end() ) { + found = TRUE; + } else { + if ( downloaded ) { + res = page_subset.find( unicode ); + offset = (subsetCount/256 + 1) * 256; + if ( res != page_subset.end() ) + found = TRUE; + } + } + if ( !found ) { + return insertIntoSubset( unicode ); + } + //qDebug("mapping unicode %x to %x", unicode, offset+*res); + return offset + *res; +} + +// This map is used for symbol fonts to get the correct glyph names for the latin range +static const unsigned short symbol_map[0x100] = { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, + 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220b, + 0x0028, 0x0029, 0x2217, 0x002b, 0x002c, 0x2212, 0x002e, 0x002f, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, + + 0x2245, 0x0391, 0x0392, 0x03a7, 0x0394, 0x0395, 0x03a6, 0x0393, + 0x0397, 0x0399, 0x03d1, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f, + 0x03a0, 0x0398, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03c2, 0x03a9, + 0x039e, 0x03a8, 0x0396, 0x005b, 0x2234, 0x005d, 0x22a5, 0x005f, + 0xf8e5, 0x03b1, 0x03b2, 0x03c7, 0x03b4, 0x03b5, 0x03c6, 0x03b3, + 0x03b7, 0x03b9, 0x03d5, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf, + 0x03c0, 0x03b8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03d6, 0x03c9, + 0x03be, 0x03c8, 0x03b6, 0x007b, 0x007c, 0x007d, 0x223c, 0x007f, + + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, + 0x20ac, 0x03d2, 0x2023, 0x2264, 0x2044, 0x221e, 0x0192, 0x2263, + 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193, + 0x00b0, 0x00b1, 0x2033, 0x2265, 0x00d7, 0x221d, 0x2202, 0x2022, + 0x00f7, 0x2260, 0x2261, 0x2248, 0x2026, 0xf8e6, 0xf8e7, 0x21b5, + + 0x2135, 0x2111, 0x211c, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229, + 0x222a, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209, + 0x2220, 0x2207, 0xf6da, 0xf6d9, 0xf6db, 0x220f, 0x221a, 0x22c5, + 0x00ac, 0x2227, 0x2228, 0x21d4, 0x21d0, 0x21d1, 0x21d2, 0x21d3, + 0x25ca, 0x2329, 0xf8e8, 0xf8e9, 0xf8ea, 0x2211, 0xf8eb, 0xf8ec, + 0xf8ed, 0xf8ee, 0xf8ef, 0xf8f0, 0xf8f1, 0xf8f2, 0xf8f3, 0xf8f4, + 0x0000, 0x232a, 0x222b, 0x2320, 0xf8f5, 0x2321, 0xf8f6, 0xf8f7, + 0xf8f8, 0xf8f9, 0xf8fa, 0xf8fb, 0xf8fc, 0xf8fd, 0xf8fe, 0x0000, +}; + +QString QPSPrinterFontPrivate::glyphName( unsigned short glyphindex, bool *glyphSet ) +{ + QString glyphname; + int l = 0; + unsigned short unicode = unicode_for_glyph( glyphindex ); + if (symbol && unicode < 0x100) { + // map from latin1 to symbol + unicode = symbol_map[unicode]; + } + if ( !unicode && glyphindex ) { + glyphname = "gl"; + glyphname += toHex( glyphindex ); + } else { + while( unicodetoglyph[l].u < unicode ) + l++; + if ( unicodetoglyph[l].u == unicode ) { + glyphname = unicodetoglyph[l].g; + if ( glyphSet ) { + int other = 0; + switch ( unicode ) { + // some glyph names are duplicate in postscript. Make sure we give the + // duplicate a different name to avoid infinite recursion + case 0x0394: + other = 0x2206; + break; + case 0x03a9: + other = 0x2126; + break; + case 0x0162: + other = 0x021a; + break; + case 0x2215: + other = 0x2044; + break; + case 0x00ad: + other = 0x002d; + break; + case 0x02c9: + other = 0x00af; + break; + case 0x03bc: + other = 0x00b5; + break; + case 0x2219: + other = 0x00b7; + break; + case 0x00a0: + other = 0x0020; + break; + case 0x0163: + other = 0x021b; + break; + default: + break; + } + if ( other ) { + int oglyph = glyph_for_unicode( other ); + if( oglyph && oglyph != glyphindex && glyphSet[oglyph] ) { + glyphname = "uni"; + glyphname += toHex( unicode ); + } + } + } + } else { + glyphname = "uni"; + glyphname += toHex( unicode ); + } + } + return glyphname; +} + +void QPSPrinterFontPrivate::download(QTextStream &s, bool global) +{ + //printf("defining mapping for printer font %s\n",psname.latin1()); + downloadMapping( s, global ); +} + +void QPSPrinterFontPrivate::downloadMapping( QTextStream &s, bool global ) +{ + uchar rangeOffset = 0; + uchar numRanges = (uchar)(subsetCount/256 + 1); + uchar range; + QMap<unsigned short, unsigned short> *subsetDict = ⊂ + if ( !global ) { + rangeOffset = numRanges; + numRanges = pageSubsetCount/256 + 1; + subsetDict = &page_subset; + } + // build up inverse table + unsigned short *inverse = new unsigned short[numRanges * 256]; + memset( inverse, 0, numRanges * 256 * sizeof( unsigned short ) ); + + QMap<unsigned short, unsigned short>::iterator it; + for ( it = subsetDict->begin(); it != subsetDict->end(); ++it) { + const unsigned short &mapped = *it; + inverse[mapped] = it.key(); + } + + QString vector; + QString glyphname; + + for (range=0; range < numRanges; range++) { + //printf("outputing range %04x\n",range*256); + vector = "%% Font Page "; + vector += toHex((uchar)(range + rangeOffset)); + vector += "\n/"; + vector += psname; + vector += "-ENC-"; + vector += toHex((uchar)(range + rangeOffset)); + vector += " [\n"; + + QString line; + for(int k=0; k<256; k++ ) { + int c = range*256 + k; + unsigned short glyph = inverse[c]; + glyphname = glyphName( glyph ); + if ( line.length() + glyphname.length() > 76 ) { + vector += line; + vector += "\n"; + line = ""; + } + line += "/" + glyphname; + } + vector += line; + vector += "] def\n"; + s << vector; + } + + delete [] inverse; + + // DEFINE BASE FONTS + + for (range=0; range < numRanges; range++) { + s << "/"; + s << psname; + s << "-Uni-"; + s << toHex((uchar)(range + rangeOffset)); + s << " "; + s << psname; + s << "-ENC-"; + s << toHex((uchar)(range + rangeOffset)); + if ( embedded() && embedFonts ) { + s << " /"; + s << psname; + s << " MFEmb\n"; + } else { + s << " " << psname << "List"; + s << " MF\n"; + } + } + + // === write header === + // int VMMin; + // int VMMax; + + s << wrapDSC( "%%BeginFont: " + psname ) + << "%!PS-AdobeFont-1.0 Composite Font\n" + << wrapDSC( "%%FontName: " + psname + "-Uni" ) + << "%%Creator: Composite font created by Qt\n"; + + /* Start the dictionary which will eventually */ + /* become the font. */ + s << "25 dict begin\n"; // need to verify. Sivan + + s << "/FontName /"; + s << psname; + s << "-Uni"; + s << " def\n"; + s << "/PaintType 0 def\n"; + + // This is concatenated with the base fonts, so it should perform + // no transformation. Sivan + s << "/FontMatrix[1 0 0 1 0 0]def\n"; + + s << "/FontType "; + s << 0; + s << " def\n"; + + // now come composite font structures + // FMapTypes: + // 2: 8/8, 8 bits select the font, 8 the glyph + + s << "/FMapType 2 def\n"; + + // The encoding in a composite font is used for indirection. + // Every char is split into a font-number and a character-selector. + // PostScript prints glyph number character-selector from the font + // FDepVector[ Encoding[ font-number ] ]. + + s << "/Encoding ["; + for (range=0; range < rangeOffset + numRanges; range++) { + if (range % 16 == 0) + s << "\n"; + else + s << " "; + s << range; + } + s << "]def\n"; + + // Descendent fonts + + s << "/FDepVector [\n"; + for (range=0; range < rangeOffset + numRanges; range++) { + s << "/"; + s << psname; + s << "-Uni-"; + s << toHex( range ); + s << " findfont\n"; + } + s << "]def\n"; + + // === trailer === + + s << "FontName currentdict end definefont pop\n"; + s << "%%EndFont\n"; +} + + +// ================== TTF ==================== + +typedef Q_UINT8 BYTE; +typedef Q_UINT16 USHORT; +typedef Q_UINT16 uFWord; +typedef Q_INT16 SHORT; +typedef Q_INT16 FWord; +typedef Q_UINT32 ULONG; +typedef Q_INT32 FIXED; + +typedef struct { + Q_INT16 whole; + Q_UINT16 fraction; +} Fixed; // 16.16 bit fixed-point number + +static float f2dot14( ushort s ) +{ + float f = ((float)( s & 0x3fff ))/ 16384.; + f += (s & 0x8000) ? ( (s & 0x4000) ? -1 : -2 ) : ( (s & 0x4000) ? 1 : 0 ); + return f; +} + +typedef struct { + int* epts_ctr; /* array of contour endpoints */ + int num_pts, num_ctr; /* number of points, number of coutours */ + FWord* xcoor, *ycoor; /* arrays of x and y coordinates */ + BYTE* tt_flags; /* array of TrueType flags */ + double* area_ctr; + char* check_ctr; + int* ctrset; /* in contour index followed by out contour index */ +} charproc_data; + + +class QPSPrinterFontTTF + : public QPSPrinterFontPrivate { +public: + QPSPrinterFontTTF(const QFontEngine *f, QByteArray& data); + virtual void download(QTextStream& s, bool global); + virtual void drawText( QTextStream &stream, const QPoint &p, QTextEngine *engine, int item, + const QString &text, QPSPrinterPrivate *d, QPainter *paint); + // virtual ~QPSPrinterFontTTF(); + + virtual bool embedded() { return TRUE; } +private: + QByteArray data; + QMemArray<ushort> uni2glyph; // to speed up lookups + QMemArray<ushort> glyph2uni; // to speed up lookups + bool defective; // if we can't process this file + + BYTE* getTable(const char *); + void uni2glyphSetup(); + unsigned short unicode_for_glyph(int glyphindex); + unsigned short glyph_for_unicode(unsigned short unicode); + int topost(FWord x) { return (int)( ((int)(x) * 1000 + HUPM) / unitsPerEm ); } + +#ifdef Q_PRINTER_USE_TYPE42 + void sfnts_pputBYTE(BYTE n,QTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_pputUSHORT(USHORT n,QTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_pputULONG(ULONG n,QTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_end_string(QTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_new_table(ULONG length,QTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_glyf_table(ULONG oldoffset, + ULONG correct_total_length, + QTextStream& s, + int& string_len, int& line_len, bool& in_string); + void download_sfnts(QTextStream& s); +#endif + + void subsetGlyph(int charindex,bool* glyphset); + + void charproc(int charindex, QTextStream& s, bool *glyphSet); + BYTE* charprocFindGlyphData(int charindex); + void charprocComposite(BYTE *glyph, QTextStream& s, bool *glyphSet); + void charprocLoad(BYTE *glyph, charproc_data* cd); + + int target_type; /* 42 or 3 */ + + int numTables; /* number of tables present */ + QString PostName; /* Font's PostScript name */ + QString FullName; /* Font's full name */ + QString FamilyName; /* Font's family name */ + QString Style; /* Font's style string */ + QString Copyright; /* Font's copyright string */ + QString Version; /* Font's version string */ + QString Trademark; /* Font's trademark string */ + int llx,lly,urx,ury; /* bounding box */ + + Fixed TTVersion; /* Truetype version number from offset table */ + Fixed MfrRevision; /* Revision number of this font */ + + BYTE *offset_table; /* Offset table in memory */ + BYTE *post_table; /* 'post' table in memory */ + + BYTE *loca_table; /* 'loca' table in memory */ + BYTE *glyf_table; /* 'glyf' table in memory */ + BYTE *hmtx_table; /* 'hmtx' table in memory */ + + USHORT numberOfHMetrics; + int unitsPerEm; /* unitsPerEm converted to int */ + int HUPM; /* half of above */ + + int numGlyphs; /* from 'post' table */ + + int indexToLocFormat; /* short or long offsets */ + +}; + + +static ULONG getULONG(BYTE *p) +{ + int x; + ULONG val=0; + + for(x=0; x<4; x++) { + val *= 0x100; + val += p[x]; + } + + return val; +} + +static USHORT getUSHORT(BYTE *p) +{ + int x; + USHORT val=0; + + for(x=0; x<2; x++) { + val *= 0x100; + val += p[x]; + } + + return val; +} + +static Fixed getFixed(BYTE *s) +{ + Fixed val={0,0}; + + val.whole = ((s[0] * 256) + s[1]); + val.fraction = ((s[2] * 256) + s[3]); + + return val; +} + +static FWord getFWord(BYTE* s) { return (FWord) getUSHORT(s); } +static uFWord getuFWord(BYTE* s) { return (uFWord) getUSHORT(s); } +static SHORT getSHORT(BYTE* s) { return (SHORT) getUSHORT(s); } + +#if 0 +static const char * const Apple_CharStrings[]={ + ".notdef",".null","nonmarkingreturn","space","exclam","quotedbl","numbersign", + "dollar","percent","ampersand","quotesingle","parenleft","parenright", + "asterisk","plus", "comma","hyphen","period","slash","zero","one","two", + "three","four","five","six","seven","eight","nine","colon","semicolon", + "less","equal","greater","question","at","A","B","C","D","E","F","G","H","I", + "J","K", "L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", + "bracketleft","backslash","bracketright","asciicircum","underscore","grave", + "a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s", + "t","u","v","w","x","y","z","braceleft","bar","braceright","asciitilde", + "Adieresis","Aring","Ccedilla","Eacute","Ntilde","Odieresis","Udieresis", + "aacute","agrave","acircumflex","adieresis","atilde","aring","ccedilla", + "eacute","egrave","ecircumflex","edieresis","iacute","igrave","icircumflex", + "idieresis","ntilde","oacute","ograve","ocircumflex","odieresis","otilde", + "uacute","ugrave","ucircumflex","udieresis","dagger","degree","cent", + "sterling","section","bullet","paragraph","germandbls","registered", + "copyright","trademark","acute","dieresis","notequal","AE","Oslash", + "infinity","plusminus","lessequal","greaterequal","yen","mu","partialdiff", + "summation","product","pi","integral","ordfeminine","ordmasculine","Omega", + "ae","oslash","questiondown","exclamdown","logicalnot","radical","florin", + "approxequal","Delta","guillemotleft","guillemotright","ellipsis", + "nobreakspace","Agrave","Atilde","Otilde","OE","oe","endash","emdash", + "quotedblleft","quotedblright","quoteleft","quoteright","divide","lozenge", + "ydieresis","Ydieresis","fraction","currency","guilsinglleft","guilsinglright", + "fi","fl","daggerdbl","periodcentered","quotesinglbase","quotedblbase", + "perthousand","Acircumflex","Ecircumflex","Aacute","Edieresis","Egrave", + "Iacute","Icircumflex","Idieresis","Igrave","Oacute","Ocircumflex","apple", + "Ograve","Uacute","Ucircumflex","Ugrave","dotlessi","circumflex","tilde", + "macron","breve","dotaccent","ring","cedilla","hungarumlaut","ogonek","caron", + "Lslash","lslash","Scaron","scaron","Zcaron","zcaron","brokenbar","Eth","eth", + "Yacute","yacute","Thorn","thorn","minus","multiply","onesuperior", + "twosuperior","threesuperior","onehalf","onequarter","threequarters","franc", + "Gbreve","gbreve","Idot","Scedilla","scedilla","Cacute","cacute","Ccaron", + "ccaron","dmacron","markingspace","capslock","shift","propeller","enter", + "markingtabrtol","markingtabltor","control","markingdeleteltor", + "markingdeletertol","option","escape","parbreakltor","parbreakrtol", + "newpage","checkmark","linebreakltor","linebreakrtol","markingnobreakspace", + "diamond","appleoutline"}; +#endif + +// #define DEBUG_TRUETYPE + +QPSPrinterFontTTF::QPSPrinterFontTTF(const QFontEngine *f, QByteArray& d) +{ + data = d; + defective = FALSE; + + BYTE *ptr; + + target_type = 3; // will work on any printer + //target_type = 42; // works with gs, faster, better quality + +#ifdef Q_PRINTER_USE_TYPE42 + char* environment_preference = getenv("QT_TTFTOPS"); + if (environment_preference) { + if (QString(environment_preference) == "42") + target_type = 42; + else if (QString(environment_preference) == "3") + target_type = 3; + else + qWarning("The value of QT_TTFTOPS must be 42 or 3"); + } +#endif + offset_table = (unsigned char*) data.data(); /* first 12 bytes */ + + /* Determine how many directory entries there are. */ + numTables = getUSHORT( offset_table + 4 ); + + /* Extract information from the "Offset" table. */ + TTVersion = getFixed( offset_table ); + + /* Load the "head" table and extract information from it. */ + ptr = getTable("head"); + if ( !ptr ) { + defective = TRUE; + return; + } + MfrRevision = getFixed( ptr + 4 ); /* font revision number */ + unitsPerEm = getUSHORT( ptr + 18 ); + HUPM = unitsPerEm / 2; +#ifdef DEBUG_TRUETYPE + printf("unitsPerEm=%d",(int)unitsPerEm); +#endif + llx = topost( getFWord( ptr + 36 ) ); /* bounding box info */ + lly = topost( getFWord( ptr + 38 ) ); + urx = topost( getFWord( ptr + 40 ) ); + ury = topost( getFWord( ptr + 42 ) ); + indexToLocFormat = getSHORT( ptr + 50 ); /* size of 'loca' data */ + if(indexToLocFormat != 0 && indexToLocFormat != 1) { + qWarning("TrueType font is unusable because indexToLocFormat != 0"); + defective = TRUE; + return; + } + if( getSHORT(ptr+52) != 0 ) { + qWarning("TrueType font is unusable because glyphDataFormat != 0"); + defective = TRUE; + return; + } + + // === Load information from the "name" table === + + /* Set default values to avoid future references to */ + /* undefined pointers. */ + + psname = FullName = FamilyName = Version = Style = "unknown"; + Copyright = "No copyright notice"; + Trademark = "No trademark notice"; + + BYTE* table_ptr = getTable("name"); /* pointer to table */ + if ( !table_ptr ) { + defective = TRUE; + qDebug("couldn't find name table" ); + return; + } + int numrecords = getUSHORT( table_ptr + 2 ); /* number of names */ + char* strings = (char *)table_ptr + getUSHORT( table_ptr + 4 ); /* start of string storage */ + + BYTE* ptr2 = table_ptr + 6; + for(int x=0; x < numrecords; x++,ptr2+=12) { + int platform = getUSHORT(ptr2); + //int encoding = getUSHORT(ptr2+2); + //int language = getUSHORT(ptr2+4); + int nameid = getUSHORT(ptr2+6); + int length = getUSHORT(ptr2+8); + int offset = getUSHORT(ptr2+10); + + if( platform == 1 && nameid == 0 ) + Copyright.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 1 ) + FamilyName.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 2 ) + Style.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 4 ) + FullName.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 5 ) + Version.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 6 ) + psname.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 7 ) + Trademark.setLatin1(strings+offset,length); + + } + psname.replace(' ', '-'); + psname.replace("/", ""); + if (psname.isEmpty()) + psname = makePSFontName(f); + + //read_cmap(font); + + /* We need to have the PostScript table around. */ + + post_table = getTable("post"); +#if 0 + if ( post_table ) { + Fixed post_format = getFixed( post_table ); + + if( post_format.whole != 2 || post_format.fraction != 0 ) { + qWarning("TrueType font does not have a format 2.0 'post' table"); + qWarning("post format is %d.%d",post_format.whole,post_format.fraction); + // Sivan Feb 2001: no longer defective. + // defective = TRUE; + } + } +#endif + BYTE *maxp = getTable("maxp"); + if ( !maxp ) { + defective = TRUE; + qDebug("no maxp table in font"); + return; + } + numGlyphs = getUSHORT( maxp + 4 ); +// qDebug("number of glyphs is %d", numGlyphs); + replacementList = makePSFontNameList( f, psname ); + uni2glyphSetup(); +} + + +void QPSPrinterFontTTF::drawText( QTextStream &stream, const QPoint &p, QTextEngine *engine, int item, + const QString &text, QPSPrinterPrivate *d, QPainter *paint) +{ + // we draw glyphs here to get correct shaping of arabic and indic + QScriptItem &si = engine->items[item]; + engine->shape( item ); + int len = si.num_glyphs; + + int x = p.x() + si.x; + int y = p.y() + si.y; + if ( y != d->textY || d->textY == 0 ) + stream << y << " Y"; + d->textY = y; + + QCString xyarray; + int xo = 0; + int yo = 0; + + glyph_t *glyphs = engine->glyphs( &si ); + advance_t *advances = engine->advances( &si ); + qoffset_t *offsets = engine->offsets( &si ); +#ifdef Q_WS_X11 + int type = si.fontEngine->type(); + bool glyphIndices = (type == QFontEngine::Xft); + // This helps us get arabic for XLFD fonts working. In that case we have a Unicode + // cmap (== 0), and the glyphs array contains the shaped string. + bool useGlyphAsUnicode = (type == QFontEngine::XLFD && si.fontEngine->cmap() == 0); +#else // Q_WS_QWS + const bool glyphIndices = FALSE; + const bool useGlyphAsUnicode = TRUE; +#endif + stream << "<"; + if ( si.analysis.bidiLevel % 2 ) { + for ( int i = len-1; i >=0; i-- ) { + // map unicode is not really the correct name, as we map glyphs, but we also download glyphs, so this works + unsigned short glyph; + if (glyphIndices) + glyph = glyphs[i]; + else + glyph = glyph_for_unicode(useGlyphAsUnicode ? glyphs[i] : text.unicode()[i].unicode()); + stream << toHex(mapUnicode(glyph)); + if ( i != len-1 ) { + xyarray += toInt( xo + offsets[i].x + advances[i+1] ); + xyarray += " "; + xyarray += toInt( yo + offsets[i].y ); + xyarray += " "; + xo = -offsets[i].x; + yo = -offsets[i].y; + } + } + } else { + for ( int i = 0; i < len; i++ ) { + // map unicode is not really the correct name, as we map glyphs, but we also download glyphs, so this works + unsigned short glyph; + if (glyphIndices) + glyph = glyphs[i]; + else + glyph = glyph_for_unicode(useGlyphAsUnicode ? glyphs[i] : text.unicode()[i].unicode()); + stream << toHex(mapUnicode(glyph)); + if ( i ) { + xyarray += toInt( xo + offsets[i].x + advances[i-1] ); + xyarray += " "; + xyarray += toInt( yo + offsets[i].y ); + xyarray += " "; + xo = -offsets[i].x; + yo = -offsets[i].y; + } + } + } + stream << ">"; + + stream << "[" << xyarray << "0 0]"; + stream << si.width << " " << x; + + if ( paint->font().underline() ) + stream << ' ' << y + d->fm.underlinePos() + d->fm.lineWidth() + << " " << d->fm.lineWidth() << " Tl"; + if ( paint->font().strikeOut() ) + stream << ' ' << y + d->fm.strikeOutPos() + << " " << d->fm.lineWidth() << " Tl"; + stream << " XYT\n"; + +} + + +void QPSPrinterFontTTF::download(QTextStream& s,bool global) +{ + emitPSFontNameList( s, psname, replacementList); + if ( !embedFonts ) { + downloadMapping(s, global); + return; + } + + //qDebug("downloading ttf font %s", psname.latin1() ); + //qDebug("target type=%d", target_type); + global_dict = global; + QMap<unsigned short, unsigned short> *subsetDict = ⊂ + if ( !global ) + subsetDict = &page_subset; + + downloaded = TRUE; + + if (defective) { + s << "% Font "; + s << FullName; + s << " cannot be downloaded\n"; + return; + } + + // === write header === + int VMMin; + int VMMax; + + s << wrapDSC( "%%BeginFont: " + FullName ); + if( target_type == 42 ) { + s << "%!PS-TrueTypeFont-" + << TTVersion.whole + << "." + << TTVersion.fraction + << "-" + << MfrRevision.whole + << "." + << MfrRevision.fraction + << "\n"; + } else { + /* If it is not a Type 42 font, we will use a different format. */ + s << "%!PS-Adobe-3.0 Resource-Font\n"; + } /* See RBIIp 641 */ + + if( Copyright != (char*)NULL ) { + s << wrapDSC( "%%Copyright: " + Copyright ); + } + + if( target_type == 42 ) + s << "%%Creator: Converted from TrueType to type 42 by Qt\n"; + else + s << "%%Creator: Converted from TrueType by Qt\n"; + + /* If VM usage information is available, print it. */ + if( target_type == 42 && post_table) + { + VMMin = (int)getULONG( post_table + 16 ); + VMMax = (int)getULONG( post_table + 20 ); + if( VMMin > 0 && VMMax > 0 ) + s << "%%VMUsage: " << VMMin << " " << VMMax << "\n"; + } + + /* Start the dictionary which will eventually */ + /* become the font. */ + if( target_type != 3 ) { + s << "15 dict begin\n"; + } else { + s << "25 dict begin\n"; + + /* Type 3 fonts will need some subroutines here. */ + s << "/_d{bind def}bind def\n"; + s << "/_m{moveto}_d\n"; + s << "/_l{lineto}_d\n"; + s << "/_cl{closepath eofill}_d\n"; + s << "/_c{curveto}_d\n"; + s << "/_sc{7 -1 roll{setcachedevice}{pop pop pop pop pop pop}ifelse}_d\n"; + s << "/_e{exec}_d\n"; + } + + s << "/FontName /"; + s << psname; + s << " def\n"; + s << "/PaintType 0 def\n"; + + if(target_type == 42) + s << "/FontMatrix[1 0 0 1 0 0]def\n"; + else + s << "/FontMatrix[.001 0 0 .001 0 0]def\n"; + + s << "/FontBBox["; + s<< llx; + s << " "; + s<< lly; + s << " "; + s<< urx; + s << " "; + s<< ury; + s << "]def\n"; + + s << "/FontType "; + s<< target_type; + s << " def\n"; + + // === write encoding === + + s << "/Encoding StandardEncoding def\n"; + + // === write fontinfo dict === + + /* We create a sub dictionary named "FontInfo" where we */ + /* store information which though it is not used by the */ + /* interpreter, is useful to some programs which will */ + /* be printing with the font. */ + s << "/FontInfo 10 dict dup begin\n"; + + /* These names come from the TrueType font's "name" table. */ + s << "/FamilyName ("; + s << FamilyName; + s << ") def\n"; + + s << "/FullName ("; + s << FullName; + s << ") def\n"; + + s << "/Notice ("; + s << Copyright; + s << " "; + s << Trademark; + s << ") def\n"; + + /* This information is not quite correct. */ + s << "/Weight ("; + s << Style; + s << ") def\n"; + + /* Some fonts have this as "version". */ + s << "/Version ("; + s << Version; + s << ") def\n"; + + /* Some information from the "post" table. */ + if ( post_table ) { + Fixed ItalicAngle = getFixed( post_table + 4 ); + s << "/ItalicAngle "; + s << ItalicAngle.whole; + s << "."; + s << ItalicAngle.fraction; + s << " def\n"; + + s << "/isFixedPitch "; + s << (getULONG( post_table + 12 ) ? "true" : "false" ); + s << " def\n"; + + s << "/UnderlinePosition "; + s << (int)getFWord( post_table + 8 ); + s << " def\n"; + + s << "/UnderlineThickness "; + s << (int)getFWord( post_table + 10 ); + s << " def\n"; + } + s << "end readonly def\n"; + +#ifdef Q_PRINTER_USE_TYPE42 + /* If we are generating a type 42 font, */ + /* emmit the sfnts array. */ + if( target_type == 42 ) + download_sfnts(s); +#endif + /* If we are generating a Type 3 font, we will need to */ + /* have the 'loca' and 'glyf' tables arround while */ + /* we are generating the CharStrings. */ + if(target_type == 3) + { + BYTE *ptr; /* We need only one value */ + ptr = getTable("hhea"); + numberOfHMetrics = getUSHORT(ptr + 34); + + loca_table = getTable("loca"); + glyf_table = getTable("glyf"); + hmtx_table = getTable("hmtx"); + } + + // === CharStrings array === + + // subsetting. We turn a char subset into a glyph subset + // and we mark as used the base glyphs of used composite glyphs. + + bool glyphset[65536]; + for(int c=0; c < 65536; c++) + glyphset[c] = FALSE; + glyphset[0] = TRUE; // always output .notdef + + QMap<unsigned short, unsigned short>::iterator it; + for( it = subsetDict->begin(); it != subsetDict->end(); ++it ) { + subsetGlyph( it.key(), glyphset ); + } + int nGlyphs = numGlyphs; + if ( target_type == 3 ) { + nGlyphs = 0;; + for(int c=0; c < 65536; c++) + if ( glyphset[c] ) nGlyphs++; + } + + s << "/CharStrings "; + s << nGlyphs; + s << " dict dup begin\n"; + + // Emmit one key-value pair for each glyph. + for(int x=0; x < 65536; x++) { + if(target_type == 42) { + s << "/"; + s << glyphName( x ); + s << " "; + s << x; + s << " def\n"; + } else { /* type 3 */ + if (!glyphset[x]) continue; + + //qDebug("emitting charproc for glyph %d, name=%s", x, glyphName(x).latin1() ); + s << "/"; + s << glyphName( x, glyphset ); + s << "{"; + charproc(x,s, glyphset); + s << "}_d\n"; /* "} bind def" */ + } + } + + s << "end readonly def\n"; + + // === trailer === + + /* If we are generating a type 3 font, we need to provide */ + /* a BuildGlyph and BuildChar proceedures. */ + if( target_type == 3 ) { + s << "\n"; + + s << "/BuildGlyph\n"; + s << " {exch begin\n"; /* start font dictionary */ + s << " CharStrings exch\n"; + s << " 2 copy known not{pop /.notdef}if\n"; + s << " true 3 1 roll get exec\n"; + s << " end}_d\n"; + + s << "\n"; + + /* This proceedure is for compatiblity with */ + /* level 1 interpreters. */ + s << "/BuildChar {\n"; + s << " 1 index /Encoding get exch get\n"; + s << " 1 index /BuildGlyph get exec\n"; + s << "}_d\n"; + + s << "\n"; + + } + + /* If we are generating a type 42 font, we need to check to see */ + /* if this PostScript interpreter understands type 42 fonts. If */ + /* it doesn't, we will hope that the Apple TrueType rasterizer */ + /* has been loaded and we will adjust the font accordingly. */ + /* I found out how to do this by examining a TrueType font */ + /* generated by a Macintosh. That is where the TrueType interpreter */ + /* setup instructions and part of BuildGlyph came from. */ + else if( target_type == 42 ) { + s << "\n"; + + /* If we have no "resourcestatus" command, or FontType 42 */ + /* is unknown, leave "true" on the stack. */ + s << "systemdict/resourcestatus known\n"; + s << " {42 /FontType resourcestatus\n"; + s << " {pop pop false}{true}ifelse}\n"; + s << " {true}ifelse\n"; + + /* If true, execute code to produce an error message if */ + /* we can't find Apple's TrueDict in VM. */ + s << "{/TrueDict where{pop}{(%%[ Error: no TrueType rasterizer ]%%)= flush}ifelse\n"; + + /* Since we are expected to use Apple's TrueDict TrueType */ + /* reasterizer, change the font type to 3. */ + s << "/FontType 3 def\n"; + + /* Define a string to hold the state of the Apple */ + /* TrueType interpreter. */ + s << " /TrueState 271 string def\n"; + + /* It looks like we get information about the resolution */ + /* of the printer and store it in the TrueState string. */ + s << " TrueDict begin sfnts save\n"; + s << " 72 0 matrix defaultmatrix dtransform dup\n"; + s << " mul exch dup mul add sqrt cvi 0 72 matrix\n"; + s << " defaultmatrix dtransform dup mul exch dup\n"; + s << " mul add sqrt cvi 3 -1 roll restore\n"; + s << " TrueState initer end\n"; + + /* This BuildGlyph procedure will look the name up in the */ + /* CharStrings array, and then check to see if what it gets */ + /* is a procedure. If it is, it executes it, otherwise, it */ + /* lets the TrueType rasterizer loose on it. */ + + /* When this proceedure is executed the stack contains */ + /* the font dictionary and the character name. We */ + /* exchange arguments and move the dictionary to the */ + /* dictionary stack. */ + s << " /BuildGlyph{exch begin\n"; + /* stack: charname */ + + /* Put two copies of CharStrings on the stack and consume */ + /* one testing to see if the charname is defined in it, */ + /* leave the answer on the stack. */ + s << " CharStrings dup 2 index known\n"; + /* stack: charname CharStrings bool */ + + /* Exchange the CharStrings dictionary and the charname, */ + /* but if the answer was false, replace the character name */ + /* with ".notdef". */ + s << " {exch}{exch pop /.notdef}ifelse\n"; + /* stack: CharStrings charname */ + + /* Get the value from the CharStrings dictionary and see */ + /* if it is executable. */ + s << " get dup xcheck\n"; + /* stack: CharStrings_entry */ + + /* If is a proceedure. Execute according to RBIIp 277-278. */ + s << " {currentdict systemdict begin begin exec end end}\n"; + + /* Is a TrueType character index, let the rasterizer at it. */ + s << " {TrueDict begin /bander load cvlit exch TrueState render end}\n"; + + s << " ifelse\n"; + + /* Pop the font's dictionary off the stack. */ + s << " end}bind def\n"; + + /* This is the level 1 compatibility BuildChar procedure. */ + /* See RBIIp 281. */ + s << " /BuildChar{\n"; + s << " 1 index /Encoding get exch get\n"; + s << " 1 index /BuildGlyph get exec\n"; + s << " }bind def\n"; + + /* Here we close the condition which is true */ + /* if the printer has no built-in TrueType */ + /* rasterizer. */ + s << "}if\n"; + s << "\n"; + } /* end of if Type 42 not understood. */ + + s << "FontName currentdict end definefont pop\n"; + + downloadMapping(s, global); + s << "%%EndFont\n"; +} + +BYTE* QPSPrinterFontTTF::getTable(const char* name) +{ + BYTE *ptr; + int x; + + /* We must search the table directory. */ + ptr = offset_table + 12; + x=0; + while (x != numTables) { + if( strncmp((const char *)ptr,name,4) == 0 ) { + ULONG offset; + //ULONG length; + BYTE *table; + + offset = getULONG( ptr + 8 ); + //length = getULONG( ptr + 12 ); + + table = offset_table + offset; + return table; + } + + x++; + ptr += 16; + } + + return 0; +} + +void QPSPrinterFontTTF::uni2glyphSetup() +{ + uni2glyph.resize(65536); + int i; + for (i=0; i<65536; i++) uni2glyph[i] = 0x0000; + glyph2uni.resize(65536); + for (i=0; i<65536; i++) glyph2uni[i] = 0x0000; + + unsigned char* cmap = getTable("cmap"); + int pos = 0; + + //USHORT version = getUSHORT(cmap + pos); + pos += 2; + USHORT nmaps = getUSHORT(cmap + pos); pos += 2; + + //fprintf(stderr,"cmap version %d (should be 0), %d maps\n",version,nmaps); + + ULONG offset = 0; + int map = -1; + bool symbol = TRUE; + for (i=0; i<nmaps; i++) { + USHORT platform = getUSHORT(cmap+pos); pos+=2; + USHORT encoding = getUSHORT(cmap+pos); pos+=2; + offset = getULONG( cmap+pos); pos+=4; + //fprintf(stderr,"[%d] plat %d enc %d\n",i,platform,encoding); + if (platform == 3 && encoding == 1) { + map = i; + symbol = FALSE; + break; // unicode + } + if (platform == 3 && encoding == 0) { + // symbol, continue looking + map = i; + } + } + if (map==nmaps) { + qWarning("Font does not have unicode encoding\n"); + return; // no unicode encoding! + } + + pos = 8*map; + //fprintf(stderr,"Doing Unicode encoding\n"); + + pos = offset; + USHORT format = getUSHORT(cmap+pos); pos+=2; + //fprintf(stderr,"Unicode cmap format %d\n",format); + + if (format != 4) { + //qWarning("Unicode cmap format is not 4"); + return; + } + + pos += 2; // length + pos += 2; // version + USHORT segcount = getUSHORT(cmap+pos) / 2; pos+=2; + + //fprintf(stderr,"Unicode cmap seg count %d\n",segcount); + + // skip search data + pos += 2; + pos += 2; + pos += 2; + + unsigned char* endcode = cmap + offset + 14; + unsigned char* startcode = cmap + offset + 16 + 2*segcount; + unsigned char* iddelta = cmap + offset + 16 + 4*segcount; + unsigned char* idrangeoff = cmap + offset + 16 + 6*segcount; + //unsigned char* glyphid = cmap + offset + 16 + 8*segcount; + for (i=0; i<segcount; i++) { + USHORT endcode_i = getUSHORT(endcode +2*i); + USHORT startcode_i = getUSHORT(startcode +2*i); + SHORT iddelta_i = getSHORT(iddelta +2*i); + USHORT idrangeoff_i = getUSHORT(idrangeoff+2*i); + +// fprintf(stderr,"[%d] %04x-%04x (%x %x)\n", +// i,startcode_i,endcode_i,iddelta_i,idrangeoff_i); + if (endcode_i == 0xffff) break; // last dummy segment + + if (idrangeoff_i == 0) { + for (USHORT c = startcode_i; c <= endcode_i; c++) { + USHORT g = c + iddelta_i; // glyph index + if ( g != 0 ) { + uni2glyph[g] = c; + glyph2uni[c] = g; + } + } + } else { + for (USHORT c = startcode_i; c <= endcode_i; c++) { + USHORT g = getUSHORT(idrangeoff+2*i + + 2*(c - startcode_i) + + idrangeoff_i); + if ( g != 0 ) { + uni2glyph[g] = c; + glyph2uni[c] = g; + } + } + } + } + if (symbol && glyph2uni[0x40] == 0 && glyph2uni[0xf040] != 0) { + // map 0xf000-0xf0ff into latin1 range. + for (int i = 0; i < 0x100; ++i) { + if (!glyph2uni[i]) + glyph2uni[i] = glyph2uni[i+0xf000]; + + } + } +} + +USHORT QPSPrinterFontTTF::unicode_for_glyph(int glyphindex) +{ + return uni2glyph[glyphindex]; +} + +USHORT QPSPrinterFontTTF::glyph_for_unicode(unsigned short unicode) +{ + return glyph2uni[unicode]; +} + +#ifdef Q_PRINTER_USE_TYPE42 +// ****************** SNFTS ROUTINES ******* + +/*------------------------------------------------------------------- +** sfnts routines +** These routines generate the PostScript "sfnts" array which +** contains one or more strings which contain a reduced version +** of the TrueType font. +** +** A number of functions are required to accomplish this rather +** complicated task. +-------------------------------------------------------------------*/ + +// Write a BYTE as a hexadecimal value as part of the sfnts array. + +void QPSPrinterFontTTF::sfnts_pputBYTE(BYTE n,QTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + static const char hexdigits[]="0123456789ABCDEF"; + + if(!in_string) { + s << "<"; + string_len = 0; + line_len++; + in_string = TRUE; + } + + s << hexdigits[ n / 16 ] ; + s << hexdigits[ n % 16 ] ; + string_len++; + line_len+=2; + + if(line_len > 70) { + s << "\n"; + line_len=0; + } +} + +// Write a USHORT as a hexadecimal value as part of the sfnts array. + +void QPSPrinterFontTTF::sfnts_pputUSHORT(USHORT n,QTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + sfnts_pputBYTE(n / 256,s, string_len, line_len, in_string); + sfnts_pputBYTE(n % 256,s, string_len, line_len, in_string); +} + + +// Write a ULONG as part of the sfnts array. + +void QPSPrinterFontTTF::sfnts_pputULONG(ULONG n,QTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + int x1 = n % 256; n /= 256; + int x2 = n % 256; n /= 256; + int x3 = n % 256; n /= 256; + + sfnts_pputBYTE(n,s , string_len, line_len, in_string); + sfnts_pputBYTE(x3,s, string_len, line_len, in_string); + sfnts_pputBYTE(x2,s, string_len, line_len, in_string); + sfnts_pputBYTE(x1,s, string_len, line_len, in_string); +} + +/* +** This is called whenever it is +** necessary to end a string in the sfnts array. +** +** (The array must be broken into strings which are +** no longer than 64K characters.) +*/ +void QPSPrinterFontTTF::sfnts_end_string(QTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + if(in_string) { + string_len=0; /* fool sfnts_pputBYTE() */ + + // s << "\n% dummy byte:\n"; + + // extra byte for pre-2013 compatibility + sfnts_pputBYTE(0, s, string_len, line_len, in_string); + + s << ">"; + line_len++; + } + + in_string=FALSE; +} + +/* +** This is called at the start of each new table. +** The argement is the length in bytes of the table +** which will follow. If the new table will not fit +** in the current string, a new one is started. +*/ +void QPSPrinterFontTTF::sfnts_new_table(ULONG length,QTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + if( (string_len + length) > 65528 ) + sfnts_end_string(s, string_len, line_len, in_string); +} + +/* +** We may have to break up the 'glyf' table. That is the reason +** why we provide this special routine to copy it into the sfnts +** array. +*/ +void QPSPrinterFontTTF::sfnts_glyf_table(ULONG oldoffset, + ULONG correct_total_length, + QTextStream& s, + int& string_len, int& line_len, bool& in_string) + +{ + int x; + ULONG off; + ULONG length; + int c; + ULONG total=0; /* running total of bytes written to table */ + + loca_table = getTable("loca"); + + int font_off = oldoffset; + + /* Copy the glyphs one by one */ + for(x=0; x < numGlyphs; x++) { + /* Read the glyph offset from the index-to-location table. */ + if(indexToLocFormat == 0) { + off = getUSHORT( loca_table + (x * 2) ); + off *= 2; + length = getUSHORT( loca_table + ((x+1) * 2) ); + length *= 2; + length -= off; + } else { + off = getULONG( loca_table + (x * 4) ); + length = getULONG( loca_table + ((x+1) * 4) ); + length -= off; + } + + // fprintf(stderr,"glyph length=%d",(int)length); + + /* Start new string if necessary. */ + sfnts_new_table( (int)length, s, string_len, line_len, in_string ); + + /* + ** Make sure the glyph is padded out to a + ** two byte boundary. + */ + if( length % 2 ) { + qWarning("TrueType font contains a 'glyf' table without 2 byte padding"); + defective = TRUE; + return; + } + + /* Copy the bytes of the glyph. */ + while( length-- ) { + c = offset_table[ font_off ]; + font_off++; + + sfnts_pputBYTE(c, s, string_len, line_len, in_string); + total++; /* add to running total */ + } + } + + /* Pad out to full length from table directory */ + while( total < correct_total_length ) { + sfnts_pputBYTE(0, s, string_len, line_len, in_string); + total++; + } + + /* Look for unexplainable descrepancies between sizes */ + if( total != correct_total_length ) { + qWarning("QPSPrinterFontTTF::sfnts_glyf_table: total != correct_total_length"); + defective = TRUE; + return; + } +} + +/* +** Here is the routine which ties it all together. +** +** Create the array called "sfnts" which +** holds the actual TrueType data. +*/ + +void QPSPrinterFontTTF::download_sfnts(QTextStream& s) +{ + // tables worth including in a type 42 font + char *table_names[]= { + "cvt ", + "fpgm", + "glyf", + "head", + "hhea", + "hmtx", + "loca", + "maxp", + "prep" + }; + + struct { /* The location of each of */ + ULONG oldoffset; /* the above tables. */ + ULONG newoffset; + ULONG length; + ULONG checksum; + } tables[9]; + + int c; /* Input character. */ + int diff; + int count; /* How many `important' tables did we find? */ + + BYTE* ptr = offset_table + 12; // original table directory + ULONG nextoffset=0; + count=0; + + /* + ** Find the tables we want and store there vital + ** statistics in tables[]. + */ + for(int x=0; x < 9; x++ ) { + do { + diff = strncmp( (char*)ptr, table_names[x], 4 ); + + if( diff > 0 ) { /* If we are past it. */ + tables[x].length = 0; + diff = 0; + } + else if( diff < 0 ) { /* If we haven't hit it yet. */ + ptr += 16; + } + else if( diff == 0 ) { /* Here it is! */ + tables[x].newoffset = nextoffset; + tables[x].checksum = getULONG( ptr + 4 ); + tables[x].oldoffset = getULONG( ptr + 8 ); + tables[x].length = getULONG( ptr + 12 ); + nextoffset += ( ((tables[x].length + 3) / 4) * 4 ); + count++; + ptr += 16; + } + } while(diff != 0); + } /* end of for loop which passes over the table directory */ + + /* Begin the sfnts array. */ + + s << "/sfnts[<"; + + bool in_string=TRUE; + int string_len=0; + int line_len=8; + + /* Generate the offset table header */ + /* Start by copying the TrueType version number. */ + ptr = offset_table; + for(int x=0; x < 4; x++) + sfnts_pputBYTE( *(ptr++) , s, string_len, line_len, in_string ); + + /* Now, generate those silly numTables numbers. */ + sfnts_pputUSHORT(count,s, string_len, line_len, in_string); /* number of tables */ + if( count == 9 ) { + sfnts_pputUSHORT(7,s, string_len, line_len, in_string); /* searchRange */ + sfnts_pputUSHORT(3,s, string_len, line_len, in_string); /* entrySelector */ + sfnts_pputUSHORT(81,s, string_len, line_len, in_string); /* rangeShift */ + } + else { + qWarning("Fewer than 9 tables selected"); + } + + /* Now, emmit the table directory. */ + for(int x=0; x < 9; x++) { + if( tables[x].length == 0 ) /* Skip missing tables */ + continue; + + /* Name */ + sfnts_pputBYTE( table_names[x][0], s, string_len, line_len, in_string); + sfnts_pputBYTE( table_names[x][1], s, string_len, line_len, in_string); + sfnts_pputBYTE( table_names[x][2], s, string_len, line_len, in_string); + sfnts_pputBYTE( table_names[x][3], s, string_len, line_len, in_string); + + /* Checksum */ + sfnts_pputULONG( tables[x].checksum, s, string_len, line_len, in_string ); + + /* Offset */ + sfnts_pputULONG( tables[x].newoffset + 12 + (count * 16), s, + string_len, line_len, in_string ); + + /* Length */ + sfnts_pputULONG( tables[x].length, s, + string_len, line_len, in_string ); + } + + /* Now, send the tables */ + for(int x=0; x < 9; x++) { + if( tables[x].length == 0 ) /* skip tables that aren't there */ + continue; + + /* 'glyf' table gets special treatment */ + if( strcmp(table_names[x],"glyf")==0 ) { + sfnts_glyf_table(tables[x].oldoffset,tables[x].length, s, + string_len, line_len, in_string); + } else { // other tables should not exceed 64K (not always true; Sivan) + if( tables[x].length > 65535 ) { + qWarning("TrueType font has a table which is too long"); + defective = TRUE; + return; + } + + /* Start new string if necessary. */ + sfnts_new_table(tables[x].length, s, + string_len, line_len, in_string); + + int font_off = tables[x].oldoffset; + /* Copy the bytes of the table. */ + for( int y=0; y < (int)tables[x].length; y++ ) { + c = offset_table[ font_off ]; + font_off++; + + sfnts_pputBYTE(c, s, string_len, line_len, in_string); + } + } + + /* Padd it out to a four byte boundary. */ + int y=tables[x].length; + while( (y % 4) != 0 ) { + sfnts_pputBYTE(0, s, string_len, line_len, in_string); + y++; + } + + } /* End of loop for all tables */ + + /* Close the array. */ + sfnts_end_string(s, string_len, line_len, in_string); + s << "]def\n"; +} +#endif + +// ****************** Type 3 CharProcs ******* + +/* +** This routine is used to break the character +** procedure up into a number of smaller +** procedures. This is necessary so as not to +** overflow the stack on certain level 1 interpreters. +** +** Prepare to push another item onto the stack, +** starting a new proceedure if necessary. +** +** Not all the stack depth calculations in this routine +** are perfectly accurate, but they do the job. +*/ +static int stack_depth = 0; +static void stack(int num_pts, int newnew, QTextStream& s) +{ + if( num_pts > 25 ) { /* Only do something of we will */ + /* have a log of points. */ + if(stack_depth == 0) { + s << "{"; + stack_depth=1; + } + + stack_depth += newnew; /* Account for what we propose to add */ + + if(stack_depth > 100) { + s << "}_e{"; + stack_depth = 3 + newnew; /* A rough estimate */ + } + } +} + +static void stack_end(QTextStream& s) /* called at end */ +{ + if(stack_depth) { + s << "}_e"; + stack_depth=0; + } +} + +// postscript drawing commands + +static void PSMoveto(FWord x, FWord y, QTextStream& ts) +{ + ts << x; + ts << " "; + ts << y; + ts << " _m\n"; +} + +static void PSLineto(FWord x, FWord y, QTextStream& ts) +{ + ts << x; + ts << " "; + ts << y; + ts << " _l\n"; +} + +/* Emmit a PostScript "curveto" command. */ +static void PSCurveto(FWord* xcoor, FWord* ycoor, + FWord x, FWord y, int s, int t, QTextStream& ts) +{ + int N, i; + double sx[3], sy[3], cx[4], cy[4]; + + N = t-s+2; + for(i=0; i<N-1; i++) { + sx[0] = i==0?xcoor[s-1]:(xcoor[i+s]+xcoor[i+s-1])/2; + sy[0] = i==0?ycoor[s-1]:(ycoor[i+s]+ycoor[i+s-1])/2; + sx[1] = xcoor[s+i]; + sy[1] = ycoor[s+i]; + sx[2] = i==N-2?x:(xcoor[s+i]+xcoor[s+i+1])/2; + sy[2] = i==N-2?y:(ycoor[s+i]+ycoor[s+i+1])/2; + cx[3] = sx[2]; + cy[3] = sy[2]; + cx[1] = (2*sx[1]+sx[0])/3; + cy[1] = (2*sy[1]+sy[0])/3; + cx[2] = (sx[2]+2*sx[1])/3; + cy[2] = (sy[2]+2*sy[1])/3; + + ts << (int)cx[1]; + ts << " "; + ts << (int)cy[1]; + ts << " "; + ts << (int)cx[2]; + ts << " "; + ts << (int)cy[2]; + ts << " "; + ts << (int)cx[3]; + ts << " "; + ts << (int)cy[3]; + ts << " _c\n"; + } +} + +/* The PostScript bounding box. */ +/* Variables to hold the character data. */ + +//void load_char(struct TTFONT *font, BYTE *glyph); +//void clear_data(); + +//void PSMoveto(FWord x, FWord y, QTextStream& ts); +//void PSLineto(FWord x, FWord y, QTextStream& ts); +//void PSCurveto(FWord x, FWord y, int s, int t, QTextStream& ts); + +//double area(FWord *x, FWord *y, int n); +//int nextinctr(int co, int ci); +//int nextoutctr(int co); +//int nearout(int ci); +//double intest(int co, int ci); +#define sqr(x) ((x)*(x)) + +#define NOMOREINCTR -1 +#define NOMOREOUTCTR -1 + +/* +** Find the area of a contour? +*/ +static double area(FWord *x, FWord *y, int n) +{ + int i; + double sum; + + sum=x[n-1]*y[0]-y[n-1]*x[0]; + for (i=0; i<=n-2; i++) sum += x[i]*y[i+1] - y[i]*x[i+1]; + return sum; +} + +static int nextoutctr(int /*co*/, charproc_data* cd) +{ + int j; + + for(j=0; j<cd->num_ctr; j++) + if (cd->check_ctr[j]==0 && cd->area_ctr[j] < 0) { + cd->check_ctr[j]=1; + return j; + } + + return NOMOREOUTCTR; +} /* end of nextoutctr() */ + +static int nextinctr(int co, int /*ci*/, charproc_data* cd) +{ + int j; + + for(j=0; j<cd->num_ctr; j++) + if (cd->ctrset[2*j+1]==co) + if (cd->check_ctr[ cd->ctrset[2*j] ]==0) { + cd->check_ctr[ cd->ctrset[2*j] ]=1; + return cd->ctrset[2*j]; + } + + return NOMOREINCTR; +} + +static double intest( int co, int ci, charproc_data *cd ) +{ + int i, j, start, end; + double r1, r2; + FWord xi[3], yi[3]; + + j = start = ( co == 0 ) ? 0 : ( cd->epts_ctr[co - 1] + 1 ); + end = cd->epts_ctr[co]; + i = ( ci == 0 ) ? 0 : ( cd->epts_ctr[ci - 1] + 1 ); + xi[0] = cd->xcoor[i]; + yi[0] = cd->ycoor[i]; + r1 = sqr( cd->xcoor[start] - xi[0] ) + sqr( cd->ycoor[start] - yi[0] ); + + for ( i = start; i <= end; i++ ) { + r2 = sqr( cd->xcoor[i] - xi[0] ) + sqr( cd->ycoor[i] - yi[0] ); + if ( r2 < r1 ) { + r1 = r2; + j = i; + } + } + if ( j == start ) { + xi[1] = cd->xcoor[end]; + yi[1] = cd->ycoor[end]; + } else { + xi[1] = cd->xcoor[j - 1]; + yi[1] = cd->ycoor[j - 1]; + } + if ( j == end ) { + xi[2] = cd->xcoor[start]; + yi[2] = cd->ycoor[start]; + } else { + xi[2] = cd->xcoor[j + 1]; + yi[2] = cd->ycoor[j + 1]; + } + return area( xi, yi, 3 ); +} + +/* +** find the nearest out contour to a specified in contour. +*/ +static int nearout(int ci, charproc_data* cd) +{ + int k = 0; /* !!! is this right? */ + int co; + double a, a1=0; + + for (co=0; co < cd->num_ctr; co++) { + if(cd->area_ctr[co] < 0) { + a=intest(co,ci, cd); + if (a<0 && a1==0) { + k=co; + a1=a; + } + if(a<0 && a1!=0 && a>a1) { + k=co; + a1=a; + } + } + } + + return k; +} /* end of nearout() */ + + +/* +** We call this routine to emmit the PostScript code +** for the character we have loaded with load_char(). +*/ +static void PSConvert(QTextStream& s, charproc_data* cd) +{ + int i,j,k,fst,start_offpt; + int end_offpt=0; + + cd->area_ctr = new double[cd->num_ctr]; + memset(cd->area_ctr, 0, (cd->num_ctr*sizeof(double))); + + cd->check_ctr = new char[cd->num_ctr]; + memset(cd->check_ctr, 0, (cd->num_ctr*sizeof(char))); + + cd->ctrset = new int[2*(cd->num_ctr)]; + memset(cd->ctrset, 0, (cd->num_ctr*2*sizeof(int))); + + cd->check_ctr[0]=1; + cd->area_ctr[0]=area(cd->xcoor, cd->ycoor, cd->epts_ctr[0]+1); + + for (i=1; i<cd->num_ctr; i++) + cd->area_ctr[i]=area(cd->xcoor+cd->epts_ctr[i-1]+1, + cd->ycoor+cd->epts_ctr[i-1]+1, + cd->epts_ctr[i]-cd->epts_ctr[i-1]); + + for (i=0; i<cd->num_ctr; i++) { + if (cd->area_ctr[i]>0) { + cd->ctrset[2*i]=i; + cd->ctrset[2*i+1]=nearout(i,cd); + } else { + cd->ctrset[2*i]=-1; + cd->ctrset[2*i+1]=-1; + } + } + + /* Step thru the coutours. */ + /* I believe that a contour is a detatched */ + /* set of curves and lines. */ + i=j=k=0; + while (i < cd->num_ctr ) { + fst = j = (k==0) ? 0 : (cd->epts_ctr[k-1]+1); + + /* Move to the first point on the contour. */ + stack(cd->num_pts,3,s); + PSMoveto(cd->xcoor[j],cd->ycoor[j],s); + start_offpt = 0; /* No off curve points yet. */ + + /* Step thru the remaining points of this contour. */ + for(j++; j <= cd->epts_ctr[k]; j++) { + if (!(cd->tt_flags[j]&1)) { /* Off curve */ + if (!start_offpt) + { start_offpt = end_offpt = j; } + else + end_offpt++; + } else { /* On Curve */ + if (start_offpt) { + stack(cd->num_pts,7,s); + PSCurveto(cd->xcoor,cd->ycoor, + cd->xcoor[j],cd->ycoor[j], + start_offpt,end_offpt,s); + start_offpt = 0; + } else { + stack(cd->num_pts,3,s); + PSLineto(cd->xcoor[j], cd->ycoor[j],s); + } + } + } + + /* Do the final curve or line */ + /* of this coutour. */ + if (start_offpt) { + stack(cd->num_pts,7,s); + PSCurveto(cd->xcoor,cd->ycoor, + cd->xcoor[fst],cd->ycoor[fst], + start_offpt,end_offpt,s); + } else { + stack(cd->num_pts,3,s); + PSLineto(cd->xcoor[fst],cd->ycoor[fst],s); + } + + k=nextinctr(i,k,cd); + + if (k==NOMOREINCTR) + i=k=nextoutctr(i,cd); + + if (i==NOMOREOUTCTR) + break; + } + + /* Now, we can fill the whole thing. */ + stack(cd->num_pts,1,s); + s << "_cl"; /* "closepath eofill" */ + + /* Free our work arrays. */ + delete [] cd->area_ctr; + delete [] cd->check_ctr; + delete [] cd->ctrset; +} + + +/* +** Load the simple glyph data pointed to by glyph. +** The pointer "glyph" should point 10 bytes into +** the glyph data. +*/ +void QPSPrinterFontTTF::charprocLoad(BYTE *glyph, charproc_data* cd) +{ + int x; + BYTE c, ct; + + /* Read the contour endpoints list. */ + cd->epts_ctr = new int[cd->num_ctr]; + //cd->epts_ctr = (int *)myalloc(cd->num_ctr,sizeof(int)); + for (x = 0; x < cd->num_ctr; x++) { + cd->epts_ctr[x] = getUSHORT(glyph); + glyph += 2; + } + + /* From the endpoint of the last contour, we can */ + /* determine the number of points. */ + cd->num_pts = cd->epts_ctr[cd->num_ctr-1]+1; +#ifdef DEBUG_TRUETYPE + fprintf(stderr,"num_pts=%d\n",cd->num_pts); +#endif + + /* Skip the instructions. */ + x = getUSHORT(glyph); + glyph += 2; + glyph += x; + + /* Allocate space to hold the data. */ + //cd->tt_flags = (BYTE *)myalloc(num_pts,sizeof(BYTE)); + //cd->xcoor = (FWord *)myalloc(num_pts,sizeof(FWord)); + //cd->ycoor = (FWord *)myalloc(num_pts,sizeof(FWord)); + cd->tt_flags = new BYTE[cd->num_pts]; + cd->xcoor = new FWord[cd->num_pts]; + cd->ycoor = new FWord[cd->num_pts]; + + /* Read the flags array, uncompressing it as we go. */ + /* There is danger of overflow here. */ + for (x = 0; x < cd->num_pts; ) { + cd->tt_flags[x++] = c = *(glyph++); + + if (c&8) { /* If next byte is repeat count, */ + ct = *(glyph++); + + if( (x + ct) > cd->num_pts ) { + qWarning("Fatal Error in TT flags"); + return; + } + + while (ct--) + cd->tt_flags[x++] = c; + } + } + + /* Read the x coordinates */ + for (x = 0; x < cd->num_pts; x++) { + if (cd->tt_flags[x] & 2) { /* one byte value with */ + /* external sign */ + c = *(glyph++); + cd->xcoor[x] = (cd->tt_flags[x] & 0x10) ? c : (-1 * (int)c); + } else if(cd->tt_flags[x] & 0x10) { /* repeat last */ + cd->xcoor[x] = 0; + } else { /* two byte signed value */ + cd->xcoor[x] = getFWord(glyph); + glyph+=2; + } + } + + /* Read the y coordinates */ + for(x = 0; x < cd->num_pts; x++) { + if (cd->tt_flags[x] & 4) { /* one byte value with */ + /* external sign */ + c = *(glyph++); + cd->ycoor[x] = (cd->tt_flags[x] & 0x20) ? c : (-1 * (int)c); + } else if (cd->tt_flags[x] & 0x20) { /* repeat last value */ + cd->ycoor[x] = 0; + } else { /* two byte signed value */ + cd->ycoor[x] = getUSHORT(glyph); + glyph+=2; + } + } + + /* Convert delta values to absolute values. */ + for(x = 1; x < cd->num_pts; x++) { + cd->xcoor[x] += cd->xcoor[x-1]; + cd->ycoor[x] += cd->ycoor[x-1]; + } + + for(x=0; x < cd->num_pts; x++) { + cd->xcoor[x] = topost(cd->xcoor[x]); + cd->ycoor[x] = topost(cd->ycoor[x]); + } +} + +#define ARG_1_AND_2_ARE_WORDS 1 +#define ARGS_ARE_XY_VALUES 2 +#define ROUND_XY_TO_GRID 4 +#define WE_HAVE_A_SCALE 8 +/* RESERVED 16 */ +#define MORE_COMPONENTS 32 +#define WE_HAVE_AN_X_AND_Y_SCALE 64 +#define WE_HAVE_A_TWO_BY_TWO 128 +#define WE_HAVE_INSTRUCTIONS 256 +#define USE_MY_METRICS 512 + +void QPSPrinterFontTTF::subsetGlyph(int charindex,bool* glyphset) +{ + USHORT flags; + USHORT glyphIndex; + charproc_data cd; + + glyphset[charindex] = TRUE; + //printf("subsetting %s ==> ",glyphName(charindex).latin1()); + + /* Get a pointer to the data. */ + BYTE* glyph = charprocFindGlyphData( charindex ); + + /* If the character is blank, it has no bounding box, */ + /* otherwise read the bounding box. */ + if( glyph == (BYTE*)NULL ) { + cd.num_ctr=0; + } else { + cd.num_ctr = getSHORT(glyph); + /* Advance the pointer past bounding box. */ + glyph += 10; + } + + if( cd.num_ctr < 0 ) { // composite + /* Once around this loop for each component. */ + do { + flags = getUSHORT(glyph); /* read the flags word */ + glyph += 2; + glyphIndex = getUSHORT(glyph); /* read the glyphindex word */ + glyph += 2; + + glyphset[ glyphIndex ] = TRUE; + subsetGlyph( glyphIndex, glyphset ); + //printf("subset contains: %d %s ",glyphIndex, glyphName(glyphIndex).latin1()); + + if(flags & ARG_1_AND_2_ARE_WORDS) { + glyph += 2; + glyph += 2; + } else { + glyph += 1; + glyph += 1; + } + + if(flags & WE_HAVE_A_SCALE) { + glyph += 2; + } else if(flags & WE_HAVE_AN_X_AND_Y_SCALE) { + glyph += 2; + glyph += 2; + } else if(flags & WE_HAVE_A_TWO_BY_TWO) { + glyph += 2; + glyph += 2; + glyph += 2; + glyph += 2; + } else { + } + } while(flags & MORE_COMPONENTS); + } + //printf("\n"); +} + + +/* +** Emmit PostScript code for a composite character. +*/ +void QPSPrinterFontTTF::charprocComposite(BYTE *glyph, QTextStream& s, bool *glyphSet) +{ + USHORT flags; + USHORT glyphIndex; + int arg1; + int arg2; + float xscale = 1; + float yscale = 1; +#ifdef DEBUG_TRUETYPE + float scale01 = 0; + float scale10 = 0; +#endif + + /* Once around this loop for each component. */ + do { + flags = getUSHORT(glyph); /* read the flags word */ + glyph += 2; + + glyphIndex = getUSHORT(glyph); /* read the glyphindex word */ + glyph += 2; + + if(flags & ARG_1_AND_2_ARE_WORDS) { + /* The tt spec. seems to say these are signed. */ + arg1 = getSHORT(glyph); + glyph += 2; + arg2 = getSHORT(glyph); + glyph += 2; + } else { /* The tt spec. does not clearly indicate */ + /* whether these values are signed or not. */ + arg1 = (char)*(glyph++); + arg2 = (char)*(glyph++); + } + + if(flags & WE_HAVE_A_SCALE) { + xscale = yscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + } else if(flags & WE_HAVE_AN_X_AND_Y_SCALE) { + xscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + yscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + } else if(flags & WE_HAVE_A_TWO_BY_TWO) { + xscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; +#ifdef DEBUG_TRUETYPE + scale01 = f2dot14( getUSHORT(glyph) ); +#endif + glyph += 2; +#ifdef DEBUG_TRUETYPE + scale10 = f2dot14( getUSHORT(glyph) ); +#endif + glyph += 2; + yscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + } + + /* Debugging */ +#ifdef DEBUG_TRUETYPE + s << "% flags=" << flags << ", arg1=" << arg1 << ", arg2=" << arg2 << ", xscale=" << xscale << ", yscale=" << yscale << + ", scale01=" << scale01 << ", scale10=" << scale10 << endl; +#endif + + + if ( (flags & ARGS_ARE_XY_VALUES) != ARGS_ARE_XY_VALUES ) { + s << "% unimplemented shift, arg1=" << arg1; + s << ", arg2=" << arg2 << "\n"; + arg1 = arg2 = 0; + } + + /* If we have an (X,Y) shif and it is non-zero, */ + /* translate the coordinate system. */ + if ( flags & (WE_HAVE_A_TWO_BY_TWO|WE_HAVE_AN_X_AND_Y_SCALE) ) { +#if 0 + // code similar to this would be needed for two_by_two + s << "gsave [ " << xscale << " " << scale01 << " " << scale10 << " " + << yscale << " " << topost(arg1) << " " << topost(arg2) << "] SM\n"; +#endif + if ( flags & WE_HAVE_A_TWO_BY_TWO ) + s << "% Two by two transformation, unimplemented\n"; + s << "gsave " << topost(arg1); + s << " " << topost(arg2); + s << " translate\n"; + s << xscale << " " << yscale << " scale\n"; + } else if ( flags & ARGS_ARE_XY_VALUES && ( arg1 != 0 || arg2 != 0 ) ) { + s << "gsave " << topost(arg1); + s << " " << topost(arg2); + s << " translate\n"; + } + + /* Invoke the CharStrings procedure to print the component. */ + s << "false CharStrings /"; + s << glyphName( glyphIndex, glyphSet ); + s << " get exec\n"; + + // printf("false CharStrings /%s get exec\n", + //ttfont_CharStrings_getname(font,glyphIndex)); + + /* If we translated the coordinate system, */ + /* put it back the way it was. */ + if( (flags & ARGS_ARE_XY_VALUES && (arg1 != 0 || arg2 != 0) ) || + ( flags & (WE_HAVE_A_TWO_BY_TWO|WE_HAVE_AN_X_AND_Y_SCALE) ) ) { + s << "grestore "; + } + } while (flags & MORE_COMPONENTS); +} + +/* +** Return a pointer to a specific glyph's data. +*/ +BYTE* QPSPrinterFontTTF::charprocFindGlyphData(int charindex) +{ + ULONG off; + ULONG length; + + /* Read the glyph offset from the index to location table. */ + if(indexToLocFormat == 0) { + off = getUSHORT( loca_table + (charindex * 2) ); + off *= 2; + length = getUSHORT( loca_table + ((charindex+1) * 2) ); + length *= 2; + length -= off; + } else { + off = getULONG( loca_table + (charindex * 4) ); + length = getULONG( loca_table + ((charindex+1) * 4) ); + length -= off; + } + + if(length > 0) + return glyf_table + off; + else + return (BYTE*)NULL; +} + +void QPSPrinterFontTTF::charproc(int charindex, QTextStream& s, bool *glyphSet ) +{ + int llx,lly,urx,ury; + int advance_width; + charproc_data cd; + +#ifdef DEBUG_TRUETYPE + s << "% tt_type3_charproc for "; + s << charindex; + s << "\n"; +#endif + + /* Get a pointer to the data. */ + BYTE* glyph = charprocFindGlyphData( charindex ); + + /* If the character is blank, it has no bounding box, */ + /* otherwise read the bounding box. */ + if( glyph == (BYTE*)NULL ) { + llx=lly=urx=ury=0; /* A blank char has an all zero BoundingBox */ + cd.num_ctr=0; /* Set this for later if()s */ + } else { + /* Read the number of contours. */ + cd.num_ctr = getSHORT(glyph); + + /* Read PostScript bounding box. */ + llx = getFWord(glyph + 2); + lly = getFWord(glyph + 4); + urx = getFWord(glyph + 6); + ury = getFWord(glyph + 8); + + /* Advance the pointer. */ + glyph += 10; + } + + /* If it is a simple character, load its data. */ + if (cd.num_ctr > 0) + charprocLoad(glyph, &cd); + else + cd.num_pts=0; + + /* Consult the horizontal metrics table to determine */ + /* the character width. */ + if( charindex < numberOfHMetrics ) + advance_width = getuFWord( hmtx_table + (charindex * 4) ); + else + advance_width = getuFWord( hmtx_table + ((numberOfHMetrics-1) * 4) ); + + /* Execute setcachedevice in order to inform the font machinery */ + /* of the character bounding box and advance width. */ + stack(cd.num_pts,7,s); + s << topost(advance_width); + s << " 0 "; + s << topost(llx); + s << " "; + s << topost(lly); + s << " "; + s << topost(urx); + s << " "; + s << topost(ury); + s << " _sc\n"; + + /* If it is a simple glyph, convert it, */ + /* otherwise, close the stack business. */ + if( cd.num_ctr > 0 ) { // simple + PSConvert(s,&cd); + delete [] cd.tt_flags; + delete [] cd.xcoor; + delete [] cd.ycoor; + delete [] cd.epts_ctr; + } else if( cd.num_ctr < 0 ) { // composite + charprocComposite(glyph,s, glyphSet); + } + + stack_end(s); +} /* end of tt_type3_charproc() */ + + +// ================== PFA ==================== + +class QPSPrinterFontPFA + : public QPSPrinterFontPrivate { +public: + QPSPrinterFontPFA(const QFontEngine *f, QByteArray& data); + virtual void download(QTextStream& s, bool global); + virtual bool embedded() { return TRUE; } +private: + QByteArray data; +}; + +QPSPrinterFontPFA::QPSPrinterFontPFA(const QFontEngine *f, QByteArray& d) +{ + data = d; + + int pos = 0; + char* p = data.data(); + QString fontname; + + if (p[ pos ] != '%' || p[ pos+1 ] != '!') { // PFA marker + qWarning("invalid pfa file"); + return; + } + + char* fontnameptr = strstr(p+pos,"/FontName"); + if (fontnameptr == NULL) + return; + + fontnameptr += strlen("/FontName") + 1; + while (*fontnameptr == ' ' || *fontnameptr == '/') fontnameptr++; + int l=0; + while (fontnameptr[l] != ' ') l++; + + psname = QString::fromLatin1(fontnameptr,l); + replacementList = makePSFontNameList( f, psname ); +} + +void QPSPrinterFontPFA::download(QTextStream& s, bool global) +{ + emitPSFontNameList( s, psname, replacementList); + + if ( !embedFonts ) { + downloadMapping(s, global); + return; + } + + //qDebug("downloading pfa font %s", psname.latin1() ); + char* p = data.data(); + + s << "% Font resource\n"; + for (int i=0; i < (int)data.size(); i++) s << p[i]; + s << "% End of font resource\n"; + downloadMapping( s, global ); +} + +// ================== PFB ==================== + +class QPSPrinterFontPFB + : public QPSPrinterFontPrivate { +public: + QPSPrinterFontPFB(const QFontEngine *f, QByteArray& data); + virtual void download(QTextStream& s, bool global); + virtual bool embedded() { return TRUE; } +private: + QByteArray data; +}; + +QPSPrinterFontPFB::QPSPrinterFontPFB(const QFontEngine *f, QByteArray& d) +{ + data = d; + + int pos = 0; + int len; + // int typ; + unsigned char* p = (unsigned char*) data.data(); + QString fontname; + + if (p[ pos ] != 0x80) { // PFB marker + qWarning("pfb file does not start with 0x80"); + return; + } + pos++; + // typ = p[ pos ]; // 1=ascii 2=binary 3=done + pos++; + len = p[ pos ]; pos++; + len |= (p[ pos ] << 8) ; pos++; + len |= (p[ pos ] << 16); pos++; + len |= (p[ pos ] << 24); pos++; + + //printf("font block type %d len %d\n",typ,len); + + char* fontnameptr = strstr((char*)p+pos,"/FontName"); + if (fontnameptr == NULL) + return; + + fontnameptr += strlen("/FontName") + 1; + while (*fontnameptr == ' ' || *fontnameptr == '/') fontnameptr++; + int l=0; + while (fontnameptr[l] != ' ') l++; + + psname = QString::fromLatin1(fontnameptr,l); + replacementList = makePSFontNameList( f, psname ); +} + +void QPSPrinterFontPFB::download(QTextStream& s, bool global) +{ + emitPSFontNameList( s, psname, replacementList); + + if ( !embedFonts ) { + downloadMapping(s, global); + return; + } + + //qDebug("downloading pfb font %s", psname.latin1() ); + unsigned char* p = (unsigned char*) data.data(); + int pos; + int len; + int typ; + + int hexcol = 0; + int line_length = 64; + + s << "% Font resource\n"; + + pos = 0; + typ = -1; + while (typ != 3) { // not end of file + if (p[ pos ] != 0x80) // PFB marker + return; // pfb file does not start with 0x80 + pos++; + typ = p[ pos ]; // 1=ascii 2=binary 3=done + pos++; + + if (typ == 3) break; + + len = p[ pos ]; pos++; + len |= (p[ pos ] << 8) ; pos++; + len |= (p[ pos ] << 16); pos++; + len |= (p[ pos ] << 24); pos++; + + //qDebug("font block type %d len %d",typ,len); + + int end = pos + len; + if (typ==1) { + while (pos < end) { + if (hexcol > 0) { + s << "\n"; + hexcol = 0; + } + //qWarning(QString::fromLatin1((char*)(p+pos),1)); + if (p[pos] == '\r' || p[pos] == '\n') { + s << "\n"; + while (pos < end && (p[pos] == '\r' || p[pos] == '\n')) + pos++; + } else { + s << QString::fromLatin1((char*)(p+pos),1); + pos++; + } + } + } + if (typ==2) { + static const char *hexchar = "0123456789abcdef"; + while (pos < end) { + /* trim hexadecimal lines to line_length columns */ + if (hexcol >= line_length) { + s << "\n"; + hexcol = 0; + } + s << QString::fromLatin1(hexchar+((p[pos] >> 4) & 0xf),1) + << QString::fromLatin1(hexchar+((p[pos] ) & 0xf),1); + pos++; + hexcol += 2; + } + } + } + s << "% End of font resource\n"; + downloadMapping( s, global ); +} + +// ================== AFontFileNotFound ============ + + + +class QPSPrinterFontNotFound + : public QPSPrinterFontPrivate { +public: + QPSPrinterFontNotFound(const QFontEngine* f); + virtual void download(QTextStream& s, bool global); +private: + QByteArray data; +}; + +QPSPrinterFontNotFound::QPSPrinterFontNotFound(const QFontEngine* f) +{ + psname = makePSFontName( f ); + replacementList = makePSFontNameList( f ); +} + +void QPSPrinterFontNotFound::download(QTextStream& s, bool) +{ + //qDebug("downloading not found font %s", psname.latin1() ); + emitPSFontNameList( s, psname, replacementList ); + s << "% No embeddable font for "; + s << psname; + s << " found\n"; + QPSPrinterFontPrivate::download(s, TRUE); +} + +#ifndef QT_NO_TEXTCODEC +// =================== A font file for asian ============ + +class QPSPrinterFontAsian + : public QPSPrinterFontPrivate { +public: + QPSPrinterFontAsian() + : QPSPrinterFontPrivate(), codec( 0 ) {} + void download(QTextStream& s, bool global); + QString defineFont( QTextStream &stream, const QString &ps, const QFont &f, const QString &key, + QPSPrinterPrivate *d ); + void drawText( QTextStream &stream, const QPoint &p, QTextEngine *engine, int item, + const QString &text, QPSPrinterPrivate *d, QPainter *paint ); + + QString makePSFontName( const QFontEngine *f, int type ) const; + virtual QString extension() const = 0; + + QTextCodec *codec; +}; + +QString QPSPrinterFontAsian::makePSFontName( const QFontEngine *f, int type ) const +{ + QString ps; + int i; + + QString family = f->fontDef.family.lower(); + + // try to make a "good" postscript name + ps = family.simplifyWhiteSpace(); + i = 0; + while( (unsigned int)i < ps.length() ) { + if ( i != 0 && ps[i] == '[') { + if ( ps[i-1] == ' ' ) + ps.truncate (i-1); + else + ps.truncate (i); + break; + } + if ( i == 0 || ps[i-1] == ' ' ) { + ps[i] = ps[i].upper(); + if ( i ) + ps.remove( i-1, 1 ); + else + i++; + } else { + i++; + } + } + + switch ( type ) { + case 1: + ps.append( QString::fromLatin1("-Italic") ); + break; + case 2: + ps.append( QString::fromLatin1("-Bold") ); + break; + case 3: + ps.append( QString::fromLatin1("-BoldItalic") ); + break; + case 0: + default: + break; + } + + ps += extension(); + + return ps; +} + + +QString QPSPrinterFontAsian::defineFont( QTextStream &stream, const QString &ps, const QFont &f, + const QString &key, QPSPrinterPrivate *d) +{ + QString fontName; + QString fontName2; + + QString *tmp = d->headerFontNames.find( ps ); + + if ( d->buffer ) { + if ( tmp ) { + fontName = *tmp; + } else { + fontName.sprintf( "F%d", ++d->headerFontNumber ); + d->fontStream << "/" << fontName << " false " << ps << "List MF\n"; + d->headerFontNames.insert( ps, new QString( fontName ) ); + } + fontName2.sprintf( "F%d", ++d->headerFontNumber ); + d->fontStream << "/" << fontName2 << " " + << pointSize( f, d->scale ) << "/" << fontName << " DF\n"; + d->headerFontNames.insert( key, new QString( fontName2 ) ); + } else { + if ( tmp ) { + fontName = *tmp; + } else { + fontName.sprintf( "F%d", ++d->pageFontNumber ); + stream << "/" << fontName << " false " << ps << "List MF\n"; + d->pageFontNames.insert( ps, new QString( fontName ) ); + } + fontName2.sprintf( "F%d", ++d->pageFontNumber ); + stream << "/" << fontName2 << " " + << pointSize( f, d->scale ) << "/" << fontName << " DF\n"; + d->pageFontNames.insert( key, new QString( fontName2 ) ); + } + return fontName2; +} + + +void QPSPrinterFontAsian::download(QTextStream& s, bool) +{ + //qDebug("downloading asian font %s", psname.latin1() ); + s << "% Asian postscript font requested. Using " + << psname << endl; + emitPSFontNameList( s, psname, replacementList ); +} + +void QPSPrinterFontAsian::drawText( QTextStream &stream, const QPoint &p, QTextEngine *engine, int item, + const QString &text, QPSPrinterPrivate *d, QPainter *paint) +{ + int len = engine->length( item ); + QScriptItem &si = engine->items[item]; + + int x = p.x() + si.x; + int y = p.y() + si.y; + if ( y != d->textY || d->textY == 0 ) + stream << y << " Y"; + d->textY = y; + + QString mdf; + if ( paint->font().underline() ) + mdf += " " + QString().setNum( y + d->fm.underlinePos() + d->fm.lineWidth() ) + + " " + toString( d->fm.lineWidth() ) + " Tl"; + if ( paint->font().strikeOut() ) + mdf += " " + QString().setNum( y + d->fm.strikeOutPos() ) + + " " + toString( d->fm.lineWidth() ) + " Tl"; + QCString mb; + QCString out; + QString dummy( QChar(0x20) ); + + if ( si.analysis.bidiLevel % 2 ) { + for ( int i = len-1; i >= 0; i-- ) { + QChar ch = text.unicode()[i]; + if ( !ch.row() ) { + ; // ignore, we should never get here anyway + } else { + if ( codec ) { + dummy[0] = ch; + mb = codec->fromUnicode( dummy ); + } else + mb = " "; + + for ( unsigned int j = 0; j < mb.length (); j++ ) { + if ( mb.at(j) == '(' || mb.at(j) == ')' || mb.at(j) == '\\' ) + out += "\\"; + out += mb.at(j); + } + } + } + } else { + for ( int i = 0; i < len; i++ ) { + QChar ch = text.unicode()[i]; + if ( !ch.row() ) { + ; // ignore, we should never get here anyway + } else { + if ( codec ) { + dummy[0] = ch; + mb = codec->fromUnicode( dummy ); + } else + mb = " "; + + for ( unsigned int j = 0; j < mb.length (); j++ ) { + if ( mb.at(j) == '(' || mb.at(j) == ')' || mb.at(j) == '\\' ) + out += "\\"; + out += mb.at(j); + } + } + } + } + stream << "(" << out << ")" << si.width << " " << x << mdf << " AT\n"; +} + +// ----------- Japanese -------------- + +static const psfont Japanese1 [] = { + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. }, + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. } +}; + +static const psfont Japanese1a [] = { + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. }, + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. } +}; + +static const psfont Japanese2 [] = { + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. }, + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. } +}; + +static const psfont Japanese2a [] = { + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. }, + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. } +}; + + +// Wadalab fonts + +static const psfont WadaMin [] = { + { "WadaMin-Regular-H", 0, 100. }, + { "WadaMin-Regular-H", 0.2, 100. }, + { "WadaMin-Bold-H", 0, 100. }, + { "WadaMin-Bold-H", 0.2, 100. } +}; + +static const psfont WadaGo [] = { + { "WadaMaruGo-Regular-H", 0, 100. }, + { "WadaMaruGo-Regular-H", 0.2, 100. }, + { "WadaGo-Bold-H", 0, 100. }, + { "WadaGo-Bold-H", 0.2, 100. } +}; + +// Adobe Wadalab + +static const psfont WadaGoAdobe [] = { + { "WadaMaruGo-RegularH-Hojo-H", 0, 100. }, + { "WadaMaruGo-RegularH-Hojo-H", 0.2, 100. }, + { "WadaMaruGo-RegularH-Hojo-H", 0, 100. }, + { "WadaMaruGo-RegularH-Hojo-H", 0.2, 100. }, +}; +static const psfont WadaMinAdobe [] = { + { "WadaMin-RegularH-Hojo-H", 0, 100. }, + { "WadaMin-RegularH-Hojo-H", 0.2, 100. }, + { "WadaMin-RegularH-Hojo-H", 0, 100. }, + { "WadaMin-RegularH-Hojo-H", 0.2, 100. }, +}; + + +static const psfont * const Japanese1Replacements[] = { + Japanese1, Japanese1a, WadaMin, WadaGo, WadaMinAdobe, WadaGoAdobe, 0 +}; +static const psfont * const Japanese2Replacements[] = { + Japanese2, Japanese2a, WadaMin, WadaGo, WadaMinAdobe, WadaGoAdobe, 0 +}; + +class QPSPrinterFontJapanese + : public QPSPrinterFontAsian { +public: + QPSPrinterFontJapanese(const QFontEngine* f); + virtual QString extension() const; +}; + +QPSPrinterFontJapanese::QPSPrinterFontJapanese(const QFontEngine* f) +{ + codec = QTextCodec::codecForMib( 63 ); // jisx0208.1983-0 + + int type = getPsFontType( f ); + psname = makePSFontName( f, type ); + QString best = "[ /" + psname + " 1.0 0.0 ]"; + replacementList.append( best ); + + const psfont *const *replacements = ( psname.contains( "Helvetica" ) ? Japanese2Replacements : Japanese1Replacements ); + appendReplacements( replacementList, replacements, type ); +} + +QString QPSPrinterFontJapanese::extension() const +{ + return "-H"; +} + +// ----------- Korean -------------- + +// sans serif +static const psfont SMGothic [] = { + { "SMGothic-Medium-KSC-EUC-H", 0, 100. }, + { "SMGothic-Medium-KSC-EUC-H", 0.2, 100. }, + { "SMGothic-DemiBold-KSC-EUC-H", 0, 100. }, + { "SMGothic-DemiBold-KSC-EUC-H", 0.2, 100. } +}; + +// serif +#if 0 // ### this is never used? +static const psfont SMMyungjo [] = { + { "SMMyungjo-Light-KSC-EUC-H", 0, 100. }, + { "SMMyungjo-Light-KSC-EUC-H", 0.2, 100. }, + { "SMMyungjo-Bold-KSC-EUC-H", 0, 100. }, + { "SMMyungjo-Bold-KSC-EUC-H", 0.2, 100. } +}; +#endif + +static const psfont MKai [] = { + { "MingMT-Light-KSC-EUC-H", 0, 100. }, + { "MingMT-Light-KSC-EUC-H", 0.2, 100. }, + { "MKai-Medium-KSC-EUC-H", 0, 100. }, + { "MKai-Medium-KSC-EUC-H", 0.2, 100. }, +}; + + +static const psfont Munhwa [] = { + { "Munhwa-Regular-KSC-EUC-H", 0, 100. }, + { "Munhwa-Regular-KSC-EUC-H", 0.2, 100. }, + { "Munhwa-Bold-KSC-EUC-H", 0, 100. }, + { "Munhwa-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaGothic [] = { + { "MunhwaGothic-Regular-KSC-EUC-H", 0, 100. }, + { "MunhwaGothic-Regular-KSC-EUC-H", 0.2, 100. }, + { "MunhwaGothic-Bold-KSC-EUC-H", 0, 100. }, + { "MunhwaGothic-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaGungSeo [] = { + { "MunhwaGungSeo-Light-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeo-Light-KSC-EUC-H", 0.2, 100. }, + { "MunhwaGungSeo-Bold-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeo-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaGungSeoHeulim [] = { + { "MunhwaGungSeoHeulim-Light-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeoHeulim-Light-KSC-EUC-H", 0.2, 100. }, + { "MunhwaGungSeoHeulim-Bold-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeoHeulim-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaHoonMin [] = { + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0, 100. }, + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0.2, 100. }, + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0, 100. }, + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont BaekmukGulim [] = { + { "Baekmuk-Gulim-KSC-EUC-H", 0, 100. }, + { "Baekmuk-Gulim-KSC-EUC-H", 0.2, 100. }, + { "Baekmuk-Gulim-KSC-EUC-H", 0, 100. }, + { "Baekmuk-Gulim-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont * const KoreanReplacements[] = { + BaekmukGulim, SMGothic, Munhwa, MunhwaGothic, MKai, MunhwaGungSeo, + MunhwaGungSeoHeulim, MunhwaHoonMin, Helvetica, 0 +}; + +class QPSPrinterFontKorean + : public QPSPrinterFontAsian { +public: + QPSPrinterFontKorean(const QFontEngine* f); + QString extension() const; +}; + +QPSPrinterFontKorean::QPSPrinterFontKorean(const QFontEngine* f) +{ + codec = QTextCodec::codecForMib( 38 ); // eucKR + int type = getPsFontType( f ); + psname = makePSFontName( f, type ); + QString best = "[ /" + psname + " 1.0 0.0 ]"; + replacementList.append( best ); + appendReplacements( replacementList, KoreanReplacements, type ); +} + +QString QPSPrinterFontKorean::extension() const +{ + return "-KSC-EUC-H"; +} +// ----------- traditional chinese ------------ + +// Arphic Public License Big5 TrueType fonts (on Debian and CLE and others) +static const psfont ShanHeiSun [] = { + { "ShanHeiSun-Light-ETen-B5-H", 0, 100. }, + { "ShanHeiSun-Light-ETen-B5-H", 0.2, 100. }, + { "ShanHeiSun-Light-ETen-B5-H", 0, 100. }, + { "ShanHeiSun-Light-ETen-B5-H", 0.2, 100. }, +}; +static const psfont ZenKai [] = { + { "ZenKai-Medium-ETen-B5-H", 0, 100. }, + { "ZenKai-Medium-Italic-ETen-B5-H", 0.2, 100. }, + { "ZenKai-Medium-Bold-ETen-B5-H", 0, 100. }, + { "ZenKai-Medium-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; + +// Fonts on Turbolinux +static const psfont SongB5 [] = { + { "B5-MSung-Light-ETen-B5-H", 0, 100. }, + { "B5-MSung-Italic-ETen-B5-H", 0, 100. }, + { "B5-MSung-Bold-ETen-B5-H", 0, 100. }, + { "B5-MSung-BoldItalic-ETen-B5-H", 0, 100. }, +}; +static const psfont KaiB5 [] = { + { "B5-MKai-Medium-ETen-B5-H", 0, 100. }, + { "B5-MKai-Italic-ETen-B5-H", 0, 100. }, + { "B5-MKai-Bold-ETen-B5-H", 0, 100. }, + { "B5-MKai-BoldItalic-ETen-B5-H", 0, 100. }, +}; +static const psfont HeiB5 [] = { + { "B5-MHei-Medium-ETen-B5-H", 0, 100. }, + { "B5-MHei-Italic-ETen-B5-H", 0, 100. }, + { "B5-MHei-Bold-ETen-B5-H", 0, 100. }, + { "B5-MHei-BoldItalic-ETen-B5-H", 0, 100. }, +}; +static const psfont FangSongB5 [] = { + { "B5-CFangSong-Light-ETen-B5-H", 0, 100. }, + { "B5-CFangSong-Italic-ETen-B5-H", 0, 100. }, + { "B5-CFangSong-Bold-ETen-B5-H", 0, 100. }, + { "B5-CFangSong-BoldItalic-ETen-B5-H", 0, 100. }, +}; + +// Arphic fonts on Thiz Linux +static const psfont LinGothic [] = { + { "LinGothic-Light-ETen-B5-H", 0, 100. }, + { "LinGothic-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "LinGothic-Light-Bold-ETen-B5-H", 0, 100. }, + { "LinGothic-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; +static const psfont YenRound [] = { + { "YenRound-Light-ETen-B5-H", 0, 100. }, + { "YenRound-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "YenRound-Light-Bold-ETen-B5-H", 0, 100. }, + { "YenRound-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; + +// Dr. Wang Hann-Tzong's GPL'ed Big5 TrueType fonts +#if 0 // ### this is never used? +static const psfont HtWFangSong [] = { + { "HtW-FSong-Light-ETen-B5-H", 0, 100. }, + { "HtW-FSong-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "HtW-FSong-Light-Bold-ETen-B5-H", 0, 100. }, + { "HtW-FSong-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; +#endif + +static const psfont MingB5 [] = { + { "Ming-Light-ETen-B5-H", 0, 100. }, + { "Ming-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "Ming-Light-Bold-ETen-B5-H", 0, 100. }, + { "Ming-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; + +// Microsoft's Ming/Sung font? +static const psfont MSung [] = { + { "MSung-Light-ETenms-B5-H", 0, 100. }, + { "MSung-Light-ETenms-B5-H", 0.2, 100. }, + { "MSung-Light-ETenms-B5-H", 0, 100. }, + { "MSung-Light-ETenms-B5-H", 0.2, 100. }, +}; +// "Standard Sung/Ming" font by Taiwan Ministry of Education +static const psfont MOESung [] = { + { "MOESung-Regular-B5-H", 0, 100. }, + { "MOESung-Regular-B5-H", 0.2, 100. }, + { "MOESung-Regular-B5-H", 0, 100. }, + { "MOESung-Regular-B5-H", 0.2, 100. }, +}; + +static const psfont MOEKai [] = { + { "MOEKai-Regular-B5-H", 0, 100. }, + { "MOEKai-Regular-B5-H", 0.2, 100. }, + { "MOEKai-Regular-B5-H", 0, 100. }, + { "MOEKai-Regular-B5-H", 0.2, 100. }, +}; + +static const psfont * const TraditionalReplacements[] = { + MOESung, SongB5, ShanHeiSun, MingB5, MSung, FangSongB5, KaiB5, ZenKai, HeiB5, + LinGothic, YenRound, MOEKai, Helvetica, 0 + }; + +#if 0 // ### these are never used? +static const psfont * const SongB5Replacements[] = { + SongB5, ShanHeiSun, MingB5, MSung, MOESung, Helvetica, 0 + }; + +static const psfont * const FangSongB5Replacements[] = { + FangSongB5, HtWFangSong, Courier, 0 + }; +static const psfont * const KaiB5Replacements[] = { + KaiB5, ZenKai, Times, 0 + }; +static const psfont * const HeiB5Replacements[] = { + HeiB5, LinGothic, YenRound, LucidaSans, 0 + }; +static const psfont * const YuanB5Replacements[] = { + YenRound, LinGothic, HeiB5, LucidaSans, 0 + }; +#endif + + +class QPSPrinterFontTraditionalChinese + : public QPSPrinterFontAsian { +public: + QPSPrinterFontTraditionalChinese(const QFontEngine* f); + QString extension() const; +}; + +QPSPrinterFontTraditionalChinese::QPSPrinterFontTraditionalChinese(const QFontEngine* f) +{ + codec = QTextCodec::codecForMib( 2026 ); // Big5-0 + int type = getPsFontType( f ); + psname = makePSFontName( f, type ); + QString best = "[ /" + psname + " 1.0 0.0 ]"; + replacementList.append( best ); + appendReplacements( replacementList, TraditionalReplacements, type ); +} + +QString QPSPrinterFontTraditionalChinese::extension() const +{ + return "-ETen-B5-H"; +} + +// ----------- simplified chinese ------------ + +#if 0 +// GB18030 fonts on XteamLinux (?) +static const psfont SimplifiedGBK2K [] = { + { "MSung-Light-GBK2K-H", 0, 100. }, + { "MSung-Light-GBK2K-H", 0.2, 100. }, + { "MKai-Medium-GBK2K-H", 0, 100. }, + { "MKai-Medium-GBK2K-H", 0.2, 100. }, +}; +#endif + +// GB18030 fonts on Turbolinux +static const psfont SongGBK2K [] = { + { "MSung-Light-GBK2K-H", 0, 100. }, + { "MSung-Italic-GBK2K-H", 0, 100. }, + { "MSung-Bold-GBK2K-H", 0, 100. }, + { "MSung-BoldItalic-GBK2K-H", 0, 100. }, +}; +static const psfont KaiGBK2K [] = { + { "MKai-Medium-GBK2K-H", 0, 100. }, + { "MKai-Italic-GBK2K-H", 0, 100. }, + { "MKai-Bold-GBK2K-H", 0, 100. }, + { "MKai-BoldItalic-GBK2K-H", 0, 100. }, +}; +static const psfont HeiGBK2K [] = { + { "MHei-Medium-GBK2K-H", 0, 100. }, + { "MHei-Italic-GBK2K-H", 0, 100. }, + { "MHei-Bold-GBK2K-H", 0, 100. }, + { "MHei-BoldItalic-GBK2K-H", 0, 100. }, +}; +static const psfont FangSongGBK2K [] = { + { "CFangSong-Light-GBK2K-H", 0, 100. }, + { "CFangSong-Italic-GBK2K-H", 0, 100. }, + { "CFangSong-Bold-GBK2K-H", 0, 100. }, + { "CFangSong-BoldItalic-GBK2K-H", 0, 100. }, +}; + +static const psfont Simplified [] = { + { "MSung-Light-GBK-EUC-H", 0, 100. }, + { "MSung-Light-GBK-EUC-H", 0.2, 100. }, + { "MKai-Medium-GBK-EUC-H", 0, 100. }, + { "MKai-Medium-GBK-EUC-H", 0.2, 100. }, +}; + +static const psfont MSungGBK [] = { + { "MSung-Light-GBK-EUC-H", 0, 100. }, + { "MSung-Light-GBK-EUC-H", 0.2, 100. }, + { "MSung-Light-GBK-EUC-H", 0, 100. }, + { "MSung-Light-GBK-EUC-H", 0.2, 100. }, +}; + +static const psfont FangSong [] = { + { "CFangSong-Light-GBK-EUC-H", 0, 100. }, + { "CFangSong-Light-GBK-EUC-H", 0.2, 100. }, + { "CFangSong-Light-GBK-EUC-H", 0, 100. }, + { "CFangSong-Light-GBK-EUC-H", 0.2, 100. }, +}; + +// Arphic Public License GB2312 TrueType fonts (on Debian and CLE and others) +static const psfont BousungEG [] = { + { "BousungEG-Light-GB-GB-EUC-H", 0, 100. }, + { "BousungEG-Light-GB-GB-EUC-H", 0.2, 100. }, + { "BousungEG-Light-GB-Bold-GB-EUC-H", 0, 100. }, + { "BousungEG-Light-GB-Bold-GB-EUC-H", 0.2, 100. }, +}; +static const psfont GBZenKai [] = { + { "GBZenKai-Medium-GB-GB-EUC-H", 0, 100. }, + { "GBZenKai-Medium-GB-GB-EUC-H", 0.2, 100. }, + { "GBZenKai-Medium-GB-Bold-GB-EUC-H", 0, 100. }, + { "GBZenKai-Medium-GB-Bold-GB-EUC-H", 0.2, 100. }, +}; + +static const psfont * const SimplifiedReplacements[] = { + SongGBK2K, FangSongGBK2K, KaiGBK2K, HeiGBK2K, + Simplified, MSungGBK, FangSong, BousungEG, GBZenKai, Helvetica, 0 + }; +#if 0 +static const psfont * const SongGBK2KReplacements[] = { + SongGBK2K, MSungGBK, BousungEG, Helvetica, 0 + }; +#endif +static const psfont * const FangSongGBK2KReplacements[] = { + FangSongGBK2K, FangSong, Courier, 0 + }; +static const psfont * const KaiGBK2KReplacements[] = { + KaiGBK2K, GBZenKai, Times, 0 + }; +static const psfont * const HeiGBK2KReplacements[] = { + HeiGBK2K, LucidaSans, 0 + }; + +class QPSPrinterFontSimplifiedChinese + : public QPSPrinterFontAsian { +public: + QPSPrinterFontSimplifiedChinese(const QFontEngine* f); + QString extension() const; +}; + +QPSPrinterFontSimplifiedChinese::QPSPrinterFontSimplifiedChinese(const QFontEngine* f) +{ + codec = QTextCodec::codecForMib( 114 ); // GB18030 + int type = getPsFontType( f ); + QString family = f->fontDef.family.lower(); + if( family.contains("kai",FALSE) ) { + psname = KaiGBK2K[type].psname; + appendReplacements( replacementList, KaiGBK2KReplacements, type ); + } else if( family.contains("fangsong",FALSE) ) { + psname = FangSongGBK2K[type].psname; + appendReplacements( replacementList, FangSongGBK2KReplacements, type ); + } else if( family.contains("hei",FALSE) ) { + psname = HeiGBK2K[type].psname; + appendReplacements( replacementList, HeiGBK2KReplacements, type ); + } else { + psname = SongGBK2K[type].psname; + appendReplacements( replacementList, SimplifiedReplacements, type ); + } + //qDebug("simplified chinese: fontname is %s, psname=%s", f.family().latin1(), psname.latin1() ); +} + +QString QPSPrinterFontSimplifiedChinese::extension() const +{ + return "-GBK2K-H"; +} + +#endif + + +// ================== QPSPrinterFont ==================== + +class QPSPrinterFont { +public: + QPSPrinterFont(const QFont& f, int script, QPSPrinterPrivate *priv); + ~QPSPrinterFont(); + QString postScriptFontName() { return p->postScriptFontName(); } + QString defineFont( QTextStream &stream, const QString &ps, const QFont &f, const QString &key, + QPSPrinterPrivate *d ) + { return p->defineFont( stream, ps, f, key, d ); } + void download(QTextStream& s, bool global) { p->download(s, global); } + QPSPrinterFontPrivate *handle() { return p; } + QString xfontname; +private: + QByteArray data; + QPSPrinterFontPrivate* p; +}; + +QPSPrinterFont::~QPSPrinterFont() +{ + // the dict in QFontPrivate does deletion for us. + // delete p; +} + + +QPSPrinterFont::QPSPrinterFont(const QFont &f, int script, QPSPrinterPrivate *priv) + : p(0) +{ + QString fontfilename; + QString fontname; + + enum { NONE, PFB, PFA, TTF } type = NONE; + + QFontEngine *engine = f.d->engineForScript( (QFont::Script) script ); + // ### implement similar code for QWS and WIN + xfontname = makePSFontName( engine ); + +#if defined( Q_WS_X11 ) + bool xlfd = FALSE; + //qDebug("engine = %p name=%s, script=%d", engine, engine ? engine->name() : "(null)", script); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft && engine && engine->type() == QFontEngine::Xft ) { + XftPattern *pattern = static_cast<QFontEngineXft *>( engine )->pattern(); + char *filename = 0; + XftPatternGetString (pattern, XFT_FILE, 0, &filename); + //qDebug("filename for font is '%s'", filename); + if ( filename ) { + fontfilename = QString::fromLocal8Bit( filename ); + xfontname = fontfilename; + } + } else +#endif + { + QString rawName; + if ( engine && engine != (QFontEngine *)-1 ) + rawName = engine->name(); + int index = rawName.find('-'); + if (index == 0) { + // this is an XLFD font name + for (int i=0; i < 6; i++) { + index = rawName.find('-',index+1); + } + xfontname = rawName.mid(0,index); + if ( xfontname.endsWith( "*" ) ) + xfontname.truncate( xfontname.length() - 1 ); + xlfd = TRUE; + } + } +#endif // Q_WS_X11 +#ifndef QT_NO_TEXTCODEC + // map some scripts to something more useful + if ( script == QFont::Han ) { + QTextCodec *lc = QTextCodec::codecForLocale(); + switch( lc->mibEnum() ) { + case 36: // KS C 5601 + case 38: // EUC KR + script = QFont::Hangul; + break; + + case 57: // gb2312.1980-0 + case 113: // GBK + case -113: // gbk-0 + case 114: // GB18030 + case -114: // gb18030-0 + case 2025: // GB2312 + case 2026: // Big5 + case -2026: // Big5-HKSCS + case 2101: // big5-0, big5.eten-0 + case -2101: // big5hkscs-0, hkscs-1 + break; + + case 16: // JIS7 + case 17: // SJIS + case 18: // EUC JP + case 63: // JIS X 0208 + default: + script = QFont::Hiragana; + break; + } + } else if ( script == QFont::Katakana ) + script = QFont::Hiragana; + else if ( script == QFont::Bopomofo ) + script = QFont::Han; +#endif + + QString searchname = xfontname; +#if defined(Q_WS_X11) + // we need an extension here due to the fact that we use different + // fonts for different scripts + if ( xlfd && script >= QFont::Han && script <= QFont::Bopomofo ) + xfontname += "/" + toString( script ); +#endif + + //qDebug("looking for font %s in dict", xfontname.latin1() ); + p = priv->fonts.find(xfontname); + if ( p ) + return; + +#if defined(Q_WS_X11) + if ( xlfd ) { + + for (QStringList::Iterator it=priv->fontpath.begin(); it!=priv->fontpath.end() && fontfilename.isEmpty(); ++it) { + if ((*it).left(1) != "/") continue; // not a path name, a font server + QString fontmapname; + int num = 0; + // search font.dir and font.scale for the right file + while ( num < 2 ) { + if ( num == 0 ) + fontmapname = (*it) + "/fonts.scale"; + else + fontmapname = (*it) + "/fonts.dir"; + //qWarning(fontmapname); + QFile fontmap(fontmapname); + if (fontmap.open(IO_ReadOnly)) { + while (!fontmap.atEnd()) { + QString mapping; + fontmap.readLine(mapping,512); + // fold to lower (since X folds to lowercase) + //qWarning(xfontname); + //qWarning(mapping); + if (mapping.lower().contains(searchname.lower())) { + int index = mapping.find(' ',0); + QString ffn = mapping.mid(0,index); + // remove the most common bitmap formats + if( !ffn.contains( ".pcf" ) && !ffn.contains( ".bdf" ) && + !ffn.contains( ".spd" ) && !ffn.contains( ".phont" ) ) { + fontfilename = (*it) + QString("/") + ffn; + if ( QFile::exists(fontfilename) ) { + //qDebug("found font file %s", fontfilename.latin1()); + break; + } else // unset fontfilename + fontfilename = QString(); + } + } + } + fontmap.close(); + } + num++; + } + } + } +#endif + + //qDebug("font=%s, fontname=%s, file=%s, p=%p", f.family().latin1(), xfontname.latin1(), fontfilename.latin1(), p); + + // memory mapping would be better here + if (fontfilename.length() > 0) { // maybe there is no file name + QFile fontfile(fontfilename); + if ( fontfile.exists() ) { + //printf("font name %s size = %d\n",fontfilename.latin1(),fontfile.size()); + data = QByteArray( fontfile.size() ); + + fontfile.open(IO_Raw | IO_ReadOnly); + fontfile.readBlock(data.data(), fontfile.size()); + fontfile.close(); + } + } + + if (!data.isNull() && data.size() > 0) { + unsigned char* d = (unsigned char *)data.data(); + if (d[0] == 0x80 && d[1] == 0x01 && d[6] == '%' && d[7] == '!') + type = PFB; + else if (d[0] == '%' && d[1] == '!' && d[2] == 'P' && d[3] == 'S') + type = PFA; + else if (d[0]==0x00 && d[1]==0x01 && d[2]==0x00 && d[3]==0x00) + type = TTF; + else + type = NONE; + } else + type = NONE; + + //qDebug("font is of type %d", type ); + switch (type) { + case TTF : + p = new QPSPrinterFontTTF(engine, data); + break; + case PFB: + p = new QPSPrinterFontPFB(engine, data); + break; + case PFA: + p = new QPSPrinterFontPFA(engine, data); + break; + case NONE: + default: + +#ifndef QT_NO_TEXTCODEC + + if ( script == QFont::Hiragana ) + p = new QPSPrinterFontJapanese( engine ); + else if ( script == QFont::Hangul ) + p = new QPSPrinterFontKorean( engine ); + else if ( script == QFont::Han ) { + QTextCodec *lc = QTextCodec::codecForLocale(); + switch( lc->mibEnum() ) { + case 2025: // GB2312 + case 57: // gb2312.1980-0 + case 113: // GBK + case -113: // gbk-0 + case 114: // GB18030 + case -114: // gb18030-0 + p = new QPSPrinterFontSimplifiedChinese( engine ); + break; + case 2026: // Big5 + case -2026: // big5-0, big5.eten-0 + case 2101: // Big5-HKSCS + case -2101: // big5hkscs-0, hkscs-1 + p = new QPSPrinterFontTraditionalChinese( engine ); + break; + default: + p = new QPSPrinterFontJapanese( engine ); + } + } else +#endif + //qDebug("didnt find font for %s", xfontname.latin1()); + p = new QPSPrinterFontNotFound( engine ); + break; + } + + if (p->postScriptFontName() == "Symbol") + p->setSymbol(); + + // this is needed to make sure we don't get the same postscriptname twice + QDictIterator<QPSPrinterFontPrivate> it( priv->fonts ); + for( it.toFirst(); it.current(); ++it ) { + if ( *(*it) == *p ) { +// qWarning("Post script driver: font already in dict"); + delete p; + p = *it; + return; + } + } + + //qDebug("inserting font %s in dict psname=%s", xfontname.latin1(), p->postScriptFontName().latin1() ); + priv->fonts.insert( xfontname, p ); +} + +// ================= END OF PS FONT METHODS ============ + + +QPSPrinterPrivate::QPSPrinterPrivate( QPrinter *prt, int filedes ) + : buffer( 0 ), outDevice( 0 ), fd( filedes ), pageBuffer( 0 ), fonts(27, FALSE), fontBuffer(0), savedImage( 0 ), + dirtypen( FALSE ), dirtybrush( FALSE ), dirtyBkColor( FALSE ), bkMode( Qt::TransparentMode ), dirtyBkMode( FALSE ), +#ifndef QT_NO_TEXTCODEC + currentFontCodec( 0 ), +#endif + fm( QFont() ), textY( 0 ) +{ + printer = prt; + headerFontNames.setAutoDelete( TRUE ); + pageFontNames.setAutoDelete( TRUE ); + fonts.setAutoDelete( TRUE ); + currentFontFile = 0; + scale = 1.; + scriptUsed = -1; + +#ifdef Q_WS_X11 + // append qsettings fontpath + QSettings settings; + embedFonts = settings.readBoolEntry( "/qt/embedFonts", TRUE ); + + int npaths; + char** font_path; + font_path = XGetFontPath( qt_xdisplay(), &npaths); + bool xfsconfig_read = FALSE; + for (int i=0; i<npaths; i++) { + // If we're using xfs, append font paths from /etc/X11/fs/config + // can't hurt, and chances are we'll get all fonts that way. + if (((font_path[i])[0] != '/') && !xfsconfig_read) { + // We're using xfs -> read its config + bool finished = FALSE; + QFile f("/etc/X11/fs/config"); + if ( !f.exists() ) + f.setName("/usr/X11R6/lib/X11/fs/config"); + if ( !f.exists() ) + f.setName("/usr/X11/lib/X11/fs/config"); + if ( f.exists() ) { + f.open(IO_ReadOnly); + while(f.status()==IO_Ok && !finished) { + QString fs; + f.readLine(fs, 1024); + fs=fs.stripWhiteSpace(); + if (fs.left(9)=="catalogue" && fs.contains('=')) { + fs=fs.mid(fs.find('=')+1).stripWhiteSpace(); + bool end = FALSE; + while( f.status()==IO_Ok && !end ) { + if ( fs[int(fs.length())-1] == ',' ) + fs = fs.left(fs.length()-1); + else + end = TRUE; + if (fs[0] != '#' && !fs.contains(":unscaled")) + fontpath += fs; + f.readLine(fs, 1024); + fs=fs.stripWhiteSpace(); + } + finished = TRUE; + } + } + f.close(); + } + xfsconfig_read = TRUE; + } else if(!strstr(font_path[i], ":unscaled")) { + // Fonts paths marked :unscaled are always bitmapped fonts + // -> we can as well ignore them now and save time + fontpath += font_path[i]; + } + } + XFreeFontPath(font_path); + + // append qsettings fontpath + QStringList fp = settings.readListEntry( "/qt/fontPath", ':' ); + if ( !fp.isEmpty() ) + fontpath += fp; +#else + embedFonts = FALSE; +#endif +} + +QPSPrinterPrivate::~QPSPrinterPrivate() +{ + delete pageBuffer; +} + +void QPSPrinterPrivate::setFont( const QFont & fnt, int script ) +{ + QFont f = fnt; + if ( f.rawMode() ) { + QFont fnt( QString::fromLatin1("Helvetica"), 12 ); + setFont( fnt, QFont::Unicode ); + return; + } + if ( f.pointSize() == 0 ) { +#if defined(CHECK_RANGE) + qWarning( "QPrinter: Cannot set a font with zero point size." ); +#endif + f.setPointSize(QApplication::font().pointSize()); + if ( f.pointSize() == 0 ) + f.setPointSize( 11 ); + } + + QPSPrinterFont ff( f, script, this ); + QString ps = ff.postScriptFontName(); + + QString s = ps; + s.append( ' ' ); + s.prepend( ' ' ); + + QString key = ff.xfontname; + + if ( f.pointSize() != -1 ) + key += " " + toString( f.pointSize() ); + else + key += " px" + toString( f.pixelSize() ); + QString * tmp; + if ( !buffer ) + tmp = pageFontNames.find( key ); + else + tmp = headerFontNames.find( key ); + + QString fontName; + if ( tmp ) + fontName = *tmp; + + if ( fontName.isEmpty() ) { + fontName = ff.defineFont( pageStream, ps, f, key, this ); + } + pageStream << fontName << " F\n"; + + ps.append( ' ' ); + ps.prepend( ' ' ); + if ( !fontsUsed.contains( ps ) ) + fontsUsed += ps; + +#ifndef QT_NO_TEXTCODEC + QTextCodec * codec = 0; +// ### +// #ifndef QT_NO_TEXTCODEC +// i = 0; +// do { +// if ( unicodevalues[i].cs == f.charSet() ) +// codec = QTextCodec::codecForMib( unicodevalues[i++].mib ); +// } while( codec == 0 && unicodevalues[i++].cs != unicodevalues_LAST ); +// #endif + currentFontCodec = codec; +#endif + currentFont = fontName; + currentFontFile = ff.handle(); + scriptUsed = script; +} + + +static void ps_r7( QTextStream& stream, const char * s, int l ) +{ + int i = 0; + uchar line[79]; + int col = 0; + + while( i < l ) { + line[col++] = s[i++]; + if ( col >= 76 ) { + line[col++] = '\n'; + line[col++] = '\0'; + stream << (const char *)line; + col = 0; + } + } + if ( col > 0 ) { + while( (col&3) != 0 ) + line[col++] = '%'; // use a comment as padding + line[col++] = '\n'; + line[col++] = '\0'; + stream << (const char *)line; + } +} + + +static const int quoteSize = 3; // 1-8 pixels +static const int maxQuoteLength = 4+16+32+64+128+256; // magic extended quote +static const int quoteReach = 10; // ... 1-1024 pixels back +static const int tableSize = 1024; // 2 ** quoteReach; +static const int numAttempts = 128; + +static const int hashSize = 71; + +static const int None = INT_MAX; + +/* puts the lowest numBits of data into the out array starting at postion (byte/bit). + Adjusts byte and bit to point ot the next position. + + Need to make sure the out array is long enough before calling the method. +*/ +static void emitBits( char *out, int & byte, int & bit, + int numBits, uint data ) +{ + int b = 0; + uint d = data; + while( b < numBits ) { + if ( bit == 0 ) + out[byte] = 0; + if ( d & 1 ) + out[byte] = (uchar)out[byte] | ( 1 << bit ); + d = d >> 1; + b++; + bit++; + if ( bit > 6 ) { + bit = 0; + byte++; + } + } +} + +//#define DEBUG_COMPRESS +#ifdef DEBUG_COMPRESS +#include <qdatetime.h> +#endif + +static QByteArray compress( const QImage & image, bool gray ) { +#ifdef DEBUG_COMPRESS + QTime t; + t.start(); + int sizeUncompressed[11]; + for( int i = 0; i < 11; i++ ) + sizeUncompressed[i] = 0; + int sizeCompressed[11]; + for( int i = 0; i < 11; i++ ) + sizeCompressed[i] = 0; +#endif + + int width = image.width(); + int height = image.height(); + int depth = image.depth(); + int size = width*height; + + int pastPixel[tableSize]; + int mostRecentPixel[hashSize]; + if ( depth == 1 ) + size = (width+7)/8*height; + else if ( !gray ) + size = size*3; + + unsigned char *pixel = new unsigned char[size+1]; + int i = 0; + if ( depth == 1 ) { + QImage::Endian bitOrder = image.bitOrder(); + memset( pixel, 0xff, size ); + for( int y=0; y < height; y++ ) { + uchar * s = image.scanLine( y ); + for( int x=0; x < width; x++ ) { + // need to copy bit for bit... + bool b = ( bitOrder == QImage::LittleEndian ) ? + (*(s + (x >> 3)) >> (x & 7)) & 1 : + (*(s + (x >> 3)) << (x & 7)) & 0x80 ; + if ( b ) + pixel[i >> 3] ^= (0x80 >> ( i & 7 )); + i++; + } + // we need to align to 8 bit here + i = (i+7) & 0xffffff8; + } + } else if ( depth == 8 ) { + for( int y=0; y < height; y++ ) { + uchar * s = image.scanLine( y ); + for( int x=0; x < width; x++ ) { + QRgb rgb = image.color( s[x] ); + if ( gray ) { + pixel[i] = (unsigned char) qGray( rgb ); + i++; + } else { + pixel[i] = (unsigned char) qRed( rgb ); + pixel[i+1] = (unsigned char) qGreen( rgb ); + pixel[i+2] = (unsigned char) qBlue( rgb ); + i += 3; + } + } + } + } else { + bool alpha = image.hasAlphaBuffer(); + for( int y=0; y < height; y++ ) { + QRgb * s = (QRgb*)(image.scanLine( y )); + for( int x=0; x < width; x++ ) { + QRgb rgb = (*s++); + if ( alpha && qAlpha( rgb ) < 0x40 ) // 25% alpha, convert to white - + rgb = qRgb( 0xff, 0xff, 0xff ); + if ( gray ) { + pixel[i] = (unsigned char) qGray( rgb ); + i++; + } else { + pixel[i] = (unsigned char) qRed( rgb ); + pixel[i+1] = (unsigned char) qGreen( rgb ); + pixel[i+2] = (unsigned char) qBlue( rgb ); + i += 3; + } + } + } + } + + pixel[size] = 0; + + /* this compression function emits blocks of data, where each + block is an unquoted series of pixels, or a quote from earlier + pixels. if the six-letter string "banana" were a six-pixel + image, it might be unquoted "ban" followed by a 3-pixel quote + from -2. note that the final "a" is then copied from the + second "a", which is copied from the first "a" in the same copy + operation. + + the scanning for quotable blocks uses a cobol-like loop and a + hash table: we know how many pixels we need to quote, hash the + first and last pixel we need, and then go backwards in time + looking for some spot where those pixels of those two colours + occur at the right distance from each other. + + when we find a spot, we'll try a string-compare of all the + intervening pixels. we only do a maximum of 128 both-ends + compares or 64 full-string compares. it's more important to be + fast than get the ultimate in compression. + + The format of the compressed stream is as follows: + // 2 bits step size for search and backreference ( 1 or 3 ) + 1 bit compressed or uncompressed block follows + + uncompressed block: + 3 bits size of block in bytes + size*8 bits data + + compressed block: + 3 bits compression header + 0-2 size of block is 1-3 bytes + 3-7 size of block is bigger, 4-8 additional bits specifying size follow + 0/4-8 additional size fields + 10 location of backreference + */ + + for( i=0; i < hashSize; i++ ) + mostRecentPixel[i] = None; + int index = 0; + int emittedUntil = 0; + char *out = (char *)malloc( 256 * sizeof( char ) ); + int outLen = 256; + int outOffset = 0; + int outBit = 0; + + /* we process pixels serially, emitting as necessary/possible. */ + while( index <= size ) { + int bestCandidate = None; + int bestLength = 0; + i = index % tableSize; + int h = pixel[index] % hashSize; + int start, end; + start = end = pastPixel[i] = mostRecentPixel[h]; + mostRecentPixel[h] = index; + /* if our first candidate quote is unusable, or we don't need + to quote because we've already emitted something for this + pixel, just skip. */ + if ( start < index - tableSize || index >= size || + emittedUntil > index) + start = end = None; + int attempts = 0; + /* scan for suitable quote candidates: not too far back, and + if we've found one that's as big as it can get, don't look + for more */ + while( start != None && end != None && + bestLength < maxQuoteLength && + start >= index - tableSize && + end >= index - tableSize + bestLength ) { + /* scan backwards, looking for something good enough to + try a (slow) string comparison. we maintain indexes to + the start and the end of the quote candidate here */ + while( start != None && end != None && + ( pixel[start] != pixel[index] || + pixel[end] != pixel[index+bestLength] ) ) { + if ( attempts++ > numAttempts ) { + start = None; + } else if ( pixel[end] % hashSize == + pixel[index+bestLength] % hashSize ) { + /* we move the area along the end index' chain */ + end = pastPixel[end%tableSize]; + start = end - bestLength; + } else if ( pixel[start] % hashSize == + pixel[index] % hashSize ) { + /* ... or along the start index' chain */ + start = pastPixel[start%tableSize]; + end = start + bestLength; + } else { +#if 0 + /* this should never happen: both the start and + the end pointers ran off their tracks. */ + qDebug( "oops! %06x %06x %06x %06x %5d %5d %5d %d", + pixel[start], pixel[end], + pixel[index], pixel[index+bestLength], + start, end, index, bestLength ); +#endif + /* but if it should happen, no problem. we'll just + say we found nothing, and the compression will + be a bit worse. */ + start = None; + } + /* if we've moved either index too far to use the + quote candidate, let's just give up here. there's + also a guard against "start" insanity. */ + if ( start < index - tableSize || start < 0 || start >= index ) + start = None; + if ( end < index - tableSize + bestLength || end < bestLength ) + end = None; + } + /* ok, now start and end point to an area of suitable + length whose first and last points match, or one/both + is/are set to None. */ + if ( start != None && end != None ) { + /* slow string compare... */ + int length = 0; + while( length < maxQuoteLength && + index+length < size && + pixel[start+length] == pixel[index+length] ) + length++; + /* if we've found something that overlaps the index + point, maybe we can move the quote point back? if + we're copying 10 pixels from 8 pixels back (an + overlap of 2), that'll be faster than copying from + 4 pixels back (an overlap of 6). */ + if ( start + length > index && length > 0 ) { + int d = index-start; + int equal = TRUE; + while( equal && start + length > index && + start > d && start-d >= index-tableSize ) { + int i = 0; + while( equal && i < d ) { + if( pixel[start+i] != pixel[start+i-d] ) + equal = FALSE; + i++; + } + if ( equal ) + start -= d; + } + } + /* if what we have is longer than the best previous + candidate, we'll use this one. */ + if ( length > bestLength ) { + attempts = 0; + bestCandidate = start; + bestLength = length; + if ( length < maxQuoteLength && index + length < size ) + end = mostRecentPixel[pixel[index+length]%hashSize]; + } else { + /* and if it ins't, we'll try some more. but we'll + count each string compare extra, since they're + so expensive. */ + attempts += 2; + if ( attempts > numAttempts ) { + start = None; + } else if ( pastPixel[start%tableSize] + bestLength < + pastPixel[end%tableSize] ) { + start = pastPixel[start%tableSize]; + end = start + bestLength; + } else { + end = pastPixel[end%tableSize]; + start = end - bestLength; + } + } + /* again, if we can't make use of the current quote + candidate, we don't try any more */ + if ( start < index - tableSize || start < 0 || start > size+1 ) + start = None; + if ( end < index - tableSize + bestLength || end < 0 || end > size+1 ) + end = None; + } + } + /* backreferences to 1 byte of data are actually more costly than + emitting the data directly, 2 bytes don't save much. */ + if ( bestCandidate != None && bestLength < 3 ) + bestCandidate = None; + /* at this point, bestCandidate is a candidate of bestLength + length, or else it's None. if we have such a candidate, or + we're at the end, we have to emit all unquoted data. */ + if ( index == size || bestCandidate != None ) { + /* we need a double loop, because there's a maximum length + on the "unquoted data" section. */ + while( emittedUntil < index ) { +#ifdef DEBUG_COMPRESS + int x = 0; + int bl = emittedUntil - index; + while ( (bl /= 2) ) + x++; + if ( x > 10 ) x = 10; + sizeUncompressed[x]++; +#endif + int l = QMIN( 8, index - emittedUntil ); + if ( outOffset + l + 2 >= outLen ) { + outLen *= 2; + out = (char *) realloc( out, outLen ); + } + emitBits( out, outOffset, outBit, + 1, 0 ); + emitBits( out, outOffset, outBit, + quoteSize, l-1 ); + while( l-- ) { + emitBits( out, outOffset, outBit, + 8, pixel[emittedUntil] ); + emittedUntil++; + } + } + } + /* if we have some quoted data to output, do it. */ + if ( bestCandidate != None ) { +#ifdef DEBUG_COMPRESS + int x = 0; + int bl = bestLength; + while ( (bl /= 2) ) + x++; + if ( x > 10 ) x = 10; + sizeCompressed[x]++; +#endif + if ( outOffset + 4 >= outLen ) { + outLen *= 2; + out = (char *) realloc( out, outLen ); + } + emitBits( out, outOffset, outBit, + 1, 1 ); + int l = bestLength - 3; + const struct off_len { + int off; + int bits; + } ol_table [] = { + /* Warning: if you change the table here, change /uc in the PS code! */ + { 3, 0/*dummy*/ }, + { 16, 4 }, + { 32, 5 }, + { 64, 6 }, + { 128, 7 }, + { /*256*/ 0xfffffff, 8 }, + }; + + if ( l < ol_table[0].off ) { + emitBits( out, outOffset, outBit, + quoteSize, l ); + } else { + const off_len *ol = ol_table; + l -= ol->off; + ol++; + while ( l >= ol->off ) { + l -= ol->off; + ol++; + } + emitBits( out, outOffset, outBit, + quoteSize, ol->bits-1 ); + emitBits( out, outOffset, outBit, + ol->bits, l ); + } + emitBits( out, outOffset, outBit, + quoteReach, index - bestCandidate - 1 ); + emittedUntil += bestLength; + } + index++; + } + /* we've output all the data; time to clean up and finish off the + last characters. */ + if ( outBit ) + outOffset++; + i = 0; + /* we have to make sure the data is encoded in a stylish way :) */ + while( i < outOffset ) { + uchar c = out[i]; + c += 42; + if ( c > 'Z' && ( c != 't' || i == 0 || out[i-1] != 'Q' ) ) + c += 84; + out[i] = c; + i++; + } + QByteArray outarr; + outarr.duplicate( out, outOffset ); + free( out ); + delete [] pixel; + +#ifdef DEBUG_COMPRESS + qDebug( "------------- image compression statistics ----------------" ); + qDebug(" compression time %d", t.elapsed() ); + qDebug( "Size dist of uncompressed blocks:" ); + qDebug( "\t%d\t%d\t%d\t%d\t%d\t%d\n", sizeUncompressed[0], sizeUncompressed[1], + sizeUncompressed[2], sizeUncompressed[3], sizeUncompressed[4], sizeUncompressed[5]); + qDebug( "\t%d\t%d\t%d\t%d\t%d\n", sizeUncompressed[6], sizeUncompressed[7], + sizeUncompressed[8], sizeUncompressed[9], sizeUncompressed[10] ); + qDebug( "Size dist of compressed blocks:" ); + qDebug( "\t%d\t%d\t%d\t%d\t%d\t%d\n", sizeCompressed[0], sizeCompressed[1], + sizeCompressed[2], sizeCompressed[3], sizeCompressed[4], sizeCompressed[5]); + qDebug( "\t%d\t%d\t%d\t%d\t%d\n", sizeCompressed[6], sizeCompressed[7], + sizeCompressed[8], sizeCompressed[9], sizeCompressed[10] ); + qDebug( "===> total compression ratio %d/%d = %f", outOffset, size, (float)outOffset/(float)size ); + qDebug( "-----------------------------------------------------------" ); +#endif + + return outarr; +} + +#undef XCOORD +#undef YCOORD +#undef WIDTH +#undef HEIGHT +#undef POINT +#undef RECT +#undef INT_ARG + +#define XCOORD(x) (float)(x) +#define YCOORD(y) (float)(y) +#define WIDTH(w) (float)(w) +#define HEIGHT(h) (float)(h) + +#define POINT(index) XCOORD(p[index].point->x()) << ' ' << \ + YCOORD(p[index].point->y()) << ' ' +#define RECT(index) XCOORD(p[index].rect->normalize().x()) << ' ' << \ + YCOORD(p[index].rect->normalize().y()) << ' ' << \ + WIDTH (p[index].rect->normalize().width()) << ' ' << \ + HEIGHT(p[index].rect->normalize().height()) << ' ' +#define INT_ARG(index) p[index].ival << ' ' + +static char returnbuffer[13]; +static const char * color( const QColor &c, QPrinter * printer ) +{ + if ( c == Qt::black ) + qstrcpy( returnbuffer, "B " ); + else if ( c == Qt::white ) + qstrcpy( returnbuffer, "W " ); + else if ( c.red() == c.green() && c.red() == c.blue() ) + sprintf( returnbuffer, "%d d2 ", c.red() ); + else if ( printer->colorMode() == QPrinter::GrayScale ) + sprintf( returnbuffer, "%d d2 ", + qGray( c.red(), c.green(),c.blue() ) ); + else + sprintf( returnbuffer, "%d %d %d ", + c.red(), c.green(), c.blue() ); + return returnbuffer; +} + + +static const char * psCap( Qt::PenCapStyle p ) +{ + if ( p == Qt::SquareCap ) + return "2 "; + else if ( p == Qt::RoundCap ) + return "1 "; + return "0 "; +} + + +static const char * psJoin( Qt::PenJoinStyle p ) { + if ( p == Qt::BevelJoin ) + return "2 "; + else if ( p == Qt::RoundJoin ) + return "1 "; + return "0 "; +} + + + +void QPSPrinterPrivate::drawImage( QPainter *paint, float x, float y, float w, float h, + const QImage &img, const QImage &mask ) +{ + if ( !w || !h || img.isNull() ) return; + + int width = img.width(); + int height = img.height(); + float scaleX = (float)width/w; + float scaleY = (float)height/h; + + bool gray = (printer->colorMode() == QPrinter::GrayScale) || + img.allGray(); + int splitSize = 21830 * (gray ? 3 : 1 ); + if ( width * height > splitSize ) { // 65535/3, tolerance for broken printers + int images, subheight; + images = ( width * height + splitSize - 1 ) / splitSize; + subheight = ( height + images-1 ) / images; + while ( subheight * width > splitSize ) { + images++; + subheight = ( height + images-1 ) / images; + } + int suby = 0; + while( suby < height ) { + drawImage(paint, x, y + suby/scaleY, w, QMIN( subheight, height-suby )/scaleY, + img.copy( 0, suby, width, QMIN( subheight, height-suby ) ), + mask.isNull() ? mask : mask.copy( 0, suby, width, QMIN( subheight, height-suby ) )); + suby += subheight; + } + } else { + QByteArray out; + int size = 0; + const char *bits; + + if ( !mask.isNull() ) { + out = ::compress( mask, TRUE ); + size = (width+7)/8*height; + pageStream << "/mask " << size << " string uc\n"; + ps_r7( pageStream, out, out.size() ); + pageStream << "d\n"; + } + if ( img.depth() == 1 ) { + size = (width+7)/8*height; + bits = "1 "; + } else if ( gray ) { + size = width*height; + bits = "8 "; + } else { + size = width*height*3; + bits = "24 "; + } + + out = ::compress( img, gray ); + pageStream << "/sl " << size << " string uc\n"; + ps_r7( pageStream, out, out.size() ); + pageStream << "d\n" + << width << ' ' << height << "[" << scaleX << " 0 0 " << scaleY << " 0 0]sl " + << bits << (!mask.isNull() ? "mask " : "false ") + << x << ' ' << y << " di\n"; + } +} + + +void QPSPrinterPrivate::matrixSetup( QPainter *paint ) +{ +#ifndef QT_NO_TRANSFORMATIONS + QWMatrix tmp; + if ( paint->hasViewXForm() ) { + QRect viewport = paint->viewport(); + QRect window = paint->window(); + tmp.translate( viewport.x(), viewport.y() ); + tmp.scale( 1.0 * viewport.width() / window.width(), + 1.0 * viewport.height() / window.height() ); + tmp.translate( -window.x(), -window.y() ); + } + if ( paint->hasWorldXForm() ) { + tmp = paint->worldMatrix() * tmp; + } + pageStream << "[" + << tmp.m11() << ' ' << tmp.m12() << ' ' + << tmp.m21() << ' ' << tmp.m22() << ' ' + << tmp.dx() << ' ' << tmp.dy() + << "]ST\n"; +#else + QPoint p(0,0); + p = paint->xForm(p); + pageStream << "[" + << 0 << ' ' << 0 << ' ' + << 0 << ' ' << 0 << ' ' + << p.x() << ' ' << p.y() + << "]ST\n"; +#endif + dirtyMatrix = FALSE; +} + +void QPSPrinterPrivate::orientationSetup() +{ + if ( printer->orientation() == QPrinter::Landscape ) + pageStream << "QLS\n"; +} + + +void QPSPrinterPrivate::emitHeader( bool finished ) +{ + QString title = printer->docName(); + QString creator = printer->creator(); + if ( !creator ) // default creator + creator = QString::fromLatin1("Qt " QT_VERSION_STR); + outDevice = new QFile(); + (void)((QFile *)outDevice)->open( IO_WriteOnly, fd ); + outStream.setDevice( outDevice ); + outStream << "%!PS-Adobe-1.0"; + QPaintDeviceMetrics m( printer ); + scale = 72. / ((float) m.logicalDpiY()); + uint mtop, mleft, mbottom, mright; + printer->margins( &mtop, &mleft, &mbottom, &mright ); + int width = m.width(); + int height = m.height(); + bool fullPage = printer->fullPage(); + if ( finished && pageCount == 1 && printer->numCopies() == 1 && + ( ( printer->fullPage() && qt_gen_epsf ) || + ( printer->outputToFile() && printer->outputFileName().endsWith( ".eps" ) ) ) + ) { + if ( !boundingBox.isValid() ) + boundingBox.setRect( 0, 0, width, height ); + if ( printer->orientation() == QPrinter::Landscape ) { + if ( !fullPage ) + boundingBox.moveBy( -mleft, -mtop ); + outStream << " EPSF-3.0\n%%BoundingBox: " + << (int)(m.height() - boundingBox.bottom())*scale << " " // llx + << (int)(m.width() - boundingBox.right())*scale - 1 << " " // lly + << (int)(m.height() - boundingBox.top())*scale + 1 << " " // urx + << (int)(m.width() - boundingBox.left())*scale; // ury + } else { + if ( !fullPage ) + boundingBox.moveBy( mleft, -mtop ); + outStream << " EPSF-3.0\n%%BoundingBox: " + << (int)(boundingBox.left())*scale << " " + << (int)(m.height() - boundingBox.bottom())*scale - 1 << " " + << (int)(boundingBox.right())*scale + 1 << " " + << (int)(m.height() - boundingBox.top())*scale; + } + } else { + int w = width + (fullPage ? 0 : mleft + mright); + int h = height + (fullPage ? 0 : mtop + mbottom); + w = (int)(w*scale); + h = (int)(h*scale); + // set a bounding box according to the DSC + if ( printer->orientation() == QPrinter::Landscape ) + outStream << "\n%%BoundingBox: 0 0 " << h << " " << w; + else + outStream << "\n%%BoundingBox: 0 0 " << w << " " << h; + } + outStream << "\n" << wrapDSC( "%%Creator: " + creator ); + if ( !!title ) + outStream << wrapDSC( "%%Title: " + title ); + outStream << "%%CreationDate: " << QDateTime::currentDateTime().toString(); + outStream << "\n%%Orientation: "; + if ( printer->orientation() == QPrinter::Landscape ) + outStream << "Landscape"; + else + outStream << "Portrait"; + if ( finished ) + outStream << "\n%%Pages: " << pageCount << "\n" + << wrapDSC( "%%DocumentFonts: " + fontsUsed ); + else + outStream << "%%Pages: (atend)" + << "\n%%DocumentFonts: (atend)"; + outStream << "\n%%EndComments\n"; + + outStream << "%%BeginProlog\n"; + const char * const prologLicense = "% Prolog copyright 1994-2006 Trolltech. " + "You may copy this prolog in any way\n" + "% that is directly related to this " + "document. For other use of this prolog,\n" + "% see your licensing agreement for Qt.\n"; + outStream << prologLicense << ps_header << "\n"; + + // we have to do this here, as scaling can affect this. + QString lineStyles = "/LArr[" // Pen styles: + " [] []" // solid line + " [ w s ] [ s w ]" // dash line + " [ s s ] [ s s ]" // dot line + " [ m s s s ] [ s m s s ]" // dash dot line + " [ m s s s s ] [ s m s s s s ]" // dash dot dot line + " ] d\n"; + lineStyles.replace( QRegExp( "w" ), toString( 10./scale ) ); + lineStyles.replace( QRegExp( "m" ), toString( 5./scale ) ); + lineStyles.replace( QRegExp( "s" ), toString( 3./scale ) ); + + outStream << lineStyles; + + outStream << "/pageinit {\n"; + if ( !printer->fullPage() ) { + if ( printer->orientation() == QPrinter::Portrait ) + outStream << mleft*scale << " " + << mbottom*scale << " translate\n"; + else + outStream << mtop*scale << " " + << mleft*scale << " translate\n"; + } + if ( printer->orientation() == QPrinter::Portrait ) { + outStream << "% " << m.widthMM() << "*" << m.heightMM() + << "mm (portrait)\n0 " << height*scale + << " translate " << scale << " -" << scale << " scale/defM matrix CM d } d\n"; + } else { + outStream << "% " << m.heightMM() << "*" << m.widthMM() + << " mm (landscape)\n 90 rotate " << scale << " -" << scale << " scale/defM matrix CM d } d\n"; + } + outStream << "%%EndProlog\n"; + + + outStream << "%%BeginSetup\n"; + if ( printer->numCopies() > 1 ) { + outStream << "/#copies " << printer->numCopies() << " def\n"; + outStream << "/NumCopies " << printer->numCopies() << " SPD\n"; + outStream << "/Collate " << (printer->collateCopies() ? "true" : "false") << " SPD\n"; + } + if ( fontBuffer->buffer().size() ) { + if ( pageCount == 1 || finished ) + outStream << "% Fonts and encodings used\n"; + else + outStream << "% Fonts and encodings used on pages 1-" + << pageCount << "\n"; + QDictIterator<QPSPrinterFontPrivate> it(fonts); + while (it.current()) { + it.current()->download(outStream,TRUE); // true means its global + ++it; + } + outStream.writeRawBytes( fontBuffer->buffer().data(), + fontBuffer->buffer().size() ); + } + outStream << "%%EndSetup\n"; + + outStream.writeRawBytes( buffer->buffer().data(), + buffer->buffer().size() ); + + delete buffer; + buffer = 0; + fontStream.unsetDevice(); + delete fontBuffer; + fontBuffer = 0; +} + + +/* Called whenever a restore has been done. Currently done at the top of a + new page and whenever clipping is turned off. */ +void QPSPrinterPrivate::resetDrawingTools( QPainter *paint ) +{ + QPen defaultPen; // default drawing tools + QBrush defaultBrush; + + QColor c = paint->backgroundColor(); + if ( c != Qt::white ) + pageStream << color( c, printer ) << "BC\n"; + + if ( paint->backgroundMode() != Qt::TransparentMode ) + pageStream << "/OMo true d\n"; + + //currentUsed = currentSet; + //setFont( currentSet ); + currentFontFile = 0; + + QBrush b = paint->brush(); + if ( b != defaultBrush ) { + if ( b == Qt::CustomPattern ) { +#if defined(CHECK_RANGE) + qWarning( "QPrinter: Pixmap brush not supported" ); +#endif + } else { + cbrush = b; + } + } + + dirtypen = TRUE; + dirtybrush = TRUE; + + if ( paint->hasViewXForm() || paint->hasWorldXForm() ) + matrixSetup( paint ); +} + + +static void putRect( QTextStream &stream, const QRect &r ) +{ + stream << r.x() << " " + << r.y() << " " + << r.width() << " " + << r.height() << " "; +} + + +void QPSPrinterPrivate::setClippingOff( QPainter *paint ) +{ + pageStream << "CLO\n"; // clipping off, includes a restore + resetDrawingTools( paint ); // so drawing tools must be reset +} + + +void QPSPrinterPrivate::clippingSetup( QPainter *paint ) +{ + if ( paint->hasClipping() ) { + if ( !firstClipOnPage ) + setClippingOff( paint ); + const QRegion rgn = paint->clipRegion(); + QMemArray<QRect> rects = rgn.rects(); + int i; + pageStream<< "CLSTART\n"; // start clipping + for( i = 0 ; i < (int)rects.size() ; i++ ) { + putRect( pageStream, rects[i] ); + pageStream << "ACR\n"; // add clip rect + if ( pageCount == 1 ) + boundingBox = boundingBox.unite( rects[i] ); + } + pageStream << "CLEND\n"; // end clipping + firstClipOnPage = FALSE; + } else { + if ( !firstClipOnPage ) // no need to turn off if first on page + setClippingOff( paint ); + // if we're painting without clipping, the bounding box must + // be everything. NOTE: this assumes that this function is + // only ever called when something is to be painted. + QPaintDeviceMetrics m( printer ); + if ( !boundingBox.isValid() ) + boundingBox.setRect( 0, 0, m.width(), m.height() ); + } + dirtyClipping = FALSE; +} + +void QPSPrinterPrivate::initPage(QPainter *paint) +{ + + // a restore undefines all the fonts that have been defined + // inside the scope (normally within pages) and all the glyphs that + // have been added in the scope. + + QDictIterator<QPSPrinterFontPrivate> it(fonts); + while (it.current()) { + it.current()->restore(); + ++it; + } + if ( !buffer ) { + pageFontNames.clear(); + } + + pageStream.unsetDevice(); + if ( pageBuffer ) + delete pageBuffer; + pageBuffer = new QBuffer(); + pageBuffer->open( IO_WriteOnly ); + pageStream.setEncoding( QTextStream::Latin1 ); + pageStream.setDevice( pageBuffer ); + delete savedImage; + savedImage = 0; + textY = 0; + dirtyClipping = TRUE; + firstClipOnPage = TRUE; + + + resetDrawingTools( paint ); + dirtyNewPage = FALSE; + pageFontNumber = headerFontNumber; +} + +void QPSPrinterPrivate::flushPage( bool last ) +{ + if ( last && !pageBuffer ) + return; + bool pageFonts = ( buffer == 0 ); + if ( buffer && +// ( last || pagesInBuffer++ > -1 || +// ( pagesInBuffer > 4 && buffer->size() > 262144 ) ) ) +#ifdef Q_WS_QWS + (last || buffer->size() > 2000000) // embedded is usually limited in memory +#else + (last || buffer->size() > 50000000) +#endif + ) { +// qDebug("emiting header at page %d", pageCount ); + emitHeader( last ); + } + outStream << "%%Page: " + << pageCount << ' ' << pageCount << endl + << "%%BeginPageSetup\n" + << "QI\n"; + if (!dirtyNewPage) { + if ( pageFonts ) { + //qDebug("page fonts for page %d", pageCount); + // we have already downloaded the header. Maybe we have page fonts here + QDictIterator<QPSPrinterFontPrivate> it(fonts); + while (it.current()) { + it.current()->download( outStream, FALSE ); // FALSE means its for the page only + ++it; + } + } + outStream << "%%EndPageSetup\n"; + if ( pageBuffer ) + outStream.writeRawBytes( pageBuffer->buffer().data(), + pageBuffer->buffer().size() ); + } + outStream << "\nQP\n"; + pageCount++; +} + +// ================ PSPrinter class ======================== + +QPSPrinter::QPSPrinter( QPrinter *prt, int fd ) + : QPaintDevice( QInternal::Printer | QInternal::ExternalDevice ) +{ + d = new QPSPrinterPrivate( prt, fd ); +} + + +QPSPrinter::~QPSPrinter() +{ + if ( d->fd >= 0 ) +#if defined(_OS_WIN32_) + ::_close( d->fd ); +#else + ::close( d->fd ); +#endif + delete d; +} + + + +static void ignoreSigPipe(bool b) +{ + static struct sigaction *users_sigpipe_handler = 0; + + if (b) { + if (users_sigpipe_handler != 0) + return; // already ignoring sigpipe + + users_sigpipe_handler = new struct sigaction; + struct sigaction tmp_sigpipe_handler; + tmp_sigpipe_handler.sa_handler = SIG_IGN; + sigemptyset(&tmp_sigpipe_handler.sa_mask); + tmp_sigpipe_handler.sa_flags = 0; + + if (sigaction(SIGPIPE, &tmp_sigpipe_handler, users_sigpipe_handler) == -1) { + delete users_sigpipe_handler; + users_sigpipe_handler = 0; + } + } + else { + if (users_sigpipe_handler == 0) + return; // not ignoring sigpipe + + if (sigaction(SIGPIPE, users_sigpipe_handler, 0) == -1) + qWarning("QPSPrinter: could not restore SIGPIPE handler"); + + delete users_sigpipe_handler; + users_sigpipe_handler = 0; + } +} + +bool QPSPrinter::cmd( int c , QPainter *paint, QPDevCmdParam *p ) +{ + if ( c == PdcBegin ) { // start painting + d->pagesInBuffer = 0; + d->buffer = new QBuffer(); + d->buffer->open( IO_WriteOnly ); + d->outStream.setEncoding( QTextStream::Latin1 ); + d->outStream.setDevice( d->buffer ); + d->fontBuffer = new QBuffer(); + d->fontBuffer->open( IO_WriteOnly ); + d->fontStream.setEncoding( QTextStream::Latin1 ); + d->fontStream.setDevice( d->fontBuffer ); + d->headerFontNumber = 0; + d->pageCount = 1; // initialize state + d->dirtyMatrix = TRUE; + d->dirtyClipping = TRUE; + d->dirtyNewPage = TRUE; + d->firstClipOnPage = TRUE; + d->boundingBox = QRect( 0, 0, -1, -1 ); + d->fontsUsed = QString::fromLatin1(""); + + QPaintDeviceMetrics m( d->printer ); + d->scale = 72. / ((float) m.logicalDpiY()); + + return TRUE; + } + + if ( c == PdcEnd ) { // painting done + bool pageCountAtEnd = (d->buffer != 0); + + // we're writing to lp/lpr through a pipe, we don't want to crash with SIGPIPE + // if lp/lpr dies + ignoreSigPipe(TRUE); + d->flushPage( TRUE ); + d->outStream << "%%Trailer\n"; + if ( pageCountAtEnd ) + d->outStream << "%%Pages: " << d->pageCount - 1 << "\n" << + wrapDSC( "%%DocumentFonts: " + d->fontsUsed ); + d->outStream << "%%EOF\n"; + ignoreSigPipe(FALSE); + + d->outStream.unsetDevice(); + if ( d->outDevice ) + d->outDevice->close(); + if ( d->fd >= 0 ) + ::close( d->fd ); + d->fd = -1; + delete d->outDevice; + d->outDevice = 0; + } + + if ( c >= PdcDrawFirst && c <= PdcDrawLast ) { + if ( !paint ) + return FALSE; // sanity + if ( d->dirtyNewPage ) + d->initPage( paint ); + if ( d->dirtyMatrix ) + d->matrixSetup( paint ); + if ( d->dirtyClipping ) // Must be after matrixSetup and initPage + d->clippingSetup( paint ); + if ( d->dirtypen ) { + // we special-case for narrow solid lines with the default + // cap and join styles + if ( d->cpen.style() == Qt::SolidLine && d->cpen.width() == 0 && + d->cpen.capStyle() == Qt::FlatCap && + d->cpen.joinStyle() == Qt::MiterJoin ) + d->pageStream << color( d->cpen.color(), d->printer ) << "P1\n"; + else + d->pageStream << (int)d->cpen.style() << ' ' << d->cpen.width() + << ' ' << color( d->cpen.color(), d->printer ) + << psCap( d->cpen.capStyle() ) + << psJoin( d->cpen.joinStyle() ) << "PE\n"; + d->dirtypen = FALSE; + } + if ( d->dirtybrush ) { + // we special-case for nobrush and solid white, since + // those are the two most common brushes + if ( d->cbrush.style() == Qt::NoBrush ) + d->pageStream << "NB\n"; + else if ( d->cbrush.style() == Qt::SolidPattern && + d->cbrush.color() == Qt::white ) + d->pageStream << "WB\n"; + else + d->pageStream << (int)d->cbrush.style() << ' ' + << color( d->cbrush.color(), d->printer ) << "BR\n"; + d->dirtybrush = FALSE; + } + if ( d->dirtyBkColor ) { + d->pageStream << color( d->bkColor, d->printer ) << "BC\n"; + d->dirtyBkColor = FALSE; + } + if ( d->dirtyBkMode ) { + if ( d->bkMode == Qt::TransparentMode ) + d->pageStream << "/OMo false d\n"; + else + d->pageStream << "/OMo true d\n"; + d->dirtyBkMode = FALSE; + } + } + + switch( c ) { + case PdcDrawPoint: + d->pageStream << POINT(0) << "P\n"; + break; + case PdcMoveTo: + d->pageStream << POINT(0) << "M\n"; + break; + case PdcLineTo: + d->pageStream << POINT(0) << "L\n"; + break; + case PdcDrawLine: + if ( p[0].point->y() == p[1].point->y() ) + d->pageStream << POINT(1) << p[0].point->x() << " HL\n"; + else if ( p[0].point->x() == p[1].point->x() ) + d->pageStream << POINT(1) << p[0].point->y() << " VL\n"; + else + d->pageStream << POINT(1) << POINT(0) << "DL\n"; + break; + case PdcDrawRect: + d->pageStream << RECT(0) << "R\n"; + break; + case PdcDrawRoundRect: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "RR\n"; + break; + case PdcDrawEllipse: + d->pageStream << RECT(0) << "E\n"; + break; + case PdcDrawArc: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "A\n"; + break; + case PdcDrawPie: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "PIE\n"; + break; + case PdcDrawChord: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "CH\n"; + break; + case PdcDrawLineSegments: + if ( p[0].ptarr->size() > 0 ) { + QPointArray a = *p[0].ptarr; + QPoint pt; + d->pageStream << "NP\n"; + for ( int i=0; i<(int)a.size(); i+=2 ) { + pt = a.point( i ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " MT\n"; + pt = a.point( i+1 ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " LT\n"; + } + d->pageStream << "QS\n"; + } + break; + case PdcDrawPolyline: + if ( p[0].ptarr->size() > 1 ) { + QPointArray a = *p[0].ptarr; + QPoint pt = a.point( 0 ); + d->pageStream << "NP\n" + << XCOORD(pt.x()) << ' ' << YCOORD(pt.y()) << " MT\n"; + for ( int i=1; i<(int)a.size(); i++ ) { + pt = a.point( i ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " LT\n"; + } + d->pageStream << "QS\n"; + } + break; + case PdcDrawPolygon: + if ( p[0].ptarr->size() > 2 ) { + QPointArray a = *p[0].ptarr; + if ( p[1].ival ) + d->pageStream << "/WFi true d\n"; + QPoint pt = a.point(0); + d->pageStream << "NP\n"; + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " MT\n"; + for( int i=1; i<(int)a.size(); i++) { + pt = a.point( i ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " LT\n"; + } + d->pageStream << "CP BF QS\n"; + if ( p[1].ival ) + d->pageStream << "/WFi false d\n"; + } + break; + case PdcDrawCubicBezier: + if ( p[0].ptarr->size() == 4 ) { + d->pageStream << "NP\n"; + QPointArray a = *p[0].ptarr; + d->pageStream << XCOORD(a[0].x()) << ' ' + << YCOORD(a[0].y()) << " MT "; + for ( int i=1; i<4; i++ ) { + d->pageStream << XCOORD(a[i].x()) << ' ' + << YCOORD(a[i].y()) << ' '; + } + d->pageStream << "BZ\n"; + } + break; + case PdcDrawText2: + // we use drawTextItem instead + return TRUE; + case PdcDrawText2Formatted: + return TRUE; + case PdcDrawTextItem: { + const QTextItem *ti = p[1].textItem; + QScriptItem &si = ti->engine->items[ti->item]; + int len = ti->engine->length( ti->item ); + if ( si.isSpace || si.isObject ) + return FALSE; + + if ( d->currentSet != d->currentUsed || d->scriptUsed != si.analysis.script || !d->currentFontFile ) { + d->currentUsed = d->currentSet; + d->setFont( d->currentSet, si.analysis.script ); + } + if( d->currentFontFile ) // better not crash in case somethig goes wrong. + d->currentFontFile->drawText( d->pageStream, *p[0].point, ti->engine, ti->item, + ti->engine->string.mid( si.position, len ), d, paint); + return FALSE; + } + case PdcDrawPixmap: { + if ( p[1].pixmap->isNull() ) + break; + QRect r = *p[0].rect; + QImage img; + img = *(p[1].pixmap); + QImage mask; + if ( p[1].pixmap->mask() ) + mask = *(p[1].pixmap->mask()); + d->drawImage(paint, r.x(), r.y(), r.width(), r.height(), img, mask); + break; + } + case PdcDrawImage: { + if ( p[1].image->isNull() ) + break; + QRect r = *(p[0].rect); + QImage img = *(p[1].image); + QImage mask; +#ifndef QT_NO_IMAGE_DITHER_TO_1 + if ( img.hasAlphaBuffer() ) + mask = img.createAlphaMask(); +#endif + d->drawImage(paint, r.x(), r.y(), r.width(), r.height(), img, mask); + break; + } + case PdcSetBkColor: + { + if ( d->bkColor != *(p[0].color) ) { + d->bkColor = *(p[0].color); + d->dirtyBkColor = TRUE; + } + break; + } + case PdcSetBkMode: + { + if ( d->bkMode != p[0].ival ) { + d->bkMode = (Qt::BGMode) p[0].ival; + d->dirtyBkMode = TRUE; + } + break; + } + case PdcSetROP: +#if defined(CHECK_RANGE) + if ( p[0].ival != Qt::CopyROP ) + qWarning( "QPrinter: Raster operation setting not supported" ); +#endif + break; + case PdcSetBrushOrigin: + break; + case PdcSetFont: + d->currentSet = *(p[0].font); + d->fm = paint->fontMetrics(); + // turn these off - they confuse the 'avoid font change' logic + d->currentSet.setUnderline( FALSE ); + d->currentSet.setStrikeOut( FALSE ); + break; + case PdcSetPen: + if ( d->cpen != *(p[0].pen) ) { + d->dirtypen = TRUE; + d->cpen = *(p[0].pen); + } + break; + case PdcSetBrush: + if ( p[0].brush->style() == Qt::CustomPattern ) { +#if defined(CHECK_RANGE) + qWarning( "QPrinter: Pixmap brush not supported" ); +#endif + return FALSE; + } + if ( d->cbrush != *(p[0].brush) ) { + d->dirtybrush = TRUE; + d->cbrush = *(p[0].brush); + } + break; + case PdcSetTabStops: + case PdcSetTabArray: + return FALSE; + case PdcSetUnit: + break; + case PdcSetVXform: + case PdcSetWindow: + case PdcSetViewport: + case PdcSetWXform: + case PdcSetWMatrix: + case PdcRestoreWMatrix: + d->dirtyMatrix = TRUE; + break; + case PdcSetClip: + d->dirtyClipping = TRUE; + break; + case PdcSetClipRegion: + d->dirtyClipping = TRUE; + break; + case NewPage: + // we're writing to lp/lpr through a pipe, we don't want to crash with SIGPIPE + // if lp/lpr dies + ignoreSigPipe(TRUE); + d->flushPage(); + ignoreSigPipe(FALSE); + + d->dirtyNewPage = TRUE; + break; + case AbortPrinting: + break; + default: + break; + } + return TRUE; +} + +#endif // QT_NO_PRINTER diff --git a/src/kernel/qpsprinter.ps b/src/kernel/qpsprinter.ps new file mode 100644 index 0000000..12a4c40 --- /dev/null +++ b/src/kernel/qpsprinter.ps @@ -0,0 +1,805 @@ +% the postscript header we use for our qpsprinter in uncompressed and commented form. +% use the makepsheader perl script to generate a compressed version of this header +% you can then paste into qpsprinter.cpp +% +% some compression of the code is done by the makepsheader script, so we don't need to +% write too criptically here. + +/d /def load def +/D {bind d} bind d +/d2 {dup dup} D +/B {0 d2} D +/W {255 d2} D +/ED {exch d} D +/D0 {0 ED} D +/LT {lineto} D +/MT {moveto} D +/S {stroke} D +/F {setfont} D +/SW {setlinewidth} D +/CP {closepath} D +/RL {rlineto} D +/NP {newpath} D +/CM {currentmatrix} D +/SM {setmatrix} D +/TR {translate} D +/SD {setdash} D +/SC {aload pop setrgbcolor} D +/CR {currentfile read pop} D +/i {index} D +/bs {bitshift} D +/scs {setcolorspace} D +/DB {dict dup begin} D +/DE {end d} D +/ie {ifelse} D +/sp {astore pop} D + +% ENDUNCOMPRESSED: Warning: leave this line in. +% Everything before this line will be left untouched by the compression + + + +/BSt 0 d % brush style +/LWi 1 d % line width +/PSt 1 d % pen style +/Cx 0 d % current x position +/Cy 0 d % current y position +/WFi false d % winding fill +/OMo false d % opaque mode (not transparent) + +/BCol [ 1 1 1 ] d % brush color +/PCol [ 0 0 0 ] d % pen color +/BkCol [ 1 1 1 ] d % background color +/BDArr [ % Brush dense patterns + 0.94 + 0.88 + 0.63 + 0.50 + 0.37 + 0.12 + 0.06 +] d +/defM matrix d + +/nS 0 d % number of saved painter states + +% LArr for the Pen styles is defined in emitHeader because of scaling + +% GPS: GetPenStyle +% Returns the line pattern (from pen style PSt). +% +% bool GPS pattern +% true : returns draw pattern +% false: returns fill pattern +/GPS { + PSt 1 ge PSt 5 le and % valid pen pattern? + { + { LArr PSt 1 sub 2 mul get } % draw pattern + { LArr PSt 2 mul 1 sub get } ifelse % opaque pattern + } + { [] } ifelse % out of range => solid line +} D + +% QS: QtStroke +% draw and fill current path +% +% - QS - +/QS { % stroke command + PSt 0 ne % != NO_PEN + { + gsave + LWi SW % set line width + true GPS 0 setdash S % draw line pattern + OMo PSt 1 ne and % opaque mode and not solid line? + { + BkCol SC + false GPS dup 0 get setdash S % fill in opaque pattern + } if + grestore + } if +} D + + + + +%% The following operations are used to read compressed data from the file +%% Until now this is only used for image compression + +% read 28 bits and leave them on tos +% +% - r28 num +/r28 { + % skip past whitespace and read one character + { currentfile read pop + dup 32 gt { exit } if + pop + } loop + % read three more + 3 { + currentfile read pop + } repeat + % make an accumulator + 0 + % for each character, shift the accumulator and add in the character + 4 { + 7 bitshift exch + dup 128 gt { 84 sub } if 42 sub 127 and + add + } repeat +} D + +/rA 0 d % accumulator +/rL 0 d % bits left + +% takes number of bits, leaves number +% +% num rB num +/rB { + rL 0 eq { + % if we have nothing, let's get something + /rA r28 d + /rL 28 d + } if + dup rL gt { + % if we don't have enough, take what we have and get more + rA exch rL sub rL exch + /rA 0 d /rL 0 d + rB exch bitshift add + } { + % else take some of what we have + dup rA 16#fffffff 3 -1 roll bitshift not and exch + % ... and update rL and rA + dup rL exch sub /rL ED + neg rA exch bitshift /rA ED + } ifelse +} D + +% uncompresses image data from currentfile until the string on the +% stack is full; leaves the string there. +% assumes that nothing could conceivably go wrong, ie. the compressed data has +% to be in the correct format and the length of the string has to be exactly right +% to hold the compressed data +% +% string uc string +% +%%% Warning: if you change the method here, change the table in qpsprinter.cpp:compress()! +/uc { + /rL 0 d + 0 + { % string pos + dup 2 index length ge { exit } if + 1 rB + 1 eq { % compressed + 3 rB % string pos bits + dup 3 ge { + 1 add dup rB % string pos bits extra + 1 index 5 ge { + 1 index 6 ge { + 1 index 7 ge { + 1 index 8 ge { + 128 add + } if + 64 add + } if + 32 add + } if + 16 add + } if + 3 add + exch pop + } if + 3 add + % string pos length + exch 10 rB 1 add + % string length pos dist + { + dup 3 index lt { + dup + } { + 2 index + } ifelse % string length pos dist length-this-time + 4 index 3 index 3 index sub 2 index getinterval + 5 index 4 index 3 -1 roll putinterval + dup 4 -1 roll add 3 1 roll + 4 -1 roll exch sub + dup 0 eq { exit } if + 3 1 roll + } loop % string pos dist length + pop pop + } { % uncompressed + 3 rB 1 add + { + 2 copy 8 rB put 1 add + } repeat + } ifelse + } loop + pop +} D + +%% image drawing routines + +/sl D0 % ### is this needed ? + +% defines for QCI +/QCIgray D0 /QCIcolor D0 /QCIindex D0 + +% this method prints color images if colorimage is available, otherwise +% converts the string to a grayscale image and uses the reular postscript image +% operator for printing. +% Arguments are the same as for the image operator: +% +% width height bits/sample matrix datasrc QCI - +/QCI { + /colorimage where { + pop + false 3 colorimage + }{ % the hard way, based on PD code by John Walker <kelvin@autodesk.com> + exec /QCIcolor ED + /QCIgray QCIcolor length 3 idiv string d + 0 1 QCIcolor length 3 idiv 1 sub + { /QCIindex ED + /x QCIindex 3 mul d + QCIgray QCIindex + QCIcolor x get 0.30 mul + QCIcolor x 1 add get 0.59 mul + QCIcolor x 2 add get 0.11 mul + add add cvi + put + } for + QCIgray image + } ifelse +} D + +% general image drawing routine, used from the postscript driver +% +% Draws images with and without mask with 1, 8 and 24(rgb) bits depth. +% +% width height matrix image 1|8|24 mask|false x y di +% +% width and height specify the width/height of the image, +% matrix a transformation matrix, image a procedure holding the image data +% (same for mask) and x/y an additional translation. +% +% ### should move the translation into the matrix!!! +/di +{ + gsave + translate + 1 index 1 eq { % bitmap + false eq { % no mask, draw solid background + pop + true 3 1 roll % width height false matrix image + 4 index + 4 index + false + 4 index + 4 index + imagemask + BkCol SC + imagemask + } { + pop + false 3 1 roll % width height false matrix image + imagemask + } ifelse + } { + dup false ne { + % have a mask, see if we can use it + /languagelevel where { + pop + languagelevel 3 ge + } { false } ifelse + } { + false + } ifelse + + { + % languagelevel3, we can use image mask and dicts + + % store the image mask + /ma exch d + % select colorspace according to 8|24 bit depth and set the decode array /dc + 8 eq { + /dc [0 1] d + /DeviceGray + } { + /dc [0 1 0 1 0 1] d + /DeviceRGB + } ifelse + setcolorspace + % the image data + /im exch d + % transformation matrix + /mt exch d + % width and height + /h exch def + /w exch def + % the image dict + /id + 7 dict dup begin + /ImageType 1 d + /Width w d + /Height h d + /ImageMatrix mt d + /DataSource im d + /BitsPerComponent 8 d + /Decode dc d + end d + % the mask dictionary + /md + 7 dict dup begin + /ImageType 1 d + /Width w d + /Height h d + /ImageMatrix mt d + /DataSource ma d + /BitsPerComponent 1 d + /Decode [0 1] d + end d + % and the combined image dict + 4 dict dup begin + /ImageType 3 d + /DataDict id d + /MaskDict md d + /InterleaveType 3 d + end + image + } { + pop % no mask or can't use it, get rid of it + 8 % width height image 8|24 8 matrix + 4 1 roll + 8 eq { % grayscale + image + } { %color + QCI + } ifelse + } ifelse + } ifelse + grestore +} d + + + + +/BF { % brush fill + gsave + BSt 1 eq % solid brush? + { + BCol SC + WFi { fill } { eofill } ifelse + } if + BSt 2 ge BSt 8 le and % dense pattern? + { + BDArr BSt 2 sub get /sc ED + % the following line scales the brush color according to the pattern. the higher the pattern the lighter the color. + BCol + { + 1. exch sub sc mul 1. exch sub + } forall + 3 array astore + SC + WFi { fill } { eofill } ifelse + } if + BSt 9 ge BSt 14 le and % brush pattern? + { + WFi { clip } { eoclip } ifelse + defM SM + pathbbox % left upper right lower + 3 index 3 index translate + 4 2 roll % right lower left upper + 3 2 roll % right left upper lower + exch % left right lower upper + sub /h ED + sub /w ED + OMo { + NP + 0 0 MT + 0 h RL + w 0 RL + 0 h neg RL + CP + BkCol SC + fill + } if + BCol SC + 0.3 SW + NP + BSt 9 eq BSt 11 eq or % horiz or cross pattern + { 0 4 h + { dup 0 exch MT w exch LT } for + } if + BSt 10 eq BSt 11 eq or % vert or cross pattern + { 0 4 w + { dup 0 MT h LT } for + } if + BSt 12 eq BSt 14 eq or % F-diag or diag cross + { w h gt + { 0 6 w h add + { dup 0 MT h sub h LT } for + } { 0 6 w h add + { dup 0 exch MT w sub w exch LT } for + } ifelse + } if + BSt 13 eq BSt 14 eq or % B-diag or diag cross + { w h gt + { 0 6 w h add + { dup h MT h sub 0 LT } for + } { 0 6 w h add + { dup w exch MT w sub 0 exch LT } for + } ifelse + } if + S + } if + BSt 24 eq % CustomPattern + + { + } if + grestore +} D + +% for arc +/mat matrix d +/ang1 D0 /ang2 D0 +/w D0 /h D0 +/x D0 /y D0 + +/ARC { % Generic ARC function [ X Y W H ang1 ang2 ] + /ang2 ED /ang1 ED /h ED /w ED /y ED /x ED + mat CM pop + x w 2 div add y h 2 div add TR + 1 h w div neg scale + ang2 0 ge + {0 0 w 2 div ang1 ang1 ang2 add arc } + {0 0 w 2 div ang1 ang1 ang2 add arcn} ifelse + mat SM +} D + +/C D0 + +/P { % PdcDrawPoint [x y] + NP + MT + 0.5 0.5 rmoveto + 0 -1 RL + -1 0 RL + 0 1 RL + CP + fill +} D + +/M { % PdcMoveTo [x y] + /Cy ED /Cx ED +} D + +/L { % PdcLineTo [x y] + NP + Cx Cy MT + /Cy ED /Cx ED + Cx Cy LT + QS +} D + +/DL { % PdcDrawLine [x1 y1 x0 y0] + NP + MT + LT + QS +} D + +/HL { % PdcDrawLine [x1 y x0] + 1 index DL +} D + +/VL { % PdcDrawLine [x y1 y0] + 2 index exch DL +} D + +/R { % PdcDrawRect [x y w h] + /h ED /w ED /y ED /x ED + NP + x y MT + 0 h RL + w 0 RL + 0 h neg RL + CP + BF + QS +} D + +/ACR { % add clip rect + /h ED /w ED /y ED /x ED + x y MT + 0 h RL + w 0 RL + 0 h neg RL + CP +} D + +/xr D0 /yr D0 +/rx D0 /ry D0 /rx2 D0 /ry2 D0 + +/RR { % PdcDrawRoundRect [x y w h xr yr] + /yr ED /xr ED /h ED /w ED /y ED /x ED + xr 0 le yr 0 le or + {x y w h R} % Do rect if one of rounding values is less than 0. + {xr 100 ge yr 100 ge or + {x y w h E} % Do ellipse if both rounding values are larger than 100 + { + /rx xr w mul 200 div d + /ry yr h mul 200 div d + /rx2 rx 2 mul d + /ry2 ry 2 mul d + NP + x rx add y MT + x y rx2 ry2 180 -90 + x y h add ry2 sub rx2 ry2 270 -90 + x w add rx2 sub y h add ry2 sub rx2 ry2 0 -90 + x w add rx2 sub y rx2 ry2 90 -90 + ARC ARC ARC ARC + CP + BF + QS + } ifelse + } ifelse +} D + +/E { % PdcDrawEllipse [x y w h] + /h ED /w ED /y ED /x ED + mat CM pop + x w 2 div add y h 2 div add translate + 1 h w div scale + NP + 0 0 w 2 div 0 360 arc + mat SM + BF + QS +} D + +/A { % PdcDrawArc [x y w h ang1 ang2] + 16 div exch 16 div exch + NP + ARC + QS +} D + +/PIE { % PdcDrawPie [x y w h ang1 ang2] + /ang2 ED /ang1 ED /h ED /w ED /y ED /x ED + NP + x w 2 div add y h 2 div add MT + x y w h ang1 16 div ang2 16 div ARC + CP + BF + QS +} D + +/CH { % PdcDrawChord [x y w h ang1 ang2] + 16 div exch 16 div exch + NP + ARC + CP + BF + QS +} D + +/BZ { % PdcDrawCubicBezier [4 points] + curveto + QS +} D + +/CRGB { % Compute RGB [R G B] => R/255 G/255 B/255 + 255 div 3 1 roll + 255 div 3 1 roll + 255 div 3 1 roll +} D + + +/BC { % PdcSetBkColor [R G B] + CRGB + BkCol astore pop +} D + +/BR { % PdcSetBrush [style R G B] + CRGB + BCol astore pop + /BSt ED +} D + +/WB { % set white solid brush + 1 W BR +} D + +/NB { % set nobrush + 0 B BR +} D + +/PE { % PdcSetPen [style width R G B Cap Join] + setlinejoin setlinecap + CRGB + PCol astore pop + /LWi ED + /PSt ED + LWi 0 eq { 0.25 /LWi ED } if % ### 3.0 remove this line + PCol SC +} D + +/P1 { % PdcSetPen [R G B] + 1 0 5 2 roll 0 0 PE +} D + +/ST { % SET TRANSFORM [matrix] + defM setmatrix + concat +} D + +%% Font handling + +% the next three commands are for defining fonts. The first one +% tries to find the most suitable printer font out of a fontlist. +% if encoding is false the default one will be used. +/MF { % newname encoding fontlist + % this function tries to find a suitable postscript font. + % We try quite hard not to get courier for a + % proportional font. The following takes an array of fonts. + % The algorithm will take the first font that + % gives a match (defined as not resulting in a courier font). + % each entry in the table is an array of the form [ /Fontname x-stretch slant ] + % x-strtch can be used to stretch/squeeze the font in x direction. + % This gives better results when eg substituting helvetica for arial + % slant is an optional slant. 0 is non slanted, 0.2 is a typical value for a syntetic oblique. + % encoding can be either an encoding vector of false if the default font encoding is requested. + true exch true exch % push a dummy on the stack, + { % so the loop over the array will leave a font in any case when exiting. + exch pop exch pop % (dummy | oldfont) (dummy | fontdict) fontarray + dup 0 get dup findfont % get the fontname from the array and load it + dup /FontName get % see if the font exists + 3 -1 roll eq { % see if fontname and the one provided are equal + exit + } if + } forall + exch % font fontarray + + % newname encoding font fontarray defines a postscript font + dup + 1 get /fxscale exch def % define scale, sland and encoding + 2 get /fslant exch def + exch /fencoding exch def + [ fxscale 0 fslant 1 0 0 ] makefont % transform font accordingly + fencoding false eq { % check if we have an encoding and use it if available + } { + dup maxlength dict begin % copy font + { + 1 index /FID ne % don't copy FID, as it's not allowed in PS Level 1 + {def}{pop pop}ifelse + } forall + /Encoding fencoding def % replace encoding + currentdict + end + } ifelse + definefont pop +} D + +% an embedded font. This is used for the base fonts of the composite font used later on. +/MFEmb { % newname encoding fontname + findfont dup length dict + begin + { + 1 index /FID ne + {d}{pop pop}ifelse + } forall + /Encoding ED currentdict + end + definefont pop +} D + +% DF: define font +% used to get a scaled version of an already loaded font +% +% newname pointsize fontmame DF - +/DF { + findfont + % get the fontsize on top of the stack and define font matrix + /fs 3 -1 roll d [ fs 0 0 fs -1 mul 0 0 ] + makefont + d +} D + +/ty 0 d +/Y { + /ty ED +} D + +/Tl { % draw underline/strikeout line: () w x y lw ->Tl-> () w x + gsave + setlinewidth + NP 1 index exch MT + 1 index 0 rlineto stroke + grestore +} D + +/XYT { % [string [x/y displacement array] width x] + ty MT % pops x + + /xyshow where { % interpreter has xyshow + pop pop + xyshow + } { % use ashow + exch pop % string cwidth + 1 index % string cwidth string + dup length 2 div exch % string cwidth length string !have to divide by 2 since we use unicode! + stringwidth pop % string cwidth length pwidth + 3 -1 roll % string length pwidth cwidth + exch sub exch div % string extraperchar + exch 0 exch % extraperchar 0 string + ashow + } ifelse +} D + +/AT { + ty MT % pops x + 1 index % string cwidth string + dup length 2 div exch % string cwidth length string !have to divide by 2 since we use unicode! + stringwidth pop % string cwidth length pwidth + 3 -1 roll % string length pwidth cwidth + exch sub exch div % string extraperchar + exch 0 exch % extraperchar 0 string + ashow +} D + +%% start of page +/QI { + /C save d + pageinit + /Cx 0 d % reset current x position + /Cy 0 d % reset current y position + /OMo false d +} D + +%% end of page +/QP { % show page + C restore + showpage +} D + +% merges one key value pair into the page device dict +% +% key value SPD - +/SPD { + /setpagedevice where { + 1 dict dup begin 3 1 roll def end + setpagedevice + } { pop pop } ifelse +} D + +/SV { % Save painter state + BSt LWi PSt Cx Cy WFi OMo BCol PCol BkCol + /nS nS 1 add d + gsave +} D + +/RS { % Restore painter state + nS 0 gt + { grestore + /BkCol ED /PCol ED /BCol ED /OMo ED /WFi ED + /Cy ED /Cx ED /PSt ED /LWi ED /BSt ED + /nS nS 1 sub d + } if +} D + +/CLSTART { % clipping start + /clipTmp matrix CM d % save current matrix + defM SM % Page default matrix + NP +} D + +/CLEND { % clipping end + clip + NP + clipTmp SM % restore the current matrix +} D + +/CLO { % clipping off + grestore % restore top of page state + gsave % save it back again + defM SM % set coordsys (defensive progr.) +} D + diff --git a/src/kernel/qpsprinter_p.h b/src/kernel/qpsprinter_p.h new file mode 100644 index 0000000..41710e7 --- /dev/null +++ b/src/kernel/qpsprinter_p.h @@ -0,0 +1,92 @@ +/********************************************************************** +** +** Definition of internal QPSPrinter class. +** QPSPrinter implements PostScript (tm) output via QPrinter. +** +** Created : 940927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QPSPRINTER_P_H +#define QPSPRINTER_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qpsprinter.cpp and qprinter_x11.cpp. +// This header file may change from version to version without notice, +// or even be removed. +// +// We mean it. +// +// + + +#ifndef QT_H +#include "qprinter.h" +#include "qtextstream.h" +#endif // QT_H + +#ifndef QT_NO_PRINTER + +class QPSPrinterPrivate; + +class Q_EXPORT QPSPrinter : public QPaintDevice +{ +private: + // QPrinter uses these + QPSPrinter( QPrinter *, int ); + ~QPSPrinter(); + + bool cmd ( int, QPainter *, QPDevCmdParam * ); + + enum { NewPage = 100, AbortPrinting }; + + friend class QPrinter; +private: + // not used by QPrinter + QPSPrinterPrivate *d; + + // Disabled copy constructor and operator= + QPSPrinter( const QPSPrinter & ); + QPSPrinter &operator=( const QPSPrinter & ); +}; + +#endif // QT_NO_PRINTER + +#endif // QPSPRINTER_P_H diff --git a/src/kernel/qrect.cpp b/src/kernel/qrect.cpp new file mode 100644 index 0000000..87aa78f --- /dev/null +++ b/src/kernel/qrect.cpp @@ -0,0 +1,960 @@ +/**************************************************************************** +** +** Implementation of QRect class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#define QRECT_C +#include "qrect.h" +#include "qdatastream.h" + +/*! + \class QRect + \brief The QRect class defines a rectangle in the plane. + + \ingroup images + \ingroup graphics + \mainclass + + A rectangle is internally represented as an upper-left corner and + a bottom-right corner, but it is normally expressed as an + upper-left corner and a size. + + The coordinate type is QCOORD (defined in \c qwindowdefs.h as \c + int). The minimum value of QCOORD is QCOORD_MIN (-2147483648) and + the maximum value is QCOORD_MAX (2147483647). + + Note that the size (width and height) of a rectangle might be + different from what you are used to. If the top-left corner and + the bottom-right corner are the same, the height and the width of + the rectangle will both be 1. + + Generally, \e{width = right - left + 1} and \e{height = bottom - + top + 1}. We designed it this way to make it correspond to + rectangular spaces used by drawing functions in which the width + and height denote a number of pixels. For example, drawing a + rectangle with width and height 1 draws a single pixel. + + The default coordinate system has origin (0, 0) in the top-left + corner. The positive direction of the y axis is down, and the + positive x axis is from left to right. + + A QRect can be constructed with a set of left, top, width and + height integers, from two QPoints or from a QPoint and a QSize. + After creation the dimensions can be changed, e.g. with setLeft(), + setRight(), setTop() and setBottom(), or by setting sizes, e.g. + setWidth(), setHeight() and setSize(). The dimensions can also be + changed with the move functions, e.g. moveBy(), moveCenter(), + moveBottomRight(), etc. You can also add coordinates to a + rectangle with addCoords(). + + You can test to see if a QRect contains a specific point with + contains(). You can also test to see if two QRects intersect with + intersects() (see also intersect()). To get the bounding rectangle + of two QRects use unite(). + + \sa QPoint, QSize +*/ + + +/***************************************************************************** + QRect member functions + *****************************************************************************/ + +/*! + \fn QRect::QRect() + + Constructs an invalid rectangle. +*/ + +/*! + Constructs a rectangle with \a topLeft as the top-left corner and + \a bottomRight as the bottom-right corner. +*/ + +QRect::QRect( const QPoint &topLeft, const QPoint &bottomRight ) +{ + x1 = (QCOORD)topLeft.x(); + y1 = (QCOORD)topLeft.y(); + x2 = (QCOORD)bottomRight.x(); + y2 = (QCOORD)bottomRight.y(); +} + +/*! + Constructs a rectangle with \a topLeft as the top-left corner and + \a size as the rectangle size. +*/ + +QRect::QRect( const QPoint &topLeft, const QSize &size ) +{ + x1 = (QCOORD)topLeft.x(); + y1 = (QCOORD)topLeft.y(); + x2 = (QCOORD)(x1+size.width()-1); + y2 = (QCOORD)(y1+size.height()-1); +} + +/*! + \fn QRect::QRect( int left, int top, int width, int height ) + + Constructs a rectangle with the \a top, \a left corner and \a + width and \a height. + + Example (creates three identical rectangles): + \code + QRect r1( QPoint(100,200), QPoint(110,215) ); + QRect r2( QPoint(100,200), QSize(11,16) ); + QRect r3( 100, 200, 11, 16 ); + \endcode +*/ + + +/*! + \fn bool QRect::isNull() const + + Returns TRUE if the rectangle is a null rectangle; otherwise + returns FALSE. + + A null rectangle has both the width and the height set to 0, that + is right() == left() - 1 and bottom() == top() - 1. + + Note that if right() == left() and bottom() == top(), then the + rectangle has width 1 and height 1. + + A null rectangle is also empty. + + A null rectangle is not valid. + + \sa isEmpty(), isValid() +*/ + +/*! + \fn bool QRect::isEmpty() const + + Returns TRUE if the rectangle is empty; otherwise returns FALSE. + + An empty rectangle has a left() \> right() or top() \> bottom(). + + An empty rectangle is not valid. \c{isEmpty() == !isValid()} + + \sa isNull(), isValid(), normalize() +*/ + +/*! + \fn bool QRect::isValid() const + + Returns TRUE if the rectangle is valid; otherwise returns FALSE. + + A valid rectangle has a left() \<= right() and top() \<= bottom(). + + Note that non-trivial operations like intersections are not defined + for invalid rectangles. + + \c{isValid() == !isEmpty()} + + \sa isNull(), isEmpty(), normalize() +*/ + + +/*! + Returns a normalized rectangle, i.e. a rectangle that has a + non-negative width and height. + + It swaps left and right if left() \> right(), and swaps top and + bottom if top() \> bottom(). + + \sa isValid() +*/ + +QRect QRect::normalize() const +{ + QRect r; + if ( x2 < x1 ) { // swap bad x values + r.x1 = x2; + r.x2 = x1; + } else { + r.x1 = x1; + r.x2 = x2; + } + if ( y2 < y1 ) { // swap bad y values + r.y1 = y2; + r.y2 = y1; + } else { + r.y1 = y1; + r.y2 = y2; + } + return r; +} + + +/*! + \fn int QRect::left() const + + Returns the left coordinate of the rectangle. Identical to x(). + + \sa setLeft(), right(), topLeft(), bottomLeft() +*/ + +/*! + \fn int QRect::top() const + + Returns the top coordinate of the rectangle. Identical to y(). + + \sa setTop(), bottom(), topLeft(), topRight() +*/ + +/*! + \fn int QRect::right() const + + Returns the right coordinate of the rectangle. + + \sa setRight(), left(), topRight(), bottomRight() +*/ + +/*! + \fn int QRect::bottom() const + + Returns the bottom coordinate of the rectangle. + + \sa setBottom(), top(), bottomLeft(), bottomRight() +*/ + +/*! + \fn QCOORD &QRect::rLeft() + + Returns a reference to the left coordinate of the rectangle. + + \sa rTop(), rRight(), rBottom() +*/ + +/*! + \fn QCOORD &QRect::rTop() + + Returns a reference to the top coordinate of the rectangle. + + \sa rLeft(), rRight(), rBottom() +*/ + +/*! + \fn QCOORD &QRect::rRight() + + Returns a reference to the right coordinate of the rectangle. + + \sa rLeft(), rTop(), rBottom() +*/ + +/*! + \fn QCOORD &QRect::rBottom() + + Returns a reference to the bottom coordinate of the rectangle. + + \sa rLeft(), rTop(), rRight() +*/ + +/*! + \fn int QRect::x() const + + Returns the left coordinate of the rectangle. Identical to left(). + + \sa left(), y(), setX() +*/ + +/*! + \fn int QRect::y() const + + Returns the top coordinate of the rectangle. Identical to top(). + + \sa top(), x(), setY() +*/ + +/*! + \fn void QRect::setLeft( int pos ) + + Sets the left edge of the rectangle to \a pos. May change the + width, but will never change the right edge of the rectangle. + + Identical to setX(). + + \sa left(), setTop(), setWidth() +*/ + +/*! + \fn void QRect::setTop( int pos ) + + Sets the top edge of the rectangle to \a pos. May change the + height, but will never change the bottom edge of the rectangle. + + Identical to setY(). + + \sa top(), setBottom(), setHeight() +*/ + +/*! + \fn void QRect::setRight( int pos ) + + Sets the right edge of the rectangle to \a pos. May change the + width, but will never change the left edge of the rectangle. + + \sa right(), setLeft(), setWidth() +*/ + +/*! + \fn void QRect::setBottom( int pos ) + + Sets the bottom edge of the rectangle to \a pos. May change the + height, but will never change the top edge of the rectangle. + + \sa bottom(), setTop(), setHeight() +*/ + +/*! + \fn void QRect::setX( int x ) + + Sets the x position of the rectangle (its left end) to \a x. May + change the width, but will never change the right edge of the + rectangle. + + Identical to setLeft(). + + \sa x(), setY() +*/ + +/*! + \fn void QRect::setY( int y ) + + Sets the y position of the rectangle (its top) to \a y. May change + the height, but will never change the bottom edge of the + rectangle. + + Identical to setTop(). + + \sa y(), setX() +*/ + +/*! + Set the top-left corner of the rectangle to \a p. May change + the size, but will the never change the bottom-right corner of + the rectangle. + + \sa topLeft(), moveTopLeft(), setBottomRight(), setTopRight(), setBottomLeft() +*/ +void QRect::setTopLeft( const QPoint &p ) +{ + setLeft( p.x() ); + setTop( p.y() ); +} + +/*! + Set the bottom-right corner of the rectangle to \a p. May change + the size, but will the never change the top-left corner of + the rectangle. + + \sa bottomRight(), moveBottomRight(), setTopLeft(), setTopRight(), setBottomLeft() +*/ +void QRect::setBottomRight( const QPoint &p ) +{ + setRight( p.x() ); + setBottom( p.y() ); +} + +/*! + Set the top-right corner of the rectangle to \a p. May change + the size, but will the never change the bottom-left corner of + the rectangle. + + \sa topRight(), moveTopRight(), setTopLeft(), setBottomRight(), setBottomLeft() +*/ +void QRect::setTopRight( const QPoint &p ) +{ + setRight( p.x() ); + setTop( p.y() ); +} + +/*! + Set the bottom-left corner of the rectangle to \a p. May change + the size, but will the never change the top-right corner of + the rectangle. + + \sa bottomLeft(), moveBottomLeft(), setTopLeft(), setBottomRight(), setTopRight() +*/ +void QRect::setBottomLeft( const QPoint &p ) +{ + setLeft( p.x() ); + setBottom( p.y() ); +} + +/*! + \fn QPoint QRect::topLeft() const + + Returns the top-left position of the rectangle. + + \sa setTopLeft(), moveTopLeft(), bottomRight(), left(), top() +*/ + +/*! + \fn QPoint QRect::bottomRight() const + + Returns the bottom-right position of the rectangle. + + \sa setBottomRight(), moveBottomRight(), topLeft(), right(), bottom() +*/ + +/*! + \fn QPoint QRect::topRight() const + + Returns the top-right position of the rectangle. + + \sa setTopRight(), moveTopRight(), bottomLeft(), top(), right() +*/ + +/*! + \fn QPoint QRect::bottomLeft() const + + Returns the bottom-left position of the rectangle. + + \sa setBottomLeft(), moveBottomLeft(), topRight(), bottom(), left() +*/ + +/*! + \fn QPoint QRect::center() const + + Returns the center point of the rectangle. + + \sa moveCenter(), topLeft(), bottomRight(), topRight(), bottomLeft() +*/ + + +/*! + Extracts the rectangle parameters as the position \a *x, \a *y and + width \a *w and height \a *h. + + \sa setRect(), coords() +*/ + +void QRect::rect( int *x, int *y, int *w, int *h ) const +{ + *x = x1; + *y = y1; + *w = x2-x1+1; + *h = y2-y1+1; +} + +/*! + Extracts the rectangle parameters as the top-left point \a *xp1, + \a *yp1 and the bottom-right point \a *xp2, \a *yp2. + + \sa setCoords(), rect() +*/ + +void QRect::coords( int *xp1, int *yp1, int *xp2, int *yp2 ) const +{ + *xp1 = x1; + *yp1 = y1; + *xp2 = x2; + *yp2 = y2; +} + + +/*! + Sets the left position of the rectangle to \a pos, leaving the + size unchanged. + + \sa left(), setLeft(), moveTop(), moveRight(), moveBottom() +*/ +void QRect::moveLeft( int pos ) +{ + x2 += (QCOORD)(pos - x1); + x1 = (QCOORD)pos; +} + +/*! + Sets the top position of the rectangle to \a pos, leaving the + size unchanged. + + \sa top(), setTop(), moveLeft(), moveRight(), moveBottom() +*/ + +void QRect::moveTop( int pos ) +{ + y2 += (QCOORD)(pos - y1); + y1 = (QCOORD)pos; +} + +/*! + Sets the right position of the rectangle to \a pos, leaving the + size unchanged. + + \sa right(), setRight(), moveLeft(), moveTop(), moveBottom() +*/ + +void QRect::moveRight( int pos ) +{ + x1 += (QCOORD)(pos - x2); + x2 = (QCOORD)pos; +} + +/*! + Sets the bottom position of the rectangle to \a pos, leaving the + size unchanged. + + \sa bottom(), setBottom(), moveLeft(), moveTop(), moveRight() +*/ + +void QRect::moveBottom( int pos ) +{ + y1 += (QCOORD)(pos - y2); + y2 = (QCOORD)pos; +} + +/*! + Sets the top-left position of the rectangle to \a p, leaving the + size unchanged. + + \sa topLeft(), setTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft() +*/ + +void QRect::moveTopLeft( const QPoint &p ) +{ + moveLeft( p.x() ); + moveTop( p.y() ); +} + +/*! + Sets the bottom-right position of the rectangle to \a p, leaving + the size unchanged. + + \sa bottomRight(), setBottomRight(), moveTopLeft(), moveTopRight(), moveBottomLeft() +*/ + +void QRect::moveBottomRight( const QPoint &p ) +{ + moveRight( p.x() ); + moveBottom( p.y() ); +} + +/*! + Sets the top-right position of the rectangle to \a p, leaving the + size unchanged. + + \sa topRight(), setTopRight(), moveTopLeft(), moveBottomRight(), moveBottomLeft() +*/ + +void QRect::moveTopRight( const QPoint &p ) +{ + moveRight( p.x() ); + moveTop( p.y() ); +} + +/*! + Sets the bottom-left position of the rectangle to \a p, leaving + the size unchanged. + + \sa bottomLeft(), setBottomLeft(), moveTopLeft(), moveBottomRight(), moveTopRight() +*/ + +void QRect::moveBottomLeft( const QPoint &p ) +{ + moveLeft( p.x() ); + moveBottom( p.y() ); +} + + +/*! + Sets the center point of the rectangle to \a p, leaving the size + unchanged. + + \sa center(), moveTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft() +*/ + +void QRect::moveCenter( const QPoint &p ) +{ + QCOORD w = x2 - x1; + QCOORD h = y2 - y1; + x1 = (QCOORD)(p.x() - w/2); + y1 = (QCOORD)(p.y() - h/2); + x2 = x1 + w; + y2 = y1 + h; +} + + +/*! + Moves the rectangle \a dx along the x axis and \a dy along the y + axis, relative to the current position. Positive values move the + rectangle to the right and down. + + \sa moveTopLeft() +*/ + +void QRect::moveBy( int dx, int dy ) +{ + x1 += (QCOORD)dx; + y1 += (QCOORD)dy; + x2 += (QCOORD)dx; + y2 += (QCOORD)dy; +} + +/*! + Sets the coordinates of the rectangle's top-left corner to \a (x, + y), and its size to \a (w, h). + + \sa rect(), setCoords() +*/ + +void QRect::setRect( int x, int y, int w, int h ) +{ + x1 = (QCOORD)x; + y1 = (QCOORD)y; + x2 = (QCOORD)(x+w-1); + y2 = (QCOORD)(y+h-1); +} + +/*! + Sets the coordinates of the rectangle's top-left corner to \a + (xp1, yp1), and the coordinates of its bottom-right corner to \a + (xp2, yp2). + + \sa coords(), setRect() +*/ + +void QRect::setCoords( int xp1, int yp1, int xp2, int yp2 ) +{ + x1 = (QCOORD)xp1; + y1 = (QCOORD)yp1; + x2 = (QCOORD)xp2; + y2 = (QCOORD)yp2; +} + +/*! + Adds \a xp1, \a yp1, \a xp2 and \a yp2 respectively to the + existing coordinates of the rectangle. +*/ + +void QRect::addCoords( int xp1, int yp1, int xp2, int yp2 ) +{ + x1 += (QCOORD)xp1; + y1 += (QCOORD)yp1; + x2 += (QCOORD)xp2; + y2 += (QCOORD)yp2; +} + +/*! + \fn QSize QRect::size() const + + Returns the size of the rectangle. + + \sa width(), height() +*/ + +/*! + \fn int QRect::width() const + + Returns the width of the rectangle. The width includes both the + left and right edges, i.e. width = right - left + 1. + + \sa height(), size(), setHeight() +*/ + +/*! + \fn int QRect::height() const + + Returns the height of the rectangle. The height includes both the + top and bottom edges, i.e. height = bottom - top + 1. + + \sa width(), size(), setHeight() +*/ + +/*! + Sets the width of the rectangle to \a w. The right edge is + changed, but not the left edge. + + \sa width(), setLeft(), setRight(), setSize() +*/ + +void QRect::setWidth( int w ) +{ + x2 = (QCOORD)(x1 + w - 1); +} + +/*! + Sets the height of the rectangle to \a h. The top edge is not + moved, but the bottom edge may be moved. + + \sa height(), setTop(), setBottom(), setSize() +*/ + +void QRect::setHeight( int h ) +{ + y2 = (QCOORD)(y1 + h - 1); +} + +/*! + Sets the size of the rectangle to \a s. The top-left corner is not + moved. + + \sa size(), setWidth(), setHeight() +*/ + +void QRect::setSize( const QSize &s ) +{ + x2 = (QCOORD)(s.width() +x1-1); + y2 = (QCOORD)(s.height()+y1-1); +} + +/*! + Returns TRUE if the point \a p is inside or on the edge of the + rectangle; otherwise returns FALSE. + + If \a proper is TRUE, this function returns TRUE only if \a p is + inside (not on the edge). +*/ + +bool QRect::contains( const QPoint &p, bool proper ) const +{ + if ( proper ) + return p.x() > x1 && p.x() < x2 && + p.y() > y1 && p.y() < y2; + else + return p.x() >= x1 && p.x() <= x2 && + p.y() >= y1 && p.y() <= y2; +} + +/*! + \overload bool QRect::contains( int x, int y, bool proper ) const + + Returns TRUE if the point \a x, \a y is inside this rectangle; + otherwise returns FALSE. + + If \a proper is TRUE, this function returns TRUE only if the point + is entirely inside (not on the edge). +*/ + +/*! + \overload bool QRect::contains( int x, int y ) const + + Returns TRUE if the point \a x, \a y is inside this rectangle; + otherwise returns FALSE. +*/ + +/*! + \overload + + Returns TRUE if the rectangle \a r is inside this rectangle; + otherwise returns FALSE. + + If \a proper is TRUE, this function returns TRUE only if \a r is + entirely inside (not on the edge). + + \sa unite(), intersect(), intersects() +*/ + +bool QRect::contains( const QRect &r, bool proper ) const +{ + if ( proper ) + return r.x1 > x1 && r.x2 < x2 && r.y1 > y1 && r.y2 < y2; + else + return r.x1 >= x1 && r.x2 <= x2 && r.y1 >= y1 && r.y2 <= y2; +} + +/*! + Unites this rectangle with rectangle \a r. +*/ +QRect& QRect::operator|=(const QRect &r) +{ + *this = *this | r; + return *this; +} + +/*! + Intersects this rectangle with rectangle \a r. +*/ +QRect& QRect::operator&=(const QRect &r) +{ + *this = *this & r; + return *this; +} + + +/*! + Returns the bounding rectangle of this rectangle and rectangle \a + r. + + The bounding rectangle of a nonempty rectangle and an empty or + invalid rectangle is defined to be the nonempty rectangle. + + \sa operator|=(), operator&(), intersects(), contains() +*/ + +QRect QRect::operator|(const QRect &r) const +{ + if ( isValid() ) { + if ( r.isValid() ) { + QRect tmp; + tmp.setLeft( QMIN( x1, r.x1 ) ); + tmp.setRight( QMAX( x2, r.x2 ) ); + tmp.setTop( QMIN( y1, r.y1 ) ); + tmp.setBottom( QMAX( y2, r.y2 ) ); + return tmp; + } else { + return *this; + } + } else { + return r; + } +} + +/*! + Returns the bounding rectangle of this rectangle and rectangle \a + r. \c{r.unite(s)} is equivalent to \c{r|s}. +*/ +QRect QRect::unite( const QRect &r ) const +{ + return *this | r; +} + + +/*! + Returns the intersection of this rectangle and rectangle \a r. + + Returns an empty rectangle if there is no intersection. + + \sa operator&=(), operator|(), isEmpty(), intersects(), contains() +*/ + +QRect QRect::operator&( const QRect &r ) const +{ + QRect tmp; + tmp.x1 = QMAX( x1, r.x1 ); + tmp.x2 = QMIN( x2, r.x2 ); + tmp.y1 = QMAX( y1, r.y1 ); + tmp.y2 = QMIN( y2, r.y2 ); + return tmp; +} + +/*! + Returns the intersection of this rectangle and rectangle \a r. + \c{r.intersect(s)} is equivalent to \c{r&s}. +*/ +QRect QRect::intersect( const QRect &r ) const +{ + return *this & r; +} + +/*! + Returns TRUE if this rectangle intersects with rectangle \a r + (there is at least one pixel that is within both rectangles); + otherwise returns FALSE. + + \sa intersect(), contains() +*/ + +bool QRect::intersects( const QRect &r ) const +{ + return ( QMAX( x1, r.x1 ) <= QMIN( x2, r.x2 ) && + QMAX( y1, r.y1 ) <= QMIN( y2, r.y2 ) ); +} + + +/*! + \relates QRect + + Returns TRUE if \a r1 and \a r2 are equal; otherwise returns FALSE. +*/ + +bool operator==( const QRect &r1, const QRect &r2 ) +{ + return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2; +} + +/*! + \relates QRect + + Returns TRUE if \a r1 and \a r2 are different; otherwise returns FALSE. +*/ + +bool operator!=( const QRect &r1, const QRect &r2 ) +{ + return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2; +} + + +/***************************************************************************** + QRect stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QRect + + Writes the QRect, \a r, to the stream \a s, and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QRect &r ) +{ + if ( s.version() == 1 ) + s << (Q_INT16)r.left() << (Q_INT16)r.top() + << (Q_INT16)r.right() << (Q_INT16)r.bottom(); + else + s << (Q_INT32)r.left() << (Q_INT32)r.top() + << (Q_INT32)r.right() << (Q_INT32)r.bottom(); + return s; +} + +/*! + \relates QRect + + Reads a QRect from the stream \a s into rect \a r and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QRect &r ) +{ + if ( s.version() == 1 ) { + Q_INT16 x1, y1, x2, y2; + s >> x1; s >> y1; s >> x2; s >> y2; + r.setCoords( x1, y1, x2, y2 ); + } + else { + Q_INT32 x1, y1, x2, y2; + s >> x1; s >> y1; s >> x2; s >> y2; + r.setCoords( x1, y1, x2, y2 ); + } + return s; +} +#endif // QT_NO_DATASTREAM diff --git a/src/kernel/qrect.h b/src/kernel/qrect.h new file mode 100644 index 0000000..3a2e5fe --- /dev/null +++ b/src/kernel/qrect.h @@ -0,0 +1,276 @@ +/**************************************************************************** +** +** Definition of QRect class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QRECT_H +#define QRECT_H + +#ifndef QT_H +#include "qsize.h" +#endif // QT_H + +#if defined(topLeft) +#error "Macro definition of topLeft conflicts with QRect" +// don't just silently undo people's defines: #undef topLeft +#endif + +class Q_EXPORT QRect // rectangle class +{ +public: + QRect() { x1 = y1 = 0; x2 = y2 = -1; } + QRect( const QPoint &topleft, const QPoint &bottomright ); + QRect( const QPoint &topleft, const QSize &size ); + QRect( int left, int top, int width, int height ); + + bool isNull() const; + bool isEmpty() const; + bool isValid() const; + QRect normalize() const; + + int left() const; + int top() const; + int right() const; + int bottom() const; + + QCOORD &rLeft(); + QCOORD &rTop(); + QCOORD &rRight(); + QCOORD &rBottom(); + + int x() const; + int y() const; + void setLeft( int pos ); + void setTop( int pos ); + void setRight( int pos ); + void setBottom( int pos ); + void setX( int x ); + void setY( int y ); + + void setTopLeft( const QPoint &p ); + void setBottomRight( const QPoint &p ); + void setTopRight( const QPoint &p ); + void setBottomLeft( const QPoint &p ); + + QPoint topLeft() const; + QPoint bottomRight() const; + QPoint topRight() const; + QPoint bottomLeft() const; + QPoint center() const; + + void rect( int *x, int *y, int *w, int *h ) const; + void coords( int *x1, int *y1, int *x2, int *y2 ) const; + + void moveLeft( int pos ); + void moveTop( int pos ); + void moveRight( int pos ); + void moveBottom( int pos ); + void moveTopLeft( const QPoint &p ); + void moveBottomRight( const QPoint &p ); + void moveTopRight( const QPoint &p ); + void moveBottomLeft( const QPoint &p ); + void moveCenter( const QPoint &p ); + void moveBy( int dx, int dy ); + + void setRect( int x, int y, int w, int h ); + void setCoords( int x1, int y1, int x2, int y2 ); + void addCoords( int x1, int y1, int x2, int y2 ); + + QSize size() const; + int width() const; + int height() const; + void setWidth( int w ); + void setHeight( int h ); + void setSize( const QSize &s ); + + QRect operator|(const QRect &r) const; + QRect operator&(const QRect &r) const; + QRect& operator|=(const QRect &r); + QRect& operator&=(const QRect &r); + + bool contains( const QPoint &p, bool proper=FALSE ) const; + bool contains( int x, int y ) const; // inline methods, _don't_ merge these + bool contains( int x, int y, bool proper ) const; + bool contains( const QRect &r, bool proper=FALSE ) const; + QRect unite( const QRect &r ) const; + QRect intersect( const QRect &r ) const; + bool intersects( const QRect &r ) const; + + friend Q_EXPORT bool operator==( const QRect &, const QRect & ); + friend Q_EXPORT bool operator!=( const QRect &, const QRect & ); + +private: +#if defined(Q_WS_X11) || defined(Q_OS_TEMP) + friend void qt_setCoords( QRect *r, int xp1, int yp1, int xp2, int yp2 ); +#endif +#if defined(Q_OS_MAC) + QCOORD y1; + QCOORD x1; + QCOORD y2; + QCOORD x2; +#else + QCOORD x1; + QCOORD y1; + QCOORD x2; + QCOORD y2; +#endif +}; + +Q_EXPORT bool operator==( const QRect &, const QRect & ); +Q_EXPORT bool operator!=( const QRect &, const QRect & ); + + +/***************************************************************************** + QRect stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QRect & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QRect & ); +#endif + +/***************************************************************************** + QRect inline member functions + *****************************************************************************/ + +inline QRect::QRect( int left, int top, int width, int height ) +{ + x1 = (QCOORD)left; + y1 = (QCOORD)top; + x2 = (QCOORD)(left+width-1); + y2 = (QCOORD)(top+height-1); +} + +inline bool QRect::isNull() const +{ return x2 == x1-1 && y2 == y1-1; } + +inline bool QRect::isEmpty() const +{ return x1 > x2 || y1 > y2; } + +inline bool QRect::isValid() const +{ return x1 <= x2 && y1 <= y2; } + +inline int QRect::left() const +{ return x1; } + +inline int QRect::top() const +{ return y1; } + +inline int QRect::right() const +{ return x2; } + +inline int QRect::bottom() const +{ return y2; } + +inline QCOORD &QRect::rLeft() +{ return x1; } + +inline QCOORD & QRect::rTop() +{ return y1; } + +inline QCOORD & QRect::rRight() +{ return x2; } + +inline QCOORD & QRect::rBottom() +{ return y2; } + +inline int QRect::x() const +{ return x1; } + +inline int QRect::y() const +{ return y1; } + +inline void QRect::setLeft( int pos ) +{ x1 = (QCOORD)pos; } + +inline void QRect::setTop( int pos ) +{ y1 = (QCOORD)pos; } + +inline void QRect::setRight( int pos ) +{ x2 = (QCOORD)pos; } + +inline void QRect::setBottom( int pos ) +{ y2 = (QCOORD)pos; } + +inline void QRect::setX( int x ) +{ x1 = (QCOORD)x; } + +inline void QRect::setY( int y ) +{ y1 = (QCOORD)y; } + +inline QPoint QRect::topLeft() const +{ return QPoint(x1, y1); } + +inline QPoint QRect::bottomRight() const +{ return QPoint(x2, y2); } + +inline QPoint QRect::topRight() const +{ return QPoint(x2, y1); } + +inline QPoint QRect::bottomLeft() const +{ return QPoint(x1, y2); } + +inline QPoint QRect::center() const +{ return QPoint((x1+x2)/2, (y1+y2)/2); } + +inline int QRect::width() const +{ return x2 - x1 + 1; } + +inline int QRect::height() const +{ return y2 - y1 + 1; } + +inline QSize QRect::size() const +{ return QSize(x2-x1+1, y2-y1+1); } + +inline bool QRect::contains( int x, int y, bool proper ) const +{ + if ( proper ) + return x > x1 && x < x2 && + y > y1 && y < y2; + else + return x >= x1 && x <= x2 && + y >= y1 && y <= y2; +} + +inline bool QRect::contains( int x, int y ) const +{ + return x >= x1 && x <= x2 && + y >= y1 && y <= y2; +} +#define Q_DEFINED_QRECT +#include "qwinexport.h" +#endif // QRECT_H diff --git a/src/kernel/qregion.cpp b/src/kernel/qregion.cpp new file mode 100644 index 0000000..603ab44 --- /dev/null +++ b/src/kernel/qregion.cpp @@ -0,0 +1,383 @@ +/**************************************************************************** +** +** Implementation of QRegion class +** +** Created : 950726 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qregion.h" +#include "qpointarray.h" +#include "qbuffer.h" +#include "qdatastream.h" + +// BEING REVISED: paul +/*! + \class QRegion qregion.h + \brief The QRegion class specifies a clip region for a painter. + + \ingroup images + \ingroup graphics + + QRegion is used with QPainter::setClipRegion() to limit the paint + area to what needs to be painted. There is also a + QWidget::repaint() that takes a QRegion parameter. QRegion is the + best tool for reducing flicker. + + A region can be created from a rectangle, an ellipse, a polygon or + a bitmap. Complex regions may be created by combining simple + regions using unite(), intersect(), subtract() or eor() (exclusive + or). You can move a region using translate(). + + You can test whether a region isNull(), isEmpty() or if it + contains() a QPoint or QRect. The bounding rectangle is given by + boundingRect(). + + The function rects() gives a decomposition of the region into + rectangles. + + Example of using complex regions: + \code + void MyWidget::paintEvent( QPaintEvent * ) + { + QPainter p; // our painter + QRegion r1( QRect(100,100,200,80), // r1 = elliptic region + QRegion::Ellipse ); + QRegion r2( QRect(100,120,90,30) ); // r2 = rectangular region + QRegion r3 = r1.intersect( r2 ); // r3 = intersection + p.begin( this ); // start painting widget + p.setClipRegion( r3 ); // set clip region + ... // paint clipped graphics + p.end(); // painting done + } + \endcode + + QRegion is an \link shclass.html implicitly shared\endlink class. + + \warning Due to window system limitations, the whole coordinate + space for a region is limited to the points between -32767 and + 32767 on Mac OS X and Windows 95/98/ME. + + \sa QPainter::setClipRegion(), QPainter::setClipRect() +*/ + + +/*! + \enum QRegion::RegionType + + Specifies the shape of the region to be created. + + \value Rectangle the region covers the entire rectangle. + \value Ellipse the region is an ellipse inside the rectangle. +*/ + +/*! + \fn Region QRegion::handle() const + + Returns the region's handle. +*/ + +/***************************************************************************** + QRegion member functions + *****************************************************************************/ + +/*! + Constructs a rectangular or elliptic region. + + If \a t is \c Rectangle, the region is the filled rectangle (\a x, + \a y, \a w, \a h). If \a t is \c Ellipse, the region is the filled + ellipse with center at (\a x + \a w / 2, \a y + \a h / 2) and size + (\a w ,\a h ). +*/ +QRegion::QRegion( int x, int y, int w, int h, RegionType t ) +{ + QRegion tmp(QRect(x,y,w,h),t); + tmp.data->ref(); + data = tmp.data; +} + +/*! + Detaches from shared region data to make sure that this region is + the only one referring to the data. + + \sa copy(), \link shclass.html shared classes\endlink +*/ + +void QRegion::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + +#ifndef QT_NO_DATASTREAM +/* + Executes region commands in the internal buffer and rebuilds the + original region. + + We do this when we read a region from the data stream. + + If \a ver is non-0, uses the format version \a ver on reading the + byte array. +*/ + +void QRegion::exec( const QByteArray &buffer, int ver ) +{ + QBuffer buf( buffer ); + QDataStream s( &buf ); + if ( ver ) + s.setVersion( ver ); + buf.open( IO_ReadOnly ); + QRegion rgn; +#if defined(QT_CHECK_STATE) + int test_cnt = 0; +#endif + while ( !s.eof() ) { + Q_INT32 id; + if ( s.version() == 1 ) { + int id_int; + s >> id_int; + id = id_int; + } else { + s >> id; + } +#if defined(QT_CHECK_STATE) + if ( test_cnt > 0 && id != QRGN_TRANSLATE ) + qWarning( "QRegion::exec: Internal error" ); + test_cnt++; +#endif + if ( id == QRGN_SETRECT || id == QRGN_SETELLIPSE ) { + QRect r; + s >> r; + rgn = QRegion( r, id == QRGN_SETRECT ? Rectangle : Ellipse ); + } else if ( id == QRGN_SETPTARRAY_ALT || id == QRGN_SETPTARRAY_WIND ) { + QPointArray a; + s >> a; + rgn = QRegion( a, id == QRGN_SETPTARRAY_WIND ); + } else if ( id == QRGN_TRANSLATE ) { + QPoint p; + s >> p; + rgn.translate( p.x(), p.y() ); + } else if ( id >= QRGN_OR && id <= QRGN_XOR ) { + QByteArray bop1, bop2; + QRegion r1, r2; + s >> bop1; r1.exec( bop1 ); + s >> bop2; r2.exec( bop2 ); + switch ( id ) { + case QRGN_OR: + rgn = r1.unite( r2 ); + break; + case QRGN_AND: + rgn = r1.intersect( r2 ); + break; + case QRGN_SUB: + rgn = r1.subtract( r2 ); + break; + case QRGN_XOR: + rgn = r1.eor( r2 ); + break; + } + } else if ( id == QRGN_RECTS ) { + // (This is the only form used in Qt 2.0) + Q_UINT32 n; + s >> n; + QRect r; + for ( int i=0; i<(int)n; i++ ) { + s >> r; + rgn = rgn.unite( QRegion(r) ); + } + } + } + buf.close(); + *this = rgn; +} + + +/***************************************************************************** + QRegion stream functions + *****************************************************************************/ + +/*! + \relates QRegion + + Writes the region \a r to the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QRegion &r ) +{ + QMemArray<QRect> a = r.rects(); + if ( a.isEmpty() ) { + s << (Q_UINT32)0; + } else { + if ( s.version() == 1 ) { + int i; + for ( i=(int)a.size()-1; i>0; i-- ) { + s << (Q_UINT32)(12+i*24); + s << (int)QRGN_OR; + } + for ( i=0; i<(int)a.size(); i++ ) { + s << (Q_UINT32)(4+8) << (int)QRGN_SETRECT << a[i]; + } + } + else { + s << (Q_UINT32)(4+4+16*a.size()); // 16: storage size of QRect + s << (Q_INT32)QRGN_RECTS; + s << (Q_UINT32)a.size(); + for ( int i=0; i<(int)a.size(); i++ ) + s << a[i]; + } + } + return s; +} + +/*! + \relates QRegion + + Reads a region from the stream \a s into \a r and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QRegion &r ) +{ + QByteArray b; + s >> b; + r.exec( b, s.version() ); + return s; +} +#endif //QT_NO_DATASTREAM + +// These are not inline - they can be implemented better on some platforms +// (eg. Windows at least provides 3-variable operations). For now, simple. + + +/*! + Applies the unite() function to this region and \a r. \c r1|r2 is + equivalent to \c r1.unite(r2) + + \sa unite(), operator+() +*/ +const QRegion QRegion::operator|( const QRegion &r ) const + { return unite(r); } + +/*! + Applies the unite() function to this region and \a r. \c r1+r2 is + equivalent to \c r1.unite(r2) + + \sa unite(), operator|() +*/ +const QRegion QRegion::operator+( const QRegion &r ) const + { return unite(r); } + +/*! + Applies the intersect() function to this region and \a r. \c r1&r2 + is equivalent to \c r1.intersect(r2) + + \sa intersect() +*/ +const QRegion QRegion::operator&( const QRegion &r ) const + { return intersect(r); } + +/*! + Applies the subtract() function to this region and \a r. \c r1-r2 + is equivalent to \c r1.subtract(r2) + + \sa subtract() +*/ +const QRegion QRegion::operator-( const QRegion &r ) const + { return subtract(r); } + +/*! + Applies the eor() function to this region and \a r. \c r1^r2 is + equivalent to \c r1.eor(r2) + + \sa eor() +*/ +const QRegion QRegion::operator^( const QRegion &r ) const + { return eor(r); } + +/*! + Applies the unite() function to this region and \a r and assigns + the result to this region. \c r1|=r2 is equivalent to \c + r1=r1.unite(r2) + + \sa unite() +*/ +QRegion& QRegion::operator|=( const QRegion &r ) + { return *this = *this | r; } + +/*! + Applies the unite() function to this region and \a r and assigns + the result to this region. \c r1+=r2 is equivalent to \c + r1=r1.unite(r2) + + \sa intersect() +*/ +QRegion& QRegion::operator+=( const QRegion &r ) + { return *this = *this + r; } + +/*! + Applies the intersect() function to this region and \a r and + assigns the result to this region. \c r1&=r2 is equivalent to \c + r1=r1.intersect(r2) + + \sa intersect() +*/ +QRegion& QRegion::operator&=( const QRegion &r ) + { return *this = *this & r; } + +/*! + Applies the subtract() function to this region and \a r and + assigns the result to this region. \c r1-=r2 is equivalent to \c + r1=r1.subtract(r2) + + \sa subtract() +*/ +QRegion& QRegion::operator-=( const QRegion &r ) + { return *this = *this - r; } + +/*! + Applies the eor() function to this region and \a r and + assigns the result to this region. \c r1^=r2 is equivalent to \c + r1=r1.eor(r2) + + \sa eor() +*/ +QRegion& QRegion::operator^=( const QRegion &r ) + { return *this = *this ^ r; } + diff --git a/src/kernel/qregion.h b/src/kernel/qregion.h new file mode 100644 index 0000000..c2650ef --- /dev/null +++ b/src/kernel/qregion.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Definition of QRegion class +** +** Created : 940514 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QREGION_H +#define QREGION_H + +#ifndef QT_H +#include "qshared.h" +#include "qrect.h" +#endif // QT_H + +#ifdef Q_WS_X11 +struct QRegionPrivate; +#endif + +class Q_EXPORT QRegion +{ +public: + enum RegionType { Rectangle, Ellipse }; + + QRegion(); + QRegion( int x, int y, int w, int h, RegionType = Rectangle ); + QRegion( const QRect &, RegionType = Rectangle ); + QRegion( const QPointArray &, bool winding=FALSE ); + QRegion( const QRegion & ); + QRegion( const QBitmap & ); + ~QRegion(); + QRegion &operator=( const QRegion & ); + + bool isNull() const; + bool isEmpty() const; + + bool contains( const QPoint &p ) const; + bool contains( const QRect &r ) const; + + void translate( int dx, int dy ); + + QRegion unite( const QRegion & ) const; + QRegion intersect( const QRegion &) const; + QRegion subtract( const QRegion & ) const; + QRegion eor( const QRegion & ) const; + + QRect boundingRect() const; + QMemArray<QRect> rects() const; + void setRects( const QRect *, int ); + + const QRegion operator|( const QRegion & ) const; + const QRegion operator+( const QRegion & ) const; + const QRegion operator&( const QRegion & ) const; + const QRegion operator-( const QRegion & ) const; + const QRegion operator^( const QRegion & ) const; + QRegion& operator|=( const QRegion & ); + QRegion& operator+=( const QRegion & ); + QRegion& operator&=( const QRegion & ); + QRegion& operator-=( const QRegion & ); + QRegion& operator^=( const QRegion & ); + + bool operator==( const QRegion & ) const; + bool operator!=( const QRegion &r ) const + { return !(operator==(r)); } + +#if defined(Q_WS_WIN) + HRGN handle() const { return data->rgn; } +#elif defined(Q_WS_X11) + Region handle() const { if(!data->rgn) updateX11Region(); return data->rgn; } +#elif defined(Q_WS_MAC) + RgnHandle handle(bool require_rgn=FALSE) const; +#elif defined(Q_WS_QWS) + // QGfx_QWS needs this for region drawing + void * handle() const { return data->rgn; } +#endif + +#ifndef QT_NO_DATASTREAM + friend Q_EXPORT QDataStream &operator<<( QDataStream &, const QRegion & ); + friend Q_EXPORT QDataStream &operator>>( QDataStream &, QRegion & ); +#endif +private: + QRegion( bool ); + QRegion copy() const; + void detach(); +#if defined(Q_WS_WIN) + QRegion winCombine( const QRegion &, int ) const; +#endif +#if defined(Q_WS_X11) + void updateX11Region() const; + void *clipRectangles( int &num ) const; + friend void *qt_getClipRects( const QRegion &, int & ); +#endif + void exec( const QByteArray &, int ver = 0 ); + struct QRegionData : public QShared { +#if defined(Q_WS_WIN) + HRGN rgn; +#elif defined(Q_WS_X11) + Region rgn; + void *xrectangles; + QRegionPrivate *region; +#elif defined(Q_WS_MAC) + uint is_rect:1; + QRect rect; + RgnHandle rgn; +#elif defined(Q_WS_QWS) + void * rgn; +#endif + bool is_null; + } *data; +#if defined(Q_WS_MAC) + friend struct qt_mac_rgn_data_cache; + friend QRegionData *qt_mac_get_rgn_data(); + friend void qt_mac_free_rgn_data(QRegionData *); + void rectifyRegion(); +#elif defined(Q_WS_WIN) + friend class QETWidget; +#endif + +}; + + +#define QRGN_SETRECT 1 // region stream commands +#define QRGN_SETELLIPSE 2 // (these are internal) +#define QRGN_SETPTARRAY_ALT 3 +#define QRGN_SETPTARRAY_WIND 4 +#define QRGN_TRANSLATE 5 +#define QRGN_OR 6 +#define QRGN_AND 7 +#define QRGN_SUB 8 +#define QRGN_XOR 9 +#define QRGN_RECTS 10 + + +/***************************************************************************** + QRegion stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream &operator<<( QDataStream &, const QRegion & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QRegion & ); +#endif + + +#endif // QREGION_H diff --git a/src/kernel/qregion_x11.cpp b/src/kernel/qregion_x11.cpp new file mode 100644 index 0000000..7b04a15 --- /dev/null +++ b/src/kernel/qregion_x11.cpp @@ -0,0 +1,2898 @@ +/**************************************************************************** +** +** Implementation of QRegion class for X11 +** +** Created : 940729 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qregion.h" +#include "qpointarray.h" +#include "qbuffer.h" +#include "qimage.h" +#include "qbitmap.h" +#include "qt_x11_p.h" + +#include <stdlib.h> + +// inline QRect::setCoords +inline void qt_setCoords( QRect *r, int xp1, int yp1, int xp2, int yp2 ) +{ + r->x1 = (QCOORD)xp1; + r->y1 = (QCOORD)yp1; + r->x2 = (QCOORD)xp2; + r->y2 = (QCOORD)yp2; +} + +/* + * clip region + */ + +struct QRegionPrivate { + int numRects; + QMemArray<QRect> rects; + QRect extents; + + QRegionPrivate() { numRects = 0; } + QRegionPrivate( const QRect &r ) : rects(1) { + numRects = 1; + rects[0] = r; + extents = r; + } + + QRegionPrivate( const QRegionPrivate &r ) { + rects = r.rects.copy(); + numRects = r.numRects; + extents = r.extents; + } + + QRegionPrivate &operator=( const QRegionPrivate &r ) { + rects = r.rects.copy(); + numRects = r.numRects; + extents = r.extents; + return *this; + } + +}; + + +static void UnionRegion(QRegionPrivate *reg1, QRegionPrivate *reg2, QRegionPrivate *newReg); +static void IntersectRegion(QRegionPrivate *reg1, QRegionPrivate *reg2, register QRegionPrivate *newReg); +static void miRegionOp(register QRegionPrivate *newReg, QRegionPrivate *reg1, QRegionPrivate *reg2, + void (*overlapFunc)(...), + void (*nonOverlap1Func)(...), + void (*nonOverlap2Func)(...)); +#define RectangleOut 0 +#define RectangleIn 1 +#define RectanglePart 2 +#define EvenOddRule 0 +#define WindingRule 1 + +// START OF region.h extract +/* $XConsortium: region.h,v 11.14 94/04/17 20:22:20 rws Exp $ */ +/************************************************************************ + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ + +#ifndef _XREGION_H +#define _XREGION_H + +#include <limits.h> + +#ifndef MAX +#define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#endif +#ifndef MIN +#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#endif + + +/* 1 if two BOXs overlap. + * 0 if two BOXs do not overlap. + * Remember, x2 and y2 are not in the region + */ +#define EXTENTCHECK(r1, r2) \ + ((r1)->right() >= (r2)->left() && \ + (r1)->left() <= (r2)->right() && \ + (r1)->bottom() >= (r2)->top() && \ + (r1)->top() <= (r2)->bottom()) + +/* + * update region extents + */ +#define EXTENTS(r,idRect){\ + if((r)->left() < (idRect)->extents.left())\ + (idRect)->extents.setLeft( (r)->left() );\ + if((r)->top() < (idRect)->extents.top())\ + (idRect)->extents.setTop( (r)->top() );\ + if((r)->right() > (idRect)->extents.right())\ + (idRect)->extents.setRight( (r)->right() );\ + if((r)->bottom() > (idRect)->extents.bottom())\ + (idRect)->extents.setBottom( (r)->bottom() );\ + } + +/* + * Check to see if there is enough memory in the present region. + */ +#define MEMCHECK(reg, rect, firstrect){\ + if ((reg)->numRects >= (int)((reg)->rects.size()-1)){\ + firstrect.resize(firstrect.size() * 2); \ + (rect) = (firstrect).data() + (reg)->numRects;\ + }\ + } + + +#define EMPTY_REGION(pReg) pReg->numRects = 0 + +#define REGION_NOT_EMPTY(pReg) pReg->numRects + +/* + * number of points to buffer before sending them off + * to scanlines() : Must be an even number + */ +#define NUMPTSTOBUFFER 200 + +/* + * used to allocate buffers for points and link + * the buffers together + */ +typedef struct _POINTBLOCK { + QPoint pts[NUMPTSTOBUFFER]; + struct _POINTBLOCK *next; +} POINTBLOCK; + +#endif +// END OF region.h extract + +// START OF Region.c extract +/* $XConsortium: Region.c /main/30 1996/10/22 14:21:24 kaleb $ */ +/************************************************************************ + +Copyright (c) 1987, 1988 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ +/* + * The functions in this file implement the Region abstraction, similar to one + * used in the X11 sample server. A Region is simply an area, as the name + * implies, and is implemented as a "y-x-banded" array of rectangles. To + * explain: Each Region is made up of a certain number of rectangles sorted + * by y coordinate first, and then by x coordinate. + * + * Furthermore, the rectangles are banded such that every rectangle with a + * given upper-left y coordinate (y1) will have the same lower-right y + * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it + * will span the entire vertical distance of the band. This means that some + * areas that could be merged into a taller rectangle will be represented as + * several shorter rectangles to account for shorter rectangles to its left + * or right but within its "vertical scope". + * + * An added constraint on the rectangles is that they must cover as much + * horizontal area as possible. E.g. no two rectangles in a band are allowed + * to touch. + * + * Whenever possible, bands will be merged together to cover a greater vertical + * distance (and thus reduce the number of rectangles). Two bands can be merged + * only if the bottom of one touches the top of the other and they have + * rectangles in the same places (of the same width, of course). This maintains + * the y-x-banding that's so nice to have... + */ +/* $XFree86: xc/lib/X11/Region.c,v 1.1.1.2.2.2 1998/10/04 15:22:50 hohndel Exp $ */ + +typedef void (*voidProcp)(...); + + +static +void UnionRectWithRegion(register const QRect *rect, QRegionPrivate *source, QRegionPrivate *dest) +{ + QRegionPrivate region; + + if (!rect->width() || !rect->height()) + return; + region.rects.resize(1); + region.numRects = 1; + region.rects[0] = *rect; + region.extents = *rect; + + UnionRegion(®ion, source, dest); + return; +} + +/*- + *----------------------------------------------------------------------- + * miSetExtents -- + * Reset the extents of a region to what they should be. Called by + * miSubtract and miIntersect b/c they can't figure it out along the + * way or do so easily, as miUnion can. + * + * Results: + * None. + * + * Side Effects: + * The region's 'extents' structure is overwritten. + * + *----------------------------------------------------------------------- + */ +static void +miSetExtents (QRegionPrivate *pReg) +{ + register QRect *pBox, + *pBoxEnd, + *pExtents; + + if (pReg->numRects == 0) + { + qt_setCoords(&pReg->extents, 0, 0, 0, 0); + return; + } + + pExtents = &pReg->extents; + pBox = pReg->rects.data(); + pBoxEnd = &pBox[pReg->numRects - 1]; + + /* + * Since pBox is the first rectangle in the region, it must have the + * smallest y1 and since pBoxEnd is the last rectangle in the region, + * it must have the largest y2, because of banding. Initialize x1 and + * x2 from pBox and pBoxEnd, resp., as good things to initialize them + * to... + */ + pExtents->setLeft( pBox->left() ); + pExtents->setTop( pBox->top() ); + pExtents->setRight( pBoxEnd->right() ); + pExtents->setBottom( pBoxEnd->bottom() ); + + Q_ASSERT(pExtents->top() <= pExtents->bottom()); + while (pBox <= pBoxEnd) + { + if (pBox->left() < pExtents->left()) + { + pExtents->setLeft( pBox->left() ); + } + if (pBox->right() > pExtents->right()) + { + pExtents->setRight( pBox->right() ); + } + pBox++; + } + Q_ASSERT(pExtents->left() <= pExtents->right()); +} + + +/* TranslateRegion(pRegion, x, y) + translates in place + added by raymond +*/ + +static +int +OffsetRegion(register QRegionPrivate *pRegion, register int x, register int y) +{ + register int nbox; + register QRect *pbox; + + pbox = pRegion->rects.data(); + nbox = pRegion->numRects; + + while(nbox--) + { + pbox->moveBy(x, y); + pbox++; + } + pRegion->extents.moveBy(x, y); + return 1; +} + +/*====================================================================== + * Region Intersection + *====================================================================*/ +/*- + *----------------------------------------------------------------------- + * miIntersectO -- + * Handle an overlapping band for miIntersect. + * + * Results: + * None. + * + * Side Effects: + * Rectangles may be added to the region. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miIntersectO (register QRegionPrivate *pReg, register QRect *r1, QRect *r1End, + register QRect *r2, QRect *r2End, int y1, int y2) +{ + register int x1; + register int x2; + register QRect *pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + + while ((r1 != r1End) && (r2 != r2End)) + { + x1 = QMAX(r1->left(),r2->left()); + x2 = QMIN(r1->right(),r2->right()); + + /* + * If there's any overlap between the two rectangles, add that + * overlap to the new region. + * There's no need to check for subsumption because the only way + * such a need could arise is if some region has two rectangles + * right next to each other. Since that should never happen... + */ + if (x1 <= x2) + { + Q_ASSERT(y1<=y2); + + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, x2, y2 ); + pReg->numRects++; + pNextRect++; + } + + /* + * Need to advance the pointers. Shift the one that extends + * to the right the least, since the other still has a chance to + * overlap with that region's next rectangle, if you see what I mean. + */ + if (r1->right() < r2->right()) + { + r1++; + } + else if (r2->right() < r1->right()) + { + r2++; + } + else + { + r1++; + r2++; + } + } + return 0; /* lint */ +} + +static +void +IntersectRegion(QRegionPrivate *reg1, QRegionPrivate *reg2, register QRegionPrivate *newReg) +{ + /* check for trivial reject */ + if ( (!(reg1->numRects)) || (!(reg2->numRects)) || + (!EXTENTCHECK(®1->extents, ®2->extents))) + newReg->numRects = 0; + else + miRegionOp (newReg, reg1, reg2, + (voidProcp) miIntersectO, (voidProcp) NULL, (voidProcp) NULL); + + /* + * Can't alter newReg's extents before we call miRegionOp because + * it might be one of the source regions and miRegionOp depends + * on the extents of those regions being the same. Besides, this + * way there's no checking against rectangles that will be nuked + * due to coalescing, so we have to examine fewer rectangles. + */ + miSetExtents(newReg); + return; +} + +/*====================================================================== + * Generic Region Operator + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miCoalesce -- + * Attempt to merge the boxes in the current band with those in the + * previous one. Used only by miRegionOp. + * + * Results: + * The new index for the previous band. + * + * Side Effects: + * If coalescing takes place: + * - rectangles in the previous band will have their y2 fields + * altered. + * - pReg->numRects will be decreased. + * + *----------------------------------------------------------------------- + */ +/* static int*/ +static +int +miCoalesce (register QRegionPrivate *pReg, int prevStart, int curStart) + //Region pReg; /* Region to coalesce */ + //prevStart; /* Index of start of previous band */ + //curStart; /* Index of start of current band */ +{ + register QRect *pPrevBox; /* Current box in previous band */ + register QRect *pCurBox; /* Current box in current band */ + register QRect *pRegEnd; /* End of region */ + int curNumRects; /* Number of rectangles in current + * band */ + int prevNumRects; /* Number of rectangles in previous + * band */ + int bandY1; /* Y1 coordinate for current band */ + + pRegEnd = pReg->rects.data() + pReg->numRects; + + pPrevBox = pReg->rects.data() + prevStart; + prevNumRects = curStart - prevStart; + + /* + * Figure out how many rectangles are in the current band. Have to do + * this because multiple bands could have been added in miRegionOp + * at the end when one region has been exhausted. + */ + pCurBox = pReg->rects.data() + curStart; + bandY1 = pCurBox->top(); + for (curNumRects = 0; + (pCurBox != pRegEnd) && (pCurBox->top() == bandY1); + curNumRects++) + { + pCurBox++; + } + + if (pCurBox != pRegEnd) + { + /* + * If more than one band was added, we have to find the start + * of the last band added so the next coalescing job can start + * at the right place... (given when multiple bands are added, + * this may be pointless -- see above). + */ + pRegEnd--; + while ((pRegEnd-1)->top() == pRegEnd->top()) + { + pRegEnd--; + } + curStart = pRegEnd - pReg->rects.data(); + pRegEnd = pReg->rects.data() + pReg->numRects; + } + + if ((curNumRects == prevNumRects) && (curNumRects != 0)) { + pCurBox -= curNumRects; + /* + * The bands may only be coalesced if the bottom of the previous + * matches the top scanline of the current. + */ + if (pPrevBox->bottom() == pCurBox->top() - 1) + { + /* + * Make sure the bands have boxes in the same places. This + * assumes that boxes have been added in such a way that they + * cover the most area possible. I.e. two boxes in a band must + * have some horizontal space between them. + */ + do + { + if ((pPrevBox->left() != pCurBox->left()) || + (pPrevBox->right() != pCurBox->right())) + { + /* + * The bands don't line up so they can't be coalesced. + */ + return (curStart); + } + pPrevBox++; + pCurBox++; + prevNumRects -= 1; + } while (prevNumRects != 0); + + pReg->numRects -= curNumRects; + pCurBox -= curNumRects; + pPrevBox -= curNumRects; + + /* + * The bands may be merged, so set the bottom y of each box + * in the previous band to that of the corresponding box in + * the current band. + */ + do + { + pPrevBox->setBottom( pCurBox->bottom() ); + pPrevBox++; + pCurBox++; + curNumRects -= 1; + } while (curNumRects != 0); + + /* + * If only one band was added to the region, we have to backup + * curStart to the start of the previous band. + * + * If more than one band was added to the region, copy the + * other bands down. The assumption here is that the other bands + * came from the same region as the current one and no further + * coalescing can be done on them since it's all been done + * already... curStart is already in the right place. + */ + if (pCurBox == pRegEnd) + { + curStart = prevStart; + } + else + { + do + { + *pPrevBox++ = *pCurBox++; + } while (pCurBox != pRegEnd); + } + + } + } + return (curStart); +} + +/*- + *----------------------------------------------------------------------- + * miRegionOp -- + * Apply an operation to two regions. Called by miUnion, miInverse, + * miSubtract, miIntersect... + * + * Results: + * None. + * + * Side Effects: + * The new region is overwritten. + * + * Notes: + * The idea behind this function is to view the two regions as sets. + * Together they cover a rectangle of area that this function divides + * into horizontal bands where points are covered only by one region + * or by both. For the first case, the nonOverlapFunc is called with + * each the band and the band's upper and lower extents. For the + * second, the overlapFunc is called to process the entire band. It + * is responsible for clipping the rectangles in the band, though + * this function provides the boundaries. + * At the end of each band, the new region is coalesced, if possible, + * to reduce the number of rectangles in the region. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static void +miRegionOp(register QRegionPrivate *newReg, QRegionPrivate *reg1, QRegionPrivate *reg2, + void (*overlapFunc)(...), + void (*nonOverlap1Func)(...), + void (*nonOverlap2Func)(...)) + //register Region newReg; /* Place to store result */ + //Region reg1; /* First region in operation */ + //Region reg2; /* 2d region in operation */ + //void (*overlapFunc)(); /* Function to call for over- + //* lapping bands */ + //void (*nonOverlap1Func)(); /* Function to call for non- + //* overlapping bands in region + //* 1 */ + //void (*nonOverlap2Func)(); /* Function to call for non- + //* overlapping bands in region + //* 2 */ +{ + register QRect *r1; /* Pointer into first region */ + register QRect *r2; /* Pointer into 2d region */ + QRect *r1End; /* End of 1st region */ + QRect *r2End; /* End of 2d region */ + register int ybot; /* Bottom of intersection */ + register int ytop; /* Top of intersection */ + int prevBand; /* Index of start of + * previous band in newReg */ + int curBand; /* Index of start of current + * band in newReg */ + register QRect *r1BandEnd; /* End of current band in r1 */ + register QRect *r2BandEnd; /* End of current band in r2 */ + int top; /* Top of non-overlapping + * band */ + int bot; /* Bottom of non-overlapping + * band */ + + /* + * Initialization: + * set r1, r2, r1End and r2End appropriately, preserve the important + * parts of the destination region until the end in case it's one of + * the two source regions, then mark the "new" region empty, allocating + * another array of rectangles for it to use. + */ + r1 = reg1->rects.data(); + r2 = reg2->rects.data(); + r1End = r1 + reg1->numRects; + r2End = r2 + reg2->numRects; + + QMemArray<QRect> oldRects = newReg->rects; + + newReg->rects.detach(); + EMPTY_REGION(newReg); + + /* + * Allocate a reasonable number of rectangles for the new region. The idea + * is to allocate enough so the individual functions don't need to + * reallocate and copy the array, which is time consuming, yet we don't + * have to worry about using too much memory. I hope to be able to + * nuke the realloc() at the end of this function eventually. + */ + newReg->rects.resize( QMAX(reg1->numRects,reg2->numRects) * 2 ); + + /* + * Initialize ybot and ytop. + * In the upcoming loop, ybot and ytop serve different functions depending + * on whether the band being handled is an overlapping or non-overlapping + * band. + * In the case of a non-overlapping band (only one of the regions + * has points in the band), ybot is the bottom of the most recent + * intersection and thus clips the top of the rectangles in that band. + * ytop is the top of the next intersection between the two regions and + * serves to clip the bottom of the rectangles in the current band. + * For an overlapping band (where the two regions intersect), ytop clips + * the top of the rectangles of both regions and ybot clips the bottoms. + */ + if (reg1->extents.top() < reg2->extents.top()) + ybot = reg1->extents.top() - 1; + else + ybot = reg2->extents.top() - 1; + + /* + * prevBand serves to mark the start of the previous band so rectangles + * can be coalesced into larger rectangles. qv. miCoalesce, above. + * In the beginning, there is no previous band, so prevBand == curBand + * (curBand is set later on, of course, but the first band will always + * start at index 0). prevBand and curBand must be indices because of + * the possible expansion, and resultant moving, of the new region's + * array of rectangles. + */ + prevBand = 0; + + do + { + curBand = newReg->numRects; + + /* + * This algorithm proceeds one source-band (as opposed to a + * destination band, which is determined by where the two regions + * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the + * rectangle after the last one in the current band for their + * respective regions. + */ + r1BandEnd = r1; + while ((r1BandEnd != r1End) && (r1BandEnd->top() == r1->top())) + { + r1BandEnd++; + } + + r2BandEnd = r2; + while ((r2BandEnd != r2End) && (r2BandEnd->top() == r2->top())) + { + r2BandEnd++; + } + + /* + * First handle the band that doesn't intersect, if any. + * + * Note that attention is restricted to one band in the + * non-intersecting region at once, so if a region has n + * bands between the current position and the next place it overlaps + * the other, this entire loop will be passed through n times. + */ + if (r1->top() < r2->top()) + { + top = QMAX(r1->top(),ybot+1); + bot = QMIN(r1->bottom(),r2->top()-1); + + if ((nonOverlap1Func != (voidProcp)NULL) && bot >= top) + { + (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot); + } + + ytop = r2->top(); + } + else if (r2->top() < r1->top()) + { + top = QMAX(r2->top(),ybot+1); + bot = QMIN(r2->bottom(),r1->top()-1); + + if ((nonOverlap2Func != (voidProcp)NULL) && bot >= top) + { + (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot); + } + + ytop = r1->top(); + } + else + { + ytop = r1->top(); + } + + /* + * If any rectangles got added to the region, try and coalesce them + * with rectangles from the previous band. Note we could just do + * this test in miCoalesce, but some machines incur a not + * inconsiderable cost for function calls, so... + */ + if (newReg->numRects != curBand) + { + prevBand = miCoalesce (newReg, prevBand, curBand); + } + + /* + * Now see if we've hit an intersecting band. The two bands only + * intersect if ybot >= ytop + */ + ybot = QMIN(r1->bottom(), r2->bottom()); + curBand = newReg->numRects; + if (ybot >= ytop) + { + (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot); + + } + + if (newReg->numRects != curBand) + { + prevBand = miCoalesce (newReg, prevBand, curBand); + } + + /* + * If we've finished with a band (y2 == ybot) we skip forward + * in the region to the next band. + */ + if (r1->bottom() == ybot) + { + r1 = r1BandEnd; + } + if (r2->bottom() == ybot) + { + r2 = r2BandEnd; + } + } while ((r1 != r1End) && (r2 != r2End)); + + /* + * Deal with whichever region still has rectangles left. + */ + curBand = newReg->numRects; + if (r1 != r1End) + { + if (nonOverlap1Func != (voidProcp)NULL) + { + do + { + r1BandEnd = r1; + while ((r1BandEnd < r1End) && (r1BandEnd->top() == r1->top())) + { + r1BandEnd++; + } + (* nonOverlap1Func) (newReg, r1, r1BandEnd, + QMAX(r1->top(),ybot+1), r1->bottom()); + r1 = r1BandEnd; + } while (r1 != r1End); + } + } + else if ((r2 != r2End) && (nonOverlap2Func != (voidProcp)NULL)) + { + do + { + r2BandEnd = r2; + while ((r2BandEnd < r2End) && (r2BandEnd->top() == r2->top())) + { + r2BandEnd++; + } + (* nonOverlap2Func) (newReg, r2, r2BandEnd, + QMAX(r2->top(),ybot+1), r2->bottom()); + r2 = r2BandEnd; + } while (r2 != r2End); + } + + if (newReg->numRects != curBand) + { + (void) miCoalesce (newReg, prevBand, curBand); + } + + /* + * A bit of cleanup. To keep regions from growing without bound, + * we shrink the array of rectangles to match the new number of + * rectangles in the region. This never goes to 0, however... + * + * Only do this stuff if the number of rectangles allocated is more than + * twice the number of rectangles in the region (a simple optimization...). + */ + if (newReg->numRects < (int)(newReg->rects.size() >> 1)) + { + if (REGION_NOT_EMPTY(newReg)) + { + newReg->rects.resize(newReg->numRects); + } + else + { + /* + * No point in doing the extra work involved in an realloc if + * the region is empty + */ + newReg->rects.resize(1); + } + } + return; +} + + +/*====================================================================== + * Region Union + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miUnionNonO -- + * Handle a non-overlapping band for the union operation. Just + * Adds the rectangles into the region. Doesn't have to check for + * subsumption or anything. + * + * Results: + * None. + * + * Side Effects: + * pReg->numRects is incremented and the final rectangles overwritten + * with the rectangles we're passed. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miUnionNonO (register QRegionPrivate *pReg, register QRect * r, + QRect * rEnd, register int y1, register int y2) +{ + register QRect * pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + + Q_ASSERT(y1 <= y2); + + while (r != rEnd) + { + Q_ASSERT(r->left() <= r->right()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, r->left(), y1, r->right(), y2 ); + pReg->numRects++; + pNextRect++; + + r++; + } + return 0; /* lint */ +} + + +/*- + *----------------------------------------------------------------------- + * miUnionO -- + * Handle an overlapping band for the union operation. Picks the + * left-most rectangle each time and merges it into the region. + * + * Results: + * None. + * + * Side Effects: + * Rectangles are overwritten in pReg->rects and pReg->numRects will + * be changed. + * + *----------------------------------------------------------------------- + */ + +/* static void*/ +static +int +miUnionO (register QRegionPrivate *pReg, register QRect *r1, QRect *r1End, + register QRect *r2, QRect *r2End, register int y1, register int y2) +{ + register QRect *pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + +#define MERGERECT(r) \ + if ((pReg->numRects != 0) && \ + (pNextRect[-1].top() == y1) && \ + (pNextRect[-1].bottom() == y2) && \ + (pNextRect[-1].right() >= r->left()-1)) { \ + if (pNextRect[-1].right() < r->right()) { \ + pNextRect[-1].setRight( r->right() ); \ + Q_ASSERT(pNextRect[-1].left() <= pNextRect[-1].right()); \ + } \ + } else { \ + MEMCHECK(pReg, pNextRect, pReg->rects) \ + qt_setCoords( pNextRect, r->left(), y1, r->right(), y2 ); \ + pReg->numRects++; \ + pNextRect++; \ + } \ + r++; + + Q_ASSERT (y1<=y2); + while ((r1 != r1End) && (r2 != r2End)) { + if (r1->left() < r2->left()) { + MERGERECT(r1) + } else { + MERGERECT(r2) + } + } + + if (r1 != r1End) + { + do + { + MERGERECT(r1) + } while (r1 != r1End); + } + else while (r2 != r2End) + { + MERGERECT(r2) + } + return 0; /* lint */ +} + +static void UnionRegion(QRegionPrivate *reg1, QRegionPrivate *reg2, QRegionPrivate *newReg) +{ + /* checks all the simple cases */ + + /* + * Region 1 and 2 are the same or region 1 is empty + */ + if ( (reg1 == reg2) || (!(reg1->numRects)) ) + { + *newReg = *reg2; + return; + } + + /* + * if nothing to union (region 2 empty) + */ + if (!(reg2->numRects)) + { + *newReg = *reg1; + return; + } + + /* + * Region 1 completely subsumes region 2 + */ + if ((reg1->numRects == 1) && + (reg1->extents.left() <= reg2->extents.left()) && + (reg1->extents.top() <= reg2->extents.top()) && + (reg1->extents.right() >= reg2->extents.right()) && + (reg1->extents.bottom() >= reg2->extents.bottom())) + { + *newReg = *reg1; + return; + } + + /* + * Region 2 completely subsumes region 1 + */ + if ((reg2->numRects == 1) && + (reg2->extents.left() <= reg1->extents.left()) && + (reg2->extents.top() <= reg1->extents.top()) && + (reg2->extents.right() >= reg1->extents.right()) && + (reg2->extents.bottom() >= reg1->extents.bottom())) + { + *newReg = *reg2; + return; + } + + miRegionOp (newReg, reg1, reg2, (voidProcp) miUnionO, + (voidProcp) miUnionNonO, (voidProcp) miUnionNonO); + + qt_setCoords( &newReg->extents, + QMIN(reg1->extents.left(), reg2->extents.left()), + QMIN(reg1->extents.top(), reg2->extents.top()), + QMAX(reg1->extents.right(), reg2->extents.right()), + QMAX(reg1->extents.bottom(), reg2->extents.bottom()) ); + + return; +} + +/*====================================================================== + * Region Subtraction + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miSubtractNonO -- + * Deal with non-overlapping band for subtraction. Any parts from + * region 2 we discard. Anything from region 1 we add to the region. + * + * Results: + * None. + * + * Side Effects: + * pReg may be affected. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miSubtractNonO1 (register QRegionPrivate *pReg, register QRect *r, + QRect *rEnd, register int y1, register int y2) +{ + register QRect *pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + + Q_ASSERT(y1<=y2); + + while (r != rEnd) + { + Q_ASSERT(r->left()<=r->right()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, r->left(), y1, r->right(), y2 ); + pReg->numRects++; + pNextRect++; + + r++; + } + return 0; /* lint */ +} + +/*- + *----------------------------------------------------------------------- + * miSubtractO -- + * Overlapping band subtraction. x1 is the left-most point not yet + * checked. + * + * Results: + * None. + * + * Side Effects: + * pReg may have rectangles added to it. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miSubtractO (register QRegionPrivate *pReg, register QRect *r1, QRect *r1End, + register QRect *r2, QRect *r2End, register int y1, register int y2) +{ + register QRect *pNextRect; + register int x1; + + x1 = r1->left(); + + Q_ASSERT(y1<=y2); + pNextRect = pReg->rects.data() + pReg->numRects; + + while ((r1 != r1End) && (r2 != r2End)) + { + if (r2->right() < x1) + { + /* + * Subtrahend missed the boat: go to next subtrahend. + */ + r2++; + } + else if (r2->left() <= x1) + { + /* + * Subtrahend precedes minuend: nuke left edge of minuend. + */ + x1 = r2->right()+1; + if (x1 > r1->right()) + { + /* + * Minuend completely covered: advance to next minuend and + * reset left fence to edge of new minuend. + */ + r1++; + if (r1 != r1End) + x1 = r1->left(); + } + else + { + /* + * Subtrahend now used up since it doesn't extend beyond + * minuend + */ + r2++; + } + } + else if (r2->left() <= r1->right()) + { + /* + * Left part of subtrahend covers part of minuend: add uncovered + * part of minuend to region and skip to next subtrahend. + */ + Q_ASSERT(x1<r2->left()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, r2->left() - 1, y2 ); + pReg->numRects++; + pNextRect++; + + x1 = r2->right() + 1; + if (x1 > r1->right()) + { + /* + * Minuend used up: advance to new... + */ + r1++; + if (r1 != r1End) + x1 = r1->left(); + } + else + { + /* + * Subtrahend used up + */ + r2++; + } + } + else + { + /* + * Minuend used up: add any remaining piece before advancing. + */ + if (r1->right() >= x1) + { + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, r1->right(), y2 ); + pReg->numRects++; + pNextRect++; + } + r1++; + if ( r1 != r1End ) + x1 = r1->left(); + } + } + + /* + * Add remaining minuend rectangles to region. + */ + while (r1 != r1End) + { + Q_ASSERT(x1<=r1->right()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, r1->right(), y2 ); + pReg->numRects++; + pNextRect++; + + r1++; + if (r1 != r1End) + { + x1 = r1->left(); + } + } + return 0; /* lint */ +} + +/*- + *----------------------------------------------------------------------- + * miSubtract -- + * Subtract regS from regM and leave the result in regD. + * S stands for subtrahend, M for minuend and D for difference. + * + * Side Effects: + * regD is overwritten. + * + *----------------------------------------------------------------------- + */ + +static void SubtractRegion(QRegionPrivate *regM, QRegionPrivate *regS, register QRegionPrivate *regD) +{ + /* check for trivial reject */ + if ( (!(regM->numRects)) || (!(regS->numRects)) || + (!EXTENTCHECK(®M->extents, ®S->extents)) ) + { + *regD = *regM; + return; + } + + miRegionOp (regD, regM, regS, (voidProcp) miSubtractO, + (voidProcp) miSubtractNonO1, (voidProcp) NULL); + + /* + * Can't alter newReg's extents before we call miRegionOp because + * it might be one of the source regions and miRegionOp depends + * on the extents of those regions being the unaltered. Besides, this + * way there's no checking against rectangles that will be nuked + * due to coalescing, so we have to examine fewer rectangles. + */ + miSetExtents (regD); +} + +static void XorRegion( QRegionPrivate *sra, QRegionPrivate *srb, QRegionPrivate *dr ) +{ + QRegionPrivate tra, trb; + + SubtractRegion(sra,srb,&tra); + SubtractRegion(srb,sra,&trb); + UnionRegion(&tra,&trb,dr); +} + +/* + * Check to see if two regions are equal + */ +static bool EqualRegion( QRegionPrivate *r1, QRegionPrivate *r2 ) +{ + int i; + + if( r1->numRects != r2->numRects ) return FALSE; + else if( r1->numRects == 0 ) return TRUE; + else if ( r1->extents.left() != r2->extents.left() || + r1->extents.right() != r2->extents.right() || + r1->extents.top() != r2->extents.top() || + r1->extents.bottom() != r2->extents.bottom() ) + return FALSE; + else { + QRect *rr1 = r1->rects.data(); + QRect *rr2 = r2->rects.data(); + for( i=0; i < r1->numRects; i++, rr1++, rr2++ ) { + if ( rr1->left() != rr2->left() || + rr1->right() != rr2->right() || + rr1->top() != rr2->top() || + rr1->bottom() != rr2->bottom() ) + return FALSE; + } + } + return TRUE; +} + +static bool PointInRegion( QRegionPrivate *pRegion, int x, int y ) +{ + int i; + + if (pRegion->numRects == 0) + return FALSE; + if (!pRegion->extents.contains(x, y)) + return FALSE; + for (i=0; i<pRegion->numRects; i++) + { + if (pRegion->rects[i].contains(x, y)) + return TRUE; + } + return FALSE; +} + +static bool RectInRegion(register QRegionPrivate *region, + int rx, int ry, unsigned int rwidth, unsigned int rheight) +{ + register QRect *pbox; + register QRect *pboxEnd; + QRect rect(rx, ry, rwidth, rheight); + register QRect *prect = ▭ + int partIn, partOut; + + /* this is (just) a useful optimization */ + if ((region->numRects == 0) || !EXTENTCHECK(®ion->extents, prect)) + return(RectangleOut); + + partOut = FALSE; + partIn = FALSE; + + /* can stop when both partOut and partIn are TRUE, or we reach prect->y2 */ + for (pbox = region->rects.data(), pboxEnd = pbox + region->numRects; + pbox < pboxEnd; + pbox++) + { + + if (pbox->bottom() < ry) + continue; /* getting up to speed or skipping remainder of band */ + + if (pbox->top() > ry) + { + partOut = TRUE; /* missed part of rectangle above */ + if (partIn || (pbox->top() > prect->bottom())) + break; + ry = pbox->top(); /* x guaranteed to be == prect->x1 */ + } + + if (pbox->right() < rx) + continue; /* not far enough over yet */ + + if (pbox->left() > rx) + { + partOut = TRUE; /* missed part of rectangle to left */ + if (partIn) + break; + } + + if (pbox->left() <= prect->right()) + { + partIn = TRUE; /* definitely overlap */ + if (partOut) + break; + } + + if (pbox->right() >= prect->right()) + { + ry = pbox->bottom() + 1; /* finished with this band */ + if (ry > prect->bottom()) + break; + rx = prect->left(); /* reset x out to left again */ + } else + { + /* + * Because boxes in a band are maximal width, if the first box + * to overlap the rectangle doesn't completely cover it in that + * band, the rectangle must be partially out, since some of it + * will be uncovered in that band. partIn will have been set true + * by now... + */ + break; + } + + } + + return(partIn ? ((ry <= prect->bottom()) ? RectanglePart : RectangleIn) : + RectangleOut); +} +// END OF Region.c extract +// START OF poly.h extract +/* $XConsortium: poly.h,v 1.4 94/04/17 20:22:19 rws Exp $ */ +/************************************************************************ + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ + +/* + * This file contains a few macros to help track + * the edge of a filled object. The object is assumed + * to be filled in scanline order, and thus the + * algorithm used is an extension of Bresenham's line + * drawing algorithm which assumes that y is always the + * major axis. + * Since these pieces of code are the same for any filled shape, + * it is more convenient to gather the library in one + * place, but since these pieces of code are also in + * the inner loops of output primitives, procedure call + * overhead is out of the question. + * See the author for a derivation if needed. + */ + + +/* + * In scan converting polygons, we want to choose those pixels + * which are inside the polygon. Thus, we add .5 to the starting + * x coordinate for both left and right edges. Now we choose the + * first pixel which is inside the pgon for the left edge and the + * first pixel which is outside the pgon for the right edge. + * Draw the left pixel, but not the right. + * + * How to add .5 to the starting x coordinate: + * If the edge is moving to the right, then subtract dy from the + * error term from the general form of the algorithm. + * If the edge is moving to the left, then add dy to the error term. + * + * The reason for the difference between edges moving to the left + * and edges moving to the right is simple: If an edge is moving + * to the right, then we want the algorithm to flip immediately. + * If it is moving to the left, then we don't want it to flip until + * we traverse an entire pixel. + */ +#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \ + int dx; /* local storage */ \ +\ + /* \ + * if the edge is horizontal, then it is ignored \ + * and assumed not to be processed. Otherwise, do this stuff. \ + */ \ + if ((dy) != 0) { \ + xStart = (x1); \ + dx = (x2) - xStart; \ + if (dx < 0) { \ + m = dx / (dy); \ + m1 = m - 1; \ + incr1 = -2 * dx + 2 * (dy) * m1; \ + incr2 = -2 * dx + 2 * (dy) * m; \ + d = 2 * m * (dy) - 2 * dx - 2 * (dy); \ + } else { \ + m = dx / (dy); \ + m1 = m + 1; \ + incr1 = 2 * dx - 2 * (dy) * m1; \ + incr2 = 2 * dx - 2 * (dy) * m; \ + d = -2 * m * (dy) + 2 * dx; \ + } \ + } \ +} + +#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \ + if (m1 > 0) { \ + if (d > 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } else {\ + if (d >= 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } \ +} + + +/* + * This structure contains all of the information needed + * to run the bresenham algorithm. + * The variables may be hardcoded into the declarations + * instead of using this structure to make use of + * register declarations. + */ +typedef struct { + int minor_axis; /* minor axis */ + int d; /* decision variable */ + int m, m1; /* slope and slope+1 */ + int incr1, incr2; /* error increments */ +} BRESINFO; + + +#define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \ + BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \ + bres.m, bres.m1, bres.incr1, bres.incr2) + +#define BRESINCRPGONSTRUCT(bres) \ + BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2) + + + +/* + * These are the data structures needed to scan + * convert regions. Two different scan conversion + * methods are available -- the even-odd method, and + * the winding number method. + * The even-odd rule states that a point is inside + * the polygon if a ray drawn from that point in any + * direction will pass through an odd number of + * path segments. + * By the winding number rule, a point is decided + * to be inside the polygon if a ray drawn from that + * point in any direction passes through a different + * number of clockwise and counter-clockwise path + * segments. + * + * These data structures are adapted somewhat from + * the algorithm in (Foley/Van Dam) for scan converting + * polygons. + * The basic algorithm is to start at the top (smallest y) + * of the polygon, stepping down to the bottom of + * the polygon by incrementing the y coordinate. We + * keep a list of edges which the current scanline crosses, + * sorted by x. This list is called the Active Edge Table (AET) + * As we change the y-coordinate, we update each entry in + * in the active edge table to reflect the edges new xcoord. + * This list must be sorted at each scanline in case + * two edges intersect. + * We also keep a data structure known as the Edge Table (ET), + * which keeps track of all the edges which the current + * scanline has not yet reached. The ET is basically a + * list of ScanLineList structures containing a list of + * edges which are entered at a given scanline. There is one + * ScanLineList per scanline at which an edge is entered. + * When we enter a new edge, we move it from the ET to the AET. + * + * From the AET, we can implement the even-odd rule as in + * (Foley/Van Dam). + * The winding number rule is a little trickier. We also + * keep the EdgeTableEntries in the AET linked by the + * nextWETE (winding EdgeTableEntry) link. This allows + * the edges to be linked just as before for updating + * purposes, but only uses the edges linked by the nextWETE + * link as edges representing spans of the polygon to + * drawn (as with the even-odd rule). + */ + +/* + * for the winding number rule + */ +#define CLOCKWISE 1 +#define COUNTERCLOCKWISE -1 + +typedef struct _EdgeTableEntry { + int ymax; /* ycoord at which we exit this edge. */ + BRESINFO bres; /* Bresenham info to run the edge */ + struct _EdgeTableEntry *next; /* next in the list */ + struct _EdgeTableEntry *back; /* for insertion sort */ + struct _EdgeTableEntry *nextWETE; /* for winding num rule */ + int ClockWise; /* flag for winding number rule */ +} EdgeTableEntry; + + +typedef struct _ScanLineList{ + int scanline; /* the scanline represented */ + EdgeTableEntry *edgelist; /* header node */ + struct _ScanLineList *next; /* next in the list */ +} ScanLineList; + + +typedef struct { + int ymax; /* ymax for the polygon */ + int ymin; /* ymin for the polygon */ + ScanLineList scanlines; /* header node */ +} EdgeTable; + + +/* + * Here is a struct to help with storage allocation + * so we can allocate a big chunk at a time, and then take + * pieces from this heap when we need to. + */ +#define SLLSPERBLOCK 25 + +typedef struct _ScanLineListBlock { + ScanLineList SLLs[SLLSPERBLOCK]; + struct _ScanLineListBlock *next; +} ScanLineListBlock; + + + +/* + * + * a few macros for the inner loops of the fill code where + * performance considerations don't allow a procedure call. + * + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The winding number rule is in effect, so we must notify + * the caller when the edge has been removed so he + * can reorder the Winding Active Edge Table. + */ +#define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + fixWAET = 1; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres) \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} + + +/* + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The even-odd rule is in effect. + */ +#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres) \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} +// END OF poly.h extract +// START OF PolyReg.c extract +/* $XConsortium: PolyReg.c,v 11.23 94/11/17 21:59:37 converse Exp $ */ +/************************************************************************ + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ +/* $XFree86: xc/lib/X11/PolyReg.c,v 1.1.1.2.8.2 1998/10/04 15:22:49 hohndel Exp $ */ + +#define LARGE_COORDINATE 1000000 +#define SMALL_COORDINATE -LARGE_COORDINATE + +/* + * InsertEdgeInET + * + * Insert the given edge into the edge table. + * First we must find the correct bucket in the + * Edge table, then find the right slot in the + * bucket. Finally, we can insert it. + * + */ +static void +InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE, int scanline, + ScanLineListBlock **SLLBlock, int *iSLLBlock) +{ + register EdgeTableEntry *start, *prev; + register ScanLineList *pSLL, *pPrevSLL; + ScanLineListBlock *tmpSLLBlock; + + /* + * find the right bucket to put the edge into + */ + pPrevSLL = &ET->scanlines; + pSLL = pPrevSLL->next; + while (pSLL && (pSLL->scanline < scanline)) + { + pPrevSLL = pSLL; + pSLL = pSLL->next; + } + + /* + * reassign pSLL (pointer to ScanLineList) if necessary + */ + if ((!pSLL) || (pSLL->scanline > scanline)) + { + if (*iSLLBlock > SLLSPERBLOCK-1) + { + tmpSLLBlock = + (ScanLineListBlock *)malloc(sizeof(ScanLineListBlock)); + (*SLLBlock)->next = tmpSLLBlock; + tmpSLLBlock->next = (ScanLineListBlock *)NULL; + *SLLBlock = tmpSLLBlock; + *iSLLBlock = 0; + } + pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]); + + pSLL->next = pPrevSLL->next; + pSLL->edgelist = (EdgeTableEntry *)NULL; + pPrevSLL->next = pSLL; + } + pSLL->scanline = scanline; + + /* + * now insert the edge in the right bucket + */ + prev = (EdgeTableEntry *)NULL; + start = pSLL->edgelist; + while (start && (start->bres.minor_axis < ETE->bres.minor_axis)) + { + prev = start; + start = start->next; + } + ETE->next = start; + + if (prev) + prev->next = ETE; + else + pSLL->edgelist = ETE; +} + +/* + * CreateEdgeTable + * + * This routine creates the edge table for + * scan converting polygons. + * The Edge Table (ET) looks like: + * + * EdgeTable + * -------- + * | ymax | ScanLineLists + * |scanline|-->------------>-------------->... + * -------- |scanline| |scanline| + * |edgelist| |edgelist| + * --------- --------- + * | | + * | | + * V V + * list of ETEs list of ETEs + * + * where ETE is an EdgeTableEntry data structure, + * and there is one ScanLineList per scanline at + * which an edge is initially entered. + * + */ + +static void +CreateETandAET(register int count, register QPoint *pts, + EdgeTable *ET, EdgeTableEntry *AET, register EdgeTableEntry *pETEs, + ScanLineListBlock *pSLLBlock) +{ + register QPoint *top, *bottom; + register QPoint *PrevPt, *CurrPt; + int iSLLBlock = 0; + int dy; + + if (count < 2) return; + + /* + * initialize the Active Edge Table + */ + AET->next = (EdgeTableEntry *)NULL; + AET->back = (EdgeTableEntry *)NULL; + AET->nextWETE = (EdgeTableEntry *)NULL; + AET->bres.minor_axis = SMALL_COORDINATE; + + /* + * initialize the Edge Table. + */ + ET->scanlines.next = (ScanLineList *)NULL; + ET->ymax = SMALL_COORDINATE; + ET->ymin = LARGE_COORDINATE; + pSLLBlock->next = (ScanLineListBlock *)NULL; + + PrevPt = &pts[count-1]; + + /* + * for each vertex in the array of points. + * In this loop we are dealing with two vertices at + * a time -- these make up one edge of the polygon. + */ + while (count--) + { + CurrPt = pts++; + + /* + * find out which point is above and which is below. + */ + if (PrevPt->y() > CurrPt->y() ) + { + bottom = PrevPt, top = CurrPt; + pETEs->ClockWise = 0; + } + else + { + bottom = CurrPt, top = PrevPt; + pETEs->ClockWise = 1; + } + + /* + * don't add horizontal edges to the Edge table. + */ + if ( bottom->y() != top->y() ) + { + pETEs->ymax = bottom->y()-1; /* -1 so we don't get last scanline */ + + /* + * initialize integer edge algorithm + */ + dy = bottom->y() - top->y(); + BRESINITPGONSTRUCT(dy, top->x(), bottom->x(), pETEs->bres) + + InsertEdgeInET(ET, pETEs, top->y(), &pSLLBlock, &iSLLBlock); + + if (PrevPt->y() > ET->ymax) + ET->ymax = PrevPt->y(); + if (PrevPt->y() < ET->ymin) + ET->ymin = PrevPt->y(); + pETEs++; + } + + PrevPt = CurrPt; + } +} + +/* + * loadAET + * + * This routine moves EdgeTableEntries from the + * EdgeTable into the Active Edge Table, + * leaving them sorted by smaller x coordinate. + * + */ + +static void +loadAET(register EdgeTableEntry *AET, register EdgeTableEntry *ETEs) +{ + register EdgeTableEntry *pPrevAET; + register EdgeTableEntry *tmp; + + pPrevAET = AET; + AET = AET->next; + while (ETEs) + { + while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis)) + { + pPrevAET = AET; + AET = AET->next; + } + tmp = ETEs->next; + ETEs->next = AET; + if (AET) + AET->back = ETEs; + ETEs->back = pPrevAET; + pPrevAET->next = ETEs; + pPrevAET = ETEs; + + ETEs = tmp; + } +} + +/* + * computeWAET + * + * This routine links the AET by the + * nextWETE (winding EdgeTableEntry) link for + * use by the winding number rule. The final + * Active Edge Table (AET) might look something + * like: + * + * AET + * ---------- --------- --------- + * |ymax | |ymax | |ymax | + * | ... | |... | |... | + * |next |->|next |->|next |->... + * |nextWETE| |nextWETE| |nextWETE| + * --------- --------- ^-------- + * | | | + * V-------------------> V---> ... + * + */ +static void +computeWAET(register EdgeTableEntry *AET) +{ + register EdgeTableEntry *pWETE; + register int inside = 1; + register int isInside = 0; + + AET->nextWETE = (EdgeTableEntry *)NULL; + pWETE = AET; + AET = AET->next; + while (AET) + { + if (AET->ClockWise) + isInside++; + else + isInside--; + + if ((!inside && !isInside) || + ( inside && isInside)) + { + pWETE->nextWETE = AET; + pWETE = AET; + inside = !inside; + } + AET = AET->next; + } + pWETE->nextWETE = (EdgeTableEntry *)NULL; +} + +/* + * InsertionSort + * + * Just a simple insertion sort using + * pointers and back pointers to sort the Active + * Edge Table. + * + */ + +static int +InsertionSort(register EdgeTableEntry *AET) +{ + register EdgeTableEntry *pETEchase; + register EdgeTableEntry *pETEinsert; + register EdgeTableEntry *pETEchaseBackTMP; + register int changed = 0; + + AET = AET->next; + while (AET) + { + pETEinsert = AET; + pETEchase = AET; + while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis) + pETEchase = pETEchase->back; + + AET = AET->next; + if (pETEchase != pETEinsert) + { + pETEchaseBackTMP = pETEchase->back; + pETEinsert->back->next = AET; + if (AET) + AET->back = pETEinsert->back; + pETEinsert->next = pETEchase; + pETEchase->back->next = pETEinsert; + pETEchase->back = pETEinsert; + pETEinsert->back = pETEchaseBackTMP; + changed = 1; + } + } + return(changed); +} + +/* + * Clean up our act. + */ +static void +FreeStorage(register ScanLineListBlock *pSLLBlock) +{ + register ScanLineListBlock *tmpSLLBlock; + + while (pSLLBlock) + { + tmpSLLBlock = pSLLBlock->next; + free((char *)pSLLBlock); + pSLLBlock = tmpSLLBlock; + } +} + +/* + * Create an array of rectangles from a list of points. + * If indeed these things (POINTS, RECTS) are the same, + * then this proc is still needed, because it allocates + * storage for the array, which was allocated on the + * stack by the calling procedure. + * + */ +static int PtsToRegion(register int numFullPtBlocks, register int iCurPtBlock, + POINTBLOCK *FirstPtBlock, QRegionPrivate *reg) +{ + register QRect *rects; + register QPoint *pts; + register POINTBLOCK *CurPtBlock; + register int i; + register QRect *extents; + register int numRects; + + extents = ®->extents; + + numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1; + + reg->rects.resize(numRects); + + CurPtBlock = FirstPtBlock; + rects = reg->rects.data() - 1; + numRects = 0; + extents->setLeft( INT_MAX ); + extents->setRight( INT_MIN ); + + for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) { + /* the loop uses 2 points per iteration */ + i = NUMPTSTOBUFFER >> 1; + if (!numFullPtBlocks) + i = iCurPtBlock >> 1; + for (pts = CurPtBlock->pts; i--; pts += 2) { + if ( pts->x() == pts[1].x() ) + continue; + if (numRects && pts->x() == rects->left() && pts->y() == rects->bottom() + 1 && + pts[1].x() == rects->right() && + (numRects == 1 || rects[-1].top() != rects->top()) && + (i && pts[2].y() > pts[1].y() )) { + rects->setBottom( pts[1].y() ); + continue; + } + numRects++; + rects++; + qt_setCoords( rects, pts->x(), pts->y(), pts[1].x() - 1, pts[1].y() ); + if (rects->left() < extents->left()) + extents->setLeft( rects->left() ); + if (rects->right() > extents->right()) + extents->setRight( rects->right() ); + } + CurPtBlock = CurPtBlock->next; + } + + if (numRects) { + extents->setTop( reg->rects[0].top() ); + extents->setBottom( rects->bottom() ); + } else { + qt_setCoords(extents, 0, 0, 0, 0); + } + reg->numRects = numRects; + + return(TRUE); +} + +/* + * polytoregion + * + * Scan converts a polygon by returning a run-length + * encoding of the resultant bitmap -- the run-length + * encoding is in the form of an array of rectangles. + */ +static QRegionPrivate *PolygonRegion(QPoint *Pts, int Count, int rule) + //Point *Pts; /* the pts */ + //int Count; /* number of pts */ + //int rule; /* winding rule */ +{ + QRegionPrivate *region; + register EdgeTableEntry *pAET; /* Active Edge Table */ + register int y; /* current scanline */ + register int iPts = 0; /* number of pts in buffer */ + register EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/ + register ScanLineList *pSLL; /* current scanLineList */ + register QPoint *pts; /* output buffer */ + EdgeTableEntry *pPrevAET; /* ptr to previous AET */ + EdgeTable ET; /* header node for ET */ + EdgeTableEntry AET; /* header node for AET */ + EdgeTableEntry *pETEs; /* EdgeTableEntries pool */ + ScanLineListBlock SLLBlock; /* header for scanlinelist */ + int fixWAET = FALSE; + POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */ + POINTBLOCK *tmpPtBlock; + int numFullPtBlocks = 0; + + if ( !(region = new QRegionPrivate) ) + return 0; + + /* special case a rectangle */ + pts = Pts; + if (((Count == 4) || + ((Count == 5) && (pts[4].x() == pts[0].x() ) && (pts[4].y() == pts[0].y() ))) && + (((pts[0].y() == pts[1].y()) && + (pts[1].x() == pts[2].x()) && + (pts[2].y() == pts[3].y()) && + (pts[3].x() == pts[0].x())) || + ((pts[0].x() == pts[1].x()) && + (pts[1].y() == pts[2].y()) && + (pts[2].x() == pts[3].x()) && + (pts[3].y() == pts[0].y())))) { + region->extents.setLeft( QMIN(pts[0].x(), pts[2].x()) ); + region->extents.setTop( QMIN(pts[0].y(), pts[2].y()) ); + region->extents.setRight( QMAX(pts[0].x(), pts[2].x()) ); + region->extents.setBottom( QMAX(pts[0].y(), pts[2].y()) ); + if ((region->extents.left() <= region->extents.right()) && + (region->extents.top() <= region->extents.bottom())) { + region->numRects = 1; + region->rects.resize(1); + region->rects[0] = region->extents; + } + return region; + } + + if (! (pETEs = (EdgeTableEntry *) + malloc((unsigned) (sizeof(EdgeTableEntry) * Count)))) + return 0; + + pts = FirstPtBlock.pts; + CreateETandAET(Count, Pts, &ET, &AET, pETEs, &SLLBlock); + pSLL = ET.scanlines.next; + curPtBlock = &FirstPtBlock; + + if (rule == EvenOddRule) { + /* + * for each scanline + */ + for (y = ET.ymin; y < ET.ymax; y++) { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL != NULL && y == pSLL->scanline) { + loadAET(&AET, pSLL->edgelist); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + + /* + * for each active edge + */ + while (pAET) { + pts->setX( pAET->bres.minor_axis ), pts->setY( y ); + pts++, iPts++; + + /* + * send out the buffer + */ + if (iPts == NUMPTSTOBUFFER) { + tmpPtBlock = (POINTBLOCK *)malloc(sizeof(POINTBLOCK)); + curPtBlock->next = tmpPtBlock; + curPtBlock = tmpPtBlock; + pts = curPtBlock->pts; + numFullPtBlocks++; + iPts = 0; + } + EVALUATEEDGEEVENODD(pAET, pPrevAET, y) + } + (void) InsertionSort(&AET); + } + } + else { + /* + * for each scanline + */ + for (y = ET.ymin; y < ET.ymax; y++) { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL != NULL && y == pSLL->scanline) { + loadAET(&AET, pSLL->edgelist); + computeWAET(&AET); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + pWETE = pAET; + + /* + * for each active edge + */ + while (pAET) { + /* + * add to the buffer only those edges that + * are in the Winding active edge table. + */ + if (pWETE == pAET) { + pts->setX( pAET->bres.minor_axis), pts->setY( y ); + pts++, iPts++; + + /* + * send out the buffer + */ + if (iPts == NUMPTSTOBUFFER) { + tmpPtBlock = (POINTBLOCK *)malloc(sizeof(POINTBLOCK)); + curPtBlock->next = tmpPtBlock; + curPtBlock = tmpPtBlock; + pts = curPtBlock->pts; + numFullPtBlocks++; iPts = 0; + } + pWETE = pWETE->nextWETE; + } + EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) + } + + /* + * recompute the winding active edge table if + * we just resorted or have exited an edge. + */ + if (InsertionSort(&AET) || fixWAET) { + computeWAET(&AET); + fixWAET = FALSE; + } + } + } + FreeStorage(SLLBlock.next); + (void) PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, region); + for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) { + tmpPtBlock = curPtBlock->next; + free((char *)curPtBlock); + curPtBlock = tmpPtBlock; + } + free((char *)pETEs); + return region; +} +// END OF PolyReg.c extract + +QRegionPrivate *qt_bitmapToRegion(const QBitmap& bitmap) +{ + QImage image = bitmap.convertToImage(); + + QRegionPrivate *region = new QRegionPrivate; + QRect xr; + +#define AddSpan \ + { \ + qt_setCoords( &xr, prev1, y, x-1, y ); \ + UnionRectWithRegion( &xr, region, region ); \ + } + + const int zero=0; + bool little = image.bitOrder() == QImage::LittleEndian; + + int x, y; + for (y=0; y<image.height(); y++) { + uchar *line = image.scanLine(y); + int w = image.width(); + uchar all=zero; + int prev1 = -1; + for (x=0; x<w; ) { + uchar byte = line[x/8]; + if ( x>w-8 || byte!=all ) { + if ( little ) { + for ( int b=8; b>0 && x<w; b-- ) { + if ( !(byte&0x01) == !all ) { + // More of the same + } else { + // A change. + if ( all!=zero ) { + AddSpan + all = zero; + } else { + prev1 = x; + all = ~zero; + } + } + byte >>= 1; + x++; + } + } else { + for ( int b=8; b>0 && x<w; b-- ) { + if ( !(byte&0x80) == !all ) { + // More of the same + } else { + // A change. + if ( all!=zero ) { + AddSpan + all = zero; + } else { + prev1 = x; + all = ~zero; + } + } + byte <<= 1; + x++; + } + } + } else { + x+=8; + } + } + if ( all != zero ) { + AddSpan + } + } + + return region; +} + +// NOT REVISED + +static QRegion *empty_region = 0; + +static void cleanup_empty_region() +{ + delete empty_region; + empty_region = 0; +} + + +/*! + Constructs a null region. + + \sa isNull() +*/ + +QRegion::QRegion() +{ + if ( !empty_region ) { // avoid too many allocs + qAddPostRoutine( cleanup_empty_region ); + empty_region = new QRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); +} + +/*! \internal + Internal constructor that creates a null region. +*/ + +QRegion::QRegion( bool is_null ) +{ + data = new QRegionData; + Q_CHECK_PTR( data ); + data->region = new QRegionPrivate; + data->is_null = is_null; + data->rgn = 0; + data->xrectangles = 0; +} + +/*! + \overload + + Create a region based on the rectange \a r with region type \a t. + + If the rectangle is invalid a null region will be created. + + \sa QRegion::RegionType +*/ + +QRegion::QRegion( const QRect &r, RegionType t ) +{ + if ( r.isEmpty() ) { + if ( !empty_region ) { // avoid too many allocs + qAddPostRoutine( cleanup_empty_region ); + empty_region = new QRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); + } else { + data = new QRegionData; + Q_CHECK_PTR( data ); + data->is_null = FALSE; + data->rgn = 0; + data->xrectangles = 0; + if ( t == Rectangle ) { // rectangular region + data->region = new QRegionPrivate( r ); + } else if ( t == Ellipse ) { // elliptic region + QPointArray a; + a.makeEllipse( r.x(), r.y(), r.width(), r.height() ); + data->region = PolygonRegion( (QPoint*)a.data(), a.size(), + EvenOddRule ); + } + } +} + + +/*! + Constructs a polygon region from the point array \a a. + + If \a winding is TRUE, the polygon region is filled using the + winding algorithm, otherwise the default even-odd fill algorithm + is used. + + This constructor may create complex regions that will slow down + painting when used. +*/ + +QRegion::QRegion( const QPointArray &a, bool winding ) +{ + if (a.size() > 2) { + data = new QRegionData; + Q_CHECK_PTR( data ); + data->is_null = FALSE; + data->rgn = 0; + data->xrectangles = 0; + data->region = PolygonRegion( (QPoint*)a.data(), a.size(), + winding ? WindingRule : EvenOddRule ); + } else { + if ( !empty_region ) { + qAddPostRoutine( cleanup_empty_region ); + empty_region = new QRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); + } +} + + +/*! + Constructs a new region which is equal to region \a r. +*/ + +QRegion::QRegion( const QRegion &r ) +{ + data = r.data; + data->ref(); +} + + +/*! + Constructs a region from the bitmap \a bm. + + The resulting region consists of the pixels in bitmap \a bm that + are \c color1, as if each pixel was a 1 by 1 rectangle. + + This constructor may create complex regions that will slow down + painting when used. Note that drawing masked pixmaps can be done + much faster using QPixmap::setMask(). +*/ +QRegion::QRegion( const QBitmap & bm ) +{ + if ( bm.isNull() ) { + if ( !empty_region ) { // avoid too many allocs + qAddPostRoutine( cleanup_empty_region ); + empty_region = new QRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); + } else { + data = new QRegionData; + Q_CHECK_PTR( data ); + data->is_null = FALSE; + data->rgn = 0; + data->xrectangles = 0; + data->region = qt_bitmapToRegion(bm); + } +} + +/*! + Destroys the region. +*/ + +QRegion::~QRegion() +{ + if ( data->deref() ) { + delete data->region; + if ( data->rgn ) + XDestroyRegion( data->rgn ); + if ( data->xrectangles ) + free( data->xrectangles ); + delete data; + } +} + + +/*! + Assigns \a r to this region and returns a reference to the region. +*/ + +QRegion &QRegion::operator=( const QRegion &r ) +{ + r.data->ref(); // beware of r = r + if ( data->deref() ) { + delete data->region; + if ( data->rgn ) + XDestroyRegion( data->rgn ); + if ( data->xrectangles ) + free( data->xrectangles ); + delete data; + } + data = r.data; + return *this; +} + + +/*! + Returns a \link shclass.html deep copy\endlink of the region. + + \sa detach() +*/ + +QRegion QRegion::copy() const +{ + QRegion r( data->is_null ); + *r.data->region = *data->region; + return r; +} + +/*! + Returns TRUE if the region is a null region; otherwise returns + FALSE. + + A null region is a region that has not been initialized. A null + region is always empty. + + \sa isEmpty() +*/ + +bool QRegion::isNull() const +{ + return data->is_null; +} + + +/*! + Returns TRUE if the region is empty; otherwise returns FALSE. An + empty region is a region that contains no points. + + Example: + \code + QRegion r1( 10, 10, 20, 20 ); + QRegion r2( 40, 40, 20, 20 ); + QRegion r3; + r1.isNull(); // FALSE + r1.isEmpty(); // FALSE + r3.isNull(); // TRUE + r3.isEmpty(); // TRUE + r3 = r1.intersect( r2 ); // r3 = intersection of r1 and r2 + r3.isNull(); // FALSE + r3.isEmpty(); // TRUE + r3 = r1.unite( r2 ); // r3 = union of r1 and r2 + r3.isNull(); // FALSE + r3.isEmpty(); // FALSE + \endcode + + \sa isNull() +*/ + +bool QRegion::isEmpty() const +{ + return data->is_null || ( data->region->numRects == 0 ); +} + + +/*! + Returns TRUE if the region contains the point \a p; otherwise + returns FALSE. +*/ + +bool QRegion::contains( const QPoint &p ) const +{ + return PointInRegion( data->region, p.x(), p.y() ); +} + +/*! + \overload + + Returns TRUE if the region overlaps the rectangle \a r; otherwise + returns FALSE. +*/ + +bool QRegion::contains( const QRect &r ) const +{ + return RectInRegion( data->region, r.left(), r.top(), + r.width(), r.height() ) != RectangleOut; +} + + +/*! + Translates (moves) the region \a dx along the X axis and \a dy + along the Y axis. +*/ + +void QRegion::translate( int dx, int dy ) +{ + if ( empty_region && data == empty_region->data ) + return; + detach(); + OffsetRegion( data->region, dx, dy ); + if ( data->xrectangles ) { + free( data->xrectangles ); + data->xrectangles = 0; + } +} + + +/*! + Returns a region which is the union of this region and \a r. + + \img runion.png Region Union + + The figure shows the union of two elliptical regions. +*/ + +QRegion QRegion::unite( const QRegion &r ) const +{ + QRegion result( FALSE ); + UnionRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns a region which is the intersection of this region and \a r. + + \img rintersect.png Region Intersection + + The figure shows the intersection of two elliptical regions. +*/ + +QRegion QRegion::intersect( const QRegion &r ) const +{ + QRegion result( FALSE ); + IntersectRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns a region which is \a r subtracted from this region. + + \img rsubtract.png Region Subtraction + + The figure shows the result when the ellipse on the right is + subtracted from the ellipse on the left. (\c left-right ) +*/ + +QRegion QRegion::subtract( const QRegion &r ) const +{ + QRegion result( FALSE ); + SubtractRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns a region which is the exclusive or (XOR) of this region + and \a r. + + \img rxor.png Region XORed + + The figure shows the exclusive or of two elliptical regions. +*/ + +QRegion QRegion::eor( const QRegion &r ) const +{ + QRegion result( FALSE ); + XorRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns the bounding rectangle of this region. An empty region + gives a rectangle that is QRect::isNull(). +*/ + +QRect QRegion::boundingRect() const +{ + return data->region->extents; +} + + +/*! + Returns an array of non-overlapping rectangles that make up the + region. + + The union of all the rectangles is equal to the original region. +*/ + +QMemArray<QRect> QRegion::rects() const +{ + QMemArray<QRect> rects; + rects.duplicate( data->region->rects, data->region->numRects ); + return rects; +} + +/*! + Sets the region to be the given set of rectangles. The rectangles + \e must be optimal Y-X sorted bands as follows: + <ul> + <li> The rectangles must not intersect + <li> All rectangles with a given top coordinate must have the same height. + <li> No two rectangles may abut horizontally (they should be combined + into a single wider rectangle in that case). + <li> The rectangles must be sorted ascendingly by Y as the major sort key + and X as the minor sort key. + </ul> + \internal + Only some platforms have that restriction (QWS and X11). +*/ +void QRegion::setRects( const QRect *rects, int num ) +{ + *this = QRegion( FALSE ); + if ( !rects || (num == 1 && rects->isEmpty()) ) + num = 0; + + data->region->rects.duplicate( rects, num ); + data->region->numRects = num; + if ( num == 0 ) { + data->region->extents = QRect(); + } else { + int left = INT_MAX, right = INT_MIN, top = INT_MAX, bottom = INT_MIN; + int i; + for ( i = 0; i < num; i++ ) { + left = QMIN( rects[i].left(), left ); + right = QMAX( rects[i].right(), right ); + top = QMIN( rects[i].top(), top ); + bottom = QMAX( rects[i].bottom(), bottom ); + } + data->region->extents = QRect( QPoint(left, top), QPoint(right, bottom) ); + } +} + +/*! + Returns TRUE if the region is equal to \a r; otherwise returns + FALSE. +*/ + +bool QRegion::operator==( const QRegion &r ) const +{ + return data == r.data ? + TRUE : EqualRegion( data->region, r.data->region ); +} + +/*! + \fn bool QRegion::operator!=( const QRegion &r ) const + + Returns TRUE if the region is different from \a r; otherwise + returns FALSE. +*/ + +/* + This is how X represents regions internally. +*/ + +struct BOX { + short x1, x2, y1, y2; +}; + +struct _XRegion { + long size; + long numRects; + BOX *rects; + BOX extents; +}; + + +void QRegion::updateX11Region() const +{ + data->rgn = XCreateRegion(); + + for( int i = 0; i < data->region->numRects; i++ ) { + XRectangle r; + const QRect &rect = data->region->rects[i]; + r.x = QMAX( SHRT_MIN, rect.x() ); + r.y = QMAX( SHRT_MIN, rect.y() ); + r.width = QMIN( USHRT_MAX, rect.width() ); + r.height = QMIN( USHRT_MAX, rect.height() ); + XUnionRectWithRegion( &r, data->rgn, data->rgn ); + } +} + + +void *QRegion::clipRectangles( int &num ) const +{ + if ( !data->xrectangles ) { + XRectangle *r = (XRectangle *) malloc( data->region->numRects * sizeof( XRectangle ) ); + data->xrectangles = r; + for( int i = 0; i < data->region->numRects; i++ ) { + const QRect &rect = data->region->rects[i]; + r->x = QMAX( SHRT_MIN, rect.x() ); + r->y = QMAX( SHRT_MIN, rect.y() ); + r->width = QMIN( USHRT_MAX, rect.width() ); + r->height = QMIN( USHRT_MAX, rect.height() ); + r++; + } + } + num = data->region->numRects; + return data->xrectangles; +} diff --git a/src/kernel/qrichtext.cpp b/src/kernel/qrichtext.cpp new file mode 100644 index 0000000..eb43f3c --- /dev/null +++ b/src/kernel/qrichtext.cpp @@ -0,0 +1,8258 @@ +/**************************************************************************** +** +** Implementation of the internal Qt classes dealing with rich text +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qrichtext_p.h" + +#ifndef QT_NO_RICHTEXT + + +#include "qstringlist.h" +#include "qfont.h" +#include "qtextstream.h" +#include "qfile.h" +#include "qapplication.h" +#include "qmap.h" +#include "qfileinfo.h" +#include "qstylesheet.h" +#include "qmime.h" +#include "qimage.h" +#include "qdragobject.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qdrawutil.h" +#include "qcursor.h" +#include "qptrstack.h" +#include "qptrdict.h" +#include "qstyle.h" +#include "qcleanuphandler.h" +#include "qtextengine_p.h" +#include <private/qunicodetables_p.h> + +#include <stdlib.h> + +static QTextCursor* richTextExportStart = 0; +static QTextCursor* richTextExportEnd = 0; + +class QTextFormatCollection; + +const int border_tolerance = 2; + +#ifdef Q_WS_WIN +#include "qt_windows.h" +#endif + +#define QChar_linesep QChar(0x2028U) + +static inline bool is_printer( QPainter *p ) +{ + if ( !p || !p->device() ) + return FALSE; + return p->device()->devType() == QInternal::Printer; +} + +static inline int scale( int value, QPainter *painter ) +{ + if ( is_printer( painter ) ) { + QPaintDeviceMetrics metrics( painter->device() ); +#if defined(Q_WS_X11) + value = value * metrics.logicalDpiY() / + QPaintDevice::x11AppDpiY( painter->device()->x11Screen() ); +#elif defined (Q_WS_WIN) + HDC hdc = GetDC( 0 ); + int gdc = GetDeviceCaps( hdc, LOGPIXELSY ); + if ( gdc ) + value = value * metrics.logicalDpiY() / gdc; + ReleaseDC( 0, hdc ); +#elif defined (Q_WS_MAC) + value = value * metrics.logicalDpiY() / 75; // ##### FIXME +#elif defined (Q_WS_QWS) + value = value * metrics.logicalDpiY() / 75; +#endif + } + return value; +} + + +inline bool isBreakable( QTextString *string, int pos ) +{ + if (string->at(pos).nobreak) + return FALSE; + return (pos < string->length()-1 && string->at(pos+1).softBreak); +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +void QTextCommandHistory::addCommand( QTextCommand *cmd ) +{ + if ( current < (int)history.count() - 1 ) { + QPtrList<QTextCommand> commands; + commands.setAutoDelete( FALSE ); + + for( int i = 0; i <= current; ++i ) { + commands.insert( i, history.at( 0 ) ); + history.take( 0 ); + } + + commands.append( cmd ); + history.clear(); + history = commands; + history.setAutoDelete( TRUE ); + } else { + history.append( cmd ); + } + + if ( (int)history.count() > steps ) + history.removeFirst(); + else + ++current; +} + +QTextCursor *QTextCommandHistory::undo( QTextCursor *c ) +{ + if ( current > -1 ) { + QTextCursor *c2 = history.at( current )->unexecute( c ); + --current; + return c2; + } + return 0; +} + +QTextCursor *QTextCommandHistory::redo( QTextCursor *c ) +{ + if ( current > -1 ) { + if ( current < (int)history.count() - 1 ) { + ++current; + return history.at( current )->execute( c ); + } + } else { + if ( history.count() > 0 ) { + ++current; + return history.at( current )->execute( c ); + } + } + return 0; +} + +bool QTextCommandHistory::isUndoAvailable() +{ + return current > -1; +} + +bool QTextCommandHistory::isRedoAvailable() +{ + return current > -1 && current < (int)history.count() - 1 || current == -1 && history.count() > 0; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextDeleteCommand::QTextDeleteCommand( QTextDocument *d, int i, int idx, const QMemArray<QTextStringChar> &str, + const QByteArray& oldStyleInfo ) + : QTextCommand( d ), id( i ), index( idx ), parag( 0 ), text( str ), styleInformation( oldStyleInfo ) +{ + for ( int j = 0; j < (int)text.size(); ++j ) { + if ( text[ j ].format() ) + text[ j ].format()->addRef(); + } +} + +QTextDeleteCommand::QTextDeleteCommand( QTextParagraph *p, int idx, const QMemArray<QTextStringChar> &str ) + : QTextCommand( 0 ), id( -1 ), index( idx ), parag( p ), text( str ) +{ + for ( int i = 0; i < (int)text.size(); ++i ) { + if ( text[ i ].format() ) + text[ i ].format()->addRef(); + } +} + +QTextDeleteCommand::~QTextDeleteCommand() +{ + for ( int i = 0; i < (int)text.size(); ++i ) { + if ( text[ i ].format() ) + text[ i ].format()->removeRef(); + } + text.resize( 0 ); +} + +QTextCursor *QTextDeleteCommand::execute( QTextCursor *c ) +{ + QTextParagraph *s = doc ? doc->paragAt( id ) : parag; + if ( !s ) { + qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParagraph()->paragId() ); + return 0; + } + + cursor.setParagraph( s ); + cursor.setIndex( index ); + int len = text.size(); + if ( c ) + *c = cursor; + if ( doc ) { + doc->setSelectionStart( QTextDocument::Temp, cursor ); + for ( int i = 0; i < len; ++i ) + cursor.gotoNextLetter(); + doc->setSelectionEnd( QTextDocument::Temp, cursor ); + doc->removeSelectedText( QTextDocument::Temp, &cursor ); + if ( c ) + *c = cursor; + } else { + s->remove( index, len ); + } + + return c; +} + +QTextCursor *QTextDeleteCommand::unexecute( QTextCursor *c ) +{ + QTextParagraph *s = doc ? doc->paragAt( id ) : parag; + if ( !s ) { + qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParagraph()->paragId() ); + return 0; + } + + cursor.setParagraph( s ); + cursor.setIndex( index ); + QString str = QTextString::toString( text ); + cursor.insert( str, TRUE, &text ); + if ( c ) + *c = cursor; + cursor.setParagraph( s ); + cursor.setIndex( index ); + +#ifndef QT_NO_DATASTREAM + if ( !styleInformation.isEmpty() ) { + QDataStream styleStream( styleInformation, IO_ReadOnly ); + int num; + styleStream >> num; + QTextParagraph *p = s; + while ( num-- && p ) { + p->readStyleInformation( styleStream ); + p = p->next(); + } + } +#endif + s = cursor.paragraph(); + while ( s ) { + s->format(); + s->setChanged( TRUE ); + if ( s == c->paragraph() ) + break; + s = s->next(); + } + + return &cursor; +} + +QTextFormatCommand::QTextFormatCommand( QTextDocument *d, int sid, int sidx, int eid, int eidx, + const QMemArray<QTextStringChar> &old, QTextFormat *f, int fl ) + : QTextCommand( d ), startId( sid ), startIndex( sidx ), endId( eid ), endIndex( eidx ), format( f ), oldFormats( old ), flags( fl ) +{ + format = d->formatCollection()->format( f ); + for ( int j = 0; j < (int)oldFormats.size(); ++j ) { + if ( oldFormats[ j ].format() ) + oldFormats[ j ].format()->addRef(); + } +} + +QTextFormatCommand::~QTextFormatCommand() +{ + format->removeRef(); + for ( int j = 0; j < (int)oldFormats.size(); ++j ) { + if ( oldFormats[ j ].format() ) + oldFormats[ j ].format()->removeRef(); + } +} + +QTextCursor *QTextFormatCommand::execute( QTextCursor *c ) +{ + QTextParagraph *sp = doc->paragAt( startId ); + QTextParagraph *ep = doc->paragAt( endId ); + if ( !sp || !ep ) + return c; + + QTextCursor start( doc ); + start.setParagraph( sp ); + start.setIndex( startIndex ); + QTextCursor end( doc ); + end.setParagraph( ep ); + end.setIndex( endIndex ); + + doc->setSelectionStart( QTextDocument::Temp, start ); + doc->setSelectionEnd( QTextDocument::Temp, end ); + doc->setFormat( QTextDocument::Temp, format, flags ); + doc->removeSelection( QTextDocument::Temp ); + if ( endIndex == ep->length() ) + end.gotoLeft(); + *c = end; + return c; +} + +QTextCursor *QTextFormatCommand::unexecute( QTextCursor *c ) +{ + QTextParagraph *sp = doc->paragAt( startId ); + QTextParagraph *ep = doc->paragAt( endId ); + if ( !sp || !ep ) + return 0; + + int idx = startIndex; + int fIndex = 0; + while ( fIndex < int(oldFormats.size()) ) { + if ( oldFormats.at( fIndex ).c == '\n' ) { + if ( idx > 0 ) { + if ( idx < sp->length() && fIndex > 0 ) + sp->setFormat( idx, 1, oldFormats.at( fIndex - 1 ).format() ); + if ( sp == ep ) + break; + sp = sp->next(); + idx = 0; + } + fIndex++; + } + if ( oldFormats.at( fIndex ).format() ) + sp->setFormat( idx, 1, oldFormats.at( fIndex ).format() ); + idx++; + fIndex++; + if ( fIndex >= (int)oldFormats.size() ) + break; + if ( idx >= sp->length() ) { + if ( sp == ep ) + break; + sp = sp->next(); + idx = 0; + } + } + + QTextCursor end( doc ); + end.setParagraph( ep ); + end.setIndex( endIndex ); + if ( endIndex == ep->length() ) + end.gotoLeft(); + *c = end; + return c; +} + +QTextStyleCommand::QTextStyleCommand( QTextDocument *d, int fParag, int lParag, const QByteArray& beforeChange ) + : QTextCommand( d ), firstParag( fParag ), lastParag( lParag ), before( beforeChange ) +{ + after = readStyleInformation( d, fParag, lParag ); +} + + +QByteArray QTextStyleCommand::readStyleInformation( QTextDocument* doc, int fParag, int lParag ) +{ + QByteArray style; +#ifndef QT_NO_DATASTREAM + QTextParagraph *p = doc->paragAt( fParag ); + if ( !p ) + return style; + QDataStream styleStream( style, IO_WriteOnly ); + int num = lParag - fParag + 1; + styleStream << num; + while ( num -- && p ) { + p->writeStyleInformation( styleStream ); + p = p->next(); + } +#endif + return style; +} + +void QTextStyleCommand::writeStyleInformation( QTextDocument* doc, int fParag, const QByteArray& style ) +{ +#ifndef QT_NO_DATASTREAM + QTextParagraph *p = doc->paragAt( fParag ); + if ( !p ) + return; + QDataStream styleStream( style, IO_ReadOnly ); + int num; + styleStream >> num; + while ( num-- && p ) { + p->readStyleInformation( styleStream ); + p = p->next(); + } +#endif +} + +QTextCursor *QTextStyleCommand::execute( QTextCursor *c ) +{ + writeStyleInformation( doc, firstParag, after ); + return c; +} + +QTextCursor *QTextStyleCommand::unexecute( QTextCursor *c ) +{ + writeStyleInformation( doc, firstParag, before ); + return c; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextCursor::QTextCursor( QTextDocument *d ) + : idx( 0 ), tmpX( -1 ), ox( 0 ), oy( 0 ), + valid( TRUE ) +{ + para = d ? d->firstParagraph() : 0; +} + +QTextCursor::QTextCursor( const QTextCursor &c ) +{ + ox = c.ox; + oy = c.oy; + idx = c.idx; + para = c.para; + tmpX = c.tmpX; + indices = c.indices; + paras = c.paras; + xOffsets = c.xOffsets; + yOffsets = c.yOffsets; + valid = c.valid; +} + +QTextCursor &QTextCursor::operator=( const QTextCursor &c ) +{ + ox = c.ox; + oy = c.oy; + idx = c.idx; + para = c.para; + tmpX = c.tmpX; + indices = c.indices; + paras = c.paras; + xOffsets = c.xOffsets; + yOffsets = c.yOffsets; + valid = c.valid; + + return *this; +} + +bool QTextCursor::operator==( const QTextCursor &c ) const +{ + return para == c.para && idx == c.idx; +} + +int QTextCursor::totalOffsetX() const +{ + int xoff = ox; + for ( QValueStack<int>::ConstIterator xit = xOffsets.begin(); xit != xOffsets.end(); ++xit ) + xoff += *xit; + return xoff; +} + +int QTextCursor::totalOffsetY() const +{ + int yoff = oy; + for ( QValueStack<int>::ConstIterator yit = yOffsets.begin(); yit != yOffsets.end(); ++yit ) + yoff += *yit; + return yoff; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void QTextCursor::gotoIntoNested( const QPoint &globalPos ) +{ + if ( !para ) + return; + Q_ASSERT( para->at( idx )->isCustom() ); + push(); + ox = 0; + int bl, y; + para->lineHeightOfChar( idx, &bl, &y ); + oy = y + para->rect().y(); + ox = para->at( idx )->x; + QTextDocument* doc = document(); + para->at( idx )->customItem()->enterAt( this, doc, para, idx, ox, oy, globalPos-QPoint(ox,oy) ); +} +#endif + +void QTextCursor::invalidateNested() +{ + if ( nestedDepth() ) { + QValueStack<QTextParagraph*>::Iterator it = paras.begin(); + QValueStack<int>::Iterator it2 = indices.begin(); + for ( ; it != paras.end(); ++it, ++it2 ) { + if ( *it == para ) + continue; + (*it)->invalidate( 0 ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( (*it)->at( *it2 )->isCustom() ) + (*it)->at( *it2 )->customItem()->invalidate(); +#endif + } + } +} + +void QTextCursor::insert( const QString &str, bool checkNewLine, QMemArray<QTextStringChar> *formatting ) +{ + tmpX = -1; + bool justInsert = TRUE; + QString s( str ); +#if defined(Q_WS_WIN) + if ( checkNewLine ) { + int i = 0; + while ( ( i = s.find( '\r', i ) ) != -1 ) + s.remove( i ,1 ); + } +#endif + if ( checkNewLine ) + justInsert = s.find( '\n' ) == -1; + if ( justInsert ) { // we ignore new lines and insert all in the current para at the current index + para->insert( idx, s.unicode(), s.length() ); + if ( formatting ) { + for ( int i = 0; i < (int)s.length(); ++i ) { + if ( formatting->at( i ).format() ) { + formatting->at( i ).format()->addRef(); + para->string()->setFormat( idx + i, formatting->at( i ).format(), TRUE ); + } + } + } + idx += s.length(); + } else { // we split at new lines + int start = -1; + int end; + int y = para->rect().y() + para->rect().height(); + int lastIndex = 0; + do { + end = s.find( '\n', start + 1 ); // find line break + if ( end == -1 ) // didn't find one, so end of line is end of string + end = s.length(); + int len = (start == -1 ? end : end - start - 1); + if ( len > 0 ) // insert the line + para->insert( idx, s.unicode() + start + 1, len ); + else + para->invalidate( 0 ); + if ( formatting ) { // set formats to the chars of the line + for ( int i = 0; i < len; ++i ) { + if ( formatting->at( i + lastIndex ).format() ) { + formatting->at( i + lastIndex ).format()->addRef(); + para->string()->setFormat( i + idx, formatting->at( i + lastIndex ).format(), TRUE ); + } + } + lastIndex += len; + } + start = end; // next start is at the end of this line + idx += len; // increase the index of the cursor to the end of the inserted text + if ( s[end] == '\n' ) { // if at the end was a line break, break the line + splitAndInsertEmptyParagraph( FALSE, TRUE ); + para->setEndState( -1 ); + para->prev()->format( -1, FALSE ); + lastIndex++; + } + + } while ( end < (int)s.length() ); + + para->format( -1, FALSE ); + int dy = para->rect().y() + para->rect().height() - y; + QTextParagraph *p = para; + p->setParagId( p->prev() ? p->prev()->paragId() + 1 : 0 ); + p = p->next(); + while ( p ) { + p->setParagId( p->prev()->paragId() + 1 ); + p->move( dy ); + p->invalidate( 0 ); + p->setEndState( -1 ); + p = p->next(); + } + } + + int h = para->rect().height(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) + invalidateNested(); + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; + + fixCursorPosition(); +} + +void QTextCursor::gotoLeft() +{ + if ( para->string()->isRightToLeft() ) + gotoNextLetter(); + else + gotoPreviousLetter(); +} + +void QTextCursor::gotoPreviousLetter() +{ + tmpX = -1; + + if ( idx > 0 ) { + idx = para->string()->previousCursorPosition( idx ); +#ifndef QT_NO_TEXTCUSTOMITEM + const QTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) + processNesting( EnterEnd ); +#endif + } else if ( para->prev() ) { + para = para->prev(); + while ( !para->isVisible() && para->prev() ) + para = para->prev(); + idx = para->length() - 1; + } else if ( nestedDepth() ) { + pop(); + processNesting( Prev ); + if ( idx == -1 ) { + pop(); + if ( idx > 0 ) { + idx = para->string()->previousCursorPosition( idx ); +#ifndef QT_NO_TEXTCUSTOMITEM + const QTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) + processNesting( EnterEnd ); +#endif + } else if ( para->prev() ) { + para = para->prev(); + idx = para->length() - 1; + } + } + } +} + +void QTextCursor::push() +{ + indices.push( idx ); + paras.push( para ); + xOffsets.push( ox ); + yOffsets.push( oy ); +} + +void QTextCursor::pop() +{ + if ( indices.isEmpty() ) + return; + idx = indices.pop(); + para = paras.pop(); + ox = xOffsets.pop(); + oy = yOffsets.pop(); +} + +void QTextCursor::restoreState() +{ + while ( !indices.isEmpty() ) + pop(); +} + +bool QTextCursor::place( const QPoint &p, QTextParagraph *s, bool link, bool loosePlacing, bool matchBetweenCharacters ) +{ + QPoint pos( p ); + QRect r; + QTextParagraph *str = s; + if ( pos.y() < s->rect().y() ) { + pos.setY( s->rect().y() ); +#ifdef Q_WS_MACX + pos.setX( s->rect().x() ); +#endif + } + while ( s ) { + r = s->rect(); + r.setWidth( document() ? document()->width() : QWIDGETSIZE_MAX ); + if ( s->isVisible() ) + str = s; + if ( pos.y() >= r.y() && pos.y() <= r.y() + r.height() ) + break; + if ( loosePlacing == TRUE && !s->next() ) { +#ifdef Q_WS_MACX + pos.setX( s->rect().x() + s->rect().width() ); +#endif + break; + } + s = s->next(); + } + + if ( !s || !str ) + return FALSE; + + s = str; + + setParagraph( s ); + int y = s->rect().y(); + int lines = s->lines(); + QTextStringChar *chr = 0; + int index = 0; + int i = 0; + int cy = 0; + int ch = 0; + for ( ; i < lines; ++i ) { + chr = s->lineStartOfLine( i, &index ); + cy = s->lineY( i ); + ch = s->lineHeight( i ); + if ( !chr ) + return FALSE; + if ( pos.y() <= y + cy + ch ) + break; + } + int nextLine; + if ( i < lines - 1 ) + s->lineStartOfLine( i+1, &nextLine ); + else + nextLine = s->length(); + i = index; + int x = s->rect().x(); + if ( pos.x() < x ) + pos.setX( x + 1 ); + int cw; + int curpos = -1; + int dist = 10000000; + bool inCustom = FALSE; + while ( i < nextLine ) { + chr = s->at(i); + int cpos = x + chr->x; + cw = s->string()->width( i ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( chr->isCustom() && chr->customItem()->isNested() ) { + if ( pos.x() >= cpos && pos.x() <= cpos + cw && + pos.y() >= y + cy && pos.y() <= y + cy + chr->height() ) { + inCustom = TRUE; + curpos = i; + break; + } + } else +#endif + { + if( chr->rightToLeft ) + cpos += cw; + int d = cpos - pos.x(); + bool dm = d < 0 ? !chr->rightToLeft : chr->rightToLeft; + if ( ( matchBetweenCharacters == TRUE && (QABS( d ) < dist || (dist == d && dm == TRUE )) && para->string()->validCursorPosition( i ) ) || + ( matchBetweenCharacters == FALSE && ( d == 0 || dm == TRUE ) ) ) { + dist = QABS( d ); + if ( !link || ( pos.x() >= x + chr->x && ( loosePlacing == TRUE || pos.x() < cpos ) ) ) + curpos = i; + } + } + i++; + } + if ( curpos == -1 ) { + if ( loosePlacing == TRUE ) + curpos = s->length()-1; + else + return FALSE; + } + setIndex( curpos ); + +#ifndef QT_NO_TEXTCUSTOMITEM + if ( inCustom && para->document() && para->at( curpos )->isCustom() && para->at( curpos )->customItem()->isNested() ) { + QTextDocument *oldDoc = para->document(); + gotoIntoNested( pos ); + if ( oldDoc == para->document() ) + return TRUE; + QPoint p( pos.x() - offsetX(), pos.y() - offsetY() ); + if ( !place( p, document()->firstParagraph(), link ) ) + pop(); + } +#endif + return TRUE; +} + +bool QTextCursor::processNesting( Operation op ) +{ + if ( !para->document() ) + return FALSE; + QTextDocument* doc = para->document(); + push(); + ox = para->at( idx )->x; + int bl, y; + para->lineHeightOfChar( idx, &bl, &y ); + oy = y + para->rect().y(); + bool ok = FALSE; + +#ifndef QT_NO_TEXTCUSTOMITEM + switch ( op ) { + case EnterBegin: + ok = para->at( idx )->customItem()->enter( this, doc, para, idx, ox, oy ); + break; + case EnterEnd: + ok = para->at( idx )->customItem()->enter( this, doc, para, idx, ox, oy, TRUE ); + break; + case Next: + ok = para->at( idx )->customItem()->next( this, doc, para, idx, ox, oy ); + break; + case Prev: + ok = para->at( idx )->customItem()->prev( this, doc, para, idx, ox, oy ); + break; + case Down: + ok = para->at( idx )->customItem()->down( this, doc, para, idx, ox, oy ); + break; + case Up: + ok = para->at( idx )->customItem()->up( this, doc, para, idx, ox, oy ); + break; + } + if ( !ok ) +#endif + pop(); + return ok; +} + +void QTextCursor::gotoRight() +{ + if ( para->string()->isRightToLeft() ) + gotoPreviousLetter(); + else + gotoNextLetter(); +} + +void QTextCursor::gotoNextLetter() +{ + tmpX = -1; + +#ifndef QT_NO_TEXTCUSTOMITEM + const QTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) { + if ( processNesting( EnterBegin ) ) + return; + } +#endif + + if ( idx < para->length() - 1 ) { + idx = para->string()->nextCursorPosition( idx ); + } else if ( para->next() ) { + para = para->next(); + while ( !para->isVisible() && para->next() ) + para = para->next(); + idx = 0; + } else if ( nestedDepth() ) { + pop(); + processNesting( Next ); + if ( idx == -1 ) { + pop(); + if ( idx < para->length() - 1 ) { + idx = para->string()->nextCursorPosition( idx ); + } else if ( para->next() ) { + para = para->next(); + idx = 0; + } + } + } +} + +void QTextCursor::gotoUp() +{ + int indexOfLineStart; + int line; + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + if (tmpX < 0) + tmpX = x(); + + if ( indexOfLineStart == 0 ) { + if ( !para->prev() ) { + if ( !nestedDepth() ) + return; + pop(); + processNesting( Up ); + if ( idx == -1 ) { + pop(); + if ( !para->prev() ) + return; + idx = tmpX = 0; + } else { + tmpX = -1; + return; + } + } + QTextParagraph *p = para->prev(); + while ( p && !p->isVisible() ) + p = p->prev(); + if ( p ) + para = p; + int lastLine = para->lines() - 1; + if ( !para->lineStartOfLine( lastLine, &indexOfLineStart ) ) + return; + idx = indexOfLineStart; + while (idx < para->length()-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } else { + --line; + int oldIndexOfLineStart = indexOfLineStart; + if ( !para->lineStartOfLine( line, &indexOfLineStart ) ) + return; + idx = indexOfLineStart; + while (idx < oldIndexOfLineStart-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } + fixCursorPosition(); +} + +void QTextCursor::gotoDown() +{ + int indexOfLineStart; + int line; + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + if (tmpX < 0) + tmpX = x(); + if ( line == para->lines() - 1 ) { + if ( !para->next() ) { + if ( !nestedDepth() ) + return; + pop(); + processNesting( Down ); + if ( idx == -1 ) { + pop(); + if ( !para->next() ) + return; + idx = tmpX = 0; + } else { + tmpX = -1; + return; + } + } + QTextParagraph *s = para->next(); + while ( s && !s->isVisible() ) + s = s->next(); + if ( s ) + para = s; + if ( !para->lineStartOfLine( 0, &indexOfLineStart ) ) + return; + int end; + if ( para->lines() == 1 ) + end = para->length(); + else + para->lineStartOfLine( 1, &end ); + + idx = indexOfLineStart; + while (idx < end-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } else { + ++line; + int end; + if ( line == para->lines() - 1 ) + end = para->length(); + else + para->lineStartOfLine( line + 1, &end ); + if ( !para->lineStartOfLine( line, &indexOfLineStart ) ) + return; + idx = indexOfLineStart; + while (idx < end-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } + fixCursorPosition(); +} + +void QTextCursor::gotoLineEnd() +{ + tmpX = -1; + int indexOfLineStart; + int line; + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + if ( line == para->lines() - 1 ) { + idx = para->length() - 1; + } else { + c = para->lineStartOfLine( ++line, &indexOfLineStart ); + indexOfLineStart--; + idx = indexOfLineStart; + } +} + +void QTextCursor::gotoLineStart() +{ + tmpX = -1; + int indexOfLineStart; + int line; + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + idx = indexOfLineStart; +} + +void QTextCursor::gotoHome() +{ + if ( topParagraph()->document() ) + gotoPosition( topParagraph()->document()->firstParagraph() ); + else + gotoLineStart(); +} + +void QTextCursor::gotoEnd() +{ + if ( topParagraph()->document() && topParagraph()->document()->lastParagraph()->isValid() ) + gotoPosition( topParagraph()->document()->lastParagraph(), + topParagraph()->document()->lastParagraph()->length() - 1); + else + gotoLineEnd(); +} + +void QTextCursor::gotoPageUp( int visibleHeight ) +{ + int targetY = globalY() - visibleHeight; + QTextParagraph* old; int index; + do { + old = para; index = idx; + gotoUp(); + } while ( (old != para || index != idx) && globalY() > targetY ); +} + +void QTextCursor::gotoPageDown( int visibleHeight ) +{ + int targetY = globalY() + visibleHeight; + QTextParagraph* old; int index; + do { + old = para; index = idx; + gotoDown(); + } while ( (old != para || index != idx) && globalY() < targetY ); +} + +void QTextCursor::gotoWordRight() +{ + if ( para->string()->isRightToLeft() ) + gotoPreviousWord(); + else + gotoNextWord(); +} + +void QTextCursor::gotoWordLeft() +{ + if ( para->string()->isRightToLeft() ) + gotoNextWord(); + else + gotoPreviousWord(); +} + +static bool is_seperator( const QChar &c, bool onlySpace ) +{ + if ( onlySpace ) + return c.isSpace(); + return c.isSpace() || + c == '\t' || + c == '.' || + c == ',' || + c == ':' || + c == ';' || + c == '-' || + c == '<' || + c == '>' || + c == '[' || + c == ']' || + c == '(' || + c == ')' || + c == '{' || + c == '}'; +} + +void QTextCursor::gotoPreviousWord( bool onlySpace ) +{ + gotoPreviousLetter(); + tmpX = -1; + QTextString *s = para->string(); + bool allowSame = FALSE; + if ( idx == ((int)s->length()-1) ) + return; + for ( int i = idx; i >= 0; --i ) { + if ( is_seperator( s->at( i ).c, onlySpace ) ) { + if ( !allowSame ) + continue; + idx = i + 1; + return; + } + if ( !allowSame && !is_seperator( s->at( i ).c, onlySpace ) ) + allowSame = TRUE; + } + idx = 0; +} + +void QTextCursor::gotoNextWord( bool onlySpace ) +{ + tmpX = -1; + QTextString *s = para->string(); + bool allowSame = FALSE; + for ( int i = idx; i < (int)s->length(); ++i ) { + if ( !is_seperator( s->at( i ).c, onlySpace ) ) { + if ( !allowSame ) + continue; + idx = i; + return; + } + if ( !allowSame && is_seperator( s->at( i ).c, onlySpace ) ) + allowSame = TRUE; + + } + + if ( idx < ((int)s->length()-1) ) { + gotoLineEnd(); + } else if ( para->next() ) { + QTextParagraph *p = para->next(); + while ( p && !p->isVisible() ) + p = p->next(); + if ( s ) { + para = p; + idx = 0; + } + } else { + gotoLineEnd(); + } +} + +bool QTextCursor::atParagStart() +{ + return idx == 0; +} + +bool QTextCursor::atParagEnd() +{ + return idx == para->length() - 1; +} + +void QTextCursor::splitAndInsertEmptyParagraph( bool ind, bool updateIds ) +{ + if ( !para->document() ) + return; + tmpX = -1; + QTextFormat *f = 0; + if ( para->document()->useFormatCollection() ) { + f = para->at( idx )->format(); + if ( idx == para->length() - 1 && idx > 0 ) + f = para->at( idx - 1 )->format(); + if ( f->isMisspelled() ) { + f->removeRef(); + f = para->document()->formatCollection()->format( f->font(), f->color() ); + } + } + + if ( atParagEnd() ) { + QTextParagraph *n = para->next(); + QTextParagraph *s = para->document()->createParagraph( para->document(), para, n, updateIds ); + if ( f ) + s->setFormat( 0, 1, f, TRUE ); + s->copyParagData( para ); + if ( ind ) { + int oi, ni; + s->indent( &oi, &ni ); + para = s; + idx = ni; + } else { + para = s; + idx = 0; + } + } else if ( atParagStart() ) { + QTextParagraph *p = para->prev(); + QTextParagraph *s = para->document()->createParagraph( para->document(), p, para, updateIds ); + if ( f ) + s->setFormat( 0, 1, f, TRUE ); + s->copyParagData( para ); + if ( ind ) { + s->indent(); + s->format(); + indent(); + para->format(); + } + } else { + QString str = para->string()->toString().mid( idx, 0xFFFFFF ); + QTextParagraph *n = para->next(); + QTextParagraph *s = para->document()->createParagraph( para->document(), para, n, updateIds ); + s->copyParagData( para ); + s->remove( 0, 1 ); + s->append( str, TRUE ); + for ( uint i = 0; i < str.length(); ++i ) { + QTextStringChar* tsc = para->at( idx + i ); + s->setFormat( i, 1, tsc->format(), TRUE ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( tsc->isCustom() ) { + QTextCustomItem * item = tsc->customItem(); + s->at( i )->setCustomItem( item ); + tsc->loseCustomItem(); + } +#endif + if ( tsc->isAnchor() ) + s->at( i )->setAnchor( tsc->anchorName(), + tsc->anchorHref() ); + } + para->truncate( idx ); + if ( ind ) { + int oi, ni; + s->indent( &oi, &ni ); + para = s; + idx = ni; + } else { + para = s; + idx = 0; + } + } + + invalidateNested(); +} + +bool QTextCursor::remove() +{ + tmpX = -1; + if ( !atParagEnd() ) { + int next = para->string()->nextCursorPosition( idx ); + para->remove( idx, next-idx ); + int h = para->rect().height(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) + invalidateNested(); + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; + return FALSE; + } else if ( para->next() ) { + para->join( para->next() ); + invalidateNested(); + return TRUE; + } + return FALSE; +} + +/* needed to implement backspace the correct way */ +bool QTextCursor::removePreviousChar() +{ + tmpX = -1; + if ( !atParagStart() ) { + para->remove( idx-1, 1 ); + int h = para->rect().height(); + idx--; + // shouldn't be needed, just to make sure. + fixCursorPosition(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) + invalidateNested(); + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; + return FALSE; + } else if ( para->prev() ) { + para = para->prev(); + para->join( para->next() ); + invalidateNested(); + return TRUE; + } + return FALSE; +} + +void QTextCursor::indent() +{ + int oi = 0, ni = 0; + para->indent( &oi, &ni ); + if ( oi == ni ) + return; + + if ( idx >= oi ) + idx += ni - oi; + else + idx = ni; +} + +void QTextCursor::fixCursorPosition() +{ + // searches for the closest valid cursor position + if ( para->string()->validCursorPosition( idx ) ) + return; + + int lineIdx; + QTextStringChar *start = para->lineStartOfChar( idx, &lineIdx, 0 ); + int x = para->string()->at( idx ).x; + int diff = QABS(start->x - x); + int best = lineIdx; + + QTextStringChar *c = start; + ++c; + + QTextStringChar *end = ¶->string()->at( para->length()-1 ); + while ( c <= end && !c->lineStart ) { + int xp = c->x; + if ( c->rightToLeft ) + xp += para->string()->width( lineIdx + (c-start) ); + int ndiff = QABS(xp - x); + if ( ndiff < diff && para->string()->validCursorPosition(lineIdx + (c-start)) ) { + diff = ndiff; + best = lineIdx + (c-start); + } + ++c; + } + idx = best; +} + + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextDocument::QTextDocument( QTextDocument *p ) + : par( p ), parentPar( 0 ) +#ifndef QT_NO_TEXTCUSTOMITEM + , tc( 0 ) +#endif + , tArray( 0 ), tStopWidth( 0 ) +{ + fCollection = par ? par->fCollection : new QTextFormatCollection; + init(); +} + +void QTextDocument::init() +{ + oTextValid = TRUE; + mightHaveCustomItems = FALSE; + if ( par ) + par->insertChild( this ); + pProcessor = 0; + useFC = TRUE; + pFormatter = 0; + indenter = 0; + fParag = 0; + txtFormat = Qt::AutoText; + preferRichText = FALSE; + pages = FALSE; + focusIndicator.parag = 0; + minw = 0; + wused = 0; + minwParag = curParag = 0; + align = AlignAuto; + nSelections = 1; + + setStyleSheet( QStyleSheet::defaultSheet() ); +#ifndef QT_NO_MIME + factory_ = QMimeSourceFactory::defaultFactory(); +#endif + contxt = QString::null; + + underlLinks = par ? par->underlLinks : TRUE; + backBrush = 0; + buf_pixmap = 0; + nextDoubleBuffered = FALSE; + + if ( par ) + withoutDoubleBuffer = par->withoutDoubleBuffer; + else + withoutDoubleBuffer = FALSE; + + lParag = fParag = createParagraph( this, 0, 0 ); + + cx = 0; + cy = 2; + if ( par ) + cx = cy = 0; + cw = 600; + vw = 0; + flow_ = new QTextFlow; + flow_->setWidth( cw ); + + leftmargin = rightmargin = 4; + scaleFontsFactor = 1; + + + selectionColors[ Standard ] = QApplication::palette().color( QPalette::Active, QColorGroup::Highlight ); + selectionText[ Standard ] = TRUE; + selectionText[ IMSelectionText ] = TRUE; + selectionText[ IMCompositionText ] = FALSE; + commandHistory = new QTextCommandHistory( 100 ); + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; +} + +QTextDocument::~QTextDocument() +{ + delete commandHistory; + if ( par ) + par->removeChild( this ); + clear(); + delete flow_; + if ( !par ) { + delete pFormatter; + delete fCollection; + } + delete pProcessor; + delete buf_pixmap; + delete indenter; + delete backBrush; + delete [] tArray; +} + +void QTextDocument::clear( bool createEmptyParag ) +{ + while ( fParag ) { + QTextParagraph *p = fParag->next(); + delete fParag; + fParag = p; + } + if ( flow_ ) + flow_->clear(); + fParag = lParag = 0; + if ( createEmptyParag ) + fParag = lParag = createParagraph( this ); + focusIndicator.parag = 0; + selections.clear(); + oText = QString::null; + oTextValid = FALSE; +} + +int QTextDocument::widthUsed() const +{ + return wused + 2*border_tolerance; +} + +int QTextDocument::height() const +{ + int h = 0; + if ( lParag ) + h = lParag->rect().top() + lParag->rect().height() + 1; + int fh = flow_->boundingRect().bottom(); + return QMAX( h, fh ); +} + + + +QTextParagraph *QTextDocument::createParagraph( QTextDocument *d, QTextParagraph *pr, QTextParagraph *nx, bool updateIds ) +{ + return new QTextParagraph( d, pr, nx, updateIds ); +} + +bool QTextDocument::setMinimumWidth( int needed, int used, QTextParagraph *p ) +{ + if ( needed == -1 ) { + minw = 0; + wused = 0; + p = 0; + } + if ( p == minwParag ) { + if (minw > needed) { + QTextParagraph *tp = fParag; + while (tp) { + if (tp != p && tp->minwidth > needed) { + needed = tp->minwidth; + minwParag = tp; + } + tp = tp->n; + } + } + minw = needed; + emit minimumWidthChanged( minw ); + } else if ( needed > minw ) { + minw = needed; + minwParag = p; + emit minimumWidthChanged( minw ); + } + wused = QMAX( wused, used ); + wused = QMAX( wused, minw ); + cw = QMAX( minw, cw ); + return TRUE; +} + +void QTextDocument::setPlainText( const QString &text ) +{ + preferRichText = FALSE; + clear(); + oTextValid = TRUE; + oText = text; + + int lastNl = 0; + int nl = text.find( '\n' ); + if ( nl == -1 ) { + lParag = createParagraph( this, lParag, 0 ); + if ( !fParag ) + fParag = lParag; + QString s = text; + if ( !s.isEmpty() ) { + if ( s[ (int)s.length() - 1 ] == '\r' ) + s.remove( s.length() - 1, 1 ); + lParag->append( s ); + } + } else { + for (;;) { + lParag = createParagraph( this, lParag, 0 ); + if ( !fParag ) + fParag = lParag; + int l = nl - lastNl; + if ( l > 0 ) { + if (text.unicode()[nl-1] == '\r') + l--; + QConstString cs(text.unicode()+lastNl, l); + lParag->append( cs.string() ); + } + if ( nl == (int)text.length() ) + break; + lastNl = nl + 1; + nl = text.find( '\n', nl + 1 ); + if ( nl == -1 ) + nl = text.length(); + } + } + if ( !lParag ) + lParag = fParag = createParagraph( this, 0, 0 ); +} + +struct Q_EXPORT QTextDocumentTag { + QTextDocumentTag(){} + QTextDocumentTag( const QString&n, const QStyleSheetItem* s, const QTextFormat& f ) + :name(n),style(s), format(f), alignment(Qt::AlignAuto), direction(QChar::DirON),liststyle(QStyleSheetItem::ListDisc) { + wsm = QStyleSheetItem::WhiteSpaceNormal; + } + QString name; + const QStyleSheetItem* style; + QString anchorHref; + QStyleSheetItem::WhiteSpaceMode wsm; + QTextFormat format; + int alignment : 16; + int direction : 5; + QStyleSheetItem::ListStyle liststyle; + + QTextDocumentTag( const QTextDocumentTag& t ) { + name = t.name; + style = t.style; + anchorHref = t.anchorHref; + wsm = t.wsm; + format = t.format; + alignment = t.alignment; + direction = t.direction; + liststyle = t.liststyle; + } + QTextDocumentTag& operator=(const QTextDocumentTag& t) { + name = t.name; + style = t.style; + anchorHref = t.anchorHref; + wsm = t.wsm; + format = t.format; + alignment = t.alignment; + direction = t.direction; + liststyle = t.liststyle; + return *this; + } + + Q_DUMMY_COMPARISON_OPERATOR(QTextDocumentTag) +}; + + +#define NEWPAR do{ if ( !hasNewPar) { \ + if ( !textEditMode && curpar && curpar->length()>1 && curpar->at( curpar->length()-2)->c == QChar_linesep ) \ + curpar->remove( curpar->length()-2, 1 ); \ + curpar = createParagraph( this, curpar, curpar->next() ); styles.append( vec ); vec = 0;} \ + hasNewPar = TRUE; \ + curpar->rtext = TRUE; \ + curpar->align = curtag.alignment; \ + curpar->lstyle = curtag.liststyle; \ + curpar->litem = ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ); \ + curpar->str->setDirection( (QChar::Direction)curtag.direction ); \ + space = TRUE; \ + tabExpansionColumn = 0; \ + delete vec; vec = new QPtrVector<QStyleSheetItem>( (uint)tags.count() + 1); \ + int i = 0; \ + for ( QValueStack<QTextDocumentTag>::Iterator it = tags.begin(); it != tags.end(); ++it ) \ + vec->insert( i++, (*it).style ); \ + vec->insert( i, curtag.style ); \ + }while(FALSE); + + +void QTextDocument::setRichText( const QString &text, const QString &context, const QTextFormat *initialFormat ) +{ + preferRichText = TRUE; + if ( !context.isEmpty() ) + setContext( context ); + clear(); + fParag = lParag = createParagraph( this ); + oTextValid = TRUE; + oText = text; + setRichTextInternal( text, 0, initialFormat ); + fParag->rtext = TRUE; +} + +void QTextDocument::setRichTextInternal( const QString &text, QTextCursor* cursor, const QTextFormat *initialFormat ) +{ + QTextParagraph* curpar = lParag; + int pos = 0; + QValueStack<QTextDocumentTag> tags; + if ( !initialFormat ) + initialFormat = formatCollection()->defaultFormat(); + QTextDocumentTag initag( "", sheet_->item(""), *initialFormat ); + if ( bodyText.isValid() ) + initag.format.setColor( bodyText ); + QTextDocumentTag curtag = initag; + bool space = TRUE; + bool canMergeLi = FALSE; + + bool textEditMode = FALSE; + int tabExpansionColumn = 0; + + const QChar* doc = text.unicode(); + int length = text.length(); + bool hasNewPar = curpar->length() <= 1; + QString anchorName; + + // style sheet handling for margin and line spacing calculation below + QTextParagraph* stylesPar = curpar; + QPtrVector<QStyleSheetItem>* vec = 0; + QPtrList< QPtrVector<QStyleSheetItem> > styles; + styles.setAutoDelete( TRUE ); + + if ( cursor ) { + cursor->splitAndInsertEmptyParagraph(); + QTextCursor tmp = *cursor; + tmp.gotoPreviousLetter(); + stylesPar = curpar = tmp.paragraph(); + hasNewPar = TRUE; + textEditMode = TRUE; + } else { + NEWPAR; + } + + // set rtext spacing to FALSE for the initial paragraph. + curpar->rtext = FALSE; + + QString wellKnownTags = "br hr wsp table qt body meta title"; + + while ( pos < length ) { + if ( hasPrefix(doc, length, pos, '<' ) ){ + if ( !hasPrefix( doc, length, pos+1, QChar('/') ) ) { + // open tag + QMap<QString, QString> attr; + bool emptyTag = FALSE; + QString tagname = parseOpenTag(doc, length, pos, attr, emptyTag); + if ( tagname.isEmpty() ) + continue; // nothing we could do with this, probably parse error + + const QStyleSheetItem* nstyle = sheet_->item(tagname); + + if ( nstyle ) { + // we might have to close some 'forgotten' tags + while ( !nstyle->allowedInContext( curtag.style ) ) { + QString msg; + msg.sprintf( "QText Warning: Document not valid ( '%s' not allowed in '%s' #%d)", + tagname.ascii(), curtag.style->name().ascii(), pos); + sheet_->error( msg ); + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + + /* special handling for p and li for HTML + compatibility. We do not want to embed blocks in + p, and we do not want new blocks inside non-empty + lis. Plus we want to merge empty lis sometimes. */ + if( nstyle->displayMode() == QStyleSheetItem::DisplayListItem ) { + canMergeLi = TRUE; + } else if ( nstyle->displayMode() == QStyleSheetItem::DisplayBlock ) { + while ( curtag.style->name() == "p" ) { + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + + if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { + // we are in a li and a new block comes along + if ( nstyle->name() == "ul" || nstyle->name() == "ol" ) + hasNewPar = FALSE; // we want an empty li (like most browsers) + if ( !hasNewPar ) { + /* do not add new blocks inside + non-empty lis */ + while ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + } else if ( canMergeLi ) { + /* we have an empty li and a block + comes along, merge them */ + nstyle = curtag.style; + } + canMergeLi = FALSE; + } + } + } + +#ifndef QT_NO_TEXTCUSTOMITEM + QTextCustomItem* custom = 0; +#else + bool custom = FALSE; +#endif + + // some well-known tags, some have a nstyle, some not + if ( wellKnownTags.find( tagname ) != -1 ) { + if ( tagname == "br" ) { + emptyTag = space = TRUE; + int index = QMAX( curpar->length(),1) - 1; + QTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->append( QChar_linesep ); + curpar->setFormat( index, 1, &format ); + hasNewPar = false; + } else if ( tagname == "hr" ) { + emptyTag = space = TRUE; +#ifndef QT_NO_TEXTCUSTOMITEM + custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); +#endif + } else if ( tagname == "table" ) { + emptyTag = space = TRUE; +#ifndef QT_NO_TEXTCUSTOMITEM + QTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->setAlignment( curtag.alignment ); + custom = parseTable( attr, format, doc, length, pos, curpar ); +#endif + } else if ( tagname == "qt" || tagname == "body" ) { + if ( attr.contains( "bgcolor" ) ) { + QBrush *b = new QBrush( QColor( attr["bgcolor"] ) ); + setPaper( b ); + } + if ( attr.contains( "background" ) ) { +#ifndef QT_NO_MIME + QImage img; + QString bg = attr["background"]; + const QMimeSource* m = factory_->data( bg, contxt ); + if ( !m ) { + qWarning("QRichText: no mimesource for %s", bg.latin1() ); + } else { + if ( !QImageDrag::decode( m, img ) ) { + qWarning("QTextImage: cannot decode %s", bg.latin1() ); + } + } + if ( !img.isNull() ) { + QBrush *b = new QBrush( QColor(), QPixmap( img ) ); + setPaper( b ); + } +#endif + } + if ( attr.contains( "text" ) ) { + QColor c( attr["text"] ); + initag.format.setColor( c ); + curtag.format.setColor( c ); + bodyText = c; + } + if ( attr.contains( "link" ) ) + linkColor = QColor( attr["link"] ); + if ( attr.contains( "title" ) ) + attribs.replace( "title", attr["title"] ); + + if ( textEditMode ) { + if ( attr.contains("style" ) ) { + QString a = attr["style"]; + for ( int s = 0; s < a.contains(';')+1; s++ ) { + QString style = a.section( ';', s, s ); + if ( style.startsWith("font-size:" ) && style.endsWith("pt") ) { + scaleFontsFactor = double( formatCollection()->defaultFormat()->fn.pointSize() ) / + style.mid( 10, style.length() - 12 ).toInt(); + } + } + } + nstyle = 0; // ignore body in textEditMode + } + // end qt- and body-tag handling + } else if ( tagname == "meta" ) { + if ( attr["name"] == "qrichtext" && attr["content"] == "1" ) + textEditMode = TRUE; + } else if ( tagname == "title" ) { + QString title; + while ( pos < length ) { + if ( hasPrefix( doc, length, pos, QChar('<') ) && hasPrefix( doc, length, pos+1, QChar('/') ) && + parseCloseTag( doc, length, pos ) == "title" ) + break; + title += doc[ pos ]; + ++pos; + } + attribs.replace( "title", title ); + } + } // end of well-known tag handling + +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !custom ) // try generic custom item + custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); +#endif + if ( !nstyle && !custom ) // we have no clue what this tag could be, ignore it + continue; + + if ( custom ) { +#ifndef QT_NO_TEXTCUSTOMITEM + int index = QMAX( curpar->length(),1) - 1; + QTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->append( QChar('*') ); + QTextFormat* f = formatCollection()->format( &format ); + curpar->setFormat( index, 1, f ); + curpar->at( index )->setCustomItem( custom ); + if ( !curtag.anchorHref.isEmpty() ) + curpar->at(index)->setAnchor( QString::null, curtag.anchorHref ); + if ( !anchorName.isEmpty() ) { + curpar->at(index)->setAnchor( anchorName, curpar->at(index)->anchorHref() ); + anchorName = QString::null; + } + registerCustomItem( custom, curpar ); + hasNewPar = FALSE; +#endif + } else if ( !emptyTag ) { + /* if we do nesting, push curtag on the stack, + otherwise reinint curag. */ + if ( curtag.style->name() != tagname || nstyle->selfNesting() ) { + tags.push( curtag ); + } else { + if ( !tags.isEmpty() ) + curtag = tags.top(); + else + curtag = initag; + } + + curtag.name = tagname; + curtag.style = nstyle; + curtag.name = tagname; + curtag.style = nstyle; + if ( nstyle->whiteSpaceMode() != QStyleSheetItem::WhiteSpaceModeUndefined ) + curtag.wsm = nstyle->whiteSpaceMode(); + + /* netscape compatibility: eat a newline and only a newline if a pre block starts */ + if ( curtag.wsm == QStyleSheetItem::WhiteSpacePre && + nstyle->displayMode() == QStyleSheetItem::DisplayBlock ) + eat( doc, length, pos, '\n' ); + + /* ignore whitespace for inline elements if there + was already one*/ + if ( !textEditMode && + (curtag.wsm == QStyleSheetItem::WhiteSpaceNormal + || curtag.wsm == QStyleSheetItem::WhiteSpaceNoWrap) + && ( space || nstyle->displayMode() != QStyleSheetItem::DisplayInline ) ) + eatSpace( doc, length, pos ); + + curtag.format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + if ( nstyle->isAnchor() ) { + if ( !anchorName.isEmpty() ) + anchorName += "#" + attr["name"]; + else + anchorName = attr["name"]; + curtag.anchorHref = attr["href"]; + } + + if ( nstyle->alignment() != QStyleSheetItem::Undefined ) + curtag.alignment = nstyle->alignment(); + + if ( nstyle->listStyle() != QStyleSheetItem::ListStyleUndefined ) + curtag.liststyle = nstyle->listStyle(); + + if ( nstyle->displayMode() == QStyleSheetItem::DisplayBlock + || nstyle->displayMode() == QStyleSheetItem::DisplayListItem ) { + + if ( nstyle->name() == "ol" || nstyle->name() == "ul" || nstyle->name() == "li") { + QString type = attr["type"]; + if ( !type.isEmpty() ) { + if ( type == "1" ) { + curtag.liststyle = QStyleSheetItem::ListDecimal; + } else if ( type == "a" ) { + curtag.liststyle = QStyleSheetItem::ListLowerAlpha; + } else if ( type == "A" ) { + curtag.liststyle = QStyleSheetItem::ListUpperAlpha; + } else { + type = type.lower(); + if ( type == "square" ) + curtag.liststyle = QStyleSheetItem::ListSquare; + else if ( type == "disc" ) + curtag.liststyle = QStyleSheetItem::ListDisc; + else if ( type == "circle" ) + curtag.liststyle = QStyleSheetItem::ListCircle; + } + } + } + + + /* Internally we treat ordered and bullet + lists the same for margin calculations. In + order to have fast pointer compares in the + xMargin() functions we restrict ourselves to + <ol>. Once we calculate the margins in the + parser rathern than later, the unelegance of + this approach goes awy + */ + if ( nstyle->name() == "ul" ) + curtag.style = sheet_->item( "ol" ); + + if ( attr.contains( "align" ) ) { + QString align = attr["align"].lower(); + if ( align == "center" ) + curtag.alignment = Qt::AlignCenter; + else if ( align == "right" ) + curtag.alignment = Qt::AlignRight; + else if ( align == "justify" ) + curtag.alignment = Qt::AlignJustify; + } + if ( attr.contains( "dir" ) ) { + QString dir = attr["dir"]; + if ( dir == "rtl" ) + curtag.direction = QChar::DirR; + else if ( dir == "ltr" ) + curtag.direction = QChar::DirL; + } + + NEWPAR; + + if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { + if ( attr.contains( "value " ) ) + curpar->setListValue( attr["value"].toInt() ); + } + + if ( attr.contains( "style" ) ) { + QString a = attr["style"]; + bool ok = TRUE; + for ( int s = 0; ok && s < a.contains(';')+1; s++ ) { + QString style = a.section( ';', s, s ); + if ( style.startsWith("margin-top:" ) && style.endsWith("px") ) + curpar->utm = 1+style.mid(11, style.length() - 13).toInt(&ok); + else if ( style.startsWith("margin-bottom:" ) && style.endsWith("px") ) + curpar->ubm = 1+style.mid(14, style.length() - 16).toInt(&ok); + else if ( style.startsWith("margin-left:" ) && style.endsWith("px") ) + curpar->ulm = 1+style.mid(12, style.length() - 14).toInt(&ok); + else if ( style.startsWith("margin-right:" ) && style.endsWith("px") ) + curpar->urm = 1+style.mid(13, style.length() - 15).toInt(&ok); + else if ( style.startsWith("text-indent:" ) && style.endsWith("px") ) + curpar->uflm = 1+style.mid(12, style.length() - 14).toInt(&ok); + } + if ( !ok ) // be pressmistic + curpar->utm = curpar->ubm = curpar->urm = curpar->ulm = 0; + } + } + } + } else { + QString tagname = parseCloseTag( doc, length, pos ); + if ( tagname.isEmpty() ) + continue; // nothing we could do with this, probably parse error + if ( !sheet_->item( tagname ) ) // ignore unknown tags + continue; + if ( tagname == "li" ) + continue; + + // we close a block item. Since the text may continue, we need to have a new paragraph + bool needNewPar = curtag.style->displayMode() == QStyleSheetItem::DisplayBlock + || curtag.style->displayMode() == QStyleSheetItem::DisplayListItem; + + + // html slopiness: handle unbalanched tag closing + while ( curtag.name != tagname ) { + QString msg; + msg.sprintf( "QText Warning: Document not valid ( '%s' not closed before '%s' #%d)", + curtag.name.ascii(), tagname.ascii(), pos); + sheet_->error( msg ); + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + + + // close the tag + if ( !tags.isEmpty() ) + curtag = tags.pop(); + else + curtag = initag; + + if ( needNewPar ) { + if ( textEditMode && (tagname == "p" || tagname == "div" ) ) // preserve empty paragraphs + hasNewPar = FALSE; + NEWPAR; + } + } + } else { + // normal contents + QString s; + QChar c; + while ( pos < length && !hasPrefix(doc, length, pos, QChar('<') ) ){ + if ( textEditMode ) { + // text edit mode: we handle all white space but ignore newlines + c = parseChar( doc, length, pos, QStyleSheetItem::WhiteSpacePre ); + if ( c == QChar_linesep ) + break; + } else { + int l = pos; + c = parseChar( doc, length, pos, curtag.wsm ); + + // in white space pre mode: treat any space as non breakable + // and expand tabs to eight character wide columns. + if ( curtag.wsm == QStyleSheetItem::WhiteSpacePre ) { + if ( c == '\t' ) { + c = ' '; + while( (++tabExpansionColumn)%8 ) + s += c; + } + if ( c == QChar_linesep ) + tabExpansionColumn = 0; + else + tabExpansionColumn++; + + } + if ( c == ' ' || c == QChar_linesep ) { + /* avoid overlong paragraphs by forcing a new + paragraph after 4096 characters. This case can + occur when loading undiscovered plain text + documents in rich text mode. Instead of hanging + forever, we do the trick. + */ + if ( curtag.wsm == QStyleSheetItem::WhiteSpaceNormal && s.length() > 4096 ) do { + if ( doc[l] == '\n' ) { + hasNewPar = FALSE; // for a new paragraph ... + NEWPAR; + hasNewPar = FALSE; // ... and make it non-reusable + c = '\n'; // make sure we break below + break; + } + } while ( ++l < pos ); + } + } + + if ( c == '\n' ) + break; // break on newlines, pre delievers a QChar_linesep + + bool c_isSpace = c.isSpace() && c.unicode() != 0x00a0U && !textEditMode; + + if ( curtag.wsm == QStyleSheetItem::WhiteSpaceNormal && c_isSpace && space ) + continue; + if ( c == '\r' ) + continue; + space = c_isSpace; + s += c; + } + if ( !s.isEmpty() && curtag.style->displayMode() != QStyleSheetItem::DisplayNone ) { + hasNewPar = FALSE; + int index = QMAX( curpar->length(),1) - 1; + curpar->append( s ); + if (curtag.wsm != QStyleSheetItem::WhiteSpaceNormal) { + QTextString *str = curpar->string(); + for (uint i = index; i < index + s.length(); ++i) + str->at(i).nobreak = TRUE; + } + + QTextFormat* f = formatCollection()->format( &curtag.format ); + curpar->setFormat( index, s.length(), f, FALSE ); // do not use collection because we have done that already + f->ref += s.length() -1; // that what friends are for... + if ( !curtag.anchorHref.isEmpty() ) { + for ( int i = 0; i < int(s.length()); i++ ) + curpar->at(index + i)->setAnchor( QString::null, curtag.anchorHref ); + } + if ( !anchorName.isEmpty() ) { + for ( int i = 0; i < int(s.length()); i++ ) + curpar->at(index + i)->setAnchor( anchorName, curpar->at(index + i)->anchorHref() ); + anchorName = QString::null; + } + } + } + } + + if ( hasNewPar && curpar != fParag && !cursor && stylesPar != curpar ) { + // cleanup unused last paragraphs + curpar = curpar->p; + delete curpar->n; + } + + if ( !anchorName.isEmpty() ) { + curpar->at(curpar->length() - 1)->setAnchor( anchorName, curpar->at( curpar->length() - 1 )->anchorHref() ); + anchorName = QString::null; + } + + + setRichTextMarginsInternal( styles, stylesPar ); + + if ( cursor ) { + cursor->gotoPreviousLetter(); + cursor->remove(); + } + delete vec; +} + +void QTextDocument::setRichTextMarginsInternal( QPtrList< QPtrVector<QStyleSheetItem> >& styles, QTextParagraph* stylesPar ) +{ + // margin and line spacing calculation + QPtrVector<QStyleSheetItem>* prevStyle = 0; + QPtrVector<QStyleSheetItem>* curStyle = styles.first(); + QPtrVector<QStyleSheetItem>* nextStyle = styles.next(); + while ( stylesPar ) { + if ( !curStyle ) { + stylesPar = stylesPar->next(); + prevStyle = curStyle; + curStyle = nextStyle; + nextStyle = styles.next(); + continue; + } + + int i, mar; + QStyleSheetItem* mainStyle = curStyle->size() ? (*curStyle)[curStyle->size()-1] : 0; + if ( mainStyle && mainStyle->displayMode() == QStyleSheetItem::DisplayListItem ) + stylesPar->setListItem( TRUE ); + int numLists = 0; + for ( i = 0; i < (int)curStyle->size(); ++i ) { + if ( (*curStyle)[ i ]->displayMode() == QStyleSheetItem::DisplayBlock + && (*curStyle)[ i ]->listStyle() != QStyleSheetItem::ListStyleUndefined ) + numLists++; + } + stylesPar->ldepth = numLists; + if ( stylesPar->next() && nextStyle ) { + // also set the depth of the next paragraph, required for the margin calculation + numLists = 0; + for ( i = 0; i < (int)nextStyle->size(); ++i ) { + if ( (*nextStyle)[ i ]->displayMode() == QStyleSheetItem::DisplayBlock + && (*nextStyle)[ i ]->listStyle() != QStyleSheetItem::ListStyleUndefined ) + numLists++; + } + stylesPar->next()->ldepth = numLists; + } + + // do the top margin + QStyleSheetItem* item = mainStyle; + int m; + if (stylesPar->utm > 0 ) { + m = stylesPar->utm-1; + stylesPar->utm = 0; + } else { + m = QMAX(0, item->margin( QStyleSheetItem::MarginTop ) ); + if ( stylesPar->ldepth ) + if ( item->displayMode() == QStyleSheetItem::DisplayListItem ) + m /= stylesPar->ldepth * stylesPar->ldepth; + else + m = 0; + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( prevStyle && i < (int) prevStyle->size() && + ( item->displayMode() == QStyleSheetItem::DisplayBlock && + (*prevStyle)[ i ] == item ) ) + break; + // emulate CSS2' standard 0 vertical margin for multiple ul or ol tags + if ( item->listStyle() != QStyleSheetItem::ListStyleUndefined && + ( ( i> 0 && (*curStyle)[ i-1 ] == item ) || (*curStyle)[i+1] == item ) ) + continue; + mar = QMAX( 0, item->margin( QStyleSheetItem::MarginTop ) ); + m = QMAX( m, mar ); + } + stylesPar->utm = m - stylesPar->topMargin(); + + // do the bottom margin + item = mainStyle; + if (stylesPar->ubm > 0 ) { + m = stylesPar->ubm-1; + stylesPar->ubm = 0; + } else { + m = QMAX(0, item->margin( QStyleSheetItem::MarginBottom ) ); + if ( stylesPar->ldepth ) + if ( item->displayMode() == QStyleSheetItem::DisplayListItem ) + m /= stylesPar->ldepth * stylesPar->ldepth; + else + m = 0; + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( nextStyle && i < (int) nextStyle->size() && + ( item->displayMode() == QStyleSheetItem::DisplayBlock && + (*nextStyle)[ i ] == item ) ) + break; + // emulate CSS2' standard 0 vertical margin for multiple ul or ol tags + if ( item->listStyle() != QStyleSheetItem::ListStyleUndefined && + ( ( i> 0 && (*curStyle)[ i-1 ] == item ) || (*curStyle)[i+1] == item ) ) + continue; + mar = QMAX(0, item->margin( QStyleSheetItem::MarginBottom ) ); + m = QMAX( m, mar ); + } + stylesPar->ubm = m - stylesPar->bottomMargin(); + + // do the left margin, simplyfied + item = mainStyle; + if (stylesPar->ulm > 0 ) { + m = stylesPar->ulm-1; + stylesPar->ulm = 0; + } else { + m = QMAX( 0, item->margin( QStyleSheetItem::MarginLeft ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + m += QMAX( 0, item->margin( QStyleSheetItem::MarginLeft ) ); + } + stylesPar->ulm = m - stylesPar->leftMargin(); + + // do the right margin, simplyfied + item = mainStyle; + if (stylesPar->urm > 0 ) { + m = stylesPar->urm-1; + stylesPar->urm = 0; + } else { + m = QMAX( 0, item->margin( QStyleSheetItem::MarginRight ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + m += QMAX( 0, item->margin( QStyleSheetItem::MarginRight ) ); + } + stylesPar->urm = m - stylesPar->rightMargin(); + + // do the first line margin, which really should be called text-indent + item = mainStyle; + if (stylesPar->uflm > 0 ) { + m = stylesPar->uflm-1; + stylesPar->uflm = 0; + } else { + m = QMAX( 0, item->margin( QStyleSheetItem::MarginFirstLine ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + mar = QMAX( 0, item->margin( QStyleSheetItem::MarginFirstLine ) ); + m = QMAX( m, mar ); + } + stylesPar->uflm =m - stylesPar->firstLineMargin(); + + // do the bogus line "spacing", which really is just an extra margin + item = mainStyle; + for ( i = (int)curStyle->size() - 1 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( item->lineSpacing() != QStyleSheetItem::Undefined ) { + stylesPar->ulinespacing = item->lineSpacing(); + if ( formatCollection() && + stylesPar->ulinespacing < formatCollection()->defaultFormat()->height() ) + stylesPar->ulinespacing += formatCollection()->defaultFormat()->height(); + break; + } + } + + stylesPar = stylesPar->next(); + prevStyle = curStyle; + curStyle = nextStyle; + nextStyle = styles.next(); + } +} + +void QTextDocument::setText( const QString &text, const QString &context ) +{ + focusIndicator.parag = 0; + selections.clear(); + if ( txtFormat == Qt::AutoText && QStyleSheet::mightBeRichText( text ) || + txtFormat == Qt::RichText ) + setRichText( text, context ); + else + setPlainText( text ); +} + +QString QTextDocument::plainText() const +{ + QString buffer; + QString s; + QTextParagraph *p = fParag; + while ( p ) { + if ( !p->mightHaveCustomItems ) { + const QTextString *ts = p->string(); // workaround VC++ and Borland + s = ts->toString(); // with FALSE we don't fix spaces (nbsp) + } else { + for ( int i = 0; i < p->length() - 1; ++i ) { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else +#endif + { + s += p->at( i )->c; + } + } + } + s.remove( s.length() - 1, 1 ); + if ( p->next() ) + s += "\n"; + buffer += s; + p = p->next(); + } + return buffer; +} + +static QString align_to_string( int a ) +{ + if ( a & Qt::AlignRight ) + return " align=\"right\""; + if ( a & Qt::AlignHCenter ) + return " align=\"center\""; + if ( a & Qt::AlignJustify ) + return " align=\"justify\""; + return QString::null; +} + +static QString direction_to_string( int d ) +{ + if ( d != QChar::DirON ) + return ( d == QChar::DirL? " dir=\"ltr\"" : " dir=\"rtl\"" ); + return QString::null; +} + +static QString list_value_to_string( int v ) +{ + if ( v != -1 ) + return " listvalue=\"" + QString::number( v ) + "\""; + return QString::null; +} + +static QString list_style_to_string( int v ) +{ + switch( v ) { + case QStyleSheetItem::ListDecimal: return "\"1\""; + case QStyleSheetItem::ListLowerAlpha: return "\"a\""; + case QStyleSheetItem::ListUpperAlpha: return "\"A\""; + case QStyleSheetItem::ListDisc: return "\"disc\""; + case QStyleSheetItem::ListSquare: return "\"square\""; + case QStyleSheetItem::ListCircle: return "\"circle\""; + default: + return QString::null; + } +} + +static inline bool list_is_ordered( int v ) +{ + return v == QStyleSheetItem::ListDecimal || + v == QStyleSheetItem::ListLowerAlpha || + v == QStyleSheetItem::ListUpperAlpha; +} + + +static QString margin_to_string( QStyleSheetItem* style, int t, int b, int l, int r, int fl ) +{ + QString s; + if ( l > 0 ) + s += QString(!!s?";":"") + "margin-left:" + QString::number(l+QMAX(0,style->margin(QStyleSheetItem::MarginLeft))) + "px"; + if ( r > 0 ) + s += QString(!!s?";":"") + "margin-right:" + QString::number(r+QMAX(0,style->margin(QStyleSheetItem::MarginRight))) + "px"; + if ( t > 0 ) + s += QString(!!s?";":"") + "margin-top:" + QString::number(t+QMAX(0,style->margin(QStyleSheetItem::MarginTop))) + "px"; + if ( b > 0 ) + s += QString(!!s?";":"") + "margin-bottom:" + QString::number(b+QMAX(0,style->margin(QStyleSheetItem::MarginBottom))) + "px"; + if ( fl > 0 ) + s += QString(!!s?";":"") + "text-indent:" + QString::number(fl+QMAX(0,style->margin(QStyleSheetItem::MarginFirstLine))) + "px"; + if ( !!s ) + return " style=\"" + s + "\""; + return QString::null; +} + +QString QTextDocument::richText() const +{ + QString s = ""; + if ( !par ) { + s += "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\"font-size:" ; + s += QString::number( formatCollection()->defaultFormat()->font().pointSize() ); + s += "pt;font-family:"; + s += formatCollection()->defaultFormat()->font().family(); + s +="\">"; + } + QTextParagraph* p = fParag; + + QStyleSheetItem* item_p = styleSheet()->item("p"); + QStyleSheetItem* item_div = styleSheet()->item("div"); + QStyleSheetItem* item_ul = styleSheet()->item("ul"); + QStyleSheetItem* item_ol = styleSheet()->item("ol"); + QStyleSheetItem* item_li = styleSheet()->item("li"); + if ( !item_p || !item_div || !item_ul || !item_ol || !item_li ) { + qWarning( "QTextEdit: cannot export HTML due to insufficient stylesheet (lack of p, div, ul, ol, or li)" ); + return QString::null; + } + int pastListDepth = 0; + int listDepth = 0; +#if 0 + int futureListDepth = 0; +#endif + QMemArray<int> listStyles(10); + + while ( p ) { + listDepth = p->listDepth(); + if ( listDepth < pastListDepth ) { + for ( int i = pastListDepth; i > listDepth; i-- ) + s += list_is_ordered( listStyles[i] ) ? "</ol>" : "</ul>"; + s += '\n'; + } else if ( listDepth > pastListDepth ) { + s += '\n'; + listStyles.resize( QMAX( (int)listStyles.size(), listDepth+1 ) ); + QString list_type; + listStyles[listDepth] = p->listStyle(); + if ( !list_is_ordered( p->listStyle() ) || item_ol->listStyle() != p->listStyle() ) + list_type = " type=" + list_style_to_string( p->listStyle() ); + for ( int i = pastListDepth; i < listDepth; i++ ) { + s += list_is_ordered( p->listStyle() ) ? "<ol" : "<ul" ; + s += list_type + ">"; + } + } else { + s += '\n'; + } + + QString ps = p->richText(); + +#if 0 + // for the bottom margin we need to know whether we are at the end of a list + futureListDepth = 0; + if ( listDepth > 0 && p->next() ) + futureListDepth = p->next()->listDepth(); +#endif + + if ( richTextExportStart && richTextExportStart->paragraph() ==p && + richTextExportStart->index() == 0 ) + s += "<!--StartFragment-->"; + + if ( p->isListItem() ) { + s += "<li"; + if ( p->listStyle() != listStyles[listDepth] ) + s += " type=" + list_style_to_string( p->listStyle() ); + s +=align_to_string( p->alignment() ); + s += margin_to_string( item_li, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s += list_value_to_string( p->listValue() ); + s += direction_to_string( p->direction() ); + s +=">"; + s += ps; + s += "</li>"; + } else if ( p->listDepth() ) { + s += "<div"; + s += align_to_string( p->alignment() ); + s += margin_to_string( item_div, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s +=direction_to_string( p->direction() ); + s += ">"; + s += ps; + s += "</div>"; + } else { + // normal paragraph item + s += "<p"; + s += align_to_string( p->alignment() ); + s += margin_to_string( item_p, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s +=direction_to_string( p->direction() ); + s += ">"; + s += ps; + s += "</p>"; + } + pastListDepth = listDepth; + p = p->next(); + } + while ( listDepth > 0 ) { + s += list_is_ordered( listStyles[listDepth] ) ? "</ol>" : "</ul>"; + listDepth--; + } + + if ( !par ) + s += "\n</body></html>\n"; + + return s; +} + +QString QTextDocument::text() const +{ + if ( txtFormat == Qt::AutoText && preferRichText || txtFormat == Qt::RichText ) + return richText(); + return plainText(); +} + +QString QTextDocument::text( int parag ) const +{ + QTextParagraph *p = paragAt( parag ); + if ( !p ) + return QString::null; + + if ( txtFormat == Qt::AutoText && preferRichText || txtFormat == Qt::RichText ) + return p->richText(); + else + return p->string()->toString(); +} + +void QTextDocument::invalidate() +{ + QTextParagraph *s = fParag; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +void QTextDocument::selectionStart( int id, int ¶gId, int &index ) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + QTextDocumentSelection &sel = *it; + paragId = !sel.swapped ? sel.startCursor.paragraph()->paragId() : sel.endCursor.paragraph()->paragId(); + index = !sel.swapped ? sel.startCursor.index() : sel.endCursor.index(); +} + +QTextCursor QTextDocument::selectionStartCursor( int id) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return QTextCursor( this ); + QTextDocumentSelection &sel = *it; + if ( sel.swapped ) + return sel.endCursor; + return sel.startCursor; +} + +QTextCursor QTextDocument::selectionEndCursor( int id) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return QTextCursor( this ); + QTextDocumentSelection &sel = *it; + if ( !sel.swapped ) + return sel.endCursor; + return sel.startCursor; +} + +void QTextDocument::selectionEnd( int id, int ¶gId, int &index ) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + QTextDocumentSelection &sel = *it; + paragId = sel.swapped ? sel.startCursor.paragraph()->paragId() : sel.endCursor.paragraph()->paragId(); + index = sel.swapped ? sel.startCursor.index() : sel.endCursor.index(); +} + +void QTextDocument::addSelection( int id ) +{ + nSelections = QMAX( nSelections, id + 1 ); +} + +static void setSelectionEndHelper( int id, QTextDocumentSelection &sel, QTextCursor &start, QTextCursor &end ) +{ + QTextCursor c1 = start; + QTextCursor c2 = end; + if ( sel.swapped ) { + c1 = end; + c2 = start; + } + + c1.paragraph()->removeSelection( id ); + c2.paragraph()->removeSelection( id ); + if ( c1.paragraph() != c2.paragraph() ) { + c1.paragraph()->setSelection( id, c1.index(), c1.paragraph()->length() - 1 ); + c2.paragraph()->setSelection( id, 0, c2.index() ); + } else { + c1.paragraph()->setSelection( id, QMIN( c1.index(), c2.index() ), QMAX( c1.index(), c2.index() ) ); + } + + sel.startCursor = start; + sel.endCursor = end; + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() ) + sel.swapped = sel.startCursor.index() > sel.endCursor.index(); +} + +bool QTextDocument::setSelectionEnd( int id, const QTextCursor &cursor ) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return FALSE; + QTextDocumentSelection &sel = *it; + + QTextCursor start = sel.startCursor; + QTextCursor end = cursor; + + if ( start == end ) { + removeSelection( id ); + setSelectionStart( id, cursor ); + return TRUE; + } + + if ( sel.endCursor.paragraph() == end.paragraph() ) { + setSelectionEndHelper( id, sel, start, end ); + return TRUE; + } + + bool inSelection = FALSE; + QTextCursor c( this ); + QTextCursor tmp = sel.startCursor; + if ( sel.swapped ) + tmp = sel.endCursor; + tmp.restoreState(); + QTextCursor tmp2 = cursor; + tmp2.restoreState(); + c.setParagraph( tmp.paragraph()->paragId() < tmp2.paragraph()->paragId() ? tmp.paragraph() : tmp2.paragraph() ); + bool hadStart = FALSE; + bool hadEnd = FALSE; + bool hadStartParag = FALSE; + bool hadEndParag = FALSE; + bool hadOldStart = FALSE; + bool hadOldEnd = FALSE; + bool leftSelection = FALSE; + sel.swapped = FALSE; + for ( ;; ) { + if ( c == start ) + hadStart = TRUE; + if ( c == end ) + hadEnd = TRUE; + if ( c.paragraph() == start.paragraph() ) + hadStartParag = TRUE; + if ( c.paragraph() == end.paragraph() ) + hadEndParag = TRUE; + if ( c == sel.startCursor ) + hadOldStart = TRUE; + if ( c == sel.endCursor ) + hadOldEnd = TRUE; + + if ( !sel.swapped && + ( hadEnd && !hadStart || + hadEnd && hadStart && start.paragraph() == end.paragraph() && start.index() > end.index() ) ) + sel.swapped = TRUE; + + if ( c == end && hadStartParag || + c == start && hadEndParag ) { + QTextCursor tmp = c; + tmp.restoreState(); + if ( tmp.paragraph() != c.paragraph() ) { + int sstart = tmp.paragraph()->selectionStart( id ); + tmp.paragraph()->removeSelection( id ); + tmp.paragraph()->setSelection( id, sstart, tmp.index() ); + } + } + + if ( inSelection && + ( c == end && hadStart || c == start && hadEnd ) ) + leftSelection = TRUE; + else if ( !leftSelection && !inSelection && ( hadStart || hadEnd ) ) + inSelection = TRUE; + + bool noSelectionAnymore = hadOldStart && hadOldEnd && leftSelection && !inSelection && !c.paragraph()->hasSelection( id ) && c.atParagEnd(); + c.paragraph()->removeSelection( id ); + if ( inSelection ) { + if ( c.paragraph() == start.paragraph() && start.paragraph() == end.paragraph() ) { + c.paragraph()->setSelection( id, QMIN( start.index(), end.index() ), QMAX( start.index(), end.index() ) ); + } else if ( c.paragraph() == start.paragraph() && !hadEndParag ) { + c.paragraph()->setSelection( id, start.index(), c.paragraph()->length() - 1 ); + } else if ( c.paragraph() == end.paragraph() && !hadStartParag ) { + c.paragraph()->setSelection( id, end.index(), c.paragraph()->length() - 1 ); + } else if ( c.paragraph() == end.paragraph() && hadEndParag ) { + c.paragraph()->setSelection( id, 0, end.index() ); + } else if ( c.paragraph() == start.paragraph() && hadStartParag ) { + c.paragraph()->setSelection( id, 0, start.index() ); + } else { + c.paragraph()->setSelection( id, 0, c.paragraph()->length() - 1 ); + } + } + + if ( leftSelection ) + inSelection = FALSE; + + if ( noSelectionAnymore ) + break; + // *ugle*hack optimization + QTextParagraph *p = c.paragraph(); + if ( p->mightHaveCustomItems || p == start.paragraph() || p == end.paragraph() || p == lastParagraph() ) { + c.gotoNextLetter(); + if ( p == lastParagraph() && c.atParagEnd() ) + break; + } else { + if ( p->document()->parent() ) + do { + c.gotoNextLetter(); + } while ( c.paragraph() == p ); + else + c.setParagraph( p->next() ); + } + } + + if ( !sel.swapped ) + sel.startCursor.paragraph()->setSelection( id, sel.startCursor.index(), sel.startCursor.paragraph()->length() - 1 ); + + sel.startCursor = start; + sel.endCursor = end; + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() ) + sel.swapped = sel.startCursor.index() > sel.endCursor.index(); + + setSelectionEndHelper( id, sel, start, end ); + + return TRUE; +} + +void QTextDocument::selectAll( int id ) +{ + removeSelection( id ); + + QTextDocumentSelection sel; + sel.swapped = FALSE; + QTextCursor c( this ); + + c.setParagraph( fParag ); + c.setIndex( 0 ); + sel.startCursor = c; + + c.setParagraph( lParag ); + c.setIndex( lParag->length() - 1 ); + sel.endCursor = c; + + selections.insert( id, sel ); + + QTextParagraph *p = fParag; + while ( p ) { + p->setSelection( id, 0, p->length() - 1 ); + p = p->next(); + } + + for ( QTextDocument *d = childList.first(); d; d = childList.next() ) + d->selectAll( id ); +} + +bool QTextDocument::removeSelection( int id ) +{ + if ( !selections.contains( id ) ) + return FALSE; + + QTextDocumentSelection &sel = selections[ id ]; + + QTextCursor start = sel.swapped ? sel.endCursor : sel.startCursor; + QTextCursor end = sel.swapped ? sel.startCursor : sel.endCursor; + QTextParagraph* p = 0; + while ( start != end ) { + if ( p != start.paragraph() ) { + p = start.paragraph(); + p->removeSelection( id ); + //### avoid endless loop by all means necessary, did somebody mention refactoring? + if ( !parent() && p == lParag ) + break; + } + start.gotoNextLetter(); + } + p = start.paragraph(); + p->removeSelection( id ); + selections.remove( id ); + return TRUE; +} + +QString QTextDocument::selectedText( int id, bool asRichText ) const +{ + QMap<int, QTextDocumentSelection>::ConstIterator it = selections.find( id ); + if ( it == selections.end() ) + return QString::null; + + QTextDocumentSelection sel = *it; + + + QTextCursor c1 = sel.startCursor; + QTextCursor c2 = sel.endCursor; + if ( sel.swapped ) { + c2 = sel.startCursor; + c1 = sel.endCursor; + } + + /* 3.0.3 improvement: Make it possible to get a reasonable + selection inside a table. This approach is very conservative: + make sure that both cursors have the same depth level and point + to paragraphs within the same text document. + + Meaning if you select text in two table cells, you will get the + entire table. This is still far better than the 3.0.2, where + you always got the entire table. + + ### Fix this properly when refactoring + */ + while ( c2.nestedDepth() > c1.nestedDepth() ) + c2.oneUp(); + while ( c1.nestedDepth() > c2.nestedDepth() ) + c1.oneUp(); + while ( c1.nestedDepth() && c2.nestedDepth() && + c1.paragraph()->document() != c2.paragraph()->document() ) { + c1.oneUp(); + c2.oneUp(); + } + // do not trust sel_swapped with tables. Fix this properly when refactoring as well + if ( c1.paragraph()->paragId() > c2.paragraph()->paragId() || + (c1.paragraph() == c2.paragraph() && c1.index() > c2.index() ) ) { + QTextCursor tmp = c1; + c2 = c1; + c1 = tmp; + } + + // end selection 3.0.3 improvement + + if ( asRichText && !parent() ) { + richTextExportStart = &c1; + richTextExportEnd = &c2; + + QString sel = richText(); + int from = sel.find( "<!--StartFragment-->" ); + if ( from >= 0 ) { + from += 20; + // find the previous span and move it into the start fragment before we clip it + QString prevspan; + int pspan = sel.findRev( "<span", from-21 ); + if ( pspan > sel.findRev( "</span", from-21 ) ) { + int spanend = sel.find( '>', pspan ); + prevspan = sel.mid( pspan, spanend - pspan + 1 ); + } + int to = sel.findRev( "<!--EndFragment-->" ); + if ( from <= to ) + sel = "<!--StartFragment-->" + prevspan + sel.mid( from, to - from ); + } + richTextExportStart = richTextExportEnd = 0; + return sel; + } + + QString s; + if ( c1.paragraph() == c2.paragraph() ) { + QTextParagraph *p = c1.paragraph(); + int end = c2.index(); + if ( p->at( QMAX( 0, end - 1 ) )->isCustom() ) + ++end; + if ( !p->mightHaveCustomItems ) { + s += p->string()->toString().mid( c1.index(), end - c1.index() ); + } else { + for ( int i = c1.index(); i < end; ++i ) { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else +#endif + { + s += p->at( i )->c; + } + } + } + } else { + QTextParagraph *p = c1.paragraph(); + int start = c1.index(); + while ( p ) { + int end = p == c2.paragraph() ? c2.index() : p->length() - 1; + if ( p == c2.paragraph() && p->at( QMAX( 0, end - 1 ) )->isCustom() ) + ++end; + if ( !p->mightHaveCustomItems ) { + s += p->string()->toString().mid( start, end - start ); + if ( p != c2.paragraph() ) + s += "\n"; + } else { + for ( int i = start; i < end; ++i ) { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else +#endif + { + s += p->at( i )->c; + } + } + } + start = 0; + if ( p == c2.paragraph() ) + break; + p = p->next(); + } + } + // ### workaround for plain text export until we get proper + // mime types: turn unicode line seperators into the more + // widely understood \n. Makes copy and pasting code snipplets + // from within Assistent possible + QChar* uc = (QChar*) s.unicode(); + for ( uint ii = 0; ii < s.length(); ii++ ) { + if ( uc[(int)ii] == QChar_linesep ) + uc[(int)ii] = QChar('\n'); + else if ( uc[(int)ii] == QChar::nbsp ) + uc[(int)ii] = QChar(' '); + } + return s; +} + +void QTextDocument::setFormat( int id, QTextFormat *f, int flags ) +{ + QMap<int, QTextDocumentSelection>::ConstIterator it = selections.find( id ); + if ( it == selections.end() ) + return; + + QTextDocumentSelection sel = *it; + + QTextCursor c1 = sel.startCursor; + QTextCursor c2 = sel.endCursor; + if ( sel.swapped ) { + c2 = sel.startCursor; + c1 = sel.endCursor; + } + + c2.restoreState(); + c1.restoreState(); + + if ( c1.paragraph() == c2.paragraph() ) { + c1.paragraph()->setFormat( c1.index(), c2.index() - c1.index(), f, TRUE, flags ); + return; + } + + c1.paragraph()->setFormat( c1.index(), c1.paragraph()->length() - c1.index(), f, TRUE, flags ); + QTextParagraph *p = c1.paragraph()->next(); + while ( p && p != c2.paragraph() ) { + p->setFormat( 0, p->length(), f, TRUE, flags ); + p = p->next(); + } + c2.paragraph()->setFormat( 0, c2.index(), f, TRUE, flags ); +} + +void QTextDocument::removeSelectedText( int id, QTextCursor *cursor ) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + + QTextDocumentSelection sel = *it; + QTextCursor c1 = sel.startCursor; + QTextCursor c2 = sel.endCursor; + if ( sel.swapped ) { + c2 = sel.startCursor; + c1 = sel.endCursor; + } + + // ### no support for editing tables yet + if ( c1.nestedDepth() || c2.nestedDepth() ) + return; + + c2.restoreState(); + c1.restoreState(); + + *cursor = c1; + removeSelection( id ); + + if ( c1.paragraph() == c2.paragraph() ) { + c1.paragraph()->remove( c1.index(), c2.index() - c1.index() ); + return; + } + + if ( c1.paragraph() == fParag && c1.index() == 0 && + c2.paragraph() == lParag && c2.index() == lParag->length() - 1 ) + cursor->setValid( FALSE ); + + bool didGoLeft = FALSE; + if ( c1.index() == 0 && c1.paragraph() != fParag ) { + cursor->gotoPreviousLetter(); + didGoLeft = cursor->isValid(); + } + + c1.paragraph()->remove( c1.index(), c1.paragraph()->length() - 1 - c1.index() ); + QTextParagraph *p = c1.paragraph()->next(); + int dy = 0; + QTextParagraph *tmp; + while ( p && p != c2.paragraph() ) { + tmp = p->next(); + dy -= p->rect().height(); + delete p; + p = tmp; + } + c2.paragraph()->remove( 0, c2.index() ); + while ( p ) { + p->move( dy ); + p->invalidate( 0 ); + p->setEndState( -1 ); + p = p->next(); + } + + + c1.paragraph()->join( c2.paragraph() ); + + if ( didGoLeft ) + cursor->gotoNextLetter(); +} + +void QTextDocument::indentSelection( int id ) +{ + QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + + QTextDocumentSelection sel = *it; + QTextParagraph *startParag = sel.startCursor.paragraph(); + QTextParagraph *endParag = sel.endCursor.paragraph(); + if ( sel.endCursor.paragraph()->paragId() < sel.startCursor.paragraph()->paragId() ) { + endParag = sel.startCursor.paragraph(); + startParag = sel.endCursor.paragraph(); + } + + QTextParagraph *p = startParag; + while ( p && p != endParag ) { + p->indent(); + p = p->next(); + } +} + +void QTextDocument::addCommand( QTextCommand *cmd ) +{ + commandHistory->addCommand( cmd ); +} + +QTextCursor *QTextDocument::undo( QTextCursor *c ) +{ + return commandHistory->undo( c ); +} + +QTextCursor *QTextDocument::redo( QTextCursor *c ) +{ + return commandHistory->redo( c ); +} + +bool QTextDocument::find( QTextCursor& cursor, const QString &expr, bool cs, bool wo, bool forward ) +{ + removeSelection( Standard ); + QTextParagraph *p = 0; + if ( expr.isEmpty() ) + return FALSE; + for (;;) { + if ( p != cursor.paragraph() ) { + p = cursor.paragraph(); + QString s = cursor.paragraph()->string()->toString(); + int start = cursor.index(); + for ( ;; ) { + int res = forward ? s.find( expr, start, cs ) : s.findRev( expr, start, cs ); + int end = res + expr.length(); + if ( res == -1 || ( !forward && start <= res ) ) + break; + if ( !wo || ( ( res == 0 || s[ res - 1 ].isSpace() || s[ res - 1 ].isPunct() ) && + ( end == (int)s.length() || s[ end ].isSpace() || s[ end ].isPunct() ) ) ) { + removeSelection( Standard ); + cursor.setIndex( forward ? end : res ); + setSelectionStart( Standard, cursor ); + cursor.setIndex( forward ? res : end ); + setSelectionEnd( Standard, cursor ); + if ( !forward ) + cursor.setIndex( res ); + return TRUE; + } + start = res + (forward ? 1 : -1); + } + } + if ( forward ) { + if ( cursor.paragraph() == lastParagraph() && cursor.atParagEnd() ) + break; + cursor.gotoNextLetter(); + } else { + if ( cursor.paragraph() == firstParagraph() && cursor.atParagStart() ) + break; + cursor.gotoPreviousLetter(); + } + } + return FALSE; +} + +void QTextDocument::setTextFormat( Qt::TextFormat f ) +{ + txtFormat = f; + if ( fParag == lParag && fParag->length() <= 1 ) + fParag->rtext = ( f == Qt::RichText ); +} + +Qt::TextFormat QTextDocument::textFormat() const +{ + return txtFormat; +} + +bool QTextDocument::inSelection( int selId, const QPoint &pos ) const +{ + QMap<int, QTextDocumentSelection>::ConstIterator it = selections.find( selId ); + if ( it == selections.end() ) + return FALSE; + + QTextDocumentSelection sel = *it; + QTextParagraph *startParag = sel.startCursor.paragraph(); + QTextParagraph *endParag = sel.endCursor.paragraph(); + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() && + sel.startCursor.paragraph()->selectionStart( selId ) == sel.endCursor.paragraph()->selectionEnd( selId ) ) + return FALSE; + if ( sel.endCursor.paragraph()->paragId() < sel.startCursor.paragraph()->paragId() ) { + endParag = sel.startCursor.paragraph(); + startParag = sel.endCursor.paragraph(); + } + + QTextParagraph *p = startParag; + while ( p ) { + if ( p->rect().contains( pos ) ) { + bool inSel = FALSE; + int selStart = p->selectionStart( selId ); + int selEnd = p->selectionEnd( selId ); + int y = 0; + int h = 0; + for ( int i = 0; i < p->length(); ++i ) { + if ( i == selStart ) + inSel = TRUE; + if ( i == selEnd ) + break; + if ( p->at( i )->lineStart ) { + y = (*p->lineStarts.find( i ))->y; + h = (*p->lineStarts.find( i ))->h; + } + if ( pos.y() - p->rect().y() >= y && pos.y() - p->rect().y() <= y + h ) { + if ( inSel && pos.x() >= p->at( i )->x && + pos.x() <= p->at( i )->x + p->at( i )->format()->width( p->at( i )->c ) ) + return TRUE; + } + } + } + if ( pos.y() < p->rect().y() ) + break; + if ( p == endParag ) + break; + p = p->next(); + } + + return FALSE; +} + +void QTextDocument::doLayout( QPainter *p, int w ) +{ + minw = wused = 0; + if ( !is_printer( p ) ) + p = 0; + withoutDoubleBuffer = ( p != 0 ); + QPainter * oldPainter = QTextFormat::painter(); + QTextFormat::setPainter( p ); + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; + flow_->setWidth( w ); + cw = w; + vw = w; + QTextParagraph *parag = fParag; + while ( parag ) { + parag->invalidate( 0 ); + if ( p ) + parag->adjustToPainter( p ); + parag->format(); + parag = parag->next(); + } + QTextFormat::setPainter( oldPainter ); +} + +QPixmap *QTextDocument::bufferPixmap( const QSize &s ) +{ + if ( !buf_pixmap ) + buf_pixmap = new QPixmap( s.expandedTo( QSize(1,1) ) ); + else if ( buf_pixmap->size() != s ) + buf_pixmap->resize( s.expandedTo( buf_pixmap->size() ) ); + return buf_pixmap; +} + +void QTextDocument::draw( QPainter *p, const QRect &rect, const QColorGroup &cg, const QBrush *paper ) +{ + if ( !firstParagraph() ) + return; + + if ( paper ) { + p->setBrushOrigin( -int( p->translationX() ), + -int( p->translationY() ) ); + + p->fillRect( rect, *paper ); + } + + QPainter * oldPainter = QTextFormat::painter(); + QTextFormat::setPainter( p ); + + if ( formatCollection()->defaultFormat()->color() != cg.text() ) + setDefaultFormat( formatCollection()->defaultFormat()->font(), cg.text() ); + + QTextParagraph *parag = firstParagraph(); + while ( parag ) { + if ( !parag->isValid() ) + parag->format(); + int y = parag->rect().y(); + QRect pr( parag->rect() ); + pr.setX( 0 ); + pr.setWidth( QWIDGETSIZE_MAX ); + if ( !rect.isNull() && !rect.intersects( pr ) ) { + parag = parag->next(); + continue; + } + p->translate( 0, y ); + if ( rect.isValid() ) + parag->paint( *p, cg, 0, FALSE, rect.x(), rect.y(), rect.width(), rect.height() ); + else + parag->paint( *p, cg, 0, FALSE ); + p->translate( 0, -y ); + parag = parag->next(); + if ( !flow()->isEmpty() ) + flow()->drawFloatingItems( p, rect.x(), rect.y(), rect.width(), rect.height(), cg, FALSE ); + } + QTextFormat::setPainter(oldPainter); +} + +void QTextDocument::drawParagraph( QPainter *p, QTextParagraph *parag, int cx, int cy, int cw, int ch, + QPixmap *&doubleBuffer, const QColorGroup &cg, + bool drawCursor, QTextCursor *cursor, bool resetChanged ) +{ + QPainter *painter = 0; + if ( resetChanged ) + parag->setChanged( FALSE ); + QRect ir( parag->rect() ); +#ifndef QT_NO_TEXTCUSTOMITEM + if (!parag->tableCell()) +#endif + ir.setWidth(width()); + + bool uDoubleBuffer = useDoubleBuffer( parag, p ); + + if ( uDoubleBuffer ) { + painter = new QPainter; + if ( cx >= 0 && cy >= 0 ) + ir = ir.intersect( QRect( cx, cy, cw, ch ) ); + if ( !doubleBuffer || + ir.width() > doubleBuffer->width() || + ir.height() > doubleBuffer->height() ) { + doubleBuffer = bufferPixmap( ir.size() ); + painter->begin( doubleBuffer ); + } else { + painter->begin( doubleBuffer ); + } + } else { + painter = p; + painter->translate( ir.x(), ir.y() ); + } + + painter->setBrushOrigin( -ir.x(), -ir.y() ); + + if ( uDoubleBuffer || is_printer( painter ) ) + painter->fillRect( QRect( 0, 0, ir.width(), ir.height() ), parag->backgroundBrush( cg ) ); + else if ( cursor && cursor->paragraph() == parag ) + painter->fillRect( QRect( parag->at( cursor->index() )->x, 0, 2, ir.height() ), + parag->backgroundBrush( cg ) ); + + painter->translate( -( ir.x() - parag->rect().x() ), + -( ir.y() - parag->rect().y() ) ); + parag->paint( *painter, cg, drawCursor ? cursor : 0, TRUE, cx, cy, cw, ch ); + + if ( uDoubleBuffer ) { + delete painter; + painter = 0; + p->drawPixmap( ir.topLeft(), *doubleBuffer, QRect( QPoint( 0, 0 ), ir.size() ) ); + } else { + painter->translate( -ir.x(), -ir.y() ); + } + + parag->document()->nextDoubleBuffered = FALSE; +} + +QTextParagraph *QTextDocument::draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg, + bool onlyChanged, bool drawCursor, QTextCursor *cursor, bool resetChanged ) +{ + if ( withoutDoubleBuffer || par && par->withoutDoubleBuffer ) { + withoutDoubleBuffer = TRUE; + QRect r; + draw( p, r, cg ); + return 0; + } + withoutDoubleBuffer = FALSE; + + if ( !firstParagraph() ) + return 0; + + QPainter * oldPainter = QTextFormat::painter(); + QTextFormat::setPainter( p ); + if ( formatCollection()->defaultFormat()->color() != cg.text() ) + setDefaultFormat( formatCollection()->defaultFormat()->font(), cg.text() ); + + if ( cx < 0 && cy < 0 ) { + cx = 0; + cy = 0; + cw = width(); + ch = height(); + } + + QTextParagraph *lastFormatted = 0; + QTextParagraph *parag = firstParagraph(); + + QPixmap *doubleBuffer = 0; + + while ( parag ) { + lastFormatted = parag; + if ( !parag->isValid() ) + parag->format(); + + QRect pr = parag->rect(); + pr.setWidth( parag->document()->width() ); + if ( pr.y() > cy + ch ) + goto floating; + QRect clipr( cx, cy, cw, ch ); + if ( !pr.intersects( clipr ) || ( onlyChanged && !parag->hasChanged() ) ) { + pr.setWidth( parag->document()->width() ); + parag = parag->next(); + continue; + } + + drawParagraph( p, parag, cx, cy, cw, ch, doubleBuffer, cg, drawCursor, cursor, resetChanged ); + parag = parag->next(); + } + + parag = lastParagraph(); + + floating: + if ( parag->rect().y() + parag->rect().height() < parag->document()->height() ) { + if ( !parag->document()->parent() ) { + QRect fillRect = QRect( 0, parag->rect().y() + parag->rect().height(), parag->document()->width(), + parag->document()->height() - ( parag->rect().y() + parag->rect().height() ) ); + if ( QRect( cx, cy, cw, ch ).intersects( fillRect ) ) + p->fillRect( fillRect, cg.brush( QColorGroup::Base ) ); + } + if ( !flow()->isEmpty() ) { + QRect cr( cx, cy, cw, ch ); + flow()->drawFloatingItems( p, cr.x(), cr.y(), cr.width(), cr.height(), cg, FALSE ); + } + } + + if ( buf_pixmap && buf_pixmap->height() > 300 ) { + delete buf_pixmap; + buf_pixmap = 0; + } + + QTextFormat::setPainter(oldPainter); + return lastFormatted; +} + +/* + #### this function only sets the default font size in the format collection + */ +void QTextDocument::setDefaultFormat( const QFont &font, const QColor &color ) +{ + bool reformat = font != fCollection->defaultFormat()->font(); + for ( QTextDocument *d = childList.first(); d; d = childList.next() ) + d->setDefaultFormat( font, color ); + fCollection->updateDefaultFormat( font, color, sheet_ ); + + if ( !reformat ) + return; + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; + + // invalidate paragraphs and custom items + QTextParagraph *p = fParag; + while ( p ) { + p->invalidate( 0 ); +#ifndef QT_NO_TEXTCUSTOMITEM + for ( int i = 0; i < p->length() - 1; ++i ) + if ( p->at( i )->isCustom() ) + p->at( i )->customItem()->invalidate(); +#endif + p = p->next(); + } +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void QTextDocument::registerCustomItem( QTextCustomItem *i, QTextParagraph *p ) +{ + if ( i && i->placement() != QTextCustomItem::PlaceInline ) { + flow_->registerFloatingItem( i ); + p->registerFloatingItem( i ); + } + if (i) i->setParagraph( p ); + p->mightHaveCustomItems = mightHaveCustomItems = TRUE; +} + +void QTextDocument::unregisterCustomItem( QTextCustomItem *i, QTextParagraph *p ) +{ + p->unregisterFloatingItem( i ); + i->setParagraph( 0 ); + flow_->unregisterFloatingItem( i ); +} +#endif + +bool QTextDocument::hasFocusParagraph() const +{ + return !!focusIndicator.parag; +} + +QString QTextDocument::focusHref() const +{ + return focusIndicator.href; +} + +QString QTextDocument::focusName() const +{ + return focusIndicator.name; +} + +bool QTextDocument::focusNextPrevChild( bool next ) +{ + if ( !focusIndicator.parag ) { + if ( next ) { + focusIndicator.parag = fParag; + focusIndicator.start = 0; + focusIndicator.len = 0; + } else { + focusIndicator.parag = lParag; + focusIndicator.start = lParag->length(); + focusIndicator.len = 0; + } + } else { + focusIndicator.parag->setChanged( TRUE ); + } + focusIndicator.href = QString::null; + focusIndicator.name = QString::null; + + if ( next ) { + QTextParagraph *p = focusIndicator.parag; + int index = focusIndicator.start + focusIndicator.len; + while ( p ) { + for ( int i = index; i < p->length(); ++i ) { + if ( p->at( i )->isAnchor() ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = p->at( i )->anchorHref(); + focusIndicator.name = p->at( i )->anchorName(); + while ( i < p->length() ) { + if ( !p->at( i )->isAnchor() ) + return TRUE; + focusIndicator.len++; + i++; + } +#ifndef QT_NO_TEXTCUSTOMITEM + } else if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + // first try to continue + QTextTableCell *c; + bool resetCells = TRUE; + for ( c = cells.first(); c; c = cells.next() ) { + if ( c->richText()->hasFocusParagraph() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } else { + resetCells = FALSE; + c = cells.next(); + break; + } + } + } + // now really try + if ( resetCells ) + c = cells.first(); + for ( ; c; c = cells.next() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } + } + } +#endif + } + } + index = 0; + p = p->next(); + } + } else { + QTextParagraph *p = focusIndicator.parag; + int index = focusIndicator.start - 1; + if ( focusIndicator.len == 0 && index < focusIndicator.parag->length() - 1 ) + index++; + while ( p ) { + for ( int i = index; i >= 0; --i ) { + if ( p->at( i )->isAnchor() ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = p->at( i )->anchorHref(); + focusIndicator.name = p->at( i )->anchorName(); + while ( i >= -1 ) { + if ( i < 0 || !p->at( i )->isAnchor() ) { + focusIndicator.start++; + return TRUE; + } + if ( i < 0 ) + break; + focusIndicator.len++; + focusIndicator.start--; + i--; + } +#ifndef QT_NO_TEXTCUSTOMITEM + } else if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + // first try to continue + QTextTableCell *c; + bool resetCells = TRUE; + for ( c = cells.last(); c; c = cells.prev() ) { + if ( c->richText()->hasFocusParagraph() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } else { + resetCells = FALSE; + c = cells.prev(); + break; + } + } + if ( cells.at() == 0 ) + break; + } + // now really try + if ( resetCells ) + c = cells.last(); + for ( ; c; c = cells.prev() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } + if ( cells.at() == 0 ) + break; + } + } +#endif + } + } + p = p->prev(); + if ( p ) + index = p->length() - 1; + } + } + + focusIndicator.parag = 0; + + return FALSE; +} + +int QTextDocument::length() const +{ + int l = -1; + QTextParagraph *p = fParag; + while ( p ) { + l += p->length(); + p = p->next(); + } + return QMAX(0,l); +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +int QTextFormat::width( const QChar &c ) const +{ + if ( c.unicode() == 0xad ) // soft hyphen + return 0; + if ( !pntr || !pntr->isActive() ) { + if ( c == '\t' ) + return fm.width( ' ' ); + if ( ha == AlignNormal ) { + int w; + if ( c.row() ) + w = fm.width( c ); + else + w = widths[ c.unicode() ]; + if ( w == 0 && !c.row() ) { + w = fm.width( c ); + ( (QTextFormat*)this )->widths[ c.unicode() ] = w; + } + return w; + } else { + QFont f( fn ); + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + QFontMetrics fm_( f ); + return fm_.width( c ); + } + } + + QFont f( fn ); + if ( ha != AlignNormal ) { + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + } + applyFont( f ); + + return pntr_fm->width( c ); +} + +int QTextFormat::width( const QString &str, int pos ) const +{ + int w = 0; + if ( str.unicode()[ pos ].unicode() == 0xad ) + return w; + if ( !pntr || !pntr->isActive() ) { + if ( ha == AlignNormal ) { + w = fm.charWidth( str, pos ); + } else { + QFont f( fn ); + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + QFontMetrics fm_( f ); + w = fm_.charWidth( str, pos ); + } + } else { + QFont f( fn ); + if ( ha != AlignNormal ) { + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + } + applyFont( f ); + w = pntr_fm->charWidth( str, pos ); + } + return w; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextString::QTextString() +{ + bidiDirty = TRUE; + bidi = FALSE; + rightToLeft = FALSE; + dir = QChar::DirON; +} + +QTextString::QTextString( const QTextString &s ) +{ + bidiDirty = TRUE; + bidi = s.bidi; + rightToLeft = s.rightToLeft; + dir = s.dir; + data = s.data; + data.detach(); + for ( int i = 0; i < (int)data.size(); ++i ) { + QTextFormat *f = data[i].format(); + if ( f ) + f->addRef(); + } +} + +void QTextString::insert( int index, const QString &s, QTextFormat *f ) +{ + insert( index, s.unicode(), s.length(), f ); +} + +void QTextString::insert( int index, const QChar *unicode, int len, QTextFormat *f ) +{ + int os = data.size(); + data.resize( data.size() + len, QGArray::SpeedOptim ); + if ( index < os ) { + memmove( data.data() + index + len, data.data() + index, + sizeof( QTextStringChar ) * ( os - index ) ); + } + QTextStringChar *ch = data.data() + index; + for ( int i = 0; i < len; ++i ) { + ch->x = 0; + ch->lineStart = 0; + ch->d.format = 0; + ch->nobreak = FALSE; + ch->type = QTextStringChar::Regular; + ch->d.format = f; + ch->rightToLeft = 0; + ch->c = unicode[i]; + ++ch; + } + bidiDirty = TRUE; +} + +QTextString::~QTextString() +{ + clear(); +} + +void QTextString::insert( int index, QTextStringChar *c, bool doAddRefFormat ) +{ + int os = data.size(); + data.resize( data.size() + 1, QGArray::SpeedOptim ); + if ( index < os ) { + memmove( data.data() + index + 1, data.data() + index, + sizeof( QTextStringChar ) * ( os - index ) ); + } + QTextStringChar &ch = data[ (int)index ]; + ch.c = c->c; + ch.x = 0; + ch.lineStart = 0; + ch.rightToLeft = 0; + ch.d.format = 0; + ch.type = QTextStringChar::Regular; + ch.nobreak = FALSE; + if ( doAddRefFormat && c->format() ) + c->format()->addRef(); + ch.setFormat( c->format() ); + bidiDirty = TRUE; +} + +int QTextString::appendParagraphs( QTextParagraph *start, QTextParagraph *end ) +{ + int paragCount = 0; + int newLength = data.size(); + QTextParagraph *p = start; + for (; p != end; p = p->next()) { + newLength += p->length(); + ++paragCount; + } + + const int oldLength = data.size(); + data.resize(newLength, QGArray::SpeedOptim); + + QTextStringChar *d = &data[oldLength]; + for (p = start; p != end; p = p->next()) { + const QTextStringChar * const src = p->at(0); + int i = 0; + for (; i < p->length() - 1; ++i) { + d[i].c = src[i].c; + d[i].x = 0; + d[i].lineStart = 0; + d[i].rightToLeft = 0; + d[i].type = QTextStringChar::Regular; + d[i].nobreak = FALSE; + d[i].d.format = src[i].format(); + if (d[i].d.format) + d[i].d.format->addRef(); + } + d[i].x = 0; + d[i].lineStart = 0; + d[i].nobreak = FALSE; + d[i].type = QTextStringChar::Regular; + d[i].d.format = 0; + d[i].rightToLeft = 0; + d[i].c = '\n'; + d += p->length(); + } + + bidiDirty = TRUE; + return paragCount; +} + +void QTextString::truncate( int index ) +{ + index = QMAX( index, 0 ); + index = QMIN( index, (int)data.size() - 1 ); + if ( index < (int)data.size() ) { + for ( int i = index + 1; i < (int)data.size(); ++i ) { + QTextStringChar &ch = data[ i ]; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !(ch.type == QTextStringChar::Regular) ) { + delete ch.customItem(); + if ( ch.d.custom->format ) + ch.d.custom->format->removeRef(); + delete ch.d.custom; + ch.d.custom = 0; + } else +#endif + if ( ch.format() ) { + ch.format()->removeRef(); + } + } + } + data.truncate( index ); + bidiDirty = TRUE; +} + +void QTextString::remove( int index, int len ) +{ + for ( int i = index; i < (int)data.size() && i - index < len; ++i ) { + QTextStringChar &ch = data[ i ]; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !(ch.type == QTextStringChar::Regular) ) { + delete ch.customItem(); + if ( ch.d.custom->format ) + ch.d.custom->format->removeRef(); + delete ch.d.custom; + ch.d.custom = 0; + } else +#endif + if ( ch.format() ) { + ch.format()->removeRef(); + } + } + memmove( data.data() + index, data.data() + index + len, + sizeof( QTextStringChar ) * ( data.size() - index - len ) ); + data.resize( data.size() - len, QGArray::SpeedOptim ); + bidiDirty = TRUE; +} + +void QTextString::clear() +{ + for ( int i = 0; i < (int)data.count(); ++i ) { + QTextStringChar &ch = data[ i ]; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !(ch.type == QTextStringChar::Regular) ) { + if ( ch.customItem() && ch.customItem()->placement() == QTextCustomItem::PlaceInline ) + delete ch.customItem(); + if ( ch.d.custom->format ) + ch.d.custom->format->removeRef(); + delete ch.d.custom; + ch.d.custom = 0; + } else +#endif + if ( ch.format() ) { + ch.format()->removeRef(); + } + } + data.resize( 0 ); + bidiDirty = TRUE; +} + +void QTextString::setFormat( int index, QTextFormat *f, bool useCollection ) +{ + QTextStringChar &ch = data[ index ]; + if ( useCollection && ch.format() ) + ch.format()->removeRef(); + ch.setFormat( f ); +} + +void QTextString::checkBidi() const +{ + QTextString *that = (QTextString *)this; + that->bidiDirty = FALSE; + int length = data.size(); + if ( !length ) { + that->bidi = FALSE; + that->rightToLeft = dir == QChar::DirR; + return; + } + const QTextStringChar *start = data.data(); + const QTextStringChar *end = start + length; + + ((QTextString *)this)->stringCache = toString(data); + + + // determines the properties we need for layouting + QTextEngine textEngine( toString(), 0 ); + textEngine.direction = (QChar::Direction) dir; + textEngine.itemize(QTextEngine::SingleLine); + const QCharAttributes *ca = textEngine.attributes() + length-1; + QTextStringChar *ch = (QTextStringChar *)end - 1; + QScriptItem *item = &textEngine.items[textEngine.items.size()-1]; + unsigned char bidiLevel = item->analysis.bidiLevel; + if ( bidiLevel ) + that->bidi = TRUE; + int pos = length-1; + while ( ch >= start ) { + if ( item->position > pos ) { + --item; + Q_ASSERT( item >= &textEngine.items[0] ); + Q_ASSERT( item < &textEngine.items[textEngine.items.size()] ); + bidiLevel = item->analysis.bidiLevel; + if ( bidiLevel ) + that->bidi = TRUE; + } + ch->softBreak = ca->softBreak; + ch->whiteSpace = ca->whiteSpace; + ch->charStop = ca->charStop; + ch->wordStop = ca->wordStop; + ch->bidiLevel = bidiLevel; + ch->rightToLeft = (bidiLevel%2); + --ch; + --ca; + --pos; + } + + if ( dir == QChar::DirR ) { + that->bidi = TRUE; + that->rightToLeft = TRUE; + } else if ( dir == QChar::DirL ) { + that->rightToLeft = FALSE; + } else { + that->rightToLeft = (textEngine.direction == QChar::DirR); + } +} + +void QTextDocument::setStyleSheet( QStyleSheet *s ) +{ + if ( !s ) + return; + sheet_ = s; + list_tm = list_bm = par_tm = par_bm = 12; + list_lm = 40; + li_tm = li_bm = 0; + QStyleSheetItem* item = s->item( "ol" ); + if ( item ) { + list_tm = QMAX(0,item->margin( QStyleSheetItem::MarginTop )); + list_bm = QMAX(0,item->margin( QStyleSheetItem::MarginBottom )); + list_lm = QMAX(0,item->margin( QStyleSheetItem::MarginLeft )); + } + if ( (item = s->item( "li" ) ) ) { + li_tm = QMAX(0,item->margin( QStyleSheetItem::MarginTop )); + li_bm = QMAX(0,item->margin( QStyleSheetItem::MarginBottom )); + } + if ( (item = s->item( "p" ) ) ) { + par_tm = QMAX(0,item->margin( QStyleSheetItem::MarginTop )); + par_bm = QMAX(0,item->margin( QStyleSheetItem::MarginBottom )); + } +} + +void QTextDocument::setUnderlineLinks( bool b ) { + underlLinks = b; + for ( QTextDocument *d = childList.first(); d; d = childList.next() ) + d->setUnderlineLinks( b ); +} + +void QTextStringChar::setFormat( QTextFormat *f ) +{ + if ( type == Regular ) { + d.format = f; + } else { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !d.custom ) { + d.custom = new CustomData; + d.custom->custom = 0; + } + d.custom->format = f; +#endif + } +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void QTextStringChar::setCustomItem( QTextCustomItem *i ) +{ + if ( type == Regular ) { + QTextFormat *f = format(); + d.custom = new CustomData; + d.custom->format = f; + } else { + delete d.custom->custom; + } + d.custom->custom = i; + type = (type == Anchor ? CustomAnchor : Custom); +} + +void QTextStringChar::loseCustomItem() +{ + if ( type == Custom ) { + QTextFormat *f = d.custom->format; + d.custom->custom = 0; + delete d.custom; + type = Regular; + d.format = f; + } else if ( type == CustomAnchor ) { + d.custom->custom = 0; + type = Anchor; + } +} + +#endif + +QString QTextStringChar::anchorName() const +{ + if ( type == Regular ) + return QString::null; + else + return d.custom->anchorName; +} + +QString QTextStringChar::anchorHref() const +{ + if ( type == Regular ) + return QString::null; + else + return d.custom->anchorHref; +} + +void QTextStringChar::setAnchor( const QString& name, const QString& href ) +{ + if ( type == Regular ) { + QTextFormat *f = format(); + d.custom = new CustomData; +#ifndef QT_NO_TEXTCUSTOMITEM + d.custom->custom = 0; +#endif + d.custom->format = f; + type = Anchor; + } else if ( type == Custom ) { + type = CustomAnchor; + } + d.custom->anchorName = name; + d.custom->anchorHref = href; +} + + +int QTextString::width( int idx ) const +{ + int w = 0; + QTextStringChar *c = &at( idx ); + if ( !c->charStop || c->c.unicode() == 0xad || c->c.unicode() == 0x2028 ) + return 0; +#ifndef QT_NO_TEXTCUSTOMITEM + if( c->isCustom() ) { + if( c->customItem()->placement() == QTextCustomItem::PlaceInline ) + w = c->customItem()->width; + } else +#endif + { + int r = c->c.row(); + if(r < 0x06 +#ifndef Q_WS_WIN + // Uniscribe's handling of Asian makes the condition below fail. + || (r > 0x1f && !(r > 0xd7 && r < 0xe0)) +#endif + ) { + w = c->format()->width( c->c ); + } else { + w = c->format()->width(toString(), idx); + } + } + return w; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextParagraph::QTextParagraph( QTextDocument *d, QTextParagraph *pr, QTextParagraph *nx, bool updateIds ) + : p( pr ), n( nx ), docOrPseudo( d ), + changed(FALSE), firstFormat(TRUE), firstPProcess(TRUE), needPreProcess(FALSE), fullWidth(TRUE), + lastInFrame(FALSE), visible(TRUE), breakable(TRUE), movedDown(FALSE), + mightHaveCustomItems(FALSE), hasdoc( d != 0 ), litem(FALSE), rtext(FALSE), + align( 0 ), lstyle( QStyleSheetItem::ListDisc ), invalid( 0 ), mSelections( 0 ), +#ifndef QT_NO_TEXTCUSTOMITEM + mFloatingItems( 0 ), +#endif + utm( 0 ), ubm( 0 ), ulm( 0 ), urm( 0 ), uflm( 0 ), ulinespacing( 0 ), + tabStopWidth(0), minwidth(0), tArray(0), eData( 0 ), ldepth( 0 ) +{ + lstyle = QStyleSheetItem::ListDisc; + if ( !hasdoc ) + docOrPseudo = new QTextParagraphPseudoDocument; + bgcol = 0; + list_val = -1; + paintdevice = 0; + QTextFormat* defFormat = formatCollection()->defaultFormat(); + if ( !hasdoc ) { + tabStopWidth = defFormat->width( 'x' ) * 8; + pseudoDocument()->commandHistory = new QTextCommandHistory( 100 ); + } + + if ( p ) + p->n = this; + if ( n ) + n->p = this; + + if ( !p && hasdoc ) + document()->setFirstParagraph( this ); + if ( !n && hasdoc ) + document()->setLastParagraph( this ); + + state = -1; + + if ( p ) + id = p->id + 1; + else + id = 0; + if ( n && updateIds ) { + QTextParagraph *s = n; + while ( s ) { + s->id = s->p->id + 1; + s->invalidateStyleCache(); + s = s->n; + } + } + + str = new QTextString(); + QChar ch(' '); + str->insert( 0, &ch, 1, formatCollection()->defaultFormat() ); +} + +QTextParagraph::~QTextParagraph() +{ + delete str; + if ( hasdoc ) { + register QTextDocument *doc = document(); + if ( this == doc->minwParag ) { + doc->minwParag = 0; + doc->minw = 0; + } + if ( this == doc->curParag ) + doc->curParag = 0; + } else { + delete pseudoDocument(); + } + delete [] tArray; + delete eData; + QMap<int, QTextLineStart*>::Iterator it = lineStarts.begin(); + for ( ; it != lineStarts.end(); ++it ) + delete *it; + if ( mSelections ) + delete mSelections; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( mFloatingItems ) + delete mFloatingItems; +#endif + if ( p ) + p->setNext( n ); + if ( n ) + n->setPrev( p ); + delete bgcol; +} + +void QTextParagraph::setNext( QTextParagraph *s ) +{ + n = s; + if ( !n && hasdoc ) + document()->setLastParagraph( this ); +} + +void QTextParagraph::setPrev( QTextParagraph *s ) +{ + p = s; + if ( !p && hasdoc ) + document()->setFirstParagraph( this ); +} + +void QTextParagraph::invalidate( int chr ) +{ + if ( invalid < 0 ) + invalid = chr; + else + invalid = QMIN( invalid, chr ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( mFloatingItems ) { + for ( QTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) + i->ypos = -1; + } +#endif + invalidateStyleCache(); +} + +void QTextParagraph::invalidateStyleCache() +{ + if ( list_val < 0 ) + list_val = -1; +} + + +void QTextParagraph::insert( int index, const QString &s ) +{ + insert( index, s.unicode(), s.length() ); +} + +void QTextParagraph::insert( int index, const QChar *unicode, int len ) +{ + if ( hasdoc && !document()->useFormatCollection() && document()->preProcessor() ) + str->insert( index, unicode, len, + document()->preProcessor()->format( QTextPreProcessor::Standard ) ); + else + str->insert( index, unicode, len, formatCollection()->defaultFormat() ); + invalidate( index ); + needPreProcess = TRUE; +} + +void QTextParagraph::truncate( int index ) +{ + str->truncate( index ); + insert( length(), " " ); + needPreProcess = TRUE; +} + +void QTextParagraph::remove( int index, int len ) +{ + if ( index + len - str->length() > 0 ) + return; +#ifndef QT_NO_TEXTCUSTOMITEM + for ( int i = index; i < index + len; ++i ) { + QTextStringChar *c = at( i ); + if ( hasdoc && c->isCustom() ) { + document()->unregisterCustomItem( c->customItem(), this ); + } + } +#endif + str->remove( index, len ); + invalidate( 0 ); + needPreProcess = TRUE; +} + +void QTextParagraph::join( QTextParagraph *s ) +{ + int oh = r.height() + s->r.height(); + n = s->n; + if ( n ) + n->p = this; + else if ( hasdoc ) + document()->setLastParagraph( this ); + + int start = str->length(); + if ( length() > 0 && at( length() - 1 )->c == ' ' ) { + remove( length() - 1, 1 ); + --start; + } + append( s->str->toString(), TRUE ); + + for ( int i = 0; i < s->length(); ++i ) { + if ( !hasdoc || document()->useFormatCollection() ) { + s->str->at( i ).format()->addRef(); + str->setFormat( i + start, s->str->at( i ).format(), TRUE ); + } +#ifndef QT_NO_TEXTCUSTOMITEM + if ( s->str->at( i ).isCustom() ) { + QTextCustomItem * item = s->str->at( i ).customItem(); + str->at( i + start ).setCustomItem( item ); + s->str->at( i ).loseCustomItem(); + if ( hasdoc ) { + document()->unregisterCustomItem( item, s ); + document()->registerCustomItem( item, this ); + } + } + if ( s->str->at( i ).isAnchor() ) { + str->at( i + start ).setAnchor( s->str->at( i ).anchorName(), + s->str->at( i ).anchorHref() ); + } +#endif + } + + if ( !extraData() && s->extraData() ) { + setExtraData( s->extraData() ); + s->setExtraData( 0 ); + } else if ( extraData() && s->extraData() ) { + extraData()->join( s->extraData() ); + } + delete s; + invalidate( 0 ); + r.setHeight( oh ); + needPreProcess = TRUE; + if ( n ) { + QTextParagraph *s = n; + s->invalidate( 0 ); + while ( s ) { + s->id = s->p->id + 1; + s->state = -1; + s->needPreProcess = TRUE; + s->changed = TRUE; + s->invalidateStyleCache(); + s = s->n; + } + } + format(); + state = -1; +} + +void QTextParagraph::move( int &dy ) +{ + if ( dy == 0 ) + return; + changed = TRUE; + r.moveBy( 0, dy ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( mFloatingItems ) { + for ( QTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) + i->ypos += dy; + } +#endif + if ( p ) + p->lastInFrame = TRUE; + + // do page breaks if required + if ( hasdoc && document()->isPageBreakEnabled() ) { + int shift; + if ( ( shift = document()->formatter()->formatVertically( document(), this ) ) ) { + if ( p ) + p->setChanged( TRUE ); + dy += shift; + } + } +} + +void QTextParagraph::format( int start, bool doMove ) +{ + if ( !str || str->length() == 0 || !formatter() ) + return; + + if ( hasdoc && + document()->preProcessor() && + ( needPreProcess || state == -1 ) ) + document()->preProcessor()->process( document(), this, invalid <= 0 ? 0 : invalid ); + needPreProcess = FALSE; + + if ( invalid == -1 ) + return; + + r.moveTopLeft( QPoint( documentX(), p ? p->r.y() + p->r.height() : documentY() ) ); + if ( p ) + p->lastInFrame = FALSE; + + movedDown = FALSE; + bool formattedAgain = FALSE; + + formatAgain: + + r.setWidth( documentWidth() ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( hasdoc && mFloatingItems ) { + for ( QTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) { + i->ypos = r.y(); + if ( i->placement() == QTextCustomItem::PlaceRight ) { + i->xpos = r.x() + r.width() - i->width; + } + } + } +#endif + QMap<int, QTextLineStart*> oldLineStarts = lineStarts; + lineStarts.clear(); + int y = formatter()->format( document(), this, start, oldLineStarts ); + + + r.setWidth( QMAX( r.width(), formatter()->minimumWidth() ) ); + + + QMap<int, QTextLineStart*>::Iterator it = oldLineStarts.begin(); + + for ( ; it != oldLineStarts.end(); ++it ) + delete *it; + + if ( !hasdoc ) { // qt_format_text bounding rect handling + it = lineStarts.begin(); + int usedw = 0; + for ( ; it != lineStarts.end(); ++it ) + usedw = QMAX( usedw, (*it)->w ); + if ( r.width() <= 0 ) { + // if the user specifies an invalid rect, this means that the + // bounding box should grow to the width that the text actually + // needs + r.setWidth( usedw ); + } else { + r.setWidth( QMIN( usedw, r.width() ) ); + } + } + + if ( y != r.height() ) + r.setHeight( y ); + + if ( !visible ) { + r.setHeight( 0 ); + } else { + int minw = minwidth = formatter()->minimumWidth(); + int wused = formatter()->widthUsed(); + wused = QMAX( minw, wused ); + if ( hasdoc ) { + document()->setMinimumWidth( minw, wused, this ); + } else { + pseudoDocument()->minw = QMAX( pseudoDocument()->minw, minw ); + pseudoDocument()->wused = QMAX( pseudoDocument()->wused, wused ); + } + } + + // do page breaks if required + if ( hasdoc && document()->isPageBreakEnabled() ) { + int shift = document()->formatter()->formatVertically( document(), this ); + if ( shift && !formattedAgain ) { + formattedAgain = TRUE; + goto formatAgain; + } + } + + if ( n && doMove && n->invalid == -1 && r.y() + r.height() != n->r.y() ) { + int dy = ( r.y() + r.height() ) - n->r.y(); + QTextParagraph *s = n; + bool makeInvalid = p && p->lastInFrame; + while ( s && dy ) { + if ( !s->isFullWidth() ) + makeInvalid = TRUE; + if ( makeInvalid ) + s->invalidate( 0 ); + s->move( dy ); + if ( s->lastInFrame ) + makeInvalid = TRUE; + s = s->n; + } + } + + firstFormat = FALSE; + changed = TRUE; + invalid = -1; + //##### string()->setTextChanged( FALSE ); +} + +int QTextParagraph::lineHeightOfChar( int i, int *bl, int *y ) const +{ + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.end(); + --it; + for ( ;; ) { + if ( i >= it.key() ) { + if ( bl ) + *bl = ( *it )->baseLine; + if ( y ) + *y = ( *it )->y; + return ( *it )->h; + } + if ( it == lineStarts.begin() ) + break; + --it; + } + + qWarning( "QTextParagraph::lineHeightOfChar: couldn't find lh for %d", i ); + return 15; +} + +QTextStringChar *QTextParagraph::lineStartOfChar( int i, int *index, int *line ) const +{ + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + int l = (int)lineStarts.count() - 1; + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.end(); + --it; + for ( ;; ) { + if ( i >= it.key() ) { + if ( index ) + *index = it.key(); + if ( line ) + *line = l; + return &str->at( it.key() ); + } + if ( it == lineStarts.begin() ) + break; + --it; + --l; + } + + qWarning( "QTextParagraph::lineStartOfChar: couldn't find %d", i ); + return 0; +} + +int QTextParagraph::lines() const +{ + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + return (int)lineStarts.count(); +} + +QTextStringChar *QTextParagraph::lineStartOfLine( int line, int *index ) const +{ + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + if ( line >= 0 && line < (int)lineStarts.count() ) { + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); + while ( line-- > 0 ) + ++it; + int i = it.key(); + if ( index ) + *index = i; + return &str->at( i ); + } + + qWarning( "QTextParagraph::lineStartOfLine: couldn't find %d", line ); + return 0; +} + +int QTextParagraph::leftGap() const +{ + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + if ( str->length() == 0) + return 0; + + int line = 0; + int x = str->length() ? str->at(0).x : 0; /* set x to x of first char */ + if ( str->isBidi() ) { + for ( int i = 1; i < str->length()-1; ++i ) + x = QMIN(x, str->at(i).x); + return x; + } + + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); + while (line < (int)lineStarts.count()) { + int i = it.key(); /* char index */ + x = QMIN(x, str->at(i).x); + ++it; + ++line; + } + return x; +} + +void QTextParagraph::setFormat( int index, int len, QTextFormat *f, bool useCollection, int flags ) +{ + if ( !f ) + return; + if ( index < 0 ) + index = 0; + if ( index > str->length() - 1 ) + index = str->length() - 1; + if ( index + len >= str->length() ) + len = str->length() - index; + + QTextFormatCollection *fc = 0; + if ( useCollection ) + fc = formatCollection(); + QTextFormat *of; + for ( int i = 0; i < len; ++i ) { + of = str->at( i + index ).format(); + if ( !changed && ( !of || f->key() != of->key() ) ) + changed = TRUE; + if ( invalid == -1 && + ( f->font().family() != of->font().family() || + f->font().pointSize() != of->font().pointSize() || + f->font().weight() != of->font().weight() || + f->font().italic() != of->font().italic() || + f->vAlign() != of->vAlign() ) ) { + invalidate( 0 ); + } + if ( flags == -1 || flags == QTextFormat::Format || !fc ) { + if ( fc ) + f = fc->format( f ); + str->setFormat( i + index, f, useCollection ); + } else { + QTextFormat *fm = fc->format( of, f, flags ); + str->setFormat( i + index, fm, useCollection ); + } + } +} + +void QTextParagraph::indent( int *oldIndent, int *newIndent ) +{ + if ( !hasdoc || !document()->indent() || isListItem() ) { + if ( oldIndent ) + *oldIndent = 0; + if ( newIndent ) + *newIndent = 0; + if ( oldIndent && newIndent ) + *newIndent = *oldIndent; + return; + } + document()->indent()->indent( document(), this, oldIndent, newIndent ); +} + +void QTextParagraph::paint( QPainter &painter, const QColorGroup &cg, QTextCursor *cursor, bool drawSelections, + int clipx, int clipy, int clipw, int cliph ) +{ + if ( !visible ) + return; + int i, y, h, baseLine, xstart, xend = 0; + i = y =h = baseLine = 0; + QRect cursorRect; + drawSelections &= ( mSelections != 0 ); + // macintosh full-width selection style + bool fullWidthStyle = QApplication::style().styleHint(QStyle::SH_RichText_FullWidthSelection); + int fullSelectionWidth = 0; + if ( drawSelections && fullWidthStyle ) + fullSelectionWidth = (hasdoc ? document()->width() : r.width()); + + QString qstr = str->toString(); + // detach string + qstr.setLength(qstr.length()); + // ### workaround so that \n are not drawn, actually this should + // be fixed in QFont somewhere (under Windows you get ugly boxes + // otherwise) + QChar* uc = (QChar*) qstr.unicode(); + for ( uint ii = 0; ii < qstr.length(); ii++ ) + if ( uc[(int)ii]== '\n' || uc[(int)ii] == '\t' ) + uc[(int)ii] = 0x20; + + int line = -1; + int paintStart = 0; + QTextStringChar *chr = 0; + QTextStringChar *nextchr = at( 0 ); + for ( i = 0; i < length(); i++ ) { + chr = nextchr; + if ( i < length()-1 ) + nextchr = at( i+1 ); + + // we flush at end of document + bool flush = (i == length()-1); + bool ignoreSoftHyphen = FALSE; + if ( !flush ) { + // we flush at end of line + flush |= nextchr->lineStart; + // we flush on format changes + flush |= ( nextchr->format() != chr->format() ); + // we flush on link changes + flush |= ( nextchr->isLink() != chr->isLink() ); + // we flush on start of run + flush |= ( nextchr->bidiLevel != chr->bidiLevel ); + // we flush on bidi changes + flush |= ( nextchr->rightToLeft != chr->rightToLeft ); + // we flush before and after tabs + flush |= ( chr->c == '\t' || nextchr->c == '\t' ); + // we flush on soft hypens + if (chr->c.unicode() == 0xad) { + flush = TRUE; + if (!nextchr->lineStart) + ignoreSoftHyphen = TRUE; + } + // we flush on custom items + flush |= chr->isCustom(); + // we flush before custom items + flush |= nextchr->isCustom(); + // when painting justified, we flush on spaces + if ((alignment() & Qt::AlignJustify) == Qt::AlignJustify ) + flush |= chr->whiteSpace; + } + + // init a new line + if ( chr->lineStart ) { + ++line; + paintStart = i; + lineInfo( line, y, h, baseLine ); + if ( clipy != -1 && cliph != 0 && y + r.y() - h > clipy + cliph ) { // outside clip area, leave + break; + } + + // if this is the first line and we are a list item, draw the the bullet label + if ( line == 0 && isListItem() ) { + int x = chr->x; + if (str->isBidi()) { + if (str->isRightToLeft()) { + x = chr->x + str->width(0); + for (int k = 1; k < length(); ++k) { + if (str->at(k).lineStart) + break; + x = QMAX(x, str->at(k).x + str->width(k)); + } + } else { + x = chr->x; + for (int k = 1; k < length(); ++k) { + if (str->at(k).lineStart) + break; + x = QMIN(x, str->at(k).x); + } + } + } + drawLabel( &painter, x, y, 0, 0, baseLine, cg ); + } + } + + // check for cursor mark + if ( cursor && this == cursor->paragraph() && i == cursor->index() ) { + QTextStringChar *c = i == 0 ? chr : chr - 1; + cursorRect.setRect( cursor->x() , y + baseLine - c->format()->ascent(), + 1, c->format()->height() ); + } + + if ( flush ) { // something changed, draw what we have so far + if ( chr->rightToLeft ) { + xstart = chr->x; + xend = at( paintStart )->x + str->width( paintStart ); + } else { + xstart = at( paintStart )->x; + xend = chr->x; + if ( i < length() - 1 ) { + if ( !str->at( i + 1 ).lineStart && + str->at( i + 1 ).rightToLeft == chr->rightToLeft ) + xend = str->at( i + 1 ).x; + else + xend += str->width( i ); + } + } + + if ( (clipx == -1 || clipw <= 0 || (xend >= clipx && xstart <= clipx + clipw)) && + ( clipy == -1 || clipy < y+r.y()+h ) ) { + if ( !chr->isCustom() ) + drawString( painter, qstr, paintStart, i - paintStart + (ignoreSoftHyphen ? 0 : 1), xstart, y, + baseLine, xend-xstart, h, drawSelections, fullSelectionWidth, + chr, cg, chr->rightToLeft ); +#ifndef QT_NO_TEXTCUSTOMITEM + else if ( chr->customItem()->placement() == QTextCustomItem::PlaceInline ) { + bool inSelection = FALSE; + if (drawSelections) { + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( QTextDocument::Standard ); + inSelection = (it != mSelections->end() && (*it).start <= i && (*it).end > i); + } + chr->customItem()->draw( &painter, chr->x, y, + clipx == -1 ? clipx : (clipx - r.x()), + clipy == -1 ? clipy : (clipy - r.y()), + clipw, cliph, cg, inSelection ); + } +#endif + } + paintStart = i+1; + } + + } + + // time to draw the cursor + const int cursor_extent = 4; + if ( !cursorRect.isNull() && cursor && + ((clipx == -1 || clipw == -1) || (cursorRect.right()+cursor_extent >= clipx && cursorRect.left()-cursor_extent <= clipx + clipw)) ) { + painter.fillRect( cursorRect, cg.color( QColorGroup::Text ) ); + painter.save(); + if ( string()->isBidi() ) { + if ( at( cursor->index() )->rightToLeft ) { + painter.setPen( Qt::black ); + painter.drawLine( cursorRect.x(), cursorRect.y(), cursorRect.x() - cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y() + cursor_extent, cursorRect.x() - cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + } else { + painter.setPen( Qt::black ); + painter.drawLine( cursorRect.x(), cursorRect.y(), cursorRect.x() + cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y() + cursor_extent, cursorRect.x() + cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + } + } + painter.restore(); + } +} + +//#define BIDI_DEBUG + +void QTextParagraph::setColorForSelection( QColor &color, QPainter &painter, + const QColorGroup& cg, int selection ) +{ + if (selection < 0) + return; + color = ( hasdoc && selection != QTextDocument::Standard ) ? + document()->selectionColor( selection ) : + cg.color( QColorGroup::Highlight ); + if ( selection == QTextDocument::IMCompositionText ) { +#ifndef Q_WS_MACX + int h1, s1, v1, h2, s2, v2; + cg.color( QColorGroup::Base ).hsv( &h1, &s1, &v1 ); + cg.color( QColorGroup::Background ).hsv( &h2, &s2, &v2 ); + color.setHsv( h1, s1, ( v1 + v2 ) / 2 ); +#else + color = Qt::lightGray; +#endif + painter.setPen( cg.color( QColorGroup::Text ) ); + } else if ( selection == QTextDocument::IMSelectionText ) { + color = cg.color( QColorGroup::Dark ); + painter.setPen( cg.color( QColorGroup::BrightText ) ); + } else if ( !hasdoc || document()->invertSelectionText( selection ) ) { + painter.setPen( cg.color( QColorGroup::HighlightedText ) ); + } +} + +void QTextParagraph::drawString( QPainter &painter, const QString &str, int start, int len, int xstart, + int y, int baseLine, int w, int h, bool drawSelections, int fullSelectionWidth, + QTextStringChar *formatChar, const QColorGroup& cg, + bool rightToLeft ) +{ + bool plainText = hasdoc ? document()->textFormat() == Qt::PlainText : FALSE; + QTextFormat* format = formatChar->format(); + + if ( !plainText || hasdoc && format->color() != document()->formatCollection()->defaultFormat()->color() ) + painter.setPen( QPen( format->color() ) ); + else + painter.setPen( cg.text() ); + painter.setFont( format->font() ); + + if ( hasdoc && formatChar->isAnchor() && !formatChar->anchorHref().isEmpty() ) { + if ( format->useLinkColor() ) + painter.setPen(document()->linkColor.isValid() ? document()->linkColor : cg.link()); + if ( document()->underlineLinks() ) { + QFont fn = format->font(); + fn.setUnderline( TRUE ); + painter.setFont( fn ); + } + } + + QPainter::TextDirection dir = rightToLeft ? QPainter::RTL : QPainter::LTR; + + int real_length = len; + if (len && dir != QPainter::RTL && start + len == length() ) // don't draw the last character (trailing space) + len--; + if (len && str.unicode()[start+len-1] == QChar_linesep) + len--; + + + QTextFormat::VerticalAlignment vAlign = format->vAlign(); + if ( vAlign != QTextFormat::AlignNormal ) { + // sub or superscript + QFont f( painter.font() ); + if ( format->fontSizesInPixels() ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + painter.setFont( f ); + int h = painter.fontMetrics().height(); + baseLine += (vAlign == QTextFormat::AlignSubScript) ? h/6 : -h/2; + } + + bool allSelected = FALSE; + if (drawSelections) { + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( QTextDocument::Standard ); + allSelected = (it != mSelections->end() && (*it).start <= start && (*it).end >= start+len); + } + if (!allSelected) + painter.drawText(xstart, y + baseLine, str, start, len, dir); + +#ifdef BIDI_DEBUG + painter.save(); + painter.setPen ( Qt::red ); + painter.drawLine( xstart, y, xstart, y + baseLine ); + painter.drawLine( xstart, y + baseLine/2, xstart + 10, y + baseLine/2 ); + int w = 0; + int i = 0; + while( i < len ) + w += painter.fontMetrics().charWidth( str, start + i++ ); + painter.setPen ( Qt::blue ); + painter.drawLine( xstart + w - 1, y, xstart + w - 1, y + baseLine ); + painter.drawLine( xstart + w - 1, y + baseLine/2, xstart + w - 1 - 10, y + baseLine/2 ); + painter.restore(); +#endif + + // check if we are in a selection and draw it + if (drawSelections) { + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->end(); + while ( it != mSelections->begin() ) { + --it; + int selStart = (*it).start; + int selEnd = (*it).end; + int tmpw = w; + + selStart = QMAX(selStart, start); + int real_selEnd = QMIN(selEnd, start+real_length); + selEnd = QMIN(selEnd, start+len); + bool extendRight = FALSE; + bool extendLeft = FALSE; + bool selWrap = (real_selEnd == length()-1 && n && n->hasSelection(it.key())); + if (selWrap || this->str->at(real_selEnd).lineStart) { + extendRight = (fullSelectionWidth != 0); + if (!extendRight && !rightToLeft) + tmpw += painter.fontMetrics().width(' '); + } + if (fullSelectionWidth && (selStart == 0 || this->str->at(selStart).lineStart)) { + extendLeft = TRUE; + } + if (this->str->isRightToLeft() != rightToLeft) + extendLeft = extendRight = FALSE; + + if (this->str->isRightToLeft()) { + bool tmp = extendLeft; + extendLeft = extendRight; + extendRight = tmp; + } + + if (selStart < real_selEnd || + selWrap && fullSelectionWidth && extendRight && + // don't draw the standard selection on a printer= + (it.key() != QTextDocument::Standard || !is_printer( &painter))) { + int selection = it.key(); + QColor color; + setColorForSelection( color, painter, cg, selection ); + if (selStart != start || selEnd != start + len || selWrap) { + // have to clip + painter.save(); + int cs, ce; + if (rightToLeft) { + cs = (selEnd != start + len) ? + this->str->at(this->str->previousCursorPosition(selEnd)).x : xstart; + ce = (selStart != start) ? + this->str->at(this->str->previousCursorPosition(selStart)).x : xstart+tmpw; + } else { + cs = (selStart != start) ? this->str->at(selStart).x : xstart; + ce = (selEnd != start + len) ? this->str->at(selEnd).x : xstart+tmpw; + } + QRect r(cs, y, ce-cs, h); + if (extendLeft) + r.setLeft(0); + if (extendRight) + r.setRight(fullSelectionWidth); + QRegion reg(r); + if ( painter.hasClipping() ) + reg &= painter.clipRegion(QPainter::CoordPainter); + painter.setClipRegion(reg, QPainter::CoordPainter); + } + int xleft = xstart; + if ( extendLeft ) { + tmpw += xstart; + xleft = 0; + } + if ( extendRight ) + tmpw = fullSelectionWidth - xleft; + painter.fillRect( xleft, y, tmpw, h, color ); + painter.drawText( xstart, y + baseLine, str, start, len, dir ); + // draw preedit's underline + if (selection == QTextDocument::IMCompositionText) + painter.drawLine(xstart, y + baseLine + 1, xstart + w, y + baseLine + 1); + if (selStart != start || selEnd != start + len || selWrap) + painter.restore(); + } + } + } + + if ( format->isMisspelled() ) { + painter.save(); + painter.setPen( QPen( Qt::red, 1, Qt::DotLine ) ); + painter.drawLine( xstart, y + baseLine + 1, xstart + w, y + baseLine + 1 ); + painter.restore(); + } + + if ( hasdoc && formatChar->isAnchor() && !formatChar->anchorHref().isEmpty() && + document()->focusIndicator.parag == this && + ( document()->focusIndicator.start >= start && + document()->focusIndicator.start + document()->focusIndicator.len <= start + len || + document()->focusIndicator.start <= start && + document()->focusIndicator.start + document()->focusIndicator.len >= start + len ) ) + painter.drawWinFocusRect( QRect( xstart, y, w, h ) ); +} + +void QTextParagraph::drawLabel( QPainter* p, int x, int y, int w, int h, int base, const QColorGroup& cg ) +{ + QRect r ( x, y, w, h ); + QStyleSheetItem::ListStyle s = listStyle(); + + p->save(); + QTextFormat *format = at( 0 )->format(); + if ( format ) { + p->setPen( format->color() ); + p->setFont( format->font() ); + } + QFontMetrics fm( p->fontMetrics() ); + int size = fm.lineSpacing() / 3; + + bool rtl = str->isRightToLeft(); + + switch ( s ) { + case QStyleSheetItem::ListDecimal: + case QStyleSheetItem::ListLowerAlpha: + case QStyleSheetItem::ListUpperAlpha: + { + if ( list_val == -1 ) { // uninitialised list value, calcluate the right one + int depth = listDepth(); + list_val--; + // ### evil, square and expensive. This needs to be done when formatting, not when painting + QTextParagraph* s = prev(); + int depth_s; + while ( s && (depth_s = s->listDepth()) >= depth ) { + if ( depth_s == depth && s->isListItem() ) + list_val--; + s = s->prev(); + } + } + + int n = list_val; + if ( n < -1 ) + n = -n - 1; + QString l; + switch ( s ) { + case QStyleSheetItem::ListLowerAlpha: + if ( n < 27 ) { + l = QChar( ('a' + (char) (n-1))); + break; + } + case QStyleSheetItem::ListUpperAlpha: + if ( n < 27 ) { + l = QChar( ('A' + (char) (n-1))); + break; + } + break; + default: //QStyleSheetItem::ListDecimal: + l.setNum( n ); + break; + } + if (rtl) + l.prepend(" ."); + else + l += QString::fromLatin1(". "); + int x = ( rtl ? r.left() : r.right() - fm.width(l)); + p->drawText( x, r.top() + base, l ); + } + break; + case QStyleSheetItem::ListSquare: + { + int x = rtl ? r.left() + size : r.right() - size*2; + QRect er( x, r.top() + fm.height() / 2 - size / 2, size, size ); + p->fillRect( er , cg.brush( QColorGroup::Text ) ); + } + break; + case QStyleSheetItem::ListCircle: + { + int x = rtl ? r.left() + size : r.right() - size*2; + QRect er( x, r.top() + fm.height() / 2 - size / 2, size, size); + p->drawEllipse( er ); + } + break; + case QStyleSheetItem::ListDisc: + default: + { + p->setBrush( cg.brush( QColorGroup::Text )); + int x = rtl ? r.left() + size : r.right() - size*2; + QRect er( x, r.top() + fm.height() / 2 - size / 2, size, size); + p->drawEllipse( er ); + p->setBrush( Qt::NoBrush ); + } + break; + } + + p->restore(); +} + +#ifndef QT_NO_DATASTREAM +void QTextParagraph::readStyleInformation( QDataStream& stream ) +{ + int int_align, int_lstyle; + uchar uchar_litem, uchar_rtext, uchar_dir; + stream >> int_align >> int_lstyle >> utm >> ubm >> ulm >> urm >> uflm + >> ulinespacing >> ldepth >> uchar_litem >> uchar_rtext >> uchar_dir; + align = int_align; lstyle = (QStyleSheetItem::ListStyle) int_lstyle; + litem = uchar_litem; rtext = uchar_rtext; str->setDirection( (QChar::Direction)uchar_dir ); + QTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +void QTextParagraph::writeStyleInformation( QDataStream& stream ) const +{ + stream << (int) align << (int) lstyle << utm << ubm << ulm << urm << uflm << ulinespacing << ldepth << (uchar)litem << (uchar)rtext << (uchar)str->direction(); +} +#endif + + +void QTextParagraph::setListItem( bool li ) +{ + if ( (bool)litem == li ) + return; + litem = li; + changed = TRUE; + QTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +void QTextParagraph::setListDepth( int depth ) { + if ( !hasdoc || depth == ldepth ) + return; + ldepth = depth; + QTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +int *QTextParagraph::tabArray() const +{ + int *ta = tArray; + if ( !ta && hasdoc ) + ta = document()->tabArray(); + return ta; +} + +int QTextParagraph::nextTab( int, int x ) +{ + int *ta = tArray; + if ( hasdoc ) { + if ( !ta ) + ta = document()->tabArray(); + tabStopWidth = document()->tabStopWidth(); + } + if ( ta ) { + int i = 0; + while ( ta[ i ] ) { + if ( ta[ i ] >= x ) + return tArray[ i ]; + ++i; + } + return tArray[ 0 ]; + } else { + int d; + if ( tabStopWidth != 0 ) + d = x / tabStopWidth; + else + return x; + return tabStopWidth * ( d + 1 ); + } +} + +void QTextParagraph::adjustToPainter( QPainter *p ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + for ( int i = 0; i < length(); ++i ) { + if ( at( i )->isCustom() ) + at( i )->customItem()->adjustToPainter( p ); + } +#endif +} + +QTextFormatCollection *QTextParagraph::formatCollection() const +{ + if ( hasdoc ) + return document()->formatCollection(); + QTextFormatCollection* fc = &pseudoDocument()->collection; + if ( paintdevice != fc->paintDevice() ) + fc->setPaintDevice( paintdevice ); + return fc; +} + +QString QTextParagraph::richText() const +{ + QString s; + QTextStringChar *formatChar = 0; + QString spaces; + bool doStart = richTextExportStart && richTextExportStart->paragraph() == this; + bool doEnd = richTextExportEnd && richTextExportEnd->paragraph() == this; + int i; + QString lastAnchorName; + for ( i = 0; i < length()-1; ++i ) { + if ( doStart && i && richTextExportStart->index() == i ) + s += "<!--StartFragment-->"; + if ( doEnd && richTextExportEnd->index() == i ) + s += "<!--EndFragment-->"; + QTextStringChar *c = &str->at( i ); + if ( c->isAnchor() && !c->anchorName().isEmpty() && c->anchorName() != lastAnchorName ) { + lastAnchorName = c->anchorName(); + if ( c->anchorName().contains( '#' ) ) { + QStringList l = QStringList::split( '#', c->anchorName() ); + for ( QStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) + s += "<a name=\"" + *it + "\"></a>"; + } else { + s += "<a name=\"" + c->anchorName() + "\"></a>"; + } + } + if ( !formatChar ) { + s += c->format()->makeFormatChangeTags( formatCollection()->defaultFormat(), + 0, QString::null, c->anchorHref() ); + formatChar = c; + } else if ( ( formatChar->format()->key() != c->format()->key() ) || + (c->anchorHref() != formatChar->anchorHref() ) ) { + s += c->format()->makeFormatChangeTags( formatCollection()->defaultFormat(), + formatChar->format() , formatChar->anchorHref(), c->anchorHref() ); + formatChar = c; + } + if ( c->c == '<' ) + s += "<"; + else if ( c->c == '>' ) + s += ">"; + else if ( c->c =='&' ) + s += "&"; + else if ( c->c =='\"' ) + s += """; +#ifndef QT_NO_TEXTCUSTOMITEM + else if ( c->isCustom() ) + s += c->customItem()->richText(); +#endif + else if ( c->c == '\n' || c->c == QChar_linesep ) + s += "<br />"; // space on purpose for compatibility with Netscape, Lynx & Co. + else + s += c->c; + } + if ( doEnd && richTextExportEnd->index() == i ) + s += "<!--EndFragment-->"; + if ( formatChar ) + s += formatChar->format()->makeFormatEndTags( formatCollection()->defaultFormat(), formatChar->anchorHref() ); + return s; +} + +void QTextParagraph::addCommand( QTextCommand *cmd ) +{ + if ( !hasdoc ) + pseudoDocument()->commandHistory->addCommand( cmd ); + else + document()->commands()->addCommand( cmd ); +} + +QTextCursor *QTextParagraph::undo( QTextCursor *c ) +{ + if ( !hasdoc ) + return pseudoDocument()->commandHistory->undo( c ); + return document()->commands()->undo( c ); +} + +QTextCursor *QTextParagraph::redo( QTextCursor *c ) +{ + if ( !hasdoc ) + return pseudoDocument()->commandHistory->redo( c ); + return document()->commands()->redo( c ); +} + +int QTextParagraph::topMargin() const +{ + int m = 0; + if ( rtext ) { + m = isListItem() ? (document()->li_tm/QMAX(1,listDepth()*listDepth())) : + ( listDepth() ? 0 : document()->par_tm ); + if ( listDepth() == 1 &&( !prev() || prev()->listDepth() < listDepth() ) ) + m = QMAX( m, document()->list_tm ); + } + m += utm; + return scale( m, QTextFormat::painter() ); +} + +int QTextParagraph::bottomMargin() const +{ + int m = 0; + if ( rtext ) { + m = isListItem() ? (document()->li_bm/QMAX(1,listDepth()*listDepth())) : + ( listDepth() ? 0 : document()->par_bm ); + if ( listDepth() == 1 &&( !next() || next()->listDepth() < listDepth() ) ) + m = QMAX( m, document()->list_bm ); + } + m += ubm; + return scale( m, QTextFormat::painter() ); +} + +int QTextParagraph::leftMargin() const +{ + int m = ulm; + if ( listDepth() && !string()->isRightToLeft() ) + m += listDepth() * document()->list_lm; + return scale( m, QTextFormat::painter() ); +} + +int QTextParagraph::firstLineMargin() const +{ + int m = uflm; + return scale( m, QTextFormat::painter() ); +} + +int QTextParagraph::rightMargin() const +{ + int m = urm; + if ( listDepth() && string()->isRightToLeft() ) + m += listDepth() * document()->list_lm; + return scale( m, QTextFormat::painter() ); +} + +int QTextParagraph::lineSpacing() const +{ + int l = ulinespacing; + l = scale( l, QTextFormat::painter() ); + return l; +} + +void QTextParagraph::copyParagData( QTextParagraph *parag ) +{ + rtext = parag->rtext; + lstyle = parag->lstyle; + ldepth = parag->ldepth; + litem = parag->litem; + align = parag->align; + utm = parag->utm; + ubm = parag->ubm; + urm = parag->urm; + ulm = parag->ulm; + uflm = parag->uflm; + ulinespacing = parag->ulinespacing; + QColor *c = parag->backgroundColor(); + if ( c ) + setBackgroundColor( *c ); + str->setDirection( parag->str->direction() ); +} + +void QTextParagraph::show() +{ + if ( visible || !hasdoc ) + return; + visible = TRUE; +} + +void QTextParagraph::hide() +{ + if ( !visible || !hasdoc ) + return; + visible = FALSE; +} + +void QTextParagraph::setDirection( QChar::Direction d ) +{ + if ( str && str->direction() != d ) { + str->setDirection( d ); + invalidate( 0 ); + } +} + +QChar::Direction QTextParagraph::direction() const +{ + return (str ? str->direction() : QChar::DirON ); +} + +void QTextParagraph::setChanged( bool b, bool recursive ) +{ + changed = b; + if ( recursive ) { + if ( document() && document()->parentParagraph() ) + document()->parentParagraph()->setChanged( b, recursive ); + } +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +QTextPreProcessor::QTextPreProcessor() +{ +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextFormatter::QTextFormatter() + : thisminw(0), thiswused(0), wrapEnabled( TRUE ), wrapColumn( -1 ), biw( FALSE ) +{ +} + +QTextLineStart *QTextFormatter::formatLine( QTextParagraph *parag, QTextString *string, QTextLineStart *line, + QTextStringChar *startChar, QTextStringChar *lastChar, int align, int space ) +{ + if ( lastChar < startChar ) + return new QTextLineStart; +#ifndef QT_NO_COMPLEXTEXT + if( string->isBidi() ) + return bidiReorderLine( parag, string, line, startChar, lastChar, align, space ); +#endif + int start = (startChar - &string->at(0)); + int last = (lastChar - &string->at(0) ); + + // ignore white space at the end of the line. + QTextStringChar *ch = lastChar; + while ( ch > startChar && ch->whiteSpace ) { + space += ch->format()->width( ' ' ); + --ch; + } + + if (space < 0) + space = 0; + + // do alignment Auto == Left in this case + if ( align & Qt::AlignHCenter || align & Qt::AlignRight ) { + if ( align & Qt::AlignHCenter ) + space /= 2; + for ( int j = start; j <= last; ++j ) + string->at( j ).x += space; + } else if ( align & Qt::AlignJustify ) { + int numSpaces = 0; + // End at "last-1", the last space ends up with a width of 0 + for ( int j = last-1; j >= start; --j ) { + // Start at last tab, if any. + QTextStringChar &ch = string->at( j ); + if ( ch.c == '\t' ) { + start = j+1; + break; + } + if(ch.whiteSpace) + numSpaces++; + } + int toAdd = 0; + for ( int k = start + 1; k <= last; ++k ) { + QTextStringChar &ch = string->at( k ); + if( numSpaces && ch.whiteSpace ) { + int s = space / numSpaces; + toAdd += s; + space -= s; + numSpaces--; + } + string->at( k ).x += toAdd; + } + } + + if ( last >= 0 && last < string->length() ) + line->w = string->at( last ).x + string->width( last ); + else + line->w = 0; + + return new QTextLineStart; +} + +#ifndef QT_NO_COMPLEXTEXT + +#ifdef BIDI_DEBUG +#include <iostream> +#endif + +// collects one line of the paragraph and transforms it to visual order +QTextLineStart *QTextFormatter::bidiReorderLine( QTextParagraph * /*parag*/, QTextString *text, QTextLineStart *line, + QTextStringChar *startChar, QTextStringChar *lastChar, int align, int space ) +{ + // ignore white space at the end of the line. + int endSpaces = 0; + while ( lastChar > startChar && lastChar->whiteSpace ) { + space += lastChar->format()->width( ' ' ); + --lastChar; + ++endSpaces; + } + + int start = (startChar - &text->at(0)); + int last = (lastChar - &text->at(0) ); + + int length = lastChar - startChar + 1; + + + int x = startChar->x; + + unsigned char _levels[256]; + int _visual[256]; + + unsigned char *levels = _levels; + int *visual = _visual; + + if ( length > 255 ) { + levels = (unsigned char *)malloc( length*sizeof( unsigned char ) ); + visual = (int *)malloc( length*sizeof( int ) ); + } + + //qDebug("bidiReorderLine: length=%d (%d-%d)", length, start, last ); + + QTextStringChar *ch = startChar; + unsigned char *l = levels; + while ( ch <= lastChar ) { + //qDebug( " level: %d", ch->bidiLevel ); + *(l++) = (ch++)->bidiLevel; + } + + QTextEngine::bidiReorder( length, levels, visual ); + + // now construct the reordered string out of the runs... + + int numSpaces = 0; + // set the correct alignment. This is a bit messy.... + if( align == Qt::AlignAuto ) { + // align according to directionality of the paragraph... + if ( text->isRightToLeft() ) + align = Qt::AlignRight; + } + + // This is not really correct, but as we can't make the scrollbar move to the left of the origin, + // this ensures all text can be scrolled to and read. + if (space < 0) + space = 0; + + if ( align & Qt::AlignHCenter ) + x += space/2; + else if ( align & Qt::AlignRight ) + x += space; + else if ( align & Qt::AlignJustify ) { + // End at "last-1", the last space ends up with a width of 0 + for ( int j = last-1; j >= start; --j ) { + // Start at last tab, if any. + QTextStringChar &ch = text->at( j ); + if ( ch.c == '\t' ) { + start = j+1; + break; + } + if(ch.whiteSpace) + numSpaces++; + } + } + + int toAdd = 0; + int xorig = x; + QTextStringChar *lc = startChar + visual[0]; + for ( int i = 0; i < length; i++ ) { + QTextStringChar *ch = startChar + visual[i]; + if (numSpaces && ch->whiteSpace) { + int s = space / numSpaces; + toAdd += s; + space -= s; + numSpaces--; + } + + if (lc->format() != ch->format() && !ch->c.isSpace() + && lc->format()->font().italic() && !ch->format()->font().italic()) { + int rb = lc->format()->fontMetrics().rightBearing(lc->c); + if (rb < 0) + x -= rb; + } + + ch->x = x + toAdd; + ch->rightToLeft = ch->bidiLevel % 2; + //qDebug("visual: %d (%x) placed at %d rightToLeft=%d", visual[i], ch->c.unicode(), x +toAdd, ch->rightToLeft ); + int ww = 0; + if ( ch->c.unicode() >= 32 || ch->c == '\t' || ch->c == '\n' || ch->isCustom() ) { + ww = text->width( start+visual[i] ); + } else { + ww = ch->format()->width( ' ' ); + } + x += ww; + lc = ch; + } + x += toAdd; + + while ( endSpaces-- ) { + ++lastChar; + int sw = lastChar->format()->width( ' ' ); + if ( text->isRightToLeft() ) { + xorig -= sw; + lastChar->x = xorig; + ch->rightToLeft = TRUE; + } else { + lastChar->x = x; + x += sw; + ch->rightToLeft = FALSE; + } + } + + line->w = x; + + if ( length > 255 ) { + free( levels ); + free( visual ); + } + + return new QTextLineStart; +} +#endif + + +void QTextFormatter::insertLineStart( QTextParagraph *parag, int index, QTextLineStart *ls ) +{ + QMap<int, QTextLineStart*>::Iterator it; + if ( ( it = parag->lineStartList().find( index ) ) == parag->lineStartList().end() ) { + parag->lineStartList().insert( index, ls ); + } else { + delete *it; + parag->lineStartList().remove( it ); + parag->lineStartList().insert( index, ls ); + } +} + + +/* Standard pagebreak algorithm using QTextFlow::adjustFlow. Returns + the shift of the paragraphs bottom line. + */ +int QTextFormatter::formatVertically( QTextDocument* doc, QTextParagraph* parag ) +{ + int oldHeight = parag->rect().height(); + QMap<int, QTextLineStart*>& lineStarts = parag->lineStartList(); + QMap<int, QTextLineStart*>::Iterator it = lineStarts.begin(); + int h = parag->prev() ? QMAX(parag->prev()->bottomMargin(),parag->topMargin() ) / 2: 0; + for ( ; it != lineStarts.end() ; ++it ) { + QTextLineStart * ls = it.data(); + ls->y = h; + QTextStringChar *c = ¶g->string()->at(it.key()); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( c && c->customItem() && c->customItem()->ownLine() ) { + int h = c->customItem()->height; + c->customItem()->pageBreak( parag->rect().y() + ls->y + ls->baseLine - h, doc->flow() ); + int delta = c->customItem()->height - h; + ls->h += delta; + if ( delta ) + parag->setMovedDown( TRUE ); + } else +#endif + { + + int shift = doc->flow()->adjustFlow( parag->rect().y() + ls->y, ls->w, ls->h ); + ls->y += shift; + if ( shift ) + parag->setMovedDown( TRUE ); + } + h = ls->y + ls->h; + } + int m = parag->bottomMargin(); + if ( !parag->next() ) + m = 0; + else + m = QMAX(m, parag->next()->topMargin() ) / 2; + h += m; + parag->setHeight( h ); + return h - oldHeight; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextFormatterBreakInWords::QTextFormatterBreakInWords() +{ +} + +#define SPACE(s) s + +int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParagraph *parag, + int start, const QMap<int, QTextLineStart*> & ) +{ + // make sure bidi information is correct. + (void )parag->string()->isBidi(); + + QTextStringChar *c = 0; + QTextStringChar *firstChar = 0; + int left = doc ? parag->leftMargin() + doc->leftMargin() : 0; + int x = left + ( doc ? parag->firstLineMargin() : 0 ); + int dw = parag->documentVisibleWidth() - ( doc ? doc->rightMargin() : 0 ); + int y = parag->prev() ? QMAX(parag->prev()->bottomMargin(),parag->topMargin()) / 2: 0; + int h = y; + int len = parag->length(); + if ( doc ) + x = doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), x, 4 ); + int rm = parag->rightMargin(); + int w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + bool fullWidth = TRUE; + int minw = 0; + int wused = 0; + bool wrapEnabled = isWrapEnabled( parag ); + + start = 0; //######### what is the point with start?! (Matthias) + if ( start == 0 ) + c = ¶g->string()->at( 0 ); + + int i = start; + QTextLineStart *lineStart = new QTextLineStart( y, y, 0 ); + insertLineStart( parag, 0, lineStart ); + + QPainter *painter = QTextFormat::painter(); + + int col = 0; + int ww = 0; + QChar lastChr; + for ( ; i < len; ++i, ++col ) { + if ( c ) + lastChr = c->c; + c = ¶g->string()->at( i ); + // ### the lines below should not be needed + if ( painter ) + c->format()->setPainter( painter ); + if ( i > 0 ) { + c->lineStart = 0; + } else { + c->lineStart = 1; + firstChar = c; + } + if ( c->c.unicode() >= 32 || c->isCustom() ) { + ww = parag->string()->width( i ); + } else if ( c->c == '\t' ) { + int nx = parag->nextTab( i, x - left ) + left; + if ( nx < x ) + ww = w - x; + else + ww = nx - x; + } else { + ww = c->format()->width( ' ' ); + } + +#ifndef QT_NO_TEXTCUSTOMITEM + if ( c->isCustom() && c->customItem()->ownLine() ) { + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + c->customItem()->resize( w - x ); + w = dw; + y += h; + h = c->height(); + lineStart = new QTextLineStart( y, h, h ); + insertLineStart( parag, i, lineStart ); + c->lineStart = 1; + firstChar = c; + x = 0xffffff; + continue; + } +#endif + + if ( wrapEnabled && + ( wrapAtColumn() == -1 && x + ww > w || + wrapAtColumn() != -1 && col >= wrapAtColumn() ) ) { + x = doc ? parag->document()->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw; + y += h; + h = c->height(); + lineStart = formatLine( parag, parag->string(), lineStart, firstChar, c-1 ); + lineStart->y = y; + insertLineStart( parag, i, lineStart ); + lineStart->baseLine = c->ascent(); + lineStart->h = c->height(); + c->lineStart = 1; + firstChar = c; + col = 0; + if ( wrapAtColumn() != -1 ) + minw = QMAX( minw, w ); + } else if ( lineStart ) { + lineStart->baseLine = QMAX( lineStart->baseLine, c->ascent() ); + h = QMAX( h, c->height() ); + lineStart->h = h; + } + + c->x = x; + x += ww; + wused = QMAX( wused, x ); + } + + int m = parag->bottomMargin(); + if ( !parag->next() ) + m = 0; + else + m = QMAX(m, parag->next()->topMargin() ) / 2; + parag->setFullWidth( fullWidth ); + y += h + m; + if ( doc ) + minw += doc->rightMargin(); + if ( !wrapEnabled ) + minw = QMAX(minw, wused); + + thisminw = minw; + thiswused = wused; + return y; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextFormatterBreakWords::QTextFormatterBreakWords() +{ +} + +#define DO_FLOW( lineStart ) do{ if ( doc && doc->isPageBreakEnabled() ) { \ + int yflow = lineStart->y + parag->rect().y();\ + int shift = doc->flow()->adjustFlow( yflow, dw, lineStart->h ); \ + lineStart->y += shift;\ + y += shift;\ + }}while(FALSE) + +int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParagraph *parag, + int start, const QMap<int, QTextLineStart*> & ) +{ + // make sure bidi information is correct. + (void )parag->string()->isBidi(); + + QTextStringChar *c = 0; + QTextStringChar *firstChar = 0; + QTextString *string = parag->string(); + int left = doc ? parag->leftMargin() + doc->leftMargin() : 0; + int x = left + ( doc ? parag->firstLineMargin() : 0 ); + int y = parag->prev() ? QMAX(parag->prev()->bottomMargin(),parag->topMargin()) / 2: 0; + int h = y; + int len = parag->length(); + if ( doc ) + x = doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), x, 0 ); + int dw = parag->documentVisibleWidth() - ( doc ? ( left != x ? 0 : doc->rightMargin() ) : 0 ); + + int curLeft = x; + int rm = parag->rightMargin(); + int rdiff = doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 0 ) : 0; + int w = dw - rdiff; + bool fullWidth = TRUE; + int marg = left + rdiff; + int minw = 0; + int wused = 0; + int tminw = marg; + int linespacing = doc ? parag->lineSpacing() : 0; + bool wrapEnabled = isWrapEnabled( parag ); + + start = 0; + + int i = start; + QTextLineStart *lineStart = new QTextLineStart( y, y, 0 ); + insertLineStart( parag, 0, lineStart ); + int lastBreak = -1; + int tmpBaseLine = 0, tmph = 0; + bool lastWasNonInlineCustom = FALSE; + + int align = parag->alignment(); + if ( align == Qt::AlignAuto && doc && doc->alignment() != Qt::AlignAuto ) + align = doc->alignment(); + + align &= Qt::AlignHorizontal_Mask; + + // ### hack. The last char in the paragraph is always invisible, + // ### and somehow sometimes has a wrong format. It changes + // ### between // layouting and printing. This corrects some + // ### layouting errors in BiDi mode due to this. + if ( len > 1 ) { + c = ¶g->string()->at(len - 1); + if (!c->isAnchor()) { + if (c->format()) + c->format()->removeRef(); + c->setFormat( string->at( len - 2 ).format() ); + if (c->format()) + c->format()->addRef(); + } + } + + c = ¶g->string()->at( 0 ); + + QPainter *painter = QTextFormat::painter(); + int col = 0; + int ww = 0; + QChar lastChr = c->c; + QTextFormat *lastFormat = c->format(); + for ( ; i < len; ++i, ++col ) { + if ( i ) { + c = ¶g->string()->at(i-1); + lastChr = c->c; + lastFormat = c->format(); + } + bool lastWasOwnLineCustomItem = lastBreak == -2; + bool hadBreakableChar = lastBreak != -1; + bool lastWasHardBreak = lastChr == QChar_linesep; + + // ### next line should not be needed + if ( painter ) + c->format()->setPainter( painter ); + c = &string->at( i ); + + if (lastFormat != c->format() && !c->c.isSpace() + && lastFormat->font().italic() && !c->format()->font().italic()) { + int rb = lastFormat->fontMetrics().rightBearing(lastChr); + if (rb < 0) + x -= rb; + } + + if ( i > 0 && (x > curLeft || ww == 0) || lastWasNonInlineCustom ) { + c->lineStart = 0; + } else { + c->lineStart = 1; + firstChar = c; + } + + // ignore non spacing marks for column count. + if (col != 0 && ::category(c->c) == QChar::Mark_NonSpacing) + --col; + +#ifndef QT_NO_TEXTCUSTOMITEM + lastWasNonInlineCustom = ( c->isCustom() && c->customItem()->placement() != QTextCustomItem::PlaceInline ); +#endif + + if ( c->c.unicode() >= 32 || c->isCustom() ) { + ww = string->width( i ); + } else if ( c->c == '\t' ) { + if ( align == Qt::AlignRight || align == Qt::AlignCenter ) { + // we can not (yet) do tabs + ww = c->format()->width(' ' ); + } else { + int tabx = lastWasHardBreak ? (left + ( doc ? parag->firstLineMargin() : 0 )) : x; + int nx = parag->nextTab( i, tabx - left ) + left; + if ( nx < tabx ) // strrrange... + ww = 0; + else + ww = nx - tabx; + } + } else { + ww = c->format()->width( ' ' ); + } + +#ifndef QT_NO_TEXTCUSTOMITEM + QTextCustomItem* ci = c->customItem(); + if ( c->isCustom() && ci->ownLine() ) { + QTextLineStart *lineStart2 = formatLine( parag, string, lineStart, firstChar, c-1, align, SPACE(w - x - ww) ); + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + ci->resize(w - x); + if ( ci->width < w - x ) { + if ( align & Qt::AlignHCenter ) + x = ( w - ci->width ) / 2; + else if ( align & Qt::AlignRight ) { + x = w - ci->width; + } + } + c->x = x; + curLeft = x; + if ( i == 0 || !isBreakable(string, i-1) || + string->at( i - 1 ).lineStart == 0 ) { + y += QMAX( h, QMAX( tmph, linespacing ) ); + tmph = c->height(); + h = tmph; + lineStart = lineStart2; + lineStart->y = y; + insertLineStart( parag, i, lineStart ); + c->lineStart = 1; + firstChar = c; + } else { + tmph = c->height(); + h = tmph; + delete lineStart2; + } + lineStart->h = h; + lineStart->baseLine = h; + tmpBaseLine = lineStart->baseLine; + lastBreak = -2; + x = w; + minw = QMAX( minw, tminw ); + + int tw = ci->minimumWidth() + ( doc ? doc->leftMargin() : 0 ); + if ( tw < QWIDGETSIZE_MAX ) + tminw = tw; + else + tminw = marg; + wused = QMAX( wused, ci->width ); + continue; + } else if ( c->isCustom() && ci->placement() != QTextCustomItem::PlaceInline ) { + int tw = ci->minimumWidth(); + if ( tw < QWIDGETSIZE_MAX ) + minw = QMAX( minw, tw ); + } +#endif + // we break if + // 1. the last character was a hard break (QChar_linesep) or + // 2. the last charater was a own-line custom item (eg. table or ruler) or + // 3. wrapping was enabled, it was not a space and following + // condition is true: We either had a breakable character + // previously or we ar allowed to break in words and - either + // we break at w pixels and the current char would exceed that + // or - we break at a column and the current character would + // exceed that. + if ( lastWasHardBreak || lastWasOwnLineCustomItem || + ( wrapEnabled && + ( (!c->c.isSpace() && (hadBreakableChar || allowBreakInWords()) && + ( (wrapAtColumn() == -1 && x + ww > w) || + (wrapAtColumn() != -1 && col >= wrapAtColumn()) ) ) ) + ) + ) { + if ( wrapAtColumn() != -1 ) + minw = QMAX( minw, x + ww ); + // if a break was forced (no breakable char, hard break or own line custom item), break immediately.... + if ( !hadBreakableChar || lastWasHardBreak || lastWasOwnLineCustomItem ) { + if ( lineStart ) { + lineStart->baseLine = QMAX( lineStart->baseLine, tmpBaseLine ); + h = QMAX( h, tmph ); + lineStart->h = h; + DO_FLOW( lineStart ); + } + lineStart = formatLine( parag, string, lineStart, firstChar, c-1, align, SPACE(w - x) ); + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + if ( !doc && c->c == '\t' ) { // qt_format_text tab handling + int nx = parag->nextTab( i, x - left ) + left; + if ( nx < x ) + ww = w - x; + else + ww = nx - x; + } + curLeft = x; + y += QMAX( h, linespacing ); + tmph = c->height(); + h = 0; + lineStart->y = y; + insertLineStart( parag, i, lineStart ); + lineStart->baseLine = c->ascent(); + lineStart->h = c->height(); + c->lineStart = 1; + firstChar = c; + tmpBaseLine = lineStart->baseLine; + lastBreak = -1; + col = 0; + if ( allowBreakInWords() || lastWasHardBreak ) { + minw = QMAX(minw, tminw); + tminw = marg + ww; + } + } else { // ... otherwise if we had a breakable char, break there + DO_FLOW( lineStart ); + c->x = x; + i = lastBreak; + lineStart = formatLine( parag, string, lineStart, firstChar, parag->at( lastBreak ),align, SPACE(w - string->at( i+1 ).x) ); + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + if ( !doc && c->c == '\t' ) { // qt_format_text tab handling + int nx = parag->nextTab( i, x - left ) + left; + if ( nx < x ) + ww = w - x; + else + ww = nx - x; + } + curLeft = x; + y += QMAX( h, linespacing ); + tmph = c->height(); + h = tmph; + lineStart->y = y; + insertLineStart( parag, i + 1, lineStart ); + lineStart->baseLine = c->ascent(); + lineStart->h = c->height(); + c->lineStart = 1; + firstChar = c; + tmpBaseLine = lineStart->baseLine; + lastBreak = -1; + col = 0; + minw = QMAX(minw, tminw); + tminw = marg; + continue; + } + } else if (lineStart && isBreakable(string, i)) { + if ( len <= 2 || i < len - 1 ) { + tmpBaseLine = QMAX( tmpBaseLine, c->ascent() ); + tmph = QMAX( tmph, c->height() ); + } + minw = QMAX( minw, tminw ); + + tminw = marg + ww; + lineStart->baseLine = QMAX( lineStart->baseLine, tmpBaseLine ); + h = QMAX( h, tmph ); + lineStart->h = h; + if ( i < len - 2 || c->c != ' ' ) + lastBreak = i; + } else { + tminw += ww; + int cascent = c->ascent(); + int cheight = c->height(); + int belowBaseLine = QMAX( tmph - tmpBaseLine, cheight-cascent ); + tmpBaseLine = QMAX( tmpBaseLine, cascent ); + tmph = tmpBaseLine + belowBaseLine; + } + + c->x = x; + x += ww; + wused = QMAX( wused, x ); + } + + if ( lineStart ) { + lineStart->baseLine = QMAX( lineStart->baseLine, tmpBaseLine ); + h = QMAX( h, tmph ); + lineStart->h = h; + // last line in a paragraph is not justified + if ( align == Qt::AlignJustify ) + align = Qt::AlignAuto; + DO_FLOW( lineStart ); + lineStart = formatLine( parag, string, lineStart, firstChar, c, align, SPACE(w - x) ); + delete lineStart; + } + + minw = QMAX( minw, tminw ); + if ( doc ) + minw += doc->rightMargin(); + + int m = parag->bottomMargin(); + if ( !parag->next() ) + m = 0; + else + m = QMAX(m, parag->next()->topMargin() ) / 2; + parag->setFullWidth( fullWidth ); + y += QMAX( h, linespacing ) + m; + + wused += rm; + if ( !wrapEnabled || wrapAtColumn() != -1 ) + minw = QMAX(minw, wused); + + // This is the case where we are breaking wherever we darn well please + // in cases like that, the minw should not be the length of the entire + // word, because we necessarily want to show the word on the whole line. + // example: word wrap in iconview + if ( allowBreakInWords() && minw > wused ) + minw = wused; + + thisminw = minw; + thiswused = wused; + return y; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextIndent::QTextIndent() +{ +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +QTextFormatCollection::QTextFormatCollection() + : cKey( 307 ), paintdevice( 0 ) +{ + defFormat = new QTextFormat( QApplication::font(), + QApplication::palette().color( QPalette::Active, QColorGroup::Text ) ); + lastFormat = cres = 0; + cflags = -1; + cKey.setAutoDelete( TRUE ); + cachedFormat = 0; +} + +QTextFormatCollection::~QTextFormatCollection() +{ + delete defFormat; +} + +void QTextFormatCollection::setPaintDevice( QPaintDevice *pd ) +{ + paintdevice = pd; + +#if defined(Q_WS_X11) + int scr = ( paintdevice ) ? paintdevice->x11Screen() : QPaintDevice::x11AppScreen(); + + defFormat->fn.x11SetScreen( scr ); + defFormat->update(); + + QDictIterator<QTextFormat> it( cKey ); + QTextFormat *format; + while ( ( format = it.current() ) != 0 ) { + ++it; + format->fn.x11SetScreen( scr ); + format->update(); + } +#endif // Q_WS_X11 +} + +QTextFormat *QTextFormatCollection::format( QTextFormat *f ) +{ + if ( f->parent() == this || f == defFormat ) { + lastFormat = f; + lastFormat->addRef(); + return lastFormat; + } + + if ( f == lastFormat || ( lastFormat && f->key() == lastFormat->key() ) ) { + lastFormat->addRef(); + return lastFormat; + } + + QTextFormat *fm = cKey.find( f->key() ); + if ( fm ) { + lastFormat = fm; + lastFormat->addRef(); + return lastFormat; + } + + if ( f->key() == defFormat->key() ) + return defFormat; + + lastFormat = createFormat( *f ); + lastFormat->collection = this; + cKey.insert( lastFormat->key(), lastFormat ); + return lastFormat; +} + +QTextFormat *QTextFormatCollection::format( QTextFormat *of, QTextFormat *nf, int flags ) +{ + if ( cres && kof == of->key() && knf == nf->key() && cflags == flags ) { + cres->addRef(); + return cres; + } + + cres = createFormat( *of ); + kof = of->key(); + knf = nf->key(); + cflags = flags; + if ( flags & QTextFormat::Bold ) + cres->fn.setBold( nf->fn.bold() ); + if ( flags & QTextFormat::Italic ) + cres->fn.setItalic( nf->fn.italic() ); + if ( flags & QTextFormat::Underline ) + cres->fn.setUnderline( nf->fn.underline() ); + if ( flags & QTextFormat::StrikeOut ) + cres->fn.setStrikeOut( nf->fn.strikeOut() ); + if ( flags & QTextFormat::Family ) + cres->fn.setFamily( nf->fn.family() ); + if ( flags & QTextFormat::Size ) { + if ( of->usePixelSizes ) + cres->fn.setPixelSize( nf->fn.pixelSize() ); + else + cres->fn.setPointSize( nf->fn.pointSize() ); + } + if ( flags & QTextFormat::Color ) + cres->col = nf->col; + if ( flags & QTextFormat::Misspelled ) + cres->missp = nf->missp; + if ( flags & QTextFormat::VAlign ) + cres->ha = nf->ha; + cres->update(); + + QTextFormat *fm = cKey.find( cres->key() ); + if ( !fm ) { + cres->collection = this; + cKey.insert( cres->key(), cres ); + } else { + delete cres; + cres = fm; + cres->addRef(); + } + + return cres; +} + +QTextFormat *QTextFormatCollection::format( const QFont &f, const QColor &c ) +{ + if ( cachedFormat && cfont == f && ccol == c ) { + cachedFormat->addRef(); + return cachedFormat; + } + + QString key = QTextFormat::getKey( f, c, FALSE, QTextFormat::AlignNormal ); + cachedFormat = cKey.find( key ); + cfont = f; + ccol = c; + + if ( cachedFormat ) { + cachedFormat->addRef(); + return cachedFormat; + } + + if ( key == defFormat->key() ) + return defFormat; + + cachedFormat = createFormat( f, c ); + cachedFormat->collection = this; + cKey.insert( cachedFormat->key(), cachedFormat ); + if ( cachedFormat->key() != key ) + qWarning("ASSERT: keys for format not identical: '%s '%s'", cachedFormat->key().latin1(), key.latin1() ); + return cachedFormat; +} + +void QTextFormatCollection::remove( QTextFormat *f ) +{ + if ( lastFormat == f ) + lastFormat = 0; + if ( cres == f ) + cres = 0; + if ( cachedFormat == f ) + cachedFormat = 0; + if (cKey.find(f->key()) == f) + cKey.remove( f->key() ); +} + +#define UPDATE( up, lo, rest ) \ + if ( font.lo##rest() != defFormat->fn.lo##rest() && fm->fn.lo##rest() == defFormat->fn.lo##rest() ) \ + fm->fn.set##up##rest( font.lo##rest() ) + +void QTextFormatCollection::updateDefaultFormat( const QFont &font, const QColor &color, QStyleSheet *sheet ) +{ + QDictIterator<QTextFormat> it( cKey ); + QTextFormat *fm; + bool usePixels = font.pointSize() == -1; + bool changeSize = usePixels ? font.pixelSize() != defFormat->fn.pixelSize() : + font.pointSize() != defFormat->fn.pointSize(); + int base = usePixels ? font.pixelSize() : font.pointSize(); + while ( ( fm = it.current() ) ) { + ++it; + UPDATE( F, f, amily ); + UPDATE( W, w, eight ); + UPDATE( B, b, old ); + UPDATE( I, i, talic ); + UPDATE( U, u, nderline ); + if ( changeSize ) { + fm->stdSize = base; + fm->usePixelSizes = usePixels; + if ( usePixels ) + fm->fn.setPixelSize( fm->stdSize ); + else + fm->fn.setPointSize( fm->stdSize ); + sheet->scaleFont( fm->fn, fm->logicalFontSize ); + } + if ( color.isValid() && color != defFormat->col && fm->col == defFormat->col ) + fm->col = color; + fm->update(); + } + + defFormat->fn = font; + defFormat->col = color; + defFormat->update(); + defFormat->stdSize = base; + defFormat->usePixelSizes = usePixels; + + updateKeys(); +} + +// the keys in cKey have changed, rebuild the hashtable +void QTextFormatCollection::updateKeys() +{ + if ( cKey.isEmpty() ) + return; + cKey.setAutoDelete( FALSE ); + QTextFormat** formats = new QTextFormat*[ cKey.count() + 1 ]; + QTextFormat **f = formats; + QDictIterator<QTextFormat> it( cKey ); + while ( ( *f = it.current() ) ) { + ++it; + ++f; + } + cKey.clear(); + for ( f = formats; *f; f++ ) + cKey.insert( (*f)->key(), *f ); + cKey.setAutoDelete( TRUE ); + delete [] formats; +} + + + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +void QTextFormat::setBold( bool b ) +{ + if ( b == fn.bold() ) + return; + fn.setBold( b ); + update(); +} + +void QTextFormat::setMisspelled( bool b ) +{ + if ( b == (bool)missp ) + return; + missp = b; + update(); +} + +void QTextFormat::setVAlign( VerticalAlignment a ) +{ + if ( a == ha ) + return; + ha = a; + update(); +} + +void QTextFormat::setItalic( bool b ) +{ + if ( b == fn.italic() ) + return; + fn.setItalic( b ); + update(); +} + +void QTextFormat::setUnderline( bool b ) +{ + if ( b == fn.underline() ) + return; + fn.setUnderline( b ); + update(); +} + +void QTextFormat::setStrikeOut( bool b ) +{ + if ( b == fn.strikeOut() ) + return; + fn.setStrikeOut( b ); + update(); +} + +void QTextFormat::setFamily( const QString &f ) +{ + if ( f == fn.family() ) + return; + fn.setFamily( f ); + update(); +} + +void QTextFormat::setPointSize( int s ) +{ + if ( s == fn.pointSize() ) + return; + fn.setPointSize( s ); + usePixelSizes = FALSE; + update(); +} + +void QTextFormat::setFont( const QFont &f ) +{ + if ( f == fn && !k.isEmpty() ) + return; + fn = f; + update(); +} + +void QTextFormat::setColor( const QColor &c ) +{ + if ( c == col ) + return; + col = c; + update(); +} + +QString QTextFormat::makeFormatChangeTags( QTextFormat* defaultFormat, QTextFormat *f, + const QString& oldAnchorHref, const QString& anchorHref ) const +{ + QString tag; + if ( f ) + tag += f->makeFormatEndTags( defaultFormat, oldAnchorHref ); + + if ( !anchorHref.isEmpty() ) + tag += "<a href=\"" + anchorHref + "\">"; + + if ( font() != defaultFormat->font() + || vAlign() != defaultFormat->vAlign() + || color().rgb() != defaultFormat->color().rgb() ) { + QString s; + if ( font().family() != defaultFormat->font().family() ) + s += QString(!!s?";":"") + "font-family:" + fn.family(); + if ( font().italic() && font().italic() != defaultFormat->font().italic() ) + s += QString(!!s?";":"") + "font-style:" + (font().italic() ? "italic" : "normal"); + if ( font().pointSize() != defaultFormat->font().pointSize() ) + s += QString(!!s?";":"") + "font-size:" + QString::number( fn.pointSize() ) + "pt"; + if ( font().weight() != defaultFormat->font().weight() ) + s += QString(!!s?";":"") + "font-weight:" + QString::number( fn.weight() * 8 ); + QString textDecoration; + bool none = FALSE; + if ( font().underline() != defaultFormat->font().underline() ) { + if (font().underline()) + textDecoration = "underline"; + else + none = TRUE; + } + if ( font().overline() != defaultFormat->font().overline() ) { + if (font().overline()) + textDecoration += " overline"; + else + none = TRUE; + } + if ( font().strikeOut() != defaultFormat->font().strikeOut() ) { + if (font().strikeOut()) + textDecoration += " line-through"; + else + none = TRUE; + } + if (none && textDecoration.isEmpty()) + textDecoration = "none"; + if (!textDecoration.isEmpty()) + s += QString(!!s?";":"") + "text-decoration:" + textDecoration; + if ( vAlign() != defaultFormat->vAlign() ) { + s += QString(!!s?";":"") + "vertical-align:"; + if ( vAlign() == QTextFormat::AlignSuperScript ) + s += "super"; + else if ( vAlign() == QTextFormat::AlignSubScript ) + s += "sub"; + else + s += "normal"; + } + if ( color().rgb() != defaultFormat->color().rgb() ) + s += QString(!!s?";":"") + "color:" + col.name(); + if ( !s.isEmpty() ) + tag += "<span style=\"" + s + "\">"; + } + + return tag; +} + +QString QTextFormat::makeFormatEndTags( QTextFormat* defaultFormat, const QString& anchorHref ) const +{ + QString tag; + if ( font().family() != defaultFormat->font().family() + || font().pointSize() != defaultFormat->font().pointSize() + || font().weight() != defaultFormat->font().weight() + || font().italic() != defaultFormat->font().italic() + || font().underline() != defaultFormat->font().underline() + || font().strikeOut() != defaultFormat->font().strikeOut() + || vAlign() != defaultFormat->vAlign() + || color().rgb() != defaultFormat->color().rgb() ) + tag += "</span>"; + if ( !anchorHref.isEmpty() ) + tag += "</a>"; + return tag; +} + +QTextFormat QTextFormat::makeTextFormat( const QStyleSheetItem *style, const QMap<QString,QString>& attr, double scaleFontsFactor ) const +{ + QTextFormat format(*this); + if (!style ) + return format; + + if ( !style->isAnchor() && style->color().isValid() ) { + // the style is not an anchor and defines a color. + // It might be used inside an anchor and it should + // override the link color. + format.linkColor = FALSE; + } + switch ( style->verticalAlignment() ) { + case QStyleSheetItem::VAlignBaseline: + format.setVAlign( QTextFormat::AlignNormal ); + break; + case QStyleSheetItem::VAlignSuper: + format.setVAlign( QTextFormat::AlignSuperScript ); + break; + case QStyleSheetItem::VAlignSub: + format.setVAlign( QTextFormat::AlignSubScript ); + break; + } + + if ( style->fontWeight() != QStyleSheetItem::Undefined ) + format.fn.setWeight( style->fontWeight() ); + if ( style->fontSize() != QStyleSheetItem::Undefined ) { + format.fn.setPointSize( style->fontSize() ); + } else if ( style->logicalFontSize() != QStyleSheetItem::Undefined ) { + format.logicalFontSize = style->logicalFontSize(); + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } else if ( style->logicalFontSizeStep() ) { + format.logicalFontSize += style->logicalFontSizeStep(); + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } + if ( !style->fontFamily().isEmpty() ) + format.fn.setFamily( style->fontFamily() ); + if ( style->color().isValid() ) + format.col = style->color(); + if ( style->definesFontItalic() ) + format.fn.setItalic( style->fontItalic() ); + if ( style->definesFontUnderline() ) + format.fn.setUnderline( style->fontUnderline() ); + if ( style->definesFontStrikeOut() ) + format.fn.setStrikeOut( style->fontStrikeOut() ); + + + if ( style->name() == "font") { + if ( attr.contains("color") ) { + QString s = attr["color"]; + if ( !s.isEmpty() ) { + format.col.setNamedColor( s ); + format.linkColor = FALSE; + } + } + if ( attr.contains("face") ) { + QString a = attr["face"]; + QString family = a.section( ',', 0, 0 ); + if ( !!family ) + format.fn.setFamily( family ); + } + if ( attr.contains("size") ) { + QString a = attr["size"]; + int n = a.toInt(); + if ( a[0] == '+' || a[0] == '-' ) + n += 3; + format.logicalFontSize = n; + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } + } + if ( attr.contains("style" ) ) { + QString a = attr["style"]; + for ( int s = 0; s < a.contains(';')+1; s++ ) { + QString style = a.section( ';', s, s ); + if ( style.startsWith("font-size:" ) && style.endsWith("pt") ) { + format.logicalFontSize = 0; + int size = int( scaleFontsFactor * style.mid( 10, style.length() - 12 ).toDouble() ); + format.setPointSize( size ); + } else if ( style.startsWith("font-style:" ) ) { + QString s = style.mid( 11 ).stripWhiteSpace(); + if ( s == "normal" ) + format.fn.setItalic( FALSE ); + else if ( s == "italic" || s == "oblique" ) + format.fn.setItalic( TRUE ); + } else if ( style.startsWith("font-weight:" ) ) { + QString s = style.mid( 12 ); + bool ok = TRUE; + int n = s.toInt( &ok ); + if ( ok ) + format.fn.setWeight( n/8 ); + } else if ( style.startsWith("font-family:" ) ) { + QString family = style.mid(12).section(',',0,0); + family.replace( '\"', ' ' ); + family.replace( '\'', ' ' ); + family = family.stripWhiteSpace(); + format.fn.setFamily( family ); + } else if ( style.startsWith("text-decoration:" ) ) { + QString s = style.mid( 16 ); + format.fn.setOverline( s.find("overline") != -1 ); + format.fn.setStrikeOut( s.find("line-through") != -1 ); + format.fn.setUnderline( s.find("underline") != -1 ); + } else if ( style.startsWith("vertical-align:" ) ) { + QString s = style.mid( 15 ).stripWhiteSpace(); + if ( s == "sub" ) + format.setVAlign( QTextFormat::AlignSubScript ); + else if ( s == "super" ) + format.setVAlign( QTextFormat::AlignSuperScript ); + else + format.setVAlign( QTextFormat::AlignNormal ); + } else if ( style.startsWith("color:" ) ) { + format.col.setNamedColor( style.mid(6) ); + format.linkColor = FALSE; + } + } + } + + format.update(); + return format; +} + +#ifndef QT_NO_TEXTCUSTOMITEM + +struct QPixmapInt +{ + QPixmapInt() : ref( 0 ) {} + QPixmap pm; + int ref; + Q_DUMMY_COMPARISON_OPERATOR(QPixmapInt) +}; + +static QMap<QString, QPixmapInt> *pixmap_map = 0; + +QTextImage::QTextImage( QTextDocument *p, const QMap<QString, QString> &attr, const QString& context, + QMimeSourceFactory &factory ) + : QTextCustomItem( p ) +{ + width = height = 0; + if ( attr.contains("width") ) + width = attr["width"].toInt(); + if ( attr.contains("height") ) + height = attr["height"].toInt(); + + reg = 0; + QString imageName = attr["src"]; + + if (!imageName) + imageName = attr["source"]; + + if ( !imageName.isEmpty() ) { + imgId = QString( "%1,%2,%3,%4" ).arg( imageName ).arg( width ).arg( height ).arg( (ulong)&factory ); + if ( !pixmap_map ) + pixmap_map = new QMap<QString, QPixmapInt>; + if ( pixmap_map->contains( imgId ) ) { + QPixmapInt& pmi = pixmap_map->operator[](imgId); + pm = pmi.pm; + pmi.ref++; + width = pm.width(); + height = pm.height(); + } else { + QImage img; + const QMimeSource* m = + factory.data( imageName, context ); + if ( !m ) { + qWarning("QTextImage: no mimesource for %s", imageName.latin1() ); + } + else { + if ( !QImageDrag::decode( m, img ) ) { + qWarning("QTextImage: cannot decode %s", imageName.latin1() ); + } + } + + if ( !img.isNull() ) { + if ( width == 0 ) { + width = img.width(); + if ( height != 0 ) { + width = img.width() * height / img.height(); + } + } + if ( height == 0 ) { + height = img.height(); + if ( width != img.width() ) { + height = img.height() * width / img.width(); + } + } + if ( img.width() != width || img.height() != height ){ +#ifndef QT_NO_IMAGE_SMOOTHSCALE + img = img.smoothScale(width, height); +#endif + width = img.width(); + height = img.height(); + } + pm.convertFromImage( img ); + } + if ( !pm.isNull() ) { + QPixmapInt& pmi = pixmap_map->operator[](imgId); + pmi.pm = pm; + pmi.ref++; + } + } + if ( pm.mask() ) { + QRegion mask( *pm.mask() ); + QRegion all( 0, 0, pm.width(), pm.height() ); + reg = new QRegion( all.subtract( mask ) ); + } + } + + if ( pm.isNull() && (width*height)==0 ) + width = height = 50; + + place = PlaceInline; + if ( attr["align"] == "left" ) + place = PlaceLeft; + else if ( attr["align"] == "right" ) + place = PlaceRight; + + tmpwidth = width; + tmpheight = height; + + attributes = attr; +} + +QTextImage::~QTextImage() +{ + if ( pixmap_map && pixmap_map->contains( imgId ) ) { + QPixmapInt& pmi = pixmap_map->operator[](imgId); + pmi.ref--; + if ( !pmi.ref ) { + pixmap_map->remove( imgId ); + if ( pixmap_map->isEmpty() ) { + delete pixmap_map; + pixmap_map = 0; + } + } + } + delete reg; +} + +QString QTextImage::richText() const +{ + QString s; + s += "<img "; + QMap<QString, QString>::ConstIterator it = attributes.begin(); + for ( ; it != attributes.end(); ++it ) { + s += it.key() + "="; + if ( (*it).find( ' ' ) != -1 ) + s += "\"" + *it + "\"" + " "; + else + s += *it + " "; + } + s += ">"; + return s; +} + +void QTextImage::adjustToPainter( QPainter* p ) +{ + width = scale( tmpwidth, p ); + height = scale( tmpheight, p ); +} + +#if !defined(Q_WS_X11) +#include <qbitmap.h> +#include <qcleanuphandler.h> +static QPixmap *qrt_selection = 0; +static QSingleCleanupHandler<QPixmap> qrt_cleanup_pixmap; +static void qrt_createSelectionPixmap( const QColorGroup &cg ) +{ + qrt_selection = new QPixmap( 2, 2 ); + qrt_cleanup_pixmap.set( &qrt_selection ); + qrt_selection->fill( Qt::color0 ); + QBitmap m( 2, 2 ); + m.fill( Qt::color1 ); + QPainter p( &m ); + p.setPen( Qt::color0 ); + for ( int j = 0; j < 2; ++j ) { + p.drawPoint( j % 2, j ); + } + p.end(); + qrt_selection->setMask( m ); + qrt_selection->fill( cg.highlight() ); +} +#endif + +void QTextImage::draw( QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ) +{ + if ( placement() != PlaceInline ) { + x = xpos; + y = ypos; + } + + if ( pm.isNull() ) { + p->fillRect( x , y, width, height, cg.dark() ); + return; + } + + if ( is_printer( p ) ) { + p->drawPixmap( QRect( x, y, width, height ), pm ); + return; + } + + if ( placement() != PlaceInline && !QRect( xpos, ypos, width, height ).intersects( QRect( cx, cy, cw, ch ) ) ) + return; + + if ( placement() == PlaceInline ) + p->drawPixmap( x , y, pm ); + else + p->drawPixmap( cx , cy, pm, cx - x, cy - y, cw, ch ); + + if ( selected && placement() == PlaceInline && is_printer( p ) ) { +#if defined(Q_WS_X11) + p->fillRect( QRect( QPoint( x, y ), pm.size() ), QBrush( cg.highlight(), QBrush::Dense4Pattern) ); +#else // in WIN32 Dense4Pattern doesn't work correctly (transparency problem), so work around it + if ( !qrt_selection ) + qrt_createSelectionPixmap( cg ); + p->drawTiledPixmap( x, y, pm.width(), pm.height(), *qrt_selection ); +#endif + } +} + +void QTextHorizontalLine::adjustToPainter( QPainter* p ) +{ + height = scale( tmpheight, p ); +} + + +QTextHorizontalLine::QTextHorizontalLine( QTextDocument *p, const QMap<QString, QString> &attr, + const QString &, + QMimeSourceFactory & ) + : QTextCustomItem( p ) +{ + height = tmpheight = 8; + if ( attr.find( "color" ) != attr.end() ) + color = QColor( *attr.find( "color" ) ); + shade = attr.find( "noshade" ) == attr.end(); +} + +QTextHorizontalLine::~QTextHorizontalLine() +{ +} + +QString QTextHorizontalLine::richText() const +{ + return "<hr>"; +} + +void QTextHorizontalLine::draw( QPainter* p, int x, int y, int , int , int , int , const QColorGroup& cg, bool selected ) +{ + QRect r( x, y, width, height); + if ( is_printer( p ) || !shade ) { + QPen oldPen = p->pen(); + if ( !color.isValid() ) + p->setPen( QPen( cg.text(), is_printer( p ) ? height/8 : QMAX( 2, height/4 ) ) ); + else + p->setPen( QPen( color, is_printer( p ) ? height/8 : QMAX( 2, height/4 ) ) ); + p->drawLine( r.left()-1, y + height / 2, r.right() + 1, y + height / 2 ); + p->setPen( oldPen ); + } else { + QColorGroup g( cg ); + if ( color.isValid() ) + g.setColor( QColorGroup::Dark, color ); + if ( selected ) + p->fillRect( r, g.highlight() ); + qDrawShadeLine( p, r.left() - 1, y + height / 2, r.right() + 1, y + height / 2, g, TRUE, height / 8 ); + } +} +#endif //QT_NO_TEXTCUSTOMITEM + +/*****************************************************************/ +// Small set of utility functions to make the parser a bit simpler +// + +bool QTextDocument::hasPrefix(const QChar* doc, int length, int pos, QChar c) +{ + if ( pos + 1 > length ) + return FALSE; + return doc[ pos ].lower() == c.lower(); +} + +bool QTextDocument::hasPrefix( const QChar* doc, int length, int pos, const QString& s ) +{ + if ( pos + (int) s.length() > length ) + return FALSE; + for ( int i = 0; i < (int)s.length(); i++ ) { + if ( doc[ pos + i ].lower() != s[ i ].lower() ) + return FALSE; + } + return TRUE; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +static bool qt_is_cell_in_use( QPtrList<QTextTableCell>& cells, int row, int col ) +{ + for ( QTextTableCell* c = cells.first(); c; c = cells.next() ) { + if ( row >= c->row() && row < c->row() + c->rowspan() + && col >= c->column() && col < c->column() + c->colspan() ) + return TRUE; + } + return FALSE; +} + +QTextCustomItem* QTextDocument::parseTable( const QMap<QString, QString> &attr, const QTextFormat &fmt, + const QChar* doc, int length, int& pos, QTextParagraph *curpar ) +{ + + QTextTable* table = new QTextTable( this, attr ); + int row = -1; + int col = -1; + + QString rowbgcolor; + QString rowalign; + QString tablebgcolor = attr["bgcolor"]; + + QPtrList<QTextTableCell> multicells; + + QString tagname; + (void) eatSpace(doc, length, pos); + while ( pos < length) { + if (hasPrefix(doc, length, pos, QChar('<')) ){ + if (hasPrefix(doc, length, pos+1, QChar('/'))) { + tagname = parseCloseTag( doc, length, pos ); + if ( tagname == "table" ) { + return table; + } + } else { + QMap<QString, QString> attr2; + bool emptyTag = FALSE; + tagname = parseOpenTag( doc, length, pos, attr2, emptyTag ); + if ( tagname == "tr" ) { + rowbgcolor = attr2["bgcolor"]; + rowalign = attr2["align"]; + row++; + col = -1; + } + else if ( tagname == "td" || tagname == "th" ) { + col++; + while ( qt_is_cell_in_use( multicells, row, col ) ) { + col++; + } + + if ( row >= 0 && col >= 0 ) { + const QStyleSheetItem* s = sheet_->item(tagname); + if ( !attr2.contains("bgcolor") ) { + if (!rowbgcolor.isEmpty() ) + attr2["bgcolor"] = rowbgcolor; + else if (!tablebgcolor.isEmpty() ) + attr2["bgcolor"] = tablebgcolor; + } + if ( !attr2.contains("align") ) { + if (!rowalign.isEmpty() ) + attr2["align"] = rowalign; + } + + // extract the cell contents + int end = pos; + while ( end < length + && !hasPrefix( doc, length, end, "</td") + && !hasPrefix( doc, length, end, "<td") + && !hasPrefix( doc, length, end, "</th") + && !hasPrefix( doc, length, end, "<th") + && !hasPrefix( doc, length, end, "<td") + && !hasPrefix( doc, length, end, "</tr") + && !hasPrefix( doc, length, end, "<tr") + && !hasPrefix( doc, length, end, "</table") ) { + if ( hasPrefix( doc, length, end, "<table" ) ) { // nested table + int nested = 1; + ++end; + while ( end < length && nested != 0 ) { + if ( hasPrefix( doc, length, end, "</table" ) ) + nested--; + if ( hasPrefix( doc, length, end, "<table" ) ) + nested++; + end++; + } + } + end++; + } + QTextTableCell* cell = new QTextTableCell( table, row, col, + attr2, s, fmt.makeTextFormat( s, attr2, scaleFontsFactor ), + contxt, *factory_, sheet_, + QConstString( doc + pos, end - pos ).string() ); + cell->richText()->parentPar = curpar; + if ( cell->colspan() > 1 || cell->rowspan() > 1 ) + multicells.append( cell ); + col += cell->colspan()-1; + pos = end; + } + } + } + + } else { + ++pos; + } + } + return table; +} +#endif // QT_NO_TEXTCUSTOMITEM + +bool QTextDocument::eatSpace(const QChar* doc, int length, int& pos, bool includeNbsp ) +{ + int old_pos = pos; + while (pos < length && doc[pos].isSpace() && ( includeNbsp || (doc[pos] != QChar::nbsp ) ) ) + pos++; + return old_pos < pos; +} + +bool QTextDocument::eat(const QChar* doc, int length, int& pos, QChar c) +{ + bool ok = pos < length && doc[pos] == c; + if ( ok ) + pos++; + return ok; +} +/*****************************************************************/ + +struct Entity { + const char * name; + Q_UINT16 code; +}; + +static const Entity entitylist [] = { + { "AElig", 0x00c6 }, + { "Aacute", 0x00c1 }, + { "Acirc", 0x00c2 }, + { "Agrave", 0x00c0 }, + { "Alpha", 0x0391 }, + { "AMP", 38 }, + { "Aring", 0x00c5 }, + { "Atilde", 0x00c3 }, + { "Auml", 0x00c4 }, + { "Beta", 0x0392 }, + { "Ccedil", 0x00c7 }, + { "Chi", 0x03a7 }, + { "Dagger", 0x2021 }, + { "Delta", 0x0394 }, + { "ETH", 0x00d0 }, + { "Eacute", 0x00c9 }, + { "Ecirc", 0x00ca }, + { "Egrave", 0x00c8 }, + { "Epsilon", 0x0395 }, + { "Eta", 0x0397 }, + { "Euml", 0x00cb }, + { "Gamma", 0x0393 }, + { "GT", 62 }, + { "Iacute", 0x00cd }, + { "Icirc", 0x00ce }, + { "Igrave", 0x00cc }, + { "Iota", 0x0399 }, + { "Iuml", 0x00cf }, + { "Kappa", 0x039a }, + { "Lambda", 0x039b }, + { "LT", 60 }, + { "Mu", 0x039c }, + { "Ntilde", 0x00d1 }, + { "Nu", 0x039d }, + { "OElig", 0x0152 }, + { "Oacute", 0x00d3 }, + { "Ocirc", 0x00d4 }, + { "Ograve", 0x00d2 }, + { "Omega", 0x03a9 }, + { "Omicron", 0x039f }, + { "Oslash", 0x00d8 }, + { "Otilde", 0x00d5 }, + { "Ouml", 0x00d6 }, + { "Phi", 0x03a6 }, + { "Pi", 0x03a0 }, + { "Prime", 0x2033 }, + { "Psi", 0x03a8 }, + { "QUOT", 34 }, + { "Rho", 0x03a1 }, + { "Scaron", 0x0160 }, + { "Sigma", 0x03a3 }, + { "THORN", 0x00de }, + { "Tau", 0x03a4 }, + { "Theta", 0x0398 }, + { "Uacute", 0x00da }, + { "Ucirc", 0x00db }, + { "Ugrave", 0x00d9 }, + { "Upsilon", 0x03a5 }, + { "Uuml", 0x00dc }, + { "Xi", 0x039e }, + { "Yacute", 0x00dd }, + { "Yuml", 0x0178 }, + { "Zeta", 0x0396 }, + { "aacute", 0x00e1 }, + { "acirc", 0x00e2 }, + { "acute", 0x00b4 }, + { "aelig", 0x00e6 }, + { "agrave", 0x00e0 }, + { "alefsym", 0x2135 }, + { "alpha", 0x03b1 }, + { "amp", 38 }, + { "and", 0x22a5 }, + { "ang", 0x2220 }, + { "apos", 0x0027 }, + { "aring", 0x00e5 }, + { "asymp", 0x2248 }, + { "atilde", 0x00e3 }, + { "auml", 0x00e4 }, + { "bdquo", 0x201e }, + { "beta", 0x03b2 }, + { "brvbar", 0x00a6 }, + { "bull", 0x2022 }, + { "cap", 0x2229 }, + { "ccedil", 0x00e7 }, + { "cedil", 0x00b8 }, + { "cent", 0x00a2 }, + { "chi", 0x03c7 }, + { "circ", 0x02c6 }, + { "clubs", 0x2663 }, + { "cong", 0x2245 }, + { "copy", 0x00a9 }, + { "crarr", 0x21b5 }, + { "cup", 0x222a }, + { "curren", 0x00a4 }, + { "dArr", 0x21d3 }, + { "dagger", 0x2020 }, + { "darr", 0x2193 }, + { "deg", 0x00b0 }, + { "delta", 0x03b4 }, + { "diams", 0x2666 }, + { "divide", 0x00f7 }, + { "eacute", 0x00e9 }, + { "ecirc", 0x00ea }, + { "egrave", 0x00e8 }, + { "empty", 0x2205 }, + { "emsp", 0x2003 }, + { "ensp", 0x2002 }, + { "epsilon", 0x03b5 }, + { "equiv", 0x2261 }, + { "eta", 0x03b7 }, + { "eth", 0x00f0 }, + { "euml", 0x00eb }, + { "euro", 0x20ac }, + { "exist", 0x2203 }, + { "fnof", 0x0192 }, + { "forall", 0x2200 }, + { "frac12", 0x00bd }, + { "frac14", 0x00bc }, + { "frac34", 0x00be }, + { "frasl", 0x2044 }, + { "gamma", 0x03b3 }, + { "ge", 0x2265 }, + { "gt", 62 }, + { "hArr", 0x21d4 }, + { "harr", 0x2194 }, + { "hearts", 0x2665 }, + { "hellip", 0x2026 }, + { "iacute", 0x00ed }, + { "icirc", 0x00ee }, + { "iexcl", 0x00a1 }, + { "igrave", 0x00ec }, + { "image", 0x2111 }, + { "infin", 0x221e }, + { "int", 0x222b }, + { "iota", 0x03b9 }, + { "iquest", 0x00bf }, + { "isin", 0x2208 }, + { "iuml", 0x00ef }, + { "kappa", 0x03ba }, + { "lArr", 0x21d0 }, + { "lambda", 0x03bb }, + { "lang", 0x2329 }, + { "laquo", 0x00ab }, + { "larr", 0x2190 }, + { "lceil", 0x2308 }, + { "ldquo", 0x201c }, + { "le", 0x2264 }, + { "lfloor", 0x230a }, + { "lowast", 0x2217 }, + { "loz", 0x25ca }, + { "lrm", 0x200e }, + { "lsaquo", 0x2039 }, + { "lsquo", 0x2018 }, + { "lt", 60 }, + { "macr", 0x00af }, + { "mdash", 0x2014 }, + { "micro", 0x00b5 }, + { "middot", 0x00b7 }, + { "minus", 0x2212 }, + { "mu", 0x03bc }, + { "nabla", 0x2207 }, + { "nbsp", 0x00a0 }, + { "ndash", 0x2013 }, + { "ne", 0x2260 }, + { "ni", 0x220b }, + { "not", 0x00ac }, + { "notin", 0x2209 }, + { "nsub", 0x2284 }, + { "ntilde", 0x00f1 }, + { "nu", 0x03bd }, + { "oacute", 0x00f3 }, + { "ocirc", 0x00f4 }, + { "oelig", 0x0153 }, + { "ograve", 0x00f2 }, + { "oline", 0x203e }, + { "omega", 0x03c9 }, + { "omicron", 0x03bf }, + { "oplus", 0x2295 }, + { "or", 0x22a6 }, + { "ordf", 0x00aa }, + { "ordm", 0x00ba }, + { "oslash", 0x00f8 }, + { "otilde", 0x00f5 }, + { "otimes", 0x2297 }, + { "ouml", 0x00f6 }, + { "para", 0x00b6 }, + { "part", 0x2202 }, + { "percnt", 0x0025 }, + { "permil", 0x2030 }, + { "perp", 0x22a5 }, + { "phi", 0x03c6 }, + { "pi", 0x03c0 }, + { "piv", 0x03d6 }, + { "plusmn", 0x00b1 }, + { "pound", 0x00a3 }, + { "prime", 0x2032 }, + { "prod", 0x220f }, + { "prop", 0x221d }, + { "psi", 0x03c8 }, + { "quot", 34 }, + { "rArr", 0x21d2 }, + { "radic", 0x221a }, + { "rang", 0x232a }, + { "raquo", 0x00bb }, + { "rarr", 0x2192 }, + { "rceil", 0x2309 }, + { "rdquo", 0x201d }, + { "real", 0x211c }, + { "reg", 0x00ae }, + { "rfloor", 0x230b }, + { "rho", 0x03c1 }, + { "rlm", 0x200f }, + { "rsaquo", 0x203a }, + { "rsquo", 0x2019 }, + { "sbquo", 0x201a }, + { "scaron", 0x0161 }, + { "sdot", 0x22c5 }, + { "sect", 0x00a7 }, + { "shy", 0x00ad }, + { "sigma", 0x03c3 }, + { "sigmaf", 0x03c2 }, + { "sim", 0x223c }, + { "spades", 0x2660 }, + { "sub", 0x2282 }, + { "sube", 0x2286 }, + { "sum", 0x2211 }, + { "sup1", 0x00b9 }, + { "sup2", 0x00b2 }, + { "sup3", 0x00b3 }, + { "sup", 0x2283 }, + { "supe", 0x2287 }, + { "szlig", 0x00df }, + { "tau", 0x03c4 }, + { "there4", 0x2234 }, + { "theta", 0x03b8 }, + { "thetasym", 0x03d1 }, + { "thinsp", 0x2009 }, + { "thorn", 0x00fe }, + { "tilde", 0x02dc }, + { "times", 0x00d7 }, + { "trade", 0x2122 }, + { "uArr", 0x21d1 }, + { "uacute", 0x00fa }, + { "uarr", 0x2191 }, + { "ucirc", 0x00fb }, + { "ugrave", 0x00f9 }, + { "uml", 0x00a8 }, + { "upsih", 0x03d2 }, + { "upsilon", 0x03c5 }, + { "uuml", 0x00fc }, + { "weierp", 0x2118 }, + { "xi", 0x03be }, + { "yacute", 0x00fd }, + { "yen", 0x00a5 }, + { "yuml", 0x00ff }, + { "zeta", 0x03b6 }, + { "zwj", 0x200d }, + { "zwnj", 0x200c }, + { "", 0x0000 } +}; + + + + + +static QMap<QString, QChar> *html_map = 0; +static void qt_cleanup_html_map() +{ + delete html_map; + html_map = 0; +} + +static QMap<QString, QChar> *htmlMap() +{ + if ( !html_map ) { + html_map = new QMap<QString, QChar>; + qAddPostRoutine( qt_cleanup_html_map ); + + const Entity *ent = entitylist; + while( ent->code ) { + html_map->insert( ent->name, QChar(ent->code) ); + ent++; + } + } + return html_map; +} + +QChar QTextDocument::parseHTMLSpecialChar(const QChar* doc, int length, int& pos) +{ + QString s; + pos++; + int recoverpos = pos; + while ( pos < length && doc[pos] != ';' && !doc[pos].isSpace() && pos < recoverpos + 8 ) { + s += doc[pos]; + pos++; + } + if (doc[pos] != ';' && !doc[pos].isSpace() ) { + pos = recoverpos; + return '&'; + } + pos++; + + if ( s.length() > 1 && s[0] == '#') { + int off = 1; + int base = 10; + if (s[1] == 'x') { + off = 2; + base = 16; + } + bool ok; + int num = s.mid(off).toInt(&ok, base); + if ( num == 151 ) // ### hack for designer manual + return '-'; + if (ok) + return num; + } else { + QMap<QString, QChar>::Iterator it = htmlMap()->find(s); + if ( it != htmlMap()->end() ) { + return *it; + } + } + + pos = recoverpos; + return '&'; +} + +QString QTextDocument::parseWord(const QChar* doc, int length, int& pos, bool lower) +{ + QString s; + + if (doc[pos] == '"') { + pos++; + while ( pos < length && doc[pos] != '"' ) { + if ( doc[pos] == '&' ) { + s += parseHTMLSpecialChar( doc, length, pos ); + } else { + s += doc[pos]; + pos++; + } + } + eat(doc, length, pos, '"'); + } else if (doc[pos] == '\'') { + pos++; + while ( pos < length && doc[pos] != '\'' ) { + s += doc[pos]; + pos++; + } + eat(doc, length, pos, '\''); + } else { + static QString term = QString::fromLatin1("/>"); + while ( pos < length + && doc[pos] != '>' + && !hasPrefix(doc, length, pos, term) + && doc[pos] != '<' + && doc[pos] != '=' + && !doc[pos].isSpace() ) + { + if ( doc[pos] == '&' ) { + s += parseHTMLSpecialChar( doc, length, pos ); + } else { + s += doc[pos]; + pos++; + } + } + if (lower) + s = s.lower(); + } + return s; +} + +QChar QTextDocument::parseChar(const QChar* doc, int length, int& pos, QStyleSheetItem::WhiteSpaceMode wsm ) +{ + if ( pos >= length ) + return QChar::null; + + QChar c = doc[pos++]; + + if (c == '<' ) + return QChar::null; + + if ( c.isSpace() && c != QChar::nbsp ) { + if ( wsm == QStyleSheetItem::WhiteSpacePre ) { + if ( c == '\n' ) + return QChar_linesep; + else + return c; + } else { // non-pre mode: collapse whitespace except nbsp + while ( pos< length && + doc[pos].isSpace() && doc[pos] != QChar::nbsp ) + pos++; + return ' '; + } + } + else if ( c == '&' ) + return parseHTMLSpecialChar( doc, length, --pos ); + else + return c; +} + +QString QTextDocument::parseOpenTag(const QChar* doc, int length, int& pos, + QMap<QString, QString> &attr, bool& emptyTag) +{ + emptyTag = FALSE; + pos++; + if ( hasPrefix(doc, length, pos, '!') ) { + if ( hasPrefix( doc, length, pos+1, "--")) { + pos += 3; + // eat comments + QString pref = QString::fromLatin1("-->"); + while ( !hasPrefix(doc, length, pos, pref ) && pos < length ) + pos++; + if ( hasPrefix(doc, length, pos, pref ) ) { + pos += 3; + eatSpace(doc, length, pos, TRUE); + } + emptyTag = TRUE; + return QString::null; + } + else { + // eat strange internal tags + while ( !hasPrefix(doc, length, pos, '>') && pos < length ) + pos++; + if ( hasPrefix(doc, length, pos, '>') ) { + pos++; + eatSpace(doc, length, pos, TRUE); + } + return QString::null; + } + } + + QString tag = parseWord(doc, length, pos ); + eatSpace(doc, length, pos, TRUE); + static QString term = QString::fromLatin1("/>"); + static QString s_TRUE = QString::fromLatin1("TRUE"); + + while (doc[pos] != '>' && ! (emptyTag = hasPrefix(doc, length, pos, term) )) { + QString key = parseWord(doc, length, pos ); + eatSpace(doc, length, pos, TRUE); + if ( key.isEmpty()) { + // error recovery + while ( pos < length && doc[pos] != '>' ) + pos++; + break; + } + QString value; + if (hasPrefix(doc, length, pos, '=') ){ + pos++; + eatSpace(doc, length, pos); + value = parseWord(doc, length, pos, FALSE); + } + else + value = s_TRUE; + attr.insert(key.lower(), value ); + eatSpace(doc, length, pos, TRUE); + } + + if (emptyTag) { + eat(doc, length, pos, '/'); + eat(doc, length, pos, '>'); + } + else + eat(doc, length, pos, '>'); + + return tag; +} + +QString QTextDocument::parseCloseTag( const QChar* doc, int length, int& pos ) +{ + pos++; + pos++; + QString tag = parseWord(doc, length, pos ); + eatSpace(doc, length, pos, TRUE); + eat(doc, length, pos, '>'); + return tag; +} + +QTextFlow::QTextFlow() +{ + w = pagesize = 0; +} + +QTextFlow::~QTextFlow() +{ + clear(); +} + +void QTextFlow::clear() +{ +#ifndef QT_NO_TEXTCUSTOMITEM + leftItems.setAutoDelete( TRUE ); + rightItems.setAutoDelete( TRUE ); + leftItems.clear(); + rightItems.clear(); + leftItems.setAutoDelete( FALSE ); + rightItems.setAutoDelete( FALSE ); +#endif +} + +void QTextFlow::setWidth( int width ) +{ + w = width; +} + +int QTextFlow::adjustLMargin( int yp, int, int margin, int space ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + for ( QTextCustomItem* item = leftItems.first(); item; item = leftItems.next() ) { + if ( item->ypos == -1 ) + continue; + if ( yp >= item->ypos && yp < item->ypos + item->height ) + margin = QMAX( margin, item->xpos + item->width + space ); + } +#endif + return margin; +} + +int QTextFlow::adjustRMargin( int yp, int, int margin, int space ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + for ( QTextCustomItem* item = rightItems.first(); item; item = rightItems.next() ) { + if ( item->ypos == -1 ) + continue; + if ( yp >= item->ypos && yp < item->ypos + item->height ) + margin = QMAX( margin, w - item->xpos - space ); + } +#endif + return margin; +} + + +int QTextFlow::adjustFlow( int y, int /*w*/, int h ) +{ + if ( pagesize > 0 ) { // check pages + int yinpage = y % pagesize; + if ( yinpage <= border_tolerance ) + return border_tolerance - yinpage; + else + if ( yinpage + h > pagesize - border_tolerance ) + return ( pagesize - yinpage ) + border_tolerance; + } + return 0; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void QTextFlow::unregisterFloatingItem( QTextCustomItem* item ) +{ + leftItems.removeRef( item ); + rightItems.removeRef( item ); +} + +void QTextFlow::registerFloatingItem( QTextCustomItem* item ) +{ + if ( item->placement() == QTextCustomItem::PlaceRight ) { + if ( !rightItems.contains( item ) ) + rightItems.append( item ); + } else if ( item->placement() == QTextCustomItem::PlaceLeft && + !leftItems.contains( item ) ) { + leftItems.append( item ); + } +} +#endif // QT_NO_TEXTCUSTOMITEM + +QRect QTextFlow::boundingRect() const +{ + QRect br; +#ifndef QT_NO_TEXTCUSTOMITEM + QPtrListIterator<QTextCustomItem> l( leftItems ); + while( l.current() ) { + br = br.unite( l.current()->geometry() ); + ++l; + } + QPtrListIterator<QTextCustomItem> r( rightItems ); + while( r.current() ) { + br = br.unite( r.current()->geometry() ); + ++r; + } +#endif + return br; +} + + +void QTextFlow::drawFloatingItems( QPainter* p, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + QTextCustomItem *item; + for ( item = leftItems.first(); item; item = leftItems.next() ) { + if ( item->xpos == -1 || item->ypos == -1 ) + continue; + item->draw( p, item->xpos, item->ypos, cx, cy, cw, ch, cg, selected ); + } + + for ( item = rightItems.first(); item; item = rightItems.next() ) { + if ( item->xpos == -1 || item->ypos == -1 ) + continue; + item->draw( p, item->xpos, item->ypos, cx, cy, cw, ch, cg, selected ); + } +#endif +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#ifndef QT_NO_TEXTCUSTOMITEM +void QTextCustomItem::pageBreak( int /*y*/ , QTextFlow* /*flow*/ ) +{ +} +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +QTextTable::QTextTable( QTextDocument *p, const QMap<QString, QString> & attr ) + : QTextCustomItem( p ) +{ + cells.setAutoDelete( FALSE ); + cellspacing = 2; + if ( attr.contains("cellspacing") ) + cellspacing = attr["cellspacing"].toInt(); + cellpadding = 1; + if ( attr.contains("cellpadding") ) + cellpadding = attr["cellpadding"].toInt(); + border = innerborder = 0; + if ( attr.contains("border" ) ) { + QString s( attr["border"] ); + if ( s == "TRUE" ) + border = 1; + else + border = attr["border"].toInt(); + } + us_b = border; + + innerborder = us_ib = border ? 1 : 0; + + if ( border ) + cellspacing += 2; + + us_ib = innerborder; + us_cs = cellspacing; + us_cp = cellpadding; + outerborder = cellspacing + border; + us_ob = outerborder; + layout = new QGridLayout( 1, 1, cellspacing ); + + fixwidth = 0; + stretch = 0; + if ( attr.contains("width") ) { + bool b; + QString s( attr["width"] ); + int w = s.toInt( &b ); + if ( b ) { + fixwidth = w; + } else { + s = s.stripWhiteSpace(); + if ( s.length() > 1 && s[ (int)s.length()-1 ] == '%' ) + stretch = s.left( s.length()-1).toInt(); + } + } + us_fixwidth = fixwidth; + + place = PlaceInline; + if ( attr["align"] == "left" ) + place = PlaceLeft; + else if ( attr["align"] == "right" ) + place = PlaceRight; + cachewidth = 0; + attributes = attr; + pageBreakFor = -1; +} + +QTextTable::~QTextTable() +{ + delete layout; +} + +QString QTextTable::richText() const +{ + QString s; + s = "<table "; + QMap<QString, QString>::ConstIterator it = attributes.begin(); + for ( ; it != attributes.end(); ++it ) + s += it.key() + "=" + *it + " "; + s += ">\n"; + + int lastRow = -1; + bool needEnd = FALSE; + QPtrListIterator<QTextTableCell> it2( cells ); + while ( it2.current() ) { + QTextTableCell *cell = it2.current(); + ++it2; + if ( lastRow != cell->row() ) { + if ( lastRow != -1 ) + s += "</tr>\n"; + s += "<tr>"; + lastRow = cell->row(); + needEnd = TRUE; + } + s += "<td"; + it = cell->attributes.begin(); + for ( ; it != cell->attributes.end(); ++it ) + s += " " + it.key() + "=" + *it; + s += ">"; + s += cell->richText()->richText(); + s += "</td>"; + } + if ( needEnd ) + s += "</tr>\n"; + s += "</table>\n"; + return s; +} + +void QTextTable::setParagraph(QTextParagraph *p) +{ + for ( QTextTableCell* cell = cells.first(); cell; cell = cells.next() ) + cell->richText()->parentPar = p; + QTextCustomItem::setParagraph(p); +} + +void QTextTable::adjustToPainter( QPainter* p ) +{ + cellspacing = scale( us_cs, p ); + cellpadding = scale( us_cp, p ); + border = scale( us_b , p ); + innerborder = scale( us_ib, p ); + outerborder = scale( us_ob ,p ); + fixwidth = scale( us_fixwidth, p); + width = 0; + cachewidth = 0; + for ( QTextTableCell* cell = cells.first(); cell; cell = cells.next() ) + cell->adjustToPainter( p ); +} + +void QTextTable::adjustCells( int y , int shift ) +{ + QPtrListIterator<QTextTableCell> it( cells ); + QTextTableCell* cell; + bool enlarge = FALSE; + while ( ( cell = it.current() ) ) { + ++it; + QRect r = cell->geometry(); + if ( y <= r.top() ) { + r.moveBy(0, shift ); + cell->setGeometry( r ); + enlarge = TRUE; + } else if ( y <= r.bottom() ) { + r.rBottom() += shift; + cell->setGeometry( r ); + enlarge = TRUE; + } + } + if ( enlarge ) + height += shift; +} + +void QTextTable::pageBreak( int yt, QTextFlow* flow ) +{ + if ( flow->pageSize() <= 0 ) + return; + if ( layout && pageBreakFor > 0 && pageBreakFor != yt ) { + layout->invalidate(); + int h = layout->heightForWidth( width-2*outerborder ); + layout->setGeometry( QRect(0, 0, width-2*outerborder, h) ); + height = layout->geometry().height()+2*outerborder; + } + pageBreakFor = yt; + QPtrListIterator<QTextTableCell> it( cells ); + QTextTableCell* cell; + while ( ( cell = it.current() ) ) { + ++it; + int y = yt + outerborder + cell->geometry().y(); + int shift = flow->adjustFlow( y - cellspacing, width, cell->richText()->height() + 2*cellspacing ); + adjustCells( y - outerborder - yt, shift ); + } +} + + +void QTextTable::draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ) +{ + if ( placement() != PlaceInline ) { + x = xpos; + y = ypos; + } + + for (QTextTableCell* cell = cells.first(); cell; cell = cells.next() ) { + if ( cx < 0 && cy < 0 || + QRect( cx, cy, cw, ch ).intersects( QRect( x + outerborder + cell->geometry().x(), + y + outerborder + cell->geometry().y(), + cell->geometry().width(), cell->geometry().height() ) ) ) { + cell->draw( p, x+outerborder, y+outerborder, cx, cy, cw, ch, cg, selected ); + if ( border ) { + QRect r( x+outerborder+cell->geometry().x() - innerborder, + y+outerborder+cell->geometry().y() - innerborder, + cell->geometry().width() + 2 * innerborder, + cell->geometry().height() + 2 * innerborder ); + if ( is_printer( p ) ) { + QPen oldPen = p->pen(); + QRect r2 = r; + r2.addCoords( innerborder/2, innerborder/2, -innerborder/2, -innerborder/2 ); + p->setPen( QPen( cg.text(), innerborder ) ); + p->drawRect( r2 ); + p->setPen( oldPen ); + } else { + int s = QMAX( cellspacing-2*innerborder, 0); + if ( s ) { + p->fillRect( r.left()-s, r.top(), s+1, r.height(), cg.button() ); + p->fillRect( r.right(), r.top(), s+1, r.height(), cg.button() ); + p->fillRect( r.left()-s, r.top()-s, r.width()+2*s, s, cg.button() ); + p->fillRect( r.left()-s, r.bottom(), r.width()+2*s, s, cg.button() ); + } + qDrawShadePanel( p, r, cg, TRUE, innerborder ); + } + } + } + } + if ( border ) { + QRect r ( x, y, width, height ); + if ( is_printer( p ) ) { + QRect r2 = r; + r2.addCoords( border/2, border/2, -border/2, -border/2 ); + QPen oldPen = p->pen(); + p->setPen( QPen( cg.text(), border ) ); + p->drawRect( r2 ); + p->setPen( oldPen ); + } else { + int s = border+QMAX( cellspacing-2*innerborder, 0); + if ( s ) { + p->fillRect( r.left(), r.top(), s, r.height(), cg.button() ); + p->fillRect( r.right()-s, r.top(), s, r.height(), cg.button() ); + p->fillRect( r.left(), r.top(), r.width(), s, cg.button() ); + p->fillRect( r.left(), r.bottom()-s, r.width(), s, cg.button() ); + } + qDrawShadePanel( p, r, cg, FALSE, border ); + } + } + +} + +int QTextTable::minimumWidth() const +{ + return fixwidth ? fixwidth : ((layout ? layout->minimumSize().width() : 0) + 2 * outerborder); +} + +void QTextTable::resize( int nwidth ) +{ + if ( fixwidth && cachewidth != 0 ) + return; + if ( nwidth == cachewidth ) + return; + + + cachewidth = nwidth; + int w = nwidth; + + format( w ); + + if ( stretch ) + nwidth = nwidth * stretch / 100; + + width = nwidth; + layout->invalidate(); + int shw = layout->sizeHint().width() + 2*outerborder; + int mw = layout->minimumSize().width() + 2*outerborder; + if ( stretch ) + width = QMAX( mw, nwidth ); + else + width = QMAX( mw, QMIN( nwidth, shw ) ); + + if ( fixwidth ) + width = fixwidth; + + layout->invalidate(); + mw = layout->minimumSize().width() + 2*outerborder; + width = QMAX( width, mw ); + + int h = layout->heightForWidth( width-2*outerborder ); + layout->setGeometry( QRect(0, 0, width-2*outerborder, h) ); + height = layout->geometry().height()+2*outerborder; +} + +void QTextTable::format( int w ) +{ + for ( int i = 0; i < (int)cells.count(); ++i ) { + QTextTableCell *cell = cells.at( i ); + QRect r = cell->geometry(); + r.setWidth( w - 2*outerborder ); + cell->setGeometry( r ); + } +} + +void QTextTable::addCell( QTextTableCell* cell ) +{ + cells.append( cell ); + layout->addMultiCell( cell, cell->row(), cell->row() + cell->rowspan()-1, + cell->column(), cell->column() + cell->colspan()-1 ); +} + +bool QTextTable::enter( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd ) +{ + currCell.remove( c ); + if ( !atEnd ) + return next( c, doc, parag, idx, ox, oy ); + currCell.insert( c, cells.count() ); + return prev( c, doc, parag, idx, ox, oy ); +} + +bool QTextTable::enterAt( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, const QPoint &pos ) +{ + currCell.remove( c ); + int lastCell = -1; + int lastY = -1; + int i; + for ( i = 0; i < (int)cells.count(); ++i ) { + QTextTableCell *cell = cells.at( i ); + if ( !cell ) + continue; + QRect r( cell->geometry().x(), + cell->geometry().y(), + cell->geometry().width() + 2 * innerborder + 2 * outerborder, + cell->geometry().height() + 2 * innerborder + 2 * outerborder ); + + if ( r.left() <= pos.x() && r.right() >= pos.x() ) { + if ( cell->geometry().y() > lastY ) { + lastCell = i; + lastY = cell->geometry().y(); + } + if ( r.top() <= pos.y() && r.bottom() >= pos.y() ) { + currCell.insert( c, i ); + break; + } + } + } + if ( i == (int) cells.count() ) + return FALSE; // no cell found + + if ( currCell.find( c ) == currCell.end() ) { + if ( lastY != -1 ) + currCell.insert( c, lastCell ); + else + return FALSE; + } + + QTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( !cell ) + return FALSE; + doc = cell->richText(); + parag = doc->firstParagraph(); + idx = 0; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool QTextTable::next( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + int cc = -1; + if ( currCell.find( c ) != currCell.end() ) + cc = *currCell.find( c ); + if ( cc > (int)cells.count() - 1 || cc < 0 ) + cc = -1; + currCell.remove( c ); + currCell.insert( c, ++cc ); + if ( cc >= (int)cells.count() ) { + currCell.insert( c, 0 ); + QTextCustomItem::next( c, doc, parag, idx, ox, oy ); + QTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + QTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( !cell ) + return FALSE; + doc = cell->richText(); + parag = doc->firstParagraph(); + idx = 0; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool QTextTable::prev( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + int cc = -1; + if ( currCell.find( c ) != currCell.end() ) + cc = *currCell.find( c ); + if ( cc > (int)cells.count() - 1 || cc < 0 ) + cc = cells.count(); + currCell.remove( c ); + currCell.insert( c, --cc ); + if ( cc < 0 ) { + currCell.insert( c, 0 ); + QTextCustomItem::prev( c, doc, parag, idx, ox, oy ); + QTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + QTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( !cell ) + return FALSE; + doc = cell->richText(); + parag = doc->lastParagraph(); + idx = parag->length() - 1; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool QTextTable::down( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + QTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( cell->row_ == layout->numRows() - 1 ) { + currCell.insert( c, 0 ); + QTextCustomItem::down( c, doc, parag, idx, ox, oy ); + QTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + int oldRow = cell->row_; + int oldCol = cell->col_; + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + int cc = *currCell.find( c ); + for ( int i = cc; i < (int)cells.count(); ++i ) { + cell = cells.at( i ); + if ( cell->row_ > oldRow && cell->col_ == oldCol ) { + currCell.insert( c, i ); + break; + } + } + doc = cell->richText(); + if ( !cell ) + return FALSE; + parag = doc->firstParagraph(); + idx = 0; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool QTextTable::up( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + QTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( cell->row_ == 0 ) { + currCell.insert( c, 0 ); + QTextCustomItem::up( c, doc, parag, idx, ox, oy ); + QTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + int oldRow = cell->row_; + int oldCol = cell->col_; + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + int cc = *currCell.find( c ); + for ( int i = cc; i >= 0; --i ) { + cell = cells.at( i ); + if ( cell->row_ < oldRow && cell->col_ == oldCol ) { + currCell.insert( c, i ); + break; + } + } + doc = cell->richText(); + if ( !cell ) + return FALSE; + parag = doc->lastParagraph(); + idx = parag->length() - 1; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +QTextTableCell::QTextTableCell( QTextTable* table, + int row, int column, + const QMap<QString, QString> &attr, + const QStyleSheetItem* /*style*/, // ### use them + const QTextFormat& fmt, const QString& context, + QMimeSourceFactory &factory, QStyleSheet *sheet, + const QString& doc) +{ + cached_width = -1; + cached_sizehint = -1; + + maxw = QWIDGETSIZE_MAX; + minw = 0; + + parent = table; + row_ = row; + col_ = column; + stretch_ = 0; + richtext = new QTextDocument( table->parent ); + richtext->formatCollection()->setPaintDevice( table->parent->formatCollection()->paintDevice() ); + richtext->bodyText = fmt.color(); + richtext->setTableCell( this ); + QString a = *attr.find( "align" ); + if ( !a.isEmpty() ) { + a = a.lower(); + if ( a == "left" ) + richtext->setAlignment( Qt::AlignLeft ); + else if ( a == "center" ) + richtext->setAlignment( Qt::AlignHCenter ); + else if ( a == "right" ) + richtext->setAlignment( Qt::AlignRight ); + } + align = 0; + QString va = *attr.find( "valign" ); + if ( !va.isEmpty() ) { + va = va.lower(); + if ( va == "top" ) + align |= Qt::AlignTop; + else if ( va == "center" || va == "middle" ) + align |= Qt::AlignVCenter; + else if ( va == "bottom" ) + align |= Qt::AlignBottom; + } + richtext->setFormatter( table->parent->formatter() ); + richtext->setUseFormatCollection( table->parent->useFormatCollection() ); + richtext->setMimeSourceFactory( &factory ); + richtext->setStyleSheet( sheet ); + richtext->setRichText( doc, context, &fmt ); + rowspan_ = 1; + colspan_ = 1; + if ( attr.contains("colspan") ) + colspan_ = attr["colspan"].toInt(); + if ( attr.contains("rowspan") ) + rowspan_ = attr["rowspan"].toInt(); + + background = 0; + if ( attr.contains("bgcolor") ) { + background = new QBrush(QColor( attr["bgcolor"] )); + } + + + hasFixedWidth = FALSE; + if ( attr.contains("width") ) { + bool b; + QString s( attr["width"] ); + int w = s.toInt( &b ); + if ( b ) { + maxw = w; + minw = maxw; + hasFixedWidth = TRUE; + } else { + s = s.stripWhiteSpace(); + if ( s.length() > 1 && s[ (int)s.length()-1 ] == '%' ) + stretch_ = s.left( s.length()-1).toInt(); + } + } + + attributes = attr; + + parent->addCell( this ); +} + +QTextTableCell::~QTextTableCell() +{ + delete background; + background = 0; + delete richtext; + richtext = 0; +} + +QSize QTextTableCell::sizeHint() const +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding + border_tolerance); + int used = richtext->widthUsed() + extra; + + if (stretch_ ) { + int w = parent->width * stretch_ / 100 - 2*parent->cellspacing - 2*parent->cellpadding; + return QSize( QMIN( w, maxw ), 0 ).expandedTo( minimumSize() ); + } + + return QSize( used, 0 ).expandedTo( minimumSize() ); +} + +QSize QTextTableCell::minimumSize() const +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding + border_tolerance); + return QSize( QMAX( richtext->minimumWidth() + extra, minw), 0 ); +} + +QSize QTextTableCell::maximumSize() const +{ + return QSize( maxw, QWIDGETSIZE_MAX ); +} + +QSizePolicy::ExpandData QTextTableCell::expanding() const +{ + return QSizePolicy::BothDirections; +} + +bool QTextTableCell::isEmpty() const +{ + return FALSE; +} +void QTextTableCell::setGeometry( const QRect& r ) +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding ); + if ( r.width() != cached_width ) + richtext->doLayout( QTextFormat::painter(), r.width() - extra ); + cached_width = r.width(); + geom = r; +} + +QRect QTextTableCell::geometry() const +{ + return geom; +} + +bool QTextTableCell::hasHeightForWidth() const +{ + return TRUE; +} + +int QTextTableCell::heightForWidth( int w ) const +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding ); + w = QMAX( minw, w ); + + if ( cached_width != w ) { + QTextTableCell* that = (QTextTableCell*) this; + that->richtext->doLayout( QTextFormat::painter(), w - extra ); + that->cached_width = w; + } + return richtext->height() + extra; +} + +void QTextTableCell::adjustToPainter( QPainter* p ) +{ + QTextParagraph *parag = richtext->firstParagraph(); + while ( parag ) { + parag->adjustToPainter( p ); + parag = parag->next(); + } +} + +int QTextTableCell::horizontalAlignmentOffset() const +{ + return parent->cellpadding; +} + +int QTextTableCell::verticalAlignmentOffset() const +{ + if ( (align & Qt::AlignVCenter ) == Qt::AlignVCenter ) + return ( geom.height() - richtext->height() ) / 2; + else if ( ( align & Qt::AlignBottom ) == Qt::AlignBottom ) + return geom.height() - parent->cellpadding - richtext->height() ; + return parent->cellpadding; +} + +void QTextTableCell::draw( QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool ) +{ + if ( cached_width != geom.width() ) { + int extra = 2 * ( parent->innerborder + parent->cellpadding ); + richtext->doLayout( p, geom.width() - extra ); + cached_width = geom.width(); + } + QColorGroup g( cg ); + if ( background ) + g.setBrush( QColorGroup::Base, *background ); + else if ( richtext->paper() ) + g.setBrush( QColorGroup::Base, *richtext->paper() ); + + p->save(); + p->translate( x + geom.x(), y + geom.y() ); + if ( background ) + p->fillRect( 0, 0, geom.width(), geom.height(), *background ); + else if ( richtext->paper() ) + p->fillRect( 0, 0, geom.width(), geom.height(), *richtext->paper() ); + + p->translate( horizontalAlignmentOffset(), verticalAlignmentOffset() ); + + QRegion r; + if ( cx >= 0 && cy >= 0 ) + richtext->draw( p, cx - ( x + horizontalAlignmentOffset() + geom.x() ), + cy - ( y + geom.y() + verticalAlignmentOffset() ), + cw, ch, g, FALSE, FALSE, 0 ); + else + richtext->draw( p, -1, -1, -1, -1, g, FALSE, FALSE, 0 ); + + p->restore(); +} +#endif + +#endif //QT_NO_RICHTEXT diff --git a/src/kernel/qrichtext_p.cpp b/src/kernel/qrichtext_p.cpp new file mode 100644 index 0000000..cf79a51 --- /dev/null +++ b/src/kernel/qrichtext_p.cpp @@ -0,0 +1,636 @@ +/**************************************************************************** +** +** Implementation of the internal Qt classes dealing with rich text +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qrichtext_p.h" + +#ifndef QT_NO_RICHTEXT + +QTextCommand::~QTextCommand() {} +QTextCommand::Commands QTextCommand::type() const { return Invalid; } + + +#ifndef QT_NO_TEXTCUSTOMITEM +QTextCustomItem::~QTextCustomItem() {} +void QTextCustomItem::adjustToPainter( QPainter* p){ if ( p ) width = 0; } +QTextCustomItem::Placement QTextCustomItem::placement() const { return PlaceInline; } + +bool QTextCustomItem::ownLine() const { return FALSE; } +void QTextCustomItem::resize( int nwidth ){ width = nwidth; } +void QTextCustomItem::invalidate() {} + +bool QTextCustomItem::isNested() const { return FALSE; } +int QTextCustomItem::minimumWidth() const { return 0; } + +QString QTextCustomItem::richText() const { return QString::null; } + +bool QTextCustomItem::enter( QTextCursor *, QTextDocument*&, QTextParagraph *&, int &, int &, int &, bool ) +{ + return TRUE; +} +bool QTextCustomItem::enterAt( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int &, const QPoint & ) +{ + return TRUE; +} +bool QTextCustomItem::next( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +bool QTextCustomItem::prev( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +bool QTextCustomItem::down( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +bool QTextCustomItem::up( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +#endif // QT_NO_TEXTCUSTOMITEM + +void QTextFlow::setPageSize( int ps ) { pagesize = ps; } +#ifndef QT_NO_TEXTCUSTOMITEM +bool QTextFlow::isEmpty() { return leftItems.isEmpty() && rightItems.isEmpty(); } +#else +bool QTextFlow::isEmpty() { return TRUE; } +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +void QTextTableCell::invalidate() { cached_width = -1; cached_sizehint = -1; } + +void QTextTable::invalidate() { cachewidth = -1; } +#endif + +QTextParagraphData::~QTextParagraphData() {} +void QTextParagraphData::join( QTextParagraphData * ) {} + +QTextFormatter::~QTextFormatter() {} +void QTextFormatter::setWrapEnabled( bool b ) { wrapEnabled = b; } +void QTextFormatter::setWrapAtColumn( int c ) { wrapColumn = c; } + + + +int QTextCursor::x() const +{ + if ( idx >= para->length() ) + return 0; + QTextStringChar *c = para->at( idx ); + int curx = c->x; + if ( !c->rightToLeft && + c->c.isSpace() && + idx > 0 && + para->at( idx - 1 )->c != '\t' && + !c->lineStart && + ( para->alignment() & Qt::AlignJustify ) == Qt::AlignJustify ) + curx = para->at( idx - 1 )->x + para->string()->width( idx - 1 ); + if ( c->rightToLeft ) + curx += para->string()->width( idx ); + return curx; +} + +int QTextCursor::y() const +{ + int dummy, line; + para->lineStartOfChar( idx, &dummy, &line ); + return para->lineY( line ); +} + +int QTextCursor::globalX() const { return totalOffsetX() + para->rect().x() + x(); } +int QTextCursor::globalY() const { return totalOffsetY() + para->rect().y() + y(); } + +QTextDocument *QTextCursor::document() const +{ + return para ? para->document() : 0; +} + +void QTextCursor::gotoPosition( QTextParagraph* p, int index ) +{ + if ( para && p != para ) { + while ( !indices.isEmpty() && para->document() != p->document() ) + pop(); + Q_ASSERT( indices.isEmpty() || para->document() == p->document() ); + } + para = p; + if ( index < 0 || index >= para->length() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QTextCursor::gotoParagraph Index: %d out of range", index ); +#endif + if ( index < 0 || para->length() == 0 ) + index = 0; + else + index = para->length() - 1; + } + + tmpX = -1; + idx = index; + fixCursorPosition(); +} + +bool QTextDocument::hasSelection( int id, bool visible ) const +{ + return ( selections.find( id ) != selections.end() && + ( !visible || + ( (QTextDocument*)this )->selectionStartCursor( id ) != + ( (QTextDocument*)this )->selectionEndCursor( id ) ) ); +} + +void QTextDocument::setSelectionStart( int id, const QTextCursor &cursor ) +{ + QTextDocumentSelection sel; + sel.startCursor = cursor; + sel.endCursor = cursor; + sel.swapped = FALSE; + selections[ id ] = sel; +} + +QTextParagraph *QTextDocument::paragAt( int i ) const +{ + QTextParagraph* p = curParag; + if ( !p || p->paragId() > i ) + p = fParag; + while ( p && p->paragId() != i ) + p = p->next(); + ((QTextDocument*)this)->curParag = p; + return p; +} + + +QTextFormat::~QTextFormat() +{ +} + +QTextFormat::QTextFormat() + : fm( QFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ) +{ + ref = 0; + + usePixelSizes = FALSE; + if ( stdSize == -1 ) { + stdSize = qApp->font().pixelSize(); + usePixelSizes = TRUE; + } + + missp = FALSE; + ha = AlignNormal; + collection = 0; +} + +QTextFormat::QTextFormat( const QStyleSheetItem *style ) + : fm( QFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ) +{ + ref = 0; + + usePixelSizes = FALSE; + if ( stdSize == -1 ) { + stdSize = qApp->font().pixelSize(); + usePixelSizes = TRUE; + } + + missp = FALSE; + ha = AlignNormal; + collection = 0; + fn = QFont( style->fontFamily(), + style->fontSize(), + style->fontWeight(), + style->fontItalic() ); + fn.setUnderline( style->fontUnderline() ); + fn.setStrikeOut( style->fontStrikeOut() ); + col = style->color(); + fm = QFontMetrics( fn ); + leftBearing = fm.minLeftBearing(); + rightBearing = fm.minRightBearing(); + hei = fm.lineSpacing(); + asc = fm.ascent() + (fm.leading()+1)/2; + dsc = fm.descent(); + missp = FALSE; + ha = AlignNormal; + memset( widths, 0, 256 ); + generateKey(); + addRef(); +} + +QTextFormat::QTextFormat( const QFont &f, const QColor &c, QTextFormatCollection *parent ) + : fn( f ), col( c ), fm( QFontMetrics( f ) ), linkColor( TRUE ), + logicalFontSize( 3 ), stdSize( f.pointSize() ) +{ + ref = 0; + usePixelSizes = FALSE; + if ( stdSize == -1 ) { + stdSize = f.pixelSize(); + usePixelSizes = TRUE; + } + collection = parent; + leftBearing = fm.minLeftBearing(); + rightBearing = fm.minRightBearing(); + hei = fm.lineSpacing(); + asc = fm.ascent() + (fm.leading()+1)/2; + dsc = fm.descent(); + missp = FALSE; + ha = AlignNormal; + memset( widths, 0, 256 ); + generateKey(); + addRef(); +} + +QTextFormat::QTextFormat( const QTextFormat &f ) + : fm( f.fm ) +{ + ref = 0; + collection = 0; + fn = f.fn; + col = f.col; + leftBearing = f.leftBearing; + rightBearing = f.rightBearing; + memset( widths, 0, 256 ); + hei = f.hei; + asc = f.asc; + dsc = f.dsc; + stdSize = f.stdSize; + usePixelSizes = f.usePixelSizes; + logicalFontSize = f.logicalFontSize; + missp = f.missp; + ha = f.ha; + k = f.k; + linkColor = f.linkColor; + addRef(); +} + +QTextFormat& QTextFormat::operator=( const QTextFormat &f ) +{ + ref = 0; + collection = f.collection; + fn = f.fn; + col = f.col; + fm = f.fm; + leftBearing = f.leftBearing; + rightBearing = f.rightBearing; + memset( widths, 0, 256 ); + hei = f.hei; + asc = f.asc; + dsc = f.dsc; + stdSize = f.stdSize; + usePixelSizes = f.usePixelSizes; + logicalFontSize = f.logicalFontSize; + missp = f.missp; + ha = f.ha; + k = f.k; + linkColor = f.linkColor; + addRef(); + return *this; +} + +void QTextFormat::update() +{ + fm = QFontMetrics( fn ); + leftBearing = fm.minLeftBearing(); + rightBearing = fm.minRightBearing(); + hei = fm.lineSpacing(); + asc = fm.ascent() + (fm.leading()+1)/2; + dsc = fm.descent(); + memset( widths, 0, 256 ); + generateKey(); +} + + +QPainter* QTextFormat::pntr = 0; +QFontMetrics* QTextFormat::pntr_fm = 0; +int QTextFormat::pntr_ldg=-1; +int QTextFormat::pntr_asc=-1; +int QTextFormat::pntr_hei=-1; +int QTextFormat::pntr_dsc=-1; + +void QTextFormat::setPainter( QPainter *p ) +{ + pntr = p; +} + +QPainter* QTextFormat::painter() +{ + return pntr; +} + +void QTextFormat::applyFont( const QFont &f ) +{ + QFontMetrics fm( pntr->fontMetrics() ); + if ( !pntr_fm + || pntr_fm->painter != pntr + || pntr_fm->d != fm.d + || !pntr->font().isCopyOf( f ) ) { + pntr->setFont( f ); + delete pntr_fm; + pntr_fm = new QFontMetrics( pntr->fontMetrics() ); + pntr_ldg = pntr_fm->leading(); + pntr_asc = pntr_fm->ascent()+(pntr_ldg+1)/2; + pntr_hei = pntr_fm->lineSpacing(); + pntr_dsc = -1; + } +} + +int QTextFormat::minLeftBearing() const +{ + if ( !pntr || !pntr->isActive() ) + return leftBearing; + applyFont( fn ); + return pntr_fm->minLeftBearing(); +} + +int QTextFormat::minRightBearing() const +{ + if ( !pntr || !pntr->isActive() ) + return rightBearing; + applyFont( fn ); + return pntr_fm->minRightBearing(); +} + +int QTextFormat::height() const +{ + if ( !pntr || !pntr->isActive() ) + return hei; + applyFont( fn ); + return pntr_hei; +} + +int QTextFormat::ascent() const +{ + if ( !pntr || !pntr->isActive() ) + return asc; + applyFont( fn ); + return pntr_asc; +} + +int QTextFormat::descent() const +{ + if ( !pntr || !pntr->isActive() ) + return dsc; + applyFont( fn ); + if ( pntr_dsc < 0 ) + pntr_dsc = pntr_fm->descent(); + return pntr_dsc; +} + +int QTextFormat::leading() const +{ + if ( !pntr || !pntr->isActive() ) + return fm.leading(); + applyFont( fn ); + return pntr_ldg; +} + +void QTextFormat::generateKey() +{ + k = getKey( fn, col, isMisspelled(), vAlign() ); +} + +QString QTextFormat::getKey( const QFont &fn, const QColor &col, bool misspelled, VerticalAlignment a ) +{ + QString k = fn.key(); + k += '/'; + k += QString::number( (uint)col.rgb() ); + k += '/'; + k += QString::number( (int)misspelled ); + k += '/'; + k += QString::number( (int)a ); + return k; +} + +QString QTextString::toString( const QMemArray<QTextStringChar> &data ) +{ + QString s; + int l = data.size(); + s.setUnicode( 0, l ); + QTextStringChar *c = data.data(); + QChar *uc = (QChar *)s.unicode(); + while ( l-- ) + *(uc++) = (c++)->c; + + return s; +} + +void QTextParagraph::setSelection( int id, int start, int end ) +{ + QMap<int, QTextParagraphSelection>::ConstIterator it = selections().find( id ); + if ( it != mSelections->end() ) { + if ( start == ( *it ).start && end == ( *it ).end ) + return; + } + + QTextParagraphSelection sel; + sel.start = start; + sel.end = end; + (*mSelections)[ id ] = sel; + setChanged( TRUE, TRUE ); +} + +void QTextParagraph::removeSelection( int id ) +{ + if ( !hasSelection( id ) ) + return; + if ( mSelections ) + mSelections->remove( id ); + setChanged( TRUE, TRUE ); +} + +int QTextParagraph::selectionStart( int id ) const +{ + if ( !mSelections ) + return -1; + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( id ); + if ( it == mSelections->end() ) + return -1; + return ( *it ).start; +} + +int QTextParagraph::selectionEnd( int id ) const +{ + if ( !mSelections ) + return -1; + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( id ); + if ( it == mSelections->end() ) + return -1; + return ( *it ).end; +} + +bool QTextParagraph::hasSelection( int id ) const +{ + return mSelections ? mSelections->contains( id ) : FALSE; +} + +bool QTextParagraph::fullSelected( int id ) const +{ + if ( !mSelections ) + return FALSE; + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( id ); + if ( it == mSelections->end() ) + return FALSE; + return ( *it ).start == 0 && ( *it ).end == str->length() - 1; +} + +int QTextParagraph::lineY( int l ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "QTextParagraph::lineY: line %d out of range!", l ); + return 0; + } + + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + return ( *it )->y; +} + +int QTextParagraph::lineBaseLine( int l ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "QTextParagraph::lineBaseLine: line %d out of range!", l ); + return 10; + } + + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + return ( *it )->baseLine; +} + +int QTextParagraph::lineHeight( int l ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "QTextParagraph::lineHeight: line %d out of range!", l ); + return 15; + } + + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + return ( *it )->h; +} + +void QTextParagraph::lineInfo( int l, int &y, int &h, int &bl ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "QTextParagraph::lineInfo: line %d out of range!", l ); + qDebug( "%d %d", (int)lineStarts.count() - 1, l ); + y = 0; + h = 15; + bl = 10; + return; + } + + if ( !isValid() ) + ( (QTextParagraph*)this )->format(); + + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + y = ( *it )->y; + h = ( *it )->h; + bl = ( *it )->baseLine; +} + + +void QTextParagraph::setAlignment( int a ) +{ + if ( a == (int)align ) + return; + align = a; + invalidate( 0 ); +} + +QTextFormatter *QTextParagraph::formatter() const +{ + if ( hasdoc ) + return document()->formatter(); + if ( pseudoDocument()->pFormatter ) + return pseudoDocument()->pFormatter; + return ( ( (QTextParagraph*)this )->pseudoDocument()->pFormatter = new QTextFormatterBreakWords ); +} + +void QTextParagraph::setTabArray( int *a ) +{ + delete [] tArray; + tArray = a; +} + +void QTextParagraph::setTabStops( int tw ) +{ + if ( hasdoc ) + document()->setTabStops( tw ); + else + tabStopWidth = tw; +} + +QMap<int, QTextParagraphSelection> &QTextParagraph::selections() const +{ + if ( !mSelections ) + ((QTextParagraph *)this)->mSelections = new QMap<int, QTextParagraphSelection>; + return *mSelections; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +QPtrList<QTextCustomItem> &QTextParagraph::floatingItems() const +{ + if ( !mFloatingItems ) + ((QTextParagraph *)this)->mFloatingItems = new QPtrList<QTextCustomItem>; + return *mFloatingItems; +} +#endif + +QTextStringChar::~QTextStringChar() +{ + if ( format() ) + format()->removeRef(); + if ( type ) // not Regular + delete d.custom; +} + +QTextParagraphPseudoDocument::QTextParagraphPseudoDocument():pFormatter(0),commandHistory(0), minw(0),wused(0),collection(){} +QTextParagraphPseudoDocument::~QTextParagraphPseudoDocument(){ delete pFormatter; delete commandHistory; } + + +#endif //QT_NO_RICHTEXT diff --git a/src/kernel/qrichtext_p.h b/src/kernel/qrichtext_p.h new file mode 100644 index 0000000..614841e --- /dev/null +++ b/src/kernel/qrichtext_p.h @@ -0,0 +1,2141 @@ +/**************************************************************************** +** +** Definition of internal rich text classes +** +** Created : 990124 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QRICHTEXT_P_H +#define QRICHTEXT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of a number of Qt sources files. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qstring.h" +#include "qptrlist.h" +#include "qrect.h" +#include "qfontmetrics.h" +#include "qintdict.h" +#include "qmap.h" +#include "qstringlist.h" +#include "qfont.h" +#include "qcolor.h" +#include "qsize.h" +#include "qvaluelist.h" +#include "qvaluestack.h" +#include "qobject.h" +#include "qdict.h" +#include "qpixmap.h" +#include "qstylesheet.h" +#include "qptrvector.h" +#include "qpainter.h" +#include "qlayout.h" +#include "qobject.h" +#include "qapplication.h" +#endif // QT_H + +#ifndef QT_NO_RICHTEXT + +class QTextDocument; +class QTextString; +class QTextPreProcessor; +class QTextFormat; +class QTextCursor; +class QTextParagraph; +class QTextFormatter; +class QTextIndent; +class QTextFormatCollection; +class QStyleSheetItem; +#ifndef QT_NO_TEXTCUSTOMITEM +class QTextCustomItem; +#endif +class QTextFlow; +struct QBidiContext; + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextStringChar +{ + friend class QTextString; + +public: + // this is never called, initialize variables in QTextString::insert()!!! + QTextStringChar() : nobreak(FALSE), lineStart( 0 ), type( Regular ) {d.format=0;} + ~QTextStringChar(); + + struct CustomData + { + QTextFormat *format; +#ifndef QT_NO_TEXTCUSTOMITEM + QTextCustomItem *custom; +#endif + QString anchorName; + QString anchorHref; + }; + enum Type { Regular=0, Custom=1, Anchor=2, CustomAnchor=3 }; + + QChar c; + // this is the same struct as in qtextengine_p.h. Don't change! + uchar softBreak :1; // Potential linebreak point + uchar whiteSpace :1; // A unicode whitespace character, except NBSP, ZWNBSP + uchar charStop :1; // Valid cursor position (for left/right arrow) + uchar wordStop :1; // Valid cursor position (for ctrl + left/right arrow) + uchar nobreak :1; + + uchar lineStart : 1; + uchar /*Type*/ type : 2; + uchar bidiLevel :7; + uchar rightToLeft : 1; + + int x; + union { + QTextFormat* format; + CustomData* custom; + } d; + + + int height() const; + int ascent() const; + int descent() const; + bool isCustom() const { return (type & Custom) != 0; } + QTextFormat *format() const; +#ifndef QT_NO_TEXTCUSTOMITEM + QTextCustomItem *customItem() const; +#endif + void setFormat( QTextFormat *f ); +#ifndef QT_NO_TEXTCUSTOMITEM + void setCustomItem( QTextCustomItem *i ); +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM + void loseCustomItem(); +#endif + + + bool isAnchor() const { return ( type & Anchor) != 0; } + bool isLink() const { return isAnchor() && !!d.custom->anchorHref; } + QString anchorName() const; + QString anchorHref() const; + void setAnchor( const QString& name, const QString& href ); + +private: + QTextStringChar &operator=( const QTextStringChar & ) { + //abort(); + return *this; + } + QTextStringChar( const QTextStringChar & ) { + } + friend class QTextParagraph; +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QMemArray<QTextStringChar>; +// MOC_SKIP_END +#endif + +class Q_EXPORT QTextString +{ +public: + + QTextString(); + QTextString( const QTextString &s ); + virtual ~QTextString(); + + static QString toString( const QMemArray<QTextStringChar> &data ); + QString toString() const; + + inline QTextStringChar &at( int i ) const { return data[ i ]; } + inline int length() const { return data.size(); } + + int width( int idx ) const; + + void insert( int index, const QString &s, QTextFormat *f ); + void insert( int index, const QChar *unicode, int len, QTextFormat *f ); + void insert( int index, QTextStringChar *c, bool doAddRefFormat = FALSE ); + void truncate( int index ); + void remove( int index, int len ); + void clear(); + + void setFormat( int index, QTextFormat *f, bool useCollection ); + + void setBidi( bool b ) { bidi = b; } + bool isBidi() const; + bool isRightToLeft() const; + QChar::Direction direction() const; + void setDirection( QChar::Direction d ) { dir = d; bidiDirty = TRUE; } + + QMemArray<QTextStringChar> rawData() const { return data.copy(); } + + void operator=( const QString &s ) { clear(); insert( 0, s, 0 ); } + void operator+=( const QString &s ) { insert( length(), s, 0 ); } + void prepend( const QString &s ) { insert( 0, s, 0 ); } + int appendParagraphs( QTextParagraph *start, QTextParagraph *end ); + + // return next and previous valid cursor positions. + bool validCursorPosition( int idx ); + int nextCursorPosition( int idx ); + int previousCursorPosition( int idx ); + +private: + void checkBidi() const; + + QMemArray<QTextStringChar> data; + QString stringCache; + uint bidiDirty : 1; + uint bidi : 1; // true when the paragraph has right to left characters + uint rightToLeft : 1; + uint dir : 5; +}; + +inline bool QTextString::isBidi() const +{ + if ( bidiDirty ) + checkBidi(); + return bidi; +} + +inline bool QTextString::isRightToLeft() const +{ + if ( bidiDirty ) + checkBidi(); + return rightToLeft; +} + +inline QString QTextString::toString() const +{ + if(bidiDirty) + checkBidi(); + return stringCache; +} + +inline QChar::Direction QTextString::direction() const +{ + return (QChar::Direction) dir; +} + +inline int QTextString::nextCursorPosition( int next ) +{ + if ( bidiDirty ) + checkBidi(); + + const QTextStringChar *c = data.data(); + int len = length(); + + if ( next < len - 1 ) { + next++; + while ( next < len - 1 && !c[next].charStop ) + next++; + } + return next; +} + +inline int QTextString::previousCursorPosition( int prev ) +{ + if ( bidiDirty ) + checkBidi(); + + const QTextStringChar *c = data.data(); + + if ( prev ) { + prev--; + while ( prev && !c[prev].charStop ) + prev--; + } + return prev; +} + +inline bool QTextString::validCursorPosition( int idx ) +{ + if ( bidiDirty ) + checkBidi(); + + return (at( idx ).charStop); +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueStack<int>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueStack<QTextParagraph*>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueStack<bool>; +// MOC_SKIP_END +#endif + +class Q_EXPORT QTextCursor +{ +public: + QTextCursor( QTextDocument *d = 0 ); + QTextCursor( const QTextCursor &c ); + QTextCursor &operator=( const QTextCursor &c ); + virtual ~QTextCursor() {} + + bool operator==( const QTextCursor &c ) const; + bool operator!=( const QTextCursor &c ) const { return !(*this == c); } + + inline QTextParagraph *paragraph() const { return para; } + + QTextDocument *document() const; + int index() const; + + void gotoPosition( QTextParagraph* p, int index = 0); + void setIndex( int index ) { gotoPosition(paragraph(), index ); } + void setParagraph( QTextParagraph*p ) { gotoPosition(p, 0 ); } + + void gotoLeft(); + void gotoRight(); + void gotoNextLetter(); + void gotoPreviousLetter(); + void gotoUp(); + void gotoDown(); + void gotoLineEnd(); + void gotoLineStart(); + void gotoHome(); + void gotoEnd(); + void gotoPageUp( int visibleHeight ); + void gotoPageDown( int visibleHeight ); + void gotoNextWord( bool onlySpace = FALSE ); + void gotoPreviousWord( bool onlySpace = FALSE ); + void gotoWordLeft(); + void gotoWordRight(); + + void insert( const QString &s, bool checkNewLine, QMemArray<QTextStringChar> *formatting = 0 ); + void splitAndInsertEmptyParagraph( bool ind = TRUE, bool updateIds = TRUE ); + bool remove(); + bool removePreviousChar(); + void indent(); + + bool atParagStart(); + bool atParagEnd(); + + int x() const; // x in current paragraph + int y() const; // y in current paragraph + + int globalX() const; + int globalY() const; + + QTextParagraph *topParagraph() const { return paras.isEmpty() ? para : paras.first(); } + int offsetX() const { return ox; } // inner document offset + int offsetY() const { return oy; } // inner document offset + int totalOffsetX() const; // total document offset + int totalOffsetY() const; // total document offset + + bool place( const QPoint &pos, QTextParagraph *s ) { return place( pos, s, FALSE ); } + bool place( const QPoint &pos, QTextParagraph *s, bool link ) { return place( pos, s, link, TRUE, TRUE ); } + bool place( const QPoint &pos, QTextParagraph *s, bool link, bool loosePlacing, bool matchBetweenCharacters ); + void restoreState(); + + + int nestedDepth() const { return (int)indices.count(); } //### size_t/int cast + void oneUp() { if ( !indices.isEmpty() ) pop(); } + void setValid( bool b ) { valid = b; } + bool isValid() const { return valid; } + + void fixCursorPosition(); +private: + enum Operation { EnterBegin, EnterEnd, Next, Prev, Up, Down }; + + void push(); + void pop(); + bool processNesting( Operation op ); + void invalidateNested(); + void gotoIntoNested( const QPoint &globalPos ); + + QTextParagraph *para; + int idx, tmpX; + int ox, oy; + QValueStack<int> indices; + QValueStack<QTextParagraph*> paras; + QValueStack<int> xOffsets; + QValueStack<int> yOffsets; + uint valid : 1; + +}; + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextCommand +{ +public: + enum Commands { Invalid, Insert, Delete, Format, Style }; + + QTextCommand( QTextDocument *d ) : doc( d ), cursor( d ) {} + virtual ~QTextCommand(); + + virtual Commands type() const; + + virtual QTextCursor *execute( QTextCursor *c ) = 0; + virtual QTextCursor *unexecute( QTextCursor *c ) = 0; + +protected: + QTextDocument *doc; + QTextCursor cursor; + +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextCommand>; +// MOC_SKIP_END +#endif + +class Q_EXPORT QTextCommandHistory +{ +public: + QTextCommandHistory( int s ) : current( -1 ), steps( s ) { history.setAutoDelete( TRUE ); } + virtual ~QTextCommandHistory(); + + void clear() { history.clear(); current = -1; } + + void addCommand( QTextCommand *cmd ); + QTextCursor *undo( QTextCursor *c ); + QTextCursor *redo( QTextCursor *c ); + + bool isUndoAvailable(); + bool isRedoAvailable(); + + void setUndoDepth( int d ) { steps = d; } + int undoDepth() const { return steps; } + + int historySize() const { return history.count(); } + int currentPosition() const { return current; } + +private: + QPtrList<QTextCommand> history; + int current, steps; + +}; + +inline QTextCommandHistory::~QTextCommandHistory() +{ + clear(); +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT QTextCustomItem +{ +public: + QTextCustomItem( QTextDocument *p ) + : xpos(0), ypos(-1), width(-1), height(0), parent( p ) + {} + virtual ~QTextCustomItem(); + virtual void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ) = 0; + + virtual void adjustToPainter( QPainter* ); + + enum Placement { PlaceInline = 0, PlaceLeft, PlaceRight }; + virtual Placement placement() const; + bool placeInline() { return placement() == PlaceInline; } + + virtual bool ownLine() const; + virtual void resize( int nwidth ); + virtual void invalidate(); + virtual int ascent() const { return height; } + + virtual bool isNested() const; + virtual int minimumWidth() const; + + virtual QString richText() const; + + int xpos; // used for floating items + int ypos; // used for floating items + int width; + int height; + + QRect geometry() const { return QRect( xpos, ypos, width, height ); } + + virtual bool enter( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); + virtual bool enterAt( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, const QPoint & ); + virtual bool next( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool prev( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool down( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool up( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + + virtual void setParagraph( QTextParagraph *p ) { parag = p; } + QTextParagraph *paragraph() const { return parag; } + + QTextDocument *parent; + QTextParagraph *parag; + + virtual void pageBreak( int y, QTextFlow* flow ); +}; +#endif + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +//Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<QString, QString>; +// MOC_SKIP_END +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT QTextImage : public QTextCustomItem +{ +public: + QTextImage( QTextDocument *p, const QMap<QString, QString> &attr, const QString& context, + QMimeSourceFactory &factory ); + virtual ~QTextImage(); + + Placement placement() const { return place; } + void adjustToPainter( QPainter* ); + int minimumWidth() const { return width; } + + QString richText() const; + + void draw( QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ); + +private: + QRegion* reg; + QPixmap pm; + Placement place; + int tmpwidth, tmpheight; + QMap<QString, QString> attributes; + QString imgId; + +}; +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT QTextHorizontalLine : public QTextCustomItem +{ +public: + QTextHorizontalLine( QTextDocument *p, const QMap<QString, QString> &attr, const QString& context, + QMimeSourceFactory &factory ); + virtual ~QTextHorizontalLine(); + + void adjustToPainter( QPainter* ); + void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ); + QString richText() const; + + bool ownLine() const { return TRUE; } + +private: + int tmpheight; + QColor color; + bool shade; + +}; +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextCustomItem>; +// MOC_SKIP_END +#endif +#endif + +class Q_EXPORT QTextFlow +{ + friend class QTextDocument; +#ifndef QT_NO_TEXTCUSTOMITEM + friend class QTextTableCell; +#endif + +public: + QTextFlow(); + virtual ~QTextFlow(); + + virtual void setWidth( int width ); + int width() const; + + virtual void setPageSize( int ps ); + int pageSize() const { return pagesize; } + + virtual int adjustLMargin( int yp, int h, int margin, int space ); + virtual int adjustRMargin( int yp, int h, int margin, int space ); + +#ifndef QT_NO_TEXTCUSTOMITEM + virtual void registerFloatingItem( QTextCustomItem* item ); + virtual void unregisterFloatingItem( QTextCustomItem* item ); +#endif + virtual QRect boundingRect() const; + virtual void drawFloatingItems(QPainter* p, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ); + + virtual int adjustFlow( int y, int w, int h ); // adjusts y according to the defined pagesize. Returns the shift. + + virtual bool isEmpty(); + + void clear(); + +private: + int w; + int pagesize; + +#ifndef QT_NO_TEXTCUSTOMITEM + QPtrList<QTextCustomItem> leftItems; + QPtrList<QTextCustomItem> rightItems; +#endif +}; + +inline int QTextFlow::width() const { return w; } + +#ifndef QT_NO_TEXTCUSTOMITEM +class QTextTable; + +class Q_EXPORT QTextTableCell : public QLayoutItem +{ + friend class QTextTable; + +public: + QTextTableCell( QTextTable* table, + int row, int column, + const QMap<QString, QString> &attr, + const QStyleSheetItem* style, + const QTextFormat& fmt, const QString& context, + QMimeSourceFactory &factory, QStyleSheet *sheet, const QString& doc ); + virtual ~QTextTableCell(); + + QSize sizeHint() const ; + QSize minimumSize() const ; + QSize maximumSize() const ; + QSizePolicy::ExpandData expanding() const; + bool isEmpty() const; + void setGeometry( const QRect& ) ; + QRect geometry() const; + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + + void adjustToPainter( QPainter* ); + + int row() const { return row_; } + int column() const { return col_; } + int rowspan() const { return rowspan_; } + int colspan() const { return colspan_; } + int stretch() const { return stretch_; } + + QTextDocument* richText() const { return richtext; } + QTextTable* table() const { return parent; } + + void draw( QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected ); + + QBrush *backGround() const { return background; } + virtual void invalidate(); + + int verticalAlignmentOffset() const; + int horizontalAlignmentOffset() const; + +private: + QRect geom; + QTextTable* parent; + QTextDocument* richtext; + int row_; + int col_; + int rowspan_; + int colspan_; + int stretch_; + int maxw; + int minw; + bool hasFixedWidth; + QBrush *background; + int cached_width; + int cached_sizehint; + QMap<QString, QString> attributes; + int align; +}; +#endif + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextTableCell>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<QTextCursor*, int>; +// MOC_SKIP_END +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT QTextTable: public QTextCustomItem +{ + friend class QTextTableCell; + +public: + QTextTable( QTextDocument *p, const QMap<QString, QString> &attr ); + virtual ~QTextTable(); + + void adjustToPainter( QPainter *p ); + void pageBreak( int y, QTextFlow* flow ); + void draw( QPainter* p, int x, int y, int cx, int cy, int cw, int ch, + const QColorGroup& cg, bool selected ); + + bool noErase() const { return TRUE; } + bool ownLine() const { return TRUE; } + Placement placement() const { return place; } + bool isNested() const { return TRUE; } + void resize( int nwidth ); + virtual void invalidate(); + + virtual bool enter( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); + virtual bool enterAt( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, const QPoint &pos ); + virtual bool next( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool prev( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool down( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool up( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + + QString richText() const; + + int minimumWidth() const; + + QPtrList<QTextTableCell> tableCells() const { return cells; } + + bool isStretching() const { return stretch; } + void setParagraph(QTextParagraph *p); + +private: + void format( int w ); + void addCell( QTextTableCell* cell ); + +private: + QGridLayout* layout; + QPtrList<QTextTableCell> cells; + int cachewidth; + int fixwidth; + int cellpadding; + int cellspacing; + int border; + int outerborder; + int stretch; + int innerborder; + int us_cp, us_ib, us_b, us_ob, us_cs; + int us_fixwidth; + QMap<QString, QString> attributes; + QMap<QTextCursor*, int> currCell; + Placement place; + void adjustCells( int y , int shift ); + int pageBreakFor; +}; +#endif +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#ifndef QT_NO_TEXTCUSTOMITEM +class QTextTableCell; +class QTextParagraph; +#endif + +struct Q_EXPORT QTextDocumentSelection +{ + QTextCursor startCursor, endCursor; + bool swapped; + Q_DUMMY_COMPARISON_OPERATOR(QTextDocumentSelection) +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QColor>; +//Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, bool>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QTextDocumentSelection>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextDocument>; +// MOC_SKIP_END +#endif + +class Q_EXPORT QTextDocument : public QObject +{ + Q_OBJECT + +#ifndef QT_NO_TEXTCUSTOMITEM + friend class QTextTableCell; +#endif + friend class QTextCursor; + friend class QTextEdit; + friend class QTextParagraph; + friend class QTextTable; + +public: + enum SelectionIds { + Standard = 0, + IMSelectionText = 31998, + IMCompositionText = 31999, // this must be higher! + Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to + // remove multiple lines with removeSelectedText() + }; + + QTextDocument( QTextDocument *p ); + virtual ~QTextDocument(); + + QTextDocument *parent() const { return par; } + QTextParagraph *parentParagraph() const { return parentPar; } + + void setText( const QString &text, const QString &context ); + QMap<QString, QString> attributes() const { return attribs; } + void setAttributes( const QMap<QString, QString> &attr ) { attribs = attr; } + + QString text() const; + QString text( int parag ) const; + QString originalText() const; + + int x() const; + int y() const; + int width() const; + int widthUsed() const; + int visibleWidth() const; + int height() const; + void setWidth( int w ); + int minimumWidth() const; + bool setMinimumWidth( int needed, int used = -1, QTextParagraph *parag = 0 ); + + void setY( int y ); + int leftMargin() const; + void setLeftMargin( int lm ); + int rightMargin() const; + void setRightMargin( int rm ); + + QTextParagraph *firstParagraph() const; + QTextParagraph *lastParagraph() const; + void setFirstParagraph( QTextParagraph *p ); + void setLastParagraph( QTextParagraph *p ); + + void invalidate(); + + void setPreProcessor( QTextPreProcessor *sh ); + QTextPreProcessor *preProcessor() const; + + void setFormatter( QTextFormatter *f ); + QTextFormatter *formatter() const; + + void setIndent( QTextIndent *i ); + QTextIndent *indent() const; + + QColor selectionColor( int id ) const; + bool invertSelectionText( int id ) const; + void setSelectionColor( int id, const QColor &c ); + void setInvertSelectionText( int id, bool b ); + bool hasSelection( int id, bool visible = FALSE ) const; + void setSelectionStart( int id, const QTextCursor &cursor ); + bool setSelectionEnd( int id, const QTextCursor &cursor ); + void selectAll( int id ); + bool removeSelection( int id ); + void selectionStart( int id, int ¶gId, int &index ); + QTextCursor selectionStartCursor( int id ); + QTextCursor selectionEndCursor( int id ); + void selectionEnd( int id, int ¶gId, int &index ); + void setFormat( int id, QTextFormat *f, int flags ); + int numSelections() const { return nSelections; } + void addSelection( int id ); + + QString selectedText( int id, bool asRichText = FALSE ) const; + void removeSelectedText( int id, QTextCursor *cursor ); + void indentSelection( int id ); + + QTextParagraph *paragAt( int i ) const; + + void addCommand( QTextCommand *cmd ); + QTextCursor *undo( QTextCursor *c = 0 ); + QTextCursor *redo( QTextCursor *c = 0 ); + QTextCommandHistory *commands() const { return commandHistory; } + + QTextFormatCollection *formatCollection() const; + + bool find( QTextCursor &cursor, const QString &expr, bool cs, bool wo, bool forward); + + void setTextFormat( Qt::TextFormat f ); + Qt::TextFormat textFormat() const; + + bool inSelection( int selId, const QPoint &pos ) const; + + QStyleSheet *styleSheet() const { return sheet_; } +#ifndef QT_NO_MIME + QMimeSourceFactory *mimeSourceFactory() const { return factory_; } +#endif + QString context() const { return contxt; } + + void setStyleSheet( QStyleSheet *s ); + void setDefaultFormat( const QFont &font, const QColor &color ); +#ifndef QT_NO_MIME + void setMimeSourceFactory( QMimeSourceFactory *f ) { if ( f ) factory_ = f; } +#endif + void setContext( const QString &c ) { if ( !c.isEmpty() ) contxt = c; } + + void setUnderlineLinks( bool b ); + bool underlineLinks() const { return underlLinks; } + + void setPaper( QBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; } + QBrush *paper() const { return backBrush; } + + void doLayout( QPainter *p, int w ); + void draw( QPainter *p, const QRect& rect, const QColorGroup &cg, const QBrush *paper = 0 ); + bool useDoubleBuffer( QTextParagraph *parag, QPainter *p ); + + void drawParagraph( QPainter *p, QTextParagraph *parag, int cx, int cy, int cw, int ch, + QPixmap *&doubleBuffer, const QColorGroup &cg, + bool drawCursor, QTextCursor *cursor, bool resetChanged = TRUE ); + QTextParagraph *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg, + bool onlyChanged = FALSE, bool drawCursor = FALSE, QTextCursor *cursor = 0, + bool resetChanged = TRUE ); + +#ifndef QT_NO_TEXTCUSTOMITEM + void registerCustomItem( QTextCustomItem *i, QTextParagraph *p ); + void unregisterCustomItem( QTextCustomItem *i, QTextParagraph *p ); +#endif + + void setFlow( QTextFlow *f ); + void takeFlow(); + QTextFlow *flow() const { return flow_; } + bool isPageBreakEnabled() const { return pages; } + void setPageBreakEnabled( bool b ) { pages = b; } + + void setUseFormatCollection( bool b ) { useFC = b; } + bool useFormatCollection() const { return useFC; } + +#ifndef QT_NO_TEXTCUSTOMITEM + QTextTableCell *tableCell() const { return tc; } + void setTableCell( QTextTableCell *c ) { tc = c; } +#endif + + void setPlainText( const QString &text ); + void setRichText( const QString &text, const QString &context, const QTextFormat *initialFormat = 0 ); + QString richText() const; + QString plainText() const; + + bool focusNextPrevChild( bool next ); + + int alignment() const; + void setAlignment( int a ); + + int *tabArray() const; + int tabStopWidth() const; + void setTabArray( int *a ); + void setTabStops( int tw ); + + void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); } + int undoDepth() const { return commandHistory->undoDepth(); } + + int length() const; + void clear( bool createEmptyParag = FALSE ); + + virtual QTextParagraph *createParagraph( QTextDocument *d, QTextParagraph *pr = 0, QTextParagraph *nx = 0, bool updateIds = TRUE ); + void insertChild( QObject *o ) { QObject::insertChild( o ); } + void removeChild( QObject *o ) { QObject::removeChild( o ); } + void insertChild( QTextDocument *d ) { childList.append( d ); } + void removeChild( QTextDocument *d ) { childList.removeRef( d ); } + QPtrList<QTextDocument> children() const { return childList; } + + bool hasFocusParagraph() const; + QString focusHref() const; + QString focusName() const; + + void invalidateOriginalText() { oTextValid = FALSE; oText = ""; } + +signals: + void minimumWidthChanged( int ); + +private: + void init(); + QPixmap *bufferPixmap( const QSize &s ); + // HTML parser + bool hasPrefix(const QChar* doc, int length, int pos, QChar c); + bool hasPrefix(const QChar* doc, int length, int pos, const QString& s); +#ifndef QT_NO_TEXTCUSTOMITEM + QTextCustomItem* parseTable( const QMap<QString, QString> &attr, const QTextFormat &fmt, + const QChar* doc, int length, int& pos, QTextParagraph *curpar ); +#endif + bool eatSpace(const QChar* doc, int length, int& pos, bool includeNbsp = FALSE ); + bool eat(const QChar* doc, int length, int& pos, QChar c); + QString parseOpenTag(const QChar* doc, int length, int& pos, QMap<QString, QString> &attr, bool& emptyTag); + QString parseCloseTag( const QChar* doc, int length, int& pos ); + QChar parseHTMLSpecialChar(const QChar* doc, int length, int& pos); + QString parseWord(const QChar* doc, int length, int& pos, bool lower = TRUE); + QChar parseChar(const QChar* doc, int length, int& pos, QStyleSheetItem::WhiteSpaceMode wsm ); + void setRichTextInternal( const QString &text, QTextCursor* cursor = 0, const QTextFormat *initialFormat = 0 ); + void setRichTextMarginsInternal( QPtrList< QPtrVector<QStyleSheetItem> >& styles, QTextParagraph* stylesPar ); + +private: + struct Q_EXPORT Focus { + QTextParagraph *parag; + int start, len; + QString href; + QString name; + }; + + int cx, cy, cw, vw; + QTextParagraph *fParag, *lParag; + QTextPreProcessor *pProcessor; + QMap<int, QColor> selectionColors; + QMap<int, QTextDocumentSelection> selections; + QMap<int, bool> selectionText; + QTextCommandHistory *commandHistory; + QTextFormatter *pFormatter; + QTextIndent *indenter; + QTextFormatCollection *fCollection; + Qt::TextFormat txtFormat; + uint preferRichText : 1; + uint pages : 1; + uint useFC : 1; + uint withoutDoubleBuffer : 1; + uint underlLinks : 1; + uint nextDoubleBuffered : 1; + uint oTextValid : 1; + uint mightHaveCustomItems : 1; + int align; + int nSelections; + QTextFlow *flow_; + QTextDocument *par; + QTextParagraph *parentPar; +#ifndef QT_NO_TEXTCUSTOMITEM + QTextTableCell *tc; +#endif + QBrush *backBrush; + QPixmap *buf_pixmap; + Focus focusIndicator; + int minw; + int wused; + int leftmargin; + int rightmargin; + QTextParagraph *minwParag, *curParag; + QStyleSheet* sheet_; +#ifndef QT_NO_MIME + QMimeSourceFactory* factory_; +#endif + QString contxt; + QMap<QString, QString> attribs; + int *tArray; + int tStopWidth; + int uDepth; + QString oText; + QPtrList<QTextDocument> childList; + QColor linkColor, bodyText; + double scaleFontsFactor; + + short list_tm,list_bm, list_lm, li_tm, li_bm, par_tm, par_bm; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + QTextDocument( const QTextDocument & ); + QTextDocument &operator=( const QTextDocument & ); +#endif +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +class Q_EXPORT QTextDeleteCommand : public QTextCommand +{ +public: + QTextDeleteCommand( QTextDocument *d, int i, int idx, const QMemArray<QTextStringChar> &str, + const QByteArray& oldStyle ); + QTextDeleteCommand( QTextParagraph *p, int idx, const QMemArray<QTextStringChar> &str ); + virtual ~QTextDeleteCommand(); + + Commands type() const { return Delete; } + QTextCursor *execute( QTextCursor *c ); + QTextCursor *unexecute( QTextCursor *c ); + +protected: + int id, index; + QTextParagraph *parag; + QMemArray<QTextStringChar> text; + QByteArray styleInformation; + +}; + +class Q_EXPORT QTextInsertCommand : public QTextDeleteCommand +{ +public: + QTextInsertCommand( QTextDocument *d, int i, int idx, const QMemArray<QTextStringChar> &str, + const QByteArray& oldStyleInfo ) + : QTextDeleteCommand( d, i, idx, str, oldStyleInfo ) {} + QTextInsertCommand( QTextParagraph *p, int idx, const QMemArray<QTextStringChar> &str ) + : QTextDeleteCommand( p, idx, str ) {} + virtual ~QTextInsertCommand() {} + + Commands type() const { return Insert; } + QTextCursor *execute( QTextCursor *c ) { return QTextDeleteCommand::unexecute( c ); } + QTextCursor *unexecute( QTextCursor *c ) { return QTextDeleteCommand::execute( c ); } + +}; + +class Q_EXPORT QTextFormatCommand : public QTextCommand +{ +public: + QTextFormatCommand( QTextDocument *d, int sid, int sidx, int eid, int eidx, const QMemArray<QTextStringChar> &old, QTextFormat *f, int fl ); + virtual ~QTextFormatCommand(); + + Commands type() const { return Format; } + QTextCursor *execute( QTextCursor *c ); + QTextCursor *unexecute( QTextCursor *c ); + +protected: + int startId, startIndex, endId, endIndex; + QTextFormat *format; + QMemArray<QTextStringChar> oldFormats; + int flags; + +}; + +class Q_EXPORT QTextStyleCommand : public QTextCommand +{ +public: + QTextStyleCommand( QTextDocument *d, int fParag, int lParag, const QByteArray& beforeChange ); + virtual ~QTextStyleCommand() {} + + Commands type() const { return Style; } + QTextCursor *execute( QTextCursor *c ); + QTextCursor *unexecute( QTextCursor *c ); + + static QByteArray readStyleInformation( QTextDocument* d, int fParag, int lParag ); + static void writeStyleInformation( QTextDocument* d, int fParag, const QByteArray& style ); + +private: + int firstParag, lastParag; + QByteArray before; + QByteArray after; +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +struct Q_EXPORT QTextParagraphSelection +{ + QTextParagraphSelection() : start(0), end(0) { } + int start, end; + Q_DUMMY_COMPARISON_OPERATOR(QTextParagraphSelection) +}; + +struct Q_EXPORT QTextLineStart +{ + QTextLineStart() : y( 0 ), baseLine( 0 ), h( 0 ) + { } + QTextLineStart( int y_, int bl, int h_ ) : y( y_ ), baseLine( bl ), h( h_ ), + w( 0 ) + { } + +public: + int y, baseLine, h; + int w; +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QTextParagraphSelection>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QTextLineStart*>; +// MOC_SKIP_END +#endif + +class Q_EXPORT QTextParagraphData +{ +public: + QTextParagraphData() {} + virtual ~QTextParagraphData(); + virtual void join( QTextParagraphData * ); +}; + +class QTextParagraphPseudoDocument; + +class QSyntaxHighlighter; + +class Q_EXPORT QTextParagraph +{ + friend class QTextDocument; + friend class QTextCursor; + friend class QSyntaxHighlighter; + +public: + QTextParagraph( QTextDocument *d, QTextParagraph *pr = 0, QTextParagraph *nx = 0, bool updateIds = TRUE ); + ~QTextParagraph(); + + QTextString *string() const; + QTextStringChar *at( int i ) const; // maybe remove later + int leftGap() const; + int length() const; // maybe remove later + + void setListStyle( QStyleSheetItem::ListStyle ls ) { lstyle = ls; changed = TRUE; } + QStyleSheetItem::ListStyle listStyle() const { return (QStyleSheetItem::ListStyle)lstyle; } + void setListItem( bool li ); + bool isListItem() const { return litem; } + void setListValue( int v ) { list_val = v; } + int listValue() const { return list_val > 0 ? list_val : -1; } + + void setListDepth( int depth ); + int listDepth() const { return ldepth; } + +// void setFormat( QTextFormat *fm ); +// QTextFormat *paragFormat() const; + + inline QTextDocument *document() const { + if (hasdoc) return (QTextDocument*) docOrPseudo; + return 0; + } + QTextParagraphPseudoDocument *pseudoDocument() const; + + QRect rect() const; + void setHeight( int h ) { r.setHeight( h ); } + void show(); + void hide(); + bool isVisible() const { return visible; } + + QTextParagraph *prev() const; + QTextParagraph *next() const; + void setPrev( QTextParagraph *s ); + void setNext( QTextParagraph *s ); + + void insert( int index, const QString &s ); + void insert( int index, const QChar *unicode, int len ); + void append( const QString &s, bool reallyAtEnd = FALSE ); + void truncate( int index ); + void remove( int index, int len ); + void join( QTextParagraph *s ); + + void invalidate( int chr ); + + void move( int &dy ); + void format( int start = -1, bool doMove = TRUE ); + + bool isValid() const; + bool hasChanged() const; + void setChanged( bool b, bool recursive = FALSE ); + + int lineHeightOfChar( int i, int *bl = 0, int *y = 0 ) const; + QTextStringChar *lineStartOfChar( int i, int *index = 0, int *line = 0 ) const; + int lines() const; + QTextStringChar *lineStartOfLine( int line, int *index = 0 ) const; + int lineY( int l ) const; + int lineBaseLine( int l ) const; + int lineHeight( int l ) const; + void lineInfo( int l, int &y, int &h, int &bl ) const; + + void setSelection( int id, int start, int end ); + void removeSelection( int id ); + int selectionStart( int id ) const; + int selectionEnd( int id ) const; + bool hasSelection( int id ) const; + bool hasAnySelection() const; + bool fullSelected( int id ) const; + + void setEndState( int s ); + int endState() const; + + void setParagId( int i ); + int paragId() const; + + bool firstPreProcess() const; + void setFirstPreProcess( bool b ); + + void indent( int *oldIndent = 0, int *newIndent = 0 ); + + void setExtraData( QTextParagraphData *data ); + QTextParagraphData *extraData() const; + + QMap<int, QTextLineStart*> &lineStartList(); + + void setFormat( int index, int len, QTextFormat *f, bool useCollection = TRUE, int flags = -1 ); + + void setAlignment( int a ); + int alignment() const; + + void paint( QPainter &painter, const QColorGroup &cg, QTextCursor *cursor = 0, bool drawSelections = FALSE, + int clipx = -1, int clipy = -1, int clipw = -1, int cliph = -1 ); + + int topMargin() const; + int bottomMargin() const; + int leftMargin() const; + int firstLineMargin() const; + int rightMargin() const; + int lineSpacing() const; + +#ifndef QT_NO_TEXTCUSTOMITEM + void registerFloatingItem( QTextCustomItem *i ); + void unregisterFloatingItem( QTextCustomItem *i ); +#endif + + void setFullWidth( bool b ) { fullWidth = b; } + bool isFullWidth() const { return fullWidth; } + +#ifndef QT_NO_TEXTCUSTOMITEM + QTextTableCell *tableCell() const; +#endif + + QBrush *background() const; + + int documentWidth() const; + int documentVisibleWidth() const; + int documentX() const; + int documentY() const; + QTextFormatCollection *formatCollection() const; + QTextFormatter *formatter() const; + + int nextTab( int i, int x ); + int *tabArray() const; + void setTabArray( int *a ); + void setTabStops( int tw ); + + void adjustToPainter( QPainter *p ); + + void setNewLinesAllowed( bool b ); + bool isNewLinesAllowed() const; + + QString richText() const; + + void addCommand( QTextCommand *cmd ); + QTextCursor *undo( QTextCursor *c = 0 ); + QTextCursor *redo( QTextCursor *c = 0 ); + QTextCommandHistory *commands() const; + void copyParagData( QTextParagraph *parag ); + + void setBreakable( bool b ) { breakable = b; } + bool isBreakable() const { return breakable; } + + void setBackgroundColor( const QColor &c ); + QColor *backgroundColor() const { return bgcol; } + void clearBackgroundColor(); + + void setMovedDown( bool b ) { movedDown = b; } + bool wasMovedDown() const { return movedDown; } + + void setDirection( QChar::Direction d ); + QChar::Direction direction() const; + void setPaintDevice( QPaintDevice *pd ) { paintdevice = pd; } + + void readStyleInformation( QDataStream& stream ); + void writeStyleInformation( QDataStream& stream ) const; + +protected: + void setColorForSelection( QColor &c, QPainter &p, const QColorGroup& cg, int selection ); + void drawLabel( QPainter* p, int x, int y, int w, int h, int base, const QColorGroup& cg ); + void drawString( QPainter &painter, const QString &str, int start, int len, int xstart, + int y, int baseLine, int w, int h, bool drawSelections, int fullSelectionWidth, + QTextStringChar *formatChar, const QColorGroup& cg, + bool rightToLeft ); + +private: + QMap<int, QTextParagraphSelection> &selections() const; +#ifndef QT_NO_TEXTCUSTOMITEM + QPtrList<QTextCustomItem> &floatingItems() const; +#endif + QBrush backgroundBrush( const QColorGroup&cg ) { if ( bgcol ) return *bgcol; return cg.brush( QColorGroup::Base ); } + void invalidateStyleCache(); + + QMap<int, QTextLineStart*> lineStarts; + QRect r; + QTextParagraph *p, *n; + void *docOrPseudo; + uint changed : 1; + uint firstFormat : 1; + uint firstPProcess : 1; + uint needPreProcess : 1; + uint fullWidth : 1; + uint lastInFrame : 1; + uint visible : 1; + uint breakable : 1; + uint movedDown : 1; + uint mightHaveCustomItems : 1; + uint hasdoc : 1; + uint litem : 1; // whether the paragraph is a list item + uint rtext : 1; // whether the paragraph needs rich text margin + int align : 4; + uint /*QStyleSheetItem::ListStyle*/ lstyle : 4; + int invalid; + int state, id; + QTextString *str; + QMap<int, QTextParagraphSelection> *mSelections; +#ifndef QT_NO_TEXTCUSTOMITEM + QPtrList<QTextCustomItem> *mFloatingItems; +#endif + short utm, ubm, ulm, urm, uflm, ulinespacing; + short tabStopWidth; + int minwidth; + int *tArray; + QTextParagraphData *eData; + short list_val; + ushort ldepth; + QColor *bgcol; + QPaintDevice *paintdevice; +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextFormatter +{ +public: + QTextFormatter(); + virtual ~QTextFormatter(); + + virtual int format( QTextDocument *doc, QTextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts ) = 0; + virtual int formatVertically( QTextDocument* doc, QTextParagraph* parag ); + + bool isWrapEnabled( QTextParagraph *p ) const { if ( !wrapEnabled ) return FALSE; if ( p && !p->isBreakable() ) return FALSE; return TRUE;} + int wrapAtColumn() const { return wrapColumn;} + virtual void setWrapEnabled( bool b ); + virtual void setWrapAtColumn( int c ); + virtual void setAllowBreakInWords( bool b ) { biw = b; } + bool allowBreakInWords() const { return biw; } + + int minimumWidth() const { return thisminw; } + int widthUsed() const { return thiswused; } + +protected: + virtual QTextLineStart *formatLine( QTextParagraph *parag, QTextString *string, QTextLineStart *line, QTextStringChar *start, + QTextStringChar *last, int align = Qt::AlignAuto, int space = 0 ); +#ifndef QT_NO_COMPLEXTEXT + virtual QTextLineStart *bidiReorderLine( QTextParagraph *parag, QTextString *string, QTextLineStart *line, QTextStringChar *start, + QTextStringChar *last, int align, int space ); +#endif + void insertLineStart( QTextParagraph *parag, int index, QTextLineStart *ls ); + + int thisminw; + int thiswused; + +private: + bool wrapEnabled; + int wrapColumn; + bool biw; + +#ifdef HAVE_THAI_BREAKS + static QCString *thaiCache; + static QTextString *cachedString; + static ThBreakIterator *thaiIt; +#endif +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextFormatterBreakInWords : public QTextFormatter +{ +public: + QTextFormatterBreakInWords(); + virtual ~QTextFormatterBreakInWords() {} + + int format( QTextDocument *doc, QTextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts ); + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextFormatterBreakWords : public QTextFormatter +{ +public: + QTextFormatterBreakWords(); + virtual ~QTextFormatterBreakWords() {} + + int format( QTextDocument *doc, QTextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts ); + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextIndent +{ +public: + QTextIndent(); + virtual ~QTextIndent() {} + + virtual void indent( QTextDocument *doc, QTextParagraph *parag, int *oldIndent = 0, int *newIndent = 0 ) = 0; + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextPreProcessor +{ +public: + enum Ids { + Standard = 0 + }; + + QTextPreProcessor(); + virtual ~QTextPreProcessor() {} + + virtual void process( QTextDocument *doc, QTextParagraph *, int, bool = TRUE ) = 0; + virtual QTextFormat *format( int id ) = 0; + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT QTextFormat +{ + friend class QTextFormatCollection; + friend class QTextDocument; + +public: + enum Flags { + NoFlags, + Bold = 1, + Italic = 2, + Underline = 4, + Family = 8, + Size = 16, + Color = 32, + Misspelled = 64, + VAlign = 128, + StrikeOut= 256, + Font = Bold | Italic | Underline | Family | Size | StrikeOut, + Format = Font | Color | Misspelled | VAlign + }; + + enum VerticalAlignment { AlignNormal, AlignSuperScript, AlignSubScript }; + + QTextFormat(); + virtual ~QTextFormat(); + + QTextFormat( const QStyleSheetItem *s ); + QTextFormat( const QFont &f, const QColor &c, QTextFormatCollection *parent = 0 ); + QTextFormat( const QTextFormat &fm ); + QTextFormat makeTextFormat( const QStyleSheetItem *style, const QMap<QString,QString>& attr, double scaleFontsFactor ) const; + QTextFormat& operator=( const QTextFormat &fm ); + QColor color() const; + QFont font() const; + QFontMetrics fontMetrics() const { return fm; } + bool isMisspelled() const; + VerticalAlignment vAlign() const; + int minLeftBearing() const; + int minRightBearing() const; + int width( const QChar &c ) const; + int width( const QString &str, int pos ) const; + int height() const; + int ascent() const; + int descent() const; + int leading() const; + bool useLinkColor() const; + + void setBold( bool b ); + void setItalic( bool b ); + void setUnderline( bool b ); + void setStrikeOut( bool b ); + void setFamily( const QString &f ); + void setPointSize( int s ); + void setFont( const QFont &f ); + void setColor( const QColor &c ); + void setMisspelled( bool b ); + void setVAlign( VerticalAlignment a ); + + bool operator==( const QTextFormat &f ) const; + QTextFormatCollection *parent() const; + const QString &key() const; + + static QString getKey( const QFont &f, const QColor &c, bool misspelled, VerticalAlignment vAlign ); + + void addRef(); + void removeRef(); + + QString makeFormatChangeTags( QTextFormat* defaultFormat, QTextFormat *f, const QString& oldAnchorHref, const QString& anchorHref ) const; + QString makeFormatEndTags( QTextFormat* defaultFormat, const QString& anchorHref ) const; + + static void setPainter( QPainter *p ); + static QPainter* painter(); + + bool fontSizesInPixels() { return usePixelSizes; } + +protected: + virtual void generateKey(); + +private: + void update(); + static void applyFont( const QFont &f ); + +private: + QFont fn; + QColor col; + QFontMetrics fm; + uint missp : 1; + uint linkColor : 1; + uint usePixelSizes : 1; + int leftBearing, rightBearing; + VerticalAlignment ha; + uchar widths[ 256 ]; + int hei, asc, dsc; + QTextFormatCollection *collection; + int ref; + QString k; + int logicalFontSize; + int stdSize; + static QPainter *pntr; + static QFontMetrics *pntr_fm; + static int pntr_asc; + static int pntr_hei; + static int pntr_ldg; + static int pntr_dsc; + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QDict<QTextFormat>; +// MOC_SKIP_END +#endif + +class Q_EXPORT QTextFormatCollection +{ + friend class QTextDocument; + friend class QTextFormat; + +public: + QTextFormatCollection(); + virtual ~QTextFormatCollection(); + + void setDefaultFormat( QTextFormat *f ); + QTextFormat *defaultFormat() const; + virtual QTextFormat *format( QTextFormat *f ); + virtual QTextFormat *format( QTextFormat *of, QTextFormat *nf, int flags ); + virtual QTextFormat *format( const QFont &f, const QColor &c ); + virtual void remove( QTextFormat *f ); + virtual QTextFormat *createFormat( const QTextFormat &f ) { return new QTextFormat( f ); } + virtual QTextFormat *createFormat( const QFont &f, const QColor &c ) { return new QTextFormat( f, c, this ); } + + void updateDefaultFormat( const QFont &font, const QColor &c, QStyleSheet *sheet ); + + QPaintDevice *paintDevice() const { return paintdevice; } + void setPaintDevice( QPaintDevice * ); + +private: + void updateKeys(); + +private: + QTextFormat *defFormat, *lastFormat, *cachedFormat; + QDict<QTextFormat> cKey; + QTextFormat *cres; + QFont cfont; + QColor ccol; + QString kof, knf; + int cflags; + + QPaintDevice *paintdevice; +}; + +class Q_EXPORT QTextParagraphPseudoDocument +{ +public: + QTextParagraphPseudoDocument(); + ~QTextParagraphPseudoDocument(); + QRect docRect; + QTextFormatter *pFormatter; + QTextCommandHistory *commandHistory; + int minw; + int wused; + QTextFormatCollection collection; +}; + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline int QTextParagraph::length() const +{ + return str->length(); +} + +inline QRect QTextParagraph::rect() const +{ + return r; +} + +inline int QTextCursor::index() const +{ + return idx; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline int QTextDocument::x() const +{ + return cx; +} + +inline int QTextDocument::y() const +{ + return cy; +} + +inline int QTextDocument::width() const +{ + return QMAX( cw, flow_->width() ); +} + +inline int QTextDocument::visibleWidth() const +{ + return vw; +} + +inline QTextParagraph *QTextDocument::firstParagraph() const +{ + return fParag; +} + +inline QTextParagraph *QTextDocument::lastParagraph() const +{ + return lParag; +} + +inline void QTextDocument::setFirstParagraph( QTextParagraph *p ) +{ + fParag = p; +} + +inline void QTextDocument::setLastParagraph( QTextParagraph *p ) +{ + lParag = p; +} + +inline void QTextDocument::setWidth( int w ) +{ + cw = QMAX( w, minw ); + flow_->setWidth( cw ); + vw = w; +} + +inline int QTextDocument::minimumWidth() const +{ + return minw; +} + +inline void QTextDocument::setY( int y ) +{ + cy = y; +} + +inline int QTextDocument::leftMargin() const +{ + return leftmargin; +} + +inline void QTextDocument::setLeftMargin( int lm ) +{ + leftmargin = lm; +} + +inline int QTextDocument::rightMargin() const +{ + return rightmargin; +} + +inline void QTextDocument::setRightMargin( int rm ) +{ + rightmargin = rm; +} + +inline QTextPreProcessor *QTextDocument::preProcessor() const +{ + return pProcessor; +} + +inline void QTextDocument::setPreProcessor( QTextPreProcessor * sh ) +{ + pProcessor = sh; +} + +inline void QTextDocument::setFormatter( QTextFormatter *f ) +{ + delete pFormatter; + pFormatter = f; +} + +inline QTextFormatter *QTextDocument::formatter() const +{ + return pFormatter; +} + +inline void QTextDocument::setIndent( QTextIndent *i ) +{ + indenter = i; +} + +inline QTextIndent *QTextDocument::indent() const +{ + return indenter; +} + +inline QColor QTextDocument::selectionColor( int id ) const +{ + return selectionColors[ id ]; +} + +inline bool QTextDocument::invertSelectionText( int id ) const +{ + return selectionText[ id ]; +} + +inline void QTextDocument::setSelectionColor( int id, const QColor &c ) +{ + selectionColors[ id ] = c; +} + +inline void QTextDocument::setInvertSelectionText( int id, bool b ) +{ + selectionText[ id ] = b; +} + +inline QTextFormatCollection *QTextDocument::formatCollection() const +{ + return fCollection; +} + +inline int QTextDocument::alignment() const +{ + return align; +} + +inline void QTextDocument::setAlignment( int a ) +{ + align = a; +} + +inline int *QTextDocument::tabArray() const +{ + return tArray; +} + +inline int QTextDocument::tabStopWidth() const +{ + return tStopWidth; +} + +inline void QTextDocument::setTabArray( int *a ) +{ + tArray = a; +} + +inline void QTextDocument::setTabStops( int tw ) +{ + tStopWidth = tw; +} + +inline QString QTextDocument::originalText() const +{ + if ( oTextValid ) + return oText; + return text(); +} + +inline void QTextDocument::setFlow( QTextFlow *f ) +{ + if ( flow_ ) + delete flow_; + flow_ = f; +} + +inline void QTextDocument::takeFlow() +{ + flow_ = 0; +} + +inline bool QTextDocument::useDoubleBuffer( QTextParagraph *parag, QPainter *p ) +{ + return ( !parag->document()->parent() || parag->document()->nextDoubleBuffered ) && + ( !p || !p->device() || p->device()->devType() != QInternal::Printer ); +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline QColor QTextFormat::color() const +{ + return col; +} + +inline QFont QTextFormat::font() const +{ + return fn; +} + +inline bool QTextFormat::isMisspelled() const +{ + return missp; +} + +inline QTextFormat::VerticalAlignment QTextFormat::vAlign() const +{ + return ha; +} + +inline bool QTextFormat::operator==( const QTextFormat &f ) const +{ + return k == f.k; +} + +inline QTextFormatCollection *QTextFormat::parent() const +{ + return collection; +} + +inline void QTextFormat::addRef() +{ + ref++; +} + +inline void QTextFormat::removeRef() +{ + ref--; + if ( !collection ) + return; + if ( this == collection->defFormat ) + return; + if ( ref == 0 ) + collection->remove( this ); +} + +inline const QString &QTextFormat::key() const +{ + return k; +} + +inline bool QTextFormat::useLinkColor() const +{ + return linkColor; +} + +inline QTextStringChar *QTextParagraph::at( int i ) const +{ + return &str->at( i ); +} + +inline bool QTextParagraph::isValid() const +{ + return invalid == -1; +} + +inline bool QTextParagraph::hasChanged() const +{ + return changed; +} + +inline void QTextParagraph::setBackgroundColor( const QColor & c ) +{ + delete bgcol; + bgcol = new QColor( c ); + setChanged( TRUE ); +} + +inline void QTextParagraph::clearBackgroundColor() +{ + delete bgcol; bgcol = 0; setChanged( TRUE ); +} + +inline void QTextParagraph::append( const QString &s, bool reallyAtEnd ) +{ + if ( reallyAtEnd ) + insert( str->length(), s ); + else + insert( QMAX( str->length() - 1, 0 ), s ); +} + +inline QTextParagraph *QTextParagraph::prev() const +{ + return p; +} + +inline QTextParagraph *QTextParagraph::next() const +{ + return n; +} + +inline bool QTextParagraph::hasAnySelection() const +{ + return mSelections ? !selections().isEmpty() : FALSE; +} + +inline void QTextParagraph::setEndState( int s ) +{ + if ( s == state ) + return; + state = s; +} + +inline int QTextParagraph::endState() const +{ + return state; +} + +inline void QTextParagraph::setParagId( int i ) +{ + id = i; +} + +inline int QTextParagraph::paragId() const +{ + if ( id == -1 ) + qWarning( "invalid parag id!!!!!!!! (%p)", (void*)this ); + return id; +} + +inline bool QTextParagraph::firstPreProcess() const +{ + return firstPProcess; +} + +inline void QTextParagraph::setFirstPreProcess( bool b ) +{ + firstPProcess = b; +} + +inline QMap<int, QTextLineStart*> &QTextParagraph::lineStartList() +{ + return lineStarts; +} + +inline QTextString *QTextParagraph::string() const +{ + return str; +} + +inline QTextParagraphPseudoDocument *QTextParagraph::pseudoDocument() const +{ + if ( hasdoc ) + return 0; + return (QTextParagraphPseudoDocument*) docOrPseudo; +} + + +#ifndef QT_NO_TEXTCUSTOMITEM +inline QTextTableCell *QTextParagraph::tableCell() const +{ + return hasdoc ? document()->tableCell () : 0; +} +#endif + +inline QTextCommandHistory *QTextParagraph::commands() const +{ + return hasdoc ? document()->commands() : pseudoDocument()->commandHistory; +} + + +inline int QTextParagraph::alignment() const +{ + return align; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +inline void QTextParagraph::registerFloatingItem( QTextCustomItem *i ) +{ + floatingItems().append( i ); +} + +inline void QTextParagraph::unregisterFloatingItem( QTextCustomItem *i ) +{ + floatingItems().removeRef( i ); +} +#endif + +inline QBrush *QTextParagraph::background() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return tableCell() ? tableCell()->backGround() : 0; +#else + return 0; +#endif +} + +inline int QTextParagraph::documentWidth() const +{ + return hasdoc ? document()->width() : pseudoDocument()->docRect.width(); +} + +inline int QTextParagraph::documentVisibleWidth() const +{ + return hasdoc ? document()->visibleWidth() : pseudoDocument()->docRect.width(); +} + +inline int QTextParagraph::documentX() const +{ + return hasdoc ? document()->x() : pseudoDocument()->docRect.x(); +} + +inline int QTextParagraph::documentY() const +{ + return hasdoc ? document()->y() : pseudoDocument()->docRect.y(); +} + +inline void QTextParagraph::setExtraData( QTextParagraphData *data ) +{ + eData = data; +} + +inline QTextParagraphData *QTextParagraph::extraData() const +{ + return eData; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline void QTextFormatCollection::setDefaultFormat( QTextFormat *f ) +{ + defFormat = f; +} + +inline QTextFormat *QTextFormatCollection::defaultFormat() const +{ + return defFormat; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline QTextFormat *QTextStringChar::format() const +{ + return (type == Regular) ? d.format : d.custom->format; +} + + +#ifndef QT_NO_TEXTCUSTOMITEM +inline QTextCustomItem *QTextStringChar::customItem() const +{ + return isCustom() ? d.custom->custom : 0; +} +#endif + +inline int QTextStringChar::height() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return !isCustom() ? format()->height() : ( customItem()->placement() == QTextCustomItem::PlaceInline ? customItem()->height : 0 ); +#else + return format()->height(); +#endif +} + +inline int QTextStringChar::ascent() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return !isCustom() ? format()->ascent() : ( customItem()->placement() == QTextCustomItem::PlaceInline ? customItem()->ascent() : 0 ); +#else + return format()->ascent(); +#endif +} + +inline int QTextStringChar::descent() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return !isCustom() ? format()->descent() : 0; +#else + return format()->descent(); +#endif +} + +#endif //QT_NO_RICHTEXT + +#endif diff --git a/src/kernel/qscriptengine.cpp b/src/kernel/qscriptengine.cpp new file mode 100644 index 0000000..66aa07b --- /dev/null +++ b/src/kernel/qscriptengine.cpp @@ -0,0 +1,1624 @@ +/**************************************************************************** +** +** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qscriptengine_p.h" + +#include "qstring.h" +#include "qrect.h" +#include "qfont.h" +#include <private/qunicodetables_p.h> +#include "qtextengine_p.h" +#include "qfontengine_p.h" +#include <stdlib.h> + +#undef None +#undef Pre +#undef Above +#undef Below + +const int Prealloc = 256; +template<class T> +class QVarLengthArray +{ +public: + inline explicit QVarLengthArray(int size = 0); + inline ~QVarLengthArray() { + if (ptr != reinterpret_cast<T *>(array)) + free(ptr); + } + + inline int size() const { return s; } + inline int count() const { return s; } + inline bool isEmpty() const { return (s == 0); } + inline void resize(int size); + inline void clear() { resize(0); } + + inline int capacity() const { return a; } + inline void reserve(int size); + + inline T &operator[](int idx) { + Q_ASSERT(idx >= 0 && idx < s); + return ptr[idx]; + } + inline const T &operator[](int idx) const { + Q_ASSERT(idx >= 0 && idx < s); + return ptr[idx]; + } + + inline void append(const T &t) { + const int idx = s; + resize(idx + 1); + ptr[idx] = t; + } + + inline T *data() { return ptr; } + inline const T *data() const { return ptr; } + inline const T * constData() const { return ptr; } + +private: + void realloc(int size, int alloc); + + int a; + int s; + T *ptr; + Q_UINT64 array[((Prealloc * sizeof(T)) / sizeof(Q_UINT64)) + 1]; +}; + +template <class T> +Q_INLINE_TEMPLATES QVarLengthArray<T>::QVarLengthArray(int asize) + : s(asize) { + if (s > Prealloc) { + ptr = reinterpret_cast<T *>(malloc(s * sizeof(T))); + a = s; + } else { + ptr = reinterpret_cast<T *>(array); + a = Prealloc; + } +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Basic processing +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +static inline void positionCluster(QShaperItem *item, int gfrom, int glast) +{ + int nmarks = glast - gfrom; + if (nmarks <= 0) { + qWarning("positionCluster: no marks to position!"); + return; + } + + QFontEngine *f = item->font; + + glyph_metrics_t baseInfo = f->boundingBox(item->glyphs[gfrom]); + + if (item->script == QFont::Hebrew) + // we need to attach below the baseline, because of the hebrew iud. + baseInfo.height = QMAX(baseInfo.height, -baseInfo.y); + + QRect baseRect(baseInfo.x, baseInfo.y, baseInfo.width, baseInfo.height); + +// qDebug("---> positionCluster: cluster from %d to %d", gfrom, glast); +// qDebug("baseInfo: %f/%f (%f/%f) off=%f/%f", baseInfo.x, baseInfo.y, baseInfo.width, baseInfo.height, baseInfo.xoff, baseInfo.yoff); + + int size = (f->ascent()/10); + int offsetBase = (size - 4) / 4 + QMIN(size, 4) + 1; +// qDebug("offset = %f", offsetBase); + + bool rightToLeft = item->flags & QTextEngine::RightToLeft; + + int i; + unsigned char lastCmb = 0; + QRect attachmentRect; + + for(i = 1; i <= nmarks; i++) { + glyph_t mark = item->glyphs[gfrom+i]; + QPoint p; + glyph_metrics_t markInfo = f->boundingBox(mark); + QRect markRect(markInfo.x, markInfo.y, markInfo.width, markInfo.height); +// qDebug("markInfo: %f/%f (%f/%f) off=%f/%f", markInfo.x, markInfo.y, markInfo.width, markInfo.height, markInfo.xoff, markInfo.yoff); + + int offset = offsetBase; + unsigned char cmb = item->attributes[gfrom+i].combiningClass; + + // ### maybe the whole position determination should move down to heuristicSetGlyphAttributes. Would save some + // bits in the glyphAttributes structure. + if (cmb < 200) { + // fixed position classes. We approximate by mapping to one of the others. + // currently I added only the ones for arabic, hebrew, lao and thai. + + // for Lao and Thai marks with class 0, see below (heuristicSetGlyphAttributes) + + // add a bit more offset to arabic, a bit hacky + if (cmb >= 27 && cmb <= 36 && offset < 3) + offset +=1; + // below + if ((cmb >= 10 && cmb <= 18) || + cmb == 20 || cmb == 22 || + cmb == 29 || cmb == 32) + cmb = QChar::Combining_Below; + // above + else if (cmb == 23 || cmb == 27 || cmb == 28 || + cmb == 30 || cmb == 31 || (cmb >= 33 && cmb <= 36)) + cmb = QChar::Combining_Above; + //below-right + else if (cmb == 9 || cmb == 103 || cmb == 118) + cmb = QChar::Combining_BelowRight; + // above-right + else if (cmb == 24 || cmb == 107 || cmb == 122) + cmb = QChar::Combining_AboveRight; + else if (cmb == 25) + cmb = QChar::Combining_AboveLeft; + // fixed: + // 19 21 + + } + + // combining marks of different class don't interact. Reset the rectangle. + if (cmb != lastCmb) { + //qDebug("resetting rect"); + attachmentRect = baseRect; + } + + switch(cmb) { + case QChar::Combining_DoubleBelow: + // ### wrong in rtl context! + case QChar::Combining_BelowLeft: + p += QPoint(0, offset); + case QChar::Combining_BelowLeftAttached: + p += attachmentRect.bottomLeft() - markRect.topLeft(); + break; + case QChar::Combining_Below: + p += QPoint(0, offset); + case QChar::Combining_BelowAttached: + p += attachmentRect.bottomLeft() - markRect.topLeft(); + p += QPoint((attachmentRect.width() - markRect.width())/2 , 0); + break; + case QChar::Combining_BelowRight: + p += QPoint(0, offset); + case QChar::Combining_BelowRightAttached: + p += attachmentRect.bottomRight() - markRect.topRight(); + break; + case QChar::Combining_Left: + p += QPoint(-offset, 0); + case QChar::Combining_LeftAttached: + break; + case QChar::Combining_Right: + p += QPoint(offset, 0); + case QChar::Combining_RightAttached: + break; + case QChar::Combining_DoubleAbove: + // ### wrong in RTL context! + case QChar::Combining_AboveLeft: + p += QPoint(0, -offset); + case QChar::Combining_AboveLeftAttached: + p += attachmentRect.topLeft() - markRect.bottomLeft(); + break; + case QChar::Combining_Above: + p += QPoint(0, -offset); + case QChar::Combining_AboveAttached: + p += attachmentRect.topLeft() - markRect.bottomLeft(); + p += QPoint((attachmentRect.width() - markRect.width())/2 , 0); + break; + case QChar::Combining_AboveRight: + p += QPoint(0, -offset); + case QChar::Combining_AboveRightAttached: + p += attachmentRect.topRight() - markRect.bottomRight(); + break; + + case QChar::Combining_IotaSubscript: + default: + break; + } +// qDebug("char=%x combiningClass = %d offset=%d/%d", mark, cmb, p.x(), p.y()); + markRect.moveBy(p.x(), p.y()); + attachmentRect |= markRect; + lastCmb = cmb; + if (rightToLeft) { + item->offsets[gfrom+i].x = p.x(); + item->offsets[gfrom+i].y = p.y(); + } else { + item->offsets[gfrom+i].x = p.x() - baseInfo.xoff; + item->offsets[gfrom+i].y = p.y() - baseInfo.yoff; + } + item->advances[gfrom+i] = 0; + } + item->has_positioning = TRUE; +} + + +void qt_heuristicPosition(QShaperItem *item) +{ + int cEnd = -1; + int i = item->num_glyphs; + while (i--) { + if (cEnd == -1 && item->attributes[i].mark) { + cEnd = i; + } else if (cEnd != -1 && !item->attributes[i].mark) { + positionCluster(item, i, cEnd); + cEnd = -1; + } + } +} + + + +// set the glyph attributes heuristically. Assumes a 1 to 1 relationship between chars and glyphs +// and no reordering. +// also computes logClusters heuristically +static void heuristicSetGlyphAttributes(QShaperItem *item, const QChar *uc, int length) +{ + // justification is missing here!!!!! + + if ( item->num_glyphs != length ) + qWarning("QScriptEngine::heuristicSetGlyphAttributes: char length and num glyphs disagree" ); + + unsigned short *logClusters = item->log_clusters; + + int i; + for (i = 0; i < length; ++i) + logClusters[i] = i; + + // first char in a run is never (treated as) a mark + int cStart = 0; + item->attributes[0].mark = FALSE; + item->attributes[0].clusterStart = TRUE; + item->attributes[0].combiningClass = 0; + if (qIsZeroWidthChar(uc[0].unicode())) { + item->attributes[0].zeroWidth = TRUE; + item->advances[0] = 0; + item->has_positioning = TRUE; + } else { + item->attributes[0].zeroWidth = FALSE; + } + + int lastCat = ::category(uc[0]); + for (i = 1; i < length; ++i) { + int cat = ::category(uc[i]); + if (qIsZeroWidthChar(uc[i].unicode())) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = TRUE; + item->attributes[i].zeroWidth = TRUE; + item->attributes[i].combiningClass = 0; + cStart = i; + item->advances[i] = 0; + item->has_positioning = TRUE; + } else if (cat != QChar::Mark_NonSpacing) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = TRUE; + item->attributes[i].combiningClass = 0; + cStart = i; + } else { + int cmb = ::combiningClass(uc[i]); + + if (cmb == 0) { + // Fix 0 combining classes + if ((uc[i].unicode() & 0xff00) == 0x0e00) { + // thai or lao + unsigned char col = uc[i].cell(); + if (col == 0x31 || + col == 0x34 || + col == 0x35 || + col == 0x36 || + col == 0x37 || + col == 0x47 || + col == 0x4c || + col == 0x4d || + col == 0x4e) { + cmb = QChar::Combining_AboveRight; + } else if (col == 0xb1 || + col == 0xb4 || + col == 0xb5 || + col == 0xb6 || + col == 0xb7 || + col == 0xbb || + col == 0xcc || + col == 0xcd) { + cmb = QChar::Combining_Above; + } else if (col == 0xbc) { + cmb = QChar::Combining_Below; + } + } + } + + item->attributes[i].mark = TRUE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].combiningClass = cmb; + logClusters[i] = cStart; + item->advances[i] = 0; + item->has_positioning = TRUE; + } + + if (lastCat == QChar::Separator_Space) + item->attributes[i-1].justification = GlyphAttributes::Space; + else if (cat != QChar::Mark_NonSpacing) + item->attributes[i-1].justification = GlyphAttributes::Character; + else + item->attributes[i-1].justification = GlyphAttributes::NoJustification; + + lastCat = cat; + } +} + +static void heuristicSetGlyphAttributes(QShaperItem *item) +{ + heuristicSetGlyphAttributes(item, item->string->unicode() + item->from, item->length); +} + + +static bool basic_shape(QShaperItem *item) +{ + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + heuristicSetGlyphAttributes(item); + qt_heuristicPosition(item); + return TRUE; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Middle eastern languages +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +// Uniscribe also defines dlig for Hebrew, but we leave this out for now, as it's mostly +// ligatures one does not want in modern Hebrew (as lam-alef ligatures). +enum { + CcmpProperty = 0x1 +}; +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) +static const QOpenType::Features hebrew_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + {0, 0} +}; +#endif +/* Hebrew shaping. In the non opentype case we try to use the + presentation forms specified for Hebrew. Especially for the + ligatures with Dagesh this gives much better results than we could + achieve manually. +*/ +static bool hebrew_shape(QShaperItem *item) +{ + Q_ASSERT(item->script == QFont::Hebrew); + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + QOpenType *openType = item->font->openType(); + + if (openType && openType->supportsScript(item->script)) { + openType->selectScript(item->script, hebrew_features); + + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + heuristicSetGlyphAttributes(item); + openType->shape(item); + return openType->positionAndAdd(item); + } +#endif + + enum { + Dagesh = 0x5bc, + ShinDot = 0x5c1, + SinDot = 0x5c2, + Patah = 0x5b7, + Qamats = 0x5b8, + Holam = 0x5b9, + Rafe = 0x5bf + }; + unsigned short chars[512]; + QChar *shapedChars = item->length > 256 ? (QChar *)::malloc(2*item->length * sizeof(QChar)) : (QChar *)chars; + + const QChar *uc = item->string->unicode() + item->from; + unsigned short *logClusters = item->log_clusters; + + *shapedChars = *uc; + logClusters[0] = 0; + int slen = 1; + int cluster_start = 0; + int i; + for (i = 1; i < item->length; ++i) { + ushort base = shapedChars[slen-1].unicode(); + ushort shaped = 0; + bool invalid = FALSE; + if (uc[i].unicode() == Dagesh) { + if (base >= 0x5d0 + && base <= 0x5ea + && base != 0x5d7 + && base != 0x5dd + && base != 0x5df + && base != 0x5e2 + && base != 0x5e5) { + shaped = base - 0x5d0 + 0xfb30; + } else if (base == 0xfb2a || base == 0xfb2b /* Shin with Shin or Sin dot */) { + shaped = base + 2; + } else { + invalid = TRUE; + } + } else if (uc[i].unicode() == ShinDot) { + if (base == 0x05e9) + shaped = 0xfb2a; + else if (base == 0xfb49) + shaped = 0xfb2c; + else + invalid = TRUE; + } else if (uc[i].unicode() == SinDot) { + if (base == 0x05e9) + shaped = 0xfb2b; + else if (base == 0xfb49) + shaped = 0xfb2d; + else + invalid = TRUE; + } else if (uc[i].unicode() == Patah) { + if (base == 0x5d0) + shaped = 0xfb2e; + } else if (uc[i].unicode() == Qamats) { + if (base == 0x5d0) + shaped = 0xfb2f; + } else if (uc[i].unicode() == Holam) { + if (base == 0x5d5) + shaped = 0xfb4b; + } else if (uc[i].unicode() == Rafe) { + if (base == 0x5d1) + shaped = 0xfb4c; + else if (base == 0x5db) + shaped = 0xfb4d; + else if (base == 0x5e4) + shaped = 0xfb4e; + } + + if (invalid) { + shapedChars[slen] = 0x25cc; + item->attributes[slen].clusterStart = TRUE; + item->attributes[slen].mark = FALSE; + item->attributes[slen].combiningClass = 0; + cluster_start = slen; + ++slen; + } + if (shaped) { + if (item->font->canRender((QChar *)&shaped, 1)) { + shapedChars[slen-1] = QChar(shaped); + } else + shaped = 0; + } + if (!shaped) { + shapedChars[slen] = uc[i]; + if (::category(uc[i]) != QChar::Mark_NonSpacing) { + item->attributes[slen].clusterStart = TRUE; + item->attributes[slen].mark = FALSE; + item->attributes[slen].combiningClass = 0; + cluster_start = slen; + } else { + item->attributes[slen].clusterStart = FALSE; + item->attributes[slen].mark = TRUE; + item->attributes[slen].combiningClass = ::combiningClass(uc[i]); + } + ++slen; + } + logClusters[i] = cluster_start; + } + + if (item->font->stringToCMap(shapedChars, slen, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + for (i = 0; i < item->num_glyphs; ++i) { + if (item->attributes[i].mark) + item->advances[i] = 0; + } + qt_heuristicPosition(item); + + if (item->length > 256) + ::free(shapedChars); + return TRUE; +} + +// these groups correspond to the groups defined in the Unicode standard. +// Some of these groups are equal whith regards to both joining and line breaking behaviour, +// and thus have the same enum value +// +// I'm not sure the mapping of syriac to arabic enums is correct with regards to justification, but as +// I couldn't find any better document I'll hope for the best. +enum ArabicGroup { + // NonJoining + ArabicNone, + ArabicSpace, + // Transparent + Transparent, + // Causing + Center, + Kashida, + + // Arabic + // Dual + Beh, + Noon, + Meem = Noon, + Heh = Noon, + KnottedHeh = Noon, + HehGoal = Noon, + SwashKaf = Noon, + Yeh, + Hah, + Seen, + Sad = Seen, + Tah, + Kaf = Tah, + Gaf = Tah, + Lam = Tah, + Ain, + Feh = Ain, + Qaf = Ain, + // Right + Alef, + Waw, + Dal, + TehMarbuta = Dal, + Reh, + HamzaOnHehGoal, + YehWithTail = HamzaOnHehGoal, + YehBarre = HamzaOnHehGoal, + + // Syriac + // Dual + Beth = Beh, + Gamal = Ain, + Heth = Noon, + Teth = Hah, + Yudh = Noon, + Kaph = Noon, + Lamadh = Lam, + Mim = Noon, + Nun = Noon, + Semakh = Noon, + FinalSemakh = Noon, + SyriacE = Ain, + Pe = Ain, + ReversedPe = Hah, + Qaph = Noon, + Shin = Noon, + Fe = Ain, + + // Right + Alaph = Alef, + Dalath = Dal, + He = Dal, + SyriacWaw = Waw, + Zain = Alef, + YudhHe = Waw, + Sadhe = HamzaOnHehGoal, + Taw = Dal, + + // Compiler bug? Otherwise ArabicGroupsEnd would be equal to Dal + 1. + Dummy = HamzaOnHehGoal, + ArabicGroupsEnd +}; + +static const unsigned char arabic_group[0x150] = { + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + ArabicNone, ArabicNone, Alef, Alef, + Waw, Alef, Yeh, Alef, + Beh, TehMarbuta, Beh, Beh, + Hah, Hah, Hah, Dal, + + Dal, Reh, Reh, Seen, + Seen, Sad, Sad, Tah, + Tah, Ain, Ain, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + // 0x640 + Kashida, Feh, Qaf, Kaf, + Lam, Meem, Noon, Heh, + Waw, Yeh, Yeh, Transparent, + Transparent, Transparent, Transparent, Transparent, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, Beh, Qaf, + + Transparent, Alef, Alef, Alef, + ArabicNone, Alef, Waw, Waw, + Yeh, Beh, Beh, Beh, + Beh, Beh, Beh, Beh, + + // 0x680 + Beh, Hah, Hah, Hah, + Hah, Hah, Hah, Hah, + Dal, Dal, Dal, Dal, + Dal, Dal, Dal, Dal, + + Dal, Reh, Reh, Reh, + Reh, Reh, Reh, Reh, + Reh, Reh, Seen, Seen, + Seen, Sad, Sad, Tah, + + Ain, Feh, Feh, Feh, + Feh, Feh, Feh, Qaf, + Qaf, Gaf, SwashKaf, Gaf, + Kaf, Kaf, Kaf, Gaf, + + Gaf, Gaf, Gaf, Gaf, + Gaf, Lam, Lam, Lam, + Lam, Noon, Noon, Noon, + Noon, Noon, KnottedHeh, Hah, + + // 0x6c0 + TehMarbuta, HehGoal, HamzaOnHehGoal, HamzaOnHehGoal, + Waw, Waw, Waw, Waw, + Waw, Waw, Waw, Waw, + Yeh, YehWithTail, Yeh, Waw, + + Yeh, Yeh, YehBarre, YehBarre, + ArabicNone, TehMarbuta, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, ArabicNone, ArabicNone, Transparent, + + Transparent, Transparent, Transparent, Transparent, + Transparent, ArabicNone, ArabicNone, Transparent, + Transparent, ArabicNone, Transparent, Transparent, + Transparent, Transparent, Dal, Reh, + + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, Seen, Sad, + Ain, ArabicNone, ArabicNone, KnottedHeh, + + // 0x700 + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + Alaph, Transparent, Beth, Gamal, + Gamal, Dalath, Dalath, He, + SyriacWaw, Zain, Heth, Teth, + Teth, Yudh, YudhHe, Kaph, + + Lamadh, Mim, Nun, Semakh, + FinalSemakh, SyriacE, Pe, ReversedPe, + Sadhe, Qaph, Dalath, Shin, + Taw, Beth, Gamal, Dalath, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, ArabicNone, + ArabicNone, Zain, Kaph, Fe, +}; + +static inline ArabicGroup arabicGroup(unsigned short uc) +{ + if (uc >= 0x0600 && uc < 0x750) + return (ArabicGroup) arabic_group[uc-0x600]; + else if (uc == 0x200d) + return Center; + else if (::category(uc) == QChar::Separator_Space) + return ArabicSpace; + else + return ArabicNone; +} + + +/* + Arabic shaping obeys a number of rules according to the joining classes (see Unicode book, section on + arabic). + + Each unicode char has a joining class (right, dual (left&right), center (joincausing) or transparent). + transparent joining is not encoded in QChar::joining(), but applies to all combining marks and format marks. + + Right join-causing: dual + center + Left join-causing: dual + right + center + + Rules are as follows (for a string already in visual order, as we have it here): + + R1 Transparent characters do not affect joining behaviour. + R2 A right joining character, that has a right join-causing char on the right will get form XRight + (R3 A left joining character, that has a left join-causing char on the left will get form XLeft) + Note: the above rule is meaningless, as there are no pure left joining characters defined in Unicode + R4 A dual joining character, that has a left join-causing char on the left and a right join-causing char on + the right will get form XMedial + R5 A dual joining character, that has a right join causing char on the right, and no left join causing char on the left + will get form XRight + R6 A dual joining character, that has a left join causing char on the left, and no right join causing char on the right + will get form XLeft + R7 Otherwise the character will get form XIsolated + + Additionally we have to do the minimal ligature support for lam-alef ligatures: + + L1 Transparent characters do not affect ligature behaviour. + L2 Any sequence of Alef(XRight) + Lam(XMedial) will form the ligature Alef.Lam(XLeft) + L3 Any sequence of Alef(XRight) + Lam(XLeft) will form the ligature Alef.Lam(XIsolated) + + The state table below handles rules R1-R7. +*/ + +enum Shape { + XIsolated, + XFinal, + XInitial, + XMedial, + // intermediate state + XCausing +}; + + +enum Joining { + JNone, + JCausing, + JDual, + JRight, + JTransparent +}; + + +static const Joining joining_for_group[ArabicGroupsEnd] = { + // NonJoining + JNone, // ArabicNone + JNone, // ArabicSpace + // Transparent + JTransparent, // Transparent + // Causing + JCausing, // Center + JCausing, // Kashida + // Dual + JDual, // Beh + JDual, // Noon + JDual, // Yeh + JDual, // Hah + JDual, // Seen + JDual, // Tah + JDual, // Ain + // Right + JRight, // Alef + JRight, // Waw + JRight, // Dal + JRight, // Reh + JRight // HamzaOnHehGoal +}; + + +struct JoiningPair { + Shape form1; + Shape form2; +}; + +static const JoiningPair joining_table[5][4] = +// None, Causing, Dual, Right +{ + { { XIsolated, XIsolated }, { XIsolated, XCausing }, { XIsolated, XInitial }, { XIsolated, XIsolated } }, // XIsolated + { { XFinal, XIsolated }, { XFinal, XCausing }, { XFinal, XInitial }, { XFinal, XIsolated } }, // XFinal + { { XIsolated, XIsolated }, { XInitial, XCausing }, { XInitial, XMedial }, { XInitial, XFinal } }, // XInitial + { { XFinal, XIsolated }, { XMedial, XCausing }, { XMedial, XMedial }, { XMedial, XFinal } }, // XMedial + { { XIsolated, XIsolated }, { XIsolated, XCausing }, { XIsolated, XMedial }, { XIsolated, XFinal } }, // XCausing +}; + + +/* +According to http://www.microsoft.com/middleeast/Arabicdev/IE6/KBase.asp + +1. Find the priority of the connecting opportunities in each word +2. Add expansion at the highest priority connection opportunity +3. If more than one connection opportunity have the same highest value, + use the opportunity closest to the end of the word. + +Following is a chart that provides the priority for connection +opportunities and where expansion occurs. The character group names +are those in table 6.6 of the UNICODE 2.0 book. + + +PrioritY Glyph Condition Kashida Location + +Arabic_Kashida User inserted Kashida The user entered a Kashida in a position. After the user + (Shift+j or Shift+[E with hat]) Thus, it is the highest priority to insert an inserted kashida + automatic kashida. + +Arabic_Seen Seen, Sad Connecting to the next character. After the character. + (Initial or medial form). + +Arabic_HaaDal Teh Marbutah, Haa, Dal Connecting to previous character. Before the final form + of these characters. + +Arabic_Alef Alef, Tah, Lam, Connecting to previous character. Before the final form + Kaf and Gaf of these characters. + +Arabic_BaRa Reh, Yeh Connected to medial Beh Before preceding medial Baa + +Arabic_Waw Waw, Ain, Qaf, Feh Connecting to previous character. Before the final form of + these characters. + +Arabic_Normal Other connecting Connecting to previous character. Before the final form + characters of these characters. + + + +This seems to imply that we have at most one kashida point per arabic word. + +*/ + +struct QArabicProperties { + unsigned char shape; + unsigned char justification; +}; + + +static void getArabicProperties(const unsigned short *chars, int len, QArabicProperties *properties) +{ +// qDebug("arabicSyriacOpenTypeShape: properties:"); + int lastPos = 0; + int lastGroup = ArabicNone; + + ArabicGroup group = arabicGroup(chars[0]); + Joining j = joining_for_group[group]; + Shape shape = joining_table[XIsolated][j].form2; + properties[0].justification = GlyphAttributes::NoJustification; + + for (int i = 1; i < len; ++i) { + // #### fix handling for spaces and punktuation + properties[i].justification = GlyphAttributes::NoJustification; + + group = arabicGroup(chars[i]); + j = joining_for_group[group]; + + if (j == JTransparent) { + properties[i].shape = XIsolated; + continue; + } + + properties[lastPos].shape = joining_table[shape][j].form1; + shape = joining_table[shape][j].form2; + + switch(lastGroup) { + case Seen: + if (properties[lastPos].shape == XInitial || properties[lastPos].shape == XMedial) + properties[i-1].justification = GlyphAttributes::Arabic_Seen; + break; + case Hah: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_HaaDal; + break; + case Alef: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_Alef; + break; + case Ain: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_Waw; + break; + case Noon: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_Normal; + break; + case ArabicNone: + break; + + default: + Q_ASSERT(FALSE); + } + + lastGroup = ArabicNone; + + switch(group) { + case ArabicNone: + case Transparent: + // ### Center should probably be treated as transparent when it comes to justification. + case Center: + break; + case ArabicSpace: + properties[i].justification = GlyphAttributes::Arabic_Space; + break; + case Kashida: + properties[i].justification = GlyphAttributes::Arabic_Kashida; + break; + case Seen: + lastGroup = Seen; + break; + + case Hah: + case Dal: + lastGroup = Hah; + break; + + case Alef: + case Tah: + lastGroup = Alef; + break; + + case Yeh: + case Reh: + if (properties[lastPos].shape == XMedial && arabicGroup(chars[lastPos]) == Beh) + properties[lastPos-1].justification = GlyphAttributes::Arabic_BaRa; + break; + + case Ain: + case Waw: + lastGroup = Ain; + break; + + case Noon: + case Beh: + case HamzaOnHehGoal: + lastGroup = Noon; + break; + case ArabicGroupsEnd: + Q_ASSERT(FALSE); + } + + lastPos = i; + } + properties[lastPos].shape = joining_table[shape][JNone].form1; + + +// for (int i = 0; i < len; ++i) +// qDebug("arabic properties(%d): uc=%x shape=%d, justification=%d", i, chars[i], properties[i].shape, properties[i].justification); +} + + + + + + +// The unicode to unicode shaping codec. +// does only presentation forms B at the moment, but that should be enough for +// simple display +static const ushort arabicUnicodeMapping[256][2] = { + // base of shaped forms, and number-1 of them (0 for non shaping, + // 1 for right binding and 3 for dual binding + + // These are just the glyphs available in Unicode, + // some characters are in R class, but have no glyphs in Unicode. + + { 0x0600, 0 }, // 0x0600 + { 0x0601, 0 }, // 0x0601 + { 0x0602, 0 }, // 0x0602 + { 0x0603, 0 }, // 0x0603 + { 0x0604, 0 }, // 0x0604 + { 0x0605, 0 }, // 0x0605 + { 0x0606, 0 }, // 0x0606 + { 0x0607, 0 }, // 0x0607 + { 0x0608, 0 }, // 0x0608 + { 0x0609, 0 }, // 0x0609 + { 0x060A, 0 }, // 0x060A + { 0x060B, 0 }, // 0x060B + { 0x060C, 0 }, // 0x060C + { 0x060D, 0 }, // 0x060D + { 0x060E, 0 }, // 0x060E + { 0x060F, 0 }, // 0x060F + + { 0x0610, 0 }, // 0x0610 + { 0x0611, 0 }, // 0x0611 + { 0x0612, 0 }, // 0x0612 + { 0x0613, 0 }, // 0x0613 + { 0x0614, 0 }, // 0x0614 + { 0x0615, 0 }, // 0x0615 + { 0x0616, 0 }, // 0x0616 + { 0x0617, 0 }, // 0x0617 + { 0x0618, 0 }, // 0x0618 + { 0x0619, 0 }, // 0x0619 + { 0x061A, 0 }, // 0x061A + { 0x061B, 0 }, // 0x061B + { 0x061C, 0 }, // 0x061C + { 0x061D, 0 }, // 0x061D + { 0x061E, 0 }, // 0x061E + { 0x061F, 0 }, // 0x061F + + { 0x0620, 0 }, // 0x0620 + { 0xFE80, 0 }, // 0x0621 HAMZA + { 0xFE81, 1 }, // 0x0622 R ALEF WITH MADDA ABOVE + { 0xFE83, 1 }, // 0x0623 R ALEF WITH HAMZA ABOVE + { 0xFE85, 1 }, // 0x0624 R WAW WITH HAMZA ABOVE + { 0xFE87, 1 }, // 0x0625 R ALEF WITH HAMZA BELOW + { 0xFE89, 3 }, // 0x0626 D YEH WITH HAMZA ABOVE + { 0xFE8D, 1 }, // 0x0627 R ALEF + { 0xFE8F, 3 }, // 0x0628 D BEH + { 0xFE93, 1 }, // 0x0629 R TEH MARBUTA + { 0xFE95, 3 }, // 0x062A D TEH + { 0xFE99, 3 }, // 0x062B D THEH + { 0xFE9D, 3 }, // 0x062C D JEEM + { 0xFEA1, 3 }, // 0x062D D HAH + { 0xFEA5, 3 }, // 0x062E D KHAH + { 0xFEA9, 1 }, // 0x062F R DAL + + { 0xFEAB, 1 }, // 0x0630 R THAL + { 0xFEAD, 1 }, // 0x0631 R REH + { 0xFEAF, 1 }, // 0x0632 R ZAIN + { 0xFEB1, 3 }, // 0x0633 D SEEN + { 0xFEB5, 3 }, // 0x0634 D SHEEN + { 0xFEB9, 3 }, // 0x0635 D SAD + { 0xFEBD, 3 }, // 0x0636 D DAD + { 0xFEC1, 3 }, // 0x0637 D TAH + { 0xFEC5, 3 }, // 0x0638 D ZAH + { 0xFEC9, 3 }, // 0x0639 D AIN + { 0xFECD, 3 }, // 0x063A D GHAIN + { 0x063B, 0 }, // 0x063B + { 0x063C, 0 }, // 0x063C + { 0x063D, 0 }, // 0x063D + { 0x063E, 0 }, // 0x063E + { 0x063F, 0 }, // 0x063F + + { 0x0640, 0 }, // 0x0640 C TATWEEL // ### Join Causing, only one glyph + { 0xFED1, 3 }, // 0x0641 D FEH + { 0xFED5, 3 }, // 0x0642 D QAF + { 0xFED9, 3 }, // 0x0643 D KAF + { 0xFEDD, 3 }, // 0x0644 D LAM + { 0xFEE1, 3 }, // 0x0645 D MEEM + { 0xFEE5, 3 }, // 0x0646 D NOON + { 0xFEE9, 3 }, // 0x0647 D HEH + { 0xFEED, 1 }, // 0x0648 R WAW + { 0x0649, 3 }, // 0x0649 ALEF MAKSURA // ### Dual, glyphs not consecutive, handle in code. + { 0xFEF1, 3 }, // 0x064A D YEH + { 0x064B, 0 }, // 0x064B + { 0x064C, 0 }, // 0x064C + { 0x064D, 0 }, // 0x064D + { 0x064E, 0 }, // 0x064E + { 0x064F, 0 }, // 0x064F + + { 0x0650, 0 }, // 0x0650 + { 0x0651, 0 }, // 0x0651 + { 0x0652, 0 }, // 0x0652 + { 0x0653, 0 }, // 0x0653 + { 0x0654, 0 }, // 0x0654 + { 0x0655, 0 }, // 0x0655 + { 0x0656, 0 }, // 0x0656 + { 0x0657, 0 }, // 0x0657 + { 0x0658, 0 }, // 0x0658 + { 0x0659, 0 }, // 0x0659 + { 0x065A, 0 }, // 0x065A + { 0x065B, 0 }, // 0x065B + { 0x065C, 0 }, // 0x065C + { 0x065D, 0 }, // 0x065D + { 0x065E, 0 }, // 0x065E + { 0x065F, 0 }, // 0x065F + + { 0x0660, 0 }, // 0x0660 + { 0x0661, 0 }, // 0x0661 + { 0x0662, 0 }, // 0x0662 + { 0x0663, 0 }, // 0x0663 + { 0x0664, 0 }, // 0x0664 + { 0x0665, 0 }, // 0x0665 + { 0x0666, 0 }, // 0x0666 + { 0x0667, 0 }, // 0x0667 + { 0x0668, 0 }, // 0x0668 + { 0x0669, 0 }, // 0x0669 + { 0x066A, 0 }, // 0x066A + { 0x066B, 0 }, // 0x066B + { 0x066C, 0 }, // 0x066C + { 0x066D, 0 }, // 0x066D + { 0x066E, 0 }, // 0x066E + { 0x066F, 0 }, // 0x066F + + { 0x0670, 0 }, // 0x0670 + { 0xFB50, 1 }, // 0x0671 R ALEF WASLA + { 0x0672, 0 }, // 0x0672 + { 0x0673, 0 }, // 0x0673 + { 0x0674, 0 }, // 0x0674 + { 0x0675, 0 }, // 0x0675 + { 0x0676, 0 }, // 0x0676 + { 0x0677, 0 }, // 0x0677 + { 0x0678, 0 }, // 0x0678 + { 0xFB66, 3 }, // 0x0679 D TTEH + { 0xFB5E, 3 }, // 0x067A D TTEHEH + { 0xFB52, 3 }, // 0x067B D BEEH + { 0x067C, 0 }, // 0x067C + { 0x067D, 0 }, // 0x067D + { 0xFB56, 3 }, // 0x067E D PEH + { 0xFB62, 3 }, // 0x067F D TEHEH + + { 0xFB5A, 3 }, // 0x0680 D BEHEH + { 0x0681, 0 }, // 0x0681 + { 0x0682, 0 }, // 0x0682 + { 0xFB76, 3 }, // 0x0683 D NYEH + { 0xFB72, 3 }, // 0x0684 D DYEH + { 0x0685, 0 }, // 0x0685 + { 0xFB7A, 3 }, // 0x0686 D TCHEH + { 0xFB7E, 3 }, // 0x0687 D TCHEHEH + { 0xFB88, 1 }, // 0x0688 R DDAL + { 0x0689, 0 }, // 0x0689 + { 0x068A, 0 }, // 0x068A + { 0x068B, 0 }, // 0x068B + { 0xFB84, 1 }, // 0x068C R DAHAL + { 0xFB82, 1 }, // 0x068D R DDAHAL + { 0xFB86, 1 }, // 0x068E R DUL + { 0x068F, 0 }, // 0x068F + + { 0x0690, 0 }, // 0x0690 + { 0xFB8C, 1 }, // 0x0691 R RREH + { 0x0692, 0 }, // 0x0692 + { 0x0693, 0 }, // 0x0693 + { 0x0694, 0 }, // 0x0694 + { 0x0695, 0 }, // 0x0695 + { 0x0696, 0 }, // 0x0696 + { 0x0697, 0 }, // 0x0697 + { 0xFB8A, 1 }, // 0x0698 R JEH + { 0x0699, 0 }, // 0x0699 + { 0x069A, 0 }, // 0x069A + { 0x069B, 0 }, // 0x069B + { 0x069C, 0 }, // 0x069C + { 0x069D, 0 }, // 0x069D + { 0x069E, 0 }, // 0x069E + { 0x069F, 0 }, // 0x069F + + { 0x06A0, 0 }, // 0x06A0 + { 0x06A1, 0 }, // 0x06A1 + { 0x06A2, 0 }, // 0x06A2 + { 0x06A3, 0 }, // 0x06A3 + { 0xFB6A, 3 }, // 0x06A4 D VEH + { 0x06A5, 0 }, // 0x06A5 + { 0xFB6E, 3 }, // 0x06A6 D PEHEH + { 0x06A7, 0 }, // 0x06A7 + { 0x06A8, 0 }, // 0x06A8 + { 0xFB8E, 3 }, // 0x06A9 D KEHEH + { 0x06AA, 0 }, // 0x06AA + { 0x06AB, 0 }, // 0x06AB + { 0x06AC, 0 }, // 0x06AC + { 0xFBD3, 3 }, // 0x06AD D NG + { 0x06AE, 0 }, // 0x06AE + { 0xFB92, 3 }, // 0x06AF D GAF + + { 0x06B0, 0 }, // 0x06B0 + { 0xFB9A, 3 }, // 0x06B1 D NGOEH + { 0x06B2, 0 }, // 0x06B2 + { 0xFB96, 3 }, // 0x06B3 D GUEH + { 0x06B4, 0 }, // 0x06B4 + { 0x06B5, 0 }, // 0x06B5 + { 0x06B6, 0 }, // 0x06B6 + { 0x06B7, 0 }, // 0x06B7 + { 0x06B8, 0 }, // 0x06B8 + { 0x06B9, 0 }, // 0x06B9 + { 0xFB9E, 1 }, // 0x06BA R NOON GHUNNA + { 0xFBA0, 3 }, // 0x06BB D RNOON + { 0x06BC, 0 }, // 0x06BC + { 0x06BD, 0 }, // 0x06BD + { 0xFBAA, 3 }, // 0x06BE D HEH DOACHASHMEE + { 0x06BF, 0 }, // 0x06BF + + { 0xFBA4, 1 }, // 0x06C0 R HEH WITH YEH ABOVE + { 0xFBA6, 3 }, // 0x06C1 D HEH GOAL + { 0x06C2, 0 }, // 0x06C2 + { 0x06C3, 0 }, // 0x06C3 + { 0x06C4, 0 }, // 0x06C4 + { 0xFBE0, 1 }, // 0x06C5 R KIRGHIZ OE + { 0xFBD9, 1 }, // 0x06C6 R OE + { 0xFBD7, 1 }, // 0x06C7 R U + { 0xFBDB, 1 }, // 0x06C8 R YU + { 0xFBE2, 1 }, // 0x06C9 R KIRGHIZ YU + { 0x06CA, 0 }, // 0x06CA + { 0xFBDE, 1 }, // 0x06CB R VE + { 0xFBFC, 3 }, // 0x06CC D FARSI YEH + { 0x06CD, 0 }, // 0x06CD + { 0x06CE, 0 }, // 0x06CE + { 0x06CF, 0 }, // 0x06CF + + { 0xFBE4, 3 }, // 0x06D0 D E + { 0x06D1, 0 }, // 0x06D1 + { 0xFBAE, 1 }, // 0x06D2 R YEH BARREE + { 0xFBB0, 1 }, // 0x06D3 R YEH BARREE WITH HAMZA ABOVE + { 0x06D4, 0 }, // 0x06D4 + { 0x06D5, 0 }, // 0x06D5 + { 0x06D6, 0 }, // 0x06D6 + { 0x06D7, 0 }, // 0x06D7 + { 0x06D8, 0 }, // 0x06D8 + { 0x06D9, 0 }, // 0x06D9 + { 0x06DA, 0 }, // 0x06DA + { 0x06DB, 0 }, // 0x06DB + { 0x06DC, 0 }, // 0x06DC + { 0x06DD, 0 }, // 0x06DD + { 0x06DE, 0 }, // 0x06DE + { 0x06DF, 0 }, // 0x06DF + + { 0x06E0, 0 }, // 0x06E0 + { 0x06E1, 0 }, // 0x06E1 + { 0x06E2, 0 }, // 0x06E2 + { 0x06E3, 0 }, // 0x06E3 + { 0x06E4, 0 }, // 0x06E4 + { 0x06E5, 0 }, // 0x06E5 + { 0x06E6, 0 }, // 0x06E6 + { 0x06E7, 0 }, // 0x06E7 + { 0x06E8, 0 }, // 0x06E8 + { 0x06E9, 0 }, // 0x06E9 + { 0x06EA, 0 }, // 0x06EA + { 0x06EB, 0 }, // 0x06EB + { 0x06EC, 0 }, // 0x06EC + { 0x06ED, 0 }, // 0x06ED + { 0x06EE, 0 }, // 0x06EE + { 0x06EF, 0 }, // 0x06EF + + { 0x06F0, 0 }, // 0x06F0 + { 0x06F1, 0 }, // 0x06F1 + { 0x06F2, 0 }, // 0x06F2 + { 0x06F3, 0 }, // 0x06F3 + { 0x06F4, 0 }, // 0x06F4 + { 0x06F5, 0 }, // 0x06F5 + { 0x06F6, 0 }, // 0x06F6 + { 0x06F7, 0 }, // 0x06F7 + { 0x06F8, 0 }, // 0x06F8 + { 0x06F9, 0 }, // 0x06F9 + { 0x06FA, 0 }, // 0x06FA + { 0x06FB, 0 }, // 0x06FB + { 0x06FC, 0 }, // 0x06FC + { 0x06FD, 0 }, // 0x06FD + { 0x06FE, 0 }, // 0x06FE + { 0x06FF, 0 } // 0x06FF +}; + +// the arabicUnicodeMapping does not work for U+0649 ALEF MAKSURA, this table does +static const ushort alefMaksura[4] = {0xFEEF, 0xFEF0, 0xFBE8, 0xFBE9}; + +// this is a bit tricky. Alef always binds to the right, so the second parameter descibing the shape +// of the lam can be either initial of medial. So initial maps to the isolated form of the ligature, +// medial to the final form +static const ushort arabicUnicodeLamAlefMapping[6][4] = { + { 0xfffd, 0xfffd, 0xfef5, 0xfef6 }, // 0x622 R Alef with Madda above + { 0xfffd, 0xfffd, 0xfef7, 0xfef8 }, // 0x623 R Alef with Hamza above + { 0xfffd, 0xfffd, 0xfffd, 0xfffd }, // 0x624 // Just to fill the table ;-) + { 0xfffd, 0xfffd, 0xfef9, 0xfefa }, // 0x625 R Alef with Hamza below + { 0xfffd, 0xfffd, 0xfffd, 0xfffd }, // 0x626 // Just to fill the table ;-) + { 0xfffd, 0xfffd, 0xfefb, 0xfefc } // 0x627 R Alef +}; + +static inline int getShape(uchar cell, int shape) +{ + // the arabicUnicodeMapping does not work for U+0649 ALEF MAKSURA, handle this here + uint ch = (cell != 0x49) + ? (shape ? arabicUnicodeMapping[cell][0] + shape : 0x600+cell) + : alefMaksura[shape] ; + return ch; +} + + +/* + Two small helper functions for arabic shaping. +*/ +static inline const QChar prevChar(const QString *str, int pos) +{ + //qDebug("leftChar: pos=%d", pos); + pos--; + const QChar *ch = str->unicode() + pos; + while(pos > -1) { + if(::category(*ch) != QChar::Mark_NonSpacing) + return *ch; + pos--; + ch--; + } + return QChar::replacement; +} + +static inline const QChar nextChar(const QString *str, int pos) +{ + pos++; + int len = str->length(); + const QChar *ch = str->unicode() + pos; + while(pos < len) { + //qDebug("rightChar: %d isLetter=%d, joining=%d", pos, ch.isLetter(), ch.joining()); + if(::category(*ch) != QChar::Mark_NonSpacing) + return *ch; + // assume it's a transparent char, this might not be 100% correct + pos++; + ch++; + } + return QChar::replacement; +} + + +static void shapedString(const QString *uc, int from, int len, QChar *shapeBuffer, int *shapedLength, + bool reverse, GlyphAttributes *attributes, unsigned short *logClusters) +{ + Q_ASSERT((int)uc->length() >= from + len); + + if(len == 0) { + *shapedLength = 0; + return; + } + + QVarLengthArray<QArabicProperties> props(len + 2); + QArabicProperties *properties = props.data(); + int f = from; + int l = len; + if (from > 0) { + --f; + ++l; + ++properties; + } + if (f + l < (int)uc->length()) { + ++l; + } + getArabicProperties((const unsigned short *)(uc->unicode()+f), l, props.data()); + + const QChar *ch = uc->unicode() + from; + QChar *data = shapeBuffer; + int clusterStart = 0; + + for (int i = 0; i < len; i++) { + uchar r = ch->row(); + int gpos = data - shapeBuffer; + + if (r != 0x06) { + if (r == 0x20) { + uchar c = ch->cell(); + if (c == 0x0c || c == 0x0d) + // remove ZWJ and ZWNJ + goto skip; + } + if (reverse) + *data = mirroredChar(*ch); + else + *data = *ch; + } else { + uchar c = ch->cell(); + int pos = i + from; + int shape = properties[i].shape; +// qDebug("mapping U+%x to shape %d glyph=0x%x", ch->unicode(), shape, getShape(c, shape)); + // take care of lam-alef ligatures (lam right of alef) + ushort map; + switch (c) { + case 0x44: { // lam + const QChar pch = nextChar(uc, pos); + if (pch.row() == 0x06) { + switch (pch.cell()) { + case 0x22: + case 0x23: + case 0x25: + case 0x27: +// qDebug(" lam of lam-alef ligature"); + map = arabicUnicodeLamAlefMapping[pch.cell() - 0x22][shape]; + goto next; + default: + break; + } + } + break; + } + case 0x22: // alef with madda + case 0x23: // alef with hamza above + case 0x25: // alef with hamza below + case 0x27: // alef + if (prevChar(uc, pos).unicode() == 0x0644) { + // have a lam alef ligature + //qDebug(" alef of lam-alef ligature"); + goto skip; + } + default: + break; + } + map = getShape(c, shape); + next: + *data = map; + } + // ##### Fixme + //attributes[gpos].zeroWidth = zeroWidth; + if (::category(*ch) == QChar::Mark_NonSpacing) { + attributes[gpos].mark = TRUE; +// qDebug("glyph %d (char %d) is mark!", gpos, i); + } else { + attributes[gpos].mark = FALSE; + clusterStart = data - shapeBuffer; + } + attributes[gpos].clusterStart = !attributes[gpos].mark; + attributes[gpos].combiningClass = combiningClass(*ch); + attributes[gpos].justification = properties[i].justification; +// qDebug("data[%d] = %x (from %x)", gpos, (uint)data->unicode(), ch->unicode()); + data++; + skip: + ch++; + logClusters[i] = clusterStart; + } + *shapedLength = data - shapeBuffer; +} + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + +enum { + InitProperty = 0x2, + IsolProperty = 0x4, + FinaProperty = 0x8, + MediProperty = 0x10, + RligProperty = 0x20, + CaltProperty = 0x40, + LigaProperty = 0x80, + DligProperty = 0x100, + CswhProperty = 0x200, + MsetProperty = 0x400 +}; + +static const QOpenType::Features arabic_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('i', 's', 'o', 'l'), IsolProperty }, + { FT_MAKE_TAG('f', 'i', 'n', 'a'), FinaProperty }, + { FT_MAKE_TAG('m', 'e', 'd', 'i'), MediProperty }, + { FT_MAKE_TAG('i', 'n', 'i', 't'), InitProperty }, + { FT_MAKE_TAG('r', 'l', 'i', 'g'), RligProperty }, + { FT_MAKE_TAG('c', 'a', 'l', 't'), CaltProperty }, + { FT_MAKE_TAG('l', 'i', 'g', 'a'), LigaProperty }, + { FT_MAKE_TAG('d', 'l', 'i', 'g'), DligProperty }, + { FT_MAKE_TAG('c', 's', 'w', 'h'), CswhProperty }, + // mset is used in old Win95 fonts that don't have a 'mark' positioning table. + { FT_MAKE_TAG('m', 's', 'e', 't'), MsetProperty }, + {0, 0} +}; + +static const QOpenType::Features syriac_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('i', 's', 'o', 'l'), IsolProperty }, + { FT_MAKE_TAG('f', 'i', 'n', 'a'), FinaProperty }, + { FT_MAKE_TAG('f', 'i', 'n', '2'), FinaProperty }, + { FT_MAKE_TAG('f', 'i', 'n', '3'), FinaProperty }, + { FT_MAKE_TAG('m', 'e', 'd', 'i'), MediProperty }, + { FT_MAKE_TAG('m', 'e', 'd', '2'), MediProperty }, + { FT_MAKE_TAG('i', 'n', 'i', 't'), InitProperty }, + { FT_MAKE_TAG('r', 'l', 'i', 'g'), RligProperty }, + { FT_MAKE_TAG('c', 'a', 'l', 't'), CaltProperty }, + { FT_MAKE_TAG('l', 'i', 'g', 'a'), LigaProperty }, + { FT_MAKE_TAG('d', 'l', 'i', 'g'), DligProperty }, + {0, 0} +}; + +static bool arabicSyriacOpenTypeShape(QOpenType *openType, QShaperItem *item, bool *ot_ok) +{ + *ot_ok = true; + + openType->selectScript(item->script, item->script == QFont::Arabic ? arabic_features : syriac_features); + int nglyphs = item->num_glyphs; + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + heuristicSetGlyphAttributes(item); + + unsigned short *logClusters = item->log_clusters; + const unsigned short *uc = (const unsigned short *)item->string->unicode() + item->from; + + QVarLengthArray<QArabicProperties> props(item->length+2); + QArabicProperties *properties = props.data(); + int f = 0; + int l = item->length; + if (item->from > 0) { + --f; + ++l; + ++properties; + } + if (f + l < (int)item->string->length()) { + ++l; + } + getArabicProperties((const unsigned short *)(uc+f), l, props.data()); + + QVarLengthArray<uint> apply(item->num_glyphs); + + + // Hack to remove ZWJ and ZWNJ from rendered output. + int j = 0; + for (int i = 0; i < item->num_glyphs; i++) { + if (uc[i] == 0x200c || uc[i] == 0x200d) + continue; + item->glyphs[j] = item->glyphs[i]; + item->attributes[j] = item->attributes[i]; + item->advances[j] = item->advances[i]; + item->offsets[j] = item->offsets[i]; + properties[j] = properties[i]; + item->attributes[j].justification = properties[i].justification; + logClusters[i] = logClusters[j]; + ++j; + } + item->num_glyphs = j; + + for (int i = 0; i < item->num_glyphs; i++) { + apply[i] = 0; + + if (properties[i].shape == XIsolated) + apply[i] |= MediProperty|FinaProperty|InitProperty; + else if (properties[i].shape == XMedial) + apply[i] |= IsolProperty|FinaProperty|InitProperty; + else if (properties[i].shape == XFinal) + apply[i] |= IsolProperty|MediProperty|InitProperty; + else if (properties[i].shape == XInitial) + apply[i] |= IsolProperty|MediProperty|FinaProperty; + } + + if (!openType->shape(item, apply.data())) { + *ot_ok = false; + return false; + } + item->num_glyphs = nglyphs; + return openType->positionAndAdd(item); +} + +#endif + +// #### stil missing: identify invalid character combinations +static bool arabic_shape(QShaperItem *item) +{ + Q_ASSERT(item->script == QFont::Arabic); + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + QOpenType *openType = item->font->openType(); + + if (openType && openType->supportsScript(QFont::Arabic)) { + bool ot_ok; + if (arabicSyriacOpenTypeShape(openType, item, &ot_ok)) + return true; + if (ot_ok) + return false; + // fall through to the non OT code + } +#endif + + QVarLengthArray<ushort> shapedChars(item->length); + + int slen; + shapedString(item->string, item->from, item->length, (QChar *)shapedChars.data(), &slen, + item->flags & QTextEngine::RightToLeft, + item->attributes, item->log_clusters); + + if (item->font->stringToCMap((QChar *)shapedChars.data(), slen, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + for (int i = 0; i < slen; ++i) + if (item->attributes[i].mark) + item->advances[i] = 0; + qt_heuristicPosition(item); + return TRUE; +} + +#if defined(Q_WS_X11) +# include "qscriptengine_x11.cpp" +#elif defined(Q_WS_WIN) +# include "qscriptengine_win.cpp" +#elif defined(Q_WS_MAC) +# include "qscriptengine_mac.cpp" +#elif defined(Q_WS_QWS) +# include "qscriptengine_qws.cpp" +#endif diff --git a/src/kernel/qscriptengine_p.h b/src/kernel/qscriptengine_p.h new file mode 100644 index 0000000..ce3dbc0 --- /dev/null +++ b/src/kernel/qscriptengine_p.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the $MODULE$ of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +****************************************************************************/ + +#ifndef QSCRIPTENGINE_P_H +#define QSCRIPTENGINE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qtextengine_p.h" + +class QString; +struct QCharAttributes; + +struct QShaperItem { + int script; + const QString *string; + int from; + int length; + QFontEngine *font; + glyph_t *glyphs; + advance_t *advances; + qoffset_t *offsets; + GlyphAttributes *attributes; + int num_glyphs; // in: available glyphs out: glyphs used/needed + unsigned short *log_clusters; + int flags; + bool has_positioning; +}; + +// return true if ok. +typedef bool (*ShapeFunction)(QShaperItem *item); +typedef void (*AttributeFunction)(int script, const QString &, int, int, QCharAttributes *); + +struct q_scriptEngine { + ShapeFunction shape; + AttributeFunction charAttributes; +}; + +extern const q_scriptEngine scriptEngines[]; + +#endif // QSCRIPTENGINE_P_H diff --git a/src/kernel/qscriptengine_x11.cpp b/src/kernel/qscriptengine_x11.cpp new file mode 100644 index 0000000..7d2b77d --- /dev/null +++ b/src/kernel/qscriptengine_x11.cpp @@ -0,0 +1,3752 @@ +/**************************************************************************** +** +** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +// ------------------------------------------------------------------------------------------------------------------ +// +// Continuation of middle eastern languages +// +// ------------------------------------------------------------------------------------------------------------------ + +// #### stil missing: identify invalid character combinations +static bool syriac_shape(QShaperItem *item) +{ + Q_ASSERT(item->script == QFont::Syriac); + +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + if (openType && openType->supportsScript(QFont::Syriac)) { + bool ot_ok; + if (arabicSyriacOpenTypeShape(openType, item, &ot_ok)) + return true; + if (ot_ok) + return false; + // fall through to the non OT code + } +#endif + return basic_shape(item); +} + + +static bool thaana_shape(QShaperItem *item) +{ + Q_ASSERT(item->script == QFont::Thaana); + +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + + if (openType && openType->supportsScript(item->script)) { + openType->selectScript(QFont::Thaana); + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + heuristicSetGlyphAttributes(item); + openType->shape(item); + return openType->positionAndAdd(item); + } +#endif + return basic_shape(item); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Indic languages +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +enum Form { + Invalid = 0x0, + Unknown = Invalid, + Consonant, + Nukta, + Halant, + Matra, + VowelMark, + StressMark, + IndependentVowel, + LengthMark, + Control, + Other +}; + +static const unsigned char indicForms[0xe00-0x900] = { + // Devangari + Invalid, VowelMark, VowelMark, VowelMark, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Matra, Matra, Matra, + Matra, Matra, Matra, Matra, + Matra, Halant, Unknown, Unknown, + + Other, StressMark, StressMark, StressMark, + StressMark, Unknown, Unknown, Unknown, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Consonant, + Consonant, Consonant /* ??? */, Consonant, Consonant, + + // Bengali + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, Invalid, IndependentVowel, + + IndependentVowel, Invalid, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Invalid, + Invalid, Invalid, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Halant, Consonant, Unknown, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, VowelMark, + Invalid, Invalid, Invalid, Invalid, + Consonant, Consonant, Invalid, Consonant, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Consonant, Consonant, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Gurmukhi + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, Invalid, + Invalid, Invalid, Invalid, IndependentVowel, + + IndependentVowel, Invalid, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Consonant, + Invalid, Consonant, Consonant, Invalid, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Invalid, + Invalid, Invalid, Invalid, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Halant, Unknown, Unknown, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Unknown, Unknown, Unknown, + Invalid, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Invalid, + + Other, Other, Invalid, Invalid, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + StressMark, StressMark, Consonant, Consonant, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Gujarati + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, Invalid, IndependentVowel, + + IndependentVowel, IndependentVowel, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Matra, Invalid, Matra, + Matra, Matra, Invalid, Matra, + Matra, Halant, Unknown, Unknown, + + Other, Unknown, Unknown, Unknown, + Unknown, Unknown, Unknown, Unknown, + Unknown, Unknown, Unknown, Unknown, + Unknown, Unknown, Unknown, Unknown, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Oriya + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, Invalid, IndependentVowel, + + IndependentVowel, Invalid, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Invalid, Invalid, Invalid, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Halant, Unknown, Unknown, + + Other, Invalid, Invalid, Invalid, + Invalid, Unknown, LengthMark, LengthMark, + Invalid, Invalid, Invalid, Invalid, + Consonant, Consonant, Invalid, Consonant, + + IndependentVowel, IndependentVowel, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Consonant, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + //Tamil + Invalid, Invalid, VowelMark, Other, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, Invalid, + Invalid, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Invalid, Invalid, + Invalid, Consonant, Consonant, Invalid, + Consonant, Invalid, Consonant, Consonant, + + Invalid, Invalid, Invalid, Consonant, + Consonant, Invalid, Invalid, Invalid, + Consonant, Consonant, Consonant, Invalid, + Invalid, Invalid, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Invalid, Invalid, Matra, Matra, + + Matra, Matra, Matra, Invalid, + Invalid, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, LengthMark, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Telugu + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Invalid, Invalid, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, LengthMark, Matra, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + IndependentVowel, IndependentVowel, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Kannada + Invalid, Invalid, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, LengthMark, LengthMark, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Consonant, Invalid, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Malayalam + Invalid, Invalid, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Invalid, Invalid, Matra, Matra, + + Matra, Matra, Matra, Matra, + Invalid, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, LengthMark, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + IndependentVowel, IndependentVowel, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Sinhala + Invalid, Invalid, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, Invalid, + Invalid, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Invalid, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Invalid, Consonant, Invalid, Invalid, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Invalid, + Invalid, Invalid, Halant, Invalid, + Invalid, Invalid, Invalid, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Matra, Invalid, + Matra, Matra, Matra, Matra, + Matra, Matra, Matra, Matra, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + Invalid, Invalid, Matra, Matra, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, +}; + +enum Position { + None, + Pre, + Above, + Below, + Post, + Split, + Base, + Reph, + Vattu, + Inherit +}; + +static const unsigned char indicPosition[0xe00-0x900] = { + // Devanagari + None, Above, Above, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + Below, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, Post, Pre, + + Post, Below, Below, Below, + Below, Above, Above, Above, + Above, Post, Post, Post, + Post, None, None, None, + + None, Above, Below, Above, + Above, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Bengali + None, Above, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + Below, None, None, Post, + + Below, None, None, None, + None, None, None, None, + None, None, None, None, + Below, None, Post, Pre, + + Post, Below, Below, Below, + Below, None, None, Pre, + Pre, None, None, Split, + Split, Below, None, None, + + None, None, None, None, + None, None, None, Post, + None, None, None, None, + None, None, None, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Gurmukhi + None, Above, Above, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, Post, + + Below, None, None, None, + None, Below, None, None, + None, Below, None, None, + Below, None, Post, Pre, + + Post, Below, Below, None, + None, None, None, Above, + Above, None, None, Above, + Above, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + Above, Above, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Gujarati + None, Above, Above, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + Below, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, Post, Pre, + + Post, Below, Below, Below, + Below, Above, None, Above, + Above, Post, None, Post, + Post, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Oriya + None, Above, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + Below, None, None, None, + Below, None, None, None, + Below, Below, Below, Post, + + Below, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, Post, Above, + + Post, Below, Below, Below, + None, None, None, Pre, + Split, None, None, Split, + Split, None, None, None, + + None, None, None, None, + None, None, Above, Post, + None, None, None, None, + None, None, None, Post, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, Below, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Tamil + None, None, Above, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, Post, Post, + + Above, Below, Below, None, + None, None, Pre, Pre, + Pre, None, Split, Split, + Split, Halant, None, None, + + None, None, None, None, + None, None, None, Post, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Telugu + None, Post, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + + Below, Below, Below, Below, + Below, Below, Below, Below, + Below, None, Below, Below, + Below, Below, Below, Below, + + Below, None, Below, Below, + None, Below, Below, Below, + Below, Below, None, None, + None, None, Post, Above, + + Above, Post, Post, Post, + Post, None, Above, Above, + Split, None, Post, Above, + Above, Halant, None, None, + + None, None, None, None, + None, Above, Below, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Kannada + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + + Below, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + + Below, None, Below, Below, + None, Below, Below, Below, + Below, Below, None, None, + None, None, Post, Above, + + Split, Post, Post, Post, + Post, None, Above, Split, + Split, None, Split, Split, + Above, Halant, None, None, + + None, None, None, None, + None, Post, Post, None, + None, None, None, None, + None, None, Below, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Malayalam + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, Post, + + Post, None, Below, None, + None, Post, None, None, + None, None, None, None, + None, None, Post, Post, + + Post, Post, Post, Post, + None, None, Pre, Pre, + Pre, None, Split, Split, + Split, Halant, None, None, + + None, None, None, None, + None, None, None, Post, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Sinhala + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, Post, + + Post, Post, Above, Above, + Below, None, Below, None, + Post, Pre, Split, Pre, + Split, Split, Split, Post, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None +}; + +static inline Form form(unsigned short uc) { + if (uc < 0x900 || uc > 0xdff) { + if (uc == 0x25cc) + return Consonant; + if (uc == 0x200c || uc == 0x200d) + return Control; + return Other; + } + return (Form)indicForms[uc-0x900]; +} + +static inline Position indic_position(unsigned short uc) { + if (uc < 0x900 || uc > 0xdff) + return None; + return (Position) indicPosition[uc-0x900]; +} + + +enum IndicScriptProperties { + HasReph = 0x01, + HasSplit = 0x02 +}; + +const uchar scriptProperties[10] = { + // Devanagari, + HasReph, + // Bengali, + HasReph|HasSplit, + // Gurmukhi, + 0, + // Gujarati, + HasReph, + // Oriya, + HasReph|HasSplit, + // Tamil, + HasSplit, + // Telugu, + HasSplit, + // Kannada, + HasSplit|HasReph, + // Malayalam, + HasSplit, + // Sinhala, + HasSplit +}; + +struct IndicOrdering { + Form form; + Position position; +}; + +static const IndicOrdering devanagari_order [] = { + { Consonant, Below }, + { Matra, Below }, + { VowelMark, Below }, + { StressMark, Below }, + { Matra, Above }, + { Matra, Post }, + { Consonant, Reph }, + { VowelMark, Above }, + { StressMark, Above }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering bengali_order [] = { + { Consonant, Below }, + { Matra, Below }, + { Matra, Above }, + { Consonant, Reph }, + { VowelMark, Above }, + { Consonant, Post }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering gurmukhi_order [] = { + { Consonant, Below }, + { Matra, Below }, + { Matra, Above }, + { Consonant, Post }, + { Matra, Post }, + { VowelMark, Above }, + { (Form)0, None } +}; + +static const IndicOrdering tamil_order [] = { + { Matra, Above }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering telugu_order [] = { + { Matra, Above }, + { Matra, Below }, + { Matra, Post }, + { Consonant, Below }, + { Consonant, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering kannada_order [] = { + { Matra, Above }, + { Matra, Post }, + { Consonant, Below }, + { Consonant, Post }, + { LengthMark, Post }, + { Consonant, Reph }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering malayalam_order [] = { + { Consonant, Below }, + { Matra, Below }, + { Consonant, Reph }, + { Consonant, Post }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering sinhala_order [] = { + { Matra, Below }, + { Matra, Above }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering * const indic_order[] = { + devanagari_order, // Devanagari + bengali_order, // Bengali + gurmukhi_order, // Gurmukhi + devanagari_order, // Gujarati + bengali_order, // Oriya + tamil_order, // Tamil + telugu_order, // Telugu + kannada_order, // Kannada + malayalam_order, // Malayalam + sinhala_order // Sinhala +}; + + + +// vowel matras that have to be split into two parts. +static const unsigned short split_matras[] = { + // matra, split1, split2 + + // bengalis + 0x9cb, 0x9c7, 0x9be, + 0x9cc, 0x9c7, 0x9d7, + // oriya + 0xb48, 0xb47, 0xb56, + 0xb4b, 0xb47, 0xb3e, + 0xb4c, 0xb47, 0xb57, + // tamil + 0xbca, 0xbc6, 0xbbe, + 0xbcb, 0xbc7, 0xbbe, + 0xbcc, 0xbc6, 0xbd7, + // telugu + 0xc48, 0xc46, 0xc56, + // kannada + 0xcc0, 0xcbf, 0xcd5, + 0xcc7, 0xcc6, 0xcd5, + 0xcc8, 0xcc6, 0xcd6, + 0xcca, 0xcc6, 0xcc2, + 0xccb, 0xcca, 0xcd5, + // malayalam + 0xd4a, 0xd46, 0xd3e, + 0xd4b, 0xd47, 0xd3e, + 0xd4c, 0xd46, 0xd57, + // sinhala + 0xdda, 0xdd9, 0xdca, + 0xddc, 0xdd9, 0xdcf, + 0xddd, 0xddc, 0xdca, + 0xdde, 0xdd9, 0xddf, + 0xffff +}; + +static inline void splitMatra(unsigned short *reordered, int matra, int &len, int &base) +{ + unsigned short matra_uc = reordered[matra]; + //qDebug("matra=%d, reordered[matra]=%x", matra, reordered[matra]); + + const unsigned short *split = split_matras; + while (split[0] < matra_uc) + split += 3; + + assert(*split == matra_uc); + ++split; + + if (indic_position(*split) == Pre) { + reordered[matra] = split[1]; + memmove(reordered + 1, reordered, len*sizeof(unsigned short)); + reordered[0] = split[0]; + base++; + } else { + memmove(reordered + matra + 1, reordered + matra, (len-matra)*sizeof(unsigned short)); + reordered[matra] = split[0]; + reordered[matra+1] = split[1]; + } + len++; +} + +enum IndicProperties { + // these two are already defined +// CcmpProperty = 0x1, +// InitProperty = 0x2, + NuktaProperty = 0x4, + AkhantProperty = 0x8, + RephProperty = 0x10, + PreFormProperty = 0x20, + BelowFormProperty = 0x40, + AboveFormProperty = 0x80, + HalfFormProperty = 0x100, + PostFormProperty = 0x200, + VattuProperty = 0x400, + PreSubstProperty = 0x800, + BelowSubstProperty = 0x1000, + AboveSubstProperty = 0x2000, + PostSubstProperty = 0x4000, + HalantProperty = 0x8000, + CligProperty = 0x10000 +}; + +#ifndef QT_NO_XFTFREETYPE +static const QOpenType::Features indic_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('i', 'n', 'i', 't'), InitProperty }, + { FT_MAKE_TAG('n', 'u', 'k', 't'), NuktaProperty }, + { FT_MAKE_TAG('a', 'k', 'h', 'n'), AkhantProperty }, + { FT_MAKE_TAG('r', 'p', 'h', 'f'), RephProperty }, + { FT_MAKE_TAG('b', 'l', 'w', 'f'), BelowFormProperty }, + { FT_MAKE_TAG('h', 'a', 'l', 'f'), HalfFormProperty }, + { FT_MAKE_TAG('p', 's', 't', 'f'), PostFormProperty }, + { FT_MAKE_TAG('v', 'a', 't', 'u'), VattuProperty }, + { FT_MAKE_TAG('p', 'r', 'e', 's'), PreSubstProperty }, + { FT_MAKE_TAG('b', 'l', 'w', 's'), BelowSubstProperty }, + { FT_MAKE_TAG('a', 'b', 'v', 's'), AboveSubstProperty }, + { FT_MAKE_TAG('p', 's', 't', 's'), PostSubstProperty }, + { FT_MAKE_TAG('h', 'a', 'l', 'n'), HalantProperty }, + { 0, 0 } +}; +#endif + +// #define INDIC_DEBUG +#ifdef INDIC_DEBUG +#define IDEBUG qDebug +#else +#define IDEBUG if(0) qDebug +#endif + +#ifdef INDIC_DEBUG +static QString propertiesToString(int properties) +{ + QString res; + properties = ~properties; + if (properties & CcmpProperty) + res += "Ccmp "; + if (properties & InitProperty) + res += "Init "; + if (properties & NuktaProperty) + res += "Nukta "; + if (properties & AkhantProperty) + res += "Akhant "; + if (properties & RephProperty) + res += "Reph "; + if (properties & PreFormProperty) + res += "PreForm "; + if (properties & BelowFormProperty) + res += "BelowForm "; + if (properties & AboveFormProperty) + res += "AboveForm "; + if (properties & HalfFormProperty) + res += "HalfForm "; + if (properties & PostFormProperty) + res += "PostForm "; + if (properties & VattuProperty) + res += "Vattu "; + if (properties & PreSubstProperty) + res += "PreSubst "; + if (properties & BelowSubstProperty) + res += "BelowSubst "; + if (properties & AboveSubstProperty) + res += "AboveSubst "; + if (properties & PostSubstProperty) + res += "PostSubst "; + if (properties & HalantProperty) + res += "Halant "; + if (properties & CligProperty) + res += "Clig "; + return res; +} +#endif + +static bool indic_shape_syllable(QOpenType *openType, QShaperItem *item, bool invalid) +{ + Q_UNUSED(openType) + int script = item->script; + Q_ASSERT(script >= QFont::Devanagari && script <= QFont::Sinhala); + const unsigned short script_base = 0x0900 + 0x80*(script-QFont::Devanagari); + const unsigned short ra = script_base + 0x30; + const unsigned short halant = script_base + 0x4d; + const unsigned short nukta = script_base + 0x3c; + + int len = item->length; + IDEBUG(">>>>> indic shape: from=%d, len=%d invalid=%d", item->from, item->length, invalid); + + if (item->num_glyphs < len+4) { + item->num_glyphs = len+4; + return FALSE; + } + + QVarLengthArray<unsigned short> reordered(len+4); + QVarLengthArray<unsigned char> position(len+4); + + unsigned char properties = scriptProperties[script-QFont::Devanagari]; + + if (invalid) { + *reordered.data() = 0x25cc; + memcpy(reordered.data()+1, item->string->unicode() + item->from, len*sizeof(QChar)); + len++; + } else { + memcpy(reordered.data(), item->string->unicode() + item->from, len*sizeof(QChar)); + } + if (reordered[len-1] == 0x200c) // zero width non joiner + len--; + + int i; + int base = 0; + int reph = -1; + +#ifdef INDIC_DEBUG + IDEBUG("original:"); + for (i = 0; i < len; i++) { + IDEBUG(" %d: %4x", i, reordered[i]); + } +#endif + + if (len != 1) { + unsigned short *uc = reordered.data(); + bool beginsWithRa = FALSE; + + // Rule 1: find base consonant + // + // The shaping engine finds the base consonant of the + // syllable, using the following algorithm: starting from the + // end of the syllable, move backwards until a consonant is + // found that does not have a below-base or post-base form + // (post-base forms have to follow below-base forms), or + // arrive at the first consonant. The consonant stopped at + // will be the base. + // + // * If the syllable starts with Ra + H (in a script that has + // 'Reph'), Ra is excluded from candidates for base + // consonants. + // + // * In Kannada and Telugu, the base consonant cannot be + // farther than 3 consonants from the end of the syllable. + // #### replace the HasReph property by testing if the feature exists in the font! + if (form(*uc) == Consonant || (script == QFont::Bengali && form(*uc) == IndependentVowel)) { + beginsWithRa = (properties & HasReph) && ((len > 2) && *uc == ra && *(uc+1) == halant); + + if (beginsWithRa && form(*(uc+2)) == Control) + beginsWithRa = FALSE; + + base = (beginsWithRa ? 2 : 0); + IDEBUG(" length = %d, beginsWithRa = %d, base=%d", len, beginsWithRa, base); + + int lastConsonant = 0; + int matra = -1; + // we remember: + // * the last consonant since we need it for rule 2 + // * the matras position for rule 3 and 4 + + // figure out possible base glyphs + memset(position.data(), 0, len); + if (script == QFont::Devanagari || script == QFont::Gujarati) { + bool vattu = FALSE; + for (i = base; i < len; ++i) { + position[i] = form(uc[i]); + if (position[i] == Consonant) { + lastConsonant = i; + vattu = (!vattu && uc[i] == ra); + if (vattu) { + IDEBUG("excluding vattu glyph at %d from base candidates", i); + position[i] = Vattu; + } + } else if (position[i] == Matra) { + matra = i; + } + } + } else { + for (i = base; i < len; ++i) { + position[i] = form(uc[i]); + if (position[i] == Consonant) + lastConsonant = i; + else if (matra < 0 && position[i] == Matra) + matra = i; + } + } + int skipped = 0; + Position pos = Post; + for (i = len-1; i > base; i--) { + if (position[i] != Consonant && (position[i] != Control || script == QFont::Kannada)) + continue; + + Position charPosition = indic_position(uc[i]); + if (pos == Post && charPosition == Post) { + pos = Post; + } else if ((pos == Post || pos == Below) && charPosition == Below) { + if (script == QFont::Devanagari || script == QFont::Gujarati) + base = i; + pos = Below; + } else { + base = i; + break; + } + if (skipped == 2 && (script == QFont::Kannada || script == QFont::Telugu)) { + base = i; + break; + } + ++skipped; + } + + IDEBUG(" base consonant at %d skipped=%d, lastConsonant=%d", base, skipped, lastConsonant); + + // Rule 2: + // + // If the base consonant is not the last one, Uniscribe + // moves the halant from the base consonant to the last + // one. + if (lastConsonant > base) { + int halantPos = 0; + if (uc[base+1] == halant) + halantPos = base + 1; + else if (uc[base+1] == nukta && uc[base+2] == halant) + halantPos = base + 2; + if (halantPos > 0) { + IDEBUG(" moving halant from %d to %d!", base+1, lastConsonant); + for (i = halantPos; i < lastConsonant; i++) + uc[i] = uc[i+1]; + uc[lastConsonant] = halant; + } + } + + // Rule 3: + // + // If the syllable starts with Ra + H, Uniscribe moves + // this combination so that it follows either: + + // * the post-base 'matra' (if any) or the base consonant + // (in scripts that show similarity to Devanagari, i.e., + // Devanagari, Gujarati, Bengali) + // * the base consonant (other scripts) + // * the end of the syllable (Kannada) + + Position matra_position = None; + if (matra > 0) + matra_position = indic_position(uc[matra]); + IDEBUG(" matra at %d with form %d, base=%d", matra, matra_position, base); + + if (beginsWithRa && base != 0) { + int toPos = base+1; + if (toPos < len && uc[toPos] == nukta) + toPos++; + if (toPos < len && uc[toPos] == halant) + toPos++; + if (toPos < len && uc[toPos] == 0x200d) + toPos++; + if (toPos < len-1 && uc[toPos] == ra && uc[toPos+1] == halant) + toPos += 2; + if (script == QFont::Devanagari || script == QFont::Gujarati || script == QFont::Bengali) { + if (matra_position == Post || matra_position == Split) { + toPos = matra+1; + matra -= 2; + } + } else if (script == QFont::Kannada) { + toPos = len; + matra -= 2; + } + + IDEBUG("moving leading ra+halant to position %d", toPos); + for (i = 2; i < toPos; i++) + uc[i-2] = uc[i]; + uc[toPos-2] = ra; + uc[toPos-1] = halant; + base -= 2; + if (properties & HasReph) + reph = toPos-2; + } + + // Rule 4: + + // Uniscribe splits two- or three-part matras into their + // parts. This splitting is a character-to-character + // operation). + // + // Uniscribe describes some moving operations for these + // matras here. For shaping however all pre matras need + // to be at the begining of the syllable, so we just move + // them there now. + if (matra_position == Split) { + splitMatra(uc, matra, len, base); + // Handle three-part matras (0xccb in Kannada) + matra_position = indic_position(uc[matra]); + if (matra_position == Split) + splitMatra(uc, matra, len, base); + } else if (matra_position == Pre) { + unsigned short m = uc[matra]; + while (matra--) + uc[matra+1] = uc[matra]; + uc[0] = m; + base++; + } + } + + // Rule 5: + // + // Uniscribe classifies consonants and 'matra' parts as + // pre-base, above-base (Reph), below-base or post-base. This + // classification exists on the character code level and is + // language-dependent, not font-dependent. + for (i = 0; i < base; ++i) + position[i] = Pre; + position[base] = Base; + for (i = base+1; i < len; ++i) { + position[i] = indic_position(uc[i]); + // #### replace by adjusting table + if (uc[i] == nukta || uc[i] == halant) + position[i] = Inherit; + } + if (reph > 0) { + // recalculate reph, it might have changed. + for (i = base+1; i < len; ++i) + if (uc[i] == ra) + reph = i; + position[reph] = Reph; + position[reph+1] = Inherit; + } + + // all reordering happens now to the chars after the base + int fixed = base+1; + if (fixed < len && uc[fixed] == nukta) + fixed++; + if (fixed < len && uc[fixed] == halant) + fixed++; + if (fixed < len && uc[fixed] == 0x200d) + fixed++; + +#ifdef INDIC_DEBUG + for (i = fixed; i < len; ++i) + IDEBUG("position[%d] = %d, form=%d", i, position[i], form(uc[i])); +#endif + // we continuosly position the matras and vowel marks and increase the fixed + // until we reached the end. + const IndicOrdering *finalOrder = indic_order[script-QFont::Devanagari]; + + IDEBUG(" reordering pass:"); + //IDEBUG(" base=%d fixed=%d", base, fixed); + int toMove = 0; + while (finalOrder[toMove].form && fixed < len-1) { + //IDEBUG(" fixed = %d, moving form %d with pos %d", fixed, finalOrder[toMove].form, finalOrder[toMove].position); + for (i = fixed; i < len; i++) { + if (form(uc[i]) == finalOrder[toMove].form && + position[i] == finalOrder[toMove].position) { + // need to move this glyph + int to = fixed; + if (i < len-1 && position[i+1] == Inherit) { + IDEBUG(" moving two chars from %d to %d", i, to); + unsigned short ch = uc[i]; + unsigned short ch2 = uc[i+1]; + unsigned char pos = position[i]; + for (int j = i+1; j > to+1; j--) { + uc[j] = uc[j-2]; + position[j] = position[j-2]; + } + uc[to] = ch; + uc[to+1] = ch2; + position[to] = pos; + position[to+1] = pos; + fixed += 2; + } else { + IDEBUG(" moving one char from %d to %d", i, to); + unsigned short ch = uc[i]; + unsigned char pos = position[i]; + for (int j = i; j > to; j--) { + uc[j] = uc[j-1]; + position[j] = position[j-1]; + } + uc[to] = ch; + position[to] = pos; + fixed++; + } + } + } + toMove++; + } + + } + + if (reph > 0) { + // recalculate reph, it might have changed. + for (i = base+1; i < len; ++i) + if (reordered[i] == ra) + reph = i; + } + + if (item->font->stringToCMap((const QChar *)reordered.data(), len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + + IDEBUG(" base=%d, reph=%d", base, reph); + IDEBUG("reordered:"); + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + IDEBUG(" %d: %4x", i, reordered[i]); + } + + // now we have the syllable in the right order, and can start running it through open type. + + bool control = FALSE; + for (i = 0; i < len; ++i) + control |= (form(reordered[i]) == Control); + +#ifndef QT_NO_XFTFREETYPE + if (openType) { + + // we need to keep track of where the base glyph is for some + // scripts and use the cluster feature for this. This + // also means we have to correct the logCluster output from + // the open type engine manually afterwards. for indic this + // is rather simple, as all chars just point to the first + // glyph in the syllable. + QVarLengthArray<unsigned short> clusters(len); + QVarLengthArray<unsigned int> properties(len); + + for (i = 0; i < len; ++i) + clusters[i] = i; + + // features we should always apply + for (i = 0; i < len; ++i) + properties[i] = ~(CcmpProperty + | NuktaProperty + | VattuProperty + | PreSubstProperty + | BelowSubstProperty + | AboveSubstProperty + | HalantProperty + | PositioningProperties); + + // Ccmp always applies + // Init + if (item->from == 0 + || !(item->string->unicode()[item->from-1].isLetter() || item->string->unicode()[item->from-1].isMark())) + properties[0] &= ~InitProperty; + + // Nukta always applies + // Akhant + for (i = 0; i <= base; ++i) + properties[i] &= ~AkhantProperty; + // Reph + if (reph >= 0) { + properties[reph] &= ~RephProperty; + properties[reph+1] &= ~RephProperty; + } + // BelowForm + for (i = base+1; i < len; ++i) + properties[i] &= ~BelowFormProperty; + + if (script == QFont::Devanagari || script == QFont::Gujarati) { + // vattu glyphs need this aswell + bool vattu = FALSE; + for (i = base-2; i > 1; --i) { + if (form(reordered[i]) == Consonant) { + vattu = (!vattu && reordered[i] == ra); + if (vattu) { + IDEBUG("forming vattu ligature at %d", i); + properties[i] &= ~BelowFormProperty; + properties[i+1] &= ~BelowFormProperty; + } + } + } + } + // HalfFormProperty + for (i = 0; i < base; ++i) + properties[i] &= ~HalfFormProperty; + if (control) { + for (i = 2; i < len; ++i) { + if (reordered[i] == 0x200d /* ZWJ */) { + properties[i-1] &= ~HalfFormProperty; + properties[i-2] &= ~HalfFormProperty; + } else if (reordered[i] == 0x200c /* ZWNJ */) { + properties[i-1] &= ~HalfFormProperty; + properties[i-2] &= ~HalfFormProperty; + } + } + } + // PostFormProperty + for (i = base+1; i < len; ++i) + properties[i] &= ~PostFormProperty; + // vattu always applies + // pres always applies + // blws always applies + // abvs always applies + + // psts + // ### this looks slightly different from before, but I believe it's correct + if (reordered[len-1] != halant || base != len-2) + properties[base] &= ~PostSubstProperty; + for (i = base+1; i < len; ++i) + properties[i] &= ~PostSubstProperty; + + // halant always applies + +#ifdef INDIC_DEBUG + { + IDEBUG("OT properties:"); + for (int i = 0; i < len; ++i) + qDebug(" i: %s", ::propertiesToString(properties[i]).toLatin1().data()); + } +#endif + + // initialize + item->log_clusters = clusters.data(); + openType->shape(item, properties.data()); + + int newLen = openType->len(); + OTL_GlyphItem otl_glyphs = openType->glyphs(); + + // move the left matra back to it's correct position in malayalam and tamil + if ((script == QFont::Malayalam || script == QFont::Tamil) && (form(reordered[0]) == Matra)) { +// qDebug("reordering matra, len=%d", newLen); + // need to find the base in the shaped string and move the matra there + int basePos = 0; + while (basePos < newLen && (int)otl_glyphs[basePos].cluster <= base) + basePos++; + --basePos; + if (basePos < newLen && basePos > 1) { +// qDebug("moving prebase matra to position %d in syllable newlen=%d", basePos, newLen); + OTL_GlyphItemRec m = otl_glyphs[0]; + --basePos; + for (i = 0; i < basePos; ++i) + otl_glyphs[i] = otl_glyphs[i+1]; + otl_glyphs[basePos] = m; + } + } + + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + + if (control) { + IDEBUG("found a control char in the syllable"); + int i = 0, j = 0; + while (i < item->num_glyphs) { + if (form(reordered[otl_glyphs[i].cluster]) == Control) { + ++i; + if (i >= item->num_glyphs) + break; + } + item->glyphs[j] = item->glyphs[i]; + ++i; + ++j; + } + item->num_glyphs = j; + } + + } +#endif + + item->attributes[0].clusterStart = TRUE; + IDEBUG("<<<<<<"); + return TRUE; +} + + +/* syllables are of the form: + + (Consonant Nukta? Halant)* Consonant Matra? VowelMark? StressMark? + (Consonant Nukta? Halant)* Consonant Halant + IndependentVowel VowelMark? StressMark? + + We return syllable boundaries on invalid combinations aswell +*/ +static int indic_nextSyllableBoundary(int script, const QString &s, int start, int end, bool *invalid) +{ + *invalid = FALSE; + IDEBUG("indic_nextSyllableBoundary: start=%d, end=%d", start, end); + const QChar *uc = s.unicode()+start; + + int pos = 0; + Form state = form(uc[pos].unicode()); + IDEBUG("state[%d]=%d (uc=%4x)", pos, state, uc[pos].unicode()); + pos++; + + if (state != Consonant && state != IndependentVowel) { + if (state != Other) + *invalid = TRUE; + goto finish; + } + + while (pos < end - start) { + Form newState = form(uc[pos].unicode()); + IDEBUG("state[%d]=%d (uc=%4x)", pos, newState, uc[pos].unicode()); + switch(newState) { + case Control: + newState = state; + if (state == Halant && uc[pos].unicode() == 0x200d /* ZWJ */) + break; + // the control character should be the last char in the item + ++pos; + goto finish; + case Consonant: + if (state == Halant && (script != QFont::Sinhala || uc[pos-1].unicode() == 0x200d /* ZWJ */)) + break; + goto finish; + case Halant: + if (state == Nukta || state == Consonant) + break; + // Bengali has a special exception allowing the combination Vowel_A/E + Halant + Ya + if (script == QFont::Bengali && pos == 1 && + (uc[0].unicode() == 0x0985 || uc[0].unicode() == 0x098f)) + break; + goto finish; + case Nukta: + if (state == Consonant) + break; + goto finish; + case StressMark: + if (state == VowelMark) + break; + // fall through + case VowelMark: + if (state == Matra || state == IndependentVowel) + break; + // fall through + case Matra: + if (state == Consonant || state == Nukta) + break; + // ### not sure if this is correct. If it is, does it apply only to Bengali or should + // it work for all Indic languages? + // the combination Independent_A + Vowel Sign AA is allowed. + if (script == QFont::Bengali && uc[pos].unicode() == 0x9be && uc[pos-1].unicode() == 0x985) + break; + if (script == QFont::Tamil && state == Matra) { + if (uc[pos-1].unicode() == 0x0bc6 && + (uc[pos].unicode() == 0xbbe || uc[pos].unicode() == 0xbd7)) + break; + if (uc[pos-1].unicode() == 0x0bc7 && uc[pos].unicode() == 0xbbe) + break; + } + goto finish; + + case LengthMark: + case IndependentVowel: + case Invalid: + case Other: + goto finish; + } + state = newState; + pos++; + } + finish: + return pos+start; +} + +static bool indic_shape(QShaperItem *item) +{ + Q_ASSERT(item->script >= QFont::Devanagari && item->script <= QFont::Sinhala); + +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + if (openType) + openType->selectScript(item->script, indic_features); +#else + QOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + QShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + IDEBUG("indic_shape: from %d length %d", item->from, item->length); + while (sstart < end) { + bool invalid; + int send = indic_nextSyllableBoundary(item->script, *item->string, sstart, end, &invalid); + IDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!indic_shape_syllable(openType, &syllable, invalid)) { + IDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs); + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + IDEBUG("syllable:"); + int i; + for (i = first_glyph; i < first_glyph + syllable.num_glyphs; ++i) + IDEBUG(" %d -> glyph %x", i, item->glyphs[i]); + IDEBUG(" logclusters:"); + for (i = sstart; i < send; ++i) { + IDEBUG(" %d -> glyph %d", i, first_glyph); + logClusters[i-item->from] = first_glyph; + } + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + + +static void indic_attributes(int script, const QString &text, int from, int len, QCharAttributes *attributes) +{ + int end = from + len; + const QChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while (i < len) { + bool invalid; + int boundary = indic_nextSyllableBoundary(script, text, from+i, end, &invalid) - from; + attributes[i].charStop = TRUE; + + if (boundary > len-1) boundary = len; + i++; + while (i < boundary) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert(i == boundary); + } + + +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Thai and Lao +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +#include <qtextcodec.h> +#include <qlibrary.h> + + +static void thaiWordBreaks(const QChar *string, const int len, QCharAttributes *attributes) +{ +#ifndef QT_NO_TEXTCODEC + typedef int (*th_brk_def)(const char*, int[], int); + static QTextCodec *thaiCodec = QTextCodec::codecForMib(2259); + static th_brk_def th_brk = 0; + +#ifndef QT_NO_LIBRARY + /* load libthai dynamically */ + if (!th_brk && thaiCodec) { + th_brk = (th_brk_def)QLibrary::resolve("thai", "th_brk"); + if (!th_brk) + thaiCodec = 0; + } +#endif + + if (!th_brk) + return; + + QCString cstr = thaiCodec->fromUnicode(QConstString(string, len).string()); + + int brp[128]; + int *break_positions = brp; + int numbreaks = th_brk(cstr.data(), break_positions, 128); + if (numbreaks > 128) { + break_positions = new int[numbreaks]; + numbreaks = th_brk(cstr.data(),break_positions, numbreaks); + } + + attributes[0].softBreak = TRUE; + int i; + for (i = 1; i < len; ++i) + attributes[i].softBreak = FALSE; + + for (i = 0; i < numbreaks; ++i) + attributes[break_positions[i]].softBreak = TRUE; + + if (break_positions != brp) + delete [] break_positions; +#endif +} + + +static void thai_attributes( int script, const QString &text, int from, int len, QCharAttributes *attributes ) +{ + Q_UNUSED(script); + Q_ASSERT(script == QFont::Thai); + thaiWordBreaks(text.unicode() + from, len, attributes); +} + + + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Tibetan +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +// tibetan syllables are of the form: +// head position consonant +// first sub-joined consonant +// ....intermediate sub-joined consonants (if any) +// last sub-joined consonant +// sub-joined vowel (a-chung U+0F71) +// standard or compound vowel sign (or 'virama' for devanagari transliteration) + +enum TibetanForm { + TibetanOther, + TibetanHeadConsonant, + TibetanSubjoinedConsonant, + TibetanSubjoinedVowel, + TibetanVowel +}; + +// this table starts at U+0f40 +static const unsigned char tibetanForm[0x80] = { + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanOther, TibetanOther, TibetanOther, TibetanOther, + + TibetanOther, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanOther, TibetanOther, TibetanOther, TibetanOther, + TibetanOther, TibetanOther, TibetanOther, TibetanOther, + + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanOther, TibetanOther, TibetanOther +}; + + +static inline TibetanForm tibetan_form(const QChar &c) +{ + return (TibetanForm)tibetanForm[c.unicode() - 0x0f40]; +} + +#ifndef QT_NO_XFTFREETYPE +static const QOpenType::Features tibetan_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('a', 'b', 'v', 's'), AboveSubstProperty }, + { FT_MAKE_TAG('b', 'l', 'w', 's'), BelowSubstProperty }, + {0, 0} +}; +#endif + +static bool tibetan_shape_syllable(QOpenType *openType, QShaperItem *item, bool invalid) +{ + Q_UNUSED(openType) + int len = item->length; + + if (item->num_glyphs < item->length + 4) { + item->num_glyphs = item->length + 4; + return FALSE; + } + + int i; + QVarLengthArray<unsigned short> reordered(len+4); + + const QChar *str = item->string->unicode() + item->from; + if (invalid) { + *reordered.data() = 0x25cc; + memcpy(reordered.data()+1, str, len*sizeof(QChar)); + len++; + str = (QChar *)reordered.data(); + } + + if (item->font->stringToCMap(str, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + for (i = 0; i < item->length; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + IDEBUG(" %d: %4x", i, str[i].unicode()); + } + + // now we have the syllable in the right order, and can start running it through open type. + +#ifndef QT_NO_XFTFREETYPE + if (openType && openType->supportsScript(QFont::Tibetan)) { + openType->selectScript(QFont::Tibetan, tibetan_features); + + openType->shape(item); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + } +#endif + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + + +static int tibetan_nextSyllableBoundary(const QString &s, int start, int end, bool *invalid) +{ + const QChar *uc = s.unicode() + start; + + int pos = 0; + TibetanForm state = tibetan_form(*uc); + +// qDebug("state[%d]=%d (uc=%4x)", pos, state, uc[pos].unicode()); + pos++; + + if (state != TibetanHeadConsonant) { + if (state != TibetanOther) + *invalid = TRUE; + goto finish; + } + + while (pos < end - start) { + TibetanForm newState = tibetan_form(uc[pos]); + switch(newState) { + case TibetanSubjoinedConsonant: + case TibetanSubjoinedVowel: + if (state != TibetanHeadConsonant && + state != TibetanSubjoinedConsonant) + goto finish; + state = newState; + break; + case TibetanVowel: + if (state != TibetanHeadConsonant && + state != TibetanSubjoinedConsonant && + state != TibetanSubjoinedVowel) + goto finish; + break; + case TibetanOther: + case TibetanHeadConsonant: + goto finish; + } + pos++; + } + +finish: + *invalid = FALSE; + return start+pos; +} + +static bool tibetan_shape(QShaperItem *item) +{ + Q_ASSERT(item->script == QFont::Tibetan); + +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; +#else + QOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + QShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + while (sstart < end) { + bool invalid; + int send = tibetan_nextSyllableBoundary(*(item->string), sstart, end, &invalid); + IDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!tibetan_shape_syllable(openType, &syllable, invalid)) { + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + for (int i = sstart; i < send; ++i) + logClusters[i-item->from] = first_glyph; + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + +static void tibetan_attributes(int script, const QString &text, int from, int len, QCharAttributes *attributes) +{ + Q_UNUSED(script); + + int end = from + len; + const QChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while (i < len) { + bool invalid; + int boundary = tibetan_nextSyllableBoundary(text, from+i, end, &invalid) - from; + + attributes[i].charStop = TRUE; + + if (boundary > len-1) boundary = len; + i++; + while (i < boundary) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert(i == boundary); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Khmer +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + + +// Vocabulary +// Base -> A consonant or an independent vowel in its full (not subscript) form. It is the +// center of the syllable, it can be surrounded by coeng (subscript) consonants, vowels, +// split vowels, signs... but there is only one base in a syllable, it has to be coded as +// the first character of the syllable. +// split vowel --> vowel that has two parts placed separately (e.g. Before and after the consonant). +// Khmer language has five of them. Khmer split vowels either have one part before the +// base and one after the base or they have a part before the base and a part above the base. +// The first part of all Khmer split vowels is the same character, identical to +// the glyph of Khmer dependent vowel SRA EI +// coeng --> modifier used in Khmer to construct coeng (subscript) consonants +// Differently than indian languages, the coeng modifies the consonant that follows it, +// not the one preceding it Each consonant has two forms, the base form and the subscript form +// the base form is the normal one (using the consonants code-point), the subscript form is +// displayed when the combination coeng + consonant is encountered. +// Consonant of type 1 -> A consonant which has subscript for that only occupies space under a base consonant +// Consonant of type 2.-> Its subscript form occupies space under and before the base (only one, RO) +// Consonant of Type 3 -> Its subscript form occupies space under and after the base (KHO, CHHO, THHO, BA, YO, SA) +// Consonant shifter -> Khmer has to series of consonants. The same dependent vowel has different sounds +// if it is attached to a consonant of the first series or a consonant of the second series +// Most consonants have an equivalent in the other series, but some of theme exist only in +// one series (for example SA). If we want to use the consonant SA with a vowel sound that +// can only be done with a vowel sound that corresponds to a vowel accompanying a consonant +// of the other series, then we need to use a consonant shifter: TRIISAP or MUSIKATOAN +// x17C9 y x17CA. TRIISAP changes a first series consonant to second series sound and +// MUSIKATOAN a second series consonant to have a first series vowel sound. +// Consonant shifter are both normally supercript marks, but, when they are followed by a +// superscript, they change shape and take the form of subscript dependent vowel SRA U. +// If they are in the same syllable as a coeng consonant, Unicode 3.0 says that they +// should be typed before the coeng. Unicode 4.0 breaks the standard and says that it should +// be placed after the coeng consonant. +// Dependent vowel -> In khmer dependent vowels can be placed above, below, before or after the base +// Each vowel has its own position. Only one vowel per syllable is allowed. +// Signs -> Khmer has above signs and post signs. Only one above sign and/or one post sign are +// Allowed in a syllable. +// +// +// order is important here! This order must be the same that is found in each horizontal +// line in the statetable for Khmer (see khmerStateTable) . +// +enum KhmerCharClassValues { + CC_RESERVED = 0, + CC_CONSONANT = 1, // Consonant of type 1 or independent vowel + CC_CONSONANT2 = 2, // Consonant of type 2 + CC_CONSONANT3 = 3, // Consonant of type 3 + CC_ZERO_WIDTH_NJ_MARK = 4, // Zero Width non joiner character (0x200C) + CC_CONSONANT_SHIFTER = 5, + CC_ROBAT = 6, // Khmer special diacritic accent -treated differently in state table + CC_COENG = 7, // Subscript consonant combining character + CC_DEPENDENT_VOWEL = 8, + CC_SIGN_ABOVE = 9, + CC_SIGN_AFTER = 10, + CC_ZERO_WIDTH_J_MARK = 11, // Zero width joiner character + CC_COUNT = 12 // This is the number of character classes +}; + + +enum KhmerCharClassFlags { + CF_CLASS_MASK = 0x0000FFFF, + + CF_CONSONANT = 0x01000000, // flag to speed up comparing + CF_SPLIT_VOWEL = 0x02000000, // flag for a split vowel -> the first part is added in front of the syllable + CF_DOTTED_CIRCLE = 0x04000000, // add a dotted circle if a character with this flag is the first in a syllable + CF_COENG = 0x08000000, // flag to speed up comparing + CF_SHIFTER = 0x10000000, // flag to speed up comparing + CF_ABOVE_VOWEL = 0x20000000, // flag to speed up comparing + + // position flags + CF_POS_BEFORE = 0x00080000, + CF_POS_BELOW = 0x00040000, + CF_POS_ABOVE = 0x00020000, + CF_POS_AFTER = 0x00010000, + CF_POS_MASK = 0x000f0000 +}; + + +// Characters that get refered to by name +enum KhmerChar { + C_SIGN_ZWNJ = 0x200C, + C_SIGN_ZWJ = 0x200D, + C_DOTTED_CIRCLE = 0x25CC, + C_RO = 0x179A, + C_VOWEL_AA = 0x17B6, + C_SIGN_NIKAHIT = 0x17C6, + C_VOWEL_E = 0x17C1, + C_COENG = 0x17D2 +}; + + +// simple classes, they are used in the statetable (in this file) to control the length of a syllable +// they are also used to know where a character should be placed (location in reference to the base character) +// and also to know if a character, when independently displayed, should be displayed with a dotted-circle to +// indicate error in syllable construction +// +enum { + _xx = CC_RESERVED, + _sa = CC_SIGN_ABOVE | CF_DOTTED_CIRCLE | CF_POS_ABOVE, + _sp = CC_SIGN_AFTER | CF_DOTTED_CIRCLE| CF_POS_AFTER, + _c1 = CC_CONSONANT | CF_CONSONANT, + _c2 = CC_CONSONANT2 | CF_CONSONANT, + _c3 = CC_CONSONANT3 | CF_CONSONANT, + _rb = CC_ROBAT | CF_POS_ABOVE | CF_DOTTED_CIRCLE, + _cs = CC_CONSONANT_SHIFTER | CF_DOTTED_CIRCLE | CF_SHIFTER, + _dl = CC_DEPENDENT_VOWEL | CF_POS_BEFORE | CF_DOTTED_CIRCLE, + _db = CC_DEPENDENT_VOWEL | CF_POS_BELOW | CF_DOTTED_CIRCLE, + _da = CC_DEPENDENT_VOWEL | CF_POS_ABOVE | CF_DOTTED_CIRCLE | CF_ABOVE_VOWEL, + _dr = CC_DEPENDENT_VOWEL | CF_POS_AFTER | CF_DOTTED_CIRCLE, + _co = CC_COENG | CF_COENG | CF_DOTTED_CIRCLE, + + // split vowel + _va = _da | CF_SPLIT_VOWEL, + _vr = _dr | CF_SPLIT_VOWEL +}; + + +// Character class: a character class value +// ORed with character class flags. +// +typedef unsigned long KhmerCharClass; + + +// Character class tables +// _xx character does not combine into syllable, such as numbers, puntuation marks, non-Khmer signs... +// _sa Sign placed above the base +// _sp Sign placed after the base +// _c1 Consonant of type 1 or independent vowel (independent vowels behave as type 1 consonants) +// _c2 Consonant of type 2 (only RO) +// _c3 Consonant of type 3 +// _rb Khmer sign robat u17CC. combining mark for subscript consonants +// _cd Consonant-shifter +// _dl Dependent vowel placed before the base (left of the base) +// _db Dependent vowel placed below the base +// _da Dependent vowel placed above the base +// _dr Dependent vowel placed behind the base (right of the base) +// _co Khmer combining mark COENG u17D2, combines with the consonant or independent vowel following +// it to create a subscript consonant or independent vowel +// _va Khmer split vowel in wich the first part is before the base and the second one above the base +// _vr Khmer split vowel in wich the first part is before the base and the second one behind (right of) the base +// +static const KhmerCharClass khmerCharClasses[] = { + _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, // 1780 - 178F + _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c2, _c1, _c1, _c1, _c3, _c3, // 1790 - 179F + _c1, _c3, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, // 17A0 - 17AF + _c1, _c1, _c1, _c1, _dr, _dr, _dr, _da, _da, _da, _da, _db, _db, _db, _va, _vr, // 17B0 - 17BF + _vr, _dl, _dl, _dl, _vr, _vr, _sa, _sp, _sp, _cs, _cs, _sa, _rb, _sa, _sa, _sa, // 17C0 - 17CF + _sa, _sa, _co, _sa, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _sa, _xx, _xx // 17D0 - 17DF +}; + +// this enum must reflect the range of khmerCharClasses +enum KhmerCharClassesRange { + KhmerFirstChar = 0x1780, + KhmerLastChar = 0x17df +}; + +// Below we define how a character in the input string is either in the khmerCharClasses table +// (in which case we get its type back), a ZWJ or ZWNJ (two characters that may appear +// within the syllable, but are not in the table) we also get their type back, or an unknown object +// in which case we get _xx (CC_RESERVED) back +// +static inline KhmerCharClass getKhmerCharClass(const QChar &uc) +{ + if (uc.unicode() == C_SIGN_ZWJ) { + return CC_ZERO_WIDTH_J_MARK; + } + + if (uc.unicode() == C_SIGN_ZWNJ) { + return CC_ZERO_WIDTH_NJ_MARK; + } + + if (uc.unicode() < KhmerFirstChar || uc.unicode() > KhmerLastChar) { + return CC_RESERVED; + } + + return khmerCharClasses[uc.unicode() - KhmerFirstChar]; +} + + +// The stateTable is used to calculate the end (the length) of a well +// formed Khmer Syllable. +// +// Each horizontal line is ordered exactly the same way as the values in KhmerClassTable +// CharClassValues. This coincidence of values allows the follow up of the table. +// +// Each line corresponds to a state, which does not necessarily need to be a type +// of component... for example, state 2 is a base, with is always a first character +// in the syllable, but the state could be produced a consonant of any type when +// it is the first character that is analysed (in ground state). +// +// Differentiating 3 types of consonants is necessary in order to +// forbid the use of certain combinations, such as having a second +// coeng after a coeng RO, +// The inexistent possibility of having a type 3 after another type 3 is permitted, +// eliminating it would very much complicate the table, and it does not create typing +// problems, as the case above. +// +// The table is quite complex, in order to limit the number of coeng consonants +// to 2 (by means of the table). +// +// There a peculiarity, as far as Unicode is concerned: +// - The consonant-shifter is considered in two possible different +// locations, the one considered in Unicode 3.0 and the one considered in +// Unicode 4.0. (there is a backwards compatibility problem in this standard). +// +// +// xx independent character, such as a number, punctuation sign or non-khmer char +// +// c1 Khmer consonant of type 1 or an independent vowel +// that is, a letter in which the subscript for is only under the +// base, not taking any space to the right or to the left +// +// c2 Khmer consonant of type 2, the coeng form takes space under +// and to the left of the base (only RO is of this type) +// +// c3 Khmer consonant of type 3. Its subscript form takes space under +// and to the right of the base. +// +// cs Khmer consonant shifter +// +// rb Khmer robat +// +// co coeng character (u17D2) +// +// dv dependent vowel (including split vowels, they are treated in the same way). +// even if dv is not defined above, the component that is really tested for is +// KhmerClassTable::CC_DEPENDENT_VOWEL, which is common to all dependent vowels +// +// zwj Zero Width joiner +// +// zwnj Zero width non joiner +// +// sa above sign +// +// sp post sign +// +// there are lines with equal content but for an easier understanding +// (and maybe change in the future) we did not join them +// +static const signed char khmerStateTable[][CC_COUNT] = +{ + // xx c1 c2 c3 zwnj cs rb co dv sa sp zwj + { 1, 2, 2, 2, 1, 1, 1, 6, 1, 1, 1, 2}, // 0 - ground state + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1 - exit state (or sign to the right of the syllable) + {-1, -1, -1, -1, 3, 4, 5, 6, 16, 17, 1, -1}, // 2 - Base consonant + {-1, -1, -1, -1, -1, 4, -1, -1, 16, -1, -1, -1}, // 3 - First ZWNJ before a register shifter It can only be followed by a shifter or a vowel + {-1, -1, -1, -1, 15, -1, -1, 6, 16, 17, 1, 14}, // 4 - First register shifter + {-1, -1, -1, -1, -1, -1, -1, -1, 20, -1, 1, -1}, // 5 - Robat + {-1, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1}, // 6 - First Coeng + {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 7 - First consonant of type 1 after coeng + {-1, -1, -1, -1, 12, 13, -1, -1, 16, 17, 1, 14}, // 8 - First consonant of type 2 after coeng + {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 9 - First consonant or type 3 after ceong + {-1, 11, 11, 11, -1, -1, -1, -1, -1, -1, -1, -1}, // 10 - Second Coeng (no register shifter before) + {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 11 - Second coeng consonant (or ind. vowel) no register shifter before + {-1, -1, -1, -1, -1, 13, -1, -1, 16, -1, -1, -1}, // 12 - Second ZWNJ before a register shifter + {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 13 - Second register shifter + {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 14 - ZWJ before vowel + {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 15 - ZWNJ before vowel + {-1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 1, 18}, // 16 - dependent vowel + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 18}, // 17 - sign above + {-1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1}, // 18 - ZWJ after vowel + {-1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1}, // 19 - Third coeng + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1}, // 20 - dependent vowel after a Robat +}; + + +// #define KHMER_DEBUG +#ifdef KHMER_DEBUG +#define KHDEBUG qDebug +#else +#define KHDEBUG if(0) qDebug +#endif + +// Given an input string of characters and a location in which to start looking +// calculate, using the state table, which one is the last character of the syllable +// that starts in the starting position. +// +static inline int khmer_nextSyllableBoundary(const QString &s, int start, int end, bool *invalid) +{ + *invalid = FALSE; + const QChar *uc = s.unicode() + start; + int state = 0; + int pos = start; + + while (pos < end) { + KhmerCharClass charClass = getKhmerCharClass(*uc); + if (pos == start) { + *invalid = (charClass > 0) && ! (charClass & CF_CONSONANT); + } + state = khmerStateTable[state][charClass & CF_CLASS_MASK]; + + KHDEBUG("state[%d]=%d class=%8lx (uc=%4x)", pos - start, state, + charClass, uc->unicode() ); + + if (state < 0) { + break; + } + ++uc; + ++pos; + } + return pos; +} + + +#ifndef QT_NO_XFTFREETYPE +static const QOpenType::Features khmer_features[] = { + { FT_MAKE_TAG( 'p', 'r', 'e', 'f' ), PreFormProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 'f' ), BelowFormProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 'f' ), AboveFormProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 'f' ), PostFormProperty }, + { FT_MAKE_TAG( 'p', 'r', 'e', 's' ), PreSubstProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 's' ), BelowSubstProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 's' ), AboveSubstProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 's' ), PostSubstProperty }, + { FT_MAKE_TAG( 'c', 'l', 'i', 'g' ), CligProperty }, + { 0, 0 } +}; +#endif + + +static bool khmer_shape_syllable(QOpenType *openType, QShaperItem *item) +{ +#ifndef QT_NO_XFTFREETYPE + if (openType) + openType->selectScript(QFont::Khmer, khmer_features); +#endif + // according to the specs this is the max length one can get + // ### the real value should be smaller + assert(item->length < 13); + + KHDEBUG("syllable from %d len %d, str='%s'", item->from, item->length, + item->string->mid(item->from, item->length).utf8().data()); + + int len = 0; + int syllableEnd = item->from + item->length; + unsigned short reordered[16]; + unsigned char properties[16]; + enum { + AboveForm = 0x01, + PreForm = 0x02, + PostForm = 0x04, + BelowForm = 0x08 + }; + memset(properties, 0, 16*sizeof(unsigned char)); + +#ifdef KHMER_DEBUG + qDebug("original:"); + for (int i = from; i < syllableEnd; i++) { + qDebug(" %d: %4x", i, string[i].unicode()); + } +#endif + + // write a pre vowel or the pre part of a split vowel first + // and look out for coeng + ro. RO is the only vowel of type 2, and + // therefore the only one that requires saving space before the base. + // + int coengRo = -1; // There is no Coeng Ro, if found this value will change + int i; + for (i = item->from; i < syllableEnd; i += 1) { + KhmerCharClass charClass = getKhmerCharClass(item->string->at(i)); + + // if a split vowel, write the pre part. In Khmer the pre part + // is the same for all split vowels, same glyph as pre vowel C_VOWEL_E + if (charClass & CF_SPLIT_VOWEL) { + reordered[len] = C_VOWEL_E; + properties[len] = PreForm; + ++len; + break; // there can be only one vowel + } + // if a vowel with pos before write it out + if (charClass & CF_POS_BEFORE) { + reordered[len] = item->string->at(i).unicode(); + properties[len] = PreForm; + ++len; + break; // there can be only one vowel + } + // look for coeng + ro and remember position + // works because coeng + ro is always in front of a vowel (if there is a vowel) + // and because CC_CONSONANT2 is enough to identify it, as it is the only consonant + // with this flag + if ( (charClass & CF_COENG) && (i + 1 < syllableEnd) && + ( (getKhmerCharClass(item->string->at(i+1)) & CF_CLASS_MASK) == CC_CONSONANT2) ) { + coengRo = i; + } + } + + // write coeng + ro if found + if (coengRo > -1) { + reordered[len] = C_COENG; + properties[len] = PreForm; + ++len; + reordered[len] = C_RO; + properties[len] = PreForm; + ++len; + } + + // shall we add a dotted circle? + // If in the position in which the base should be (first char in the string) there is + // a character that has the Dotted circle flag (a character that cannot be a base) + // then write a dotted circle + if (getKhmerCharClass(item->string->at(item->from)) & CF_DOTTED_CIRCLE) { + reordered[len] = C_DOTTED_CIRCLE; + ++len; + } + + // copy what is left to the output, skipping before vowels and + // coeng Ro if they are present + for (i = item->from; i < syllableEnd; i += 1) { + QChar uc = item->string->at(i); + KhmerCharClass charClass = getKhmerCharClass(uc); + + // skip a before vowel, it was already processed + if (charClass & CF_POS_BEFORE) { + continue; + } + + // skip coeng + ro, it was already processed + if (i == coengRo) { + i += 1; + continue; + } + + switch (charClass & CF_POS_MASK) + { + case CF_POS_ABOVE : + reordered[len] = uc.unicode(); + properties[len] = AboveForm; + ++len; + break; + + case CF_POS_AFTER : + reordered[len] = uc.unicode(); + properties[len] = PostForm; + ++len; + break; + + case CF_POS_BELOW : + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + + default: + // assign the correct flags to a coeng consonant + // Consonants of type 3 are taged as Post forms and those type 1 as below forms + if ( (charClass & CF_COENG) && i + 1 < syllableEnd ) { + unsigned char property = (getKhmerCharClass(item->string->at(i+1)) & CF_CLASS_MASK) == CC_CONSONANT3 ? + PostForm : BelowForm; + reordered[len] = uc.unicode(); + properties[len] = property; + ++len; + i += 1; + reordered[len] = item->string->at(i).unicode(); + properties[len] = property; + ++len; + break; + } + + // if a shifter is followed by an above vowel change the shifter to below form, + // an above vowel can have two possible positions i + 1 or i + 3 + // (position i+1 corresponds to unicode 3, position i+3 to Unicode 4) + // and there is an extra rule for C_VOWEL_AA + C_SIGN_NIKAHIT also for two + // different positions, right after the shifter or after a vowel (Unicode 4) + if ( (charClass & CF_SHIFTER) && (i + 1 < syllableEnd) ) { + if (getKhmerCharClass(item->string->at(i+1)) & CF_ABOVE_VOWEL ) { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + if (i + 2 < syllableEnd && + (item->string->at(i+1).unicode() == C_VOWEL_AA) && + (item->string->at(i+2).unicode() == C_SIGN_NIKAHIT) ) + { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + if (i + 3 < syllableEnd && (getKhmerCharClass(item->string->at(i+3)) & CF_ABOVE_VOWEL) ) { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + if (i + 4 < syllableEnd && + (item->string->at(i+3).unicode() == C_VOWEL_AA) && + (item->string->at(i+4).unicode() == C_SIGN_NIKAHIT) ) + { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + } + + // default - any other characters + reordered[len] = uc.unicode(); + ++len; + break; + } // switch + } // for + + if (item->font->stringToCMap((const QChar *)reordered, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + KHDEBUG("after shaping: len=%d", len); + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + KHDEBUG(" %d: %4x property=%x", i, reordered[i], properties[i]); + } + + // now we have the syllable in the right order, and can start running it through open type. + +#ifndef QT_NO_XFTFREETYPE + if (openType) { + unsigned short logClusters[16]; + for (int i = 0; i < len; ++i) + logClusters[i] = i; + + uint where[16]; + + for (int i = 0; i < len; ++i) { + where[i] = ~(PreSubstProperty + | BelowSubstProperty + | AboveSubstProperty + | PostSubstProperty + | CligProperty + | PositioningProperties); + if (properties[i] == PreForm) + where[i] &= ~PreFormProperty; + else if (properties[i] == BelowForm) + where[i] &= ~BelowFormProperty; + else if (properties[i] == AboveForm) + where[i] &= ~AboveFormProperty; + else if (properties[i] == PostForm) + where[i] &= ~PostFormProperty; + } + + openType->shape(item, where); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + } else +#endif + { + KHDEBUG("Not using openType"); + Q_UNUSED(openType); + } + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + +static bool khmer_shape(QShaperItem *item) +{ + assert(item->script == QFont::Khmer); + +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; +#else + QOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + QShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + KHDEBUG("khmer_shape: from %d length %d", item->from, item->length); + while (sstart < end) { + bool invalid; + int send = khmer_nextSyllableBoundary(*item->string, sstart, end, &invalid); + KHDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!khmer_shape_syllable(openType, &syllable)) { + KHDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs); + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + KHDEBUG("syllable:"); + int i; + for (i = first_glyph; i < first_glyph + syllable.num_glyphs; ++i) + KHDEBUG(" %d -> glyph %x", i, item->glyphs[i]); + KHDEBUG(" logclusters:"); + for (i = sstart; i < send; ++i) { + KHDEBUG(" %d -> glyph %d", i, first_glyph); + logClusters[i-item->from] = first_glyph; + } + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + +static void khmer_attributes( int script, const QString &text, int from, int len, QCharAttributes *attributes ) +{ + Q_UNUSED(script); + + int end = from + len; + const QChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while ( i < len ) { + bool invalid; + int boundary = khmer_nextSyllableBoundary( text, from+i, end, &invalid ) - from; + + attributes[i].charStop = TRUE; + + if ( boundary > len-1 ) boundary = len; + i++; + while ( i < boundary ) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert( i == boundary ); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Myanmar +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +enum MymrCharClassValues +{ + Mymr_CC_RESERVED = 0, + Mymr_CC_CONSONANT = 1, /* Consonant of type 1, that has subscript form */ + Mymr_CC_CONSONANT2 = 2, /* Consonant of type 2, that has no subscript form */ + Mymr_CC_NGA = 3, /* Consonant NGA */ + Mymr_CC_YA = 4, /* Consonant YA */ + Mymr_CC_RA = 5, /* Consonant RA */ + Mymr_CC_WA = 6, /* Consonant WA */ + Mymr_CC_HA = 7, /* Consonant HA */ + Mymr_CC_IND_VOWEL = 8, /* Independent vowel */ + Mymr_CC_ZERO_WIDTH_NJ_MARK = 9, /* Zero Width non joiner character (0x200C) */ + Mymr_CC_VIRAMA = 10, /* Subscript consonant combining character */ + Mymr_CC_PRE_VOWEL = 11, /* Dependent vowel, prebase (Vowel e) */ + Mymr_CC_BELOW_VOWEL = 12, /* Dependent vowel, prebase (Vowel u, uu) */ + Mymr_CC_ABOVE_VOWEL = 13, /* Dependent vowel, prebase (Vowel i, ii, ai) */ + Mymr_CC_POST_VOWEL = 14, /* Dependent vowel, prebase (Vowel aa) */ + Mymr_CC_SIGN_ABOVE = 15, + Mymr_CC_SIGN_BELOW = 16, + Mymr_CC_SIGN_AFTER = 17, + Mymr_CC_ZERO_WIDTH_J_MARK = 18, /* Zero width joiner character */ + Mymr_CC_COUNT = 19 /* This is the number of character classes */ +}; + +enum MymrCharClassFlags +{ + Mymr_CF_CLASS_MASK = 0x0000FFFF, + + Mymr_CF_CONSONANT = 0x01000000, /* flag to speed up comparing */ + Mymr_CF_MEDIAL = 0x02000000, /* flag to speed up comparing */ + Mymr_CF_IND_VOWEL = 0x04000000, /* flag to speed up comparing */ + Mymr_CF_DEP_VOWEL = 0x08000000, /* flag to speed up comparing */ + Mymr_CF_DOTTED_CIRCLE = 0x10000000, /* add a dotted circle if a character with this flag is the first in a syllable */ + Mymr_CF_VIRAMA = 0x20000000, /* flag to speed up comparing */ + + /* position flags */ + Mymr_CF_POS_BEFORE = 0x00080000, + Mymr_CF_POS_BELOW = 0x00040000, + Mymr_CF_POS_ABOVE = 0x00020000, + Mymr_CF_POS_AFTER = 0x00010000, + Mymr_CF_POS_MASK = 0x000f0000, + + Mymr_CF_AFTER_KINZI = 0x00100000 +}; + +/* Characters that get refrered to by name */ +enum MymrChar +{ + Mymr_C_SIGN_ZWNJ = 0x200C, + Mymr_C_SIGN_ZWJ = 0x200D, + Mymr_C_DOTTED_CIRCLE = 0x25CC, + Mymr_C_RA = 0x101B, + Mymr_C_YA = 0x101A, + Mymr_C_NGA = 0x1004, + Mymr_C_VOWEL_E = 0x1031, + Mymr_C_VIRAMA = 0x1039 +}; + +enum +{ + Mymr_xx = Mymr_CC_RESERVED, + Mymr_c1 = Mymr_CC_CONSONANT | Mymr_CF_CONSONANT | Mymr_CF_POS_BELOW, + Mymr_c2 = Mymr_CC_CONSONANT2 | Mymr_CF_CONSONANT, + Mymr_ng = Mymr_CC_NGA | Mymr_CF_CONSONANT | Mymr_CF_POS_ABOVE, + Mymr_ya = Mymr_CC_YA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_AFTER | Mymr_CF_AFTER_KINZI, + Mymr_ra = Mymr_CC_RA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_BEFORE, + Mymr_wa = Mymr_CC_WA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_BELOW, + Mymr_ha = Mymr_CC_HA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_BELOW, + Mymr_id = Mymr_CC_IND_VOWEL | Mymr_CF_IND_VOWEL, + Mymr_vi = Mymr_CC_VIRAMA | Mymr_CF_VIRAMA | Mymr_CF_POS_ABOVE | Mymr_CF_DOTTED_CIRCLE, + Mymr_dl = Mymr_CC_PRE_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_BEFORE | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_db = Mymr_CC_BELOW_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_BELOW | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_da = Mymr_CC_ABOVE_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_ABOVE | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_dr = Mymr_CC_POST_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_AFTER | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_sa = Mymr_CC_SIGN_ABOVE | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_POS_ABOVE | Mymr_CF_AFTER_KINZI, + Mymr_sb = Mymr_CC_SIGN_BELOW | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_POS_BELOW | Mymr_CF_AFTER_KINZI, + Mymr_sp = Mymr_CC_SIGN_AFTER | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI +}; + + +typedef int MymrCharClass; + + +static const MymrCharClass mymrCharClasses[] = +{ + Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_ng, Mymr_c1, Mymr_c1, Mymr_c1, + Mymr_c1, Mymr_c1, Mymr_c2, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, /* 1000 - 100F */ + Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, + Mymr_c1, Mymr_c1, Mymr_ya, Mymr_ra, Mymr_c1, Mymr_wa, Mymr_c1, Mymr_ha, /* 1010 - 101F */ + Mymr_c2, Mymr_c2, Mymr_xx, Mymr_id, Mymr_id, Mymr_id, Mymr_id, Mymr_id, + Mymr_xx, Mymr_id, Mymr_id, Mymr_xx, Mymr_dr, Mymr_da, Mymr_da, Mymr_db, /* 1020 - 102F */ + Mymr_db, Mymr_dl, Mymr_da, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_sa, Mymr_sb, + Mymr_sp, Mymr_vi, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, /* 1030 - 103F */ + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, /* 1040 - 104F */ + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, /* 1050 - 105F */ +}; + +static MymrCharClass +getMyanmarCharClass (const QChar &ch) +{ + if (ch.unicode() == Mymr_C_SIGN_ZWJ) + return Mymr_CC_ZERO_WIDTH_J_MARK; + + if (ch.unicode() == Mymr_C_SIGN_ZWNJ) + return Mymr_CC_ZERO_WIDTH_NJ_MARK; + + if (ch.unicode() < 0x1000 || ch.unicode() > 0x105f) + return Mymr_CC_RESERVED; + + return mymrCharClasses[ch.unicode() - 0x1000]; +} + +static const signed char mymrStateTable[][Mymr_CC_COUNT] = +{ +// xx c1, c2 ng ya ra wa ha id zwnj vi dl db da dr sa sb sp zwj + { 1, 4, 4, 2, 4, 4, 4, 4, 24, 1, 27, 17, 18, 19, 20, 21, 1, 1, 4}, // 0 - ground state + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1 - exit state (or sp to the right of the syllable) + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 17, 18, 19, 20, 21, -1, -1, 4}, // 2 - NGA + {-1, 4, 4, 4, 4, 4, 4, 4, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 3 - Virama after NGA + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 17, 18, 19, 20, 21, 1, 1, -1}, // 4 - Base consonant + {-2, 6, -2, -2, 7, 8, 9, 10, -2, 23, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 5 - First virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 25, 17, 18, 19, 20, 21, -1, -1, -1}, // 6 - c1 after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, -1, -1}, // 7 - ya after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, -1, -1}, // 8 - ra after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, -1, -1}, // 9 - wa after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 20, 21, -1, -1, -1}, // 10 - ha after virama + {-1, -1, -1, -1, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 11 - Virama after NGA+zwj + {-2, -2, -2, -2, -2, -2, 13, 14, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 12 - Second virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 17, 18, 19, 20, 21, -1, -1, -1}, // 13 - wa after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 20, 21, -1, -1, -1}, // 14 - ha after virama + {-2, -2, -2, -2, -2, -2, -2, 16, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 15 - Third virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 20, 21, -1, -1, -1}, // 16 - ha after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 20, 21, 1, 1, -1}, // 17 - dl, Dependent vowel e + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 19, -1, 21, 1, 1, -1}, // 18 - db, Dependent vowel u,uu + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1}, // 19 - da, Dependent vowel i,ii,ai + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 22, -1, -1, -1, -1, -1, 1, 1, -1}, // 20 - dr, Dependent vowel aa + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1}, // 21 - sa, Sign anusvara + {-1, -1, -1, -1, -1, -1, -1, -1, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 22 - atha + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1}, // 23 - zwnj for atha + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1}, // 24 - Independent vowel + {-2, -2, -2, -2, 26, 26, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 25 - Virama after subscript consonant + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, 1, -1}, // 26 - ra/ya after subscript consonant + virama + {-1, 6, -1, -1, 7, 8, 9, 10, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 27 - Virama after ground state +// exit state -2 is for invalid order of medials and combination of invalids +// with virama where virama should treat as start of next syllable +}; + + + +// #define MYANMAR_DEBUG +#ifdef MYANMAR_DEBUG +#define MMDEBUG qDebug +#else +#define MMDEBUG if(0) qDebug +#endif + +// Given an input string of characters and a location in which to start looking +// calculate, using the state table, which one is the last character of the syllable +// that starts in the starting position. +// +static inline int myanmar_nextSyllableBoundary(const QString &s, int start, int end, bool *invalid) +{ + *invalid = FALSE; + const QChar *uc = s.unicode() + start; + int state = 0; + int pos = start; + + while (pos < end) { + MymrCharClass charClass = getMyanmarCharClass(*uc); + state = mymrStateTable[state][charClass & Mymr_CF_CLASS_MASK]; + if (pos == start) + *invalid = charClass & Mymr_CF_DOTTED_CIRCLE; + + MMDEBUG("state[%d]=%d class=%8x (uc=%4x)", pos - start, state, charClass, uc->unicode() ); + + if (state < 0) { + if (state < -1) + --pos; + break; + } + ++uc; + ++pos; + } + return pos; +} + + +#ifndef QT_NO_XFTFREETYPE +// ###### might have to change order of above and below forms and substitutions, +// but according to Unicode below comes before above +static const QOpenType::Features myanmar_features[] = { + { FT_MAKE_TAG( 'p', 'r', 'e', 'f' ), PreFormProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 'f' ), BelowFormProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 'f' ), AboveFormProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 'f' ), PostFormProperty }, + { FT_MAKE_TAG( 'p', 'r', 'e', 's' ), PreSubstProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 's' ), BelowSubstProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 's' ), AboveSubstProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 's' ), PostSubstProperty }, + { FT_MAKE_TAG( 'r', 'l', 'i', 'g' ), CligProperty }, // Myanmar1 uses this instead of the other features + { 0, 0 } +}; +#endif + + +// Visual order before shaping should be: +// +// [Vowel Mark E] +// [Virama + Medial Ra] +// [Base] +// [Virama + Consonant] +// [Nga + Virama] (Kinzi) ### should probably come before post forms (medial ya) +// [Vowels] +// [Marks] +// +// This means that we can keep the logical order apart from having to +// move the pre vowel, medial ra and kinzi + +static bool myanmar_shape_syllable(QOpenType *openType, QShaperItem *item, bool invalid) +{ +#ifndef QT_NO_XFTFREETYPE + if (openType) + openType->selectScript(QFont::Myanmar, myanmar_features); +#endif + // according to the table the max length of a syllable should be around 14 chars + assert(item->length < 32); + + MMDEBUG("\nsyllable from %d len %d, str='%s'", item->from, item->length, + item->string->mid(item->from, item->length).utf8().data()); + + const QChar *uc = item->string->unicode() + item->from; +#ifdef MYANMAR_DEBUG + qDebug("original:"); + for (int i = 0; i < item->length; i++) { + qDebug(" %d: %4x", i, uc[i].unicode()); + } +#endif + int vowel_e = -1; + int kinzi = -1; + int medial_ra = -1; + int base = -1; + int i; + for (i = 0; i < item->length; ++i) { + ushort chr = uc[i].unicode(); + + if (chr == Mymr_C_VOWEL_E) { + vowel_e = i; + continue; + } + if (i == 0 + && chr == Mymr_C_NGA + && i + 2 < item->length + && uc[i+1].unicode() == Mymr_C_VIRAMA) { + int mc = getMyanmarCharClass(uc[i+2]); + //MMDEBUG("maybe kinzi: mc=%x", mc); + if ((mc & Mymr_CF_CONSONANT) == Mymr_CF_CONSONANT) { + kinzi = i; + continue; + } + } + if (base >= 0 + && chr == Mymr_C_VIRAMA + && i + 1 < item->length + && uc[i+1].unicode() == Mymr_C_RA) { + medial_ra = i; + continue; + } + if (base < 0) + base = i; + } + + MMDEBUG("\n base=%d, vowel_e=%d, kinzi=%d, medial_ra=%d", base, vowel_e, kinzi, medial_ra); + int len = 0; + unsigned short reordered[32]; + unsigned char properties[32]; + enum { + AboveForm = 0x01, + PreForm = 0x02, + PostForm = 0x04, + BelowForm = 0x08 + }; + memset(properties, 0, 32*sizeof(unsigned char)); + + // write vowel_e if found + if (vowel_e >= 0) { + reordered[0] = Mymr_C_VOWEL_E; + len = 1; + } + // write medial_ra + if (medial_ra >= 0) { + reordered[len] = Mymr_C_VIRAMA; + reordered[len+1] = Mymr_C_RA; + properties[len] = PreForm; + properties[len+1] = PreForm; + len += 2; + } + + // shall we add a dotted circle? + // If in the position in which the base should be (first char in the string) there is + // a character that has the Dotted circle flag (a character that cannot be a base) + // then write a dotted circle + if (invalid) { + reordered[len] = C_DOTTED_CIRCLE; + ++len; + } + + bool lastWasVirama = FALSE; + int basePos = -1; + // copy the rest of the syllable to the output, inserting the kinzi + // at the correct place + for (i = 0; i < item->length; ++i) { + if (i == vowel_e) + continue; + if (i == medial_ra || i == kinzi) { + ++i; + continue; + } + + ushort chr = uc[i].unicode(); + MymrCharClass cc = getMyanmarCharClass(uc[i]); + if (kinzi >= 0 && i > base && (cc & Mymr_CF_AFTER_KINZI)) { + reordered[len] = Mymr_C_NGA; + reordered[len+1] = Mymr_C_VIRAMA; + properties[len-1] = AboveForm; + properties[len] = AboveForm; + len += 2; + kinzi = -1; + } + + if (lastWasVirama) { + int prop = 0; + switch(cc & Mymr_CF_POS_MASK) { + case Mymr_CF_POS_BEFORE: + prop = PreForm; + break; + case Mymr_CF_POS_BELOW: + prop = BelowForm; + break; + case Mymr_CF_POS_ABOVE: + prop = AboveForm; + break; + case Mymr_CF_POS_AFTER: + prop = PostForm; + break; + default: + break; + } + properties[len-1] = prop; + properties[len] = prop; + if(basePos >= 0 && basePos == len-2) + properties[len-2] = prop; + } + lastWasVirama = (chr == Mymr_C_VIRAMA); + if(i == base) + basePos = len; + + if ((chr != Mymr_C_SIGN_ZWNJ && chr != Mymr_C_SIGN_ZWJ) || !len) { + reordered[len] = chr; + ++len; + } + } + if (kinzi >= 0) { + reordered[len] = Mymr_C_NGA; + reordered[len+1] = Mymr_C_VIRAMA; + properties[len] = AboveForm; + properties[len+1] = AboveForm; + len += 2; + } + + if (item->font->stringToCMap((const QChar *)reordered, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + + MMDEBUG("after shaping: len=%d", len); + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + MMDEBUG(" %d: %4x property=%x", i, reordered[i], properties[i]); + } + + // now we have the syllable in the right order, and can start running it through open type. + +#ifndef QT_NO_XFTFREETYPE + if (openType) { + unsigned short logClusters[32]; + for (int i = 0; i < len; ++i) + logClusters[i] = i; + + uint where[32]; + + for (int i = 0; i < len; ++i) { + where[i] = ~(PreSubstProperty + | BelowSubstProperty + | AboveSubstProperty + | PostSubstProperty + | CligProperty + | PositioningProperties); + if (properties[i] == PreForm) + where[i] &= ~PreFormProperty; + else if (properties[i] == BelowForm) + where[i] &= ~BelowFormProperty; + else if (properties[i] == AboveForm) + where[i] &= ~AboveFormProperty; + else if (properties[i] == PostForm) + where[i] &= ~PostFormProperty; + } + + openType->shape(item, where); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + } else +#endif + { + MMDEBUG("Not using openType"); + Q_UNUSED(openType); + } + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + +static bool myanmar_shape(QShaperItem *item) +{ + assert(item->script == QFont::Myanmar); + +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; +#else + QOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + QShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + MMDEBUG("myanmar_shape: from %d length %d", item->from, item->length); + while (sstart < end) { + bool invalid; + int send = myanmar_nextSyllableBoundary(*item->string, sstart, end, &invalid); + MMDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!myanmar_shape_syllable(openType, &syllable, invalid)) { + MMDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs); + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + MMDEBUG("syllable:"); + int i; + for (i = first_glyph; i < first_glyph + syllable.num_glyphs; ++i) + MMDEBUG(" %d -> glyph %x", i, item->glyphs[i]); + MMDEBUG(" logclusters:"); + for (i = sstart; i < send; ++i) { + MMDEBUG(" %d -> glyph %d", i, first_glyph); + logClusters[i-item->from] = first_glyph; + } + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + +static void myanmar_attributes( int script, const QString &text, int from, int len, QCharAttributes *attributes ) +{ + Q_UNUSED(script); + + int end = from + len; + const QChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while ( i < len ) { + bool invalid; + int boundary = myanmar_nextSyllableBoundary( text, from+i, end, &invalid ) - from; + + attributes[i].charStop = TRUE; + attributes[i].softBreak = TRUE; + + if ( boundary > len-1 ) boundary = len; + i++; + while ( i < boundary ) { + attributes[i].charStop = FALSE; + attributes[i].softBreak = FALSE; + ++uc; + ++i; + } + assert( i == boundary ); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Hangul +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +// Hangul is a syllable based script. Unicode reserves a large range +// for precomposed hangul, where syllables are already precomposed to +// their final glyph shape. In addition, a so called jamo range is +// defined, that can be used to express old Hangul. Modern hangul +// syllables can also be expressed as jamo, and should be composed +// into syllables. The operation is rather simple and mathematical. + +// Every hangul jamo is classified as being either a Leading consonant +// (L), and intermediat Vowel (V) or a trailing consonant (T). Modern +// hangul syllables (the ones in the precomposed area can be of type +// LV or LVT. +// +// Syllable breaks do _not_ occur between: +// +// L L, V or precomposed +// V, LV V, T +// LVT, T T +// +// A standard syllable is of the form L+V+T*. The above rules allow +// nonstandard syllables L*V*T*. To transform them into standard +// syllables fill characers L_f and V_f can be inserted. + +enum { + Hangul_SBase = 0xac00, + Hangul_LBase = 0x1100, + Hangul_VBase = 0x1161, + Hangul_TBase = 0x11a7, + Hangul_SCount = 11172, + Hangul_LCount = 19, + Hangul_VCount = 21, + Hangul_TCount = 28, + Hangul_NCount = 21*28 +}; + +static inline bool hangul_isPrecomposed(unsigned short uc) { + return (uc >= Hangul_SBase && uc < Hangul_SBase + Hangul_SCount); +} + +static inline bool hangul_isLV(unsigned short uc) { + return ((uc - Hangul_SBase) % Hangul_TCount == 0); +} + +enum HangulType { + L, + V, + T, + LV, + LVT, + X +}; + +static inline HangulType hangul_type(unsigned short uc) { + if (uc > Hangul_SBase && uc < Hangul_SBase + Hangul_SCount) + return hangul_isLV(uc) ? LV : LVT; + if (uc < Hangul_LBase || uc > 0x11ff) + return X; + if (uc < Hangul_VBase) + return L; + if (uc < Hangul_TBase) + return V; + return T; +} + +static int hangul_nextSyllableBoundary(const QString &s, int start, int end) +{ + const QChar *uc = s.unicode() + start; + + HangulType state = hangul_type(uc->unicode()); + int pos = 1; + + while (pos < end - start) { + HangulType newState = hangul_type(uc[pos].unicode()); + switch(newState) { + case X: + goto finish; + case L: + case V: + case T: + if (state > newState) + goto finish; + state = newState; + break; + case LV: + if (state > L) + goto finish; + state = V; + break; + case LVT: + if (state > L) + goto finish; + state = T; + } + ++pos; + } + + finish: + return start+pos; +} + +#ifndef QT_NO_XFTFREETYPE +static const QOpenType::Features hangul_features [] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('l', 'j', 'm', 'o'), CcmpProperty }, + { FT_MAKE_TAG('j', 'j', 'm', 'o'), CcmpProperty }, + { FT_MAKE_TAG('t', 'j', 'm', 'o'), CcmpProperty }, + { 0, 0 } +}; +#endif + +static bool hangul_shape_syllable(QOpenType *openType, QShaperItem *item) +{ + Q_UNUSED(openType) + const QChar *ch = item->string->unicode() + item->from; + + int i; + unsigned short composed = 0; + // see if we can compose the syllable into a modern hangul + if (item->length == 2) { + int LIndex = ch[0].unicode() - Hangul_LBase; + int VIndex = ch[1].unicode() - Hangul_VBase; + if (LIndex >= 0 && LIndex < Hangul_LCount && + VIndex >= 0 && VIndex < Hangul_VCount) + composed = (LIndex * Hangul_VCount + VIndex) * Hangul_TCount + Hangul_SBase; + } else if (item->length == 3) { + int LIndex = ch[0].unicode() - Hangul_LBase; + int VIndex = ch[1].unicode() - Hangul_VBase; + int TIndex = ch[2].unicode() - Hangul_TBase; + if (LIndex >= 0 && LIndex < Hangul_LCount && + VIndex >= 0 && VIndex < Hangul_VCount && + TIndex >= 0 && TIndex < Hangul_TCount) + composed = (LIndex * Hangul_VCount + VIndex) * Hangul_TCount + TIndex + Hangul_SBase; + } + + + int len = item->length; + QChar c(composed); + + // ### icc says 'chars' is unused + // const QChar *chars = ch; + + // if we have a modern hangul use the composed form + if (composed) { + // chars = &c; + len = 1; + } + + if (item->font->stringToCMap(ch, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & QTextEngine::RightToLeft) != QFontEngine::NoError) + return FALSE; + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + IDEBUG(" %d: %4x", i, ch[i].unicode()); + } + +#ifndef QT_NO_XFTFREETYPE + if (openType && !composed) { + + QVarLengthArray<unsigned short> logClusters(len); + for (i = 0; i < len; ++i) + logClusters[i] = i; + item->log_clusters = logClusters.data(); + + openType->shape(item); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + + } +#endif + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + +static bool hangul_shape(QShaperItem *item) +{ + Q_ASSERT(item->script == QFont::Hangul); + + const QChar *uc = item->string->unicode() + item->from; + + bool allPrecomposed = TRUE; + for (int i = 0; i < item->length; ++i) { + if (!hangul_isPrecomposed(uc[i].unicode())) { + allPrecomposed = FALSE; + break; + } + } + + if (!allPrecomposed) { +#ifndef QT_NO_XFTFREETYPE + QOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; + if (openType) + openType->selectScript(QFont::Hangul, hangul_features); +#else + QOpenType *openType = 0; +#endif + + unsigned short *logClusters = item->log_clusters; + + QShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + while (sstart < end) { + int send = hangul_nextSyllableBoundary(*(item->string), sstart, end); + + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!hangul_shape_syllable(openType, &syllable)) { + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + // fix logcluster array + for (int i = sstart; i < send; ++i) + logClusters[i-item->from] = first_glyph; + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; + } + + return basic_shape(item); +} + +static void hangul_attributes(int script, const QString &text, int from, int len, QCharAttributes *attributes) +{ + Q_UNUSED(script); + + int end = from + len; + const QChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while (i < len) { + int boundary = hangul_nextSyllableBoundary(text, from+i, end) - from; + + attributes[i].charStop = TRUE; + + if (boundary > len-1) boundary = len; + i++; + while (i < boundary) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert(i == boundary); + } +} + +// ----------------------------------------------------------------------------------------------- +// +// The script engine jump table +// +// ----------------------------------------------------------------------------------------------- + +const q_scriptEngine scriptEngines[] = { + // Latin, + { basic_shape, 0 }, + // Greek, + { basic_shape, 0 }, + // Cyrillic, + { basic_shape, 0 }, + // Armenian, + { basic_shape, 0 }, + // Georgian, + { basic_shape, 0 }, + // Runic, + { basic_shape, 0 }, + // Ogham, + { basic_shape, 0 }, + // SpacingModifiers, + { basic_shape, 0 }, + // CombiningMarks, + { basic_shape, 0 }, + + // // Middle Eastern Scripts + // Hebrew, + { hebrew_shape, 0 }, + // Arabic, + { arabic_shape, 0 }, + // Syriac, + { syriac_shape, 0 }, + // Thaana, + { thaana_shape, 0 }, + + // // South and Southeast Asian Scripts + // Devanagari, + { indic_shape, indic_attributes }, + // Bengali, + { indic_shape, indic_attributes }, + // Gurmukhi, + { indic_shape, indic_attributes }, + // Gujarati, + { indic_shape, indic_attributes }, + // Oriya, + { indic_shape, indic_attributes }, + // Tamil, + { indic_shape, indic_attributes }, + // Telugu, + { indic_shape, indic_attributes }, + // Kannada, + { indic_shape, indic_attributes }, + // Malayalam, + { indic_shape, indic_attributes }, + // Sinhala, + { indic_shape, indic_attributes }, + // Thai, + { basic_shape, thai_attributes }, + // Lao, + { basic_shape, thai_attributes }, + // Tibetan, + { tibetan_shape, tibetan_attributes }, + // Myanmar, + { myanmar_shape, myanmar_attributes }, + // Khmer, + { khmer_shape, khmer_attributes }, + + // // East Asian Scripts + // Han, + { basic_shape, 0 }, + // Hiragana, + { basic_shape, 0 }, + // Katakana, + { basic_shape, 0 }, + // Hangul, + { hangul_shape, hangul_attributes }, + // Bopomofo, + { basic_shape, 0 }, + // Yi, + { basic_shape, 0 }, + + // // Additional Scripts + // Ethiopic, + { basic_shape, 0 }, + // Cherokee, + { basic_shape, 0 }, + // CanadianAboriginal, + { basic_shape, 0 }, + // Mongolian, + { basic_shape, 0 }, + + // // Symbols + // CurrencySymbols, + { basic_shape, 0 }, + // LetterlikeSymbols, + { basic_shape, 0 }, + // NumberForms, + { basic_shape, 0 }, + // MathematicalOperators, + { basic_shape, 0 }, + // TechnicalSymbols, + { basic_shape, 0 }, + // GeometricSymbols, + { basic_shape, 0 }, + // MiscellaneousSymbols, + { basic_shape, 0 }, + // EnclosedAndSquare, + { basic_shape, 0 }, + // Braille, + { basic_shape, 0 }, + + // Unicode, + { basic_shape, 0 }, + //Tagalog, + { basic_shape, 0 }, + //Hanunoo, + { basic_shape, 0 }, + //Buhid, + { basic_shape, 0 }, + //Tagbanwa, + { basic_shape, 0 }, + // KatakanaHalfWidth + { basic_shape, 0 }, + // Limbu + { basic_shape, 0 }, + // TaiLe + { basic_shape, 0 } +}; diff --git a/src/kernel/qsession.h b/src/kernel/qsession.h new file mode 100644 index 0000000..59185bc --- /dev/null +++ b/src/kernel/qsession.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Definition of QSession class +** +** Created : 990510 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSESSION_H +#define QSESSION_H + +#ifndef QT_H +#endif // QT_H + +#endif diff --git a/src/kernel/qsessionmanager.h b/src/kernel/qsessionmanager.h new file mode 100644 index 0000000..513196b --- /dev/null +++ b/src/kernel/qsessionmanager.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Definition of QSessionManager class +** +** Created : 990510 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSESSIONMANAGER_H +#define QSESSIONMANAGER_H + +#ifndef QT_H +#include "qobject.h" +#include "qwindowdefs.h" +#include "qstring.h" +#include "qstringlist.h" +#endif // QT_H +#ifndef QT_NO_SESSIONMANAGER + +class QSessionManagerData; + +class Q_EXPORT QSessionManager : public QObject +{ + Q_OBJECT + QSessionManager( QApplication *app, QString &id, QString &key ); + ~QSessionManager(); +public: + QString sessionId() const; + QString sessionKey() const; +#if defined(Q_WS_X11) || defined(Q_WS_MAC) + void* handle() const; +#endif + + bool allowsInteraction(); + bool allowsErrorInteraction(); + void release(); + + void cancel(); + + enum RestartHint { + RestartIfRunning, + RestartAnyway, + RestartImmediately, + RestartNever + }; + void setRestartHint( RestartHint ); + RestartHint restartHint() const; + + void setRestartCommand( const QStringList& ); + QStringList restartCommand() const; + void setDiscardCommand( const QStringList& ); + QStringList discardCommand() const; + + void setManagerProperty( const QString& name, const QString& value ); + void setManagerProperty( const QString& name, const QStringList& value ); + + bool isPhase2() const; + void requestPhase2(); + +private: + friend class QApplication; + friend class QBaseApplication; + QSessionManagerData* d; +}; + +#endif // QT_NO_SESSIONMANAGER +#endif // QSESSIONMANAGER_H diff --git a/src/kernel/qsharedmemory_p.cpp b/src/kernel/qsharedmemory_p.cpp new file mode 100644 index 0000000..e5b0b92 --- /dev/null +++ b/src/kernel/qsharedmemory_p.cpp @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Provides a standardised interface to shared memory +** +** Created : 020124 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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 "qsharedmemory_p.h" + +#if !defined(QT_QWS_NO_SHM) + +#if defined(QT_POSIX_QSHM) +#include <fcntl.h> +#include <sys/mman.h> + +QSharedMemory::QSharedMemory (int size, QString filename, char c ) +{ + shmSize = size; + shmFile = filename; + character = c; + shmFile.append(c); +} + +bool QSharedMemory::create () +{ + shmFD = shm_open (shmFile.latin1 (), O_RDWR | O_EXCL | O_CREAT, 0666); + if (shmFD == -1) + return FALSE; + else if (ftruncate (shmFD, shmSize) == -1) + { + close (shmFD); + return FALSE; + } + + return TRUE; +} + +void QSharedMemory::destroy () +{ + shm_unlink (shmFile.latin1 ()); +} + +bool QSharedMemory::attach () +{ + shmBase = mmap (0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, shmFD, 0); + + if (shmBase == MAP_FAILED) + return FALSE; + + close (shmFD); + return TRUE; +} + +void QSharedMemory::detach () +{ + munmap (shmBase, shmSize); +} + +void QSharedMemory::setPermissions (mode_t mode) +{ + mprotect (shmBase, shmSize, mode); // Provide defines to make prot work properly +} + +int QSharedMemory::size() +{ + struct stat buf; + int rc = fstat (shmFD, &buf); + if (rc != -1) + return buf.st_size; + else + return rc; +} + +#else // Assume SysV for backwards compat +#include <sys/shm.h> + +QSharedMemory::QSharedMemory (int size, QString filename, char c ) +{ + shmSize = size; + shmFile = filename; + character = c; + key = ftok (shmFile.latin1 (), c); + idInitted = FALSE; + shmId = -1; +} + +bool QSharedMemory::create () +{ + shmId = shmget (key, shmSize, IPC_CREAT | 0666); + if (shmId == -1) + return FALSE; + else + return TRUE; +} + +void QSharedMemory::destroy () +{ + if (shmId != -1) { + struct shmid_ds shm; + shmctl (shmId, IPC_RMID, &shm); + } +} + +bool QSharedMemory::attach () +{ + if (shmId == -1) + shmId = shmget (key, shmSize, 0); + + shmBase = shmat (shmId, 0, 0); + if ((int) shmBase == -1 || shmBase == 0) + return FALSE; + else + return TRUE; +} + +void QSharedMemory::detach () +{ + shmdt (shmBase); +} + +void QSharedMemory::setPermissions (mode_t mode) +{ + struct shmid_ds shm; + shmctl (shmId, IPC_STAT, &shm); + shm.shm_perm.mode = mode; + shmctl (shmId, IPC_SET, &shm); +} + +int QSharedMemory::size () +{ + struct shmid_ds shm; + shmctl (shmId, IPC_STAT, &shm); + return shm.shm_segsz; +} + +#endif + +#endif diff --git a/src/kernel/qsharedmemory_p.h b/src/kernel/qsharedmemory_p.h new file mode 100644 index 0000000..7de8f88 --- /dev/null +++ b/src/kernel/qsharedmemory_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Includes system files for shared memory +** +** Created : 020124 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QSHAREDMEMORY_P_H +#define QSHAREDMEMORY_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qapplication_qws.cpp and qgfxvnc_qws.cpp. This header file may +// change from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qstring.h" +#endif // QT_H + +#if !defined (QT_QWS_NO_SHM) + +#include <sys/types.h> +#include <sys/ipc.h> + +class QSharedMemory { +public: + QSharedMemory(){}; + QSharedMemory(int, QString, char c = 'Q'); + ~QSharedMemory(){}; + + bool create(); + void destroy(); + + bool attach(); + void detach(); + + void setPermissions(mode_t mode); + int size(); + void * base() { return shmBase; }; + +private: + void *shmBase; + int shmSize; + QString shmFile; + char character; +#if defined(QT_POSIX_QSHM) + int shmFD; +#else + int shmId; + key_t key; + int idInitted; +#endif +}; + +#endif + +#endif diff --git a/src/kernel/qsignal.cpp b/src/kernel/qsignal.cpp new file mode 100644 index 0000000..8296701 --- /dev/null +++ b/src/kernel/qsignal.cpp @@ -0,0 +1,257 @@ +/**************************************************************************** +** +** Implementation of QSignal class +** +** Created : 941201 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsignal.h" +#include "qmetaobject.h" +#include "qguardedptr.h" + +/*! + \class QSignal qsignal.h + \brief The QSignal class can be used to send signals for classes + that don't inherit QObject. + + \ingroup io + \ingroup misc + + If you want to send signals from a class that does not inherit + QObject, you can create an internal QSignal object to emit the + signal. You must also provide a function that connects the signal + to an outside object slot. This is how we have implemented + signals in the QMenuData class, which is not a QObject. + + In general, we recommend inheriting QObject instead. QObject + provides much more functionality. + + You can set a single QVariant parameter for the signal with + setValue(). + + Note that QObject is a \e private base class of QSignal, i.e. you + cannot call any QObject member functions from a QSignal object. + + Example: + \code + #include <qsignal.h> + + class MyClass + { + public: + MyClass(); + ~MyClass(); + + void doSomething(); + + void connect( QObject *receiver, const char *member ); + + private: + QSignal *sig; + }; + + MyClass::MyClass() + { + sig = new QSignal; + } + + MyClass::~MyClass() + { + delete sig; + } + + void MyClass::doSomething() + { + // ... does something + sig->activate(); // emits the signal + } + + void MyClass::connect( QObject *receiver, const char *member ) + { + sig->connect( receiver, member ); + } + \endcode +*/ + +/*! + Constructs a signal object called \a name, with the parent object + \a parent. These arguments are passed directly to QObject. +*/ + +QSignal::QSignal( QObject *parent, const char *name ) + : QObject( parent, name ) +{ + isSignal = TRUE; +#ifndef QT_NO_VARIANT + val = 0; +#endif +} + +/*! + Destroys the signal. All connections are removed, as is the case + with all QObjects. +*/ +QSignal::~QSignal() +{ +} +#ifndef QT_NO_VARIANT +// Returns TRUE if it matches ".+(.*int.*" +static inline bool intSignature( const char *member ) +{ + QCString s( member ); + int p = s.find( '(' ); + return p > 0 && p < s.findRev( "int" ); +} +#endif +/*! + Connects the signal to \a member in object \a receiver. + + \sa disconnect(), QObject::connect() +*/ + +bool QSignal::connect( const QObject *receiver, const char *member ) +{ +#ifndef QT_NO_VARIANT + if ( intSignature( member ) ) +#endif + return QObject::connect( (QObject *)this, SIGNAL(intSignal(int)), receiver, member ); +#ifndef QT_NO_VARIANT + return QObject::connect( (QObject *)this, SIGNAL(signal(const QVariant&)), + receiver, member ); +#endif +} + +/*! + Disonnects the signal from \a member in object \a receiver. + + \sa connect(), QObject::disconnect() +*/ + +bool QSignal::disconnect( const QObject *receiver, const char *member ) +{ + if (!member) + return QObject::disconnect( (QObject *)this, 0, receiver, member); +#ifndef QT_NO_VARIANT + if ( intSignature( member ) ) +#endif + return QObject::disconnect( (QObject *)this, SIGNAL(intSignal(int)), receiver, member ); +#ifndef QT_NO_VARIANT + return QObject::disconnect( (QObject *)this, SIGNAL(signal(const QVariant&)), + receiver, member ); +#endif +} + + +/*! + \fn bool QSignal::isBlocked() const + \obsolete + Returns TRUE if the signal is blocked, or FALSE if it is not blocked. + + The signal is not blocked by default. + + \sa block(), QObject::signalsBlocked() +*/ + +/*! + \fn void QSignal::block( bool b ) + \obsolete + Blocks the signal if \a b is TRUE, or unblocks the signal if \a b is FALSE. + + An activated signal disappears into hyperspace if it is blocked. + + \sa isBlocked(), activate(), QObject::blockSignals() +*/ + + +/*! + \fn void QSignal::activate() + + Emits the signal. If the platform supports QVariant and a + parameter has been set with setValue(), this value is passed in + the signal. +*/ +void QSignal::activate() +{ +#ifndef QT_NO_VARIANT + /* Create this QGuardedPtr on this, if we get destroyed after the intSignal (but before the variant signal) + we cannot just emit the signal (because val has been destroyed already) */ + QGuardedPtr<QSignal> me = this; + if( me ) + emit intSignal( val.toInt() ); + if( me ) + emit signal( val ); +#else + emit intSignal(0); +#endif +} + +#ifndef QT_NO_VARIANT +/*! + Sets the signal's parameter to \a value +*/ +void QSignal::setValue( const QVariant &value ) +{ + val = value; +} + +/*! + Returns the signal's parameter +*/ +QVariant QSignal::value() const +{ + return val; +} +/*! \fn void QSignal::signal( const QVariant & ) + \internal +*/ +/*! \fn void QSignal::intSignal( int ) + \internal +*/ + +#ifndef QT_NO_COMPAT +/*! \obsolete */ +void QSignal::setParameter( int value ) +{ + val = value; +} + +/*! \obsolete */ +int QSignal::parameter() const +{ + return val.toInt(); +} +#endif +#endif //QT_NO_VARIANT diff --git a/src/kernel/qsignal.h b/src/kernel/qsignal.h new file mode 100644 index 0000000..b8298c4 --- /dev/null +++ b/src/kernel/qsignal.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Definition of QSignal class +** +** Created : 941201 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIGNAL_H +#define QSIGNAL_H + +#ifndef QT_H +#include "qvariant.h" +#include "qobject.h" +#endif // QT_H + + +class Q_EXPORT QSignal : public QObject +{ + Q_OBJECT + +public: + QSignal( QObject *parent=0, const char *name=0 ); + ~QSignal(); + + bool connect( const QObject *receiver, const char *member ); + bool disconnect( const QObject *receiver, const char *member=0 ); + + void activate(); + +#ifndef QT_NO_COMPAT + bool isBlocked() const { return QObject::signalsBlocked(); } + void block( bool b ) { QObject::blockSignals( b ); } +#ifndef QT_NO_VARIANT + void setParameter( int value ); + int parameter() const; +#endif +#endif + +#ifndef QT_NO_VARIANT + void setValue( const QVariant &value ); + QVariant value() const; +#endif +signals: +#ifndef QT_NO_VARIANT + void signal( const QVariant& ); +#endif + void intSignal( int ); + +private: +#ifndef QT_NO_VARIANT + QVariant val; +#endif +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QSignal( const QSignal & ); + QSignal &operator=( const QSignal & ); +#endif +}; + + +#endif // QSIGNAL_H diff --git a/src/kernel/qsignalmapper.cpp b/src/kernel/qsignalmapper.cpp new file mode 100644 index 0000000..848976c --- /dev/null +++ b/src/kernel/qsignalmapper.cpp @@ -0,0 +1,183 @@ +/**************************************************************************** +** +** Implementation of QSignalMapper class +** +** Created : 980503 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsignalmapper.h" +#ifndef QT_NO_SIGNALMAPPER +#include "qptrdict.h" + +struct QSignalMapperRec { + QSignalMapperRec() + { + has_int = 0; + str_id = QString::null; + } + + uint has_int:1; + + int int_id; + QString str_id; + // extendable to other types of identification +}; + +class QSignalMapperData { +public: + QSignalMapperData() + { + dict.setAutoDelete( TRUE ); + } + + QPtrDict<QSignalMapperRec> dict; +}; + +/*! + \class QSignalMapper qsignalmapper.h + \brief The QSignalMapper class bundles signals from identifiable senders. + + \ingroup io + + This class collects a set of parameterless signals, and re-emits + them with integer or string parameters corresponding to the object + that sent the signal. +*/ + +/*! + Constructs a QSignalMapper called \a name, with parent \a parent. + Like all QObjects, it will be deleted when the parent is deleted. +*/ +QSignalMapper::QSignalMapper( QObject* parent, const char* name ) : + QObject( parent, name ) +{ + d = new QSignalMapperData; +} + +/*! + Destroys the QSignalMapper. +*/ +QSignalMapper::~QSignalMapper() +{ + delete d; +} + +/*! + Adds a mapping so that when map() is signaled from the given \a + sender, the signal mapped(\a identifier) is emitted. + + There may be at most one integer identifier for each object. +*/ +void QSignalMapper::setMapping( const QObject* sender, int identifier ) +{ + QSignalMapperRec* rec = getRec(sender); + rec->int_id = identifier; + rec->has_int = 1; +} + +/*! + \overload + + Adds a mapping so that when map() is signaled from the given \a + sender, the signal mapper(\a identifier) is emitted. + + There may be at most one string identifier for each object, and it + may not be empty. +*/ +void QSignalMapper::setMapping( const QObject* sender, const QString &identifier ) +{ + QSignalMapperRec* rec = getRec(sender); + rec->str_id = identifier; +} + +/*! + Removes all mappings for \a sender. This is done automatically + when mapped objects are destroyed. +*/ +void QSignalMapper::removeMappings( const QObject* sender ) +{ + d->dict.remove((void*)sender); +} + +void QSignalMapper::removeMapping() +{ + removeMappings(sender()); +} + +/*! + This slot emits signals based on which object sends signals to it. +*/ +void QSignalMapper::map() +{ + const QObject* s = sender(); + QSignalMapperRec* rec = d->dict.find( (void*)s ); + if ( rec ) { + if ( rec->has_int ) + emit mapped( rec->int_id ); + if ( !rec->str_id.isEmpty() ) + emit mapped( rec->str_id ); + } +} + +QSignalMapperRec* QSignalMapper::getRec( const QObject* sender ) +{ + QSignalMapperRec* rec = d->dict.find( (void*)sender ); + if (!rec) { + rec = new QSignalMapperRec; + d->dict.insert( (void*)sender, rec ); + connect( sender, SIGNAL(destroyed()), this, SLOT(removeMapping()) ); + } + return rec; +} + +/*! + \fn void QSignalMapper::mapped(int) + + This signal is emitted when map() is signaled from an object that + has an integer mapping set. + + \sa setMapping() +*/ + +/*! + \overload void QSignalMapper::mapped(const QString&) + + This signal is emitted when map() is signaled from an object that + has a string mapping set. + + \sa setMapping() +*/ +#endif //QT_NO_SIGNALMAPPER diff --git a/src/kernel/qsignalmapper.h b/src/kernel/qsignalmapper.h new file mode 100644 index 0000000..2588d90 --- /dev/null +++ b/src/kernel/qsignalmapper.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Definition of QSignalMapper class +** +** Created : 980503 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIGNALMAPPER_H +#define QSIGNALMAPPER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H +#ifndef QT_NO_SIGNALMAPPER +class QSignalMapperData; +struct QSignalMapperRec; + + +class Q_EXPORT QSignalMapper : public QObject { + Q_OBJECT +public: + QSignalMapper( QObject* parent, const char* name=0 ); + ~QSignalMapper(); + + virtual void setMapping( const QObject* sender, int identifier ); + virtual void setMapping( const QObject* sender, const QString &identifier ); + void removeMappings( const QObject* sender ); + +signals: + void mapped(int); + void mapped(const QString &); + +public slots: + void map(); + +private: + QSignalMapperData* d; + QSignalMapperRec* getRec( const QObject* ); + +private slots: + void removeMapping(); +}; + +#endif // QT_NO_SIGNALMAPPER +#endif // QSIGNALMAPPER_H diff --git a/src/kernel/qsignalslotimp.h b/src/kernel/qsignalslotimp.h new file mode 100644 index 0000000..ddb4857 --- /dev/null +++ b/src/kernel/qsignalslotimp.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Definition of signal/slot collections etc. +** +** Created : 980821 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIGNALSLOTIMP_H +#define QSIGNALSLOTIMP_H + +#ifndef QT_H +#include "qconnection.h" +#include "qptrlist.h" +#include "qptrvector.h" +#endif // QT_H + +class Q_EXPORT QConnectionList : public QPtrList<QConnection> +{ +public: + QConnectionList() : QPtrList<QConnection>() {} + QConnectionList( const QConnectionList &list ) : QPtrList<QConnection>(list) {} + ~QConnectionList() { clear(); } + QConnectionList &operator=(const QConnectionList &list) + { return (QConnectionList&)QPtrList<QConnection>::operator=(list); } +}; + +class Q_EXPORT QConnectionListIt : public QPtrListIterator<QConnection> +{ +public: + QConnectionListIt( const QConnectionList &l ) : QPtrListIterator<QConnection>(l) {} + QConnectionListIt &operator=(const QConnectionListIt &i) + { return (QConnectionListIt&)QPtrListIterator<QConnection>::operator=(i); } +}; + +#if defined(Q_TEMPLATEDLL) && defined(Q_CC_INTEL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrVector<QConnectionList>; +#define Q_EXPORTED_QPTRVECTORCONNECTTIONLIST_TEMPLATES +// MOC_SKIP_END +#endif + +class Q_EXPORT QSignalVec : public QPtrVector<QConnectionList> +{ +public: + QSignalVec(int size=17 ) + : QPtrVector<QConnectionList>(size) {} + QSignalVec( const QSignalVec &dict ) + : QPtrVector<QConnectionList>(dict) {} + ~QSignalVec() { clear(); } + QSignalVec &operator=(const QSignalVec &dict) + { return (QSignalVec&)QPtrVector<QConnectionList>::operator=(dict); } + QConnectionList* at( uint index ) const { + return index >= size()? 0 : QPtrVector<QConnectionList>::at(index); + } + bool insert( uint index, const QConnectionList* d ) { + if (index >= size() ) + resize( 2*index + 1); + return QPtrVector<QConnectionList>::insert(index, d); + } +}; + +#define Q_DEFINED_QCONNECTION_LIST +#include "qwinexport.h" +#endif // QSIGNALSLOTIMP_H diff --git a/src/kernel/qsimplerichtext.cpp b/src/kernel/qsimplerichtext.cpp new file mode 100644 index 0000000..9712b90 --- /dev/null +++ b/src/kernel/qsimplerichtext.cpp @@ -0,0 +1,421 @@ +/**************************************************************************** +** +** Implementation of the QSimpleRichText class +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsimplerichtext.h" + +#ifndef QT_NO_RICHTEXT +#include "qrichtext_p.h" +#include "qapplication.h" + +class QSimpleRichTextData +{ +public: + QTextDocument *doc; + QFont font; + int cachedWidth; + bool cachedWidthWithPainter; + void adjustSize(QPainter *p = 0); +}; + +// Pull this private function in from qglobal.cpp +extern unsigned int qt_int_sqrt( unsigned int n ); + +void QSimpleRichTextData::adjustSize(QPainter *p) { + QFontMetrics fm( font ); + int mw = fm.width( 'x' ) * 80; + int w = mw; + doc->doLayout(p, w); + if ( doc->widthUsed() != 0 ) { + w = qt_int_sqrt( 5 * doc->height() * doc->widthUsed() / 3 ); + doc->doLayout(p, QMIN(w, mw)); + + if ( w*3 < 5*doc->height() ) { + w = qt_int_sqrt( 2 * doc->height() * doc->widthUsed() ); + doc->doLayout(p, QMIN(w, mw)); + } + } + cachedWidth = doc->width(); + cachedWidthWithPainter = FALSE; +} + +/*! + \class QSimpleRichText qsimplerichtext.h + \brief The QSimpleRichText class provides a small displayable piece of rich text. + + \ingroup text + \mainclass + + This class encapsulates simple rich text usage in which a string + is interpreted as rich text and can be drawn. This is particularly + useful if you want to display some rich text in a custom widget. A + QStyleSheet is needed to interpret the tags and format the rich + text. Qt provides a default HTML-like style sheet, but you may + define custom style sheets. + + Once created, the rich text object can be queried for its width(), + height(), and the actual width used (see widthUsed()). Most + importantly, it can be drawn on any given QPainter with draw(). + QSimpleRichText can also be used to implement hypertext or active + text facilities by using anchorAt(). A hit test through inText() + makes it possible to use simple rich text for text objects in + editable drawing canvases. + + Once constructed from a string the contents cannot be changed, + only resized. If the contents change, just throw the rich text + object away and make a new one with the new contents. + + For large documents use QTextEdit or QTextBrowser. For very small + items of rich text you can use a QLabel. + + If you are using QSimpleRichText to print in high resolution you + should call setWidth(QPainter, int) so that the content will be + laid out properly on the page. +*/ + +/*! + Constructs a QSimpleRichText from the rich text string \a text and + the font \a fnt. + + The font is used as a basis for the text rendering. When using + rich text rendering on a widget \e w, you would normally specify + the widget's font, for example: + + \code + QSimpleRichText myrichtext( contents, mywidget->font() ); + \endcode + + \a context is the optional context of the rich text object. This + becomes important if \a text contains relative references, for + example within image tags. QSimpleRichText always uses the default + mime source factory (see \l{QMimeSourceFactory::defaultFactory()}) + to resolve those references. The context will then be used to + calculate the absolute path. See + QMimeSourceFactory::makeAbsolute() for details. + + The \a sheet is an optional style sheet. If it is 0, the default + style sheet will be used (see \l{QStyleSheet::defaultSheet()}). +*/ + +QSimpleRichText::QSimpleRichText( const QString& text, const QFont& fnt, + const QString& context, const QStyleSheet* sheet ) +{ + d = new QSimpleRichTextData; + d->cachedWidth = -1; + d->cachedWidthWithPainter = FALSE; + d->font = fnt; + d->doc = new QTextDocument( 0 ); + d->doc->setTextFormat( Qt::RichText ); + d->doc->setLeftMargin( 0 ); + d->doc->setRightMargin( 0 ); + d->doc->setFormatter( new QTextFormatterBreakWords ); + d->doc->setStyleSheet( (QStyleSheet*)sheet ); + d->doc->setDefaultFormat( fnt, QColor() ); + d->doc->setText( text, context ); +} + + +/*! + Constructs a QSimpleRichText from the rich text string \a text and + the font \a fnt. + + This is a slightly more complex constructor for QSimpleRichText + that takes an additional mime source factory \a factory, a page + break parameter \a pageBreak and a bool \a linkUnderline. \a + linkColor is only provided for compatibility, but has no effect, + as QColorGroup's QColorGroup::link() color is used now. + + \a context is the optional context of the rich text object. This + becomes important if \a text contains relative references, for + example within image tags. QSimpleRichText always uses the default + mime source factory (see \l{QMimeSourceFactory::defaultFactory()}) + to resolve those references. The context will then be used to + calculate the absolute path. See + QMimeSourceFactory::makeAbsolute() for details. + + The \a sheet is an optional style sheet. If it is 0, the default + style sheet will be used (see \l{QStyleSheet::defaultSheet()}). + + This constructor is useful for creating a QSimpleRichText object + suitable for printing. Set \a pageBreak to be the height of the + contents area of the pages. +*/ + +QSimpleRichText::QSimpleRichText( const QString& text, const QFont& fnt, + const QString& context, const QStyleSheet* sheet, + const QMimeSourceFactory* factory, int pageBreak, + const QColor& /*linkColor*/, bool linkUnderline ) +{ + d = new QSimpleRichTextData; + d->cachedWidth = -1; + d->cachedWidthWithPainter = FALSE; + d->font = fnt; + d->doc = new QTextDocument( 0 ); + d->doc->setTextFormat( Qt::RichText ); + d->doc->setFormatter( new QTextFormatterBreakWords ); + d->doc->setStyleSheet( (QStyleSheet*)sheet ); + d->doc->setDefaultFormat( fnt, QColor() ); + d->doc->flow()->setPageSize( pageBreak ); + d->doc->setPageBreakEnabled( TRUE ); +#ifndef QT_NO_MIME + d->doc->setMimeSourceFactory( (QMimeSourceFactory*)factory ); +#endif + d->doc->setUnderlineLinks( linkUnderline ); + d->doc->setText( text, context ); +} + +/*! + Destroys the rich text object, freeing memory. +*/ + +QSimpleRichText::~QSimpleRichText() +{ + delete d->doc; + delete d; +} + +/*! + \overload + + Sets the width of the rich text object to \a w pixels. + + \sa height(), adjustSize() +*/ + +void QSimpleRichText::setWidth( int w ) +{ + if ( w == d->cachedWidth && !d->cachedWidthWithPainter ) + return; + d->doc->formatter()->setAllowBreakInWords( d->doc->isPageBreakEnabled() ); + d->cachedWidth = w; + d->cachedWidthWithPainter = FALSE; + d->doc->doLayout( 0, w ); +} + +/*! + Sets the width of the rich text object to \a w pixels, + recalculating the layout as if it were to be drawn with painter \a + p. + + Passing a painter is useful when you intend drawing on devices + other than the screen, for example a QPrinter. + + \sa height(), adjustSize() +*/ + +void QSimpleRichText::setWidth( QPainter *p, int w ) +{ + if ( w == d->cachedWidth && d->cachedWidthWithPainter ) + return; + d->doc->formatter()->setAllowBreakInWords( d->doc->isPageBreakEnabled() || + p && p->device() && + p->device()->devType() == QInternal::Printer ); + p->save(); + d->cachedWidth = w; + d->cachedWidthWithPainter = TRUE; + d->doc->doLayout( p, w ); + p->restore(); +} + +/*! + Returns the set width of the rich text object in pixels. + + \sa widthUsed() +*/ + +int QSimpleRichText::width() const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + return d->doc->width(); +} + +/*! + Returns the width in pixels that is actually used by the rich text + object. This can be smaller or wider than the set width. + + It may be wider, for example, if the text contains images or + non-breakable words that are already wider than the available + space. It's smaller when the object only consists of lines that do + not fill the width completely. + + \sa width() +*/ + +int QSimpleRichText::widthUsed() const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + return d->doc->widthUsed(); +} + +/*! + Returns the height of the rich text object in pixels. + + \sa setWidth() +*/ + +int QSimpleRichText::height() const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + return d->doc->height(); +} + +/*! + Adjusts the richt text object to a reasonable size. + + \sa setWidth() +*/ + +void QSimpleRichText::adjustSize() +{ + d->adjustSize(); +} + +/*! + Draws the formatted text with painter \a p, at position (\a x, \a + y), clipped to \a clipRect. The clipping rectangle is given in the + rich text object's coordinates translated by (\a x, \a y). Passing + an null rectangle results in no clipping. Colors from the color + group \a cg are used as needed, and if not 0, \a *paper is used as + the background brush. + + Note that the display code is highly optimized to reduce flicker, + so passing a brush for \a paper is preferable to simply clearing + the area to be painted and then calling this without a brush. +*/ + +void QSimpleRichText::draw( QPainter *p, int x, int y, const QRect& clipRect, + const QColorGroup& cg, const QBrush* paper ) const +{ + p->save(); + if ( d->cachedWidth < 0 ) + d->adjustSize(p); + + QRect r = clipRect; + if ( !r.isNull() ) + r.moveBy( -x, -y ); + + if ( paper ) + d->doc->setPaper( new QBrush( *paper ) ); + QColorGroup g = cg; + if ( d->doc->paper() ) + g.setBrush( QColorGroup::Base, *d->doc->paper() ); + + if ( !clipRect.isNull() ) + p->setClipRect( clipRect, QPainter::CoordPainter ); + p->translate( x, y ); + d->doc->draw( p, r, g, paper ); + p->translate( -x, -y ); + p->restore(); +} + + +/*! \fn void QSimpleRichText::draw( QPainter *p, int x, int y, const QRegion& clipRegion, + const QColorGroup& cg, const QBrush* paper ) const + + \obsolete + + Use the version with clipRect instead. The region version has + problems with larger documents on some platforms (on X11 regions + internally are represented with 16bit coordinates). +*/ + + + +/*! + Returns the context of the rich text object. If no context has + been specified in the constructor, a null string is returned. The + context is the path to use to look up relative links, such as + image tags and anchor references. +*/ + +QString QSimpleRichText::context() const +{ + return d->doc->context(); +} + +/*! + Returns the anchor at the requested position, \a pos. An empty + string is returned if no anchor is specified for this position. +*/ + +QString QSimpleRichText::anchorAt( const QPoint& pos ) const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + QTextCursor c( d->doc ); + c.place( pos, d->doc->firstParagraph(), TRUE ); + return c.paragraph()->at( c.index() )->anchorHref(); +} + +/*! + Returns TRUE if \a pos is within a text line of the rich text + object; otherwise returns FALSE. +*/ + +bool QSimpleRichText::inText( const QPoint& pos ) const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + if ( pos.y() > d->doc->height() ) + return FALSE; + QTextCursor c( d->doc ); + c.place( pos, d->doc->firstParagraph() ); + return c.totalOffsetX() + c.paragraph()->at( c.index() )->x + + c.paragraph()->at( c.index() )->format()->width( c.paragraph()->at( c.index() )->c ) > pos.x(); +} + +/*! + Sets the default font for the rich text object to \a f +*/ + +void QSimpleRichText::setDefaultFont( const QFont &f ) +{ + if ( d->font == f ) + return; + d->font = f; + d->cachedWidth = -1; + d->cachedWidthWithPainter = FALSE; + d->doc->setDefaultFormat( f, QColor() ); + d->doc->setText( d->doc->originalText(), d->doc->context() ); +} + +#endif //QT_NO_RICHTEXT diff --git a/src/kernel/qsimplerichtext.h b/src/kernel/qsimplerichtext.h new file mode 100644 index 0000000..6874767 --- /dev/null +++ b/src/kernel/qsimplerichtext.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Definition of the QSimpleRichText class +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIMPLERICHTEXT_H +#define QSIMPLERICHTEXT_H + +#ifndef QT_H +#include "qnamespace.h" +#include "qstring.h" +#include "qregion.h" +#endif // QT_H + +#ifndef QT_NO_RICHTEXT + +class QPainter; +class QWidget; +class QStyleSheet; +class QBrush; +class QMimeSourceFactory; +class QSimpleRichTextData; + +class Q_EXPORT QSimpleRichText +{ +public: + QSimpleRichText( const QString& text, const QFont& fnt, + const QString& context = QString::null, const QStyleSheet* sheet = 0); + QSimpleRichText( const QString& text, const QFont& fnt, + const QString& context, const QStyleSheet* sheet, + const QMimeSourceFactory* factory, int pageBreak = -1, + const QColor& linkColor = Qt::blue, bool linkUnderline = TRUE ); + ~QSimpleRichText(); + + void setWidth( int ); + void setWidth( QPainter*, int ); + void setDefaultFont( const QFont &f ); + int width() const; + int widthUsed() const; + int height() const; + void adjustSize(); + + void draw( QPainter* p, int x, int y, const QRect& clipRect, + const QColorGroup& cg, const QBrush* paper = 0) const; + + // obsolete + void draw( QPainter* p, int x, int y, const QRegion& clipRegion, + const QColorGroup& cg, const QBrush* paper = 0) const { + draw( p, x, y, clipRegion.boundingRect(), cg, paper ); + } + + QString context() const; + QString anchorAt( const QPoint& pos ) const; + + bool inText( const QPoint& pos ) const; + +private: + QSimpleRichTextData* d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QSimpleRichText( const QSimpleRichText & ); + QSimpleRichText &operator=( const QSimpleRichText & ); +#endif +}; + +#endif // QT_NO_RICHTEXT + +#endif // QSIMPLERICHTEXT_H diff --git a/src/kernel/qsize.cpp b/src/kernel/qsize.cpp new file mode 100644 index 0000000..8dc25e4 --- /dev/null +++ b/src/kernel/qsize.cpp @@ -0,0 +1,432 @@ +/**************************************************************************** +** +** Implementation of QSize class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsize.h" +#include "qdatastream.h" + + +/*! + \class QSize + \brief The QSize class defines the size of a two-dimensional object. + + \ingroup images + \ingroup graphics + + A size is specified by a width and a height. + + The coordinate type is QCOORD (defined in \c <qwindowdefs.h> as \c int). + The minimum value of QCOORD is QCOORD_MIN (-2147483648) and the maximum + value is QCOORD_MAX (2147483647). + + The size can be set in the constructor and changed with setWidth() + and setHeight(), or using operator+=(), operator-=(), operator*=() + and operator/=(), etc. You can swap the width and height with + transpose(). You can get a size which holds the maximum height and + width of two sizes using expandedTo(), and the minimum height and + width of two sizes using boundedTo(). + + + \sa QPoint, QRect +*/ + + +/***************************************************************************** + QSize member functions + *****************************************************************************/ + +/*! + \fn QSize::QSize() + Constructs a size with invalid (negative) width and height. +*/ + +/*! + \fn QSize::QSize( int w, int h ) + Constructs a size with width \a w and height \a h. +*/ + +/*! + \fn bool QSize::isNull() const + Returns TRUE if the width is 0 and the height is 0; otherwise + returns FALSE. +*/ + +/*! + \fn bool QSize::isEmpty() const + Returns TRUE if the width is less than or equal to 0, or the height is + less than or equal to 0; otherwise returns FALSE. +*/ + +/*! + \fn bool QSize::isValid() const + Returns TRUE if the width is equal to or greater than 0 and the height is + equal to or greater than 0; otherwise returns FALSE. +*/ + +/*! + \fn int QSize::width() const + Returns the width. + \sa height() +*/ + +/*! + \fn int QSize::height() const + Returns the height. + \sa width() +*/ + +/*! + \fn void QSize::setWidth( int w ) + Sets the width to \a w. + \sa width(), setHeight() +*/ + +/*! + \fn void QSize::setHeight( int h ) + Sets the height to \a h. + \sa height(), setWidth() +*/ + +/*! + Swaps the values of width and height. +*/ + +void QSize::transpose() +{ + QCOORD tmp = wd; + wd = ht; + ht = tmp; +} + +/*! \enum QSize::ScaleMode + + This enum type defines the different ways of scaling a size. + + \img scaling.png + + \value ScaleFree The size is scaled freely. The ratio is not preserved. + \value ScaleMin The size is scaled to a rectangle as large as possible + inside a given rectangle, preserving the aspect ratio. + \value ScaleMax The size is scaled to a rectangle as small as possible + outside a given rectangle, preserving the aspect ratio. + + \sa QSize::scale(), QImage::scale(), QImage::smoothScale() +*/ + +/*! + Scales the size to a rectangle of width \a w and height \a h according + to the ScaleMode \a mode. + + \list + \i If \a mode is \c ScaleFree, the size is set to (\a w, \a h). + \i If \a mode is \c ScaleMin, the current size is scaled to a rectangle + as large as possible inside (\a w, \a h), preserving the aspect ratio. + \i If \a mode is \c ScaleMax, the current size is scaled to a rectangle + as small as possible outside (\a w, \a h), preserving the aspect ratio. + \endlist + + Example: + \code + QSize t1( 10, 12 ); + t1.scale( 60, 60, QSize::ScaleFree ); + // t1 is (60, 60) + + QSize t2( 10, 12 ); + t2.scale( 60, 60, QSize::ScaleMin ); + // t2 is (50, 60) + + QSize t3( 10, 12 ); + t3.scale( 60, 60, QSize::ScaleMax ); + // t3 is (60, 72) + \endcode +*/ +void QSize::scale( int w, int h, ScaleMode mode ) +{ + if ( mode == ScaleFree ) { + wd = (QCOORD)w; + ht = (QCOORD)h; + } else { + bool useHeight = TRUE; + int w0 = width(); + int h0 = height(); + int rw = h * w0 / h0; + + if ( mode == ScaleMin ) { + useHeight = ( rw <= w ); + } else { // mode == ScaleMax + useHeight = ( rw >= w ); + } + + if ( useHeight ) { + wd = (QCOORD)rw; + ht = (QCOORD)h; + } else { + wd = (QCOORD)w; + ht = (QCOORD)( w * h0 / w0 ); + } + } +} + +/*! + \overload + + Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode). +*/ +void QSize::scale( const QSize &s, ScaleMode mode ) +{ + scale( s.width(), s.height(), mode ); +} + +/*! + \fn QCOORD &QSize::rwidth() + Returns a reference to the width. + + Using a reference makes it possible to directly manipulate the width. + + Example: + \code + QSize s( 100, 10 ); + s.rwidth() += 20; // s becomes (120,10) + \endcode + + \sa rheight() +*/ + +/*! + \fn QCOORD &QSize::rheight() + Returns a reference to the height. + + Using a reference makes it possible to directly manipulate the height. + + Example: + \code + QSize s( 100, 10 ); + s.rheight() += 5; // s becomes (100,15) + \endcode + + \sa rwidth() +*/ + +/*! + \fn QSize &QSize::operator+=( const QSize &s ) + + Adds \a s to the size and returns a reference to this size. + + Example: + \code + QSize s( 3, 7 ); + QSize r( -1, 4 ); + s += r; // s becomes (2,11) +\endcode +*/ + +/*! + \fn QSize &QSize::operator-=( const QSize &s ) + + Subtracts \a s from the size and returns a reference to this size. + + Example: + \code + QSize s( 3, 7 ); + QSize r( -1, 4 ); + s -= r; // s becomes (4,3) + \endcode +*/ + +/*! + \fn QSize &QSize::operator*=( int c ) + Multiplies both the width and height by \a c and returns a reference to + the size. +*/ + +/*! + \overload QSize &QSize::operator*=( double c ) + + Multiplies both the width and height by \a c and returns a reference to + the size. + + Note that the result is truncated. +*/ + +/*! + \fn bool operator==( const QSize &s1, const QSize &s2 ) + \relates QSize + Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE. +*/ + +/*! + \fn bool operator!=( const QSize &s1, const QSize &s2 ) + \relates QSize + Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE. +*/ + +/*! + \fn const QSize operator+( const QSize &s1, const QSize &s2 ) + \relates QSize + Returns the sum of \a s1 and \a s2; each component is added separately. +*/ + +/*! + \fn const QSize operator-( const QSize &s1, const QSize &s2 ) + \relates QSize + Returns \a s2 subtracted from \a s1; each component is + subtracted separately. +*/ + +/*! + \fn const QSize operator*( const QSize &s, int c ) + \relates QSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \overload const QSize operator*( int c, const QSize &s ) + \relates QSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \overload const QSize operator*( const QSize &s, double c ) + \relates QSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \overload const QSize operator*( double c, const QSize &s ) + \relates QSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \fn QSize &QSize::operator/=( int c ) + Divides both the width and height by \a c and returns a reference to the + size. +*/ + +/*! + \fn QSize &QSize::operator/=( double c ) + \overload + Divides both the width and height by \a c and returns a reference to the + size. + + Note that the result is truncated. +*/ + +/*! + \fn const QSize operator/( const QSize &s, int c ) + \relates QSize + Divides \a s by \a c and returns the result. +*/ + +/*! + \fn const QSize operator/( const QSize &s, double c ) + \relates QSize + \overload + Divides \a s by \a c and returns the result. + + Note that the result is truncated. +*/ + +/*! + \fn QSize QSize::expandedTo( const QSize & otherSize ) const + + Returns a size with the maximum width and height of this size and + \a otherSize. +*/ + +/*! + \fn QSize QSize::boundedTo( const QSize & otherSize ) const + + Returns a size with the minimum width and height of this size and + \a otherSize. +*/ + + +void QSize::warningDivByZero() +{ +#if defined(QT_CHECK_MATH) + qWarning( "QSize: Division by zero error" ); +#endif +} + + +/***************************************************************************** + QSize stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QSize + Writes the size \a sz to the stream \a s and returns a reference to + the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QSize &sz ) +{ + if ( s.version() == 1 ) + s << (Q_INT16)sz.width() << (Q_INT16)sz.height(); + else + s << (Q_INT32)sz.width() << (Q_INT32)sz.height(); + return s; +} + +/*! + \relates QSize + Reads the size from the stream \a s into size \a sz and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QSize &sz ) +{ + if ( s.version() == 1 ) { + Q_INT16 w, h; + s >> w; sz.rwidth() = w; + s >> h; sz.rheight() = h; + } + else { + Q_INT32 w, h; + s >> w; sz.rwidth() = w; + s >> h; sz.rheight() = h; + } + return s; +} +#endif // QT_NO_DATASTREAM diff --git a/src/kernel/qsize.h b/src/kernel/qsize.h new file mode 100644 index 0000000..f964bcd --- /dev/null +++ b/src/kernel/qsize.h @@ -0,0 +1,237 @@ +/**************************************************************************** +** +** Definition of QSize class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIZE_H +#define QSIZE_H + +#ifndef QT_H +#include "qpoint.h" // ### change to qwindowdefs.h? +#endif // QT_H + +class Q_EXPORT QSize +// ### Make QSize inherit Qt in Qt 4.0 +{ +public: + // ### Move this enum to qnamespace.h in Qt 4.0 + enum ScaleMode { + ScaleFree, + ScaleMin, + ScaleMax + }; + + QSize(); + QSize( int w, int h ); + + bool isNull() const; + bool isEmpty() const; + bool isValid() const; + + int width() const; + int height() const; + void setWidth( int w ); + void setHeight( int h ); + void transpose(); + + void scale( int w, int h, ScaleMode mode ); + void scale( const QSize &s, ScaleMode mode ); + + QSize expandedTo( const QSize & ) const; + QSize boundedTo( const QSize & ) const; + + QCOORD &rwidth(); + QCOORD &rheight(); + + QSize &operator+=( const QSize & ); + QSize &operator-=( const QSize & ); + QSize &operator*=( int c ); + QSize &operator*=( double c ); + QSize &operator/=( int c ); + QSize &operator/=( double c ); + + friend inline bool operator==( const QSize &, const QSize & ); + friend inline bool operator!=( const QSize &, const QSize & ); + friend inline const QSize operator+( const QSize &, const QSize & ); + friend inline const QSize operator-( const QSize &, const QSize & ); + friend inline const QSize operator*( const QSize &, int ); + friend inline const QSize operator*( int, const QSize & ); + friend inline const QSize operator*( const QSize &, double ); + friend inline const QSize operator*( double, const QSize & ); + friend inline const QSize operator/( const QSize &, int ); + friend inline const QSize operator/( const QSize &, double ); + +private: + static void warningDivByZero(); + + QCOORD wd; + QCOORD ht; +}; + + +/***************************************************************************** + QSize stream functions + *****************************************************************************/ + +Q_EXPORT QDataStream &operator<<( QDataStream &, const QSize & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QSize & ); + + +/***************************************************************************** + QSize inline functions + *****************************************************************************/ + +inline QSize::QSize() +{ wd = ht = -1; } + +inline QSize::QSize( int w, int h ) +{ wd=(QCOORD)w; ht=(QCOORD)h; } + +inline bool QSize::isNull() const +{ return wd==0 && ht==0; } + +inline bool QSize::isEmpty() const +{ return wd<1 || ht<1; } + +inline bool QSize::isValid() const +{ return wd>=0 && ht>=0; } + +inline int QSize::width() const +{ return wd; } + +inline int QSize::height() const +{ return ht; } + +inline void QSize::setWidth( int w ) +{ wd=(QCOORD)w; } + +inline void QSize::setHeight( int h ) +{ ht=(QCOORD)h; } + +inline QCOORD &QSize::rwidth() +{ return wd; } + +inline QCOORD &QSize::rheight() +{ return ht; } + +inline QSize &QSize::operator+=( const QSize &s ) +{ wd+=s.wd; ht+=s.ht; return *this; } + +inline QSize &QSize::operator-=( const QSize &s ) +{ wd-=s.wd; ht-=s.ht; return *this; } + +inline QSize &QSize::operator*=( int c ) +{ wd*=(QCOORD)c; ht*=(QCOORD)c; return *this; } + +inline QSize &QSize::operator*=( double c ) +{ wd=(QCOORD)(wd*c); ht=(QCOORD)(ht*c); return *this; } + +inline bool operator==( const QSize &s1, const QSize &s2 ) +{ return s1.wd == s2.wd && s1.ht == s2.ht; } + +inline bool operator!=( const QSize &s1, const QSize &s2 ) +{ return s1.wd != s2.wd || s1.ht != s2.ht; } + +inline const QSize operator+( const QSize & s1, const QSize & s2 ) +{ return QSize(s1.wd+s2.wd, s1.ht+s2.ht); } + +inline const QSize operator-( const QSize &s1, const QSize &s2 ) +{ return QSize(s1.wd-s2.wd, s1.ht-s2.ht); } + +inline const QSize operator*( const QSize &s, int c ) +{ return QSize(s.wd*c, s.ht*c); } + +inline const QSize operator*( int c, const QSize &s ) +{ return QSize(s.wd*c, s.ht*c); } + +inline const QSize operator*( const QSize &s, double c ) +{ return QSize((QCOORD)(s.wd*c), (QCOORD)(s.ht*c)); } + +inline const QSize operator*( double c, const QSize &s ) +{ return QSize((QCOORD)(s.wd*c), (QCOORD)(s.ht*c)); } + +inline QSize &QSize::operator/=( int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + warningDivByZero(); +#endif + wd/=(QCOORD)c; ht/=(QCOORD)c; + return *this; +} + +inline QSize &QSize::operator/=( double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + warningDivByZero(); +#endif + wd=(QCOORD)(wd/c); ht=(QCOORD)(ht/c); + return *this; +} + +inline const QSize operator/( const QSize &s, int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + QSize::warningDivByZero(); +#endif + return QSize(s.wd/c, s.ht/c); +} + +inline const QSize operator/( const QSize &s, double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + QSize::warningDivByZero(); +#endif + return QSize((QCOORD)(s.wd/c), (QCOORD)(s.ht/c)); +} + +inline QSize QSize::expandedTo( const QSize & otherSize ) const +{ + return QSize( QMAX(wd,otherSize.wd), QMAX(ht,otherSize.ht) ); +} + +inline QSize QSize::boundedTo( const QSize & otherSize ) const +{ + return QSize( QMIN(wd,otherSize.wd), QMIN(ht,otherSize.ht) ); +} + + +#endif // QSIZE_H diff --git a/src/kernel/qsizegrip.cpp b/src/kernel/qsizegrip.cpp new file mode 100644 index 0000000..61ed6ae --- /dev/null +++ b/src/kernel/qsizegrip.cpp @@ -0,0 +1,286 @@ +/**************************************************************************** +** +** Implementation of QSizeGrip class +** +** Created : 980119 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsizegrip.h" + +#ifndef QT_NO_SIZEGRIP + +#include "qpainter.h" +#include "qapplication.h" +#include "qstyle.h" + +#if defined(Q_WS_X11) +#include "qt_x11_p.h" +extern Atom qt_sizegrip; // defined in qapplication_x11.cpp +#elif defined (Q_WS_WIN ) +#include "qobjectlist.h" +#include "qt_windows.h" +#elif defined(Q_WS_MAC) +bool qt_mac_update_sizer(QWidget *, int); //qwidget_mac.cpp +#endif + + +static QWidget *qt_sizegrip_topLevelWidget( QWidget* w) +{ + QWidget *p = w->parentWidget(); + while ( !w->isTopLevel() && p && !p->inherits("QWorkspace") ) { + w = p; + p = p->parentWidget(); + } + return w; +} + +static QWidget* qt_sizegrip_workspace( QWidget* w ) +{ + while ( w && !w->inherits("QWorkspace" ) ) { + if ( w->isTopLevel() ) + return 0; + w = w->parentWidget(); + } + return w; +} + + +/*! + \class QSizeGrip qsizegrip.h + + \brief The QSizeGrip class provides a corner-grip for resizing a top-level window. + + \ingroup application + \ingroup basic + \ingroup appearance + + This widget works like the standard Windows resize handle. In the + X11 version this resize handle generally works differently from + the one provided by the system; we hope to reduce this difference + in the future. + + Put this widget anywhere in a widget tree and the user can use it + to resize the top-level window. Generally, this should be in the + lower right-hand corner. Note that QStatusBar already uses this + widget, so if you have a status bar (e.g. you are using + QMainWindow), then you don't need to use this widget explicitly. + + <img src=qsizegrip-m.png> <img src=qsizegrip-w.png> + + \sa QStatusBar +*/ + + +/*! + Constructs a resize corner called \a name, as a child widget of \a + parent. +*/ +QSizeGrip::QSizeGrip( QWidget * parent, const char* name ) + : QWidget( parent, name ) +{ +#ifndef QT_NO_CURSOR +#ifndef Q_WS_MAC + if ( QApplication::reverseLayout() ) + setCursor( sizeBDiagCursor ); + else + setCursor( sizeFDiagCursor ); +#endif +#endif + setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) ); +#if defined(Q_WS_X11) + if ( !qt_sizegrip_workspace( this ) ) { + WId id = winId(); + XChangeProperty(qt_xdisplay(), topLevelWidget()->winId(), + qt_sizegrip, XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&id, 1); + } +#endif + tlw = qt_sizegrip_topLevelWidget( this ); + if ( tlw ) + tlw->installEventFilter( this ); + installEventFilter( this ); //for binary compatibility fix in 4.0 with an event() ### +} + + +/*! + Destroys the size grip. +*/ +QSizeGrip::~QSizeGrip() +{ +#if defined(Q_WS_X11) + if ( !QApplication::closingDown() && parentWidget() ) { + WId id = None; + XChangeProperty(qt_xdisplay(), topLevelWidget()->winId(), + qt_sizegrip, XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&id, 1); + } +#endif +} + +/*! + Returns the size grip's size hint; this is a small size. +*/ +QSize QSizeGrip::sizeHint() const +{ + return (style().sizeFromContents(QStyle::CT_SizeGrip, this, QSize(13, 13)). + expandedTo(QApplication::globalStrut())); +} + +/*! + Paints the resize grip. Resize grips are usually rendered as small + diagonal textured lines in the lower-right corner. The event is in + \a e. +*/ +void QSizeGrip::paintEvent( QPaintEvent *e ) +{ + QPainter painter( this ); + painter.setClipRegion(e->region()); + style().drawPrimitive(QStyle::PE_SizeGrip, &painter, rect(), colorGroup()); +} + +/*! + Primes the resize operation. The event is in \a e. +*/ +void QSizeGrip::mousePressEvent( QMouseEvent * e ) +{ + p = e->globalPos(); + s = qt_sizegrip_topLevelWidget(this)->size(); +} + + +/*! + Resizes the top-level widget containing this widget. The event is + in \a e. +*/ +void QSizeGrip::mouseMoveEvent( QMouseEvent * e ) +{ + if ( e->state() != LeftButton ) + return; + + QWidget* tlw = qt_sizegrip_topLevelWidget(this); + if ( tlw->testWState(WState_ConfigPending) ) + return; + + QPoint np( e->globalPos() ); + + QWidget* ws = qt_sizegrip_workspace( this ); + if ( ws ) { + QPoint tmp( ws->mapFromGlobal( np ) ); + if ( tmp.x() > ws->width() ) + tmp.setX( ws->width() ); + if ( tmp.y() > ws->height() ) + tmp.setY( ws->height() ); + np = ws->mapToGlobal( tmp ); + } + + int w; + int h = np.y() - p.y() + s.height(); + + if ( QApplication::reverseLayout() ) + w = s.width() - ( np.x() - p.x() ); + else + w = np.x() - p.x() + s.width(); + + if ( w < 1 ) + w = 1; + if ( h < 1 ) + h = 1; + QSize ms( tlw->minimumSizeHint() ); + ms = ms.expandedTo( minimumSize() ); + if ( w < ms.width() ) + w = ms.width(); + if ( h < ms.height() ) + h = ms.height(); + + if (QApplication::reverseLayout()) { + tlw->resize( w, h ); + if (tlw->size() == QSize(w,h)) + tlw->move( tlw->x() + ( np.x()-p.x() ), tlw->y() ); + } else { + tlw->resize( w, h ); + } +#ifdef Q_WS_WIN + MSG msg; + while( PeekMessage( &msg, winId(), WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE ) ) + ; +#endif + QApplication::syncX(); + + if ( QApplication::reverseLayout() && tlw->size() == QSize(w,h) ) { + s.rwidth() = tlw->size().width(); + p.rx() = np.x(); + } +} + +/*! \reimp */ +bool QSizeGrip::eventFilter( QObject *o, QEvent *e ) +{ + if ( o == tlw ) { + switch ( e->type() ) { +#ifndef Q_WS_MAC + /* The size grip goes no where on Mac OS X when you maximize! --Sam */ + case QEvent::ShowMaximized: +#endif + case QEvent::ShowFullScreen: + hide(); + break; + case QEvent::ShowNormal: + show(); + break; + default: + break; + } + } else if(o == this) { +#if defined(Q_WS_MAC) + switch(e->type()) { + case QEvent::Hide: + case QEvent::Show: + if(!QApplication::closingDown() && parentWidget() && !qt_sizegrip_workspace(this)) { + if(QWidget *w = qt_sizegrip_topLevelWidget(this)) { + if(w->isTopLevel()) + qt_mac_update_sizer(w, e->type() == QEvent::Hide ? -1 : 1); + } + } + break; + default: + break; + } + #endif + } + return FALSE; +} + +#endif //QT_NO_SIZEGRIP diff --git a/src/kernel/qsizegrip.h b/src/kernel/qsizegrip.h new file mode 100644 index 0000000..b7e010a --- /dev/null +++ b/src/kernel/qsizegrip.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Definition of QSizeGrip class +** +** Created : 980316 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIZEGRIP_H +#define QSIZEGRIP_H + +#ifndef QT_H +#include "qwidget.h" +#endif // QT_H + +#ifndef QT_NO_SIZEGRIP + +class Q_EXPORT QSizeGrip: public QWidget +{ + Q_OBJECT +public: + QSizeGrip( QWidget* parent, const char* name=0 ); + ~QSizeGrip(); + + QSize sizeHint() const; + +protected: + void paintEvent( QPaintEvent * ); + void mousePressEvent( QMouseEvent * ); + void mouseMoveEvent( QMouseEvent * ); + + bool eventFilter( QObject *, QEvent * ); + +private: + QPoint p; + QSize s; + int d; + QWidget *tlw; +}; + +#endif //QT_NO_SIZEGRIP +#endif diff --git a/src/kernel/qsizepolicy.h b/src/kernel/qsizepolicy.h new file mode 100644 index 0000000..38011bb --- /dev/null +++ b/src/kernel/qsizepolicy.h @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Definition of the QSizePolicy class +** +** Created : 980929 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSIZEPOLICY_H +#define QSIZEPOLICY_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +// Documentation is in qabstractlayout.cpp. + +class Q_EXPORT QSizePolicy +{ +private: + enum SizePolicy_Internal { HSize = 6, HMask = 0x3f, VMask = HMask << HSize, + MayGrow = 1, ExpMask = 2, MayShrink = 4 }; +public: + enum SizeType { Fixed = 0, + Minimum = MayGrow, + Maximum = MayShrink, + Preferred = MayGrow | MayShrink, + MinimumExpanding = MayGrow | ExpMask, + Expanding = MayGrow | MayShrink | ExpMask, + Ignored = ExpMask /* magic value */ }; + + enum ExpandData { NoDirection = 0, + Horizontally = 1, + Vertically = 2, +#ifndef QT_NO_COMPAT + Horizontal = Horizontally, + Vertical = Vertically, +#endif + BothDirections = Horizontally | Vertically }; + + QSizePolicy() : data( 0 ) { } + + QSizePolicy( SizeType hor, SizeType ver, bool hfw = FALSE ) + : data( hor | (ver<<HSize) | (hfw ? (Q_UINT32)(1<<2*HSize) : 0) ) { } + QSizePolicy( SizeType hor, SizeType ver, uchar hors, uchar vers, bool hfw = FALSE ); + + SizeType horData() const { return (SizeType)( data & HMask ); } + SizeType verData() const { return (SizeType)( (data & VMask) >> HSize ); } + + bool mayShrinkHorizontally() const { return horData() & MayShrink || horData() == Ignored; } + bool mayShrinkVertically() const { return verData() & MayShrink || verData() == Ignored; } + bool mayGrowHorizontally() const { return horData() & MayGrow || horData() == Ignored; } + bool mayGrowVertically() const { return verData() & MayGrow || verData() == Ignored; } + + ExpandData expanding() const + { + return (ExpandData)( (int)(verData() & ExpMask ? Vertically : 0) | + (int)(horData() & ExpMask ? Horizontally : 0) ); + } + + void setHorData( SizeType d ) { data = (Q_UINT32)(data & ~HMask) | d; } + void setVerData( SizeType d ) { data = (Q_UINT32)(data & ~(HMask << HSize)) | + (d << HSize); } + + void setHeightForWidth( bool b ) { data = b ? (Q_UINT32)( data | ( 1 << 2*HSize ) ) + : (Q_UINT32)( data & ~( 1 << 2*HSize ) ); } + bool hasHeightForWidth() const { return data & ( 1 << 2*HSize ); } + + bool operator==( const QSizePolicy& s ) const { return data == s.data; } + bool operator!=( const QSizePolicy& s ) const { return data != s.data; } + + + uint horStretch() const { return data >> 24; } + uint verStretch() const { return (data >> 16) & 0xff; } + void setHorStretch( uchar sf ) { data = (data&0x00ffffff) | (uint(sf)<<24); } + void setVerStretch( uchar sf ) { data = (data&0xff00ffff) | (uint(sf)<<16); } + inline void transpose(); + +private: + QSizePolicy( int i ) : data( (Q_UINT32)i ) { } + + Q_UINT32 data; +}; + +inline QSizePolicy::QSizePolicy( SizeType hor, SizeType ver, uchar hors, uchar vers, bool hfw ) + : data( hor | (ver<<HSize) | (hfw ? (Q_UINT32)(1<<2*HSize) : 0) ) { + setHorStretch( hors ); + setVerStretch( vers ); +} + +inline void QSizePolicy::transpose() { + *this = QSizePolicy( verData(), horData(), verStretch(), horStretch(), + hasHeightForWidth() ); +} + +#endif // QSIZEPOLICY_H diff --git a/src/kernel/qsocketnotifier.cpp b/src/kernel/qsocketnotifier.cpp new file mode 100644 index 0000000..a5cdbbd --- /dev/null +++ b/src/kernel/qsocketnotifier.cpp @@ -0,0 +1,265 @@ +/**************************************************************************** +** +** Implementation of QSocketNotifier class +** +** Created : 951114 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsocketnotifier.h" +#include "qapplication.h" +#include "qevent.h" +#include "qeventloop.h" +#include "qplatformdefs.h" + +#if defined(Q_OS_UNIX) +#include <sys/types.h> +#endif + + +/*! + \class QSocketNotifier qsocketnotifier.h + \brief The QSocketNotifier class provides support for socket callbacks. + + \ingroup io + + This class makes it possible to write asynchronous socket-based + code in Qt. Using synchronous socket operations blocks the + program, which is clearly not acceptable for an event-driven GUI + program. + + Once you have opened a non-blocking socket (whether for TCP, UDP, + a UNIX-domain socket, or any other protocol family your operating + system supports), you can create a socket notifier to monitor the + socket. Then you connect the activated() signal to the slot you + want to be called when a socket event occurs. + + Note for Windows users: the socket passed to QSocketNotifier will + become non-blocking, even if it was created as a blocking socket. + + There are three types of socket notifiers (read, write and + exception); you must specify one of these in the constructor. + + The type specifies when the activated() signal is to be emitted: + \list 1 + \i QSocketNotifier::Read - There is data to be read (socket read event). + \i QSocketNotifier::Write - Data can be written (socket write event). + \i QSocketNofifier::Exception - An exception has occurred (socket + exception event). We recommend against using this. + \endlist + + For example, if you need to monitor both reads and writes for the + same socket you must create two socket notifiers. + + For read notifiers it makes little sense to connect the + activated() signal to more than one slot because the data can be + read from the socket only once. + + Also observe that if you do not read all the available data when + the read notifier fires, it fires again and again. + + For write notifiers, immediately disable the notifier after the + activated() signal has been received and you have sent the data to + be written on the socket. When you have more data to be written, + enable it again to get a new activated() signal. The exception is + if the socket data writing operation (send() or equivalent) fails + with a "would block" error, which means that some buffer is full + and you must wait before sending more data. In that case you do + not need to disable and re-enable the write notifier; it will fire + again as soon as the system allows more data to be sent. + + The behavior of a write notifier that is left in enabled state + after having emitting the first activated() signal (and no "would + block" error has occurred) is undefined. Depending on the + operating system, it may fire on every pass of the event loop or + not at all. + + If you need a time-out for your sockets you can use either \link + QObject::startTimer() timer events\endlink or the QTimer class. + + Socket action is detected in the \link QApplication::exec() main + event loop\endlink of Qt. The X11 version of Qt has a single UNIX + select() call that incorporates all socket notifiers and the X + socket. + + Note that on XFree86 for OS/2, select() works only in the thread + in which main() is running; you should therefore use that thread + for GUI operations. + + \sa QSocket, QServerSocket, QSocketDevice, QFile::handle() +*/ + +/*! + \enum QSocketNotifier::Type + + \value Read + \value Write + \value Exception +*/ + + +/*! + Constructs a socket notifier called \a name, with the parent, \a + parent. It watches \a socket for \a type events, and enables it. + + It is generally advisable to explicitly enable or disable the + socket notifier, especially for write notifiers. + + \sa setEnabled(), isEnabled() +*/ + +QSocketNotifier::QSocketNotifier( int socket, Type type, QObject *parent, + const char *name ) + : QObject( parent, name ) +{ +#if defined(QT_CHECK_RANGE) + if ( socket < 0 ) + qWarning( "QSocketNotifier: Invalid socket specified" ); +# if defined(Q_OS_UNIX) + if ( socket >= FD_SETSIZE ) + qWarning( "QSocketNotifier: Socket descriptor too large for select()" ); +# endif +#endif + sockfd = socket; + sntype = type; + snenabled = TRUE; + QApplication::eventLoop()->registerSocketNotifier( this ); +} + +/*! + Destroys the socket notifier. +*/ + +QSocketNotifier::~QSocketNotifier() +{ + setEnabled( FALSE ); +} + + +/*! + \fn void QSocketNotifier::activated( int socket ) + + This signal is emitted under certain conditions specified by the + notifier type(): + \list 1 + \i QSocketNotifier::Read - There is data to be read (socket read event). + \i QSocketNotifier::Write - Data can be written (socket write event). + \i QSocketNofifier::Exception - An exception has occurred (socket + exception event). + \endlist + + The \a socket argument is the \link socket() socket\endlink identifier. + + \sa type(), socket() +*/ + + +/*! + \fn int QSocketNotifier::socket() const + + Returns the socket identifier specified to the constructor. + + \sa type() +*/ + +/*! + \fn Type QSocketNotifier::type() const + + Returns the socket event type specified to the constructor: \c + QSocketNotifier::Read, \c QSocketNotifier::Write, or \c + QSocketNotifier::Exception. + + \sa socket() +*/ + + +/*! + \fn bool QSocketNotifier::isEnabled() const + + Returns TRUE if the notifier is enabled; otherwise returns FALSE. + + \sa setEnabled() +*/ + +/*! + Enables the notifier if \a enable is TRUE or disables it if \a + enable is FALSE. + + The notifier is enabled by default. + + If the notifier is enabled, it emits the activated() signal + whenever a socket event corresponding to its \link type() + type\endlink occurs. If it is disabled, it ignores socket events + (the same effect as not creating the socket notifier). + + Write notifiers should normally be disabled immediately after the + activated() signal has been emitted; see discussion of write + notifiers in the \link #details class description\endlink above. + + \sa isEnabled(), activated() +*/ + +void QSocketNotifier::setEnabled( bool enable ) +{ + if ( sockfd < 0 ) + return; + if ( snenabled == enable ) // no change + return; + snenabled = enable; + + QEventLoop *eventloop = QApplication::eventLoop(); + if ( ! eventloop ) // perhaps application is shutting down + return; + + if ( snenabled ) + eventloop->registerSocketNotifier( this ); + else + eventloop->unregisterSocketNotifier( this ); +} + + +/*!\reimp +*/ +bool QSocketNotifier::event( QEvent *e ) +{ + // Emits the activated() signal when a \c QEvent::SockAct is + // received. + QObject::event( e ); // will activate filters + if ( e->type() == QEvent::SockAct ) { + emit activated( sockfd ); + return TRUE; + } + return FALSE; +} diff --git a/src/kernel/qsocketnotifier.h b/src/kernel/qsocketnotifier.h new file mode 100644 index 0000000..199d937 --- /dev/null +++ b/src/kernel/qsocketnotifier.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Definition of QSocketNotifier class +** +** Created : 951114 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSOCKETNOTIFIER_H +#define QSOCKETNOTIFIER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + + +class Q_EXPORT QSocketNotifier : public QObject +{ + Q_OBJECT +public: + enum Type { Read, Write, Exception }; + + QSocketNotifier( int socket, Type, QObject *parent=0, const char *name=0 ); + ~QSocketNotifier(); + + int socket() const; + Type type() const; + + bool isEnabled() const; + virtual void setEnabled( bool ); + +signals: + void activated( int socket ); + +protected: + bool event( QEvent * ); + +private: + int sockfd; + Type sntype; + bool snenabled; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QSocketNotifier( const QSocketNotifier & ); + QSocketNotifier &operator=( const QSocketNotifier & ); +#endif +}; + + +inline int QSocketNotifier::socket() const +{ return sockfd; } + +inline QSocketNotifier::Type QSocketNotifier::type() const +{ return sntype; } + +inline bool QSocketNotifier::isEnabled() const +{ return snenabled; } + + +#endif // QSOCKETNOTIFIER_H diff --git a/src/kernel/qsound.cpp b/src/kernel/qsound.cpp new file mode 100644 index 0000000..1079df4 --- /dev/null +++ b/src/kernel/qsound.cpp @@ -0,0 +1,316 @@ +/**************************************************************************** +** +** Implementation of QSound class and QAuServer internal class +** +** Created : 000117 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qsound.h" + +#ifndef QT_NO_SOUND + +#include "qptrlist.h" + +static QPtrList<QAuServer> *servers=0; + +QAuServer::QAuServer(QObject* parent, const char* name) : + QObject(parent,name) +{ + if ( !servers ) { + servers = new QPtrList<QAuServer>; + // ### add cleanup + } + servers->prepend(this); +} + +QAuServer::~QAuServer() +{ + servers->remove(this); + if ( servers->count() == 0 ) { + delete servers; + servers = 0; + } +} + +void QAuServer::play(const QString& filename) +{ + QSound s(filename); + play(&s); +} + +extern QAuServer* qt_new_audio_server(); + +static QAuServer& server() +{ + if (!servers) qt_new_audio_server(); + return *servers->first(); +} + +class QSoundData { +public: + QSoundData(const QString& fname) : + filename(fname), bucket(0), looprem(0), looptotal(1) + { + } + + ~QSoundData() + { + delete bucket; + } + + QString filename; + QAuBucket* bucket; + int looprem; + int looptotal; +}; + +/*! + \class QSound qsound.h + \brief The QSound class provides access to the platform audio facilities. + + \ingroup multimedia + \mainclass + + Qt provides the most commonly required audio operation in GUI + applications: asynchronously playing a sound file. This is most + easily accomplished with a single call: + \code + QSound::play("mysounds/bells.wav"); + \endcode + + A second API is provided in which a QSound object is created from + a sound file and is played later: + \code + QSound bells("mysounds/bells.wav"); + + bells.play(); + \endcode + + Sounds played using the second model may use more memory but play + more immediately than sounds played using the first model, + depending on the underlying platform audio facilities. + + On Microsoft Windows the underlying multimedia system is used; + only WAVE format sound files are supported. + + On X11 the \link ftp://ftp.x.org/contrib/audio/nas/ Network Audio + System\endlink is used if available, otherwise all operations work + silently. NAS supports WAVE and AU files. + + On Macintosh, ironically, we use QT (\link + http://quicktime.apple.com QuickTime\endlink) for sound, this + means all QuickTime formats are supported by Qt/Mac. + + On Qt/Embedded, a built-in mixing sound server is used, which + accesses \c /dev/dsp directly. Only the WAVE format is supported. + + The availability of sound can be tested with + QSound::isAvailable(). +*/ + +/*! + \fn static bool QSound::available() + + Returns TRUE if sound support is available; otherwise returns FALSE. +*/ + +/*! + Plays the sound in a file called \a filename. +*/ +void QSound::play(const QString& filename) +{ + server().play(filename); +} + +/*! + Constructs a QSound that can quickly play the sound in a file + named \a filename. + + This may use more memory than the static \c play function. + + The \a parent and \a name arguments (default 0) are passed on to + the QObject constructor. +*/ +QSound::QSound(const QString& filename, QObject* parent, const char* name) : + QObject(parent,name), + d(new QSoundData(filename)) +{ + server().init(this); +} + +/*! + Destroys the sound object. If the sound is not finished playing stop() is called on it. + + \sa stop() isFinished() +*/ +QSound::~QSound() +{ + if ( !isFinished() ) + stop(); + delete d; +} + +/*! + Returns TRUE if the sound has finished playing; otherwise returns FALSE. + + \warning On Windows this function always returns TRUE for unlooped sounds. +*/ +bool QSound::isFinished() const +{ + return d->looprem == 0; +} + +/*! + \overload + + Starts the sound playing. The function returns immediately. + Depending on the platform audio facilities, other sounds may stop + or may be mixed with the new sound. + + The sound can be played again at any time, possibly mixing or + replacing previous plays of the sound. +*/ +void QSound::play() +{ + d->looprem = d->looptotal; + server().play(this); +} + +/*! + Returns the number of times the sound will play. +*/ +int QSound::loops() const +{ + return d->looptotal; +} + +/*! + Returns the number of times the sound will loop. This value + decreases each time the sound loops. +*/ +int QSound::loopsRemaining() const +{ + return d->looprem; +} + +/*! + Sets the sound to repeat \a l times when it is played. Passing the + value -1 will cause the sound to loop indefinitely. + + \sa loops() +*/ +void QSound::setLoops(int l) +{ + d->looptotal = l; +} + +/*! + Returns the filename associated with the sound. +*/ +QString QSound::fileName() const +{ + return d->filename; +} + +/*! + Stops the sound playing. + + On Windows the current loop will finish if a sound is played + in a loop. + + \sa play() +*/ +void QSound::stop() +{ + server().stop(this); +} + + +/*! + Returns TRUE if sound facilities exist on the platform; otherwise + returns FALSE. An application may choose either to notify the user + if sound is crucial to the application or to operate silently + without bothering the user. + + If no sound is available, all QSound operations work silently and + quickly. +*/ +bool QSound::isAvailable() +{ + return server().okay(); +} + +/*! + Sets the internal bucket record of sound \a s to \a b, deleting + any previous setting. +*/ +void QAuServer::setBucket(QSound* s, QAuBucket* b) +{ + delete s->d->bucket; + s->d->bucket = b; +} + +/*! + Returns the internal bucket record of sound \a s. +*/ +QAuBucket* QAuServer::bucket(QSound* s) +{ + return s->d->bucket; +} + +/*! + Decrements the QSound::loopRemaining() value for sound \a s, + returning the result. +*/ +int QAuServer::decLoop(QSound* s) +{ + if ( s->d->looprem > 0 ) + --s->d->looprem; + return s->d->looprem; +} + +/*! + Initializes the sound. The default implementation does nothing. +*/ +void QAuServer::init(QSound*) +{ +} + +QAuBucket::~QAuBucket() +{ +} + +#endif // QT_NO_SOUND diff --git a/src/kernel/qsound.h b/src/kernel/qsound.h new file mode 100644 index 0000000..df5220b --- /dev/null +++ b/src/kernel/qsound.h @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Definition of QSound class and QAuServer internal class +** +** Created : 000117 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ +#ifndef QSOUND_H +#define QSOUND_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_SOUND + +class QSoundData; + +class Q_EXPORT QSound : public QObject { + Q_OBJECT +public: + static bool isAvailable(); + static void play(const QString& filename); + + QSound(const QString& filename, QObject* parent=0, const char* name=0); + ~QSound(); + + /* Coming soon... + ? + QSound(int hertz, Type type=Mono); + int play(const ushort* data, int samples); + bool full(); + signal void notFull(); + ? + */ + +#ifndef QT_NO_COMPAT + static bool available() { return isAvailable(); } +#endif + + int loops() const; + int loopsRemaining() const; + void setLoops(int); + QString fileName() const; + + bool isFinished() const; + +public slots: + void play(); + void stop(); + +private: + QSoundData* d; + friend class QAuServer; +}; + + +/* + QAuServer is an INTERNAL class. If you wish to provide support for + additional audio servers, you can make a subclass of QAuServer to do + so, HOWEVER, your class may need to be re-engineered to some degree + with each new Qt release, including minor releases. + + QAuBucket is whatever you want. +*/ + +class QAuBucket { +public: + virtual ~QAuBucket(); +}; + +class QAuServer : public QObject { + Q_OBJECT + +public: + QAuServer(QObject* parent, const char* name); + ~QAuServer(); + + virtual void init(QSound*); + virtual void play(const QString& filename); + virtual void play(QSound*)=0; + virtual void stop(QSound*)=0; + virtual bool okay()=0; + +protected: + void setBucket(QSound*, QAuBucket*); + QAuBucket* bucket(QSound*); + int decLoop(QSound*); +}; + +#endif // QT_NO_SOUND + +#endif diff --git a/src/kernel/qsound_x11.cpp b/src/kernel/qsound_x11.cpp new file mode 100644 index 0000000..82639a4 --- /dev/null +++ b/src/kernel/qsound_x11.cpp @@ -0,0 +1,283 @@ +/**************************************************************************** +** +** Implementation of QSound class and QAuServer internal class +** +** Created : 000117 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#define QT_CLEAN_NAMESPACE + +#include "qsound.h" + +#ifndef QT_NO_SOUND + +#include "qptrdict.h" +#include "qsocketnotifier.h" +#include "qapplication.h" + + +#ifdef QT_NAS_SUPPORT + +#include <audio/audiolib.h> +#include <audio/soundlib.h> + +static AuServer *nas=0; + +static AuBool eventPred(AuServer *, AuEvent *e, AuPointer p) +{ + if (e && (e->type == AuEventTypeElementNotify)) { + if (e->auelementnotify.flow == *((AuFlowID *)p)) + return TRUE; + } + return FALSE; +} + +class QAuBucketNAS : public QAuBucket { +public: + QAuBucketNAS(AuBucketID b, AuFlowID f = 0) : id(b), flow(f), stopped(TRUE), numplaying(0) { } + ~QAuBucketNAS() + { + if ( nas ) { + AuSync(nas, FALSE); + AuDestroyBucket(nas, id, NULL); + + AuEvent ev; + while (AuScanEvents(nas, AuEventsQueuedAfterFlush, TRUE, eventPred, &flow, &ev)) + ; + } + } + + AuBucketID id; + AuFlowID flow; + bool stopped; + int numplaying; +}; + +class QAuServerNAS : public QAuServer { + Q_OBJECT + + QSocketNotifier* sn; + +public: + QAuServerNAS(QObject* parent); + ~QAuServerNAS(); + + void init(QSound*); + void play(const QString& filename); + void play(QSound*); + void stop(QSound*); + bool okay(); + void setDone(QSound*); + +public slots: + void dataReceived(); + void soundDestroyed(QObject *o); + +private: + QAuBucketNAS* bucket(QSound* s) + { + return (QAuBucketNAS*)QAuServer::bucket(s); + } +}; + +QAuServerNAS::QAuServerNAS(QObject* parent) : + QAuServer(parent,"Network Audio System") +{ + nas = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL); + if (nas) { + AuSetCloseDownMode(nas, AuCloseDownDestroy, NULL); + // Ask Qt for async messages... + sn=new QSocketNotifier(AuServerConnectionNumber(nas), + QSocketNotifier::Read); + QObject::connect(sn, SIGNAL(activated(int)), + this, SLOT(dataReceived())); + } else { + sn = 0; + } +} + +QAuServerNAS::~QAuServerNAS() +{ + if ( nas ) + AuCloseServer( nas ); + delete sn; + nas = 0; +} + +static QPtrDict<void> *inprogress=0; + +void QAuServerNAS::soundDestroyed(QObject *o) +{ + if (inprogress) { + QSound *so = static_cast<QSound *>(o); + while (inprogress->remove(so)) + ; // Loop while remove returns TRUE + } +} + +void QAuServerNAS::play(const QString& filename) +{ + if (nas) { + int iv=100; + AuFixedPoint volume=AuFixedPointFromFraction(iv,100); + AuSoundPlayFromFile(nas, filename, AuNone, volume, NULL, NULL, NULL, NULL, NULL, NULL); + AuFlush(nas); + dataReceived(); + AuFlush(nas); + qApp->flushX(); + } +} + +static void callback( AuServer*, AuEventHandlerRec*, AuEvent* e, AuPointer p) +{ + if ( inprogress->find(p) && e ) { + if (e->type==AuEventTypeElementNotify && + e->auelementnotify.kind==AuElementNotifyKindState) { + if ( e->auelementnotify.cur_state == AuStateStop ) + ((QAuServerNAS*)inprogress->find(p))->setDone((QSound*)p); + } + } +} + +void QAuServerNAS::setDone(QSound* s) +{ + if (nas) { + decLoop(s); + if (s->loopsRemaining() && !bucket(s)->stopped) { + bucket(s)->stopped = TRUE; + play(s); + } else { + if (--(bucket(s)->numplaying) == 0) + bucket(s)->stopped = TRUE; + inprogress->remove(s); + } + } +} + +void QAuServerNAS::play(QSound* s) +{ + if (nas) { + ++(bucket(s)->numplaying); + if (!bucket(s)->stopped) { + stop(s); + } + + bucket(s)->stopped = FALSE; + if ( !inprogress ) + inprogress = new QPtrDict<void>; + inprogress->insert(s,(void*)this); + int iv=100; + AuFixedPoint volume=AuFixedPointFromFraction(iv,100); + QAuBucketNAS *b = bucket(s); + AuSoundPlayFromBucket(nas, b->id, AuNone, volume, + callback, s, 0, &b->flow, NULL, NULL, NULL); + AuFlush(nas); + dataReceived(); + AuFlush(nas); + qApp->flushX(); + } +} + +void QAuServerNAS::stop(QSound* s) +{ + if (nas && !bucket(s)->stopped) { + bucket(s)->stopped = TRUE; + AuStopFlow(nas, bucket(s)->flow, NULL); + AuFlush(nas); + dataReceived(); + AuFlush(nas); + qApp->flushX(); + } +} + +void QAuServerNAS::init(QSound* s) +{ + connect(s, SIGNAL(destroyed(QObject *)), + this, SLOT(soundDestroyed(QObject *))); + + if ( nas ) { + AuBucketID b_id = + AuSoundCreateBucketFromFile(nas, s->fileName(), + 0 /*AuAccessAllMasks*/, NULL, NULL); + setBucket(s, new QAuBucketNAS(b_id)); + } +} + +bool QAuServerNAS::okay() +{ + return !!nas; +} + +void QAuServerNAS::dataReceived() +{ + AuHandleEvents(nas); +} + +#include "qsound_x11.moc" + +#endif + + +class QAuServerNull : public QAuServer { +public: + QAuServerNull(QObject* parent); + + void play(const QString&) { } + void play(QSound*s) { while(decLoop(s) > 0) /* nothing */ ; } + void stop(QSound*) { } + bool okay() { return FALSE; } +}; + +QAuServerNull::QAuServerNull(QObject* parent) : + QAuServer(parent,"Null Audio Server") +{ +} + + +QAuServer* qt_new_audio_server() +{ +#ifdef QT_NAS_SUPPORT + QAuServer* s=new QAuServerNAS(qApp); + if (s->okay()) { + return s; + } else { + delete s; + } +#endif + return new QAuServerNull(qApp); +} + +#endif // QT_NO_SOUND diff --git a/src/kernel/qstyle.cpp b/src/kernel/qstyle.cpp new file mode 100644 index 0000000..8c150d5 --- /dev/null +++ b/src/kernel/qstyle.cpp @@ -0,0 +1,1896 @@ +/**************************************************************************** +** +** Implementation of QStyle class +** +** Created : 981231 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qstyle.h" +#ifndef QT_NO_STYLE +#include "qapplication.h" +#include "qpainter.h" +#include "qbitmap.h" +#include "qpixmapcache.h" + +#include <limits.h> + + +class QStylePrivate +{ +public: + QStylePrivate() + { + } +}; + +/*! + \class QStyleOption qstyle.h + \brief The QStyleOption class specifies optional parameters for QStyle functions. + \ingroup appearance + + Some QStyle functions take an optional argument specifying extra + information that is required for a paritical primitive or control. + So that the QStyle class can be extended, QStyleOption is used to + provide a variable-argument for these options. + + The QStyleOption class has constructors for each type of optional + argument, and this set of constructors may be extended in future + Qt releases. There are also corresponding access functions that + return the optional arguments: these too may be extended. + + For each constructor, you should refer to the documentation of the + QStyle functions to see the meaning of the arguments. + + When calling QStyle functions from your own widgets, you must only + pass the default QStyleOption or the argument that QStyle is + documented to accept. For example, if the function expects + QStyleOption(QMenuItem *, int), passing QStyleOption(QMenuItem *) + leaves the optional integer argument uninitialized. + + When subclassing QStyle, you must similarly only expect the + default or documented arguments. The other arguments will have + uninitialized values. + + If you make your own QStyle subclasses and your own widgets, you + can make a subclass of QStyleOption to pass additional arguments + to your QStyle subclass. You will need to cast the "const + QStyleOption&" argument to your subclass, so be sure your style + has been called from your widget. +*/ + +/*! + \enum QStyleOption::StyleOptionDefault + + This enum value can be passed as the optional argument to any + QStyle function. + + \value Default +*/ + +/*! + \fn QStyleOption::QStyleOption(StyleOptionDefault) + + The default option. This can always be passed as the optional + argument to QStyle functions. +*/ + +/*! + \fn QStyleOption::QStyleOption(int) + + Pass one integer, \a in1. For example, headerSection. +*/ + +/*! + \fn QStyleOption::QStyleOption(int, int) + + Pass two integers, \a in1 and \a in2. For example, linewidth and + midlinewidth. +*/ + +/*! + \fn QStyleOption::QStyleOption(int, int, int, int) + + Pass four integers, \a in1, \a in2, \a in3 and \a in4. +*/ + +/*! + \fn QStyleOption::QStyleOption(QMenuItem*) + + Pass a menu item, \a m. +*/ + +/*! + \fn QStyleOption::QStyleOption(QMenuItem*, int) + + Pass a menu item and an integer, \a m and \a in1. +*/ + +/*! + \fn QStyleOption::QStyleOption(QMenuItem*, int, int) + + Pass a menu item and two integers, \a m, \a in1 and \a in2. +*/ + +/*! + \fn QStyleOption::QStyleOption(const QColor&) + + Pass a color, \a c. +*/ + +/*! + \fn QStyleOption::QStyleOption(QTab*) + + Pass a QTab, \a t. +*/ + +/*! + \fn QStyleOption::QStyleOption(QListViewItem*) + + Pass a QListViewItem, \a i. +*/ + +/*! + \fn QStyleOption::QStyleOption(Qt::ArrowType) + + Pass an Qt::ArrowType, \a a. +*/ + +/*! + \fn QStyleOption::QStyleOption(QCheckListItem* i) + + Pass a QCheckListItem, \a i. +*/ + +/*! + \fn QStyleOption::QStyleOption( const QRect &r ) + + Pass a QRect, \a r. +*/ + +/*! + \fn QStyleOption::QStyleOption( QWidget *w ) + + Pass a QWidget, \a w. +*/ + +/*! + \fn bool QStyleOption::isDefault() const + + Returns TRUE if the option was constructed with the default + constructor; otherwise returns FALSE. +*/ + +/*! + \fn int QStyleOption::day() const + + Returns the index of the day in the month if the appropriate + constructor was called; otherwise the return value is undefined. +*/ + +/*! + \fn int QStyleOption::lineWidth() const + + Returns the line width if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \fn int QStyleOption::midLineWidth() const + + Returns the mid-line width if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn int QStyleOption::frameShape() const + + Returns a QFrame::Shape value if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn int QStyleOption::frameShadow() const + + Returns a QFrame::Shadow value if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn QMenuItem* QStyleOption::menuItem() const + + Returns a menu item if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \fn int QStyleOption::maxIconWidth() const + + Returns the maximum width of the menu item check area if the + appropriate constructor was called; otherwise the return value is + undefined. +*/ + +/*! + \fn int QStyleOption::tabWidth() const + + Returns the tab indent width if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn int QStyleOption::headerSection() const + + Returns the header section if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn const QColor& QStyleOption::color() const + + Returns a color if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \fn QTab* QStyleOption::tab() const + + Returns a QTabBar tab if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \fn QListViewItem* QStyleOption::listViewItem() const + + Returns a QListView item if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn Qt::ArrowType QStyleOption::arrowType() const + + Returns an arrow type if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \fn QCheckListItem* QStyleOption::checkListItem() const + + Returns a check list item if the appropriate constructor was + called; otherwise the return value is undefined. +*/ + +/*! + \fn QRect QStyleOption::rect() const + + Returns a rectangle if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \fn QWidget* QStyleOption::widget() const + + Returns a pointer to a widget if the appropriate constructor was called; + otherwise the return value is undefined. +*/ + +/*! + \class QStyle qstyle.h + \brief The QStyle class specifies the look and feel of a GUI. + \ingroup appearance + + A large number of GUI elements are common to many widgets. The + QStyle class allows the look of these elements to be modified + across all widgets that use the QStyle functions. It also + provides two feel options: Motif and Windows. + + Although it is not possible to fully enumerate the look of + graphical elements and the feel of widgets in a GUI, QStyle + provides a considerable amount of control and customisability. + + In Qt 1.x the look and feel option for widgets was specified by a + single value: the GUIStyle. Starting with Qt 2.0, this notion was + expanded to allow the look to be specified by virtual drawing + functions. + + Derived classes may reimplement some or all of the drawing + functions to modify the look of all widgets that use those + functions. + + Languages written from right to left (such as Arabic and Hebrew) + usually also mirror the whole layout of widgets. If you design a + style, you should take special care when drawing asymmetric + elements to make sure that they also look correct in a mirrored + layout. You can start your application with \c -reverse to check + the mirrored layout. Also notice, that for a reversed layout, the + light usually comes from top right instead of top left. + + The actual reverse layout is performed automatically when + possible. However, for the sake of flexibility, the translation + cannot be performed everywhere. The documentation for each + function in the QStyle API states whether the function + expects/returns logical or screen coordinates. Using logical + coordinates (in ComplexControls, for example) provides great + flexibility in controlling the look of a widget. Use visualRect() + when necessary to translate logical coordinates into screen + coordinates for drawing. + + In Qt versions prior to 3.0, if you wanted a low level route into + changing the appearance of a widget, you would reimplement + polish(). With the new 3.0 style engine the recommended approach + is to reimplement the draw functions, for example drawItem(), + drawPrimitive(), drawControl(), drawControlMask(), + drawComplexControl() and drawComplexControlMask(). Each of these + functions is called with a range of parameters that provide + information that you can use to determine how to draw them, e.g. + style flags, rectangle, color group, etc. + + For information on changing elements of an existing style or + creating your own style see the \link customstyles.html Style + overview\endlink. + + Styles can also be created as \link plugins-howto.html + plugins\endlink. +*/ + +/*! + \enum Qt::GUIStyle + + \obsolete + + \value WindowsStyle + \value MotifStyle + \value MacStyle + \value Win3Style + \value PMStyle +*/ + +/*! + \enum Qt::UIEffect + + \value UI_General + \value UI_AnimateMenu + \value UI_FadeMenu + \value UI_AnimateCombo + \value UI_AnimateTooltip + \value UI_FadeTooltip + \value UI_AnimateToolBox Reserved +*/ + +/*! + Constructs a QStyle. +*/ +QStyle::QStyle() +{ + d = new QStylePrivate; +} + +/*! + Destroys the style and frees all allocated resources. +*/ +QStyle::~QStyle() +{ + delete d; + d = 0; +} + +/* + \fn GUIStyle QStyle::guiStyle() const + \obsolete + + Returns an indicator to the additional "feel" component of a + style. Current supported values are Qt::WindowsStyle and Qt::MotifStyle. +*/ + + + +/*! + Initializes the appearance of a widget. + + This function is called for every widget at some point after it + has been fully created but just \e before it is shown the very + first time. + + Reasonable actions in this function might be to call + QWidget::setBackgroundMode() for the widget. An example of highly + unreasonable use would be setting the geometry! Reimplementing + this function gives you a back-door through which you can change + the appearance of a widget. With Qt 3.0's style engine you will + rarely need to write your own polish(); instead reimplement + drawItem(), drawPrimitive(), etc. + + The QWidget::inherits() function may provide enough information to + allow class-specific customizations. But be careful not to + hard-code things too much because new QStyle subclasses are + expected to work reasonably with all current and \e future + widgets. + + \sa unPolish() +*/ +void QStyle::polish( QWidget*) +{ +} + +/*! + Undoes the initialization of a widget's appearance. + + This function is the counterpart to polish. It is called for every + polished widget when the style is dynamically changed. The former + style has to unpolish its settings before the new style can polish + them again. + + \sa polish() +*/ +void QStyle::unPolish( QWidget*) +{ +} + + +/*! + \overload + Late initialization of the QApplication object. + + \sa unPolish() +*/ +void QStyle::polish( QApplication*) +{ +} + +/*! + \overload + + Undoes the application polish. + + \sa polish() +*/ +void QStyle::unPolish( QApplication*) +{ +} + +/*! + \overload + + The style may have certain requirements for color palettes. In + this function it has the chance to change the palette according to + these requirements. + + \sa QPalette, QApplication::setPalette() +*/ +void QStyle::polish( QPalette&) +{ +} + +/*! + Polishes the popup menu according to the GUI style. This usually + means setting the mouse tracking + (\l{QPopupMenu::setMouseTracking()}) and whether the menu is + checkable by default (\l{QPopupMenu::setCheckable()}). +*/ +void QStyle::polishPopupMenu( QPopupMenu *) +{ +} + +/*! + Returns the appropriate area (see below) within rectangle \a r in + which to draw the \a text or \a pixmap using painter \a p. If \a + len is -1 (the default) all the \a text is drawn; otherwise only + the first \a len characters of \a text are drawn. The text is + aligned in accordance with the alignment \a flags (see + \l{Qt::AlignmentFlags}). The \a enabled bool indicates whether or + not the item is enabled. + + If \a r is larger than the area needed to render the \a text the + rectangle that is returned will be offset within \a r in + accordance with the alignment \a flags. For example if \a flags is + \c AlignCenter the returned rectangle will be centered within \a + r. If \a r is smaller than the area needed the rectangle that is + returned will be \e larger than \a r (the smallest rectangle large + enough to render the \a text or \a pixmap). + + By default, if both the text and the pixmap are not null, the + pixmap is drawn and the text is ignored. +*/ +QRect QStyle::itemRect( QPainter *p, const QRect &r, + int flags, bool enabled, const QPixmap *pixmap, + const QString& text, int len ) const +{ + QRect result; + int x = r.x(); + int y = r.y(); + int w = r.width(); + int h = r.height(); + GUIStyle gs = (GUIStyle)styleHint( SH_GUIStyle ); + + if ( pixmap ) { + if ( (flags & Qt::AlignVCenter) == Qt::AlignVCenter ) + y += h/2 - pixmap->height()/2; + else if ( (flags & Qt::AlignBottom) == Qt::AlignBottom) + y += h - pixmap->height(); + if ( (flags & Qt::AlignRight) == Qt::AlignRight ) + x += w - pixmap->width(); + else if ( (flags & Qt::AlignHCenter) == Qt::AlignHCenter ) + x += w/2 - pixmap->width()/2; + else if ( (flags & Qt::AlignLeft) != Qt::AlignLeft && QApplication::reverseLayout() ) + x += w - pixmap->width(); + result = QRect(x, y, pixmap->width(), pixmap->height()); + } else if ( !text.isNull() && p ) { + result = p->boundingRect( x, y, w, h, flags, text, len ); + if ( gs == Qt::WindowsStyle && !enabled ) { + result.setWidth(result.width()+1); + result.setHeight(result.height()+1); + } + } else { + result = QRect(x, y, w, h); + } + + return result; +} + + +/*! + Draws the \a text or \a pixmap in rectangle \a r using painter \a + p and color group \a g. The pen color is specified with \a + penColor. The \a enabled bool indicates whether or not the item is + enabled; when reimplementing this bool should influence how the + item is drawn. If \a len is -1 (the default) all the \a text is + drawn; otherwise only the first \a len characters of \a text are + drawn. The text is aligned and wrapped according to the alignment + \a flags (see \l{Qt::AlignmentFlags}). + + By default, if both the text and the pixmap are not null, the + pixmap is drawn and the text is ignored. +*/ +void QStyle::drawItem( QPainter *p, const QRect &r, + int flags, const QColorGroup &g, bool enabled, + const QPixmap *pixmap, const QString& text, int len, + const QColor* penColor ) const +{ + int x = r.x(); + int y = r.y(); + int w = r.width(); + int h = r.height(); + GUIStyle gs = (GUIStyle)styleHint( SH_GUIStyle ); + + p->setPen( penColor?*penColor:g.foreground() ); + if ( pixmap ) { + QPixmap pm( *pixmap ); + bool clip = (flags & Qt::DontClip) == 0; + if ( clip ) { + if ( pm.width() < w && pm.height() < h ) { + clip = FALSE; + } else { + p->save(); + QRegion cr = QRect(x, y, w, h); + if (p->hasClipping()) + cr &= p->clipRegion(QPainter::CoordPainter); + p->setClipRegion(cr); + } + } + if ( (flags & Qt::AlignVCenter) == Qt::AlignVCenter ) + y += h/2 - pm.height()/2; + else if ( (flags & Qt::AlignBottom) == Qt::AlignBottom) + y += h - pm.height(); + if ( (flags & Qt::AlignRight) == Qt::AlignRight ) + x += w - pm.width(); + else if ( (flags & Qt::AlignHCenter) == Qt::AlignHCenter ) + x += w/2 - pm.width()/2; + else if ( ((flags & Qt::AlignLeft) != Qt::AlignLeft) && QApplication::reverseLayout() ) // AlignAuto && rightToLeft + x += w - pm.width(); + + if ( !enabled ) { + if ( pm.mask() ) { // pixmap with a mask + if ( !pm.selfMask() ) { // mask is not pixmap itself + QPixmap pmm( *pm.mask() ); + pmm.setMask( *((QBitmap *)&pmm) ); + pm = pmm; + } + } else if ( pm.depth() == 1 ) { // monochrome pixmap, no mask + pm.setMask( *((QBitmap *)&pm) ); +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + } else { // color pixmap, no mask + QString k; + k.sprintf( "$qt-drawitem-%x", pm.serialNumber() ); + QPixmap *mask = QPixmapCache::find(k); + bool del=FALSE; + if ( !mask ) { + mask = new QPixmap( pm.createHeuristicMask() ); + mask->setMask( *((QBitmap*)mask) ); + del = !QPixmapCache::insert( k, mask ); + } + pm = *mask; + if (del) delete mask; +#endif + } + if ( gs == Qt::WindowsStyle ) { + p->setPen( g.light() ); + p->drawPixmap( x+1, y+1, pm ); + p->setPen( g.text() ); + } + } + p->drawPixmap( x, y, pm ); + if ( clip ) + p->restore(); + } else if ( !text.isNull() ) { + if ( gs == Qt::WindowsStyle && !enabled ) { + p->setPen( g.light() ); + p->drawText( x+1, y+1, w, h, flags, text, len ); + p->setPen( g.text() ); + } + p->drawText( x, y, w, h, flags, text, len ); + } +} + +/*! + \enum QStyle::PrimitiveElement + + This enum represents the PrimitiveElements of a style. A + PrimitiveElement is a common GUI element, such as a checkbox + indicator or pushbutton bevel. + + \value PE_ButtonCommand button used to initiate an action, for + example, a QPushButton. + \value PE_ButtonDefault this button is the default button, e.g. + in a dialog. + \value PE_ButtonBevel generic button bevel. + \value PE_ButtonTool tool button, for example, a QToolButton. + \value PE_ButtonDropDown drop down button, for example, a tool + button that displays a popup menu, for example, QPopupMenu. + + + \value PE_FocusRect generic focus indicator. + + + \value PE_ArrowUp up arrow. + \value PE_ArrowDown down arrow. + \value PE_ArrowRight right arrow. + \value PE_ArrowLeft left arrow. + + + \value PE_SpinWidgetUp up symbol for a spin widget, for example a + QSpinBox. + \value PE_SpinWidgetDown down symbol for a spin widget. + \value PE_SpinWidgetPlus increase symbol for a spin widget. + \value PE_SpinWidgetMinus decrease symbol for a spin widget. + + + \value PE_Indicator on/off indicator, for example, a QCheckBox. + \value PE_IndicatorMask bitmap mask for an indicator. + \value PE_ExclusiveIndicator exclusive on/off indicator, for + example, a QRadioButton. + \value PE_ExclusiveIndicatorMask bitmap mask for an exclusive indicator. + + + \value PE_DockWindowHandle tear off handle for dock windows and + toolbars, for example \l{QDockWindow}s and \l{QToolBar}s. + \value PE_DockWindowSeparator item separator for dock window and + toolbar contents. + \value PE_DockWindowResizeHandle resize handle for dock windows. + + \value PE_Splitter splitter handle; see also QSplitter. + + + \value PE_Panel generic panel frame; see also QFrame. + \value PE_PanelPopup panel frame for popup windows/menus; see also + QPopupMenu. + \value PE_PanelMenuBar panel frame for menu bars. + \value PE_PanelDockWindow panel frame for dock windows and toolbars. + \value PE_PanelTabWidget panel frame for tab widgets. + \value PE_PanelLineEdit panel frame for line edits. + \value PE_PanelGroupBox panel frame for group boxes. + + \value PE_TabBarBase area below tabs in a tab widget, for example, + QTab. + + + \value PE_HeaderSection section of a list or table header; see also + QHeader. + \value PE_HeaderArrow arrow used to indicate sorting on a list or table + header + \value PE_StatusBarSection section of a status bar; see also + QStatusBar. + + + \value PE_GroupBoxFrame frame around a group box; see also + QGroupBox. + \value PE_WindowFrame frame around a MDI window or a docking window + + + \value PE_Separator generic separator. + + + \value PE_SizeGrip window resize handle; see also QSizeGrip. + + + \value PE_CheckMark generic check mark; see also QCheckBox. + + + \value PE_ScrollBarAddLine scrollbar line increase indicator + (i.e. scroll down); see also QScrollBar. + \value PE_ScrollBarSubLine scrollbar line decrease indicator (i.e. scroll up). + \value PE_ScrollBarAddPage scolllbar page increase indicator (i.e. page down). + \value PE_ScrollBarSubPage scrollbar page decrease indicator (i.e. page up). + \value PE_ScrollBarSlider scrollbar slider + \value PE_ScrollBarFirst scrollbar first line indicator (i.e. home). + \value PE_ScrollBarLast scrollbar last line indicator (i.e. end). + + + \value PE_ProgressBarChunk section of a progress bar indicator; see + also QProgressBar. + + \value PE_CheckListController controller part of a listview item + \value PE_CheckListIndicator checkbox part of a listview item + \value PE_CheckListExclusiveIndicator radiobutton part of a listview item + \value PE_RubberBand rubber band used in such things as iconview + + \value PE_CustomBase base value for custom PrimitiveElements. + All values above this are reserved for custom use. Custom + values must be greater than this value. + + \sa drawPrimitive() +*/ +/*! \enum QStyle::SFlags + \internal +*/ +/*! \enum QStyle::SCFlags + \internal +*/ + +/*! + \enum QStyle::StyleFlags + + This enum represents flags for drawing PrimitiveElements. Not all + primitives use all of these flags. Note that these flags may mean + different things to different primitives. For an explanation of + the relationship between primitives and their flags, as well as + the different meanings of the flags, see the \link + customstyles.html Style overview\endlink. + + \value Style_Default + \value Style_Enabled + \value Style_Raised + \value Style_Sunken + \value Style_Off + \value Style_NoChange + \value Style_On + \value Style_Down + \value Style_Horizontal + \value Style_HasFocus + \value Style_Top + \value Style_Bottom + \value Style_FocusAtBorder + \value Style_AutoRaise + \value Style_MouseOver + \value Style_Up + \value Style_Selected + \value Style_HasFocus + \value Style_Active + \value Style_ButtonDefault + + \sa drawPrimitive() +*/ + +/*! + \fn void QStyle::drawPrimitive( PrimitiveElement pe, QPainter *p, const QRect &r, const QColorGroup &cg, SFlags flags, const QStyleOption& opt) const + + Draws the style PrimitiveElement \a pe using the painter \a p in + the area \a r. Colors are used from the color group \a cg. + + The rect \a r should be in screen coordinates. + + The \a flags argument is used to control how the PrimitiveElement + is drawn. Multiple flags can be OR'ed together. + + For example, a pressed button would be drawn with the flags \c + Style_Enabled and \c Style_Down. + + The \a opt argument can be used to control how various + PrimitiveElements are drawn. Note that \a opt may be the default + value even for PrimitiveElements that make use of extra options. + When \a opt is non-default, it is used as follows: + + \table + \header \i PrimitiveElement \i Options \i Notes + \row \i \l PE_FocusRect + \i \l QStyleOption ( const \l QColor & bg ) + \list + \i opt.\link QStyleOption::color() color\endlink() + \endlist + \i \e bg is the background color on which the focus rect is being drawn. + \row \i12 \l PE_Panel + \i12 \l QStyleOption ( int linewidth, int midlinewidth ) + \list + \i opt.\link QStyleOption::lineWidth() lineWidth\endlink() + \i opt.\link QStyleOption::midLineWidth() midLineWidth\endlink() + \endlist + \i \e linewidth is the line width for drawing the panel. + \row \i \e midlinewidth is the mid-line width for drawing the panel. + \row \i12 \l PE_PanelPopup + \i12 \l QStyleOption ( int linewidth, int midlinewidth ) + \list + \i opt.\link QStyleOption::lineWidth() lineWidth\endlink() + \i opt.\link QStyleOption::midLineWidth() midLineWidth\endlink() + \endlist + \i \e linewidth is the line width for drawing the panel. + \row \i \e midlinewidth is the mid-line width for drawing the panel. + \row \i12 \l PE_PanelMenuBar + \i12 \l QStyleOption ( int linewidth, int midlinewidth ) + \list + \i opt.\link QStyleOption::lineWidth() lineWidth\endlink() + \i opt.\link QStyleOption::midLineWidth() midLineWidth\endlink() + \endlist + \i \e linewidth is the line width for drawing the panel. + \row \i \e midlinewidth is the mid-line width for drawing the panel. + \row \i12 \l PE_PanelDockWindow + \i12 \l QStyleOption ( int linewidth, int midlinewidth ) + \list + \i opt.\link QStyleOption::lineWidth() lineWidth\endlink() + \i opt.\link QStyleOption::midLineWidth() midLineWidth\endlink() + \endlist + \i \e linewidth is the line width for drawing the panel. + \row \i \e midlinewidth is the mid-line width for drawing the panel. + \row \i14 \l PE_GroupBoxFrame + \i14 \l QStyleOption ( int linewidth, int midlinewidth, int shape, int shadow ) + \list + \i opt.\link QStyleOption::lineWidth() lineWidth\endlink() + \i opt.\link QStyleOption::midLineWidth() midLineWidth\endlink() + \i opt.\link QStyleOption::frameShape() frameShape\endlink() + \i opt.\link QStyleOption::frameShadow() frameShadow\endlink() + \endlist + \i \e linewidth is the line width for the group box. + \row \i \e midlinewidth is the mid-line width for the group box. + \row \i \e shape is the \link QFrame::frameShape frame shape \endlink + for the group box. + \row \i \e shadow is the \link QFrame::frameShadow frame shadow \endlink + for the group box. + \endtable + + + For all other \link QStyle::PrimitiveElement + PrimitiveElements\endlink, \a opt is unused. + + \sa StyleFlags +*/ + +/*! + \enum QStyle::ControlElement + + This enum represents a ControlElement. A ControlElement is part of + a widget that performs some action or displays information to the + user. + + \value CE_PushButton the bevel and default indicator of a QPushButton. + \value CE_PushButtonLabel the label (iconset with text or pixmap) + of a QPushButton. + + \value CE_CheckBox the indicator of a QCheckBox. + \value CE_CheckBoxLabel the label (text or pixmap) of a QCheckBox. + + \value CE_RadioButton the indicator of a QRadioButton. + \value CE_RadioButtonLabel the label (text or pixmap) of a QRadioButton. + + \value CE_TabBarTab the tab within a QTabBar (a QTab). + \value CE_TabBarLabel the label within a QTab. + + \value CE_ProgressBarGroove the groove where the progress + indicator is drawn in a QProgressBar. + \value CE_ProgressBarContents the progress indicator of a QProgressBar. + \value CE_ProgressBarLabel the text label of a QProgressBar. + + \value CE_PopupMenuItem a menu item in a QPopupMenu. + \value CE_PopupMenuScroller scrolling areas in a popumenu when the + style supports scrolling. + \value CE_PopupMenuHorizontalExtra extra frame area set aside with PM_PopupMenuFrameHorizontalExtra + \value CE_PopupMenuVerticalExtra extra frame area set aside with PM_PopupMenuFrameVerticalExtra + + \value CE_MenuBarItem a menu item in a QMenuBar. + + \value CE_ToolButtonLabel a tool button's label. + + \value CE_MenuBarEmptyArea the empty area of a QMenuBar. + \value CE_DockWindowEmptyArea the empty area of a QDockWindow. + + \value CE_ToolBoxTab the toolbox's tab area + \value CE_HeaderLabel the header's label + + \value CE_CustomBase base value for custom ControlElements. All values above + this are reserved for custom use. Therefore, custom values must be + greater than this value. + + \sa drawControl() +*/ + +/*! + \fn void QStyle::drawControl( ControlElement element, QPainter *p, const QWidget *widget, const QRect &r, const QColorGroup &cg, SFlags how, const QStyleOption& opt) const + + Draws the ControlElement \a element using the painter \a p in the + area \a r. Colors are used from the color group \a cg. + + The rect \a r should be in screen coordinates. + + The \a how argument is used to control how the ControlElement is + drawn. Multiple flags can be OR'ed together. See the table below + for an explanation of which flags are used with the various + ControlElements. + + The \a widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a element. The \a opt argument can be used to + pass extra information required when drawing the ControlElement. + Note that \a opt may be the default value even for ControlElements + that can make use of the extra options. See the table below for + the appropriate \a widget and \a opt usage: + + \table + \header \i ControlElement<br>\& Widget Cast + \i Style Flags + \i Notes + \i Options + \i Notes + + \row \i16 \l{CE_PushButton}(const \l QPushButton *) + + and + + \l{CE_PushButtonLabel}(const \l QPushButton *) + \i \l Style_Enabled \i Set if the button is enabled. + \i16 Unused. + \i16 + \row \i \l Style_HasFocus \i Set if the button has input focus. + \row \i \l Style_Raised \i Set if the button is not down, not on and not flat. + \row \i \l Style_On \i Set if the button is a toggle button and toggled on. + \row \i \l Style_Down \i Set if the button is down (i.e., the mouse button or + space bar is pressed on the button). + \row \i \l Style_ButtonDefault \i Set if the button is a default button. + + \row \i16 \l{CE_CheckBox}(const \l QCheckBox *) + + and + + \l{CE_CheckBoxLabel}(const \l QCheckBox *) + + \i \l Style_Enabled \i Set if the checkbox is enabled. + \i16 Unused. + \i16 + \row \i \l Style_HasFocus \i Set if the checkbox has input focus. + \row \i \l Style_On \i Set if the checkbox is checked. + \row \i \l Style_Off \i Set if the checkbox is not checked. + \row \i \l Style_NoChange \i Set if the checkbox is in the NoChange state. + \row \i \l Style_Down \i Set if the checkbox is down (i.e., the mouse button or + space bar is pressed on the button). + + \row \i15 \l{CE_RadioButton}(const QRadioButton *) + + and + + \l{CE_RadioButtonLabel}(const QRadioButton *) + \i \l Style_Enabled \i Set if the radiobutton is enabled. + \i15 Unused. + \i15 + \row \i \l Style_HasFocus \i Set if the radiobutton has input focus. + \row \i \l Style_On \i Set if the radiobutton is checked. + \row \i \l Style_Off \i Set if the radiobutton is not checked. + \row \i \l Style_Down \i Set if the radiobutton is down (i.e., the mouse + button or space bar is pressed on the radiobutton). + + \row \i12 \l{CE_TabBarTab}(const \l QTabBar *) + + and + + \l{CE_TabBarLabel}(const \l QTabBar *) + + \i \l Style_Enabled \i Set if the tabbar and tab is enabled. + \i12 \l QStyleOption ( \l QTab *t ) + \list + \i opt.\link QStyleOption::tab() tab\endlink() + \endlist + \i12 \e t is the QTab being drawn. + \row \i \l Style_Selected \i Set if the tab is the current tab. + + \row \i12 \l{CE_ProgressBarGroove}(const QProgressBar *) + + and + + \l{CE_ProgressBarContents}(const QProgressBar *) + + and + + \l{CE_ProgressBarLabel}(const QProgressBar *) + + \i \l Style_Enabled \i Set if the progressbar is enabled. + \i12 Unused. + \i12 + \row \i \l Style_HasFocus \i Set if the progressbar has input focus. + + \row \i13 \l{CE_PopupMenuItem}(const \l QPopupMenu *) + \i \l Style_Enabled \i Set if the menuitem is enabled. + \i13 \l QStyleOption ( QMenuItem *mi, int tabwidth, int maxpmwidth ) + \list + \i opt.\link QStyleOption::menuItem() menuItem\endlink() + \i opt.\link QStyleOption::tabWidth() tabWidth\endlink() + \i opt.\link QStyleOption::maxIconWidth() maxIconWidth\endlink() + \endlist + \i \e mi is the menu item being drawn. QMenuItem is currently an + internal class. + \row \i \l Style_Active \i Set if the menuitem is the current item. + \i \e tabwidth is the width of the tab column where key accelerators + are drawn. + \row \i \l Style_Down \i Set if the menuitem is down (i.e., the mouse button + or space bar is pressed). + \i \e maxpmwidth is the maximum width of the check column where + checkmarks and iconsets are drawn. + + \row \i14 \l{CE_MenuBarItem}(const \l QMenuBar *) + \i \l Style_Enabled \i Set if the menuitem is enabled + \i14 \l QStyleOption ( QMenuItem *mi ) + \list + \i opt.\link QStyleOption::menuItem() menuItem\endlink() + \endlist + \i14 \e mi is the menu item being drawn. + \row \i \l Style_Active \i Set if the menuitem is the current item. + \row \i \l Style_Down \i Set if the menuitem is down (i.e., a mouse button or + the space bar is pressed). + \row \i \l Style_HasFocus \i Set if the menubar has input focus. + + \row \i17 \l{CE_ToolButtonLabel}(const \l QToolButton *) + \i \l Style_Enabled \i Set if the toolbutton is enabled. + \i17 \l QStyleOption ( \l ArrowType t ) + \list + \i opt.\link QStyleOption::arrowType() arrowType\endlink() + \endlist + \i17 When the tool button only contains an arrow, \e t is the + arrow's type. + \row \i \l Style_HasFocus \i Set if the toolbutton has input focus. + \row \i \l Style_Down \i Set if the toolbutton is down (i.e., a + mouse button or the space is pressed). + \row \i \l Style_On \i Set if the toolbutton is a toggle button + and is toggled on. + \row \i \l Style_AutoRaise \i Set if the toolbutton has auto-raise enabled. + \row \i \l Style_MouseOver \i Set if the mouse pointer is over the toolbutton. + \row \i \l Style_Raised \i Set if the button is not down, not on and doesn't + contain the mouse when auto-raise is enabled. + \endtable + + \sa ControlElement, StyleFlags +*/ + +/*! + \fn void QStyle::drawControlMask( ControlElement element, QPainter *p, const QWidget *widget, const QRect &r, const QStyleOption& opt) const + + Draw a bitmask for the ControlElement \a element using the painter + \a p in the area \a r. See drawControl() for an explanation of the + use of the \a widget and \a opt arguments. + + The rect \a r should be in screen coordinates. + + \sa drawControl(), ControlElement +*/ + +/*! + \enum QStyle::SubRect + + This enum represents a sub-area of a widget. Style implementations + would use these areas to draw the different parts of a widget. + + \value SR_PushButtonContents area containing the label (iconset + with text or pixmap). + \value SR_PushButtonFocusRect area for the focus rect (usually + larger than the contents rect). + + \value SR_CheckBoxIndicator area for the state indicator (e.g. check mark). + \value SR_CheckBoxContents area for the label (text or pixmap). + \value SR_CheckBoxFocusRect area for the focus indicator. + + + \value SR_RadioButtonIndicator area for the state indicator. + \value SR_RadioButtonContents area for the label. + \value SR_RadioButtonFocusRect area for the focus indicator. + + + \value SR_ComboBoxFocusRect area for the focus indicator. + + + \value SR_SliderFocusRect area for the focus indicator. + + + \value SR_DockWindowHandleRect area for the tear-off handle. + + + \value SR_ProgressBarGroove area for the groove. + \value SR_ProgressBarContents area for the progress indicator. + \value SR_ProgressBarLabel area for the text label. + + + \value SR_ToolButtonContents area for the tool button's label. + + \value SR_DialogButtonAccept area for a dialog's accept button. + \value SR_DialogButtonReject area for a dialog's reject button. + \value SR_DialogButtonApply area for a dialog's apply button. + \value SR_DialogButtonHelp area for a dialog's help button. + \value SR_DialogButtonAll area for a dialog's all button. + \value SR_DialogButtonRetry area for a dialog's retry button. + \value SR_DialogButtonAbort area for a dialog's abort button. + \value SR_DialogButtonIgnore area for a dialog's ignore button. + \value SR_DialogButtonCustom area for a dialog's custom widget area (in button row). + + \value SR_ToolBoxTabContents area for a toolbox tab's icon and label + + \value SR_CustomBase base value for custom ControlElements. All values above + this are reserved for custom use. Therefore, custom values must be + greater than this value. + + \sa subRect() +*/ + +/*! + \fn QRect QStyle::subRect( SubRect subrect, const QWidget *widget ) const; + + Returns the sub-area \a subrect for the \a widget in logical + coordinates. + + The \a widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a subrect. See the table below for the + appropriate \a widget casts: + + \table + \header \i SubRect \i Widget Cast + \row \i \l SR_PushButtonContents \i (const \l QPushButton *) + \row \i \l SR_PushButtonFocusRect \i (const \l QPushButton *) + \row \i \l SR_CheckBoxIndicator \i (const \l QCheckBox *) + \row \i \l SR_CheckBoxContents \i (const \l QCheckBox *) + \row \i \l SR_CheckBoxFocusRect \i (const \l QCheckBox *) + \row \i \l SR_RadioButtonIndicator \i (const \l QRadioButton *) + \row \i \l SR_RadioButtonContents \i (const \l QRadioButton *) + \row \i \l SR_RadioButtonFocusRect \i (const \l QRadioButton *) + \row \i \l SR_ComboBoxFocusRect \i (const \l QComboBox *) + \row \i \l SR_DockWindowHandleRect \i (const \l QWidget *) + \row \i \l SR_ProgressBarGroove \i (const \l QProgressBar *) + \row \i \l SR_ProgressBarContents \i (const \l QProgressBar *) + \row \i \l SR_ProgressBarLabel \i (const \l QProgressBar *) + \endtable + + The tear-off handle (SR_DockWindowHandleRect) for QDockWindow + is a private class. Use QWidget::parentWidget() to access the + QDockWindow: + + \code + if ( !widget->parentWidget() ) + return; + const QDockWindow *dw = (const QDockWindow *) widget->parentWidget(); + \endcode + + \sa SubRect +*/ + +/*! + \enum QStyle::ComplexControl + + This enum represents a ComplexControl. ComplexControls have + different behaviour depending upon where the user clicks on them + or which keys are pressed. + + \value CC_SpinWidget + \value CC_ComboBox + \value CC_ScrollBar + \value CC_Slider + \value CC_ToolButton + \value CC_TitleBar + \value CC_ListView + + + \value CC_CustomBase base value for custom ControlElements. All + values above this are reserved for custom use. Therefore, + custom values must be greater than this value. + + \sa SubControl drawComplexControl() +*/ + +/*! + \enum QStyle::SubControl + + This enum represents a SubControl within a ComplexControl. + + \value SC_None special value that matches no other SubControl. + + + \value SC_ScrollBarAddLine scrollbar add line (i.e. down/right + arrow); see also QScrollbar. + \value SC_ScrollBarSubLine scrollbar sub line (i.e. up/left arrow). + \value SC_ScrollBarAddPage scrollbar add page (i.e. page down). + \value SC_ScrollBarSubPage scrollbar sub page (i.e. page up). + \value SC_ScrollBarFirst scrollbar first line (i.e. home). + \value SC_ScrollBarLast scrollbar last line (i.e. end). + \value SC_ScrollBarSlider scrollbar slider handle. + \value SC_ScrollBarGroove special subcontrol which contains the + area in which the slider handle may move. + + + \value SC_SpinWidgetUp spinwidget up/increase; see also QSpinBox. + \value SC_SpinWidgetDown spinwidget down/decrease. + \value SC_SpinWidgetFrame spinwidget frame. + \value SC_SpinWidgetEditField spinwidget edit field. + \value SC_SpinWidgetButtonField spinwidget button field. + + + \value SC_ComboBoxEditField combobox edit field; see also QComboBox. + \value SC_ComboBoxArrow combobox arrow + \value SC_ComboBoxFrame combobox frame + \value SC_ComboBoxListBoxPopup combobox list box + + \value SC_SliderGroove special subcontrol which contains the area + in which the slider handle may move. + \value SC_SliderHandle slider handle. + \value SC_SliderTickmarks slider tickmarks. + + + \value SC_ToolButton tool button; see also QToolbutton. + \value SC_ToolButtonMenu subcontrol for opening a popup menu in a + tool button; see also QPopupMenu. + + + \value SC_TitleBarSysMenu system menu button (i.e. restore, close, etc.). + \value SC_TitleBarMinButton minimize button. + \value SC_TitleBarMaxButton maximize button. + \value SC_TitleBarCloseButton close button. + \value SC_TitleBarLabel window title label. + \value SC_TitleBarNormalButton normal (restore) button. + \value SC_TitleBarShadeButton shade button. + \value SC_TitleBarUnshadeButton unshade button. + + + \value SC_ListView the list view area. + \value SC_ListViewBranch (internal) + \value SC_ListViewExpand expand item (i.e. show/hide child items). + + + \value SC_All special value that matches all SubControls. + + + \sa ComplexControl +*/ + +/*! + \fn void QStyle::drawComplexControl( ComplexControl control, QPainter *p, const QWidget *widget, const QRect &r, const QColorGroup &cg, SFlags how, SCFlags sub, SCFlags subActive, const QStyleOption& opt ) const + + Draws the ComplexControl \a control using the painter \a p in the + area \a r. Colors are used from the color group \a cg. The \a sub + argument specifies which SubControls to draw. Multiple SubControls + can be OR'ed together. The \a subActive argument specifies which + SubControl is active. + + The rect \a r should be in logical coordinates. Reimplementations + of this function should use visualRect() to change the logical + coordinates into screen coordinates when using drawPrimitive() and + drawControl(). + + The \a how argument is used to control how the ComplexControl is + drawn. Multiple flags can OR'ed together. See the table below for + an explanation of which flags are used with the various + ComplexControls. + + The \a widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a control. The \a opt argument can be used to + pass extra information required when drawing the ComplexControl. + Note that \a opt may be the default value even for ComplexControls + that can make use of the extra options. See the table below for + the appropriate \a widget and \a opt usage: + + \table + \header \i ComplexControl<br>\& Widget Cast + \i Style Flags + \i Notes + \i Options + \i Notes + + \row \i12 \l{CC_SpinWidget}(const QSpinWidget *) + \i \l Style_Enabled \i Set if the spinwidget is enabled. + \i12 Unused. + \i12 + \row \i \l Style_HasFocus \i Set if the spinwidget has input focus. + + \row \i12 \l{CC_ComboBox}(const \l QComboBox *) + \i \l Style_Enabled \i Set if the combobox is enabled. + \i12 Unused. + \i12 + \row \i \l Style_HasFocus \i Set if the combobox has input focus. + + \row \i12 \l{CC_ScrollBar}(const \l QScrollBar *) + \i \l Style_Enabled \i Set if the scrollbar is enabled. + \i12 Unused. + \i12 + \row \i \l Style_HasFocus \i Set if the scrollbar has input focus. + + \row \i12 \l{CC_Slider}(const \l QSlider *) + \i \l Style_Enabled \i Set if the slider is enabled. + \i12 Unused. + \i12 + + \row \i \l Style_HasFocus \i Set if the slider has input focus. + + \row \i16 \l{CC_ToolButton}(const \l QToolButton *) + \i \l Style_Enabled \i Set if the toolbutton is enabled. + \i16 \l QStyleOption ( \l ArrowType t ) + \list + \i opt.\link QStyleOption::arrowType() arrowType\endlink() + \endlist + \i16 When the tool button only contains an arrow, \e t is the + arrow's type. + \row \i \l Style_HasFocus \i Set if the toolbutton has input focus. + \row \i \l Style_Down \i Set if the toolbutton is down (ie. mouse + button or space pressed). + \row \i \l Style_On \i Set if the toolbutton is a toggle button + and is toggled on. + \row \i \l Style_AutoRaise \i Set if the toolbutton has auto-raise enabled. + \row \i \l Style_Raised \i Set if the button is not down, not on and doesn't + contain the mouse when auto-raise is enabled. + + \row \i \l{CC_TitleBar}(const \l QWidget *) + \i \l Style_Enabled \i Set if the titlebar is enabled. + \i Unused. + \i + + \row \i \l{CC_ListView}(const \l QListView *) + \i \l Style_Enabled \i Set if the titlebar is enabled. + \i \l QStyleOption ( \l QListViewItem *item ) + \list + \i opt.\link QStyleOption::listViewItem() listViewItem\endlink() + \endlist + \i \e item is the item that needs branches drawn + \endtable + + \sa ComplexControl, SubControl +*/ + +/*! + \fn void QStyle::drawComplexControlMask( ComplexControl control, QPainter *p, const QWidget *widget, const QRect &r, const QStyleOption& opt) const + + Draw a bitmask for the ComplexControl \a control using the painter + \a p in the area \a r. See drawComplexControl() for an explanation + of the use of the \a widget and \a opt arguments. + + The rect \a r should be in logical coordinates. Reimplementations + of this function should use visualRect() to change the logical + corrdinates into screen coordinates when using drawPrimitive() and + drawControl(). + + \sa drawComplexControl() ComplexControl +*/ + +/*! + \fn QRect QStyle::querySubControlMetrics( ComplexControl control, const QWidget *widget, SubControl subcontrol, const QStyleOption& opt = QStyleOption::Default ) const; + + Returns the rect for the SubControl \a subcontrol for \a widget in + logical coordinates. + + The \a widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a control. The \a opt argument can be used to + pass extra information required when drawing the ComplexControl. + Note that \a opt may be the default value even for ComplexControls + that can make use of the extra options. See drawComplexControl() + for an explanation of the \a widget and \a opt arguments. + + \sa drawComplexControl(), ComplexControl, SubControl +*/ + +/*! + \fn SubControl QStyle::querySubControl( ComplexControl control, const QWidget *widget, const QPoint &pos, const QStyleOption& opt = QStyleOption::Default ) const; + + Returns the SubControl for \a widget at the point \a pos. The \a + widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a control. The \a opt argument can be used to + pass extra information required when drawing the ComplexControl. + Note that \a opt may be the default value even for ComplexControls + that can make use of the extra options. See drawComplexControl() + for an explanation of the \a widget and \a opt arguments. + + Note that \a pos is passed in screen coordinates. When using + querySubControlMetrics() to check for hits and misses, use + visualRect() to change the logical coordinates into screen + coordinates. + + \sa drawComplexControl(), ComplexControl, SubControl, querySubControlMetrics() +*/ + +/*! + \enum QStyle::PixelMetric + + This enum represents a PixelMetric. A PixelMetric is a style + dependent size represented as a single pixel value. + + \value PM_ButtonMargin amount of whitespace between pushbutton + labels and the frame. + \value PM_ButtonDefaultIndicator width of the default-button indicator frame. + \value PM_MenuButtonIndicator width of the menu button indicator + proportional to the widget height. + \value PM_ButtonShiftHorizontal horizontal contents shift of a + button when the button is down. + \value PM_ButtonShiftVertical vertical contents shift of a button when the + button is down. + + \value PM_DefaultFrameWidth default frame width, usually 2. + \value PM_SpinBoxFrameWidth frame width of a spin box. + \value PM_MDIFrameWidth frame width of an MDI window. + \value PM_MDIMinimizedWidth width of a minimized MSI window. + + \value PM_MaximumDragDistance Some feels require the scrollbar or + other sliders to jump back to the original position when the + mouse pointer is too far away while dragging. A value of -1 + disables this behavior. + + \value PM_ScrollBarExtent width of a vertical scrollbar and the + height of a horizontal scrollbar. + \value PM_ScrollBarSliderMin the minimum height of a vertical + scrollbar's slider and the minimum width of a horiztonal + scrollbar slider. + + \value PM_SliderThickness total slider thickness. + \value PM_SliderControlThickness thickness of the slider handle. + \value PM_SliderLength length of the slider. + \value PM_SliderTickmarkOffset the offset between the tickmarks + and the slider. + \value PM_SliderSpaceAvailable the available space for the slider to move. + + \value PM_DockWindowSeparatorExtent width of a separator in a + horiztonal dock window and the height of a separator in a + vertical dock window. + \value PM_DockWindowHandleExtent width of the handle in a + horizontal dock window and the height of the handle in a + vertical dock window. + \value PM_DockWindowFrameWidth frame width of a dock window. + + \value PM_MenuBarFrameWidth frame width of a menubar. + + \value PM_MenuBarItemSpacing spacing between menubar items. + \value PM_ToolBarItemSpacing spacing between toolbar items. + + \value PM_TabBarTabOverlap number of pixels the tabs should overlap. + \value PM_TabBarTabHSpace extra space added to the tab width. + \value PM_TabBarTabVSpace extra space added to the tab height. + \value PM_TabBarBaseHeight height of the area between the tab bar + and the tab pages. + \value PM_TabBarBaseOverlap number of pixels the tab bar overlaps + the tab bar base. + \value PM_TabBarScrollButtonWidth + \value PM_TabBarTabShiftHorizontal horizontal pixel shift when a + tab is selected. + \value PM_TabBarTabShiftVertical vertical pixel shift when a + tab is selected. + + \value PM_ProgressBarChunkWidth width of a chunk in a progress bar indicator. + + \value PM_SplitterWidth width of a splitter. + + \value PM_TitleBarHeight height of the title bar. + \value PM_PopupMenuFrameHorizontalExtra additional border, e.g. for panels + \value PM_PopupMenuFrameVerticalExtra additional border, e.g. for panels + + \value PM_IndicatorWidth width of a check box indicator. + \value PM_IndicatorHeight height of a checkbox indicator. + \value PM_ExclusiveIndicatorWidth width of a radio button indicator. + \value PM_ExclusiveIndicatorHeight height of a radio button indicator. + + \value PM_PopupMenuScrollerHeight height of the scroller area in a popupmenu. + \value PM_PopupMenuScrollerHeight height of the scroller area in a popupmenu. + \value PM_CheckListButtonSize area (width/height) of the + checkbox/radiobutton in a QCheckListItem + \value PM_CheckListControllerSize area (width/height) of the + controller in a QCheckListItem + + \value PM_DialogButtonsSeparator distance between buttons in a dialog buttons widget. + \value PM_DialogButtonsButtonWidth minimum width of a button in a dialog buttons widget. + \value PM_DialogButtonsButtonHeight minimum height of a button in a dialog buttons widget. + + \value PM_HeaderMarkSize + \value PM_HeaderGripMargin + \value PM_HeaderMargin + + \value PM_CustomBase base value for custom ControlElements. All + values above this are reserved for custom use. Therefore, + custom values must be greater than this value. + + + \sa pixelMetric() +*/ + +/*! + \fn int QStyle::pixelMetric( PixelMetric metric, const QWidget *widget = 0 ) const; + + Returns the pixel metric for \a metric. The \a widget argument is + a pointer to a QWidget or one of its subclasses. The widget can be + cast to the appropriate type based on the value of \a metric. Note + that \a widget may be zero even for PixelMetrics that can make use + of \a widget. See the table below for the appropriate \a widget + casts: + + \table + \header \i PixelMetric \i Widget Cast + \row \i \l PM_SliderControlThickness \i (const \l QSlider *) + \row \i \l PM_SliderLength \i (const \l QSlider *) + \row \i \l PM_SliderTickmarkOffset \i (const \l QSlider *) + \row \i \l PM_SliderSpaceAvailable \i (const \l QSlider *) + \row \i \l PM_TabBarTabOverlap \i (const \l QTabBar *) + \row \i \l PM_TabBarTabHSpace \i (const \l QTabBar *) + \row \i \l PM_TabBarTabVSpace \i (const \l QTabBar *) + \row \i \l PM_TabBarBaseHeight \i (const \l QTabBar *) + \row \i \l PM_TabBarBaseOverlap \i (const \l QTabBar *) + \endtable +*/ + +/*! + \enum QStyle::ContentsType + + This enum represents a ContentsType. It is used to calculate sizes + for the contents of various widgets. + + \value CT_PushButton + \value CT_CheckBox + \value CT_RadioButton + \value CT_ToolButton + \value CT_ComboBox + \value CT_Splitter + \value CT_DockWindow + \value CT_ProgressBar + \value CT_PopupMenuItem + \value CT_TabBarTab + \value CT_Slider + \value CT_Header + \value CT_LineEdit + \value CT_MenuBar + \value CT_SpinBox + \value CT_SizeGrip + \value CT_TabWidget + \value CT_DialogButtons + + \value CT_CustomBase base value for custom ControlElements. All + values above this are reserved for custom use. Custom values + must be greater than this value. + + \sa sizeFromContents() +*/ + +/*! + \fn QSize QStyle::sizeFromContents( ContentsType contents, const QWidget *widget, const QSize &contentsSize, const QStyleOption& opt = QStyleOption::Default ) const; + + Returns the size of \a widget based on the contents size \a + contentsSize. + + The \a widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a contents. The \a opt argument can be used to + pass extra information required when calculating the size. Note + that \a opt may be the default value even for ContentsTypes that + can make use of the extra options. See the table below for the + appropriate \a widget and \a opt usage: + + \table + \header \i ContentsType \i Widget Cast \i Options \i Notes + \row \i \l CT_PushButton \i (const \l QPushButton *) \i Unused. \i + \row \i \l CT_CheckBox \i (const \l QCheckBox *) \i Unused. \i + \row \i \l CT_RadioButton \i (const \l QRadioButton *) \i Unused. \i + \row \i \l CT_ToolButton \i (const \l QToolButton *) \i Unused. \i + \row \i \l CT_ComboBox \i (const \l QComboBox *) \i Unused. \i + \row \i \l CT_Splitter \i (const \l QSplitter *) \i Unused. \i + \row \i \l CT_DockWindow \i (const \l QDockWindow *) \i Unused. \i + \row \i \l CT_ProgressBar \i (const \l QProgressBar *) \i Unused. \i + \row \i \l CT_PopupMenuItem \i (const QPopupMenu *) + \i \l QStyleOption ( QMenuItem *mi ) + \list + \i opt.\link QStyleOption::menuItem() menuItem\endlink() + \endlist + \i \e mi is the menu item to use when calculating the size. + QMenuItem is currently an internal class. + \endtable +*/ + +/*! + \enum QStyle::StyleHint + + This enum represents a StyleHint. A StyleHint is a general look + and/or feel hint. + + \value SH_EtchDisabledText disabled text is "etched" like Windows. + + \value SH_GUIStyle the GUI style to use. + + \value SH_ScrollBar_BackgroundMode the background mode for a + QScrollBar. Possible values are any of those in the \link + Qt::BackgroundMode BackgroundMode\endlink enum. + + \value SH_ScrollBar_MiddleClickAbsolutePosition a boolean value. + If TRUE, middle clicking on a scrollbar causes the slider to + jump to that position. If FALSE, the middle clicking is + ignored. + + \value SH_ScrollBar_LeftClickAbsolutePosition a boolean value. + If TRUE, left clicking on a scrollbar causes the slider to + jump to that position. If FALSE, the left clicking will + behave as appropriate for each control. + + \value SH_ScrollBar_ScrollWhenPointerLeavesControl a boolean + value. If TRUE, when clicking a scrollbar SubControl, holding + the mouse button down and moving the pointer outside the + SubControl, the scrollbar continues to scroll. If FALSE, the + scollbar stops scrolling when the pointer leaves the + SubControl. + + \value SH_TabBar_Alignment the alignment for tabs in a + QTabWidget. Possible values are \c Qt::AlignLeft, \c + Qt::AlignCenter and \c Qt::AlignRight. + + \value SH_Header_ArrowAlignment the placement of the sorting + indicator may appear in list or table headers. Possible values + are \c Qt::Left or \c Qt::Right. + + \value SH_Slider_SnapToValue sliders snap to values while moving, + like Windows + + \value SH_Slider_SloppyKeyEvents key presses handled in a sloppy + manner, i.e. left on a vertical slider subtracts a line. + + \value SH_ProgressDialog_CenterCancelButton center button on + progress dialogs, like Motif, otherwise right aligned. + + \value SH_ProgressDialog_TextLabelAlignment Qt::AlignmentFlags -- + text label alignment in progress dialogs; Center on windows, + Auto|VCenter otherwise. + + \value SH_PrintDialog_RightAlignButtons right align buttons in + the print dialog, like Windows. + + \value SH_MainWindow_SpaceBelowMenuBar 1 or 2 pixel space between + the menubar and the dockarea, like Windows. + + \value SH_FontDialog_SelectAssociatedText select the text in the + line edit, or when selecting an item from the listbox, or when + the line edit receives focus, like Windows. + + \value SH_PopupMenu_AllowActiveAndDisabled allows disabled menu + items to be active. + + \value SH_PopupMenu_SpaceActivatesItem pressing Space activates + the item, like Motif. + + \value SH_PopupMenu_SubMenuPopupDelay the number of milliseconds + to wait before opening a submenu; 256 on windows, 96 on Motif. + + \value SH_PopupMenu_Scrollable whether popupmenu's must support + scrolling. + + \value SH_PopupMenu_SloppySubMenus whether popupmenu's must support + sloppy submenu; as implemented on Mac OS. + + \value SH_ScrollView_FrameOnlyAroundContents whether scrollviews + draw their frame only around contents (like Motif), or around + contents, scrollbars and corner widgets (like Windows). + + \value SH_MenuBar_AltKeyNavigation menubars items are navigable + by pressing Alt, followed by using the arrow keys to select + the desired item. + + \value SH_ComboBox_ListMouseTracking mouse tracking in combobox + dropdown lists. + + \value SH_PopupMenu_MouseTracking mouse tracking in popup menus. + + \value SH_MenuBar_MouseTracking mouse tracking in menubars. + + \value SH_ItemView_ChangeHighlightOnFocus gray out selected items + when losing focus. + + \value SH_Widget_ShareActivation turn on sharing activation with + floating modeless dialogs. + + \value SH_TabBar_SelectMouseType which type of mouse event should + cause a tab to be selected. + + \value SH_ListViewExpand_SelectMouseType which type of mouse event should + cause a listview expansion to be selected. + + \value SH_TabBar_PreferNoArrows whether a tabbar should suggest a size + to prevent scoll arrows. + + \value SH_ComboBox_Popup allows popups as a combobox dropdown + menu. + + \value SH_Workspace_FillSpaceOnMaximize the workspace should + maximize the client area. + + \value SH_TitleBar_NoBorder the titlebar has no border + + \value SH_ScrollBar_StopMouseOverSlider stops autorepeat when + slider reaches mouse + + \value SH_BlinkCursorWhenTextSelected whether cursor should blink + when text is selected + + \value SH_RichText_FullWidthSelection whether richtext selections + should extend the full width of the document. + + \value SH_GroupBox_TextLabelVerticalAlignment how to vertically align a + groupbox's text label. + + \value SH_GroupBox_TextLabelColor how to paint a groupbox's text label. + + \value SH_DialogButtons_DefaultButton which buttons gets the + default status in a dialog's button widget. + + \value SH_CustomBase base value for custom ControlElements. All + values above this are reserved for custom use. Therefore, + custom values must be greater than this value. + + \value SH_ToolButton_Uses3D indicates whether QToolButtons should + use a 3D frame when the mouse is over them + + \value SH_ToolBox_SelectedPageTitleBold Boldness of the selected + page title in a QToolBox. + + \value SH_LineEdit_PasswordCharacter The QChar Unicode character + to be used for passwords. + + \value SH_Table_GridLineColor + + \value SH_UnderlineAccelerator whether accelerators are underlined + + \sa styleHint() +*/ + +/*! + \fn int QStyle::styleHint( StyleHint stylehint, const QWidget *widget = 0, const QStyleOption &opt = QStyleOption::Default, QStyleHintReturn *returnData = 0 ) const; + + Returns the style hint \a stylehint for \a widget. Currently, \a + widget, \a opt, and \a returnData are unused; they're included to + allow for future enhancements. + + For an explanation of the return value see \l StyleHint. +*/ + +/*! + \enum QStyle::StylePixmap + + This enum represents a StylePixmap. A StylePixmap is a pixmap that + can follow some existing GUI style or guideline. + + + \value SP_TitleBarMinButton minimize button on titlebars. For + example, in a QWorkspace. + \value SP_TitleBarMaxButton maximize button on titlebars. + \value SP_TitleBarCloseButton close button on titlebars. + \value SP_TitleBarNormalButton normal (restore) button on titlebars. + \value SP_TitleBarShadeButton shade button on titlebars. + \value SP_TitleBarUnshadeButton unshade button on titlebars. + \value SP_MessageBoxInformation the 'information' icon. + \value SP_MessageBoxWarning the 'warning' icon. + \value SP_MessageBoxCritical the 'critical' icon. + \value SP_MessageBoxQuestion the 'question' icon. + + + \value SP_DockWindowCloseButton close button on dock windows; + see also QDockWindow. + + + \value SP_CustomBase base value for custom ControlElements. All + values above this are reserved for custom use. Therefore, + custom values must be greater than this value. + + \sa stylePixmap() +*/ + +/*! + \fn QPixmap QStyle::stylePixmap( StylePixmap stylepixmap, const QWidget *widget = 0, const QStyleOption& opt = QStyleOption::Default ) const; + + Returns a pixmap for \a stylepixmap. + + The \a opt argument can be used to pass extra information required + when drawing the ControlElement. Note that \a opt may be the + default value even for StylePixmaps that can make use of the extra + options. Currently, the \a opt argument is unused. + + The \a widget argument is a pointer to a QWidget or one of its + subclasses. The widget can be cast to the appropriate type based + on the value of \a stylepixmap. See the table below for the + appropriate \a widget casts: + + \table + \header \i StylePixmap \i Widget Cast + \row \i \l SP_TitleBarMinButton \i (const \l QWidget *) + \row \i \l SP_TitleBarMaxButton \i (const \l QWidget *) + \row \i \l SP_TitleBarCloseButton \i (const \l QWidget *) + \row \i \l SP_TitleBarNormalButton \i (const \l QWidget *) + \row \i \l SP_TitleBarShadeButton \i (const \l QWidget *) + \row \i \l SP_TitleBarUnshadeButton \i (const \l QWidget *) + \row \i \l SP_DockWindowCloseButton \i (const \l QDockWindow *) + \endtable + + \sa StylePixmap +*/ + +/*! + \fn QRect QStyle::visualRect( const QRect &logical, const QWidget *w ); + + Returns the rect \a logical in screen coordinates. The bounding + rect for widget \a w is used to perform the translation. This + function is provided to aid style implementors in supporting + right-to-left mode. + + \sa QApplication::reverseLayout() +*/ +QRect QStyle::visualRect( const QRect &logical, const QWidget *w ) +{ + QRect boundingRect = w->rect(); + QRect r = logical; + if ( QApplication::reverseLayout() ) + r.moveBy( 2*(boundingRect.right() - logical.right()) + + logical.width() - boundingRect.width(), 0 ); + return r; +} + +/*! + \overload QRect QStyle::visualRect( const QRect &logical, const QRect &bounding ); + + Returns the rect \a logical in screen coordinates. The rect \a + bounding is used to perform the translation. This function is + provided to aid style implementors in supporting right-to-left + mode. + + \sa QApplication::reverseLayout() +*/ +QRect QStyle::visualRect( const QRect &logical, const QRect &boundingRect ) +{ + QRect r = logical; + if ( QApplication::reverseLayout() ) + r.moveBy( 2*(boundingRect.right() - logical.right()) + + logical.width() - boundingRect.width(), 0 ); + return r; +} + +/*! + \fn int QStyle::defaultFrameWidth() const + \obsolete +*/ + +/*! + \fn void QStyle::tabbarMetrics( const QWidget *, int &, int &, int & ) const + \obsolete +*/ + +/*! + \fn QSize QStyle::scrollBarExtent() const + \obsolete +*/ + +#endif // QT_NO_STYLE diff --git a/src/kernel/qstyle.h b/src/kernel/qstyle.h new file mode 100644 index 0000000..5531def --- /dev/null +++ b/src/kernel/qstyle.h @@ -0,0 +1,756 @@ +/**************************************************************************** +** +** Definition of QStyle class +** +** Created : 980616 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ +#ifndef QSTYLE_H +#define QSTYLE_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + + +#ifndef QT_NO_STYLE + +class QPopupMenu; +class QStylePrivate; +class QMenuItem; +class QTab; +class QListViewItem; +class QCheckListItem; + +class QStyleOption { +public: + enum StyleOptionDefault { Default }; + + QStyleOption(StyleOptionDefault=Default) : def(TRUE) {} + + // Note: we don't use default arguments since that is unnecessary + // initialization. + QStyleOption(int in1) : + def(FALSE), i1(in1) {} + QStyleOption(int in1, int in2) : + def(FALSE), i1(in1), i2(in2) {} + QStyleOption(int in1, int in2, int in3, int in4) : + def(FALSE), i1(in1), i2(in2), i3(in3), i4(in4) {} + QStyleOption(QMenuItem* m) : def(FALSE), mi(m) {} + QStyleOption(QMenuItem* m, int in1) : def(FALSE), mi(m), i1(in1) {} + QStyleOption(QMenuItem* m, int in1, int in2) : def(FALSE), mi(m), i1(in1), i2(in2) {} + QStyleOption(const QColor& c) : def(FALSE), cl(&c) {} + QStyleOption(QTab* t) : def(FALSE), tb(t) {} + QStyleOption(QListViewItem* i) : def(FALSE), li(i) {} + QStyleOption(QCheckListItem* i) : def(FALSE), cli(i) {} + QStyleOption(Qt::ArrowType a) : def(FALSE), i1((int)a) {} + QStyleOption(const QRect& r) : def(FALSE), i1(r.x()), i2(r.y()), i3(r.width()),i4(r.height()){} + QStyleOption(QWidget *w) : def(FALSE), p1((void*)w) {} + + bool isDefault() const { return def; } + + int day() const { return i1; } + + int lineWidth() const { return i1; } + int midLineWidth() const { return i2; } + int frameShape() const { return i3; } + int frameShadow() const { return i4; } + + int headerSection() const { return i1; } + QMenuItem* menuItem() const { return mi; } + int maxIconWidth() const { return i1; } + int tabWidth() const { return i2; } + + const QColor& color() const { return *cl; } + + QTab* tab() const { return tb; } + + QCheckListItem* checkListItem() const { return cli; } + QListViewItem* listViewItem() const { return li; } + + Qt::ArrowType arrowType() const { return (Qt::ArrowType)i1; } + QRect rect() const { return QRect( i1, i2, i3, i4 ); } + QWidget* widget() const { return (QWidget*)p1; } + +private: + // NOTE: none of these components have constructors. + bool def; + bool b1,b2,b3; // reserved + QMenuItem* mi; + QTab* tb; + QListViewItem* li; + const QColor* cl; + int i1, i2, i3, i4; + int i5, i6; // reserved + QCheckListItem* cli; + void *p1, *p2, *p3, *p4; // reserved + // (padded to 64 bytes on some architectures) +}; + +class QStyleHintReturn; // not defined yet + +class Q_EXPORT QStyle: public QObject +{ + Q_OBJECT + +public: + QStyle(); + virtual ~QStyle(); + + // New QStyle API - most of these should probably be pure virtual + + virtual void polish( QWidget * ); + virtual void unPolish( QWidget * ); + + virtual void polish( QApplication * ); + virtual void unPolish( QApplication * ); + + virtual void polish( QPalette & ); + + virtual void polishPopupMenu( QPopupMenu* ) = 0; + + virtual QRect itemRect( QPainter *p, const QRect &r, + int flags, bool enabled, + const QPixmap *pixmap, + const QString &text, int len = -1 ) const; + + virtual void drawItem( QPainter *p, const QRect &r, + int flags, const QColorGroup &g, bool enabled, + const QPixmap *pixmap, const QString &text, + int len = -1, const QColor *penColor = 0 ) const; + + + enum PrimitiveElement { + PE_ButtonCommand, + PE_ButtonDefault, + PE_ButtonBevel, + PE_ButtonTool, + PE_ButtonDropDown, + + PE_FocusRect, + + PE_ArrowUp, + PE_ArrowDown, + PE_ArrowRight, + PE_ArrowLeft, + + PE_SpinWidgetUp, + PE_SpinWidgetDown, + PE_SpinWidgetPlus, + PE_SpinWidgetMinus, + + PE_Indicator, + PE_IndicatorMask, + PE_ExclusiveIndicator, + PE_ExclusiveIndicatorMask, + + PE_DockWindowHandle, + PE_DockWindowSeparator, + PE_DockWindowResizeHandle, + + PE_Splitter, + + PE_Panel, + PE_PanelPopup, + PE_PanelMenuBar, + PE_PanelDockWindow, + + PE_TabBarBase, + + PE_HeaderSection, + PE_HeaderArrow, + PE_StatusBarSection, + + PE_GroupBoxFrame, + + PE_Separator, + + PE_SizeGrip, + + PE_CheckMark, + + PE_ScrollBarAddLine, + PE_ScrollBarSubLine, + PE_ScrollBarAddPage, + PE_ScrollBarSubPage, + PE_ScrollBarSlider, + PE_ScrollBarFirst, + PE_ScrollBarLast, + + PE_ProgressBarChunk, + + PE_PanelLineEdit, + PE_PanelTabWidget, + + PE_WindowFrame, + + PE_CheckListController, + PE_CheckListIndicator, + PE_CheckListExclusiveIndicator, + + PE_PanelGroupBox, + PE_RubberBand, + + // do not add any values below/greater this + PE_CustomBase = 0xf000000 + }; + + enum StyleFlags { + Style_Default = 0x00000000, + Style_Enabled = 0x00000001, + Style_Raised = 0x00000002, + Style_Sunken = 0x00000004, + Style_Off = 0x00000008, + Style_NoChange = 0x00000010, + Style_On = 0x00000020, + Style_Down = 0x00000040, + Style_Horizontal = 0x00000080, + Style_HasFocus = 0x00000100, + Style_Top = 0x00000200, + Style_Bottom = 0x00000400, + Style_FocusAtBorder = 0x00000800, + Style_AutoRaise = 0x00001000, + Style_MouseOver = 0x00002000, + Style_Up = 0x00004000, + Style_Selected = 0x00008000, + Style_Active = 0x00010000, + Style_ButtonDefault = 0x00020000 + }; + typedef uint SFlags; + + virtual void drawPrimitive( PrimitiveElement pe, + QPainter *p, + const QRect &r, + const QColorGroup &cg, + SFlags flags = Style_Default, + const QStyleOption& = QStyleOption::Default ) const = 0; + + + enum ControlElement { + CE_PushButton, + CE_PushButtonLabel, + + CE_CheckBox, + CE_CheckBoxLabel, + + CE_RadioButton, + CE_RadioButtonLabel, + + CE_TabBarTab, + CE_TabBarLabel, + + CE_ProgressBarGroove, + CE_ProgressBarContents, + CE_ProgressBarLabel, + + CE_PopupMenuItem, + CE_MenuBarItem, + + CE_ToolButtonLabel, + CE_MenuBarEmptyArea, + CE_PopupMenuScroller, + CE_DockWindowEmptyArea, + CE_PopupMenuVerticalExtra, + CE_PopupMenuHorizontalExtra, + + CE_ToolBoxTab, + CE_HeaderLabel, + + // do not add any values below/greater than this + CE_CustomBase = 0xf0000000 + }; + + virtual void drawControl( ControlElement element, + QPainter *p, + const QWidget *widget, + const QRect &r, + const QColorGroup &cg, + SFlags how = Style_Default, + const QStyleOption& = QStyleOption::Default ) const = 0; + virtual void drawControlMask( ControlElement element, + QPainter *p, + const QWidget *widget, + const QRect &r, + const QStyleOption& = QStyleOption::Default ) const = 0; + + enum SubRect { + SR_PushButtonContents, + SR_PushButtonFocusRect, + + SR_CheckBoxIndicator, + SR_CheckBoxContents, + SR_CheckBoxFocusRect, + + SR_RadioButtonIndicator, + SR_RadioButtonContents, + SR_RadioButtonFocusRect, + + SR_ComboBoxFocusRect, + + SR_SliderFocusRect, + + SR_DockWindowHandleRect, + + SR_ProgressBarGroove, + SR_ProgressBarContents, + SR_ProgressBarLabel, + + SR_ToolButtonContents, + + SR_DialogButtonAccept, + SR_DialogButtonReject, + SR_DialogButtonApply, + SR_DialogButtonHelp, + SR_DialogButtonAll, + SR_DialogButtonAbort, + SR_DialogButtonIgnore, + SR_DialogButtonRetry, + SR_DialogButtonCustom, + + SR_ToolBoxTabContents, + + // do not add any values below/greater than this + SR_CustomBase = 0xf0000000 + }; + + virtual QRect subRect( SubRect r, const QWidget *widget ) const = 0; + + + enum ComplexControl{ + CC_SpinWidget, + CC_ComboBox, + CC_ScrollBar, + CC_Slider, + CC_ToolButton, + CC_TitleBar, + CC_ListView, + + // do not add any values below/greater than this + CC_CustomBase = 0xf0000000 + }; + + enum SubControl { + SC_None = 0x00000000, + + SC_ScrollBarAddLine = 0x00000001, + SC_ScrollBarSubLine = 0x00000002, + SC_ScrollBarAddPage = 0x00000004, + SC_ScrollBarSubPage = 0x00000008, + SC_ScrollBarFirst = 0x00000010, + SC_ScrollBarLast = 0x00000020, + SC_ScrollBarSlider = 0x00000040, + SC_ScrollBarGroove = 0x00000080, + + SC_SpinWidgetUp = 0x00000001, + SC_SpinWidgetDown = 0x00000002, + SC_SpinWidgetFrame = 0x00000004, + SC_SpinWidgetEditField = 0x00000008, + SC_SpinWidgetButtonField = 0x00000010, + + SC_ComboBoxFrame = 0x00000001, + SC_ComboBoxEditField = 0x00000002, + SC_ComboBoxArrow = 0x00000004, + SC_ComboBoxListBoxPopup = 0x00000008, + + SC_SliderGroove = 0x00000001, + SC_SliderHandle = 0x00000002, + SC_SliderTickmarks = 0x00000004, + + SC_ToolButton = 0x00000001, + SC_ToolButtonMenu = 0x00000002, + + SC_TitleBarLabel = 0x00000001, + SC_TitleBarSysMenu = 0x00000002, + SC_TitleBarMinButton = 0x00000004, + SC_TitleBarMaxButton = 0x00000008, + SC_TitleBarCloseButton = 0x00000010, + SC_TitleBarNormalButton = 0x00000020, + SC_TitleBarShadeButton = 0x00000040, + SC_TitleBarUnshadeButton = 0x00000080, + + SC_ListView = 0x00000001, + SC_ListViewBranch = 0x00000002, + SC_ListViewExpand = 0x00000004, + + SC_All = 0xffffffff + }; + typedef uint SCFlags; + + + virtual void drawComplexControl( ComplexControl control, + QPainter *p, + const QWidget *widget, + const QRect &r, + const QColorGroup &cg, + SFlags how = Style_Default, +#ifdef Q_QDOC + SCFlags sub = SC_All, +#else + SCFlags sub = (uint)SC_All, +#endif + SCFlags subActive = SC_None, + const QStyleOption& = QStyleOption::Default ) const = 0; + virtual void drawComplexControlMask( ComplexControl control, + QPainter *p, + const QWidget *widget, + const QRect &r, + const QStyleOption& = QStyleOption::Default ) const = 0; + + virtual QRect querySubControlMetrics( ComplexControl control, + const QWidget *widget, + SubControl sc, + const QStyleOption& = QStyleOption::Default ) const = 0; + virtual SubControl querySubControl( ComplexControl control, + const QWidget *widget, + const QPoint &pos, + const QStyleOption& = QStyleOption::Default ) const = 0; + + + enum PixelMetric { + PM_ButtonMargin, + PM_ButtonDefaultIndicator, + PM_MenuButtonIndicator, + PM_ButtonShiftHorizontal, + PM_ButtonShiftVertical, + + PM_DefaultFrameWidth, + PM_SpinBoxFrameWidth, + + PM_MaximumDragDistance, + + PM_ScrollBarExtent, + PM_ScrollBarSliderMin, + + PM_SliderThickness, // total slider thickness + PM_SliderControlThickness, // thickness of the business part + PM_SliderLength, // total length of slider + PM_SliderTickmarkOffset, // + PM_SliderSpaceAvailable, // available space for slider to move + + PM_DockWindowSeparatorExtent, + PM_DockWindowHandleExtent, + PM_DockWindowFrameWidth, + + PM_MenuBarFrameWidth, + + PM_TabBarTabOverlap, + PM_TabBarTabHSpace, + PM_TabBarTabVSpace, + PM_TabBarBaseHeight, + PM_TabBarBaseOverlap, + + PM_ProgressBarChunkWidth, + + PM_SplitterWidth, + PM_TitleBarHeight, + + PM_IndicatorWidth, + PM_IndicatorHeight, + PM_ExclusiveIndicatorWidth, + PM_ExclusiveIndicatorHeight, + PM_PopupMenuScrollerHeight, + PM_CheckListButtonSize, + PM_CheckListControllerSize, + PM_PopupMenuFrameHorizontalExtra, + PM_PopupMenuFrameVerticalExtra, + + PM_DialogButtonsSeparator, + PM_DialogButtonsButtonWidth, + PM_DialogButtonsButtonHeight, + + PM_MDIFrameWidth, + PM_MDIMinimizedWidth, + PM_HeaderMargin, + PM_HeaderMarkSize, + PM_HeaderGripMargin, + PM_TabBarTabShiftHorizontal, + PM_TabBarTabShiftVertical, + PM_TabBarScrollButtonWidth, + + PM_MenuBarItemSpacing, + PM_ToolBarItemSpacing, + + // do not add any values below/greater than this + PM_CustomBase = 0xf0000000 + }; + + virtual int pixelMetric( PixelMetric metric, + const QWidget *widget = 0 ) const = 0; + + + enum ContentsType { + CT_PushButton, + CT_CheckBox, + CT_RadioButton, + CT_ToolButton, + CT_ComboBox, + CT_Splitter, + CT_DockWindow, + CT_ProgressBar, + CT_PopupMenuItem, + CT_TabBarTab, + CT_Slider, + CT_Header, + CT_LineEdit, + CT_MenuBar, + CT_SpinBox, + CT_SizeGrip, + CT_TabWidget, + CT_DialogButtons, + + // do not add any values below/greater than this + CT_CustomBase = 0xf0000000 + }; + + virtual QSize sizeFromContents( ContentsType contents, + const QWidget *widget, + const QSize &contentsSize, + const QStyleOption& = QStyleOption::Default ) const = 0; + + enum StyleHint { + // ... + // the general hints + // ... + // disabled text should be etched, ala Windows + SH_EtchDisabledText, + + // the GUI style enum, argh! + SH_GUIStyle, + + // ... + // widget specific hints + // ... + SH_ScrollBar_BackgroundMode, + SH_ScrollBar_MiddleClickAbsolutePosition, + SH_ScrollBar_ScrollWhenPointerLeavesControl, + + // QEvent::Type - which mouse event to select a tab + SH_TabBar_SelectMouseType, + + SH_TabBar_Alignment, + + SH_Header_ArrowAlignment, + + // bool - sliders snap to values while moving, ala Windows + SH_Slider_SnapToValue, + + // bool - key presses handled in a sloppy manner - ie. left on a vertical + // slider subtracts a line + SH_Slider_SloppyKeyEvents, + + // bool - center button on progress dialogs, ala Motif, else right aligned + // perhaps this should be a Qt::Alignment value + SH_ProgressDialog_CenterCancelButton, + + // Qt::AlignmentFlags - text label alignment in progress dialogs + // Center on windows, Auto|VCenter otherwize + SH_ProgressDialog_TextLabelAlignment, + + // bool - right align buttons on print dialog, ala Windows + SH_PrintDialog_RightAlignButtons, + + // bool - 1 or 2 pixel space between the menubar and the dockarea, ala Windows + // this *REALLY* needs a better name + SH_MainWindow_SpaceBelowMenuBar, + + // bool - select the text in the line edit about the listbox when selecting + // an item from the listbox, or when the line edit receives focus, ala Windows + SH_FontDialog_SelectAssociatedText, + + // bool - allows disabled menu items to be active + SH_PopupMenu_AllowActiveAndDisabled, + + // bool - pressing space activates item, ala Motif + SH_PopupMenu_SpaceActivatesItem, + + // int - number of milliseconds to wait before opening a submenu + // 256 on windows, 96 on motif + SH_PopupMenu_SubMenuPopupDelay, + + // bool - should scrollviews draw their frame only around contents (ala Motif), + // or around contents, scrollbars and corner widgets (ala Windows) ? + SH_ScrollView_FrameOnlyAroundContents, + + // bool - menubars items are navigatable by pressing alt, followed by using + // the arrow keys to select the desired item + SH_MenuBar_AltKeyNavigation, + + // bool - mouse tracking in combobox dropdown lists + SH_ComboBox_ListMouseTracking, + + // bool - mouse tracking in popupmenus + SH_PopupMenu_MouseTracking, + + // bool - mouse tracking in menubars + SH_MenuBar_MouseTracking, + + // bool - gray out selected items when loosing focus + SH_ItemView_ChangeHighlightOnFocus, + + // bool - supports shared activation among modeless widgets + SH_Widget_ShareActivation, + + // bool - workspace should just maximize the client area + SH_Workspace_FillSpaceOnMaximize, + + // bool - supports popup menu comboboxes + SH_ComboBox_Popup, + + // bool - titlebar has no border + SH_TitleBar_NoBorder, + + // bool - stop scrollbar at mouse + SH_ScrollBar_StopMouseOverSlider, + + //bool - blink cursort with selected text + SH_BlinkCursorWhenTextSelected, + + //bool - richtext selections extend the full width of the docuemnt + SH_RichText_FullWidthSelection, + + //bool - popupmenu supports scrolling instead of multicolumn mode + SH_PopupMenu_Scrollable, + + // Qt::AlignmentFlags - text label vertical alignment in groupboxes + // Center on windows, Auto|VCenter otherwize + SH_GroupBox_TextLabelVerticalAlignment, + + // Qt::QRgb - text label color in groupboxes + SH_GroupBox_TextLabelColor, + + // bool - popupmenu supports sloppy submenus + SH_PopupMenu_SloppySubMenus, + + // Qt::QRgb - table grid color + SH_Table_GridLineColor, + + // QChar - Unicode character for password char + SH_LineEdit_PasswordCharacter, + + // QDialogButtons::Button - default button + SH_DialogButtons_DefaultButton, + + // QToolBox - Boldness of the selected page title + SH_ToolBox_SelectedPageTitleBold, + + //bool - if a tabbar prefers not to have scroller arrows + SH_TabBar_PreferNoArrows, + + //bool - if left button should cause an absolute position + SH_ScrollBar_LeftClickAbsolutePosition, + + // QEvent::Type - which mouse event to select a list view expansion + SH_ListViewExpand_SelectMouseType, + + //bool - if underline for accelerators + SH_UnderlineAccelerator, + + // bool - QToolButton - if tool buttons should use a 3D frame + // when the mouse is over the button + SH_ToolButton_Uses3D, + + // do not add any values below/greater than this + SH_CustomBase = 0xf0000000 + }; + + virtual int styleHint( StyleHint stylehint, + const QWidget *widget = 0, + const QStyleOption& = QStyleOption::Default, + QStyleHintReturn* returnData = 0 + ) const = 0; + + + enum StylePixmap { + SP_TitleBarMinButton, + SP_TitleBarMaxButton, + SP_TitleBarCloseButton, + SP_TitleBarNormalButton, + SP_TitleBarShadeButton, + SP_TitleBarUnshadeButton, + SP_DockWindowCloseButton, + SP_MessageBoxInformation, + SP_MessageBoxWarning, + SP_MessageBoxCritical, + SP_MessageBoxQuestion, + + // do not add any values below/greater than this + SP_CustomBase = 0xf0000000 + }; + + virtual QPixmap stylePixmap( StylePixmap stylepixmap, + const QWidget *widget = 0, + const QStyleOption& = QStyleOption::Default ) const = 0; + + + static QRect visualRect( const QRect &logical, const QWidget *w ); + + static QRect visualRect( const QRect &logical, const QRect &bounding ); + + + + + // Old 2.x QStyle API + +#ifndef QT_NO_COMPAT + int defaultFrameWidth() const + { + return pixelMetric( PM_DefaultFrameWidth ); + } + void tabbarMetrics( const QWidget* t, + int& hf, int& vf, int& ov ) const + { + hf = pixelMetric( PM_TabBarTabHSpace, t ); + vf = pixelMetric( PM_TabBarTabVSpace, t ); + ov = pixelMetric( PM_TabBarBaseOverlap, t ); + } + QSize scrollBarExtent() const + { + return QSize(pixelMetric(PM_ScrollBarExtent), + pixelMetric(PM_ScrollBarExtent)); + } +#endif + + +private: + QStylePrivate * d; + +#if defined(Q_DISABLE_COPY) + QStyle( const QStyle & ); + QStyle& operator=( const QStyle & ); +#endif +}; + +#endif // QT_NO_STYLE +#endif // QSTYLE_H diff --git a/src/kernel/qstylesheet.cpp b/src/kernel/qstylesheet.cpp new file mode 100644 index 0000000..a61914d --- /dev/null +++ b/src/kernel/qstylesheet.cpp @@ -0,0 +1,1623 @@ +/**************************************************************************** +** +** Implementation of the QStyleSheet class +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qstylesheet.h" + +#ifndef QT_NO_RICHTEXT + +#include "private/qrichtext_p.h" +#include "qlayout.h" +#include "qpainter.h" +#include "qcleanuphandler.h" + +#include <stdio.h> + +class QStyleSheetItemData +{ +public: + QStyleSheetItem::DisplayMode disp; + int fontitalic; + int fontunderline; + int fontstrikeout; + int fontweight; + int fontsize; + int fontsizelog; + int fontsizestep; + int lineSpacing; + QString fontfamily; + QStyleSheetItem *parentstyle; + QString stylename; + int ncolumns; + QColor col; + bool anchor; + int align; + QStyleSheetItem::VerticalAlignment valign; + int margin[5]; + QStyleSheetItem::ListStyle list; + QStyleSheetItem::WhiteSpaceMode whitespacemode; + QString contxt; + bool selfnest; + QStyleSheet* sheet; +}; + +/*! + \class QStyleSheetItem qstylesheet.h + \brief The QStyleSheetItem 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 QStyleSheet + style sheet\endlink (see styleSheet()), items define the name() of + a rich text tag and the display property changes associated with + it. + + The \link QStyleSheetItem::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 + QStyleSheetItem::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 QStyleSheetItem::AdditionalStyleValues + \internal +*/ + +/*! + \enum QStyleSheetItem::WhiteSpaceMode + + This enum defines the ways in which QStyleSheet 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{<br>} tag. + +*/ + +/*! + \enum QStyleSheetItem::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 QStyleSheetItem are initially in the "do not + change" state, except \link QStyleSheetItem::DisplayMode display + mode\endlink, which defaults to \c DisplayInline. +*/ +QStyleSheetItem::QStyleSheetItem( QStyleSheet* parent, const QString& name ) +{ + d = new QStyleSheetItemData; + 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. +*/ +QStyleSheetItem::QStyleSheetItem( const QStyleSheetItem & other ) +{ + d = new QStyleSheetItemData; + *d = *other.d; +} + + +/*! + Destroys the style. Note that QStyleSheetItem objects become + owned by QStyleSheet when they are created. +*/ +QStyleSheetItem::~QStyleSheetItem() +{ + delete d; +} + +/*! + Assignment. Assings a copy of \a other that is not bound to any style sheet. + Unbounds first from previous style sheet. + */ +QStyleSheetItem& QStyleSheetItem::operator=( const QStyleSheetItem& other ) +{ + if ( &other == this ) + return *this; + delete d; + d = new QStyleSheetItemData; + *d = *other.d; + return *this; +} + +/*! + Returns the style sheet this item is in. +*/ +QStyleSheet* QStyleSheetItem::styleSheet() +{ + return d->sheet; +} + +/*! + \overload + + Returns the style sheet this item is in. +*/ +const QStyleSheet* QStyleSheetItem::styleSheet() const +{ + return d->sheet; +} + +/*! + \internal + Internal initialization + */ +void QStyleSheetItem::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 = QColor(); // !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. +*/ +QString QStyleSheetItem::name() const +{ + return d->stylename; +} + +/*! + Returns the \link QStyleSheetItem::DisplayMode display + mode\endlink of the style. + + \sa setDisplayMode() +*/ +QStyleSheetItem::DisplayMode QStyleSheetItem::displayMode() const +{ + return d->disp; +} + +/*! + \enum QStyleSheetItem::DisplayMode + + This enum type defines the way adjacent elements are displayed. + + \value DisplayBlock elements are displayed as a rectangular block + (e.g. \c{<p>...</p>}). + + \value DisplayInline elements are displayed in a horizontally + flowing sequence (e.g. \c{<em>...</em>}). + + \value DisplayListItem elements are displayed in a vertical + sequence (e.g. \c{<li>...</li>}). + + \value DisplayNone elements are not displayed at all. +*/ + +/*! + Sets the display mode of the style to \a m. + + \sa displayMode() + */ +void QStyleSheetItem::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(), Qt::AlignmentFlags +*/ +int QStyleSheetItem::alignment() const +{ + return d->align; +} + +/*! + Sets the alignment to \a f. This only makes sense for styles with + a \link QStyleSheetItem::DisplayMode display mode\endlink of + DisplayBlock. Possible values are \c AlignAuto, \c AlignLeft, + \c AlignRight, \c AlignCenter or \c AlignJustify. + + \sa alignment(), displayMode(), Qt::AlignmentFlags +*/ +void QStyleSheetItem::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() +*/ +QStyleSheetItem::VerticalAlignment QStyleSheetItem::verticalAlignment() const +{ + return d->valign; +} + +/*! + \enum QStyleSheetItem::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 QStyleSheetItem::setVerticalAlignment( VerticalAlignment valign ) +{ + d->valign = valign; +} + + +/*! + Returns TRUE if the style sets an italic font; otherwise returns + FALSE. + + \sa setFontItalic(), definesFontItalic() +*/ +bool QStyleSheetItem::fontItalic() const +{ + return d->fontitalic > 0; +} + +/*! + If \a italic is TRUE sets italic for the style; otherwise sets + upright. + + \sa fontItalic(), definesFontItalic() +*/ +void QStyleSheetItem::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 QStyleSheetItem::definesFontItalic() const +{ + return d->fontitalic != Undefined; +} + +/*! + Returns TRUE if the style sets an underlined font; otherwise + returns FALSE. + + \sa setFontUnderline(), definesFontUnderline() +*/ +bool QStyleSheetItem::fontUnderline() const +{ + return d->fontunderline > 0; +} + +/*! + If \a underline is TRUE, sets underline for the style; otherwise + sets no underline. + + \sa fontUnderline(), definesFontUnderline() +*/ +void QStyleSheetItem::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 QStyleSheetItem::definesFontUnderline() const +{ + return d->fontunderline != Undefined; +} + + +/*! + Returns TRUE if the style sets a strike out font; otherwise + returns FALSE. + + \sa setFontStrikeOut(), definesFontStrikeOut() +*/ +bool QStyleSheetItem::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 QStyleSheetItem::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 QStyleSheetItem::definesFontStrikeOut() const +{ + return d->fontstrikeout != Undefined; +} + + +/*! + Returns the font weight setting of the style. This is either a + valid \c QFont::Weight or the value \c QStyleSheetItem::Undefined. + + \sa setFontWeight(), QFont +*/ +int QStyleSheetItem::fontWeight() const +{ + return d->fontweight; +} + +/*! + Sets the font weight setting of the style to \a w. Valid values + are those defined by \c QFont::Weight. + + \sa QFont, fontWeight() +*/ +void QStyleSheetItem::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 QStyleSheetItem::Undefined. + + \sa setLogicalFontSize(), setLogicalFontSizeStep(), QFont::pointSize(), QFont::setPointSize() +*/ +int QStyleSheetItem::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(), QFont::pointSize(), QFont::setPointSize() +*/ +void QStyleSheetItem::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 QStyleSheetItem::logicalFontSizeStep() const +{ + return d->fontsizestep; +} + +/*! + Sets the logical font size step of this style to \a s. + + \sa logicalFontSizeStep() +*/ +void QStyleSheetItem::setLogicalFontSizeStep( int s ) +{ + d->fontsizestep = s; +} + + + +/*! + Sets the font size setting of the style to \a s points. + + \sa fontSize(), QFont::pointSize(), QFont::setPointSize() +*/ +void QStyleSheetItem::setFontSize(int s) +{ + d->fontsize = s; +} + +/*! + Returns the font size setting of the style. This is either a valid + point size or \c QStyleSheetItem::Undefined. + + \sa setFontSize(), QFont::pointSize(), QFont::setPointSize() +*/ +int QStyleSheetItem::fontSize() const +{ + return d->fontsize; +} + + +/*! + Returns the font family setting of the style. This is either a + valid font family or QString::null if no family has been set. + + \sa setFontFamily(), QFont::family(), QFont::setFamily() +*/ +QString QStyleSheetItem::fontFamily() const +{ + return d->fontfamily; +} + +/*! + Sets the font family setting of the style to \a fam. + + \sa fontFamily(), QFont::family(), QFont::setFamily() +*/ +void QStyleSheetItem::setFontFamily( const QString& fam) +{ + d->fontfamily = fam; +} + + +/*!\obsolete + Returns the number of columns for this style. + + \sa setNumberOfColumns(), displayMode(), setDisplayMode() + + */ +int QStyleSheetItem::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 QStyleSheetItem::DisplayMode). + + \sa numberOfColumns() + */ +void QStyleSheetItem::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() QColor::isValid() +*/ +QColor QStyleSheetItem::color() const +{ + return d->col; +} + +/*! + Sets the text color of this style to \a c. + + \sa color() +*/ +void QStyleSheetItem::setColor( const QColor &c) +{ + d->col = c; +} + +/*! + Returns whether this style is an anchor. + + \sa setAnchor() +*/ +bool QStyleSheetItem::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 QStyleSheetItem::setAnchor(bool anc) +{ + d->anchor = anc; +} + + +/*! + Returns the whitespace mode. + + \sa setWhiteSpaceMode() WhiteSpaceMode +*/ +QStyleSheetItem::WhiteSpaceMode QStyleSheetItem::whiteSpaceMode() const +{ + return d->whitespacemode; +} + +/*! + Sets the whitespace mode to \a m. + + \sa WhiteSpaceMode +*/ +void QStyleSheetItem::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 QStyleSheetItem::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 QStyleSheetItem::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 + */ +QStyleSheetItem::ListStyle QStyleSheetItem::listStyle() const +{ + return d->list; +} + +/*! + \enum QStyleSheetItem::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 QStyleSheetItem::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() +*/ +QString QStyleSheetItem::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 QStyleSheetItem::setContexts( const QString& c) +{ + d->contxt = QChar(' ') + c + QChar(' '); +} + +/*! + Returns TRUE if this style can be nested into an element of style + \a s; otherwise returns FALSE. + + \sa contexts(), setContexts() +*/ +bool QStyleSheetItem::allowedInContext( const QStyleSheetItem* s) const +{ + if ( d->contxt.isEmpty() ) + return TRUE; + return d->contxt.find( QChar(' ')+s->name()+QChar(' ')) != -1; +} + + +/*! + Returns TRUE if this style has self-nesting enabled; otherwise + returns FALSE. + + \sa setSelfNesting() +*/ +bool QStyleSheetItem::selfNesting() const +{ + return d->selfnest; +} + +/*! + Sets the self-nesting property for this style to \a nesting. + + In order to support "dirty" HTML, paragraphs \c{<p>} and list + items \c{<li>} are not self-nesting. This means that starting a + new paragraph or list item automatically closes the previous one. + + \sa selfNesting() +*/ +void QStyleSheetItem::setSelfNesting( bool nesting ) +{ + d->selfnest = nesting; +} + +/*! + \internal + Sets the linespacing to be at least \a ls pixels. + + For compatibility with previous Qt 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 QStyleSheetItem::setLineSpacing( int ls ) +{ + d->lineSpacing = ls; +} + +/*! + \obsolete + + Returns the linespacing +*/ + +int QStyleSheetItem::lineSpacing() const +{ + return d->lineSpacing; +} + +//************************************************************************ + + + + +//************************************************************************ + + +/*! + \class QStyleSheet qstylesheet.h + \ingroup text + \brief The QStyleSheet class is a collection of styles for rich text + rendering and a generator of tags. + + \ingroup graphics + \ingroup helpsystem + + By creating QStyleSheetItem 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 QTextEdit or a QTextBrowser. However, QLabel, + QWhatsThis and QMessageBox also support it, and other classes are + likely to follow. With QSimpleRichText it is possible to use the + rich text renderer for custom widgets as well. + + The default QStyleSheet 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{<qt>}...\c{</qt>} + \i A Qt rich text document. It understands the following + attributes: + \list + \i \c title -- The caption of the document. This attribute is + easily accessible with QTextEdit::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, QTextBrowser will then keep + the current page and display the new document in a small + popup similar to QWhatsThis. Note that links will not work + in documents with \c{<qt type="detail">...</qt>}. + \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 QMimeSourceFactory(). + \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{<h1>...</h1>} + \i A top-level heading. + \row \i \c{<h2>...</h2>} + \i A sublevel heading. + \row \i \c{<h3>...</h3>} + \i A sub-sublevel heading. + \row \i \c{<h4>...</h4>} \c{<h5>...</h5>} + \i Headings of lesser importance. + \row \i \c{<p>...</p>} + \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{<center>...}<br>\c{</center>} + \i A centered paragraph. + \row \i \c{<blockquote>...}<br>\c{</blockquote>} + \i An indented paragraph that is useful for quotes. + \row \i \c{<ul>...</ul>} + \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{<ol>...</ol>} + \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{<li>...</li>} + \i A list item. This tag can be used only within the context + of \c{<ol>} or \c{<ul>}. + \row \i \c{<dl>...</dl>} + \i A list of definitions, consisting of terms and descriptions. + \row \i \c{<dt>...</dt>} + \i A term in a list of definitions. This tag can be used only + in the context of \c{<dl>...</dl>}. + \row \i \c{<dd>...</dd>} + \i A description in a list of definitions. This tag can be + used only in the context of \c{<dl>...</dl>}. + \row \i \c{<pre>...</pre>} + \i For larger chunks of code. Whitespaces in the contents are + preserved. For small bits of code use the inline-style \c + code. + \row \i \c{<div>...</div>} and \c{<span>...</span>} + \i Block grouping elements. These are used to structure the + document, and are often used to provide hints about the + intended presentation of the document. + \endtable + + Anchors and links are done with a single tag: + \table + \header \i Anchor tags \i Notes + \row \i \c{<a>...</a>} + \i An anchor or link. + \list + \i A link is created by using an \c href + attribute, for example + <br>\c{<a href="target.qml">Link Text</a>}. Links to + targets within a document are achieved in the same way + as for HTML, e.g. + <br>\c{<a href="target.qml#subtitle">Link Text</a>}. + \i A target is created by using a \c name + attribute, for example + <br>\c{<a name="subtitle"><h2>Sub Title</h2></a>}. + \endlist + \endtable + + The default character style bindings are + \table + \header \i Style tags \i Notes + \row \i \c{<em>...</em>} + \i Emphasized. By default this is the same as \c{<i>...</i>} + (italic). + \row \i \c{<strong>...</strong>} + \i Strong. By default this is the same as \c{<b>...</b>} + (bold). + \row \i \c{<i>...</i>} + \i Italic font style. + \row \i \c{<b>...</b>} + \i Bold font style. + \row \i \c{<u>...</u>} + \i Underlined font style. + \row \i \c{<s>...</s>} + \i Strike out font style. + \row \i \c{<big>...</big>} + \i A larger font size. + \row \i \c{<small>...</small>} + \i A smaller font size. + \row \i \c{<sub>...</sub>} + \i Subscripted text + \row \i \c{<sup>...</sup>} + \i Superscripted text + \row \i \c{<code>...</code>} + \i Indicates code. By default this is the same as + \c{<tt>...</tt>} (typewriter). For larger chunks of code + use the block-tag \c{<}\c{pre>}. + \row \i \c{<tt>...</tt>} + \i Typewriter font style. + \row \i \c{<font>...</font>} + \i Customizes the font size, family and text color. The tag + understands the following attributes: + \list + \i \c color -- The text color, for example \c color="red" or + \c color="#FF0000". + \i \c size -- The logical size of the font. Logical sizes 1 + to 7 are supported. The value may either be absolute + (for example, \c size=3) or relative (\c size=-2). In + the latter case the sizes are simply added. + \i \c face -- The family of the font, for example \c face=times. + \endlist + \endtable + + Special elements are: + \table + \header \i Special tags \i Notes + \row \i \c{<img>} + \i An image. The image name for the mime source factory is + given in the source attribute, for example + \c{<img src="qt.xpm">} The image tag also understands the + attributes \c width and \c height that determine the size + of the image. If the pixmap does not fit the specified + size it will be scaled automatically (by using + QImage::smoothScale()). + <br> + The \c align attribute determines where the image is + placed. By default, an image is placed inline just like a + normal character. Specify \c left or \c right to place the + image at the respective side. + \row \i \c{<hr>} + \i A horizontal line. + \row \i \c{<br>} + \i A line break. + \row \i \c{<nobr>...</nobr>} + \i No break. Prevents word wrap. + \endtable + + In addition, rich text supports simple HTML tables. A table + consists of one or more rows each of which contains one or more + cells. Cells are either data cells or header cells, depending on + their content. Cells which span rows and columns are supported. + + \table + \header \i Table tags \i Notes + \row \i \c{<table>...</table>} + \i A table. Tables support the following attributes: + \list + \i \c bgcolor -- The background color. + \i \c width -- The table width. This is either an absolute + pixel width or a relative percentage of the table's + width, for example \c width=80%. + \i \c border -- The width of the table border. The default is + 0 (= no border). + \i \c cellspacing -- Additional space around the table cells. + The default is 2. + \i \c cellpadding -- Additional space around the contents of + table cells. The default is 1. + \endlist + \row \i \c{<tr>...</tr>} + \i A table row. This is only valid within a \c table. Rows + support the following attribute: + \list + \i \c bgcolor -- The background color. + \endlist + \row \i \c{<th>...</th>} + \i A table header cell. Similar to \c td, but defaults to + center alignment and a bold font. + \row \i \c{<td>...</td>} + \i A table data cell. This is only valid within a \c tr. + Cells support the following attributes: + \list + \i \c bgcolor -- The background color. + \i \c width -- The cell width. This is either an absolute + pixel width or a relative percentage of table's width, + for example \c width=50%. + \i \c colspan -- Specifies how many columns this cell spans. + The default is 1. + \i \c rowspan -- Specifies how many rows this cell spans. The + default is 1. + \i \c align -- Alignment; possible values are \c left, \c + right, and \c center. The default is \c left. + \i \c valign -- Vertical alignment; possible values are \c + top, \c middle, and \c bottom. The default is \c middle. + \endlist + \endtable +*/ + +/*! + Creates a style sheet called \a name, with parent \a parent. Like + any QObject it will be deleted when its parent is destroyed (if + the child still exists). + + By default the style sheet has the tag definitions defined above. +*/ +QStyleSheet::QStyleSheet( QObject *parent, const char *name ) + : QObject( parent, name ) +{ + init(); +} + +/*! + Destroys the style sheet. All styles inserted into the style sheet + will be deleted. +*/ +QStyleSheet::~QStyleSheet() +{ +} + +/*! + \internal + Initialized the style sheet to the basic Qt style. +*/ +void QStyleSheet::init() +{ + styles.setAutoDelete( TRUE ); + + nullstyle = new QStyleSheetItem( this, + QString::fromLatin1("") ); + + QStyleSheetItem* style; + + style = new QStyleSheetItem( this, "qml" ); // compatibility + style->setDisplayMode( QStyleSheetItem::DisplayBlock ); + + style = new QStyleSheetItem( this, QString::fromLatin1("qt") ); + style->setDisplayMode( QStyleSheetItem::DisplayBlock ); + + style = new QStyleSheetItem( this, QString::fromLatin1("a") ); + style->setAnchor( TRUE ); + + style = new QStyleSheetItem( this, QString::fromLatin1("em") ); + style->setFontItalic( TRUE ); + + style = new QStyleSheetItem( this, QString::fromLatin1("i") ); + style->setFontItalic( TRUE ); + + style = new QStyleSheetItem( this, QString::fromLatin1("big") ); + style->setLogicalFontSizeStep( 1 ); + style = new QStyleSheetItem( this, QString::fromLatin1("large") ); // compatibility + style->setLogicalFontSizeStep( 1 ); + + style = new QStyleSheetItem( this, QString::fromLatin1("small") ); + style->setLogicalFontSizeStep( -1 ); + + style = new QStyleSheetItem( this, QString::fromLatin1("strong") ); + style->setFontWeight( QFont::Bold); + + style = new QStyleSheetItem( this, QString::fromLatin1("b") ); + style->setFontWeight( QFont::Bold); + + style = new QStyleSheetItem( this, QString::fromLatin1("h1") ); + style->setFontWeight( QFont::Bold); + style->setLogicalFontSize(6); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style-> setMargin(QStyleSheetItem::MarginTop, 18); + style-> setMargin(QStyleSheetItem::MarginBottom, 12); + + style = new QStyleSheetItem( this, QString::fromLatin1("h2") ); + style->setFontWeight( QFont::Bold); + style->setLogicalFontSize(5); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style-> setMargin(QStyleSheetItem::MarginTop, 16); + style-> setMargin(QStyleSheetItem::MarginBottom, 12); + + style = new QStyleSheetItem( this, QString::fromLatin1("h3") ); + style->setFontWeight( QFont::Bold); + style->setLogicalFontSize(4); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style-> setMargin(QStyleSheetItem::MarginTop, 14); + style-> setMargin(QStyleSheetItem::MarginBottom, 12); + + style = new QStyleSheetItem( this, QString::fromLatin1("h4") ); + style->setFontWeight( QFont::Bold); + style->setLogicalFontSize(3); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + + style = new QStyleSheetItem( this, QString::fromLatin1("h5") ); + style->setFontWeight( QFont::Bold); + style->setLogicalFontSize(2); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style-> setMargin(QStyleSheetItem::MarginTop, 12); + style-> setMargin(QStyleSheetItem::MarginBottom, 4); + + style = new QStyleSheetItem( this, QString::fromLatin1("p") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + style->setSelfNesting( FALSE ); + + style = new QStyleSheetItem( this, QString::fromLatin1("center") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setAlignment( AlignCenter ); + + style = new QStyleSheetItem( this, QString::fromLatin1("twocolumn") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setNumberOfColumns( 2 ); + + style = new QStyleSheetItem( this, QString::fromLatin1("multicol") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + (void) new QStyleSheetItem( this, QString::fromLatin1("font") ); + + style = new QStyleSheetItem( this, QString::fromLatin1("ul") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setListStyle( QStyleSheetItem::ListDisc ); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + style->setMargin( QStyleSheetItem::MarginLeft, 40 ); + + style = new QStyleSheetItem( this, QString::fromLatin1("ol") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setListStyle( QStyleSheetItem::ListDecimal ); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + style->setMargin( QStyleSheetItem::MarginLeft, 40 ); + + style = new QStyleSheetItem( this, QString::fromLatin1("li") ); + style->setDisplayMode(QStyleSheetItem::DisplayListItem); + style->setSelfNesting( FALSE ); + + style = new QStyleSheetItem( this, QString::fromLatin1("code") ); + style->setFontFamily( QString::fromLatin1("Courier New,courier") ); + + style = new QStyleSheetItem( this, QString::fromLatin1("tt") ); + style->setFontFamily( QString::fromLatin1("Courier New,courier") ); + + new QStyleSheetItem(this, QString::fromLatin1("img")); + new QStyleSheetItem(this, QString::fromLatin1("br")); + new QStyleSheetItem(this, QString::fromLatin1("hr")); + + style = new QStyleSheetItem(this, QString::fromLatin1("sub")); + style->setVerticalAlignment( QStyleSheetItem::VAlignSub ); + style = new QStyleSheetItem(this, QString::fromLatin1("sup")); + style->setVerticalAlignment( QStyleSheetItem::VAlignSuper ); + + style = new QStyleSheetItem( this, QString::fromLatin1("pre") ); + style->setFontFamily( QString::fromLatin1("Courier New,courier") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setWhiteSpaceMode(QStyleSheetItem::WhiteSpacePre); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + + style = new QStyleSheetItem( this, QString::fromLatin1("blockquote") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setMargin(QStyleSheetItem::MarginHorizontal, 40 ); + + style = new QStyleSheetItem( this, QString::fromLatin1("head") ); + style->setDisplayMode(QStyleSheetItem::DisplayNone); + style = new QStyleSheetItem( this, QString::fromLatin1("body") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style = new QStyleSheetItem( this, QString::fromLatin1("div") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock) ; + style = new QStyleSheetItem( this, QString::fromLatin1("span") ); + style = new QStyleSheetItem( this, QString::fromLatin1("dl") ); + style-> setMargin(QStyleSheetItem::MarginVertical, 8); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style = new QStyleSheetItem( this, QString::fromLatin1("dt") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setContexts(QString::fromLatin1("dl") ); + style = new QStyleSheetItem( this, QString::fromLatin1("dd") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); + style->setMargin(QStyleSheetItem::MarginLeft, 30); + style->setContexts(QString::fromLatin1("dt dl") ); + style = new QStyleSheetItem( this, QString::fromLatin1("u") ); + style->setFontUnderline( TRUE); + style = new QStyleSheetItem( this, QString::fromLatin1("s") ); + style->setFontStrikeOut( TRUE); + style = new QStyleSheetItem( this, QString::fromLatin1("nobr") ); + style->setWhiteSpaceMode( QStyleSheetItem::WhiteSpaceNoWrap ); + + // compatibily with some minor 3.0.x Qt versions that had an + // undocumented <wsp> tag. ### Remove 3.1 + style = new QStyleSheetItem( this, QString::fromLatin1("wsp") ); + style->setWhiteSpaceMode( QStyleSheetItem::WhiteSpacePre ); + + // tables + style = new QStyleSheetItem( this, QString::fromLatin1("table") ); + style = new QStyleSheetItem( this, QString::fromLatin1("tr") ); + style->setContexts(QString::fromLatin1("table")); + style = new QStyleSheetItem( this, QString::fromLatin1("td") ); + style->setContexts(QString::fromLatin1("tr")); + style = new QStyleSheetItem( this, QString::fromLatin1("th") ); + style->setFontWeight( QFont::Bold ); + style->setAlignment( Qt::AlignCenter ); + style->setContexts(QString::fromLatin1("tr")); + + style = new QStyleSheetItem( this, QString::fromLatin1("html") ); +} + + + +static QStyleSheet* defaultsheet = 0; +static QSingleCleanupHandler<QStyleSheet> qt_cleanup_stylesheet; + +/*! + Returns the application-wide default style sheet. This style sheet + is used by rich text rendering classes such as QSimpleRichText, + QWhatsThis and QMessageBox to define the rendering style and + available tags within rich text documents. It also serves as the + initial style sheet for the more complex render widgets, QTextEdit + and QTextBrowser. + + \sa setDefaultSheet() +*/ +QStyleSheet* QStyleSheet::defaultSheet() +{ + if (!defaultsheet) { + defaultsheet = new QStyleSheet(); + qt_cleanup_stylesheet.set( &defaultsheet ); + } + return defaultsheet; +} + +/*! + Sets the application-wide default style sheet to \a sheet, + deleting any style sheet previously set. The ownership is + transferred to QStyleSheet. + + \sa defaultSheet() +*/ +void QStyleSheet::setDefaultSheet( QStyleSheet* sheet) +{ + if ( defaultsheet != sheet ) { + if ( defaultsheet ) + qt_cleanup_stylesheet.reset(); + delete defaultsheet; + } + defaultsheet = sheet; + if ( defaultsheet ) + qt_cleanup_stylesheet.set( &defaultsheet ); +} + +/*!\internal + Inserts \a style. Any tags generated after this time will be + bound to this style. Note that \a style becomes owned by the + style sheet and will be deleted when the style sheet is destroyed. +*/ +void QStyleSheet::insert( QStyleSheetItem* style ) +{ + styles.insert(style->name(), style); +} + + +/*! + Returns the style called \a name or 0 if there is no such style. +*/ +QStyleSheetItem* QStyleSheet::item( const QString& name) +{ + if ( name.isNull() ) + return 0; + return styles[name]; +} + +/*! + \overload + + Returns the style called \a name or 0 if there is no such style + (const version) +*/ +const QStyleSheetItem* QStyleSheet::item( const QString& name) const +{ + if ( name.isNull() ) + return 0; + return styles[name]; +} + + +/*! + \preliminary + + Generates an internal object for the tag called \a name, given the + attributes \a attr, and using additional information provided by + the mime source factory \a factory. + + \a context is the optional context of the document, i.e. the path + to look for relative links. This becomes important if the text + contains relative references, for example within image tags. + QSimpleRichText always uses the default mime source factory (see + \l{QMimeSourceFactory::defaultFactory()}) to resolve these + references. The context will then be used to calculate the + absolute path. See QMimeSourceFactory::makeAbsolute() for details. + + \a emptyTag and \a doc are for internal use only. + + This function should not be used in application code. +*/ +#ifndef QT_NO_TEXTCUSTOMITEM +QTextCustomItem* QStyleSheet::tag( const QString& name, + const QMap<QString, QString> &attr, + const QString& context, + const QMimeSourceFactory& factory, + bool /*emptyTag */, QTextDocument *doc ) const +{ + const QStyleSheetItem* style = item( name ); + // first some known tags + if ( !style ) + return 0; + if ( style->name() == "img" ) + return new QTextImage( doc, attr, context, (QMimeSourceFactory&)factory ); + if ( style->name() == "hr" ) + return new QTextHorizontalLine( doc, attr, context, (QMimeSourceFactory&)factory ); + return 0; +} +#endif + + +/*! Auxiliary function. Converts the plain text string \a plain to a + rich text formatted paragraph while preserving most of its look. + + \a mode defines the whitespace mode. Possible values are \c + QStyleSheetItem::WhiteSpacePre (no wrapping, all whitespaces + preserved) and \c QStyleSheetItem::WhiteSpaceNormal (wrapping, + simplified whitespaces). + + \sa escape() +*/ +QString QStyleSheet::convertFromPlainText( const QString& plain, QStyleSheetItem::WhiteSpaceMode mode ) +{ + int col = 0; + QString rich; + rich += "<p>"; + for ( int i = 0; i < int(plain.length()); ++i ) { + if ( plain[i] == '\n' ){ + int c = 1; + while ( i+1 < int(plain.length()) && plain[i+1] == '\n' ) { + i++; + c++; + } + if ( c == 1) + rich += "<br>\n"; + else { + rich += "</p>\n"; + while ( --c > 1 ) + rich += "<br>\n"; + rich += "<p>"; + } + col = 0; + } else { + if ( mode == QStyleSheetItem::WhiteSpacePre && plain[i] == '\t' ){ + rich += 0x00a0U; + ++col; + while ( col % 8 ) { + rich += 0x00a0U; + ++col; + } + } + else if ( mode == QStyleSheetItem::WhiteSpacePre && plain[i].isSpace() ) + rich += 0x00a0U; + else if ( plain[i] == '<' ) + rich +="<"; + else if ( plain[i] == '>' ) + rich +=">"; + else if ( plain[i] == '&' ) + rich +="&"; + else + rich += plain[i]; + ++col; + } + } + if ( col != 0 ) + rich += "</p>"; + return rich; +} + +/*! + Auxiliary function. Converts the plain text string \a plain to a + rich text formatted string with any HTML meta-characters escaped. + + \sa convertFromPlainText() +*/ +QString QStyleSheet::escape( const QString& plain) +{ + QString rich; + for ( int i = 0; i < int(plain.length()); ++i ) { + if ( plain[i] == '<' ) + rich +="<"; + else if ( plain[i] == '>' ) + rich +=">"; + else if ( plain[i] == '&' ) + rich +="&"; + else + rich += plain[i]; + } + return rich; +} + +// Must doc this enum somewhere, and it is logically related to QStyleSheet + +/*! + \enum Qt::TextFormat + + This enum is used in widgets that can display both plain text and + rich text, e.g. QLabel. It is used for deciding whether a text + string should be interpreted as one or the other. This is normally + done by passing one of the enum values to a setTextFormat() + function. + + \value PlainText The text string is interpreted as a plain text + string. + + \value RichText The text string is interpreted as a rich text + string using the current QStyleSheet::defaultSheet(). + + \value AutoText The text string is interpreted as for \c RichText + if QStyleSheet::mightBeRichText() returns TRUE, otherwise as + \c PlainText. + + \value LogText A special, limited text format which is only used + by QTextEdit in an optimized mode. +*/ + +/*! + Returns TRUE if the string \a text is likely to be rich text; + otherwise returns FALSE. + + This function uses a fast and therefore simple heuristic. It + mainly checks whether there is something that looks like a tag + before the first line break. Although the result may be correct + for common cases, there is no guarantee. +*/ +bool QStyleSheet::mightBeRichText( const QString& text) +{ + if ( text.isEmpty() ) + return FALSE; + int start = 0; + + while ( start < int(text.length()) && text[start].isSpace() ) + ++start; + if ( text.mid( start, 5 ).lower() == "<!doc" ) + return TRUE; + int open = start; + while ( open < int(text.length()) && text[open] != '<' + && text[open] != '\n' ) { + if ( text[open] == '&' && text.mid(open+1,3) == "lt;" ) + return TRUE; // support desperate attempt of user to see <...> + ++open; + } + if ( open < (int)text.length() && text[open] == '<' ) { + int close = text.find('>', open); + if ( close > -1 ) { + QString tag; + for (int i = open+1; i < close; ++i) { + if ( text[i].isDigit() || text[i].isLetter() ) + tag += text[i]; + else if ( !tag.isEmpty() && text[i].isSpace() ) + break; + else if ( !text[i].isSpace() && (!tag.isEmpty() || text[i] != '!' ) ) + return FALSE; // that's not a tag + } + return defaultSheet()->item( tag.lower() ) != 0; + } + } + return FALSE; +} + + +/*! + \fn void QStyleSheet::error( const QString& msg) const + + This virtual function is called when an error occurs when + processing rich text. Reimplement it if you need to catch error + messages. + + Errors might occur if some rich text strings contain tags that are + not understood by the stylesheet, if some tags are nested + incorrectly, or if tags are not closed properly. + + \a msg is the error message. +*/ +void QStyleSheet::error( const QString& ) const +{ +} + + +/*! + Scales the font \a font to the appropriate physical point size + corresponding to the logical font size \a logicalSize. + + When calling this function, \a font has a point size corresponding + to the logical font size 3. + + Logical font sizes range from 1 to 7, with 1 being the smallest. + + \sa QStyleSheetItem::logicalFontSize(), QStyleSheetItem::logicalFontSizeStep(), QFont::setPointSize() + */ +void QStyleSheet::scaleFont( QFont& font, int logicalSize ) const +{ + if ( logicalSize < 1 ) + logicalSize = 1; + if ( logicalSize > 7 ) + logicalSize = 7; + int baseSize = font.pointSize(); + bool pixel = FALSE; + if ( baseSize == -1 ) { + baseSize = font.pixelSize(); + pixel = TRUE; + } + int s; + switch ( logicalSize ) { + case 1: + s = 7*baseSize/10; + break; + case 2: + s = (8 * baseSize) / 10; + break; + case 4: + s = (12 * baseSize) / 10; + break; + case 5: + s = (15 * baseSize) / 10; + break; + case 6: + s = 2 * baseSize; + break; + case 7: + s = (24 * baseSize) / 10; + break; + default: + s = baseSize; + } + if ( pixel ) + font.setPixelSize( s ); + else + font.setPointSize( s ); +} + +#endif // QT_NO_RICHTEXT diff --git a/src/kernel/qstylesheet.h b/src/kernel/qstylesheet.h new file mode 100644 index 0000000..fb85bcd --- /dev/null +++ b/src/kernel/qstylesheet.h @@ -0,0 +1,253 @@ +/**************************************************************************** +** +** Definition of the QStyleSheet class +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QSTYLESHEET_H +#define QSTYLESHEET_H + +#ifndef QT_H +#include "qstring.h" +#include "qvaluelist.h" +#include "qptrvector.h" +#include "qdict.h" +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_RICHTEXT + +class QStyleSheet; +class QTextDocument; +template<class Key, class T> class QMap; +class QStyleSheetItemData; + +class Q_EXPORT QStyleSheetItem : public Qt +{ +public: + QStyleSheetItem( QStyleSheet* parent, const QString& name ); + QStyleSheetItem( const QStyleSheetItem & ); + ~QStyleSheetItem(); + + QStyleSheetItem& operator=( const QStyleSheetItem& other ); + + QString name() const; + + QStyleSheet* styleSheet(); + const QStyleSheet* styleSheet() const; + + enum AdditionalStyleValues { Undefined = - 1}; + + enum DisplayMode { + DisplayBlock, + DisplayInline, + DisplayListItem, + DisplayNone +#ifndef Q_QDOC + , DisplayModeUndefined = -1 +#endif + }; + + DisplayMode displayMode() const; + void setDisplayMode(DisplayMode m); + + int alignment() const; + void setAlignment( int f); + + enum VerticalAlignment { + VAlignBaseline, + VAlignSub, + VAlignSuper + }; + + VerticalAlignment verticalAlignment() const; + void setVerticalAlignment( VerticalAlignment valign ); + + int fontWeight() const; + void setFontWeight(int w); + + int logicalFontSize() const; + void setLogicalFontSize(int s); + + int logicalFontSizeStep() const; + void setLogicalFontSizeStep( int s ); + + int fontSize() const; + void setFontSize(int s); + + QString fontFamily() const; + void setFontFamily( const QString& ); + + int numberOfColumns() const; + void setNumberOfColumns(int ncols); + + QColor color() const; + void setColor( const QColor &); + + bool fontItalic() const; + void setFontItalic( bool ); + bool definesFontItalic() const; + + bool fontUnderline() const; + void setFontUnderline( bool ); + bool definesFontUnderline() const; + + bool fontStrikeOut() const; + void setFontStrikeOut( bool ); + bool definesFontStrikeOut() const; + + bool isAnchor() const; + void setAnchor(bool anc); + + enum WhiteSpaceMode { + WhiteSpaceNormal, + WhiteSpacePre, + WhiteSpaceNoWrap +#ifndef Q_QDOC + , WhiteSpaceModeUndefined = -1 +#endif + }; + WhiteSpaceMode whiteSpaceMode() const; + void setWhiteSpaceMode(WhiteSpaceMode m); + + enum Margin { + MarginLeft, + MarginRight, + MarginTop, + MarginBottom, + MarginFirstLine, + MarginAll, + MarginVertical, + MarginHorizontal +#ifndef Q_QDOC + , MarginUndefined = -1 +#endif + }; + + int margin( Margin m) const; + void setMargin( Margin, int); + + enum ListStyle { + ListDisc, + ListCircle, + ListSquare, + ListDecimal, + ListLowerAlpha, + ListUpperAlpha +#ifndef Q_QDOC + , ListStyleUndefined = -1 +#endif + }; + + ListStyle listStyle() const; + void setListStyle( ListStyle ); + + QString contexts() const; + void setContexts( const QString& ); + bool allowedInContext( const QStyleSheetItem* ) const; + + bool selfNesting() const; + void setSelfNesting( bool ); + + void setLineSpacing( int ls ); + int lineSpacing() const; + +private: + void init(); + QStyleSheetItemData* d; +}; + + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT QDict<QStyleSheetItem>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueList< QPtrVector<QStyleSheetItem> >; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrVector<QStyleSheetItem>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueList<QStyleSheetItem::ListStyle>; +// MOC_SKIP_END +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class QTextCustomItem; +#endif + +class Q_EXPORT QStyleSheet : public QObject +{ + Q_OBJECT +public: + QStyleSheet( QObject *parent=0, const char *name=0 ); + virtual ~QStyleSheet(); + + static QStyleSheet* defaultSheet(); + static void setDefaultSheet( QStyleSheet* ); + + + QStyleSheetItem* item( const QString& name); + const QStyleSheetItem* item( const QString& name) const; + + void insert( QStyleSheetItem* item); + +#ifndef QT_NO_TEXTCUSTOMITEM + virtual QTextCustomItem* tag( const QString& name, + const QMap<QString, QString> &attr, + const QString& context, + const QMimeSourceFactory& factory, + bool emptyTag, QTextDocument *doc ) const; +#endif + static QString escape( const QString& ); + static QString convertFromPlainText( const QString&, + QStyleSheetItem::WhiteSpaceMode mode = QStyleSheetItem::WhiteSpacePre ); + static bool mightBeRichText( const QString& ); + + virtual void scaleFont( QFont& font, int logicalSize ) const; + + virtual void error( const QString& ) const; + +private: + void init(); + QDict<QStyleSheetItem> styles; + QStyleSheetItem* nullstyle; +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QStyleSheet( const QStyleSheet & ); + QStyleSheet &operator=( const QStyleSheet & ); +#endif +}; + +#endif // QT_NO_RICHTEXT + +#endif // QSTYLESHEET_H diff --git a/src/kernel/qt.h b/src/kernel/qt.h new file mode 100644 index 0000000..5a77207 --- /dev/null +++ b/src/kernel/qt.h @@ -0,0 +1,371 @@ +/**************************************************************************** +** +** Qt GUI Toolkit +** +** This header file efficiently includes all Qt GUI Toolkit functionality. +** +** Generated : Mon Oct 13 13:07:29 CEST 2003 + +** +** Copyright (C) 1995-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the Qt GUI Toolkit. +** +*****************************************************************************/ + +#ifndef QT_H +#define QT_H +#include "qglobal.h" +#include "qfeatures.h" +#include "qshared.h" +#include "qptrcollection.h" +#include "qglist.h" +#include "qobjectdefs.h" +#include "qnamespace.h" +#include "qgarray.h" +#include "qmemarray.h" +#include "qcstring.h" +#include "qstring.h" +#include "qptrlist.h" +#include "qiodevice.h" +#include "qkeysequence.h" +#include "qwindowdefs.h" +#include "qgdict.h" +#include "qfont.h" +#include "qdatastream.h" +#include "qpair.h" +#include "qpoint.h" +#include <stdio.h> +#include "qtextstream.h" +#include "qfontinfo.h" +#include "qsizepolicy.h" +#include "qtl.h" +#include "qsize.h" +#include "qrect.h" +#include "qbitarray.h" +#include "qregion.h" +#include "qsql.h" +#include "qstrlist.h" +#include "qvaluelist.h" +#include "qmap.h" +#include "qdatetime.h" +#include "qmime.h" +#include "qasciidict.h" +#include "qpaintdevice.h" +#include "qfontmetrics.h" +#include "qdict.h" +#include "qevent.h" +#include "qhostaddress.h" +#include "qstringlist.h" +#include "qcolor.h" +#include <qdom.h> +#include "qobject.h" +#include <qdrawutil.h> +#include "qbrush.h" +#include "qpalette.h" +#include "qwidget.h" +#include "qjpunicode.h" +#include "qtextcodec.h" +#include "qstyle.h" +#include "qframe.h" +#include "qfile.h" +#include "qfileinfo.h" +#include "qurlinfo.h" +#include "qwidgetlist.h" +#include <qcombobox.h> +#include "qgroupbox.h" +#include "qdialog.h" +#include <qdataview.h> +#include "qdockwindow.h" +#include "qcommonstyle.h" +#include "qnetworkprotocol.h" +#include <qeuckrcodec.h> +#include <qgb18030codec.h> +#include "qgcache.h" +#include "qpixmap.h" +#include <qgif.h> +#include <qglcolormap.h> +#include <qcache.h> +#include <qdropsite.h> +#include "qgplugin.h" +#include <qgrid.h> +#include "qrangecontrol.h" +#include "qbuttongroup.h" +#include <qdatetimeedit.h> +#include "qgvector.h" +#include "qhbox.h" +#include <qhbuttongroup.h> +#include "qiconset.h" +#include <qhgroupbox.h> +#include "qsocketnotifier.h" +#include <qeventloop.h> +#include <qhttp.h> +#include <qaction.h> +#include "qbuffer.h" +#include "qimage.h" +#include <qimageformatplugin.h> +#include "qlineedit.h" +#include <qintcache.h> +#include "qintdict.h" +#include "qmotifstyle.h" +#include "qpicture.h" +#include <qjiscodec.h> +#include <qeucjpcodec.h> +#include <qkeycode.h> +#include <qaccel.h> +#include "qlabel.h" +#include "qlayout.h" +#include <qlcdnumber.h> +#include <qlibrary.h> +#include <qinputdialog.h> +#include "qscrollbar.h" +#include "qscrollview.h" +#include "qdir.h" +#include "qwindowsstyle.h" +#include "qconnection.h" +#include "qbitmap.h" +#include "qvariant.h" +#include "qsignal.h" +#include <qmessagebox.h> +#include "qmetaobject.h" +#include "qheader.h" +#include <qmotifplusstyle.h> +#include <qcdestyle.h> +#include <qmovie.h> +#include "qptrvector.h" +#include "qmutex.h" +#include "qbutton.h" +#include <qnetwork.h> +#include <qftp.h> +#include "qguardedptr.h" +#include <qobjectcleanuphandler.h> +#include "qsqlfield.h" +#include <qobjectdict.h> +#include <qobjectlist.h> +#include <qcolordialog.h> +#include <qpaintdevicemetrics.h> +#include "qpointarray.h" +#include "qmenudata.h" +#include <qlistview.h> +#include "qpen.h" +#include "qdragobject.h" +#include <qiconview.h> +#include <qpixmapcache.h> +#include <qplatinumstyle.h> +#include <qpngio.h> +#include <qcursor.h> +#include <qerrormessage.h> +#include <qpolygonscanner.h> +#include "qpopupmenu.h" +#include <qprintdialog.h> +#include <qprinter.h> +#include <qprocess.h> +#include "qprogressbar.h" +#include "qsemimodal.h" +#include <qasciicache.h> +#include "qptrdict.h" +#include <qcleanuphandler.h> +#include <qptrqueue.h> +#include <qptrstack.h> +#include "qstylesheet.h" +#include <qpushbutton.h> +#include <qradiobutton.h> +#include <qdial.h> +#include <qdockarea.h> +#include "qregexp.h" +#include <qclipboard.h> +#include <qrtlcodec.h> +#include <qlistbox.h> +#include <qgridview.h> +#include "qsemaphore.h" +#include <qprogressdialog.h> +#include "qsocketdevice.h" +#include <qsessionmanager.h> +#include <qsettings.h> +#include <qsgistyle.h> +#include <qfontdialog.h> +#include "qtimer.h" +#include <qsignalmapper.h> +#include <qsignalslotimp.h> +#include <qsimplerichtext.h> +#include "qwmatrix.h" +#include <qsizegrip.h> +#include <qabstractlayout.h> +#include <qsjiscodec.h> +#include <qslider.h> +#include <qsocket.h> +#include <qserversocket.h> +#include <qdns.h> +#include <qsortedlist.h> +#include <qsound.h> +#include <qspinbox.h> +#include <qsplashscreen.h> +#include <qsplitter.h> +#include "qsqlerror.h" +#include "qeditorfactory.h" +#include "qsqlquery.h" +#include "qsqlrecord.h" +#include <qsqldriverplugin.h> +#include "qsqlindex.h" +#include "qsqlcursor.h" +#include <qsqldriver.h> +#include <qsqlform.h> +#include "qtable.h" +#include <qsqlpropertymap.h> +#include <qsqldatabase.h> +#include <qdatabrowser.h> +#include <qsqlresult.h> +#include <qsqlselectcursor.h> +#include <qstatusbar.h> +#include <qmenubar.h> +#include <qcanvas.h> +#include "qtranslator.h" +#include <qstrvec.h> +#include <qinterlacestyle.h> +#include <qstylefactory.h> +#include <qstyleplugin.h> +#include "qtextedit.h" +#include <qsyntaxhighlighter.h> +#include <qtabbar.h> +#include <qtabdialog.h> +#include "qsqleditorfactory.h" +#include <qtabwidget.h> +#include <qtextbrowser.h> +#include <qbig5codec.h> +#include <qtextcodecfactory.h> +#include <qtextcodecplugin.h> +#include <qmultilineedit.h> +#include "qtoolbar.h" +#include <qtextview.h> +#include "qwaitcondition.h" +#include <qasyncio.h> +#include <qfontdatabase.h> +#include <qmainwindow.h> +#include <qtoolbox.h> +#include <qtoolbutton.h> +#include <qtooltip.h> +#include "qdesktopwidget.h" +#include <qtsciicodec.h> +#include "qurl.h" +#include "qurloperator.h" +#include <qfiledialog.h> +#include <qutfcodec.h> +#include <quuid.h> +#include <qvalidator.h> +#include <qasyncimageio.h> +#include <qvaluestack.h> +#include <qvaluevector.h> +#include <qdatatable.h> +#include <qvbox.h> +#include <qvbuttongroup.h> +#include <qvfbhdr.h> +#include <qvgroupbox.h> +#include <qthread.h> +#include <qwhatsthis.h> +#include <qapplication.h> +#include <qwidgetintdict.h> +#include <qfocusdata.h> +#include <qwidgetplugin.h> +#include <qwidgetstack.h> +#include <qcheckbox.h> +#include <qcompactstyle.h> +#include <qwizard.h> +#include <qpainter.h> +#include <qworkspace.h> +#include <qlocalfs.h> +#include <qxml.h> + +#if defined( QT_MOC_CPP ) || defined( QT_H_CPP ) || defined( Q_OS_MACX ) +#include <private/qcom_p.h> +#include <private/qucom_p.h> +#include "private/qgfxdriverinterface_p.h" +#include "private/qcom_p.h" +#include "private/qimageformatinterface_p.h" +#include "private/qisciicodec_p.h" +#include "private/qkbddriverinterface_p.h" +#include "private/qlayoutengine_p.h" +#include "private/qcomlibrary_p.h" +#include "private/qmousedriverinterface_p.h" +#include "private/qeffects_p.h" +#include "private/qgpluginmanager_p.h" +#include "private/qinternal_p.h" +#include "private/qsqldriverinterface_p.h" +#include "private/qsqlmanager_p.h" +#include "private/qlock_p.h" +#include "private/qcomponentfactory_p.h" +#include "private/qstyleinterface_p.h" +#include "private/qrichtext_p.h" +#include "private/qsvgdevice_p.h" +#include "private/qfontcodecs_p.h" +#include "private/qtextcodecinterface_p.h" +#include "private/qpsprinter_p.h" +#include "private/qtitlebar_p.h" +#include "private/qucom_p.h" +#include "private/qucomextra_p.h" +#include "private/qpluginmanager_p.h" +#include "private/qdir_p.h" +#include "private/qsettings_p.h" +#include "private/qsqlextension_p.h" +#include "private/qdialogbuttons_p.h" +#include "private/qwidgetinterface_p.h" +#include "private/qwidgetresizehandler_p.h" +#include "private/qlibrary_p.h" +#endif // Private headers + + +#ifdef Q_WS_MAC +#include <qaquastyle.h> +#include <qmacstyle_mac.h> +#endif // Q_WS_MAC + +#ifdef Q_WS_WIN +#include <qwindowsxpstyle.h> +#endif + +#ifdef Q_WS_QWS +#include <private/qtextengine_p.h> +#include "qfontmanager_qws.h" +#include <qfontfactorybdf_qws.h> +#include <qgfxvoodoodefs_qws.h> +#include <qgfxmatroxdefs_qws.h> +#include <qgfxdriverplugin_qws.h> +#include <qkbddriverfactory_qws.h> +#include <qkbddriverplugin_qws.h> +#include <qmousedriverfactory_qws.h> +#include <qmousedriverplugin_qws.h> +#include <qcopchannel_qws.h> +#include <qdirectpainter_qws.h> +#include "qmemorymanager_qws.h" +#include <qmouse_qws.h> +#include <qfontfactoryttf_qws.h> +#ifndef Q_OS_MAC +# include <qsoundqss_qws.h> +#endif +#include "qgfx_qws.h" +#include <qgfxdriverfactory_qws.h> +#include "qwsdisplay_qws.h" +#include "qwssocket_qws.h" +#include "qwsdecoration_qws.h" +#include "qwsutils_qws.h" +#include <qwscursor_qws.h> +#include "qwsmanager_qws.h" +#include "qwsdefaultdecoration_qws.h" +#include <qgfxraster_qws.h> +#include "qwscommand_qws.h" +#include <qwshydrodecoration_qws.h> +#include <qwskde2decoration_qws.h> +#include <qwskdedecoration_qws.h> +#include <qwsbeosdecoration_qws.h> +#include "qwsproperty_qws.h" +#include <qwsregionmanager_qws.h> +#include "qwsevent_qws.h" +#include <qwindowsystem_qws.h> +#include <qwswindowsdecoration_qws.h> +#endif // Q_WS_QWS + + +#ifdef Q_WS_WCE +#include <qpocketpcstyle_wce.h> +#endif // Q_WS_WCE + +#endif // QT_H diff --git a/src/kernel/qt_compat.pri b/src/kernel/qt_compat.pri new file mode 100644 index 0000000..2bffb79 --- /dev/null +++ b/src/kernel/qt_compat.pri @@ -0,0 +1,27 @@ +# Qt compatibility + +# scratch pad for internal development +# hack these for your build like +# internal { +# CONFIG += blah +# } + +########################################################## + +# mac hac fu +!embedded:!x11:mac { + #never + CONFIG -= nas x11 x11sm + #CONFIG += sqlcrap + sql:sqlcrap { + sql-drivers += postgres + INCLUDEPATH+=/Users/sam/postgresql-7.0.2/src/include \ + /Users/sam/postgresql-7.0.2/src/interfaces/libpq + LIBS += -L/Users/sam/postgresql-7.0.2/src/interfaces/libpq + } +} + +attic { + SOURCES += attic/qttableview.cpp + HEADERS += attic/qttableview.h +} diff --git a/src/kernel/qt_gfx.pri b/src/kernel/qt_gfx.pri new file mode 100644 index 0000000..e27523c --- /dev/null +++ b/src/kernel/qt_gfx.pri @@ -0,0 +1,155 @@ +# Qt graphics + +#mng support +HEADERS += $$KERNEL_H/qmngio.h +SOURCES += $$KERNEL_CPP/qmngio.cpp +mng { + system-mng { + win32:LIBS += libmng.lib + unix:LIBS += -lmng + } else { + INCLUDEPATH += 3rdparty/libmng + SOURCES += 3rdparty/libmng/libmng_callback_xs.c \ + 3rdparty/libmng/libmng_chunk_io.c \ + 3rdparty/libmng/libmng_chunk_prc.c \ + 3rdparty/libmng/libmng_chunk_xs.c \ + 3rdparty/libmng/libmng_cms.c \ + 3rdparty/libmng/libmng_display.c \ + 3rdparty/libmng/libmng_dither.c \ + 3rdparty/libmng/libmng_error.c \ + 3rdparty/libmng/libmng_filter.c \ + 3rdparty/libmng/libmng_hlapi.c \ + 3rdparty/libmng/libmng_jpeg.c \ + 3rdparty/libmng/libmng_object_prc.c \ + 3rdparty/libmng/libmng_pixels.c \ + 3rdparty/libmng/libmng_prop_xs.c \ + 3rdparty/libmng/libmng_read.c \ + 3rdparty/libmng/libmng_trace.c \ + 3rdparty/libmng/libmng_write.c \ + 3rdparty/libmng/libmng_zlib.c + } + no-jpeg { + message(Use of mng requires support for jpeg) + CONFIG += jpeg + } else:!jpeg { + message(Use of mng requires support for jpeg) + CONFIG += jpeg + } +} +else:DEFINES += QT_NO_IMAGEIO_MNG + +#jpeg support.. +HEADERS += $$KERNEL_H/qjpegio.h +SOURCES += $$KERNEL_CPP/qjpegio.cpp +jpeg { + system-jpeg { + unix:LIBS += -ljpeg + win32:LIBS += libjpeg.lib + } else { + INCLUDEPATH += 3rdparty/libjpeg + SOURCES += 3rdparty/libjpeg/jcapimin.c \ + 3rdparty/libjpeg/jcapistd.c \ + 3rdparty/libjpeg/jccoefct.c \ + 3rdparty/libjpeg/jccolor.c \ + 3rdparty/libjpeg/jcdctmgr.c \ + 3rdparty/libjpeg/jchuff.c \ + 3rdparty/libjpeg/jcinit.c \ + 3rdparty/libjpeg/jcmainct.c \ + 3rdparty/libjpeg/jcmarker.c \ + 3rdparty/libjpeg/jcmaster.c \ + 3rdparty/libjpeg/jcomapi.c \ + 3rdparty/libjpeg/jcparam.c \ + 3rdparty/libjpeg/jcphuff.c \ + 3rdparty/libjpeg/jcprepct.c \ + 3rdparty/libjpeg/jcsample.c \ + 3rdparty/libjpeg/jctrans.c \ + 3rdparty/libjpeg/jdapimin.c \ + 3rdparty/libjpeg/jdapistd.c \ + 3rdparty/libjpeg/jdatadst.c \ + 3rdparty/libjpeg/jdatasrc.c \ + 3rdparty/libjpeg/jdcoefct.c \ + 3rdparty/libjpeg/jdcolor.c \ + 3rdparty/libjpeg/jddctmgr.c \ + 3rdparty/libjpeg/jdhuff.c \ + 3rdparty/libjpeg/jdinput.c \ + 3rdparty/libjpeg/jdmainct.c \ + 3rdparty/libjpeg/jdmarker.c \ + 3rdparty/libjpeg/jdmaster.c \ + 3rdparty/libjpeg/jdmerge.c \ + 3rdparty/libjpeg/jdphuff.c \ + 3rdparty/libjpeg/jdpostct.c \ + 3rdparty/libjpeg/jdsample.c \ + 3rdparty/libjpeg/jdtrans.c \ + 3rdparty/libjpeg/jerror.c \ + 3rdparty/libjpeg/jfdctflt.c \ + 3rdparty/libjpeg/jfdctfst.c \ + 3rdparty/libjpeg/jfdctint.c \ + 3rdparty/libjpeg/jidctflt.c \ + 3rdparty/libjpeg/jidctfst.c \ + 3rdparty/libjpeg/jidctint.c \ + 3rdparty/libjpeg/jidctred.c \ + 3rdparty/libjpeg/jmemmgr.c \ + 3rdparty/libjpeg/jquant1.c \ + 3rdparty/libjpeg/jquant2.c \ + 3rdparty/libjpeg/jutils.c \ + 3rdparty/libjpeg/jmemnobs.c + } +} +else:DEFINES += QT_NO_IMAGEIO_JPEG + +#png support +HEADERS+=$$KERNEL_H/qpngio.h +SOURCES+=$$KERNEL_CPP/qpngio.cpp +png { + system-png { + unix:LIBS += -lpng + win32:LIBS += libpng.lib + } else { + INCLUDEPATH += 3rdparty/libpng + SOURCES += 3rdparty/libpng/png.c \ + 3rdparty/libpng/pngerror.c \ + 3rdparty/libpng/pngget.c \ + 3rdparty/libpng/pngmem.c \ + 3rdparty/libpng/pngpread.c \ + 3rdparty/libpng/pngread.c \ + 3rdparty/libpng/pngrio.c \ + 3rdparty/libpng/pngrtran.c \ + 3rdparty/libpng/pngrutil.c \ + 3rdparty/libpng/pngset.c \ + 3rdparty/libpng/pngtrans.c \ + 3rdparty/libpng/pngwio.c \ + 3rdparty/libpng/pngwrite.c \ + 3rdparty/libpng/pngwtran.c \ + 3rdparty/libpng/pngwutil.c + } +} +else:DEFINES += QT_NO_IMAGEIO_PNG + +#zlib support +zlib { + INCLUDEPATH += 3rdparty/zlib + SOURCES += 3rdparty/zlib/adler32.c \ + 3rdparty/zlib/compress.c \ + 3rdparty/zlib/crc32.c \ + 3rdparty/zlib/deflate.c \ + 3rdparty/zlib/gzio.c \ + 3rdparty/zlib/inffast.c \ + 3rdparty/zlib/inflate.c \ + 3rdparty/zlib/inftrees.c \ + 3rdparty/zlib/trees.c \ + 3rdparty/zlib/uncompr.c \ + 3rdparty/zlib/zutil.c +} +!no-zlib:!zlib { + unix:LIBS += -lz + win32:LIBS += libz.lib +} + +unix:xftfreetype { + INCLUDEPATH += 3rdparty/opentype + SOURCES += 3rdparty/opentype/ftxopentype.c +} + +#use Qt gif +gif:DEFINES += QT_BUILTIN_GIF_READER=1 + diff --git a/src/kernel/qt_kernel.pri b/src/kernel/qt_kernel.pri new file mode 100644 index 0000000..5a89e08 --- /dev/null +++ b/src/kernel/qt_kernel.pri @@ -0,0 +1,267 @@ +# Qt kernel module + +kernel { + KERNEL_P = kernel + HEADERS += $$KERNEL_H/qabstractlayout.h \ + $$KERNEL_H/qaccel.h \ + $$KERNEL_P/qucomextra_p.h \ + $$KERNEL_H/qapplication.h \ + $$KERNEL_P/qapplication_p.h \ + $$KERNEL_H/qasyncimageio.h \ + $$KERNEL_H/qasyncio.h \ + $$KERNEL_H/qbitmap.h \ + $$KERNEL_H/qbrush.h \ + $$KERNEL_H/qclipboard.h \ + $$KERNEL_H/qcolor.h \ + $$KERNEL_P/qcolor_p.h \ + $$KERNEL_H/qconnection.h \ + $$KERNEL_H/qcursor.h \ + $$KERNEL_H/qdesktopwidget.h \ + $$KERNEL_H/qdragobject.h \ + $$KERNEL_H/qdrawutil.h \ + $$KERNEL_H/qdropsite.h \ + $$KERNEL_H/qevent.h \ + $$KERNEL_H/qeventloop.h\ + $$KERNEL_P/qeventloop_p.h \ + $$KERNEL_H/qfocusdata.h \ + $$KERNEL_H/qfont.h \ + $$KERNEL_P/qfontdata_p.h \ + $$KERNEL_H/qfontinfo.h \ + $$KERNEL_H/qfontmetrics.h \ + $$KERNEL_H/qguardedptr.h \ + $$KERNEL_H/qgif.h \ + $$KERNEL_H/qiconset.h \ + $$KERNEL_H/qimage.h \ + $$KERNEL_P/qimageformatinterface_p.h \ + $$KERNEL_H/qimageformatplugin.h \ + $$KERNEL_H/qkeycode.h \ + $$KERNEL_H/qkeysequence.h \ + $$KERNEL_H/qlayout.h \ + $$KERNEL_P/qlayoutengine_p.h \ + $$KERNEL_H/qtranslator.h \ + $$KERNEL_H/qmetaobject.h \ + $$KERNEL_H/qmime.h \ + $$KERNEL_H/qmovie.h \ + $$KERNEL_H/qnamespace.h \ + $$KERNEL_H/qnetworkprotocol.h \ + $$KERNEL_H/qobject.h \ + $$KERNEL_H/qobjectcleanuphandler.h \ + $$KERNEL_H/qobjectdefs.h \ + $$KERNEL_H/qobjectdict.h \ + $$KERNEL_H/qobjectlist.h \ + $$KERNEL_H/qpaintdevice.h \ + $$KERNEL_H/qpaintdevicedefs.h \ + $$KERNEL_H/qpainter.h \ + $$KERNEL_P/qpainter_p.h \ + $$KERNEL_H/qpalette.h \ + $$KERNEL_H/qpaintdevicemetrics.h \ + $$KERNEL_H/qpen.h \ + $$KERNEL_H/qpicture.h \ + $$KERNEL_H/qpixmap.h \ + $$KERNEL_H/qpixmapcache.h \ + $$KERNEL_H/qpointarray.h \ + $$KERNEL_H/qpoint.h \ + $$KERNEL_H/qpolygonscanner.h \ + $$KERNEL_H/qprinter.h \ + $$KERNEL_H/qprocess.h \ + $$KERNEL_H/qrect.h \ + $$KERNEL_H/qregion.h \ + $$KERNEL_H/qsessionmanager.h \ + $$KERNEL_H/qsignal.h \ + $$KERNEL_H/qsignalmapper.h \ + $$KERNEL_H/qsignalslotimp.h \ + $$KERNEL_H/qsize.h \ + $$KERNEL_H/qsizegrip.h \ + $$KERNEL_H/qsizepolicy.h \ + $$KERNEL_H/qsocketnotifier.h \ + $$KERNEL_H/qsound.h \ + $$KERNEL_H/qstyle.h \ + $$KERNEL_H/qstylesheet.h \ + $$KERNEL_H/qthread.h \ + $$KERNEL_H/qtimer.h \ + $$KERNEL_H/qurl.h \ + $$KERNEL_H/qlocalfs.h \ + $$KERNEL_H/qurloperator.h \ + $$KERNEL_H/qurlinfo.h \ + $$KERNEL_H/qwidget.h \ + $$KERNEL_H/qwidgetintdict.h \ + $$KERNEL_H/qwidgetlist.h \ + $$KERNEL_H/qwindowdefs.h \ + $$KERNEL_H/qwmatrix.h \ + $$KERNEL_H/qvariant.h \ + $$KERNEL_P/qrichtext_p.h \ + $$KERNEL_P/qinternal_p.h \ + $$KERNEL_H/qgplugin.h \ + $$KERNEL_H/qsimplerichtext.h \ + $$KERNEL_CPP/qscriptengine_p.h \ + $$KERNEL_CPP/qtextengine_p.h \ + $$KERNEL_CPP/qfontengine_p.h \ + $$KERNEL_CPP/qtextlayout_p.h + + unix:x11 { + HEADERS += $$KERNEL_H/qinputcontext.h + } else { + HEADERS += $$KERNEL_P/qinputcontext_p.h + } + + win32:SOURCES += $$KERNEL_CPP/qapplication_win.cpp \ + $$KERNEL_CPP/qclipboard_win.cpp \ + $$KERNEL_CPP/qcolor_win.cpp \ + $$KERNEL_CPP/qcursor_win.cpp \ + $$KERNEL_CPP/qdesktopwidget_win.cpp \ + $$KERNEL_CPP/qdnd_win.cpp \ + $$KERNEL_CPP/qeventloop_win.cpp \ + $$KERNEL_CPP/qfont_win.cpp \ + $$KERNEL_CPP/qinputcontext_win.cpp \ + $$KERNEL_CPP/qmime_win.cpp \ + $$KERNEL_CPP/qpixmap_win.cpp \ + $$KERNEL_CPP/qprinter_win.cpp \ + $$KERNEL_CPP/qprocess_win.cpp \ + $$KERNEL_CPP/qpaintdevice_win.cpp \ + $$KERNEL_CPP/qpainter_win.cpp \ + $$KERNEL_CPP/qregion_win.cpp \ + $$KERNEL_CPP/qsound_win.cpp \ + $$KERNEL_CPP/qthread_win.cpp \ + $$KERNEL_CPP/qwidget_win.cpp \ + $$KERNEL_CPP/qole_win.c \ + $$KERNEL_CPP/qfontengine_win.cpp + + unix:x11 { + SOURCES += $$KERNEL_CPP/qapplication_x11.cpp \ + $$KERNEL_CPP/qclipboard_x11.cpp \ + $$KERNEL_CPP/qcolor_x11.cpp \ + $$KERNEL_CPP/qcursor_x11.cpp \ + $$KERNEL_CPP/qdnd_x11.cpp \ + $$KERNEL_CPP/qdesktopwidget_x11.cpp \ + $$KERNEL_CPP/qeventloop_x11.cpp \ + $$KERNEL_CPP/qfont_x11.cpp \ + $$KERNEL_CPP/qinputcontext.cpp \ + $$KERNEL_CPP/qinputcontext_x11.cpp \ + $$KERNEL_CPP/qmotifdnd_x11.cpp \ + $$KERNEL_CPP/qpixmap_x11.cpp \ + $$KERNEL_CPP/qpaintdevice_x11.cpp \ + $$KERNEL_CPP/qpainter_x11.cpp \ + $$KERNEL_CPP/qregion_x11.cpp \ + $$KERNEL_CPP/qsound_x11.cpp \ + $$KERNEL_CPP/qwidget_x11.cpp \ + $$KERNEL_CPP/qwidgetcreate_x11.cpp \ + $$KERNEL_CPP/qfontengine_x11.cpp + } + + !x11:mac { + exists(qsound_mac.cpp):SOURCES += $$KERNEL_CPP/qsound_mac.cpp + else:SOURCES += $$KERNEL_CPP/qsound_qws.cpp + } + !embedded:!x11:mac { + SOURCES += $$KERNEL_CPP/qapplication_mac.cpp \ + $$KERNEL_CPP/qclipboard_mac.cpp \ + $$KERNEL_CPP/qcolor_mac.cpp \ + $$KERNEL_CPP/qcursor_mac.cpp \ + $$KERNEL_CPP/qmime_mac.cpp \ + $$KERNEL_CPP/qdnd_mac.cpp \ + $$KERNEL_CPP/qdesktopwidget_mac.cpp \ + $$KERNEL_CPP/qpixmap_mac.cpp \ + $$KERNEL_CPP/qprinter_mac.cpp \ + $$KERNEL_CPP/qpaintdevice_mac.cpp \ + $$KERNEL_CPP/qpainter_mac.cpp \ + $$KERNEL_CPP/qregion_mac.cpp \ + $$KERNEL_CPP/qwidget_mac.cpp \ + $$KERNEL_CPP/qeventloop_mac.cpp \ + $$KERNEL_CPP/qfont_mac.cpp \ + $$KERNEL_CPP/qfontengine_mac.cpp + DEFINES += QMAC_ONE_PIXEL_LOCK + } else:unix { + SOURCES += $$KERNEL_CPP/qprinter_unix.cpp \ + $$KERNEL_CPP/qpsprinter.cpp \ + $$KERNEL_CPP/qeventloop_unix.cpp + } + unix:SOURCES += $$KERNEL_CPP/qprocess_unix.cpp \ + $$KERNEL_CPP/qthread_unix.cpp + + SOURCES += $$KERNEL_CPP/qabstractlayout.cpp \ + $$KERNEL_CPP/qucomextra.cpp \ + $$KERNEL_CPP/qaccel.cpp \ + $$KERNEL_CPP/qapplication.cpp \ + $$KERNEL_CPP/qasyncimageio.cpp \ + $$KERNEL_CPP/qasyncio.cpp \ + $$KERNEL_CPP/qbitmap.cpp \ + $$KERNEL_CPP/qclipboard.cpp \ + $$KERNEL_CPP/qcolor.cpp \ + $$KERNEL_CPP/qcolor_p.cpp \ + $$KERNEL_CPP/qconnection.cpp \ + $$KERNEL_CPP/qcursor.cpp \ + $$KERNEL_CPP/qdragobject.cpp \ + $$KERNEL_CPP/qdrawutil.cpp \ + $$KERNEL_CPP/qdropsite.cpp \ + $$KERNEL_CPP/qevent.cpp \ + $$KERNEL_CPP/qeventloop.cpp \ + $$KERNEL_CPP/qfocusdata.cpp \ + $$KERNEL_CPP/qfont.cpp \ + $$KERNEL_CPP/qfontdatabase.cpp \ + $$KERNEL_CPP/qguardedptr.cpp \ + $$KERNEL_CPP/qiconset.cpp \ + $$KERNEL_CPP/qimage.cpp \ + $$KERNEL_CPP/qimageformatplugin.cpp \ + $$KERNEL_CPP/qkeysequence.cpp \ + $$KERNEL_CPP/qlayout.cpp \ + $$KERNEL_CPP/qlayoutengine.cpp \ + $$KERNEL_CPP/qtranslator.cpp \ + $$KERNEL_CPP/qmetaobject.cpp \ + $$KERNEL_CPP/qmime.cpp \ + $$KERNEL_CPP/qmovie.cpp \ + $$KERNEL_CPP/qnetworkprotocol.cpp \ + $$KERNEL_CPP/qobject.cpp \ + $$KERNEL_CPP/qobjectcleanuphandler.cpp \ + $$KERNEL_CPP/qpainter.cpp \ + $$KERNEL_CPP/qpalette.cpp \ + $$KERNEL_CPP/qpaintdevicemetrics.cpp \ + $$KERNEL_CPP/qpicture.cpp \ + $$KERNEL_CPP/qpixmap.cpp \ + $$KERNEL_CPP/qpixmapcache.cpp \ + $$KERNEL_CPP/qpointarray.cpp \ + $$KERNEL_CPP/qpoint.cpp \ + $$KERNEL_CPP/qpolygonscanner.cpp \ + $$KERNEL_CPP/qprinter.cpp \ + $$KERNEL_CPP/qprocess.cpp \ + $$KERNEL_CPP/qrect.cpp \ + $$KERNEL_CPP/qregion.cpp \ + $$KERNEL_CPP/qsignal.cpp \ + $$KERNEL_CPP/qsignalmapper.cpp \ + $$KERNEL_CPP/qsize.cpp \ + $$KERNEL_CPP/qsizegrip.cpp \ + $$KERNEL_CPP/qstyle.cpp \ + $$KERNEL_CPP/qsocketnotifier.cpp \ + $$KERNEL_CPP/qsound.cpp \ + $$KERNEL_CPP/qstylesheet.cpp \ + $$KERNEL_CPP/qthread.cpp \ + $$KERNEL_CPP/qtimer.cpp \ + $$KERNEL_CPP/qurl.cpp \ + $$KERNEL_CPP/qlocalfs.cpp \ + $$KERNEL_CPP/qurloperator.cpp \ + $$KERNEL_CPP/qurlinfo.cpp \ + $$KERNEL_CPP/qwidget.cpp \ + $$KERNEL_CPP/qwmatrix.cpp \ + $$KERNEL_CPP/qvariant.cpp \ + $$KERNEL_CPP/qrichtext.cpp \ + $$KERNEL_CPP/qinternal.cpp \ + $$KERNEL_CPP/qrichtext_p.cpp \ + $$KERNEL_CPP/qgplugin.cpp \ + $$KERNEL_CPP/qsimplerichtext.cpp \ + $$KERNEL_CPP/qscriptengine.cpp \ + $$KERNEL_CPP/qtextlayout.cpp \ + $$KERNEL_CPP/qtextengine.cpp + + unix:HEADERS += $$KERNEL_P/qpsprinter_p.h \ + $$KERNEL_H/qfontdatabase.h + + embedded:SOURCES += $$KERNEL_CPP/qsharedmemory_p.cpp \ + $$KERNEL_CPP/qfontengine_qws.cpp + + accessibility { + HEADERS += $$KERNEL_H/qaccessible.h + SOURCES += $$KERNEL_CPP/qaccessible.cpp + + !embedded:!x11:mac:SOURCES += $$KERNEL_CPP/qaccessible_mac.cpp + else:win32:SOURCES += $$KERNEL_CPP/qaccessible_win.cpp + } +} diff --git a/src/kernel/qt_pch.h b/src/kernel/qt_pch.h new file mode 100644 index 0000000..67c33b0 --- /dev/null +++ b/src/kernel/qt_pch.h @@ -0,0 +1,57 @@ +/* + * This is a precompiled header file for use in Xcode / Mac GCC / + * GCC >= 3.4 / VC to greatly speed the building of Qt. It may also be + * of use to people developing their own project, but it is probably + * better to define your own header. Use of this header is currently + * UNSUPPORTED. + */ + +#if (defined(_WIN32) || defined(__NT__)) +# define QT_UNDEF_MACROS_IN_PCH +# define _WINSCARD_H_ +# define _POSIX_ /* Make sure PATH_MAX et al. are defined */ +# include <limits.h> +# undef _POSIX_ /* Don't polute */ +#endif + +#if defined __cplusplus +# if defined(__GNUC__) +# ifndef QT_NO_STL +# include <ios> +# undef _GLIBCPP_FULLY_COMPLIANT_HEADERS // Makes qlocale.cpp compile +# endif +# endif +#include <qmap.h> // I must be first! +#include <private/qucomextra_p.h> // All moc genereated code has this include +#include <qapplication.h> +#include <qbitmap.h> +#include <qcursor.h> +#include <qdatetime.h> +#include <qglobal.h> +#include <qimage.h> +#include <qmetaobject.h> // All moc genereated code has this include +#include <qobject.h> +#include <qpainter.h> +#include <qpixmap.h> +#include <qplatformdefs.h> +#include <qptrlist.h> +#include <qstring.h> +#include <qstringlist.h> +#include <qstyle.h> +#include <qtimer.h> +#include <qwidget.h> + +#include <limits.h> +#include <stdlib.h> +#if defined(__GNUC__) +# ifndef QT_NO_STL +# define _GLIBCPP_FULLY_COMPLIANT_HEADERS +# endif +#endif +#endif + +#if defined(QT_UNDEF_MACROS_IN_PCH) +# undef max /* These are defined in windef.h, but */ +# undef min /* we don't want them when building Qt */ +# undef _WINSCARD_H_ +#endif diff --git a/src/kernel/qt_x11.pri b/src/kernel/qt_x11.pri new file mode 100644 index 0000000..9a751e6 --- /dev/null +++ b/src/kernel/qt_x11.pri @@ -0,0 +1,20 @@ +unix { + !xinerama:DEFINES += QT_NO_XINERAMA + !xshape:DEFINES += QT_NO_SHAPE + !xcursor:DEFINES += QT_NO_XCURSOR + !xrandr:DEFINES += QT_NO_XRANDR + !xrender:DEFINES += QT_NO_XRENDER + !xftfreetype:DEFINES += QT_NO_XFTFREETYPE + !xkb:DEFINES += QT_NO_XKB + xft2header:DEFINES+=QT_USE_XFT2_HEADER + + SOURCES += $$KERNEL_CPP/qtaddons_x11.cpp + PRECOMPILED_HEADER = kernel/qt_pch.h +} + +nas { + DEFINES += QT_NAS_SUPPORT + LIBS += -laudio -lXt +} + +!x11sm:DEFINES += QT_NO_SM_SUPPORT diff --git a/src/kernel/qt_x11_p.h b/src/kernel/qt_x11_p.h new file mode 100644 index 0000000..18e644c --- /dev/null +++ b/src/kernel/qt_x11_p.h @@ -0,0 +1,280 @@ +/**************************************************************************** +** +** Includes X11 system header files. +** +** Created : 981123 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QT_X11_H +#define QT_X11_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of q*_x11.cpp. This header file may change from version to version +// without notice, or even be removed. +// +// + + +#ifndef QT_H +#include "qwindowdefs.h" +#endif // QT_H + +// the following is necessary to work around breakage in many versions +// of XFree86's Xlib.h still in use +// ### which versions? +#if defined(_XLIB_H_) // crude hack, but... +#error "cannot include <X11/Xlib.h> before this file" +#endif +#define XRegisterIMInstantiateCallback qt_XRegisterIMInstantiateCallback +#define XUnregisterIMInstantiateCallback qt_XUnregisterIMInstantiateCallback +#define XSetIMValues qt_XSetIMValues +#include <X11/Xlib.h> +#undef XRegisterIMInstantiateCallback +#undef XUnregisterIMInstantiateCallback +#undef XSetIMValues + +#include <X11/Xutil.h> +#include <X11/Xos.h> +#include <X11/Xatom.h> + + +//#define QT_NO_SHAPE +#ifdef QT_NO_SHAPE +#define XShapeCombineRegion(a,b,c,d,e,f,g) +#define XShapeCombineMask(a,b,c,d,e,f,g) +#else +#include <X11/extensions/shape.h> +#endif // QT_NO_SHAPE + + +// the wacom tablet (currently just the IRIX version) +#if defined (QT_TABLET_SUPPORT) +# include <X11/extensions/XInput.h> +#if defined (Q_OS_IRIX) +# include <wacom.h> // wacom driver defines for IRIX (quite handy) +#endif +#endif // QT_TABLET_SUPPORT + + +// #define QT_NO_XINERAMA +#ifndef QT_NO_XINERAMA +# if 0 // ### Xsun, but how to detect it? +// Xinerama is only supported in Solaris 7 with patches 107648/108376 and +// Solaris 8 or above which introduce the X11R6.4 Xserver. +// To switch the Xinerama functionality on, you need to add the "+xinerama" +// argument to the Xsun start line. +// At least Solaris 7 and 8 are missing Xinerama system headers and function +// declarations (bug 4284701). +// The Xinerama API is not documented. In theory it could change but it +// probably won't because Sun are using it in at least dtlogin (bug 4221829). +extern "C" Bool XPanoramiXQueryExtension( + Display*, + int*, + int* +); +extern "C" Status XPanoramiXQueryVersion( + Display*, + int*, + int* +); +extern "C" Status XPanoramiXGetState( + Display*, + Drawable, + XPanoramiXInfo* +); +extern "C" Status XPanoramiXGetScreenCount( + Display *, + Drawable, + XPanoramiXInfo* +); +extern "C" Status XPanoramiXGetScreenSize( + Display*, + Drawable, + int, + XPanoramiXInfo* +); +# else // XFree86 +// XFree86 does not C++ify Xinerama (at least up to XFree86 4.0.3). +extern "C" { +# include <X11/extensions/Xinerama.h> +} +# endif +#endif // QT_NO_XINERAMA + +// #define QT_NO_XRANDR +#ifndef QT_NO_XRANDR +# include <X11/extensions/Xrandr.h> +#endif // QT_NO_XRANDR + +// #define QT_NO_XRENDER +#ifndef QT_NO_XRENDER +# include <X11/extensions/Xrender.h> +// #define QT_NO_XFTFREETYPE +# ifndef QT_NO_XFTFREETYPE +// This hacks around the freetype poeple putting an #error into freetype.h in 2.1.7, making +// it impossible to use an updated freetype with older Xft header files. +# include <ft2build.h> +# ifdef QT_USE_XFT2_HEADER +# include <X11/Xft/Xft2.h> +# else +# include <X11/Xft/Xft.h> +# endif // QT_USE_XFT2_HEADER +# if defined(XFT_VERSION) && XFT_VERSION >= 20000 +# define QT_XFT2 +# else +# include <X11/Xft/XftFreetype.h> +// Xft1 doesn't have these functions, so we implement them in qtaddons_x11.cpp +extern "C" { + Qt::HANDLE XftDrawPicture( XftDraw * ); + void XftDrawSetClipRectangles(XftDraw *, int, int, XRectangle *, int); + void XftDrawSetSubwindowMode(XftDraw *, int); +} +# endif // XFT_VERSION +# endif // QT_NO_XFTFREETYPE +#else +// make sure QT_NO_XFTFREETYPE is defined if QT_NO_XRENDER is defined +# ifndef QT_NO_XFTFREETYPE +# define QT_NO_XFTFREETYPE +# endif +#endif // QT_NO_XRENDER + + +#ifndef QT_NO_XSYNC +# include <X11/extensions/sync.h> +#endif // QT_NO_XSYNC + + +#ifndef QT_NO_XKB +# include <X11/XKBlib.h> +#endif // QT_NO_XKB + + +#if !defined(XlibSpecificationRelease) +# define X11R4 +typedef char *XPointer; +#else +# undef X11R4 +#endif + +// #define QT_NO_XIM +#if defined(X11R4) +// X11R4 does not have XIM +#define QT_NO_XIM +#elif defined(Q_OS_OSF) && (XlibSpecificationRelease < 6) +// broken in Xlib up to OSF/1 3.2 +#define QT_NO_XIM +#elif defined(Q_OS_AIX) +// broken in Xlib up to what version of AIX? +#define QT_NO_XIM +#elif defined(QT_NO_DEBUG) && defined(Q_OS_IRIX) +// XmbLookupString broken on IRIX +// XCreateIC broken when compiling -64 on IRIX 6.5.2 +#define QT_NO_XIM +#elif defined(Q_OS_HPUX) && defined(__LP64__) +// XCreateIC broken when compiling 64-bit ELF on HP-UX 11.0 +#define QT_NO_XIM +#elif defined(Q_OS_SCO) +// ### suggested by user... +// ### #define QT_NO_XIM +#endif // QT_NO_XIM + + +/* + * Solaris patch 108652-47 and higher fixes crases in + * XRegisterIMInstantiateCallback, but the function doesn't seem to + * work. + * + * Instead, we disabled R6 input, and open the input method + * immediately at application start. + */ +#if !defined(QT_NO_XIM) && (XlibSpecificationRelease >= 6) && \ + !defined(Q_OS_SOLARIS) +#define USE_X11R6_XIM + +//######### XFree86 has wrong declarations for XRegisterIMInstantiateCallback +//######### and XUnregisterIMInstantiateCallback in at least version 3.3.2. +//######### Many old X11R6 header files lack XSetIMValues. +//######### Therefore, we have to declare these functions ourselves. + +extern "C" Bool XRegisterIMInstantiateCallback( + Display*, + struct _XrmHashBucketRec*, + char*, + char*, + XIMProc, //XFree86 has XIDProc, which has to be wrong + XPointer +); + +extern "C" Bool XUnregisterIMInstantiateCallback( + Display*, + struct _XrmHashBucketRec*, + char*, + char*, + XIMProc, //XFree86 has XIDProc, which has to be wrong + XPointer +); + +extern "C" char *XSetIMValues( XIM /* im */, ... ); + +#endif + +#ifndef QT_NO_XIM +// some platforms (eg. Solaris 2.51) don't have these defines in Xlib.h +#ifndef XNResetState +#define XNResetState "resetState" +#endif +#ifndef XIMPreserveState +#define XIMPreserveState (1L<<1) +#endif +#endif + + +#ifndef X11R4 +# include <X11/Xlocale.h> +#endif // X11R4 + + +#ifdef QT_MITSHM +# include <X11/extensions/XShm.h> +#endif // QT_MITSHM + + +#endif // QT_X11_H diff --git a/src/kernel/qtaddons_x11.cpp b/src/kernel/qtaddons_x11.cpp new file mode 100644 index 0000000..5418bd0 --- /dev/null +++ b/src/kernel/qtaddons_x11.cpp @@ -0,0 +1,144 @@ +/* + * $XFree86: xc/lib/Xft/xftname.c,v 1.10 2001/03/30 18:50:18 keithp Exp $ + * + * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of Keith Packard not be used in + * advertising or publicity pertaining to distribution of the software without + * specific, written prior permission. Keith Packard makes no + * representations about the suitability of this software for any purpose. It + * is provided "as is" without express or implied warranty. + * + * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +#include "qt_x11_p.h" + +#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_XFT2) + +#include <X11/Xft/Xft.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +extern bool qt_use_xrender; // defined in qapplication_x11.cpp + +extern "C" { + +#define XFT_DRAW_N_SRC 2 + +struct _XftDraw { + Display *dpy; + Drawable drawable; + Visual *visual; + Colormap colormap; + Region clip; + Bool core_set; + Bool render_set; + Bool render_able; + struct { + Picture pict; + struct { + Picture pict; + XRenderColor color; + } src[XFT_DRAW_N_SRC]; + } render; + struct { + GC draw_gc; + unsigned long fg; + Font font; + } core; +}; + +Picture XftDrawPicture( XftDraw *draw ) +{ + if ( ! draw ) return 0; + if ( ! draw->render_set ) { + // force the RENDER Picture to be created... + XftColor color; + color.color.red = color.color.green = color.color.blue = color.color.alpha = + color.pixel = 0; + XftDrawRect( draw, &color, -100, -100, 1, 1 ); + } + return draw->render.pict; +} + +XftDraw *XftDrawCreateAlpha( Display *display, + Pixmap pixmap, + int depth ) +{ + // taken from Xft 1 sources, see copyright above + XftDraw *draw; + + draw = (XftDraw *) malloc (sizeof (XftDraw)); + if (!draw) + return 0; + draw->dpy = display; + draw->drawable = pixmap; + draw->visual = 0; + draw->colormap = 0; + draw->core_set = False; + draw->clip = 0; + + // Qt addition - go ahead and create the render picture now + draw->render_set = True; + draw->render_able = False; + + if ( qt_use_xrender ) { + draw->render_able = True; + + XRenderPictFormat *format = 0; + XRenderPictFormat req; + unsigned long mask = PictFormatType | PictFormatDepth | PictFormatAlphaMask; + req.type = PictTypeDirect; + req.depth = depth; + req.direct.alphaMask = 0xff; + format = XRenderFindFormat(draw->dpy, mask, &req, 0); + if (format) { + draw->render.pict = + XRenderCreatePicture(draw->dpy, draw->drawable, format, 0, 0); + } + + // to keep Xft from trying to free zero pixmaps/pictures, we need to create + // 2 more pictures (that are identical to draw->render.pict) :/ + draw->render.src[0].pict = + XRenderCreatePicture( draw->dpy, draw->drawable, format, 0, 0 ); + draw->render.src[1].pict = + XRenderCreatePicture( draw->dpy, draw->drawable, format, 0, 0 ); + } + + return draw; +} + +void XftDrawSetClipRectangles(XftDraw *draw, int xoff, int yoff, XRectangle *rects, int count) +{ + if (!draw) return; + + Picture pict = XftDrawPicture(draw); + XRenderSetPictureClipRectangles(draw->dpy, pict, xoff, yoff, rects, count); +} + +void XftDrawSetSubwindowMode(XftDraw *draw, int mode) +{ + if (!draw) return; + + Picture pict = XftDrawPicture(draw); + XRenderPictureAttributes pattr; + pattr.subwindow_mode = mode; + XRenderChangePicture(draw->dpy, pict, CPSubwindowMode, &pattr); +} + +} // extern "C" + +#endif // !QT_NO_XFTFREETYPE && !QT_XFT2 diff --git a/src/kernel/qtextengine.cpp b/src/kernel/qtextengine.cpp new file mode 100644 index 0000000..5f00fb7 --- /dev/null +++ b/src/kernel/qtextengine.cpp @@ -0,0 +1,1180 @@ +/**************************************************************************** +** +** Text engine classes +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qtextengine_p.h" + +#include "qscriptengine_p.h" +#include <qfont.h> +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include <qstring.h> +#include <private/qunicodetables_p.h> +#include <stdlib.h> + +// ----------------------------------------------------------------------------------------------------- +// +// The BiDi algorithm +// +// ----------------------------------------------------------------------------------------------------- + + +#define BIDI_DEBUG 0//2 +#if (BIDI_DEBUG >= 1) +#include <iostream> +using namespace std; + +static const char *directions[] = { + "DirL", "DirR", "DirEN", "DirES", "DirET", "DirAN", "DirCS", "DirB", "DirS", "DirWS", "DirON", + "DirLRE", "DirLRO", "DirAL", "DirRLE", "DirRLO", "DirPDF", "DirNSM", "DirBN" +}; + +#endif + +struct BidiStatus { + BidiStatus() { + eor = QChar::DirON; + lastStrong = QChar::DirON; + last = QChar:: DirON; + dir = QChar::DirON; + } + QChar::Direction eor; + QChar::Direction lastStrong; + QChar::Direction last; + QChar::Direction dir; +}; + +struct BidiControl { + struct Context { + unsigned char level : 6; + unsigned char override : 1; + unsigned char unused : 1; + }; + + inline BidiControl( bool rtl ) + : cCtx( 0 ), singleLine( FALSE ) { + ctx[0].level = (rtl ? 1 : 0); + ctx[0].override = FALSE; + } + + inline void embed( int level, bool override = FALSE ) { + if ( ctx[cCtx].level < 61 && cCtx < 61 ) { + (void) ++cCtx; + ctx[cCtx].level = level; + ctx[cCtx].override = override; + } + } + inline void pdf() { + if ( cCtx ) (void) --cCtx; + } + + inline uchar level() const { + return ctx[cCtx].level; + } + inline bool override() const { + return ctx[cCtx].override; + } + inline QChar::Direction basicDirection() { + return (ctx[0].level ? QChar::DirR : QChar:: DirL ); + } + inline uchar baseLevel() { + return ctx[0].level; + } + inline QChar::Direction direction() { + return ((ctx[cCtx].level%2) ? QChar::DirR : QChar:: DirL ); + } + + Context ctx[63]; + unsigned int cCtx : 8; + bool singleLine : 8; +}; + +static QChar::Direction basicDirection( const QString &str ) +{ + int len = str.length(); + int pos = 0; + const QChar *uc = str.unicode() + pos; + while( pos < len ) { + switch( direction( *uc ) ) + { + case QChar::DirL: + case QChar::DirLRO: + case QChar::DirLRE: + return QChar::DirL; + case QChar::DirR: + case QChar::DirAL: + case QChar::DirRLO: + case QChar::DirRLE: + return QChar::DirR; + default: + break; + } + ++pos; + ++uc; + } + return QChar::DirL; +} + + +static void qAppendItems(QTextEngine *engine, int &start, int &stop, BidiControl &control, QChar::Direction dir ) +{ + QScriptItemArray &items = engine->items; + const QChar *text = engine->string.unicode(); + + if ( start > stop ) { + // #### the algorithm is currently not really safe against this. Still needs fixing. +// qWarning( "Bidi: appendItems() internal error" ); + return; + } + + int level = control.level(); + + if(dir != QChar::DirON && !control.override()) { + // add level of run (cases I1 & I2) + if( level % 2 ) { + if(dir == QChar::DirL || dir == QChar::DirAN || dir == QChar::DirEN ) + level++; + } else { + if( dir == QChar::DirR ) + level++; + else if( dir == QChar::DirAN || dir == QChar::DirEN ) + level += 2; + } + } + +#if (BIDI_DEBUG >= 1) + qDebug("new run: dir=%s from %d, to %d level = %d\n", directions[dir], start, stop, level); +#endif + QFont::Script script = QFont::NoScript; + QScriptItem item; + item.position = start; + item.analysis.script = script; + item.analysis.bidiLevel = level; + item.analysis.override = control.override(); + item.analysis.reserved = 0; + + if ( control.singleLine ) { + for ( int i = start; i <= stop; i++ ) { + + unsigned short uc = text[i].unicode(); + QFont::Script s = (QFont::Script)scriptForChar( uc ); + if (s == QFont::UnknownScript || s == QFont::CombiningMarks) + s = script; + + if (s != script) { + item.analysis.script = s; + item.analysis.bidiLevel = level; + item.position = i; + items.append( item ); + script = s; + } + } + } else { + for ( int i = start; i <= stop; i++ ) { + + unsigned short uc = text[i].unicode(); + QFont::Script s = (QFont::Script)scriptForChar( uc ); + if (s == QFont::UnknownScript || s == QFont::CombiningMarks) + s = script; + + QChar::Category category = ::category( uc ); + if ( uc == 0xfffcU || uc == 0x2028U ) { + item.analysis.bidiLevel = level % 2 ? level-1 : level; + item.analysis.script = QFont::Latin; + item.isObject = TRUE; + s = QFont::NoScript; + } else if ((uc >= 9 && uc <=13) || + (category >= QChar::Separator_Space && category <= QChar::Separator_Paragraph)) { + item.analysis.script = QFont::Latin; + item.isSpace = TRUE; + item.isTab = ( uc == '\t' ); + item.analysis.bidiLevel = item.isTab ? control.baseLevel() : level; + s = QFont::NoScript; + } else if ( s != script && (category != QChar::Mark_NonSpacing || script == QFont::NoScript)) { + item.analysis.script = s; + item.analysis.bidiLevel = level; + } else { + if (i - start < 32000) + continue; + start = i; + } + + item.position = i; + items.append( item ); + script = s; + item.isSpace = item.isTab = item.isObject = FALSE; + } + } + ++stop; + start = stop; +} + +typedef void (* fAppendItems)(QTextEngine *, int &start, int &stop, BidiControl &control, QChar::Direction dir); +static fAppendItems appendItems = qAppendItems; + +// creates the next QScript items. +static void bidiItemize( QTextEngine *engine, bool rightToLeft, int mode ) +{ + BidiControl control( rightToLeft ); + if ( mode & QTextEngine::SingleLine ) + control.singleLine = TRUE; + + int sor = 0; + int eor = -1; + + // ### should get rid of this! + bool first = TRUE; + + int length = engine->string.length(); + + if ( !length ) + return; + + const QChar *unicode = engine->string.unicode(); + int current = 0; + + QChar::Direction dir = rightToLeft ? QChar::DirR : QChar::DirL; + BidiStatus status; + QChar::Direction sdir = direction( *unicode ); + if ( sdir != QChar::DirL && sdir != QChar::DirR && sdir != QChar::DirAL && sdir != QChar::DirEN && sdir != QChar::DirAN ) + sdir = QChar::DirON; + else + dir = QChar::DirON; + status.eor = sdir; + status.lastStrong = rightToLeft ? QChar::DirR : QChar::DirL; + status.last = status.lastStrong; + status.dir = sdir; +#if (BIDI_DEBUG >= 2) + qDebug("---- bidiReorder --- '%s'", engine->string.utf8().data()); + qDebug("rightToLeft = %d", rightToLeft); +#endif + + + while ( current <= length ) { + + QChar::Direction dirCurrent; + if ( current == (int)length ) + dirCurrent = control.basicDirection(); + else + dirCurrent = direction( unicode[current] ); + +#if (BIDI_DEBUG >= 2) + cout << "pos=" << current << " dir=" << directions[dir] + << " current=" << directions[dirCurrent] << " last=" << directions[status.last] + << " eor=" << eor << "/" << directions[status.eor] + << " sor=" << sor << " lastStrong=" + << directions[status.lastStrong] + << " level=" << (int)control.level() << endl; +#endif + + switch(dirCurrent) { + + // embedding and overrides (X1-X9 in the BiDi specs) + case QChar::DirRLE: + case QChar::DirRLO: + case QChar::DirLRE: + case QChar::DirLRO: + { + bool rtl = (dirCurrent == QChar::DirRLE || dirCurrent == QChar::DirRLO ); + bool override = (dirCurrent == QChar::DirLRO || dirCurrent == QChar::DirRLO ); + + uchar level = control.level(); + if( (level%2 != 0) == rtl ) + level += 2; + else + level++; + if(level < 61) { + eor = current-1; + appendItems(engine, sor, eor, control, dir); + eor = current; + control.embed( level, override ); + QChar::Direction edir = (rtl ? QChar::DirR : QChar::DirL ); + dir = status.eor = edir; + status.lastStrong = edir; + } + break; + } + case QChar::DirPDF: + { + if (dir != control.direction()) { + eor = current-1; + appendItems(engine, sor, eor, control, dir); + dir = control.direction(); + } + eor = current; + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirON; + status.last = control.direction(); + control.pdf(); + if ( control.override() ) + dir = control.direction(); + else + dir = QChar::DirON; + status.lastStrong = control.direction(); + break; + } + + // strong types + case QChar::DirL: + if(dir == QChar::DirON) + dir = QChar::DirL; + switch(status.last) + { + case QChar::DirL: + eor = current; status.eor = QChar::DirL; break; + case QChar::DirR: + case QChar::DirAL: + case QChar::DirEN: + case QChar::DirAN: + if ( !first ) { + appendItems(engine, sor, eor, control, dir); + dir = eor < length ? direction( unicode[eor] ) : control.basicDirection(); + status.eor = dir; + } else { + eor = current; status.eor = dir; + } + break; + case QChar::DirES: + case QChar::DirET: + case QChar::DirCS: + case QChar::DirBN: + case QChar::DirB: + case QChar::DirS: + case QChar::DirWS: + case QChar::DirON: + if(dir != QChar::DirL) { + //last stuff takes embedding dir + if( control.direction() == QChar::DirR ) { + if(status.eor != QChar::DirR) { + // AN or EN + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirON; + dir = QChar::DirR; + } + eor = current - 1; + appendItems(engine, sor, eor, control, dir); + dir = eor < length ? direction( unicode[eor] ) : control.basicDirection(); + status.eor = dir; + } else { + if(status.eor != QChar::DirL) { + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirON; + dir = QChar::DirL; + } else { + eor = current; status.eor = QChar::DirL; break; + } + } + } else { + eor = current; status.eor = QChar::DirL; + } + default: + break; + } + status.lastStrong = QChar::DirL; + break; + case QChar::DirAL: + case QChar::DirR: + if(dir == QChar::DirON) dir = QChar::DirR; + switch(status.last) + { + case QChar::DirL: + case QChar::DirEN: + case QChar::DirAN: + if ( !first ) { + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirON; + break; + } + case QChar::DirR: + case QChar::DirAL: + eor = current; status.eor = QChar::DirR; break; + case QChar::DirES: + case QChar::DirET: + case QChar::DirCS: + case QChar::DirBN: + case QChar::DirB: + case QChar::DirS: + case QChar::DirWS: + case QChar::DirON: + if( status.eor != QChar::DirR && status.eor != QChar::DirAL ) { + //last stuff takes embedding dir + if(control.direction() == QChar::DirR + || status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) { + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirON; + dir = QChar::DirR; + eor = current; + } else { + eor = current - 1; + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirON; + dir = QChar::DirR; + } + } else { + eor = current; status.eor = QChar::DirR; + } + default: + break; + } + status.lastStrong = dirCurrent; + break; + + // weak types: + + case QChar::DirNSM: + if (eor == current-1) + eor = current; + break; + case QChar::DirEN: + // if last strong was AL change EN to AN + if(status.lastStrong != QChar::DirAL) { + if(dir == QChar::DirON) { + if(status.lastStrong == QChar::DirL) + dir = QChar::DirL; + else + dir = QChar::DirEN; + } + switch(status.last) + { + case QChar::DirET: + if ( status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL ) { + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirON; + dir = QChar::DirAN; + } + // fall through + case QChar::DirEN: + case QChar::DirL: + eor = current; + status.eor = dirCurrent; + break; + case QChar::DirR: + case QChar::DirAL: + case QChar::DirAN: + if ( !first ) + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirEN; + dir = QChar::DirAN; break; + case QChar::DirES: + case QChar::DirCS: + if(status.eor == QChar::DirEN || dir == QChar::DirAN) { + eor = current; break; + } + case QChar::DirBN: + case QChar::DirB: + case QChar::DirS: + case QChar::DirWS: + case QChar::DirON: + if(status.eor == QChar::DirR) { + // neutrals go to R + eor = current - 1; + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirEN; + dir = QChar::DirAN; + } + else if( status.eor == QChar::DirL || + (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) { + eor = current; status.eor = dirCurrent; + } else { + // numbers on both sides, neutrals get right to left direction + if(dir != QChar::DirL) { + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirON; + eor = current - 1; + dir = QChar::DirR; + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirON; + dir = QChar::DirAN; + } else { + eor = current; status.eor = dirCurrent; + } + } + default: + break; + } + break; + } + case QChar::DirAN: + dirCurrent = QChar::DirAN; + if(dir == QChar::DirON) dir = QChar::DirAN; + switch(status.last) + { + case QChar::DirL: + case QChar::DirAN: + eor = current; status.eor = QChar::DirAN; break; + case QChar::DirR: + case QChar::DirAL: + case QChar::DirEN: + if ( !first ) + appendItems(engine, sor, eor, control, dir); + dir = QChar::DirON; status.eor = QChar::DirAN; + break; + case QChar::DirCS: + if(status.eor == QChar::DirAN) { + eor = current; break; + } + case QChar::DirES: + case QChar::DirET: + case QChar::DirBN: + case QChar::DirB: + case QChar::DirS: + case QChar::DirWS: + case QChar::DirON: + if(status.eor == QChar::DirR) { + // neutrals go to R + eor = current - 1; + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirAN; + dir = QChar::DirAN; + } else if( status.eor == QChar::DirL || + (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) { + eor = current; status.eor = dirCurrent; + } else { + // numbers on both sides, neutrals get right to left direction + if(dir != QChar::DirL) { + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirON; + eor = current - 1; + dir = QChar::DirR; + appendItems(engine, sor, eor, control, dir); + status.eor = QChar::DirAN; + dir = QChar::DirAN; + } else { + eor = current; status.eor = dirCurrent; + } + } + default: + break; + } + break; + case QChar::DirES: + case QChar::DirCS: + break; + case QChar::DirET: + if(status.last == QChar::DirEN) { + dirCurrent = QChar::DirEN; + eor = current; status.eor = dirCurrent; + } + break; + + // boundary neutrals should be ignored + case QChar::DirBN: + break; + // neutrals + case QChar::DirB: + // ### what do we do with newline and paragraph separators that come to here? + break; + case QChar::DirS: + // ### implement rule L1 + break; + case QChar::DirWS: + case QChar::DirON: + break; + default: + break; + } + + //cout << " after: dir=" << // dir << " current=" << dirCurrent << " last=" << status.last << " eor=" << status.eor << " lastStrong=" << status.lastStrong << " embedding=" << control.direction() << endl; + + if(current >= (int)length) break; + + // set status.last as needed. + switch(dirCurrent) { + case QChar::DirET: + case QChar::DirES: + case QChar::DirCS: + case QChar::DirS: + case QChar::DirWS: + case QChar::DirON: + switch(status.last) + { + case QChar::DirL: + case QChar::DirR: + case QChar::DirAL: + case QChar::DirEN: + case QChar::DirAN: + status.last = dirCurrent; + break; + default: + status.last = QChar::DirON; + } + break; + case QChar::DirNSM: + case QChar::DirBN: + // ignore these + break; + case QChar::DirLRO: + case QChar::DirLRE: + status.last = QChar::DirL; + break; + case QChar::DirRLO: + case QChar::DirRLE: + status.last = QChar::DirR; + break; + case QChar::DirEN: + if ( status.last == QChar::DirL ) { + status.last = QChar::DirL; + break; + } + // fall through + default: + status.last = dirCurrent; + } + + first = FALSE; + ++current; + } + +#if (BIDI_DEBUG >= 1) + cout << "reached end of line current=" << current << ", eor=" << eor << endl; +#endif + eor = current - 1; // remove dummy char + + if ( sor <= eor ) + appendItems(engine, sor, eor, control, dir); + + +} + +void QTextEngine::bidiReorder( int numItems, const Q_UINT8 *levels, int *visualOrder ) +{ + + // first find highest and lowest levels + uchar levelLow = 128; + uchar levelHigh = 0; + int i = 0; + while ( i < numItems ) { + //printf("level = %d\n", r->level); + if ( levels[i] > levelHigh ) + levelHigh = levels[i]; + if ( levels[i] < levelLow ) + levelLow = levels[i]; + i++; + } + + // implements reordering of the line (L2 according to BiDi spec): + // L2. From the highest level found in the text to the lowest odd level on each line, + // reverse any contiguous sequence of characters that are at that level or higher. + + // reversing is only done up to the lowest odd level + if(!(levelLow%2)) levelLow++; + +#if (BIDI_DEBUG >= 1) + cout << "reorderLine: lineLow = " << (uint)levelLow << ", lineHigh = " << (uint)levelHigh << endl; +#endif + + int count = numItems - 1; + for ( i = 0; i < numItems; i++ ) + visualOrder[i] = i; + + while(levelHigh >= levelLow) { + int i = 0; + while ( i < count ) { + while(i < count && levels[i] < levelHigh) i++; + int start = i; + while(i <= count && levels[i] >= levelHigh) i++; + int end = i-1; + + if(start != end) { + //cout << "reversing from " << start << " to " << end << endl; + for(int j = 0; j < (end-start+1)/2; j++) { + int tmp = visualOrder[start+j]; + visualOrder[start+j] = visualOrder[end-j]; + visualOrder[end-j] = tmp; + } + } + i++; + } + levelHigh--; + } + +#if (BIDI_DEBUG >= 1) + cout << "visual order is:" << endl; + for ( i = 0; i < numItems; i++ ) + cout << visualOrder[i] << endl; +#endif +} + + +// ----------------------------------------------------------------------------------------------------- +// +// The line break algorithm. See http://www.unicode.org/reports/tr14/tr14-13.html +// +// ----------------------------------------------------------------------------------------------------- + +/* The Unicode algorithm does in our opinion allow line breaks at some + places they shouldn't be allowed. The following changes were thus + made in comparison to the Unicode reference: + + CL->AL from Dbk to Ibk + CL->PR from Dbk to Ibk + EX->AL from Dbk to Ibk + IS->AL from Dbk to Ibk + PO->AL from Dbk to Ibk + SY->AL from Dbk to Ibk + SY->PO from Dbk to Ibk + SY->PR from Dbk to Ibk + SY->OP from Dbk to Ibk + Al->OP from Dbk to Ibk + AL->HY from Dbk to Ibk + AL->PR from Dbk to Ibk + AL->PO from Dbk to Ibk + PR->PR from Dbk to Ibk + PO->PO from Dbk to Ibk + PR->PO from Dbk to Ibk + PO->PR from Dbk to Ibk + HY->PO from Dbk to Ibk + HY->PR from Dbk to Ibk + HY->OP from Dbk to Ibk + PO->OP from Dbk to Ibk + NU->EX from Dbk to Ibk + NU->PR from Dbk to Ibk + PO->NU from Dbk to Ibk + EX->PO from Dbk to Ibk +*/ + +enum break_action { + Dbk, // Direct break + Ibk, // Indirect break; only allowed if space between the two chars + Pbk // Prohibited break; no break allowed even if space between chars +}; + +// The following line break classes are not treated by the table: +// SA, BK, CR, LF, SG, CB, SP +static const Q_UINT8 breakTable[QUnicodeTables::LineBreak_CM+1][QUnicodeTables::LineBreak_CM+1] = +{ + // OP, CL, QU, GL, NS, EX, SY, IS, PR, PO, NU, AL, ID, IN, HY, BA, BB, B2, ZW, CM + { Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk, Pbk }, // OP + { Dbk, Pbk, Ibk, Pbk, Pbk, Pbk, Pbk, Pbk, Ibk, Ibk, Dbk, Ibk, Dbk, Dbk, Ibk, Ibk, Pbk, Pbk, Pbk, Pbk }, // CL + { Pbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Pbk, Pbk }, // QU + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Pbk, Pbk }, // GL + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // NS + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Ibk, Ibk, Ibk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // EX + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // SY + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // IS + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Ibk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Pbk }, // PR + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // PO + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Dbk, Ibk, Ibk, Ibk, Dbk, Dbk, Pbk, Pbk }, // NU + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Dbk, Ibk, Ibk, Ibk, Dbk, Dbk, Pbk, Pbk }, // AL + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Ibk, Dbk, Dbk, Dbk, Ibk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // ID + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Dbk, Dbk, Dbk, Dbk, Ibk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // IN + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // HY + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Ibk, Ibk, Dbk, Dbk, Pbk, Ibk }, // BA + { Ibk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Ibk, Pbk, Ibk }, // BB + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Ibk, Ibk, Dbk, Pbk, Pbk, Ibk }, // B2 + { Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Dbk, Pbk, Ibk }, // ZW + { Dbk, Pbk, Ibk, Pbk, Ibk, Pbk, Pbk, Pbk, Dbk, Ibk, Dbk, Dbk, Dbk, Ibk, Ibk, Ibk, Dbk, Dbk, Pbk, Pbk } // CM +}; + +// set the soft break flag at every possible line breaking point. This needs correct clustering information. +static void calcLineBreaks(const QString &str, QCharAttributes *charAttributes) +{ + int len = str.length(); + if (!len) + return; + + const QChar *uc = str.unicode(); + int cls = lineBreakClass(*uc); + if (cls >= QUnicodeTables::LineBreak_CM) + cls = QUnicodeTables::LineBreak_ID; + + charAttributes[0].softBreak = FALSE; + charAttributes[0].whiteSpace = (cls == QUnicodeTables::LineBreak_SP); + charAttributes[0].charStop = TRUE; + + for (int i = 1; i < len; ++i) { + int ncls = ::lineBreakClass(uc[i]); + int category = ::category(uc[i]); + if (category == QChar::Mark_NonSpacing) + goto nsm; + + if (category == QChar::Other_Surrogate) { + // char stop only on first pair + if (uc[i].unicode() >= 0xd800 && uc[i].unicode() < 0xdc00 && i < len-1 + && uc[i+1].unicode() >= 0xdc00 && uc[i+1].unicode() < 0xe000) + goto nsm; + // ### correctly handle second surrogate + } + + if (ncls == QUnicodeTables::LineBreak_SP) { + charAttributes[i].softBreak = FALSE; + charAttributes[i].whiteSpace = TRUE; + charAttributes[i].charStop = TRUE; + cls = ncls; + continue; + } + + + if (cls == QUnicodeTables::LineBreak_SA && ncls == QUnicodeTables::LineBreak_SA) { + // two complex chars (thai or lao), thai_attributes might override, but here + // we do a best guess + charAttributes[i].softBreak = TRUE; + charAttributes[i].whiteSpace = FALSE; + charAttributes[i].charStop = TRUE; + cls = ncls; + continue; + } + { + int tcls = ncls; + if (tcls >= QUnicodeTables::LineBreak_SA) + tcls = QUnicodeTables::LineBreak_ID; + if (cls >= QUnicodeTables::LineBreak_SA) + cls = QUnicodeTables::LineBreak_ID; + + bool softBreak; + int brk = breakTable[cls][tcls]; + if (brk == Ibk) + softBreak = (cls == QUnicodeTables::LineBreak_SP); + else + softBreak = (brk == Dbk); +// qDebug("char = %c %04x, cls=%d, ncls=%d, brk=%d soft=%d", uc[i].cell(), uc[i].unicode(), cls, ncls, brk, charAttributes[i].softBreak); + charAttributes[i].softBreak = softBreak; + charAttributes[i].whiteSpace = FALSE; + charAttributes[i].charStop = TRUE; + cls = ncls; + } + continue; + nsm: + charAttributes[i].softBreak = FALSE; + charAttributes[i].whiteSpace = FALSE; + charAttributes[i].charStop = FALSE; + } +} + +#if defined( Q_WS_X11 ) || defined ( Q_WS_QWS ) +# include "qtextengine_unix.cpp" +#elif defined( Q_WS_WIN ) +# include "qtextengine_win.cpp" +#elif defined( Q_WS_MAC ) +# include "qtextengine_mac.cpp" +#endif + + + +QTextEngine::QTextEngine( const QString &str, QFontPrivate *f ) + : string( str ), fnt( f ), direction( QChar::DirON ), haveCharAttributes( FALSE ), widthOnly( FALSE ) +{ +#ifdef Q_WS_WIN + if ( !resolvedUsp10 ) + resolveUsp10(); +#endif + if ( fnt ) fnt->ref(); + + num_glyphs = QMAX( 16, str.length()*3/2 ); + int space_charAttributes = (sizeof(QCharAttributes)*str.length()+sizeof(void*)-1)/sizeof(void*); + int space_logClusters = (sizeof(unsigned short)*str.length()+sizeof(void*)-1)/sizeof(void*); + int space_glyphs = (sizeof(glyph_t)*num_glyphs+sizeof(void*)-1)/sizeof(void*); + int space_advances = (sizeof(advance_t)*num_glyphs+sizeof(void*)-1)/sizeof(void*); + int space_offsets = (sizeof(qoffset_t)*num_glyphs+sizeof(void*)-1)/sizeof(void*); + int space_glyphAttributes = (sizeof(GlyphAttributes)*num_glyphs+sizeof(void*)-1)/sizeof(void*); + + allocated = space_charAttributes + space_glyphs + space_advances + + space_offsets + space_logClusters + space_glyphAttributes; + memory = (void **)::malloc( allocated*sizeof( void * ) ); + memset( memory, 0, allocated*sizeof( void * ) ); + + void **m = memory; + m += space_charAttributes; + logClustersPtr = (unsigned short *) m; + m += space_logClusters; + glyphPtr = (glyph_t *) m; + m += space_glyphs; + advancePtr = (advance_t *) m; + m += space_advances; + offsetsPtr = (qoffset_t *) m; + m += space_offsets; + glyphAttributesPtr = (GlyphAttributes *) m; + + used = 0; +} + +QTextEngine::~QTextEngine() +{ + if ( fnt && fnt->deref()) + delete fnt; + free( memory ); + allocated = 0; +} + +void QTextEngine::reallocate( int totalGlyphs ) +{ + int new_num_glyphs = totalGlyphs; + int space_charAttributes = (sizeof(QCharAttributes)*string.length()+sizeof(void*)-1)/sizeof(void*); + int space_logClusters = (sizeof(unsigned short)*string.length()+sizeof(void*)-1)/sizeof(void*); + int space_glyphs = (sizeof(glyph_t)*new_num_glyphs+sizeof(void*)-1)/sizeof(void*); + int space_advances = (sizeof(advance_t)*new_num_glyphs+sizeof(void*)-1)/sizeof(void*); + int space_offsets = (sizeof(qoffset_t)*new_num_glyphs+sizeof(void*)-1)/sizeof(void*); + int space_glyphAttributes = (sizeof(GlyphAttributes)*new_num_glyphs+sizeof(void*)-1)/sizeof(void*); + + int newAllocated = space_charAttributes + space_glyphs + space_advances + + space_offsets + space_logClusters + space_glyphAttributes; + void ** newMemory = (void **)::malloc( newAllocated*sizeof( void * ) ); + + void **nm = newMemory; + memcpy( nm, memory, string.length()*sizeof(QCharAttributes) ); + nm += space_charAttributes; + memcpy( nm, logClustersPtr, num_glyphs*sizeof(unsigned short) ); + logClustersPtr = (unsigned short *) nm; + nm += space_logClusters; + memcpy( nm, glyphPtr, num_glyphs*sizeof(glyph_t) ); + glyphPtr = (glyph_t *) nm; + nm += space_glyphs; + memcpy( nm, advancePtr, num_glyphs*sizeof(advance_t) ); + advancePtr = (advance_t *) nm; + nm += space_advances; + memcpy( nm, offsetsPtr, num_glyphs*sizeof(qoffset_t) ); + offsetsPtr = (qoffset_t *) nm; + nm += space_offsets; + memcpy( nm, glyphAttributesPtr, num_glyphs*sizeof(GlyphAttributes) ); + glyphAttributesPtr = (GlyphAttributes *) nm; + + free( memory ); + memory = newMemory; + allocated = newAllocated; + num_glyphs = new_num_glyphs; +} + +const QCharAttributes *QTextEngine::attributes() +{ + QCharAttributes *charAttributes = (QCharAttributes *) memory; + if ( haveCharAttributes ) + return charAttributes; + + if ( !items.d ) + itemize(); + + ensureSpace(string.length()); + charAttributes = (QCharAttributes *) memory; + calcLineBreaks(string, charAttributes); + + for ( int i = 0; i < items.size(); i++ ) { + QScriptItem &si = items[i]; +#ifdef Q_WS_WIN + int script = uspScriptForItem(this, i); +#else + int script = si.analysis.script; +#endif + Q_ASSERT( script < QFont::NScripts ); + AttributeFunction attributes = scriptEngines[script].charAttributes; + if (!attributes) + continue; + int from = si.position; + int len = length( i ); + attributes( script, string, from, len, charAttributes ); + } + + haveCharAttributes = TRUE; + return charAttributes; +} + +void QTextEngine::splitItem( int item, int pos ) +{ + if ( pos <= 0 ) + return; + + // we have to ensure we get correct shaping for arabic and other + // complex languages so we have to call shape _before_ we split the item. + shape(item); + + if ( items.d->size == items.d->alloc ) + items.resize( items.d->size + 1 ); + + int numMove = items.d->size - item-1; + if ( numMove > 0 ) + memmove( items.d->items + item+2, items.d->items +item+1, numMove*sizeof( QScriptItem ) ); + items.d->size++; + QScriptItem &newItem = items.d->items[item+1]; + QScriptItem &oldItem = items.d->items[item]; + newItem = oldItem; + items.d->items[item+1].position += pos; + if ( newItem.fontEngine ) + newItem.fontEngine->ref(); + + if (oldItem.num_glyphs) { + // already shaped, break glyphs aswell + int breakGlyph = logClusters(&oldItem)[pos]; + + newItem.num_glyphs = oldItem.num_glyphs - breakGlyph; + oldItem.num_glyphs = breakGlyph; + newItem.glyph_data_offset = oldItem.glyph_data_offset + breakGlyph; + + for (int i = 0; i < newItem.num_glyphs; i++) + logClusters(&newItem)[i] -= breakGlyph; + + int w = 0; + const advance_t *a = advances(&oldItem); + for(int j = 0; j < breakGlyph; ++j) + w += *(a++); + + newItem.width = oldItem.width - w; + oldItem.width = w; + } + +// qDebug("split at position %d itempos=%d", pos, item ); +} + + +int QTextEngine::width( int from, int len ) const +{ + int w = 0; + +// qDebug("QTextEngine::width( from = %d, len = %d ), numItems=%d, strleng=%d", from, len, items.size(), string.length() ); + for ( int i = 0; i < items.size(); i++ ) { + QScriptItem *si = &items[i]; + int pos = si->position; + int ilen = length( i ); +// qDebug("item %d: from %d len %d", i, pos, ilen ); + if ( pos >= from + len ) + break; + if ( pos + ilen > from ) { + if ( !si->num_glyphs ) + shape( i ); + + advance_t *advances = this->advances( si ); + unsigned short *logClusters = this->logClusters( si ); + +// fprintf( stderr, " logclusters:" ); +// for ( int k = 0; k < ilen; k++ ) +// fprintf( stderr, " %d", logClusters[k] ); +// fprintf( stderr, "\n" ); + // do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0. + int charFrom = from - pos; + if ( charFrom < 0 ) + charFrom = 0; + int glyphStart = logClusters[charFrom]; + if ( charFrom > 0 && logClusters[charFrom-1] == glyphStart ) + while ( charFrom < ilen && logClusters[charFrom] == glyphStart ) + charFrom++; + if ( charFrom < ilen ) { + glyphStart = logClusters[charFrom]; + int charEnd = from + len - 1 - pos; + if ( charEnd >= ilen ) + charEnd = ilen-1; + int glyphEnd = logClusters[charEnd]; + while ( charEnd < ilen && logClusters[charEnd] == glyphEnd ) + charEnd++; + glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd]; + +// qDebug("char: start=%d end=%d / glyph: start = %d, end = %d", charFrom, charEnd, glyphStart, glyphEnd ); + for ( int i = glyphStart; i < glyphEnd; i++ ) + w += advances[i]; + } + } + } +// qDebug(" --> w= %d ", w ); + return w; +} + +void QTextEngine::itemize( int mode ) +{ + if ( !items.d ) { + int size = 8; + items.d = (QScriptItemArrayPrivate *)malloc( sizeof( QScriptItemArrayPrivate ) + + sizeof( QScriptItem ) * size ); + items.d->alloc = size; + } + items.d->size = 0; + if ( string.length() == 0 ) + return; + + if ( !(mode & NoBidi) ) { + if ( direction == QChar::DirON ) + direction = basicDirection( string ); + bidiItemize( this, direction == QChar::DirR, mode ); + } else { + BidiControl control( FALSE ); + if ( mode & QTextEngine::SingleLine ) + control.singleLine = TRUE; + int start = 0; + int stop = string.length() - 1; + appendItems(this, start, stop, control, QChar::DirL); + } + if ( (mode & WidthOnly) == WidthOnly ) + widthOnly = TRUE; +} + +glyph_metrics_t QTextEngine::boundingBox( int from, int len ) const +{ + glyph_metrics_t gm; + + for ( int i = 0; i < items.size(); i++ ) { + QScriptItem *si = &items[i]; + int pos = si->position; + int ilen = length( i ); + if ( pos > from + len ) + break; + if ( pos + len > from ) { + if ( !si->num_glyphs ) + shape( i ); + advance_t *advances = this->advances( si ); + unsigned short *logClusters = this->logClusters( si ); + glyph_t *glyphs = this->glyphs( si ); + qoffset_t *offsets = this->offsets( si ); + + // do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0. + int charFrom = from - pos; + if ( charFrom < 0 ) + charFrom = 0; + int glyphStart = logClusters[charFrom]; + if ( charFrom > 0 && logClusters[charFrom-1] == glyphStart ) + while ( charFrom < ilen && logClusters[charFrom] == glyphStart ) + charFrom++; + if ( charFrom < ilen ) { + glyphStart = logClusters[charFrom]; + int charEnd = from + len - 1 - pos; + if ( charEnd >= ilen ) + charEnd = ilen-1; + int glyphEnd = logClusters[charEnd]; + while ( charEnd < ilen && logClusters[charEnd] == glyphEnd ) + charEnd++; + glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd]; + if ( glyphStart <= glyphEnd ) { + QFontEngine *fe = si->fontEngine; + glyph_metrics_t m = fe->boundingBox( glyphs+glyphStart, advances+glyphStart, + offsets+glyphStart, glyphEnd-glyphStart ); + gm.x = QMIN( gm.x, m.x + gm.xoff ); + gm.y = QMIN( gm.y, m.y + gm.yoff ); + gm.width = QMAX( gm.width, m.width+gm.xoff ); + gm.height = QMAX( gm.height, m.height+gm.yoff ); + gm.xoff += m.xoff; + gm.yoff += m.yoff; + } + } + } + } + return gm; +} diff --git a/src/kernel/qtextengine_p.h b/src/kernel/qtextengine_p.h new file mode 100644 index 0000000..df7a79f --- /dev/null +++ b/src/kernel/qtextengine_p.h @@ -0,0 +1,377 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QTEXTENGINE_P_H +#define QTEXTENGINE_P_H + +#ifndef QT_H +#include "qglobal.h" +#include "qstring.h" +#include "qnamespace.h" +#include <private/qfontdata_p.h> +#endif // QT_H + +#include <stdlib.h> +#ifndef Q_OS_TEMP +#include <assert.h> +#endif // Q_OS_TEMP + +class QFontPrivate; +class QString; + +class QOpenType; +class QPainter; + +// this uses the same coordinate system as Qt, but a different one to freetype and Xft. +// * y is usually negative, and is equal to the ascent. +// * negative yoff means the following stuff is drawn higher up. +// the characters bounding rect is given by QRect( x,y,width,height), it's advance by +// xoo and yoff +struct glyph_metrics_t +{ + inline glyph_metrics_t() { + x = 100000; + y = 100000; + width = 0; + height = 0; + xoff = 0; + yoff = 0; + } + inline glyph_metrics_t( int _x, int _y, int _width, int _height, int _xoff, int _yoff ) { + x = _x; + y = _y; + width = _width; + height = _height; + xoff = _xoff; + yoff = _yoff; + } + int x; + int y; + int width; + int height; + int xoff; + int yoff; +}; + +#if defined( Q_WS_X11 ) || defined ( Q_WS_QWS ) +typedef unsigned short glyph_t; + +struct qoffset_t { + short x; + short y; +}; + +typedef int advance_t; + +struct QScriptAnalysis +{ + unsigned short script : 7; + unsigned short bidiLevel : 6; // Unicode Bidi algorithm embedding level (0-61) + unsigned short override : 1; // Set when in LRO/RLO embedding + unsigned short reserved : 2; + bool operator == ( const QScriptAnalysis &other ) { + return + script == other.script && + bidiLevel == other.bidiLevel; + // ### +// && override == other.override; + } + +}; + +#elif defined( Q_WS_MAC ) + +typedef unsigned short glyph_t; + +struct qoffset_t { + short x; + short y; +}; + +typedef int advance_t; + +struct QScriptAnalysis +{ + unsigned short script : 7; + unsigned short bidiLevel : 6; // Unicode Bidi algorithm embedding level (0-61) + unsigned short override : 1; // Set when in LRO/RLO embedding + unsigned short reserved : 2; + bool operator == ( const QScriptAnalysis &other ) { + return + script == other.script && + bidiLevel == other.bidiLevel; + // ### +// && override == other.override; + } + +}; + +#elif defined( Q_WS_WIN ) + +// do not change the definitions below unless you know what you are doing! +// it is designed to be compatible with the types found in uniscribe. + +typedef unsigned short glyph_t; + +struct qoffset_t { + int x; + int y; +}; + +typedef int advance_t; + +struct QScriptAnalysis { + unsigned short script :10; + unsigned short rtl :1; + unsigned short layoutRTL :1; + unsigned short linkBefore :1; + unsigned short linkAfter :1; + unsigned short logicalOrder :1; + unsigned short noGlyphIndex :1; + unsigned short bidiLevel :5; + unsigned short override :1; + unsigned short inhibitSymSwap :1; + unsigned short charShape :1; + unsigned short digitSubstitute :1; + unsigned short inhibitLigate :1; + unsigned short fDisplayZWG :1; + unsigned short arabicNumContext :1; + unsigned short gcpClusters :1; + unsigned short reserved :1; + unsigned short engineReserved :2; +}; + +inline bool operator == ( const QScriptAnalysis &sa1, const QScriptAnalysis &sa2 ) +{ + return + sa1.script == sa2.script && + sa1.bidiLevel == sa2.bidiLevel; + // ### +// && override == other.override; +} + +#endif + +// enum and struct are made to be compatible with Uniscribe, dont change unless you know what you're doing. +struct GlyphAttributes { + // highest value means highest priority for justification. Justification is done by first inserting kashidas + // starting with the highest priority positions, then stretching spaces, afterwards extending inter char + // spacing, and last spacing between arabic words. + // NoJustification is for example set for arabic where no Kashida can be inserted or for diacritics. + enum Justification { + NoJustification= 0, // Justification can't be applied after this glyph + Arabic_Space = 1, // This glyph represents a space inside arabic text + Character = 2, // Inter-character justification point follows this glyph + Space = 4, // This glyph represents a blank outside an Arabic run + Arabic_Normal = 7, // Normal Middle-Of-Word glyph that connects to the right (begin) + Arabic_Waw = 8, // Next character is final form of Waw/Ain/Qaf/Fa + Arabic_BaRa = 9, // Next two chars are Ba + Ra/Ya/AlefMaksura + Arabic_Alef = 10, // Next character is final form of Alef/Tah/Lam/Kaf/Gaf + Arabic_HaaDal = 11, // Next character is final form of Haa/Dal/Taa Marbutah + Arabic_Seen = 12, // Initial or Medial form Of Seen/Sad + Arabic_Kashida = 13 // Kashida(U+640) in middle of word + }; + unsigned short justification :4; // Justification class + unsigned short clusterStart :1; // First glyph of representation of cluster + unsigned short mark :1; // needs to be positioned around base char + unsigned short zeroWidth :1; // ZWJ, ZWNJ etc, with no width, currently used as "Don't print" for ZWSP + unsigned short reserved :1; + unsigned short combiningClass :8; +}; + +// also this is compatible to uniscribe. Do not change. +struct QCharAttributes { + uchar softBreak :1; // Potential linebreak point _before_ this character + uchar whiteSpace :1; // A unicode whitespace character, except NBSP, ZWNBSP + uchar charStop :1; // Valid cursor position (for left/right arrow) + uchar wordStop :1; // Valid cursor position (for ctrl + left/right arrow) + uchar invalid :1; + uchar reserved :3; +}; + +inline bool qIsZeroWidthChar(ushort uc) +{ + return (uc >= 0x200b && uc <= 0x200f /* ZW Space, ZWNJ, ZWJ, LRM and RLM */) + || (uc >= 0x2028 && uc <= 0x202f /* LS, PS, LRE, RLE, PDF, LRO, RLO, NNBSP */) + || (uc >= 0x206a && uc <= 0x206f /* ISS, ASS, IAFS, AFS, NADS, NODS */); +} + +class QFontEngine; + +struct QScriptItem +{ + inline QScriptItem() : position( 0 ), isSpace( FALSE ), isTab( FALSE ), + isObject( FALSE ), hasPositioning( FALSE ), + descent( -1 ), ascent( -1 ), width( -1 ), + x( 0 ), y( 0 ), num_glyphs( 0 ), glyph_data_offset( 0 ), + fontEngine( 0 ) { } + int position; + QScriptAnalysis analysis; + unsigned short isSpace : 1; + unsigned short isTab : 1; + unsigned short isObject : 1; + unsigned short hasPositioning : 1; + unsigned short complex : 1; // Windows only + unsigned short private_use : 1; // Windows only + unsigned short reserved : 10; + short descent; + int ascent; + int width; + int x; + int y; + int num_glyphs; + int glyph_data_offset; + QFontEngine *fontEngine; +}; + +struct QScriptItemArrayPrivate +{ + unsigned int alloc; + unsigned int size; + QScriptItem items[1]; +}; + +class QScriptItemArray +{ +public: + QScriptItemArray() : d( 0 ) {} + ~QScriptItemArray(); + + inline QScriptItem &operator[] (int i) const {return d->items[i]; } + inline void append( const QScriptItem &item ) { + if ( d->size == d->alloc ) + resize( d->size + 1 ); + d->items[d->size] = item; + d->size++; + } + inline int size() const { return d ? d->size : 0; } + + void resize( int s ); + void clear(); + + QScriptItemArrayPrivate *d; +private: +#ifdef Q_DISABLE_COPY + QScriptItemArray( const QScriptItemArray & ); + QScriptItemArray &operator = ( const QScriptItemArray & ); +#endif +}; + +class QFontPrivate; + +class QTextEngine { +public: + QTextEngine( const QString &str, QFontPrivate *f ); + ~QTextEngine(); + + enum Mode { + Full = 0x00, + NoBidi = 0x01, + SingleLine = 0x02, + WidthOnly = 0x07 + }; + + void itemize( int mode = Full ); + + static void bidiReorder( int numRuns, const Q_UINT8 *levels, int *visualOrder ); + + const QCharAttributes *attributes(); + void shape( int item ) const; + + // ### we need something for justification + + enum Edge { + Leading, + Trailing + }; + enum ShaperFlag { + RightToLeft = 0x0001, + Mirrored = 0x0001 + }; + + int width( int charFrom, int numChars ) const; + glyph_metrics_t boundingBox( int from, int len ) const; + + QScriptItemArray items; + QString string; + QFontPrivate *fnt; + int lineWidth; + int widthUsed; + int firstItemInLine; + int currentItem; + QChar::Direction direction : 5; + unsigned int haveCharAttributes : 1; + unsigned int widthOnly : 1; + unsigned int reserved : 25; + + int length( int item ) const { + const QScriptItem &si = items[item]; + int from = si.position; + item++; + return ( item < items.size() ? items[item].position : string.length() ) - from; + } + void splitItem( int item, int pos ); + + unsigned short *logClustersPtr; + glyph_t *glyphPtr; + advance_t *advancePtr; + qoffset_t *offsetsPtr; + GlyphAttributes *glyphAttributesPtr; + + inline unsigned short *logClusters( const QScriptItem *si ) const + { return logClustersPtr+si->position; } + inline glyph_t *glyphs( const QScriptItem *si ) const + { return glyphPtr+si->glyph_data_offset; } + inline advance_t *advances( const QScriptItem *si ) const + { return advancePtr+si->glyph_data_offset; } + inline qoffset_t *offsets( const QScriptItem *si ) const + { return offsetsPtr+si->glyph_data_offset; } + inline GlyphAttributes *glyphAttributes( const QScriptItem *si ) const + { return glyphAttributesPtr+si->glyph_data_offset; } + + void reallocate( int totalGlyphs ); + inline void ensureSpace( int nGlyphs ) const { + if ( num_glyphs - used < nGlyphs ) + ((QTextEngine *)this)->reallocate( ( (used + nGlyphs + 16) >> 4 ) << 4 ); + } + + int allocated; + void **memory; + int num_glyphs; + int used; +}; + +#endif diff --git a/src/kernel/qtextengine_unix.cpp b/src/kernel/qtextengine_unix.cpp new file mode 100644 index 0000000..64d0a77 --- /dev/null +++ b/src/kernel/qtextengine_unix.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Text engine classes +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 <assert.h> + + +QScriptItemArray::~QScriptItemArray() +{ + clear(); + free( d ); +} + +void QScriptItemArray::clear() +{ + if ( d ) { + for ( unsigned int i = 0; i < d->size; i++ ) { + QScriptItem &si = d->items[i]; + if ( si.fontEngine ) + si.fontEngine->deref(); + } + d->size = 0; + } +} + +void QScriptItemArray::resize( int s ) +{ + int alloc = (s + 8) >> 3 << 3; + d = (QScriptItemArrayPrivate *)realloc( d, sizeof( QScriptItemArrayPrivate ) + + sizeof( QScriptItem ) * alloc ); + d->alloc = alloc; +} + +void QTextEngine::shape( int item ) const +{ + assert( item < items.size() ); + QScriptItem &si = items[item]; + + if ( si.num_glyphs ) + return; + + QFont::Script script = (QFont::Script)si.analysis.script; + si.glyph_data_offset = used; + + if ( !si.fontEngine ) + si.fontEngine = fnt->engineForScript( script ); + si.fontEngine->ref(); + + si.ascent = si.fontEngine->ascent(); + si.descent = si.fontEngine->descent(); + si.num_glyphs = 0; + + if ( si.fontEngine && si.fontEngine != (QFontEngine*)-1 ) { + QShaperItem shaper_item; + shaper_item.script = si.analysis.script; + shaper_item.string = &string; + shaper_item.from = si.position; + shaper_item.length = length(item); + shaper_item.font = si.fontEngine; + shaper_item.num_glyphs = QMAX(int(num_glyphs - used), shaper_item.length); + shaper_item.flags = si.analysis.bidiLevel % 2 ? RightToLeft : 0; + shaper_item.has_positioning = FALSE; + + while (1) { +// qDebug(" . num_glyphs=%d, used=%d, item.num_glyphs=%d", num_glyphs, used, shaper_item.num_glyphs); + ensureSpace(shaper_item.num_glyphs); + shaper_item.num_glyphs = num_glyphs - used; +// qDebug(" .. num_glyphs=%d, used=%d, item.num_glyphs=%d", num_glyphs, used, shaper_item.num_glyphs); + shaper_item.glyphs = glyphs(&si); + shaper_item.advances = advances(&si); + shaper_item.offsets = offsets(&si); + shaper_item.attributes = glyphAttributes(&si); + shaper_item.log_clusters = logClusters(&si); + if (scriptEngines[shaper_item.script].shape(&shaper_item)) + break; + } + + si.num_glyphs = shaper_item.num_glyphs; + si.hasPositioning = shaper_item.has_positioning; + } + ((QTextEngine *)this)->used += si.num_glyphs; + + si.width = 0; + advance_t *advances = this->advances( &si ); + advance_t *end = advances + si.num_glyphs; + while ( advances < end ) { +// qDebug("advances[%d] = %d", advances - this->advances(&si), *advances); + si.width += *(advances++); + } + + return; +} + diff --git a/src/kernel/qtextlayout.cpp b/src/kernel/qtextlayout.cpp new file mode 100644 index 0000000..5f677a0 --- /dev/null +++ b/src/kernel/qtextlayout.cpp @@ -0,0 +1,643 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qtextlayout_p.h" +#include "qtextengine_p.h" + +#include <qfont.h> +#include <qapplication.h> +#include <qpainter.h> + + +QRect QTextItem::rect() const +{ + QScriptItem& si = engine->items[item]; + return QRect( si.x, si.y, si.width, si.ascent+si.descent ); +} + +int QTextItem::x() const +{ + return engine->items[item].x; +} + +int QTextItem::y() const +{ + return engine->items[item].y; +} + +int QTextItem::width() const +{ + return engine->items[item].width; +} + +int QTextItem::ascent() const +{ + return engine->items[item].ascent; +} + +int QTextItem::descent() const +{ + return engine->items[item].descent; +} + +void QTextItem::setWidth( int w ) +{ + engine->items[item].width = w; +} + +void QTextItem::setAscent( int a ) +{ + engine->items[item].ascent = a; +} + +void QTextItem::setDescent( int d ) +{ + engine->items[item].descent = d; +} + +int QTextItem::from() const +{ + return engine->items[item].position; +} + +int QTextItem::length() const +{ + return engine->length(item); +} + + +int QTextItem::cursorToX( int *cPos, Edge edge ) const +{ + int pos = *cPos; + QScriptItem *si = &engine->items[item]; + + engine->shape( item ); + advance_t *advances = engine->advances( si ); + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + unsigned short *logClusters = engine->logClusters( si ); + + int l = engine->length( item ); + if ( pos > l ) + pos = l; + if ( pos < 0 ) + pos = 0; + + int glyph_pos = pos == l ? si->num_glyphs : logClusters[pos]; + if ( edge == Trailing ) { + // trailing edge is leading edge of next cluster + while ( glyph_pos < si->num_glyphs && !glyphAttributes[glyph_pos].clusterStart ) + glyph_pos++; + } + + int x = 0; + bool reverse = engine->items[item].analysis.bidiLevel % 2; + + if ( reverse ) { + for ( int i = si->num_glyphs-1; i >= glyph_pos; i-- ) + x += advances[i]; + } else { + for ( int i = 0; i < glyph_pos; i++ ) + x += advances[i]; + } +// qDebug("cursorToX: pos=%d, gpos=%d x=%d", pos, glyph_pos, x ); + *cPos = pos; + return x; +} + +int QTextItem::xToCursor( int x, CursorPosition cpos ) const +{ + QScriptItem *si = &engine->items[item]; + engine->shape( item ); + advance_t *advances = engine->advances( si ); + unsigned short *logClusters = engine->logClusters( si ); + + int l = engine->length( item ); + bool reverse = si->analysis.bidiLevel % 2; + if ( x < 0 ) + return reverse ? l : 0; + + + if ( reverse ) { + int width = 0; + for ( int i = 0; i < si->num_glyphs; i++ ) { + width += advances[i]; + } + x = -x + width; + } + int cp_before = 0; + int cp_after = 0; + int x_before = 0; + int x_after = 0; + + int lastCluster = 0; + for ( int i = 1; i <= l; i++ ) { + int newCluster = i < l ? logClusters[i] : si->num_glyphs; + if ( newCluster != lastCluster ) { + // calculate cluster width + cp_before = cp_after; + x_before = x_after; + cp_after = i; + for ( int j = lastCluster; j < newCluster; j++ ) + x_after += advances[j]; + // qDebug("cluster boundary: lastCluster=%d, newCluster=%d, x_before=%d, x_after=%d", + // lastCluster, newCluster, x_before, x_after ); + if ( x_after > x ) + break; + lastCluster = newCluster; + } + } + + bool before = ( cpos == OnCharacters || (x - x_before) < (x_after - x) ); + +// qDebug("got cursor position for %d: %d/%d, x_ba=%d/%d using %d", +// x, cp_before,cp_after, x_before, x_after, before ? cp_before : cp_after ); + + return before ? cp_before : cp_after; + +} + + +bool QTextItem::isRightToLeft() const +{ + return (engine->items[item].analysis.bidiLevel % 2); +} + +bool QTextItem::isObject() const +{ + return engine->items[item].isObject; +} + +bool QTextItem::isSpace() const +{ + return engine->items[item].isSpace; +} + +bool QTextItem::isTab() const +{ + return engine->items[item].isTab; +} + + +QTextLayout::QTextLayout() + :d(0) {} + +QTextLayout::QTextLayout( const QString& string, QPainter *p ) +{ + QFontPrivate *f = p ? ( p->pfont ? p->pfont->d : p->cfont.d ) : QApplication::font().d; + d = new QTextEngine( (string.isNull() ? (const QString&)QString::fromLatin1("") : string), f ); +} + +QTextLayout::QTextLayout( const QString& string, const QFont& fnt ) +{ + d = new QTextEngine( (string.isNull() ? (const QString&)QString::fromLatin1("") : string), fnt.d ); +} + +QTextLayout::~QTextLayout() +{ + delete d; +} + +void QTextLayout::setText( const QString& string, const QFont& fnt ) +{ + delete d; + d = new QTextEngine( (string.isNull() ? (const QString&)QString::fromLatin1("") : string), fnt.d ); +} + +/* add an additional item boundary eg. for style change */ +void QTextLayout::setBoundary( int strPos ) +{ + if ( strPos <= 0 || strPos >= (int)d->string.length() ) + return; + + int itemToSplit = 0; + while ( itemToSplit < d->items.size() && d->items[itemToSplit].position <= strPos ) + itemToSplit++; + itemToSplit--; + if ( d->items[itemToSplit].position == strPos ) { + // already a split at the requested position + return; + } + d->splitItem( itemToSplit, strPos - d->items[itemToSplit].position ); +} + + +int QTextLayout::numItems() const +{ + return d->items.size(); +} + +QTextItem QTextLayout::itemAt( int i ) const +{ + return QTextItem( i, d ); +} + + +QTextItem QTextLayout::findItem( int strPos ) const +{ + if ( strPos == 0 && d->items.size() ) + return QTextItem( 0, d ); + // ## TODO use bsearch + for ( int i = d->items.size()-1; i >= 0; --i ) { + if ( d->items[i].position < strPos ) + return QTextItem( i, d ); + } + return QTextItem(); +} + + +void QTextLayout::beginLayout( QTextLayout::LayoutMode m ) +{ + d->items.clear(); + QTextEngine::Mode mode = QTextEngine::Full; + if (m == NoBidi) + mode = QTextEngine::NoBidi; + else if (m == SingleLine) + mode = QTextEngine::SingleLine; + d->itemize( mode ); + d->currentItem = 0; + d->firstItemInLine = -1; +} + +void QTextLayout::beginLine( int width ) +{ + d->lineWidth = width; + d->widthUsed = 0; + d->firstItemInLine = -1; +} + +bool QTextLayout::atEnd() const +{ + return d->currentItem >= d->items.size(); +} + +QTextItem QTextLayout::nextItem() +{ + d->currentItem++; + + if ( d->currentItem >= d->items.size() ) + return QTextItem(); + + d->shape( d->currentItem ); + return QTextItem( d->currentItem, d ); +} + +QTextItem QTextLayout::currentItem() +{ + if ( d->currentItem >= d->items.size() ) + return QTextItem(); + + d->shape( d->currentItem ); + return QTextItem( d->currentItem, d ); +} + +/* ## maybe also currentItem() */ +void QTextLayout::setLineWidth( int newWidth ) +{ + d->lineWidth = newWidth; +} + +int QTextLayout::lineWidth() const +{ + return d->lineWidth; +} + +int QTextLayout::widthUsed() const +{ + return d->widthUsed; +} + +int QTextLayout::availableWidth() const +{ + return d->lineWidth - d->widthUsed; +} + + +/* returns true if completely added */ +QTextLayout::Result QTextLayout::addCurrentItem() +{ + if ( d->firstItemInLine == -1 ) + d->firstItemInLine = d->currentItem; + QScriptItem ¤t = d->items[d->currentItem]; + d->shape( d->currentItem ); + d->widthUsed += current.width; +// qDebug("trying to add item %d with width %d, remaining %d", d->currentItem, current.width, d->lineWidth-d->widthUsed ); + + d->currentItem++; + + return (d->widthUsed <= d->lineWidth + || (d->currentItem < d->items.size() && d->items[d->currentItem].isSpace)) ? Ok : LineFull; +} + +QTextLayout::Result QTextLayout::endLine( int x, int y, int alignment, + int *ascent, int *descent, int *lineLeft, int *lineRight ) +{ + int available = d->lineWidth; + int numRuns = 0; + int numSpaceItems = 0; + Q_UINT8 _levels[128]; + int _visual[128]; + Q_UINT8 *levels = _levels; + int *visual = _visual; + int i; + QTextLayout::Result result = LineEmpty; + +// qDebug("endLine x=%d, y=%d, first=%d, current=%d lw=%d wu=%d", x, y, d->firstItemInLine, d->currentItem, d->lineWidth, d->widthUsed ); + int width_nobreak_found = d->widthUsed; + if ( d->firstItemInLine == -1 ) + goto end; + + if ( !(alignment & (Qt::SingleLine|Qt::IncludeTrailingSpaces)) + && d->currentItem > d->firstItemInLine && d->items[d->currentItem-1].isSpace ) { + int i = d->currentItem-1; + while ( i > d->firstItemInLine && d->items[i].isSpace ) { + numSpaceItems++; + d->widthUsed -= d->items[i--].width; + } + } + + if ( (alignment & (Qt::WordBreak|Qt::BreakAnywhere)) && + d->widthUsed > d->lineWidth ) { + // find linebreak + + // even though we removed trailing spaces the line was too wide. We'll have to break at an earlier + // position. To not confuse the layouting below, reset the number of space items + numSpaceItems = 0; + + + bool breakany = alignment & Qt::BreakAnywhere; + + const QCharAttributes *attrs = d->attributes(); + int w = 0; + int itemWidth = 0; + int breakItem = d->firstItemInLine; + int breakPosition = -1; +#if 0 + // we iterate backwards or forward depending on what we guess is closer + if ( d->widthUsed - d->lineWidth < d->lineWidth ) { + // backwards search should be faster + + } else +#endif + { + int tmpWidth = 0; + int swidth = 0; + // forward search is probably faster + for ( int i = d->firstItemInLine; i < d->currentItem; i++ ) { + const QScriptItem *si = &d->items[i]; + int length = d->length( i ); + const QCharAttributes *itemAttrs = attrs + si->position; + + advance_t *advances = d->advances( si ); + unsigned short *logClusters = d->logClusters( si ); + + int lastGlyph = 0; + int tmpItemWidth = 0; + +// qDebug("looking for break in item %d, isSpace=%d", i, si->isSpace ); + if(si->isSpace && !(alignment & (Qt::SingleLine|Qt::IncludeTrailingSpaces))) { + swidth += si->width; + } else { + tmpWidth += swidth; + swidth = 0; + for ( int pos = 0; pos < length; pos++ ) { +// qDebug("advance=%d, w=%d, tmpWidth=%d, softbreak=%d, whitespace=%d", +// *advances, w, tmpWidth, itemAttrs->softBreak, itemAttrs->whiteSpace ); + int glyph = logClusters[pos]; + if ( lastGlyph != glyph ) { + while ( lastGlyph < glyph ) + tmpItemWidth += advances[lastGlyph++]; + if ( breakPosition != -1 && w + tmpWidth + tmpItemWidth > d->lineWidth ) { +// qDebug("found break at w=%d, tmpWidth=%d, tmpItemWidth=%d", w, tmpWidth, tmpItemWidth); + d->widthUsed = w; + goto found; + } + } + if ( (itemAttrs->softBreak || + ( breakany && itemAttrs->charStop ) ) && + (i != d->firstItemInLine || pos != 0) ) { + if ( breakItem != i ) + itemWidth = 0; + if (itemAttrs->softBreak) + breakany = FALSE; + breakItem = i; + breakPosition = pos; +// qDebug("found possible break at item %d, position %d (absolute=%d), w=%d, tmpWidth=%d, tmpItemWidth=%d", breakItem, breakPosition, d->items[breakItem].position+breakPosition, w, tmpWidth, tmpItemWidth); + w += tmpWidth + tmpItemWidth; + itemWidth += tmpItemWidth; + tmpWidth = 0; + tmpItemWidth = 0; + } + itemAttrs++; + } + while ( lastGlyph < si->num_glyphs ) + tmpItemWidth += advances[lastGlyph++]; + tmpWidth += tmpItemWidth; + if ( w + tmpWidth > d->lineWidth ) { + d->widthUsed = w; + goto found; + } + } + } + } + + found: + // no valid break point found + if ( breakPosition == -1 ) { + d->widthUsed = width_nobreak_found; + goto nobreak; + } + +// qDebug("linebreak at item %d, position %d, wu=%d", breakItem, breakPosition, d->widthUsed ); + // split the line + if ( breakPosition > 0 ) { +// int length = d->length( breakItem ); + +// qDebug("splitting item, itemWidth=%d", itemWidth); + // not a full item, need to break + d->splitItem( breakItem, breakPosition ); + d->currentItem = breakItem+1; + } else { + d->currentItem = breakItem; + } + } + + result = Ok; + + nobreak: + // position the objects in the line + available -= d->widthUsed; + + numRuns = d->currentItem - d->firstItemInLine - numSpaceItems; + if ( numRuns > 127 ) { + levels = new Q_UINT8[numRuns]; + visual = new int[numRuns]; + } + +// qDebug("reordering %d runs, numSpaceItems=%d", numRuns, numSpaceItems ); + for ( i = 0; i < numRuns; i++ ) { + levels[i] = d->items[i+d->firstItemInLine].analysis.bidiLevel; +// qDebug(" level = %d", d->items[i+d->firstItemInLine].analysis.bidiLevel ); + } + d->bidiReorder( numRuns, levels, visual ); + + end: + // ### FIXME + if ( alignment & Qt::AlignJustify ) { + // #### justify items + alignment = Qt::AlignAuto; + } + if ( (alignment & Qt::AlignHorizontal_Mask) == Qt::AlignAuto ) + alignment = Qt::AlignLeft; + if ( alignment & Qt::AlignRight ) + x += available; + else if ( alignment & Qt::AlignHCenter ) + x += available/2; + + + int asc = ascent ? *ascent : 0; + int desc = descent ? *descent : 0; + + for ( i = 0; i < numRuns; i++ ) { + QScriptItem &si = d->items[d->firstItemInLine+visual[i]]; + asc = QMAX( asc, si.ascent ); + desc = QMAX( desc, si.descent ); + } + + int left = x; + for ( i = 0; i < numRuns; i++ ) { + QScriptItem &si = d->items[d->firstItemInLine+visual[i]]; +// qDebug("positioning item %d with width %d (from=%d/length=%d) at %d", d->firstItemInLine+visual[i], si.width, si.position, +// d->length(d->firstItemInLine+visual[i]), x ); + si.x = x; + si.y = y + asc; + x += si.width; + } + int right = x; + + if ( numSpaceItems ) { + if ( d->items[d->firstItemInLine+numRuns].analysis.bidiLevel % 2 ) { + x = left; + for ( i = 0; i < numSpaceItems; i++ ) { + QScriptItem &si = d->items[d->firstItemInLine + numRuns + i]; + x -= si.width; + si.x = x; + si.y = y + asc; + } + } else { + for ( i = 0; i < numSpaceItems; i++ ) { + QScriptItem &si = d->items[d->firstItemInLine + numRuns + i]; + si.x = x; + si.y = y + asc; + x += si.width; + } + } + } + + if ( lineLeft ) + *lineLeft = left; + if ( lineRight ) + *lineRight = right; + if ( ascent ) + *ascent = asc; + if ( descent ) + *descent = desc; + + if (levels != _levels) + delete []levels; + if (visual != _visual) + delete []visual; + + return result; +} + +void QTextLayout::endLayout() +{ + // nothing to do currently +} + + +int QTextLayout::nextCursorPosition( int oldPos, CursorMode mode ) const +{ +// qDebug("looking for next cursor pos for %d", oldPos ); + const QCharAttributes *attributes = d->attributes(); + int len = d->string.length(); + if ( oldPos >= len ) + return oldPos; + oldPos++; + if ( mode == SkipCharacters ) { + while ( oldPos < len && !attributes[oldPos].charStop ) + oldPos++; + } else { + while ( oldPos < len && !attributes[oldPos].wordStop && !attributes[oldPos-1].whiteSpace ) + oldPos++; + } +// qDebug(" -> %d", oldPos ); + return oldPos; +} + +int QTextLayout::previousCursorPosition( int oldPos, CursorMode mode ) const +{ +// qDebug("looking for previous cursor pos for %d", oldPos ); + const QCharAttributes *attributes = d->attributes(); + if ( oldPos <= 0 ) + return 0; + oldPos--; + if ( mode == SkipCharacters ) { + while ( oldPos && !attributes[oldPos].charStop ) + oldPos--; + } else { + while ( oldPos && !attributes[oldPos].wordStop && !attributes[oldPos-1].whiteSpace ) + oldPos--; + } +// qDebug(" -> %d", oldPos ); + return oldPos; +} + + +bool QTextLayout::validCursorPosition( int pos ) const +{ + const QCharAttributes *attributes = d->attributes(); + if ( pos < 0 || pos > (int)d->string.length() ) + return FALSE; + return attributes[pos].charStop; +} + +void QTextLayout::setDirection(QChar::Direction dir) +{ + d->direction = dir; +} diff --git a/src/kernel/qtextlayout_p.h b/src/kernel/qtextlayout_p.h new file mode 100644 index 0000000..cde0cc4 --- /dev/null +++ b/src/kernel/qtextlayout_p.h @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QTEXTLAYOUT_P_H +#define QTEXTLAYOUT_P_H + +#ifndef QT_H +#include "qstring.h" +#include "qnamespace.h" +#include "qrect.h" +#endif // QT_H + +class QTextEngine; +class QFont; + +class Q_EXPORT QTextItem +{ +public: + inline QTextItem() : item(0), engine(0) {} + inline bool isValid() const { return (bool)engine; } + + QRect rect() const; + int x() const; + int y() const; + int width() const; + int ascent() const; + int descent() const; + + enum Edge { + Leading, + Trailing + }; + enum CursorPosition { + BetweenCharacters, + OnCharacters + }; + + /* cPos gets set to the valid position */ + int cursorToX( int *cPos, Edge edge = Leading ) const; + inline int cursorToX( int cPos, Edge edge = Leading ) const { return cursorToX( &cPos, edge ); } + int xToCursor( int x, CursorPosition = BetweenCharacters ) const; + + bool isRightToLeft() const; + bool isObject() const; + bool isSpace() const; + bool isTab() const; + + void setWidth( int w ); + void setAscent( int a ); + void setDescent( int d ); + + int from() const; + int length() const; + +private: + friend class QTextLayout; + friend class QPainter; + friend class QPSPrinter; + QTextItem( int i, QTextEngine *e ) : item( i ), engine( e ) {} + int item; + QTextEngine *engine; +}; + + +class QPainter; + +class Q_EXPORT QTextLayout +{ +public: + // does itemization + QTextLayout(); + QTextLayout( const QString& string, QPainter * = 0 ); + QTextLayout( const QString& string, const QFont& fnt ); + ~QTextLayout(); + + void setText( const QString& string, const QFont& fnt ); + + enum LineBreakStrategy { + AtWordBoundaries, + AtCharBoundaries + }; + + /* add an additional item boundary eg. for style change */ + void setBoundary( int strPos ); + + int numItems() const; + QTextItem itemAt( int i ) const; + QTextItem findItem( int strPos ) const; + + enum LayoutMode { + NoBidi, + SingleLine, + MultiLine + }; + void beginLayout( LayoutMode m = MultiLine ); + void beginLine( int width ); + + bool atEnd() const; + QTextItem nextItem(); + QTextItem currentItem(); + /* ## maybe also currentItem() */ + void setLineWidth( int newWidth ); + int lineWidth() const; + int widthUsed() const; + int availableWidth() const; + + enum Result { + Ok, + LineFull, + LineEmpty, + Error + }; + /* returns true if completely added */ + Result addCurrentItem(); + + /* Note: if ascent and descent are used they must be initialized to the minimum ascent/descent + acceptable for the line. QFontMetrics::ascent/descent() is usually the right choice */ + Result endLine( int x = 0, int y = 0, int alignment = Qt::AlignLeft, + int *ascent = 0, int *descent = 0, int *left = 0, int *right = 0 ); + void endLayout(); + + enum CursorMode { + SkipCharacters, + SkipWords + }; + bool validCursorPosition( int pos ) const; + int nextCursorPosition( int oldPos, CursorMode mode = SkipCharacters ) const; + int previousCursorPosition( int oldPos, CursorMode mode = SkipCharacters ) const; + + void setDirection(QChar::Direction); +private: + QTextLayout( QTextEngine *e ) : d( e ) {} + /* disable copy and assignment */ + QTextLayout( const QTextLayout & ) {} + void operator = ( const QTextLayout & ) {} + + friend class QTextItem; + friend class QPainter; + friend class QPSPrinter; + QTextEngine *d; +}; + + +/* + class QPainter { + ..... + void drawTextItem( int x, int y, QTextItem *item ); + }; +*/ + +#endif diff --git a/src/kernel/qthread.cpp b/src/kernel/qthread.cpp new file mode 100644 index 0000000..1653a51 --- /dev/null +++ b/src/kernel/qthread.cpp @@ -0,0 +1,241 @@ +/**************************************************************************** +** +** Cross-platform QThread implementation. +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifdef QT_THREAD_SUPPORT + +#include "qplatformdefs.h" + +#include "qthread.h" +#include <private/qthreadinstance_p.h> + +#ifndef QT_H +# include "qapplication.h" +#endif // QT_H + +#if QT_VERSION >= 0x040000 +# error "Remove QThread::QThread() and QThread::start()." +#endif + + +/*! + \class QThread qthread.h + \threadsafe + \brief The QThread class provides platform-independent threads. + + \ingroup thread + \ingroup environment + + A QThread represents a separate thread of control within the + program; it shares data with all the other threads within the + process but executes independently in the way that a separate + program does on a multitasking operating system. Instead of + starting in main(), QThreads begin executing in run(). You inherit + run() to include your code. For example: + + \code + class MyThread : public QThread { + + public: + + virtual void run(); + + }; + + void MyThread::run() + { + for( int count = 0; count < 20; count++ ) { + sleep( 1 ); + qDebug( "Ping!" ); + } + } + + int main() + { + MyThread a; + MyThread b; + a.start(); + b.start(); + a.wait(); + b.wait(); + } + \endcode + + This will start two threads, each of which writes Ping! 20 times + to the screen and exits. The wait() calls at the end of main() are + necessary because exiting main() ends the program, unceremoniously + killing all other threads. Each MyThread stops executing when it + reaches the end of MyThread::run(), just as an application does + when it leaves main(). + + \sa \link threads.html Thread Support in Qt\endlink. +*/ + +/*! + \enum QThread::Priority + + This enum type indicates how the operating system should schedule + newly created threads. + + \value IdlePriority scheduled only when no other threads are + running. + + \value LowestPriority scheduled less often than LowPriority. + \value LowPriority scheduled less often than NormalPriority. + + \value NormalPriority the default priority of the operating + system. + + \value HighPriority scheduled more often than NormalPriority. + \value HighestPriority scheduled more often then HighPriority. + + \value TimeCriticalPriority scheduled as often as possible. + + \value InheritPriority use the same priority as the creating + thread. This is the default. +*/ + +QThread::QThread() +{ + d = new QThreadInstance; + d->init(0); +} + +/*! + Constructs a new thread. The thread does not begin executing until + start() is called. + + If \a stackSize is greater than zero, the maximum stack size is + set to \a stackSize bytes, otherwise the maximum stack size is + automatically determined by the operating system. + + \warning Most operating systems place minimum and maximum limits + on thread stack sizes. The thread will fail to start if the stack + size is outside these limits. +*/ +QThread::QThread( unsigned int stackSize ) +{ + d = new QThreadInstance; + d->init(stackSize); +} + +/*! + QThread destructor. + + Note that deleting a QThread object will not stop the execution of + the thread it represents. Deleting a running QThread (i.e. + finished() returns FALSE) will probably result in a program crash. + You can wait() on a thread to make sure that it has finished. +*/ +QThread::~QThread() +{ + QMutexLocker locker( d->mutex() ); + if ( d->running && !d->finished ) { +#ifdef QT_CHECK_STATE + qWarning("QThread object destroyed while thread is still running."); +#endif + + d->orphan = TRUE; + return; + } + + d->deinit(); + delete d; +} + +/*! + This function terminates the execution of the thread. The thread + may or may not be terminated immediately, depending on the + operating system's scheduling policies. Use QThread::wait() + after terminate() for synchronous termination. + + When the thread is terminated, all threads waiting for the + the thread to finish will be woken up. + + \warning This function is dangerous, and its use is discouraged. + The thread can be terminated at any point in its code path. Threads + can be terminated while modifying data. There is no chance for + the thread to cleanup after itself, unlock any held mutexes, etc. + In short, use this function only if \e absolutely necessary. +*/ +void QThread::terminate() +{ + QMutexLocker locker( d->mutex() ); + if ( d->finished || !d->running ) + return; + d->terminate(); +} + +/*! + Returns TRUE if the thread is finished; otherwise returns FALSE. +*/ +bool QThread::finished() const +{ + QMutexLocker locker( d->mutex() ); + return d->finished; +} + +/*! + Returns TRUE if the thread is running; otherwise returns FALSE. +*/ +bool QThread::running() const +{ + QMutexLocker locker( d->mutex() ); + return d->running; +} + +/*! + \fn void QThread::run() + + This method is pure virtual, and must be implemented in derived + classes in order to do useful work. Returning from this method + will end the execution of the thread. + + \sa wait() +*/ + +#ifndef QT_NO_COMPAT +/*! \obsolete + Use QApplication::postEvent() instead. +*/ +void QThread::postEvent( QObject * receiver, QEvent * event ) +{ + QApplication::postEvent( receiver, event ); +} +#endif + +#endif // QT_THREAD_SUPPORT diff --git a/src/kernel/qthread.h b/src/kernel/qthread.h new file mode 100644 index 0000000..160919f --- /dev/null +++ b/src/kernel/qthread.h @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** Definition of QThread class +** +** Created : 931107 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QTHREAD_H +#define QTHREAD_H + +#if defined(QT_THREAD_SUPPORT) + +#ifndef QT_H +#include "qwindowdefs.h" +#ifndef QT_NO_COMPAT +#include "qmutex.h" +#include "qsemaphore.h" +#include "qwaitcondition.h" +#endif // QT_NO_COMPAT +#endif // QT_H + +#include <limits.h> + +class QThreadInstance; + +class Q_EXPORT QThread : public Qt +{ +public: + static Qt::HANDLE currentThread(); + +#ifndef QT_NO_COMPAT + static void postEvent( QObject *,QEvent * ); +#endif + + static void initialize(); + static void cleanup(); + + static void exit(); + +#ifdef Q_QDOC + QThread( unsigned int stackSize = 0 ); +#else + QThread( unsigned int stackSize ); + QThread(); +#endif + + virtual ~QThread(); + + // default argument causes thread to block indefinately + bool wait( unsigned long time = ULONG_MAX ); + + enum Priority { + IdlePriority, + + LowestPriority, + LowPriority, + NormalPriority, + HighPriority, + HighestPriority, + + TimeCriticalPriority, + + InheritPriority + }; + +#ifdef Q_QDOC + void start( Priority = InheritPriority ); +#else + void start( Priority ); + void start(); +#endif + + void terminate(); + + bool finished() const; + bool running() const; + +protected: + virtual void run() = 0; + + static void sleep( unsigned long ); + static void msleep( unsigned long ); + static void usleep( unsigned long ); + +private: + QThreadInstance * d; + friend class QThreadInstance; + +#if defined(Q_DISABLE_COPY) + QThread( const QThread & ); + QThread &operator=( const QThread & ); +#endif // Q_DISABLE_COPY +}; + +#endif // QT_THREAD_SUPPORT + +#endif // QTHREAD_H diff --git a/src/kernel/qthread_unix.cpp b/src/kernel/qthread_unix.cpp new file mode 100644 index 0000000..e4d6625 --- /dev/null +++ b/src/kernel/qthread_unix.cpp @@ -0,0 +1,474 @@ +/**************************************************************************** +** +** QThread class for Unix +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#if defined(QT_THREAD_SUPPORT) + +#include "qplatformdefs.h" + +typedef pthread_mutex_t Q_MUTEX_T; + +#include "qthread.h" +#include <private/qthreadinstance_p.h> +#include <private/qmutex_p.h> +#include <private/qmutexpool_p.h> +#include <qthreadstorage.h> + +#include <errno.h> +#include <sched.h> + + +static QThreadInstance main_instance = { + 0, { 0, &main_instance }, 0, 0, 1, 0, PTHREAD_COND_INITIALIZER, 0 +}; + + +static QMutexPool *qt_thread_mutexpool = 0; + + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +typedef void*(*QtThreadCallback)(void*); + +static pthread_once_t storage_key_once = PTHREAD_ONCE_INIT; +static pthread_key_t storage_key; +static void create_storage_key() +{ + pthread_key_create( &storage_key, NULL ); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +/************************************************************************** + ** QThreadInstance + *************************************************************************/ + +QThreadInstance *QThreadInstance::current() +{ + pthread_once( &storage_key_once, create_storage_key ); + QThreadInstance *ret = (QThreadInstance *) pthread_getspecific( storage_key ); + return ret; +} + +void QThreadInstance::init(unsigned int stackSize) +{ + stacksize = stackSize; + args[0] = args[1] = 0; + thread_storage = 0; + finished = FALSE; + running = FALSE; + orphan = FALSE; + + pthread_cond_init(&thread_done, NULL); + thread_id = 0; + + // threads have not been initialized yet, do it now + if (! qt_thread_mutexpool) QThread::initialize(); +} + +void QThreadInstance::deinit() +{ + pthread_cond_destroy(&thread_done); +} + +void *QThreadInstance::start( void *_arg ) +{ + void **arg = (void **) _arg; + + pthread_once( &storage_key_once, create_storage_key ); + pthread_setspecific( storage_key, arg[1] ); + pthread_cleanup_push( QThreadInstance::finish, arg[1] ); + pthread_testcancel(); + + ( (QThread *) arg[0] )->run(); + + pthread_cleanup_pop( TRUE ); + return 0; +} + +void QThreadInstance::finish( void * ) +{ + QThreadInstance *d = current(); + + if ( ! d ) { +#ifdef QT_CHECK_STATE + qWarning( "QThread: internal error: zero data for running thread." ); +#endif // QT_CHECK_STATE + return; + } + + QMutexLocker locker( d->mutex() ); + d->running = FALSE; + d->finished = TRUE; + d->args[0] = d->args[1] = 0; + + + QThreadStorageData::finish( d->thread_storage ); + d->thread_storage = 0; + + d->thread_id = 0; + pthread_cond_broadcast(&d->thread_done); + + if (d->orphan) { + d->deinit(); + delete d; + } +} + +QMutex *QThreadInstance::mutex() const +{ + return qt_thread_mutexpool ? qt_thread_mutexpool->get( (void *) this ) : 0; +} + +void QThreadInstance::terminate() +{ + if ( ! thread_id ) return; + pthread_cancel( thread_id ); +} + + +/************************************************************************** + ** QThread + *************************************************************************/ + +/*! + This returns the thread handle of the currently executing thread. + + \warning The handle returned by this function is used for internal + purposes and should \e not be used in any application code. On + Windows, the returned value is a pseudo handle for the current + thread, and it cannot be used for numerical comparison. +*/ +Qt::HANDLE QThread::currentThread() +{ + return (HANDLE) pthread_self(); +} + +/*! \internal + Initializes the QThread system. +*/ +void QThread::initialize() +{ + if ( ! qt_global_mutexpool ) + qt_global_mutexpool = new QMutexPool( TRUE, 73 ); + if ( ! qt_thread_mutexpool ) + qt_thread_mutexpool = new QMutexPool( FALSE, 127 ); + + pthread_once( &storage_key_once, create_storage_key ); + pthread_setspecific( storage_key, &main_instance ); +} + +/*! \internal + Cleans up the QThread system. +*/ +void QThread::cleanup() +{ + delete qt_global_mutexpool; + delete qt_thread_mutexpool; + qt_global_mutexpool = 0; + qt_thread_mutexpool = 0; + + QThreadInstance::finish(&main_instance); + + pthread_once( &storage_key_once, create_storage_key ); + pthread_setspecific( storage_key, 0 ); +} + +/*! + Ends the execution of the calling thread and wakes up any threads + waiting for its termination. +*/ +void QThread::exit() +{ + pthread_exit( 0 ); +} + +/* \internal + helper function to do thread sleeps, since usleep()/nanosleep() + aren't reliable enough (in terms of behavior and availability) +*/ +static void thread_sleep( struct timespec *ti ) +{ + pthread_mutex_t mtx; + pthread_cond_t cnd; + + pthread_mutex_init(&mtx, 0); + pthread_cond_init(&cnd, 0); + + pthread_mutex_lock( &mtx ); + (void) pthread_cond_timedwait( &cnd, &mtx, ti ); + pthread_mutex_unlock( &mtx ); + + pthread_cond_destroy( &cnd ); + pthread_mutex_destroy( &mtx ); +} + +/*! + System independent sleep. This causes the current thread to sleep + for \a secs seconds. +*/ +void QThread::sleep( unsigned long secs ) +{ + struct timeval tv; + gettimeofday( &tv, 0 ); + struct timespec ti; + ti.tv_sec = tv.tv_sec + secs; + ti.tv_nsec = ( tv.tv_usec * 1000 ); + thread_sleep( &ti ); +} + +/*! + System independent sleep. This causes the current thread to sleep + for \a msecs milliseconds +*/ +void QThread::msleep( unsigned long msecs ) +{ + struct timeval tv; + gettimeofday( &tv, 0 ); + struct timespec ti; + + ti.tv_nsec = ( tv.tv_usec + ( msecs % 1000 ) * 1000 ) * 1000; + ti.tv_sec = tv.tv_sec + ( msecs / 1000 ) + ( ti.tv_nsec / 1000000000 ); + ti.tv_nsec %= 1000000000; + thread_sleep( &ti ); +} + +/*! + System independent sleep. This causes the current thread to sleep + for \a usecs microseconds +*/ +void QThread::usleep( unsigned long usecs ) +{ + struct timeval tv; + gettimeofday( &tv, 0 ); + struct timespec ti; + + ti.tv_nsec = ( tv.tv_usec + ( usecs % 1000000 ) ) * 1000; + ti.tv_sec = tv.tv_sec + ( usecs / 1000000 ) + ( ti.tv_nsec / 1000000000 ); + ti.tv_nsec %= 1000000000; + thread_sleep( &ti ); +} + +/*! + Begins execution of the thread by calling run(), which should be + reimplemented in a QThread subclass to contain your code. The + operating system will schedule the thread according to the \a + priority argument. + + If you try to start a thread that is already running, this + function will wait until the the thread has finished and then + restart the thread. + + \sa Priority +*/ +void QThread::start(Priority priority) +{ + QMutexLocker locker( d->mutex() ); + + if ( d->running ) + pthread_cond_wait(&d->thread_done, &locker.mutex()->d->handle); + d->running = TRUE; + d->finished = FALSE; + + int ret; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + +#if !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) + switch (priority) { + case InheritPriority: + { + pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); + break; + } + + default: + { + int sched_policy; + if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) { + // failed to get the scheduling policy, don't bother + // setting the priority + qWarning("QThread: cannot determine default scheduler policy"); + break; + } + + int prio_min = sched_get_priority_min(sched_policy); + int prio_max = sched_get_priority_max(sched_policy); + if (prio_min == -1 || prio_max == -1) { + // failed to get the scheduling parameters, don't + // bother setting the priority + qWarning("QThread: cannot determine scheduler priority range"); + break; + } + + int prio; + switch (priority) { + case IdlePriority: + prio = prio_min; + break; + + case HighestPriority: + prio = prio_max; + break; + + default: + // crudely scale our priority enum values to the prio_min/prio_max + prio = (((prio_max - prio_min) / TimeCriticalPriority) * + priority) + prio_min; + prio = QMAX(prio_min, QMIN(prio_max, prio)); + break; + } + + sched_param sp; + sp.sched_priority = prio; + + pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&attr, &sp); + break; + } + } +#endif // _POSIX_THREAD_PRIORITY_SCHEDULING + + if ( d->stacksize > 0 ) { +#if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0) + ret = pthread_attr_setstacksize( &attr, d->stacksize ); +#else + ret = ENOSYS; // stack size not supported, automatically fail +#endif // _POSIX_THREAD_ATTR_STACKSIZE + + if ( ret ) { +#ifdef QT_CHECK_STATE + qWarning( "QThread::start: thread stack size error: %s", strerror( ret ) ) ; +#endif // QT_CHECK_STATE + + // we failed to set the stacksize, and as the documentation states, + // the thread will fail to run... + d->running = FALSE; + d->finished = FALSE; + return; + } + } + + d->args[0] = this; + d->args[1] = d; + ret = pthread_create( &d->thread_id, &attr, (QtThreadCallback)QThreadInstance::start, d->args ); +#if defined (Q_OS_HPUX) + if (ret == EPERM) { + pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); + ret = pthread_create(&d->thread_id, &attr, (QtThreadCallback)QThreadInstance::start, d->args); + } +#endif + pthread_attr_destroy( &attr ); + + if ( ret ) { +#ifdef QT_CHECK_STATE + qWarning( "QThread::start: thread creation error: %s", strerror( ret ) ); +#endif // QT_CHECK_STATE + + d->running = FALSE; + d->finished = FALSE; + d->args[0] = d->args[1] = 0; + } +} + +void QThread::start() +{ + start(InheritPriority); +} + +/*! + A thread calling this function will block until either of these + conditions is met: + + \list + \i The thread associated with this QThread object has finished + execution (i.e. when it returns from \l{run()}). This function + will return TRUE if the thread has finished. It also returns + TRUE if the thread has not been started yet. + \i \a time milliseconds has elapsed. If \a time is ULONG_MAX (the + default), then the wait will never timeout (the thread must + return from \l{run()}). This function will return FALSE if the + wait timed out. + \endlist + + This provides similar functionality to the POSIX \c pthread_join() function. +*/ +bool QThread::wait( unsigned long time ) +{ + QMutexLocker locker( d->mutex() ); + + if ( d->thread_id == pthread_self() ) { +#ifdef QT_CHECK_STATE + qWarning( "QThread::wait: thread tried to wait on itself" ); +#endif // QT_CHECK_STATE + + return FALSE; + } + + if ( d->finished || ! d->running ) + return TRUE; + + int ret; + if (time != ULONG_MAX) { + struct timeval tv; + gettimeofday(&tv, 0); + + timespec ti; + ti.tv_nsec = (tv.tv_usec + (time % 1000) * 1000) * 1000; + ti.tv_sec = tv.tv_sec + (time / 1000) + (ti.tv_nsec / 1000000000); + ti.tv_nsec %= 1000000000; + + ret = pthread_cond_timedwait(&d->thread_done, &locker.mutex()->d->handle, &ti); + } else + ret = pthread_cond_wait(&d->thread_done, &locker.mutex()->d->handle); + +#ifdef QT_CHECK_RANGE + if (ret && ret != ETIMEDOUT) + qWarning("Wait condition wait failure: %s",strerror(ret)); +#endif + + return (ret == 0); +} + + +#endif // QT_THREAD_SUPPORT diff --git a/src/kernel/qtimer.cpp b/src/kernel/qtimer.cpp new file mode 100644 index 0000000..c748425 --- /dev/null +++ b/src/kernel/qtimer.cpp @@ -0,0 +1,338 @@ +/**************************************************************************** +** +** Implementation of QTimer class +** +** Created : 931111 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qtimer.h" +#include "qsignal.h" +#include "qobjectlist.h" + +/*! + \class QTimer qtimer.h + \brief The QTimer class provides timer signals and single-shot timers. + + \ingroup time + \ingroup events + \mainclass + + It uses \link QTimerEvent timer events\endlink internally to + provide a more versatile timer. QTimer is very easy to use: + create a QTimer, call start() to start it and connect its + timeout() to the appropriate slots. When the time is up it will + emit the timeout() signal. + + Note that a QTimer object is destroyed automatically when its + parent object is destroyed. + + Example: + \code + QTimer *timer = new QTimer( myObject ); + connect( timer, SIGNAL(timeout()), myObject, SLOT(timerDone()) ); + timer->start( 2000, TRUE ); // 2 seconds single-shot timer + \endcode + + You can also use the static singleShot() function to create a + single shot timer. + + As a special case, a QTimer with timeout 0 times out as soon as + all the events in the window system's event queue have been + processed. + + This can be used to do heavy work while providing a snappy + user interface: + \code + QTimer *t = new QTimer( myObject ); + connect( t, SIGNAL(timeout()), SLOT(processOneThing()) ); + t->start( 0, FALSE ); + \endcode + + myObject->processOneThing() will be called repeatedly and should + return quickly (typically after processing one data item) so that + Qt can deliver events to widgets and stop the timer as soon as it + has done all its work. This is the traditional way of + implementing heavy work in GUI applications; multi-threading is + now becoming available on more and more platforms, and we expect + that null events will eventually be replaced by threading. + + Note that QTimer's accuracy depends on the underlying operating + system and hardware. Most platforms support an accuracy of 20ms; + some provide more. If Qt is unable to deliver the requested + number of timer clicks, it will silently discard some. + + An alternative to using QTimer is to call QObject::startTimer() + for your object and reimplement the QObject::timerEvent() event + handler in your class (which must, of course, inherit QObject). + The disadvantage is that timerEvent() does not support such + high-level features as single-shot timers or signals. + + Some operating systems limit the number of timers that may be + used; Qt tries to work around these limitations. +*/ + + +static const int INV_TIMER = -1; // invalid timer id + + +/*! + Constructs a timer called \a name, with the parent \a parent. + + Note that the parent object's destructor will destroy this timer + object. +*/ + +QTimer::QTimer( QObject *parent, const char *name ) + : QObject( parent, name ), id(INV_TIMER), single(0), nulltimer(0) +{ +} + +/*! + Destroys the timer. +*/ + +QTimer::~QTimer() +{ + if ( id != INV_TIMER ) // stop running timer + stop(); +} + + +/*! + \fn void QTimer::timeout() + + This signal is emitted when the timer is activated. +*/ + +/*! + \fn bool QTimer::isActive() const + + Returns TRUE if the timer is running (pending); otherwise returns + FALSE. +*/ + +/*! + \fn int QTimer::timerId() const + + Returns the ID of the timer if the timer is running; otherwise returns + -1. +*/ + + +/*! + Starts the timer with a \a msec milliseconds timeout, and returns + the ID of the timer, or zero when starting the timer failed. + + If \a sshot is TRUE, the timer will be activated only once; + otherwise it will continue until it is stopped. + + Any pending timer will be stopped. + + \sa singleShot() stop(), changeInterval(), isActive() +*/ + +int QTimer::start( int msec, bool sshot ) +{ + if ( id >=0 && nulltimer && !msec && sshot ) + return id; + if ( id != INV_TIMER ) // stop running timer + stop(); + single = sshot; + nulltimer = ( !msec && sshot ); + return id = startTimer( msec ); +} + + +/*! + Changes the timeout interval to \a msec milliseconds. + + If the timer signal is pending, it will be stopped and restarted; + otherwise it will be started. + + \sa start(), isActive() +*/ + +void QTimer::changeInterval( int msec ) +{ + if ( id == INV_TIMER ) { // create new timer + start( msec ); + } else { + killTimer( id ); // restart timer + id = startTimer( msec ); + } +} + +/*! + Stops the timer. + + \sa start() +*/ + +void QTimer::stop() +{ + if ( id != INV_TIMER ) { + killTimer( id ); + id = INV_TIMER; + } +} + + +/*! + \reimp +*/ +bool QTimer::event( QEvent *e ) +{ + if ( e->type() != QEvent::Timer ) // ignore all other events + return FALSE; + if ( single ) // stop single shot timer + stop(); + emit timeout(); // emit timeout signal + return TRUE; +} + + +/* + The QSingleShotTimer class is an internal class for implementing + QTimer::singleShot(). It starts a timer and emits the signal + and kills itself when it gets the timeout. +*/ + +static QObjectList *sst_list = 0; // list of single shot timers + +static void sst_cleanup() +{ + if ( sst_list ) { + sst_list->setAutoDelete( TRUE ); + delete sst_list; + sst_list = 0; + } +} + +static void sst_init() +{ + if ( !sst_list ) { + sst_list = new QObjectList; + Q_CHECK_PTR( sst_list ); + qAddPostRoutine( sst_cleanup ); + } +} + + +class QSingleShotTimer : public QObject +{ +public: + ~QSingleShotTimer(); + bool start( int msec, QObject *r, const char * m ); + bool isActive() const { return timerId > 0; } +protected: + bool event( QEvent * ); +private: + QSignal signal; + int timerId; +}; + +extern int qStartTimer( int interval, QObject *obj ); // implemented in qapp_xxx.cpp +extern bool qKillTimer( int id ); + +QSingleShotTimer::~QSingleShotTimer() +{ + if (timerId != 0) { + qKillTimer(timerId); + timerId = 0; + } +} + +bool QSingleShotTimer::start( int msec, QObject *r, const char *m ) +{ + timerId = 0; + if ( signal.connect(r, m) ) + timerId = qStartTimer( msec, (QObject *)this ); + return timerId != 0; +} + +bool QSingleShotTimer::event( QEvent * ) +{ + qKillTimer( timerId ); // no more timeouts + signal.activate(); // emit the signal + signal.disconnect( 0, 0 ); + timerId = 0; // mark as inactive + return TRUE; +} + + +/*! + This static function calls a slot after a given time interval. + + It is very convenient to use this function because you do not need + to bother with a \link QObject::timerEvent() timerEvent\endlink or + to create a local QTimer object. + + Example: + \code + #include <qapplication.h> + #include <qtimer.h> + + int main( int argc, char **argv ) + { + QApplication a( argc, argv ); + QTimer::singleShot( 10*60*1000, &a, SLOT(quit()) ); + ... // create and show your widgets + return a.exec(); + } + \endcode + + This sample program automatically terminates after 10 minutes (i.e. + 600000 milliseconds). + + The \a receiver is the receiving object and the \a member is the + slot. The time interval is \a msec. +*/ + +void QTimer::singleShot( int msec, QObject *receiver, const char *member ) +{ + if ( !sst_list ) + sst_init(); + // search the list for a free ss timer we could reuse + QSingleShotTimer *sst = (QSingleShotTimer*)sst_list->first(); + while ( sst && sst->isActive() ) + sst = (QSingleShotTimer*)sst_list->next(); + // create a new one if not successful + if ( !sst ) { + sst = new QSingleShotTimer; + sst_list->append( sst ); + } + sst->start(msec, receiver, member); +} diff --git a/src/kernel/qtimer.h b/src/kernel/qtimer.h new file mode 100644 index 0000000..565cc25 --- /dev/null +++ b/src/kernel/qtimer.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Definition of QTimer class +** +** Created : 931111 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QTIMER_H +#define QTIMER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + + +class Q_EXPORT QTimer : public QObject +{ + Q_OBJECT +public: + QTimer( QObject *parent=0, const char *name=0 ); + ~QTimer(); + + bool isActive() const; + + int start( int msec, bool sshot = FALSE ); + void changeInterval( int msec ); + void stop(); + + static void singleShot( int msec, QObject *receiver, const char *member ); + + int timerId() const { return id; } + +signals: + void timeout(); + +protected: + bool event( QEvent * ); + +private: + int id; + uint single : 1; + uint nulltimer : 1; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QTimer( const QTimer & ); + QTimer &operator=( const QTimer & ); +#endif +}; + + +inline bool QTimer::isActive() const +{ + return id >= 0; +} + + +#endif // QTIMER_H diff --git a/src/kernel/qtranslator.cpp b/src/kernel/qtranslator.cpp new file mode 100644 index 0000000..ca160f9 --- /dev/null +++ b/src/kernel/qtranslator.cpp @@ -0,0 +1,1478 @@ +/**************************************************************************** +** +** Localization database support. +** +** Created : 980906 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +static inline int qt_open(const char *pathname, int flags, mode_t mode) +{ return ::open(pathname, flags, mode); } +#if defined(open) +# undef open +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +#include "qtranslator.h" + +#ifndef QT_NO_TRANSLATION + +#include "qfileinfo.h" +#include "qwidgetlist.h" +#include "qintdict.h" +#include "qstring.h" +#include "qapplication.h" +#include "qfile.h" +#include "qbuffer.h" +#include "qdatastream.h" +#include "qmap.h" +#include "qtl.h" + +#if defined(Q_OS_UNIX) +#define QT_USE_MMAP +#endif + +// most of the headers below are already included in qplatformdefs.h +// also this lacks Large File support but that's probably irrelevant +#if defined(QT_USE_MMAP) +// for mmap +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <errno.h> +// for htonl +#include <netinet/in.h> +#endif + +#include <stdlib.h> + +/* +$ mcookie +3cb86418caef9c95cd211cbf60a1bddd +$ +*/ + +// magic number for the file +static const int MagicLength = 16; +static const uchar magic[MagicLength] = { + 0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95, + 0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd +}; + +static bool match( const char* found, const char* target ) +{ + // 0 means anything, "" means empty + return found == 0 || qstrcmp( found, target ) == 0; +} + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +/* + Yes, unfortunately, we have code here that depends on endianness. + The candidate is big endian (it comes from a .qm file) whereas the + target endianness depends on the system Qt is running on. +*/ +#ifdef Q_OS_TEMP +static int __cdecl cmp_uint32_little( const void* target, const void* candidate ) +#else +static int cmp_uint32_little( const void* target, const void* candidate ) +#endif +{ + const uchar* t = (const uchar*) target; + const uchar* c = (const uchar*) candidate; + return t[3] != c[0] ? (int) t[3] - (int) c[0] + : t[2] != c[1] ? (int) t[2] - (int) c[1] + : t[1] != c[2] ? (int) t[1] - (int) c[2] + : (int) t[0] - (int) c[3]; +} + +#ifdef Q_OS_TEMP +static int __cdecl cmp_uint32_big( const void* target, const void* candidate ) +#else +static int cmp_uint32_big( const void* target, const void* candidate ) +#endif +{ + const uchar* t = (const uchar*) target; + const uchar* c = (const uchar*) candidate; + return t[0] != c[0] ? (int) t[0] - (int) c[0] + : t[1] != c[1] ? (int) t[1] - (int) c[1] + : t[2] != c[2] ? (int) t[2] - (int) c[2] + : (int) t[3] - (int) c[3]; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +static int systemWordSize = 0; +static bool systemBigEndian; + +static uint elfHash( const char * name ) +{ + const uchar *k; + uint h = 0; + uint g; + + if ( name ) { + k = (const uchar *) name; + while ( *k ) { + h = ( h << 4 ) + *k++; + if ( (g = (h & 0xf0000000)) != 0 ) + h ^= g >> 24; + h &= ~g; + } + } + if ( !h ) + h = 1; + return h; +} + +extern bool qt_detectRTLLanguage(); + +class QTranslatorPrivate { +public: + struct Offset { + Offset() + : h( 0 ), o( 0 ) { } + Offset( const QTranslatorMessage& m, int offset ) + : h( m.hash() ), o( offset ) { } + + bool operator<( const Offset&k ) const { + return ( h != k.h ) ? h < k.h : o < k.o; + } + Q_DUMMY_COMPARISON_OPERATOR(QTranslatorPrivate::Offset) + uint h; + uint o; + }; + + enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69 }; + + QTranslatorPrivate() : + unmapPointer( 0 ), unmapLength( 0 ), + messageArray( 0 ), offsetArray( 0 ), contextArray( 0 ) +#ifndef QT_NO_TRANSLATION_BUILDER + , messages( 0 ) +#endif + { } + // QTranslator must finalize this before deallocating it + + // for mmap'ed files, this is what needs to be unmapped. + char * unmapPointer; + unsigned int unmapLength; + + // for squeezed but non-file data, this is what needs to be deleted + QByteArray * messageArray; + QByteArray * offsetArray; + QByteArray * contextArray; + +#ifndef QT_NO_TRANSLATION_BUILDER + QMap<QTranslatorMessage, void *> * messages; +#endif +#ifdef Q_WS_WIN + int oldPermissionLookup; +#endif +}; + + +/*! + \class QTranslator + + \brief The QTranslator class provides internationalization support for text + output. + + \ingroup i18n + \ingroup environment + \mainclass + + An object of this class contains a set of QTranslatorMessage + objects, each of which specifies a translation from a source + language to a target language. QTranslator provides functions to + look up translations, add new ones, remove them, load and save + them, etc. + + The most common use of QTranslator is to: load a translator file + created with \link linguist-manual.book Qt Linguist\endlink, + install it using QApplication::installTranslator(), and use it via + QObject::tr(). For example: + + \code + int main( int argc, char ** argv ) + { + QApplication app( argc, argv ); + + QTranslator translator( 0 ); + translator.load( "french.qm", "." ); + app.installTranslator( &translator ); + + MyWidget m; + app.setMainWidget( &m ); + m.show(); + + return app.exec(); + } + \endcode + Note that the translator must be created \e before the + application's main window. + + Most applications will never need to do anything else with this + class. The other functions provided by this class are useful for + applications that work on translator files. + + We call a translation a "messsage". For this reason, translation + files are sometimes referred to as "message files". + + It is possible to lookup a translation using findMessage() (as + tr() and QApplication::translate() do) and contains(), to insert a + new translation messsage using insert(), and to remove one using + remove(). + + Translation tools often need more information than the bare source + text and translation, for example, context information to help + the translator. But end-user programs that are using translations + usually only need lookup. To cater for these different needs, + QTranslator can use stripped translator files that use the minimum + of memory and which support little more functionality than + findMessage(). + + Thus, load() may not load enough information to make anything more + than findMessage() work. save() has an argument indicating + whether to save just this minimum of information or to save + everything. + + "Everything" means that for each translation item the following + information is kept: + + \list + \i The \e {translated text} - the return value from tr(). + \i The input key: + \list + \i The \e {source text} - usually the argument to tr(). + \i The \e context - usually the class name for the tr() caller. + \i The \e comment - a comment that helps disambiguate different uses + of the same text in the same context. + \endlist + \endlist + + The minimum for each item is just the information necessary for + findMessage() to return the right text. This may include the + source, context and comment, but usually it is just a hash value + and the translated text. + + For example, the "Cancel" in a dialog might have "Anuluj" when the + program runs in Polish (in this case the source text would be + "Cancel"). The context would (normally) be the dialog's class + name; there would normally be no comment, and the translated text + would be "Anuluj". + + But it's not always so simple. The Spanish version of a printer + dialog with settings for two-sided printing and binding would + probably require both "Activado" and "Activada" as translations + for "Enabled". In this case the source text would be "Enabled" in + both cases, and the context would be the dialog's class name, but + the two items would have disambiguating comments such as + "two-sided printing" for one and "binding" for the other. The + comment enables the translator to choose the appropriate gender + for the Spanish version, and enables Qt to distinguish between + translations. + + Note that when QTranslator loads a stripped file, most functions + do not work. The functions that do work with stripped files are + explicitly documented as such. + + \sa QTranslatorMessage QApplication::installTranslator() + QApplication::removeTranslator() QObject::tr() QApplication::translate() +*/ + +/*! + \enum QTranslator::SaveMode + + This enum type defines how QTranslator writes translation + files. There are two modes: + + \value Everything files are saved with all available information + \value Stripped files are saved with just enough information for + end-user applications + + Note that when QTranslator loads a stripped file, most functions do + not work. The functions that do work with stripped files are + explicitly documented as such. +*/ + +#if defined(Q_WS_WIN) +extern int qt_ntfs_permission_lookup; +#endif + +/*! + Constructs an empty message file object that is not connected to + any file. The object is called \a name with parent \a parent. +*/ + +QTranslator::QTranslator( QObject * parent, const char * name ) + : QObject( parent, name ) +{ + d = new QTranslatorPrivate; +#if defined(Q_WS_WIN) + d->oldPermissionLookup = qt_ntfs_permission_lookup; +#endif +} + + +/*! + Destroys the object and frees any allocated resources. +*/ + +QTranslator::~QTranslator() +{ + if ( qApp ) + qApp->removeTranslator( this ); + clear(); + delete d; +} + + +extern bool qt_detectRTLLanguage(); + +/*! + Loads \a filename, which may be an absolute file name or relative + to \a directory. The previous contents of this translator object + is discarded. Returns TRUE if the file is loaded successfully; + otherwise returns FALSE. + + If the full file name does not exist, other file names are tried + in the following order: + + \list 1 + \i File name with \a suffix appended (".qm" if the \a suffix is + QString::null). + \i File name with text after a character in \a search_delimiters + stripped ("_." is the default for \a search_delimiters if it is + QString::null). + \i File name stripped and \a suffix appended. + \i File name stripped further, etc. + \endlist + + For example, an application running in the fr_CA locale + (French-speaking Canada) might call load("foo.fr_ca", + "/opt/foolib"). load() would then try to open the first existing + readable file from this list: + + \list 1 + \i /opt/foolib/foo.fr_ca + \i /opt/foolib/foo.fr_ca.qm + \i /opt/foolib/foo.fr + \i /opt/foolib/foo.fr.qm + \i /opt/foolib/foo + \i /opt/foolib/foo.qm + \endlist + + \sa save() +*/ + +bool QTranslator::load( const QString & filename, const QString & directory, + const QString & search_delimiters, + const QString & suffix ) +{ + clear(); + + QString prefix; + + if ( filename[0] == '/' +#ifdef Q_WS_WIN + || (filename[0] && filename[1] == ':') || filename[0] == '\\' +#endif + ) + prefix = QString::fromLatin1( "" ); + else + prefix = directory; + + if ( prefix.length() ) { + if ( prefix[int(prefix.length()-1)] != '/' ) + prefix += QChar( '/' ); + } + + QString fname = filename; + QString realname; + QString delims; + delims = search_delimiters.isNull() ? + QString::fromLatin1( "_." ) : search_delimiters; + +#if defined(Q_WS_WIN) + qt_ntfs_permission_lookup--; +#endif + for ( ;; ) { + QFileInfo fi; + + realname = prefix + fname; + fi.setFile( realname ); + if ( fi.isReadable() ) + break; + + realname += suffix.isNull() ? QString::fromLatin1( ".qm" ) : suffix; + fi.setFile( realname ); + if ( fi.isReadable() ) + break; + + int rightmost = 0; + for ( int i = 0; i < (int)delims.length(); i++ ) { + int k = fname.findRev( delims[i] ); + if ( k > rightmost ) + rightmost = k; + } + + // no truncations? fail + if ( rightmost == 0 ) { +#if defined(Q_WS_WIN) + if (d->oldPermissionLookup != qt_ntfs_permission_lookup) + qt_ntfs_permission_lookup++; +#endif + return FALSE; + } + + fname.truncate( rightmost ); + } +#if defined(Q_WS_WIN) + if (d->oldPermissionLookup != qt_ntfs_permission_lookup) + qt_ntfs_permission_lookup++; +#endif + + // realname is now the fully qualified name of a readable file. + +#if defined(QT_USE_MMAP) + +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif +#ifndef MAP_FAILED +#define MAP_FAILED -1 +#endif + + int f; + + f = qt_open( QFile::encodeName(realname), O_RDONLY, 0666 ); + if ( f < 0 ) { + // qDebug( "can't open %s: %s", realname.ascii(), strerror( errno ) ); + return FALSE; + } + + struct stat st; + if ( fstat( f, &st ) ) { + // qDebug( "can't stat %s: %s", realname.ascii(), strerror( errno ) ); + return FALSE; + } + char * tmp; + tmp = (char*)mmap( 0, st.st_size, // any address, whole file + PROT_READ, // read-only memory + MAP_FILE | MAP_PRIVATE, // swap-backed map from file + f, 0 ); // from offset 0 of f + if ( !tmp || tmp == (char*)MAP_FAILED ) { + // qDebug( "can't mmap %s: %s", filename.ascii(), strerror( errno ) ); + return FALSE; + } + + ::close( f ); + + d->unmapPointer = tmp; + d->unmapLength = st.st_size; +#else + QFile f( realname ); + if ( !f.exists() ) + return FALSE; + d->unmapLength = f.size(); + d->unmapPointer = new char[d->unmapLength]; + bool ok = FALSE; + if ( f.open(IO_ReadOnly) ) { + ok = d->unmapLength == + (uint)f.readBlock( d->unmapPointer, d->unmapLength ); + f.close(); + } + if ( !ok ) { + delete [] d->unmapPointer; + d->unmapPointer = 0; + return FALSE; + } +#endif + + return do_load( (const uchar *) d->unmapPointer, d->unmapLength ); +} + +/*! + \overload + \fn bool QTranslator::load( const uchar *data, int len ) + + Loads the .qm file data \a data of length \a len into the + translator. Returns TRUE if the data is loaded successfully; + otherwise returns FALSE. + + The data is not copied. The caller must be able to guarantee that \a data + will not be deleted or modified. +*/ + +bool QTranslator::do_load( const uchar *data, int len ) +{ + if ( len < MagicLength || memcmp( data, magic, MagicLength ) != 0 ) { + clear(); + return FALSE; + } + + QByteArray array; + array.setRawData( (const char *) data, len ); + QDataStream s( array, IO_ReadOnly ); + bool ok = TRUE; + + s.device()->at( MagicLength ); + + Q_UINT8 tag = 0; + Q_UINT32 blockLen = 0; + s >> tag >> blockLen; + while ( tag && blockLen ) { + if ( (Q_UINT32) s.device()->at() + blockLen > (Q_UINT32) len ) { + ok = FALSE; + break; + } + + if ( tag == QTranslatorPrivate::Contexts && !d->contextArray ) { + d->contextArray = new QByteArray; + d->contextArray->setRawData( array.data() + s.device()->at(), + blockLen ); + } else if ( tag == QTranslatorPrivate::Hashes && !d->offsetArray ) { + d->offsetArray = new QByteArray; + d->offsetArray->setRawData( array.data() + s.device()->at(), + blockLen ); + } else if ( tag == QTranslatorPrivate::Messages && !d->messageArray ) { + d->messageArray = new QByteArray; + d->messageArray->setRawData( array.data() + s.device()->at(), + blockLen ); + } + + if ( !s.device()->at(s.device()->at() + blockLen) ) { + ok = FALSE; + break; + } + tag = 0; + blockLen = 0; + if ( !s.atEnd() ) + s >> tag >> blockLen; + } + array.resetRawData( (const char *) data, len ); + + if ( qApp && qApp->translators && qApp->translators->contains(this) ) + qApp->setReverseLayout( qt_detectRTLLanguage() ); + return ok; +} + +#ifndef QT_NO_TRANSLATION_BUILDER + +/*! + Saves this message file to \a filename, overwriting the previous + contents of \a filename. If \a mode is \c Everything (the + default), all the information is preserved. If \a mode is \c + Stripped, any information that is not necessary for findMessage() + is stripped away. + + \sa load() +*/ + +bool QTranslator::save( const QString & filename, SaveMode mode ) +{ + QFile f( filename ); + if ( f.open( IO_WriteOnly ) ) { + squeeze( mode ); + + QDataStream s( &f ); + s.writeRawBytes( (const char *)magic, MagicLength ); + Q_UINT8 tag; + + if ( d->offsetArray != 0 ) { + tag = (Q_UINT8) QTranslatorPrivate::Hashes; + Q_UINT32 oas = (Q_UINT32) d->offsetArray->size(); + s << tag << oas; + s.writeRawBytes( d->offsetArray->data(), oas ); + } + if ( d->messageArray != 0 ) { + tag = (Q_UINT8) QTranslatorPrivate::Messages; + Q_UINT32 mas = (Q_UINT32) d->messageArray->size(); + s << tag << mas; + s.writeRawBytes( d->messageArray->data(), mas ); + } + if ( d->contextArray != 0 ) { + tag = (Q_UINT8) QTranslatorPrivate::Contexts; + Q_UINT32 cas = (Q_UINT32) d->contextArray->size(); + s << tag << cas; + s.writeRawBytes( d->contextArray->data(), cas ); + } + return TRUE; + } + return FALSE; +} + +#endif + +/*! + Empties this translator of all contents. + + This function works with stripped translator files. +*/ + +void QTranslator::clear() +{ + if ( d->unmapPointer && d->unmapLength ) { +#if defined(QT_USE_MMAP) + munmap( d->unmapPointer, d->unmapLength ); +#else + delete [] d->unmapPointer; +#endif + d->unmapPointer = 0; + d->unmapLength = 0; + } + + if ( d->messageArray ) { + d->messageArray->resetRawData( d->messageArray->data(), + d->messageArray->size() ); + delete d->messageArray; + d->messageArray = 0; + } + if ( d->offsetArray ) { + d->offsetArray->resetRawData( d->offsetArray->data(), + d->offsetArray->size() ); + delete d->offsetArray; + d->offsetArray = 0; + } + if ( d->contextArray ) { + d->contextArray->resetRawData( d->contextArray->data(), + d->contextArray->size() ); + delete d->contextArray; + d->contextArray = 0; + } +#ifndef QT_NO_TRANSLATION_BUILDER + delete d->messages; + d->messages = 0; +#endif + + if ( qApp ) { + qApp->setReverseLayout( qt_detectRTLLanguage() ); + + QWidgetList *list = QApplication::topLevelWidgets(); + QWidgetListIt it( *list ); + QWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + if (!w->isDesktop()) + qApp->postEvent( w, new QEvent( QEvent::LanguageChange ) ); + } + delete list; + } +} + +#ifndef QT_NO_TRANSLATION_BUILDER + +/*! + Converts this message file to the compact format used to store + message files on disk. + + You should never need to call this directly; save() and other + functions call it as necessary. \a mode is for internal use. + + \sa save() unsqueeze() +*/ + +void QTranslator::squeeze( SaveMode mode ) +{ + if ( !d->messages ) { + if ( mode == Stripped ) + unsqueeze(); + else + return; + } + + QMap<QTranslatorMessage, void *> * messages = d->messages; + + d->messages = 0; + clear(); + + d->messageArray = new QByteArray; + d->offsetArray = new QByteArray; + + QMap<QTranslatorPrivate::Offset, void *> offsets; + + QDataStream ms( *d->messageArray, IO_WriteOnly ); + QMap<QTranslatorMessage, void *>::Iterator it = messages->begin(), next; + int cpPrev = 0, cpNext = 0; + for ( it = messages->begin(); it != messages->end(); ++it ) { + cpPrev = cpNext; + next = it; + ++next; + if ( next == messages->end() ) + cpNext = 0; + else + cpNext = (int) it.key().commonPrefix( next.key() ); + offsets.replace( QTranslatorPrivate::Offset(it.key(), + ms.device()->at()), (void*)0 ); + it.key().write( ms, mode == Stripped, + (QTranslatorMessage::Prefix) QMAX(cpPrev, cpNext + 1) ); + } + + d->offsetArray->resize( 0 ); + QMap<QTranslatorPrivate::Offset, void *>::Iterator offset; + offset = offsets.begin(); + QDataStream ds( *d->offsetArray, IO_WriteOnly ); + while ( offset != offsets.end() ) { + QTranslatorPrivate::Offset k = offset.key(); + ++offset; + ds << (Q_UINT32)k.h << (Q_UINT32)k.o; + } + + if ( mode == Stripped ) { + QAsciiDict<int> contextSet( 1511 ); + int baudelaire; + + for ( it = messages->begin(); it != messages->end(); ++it ) + contextSet.replace( it.key().context(), &baudelaire ); + + Q_UINT16 hTableSize; + if ( contextSet.count() < 200 ) + hTableSize = ( contextSet.count() < 60 ) ? 151 : 503; + else if ( contextSet.count() < 2500 ) + hTableSize = ( contextSet.count() < 750 ) ? 1511 : 5003; + else + hTableSize = 15013; + + QIntDict<char> hDict( hTableSize ); + QAsciiDictIterator<int> c = contextSet; + while ( c.current() != 0 ) { + hDict.insert( (long) (elfHash(c.currentKey()) % hTableSize), + c.currentKey() ); + ++c; + } + + /* + The contexts found in this translator are stored in a hash + table to provide fast lookup. The context array has the + following format: + + Q_UINT16 hTableSize; + Q_UINT16 hTable[hTableSize]; + Q_UINT8 contextPool[...]; + + The context pool stores the contexts as Pascal strings: + + Q_UINT8 len; + Q_UINT8 data[len]; + + Let's consider the look-up of context "FunnyDialog". A + hash value between 0 and hTableSize - 1 is computed, say h. + If hTable[h] is 0, "FunnyDialog" is not covered by this + translator. Else, we check in the contextPool at offset + 2 * hTable[h] to see if "FunnyDialog" is one of the + contexts stored there, until we find it or we meet the + empty string. + */ + d->contextArray = new QByteArray; + d->contextArray->resize( 2 + (hTableSize << 1) ); + QDataStream t( *d->contextArray, IO_WriteOnly ); + Q_UINT16 *hTable = new Q_UINT16[hTableSize]; + memset( hTable, 0, hTableSize * sizeof(Q_UINT16) ); + + t << hTableSize; + t.device()->at( 2 + (hTableSize << 1) ); + t << (Q_UINT16) 0; // the entry at offset 0 cannot be used + uint upto = 2; + + for ( int i = 0; i < hTableSize; i++ ) { + const char *con = hDict.find( i ); + if ( con == 0 ) { + hTable[i] = 0; + } else { + hTable[i] = (Q_UINT16) ( upto >> 1 ); + do { + uint len = (uint) qstrlen( con ); + len = QMIN( len, 255 ); + t << (Q_UINT8) len; + t.writeRawBytes( con, len ); + upto += 1 + len; + hDict.remove( i ); + } while ( (con = hDict.find(i)) != 0 ); + do { + t << (Q_UINT8) 0; // empty string (at least one) + upto++; + } while ( (upto & 0x1) != 0 ); // offsets have to be even + } + } + t.device()->at( 2 ); + for ( int j = 0; j < hTableSize; j++ ) + t << hTable[j]; + delete [] hTable; + + if ( upto > 131072 ) { + qWarning( "QTranslator::squeeze: Too many contexts" ); + delete d->contextArray; + d->contextArray = 0; + } + } + delete messages; +} + + +/*! + Converts this message file into an easily modifiable data + structure, less compact than the format used in the files. + + You should never need to call this function; it is called by + insert() and friends as necessary. + + \sa squeeze() +*/ + +void QTranslator::unsqueeze() +{ + if ( d->messages ) + return; + + d->messages = new QMap<QTranslatorMessage, void *>; + if ( !d->messageArray ) + return; + + QDataStream s( *d->messageArray, IO_ReadOnly ); + for ( ;; ) { + QTranslatorMessage m( s ); + if ( m.hash() == 0 ) + break; + d->messages->insert( m, (void *) 0 ); + } +} + + +/*! + Returns TRUE if this message file contains a message with the key + (\a context, \a sourceText, \a comment); otherwise returns FALSE. + + This function works with stripped translator files. + + (This is is a one-liner that calls findMessage().) +*/ + +bool QTranslator::contains( const char* context, const char* sourceText, + const char* comment ) const +{ + return !findMessage( context, sourceText, comment ).translation().isNull(); +} + + +/*! + Inserts \a message into this message file. + + This function does \e not work with stripped translator files. It + may appear to, but that is not dependable. + + \sa remove() +*/ + +void QTranslator::insert( const QTranslatorMessage& message ) +{ + unsqueeze(); + d->messages->remove( message ); // safer + d->messages->insert( message, (void *) 0 ); +} + + +/*! + \fn void QTranslator::insert( const char *, const char *, const QString & ) + \overload + \obsolete +*/ + +/*! + Removes \a message from this translator. + + This function works with stripped translator files. + + \sa insert() +*/ + +void QTranslator::remove( const QTranslatorMessage& message ) +{ + unsqueeze(); + d->messages->remove( message ); +} + + +/*! + \fn void QTranslator::remove( const char *, const char * ) + \overload + \obsolete + + Removes the translation associated to the key (\a context, \a sourceText, + "") from this translator. +*/ +#endif + +/*! + \fn QString QTranslator::find( const char*, const char*, const char* ) const + \obsolete + + Please use findMessage() instead. + + Returns the translation for the key (\a context, \a sourceText, + \a comment) or QString::null if there is none in this translator. +*/ + +/*! Returns the QTranslatorMessage for the key + (\a context, \a sourceText, \a comment). If none is found, + also tries (\a context, \a sourceText, ""). +*/ + +QTranslatorMessage QTranslator::findMessage( const char* context, + const char* sourceText, + const char* comment ) const +{ + if ( context == 0 ) + context = ""; + if ( sourceText == 0 ) + sourceText = ""; + if ( comment == 0 ) + comment = ""; + +#ifndef QT_NO_TRANSLATION_BUILDER + if ( d->messages ) { + QMap<QTranslatorMessage, void *>::ConstIterator it; + + it = d->messages->find( QTranslatorMessage(context, sourceText, + comment) ); + if ( it != d->messages->end() ) + return it.key(); + + if ( comment[0] ) { + it = d->messages->find( QTranslatorMessage(context, sourceText, + "") ); + if ( it != d->messages->end() ) + return it.key(); + } + return QTranslatorMessage(); + } +#endif + + if ( !d->offsetArray ) + return QTranslatorMessage(); + + /* + Check if the context belongs to this QTranslator. If many translators are + installed, this step is necessary. + */ + if ( d->contextArray ) { + Q_UINT16 hTableSize = 0; + QDataStream t( *d->contextArray, IO_ReadOnly ); + t >> hTableSize; + uint g = elfHash( context ) % hTableSize; + t.device()->at( 2 + (g << 1) ); + Q_UINT16 off; + t >> off; + if ( off == 0 ) + return QTranslatorMessage(); + t.device()->at( 2 + (hTableSize << 1) + (off << 1) ); + + Q_UINT8 len; + char con[256]; + for ( ;; ) { + t >> len; + if ( len == 0 ) + return QTranslatorMessage(); + t.readRawBytes( con, len ); + con[len] = '\0'; + if ( qstrcmp(con, context) == 0 ) + break; + } + } + + size_t numItems = d->offsetArray->size() / ( 2 * sizeof(Q_UINT32) ); + if ( !numItems ) + return QTranslatorMessage(); + + if ( systemWordSize == 0 ) + qSysInfo( &systemWordSize, &systemBigEndian ); + + for ( ;; ) { + Q_UINT32 h = elfHash( QCString(sourceText) + comment ); + + char *r = (char *) bsearch( &h, d->offsetArray->data(), numItems, + 2 * sizeof(Q_UINT32), + systemBigEndian ? cmp_uint32_big + : cmp_uint32_little ); + if ( r != 0 ) { + // go back on equal key + while ( r != d->offsetArray->data() && + cmp_uint32_big(r - 8, r) == 0 ) + r -= 8; + + QDataStream s( *d->offsetArray, IO_ReadOnly ); + s.device()->at( r - d->offsetArray->data() ); + + Q_UINT32 rh, ro; + s >> rh >> ro; + + QDataStream ms( *d->messageArray, IO_ReadOnly ); + while ( rh == h ) { + ms.device()->at( ro ); + QTranslatorMessage m( ms ); + if ( match(m.context(), context) + && match(m.sourceText(), sourceText) + && match(m.comment(), comment) ) + return m; + if ( s.atEnd() ) + break; + s >> rh >> ro; + } + } + if ( !comment[0] ) + break; + comment = ""; + } + return QTranslatorMessage(); +} + +/*! + Returns TRUE if this translator is empty, otherwise returns FALSE. + This function works with stripped and unstripped translation files. +*/ +bool QTranslator::isEmpty() const +{ + return !( d->unmapPointer || d->unmapLength || d->messageArray || + d->offsetArray || d->contextArray +#ifndef QT_NO_TRANSLATION_BUILDER + || (d->messages && d->messages->count()) +#endif + ); +} + + +#ifndef QT_NO_TRANSLATION_BUILDER + +/*! + Returns a list of the messages in the translator. This function is + rather slow. Because it is seldom called, it's optimized for + simplicity and small size, rather than speed. + + If you want to iterate over the list, you should iterate over a + copy, e.g. + \code + QValueList<QTranslatorMessage> list = myTranslator.messages(); + QValueList<QTranslatorMessage>::Iterator it = list.begin(); + while ( it != list.end() ) { + process_message( *it ); + ++it; + } + \endcode +*/ + +QValueList<QTranslatorMessage> QTranslator::messages() const +{ + if ( d->messages ) + return d->messages->keys(); + + ((QTranslator *) this)->unsqueeze(); + QValueList<QTranslatorMessage> result = d->messages->keys(); + delete d->messages; + d->messages = 0; + return result; +} + +#endif + +/*! + \class QTranslatorMessage + + \brief The QTranslatorMessage class contains a translator message and its + properties. + + \ingroup i18n + \ingroup environment + + This class is of no interest to most applications. It is useful + for translation tools such as \link linguist-manual.book Qt + Linguist\endlink. It is provided simply to make the API complete + and regular. + + For a QTranslator object, a lookup key is a triple (\e context, \e + {source text}, \e comment) that uniquely identifies a message. An + extended key is a quadruple (\e hash, \e context, \e {source + text}, \e comment), where \e hash is computed from the source text + and the comment. Unless you plan to read and write messages + yourself, you need not worry about the hash value. + + QTranslatorMessage stores this triple or quadruple and the relevant + translation if there is any. + + \sa QTranslator +*/ + +/*! + Constructs a translator message with the extended key (0, 0, 0, 0) + and QString::null as translation. +*/ + +QTranslatorMessage::QTranslatorMessage() + : h( 0 ), cx( 0 ), st( 0 ), cm( 0 ) +{ +} + + +/*! + Constructs an translator message with the extended key (\e h, \a + context, \a sourceText, \a comment), where \e h is computed from + \a sourceText and \a comment, and possibly with a \a translation. +*/ + +QTranslatorMessage::QTranslatorMessage( const char * context, + const char * sourceText, + const char * comment, + const QString& translation ) + : cx( context ), st( sourceText ), cm( comment ), tn( translation ) +{ + // 0 means we don't know, "" means empty + if ( cx == (const char*)0 ) + cx = ""; + if ( st == (const char*)0 ) + st = ""; + if ( cm == (const char*)0 ) + cm = ""; + h = elfHash( st + cm ); +} + + +/*! + Constructs a translator message read from the \a stream. The + resulting message may have any combination of content. + + \sa QTranslator::save() +*/ + +QTranslatorMessage::QTranslatorMessage( QDataStream & stream ) + : cx( 0 ), st( 0 ), cm( 0 ) +{ + QString str16; + char tag; + Q_UINT8 obs1; + + for ( ;; ) { + tag = 0; + if ( !stream.atEnd() ) + stream.readRawBytes( &tag, 1 ); + switch( (Tag)tag ) { + case Tag_End: + if ( h == 0 ) + h = elfHash( st + cm ); + return; + case Tag_SourceText16: // obsolete + stream >> str16; + st = str16.latin1(); + break; + case Tag_Translation: + stream >> tn; + break; + case Tag_Context16: // obsolete + stream >> str16; + cx = str16.latin1(); + break; + case Tag_Hash: + stream >> h; + break; + case Tag_SourceText: + stream >> st; + break; + case Tag_Context: + stream >> cx; + if ( cx == "" ) // for compatibility + cx = 0; + break; + case Tag_Comment: + stream >> cm; + break; + case Tag_Obsolete1: // obsolete + stream >> obs1; + break; + default: + h = 0; + st = 0; + cx = 0; + cm = 0; + tn = QString::null; + return; + } + } +} + + +/*! + Constructs a copy of translator message \a m. +*/ + +QTranslatorMessage::QTranslatorMessage( const QTranslatorMessage & m ) + : cx( m.cx ), st( m.st ), cm( m.cm ), tn( m.tn ) +{ + h = m.h; +} + + +/*! + Assigns message \a m to this translator message and returns a + reference to this translator message. +*/ + +QTranslatorMessage & QTranslatorMessage::operator=( + const QTranslatorMessage & m ) +{ + h = m.h; + cx = m.cx; + st = m.st; + cm = m.cm; + tn = m.tn; + return *this; +} + + +/*! + \fn uint QTranslatorMessage::hash() const + + Returns the hash value used internally to represent the lookup + key. This value is zero only if this translator message was + constructed from a stream containing invalid data. + + The hashing function is unspecified, but it will remain unchanged + in future versions of Qt. +*/ + +/*! + \fn const char *QTranslatorMessage::context() const + + Returns the context for this message (e.g. "MyDialog"). + + \warning This may return 0 if the QTranslator object is stripped + (compressed). +*/ + +/*! + \fn const char *QTranslatorMessage::sourceText() const + + Returns the source text of this message (e.g. "&Save"). + + \warning This may return 0 if the QTranslator object is stripped + (compressed). +*/ + +/*! + \fn const char *QTranslatorMessage::comment() const + + Returns the comment for this message (e.g. "File|Save"). + + \warning This may return 0 if the QTranslator object is stripped + (compressed). +*/ + +/*! + \fn void QTranslatorMessage::setTranslation( const QString & translation ) + + Sets the translation of the source text to \a translation. + + \sa translation() +*/ + +/*! + \fn QString QTranslatorMessage::translation() const + + Returns the translation of the source text (e.g., "&Sauvegarder"). + + \sa setTranslation() +*/ + +/*! + \enum QTranslatorMessage::Prefix + + Let (\e h, \e c, \e s, \e m) be the extended key. The possible + prefixes are + + \value NoPrefix no prefix + \value Hash only (\e h) + \value HashContext only (\e h, \e c) + \value HashContextSourceText only (\e h, \e c, \e s) + \value HashContextSourceTextComment the whole extended key, (\e + h, \e c, \e s, \e m) + + \sa write() commonPrefix() +*/ + +/*! + Writes this translator message to the \a stream. If \a strip is + FALSE (the default), all the information in the message is + written. If \a strip is TRUE, only the part of the extended key + specified by \a prefix is written with the translation (\c + HashContextSourceTextComment by default). + + \sa commonPrefix() +*/ + +void QTranslatorMessage::write( QDataStream & stream, bool strip, + Prefix prefix ) const +{ + char tag; + + tag = (char)Tag_Translation; + stream.writeRawBytes( &tag, 1 ); + stream << tn; + + bool mustWriteHash = TRUE; + if ( !strip ) + prefix = HashContextSourceTextComment; + + switch ( prefix ) { + case HashContextSourceTextComment: + tag = (char)Tag_Comment; + stream.writeRawBytes( &tag, 1 ); + stream << cm; + // fall through + case HashContextSourceText: + tag = (char)Tag_SourceText; + stream.writeRawBytes( &tag, 1 ); + stream << st; + // fall through + case HashContext: + tag = (char)Tag_Context; + stream.writeRawBytes( &tag, 1 ); + stream << cx; + // fall through + default: + if ( mustWriteHash ) { + tag = (char)Tag_Hash; + stream.writeRawBytes( &tag, 1 ); + stream << h; + } + } + + tag = (char)Tag_End; + stream.writeRawBytes( &tag, 1 ); +} + + +/*! + Returns the widest lookup prefix that is common to this translator + message and to message \a m. + + For example, if the extended key is for this message is (71, + "PrintDialog", "Yes", "Print?") and that for \a m is (71, + "PrintDialog", "No", "Print?"), this function returns \c + HashContext. + + \sa write() +*/ + +QTranslatorMessage::Prefix QTranslatorMessage::commonPrefix( + const QTranslatorMessage& m ) const +{ + if ( h != m.h ) + return NoPrefix; + if ( cx != m.cx ) + return Hash; + if ( st != m.st ) + return HashContext; + if ( cm != m.cm ) + return HashContextSourceText; + return HashContextSourceTextComment; +} + + +/*! + Returns TRUE if the extended key of this object is equal to that of + \a m; otherwise returns FALSE. +*/ + +bool QTranslatorMessage::operator==( const QTranslatorMessage& m ) const +{ + return h == m.h && cx == m.cx && st == m.st && cm == m.cm; +} + + +/*! + \fn bool QTranslatorMessage::operator!=( const QTranslatorMessage& m ) const + + Returns TRUE if the extended key of this object is different from + that of \a m; otherwise returns FALSE. +*/ + + +/*! + Returns TRUE if the extended key of this object is + lexicographically before than that of \a m; otherwise returns + FALSE. +*/ + +bool QTranslatorMessage::operator<( const QTranslatorMessage& m ) const +{ + return h != m.h ? h < m.h + : ( cx != m.cx ? cx < m.cx + : (st != m.st ? st < m.st : cm < m.cm) ); +} + + +/*! + \fn bool QTranslatorMessage::operator<=( const QTranslatorMessage& m ) const + + Returns TRUE if the extended key of this object is + lexicographically before that of \a m or if they are equal; + otherwise returns FALSE. +*/ + +/*! + \fn bool QTranslatorMessage::operator>( const QTranslatorMessage& m ) const + + Returns TRUE if the extended key of this object is + lexicographically after that of \a m; otherwise returns FALSE. +*/ + +/*! + \fn bool QTranslatorMessage::operator>=( const QTranslatorMessage& m ) const + + Returns TRUE if the extended key of this object is + lexicographically after that of \a m or if they are equal; + otherwise returns FALSE. +*/ + +#endif // QT_NO_TRANSLATION diff --git a/src/kernel/qtranslator.h b/src/kernel/qtranslator.h new file mode 100644 index 0000000..28c3345 --- /dev/null +++ b/src/kernel/qtranslator.h @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Definition of the translator class +** +** Created : 980906 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + + +#ifndef QTRANSLATOR_H +#define QTRANSLATOR_H + +#ifndef QT_H +#include "qobject.h" +#include "qvaluelist.h" +#endif // QT_H + +#ifndef QT_NO_TRANSLATION + +class QTranslatorPrivate; + +class Q_EXPORT QTranslatorMessage +{ +public: + QTranslatorMessage(); + QTranslatorMessage( const char * context, + const char * sourceText, + const char * comment, + const QString& translation = QString::null ); + QTranslatorMessage( QDataStream & ); + QTranslatorMessage( const QTranslatorMessage & m ); + + QTranslatorMessage & operator=( const QTranslatorMessage & m ); + + uint hash() const { return h; } + const char *context() const { return cx; } + const char *sourceText() const { return st; } + const char *comment() const { return cm; } + + void setTranslation( const QString & translation ) { tn = translation; } + QString translation() const { return tn; } + + enum Prefix { NoPrefix, Hash, HashContext, HashContextSourceText, + HashContextSourceTextComment }; + void write( QDataStream & s, bool strip = FALSE, + Prefix prefix = HashContextSourceTextComment ) const; + Prefix commonPrefix( const QTranslatorMessage& ) const; + + bool operator==( const QTranslatorMessage& m ) const; + bool operator!=( const QTranslatorMessage& m ) const + { return !operator==( m ); } + bool operator<( const QTranslatorMessage& m ) const; + bool operator<=( const QTranslatorMessage& m ) const + { return !m.operator<( *this ); } + bool operator>( const QTranslatorMessage& m ) const + { return m.operator<( *this ); } + bool operator>=( const QTranslatorMessage& m ) const + { return !operator<( m ); } + +private: + uint h; + QCString cx; + QCString st; + QCString cm; + QString tn; + + enum Tag { Tag_End = 1, Tag_SourceText16, Tag_Translation, Tag_Context16, + Tag_Hash, Tag_SourceText, Tag_Context, Tag_Comment, + Tag_Obsolete1 }; +}; + + +class Q_EXPORT QTranslator: public QObject +{ + Q_OBJECT +public: + QTranslator( QObject * parent = 0, const char * name = 0 ); + ~QTranslator(); + +#ifndef QT_NO_COMPAT + QString find( const char *context, const char *sourceText, const char * comment = 0 ) const { + return findMessage( context, sourceText, comment ).translation(); + } +#endif + virtual QTranslatorMessage findMessage( const char *, const char *, + const char * = 0 ) const; + + bool load( const QString & filename, + const QString & directory = QString::null, + const QString & search_delimiters = QString::null, + const QString & suffix = QString::null ); + bool load( const uchar *data, int len ) { + clear(); + return do_load( data, len ); + } + + void clear(); + +#ifndef QT_NO_TRANSLATION_BUILDER + enum SaveMode { Everything, Stripped }; + + bool save( const QString & filename, SaveMode mode = Everything ); + + void insert( const QTranslatorMessage& ); + void insert( const char *context, const char *sourceText, const QString &translation ) { + insert( QTranslatorMessage(context, sourceText, "", translation) ); + } + void remove( const QTranslatorMessage& ); + void remove( const char *context, const char *sourceText ) { + remove( QTranslatorMessage(context, sourceText, "") ); + } + bool contains( const char *, const char *, const char * comment = 0 ) const; + + void squeeze( SaveMode = Everything ); + void unsqueeze(); + + QValueList<QTranslatorMessage> messages() const; +#endif + + bool isEmpty() const; + +private: +#if defined(Q_DISABLE_COPY) + QTranslator( const QTranslator & ); + QTranslator &operator=( const QTranslator & ); +#endif + + bool do_load( const uchar *data, int len ); + + QTranslatorPrivate * d; +}; + +#endif // QT_NO_TRANSLATION + +#endif diff --git a/src/kernel/qucomextra.cpp b/src/kernel/qucomextra.cpp new file mode 100644 index 0000000..427c29a --- /dev/null +++ b/src/kernel/qucomextra.cpp @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Implementation of extra QUcom classes +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qucomextra_p.h" +#include <qvariant.h> + + +#ifndef QT_NO_VARIANT +// 6dc75d58-a1d9-4417-b591-d45c63a3a4ea +const QUuid TID_QUType_QVariant( 0x6dc75d58, 0xa1d9, 0x4417, 0xb5, 0x91, 0xd4, 0x5c, 0x63, 0xa3, 0xa4, 0xea ); +QUType_QVariant static_QUType_QVariant; + +const QUuid *QUType_QVariant::uuid() const { return &TID_QUType_QVariant; } +const char *QUType_QVariant::desc() const { return "QVariant"; } + +void QUType_QVariant::set( QUObject *o, const QVariant& v ) +{ + o->payload.ptr = new QVariant( v ); + o->type = this; +} + +QVariant &QUType_QVariant::get( QUObject * o ) +{ + return *(QVariant*)o->payload.ptr; +} + +bool QUType_QVariant::canConvertFrom( QUObject *o, QUType *t ) +{ + if ( isEqual( o->type, &static_QUType_QString ) + || isEqual( o->type, &static_QUType_int ) + || isEqual( o->type, &static_QUType_bool ) + || isEqual( o->type, &static_QUType_double ) + || isEqual( o->type, &static_QUType_charstar ) ) + return TRUE; + return t->canConvertTo( o, this ); +} + +bool QUType_QVariant::canConvertTo( QUObject * /*o*/, QUType * /*t*/ ) +{ + return FALSE; +} + +bool QUType_QVariant::convertFrom( QUObject *o, QUType *t ) +{ + QVariant *var = 0; + if ( isEqual( o->type, &static_QUType_QString ) ) + var = new QVariant( static_QUType_QString.get( o ) ); + else if ( isEqual( o->type, &static_QUType_int ) ) + var = new QVariant( static_QUType_int.get( o ) ); + else if ( isEqual( o->type, &static_QUType_bool ) ) + var = new QVariant( static_QUType_bool.get( o ), 42 ); + else if ( isEqual( o->type, &static_QUType_double ) ) + var = new QVariant( static_QUType_double.get( o ) ); + else if ( isEqual( o->type, &static_QUType_charstar ) ) + var = new QVariant( static_QUType_charstar.get( o ) ); + else + return t->convertTo( o, this ); + + o->type->clear( o ); + o->payload.ptr = var; + o->type = this; + return TRUE; +} + +bool QUType_QVariant::convertTo( QUObject * /*o*/, QUType * /*t*/ ) +{ + return FALSE; +} + +void QUType_QVariant::clear( QUObject *o ) +{ + delete (QVariant*)o->payload.ptr; + o->payload.ptr = 0; +} + +int QUType_QVariant::serializeTo( QUObject *, QUBuffer * ) +{ + return 0; +} + +int QUType_QVariant::serializeFrom( QUObject *, QUBuffer * ) +{ + return 0; +} + + +#endif + +const QUuid TID_QUType_varptr( 0x8d48b3a8, 0xbd7f, 0x11d5, 0x8d, 0x74, 0x00, 0xc0, 0xf0, 0x3b, 0xc0, 0xf3 ); +QUType_varptr static_QUType_varptr; +const QUuid *QUType_varptr::uuid() const { return &TID_QUType_varptr; } +const char *QUType_varptr::desc() const { return "varptr"; } + +void QUType_varptr::set( QUObject *o, const void* v ) +{ + o->payload.ptr = (void*) v; + o->type = this; +} + +bool QUType_varptr::canConvertFrom( QUObject *o, QUType *t ) +{ + if ( isEqual( t, &static_QUType_ptr ) ) + return TRUE; + return t->canConvertTo( o, this ); +} + +bool QUType_varptr::canConvertTo( QUObject *, QUType * t) +{ + return isEqual( t, &static_QUType_ptr ); +} + +bool QUType_varptr::convertFrom( QUObject *o, QUType *t ) +{ + if ( isEqual( t, &static_QUType_ptr ) ) + ; + else + return t->convertTo( o, this ); + + o->type = this; + return TRUE; +} + +bool QUType_varptr::convertTo( QUObject *o, QUType * t) +{ + if ( isEqual( t, &static_QUType_ptr ) ) { + o->type = &static_QUType_ptr; + return TRUE; + } + return FALSE; +} + +int QUType_varptr::serializeTo( QUObject *, QUBuffer * ) +{ + return 0; +} + +int QUType_varptr::serializeFrom( QUObject *, QUBuffer * ) +{ + return 0; +} diff --git a/src/kernel/qucomextra_p.h b/src/kernel/qucomextra_p.h new file mode 100644 index 0000000..d100e66 --- /dev/null +++ b/src/kernel/qucomextra_p.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Definition of extra QUcom classes +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QUCOMEXTRA_P_H +#define QUCOMEXTRA_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. This header file may +// change from version to version without notice, or even be +// removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include <private/qucom_p.h> +#endif // QT_H + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +class QVariant; + +#ifndef QT_NO_VARIANT +// 6dc75d58-a1d9-4417-b591-d45c63a3a4ea +extern const QUuid TID_QUType_QVariant; + +struct Q_EXPORT QUType_QVariant : public QUType +{ + const QUuid *uuid() const; + const char *desc() const; + + void set( QUObject *, const QVariant & ); + QVariant &get( QUObject * o ); + + bool canConvertFrom( QUObject *, QUType * ); + bool canConvertTo( QUObject *, QUType * ); + bool convertFrom( QUObject *, QUType * ); + bool convertTo( QUObject *, QUType * ); + void clear( QUObject * ); + int serializeTo( QUObject *, QUBuffer * ); + int serializeFrom( QUObject *, QUBuffer * ); +}; +extern Q_EXPORT QUType_QVariant static_QUType_QVariant; +#endif //QT_NO_VARIANT + + +// {0x8d48b3a8, 0xbd7f, 0x11d5, 0x8d, 0x74, 0x00, 0xc0, 0xf0, 0x3b, 0xc0, 0xf3 } +extern Q_EXPORT const QUuid TID_QUType_varptr; +struct Q_EXPORT QUType_varptr : public QUType +{ + const QUuid *uuid() const; + const char *desc() const; + + void set( QUObject *, const void* ); + void* &get( QUObject * o ) { return o->payload.ptr; } + bool canConvertFrom( QUObject *, QUType * ); + bool canConvertTo( QUObject *, QUType * ); + bool convertFrom( QUObject *, QUType * ); + bool convertTo( QUObject *, QUType * ); + void clear( QUObject * ) {} + int serializeTo( QUObject *, QUBuffer * ); + int serializeFrom( QUObject *, QUBuffer * ); +}; +extern Q_EXPORT QUType_varptr static_QUType_varptr; + + +#endif // QUCOMEXTRA_P_H + diff --git a/src/kernel/qurl.cpp b/src/kernel/qurl.cpp new file mode 100644 index 0000000..fd377ad --- /dev/null +++ b/src/kernel/qurl.cpp @@ -0,0 +1,1345 @@ +/**************************************************************************** +** +** Implementation of QUrl class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qurl.h" + +#ifndef QT_NO_URL + +#include "qdir.h" + +#include <stdlib.h> + +class QUrlPrivate +{ +public: + QString protocol; + QString user; + QString pass; + QString host; + QString path, cleanPath; + QString refEncoded; + QString queryEncoded; + bool isValid; + int port; + bool cleanPathDirty; +}; + +/*! + Replaces backslashes with slashes and removes multiple occurrences + of slashes or backslashes if \c allowMultiple is FALSE. +*/ + +static void slashify( QString& s, bool allowMultiple = TRUE ) +{ + bool justHadSlash = FALSE; + for ( int i = 0; i < (int)s.length(); i++ ) { + if ( !allowMultiple && justHadSlash && + ( s[ i ] == '/' || s[ i ] == '\\' ) ) { + s.remove( i, 1 ); + --i; + continue; + } + if ( s[ i ] == '\\' ) + s[ i ] = '/'; +#if defined (Q_WS_MAC9) + if ( s[ i ] == ':' && (i == (int)s.length()-1 || s[ i + 1 ] != '/' ) ) //mac colon's go away, unless after a protocol + s[ i ] = '/'; +#endif + if ( s[ i ] == '/' ) + justHadSlash = TRUE; + else + justHadSlash = FALSE; + } +} + + + +/*! + \class QUrl qurl.h + + \brief The QUrl class provides a URL parser and simplifies working with URLs. +\if defined(commercial) + It is part of the <a href="commercialeditions.html">Qt Enterprise Edition</a>. +\endif + + \ingroup io + \ingroup misc + \mainclass + + \module network + + The QUrl class is provided for simple work with URLs. It can + parse, decode, encode, etc. + + QUrl works with the decoded path and encoded query in turn. + + Example: + + <tt>http://www.trolltech.com:80/cgi-bin/test%20me.pl?cmd=Hello%20you</tt> + + \table + \header \i Function \i Returns + \row \i \l protocol() \i "http" + \row \i \l host() \i "www.trolltech.com" + \row \i \l port() \i 80 + \row \i \l path() \i "/cgi-bin/test me.pl" + \row \i \l fileName() \i "test me.pl" + \row \i \l query() \i "cmd=Hello%20you" + \endtable + + Example: + + <tt>http://doc.trolltech.com/qdockarea.html#lines</tt> + + \table + \header \i Function \i Returns + \row \i \l protocol() \i "http" + \row \i \l host() \i "doc.trolltech.com" + \row \i \l fileName() \i "qdockarea.html" + \row \i \l ref() \i "lines" + \endtable + + The individual parts of a URL can be set with setProtocol(), + setHost(), setPort(), setPath(), setFileName(), setRef() and + setQuery(). A URL could contain, for example, an ftp address which + requires a user name and password; these can be set with setUser() + and setPassword(). + + Because path is always encoded internally you must not use "%00" + in the path, although this is okay (but not recommended) for the + query. + + QUrl is normally used like this: + + \code + QUrl url( "http://www.trolltech.com" ); + // or + QUrl url( "file:/home/myself/Mail", "Inbox" ); + \endcode + + You can then access and manipulate the various parts of the URL. + + To make it easy to work with QUrls and QStrings, QUrl implements + the necessary cast and assignment operators so you can do + following: + + \code + QUrl url( "http://www.trolltech.com" ); + QString s = url; + // or + QString s( "http://www.trolltech.com" ); + QUrl url( s ); + \endcode + + Use the static functions, encode() and decode() to encode or + decode a URL in a string. (They operate on the string in-place.) + The isRelativeUrl() static function returns TRUE if the given + string is a relative URL. + + If you want to use a URL to work on a hierarchical structure (e.g. + a local or remote filesystem), you might want to use the subclass + QUrlOperator. + + \sa QUrlOperator +*/ + + +/*! + Constructs an empty URL that is invalid. +*/ + +QUrl::QUrl() +{ + d = new QUrlPrivate; + d->isValid = FALSE; + d->port = -1; + d->cleanPathDirty = TRUE; +} + +/*! + Constructs a URL by parsing the string \a url. + + If you pass a string like "/home/qt", the "file" protocol is + assumed. +*/ + +QUrl::QUrl( const QString& url ) +{ + d = new QUrlPrivate; + d->protocol = "file"; + d->port = -1; + parse( url ); +} + +/*! + Copy constructor. Copies the data of \a url. +*/ + +QUrl::QUrl( const QUrl& url ) +{ + d = new QUrlPrivate; + *d = *url.d; +} + +/*! + Returns TRUE if \a url is relative; otherwise returns FALSE. +*/ + +bool QUrl::isRelativeUrl( const QString &url ) +{ + int colon = url.find( ":" ); + int slash = url.find( "/" ); + + return ( slash != 0 && ( colon == -1 || ( slash != -1 && colon > slash ) ) ); +} + +/*! + Constructs an URL taking \a url as the base (context) and + \a relUrl as a relative URL to \a url. If \a relUrl is not relative, + \a relUrl is taken as the new URL. + + For example, the path of + \code + QUrl url( "ftp://ftp.trolltech.com/qt/source", "qt-2.1.0.tar.gz" ); + \endcode + will be "/qt/srource/qt-2.1.0.tar.gz". + + On the other hand, + \code + QUrl url( "ftp://ftp.trolltech.com/qt/source", "/usr/local" ); + \endcode + will result in a new URL, "ftp://ftp.trolltech.com/usr/local", + because "/usr/local" isn't relative. + + Similarly, + \code + QUrl url( "ftp://ftp.trolltech.com/qt/source", "file:/usr/local" ); + \endcode + will result in a new URL, with "/usr/local" as the path + and "file" as the protocol. + + Normally it is expected that the path of \a url points to a + directory, even if the path has no slash at the end. But if you + want the constructor to handle the last part of the path as a file + name if there is no slash at the end, and to let it be replaced by + the file name of \a relUrl (if it contains one), set \a checkSlash + to TRUE. +*/ + +QUrl::QUrl( const QUrl& url, const QString& relUrl, bool checkSlash ) +{ + d = new QUrlPrivate; + QString rel = relUrl; + slashify( rel ); + + QUrl urlTmp( url ); + if ( !urlTmp.isValid() ) { + urlTmp.reset(); + } + if ( isRelativeUrl( rel ) ) { + if ( rel[ 0 ] == '#' ) { + *this = urlTmp; + rel.remove( (uint)0, 1 ); + decode( rel ); + setRef( rel ); + } else if ( rel[ 0 ] == '?' ) { + *this = urlTmp; + rel.remove( (uint)0, 1 ); + setQuery( rel ); + } else { + decode( rel ); + *this = urlTmp; + setRef( QString::null ); + if ( checkSlash && d->cleanPath[(int)path().length()-1] != '/' ) { + if ( isRelativeUrl( path() ) ) + setEncodedPathAndQuery( rel ); + else + setFileName( rel ); + } else { + QString p = urlTmp.path(); + if ( p.isEmpty() ) { + // allow URLs like "file:foo" + if ( !d->host.isEmpty() && !d->user.isEmpty() && !d->pass.isEmpty() ) + p = "/"; + } + if ( !p.isEmpty() && p.right(1)!="/" ) + p += "/"; + p += rel; + d->path = p; + d->cleanPathDirty = TRUE; + } + } + } else { + if ( rel[ 0 ] == QChar( '/' ) ) { + *this = urlTmp; + setEncodedPathAndQuery( rel ); + } else { + *this = rel; + } + } +} + +/*! + Destructor. +*/ + +QUrl::~QUrl() +{ + delete d; + d = 0; +} + +/*! + Returns the protocol of the URL. Typically, "file", "http", "ftp", + etc. + + \sa setProtocol() +*/ + +QString QUrl::protocol() const +{ + return d->protocol; +} + +/*! + Sets the protocol of the URL to \a protocol. Typically, "file", + "http", "ftp", etc. + + \sa protocol() +*/ + +void QUrl::setProtocol( const QString& protocol ) +{ + d->protocol = protocol; + if ( hasHost() ) + d->isValid = TRUE; +} + +/*! + Returns the username of the URL. + + \sa setUser() setPassword() +*/ + +QString QUrl::user() const +{ + return d->user; +} + +/*! + Sets the username of the URL to \a user. + + \sa user() setPassword() +*/ + +void QUrl::setUser( const QString& user ) +{ + d->user = user; +} + +/*! + Returns TRUE if the URL contains a username; otherwise returns + FALSE. + + \sa setUser() setPassword() +*/ + +bool QUrl::hasUser() const +{ + return !d->user.isEmpty(); +} + +/*! + Returns the password of the URL. + + \warning Passwords passed in URLs are normally \e insecure; this + is due to the mechanism, not because of Qt. + + \sa setPassword() setUser() +*/ + +QString QUrl::password() const +{ + return d->pass; +} + +/*! + Sets the password of the URL to \a pass. + + \warning Passwords passed in URLs are normally \e insecure; this + is due to the mechanism, not because of Qt. + + \sa password() setUser() +*/ + +void QUrl::setPassword( const QString& pass ) +{ + d->pass = pass; +} + +/*! + Returns TRUE if the URL contains a password; otherwise returns + FALSE. + + \warning Passwords passed in URLs are normally \e insecure; this + is due to the mechanism, not because of Qt. + + \sa setPassword() setUser() +*/ + +bool QUrl::hasPassword() const +{ + return !d->pass.isEmpty(); +} + +/*! + Returns the hostname of the URL. + + \sa setHost() hasHost() +*/ + +QString QUrl::host() const +{ + return d->host; +} + +/*! + Sets the hostname of the URL to \a host. + + \sa host() hasHost() +*/ + +void QUrl::setHost( const QString& host ) +{ + d->host = host; + if ( !d->protocol.isNull() && d->protocol != "file" ) + d->isValid = TRUE; +} + +/*! + Returns TRUE if the URL contains a hostname; otherwise returns + FALSE. + + \sa setHost() +*/ + +bool QUrl::hasHost() const +{ + return !d->host.isEmpty(); +} + +/*! + Returns the port of the URL or -1 if no port has been set. + + \sa setPort() +*/ + +int QUrl::port() const +{ + return d->port; +} + +/*! + Sets the port of the URL to \a port. + + \sa port() +*/ + +void QUrl::setPort( int port ) +{ + d->port = port; +} + +/*! + Returns TRUE if the URL contains a port; otherwise returns FALSE. + + \sa setPort() +*/ + +bool QUrl::hasPort() const +{ + return d->port >= 0; +} + +/*! + Sets the path of the URL to \a path. + + \sa path() hasPath() +*/ + +void QUrl::setPath( const QString& path ) +{ + d->path = path; + slashify( d->path ); + d->cleanPathDirty = TRUE; + d->isValid = TRUE; +} + +/*! + Returns TRUE if the URL contains a path; otherwise returns FALSE. + + \sa path() setPath() +*/ + +bool QUrl::hasPath() const +{ + return !d->path.isEmpty(); +} + +/*! + Sets the query of the URL to \a txt. \a txt must be encoded. + + \sa query() encode() +*/ + +void QUrl::setQuery( const QString& txt ) +{ + d->queryEncoded = txt; +} + +/*! + Returns the (encoded) query of the URL. + + \sa setQuery() decode() +*/ + +QString QUrl::query() const +{ + return d->queryEncoded; +} + +/*! + Returns the (encoded) reference of the URL. + + \sa setRef() hasRef() decode() +*/ + +QString QUrl::ref() const +{ + return d->refEncoded; +} + +/*! + Sets the reference of the URL to \a txt. \a txt must be encoded. + + \sa ref() hasRef() encode() +*/ + +void QUrl::setRef( const QString& txt ) +{ + d->refEncoded = txt; +} + +/*! + Returns TRUE if the URL has a reference; otherwise returns FALSE. + + \sa setRef() +*/ + +bool QUrl::hasRef() const +{ + return !d->refEncoded.isEmpty(); +} + +/*! + Returns TRUE if the URL is valid; otherwise returns FALSE. A URL + is invalid if it cannot be parsed, for example. +*/ + +bool QUrl::isValid() const +{ + return d->isValid; +} + +/*! + Resets all parts of the URL to their default values and + invalidates it. +*/ + +void QUrl::reset() +{ + d->protocol = "file"; + d->user = ""; + d->pass = ""; + d->host = ""; + d->path = ""; + d->queryEncoded = ""; + d->refEncoded = ""; + d->isValid = TRUE; + d->port = -1; + d->cleanPathDirty = TRUE; +} + +/*! + Parses the \a url. +*/ + +bool QUrl::parse( const QString& url ) +{ + QString url_( url ); + slashify( url_ ); + + if ( url_.isEmpty() ) { + d->isValid = FALSE; + return FALSE; + } + + d->cleanPathDirty = TRUE; + d->isValid = TRUE; + QString oldProtocol = d->protocol; + d->protocol = QString::null; + + const int Init = 0; + const int Protocol = 1; + const int Separator1= 2; // : + const int Separator2= 3; // :/ + const int Separator3= 4; // :// or more slashes + const int User = 5; + const int Pass = 6; + const int Host = 7; + const int Path = 8; + const int Ref = 9; + const int Query = 10; + const int Port = 11; + const int Done = 12; + + const int InputAlpha= 1; + const int InputDigit= 2; + const int InputSlash= 3; + const int InputColon= 4; + const int InputAt = 5; + const int InputHash = 6; + const int InputQuery= 7; + + static uchar table[ 12 ][ 8 ] = { + /* None InputAlpha InputDigit InputSlash InputColon InputAt InputHash InputQuery */ + { 0, Protocol, 0, Path, 0, 0, 0, 0, }, // Init + { 0, Protocol, Protocol, 0, Separator1, 0, 0, 0, }, // Protocol + { 0, Path, Path, Separator2, 0, 0, 0, 0, }, // Separator1 + { 0, Path, Path, Separator3, 0, 0, 0, 0, }, // Separator2 + { 0, User, User, Separator3, Pass, Host, 0, 0, }, // Separator3 + { 0, User, User, User, Pass, Host, User, User, }, // User + { 0, Pass, Pass, Pass, Pass, Host, Pass, Pass, }, // Pass + { 0, Host, Host, Path, Port, Host, Ref, Query, }, // Host + { 0, Path, Path, Path, Path, Path, Ref, Query, }, // Path + { 0, Ref, Ref, Ref, Ref, Ref, Ref, Query, }, // Ref + { 0, Query, Query, Query, Query, Query, Query, Query, }, // Query + { 0, 0, Port, Path, 0, 0, 0, 0, } // Port + }; + + bool relPath = FALSE; + + relPath = FALSE; + bool forceRel = FALSE; + + // If ':' is at pos 1, we have only one letter + // before that separator => that's a drive letter! + if ( url_.length() >= 2 && url_[1] == ':' ) + relPath = forceRel = TRUE; + + int hasNoHost = -1; + int cs = url_.find( ":/" ); + if ( cs != -1 ) // if a protocol is there, find out if there is a host or directly the path after it + hasNoHost = url_.find( "///", cs ); + table[ 4 ][ 1 ] = User; + table[ 4 ][ 2 ] = User; + if ( cs == -1 || forceRel ) { // we have a relative file + if ( url.find( ':' ) == -1 || forceRel ) { + table[ 0 ][ 1 ] = Path; + // Filenames may also begin with a digit + table[ 0 ][ 2 ] = Path; + } else { + table[ 0 ][ 1 ] = Protocol; + } + relPath = TRUE; + } else { // some checking + table[ 0 ][ 1 ] = Protocol; + + // find the part between the protocol and the path as the meaning + // of that part is dependend on some chars + ++cs; + while ( url_[ cs ] == '/' ) + ++cs; + int slash = url_.find( "/", cs ); + if ( slash == -1 ) + slash = url_.length() - 1; + QString tmp = url_.mid( cs, slash - cs + 1 ); + + if ( !tmp.isEmpty() ) { // if this part exists + + // look for the @ in this part + int at = tmp.find( "@" ); + if ( at != -1 ) + at += cs; + // we have no @, which means host[:port], so directly + // after the protocol the host starts, or if the protocol + // is file or there were more than 2 slashes, it´s the + // path + if ( at == -1 ) { + if ( url_.left( 4 ) == "file" || hasNoHost != -1 ) + table[ 4 ][ 1 ] = Path; + else + table[ 4 ][ 1 ] = Host; + table[ 4 ][ 2 ] = table[ 4 ][ 1 ]; + } + } + } + + int state = Init; // parse state + int input; // input token + + QChar c = url_[ 0 ]; + int i = 0; + QString port; + + for ( ;; ) { + switch ( c ) { + case '?': + input = InputQuery; + break; + case '#': + input = InputHash; + break; + case '@': + input = InputAt; + break; + case ':': + input = InputColon; + break; + case '/': + input = InputSlash; + break; + case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': case '0': + input = InputDigit; + break; + default: + input = InputAlpha; + } + + state = table[ state ][ input ]; + + switch ( state ) { + case Protocol: + d->protocol += c; + break; + case User: + d->user += c; + break; + case Pass: + d->pass += c; + break; + case Host: + d->host += c; + break; + case Path: + d->path += c; + break; + case Ref: + d->refEncoded += c; + break; + case Query: + d->queryEncoded += c; + break; + case Port: + port += c; + break; + default: + break; + } + + ++i; + if ( i > (int)url_.length() - 1 || state == Done || state == 0 ) + break; + c = url_[ i ]; + + } + + if ( !port.isEmpty() ) { + port.remove( (uint)0, 1 ); + d->port = atoi( port.latin1() ); + } + + // error + if ( i < (int)url_.length() - 1 ) { + d->isValid = FALSE; + return FALSE; + } + + + if ( d->protocol.isEmpty() ) + d->protocol = oldProtocol; + + if ( d->path.isEmpty() ) + d->path = "/"; + + // hack for windows + if ( d->path.length() == 2 && d->path[ 1 ] == ':' ) + d->path += "/"; + + // #### do some corrections, should be done nicer too + if ( !d->pass.isEmpty() ) { + if ( d->pass[ 0 ] == ':' ) + d->pass.remove( (uint)0, 1 ); + decode( d->pass ); + } + if ( !d->user.isEmpty() ) { + decode( d->user ); + } + if ( !d->path.isEmpty() ) { + if ( d->path[ 0 ] == '@' || d->path[ 0 ] == ':' ) + d->path.remove( (uint)0, 1 ); + if ( d->path[ 0 ] != '/' && !relPath && d->path[ 1 ] != ':' ) + d->path.prepend( "/" ); + } + if ( !d->refEncoded.isEmpty() && d->refEncoded[ 0 ] == '#' ) + d->refEncoded.remove( (uint)0, 1 ); + if ( !d->queryEncoded.isEmpty() && d->queryEncoded[ 0 ] == '?' ) + d->queryEncoded.remove( (uint)0, 1 ); + if ( !d->host.isEmpty() && d->host[ 0 ] == '@' ) + d->host.remove( (uint)0, 1 ); + +#if defined(Q_OS_WIN32) + // hack for windows file://machine/path syntax + if ( d->protocol == "file" ) { + if ( url.left( 7 ) == "file://" && + d->path.length() > 1 && d->path[ 1 ] != ':' ) + d->path.prepend( "/" ); + } +#endif + + decode( d->path ); + d->cleanPathDirty = TRUE; + +#if 0 + qDebug( "URL: %s", url.latin1() ); + qDebug( "protocol: %s", d->protocol.latin1() ); + qDebug( "user: %s", d->user.latin1() ); + qDebug( "pass: %s", d->pass.latin1() ); + qDebug( "host: %s", d->host.latin1() ); + qDebug( "path: %s", path().latin1() ); + qDebug( "ref: %s", d->refEncoded.latin1() ); + qDebug( "query: %s", d->queryEncoded.latin1() ); + qDebug( "port: %d\n\n----------------------------\n\n", d->port ); +#endif + + return TRUE; +} + +/*! + \overload + + Parses \a url and assigns the resulting data to this class. + + If you pass a string like "/home/qt" the "file" protocol will be + assumed. +*/ + +QUrl& QUrl::operator=( const QString& url ) +{ + reset(); + parse( url ); + + return *this; +} + +/*! + Assigns the data of \a url to this class. +*/ + +QUrl& QUrl::operator=( const QUrl& url ) +{ + *d = *url.d; + return *this; +} + +/*! + Compares this URL with \a url and returns TRUE if they are equal; + otherwise returns FALSE. +*/ + +bool QUrl::operator==( const QUrl& url ) const +{ + if ( !isValid() || !url.isValid() ) + return FALSE; + + if ( d->protocol == url.d->protocol && + d->user == url.d->user && + d->pass == url.d->pass && + d->host == url.d->host && + d->path == url.d->path && + d->queryEncoded == url.d->queryEncoded && + d->refEncoded == url.d->refEncoded && + d->isValid == url.d->isValid && + d->port == url.d->port ) + return TRUE; + + return FALSE; +} + +/*! + \overload + + Compares this URL with \a url. \a url is parsed first. Returns + TRUE if \a url is equal to this url; otherwise returns FALSE. +*/ + +bool QUrl::operator==( const QString& url ) const +{ + QUrl u( url ); + return ( *this == u ); +} + +/*! + Sets the file name of the URL to \a name. If this URL contains a + fileName(), the original file name is replaced by \a name. + + See the documentation of fileName() for a more detailed discussion + of what is handled as file name and what is handled as a directory + path. + + \sa fileName() +*/ + +void QUrl::setFileName( const QString& name ) +{ + QString fn( name ); + slashify( fn ); + + while ( fn[ 0 ] == '/' ) + fn.remove( (uint)0, 1 ); + + QString p; + if ( path().isEmpty() ) { + p = "/"; + } else { + p = path(); + int slash = p.findRev( QChar( '/' ) ); + if ( slash == -1 ) { + p = "/"; + } else if ( p[ (int)p.length() - 1 ] != '/' ) { + p.truncate( slash + 1 ); + } + } + + p += fn; + if ( !d->queryEncoded.isEmpty() ) + p += "?" + d->queryEncoded; + setEncodedPathAndQuery( p ); +} + +/*! + Returns the encoded path and query. + + \sa decode() +*/ + +QString QUrl::encodedPathAndQuery() +{ + QString p = path(); + if ( p.isEmpty() ) + p = "/"; + + encode( p ); + + if ( !d->queryEncoded.isEmpty() ) { + p += "?"; + p += d->queryEncoded; + } + + return p; +} + +/*! + Parses \a pathAndQuery for a path and query and sets those values. + The whole string must be encoded. + + \sa encode() +*/ + +void QUrl::setEncodedPathAndQuery( const QString& pathAndQuery ) +{ + d->cleanPathDirty = TRUE; + int pos = pathAndQuery.find( '?' ); + if ( pos == -1 ) { + d->path = pathAndQuery; + d->queryEncoded = ""; + } else { + d->path = pathAndQuery.left( pos ); + d->queryEncoded = pathAndQuery.mid( pos + 1 ); + } + + decode( d->path ); + d->cleanPathDirty = TRUE; +} + +extern bool qt_resolve_symlinks; // defined in qapplication.cpp + +/*! + Returns the path of the URL. If \a correct is TRUE, the path is + cleaned (deals with too many or too few slashes, cleans things + like "/../..", etc). Otherwise path() returns exactly the path + that was parsed or set. + + \sa setPath() hasPath() +*/ +QString QUrl::path( bool correct ) const +{ + if ( !correct ) + return d->path; + + if ( d->cleanPathDirty ) { + bool check = TRUE; + if ( QDir::isRelativePath( d->path ) ) { + d->cleanPath = d->path; + } else if ( isLocalFile() ) { +#if defined(Q_OS_WIN32) + // hack for stuff like \\machine\path and //machine/path on windows + if ( ( d->path.left( 1 ) == "/" || d->path.left( 1 ) == "\\" ) && + d->path.length() > 1 ) { + d->cleanPath = d->path; + bool share = (d->cleanPath[0] == '\\' && d->cleanPath[1] == '\\') || + (d->cleanPath[0] == '/' && d->cleanPath[1] == '/'); + slashify( d->cleanPath, FALSE ); + d->cleanPath = QDir::cleanDirPath( d->cleanPath ); + if ( share ) { + check = FALSE; + while (d->cleanPath.at(0) != '/' || d->cleanPath.at(1) != '/') + d->cleanPath.prepend("/"); + } + } +#endif + if ( check ) { + QFileInfo fi( d->path ); + if ( !fi.exists() ) + d->cleanPath = d->path; + else if ( fi.isDir() ) { + QString canPath = QDir( d->path ).canonicalPath(); + QString dir; + if ( qt_resolve_symlinks && !canPath.isNull() ) + dir = QDir::cleanDirPath( canPath ); + else + dir = QDir::cleanDirPath( QDir( d->path ).absPath() ); + dir += "/"; + if ( dir == "//" ) + d->cleanPath = "/"; + else + d->cleanPath = dir; + } else { + QString p = + QDir::cleanDirPath( (qt_resolve_symlinks ? + fi.dir().canonicalPath() : + fi.dir().absPath()) ); + d->cleanPath = p + "/" + fi.fileName(); + } + } + } else { + if ( d->path != "/" && d->path[ (int)d->path.length() - 1 ] == '/' ) + d->cleanPath = QDir::cleanDirPath( d->path ) + "/"; + else + d->cleanPath = QDir::cleanDirPath( d->path ); + } + + if ( check ) + slashify( d->cleanPath, FALSE ); + d->cleanPathDirty = FALSE; + } + + return d->cleanPath; +} + +/*! + Returns TRUE if the URL is a local file; otherwise returns FALSE. +*/ + +bool QUrl::isLocalFile() const +{ + return d->protocol == "file"; +} + +/*! + Returns the file name of the URL. If the path of the URL doesn't + have a slash at the end, the part between the last slash and the + end of the path string is considered to be the file name. If the + path has a slash at the end, an empty string is returned here. + + \sa setFileName() +*/ + +QString QUrl::fileName() const +{ + if ( d->path.isEmpty() || d->path.endsWith( "/" ) +#ifdef Q_WS_WIN + || d->path.endsWith( "\\" ) +#endif + ) + return QString::null; + + return QFileInfo( d->path ).fileName(); +} + +/*! + Adds the path \a pa to the path of the URL. + + \sa setPath() hasPath() +*/ + +void QUrl::addPath( const QString& pa ) +{ + if ( pa.isEmpty() ) + return; + + QString p( pa ); + slashify( p ); + + if ( path().isEmpty() ) { + if ( p[ 0 ] != QChar( '/' ) ) + d->path = "/" + p; + else + d->path = p; + } else { + if ( p[ 0 ] != QChar( '/' ) && d->path[ (int)d->path.length() - 1 ] != '/' ) + d->path += "/" + p; + else + d->path += p; + } + d->cleanPathDirty = TRUE; +} + +/*! + Returns the directory path of the URL. This is the part of the + path of the URL without the fileName(). See the documentation of + fileName() for a discussion of what is handled as file name and + what is handled as directory path. + + \sa setPath() hasPath() +*/ + +QString QUrl::dirPath() const +{ + if ( path().isEmpty() ) + return QString::null; + + QString s = path(); + int pos = s.findRev( '/' ); + if ( pos == -1 ) { + return QString::fromLatin1("."); + } else { + if ( pos == 0 ) + return QString::fromLatin1( "/" ); + return s.left( pos ); + } +} + +/*! + Encodes the \a url in-place into UTF-8. For example + + \code + QString url = http://www.trolltech.com + QUrl::encode( url ); + // url is now "http%3A//www%20trolltech%20com" + \endcode + + \sa decode() +*/ + +void QUrl::encode( QString& url ) +{ + if ( url.isEmpty() ) + return; + + QCString curl = url.utf8(); + int oldlen = curl.length(); + + const QCString special( "+<>#@\"&%$:,;?={}|^~[]\'`\\ \n\t\r" ); + QString newUrl; + int newlen = 0; + + for ( int i = 0; i < oldlen ;++i ) { + uchar inCh = (uchar)curl[ i ]; + + if ( inCh >= 128 || special.contains(inCh) ) { + newUrl[ newlen++ ] = QChar( '%' ); + + ushort c = inCh / 16; + c += c > 9 ? 'A' - 10 : '0'; + newUrl[ newlen++ ] = c; + + c = inCh % 16; + c += c > 9 ? 'A' - 10 : '0'; + newUrl[ newlen++ ] = c; + } else { + newUrl[ newlen++ ] = inCh; + } + } + + url = newUrl; +} + +static uchar hex_to_int( uchar c ) +{ + if ( c >= 'A' && c <= 'F' ) + return c - 'A' + 10; + if ( c >= 'a' && c <= 'f') + return c - 'a' + 10; + if ( c >= '0' && c <= '9') + return c - '0'; + return 0; +} + +/*! + Decodes the \a url in-place into UTF-8. For example + + \code + QString url = "http%3A//www%20trolltech%20com" + QUrl::decode( url ); + // url is now "http://www.trolltech.com" + \endcode + + \sa encode() +*/ + +void QUrl::decode( QString& url ) +{ + if ( url.isEmpty() ) + return; + + int newlen = 0; + QCString curl = url.utf8(); + int oldlen = curl.length(); + + QCString newUrl(oldlen); + + int i = 0; + while ( i < oldlen ) { + uchar c = (uchar)curl[ i++ ]; + if ( c == '%' && i <= oldlen - 2 ) { + c = hex_to_int( (uchar)curl[ i ] ) * 16 + hex_to_int( (uchar)curl[ i + 1 ] ); + i += 2; + } + newUrl [ newlen++ ] = c; + } + newUrl.truncate( newlen ); + + url = QString::fromUtf8(newUrl.data()); +} + + +/*! + Composes a string version of the URL and returns it. If \a + encodedPath is TRUE the path in the returned string is encoded. If + \a forcePrependProtocol is TRUE and \a encodedPath looks like a + local filename, the "file:/" protocol is also prepended. + + \sa encode() decode() +*/ + +QString QUrl::toString( bool encodedPath, bool forcePrependProtocol ) const +{ + QString res, p = path(); + if ( encodedPath ) + encode( p ); + + if ( isLocalFile() ) { + if ( forcePrependProtocol ) + res = d->protocol + ":" + p; + else + res = p; + } else if ( d->protocol == "mailto" ) { + res = d->protocol + ":" + p; + } else { + res = d->protocol + "://"; + if ( !d->user.isEmpty() || !d->pass.isEmpty() ) { + QString tmp; + if ( !d->user.isEmpty() ) { + tmp = d->user; + encode( tmp ); + res += tmp; + } + if ( !d->pass.isEmpty() ) { + tmp = d->pass; + encode( tmp ); + res += ":" + tmp; + } + res += "@"; + } + res += d->host; + if ( d->port != -1 ) + res += ":" + QString( "%1" ).arg( d->port ); + if ( !p.isEmpty() ) { + if ( !d->host.isEmpty() && p[0]!='/' ) + res += "/"; + res += p; + } + } + + if ( !d->refEncoded.isEmpty() ) + res += "#" + d->refEncoded; + if ( !d->queryEncoded.isEmpty() ) + res += "?" + d->queryEncoded; + + return res; +} + +/*! + Composes a string version of the URL and returns it. + + \sa QUrl::toString() +*/ + +QUrl::operator QString() const +{ + return toString(); +} + +/*! + Changes the directory to one directory up. + + \sa setPath() +*/ + +bool QUrl::cdUp() +{ + d->path += "/.."; + d->cleanPathDirty = TRUE; + return TRUE; +} + +#endif // QT_NO_URL diff --git a/src/kernel/qurl.h b/src/kernel/qurl.h new file mode 100644 index 0000000..d7a291e --- /dev/null +++ b/src/kernel/qurl.h @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** Definition of QUrl class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QURL_H +#define QURL_H + +#ifndef QT_H +#include "qstring.h" +#endif // QT_H + +#ifndef QT_NO_URL + +class QUrlPrivate; + +class Q_EXPORT QUrl +{ +public: + QUrl(); + QUrl( const QString& url ); + QUrl( const QUrl& url ); + QUrl( const QUrl& url, const QString& relUrl, bool checkSlash = FALSE ); + virtual ~QUrl(); + + QString protocol() const; + virtual void setProtocol( const QString& protocol ); + + QString user() const; + virtual void setUser( const QString& user ); + bool hasUser() const; + + QString password() const; + virtual void setPassword( const QString& pass ); + bool hasPassword() const; + + QString host() const; + virtual void setHost( const QString& user ); + bool hasHost() const; + + int port() const; + virtual void setPort( int port ); + bool hasPort() const; + + QString path( bool correct = TRUE ) const; + virtual void setPath( const QString& path ); + bool hasPath() const; + + virtual void setEncodedPathAndQuery( const QString& enc ); + QString encodedPathAndQuery(); + + virtual void setQuery( const QString& txt ); + QString query() const; + + QString ref() const; + virtual void setRef( const QString& txt ); + bool hasRef() const; + + bool isValid() const; + bool isLocalFile() const; + + virtual void addPath( const QString& path ); + virtual void setFileName( const QString& txt ); + + QString fileName() const; + QString dirPath() const; + + QUrl& operator=( const QUrl& url ); + QUrl& operator=( const QString& url ); + + bool operator==( const QUrl& url ) const; + bool operator==( const QString& url ) const; + + static void decode( QString& url ); + static void encode( QString& url ); + + operator QString() const; + virtual QString toString( bool encodedPath = FALSE, bool forcePrependProtocol = TRUE ) const; + + virtual bool cdUp(); + + static bool isRelativeUrl( const QString &url ); + +protected: + virtual void reset(); + virtual bool parse( const QString& url ); + +private: + QUrlPrivate *d; + +}; + +#endif //QT_NO_URL + +#endif diff --git a/src/kernel/qurlinfo.cpp b/src/kernel/qurlinfo.cpp new file mode 100644 index 0000000..7c6155a --- /dev/null +++ b/src/kernel/qurlinfo.cpp @@ -0,0 +1,761 @@ +/**************************************************************************** +** +** Implementation of QUrlInfo class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qurlinfo.h" + +#ifndef QT_NO_NETWORKPROTOCOL + +#include "qurloperator.h" +#include "qdir.h" +#include <limits.h> + +class QUrlInfoPrivate +{ +public: + QUrlInfoPrivate() : + permissions(0), + size(0), + isDir(FALSE), + isFile(TRUE), + isSymLink(FALSE), + isWritable(TRUE), + isReadable(TRUE), + isExecutable(FALSE) + {} + + QString name; + int permissions; + QString owner; + QString group; +#if defined(QT_LARGEFILE_SUPPORT) + QIODevice::Offset size; +#else + uint size; +#endif + QDateTime lastModified; + QDateTime lastRead; + bool isDir; + bool isFile; + bool isSymLink; + bool isWritable; + bool isReadable; + bool isExecutable; +}; + + +/*! + \class QUrlInfo qurlinfo.h + \brief The QUrlInfo class stores information about URLs. + + \ingroup io + \ingroup misc + + This class is just a container for storing information about URLs, + which is why all information must be passed in the constructor. + + Unless you're reimplementing a network protocol you're unlikely to + create QUrlInfo objects yourself, but you may receive QUrlInfo + objects from functions, e.g. QUrlOperator::info(). + + The information that can be retrieved includes name(), + permissions(), owner(), group(), size(), lastModified(), + lastRead(), isDir(), isFile(), isSymLink(), isWritable(), + isReadable() and isExecutable(). +*/ + +/*! + \enum QUrlInfo::PermissionSpec + + This enum is used by the permissions() function to report the + permissions of a file. + + \value ReadOwner The file is readable by the owner of the file. + \value WriteOwner The file is writable by the owner of the file. + \value ExeOwner The file is executable by the owner of the file. + \value ReadGroup The file is readable by the group. + \value WriteGroup The file is writable by the group. + \value ExeGroup The file is executable by the group. + \value ReadOther The file is readable by anyone. + \value WriteOther The file is writable by anyone. + \value ExeOther The file is executable by anyone. +*/ + +/*! + Constructs an invalid QUrlInfo object with default values. + + \sa isValid() +*/ + +QUrlInfo::QUrlInfo() +{ + d = 0; +} + +/*! + Constructs a QUrlInfo object with information about the file \a + file in the \a path. It tries to find the information about the \a + file in the QUrlOperator \a path. + + If the information is not found, this constructor creates an + invalid QUrlInfo, i.e. isValid() returns FALSE. You should always + check if the URL info is valid before relying on the return values + of any getter functions. + + If \a file is empty, it defaults to the QUrlOperator \a path, i.e. + to the directory. + + \sa isValid() QUrlOperator::info() +*/ + +QUrlInfo::QUrlInfo( const QUrlOperator &path, const QString &file ) +{ + QString file_ = file; + if ( file_.isEmpty() ) + file_ = "."; + + QUrlInfo inf = path.info( file_ ); + if ( inf.d ) { + d = new QUrlInfoPrivate; + *d = *inf.d; + } else { + d = 0; + } +} + +/*! + Copy constructor, copies \a ui to this URL info object. +*/ + +QUrlInfo::QUrlInfo( const QUrlInfo &ui ) +{ + if ( ui.d ) { + d = new QUrlInfoPrivate; + *d = *ui.d; + } else { + d = 0; + } +} + +/*! + Constructs a QUrlInfo object by specifying all the URL's + information. + + The information that is passed is the \a name, file \a + permissions, \a owner and \a group and the file's \a size. Also + passed is the \a lastModified date/time and the \a lastRead + date/time. Flags are also passed, specifically, \a isDir, \a + isFile, \a isSymLink, \a isWritable, \a isReadable and \a + isExecutable. +*/ + +#if defined(QT_ABI_QT4) +QUrlInfo::QUrlInfo( const QString &name, int permissions, const QString &owner, + const QString &group, QIODevice::Offset size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ) +#else +QUrlInfo::QUrlInfo( const QString &name, int permissions, const QString &owner, + const QString &group, uint size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ) +#endif +{ + d = new QUrlInfoPrivate; + d->name = name; + d->permissions = permissions; + d->owner = owner; + d->group = group; + d->size = size; + d->lastModified = lastModified; + d->lastRead = lastRead; + d->isDir = isDir; + d->isFile = isFile; + d->isSymLink = isSymLink; + d->isWritable = isWritable; + d->isReadable = isReadable; + d->isExecutable = isExecutable; +} + + +/*! + Constructs a QUrlInfo object by specifying all the URL's + information. + + The information that is passed is the \a url, file \a + permissions, \a owner and \a group and the file's \a size. Also + passed is the \a lastModified date/time and the \a lastRead + date/time. Flags are also passed, specifically, \a isDir, \a + isFile, \a isSymLink, \a isWritable, \a isReadable and \a + isExecutable. +*/ + +#if defined(QT_ABI_QT4) +QUrlInfo::QUrlInfo( const QUrl &url, int permissions, const QString &owner, + const QString &group, QIODevice::Offset size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ) +#else +QUrlInfo::QUrlInfo( const QUrl &url, int permissions, const QString &owner, + const QString &group, uint size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ) +#endif +{ + d = new QUrlInfoPrivate; + d->name = QFileInfo( url.path() ).fileName(); + d->permissions = permissions; + d->owner = owner; + d->group = group; + d->size = size; + d->lastModified = lastModified; + d->lastRead = lastRead; + d->isDir = isDir; + d->isFile = isFile; + d->isSymLink = isSymLink; + d->isWritable = isWritable; + d->isReadable = isReadable; + d->isExecutable = isExecutable; +} + + +/*! + Sets the name of the URL to \a name. The name is the full text, + for example, "http://doc.trolltech.com/qurlinfo.html". + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setName( const QString &name ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->name = name; +} + + +/*! + If \a b is TRUE then the URL is set to be a directory; if \b is + FALSE then the URL is set not to be a directory (which normally + means it is a file). (Note that a URL can refer to both a file and + a directory even though most file systems do not support this.) + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setDir( bool b ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->isDir = b; +} + + +/*! + If \a b is TRUE then the URL is set to be a file; if \b is FALSE + then the URL is set not to be a file (which normally means it is a + directory). (Note that a URL can refer to both a file and a + directory even though most file systems do not support this.) + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setFile( bool b ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->isFile = b; +} + + +/*! + Specifies that the URL refers to a symbolic link if \a b is TRUE + and that it does not if \a b is FALSE. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setSymLink( bool b ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->isSymLink = b; +} + + +/*! + Specifies that the URL is writable if \a b is TRUE and not + writable if \a b is FALSE. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setWritable( bool b ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->isWritable = b; +} + + +/*! + Specifies that the URL is readable if \a b is TRUE and not + readable if \a b is FALSE. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setReadable( bool b ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->isReadable = b; +} + +/*! + Specifies that the owner of the URL is called \a s. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setOwner( const QString &s ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->owner = s; +} + +/*! + Specifies that the owning group of the URL is called \a s. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setGroup( const QString &s ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->group = s; +} + +/*! + Specifies the \a size of the URL. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +#if defined(QT_ABI_QT4) +void QUrlInfo::setSize( QIODevice::Offset size ) +#else +void QUrlInfo::setSize( uint size ) +#endif +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->size = size; +} + + +// ### reggie - what's the permission type? As in Unix? + +/*! + Specifies that the URL has access permisions, \a p. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setPermissions( int p ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->permissions = p; +} + +/*! + Specifies that the object the URL refers to was last modified at + \a dt. + + If you call this function for an invalid URL info, this function + turns it into a valid one. + + \sa isValid() +*/ + +void QUrlInfo::setLastModified( const QDateTime &dt ) +{ + if ( !d ) + d = new QUrlInfoPrivate; + d->lastModified = dt; +} + +/*! + Destroys the URL info object. + + The QUrlOperator object to which this URL referred (if any) is not + affected. +*/ + +QUrlInfo::~QUrlInfo() +{ + delete d; +} + +/*! + Assigns the values of \a ui to this QUrlInfo object. +*/ + +QUrlInfo &QUrlInfo::operator=( const QUrlInfo &ui ) +{ + if ( ui.d ) { + if ( !d ) + d= new QUrlInfoPrivate; + *d = *ui.d; + } else { + delete d; + d = 0; + } + return *this; +} + +/*! + Returns the file name of the URL. + + \sa isValid() +*/ + +QString QUrlInfo::name() const +{ + if ( !d ) + return QString::null; + return d->name; +} + +/*! + Returns the permissions of the URL. You can use the \c PermissionSpec flags + to test for certain permissions. + + \sa isValid() +*/ + +int QUrlInfo::permissions() const +{ + if ( !d ) + return 0; + return d->permissions; +} + +/*! + Returns the owner of the URL. + + \sa isValid() +*/ + +QString QUrlInfo::owner() const +{ + if ( !d ) + return QString::null; + return d->owner; +} + +/*! + Returns the group of the URL. + + \sa isValid() +*/ + +QString QUrlInfo::group() const +{ + if ( !d ) + return QString::null; + return d->group; +} + +/*! + Returns the size of the URL. + + \sa isValid() +*/ + +#if defined(QT_ABI_QT4) +QIODevice::Offset QUrlInfo::size() const +#else +uint QUrlInfo::size() const +#endif +{ + if ( !d ) + return 0; +#if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4) + return d->size > UINT_MAX ? UINT_MAX : (uint)d->size; +#else + return d->size; +#endif +} + +/*! + Returns the last modification date of the URL. + + \sa isValid() +*/ + +QDateTime QUrlInfo::lastModified() const +{ + if ( !d ) + return QDateTime(); + return d->lastModified; +} + +/*! + Returns the date when the URL was last read. + + \sa isValid() +*/ + +QDateTime QUrlInfo::lastRead() const +{ + if ( !d ) + return QDateTime(); + return d->lastRead; +} + +/*! + Returns TRUE if the URL is a directory; otherwise returns FALSE. + + \sa isValid() +*/ + +bool QUrlInfo::isDir() const +{ + if ( !d ) + return FALSE; + return d->isDir; +} + +/*! + Returns TRUE if the URL is a file; otherwise returns FALSE. + + \sa isValid() +*/ + +bool QUrlInfo::isFile() const +{ + if ( !d ) + return FALSE; + return d->isFile; +} + +/*! + Returns TRUE if the URL is a symbolic link; otherwise returns FALSE. + + \sa isValid() +*/ + +bool QUrlInfo::isSymLink() const +{ + if ( !d ) + return FALSE; + return d->isSymLink; +} + +/*! + Returns TRUE if the URL is writable; otherwise returns FALSE. + + \sa isValid() +*/ + +bool QUrlInfo::isWritable() const +{ + if ( !d ) + return FALSE; + return d->isWritable; +} + +/*! + Returns TRUE if the URL is readable; otherwise returns FALSE. + + \sa isValid() +*/ + +bool QUrlInfo::isReadable() const +{ + if ( !d ) + return FALSE; + return d->isReadable; +} + +/*! + Returns TRUE if the URL is executable; otherwise returns FALSE. + + \sa isValid() +*/ + +bool QUrlInfo::isExecutable() const +{ + if ( !d ) + return FALSE; + return d->isExecutable; +} + +/*! + Returns TRUE if \a i1 is greater than \a i2; otherwise returns + FALSE. The objects are compared by the value, which is specified + by \a sortBy. This must be one of QDir::Name, QDir::Time or + QDir::Size. +*/ + +bool QUrlInfo::greaterThan( const QUrlInfo &i1, const QUrlInfo &i2, + int sortBy ) +{ + switch ( sortBy ) { + case QDir::Name: + return i1.name() > i2.name(); + case QDir::Time: + return i1.lastModified() > i2.lastModified(); + case QDir::Size: + return i1.size() > i2.size(); + default: + return FALSE; + } +} + +/*! + Returns TRUE if \a i1 is less than \a i2; otherwise returns FALSE. + The objects are compared by the value, which is specified by \a + sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size. +*/ + +bool QUrlInfo::lessThan( const QUrlInfo &i1, const QUrlInfo &i2, + int sortBy ) +{ + return !greaterThan( i1, i2, sortBy ); +} + +/*! + Returns TRUE if \a i1 equals to \a i2; otherwise returns FALSE. + The objects are compared by the value, which is specified by \a + sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size. +*/ + +bool QUrlInfo::equal( const QUrlInfo &i1, const QUrlInfo &i2, + int sortBy ) +{ + switch ( sortBy ) { + case QDir::Name: + return i1.name() == i2.name(); + case QDir::Time: + return i1.lastModified() == i2.lastModified(); + case QDir::Size: + return i1.size() == i2.size(); + default: + return FALSE; + } +} + +/*! + Compares this QUrlInfo with \a i and returns TRUE if they are + equal; otherwise returns FALSE. +*/ + +bool QUrlInfo::operator==( const QUrlInfo &i ) const +{ + if ( !d ) + return i.d == 0; + if ( !i.d ) + return FALSE; + + return ( d->name == i.d->name && + d->permissions == i.d->permissions && + d->owner == i.d->owner && + d->group == i.d->group && + d->size == i.d->size && + d->lastModified == i.d->lastModified && + d->lastRead == i.d->lastRead && + d->isDir == i.d->isDir && + d->isFile == i.d->isFile && + d->isSymLink == i.d->isSymLink && + d->isWritable == i.d->isWritable && + d->isReadable == i.d->isReadable && + d->isExecutable == i.d->isExecutable ); +} + +/*! + Returns TRUE if the URL info is valid; otherwise returns FALSE. + Valid means that the QUrlInfo contains real information. For + example, a call to QUrlOperator::info() might return a an invalid + QUrlInfo, if no information about the requested entry is + available. + + You should always check if the URL info is valid before relying on + the values. +*/ +bool QUrlInfo::isValid() const +{ + return d != 0; +} + +#endif // QT_NO_NETWORKPROTOCOL diff --git a/src/kernel/qurlinfo.h b/src/kernel/qurlinfo.h new file mode 100644 index 0000000..425e93c --- /dev/null +++ b/src/kernel/qurlinfo.h @@ -0,0 +1,144 @@ +/**************************************************************************** +** +** Definition of QUrlInfo class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QURLINFO_H +#define QURLINFO_H + +#ifndef QT_H +#include "qdatetime.h" +#include "qstring.h" +#if defined(QT_ABI_QT4) +#include "qiodevice.h" +#endif +#endif // QT_H + +class QUrlOperator; +class QUrl; +class QUrlInfoPrivate; + +class Q_EXPORT QUrlInfo +{ +public: + enum PermissionSpec { + ReadOwner = 00400, WriteOwner = 00200, ExeOwner = 00100, + ReadGroup = 00040, WriteGroup = 00020, ExeGroup = 00010, + ReadOther = 00004, WriteOther = 00002, ExeOther = 00001 }; + + QUrlInfo(); + QUrlInfo( const QUrlOperator &path, const QString &file ); + QUrlInfo( const QUrlInfo &ui ); +#if (QT_VERSION-0 >= 0x040000) +#error "QUrlInfo::QUrlInfo() should accept QIODevice::Offset instead of uint" +#elif defined(QT_ABI_QT4) + QUrlInfo( const QString &name, int permissions, const QString &owner, + const QString &group, QIODevice::Offset size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ); + QUrlInfo( const QUrl &url, int permissions, const QString &owner, + const QString &group, QIODevice::Offset size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ); +#else + QUrlInfo( const QString &name, int permissions, const QString &owner, + const QString &group, uint size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ); + QUrlInfo( const QUrl &url, int permissions, const QString &owner, + const QString &group, uint size, const QDateTime &lastModified, + const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink, + bool isWritable, bool isReadable, bool isExecutable ); +#endif + QUrlInfo &operator=( const QUrlInfo &ui ); + virtual ~QUrlInfo(); + + virtual void setName( const QString &name ); + virtual void setDir( bool b ); + virtual void setFile( bool b ); + virtual void setSymLink( bool b ); + virtual void setOwner( const QString &s ); + virtual void setGroup( const QString &s ); +#if (QT_VERSION-0 >= 0x040000) +#error "QUrlInfo::setSize() should accept QIODevice::Offset instead of uint" +#elif defined(QT_ABI_QT4) + virtual void setSize( QIODevice::Offset size ); +#else + virtual void setSize( uint size ); +#endif + virtual void setWritable( bool b ); + virtual void setReadable( bool b ); + virtual void setPermissions( int p ); + virtual void setLastModified( const QDateTime &dt ); + + bool isValid() const; + + QString name() const; + int permissions() const; + QString owner() const; + QString group() const; +#if (QT_VERSION-0 >= 0x040000) +#error "QUrlInfo::size() should return QIODevice::Offset instead of uint" +#elif defined(QT_ABI_QT4) + QIODevice::Offset size() const; +#else + uint size() const; +#endif + QDateTime lastModified() const; + QDateTime lastRead() const; + bool isDir() const; + bool isFile() const; + bool isSymLink() const; + bool isWritable() const; + bool isReadable() const; + bool isExecutable() const; + + static bool greaterThan( const QUrlInfo &i1, const QUrlInfo &i2, + int sortBy ); + static bool lessThan( const QUrlInfo &i1, const QUrlInfo &i2, + int sortBy ); + static bool equal( const QUrlInfo &i1, const QUrlInfo &i2, + int sortBy ); + + bool operator==( const QUrlInfo &i ) const; +private: + QUrlInfoPrivate *d; + +}; + +#endif diff --git a/src/kernel/qurloperator.cpp b/src/kernel/qurloperator.cpp new file mode 100644 index 0000000..1e1b666 --- /dev/null +++ b/src/kernel/qurloperator.cpp @@ -0,0 +1,1226 @@ +/**************************************************************************** +** +** Implementation of QUrlOperator class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qurloperator.h" + +#ifndef QT_NO_NETWORKPROTOCOL + +#include "qurlinfo.h" +#include "qnetworkprotocol.h" +#include "qmap.h" +#include "qdir.h" +#include "qptrdict.h" +#include "qguardedptr.h" + +//#define QURLOPERATOR_DEBUG + +class QUrlOperatorPrivate +{ +public: + QUrlOperatorPrivate() + { + oldOps.setAutoDelete( FALSE ); + networkProtocol = 0; + nameFilter = "*"; + currPut = 0; + } + + ~QUrlOperatorPrivate() + { + delete networkProtocol; + while ( oldOps.first() ) { + oldOps.first()->free(); + oldOps.removeFirst(); + } + } + + QMap<QString, QUrlInfo> entryMap; + QNetworkProtocol *networkProtocol; + QString nameFilter; + QDir dir; + + // maps needed for copy/move operations + QPtrDict<QNetworkOperation> getOpPutOpMap; + QPtrDict<QNetworkProtocol> getOpPutProtMap; + QPtrDict<QNetworkProtocol> getOpGetProtMap; + QPtrDict<QNetworkOperation> getOpRemoveOpMap; + QGuardedPtr<QNetworkProtocol> currPut; + QStringList waitingCopies; + QString waitingCopiesDest; + bool waitingCopiesMove; + QPtrList< QNetworkOperation > oldOps; +}; + +/*! + \class QUrlOperator qurloperator.h + + \brief The QUrlOperator class provides common operations on URLs. +\if defined(commercial) + It is part of the <a href="commercialeditions.html">Qt Enterprise Edition</a>. +\endif + + \ingroup io + \ingroup misc + \mainclass + + \module network + + This class operates on hierarchical structures (such as + filesystems) using URLs. Its API facilitates all the common + operations: + \table + \header \i Operation \i Function + \row \i List files \i \l listChildren() + \row \i Make a directory \i \l mkdir() + \row \i Remove a file \i \l remove() + \row \i Rename a file \i \l rename() + \row \i Get a file \i \l get() + \row \i Put a file \i \l put() + \row \i Copy a file \i \l copy() + \endtable + + You can obtain additional information about the URL with isDir() + and info(). If a directory is to be traversed using + listChildren(), a name filter can be set with setNameFilter(). + + A QUrlOperator can be used like this, for example to download a + file (and assuming that the FTP protocol is \link + qInitNetworkProtocols() registered\endlink): + \code + QUrlOperator *op = new QUrlOperator(); + op->copy( QString("ftp://ftp.trolltech.com/qt/source/qt-2.1.0.tar.gz"), + "file:/tmp" ); + \endcode + + If you want to be notified about success/failure, progress, etc., + you can connect to QUrlOperator's signals, e.g. to start(), + newChildren(), createdDirectory(), removed(), data(), + dataTransferProgress(), startedNextCopy(), + connectionStateChanged(), finished(), etc. A network operation can + be stopped with stop(). + + The class uses the functionality of registered network protocols + to perform these operations. Depending of the protocol of the URL, + it uses an appropriate network protocol class for the operations. + Each of the operation functions of QUrlOperator creates a + QNetworkOperation object that describes the operation and puts it + into the operation queue for the network protocol used. If no + suitable protocol could be found (because no implementation of the + necessary network protocol is registered), the URL operator emits + errors. Not every protocol supports every operation, but error + handling deals with this problem. + + To register the available network protocols, use the + qInitNetworkProtocols() function. The protocols currently + supported are: + \list + \i \link QFtp FTP\endlink, + \i \link QHttp HTTP\endlink, + \i \link QLocalFs local file system\endlink. + \endlist + + For more information about the Qt Network Architecture see the + \link network.html Qt Network Documentation\endlink. + + \sa QNetworkProtocol, QNetworkOperation +*/ + +/*! + \fn void QUrlOperator::newChildren( const QValueList<QUrlInfo> &i, QNetworkOperation *op ) + + This signal is emitted after listChildren() was called and new + children (i.e. files) have been read from a list of files. \a i + holds the information about the new files. \a op is a pointer + to the operation object which contains all the information about + the operation, including the state. + + \sa QNetworkOperation, QNetworkProtocol +*/ + + +/*! + \fn void QUrlOperator::finished( QNetworkOperation *op ) + + This signal is emitted when an operation of some sort finishes, + whether with success or failure. \a op is a pointer to the + operation object, which contains all the information, including + the state, of the operation which has been finished. Check the + state and error code of the operation object to see whether or not + the operation was successful. + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::start( QNetworkOperation *op ) + + Some operations (such as listChildren()) emit this signal when + they start processing the operation. \a op is a pointer to the + operation object which contains all the information about the + operation, including the state. + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::createdDirectory( const QUrlInfo &i, QNetworkOperation *op ) + + This signal is emitted when mkdir() succeeds and the directory has + been created. \a i holds the information about the new directory. + + \a op is a pointer to the operation object, which contains all the + information about the operation, including the state. + \c op->arg(0) holds the new directory's name. + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::removed( QNetworkOperation *op ) + + This signal is emitted when remove() has been succesful and the + file has been removed. + + \a op is a pointer to the operation object which contains all the + information about the operation, including the state. + \c op->arg(0) holds the name of the file that was removed. + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::itemChanged( QNetworkOperation *op ) + + This signal is emitted whenever a file which is a child of the URL + has been changed, for example by successfully calling rename(). + \a op is a pointer to the operation object which contains all the + information about the operation, including the state. + \c op->arg(0) holds the original file name and \c op->arg(1) holds + the new file name (if it was changed). + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::data( const QByteArray &data, QNetworkOperation *op ) + + This signal is emitted when new \a data has been received after calling + get() or put(). + \a op is a pointer to the operation object which contains all + the information about the operation, including the state. + \c op->arg(0) holds the name of the file whose data is retrieved + and op->rawArg(1) holds the (raw) data. + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::dataTransferProgress( int bytesDone, int bytesTotal, QNetworkOperation *op ) + + This signal is emitted during data transfer (using put() or + get()). \a bytesDone specifies how many bytes of \a bytesTotal have + been transferred. More information about the operation is stored in + \a op, a pointer to the network operation that is processed. + \a bytesTotal may be -1, which means that the total number of bytes + is not known. + + \sa QNetworkOperation, QNetworkProtocol +*/ + +/*! + \fn void QUrlOperator::startedNextCopy( const QPtrList<QNetworkOperation> &lst ) + + This signal is emitted if copy() starts a new copy operation. \a + lst contains all QNetworkOperations related to this copy + operation. + + \sa copy() +*/ + +/*! + \fn void QUrlOperator::connectionStateChanged( int state, const QString &data ) + + This signal is emitted whenever the URL operator's connection + state changes. \a state describes the new state, which is a + \l{QNetworkProtocol::ConnectionState} value. + + \a data is a string that describes the change of the connection. + This can be used to display a message to the user. +*/ + +/*! + Constructs a QUrlOperator with an empty (i.e. invalid) URL. +*/ + +QUrlOperator::QUrlOperator() + : QUrl() +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: cstr 1" ); +#endif + d = new QUrlOperatorPrivate; +} + +/*! + Constructs a QUrlOperator using \a url and parses this string. + + If you pass strings like "/home/qt" the "file" protocol is + assumed. +*/ + +QUrlOperator::QUrlOperator( const QString &url ) + : QUrl( url ) +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: cstr 2" ); +#endif + d = new QUrlOperatorPrivate; + getNetworkProtocol(); +} + +/*! + Constructs a copy of \a url. +*/ + +QUrlOperator::QUrlOperator( const QUrlOperator& url ) + : QObject(), QUrl( url ) +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: cstr 3" ); +#endif + d = new QUrlOperatorPrivate; + *d = *url.d; + + d->networkProtocol = 0; + getNetworkProtocol(); + d->nameFilter = "*"; + d->currPut = 0; +} + +/*! + Constructs a QUrlOperator. The URL on which this QUrlOperator + operates is constructed out of the arguments \a url, \a relUrl and + \a checkSlash: see the corresponding QUrl constructor for an + explanation of these arguments. +*/ + +QUrlOperator::QUrlOperator( const QUrlOperator& url, const QString& relUrl, bool checkSlash ) + : QUrl( url, relUrl, checkSlash ) +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: cstr 4" ); +#endif + d = new QUrlOperatorPrivate; + if ( relUrl == "." ) + *d = *url.d; + + d->networkProtocol = 0; + getNetworkProtocol(); + d->currPut = 0; +} + +/*! + Destructor. +*/ + +QUrlOperator::~QUrlOperator() +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: dstr" ); +#endif + delete d; +} + +/*! + This private function is used by the simple operation functions, + i.e. listChildren(), mkdir(), remove(), rename(), get() and put(), + to really start the operation. \a op is a pointer to the network + operation that should be started. Returns \a op on success; + otherwise returns 0. +*/ +const QNetworkOperation *QUrlOperator::startOperation( QNetworkOperation *op ) +{ + if ( !d->networkProtocol ) + getNetworkProtocol(); + + if ( d->networkProtocol && (d->networkProtocol->supportedOperations()&op->operation()) ) { + d->networkProtocol->addOperation( op ); + if ( op->operation() == QNetworkProtocol::OpListChildren ) + clearEntries(); + return op; + } + + // error + QString msg; + if ( !d->networkProtocol ) { + msg = tr( "The protocol `%1' is not supported" ).arg( protocol() ); + } else { + switch ( op->operation() ) { + case QNetworkProtocol::OpListChildren: + msg = tr( "The protocol `%1' does not support listing directories" ).arg( protocol() ); + break; + case QNetworkProtocol::OpMkDir: + msg = tr( "The protocol `%1' does not support creating new directories" ).arg( protocol() ); + break; + case QNetworkProtocol::OpRemove: + msg = tr( "The protocol `%1' does not support removing files or directories" ).arg( protocol() ); + break; + case QNetworkProtocol::OpRename: + msg = tr( "The protocol `%1' does not support renaming files or directories" ).arg( protocol() ); + break; + case QNetworkProtocol::OpGet: + msg = tr( "The protocol `%1' does not support getting files" ).arg( protocol() ); + break; + case QNetworkProtocol::OpPut: + msg = tr( "The protocol `%1' does not support putting files" ).arg( protocol() ); + break; + default: + // this should never happen + break; + } + } + op->setState( QNetworkProtocol::StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)QNetworkProtocol::ErrUnsupported ); + emit finished( op ); + deleteOperation( op ); + return 0; +} + +/*! + Starts listing the children of this URL (e.g. the files in the + directory). The start() signal is emitted before the first entry + is listed and finished() is emitted after the last one. The + newChildren() signal is emitted for each list of new entries. If + an error occurs, the signal finished() is emitted, so be sure to + check the state of the network operation pointer. + + Because the operation may not be executed immediately, a pointer + to the QNetworkOperation object created by this function is + returned. This object contains all the data about the operation + and is used to refer to this operation later (e.g. in the signals + that are emitted by the QUrlOperator). The return value can also + be 0 if the operation object couldn't be created. + + The path of this QUrlOperator must to point to a directory + (because the children of this directory will be listed), not to a + file. +*/ + +const QNetworkOperation *QUrlOperator::listChildren() +{ + if ( !checkValid() ) + return 0; + + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpListChildren, QString::null, QString::null, QString::null ); + return startOperation( res ); +} + +/*! + Tries to create a directory (child) with the name \a dirname. If + it is successful, a newChildren() signal with the new child is + emitted, and the createdDirectory() signal with the information + about the new child is also emitted. The finished() signal (with + success or failure) is emitted after the operation has been + processed, so check the state of the network operation object to + see whether or not the operation was successful. + + Because the operation will not be executed immediately, a pointer + to the QNetworkOperation object created by this function is + returned. This object contains all the data about the operation + and is used to refer to this operation later (e.g. in the signals + that are emitted by the QUrlOperator). The return value can also + be 0 if the operation object couldn't be created. + + The path of this QUrlOperator must to point to a directory (not a + file) because the new directory will be created in this path. +*/ + +const QNetworkOperation *QUrlOperator::mkdir( const QString &dirname ) +{ + if ( !checkValid() ) + return 0; + + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpMkDir, dirname, QString::null, QString::null ); + return startOperation( res ); +} + +/*! + Tries to remove the file (child) \a filename. If it succeeds the + removed() signal is emitted. finished() (with success or failure) + is also emitted after the operation has been processed, so check + the state of the network operation object to see whether or not + the operation was successful. + + Because the operation will not be executed immediately, a pointer + to the QNetworkOperation object created by this function is + returned. This object contains all the data about the operation + and is used to refer to this operation later (e.g. in the signals + that are emitted by the QUrlOperator). The return value can also + be 0 if the operation object couldn't be created. + + The path of this QUrlOperator must point to a directory; because + if \a filename is relative, it will try to remove it in this + directory. +*/ + +const QNetworkOperation *QUrlOperator::remove( const QString &filename ) +{ + if ( !checkValid() ) + return 0; + + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpRemove, filename, QString::null, QString::null ); + return startOperation( res ); +} + +/*! + Tries to rename the file (child) called \a oldname to \a newname. + If it succeeds, the itemChanged() signal is emitted. finished() + (with success or failure) is also emitted after the operation has + been processed, so check the state of the network operation object + to see whether or not the operation was successful. + + Because the operation may not be executed immediately, a pointer + to the QNetworkOperation object created by this function is + returned. This object contains all the data about the operation + and is used to refer to this operation later (e.g. in the signals + that are emitted by the QUrlOperator). The return value can also + be 0 if the operation object couldn't be created. + + This path of this QUrlOperator must to point to a directory + because \a oldname and \a newname are handled relative to this + directory. +*/ + +const QNetworkOperation *QUrlOperator::rename( const QString &oldname, const QString &newname ) +{ + if ( !checkValid() ) + return 0; + + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpRename, oldname, newname, QString::null ); + return startOperation( res ); +} + +/*! + Copies the file \a from to \a to. If \a move is TRUE, the file is + moved (copied and removed). \a from must point to a file and \a to + must point to a directory (into which \a from is copied) unless \a + toPath is set to FALSE. If \a toPath is set to FALSE then the \a + to variable is assumed to be the absolute file path (destination + file path + file name). The copying is done using the get() and + put() operations. If you want to be notified about the progress of + the operation, connect to the dataTransferProgress() signal. Bear + in mind that the get() and put() operations emit this signal + through the QUrlOperator. The number of transferred bytes and the + total bytes that you receive as arguments in this signal do not + relate to the the whole copy operation; they relate first to the + get() and then to the put() operation. Always check what type of + operation the signal comes from; this is given in the signal's + last argument. + + At the end, finished() (with success or failure) is emitted, so + check the state of the network operation object to see whether or + not the operation was successful. + + Because a move or copy operation consists of multiple operations + (get(), put() and maybe remove()), this function doesn't return a + single QNetworkOperation, but rather a list of them. They are in + the order: get(), put() and (if applicable) remove(). + + \sa get(), put() +*/ + +QPtrList<QNetworkOperation> QUrlOperator::copy( const QString &from, const QString &to, bool move, bool toPath ) +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: copy %s %s %d", from.latin1(), to.latin1(), move ); +#endif + + QPtrList<QNetworkOperation> ops; + ops.setAutoDelete( FALSE ); + + QUrlOperator *uFrom = new QUrlOperator( *this, from ); + QUrlOperator *uTo = new QUrlOperator( to ); + + // prepare some string for later usage + QString frm = *uFrom; + QString file = uFrom->fileName(); + + if (frm == to + file) + return ops; + + file.prepend( "/" ); + + // uFrom and uTo are deleted when the QNetworkProtocol deletes itself via + // autodelete + uFrom->getNetworkProtocol(); + uTo->getNetworkProtocol(); + QNetworkProtocol *gProt = uFrom->d->networkProtocol; + QNetworkProtocol *pProt = uTo->d->networkProtocol; + + uFrom->setPath( uFrom->dirPath() ); + + if ( gProt && (gProt->supportedOperations()&QNetworkProtocol::OpGet) && + pProt && (pProt->supportedOperations()&QNetworkProtocol::OpPut) ) { + + connect( gProt, SIGNAL( data(const QByteArray&,QNetworkOperation*) ), + this, SLOT( copyGotData(const QByteArray&,QNetworkOperation*) ) ); + connect( gProt, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ), + this, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ) ); + connect( gProt, SIGNAL( finished(QNetworkOperation*) ), + this, SLOT( continueCopy(QNetworkOperation*) ) ); + connect( gProt, SIGNAL( finished(QNetworkOperation*) ), + this, SIGNAL( finished(QNetworkOperation*) ) ); + connect( gProt, SIGNAL( connectionStateChanged(int,const QString&) ), + this, SIGNAL( connectionStateChanged(int,const QString&) ) ); + + connect( pProt, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ), + this, SIGNAL( dataTransferProgress(int,int,QNetworkOperation*) ) ); + connect( pProt, SIGNAL( finished(QNetworkOperation*) ), + this, SIGNAL( finished(QNetworkOperation*) ) ); + connect( pProt, SIGNAL( finished(QNetworkOperation*) ), + this, SLOT( finishedCopy() ) ); + + QNetworkOperation *opGet = new QNetworkOperation( QNetworkProtocol::OpGet, frm, QString::null, QString::null ); + ops.append( opGet ); + gProt->addOperation( opGet ); + + + QString toFile = to + file; + if (!toPath) + toFile = to; + + QNetworkOperation *opPut = new QNetworkOperation( QNetworkProtocol::OpPut, toFile, QString::null, QString::null ); + ops.append( opPut ); + + d->getOpPutProtMap.insert( (void*)opGet, pProt ); + d->getOpGetProtMap.insert( (void*)opGet, gProt ); + d->getOpPutOpMap.insert( (void*)opGet, opPut ); + + if ( move && (gProt->supportedOperations()&QNetworkProtocol::OpRemove) ) { + gProt->setAutoDelete( FALSE ); + + QNetworkOperation *opRm = new QNetworkOperation( QNetworkProtocol::OpRemove, frm, QString::null, QString::null ); + ops.append( opRm ); + d->getOpRemoveOpMap.insert( (void*)opGet, opRm ); + } else { + gProt->setAutoDelete( TRUE ); + } +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: copy operation should start now..." ); +#endif + return ops; + } else { + QString msg; + if ( !gProt ) { + msg = tr( "The protocol `%1' is not supported" ).arg( uFrom->protocol() ); + } else if ( gProt->supportedOperations() & QNetworkProtocol::OpGet ) { + msg = tr( "The protocol `%1' does not support copying or moving files or directories" ).arg( uFrom->protocol() ); + } else if ( !pProt ) { + msg = tr( "The protocol `%1' is not supported" ).arg( uTo->protocol() ); + } else { + msg = tr( "The protocol `%1' does not support copying or moving files or directories" ).arg( uTo->protocol() ); + } + delete uFrom; + delete uTo; + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpGet, frm, to, QString::null ); + res->setState( QNetworkProtocol::StFailed ); + res->setProtocolDetail( msg ); + res->setErrorCode( (int)QNetworkProtocol::ErrUnsupported ); + emit finished( res ); + deleteOperation( res ); + } + + return ops; +} + +/*! + \overload + + Copies the \a files to the directory \a dest. If \a move is TRUE + the files are moved, not copied. \a dest must point to a + directory. + + This function calls copy() for each entry in \a files in turn. You + don't get a result from this function; each time a new copy + begins, startedNextCopy() is emitted, with a list of + QNetworkOperations that describe the new copy operation. +*/ + +void QUrlOperator::copy( const QStringList &files, const QString &dest, + bool move ) +{ + d->waitingCopies = files; + d->waitingCopiesDest = dest; + d->waitingCopiesMove = move; + + finishedCopy(); +} + +/*! + Returns TRUE if the URL is a directory; otherwise returns FALSE. + This may not always work correctly, if the protocol of the URL is + something other than file (local filesystem). If you pass a bool + pointer as the \a ok argument, \a *ok is set to TRUE if the result + of this function is known to be correct, and to FALSE otherwise. +*/ + +bool QUrlOperator::isDir( bool *ok ) +{ + if ( ok ) + *ok = TRUE; + if ( isLocalFile() ) { + if ( QFileInfo( path() ).isDir() ) + return TRUE; + else + return FALSE; + } + + if ( d->entryMap.contains( "." ) ) { + return d->entryMap[ "." ].isDir(); + } + // #### can assume that we are a directory? + if ( ok ) + *ok = FALSE; + return TRUE; +} + +/*! + Tells the network protocol to get data from \a location or, if + this is QString::null, to get data from the location to which this + URL points (see QUrl::fileName() and QUrl::encodedPathAndQuery()). + What happens then depends on the network protocol. The data() + signal is emitted when data comes in. Because it's unlikely that + all data will come in at once, it is common for multiple data() + signals to be emitted. The dataTransferProgress() signal is + emitted while processing the operation. At the end, finished() + (with success or failure) is emitted, so check the state of the + network operation object to see whether or not the operation was + successful. + + If \a location is QString::null, the path of this QUrlOperator + should point to a file when you use this operation. If \a location + is not empty, it can be a relative URL (a child of the path to + which the QUrlOperator points) or an absolute URL. + + For example, to get a web page you might do something like this: + + \code + QUrlOperator op( "http://www.whatever.org/cgi-bin/search.pl?cmd=Hello" ); + op.get(); + \endcode + + For most other operations, the path of the QUrlOperator must point + to a directory. If you want to download a file you could do the + following: + + \code + QUrlOperator op( "ftp://ftp.whatever.org/pub" ); + // do some other stuff like op.listChildren() or op.mkdir( "new_dir" ) + op.get( "a_file.txt" ); + \endcode + + This will get the data of ftp://ftp.whatever.org/pub/a_file.txt. + + \e Never do anything like this: + \code + QUrlOperator op( "http://www.whatever.org/cgi-bin" ); + op.get( "search.pl?cmd=Hello" ); // WRONG! + \endcode + + If \a location is not empty and relative it must not contain any + queries or references, just the name of a child. So if you need to + specify a query or reference, do it as shown in the first example + or specify the full URL (such as + http://www.whatever.org/cgi-bin/search.pl?cmd=Hello) as \a location. + + \sa copy() +*/ + +const QNetworkOperation *QUrlOperator::get( const QString &location ) +{ + QUrl u( *this ); + if ( !location.isEmpty() ) + u = QUrl( *this, location ); + + if ( !u.isValid() ) + return 0; + + if ( !d->networkProtocol ) { + setProtocol( u.protocol() ); + getNetworkProtocol(); + } + + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpGet, u, QString::null, QString::null ); + return startOperation( res ); +} + +/*! + This function tells the network protocol to put \a data in \a + location. If \a location is empty (QString::null), it puts the \a + data in the location to which the URL points. What happens depends + on the network protocol. Depending on the network protocol, some + data might come back after putting data, in which case the data() + signal is emitted. The dataTransferProgress() signal is emitted + during processing of the operation. At the end, finished() (with + success or failure) is emitted, so check the state of the network + operation object to see whether or not the operation was + successful. + + If \a location is QString::null, the path of this QUrlOperator + should point to a file when you use this operation. If \a location + is not empty, it can be a relative (a child of the path to which + the QUrlOperator points) or an absolute URL. + + For putting some data to a file you can do the following: + + \code + QUrlOperator op( "ftp://ftp.whatever.com/home/me/filename.dat" ); + op.put( data ); + \endcode + + For most other operations, the path of the QUrlOperator must point + to a directory. If you want to upload data to a file you could do + the following: + + \code + QUrlOperator op( "ftp://ftp.whatever.com/home/me" ); + // do some other stuff like op.listChildren() or op.mkdir( "new_dir" ) + op.put( data, "filename.dat" ); + \endcode + + This will upload the data to ftp://ftp.whatever.com/home/me/filename.dat. + + \sa copy() +*/ + +const QNetworkOperation *QUrlOperator::put( const QByteArray &data, const QString &location ) +{ + QUrl u( *this ); + if ( !location.isEmpty() ) + u = QUrl( *this, location ); + + if ( !u.isValid() ) + return 0; + + if ( !d->networkProtocol ) { + setProtocol( u.protocol() ); + getNetworkProtocol(); + } + + QNetworkOperation *res = new QNetworkOperation( QNetworkProtocol::OpPut, u, QString::null, QString::null ); + res->setRawArg( 1, data ); + return startOperation( res ); +} + +/*! + Sets the name filter of the URL to \a nameFilter. + + \sa QDir::setNameFilter() +*/ + +void QUrlOperator::setNameFilter( const QString &nameFilter ) +{ + d->nameFilter = nameFilter; +} + +/*! + Returns the name filter of the URL. + + \sa QUrlOperator::setNameFilter() QDir::nameFilter() +*/ + +QString QUrlOperator::nameFilter() const +{ + return d->nameFilter; +} + +/*! + Clears the cache of children. +*/ + +void QUrlOperator::clearEntries() +{ + d->entryMap.clear(); +} + +/*! + Adds an entry to the cache of children. +*/ + +void QUrlOperator::addEntry( const QValueList<QUrlInfo> &i ) +{ + QValueList<QUrlInfo>::ConstIterator it = i.begin(); + for ( ; it != i.end(); ++it ) + d->entryMap[ ( *it ).name().stripWhiteSpace() ] = *it; +} + +/*! + Returns the URL information for the child \a entry, or returns an + empty QUrlInfo object if there is no information available about + \a entry. Information about \a entry is only available after a successfully + finished listChildren() operation. +*/ + +QUrlInfo QUrlOperator::info( const QString &entry ) const +{ + if ( d->entryMap.contains( entry.stripWhiteSpace() ) ) { + return d->entryMap[ entry.stripWhiteSpace() ]; + } else if ( entry == "." || entry == ".." ) { + // return a faked QUrlInfo + QUrlInfo inf; + inf.setName( entry ); + inf.setDir( TRUE ); + inf.setFile( FALSE ); + inf.setSymLink( FALSE ); + inf.setOwner( tr( "(unknown)" ) ); + inf.setGroup( tr( "(unknown)" ) ); + inf.setSize( 0 ); + inf.setWritable( FALSE ); + inf.setReadable( TRUE ); + return inf; + } + return QUrlInfo(); +} + +/*! + Finds a network protocol for the URL and deletes the old network protocol. +*/ + +void QUrlOperator::getNetworkProtocol() +{ + delete d->networkProtocol; + QNetworkProtocol *p = QNetworkProtocol::getNetworkProtocol( protocol() ); + if ( !p ) { + d->networkProtocol = 0; + return; + } + + d->networkProtocol = (QNetworkProtocol *)p; + d->networkProtocol->setUrl( this ); + connect( d->networkProtocol, SIGNAL( itemChanged(QNetworkOperation*) ), + this, SLOT( slotItemChanged(QNetworkOperation*) ) ); +} + +/*! + Deletes the currently used network protocol. +*/ + +void QUrlOperator::deleteNetworkProtocol() +{ + if (d->networkProtocol) { + d->networkProtocol->deleteLater(); + d->networkProtocol = 0; + } +} + +/*! + \reimp +*/ + +void QUrlOperator::setPath( const QString& path ) +{ + QUrl::setPath( path ); + if ( d->networkProtocol ) + d->networkProtocol->setUrl( this ); +} + +/*! + \reimp +*/ + +void QUrlOperator::reset() +{ + QUrl::reset(); + deleteNetworkProtocol(); + d->nameFilter = "*"; +} + +/*! + \reimp +*/ + +bool QUrlOperator::parse( const QString &url ) +{ + bool b = QUrl::parse( url ); + if ( !b ) { + return b; + } + + getNetworkProtocol(); + + return b; +} + +/*! + \reimp +*/ + +QUrlOperator& QUrlOperator::operator=( const QUrlOperator &url ) +{ + deleteNetworkProtocol(); + QUrl::operator=( url ); + + QPtrDict<QNetworkOperation> getOpPutOpMap = d->getOpPutOpMap; + QPtrDict<QNetworkProtocol> getOpPutProtMap = d->getOpPutProtMap; + QPtrDict<QNetworkProtocol> getOpGetProtMap = d->getOpGetProtMap; + QPtrDict<QNetworkOperation> getOpRemoveOpMap = d->getOpRemoveOpMap; + + *d = *url.d; + + d->oldOps.setAutoDelete( FALSE ); + d->getOpPutOpMap = getOpPutOpMap; + d->getOpPutProtMap = getOpPutProtMap; + d->getOpGetProtMap = getOpGetProtMap; + d->getOpRemoveOpMap = getOpRemoveOpMap; + + d->networkProtocol = 0; + getNetworkProtocol(); + return *this; +} + +/*! + \reimp +*/ + +QUrlOperator& QUrlOperator::operator=( const QString &url ) +{ + deleteNetworkProtocol(); + QUrl::operator=( url ); + d->oldOps.setAutoDelete( FALSE ); + getNetworkProtocol(); + return *this; +} + +/*! + \reimp +*/ + +bool QUrlOperator::cdUp() +{ + bool b = QUrl::cdUp(); + if ( d->networkProtocol ) + d->networkProtocol->setUrl( this ); + return b; +} + +/*! + \reimp +*/ + +bool QUrlOperator::checkValid() +{ + // ###### + if ( !isValid() ) { + //emit error( ErrValid, tr( "The entered URL is not valid!" ) ); + return FALSE; + } else + return TRUE; +} + + +/*! + \internal +*/ + +void QUrlOperator::copyGotData( const QByteArray &data_, QNetworkOperation *op ) +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: copyGotData: %d new bytes", data_.size() ); +#endif + QNetworkOperation *put = d->getOpPutOpMap[ (void*)op ]; + if ( put ) { + QByteArray &s = put->raw( 1 ); + int size = s.size(); + s.resize( size + data_.size() ); + memcpy( s.data() + size, data_.data(), data_.size() ); + } + emit data( data_, op ); +} + +/*! + \internal +*/ + +void QUrlOperator::continueCopy( QNetworkOperation *op ) +{ + if ( op->operation() != QNetworkProtocol::OpGet ) + return; + if ( op->state()!=QNetworkProtocol::StDone && op->state()!=QNetworkProtocol::StFailed ) { + return; + } + +#ifdef QURLOPERATOR_DEBUG + if ( op->state() != QNetworkProtocol::StFailed ) { + qDebug( "QUrlOperator: continue copy (get finished, put will start)" ); + } +#endif + + QNetworkOperation *put = d->getOpPutOpMap[ (void*)op ]; + QNetworkProtocol *gProt = d->getOpGetProtMap[ (void*)op ]; + QNetworkProtocol *pProt = d->getOpPutProtMap[ (void*)op ]; + QNetworkOperation *rm = d->getOpRemoveOpMap[ (void*)op ]; + d->getOpPutOpMap.take( op ); + d->getOpGetProtMap.take( op ); + d->getOpPutProtMap.take( op ); + d->getOpRemoveOpMap.take( op ); + if ( pProt ) + pProt->setAutoDelete( TRUE ); + if ( put && pProt ) { + if ( op->state() != QNetworkProtocol::StFailed ) { + pProt->addOperation( put ); + d->currPut = pProt; + } else { + deleteOperation( put ); + } + } + if ( gProt ) { + gProt->setAutoDelete( TRUE ); + } + if ( rm && gProt ) { + if ( op->state() != QNetworkProtocol::StFailed ) { + gProt->addOperation( rm ); + } else { + deleteOperation( rm ); + } + } + disconnect( gProt, SIGNAL( data(const QByteArray&,QNetworkOperation*) ), + this, SLOT( copyGotData(const QByteArray&,QNetworkOperation*) ) ); + disconnect( gProt, SIGNAL( finished(QNetworkOperation*) ), + this, SLOT( continueCopy(QNetworkOperation*) ) ); +} + +/*! + \internal +*/ + +void QUrlOperator::finishedCopy() +{ +#ifdef QURLOPERATOR_DEBUG + qDebug( "QUrlOperator: finished copy (finished putting)" ); +#endif + + if ( d->waitingCopies.isEmpty() ) + return; + + QString cp = d->waitingCopies.first(); + d->waitingCopies.remove( cp ); + QPtrList<QNetworkOperation> lst = copy( cp, d->waitingCopiesDest, d->waitingCopiesMove ); + emit startedNextCopy( lst ); +} + +/*! + Stops the current network operation and removes all this + QUrlOperator's waiting network operations. +*/ + +void QUrlOperator::stop() +{ + d->getOpPutOpMap.clear(); + d->getOpRemoveOpMap.clear(); + d->getOpGetProtMap.setAutoDelete( TRUE ); + d->getOpPutProtMap.setAutoDelete( TRUE ); + QPtrDictIterator<QNetworkProtocol> it( d->getOpPutProtMap ); + for ( ; it.current(); ++it ) + it.current()->stop(); + d->getOpPutProtMap.clear(); + it = QPtrDictIterator<QNetworkProtocol>( d->getOpGetProtMap ); + for ( ; it.current(); ++it ) + it.current()->stop(); + d->getOpGetProtMap.clear(); + if ( d->currPut ) { + d->currPut->stop(); + delete (QNetworkProtocol *) d->currPut; + d->currPut = 0; + } + d->waitingCopies.clear(); + if ( d->networkProtocol ) + d->networkProtocol->stop(); + getNetworkProtocol(); +} + +/*! + \internal +*/ + +void QUrlOperator::deleteOperation( QNetworkOperation *op ) +{ + if ( op ) + d->oldOps.append( op ); +} + +/*! + \internal + updates the entryMap after a network operation finished +*/ + +void QUrlOperator::slotItemChanged( QNetworkOperation *op ) +{ + if ( !op ) + return; + + switch ( op->operation() ) { + case QNetworkProtocol::OpRename : + { + if ( op->arg( 0 ) == op->arg( 1 ) ) + return; + + QMap<QString, QUrlInfo>::iterator mi = d->entryMap.find( op->arg( 0 ) ); + if ( mi != d->entryMap.end() ) { + mi.data().setName( op->arg( 1 ) ); + d->entryMap[ op->arg( 1 ) ] = mi.data(); + d->entryMap.erase( mi ); + } + break; + } + case QNetworkProtocol::OpRemove : + { + QMap<QString, QUrlInfo>::iterator mi = d->entryMap.find( op->arg( 0 ) ); + if ( mi != d->entryMap.end() ) + d->entryMap.erase( mi ); + break; + } + default: + break; + } +} + + +#endif // QT_NO_NETWORKPROTOCOL diff --git a/src/kernel/qurloperator.h b/src/kernel/qurloperator.h new file mode 100644 index 0000000..fdd9dc6 --- /dev/null +++ b/src/kernel/qurloperator.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Definition of QUrlOperator class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QURLOPERATOR_H +#define QURLOPERATOR_H + +#ifndef QT_H +#include "qobject.h" +#include "qurl.h" +#include "qptrlist.h" +#include "qnetworkprotocol.h" +#include "qstringlist.h" // QString->QStringList conversion +#endif // QT_H + +#ifndef QT_NO_NETWORKPROTOCOL + +class QUrlInfo; +class QUrlOperatorPrivate; + +class Q_EXPORT QUrlOperator : public QObject, public QUrl +{ + friend class QNetworkProtocol; + + Q_OBJECT + +public: + QUrlOperator(); + QUrlOperator( const QString &urL ); + QUrlOperator( const QUrlOperator& url ); + QUrlOperator( const QUrlOperator& url, const QString& relUrl, bool checkSlash = FALSE ); + virtual ~QUrlOperator(); + + virtual void setPath( const QString& path ); + virtual bool cdUp(); + + virtual const QNetworkOperation *listChildren(); + virtual const QNetworkOperation *mkdir( const QString &dirname ); + virtual const QNetworkOperation *remove( const QString &filename ); + virtual const QNetworkOperation *rename( const QString &oldname, const QString &newname ); + virtual const QNetworkOperation *get( const QString &location = QString::null ); + virtual const QNetworkOperation *put( const QByteArray &data, const QString &location = QString::null ); + virtual QPtrList<QNetworkOperation> copy( const QString &from, const QString &to, bool move = FALSE, bool toPath = TRUE ); + virtual void copy( const QStringList &files, const QString &dest, bool move = FALSE ); + virtual bool isDir( bool *ok = 0 ); + + virtual void setNameFilter( const QString &nameFilter ); + QString nameFilter() const; + + virtual QUrlInfo info( const QString &entry ) const; + + QUrlOperator& operator=( const QUrlOperator &url ); + QUrlOperator& operator=( const QString &url ); + + virtual void stop(); + +signals: + void newChildren( const QValueList<QUrlInfo> &, QNetworkOperation *res ); + void finished( QNetworkOperation *res ); + void start( QNetworkOperation *res ); + void createdDirectory( const QUrlInfo &, QNetworkOperation *res ); + void removed( QNetworkOperation *res ); + void itemChanged( QNetworkOperation *res ); + void data( const QByteArray &, QNetworkOperation *res ); + void dataTransferProgress( int bytesDone, int bytesTotal, QNetworkOperation *res ); + void startedNextCopy( const QPtrList<QNetworkOperation> &lst ); + void connectionStateChanged( int state, const QString &data ); + +protected: + void reset(); + bool parse( const QString& url ); + virtual bool checkValid(); + virtual void clearEntries(); + void getNetworkProtocol(); + void deleteNetworkProtocol(); + +private slots: + const QNetworkOperation *startOperation( QNetworkOperation *op ); + void copyGotData( const QByteArray &data, QNetworkOperation *op ); + void continueCopy( QNetworkOperation *op ); + void finishedCopy(); + void addEntry( const QValueList<QUrlInfo> &i ); + void slotItemChanged( QNetworkOperation *op ); + +private: + void deleteOperation( QNetworkOperation *op ); + + QUrlOperatorPrivate *d; +}; + +#endif // QT_NO_NETWORKPROTOCOL + +#endif // QURLOPERATOR_H diff --git a/src/kernel/qvariant.cpp b/src/kernel/qvariant.cpp new file mode 100644 index 0000000..c9ea5b1 --- /dev/null +++ b/src/kernel/qvariant.cpp @@ -0,0 +1,3496 @@ +/**************************************************************************** +** +** Implementation of QVariant class +** +** Created : 990414 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 <float.h> + +#include "qvariant.h" +#ifndef QT_NO_VARIANT +#include "qstring.h" +#include "qcstring.h" +#include "qfont.h" +#include "qpixmap.h" +#include "qimage.h" +#include "qbrush.h" +#include "qpoint.h" +#include "qrect.h" +#include "qsize.h" +#include "qcolor.h" +#include "qpalette.h" +#include "qiconset.h" +#include "qdatastream.h" +#include "qregion.h" +#include "qpointarray.h" +#include "qbitmap.h" +#include "qcursor.h" +#include "qdatetime.h" +#include "qsizepolicy.h" +#include "qshared.h" +#include "qbitarray.h" +#include "qkeysequence.h" +#include "qpen.h" + +#ifndef DBL_DIG +#define DBL_DIG 10 +#endif //DBL_DIG + +// Uncomment to test for memory leaks or to run qt/test/qvariant/main.cpp +// #define QVARIANT_DEBUG + + +static bool isNumeric(QVariant::Type type) +{ + return (type == QVariant::Int || type == QVariant::UInt + || type == QVariant::Double || type == QVariant::LongLong + || type == QVariant::ULongLong || type == QVariant::Bool); +} + + +#ifdef QVARIANT_DEBUG +int qv_count = 0; +int get_qv_count() { return qv_count; } +#endif + +QVariant::Private::Private() +{ +#ifdef QVARIANT_DEBUG + qv_count++; +#endif + typ = QVariant::Invalid; + is_null = TRUE; +} + +QVariant::Private::Private( Private* d ) +{ +#ifdef QVARIANT_DEBUG + qv_count++; +#endif + + switch( d->typ ) + { + case QVariant::Invalid: + break; + case QVariant::Bitmap: + value.ptr = new QBitmap( *((QBitmap*)d->value.ptr) ); + break; + case QVariant::Region: + value.ptr = new QRegion( *((QRegion*)d->value.ptr) ); + // ## Force a detach + // ((QRegion*)value.ptr)->translate( 0, 0 ); + break; + case QVariant::PointArray: + // QPointArray is explicit shared + value.ptr = new QPointArray( *((QPointArray*)d->value.ptr) ); + break; + case QVariant::String: + value.ptr = new QString( *((QString*)d->value.ptr) ); + break; + case QVariant::CString: + // QCString is explicit shared + value.ptr = new QCString( *((QCString*)d->value.ptr) ); + break; +#ifndef QT_NO_STRINGLIST + case QVariant::StringList: + value.ptr = new QStringList( *((QStringList*)d->value.ptr) ); + break; +#endif //QT_NO_STRINGLIST + case QVariant::Font: + value.ptr = new QFont( *((QFont*)d->value.ptr) ); + break; + case QVariant::Pixmap: + value.ptr = new QPixmap( *((QPixmap*)d->value.ptr) ); + break; + case QVariant::Image: + // QImage is explicit shared + value.ptr = new QImage( *((QImage*)d->value.ptr) ); + break; + case QVariant::Brush: + value.ptr = new QBrush( *((QBrush*)d->value.ptr) ); + // ## Force a detach + // ((QBrush*)value.ptr)->setColor( ((QBrush*)value.ptr)->color() ); + break; + case QVariant::Point: + value.ptr = new QPoint( *((QPoint*)d->value.ptr) ); + break; + case QVariant::Rect: + value.ptr = new QRect( *((QRect*)d->value.ptr) ); + break; + case QVariant::Size: + value.ptr = new QSize( *((QSize*)d->value.ptr) ); + break; + case QVariant::Color: + value.ptr = new QColor( *((QColor*)d->value.ptr) ); + break; +#ifndef QT_NO_PALETTE + case QVariant::Palette: + value.ptr = new QPalette( *((QPalette*)d->value.ptr) ); + break; + case QVariant::ColorGroup: + value.ptr = new QColorGroup( *((QColorGroup*)d->value.ptr) ); + break; +#endif +#ifndef QT_NO_ICONSET + case QVariant::IconSet: + value.ptr = new QIconSet( *((QIconSet*)d->value.ptr) ); + break; +#endif +#ifndef QT_NO_TEMPLATE_VARIANT + case QVariant::Map: + value.ptr = new QMap<QString,QVariant>( *((QMap<QString,QVariant>*)d->value.ptr) ); + break; + case QVariant::List: + value.ptr = new QValueList<QVariant>( *((QValueList<QVariant>*)d->value.ptr) ); + break; +#endif + case QVariant::Date: + value.ptr = new QDate( *((QDate*)d->value.ptr) ); + break; + case QVariant::Time: + value.ptr = new QTime( *((QTime*)d->value.ptr) ); + break; + case QVariant::DateTime: + value.ptr = new QDateTime( *((QDateTime*)d->value.ptr) ); + break; + case QVariant::ByteArray: + value.ptr = new QByteArray( *((QByteArray*)d->value.ptr) ); + break; + case QVariant::BitArray: + value.ptr = new QBitArray( *((QBitArray*)d->value.ptr) ); + break; +#ifndef QT_NO_ACCEL + case QVariant::KeySequence: + value.ptr = new QKeySequence( *((QKeySequence*)d->value.ptr) ); + break; +#endif + case QVariant::Pen: + value.ptr = new QPen( *((QPen*)d->value.ptr) ); + break; + case QVariant::Int: + value.i = d->value.i; + break; + case QVariant::UInt: + value.u = d->value.u; + break; + case QVariant::LongLong: + value.ll = d->value.ll; + break; + case QVariant::ULongLong: + value.ull = d->value.ull; + break; + case QVariant::Bool: + value.b = d->value.b; + break; + case QVariant::Double: + value.d = d->value.d; + break; + case QVariant::SizePolicy: + value.ptr = new QSizePolicy( *((QSizePolicy*)d->value.ptr) ); + break; + case QVariant::Cursor: + value.ptr = new QCursor( *((QCursor*)d->value.ptr) ); + break; + default: + Q_ASSERT( 0 ); + } + + typ = d->typ; + is_null = d->is_null; +} + +QVariant::Private::~Private() +{ +#ifdef QVARIANT_DEBUG + qv_count--; +#endif + clear(); +} + +void QVariant::Private::clear() +{ + switch( typ ) + { + case QVariant::Bitmap: + delete (QBitmap*)value.ptr; + break; + case QVariant::Cursor: + delete (QCursor*)value.ptr; + break; + case QVariant::Region: + delete (QRegion*)value.ptr; + break; + case QVariant::PointArray: + delete (QPointArray*)value.ptr; + break; + case QVariant::String: + delete (QString*)value.ptr; + break; + case QVariant::CString: + delete (QCString*)value.ptr; + break; +#ifndef QT_NO_STRINGLIST + case QVariant::StringList: + delete (QStringList*)value.ptr; + break; +#endif //QT_NO_STRINGLIST + case QVariant::Font: + delete (QFont*)value.ptr; + break; + case QVariant::Pixmap: + delete (QPixmap*)value.ptr; + break; + case QVariant::Image: + delete (QImage*)value.ptr; + break; + case QVariant::Brush: + delete (QBrush*)value.ptr; + break; + case QVariant::Point: + delete (QPoint*)value.ptr; + break; + case QVariant::Rect: + delete (QRect*)value.ptr; + break; + case QVariant::Size: + delete (QSize*)value.ptr; + break; + case QVariant::Color: + delete (QColor*)value.ptr; + break; +#ifndef QT_NO_PALETTE + case QVariant::Palette: + delete (QPalette*)value.ptr; + break; + case QVariant::ColorGroup: + delete (QColorGroup*)value.ptr; + break; +#endif +#ifndef QT_NO_ICONSET + case QVariant::IconSet: + delete (QIconSet*)value.ptr; + break; +#endif +#ifndef QT_NO_TEMPLATE_VARIANT + case QVariant::Map: + delete (QMap<QString,QVariant>*)value.ptr; + break; + case QVariant::List: + delete (QValueList<QVariant>*)value.ptr; + break; +#endif + case QVariant::SizePolicy: + delete (QSizePolicy*)value.ptr; + break; + case QVariant::Date: + delete (QDate*)value.ptr; + break; + case QVariant::Time: + delete (QTime*)value.ptr; + break; + case QVariant::DateTime: + delete (QDateTime*)value.ptr; + break; + case QVariant::ByteArray: + delete (QByteArray*)value.ptr; + break; + case QVariant::BitArray: + delete (QBitArray*)value.ptr; + break; +#ifndef QT_NO_ACCEL + case QVariant::KeySequence: + delete (QKeySequence*)value.ptr; + break; +#endif + case QVariant::Pen: + delete (QPen*)value.ptr; + break; + case QVariant::Invalid: + case QVariant::Int: + case QVariant::UInt: + case QVariant::LongLong: + case QVariant::ULongLong: + case QVariant::Bool: + case QVariant::Double: + break; + } + + typ = QVariant::Invalid; + is_null = TRUE; +} + +/*! + \class QVariant qvariant.h + \brief The QVariant class acts like a union for the most common Qt data types. + + \ingroup objectmodel + \ingroup misc + \mainclass + + Because C++ forbids unions from including types that have + non-default constructors or destructors, most interesting Qt + classes cannot be used in unions. Without QVariant, this would be + a problem for QObject::property() and for database work, etc. + + A QVariant object holds a single value of a single type() at a + time. (Some type()s are multi-valued, for example a string list.) + You can find out what type, T, the variant holds, convert it to a + different type using one of the asT() functions, e.g. asSize(), + get its value using one of the toT() functions, e.g. toSize(), and + check whether the type can be converted to a particular type using + canCast(). + + The methods named toT() (for any supported T, see the \c Type + documentation for a list) are const. If you ask for the stored + type, they return a copy of the stored object. If you ask for a + type that can be generated from the stored type, toT() copies and + converts and leaves the object itself unchanged. If you ask for a + type that cannot be generated from the stored type, the result + depends on the type (see the function documentation for details). + + Note that three data types supported by QVariant are explicitly + shared, namely QImage, QPointArray, and QCString, and in these + cases the toT() methods return a shallow copy. In almost all cases + you must make a deep copy of the returned values before modifying + them. + + The asT() functions are not const. They do conversion like the + toT() methods, set the variant to hold the converted value, and + return a reference to the new contents of the variant. + + Here is some example code to demonstrate the use of QVariant: + + \code + QDataStream out(...); + QVariant v(123); // The variant now contains an int + int x = v.toInt(); // x = 123 + out << v; // Writes a type tag and an int to out + v = QVariant("hello"); // The variant now contains a QCString + v = QVariant(tr("hello"));// The variant now contains a QString + int y = v.toInt(); // y = 0 since v cannot be converted to an int + QString s = v.toString(); // s = tr("hello") (see QObject::tr()) + out << v; // Writes a type tag and a QString to out + ... + QDataStream in(...); // (opening the previously written stream) + in >> v; // Reads an Int variant + int z = v.toInt(); // z = 123 + qDebug("Type is %s", // prints "Type is int" + v.typeName()); + v.asInt() += 100; // The variant now hold the value 223. + v = QVariant( QStringList() ); + v.asStringList().append( "Hello" ); + \endcode + + You can even store QValueList<QVariant>s and + QMap<QString,QVariant>s in a variant, so you can easily construct + arbitrarily complex data structures of arbitrary types. This is + very powerful and versatile, but may prove less memory and speed + efficient than storing specific types in standard data structures. + + QVariant also supports the notion of NULL values, where you have a + defined type with no value set. + \code + QVariant x, y( QString() ), z( QString("") ); + x.asInt(); + // x.isNull() == TRUE, y.isNull() == TRUE, z.isNull() == FALSE + \endcode + + See the \link collection.html Collection Classes\endlink. +*/ + +/*! + \enum QVariant::Type + + This enum type defines the types of variable that a QVariant can + contain. + + \value Invalid no type + \value BitArray a QBitArray + \value ByteArray a QByteArray + \value Bitmap a QBitmap + \value Bool a bool + \value Brush a QBrush + \value Color a QColor + \value ColorGroup a QColorGroup + \value Cursor a QCursor + \value Date a QDate + \value DateTime a QDateTime + \value Double a double + \value Font a QFont + \value IconSet a QIconSet + \value Image a QImage + \value Int an int + \value KeySequence a QKeySequence + \value List a QValueList<QVariant> + \value LongLong a long long + \value ULongLong an unsigned long long + \value Map a QMap<QString,QVariant> + \value Palette a QPalette + \value Pen a QPen + \value Pixmap a QPixmap + \value Point a QPoint + \value PointArray a QPointArray + \value Rect a QRect + \value Region a QRegion + \value Size a QSize + \value SizePolicy a QSizePolicy + \value String a QString + \value CString a QCString + \value StringList a QStringList + \value Time a QTime + \value UInt an unsigned int + + Note that Qt's definition of bool depends on the compiler. + \c qglobal.h has the system-dependent definition of bool. +*/ + +/*! + Constructs an invalid variant. +*/ +QVariant::QVariant() +{ + d = new Private; +} + +/*! + Destroys the QVariant and the contained object. + + Note that subclasses that reimplement clear() should reimplement + the destructor to call clear(). This destructor calls clear(), but + because it is the destructor, QVariant::clear() is called rather + than a subclass's clear(). +*/ +QVariant::~QVariant() +{ + if ( d->deref() ) + delete d; +} + +/*! + Constructs a copy of the variant, \a p, passed as the argument to + this constructor. Usually this is a deep copy, but a shallow copy + is made if the stored data type is explicitly shared, as e.g. + QImage is. +*/ +QVariant::QVariant( const QVariant& p ) +{ + p.d->ref(); + d = p.d; +} + +#ifndef QT_NO_DATASTREAM +/*! + Reads the variant from the data stream, \a s. +*/ +QVariant::QVariant( QDataStream& s ) +{ + d = new Private; + s >> *this; +} +#endif //QT_NO_DATASTREAM + +/*! + Constructs a new variant with a string value, \a val. +*/ +QVariant::QVariant( const QString& val ) +{ + d = new Private; + d->typ = String; + d->value.ptr = new QString( val ); +} + +/*! + Constructs a new variant with a C-string value, \a val. + + If you want to modify the QCString after you've passed it to this + constructor, we recommend passing a deep copy (see + QCString::copy()). +*/ +QVariant::QVariant( const QCString& val ) +{ + d = new Private; + d->typ = CString; + d->value.ptr = new QCString( val ); +} + +/*! + Constructs a new variant with a C-string value of \a val if \a val + is non-null. The variant creates a deep copy of \a val. + + If \a val is null, the resulting variant has type Invalid. +*/ +QVariant::QVariant( const char* val ) +{ + d = new Private; + if ( val == 0 ) + return; + d->typ = CString; + d->value.ptr = new QCString( val ); +} + +#ifndef QT_NO_STRINGLIST +/*! + Constructs a new variant with a string list value, \a val. +*/ +QVariant::QVariant( const QStringList& val ) +{ + d = new Private; + d->typ = StringList; + d->value.ptr = new QStringList( val ); + d->is_null = FALSE; +} +#endif // QT_NO_STRINGLIST + +#ifndef QT_NO_TEMPLATE_VARIANT +/*! + Constructs a new variant with a map of QVariants, \a val. +*/ +QVariant::QVariant( const QMap<QString,QVariant>& val ) +{ + d = new Private; + d->typ = Map; + d->value.ptr = new QMap<QString,QVariant>( val ); + d->is_null = FALSE; +} +#endif +/*! + Constructs a new variant with a font value, \a val. +*/ +QVariant::QVariant( const QFont& val ) +{ + d = new Private; + d->typ = Font; + d->value.ptr = new QFont( val ); + d->is_null = FALSE; +} + +/*! + Constructs a new variant with a pixmap value, \a val. +*/ +QVariant::QVariant( const QPixmap& val ) +{ + d = new Private; + d->typ = Pixmap; + d->value.ptr = new QPixmap( val ); +} + + +/*! + Constructs a new variant with an image value, \a val. + + Because QImage is explicitly shared, you may need to pass a deep + copy to the variant using QImage::copy(), e.g. if you intend + changing the image you've passed later on. +*/ +QVariant::QVariant( const QImage& val ) +{ + d = new Private; + d->typ = Image; + d->value.ptr = new QImage( val ); +} + +/*! + Constructs a new variant with a brush value, \a val. +*/ +QVariant::QVariant( const QBrush& val ) +{ + d = new Private; + d->typ = Brush; + d->value.ptr = new QBrush( val ); + d->is_null = FALSE; +} + +/*! + Constructs a new variant with a point value, \a val. +*/ +QVariant::QVariant( const QPoint& val ) +{ + d = new Private; + d->typ = Point; + d->value.ptr = new QPoint( val ); +} + +/*! + Constructs a new variant with a rect value, \a val. +*/ +QVariant::QVariant( const QRect& val ) +{ + d = new Private; + d->typ = Rect; + d->value.ptr = new QRect( val ); +} + +/*! + Constructs a new variant with a size value, \a val. +*/ +QVariant::QVariant( const QSize& val ) +{ + d = new Private; + d->typ = Size; + d->value.ptr = new QSize( val ); +} + +/*! + Constructs a new variant with a color value, \a val. +*/ +QVariant::QVariant( const QColor& val ) +{ + d = new Private; + d->typ = Color; + d->value.ptr = new QColor( val ); + d->is_null = FALSE; +} + +#ifndef QT_NO_PALETTE +/*! + Constructs a new variant with a color palette value, \a val. +*/ +QVariant::QVariant( const QPalette& val ) +{ + d = new Private; + d->typ = Palette; + d->value.ptr = new QPalette( val ); + d->is_null = FALSE; +} + +/*! + Constructs a new variant with a color group value, \a val. +*/ +QVariant::QVariant( const QColorGroup& val ) +{ + d = new Private; + d->typ = ColorGroup; + d->value.ptr = new QColorGroup( val ); + d->is_null = FALSE; +} +#endif //QT_NO_PALETTE +#ifndef QT_NO_ICONSET +/*! + Constructs a new variant with an icon set value, \a val. +*/ +QVariant::QVariant( const QIconSet& val ) +{ + d = new Private; + d->typ = IconSet; + d->value.ptr = new QIconSet( val ); +} +#endif //QT_NO_ICONSET +/*! + Constructs a new variant with a region value, \a val. +*/ +QVariant::QVariant( const QRegion& val ) +{ + d = new Private; + d->typ = Region; + // ## Force a detach + d->value.ptr = new QRegion( val ); + ((QRegion*)d->value.ptr)->translate( 0, 0 ); +} + +/*! + Constructs a new variant with a bitmap value, \a val. +*/ +QVariant::QVariant( const QBitmap& val ) +{ + d = new Private; + d->typ = Bitmap; + d->value.ptr = new QBitmap( val ); +} + +/*! + Constructs a new variant with a cursor value, \a val. +*/ +QVariant::QVariant( const QCursor& val ) +{ + d = new Private; + d->typ = Cursor; + d->value.ptr = new QCursor( val ); + d->is_null = FALSE; +} + +/*! + Constructs a new variant with a point array value, \a val. + + Because QPointArray is explicitly shared, you may need to pass a + deep copy to the variant using QPointArray::copy(), e.g. if you + intend changing the point array you've passed later on. +*/ +QVariant::QVariant( const QPointArray& val ) +{ + d = new Private; + d->typ = PointArray; + d->value.ptr = new QPointArray( val ); +} + +/*! + Constructs a new variant with a date value, \a val. +*/ +QVariant::QVariant( const QDate& val ) +{ + d = new Private; + d->typ = Date; + d->value.ptr = new QDate( val ); +} + +/*! + Constructs a new variant with a time value, \a val. +*/ +QVariant::QVariant( const QTime& val ) +{ + d = new Private; + d->typ = Time; + d->value.ptr = new QTime( val ); +} + +/*! + Constructs a new variant with a date/time value, \a val. +*/ +QVariant::QVariant( const QDateTime& val ) +{ + d = new Private; + d->typ = DateTime; + d->value.ptr = new QDateTime( val ); +} + +/*! + Constructs a new variant with a bytearray value, \a val. +*/ +QVariant::QVariant( const QByteArray& val ) +{ + d = new Private; + d->typ = ByteArray; + d->value.ptr = new QByteArray( val ); +} + +/*! + Constructs a new variant with a bitarray value, \a val. +*/ +QVariant::QVariant( const QBitArray& val ) +{ + d = new Private; + d->typ = BitArray; + d->value.ptr = new QBitArray( val ); +} + +#ifndef QT_NO_ACCEL + +/*! + Constructs a new variant with a key sequence value, \a val. +*/ +QVariant::QVariant( const QKeySequence& val ) +{ + d = new Private; + d->typ = KeySequence; + d->value.ptr = new QKeySequence( val ); + d->is_null = FALSE; +} + +#endif + +/*! + Constructs a new variant with a pen value, \a val. +*/ +QVariant::QVariant( const QPen& val ) +{ + d = new Private; + d->typ = Pen; + d->value.ptr = new QPen( val ); +} + +/*! + Constructs a new variant with an integer value, \a val. +*/ +QVariant::QVariant( int val ) +{ + d = new Private; + d->typ = Int; + d->value.i = val; + d->is_null = FALSE; +} + +/*! + Constructs a new variant with an unsigned integer value, \a val. +*/ +QVariant::QVariant( uint val ) +{ + d = new Private; + d->typ = UInt; + d->value.u = val; + d->is_null = FALSE; +} + +/*! + Constructs a new variant with a long long integer value, \a val. +*/ +QVariant::QVariant( Q_LLONG val ) +{ + d = new Private; + d->typ = LongLong; + d->value.ll = val; + d->is_null = FALSE; +} + +/*! + Constructs a new variant with an unsigned long long integer value, \a val. +*/ + +QVariant::QVariant( Q_ULLONG val ) +{ + d = new Private; + d->typ = ULongLong; + d->value.ull = val; + d->is_null = FALSE; +} + +/*! + Constructs a new variant with a boolean value, \a val. The integer + argument is a dummy, necessary for compatibility with some + compilers. +*/ +QVariant::QVariant( bool val, int ) +{ // this is the comment that does NOT name said compiler. + d = new Private; + d->typ = Bool; + d->value.b = val; + d->is_null = FALSE; +} + + +/*! + Constructs a new variant with a floating point value, \a val. +*/ +QVariant::QVariant( double val ) +{ + d = new Private; + d->typ = Double; + d->value.d = val; + d->is_null = FALSE; +} + +#ifndef QT_NO_TEMPLATE_VARIANT +/*! + Constructs a new variant with a list value, \a val. +*/ +QVariant::QVariant( const QValueList<QVariant>& val ) +{ + d = new Private; + d->typ = List; + d->value.ptr = new QValueList<QVariant>( val ); + d->is_null = FALSE; +} +#endif + +/*! + Constructs a new variant with a size policy value, \a val. +*/ +QVariant::QVariant( QSizePolicy val ) +{ + d = new Private; + d->typ = SizePolicy; + d->value.ptr = new QSizePolicy( val ); + d->is_null = FALSE; +} + +/*! + Assigns the value of the variant \a variant to this variant. + + This is a deep copy of the variant, but note that if the variant + holds an explicitly shared type such as QImage, a shallow copy is + performed. +*/ +QVariant& QVariant::operator= ( const QVariant& variant ) +{ + QVariant& other = (QVariant&)variant; + + other.d->ref(); + if ( d->deref() ) + delete d; + + d = other.d; + + return *this; +} + +/*! + \internal +*/ +void QVariant::detach() +{ + if ( d->count == 1 ) + return; + + d->deref(); + d = new Private( d ); +} + +/*! + Returns the name of the type stored in the variant. The returned + strings describe the C++ datatype used to store the data: for + example, "QFont", "QString", or "QValueList<QVariant>". An Invalid + variant returns 0. +*/ +const char* QVariant::typeName() const +{ + return typeToName( (Type) d->typ ); +} + +/*! + Convert this variant to type Invalid and free up any resources + used. +*/ +void QVariant::clear() +{ + if ( d->count > 1 ) + { + d->deref(); + d = new Private; + return; + } + + d->clear(); +} + +/* Attention! + + For dependency reasons, this table is duplicated in moc.y. If you + change one, change both. + + (Search for the word 'Attention' in moc.y.) +*/ +static const int ntypes = 35; +static const char* const type_map[ntypes] = +{ + 0, + "QMap<QString,QVariant>", + "QValueList<QVariant>", + "QString", + "QStringList", + "QFont", + "QPixmap", + "QBrush", + "QRect", + "QSize", + "QColor", + "QPalette", + "QColorGroup", + "QIconSet", + "QPoint", + "QImage", + "int", + "uint", + "bool", + "double", + "QCString", + "QPointArray", + "QRegion", + "QBitmap", + "QCursor", + "QSizePolicy", + "QDate", + "QTime", + "QDateTime", + "QByteArray", + "QBitArray", + "QKeySequence", + "QPen", + "Q_LLONG", + "Q_ULLONG" +}; + + +/*! + Converts the enum representation of the storage type, \a typ, to + its string representation. +*/ +const char* QVariant::typeToName( Type typ ) +{ + if ( typ >= ntypes ) + return 0; + return type_map[typ]; +} + + +/*! + Converts the string representation of the storage type given in \a + name, to its enum representation. + + If the string representation cannot be converted to any enum + representation, the variant is set to \c Invalid. +*/ +QVariant::Type QVariant::nameToType( const char* name ) +{ + for ( int i = 0; i < ntypes; i++ ) { + if ( !qstrcmp( type_map[i], name ) ) + return (Type) i; + } + return Invalid; +} + +#ifndef QT_NO_DATASTREAM +/*! + Internal function for loading a variant from stream \a s. Use the + stream operators instead. + + \internal +*/ +void QVariant::load( QDataStream& s ) +{ + clear(); + Q_UINT32 u; + s >> u; + Type t = (Type)u; + + switch( t ) { + case Invalid: + { + // Since we wrote something, we should read something + QString x; + s >> x; + d->typ = t; + d->is_null = TRUE; + } + break; +#ifndef QT_NO_TEMPLATE_VARIANT + case Map: + { + QMap<QString,QVariant>* x = new QMap<QString,QVariant>; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; + case List: + { + QValueList<QVariant>* x = new QValueList<QVariant>; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; +#endif + case Cursor: + { +#ifndef QT_NO_CURSOR + QCursor* x = new QCursor; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; +#endif + } + break; + case Bitmap: + { + QBitmap* x = new QBitmap; +#ifndef QT_NO_IMAGEIO + s >> *x; +#endif + d->value.ptr = x; + } + break; + case Region: + { + QRegion* x = new QRegion; + s >> *x; + d->value.ptr = x; + } + break; + case PointArray: + { + QPointArray* x = new QPointArray; + s >> *x; + d->value.ptr = x; + } + break; + case String: + { + QString* x = new QString; + s >> *x; + d->value.ptr = x; + } + break; + case CString: + { + QCString* x = new QCString; + s >> *x; + d->value.ptr = x; + } + break; +#ifndef QT_NO_STRINGLIST + case StringList: + { + QStringList* x = new QStringList; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; +#endif // QT_NO_STRINGLIST + case Font: + { + QFont* x = new QFont; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; + case Pixmap: + { + QPixmap* x = new QPixmap; +#ifndef QT_NO_IMAGEIO + s >> *x; +#endif + d->value.ptr = x; + } + break; + case Image: + { + QImage* x = new QImage; +#ifndef QT_NO_IMAGEIO + s >> *x; +#endif + d->value.ptr = x; + } + break; + case Brush: + { + QBrush* x = new QBrush; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; + case Rect: + { + QRect* x = new QRect; + s >> *x; + d->value.ptr = x; + } + break; + case Point: + { + QPoint* x = new QPoint; + s >> *x; + d->value.ptr = x; + } + break; + case Size: + { + QSize* x = new QSize; + s >> *x; + d->value.ptr = x; + } + break; + case Color: + { + QColor* x = new QColor; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; +#ifndef QT_NO_PALETTE + case Palette: + { + QPalette* x = new QPalette; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; + case ColorGroup: + { + QColorGroup* x = new QColorGroup; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; +#endif +#ifndef QT_NO_ICONSET + case IconSet: + { + QPixmap x; + s >> x; + d->value.ptr = new QIconSet( x ); + } + break; +#endif + case Int: + { + int x; + s >> x; + d->value.i = x; + d->is_null = FALSE; + } + break; + case UInt: + { + uint x; + s >> x; + d->value.u = x; + d->is_null = FALSE; + } + break; + case LongLong: + { + Q_LLONG x; + s >> x; + d->value.ll = x; + } + break; + case ULongLong: + { + Q_ULLONG x; + s >> x; + d->value.ull = x; + } + break; + case Bool: + { + Q_INT8 x; + s >> x; + d->value.b = x; + d->is_null = FALSE; + } + break; + case Double: + { + double x; + s >> x; + d->value.d = x; + d->is_null = FALSE; + } + break; + case SizePolicy: + { + int h,v; + Q_INT8 hfw; + s >> h >> v >> hfw; + d->value.ptr = new QSizePolicy( (QSizePolicy::SizeType)h, + (QSizePolicy::SizeType)v, + (bool) hfw); + d->is_null = FALSE; + } + break; + case Date: + { + QDate* x = new QDate; + s >> *x; + d->value.ptr = x; + } + break; + case Time: + { + QTime* x = new QTime; + s >> *x; + d->value.ptr = x; + } + break; + case DateTime: + { + QDateTime* x = new QDateTime; + s >> *x; + d->value.ptr = x; + } + break; + case ByteArray: + { + QByteArray* x = new QByteArray; + s >> *x; + d->value.ptr = x; + } + break; + case BitArray: + { + QBitArray* x = new QBitArray; + s >> *x; + d->value.ptr = x; + } + break; +#ifndef QT_NO_ACCEL + case KeySequence: + { + QKeySequence* x = new QKeySequence; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; +#endif // QT_NO_ACCEL + case Pen: + { + QPen* x = new QPen; + s >> *x; + d->value.ptr = x; + d->is_null = FALSE; + } + break; + } + d->typ = t; +} + +/*! + Internal function for saving a variant to the stream \a s. Use the + stream operators instead. + + \internal +*/ +void QVariant::save( QDataStream& s ) const +{ + s << (Q_UINT32)type(); + + switch( d->typ ) { + case Cursor: + s << *((QCursor*)d->value.ptr); + break; + case Bitmap: +#ifndef QT_NO_IMAGEIO + s << *((QBitmap*)d->value.ptr); +#endif + break; + case PointArray: + s << *((QPointArray*)d->value.ptr); + break; + case Region: + s << *((QRegion*)d->value.ptr); + break; +#ifndef QT_NO_TEMPLATE_VARIANT + case List: + s << *((QValueList<QVariant>*)d->value.ptr); + break; + case Map: + s << *((QMap<QString,QVariant>*)d->value.ptr); + break; +#endif + case String: + s << *((QString*)d->value.ptr); + break; + case CString: + s << *((QCString*)d->value.ptr); + break; +#ifndef QT_NO_STRINGLIST + case StringList: + s << *((QStringList*)d->value.ptr); + break; +#endif + case Font: + s << *((QFont*)d->value.ptr); + break; + case Pixmap: +#ifndef QT_NO_IMAGEIO + s << *((QPixmap*)d->value.ptr); +#endif + break; + case Image: +#ifndef QT_NO_IMAGEIO + s << *((QImage*)d->value.ptr); +#endif + break; + case Brush: + s << *((QBrush*)d->value.ptr); + break; + case Point: + s << *((QPoint*)d->value.ptr); + break; + case Rect: + s << *((QRect*)d->value.ptr); + break; + case Size: + s << *((QSize*)d->value.ptr); + break; + case Color: + s << *((QColor*)d->value.ptr); + break; +#ifndef QT_NO_PALETTE + case Palette: + s << *((QPalette*)d->value.ptr); + break; + case ColorGroup: + s << *((QColorGroup*)d->value.ptr); + break; +#endif +#ifndef QT_NO_ICONSET + case IconSet: + //### add stream operator to iconset + s << ((QIconSet*)d->value.ptr)->pixmap(); + break; +#endif + case Int: + s << d->value.i; + break; + case UInt: + s << d->value.u; + break; + case LongLong: + s << d->value.ll; + break; + case ULongLong: + s << d->value.ull; + break; + case Bool: + s << (Q_INT8)d->value.b; + break; + case Double: + s << d->value.d; + break; + case SizePolicy: + { + QSizePolicy p = toSizePolicy(); + s << (int) p.horData() << (int) p.verData() + << (Q_INT8) p.hasHeightForWidth(); + } + break; + case Date: + s << *((QDate*)d->value.ptr); + break; + case Time: + s << *((QTime*)d->value.ptr); + break; + case DateTime: + s << *((QDateTime*)d->value.ptr); + break; + case ByteArray: + s << *((QByteArray*)d->value.ptr); + break; + case BitArray: + s << *((QBitArray*)d->value.ptr); + break; +#ifndef QT_NO_ACCEL + case KeySequence: + s << *((QKeySequence*)d->value.ptr); + break; +#endif + case Pen: + s << *((QPen*)d->value.ptr); + break; + case Invalid: + s << QString(); // ### looks wrong. + break; + } +} + +/*! + Reads a variant \a p from the stream \a s. + + \sa \link datastreamformat.html Format of the QDataStream + operators \endlink +*/ +QDataStream& operator>> ( QDataStream& s, QVariant& p ) +{ + p.load( s ); + return s; +} + +/*! + Writes a variant \a p to the stream \a s. + + \sa \link datastreamformat.html Format of the QDataStream + operators \endlink +*/ +QDataStream& operator<< ( QDataStream& s, const QVariant& p ) +{ + p.save( s ); + return s; +} + +/*! + Reads a variant type \a p in enum representation from the stream \a s. +*/ +QDataStream& operator>> ( QDataStream& s, QVariant::Type& p ) +{ + Q_UINT32 u; + s >> u; + p = (QVariant::Type) u; + + return s; +} + +/*! + Writes a variant type \a p to the stream \a s. +*/ +QDataStream& operator<< ( QDataStream& s, const QVariant::Type p ) +{ + s << (Q_UINT32)p; + + return s; +} + +#endif //QT_NO_DATASTREAM + +/*! + \fn Type QVariant::type() const + + Returns the storage type of the value stored in the variant. + Usually it's best to test with canCast() whether the variant can + deliver the data type you are interested in. +*/ + +/*! + \fn bool QVariant::isValid() const + + Returns TRUE if the storage type of this variant is not + QVariant::Invalid; otherwise returns FALSE. +*/ + +/*! + \fn QValueListConstIterator<QString> QVariant::stringListBegin() const + \obsolete + + Returns an iterator to the first string in the list if the + variant's type is StringList; otherwise returns a null iterator. +*/ + +/*! + \fn QValueListConstIterator<QString> QVariant::stringListEnd() const + \obsolete + + Returns the end iterator for the list if the variant's type is + StringList; otherwise returns a null iterator. +*/ + +/*! + \fn QValueListConstIterator<QVariant> QVariant::listBegin() const + \obsolete + + Returns an iterator to the first item in the list if the variant's + type is appropriate; otherwise returns a null iterator. +*/ + +/*! + \fn QValueListConstIterator<QVariant> QVariant::listEnd() const + \obsolete + + Returns the end iterator for the list if the variant's type is + appropriate; otherwise returns a null iterator. +*/ + +/*! + \fn QMapConstIterator<QString, QVariant> QVariant::mapBegin() const + \obsolete + + Returns an iterator to the first item in the map, if the variant's + type is appropriate; otherwise returns a null iterator. +*/ + +/*! + \fn QMapConstIterator<QString, QVariant> QVariant::mapEnd() const + \obsolete + + Returns the end iterator for the map, if the variant's type is + appropriate; otherwise returns a null iterator. +*/ + +/*! + \fn QMapConstIterator<QString, QVariant> QVariant::mapFind( const QString& key ) const + \obsolete + + Returns an iterator to the item in the map with \a key as key, if + the variant's type is appropriate and \a key is a valid key; + otherwise returns a null iterator. +*/ + +/*! + Returns the variant as a QString if the variant can be cast to + String, otherwise returns QString::null. + + \sa asString(), canCast() +*/ +const QString QVariant::toString() const +{ + switch( d->typ ) { + case CString: + return QString::fromLatin1( toCString() ); + case Int: + return QString::number( toInt() ); + case UInt: + return QString::number( toUInt() ); + case LongLong: + return QString::number( toLongLong() ); + case ULongLong: + return QString::number( toULongLong() ); + case Double: + return QString::number( toDouble(), 'g', DBL_DIG ); +#if !defined(QT_NO_SPRINTF) && !defined(QT_NO_DATESTRING) + case Date: + return toDate().toString( Qt::ISODate ); + case Time: + return toTime().toString( Qt::ISODate ); + case DateTime: + return toDateTime().toString( Qt::ISODate ); +#endif + case Bool: + return toInt() ? "true" : "false"; +#ifndef QT_NO_ACCEL + case KeySequence: + return (QString) *( (QKeySequence*)d->value.ptr ); +#endif + case ByteArray: + return QString( *((QByteArray*)d->value.ptr) ); + case Font: + return toFont().toString(); + case Color: + return toColor().name(); + case String: + return *((QString*)d->value.ptr); + default: + return QString::null; + } +} +/*! + Returns the variant as a QCString if the variant can be cast to a + CString; otherwise returns 0. + + \sa asCString(), canCast() +*/ +const QCString QVariant::toCString() const +{ + switch( d->typ ) { + case CString: return *((QCString*)d->value.ptr); + case String: return ((QString*)d->value.ptr)->latin1(); + default: { + if (!canCast(String)) + return 0; + QString c = toString(); + return QCString(c.latin1()); + } + } +} + + +#ifndef QT_NO_STRINGLIST +/*! + Returns the variant as a QStringList if the variant has type() + StringList or List of a type that can be converted to QString; + otherwise returns an empty list. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QStringList list = myVariant.toStringList(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa asStringList() +*/ +const QStringList QVariant::toStringList() const +{ + switch ( d->typ ) { + case StringList: + return *((QStringList*)d->value.ptr); +#ifndef QT_NO_TEMPLATE_VARIANT + case List: + { + QStringList lst; + QValueList<QVariant>::ConstIterator it = listBegin(); + QValueList<QVariant>::ConstIterator end = listEnd(); + while( it != end ) { + QString tmp = (*it).toString(); + ++it; + lst.append( tmp ); + } + return lst; + } +#endif + default: + return QStringList(); + } +} +#endif //QT_NO_STRINGLIST + +#ifndef QT_NO_TEMPLATE_VARIANT +/*! + Returns the variant as a QMap<QString,QVariant> if the variant has + type() Map; otherwise returns an empty map. + + Note that if you want to iterate over the map, you should iterate + over a copy, e.g. + \code + QMap<QString, QVariant> map = myVariant.toMap(); + QMap<QString, QVariant>::Iterator it = map.begin(); + while( it != map.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa asMap() +*/ +const QMap<QString, QVariant> QVariant::toMap() const +{ + if ( d->typ != Map ) + return QMap<QString,QVariant>(); + + return *((QMap<QString,QVariant>*)d->value.ptr); +} +#endif +/*! + Returns the variant as a QFont if the variant can be cast to Font; + otherwise returns the application's default font. + + \sa asFont(), canCast() +*/ +const QFont QVariant::toFont() const +{ + switch ( d->typ ) { + case CString: + case ByteArray: + case String: + { + QFont fnt; + fnt.fromString( toString() ); + return fnt; + } + case Font: + return *((QFont*)d->value.ptr); + default: + return QFont(); + } +} + +/*! + Returns the variant as a QPixmap if the variant has type() Pixmap; + otherwise returns a null pixmap. + + \sa asPixmap() +*/ +const QPixmap QVariant::toPixmap() const +{ + if ( d->typ != Pixmap ) + return QPixmap(); + + return *((QPixmap*)d->value.ptr); +} + +/*! + Returns the variant as a QImage if the variant has type() Image; + otherwise returns a null image. + + \sa asImage() +*/ +const QImage QVariant::toImage() const +{ + if ( d->typ != Image ) + return QImage(); + + return *((QImage*)d->value.ptr); +} + +/*! + Returns the variant as a QBrush if the variant has type() Brush; + otherwise returns a default brush (with all black colors). + + \sa asBrush() +*/ +const QBrush QVariant::toBrush() const +{ + if( d->typ != Brush ) + return QBrush(); + + return *((QBrush*)d->value.ptr); +} + +/*! + Returns the variant as a QPoint if the variant has type() Point; + otherwise returns a point (0, 0). + + \sa asPoint() +*/ +const QPoint QVariant::toPoint() const +{ + if ( d->typ != Point ) + return QPoint(); + + return *((QPoint*)d->value.ptr); +} + +/*! + Returns the variant as a QRect if the variant has type() Rect; + otherwise returns an empty rectangle. + + \sa asRect() +*/ +const QRect QVariant::toRect() const +{ + if ( d->typ != Rect ) + return QRect(); + + return *((QRect*)d->value.ptr); +} + +/*! + Returns the variant as a QSize if the variant has type() Size; + otherwise returns an invalid size. + + \sa asSize() +*/ +const QSize QVariant::toSize() const +{ + if ( d->typ != Size ) + return QSize(); + + return *((QSize*)d->value.ptr); +} + +/*! + Returns the variant as a QColor if the variant can be cast to Color; + otherwise returns an invalid color. + + \sa asColor(), canCast() +*/ +const QColor QVariant::toColor() const +{ + switch ( d->typ ) { + case ByteArray: + case CString: + case String: + { + QColor col; + col.setNamedColor( toString() ); + return col; + } + case Color: + return *((QColor*)d->value.ptr); + default: + return QColor(); + } +} +#ifndef QT_NO_PALETTE +/*! + Returns the variant as a QPalette if the variant has type() + Palette; otherwise returns a completely black palette. + + \sa asPalette() +*/ +const QPalette QVariant::toPalette() const +{ + if ( d->typ != Palette ) + return QPalette(); + + return *((QPalette*)d->value.ptr); +} + +/*! + Returns the variant as a QColorGroup if the variant has type() + ColorGroup; otherwise returns a completely black color group. + + \sa asColorGroup() +*/ +const QColorGroup QVariant::toColorGroup() const +{ + if ( d->typ != ColorGroup ) + return QColorGroup(); + + return *((QColorGroup*)d->value.ptr); +} +#endif //QT_NO_PALETTE +#ifndef QT_NO_ICONSET +/*! + Returns the variant as a QIconSet if the variant has type() + IconSet; otherwise returns an icon set of null pixmaps. + + \sa asIconSet() +*/ +const QIconSet QVariant::toIconSet() const +{ + if ( d->typ != IconSet ) + return QIconSet(); + + return *((QIconSet*)d->value.ptr); +} +#endif //QT_NO_ICONSET +/*! + Returns the variant as a QPointArray if the variant has type() + PointArray; otherwise returns an empty QPointArray. + + \sa asPointArray() +*/ +const QPointArray QVariant::toPointArray() const +{ + if ( d->typ != PointArray ) + return QPointArray(); + + return *((QPointArray*)d->value.ptr); +} + +/*! + Returns the variant as a QBitmap if the variant has type() Bitmap; + otherwise returns a null QBitmap. + + \sa asBitmap() +*/ +const QBitmap QVariant::toBitmap() const +{ + if ( d->typ != Bitmap ) + return QBitmap(); + + return *((QBitmap*)d->value.ptr); +} + +/*! + Returns the variant as a QRegion if the variant has type() Region; + otherwise returns an empty QRegion. + + \sa asRegion() +*/ +const QRegion QVariant::toRegion() const +{ + if ( d->typ != Region ) + return QRegion(); + + return *((QRegion*)d->value.ptr); +} + +/*! + Returns the variant as a QCursor if the variant has type() Cursor; + otherwise returns the default arrow cursor. + + \sa asCursor() +*/ +const QCursor QVariant::toCursor() const +{ +#ifndef QT_NO_CURSOR + if ( d->typ != Cursor ) + return QCursor(); +#endif + + return *((QCursor*)d->value.ptr); +} + +/*! + Returns the variant as a QDate if the variant can be cast to Date; + otherwise returns an invalid date. + + Note that if the type() is String, CString or ByteArray an invalid + date will be returned if the string cannot be parsed as a + Qt::ISODate format date. + + \sa asDate(), canCast() +*/ +const QDate QVariant::toDate() const +{ + switch ( d->typ ) { + case Date: + return *((QDate*)d->value.ptr); + case DateTime: + return ((QDateTime*)d->value.ptr)->date(); +#ifndef QT_NO_DATESTRING + case String: + return QDate::fromString( *((QString*)d->value.ptr), Qt::ISODate ); + case CString: + case ByteArray: + return QDate::fromString(toString(), Qt::ISODate); +#endif + default: + return QDate(); + } +} + +/*! + Returns the variant as a QTime if the variant can be cast to Time; + otherwise returns an invalid date. + + Note that if the type() is String, CString or ByteArray an invalid + time will be returned if the string cannot be parsed as a + Qt::ISODate format time. + + \sa asTime() +*/ +const QTime QVariant::toTime() const +{ + switch ( d->typ ) { + case Time: + return *((QTime*)d->value.ptr); + case DateTime: + return ((QDateTime*)d->value.ptr)->time(); +#ifndef QT_NO_DATESTRING + case String: + return QTime::fromString( *((QString*)d->value.ptr), Qt::ISODate ); + case CString: + case ByteArray: + return QTime::fromString(toString(), Qt::ISODate); +#endif + default: + return QTime(); + } +} + +/*! + Returns the variant as a QDateTime if the variant can be cast to + DateTime; otherwise returns an invalid QDateTime. + + Note that if the type() is String, CString or ByteArray an invalid + QDateTime will be returned if the string cannot be parsed as a + Qt::ISODate format date/time. + + \sa asDateTime() +*/ +const QDateTime QVariant::toDateTime() const +{ + switch ( d->typ ) { + case DateTime: + return *((QDateTime*)d->value.ptr); +#ifndef QT_NO_DATESTRING + case String: + return QDateTime::fromString( *((QString*)d->value.ptr), Qt::ISODate ); + case CString: + case ByteArray: + return QDateTime::fromString(toString(), Qt::ISODate); +#endif + case Date: + return QDateTime( *((QDate*)d->value.ptr) ); + default: + return QDateTime(); + } +} + +/*! + Returns the variant as a QByteArray if the variant can be cast to + a ByteArray; otherwise returns an empty bytearray. + + \sa asByteArray(), canCast() +*/ +const QByteArray QVariant::toByteArray() const +{ + switch(d->typ) { + case ByteArray: return *((QByteArray*)d->value.ptr); + case CString: return *((QByteArray*)d->value.ptr); + default: { + QByteArray ret; + if (canCast(String)) { + QString c = toString(); + ret.duplicate(c.latin1(), c.length()); + } + return ret; + } + } +} + +/*! + Returns the variant as a QBitArray if the variant has type() + BitArray; otherwise returns an empty bitarray. + + \sa asBitArray() +*/ +const QBitArray QVariant::toBitArray() const +{ + if ( d->typ == BitArray ) + return *((QBitArray*)d->value.ptr); + return QBitArray(); +} + +#ifndef QT_NO_ACCEL + +/*! + Returns the variant as a QKeySequence if the variant can be cast + to a KeySequence; otherwise returns an empty key sequence. + + \sa asKeySequence(), canCast() +*/ +const QKeySequence QVariant::toKeySequence() const +{ + switch ( d->typ ) { + case KeySequence: + return *((QKeySequence*)d->value.ptr); + case String: + case ByteArray: + case CString: + return QKeySequence( toString() ); + case Int: + case UInt: + case Double: + case ULongLong: + case LongLong: + return QKeySequence( toInt() ); + default: + return QKeySequence(); + } +} + +#endif // QT_NO_ACCEL + +/*! + Returns the variant as a QPen if the variant has type() + Pen; otherwise returns an empty QPen. + + \sa asPen() +*/ +const QPen QVariant::toPen() const +{ + if ( d->typ != Pen ) + return QPen(); + + return *((QPen*)d->value.ptr); +} + +/*! + Returns the variant as an int if the variant can be cast to Int; + otherwise returns 0. + + If \a ok is non-null: \a *ok is set to TRUE if the value could be + converted to an int; otherwise \a *ok is set to FALSE. + + \sa asInt(), canCast() +*/ +int QVariant::toInt( bool * ok ) const +{ + if ( ok ) + *ok = canCast( Int ); + + switch ( d->typ ) { + case String: + return ((QString*)d->value.ptr)->toInt( ok ); + case CString: + case ByteArray: + return ((QCString*)d->value.ptr)->toInt( ok ); + case Int: + return d->value.i; + case UInt: + return (int)d->value.u; + case LongLong: + return (int)d->value.ll; + case ULongLong: + return (int)d->value.ull; + case Double: + return (int)d->value.d; + case Bool: + return (int)d->value.b; +#ifndef QT_NO_ACCEL + case KeySequence: + return (int) *( (QKeySequence*)d->value.ptr ); +#endif + default: + return 0; + } +} + +/*! + Returns the variant as an unsigned int if the variant can be cast + to UInt; otherwise returns 0. + + If \a ok is non-null: \a *ok is set to TRUE if the value could be + converted to an unsigned int; otherwise \a *ok is set to FALSE. + + \sa asUInt(), canCast() +*/ +uint QVariant::toUInt( bool * ok ) const +{ + if ( ok ) + *ok = canCast( UInt ); + + switch( d->typ ) { + case String: + return ((QString*)d->value.ptr)->toUInt( ok ); + case CString: + case ByteArray: + return ((QCString*)d->value.ptr)->toUInt( ok ); + case Int: + return (uint)d->value.i; + case UInt: + return d->value.u; + case LongLong: + return (uint)d->value.ll; + case ULongLong: + return (uint)d->value.ull; + case Double: + return (uint)d->value.d; + case Bool: + return (uint)d->value.b; + default: + return 0; + } +} + +/*! + Returns the variant as a long long int if the variant can be cast + to LongLong; otherwise returns 0. + + If \a ok is non-null: \a *ok is set to TRUE if the value could be + converted to an int; otherwise \a *ok is set to FALSE. + + \sa asLongLong(), canCast() +*/ +Q_LLONG QVariant::toLongLong( bool * ok ) const +{ + if ( ok ) + *ok = canCast( LongLong ); + + switch ( d->typ ) { + case String: + return ((QString*)d->value.ptr)->toLongLong( ok ); + case CString: + case ByteArray: + return QString(*(QCString*)d->value.ptr).toLongLong(ok); + case Int: + return (Q_LLONG)d->value.i; + case UInt: + return (Q_LLONG)d->value.u; + case LongLong: + return d->value.ll; + case ULongLong: + return (Q_LLONG)d->value.ull; + case Double: + return (Q_LLONG)d->value.d; + case Bool: + return (Q_LLONG)d->value.b; + default: + return 0; + } +} + +/*! + Returns the variant as as an unsigned long long int if the variant + can be cast to ULongLong; otherwise returns 0. + + If \a ok is non-null: \a *ok is set to TRUE if the value could be + converted to an int; otherwise \a *ok is set to FALSE. + + \sa asULongLong(), canCast() +*/ +Q_ULLONG QVariant::toULongLong( bool * ok ) const +{ + if ( ok ) + *ok = canCast( ULongLong ); + + switch ( d->typ ) { + case Int: + return (Q_ULLONG)d->value.i; + case UInt: + return (Q_ULLONG)d->value.u; + case LongLong: + return (Q_ULLONG)d->value.ll; + case ULongLong: + return d->value.ull; + case Double: + return (Q_ULLONG)d->value.d; + case Bool: + return (Q_ULLONG)d->value.b; + case String: + return ((QString*)d->value.ptr)->toULongLong( ok ); + case CString: + case ByteArray: + return QString(*(QCString*)d->value.ptr).toULongLong(ok); + default: + return 0; + } +} + +/*! + Returns the variant as a bool if the variant can be cast to Bool; + otherWise returns FALSE. + + Returns TRUE if the variant has a numeric type and its value is + non-zero, or if the variant has type String, ByteArray or CString + and its lower-case content is not empty, "0" or "false"; otherwise + returns FALSE. + + \sa asBool(), canCast() +*/ +bool QVariant::toBool() const +{ + switch( d->typ ) { + case Bool: + return d->value.b; + case Double: + return d->value.d != 0.0; + case Int: + return d->value.i != 0; + case UInt: + return d->value.u != 0; + case LongLong: + return d->value.ll != 0; + case ULongLong: + return d->value.ull != 0; + case String: + case CString: + case ByteArray: + { + QString str = toString().lower(); + return !(str == "0" || str == "false" || str.isEmpty() ); + } + default: + return FALSE; + } +} + +/*! + Returns the variant as a double if the variant can be cast to + Double; otherwise returns 0.0. + + If \a ok is non-null: \a *ok is set to TRUE if the value could be + converted to a double; otherwise \a *ok is set to FALSE. + + \sa asDouble(), canCast() +*/ +double QVariant::toDouble( bool * ok ) const +{ + if ( ok ) + *ok = canCast( Double ); + + switch ( d->typ ) { + case String: + return ((QString*)d->value.ptr)->toDouble( ok ); + case CString: + case ByteArray: + return ((QCString*)d->value.ptr)->toDouble( ok ); + case Double: + return d->value.d; + case Int: + return (double)d->value.i; + case Bool: + return (double)d->value.b; + case UInt: + return (double)d->value.u; + case LongLong: + return (double)d->value.ll; + case ULongLong: +#if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET) + return (double)(Q_LLONG)d->value.ull; +#else + return (double)d->value.ull; +#endif + default: + return 0.0; + } +} + +#ifndef QT_NO_TEMPLATE_VARIANT +/*! + Returns the variant as a QValueList<QVariant> if the variant has + type() List or StringList; otherwise returns an empty list. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QValueList<QVariant> list = myVariant.toList(); + QValueList<QVariant>::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa asList() +*/ +const QValueList<QVariant> QVariant::toList() const +{ + if ( d->typ == List ) + return *((QValueList<QVariant>*)d->value.ptr); +#ifndef QT_NO_STRINGLIST + if ( d->typ == StringList ) { + QValueList<QVariant> lst; + QStringList::ConstIterator it = stringListBegin(); + QStringList::ConstIterator end = stringListEnd(); + for( ; it != end; ++it ) + lst.append( QVariant( *it ) ); + return lst; + } +#endif //QT_NO_STRINGLIST + return QValueList<QVariant>(); +} +#endif + +/*! + Returns the variant as a QSizePolicy if the variant has type() + SizePolicy; otherwise returns an undefined (but legal) size + policy. +*/ + +QSizePolicy QVariant::toSizePolicy() const +{ + if ( d->typ == SizePolicy ) + return *((QSizePolicy*)d->value.ptr); + + return QSizePolicy(); +} + + +#define Q_VARIANT_AS( f ) Q##f& QVariant::as##f() \ +{ \ + bool b = isNull(); \ + if ( d->typ != f ) \ + *this = QVariant( to##f() ); \ + else \ + detach(); \ + d->is_null = b; \ + return *((Q##f*)d->value.ptr); \ +} + +Q_VARIANT_AS(String) +Q_VARIANT_AS(CString) +#ifndef QT_NO_STRINGLIST +Q_VARIANT_AS(StringList) +#endif +Q_VARIANT_AS(Font) +Q_VARIANT_AS(Pixmap) +Q_VARIANT_AS(Image) +Q_VARIANT_AS(Brush) +Q_VARIANT_AS(Point) +Q_VARIANT_AS(Rect) +Q_VARIANT_AS(Size) +Q_VARIANT_AS(Color) +#ifndef QT_NO_PALETTE +Q_VARIANT_AS(Palette) +Q_VARIANT_AS(ColorGroup) +#endif +#ifndef QT_NO_ICONSET +Q_VARIANT_AS(IconSet) +#endif +Q_VARIANT_AS(PointArray) +Q_VARIANT_AS(Bitmap) +Q_VARIANT_AS(Region) +Q_VARIANT_AS(Cursor) +Q_VARIANT_AS(SizePolicy) +Q_VARIANT_AS(Date) +Q_VARIANT_AS(Time) +Q_VARIANT_AS(DateTime) +Q_VARIANT_AS(ByteArray) +Q_VARIANT_AS(BitArray) +#ifndef QT_NO_ACCEL +Q_VARIANT_AS(KeySequence) +#endif +Q_VARIANT_AS(Pen) + +/*! + \fn QString& QVariant::asString() + + Tries to convert the variant to hold a string value. If that is + not possible the variant is set to an empty string. + + Returns a reference to the stored string. + + \sa toString() +*/ + +/*! + \fn QCString& QVariant::asCString() + + Tries to convert the variant to hold a string value. If that is + not possible the variant is set to an empty string. + + Returns a reference to the stored string. + + \sa toCString() +*/ + +/*! + \fn QStringList& QVariant::asStringList() + + Tries to convert the variant to hold a QStringList value. If that + is not possible the variant is set to an empty string list. + + Returns a reference to the stored string list. + + Note that if you want to iterate over the list, you should + iterate over a copy, e.g. + \code + QStringList list = myVariant.asStringList(); + QStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa toStringList() +*/ + +/*! + \fn QFont& QVariant::asFont() + + Tries to convert the variant to hold a QFont. If that is not + possible the variant is set to the application's default font. + + Returns a reference to the stored font. + + \sa toFont() +*/ + +/*! + \fn QPixmap& QVariant::asPixmap() + + Tries to convert the variant to hold a pixmap value. If that is + not possible the variant is set to a null pixmap. + + Returns a reference to the stored pixmap. + + \sa toPixmap() +*/ + +/*! + \fn QImage& QVariant::asImage() + + Tries to convert the variant to hold an image value. If that is + not possible the variant is set to a null image. + + Returns a reference to the stored image. + + \sa toImage() +*/ + +/*! + \fn QBrush& QVariant::asBrush() + + Tries to convert the variant to hold a brush value. If that is not + possible the variant is set to a default black brush. + + Returns a reference to the stored brush. + + \sa toBrush() +*/ + +/*! + \fn QPoint& QVariant::asPoint() + + Tries to convert the variant to hold a point value. If that is not + possible the variant is set to a (0, 0) point. + + Returns a reference to the stored point. + + \sa toPoint() +*/ + +/*! + \fn QRect& QVariant::asRect() + + Tries to convert the variant to hold a rectangle value. If that is + not possible the variant is set to an empty rectangle. + + Returns a reference to the stored rectangle. + + \sa toRect() +*/ + +/*! + \fn QSize& QVariant::asSize() + + Tries to convert the variant to hold a QSize value. If that is not + possible the variant is set to an invalid size. + + Returns a reference to the stored size. + + \sa toSize() QSize::isValid() +*/ + +/*! + \fn QSizePolicy& QVariant::asSizePolicy() + + Tries to convert the variant to hold a QSizePolicy value. If that + fails, the variant is set to an arbitrary (valid) size policy. +*/ + + +/*! + \fn QColor& QVariant::asColor() + + Tries to convert the variant to hold a QColor value. If that is + not possible the variant is set to an invalid color. + + Returns a reference to the stored color. + + \sa toColor() QColor::isValid() +*/ + +/*! + \fn QPalette& QVariant::asPalette() + + Tries to convert the variant to hold a QPalette value. If that is + not possible the variant is set to a palette of black colors. + + Returns a reference to the stored palette. + + \sa toString() +*/ + +/*! + \fn QColorGroup& QVariant::asColorGroup() + + Tries to convert the variant to hold a QColorGroup value. If that + is not possible the variant is set to a color group of all black + colors. + + Returns a reference to the stored color group. + + \sa toColorGroup() +*/ + +/*! + \fn QIconSet& QVariant::asIconSet() + + Tries to convert the variant to hold a QIconSet value. If that is + not possible the variant is set to an empty iconset. + + Returns a reference to the stored iconset. + + \sa toIconSet() +*/ + +/*! + \fn QPointArray& QVariant::asPointArray() + + Tries to convert the variant to hold a QPointArray value. If that + is not possible the variant is set to an empty point array. + + Returns a reference to the stored point array. + + \sa toPointArray() +*/ + +/*! + \fn QBitmap& QVariant::asBitmap() + + Tries to convert the variant to hold a bitmap value. If that is + not possible the variant is set to a null bitmap. + + Returns a reference to the stored bitmap. + + \sa toBitmap() +*/ + +/*! + \fn QRegion& QVariant::asRegion() + + Tries to convert the variant to hold a QRegion value. If that is + not possible the variant is set to a null region. + + Returns a reference to the stored region. + + \sa toRegion() +*/ + +/*! + \fn QCursor& QVariant::asCursor() + + Tries to convert the variant to hold a QCursor value. If that is + not possible the variant is set to a default arrow cursor. + + Returns a reference to the stored cursor. + + \sa toCursor() +*/ + +/*! + \fn QDate& QVariant::asDate() + + Tries to convert the variant to hold a QDate value. If that is not + possible then the variant is set to an invalid date. + + Returns a reference to the stored date. + + \sa toDate() +*/ + +/*! + \fn QTime& QVariant::asTime() + + Tries to convert the variant to hold a QTime value. If that is not + possible then the variant is set to an invalid time. + + Returns a reference to the stored time. + + \sa toTime() +*/ + +/*! + \fn QDateTime& QVariant::asDateTime() + + Tries to convert the variant to hold a QDateTime value. If that is + not possible then the variant is set to an invalid date/time. + + Returns a reference to the stored date/time. + + \sa toDateTime() +*/ + +/*! + \fn QByteArray& QVariant::asByteArray() + + Tries to convert the variant to hold a QByteArray value. If that + is not possible then the variant is set to an empty bytearray. + + Returns a reference to the stored bytearray. + + \sa toByteArray() +*/ + +/*! + \fn QBitArray& QVariant::asBitArray() + + Tries to convert the variant to hold a QBitArray value. If that is + not possible then the variant is set to an empty bitarray. + + Returns a reference to the stored bitarray. + + \sa toBitArray() +*/ + +/*! + \fn QKeySequence& QVariant::asKeySequence() + + Tries to convert the variant to hold a QKeySequence value. If that + is not possible then the variant is set to an empty key sequence. + + Returns a reference to the stored key sequence. + + \sa toKeySequence() +*/ + +/*! \fn QPen& QVariant::asPen() + + Tries to convert the variant to hold a QPen value. If that + is not possible then the variant is set to an empty pen. + + Returns a reference to the stored pen. + + \sa toPen() +*/ + +/*! + Returns the variant's value as int reference. +*/ +int& QVariant::asInt() +{ + detach(); + if ( d->typ != Int ) { + int i = toInt(); + bool b = isNull(); + d->clear(); + d->value.i = i; + d->typ = Int; + d->is_null = b; + } + return d->value.i; +} + +/*! + Returns the variant's value as unsigned int reference. +*/ +uint& QVariant::asUInt() +{ + detach(); + if ( d->typ != UInt ) { + uint u = toUInt(); + bool b = isNull(); + d->clear(); + d->value.u = u; + d->typ = UInt; + d->is_null = b; + } + return d->value.u; +} + +/*! + Returns the variant's value as long long reference. +*/ +Q_LLONG& QVariant::asLongLong() +{ + detach(); + if ( d->typ != LongLong ) { + Q_LLONG ll = toLongLong(); + bool b = isNull(); + d->clear(); + d->value.ll = ll; + d->typ = LongLong; + d->is_null = b; + } + return d->value.ll; +} + +/*! + Returns the variant's value as unsigned long long reference. +*/ +Q_ULLONG& QVariant::asULongLong() +{ + detach(); + if ( d->typ != ULongLong ) { + Q_ULLONG ull = toULongLong(); + bool b = isNull(); + d->clear(); + d->value.ull = ull; + d->typ = ULongLong; + d->is_null = b; + } + return d->value.ull; +} + +/*! + Returns the variant's value as bool reference. +*/ +bool& QVariant::asBool() +{ + detach(); + if ( d->typ != Bool ) { + bool b = toBool(); + bool nb = isNull(); + d->clear(); + d->value.b = b; + d->typ = Bool; + d->is_null = nb; + } + return d->value.b; +} + +/*! + Returns the variant's value as double reference. +*/ +double& QVariant::asDouble() +{ + detach(); + if ( d->typ != Double ) { + double dbl = toDouble(); + bool b = isNull(); + d->clear(); + d->value.d = dbl; + d->typ = Double; + d->is_null = b; + } + return d->value.d; +} + +#ifndef QT_NO_TEMPLATE_VARIANT +/*! + Returns the variant's value as variant list reference. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + QValueList<QVariant> list = myVariant.asList(); + QValueList<QVariant>::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode +*/ +QValueList<QVariant>& QVariant::asList() +{ + bool b = isNull(); + if ( d->typ != List ) + *this = QVariant( toList() ); + else + detach(); + d->is_null = b; + return *((QValueList<QVariant>*)d->value.ptr); +} + +/*! + Returns the variant's value as variant map reference. + + Note that if you want to iterate over the map, you should iterate + over a copy, e.g. + \code + QMap<QString, QVariant> map = myVariant.asMap(); + QMap<QString, QVariant>::Iterator it = map.begin(); + while( it != map.end() ) { + myProcessing( *it ); + ++it; + } + \endcode +*/ +QMap<QString, QVariant>& QVariant::asMap() +{ + bool b = isNull(); + if ( d->typ != Map ) + *this = QVariant( toMap() ); + else + detach(); + d->is_null = b; + return *((QMap<QString,QVariant>*)d->value.ptr); +} +#endif + +/*! + Returns TRUE if the variant's type can be cast to the requested + type, \a t. Such casting is done automatically when calling the + toInt(), toBool(), ... or asInt(), asBool(), ... methods. + + The following casts are done automatically: + \table + \header \i Type \i Automatically Cast To + \row \i Bool \i Double, Int, UInt, LongLong, ULongLong, String, CString, ByteArray + \row \i Color \i String. CString. ByteArray + \row \i Date \i String, CString, ByteArray, DateTime + \row \i DateTime \i String, CString, ByteArray, Date, Time + \row \i Double \i String, CString, ByteArray, Int, Bool, UInt, LongLong, ULongLong + \row \i Font \i String, CString, ByteArray + \row \i Int \i String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence + \row \i LongLong \i String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence + \row \i ULongLong \i String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence + \row \i List \i StringList (if the list contains only strings or + something that can be cast to a string) + \row \i String \i CString, ByteArray, CString, Int, UInt, Bool, Double, Date, + Time, DateTime, KeySequence, Font, Color + \row \i CString \i String, ByteArray, Int, UInt, Bool, Double, Date, ULongLong, LongLong + \row \i ByteArray \i String, CString, Int, UInt, Bool, Double, Date, ULongLong, LongLong + \row \i StringList \i List + \row \i Time \i String + \row \i Int \i String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence + \row \i KeySequence \i String, CString, ByteArray, Int, UInt, LongLong, ULongLong + \endtable +*/ +bool QVariant::canCast( Type t ) const +{ + if ( Type( d->typ ) == t ) + return TRUE; + + switch ( t ) { + case Bool: + case Double: + if (d->typ == KeySequence) + break; + case Int: + case UInt: + case LongLong: + case ULongLong: + switch(d->typ) { + case Bool: + case ByteArray: + case CString: + case Double: + case Int: + case KeySequence: + case LongLong: + case String: + case UInt: + case ULongLong: + return TRUE; + default: break; + } + break; + + case CString: + case ByteArray: + case String: + switch(d->typ) { + case Bool: + case ByteArray: + case CString: + case Color: + case Date: + case DateTime: + case Double: + case Font: + case Int: + case KeySequence: + case LongLong: + case String: + case Time: + case UInt: + case ULongLong: + return TRUE; + default: break; + } + break; + + case Time: + if (d->typ == Date) + break; + case Date: + case DateTime: + switch(d->typ) { + case ByteArray: + case CString: + case Date: + case DateTime: + case String: + return TRUE; + default: break; + } + break; + + case KeySequence: + switch(d->typ) { + case ByteArray: + case CString: + case Int: + case UInt: + case LongLong: + case ULongLong: + case Double: + case String: + return TRUE; + default: break; + } + break; + + case Font: + case Color: + switch(d->typ) { + case ByteArray: + case CString: + case String: + return TRUE; + default: break; + } + break; + +#ifndef QT_NO_STRINGLIST + case List: + return d->typ == StringList; +#endif +#ifndef QT_NO_TEMPLATE_VARIANT + case StringList: + if ( d->typ == List ) { + QValueList<QVariant>::ConstIterator it = listBegin(); + QValueList<QVariant>::ConstIterator end = listEnd(); + for( ; it != end; ++it ) { + if ( !(*it).canCast( String ) ) + return FALSE; + } + return TRUE; + } +#endif + case Invalid: + case Map: + case Pixmap: + case Brush: + case Rect: + case Size: + case Palette: + case ColorGroup: + case IconSet: + case Point: + case Image: + case PointArray: + case Region: + case Bitmap: + case Cursor: + case SizePolicy: + case BitArray: + case Pen: + break; + } + return FALSE; +} + +/*! + Casts the variant to the requested type. If the cast cannot be + done, the variant is set to the default value of the requested + type (e.g. an empty string if the requested type \a t is + QVariant::String, an empty point array if the requested type \a t + is QVariant::PointArray, etc). Returns TRUE if the current type of + the variant was successfully cast; otherwise returns FALSE. + + \sa canCast() +*/ + +bool QVariant::cast( Type t ) +{ + switch ( t ) { +#ifndef QT_NO_TEMPLATE_VARIANT + case QVariant::Map: + asMap(); + break; + case QVariant::List: + asList(); + break; +#endif + case QVariant::String: + asString(); + break; +#ifndef QT_NO_STRINGLIST + case QVariant::StringList: + asStringList(); + break; +#endif + case QVariant::Font: + asFont(); + break; + case QVariant::Pixmap: + asPixmap(); + break; + case QVariant::Brush: + asBrush(); + break; + case QVariant::Rect: + asRect(); + break; + case QVariant::Size: + asSize(); + break; + case QVariant::Color: + asColor(); + break; +#ifndef QT_NO_PALETTE + case QVariant::Palette: + asPalette(); + break; + case QVariant::ColorGroup: + asColorGroup(); + break; +#endif +#ifndef QT_NO_ICONSET + case QVariant::IconSet: + asIconSet(); + break; +#endif + case QVariant::Point: + asPoint(); + break; + case QVariant::Image: + asImage(); + break; + case QVariant::Int: + asInt(); + break; + case QVariant::UInt: + asUInt(); + break; + case QVariant::Bool: + asBool(); + break; + case QVariant::Double: + asDouble(); + break; + case QVariant::CString: + asCString(); + break; + case QVariant::PointArray: + asPointArray(); + break; + case QVariant::Region: + asRegion(); + break; + case QVariant::Bitmap: + asBitmap(); + break; + case QVariant::Cursor: + asCursor(); + break; + case QVariant::SizePolicy: + asSizePolicy(); + break; + case QVariant::Date: + asDate(); + break; + case QVariant::Time: + asTime(); + break; + case QVariant::DateTime: + asDateTime(); + break; + case QVariant::ByteArray: + asByteArray(); + break; + case QVariant::BitArray: + asBitArray(); + break; +#ifndef QT_NO_ACCEL + case QVariant::KeySequence: + asKeySequence(); + break; +#endif + case QVariant::Pen: + asPen(); + break; + case QVariant::LongLong: + asLongLong(); + break; + case QVariant::ULongLong: + asULongLong(); + break; + default: + case QVariant::Invalid: + (*this) = QVariant(); + } + return canCast( t ); +} + +/*! + Compares this QVariant with \a v and returns TRUE if they are + equal; otherwise returns FALSE. +*/ + +bool QVariant::operator==( const QVariant &v ) const +{ + if (isNumeric(v.type()) && canCast(v.type())) { + bool ok; + switch(v.type()) { + case Bool: + return toBool() == v.toBool(); + case Int: + { + int val = toInt(&ok); + return (ok && val == v.toInt()); + } + case UInt: + { + uint val = toUInt(&ok); + return (ok && val == v.toUInt()); + } + + case Double: + { + double val = toDouble(&ok); + return (ok && val == v.toDouble()); + } + + case LongLong: + { + Q_LLONG val = toLongLong(&ok); + return (ok && val == v.toLongLong()); + } + + case ULongLong: + { + Q_ULLONG val = toULongLong(&ok); + return (ok && val == v.toULongLong()); + } + + default: + Q_ASSERT(FALSE); + } + } + + if (!v.canCast(d->typ)) { + return FALSE; + } + + switch( d->typ ) { + case Cursor: +#ifndef QT_NO_CURSOR + return v.toCursor().shape() == toCursor().shape(); +#endif + case Bitmap: + return v.toBitmap().serialNumber() == toBitmap().serialNumber(); + case PointArray: + return v.toPointArray() == toPointArray(); + case Region: + return v.toRegion() == toRegion(); +#ifndef QT_NO_TEMPLATE_VARIANT + case List: + return v.toList() == toList(); + case Map: { + if ( v.toMap().count() != toMap().count() ) + return FALSE; + QMap<QString, QVariant>::ConstIterator it = v.toMap().begin(); + QMap<QString, QVariant>::ConstIterator it2 = toMap().begin(); + while ( it != v.toMap().end() ) { + if ( *it != *it2 ) + return FALSE; + ++it; + ++it2; + } + return TRUE; + } +#endif + case String: + return v.toString() == toString(); + case CString: + return v.toCString() == toCString(); +#ifndef QT_NO_STRINGLIST + case StringList: + return v.toStringList() == toStringList(); +#endif + case Font: + return v.toFont() == toFont(); + case Pixmap: + return v.toPixmap().serialNumber() == toPixmap().serialNumber(); + case Image: + return v.toImage() == toImage(); + case Brush: + return v.toBrush() == toBrush(); + case Point: + return v.toPoint() == toPoint(); + case Rect: + return v.toRect() == toRect(); + case Size: + return v.toSize() == toSize(); + case Color: + return v.toColor() == toColor(); +#ifndef QT_NO_PALETTE + case Palette: + return v.toPalette() == toPalette(); + case ColorGroup: + return v.toColorGroup() == toColorGroup(); +#endif +#ifndef QT_NO_ICONSET + case IconSet: + return v.toIconSet().pixmap().serialNumber() + == toIconSet().pixmap().serialNumber(); +#endif + case Int: + return v.toInt() == toInt(); + case UInt: + return v.toUInt() == toUInt(); + case LongLong: + return v.toLongLong() == toLongLong(); + case ULongLong: + return v.toULongLong() == toULongLong(); + case Bool: + return v.toBool() == toBool(); + case Double: + return v.toDouble() == toDouble(); + case SizePolicy: + return v.toSizePolicy() == toSizePolicy(); + case Date: + return v.toDate() == toDate(); + case Time: + return v.toTime() == toTime(); + case DateTime: + return v.toDateTime() == toDateTime(); + case ByteArray: + return v.toByteArray() == toByteArray(); + case BitArray: + return v.toBitArray() == toBitArray(); +#ifndef QT_NO_ACCEL + case KeySequence: + return v.toKeySequence() == toKeySequence(); +#endif + case Pen: + return v.toPen() == toPen(); + case Invalid: + break; + } + return FALSE; +} + +/*! + Compares this QVariant with \a v and returns TRUE if they are not + equal; otherwise returns FALSE. +*/ + +bool QVariant::operator!=( const QVariant &v ) const +{ + return !( v == *this ); +} + + +/*! \internal + + Reads or sets the variant type and ptr + */ +void* QVariant::rawAccess( void* ptr, Type typ, bool deepCopy ) +{ + if ( ptr ) { + clear(); + d->typ = typ; + d->value.ptr = ptr; + d->is_null = FALSE; + if ( deepCopy ) { + QVariant::Private* p = new Private( d ); + d->typ = Invalid; + delete d; + d = p; + } + } + + if ( !deepCopy ) + return d->value.ptr; + QVariant::Private* p = new Private( d ); + void *ret = (void*)p->value.ptr; + p->typ = Invalid; + delete p; + return ret; +} + +/*! + Returns TRUE if this is a NULL variant, FALSE otherwise. +*/ +bool QVariant::isNull() const +{ + switch( d->typ ) + { + case QVariant::Bitmap: + return ((QBitmap*) d->value.ptr)->isNull(); + case QVariant::Region: + return ((QRegion*) d->value.ptr)->isNull(); + case QVariant::PointArray: + return ((QPointArray*) d->value.ptr)->isNull(); + case QVariant::String: + return ((QString*) d->value.ptr)->isNull(); + case QVariant::CString: + return ((QCString*) d->value.ptr)->isNull(); + case QVariant::Pixmap: + return ((QPixmap*) d->value.ptr)->isNull(); + case QVariant::Image: + return ((QImage*) d->value.ptr)->isNull(); + case QVariant::Point: + return ((QPoint*) d->value.ptr)->isNull(); + case QVariant::Rect: + return ((QRect*) d->value.ptr)->isNull(); + case QVariant::Size: + return ((QSize*) d->value.ptr)->isNull(); +#ifndef QT_NO_ICONSET + case QVariant::IconSet: + return ((QIconSet*) d->value.ptr)->isNull(); +#endif + case QVariant::Date: + return ((QDate*) d->value.ptr)->isNull(); + case QVariant::Time: + return ((QTime*) d->value.ptr)->isNull(); + case QVariant::DateTime: + return ((QDateTime*) d->value.ptr)->isNull(); + case QVariant::ByteArray: + return ((QByteArray*) d->value.ptr)->isNull(); + case QVariant::BitArray: + return ((QBitArray*) d->value.ptr)->isNull(); + case QVariant::Cursor: +#ifndef QT_NO_STRINGLIST + case QVariant::StringList: +#endif //QT_NO_STRINGLIST + case QVariant::Font: + case QVariant::Brush: + case QVariant::Color: +#ifndef QT_NO_PALETTE + case QVariant::Palette: + case QVariant::ColorGroup: +#endif +#ifndef QT_NO_TEMPLATE_VARIANT + case QVariant::Map: + case QVariant::List: +#endif + case QVariant::SizePolicy: +#ifndef QT_NO_ACCEL + case QVariant::KeySequence: +#endif + case QVariant::Pen: + case QVariant::Invalid: + case QVariant::Int: + case QVariant::UInt: + case QVariant::LongLong: + case QVariant::ULongLong: + case QVariant::Bool: + case QVariant::Double: + break; + } + return d->is_null; +} +#endif //QT_NO_VARIANT diff --git a/src/kernel/qvariant.h b/src/kernel/qvariant.h new file mode 100644 index 0000000..b2cab8d --- /dev/null +++ b/src/kernel/qvariant.h @@ -0,0 +1,396 @@ +/**************************************************************************** +** +** Definition of QVariant class +** +** Created : 990414 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QVARIANT_H +#define QVARIANT_H + +#ifndef QT_H +#include "qstring.h" +#endif // QT_H + +#ifndef QT_NO_VARIANT +class QString; +class QCString; +class QFont; +class QPixmap; +class QBrush; +class QRect; +class QPoint; +class QImage; +class QSize; +class QColor; +class QPalette; +class QColorGroup; +class QIconSet; +class QDataStream; +class QPointArray; +class QRegion; +class QBitmap; +class QCursor; +class QStringList; +class QSizePolicy; +class QDate; +class QTime; +class QDateTime; +class QBitArray; +class QKeySequence; +class QPen; +// Some headers rejected after QVariant declaration for GCC 2.7.* compatibility +class QVariant; +#ifndef QT_NO_TEMPLATE_VARIANT +template <class T> class QValueList; +template <class T> class QValueListConstIterator; +template <class T> class QValueListNode; +template <class Key, class T> class QMap; +template <class Key, class T> class QMapConstIterator; +#endif + +class Q_EXPORT QVariant +{ +public: + enum Type { + Invalid, + Map, + List, + String, + StringList, + Font, + Pixmap, + Brush, + Rect, + Size, + Color, + Palette, + ColorGroup, + IconSet, + Point, + Image, + Int, + UInt, + Bool, + Double, + CString, + PointArray, + Region, + Bitmap, + Cursor, + SizePolicy, + Date, + Time, + DateTime, + ByteArray, + BitArray, + KeySequence, + Pen, + LongLong, + ULongLong + }; + + QVariant(); + ~QVariant(); + QVariant( const QVariant& ); +#ifndef QT_NO_DATASTREAM + QVariant( QDataStream& s ); +#endif + QVariant( const QString& ); + QVariant( const QCString& ); + QVariant( const char* ); +#ifndef QT_NO_STRINGLIST + QVariant( const QStringList& ); +#endif + QVariant( const QFont& ); + QVariant( const QPixmap& ); + QVariant( const QImage& ); + QVariant( const QBrush& ); + QVariant( const QPoint& ); + QVariant( const QRect& ); + QVariant( const QSize& ); + QVariant( const QColor& ); + QVariant( const QPalette& ); + QVariant( const QColorGroup& ); + QVariant( const QIconSet& ); + QVariant( const QPointArray& ); + QVariant( const QRegion& ); + QVariant( const QBitmap& ); + QVariant( const QCursor& ); + QVariant( const QDate& ); + QVariant( const QTime& ); + QVariant( const QDateTime& ); + QVariant( const QByteArray& ); + QVariant( const QBitArray& ); +#ifndef QT_NO_ACCEL + QVariant( const QKeySequence& ); +#endif + QVariant( const QPen& ); +#ifndef QT_NO_TEMPLATE_VARIANT + QVariant( const QValueList<QVariant>& ); + QVariant( const QMap<QString,QVariant>& ); +#endif + QVariant( int ); + QVariant( uint ); + QVariant( Q_LLONG ); + QVariant( Q_ULLONG ); + // ### Problems on some compilers ? + QVariant( bool, int ); + QVariant( double ); + QVariant( QSizePolicy ); + + QVariant& operator= ( const QVariant& ); + bool operator==( const QVariant& ) const; + bool operator!=( const QVariant& ) const; + + Type type() const; + const char* typeName() const; + + bool canCast( Type ) const; + bool cast( Type ); + + bool isValid() const; + bool isNull() const; + + void clear(); + + const QString toString() const; + const QCString toCString() const; +#ifndef QT_NO_STRINGLIST + const QStringList toStringList() const; +#endif + const QFont toFont() const; + const QPixmap toPixmap() const; + const QImage toImage() const; + const QBrush toBrush() const; + const QPoint toPoint() const; + const QRect toRect() const; + const QSize toSize() const; + const QColor toColor() const; + const QPalette toPalette() const; + const QColorGroup toColorGroup() const; + const QIconSet toIconSet() const; + const QPointArray toPointArray() const; + const QBitmap toBitmap() const; + const QRegion toRegion() const; + const QCursor toCursor() const; + const QDate toDate() const; + const QTime toTime() const; + const QDateTime toDateTime() const; + const QByteArray toByteArray() const; + const QBitArray toBitArray() const; +#ifndef QT_NO_ACCEL + const QKeySequence toKeySequence() const; +#endif + const QPen toPen() const; + int toInt( bool * ok=0 ) const; + uint toUInt( bool * ok=0 ) const; + Q_LLONG toLongLong( bool * ok=0 ) const; + Q_ULLONG toULongLong( bool * ok=0 ) const; + bool toBool() const; + double toDouble( bool * ok=0 ) const; +#ifndef QT_NO_TEMPLATE_VARIANT + const QValueList<QVariant> toList() const; + const QMap<QString,QVariant> toMap() const; +#endif + QSizePolicy toSizePolicy() const; + +#ifndef QT_NO_TEMPLATE_VARIANT + QValueListConstIterator<QString> stringListBegin() const; + QValueListConstIterator<QString> stringListEnd() const; + QValueListConstIterator<QVariant> listBegin() const; + QValueListConstIterator<QVariant> listEnd() const; + QMapConstIterator<QString,QVariant> mapBegin() const; + QMapConstIterator<QString,QVariant> mapEnd() const; + QMapConstIterator<QString,QVariant> mapFind( const QString& ) const; +#endif + QString& asString(); + QCString& asCString(); +#ifndef QT_NO_STRINGLIST + QStringList& asStringList(); +#endif + QFont& asFont(); + QPixmap& asPixmap(); + QImage& asImage(); + QBrush& asBrush(); + QPoint& asPoint(); + QRect& asRect(); + QSize& asSize(); + QColor& asColor(); + QPalette& asPalette(); + QColorGroup& asColorGroup(); + QIconSet& asIconSet(); + QPointArray& asPointArray(); + QBitmap& asBitmap(); + QRegion& asRegion(); + QCursor& asCursor(); + QDate& asDate(); + QTime& asTime(); + QDateTime& asDateTime(); + QByteArray& asByteArray(); + QBitArray& asBitArray(); +#ifndef QT_NO_ACCEL + QKeySequence& asKeySequence(); +#endif + QPen& asPen(); + int& asInt(); + uint& asUInt(); + Q_LLONG& asLongLong(); + Q_ULLONG& asULongLong(); + bool& asBool(); + double& asDouble(); +#ifndef QT_NO_TEMPLATE_VARIANT + QValueList<QVariant>& asList(); + QMap<QString,QVariant>& asMap(); +#endif + QSizePolicy& asSizePolicy(); + +#ifndef QT_NO_DATASTREAM + void load( QDataStream& ); + void save( QDataStream& ) const; +#endif + static const char* typeToName( Type typ ); + static Type nameToType( const char* name ); + +private: + void detach(); + + class Private : public QShared + { + public: + Private(); + Private( Private* ); + ~Private(); + + void clear(); + + Type typ; + union + { + uint u; + int i; + Q_LLONG ll; + Q_ULLONG ull; + bool b; + double d; + void *ptr; + } value; + uint is_null : 1; // ## 4.0 merge with typ + }; + + Private* d; + +public: + void* rawAccess( void* ptr = 0, Type typ = Invalid, bool deepCopy = FALSE ); +}; + +// down here for GCC 2.7.* compatibility +#ifndef QT_H +#include "qvaluelist.h" +#include "qstringlist.h" +#include "qmap.h" +#endif // QT_H + +inline QVariant::Type QVariant::type() const +{ + return d->typ; +} + +inline bool QVariant::isValid() const +{ + return (d->typ != Invalid); +} + +#ifndef QT_NO_TEMPLATE_VARIANT +inline QValueListConstIterator<QString> QVariant::stringListBegin() const +{ + if ( d->typ != StringList ) + return QValueListConstIterator<QString>(); + return ((const QStringList*)d->value.ptr)->begin(); +} + +inline QValueListConstIterator<QString> QVariant::stringListEnd() const +{ + if ( d->typ != StringList ) + return QValueListConstIterator<QString>(); + return ((const QStringList*)d->value.ptr)->end(); +} + +inline QValueListConstIterator<QVariant> QVariant::listBegin() const +{ + if ( d->typ != List ) + return QValueListConstIterator<QVariant>(); + return ((const QValueList<QVariant>*)d->value.ptr)->begin(); +} + +inline QValueListConstIterator<QVariant> QVariant::listEnd() const +{ + if ( d->typ != List ) + return QValueListConstIterator<QVariant>(); + return ((const QValueList<QVariant>*)d->value.ptr)->end(); +} + +inline QMapConstIterator<QString,QVariant> QVariant::mapBegin() const +{ + if ( d->typ != Map ) + return QMapConstIterator<QString,QVariant>(); + return ((const QMap<QString,QVariant>*)d->value.ptr)->begin(); +} + +inline QMapConstIterator<QString,QVariant> QVariant::mapEnd() const +{ + if ( d->typ != Map ) + return QMapConstIterator<QString,QVariant>(); + return ((const QMap<QString,QVariant>*)d->value.ptr)->end(); +} + +inline QMapConstIterator<QString,QVariant> QVariant::mapFind( const QString& key ) const +{ + if ( d->typ != Map ) + return QMapConstIterator<QString,QVariant>(); + return ((const QMap<QString,QVariant>*)d->value.ptr)->find( key ); +} +#endif + +#ifndef QT_NO_DATASTREAM +Q_EXPORT QDataStream& operator>> ( QDataStream& s, QVariant& p ); +Q_EXPORT QDataStream& operator<< ( QDataStream& s, const QVariant& p ); +Q_EXPORT QDataStream& operator>> ( QDataStream& s, QVariant::Type& p ); +Q_EXPORT QDataStream& operator<< ( QDataStream& s, const QVariant::Type p ); +#endif + +#endif //QT_NO_VARIANT +#endif // QVARIANT_H diff --git a/src/kernel/qvfbhdr.h b/src/kernel/qvfbhdr.h new file mode 100644 index 0000000..c5d082a --- /dev/null +++ b/src/kernel/qvfbhdr.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Qt/Embedded virtual framebuffer +** +** Created : 20000605 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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. +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt 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. +** +**********************************************************************/ + +#ifndef QVFBHDR_H +#define QVFBHDR_H + +#ifndef QT_H +#include "qcolor.h" +#include "qrect.h" +#endif // QT_H + +#define QT_VFB_MOUSE_PIPE "/tmp/.qtvfb_mouse-%1" +#define QT_VFB_KEYBOARD_PIPE "/tmp/.qtvfb_keyboard-%1" + +struct QVFbHeader +{ + int width; + int height; + int depth; + int linestep; + int dataoffset; + QRect update; + bool dirty; + int numcols; + QRgb clut[256]; +}; + +struct QVFbKeyData +{ + unsigned int unicode; + unsigned int modifiers; + bool press; + bool repeat; +}; + +#endif diff --git a/src/kernel/qwidget.cpp b/src/kernel/qwidget.cpp new file mode 100644 index 0000000..856907c --- /dev/null +++ b/src/kernel/qwidget.cpp @@ -0,0 +1,6081 @@ +/**************************************************************************** +** +** Implementation of QWidget class +** +** Created : 931031 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qobjectlist.h" +#include "qwidget.h" +#include "qwidgetlist.h" +#include "qwidgetintdict.h" +#include "qptrdict.h" +#include "qfocusdata.h" +#include "qcursor.h" +#include "qpixmap.h" +#include "qapplication.h" +#include "qapplication_p.h" +#include "qbrush.h" +#include "qlayout.h" +#include "qstylefactory.h" +#include "qcleanuphandler.h" +#include "qstyle.h" +#include "qmetaobject.h" +#include "qguardedptr.h" +#if defined(QT_ACCESSIBILITY_SUPPORT) +#include "qaccessible.h" +#endif +#if defined(Q_WS_WIN) +#include "qt_windows.h" +#include "qinputcontext_p.h" +#endif +#if defined(Q_WS_QWS) +#include "qwsmanager_qws.h" +#endif +#include "qfontdata_p.h" + + +/*! + \class QWidget qwidget.h + \brief The QWidget class is the base class of all user interface objects. + + \ingroup abstractwidgets + \mainclass + + The widget is the atom of the user interface: it receives mouse, + keyboard and other events from the window system, and paints a + representation of itself on the screen. Every widget is + rectangular, and they are sorted in a Z-order. A widget is + clipped by its parent and by the widgets in front of it. + + A widget that isn't embedded in a parent widget is called a + top-level widget. Usually, top-level widgets are windows with a + frame and a title bar (although it is also possible to create + top-level widgets without such decoration if suitable widget flags + are used). In Qt, QMainWindow and the various subclasses of + QDialog are the most common top-level windows. + + A widget without a parent widget is always a top-level widget. + + Non-top-level widgets are child widgets. These are child windows + in their parent widgets. You cannot usually distinguish a child + widget from its parent visually. Most other widgets in Qt are + useful only as child widgets. (It is possible to make, say, a + button into a top-level widget, but most people prefer to put + their buttons inside other widgets, e.g. QDialog.) + + If you want to use a QWidget to hold child widgets you will + probably want to add a layout to the parent QWidget. (See \link + layout.html Layouts\endlink.) + + QWidget has many member functions, but some of them have little + direct functionality: for example, QWidget has a font property, + but never uses this itself. There are many subclasses which + provide real functionality, such as QPushButton, QListBox and + QTabDialog, etc. + + \section1 Groups of functions: + + \table + \header \i Context \i Functions + + \row \i Window functions \i + show(), + hide(), + raise(), + lower(), + close(). + + \row \i Top level windows \i + caption(), + setCaption(), + icon(), + setIcon(), + iconText(), + setIconText(), + isActiveWindow(), + setActiveWindow(), + showMinimized(). + showMaximized(), + showFullScreen(), + showNormal(). + + \row \i Window contents \i + update(), + repaint(), + erase(), + scroll(), + updateMask(). + + \row \i Geometry \i + pos(), + size(), + rect(), + x(), + y(), + width(), + height(), + sizePolicy(), + setSizePolicy(), + sizeHint(), + updateGeometry(), + layout(), + move(), + resize(), + setGeometry(), + frameGeometry(), + geometry(), + childrenRect(), + adjustSize(), + mapFromGlobal(), + mapFromParent() + mapToGlobal(), + mapToParent(), + maximumSize(), + minimumSize(), + sizeIncrement(), + setMaximumSize(), + setMinimumSize(), + setSizeIncrement(), + setBaseSize(), + setFixedSize() + + \row \i Mode \i + isVisible(), + isVisibleTo(), + isMinimized(), + isDesktop(), + isEnabled(), + isEnabledTo(), + isModal(), + isPopup(), + isTopLevel(), + setEnabled(), + hasMouseTracking(), + setMouseTracking(), + isUpdatesEnabled(), + setUpdatesEnabled(), + clipRegion(). + + \row \i Look and feel \i + style(), + setStyle(), + cursor(), + setCursor() + font(), + setFont(), + palette(), + setPalette(), + backgroundMode(), + setBackgroundMode(), + colorGroup(), + fontMetrics(), + fontInfo(). + + \row \i Keyboard focus<br>functions \i + isFocusEnabled(), + setFocusPolicy(), + focusPolicy(), + hasFocus(), + setFocus(), + clearFocus(), + setTabOrder(), + setFocusProxy(). + + \row \i Mouse and<br>keyboard grabbing \i + grabMouse(), + releaseMouse(), + grabKeyboard(), + releaseKeyboard(), + mouseGrabber(), + keyboardGrabber(). + + \row \i Event handlers \i + event(), + mousePressEvent(), + mouseReleaseEvent(), + mouseDoubleClickEvent(), + mouseMoveEvent(), + keyPressEvent(), + keyReleaseEvent(), + focusInEvent(), + focusOutEvent(), + wheelEvent(), + enterEvent(), + leaveEvent(), + paintEvent(), + moveEvent(), + resizeEvent(), + closeEvent(), + dragEnterEvent(), + dragMoveEvent(), + dragLeaveEvent(), + dropEvent(), + childEvent(), + showEvent(), + hideEvent(), + customEvent(). + + \row \i Change handlers \i + enabledChange(), + fontChange(), + paletteChange(), + styleChange(), + windowActivationChange(). + + \row \i System functions \i + parentWidget(), + topLevelWidget(), + reparent(), + polish(), + winId(), + find(), + metric(). + + \row \i What's this help \i + customWhatsThis() + + \row \i Internal kernel<br>functions \i + focusNextPrevChild(), + wmapper(), + clearWFlags(), + getWFlags(), + setWFlags(), + testWFlags(). + + \endtable + + Every widget's constructor accepts two or three standard arguments: + \list 1 + \i \c{QWidget *parent = 0} is the parent of the new widget. + If it is 0 (the default), the new widget will be a top-level window. + If not, it will be a child of \e parent, and be constrained by \e + parent's geometry (unless you specify \c WType_TopLevel as + widget flag). + \i \c{const char *name = 0} is the widget name of the new + widget. You can access it using name(). The widget name is little + used by programmers but is quite useful with GUI builders such as + \e{Qt Designer} (you can name a widget in \e{Qt Designer}, and + connect() to it using the name in your code). The dumpObjectTree() + debugging function also uses it. + \i \c{WFlags f = 0} (where available) sets the widget flags; the + default is suitable for almost all widgets, but to get, for + example, a top-level widget without a window system frame, you + must use special flags. + \endlist + + The tictac/tictac.cpp example program is good example of a simple + widget. It contains a few event handlers (as all widgets must), a + few custom routines that are specific to it (as all useful widgets + do), and has a few children and connections. Everything it does + is done in response to an event: this is by far the most common way + to design GUI applications. + + You will need to supply the content for your widgets yourself, but + here is a brief run-down of the events, starting with the most common + ones: + + \list + + \i paintEvent() - called whenever the widget needs to be + repainted. Every widget which displays output must implement it, + and it is wise \e not to paint on the screen outside + paintEvent(). + + \i resizeEvent() - called when the widget has been resized. + + \i mousePressEvent() - called when a mouse button is pressed. + There are six mouse-related events, but the mouse press and mouse + release events are by far the most important. A widget receives + mouse press events when the mouse is inside it, or when it has + grabbed the mouse using grabMouse(). + + \i mouseReleaseEvent() - called when a mouse button is released. + A widget receives mouse release events when it has received the + corresponding mouse press event. This means that if the user + presses the mouse inside \e your widget, then drags the mouse to + somewhere else, then releases, \e your widget receives the release + event. There is one exception: if a popup menu appears while the + mouse button is held down, this popup immediately steals the mouse + events. + + \i mouseDoubleClickEvent() - not quite as obvious as it might seem. + If the user double-clicks, the widget receives a mouse press event + (perhaps a mouse move event or two if they don't hold the mouse + quite steady), a mouse release event and finally this event. It is + \e{not possible} to distinguish a click from a double click until you've + seen whether the second click arrives. (This is one reason why most GUI + books recommend that double clicks be an extension of single clicks, + rather than trigger a different action.) + + \endlist + + If your widget only contains child widgets, you probably do not need to + implement any event handlers. If you want to detect a mouse click in + a child widget call the child's hasMouse() function inside the + parent widget's mousePressEvent(). + + Widgets that accept keyboard input need to reimplement a few more + event handlers: + + \list + + \i keyPressEvent() - called whenever a key is pressed, and again + when a key has been held down long enough for it to auto-repeat. + Note that the Tab and Shift+Tab keys are only passed to the widget + if they are not used by the focus-change mechanisms. To force those + keys to be processed by your widget, you must reimplement + QWidget::event(). + + \i focusInEvent() - called when the widget gains keyboard focus + (assuming you have called setFocusPolicy()). Well written widgets + indicate that they own the keyboard focus in a clear but discreet + way. + + \i focusOutEvent() - called when the widget loses keyboard focus. + + \endlist + + Some widgets will also need to reimplement some of the less common + event handlers: + + \list + + \i mouseMoveEvent() - called whenever the mouse moves while a + button is held down. This is useful for, for example, dragging. If + you call setMouseTracking(TRUE), you get mouse move events even + when no buttons are held down. (Note that applications which make + use of mouse tracking are often not very useful on low-bandwidth X + connections.) (See also the \link dnd.html drag and drop\endlink + information.) + + \i keyReleaseEvent() - called whenever a key is released, and also + while it is held down if the key is auto-repeating. In that case + the widget receives a key release event and immediately a key press + event for every repeat. Note that the Tab and Shift+Tab keys are + only passed to the widget if they are not used by the focus-change + mechanisms. To force those keys to be processed by your widget, you + must reimplement QWidget::event(). + + \i wheelEvent() -- called whenever the user turns the mouse wheel + while the widget has the focus. + + \i enterEvent() - called when the mouse enters the widget's screen + space. (This excludes screen space owned by any children of the + widget.) + + \i leaveEvent() - called when the mouse leaves the widget's screen + space. + + \i moveEvent() - called when the widget has been moved relative to its + parent. + + \i closeEvent() - called when the user closes the widget (or when + close() is called). + + \endlist + + There are also some rather obscure events. They are listed in + \c qevent.h and you need to reimplement event() to handle them. + The default implementation of event() handles Tab and Shift+Tab + (to move the keyboard focus), and passes on most other events to + one of the more specialized handlers above. + + When implementing a widget, there are a few more things to + consider. + + \list + + \i In the constructor, be sure to set up your member variables + early on, before there's any chance that you might receive an event. + + \i It is almost always useful to reimplement sizeHint() and to set + the correct size policy with setSizePolicy(), so users of your class + can set up layout management more easily. A size policy lets you + supply good defaults for the layout management handling, so that + other widgets can contain and manage yours easily. sizeHint() + indicates a "good" size for the widget. + + \i If your widget is a top-level window, setCaption() and setIcon() set + the title bar and icon respectively. + + \endlist + + \sa QEvent, QPainter, QGridLayout, QBoxLayout +*/ + + +/***************************************************************************** + Internal QWidgetMapper class + + The purpose of this class is to map widget identifiers to QWidget objects. + All QWidget objects register themselves in the QWidgetMapper when they + get an identifier. Widgets unregister themselves when they change ident- + ifier or when they are destroyed. A widget identifier is really a window + handle. + + The widget mapper is created and destroyed by the main application routines + in the file qapp_xxx.cpp. + *****************************************************************************/ + +#if defined(Q_WS_QWS) || defined(Q_OS_TEMP) +static const int WDictSize = 163; // plenty for small devices +#else +static const int WDictSize = 1123; // plenty for 5 big complex windows +#endif + +class QWidgetMapper : public QWidgetIntDict +{ // maps ids -> widgets +public: + QWidgetMapper(); + ~QWidgetMapper(); + QWidget *find( WId id ); // find widget + void insert( const QWidget * ); // insert widget + bool remove( WId id ); // remove widget +private: + WId cur_id; + QWidget *cur_widget; +}; + +QWidgetMapper *QWidget::mapper = 0; // app global widget mapper + + +QWidgetMapper::QWidgetMapper() : QWidgetIntDict(WDictSize) +{ + cur_id = 0; + cur_widget = 0; +} + +QWidgetMapper::~QWidgetMapper() +{ + clear(); +} + +inline QWidget *QWidgetMapper::find( WId id ) +{ + if ( id != cur_id ) { // need to lookup + cur_widget = QWidgetIntDict::find((long)id); + if ( cur_widget ) + cur_id = id; + else + cur_id = 0; + } + return cur_widget; +} + +inline void QWidgetMapper::insert( const QWidget *widget ) +{ + QWidgetIntDict::insert((long)widget->winId(),widget); +} + +inline bool QWidgetMapper::remove( WId id ) +{ + if ( cur_id == id ) { // reset current widget + cur_id = 0; + cur_widget = 0; + } + return QWidgetIntDict::remove((long)id); +} + + +/***************************************************************************** + QWidget utility functions + *****************************************************************************/ + +static QFont qt_naturalWidgetFont( QWidget* w ) { + QFont naturalfont = QApplication::font( w ); + if ( ! w->isTopLevel() ) { + if ( ! naturalfont.isCopyOf( QApplication::font() ) ) + naturalfont = naturalfont.resolve( w->parentWidget()->font() ); + else + naturalfont = w->parentWidget()->font(); + } + return naturalfont; +} + +#ifndef QT_NO_PALETTE +static QPalette qt_naturalWidgetPalette( QWidget* w ) { + QPalette naturalpalette = QApplication::palette( w ); + if ( !w->isTopLevel() && naturalpalette.isCopyOf( QApplication::palette() ) ) + naturalpalette = w->parentWidget()->palette(); + return naturalpalette; +} +#endif + +QSize qt_naturalWidgetSize( QWidget *w ) { + QSize s = w->sizeHint(); + QSizePolicy::ExpandData exp; +#ifndef QT_NO_LAYOUT + if ( w->layout() ) { + if ( w->layout()->hasHeightForWidth() ) + s.setHeight( w->layout()->totalHeightForWidth( s.width() ) ); + exp = w->layout()->expanding(); + } else +#endif + { + if ( w->sizePolicy().hasHeightForWidth() ) + s.setHeight( w->heightForWidth( s.width() ) ); + exp = w->sizePolicy().expanding(); + } + if ( exp & QSizePolicy::Horizontally ) + s.setWidth( QMAX( s.width(), 200 ) ); + if ( exp & QSizePolicy::Vertically ) + s.setHeight( QMAX( s.height(), 150 ) ); +#if defined(Q_WS_X11) + QRect screen = QApplication::desktop()->screenGeometry( w->x11Screen() ); +#else // all others + QRect screen = QApplication::desktop()->screenGeometry( w->pos() ); +#endif + s.setWidth( QMIN( s.width(), screen.width()*2/3 ) ); + s.setHeight( QMIN( s.height(), screen.height()*2/3 ) ); + return s; +} + +/***************************************************************************** + QWidget member functions + *****************************************************************************/ + +/* + Widget state flags: + \list + \i WState_Created The widget has a valid winId(). + \i WState_Disabled The widget does not receive any mouse or keyboard + events. + \i WState_ForceDisabled The widget is explicitly disabled, i.e. it + will remain disabled even when all its ancestors are set to the enabled + state. This implies WState_Disabled. + \i WState_Visible The widget is currently visible. + \i WState_ForceHide The widget is explicitly hidden, i.e. it won't + become visible unless you call show() on it. WState_ForceHide + implies !WState_Visible. + \i WState_OwnCursor A cursor has been set for this widget. + \i WState_MouseTracking Mouse tracking is enabled. + \i WState_CompressKeys Compress keyboard events. + \i WState_BlockUpdates Repaints and updates are disabled. + \i WState_InPaintEvent Currently processing a paint event. + \i WState_Reparented The widget has been reparented. + \i WState_ConfigPending A configuration (resize/move) event is pending. + \i WState_Resized The widget has been resized. + \i WState_AutoMask The widget has an automatic mask, see setAutoMask(). + \i WState_Polished The widget has been "polished" (i.e. late + initialization) by a QStyle. + \i WState_DND The widget supports drag and drop, see setAcceptDrops(). + \i WState_Exposed the widget was finally exposed (X11 only, + helps avoid paint event doubling). + \i WState_HasMouse The widget is under the mouse cursor. + \endlist +*/ + +/*! \enum Qt::WFlags + \internal */ +/*! \enum Qt::WState + \internal */ + +/*! + \enum Qt::WidgetFlags + + \keyword widget flag + + This enum type is used to specify various window-system properties + for the widget. They are fairly unusual but necessary in a few + cases. Some of these flags depend on whether the underlying window + manager supports them. (See the \link toplevel-example.html + toplevel example\endlink for an explanation and example of their + use.) + + The main types are + + \value WType_TopLevel indicates that this widget is a top-level + widget, usually with a window-system frame and so on. + + \value WType_Dialog indicates that this widget is a top-level + window that should be decorated as a dialog (i.e. typically no + maximize or minimize buttons in the title bar). If you want to use + it as a modal dialog it should be launched from another window, or + have a parent and this flag should be combined with \c WShowModal. + If you make it modal, the dialog will prevent other top-level + windows in the application from getting any input. \c WType_Dialog + implies \c WType_TopLevel. We refer to a top-level window that has + a parent as a \e secondary window. (See also \c WGroupLeader.) + + \value WType_Popup indicates that this widget is a popup + top-level window, i.e. that it is modal, but has a window system + frame appropriate for popup menus. \c WType_Popup implies + WType_TopLevel. + + \value WType_Desktop indicates that this widget is the desktop. + See also \c WPaintDesktop below. \c WType_Desktop implies \c + WType_TopLevel. + + There are also a number of flags which you can use to customize + the appearance of top-level windows. These have no effect on other + windows: + + \value WStyle_Customize indicates that the \c WStyle_* flags + should be used to build the window instead of the default flags. + + \value WStyle_NormalBorder gives the window a normal border. + This cannot be combined with \c WStyle_DialogBorder or \c + WStyle_NoBorder. + + \value WStyle_DialogBorder gives the window a thin dialog border. + This cannot be combined with \c WStyle_NormalBorder or \c + WStyle_NoBorder. + + \value WStyle_NoBorder produces a borderless window. Note that + the user cannot move or resize a borderless window via the window + system. This cannot be combined with \c WStyle_NormalBorder or \c + WStyle_DialogBorder. On Windows, the flag works fine. On X11, the + result of the flag is dependent on the window manager and its + ability to understand MOTIF and/or NETWM hints: most existing + modern window managers can handle this. With \c WX11BypassWM, you + can bypass the window manager completely. This results in a + borderless window that is not managed at all (i.e. no keyboard + input unless you call setActiveWindow() manually). + + \value WStyle_NoBorderEx this value is obsolete. It has the same + effect as using \c WStyle_NoBorder. + + \value WStyle_Title gives the window a title bar. + + \value WStyle_SysMenu adds a window system menu. + + \value WStyle_Minimize adds a minimize button. Note that on + Windows this has to be combined with \c WStyle_SysMenu for it to + work. + + \value WStyle_Maximize adds a maximize button. Note that on + Windows this has to be combined with \c WStyle_SysMenu for it to work. + + \value WStyle_MinMax is equal to \c + WStyle_Minimize|WStyle_Maximize. Note that on Windows this has to + be combined with \c WStyle_SysMenu to work. + + \value WStyle_ContextHelp adds a context help button to dialogs. + + \value WStyle_Tool makes the window a tool window. A tool window + is often a small window with a smaller than usual title bar and + decoration, typically used for collections of tool buttons. It + there is a parent, the tool window will always be kept on top of + it. If there isn't a parent, you may consider passing \c + WStyle_StaysOnTop as well. If the window system supports it, a + tool window can be decorated with a somewhat lighter frame. It can + also be combined with \c WStyle_NoBorder. + + \value WStyle_StaysOnTop informs the window system that the + window should stay on top of all other windows. Note that on some + window managers on X11 you also have to pass \c WX11BypassWM for + this flag to work correctly. + + \value WStyle_Dialog indicates that the window is a logical + subwindow of its parent (i.e. a dialog). The window will not get + its own taskbar entry and will be kept on top of its parent by the + window system. Usually it will also be minimized when the parent + is minimized. If not customized, the window is decorated with a + slightly simpler title bar. This is the flag QDialog uses. + + \value WStyle_Splash indicates that the window is a splash screen. + On X11, we try to follow NETWM standard for a splash screen window if the + window manager supports is otherwise it is equivalent to \c WX11BypassWM. On + other platforms, it is equivalent to \c WStyle_NoBorder \c | \c WMacNoSheet \c | + \c WStyle_Tool \c | \c WWinOwnDC + + Modifier flags: + + \value WDestructiveClose makes Qt delete this widget when the + widget has accepted closeEvent(), or when the widget tried to + ignore closeEvent() but could not. + + \value WPaintDesktop gives this widget paint events for the + desktop. + + \value WPaintUnclipped makes all painters operating on this + widget unclipped. Children of this widget or other widgets in + front of it do not clip the area the painter can paint on. + + \value WPaintClever indicates that Qt should \e not try to + optimize repainting for the widget, but instead pass on window + system repaint events directly. (This tends to produce more events + and smaller repaint regions.) + + \value WMouseNoMask indicates that even if the widget has a mask, + it wants mouse events for its entire rectangle. + + \value WStaticContents indicates that the widget contents are + north-west aligned and static. On resize, such a widget will + receive paint events only for the newly visible part of itself. + + \value WNoAutoErase indicates that the widget paints all its + pixels. Updating, resizing, scrolling and focus changes should + therefore not erase the widget. This allows smart-repainting to + avoid flicker. + + \value WResizeNoErase this value is obsolete; use WNoAutoErase instead. + \value WRepaintNoErase this value is obsolete; use WNoAutoErase instead. + \value WGroupLeader makes this window a group leader. A group + leader should \e not have a parent (i.e. it should be a top-level + window). Any decendant windows (direct or indirect) of a group + leader are in its group; other windows are not. If you show a + secondary window from the group (i.e. show a window whose top-most + parent is a group leader), that window will be modal with respect + to the other windows in the group, but modeless with respect to + windows in other groups. + + Miscellaneous flags + + \value WShowModal see WType_Dialog + + Internal flags. + + \value WNoMousePropagation + \value WStaticContents + \value WStyle_Reserved + \value WSubWindow + \value WType_Modal + \value WWinOwnDC + \value WX11BypassWM + \value WMacNoSheet + \value WMacDrawer + \value WStyle_Mask + \value WType_Mask + +*/ + +/*! + \enum Qt::WidgetState + + Internal flags. + + \value WState_Created + \value WState_Disabled + \value WState_Visible + \value WState_ForceHide + \value WState_OwnCursor + \value WState_MouseTracking + \value WState_CompressKeys + \value WState_BlockUpdates + \value WState_InPaintEvent + \value WState_Reparented + \value WState_ConfigPending + \value WState_Resized + \value WState_AutoMask + \value WState_Polished + \value WState_DND + \value WState_Reserved0 \e internal + \value WState_CreatedHidden + \value WState_Maximized + \value WState_Minimized + \value WState_ForceDisabled + \value WState_Exposed + \value WState_HasMouse + \value WState_CreatedHidden + \value WState_OwnSizePolicy + \value WState_FullScreen +*/ + + +/*! + \enum Qt::WindowState + + \keyword window state + + This enum type is used to specify the current state of a top-level + window. + + The states are + + \value WindowNoState The window has no state set (in normal state). + \value WindowMinimized The window is minimized (i.e. iconified). + \value WindowMaximized The window is maximized with a frame around it. + \value WindowFullScreen The window fills the entire screen without any frame around it. + \value WindowActive The window is the active window, i.e. it has keyboard focus. + +*/ + +/*! + Constructs a widget which is a child of \a parent, with the name + \a name and widget flags set to \a f. + + If \a parent is 0, the new widget becomes a top-level window. If + \a parent is another widget, this widget becomes a child window + inside \a parent. The new widget is deleted when its \a parent is + deleted. + + The \a name is sent to the QObject constructor. + + The widget flags argument, \a f, is normally 0, but it can be set + to customize the window frame of a top-level widget (i.e. \a + parent must be 0). To customize the frame, set the \c + WStyle_Customize flag OR'ed with any of the \l Qt::WidgetFlags. + + If you add a child widget to an already visible widget you must + explicitly show the child to make it visible. + + Note that the X11 version of Qt may not be able to deliver all + combinations of style flags on all systems. This is because on + X11, Qt can only ask the window manager, and the window manager + can override the application's settings. On Windows, Qt can set + whatever flags you want. + + Example: + \code + QLabel *splashScreen = new QLabel( 0, "mySplashScreen", + WStyle_Customize | WStyle_Splash ); + \endcode +*/ + +QWidget::QWidget( QWidget *parent, const char *name, WFlags f ) + : QObject( parent, name ), QPaintDevice( QInternal::Widget ) +{ +#if defined(QT_CHECK_STATE) && !defined(Q_WS_WIN) + if ( qApp->type() == QApplication::Tty ) { + qWarning( "QWidget: Cannot create a QWidget when no GUI " + "is being used" ); + } +#endif + + fstrut_dirty = 1; + + isWidget = TRUE; // is a widget + winid = 0; // default attributes + widget_state = 0; + widget_flags = f; + focus_policy = 0; + own_font = 0; + own_palette = 0; + sizehint_forced = 0; + is_closing = 0; + in_show = 0; + in_show_maximized = 0; + im_enabled = FALSE; +#ifndef QT_NO_LAYOUT + lay_out = 0; +#endif + extra = 0; // no extra widget info +#ifndef QT_NO_PALETTE + bg_col = pal.active().background(); // default background color +#endif + create(); // platform-dependent init +#ifndef QT_NO_PALETTE + pal = isTopLevel() ? QApplication::palette() : parentWidget()->palette(); +#endif + if ( ! isTopLevel() ) + fnt = parentWidget()->font(); +#if defined(Q_WS_X11) + fnt.x11SetScreen( x11Screen() ); +#endif // Q_WS_X11 + + if ( !isDesktop() ) + setBackgroundFromMode(); //### parts of this are done in create but not all (see reparent(...) ) + // make sure move/resize events are sent to all widgets + QApplication::postEvent( this, new QMoveEvent( crect.topLeft(), + crect.topLeft() ) ); + QApplication::postEvent( this, new QResizeEvent(crect.size(), + crect.size()) ); + if ( isTopLevel() ) { + setWState( WState_ForceHide | WState_CreatedHidden ); + QFocusData *fd = focusData( TRUE ); + if ( fd->focusWidgets.findRef(this) < 0 ) + fd->focusWidgets.append( this ); + } else { + // propagate enabled state + if ( !parentWidget()->isEnabled() ) + setWState( WState_Disabled ); + // new widgets do not show up in already visible parents + if ( parentWidget()->isVisible() ) + setWState( WState_ForceHide | WState_CreatedHidden ); + } + if ( ++instanceCounter > maxInstances ) + maxInstances = instanceCounter; +} + +/*! + Destroys the widget. + + All this widget's children are deleted first. The application + exits if this widget is the main widget. +*/ + +QWidget::~QWidget() +{ +#if defined (QT_CHECK_STATE) + if ( paintingActive() ) + qWarning( "%s (%s): deleted while being painted", className(), name() ); +#endif + + // Remove myself and all children from the can-take-focus list + QFocusData *f = focusData( FALSE ); + if ( f ) { + QPtrListIterator<QWidget> it(f->focusWidgets); + QWidget *w; + while ( (w = it.current()) ) { + ++it; + QWidget * p = w; + while( p && p != this ) + p = p->parentWidget(); + if ( p ) // my descendant + f->focusWidgets.removeRef( w ); + } + } + --instanceCounter; + + if ( QApplication::main_widget == this ) { // reset main widget + QApplication::main_widget = 0; + if (qApp) + qApp->quit(); + } + + if ( hasFocus() ) + clearFocus(); + + if ( isTopLevel() && isShown() && winId() ) + hide(); + + // A parent widget must destroy all its children before destroying itself + if ( childObjects ) { // delete children objects + QObjectListIt it(*childObjects); + QObject *obj; + while ( (obj=it.current()) ) { + ++it; + obj->parentObj = 0; + childObjects->removeRef( obj ); + delete obj; + } + delete childObjects; + childObjects = 0; + } + + QApplication::removePostedEvents( this ); + + destroy(); // platform-dependent cleanup + if ( extra ) + deleteExtra(); +} + +int QWidget::instanceCounter = 0; // Current number of widget instances +int QWidget::maxInstances = 0; // Maximum number of widget instances + +/*! + \internal + Creates the global widget mapper. + The widget mapper converts window handles to widget pointers. + \sa destroyMapper() +*/ + +void QWidget::createMapper() +{ + mapper = new QWidgetMapper; + Q_CHECK_PTR( mapper ); +} + +/*! + \internal + Destroys the global widget mapper. + \sa createMapper() +*/ + +void QWidget::destroyMapper() +{ + if ( !mapper ) // already gone + return; + QWidgetIntDictIt it( *((QWidgetIntDict*)mapper) ); + QWidgetMapper * myMapper = mapper; + mapper = 0; + register QWidget *w; + while ( (w=it.current()) ) { // remove parents widgets + ++it; + if ( !w->parentObj ) // widget is a parent + w->destroy( TRUE, TRUE ); + } + delete myMapper; +} + + +static QWidgetList *wListInternal( QWidgetMapper *mapper, bool onlyTopLevel ) +{ + QWidgetList *list = new QWidgetList; + Q_CHECK_PTR( list ); + if ( mapper ) { + QWidget *w; + QWidgetIntDictIt it( *((QWidgetIntDict*)mapper) ); + while ( (w=it.current()) ) { + ++it; + if ( !onlyTopLevel || w->isTopLevel() ) + list->append( w ); + } + } + return list; +} + +/*! + \internal + Returns a list of all widgets. + \sa tlwList(), QApplication::allWidgets() +*/ + +QWidgetList *QWidget::wList() +{ + return wListInternal( mapper, FALSE ); +} + +/*! + \internal + Returns a list of all top level widgets. + \sa wList(), QApplication::topLevelWidgets() +*/ + +QWidgetList *QWidget::tlwList() +{ + return wListInternal( mapper, TRUE ); +} + + +void QWidget::setWinId( WId id ) // set widget identifier +{ + if ( !mapper ) // mapper destroyed + return; + if ( winid ) + mapper->remove( winid ); + winid = id; +#if defined(Q_WS_X11) + hd = id; // X11: hd == ident +#endif + if ( id ) + mapper->insert( this ); +} + + +/*! + \internal + Returns a pointer to the block of extra widget data. +*/ + +QWExtra *QWidget::extraData() +{ + return extra; +} + + +/*! + \internal + Returns a pointer to the block of extra top level widget data. + + This data is guaranteed to exist for top level widgets. +*/ + +QTLWExtra *QWidget::topData() +{ + createTLExtra(); + return extra->topextra; +} + + +void QWidget::createTLExtra() +{ + if ( !extra ) + createExtra(); + if ( !extra->topextra ) { + QTLWExtra* x = extra->topextra = new QTLWExtra; +#if defined( Q_WS_WIN ) || defined( Q_WS_MAC ) + x->opacity = 255; +#endif +#ifndef QT_NO_WIDGET_TOPEXTRA + x->icon = 0; +#endif + x->focusData = 0; + x->fleft = x->fright = x->ftop = x->fbottom = 0; + x->incw = x->inch = 0; + x->basew = x->baseh = 0; + x->normalGeometry = QRect(0,0,-1,-1); +#if defined(Q_WS_X11) + x->embedded = 0; + x->parentWinId = 0; + x->spont_unmapped = 0; + x->dnd = 0; + x->uspos = 0; + x->ussize = 0; +#endif + x->savedFlags = 0; +#if defined(Q_WS_QWS) && !defined(QT_NO_QWS_MANAGER) + x->decor_allocated_region = QRegion(); + x->qwsManager = 0; +#endif + createTLSysExtra(); + } +} + +/*! + \internal + Creates the widget extra data. +*/ + +void QWidget::createExtra() +{ + if ( !extra ) { // if not exists + extra = new QWExtra; + Q_CHECK_PTR( extra ); + extra->minw = extra->minh = 0; + extra->maxw = extra->maxh = QWIDGETSIZE_MAX; + extra->bg_pix = 0; + extra->focus_proxy = 0; +#ifndef QT_NO_CURSOR + extra->curs = 0; +#endif + extra->topextra = 0; + extra->bg_mode = PaletteBackground; + extra->bg_mode_visual = PaletteBackground; + extra->bg_origin = WidgetOrigin; +#ifndef QT_NO_STYLE + extra->style = 0; +#endif + extra->size_policy = QSizePolicy( QSizePolicy::Preferred, + QSizePolicy::Preferred ); + createSysExtra(); + } +} + + +/*! + \internal + Deletes the widget extra data. +*/ + +void QWidget::deleteExtra() +{ + if ( extra ) { // if exists + delete extra->bg_pix; +#ifndef QT_NO_CURSOR + delete extra->curs; +#endif + deleteSysExtra(); + if ( extra->topextra ) { + deleteTLSysExtra(); +#ifndef QT_NO_WIDGET_TOPEXTRA + delete extra->topextra->icon; +#endif + delete extra->topextra->focusData; +#if defined(Q_WS_QWS) && !defined(QT_NO_QWS_MANAGER) + delete extra->topextra->qwsManager; +#endif + delete extra->topextra; + } + delete extra; + // extra->xic destroyed in QWidget::destroy() + extra = 0; + } +} + + +/*! + \internal + This function is called when a widget is hidden or destroyed. + It resets some application global pointers that should only refer active, + visible widgets. +*/ + +void QWidget::deactivateWidgetCleanup() +{ + // If this was the active application window, reset it + if ( this == QApplication::active_window ) + qApp->setActiveWindow( 0 ); + // If the is the active mouse press widget, reset it +#ifdef Q_WS_MAC + extern QGuardedPtr<QWidget> qt_button_down; +#else + extern QWidget *qt_button_down; +#endif + if ( this == (QWidget *)qt_button_down ) + qt_button_down = 0; +} + + +/*! + Returns a pointer to the widget with window identifer/handle \a + id. + + The window identifier type depends on the underlying window + system, see \c qwindowdefs.h for the actual definition. If there + is no widget with this identifier, 0 is returned. +*/ + +QWidget *QWidget::find( WId id ) +{ + return mapper ? mapper->find( id ) : 0; +} + +/*! + \fn QWidgetMapper *QWidget::wmapper() + \internal + Returns a pointer to the widget mapper. + + The widget mapper is an internal dictionary that is used to map from + window identifiers/handles to widget pointers. + \sa find(), id() +*/ + +/*! + \fn WFlags QWidget::getWFlags() const + + Returns the widget flags for this this widget. + + Widget flags are a combination of \l{Qt::WidgetFlags}. + + \sa testWFlags(), setWFlags(), clearWFlags() +*/ + +/*! + \fn void QWidget::setWFlags( WFlags f ) + + Sets the widget flags \a f. + + Widget flags are a combination of \l{Qt::WidgetFlags}. + + \sa testWFlags(), getWFlags(), clearWFlags() +*/ + +/*! + \fn void QWidget::clearWFlags( WFlags f ) + + Clears the widget flags \a f. + + Widget flags are a combination of \l{Qt::WidgetFlags}. + + \sa testWFlags(), getWFlags(), setWFlags() +*/ + + + +/*! + \fn WId QWidget::winId() const + + Returns the window system identifier of the widget. + + Portable in principle, but if you use it you are probably about to + do something non-portable. Be careful. + + \sa find() +*/ + +#ifndef QT_NO_STYLE +/*! + Returns the GUI style for this widget + + \sa QWidget::setStyle(), QApplication::setStyle(), QApplication::style() +*/ + +QStyle& QWidget::style() const +{ + if ( extra && extra->style ) + return *extra->style; + QStyle &ret = qApp->style(); + return ret; +} + +/*! + Sets the widget's GUI style to \a style. Ownership of the style + object is not transferred. + + If no style is set, the widget uses the application's style, + QApplication::style() instead. + + Setting a widget's style has no effect on existing or future child + widgets. + + \warning This function is particularly useful for demonstration + purposes, where you want to show Qt's styling capabilities. Real + applications should avoid it and use one consistent GUI style + instead. + + \sa style(), QStyle, QApplication::style(), QApplication::setStyle() +*/ + +void QWidget::setStyle( QStyle *style ) +{ + QStyle& old = QWidget::style(); + createExtra(); + extra->style = style; + if ( !testWFlags(WType_Desktop) // (except desktop) + && testWState(WState_Polished)) { // (and have been polished) + old.unPolish( this ); + QWidget::style().polish( this ); + } + styleChange( old ); +} + +/*! + \overload + + Sets the widget's GUI style to \a style using the QStyleFactory. +*/ +QStyle* QWidget::setStyle( const QString &style ) +{ + QStyle *s = QStyleFactory::create( style ); + setStyle( s ); + return s; +} + +/*! + This virtual function is called when the style of the widgets + changes. \a oldStyle is the previous GUI style; you can get the + new style from style(). + + Reimplement this function if your widget needs to know when its + GUI style changes. You will almost certainly need to update the + widget using update(). + + The default implementation updates the widget including its + geometry. + + \sa QApplication::setStyle(), style(), update(), updateGeometry() +*/ + +void QWidget::styleChange( QStyle& /* oldStyle */ ) +{ + update(); + updateGeometry(); +} + +#endif + +/*! + \property QWidget::isTopLevel + \brief whether the widget is a top-level widget + + A top-level widget is a widget which usually has a frame and a + \link QWidget::caption caption (title)\endlink. \link + QWidget::isPopup() Popup\endlink and \link QWidget::isDesktop() + desktop\endlink widgets are also top-level widgets. + + A top-level widget can have a \link QWidget::parentWidget() parent + widget\endlink. It will then be grouped with its parent and deleted + when the parent is deleted, minimized when the parent is minimized + etc. If supported by the window manager, it will also have a + common taskbar entry with its parent. + + QDialog and QMainWindow widgets are by default top-level, even if + a parent widget is specified in the constructor. This behavior is + specified by the \c WType_TopLevel widget flag. + + \sa topLevelWidget(), isDialog(), isModal(), isPopup(), isDesktop(), parentWidget() +*/ + +/*! + \property QWidget::isDialog + \brief whether the widget is a dialog widget + + A dialog widget is a secondary top-level widget, i.e. a top-level + widget with a parent. + + \sa isTopLevel(), QDialog +*/ + +/*! + \property QWidget::isPopup + \brief whether the widget is a popup widget + + A popup widget is created by specifying the widget flag \c + WType_Popup to the widget constructor. A popup widget is also a + top-level widget. + + \sa isTopLevel() +*/ + +/*! + \property QWidget::isDesktop + \brief whether the widget is a desktop widget, i.e. represents the desktop + + A desktop widget is also a top-level widget. + + \sa isTopLevel(), QApplication::desktop() +*/ + +/*! + \property QWidget::isModal + \brief whether the widget is a modal widget + + This property only makes sense for top-level widgets. A modal + widget prevents widgets in all other top-level widgets from + getting any input. + + \sa isTopLevel(), isDialog(), QDialog +*/ + +/*! + \property QWidget::underMouse + \brief whether the widget is under the mouse cursor + + This value is not updated properly during drag and drop + operations. + + \sa QEvent::Enter, QEvent::Leave +*/ + +/*! + \property QWidget::minimized + \brief whether this widget is minimized (iconified) + + This property is only relevant for top-level widgets. + + \sa showMinimized(), visible, show(), hide(), showNormal(), maximized +*/ +bool QWidget::isMinimized() const +{ return testWState(WState_Minimized); } + +/*! + Shows the widget minimized, as an icon. + + Calling this function only affects \link isTopLevel() top-level + widgets\endlink. + + \sa showNormal(), showMaximized(), show(), hide(), isVisible(), + isMinimized() +*/ +void QWidget::showMinimized() +{ + bool isMin = isMinimized(); + if (isMin && isVisible()) return; + + if (!isMin) + setWindowState((windowState() & ~WindowActive) | WindowMinimized); + show(); + if (!isTopLevel()) + QApplication::sendPostedEvents(this, QEvent::ShowMinimized); +} + +/*! + \property QWidget::maximized + \brief whether this widget is maximized + + This property is only relevant for top-level widgets. + + Note that due to limitations in some window-systems, this does not + always report the expected results (e.g. if the user on X11 + maximizes the window via the window manager, Qt has no way of + distinguishing this from any other resize). This is expected to + improve as window manager protocols evolve. + + \sa windowState(), showMaximized(), visible, show(), hide(), showNormal(), minimized +*/ +bool QWidget::isMaximized() const +{ return testWState(WState_Maximized); } + + + +/*! Returns the current window state. The window state is a OR'ed + combination of Qt::WindowState: \c WindowMinimized, \c + WindowMaximized, \c WindowFullScreen and \c WindowActive. + + \sa Qt::WindowState setWindowState() + */ +uint QWidget::windowState() const +{ + uint state = 0; + if (testWState(WState_Minimized)) + state |= WindowMinimized; + if (testWState(WState_Maximized)) + state |= WindowMaximized; + if (testWState(WState_FullScreen)) + state |= WindowFullScreen; + if (isActiveWindow()) + state |= WindowActive; + return state; +} + +/*! + \fn void QWidget::setWindowState(uint windowState) + + Sets the window state to \a windowState. The window state is a OR'ed + combination of Qt::WindowState: \c WindowMinimized, \c + WindowMaximized, \c WindowFullScreen and \c WindowActive. + + If the window is not visible (i.e. isVisible() returns FALSE), the + window state will take effect when show() is called. For visible + windows, the change is immediate. For example, to toggle between + full-screen and mormal mode, use the following code: + + \code + w->setWindowState(w->windowState() ^ WindowFullScreen); + \endcode + + In order to restore and activate a minimized window (while + preserving its maximized and/or full-screen state), use the following: + + \code + w->setWindowState(w->windowState() & ~WindowMinimized | WindowActive); + \endcode + + Note: On some window systems \c WindowActive is not immediate, and may be + ignored in certain cases. + + \sa Qt::WindowState windowState() +*/ + +/*! + \property QWidget::fullScreen + \brief whether the widget is full screen + + \sa windowState(), minimized, maximized +*/ +bool QWidget::isFullScreen() const +{ return testWState(WState_FullScreen); } + +/*! + Shows the widget in full-screen mode. + + Calling this function only affects top-level widgets. + + To return from full-screen mode, call showNormal(). + + Full-screen mode works fine under Windows, but has certain + problems under X. These problems are due to limitations of the + ICCCM protocol that specifies the communication between X11 + clients and the window manager. ICCCM simply does not understand + the concept of non-decorated full-screen windows. Therefore, the + best we can do is to request a borderless window and place and + resize it to fill the entire screen. Depending on the window + manager, this may or may not work. The borderless window is + requested using MOTIF hints, which are at least partially + supported by virtually all modern window managers. + + An alternative would be to bypass the window manager entirely and + create a window with the WX11BypassWM flag. This has other severe + problems though, like totally broken keyboard focus and very + strange effects on desktop changes or when the user raises other + windows. + + X11 window managers that follow modern post-ICCCM specifications + support full-screen mode properly. + + \sa showNormal(), showMaximized(), show(), hide(), isVisible() +*/ +void QWidget::showFullScreen() +{ + bool isFull = isFullScreen(); + if (isFull && isVisible()) + return; + + if (!isFull) + setWindowState(windowState() | WindowFullScreen); + show(); + if (!isTopLevel()) + QApplication::sendPostedEvents(this, QEvent::ShowFullScreen); + setActiveWindow(); +} + +/*! + Shows the widget maximized. + + Calling this function only affects \link isTopLevel() top-level + widgets\endlink. + + On X11, this function may not work properly with certain window + managers. See the \link geometry.html Window Geometry + documentation\endlink for an explanation. + + \sa setWindowState(), showNormal(), showMinimized(), show(), hide(), isVisible() +*/ +void QWidget::showMaximized() +{ + if (isMaximized() && isVisible() && !isMinimized()) + return; + + setWindowState((windowState() & ~WindowMinimized) | WindowMaximized); + show(); + if (!isTopLevel()) + QApplication::sendPostedEvents(this, QEvent::ShowMaximized); +} + +/*! + Restores the widget after it has been maximized or minimized. + + Calling this function only affects \link isTopLevel() top-level + widgets\endlink. + + \sa setWindowState(), showMinimized(), showMaximized(), show(), hide(), isVisible() +*/ +void QWidget::showNormal() +{ + setWindowState(WindowNoState); + show(); + if (!isTopLevel()) + QApplication::sendPostedEvents(this, QEvent::ShowNormal); +} + +/*! + Returns TRUE if this widget would become enabled if \a ancestor is + enabled; otherwise returns FALSE. + + This is the case if neither the widget itself nor every parent up + to but excluding \a ancestor has been explicitly disabled. + + isEnabledTo(0) is equivalent to isEnabled(). + + \sa setEnabled() enabled +*/ + +bool QWidget::isEnabledTo( QWidget* ancestor ) const +{ + const QWidget * w = this; + while ( w && !w->testWState(WState_ForceDisabled) + && !w->isTopLevel() + && w->parentWidget() + && w->parentWidget() != ancestor ) + w = w->parentWidget(); + return !w->testWState( WState_ForceDisabled ); +} + + +/*! + \fn bool QWidget::isEnabledToTLW() const + \obsolete + + This function is deprecated. It is equivalent to isEnabled() +*/ + +/*! + \property QWidget::enabled + \brief whether the widget is enabled + + An enabled widget receives keyboard and mouse events; a disabled + widget does not. In fact, an enabled widget only receives keyboard + events when it is in focus. + + Some widgets display themselves differently when they are + disabled. For example a button might draw its label grayed out. If + your widget needs to know when it becomes enabled or disabled, you + can reimplement the enabledChange() function. + + Disabling a widget implicitly disables all its children. Enabling + respectively enables all child widgets unless they have been + explicitly disabled. + + \sa isEnabled(), isEnabledTo(), QKeyEvent, QMouseEvent, enabledChange() +*/ +void QWidget::setEnabled( bool enable ) +{ + if ( enable ) + clearWState( WState_ForceDisabled ); + else + setWState( WState_ForceDisabled ); + + if ( !isTopLevel() && parentWidget() && + !parentWidget()->isEnabled() && enable ) + return; // nothing we can do + + if ( enable ) { + if ( testWState(WState_Disabled) ) { + clearWState( WState_Disabled ); + setBackgroundFromMode(); + enabledChange( !enable ); + if ( children() ) { + QObjectListIt it( *children() ); + QWidget *w; + while( (w = (QWidget *)it.current()) != 0 ) { + ++it; + if ( w->isWidgetType() && + !w->testWState( WState_ForceDisabled ) ) + w->setEnabled( TRUE ); + } + } + } + } else { + if ( !testWState(WState_Disabled) ) { + if (focusWidget() == this) { + bool parentIsEnabled = (!parentWidget() || parentWidget()->isEnabled()); + if (!parentIsEnabled || !focusNextPrevChild(TRUE)) + clearFocus(); + } + setWState( WState_Disabled ); + setBackgroundFromMode(); + enabledChange( !enable ); + if ( children() ) { + QObjectListIt it( *children() ); + QWidget *w; + while( (w = (QWidget *)it.current()) != 0 ) { + ++it; + if ( w->isWidgetType() && w->isEnabled() ) { + w->setEnabled( FALSE ); + w->clearWState( WState_ForceDisabled ); + } + } + } + } + } +#if defined(Q_WS_X11) + if ( testWState( WState_OwnCursor ) ) { + // enforce the windows behavior of clearing the cursor on + // disabled widgets + + extern void qt_x11_enforce_cursor( QWidget * w ); // defined in qwidget_x11.cpp + qt_x11_enforce_cursor( this ); + } +#endif +#ifdef Q_WS_WIN + QInputContext::enable( this, im_enabled & !((bool)testWState(WState_Disabled)) ); +#endif +} + +/*! + Disables widget input events if \a disable is TRUE; otherwise + enables input events. + + See the \l enabled documentation for more information. + + \sa isEnabledTo(), QKeyEvent, QMouseEvent, enabledChange() +*/ +void QWidget::setDisabled( bool disable ) +{ + setEnabled( !disable ); +} + +/*! + \fn void QWidget::enabledChange( bool oldEnabled ) + + This virtual function is called from setEnabled(). \a oldEnabled + is the previous setting; you can get the new setting from + isEnabled(). + + Reimplement this function if your widget needs to know when it + becomes enabled or disabled. You will almost certainly need to + update the widget using update(). + + The default implementation repaints the visible part of the + widget. + + \sa setEnabled(), isEnabled(), repaint(), update(), clipRegion() +*/ + +void QWidget::enabledChange( bool ) +{ + update(); +#if defined(QT_ACCESSIBILITY_SUPPORT) + QAccessible::updateAccessibility( this, 0, QAccessible::StateChanged ); +#endif +} + +/*! + \fn void QWidget::windowActivationChange( bool oldActive ) + + This virtual function is called for a widget when its window is + activated or deactivated by the window system. \a oldActive is the + previous state; you can get the new setting from isActiveWindow(). + + Reimplement this function if your widget needs to know when its + window becomes activated or deactivated. + + The default implementation updates the visible part of the widget + if the inactive and the active colorgroup are different for colors + other than the highlight and link colors. + + \sa setActiveWindow(), isActiveWindow(), update(), palette() +*/ + +void QWidget::windowActivationChange( bool ) +{ +#ifndef QT_NO_PALETTE + if ( !isVisible() ) + return; + + const QColorGroup &acg = palette().active(); + const QColorGroup &icg = palette().inactive(); + + if ( acg != icg ) { + BackgroundMode bm = backgroundMode(); + QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode(bm); + if ( bm > NoBackground && acg.brush(role) != icg.brush(role) ) + setBackgroundFromMode(); + else if ( acg.background() == icg.background() && + acg.base() == icg.base() && + acg.text() == icg.text() && + acg.foreground() == icg.foreground() && + acg.button() == icg.button() && + acg.buttonText() == icg.buttonText() && + acg.brightText() == icg.brightText() && + acg.dark() == icg.dark() && + acg.light() == icg.light() && + acg.mid() == icg.mid() && + acg.midlight() == icg.midlight() && + acg.shadow() == icg.shadow() ) + return; + update(); + } +#endif +} + +/*! + \property QWidget::frameGeometry + \brief geometry of the widget relative to its parent including any + window frame + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of geometry issues with top-level widgets. + + \sa geometry() x() y() pos() +*/ +QRect QWidget::frameGeometry() const +{ + if (isTopLevel() && ! isPopup()) { + if (fstrut_dirty) + updateFrameStrut(); + QWidget *that = (QWidget *) this; + QTLWExtra *top = that->topData(); + return QRect(crect.x() - top->fleft, + crect.y() - top->ftop, + crect.width() + top->fleft + top->fright, + crect.height() + top->ftop + top->fbottom); + } + return crect; +} + +/*! \property QWidget::x + \brief the x coordinate of the widget relative to its parent including + any window frame + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa frameGeometry, y, pos +*/ +int QWidget::x() const +{ + if (isTopLevel() && ! isPopup()) { + if (fstrut_dirty) + updateFrameStrut(); + QWidget *that = (QWidget *) this; + return crect.x() - that->topData()->fleft; + } + return crect.x(); +} + +/*! + \property QWidget::y + \brief the y coordinate of the widget relative to its parent and + including any window frame + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa frameGeometry, x, pos +*/ +int QWidget::y() const +{ + if (isTopLevel() && ! isPopup()) { + if (fstrut_dirty) + updateFrameStrut(); + QWidget *that = (QWidget *) this; + return crect.y() - that->topData()->ftop; + } + return crect.y(); +} + +/*! + \property QWidget::pos + \brief the position of the widget within its parent widget + + If the widget is a top-level widget, the position is that of the + widget on the desktop, including its frame. + + When changing the position, the widget, if visible, receives a + move event (moveEvent()) immediately. If the widget is not + currently visible, it is guaranteed to receive an event before it + is shown. + + move() is virtual, and all other overloaded move() implementations + in Qt call it. + + \warning Calling move() or setGeometry() inside moveEvent() can + lead to infinite recursion. + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa frameGeometry, size x(), y() +*/ +QPoint QWidget::pos() const +{ + if (isTopLevel() && ! isPopup()) { + if (fstrut_dirty) + updateFrameStrut(); + QWidget *that = (QWidget *) this; + QTLWExtra *top = that->topData(); + return QPoint(crect.x() - top->fleft, crect.y() - top->ftop); + } + return crect.topLeft(); +} + +/*! + \property QWidget::geometry + \brief the geometry of the widget relative to its parent and + excluding the window frame + + When changing the geometry, the widget, if visible, receives a + move event (moveEvent()) and/or a resize event (resizeEvent()) + immediately. If the widget is not currently visible, it is + guaranteed to receive appropriate events before it is shown. + + The size component is adjusted if it lies outside the range + defined by minimumSize() and maximumSize(). + + setGeometry() is virtual, and all other overloaded setGeometry() + implementations in Qt call it. + + \warning Calling setGeometry() inside resizeEvent() or moveEvent() + can lead to infinite recursion. + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa frameGeometry(), rect(), move(), resize(), moveEvent(), + resizeEvent(), minimumSize(), maximumSize() +*/ + +/*! + \property QWidget::size + \brief the size of the widget excluding any window frame + + When resizing, the widget, if visible, receives a resize event + (resizeEvent()) immediately. If the widget is not currently + visible, it is guaranteed to receive an event before it is shown. + + The size is adjusted if it lies outside the range defined by + minimumSize() and maximumSize(). Furthermore, the size is always + at least QSize(1, 1). For toplevel widgets, the minimum size + might be larger, depending on the window manager. + + If you want a top-level window to have a fixed size, call + setResizeMode( QLayout::FreeResize ) on its layout. + + resize() is virtual, and all other overloaded resize() + implementations in Qt call it. + + \warning Calling resize() or setGeometry() inside resizeEvent() can + lead to infinite recursion. + + \sa pos, geometry, minimumSize, maximumSize, resizeEvent() +*/ + +/*! + \property QWidget::width + \brief the width of the widget excluding any window frame + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa geometry, height, size +*/ + +/*! + \property QWidget::height + \brief the height of the widget excluding any window frame + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa geometry, width, size +*/ + +/*! + \property QWidget::rect + \brief the internal geometry of the widget excluding any window + frame + + The rect property equals QRect(0, 0, width(), height()). + + See the \link geometry.html Window Geometry documentation\endlink + for an overview of top-level widget geometry. + + \sa size +*/ + +/*! + \property QWidget::childrenRect + \brief the bounding rectangle of the widget's children + + Hidden children are excluded. + + \sa childrenRegion() geometry() +*/ + +QRect QWidget::childrenRect() const +{ + QRect r( 0, 0, 0, 0 ); + if ( !children() ) + return r; + QObjectListIt it( *children() ); + QObject *obj; + while ( (obj = it.current()) ) { + ++it; + if ( obj->isWidgetType() && !((QWidget*)obj)->isHidden() && !((QWidget*)obj)->isTopLevel()) + r = r.unite( ((QWidget*)obj)->geometry() ); + } + return r; +} + +/*! + \property QWidget::childrenRegion + \brief the combined region occupied by the widget's children + + Hidden children are excluded. + + \sa childrenRect() geometry() +*/ + +QRegion QWidget::childrenRegion() const +{ + QRegion r; + if ( !children() ) + return r; + QObjectListIt it( *children() ); // iterate over all children + QObject *obj; + while ( (obj=it.current()) ) { + ++it; + if ( obj->isWidgetType() && !((QWidget*)obj)->isHidden() && !((QWidget*)obj)->isTopLevel()) + r = r.unite( ((QWidget*)obj)->geometry() ); + } + return r; +} + + +/*! + \property QWidget::minimumSize + \brief the widget's minimum size + + The widget cannot be resized to a smaller size than the minimum + widget size. The widget's size is forced to the minimum size if + the current size is smaller. + + If you use a layout inside the widget, the minimum size will be + set by the layout and not by setMinimumSize(), unless you set the + layout's resize mode to QLayout::FreeResize. + + \sa minimumWidth, minimumHeight, maximumSize, sizeIncrement + QLayout::setResizeMode() +*/ + +QSize QWidget::minimumSize() const +{ + return extra ? QSize( extra->minw, extra->minh ) : QSize( 0, 0 ); +} + +/*! + \property QWidget::maximumSize + \brief the widget's maximum size + + The widget cannot be resized to a larger size than the maximum + widget size. + + \sa maximumWidth(), maximumHeight(), setMaximumSize(), + minimumSize(), sizeIncrement() +*/ + +QSize QWidget::maximumSize() const +{ + return extra ? QSize( extra->maxw, extra->maxh ) + : QSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ); +} + + +/*! + \property QWidget::minimumWidth + \brief the widget's minimum width + + This property corresponds to minimumSize().width(). + + \sa minimumSize, minimumHeight +*/ + +/*! + \property QWidget::minimumHeight + \brief the widget's minimum height + + This property corresponds to minimumSize().height(). + + \sa minimumSize, minimumWidth +*/ + +/*! + \property QWidget::maximumWidth + \brief the widget's maximum width + + This property corresponds to maximumSize().width(). + + \sa maximumSize, maximumHeight +*/ + +/*! + \property QWidget::maximumHeight + \brief the widget's maximum height + + This property corresponds to maximumSize().height(). + + \sa maximumSize, maximumWidth +*/ + +/*! + \property QWidget::sizeIncrement + \brief the size increment of the widget + + When the user resizes the window, the size will move in steps of + sizeIncrement().width() pixels horizontally and + sizeIncrement.height() pixels vertically, with baseSize() as the + basis. Preferred widget sizes are for non-negative integers \e i + and \e j: + \code + width = baseSize().width() + i * sizeIncrement().width(); + height = baseSize().height() + j * sizeIncrement().height(); + \endcode + + Note that while you can set the size increment for all widgets, it + only affects top-level widgets. + + \warning The size increment has no effect under Windows, and may + be disregarded by the window manager on X. + + \sa size, minimumSize, maximumSize +*/ +QSize QWidget::sizeIncrement() const +{ + return ( extra && extra->topextra ) + ? QSize( extra->topextra->incw, extra->topextra->inch ) + : QSize( 0, 0 ); +} + +/*! + \property QWidget::baseSize + \brief the base size of the widget + + The base size is used to calculate a proper widget size if the + widget defines sizeIncrement(). + + \sa setSizeIncrement() +*/ + +QSize QWidget::baseSize() const +{ + return ( extra != 0 && extra->topextra != 0 ) + ? QSize( extra->topextra->basew, extra->topextra->baseh ) + : QSize( 0, 0 ); +} + +/*! + Sets both the minimum and maximum sizes of the widget to \a s, + thereby preventing it from ever growing or shrinking. + + \sa setMaximumSize() setMinimumSize() +*/ + +void QWidget::setFixedSize( const QSize & s) +{ + setMinimumSize( s ); + setMaximumSize( s ); + resize( s ); +} + + +/*! + \overload void QWidget::setFixedSize( int w, int h ) + + Sets the width of the widget to \a w and the height to \a h. +*/ + +void QWidget::setFixedSize( int w, int h ) +{ + setMinimumSize( w, h ); + setMaximumSize( w, h ); + resize( w, h ); +} + +void QWidget::setMinimumWidth( int w ) +{ + setMinimumSize( w, minimumSize().height() ); +} + +void QWidget::setMinimumHeight( int h ) +{ + setMinimumSize( minimumSize().width(), h ); +} + +void QWidget::setMaximumWidth( int w ) +{ + setMaximumSize( w, maximumSize().height() ); +} + +void QWidget::setMaximumHeight( int h ) +{ + setMaximumSize( maximumSize().width(), h ); +} + +/*! + Sets both the minimum and maximum width of the widget to \a w + without changing the heights. Provided for convenience. + + \sa sizeHint() minimumSize() maximumSize() setFixedSize() +*/ + +void QWidget::setFixedWidth( int w ) +{ + setMinimumSize( w, minimumSize().height() ); + setMaximumSize( w, maximumSize().height() ); +} + + +/*! + Sets both the minimum and maximum heights of the widget to \a h + without changing the widths. Provided for convenience. + + \sa sizeHint() minimumSize() maximumSize() setFixedSize() +*/ + +void QWidget::setFixedHeight( int h ) +{ + setMinimumSize( minimumSize().width(), h ); + setMaximumSize( maximumSize().width(), h ); +} + + +/*! + Translates the widget coordinate \a pos to the coordinate system + of \a parent. The \a parent must not be 0 and must be a parent + of the calling widget. + + \sa mapFrom() mapToParent() mapToGlobal() hasMouse() +*/ + +QPoint QWidget::mapTo( QWidget * parent, const QPoint & pos ) const +{ + QPoint p = pos; + if ( parent ) { + const QWidget * w = this; + while ( w != parent ) { + p = w->mapToParent( p ); + w = w->parentWidget(); + } + } + return p; +} + + +/*! + Translates the widget coordinate \a pos from the coordinate system + of \a parent to this widget's coordinate system. The \a parent + must not be 0 and must be a parent of the calling widget. + + \sa mapTo() mapFromParent() mapFromGlobal() hasMouse() +*/ + +QPoint QWidget::mapFrom( QWidget * parent, const QPoint & pos ) const +{ + QPoint p( pos ); + if ( parent ) { + const QWidget * w = this; + while ( w != parent ) { + p = w->mapFromParent( p ); + w = w->parentWidget(); + } + } + return p; +} + + +/*! + Translates the widget coordinate \a pos to a coordinate in the + parent widget. + + Same as mapToGlobal() if the widget has no parent. + + \sa mapFromParent() mapTo() mapToGlobal() hasMouse() +*/ + +QPoint QWidget::mapToParent( const QPoint &pos ) const +{ + return pos + crect.topLeft(); +} + +/*! + Translates the parent widget coordinate \a pos to widget + coordinates. + + Same as mapFromGlobal() if the widget has no parent. + + \sa mapToParent() mapFrom() mapFromGlobal() hasMouse() +*/ + +QPoint QWidget::mapFromParent( const QPoint &pos ) const +{ + return pos - crect.topLeft(); +} + + +/*! + Returns the top-level widget for this widget, i.e. the next + ancestor widget that has (or could have) a window-system frame. + + If the widget is a top-level, the widget itself is returned. + + Typical usage is changing the window caption: + + \code + aWidget->topLevelWidget()->setCaption( "New Caption" ); + \endcode + + \sa isTopLevel() +*/ + +QWidget *QWidget::topLevelWidget() const +{ + QWidget *w = (QWidget *)this; + QWidget *p = w->parentWidget(); + while ( !w->testWFlags(WType_TopLevel) && p ) { + w = p; + p = p->parentWidget(); + } + return w; +} + + +/*! + \property QWidget::paletteForegroundColor + \brief the foreground color of the widget + + setPaletteForegroundColor() is a convenience function that creates + and sets a modified QPalette with setPalette(). The palette is + modified according to the widget's \e {background mode}. For + example, if the background mode is \c PaletteButton the palette entry + \c QColorGroup::ButtonText is set to color. + + \sa setPalette() QApplication::setPalette() backgroundMode() + foregroundColor() setBackgroundMode() setEraseColor() +*/ +const QColor &QWidget::paletteForegroundColor() const +{ +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + return colorGroup().color( QPalette::foregroundRoleFromMode(mode) ); +#else + return Qt::black; +#endif +} + +void QWidget::setPaletteForegroundColor( const QColor & color ) +{ +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + QPalette pal = palette(); + QColorGroup::ColorRole role = QPalette::foregroundRoleFromMode( mode ); + pal.setColor( QPalette::Active, role, color ); + pal.setColor( QPalette::Inactive, role, color ); + pal.setColor( QPalette::Disabled, role, color ); + setPalette( pal ); +#endif +} + + +/*! + Same as paletteForegroundColor() + */ +const QColor &QWidget::foregroundColor() const +{ + return paletteForegroundColor(); +} + + +/*! + \fn const QColor& QWidget::eraseColor() const + + Returns the erase color of the widget. + + \sa setEraseColor() setErasePixmap() backgroundColor() +*/ + +/*! + Sets the erase color of the widget to \a color. + + The erase color is the color the widget is to be cleared to before + paintEvent() is called. If there is an erase pixmap (set using + setErasePixmap()), then this property has an indeterminate value. + + \sa erasePixmap(), backgroundColor(), backgroundMode(), palette() +*/ +void QWidget::setEraseColor( const QColor & color ) +{ + setBackgroundModeDirect( FixedColor ); + setBackgroundColorDirect( color ); + update(); +} + +/*! + Returns the widget's erase pixmap. + + \sa setErasePixmap() eraseColor() +*/ +const QPixmap *QWidget::erasePixmap() const +{ + return ( extra && extra->bg_pix ) ? extra->bg_pix : 0; +} + +/*! + Sets the widget's erase pixmap to \a pixmap. + + This pixmap is used to clear the widget before paintEvent() is + called. +*/ +void QWidget::setErasePixmap( const QPixmap &pixmap ) +{ + // This function is called with a null pixmap by setBackgroundEmpty(). + setBackgroundPixmapDirect( pixmap ); + setBackgroundModeDirect( FixedPixmap ); + update(); +} + +void QWidget::setBackgroundFromMode() +{ +#ifndef QT_NO_PALETTE + QColorGroup::ColorRole r = QColorGroup::Background; + if ( extra ) { + int i = (BackgroundMode)extra->bg_mode; + if ( i == FixedColor || i == FixedPixmap || i == NoBackground ) { + // Mode is for fixed color, not one based on palette, + // so nothing to do. + return; + } + switch( i ) { + case PaletteForeground: + r = QColorGroup::Foreground; + break; + case PaletteButton: + r = QColorGroup::Button; + break; + case PaletteLight: + r = QColorGroup::Light; + break; + case PaletteMidlight: + r = QColorGroup::Midlight; + break; + case PaletteDark: + r = QColorGroup::Dark; + break; + case PaletteMid: + r = QColorGroup::Mid; + break; + case PaletteText: + r = QColorGroup::Text; + break; + case PaletteBrightText: + r = QColorGroup::BrightText; + break; + case PaletteBase: + r = QColorGroup::Base; + break; + case PaletteBackground: + r = QColorGroup::Background; + break; + case PaletteShadow: + r = QColorGroup::Shadow; + break; + case PaletteHighlight: + r = QColorGroup::Highlight; + break; + case PaletteHighlightedText: + r = QColorGroup::HighlightedText; + break; + case PaletteButtonText: + r = QColorGroup::ButtonText; + break; + case X11ParentRelative: +#if defined(Q_WS_X11) + setBackgroundX11Relative(); +#endif + return; + } + } + const QColorGroup &cg = colorGroup(); + QPixmap * p = cg.brush( r ).pixmap(); + if ( p ) + setBackgroundPixmapDirect( *p ); + else + setBackgroundColorDirect( cg.color( r ) ); +#endif +} + +/*! + \enum Qt::BackgroundMode + + This enum describes how the background of a widget changes, as the + widget's palette changes. + + The background is what the widget contains when \link + QWidget::paintEvent() paintEvent()\endlink is called. To minimize + flicker, this should be the most common color or pixmap in the + widget. For \c PaletteBackground, use colorGroup().brush( \c + QColorGroup::Background ), and so on. + + \value PaletteForeground + \value PaletteBackground + \value PaletteButton + \value PaletteLight + \value PaletteMidlight + \value PaletteDark + \value PaletteMid + \value PaletteText + \value PaletteBrightText + \value PaletteButtonText + \value PaletteBase + \value PaletteShadow + \value PaletteHighlight + \value PaletteHighlightedText + \value PaletteLink + \value PaletteLinkVisited + \value X11ParentRelative (internal use only) + + The final three values have special meaning: + + \value NoBackground the widget is not cleared before paintEvent(). + If the widget's paint event always draws on all the pixels, using + this mode can be both fast and flicker-free. + \value FixedColor the widget is cleared to a fixed color, normally + different from all the ones in the palette(). Set using \link + QWidget::setPaletteBackgroundColor() + setPaletteBackgroundColor()\endlink. + \value FixedPixmap the widget is cleared to a fixed pixmap, + normally different from all the ones in the palette(). Set using + \link QWidget::setPaletteBackgroundPixmap() + setPaletteBackgroundPixmap()\endlink. + + Although \c FixedColor and \c FixedPixmap are sometimes just + right, if you use them, make sure that you test your application + when the desktop color scheme has been changed. (On X11, a quick + way to test this is e.g. "./myapp -bg paleblue". On Windows, you + must use the control panel.) + + \sa QWidget::setBackgroundMode() QWidget::backgroundMode() + QWidget::setBackgroundPixmap() QWidget::setPaletteBackgroundColor() +*/ + +/*! + \property QWidget::backgroundMode + \brief the color role used for painting the background of the widget + + setPaletteBackgroundColor() reads this property to determine which + entry of the \link QWidget::palette palette\endlink to set. + + For most widgets the default suffices (\c PaletteBackground, + typically gray), but some need to use \c PaletteBase (the + background color for text output, typically white) or another + role. + + QListBox, which is "sunken" and uses the base color to contrast + with its environment, does this in its constructor: + + \code + setBackgroundMode( PaletteBase ); + \endcode + + You will never need to set the background mode of a built-in + widget in Qt, but you might consider setting it in your custom + widgets, so that setPaletteBackgroundColor() works as expected. + + Note that two of the BackgroundMode values make no sense for + setBackgroundMode(), namely \c FixedPixmap and \c FixedColor. You + must call setBackgroundPixmap() and setPaletteBackgroundColor() + instead. +*/ +Qt::BackgroundMode QWidget::backgroundMode() const +{ + return extra ? (BackgroundMode) extra->bg_mode : PaletteBackground; +} + +void QWidget::setBackgroundMode( BackgroundMode m ) +{ + setBackgroundMode( m, m ); + if ( (widget_state & (WState_Visible|WState_BlockUpdates)) == + WState_Visible ) + update(); +} + + +/*! + \overload + + Sets the widget's own background mode to \a m and the visual + background mode to \a visual. The visual background mode is used + with the designable properties \c backgroundColor, \c + foregroundColor and \c backgroundPixmap. + + For complex controls, the logical background mode sometimes + differs from a widget's own background mode. A spinbox for example + has \c PaletteBackground as background mode (typically dark gray), + while it's embedded lineedit control uses \c PaletteBase + (typically white). Since the lineedit covers most of the visual + area of a spinbox, it defines \c PaletteBase to be its \a visual + background mode. Changing the \c backgroundColor property thus + changes the lineedit control's background, which is exactly what + the user expects in \e{Qt Designer}. +*/ +void QWidget::setBackgroundMode( BackgroundMode m, BackgroundMode visual ) +{ + if ( m == NoBackground ) { + setBackgroundEmpty(); + } else if ( m == FixedColor || m == FixedPixmap ) { +#if defined(QT_DEBUG) + qWarning( "QWidget::setBackgroundMode: FixedColor or FixedPixmap makes" + " no sense" ); +#endif + return; + } + setBackgroundModeDirect(m); + if ( m != visual && !extra ) + createExtra(); + if ( extra ) + extra->bg_mode_visual = visual; +} + + +/*! + \internal +*/ +void QWidget::setBackgroundModeDirect( BackgroundMode m ) +{ + if ( m == PaletteBackground && !extra ) + return; + + createExtra(); + if ( (BackgroundMode)extra->bg_mode != m ) { + extra->bg_mode = m; + extra->bg_mode_visual = m; + setBackgroundFromMode(); + } +} + +/*! + \property QWidget::paletteBackgroundColor + \brief the background color of the widget + + The palette background color is usually set implicitly by + setBackgroundMode(), although it can also be set explicitly by + setPaletteBackgroundColor(). setPaletteBackgroundColor() is a + convenience function that creates and sets a modified QPalette + with setPalette(). The palette is modified according to the + widget's background mode. For example, if the background mode is + \c PaletteButton the color used for the palette's \c + QColorGroup::Button color entry is set. + + If there is a background pixmap (set using + setPaletteBackgroundPixmap()), then the return value of this + function is indeterminate. + + \sa paletteBackgroundPixmap, paletteForegroundColor, palette, colorGroup() +*/ +const QColor & QWidget::paletteBackgroundColor() const +{ +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + switch( mode ) { + case FixedColor: + case FixedPixmap : + case NoBackground: + case X11ParentRelative: + return eraseColor(); + default: + QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode( mode ); + return colorGroup().color( role ); + } +#else + return eraseColor(); +#endif +} + +void QWidget::setPaletteBackgroundColor( const QColor &color ) +{ +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + switch( mode ) { + case FixedColor: + case FixedPixmap : + case NoBackground: + case X11ParentRelative: + setEraseColor( color ); + break; + default: + QPalette pal = palette(); + QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode( mode ); + pal.setColor( QPalette::Active, role, color ); + pal.setColor( QPalette::Inactive, role, color ); + pal.setColor( QPalette::Disabled, role, color ); + setPalette( pal ); + break; + } +#else + setEraseColor( color ); +#endif +} + + +/*! + \property QWidget::paletteBackgroundPixmap + \brief the background pixmap of the widget + + The palette background pixmap is usually set implicitly by + setBackgroundMode(), although it can also be set explicitly by + setPaletteBackgroundPixmap(). setPaletteBackgroundPixmap() is a + convenience function that creates and sets a modified QPalette + with setPalette(). The palette is modified according to the + widget's background mode. For example, if the background mode is + \c PaletteButton the pixmap used for the palette's + \c QColorGroup::Button color entry is set. + + If there is a plain background color (set using + setPaletteBackgroundColor()), then this function returns 0. + + \sa paletteBackgroundColor, paletteForegroundColor, palette, colorGroup() +*/ +const QPixmap *QWidget::paletteBackgroundPixmap() const +{ +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + switch( mode ) { + case FixedColor: + case FixedPixmap : + case NoBackground: + case X11ParentRelative: + return erasePixmap(); + default: + QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode( mode ); + return palette().brush( QPalette::Active, role ).pixmap(); + } +#else + return erasePixmap(); +#endif +} + +void QWidget::setPaletteBackgroundPixmap( const QPixmap &pixmap ) +{ +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + switch( mode ) { + case FixedColor: + case FixedPixmap : + case NoBackground: + case X11ParentRelative: + setErasePixmap( pixmap ); + break; + default: + QPalette pal = palette(); + QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode( mode ); + pal.setBrush( QPalette::Active, role, QBrush( pal.color( QPalette::Active, role ), pixmap ) ); + pal.setBrush( QPalette::Inactive, role, QBrush( pal.color( QPalette::Inactive, role ), pixmap ) ); + pal.setBrush( QPalette::Disabled, role, QBrush( pal.color( QPalette::Disabled, role ), pixmap ) ); + setPalette( pal ); + break; + } +#else + setErasePixmap( pixmap ); +#endif +} + + +/*! + \property QWidget::backgroundBrush + \brief the widget's background brush + + The background brush depends on a widget's palette and its + background mode. + + \sa backgroundColor(), backgroundPixmap(), eraseColor(), palette, + QApplication::setPalette() +*/ +const QBrush& QWidget::backgroundBrush() const +{ + static QBrush noBrush; +#ifndef QT_NO_PALETTE + BackgroundMode mode = extra ? (BackgroundMode) extra->bg_mode_visual : PaletteBackground; + switch( mode ) { + case FixedColor: + case FixedPixmap : + case NoBackground: + case X11ParentRelative: + return noBrush; + default: + QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode( mode ); + return colorGroup().brush( role ); + } +#else + return noBrush; +#endif +} + + +/*! + \property QWidget::colorGroup + \brief the current color group of the widget palette + + The color group is determined by the state of the widget. A + disabled widget has the QPalette::disabled() color group, a widget + with keyboard focus has the QPalette::active() color group, and an + inactive widget has the QPalette::inactive() color group. + + \sa palette +*/ +#ifndef QT_NO_PALETTE +const QColorGroup &QWidget::colorGroup() const +{ + if ( !isEnabled() ) + return palette().disabled(); + else if ( !isVisible() || isActiveWindow() ) + return palette().active(); + else + return palette().inactive(); +} +#endif + +/*! + \property QWidget::palette + \brief the widget's palette + + As long as no special palette has been set, or after unsetPalette() + has been called, this is either a special palette for the widget + class, the parent's palette or (if this widget is a top level + widget), the default application palette. + + Instead of defining an entirely new palette, you can also use the + \link QWidget::paletteBackgroundColor paletteBackgroundColor\endlink, + \link QWidget::paletteBackgroundPixmap paletteBackgroundPixmap\endlink and + \link QWidget::paletteForegroundColor paletteForegroundColor\endlink + convenience properties to change a widget's + background and foreground appearance only. + + \sa ownPalette, colorGroup(), QApplication::palette() +*/ + +#ifndef QT_NO_PALETTE +void QWidget::setPalette( const QPalette &palette ) +{ + own_palette = TRUE; + if ( pal == palette ) + return; + QPalette old = pal; + pal = palette; + setBackgroundFromMode(); + QEvent ev( QEvent::PaletteChange ); + QApplication::sendEvent( this, &ev ); + if ( children() ) { + QEvent e( QEvent::ParentPaletteChange ); + QObjectListIt it( *children() ); + QWidget *w; + while( (w=(QWidget *)it.current()) != 0 ) { + ++it; + if ( w->isWidgetType() ) + QApplication::sendEvent( w, &e ); + } + } + paletteChange( old ); + update(); +} + +void QWidget::unsetPalette() +{ + // reset the palette + setPalette( qt_naturalWidgetPalette( this ) ); + own_palette = FALSE; +} + +/*! + \fn void QWidget::setPalette( const QPalette&, bool ) + \obsolete + + Use setPalette( const QPalette& p ) instead. +*/ + +/*! + \fn void QWidget::paletteChange( const QPalette &oldPalette ) + + This virtual function is called from setPalette(). \a oldPalette + is the previous palette; you can get the new palette from + palette(). + + Reimplement this function if your widget needs to know when its + palette changes. + + \sa setPalette(), palette() +*/ + +void QWidget::paletteChange( const QPalette & ) +{ +} +#endif // QT_NO_PALETTE + +/*! + \property QWidget::font + \brief the font currently set for the widget + + The fontInfo() function reports the actual font that is being used + by the widget. + + As long as no special font has been set, or after unsetFont() is + called, this is either a special font for the widget class, the + parent's font or (if this widget is a top level widget), the + default application font. + + This code fragment sets a 12 point helvetica bold font: + \code + QFont f( "Helvetica", 12, QFont::Bold ); + setFont( f ); + \endcode + + In addition to setting the font, setFont() informs all children + about the change. + + \sa fontChange() fontInfo() fontMetrics() ownFont() +*/ +void QWidget::setFont( const QFont &font ) +{ + own_font = TRUE; + if ( fnt == font && fnt.d->mask == font.d->mask ) + return; + QFont old = fnt; + fnt = font.resolve( qt_naturalWidgetFont( this ) ); +#if defined(Q_WS_X11) + // make sure the font set on this widget is associated with the correct screen + fnt.x11SetScreen( x11Screen() ); +#endif + if ( children() ) { + QEvent e( QEvent::ParentFontChange ); + QObjectListIt it( *children() ); + QWidget *w; + while( (w=(QWidget *)it.current()) != 0 ) { + ++it; + if ( w->isWidgetType() ) + QApplication::sendEvent( w, &e ); + } + } + if ( hasFocus() ) + setFontSys(); + fontChange( old ); +} + +void QWidget::unsetFont() +{ + // reset the font + setFont( qt_naturalWidgetFont( this ) ); + own_font = FALSE; +} + +/*! + \fn void QWidget::setFont( const QFont&, bool ) + \obsolete + + Use setFont(const QFont& font) instead. +*/ + +/*! + \fn void QWidget::fontChange( const QFont &oldFont ) + + This virtual function is called from setFont(). \a oldFont is the + previous font; you can get the new font from font(). + + Reimplement this function if your widget needs to know when its + font changes. You will almost certainly need to update the widget + using update(). + + The default implementation updates the widget including its + geometry. + + \sa setFont(), font(), update(), updateGeometry() +*/ + +void QWidget::fontChange( const QFont & ) +{ + update(); + updateGeometry(); +} + + +/*! + \fn QFontMetrics QWidget::fontMetrics() const + + Returns the font metrics for the widget's current font. + Equivalent to QFontMetrics(widget->font()). + + \sa font(), fontInfo(), setFont() +*/ + +/*! + \fn QFontInfo QWidget::fontInfo() const + + Returns the font info for the widget's current font. + Equivalent to QFontInto(widget->font()). + + \sa font(), fontMetrics(), setFont() +*/ + + +/*! + \property QWidget::cursor + \brief the cursor shape for this widget + + The mouse cursor will assume this shape when it's over this + widget. See the \link Qt::CursorShape list of predefined cursor + objects\endlink for a range of useful shapes. + + An editor widget might use an I-beam cursor: + \code + setCursor( IbeamCursor ); + \endcode + + If no cursor has been set, or after a call to unsetCursor(), the + parent's cursor is used. The function unsetCursor() has no effect + on top-level widgets. + + \sa QApplication::setOverrideCursor() +*/ + +#ifndef QT_NO_CURSOR +const QCursor &QWidget::cursor() const +{ + if ( testWState(WState_OwnCursor) ) + return (extra && extra->curs) + ? *extra->curs + : arrowCursor; + else + return (isTopLevel() || !parentWidget()) ? arrowCursor : parentWidget()->cursor(); +} +#endif +#ifndef QT_NO_WIDGET_TOPEXTRA +/*! + \property QWidget::caption + \brief the window caption (title) + + This property only makes sense for top-level widgets. If no + caption has been set, the caption is QString::null. + + \sa icon() iconText() +*/ +QString QWidget::caption() const +{ + return extra && extra->topextra + ? extra->topextra->caption + : QString::null; +} + +/*! + \property QWidget::icon + \brief the widget's icon + + This property only makes sense for top-level widgets. If no icon + has been set, icon() returns 0. + + \sa iconText, caption, + \link appicon.html Setting the Application Icon\endlink +*/ +const QPixmap *QWidget::icon() const +{ + return ( extra && extra->topextra ) ? extra->topextra->icon : 0; +} + +/*! + \property QWidget::iconText + \brief the widget's icon text + + This property only makes sense for top-level widgets. If no icon + text has been set, this functions returns QString::null. + + \sa icon, caption +*/ + +QString QWidget::iconText() const +{ + return ( extra && extra->topextra ) ? extra->topextra->iconText + : QString::null; +} +#endif //QT_NO_WIDGET_TOPEXTRA + +/*! + \property QWidget::mouseTracking + \brief whether mouse tracking is enabled for the widget + + If mouse tracking is disabled (the default), the widget only + receives mouse move events when at least one mouse button is + pressed while the mouse is being moved. + + If mouse tracking is enabled, the widget receives mouse move + events even if no buttons are pressed. + + \sa mouseMoveEvent(), QApplication::setGlobalMouseTracking() +*/ + + +/*! + Sets the widget's focus proxy to widget \a w. If \a w is 0, the + function resets this widget to have no focus proxy. + + Some widgets, such as QComboBox, can "have focus", but create a + child widget to actually handle the focus. QComboBox, for example, + creates a QLineEdit which handles the focus. + + setFocusProxy() sets the widget which will actually get focus when + "this widget" gets it. If there is a focus proxy, focusPolicy(), + setFocusPolicy(), setFocus() and hasFocus() all operate on the + focus proxy. + + \sa focusProxy() +*/ + +void QWidget::setFocusProxy( QWidget * w ) +{ + if ( !w && !extra ) + return; + + for ( QWidget* fp = w; fp; fp = fp->focusProxy() ) { + if ( fp == this ) { +#if defined (QT_CHECK_STATE) + qWarning( "%s (%s): already in focus proxy chain", className(), name() ); +#endif + return; + } + } + + createExtra(); + + if ( extra->focus_proxy ) { + disconnect( extra->focus_proxy, SIGNAL(destroyed()), + this, SLOT(focusProxyDestroyed()) ); + extra->focus_proxy = 0; + } + + if ( w ) { + setFocusPolicy( w->focusPolicy() ); + connect( w, SIGNAL(destroyed()), + this, SLOT(focusProxyDestroyed()) ); + } + extra->focus_proxy = w; +} + + +/*! + Returns the focus proxy, or 0 if there is no focus proxy. + + \sa setFocusProxy() +*/ + +QWidget * QWidget::focusProxy() const +{ + return extra ? extra->focus_proxy : 0; +} + + +/*! + \internal + + Internal slot used to clean up if the focus proxy is destroyed. + + \sa setFocusProxy() +*/ + +void QWidget::focusProxyDestroyed() +{ + if ( extra ) + extra->focus_proxy = 0; + setFocusPolicy( NoFocus ); +} + +/*! + \property QWidget::focus + \brief whether this widget (or its focus proxy) has the keyboard + input focus + + Effectively equivalent to \c {qApp->focusWidget() == this}. + + \sa setFocus(), clearFocus(), setFocusPolicy(), QApplication::focusWidget() +*/ +bool QWidget::hasFocus() const +{ + const QWidget* w = this; + while ( w->focusProxy() ) + w = w->focusProxy(); + return qApp->focusWidget() == w; +} + +/*! + Gives the keyboard input focus to this widget (or its focus + proxy) if this widget or one of its parents is the \link + isActiveWindow() active window\endlink. + + First, a focus out event is sent to the focus widget (if any) to + tell it that it is about to lose the focus. Then a focus in event + is sent to this widget to tell it that it just received the focus. + (Nothing happens if the focus in and focus out widgets are the + same.) + + setFocus() gives focus to a widget regardless of its focus policy, + but does not clear any keyboard grab (see grabKeyboard()). + + Be aware that if the widget is hidden, it will not accept focus. + + \warning If you call setFocus() in a function which may itself be + called from focusOutEvent() or focusInEvent(), you may get an + infinite recursion. + + \sa hasFocus() clearFocus() focusInEvent() focusOutEvent() + setFocusPolicy() QApplication::focusWidget() grabKeyboard() + grabMouse() +*/ + +void QWidget::setFocus() +{ + if ( !isEnabled() ) + return; + + if ( focusProxy() ) { + focusProxy()->setFocus(); + return; + } + + QFocusData * f = focusData( TRUE ); + if ( f->it.current() == this && qApp->focusWidget() == this +#if defined(Q_WS_WIN) + && GetFocus() == winId() +#endif + ) + return; + + f->it.toFirst(); + while ( f->it.current() != this && !f->it.atLast() ) + ++f->it; + // at this point, the iterator should point to 'this'. if it + // does not, 'this' must not be in the list - an error, but + // perhaps possible. fix it. + if ( f->it.current() != this ) { + f->focusWidgets.append( this ); + f->it.toLast(); + } + + if ( isActiveWindow() ) { + QWidget * prev = qApp->focus_widget; + if ( prev ) { + // This part is never executed when Q_WS_X11? Preceding XFocusOut + // had already reset focus_widget when received XFocusIn + + // Don't reset input context explicitly here. Whether reset or not + // when focusing out is a responsibility of input methods. For + // example, Japanese input context should not be reset here. The + // context sometimes contains a whole paragraph and has minutes of + // lifetime different to ephemeral one in other languages. The + // input context should be survived until focused again. So we + // delegate the responsibility to input context via + // unfocusInputContext(). + if ( prev != this && prev->isInputMethodEnabled() ) { +#if 0 + prev->resetInputContext(); +#else + prev->unfocusInputContext(); +#endif + } + } +#if defined(Q_WS_WIN) + else { + QInputContext::endComposition(); + } +#endif + qApp->focus_widget = this; + if( isInputMethodEnabled() ) + focusInputContext(); + +#if defined(Q_WS_WIN) + if ( !topLevelWidget()->isPopup() ) + SetFocus( winId() ); + else { +#endif +#if defined(QT_ACCESSIBILITY_SUPPORT) + QAccessible::updateAccessibility( this, 0, QAccessible::Focus ); +#endif +#if defined(Q_WS_WIN) + } +#endif + + if ( prev != this ) { + if ( prev ) { + QFocusEvent out( QEvent::FocusOut ); + QApplication::sendEvent( prev, &out ); + } + + if ( qApp->focus_widget == this ) { + QFocusEvent in( QEvent::FocusIn ); + QApplication::sendEvent( this, &in ); + } + } + } +} + +/*! + Takes keyboard input focus from the widget. + + If the widget has active focus, a \link focusOutEvent() focus out + event\endlink is sent to this widget to tell it that it is about + to lose the focus. + + This widget must enable focus setting in order to get the keyboard + input focus, i.e. it must call setFocusPolicy(). + + \sa hasFocus(), setFocus(), focusInEvent(), focusOutEvent(), + setFocusPolicy(), QApplication::focusWidget() +*/ + +void QWidget::clearFocus() +{ + if ( focusProxy() ) { + focusProxy()->clearFocus(); + return; + } else if ( hasFocus() ) { +#if !defined(Q_WS_X11) + resetInputContext(); +#else + unfocusInputContext(); +#endif + QWidget* w = qApp->focusWidget(); + // clear active focus + qApp->focus_widget = 0; + QFocusEvent out( QEvent::FocusOut ); + QApplication::sendEvent( w, &out ); +#if defined(Q_WS_WIN) + if ( !isPopup() && GetFocus() == winId() ) + SetFocus( 0 ); + else { +#endif +#if defined(QT_ACCESSIBILITY_SUPPORT) + QAccessible::updateAccessibility( this, 0, QAccessible::Focus ); +#endif +#if defined(Q_WS_WIN) + } +#endif + } +} + + +/*! + Finds a new widget to give the keyboard focus to, as appropriate + for Tab and Shift+Tab, and returns TRUE if is can find a new + widget and FALSE if it can't, + + If \a next is TRUE, this function searches "forwards", if \a next + is FALSE, it searches "backwards". + + Sometimes, you will want to reimplement this function. For + example, a web browser might reimplement it to move its "current + active link" forwards or backwards, and call + QWidget::focusNextPrevChild() only when it reaches the last or + first link on the "page". + + Child widgets call focusNextPrevChild() on their parent widgets, + but only the top-level widget decides where to redirect focus. By + overriding this method for an object, you thus gain control of + focus traversal for all child widgets. + + \warning QScrollView uses it own logic for this function, which + does the right thing in most cases. But if you are using a + QScrollView and want complete control of the focus chain you'll + need to override QScrollView::focusNextPrevChild() and your + top-level widgets' focusNextPrevChild() functions. + + \sa focusData() +*/ + +bool QWidget::focusNextPrevChild( bool next ) +{ + QWidget* p = parentWidget(); + if ( !isTopLevel() && p ) + return p->focusNextPrevChild(next); + + QFocusData *f = focusData( TRUE ); + + QWidget *startingPoint = f->it.current(); + QWidget *candidate = 0; + QWidget *w = next ? f->focusWidgets.last() : f->focusWidgets.first(); + extern bool qt_tab_all_widgets; + uint focus_flag = qt_tab_all_widgets ? TabFocus : StrongFocus; + do { + if ( w && w != startingPoint && + ( ( w->focusPolicy() & focus_flag ) == focus_flag ) + && !w->focusProxy() && w->isVisibleTo(this) && w->isEnabled()) + candidate = w; + w = next ? f->focusWidgets.prev() : f->focusWidgets.next(); + } while( w && !(candidate && w==startingPoint) ); + + if ( !candidate ) + return FALSE; + + candidate->setFocus(); + return TRUE; +} + +/*! + Returns the focus widget in this widget's window. This is not the + same as QApplication::focusWidget(), which returns the focus + widget in the currently active window. +*/ + +QWidget *QWidget::focusWidget() const +{ + QWidget *that = (QWidget *)this; // mutable + QFocusData *f = that->focusData( FALSE ); + if ( f && f->focusWidgets.count() && f->it.current() == 0 ) + f->it.toFirst(); + return ( f && f->it.current() ) ? f->it.current() : 0; +} + + +/*! + Returns the focus data for this widget's top-level widget. + + Focus data always belongs to the top-level widget. The focus data + list contains all the widgets in this top-level widget that can + accept focus, in tab order. An iterator points to the current + focus widget (focusWidget() returns a pointer to this widget). + + This information is useful for implementing advanced versions of + focusNextPrevChild(). +*/ +QFocusData * QWidget::focusData() +{ + return focusData( TRUE ); +} + +/*! + \internal + + Internal function which lets us ask for the focus data, creating + it if it doesn't exist and \a create is TRUE. +*/ +QFocusData * QWidget::focusData( bool create ) +{ + QWidget * tlw = topLevelWidget(); + QWExtra * ed = tlw->extraData(); + if ( !ed || !ed->topextra ) { + if ( !create ) + return 0; + tlw->createTLExtra(); + ed = tlw->extraData(); + } + if ( create && !ed->topextra->focusData ) + ed->topextra->focusData = new QFocusData; + + return ed->topextra->focusData; +} + +/*! + \property QWidget::inputMethodEnabled + \brief enables or disables the use of input methods for this widget. + + Most Widgets (as eg. buttons) that do not handle text input should have + the input method disabled if they have focus. This is the default. + + If a widget handles text input it should set this property to TRUE. +*/ + +void QWidget::setInputMethodEnabled( bool b ) +{ + im_enabled = b; +#ifdef Q_WS_WIN + QInputContext::enable( this, im_enabled & !((bool)testWState(WState_Disabled)) ); +#endif +} + + +/*! + Enables key event compression, if \a compress is TRUE, and + disables it if \a compress is FALSE. + + Key compression is off by default (except for QLineEdit and + QTextEdit), so widgets receive one key press event for each key + press (or more, since autorepeat is usually on). If you turn it on + and your program doesn't keep up with key input, Qt may try to + compress key events so that more than one character can be + processed in each event. + + For example, a word processor widget might receive 2, 3 or more + characters in each QKeyEvent::text(), if the layout recalculation + takes too long for the CPU. + + If a widget supports multiple character unicode input, it is + always safe to turn the compression on. + + Qt performs key event compression only for printable characters. + Modifier keys, cursor movement keys, function keys and + miscellaneous action keys (e.g. Escape, Enter, Backspace, + PrintScreen) will stop key event compression, even if there are + more compressible key events available. + + Not all platforms support this compression, in which case turning + it on will have no effect. + + \sa QKeyEvent::text(); +*/ + +void QWidget::setKeyCompression(bool compress) +{ + if ( compress ) + setWState( WState_CompressKeys ); + else + clearWState( WState_CompressKeys ); +} + +/*! + \property QWidget::isActiveWindow + \brief whether this widget is the active window + + The active window is the window that contains the widget + that has keyboard focus. + + When popup windows are visible, this property is TRUE for both the + active window \e and for the popup. + + \sa setActiveWindow(), QApplication::activeWindow() +*/ +bool QWidget::isActiveWindow() const +{ + QWidget *tlw = topLevelWidget(); + if(testWFlags(WSubWindow) && parentWidget()) + tlw = parentWidget()->topLevelWidget(); + if(tlw == qApp->activeWindow() || ( isVisible() && tlw->isPopup() )) + return TRUE; +#ifndef QT_NO_STYLE + if(style().styleHint(QStyle::SH_Widget_ShareActivation, this )) { + if((tlw->isDialog() || (tlw->testWFlags(Qt::WStyle_Tool) && !tlw->isPopup())) && + !tlw->testWFlags(Qt::WShowModal) && + (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow())) + return TRUE; + QWidget *w = qApp->activeWindow(); + if( !testWFlags(WSubWindow) && w && w->testWFlags(WSubWindow) && + w->parentWidget()->topLevelWidget() == tlw) + return TRUE; + while(w && (tlw->isDialog() || tlw->testWFlags(Qt::WStyle_Tool)) && + !w->testWFlags(Qt::WShowModal) && w->parentWidget()) { + w = w->parentWidget()->topLevelWidget(); + if( w == tlw ) + return TRUE; + } + } +#endif +#if defined(Q_WS_WIN32) + HWND parent = tlw->winId(); + HWND topparent = GetActiveWindow(); + while ( parent ) { + parent = ::GetParent( parent ); + if ( parent && parent == topparent ) + return TRUE; + } +#endif + + return FALSE; +} + +/*! + Moves the \a second widget around the ring of focus widgets so + that keyboard focus moves from the \a first widget to the \a + second widget when the Tab key is pressed. + + Note that since the tab order of the \a second widget is changed, + you should order a chain like this: + + \code + setTabOrder( a, b ); // a to b + setTabOrder( b, c ); // a to b to c + setTabOrder( c, d ); // a to b to c to d + \endcode + + \e not like this: + + \code + setTabOrder( c, d ); // c to d WRONG + setTabOrder( a, b ); // a to b AND c to d + setTabOrder( b, c ); // a to b to c, but not c to d + \endcode + + If \a first or \a second has a focus proxy, setTabOrder() + correctly substitutes the proxy. + + \sa setFocusPolicy(), setFocusProxy() +*/ +void QWidget::setTabOrder( QWidget* first, QWidget *second ) +{ + if ( !first || !second || + first->focusPolicy() == NoFocus || second->focusPolicy() == NoFocus ) + return; + + // If first is redirected, set first to the last child of first + // that can take keyboard focus so that second is inserted after + // that last child, and the focus order within first is (more + // likely to be) preserved. + if ( first->focusProxy() ) { + QObjectList *l = first->queryList( "QWidget" ); + if ( l && l->count() ) { + QObjectListIt it(*l); + it.toLast(); + while (it.current()) { + if (((QWidget*)it.current())->topLevelWidget() == first->topLevelWidget()) { + first = (QWidget*)it.current(); + if (first->focusPolicy() != NoFocus) + break; + } + --it; + } + } + delete l; + } + while ( first->focusProxy() ) + first = first->focusProxy(); + while ( second->focusProxy() ) + second = second->focusProxy(); + + QFocusData *f = first->focusData( TRUE ); + bool focusThere = (f->it.current() == second ); + f->focusWidgets.removeRef( second ); + if ( f->focusWidgets.findRef( first ) >= 0 ) + f->focusWidgets.insert( f->focusWidgets.at() + 1, second ); + else + f->focusWidgets.append( second ); + if ( focusThere ) { // reset iterator so tab will work appropriately + f->it.toFirst(); + while( f->it.current() && f->it.current() != second ) + ++f->it; + } +} + +/*!\internal + + Moves the relevant subwidgets of this widget from the \a oldtlw's + tab chain to that of the new parent, if there's anything to move and + we're really moving + + This function is called from QWidget::reparent() *after* the widget + has been reparented. + + \sa reparent() +*/ + +void QWidget::reparentFocusWidgets( QWidget * oldtlw ) +{ + if ( oldtlw == topLevelWidget() ) + return; // nothing to do + + QFocusData * from = oldtlw ? oldtlw->topData()->focusData : 0; + QFocusData * to; + to = focusData(); + + if ( from ) { + from->focusWidgets.first(); + do { + QWidget * pw = from->focusWidgets.current(); + while( pw && pw != this ) + pw = pw->parentWidget(); + if ( pw == this ) { + QWidget * w = from->focusWidgets.take(); + if ( w == from->it.current() ) + // probably best to clear keyboard focus, or + // the user might become rather confused + w->clearFocus(); + if ( !isTopLevel() ) + to->focusWidgets.append( w ); + } else { + from->focusWidgets.next(); + } + } while( from->focusWidgets.current() ); + } + + if ( to->focusWidgets.findRef(this) < 0 ) + to->focusWidgets.append( this ); + + if ( !isTopLevel() && extra && extra->topextra && extra->topextra->focusData ) { + // this widget is no longer a top-level widget, so get rid + // of old focus data + delete extra->topextra->focusData; + extra->topextra->focusData = 0; + } +} + +/*! + \fn void QWidget::recreate( QWidget *parent, WFlags f, const QPoint & p, bool showIt ) + + \obsolete + + This method is provided to aid porting from Qt 1.0 to 2.0. It has + been renamed reparent() in Qt 2.0. +*/ + +/*! + \property QWidget::frameSize + \brief the size of the widget including any window frame +*/ +QSize QWidget::frameSize() const +{ + if ( isTopLevel() && !isPopup() ) { + if ( fstrut_dirty ) + updateFrameStrut(); + QWidget *that = (QWidget *) this; + QTLWExtra *top = that->topData(); + return QSize( crect.width() + top->fleft + top->fright, + crect.height() + top->ftop + top->fbottom ); + } + return crect.size(); +} + +/*! + \internal + + Recursive function that updates \a widget and all its children, + if they have some parent background origin. +*/ +static void qt_update_bg_recursive( QWidget *widget ) +{ + if ( !widget || widget->isHidden() || widget->backgroundOrigin() == QWidget::WidgetOrigin || !widget->backgroundPixmap() ) + return; + + const QObjectList *lst = widget->children(); + + if ( lst ) { + QObjectListIterator it( *lst ); + QWidget *widget; + while ( (widget = (QWidget*)it.current()) ) { + ++it; + if ( widget->isWidgetType() && !widget->isHidden() && !widget->isTopLevel() && !widget->testWFlags(Qt::WSubWindow) ) + qt_update_bg_recursive( widget ); + } + } + QApplication::postEvent( widget, new QPaintEvent( widget->clipRegion(), !widget->testWFlags(Qt::WRepaintNoErase) ) ); +} + +/*! + \overload + + This corresponds to move( QPoint(\a x, \a y) ). +*/ + +void QWidget::move( int x, int y ) +{ + QPoint oldp(pos()); + internalSetGeometry( x + geometry().x() - QWidget::x(), + y + geometry().y() - QWidget::y(), + width(), height(), TRUE ); + if ( isVisible() && oldp != pos() ) + qt_update_bg_recursive( this ); +} + +/*! + \overload + + This corresponds to resize( QSize(\a w, \a h) ). +*/ +void QWidget::resize( int w, int h ) +{ + internalSetGeometry( geometry().x(), geometry().y(), w, h, FALSE ); + setWState( WState_Resized ); +} + +/*! + \overload + + This corresponds to setGeometry( QRect(\a x, \a y, \a w, \a h) ). +*/ +void QWidget::setGeometry( int x, int y, int w, int h ) +{ + QPoint oldp( pos( )); + internalSetGeometry( x, y, w, h, TRUE ); + setWState( WState_Resized ); + if ( isVisible() && oldp != pos() ) + qt_update_bg_recursive( this ); +} + +/*! + \property QWidget::focusEnabled + \brief whether the widget accepts keyboard focus + + Keyboard focus is initially disabled (i.e. focusPolicy() == + \c QWidget::NoFocus). + + You must enable keyboard focus for a widget if it processes + keyboard events. This is normally done from the widget's + constructor. For instance, the QLineEdit constructor calls + setFocusPolicy(QWidget::StrongFocus). + + \sa setFocusPolicy(), focusInEvent(), focusOutEvent(), keyPressEvent(), + keyReleaseEvent(), isEnabled() +*/ + +/*! + \enum QWidget::FocusPolicy + + This enum type defines the various policies a widget can have with + respect to acquiring keyboard focus. + + \value TabFocus the widget accepts focus by tabbing. + \value ClickFocus the widget accepts focus by clicking. + \value StrongFocus the widget accepts focus by both tabbing + and clicking. On Mac OS X this will also + be indicate that the widget accepts tab focus + when in 'Text/List focus mode'. + \value WheelFocus like StrongFocus plus the widget accepts + focus by using the mouse wheel. + \value NoFocus the widget does not accept focus. + +*/ + +/*! + \property QWidget::focusPolicy + \brief the way the widget accepts keyboard focus + + The policy is \c QWidget::TabFocus if the widget accepts keyboard + focus by tabbing, \c QWidget::ClickFocus if the widget accepts + focus by clicking, \c QWidget::StrongFocus if it accepts both, and + \c QWidget::NoFocus (the default) if it does not accept focus at + all. + + You must enable keyboard focus for a widget if it processes + keyboard events. This is normally done from the widget's + constructor. For instance, the QLineEdit constructor calls + setFocusPolicy(QWidget::StrongFocus). + + \sa focusEnabled, focusInEvent(), focusOutEvent(), keyPressEvent(), + keyReleaseEvent(), enabled +*/ + +void QWidget::setFocusPolicy( FocusPolicy policy ) +{ + if ( focusProxy() ) + focusProxy()->setFocusPolicy( policy ); + if ( policy != NoFocus ) { + QFocusData * f = focusData( TRUE ); + if ( f->focusWidgets.findRef( this ) < 0 ) + f->focusWidgets.append( this ); + } + focus_policy = (uint) policy; +} + +/*! + \property QWidget::updatesEnabled + \brief whether updates are enabled + + Calling update() and repaint() has no effect if updates are + disabled. Paint events from the window system are processed + normally even if updates are disabled. + + setUpdatesEnabled() is normally used to disable updates for a + short period of time, for instance to avoid screen flicker during + large changes. + + Example: + \code + setUpdatesEnabled( FALSE ); + bigVisualChanges(); + setUpdatesEnabled( TRUE ); + repaint(); + \endcode + + \sa update(), repaint(), paintEvent() +*/ +void QWidget::setUpdatesEnabled( bool enable ) +{ + if ( enable ) + clearWState( WState_BlockUpdates ); + else + setWState( WState_BlockUpdates ); +} + +/*! + Shows the widget and its child widgets. + + If its size or position has changed, Qt guarantees that a widget + gets move and resize events just before it is shown. + + You almost never have to reimplement this function. If you need to + change some settings before a widget is shown, use showEvent() + instead. If you need to do some delayed initialization use + polish(). + + \sa showEvent(), hide(), showMinimized(), showMaximized(), + showNormal(), isVisible(), polish() +*/ + +void QWidget::show() +{ + if ( testWState(WState_Visible) ) + return; + + bool wasHidden = isHidden(); + bool postLayoutHint = !isTopLevel() && wasHidden; + clearWState( WState_ForceHide | WState_CreatedHidden ); + + if ( !isTopLevel() && !parentWidget()->isVisible() ) { + // we should become visible, but one of our ancestors is + // explicitly hidden. Since we cleared the ForceHide flag, our + // immediate parent will call show() on us again during its + // own processing of show(). + if ( wasHidden ) { + QEvent showToParentEvent( QEvent::ShowToParent ); + QApplication::sendEvent( this, &showToParentEvent ); + } + if ( postLayoutHint ) + QApplication::postEvent( parentWidget(), + new QEvent(QEvent::LayoutHint) ); + return; + } + + in_show = TRUE; // set qws recursion watch + + QApplication::sendPostedEvents( this, QEvent::ChildInserted ); + + uint state = isTopLevel() ? windowState() : 0; +#ifndef Q_OS_TEMP + if ( isTopLevel() && !testWState( WState_Resized ) ) { + // do this before sending the posted resize events. Otherwise + // the layout would catch the resize event and may expand the + // minimum size. + QSize s = qt_naturalWidgetSize( this ); + if ( !s.isEmpty() ) + resize( s ); + } +#endif // Q_OS_TEMP + + QApplication::sendPostedEvents( this, QEvent::Move ); + QApplication::sendPostedEvents( this, QEvent::Resize ); + + // the resizing and layouting might have changed the window state + if (isTopLevel() && windowState() != state) + setWindowState(state); + + setWState( WState_Visible ); + + if ( parentWidget() ) + QApplication::sendPostedEvents( parentWidget(), + QEvent::ChildInserted ); + + if ( extra ) { + int w = crect.width(); + int h = crect.height(); + if ( w < extra->minw || h < extra->minh || + w > extra->maxw || h > extra->maxh ) { + w = QMAX( extra->minw, QMIN( w, extra->maxw )); + h = QMAX( extra->minh, QMIN( h, extra->maxh )); + resize( w, h ); // deferred resize + } + } + + if ( testWFlags(WStyle_Tool) || isPopup() ) { + raise(); + } else if ( testWFlags(WType_TopLevel) ) { + while ( QApplication::activePopupWidget() ) { + if ( !QApplication::activePopupWidget()->close() ) + break; + } + } + + if ( !testWState(WState_Polished) ) + polish(); + + showChildren( FALSE ); + + if ( postLayoutHint ) + QApplication::postEvent( parentWidget(), + new QEvent(QEvent::LayoutHint) ); + + // Required for Mac, not sure whether we should always do that + if( isTopLevel() ) + QApplication::sendPostedEvents(0, QEvent::LayoutHint); + + // On Windows, show the popup now so that our own focus handling + // stores the correct old focus widget even if it's stolen in the showevent +#if defined(Q_WS_WIN) + if ( testWFlags(WType_Popup) ) + qApp->openPopup( this ); +#endif + + QShowEvent showEvent; + QApplication::sendEvent( this, &showEvent ); + + if ( testWFlags(WShowModal) ) { + // qt_enter_modal *before* show, otherwise the initial + // stacking might be wrong + qt_enter_modal( this ); + } + + // do not show the window directly, but post a show-window request + // to reduce flicker with widgets in layouts + if ( postLayoutHint ) + QApplication::postEvent( this, new QEvent( QEvent::ShowWindowRequest ) ); + else + showWindow(); + +#if !defined(Q_WS_WIN) + if ( testWFlags(WType_Popup) ) + qApp->openPopup( this ); +#endif + +#if defined(QT_ACCESSIBILITY_SUPPORT) + QAccessible::updateAccessibility( this, 0, QAccessible::ObjectShow ); +#endif + + in_show = FALSE; // reset qws recursion watch +} + +/*! \fn void QWidget::iconify() + \obsolete +*/ + +/*! + Hides the widget. + + You almost never have to reimplement this function. If you need to + do something after a widget is hidden, use hideEvent() instead. + + \sa hideEvent(), isHidden(), show(), showMinimized(), isVisible(), close() +*/ + +void QWidget::hide() +{ + clearWState( WState_CreatedHidden ); + if ( testWState(WState_ForceHide) ) + return; + + setWState( WState_ForceHide ); + + if ( testWFlags(WType_Popup) ) + qApp->closePopup( this ); + + // Move test modal here. Otherwise, a modal dialog could get + // destroyed and we lose all access to its parent because we haven't + // left modality. (Eg. modal Progress Dialog) + if ( testWFlags(WShowModal) ) + qt_leave_modal( this ); + +#if defined(Q_WS_WIN) + if ( isTopLevel() && !isPopup() && parentWidget() && isActiveWindow() ) + parentWidget()->setActiveWindow(); // Activate parent +#endif + + hideWindow(); + + if ( testWState(WState_Visible) ) { + clearWState( WState_Visible ); + + // next bit tries to move the focus if the focus widget is now + // hidden. + if ( qApp && qApp->focusWidget() == this ) + focusNextPrevChild( TRUE ); + QHideEvent hideEvent; + QApplication::sendEvent( this, &hideEvent ); + hideChildren( FALSE ); + +#if defined(QT_ACCESSIBILITY_SUPPORT) + QAccessible::updateAccessibility( this, 0, QAccessible::ObjectHide ); +#endif + } else { + QEvent hideToParentEvent( QEvent::HideToParent ); + QApplication::sendEvent( this, &hideToParentEvent ); + } + + // post layout hint for non toplevels. The parent widget check is + // necessary since the function is called in the destructor + if ( !isTopLevel() && parentWidget() ) + QApplication::postEvent( parentWidget(), + new QEvent( QEvent::LayoutHint) ); +} + +void QWidget::setShown( bool show ) +{ + if ( show ) + this->show(); + else + hide(); +} + +void QWidget::setHidden( bool hide ) +{ + if ( hide ) + this->hide(); + else + show(); +} + +void QWidget::showChildren( bool spontaneous ) +{ + if ( children() ) { + QObjectListIt it(*children()); + register QObject *object; + QWidget *widget; + while ( it ) { + object = it.current(); + ++it; + if ( object->isWidgetType() ) { + widget = (QWidget*)object; + if ( !widget->isTopLevel() && widget->isShown() ) { + if ( spontaneous ) { + widget->showChildren( spontaneous ); + QShowEvent e; + QApplication::sendSpontaneousEvent( widget, &e ); + } else { + widget->show(); + } + } + } + } + } +} + +void QWidget::hideChildren( bool spontaneous ) +{ + if ( children() ) { + QObjectListIt it(*children()); + register QObject *object; + QWidget *widget; + while ( it ) { + object = it.current(); + ++it; + if ( object->isWidgetType() ) { + widget = (QWidget*)object; + if ( !widget->isTopLevel() && widget->isShown() ) { + if ( !spontaneous ) + widget->clearWState( WState_Visible ); + widget->hideChildren( spontaneous ); + QHideEvent e; + if ( spontaneous ) + QApplication::sendSpontaneousEvent( widget, &e ); + else + QApplication::sendEvent( widget, &e ); + } + } + } + } +} + + +/*! + Delayed initialization of a widget. + + This function will be called \e after a widget has been fully + created and \e before it is shown the very first time. + + Polishing is useful for final initialization which depends on + having an instantiated widget. This is something a constructor + cannot guarantee since the initialization of the subclasses might + not be finished. + + After this function, the widget has a proper font and palette and + QApplication::polish() has been called. + + Remember to call QWidget's implementation first when reimplementing this + function to ensure that your program does not end up in infinite recursion. + + \sa constPolish(), QApplication::polish() +*/ + +void QWidget::polish() +{ +#ifndef QT_NO_WIDGET_TOPEXTRA + if ( isTopLevel() ) { + const QPixmap *pm = icon(); + if ( !pm || pm->isNull() ) { + QWidget *mw = (QWidget *)parent(); + pm = mw ? mw->icon() : 0; + if ( pm && !pm->isNull() ) + setIcon( *pm ); + else { + mw = mw ? mw->topLevelWidget() : 0; + pm = mw ? mw->icon() : 0; + if ( pm && !pm->isNull() ) + setIcon( *pm ); + else { + mw = qApp ? qApp->mainWidget() : 0; + pm = mw ? mw->icon() : 0; + if ( pm && !pm->isNull() ) + setIcon( *pm ); + } + } + } + } +#endif + if ( !testWState(WState_Polished) ) { + if ( ! own_font && + ! QApplication::font( this ).isCopyOf( QApplication::font() ) ) + unsetFont(); +#ifndef QT_NO_PALETTE + if ( ! own_palette && + ! QApplication::palette( this ).isCopyOf( QApplication::palette() ) ) + unsetPalette(); +#endif + setWState(WState_Polished); + qApp->polish( this ); + QApplication::sendPostedEvents( this, QEvent::ChildInserted ); + } +} + + +/*! + \fn void QWidget::constPolish() const + + Ensures that the widget is properly initialized by calling + polish(). + + Call constPolish() from functions like sizeHint() that depends on + the widget being initialized, and that may be called before + show(). + + \warning Do not call constPolish() on a widget from inside that + widget's constructor. + + \sa polish() +*/ + +/*! + \overload + + Closes this widget. Returns TRUE if the widget was closed; + otherwise returns FALSE. + + If \a alsoDelete is TRUE or the widget has the \c + WDestructiveClose widget flag, the widget is also deleted. The + widget can prevent itself from being closed by rejecting the + \l QCloseEvent it gets. A close events is delivered to the widget + no matter if the widget is visible or not. + + The QApplication::lastWindowClosed() signal is emitted when the + last visible top level widget is closed. + + Note that closing the \l QApplication::mainWidget() terminates the + application. + + \sa closeEvent(), QCloseEvent, hide(), QApplication::quit(), + QApplication::setMainWidget(), QApplication::lastWindowClosed() +*/ + +bool QWidget::close( bool alsoDelete ) +{ + if ( is_closing ) + return TRUE; + is_closing = 1; + WId id = winId(); + bool isMain = qApp->mainWidget() == this; + bool checkLastWindowClosed = isTopLevel() && !isPopup(); + bool deleted = FALSE; + QCloseEvent e; + QApplication::sendEvent( this, &e ); + deleted = !QWidget::find(id); + if ( !deleted && !e.isAccepted() ) { + is_closing = 0; + return FALSE; + } + if ( !deleted && !isHidden() ) + hide(); + if ( checkLastWindowClosed + && qApp->receivers(SIGNAL(lastWindowClosed())) ) { + /* if there is no non-withdrawn top level window left (except + the desktop, popups, or dialogs with parents), we emit the + lastWindowClosed signal */ + QWidgetList *list = qApp->topLevelWidgets(); + QWidget *widget = list->first(); + while ( widget ) { + if ( !widget->isHidden() + && !widget->isDesktop() + && !widget->isPopup() + && (!widget->isDialog() || !widget->parentWidget())) + break; + widget = list->next(); + } + delete list; + if ( widget == 0 ) + emit qApp->lastWindowClosed(); + } + if ( isMain ) + qApp->quit(); + if ( deleted ) + return TRUE; + is_closing = 0; + if ( alsoDelete ) + delete this; + else if ( testWFlags(WDestructiveClose) ) { + clearWFlags(WDestructiveClose); + deleteLater(); + } + return TRUE; +} + + +/*! + \fn bool QWidget::close() + + Closes this widget. Returns TRUE if the widget was closed; + otherwise returns FALSE. + + First it sends the widget a QCloseEvent. The widget is \link + hide() hidden\endlink if it \link QCloseEvent::accept() + accepts\endlink the close event. The default implementation of + QWidget::closeEvent() accepts the close event. + + The \l QApplication::lastWindowClosed() signal is emitted when the + last visible top level widget is closed. + +*/ + +/*! + \property QWidget::visible + \brief whether the widget is visible + + Calling show() sets the widget to visible status if all its parent + widgets up to the top-level widget are visible. If an ancestor is + not visible, the widget won't become visible until all its + ancestors are shown. + + Calling hide() hides a widget explicitly. An explicitly hidden + widget will never become visible, even if all its ancestors become + visible, unless you show it. + + A widget receives show and hide events when its visibility status + changes. Between a hide and a show event, there is no need to + waste CPU cycles preparing or displaying information to the user. + A video application, for example, might simply stop generating new + frames. + + A widget that happens to be obscured by other windows on the + screen is considered to be visible. The same applies to iconified + top-level widgets and windows that exist on another virtual + desktop (on platforms that support this concept). A widget + receives spontaneous show and hide events when its mapping status + is changed by the window system, e.g. a spontaneous hide event + when the user minimizes the window, and a spontaneous show event + when the window is restored again. + + \sa show(), hide(), isHidden(), isVisibleTo(), isMinimized(), + showEvent(), hideEvent() +*/ + + +/*! + Returns TRUE if this widget would become visible if \a ancestor is + shown; otherwise returns FALSE. + + The TRUE case occurs if neither the widget itself nor any parent + up to but excluding \a ancestor has been explicitly hidden. + + This function will still return TRUE if the widget is obscured by + other windows on the screen, but could be physically visible if it + or they were to be moved. + + isVisibleTo(0) is identical to isVisible(). + + \sa show() hide() isVisible() +*/ + +bool QWidget::isVisibleTo(QWidget* ancestor) const +{ + if ( !ancestor ) + return isVisible(); + const QWidget * w = this; + while ( w + && w->isShown() + && !w->isTopLevel() + && w->parentWidget() + && w->parentWidget() != ancestor ) + w = w->parentWidget(); + return w->isShown(); +} + + +/*! + \fn bool QWidget::isVisibleToTLW() const + \obsolete + + This function is deprecated. It is equivalent to isVisible() +*/ + +/*! + \property QWidget::hidden + \brief whether the widget is explicitly hidden + + If FALSE, the widget is visible or would become visible if all its + ancestors became visible. + + \sa hide(), show(), isVisible(), isVisibleTo(), shown +*/ + +/*! + \property QWidget::shown + \brief whether the widget is shown + + If TRUE, the widget is visible or would become visible if all its + ancestors became visible. + + \sa hide(), show(), isVisible(), isVisibleTo(), hidden +*/ + +/*! + \property QWidget::visibleRect + \brief the visible rectangle + + \obsolete + + No longer necessary, you can simply call repaint(). If you do not + need the rectangle for repaint(), use clipRegion() instead. +*/ +QRect QWidget::visibleRect() const +{ + QRect r = rect(); + const QWidget * w = this; + int ox = 0; + int oy = 0; + while ( w + && w->isVisible() + && !w->isTopLevel() + && w->parentWidget() ) { + ox -= w->x(); + oy -= w->y(); + w = w->parentWidget(); + r = r.intersect( QRect( ox, oy, w->width(), w->height() ) ); + } + if ( !w->isVisible() ) + return QRect(); + return r; +} + +/*! + Returns the unobscured region where paint events can occur. + + For visible widgets, this is an approximation of the area not + covered by other widgets; otherwise, this is an empty region. + + The repaint() function calls this function if necessary, so in + general you do not need to call it. + +*/ +QRegion QWidget::clipRegion() const +{ + return visibleRect(); +} + + +/*! + Adjusts the size of the widget to fit the contents. + + Uses sizeHint() if valid (i.e if the size hint's width and height + are \>= 0), otherwise sets the size to the children rectangle (the + union of all child widget geometries). + + \sa sizeHint(), childrenRect() +*/ + +void QWidget::adjustSize() +{ + QApplication::sendPostedEvents( 0, QEvent::ChildInserted ); + QApplication::sendPostedEvents( 0, QEvent::LayoutHint ); + if ( !testWState(WState_Polished) ) + polish(); + QSize s = sizeHint(); + + if ( isTopLevel() ) { + +#if defined(Q_WS_X11) + QRect screen = QApplication::desktop()->screenGeometry( x11Screen() ); +#else // all others + QRect screen = QApplication::desktop()->screenGeometry( pos() ); +#endif + +#ifndef QT_NO_LAYOUT + if ( layout() ) { + if ( layout()->hasHeightForWidth() ) { + s = s.boundedTo( screen.size() ); + s.setHeight( layout()->totalHeightForWidth( s.width() ) ); + } + } else +#endif + { + if ( sizePolicy().hasHeightForWidth() ) { + s = s.boundedTo( screen.size() ); + s.setHeight( heightForWidth( s.width() ) ); + } + } + } + if ( s.isValid() ) { + resize( s ); + return; + } + QRect r = childrenRect(); // get children rectangle + if ( r.isNull() ) // probably no widgets + return; + resize( r.width() + 2 * r.x(), r.height() + 2 * r.y() ); +} + + +/*! + \property QWidget::sizeHint + \brief the recommended size for the widget + + If the value of this property is an invalid size, no size is + recommended. + + The default implementation of sizeHint() returns an invalid size + if there is no layout for this widget, and returns the layout's + preferred size otherwise. + + \sa QSize::isValid(), minimumSizeHint(), sizePolicy(), + setMinimumSize(), updateGeometry() +*/ + +QSize QWidget::sizeHint() const +{ +#ifndef QT_NO_LAYOUT + if ( layout() ) + return layout()->totalSizeHint(); +#endif + return QSize( -1, -1 ); +} + +/*! + \property QWidget::minimumSizeHint + \brief the recommended minimum size for the widget + + If the value of this property is an invalid size, no minimum size + is recommended. + + The default implementation of minimumSizeHint() returns an invalid + size if there is no layout for this widget, and returns the + layout's minimum size otherwise. Most built-in widgets reimplement + minimumSizeHint(). + + \l QLayout will never resize a widget to a size smaller than + minimumSizeHint. + + \sa QSize::isValid(), resize(), setMinimumSize(), sizePolicy() +*/ +QSize QWidget::minimumSizeHint() const +{ +#ifndef QT_NO_LAYOUT + if ( layout() ) + return layout()->totalMinimumSize(); +#endif + return QSize( -1, -1 ); +} + + +/*! + \fn QWidget *QWidget::parentWidget( bool sameWindow ) const + + Returns the parent of this widget, or 0 if it does not have any + parent widget. If \a sameWindow is TRUE and the widget is top + level returns 0; otherwise returns the widget's parent. +*/ + +/*! + \fn WFlags QWidget::testWFlags( WFlags f ) const + + Returns the bitwise AND of the widget flags and \a f. + + Widget flags are a combination of \l{Qt::WidgetFlags}. + + If you want to test for the presence of multiple flags (or + composite flags such as \c WStyle_Splash), test the + return value for equality against the argument. For example: + + \code + int flags = WStyle_Tool | WStyle_NoBorder; + if ( testWFlags(flags) ) + ... // WStyle_Tool or WStyle_NoBorder or both are set + if ( testWFlags(flags) == flags ) + ... // both WStyle_Tool and WStyle_NoBorder are set + \endcode + + \sa getWFlags(), setWFlags(), clearWFlags() +*/ + +/*! + \fn WState QWidget::testWState( WState s ) const + \internal + + Returns the bitwise AND of the widget states and \a s. +*/ + +/*! + \fn uint QWidget::getWState() const + + \internal + + Returns the current widget state. +*/ +/*! + \fn void QWidget::clearWState( uint n ) + + \internal + + Clears the widgets states \a n. +*/ +/*! + \fn void QWidget::setWState( uint n ) + + \internal + + Sets the widgets states \a n. +*/ + + + +/***************************************************************************** + QWidget event handling + *****************************************************************************/ + +/*! + This is the main event handler; it handles event \a e. You can + reimplement this function in a subclass, but we recommend using + one of the specialized event handlers instead. + + The main event handler first passes an event through all \link + QObject::installEventFilter() event filters\endlink that have been + installed. If none of the filters intercept the event, it calls + one of the specialized event handlers. + + Key press and release events are treated differently from other + events. event() checks for Tab and Shift+Tab and tries to move the + focus appropriately. If there is no widget to move the focus to + (or the key press is not Tab or Shift+Tab), event() calls + keyPressEvent(). + + This function returns TRUE if it is able to pass the event over to + someone (i.e. someone wanted the event); otherwise returns FALSE. + + \sa closeEvent(), focusInEvent(), focusOutEvent(), enterEvent(), + keyPressEvent(), keyReleaseEvent(), leaveEvent(), + mouseDoubleClickEvent(), mouseMoveEvent(), mousePressEvent(), + mouseReleaseEvent(), moveEvent(), paintEvent(), resizeEvent(), + QObject::event(), QObject::timerEvent() +*/ + +bool QWidget::event( QEvent *e ) +{ + if ( QObject::event( e ) ) + return TRUE; + + switch ( e->type() ) { + case QEvent::MouseMove: + mouseMoveEvent( (QMouseEvent*)e ); + if ( ! ((QMouseEvent*)e)->isAccepted() ) + return FALSE; + break; + + case QEvent::MouseButtonPress: + // Don't reset input context here. Whether reset or not is + // a responsibility of input method. reset() will be + // called by mouseHandler() of input method if necessary + // via mousePressEvent() of text widgets. +#if 0 + resetInputContext(); +#endif + mousePressEvent( (QMouseEvent*)e ); + if ( ! ((QMouseEvent*)e)->isAccepted() ) + return FALSE; + break; + + case QEvent::MouseButtonRelease: + mouseReleaseEvent( (QMouseEvent*)e ); + if ( ! ((QMouseEvent*)e)->isAccepted() ) + return FALSE; + break; + + case QEvent::MouseButtonDblClick: + mouseDoubleClickEvent( (QMouseEvent*)e ); + if ( ! ((QMouseEvent*)e)->isAccepted() ) + return FALSE; + break; +#ifndef QT_NO_WHEELEVENT + case QEvent::Wheel: + wheelEvent( (QWheelEvent*)e ); + if ( ! ((QWheelEvent*)e)->isAccepted() ) + return FALSE; + break; +#endif + case QEvent::TabletMove: + case QEvent::TabletPress: + case QEvent::TabletRelease: + tabletEvent( (QTabletEvent*)e ); + if ( ! ((QTabletEvent*)e)->isAccepted() ) + return FALSE; + break; + case QEvent::Accel: + ((QKeyEvent*)e)->ignore(); + return FALSE; + case QEvent::KeyPress: { + QKeyEvent *k = (QKeyEvent *)e; + bool res = FALSE; + if ( !(k->state() & ControlButton || k->state() & AltButton) ) { + if ( k->key() == Key_Backtab || + (k->key() == Key_Tab && + (k->state() & ShiftButton)) ) { + QFocusEvent::setReason( QFocusEvent::Backtab ); + res = focusNextPrevChild( FALSE ); + QFocusEvent::resetReason(); + + } else if ( k->key() == Key_Tab ) { + QFocusEvent::setReason( QFocusEvent::Tab ); + res = focusNextPrevChild( TRUE ); + QFocusEvent::resetReason(); + } + if ( res ) + break; + } + keyPressEvent( k ); + if ( !k->isAccepted() ) + return FALSE; + } + break; + + case QEvent::KeyRelease: + keyReleaseEvent( (QKeyEvent*)e ); + if ( ! ((QKeyEvent*)e)->isAccepted() ) + return FALSE; + break; + + case QEvent::IMStart: { + QIMEvent *i = (QIMEvent *) e; + imStartEvent(i); + if (! i->isAccepted()) + return FALSE; + } + break; + + case QEvent::IMCompose: { + QIMEvent *i = (QIMEvent *) e; + imComposeEvent(i); + if (! i->isAccepted()) + return FALSE; + } + break; + + case QEvent::IMEnd: { + QIMEvent *i = (QIMEvent *) e; + imEndEvent(i); + if (! i->isAccepted()) + return FALSE; + } + break; + + case QEvent::FocusIn: + focusInEvent( (QFocusEvent*)e ); + setFontSys(); + break; + + case QEvent::FocusOut: + focusOutEvent( (QFocusEvent*)e ); + break; + + case QEvent::Enter: + enterEvent( e ); + break; + + case QEvent::Leave: + leaveEvent( e ); + break; + + case QEvent::Paint: + // At this point the event has to be delivered, regardless + // whether the widget isVisible() or not because it + // already went through the filters + paintEvent( (QPaintEvent*)e ); + break; + + case QEvent::Move: + moveEvent( (QMoveEvent*)e ); + break; + + case QEvent::Resize: + resizeEvent( (QResizeEvent*)e ); + break; + + case QEvent::Close: { + QCloseEvent *c = (QCloseEvent *)e; + closeEvent( c ); + if ( !c->isAccepted() ) + return FALSE; + } + break; + + case QEvent::ContextMenu: { + QContextMenuEvent *c = (QContextMenuEvent *)e; + contextMenuEvent( c ); + if ( !c->isAccepted() ) + return FALSE; + } + break; + +#ifndef QT_NO_DRAGANDDROP + case QEvent::Drop: + dropEvent( (QDropEvent*) e); + break; + + case QEvent::DragEnter: + dragEnterEvent( (QDragEnterEvent*) e); + break; + + case QEvent::DragMove: + dragMoveEvent( (QDragMoveEvent*) e); + break; + + case QEvent::DragLeave: + dragLeaveEvent( (QDragLeaveEvent*) e); + break; +#endif + + case QEvent::Show: + showEvent( (QShowEvent*) e); + break; + + case QEvent::Hide: + hideEvent( (QHideEvent*) e); + break; + + case QEvent::ShowWindowRequest: + if ( isShown() ) + showWindow(); + break; + + case QEvent::ParentFontChange: + if ( isTopLevel() ) + break; + // fall through + case QEvent::ApplicationFontChange: + if ( own_font ) + setFont( fnt.resolve( qt_naturalWidgetFont( this ) ) ); + else + unsetFont(); + break; + +#ifndef QT_NO_PALETTE + case QEvent::ParentPaletteChange: + if ( isTopLevel() ) + break; + // fall through + case QEvent::ApplicationPaletteChange: + if ( !own_palette && !isDesktop() ) + unsetPalette(); +# if defined(Q_WS_QWS) && !defined (QT_NO_QWS_MANAGER) + if ( isTopLevel() && topData()->qwsManager ) { + QRegion r( topData()->qwsManager->region() ); + QApplication::postEvent(topData()->qwsManager, new QPaintEvent(r, FALSE) ); + } +# endif + break; +#endif + + case QEvent::WindowActivate: + case QEvent::WindowDeactivate: + windowActivationChange( e->type() != QEvent::WindowActivate ); + if ( children() ) { + QObjectListIt it( *children() ); + QObject *o; + while( ( o = it.current() ) != 0 ) { + ++it; + if ( o->isWidgetType() && + ((QWidget*)o)->isVisible() && + !((QWidget*)o)->isTopLevel() ) + QApplication::sendEvent( o, e ); + } + } + break; + + case QEvent::LanguageChange: + case QEvent::LocaleChange: + if ( children() ) { + QObjectListIt it( *children() ); + QObject *o; + while( ( o = it.current() ) != 0 ) { + ++it; + QApplication::sendEvent( o, e ); + } + } + if ( e->type() == QEvent::LanguageChange ) { + int index = metaObject()->findSlot( "languageChange()", TRUE ); + if ( index >= 0 ) + qt_invoke( index, 0 ); + } + update(); + break; +#ifndef QT_NO_LAYOUT + case QEvent::LayoutDirectionChange: + if ( layout() ) { + layout()->activate(); + } else { + QObjectList* llist = queryList( "QLayout", 0, TRUE, TRUE ); + QObjectListIt lit( *llist ); + QLayout *lay; + while ( ( lay = (QLayout*)lit.current() ) != 0 ) { + ++lit; + lay->activate(); + } + delete llist; + } + update(); + break; +#endif + + case QEvent::WindowStateChange: + { + QEvent::Type type; + if (isMinimized()) + type = QEvent::ShowMinimized; + else if (isFullScreen()) + type = QEvent::ShowFullScreen; + else if (isMaximized()) + type = QEvent::ShowMaximized; + else + type = QEvent::ShowNormal; + + QApplication::postEvent(this, new QEvent(type)); + break; + } + + case QEvent::WindowBlocked: + case QEvent::WindowUnblocked: + if ( children() ) { + QObjectListIt it( *children() ); + QObject *o; + while( ( o = it.current() ) != 0 ) { + ++it; + QWidget *w = ::qt_cast<QWidget*>(o); + if (w && !w->testWFlags(Qt::WShowModal)) + QApplication::sendEvent( o, e ); + } + } + break; + + default: + return FALSE; + } + return TRUE; +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive mouse move events for the widget. + + If mouse tracking is switched off, mouse move events only occur if + a mouse button is pressed while the mouse is being moved. If mouse + tracking is switched on, mouse move events occur even if no mouse + button is pressed. + + QMouseEvent::pos() reports the position of the mouse cursor, + relative to this widget. For press and release events, the + position is usually the same as the position of the last mouse + move event, but it might be different if the user's hand shakes. + This is a feature of the underlying window system, not Qt. + + \sa setMouseTracking(), mousePressEvent(), mouseReleaseEvent(), + mouseDoubleClickEvent(), event(), QMouseEvent +*/ + +void QWidget::mouseMoveEvent( QMouseEvent * e) +{ + e->ignore(); +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive mouse press events for the widget. + + If you create new widgets in the mousePressEvent() the + mouseReleaseEvent() may not end up where you expect, depending on + the underlying window system (or X11 window manager), the widgets' + location and maybe more. + + The default implementation implements the closing of popup widgets + when you click outside the window. For other widget types it does + nothing. + + \sa mouseReleaseEvent(), mouseDoubleClickEvent(), + mouseMoveEvent(), event(), QMouseEvent +*/ + +void QWidget::mousePressEvent( QMouseEvent *e ) +{ + e->ignore(); + if ( isPopup() ) { + e->accept(); + QWidget* w; + while ( (w = qApp->activePopupWidget() ) && w != this ){ + w->close(); + if (qApp->activePopupWidget() == w) // widget does not want to dissappear + w->hide(); // hide at least + } + if (!rect().contains(e->pos()) ){ + close(); + } + } +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive mouse release events for the widget. + + \sa mouseReleaseEvent(), mouseDoubleClickEvent(), + mouseMoveEvent(), event(), QMouseEvent +*/ + +void QWidget::mouseReleaseEvent( QMouseEvent * e ) +{ + e->ignore(); +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive mouse double click events for the widget. + + The default implementation generates a normal mouse press event. + + Note that the widgets gets a mousePressEvent() and a + mouseReleaseEvent() before the mouseDoubleClickEvent(). + + \sa mousePressEvent(), mouseReleaseEvent() mouseMoveEvent(), + event(), QMouseEvent +*/ + +void QWidget::mouseDoubleClickEvent( QMouseEvent *e ) +{ + mousePressEvent( e ); // try mouse press event +} + +#ifndef QT_NO_WHEELEVENT +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive wheel events for the widget. + + If you reimplement this handler, it is very important that you + \link QWheelEvent ignore()\endlink the event if you do not handle + it, so that the widget's parent can interpret it. + + The default implementation ignores the event. + + \sa QWheelEvent::ignore(), QWheelEvent::accept(), event(), + QWheelEvent +*/ + +void QWidget::wheelEvent( QWheelEvent *e ) +{ + e->ignore(); +} +#endif + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive tablet events for the widget. + + If you reimplement this handler, it is very important that you + \link QTabletEvent ignore()\endlink the event if you do not handle + it, so that the widget's parent can interpret it. + + The default implementation ignores the event. + + \sa QTabletEvent::ignore(), QTabletEvent::accept(), event(), + QTabletEvent +*/ + +void QWidget::tabletEvent( QTabletEvent *e ) +{ + e->ignore(); +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive key press events for the widget. + + A widget must call setFocusPolicy() to accept focus initially and + have focus in order to receive a key press event. + + If you reimplement this handler, it is very important that you + explicitly \link QKeyEvent::ignore() ignore\endlink the event + if you do not understand it, so that the widget's parent can + interpret it; otherwise, the event will be implicitly accepted. + Although top-level widgets are able to choose whether to accept + or ignore unknown events because they have no parent widgets that + could otherwise handle them, it is good practice to explicitly + ignore events to make widgets as reusable as possible. + + The default implementation closes popup widgets if the user + presses <b>Esc</b>. Otherwise the event is ignored. + + \sa keyReleaseEvent(), QKeyEvent::ignore(), setFocusPolicy(), + focusInEvent(), focusOutEvent(), event(), QKeyEvent +*/ + +void QWidget::keyPressEvent( QKeyEvent *e ) +{ + if ( isPopup() && e->key() == Key_Escape ) { + e->accept(); + close(); + } else { + e->ignore(); + } +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive key release events for the widget. + + A widget must \link setFocusPolicy() accept focus\endlink + initially and \link hasFocus() have focus\endlink in order to + receive a key release event. + + If you reimplement this handler, it is very important that you + \link QKeyEvent ignore()\endlink the release if you do not + understand it, so that the widget's parent can interpret it. + + The default implementation ignores the event. + + \sa keyPressEvent(), QKeyEvent::ignore(), setFocusPolicy(), + focusInEvent(), focusOutEvent(), event(), QKeyEvent +*/ + +void QWidget::keyReleaseEvent( QKeyEvent *e ) +{ + e->ignore(); +} + +/*! + This event handler can be reimplemented in a subclass to receive + keyboard focus events (focus received) for the widget. + + A widget normally must setFocusPolicy() to something other than + \c NoFocus in order to receive focus events. (Note that the + application programmer can call setFocus() on any widget, even + those that do not normally accept focus.) + + The default implementation updates the widget (except for toplevel + widgets that do not specify a focusPolicy() ). It also calls + setMicroFocusHint(), hinting any system-specific input tools about + the focus of the user's attention. + + \sa focusOutEvent(), setFocusPolicy(), keyPressEvent(), + keyReleaseEvent(), event(), QFocusEvent +*/ + +void QWidget::focusInEvent( QFocusEvent * ) +{ + if ( focusPolicy() != NoFocus || !isTopLevel() ) { + update(); + if ( testWState(WState_AutoMask) ) + updateMask(); + setMicroFocusHint(width()/2, 0, 1, height(), FALSE); + } +} + +/*! + This event handler can be reimplemented in a subclass to receive + keyboard focus events (focus lost) for the widget. + + A widget normally must setFocusPolicy() to something other than + \c NoFocus in order to receive focus events. (Note that the + application programmer can call setFocus() on any widget, even + those that do not normally accept focus.) + + The default implementation updates the widget (except for toplevel + widgets that do not specify a focusPolicy() ). It also calls + setMicroFocusHint(), hinting any system-specific input tools about + the focus of the user's attention. + + \sa focusInEvent(), setFocusPolicy(), keyPressEvent(), + keyReleaseEvent(), event(), QFocusEvent +*/ + +void QWidget::focusOutEvent( QFocusEvent * ) +{ + if ( focusPolicy() != NoFocus || !isTopLevel() ){ + update(); + if ( testWState(WState_AutoMask) ) + updateMask(); + } +} + +/*! + \property QWidget::microFocusHint + \brief the currently set micro focus hint for this widget. + + See the documentation of setMicroFocusHint() for more information. +*/ +QRect QWidget::microFocusHint() const +{ + if ( !extra || extra->micro_focus_hint.isEmpty() ) + return QRect(width()/2, 0, 1, height() ); + else + return extra->micro_focus_hint; +} + +/*! + This event handler can be reimplemented in a subclass to receive + widget enter events. + + An event is sent to the widget when the mouse cursor enters the + widget. + + \sa leaveEvent(), mouseMoveEvent(), event() +*/ + +void QWidget::enterEvent( QEvent * ) +{ +} + +/*! + This event handler can be reimplemented in a subclass to receive + widget leave events. + + A leave event is sent to the widget when the mouse cursor leaves + the widget. + + \sa enterEvent(), mouseMoveEvent(), event() +*/ + +void QWidget::leaveEvent( QEvent * ) +{ +} + +/*! + This event handler can be reimplemented in a subclass to receive + paint events. + + A paint event is a request to repaint all or part of the widget. + It can happen as a result of repaint() or update(), or because the + widget was obscured and has now been uncovered, or for many other + reasons. + + Many widgets can simply repaint their entire surface when asked + to, but some slow widgets need to optimize by painting only the + requested region: QPaintEvent::region(). This speed optimization + does not change the result, as painting is clipped to that region + during event processing. QListView and QCanvas do this, for + example. + + Qt also tries to speed up painting by merging multiple paint + events into one. When update() is called several times or the + window system sends several paint events, Qt merges these events + into one event with a larger region (see QRegion::unite()). + repaint() does not permit this optimization, so we suggest using + update() when possible. + + When the paint event occurs, the update region has normally been + erased, so that you're painting on the widget's background. There + are a couple of exceptions and QPaintEvent::erased() tells you + whether the widget has been erased or not. + + The background can be set using setBackgroundMode(), + setPaletteBackgroundColor() or setBackgroundPixmap(). The + documentation for setBackgroundMode() elaborates on the + background; we recommend reading it. + + \sa event(), repaint(), update(), QPainter, QPixmap, QPaintEvent +*/ + +void QWidget::paintEvent( QPaintEvent * ) +{ +} + + +/*! + This event handler can be reimplemented in a subclass to receive + widget move events. When the widget receives this event, it is + already at the new position. + + The old position is accessible through QMoveEvent::oldPos(). + + \sa resizeEvent(), event(), move(), QMoveEvent +*/ + +void QWidget::moveEvent( QMoveEvent * ) +{ +} + + +/*! + This event handler can be reimplemented in a subclass to receive + widget resize events. When resizeEvent() is called, the widget + already has its new geometry. The old size is accessible through + QResizeEvent::oldSize(). + + The widget will be erased and receive a paint event immediately + after processing the resize event. No drawing need be (or should + be) done inside this handler. + + Widgets that have been created with the \c WNoAutoErase flag + will not be erased. Nevertheless, they will receive a paint event + for their entire area afterwards. Again, no drawing needs to be + done inside this handler. + + The default implementation calls updateMask() if the widget has + \link QWidget::setAutoMask() automatic masking\endlink enabled. + + \sa moveEvent(), event(), resize(), QResizeEvent, paintEvent() +*/ + +void QWidget::resizeEvent( QResizeEvent * ) +{ + if ( testWState(WState_AutoMask) ) + updateMask(); +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive widget close events. + + The default implementation calls e->accept(), which hides this + widget. See the \l QCloseEvent documentation for more details. + + \sa event(), hide(), close(), QCloseEvent +*/ + +void QWidget::closeEvent( QCloseEvent *e ) +{ + e->accept(); +} + + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive widget context menu events. + + The default implementation calls e->ignore(), which rejects the + context event. See the \l QContextMenuEvent documentation for + more details. + + \sa event(), QContextMenuEvent +*/ + +void QWidget::contextMenuEvent( QContextMenuEvent *e ) +{ + e->ignore(); +} + + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive Input Method composition events. This handler + is called when the user begins entering text using an Input Method. + + The default implementation calls e->ignore(), which rejects the + Input Method event. See the \l QIMEvent documentation for more + details. + + \sa event(), QIMEvent +*/ +void QWidget::imStartEvent( QIMEvent *e ) +{ + e->ignore(); +} + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive Input Method composition events. This handler + is called when the user has entered some text using an Input Method. + + The default implementation calls e->ignore(), which rejects the + Input Method event. See the \l QIMEvent documentation for more + details. + + \sa event(), QIMEvent +*/ +void QWidget::imComposeEvent( QIMEvent *e ) +{ + e->ignore(); +} + + +/*! + This event handler, for event \a e, can be reimplemented in a + subclass to receive Input Method composition events. This handler + is called when the user has finished inputting text via an Input + Method. + + The default implementation calls e->ignore(), which rejects the + Input Method event. See the \l QIMEvent documentation for more + details. + + \sa event(), QIMEvent +*/ +void QWidget::imEndEvent( QIMEvent *e ) +{ + e->ignore(); +} + + +#ifndef QT_NO_DRAGANDDROP + +/*! + This event handler is called when a drag is in progress and the + mouse enters this widget. + + See the \link dnd.html Drag-and-drop documentation\endlink for an + overview of how to provide drag-and-drop in your application. + + \sa QTextDrag, QImageDrag, QDragEnterEvent +*/ +void QWidget::dragEnterEvent( QDragEnterEvent * ) +{ +} + +/*! + This event handler is called when a drag is in progress and the + mouse enters this widget, and whenever it moves within the widget. + + See the \link dnd.html Drag-and-drop documentation\endlink for an + overview of how to provide drag-and-drop in your application. + + \sa QTextDrag, QImageDrag, QDragMoveEvent +*/ +void QWidget::dragMoveEvent( QDragMoveEvent * ) +{ +} + +/*! + This event handler is called when a drag is in progress and the + mouse leaves this widget. + + See the \link dnd.html Drag-and-drop documentation\endlink for an + overview of how to provide drag-and-drop in your application. + + \sa QTextDrag, QImageDrag, QDragLeaveEvent +*/ +void QWidget::dragLeaveEvent( QDragLeaveEvent * ) +{ +} + +/*! + This event handler is called when the drag is dropped on this + widget. + + See the \link dnd.html Drag-and-drop documentation\endlink for an + overview of how to provide drag-and-drop in your application. + + \sa QTextDrag, QImageDrag, QDropEvent +*/ +void QWidget::dropEvent( QDropEvent * ) +{ +} + +#endif // QT_NO_DRAGANDDROP + +/*! + This event handler can be reimplemented in a subclass to receive + widget show events. + + Non-spontaneous show events are sent to widgets immediately before + they are shown. The spontaneous show events of top-level widgets + are delivered afterwards. + + \sa event(), QShowEvent +*/ +void QWidget::showEvent( QShowEvent * ) +{ +} + +/*! + This event handler can be reimplemented in a subclass to receive + widget hide events. + + Hide events are sent to widgets immediately after they have been + hidden. + + \sa event(), QHideEvent +*/ +void QWidget::hideEvent( QHideEvent * ) +{ +} + +/* + \fn QWidget::x11Event( MSG * ) + + This special event handler can be reimplemented in a subclass to + receive native X11 events. + + In your reimplementation of this function, if you want to stop the + event being handled by Qt, return TRUE. If you return FALSE, this + native event is passed back to Qt, which translates the event into + a Qt event and sends it to the widget. + + \warning This function is not portable. + + \sa QApplication::x11EventFilter() +*/ + + +#if defined(Q_WS_MAC) + +/*! + This special event handler can be reimplemented in a subclass to + receive native Macintosh events. + + In your reimplementation of this function, if you want to stop the + event being handled by Qt, return TRUE. If you return FALSE, this + native event is passed back to Qt, which translates the event into + a Qt event and sends it to the widget. + + \warning This function is not portable. + + \sa QApplication::macEventFilter() +*/ + +bool QWidget::macEvent( MSG * ) +{ + return FALSE; +} + +#endif +#if defined(Q_WS_WIN) + +/*! + This special event handler can be reimplemented in a subclass to + receive native Windows events. + + In your reimplementation of this function, if you want to stop the + event being handled by Qt, return TRUE. If you return FALSE, this + native event is passed back to Qt, which translates the event into + a Qt event and sends it to the widget. + + \warning This function is not portable. + + \sa QApplication::winEventFilter() +*/ +bool QWidget::winEvent( MSG * ) +{ + return FALSE; +} + +#endif +#if defined(Q_WS_X11) + +/*! + This special event handler can be reimplemented in a subclass to + receive native X11 events. + + In your reimplementation of this function, if you want to stop the + event being handled by Qt, return TRUE. If you return FALSE, this + native event is passed back to Qt, which translates the event into + a Qt event and sends it to the widget. + + \warning This function is not portable. + + \sa QApplication::x11EventFilter() +*/ +bool QWidget::x11Event( XEvent * ) +{ + return FALSE; +} + +#endif +#if defined(Q_WS_QWS) + +/*! + This special event handler can be reimplemented in a subclass to + receive native Qt/Embedded events. + + In your reimplementation of this function, if you want to stop the + event being handled by Qt, return TRUE. If you return FALSE, this + native event is passed back to Qt, which translates the event into + a Qt event and sends it to the widget. + + \warning This function is not portable. + + \sa QApplication::qwsEventFilter() +*/ +bool QWidget::qwsEvent( QWSEvent * ) +{ + return FALSE; +} + +#endif + +/*! + \property QWidget::autoMask + \brief whether the auto mask feature is enabled for the widget + + Transparent widgets use a mask to define their visible region. + QWidget has some built-in support to make the task of + recalculating the mask easier. When setting auto mask to TRUE, + updateMask() will be called whenever the widget is resized or + changes its focus state. Note that you must reimplement + updateMask() (which should include a call to setMask()) or nothing + will happen. + + Note: when you re-implement resizeEvent(), focusInEvent() or + focusOutEvent() in your custom widgets and still want to ensure + that the auto mask calculation works, you should add: + + \code + if ( autoMask() ) + updateMask(); + \endcode + + at the end of your event handlers. This is true for all member + functions that change the appearance of the widget in a way that + requires a recalculation of the mask. + + While being a technically appealing concept, masks have a big + drawback: when using complex masks that cannot be expressed easily + with relatively simple regions, they can be very slow on some + window systems. The classic example is a transparent label. The + complex shape of its contents makes it necessary to represent its + mask by a bitmap, which consumes both memory and time. If all you + want is to blend the background of several neighboring widgets + together seamlessly, you will probably want to use + setBackgroundOrigin() rather than a mask. + + \sa autoMask() updateMask() setMask() clearMask() setBackgroundOrigin() +*/ + +bool QWidget::autoMask() const +{ + return testWState(WState_AutoMask); +} + +void QWidget::setAutoMask( bool enable ) +{ + if ( enable == autoMask() ) + return; + + if ( enable ) { + setWState(WState_AutoMask); + updateMask(); + } else { + clearWState(WState_AutoMask); + clearMask(); + } +} + +/*! + \enum QWidget::BackgroundOrigin + + This enum defines the origin used to draw a widget's background + pixmap. + + The pixmap is drawn using the: + \value WidgetOrigin widget's coordinate system. + \value ParentOrigin parent's coordinate system. + \value WindowOrigin top-level window's coordinate system. + \value AncestorOrigin same origin as the parent uses. +*/ + +/*! + \property QWidget::backgroundOrigin + \brief the origin of the widget's background + + The origin is either WidgetOrigin (the default), ParentOrigin, + WindowOrigin or AncestorOrigin. + + This only makes a difference if the widget has a background + pixmap, in which case positioning matters. Using \c WindowOrigin + for several neighboring widgets makes the background blend + together seamlessly. \c AncestorOrigin allows blending backgrounds + seamlessly when an ancestor of the widget has an origin other than + \c WindowOrigin. + + \sa backgroundPixmap(), setBackgroundMode() +*/ +QWidget::BackgroundOrigin QWidget::backgroundOrigin() const +{ + return extra ? (BackgroundOrigin)extra->bg_origin : WidgetOrigin; +} + +void QWidget::setBackgroundOrigin( BackgroundOrigin origin ) +{ + if ( origin == backgroundOrigin() ) + return; + createExtra(); + extra->bg_origin = origin; + update(); +} + +/*! + This function can be reimplemented in a subclass to support + transparent widgets. It should be called whenever a widget changes + state in a way that means that the shape mask must be recalculated. + + \sa setAutoMask(), updateMask(), setMask(), clearMask() +*/ +void QWidget::updateMask() +{ +} + +/*! + \internal + Returns the offset of the widget from the backgroundOrigin. + + \sa setBackgroundMode(), backgroundMode(), +*/ +QPoint QWidget::backgroundOffset() const +{ + if (!isTopLevel()) { + switch(backgroundOrigin()) { + case WidgetOrigin: + break; + case ParentOrigin: + return pos(); + case WindowOrigin: + { + const QWidget *topl = this; + while(topl && !topl->isTopLevel() && !topl->testWFlags(Qt::WSubWindow)) + topl = topl->parentWidget(TRUE); + return mapTo((QWidget *)topl, QPoint(0, 0) ); + } + case AncestorOrigin: + { + const QWidget *topl = this; + bool ancestorIsWindowOrigin = FALSE; + while(topl && !topl->isTopLevel() && !topl->testWFlags(Qt::WSubWindow)) + { + if (!ancestorIsWindowOrigin) { + if (topl->backgroundOrigin() == QWidget::WidgetOrigin) + break; + if (topl->backgroundOrigin() == QWidget::ParentOrigin) + { + topl = topl->parentWidget(TRUE); + break; + } + if (topl->backgroundOrigin() == QWidget::WindowOrigin) + ancestorIsWindowOrigin = TRUE; + } + topl = topl->parentWidget(TRUE); + } + + return mapTo((QWidget *) topl, QPoint(0,0) ); + } + } + } + // fall back + return QPoint(0,0); +} + +/*! + \fn QLayout* QWidget::layout () const + + Returns the layout engine that manages the geometry of this + widget's children. + + If the widget does not have a layout, layout() returns 0. + + \sa sizePolicy() +*/ + + +/* Sets this widget to use layout \a l to manage the geometry of its + children. + + If the widget already had a layout, the old layout is + forgotten. (Note that it is not deleted.) + + \sa layout() QLayout sizePolicy() +*/ +#ifndef QT_NO_LAYOUT +void QWidget::setLayout( QLayout *l ) +{ + lay_out = l; +} +#endif + +/*! + \property QWidget::sizePolicy + \brief the default layout behavior of the widget + + If there is a QLayout that manages this widget's children, the + size policy specified by that layout is used. If there is no such + QLayout, the result of this function is used. + + The default policy is Preferred/Preferred, which means that the + widget can be freely resized, but prefers to be the size + sizeHint() returns. Button-like widgets set the size policy to + specify that they may stretch horizontally, but are fixed + vertically. The same applies to lineedit controls (such as + QLineEdit, QSpinBox or an editable QComboBox) and other + horizontally orientated widgets (such as QProgressBar). + QToolButton's are normally square, so they allow growth in both + directions. Widgets that support different directions (such as + QSlider, QScrollBar or QHeader) specify stretching in the + respective direction only. Widgets that can provide scrollbars + (usually subclasses of QScrollView) tend to specify that they can + use additional space, and that they can make do with less than + sizeHint(). + + \sa sizeHint() QLayout QSizePolicy updateGeometry() +*/ +QSizePolicy QWidget::sizePolicy() const +{ + return extra ? extra->size_policy + : QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); +} + +void QWidget::setSizePolicy( QSizePolicy policy ) +{ + setWState( WState_OwnSizePolicy ); + if ( policy == sizePolicy() ) + return; + createExtra(); + extra->size_policy = policy; + updateGeometry(); +} + +/*! + \overload void QWidget::setSizePolicy( QSizePolicy::SizeType hor, QSizePolicy::SizeType ver, bool hfw ) + + Sets the size policy of the widget to \a hor, \a ver and \a hfw + (height for width). + + \sa QSizePolicy::QSizePolicy() +*/ + +/*! + Returns the preferred height for this widget, given the width \a + w. The default implementation returns 0, indicating that the + preferred height does not depend on the width. + + \warning Does not look at the widget's layout. +*/ + +int QWidget::heightForWidth( int w ) const +{ + (void)w; + return 0; +} + +/*! + \property QWidget::customWhatsThis + \brief whether the widget wants to handle What's This help manually + + The default implementation of customWhatsThis() returns FALSE, + which means the widget will not receive any events in Whats This + mode. + + The widget may leave What's This mode by calling + QWhatsThis::leaveWhatsThisMode(), with or without actually + displaying any help text. + + You can also reimplement customWhatsThis() if your widget is a + "passive interactor" supposed to work under all circumstances. + Simply don't call QWhatsThis::leaveWhatsThisMode() in that case. + + \sa QWhatsThis::inWhatsThisMode() QWhatsThis::leaveWhatsThisMode() +*/ +bool QWidget::customWhatsThis() const +{ + return FALSE; +} + +/*! + Returns the visible child widget at pixel position \a (x, y) in + the widget's own coordinate system. + + If \a includeThis is TRUE, and there is no child visible at \a (x, + y), the widget itself is returned. +*/ +QWidget *QWidget::childAt( int x, int y, bool includeThis ) const +{ + if ( !rect().contains( x, y ) ) + return 0; + if ( children() ) { + QObjectListIt it( *children() ); + it.toLast(); + QWidget *w, *t; + while( (w=(QWidget *)it.current()) != 0 ) { + --it; + if ( w->isWidgetType() && !w->isTopLevel() && !w->isHidden() ) { + if ( ( t = w->childAt( x - w->x(), y - w->y(), TRUE ) ) ) + return t; + } + } + } + if ( includeThis ) + return (QWidget*)this; + return 0; +} + +/*! + \overload + + Returns the visible child widget at point \a p in the widget's own + coordinate system. + + If \a includeThis is TRUE, and there is no child visible at \a p, + the widget itself is returned. + +*/ +QWidget *QWidget::childAt( const QPoint & p, bool includeThis ) const +{ + return childAt( p.x(), p.y(), includeThis ); +} + + +/*! + Notifies the layout system that this widget has changed and may + need to change geometry. + + Call this function if the sizeHint() or sizePolicy() have changed. + + For explicitly hidden widgets, updateGeometry() is a no-op. The + layout system will be notified as soon as the widget is shown. +*/ + +void QWidget::updateGeometry() +{ + if ( !isTopLevel() && isShown() ) + QApplication::postEvent( parentWidget(), + new QEvent( QEvent::LayoutHint ) ); +} + + +/*! + Reparents the widget. The widget gets a new \a parent, new widget + flags (\a f, but as usual, use 0) at a new position in its new + parent (\a p). + + If \a showIt is TRUE, show() is called once the widget has been + reparented. + + If the new parent widget is in a different top-level widget, the + reparented widget and its children are appended to the end of the + \link setFocusPolicy() tab chain \endlink of the new parent + widget, in the same internal order as before. If one of the moved + widgets had keyboard focus, reparent() calls clearFocus() for that + widget. + + If the new parent widget is in the same top-level widget as the + old parent, reparent doesn't change the tab order or keyboard + focus. + + \warning It is extremely unlikely that you will ever need this + function. If you have a widget that changes its content + dynamically, it is far easier to use \l QWidgetStack or \l + QWizard. + + \sa getWFlags() +*/ + +void QWidget::reparent( QWidget *parent, WFlags f, const QPoint &p, + bool showIt ) +{ + reparentSys( parent, f, p, showIt ); + QEvent e( QEvent::Reparent ); + QApplication::sendEvent( this, &e ); + if (!own_font) + unsetFont(); + else + setFont( fnt.resolve( qt_naturalWidgetFont( this ) ) ); +#ifndef QT_NO_PALETTE + if (!own_palette) + unsetPalette(); +#endif +} + +/*! + \overload + + A convenience version of reparent that does not take widget flags + as argument. + + Calls reparent(\a parent, getWFlags() \& ~\l WType_Mask, \a p, \a + showIt). +*/ +void QWidget::reparent( QWidget *parent, const QPoint & p, + bool showIt ) +{ + reparent( parent, getWFlags() & ~WType_Mask, p, showIt ); +} + +/*! + \property QWidget::ownCursor + \brief whether the widget uses its own cursor + + If FALSE, the widget uses its parent widget's cursor. + + \sa cursor +*/ + +/*! + \property QWidget::ownFont + \brief whether the widget uses its own font + + If FALSE, the widget uses its parent widget's font. + + \sa font +*/ + +/*! + \property QWidget::ownPalette + \brief whether the widget uses its own palette + + If FALSE, the widget uses its parent widget's palette. + + \sa palette +*/ + + +void QWidget::repaint( bool erase ) +{ + repaint( visibleRect(), erase ); +} + + + + +/*!\obsolete Use paletteBackgroundColor() or eraseColor() instead. */ +const QColor & QWidget::backgroundColor() const { return eraseColor(); } +/*!\obsolete Use setPaletteBackgroundColor() or setEraseColor() instead. */ +void QWidget::setBackgroundColor( const QColor &c ) { setEraseColor( c ); } +/*!\obsolete Use paletteBackgroundPixmap() or erasePixmap() instead. */ +const QPixmap *QWidget::backgroundPixmap() const { return erasePixmap(); } +/*!\obsolete Use setPaletteBackgroundPixmap() or setErasePixmap() instead. */ +void QWidget::setBackgroundPixmap( const QPixmap &pm ) { setErasePixmap( pm ); } + + +// documentation in qdesktopwidget_win.cpp +void QDesktopWidget::insertChild( QObject *obj ) +{ + if ( obj->isWidgetType() ) + return; + QWidget::insertChild( obj ); +} + +/*! + \property QWidget::windowOpacity + + \brief The level of opacity for the window. + + The valid range of opacity is from 1.0 (completely opaque) to + 0.0 (completely transparent). + + By default the value of this property is 1.0. + + This feature is only present on Mac OS X and Windows 2000 and up. + + \warning Changing this property from opaque to transparent might issue a + paint event that needs to be processed before the window is displayed + correctly. This affects mainly the use of QPixmap::grabWindow(). Also note + that semi-transparent windows update and resize significantely slower than + opaque windows. +*/ diff --git a/src/kernel/qwidget.h b/src/kernel/qwidget.h new file mode 100644 index 0000000..c83acb5 --- /dev/null +++ b/src/kernel/qwidget.h @@ -0,0 +1,1074 @@ +/**************************************************************************** +** +** Definition of QWidget class +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWIDGET_H +#define QWIDGET_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qobject.h" +#include "qpaintdevice.h" +#include "qpalette.h" +#include "qfont.h" +#include "qfontmetrics.h" +#include "qfontinfo.h" +#include "qsizepolicy.h" +#endif // QT_H + +#if defined(Q_WS_X11) && !defined(QT_NO_IM) +class QInputContext; +#endif + +class QLayout; +struct QWExtra; +struct QTLWExtra; +class QFocusData; +class QCursor; +class QWSRegionManager; +class QStyle; + +class Q_EXPORT QWidget : public QObject, public QPaintDevice +{ + Q_OBJECT + Q_ENUMS( BackgroundMode FocusPolicy BackgroundOrigin ) + Q_PROPERTY( bool isTopLevel READ isTopLevel ) + Q_PROPERTY( bool isDialog READ isDialog ) + Q_PROPERTY( bool isModal READ isModal ) + Q_PROPERTY( bool isPopup READ isPopup ) + Q_PROPERTY( bool isDesktop READ isDesktop ) + Q_PROPERTY( bool enabled READ isEnabled WRITE setEnabled ) + Q_PROPERTY( QRect geometry READ geometry WRITE setGeometry ) + Q_PROPERTY( QRect frameGeometry READ frameGeometry ) + Q_PROPERTY( int x READ x ) + Q_PROPERTY( int y READ y ) + Q_PROPERTY( QPoint pos READ pos WRITE move DESIGNABLE false STORED false ) + Q_PROPERTY( QSize frameSize READ frameSize ) + Q_PROPERTY( QSize size READ size WRITE resize DESIGNABLE false STORED false ) + Q_PROPERTY( int width READ width ) + Q_PROPERTY( int height READ height ) + Q_PROPERTY( QRect rect READ rect ) + Q_PROPERTY( QRect childrenRect READ childrenRect ) + Q_PROPERTY( QRegion childrenRegion READ childrenRegion ) + Q_PROPERTY( QSizePolicy sizePolicy READ sizePolicy WRITE setSizePolicy ) + Q_PROPERTY( QSize minimumSize READ minimumSize WRITE setMinimumSize ) + Q_PROPERTY( QSize maximumSize READ maximumSize WRITE setMaximumSize ) + Q_PROPERTY( int minimumWidth READ minimumWidth WRITE setMinimumWidth STORED false DESIGNABLE false ) + Q_PROPERTY( int minimumHeight READ minimumHeight WRITE setMinimumHeight STORED false DESIGNABLE false ) + Q_PROPERTY( int maximumWidth READ maximumWidth WRITE setMaximumWidth STORED false DESIGNABLE false ) + Q_PROPERTY( int maximumHeight READ maximumHeight WRITE setMaximumHeight STORED false DESIGNABLE false ) + Q_PROPERTY( QSize sizeIncrement READ sizeIncrement WRITE setSizeIncrement ) + Q_PROPERTY( QSize baseSize READ baseSize WRITE setBaseSize ) + Q_PROPERTY( BackgroundMode backgroundMode READ backgroundMode WRITE setBackgroundMode DESIGNABLE false ) + Q_PROPERTY( QColor paletteForegroundColor READ paletteForegroundColor WRITE setPaletteForegroundColor RESET unsetPalette ) + Q_PROPERTY( QColor paletteBackgroundColor READ paletteBackgroundColor WRITE setPaletteBackgroundColor RESET unsetPalette ) + Q_PROPERTY( QPixmap paletteBackgroundPixmap READ paletteBackgroundPixmap WRITE setPaletteBackgroundPixmap RESET unsetPalette ) + Q_PROPERTY( QBrush backgroundBrush READ backgroundBrush ) + Q_PROPERTY( QColorGroup colorGroup READ colorGroup ) + Q_PROPERTY( QPalette palette READ palette WRITE setPalette RESET unsetPalette STORED ownPalette ) + Q_PROPERTY( BackgroundOrigin backgroundOrigin READ backgroundOrigin WRITE setBackgroundOrigin ) + Q_PROPERTY( bool ownPalette READ ownPalette ) + Q_PROPERTY( QFont font READ font WRITE setFont RESET unsetFont STORED ownFont ) + Q_PROPERTY( bool ownFont READ ownFont ) +#ifndef QT_NO_CURSOR + Q_PROPERTY( QCursor cursor READ cursor WRITE setCursor RESET unsetCursor STORED ownCursor ) + Q_PROPERTY( bool ownCursor READ ownCursor ) +#endif +#ifndef QT_NO_WIDGET_TOPEXTRA + Q_PROPERTY( QString caption READ caption WRITE setCaption ) + Q_PROPERTY( QPixmap icon READ icon WRITE setIcon ) + Q_PROPERTY( QString iconText READ iconText WRITE setIconText ) +#endif + Q_PROPERTY( bool mouseTracking READ hasMouseTracking WRITE setMouseTracking ) + Q_PROPERTY( bool underMouse READ hasMouse ) + Q_PROPERTY( bool isActiveWindow READ isActiveWindow ) + Q_PROPERTY( bool focusEnabled READ isFocusEnabled ) + Q_PROPERTY( FocusPolicy focusPolicy READ focusPolicy WRITE setFocusPolicy ) + Q_PROPERTY( bool focus READ hasFocus ) + Q_PROPERTY( bool updatesEnabled READ isUpdatesEnabled WRITE setUpdatesEnabled DESIGNABLE false ) + Q_PROPERTY( bool visible READ isVisible ) + Q_PROPERTY( QRect visibleRect READ visibleRect ) // obsolete + Q_PROPERTY( bool hidden READ isHidden WRITE setHidden DESIGNABLE false SCRIPTABLE false ) + Q_PROPERTY( bool shown READ isShown WRITE setShown DESIGNABLE false SCRIPTABLE false ) + Q_PROPERTY( bool minimized READ isMinimized ) + Q_PROPERTY( bool maximized READ isMaximized ) + Q_PROPERTY( bool fullScreen READ isFullScreen ) + Q_PROPERTY( QSize sizeHint READ sizeHint ) + Q_PROPERTY( QSize minimumSizeHint READ minimumSizeHint ) + Q_PROPERTY( QRect microFocusHint READ microFocusHint ) + Q_PROPERTY( bool acceptDrops READ acceptDrops WRITE setAcceptDrops ) + Q_PROPERTY( bool autoMask READ autoMask WRITE setAutoMask DESIGNABLE false SCRIPTABLE false ) + Q_PROPERTY( bool customWhatsThis READ customWhatsThis ) + Q_PROPERTY( bool inputMethodEnabled READ isInputMethodEnabled WRITE setInputMethodEnabled DESIGNABLE false SCRIPTABLE false ) + Q_PROPERTY( double windowOpacity READ windowOpacity WRITE setWindowOpacity DESIGNABLE false ) + +public: + Q_EXPLICIT QWidget( QWidget* parent=0, const char* name=0, WFlags f=0 ); + ~QWidget(); + + WId winId() const; + void setName( const char *name ); +#ifndef QT_NO_STYLE + // GUI style setting + + QStyle &style() const; + void setStyle( QStyle * ); + QStyle* setStyle( const QString& ); +#endif + // Widget types and states + + bool isTopLevel() const; + bool isDialog() const; + bool isPopup() const; + bool isDesktop() const; + bool isModal() const; + + bool isEnabled() const; + bool isEnabledTo(QWidget*) const; + bool isEnabledToTLW() const; + +public slots: + virtual void setEnabled( bool ); + void setDisabled( bool ); + + // Widget coordinates + +public: + QRect frameGeometry() const; + const QRect &geometry() const; + int x() const; + int y() const; + QPoint pos() const; + QSize frameSize() const; + QSize size() const; + int width() const; + int height() const; + QRect rect() const; + QRect childrenRect() const; + QRegion childrenRegion() const; + + QSize minimumSize() const; + QSize maximumSize() const; + int minimumWidth() const; + int minimumHeight() const; + int maximumWidth() const; + int maximumHeight() const; + void setMinimumSize( const QSize & ); + virtual void setMinimumSize( int minw, int minh ); + void setMaximumSize( const QSize & ); + virtual void setMaximumSize( int maxw, int maxh ); + void setMinimumWidth( int minw ); + void setMinimumHeight( int minh ); + void setMaximumWidth( int maxw ); + void setMaximumHeight( int maxh ); + + QSize sizeIncrement() const; + void setSizeIncrement( const QSize & ); + virtual void setSizeIncrement( int w, int h ); + QSize baseSize() const; + void setBaseSize( const QSize & ); + void setBaseSize( int basew, int baseh ); + + void setFixedSize( const QSize & ); + void setFixedSize( int w, int h ); + void setFixedWidth( int w ); + void setFixedHeight( int h ); + + // Widget coordinate mapping + + QPoint mapToGlobal( const QPoint & ) const; + QPoint mapFromGlobal( const QPoint & ) const; + QPoint mapToParent( const QPoint & ) const; + QPoint mapFromParent( const QPoint & ) const; + QPoint mapTo( QWidget *, const QPoint & ) const; + QPoint mapFrom( QWidget *, const QPoint & ) const; + + QWidget *topLevelWidget() const; + + // Widget attribute functions + + BackgroundMode backgroundMode() const; + virtual void setBackgroundMode( BackgroundMode ); + void setBackgroundMode( BackgroundMode, BackgroundMode ); + + const QColor & foregroundColor() const; + + const QColor & eraseColor() const; + virtual void setEraseColor( const QColor & ); + + const QPixmap * erasePixmap() const; + virtual void setErasePixmap( const QPixmap & ); + +#ifndef QT_NO_PALETTE + const QColorGroup & colorGroup() const; + const QPalette & palette() const; + bool ownPalette() const; + virtual void setPalette( const QPalette & ); + void unsetPalette(); +#endif + + const QColor & paletteForegroundColor() const; + void setPaletteForegroundColor( const QColor & ); + + const QColor & paletteBackgroundColor() const; + virtual void setPaletteBackgroundColor( const QColor & ); + + const QPixmap * paletteBackgroundPixmap() const; + virtual void setPaletteBackgroundPixmap( const QPixmap & ); + + const QBrush& backgroundBrush() const; + + QFont font() const; + bool ownFont() const; + virtual void setFont( const QFont & ); + void unsetFont(); + QFontMetrics fontMetrics() const; + QFontInfo fontInfo() const; + +#ifndef QT_NO_CURSOR + const QCursor &cursor() const; + bool ownCursor() const; + virtual void setCursor( const QCursor & ); + virtual void unsetCursor(); +#endif +#ifndef QT_NO_WIDGET_TOPEXTRA + QString caption() const; + const QPixmap *icon() const; + QString iconText() const; +#endif + bool hasMouseTracking() const; + bool hasMouse() const; + + virtual void setMask( const QBitmap & ); + virtual void setMask( const QRegion & ); + void clearMask(); + + const QColor & backgroundColor() const; // obsolete, use eraseColor() + virtual void setBackgroundColor( const QColor & ); // obsolete, use setEraseColor() + const QPixmap * backgroundPixmap() const; // obsolete, use erasePixmap() + virtual void setBackgroundPixmap( const QPixmap & ); // obsolete, use setErasePixmap() + +public slots: +#ifndef QT_NO_WIDGET_TOPEXTRA + virtual void setCaption( const QString &); + virtual void setIcon( const QPixmap & ); + virtual void setIconText( const QString &); +#endif + virtual void setMouseTracking( bool enable ); + + // Keyboard input focus functions + + virtual void setFocus(); + void clearFocus(); + +public: + enum FocusPolicy { + NoFocus = 0, + TabFocus = 0x1, + ClickFocus = 0x2, + StrongFocus = TabFocus | ClickFocus | 0x8, + WheelFocus = StrongFocus | 0x4 + }; + + bool isActiveWindow() const; + virtual void setActiveWindow(); + bool isFocusEnabled() const; + + FocusPolicy focusPolicy() const; + virtual void setFocusPolicy( FocusPolicy ); + bool hasFocus() const; + static void setTabOrder( QWidget *, QWidget * ); + virtual void setFocusProxy( QWidget * ); + QWidget * focusProxy() const; + + void setInputMethodEnabled( bool b ); + bool isInputMethodEnabled() const; + // Grab functions + + void grabMouse(); +#ifndef QT_NO_CURSOR + void grabMouse( const QCursor & ); +#endif + void releaseMouse(); + void grabKeyboard(); + void releaseKeyboard(); + static QWidget * mouseGrabber(); + static QWidget * keyboardGrabber(); + + // Update/refresh functions + + bool isUpdatesEnabled() const; + +#if 0 //def Q_WS_QWS + void repaintUnclipped( const QRegion &, bool erase = TRUE ); +#endif +public slots: + virtual void setUpdatesEnabled( bool enable ); + void update(); + void update( int x, int y, int w, int h ); + void update( const QRect& ); + void repaint(); + void repaint( bool erase ); + void repaint( int x, int y, int w, int h, bool erase=TRUE ); + void repaint( const QRect &, bool erase = TRUE ); + void repaint( const QRegion &, bool erase = TRUE ); + + // Widget management functions + + virtual void show(); + virtual void hide(); + void setShown( bool show ); + void setHidden( bool hide ); +#ifndef QT_NO_COMPAT + void iconify() { showMinimized(); } +#endif + virtual void showMinimized(); + virtual void showMaximized(); + void showFullScreen(); + virtual void showNormal(); + virtual void polish(); + void constPolish() const; + bool close(); + + void raise(); + void lower(); + void stackUnder( QWidget* ); + virtual void move( int x, int y ); + void move( const QPoint & ); + virtual void resize( int w, int h ); + void resize( const QSize & ); + virtual void setGeometry( int x, int y, int w, int h ); + virtual void setGeometry( const QRect & ); // ### make non virtual in Qt 4? + +public: + virtual bool close( bool alsoDelete ); + bool isVisible() const; + bool isVisibleTo(QWidget*) const; + bool isVisibleToTLW() const; // obsolete + QRect visibleRect() const; // obsolete + bool isHidden() const; + bool isShown() const; + bool isMinimized() const; + bool isMaximized() const; + bool isFullScreen() const; + + uint windowState() const; + void setWindowState(uint windowState); + + virtual QSize sizeHint() const; + virtual QSize minimumSizeHint() const; + virtual QSizePolicy sizePolicy() const; + virtual void setSizePolicy( QSizePolicy ); + void setSizePolicy( QSizePolicy::SizeType hor, QSizePolicy::SizeType ver, bool hfw = FALSE ); + virtual int heightForWidth(int) const; + + QRegion clipRegion() const; + +// ### move together with other slots in Qt 4.0 +public slots: + virtual void adjustSize(); + +public: +#ifndef QT_NO_LAYOUT + QLayout * layout() const { return lay_out; } +#endif + void updateGeometry(); + virtual void reparent( QWidget *parent, WFlags, const QPoint &, + bool showIt=FALSE ); + void reparent( QWidget *parent, const QPoint &, + bool showIt=FALSE ); +#ifndef QT_NO_COMPAT + void recreate( QWidget *parent, WFlags f, const QPoint & p, + bool showIt=FALSE ) { reparent(parent,f,p,showIt); } +#endif + + void erase(); + void erase( int x, int y, int w, int h ); + void erase( const QRect & ); + void erase( const QRegion & ); + void scroll( int dx, int dy ); + void scroll( int dx, int dy, const QRect& ); + + void drawText( int x, int y, const QString &); + void drawText( const QPoint &, const QString &); + + // Misc. functions + + QWidget * focusWidget() const; + QRect microFocusHint() const; + + // drag and drop + + bool acceptDrops() const; + virtual void setAcceptDrops( bool on ); + + // transparency and pseudo transparency + + virtual void setAutoMask(bool); + bool autoMask() const; + + enum BackgroundOrigin { WidgetOrigin, ParentOrigin, WindowOrigin, AncestorOrigin }; + + virtual void setBackgroundOrigin( BackgroundOrigin ); + BackgroundOrigin backgroundOrigin() const; + QPoint backgroundOffset() const; + + // whats this help + virtual bool customWhatsThis() const; + + QWidget * parentWidget( bool sameWindow = FALSE ) const; + WState testWState( WState s ) const; + WFlags testWFlags( WFlags f ) const; + static QWidget * find( WId ); + static QWidgetMapper *wmapper(); + + QWidget *childAt( int x, int y, bool includeThis = FALSE ) const; + QWidget *childAt( const QPoint &, bool includeThis = FALSE ) const; + +#if defined(Q_WS_QWS) + virtual QGfx * graphicsContext(bool clip_children=TRUE) const; +#endif +#if defined(Q_WS_MAC) + QRegion clippedRegion(bool do_children=TRUE); + uint clippedSerial(bool do_children=TRUE); +#ifndef QMAC_NO_QUARTZ + CGContextRef macCGContext(bool clipped=TRUE) const; +#endif +#endif +#if defined(Q_WS_X11) + enum X11WindowType { + X11WindowTypeSelect, + X11WindowTypeCombo, + X11WindowTypeDND, + X11WindowTypeTooltip, + X11WindowTypeMenu, // torn-off + X11WindowTypeDropdown, + X11WindowTypePopup + }; + void x11SetWindowType( X11WindowType type = X11WindowTypeSelect ); + void x11SetWindowTransient( QWidget* parent ); +#endif + void setWindowOpacity(double level); + double windowOpacity() const; + +protected: + // Event handlers + bool event( QEvent * ); + virtual void mousePressEvent( QMouseEvent * ); + virtual void mouseReleaseEvent( QMouseEvent * ); + virtual void mouseDoubleClickEvent( QMouseEvent * ); + virtual void mouseMoveEvent( QMouseEvent * ); +#ifndef QT_NO_WHEELEVENT + virtual void wheelEvent( QWheelEvent * ); +#endif + virtual void keyPressEvent( QKeyEvent * ); + virtual void keyReleaseEvent( QKeyEvent * ); + virtual void focusInEvent( QFocusEvent * ); + virtual void focusOutEvent( QFocusEvent * ); + virtual void enterEvent( QEvent * ); + virtual void leaveEvent( QEvent * ); + virtual void paintEvent( QPaintEvent * ); + virtual void moveEvent( QMoveEvent * ); + virtual void resizeEvent( QResizeEvent * ); + virtual void closeEvent( QCloseEvent * ); + virtual void contextMenuEvent( QContextMenuEvent * ); + virtual void imStartEvent( QIMEvent * ); + virtual void imComposeEvent( QIMEvent * ); + virtual void imEndEvent( QIMEvent * ); + virtual void tabletEvent( QTabletEvent * ); + +#ifndef QT_NO_DRAGANDDROP + virtual void dragEnterEvent( QDragEnterEvent * ); + virtual void dragMoveEvent( QDragMoveEvent * ); + virtual void dragLeaveEvent( QDragLeaveEvent * ); + virtual void dropEvent( QDropEvent * ); +#endif + + virtual void showEvent( QShowEvent * ); + virtual void hideEvent( QHideEvent * ); + +#if defined(Q_WS_MAC) + virtual bool macEvent( MSG * ); +#endif +#if defined(Q_WS_WIN) + virtual bool winEvent( MSG * ); +#endif +#if defined(Q_WS_X11) + virtual bool x11Event( XEvent * ); +#endif +#if defined(Q_WS_QWS) + virtual bool qwsEvent( QWSEvent * ); + virtual unsigned char *scanLine( int ) const; + virtual int bytesPerLine() const; +#endif + + virtual void updateMask(); + + // Misc. protected functions + +#ifndef QT_NO_STYLE + virtual void styleChange( QStyle& ); +#endif + virtual void enabledChange( bool oldEnabled ); +#ifndef QT_NO_PALETTE + virtual void paletteChange( const QPalette & ); +#endif + virtual void fontChange( const QFont & ); + virtual void windowActivationChange( bool oldActive ); + + int metric( int ) const; + +#if defined(Q_WS_X11) +#if !defined(QT_NO_IM_EXTENSIONS) + virtual QWidget *icHolderWidget(); +#else + QWidget *icHolderWidget(); +#endif + QInputContext *getInputContext(); + void changeInputContext( const QString & ); + void sendMouseEventToInputContext( int x, QEvent::Type type, + Qt::ButtonState button, + Qt::ButtonState state ); +#endif + void resetInputContext(); + + virtual void create( WId = 0, bool initializeWindow = TRUE, + bool destroyOldWindow = TRUE ); + virtual void destroy( bool destroyWindow = TRUE, + bool destroySubWindows = TRUE ); + uint getWState() const; + virtual void setWState( uint ); + void clearWState( uint n ); + WFlags getWFlags() const; + virtual void setWFlags( WFlags ); + void clearWFlags( WFlags n ); + + virtual bool focusNextPrevChild( bool next ); + + QWExtra *extraData(); + QTLWExtra *topData(); + QFocusData *focusData(); + + virtual void setKeyCompression(bool); + virtual void setMicroFocusHint(int x, int y, int w, int h, bool text=TRUE, QFont *f = 0); + +#if defined(Q_WS_MAC) + void dirtyClippedRegion(bool); + bool isClippedRegionDirty(); + virtual void setRegionDirty(bool); + virtual void macWidgetChangedWindow(); +#endif + +private slots: + void focusProxyDestroyed(); +#if defined(Q_WS_X11) + void destroyInputContext(); +#endif + +private: + void setFontSys( QFont *f = 0 ); +#if defined(Q_WS_X11) + void createInputContext(); + void focusInputContext(); + void unfocusInputContext(); + void checkChildrenDnd(); + +#ifndef QT_NO_XSYNC + void createSyncCounter(); + void destroySyncCounter(); + void incrementSyncCounter(); + void handleSyncRequest( void* ev ); +#endif + +#elif defined(Q_WS_MAC) + uint own_id : 1, macDropEnabled : 1; + EventHandlerRef window_event; + //mac event functions + void propagateUpdates(bool update_rgn=TRUE); + void update( const QRegion& ); + //friends, way too many - fix this immediately! + friend void qt_clean_root_win(); + friend bool qt_recreate_root_win(); + friend QPoint posInWindow(QWidget *); + friend bool qt_mac_update_sizer(QWidget *, int); + friend QWidget *qt_recursive_match(QWidget *widg, int x, int y); + friend bool qt_paint_children(QWidget *,QRegion &, uchar ops); + friend QMAC_PASCAL OSStatus qt_window_event(EventHandlerCallRef er, EventRef event, void *); + friend void qt_event_request_updates(QWidget *, const QRegion &, bool subtract); + friend bool qt_window_rgn(WId, short, RgnHandle, bool); + friend class QDragManager; +#endif + +#ifndef QT_NO_LAYOUT + void setLayout( QLayout *l ); +#endif + void setWinId( WId ); + void showWindow(); + void hideWindow(); + void showChildren( bool spontaneous ); + void hideChildren( bool spontaneous ); + void reparentSys( QWidget *parent, WFlags, const QPoint &, bool showIt); + void createTLExtra(); + void createExtra(); + void deleteExtra(); + void createSysExtra(); + void deleteSysExtra(); + void createTLSysExtra(); + void deleteTLSysExtra(); + void deactivateWidgetCleanup(); + void internalSetGeometry( int, int, int, int, bool ); + void reparentFocusWidgets( QWidget * ); + QFocusData *focusData( bool create ); + void setBackgroundFromMode(); + void setBackgroundColorDirect( const QColor & ); + void setBackgroundPixmapDirect( const QPixmap & ); + void setBackgroundModeDirect( BackgroundMode ); + void setBackgroundEmpty(); + void updateFrameStrut() const; +#if defined(Q_WS_X11) + void setBackgroundX11Relative(); +#endif + + WId winid; + uint widget_state; + uint widget_flags; + uint focus_policy : 4; + uint own_font :1; + uint own_palette :1; + uint sizehint_forced :1; + uint is_closing :1; + uint in_show : 1; + uint in_show_maximized : 1; + uint fstrut_dirty : 1; + uint im_enabled : 1; + QRect crect; + QColor bg_col; +#ifndef QT_NO_PALETTE + QPalette pal; +#endif + QFont fnt; +#ifndef QT_NO_LAYOUT + QLayout *lay_out; +#endif +#if defined(Q_WS_X11) && !defined(QT_NO_IM) && !defined(QT_NO_IM_EXTENSIONS) + QInputContext *ic; // Input Context +#endif + QWExtra *extra; +#if defined(Q_WS_QWS) + QRegion req_region; // Requested region + mutable QRegion paintable_region; // Paintable region + mutable bool paintable_region_dirty;// needs to be recalculated + mutable QRegion alloc_region; // Allocated region + mutable bool alloc_region_dirty; // needs to be recalculated + mutable int overlapping_children; // Handle overlapping children + + int alloc_region_index; + int alloc_region_revision; + + void updateOverlappingChildren() const; + void setChildrenAllocatedDirty(); + void setChildrenAllocatedDirty( const QRegion &r, const QWidget *dirty=0 ); + bool isAllocatedRegionDirty() const; + void updateRequestedRegion( const QPoint &gpos ); + QRegion requestedRegion() const; + QRegion allocatedRegion() const; + QRegion paintableRegion() const; + + void updateGraphicsContext( QGfx *qgfx_qws, bool clip_children ) const; +#ifndef QT_NO_CURSOR + void updateCursor( const QRegion &r ) const; +#endif + + // used to accumulate dirty region when children moved/resized. + QRegion dirtyChildren; + bool isSettingGeometry; + friend class QWSManager; +#endif + static int instanceCounter; // Current number of widget instances + static int maxInstances; // Maximum number of widget instances + + static void createMapper(); + static void destroyMapper(); + static QWidgetList *wList(); + static QWidgetList *tlwList(); + static QWidgetMapper *mapper; + friend class QApplication; + friend class QBaseApplication; + friend class QPainter; + friend class QFontMetrics; + friend class QFontInfo; + friend class QETWidget; + friend class QLayout; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + QWidget( const QWidget & ); + QWidget &operator=( const QWidget & ); +#endif + +public: // obsolete functions to dissappear or to become inline in 3.0 +#ifndef QT_NO_PALETTE + void setPalette( const QPalette &p, bool ) { setPalette( p ); } +#endif + void setFont( const QFont &f, bool ) { setFont( f ); } +}; + + +inline Qt::WState QWidget::testWState( WState s ) const +{ return (widget_state & s); } + +inline Qt::WFlags QWidget::testWFlags( WFlags f ) const +{ return (widget_flags & f); } + + +inline WId QWidget::winId() const +{ return winid; } + +inline bool QWidget::isTopLevel() const +{ return testWFlags(WType_TopLevel); } + +inline bool QWidget::isDialog() const +{ return testWFlags(WType_Dialog); } + +inline bool QWidget::isPopup() const +{ return testWFlags(WType_Popup); } + +inline bool QWidget::isDesktop() const +{ return testWFlags(WType_Desktop); } + +inline bool QWidget::isEnabled() const +{ return !testWState(WState_Disabled); } + +inline bool QWidget::isModal() const +{ return testWFlags(WShowModal); } + +inline bool QWidget::isEnabledToTLW() const +{ return isEnabled(); } + +inline const QRect &QWidget::geometry() const +{ return crect; } + +inline QSize QWidget::size() const +{ return crect.size(); } + +inline int QWidget::width() const +{ return crect.width(); } + +inline int QWidget::height() const +{ return crect.height(); } + +inline QRect QWidget::rect() const +{ return QRect(0,0,crect.width(),crect.height()); } + +inline int QWidget::minimumWidth() const +{ return minimumSize().width(); } + +inline int QWidget::minimumHeight() const +{ return minimumSize().height(); } + +inline int QWidget::maximumWidth() const +{ return maximumSize().width(); } + +inline int QWidget::maximumHeight() const +{ return maximumSize().height(); } + +inline void QWidget::setMinimumSize( const QSize &s ) +{ setMinimumSize(s.width(),s.height()); } + +inline void QWidget::setMaximumSize( const QSize &s ) +{ setMaximumSize(s.width(),s.height()); } + +inline void QWidget::setSizeIncrement( const QSize &s ) +{ setSizeIncrement(s.width(),s.height()); } + +inline void QWidget::setBaseSize( const QSize &s ) +{ setBaseSize(s.width(),s.height()); } + +inline const QColor &QWidget::eraseColor() const +{ return bg_col; } + +#ifndef QT_NO_PALETTE +inline const QPalette &QWidget::palette() const +{ return pal; } +#endif + +inline QFont QWidget::font() const +{ return fnt; } + +inline QFontMetrics QWidget::fontMetrics() const +{ return QFontMetrics(font()); } + +inline QFontInfo QWidget::fontInfo() const +{ return QFontInfo(font()); } + +inline bool QWidget::hasMouseTracking() const +{ return testWState(WState_MouseTracking); } + +inline bool QWidget::hasMouse() const +{ return testWState(WState_HasMouse); } + +inline bool QWidget::isFocusEnabled() const +{ return (FocusPolicy)focus_policy != NoFocus; } + +inline QWidget::FocusPolicy QWidget::focusPolicy() const +{ return (FocusPolicy)focus_policy; } + +inline bool QWidget::isUpdatesEnabled() const +{ return !testWState(WState_BlockUpdates); } + +inline void QWidget::update( const QRect &r ) +{ update( r.x(), r.y(), r.width(), r.height() ); } + +inline void QWidget::repaint() +{ repaint( TRUE ); } + +inline void QWidget::repaint( const QRect &r, bool erase ) +{ repaint( r.x(), r.y(), r.width(), r.height(), erase ); } + +inline void QWidget::erase() +{ erase( 0, 0, crect.width(), crect.height() ); } + +inline void QWidget::erase( const QRect &r ) +{ erase( r.x(), r.y(), r.width(), r.height() ); } + +inline bool QWidget::close() +{ return close( FALSE ); } + +inline bool QWidget::isVisible() const +{ return testWState(WState_Visible); } + +inline bool QWidget::isVisibleToTLW() const // obsolete +{ return isVisible(); } + +inline bool QWidget::isHidden() const +{ return testWState(WState_ForceHide); } + +inline bool QWidget::isShown() const +{ return !testWState(WState_ForceHide); } + +inline void QWidget::move( const QPoint &p ) +{ move( p.x(), p.y() ); } + +inline void QWidget::resize( const QSize &s ) +{ resize( s.width(), s.height()); } + +inline void QWidget::setGeometry( const QRect &r ) +{ setGeometry( r.left(), r.top(), r.width(), r.height() ); } + +inline void QWidget::drawText( const QPoint &p, const QString &s ) +{ drawText( p.x(), p.y(), s ); } + +inline QWidget *QWidget::parentWidget( bool sameWindow ) const +{ + if ( sameWindow ) + return isTopLevel() ? 0 : (QWidget *)QObject::parent(); + return (QWidget *)QObject::parent(); +} + +inline QWidgetMapper *QWidget::wmapper() +{ return mapper; } + +inline uint QWidget::getWState() const +{ return widget_state; } + +inline void QWidget::setWState( uint f ) +{ widget_state |= f; } + +inline void QWidget::clearWState( uint f ) +{ widget_state &= ~f; } + +inline Qt::WFlags QWidget::getWFlags() const +{ return widget_flags; } + +inline void QWidget::setWFlags( WFlags f ) +{ widget_flags |= f; } + +inline void QWidget::clearWFlags( WFlags f ) +{ widget_flags &= ~f; } + +inline void QWidget::constPolish() const +{ + if ( !testWState(WState_Polished) ) { + QWidget* that = (QWidget*) this; + that->polish(); + that->setWState(WState_Polished); // be on the safe side... + } +} +#ifndef QT_NO_CURSOR +inline bool QWidget::ownCursor() const +{ + return testWState( WState_OwnCursor ); +} +#endif +inline bool QWidget::ownFont() const +{ + return own_font; +} +#ifndef QT_NO_PALETTE +inline bool QWidget::ownPalette() const +{ + return own_palette; +} +#endif + +inline void QWidget::setSizePolicy( QSizePolicy::SizeType hor, QSizePolicy::SizeType ver, bool hfw ) +{ + setSizePolicy( QSizePolicy( hor, ver, hfw) ); +} + +inline bool QWidget::isInputMethodEnabled() const +{ + return (bool)im_enabled; +} + +// Extra QWidget data +// - to minimize memory usage for members that are seldom used. +// - top-level widgets have extra extra data to reduce cost further + +class QFocusData; +class QWSManager; +#if defined(Q_WS_WIN) +class QOleDropTarget; +#endif +#if defined(Q_WS_MAC) +class QMacDndExtra; +#endif + +struct Q_EXPORT QTLWExtra { +#ifndef QT_NO_WIDGET_TOPEXTRA + QString caption; // widget caption + QString iconText; // widget icon text + QPixmap *icon; // widget icon +#endif + QFocusData *focusData; // focus data (for TLW) + short incw, inch; // size increments + // frame strut + ulong fleft, fright, ftop, fbottom; + uint unused : 8; // not used at this point... +#if defined( Q_WS_WIN ) || defined( Q_WS_MAC ) + uint opacity : 8; // Stores opacity level on Windows/Mac OS X. +#endif + uint savedFlags; // Save widgetflags while showing fullscreen + short basew, baseh; // base sizes +#if defined(Q_WS_X11) + WId parentWinId; // parent window Id (valid after reparenting) + uint embedded : 1; // window is embedded in another Qt application + uint spont_unmapped: 1; // window was spontaneously unmapped + uint reserved: 1; // reserved + uint dnd : 1; // DND properties installed + uint uspos : 1; // User defined position + uint ussize : 1; // User defined size +#if defined(QT_NO_IM_EXTENSIONS) + void *xic; // Input Context +#endif +#ifndef QT_NO_XSYNC + ulong syncCounter; + uint syncRequestValue[2]; +#endif +#endif +#if defined(Q_WS_MAC) + WindowGroupRef group; + uint is_moved: 1; + uint resizer : 4; +#endif +#if defined(Q_WS_QWS) && !defined ( QT_NO_QWS_MANAGER ) + QRegion decor_allocated_region; // decoration allocated region + QWSManager *qwsManager; +#endif +#if defined(Q_WS_WIN) + HICON winIcon; // internal Windows icon +#endif + QRect normalGeometry; // used by showMin/maximized/FullScreen +#ifdef Q_WS_WIN + uint style, exstyle; +#endif +}; + + +#define QWIDGETSIZE_MAX 32767 + +// dear user: you can see this struct, but it is internal. do not touch. + +struct Q_EXPORT QWExtra { + Q_INT16 minw, minh; // minimum size + Q_INT16 maxw, maxh; // maximum size + QPixmap *bg_pix; // background pixmap + QWidget *focus_proxy; +#ifndef QT_NO_CURSOR + QCursor *curs; +#endif + QTLWExtra *topextra; // only useful for TLWs +#if defined(Q_WS_WIN) + QOleDropTarget *dropTarget; // drop target +#endif +#if defined(Q_WS_X11) + WId xDndProxy; // XDND forwarding to embedded windows +#endif +#if defined(Q_WS_MAC) + QRegion clip_saved, clip_sibs, clip_children; + QMacDndExtra *macDndExtra; + QRegion dirty_area; + uint clip_dirty : 1, clip_serial : 15; + uint child_dirty : 1, child_serial : 15; +#ifndef QMAC_NO_QUARTZ + uint ctx_children_clipped:1; +#endif // QMAC_NO_QUARTZ + uint has_dirty_area:1; +#endif // Q_WS_MAC + uint bg_origin : 2; +#if defined(Q_WS_X11) + uint children_use_dnd : 1; + uint compress_events : 1; +#endif +#if defined(Q_WS_QWS) || defined(Q_WS_MAC) + QRegion mask; // widget mask +#endif + char bg_mode; // background mode + char bg_mode_visual; // visual background mode +#ifndef QT_NO_STYLE + QStyle* style; +#endif + QRect micro_focus_hint; // micro focus hint + QSizePolicy size_policy; +}; + +#define Q_DEFINED_QWIDGET +#include "qwinexport.h" + +#endif // QWIDGET_H diff --git a/src/kernel/qwidget_p.h b/src/kernel/qwidget_p.h new file mode 100644 index 0000000..c9be04a --- /dev/null +++ b/src/kernel/qwidget_p.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Definition of some Qt private functions. +** +** Created : 000903 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWIDGET_P_H +#define QWIDGET_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qapplication_*.cpp, qwidget*.cpp and qfiledialog.cpp. This header +// file may change from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +#if defined (Q_WS_X11) || defined (Q_WS_QWS) +extern int qt_widget_tlw_gravity; +#endif + +#endif diff --git a/src/kernel/qwidget_x11.cpp b/src/kernel/qwidget_x11.cpp new file mode 100644 index 0000000..02fdebf --- /dev/null +++ b/src/kernel/qwidget_x11.cpp @@ -0,0 +1,3039 @@ +/**************************************************************************** +** +** Implementation of QWidget and QWindow classes for X11 +** +** Created : 931031 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qapplication.h" +#include "qapplication_p.h" +#include "qnamespace.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qbitmap.h" +#include "qobjectlist.h" +#include "qlayout.h" +#include "qtextcodec.h" +#include "qdatetime.h" +#include "qcursor.h" +#include "qt_x11_p.h" +#include <stdlib.h> + +// NOT REVISED + +// defined in qapplication_x11.cpp +extern Window qt_x11_wm_client_leader; +extern void qt_x11_create_wm_client_leader(); + +// defined in qapplication_x11.cpp +void qt_insert_sip( QWidget*, int, int ); +int qt_sip_count( QWidget* ); +bool qt_wstate_iconified( WId ); +void qt_updated_rootinfo(); + +#ifndef QT_NO_IM +#include "qinputcontext.h" +#include "qinputcontextfactory.h" +#endif + +// Paint event clipping magic +extern void qt_set_paintevent_clipping( QPaintDevice* dev, const QRegion& region); +extern void qt_clear_paintevent_clipping(); + +extern bool qt_dnd_enable( QWidget* w, bool on ); +extern bool qt_nograb(); + +// defined in qapplication_x11.cpp +extern void qt_deferred_map_add( QWidget* ); +extern void qt_deferred_map_take( QWidget* ); +extern bool qt_deferred_map_contains(QWidget *); + +static QWidget *mouseGrb = 0; +static QWidget *keyboardGrb = 0; + +// defined in qapplication_x11.cpp +extern Time qt_x_time; +extern Time qt_x_user_time; + +#ifndef QT_NO_XSYNC +extern Atom qt_net_wm_sync_request_counter; +extern Atom qt_net_wm_sync_request; +extern bool qt_use_xsync; +#endif + +// defined in qfont_x11.cpp +extern bool qt_has_xft; + +int qt_x11_create_desktop_on_screen = -1; + +/***************************************************************************** + QWidget member functions + *****************************************************************************/ + +// defined in qapplication_x11.cpp +extern Atom qt_wm_state; +extern Atom qt_wm_change_state; +extern Atom qt_wm_delete_window; +extern Atom qt_wm_take_focus; +extern Atom qt_wm_client_leader; +extern Atom qt_window_role; +extern Atom qt_sm_client_id; +extern Atom qt_utf8_string; +extern Atom qt_net_wm_context_help; +extern Atom qt_net_wm_ping; +extern Atom qt_xa_motif_wm_hints; +extern Atom qt_net_wm_name; +extern Atom qt_net_wm_icon_name; +extern Atom qt_net_wm_state; +extern Atom qt_net_wm_state_modal; +extern Atom qt_net_wm_state_max_v; +extern Atom qt_net_wm_state_max_h; +extern Atom qt_net_wm_state_fullscreen; +extern Atom qt_net_wm_state_above; +extern Atom qt_net_wm_state_stays_on_top; +extern Atom qt_net_wm_window_type; +extern Atom qt_net_wm_window_type_normal; +extern Atom qt_net_wm_window_type_dialog; +extern Atom qt_net_wm_window_type_toolbar; +extern Atom qt_net_wm_window_type_menu; +extern Atom qt_net_wm_window_type_utility; +extern Atom qt_net_wm_window_type_splash; +extern Atom qt_net_wm_window_type_override; +extern Atom qt_net_wm_window_type_dropdown_menu; +extern Atom qt_net_wm_window_type_popup_menu; +extern Atom qt_net_wm_window_type_combo; +extern Atom qt_net_wm_window_type_dnd; +extern Atom qt_net_wm_window_type_tooltip; +extern Atom qt_net_wm_pid; +extern Atom qt_net_wm_user_time; +extern Atom qt_enlightenment_desktop; +extern Atom qt_net_virtual_roots; +extern bool qt_broken_wm; + +// defined in qapplication_x11.cpp +extern bool qt_net_supports(Atom); +extern unsigned long *qt_net_virtual_root_list; + +#if defined (QT_TABLET_SUPPORT) +extern XDevice *devStylus; +extern XDevice *devEraser; +extern XEventClass event_list_stylus[7]; +extern XEventClass event_list_eraser[7]; +extern int qt_curr_events_stylus; +extern int qt_curr_events_eraser; +#endif + +const uint stdWidgetEventMask = // X event mask + (uint)( + KeyPressMask | KeyReleaseMask | + ButtonPressMask | ButtonReleaseMask | + KeymapStateMask | + ButtonMotionMask | + EnterWindowMask | LeaveWindowMask | + FocusChangeMask | + ExposureMask | + PropertyChangeMask | + StructureNotifyMask + ); + +const uint stdDesktopEventMask = // X event mask + (uint)( + KeymapStateMask | + EnterWindowMask | LeaveWindowMask | + PropertyChangeMask + ); + + +/* + The qt_ functions below are implemented in qwidgetcreate_x11.cpp. +*/ + +Window qt_XCreateWindow( const QWidget *creator, + Display *display, Window parent, + int x, int y, uint w, uint h, + int borderwidth, int depth, + uint windowclass, Visual *visual, + ulong valuemask, XSetWindowAttributes *attributes ); +Window qt_XCreateSimpleWindow( const QWidget *creator, + Display *display, Window parent, + int x, int y, uint w, uint h, int borderwidth, + ulong border, ulong background ); +void qt_XDestroyWindow( const QWidget *destroyer, + Display *display, Window window ); + +Q_EXPORT void qt_x11_enforce_cursor( QWidget * w ) +{ + if ( w->testWState( Qt::WState_OwnCursor ) ) { + QCursor * oc = QApplication::overrideCursor(); + if ( oc ) { + XDefineCursor( w->x11Display(), w->winId(), oc->handle() ); + } else if ( w->isEnabled() ) { + XDefineCursor( w->x11Display(), w->winId(), w->cursor().handle() ); + } else { + // enforce the windows behavior of clearing the cursor on + // disabled widgets + XDefineCursor( w->x11Display(), w->winId(), None ); + } + } else { + XDefineCursor( w->x11Display(), w->winId(), None ); + } +} + +Q_EXPORT void qt_wait_for_window_manager( QWidget* w ) +{ + QApplication::flushX(); + XEvent ev; + QTime t; + t.start(); + while ( !XCheckTypedWindowEvent( w->x11Display(), w->winId(), ReparentNotify, &ev ) ) { + if ( XCheckTypedWindowEvent( w->x11Display(), w->winId(), MapNotify, &ev ) ) + break; + if ( t.elapsed() > 500 ) + return; // give up, no event available + qApp->syncX(); // non-busy wait + } + qApp->x11ProcessEvent( &ev ); + if ( XCheckTypedWindowEvent( w->x11Display(), w->winId(), ConfigureNotify, &ev ) ) + qApp->x11ProcessEvent( &ev ); +} + +static void qt_net_change_wm_state(const QWidget* w, bool set, Atom one, Atom two = 0) +{ + if (w->isShown()) { + // managed by WM + XEvent e; + e.xclient.type = ClientMessage; + e.xclient.message_type = qt_net_wm_state; + e.xclient.display = w->x11Display(); + e.xclient.window = w->winId(); + e.xclient.format = 32; + e.xclient.data.l[ 0 ] = set ? 1 : 0; + e.xclient.data.l[ 1 ] = one; + e.xclient.data.l[ 2 ] = two; + e.xclient.data.l[ 3 ] = 0; + e.xclient.data.l[ 4 ] = 0; + XSendEvent(w->x11Display(), RootWindow(w->x11Display(), w->x11Screen()), + False, (SubstructureNotifyMask|SubstructureRedirectMask), &e); + } else { + Atom ret; + int format = 0, status; + unsigned char *data = 0; + unsigned long nitems = 0, after = 0; + Atom *old_states = 0; + status = XGetWindowProperty(w->x11Display(), w->winId(), + qt_net_wm_state, 0, 1024, False, + XA_ATOM, &ret, &format, &nitems, + &after, &data); + if (status == Success && ret == XA_ATOM && format == 32 && nitems > 0) + old_states = (Atom *) data; + else + nitems = 0; + + Atom *new_states = new Atom[nitems + 2]; + int i, j = 0; + for (i = 0; i < (int)nitems; ++i) { + if (old_states[i] && old_states[i] != one && old_states[i] != two) + new_states[j++] = old_states[i]; + } + + if (set) { + if (one) new_states[j++] = one; + if (two) new_states[j++] = two; + } + + if (j) + XChangeProperty(w->x11Display(), w->winId(), qt_net_wm_state, XA_ATOM, 32, + PropModeReplace, (uchar *) new_states, j); + else + XDeleteProperty(w->x11Display(), w->winId(), qt_net_wm_state); + + delete [] new_states; + if (data) XFree(data); + } +} + +/*! + Creates a new widget window if \a window is 0, otherwise sets the + widget's window to \a window. + + Initializes the window (sets the geometry etc.) if \a + initializeWindow is TRUE. If \a initializeWindow is FALSE, no + initialization is performed. This parameter only makes sense if \a + window is a valid window. + + Destroys the old window if \a destroyOldWindow is TRUE. If \a + destroyOldWindow is FALSE, you are responsible for destroying the + window yourself (using platform native code). + + The QWidget constructor calls create(0,TRUE,TRUE) to create a + window for this widget. +*/ + +void QWidget::create( WId window, bool initializeWindow, bool destroyOldWindow) +{ + if ( testWState(WState_Created) && window == 0 ) + return; + + // set created flag + setWState( WState_Created ); + + bool popup = testWFlags(WType_Popup); + bool dialog = testWFlags(WType_Dialog); + bool desktop = testWFlags(WType_Desktop); + + // top-level widget + if ( !parentWidget() || parentWidget()->isDesktop() ) + setWFlags( WType_TopLevel ); + + // these are top-level, too + if ( dialog || popup || desktop || testWFlags(WStyle_Splash)) + setWFlags( WType_TopLevel ); + + // a popup stays on top + if ( popup ) + setWFlags(WStyle_StaysOnTop); + + bool topLevel = testWFlags(WType_TopLevel); + Window parentw, destroyw = 0; + WId id; + + // always initialize + if ( !window ) + initializeWindow = TRUE; + + if ( desktop && + qt_x11_create_desktop_on_screen >= 0 && + qt_x11_create_desktop_on_screen != x11Screen() ) { + // desktop on a certain screen other than the default requested + QPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = qt_x11_create_desktop_on_screen; + xd->x_depth = QPaintDevice::x11AppDepth( xd->x_screen ); + xd->x_cells = QPaintDevice::x11AppCells( xd->x_screen ); + xd->x_colormap = QPaintDevice::x11AppColormap( xd->x_screen ); + xd->x_defcolormap = QPaintDevice::x11AppDefaultColormap( xd->x_screen ); + xd->x_visual = QPaintDevice::x11AppVisual( xd->x_screen ); + xd->x_defvisual = QPaintDevice::x11AppDefaultVisual( xd->x_screen ); + setX11Data( xd ); + } else if ( parentWidget() && parentWidget()->x11Screen() != x11Screen() ) { + // if we have a parent widget, move to its screen if necessary + QPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = parentWidget()->x11Screen(); + xd->x_depth = QPaintDevice::x11AppDepth( xd->x_screen ); + xd->x_cells = QPaintDevice::x11AppCells( xd->x_screen ); + xd->x_colormap = QPaintDevice::x11AppColormap( xd->x_screen ); + xd->x_defcolormap = QPaintDevice::x11AppDefaultColormap( xd->x_screen ); + xd->x_visual = QPaintDevice::x11AppVisual( xd->x_screen ); + xd->x_defvisual = QPaintDevice::x11AppDefaultVisual( xd->x_screen ); + setX11Data( xd ); + } + + //get display, screen number, root window and desktop geometry for + //the current screen + Display *dpy = x11Display(); + int scr = x11Screen(); + Window root_win = RootWindow( dpy, scr ); + int sw = DisplayWidth(dpy,scr); + int sh = DisplayHeight(dpy,scr); + + if ( desktop ) { // desktop widget + dialog = popup = FALSE; // force these flags off + crect.setRect( 0, 0, sw, sh ); + } else if ( topLevel ) { // calc pos/size from screen + crect.setRect( sw/4, 3*sh/10, sw/2, 4*sh/10 ); + } else { // child widget + crect.setRect( 0, 0, 100, 30 ); + } + + parentw = topLevel ? root_win : parentWidget()->winId(); + + XSetWindowAttributes wsa; + + if ( window ) { // override the old window + if ( destroyOldWindow ) + destroyw = winid; + id = window; + setWinId( window ); + XWindowAttributes a; + XGetWindowAttributes( dpy, window, &a ); + crect.setRect( a.x, a.y, a.width, a.height ); + + if ( a.map_state == IsUnmapped ) + clearWState( WState_Visible ); + else + setWState( WState_Visible ); + + QPaintDeviceX11Data* xd = getX11Data( TRUE ); + + // find which screen the window is on... + xd->x_screen = QPaintDevice::x11AppScreen(); // by default, use the default :) + int i; + for ( i = 0; i < ScreenCount( dpy ); i++ ) { + if ( RootWindow( dpy, i ) == a.root ) { + xd->x_screen = i; + break; + } + } + + xd->x_depth = a.depth; + xd->x_cells = DisplayCells( dpy, xd->x_screen ); + xd->x_visual = a.visual; + xd->x_defvisual = ( XVisualIDFromVisual( a.visual ) == + XVisualIDFromVisual( (Visual*)x11AppVisual(x11Screen()) ) ); + xd->x_colormap = a.colormap; + xd->x_defcolormap = ( a.colormap == x11AppColormap( x11Screen() ) ); + setX11Data( xd ); + } else if ( desktop ) { // desktop widget + id = (WId)parentw; // id = root window + QWidget *otherDesktop = find( id ); // is there another desktop? + if ( otherDesktop && otherDesktop->testWFlags(WPaintDesktop) ) { + otherDesktop->setWinId( 0 ); // remove id from widget mapper + setWinId( id ); // make sure otherDesktop is + otherDesktop->setWinId( id ); // found first + } else { + setWinId( id ); + } + } else { + if ( x11DefaultVisual() && x11DefaultColormap() ) { + id = (WId)qt_XCreateSimpleWindow( this, dpy, parentw, + crect.left(), crect.top(), + crect.width(), crect.height(), + 0, + black.pixel(x11Screen()), + bg_col.pixel(x11Screen()) ); + } else { + wsa.background_pixel = bg_col.pixel(x11Screen()); + wsa.border_pixel = black.pixel(x11Screen()); + wsa.colormap = (Colormap)x11Colormap(); + id = (WId)qt_XCreateWindow( this, dpy, parentw, + crect.left(), crect.top(), + crect.width(), crect.height(), + 0, x11Depth(), InputOutput, + (Visual*)x11Visual(), + CWBackPixel|CWBorderPixel|CWColormap, + &wsa ); + } + + setWinId( id ); // set widget id/handle + hd + } + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } + + if ( qt_has_xft ) + rendhd = (HANDLE) XftDrawCreate( dpy, id, (Visual *) x11Visual(), + x11Colormap() ); +#endif // QT_NO_XFTFREETYPE + + // NET window states + long net_winstates[6] = { 0, 0, 0, 0, 0, 0 }; + int curr_winstate = 0; + + struct { + ulong flags, functions, decorations; + long input_mode; + ulong status; + } mwmhints; + + mwmhints.flags = mwmhints.functions = 0L; + mwmhints.decorations = (1L << 0); // MWM_DECOR_ALL + mwmhints.input_mode = 0L; + mwmhints.status = 0L; + + if (topLevel && ! (desktop || popup)) { + ulong wsa_mask = 0; + + if ( testWFlags(WStyle_Splash) ) { + if (qt_net_supports(qt_net_wm_window_type_splash)) { + clearWFlags( WX11BypassWM ); + } else { + setWFlags( WX11BypassWM | WStyle_Tool | WStyle_NoBorder ); + } + } + if (testWFlags(WStyle_Customize)) { + mwmhints.decorations = 0L; + mwmhints.flags |= (1L << 1); // MWM_HINTS_DECORATIONS + + if ( testWFlags( WStyle_NormalBorder | WStyle_DialogBorder ) ) { + mwmhints.decorations |= (1L << 1); // MWM_DECOR_BORDER + mwmhints.decorations |= (1L << 2); // MWM_DECOR_RESIZEH + } + + if ( testWFlags( WStyle_Title ) ) + mwmhints.decorations |= (1L << 3); // MWM_DECOR_TITLE + + if ( testWFlags( WStyle_SysMenu ) ) + mwmhints.decorations |= (1L << 4); // MWM_DECOR_MENU + + if ( testWFlags( WStyle_Minimize ) ) + mwmhints.decorations |= (1L << 5); // MWM_DECOR_MINIMIZE + + if ( testWFlags( WStyle_Maximize ) ) + mwmhints.decorations |= (1L << 6); // MWM_DECOR_MAXIMIZE + + if (testWFlags(WStyle_Tool)) { + wsa.save_under = True; + wsa_mask |= CWSaveUnder; + } + } else if (testWFlags(WType_Dialog)) { + setWFlags(WStyle_NormalBorder | WStyle_Title | + WStyle_SysMenu | WStyle_ContextHelp); + } else { + setWFlags(WStyle_NormalBorder | WStyle_Title | + WStyle_MinMax | WStyle_SysMenu); + + // maximized netwm state + if (testWFlags(WState_Maximized)) { + net_winstates[curr_winstate++] = qt_net_wm_state_max_v; + net_winstates[curr_winstate++] = qt_net_wm_state_max_h; + } + } + + // stays on top + if (testWFlags(WStyle_StaysOnTop)) { + net_winstates[curr_winstate++] = qt_net_wm_state_above; + net_winstates[curr_winstate++] = qt_net_wm_state_stays_on_top; + } + + if (testWFlags(WShowModal)) { + mwmhints.input_mode = 3L; // MWM_INPUT_FULL_APPLICATION_MODAL + mwmhints.flags |= (1L << 2); // MWM_HINTS_INPUT_MODE + + net_winstates[curr_winstate++] = qt_net_wm_state_modal; + } + + if ( testWFlags( WX11BypassWM ) ) { + wsa.override_redirect = True; + wsa_mask |= CWOverrideRedirect; + } + + if ( wsa_mask && initializeWindow ) + XChangeWindowAttributes( dpy, id, wsa_mask, &wsa ); + } else { + if (! testWFlags(WStyle_Customize)) + setWFlags(WStyle_NormalBorder | WStyle_Title | + WStyle_MinMax | WStyle_SysMenu); + } + + + if ( !initializeWindow ) { + // do no initialization + } else if ( popup ) { // popup widget + wsa.override_redirect = True; + wsa.save_under = True; + XChangeWindowAttributes( dpy, id, CWOverrideRedirect | CWSaveUnder, + &wsa ); + x11SetWindowType(); + } else if ( topLevel && !desktop ) { // top-level widget + QWidget *p = parentWidget(); // real parent + if (p) + p = p->topLevelWidget(); + + if (dialog || testWFlags(WStyle_DialogBorder) || testWFlags(WStyle_Tool)) { + if ( p ) + XSetTransientForHint( dpy, id, p->winId() ); + else // application-modal + XSetTransientForHint( dpy, id, root_win ); + } + + // find the real client leader, i.e. a toplevel without parent + while ( p && p->parentWidget()) + p = p->parentWidget()->topLevelWidget(); + + XSizeHints size_hints; + size_hints.flags = USSize | PSize | PWinGravity; + size_hints.x = crect.left(); + size_hints.y = crect.top(); + size_hints.width = crect.width(); + size_hints.height = crect.height(); + size_hints.win_gravity = + QApplication::reverseLayout() ? NorthEastGravity : NorthWestGravity; + + XWMHints wm_hints; // window manager hints + wm_hints.input = True; + wm_hints.initial_state = NormalState; + wm_hints.flags = InputHint | StateHint; + if ( !qt_x11_wm_client_leader ) + qt_x11_create_wm_client_leader(); + + wm_hints.window_group = qt_x11_wm_client_leader; + wm_hints.flags |= WindowGroupHint; + + XClassHint class_hint; + class_hint.res_name = (char *) qAppName(); // application name + class_hint.res_class = (char *) qAppClass(); // application class + + XSetWMProperties( dpy, id, 0, 0, 0, 0, &size_hints, &wm_hints, &class_hint ); + + XResizeWindow( dpy, id, crect.width(), crect.height() ); + XStoreName( dpy, id, qAppName() ); + Atom protocols[5]; + int n = 0; + protocols[n++] = qt_wm_delete_window; // support del window protocol + protocols[n++] = qt_wm_take_focus; // support take focus window protocol + protocols[n++] = qt_net_wm_ping; // support _NET_WM_PING protocol +#ifndef QT_NO_XSYNC + protocols[n++] = qt_net_wm_sync_request;// support the _NET_WM_SYNC_REQUEST protocol +#endif + if ( testWFlags( WStyle_ContextHelp ) ) + protocols[n++] = qt_net_wm_context_help; + XSetWMProtocols( dpy, id, protocols, n ); + + // set mwm hints + if ( mwmhints.flags != 0l ) + XChangeProperty(dpy, id, qt_xa_motif_wm_hints, qt_xa_motif_wm_hints, 32, + PropModeReplace, (unsigned char *) &mwmhints, 5); + else + XDeleteProperty(dpy, id, qt_xa_motif_wm_hints); + + x11SetWindowType(); + + // set _NET_WM_WINDOW_STATE + if (curr_winstate > 0) + XChangeProperty(dpy, id, qt_net_wm_state, XA_ATOM, 32, PropModeReplace, + (unsigned char *) net_winstates, curr_winstate); + else + XDeleteProperty(dpy, id, qt_net_wm_state); + + // set _NET_WM_PID + long curr_pid = getpid(); + XChangeProperty(dpy, id, qt_net_wm_pid, XA_CARDINAL, 32, PropModeReplace, + (unsigned char *) &curr_pid, 1); + +#ifndef QT_NO_XSYNC + // set _NET_WM_SYNC_COUNTER + createSyncCounter(); + long counterVal = topData()->syncCounter; + XChangeProperty( dpy, id, qt_net_wm_sync_request_counter, XA_CARDINAL, 32, PropModeReplace, + (unsigned char*) &counterVal, 1); +#endif + + // when we create a toplevel widget, the frame strut should be dirty + fstrut_dirty = 1; + + // declare the widget's object name as window role + XChangeProperty( dpy, id, + qt_window_role, XA_STRING, 8, PropModeReplace, + (unsigned char *)name(), qstrlen( name() ) ); + + // set client leader property + XChangeProperty( dpy, id, qt_wm_client_leader, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&qt_x11_wm_client_leader, 1 ); + } else { + // non-toplevel widgets don't have a frame, so no need to + // update the strut + fstrut_dirty = 0; + } + + if ( initializeWindow ) { + // don't erase when resizing + wsa.bit_gravity = + QApplication::reverseLayout() ? NorthEastGravity : NorthWestGravity; + XChangeWindowAttributes( dpy, id, CWBitGravity, &wsa ); + } + + setWState( WState_MouseTracking ); + setMouseTracking( FALSE ); // also sets event mask + if ( desktop ) { + setWState( WState_Visible ); + } else if ( topLevel ) { // set X cursor + setWState( WState_OwnCursor ); + if ( initializeWindow ) + qt_x11_enforce_cursor( this ); + } + + if ( destroyw ) + qt_XDestroyWindow( this, dpy, destroyw ); + +#if !defined(QT_NO_IM_EXTENSIONS) + ic = 0; +#endif +} + + +/*! + Frees up window system resources. Destroys the widget window if \a + destroyWindow is TRUE. + + destroy() calls itself recursively for all the child widgets, + passing \a destroySubWindows for the \a destroyWindow parameter. + To have more control over destruction of subwidgets, destroy + subwidgets selectively first. + + This function is usually called from the QWidget destructor. +*/ + +void QWidget::destroy( bool destroyWindow, bool destroySubWindows ) +{ + deactivateWidgetCleanup(); + if ( testWState(WState_Created) ) { + clearWState( WState_Created ); + if ( children() ) { + QObjectListIt it(*children()); + register QObject *obj; + while ( (obj=it.current()) ) { // destroy all widget children + ++it; + if ( obj->isWidgetType() ) + ((QWidget*)obj)->destroy(destroySubWindows, + destroySubWindows); + } + } + if ( mouseGrb == this ) + releaseMouse(); + if ( keyboardGrb == this ) + releaseKeyboard(); + if ( isTopLevel() ) + qt_deferred_map_take( this ); + if ( testWFlags(WShowModal) ) // just be sure we leave modal + qt_leave_modal( this ); + else if ( testWFlags(WType_Popup) ) + qApp->closePopup( this ); + +#ifndef QT_NO_XFTFREETYPE + if ( rendhd) { + if ( destroyWindow ) + XftDrawDestroy( (XftDraw *) rendhd ); + else + free( (void*) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + if ( testWFlags(WType_Desktop) ) { + if ( acceptDrops() ) + qt_dnd_enable( this, FALSE ); + } else { + if ( destroyWindow ) + qt_XDestroyWindow( this, x11Display(), winid ); + } +#ifndef QT_NO_XSYNC + destroySyncCounter(); +#endif + setWinId( 0 ); + + extern void qPRCleanup( QWidget *widget ); // from qapplication_x11.cpp + if ( testWState(WState_Reparented) ) + qPRCleanup(this); + + if( this == icHolderWidget() ) { + destroyInputContext(); + } else { + // release previous focus information participating with + // preedit preservation of qic + QInputContext *qic = getInputContext(); + if ( qic ) + qic->releaseComposingWidget( this ); + } + } +} + +void QWidget::reparentSys( QWidget *parent, WFlags f, const QPoint &p, bool showIt ) +{ + extern void qPRCreate( const QWidget *, Window ); + + Display *dpy = x11Display(); + QCursor oldcurs; + bool setcurs = testWState(WState_OwnCursor); + if ( setcurs ) { + oldcurs = cursor(); + unsetCursor(); + } + + // dnd unregister (we will register again below) + bool accept_drops = acceptDrops(); + setAcceptDrops( FALSE ); + + // clear mouse tracking, re-enabled below + bool mouse_tracking = hasMouseTracking(); + clearWState(WState_MouseTracking); + + QWidget* oldtlw = topLevelWidget(); + QWidget *oldparent = parentWidget(); + WId old_winid = winid; + if ( testWFlags(WType_Desktop) ) + old_winid = 0; + setWinId( 0 ); + + // hide and reparent our own window away. Otherwise we might get + // destroyed when emitting the child remove event below. See QWorkspace. + XUnmapWindow( x11Display(), old_winid ); + XReparentWindow( x11Display(), old_winid, + RootWindow( x11Display(), x11Screen() ), 0, 0 ); + + if ( this == icHolderWidget() ) { + // input contexts are sometimes associated with toplevel widgets, so + // we need destroy the context here. if we are reparenting back to + // toplevel, then we may have another context created, otherwise we + // will use our new ic holder's context + destroyInputContext(); + } + +#ifndef QT_NO_XSYNC + destroySyncCounter(); +#endif + + if ( isTopLevel() || !parent ) // we are toplevel, or reparenting to toplevel + topData()->parentWinId = 0; + + if ( parent != parentObj ) { + if ( parentObj ) // remove from parent + parentObj->removeChild( this ); + if ( parent ) // insert into new parent + parent->insertChild( this ); + } + bool enable = isEnabled(); // remember status + FocusPolicy fp = focusPolicy(); + QSize s = size(); + QPixmap *bgp = (QPixmap *)backgroundPixmap(); + QColor bgc = bg_col; // save colors + QString capt= caption(); + widget_flags = f; + clearWState( WState_Created | WState_Visible | WState_ForceHide ); + create(); + if ( isTopLevel() || (!parent || parent->isVisible() ) ) + setWState( WState_ForceHide ); // new widgets do not show up in already visible parents + + const QObjectList *chlist = children(); + if ( chlist ) { // reparent children + QObjectList childList(*chlist); + QObjectListIt it(childList); // iterate over copy + QObject *obj; + while ( (obj=it.current()) ) { + if ( obj->isWidgetType() ) { + QWidget *w = (QWidget *)obj; + if ( !w->isTopLevel() ) { + XReparentWindow( x11Display(), w->winId(), winId(), + w->geometry().x(), w->geometry().y() ); + } else if ( w->isPopup() + || w->testWFlags(WStyle_DialogBorder) + || w->testWFlags(WType_Dialog) + || w->testWFlags(WStyle_Tool) ) { + /* + when reparenting toplevel windows with toplevel-transient children, + we need to make sure that the window manager gets the updated + WM_TRANSIENT_FOR information... unfortunately, some window managers + don't handle changing WM_TRANSIENT_FOR before the toplevel window is + visible, so we unmap and remap all toplevel-transient children *after* + the toplevel parent has been mapped. thankfully, this is easy in Qt :) + */ + XUnmapWindow(w->x11Display(), w->winId()); + XSetTransientForHint(w->x11Display(), w->winId(), winId()); + QApplication::postEvent(w, new QEvent(QEvent::ShowWindowRequest)); + } + } + ++it; + } + } + qPRCreate( this, old_winid ); + if ( bgp ) + XSetWindowBackgroundPixmap( dpy, winid, bgp->handle() ); + else + XSetWindowBackground( dpy, winid, bgc.pixel(x11Screen()) ); + + if (isTopLevel()) { + // preserve maximized/fullscreen flags and the normal geometry + uint save_state = widget_state & (WState_Maximized | WState_FullScreen); + const QRect r = topData()->normalGeometry; + setGeometry(p.x(), p.y(), s.width(), s.height()); + widget_state |= save_state; + topData()->normalGeometry = r; + } else { + setGeometry(p.x(), p.y(), s.width(), s.height()); + } + + setEnabled( enable ); + setFocusPolicy( fp ); + if ( !capt.isNull() ) { + extra->topextra->caption = QString::null; + setCaption( capt ); + } + if ( showIt ) + show(); + if ( old_winid ) + qt_XDestroyWindow( this, dpy, old_winid ); + if ( setcurs ) + setCursor(oldcurs); + + reparentFocusWidgets( oldtlw ); + + // re-register dnd + if (oldparent) + oldparent->checkChildrenDnd(); + + if ( accept_drops ) + setAcceptDrops( TRUE ); + else { + checkChildrenDnd(); + topData()->dnd = 0; + qt_dnd_enable(this, (extra && extra->children_use_dnd)); + } + + // re-enable mouse tracking + if (mouse_tracking) + setMouseTracking(mouse_tracking); +} + +// Sets the EWMH (netwm) window type. Needed as a separate function +// because create() may be too soon in some cases. +void QWidget::x11SetWindowType( X11WindowType type ) +{ + // NET window types + long net_wintypes[7] = { 0, 0, 0, 0, 0, 0, 0 }; + int curr_wintype = 0; + if( testWFlags(WType_Desktop)) + return; + if( type == X11WindowTypeSelect ) { + if ( testWFlags(WStyle_Splash)) { + if (qt_net_supports(qt_net_wm_window_type_splash)) { + net_wintypes[curr_wintype++] = qt_net_wm_window_type_splash; + } + } else if (inherits("QToolBar")) { + // toolbar netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_toolbar; + } else if (testWFlags(WStyle_Customize) && testWFlags(WStyle_Tool)) { + // utility netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_utility; + } else if (testWFlags(WType_Dialog)) { + // dialog netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_dialog; + } + } else if( type == X11WindowTypeCombo ) { + // combo netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_combo; + } else if( type == X11WindowTypeDND ) { + // dnd netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_dnd; + } else if( type == X11WindowTypeDropdown ) { + // dropdown netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_dropdown_menu; + } else if( type == X11WindowTypePopup ) { + // popup netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_popup_menu; + } else if( type == X11WindowTypeMenu ) { + // menu netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_menu; + } else if( type == X11WindowTypeTooltip ) { + // tooltip netwm type + net_wintypes[curr_wintype++] = qt_net_wm_window_type_tooltip; + } + + // normal netwm type - default + net_wintypes[curr_wintype++] = qt_net_wm_window_type_normal; + // set _NET_WM_WINDOW_TYPE + if (curr_wintype > 0) + XChangeProperty(x11Display(), winId(), qt_net_wm_window_type, XA_ATOM, 32, PropModeReplace, + (unsigned char *) net_wintypes, curr_wintype); + else + XDeleteProperty(x11Display(), winId(), qt_net_wm_window_type); +} + +void QWidget::x11SetWindowTransient( QWidget* parent ) +{ + XSetTransientForHint( x11Display(), winId(), parent->winId()); +} + +/*! + Translates the widget coordinate \a pos to global screen + coordinates. For example, \c{mapToGlobal(QPoint(0,0))} would give + the global coordinates of the top-left pixel of the widget. + + \sa mapFromGlobal() mapTo() mapToParent() +*/ + +QPoint QWidget::mapToGlobal( const QPoint &pos ) const +{ + int x, y; + Window child; + XTranslateCoordinates( x11Display(), winId(), + QApplication::desktop()->screen(x11Screen())->winId(), + pos.x(), pos.y(), &x, &y, &child ); + return QPoint( x, y ); +} + +/*! + Translates the global screen coordinate \a pos to widget + coordinates. + + \sa mapToGlobal() mapFrom() mapFromParent() +*/ + +QPoint QWidget::mapFromGlobal( const QPoint &pos ) const +{ + int x, y; + Window child; + XTranslateCoordinates( x11Display(), + QApplication::desktop()->screen(x11Screen())->winId(), + winId(), pos.x(), pos.y(), &x, &y, &child ); + return QPoint( x, y ); +} + +/*! + When a widget gets focus, it should call setMicroFocusHint() with + some appropriate position and size, \a x, \a y, \a width and \a + height. This has no \e visual effect, it just provides hints to + any system-specific input handling tools. + + The \a text argument should be TRUE if this is a position for text + input. + + In the Windows version of Qt, this method sets the system caret, + which is used for user Accessibility focus handling. If \a text + is TRUE, it also sets the IME composition window in Far East Asian + language input systems. + + In the X11 version of Qt, if \a text is TRUE, this method sets the + input method focus point in the preedit (XIM "spot" point) for + complex language input handling. + + The font \a f is a rendering hint to the currently active input method. + If \a f is 0 the widget's font is used. + + \sa microFocusHint() +*/ +void QWidget::setMicroFocusHint(int x, int y, int width, int height, + bool text, QFont *f ) +{ +#ifndef QT_NO_IM + if ( text ) { + // trigger input context creation if it hasn't happened already + createInputContext(); + + QInputContext *qic = getInputContext(); + if(qic) { + QPoint gp = mapToGlobal( QPoint( x, y ) ); + qic->setMicroFocus(gp.x(), gp.y(), width, height, f); + } + } +#endif + + if ( QRect( x, y, width, height ) != microFocusHint() ) { + createExtra(); + extraData()->micro_focus_hint.setRect( x, y, width, height ); + } +} + + +void QWidget::setFontSys( QFont * ) +{ + // Nothing +} + + +void QWidget::setBackgroundColorDirect( const QColor &color ) +{ + bg_col = color; + if ( extra && extra->bg_pix ) { // kill the background pixmap + delete extra->bg_pix; + extra->bg_pix = 0; + } + XSetWindowBackground( x11Display(), winId(), bg_col.pixel(x11Screen()) ); +} + +static int allow_null_pixmaps = 0; + + +void QWidget::setBackgroundPixmapDirect( const QPixmap &pixmap ) +{ + QPixmap old; + if ( extra && extra->bg_pix ) + old = *extra->bg_pix; + if ( !allow_null_pixmaps && pixmap.isNull() ) { + XSetWindowBackground( x11Display(), winId(), bg_col.pixel(x11Screen()) ); + if ( extra && extra->bg_pix ) { + delete extra->bg_pix; + extra->bg_pix = 0; + } + } else { + QPixmap pm = pixmap; + if (!pm.isNull()) { + if ( pm.depth() == 1 && QPixmap::defaultDepth() > 1 ) { + pm = QPixmap( pixmap.size() ); + bitBlt( &pm, 0, 0, &pixmap, 0, 0, pm.width(), pm.height() ); + } + } + if ( extra && extra->bg_pix ) + delete extra->bg_pix; + else + createExtra(); + extra->bg_pix = new QPixmap( pm ); + Q_CHECK_PTR( extra->bg_pix ); + extra->bg_pix->x11SetScreen( x11Screen() ); + XSetWindowBackgroundPixmap( x11Display(), winId(), extra->bg_pix->handle() ); + if ( testWFlags(WType_Desktop) ) // save rootinfo later + qt_updated_rootinfo(); + } +} + + +/*! + Sets the window-system background of the widget to nothing. + + Note that "nothing" is actually a pixmap that isNull(), thus you + can check for an empty background by checking backgroundPixmap(). + + \sa setBackgroundPixmap(), setBackgroundColor() +*/ +void QWidget::setBackgroundEmpty() +{ + allow_null_pixmaps++; + setErasePixmap(QPixmap()); + allow_null_pixmaps--; +} + + +void QWidget::setBackgroundX11Relative() +{ + XSetWindowBackgroundPixmap( x11Display(), winId(), ParentRelative ); +} + +void QWidget::setCursor( const QCursor &cursor ) +{ + if ( cursor.handle() != arrowCursor.handle() + || (extra && extra->curs) ) { + createExtra(); + delete extra->curs; + extra->curs = new QCursor(cursor); + } + setWState( WState_OwnCursor ); + qt_x11_enforce_cursor( this ); + XFlush( x11Display() ); +} + +void QWidget::unsetCursor() +{ + if ( extra ) { + delete extra->curs; + extra->curs = 0; + } + if ( !isTopLevel() ) + clearWState( WState_OwnCursor ); + qt_x11_enforce_cursor( this ); + XFlush( x11Display() ); +} + +static XTextProperty* +qstring_to_xtp( const QString& s ) +{ + static XTextProperty tp = { 0, 0, 0, 0 }; + static bool free_prop = TRUE; // we can't free tp.value in case it references + // the data of the static QCString below. + if ( tp.value ) { + if ( free_prop ) + XFree( tp.value ); + tp.value = 0; + free_prop = TRUE; + } + + static const QTextCodec* mapper = QTextCodec::codecForLocale(); + int errCode = 0; + if ( mapper ) { + QCString mapped = mapper->fromUnicode(s); + char* tl[2]; + tl[0] = mapped.data(); + tl[1] = 0; + errCode = XmbTextListToTextProperty( QPaintDevice::x11AppDisplay(), + tl, 1, XStdICCTextStyle, &tp ); +#if defined(QT_DEBUG) + if ( errCode < 0 ) + qDebug( "qstring_to_xtp result code %d", errCode ); +#endif + } + if ( !mapper || errCode < 0 ) { + static QCString qcs; + qcs = s.ascii(); + tp.value = (uchar*)qcs.data(); + tp.encoding = XA_STRING; + tp.format = 8; + tp.nitems = qcs.length(); + free_prop = FALSE; + } + + // ### If we knew WM could understand unicode, we could use + // ### a much simpler, cheaper encoding... + /* + tp.value = (XChar2b*)s.unicode(); + tp.encoding = XA_UNICODE; // wish + tp.format = 16; + tp.nitems = s.length(); + */ + + return &tp; +} + +void QWidget::setCaption( const QString &caption ) +{ + if ( QWidget::caption() == caption ) + return; + + topData()->caption = caption; + XSetWMName( x11Display(), winId(), qstring_to_xtp(caption) ); + + QCString net_wm_name = caption.utf8(); + XChangeProperty(x11Display(), winId(), qt_net_wm_name, qt_utf8_string, 8, + PropModeReplace, (unsigned char *)net_wm_name.data(), + net_wm_name.length()); + + QEvent e( QEvent::CaptionChange ); + QApplication::sendEvent( this, &e ); +} + +void QWidget::setIcon( const QPixmap &pixmap ) +{ + if ( extra && extra->topextra ) { + delete extra->topextra->icon; + extra->topextra->icon = 0; + } else { + createTLExtra(); + } + Pixmap icon_pixmap = 0; + Pixmap mask_pixmap = 0; + if ( !pixmap.isNull() ) { + QPixmap* pm = new QPixmap( pixmap ); + extra->topextra->icon = pm; + if ( !pm->mask() ) + pm->setMask( pm->createHeuristicMask() ); // may do detach() + icon_pixmap = pm->handle(); + if ( pm->mask() ) + mask_pixmap = pm->mask()->handle(); + } + XWMHints *h = XGetWMHints( x11Display(), winId() ); + XWMHints wm_hints; + bool got_hints = h != 0; + if ( !got_hints ) { + h = &wm_hints; + h->flags = 0; + } + h->icon_pixmap = icon_pixmap; + h->icon_mask = mask_pixmap; + h->flags |= IconPixmapHint | IconMaskHint; + XSetWMHints( x11Display(), winId(), h ); + if ( got_hints ) + XFree( (char *)h ); + QEvent e( QEvent::IconChange ); + QApplication::sendEvent( this, &e ); +} + +void QWidget::setIconText( const QString &iconText ) +{ + if (QWidget::iconText() == iconText) + return; + + topData()->iconText = iconText; + XSetWMIconName( x11Display(), winId(), qstring_to_xtp(iconText) ); + + QCString net_wm_icon_name = iconText.utf8(); + XChangeProperty(x11Display(), winId(), qt_net_wm_icon_name, qt_utf8_string, 8, PropModeReplace, + (unsigned char *) net_wm_icon_name.data(), net_wm_icon_name.length()); +} + +void QWidget::setMouseTracking( bool enable ) +{ + bool gmt = QApplication::hasGlobalMouseTracking(); + if ( !enable == !testWState(WState_MouseTracking) && !gmt ) + return; + uint m = (enable || gmt) ? (uint)PointerMotionMask : 0; + if ( enable ) + setWState( WState_MouseTracking ); + else + clearWState( WState_MouseTracking ); + if ( testWFlags(WType_Desktop) ) { // desktop widget? + QWidget* main_desktop = find( winId() ); + if ( main_desktop->testWFlags(WPaintDesktop) ) + XSelectInput( x11Display(), winId(), + stdDesktopEventMask | ExposureMask ); + else + XSelectInput( x11Display(), winId(), stdDesktopEventMask ); + } else { + XSelectInput( x11Display(), winId(), + m | stdWidgetEventMask ); +#if defined (QT_TABLET_SUPPORT) + if ( devStylus != NULL ) { + XSelectExtensionEvent( x11Display(), winId(), event_list_stylus, + qt_curr_events_stylus ); + } + if ( devEraser != NULL ) { + XSelectExtensionEvent( x11Display(), winId(), event_list_eraser, + qt_curr_events_eraser ); + } +#endif + } +} + + +/*! + Grabs the mouse input. + + This widget receives all mouse events until releaseMouse() is + called; other widgets get no mouse events at all. Keyboard + events are not affected. Use grabKeyboard() if you want to grab + that. + + \warning Bugs in mouse-grabbing applications very often lock the + terminal. Use this function with extreme caution, and consider + using the \c -nograb command line option while debugging. + + It is almost never necessary to grab the mouse when using Qt, as + Qt grabs and releases it sensibly. In particular, Qt grabs the + mouse when a mouse button is pressed and keeps it until the last + button is released. + + Note that only visible widgets can grab mouse input. If + isVisible() returns FALSE for a widget, that widget cannot call + grabMouse(). + + \sa releaseMouse() grabKeyboard() releaseKeyboard() grabKeyboard() + focusWidget() +*/ + +void QWidget::grabMouse() +{ + if ( isVisible() && !qt_nograb() ) { + if ( mouseGrb ) + mouseGrb->releaseMouse(); +#if defined(QT_CHECK_STATE) + int status = +#endif + XGrabPointer( x11Display(), winId(), False, + (uint)( ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | EnterWindowMask | + LeaveWindowMask ), + GrabModeAsync, GrabModeAsync, + None, None, qt_x_time ); +#if defined(QT_CHECK_STATE) + if ( status ) { + const char *s = + status == GrabNotViewable ? "\"GrabNotViewable\"" : + status == AlreadyGrabbed ? "\"AlreadyGrabbed\"" : + status == GrabFrozen ? "\"GrabFrozen\"" : + status == GrabInvalidTime ? "\"GrabInvalidTime\"" : + "<?>"; + qWarning( "Grabbing the mouse failed with %s", s ); + } +#endif + mouseGrb = this; + } +} + +/*! + \overload + + Grabs the mouse input and changes the cursor shape. + + The cursor will assume shape \a cursor (for as long as the mouse + focus is grabbed) and this widget will be the only one to receive + mouse events until releaseMouse() is called(). + + \warning Grabbing the mouse might lock the terminal. + + \sa releaseMouse(), grabKeyboard(), releaseKeyboard(), setCursor() +*/ + +void QWidget::grabMouse( const QCursor &cursor ) +{ + if ( !qt_nograb() ) { + if ( mouseGrb ) + mouseGrb->releaseMouse(); +#if defined(QT_CHECK_STATE) + int status = +#endif + XGrabPointer( x11Display(), winId(), False, + (uint)(ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | EnterWindowMask | LeaveWindowMask), + GrabModeAsync, GrabModeAsync, + None, cursor.handle(), qt_x_time ); +#if defined(QT_CHECK_STATE) + if ( status ) { + const char *s = + status == GrabNotViewable ? "\"GrabNotViewable\"" : + status == AlreadyGrabbed ? "\"AlreadyGrabbed\"" : + status == GrabFrozen ? "\"GrabFrozen\"" : + status == GrabInvalidTime ? "\"GrabInvalidTime\"" : + "<?>"; + qWarning( "Grabbing the mouse failed with %s", s ); + } +#endif + mouseGrb = this; + } +} + +/*! + Releases the mouse grab. + + \sa grabMouse(), grabKeyboard(), releaseKeyboard() +*/ + +void QWidget::releaseMouse() +{ + if ( !qt_nograb() && mouseGrb == this ) { + XUngrabPointer( x11Display(), qt_x_time ); + XFlush( x11Display() ); + mouseGrb = 0; + } +} + +/*! + Grabs the keyboard input. + + This widget reveives all keyboard events until releaseKeyboard() + is called; other widgets get no keyboard events at all. Mouse + events are not affected. Use grabMouse() if you want to grab that. + + The focus widget is not affected, except that it doesn't receive + any keyboard events. setFocus() moves the focus as usual, but the + new focus widget receives keyboard events only after + releaseKeyboard() is called. + + If a different widget is currently grabbing keyboard input, that + widget's grab is released first. + + \sa releaseKeyboard() grabMouse() releaseMouse() focusWidget() +*/ + +void QWidget::grabKeyboard() +{ + if ( !qt_nograb() ) { + if ( keyboardGrb ) + keyboardGrb->releaseKeyboard(); + XGrabKeyboard( x11Display(), winid, False, GrabModeAsync, GrabModeAsync, + qt_x_time ); + keyboardGrb = this; + } +} + +/*! + Releases the keyboard grab. + + \sa grabKeyboard(), grabMouse(), releaseMouse() +*/ + +void QWidget::releaseKeyboard() +{ + if ( !qt_nograb() && keyboardGrb == this ) { + XUngrabKeyboard( x11Display(), qt_x_time ); + keyboardGrb = 0; + } +} + + +/*! + Returns the widget that is currently grabbing the mouse input. + + If no widget in this application is currently grabbing the mouse, + 0 is returned. + + \sa grabMouse(), keyboardGrabber() +*/ + +QWidget *QWidget::mouseGrabber() +{ + return mouseGrb; +} + +/*! + Returns the widget that is currently grabbing the keyboard input. + + If no widget in this application is currently grabbing the + keyboard, 0 is returned. + + \sa grabMouse(), mouseGrabber() +*/ + +QWidget *QWidget::keyboardGrabber() +{ + return keyboardGrb; +} + +/*! + Sets the top-level widget containing this widget to be the active + window. + + An active window is a visible top-level window that has the + keyboard input focus. + + This function performs the same operation as clicking the mouse on + the title bar of a top-level window. On X11, the result depends on + the Window Manager. If you want to ensure that the window is + stacked on top as well you should also call raise(). Note that the + window must be visible, otherwise setActiveWindow() has no effect. + + On Windows, if you are calling this when the application is not + currently the active one then it will not make it the active + window. It will flash the task bar entry blue to indicate that + the window has done something. This is because Microsoft do not + allow an application to interrupt what the user is currently doing + in another application. + + \sa isActiveWindow(), topLevelWidget(), show() +*/ + +void QWidget::setActiveWindow() +{ + QWidget *tlw = topLevelWidget(); + if ( tlw->isVisible() && !tlw->topData()->embedded && !qt_deferred_map_contains(tlw) ) { + XSetInputFocus( x11Display(), tlw->winId(), RevertToNone, qt_x_time); + focusInputContext(); + } +} + + +/*! + Updates the widget unless updates are disabled or the widget is + hidden. + + This function does not cause an immediate repaint; instead it + schedules a paint event for processing when Qt returns to the main + event loop. This permits Qt to optimize for more speed and less + flicker than a call to repaint() does. + + Calling update() several times normally results in just one + paintEvent() call. + + Qt normally erases the widget's area before the paintEvent() call. + If the \c WRepaintNoErase widget flag is set, the widget is + responsible for painting all its pixels itself. + + \sa repaint(), paintEvent(), setUpdatesEnabled(), erase(), + setWFlags() +*/ + +void QWidget::update() +{ + if ( (widget_state & (WState_Visible|WState_BlockUpdates)) == + WState_Visible ) + QApplication::postEvent( this, new QPaintEvent( clipRegion(), !testWFlags(WRepaintNoErase) ) ); +} + +/*! + \overload + + Updates a rectangle (\a x, \a y, \a w, \a h) inside the widget + unless updates are disabled or the widget is hidden. + + This function does not cause an immediate repaint; instead it + schedules a paint event for processing when Qt returns to the main + event loop. This permits Qt to optimize for more speed and less + flicker and a call to repaint() does. + + Calling update() several times normally results in just one + paintEvent() call. + + If \a w is negative, it is replaced with \c{width() - x}. If \a h + is negative, it is replaced width \c{height() - y}. + + Qt normally erases the specified area before the paintEvent() + call. If the \c WRepaintNoErase widget flag is set, the widget is + responsible for painting all its pixels itself. + + \sa repaint(), paintEvent(), setUpdatesEnabled(), erase() +*/ + +void QWidget::update( int x, int y, int w, int h ) +{ + if ( w && h && + (widget_state & (WState_Visible|WState_BlockUpdates)) == WState_Visible ) { + if ( w < 0 ) + w = crect.width() - x; + if ( h < 0 ) + h = crect.height() - y; + if ( w != 0 && h != 0 ) + QApplication::postEvent( this, + new QPaintEvent( clipRegion().intersect(QRect(x,y,w,h)), + !testWFlags( WRepaintNoErase ) ) ); + } +} + +/*! + \overload void QWidget::update( const QRect &r ) + + Updates a rectangle \a r inside the widget unless updates are + disabled or the widget is hidden. + + This function does not cause an immediate repaint; instead it + schedules a paint event for processing when Qt returns to the main + event loop. This permits Qt to optimize for more speed and less + flicker and a call to repaint() does. + + Calling update() several times normally results in just one + paintEvent() call. +*/ + +/*! + \overload void QWidget::repaint( bool erase ) + + This version repaints the entire widget. +*/ + +/*! + \overload void QWidget::repaint() + + This version erases and repaints the entire widget. +*/ + +/*! + Repaints the widget directly by calling paintEvent() immediately, + unless updates are disabled or the widget is hidden. + + If \a erase is TRUE, Qt erases the area \a (x, y, w, h) before the + paintEvent() call. + + If \a w is negative, it is replaced with \c{width() - x}, and if + \a h is negative, it is replaced width \c{height() - y}. + + We suggest only using repaint() if you need an immediate repaint, + for example during animation. In almost all circumstances update() + is better, as it permits Qt to optimize for speed and minimize + flicker. + + \warning If you call repaint() in a function which may itself be + called from paintEvent(), you may get infinite recursion. The + update() function never causes recursion. + + \sa update(), paintEvent(), setUpdatesEnabled(), erase() +*/ + +void QWidget::repaint( int x, int y, int w, int h, bool erase ) +{ + if ( (widget_state & (WState_Visible|WState_BlockUpdates)) == WState_Visible ) { + if ( x > crect.width() || y > crect.height() ) + return; + if ( w < 0 ) + w = crect.width() - x; + if ( h < 0 ) + h = crect.height() - y; + QRect r(x,y,w,h); + if ( r.isEmpty() ) + return; // nothing to do + QPaintEvent e( r, erase ); + if ( r != rect() ) + qt_set_paintevent_clipping( this, r ); + if ( erase && w != 0 && h != 0 ) { + if ( backgroundOrigin() == WidgetOrigin ) + XClearArea( x11Display(), winId(), x, y, w, h, False ); + else + this->erase( x, y, w, h); + } + QApplication::sendEvent( this, &e ); + qt_clear_paintevent_clipping(); + } +} + +/*! + \overload + + Repaints the widget directly by calling paintEvent() directly, + unless updates are disabled or the widget is hidden. + + Erases the widget region \a reg if \a erase is TRUE. + + Only use repaint if your widget needs to be repainted immediately, + for example when doing some animation. In all other cases, use + update(). Calling update() many times in a row will generate a + single paint event. + + \warning If you call repaint() in a function which may itself be + called from paintEvent(), you may get infinite recursion. The + update() function never causes recursion. + + \sa update(), paintEvent(), setUpdatesEnabled(), erase() +*/ + +void QWidget::repaint( const QRegion& reg, bool erase ) +{ + if ( (widget_state & (WState_Visible|WState_BlockUpdates)) == WState_Visible ) { + QPaintEvent e( reg, erase ); + qt_set_paintevent_clipping( this, reg ); + if ( erase ) + this->erase(reg); + QApplication::sendEvent( this, &e ); + qt_clear_paintevent_clipping(); + } +} + +/*! + \overload void QWidget::repaint( const QRect &r, bool erase ) + + Repaints the widget directly by calling paintEvent() directly, + unless updates are disabled or the widget is hidden. + + Erases the widget region \a r if \a erase is TRUE. +*/ + +void QWidget::setWindowState(uint newstate) +{ + bool needShow = FALSE; + uint oldstate = windowState(); + if (isTopLevel()) { + QTLWExtra *top = topData(); + + if ((oldstate & WindowMaximized) != (newstate & WindowMaximized)) { + if (qt_net_supports(qt_net_wm_state_max_h) && qt_net_supports(qt_net_wm_state_max_v)) { + qt_net_change_wm_state(this, (newstate & WindowMaximized), + qt_net_wm_state_max_h, qt_net_wm_state_max_v); + } else if (! (newstate & WindowFullScreen)) { + if (newstate & WindowMaximized) { + // save original geometry + const QRect normalGeometry = geometry(); + + if (isVisible()) { + updateFrameStrut(); + const QRect maxRect = QApplication::desktop()->availableGeometry(this); + const QRect r = top->normalGeometry; + setGeometry(maxRect.x() + top->fleft, + maxRect.y() + top->ftop, + maxRect.width() - top->fleft - top->fright, + maxRect.height() - top->ftop - top->fbottom); + top->normalGeometry = r; + } + + if (top->normalGeometry.width() < 0) + top->normalGeometry = normalGeometry; + } else { + // restore original geometry + setGeometry(top->normalGeometry); + } + } + } + + if ((oldstate & WindowFullScreen) != (newstate & WindowFullScreen)) { + if (qt_net_supports(qt_net_wm_state_fullscreen)) { + qt_net_change_wm_state(this, (newstate & WindowFullScreen), + qt_net_wm_state_fullscreen); + } else { + needShow = isVisible(); + + if (newstate & WindowFullScreen) { + const QRect normalGeometry = QRect(pos(), size()); + + top->savedFlags = getWFlags(); + reparent(0, WType_TopLevel | WStyle_Customize | WStyle_NoBorder | + // preserve some widget flags + (getWFlags() & 0xffff0000), + mapToGlobal(QPoint(0, 0))); + const QRect r = top->normalGeometry; + setGeometry(qApp->desktop()->screenGeometry(this)); + top->normalGeometry = r; + + if ( top->normalGeometry.width() < 0 ) + top->normalGeometry = normalGeometry; + } else { + reparent( 0, top->savedFlags, mapToGlobal(QPoint(0, 0)) ); + + if (newstate & WindowMaximized) { + // from fullscreen to maximized + updateFrameStrut(); + const QRect maxRect = QApplication::desktop()->availableGeometry(this); + const QRect r = top->normalGeometry; + setGeometry(maxRect.x() + top->fleft, + maxRect.y() + top->ftop, + maxRect.width() - top->fleft - top->fright, + maxRect.height() - top->ftop - top->fbottom); + top->normalGeometry = r; + } else { + // restore original geometry + setGeometry(top->normalGeometry); + } + } + } + } + + if ((oldstate & WindowMinimized) != (newstate & WindowMinimized)) { + if (isVisible()) { + if (newstate & WindowMinimized) { + XEvent e; + e.xclient.type = ClientMessage; + e.xclient.message_type = qt_wm_change_state; + e.xclient.display = x11Display(); + e.xclient.window = winid; + e.xclient.format = 32; + e.xclient.data.l[0] = IconicState; + e.xclient.data.l[1] = 0; + e.xclient.data.l[2] = 0; + e.xclient.data.l[3] = 0; + e.xclient.data.l[4] = 0; + XSendEvent(x11Display(), RootWindow(x11Display(), x11Screen()), + False, (SubstructureNotifyMask|SubstructureRedirectMask), &e); + } else { + XMapWindow(x11Display(), winId()); + } + } + + needShow = FALSE; + } + } + + widget_state &= ~(WState_Minimized | WState_Maximized | WState_FullScreen); + if (newstate & WindowMinimized) + widget_state |= WState_Minimized; + if (newstate & WindowMaximized) + widget_state |= WState_Maximized; + if (newstate & WindowFullScreen) + widget_state |= WState_FullScreen; + + if (needShow) + show(); + + if (newstate & WindowActive) + setActiveWindow(); + + QEvent e(QEvent::WindowStateChange); + QApplication::sendEvent(this, &e); +} + +/*! + \internal + Platform-specific part of QWidget::show(). +*/ + +void QWidget::showWindow() +{ + if ( isTopLevel() ) { + XWMHints *h = XGetWMHints( x11Display(), winId() ); + XWMHints wm_hints; + bool got_hints = h != 0; + if ( !got_hints ) { + h = &wm_hints; + h->flags = 0; + } + h->initial_state = testWState(WState_Minimized) ? IconicState : NormalState; + h->flags |= StateHint; + XSetWMHints( x11Display(), winId(), h ); + if ( got_hints ) + XFree( (char *)h ); + + if (qt_x_user_time != CurrentTime) { + XChangeProperty(x11Display(), winId(), qt_net_wm_user_time, XA_CARDINAL, + 32, PropModeReplace, (unsigned char *) &qt_x_user_time, 1); + } + + if (!topData()->embedded && + topData()->parentWinId && + topData()->parentWinId != QPaintDevice::x11AppRootWindow(x11Screen()) && + !isMinimized() ) { + qt_deferred_map_add( this ); + return; + } + + if (isMaximized() && !isFullScreen() + && !(qt_net_supports(qt_net_wm_state_max_h) + && qt_net_supports(qt_net_wm_state_max_v))) { + XMapWindow( x11Display(), winId() ); + qt_wait_for_window_manager(this); + + // if the wm was not smart enough to adjust our size, do that manually + updateFrameStrut(); + QRect maxRect = QApplication::desktop()->availableGeometry(this); + + QTLWExtra *top = topData(); + QRect normalRect = top->normalGeometry; + + setGeometry(maxRect.x() + top->fleft, + maxRect.y() + top->ftop, + maxRect.width() - top->fleft - top->fright, + maxRect.height() - top->ftop - top->fbottom); + + // restore the original normalGeometry + top->normalGeometry = normalRect; + // internalSetGeometry() clears the maximized flag... make sure we set it back + setWState(WState_Maximized); + + return; + } + + if (isFullScreen() && !qt_net_supports(qt_net_wm_state_fullscreen)) { + XMapWindow(x11Display(), winId()); + qt_wait_for_window_manager(this); + return; + } + } + XMapWindow( x11Display(), winId() ); +} + + +/*! + \internal + Platform-specific part of QWidget::hide(). +*/ + +void QWidget::hideWindow() +{ + clearWState( WState_Exposed ); + deactivateWidgetCleanup(); + if ( isTopLevel() ) { + qt_deferred_map_take( this ); + if ( winId() ) // in nsplugin, may be 0 + XWithdrawWindow( x11Display(), winId(), x11Screen() ); + + QTLWExtra *top = topData(); + crect.moveTopLeft( QPoint(crect.x() - top->fleft, crect.y() - top->ftop ) ); + + // zero the frame strut and mark it dirty + top->fleft = top->fright = top->ftop = top->fbottom = 0; + fstrut_dirty = TRUE; + + XFlush( x11Display() ); + } else { + if ( winId() ) // in nsplugin, may be 0 + XUnmapWindow( x11Display(), winId() ); + } +} + +/*! + Raises this widget to the top of the parent widget's stack. + + After this call the widget will be visually in front of any + overlapping sibling widgets. + + \sa lower(), stackUnder() +*/ + +void QWidget::raise() +{ + QWidget *p = parentWidget(); + if ( p && p->childObjects && p->childObjects->findRef(this) >= 0 ) + p->childObjects->append( p->childObjects->take() ); + XRaiseWindow( x11Display(), winId() ); +} + +/*! + Lowers the widget to the bottom of the parent widget's stack. + + After this call the widget will be visually behind (and therefore + obscured by) any overlapping sibling widgets. + + \sa raise(), stackUnder() +*/ + +void QWidget::lower() +{ + QWidget *p = parentWidget(); + if ( p && p->childObjects && p->childObjects->findRef(this) >= 0 ) + p->childObjects->insert( 0, p->childObjects->take() ); + XLowerWindow( x11Display(), winId() ); +} + + +/*! + Places the widget under \a w in the parent widget's stack. + + To make this work, the widget itself and \a w must be siblings. + + \sa raise(), lower() +*/ +void QWidget::stackUnder( QWidget* w) +{ + QWidget *p = parentWidget(); + if ( !w || isTopLevel() || p != w->parentWidget() || this == w ) + return; + if ( p && p->childObjects && p->childObjects->findRef(w) >= 0 && p->childObjects->findRef(this) >= 0 ) { + p->childObjects->take(); + p->childObjects->insert( p->childObjects->findRef(w), this ); + } + Window stack[2]; + stack[0] = w->winId();; + stack[1] = winId(); + XRestackWindows( x11Display(), stack, 2 ); +} + + + +/* + The global variable qt_widget_tlw_gravity defines the window gravity of + the next top level window to be created. We do this when setting the + main widget's geometry and the "-geometry" command line option contains + a negative position. +*/ + +int qt_widget_tlw_gravity = NorthWestGravity; + +static void do_size_hints( QWidget* widget, QWExtra *x ) +{ + XSizeHints s; + s.flags = 0; + if ( x ) { + s.x = widget->x(); + s.y = widget->y(); + s.width = widget->width(); + s.height = widget->height(); + if ( x->minw > 0 || x->minh > 0 ) { // add minimum size hints + s.flags |= PMinSize; + s.min_width = x->minw; + s.min_height = x->minh; + } + if ( x->maxw < QWIDGETSIZE_MAX || x->maxh < QWIDGETSIZE_MAX ) { + s.flags |= PMaxSize; // add maximum size hints + s.max_width = x->maxw; + s.max_height = x->maxh; + } + if ( x->topextra && + (x->topextra->incw > 0 || x->topextra->inch > 0) ) + { // add resize increment hints + s.flags |= PResizeInc | PBaseSize; + s.width_inc = x->topextra->incw; + s.height_inc = x->topextra->inch; + s.base_width = x->topextra->basew; + s.base_height = x->topextra->baseh; + } + + if ( x->topextra && x->topextra->uspos) { + s.flags |= USPosition; + s.flags |= PPosition; + } + if ( x->topextra && x->topextra->ussize) { + s.flags |= USSize; + s.flags |= PSize; + } + } + s.flags |= PWinGravity; + s.win_gravity = qt_widget_tlw_gravity; // usually NorthWest + // reset in case it was set + qt_widget_tlw_gravity = + QApplication::reverseLayout() ? NorthEastGravity : NorthWestGravity; + XSetWMNormalHints( widget->x11Display(), widget->winId(), &s ); +} + + +void QWidget::internalSetGeometry( int x, int y, int w, int h, bool isMove ) +{ + Display *dpy = x11Display(); + + if ( testWFlags(WType_Desktop) ) + return; + if (isTopLevel()) { + if (!qt_net_supports(qt_net_wm_state_max_h) + && !qt_net_supports(qt_net_wm_state_max_v)) + clearWState(WState_Maximized); + if (!qt_net_supports(qt_net_wm_state_fullscreen)) + clearWState(WState_FullScreen); + topData()->normalGeometry = QRect(0, 0, -1, -1); + } else { + // for QWorkspace + clearWState(WState_Maximized); + clearWState(WState_FullScreen); + } + if ( extra ) { // any size restrictions? + w = QMIN(w,extra->maxw); + h = QMIN(h,extra->maxh); + w = QMAX(w,extra->minw); + h = QMAX(h,extra->minh); + } + if ( w < 1 ) // invalid size + w = 1; + if ( h < 1 ) + h = 1; + QPoint oldPos( pos() ); + QSize oldSize( size() ); + QRect oldGeom( crect ); + QRect r( x, y, w, h ); + + // We only care about stuff that changes the geometry, or may + // cause the window manager to change its state + if ( !isTopLevel() && oldGeom == r ) + return; + + crect = r; + bool isResize = size() != oldSize; + + if ( isTopLevel() ) { + if ( isMove ) + topData()->uspos = 1; + if ( isResize ) + topData()->ussize = 1; + do_size_hints( this, extra ); + } + + if ( isMove ) { + if (! qt_broken_wm) + // pos() is right according to ICCCM 4.1.5 + XMoveResizeWindow( dpy, winid, pos().x(), pos().y(), w, h ); + else + // work around 4Dwm's incompliance with ICCCM 4.1.5 + XMoveResizeWindow( dpy, winid, x, y, w, h ); + } else if ( isResize ) + XResizeWindow( dpy, winid, w, h ); + + if ( isVisible() ) { + if ( isMove && pos() != oldPos ) { + if ( ! qt_broken_wm ) { + // pos() is right according to ICCCM 4.1.5 + QMoveEvent e( pos(), oldPos ); + QApplication::sendEvent( this, &e ); + } else { + // work around 4Dwm's incompliance with ICCCM 4.1.5 + QMoveEvent e( crect.topLeft(), oldGeom.topLeft() ); + QApplication::sendEvent( this, &e ); + } + } + if ( isResize ) { + + // set config pending only on resize, see qapplication_x11.cpp, translateConfigEvent() + setWState( WState_ConfigPending ); + + QResizeEvent e( size(), oldSize ); + QApplication::sendEvent( this, &e ); + } + } else { + if ( isMove && pos() != oldPos ) { + if ( ! qt_broken_wm ) + // pos() is right according to ICCCM 4.1.5 + QApplication::postEvent( this, new QMoveEvent( pos(), oldPos ) ); + else + // work around 4Dwm's incompliance with ICCCM 4.1.5 + QApplication::postEvent( this, new QMoveEvent( crect.topLeft(), + oldGeom.topLeft() ) ); + } + if ( isResize ) + QApplication::postEvent( this, + new QResizeEvent( size(), oldSize ) ); + } +} + + +/*! + \overload + + This function corresponds to setMinimumSize( QSize(minw, minh) ). + Sets the minimum width to \a minw and the minimum height to \a + minh. +*/ + +void QWidget::setMinimumSize( int minw, int minh ) +{ +#if defined(QT_CHECK_RANGE) + if ( minw < 0 || minh < 0 ) + qWarning("QWidget::setMinimumSize: The smallest allowed size is (0,0)"); +#endif + createExtra(); + if ( extra->minw == minw && extra->minh == minh ) + return; + extra->minw = minw; + extra->minh = minh; + if ( minw > width() || minh > height() ) { + bool resized = testWState( WState_Resized ); + resize( QMAX(minw,width()), QMAX(minh,height()) ); + if ( !resized ) + clearWState( WState_Resized ); // not a user resize + } + if ( testWFlags(WType_TopLevel) ) + do_size_hints( this, extra ); + updateGeometry(); +} + +/*! + \overload + + This function corresponds to setMaximumSize( QSize(\a maxw, \a + maxh) ). Sets the maximum width to \a maxw and the maximum height + to \a maxh. +*/ +void QWidget::setMaximumSize( int maxw, int maxh ) +{ +#if defined(QT_CHECK_RANGE) + if ( maxw > QWIDGETSIZE_MAX || maxh > QWIDGETSIZE_MAX ) { + qWarning("QWidget::setMaximumSize: (%s/%s) " + "The largest allowed size is (%d,%d)", + name( "unnamed" ), className(), QWIDGETSIZE_MAX, + QWIDGETSIZE_MAX ); + maxw = QMIN( maxw, QWIDGETSIZE_MAX ); + maxh = QMIN( maxh, QWIDGETSIZE_MAX ); + } + if ( maxw < 0 || maxh < 0 ) { + qWarning("QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) " + "are not possible", + name( "unnamed" ), className(), maxw, maxh ); + maxw = QMAX( maxw, 0 ); + maxh = QMAX( maxh, 0 ); + } +#endif + createExtra(); + if ( extra->maxw == maxw && extra->maxh == maxh ) + return; + extra->maxw = maxw; + extra->maxh = maxh; + if ( maxw < width() || maxh < height() ) { + bool resized = testWState( WState_Resized ); + resize( QMIN(maxw,width()), QMIN(maxh,height()) ); + if ( !resized ) + clearWState( WState_Resized ); // not a user resize + } + if ( testWFlags(WType_TopLevel) ) + do_size_hints( this, extra ); + updateGeometry(); +} + +/*! + \overload + + Sets the x (width) size increment to \a w and the y (height) size + increment to \a h. +*/ +void QWidget::setSizeIncrement( int w, int h ) +{ + QTLWExtra* x = topData(); + if ( x->incw == w && x->inch == h ) + return; + x->incw = w; + x->inch = h; + if ( testWFlags(WType_TopLevel) ) + do_size_hints( this, extra ); +} + +/*! + \overload + + This corresponds to setBaseSize( QSize(\a basew, \a baseh) ). Sets + the widgets base size to width \a basew and height \a baseh. +*/ +void QWidget::setBaseSize( int basew, int baseh ) +{ + createTLExtra(); + QTLWExtra* x = topData(); + if ( x->basew == basew && x->baseh == baseh ) + return; + x->basew = basew; + x->baseh = baseh; + if ( testWFlags(WType_TopLevel) ) + do_size_hints( this, extra ); +} + +/*! + \overload void QWidget::erase() + + This version erases the entire widget. +*/ + +/*! + \overload void QWidget::erase( const QRect &r ) + + Erases the specified area \a r in the widget without generating a + \link paintEvent() paint event\endlink. +*/ + +/*! + Erases the specified area \a (x, y, w, h) in the widget without + generating a \link paintEvent() paint event\endlink. + + If \a w is negative, it is replaced with \c{width() - x}. If \a h + is negative, it is replaced width \c{height() - y}. + + Child widgets are not affected. + + \sa repaint() +*/ + +void QWidget::erase( int x, int y, int w, int h ) +{ + extern void qt_erase_rect( QWidget*, const QRect& ); // in qpainer_x11.cpp + if ( w < 0 ) + w = crect.width() - x; + if ( h < 0 ) + h = crect.height() - y; + if ( w != 0 && h != 0 ) + qt_erase_rect( this, QRect(x, y, w, h ) ); +} + +/*! + \overload + + Erases the area defined by \a reg, without generating a \link + paintEvent() paint event\endlink. + + Child widgets are not affected. +*/ + +void QWidget::erase( const QRegion& reg ) +{ + extern void qt_erase_region( QWidget*, const QRegion& ); // in qpainer_x11.cpp + qt_erase_region( this, reg ); +} + +/*! + Scrolls the widget including its children \a dx pixels to the + right and \a dy downwards. Both \a dx and \a dy may be negative. + + After scrolling, scroll() sends a paint event for the the part + that is read but not written. For example, when scrolling 10 + pixels rightwards, the leftmost ten pixels of the widget need + repainting. The paint event may be delivered immediately or later, + depending on some heuristics (note that you might have to force + processing of paint events using QApplication::sendPostedEvents() + when using scroll() and move() in combination). + + \sa QScrollView erase() bitBlt() +*/ + +void QWidget::scroll( int dx, int dy ) +{ + scroll( dx, dy, QRect() ); +} + +/*! + \overload + + This version only scrolls \a r and does not move the children of + the widget. + + If \a r is empty or invalid, the result is undefined. + + \sa QScrollView erase() bitBlt() +*/ +void QWidget::scroll( int dx, int dy, const QRect& r ) +{ + if ( testWState( WState_BlockUpdates ) && !children() ) + return; + bool valid_rect = r.isValid(); + bool just_update = QABS( dx ) > width() || QABS( dy ) > height(); + if ( just_update ) + update(); + QRect sr = valid_rect?r:clipRegion().boundingRect(); + int x1, y1, x2, y2, w=sr.width(), h=sr.height(); + if ( dx > 0 ) { + x1 = sr.x(); + x2 = x1+dx; + w -= dx; + } else { + x2 = sr.x(); + x1 = x2-dx; + w += dx; + } + if ( dy > 0 ) { + y1 = sr.y(); + y2 = y1+dy; + h -= dy; + } else { + y2 = sr.y(); + y1 = y2-dy; + h += dy; + } + + if ( dx == 0 && dy == 0 ) + return; + + Display *dpy = x11Display(); + GC gc = qt_xget_readonly_gc( x11Screen(), FALSE ); + // Want expose events + if ( w > 0 && h > 0 && !just_update ) { + XSetGraphicsExposures( dpy, gc, True ); + XCopyArea( dpy, winId(), winId(), gc, x1, y1, w, h, x2, y2); + XSetGraphicsExposures( dpy, gc, False ); + } + + if ( !valid_rect && children() ) { // scroll children + QPoint pd( dx, dy ); + QObjectListIt it(*children()); + register QObject *object; + while ( it ) { // move all children + object = it.current(); + if ( object->isWidgetType() ) { + QWidget *w = (QWidget *)object; + w->move( w->pos() + pd ); + } + ++it; + } + } + + if ( just_update ) + return; + + // Don't let the server be bogged-down with repaint events + bool repaint_immediately = qt_sip_count( this ) < 3; + + if ( dx ) { + int x = x2 == sr.x() ? sr.x()+w : sr.x(); + if ( repaint_immediately ) + repaint( x, sr.y(), QABS(dx), sr.height(), !testWFlags(WRepaintNoErase) ); + else + XClearArea( dpy, winid, x, sr.y(), QABS(dx), sr.height(), True ); + } + if ( dy ) { + int y = y2 == sr.y() ? sr.y()+h : sr.y(); + if ( repaint_immediately ) + repaint( sr.x(), y, sr.width(), QABS(dy), !testWFlags(WRepaintNoErase) ); + else + XClearArea( dpy, winid, sr.x(), y, sr.width(), QABS(dy), True ); + } + + qt_insert_sip( this, dx, dy ); // #### ignores r +} + + +/*! + \overload void QWidget::drawText( const QPoint &pos, const QString& str ) + + Draws the string \a str at position \a pos. +*/ + +/*! + Draws the string \a str at position \a(x, y). + + The \a y position is the base line position of the text. The text + is drawn using the default font and the default foreground color. + + This function is provided for convenience. You will generally get + more flexible results and often higher speed by using a a \link + QPainter painter\endlink instead. + + \sa setFont(), foregroundColor(), QPainter::drawText() +*/ + +void QWidget::drawText( int x, int y, const QString &str ) +{ + if ( testWState(WState_Visible) ) { + QPainter paint; + paint.begin( this ); + paint.drawText( x, y, str ); + paint.end(); + } +} + + +/*! + Internal implementation of the virtual QPaintDevice::metric() + function. + + Use the QPaintDeviceMetrics class instead. + + \a m is the metric to get. +*/ + +int QWidget::metric( int m ) const +{ + int val; + if ( m == QPaintDeviceMetrics::PdmWidth ) { + val = crect.width(); + } else if ( m == QPaintDeviceMetrics::PdmHeight ) { + val = crect.height(); + } else { + Display *dpy = x11Display(); + int scr = x11Screen(); + switch ( m ) { + case QPaintDeviceMetrics::PdmDpiX: + case QPaintDeviceMetrics::PdmPhysicalDpiX: + val = QPaintDevice::x11AppDpiX( scr ); + break; + case QPaintDeviceMetrics::PdmDpiY: + case QPaintDeviceMetrics::PdmPhysicalDpiY: + val = QPaintDevice::x11AppDpiY( scr ); + break; + case QPaintDeviceMetrics::PdmWidthMM: + val = (DisplayWidthMM(dpy,scr)*crect.width())/ + DisplayWidth(dpy,scr); + break; + case QPaintDeviceMetrics::PdmHeightMM: + val = (DisplayHeightMM(dpy,scr)*crect.height())/ + DisplayHeight(dpy,scr); + break; + case QPaintDeviceMetrics::PdmNumColors: + val = x11Cells(); + break; + case QPaintDeviceMetrics::PdmDepth: + val = x11Depth(); + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "QWidget::metric: Invalid metric command" ); +#endif + } + } + return val; +} + +void QWidget::createSysExtra() +{ + extra->xDndProxy = 0; + extra->children_use_dnd = FALSE; + extra->compress_events = TRUE; +} + +void QWidget::deleteSysExtra() +{ +} + +void QWidget::createTLSysExtra() +{ +#if defined(QT_NO_IM_EXTENSIONS) + // created lazily + extra->topextra->xic = 0; +#endif +#ifndef QT_NO_XSYNC + extra->topextra->syncCounter = 0; + extra->topextra->syncRequestValue[0] = 0; + extra->topextra->syncRequestValue[1] = 0; +#endif +} + +void QWidget::deleteTLSysExtra() +{ + // don't destroy input context here. it will be destroyed in + // QWidget::destroy() destroyInputContext(); +} + +/* + examine the children of our parent up the tree and set the + children_use_dnd extra data appropriately... this is used to keep DND enabled + for widgets that are reparented and don't have DND enabled, BUT *DO* have + children (or children of children ...) with DND enabled... +*/ +void QWidget::checkChildrenDnd() +{ + QWidget *widget = this; + const QObjectList *children; + const QObject *object; + const QWidget *child; + while (widget && ! widget->isDesktop()) { + // note: this isn't done for the desktop widget + + bool children_use_dnd = FALSE; + children = widget->children(); + if ( children ) { + QObjectListIt it(*children); + while ( (object = it.current()) ) { + ++it; + if ( object->isWidgetType() ) { + child = (const QWidget *) object; + children_use_dnd = (children_use_dnd || + child->acceptDrops() || + (child->extra && + child->extra->children_use_dnd)); + } + } + } + + widget->createExtra(); + widget->extra->children_use_dnd = children_use_dnd; + + widget = widget->parentWidget(); + } +} + + +#ifndef QT_NO_XSYNC +// create a window's XSyncCounter +void QWidget::createSyncCounter() +{ + if( !qt_use_xsync || !isTopLevel() || topData()->syncCounter ) + return; + XSyncValue zero; + XSyncIntToValue( &zero, 0 ); + topData()->syncCounter = XSyncCreateCounter( x11Display(), zero ); +} + +// destroy a window's XSyncCounter +void QWidget::destroySyncCounter() +{ + if( !qt_use_xsync || !extra || !extra->topextra + || !extra->topextra->syncCounter ) + return; + XSyncDestroyCounter( x11Display(), extra->topextra->syncCounter ); + extra->topextra->syncCounter = 0; +} + +// increment a window's XSyncCounter +void QWidget::incrementSyncCounter() +{ + if( qt_use_xsync && topData()->syncCounter && + !(topData()->syncRequestValue[0] == 0 && + topData()->syncRequestValue[1] == 0) ) { + XSyncValue val; + XSyncIntsToValue( &val, topData()->syncRequestValue[ 0 ], topData()->syncRequestValue[ 1 ] ); + XSyncSetCounter( x11Display(), topData()->syncCounter, val ); + topData()->syncRequestValue[0] = topData()->syncRequestValue[1] = 0; + } +} + +// handle _NET_WM_SYNC_REQUEST +void QWidget::handleSyncRequest( void* ev ) +{ + XEvent* xev = (XEvent*)ev; + topData()->syncRequestValue[ 0 ] = xev->xclient.data.l[ 2 ]; + topData()->syncRequestValue[ 1 ] = xev->xclient.data.l[ 3 ]; +} +#endif // QT_NO_XSYNC + + +/*! + \property QWidget::acceptDrops + \brief whether drop events are enabled for this widget + + Setting this property to TRUE announces to the system that this + widget \e may be able to accept drop events. + + If the widget is the desktop (QWidget::isDesktop()), this may + fail if another application is using the desktop; you can call + acceptDrops() to test if this occurs. + + \warning + Do not modify this property in a Drag&Drop event handler. +*/ +bool QWidget::acceptDrops() const +{ + return testWState( WState_DND ); +} + +void QWidget::setAcceptDrops( bool on ) +{ + if ( testWState(WState_DND) != on ) { + if ( qt_dnd_enable( this, on ) ) { + if ( on ) + setWState( WState_DND ); + else + clearWState( WState_DND ); + } + + checkChildrenDnd(); + } +} + +/*! + \overload + + Causes only the parts of the widget which overlap \a region to be + visible. If the region includes pixels outside the rect() of the + widget, window system controls in that area may or may not be + visible, depending on the platform. + + Note that this effect can be slow if the region is particularly + complex. + + \sa setMask(), clearMask() +*/ + +void QWidget::setMask( const QRegion& region ) +{ + XShapeCombineRegion( x11Display(), winId(), ShapeBounding, 0, 0, + region.handle(), ShapeSet ); +} + +/*! + Causes only the pixels of the widget for which \a bitmap has a + corresponding 1 bit to be visible. Use Qt::color0 to draw + transparent regions and Qt::color1 to draw opaque regions of the + bitmap. + + If the region includes pixels outside the rect() of the widget, + window system controls in that area may or may not be visible, + depending on the platform. + + Note that this effect can be slow if the region is particularly + complex. + + See \c examples/tux for an example of masking for transparency. + + \sa setMask(), clearMask() +*/ + +void QWidget::setMask( const QBitmap &bitmap ) +{ + QBitmap bm = bitmap; + if ( bm.x11Screen() != x11Screen() ) + bm.x11SetScreen( x11Screen() ); + XShapeCombineMask( x11Display(), winId(), ShapeBounding, 0, 0, + bm.handle(), ShapeSet ); +} + +/*! + Removes any mask set by setMask(). + + \sa setMask() +*/ + +void QWidget::clearMask() +{ + XShapeCombineMask( x11Display(), winId(), ShapeBounding, 0, 0, + None, ShapeSet ); +} + +/*!\reimp + */ +void QWidget::setName( const char *name ) +{ + QObject::setName( name ); + if ( isTopLevel() ) { + XChangeProperty( x11Display(), winId(), + qt_window_role, XA_STRING, 8, PropModeReplace, + (unsigned char *)name, qstrlen( name ) ); + } +} + + +/*! + \internal + + Computes the frame rectangle when needed. This is an internal function, you + should never call this. +*/ + +void QWidget::updateFrameStrut() const +{ + QWidget *that = (QWidget *) this; + + if (! isVisible() || isDesktop()) { + that->fstrut_dirty = (! isVisible()); + return; + } + + Atom type_ret; + Window l = winId(), w = winId(), p, r; // target window, it's parent, root + Window *c; + int i_unused; + unsigned int nc; + unsigned char *data_ret; + unsigned long l_unused; + + while (XQueryTree(QPaintDevice::x11AppDisplay(), w, &r, &p, &c, &nc)) { + if (c && nc > 0) + XFree(c); + + if (! p) { + qWarning("QWidget::updateFrameStrut(): ERROR - no parent"); + return; + } + + // if the parent window is the root window, an Enlightenment virtual root or + // a NET WM virtual root window, stop here + data_ret = 0; + if (p == r || + (XGetWindowProperty(QPaintDevice::x11AppDisplay(), p, + qt_enlightenment_desktop, 0, 1, False, XA_CARDINAL, + &type_ret, &i_unused, &l_unused, &l_unused, + &data_ret) == Success && + type_ret == XA_CARDINAL)) { + if (data_ret) + XFree(data_ret); + + break; + } else if (qt_net_supports(qt_net_virtual_roots) && qt_net_virtual_root_list) { + int i = 0; + while (qt_net_virtual_root_list[i] != 0) { + if (qt_net_virtual_root_list[i++] == p) + break; + } + } + + l = w; + w = p; + } + + // we have our window + int transx, transy; + XWindowAttributes wattr; + if (XTranslateCoordinates(QPaintDevice::x11AppDisplay(), l, w, + 0, 0, &transx, &transy, &p) && + XGetWindowAttributes(QPaintDevice::x11AppDisplay(), w, &wattr)) { + QTLWExtra *top = that->topData(); + top->fleft = transx; + top->ftop = transy; + top->fright = wattr.width - crect.width() - top->fleft; + top->fbottom = wattr.height - crect.height() - top->ftop; + + // add the border_width for the window managers frame... some window managers + // do not use a border_width of zero for their frames, and if we the left and + // top strut, we ensure that pos() is absolutely correct. frameGeometry() + // will still be incorrect though... perhaps i should have foffset as well, to + // indicate the frame offset (equal to the border_width on X). + // - Brad + top->fleft += wattr.border_width; + top->fright += wattr.border_width; + top->ftop += wattr.border_width; + top->fbottom += wattr.border_width; + } + + that->fstrut_dirty = 0; +} + + +/*! + This function returns the widget holding the QInputContext + instance for this widget. The instance is used for text input to + this widget, switching input method, etc. + + By default, this function delegates the role of returning input + context holder widget to QApplication::locateICHolderWidget(). + + This definition enables application developer to change the + mapping of widgets to QInputContext instance simply by overriding + QApplication::locateICHolderWidget(). + + \sa QApplication::locateICHolderWidget() +*/ +QWidget *QWidget::icHolderWidget() +{ + return qApp->locateICHolderWidget(this); +} + + +/*! + This function returns the QInputContext instance for this widget. + This instance is used for text input to this widget, etc. + It is simply the accessor function. +*/ +QInputContext *QWidget::getInputContext() +{ + QInputContext *qic = 0; + +// #if !defined(QT_NO_IM_EXTENSIONS) + if ( isInputMethodEnabled() ) { +#if !defined(QT_NO_IM_EXTENSIONS) + qic = icHolderWidget()->ic; +#else +// { + // icHolderWidget is always topLevelWidget + QTLWExtra *topdata = icHolderWidget()->topData(); + qic = (QInputContext *)topdata->xic; +#endif + } + + return qic; +} + + +/*! + This function replaces the QInputContext instance used for text + input to this widget. The \a identifierName is the identifier name + of newly choosed input method. +*/ +void QWidget::changeInputContext( const QString& identifierName ) +{ + QWidget *icWidget = icHolderWidget(); +#if !defined(QT_NO_IM_EXTENSIONS) + QInputContext **qicp = &icWidget->ic; +#else + QInputContext **qicp = (QInputContext **)&icWidget->topData()->xic; +#endif + + if( *qicp ) + delete *qicp; + // an input context that has the identifierName is generated. + QInputContext *qic = QInputContextFactory::create( identifierName, icWidget ); + *qicp = qic; + if ( qic ) { + QObject::connect( qic, SIGNAL(imEventGenerated(QObject *,QIMEvent *)), + qApp, SLOT(postIMEvent(QObject *,QIMEvent *)) ); + QObject::connect( qic, SIGNAL(deletionRequested()), + icWidget, SLOT(destroyInputContext()) ); + } +} + + +/*! + \internal + This is an internal function, you should never call this. + + This function is called to generate an input context + according to a configuration for default input method + + When QT_NO_IM_EXTENSIONS is not set, input context is + generated only when isInputMethodEnabled() returns TRUE. +*/ +void QWidget::createInputContext() +{ +// #if !defined(QT_NO_IM_EXTENSIONS) + if( !isInputMethodEnabled() || QApplication::closingDown() ) + return; +// #endif + + QWidget *icWidget = icHolderWidget(); +#ifndef QT_NO_IM +#if !defined(QT_NO_IM_EXTENSIONS) + QInputContext **qicp = &icWidget->ic; +#else + QInputContext **qicp = (QInputContext **)&icWidget->topData()->xic; +#endif + + if ( ! *qicp ) { + // an input context of the default input method is generated. + QInputContext *qic = QInputContextFactory::create( QApplication::defaultInputMethod(), icWidget ); + + *qicp = qic; + if ( qic ) { + QObject::connect( qic, SIGNAL(imEventGenerated(QObject *,QIMEvent *)), + qApp, SLOT(postIMEvent(QObject *,QIMEvent *)) ); + QObject::connect( qic, SIGNAL(deletionRequested()), + icWidget, SLOT(destroyInputContext()) ); + } + } +#endif // QT_NO_IM +} + + +/*! + \internal + + This slot is used to destroy the input context that belonging + to the widget itself, so icHolderWidget()->ic is not fetched. + + \sa QInputContext::deletionRequested() +*/ +void QWidget::destroyInputContext() +{ +#ifndef QT_NO_IM +#if !defined(QT_NO_IM_EXTENSIONS) + QInputContext **qicp = ⁣ +#else + if ( ! extra || ! extra->topextra ) + return; + + QInputContext **qicp = (QInputContext **)&extra->topextra->xic; +#endif + + if( *qicp ) + delete *qicp; + + *qicp = 0; +#endif // QT_NO_IM +} + + +/*! + This function is called when text widgets need to be neutral state to + execute text operations properly. See qlineedit.cpp and qtextedit.cpp as + example. + + Ordinary reset that along with changing focus to another widget, + moving the cursor, etc, is implicitly handled via + unfocusInputContext() because whether reset or not when such + situation is a responsibility of input methods. So we delegate the + responsibility to the input context via unfocusInputContext(). See + 'Preedit preservation' section of the class description of + QInputContext for further information. + + \sa QInputContext, unfocusInputContext(), QInputContext::unsetFocus() +*/ +void QWidget::resetInputContext() +{ +#ifndef QT_NO_IM + // trigger input context creation if it hasn't happened already + createInputContext(); + + QInputContext *qic = getInputContext(); + if( qic ) + qic->reset(); +#endif // QT_NO_IM +} + + +/*! + \internal + This is an internal function, you should never call this. + + This function is called to focus associated input context. The + code intends to eliminate duplicate focus for the context even if + the context is shared between widgets + + \sa QInputContext::setFocus() + */ +void QWidget::focusInputContext() +{ +#ifndef QT_NO_IM + QWidget* tlw = topLevelWidget(); + + if (!tlw->isPopup() || isInputMethodEnabled()) { + // trigger input context creation if it hasn't happened already + createInputContext(); + + QInputContext *qic = getInputContext(); + if ( qic ) { + if( qic->focusWidget() != this ) { + qic->setFocusWidget( this ); + qic->setFocus(); + } + } + } +#endif // QT_NO_IM +} + + +/*! + \internal + This is an internal function, you should never call this. + + This function is called to remove focus from associated input + context. + + \sa QInputContext::unsetFocus() + */ +void QWidget::unfocusInputContext() +{ +#ifndef QT_NO_IM + // trigger input context creation if it hasn't happened already + createInputContext(); + + QInputContext *qic = getInputContext(); + if ( qic ) { + // may be caused reset() in some input methods + qic->unsetFocus(); + qic->setFocusWidget( 0 ); + } +#endif // QT_NO_IM +} + + +/*! + This function is called to send mouse event to associated input + context by derived text widgets. A derived text widget must be + calculate \a x as character offset at the mouse cursor in the + preedit. + + \sa QInputContext::mouseHandler() + */ +void QWidget::sendMouseEventToInputContext( int x, QEvent::Type type, + Qt::ButtonState button, + Qt::ButtonState state ) +{ +#ifndef QT_NO_IM + // trigger input context creation if it hasn't happened already + createInputContext(); + + QInputContext *qic = getInputContext(); + if ( qic ) { + // may be causing reset() in some input methods + qic->mouseHandler( x, type, button, state ); + } +#endif // QT_NO_IM +} + + +void QWidget::setWindowOpacity(double) +{ +} + +double QWidget::windowOpacity() const +{ + return 1.0; +} diff --git a/src/kernel/qwidgetcreate_x11.cpp b/src/kernel/qwidgetcreate_x11.cpp new file mode 100644 index 0000000..4e922fc --- /dev/null +++ b/src/kernel/qwidgetcreate_x11.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Implementation of Qt calls to X11 +** +** Created : 970529 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qwidget.h" +#include "qt_x11_p.h" + + +/* + Internal Qt functions to create X windows. We have put them in + separate functions to allow the programmer to reimplement them by + custom versions. +*/ + +Window qt_XCreateWindow( const QWidget*, Display *display, Window parent, + int x, int y, uint w, uint h, + int borderwidth, int depth, + uint windowclass, Visual *visual, + ulong valuemask, XSetWindowAttributes *attributes ) +{ + return XCreateWindow( display, parent, x, y, w, h, borderwidth, depth, + windowclass, visual, valuemask, attributes ); +} + + +Window qt_XCreateSimpleWindow( const QWidget*, Display *display, Window parent, + int x, int y, uint w, uint h, int borderwidth, + ulong border, ulong background ) +{ + return XCreateSimpleWindow( display, parent, x, y, w, h, borderwidth, + border, background ); +} + + +void qt_XDestroyWindow( const QWidget*, Display *display, Window window ) +{ + XDestroyWindow( display, window ); +} diff --git a/src/kernel/qwidgetintdict.h b/src/kernel/qwidgetintdict.h new file mode 100644 index 0000000..2c30c90 --- /dev/null +++ b/src/kernel/qwidgetintdict.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Definition of QWidgetIntDict +** +** Created : 950116 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWIDGETINTDICT_H +#define QWIDGETINTDICT_H + +#ifndef QT_H +#include "qwidget.h" +#include "qintdict.h" +#endif // QT_H + + +#if defined(Q_TEMPLATEDLL) +//Q_TEMPLATE_EXTERN template class Q_EXPORT QIntDict<QWidget>; +//Q_TEMPLATE_EXTERN template class Q_EXPORT QIntDictIterator<QWidget>; +#endif + + +class Q_EXPORT QWidgetIntDict : public QIntDict<QWidget> +{ +public: + QWidgetIntDict(int size=17) : QIntDict<QWidget>(size) {} + QWidgetIntDict( const QWidgetIntDict &dict ) : QIntDict<QWidget>(dict) {} + ~QWidgetIntDict() { clear(); } + QWidgetIntDict &operator=(const QWidgetIntDict &dict) + { return (QWidgetIntDict&)QIntDict<QWidget>::operator=(dict); } +}; + +class Q_EXPORT QWidgetIntDictIt : public QIntDictIterator<QWidget> +{ +public: + QWidgetIntDictIt( const QWidgetIntDict &d ) : QIntDictIterator<QWidget>(d) {} + QWidgetIntDictIt &operator=(const QWidgetIntDictIt &i) + { return (QWidgetIntDictIt&)QIntDictIterator<QWidget>::operator=(i); } +}; + + +#endif diff --git a/src/kernel/qwidgetlist.h b/src/kernel/qwidgetlist.h new file mode 100644 index 0000000..7258a21 --- /dev/null +++ b/src/kernel/qwidgetlist.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Definition of QWidgetList +** +** Created : 950116 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWIDGETLIST_H +#define QWIDGETLIST_H + +#ifndef QT_H +#include "qwidget.h" +#include "qptrlist.h" +#endif // QT_H + +class Q_EXPORT QWidgetList : public QPtrList<QWidget> +{ +public: + QWidgetList() : QPtrList<QWidget>() {} + QWidgetList( const QWidgetList &list ) : QPtrList<QWidget>(list) {} + ~QWidgetList() { clear(); } + QWidgetList &operator=(const QWidgetList &list) + { return (QWidgetList&)QPtrList<QWidget>::operator=(list); } +}; + +class Q_EXPORT QWidgetListIt : public QPtrListIterator<QWidget> +{ +public: + QWidgetListIt( const QWidgetList &l ) : QPtrListIterator<QWidget>(l) {} + QWidgetListIt &operator=(const QWidgetListIt &i) + { return (QWidgetListIt&)QPtrListIterator<QWidget>::operator=(i); } +}; + +#endif // QWIDGETLIST_H diff --git a/src/kernel/qwindow.cpp b/src/kernel/qwindow.cpp new file mode 100644 index 0000000..5b2eec8 --- /dev/null +++ b/src/kernel/qwindow.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Implementation of QWindow class +** +** Created : 931211 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ diff --git a/src/kernel/qwindow.h b/src/kernel/qwindow.h new file mode 100644 index 0000000..30ffc80 --- /dev/null +++ b/src/kernel/qwindow.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Definition of QWindow class +** +** Created : 931112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWINDOW_H +#define QWINDOW_H + +#error "QWindow has gone away" + +#endif // QWINDOW_H diff --git a/src/kernel/qwindowdefs.h b/src/kernel/qwindowdefs.h new file mode 100644 index 0000000..05a3a33 --- /dev/null +++ b/src/kernel/qwindowdefs.h @@ -0,0 +1,197 @@ +/**************************************************************************** +** +** Definition of general window system dependent functions, types and +** constants +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWINDOWDEFS_H +#define QWINDOWDEFS_H + +#ifndef QT_H +#include "qobjectdefs.h" +#include "qstring.h" +#include "qnamespace.h" +#endif // QT_H + +// Class forward definitions + +class QPaintDevice; +class QPaintDeviceMetrics; +class QWidget; +class QWidgetMapper; +class QDialog; +class QColor; +class QColorGroup; +class QPalette; +class QCursor; +class QPoint; +class QSize; +class QRect; +class QPointArray; +class QPainter; +class QRegion; +class QFont; +class QFontMetrics; +class QFontInfo; +class QPen; +class QBrush; +class QWMatrix; +class QPixmap; +class QBitmap; +class QMovie; +class QImage; +class QImageIO; +class QPicture; +class QPrinter; +class QAccel; +class QTimer; +class QTime; +class QClipboard; + + +// Widget list (defined in qwidgetlist.h) + +class QWidgetList; +class QWidgetListIt; + + +// Window system dependent definitions + +#if defined(Q_WS_MAC) +#if QT_MACOSX_VERSION < 0x1020 +typedef struct OpaqueEventLoopTimerRef* EventLoopTimerRef; +typedef struct OpaqueMenuHandle *MenuRef; +#else +typedef struct __EventLoopTimer* EventLoopTimerRef; +typedef struct OpaqueMenuRef* MenuRef; +#endif + +#ifndef Q_WS_MACX +typedef struct CGContext *CGContextRef; +#endif +typedef struct OpaqueWindowGroupRef *WindowGroupRef; +typedef struct OpaqueGrafPtr *CGrafPtr; +typedef struct OpaquePMPrintSession *PMPrintSession; +typedef struct OpaquePMPrintSettings *PMPrintSettings; +typedef struct OpaquePMPageFormat *PMPageFormat; +typedef struct Point Point; +typedef struct OpaqueEventHandlerRef* EventHandlerRef; +typedef struct OpaqueEventHandlerCallRef* EventHandlerCallRef; +typedef struct OpaqueEventRef* EventRef; +typedef long int OSStatus; +typedef struct OpaqueScrapRef *ScrapRef; +typedef struct OpaqueRgnHandle *RgnHandle; +typedef struct OpaqueWindowPtr *WindowPtr; +typedef WindowPtr WindowRef; +typedef struct OpaqueGrafPtr *GWorldPtr; +typedef GWorldPtr GrafPtr; +typedef struct GDevice **GDHandle; +typedef struct ColorTable ColorTable; +typedef struct BitMap BitMap; +typedef struct EventRecord EventRecord; +typedef void * MSG; +typedef int WId; +typedef struct AEDesc AppleEvent; + +#endif // Q_WS_MAC + +#if defined(Q_WS_WIN) +#include "qwindowdefs_win.h" +#endif // Q_WS_WIN + + +#if defined(Q_OS_TEMP) +#include "qwinfunctions_wce.h" +#endif // Q_OS_TEMP + +#if defined(Q_WS_X11) + +typedef struct _XDisplay Display; +typedef union _XEvent XEvent; +typedef struct _XGC *GC; +typedef struct _XRegion *Region; +typedef unsigned long WId; + +Q_EXPORT Display *qt_xdisplay(); +Q_EXPORT int qt_xscreen(); +Q_EXPORT WId qt_xrootwin(); // ### REMOVE 4.0 +Q_EXPORT WId qt_xrootwin( int scrn ); // ### 4.0 add default arg of -1 +Q_EXPORT GC qt_xget_readonly_gc( int scrn, bool monochrome ); +Q_EXPORT GC qt_xget_temp_gc( int scrn, bool monochrome ); + +Q_EXPORT const char *qAppClass(); // get application class + +#endif // Q_WS_X11 + +#if defined(Q_WS_QWS) + +typedef unsigned long WId; +struct QWSEvent; +class QGfx; + +#endif // Q_WS_QWS + +class QApplication; + +#if defined(NEEDS_QMAIN) +#define main qMain +#endif + +// Global platform-independent types and functions + +typedef Q_INT32 QCOORD; // coordinate type +const QCOORD QCOORD_MAX = 2147483647; +const QCOORD QCOORD_MIN = -QCOORD_MAX - 1; + +typedef unsigned int QRgb; // RGB triplet + +Q_EXPORT const char *qAppName(); // get application name + +// Misc functions + +typedef void (*QtCleanUpFunction)(); +Q_EXPORT void qAddPostRoutine( QtCleanUpFunction ); +Q_EXPORT void qRemovePostRoutine( QtCleanUpFunction ); + +#if !defined(QT_CLEAN_NAMESPACE) +// source compatibility with Qt 2.x +typedef QtCleanUpFunction Q_CleanUpFunction; +#endif + + +#endif // QWINDOWDEFS_H diff --git a/src/kernel/qwmatrix.cpp b/src/kernel/qwmatrix.cpp new file mode 100644 index 0000000..aaefddd --- /dev/null +++ b/src/kernel/qwmatrix.cpp @@ -0,0 +1,1036 @@ +/**************************************************************************** +** +** Implementation of QWMatrix class +** +** Created : 941020 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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 "qwmatrix.h" +#include "qdatastream.h" +#include "qregion.h" +#if defined(Q_WS_X11) +double qsincos( double, bool calcCos ); // defined in qpainter_x11.cpp +#else +#include <math.h> +#endif + +#include <limits.h> + +#ifndef QT_NO_WMATRIX + +/*! + \class QWMatrix qwmatrix.h + \brief The QWMatrix class specifies 2D transformations of a + coordinate system. + + \ingroup graphics + \ingroup images + + The standard coordinate system of a \link QPaintDevice paint + device\endlink has the origin located at the top-left position. X + values increase to the right; Y values increase downward. + + This coordinate system is the default for the QPainter, which + renders graphics in a paint device. A user-defined coordinate + system can be specified by setting a QWMatrix for the painter. + + Example: + \code + MyWidget::paintEvent( QPaintEvent * ) + { + QPainter p; // our painter + QWMatrix m; // our transformation matrix + m.rotate( 22.5 ); // rotated coordinate system + p.begin( this ); // start painting + p.setWorldMatrix( m ); // use rotated coordinate system + p.drawText( 30,20, "detator" ); // draw rotated text at 30,20 + p.end(); // painting done + } + \endcode + + A matrix specifies how to translate, scale, shear or rotate the + graphics; the actual transformation is performed by the drawing + routines in QPainter and by QPixmap::xForm(). + + The QWMatrix class contains a 3x3 matrix of the form: + <table align=center border=1 cellpadding=1 cellspacing=0> + <tr align=center><td>m11</td><td>m12</td><td> 0 </td></tr> + <tr align=center><td>m21</td><td>m22</td><td> 0 </td></tr> + <tr align=center><td>dx</td> <td>dy</td> <td> 1 </td></tr> + </table> + + A matrix transforms a point in the plane to another point: + \code + x' = m11*x + m21*y + dx + y' = m22*y + m12*x + dy + \endcode + + The point \e (x, y) is the original point, and \e (x', y') is the + transformed point. \e (x', y') can be transformed back to \e (x, + y) by performing the same operation on the \link + QWMatrix::invert() inverted matrix\endlink. + + The elements \e dx and \e dy specify horizontal and vertical + translation. The elements \e m11 and \e m22 specify horizontal and + vertical scaling. The elements \e m12 and \e m21 specify + horizontal and vertical shearing. + + The identity matrix has \e m11 and \e m22 set to 1; all others are + set to 0. This matrix maps a point to itself. + + Translation is the simplest transformation. Setting \e dx and \e + dy will move the coordinate system \e dx units along the X axis + and \e dy units along the Y axis. + + Scaling can be done by setting \e m11 and \e m22. For example, + setting \e m11 to 2 and \e m22 to 1.5 will double the height and + increase the width by 50%. + + Shearing is controlled by \e m12 and \e m21. Setting these + elements to values different from zero will twist the coordinate + system. + + Rotation is achieved by carefully setting both the shearing + factors and the scaling factors. The QWMatrix also has a function + that sets \link rotate() rotation \endlink directly. + + QWMatrix lets you combine transformations like this: + \code + QWMatrix m; // identity matrix + m.translate(10, -20); // first translate (10,-20) + m.rotate(25); // then rotate 25 degrees + m.scale(1.2, 0.7); // finally scale it + \endcode + + Here's the same example using basic matrix operations: + \code + double a = pi/180 * 25; // convert 25 to radians + double sina = sin(a); + double cosa = cos(a); + QWMatrix m1(1, 0, 0, 1, 10, -20); // translation matrix + QWMatrix m2( cosa, sina, // rotation matrix + -sina, cosa, 0, 0 ); + QWMatrix m3(1.2, 0, 0, 0.7, 0, 0); // scaling matrix + QWMatrix m; + m = m3 * m2 * m1; // combine all transformations + \endcode + + \l QPainter has functions to translate, scale, shear and rotate the + coordinate system without using a QWMatrix. Although these + functions are very convenient, it can be more efficient to build a + QWMatrix and call QPainter::setWorldMatrix() if you want to perform + more than a single transform operation. + + \sa QPainter::setWorldMatrix(), QPixmap::xForm() +*/ + +bool qt_old_transformations = TRUE; + +/*! + \enum QWMatrix::TransformationMode + + \keyword transformation matrix + + QWMatrix offers two transformation modes. Calculations can either + be done in terms of points (Points mode, the default), or in + terms of area (Area mode). + + In Points mode the transformation is applied to the points that + mark out the shape's bounding line. In Areas mode the + transformation is applied in such a way that the area of the + contained region is correctly transformed under the matrix. + + \value Points transformations are applied to the shape's points. + \value Areas transformations are applied (e.g. to the width and + height) so that the area is transformed. + + Example: + + Suppose we have a rectangle, + \c{QRect( 10, 20, 30, 40 )} and a transformation matrix + \c{QWMatrix( 2, 0, 0, 2, 0, 0 )} to double the rectangle's size. + + In Points mode, the matrix will transform the top-left (10,20) and + the bottom-right (39,59) points producing a rectangle with its + top-left point at (20,40) and its bottom-right point at (78,118), + i.e. with a width of 59 and a height of 79. + + In Areas mode, the matrix will transform the top-left point in + the same way as in Points mode to (20/40), and double the width + and height, so the bottom-right will become (69,99), i.e. a width + of 60 and a height of 80. + + Because integer arithmetic is used (for speed), rounding + differences mean that the modes will produce slightly different + results given the same shape and the same transformation, + especially when scaling up. This also means that some operations + are not commutative. + + Under Points mode, \c{matrix * ( region1 | region2 )} is not equal to + \c{matrix * region1 | matrix * region2}. Under Area mode, \c{matrix * + (pointarray[i])} is not neccesarily equal to + \c{(matrix * pointarry)[i]}. + + \img xform.png Comparison of Points and Areas TransformationModes +*/ + +/*! + Sets the transformation mode that QWMatrix and painter + transformations use to \a m. + + \sa QWMatrix::TransformationMode +*/ +void QWMatrix::setTransformationMode( QWMatrix::TransformationMode m ) +{ + if ( m == QWMatrix::Points ) + qt_old_transformations = TRUE; + else + qt_old_transformations = FALSE; +} + + +/*! + Returns the current transformation mode. + + \sa QWMatrix::TransformationMode +*/ +QWMatrix::TransformationMode QWMatrix::transformationMode() +{ + return (qt_old_transformations ? QWMatrix::Points : QWMatrix::Areas ); +} + + +// some defines to inline some code +#define MAPDOUBLE( x, y, nx, ny ) \ +{ \ + double fx = x; \ + double fy = y; \ + nx = _m11*fx + _m21*fy + _dx; \ + ny = _m12*fx + _m22*fy + _dy; \ +} + +#define MAPINT( x, y, nx, ny ) \ +{ \ + double fx = x; \ + double fy = y; \ + nx = qRound(_m11*fx + _m21*fy + _dx); \ + ny = qRound(_m12*fx + _m22*fy + _dy); \ +} + +/***************************************************************************** + QWMatrix member functions + *****************************************************************************/ + +/*! + Constructs an identity matrix. All elements are set to zero except + \e m11 and \e m22 (scaling), which are set to 1. +*/ + +QWMatrix::QWMatrix() +{ + _m11 = _m22 = 1.0; + _m12 = _m21 = _dx = _dy = 0.0; +} + +/*! + Constructs a matrix with the elements, \a m11, \a m12, \a m21, \a + m22, \a dx and \a dy. +*/ + +QWMatrix::QWMatrix( double m11, double m12, double m21, double m22, + double dx, double dy ) +{ + _m11 = m11; _m12 = m12; + _m21 = m21; _m22 = m22; + _dx = dx; _dy = dy; +} + + +/*! + Sets the matrix elements to the specified values, \a m11, \a m12, + \a m21, \a m22, \a dx and \a dy. +*/ + +void QWMatrix::setMatrix( double m11, double m12, double m21, double m22, + double dx, double dy ) +{ + _m11 = m11; _m12 = m12; + _m21 = m21; _m22 = m22; + _dx = dx; _dy = dy; +} + + +/*! + \fn double QWMatrix::m11() const + + Returns the X scaling factor. +*/ + +/*! + \fn double QWMatrix::m12() const + + Returns the vertical shearing factor. +*/ + +/*! + \fn double QWMatrix::m21() const + + Returns the horizontal shearing factor. +*/ + +/*! + \fn double QWMatrix::m22() const + + Returns the Y scaling factor. +*/ + +/*! + \fn double QWMatrix::dx() const + + Returns the horizontal translation. +*/ + +/*! + \fn double QWMatrix::dy() const + + Returns the vertical translation. +*/ + + +/*! + \overload + + Transforms ( \a x, \a y ) to ( \a *tx, \a *ty ) using the + following formulae: + + \code + *tx = m11*x + m21*y + dx + *ty = m22*y + m12*x + dy + \endcode +*/ + +void QWMatrix::map( double x, double y, double *tx, double *ty ) const +{ + MAPDOUBLE( x, y, *tx, *ty ); +} + +/*! + Transforms ( \a x, \a y ) to ( \a *tx, \a *ty ) using the formulae: + + \code + *tx = m11*x + m21*y + dx (rounded to the nearest integer) + *ty = m22*y + m12*x + dy (rounded to the nearest integer) + \endcode +*/ + +void QWMatrix::map( int x, int y, int *tx, int *ty ) const +{ + MAPINT( x, y, *tx, *ty ); +} + +/*! + \fn QPoint QWMatrix::map( const QPoint &p ) const + + \overload + + Transforms \a p to using the formulae: + + \code + retx = m11*px + m21*py + dx (rounded to the nearest integer) + rety = m22*py + m12*px + dy (rounded to the nearest integer) + \endcode +*/ + +/*! + \fn QRect QWMatrix::map( const QRect &r ) const + + \obsolete + + Please use \l QWMatrix::mapRect() instead. + + Note that this method does return the bounding rectangle of the \a r, when + shearing or rotations are used. +*/ + +/*! + \fn QPointArray QWMatrix::map( const QPointArray &a ) const + + \overload + + Returns the point array \a a transformed by calling map for each point. +*/ + + +/*! + \fn QRegion QWMatrix::map( const QRegion &r ) const + + \overload + + Transforms the region \a r. + + Calling this method can be rather expensive, if rotations or + shearing are used. +*/ + +/*! + \fn QRegion QWMatrix::mapToRegion( const QRect &rect ) const + + Returns the transformed rectangle \a rect. + + A rectangle which has been rotated or sheared may result in a + non-rectangular region being returned. + + Calling this method can be expensive, if rotations or shearing are + used. If you just need to know the bounding rectangle of the + returned region, use mapRect() which is a lot faster than this + function. + + \sa QWMatrix::mapRect() +*/ + + +/*! + Returns the transformed rectangle \a rect. + + The bounding rectangle is returned if rotation or shearing has + been specified. + + If you need to know the exact region \a rect maps to use \l + operator*(). + + \sa operator*() +*/ + +QRect QWMatrix::mapRect( const QRect &rect ) const +{ + QRect result; + if( qt_old_transformations ) { + if ( _m12 == 0.0F && _m21 == 0.0F ) { + result = QRect( map(rect.topLeft()), map(rect.bottomRight()) ).normalize(); + } else { + QPointArray a( rect ); + a = map( a ); + result = a.boundingRect(); + } + } else { + if ( _m12 == 0.0F && _m21 == 0.0F ) { + int x = qRound( _m11*rect.x() + _dx ); + int y = qRound( _m22*rect.y() + _dy ); + int w = qRound( _m11*rect.width() ); + int h = qRound( _m22*rect.height() ); + if ( w < 0 ) { + w = -w; + x -= w-1; + } + if ( h < 0 ) { + h = -h; + y -= h-1; + } + result = QRect( x, y, w, h ); + } else { + + // see mapToPolygon for explanations of the algorithm. + double x0, y0; + double x, y; + MAPDOUBLE( rect.left(), rect.top(), x0, y0 ); + double xmin = x0; + double ymin = y0; + double xmax = x0; + double ymax = y0; + MAPDOUBLE( rect.right() + 1, rect.top(), x, y ); + xmin = QMIN( xmin, x ); + ymin = QMIN( ymin, y ); + xmax = QMAX( xmax, x ); + ymax = QMAX( ymax, y ); + MAPDOUBLE( rect.right() + 1, rect.bottom() + 1, x, y ); + xmin = QMIN( xmin, x ); + ymin = QMIN( ymin, y ); + xmax = QMAX( xmax, x ); + ymax = QMAX( ymax, y ); + MAPDOUBLE( rect.left(), rect.bottom() + 1, x, y ); + xmin = QMIN( xmin, x ); + ymin = QMIN( ymin, y ); + xmax = QMAX( xmax, x ); + ymax = QMAX( ymax, y ); + double w = xmax - xmin; + double h = ymax - ymin; + xmin -= ( xmin - x0 ) / w; + ymin -= ( ymin - y0 ) / h; + xmax -= ( xmax - x0 ) / w; + ymax -= ( ymax - y0 ) / h; + result = QRect( qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin)+1, qRound(ymax)-qRound(ymin)+1 ); + } + } + return result; +} + + +/*! + \internal +*/ +QPoint QWMatrix::operator *( const QPoint &p ) const +{ + double fx = p.x(); + double fy = p.y(); + return QPoint( qRound(_m11*fx + _m21*fy + _dx), + qRound(_m12*fx + _m22*fy + _dy) ); +} + + +struct QWMDoublePoint { + double x; + double y; +}; + +/*! + \internal +*/ +QPointArray QWMatrix::operator *( const QPointArray &a ) const +{ + if( qt_old_transformations ) { + QPointArray result = a.copy(); + int x, y; + for ( int i=0; i<(int)result.size(); i++ ) { + result.point( i, &x, &y ); + MAPINT( x, y, x, y ); + result.setPoint( i, x, y ); + } + return result; + } else { + int size = a.size(); + int i; + QMemArray<QWMDoublePoint> p( size ); + QPoint *da = a.data(); + QWMDoublePoint *dp = p.data(); + double xmin = INT_MAX; + double ymin = xmin; + double xmax = INT_MIN; + double ymax = xmax; + int xminp = 0; + int yminp = 0; + for( i = 0; i < size; i++ ) { + dp[i].x = da[i].x(); + dp[i].y = da[i].y(); + if ( dp[i].x < xmin ) { + xmin = dp[i].x; + xminp = i; + } + if ( dp[i].y < ymin ) { + ymin = dp[i].y; + yminp = i; + } + xmax = QMAX( xmax, dp[i].x ); + ymax = QMAX( ymax, dp[i].y ); + } + double w = QMAX( xmax - xmin, 1 ); + double h = QMAX( ymax - ymin, 1 ); + for( i = 0; i < size; i++ ) { + dp[i].x += (dp[i].x - xmin)/w; + dp[i].y += (dp[i].y - ymin)/h; + MAPDOUBLE( dp[i].x, dp[i].y, dp[i].x, dp[i].y ); + } + + // now apply correction back for transformed values... + xmin = INT_MAX; + ymin = xmin; + xmax = INT_MIN; + ymax = xmax; + for( i = 0; i < size; i++ ) { + xmin = QMIN( xmin, dp[i].x ); + ymin = QMIN( ymin, dp[i].y ); + xmax = QMAX( xmax, dp[i].x ); + ymax = QMAX( ymax, dp[i].y ); + } + w = QMAX( xmax - xmin, 1 ); + h = QMAX( ymax - ymin, 1 ); + + QPointArray result( size ); + QPoint *dr = result.data(); + for( i = 0; i < size; i++ ) { + dr[i].setX( qRound( dp[i].x - (dp[i].x - dp[xminp].x)/w ) ); + dr[i].setY( qRound( dp[i].y - (dp[i].y - dp[yminp].y)/h ) ); + } + return result; + } +} + +/*! +\internal +*/ +QRegion QWMatrix::operator * (const QRect &rect ) const +{ + QRegion result; + if ( isIdentity() ) { + result = rect; + } else if ( _m12 == 0.0F && _m21 == 0.0F ) { + if( qt_old_transformations ) { + result = QRect( map(rect.topLeft()), map(rect.bottomRight()) ).normalize(); + } else { + int x = qRound( _m11*rect.x() + _dx ); + int y = qRound( _m22*rect.y() + _dy ); + int w = qRound( _m11*rect.width() ); + int h = qRound( _m22*rect.height() ); + if ( w < 0 ) { + w = -w; + x -= w - 1; + } + if ( h < 0 ) { + h = -h; + y -= h - 1; + } + result = QRect( x, y, w, h ); + } + } else { + result = QRegion( mapToPolygon( rect ) ); + } + return result; + +} + +/*! + Returns the transformed rectangle \a rect as a polygon. + + Polygons and rectangles behave slightly differently + when transformed (due to integer rounding), so + \c{matrix.map( QPointArray( rect ) )} is not always the same as + \c{matrix.mapToPolygon( rect )}. +*/ +QPointArray QWMatrix::mapToPolygon( const QRect &rect ) const +{ + QPointArray a( 4 ); + if ( qt_old_transformations ) { + a = QPointArray( rect ); + return operator *( a ); + } + double x[4], y[4]; + if ( _m12 == 0.0F && _m21 == 0.0F ) { + x[0] = qRound( _m11*rect.x() + _dx ); + y[0] = qRound( _m22*rect.y() + _dy ); + double w = qRound( _m11*rect.width() ); + double h = qRound( _m22*rect.height() ); + if ( w < 0 ) { + w = -w; + x[0] -= w - 1.; + } + if ( h < 0 ) { + h = -h; + y[0] -= h - 1.; + } + x[1] = x[0]+w-1; + x[2] = x[1]; + x[3] = x[0]; + y[1] = y[0]; + y[2] = y[0]+h-1; + y[3] = y[2]; + } else { + MAPINT( rect.left(), rect.top(), x[0], y[0] ); + MAPINT( rect.right() + 1, rect.top(), x[1], y[1] ); + MAPINT( rect.right() + 1, rect.bottom() + 1, x[2], y[2] ); + MAPINT( rect.left(), rect.bottom() + 1, x[3], y[3] ); + + /* + Including rectangles as we have are evil. + + We now have a rectangle that is one pixel to wide and one to + high. the tranformed position of the top-left corner is + correct. All other points need some adjustments. + + Doing this mathematically exact would force us to calculate some square roots, + something we don't want for the sake of speed. + + Instead we use an approximation, that converts to the correct + answer when m12 -> 0 and m21 -> 0, and accept smaller + errors in the general transformation case. + + The solution is to calculate the width and height of the + bounding rect, and scale the points 1/2/3 by (xp-x0)/xw pixel direction + to point 0. + */ + + double xmin = x[0]; + double ymin = y[0]; + double xmax = x[0]; + double ymax = y[0]; + int i; + for( i = 1; i< 4; i++ ) { + xmin = QMIN( xmin, x[i] ); + ymin = QMIN( ymin, y[i] ); + xmax = QMAX( xmax, x[i] ); + ymax = QMAX( ymax, y[i] ); + } + double w = xmax - xmin; + double h = ymax - ymin; + + for( i = 1; i < 4; i++ ) { + x[i] -= (x[i] - x[0])/w; + y[i] -= (y[i] - y[0])/h; + } + } +#if 0 + int i; + for( i = 0; i< 4; i++ ) + qDebug("coords(%d) = (%f/%f) (%d/%d)", i, x[i], y[i], qRound(x[i]), qRound(y[i]) ); + qDebug( "width=%f, height=%f", sqrt( (x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0]) ), + sqrt( (x[0]-x[3])*(x[0]-x[3]) + (y[0]-y[3])*(y[0]-y[3]) ) ); +#endif + // all coordinates are correctly, tranform to a pointarray + // (rounding to the next integer) + a.setPoints( 4, qRound( x[0] ), qRound( y[0] ), + qRound( x[1] ), qRound( y[1] ), + qRound( x[2] ), qRound( y[2] ), + qRound( x[3] ), qRound( y[3] ) ); + return a; +} + +/*! +\internal +*/ +QRegion QWMatrix::operator * (const QRegion &r ) const +{ + if ( isIdentity() ) + return r; + QMemArray<QRect> rects = r.rects(); + QRegion result; + register QRect *rect = rects.data(); + register int i = rects.size(); + if ( _m12 == 0.0F && _m21 == 0.0F && _m11 > 1.0F && _m22 > 1.0F ) { + // simple case, no rotation + while ( i ) { + int x = qRound( _m11*rect->x() + _dx ); + int y = qRound( _m22*rect->y() + _dy ); + int w = qRound( _m11*rect->width() ); + int h = qRound( _m22*rect->height() ); + if ( w < 0 ) { + w = -w; + x -= w-1; + } + if ( h < 0 ) { + h = -h; + y -= h-1; + } + *rect = QRect( x, y, w, h ); + rect++; + i--; + } + result.setRects( rects.data(), rects.size() ); + } else { + while ( i ) { + result |= operator *( *rect ); + rect++; + i--; + } + } + return result; +} + +/*! + Resets the matrix to an identity matrix. + + All elements are set to zero, except \e m11 and \e m22 (scaling) + which are set to 1. + + \sa isIdentity() +*/ + +void QWMatrix::reset() +{ + _m11 = _m22 = 1.0; + _m12 = _m21 = _dx = _dy = 0.0; +} + +/*! + Returns TRUE if the matrix is the identity matrix; otherwise returns FALSE. + + \sa reset() +*/ +bool QWMatrix::isIdentity() const +{ + return _m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0 + && _dx == 0.0 && _dy == 0.0; +} + +/*! + Moves the coordinate system \a dx along the X-axis and \a dy along + the Y-axis. + + Returns a reference to the matrix. + + \sa scale(), shear(), rotate() +*/ + +QWMatrix &QWMatrix::translate( double dx, double dy ) +{ + _dx += dx*_m11 + dy*_m21; + _dy += dy*_m22 + dx*_m12; + return *this; +} + +/*! + Scales the coordinate system unit by \a sx horizontally and \a sy + vertically. + + Returns a reference to the matrix. + + \sa translate(), shear(), rotate() +*/ + +QWMatrix &QWMatrix::scale( double sx, double sy ) +{ + _m11 *= sx; + _m12 *= sx; + _m21 *= sy; + _m22 *= sy; + return *this; +} + +/*! + Shears the coordinate system by \a sh horizontally and \a sv + vertically. + + Returns a reference to the matrix. + + \sa translate(), scale(), rotate() +*/ + +QWMatrix &QWMatrix::shear( double sh, double sv ) +{ + double tm11 = sv*_m21; + double tm12 = sv*_m22; + double tm21 = sh*_m11; + double tm22 = sh*_m12; + _m11 += tm11; + _m12 += tm12; + _m21 += tm21; + _m22 += tm22; + return *this; +} + +const double deg2rad = 0.017453292519943295769; // pi/180 + +/*! + Rotates the coordinate system \a a degrees counterclockwise. + + Returns a reference to the matrix. + + \sa translate(), scale(), shear() +*/ + +QWMatrix &QWMatrix::rotate( double a ) +{ + double b = deg2rad*a; // convert to radians +#if defined(Q_WS_X11) + double sina = qsincos(b,FALSE); // fast and convenient + double cosa = qsincos(b,TRUE); +#else + double sina = sin(b); + double cosa = cos(b); +#endif + double tm11 = cosa*_m11 + sina*_m21; + double tm12 = cosa*_m12 + sina*_m22; + double tm21 = -sina*_m11 + cosa*_m21; + double tm22 = -sina*_m12 + cosa*_m22; + _m11 = tm11; _m12 = tm12; + _m21 = tm21; _m22 = tm22; + return *this; +} + +/*! + \fn bool QWMatrix::isInvertible() const + + Returns TRUE if the matrix is invertible; otherwise returns FALSE. + + \sa invert() +*/ + +/*! + \fn double QWMatrix::det() const + + Returns the matrix's determinant. +*/ + + +/*! + Returns the inverted matrix. + + If the matrix is singular (not invertible), the identity matrix is + returned. + + If \a invertible is not 0: the value of \a *invertible is set + to TRUE if the matrix is invertible; otherwise \a *invertible is + set to FALSE. + + \sa isInvertible() +*/ + +QWMatrix QWMatrix::invert( bool *invertible ) const +{ + double determinant = det(); + if ( determinant == 0.0 ) { + if ( invertible ) + *invertible = FALSE; // singular matrix + QWMatrix defaultMatrix; + return defaultMatrix; + } + else { // invertible matrix + if ( invertible ) + *invertible = TRUE; + double dinv = 1.0/determinant; + QWMatrix imatrix( (_m22*dinv), (-_m12*dinv), + (-_m21*dinv), ( _m11*dinv), + ((_m21*_dy - _m22*_dx)*dinv), + ((_m12*_dx - _m11*_dy)*dinv) ); + return imatrix; + } +} + + +/*! + Returns TRUE if this matrix is equal to \a m; otherwise returns FALSE. +*/ + +bool QWMatrix::operator==( const QWMatrix &m ) const +{ + return _m11 == m._m11 && + _m12 == m._m12 && + _m21 == m._m21 && + _m22 == m._m22 && + _dx == m._dx && + _dy == m._dy; +} + +/*! + Returns TRUE if this matrix is not equal to \a m; otherwise returns FALSE. +*/ + +bool QWMatrix::operator!=( const QWMatrix &m ) const +{ + return _m11 != m._m11 || + _m12 != m._m12 || + _m21 != m._m21 || + _m22 != m._m22 || + _dx != m._dx || + _dy != m._dy; +} + +/*! + Returns the result of multiplying this matrix by matrix \a m. +*/ + +QWMatrix &QWMatrix::operator*=( const QWMatrix &m ) +{ + double tm11 = _m11*m._m11 + _m12*m._m21; + double tm12 = _m11*m._m12 + _m12*m._m22; + double tm21 = _m21*m._m11 + _m22*m._m21; + double tm22 = _m21*m._m12 + _m22*m._m22; + + double tdx = _dx*m._m11 + _dy*m._m21 + m._dx; + double tdy = _dx*m._m12 + _dy*m._m22 + m._dy; + + _m11 = tm11; _m12 = tm12; + _m21 = tm21; _m22 = tm22; + _dx = tdx; _dy = tdy; + return *this; +} + +/*! + \overload + \relates QWMatrix + Returns the product of \a m1 * \a m2. + + Note that matrix multiplication is not commutative, i.e. a*b != + b*a. +*/ + +QWMatrix operator*( const QWMatrix &m1, const QWMatrix &m2 ) +{ + QWMatrix result = m1; + result *= m2; + return result; +} + +/***************************************************************************** + QWMatrix stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates QWMatrix + + Writes the matrix \a m to the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator<<( QDataStream &s, const QWMatrix &m ) +{ + if ( s.version() == 1 ) + s << (float)m.m11() << (float)m.m12() << (float)m.m21() + << (float)m.m22() << (float)m.dx() << (float)m.dy(); + else + s << m.m11() << m.m12() << m.m21() << m.m22() + << m.dx() << m.dy(); + return s; +} + +/*! + \relates QWMatrix + + Reads the matrix \a m from the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the QDataStream operators \endlink +*/ + +QDataStream &operator>>( QDataStream &s, QWMatrix &m ) +{ + if ( s.version() == 1 ) { + float m11, m12, m21, m22, dx, dy; + s >> m11; s >> m12; s >> m21; s >> m22; + s >> dx; s >> dy; + m.setMatrix( m11, m12, m21, m22, dx, dy ); + } + else { + double m11, m12, m21, m22, dx, dy; + s >> m11; s >> m12; s >> m21; s >> m22; + s >> dx; s >> dy; + m.setMatrix( m11, m12, m21, m22, dx, dy ); + } + return s; +} +#endif // QT_NO_DATASTREAM + +#endif // QT_NO_WMATRIX + diff --git a/src/kernel/qwmatrix.h b/src/kernel/qwmatrix.h new file mode 100644 index 0000000..f6bd155 --- /dev/null +++ b/src/kernel/qwmatrix.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Definition of QWMatrix class +** +** Created : 941020 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the Qt 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 Qt 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.QPL +** included in the packaging of this file. Licensees holding valid Qt +** Commercial licenses may use this file in accordance with the Qt +** 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. +** +**********************************************************************/ + +#ifndef QWMATRIX_H +#define QWMATRIX_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qpointarray.h" +#include "qrect.h" +#include "qregion.h" +#endif // QT_H + +#ifndef QT_NO_WMATRIX + + +class Q_EXPORT QWMatrix // 2D transform matrix +{ +public: + QWMatrix(); + QWMatrix( double m11, double m12, double m21, double m22, + double dx, double dy ); + + void setMatrix( double m11, double m12, double m21, double m22, + double dx, double dy ); + + double m11() const { return _m11; } + double m12() const { return _m12; } + double m21() const { return _m21; } + double m22() const { return _m22; } + double dx() const { return _dx; } + double dy() const { return _dy; } + + void map( int x, int y, int *tx, int *ty ) const; + void map( double x, double y, double *tx, double *ty ) const; + QRect mapRect( const QRect & ) const; + + QPoint map( const QPoint &p ) const { return operator *( p ); } + QRect map( const QRect &r ) const { return mapRect ( r ); } + QPointArray map( const QPointArray &a ) const { return operator * ( a ); } + QRegion map( const QRegion &r ) const { return operator *( r ); } + QRegion mapToRegion( const QRect &r ) const { return operator *( r ); } + QPointArray mapToPolygon( const QRect &r ) const; + + void reset(); + bool isIdentity() const; + + QWMatrix &translate( double dx, double dy ); + QWMatrix &scale( double sx, double sy ); + QWMatrix &shear( double sh, double sv ); + QWMatrix &rotate( double a ); + + bool isInvertible() const { return (_m11*_m22 - _m12*_m21) != 0; } + double det() const { return _m11*_m22 - _m12*_m21; } + + QWMatrix invert( bool * = 0 ) const; + + bool operator==( const QWMatrix & ) const; + bool operator!=( const QWMatrix & ) const; + QWMatrix &operator*=( const QWMatrix & ); + + /* we use matrix multiplication semantics here */ + QPoint operator * (const QPoint & ) const; + QRegion operator * (const QRect & ) const; + QRegion operator * (const QRegion & ) const; + QPointArray operator * ( const QPointArray &a ) const; + + enum TransformationMode { + Points, Areas + }; + static void setTransformationMode( QWMatrix::TransformationMode m ); + static TransformationMode transformationMode(); +private: + double _m11, _m12; + double _m21, _m22; + double _dx, _dy; +}; + +Q_EXPORT QWMatrix operator*( const QWMatrix &, const QWMatrix & ); + + +/***************************************************************************** + QWMatrix stream functions + *****************************************************************************/ + +Q_EXPORT QDataStream &operator<<( QDataStream &, const QWMatrix & ); +Q_EXPORT QDataStream &operator>>( QDataStream &, QWMatrix & ); + + +#endif // QT_NO_WMATRIX + +#endif // QWMATRIX_H |
