summaryrefslogtreecommitdiffstats
path: root/kicker/applets/minipager/pagerapplet.cpp
blob: 6ab7febf5227b9180adcfac30fcf9ac399cc8428 (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
944
945
946
947
948
949
/*****************************************************************

Copyright (c) 1996-2000 the kicker authors. See file AUTHORS.

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
AUTHORS 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.

******************************************************************/

#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqlineedit.h>
#include <tqpopupmenu.h>
#include <tqlayout.h>
#include <tqbuttongroup.h>

#include <dcopref.h>
#include <kglobalsettings.h>
#include <twin.h>
#include <twinmodule.h>
#include <kapplication.h>
#include <kglobal.h>
#include <klocale.h>
#include <kdebug.h>
#include <kprocess.h>
#include <kpopupmenu.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <dcopclient.h>
#include <netwm.h>
#include <kmanagerselection.h>

#include "global.h"
#include "kickertip.h"
#include "kickerSettings.h"
#include "kshadowengine.h"
#include "kshadowsettings.h"
#include "paneldrag.h"
#include "taskmanager.h"

#include "pagerapplet.h"
#include "pagerapplet.moc"

#ifdef FocusOut
#undef FocusOut
#endif

const int knDesktopPreviewSize = 12;
const int knBtnSpacing = 1;

// The previews tend to have a 4/3 aspect ratio
static const int smallHeight = 32;
static const int smallWidth = 42;

// config menu id offsets
static const int rowOffset = 2000;
static const int labelOffset = 200;
static const int bgOffset = 300;

extern "C"
{
    KDE_EXPORT KPanelApplet* init(TQWidget *parent, const TQString& configFile)
    {
      KGlobal::locale()->insertCatalogue("kminipagerapplet");
      return new KMiniPager(configFile, KPanelApplet::Normal, 0, parent, "kminipagerapplet");
    }
}

KMiniPager::KMiniPager(const TQString& configFile, Type type, int actions,
                       TQWidget *parent, const char *name)
    : KPanelApplet( configFile, type, actions, parent, name ),
      m_layout(0),
      m_desktopLayoutOwner( NULL ),
      m_shadowEngine(0),
      m_contextMenu(0),
      m_settings( new PagerSettings(sharedConfig()) )
{
    setBackgroundOrigin( AncestorOrigin );
    int scnum = TQApplication::desktop()->screenNumber(this);
    TQRect desk = TQApplication::desktop()->screenGeometry(scnum);
    if (desk.width() <= 800)
    {
        KConfigSkeleton::ItemBool* item = dynamic_cast<KConfigSkeleton::ItemBool*>(m_settings->findItem("Preview"));
        if (item)
        {
            item->setDefaultValue(false);
        }
    }
    m_settings->readConfig();
    m_windows.setAutoDelete(true);
    if (m_settings->preview())
    {
        TaskManager::the()->trackGeometry();
    }

    m_group = new TQButtonGroup(this);
    m_group->setBackgroundOrigin(AncestorOrigin);
    m_group->setFrameStyle(TQFrame::NoFrame);
    m_group->setExclusive( true );

    setFont( KGlobalSettings::taskbarFont() );

    m_twin = new KWinModule(TQT_TQOBJECT(this));
    m_activeWindow = m_twin->activeWindow();
    m_curDesk = m_twin->currentDesktop();

    if (m_curDesk == 0) // twin not yet launched
    {
        m_curDesk = 1;
    }

    desktopLayoutOrientation = Qt::Horizontal;
    desktopLayoutX = -1;
    desktopLayoutY = -1;

    TQSize s(m_twin->numberOfViewports(m_twin->currentDesktop()));
    m_useViewports = s.width() * s.height() > 1;

    drawButtons();

    connect( m_twin, TQT_SIGNAL( currentDesktopChanged(int)), TQT_SLOT( slotSetDesktop(int) ) );
    connect( m_twin, TQT_SIGNAL( currentDesktopViewportChanged(int, const TQPoint&)), TQT_SLOT(slotSetDesktopViewport(int, const TQPoint&)));
    connect( m_twin, TQT_SIGNAL( numberOfDesktopsChanged(int)), TQT_SLOT( slotSetDesktopCount(int) ) );
    connect( m_twin, TQT_SIGNAL( desktopGeometryChanged(int)), TQT_SLOT( slotRefreshViewportCount(int) ) );
    connect( m_twin, TQT_SIGNAL( activeWindowChanged(WId)), TQT_SLOT( slotActiveWindowChanged(WId) ) );
    connect( m_twin, TQT_SIGNAL( windowAdded(WId) ), this, TQT_SLOT( slotWindowAdded(WId) ) );
    connect( m_twin, TQT_SIGNAL( windowRemoved(WId) ), this, TQT_SLOT( slotWindowRemoved(WId) ) );
    connect( m_twin, TQT_SIGNAL( windowChanged(WId,unsigned int) ), this, TQT_SLOT( slotWindowChanged(WId,unsigned int) ) );
    connect( m_twin, TQT_SIGNAL( desktopNamesChanged() ), this, TQT_SLOT( slotDesktopNamesChanged() ) );
    connect( kapp, TQT_SIGNAL(backgroundChanged(int)), TQT_SLOT(slotBackgroundChanged(int)) );

    if (kapp->authorizeKAction("kicker_rmb") && kapp->authorizeControlModule("tde-kcmtaskbar.desktop"))
    {
        m_contextMenu = new TQPopupMenu();
        connect(m_contextMenu, TQT_SIGNAL(aboutToShow()), TQT_SLOT(aboutToShowContextMenu()));
        connect(m_contextMenu, TQT_SIGNAL(activated(int)), TQT_SLOT(contextMenuActivated(int)));
        setCustomMenu(m_contextMenu);
    }

    TQValueList<WId>::ConstIterator it;
    TQValueList<WId>::ConstIterator itEnd = m_twin->windows().end();
    for ( it = m_twin->windows().begin(); it != itEnd; ++it)
    {
        slotWindowAdded( (*it) );
    }

    slotSetDesktop( m_curDesk );
    updateLayout();
}

KMiniPager::~KMiniPager()
{
    KGlobal::locale()->removeCatalogue("kminipagerapplet");
    delete m_contextMenu;
    delete m_settings;
    delete m_shadowEngine;
}

void KMiniPager::slotBackgroundChanged(int desk)
{
    unsigned numDesktops = m_twin->numberOfDesktops();
    if (numDesktops != m_desktops.count())
    {
        slotSetDesktopCount(numDesktops);
    }

    if (desk < 1 || (unsigned) desk > m_desktops.count())
    {
        // should not happen, but better to be paranoid than crash
        return;
    }

    m_desktops[desk - 1]->backgroundChanged();
}

void KMiniPager::slotSetDesktop(int desktop)
{
    if (m_twin->numberOfDesktops() > static_cast<int>(m_desktops.count()))
    {
        slotSetDesktopCount( m_twin->numberOfDesktops() );
    }

    if (!m_useViewports && (desktop != KWin::currentDesktop()))
    {
        // this can happen when the user clicks on a desktop,
        // holds down the key combo to switch desktops, lets the
        // mouse go but keeps the key combo held. the desktop will switch
        // back to the desktop associated with the key combo and then it
        // becomes a race condition between twin's signal and the button's
        // signal. usually twin wins.
        return;
    }

    m_curDesk = desktop;
    if (m_curDesk < 1)
    {
        m_curDesk = 1;
    }

    KMiniPagerButton* button = m_desktops[m_curDesk - 1];
    if (!button->isOn())
    {
        button->toggle();
    }
}

void KMiniPager::slotSetDesktopViewport(int desktop, const TQPoint& viewport)
{
    // ###
    Q_UNUSED(desktop);
    TQSize s(m_twin->numberOfViewports(m_twin->currentDesktop()));
    slotSetDesktop((viewport.y()-1) * s.width() + viewport.x() );
}

void KMiniPager::slotButtonSelected( int desk )
{
    if (m_twin->numberOfViewports(m_twin->currentDesktop()).width() *
        m_twin->numberOfViewports(m_twin->currentDesktop()).height() > 1)
    {
        TQPoint p;

        p.setX( (desk-1) * TQApplication::desktop()->width());
        p.setY( 0 );

        KWin::setCurrentDesktopViewport(m_twin->currentDesktop(), p);
    }
    else
        KWin::setCurrentDesktop( desk );

    slotSetDesktop( desk );
}

int KMiniPager::widthForHeight(int h) const
{
    if (orientation() == Qt::Vertical)
    {
        return width();
    }

    int deskNum = m_twin->numberOfDesktops() * m_twin->numberOfViewports(0).width()
        * m_twin->numberOfViewports(0).height();

    int rowNum = m_settings->numberOfRows();
    if (rowNum == 0)
    {
        if (h <= 32 || deskNum <= 1)
        {
            rowNum = 1;
        }
        else
        {
            rowNum = 2;
        }
    }

    int deskCols = deskNum/rowNum;
    if(deskNum == 0 || deskNum % rowNum != 0)
        deskCols += 1;

    int bw = (h / rowNum);
    if( m_settings->labelType() != PagerSettings::EnumLabelType::LabelName )
    {
        if (desktopPreview() || m_settings->backgroundType() == PagerSettings::EnumBackgroundType::BgLive)
        {
            bw = (int) ( bw * (double) TQApplication::desktop()->width() / TQApplication::desktop()->height() );
        }
    }
    else
    {
        // scale to desktop width as a minimum
        bw = (int) (bw * (double) TQApplication::desktop()->width() / TQApplication::desktop()->height());
        TQFontMetrics fm = fontMetrics();
        for (int i = 1; i <= deskNum; i++)
        {
            int sw = fm.width( m_twin->desktopName( i ) ) + 8;
            if (sw > bw)
            {
                bw = sw;
            }
        }
    }

    // we add one to the width for the spacing in between the buttons
    // however, the last button doesn't have a space on the end of it (it's
    // only _between_ buttons) so we then remove that one pixel
    return (deskCols * (bw + 1)) - 1;
}

int KMiniPager::heightForWidth(int w) const
{
    if (orientation() == Qt::Horizontal)
    {
        return height();
    }

    int deskNum = m_twin->numberOfDesktops() * m_twin->numberOfViewports(0).width()
        * m_twin->numberOfViewports(0).height();
    int rowNum = m_settings->numberOfRows(); // actually these are columns now... oh well.
    if (rowNum == 0)
    {
        if (w <= 48 || deskNum == 1)
        {
            rowNum = 1;
        }
        else
        {
            rowNum = 2;
        }
    }

    int deskCols = deskNum/rowNum;
    if(deskNum == 0 || deskNum % rowNum != 0)
    {
        deskCols += 1;
    }

    int bh = (w/rowNum) + 1;
    if ( desktopPreview() )
    {
        bh = (int) ( bh *  (double) TQApplication::desktop()->height() / TQApplication::desktop()->width() );
    }
    else if ( m_settings->labelType() == PagerSettings::EnumLabelType::LabelName )
    {
       bh = fontMetrics().lineSpacing() + 8;
    }

    // we add one to the width for the spacing in between the buttons
    // however, the last button doesn't have a space on the end of it (it's
    // only _between_ buttons) so we then remove that one pixel
    int nHg = (deskCols * (bh + 1)) - 1;

    return nHg;
}

void KMiniPager::updateDesktopLayout(int o, int x, int y)
{
    if ((desktopLayoutOrientation == o) &&
       (desktopLayoutX == x) &&
       (desktopLayoutY == y))
    {
      return;
    }

    desktopLayoutOrientation = o;
    desktopLayoutX = x;
    desktopLayoutY = y;
    if( x == -1 ) // do-the-maths-yourself is encoded as 0 in the wm spec
        x = 0;
    if( y == -1 )
        y = 0;
    if( m_desktopLayoutOwner == NULL )
    { // must own manager selection before setting global desktop layout
        int screen = DefaultScreen( tqt_xdisplay());
        m_desktopLayoutOwner = new KSelectionOwner( TQString( "_NET_DESKTOP_LAYOUT_S%1" ).arg( screen ).latin1(),
            screen, TQT_TQOBJECT(this) );
        if( !m_desktopLayoutOwner->claim( false ))
        {
            delete m_desktopLayoutOwner;
            m_desktopLayoutOwner = NULL;
            return;
        }
    }
    NET::Orientation orient = o == Qt::Horizontal ? NET::OrientationHorizontal : NET::OrientationVertical;
    NETRootInfo i( tqt_xdisplay(), 0 );
    i.setDesktopLayout( orient, x, y, NET::DesktopLayoutCornerTopLeft );
}

void KMiniPager::resizeEvent(TQResizeEvent*)
{
    bool horiz = orientation() == Qt::Horizontal;

    int deskNum = m_desktops.count();
    int rowNum = m_settings->numberOfRows();
    if (rowNum == 0)
    {
        if (((horiz && height()<=32)||(!horiz && width()<=48)) || deskNum <= 1)
            rowNum = 1;
        else
            rowNum = 2;
    }

    int deskCols = deskNum/rowNum;
    if(deskNum == 0 || deskNum % rowNum != 0)
        deskCols += 1;

    if (m_layout)
    {
        delete m_layout;
        m_layout = 0;
    }

    int nDX, nDY;
    if (horiz)
    {
        nDX = rowNum;
        nDY = deskCols;
        updateDesktopLayout(Qt::Horizontal, -1, nDX);
    }
    else
    {
        nDX = deskCols;
        nDY = rowNum;
        updateDesktopLayout(Qt::Horizontal, nDY, -1);
    }

    // 1 pixel spacing.
    m_layout = new TQGridLayout(this, nDX, nDY, 0, 1);

    TQValueList<KMiniPagerButton*>::Iterator it = m_desktops.begin();
    TQValueList<KMiniPagerButton*>::Iterator itEnd = m_desktops.end();
    int c = 0,
        r = 0;
    while( it != itEnd ) {
        c = 0;
        while( (it != itEnd) && (c < nDY) ) {
            m_layout->addWidget( *it, r, c );
            ++it;
            ++c;
        }
        ++r;
    }

    m_layout->activate();
    updateGeometry();
}

void KMiniPager::wheelEvent( TQWheelEvent* e )
{
    int newDesk;
    int desktops = KWin::numberOfDesktops();
   

    if(cycleWindow()){

    if (m_twin->numberOfViewports(0).width() * m_twin->numberOfViewports(0).height() > 1 )
        desktops = m_twin->numberOfViewports(0).width() * m_twin->numberOfViewports(0).height();
    if (e->delta() < 0)
    {
        newDesk = m_curDesk % desktops + 1;
    }
    else
    {
        newDesk = (desktops + m_curDesk - 2) % desktops + 1;
    }
  
    slotButtonSelected(newDesk);
    }
}

void KMiniPager::drawButtons()
{
    int deskNum = m_twin->numberOfDesktops();
    KMiniPagerButton *desk;

    int count = 1;
    int i = 1;
    do
    {
        TQSize viewportNum = m_twin->numberOfViewports(i);
        for (int j = 1; j <= viewportNum.width() * viewportNum.height(); ++j)
        {
            TQSize s(m_twin->numberOfViewports(m_twin->currentDesktop()));
            TQPoint viewport( (j-1) % s.width(), (j-1) / s.width());
            desk = new KMiniPagerButton( count, m_useViewports, viewport, this );
            if ( m_settings->labelType() != PagerSettings::EnumLabelType::LabelName )
            {
                TQToolTip::add( desk, desk->desktopName() );
            }

            m_desktops.append( desk );
            m_group->insert( desk, count );

            connect(desk, TQT_SIGNAL(buttonSelected(int)),
                    TQT_SLOT(slotButtonSelected(int)) );
            connect(desk, TQT_SIGNAL(showMenu(const TQPoint&, int )),
                    TQT_SLOT(slotShowMenu(const TQPoint&, int )) );

            desk->show();
            ++count;
        }
    }
    while ( ++i <= deskNum );
}

void KMiniPager::slotSetDesktopCount( int )
{
    TQSize s(m_twin->numberOfViewports(m_twin->currentDesktop()));
    m_useViewports = s.width() * s.height() > 1;

    TQValueList<KMiniPagerButton*>::ConstIterator it;
    TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
    for( it = m_desktops.begin(); it != itEnd; ++it )
    {
        delete (*it);
    }
    m_desktops.clear();

    drawButtons();

    m_curDesk = m_twin->currentDesktop();
    if ( m_curDesk == 0 )
    {
        m_curDesk = 1;
    }

    resizeEvent(0);
    updateLayout();
}

void KMiniPager::slotRefreshViewportCount( int )
{
    TQSize s(m_twin->numberOfViewports(m_twin->currentDesktop()));
    m_useViewports = s.width() * s.height() > 1;

    TQValueList<KMiniPagerButton*>::ConstIterator it;
    TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
    for( it = m_desktops.begin(); it != itEnd; ++it )
    {
        delete (*it);
    }
    m_desktops.clear();

    drawButtons();

    m_curDesk = m_twin->currentDesktop();
    if ( m_curDesk == 0 )
    {
        m_curDesk = 1;
    }

    resizeEvent(0);
    updateLayout();
}

void KMiniPager::slotActiveWindowChanged( WId win )
{
    if (desktopPreview())
    {
        KWin::WindowInfo* inf1 = m_activeWindow ? info( m_activeWindow ) : NULL;
        KWin::WindowInfo* inf2 = win ? info( win ) : NULL;
        m_activeWindow = win;

        TQValueList<KMiniPagerButton*>::ConstIterator it;
        TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
        for ( it = m_desktops.begin(); it != itEnd; ++it)
        {
            if ( ( inf1 && (*it)->shouldPaintWindow(inf1)) ||
                 ( inf2 && (*it)->shouldPaintWindow(inf2)) )
            {
                (*it)->windowsChanged();
            }
        }
    }
}

void KMiniPager::slotWindowAdded( WId win)
{
    if (desktopPreview())
    {
        KWin::WindowInfo* inf = info( win );

        if (inf->state() & NET::SkipPager)
        {
            return;
        }

        TQValueList<KMiniPagerButton*>::ConstIterator it;
        TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
        for ( it = m_desktops.begin(); it != itEnd; ++it)
        {
            if ( (*it)->shouldPaintWindow(inf) )
            {
                (*it)->windowsChanged();
            }
        }
    }
}

void KMiniPager::slotWindowRemoved(WId win)
{
    if (desktopPreview())
    {
        KWin::WindowInfo* inf = info(win);
        bool onAllDesktops = inf->onAllDesktops();
        bool onAllViewports = inf->hasState(NET::Sticky);
        bool skipPager = inf->state() & NET::SkipPager;
        int desktop = inf->desktop();

        if (win == m_activeWindow)
            m_activeWindow = 0;

        m_windows.remove((long) win);

        if (skipPager)
        {
            return;
        }

        TQValueList<KMiniPagerButton*>::ConstIterator it;
        TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
        for (it = m_desktops.begin(); it != itEnd; ++it)
        {
            if (onAllDesktops || onAllViewports || desktop == (*it)->desktop())
            {
                (*it)->windowsChanged();
            }
        }
    }
    else
    {
        m_windows.remove(win);
        return;
    }

}

void KMiniPager::slotWindowChanged( WId win , unsigned int properties )
{
    if ((properties & (NET::WMState | NET::XAWMState | NET::WMDesktop)) == 0 &&
        (!desktopPreview() || (properties & NET::WMGeometry) == 0) &&
        !(desktopPreview() && windowIcons() &&
         (properties & NET::WMIcon | NET::WMIconName | NET::WMVisibleIconName) == 0))
    {
        return;
    }

    if (desktopPreview())
    {
        KWin::WindowInfo* inf = m_windows[win];
        bool skipPager = inf->hasState(NET::SkipPager);
        TQMemArray<bool> old_shouldPaintWindow(m_desktops.size());
        TQValueList<KMiniPagerButton*>::ConstIterator it;
        TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
        int i = 0;
        for ( it = m_desktops.begin(); it != itEnd; ++it)
        {
            old_shouldPaintWindow[i++] = (*it)->shouldPaintWindow(inf);
        }

        m_windows.remove(win);
        inf = info(win);

        if (inf->hasState(NET::SkipPager) || skipPager)
        {
            return;
        }

        for ( i = 0, it = m_desktops.begin(); it != itEnd; ++it)
        {
            if ( old_shouldPaintWindow[i++] || (*it)->shouldPaintWindow(inf))
            {
                (*it)->windowsChanged();
            }
        }
    }
    else
    {
        m_windows.remove(win);
        return;
    }
}

KWin::WindowInfo* KMiniPager::info( WId win )
{
    if (!m_windows[win])
    {
        KWin::WindowInfo* info = new KWin::WindowInfo( win,
            NET::WMWindowType | NET::WMState | NET::XAWMState | NET::WMDesktop | NET::WMGeometry | NET::WMKDEFrameStrut, 0 );

        m_windows.insert(win, info);
        return info;
    }

    return m_windows[win];
}

KTextShadowEngine* KMiniPager::shadowEngine()
{
    if (!m_shadowEngine)
        m_shadowEngine = new KTextShadowEngine();

    return m_shadowEngine;
}

void KMiniPager::refresh()
{
    TQValueList<KMiniPagerButton*>::ConstIterator it;
    TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
    for ( it = m_desktops.begin(); it != itEnd; ++it)
    {
        (*it)->update();
    }
}

void KMiniPager::aboutToShowContextMenu()
{
    m_contextMenu->clear();

    m_contextMenu->insertItem(SmallIcon("kpager"), i18n("&Launch Pager"), LaunchExtPager);
    m_contextMenu->insertSeparator();

    m_contextMenu->insertItem(i18n("&Rename Desktop \"%1\"")
                                   .arg(twin()->desktopName(m_rmbDesk)), RenameDesktop);
    m_contextMenu->insertSeparator();

    KPopupMenu* showMenu = new KPopupMenu(m_contextMenu);
    showMenu->setCheckable(true);
    showMenu->insertTitle(i18n("Pager Layout"));

    TQPopupMenu* rowMenu = new TQPopupMenu(showMenu);
    rowMenu->setCheckable(true);
    rowMenu->insertItem(i18n("&Automatic"), 0 + rowOffset);
    rowMenu->insertItem(i18n("one row or column", "&1"), 1 + rowOffset);
    rowMenu->insertItem(i18n("two rows or columns", "&2"), 2 + rowOffset);
    rowMenu->insertItem( i18n("three rows or columns", "&3"), 3 + rowOffset);
    connect(rowMenu, TQT_SIGNAL(activated(int)), TQT_SLOT(contextMenuActivated(int)));
    showMenu->insertItem((orientation()==Qt::Horizontal) ? i18n("&Rows"):
                                                       i18n("&Columns"),
                         rowMenu);

    showMenu->insertItem(i18n("&Window Thumbnails"), WindowThumbnails);
    showMenu->insertItem(i18n("&Window Icons"), WindowIcons);
    showMenu->insertItem(i18n("&Cycle on Wheel"), Cycle);

    showMenu->insertTitle(i18n("Text Label"));
    showMenu->insertItem(i18n("Desktop N&umber"),
                         PagerSettings::EnumLabelType::LabelNumber + labelOffset);
    showMenu->insertItem(i18n("Desktop N&ame"),
                         PagerSettings::EnumLabelType::LabelName + labelOffset);
    showMenu->insertItem(i18n("N&o Label"),
                         PagerSettings::EnumLabelType::LabelNone + labelOffset);

    showMenu->insertTitle(i18n("Background"));
    showMenu->insertItem(i18n("&Elegant"),
                         PagerSettings::EnumBackgroundType::BgPlain + bgOffset);
    showMenu->insertItem(i18n("&Transparent"),
                         PagerSettings::EnumBackgroundType::BgTransparent + bgOffset);
    if (m_useViewports == false) {
        showMenu->insertItem(i18n("&Desktop Wallpaper"),
                         PagerSettings::EnumBackgroundType::BgLive + bgOffset);
    }
    connect(showMenu, TQT_SIGNAL(activated(int)), TQT_SLOT(contextMenuActivated(int)));
    m_contextMenu->insertItem(i18n("&Pager Options"),showMenu);

    m_contextMenu->insertItem(SmallIcon("configure"),
                              i18n("&Configure Desktops..."),
                              ConfigureDesktops);

    rowMenu->setItemChecked(m_settings->numberOfRows() + rowOffset, true);
    m_contextMenu->setItemChecked(m_settings->labelType() + labelOffset, showMenu);
    m_contextMenu->setItemChecked(m_settings->backgroundType() + bgOffset, showMenu);

    m_contextMenu->setItemChecked(WindowThumbnails, m_settings->preview());
    m_contextMenu->setItemChecked(WindowIcons, m_settings->icons());
    m_contextMenu->setItemChecked(Cycle, m_settings->cycle());
    m_contextMenu->setItemEnabled(WindowIcons, m_settings->preview());
    m_contextMenu->setItemEnabled(RenameDesktop,
                                  m_settings->labelType() ==
                                  PagerSettings::EnumLabelType::LabelName);
}

void KMiniPager::slotShowMenu(const TQPoint& pos, int desktop)
{
    if (!m_contextMenu)
    {
        return;
    }

    m_rmbDesk = desktop;
    m_contextMenu->exec(pos);
    m_rmbDesk = -1;
}

void KMiniPager::contextMenuActivated(int result)
{
    if (result < 1)
    {
        return;
    }

    switch (result)
    {
        case LaunchExtPager:
            showPager();
            return;

        case ConfigureDesktops:
            kapp->startServiceByDesktopName("desktop");
            return;

        case RenameDesktop:
            m_desktops[(m_rmbDesk == -1) ? m_curDesk - 1 : m_rmbDesk - 1]->rename();
            return;
    }

    if (result >= rowOffset)
    {
        m_settings->setNumberOfRows(result - rowOffset);
        resizeEvent(0);
    }

    switch (result)
    {
        case WindowThumbnails:
            m_settings->setPreview(!m_settings->preview());
            TaskManager::the()->trackGeometry();
            break;
        case Cycle:
             m_settings->setCycle(!m_settings->cycle());
            break;
        case WindowIcons:
            m_settings->setIcons(!m_settings->icons());
            break;
        case PagerSettings::EnumBackgroundType::BgPlain + bgOffset:
            m_settings->setBackgroundType(PagerSettings::EnumBackgroundType::BgPlain);
            break;
        case PagerSettings::EnumBackgroundType::BgTransparent + bgOffset:
            m_settings->setBackgroundType(PagerSettings::EnumBackgroundType::BgTransparent);
            break;
        case PagerSettings::EnumBackgroundType::BgLive + bgOffset:
        {
           if (m_useViewports == false) {
                m_settings->setBackgroundType(PagerSettings::EnumBackgroundType::BgLive);
                TQValueList<KMiniPagerButton*>::ConstIterator it;
                TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();
                for( it = m_desktops.begin(); it != itEnd; ++it )
                {
                    (*it)->backgroundChanged();
                }
           }
           else {
               m_settings->setBackgroundType(PagerSettings::EnumBackgroundType::BgTransparent);
           }
            break;
        }

        case PagerSettings::EnumLabelType::LabelNone + labelOffset:
            m_settings->setLabelType(PagerSettings::EnumLabelType::LabelNone);
            break;
        case PagerSettings::EnumLabelType::LabelNumber + labelOffset:
            m_settings->setLabelType(PagerSettings::EnumLabelType::LabelNumber);
            break;
        case PagerSettings::EnumLabelType::LabelName + labelOffset:
            m_settings->setLabelType(PagerSettings::EnumLabelType::LabelName);
            break;
    }

    m_settings->writeConfig();
    updateGeometry();
    refresh();
}

void KMiniPager::slotDesktopNamesChanged()
{
    TQValueList<KMiniPagerButton*>::ConstIterator it = m_desktops.begin();
    TQValueList<KMiniPagerButton*>::ConstIterator itEnd = m_desktops.end();

    for (int i = 1; it != itEnd; ++it, ++i)
    {
        TQString name = m_twin->desktopName(i);
        (*it)->setDesktopName(name);
        (*it)->repaint();
        TQToolTip::remove((*it));
        TQToolTip::add((*it), name);
    }

    updateLayout();
}

void KMiniPager::showPager()
{
    DCOPClient *dcop=kapp->dcopClient();

    if (dcop->isApplicationRegistered("kpager"))
    {
       showKPager(true);
    }
    else
    {
    // Let's run kpager if it isn't running
        connect( dcop, TQT_SIGNAL( applicationRegistered(const TQCString &) ), this, TQT_SLOT(applicationRegistered(const TQCString &)) );
        dcop->setNotifications(true);
        TQString strAppPath(locate("exe", "kpager"));
        if (!strAppPath.isEmpty())
        {
            KProcess process;
            process << strAppPath;
            process << "--hidden";
            process.start(KProcess::DontCare);
        }
    }
}

void KMiniPager::showKPager(bool toggleShow)
{
    TQPoint pt;
    switch ( position() )
    {
        case pTop:
            pt = mapToGlobal( TQPoint(x(), y() + height()) );
            break;
        case pLeft:
            pt = mapToGlobal( TQPoint(x() + width(), y()) );
            break;
        case pRight:
        case pBottom:
        default:
            pt=mapToGlobal( TQPoint(x(), y()) );
    }

    DCOPClient *dcop=kapp->dcopClient();

    TQByteArray data;
    TQDataStream arg(data, IO_WriteOnly);
    arg << pt.x() << pt.y() ;
    if (toggleShow)
    {
        dcop->send("kpager", "KPagerIface", "toggleShow(int,int)", data);
    }
    else
    {
        dcop->send("kpager", "KPagerIface", "showAt(int,int)", data);
    }
}

void KMiniPager::applicationRegistered( const TQCString  & appName )
{
    if (appName == "kpager")
    {
        disconnect( kapp->dcopClient(), TQT_SIGNAL( applicationRegistered(const TQCString &) ),
                    this, TQT_SLOT(applicationRegistered(const TQCString &)) );
        showKPager(false);
    }
}