Qtopia library API Documentation

qpemenubar.cpp

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 <qguardedptr.h>
00026 #include <qtimer.h>
00027 
00028 
00029 class QMenuBarHack : public QMenuBar
00030 {
00031 public:
00032     int activeItem() const { return actItem; }
00033 
00034     void goodbye()
00035     {
00036     activateItemAt(-1);
00037     for ( unsigned int i = 0; i < count(); i++ ) {
00038         QMenuItem *mi = findItem( idAt(i) );
00039         if ( mi->popup() ) {
00040         mi->popup()->hide();
00041         }
00042     }
00043     }
00044 };
00045 
00046 
00047 // Sharp ROM compatibility
00048 void QPEMenuToolFocusManager::setMenukeyEnabled ( bool )
00049 {
00050 }
00051 int QPEMenuBar::getOldFocus ( )
00052 {
00053     return 0;
00054 }
00055 
00056 QPEMenuToolFocusManager *QPEMenuToolFocusManager::me = 0;
00057 
00058 QPEMenuToolFocusManager::QPEMenuToolFocusManager() : QObject()
00059 {
00060     qApp->installEventFilter( this );
00061 }
00062 
00063 void QPEMenuToolFocusManager::addWidget( QWidget *w )
00064 {
00065     list.append( GuardedWidget(w) );
00066 }
00067 
00068 void QPEMenuToolFocusManager::removeWidget( QWidget *w )
00069 {
00070     list.remove( GuardedWidget(w) );
00071 }
00072 
00073 void QPEMenuToolFocusManager::setActive( bool a )
00074 {
00075     if ( a ) {
00076     oldFocus = qApp->focusWidget();
00077     QValueList<GuardedWidget>::Iterator it;
00078     it = list.begin();
00079     while ( it != list.end() ) {
00080         QWidget *w = (*it);
00081         if ( w && w->isEnabled() && w->isVisible() &&
00082          w->topLevelWidget() == qApp->activeWindow() ) {
00083         setFocus( w );
00084         return;
00085         }
00086         ++it;
00087     }
00088     } else {
00089     if ( inFocus ) {
00090         if ( inFocus->inherits( "QMenuBar" ) )
00091         ((QMenuBarHack *)(QWidget *)inFocus)->goodbye();
00092         if ( inFocus->hasFocus() ) {
00093         if ( oldFocus && oldFocus->isVisible() && oldFocus->isEnabled() ) {
00094             oldFocus->setFocus();
00095         } else {
00096             inFocus->clearFocus();
00097         }
00098         }
00099     }
00100     inFocus = 0;
00101     oldFocus = 0;
00102     }
00103 }
00104 
00105 bool QPEMenuToolFocusManager::isActive() const
00106 {
00107     return !inFocus.isNull();
00108 }
00109 
00110 void QPEMenuToolFocusManager::moveFocus( bool next )
00111 {
00112     if ( !isActive() )
00113     return;
00114 
00115     int n = list.count();
00116     QValueList<GuardedWidget>::Iterator it;
00117     it = list.find( inFocus );
00118     if ( it == list.end() )
00119     it = list.begin();
00120     while ( --n ) {
00121     if ( next ) {
00122         ++it;
00123         if ( it == list.end() )
00124         it = list.begin();
00125     } else {
00126         if ( it == list.begin() )
00127         it = list.end();
00128         --it;
00129     }
00130     QWidget *w = (*it);
00131     if ( w && w->isEnabled() && w->isVisible() && !w->inherits("QToolBarSeparator") &&
00132          w->topLevelWidget() == qApp->activeWindow() ) {
00133         setFocus( w, next );
00134         return;
00135     }
00136     }
00137 }
00138 
00139 void QPEMenuToolFocusManager::initialize()
00140 {
00141     if ( !me )
00142     me = new QPEMenuToolFocusManager;
00143 }
00144 
00145 QPEMenuToolFocusManager *QPEMenuToolFocusManager::manager()
00146 {
00147     if ( !me )
00148     me = new QPEMenuToolFocusManager;
00149 
00150     return me;
00151 }
00152 
00153 void QPEMenuToolFocusManager::setFocus( QWidget *w, bool next )
00154 {
00155     inFocus = w;
00156 //    qDebug( "Set focus on %s", w->className() );
00157     if ( inFocus->inherits( "QMenuBar" ) ) {
00158     QMenuBar *mb = (QMenuBar *)(QWidget *)inFocus;
00159     if ( next )
00160         mb->activateItemAt( 0 );
00161     else
00162         mb->activateItemAt( mb->count()-1 );
00163     }
00164     inFocus->setFocus();
00165 }
00166 
00167 bool QPEMenuToolFocusManager::eventFilter( QObject *object, QEvent *event )
00168 {
00169     if ( event->type() == QEvent::KeyPress ) {
00170     QKeyEvent *ke = (QKeyEvent *)event;
00171     if ( isActive() ) {
00172         if ( object->inherits( "QButton" ) ) {
00173         switch ( ke->key() ) {
00174             case Key_Left:
00175             moveFocus( FALSE );
00176             return TRUE;
00177 
00178             case Key_Right:
00179             moveFocus( TRUE );
00180             return TRUE;
00181 
00182             case Key_Up:
00183             case Key_Down:
00184             return TRUE;
00185         }
00186         } else if ( object->inherits( "QPopupMenu" ) ) {
00187         // Deactivate when a menu item is selected
00188         if ( ke->key() == Key_Enter || ke->key() == Key_Return ||
00189              ke->key() == Key_Escape ) {
00190             QTimer::singleShot( 0, this, SLOT(deactivate()) );
00191         }
00192         } else if ( object->inherits( "QMenuBar" ) ) {
00193         int dx = 0;
00194         switch ( ke->key() ) {
00195             case Key_Left:
00196             dx = -1;
00197             break;
00198 
00199             case Key_Right:
00200             dx = 1;
00201             break;
00202         }
00203 
00204         QMenuBarHack *mb = (QMenuBarHack *)object;
00205         if ( dx && mb->activeItem() >= 0 ) {
00206             int i = mb->activeItem();
00207             int c = mb->count();
00208             int n = c;
00209             while ( n-- ) {
00210             i = i + dx;
00211             if ( i == c ) {
00212                 mb->goodbye();
00213                 moveFocus( TRUE );
00214                 return TRUE;
00215             } else if ( i < 0 ) {
00216                 mb->goodbye();
00217                 moveFocus( FALSE );
00218                 return TRUE;
00219             }
00220             QMenuItem *mi = mb->findItem( mb->idAt(i) );
00221             if ( mi->isEnabled() && !mi->isSeparator() ) {
00222                 break;
00223             }
00224             }
00225         }
00226         }
00227     }
00228     } else if ( event->type() == QEvent::KeyRelease ) {
00229     QKeyEvent *ke = (QKeyEvent *)event;
00230     if ( isActive() ) {
00231         if ( object->inherits( "QButton" ) ) {
00232         // Deactivate when a button is selected
00233         if ( ke->key() == Key_Space )
00234             QTimer::singleShot( 0, this, SLOT(deactivate()) );
00235         }
00236     }
00237     } else if ( event->type() == QEvent::FocusIn ) {
00238     if ( isActive() ) {
00239         // A non-menu/tool widget has been selected - we're deactivated
00240         QWidget *w = (QWidget *)object;
00241         if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) {
00242         inFocus = 0;
00243         }
00244     }
00245     } else if ( event->type() == QEvent::Hide ) {
00246     if ( isActive() ) {
00247         // Deaticvate if a menu/tool has been hidden
00248         QWidget *w = (QWidget *)object;
00249         if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) {
00250         setActive( FALSE );
00251         }
00252     }
00253     } else if ( event->type() == QEvent::ChildInserted ) {
00254     QChildEvent *ce = (QChildEvent *)event;
00255     if ( ce->child()->isWidgetType() ) {
00256         if ( ce->child()->inherits( "QMenuBar" ) ) {
00257         addWidget( (QWidget *)ce->child() );
00258         ce->child()->installEventFilter( this );
00259         } else if ( object->inherits( "QToolBar" ) ) {
00260         addWidget( (QWidget *)ce->child() );
00261         }
00262     }
00263     } else if ( event->type() == QEvent::ChildRemoved ) {
00264     QChildEvent *ce = (QChildEvent *)event;
00265     if ( ce->child()->isWidgetType() ) {
00266         if ( ce->child()->inherits( "QMenuBar" ) ) {
00267         removeWidget( (QWidget *)ce->child() );
00268         ce->child()->removeEventFilter( this );
00269         } else if ( object->inherits( "QToolBar" ) ) {
00270         removeWidget( (QWidget *)ce->child() );
00271         }
00272     }
00273     }
00274 
00275     return FALSE;
00276 }
00277 
00278 void QPEMenuToolFocusManager::deactivate()
00279 {
00280     setActive( FALSE );
00281 }
00282 
00297 QPEMenuBar::QPEMenuBar( QWidget *parent, const char *name )
00298     : QMenuBar( parent, name )
00299 {
00300 }
00301 
00305 QPEMenuBar::~QPEMenuBar()
00306 {
00307 }
00308 
00312 void QPEMenuBar::keyPressEvent( QKeyEvent *e )
00313 {
00314     QMenuBar::keyPressEvent( e );
00315 }
00316 
00320 void QPEMenuBar::activateItem( int index ) {
00321     activateItemAt( index );
00322 }
00323 void QPEMenuBar::goodbye() {
00324     activateItemAt(-1);
00325     for ( uint i = 0; i < count(); i++ ) {
00326         QMenuItem* mi = findItem( idAt(i) );
00327         if (mi->popup() )
00328             mi->popup()->hide();
00329     }
00330 }
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:07 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001