tdemditaskbar.cpp
00001 //---------------------------------------------------------------------------- 00002 // filename : tdemditaskbar.cpp 00003 //---------------------------------------------------------------------------- 00004 // Project : KDE MDI extension 00005 // 00006 // begin : 07/1999 by Szymon Stefanek as part of kvirc 00007 // (an IRC application) 00008 // changes : 09/1999 by Falk Brettschneider to create an 00009 // - 06/2000 stand-alone Qt extension set of 00010 // classes and a Qt-based library 00011 // 2000-2003 maintained by the KDevelop project 00012 // patches : 02/2000 by Massimo Morin (mmorin@schedsys.com) 00013 // 00014 // copyright : (C) 1999-2003 by Szymon Stefanek (stefanek@tin.it) 00015 // and 00016 // Falk Brettschneider 00017 // email : falkbr@kdevelop.org (Falk Brettschneider) 00018 //---------------------------------------------------------------------------- 00019 // 00020 //---------------------------------------------------------------------------- 00021 // 00022 // This program is free software; you can redistribute it and/or modify 00023 // it under the terms of the GNU Library General Public License as 00024 // published by the Free Software Foundation; either version 2 of the 00025 // License, or (at your option) any later version. 00026 // 00027 //---------------------------------------------------------------------------- 00028 00029 #ifdef None 00030 #undef None 00031 #endif 00032 00033 #include "tdemditaskbar.h" 00034 #include "tdemditaskbar.moc" 00035 00036 #include "tdemdimainfrm.h" 00037 #include "tdemdichildview.h" 00038 #include "tdemdidefines.h" 00039 00040 #include <tqtooltip.h> 00041 #include <tqlabel.h> 00042 #include <tqwidget.h> 00043 #include <tqstyle.h> 00044 00045 #include <tqnamespace.h> 00046 00047 /* 00048 @quickhelp: KMdiTaskBar 00049 @widget: Taskbar 00050 This window lists the currently open windows.<br> 00051 Each button corresponds to a single MDI (child) window.<br> 00052 The button is enabled (clickable) when the window is docked , and can be 00053 pressed to bring it to the top of the other docked windows.<br> 00054 The button text becomes red when new output is shown in the window and it is not the active one.<br> 00055 */ 00056 00057 //#################################################################### 00058 // 00059 // KMdiTaskBarButton 00060 // 00061 //#################################################################### 00062 KMdiTaskBarButton::KMdiTaskBarButton( KMdiTaskBar *pTaskBar, KMdiChildView *win_ptr ) 00063 : TQPushButton( pTaskBar ), 00064 m_actualText( "" ) 00065 { 00066 setToggleButton( true ); 00067 m_pWindow = win_ptr; 00068 TQToolTip::add 00069 ( this, win_ptr->caption() ); 00070 setFocusPolicy( TQ_NoFocus ); 00071 } 00072 00073 KMdiTaskBarButton::~KMdiTaskBarButton() 00074 {} 00075 00076 void KMdiTaskBarButton::mousePressEvent( TQMouseEvent* e ) 00077 { 00078 switch ( e->button() ) 00079 { 00080 case Qt::LeftButton: 00081 emit leftMouseButtonClicked( m_pWindow ); 00082 break; 00083 case Qt::RightButton: 00084 emit rightMouseButtonClicked( m_pWindow ); 00085 break; 00086 default: 00087 break; 00088 } 00089 emit clicked( m_pWindow ); 00090 } 00091 00093 void KMdiTaskBarButton::setNewText( const TQString& s ) 00094 { 00095 setText( s ); 00096 emit buttonTextChanged( 0 ); 00097 } 00098 00099 void KMdiTaskBarButton::setText( const TQString& s ) 00100 { 00101 m_actualText = s; 00102 TQButton::setText( s ); 00103 } 00104 00105 void KMdiTaskBarButton::fitText( const TQString& origStr, int newWidth ) 00106 { 00107 TQButton::setText( m_actualText ); 00108 00109 int actualWidth = sizeHint().width(); 00110 int realLetterCount = origStr.length(); 00111 int newLetterCount = ( newWidth * realLetterCount ) / actualWidth; 00112 int w = newWidth + 1; 00113 TQString s = origStr; 00114 while ( ( w > newWidth ) && ( newLetterCount >= 1 ) ) 00115 { 00116 if ( newLetterCount < realLetterCount ) 00117 { 00118 if ( newLetterCount > 3 ) 00119 s = origStr.left( newLetterCount / 2 ) + "..." + origStr.right( newLetterCount / 2 ); 00120 else 00121 { 00122 if ( newLetterCount > 1 ) 00123 s = origStr.left( newLetterCount ) + ".."; 00124 else 00125 s = origStr.left( 1 ); 00126 } 00127 } 00128 TQFontMetrics fm = fontMetrics(); 00129 w = fm.width( s ); 00130 newLetterCount--; 00131 } 00132 00133 TQButton::setText( s ); 00134 } 00135 00136 TQString KMdiTaskBarButton::actualText() const 00137 { 00138 return m_actualText; 00139 } 00140 00141 //#################################################################### 00142 // 00143 // KMdiTaskBar 00144 // 00145 //#################################################################### 00146 00147 KMdiTaskBar::KMdiTaskBar( KMdiMainFrm *parent, TQMainWindow::ToolBarDock dock ) 00148 : TDEToolBar( parent, "KMdiTaskBar", /*honor_style*/ false, /*readConfig*/ true ) 00149 , m_pCurrentFocusedWindow( 0 ) 00150 , m_pStretchSpace( 0 ) 00151 , m_layoutIsPending( false ) 00152 , m_bSwitchedOn( false ) 00153 { 00154 m_pFrm = parent; 00155 m_pButtonList = new TQPtrList<KMdiTaskBarButton>; 00156 m_pButtonList->setAutoDelete( true ); 00157 //QT30 setFontPropagation(TQWidget::SameFont); 00158 setMinimumWidth( 1 ); 00159 setFocusPolicy( TQ_NoFocus ); 00160 parent->moveToolBar( this, dock ); //XXX obsolete! 00161 } 00162 00163 KMdiTaskBar::~KMdiTaskBar() 00164 { 00165 delete m_pButtonList; 00166 } 00167 00168 KMdiTaskBarButton * KMdiTaskBar::addWinButton( KMdiChildView *win_ptr ) 00169 { 00170 if ( m_pStretchSpace ) 00171 { 00172 delete m_pStretchSpace; 00173 m_pStretchSpace = 0L; 00174 setStretchableWidget( 0L ); 00175 } 00176 00177 KMdiTaskBarButton *b = new KMdiTaskBarButton( this, win_ptr ); 00178 TQObject::connect( b, TQT_SIGNAL( clicked() ), win_ptr, TQT_SLOT( setFocus() ) ); 00179 TQObject::connect( b, TQT_SIGNAL( clicked( KMdiChildView* ) ), this, TQT_SLOT( setActiveButton( KMdiChildView* ) ) ); 00180 TQObject::connect( b, TQT_SIGNAL( leftMouseButtonClicked( KMdiChildView* ) ), m_pFrm, TQT_SLOT( activateView( KMdiChildView* ) ) ); 00181 TQObject::connect( b, TQT_SIGNAL( rightMouseButtonClicked( KMdiChildView* ) ), m_pFrm, TQT_SLOT( taskbarButtonRightClicked( KMdiChildView* ) ) ); 00182 TQObject::connect( b, TQT_SIGNAL( buttonTextChanged( int ) ), this, TQT_SLOT( layoutTaskBar( int ) ) ); 00183 m_pButtonList->append( b ); 00184 b->setToggleButton( true ); 00185 b->setText( win_ptr->tabCaption() ); 00186 00187 layoutTaskBar(); 00188 00189 m_pStretchSpace = new TQLabel( this, "empty" ); 00190 m_pStretchSpace->setText( "" ); 00191 setStretchableWidget( m_pStretchSpace ); 00192 m_pStretchSpace->show(); 00193 00194 if ( m_bSwitchedOn ) 00195 { 00196 b->show(); 00197 show(); 00198 } 00199 return b; 00200 } 00201 00202 void KMdiTaskBar::removeWinButton( KMdiChildView *win_ptr, bool haveToLayoutTaskBar ) 00203 { 00204 KMdiTaskBarButton * b = getButton( win_ptr ); 00205 if ( b ) 00206 { 00207 m_pButtonList->removeRef( b ); 00208 if ( haveToLayoutTaskBar ) 00209 layoutTaskBar(); 00210 } 00211 if ( m_pButtonList->count() == 0 ) 00212 { 00213 if ( m_pStretchSpace != 0L ) 00214 { 00215 delete m_pStretchSpace; 00216 m_pStretchSpace = 0L; 00217 hide(); 00218 } 00219 } 00220 } 00221 00222 void KMdiTaskBar::switchOn( bool bOn ) 00223 { 00224 m_bSwitchedOn = bOn; 00225 if ( !bOn ) 00226 { 00227 hide(); 00228 } 00229 else 00230 { 00231 if ( m_pButtonList->count() > 0 ) 00232 { 00233 show(); 00234 } 00235 else 00236 { 00237 hide(); 00238 } 00239 } 00240 } 00241 00242 KMdiTaskBarButton * KMdiTaskBar::getButton( KMdiChildView *win_ptr ) 00243 { 00244 for ( KMdiTaskBarButton * b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00245 { 00246 if ( b->m_pWindow == win_ptr ) 00247 return b; 00248 } 00249 return 0; 00250 } 00251 00252 KMdiTaskBarButton * KMdiTaskBar::getNextWindowButton( bool bRight, KMdiChildView *win_ptr ) 00253 { 00254 if ( bRight ) 00255 { 00256 for ( KMdiTaskBarButton * b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00257 { 00258 if ( b->m_pWindow == win_ptr ) 00259 { 00260 b = m_pButtonList->next(); 00261 if ( !b ) 00262 b = m_pButtonList->first(); 00263 if ( win_ptr != b->m_pWindow ) 00264 return b; 00265 else 00266 return 0; 00267 } 00268 } 00269 } 00270 else 00271 { 00272 for ( KMdiTaskBarButton * b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00273 { 00274 if ( b->m_pWindow == win_ptr ) 00275 { 00276 b = m_pButtonList->prev(); 00277 if ( !b ) 00278 b = m_pButtonList->last(); 00279 if ( win_ptr != b->m_pWindow ) 00280 return b; 00281 else 00282 return 0; 00283 } 00284 } 00285 } 00286 return 0; 00287 } 00288 00289 void KMdiTaskBar::setActiveButton( KMdiChildView *win_ptr ) 00290 { 00291 KMdiTaskBarButton * newPressedButton = 0L; 00292 KMdiTaskBarButton* oldPressedButton = 0L; 00293 for ( KMdiTaskBarButton * b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00294 { 00295 if ( b->m_pWindow == win_ptr ) 00296 newPressedButton = b; 00297 if ( b->m_pWindow == m_pCurrentFocusedWindow ) 00298 oldPressedButton = b; 00299 } 00300 00301 if ( newPressedButton != 0L && newPressedButton != oldPressedButton ) 00302 { 00303 if ( oldPressedButton != 0L ) 00304 oldPressedButton->toggle(); // switch off 00305 newPressedButton->toggle(); // switch on 00306 m_pCurrentFocusedWindow = win_ptr; 00307 } 00308 } 00309 00310 void KMdiTaskBar::layoutTaskBar( int taskBarWidth ) 00311 { 00312 if ( m_layoutIsPending ) 00313 return ; 00314 m_layoutIsPending = true; 00315 00316 if ( !taskBarWidth ) 00317 // no width is given 00318 taskBarWidth = width(); 00319 00320 // calculate current width of all taskbar buttons 00321 int allButtonsWidth = 0; 00322 KMdiTaskBarButton *b = 0; 00323 for ( b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00324 { 00325 allButtonsWidth += b->width(); 00326 } 00327 00328 // calculate actual width of all taskbar buttons 00329 int allButtonsWidthHint = 0; 00330 for ( b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00331 { 00332 TQFontMetrics fm = b->fontMetrics(); 00333 TQString s = b->actualText(); 00334 TQSize sz = fm.size( ShowPrefix, s ); 00335 int w = sz.width() + 6; 00336 int h = sz.height() + sz.height() / 8 + 10; 00337 w += h; 00338 allButtonsWidthHint += w; 00339 } 00340 00341 // if there's enough space, use actual width 00342 int buttonCount = m_pButtonList->count(); 00343 int tbHandlePixel; 00344 tbHandlePixel = style().pixelMetric( TQStyle::PM_DockWindowHandleExtent, this ); 00345 int buttonAreaWidth = taskBarWidth - tbHandlePixel - style().pixelMetric( TQStyle::PM_DefaultFrameWidth, this ) - 5; 00346 if ( ( ( allButtonsWidthHint ) <= buttonAreaWidth ) || ( width() < parentWidget() ->width() ) ) 00347 { 00348 for ( b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00349 { 00350 b->setText( b->actualText() ); 00351 if ( b->width() != b->sizeHint().width() ) 00352 { 00353 b->setFixedWidth( b->sizeHint().width() ); 00354 b->show(); 00355 } 00356 } 00357 } 00358 else 00359 { 00360 // too many buttons for actual width 00361 int newButtonWidth; 00362 if ( buttonCount != 0 ) 00363 newButtonWidth = buttonAreaWidth / buttonCount; 00364 else 00365 newButtonWidth = 0; 00366 if ( orientation() == Qt::Vertical ) 00367 newButtonWidth = 80; 00368 if ( newButtonWidth > 0 ) 00369 for ( b = m_pButtonList->first();b;b = m_pButtonList->next() ) 00370 { 00371 b->fitText( b->actualText(), newButtonWidth ); 00372 if ( b->width() != newButtonWidth ) 00373 { 00374 b->setFixedWidth( newButtonWidth ); 00375 b->show(); 00376 } 00377 } 00378 } 00379 m_layoutIsPending = false; 00380 } 00381 00382 void KMdiTaskBar::resizeEvent( TQResizeEvent* rse ) 00383 { 00384 if ( !m_layoutIsPending ) 00385 { 00386 if ( m_pButtonList->count() != 0 ) 00387 { 00388 layoutTaskBar( rse->size().width() ); 00389 } 00390 } 00391 TDEToolBar::resizeEvent( rse ); 00392 } 00393 00394 // kate: space-indent off; tab-width 4; replace-tabs off; indent-mode csands;