summaryrefslogtreecommitdiff
path: root/library/qpedecoration_qws.cpp
blob: 1afae8805a50463806832d4b5ed642f241b0621e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** 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.
**
** 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/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#ifdef QWS
#define QTOPIA_INTERNAL_LANGLIST
#include <qapplication.h>
#include <qstyle.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qtimer.h>
#include <qwhatsthis.h>
#include <qpopupmenu.h>
#include "qcopenvelope_qws.h"
#include "qpedecoration_qws.h"
#include <qdialog.h>
#include <qdrawutil.h>
#include <qgfx_qws.h>
#include "qpeapplication.h"
#include "resource.h"
#include "global.h"
#include "qlibrary.h"
#include "windowdecorationinterface.h"
#include <qpe/qlibrary.h>
#include <qfile.h>
#include <qsignal.h>

#include <stdlib.h>

extern Q_EXPORT QRect qt_maxWindowRect;

#define WHATSTHIS_MODE

#ifndef QT_NO_QWS_QPE_WM_STYLE

#ifndef QT_NO_IMAGEIO_XPM

/* XPM */
static const char * const qpe_close_xpm[] = {
"16 16 3 1",
"       c None",
".      c #FFFFFF",
"+      c #000000",
"                ",
"                ",
"      .....     ",
"    ..+++++..   ",
"   .+++++++++.  ",
"   .+..+++..+.  ",
"  .++...+...++. ",
"  .+++.....+++. ",
"  .++++...++++. ",
"  .+++.....+++. ",
"  .++...+...++. ",
"   .+..+++..+.  ",
"   .+++++++++.  ",
"    ..+++++..   ",
"      .....     ",
"                "};

/* XPM */
static const char * const qpe_accept_xpm[] = {
"16 16 3 1",
"       c None",
".      c #FFFFFF",
"+      c #000000",
"                ",
"                ",
"      .....     ",
"    ..+++++..   ",
"   .+++++++++.  ",
"   .+++++++++.  ",
"  .+++++++..++. ",
"  .++.+++...++. ",
"  .+...+...+++. ",
"  .+......++++. ",
"  .++....+++++. ",
"   .++..+++++.  ",
"   .+++++++++.  ",
"    ..+++++..   ",
"      .....     ",
"                "};

#endif // QT_NO_IMAGEIO_XPM

class DecorHackWidget : public QWidget
{
public:
    bool needsOk() {
	return (getWState() & WState_Reserved1 ) ||
		 (inherits("QDialog") && !inherits("QMessageBox")
		  && !inherits("QWizard") );
    }
};

static QImage scaleButton( const QImage &img, int height )
{
    //ML: We don't want to scale if the difference is less than 4 pixels to prevent blurring
    if ( img.height() != 0 && ::abs( img.height()-height ) > 4 ) {
	return img.smoothScale( img.width()*height/img.height(), height );
    } else {
	return img;
    }
}

class TLWidget : public QWidget
{
public:
    QWSManager *manager()
    {
	return topData()->qwsManager;
    }

    QTLWExtra *topExtra()
    {
	return topData();
    }

    void setWState( uint s ) { QWidget::setWState( s ); }
    void clearWState( uint s ) { QWidget::clearWState( s ); }
};


QPEManager::QPEManager( QPEDecoration *d, QObject *parent )
    : QObject( parent ), decoration( d ), helpState(0), inWhatsThis(FALSE)
{
    wtTimer = new QTimer( this );
    connect( wtTimer, SIGNAL(timeout()), this, SLOT(whatsThisTimeout()) );
}


void QPEManager::updateActive()
{
    QWidget *newActive = qApp->activeWindow();
    if ( newActive && (QWidget*)active == newActive )
	return;

    if ( active && (!newActive || ((TLWidget *)newActive)->manager()) ) {
	((TLWidget *)(QWidget*)active)->manager()->removeEventFilter( this );
    }

    if ( newActive && ((TLWidget *)newActive)->manager() ) {
	active = newActive;
	((TLWidget *)(QWidget*)active)->manager()->installEventFilter( this );
    } else if ( !newActive ) {
	active = 0;
    }
}

int QPEManager::pointInQpeRegion( QWidget *w, const QPoint &p )
{
    QRect rect(w->geometry());

    if ( decoration->region( w, rect,
		(QWSDecoration::Region)QPEDecoration::Help ).contains(p) )
	return QPEDecoration::Help;

    for (int i = QWSDecoration::LastRegion; i >= QWSDecoration::Title; i--) {
	if (decoration->region(w, rect, (QWSDecoration::Region)i).contains(p))
	    return (QWSDecoration::Region)i;
    }

    return QWSDecoration::None;
}

bool QPEManager::eventFilter( QObject *o, QEvent *e )
{
    QWSManager *mgr = (QWSManager *)o;
    QWidget *w = mgr->widget();
    switch ( e->type() ) {
	case QEvent::MouseButtonPress:
	    {
		pressTime = QTime::currentTime();
		QPoint p = ((QMouseEvent*)e)->globalPos();
		int inRegion = pointInQpeRegion( w, p );
#ifdef WHATSTHIS_MODE
		if ( !w->geometry().contains(p) && QWhatsThis::inWhatsThisMode() ) {
		    QString text;
		    switch ( inRegion ) {
			case QWSDecoration::Close:
			    if ( ((DecorHackWidget*)w)->needsOk() )
				text = tr("Click to close this window, discarding changes.");
			    else
				text = tr("Click to close this window.");
			    break;
			case QWSDecoration::Minimize:
			    text = tr("Click to close this window and apply changes.");
			    break;
			case QWSDecoration::Maximize:
			    if ( w->isMaximized() )
				text = tr("Click to make this window movable.");
			    else
				text = tr("Click to make this window use all available screen area.");
			    break;
			default:
			    break;
		    }
		    QWhatsThis::leaveWhatsThisMode( text );
		    whatsThisTimeout();
		    helpState = 0;
		    return true;
		}
#endif
		if ( inRegion == QPEDecoration::Help ) {
#ifdef WHATSTHIS_MODE
		    wtTimer->start( 400, TRUE );
#endif
		    helpState = QWSButton::Clicked|QWSButton::MouseOver;
		    drawButton( w, QPEDecoration::Help, helpState );
		    return true;
		}
	    }
	    break;
	case QEvent::MouseButtonRelease:
	    if ( helpState & QWSButton::Clicked ) {
		wtTimer->stop();
		helpState = 0;
		drawButton( w, QPEDecoration::Help, helpState );
		QPoint p = ((QMouseEvent*)e)->globalPos();
		if ( pointInQpeRegion( w, p ) == QPEDecoration::Help ) {
		    decoration->help( w );
		}
		return true;
	    }
	    break;
	case QEvent::MouseMove:
	    if ( helpState & QWSButton::Clicked ) {
		int oldState = helpState;
		QPoint p = ((QMouseEvent*)e)->globalPos();
		if ( pointInQpeRegion( w, p ) == QPEDecoration::Help ) {
		    helpState = QWSButton::Clicked|QWSButton::MouseOver;
		} else {
		    helpState = 0;
		}
		if ( helpState != oldState )
		    drawButton( w, QPEDecoration::Help, helpState );
	    }
	    break;
	default:
	    break;
    }
    return QObject::eventFilter( o, e );
}

void QPEManager::drawButton( QWidget *w, QPEDecoration::QPERegion r, int state )
{
    QPainter painter(w);
    QRegion rgn = ((TLWidget *)w)->topExtra()->decor_allocated_region;
    painter.internalGfx()->setWidgetDeviceRegion( rgn );
    painter.setClipRegion(decoration->region(w, w->rect(),QWSDecoration::All));
    decoration->paintButton( &painter, w, (QWSDecoration::Region)r, state );
}

void QPEManager::drawTitle( QWidget *w )
{
    QPainter painter(w);
    QRegion rgn = ((TLWidget *)w)->topExtra()->decor_allocated_region;
    painter.internalGfx()->setWidgetDeviceRegion( rgn );
    painter.setClipRegion(decoration->region(w, w->rect(),QWSDecoration::All));
    decoration->paint( &painter, w );
    decoration->paintButton(&painter, w, QWSDecoration::Menu, 0);
    decoration->paintButton(&painter, w, QWSDecoration::Close, 0);
    decoration->paintButton(&painter, w, QWSDecoration::Minimize, 0);
    decoration->paintButton(&painter, w, QWSDecoration::Maximize, 0);
}

void QPEManager::whatsThisTimeout()
{
    if ( !QWhatsThis::inWhatsThisMode() ) {
	if ( inWhatsThis ) {
	    if ( whatsThis ) {
		QWidget *w = whatsThis;
		whatsThis = 0;
		drawTitle( w );
	    }
	    wtTimer->stop();
	} else {
	    QWhatsThis::enterWhatsThisMode();
	    helpState = 0;
	    updateActive();
	    if ( active ) {
		whatsThis = active;
		drawTitle( active );
		// check periodically to see if we've left whats this mode
		wtTimer->start( 250 );
	    }
	}
	inWhatsThis = !inWhatsThis;
    }
}

//===========================================================================
#define IMAGE_METHOD( methodName, imageName )                             \
static QImage* methodName( int th ) {                                     \
    static QImage *i = 0;                                                 \
     if ( !i || ::abs( i->height()-th ) > 4 ) {                           \
         delete i;                                                        \
         i = new QImage(scaleButton(Resource::loadImage(#imageName),th)); \
     }                                                                    \
                                                                          \
     return i;                                                            \
}

IMAGE_METHOD(okImage,      OKButton       );
IMAGE_METHOD(closeImage,   CloseButton    );
IMAGE_METHOD(helpImage,    HelpButton     );
IMAGE_METHOD(maximizeImage,MaximizeButton );

int WindowDecorationInterface::metric( Metric m, const WindowData *wd ) const
{
    switch ( m ) {
	case TitleHeight:
	    if ( QApplication::desktop()->height() > 320 )
		return 19;
	    else
		return 15;
	case LeftBorder:
	case RightBorder:
	case TopBorder:
	case BottomBorder:
	    return 4;
	case OKWidth:
	    return okImage(metric(TitleHeight,wd))->width();
	case CloseWidth:
	    return closeImage(metric(TitleHeight,wd))->width();
	case HelpWidth:
	    return helpImage(metric(TitleHeight,wd))->width();
	case MaximizeWidth:
	    return maximizeImage(metric(TitleHeight,wd))->width();
	case CornerGrabSize:
	    return 16;
    }

    return 0;
}

void WindowDecorationInterface::drawArea( Area a, QPainter *p, const WindowData *wd ) const
{
    int th = metric( TitleHeight, wd );
    QRect r = wd->rect;

    switch ( a ) {
	case Border:
	    {
		const QColorGroup &cg = wd->palette.active();
		qDrawWinPanel(p, r.x()-metric(LeftBorder,wd),
			r.y()-th-metric(TopBorder,wd),
			r.width()+metric(LeftBorder,wd)+metric(RightBorder,wd),
			r.height()+th+metric(TopBorder,wd)+metric(BottomBorder,wd),
			cg, FALSE, &cg.brush(QColorGroup::Background));
	    }
	    break;
	case Title:
	    {
		const QColorGroup &cg = wd->palette.active();
		QBrush titleBrush;
		QPen   titleLines;

		if ( wd->flags & WindowData::Active ) {
		    titleBrush = cg.brush(QColorGroup::Highlight);
		    titleLines = titleBrush.color().dark();
		} else {
		    titleBrush = cg.brush(QColorGroup::Background);
		    titleLines = titleBrush.color();
		}

		p->fillRect( r.x(), r.y()-th, r.width(), th, titleBrush);

		p->setPen( titleLines );
		for ( int i = r.y()-th; i < r.y(); i += 2 )
		    p->drawLine( r.left(), i, r.right(), i );
	    }
	    break;
	case TitleText:
	    p->drawText( r.x()+3+metric(HelpWidth,wd), r.top()-th,
		r.width()-metric(OKWidth,wd)-metric(CloseWidth,wd),
		th, QPainter::AlignVCenter, wd->caption);
	    break;
    }
}

void WindowDecorationInterface::drawButton( Button b, QPainter *p, const WindowData *wd, int x, int y, int, int, QWSButton::State state ) const
{
    QImage *img = 0;
    switch ( b ) {
	case OK:
	    img = okImage(metric(TitleHeight,wd));
	    break;
	case Close:
	    img = closeImage(metric(TitleHeight,wd));
	    break;
	case Help:
	    img = helpImage(metric(TitleHeight,wd));
	    break;
	case Maximize:
	    img = maximizeImage(metric(TitleHeight,wd));
	    break;
    }

    if ( img ) {
	if ((state & QWSButton::MouseOver) && (state & QWSButton::Clicked))
	    p->drawImage(x+2, y+2, *img);
	else
	    p->drawImage(x+1, y+1, *img);
    }
}

QRegion WindowDecorationInterface::mask( const WindowData *wd ) const
{
    int th = metric(TitleHeight,wd);
    QRect rect( wd->rect );
    QRect r(rect.left() - metric(LeftBorder,wd),
	    rect.top() - th - metric(TopBorder,wd),
	    rect.width() + metric(LeftBorder,wd) + metric(RightBorder,wd),
	    rect.height() + th + metric(TopBorder,wd) + metric(BottomBorder,wd));
    return QRegion(r) - rect;
}

class DefaultWindowDecoration : public WindowDecorationInterface
{
public:
    DefaultWindowDecoration(){}
    QString name() const {
	return qApp->translate("WindowDecoration", "Default",
		"List box text for default window decoration");
    }
    QPixmap icon() const {
	return QPixmap();
    }
    QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
	*iface = 0;
	if ( uuid == IID_QUnknown )
	    *iface = this;
	else if ( uuid == IID_WindowDecoration )
	    *iface = this;
        else
            return QS_FALSE;

	if ( *iface )
	    (*iface)->addRef();
	return QS_OK;
    }
    Q_REFCOUNT

};

static WindowDecorationInterface *wdiface = 0;
static QLibrary* wdlib = 0;
static QString libname;

//===========================================================================
bool QPEDecoration::helpExists() const
{
    if ( helpFile.isNull() ) {
	QStringList helpPath = Global::helpPath();
	QString hf = QString(qApp->argv()[0]) + ".html";
	bool he = FALSE;
	for (QStringList::ConstIterator it=helpPath.begin(); it!=helpPath.end() && !he; ++it)
	    he = QFile::exists( *it + "/" + hf );
	((QPEDecoration*)this)->helpFile = hf;
	((QPEDecoration*)this)->helpexists = he;
	return he;
    }
    return helpexists;
}

QPEDecoration::QPEDecoration()
    : QWSDefaultDecoration()
{
    if ( wdlib ) {
	delete wdlib;
	wdlib = 0;
    } else {
	delete wdiface;
    }
    wdiface = new DefaultWindowDecoration;

    helpexists = FALSE; // We don't know (flagged by helpFile being null)
    qpeManager = new QPEManager( this );
    imageOk = Resource::loadImage( "OKButton" );
    imageClose = Resource::loadImage( "CloseButton" );
    imageHelp = Resource::loadImage( "HelpButton" );
}

QPEDecoration::QPEDecoration( const QString &plugin )
    : QWSDefaultDecoration()
{
    libname = plugin;

    if ( wdlib ) {
        wdiface->release();
        wdlib->unload();
        delete wdlib;
        wdlib = 0;
    } else {
        delete wdiface;
    }

    WindowDecorationInterface *iface = 0;
    QString path = QPEApplication::qpeDir() + "plugins/decorations/";

#ifdef Q_OS_MACX
    if ( plugin.find( ".dylib" ) > 0 ) {
#else
    if ( plugin.find( ".so" ) > 0 ) {
#endif
        // full library name supplied
        path += plugin;
    } else {
#ifdef Q_OS_MACX
        path += "lib" + plugin.lower() + ".dylib"; // compatibility
#else
        path += "lib" + plugin.lower() + ".so"; // compatibility
#endif
    }

    QLibrary *lib = new QLibrary( path );
    if ( lib->queryInterface( IID_WindowDecoration, (QUnknownInterface**)&iface ) == QS_OK && iface ) {
	wdiface = iface;
	wdlib = lib;
    } else {
	delete lib;
	wdiface = new DefaultWindowDecoration;
    }



    helpexists = FALSE; // We don't know (flagged by helpFile being null)
    qpeManager = new QPEManager( this );
}

QPEDecoration::~QPEDecoration()
{
    delete qpeManager;
}

const char **QPEDecoration::menuPixmap()
{
    return (const char **)0;
}

const char **QPEDecoration::closePixmap()
{
    return (const char **)qpe_close_xpm;
}

const char **QPEDecoration::minimizePixmap()
{
    return (const char **)qpe_accept_xpm;
}

const char **QPEDecoration::maximizePixmap()
{
    return (const char **)0;
}

const char **QPEDecoration::normalizePixmap()
{
    return (const char **)0;
}

int QPEDecoration::getTitleHeight( const QWidget *w )
{
    WindowDecorationInterface::WindowData wd;
    windowData( w, wd );
    return wdiface->metric(WindowDecorationInterface::TitleHeight,&wd);
}

/*
    If rect is empty, no frame is added. (a hack, really)
*/
QRegion QPEDecoration::region(const QWidget *widget, const QRect &rect, QWSDecoration::Region type)
{
    qpeManager->updateActive();

    WindowDecorationInterface::WindowData wd;
    windowData( widget, wd );
    wd.rect = rect;

    int titleHeight = wdiface->metric(WindowDecorationInterface::TitleHeight,&wd);
    int okWidth = wdiface->metric(WindowDecorationInterface::OKWidth,&wd);
    int closeWidth = wdiface->metric(WindowDecorationInterface::CloseWidth,&wd);
    int helpWidth = wdiface->metric(WindowDecorationInterface::HelpWidth,&wd);
    int grab = wdiface->metric(WindowDecorationInterface::CornerGrabSize,&wd);

    QRegion region;

    switch ((int)type) {
	case Menu:
	    break;
	case Maximize:
	    if ( !widget->inherits( "QDialog" ) && qApp->desktop()->width() > 350 ) {
		int maximizeWidth = wdiface->metric(WindowDecorationInterface::MaximizeWidth,&wd);
		int left = rect.right() - maximizeWidth - closeWidth;
		if ( ((DecorHackWidget *)widget)->needsOk() )
		    left -= okWidth;
		QRect r(left, rect.top() - titleHeight, closeWidth, titleHeight);
		region = r;
	    }
	    break;
	case Minimize:
	    if ( ((DecorHackWidget *)widget)->needsOk() ) {
		QRect r(rect.right() - okWidth,
		    rect.top() - titleHeight, okWidth, titleHeight);
		if (r.left() > rect.left() + titleHeight)
		    region = r;
	    }
	    break;
	case Close:
	    {
		int left = rect.right() - closeWidth;
		if ( ((DecorHackWidget *)widget)->needsOk() )
		    left -= okWidth;
		QRect r(left, rect.top() - titleHeight, closeWidth, titleHeight);
		region = r;
	    }
	    break;
    	case Title:
	    if ( !widget->isMaximized() ) {
		int width = rect.width() - helpWidth - closeWidth;
		if ( ((DecorHackWidget *)widget)->needsOk() )
		    width -= okWidth;
		QRect r(rect.left()+helpWidth, rect.top() - titleHeight,
			width, titleHeight);
		if (r.width() > 0)
		    region = r;
	    }
	    break;
	case Help:
	    if ( helpExists() || widget->testWFlags(Qt::WStyle_ContextHelp) ) {
		QRect r(rect.left(), rect.top() - titleHeight,
			  helpWidth, titleHeight);
		region = r;
	    }
	    break;
	case Top:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		int b = wdiface->metric(WindowDecorationInterface::TopBorder,&wd);
		region = m & QRect( br.left()+grab, br.top(),
				    br.width()-2*grab, b );
	    }
	    break;
	case Left:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		int b = wdiface->metric(WindowDecorationInterface::LeftBorder,&wd);
		region = m & QRect( br.left(), br.top()+grab,
				    b, br.height()-2*grab );
	    }
	    break;
	case Right:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		int b = wdiface->metric(WindowDecorationInterface::RightBorder,&wd);
		region = m & QRect( rect.right(), br.top()+grab,
				    b, br.height()-2*grab );
	    }
	    break;
	case Bottom:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		int b = wdiface->metric(WindowDecorationInterface::BottomBorder,&wd);
		region = m & QRect( br.left()+grab, rect.bottom(),
				    br.width()-2*grab, b );
	    }
	    break;
	case TopLeft:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		int tb = wdiface->metric(WindowDecorationInterface::TopBorder,&wd);
		int lb = wdiface->metric(WindowDecorationInterface::LeftBorder,&wd);
		QRegion crgn( br.left(), br.top(), grab, tb );
		crgn |= QRect( br.left(), br.top(), lb, grab );
		region = m & crgn;
	    }
	    break;
	case TopRight:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		int tb = wdiface->metric(WindowDecorationInterface::TopBorder,&wd);
		int rb = wdiface->metric(WindowDecorationInterface::RightBorder,&wd);
		QRegion crgn( br.right()-grab, br.top(), grab, tb );
		crgn |= QRect( br.right()-rb, br.top(), rb, grab );
		region = m & crgn;
	    }
	    break;
	case BottomLeft:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		region = m & QRect( br.left(), br.bottom()-grab, grab, grab );
	    }
	    break;
	case BottomRight:
	    if ( !widget->isMaximized() ) {
		QRegion m = wdiface->mask(&wd);
		QRect br = m.boundingRect();
		region = m & QRect( br.right()-grab, br.bottom()-grab, grab, grab );
	    }
	    break;
	case All:
	    if ( widget->isMaximized() )
		region = QWSDefaultDecoration::region(widget, rect, type);
	    else
		region = wdiface->mask(&wd) - rect;
	    break;
	default:
	    region = QWSDefaultDecoration::region(widget, rect, type);
	    break;
    }

    return region;
}

void QPEDecoration::paint(QPainter *painter, const QWidget *widget)
{
    WindowDecorationInterface::WindowData wd;
    windowData( widget, wd );

    int titleWidth = getTitleWidth(widget);
    int titleHeight = wdiface->metric(WindowDecorationInterface::TitleHeight,&wd);

    QRect rect(widget->rect());

    // title bar rect
    QRect tbr( rect.left(), rect.top() - titleHeight, rect.width(), titleHeight );

#ifndef QT_NO_PALETTE
    QRegion oldClip = painter->clipRegion();
    painter->setClipRegion( oldClip - QRegion( tbr ) );	// reduce flicker
    wdiface->drawArea( WindowDecorationInterface::Border, painter, &wd );
    painter->setClipRegion( oldClip );

    if (titleWidth > 0) {
	const QColorGroup &cg = widget->palette().active();
	QBrush titleBrush;
	QPen   titlePen;

	if ( wd.flags & WindowDecorationInterface::WindowData::Active ) {
	    titleBrush = cg.brush(QColorGroup::Highlight);
	    titlePen   = cg.color(QColorGroup::HighlightedText);
	} else {
	    titleBrush = cg.brush(QColorGroup::Background);
	    titlePen   = cg.color(QColorGroup::Text);
	}

	wdiface->drawArea( WindowDecorationInterface::Title, painter, &wd );

	// Draw caption
	painter->setPen(titlePen);
	QFont f( QApplication::font() );
	f.setWeight( QFont::Bold );
	painter->setFont(f);
	wdiface->drawArea( WindowDecorationInterface::TitleText, painter, &wd );
    }
#endif //QT_NO_PALETTE

    paintButton( painter, widget, (QWSDecoration::Region)Help, 0 );
}

void QPEDecoration::paintButton(QPainter *painter, const QWidget *w,
			QWSDecoration::Region type, int state)
{
    WindowDecorationInterface::Button b;
    switch ((int)type) {
	case Close:
	    b = WindowDecorationInterface::Close;
	    break;
	case Minimize:
	    if ( ((DecorHackWidget *)w)->needsOk() )
		b = WindowDecorationInterface::OK;
	    else if ( helpExists() )
		b = WindowDecorationInterface::Help;
	    else
		return;
	    break;
	case Help:
	    b = WindowDecorationInterface::Help;
	    break;
	case Maximize:
	    b = WindowDecorationInterface::Maximize;
	    break;
	default:
	    return;
    }

    WindowDecorationInterface::WindowData wd;
    windowData( w, wd );

    int titleHeight = wdiface->metric(WindowDecorationInterface::TitleHeight,&wd);
    QRect rect(w->rect());
    QRect tbr( rect.left(), rect.top() - titleHeight, rect.width(), titleHeight );
    QRect brect(region(w, w->rect(), type).boundingRect());

    const QColorGroup &cg = w->palette().active();
    if ( wd.flags & WindowDecorationInterface::WindowData::Active )
	painter->setPen( cg.color(QColorGroup::HighlightedText) );
    else
	painter->setPen( cg.color(QColorGroup::Text) );

    QRegion oldClip = painter->clipRegion();
    painter->setClipRegion( QRect(brect.x(), tbr.y(), brect.width(), tbr.height()) ); // reduce flicker
    wdiface->drawArea( WindowDecorationInterface::Title, painter, &wd );
    wdiface->drawButton( b, painter, &wd, brect.x(), brect.y(), brect.width(), brect.height(), (QWSButton::State)state );
    painter->setClipRegion( oldClip );
}

//#define QPE_DONT_SHOW_TITLEBAR

void QPEDecoration::maximize( QWidget *widget )
{
#ifdef QPE_DONT_SHOW_TITLEBAR
    if ( !widget->inherits( "QDialog" ) ) {
	widget->setGeometry( qt_maxWindowRect );
    } else
#endif
	{
	QWSDecoration::maximize( widget );
    }
}

QPopupMenu *QPEDecoration::menu( const QWidget *, const QPoint & )
{
    QPopupMenu *m = new QPopupMenu();

    m->insertItem(QPEManager::tr("Restore"), (int)Normalize);
    m->insertItem(QPEManager::tr("Move"), (int)Title);
    m->insertItem(QPEManager::tr("Size"), (int)BottomRight);
    m->insertItem(QPEManager::tr("Maximize"), (int)Maximize);
    m->insertSeparator();
    m->insertItem(QPEManager::tr("Close"), (int)Close);

    return m;
}

#ifndef QT_NO_DIALOG
class HackDialog : public QDialog
{
public:
    void acceptIt() {
	if ( isA( "QMessageBox" ) )
	    qApp->postEvent( this, new QKeyEvent( QEvent::KeyPress, Key_Enter, '\n', 0, "\n" ) );
	else
	    accept();
    }
};
#endif


void QPEDecoration::minimize( QWidget *widget )
{
#ifndef QT_NO_DIALOG
    // We use the minimize button as an "accept" button.
    if ( widget->inherits( "QDialog" ) ) {
        HackDialog *d = (HackDialog *)widget;
        d->acceptIt();
    }
#endif
    else if ( ((DecorHackWidget *)widget)->needsOk() ) {
	QSignal s;
	s.connect( widget, SLOT( accept() ) );
	s.activate();
    } else {
	help( widget );
    }
}

void QPEDecoration::help( QWidget *w )
{
    if ( helpExists() ) {
	QString hf = helpFile;
	QString localHelpFile = QString(qApp->argv()[0]) + "-" + w->name() + ".html";
	QStringList helpPath = Global::helpPath();
	for (QStringList::ConstIterator it=helpPath.begin(); it!=helpPath.end(); ++it) {
	    if ( QFile::exists( *it + "/" + localHelpFile ) ) {
		hf = localHelpFile;
		break;
	    }
	}
	Global::execute( "helpbrowser", hf );
    } else if ( w && w->testWFlags(Qt::WStyle_ContextHelp) ) {
	QWhatsThis::enterWhatsThisMode();
        QWhatsThis::leaveWhatsThisMode( QObject::tr(
	    "<Qt>Comprehensive help is not available for this application, "
	    "however there is context-sensitive help.<p>To use context-sensitive help:<p>"
	    "<ol><li>click and hold the help button."
	    "<li>when the title bar shows <b>What's this...</b>, "
	    "click on any control.</ol></Qt>" ) );
    }
}

void QPEDecoration::windowData( const QWidget *w, WindowDecorationInterface::WindowData &wd ) const
{
    wd.rect = w->rect();
    if ( qpeManager->whatsThisWidget() == w )
        wd.caption = QObject::tr( "What's this..." );
    else
	wd.caption = w->caption();
    wd.palette = qApp->palette();
    wd.flags = 0;
    wd.flags |= w->isMaximized() ? WindowDecorationInterface::WindowData::Maximized : 0;
    wd.flags |= w->testWFlags(Qt::WStyle_Dialog) ? WindowDecorationInterface::WindowData::Dialog : 0;
    const QWidget *active = qpeManager->activeWidget();
    wd.flags |= w == active ? WindowDecorationInterface::WindowData::Active : 0;
    wd.reserved = 1;
}

/*
#ifndef QT_NO_POPUPMENU
QPopupMenu *QPEDecoration::menu(QWSManager*, const QWidget*, const QPoint&)
{
    return 0;
}
#endif
*/




#endif // QT_NO_QWS_QPE_WM_STYLE
#endif