summaryrefslogtreecommitdiffstats
path: root/kdeui/kpassivepopup.cpp
blob: 69f2bd1aab833a0cf06d0b3c36f9e805942b93c5 (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
/*
 *   copyright            : (C) 2001-2002 by Richard Moore
 *   copyright            : (C) 2004-2005 by Sascha Cunz
 *   License              : This file is released under the terms of the LGPL, version 2.
 *   email                : [email protected]
 *   email                : [email protected]
 */

#include <kconfig.h>

#include <tqapplication.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqtimer.h>
#include <tqvbox.h>
#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqbitmap.h>
#include <tqpointarray.h>

#include <kdebug.h>
#include <kdialog.h>
#include <kpixmap.h>
#include <kpixmapeffect.h>
#include <kglobalsettings.h>

#include "config.h"
#ifdef Q_WS_X11
#include <netwm.h>
#endif

#include "kpassivepopup.h"
#include "kpassivepopup.moc"

class KPassivePopup::Private
{
public:
  int popupStyle;
  TQPointArray               surround;
  TQPoint                    anchor;
  TQPoint                    fixedPosition;
};

static const int DEFAULT_POPUP_TYPE = KPassivePopup::Boxed;
static const int DEFAULT_POPUP_TIME = 6*1000;
static const int POPUP_FLAGS = Qt::WStyle_Customize | Qt::WDestructiveClose | Qt::WX11BypassWM
                             | Qt::WStyle_StaysOnTop | Qt::WStyle_Tool | Qt::WStyle_NoBorder;

KPassivePopup::KPassivePopup( TQWidget *parent, const char *name, WFlags f )
    : TQFrame( 0, name, f ? f : POPUP_FLAGS ),
      window( parent ? parent->winId() : 0L ), msgView( 0 ), topLayout( 0 ),
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
      m_autoDelete( false )
{
    init( DEFAULT_POPUP_TYPE );
}

KPassivePopup::KPassivePopup( WId win, const char *name, WFlags f )
    : TQFrame( 0, name, f ? f : POPUP_FLAGS ),
      window( win ), msgView( 0 ), topLayout( 0 ),
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
      m_autoDelete( false )
{
    init( DEFAULT_POPUP_TYPE );
}

KPassivePopup::KPassivePopup( int popupStyle, TQWidget *parent, const char *name, WFlags f )
    : TQFrame( 0, name, f ? f : POPUP_FLAGS ),
      window( parent ? parent->winId() : 0L ), msgView( 0 ), topLayout( 0 ),
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
      m_autoDelete( false )
{
    init( popupStyle );
}

KPassivePopup::KPassivePopup( int popupStyle, WId win, const char *name, WFlags f )
    : TQFrame( 0, name, f ? f : POPUP_FLAGS ),
      window( win ), msgView( 0 ), topLayout( 0 ),
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
      m_autoDelete( false )
{
    init( popupStyle );
}

void KPassivePopup::init( int popupStyle )
{
    d = new Private;
    d->popupStyle = popupStyle;
    if( popupStyle == Boxed )
    {
        setFrameStyle( TQFrame::Box| TQFrame::Plain );
        setLineWidth( 2 );
    }
    else if( popupStyle == Balloon )
    {
        setPalette(TQToolTip::palette());
        setAutoMask(TRUE);
    }
    connect( hideTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( hide() ) );
    connect( this, TQT_SIGNAL( clicked() ), TQT_SLOT( hide() ) );
}

KPassivePopup::~KPassivePopup()
{
    delete d;
}

void KPassivePopup::setView( TQWidget *child )
{
    delete msgView;
    msgView = child;

    delete topLayout;
    topLayout = new TQVBoxLayout( this, d->popupStyle == Balloon ? 22 : KDialog::marginHint(), KDialog::spacingHint() );
    topLayout->addWidget( msgView );
    topLayout->activate();
}

void KPassivePopup::setView( const TQString &caption, const TQString &text,
                             const TQPixmap &icon )
{
    // kdDebug() << "KPassivePopup::setView " << caption << ", " << text << endl;
    setView( standardView( caption, text, icon, this ) );
}

TQVBox * KPassivePopup::standardView( const TQString& caption,
                                     const TQString& text,
                                     const TQPixmap& icon,
                                     TQWidget *parent )
{
    TQVBox *vb = new TQVBox( parent ? parent : this );
    vb->setSpacing( KDialog::spacingHint() );

    TQHBox *hb=0;
    if ( !icon.isNull() ) {
	hb = new TQHBox( vb );
	hb->setMargin( 0 );
	hb->setSpacing( KDialog::spacingHint() );
	ttlIcon = new TQLabel( hb, "title_icon" );
	ttlIcon->setPixmap( icon );
        ttlIcon->tqsetAlignment( AlignLeft );
    }

    if ( !caption.isEmpty() ) {
	ttl = new TQLabel( caption, hb ? hb : vb, "title_label" );
	TQFont fnt = ttl->font();
	fnt.setBold( true );
	ttl->setFont( fnt );
	ttl->tqsetAlignment( Qt::AlignHCenter );
        if ( hb )
            hb->setStretchFactor( ttl, 10 ); // enforce centering
    }

    if ( !text.isEmpty() ) {
        msg = new TQLabel( text, vb, "msg_label" );
        msg->tqsetAlignment( AlignLeft );
    }

    return vb;
}

void KPassivePopup::setView( const TQString &caption, const TQString &text )
{
    setView( caption, text, TQPixmap() );
}

void KPassivePopup::setTimeout( int delay )
{
    hideDelay = delay;
    if( hideTimer->isActive() )
    {
        if( delay ) {
            hideTimer->changeInterval( delay );
        } else {
            hideTimer->stop();
        }
    }
}

void KPassivePopup::setAutoDelete( bool autoDelete )
{
    m_autoDelete = autoDelete;
}

void KPassivePopup::mouseReleaseEvent( TQMouseEvent *e )
{
    emit clicked();
    emit clicked( e->pos() );
}

//
// Main Implementation
//

void KPassivePopup::show()
{
    if ( size() != tqsizeHint() )
	resize( tqsizeHint() );

    if ( d->fixedPosition.isNull() )
	positionSelf();
    else {
	if( d->popupStyle == Balloon )
	    setAnchor( d->fixedPosition );
	else
	    move( d->fixedPosition );
    }
    TQFrame::show();

    int delay = hideDelay;
    if ( delay < 0 ) {
        delay = DEFAULT_POPUP_TIME;
    }

    if ( delay > 0 ) {
        hideTimer->start( delay );
    }
}

void KPassivePopup::show(const TQPoint &p)
{
    d->fixedPosition = p;
    show();
}

void KPassivePopup::hideEvent( TQHideEvent * )
{
    hideTimer->stop();
    if ( m_autoDelete )
        deleteLater();
}

TQRect KPassivePopup::defaultArea() const
{
#ifdef Q_WS_X11
    NETRootInfo info( qt_xdisplay(),
                      NET::NumberOfDesktops |
                      NET::CurrentDesktop |
                      NET::WorkArea,
                      -1, false );
    info.activate();
    NETRect workArea = info.workArea( info.currentDesktop() );
    TQRect r;
    r.setRect( workArea.pos.x, workArea.pos.y, 0, 0 ); // top left
#else
    // FIX IT
    TQRect r;
    r.setRect( 100, 100, 200, 200 ); // top left
#endif
    return r;
}

void KPassivePopup::positionSelf()
{
    TQRect target;

#ifdef Q_WS_X11
    if ( !window ) {
        target = defaultArea();
    }

    else {
        NETWinInfo ni( qt_xdisplay(), window, qt_xrootwin(),
                       NET::WMIconGeometry | NET::WMKDESystemTrayWinFor );

        // Figure out where to put the popup. Note that we must handle
        // windows that skip the taskbar cleanly
        if ( ni.kdeSystemTrayWinFor() ) {
            NETRect frame, win;
            ni.kdeGeometry( frame, win );
            target.setRect( win.pos.x, win.pos.y,
                            win.size.width, win.size.height );
        }
        else if ( ni.state() & NET::SkipTaskbar ) {
            target = defaultArea();
        }
        else {
            NETRect r = ni.iconGeometry();
            target.setRect( r.pos.x, r.pos.y, r.size.width, r.size.height );
                if ( target.isNull() ) { // bogus value, use the exact position
                    NETRect dummy;
                    ni.kdeGeometry( dummy, r );
                    target.setRect( r.pos.x, r.pos.y, 
                                    r.size.width, r.size.height);
                }
        }
    }
#else
        target = defaultArea();
#endif
    moveNear( target );
}

void KPassivePopup::moveNear( TQRect target )
{
    TQPoint pos = target.topLeft();
    int x = pos.x();
    int y = pos.y();
    int w = width();
    int h = height();

    TQRect r = KGlobalSettings::desktopGeometry(TQPoint(x+w/2,y+h/2));

    if( d->popupStyle == Balloon )
    {
        // tqfind a point to anchor to
        if( x + w > r.width() ){
            x = x + target.width();
        }

        if( y + h > r.height() ){
            y = y + target.height();
        }
    } else
    {
        if ( x < r.center().x() )
            x = x + target.width();
        else
            x = x - w;

        // It's apparently trying to go off screen, so display it ALL at the bottom.
        if ( (y + h) > r.bottom() )
            y = r.bottom() - h;

        if ( (x + w) > r.right() )
            x = r.right() - w;
    }
    if ( y < r.top() )
        y = r.top();

    if ( x < r.left() )
        x = r.left();

    if( d->popupStyle == Balloon )
        setAnchor( TQPoint( x, y ) );
    else
        move( x, y );
}

void KPassivePopup::setAnchor(const TQPoint &anchor)
{
    d->anchor = anchor;
    updateMask();
}

void KPassivePopup::paintEvent( TQPaintEvent* pe )
{
    if( d->popupStyle == Balloon )
    {
        TQPainter p;
        p.begin( this );
        p.drawPolygon( d->surround );
    } else
        TQFrame::paintEvent( pe );
}

void KPassivePopup::updateMask()
{
    // get screen-tqgeometry for screen our anchor is on
    // (tqgeometry can differ from screen to screen!
    TQRect deskRect = KGlobalSettings::desktopGeometry(d->anchor);

    int xh = 70, xl = 40;
    if( width() < 80 )
        xh = xl = 40;
    else if( width() < 110 )
        xh = width() - 40;

    bool bottom = (d->anchor.y() + height()) > ((deskRect.y() + deskRect.height()-48));
    bool right = (d->anchor.x() + width()) > ((deskRect.x() + deskRect.width()-48));

    TQPoint corners[4] = {
        TQPoint( width() - 50, 10 ),
        TQPoint( 10, 10 ),
        TQPoint( 10, height() - 50 ),
        TQPoint( width() - 50, height() - 50 )
    };

    TQBitmap tqmask( width(), height(), true );
    TQPainter p( &tqmask );
    TQBrush brush( Qt::white, Qt::SolidPattern );
    p.setBrush( brush );

    int i = 0, z = 0;
    for (; i < 4; ++i) {
        TQPointArray corner;
        corner.makeArc(corners[i].x(), corners[i].y(), 40, 40, i * 16 * 90, 16 * 90);

        d->surround.resize( z + corner.count() );
        for (unsigned int s = 0; s < corner.count() - 1; s++) {
            d->surround.setPoint( z++, corner[s] );
        }
		
        if (bottom && i == 2) {
            if (right) {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, TQPoint( width() - xh, height() - 11 ) );
                d->surround.setPoint( z++, TQPoint( width() - 20, height() ) );
                d->surround.setPoint( z++, TQPoint( width() - xl, height() - 11 ) );
            } else {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, TQPoint( xl, height() - 11 ) );
                d->surround.setPoint( z++, TQPoint( 20, height() ) );
                d->surround.setPoint( z++, TQPoint( xh, height() - 11 ) );
            }
        } else if (!bottom && i == 0) {
            if (right) {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, TQPoint( width() - xl, 10 ) );
                d->surround.setPoint( z++, TQPoint( width() - 20, 0 ) );
                d->surround.setPoint( z++, TQPoint( width() - xh, 10 ) );
            } else {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, TQPoint( xh, 10 ) );
                d->surround.setPoint( z++, TQPoint( 20, 0 ) );
                d->surround.setPoint( z++, TQPoint( xl, 10 ) );
            }
        }
    }

    d->surround.resize( z + 1 );
    d->surround.setPoint( z, d->surround[0] );
    p.drawPolygon( d->surround );
    setMask(tqmask);

    move( right ? d->anchor.x() - width() + 20 : ( d->anchor.x() < 11 ? 11 : d->anchor.x() - 20 ),
          bottom ? d->anchor.y() - height() : ( d->anchor.y() < 11 ? 11 : d->anchor.y() ) );

    update();
}

//
// Convenience Methods
//

KPassivePopup *KPassivePopup::message( const TQString &caption, const TQString &text,
				       const TQPixmap &icon,
				       TQWidget *parent, const char *name, int timeout )
{
    return message( DEFAULT_POPUP_TYPE, caption, text, icon, parent, name, timeout );
}

KPassivePopup *KPassivePopup::message( const TQString &text, TQWidget *parent, const char *name )
{
    return message( DEFAULT_POPUP_TYPE, TQString::null, text, TQPixmap(), parent, name );
}

KPassivePopup *KPassivePopup::message( const TQString &caption, const TQString &text,
				       TQWidget *parent, const char *name )
{
    return message( DEFAULT_POPUP_TYPE, caption, text, TQPixmap(), parent, name );
}

KPassivePopup *KPassivePopup::message( const TQString &caption, const TQString &text,
				       const TQPixmap &icon, WId parent, const char *name, int timeout )
{
    return message( DEFAULT_POPUP_TYPE, caption, text, icon, parent, name, timeout );
}

KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &caption, const TQString &text,
				       const TQPixmap &icon,
				       TQWidget *parent, const char *name, int timeout )
{
    KPassivePopup *pop = new KPassivePopup( popupStyle, parent, name );
    pop->setAutoDelete( true );
    pop->setView( caption, text, icon );
    pop->hideDelay = timeout;
    pop->show();

    return pop;
}

KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &text, TQWidget *parent, const char *name )
{
    return message( popupStyle, TQString::null, text, TQPixmap(), parent, name );
}

KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &caption, const TQString &text,
				       TQWidget *parent, const char *name )
{
    return message( popupStyle, caption, text, TQPixmap(), parent, name );
}

KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &caption, const TQString &text,
				       const TQPixmap &icon, WId parent, const char *name, int timeout )
{
    KPassivePopup *pop = new KPassivePopup( popupStyle, parent, name );
    pop->setAutoDelete( true );
    pop->setView( caption, text, icon );
    pop->hideDelay = timeout;
    pop->show();

    return pop;
}

// Local Variables:
// c-basic-offset: 4
// End: