summaryrefslogtreecommitdiffstats
path: root/tqtinterface/qt4/src/widgets/tqwidgetstack.cpp
blob: 93be95fff75d210aaa3ad6a3cb27903213ee04b8 (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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
#include "tqtglobaldefines.h"

#ifdef USE_QT4

/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "tqwidgetstack.h"
#include "tqlayout.h"
#include "private/tqlayoutengine_p.h"
#include "Qt/qapplication.h"
#include "Qt/qpainter.h"

QT_BEGIN_NAMESPACE

using namespace Qt;

class TQWidgetStackPrivate {
public:
    class Invisible: public TQWidget
    {
    public:
	    Invisible(TQWidgetStack * parent): TQWidget(parent, "qt_invisible_widgetstack")
	    {
                setBackgroundMode(TQt::NoBackground);
	    }
	    const char * className() const
	    {
	        return "TQWidgetStackPrivate::Invisible";
	    }
    protected:
            void paintEvent(QPaintEvent *)
            {
                QPainter(this).eraseRect(rect());
            }
    };

    int nextNegativeID;
    int nextPositiveID;
};



/*!
    \class TQWidgetStack
    \brief The TQWidgetStack class provides a stack of widgets of which
    only the top widget is user-visible.

    \compat

    The application programmer can move any widget to the top of the
    stack at any time using raiseWidget(), and add or remove widgets
    using addWidget() and removeWidget(). It is not sufficient to pass
    the widget stack as parent to a widget which should be inserted into
    the widgetstack.

    visibleWidget() is the \e get equivalent of raiseWidget(); it
    returns a pointer to the widget that is currently at the top of
    the stack.

    TQWidgetStack also provides the ability to manipulate widgets
    through application-specified integer IDs. You can also translate
    from widget pointers to IDs using id() and from IDs to widget
    pointers using widget(). These numeric IDs are unique (per
    TQWidgetStack, not globally), but TQWidgetStack does not attach any
    additional meaning to them.

    The default widget stack is frameless, but you can use the usual
    TQFrame functions (such as setFrameStyle()) to add a frame.

    TQWidgetStack provides a signal, aboutToShow(), which is emitted
    just before a managed widget is shown.

    \sa TQTabDialog QTabWidget QTabBar TQFrame
*/


/*!
  Constructs an empty widget stack.

  The \a parent, \a name and \a f arguments are passed to the TQFrame
  constructor.
*/
TQWidgetStack::TQWidgetStack(TQWidget * parent, const char *name, WFlags f)
    : TQFrame(parent, name, f) //## merge constructors in 4.0
{
    init();
}

void TQWidgetStack::init()
{
   d = new TQWidgetStackPrivate();
   d->nextNegativeID = -2;
   d->nextPositiveID = 0;
   dict = new TQIntDict<TQWidget>;
   focusWidgets = 0;
   topWidget = 0;
   invisible = 0;
   invisible = new TQWidgetStackPrivate::Invisible(this);
   invisible->hide();
}


/*!
    Destroys the object and frees any allocated resources.
*/

TQWidgetStack::~TQWidgetStack()
{
    delete focusWidgets;
    delete d;
    delete dict;
}


/*!
    Adds widget \a w to this stack of widgets, with ID \a id.

    If you pass an id \>= 0 this ID is used. If you pass an \a id of
    -1 (the default), the widgets will be numbered automatically. If
    you pass -2 a unique negative integer will be generated. No widget
    has an ID of -1. Returns the ID or -1 on failure (e.g. \a w is 0).

    If you pass an id that is already used, then a unique negative
    integer will be generated to prevent two widgets having the same
    id.

    If \a w already exists in the stack the widget will be removed first.

    If \a w is not a child of this TQWidgetStack moves it using
    reparent().
*/

int TQWidgetStack::addWidget(TQWidget * w, int id)
{
    if (!w || w == invisible || invisible == 0)
        return -1;

    // prevent duplicates
    removeWidget(w);

    if (id >= 0 && dict->find(id))
        id = -2;
    if (id < -1)
        id = d->nextNegativeID--;
    else if (id == -1)
        id = d->nextPositiveID++;
    else
        d->nextPositiveID = qMax(d->nextPositiveID, id + 1);
        // use id >= 0 as-is

    dict->insert(id, w);

    // preserve existing focus
    TQWidget * f = TQT_TQWIDGET(w->focusWidget());
    while(f && f != w)
        f = TQT_TQWIDGET(f->parentWidget());
    if (f) {
        if (!focusWidgets)
            focusWidgets = new TQPtrDict<TQWidget>(17);
        focusWidgets->replace(w, TQT_TQWIDGET(w->focusWidget()));
    }

    w->hide();
    if (w->QWidget::parent() != this)
        w->reparent(this, contentsRect().topLeft(), false);
    w->setGeometry(contentsRect());
    updateGeometry();
    return id;
}


/*!
    Removes widget \a w from this stack of widgets. Does not delete \a
    w. If \a w is the currently visible widget, no other widget is
    substituted.

    \sa visibleWidget() raiseWidget()
*/

void TQWidgetStack::removeWidget(TQWidget * w)
{
    int i;
    if (!w || (i = id(w)) == -1)
        return ;

    dict->take(i);
    if (w == topWidget)
        topWidget = 0;
    if (dict->isEmpty())
        invisible->hide(); // let background shine through again
    updateGeometry();
}


/*!
    Raises the widget with ID \a id to the top of the widget stack.

    \sa visibleWidget()
*/

void TQWidgetStack::raiseWidget(int id)
{
    if (id == -1)
        return;
    TQWidget * w = dict->find(id);
    if (w)
        raiseWidget(w);
}

static bool isChildOf(TQWidget* child, TQWidget *parent)
{
    if (!child)
        return false;
    for (int i = 0; i < parent->childrenListObject().count(); ++i) {
        TQObject *obj = parent->childrenListObject().at(i);
        if (!obj->isWidgetType())
            continue;
        TQWidget *widget = TQT_TQWIDGET((QWidget*)obj);
        if (!widget->isWindow())
            continue;
        if (widget == child || isChildOf(child, widget))
            return true;
    }
    return false;
}

/*!
    \overload

    Raises widget \a w to the top of the widget stack.
*/

void TQWidgetStack::raiseWidget(TQWidget *w)
{
    if (!w || w == invisible || w->QWidget::parent() != this || w == topWidget)
        return;

    if (id(w) == -1)
        addWidget(w);
    if (!isVisible()) {
        topWidget = w;
        return;
    }

    if (w->maximumSize().width() < invisible->width()
        || w->maximumSize().height() < invisible->height())
        invisible->setBackgroundMode(backgroundMode());
    else if (invisible->backgroundMode() != TQt::NoBackground)
        invisible->setBackgroundMode(TQt::NoBackground);

    if (invisible->isHidden()) {
        invisible->setGeometry(contentsRect());
        invisible->lower();
        invisible->show();
        QApplication::sendPostedEvents(invisible, QEvent::ShowWindowRequest);
    }

    // try to move focus onto the incoming widget if focus
    // was somewhere on the outgoing widget.
    if (topWidget) {
        TQWidget * fw = TQT_TQWIDGET(window()->focusWidget());
        if (topWidget->isAncestorOf(fw)) { // focus was on old page
            // look for the best focus widget we can find
            TQWidget *p = TQT_TQWIDGET(w->focusWidget());
            if (!p) {
                // second best == first child widget in the focus chain
                TQWidget *i = fw;
                while ((i = TQT_TQWIDGET(i->nextInFocusChain())) != fw) {
                    if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus)
                        && !i->focusProxy() && i->isVisibleTo(w) && i->isEnabled()
                        && w->isAncestorOf(i)) {
                        p = i;
                        break;
                    }
                }
            }
            if (p)
                p->setFocus();
        } else {
            // the focus wasn't on the old page, so we have to ensure focus doesn't go to
            // the widget in the page that last had focus when we show the page again.
            TQWidget *oldfw = TQT_TQWIDGET(topWidget->focusWidget());
            if (oldfw)
                oldfw->clearFocus();
        }
    }

    if (isVisible()) {
        emit aboutToShow(w);
        int i = id(w);
        if (i != -1)
            emit aboutToShow(i);
    }

    topWidget = w;

    for (int i = 0; i < childrenListObject().count(); ++i) {
        TQObject * o = childrenListObject().at(i);
        if (o->isWidgetType() && TQT_BASE_OBJECT(o) != TQT_BASE_OBJECT(w) && TQT_BASE_OBJECT(o) != TQT_BASE_OBJECT(invisible))
            TQT_TQWIDGET((QWidget*)o)->hide();
    }

    w->setGeometry(invisible->geometry());
    w->show();
}

/*!
    \reimp
*/

void TQWidgetStack::frameChanged()
{
    TQFrame::frameChanged();
    setChildGeometries();
}


/*!
    \internal
*/

void TQWidgetStack::setFrameRect(const QRect & r)
{
    // ### this function used to be virtual in QFrame in Qt 3; it is no longer virtual in Qt 4
    TQFrame::setFrameRect(r);
    setChildGeometries();
}


/*!
    Fixes up the children's geometries.
*/

void TQWidgetStack::setChildGeometries()
{
    invisible->setGeometry(contentsRect());
    if (topWidget)
        topWidget->setGeometry(invisible->geometry());
}


/*!
    \reimp
*/
void TQWidgetStack::setVisible(bool visible)
{
    if (visible) {
        //  Reimplemented in order to set the children's geometries
        //  appropriately and to pick the first widget as d->topWidget if no
        //  topwidget was defined
        if (!isVisible() && !childrenListObject().isEmpty()) {
            for (int i = 0; i < childrenListObject().count(); ++i) {
                TQObject * o = childrenListObject().at(i);
                if (o->isWidgetType()) {
                    if (!topWidget && TQT_BASE_OBJECT(o) != TQT_BASE_OBJECT(invisible))
                        topWidget = TQT_TQWIDGET((QWidget*)o);
                    if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(topWidget))
                        TQT_TQWIDGET((QWidget*)o)->show();
                    else
                        TQT_TQWIDGET((QWidget*)o)->hide();
                }
            }
            setChildGeometries();
        }
    }
    TQFrame::setVisible(visible);
}


/*!
    Returns the widget with ID \a id. Returns 0 if this widget stack
    does not manage a widget with ID \a id.

    \sa id() addWidget()
*/

TQWidget * TQWidgetStack::widget(int id) const
{
    return id != -1 ? dict->find(id) : 0;
}


/*!
    Returns the ID of the \a widget. Returns -1 if \a widget is 0 or
    is not being managed by this widget stack.

    \sa widget() addWidget()
*/

int TQWidgetStack::id(TQWidget * widget) const
{
    if (!widget)
        return -1;

    TQIntDictIterator<TQWidget> it(*dict);
    while (it.current() && it.current() != widget)
        ++it;
    return it.current() == widget ? it.currentKey() : -1;
}


/*!
    Returns the currently visible widget (the one at the top of the
    stack), or 0 if nothing is currently being shown.

    \sa aboutToShow() id() raiseWidget()
*/

TQWidget * TQWidgetStack::visibleWidget() const
{
    return topWidget;
}


/*!
    \fn void TQWidgetStack::aboutToShow(int id)

    This signal is emitted just before a managed widget is shown if
    that managed widget has an ID != -1. The \a id parameter is the numeric
    ID of the widget.

    If you call visibleWidget() in a slot connected to aboutToShow(),
    the widget it returns is the one that is currently visible, not
    the one that is about to be shown.
*/


/*!
    \fn void TQWidgetStack::aboutToShow(QWidget *widget)

    \overload

    This signal is emitted just before a managed widget is shown. The
    argument is a pointer to the \a widget.

    If you call visibleWidget() in a slot connected to aboutToShow(),
    the widget returned is the one that is currently visible, not the
    one that is about to be shown.
*/


/*!
    \reimp
*/

void TQWidgetStack::resizeEvent(TQResizeEvent * e)
{
    TQFrame::resizeEvent(e);
    setChildGeometries();
}

/*!
    \reimp
*/

TQSize TQWidgetStack::tqsizeHint() const
{
    constPolish();

    TQSize size(0, 0);

    TQIntDictIterator<TQWidget> it(*dict);
    TQWidget *w;

    while ((w = it.current()) != 0) {
        ++it;
        TQSize sh = w->tqsizeHint();
        if (w->tqsizePolicy().horData() == QSizePolicy::Ignored)
            sh.rwidth() = 0;
        if (w->tqsizePolicy().verData() == QSizePolicy::Ignored)
            sh.rheight() = 0;
#ifndef QT_NO_LAYOUT
        size = size.expandedTo(sh).expandedTo(tqSmartMinSize(w));
#endif
    }
    if (size.isNull())
        size = TQSize(128, 64);
    size += TQSize(2*frameWidth(), 2*frameWidth());
    return size;
}


/*!
    \reimp
*/
TQSize TQWidgetStack::tqminimumSizeHint() const
{
    constPolish();

    TQSize size(0, 0);

    TQIntDictIterator<TQWidget> it(*dict);
    TQWidget *w;

    while ((w = it.current()) != 0) {
        ++it;
        TQSize sh = w->tqminimumSizeHint();
        if (w->tqsizePolicy().horData() == QSizePolicy::Ignored)
            sh.rwidth() = 0;
        if (w->tqsizePolicy().verData() == QSizePolicy::Ignored)
            sh.rheight() = 0;
#ifndef QT_NO_LAYOUT
        size = size.expandedTo(sh).expandedTo(w->minimumSize());
#endif
    }
    if (size.isNull())
        size = TQSize(64, 32);
    size += TQSize(2*frameWidth(), 2*frameWidth());
    return size;
}

/*!
    \reimp
*/
void TQWidgetStack::childEvent(TQChildEvent *e)
{
    if (e->child()->isWidgetType() && e->removed())
        removeWidget((TQWidget *) e->child());
}


/*!
    \reimp
*/
bool TQWidgetStack::event(TQEvent* e)
{
    if (e->type() == TQEvent::LayoutRequest || e->type() == TQEvent::LayoutHint )
        updateGeometry(); // propgate layout hints to parent
    return TQFrame::event(e);
}

QT_END_NAMESPACE

#else // USE_QT4

/****************************************************************************
**
** Implementation of TQWidgetStack class
**
** Created : 980128
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of the widgets module of the TQt 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 TQt 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.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** 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 "tqwidgetstack.h"
#include "tqlayout.h"
#include "../kernel/tqlayoutengine_p.h"
#ifndef TQT_NO_WIDGETSTACK

#include "tqobjectlist.h"
#include "tqfocusdata.h"
#include "tqbutton.h"
#include "tqbuttongroup.h"

#include "tqapplication.h"

class TQWidgetStackPrivate {
public:
    class Invisible: public TQWidget
    {
    public:
	Invisible( TQWidgetStack * parent ): TQWidget( parent, "qt_invisible_widgetstack" )
	{
	    setBackgroundMode( TQt::NoBackground );
	}
	const char * className() const
	{
	    return "TQWidgetStackPrivate::Invisible";
	}
    };
};


#if (TQT_VERSION-0 >= 0x040000)
#if defined(TQ_CC_GNU)
#warning "Remove TQWidgetStackEventFilter"
#endif
#endif
class TQWidgetStackEventFilter : public TQObject
{
    /* For binary compatibility, since we cannot implement virtual
       functions and rely on them being called. This is what we should
       have

	bool TQWidgetStack::event( TQEvent* e )
	{
	    if ( e->type() == TQEvent::LayoutHint )
		updateGeometry(); // propgate tqlayout hints to parent
	    return TQFrame::event( e );
	}
    */
public:

    TQWidgetStackEventFilter( TQObject *parent = 0, const char * name = 0 )
	: TQObject( parent, name ) {}
    bool eventFilter( TQObject *o, TQEvent * e ) {
	if ( e->type() == TQEvent::LayoutHint && o->isWidgetType() )
	    ((TQWidget*)o)->updateGeometry();
	return FALSE;
    }
};


/*!
    \class TQWidgetStack
    \brief The TQWidgetStack class provides a stack of widgets of which
    only the top widget is user-visible.

    \ingroup organizers
    \mainclass

    The application programmer can move any widget to the top of the
    stack at any time using raiseWidget(), and add or remove widgets
    using addWidget() and removeWidget(). It is not sufficient to pass
    the widget stack as parent to a widget which should be inserted into
    the widgetstack.

    visibleWidget() is the \e get equivalent of raiseWidget(); it
    returns a pointer to the widget that is currently at the top of
    the stack.

    TQWidgetStack also provides the ability to manipulate widgets
    through application-specified integer IDs. You can also translate
    from widget pointers to IDs using id() and from IDs to widget
    pointers using widget(). These numeric IDs are unique (per
    TQWidgetStack, not globally), but TQWidgetStack does not attach any
    additional meaning to them.

    The default widget stack is frameless, but you can use the usual
    TQFrame functions (such as setFrameStyle()) to add a frame.

    TQWidgetStack provides a signal, aboutToShow(), which is emitted
    just before a managed widget is shown.

    \sa TQTabDialog TQTabBar TQFrame
*/


/*!
    Constructs an empty widget stack.

    The \a parent and \a name arguments are passed to the TQFrame
    constructor.
*/

TQWidgetStack::TQWidgetStack( TQWidget * parent, const char *name )
    : TQFrame( parent, name )
{
    init();
}

/*!
  Constructs an empty widget stack.

  The \a parent, \a name and \a f arguments are passed to the TQFrame
  constructor.
*/
TQWidgetStack::TQWidgetStack( TQWidget * parent, const char *name, WFlags f )
    : TQFrame( parent, name, f ) //## merge constructors in 4.0
{
    init();
}

void TQWidgetStack::init()
{
   d = 0;
   TQWidgetStackEventFilter *ef = new TQWidgetStackEventFilter( TQT_TQOBJECT(this) );
   installEventFilter( ef );
   dict = new TQIntDict<TQWidget>;
   tqfocusWidgets = 0;
   topWidget = 0;
   invisible = new TQWidgetStackPrivate::Invisible( this );
   invisible->hide();
}


/*!
    Destroys the object and frees any allocated resources.
*/

TQWidgetStack::~TQWidgetStack()
{
    delete tqfocusWidgets;
    delete d;
    delete dict;
}


/*!
    Adds widget \a w to this stack of widgets, with ID \a id.

    If you pass an id \>= 0 this ID is used. If you pass an \a id of
    -1 (the default), the widgets will be numbered automatically. If
    you pass -2 a unique negative integer will be generated. No widget
    has an ID of -1. Returns the ID or -1 on failure (e.g. \a w is 0).

    If you pass an id that is already used, then a unique negative
    integer will be generated to prevent two widgets having the same
    id.

    If \a w already exists in the stack the widget will be removed first.

    If \a w is not a child of this TQWidgetStack moves it using
    reparent().
*/

int TQWidgetStack::addWidget( TQWidget * w, int id )
{
    static int nseq_no = -2;
    static int pseq_no = 0;

    if ( !w || w == invisible )
	return -1;

    // prevent duplicates
    removeWidget( w );

    if ( id >= 0 && dict->find( id ) )
	id = -2;
    if ( id < -1 )
	id = nseq_no--;
    else if ( id == -1 )
	id = pseq_no++;
    else
	pseq_no = TQMAX(pseq_no, id + 1);
	// use id >= 0 as-is

    dict->insert( id, w );

    // preserve existing focus
    TQWidget * f = w->tqfocusWidget();
    while( f && f != w )
	f = f->parentWidget();
    if ( f ) {
	if ( !tqfocusWidgets )
	    tqfocusWidgets = new TQPtrDict<TQWidget>( 17 );
	tqfocusWidgets->replace( w, w->tqfocusWidget() );
    }

    w->hide();
    if ( w->parent() != TQT_TQOBJECT(this) )
	w->reparent( this, contentsRect().topLeft(), FALSE );
    w->setGeometry( contentsRect() );
    updateGeometry();
    return id;
}


/*!
    Removes widget \a w from this stack of widgets. Does not delete \a
    w. If \a w is the currently visible widget, no other widget is
    substituted.

    \sa visibleWidget() raiseWidget()
*/

void TQWidgetStack::removeWidget( TQWidget * w )
{
    if ( !w )
	return;
    int i = id( w );
    if ( i != -1 )
	dict->take( i );

    if ( w == topWidget )
	topWidget = 0;
    if ( dict->isEmpty() )
	invisible->hide(); // let background shine through again
    updateGeometry();
}


/*!
    Raises the widget with ID \a id to the top of the widget stack.

    \sa visibleWidget()
*/

void TQWidgetStack::raiseWidget( int id )
{
    if ( id == -1 )
	return;
    TQWidget * w = dict->find( id );
    if ( w )
	raiseWidget( w );
}

static bool isChildOf( TQWidget* child, TQWidget *parent )
{
    if ( !child || !parent->childrenListObject().isEmpty() )
	return FALSE;
    TQObjectListIt it(parent->childrenListObject());
    TQObject *obj;
    while ( (obj = it.current()) ) {
	++it;
	if ( !obj->isWidgetType() || ((TQWidget *)obj)->isTopLevel() )
	    continue;
	TQWidget *widget = (TQWidget *)obj;
	if ( widget == child || isChildOf( child, widget ) )
	    return TRUE;
    }
    return FALSE;
}

/*!
    \overload

    Raises widget \a w to the top of the widget stack.
*/

void TQWidgetStack::raiseWidget( TQWidget *w )
{
    if ( !w || w == invisible || w->parent() != TQT_TQOBJECT(this) || w == topWidget )
	return;

    if ( id(w) == -1 )
	addWidget( w );
    if ( !isVisible() ) {
	topWidget = w;
	return;
    }

    if (w->tqmaximumSize().width() < invisible->width()
        || w->tqmaximumSize().height() < invisible->height())
        invisible->setBackgroundMode(backgroundMode());
    else if (invisible->backgroundMode() != TQt::NoBackground)
        invisible->setBackgroundMode(TQt::NoBackground);

    if ( invisible->isHidden() ) {
	invisible->setGeometry( contentsRect() );
	invisible->lower();
	invisible->show();
	TQApplication::sendPostedEvents( invisible, TQEvent::ShowWindowRequest );
    }

    // try to move focus onto the incoming widget if focus
    // was somewhere on the outgoing widget.
    if ( topWidget ) {
	TQWidget * fw = tqfocusWidget();
	TQWidget* p = fw;
	while ( p && p != topWidget )
	    p = p->parentWidget();
	if ( p == topWidget ) { // focus was on old page
	    if ( !tqfocusWidgets )
		tqfocusWidgets = new TQPtrDict<TQWidget>( 17 );
	    tqfocusWidgets->replace( topWidget, fw );
	    fw->clearFocus();
	    // look for the best focus widget we can find
	    // best == what we had (which may be deleted)
	    fw = tqfocusWidgets->take( w );
	    if ( isChildOf( fw, w ) ) {
		fw->setFocus();
	    } else {
		// second best == first child widget in the focus chain
		TQFocusData *f = focusData();
		TQWidget* home = f->home();
		TQWidget *i = home;
		do {
		    if ( ( ( i->focusPolicy() & Qt::TabFocus ) == Qt::TabFocus )
			 && !i->focusProxy() && i->isVisibleTo(w) && i->isEnabled() ) {
			p = i;
			while ( p && p != w )
			    p = p->parentWidget();
			if ( p == w ) {
			    i->setFocus();
			    break;
			}
		    }
		    i = f->next();
		} while( i != home );
	    }
	}
    }

    if ( isVisible() ) {
	emit aboutToShow( w );
	int i = id( w );
	if ( i != -1 )
	    emit aboutToShow( i );
    }

    topWidget = w;

    TQObjectListIt it( childrenListObject() );
    TQObject * o;

    while( (o=it.current()) != 0 ) {
	++it;
	if ( o->isWidgetType() && o != TQT_TQOBJECT(w) && o != TQT_TQOBJECT(invisible) )
	    ((TQWidget *)o)->hide();
    }

    w->setGeometry( invisible->tqgeometry() );
    w->show();
}

/*!
    \reimp
*/

void TQWidgetStack::frameChanged()
{
    TQFrame::frameChanged();
    setChildGeometries();
}


/*!
    \reimp
*/

void TQWidgetStack::setFrameRect( const TQRect & r )
{
    TQFrame::setFrameRect( r );
    setChildGeometries();
}


/*!
    Fixes up the tqchildren's geometries.
*/

void TQWidgetStack::setChildGeometries()
{
    invisible->setGeometry( contentsRect() );
    if ( topWidget )
	topWidget->setGeometry( invisible->tqgeometry() );
}


/*!
    \reimp
*/
void TQWidgetStack::show()
{
    //  Reimplemented in order to set the tqchildren's geometries
    //  appropriately and to pick the first widget as topWidget if no
    //  topwidget was defined
    if ( !isVisible() && !childrenListObject().isEmpty() ) {
	TQObjectListIt it( childrenListObject() );
	TQObject * o;

	while( (o=it.current()) != 0 ) {
	    ++it;
	    if ( o->isWidgetType() ) {
		if ( !topWidget && o != TQT_TQOBJECT(invisible) )
		    topWidget = (TQWidget*)o;
		if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(topWidget) )
		    ((TQWidget *)o)->show();
		else
		    ((TQWidget *)o)->hide();
	    }
	}
	setChildGeometries();
    }
    TQFrame::show();
}


/*!
    Returns the widget with ID \a id. Returns 0 if this widget stack
    does not manage a widget with ID \a id.

    \sa id() addWidget()
*/

TQWidget * TQWidgetStack::widget( int id ) const
{
    return id != -1 ? dict->find( id ) : 0;
}


/*!
    Returns the ID of the \a widget. Returns -1 if \a widget is 0 or
    is not being managed by this widget stack.

    \sa widget() addWidget()
*/

int TQWidgetStack::id( TQWidget * widget ) const
{
    if ( !widget )
	return -1;

    TQIntDictIterator<TQWidget> it( *dict );
    while ( it.current() && it.current() != widget )
	++it;
    return it.current() == widget ? it.currentKey() : -1;
}


/*!
    Returns the currently visible widget (the one at the top of the
    stack), or 0 if nothing is currently being shown.

    \sa aboutToShow() id() raiseWidget()
*/

TQWidget * TQWidgetStack::visibleWidget() const
{
    return topWidget;
}


/*!
    \fn void TQWidgetStack::aboutToShow( int )

    This signal is emitted just before a managed widget is shown if
    that managed widget has an ID != -1. The argument is the numeric
    ID of the widget.

    If you call visibleWidget() in a slot connected to aboutToShow(),
    the widget it returns is the one that is currently visible, not
    the one that is about to be shown.
*/


/*!
    \fn void TQWidgetStack::aboutToShow( TQWidget * )

    \overload

    This signal is emitted just before a managed widget is shown. The
    argument is a pointer to the widget.

    If you call visibleWidget() in a slot connected to aboutToShow(),
    the widget returned is the one that is currently visible, not the
    one that is about to be shown.
*/


/*!
    \reimp
*/

void TQWidgetStack::resizeEvent( TQResizeEvent * e )
{
    TQFrame::resizeEvent( e );
    setChildGeometries();
}


/*!
    \reimp
*/

TQSize TQWidgetStack::tqsizeHint() const
{
    constPolish();

    TQSize size( 0, 0 );

    TQIntDictIterator<TQWidget> it( *dict );
    TQWidget *w;

    while ( (w = it.current()) != 0 ) {
	++it;
	TQSize sh = w->tqsizeHint();
	if ( w->tqsizePolicy().horData() == TQSizePolicy::Ignored )
	    sh.rwidth() = 0;
	if ( w->tqsizePolicy().verData() == TQSizePolicy::Ignored )
	    sh.rheight() = 0;
#ifndef TQT_NO_LAYOUT
	size = size.expandedTo( sh ).expandedTo( tqSmartMinSize(w) );
#endif
    }
    if ( size.isNull() )
	size = TQSize( 128, 64 );
    size += TQSize( 2*frameWidth(), 2*frameWidth() );
    return size;
}


/*!
    \reimp
*/
TQSize TQWidgetStack::tqminimumSizeHint() const
{
    constPolish();

    TQSize size( 0, 0 );

    TQIntDictIterator<TQWidget> it( *dict );
    TQWidget *w;

    while ( (w = it.current()) != 0 ) {
	++it;
	TQSize sh = w->tqminimumSizeHint();
	if ( w->tqsizePolicy().horData() == TQSizePolicy::Ignored )
	    sh.rwidth() = 0;
	if ( w->tqsizePolicy().verData() == TQSizePolicy::Ignored )
	    sh.rheight() = 0;
#ifndef TQT_NO_LAYOUT
	size = size.expandedTo( sh ).expandedTo( w->tqminimumSize() );
#endif
    }
    if ( size.isNull() )
	size = TQSize( 64, 32 );
    size += TQSize( 2*frameWidth(), 2*frameWidth() );
    return size;
}

/*!
    \reimp
*/
void TQWidgetStack::childEvent( TQChildEvent * e)
{
    if ( e->child()->isWidgetType() && e->removed() )
	removeWidget( (TQWidget*) e->child() );
}
#endif

#endif // USE_QT4