tabbox.cpp

00001 /*****************************************************************
00002  KWin - the KDE window manager
00003  This file is part of the KDE project.
00004 
00005 Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
00006 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
00007 
00008 You can Freely distribute this program under the GNU General Public
00009 License. See the file "COPYING" for the exact licensing terms.
00010 ******************************************************************/
00011 
00012 //#define QT_CLEAN_NAMESPACE
00013 #include "tabbox.h"
00014 #include "workspace.h"
00015 #include "client.h"
00016 #include <qpainter.h>
00017 #include <qlabel.h>
00018 #include <qdrawutil.h>
00019 #include <qstyle.h>
00020 #include <kglobal.h>
00021 #include <fixx11h.h>
00022 #include <kconfig.h>
00023 #include <klocale.h>
00024 #include <qapplication.h>
00025 #include <qdesktopwidget.h>
00026 #include <kstringhandler.h>
00027 #include <stdarg.h>
00028 #include <kdebug.h>
00029 #include <kglobalaccel.h>
00030 #include <kkeynative.h>
00031 #include <kglobalsettings.h>
00032 #include <kiconeffect.h>
00033 #include <X11/keysym.h>
00034 #include <X11/keysymdef.h>
00035 
00036 // specify externals before namespace
00037 
00038 extern Time qt_x_time;
00039 
00040 namespace KWinInternal
00041 {
00042 
00043 extern QPixmap* kwin_get_menu_pix_hack();
00044 
00045 TabBox::TabBox( Workspace *ws, const char *name )
00046     : QFrame( 0, name, Qt::WNoAutoErase ), current_client( NULL ), wspace(ws)
00047     {
00048     setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
00049     setLineWidth(2);
00050     setMargin(2);
00051 
00052     showMiniIcon = false;
00053 
00054     no_tasks = i18n("*** No Windows ***");
00055     m = DesktopMode; // init variables
00056     updateKeyMapping();
00057     reconfigure();
00058     reset();
00059     connect(&delayedShowTimer, SIGNAL(timeout()), this, SLOT(show()));
00060     
00061     XSetWindowAttributes attr;
00062     attr.override_redirect = 1;
00063     outline_left = XCreateWindow( qt_xdisplay(), qt_xrootwin(), 0, 0, 1, 1, 0,
00064         CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect, &attr );
00065     outline_right = XCreateWindow( qt_xdisplay(), qt_xrootwin(), 0, 0, 1, 1, 0,
00066         CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect, &attr );
00067     outline_top = XCreateWindow( qt_xdisplay(), qt_xrootwin(), 0, 0, 1, 1, 0,
00068         CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect, &attr );
00069     outline_bottom = XCreateWindow( qt_xdisplay(), qt_xrootwin(), 0, 0, 1, 1, 0,
00070         CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect, &attr );
00071     }
00072 
00073 TabBox::~TabBox()
00074     {
00075     XDestroyWindow( qt_xdisplay(), outline_left );
00076     XDestroyWindow( qt_xdisplay(), outline_right );
00077     XDestroyWindow( qt_xdisplay(), outline_top );
00078     XDestroyWindow( qt_xdisplay(), outline_bottom );
00079     }
00080 
00081 
00087 void TabBox::setMode( Mode mode )
00088     {
00089     m = mode;
00090     }
00091 
00092 
00096 void TabBox::createClientList(ClientList &list, int desktop /*-1 = all*/, Client *c, bool chain)
00097     {
00098     ClientList::size_type idx = 0;
00099 
00100     list.clear();
00101 
00102     Client* start = c;
00103 
00104     if ( chain )
00105         c = workspace()->nextFocusChainClient(c);
00106     else
00107         c = workspace()->stackingOrder().first();
00108 
00109     Client* stop = c;
00110 
00111     while ( c )
00112         {
00113         Client* add = NULL;
00114         if ( ((desktop == -1) || c->isOnDesktop(desktop))
00115              && c->wantsTabFocus() )
00116             { // don't add windows that have modal dialogs
00117             Client* modal = c->findModal();
00118             if( modal == NULL || modal == c )
00119                 add = c;
00120             else if( !list.contains( modal ))
00121                 add = modal;
00122             else
00123                 {
00124                 // nothing
00125                 }
00126             }
00127 
00128         if( !options->separateScreenFocus && options->xineramaEnabled )
00129             {
00130             if( c->screen() != workspace()->activeScreen())
00131                 add = NULL;
00132             }
00133 
00134         if( add != NULL )
00135             {
00136             if ( start == add )
00137                 {
00138                 list.remove( add );
00139                 list.prepend( add );
00140                 }
00141             else
00142                 list += add;
00143             }
00144 
00145         if ( chain )
00146           c = workspace()->nextFocusChainClient( c );
00147         else
00148           {
00149           if ( idx >= (workspace()->stackingOrder().size()-1) )
00150             c = 0;
00151           else
00152             c = workspace()->stackingOrder()[++idx];
00153           }
00154 
00155         if ( c == stop )
00156             break;
00157         }
00158     }
00159 
00160 
00165 void TabBox::reset()
00166     {
00167     int w, h, cw = 0, wmax = 0;
00168 
00169     QRect r = workspace()->screenGeometry( workspace()->activeScreen());
00170 
00171     // calculate height of 1 line
00172     // fontheight + 1 pixel above + 1 pixel below, or 32x32 icon + 2 pixel above + below
00173     lineHeight = QMAX(fontMetrics().height() + 2, 32 + 4);
00174 
00175     if ( mode() == WindowsMode )
00176         {
00177         setCurrentClient( workspace()->activeClient());
00178 
00179         // get all clients to show
00180         createClientList(clients, options_traverse_all ? -1 : workspace()->currentDesktop(), current_client, true);
00181 
00182         // calculate maximum caption width
00183         cw = fontMetrics().width(no_tasks)+20;
00184         for (ClientList::ConstIterator it = clients.begin(); it != clients.end(); ++it)
00185           {
00186           cw = fontMetrics().width( (*it)->caption() );
00187           if ( cw > wmax ) wmax = cw;
00188           }
00189 
00190         // calculate height for the popup
00191         if ( clients.count() == 0 )  // height for the "not tasks" text
00192           {
00193           QFont f = font();
00194           f.setBold( TRUE );
00195           f.setPointSize( 14 );
00196 
00197           h = QFontMetrics(f).height()*4;
00198           }
00199         else
00200           {
00201           showMiniIcon = false;
00202           h = clients.count() * lineHeight;
00203 
00204           if ( h > (r.height()-(2*frameWidth())) )  // if too high, use mini icons
00205             {
00206             showMiniIcon = true;
00207             // fontheight + 1 pixel above + 1 pixel below, or 16x16 icon + 1 pixel above + below
00208             lineHeight = QMAX(fontMetrics().height() + 2, 16 + 2);
00209 
00210             h = clients.count() * lineHeight;
00211 
00212             if ( h > (r.height()-(2*frameWidth())) ) // if still too high, remove some clients
00213               {
00214                 // how many clients to remove
00215                 int howMany = (h - (r.height()-(2*frameWidth())))/lineHeight;
00216                 for (; howMany; howMany--)
00217                   clients.remove(clients.last());
00218 
00219                 h = clients.count() * lineHeight;
00220               }
00221             }
00222           }
00223         }
00224     else
00225         { // DesktopListMode
00226         showMiniIcon = false;
00227         desk = workspace()->currentDesktop();
00228 
00229         for ( int i = 1; i <= workspace()->numberOfDesktops(); i++ )
00230           {
00231           cw = fontMetrics().width( workspace()->desktopName(i) );
00232           if ( cw > wmax ) wmax = cw;
00233           }
00234 
00235         // calculate height for the popup (max. 16 desktops always fit in a 800x600 screen)
00236         h = workspace()->numberOfDesktops() * lineHeight;
00237         }
00238 
00239     // height, width for the popup
00240     h += 2 * frameWidth();
00241     w = 2*frameWidth() + 5*2 + ( showMiniIcon ? 16 : 32 ) + 8 + wmax; // 5*2=margins, ()=icon, 8=space between icon+text
00242     w = kClamp( w, r.width()/3 , r.width() * 4 / 5 );
00243 
00244     setGeometry( (r.width()-w)/2 + r.x(),
00245                  (r.height()-h)/2+ r.y(),
00246                  w, h );
00247     }
00248 
00249 
00253 void TabBox::nextPrev( bool next)
00254     {
00255     if ( mode() == WindowsMode )
00256         {
00257         Client* firstClient = NULL;
00258         Client* client = current_client;
00259         do
00260             {
00261             if ( next )
00262                 client = workspace()->nextFocusChainClient(client);
00263             else
00264                 client = workspace()->previousFocusChainClient(client);
00265             if (!firstClient)
00266                 {
00267         // When we see our first client for the second time,
00268         // it's time to stop.
00269                 firstClient = client;
00270                 }
00271             else if (client == firstClient)
00272                 {
00273         // No candidates found.
00274                 client = 0;
00275                 break;
00276                 }
00277             } while ( client && !clients.contains( client ));
00278         setCurrentClient( client );
00279         }
00280     else if( mode() == DesktopMode )
00281         {
00282         if ( next )
00283             desk = workspace()->nextDesktopFocusChain( desk );
00284         else
00285             desk = workspace()->previousDesktopFocusChain( desk );
00286         }
00287     else
00288         { // DesktopListMode
00289         if ( next )
00290             {
00291             desk++;
00292             if ( desk > workspace()->numberOfDesktops() )
00293                 desk = 1;
00294             }
00295         else
00296             {
00297             desk--;
00298             if ( desk < 1 )
00299                 desk = workspace()->numberOfDesktops();
00300             }
00301         }
00302 
00303     update();
00304     }
00305 
00306 
00307 
00312 Client* TabBox::currentClient()
00313     {
00314     if ( mode() != WindowsMode )
00315         return 0;
00316     if (!workspace()->hasClient( current_client ))
00317         return 0;
00318     return current_client;
00319     }
00320 
00321 void TabBox::setCurrentClient( Client* c )
00322     {
00323     if( current_client != c )
00324         {
00325         current_client = c;
00326         updateOutline();
00327         }
00328     }
00329 
00335 int TabBox::currentDesktop()
00336     {
00337     if ( mode() == DesktopListMode || mode() == DesktopMode )
00338         return desk;
00339     else
00340         return -1;
00341     }
00342 
00343 
00347 void TabBox::showEvent( QShowEvent* )
00348     {
00349     updateOutline();
00350     XRaiseWindow( qt_xdisplay(), outline_left );
00351     XRaiseWindow( qt_xdisplay(), outline_right );
00352     XRaiseWindow( qt_xdisplay(), outline_top );
00353     XRaiseWindow( qt_xdisplay(), outline_bottom );
00354     raise();
00355     }
00356 
00357 
00361 void TabBox::hideEvent( QHideEvent* )
00362     {
00363     XUnmapWindow( qt_xdisplay(), outline_left );
00364     XUnmapWindow( qt_xdisplay(), outline_right );
00365     XUnmapWindow( qt_xdisplay(), outline_top );
00366     XUnmapWindow( qt_xdisplay(), outline_bottom );
00367     }
00368 
00372 void TabBox::drawContents( QPainter * )
00373     {
00374     QRect r(contentsRect());
00375     QPixmap pix(r.size());  // do double buffering to avoid flickers
00376     pix.fill(this, 0, 0);
00377 
00378     QPainter p;
00379     p.begin(&pix, this);
00380 
00381     QPixmap* menu_pix = kwin_get_menu_pix_hack();
00382 
00383     int iconWidth = showMiniIcon ? 16 : 32;
00384     int x = 0;
00385     int y = 0;
00386 
00387     if ( mode () == WindowsMode )
00388         {
00389         if ( !currentClient() )
00390             {
00391             QFont f = font();
00392             f.setBold( TRUE );
00393             f.setPointSize( 14 );
00394 
00395             p.setFont(f);
00396             p.drawText( r, AlignCenter, no_tasks);
00397             }
00398         else
00399             {
00400             for (ClientList::ConstIterator it = clients.begin(); it != clients.end(); ++it)
00401               {
00402               if ( workspace()->hasClient( *it ) )  // safety
00403                   {
00404                   // draw highlight background
00405                   if ( (*it) == current_client )
00406                     p.fillRect(x, y, r.width(), lineHeight, colorGroup().highlight());
00407 
00408                   // draw icon
00409                   QPixmap icon;
00410                   if ( showMiniIcon )
00411                     {
00412                     if ( !(*it)->miniIcon().isNull() )
00413                       icon = (*it)->miniIcon();
00414                     }
00415                   else
00416                     if ( !(*it)->icon().isNull() )
00417                       icon = (*it)->icon();
00418                     else if ( menu_pix )
00419                       icon = *menu_pix;
00420                 
00421                   if( !icon.isNull())
00422                     {
00423                     if( (*it)->isMinimized())
00424                         KIconEffect::semiTransparent( icon );
00425                     p.drawPixmap( x+5, y + (lineHeight - iconWidth)/2, icon );
00426                     }
00427 
00428                   // generate text to display
00429                   QString s;
00430 
00431                   if ( !(*it)->isOnDesktop(workspace()->currentDesktop()) )
00432                     s = workspace()->desktopName((*it)->desktop()) + ": ";
00433 
00434                   if ( (*it)->isMinimized() )
00435                     s += QString("(") + (*it)->caption() + ")";
00436                   else
00437                     s += (*it)->caption();
00438 
00439                   s = KStringHandler::cPixelSqueeze(s, fontMetrics(), r.width() - 5 - iconWidth - 8);
00440 
00441                   // draw text
00442                   if ( (*it) == current_client )
00443                     p.setPen(colorGroup().highlightedText());
00444                   else if( (*it)->isMinimized())
00445                     {
00446                     QColor c1 = colorGroup().text();
00447                     QColor c2 = colorGroup().background();
00448                     // from kicker's TaskContainer::blendColors()
00449                     int r1, g1, b1;
00450                     int r2, g2, b2;
00451 
00452                     c1.rgb( &r1, &g1, &b1 );
00453                     c2.rgb( &r2, &g2, &b2 );
00454 
00455                     r1 += (int) ( .5 * ( r2 - r1 ) );
00456                     g1 += (int) ( .5 * ( g2 - g1 ) );
00457                     b1 += (int) ( .5 * ( b2 - b1 ) );
00458 
00459                     p.setPen(QColor( r1, g1, b1 ));
00460                     }
00461                   else
00462                     p.setPen(colorGroup().text());
00463 
00464                   p.drawText(x+5 + iconWidth + 8, y, r.width() - 5 - iconWidth - 8, lineHeight,
00465                               Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, s);
00466 
00467                   y += lineHeight;
00468                   }
00469               if ( y >= r.height() ) break;
00470               }
00471             }
00472         }
00473     else
00474         { // DesktopMode || DesktopListMode
00475         int iconHeight = iconWidth;
00476 
00477         // get widest desktop name/number
00478         QFont f(font());
00479         f.setBold(true);
00480         f.setPixelSize(iconHeight - 4);  // pixel, not point because I need to know the pixels
00481         QFontMetrics fm(f);
00482 
00483         int wmax = 0;
00484         for ( int i = 1; i <= workspace()->numberOfDesktops(); i++ )
00485             {
00486             wmax = QMAX(wmax, fontMetrics().width(workspace()->desktopName(i)));
00487 
00488             // calculate max width of desktop-number text
00489             QString num = QString::number(i);
00490             iconWidth = QMAX(iconWidth - 4, fm.boundingRect(num).width()) + 4;
00491             }
00492 
00493         // In DesktopMode, start at the current desktop
00494         // In DesktopListMode, start at desktop #1
00495         int iDesktop = (mode() == DesktopMode) ? workspace()->currentDesktop() : 1;
00496         for ( int i = 1; i <= workspace()->numberOfDesktops(); i++ )
00497             {
00498             // draw highlight background
00499             if ( iDesktop == desk )  // current desktop
00500               p.fillRect(x, y, r.width(), lineHeight, colorGroup().highlight());
00501 
00502             p.save();
00503 
00504             // draw "icon" (here: number of desktop)
00505             p.fillRect(x+5, y+2, iconWidth, iconHeight, colorGroup().base());
00506             p.setPen(colorGroup().text());
00507             p.drawRect(x+5, y+2, iconWidth, iconHeight);
00508 
00509             // draw desktop-number
00510             p.setFont(f);
00511             QString num = QString::number(iDesktop);
00512             p.drawText(x+5, y+2, iconWidth, iconHeight, Qt::AlignCenter, num);
00513 
00514             p.restore();
00515 
00516             // draw desktop name text
00517             if ( iDesktop == desk )
00518               p.setPen(colorGroup().highlightedText());
00519             else
00520               p.setPen(colorGroup().text());
00521 
00522             p.drawText(x+5 + iconWidth + 8, y, r.width() - 5 - iconWidth - 8, lineHeight,
00523                        Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine,
00524                        workspace()->desktopName(iDesktop));
00525 
00526             // show mini icons from that desktop aligned to each other
00527             int x1 = x + 5 + iconWidth + 8 + wmax + 5;
00528 
00529             ClientList list;
00530             createClientList(list, iDesktop, 0, false);
00531             // clients are in reversed stacking order
00532             for (ClientList::ConstIterator it = list.fromLast(); it != list.end(); --it)
00533               {
00534               if ( !(*it)->miniIcon().isNull() )
00535                 {
00536                 if ( x1+18 >= x+r.width() )  // only show full icons
00537                   break;
00538 
00539                 p.drawPixmap( x1, y + (lineHeight - 16)/2, (*it)->miniIcon() );
00540                 x1 += 18;
00541                 }
00542               }
00543 
00544             // next desktop
00545             y += lineHeight;
00546             if ( y >= r.height() ) break;
00547 
00548             if( mode() == DesktopMode )
00549                 iDesktop = workspace()->nextDesktopFocusChain( iDesktop );
00550             else
00551                 iDesktop++;
00552             }
00553         }
00554     p.end();
00555     bitBlt(this, r.x(), r.y(), &pix);
00556     }
00557 
00558 void TabBox::updateOutline()
00559     {
00560     Client* c = currentClient();
00561     if( c == NULL || this->isHidden() || !c->isShown( true ) || !c->isOnCurrentDesktop())
00562         {
00563         XUnmapWindow( qt_xdisplay(), outline_left );
00564         XUnmapWindow( qt_xdisplay(), outline_right );
00565         XUnmapWindow( qt_xdisplay(), outline_top );
00566         XUnmapWindow( qt_xdisplay(), outline_bottom );
00567         return;
00568         }
00569     // left/right parts are between top/bottom, they don't reach as far as the corners
00570     XMoveResizeWindow( qt_xdisplay(), outline_left, c->x(), c->y() + 5, 5, c->height() - 10 );
00571     XMoveResizeWindow( qt_xdisplay(), outline_right, c->x() + c->width() - 5, c->y() + 5, 5, c->height() - 10 );
00572     XMoveResizeWindow( qt_xdisplay(), outline_top, c->x(), c->y(), c->width(), 5 );
00573     XMoveResizeWindow( qt_xdisplay(), outline_bottom, c->x(), c->y() + c->height() - 5, c->width(), 5 );
00574     {
00575     QPixmap pix( 5, c->height() - 10 );
00576     QPainter p( &pix );
00577     p.setPen( white );
00578     p.drawLine( 0, 0, 0, pix.height() - 1 );
00579     p.drawLine( 4, 0, 4, pix.height() - 1 );
00580     p.setPen( gray );
00581     p.drawLine( 1, 0, 1, pix.height() - 1 );
00582     p.drawLine( 3, 0, 3, pix.height() - 1 );
00583     p.setPen( black );
00584     p.drawLine( 2, 0, 2, pix.height() - 1 );
00585     p.end();
00586     XSetWindowBackgroundPixmap( qt_xdisplay(), outline_left, pix.handle());
00587     XSetWindowBackgroundPixmap( qt_xdisplay(), outline_right, pix.handle());
00588     }
00589     {
00590     QPixmap pix( c->width(), 5 );
00591     QPainter p( &pix );
00592     p.setPen( white );
00593     p.drawLine( 0, 0, pix.width() - 1 - 0, 0 );
00594     p.drawLine( 4, 4, pix.width() - 1 - 4, 4 );
00595     p.drawLine( 0, 0, 0, 4 );
00596     p.drawLine( pix.width() - 1 - 0, 0, pix.width() - 1 - 0, 4 );
00597     p.setPen( gray );
00598     p.drawLine( 1, 1, pix.width() - 1 - 1, 1 );
00599     p.drawLine( 3, 3, pix.width() - 1 - 3, 3 );
00600     p.drawLine( 1, 1, 1, 4 );
00601     p.drawLine( 3, 3, 3, 4 );
00602     p.drawLine( pix.width() - 1 - 1, 1, pix.width() - 1 - 1, 4 );
00603     p.drawLine( pix.width() - 1 - 3, 3, pix.width() - 1 - 3, 4 );
00604     p.setPen( black );
00605     p.drawLine( 2, 2, pix.width() - 1 - 2, 2 );
00606     p.drawLine( 2, 2, 2, 4 );
00607     p.drawLine( pix.width() - 1 - 2, 2, pix.width() - 1 - 2, 4 );
00608     p.end();
00609     XSetWindowBackgroundPixmap( qt_xdisplay(), outline_top, pix.handle());
00610     }
00611     {
00612     QPixmap pix( c->width(), 5 );
00613     QPainter p( &pix );
00614     p.setPen( white );
00615     p.drawLine( 4, 0, pix.width() - 1 - 4, 0 );
00616     p.drawLine( 0, 4, pix.width() - 1 - 0, 4 );
00617     p.drawLine( 0, 4, 0, 0 );
00618     p.drawLine( pix.width() - 1 - 0, 4, pix.width() - 1 - 0, 0 );
00619     p.setPen( gray );
00620     p.drawLine( 3, 1, pix.width() - 1 - 3, 1 );
00621     p.drawLine( 1, 3, pix.width() - 1 - 1, 3 );
00622     p.drawLine( 3, 1, 3, 0 );
00623     p.drawLine( 1, 3, 1, 0 );
00624     p.drawLine( pix.width() - 1 - 3, 1, pix.width() - 1 - 3, 0 );
00625     p.drawLine( pix.width() - 1 - 1, 3, pix.width() - 1 - 1, 0 );
00626     p.setPen( black );
00627     p.drawLine( 2, 2, pix.width() - 1 - 2, 2 );
00628     p.drawLine( 2, 0, 2, 2 );
00629     p.drawLine( pix.width() - 1 - 2, 0, pix.width() - 1 - 2, 2 );
00630     p.end();
00631     XSetWindowBackgroundPixmap( qt_xdisplay(), outline_bottom, pix.handle());
00632     }
00633     XClearWindow( qt_xdisplay(), outline_left );
00634     XClearWindow( qt_xdisplay(), outline_right );
00635     XClearWindow( qt_xdisplay(), outline_top );
00636     XClearWindow( qt_xdisplay(), outline_bottom );
00637     XMapWindow( qt_xdisplay(), outline_left );
00638     XMapWindow( qt_xdisplay(), outline_right );
00639     XMapWindow( qt_xdisplay(), outline_top );
00640     XMapWindow( qt_xdisplay(), outline_bottom );
00641     }
00642 
00643 void TabBox::hide()
00644     {
00645     delayedShowTimer.stop();
00646     QWidget::hide();
00647     QApplication::syncX();
00648     XEvent otherEvent;
00649     while (XCheckTypedEvent (qt_xdisplay(), EnterNotify, &otherEvent ) )
00650         ;
00651     }
00652 
00653 
00654 void TabBox::reconfigure()
00655     {
00656     KConfig * c(KGlobal::config());
00657     c->setGroup("TabBox");
00658     options_traverse_all = c->readBoolEntry("TraverseAll", false );
00659     }
00660 
00679 void TabBox::delayedShow()
00680     {
00681     KConfig * c(KGlobal::config());
00682     c->setGroup("TabBox");
00683     bool delay = c->readBoolEntry("ShowDelay", true);
00684 
00685     if (!delay)
00686         {
00687         show();
00688         return;
00689         }
00690 
00691     int delayTime = c->readNumEntry("DelayTime", 90);
00692     delayedShowTimer.start(delayTime, true);
00693     }
00694 
00695 
00696 void TabBox::handleMouseEvent( XEvent* e )
00697     {
00698     XAllowEvents( qt_xdisplay(), AsyncPointer, qt_x_time );
00699     if( e->type != ButtonPress )
00700         return;
00701     QPoint pos( e->xbutton.x_root, e->xbutton.y_root );
00702     if( !geometry().contains( pos ))
00703         {
00704         workspace()->closeTabBox();  // click outside closes tab
00705         return;
00706         }
00707     pos.rx() -= x(); // pos is now inside tabbox
00708     pos.ry() -= y();
00709     int num = (pos.y()-frameWidth()) / lineHeight;
00710 
00711     if( mode() == WindowsMode )
00712         {
00713         for( ClientList::ConstIterator it = clients.begin();
00714              it != clients.end();
00715              ++it)
00716             {
00717             if( workspace()->hasClient( *it ) && (num == 0) ) // safety
00718                 {
00719                 setCurrentClient( *it );
00720                 break;
00721                 }
00722             num--;
00723             }
00724         }
00725     else
00726         {
00727         int iDesktop = (mode() == DesktopMode) ? workspace()->currentDesktop() : 1;
00728         for( int i = 1;
00729              i <= workspace()->numberOfDesktops();
00730              ++i )
00731             {
00732             if( num == 0 )
00733                 {
00734                 desk = iDesktop;
00735                 break;
00736                 }
00737             num--;
00738             if( mode() == DesktopMode )
00739                 iDesktop = workspace()->nextDesktopFocusChain( iDesktop );
00740             else
00741                 iDesktop++;
00742             }
00743         }
00744     update();
00745     }
00746 
00747 //*******************************
00748 // Workspace
00749 //*******************************
00750 
00751 
00756 static
00757 bool areKeySymXsDepressed( bool bAll, const uint keySyms[], int nKeySyms )
00758     {
00759     char keymap[32];
00760 
00761     kdDebug(125) << "areKeySymXsDepressed: " << (bAll ? "all of " : "any of ") << nKeySyms << endl;
00762 
00763     XQueryKeymap( qt_xdisplay(), keymap );
00764 
00765     for( int iKeySym = 0; iKeySym < nKeySyms; iKeySym++ )
00766         {
00767         uint keySymX = keySyms[ iKeySym ];
00768         uchar keyCodeX = XKeysymToKeycode( qt_xdisplay(), keySymX );
00769         int i = keyCodeX / 8;
00770         char mask = 1 << (keyCodeX - (i * 8));
00771 
00772         kdDebug(125) << iKeySym << ": keySymX=0x" << QString::number( keySymX, 16 )
00773                 << " i=" << i << " mask=0x" << QString::number( mask, 16 )
00774                 << " keymap[i]=0x" << QString::number( keymap[i], 16 ) << endl;
00775 
00776                 // Abort if bad index value,
00777         if( i < 0 || i >= 32 )
00778                 return false;
00779 
00780                 // If ALL keys passed need to be depressed,
00781         if( bAll )
00782             {
00783             if( (keymap[i] & mask) == 0 )
00784                     return false;
00785             }
00786         else
00787             {
00788                         // If we are looking for ANY key press, and this key is depressed,
00789             if( keymap[i] & mask )
00790                     return true;
00791             }
00792         }
00793 
00794         // If we were looking for ANY key press, then none was found, return false,
00795         // If we were looking for ALL key presses, then all were found, return true.
00796     return bAll;
00797     }
00798 
00799 static const int MAX_KEYSYMS = 4;
00800 static uint alt_keysyms[ MAX_KEYSYMS ];
00801 static uint win_keysyms[ MAX_KEYSYMS ];
00802 
00803 static bool areModKeysDepressed( const KKeySequence& seq )
00804     {
00805     uint rgKeySyms[10];
00806     int nKeySyms = 0;
00807     if( seq.isNull())
00808     return false;
00809     int mod = seq.key(seq.count()-1).modFlags();
00810 
00811     if ( mod & KKey::SHIFT )
00812         {
00813         rgKeySyms[nKeySyms++] = XK_Shift_L;
00814         rgKeySyms[nKeySyms++] = XK_Shift_R;
00815         }
00816     if ( mod & KKey::CTRL )
00817         {
00818         rgKeySyms[nKeySyms++] = XK_Control_L;
00819         rgKeySyms[nKeySyms++] = XK_Control_R;
00820         }
00821     if( mod & KKey::ALT )
00822         {
00823         for( int i = 0;
00824              i < MAX_KEYSYMS && alt_keysyms[ i ] != NoSymbol;
00825              ++i )
00826             rgKeySyms[nKeySyms++] = alt_keysyms[ i ];
00827         }
00828     if( mod & KKey::WIN )
00829         {
00830         for( int i = 0;
00831              i < MAX_KEYSYMS && win_keysyms[ i ] != NoSymbol;
00832              ++i )
00833             rgKeySyms[nKeySyms++] = win_keysyms[ i ];
00834         }
00835 
00836     return areKeySymXsDepressed( false, rgKeySyms, nKeySyms );
00837     }
00838 
00839 static bool areModKeysDepressed( const KShortcut& cut )
00840     {
00841     for( unsigned int i = 0;
00842      i < cut.count();
00843      ++i )
00844     {
00845     if( areModKeysDepressed( cut.seq( i )))
00846         return true;
00847     }
00848     return false;
00849     }
00850 
00851 void TabBox::updateKeyMapping()
00852     {
00853     const int size = 6;
00854     uint keysyms[ size ] = { XK_Alt_L, XK_Alt_R, XK_Super_L, XK_Super_R, XK_Meta_L, XK_Meta_R };
00855     XModifierKeymap* map = XGetModifierMapping( qt_xdisplay() );
00856     int altpos = 0;
00857     int winpos = 0;
00858     int winmodpos = -1;
00859     int winmod = KKeyNative::modX( KKey::WIN );
00860     while( winmod > 0 ) // get position of the set bit in winmod
00861         {
00862         winmod >>= 1;
00863         ++winmodpos;
00864         }
00865     for( int i = 0;
00866          i < MAX_KEYSYMS;
00867          ++i )
00868         alt_keysyms[ i ] = win_keysyms[ i ] = NoSymbol;
00869     for( int i = 0;
00870          i < size;
00871          ++i )
00872         {
00873         KeyCode keycode = XKeysymToKeycode( qt_xdisplay(), keysyms[ i ] );
00874         for( int j = 0;
00875              j < map->max_keypermod;
00876              ++j )
00877             {
00878             if( map->modifiermap[ 3 * map->max_keypermod + j ] == keycode ) // Alt
00879                 if( altpos < MAX_KEYSYMS )
00880                     alt_keysyms[ altpos++ ] = keysyms[ i ];
00881             if( winmodpos >= 0 && map->modifiermap[ winmodpos * map->max_keypermod + j ] == keycode )
00882                 if( winpos < MAX_KEYSYMS )
00883                     win_keysyms[ winpos++ ] = keysyms[ i ];
00884             }
00885         }
00886     XFreeModifiermap( map );
00887     }
00888 
00889 void Workspace::slotWalkThroughWindows()
00890     {
00891     if ( root != qt_xrootwin() )
00892         return;
00893     if ( tab_grab || control_grab )
00894         return;
00895     if ( options->altTabStyle == Options::CDE || !options->focusPolicyIsReasonable())
00896         {
00897         //XUngrabKeyboard(qt_xdisplay(), qt_x_time); // need that because of accelerator raw mode
00898         // CDE style raise / lower
00899         CDEWalkThroughWindows( true );
00900         }
00901     else
00902         {
00903         if ( areModKeysDepressed( cutWalkThroughWindows ) )
00904             {
00905             if ( startKDEWalkThroughWindows() )
00906                 KDEWalkThroughWindows( true );
00907             }
00908         else
00909             // if the shortcut has no modifiers, don't show the tabbox,
00910             // don't grab, but simply go to the next window
00911             KDEOneStepThroughWindows( true );
00912         }
00913     }
00914 
00915 void Workspace::slotWalkBackThroughWindows()
00916     {
00917     if ( root != qt_xrootwin() )
00918         return;
00919     if( tab_grab || control_grab )
00920         return;
00921     if ( options->altTabStyle == Options::CDE || !options->focusPolicyIsReasonable())
00922         {
00923         // CDE style raise / lower
00924         CDEWalkThroughWindows( false );
00925         }
00926     else
00927         {
00928         if ( areModKeysDepressed( cutWalkThroughWindowsReverse ) )
00929             {
00930             if ( startKDEWalkThroughWindows() )
00931                 KDEWalkThroughWindows( false );
00932             }
00933         else
00934             {
00935             KDEOneStepThroughWindows( false );
00936             }
00937         }
00938     }
00939 
00940 void Workspace::slotWalkThroughDesktops()
00941     {
00942     if ( root != qt_xrootwin() )
00943         return;
00944     if( tab_grab || control_grab )
00945         return;
00946     if ( areModKeysDepressed( cutWalkThroughDesktops ) )
00947         {
00948         if ( startWalkThroughDesktops() )
00949             walkThroughDesktops( true );
00950         }
00951     else
00952         {
00953         oneStepThroughDesktops( true );
00954         }
00955     }
00956 
00957 void Workspace::slotWalkBackThroughDesktops()
00958     {
00959     if ( root != qt_xrootwin() )
00960         return;
00961     if( tab_grab || control_grab )
00962         return;
00963     if ( areModKeysDepressed( cutWalkThroughDesktopsReverse ) )
00964         {
00965         if ( startWalkThroughDesktops() )
00966             walkThroughDesktops( false );
00967         }
00968     else
00969         {
00970         oneStepThroughDesktops( false );
00971         }
00972     }
00973 
00974 void Workspace::slotWalkThroughDesktopList()
00975     {
00976     if ( root != qt_xrootwin() )
00977         return;
00978     if( tab_grab || control_grab )
00979         return;
00980     if ( areModKeysDepressed( cutWalkThroughDesktopList ) )
00981         {
00982         if ( startWalkThroughDesktopList() )
00983             walkThroughDesktops( true );
00984         }
00985     else
00986         {
00987         oneStepThroughDesktopList( true );
00988         }
00989     }
00990 
00991 void Workspace::slotWalkBackThroughDesktopList()
00992     {
00993     if ( root != qt_xrootwin() )
00994         return;
00995     if( tab_grab || control_grab )
00996         return;
00997     if ( areModKeysDepressed( cutWalkThroughDesktopListReverse ) )
00998         {
00999         if ( startWalkThroughDesktopList() )
01000             walkThroughDesktops( false );
01001         }
01002     else
01003         {
01004         oneStepThroughDesktopList( false );
01005         }
01006     }
01007 
01008 bool Workspace::startKDEWalkThroughWindows()
01009     {
01010     if( !establishTabBoxGrab())
01011         return false;
01012     tab_grab        = TRUE;
01013     keys->suspend( true );
01014     disable_shortcuts_keys->suspend( true );
01015     client_keys->suspend( true );
01016     tab_box->setMode( TabBox::WindowsMode );
01017     tab_box->reset();
01018     return TRUE;
01019     }
01020 
01021 bool Workspace::startWalkThroughDesktops( int mode )
01022     {
01023     if( !establishTabBoxGrab())
01024         return false;
01025     control_grab = TRUE;
01026     keys->suspend( true );
01027     disable_shortcuts_keys->suspend( true );
01028     client_keys->suspend( true );
01029     tab_box->setMode( (TabBox::Mode) mode );
01030     tab_box->reset();
01031     return TRUE;
01032     }
01033 
01034 bool Workspace::startWalkThroughDesktops()
01035     {
01036     return startWalkThroughDesktops( TabBox::DesktopMode );
01037     }
01038 
01039 bool Workspace::startWalkThroughDesktopList()
01040     {
01041     return startWalkThroughDesktops( TabBox::DesktopListMode );
01042     }
01043 
01044 void Workspace::KDEWalkThroughWindows( bool forward )
01045     {
01046     tab_box->nextPrev( forward );
01047     tab_box->delayedShow();
01048     }
01049 
01050 void Workspace::walkThroughDesktops( bool forward )
01051     {
01052     tab_box->nextPrev( forward );
01053     tab_box->delayedShow();
01054     }
01055 
01056 void Workspace::CDEWalkThroughWindows( bool forward )
01057     {
01058     Client* c = NULL;
01059 // this function find the first suitable client for unreasonable focus
01060 // policies - the topmost one, with some exceptions (can't be keepabove/below,
01061 // otherwise it gets stuck on them)
01062     Q_ASSERT( block_stacking_updates == 0 );
01063     for( ClientList::ConstIterator it = stacking_order.fromLast();
01064          it != stacking_order.end();
01065          --it )
01066         {
01067         if ( (*it)->isOnCurrentDesktop() && !(*it)->isSpecialWindow()
01068             && (*it)->isShown( false ) && (*it)->wantsTabFocus()
01069             && !(*it)->keepAbove() && !(*it)->keepBelow())
01070             {
01071             c = *it;
01072             break;
01073             }
01074         }
01075     Client* nc = c;
01076     bool options_traverse_all;
01077         {
01078         KConfigGroupSaver saver( KGlobal::config(), "TabBox" );
01079         options_traverse_all = KGlobal::config()->readBoolEntry("TraverseAll", false );
01080         }
01081 
01082     Client* firstClient = 0;
01083     do
01084         {
01085         nc = forward ? nextStaticClient(nc) : previousStaticClient(nc);
01086         if (!firstClient)
01087             {
01088             // When we see our first client for the second time,
01089             // it's time to stop.
01090             firstClient = nc;
01091             }
01092         else if (nc == firstClient)
01093             {
01094             // No candidates found.
01095             nc = 0;
01096             break;
01097             }
01098         } while (nc && nc != c &&
01099             (( !options_traverse_all && !nc->isOnDesktop(currentDesktop())) ||
01100              nc->isMinimized() || !nc->wantsTabFocus() || nc->keepAbove() || nc->keepBelow() ) );
01101     if (nc)
01102         {
01103         if (c && c != nc)
01104             lowerClient( c );
01105         if ( options->focusPolicyIsReasonable() )
01106             {
01107             activateClient( nc );
01108             if( nc->isShade() && options->shadeHover )
01109                 nc->setShade( ShadeActivated );
01110             }
01111         else
01112             {
01113             if( !nc->isOnDesktop( currentDesktop()))
01114                 setCurrentDesktop( nc->desktop());
01115             raiseClient( nc );
01116             }
01117         }
01118     }
01119 
01120 void Workspace::KDEOneStepThroughWindows( bool forward )
01121     {
01122     tab_box->setMode( TabBox::WindowsMode );
01123     tab_box->reset();
01124     tab_box->nextPrev( forward );
01125     if( Client* c = tab_box->currentClient() )
01126         {
01127         activateClient( c );
01128         if( c->isShade() && options->shadeHover )
01129             c->setShade( ShadeActivated );
01130         }
01131     }
01132 
01133 void Workspace::oneStepThroughDesktops( bool forward, int mode )
01134     {
01135     tab_box->setMode( (TabBox::Mode) mode );
01136     tab_box->reset();
01137     tab_box->nextPrev( forward );
01138     if ( tab_box->currentDesktop() != -1 )
01139         setCurrentDesktop( tab_box->currentDesktop() );
01140     }
01141 
01142 void Workspace::oneStepThroughDesktops( bool forward )
01143     {
01144     oneStepThroughDesktops( forward, TabBox::DesktopMode );
01145     }
01146 
01147 void Workspace::oneStepThroughDesktopList( bool forward )
01148     {
01149     oneStepThroughDesktops( forward, TabBox::DesktopListMode );
01150     }
01151 
01155 void Workspace::tabBoxKeyPress( const KKeyNative& keyX )
01156     {
01157     bool forward = false;
01158     bool backward = false;
01159 
01160     if (tab_grab)
01161         {
01162         forward = cutWalkThroughWindows.contains( keyX );
01163         backward = cutWalkThroughWindowsReverse.contains( keyX );
01164         if (forward || backward)
01165             {
01166             kdDebug(125) << "== " << cutWalkThroughWindows.toStringInternal()
01167                 << " or " << cutWalkThroughWindowsReverse.toStringInternal() << endl;
01168             KDEWalkThroughWindows( forward );
01169             }
01170         }
01171     else if (control_grab)
01172         {
01173         forward = cutWalkThroughDesktops.contains( keyX ) ||
01174                   cutWalkThroughDesktopList.contains( keyX );
01175         backward = cutWalkThroughDesktopsReverse.contains( keyX ) ||
01176                    cutWalkThroughDesktopListReverse.contains( keyX );
01177         if (forward || backward)
01178             walkThroughDesktops(forward);
01179         }
01180 
01181     if (control_grab || tab_grab)
01182         {
01183         uint keyQt = keyX.keyCodeQt();
01184         if ( ((keyQt & 0xffff) == Qt::Key_Escape)
01185             && !(forward || backward) )
01186             { // if Escape is part of the shortcut, don't cancel
01187             closeTabBox();
01188             }
01189         }
01190     }
01191 
01192 void Workspace::closeTabBox()
01193     {
01194     removeTabBoxGrab();
01195     tab_box->hide();
01196     keys->suspend( false );
01197     disable_shortcuts_keys->suspend( false );
01198     client_keys->suspend( false );
01199     tab_grab = FALSE;
01200     control_grab = FALSE;
01201     }
01202 
01206 void Workspace::tabBoxKeyRelease( const XKeyEvent& ev )
01207     {
01208     unsigned int mk = ev.state &
01209         (KKeyNative::modX(KKey::SHIFT) |
01210          KKeyNative::modX(KKey::CTRL) |
01211          KKeyNative::modX(KKey::ALT) |
01212          KKeyNative::modX(KKey::WIN));
01213     // ev.state is state before the key release, so just checking mk being 0 isn't enough
01214     // using XQueryPointer() also doesn't seem to work well, so the check that all
01215     // modifiers are released: only one modifier is active and the currently released
01216     // key is this modifier - if yes, release the grab
01217     int mod_index = -1;
01218     for( int i = ShiftMapIndex;
01219          i <= Mod5MapIndex;
01220          ++i )
01221         if(( mk & ( 1 << i )) != 0 )
01222         {
01223         if( mod_index >= 0 )
01224             return;
01225         mod_index = i;
01226         }
01227     bool release = false;
01228     if( mod_index == -1 )
01229         release = true;
01230     else
01231         {
01232         XModifierKeymap* xmk = XGetModifierMapping(qt_xdisplay());
01233         for (int i=0; i<xmk->max_keypermod; i++)
01234             if (xmk->modifiermap[xmk->max_keypermod * mod_index + i]
01235                 == ev.keycode)
01236                 release = true;
01237         XFreeModifiermap(xmk);
01238         }
01239     if( !release )
01240          return;
01241     if (tab_grab)
01242         {
01243         removeTabBoxGrab();
01244         tab_box->hide();
01245         keys->suspend( false );
01246         disable_shortcuts_keys->suspend( false );
01247         client_keys->suspend( false );
01248         tab_grab = false;
01249         if( Client* c = tab_box->currentClient())
01250             {
01251             activateClient( c );
01252             if( c->isShade() && options->shadeHover )
01253                 c->setShade( ShadeActivated );
01254             }
01255         }
01256     if (control_grab)
01257         {
01258         removeTabBoxGrab();
01259         tab_box->hide();
01260         keys->suspend( false );
01261         disable_shortcuts_keys->suspend( false );
01262         client_keys->suspend( false );
01263         control_grab = False;
01264         if ( tab_box->currentDesktop() != -1 )
01265             {
01266             setCurrentDesktop( tab_box->currentDesktop() );
01267             }
01268         }
01269     }
01270 
01271 
01272 int Workspace::nextDesktopFocusChain( int iDesktop ) const
01273     {
01274     int i = desktop_focus_chain.find( iDesktop );
01275     if( i >= 0 && i+1 < (int)desktop_focus_chain.size() )
01276             return desktop_focus_chain[i+1];
01277     else if( desktop_focus_chain.size() > 0 )
01278             return desktop_focus_chain[ 0 ];
01279     else
01280             return 1;
01281     }
01282 
01283 int Workspace::previousDesktopFocusChain( int iDesktop ) const
01284     {
01285     int i = desktop_focus_chain.find( iDesktop );
01286     if( i-1 >= 0 )
01287             return desktop_focus_chain[i-1];
01288     else if( desktop_focus_chain.size() > 0 )
01289             return desktop_focus_chain[desktop_focus_chain.size()-1];
01290     else
01291             return numberOfDesktops();
01292     }
01293 
01298 Client* Workspace::nextFocusChainClient( Client* c ) const
01299     {
01300     if ( global_focus_chain.isEmpty() )
01301         return 0;
01302     ClientList::ConstIterator it = global_focus_chain.find( c );
01303     if ( it == global_focus_chain.end() )
01304         return global_focus_chain.last();
01305     if ( it == global_focus_chain.begin() )
01306         return global_focus_chain.last();
01307     --it;
01308     return *it;
01309     }
01310 
01315 Client* Workspace::previousFocusChainClient( Client* c ) const
01316     {
01317     if ( global_focus_chain.isEmpty() )
01318         return 0;
01319     ClientList::ConstIterator it = global_focus_chain.find( c );
01320     if ( it == global_focus_chain.end() )
01321         return global_focus_chain.first();
01322     ++it;
01323     if ( it == global_focus_chain.end() )
01324         return global_focus_chain.first();
01325     return *it;
01326     }
01327 
01332 Client* Workspace::nextStaticClient( Client* c ) const
01333     {
01334     if ( !c || clients.isEmpty() )
01335         return 0;
01336     ClientList::ConstIterator it = clients.find( c );
01337     if ( it == clients.end() )
01338         return clients.first();
01339     ++it;
01340     if ( it == clients.end() )
01341         return clients.first();
01342     return *it;
01343     }
01348 Client* Workspace::previousStaticClient( Client* c ) const
01349     {
01350     if ( !c || clients.isEmpty() )
01351         return 0;
01352     ClientList::ConstIterator it = clients.find( c );
01353     if ( it == clients.end() )
01354         return clients.last();
01355     if ( it == clients.begin() )
01356         return clients.last();
01357     --it;
01358     return *it;
01359     }
01360 
01361 bool Workspace::establishTabBoxGrab()
01362     {
01363     if( XGrabKeyboard( qt_xdisplay(), root, FALSE,
01364         GrabModeAsync, GrabModeAsync, qt_x_time) != GrabSuccess )
01365         return false;
01366     // Don't try to establish a global mouse grab using XGrabPointer, as that would prevent
01367     // using Alt+Tab while DND (#44972). However force passive grabs on all windows
01368     // in order to catch MouseRelease events and close the tabbox (#67416).
01369     // All clients already have passive grabs in their wrapper windows, so check only
01370     // the active client, which may not have it.
01371     assert( !forced_global_mouse_grab );
01372     forced_global_mouse_grab = true;
01373     if( active_client != NULL )
01374         active_client->updateMouseGrab();
01375     return true;
01376     }
01377 
01378 void Workspace::removeTabBoxGrab()
01379     {
01380     XUngrabKeyboard(qt_xdisplay(), qt_x_time);
01381     assert( forced_global_mouse_grab );
01382     forced_global_mouse_grab = false;
01383     if( active_client != NULL )
01384         active_client->updateMouseGrab();
01385     }
01386 
01387 } // namespace
01388 
01389 #include "tabbox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys