21 #include <tqapplication.h>
23 #include <tqpainter.h>
26 #include <tqpushbutton.h>
27 #include <tqtooltip.h>
29 #include <tdeglobalsettings.h>
30 #include <kiconloader.h>
34 #include "ktabwidget.h"
36 KTabBar::KTabBar( TQWidget *parent,
const char *name )
37 : TQTabBar( parent,
name ), mReorderStartTab( -1 ), mReorderPreviousTab( -1 ),
38 mHoverCloseButtonTab( 0 ), mDragSwitchTab( 0 ), mHoverCloseButton( 0 ),
39 mHoverCloseButtonEnabled( false ), mHoverCloseButtonDelayed( true ),
40 mTabReorderingEnabled( false ), mTabCloseActivatePrevious( false )
42 setAcceptDrops(
true );
43 setMouseTracking(
true );
45 mEnableCloseButtonTimer =
new TQTimer(
this );
46 connect( mEnableCloseButtonTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( enableCloseButton() ) );
48 mActivateDragSwitchTabTimer =
new TQTimer(
this );
49 connect( mActivateDragSwitchTabTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( activateDragSwitchTab() ) );
51 connect(
this, TQ_SIGNAL(layoutChanged()), TQ_SLOT(onLayoutChange()));
60 void KTabBar::setTabEnabled(
int id,
bool enabled )
62 TQTab * t = tab(
id );
64 if ( t->isEnabled() != enabled ) {
65 t->setEnabled( enabled );
66 TQRect r( t->rect() );
67 if ( !enabled &&
id == currentTab() && count()>1 ) {
68 TQPtrList<TQTab> *tablist = tabList();
69 if ( mTabCloseActivatePrevious )
70 t = tablist->at( count()-2 );
72 int index = indexOf(
id );
73 index += ( index+1 == count() ) ? -1 : 1;
77 if ( t->isEnabled() ) {
78 r = r.unite( t->rect() );
79 tablist->append( tablist->take( tablist->findRef( t ) ) );
80 emit selected( t->identifier() );
88 void KTabBar::mouseDoubleClickEvent( TQMouseEvent *e )
90 if( e->button() != TQt::LeftButton )
93 TQTab *tab = selectTab( e->pos() );
95 emit( mouseDoubleClick( indexOf( tab->identifier() ) ) );
98 TQTabBar::mouseDoubleClickEvent( e );
101 void KTabBar::mousePressEvent( TQMouseEvent *e )
103 if( e->button() == TQt::LeftButton ) {
104 mEnableCloseButtonTimer->stop();
105 mDragStart = e->pos();
107 else if( e->button() == TQt::RightButton ) {
108 TQTab *tab = selectTab( e->pos() );
110 emit( contextMenu( indexOf( tab->identifier() ), mapToGlobal( e->pos() ) ) );
114 TQTabBar::mousePressEvent( e );
117 void KTabBar::mouseMoveEvent( TQMouseEvent *e )
119 if ( e->state() == TQt::LeftButton ) {
120 TQTab *tab = selectTab( e->pos() );
121 if ( mDragSwitchTab && tab != mDragSwitchTab ) {
122 mActivateDragSwitchTabTimer->stop();
127 TQPoint newPos = e->pos();
128 if( newPos.x() > mDragStart.x()+delay || newPos.x() < mDragStart.x()-delay ||
129 newPos.y() > mDragStart.y()+delay || newPos.y() < mDragStart.y()-delay )
132 emit( initiateDrag( indexOf( tab->identifier() ) ) );
137 else if ( e->state() == TQt::MidButton ) {
138 if (mReorderStartTab==-1) {
140 TQPoint newPos = e->pos();
141 if( newPos.x() > mDragStart.x()+delay || newPos.x() < mDragStart.x()-delay ||
142 newPos.y() > mDragStart.y()+delay || newPos.y() < mDragStart.y()-delay )
144 TQTab *tab = selectTab( e->pos() );
145 if( tab && mTabReorderingEnabled ) {
146 mReorderStartTab = indexOf( tab->identifier() );
147 grabMouse( TQt::sizeAllCursor );
153 TQTab *tab = selectTab( e->pos() );
155 int reorderStopTab = indexOf( tab->identifier() );
156 if ( mReorderStartTab!=reorderStopTab && mReorderPreviousTab!=reorderStopTab ) {
157 emit( moveTab( mReorderStartTab, reorderStopTab ) );
158 mReorderPreviousTab=mReorderStartTab;
159 mReorderStartTab=reorderStopTab;
166 if ( mHoverCloseButtonEnabled && mReorderStartTab==-1) {
167 TQTab *t = selectTab( e->pos() );
168 if( t && t->iconSet() && t->isEnabled() ) {
169 TQPixmap pixmap = t->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal );
170 TQRect rect( 0, 0, pixmap.width() + 4, pixmap.height() +4);
172 int xoff = 0, yoff = 0;
174 if ( t == tab( currentTab() ) ) {
175 xoff = style().pixelMetric( TQStyle::PM_TabBarTabShiftHorizontal,
this ) + 3;
176 yoff = style().pixelMetric( TQStyle::PM_TabBarTabShiftVertical,
this ) - 4;
182 rect.moveLeft( t->rect().left() + 2 + xoff );
183 rect.moveTop( t->rect().center().y()-pixmap.height()/2 + yoff );
184 if ( rect.contains( e->pos() ) ) {
185 if ( mHoverCloseButton ) {
186 if ( mHoverCloseButtonTab == t )
188 mEnableCloseButtonTimer->stop();
189 mHoverCloseButton->deleteLater();
190 mHoverCloseButton = 0;
193 mHoverCloseButton =
new TQPushButton(
this );
195 mHoverCloseButton->setGeometry( rect );
196 TQToolTip::add(mHoverCloseButton,i18n(
"Close this tab"));
197 mHoverCloseButton->setFlat(
true);
198 mHoverCloseButton->show();
199 if ( mHoverCloseButtonDelayed ) {
200 mHoverCloseButton->setEnabled(
false);
201 mEnableCloseButtonTimer->start( TQApplication::doubleClickInterval(),
true );
203 mHoverCloseButtonTab = t;
204 connect( mHoverCloseButton, TQ_SIGNAL( clicked() ), TQ_SLOT( closeButtonClicked() ) );
208 if ( mHoverCloseButton ) {
209 mEnableCloseButtonTimer->stop();
210 mHoverCloseButton->deleteLater();
211 mHoverCloseButton = 0;
215 TQTabBar::mouseMoveEvent( e );
218 void KTabBar::enableCloseButton()
220 mHoverCloseButton->setEnabled(
true);
223 void KTabBar::activateDragSwitchTab()
225 TQTab *tab = selectTab( mapFromGlobal( TQCursor::pos() ) );
226 if ( tab && mDragSwitchTab == tab )
227 setCurrentTab( mDragSwitchTab );
231 void KTabBar::mouseReleaseEvent( TQMouseEvent *e )
233 if( e->button() == TQt::MidButton ) {
234 if ( mReorderStartTab==-1 ) {
235 TQTab *tab = selectTab( e->pos() );
237 emit( mouseMiddleClick( indexOf( tab->identifier() ) ) );
243 setCursor( TQt::arrowCursor );
245 mReorderPreviousTab=-1;
248 TQTabBar::mouseReleaseEvent( e );
251 void KTabBar::dragMoveEvent( TQDragMoveEvent *e )
253 TQTab *tab = selectTab( e->pos() );
258 emit testCanDecode( e, accept);
259 if ( accept && tab != TQTabBar::tab( currentTab() ) ) {
260 mDragSwitchTab = tab;
261 mActivateDragSwitchTabTimer->start( TQApplication::doubleClickInterval()*2,
true );
267 TQTabBar::dragMoveEvent( e );
270 void KTabBar::dropEvent( TQDropEvent *e )
272 TQTab *tab = selectTab( e->pos() );
274 mActivateDragSwitchTabTimer->stop();
276 emit( receivedDropEvent( indexOf( tab->identifier() ) , e ) );
279 TQTabBar::dropEvent( e );
282 #ifndef TQT_NO_WHEELEVENT
283 void KTabBar::wheelEvent( TQWheelEvent *e )
285 if ( e->orientation() == TQt::Horizontal )
288 emit( wheelDelta( e->delta() ) );
292 void KTabBar::setTabColor(
int id,
const TQColor& color )
294 TQTab *t = tab(
id );
296 mTabColors.insert(
id, color );
297 repaint( t->rect(),
false );
301 void KTabBar::resetTabColor(
int id )
305 if (mTabColors.contains(
id))
306 mTabColors.remove(
id);
307 repaint(t->rect(),
false);
311 const TQColor &KTabBar::tabColor(
int id )
const
313 if ( mTabColors.contains(
id) && mTabColors[
id].isValid() )
314 return mTabColors[id];
316 return colorGroup().foreground();
319 int KTabBar::insertTab( TQTab *t,
int index )
321 int res = TQTabBar::insertTab( t, index );
323 if ( mTabCloseActivatePrevious && count() > 2 ) {
324 TQPtrList<TQTab> *tablist = tabList();
325 tablist->insert( count()-2, tablist->take( tablist->findRef( t ) ) );
331 void KTabBar::removeTab( TQTab *t )
333 mTabColors.remove( t->identifier() );
334 TQTabBar::removeTab( t );
337 void KTabBar::paintLabel( TQPainter *p,
const TQRect& br,
338 TQTab *t,
bool has_focus )
const
341 bool selected = currentTab() == t->identifier();
342 if ( t->iconSet() ) {
344 TQIconSet::Mode mode = ( t->isEnabled() && isEnabled() )
345 ? TQIconSet::Normal : TQIconSet::Disabled;
346 if ( mode == TQIconSet::Normal && has_focus )
347 mode = TQIconSet::Active;
348 TQPixmap pixmap = t->iconSet()->pixmap( TQIconSet::Small, mode );
349 int pixw = pixmap.width();
350 int pixh = pixmap.height();
351 r.setLeft( r.left() + pixw + 4 );
352 r.setRight( r.right() + 2 );
354 int inactiveXShift = style().pixelMetric( TQStyle::PM_TabBarTabShiftHorizontal,
this );
355 int inactiveYShift = style().pixelMetric( TQStyle::PM_TabBarTabShiftVertical,
this );
357 int right = t->text().isEmpty() ? br.right() - pixw : br.left() + 2;
359 p->drawPixmap( right + (selected ? 0 : inactiveXShift),
360 br.center().y() - pixh / 2 + (selected ? 0 : inactiveYShift),
364 TQStyle::SFlags flags = TQStyle::Style_Default;
366 if ( isEnabled() && t->isEnabled() )
367 flags |= TQStyle::Style_Enabled;
369 flags |= TQStyle::Style_HasFocus;
371 TQColorGroup cg( colorGroup() );
372 if ( mTabColors.contains(t->identifier()) && mTabColors[t->identifier()].isValid() )
373 cg.setColor( TQColorGroup::Foreground, mTabColors[t->identifier()] );
375 style().drawControl( TQStyle::CE_TabBarLabel, p,
this, r,
376 t->isEnabled() ? cg : palette().disabled(),
377 flags, TQStyleOption(t) );
380 bool KTabBar::isTabReorderingEnabled()
const
382 return mTabReorderingEnabled;
385 void KTabBar::setTabReorderingEnabled(
bool on )
387 mTabReorderingEnabled = on;
390 bool KTabBar::tabCloseActivatePrevious()
const
392 return mTabCloseActivatePrevious;
395 void KTabBar::setTabCloseActivatePrevious(
bool on )
397 mTabCloseActivatePrevious = on;
400 void KTabBar::closeButtonClicked()
402 emit closeRequest( indexOf( mHoverCloseButtonTab->identifier() ) );
405 void KTabBar::setHoverCloseButton(
bool button )
407 mHoverCloseButtonEnabled = button;
412 bool KTabBar::hoverCloseButton()
const
414 return mHoverCloseButtonEnabled;
417 void KTabBar::setHoverCloseButtonDelayed(
bool delayed )
419 mHoverCloseButtonDelayed = delayed;
422 bool KTabBar::hoverCloseButtonDelayed()
const
424 return mHoverCloseButtonDelayed;
427 void KTabBar::onLayoutChange()
429 mEnableCloseButtonTimer->stop();
430 delete mHoverCloseButton;
431 mHoverCloseButton = 0;
432 mHoverCloseButtonTab = 0;
433 mActivateDragSwitchTabTimer->stop();
437 #include "ktabbar.moc"
static int dndEventDelay()
static TDEIconLoader * iconLoader()
TQString name(StdAccel id)