21 #include <tqapplication.h>
23 #include <tqstylesheet.h>
25 #include <tdeconfig.h>
26 #include <kiconloader.h>
27 #include <kstringhandler.h>
29 #include "ktabwidget.h"
32 class KTabWidgetPrivate {
34 bool m_automaticResizeTabs;
37 unsigned int m_CurrentMaxLength;
38 bool m_mouseWheelScroll;
41 TQStringList m_tabNames;
43 KTabWidgetPrivate() : m_automaticResizeTabs(false), m_mouseWheelScroll(true)
48 m_CurrentMaxLength = m_minLength;
52 KTabWidget::KTabWidget( TQWidget *parent,
const char *name, WFlags f )
53 : TQTabWidget( parent, name, f )
55 d =
new KTabWidgetPrivate;
56 setTabBar(
new KTabBar(
this,
"tabbar") );
57 setAcceptDrops(
true );
59 setHoverCloseButtonDelayed(
false);
61 connect(tabBar(), TQT_SIGNAL(contextMenu(
int,
const TQPoint & )), TQT_SLOT(contextMenu(
int,
const TQPoint & )));
62 connect(tabBar(), TQT_SIGNAL(mouseDoubleClick(
int )), TQT_SLOT(mouseDoubleClick(
int )));
63 connect(tabBar(), TQT_SIGNAL(mouseMiddleClick(
int )), TQT_SLOT(mouseMiddleClick(
int )));
64 connect(tabBar(), TQT_SIGNAL(initiateDrag(
int )), TQT_SLOT(initiateDrag(
int )));
65 connect(tabBar(), TQT_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )), TQT_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )));
66 connect(tabBar(), TQT_SIGNAL(receivedDropEvent(
int, TQDropEvent * )), TQT_SLOT(receivedDropEvent(
int, TQDropEvent * )));
67 connect(tabBar(), TQT_SIGNAL(moveTab(
int,
int )), TQT_SLOT(moveTab(
int,
int )));
68 connect(tabBar(), TQT_SIGNAL(closeRequest(
int )), TQT_SLOT(closeRequest(
int )));
69 #ifndef QT_NO_WHEELEVENT
70 connect(tabBar(), TQT_SIGNAL(wheelDelta(
int )), TQT_SLOT(wheelDelta(
int )));
81 TQTabWidget::insertTab( child, label, index );
86 TQTabWidget::insertTab( child, iconset, label, index );
91 TQTabWidget::insertTab( child, tab, index);
92 if ( d->m_automaticResizeTabs ) {
93 if ( index < 0 || index >= count() ) {
94 d->m_tabNames.append( tab->text() );
95 resizeTabs( d->m_tabNames.count()-1 );
98 d->m_tabNames.insert( d->m_tabNames.at( index ), tab->text() );
106 TQWidget *rightcorner = this->cornerWidget( TopRight );
107 TQWidget *leftcorner = this->cornerWidget( TopLeft );
110 if ( leftcorner ) leftcorner->hide();
111 if ( rightcorner ) rightcorner->hide();
115 if ( leftcorner ) leftcorner->show();
116 if ( rightcorner ) rightcorner->show();
122 return !( tabBar()->isVisible() );
127 d->m_mouseWheelScroll = mouseWheelScroll;
132 TQTab *t = tabBar()->tabAt( indexOf( w ) );
140 TQTab *t = tabBar()->tabAt( indexOf( w ) );
168 unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
171 hframe = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace, tabBar() );
172 overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap, tabBar() );
174 TQFontMetrics fm = tabBar()->fontMetrics();
176 for(
int i=0; i < count(); ++i ) {
177 TQString newTitle = d->m_tabNames[ i ];
180 TQTab* tab = tabBar()->tabAt( i );
181 int lw = fm.width( newTitle );
183 if ( tab->iconSet() )
184 iw = tab->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal ).width() + 4;
185 x += ( tabBar()->style().tqsizeFromContents( TQStyle::CT_TabBarTab,
this,
186 TQSize( TQMAX( lw + hframe + iw, TQApplication::globalStrut().width() ), 0 ),
187 TQStyleOption( tab ) ) ).width();
194 TQTabWidget::changeTab( w, label );
195 if ( d->m_automaticResizeTabs ) {
196 int index = indexOf( w );
198 d->m_tabNames[ index ] =
label;
206 TQTabWidget::changeTab( w, iconset, label );
207 if ( d->m_automaticResizeTabs ) {
208 int index = indexOf( w );
210 d->m_tabNames[ index ] =
label;
218 if ( d->m_automaticResizeTabs ) {
219 if ( index >= 0 && index < count() )
220 return d->m_tabNames[ index ];
222 return TQString::null;
225 return TQTabWidget::label( index );
230 if ( d->m_automaticResizeTabs ) {
231 int index = indexOf( w );
233 return TQString::null;
235 return d->m_tabNames[ index ];
238 return TQTabWidget::tabLabel( w );
243 TQTabWidget::setTabLabel( w, l );
244 if ( d->m_automaticResizeTabs ) {
245 int index = indexOf( w );
247 d->m_tabNames[ index ] = l;
253 void KTabWidget::resizeTabs(
int changeTabIndex )
256 if ( d->m_automaticResizeTabs ) {
258 newMaxLength=d->m_maxLength;
261 int tabBarHeight = tabBar()->sizeHint().height();
262 if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
263 lcw = TQMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
264 if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
265 rcw = TQMAX( cornerWidget( TopRight )->width(), tabBarHeight );
267 uint maxTabBarWidth = width() - lcw - rcw;
269 for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
270 if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
278 if ( d->m_CurrentMaxLength != newMaxLength ) {
279 d->m_CurrentMaxLength = newMaxLength;
280 for(
int i = 0; i < count(); ++i )
283 else if ( changeTabIndex != -1 )
284 updateTab( changeTabIndex );
287 void KTabWidget::updateTab(
int index )
289 TQString title = d->m_automaticResizeTabs ? d->m_tabNames[ index ] : TQTabWidget::label( index );
290 removeTabToolTip( page( index ) );
291 if ( title.length() > d->m_CurrentMaxLength ) {
292 if ( TQStyleSheet::mightBeRichText( title ) )
293 setTabToolTip( page( index ), TQStyleSheet::escape(title) );
295 setTabToolTip( page( index ), title );
299 title.replace(
'&',
"&&" );
301 if ( TQTabWidget::label( index ) != title )
302 TQTabWidget::setTabLabel( page( index ), title );
305 void KTabWidget::dragMoveEvent( TQDragMoveEvent *e )
307 if ( isEmptyTabbarSpace( e->pos() ) ) {
316 TQTabWidget::dragMoveEvent( e );
319 void KTabWidget::dropEvent( TQDropEvent *e )
321 if ( isEmptyTabbarSpace( e->pos() ) ) {
325 TQTabWidget::dropEvent( e );
328 #ifndef QT_NO_WHEELEVENT
329 void KTabWidget::wheelEvent( TQWheelEvent *e )
334 if ( isEmptyTabbarSpace( e->pos() ) )
335 wheelDelta( e->delta() );
340 void KTabWidget::wheelDelta(
int delta)
342 if (count()<2 || !d->m_mouseWheelScroll)
345 int page = currentPageIndex();
347 page = (page + 1) % count();
354 setCurrentPage(page);
358 void KTabWidget::mouseDoubleClickEvent( TQMouseEvent *e )
360 if( e->button() != Qt::LeftButton )
363 if ( isEmptyTabbarSpace( e->pos() ) ) {
367 TQTabWidget::mouseDoubleClickEvent( e );
370 void KTabWidget::mousePressEvent( TQMouseEvent *e )
372 if ( e->button() == Qt::RightButton ) {
373 if ( isEmptyTabbarSpace( e->pos() ) ) {
377 }
else if ( e->button() == Qt::MidButton ) {
378 if ( isEmptyTabbarSpace( e->pos() ) ) {
383 TQTabWidget::mousePressEvent( e );
413 TQString tablabel =
label( from );
414 TQWidget *w = page( from );
416 TQIconSet tabiconset = tabIconSet( w );
417 TQString tabtooltip = tabToolTip( w );
418 bool current = ( w == currentPage() );
419 bool enabled = isTabEnabled( w );
424 TQTab * t =
new TQTab();
425 t->setText(tablabel);
426 TQTabWidget::insertTab( w, t, to );
427 if ( d->m_automaticResizeTabs ) {
428 if ( to < 0 || to >= count() )
429 d->m_tabNames.append( TQString::null );
431 d->m_tabNames.insert( d->m_tabNames.at( to ), TQString::null );
436 setTabToolTip( w, tabtooltip );
440 setTabEnabled( w, enabled );
447 if ( d->m_automaticResizeTabs ) {
448 int index = indexOf( w );
450 d->m_tabNames.remove( d->m_tabNames.at( index ) );
452 TQTabWidget::removePage( w );
453 if ( d->m_automaticResizeTabs )
458 bool KTabWidget::isEmptyTabbarSpace(
const TQPoint &point )
const
460 TQSize size( tabBar()->sizeHint() );
461 if ( ( tabPosition()==
Top && point.y()< size.height() ) || ( tabPosition()==
Bottom && point.y()>(height()-size.height() ) ) ) {
462 TQWidget *rightcorner = cornerWidget( TopRight );
464 if ( point.x()>=width()-rightcorner->width() )
467 TQWidget *leftcorner = cornerWidget( TopLeft );
469 if ( point.x()<=leftcorner->width() )
472 TQTab *tab = tabBar()->selectTab( tabBar()->mapFromParent( point ) );
501 if ( d->m_automaticResizeTabs==enabled )
504 d->m_automaticResizeTabs = enabled;
506 d->m_tabNames.clear();
507 for(
int i = 0; i < count(); ++i )
508 d->m_tabNames.append( tabBar()->tabAt( i )->text() );
511 for(
int i = 0; i < count(); ++i )
512 tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
518 return d->m_automaticResizeTabs;
526 void KTabWidget::resizeEvent( TQResizeEvent *e )
528 TQTabWidget::resizeEvent( e );
532 #include "ktabwidget.moc"
static TQString rsqueeze(const TQString &str, uint maxlen=40)
static TDEConfig * config()