Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qpemenubar.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #define INCLUDE_MENUITEM_DEF
00022 
00023 #include "qpemenubar.h"
00024 #include <qapplication.h>
00025 #include <qtimer.h>
00026 
00027 
00028 class QMenuBarHack : public QMenuBar
00029 {
00030 public:
00031     int activeItem() const { return actItem; }
00032 
00033     void goodbye()
00034     {
00035         activateItemAt(-1);
00036         for ( unsigned int i = 0; i < count(); i++ ) {
00037             QMenuItem *mi = findItem( idAt(i) );
00038             if ( mi->popup() ) {
00039                 mi->popup()->hide();
00040             }
00041         }
00042     }
00043 };
00044 
00045 
00046 // Sharp ROM compatibility
00047 void QPEMenuToolFocusManager::setMenukeyEnabled ( bool )
00048 {
00049 }
00050 int QPEMenuBar::getOldFocus ( )
00051 {
00052         return 0;
00053 }
00054 
00055 QPEMenuToolFocusManager *QPEMenuToolFocusManager::me = 0;
00056 
00057 QPEMenuToolFocusManager::QPEMenuToolFocusManager() : QObject()
00058 {
00059     qApp->installEventFilter( this );
00060 }
00061 
00062 void QPEMenuToolFocusManager::addWidget( QWidget *w )
00063 {
00064     list.append( GuardedWidget(w) );
00065 }
00066 
00067 void QPEMenuToolFocusManager::removeWidget( QWidget *w )
00068 {
00069     list.remove( GuardedWidget(w) );
00070 }
00071 
00072 void QPEMenuToolFocusManager::setActive( bool a )
00073 {
00074     if ( a ) {
00075         oldFocus = qApp->focusWidget();
00076         QValueList<GuardedWidget>::Iterator it;
00077         it = list.begin();
00078         while ( it != list.end() ) {
00079             QWidget *w = (*it);
00080             if ( w && w->isEnabled() && w->isVisible() &&
00081                  w->topLevelWidget() == qApp->activeWindow() ) {
00082                 setFocus( w );
00083                 return;
00084             }
00085             ++it;
00086         }
00087     } else {
00088         if ( inFocus ) {
00089             if ( inFocus->inherits( "QMenuBar" ) )
00090                 ((QMenuBarHack *)(QWidget *)inFocus)->goodbye();
00091             if ( inFocus->hasFocus() ) {
00092                 if ( oldFocus && oldFocus->isVisible() && oldFocus->isEnabled() ) {
00093                     oldFocus->setFocus();
00094                 } else {
00095                     inFocus->clearFocus();
00096                 }
00097             }
00098         }
00099         inFocus = 0;
00100         oldFocus = 0;
00101     }
00102 }
00103 
00104 bool QPEMenuToolFocusManager::isActive() const
00105 {
00106     return !inFocus.isNull();
00107 }
00108 
00109 void QPEMenuToolFocusManager::moveFocus( bool next )
00110 {
00111     if ( !isActive() )
00112         return;
00113 
00114     int n = list.count();
00115     QValueList<GuardedWidget>::Iterator it;
00116     it = list.find( inFocus );
00117     if ( it == list.end() )
00118         it = list.begin();
00119     while ( --n ) {
00120         if ( next ) {
00121             ++it;
00122             if ( it == list.end() )
00123                 it = list.begin();
00124         } else {
00125             if ( it == list.begin() )
00126                 it = list.end();
00127             --it;
00128         }
00129         QWidget *w = (*it);
00130         if ( w && w->isEnabled() && w->isVisible() && !w->inherits("QToolBarSeparator") &&
00131              w->topLevelWidget() == qApp->activeWindow() ) {
00132             setFocus( w, next );
00133             return;
00134         }
00135     }
00136 }
00137 
00138 void QPEMenuToolFocusManager::initialize()
00139 {
00140     if ( !me )
00141         me = new QPEMenuToolFocusManager;
00142 }
00143 
00144 QPEMenuToolFocusManager *QPEMenuToolFocusManager::manager()
00145 {
00146     if ( !me )
00147         me = new QPEMenuToolFocusManager;
00148 
00149     return me;
00150 }
00151 
00152 void QPEMenuToolFocusManager::setFocus( QWidget *w, bool next )
00153 {
00154     inFocus = w;
00155 //    qDebug( "Set focus on %s", w->className() );
00156     if ( inFocus->inherits( "QMenuBar" ) ) {
00157         QMenuBar *mb = (QMenuBar *)(QWidget *)inFocus;
00158         if ( next )
00159             mb->activateItemAt( 0 );
00160         else
00161             mb->activateItemAt( mb->count()-1 );
00162     }
00163     inFocus->setFocus();
00164 }
00165 
00166 bool QPEMenuToolFocusManager::eventFilter( QObject *object, QEvent *event )
00167 {
00168     if ( event->type() == QEvent::KeyPress ) {
00169         QKeyEvent *ke = (QKeyEvent *)event;
00170         if ( isActive() ) {
00171             if ( object->inherits( "QButton" ) ) {
00172                 switch ( ke->key() ) {
00173                     case Key_Left:
00174                         moveFocus( FALSE );
00175                         return TRUE;
00176 
00177                     case Key_Right:
00178                         moveFocus( TRUE );
00179                         return TRUE;
00180 
00181                     case Key_Up:
00182                     case Key_Down:
00183                         return TRUE;
00184                 }
00185             } else if ( object->inherits( "QPopupMenu" ) ) {
00186                 // Deactivate when a menu item is selected
00187                 if ( ke->key() == Key_Enter || ke->key() == Key_Return ||
00188                      ke->key() == Key_Escape ) {
00189                     QTimer::singleShot( 0, this, SLOT(deactivate()) );
00190                 }
00191             } else if ( object->inherits( "QMenuBar" ) ) {
00192                 int dx = 0;
00193                 switch ( ke->key() ) {
00194                     case Key_Left:
00195                         dx = -1;
00196                         break;
00197 
00198                     case Key_Right:
00199                         dx = 1;
00200                         break;
00201                 }
00202 
00203                 QMenuBarHack *mb = (QMenuBarHack *)object;
00204                 if ( dx && mb->activeItem() >= 0 ) {
00205                     int i = mb->activeItem();
00206                     int c = mb->count();
00207                     int n = c;
00208                     while ( n-- ) {
00209                         i = i + dx;
00210                         if ( i == c ) {
00211                             mb->goodbye();
00212                             moveFocus( TRUE );
00213                             return TRUE;
00214                         } else if ( i < 0 ) {
00215                             mb->goodbye();
00216                             moveFocus( FALSE );
00217                             return TRUE;
00218                         }
00219                         QMenuItem *mi = mb->findItem( mb->idAt(i) );
00220                         if ( mi->isEnabled() && !mi->isSeparator() ) {
00221                             break;
00222                         }
00223                     }
00224                 }
00225             }
00226         }
00227     } else if ( event->type() == QEvent::KeyRelease ) {
00228         QKeyEvent *ke = (QKeyEvent *)event;
00229         if ( isActive() ) {
00230             if ( object->inherits( "QButton" ) ) {
00231                 // Deactivate when a button is selected
00232                 if ( ke->key() == Key_Space )
00233                     QTimer::singleShot( 0, this, SLOT(deactivate()) );
00234             }
00235         }
00236     } else if ( event->type() == QEvent::FocusIn ) {
00237         if ( isActive() ) {
00238             // A non-menu/tool widget has been selected - we're deactivated
00239             QWidget *w = (QWidget *)object;
00240             if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) {
00241                 inFocus = 0;
00242             }
00243         }
00244     } else if ( event->type() == QEvent::Hide ) {
00245         if ( isActive() ) {
00246             // Deaticvate if a menu/tool has been hidden
00247             QWidget *w = (QWidget *)object;
00248             if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) {
00249                 setActive( FALSE );
00250             }
00251         }
00252     } else if ( event->type() == QEvent::ChildInserted ) {
00253         QChildEvent *ce = (QChildEvent *)event;
00254         if ( ce->child()->isWidgetType() ) {
00255             if ( ce->child()->inherits( "QMenuBar" ) ) {
00256                 addWidget( (QWidget *)ce->child() );
00257                 ce->child()->installEventFilter( this );
00258             } else if ( object->inherits( "QToolBar" ) ) {
00259                 addWidget( (QWidget *)ce->child() );
00260             }
00261         }
00262     } else if ( event->type() == QEvent::ChildRemoved ) {
00263         QChildEvent *ce = (QChildEvent *)event;
00264         if ( ce->child()->isWidgetType() ) {
00265             if ( ce->child()->inherits( "QMenuBar" ) ) {
00266                 removeWidget( (QWidget *)ce->child() );
00267                 ce->child()->removeEventFilter( this );
00268             } else if ( object->inherits( "QToolBar" ) ) {
00269                 removeWidget( (QWidget *)ce->child() );
00270             }
00271         }
00272     }
00273 
00274     return FALSE;
00275 }
00276 
00277 void QPEMenuToolFocusManager::deactivate()
00278 {
00279     setActive( FALSE );
00280 }
00281 
00296 QPEMenuBar::QPEMenuBar( QWidget *parent, const char *name )
00297     : QMenuBar( parent, name )
00298 {
00299 }
00300 
00304 QPEMenuBar::~QPEMenuBar()
00305 {
00306 }
00307 
00311 void QPEMenuBar::keyPressEvent( QKeyEvent *e )
00312 {
00313     QMenuBar::keyPressEvent( e );
00314 }
00315 
00319 void QPEMenuBar::activateItem( int index ) {
00320     activateItemAt( index );
00321 }
00322 void QPEMenuBar::goodbye() {
00323     activateItemAt(-1);
00324     for ( uint i = 0; i < count(); i++ ) {
00325         QMenuItem* mi = findItem( idAt(i) );
00326         if (mi->popup() )
00327             mi->popup()->hide();
00328     }
00329 }

Generated on Sat Nov 5 16:16:40 2005 for OPIE by  doxygen 1.4.2