summaryrefslogtreecommitdiffstats
path: root/tdeui/ktabctl.cpp
blob: 0ff9f64e4d9f1fbeefc4f7c07224c58409414084 (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
/* This file is part of the KDE libraries
    Copyright (C) 1997 Alexander Sanda ([email protected])

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

/**
 * KTabCtl provides a universal tab control. It is in no ways limited to dialogs and
 * can be used for whatever you want. It has no buttons or any other stuff.
 *
 * However, this is based on the original TQTabDialog.
 */

#include <tqtabbar.h>
#include <tqpushbutton.h>
#include <tqpainter.h>
#include <tqpixmap.h>

#include "ktabctl.h"

KTabCtl::KTabCtl(TQWidget *parent, const char *name)
    : TQWidget(parent, name)
{
    tabs = new TQTabBar(this, "_tabbar");
    connect(tabs, TQT_SIGNAL(selected(int)), this, TQT_SLOT(showTab(int)));
    tabs->move(2, 1);

    blBorder = true;

}

KTabCtl::~KTabCtl()
{
	delete tabs;
}

void KTabCtl::resizeEvent(TQResizeEvent *)
{
    int i;
    TQRect r = getChildRect();

    if (tabs) {
        for (i=0; i<(int)pages.size(); i++) {
            pages[i]->setGeometry(r);
        }
        if( ( tabs->shape() == TQTabBar::RoundedBelow ) ||
            ( tabs->shape() == TQTabBar::TriangularBelow ) ) {
            tabs->move( 0, height()-tabs->height()-4 );
        }
    }
}

void KTabCtl::setFont(const TQFont & font)
{
    TQFont f(font);
    f.setWeight(TQFont::Light);
    TQWidget::setFont(f);

    setSizes();
}

void KTabCtl::setTabFont(const TQFont & font)
{
    TQFont f(font);
//    f.setWeight(TQFont::Light);
    tabs->setFont(f);

    setSizes();
}

void KTabCtl::show()
{
    unsigned int i;

    if(isVisible())
	return;

    setSizes();

    for(i = 0; i < pages.size(); i++)
	pages[i]->hide();

    TQResizeEvent r(size(), size());
    resizeEvent(&r);

    TQWidget::show();
}

bool KTabCtl::isTabEnabled(const TQString& name)
{
    unsigned int i;

    for(i = 0; i < pages.size(); i++)
	if (TQString::fromLatin1(pages[i]->name()) == name)
	    return tabs->isTabEnabled(i);   /* return the enabled status */
    return false;     /* tab does not exist */
}

void KTabCtl::setTabEnabled(const TQString& name, bool state)
{
    unsigned i;

    if (name.isEmpty())
        return;

    for (i = 0; i < pages.size(); i++)
	if (TQString::fromLatin1(pages[i]->name()) == name)
	    tabs->setTabEnabled(i, state);
}

void KTabCtl::setSizes()
{
    unsigned i;

    TQSize min(tabs->sizeHint());    /* the minimum required size for the tabbar */
    tabs->resize(min);         /* make sure that the tabbar does not require more space than actually needed. */


    TQSize max(TQCOORD_MAX,TQCOORD_MAX);
    //int th = min.height();          /* the height of the tabbar itself (without pages and stuff) */

    for (i = 0; i < pages.size(); i++) {

        /*
         * check the actual minimum and maximum sizes
         */

	if (pages[i]->maximumSize().height() < max.height())
	    max.setHeight(pages[i]->maximumSize().height());
	if (pages[i]->maximumSize().width() < max.width())
	    max.setWidth( pages[i]->maximumSize().width());
	if ( pages[i]->minimumSize().height() > min.height())
	    min.setHeight( pages[i]->minimumSize().height());
	if ( pages[i]->minimumSize().width() > min.width())
	    min.setWidth( pages[i]->minimumSize().width());
    }

    // BL: min and max are sizes of children, not tabcontrol
    // min.setHeight(min.height() + th);

    if (max.width() < min.width())
	max.setWidth(min.width());
    if (max.height() < min.height())
	max.setHeight(min.height());

    /*
     * now, apply the calculated size values to all of the pages
     */

    for( i=0; i<(uint)pages.size(); i++ ) {
	pages[i]->setMinimumSize(min);
	pages[i]->setMaximumSize(max);
    }


    // BL: set minimum size of tabcontrol
    setMinimumSize(min.width()+4, min.height()+tabs->height()+4);

    /*
     * generate a resizeEvent, if we're visible
     */

    if(isVisible()) {
	TQResizeEvent r(size(), size());
	resizeEvent(&r);
    }
}

void KTabCtl::setBorder( bool state )
{
    blBorder = state;
}

void KTabCtl::setShape( TQTabBar::Shape shape )
{
    tabs->setShape( shape );
}

TQSize
KTabCtl::sizeHint() const
{
	/* desired size of the tabbar */
	TQSize hint(tabs->sizeHint());

	/* overall desired size of all pages */
	TQSize pageHint;
	for (unsigned int i = 0; i < pages.size(); i++)
	{
		TQSize sizeI(pages[i]->sizeHint());

		if (sizeI.isValid())
		{
			/* only pages with valid size are used */
			if (sizeI.width() > pageHint.width())
				pageHint.setWidth(sizeI.width());

			if (sizeI.height() > pageHint.height())
				pageHint.setHeight(sizeI.height());
		}
	}

	if (pageHint.isValid())
	{
		/* use maximum of width of tabbar and pages */
		if (pageHint.width() > hint.width())
			hint.setWidth(pageHint.width());

		/* heights must just be added */
		hint.setHeight(hint.height() + pageHint.height());

		/* 1999-09-18: Espen Sand
		   I cannot get the size to be correct unless the total
		   border size is included: ie 2*2 pixels.
		*/
		return (hint + TQSize(4,4));
	}

	/*
	 * If not at least a one page has a valid sizeHint we have to return
	 * an invalid size as well.
	 */
	return (pageHint);
}

void KTabCtl::paintEvent(TQPaintEvent *)
{
    if (!tabs)
	return;

    if( !blBorder )
        return;

    TQPainter p;
    p.begin(this);

    int y0 = getChildRect().top() - 1;
    int y1 = getChildRect().bottom() + 2;
    int x1 = getChildRect().right() + 2;
    int x0 = getChildRect().left() - 1;

    p.setPen(colorGroup().light());
    p.drawLine(x0, y0 - 1, x1 - 1, y0 - 1);      /* 1st top line */
    p.setPen(colorGroup().midlight());
    p.drawLine(x0, y0, x1 - 1, y0);      /* 2nd top line */
    p.setPen(colorGroup().light());
    p.drawLine(x0, y0 + 1, x0, y1);      /* left line */
    p.setPen(black);
    p.drawLine(x1, y1, x0, y1);          /* bottom line */
    p.drawLine(x1, y1 - 1, x1, y0);
    p.setPen(colorGroup().dark());
    p.drawLine(x0 + 1, y1 - 1, x1 - 1, y1 - 1);  /* bottom */
    p.drawLine(x1 - 1, y1 - 2, x1 - 1, y0 + 1);
    p.end();
}

/*
 * return the client rect. This is the maximum size for any child
 * widget (page).
 */

TQRect KTabCtl::getChildRect() const
{
    if( ( tabs->shape() == TQTabBar::RoundedBelow ) ||
        ( tabs->shape() == TQTabBar::TriangularBelow ) ) {
    	return TQRect(2, 1, width() - 4,
		     height() - tabs->height() - 4);
    } else {
      	return TQRect(2, tabs->height() + 1, width() - 4,
		     height() - tabs->height() - 4);
    }
}

/*
 * show a single page, depending on the selected tab
 * emit tabSelected(new_pagenumber) BEFORE the page is shown
 */

void KTabCtl::showTab(int i)
{
    unsigned int j;
    for (j = 0; j < pages.size(); j++) {
      if (j != (unsigned)i) {
        pages[j]->hide();
      }
    }

    if((unsigned)i < pages.size()) {
        emit(tabSelected(i));
		if( pages.size() >= 2 ) {
			pages[i]->raise();
		}
		tabs->setCurrentTab(i);
        pages[i]->setGeometry(getChildRect());
        pages[i]->show();
    }
}

/*
 * add a tab to the control. This tab will manage the given Widget w.
 * in most cases, w will be a TQWidget and will only act as parent for the
 * actual widgets on this page
 * NOTE: w is not required to be of class TQWidget, but expect strange results with
 * other types of widgets
 */

void KTabCtl::addTab(TQWidget *w, const TQString& name)
{
    TQTab *t = new TQTab();
    t->setText( name );
    t->setEnabled( true );
    int id = tabs->addTab(t);   /* add the tab itself to the tabbar */
    if (id == (int)pages.size()) {
	pages.resize(id + 1);
        pages[id] = w;          /* remember the widget to manage by this tab */
    }
    // BL: compute sizes
    setSizes();
}

void KTabCtl::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "ktabctl.moc"