/*
    If rect is empty, no frame is added. (a hack, really)
*/
QRegion QWSWindowsDecoration::region(const QWidget *widget, const QRect &rect, QWSDecoration::Region type)
{
    int titleHeight = getTitleHeight(widget);
//    int titleWidth = getTitleWidth(widget);
//    int bw = rect.isEmpty() ? 0 : BORDER_WIDTH;
    QRegion region;

    switch (type) {
	case Maximize: {
	    QRect r(rect.right() - 2*titleHeight + 1, rect.top() - titleHeight,
			    titleHeight, titleHeight);
	    if (r.left() > rect.left() + titleHeight)
		    region = r;
	    break;
	}
	case Minimize: {
	    QRect r(rect.right() - 3*titleHeight + 4, rect.top() - titleHeight,
		    	    titleHeight, titleHeight);
	    if (r.left() > rect.left() + titleHeight)
		    region = r;
	    break;
	}
	case Menu:
	case Close:
	case All:
	case Title:
	case Top:
	case Left:
	case Right:
	case Bottom:
	case TopLeft:
	case TopRight:
	case BottomLeft:
	case BottomRight:
	default:
	    region = QWSDefaultDecoration::region(widget, rect, type);
	    break;
    }

    return region;
}
void QWSDefaultDecoration::paint(QPainter *painter, const QWidget *widget)
{
#ifndef QT_NO_STYLE
    QStyle &style = QApplication::style();
#endif

    int titleWidth = getTitleWidth(widget);
    int titleHeight = getTitleHeight(widget);

    QRect rect(widget->rect());

    // Border rect
    QRect br( rect.left() - BORDER_WIDTH,
                rect.top() - BORDER_WIDTH - titleHeight,
                rect.width() + 2 * BORDER_WIDTH,
                rect.height() + BORDER_WIDTH + BOTTOM_BORDER_WIDTH + titleHeight );

    // title bar rect
    QRect tr;

    {
        tr = QRect( titleHeight, -titleHeight,  titleWidth, titleHeight - 1);
    }

    QRegion oldClip = painter->clipRegion();
    painter->setClipRegion( oldClip - QRegion( tr ) );	// reduce flicker

#ifndef QT_NO_PALETTE
    // const QColorGroup &cg = QApplication::palette().active();
    const QColorGroup &cg = widget->palette().active();

#if !defined(QT_NO_STYLE)
    style.drawPanel(painter, br.x(), br.y(), br.width(),
		    br.height() - 4, cg, FALSE, 2,
		    &cg.brush(QColorGroup::Background));
#elif !defined(QT_NO_DRAWUTIL)
    qDrawWinPanel(painter, br.x(), br.y(), br.width(),
		  br.height() - 4, cg, FALSE,
		  &cg.brush(QColorGroup::Background));
#endif

    painter->setClipRegion( oldClip );

    if (titleWidth > 0) {
	QBrush titleBrush;
	QPen   titlePen;
	int    titleLeft = titleHeight + 4;

	if (widget == qApp->activeWindow()) {
	    titleBrush = cg.brush(QColorGroup::Highlight);
	    titlePen   = cg.color(QColorGroup::HighlightedText);
	} else {
	    titleBrush = cg.brush(QColorGroup::Background);
	    titlePen   = cg.color(QColorGroup::Text);
	}

#define CLAMP(x, y)	    ( ((x) > (y)) ? (y) : (x) )

	{

#if !defined(QT_NO_STYLE)
	    style.drawPanel(painter, tr.x(), tr.y(), tr.width(), tr.height(),
			    cg, TRUE, 1, &titleBrush);
#elif !defined(QT_NO_DRAWUTIL)
	    qDrawWinPanel(painter, tr.x(), tr.y(), tr.width(), tr.height(),
			    cg, TRUE, &titleBrush);
#endif

	    painter->setPen(titlePen);
	    painter->setFont(widget->font());
	    painter->drawText( titleLeft, -titleHeight,
			    titleWidth-5, titleHeight - 1,
			    QPainter::AlignVCenter, widget->caption());
	    return;
	}

	painter->setPen(titlePen);
	painter->setFont(widget->font());
	painter->drawText( titleLeft, -titleHeight,
	 		rect.width() - titleHeight - 10, titleHeight-1,
			QPainter::AlignVCenter, widget->caption());
    }

#endif //QT_NO_PALETTE

}
QRegion QWSDefaultDecoration::region(const QWidget *widget, const QRect &rect, QWSDecoration::Region type)
{
//    int titleWidth = getTitleWidth(widget);
    int titleHeight = getTitleHeight(widget);

    QRegion region;

    int bw = BORDER_WIDTH;

    switch (type) {
	case All: {
		if ( widget->isMaximized() ) {
		    QRect r(rect.left(), rect.top() - titleHeight,
			    rect.width(), rect.height() + titleHeight);
		    region = r;
		} else {
		    QRect r(rect.left() - bw,
			    rect.top() - titleHeight - bw,
			    rect.width() + 2 * bw,
			    rect.height() + titleHeight + 2 * bw);
		    region = r;
		}
		region -= rect;
	    }
	    break;

	case Title: {
		    QRect r(rect.left() + titleHeight, rect.top() - titleHeight,
			    rect.width() - 4*titleHeight, titleHeight);
		    if (r.width() > 0)
			region = r;
	    }
	    break;

	case Top: {
		    QRect r(rect.left() + CORNER_GRAB,
			    rect.top() - titleHeight - bw,
			    rect.width() - 2 * CORNER_GRAB,
			    bw);
		    region = r;
	    }
	    break;

	case Left: {
		QRect r(rect.left() - bw,
			rect.top() - titleHeight + CORNER_GRAB,
			bw,
			rect.height() + titleHeight - 2 * CORNER_GRAB);
		region = r;
	    }
	    break;

	case Right: {
		QRect r(rect.right() + 1,
			rect.top() - titleHeight + CORNER_GRAB,
			bw,
			rect.height() + titleHeight - 2 * CORNER_GRAB);
		region = r;
	    }
	    break;

	case Bottom: {
		QRect r(rect.left() + CORNER_GRAB,
			rect.bottom() + 1,
			rect.width() - 2 * CORNER_GRAB,
			bw);
		region = r;
	    }
	    break;

	case TopLeft: {
		QRect r1(rect.left() - bw,
			rect.top() - bw - titleHeight,
			CORNER_GRAB + bw,
			bw);

		QRect r2(rect.left() - bw,
			rect.top() - bw - titleHeight,
			bw,
			CORNER_GRAB + bw);

		region = QRegion(r1) + r2;
	    }
	    break;

	case TopRight: {
		QRect r1(rect.right() - CORNER_GRAB,
			rect.top() - bw - titleHeight,
			CORNER_GRAB + bw,
			bw);

		QRect r2(rect.right() + 1,
			rect.top() - bw - titleHeight,
			bw,
			CORNER_GRAB + bw);

		region = QRegion(r1) + r2;
	    }
	    break;

	case BottomLeft: {
		QRect r1(rect.left() - bw,
			rect.bottom() + 1,
			CORNER_GRAB + bw,
			bw);

		QRect r2(rect.left() - bw,
			rect.bottom() - CORNER_GRAB,
			bw,
			CORNER_GRAB + bw);
		region = QRegion(r1) + r2;
	    }
	    break;

	case BottomRight: {
		QRect r1(rect.right() - CORNER_GRAB,
			rect.bottom() + 1,
			CORNER_GRAB + bw,
			bw);

		QRect r2(rect.right() + 1,
			rect.bottom() - CORNER_GRAB,
			bw,
			CORNER_GRAB + bw);
		region = QRegion(r1) + r2;
	    }
	    break;

	case Menu: {
		    QRect r(rect.left(), rect.top() - titleHeight,
			    titleHeight, titleHeight);
		    region = r;
	    }
	    break;

	case Close: {
		QRect r(rect.right() - titleHeight, rect.top() - titleHeight,
			titleHeight, titleHeight);
		if (r.left() > rect.left() + titleHeight)
		    region = r;
	    }
	    break;

	case Maximize: {
		QRect r(rect.right() - 2*titleHeight, rect.top() - titleHeight,
			titleHeight, titleHeight);
		if (r.left() > rect.left() + titleHeight)
		    region = r;
	    }
	    break;

	case Minimize: {
		QRect r(rect.right() - 3*titleHeight, rect.top() - titleHeight,
			titleHeight, titleHeight);
		if (r.left() > rect.left() + titleHeight)
		    region = r;
	    }
	    break;

	default:
	    break;
    }

    return region;
}
int QWSDefaultDecoration::getTitleWidth(const QWidget *widget)
{
    return widget->width() - 4 * getTitleHeight(widget) - 4;
}
void QWSWindowsDecoration::paint(QPainter *painter, const QWidget *widget)
{
    int titleWidth = getTitleWidth(widget);
    int titleHeight = getTitleHeight(widget);

    QRect rect(widget->rect());

    // Border rect
    QRect br( rect.left() - BORDER_WIDTH,
                rect.top() - BORDER_WIDTH - titleHeight,
                rect.width() + 2 * BORDER_WIDTH,
                rect.height() + BORDER_WIDTH + BOTTOM_BORDER_WIDTH + titleHeight );

    // title bar rect
    QRect tr( 0, -titleHeight,  titleWidth - 1, titleHeight - 1);

    QRegion oldClip = painter->clipRegion();
    painter->setClipRegion( oldClip - QRegion( tr ) );	// reduce flicker

#ifndef QT_NO_PALETTE
    const QColorGroup &cg = QApplication::palette().active();
//    const QColorGroup &cg = widget->palette().active();

#if !defined(QT_NO_DRAWUTIL)
    qDrawWinPanel(painter, br.x(), br.y(), br.width(),
		  br.height() - 4, cg, FALSE,
		  &cg.brush(QColorGroup::Background));
#endif

    painter->setClipRegion( oldClip );

    if (titleWidth > 0) {
	QBrush titleBrush;
	QPen   titlePen;
	int    titleLeft = titleHeight + 4;

	if (widget == qApp->activeWindow()) {
	    titleBrush = cg.brush(QColorGroup::Highlight);
	    titlePen   = cg.color(QColorGroup::HighlightedText);
	} else {
	    titleBrush = cg.brush(QColorGroup::Background);
	    titlePen   = cg.color(QColorGroup::Text);
	}

#define CLAMP(x, y)	    ( ((x) > (y)) ? (y) : (x) )

	int x = tr.x();
	QColor c1 = titleBrush.color();
	QColor c2 = titleBrush.color();
	
	if (widget == qApp->activeWindow()) {
	    c1 = QColor( 0x10, 0x20, 0x90 ); // '98 Blue
	    // c2 = QColor( 0x50, 0x60, 0xD0 );
	    c2 = QColor( 0x70, 0x70, 0x70 );
	    // c1 = QColor( 0xBA, 0xC4, 0x05 ); // Trolltech Green
	    // c1 = QColor( 0xAA, 0xB4, 0x00 ); // Trolltech Green
	    // c2 = QColor( 0x20, 0x20, 0x20 );
	} else {
	    c1 = QColor( 0x20, 0x30, 0x50 );
	    // c2 = QColor( 0x60, 0x70, 0x90 );
	    // c2 = QColor( 0x40, 0x40, 0x40 );
	    c2 = QColor( 0x70, 0x70, 0x70 );
	    titlePen = QPen( QColor( 0x90, 0x90, 0x90 ) );
	}
	
	int r = c1.red();
	int g = c1.green();
	int b = c1.blue();
	for (int i = 0; i < 100; i++) {
	    int dr = r + (c2.red() * i) / 100;
	    int dg = g + (c2.green() * i) / 100;
	    int db = b + (c2.blue() * i) / 100;
	    x = tr.x() + (tr.width() * i) / 100;
	    int x2 = tr.x() + (tr.width() * (i + 1)) / 100;
	    QBrush tbr = QBrush( QColor( CLAMP(dr,255), CLAMP(dg,255), CLAMP(db,255) ) );
	    painter->fillRect( x, tr.y(), x2 - x, tr.height(), tbr);
	}
	
#ifndef QT_NO_WIDGET_TOPEXTRA
	painter->setPen(titlePen);
	painter->setFont(widget->font());
	painter->drawText( titleLeft, -titleHeight,
	 		rect.width() - titleHeight - 10, titleHeight-1,
			QPainter::AlignVCenter, widget->caption());
#endif
    }

#endif //QT_NO_PALETTE

}