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(), TQ_SIGNAL(contextMenu(
int,
const TQPoint & )), TQ_SLOT(contextMenu(
int,
const TQPoint & )));
62 connect(tabBar(), TQ_SIGNAL(mouseDoubleClick(
int )), TQ_SLOT(mouseDoubleClick(
int )));
63 connect(tabBar(), TQ_SIGNAL(mouseMiddleClick(
int )), TQ_SLOT(mouseMiddleClick(
int )));
64 connect(tabBar(), TQ_SIGNAL(initiateDrag(
int )), TQ_SLOT(initiateDrag(
int )));
65 connect(tabBar(), TQ_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )), TQ_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )));
66 connect(tabBar(), TQ_SIGNAL(receivedDropEvent(
int, TQDropEvent * )), TQ_SLOT(receivedDropEvent(
int, TQDropEvent * )));
67 connect(tabBar(), TQ_SIGNAL(moveTab(
int,
int )), TQ_SLOT(moveTab(
int,
int )));
68 connect(tabBar(), TQ_SIGNAL(closeRequest(
int )), TQ_SLOT(closeRequest(
int )));
69 #ifndef TQT_NO_WHEELEVENT
70 connect(tabBar(), TQ_SIGNAL(wheelDelta(
int )), TQ_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 ) );
148 TQTab *t = tabBar()->tabAt( indexOf( w ) );
176 unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
179 hframe = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace, tabBar() );
180 overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap, tabBar() );
182 TQFontMetrics fm = tabBar()->fontMetrics();
184 for(
int i=0; i < count(); ++i ) {
185 TQString newTitle = d->m_tabNames[ i ];
188 TQTab* tab = tabBar()->tabAt( i );
189 int lw = fm.width( newTitle );
191 if ( tab->iconSet() )
192 iw = tab->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal ).width() + 4;
193 x += ( tabBar()->style().sizeFromContents( TQStyle::CT_TabBarTab,
this,
194 TQSize( TQMAX( lw + hframe + iw, TQApplication::globalStrut().width() ), 0 ),
195 TQStyleOption( tab ) ) ).width();
202 TQTabWidget::changeTab( w,
label );
203 if ( d->m_automaticResizeTabs ) {
204 int index = indexOf( w );
206 d->m_tabNames[ index ] =
label;
214 TQTabWidget::changeTab( w, iconset,
label );
215 if ( d->m_automaticResizeTabs ) {
216 int index = indexOf( w );
218 d->m_tabNames[ index ] =
label;
226 if ( d->m_automaticResizeTabs ) {
227 if ( index >= 0 && index < count() )
228 return d->m_tabNames[ index ];
230 return TQString::null;
233 return TQTabWidget::label( index );
238 if ( d->m_automaticResizeTabs ) {
239 int index = indexOf( w );
241 return TQString::null;
243 return d->m_tabNames[ index ];
246 return TQTabWidget::tabLabel( w );
251 TQTabWidget::setTabLabel( w, l );
252 if ( d->m_automaticResizeTabs ) {
253 int index = indexOf( w );
255 d->m_tabNames[ index ] = l;
261 void KTabWidget::resizeTabs(
int changeTabIndex )
264 if ( d->m_automaticResizeTabs ) {
266 newMaxLength=d->m_maxLength;
269 int tabBarHeight = tabBar()->sizeHint().height();
270 if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
271 lcw = TQMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
272 if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
273 rcw = TQMAX( cornerWidget( TopRight )->width(), tabBarHeight );
275 uint maxTabBarWidth = width() - lcw - rcw;
277 for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
278 if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
286 if ( d->m_CurrentMaxLength != newMaxLength ) {
287 d->m_CurrentMaxLength = newMaxLength;
288 for(
int i = 0; i < count(); ++i )
291 else if ( changeTabIndex != -1 )
292 updateTab( changeTabIndex );
295 void KTabWidget::updateTab(
int index )
297 TQString title = d->m_automaticResizeTabs ? d->m_tabNames[ index ] : TQTabWidget::label( index );
298 removeTabToolTip( page( index ) );
299 if ( title.length() > d->m_CurrentMaxLength ) {
300 if ( TQStyleSheet::mightBeRichText( title ) )
301 setTabToolTip( page( index ), TQStyleSheet::escape(title) );
303 setTabToolTip( page( index ), title );
307 title.replace(
'&',
"&&" );
309 if ( TQTabWidget::label( index ) != title )
310 TQTabWidget::setTabLabel( page( index ), title );
313 void KTabWidget::dragMoveEvent( TQDragMoveEvent *e )
315 if ( isEmptyTabbarSpace( e->pos() ) ) {
324 TQTabWidget::dragMoveEvent( e );
327 void KTabWidget::dropEvent( TQDropEvent *e )
329 if ( isEmptyTabbarSpace( e->pos() ) ) {
333 TQTabWidget::dropEvent( e );
336 #ifndef TQT_NO_WHEELEVENT
337 void KTabWidget::wheelEvent( TQWheelEvent *e )
339 if ( e->orientation() == TQt::Horizontal )
342 if ( isEmptyTabbarSpace( e->pos() ) )
343 wheelDelta( e->delta() );
348 void KTabWidget::wheelDelta(
int delta)
350 if (count()<2 || !d->m_mouseWheelScroll)
353 int page = currentPageIndex();
355 page = (page + 1) % count();
362 setCurrentPage(page);
366 void KTabWidget::mouseDoubleClickEvent( TQMouseEvent *e )
368 if( e->button() != TQt::LeftButton )
371 if ( isEmptyTabbarSpace( e->pos() ) ) {
375 TQTabWidget::mouseDoubleClickEvent( e );
378 void KTabWidget::mousePressEvent( TQMouseEvent *e )
380 if ( e->button() == TQt::RightButton ) {
381 if ( isEmptyTabbarSpace( e->pos() ) ) {
385 }
else if ( e->button() == TQt::MidButton ) {
386 if ( isEmptyTabbarSpace( e->pos() ) ) {
391 TQTabWidget::mousePressEvent( e );
421 TQString tablabel =
label( from );
422 TQWidget *w = page( from );
424 TQIconSet tabiconset = tabIconSet( w );
425 TQString tabtooltip = tabToolTip( w );
426 bool current = ( w == currentPage() );
427 bool enabled = isTabEnabled( w );
432 TQTab * t =
new TQTab();
433 t->setText(tablabel);
434 TQTabWidget::insertTab( w, t, to );
435 if ( d->m_automaticResizeTabs ) {
436 if ( to < 0 || to >= count() )
437 d->m_tabNames.append( TQString::null );
439 d->m_tabNames.insert( d->m_tabNames.at( to ), TQString::null );
444 setTabToolTip( w, tabtooltip );
448 setTabEnabled( w, enabled );
455 if ( d->m_automaticResizeTabs ) {
456 int index = indexOf( w );
458 d->m_tabNames.remove( d->m_tabNames.at( index ) );
460 TQTabWidget::removePage( w );
461 if ( d->m_automaticResizeTabs )
466 bool KTabWidget::isEmptyTabbarSpace(
const TQPoint &point )
const
468 TQSize size( tabBar()->sizeHint() );
469 if ( ( tabPosition()==Top && point.y()< size.height() ) || ( tabPosition()==Bottom && point.y()>(height()-size.height() ) ) ) {
470 TQWidget *rightcorner = cornerWidget( TopRight );
472 if ( point.x()>=width()-rightcorner->width() )
475 TQWidget *leftcorner = cornerWidget( TopLeft );
477 if ( point.x()<=leftcorner->width() )
480 TQTab *tab = tabBar()->selectTab( tabBar()->mapFromParent( point ) );
509 if ( d->m_automaticResizeTabs==enabled )
512 d->m_automaticResizeTabs = enabled;
514 d->m_tabNames.clear();
515 for(
int i = 0; i < count(); ++i )
516 d->m_tabNames.append( tabBar()->tabAt( i )->text() );
519 for(
int i = 0; i < count(); ++i )
520 tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
526 return d->m_automaticResizeTabs;
534 void KTabWidget::resizeEvent( TQResizeEvent *e )
536 TQTabWidget::resizeEvent( e );
540 #include "ktabwidget.moc"
static TQString rsqueeze(const TQString &str, uint maxlen=40)
int readNumEntry(const TQString &pKey, int nDefault=0) const
static TDEConfig * config()
TQString name(StdAccel id)