-rw-r--r-- | library/qpemenubar.cpp | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/library/qpemenubar.cpp b/library/qpemenubar.cpp index bb3ad8d..f0171ba 100644 --- a/library/qpemenubar.cpp +++ b/library/qpemenubar.cpp | |||
@@ -1,324 +1,325 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #define INCLUDE_MENUITEM_DEF | 21 | #define INCLUDE_MENUITEM_DEF |
22 | 22 | ||
23 | #include "qpemenubar.h" | 23 | #include "qpemenubar.h" |
24 | #include <qapplication.h> | 24 | #include <qapplication.h> |
25 | #include <qguardedptr.h> | 25 | #include <qguardedptr.h> |
26 | #include <qtimer.h> | 26 | #include <qtimer.h> |
27 | 27 | ||
28 | 28 | ||
29 | class QMenuBarHack : public QMenuBar | 29 | class QMenuBarHack : public QMenuBar |
30 | { | 30 | { |
31 | public: | 31 | public: |
32 | int activeItem() const { return actItem; } | 32 | int activeItem() const { return actItem; } |
33 | 33 | ||
34 | void goodbye() | 34 | void goodbye() |
35 | { | 35 | { |
36 | activateItemAt(-1); | 36 | activateItemAt(-1); |
37 | for ( unsigned int i = 0; i < count(); i++ ) { | 37 | for ( unsigned int i = 0; i < count(); i++ ) { |
38 | QMenuItem *mi = findItem( idAt(i) ); | 38 | QMenuItem *mi = findItem( idAt(i) ); |
39 | if ( mi->popup() ) { | 39 | if ( mi->popup() ) { |
40 | mi->popup()->hide(); | 40 | mi->popup()->hide(); |
41 | } | 41 | } |
42 | } | 42 | } |
43 | } | 43 | } |
44 | }; | 44 | }; |
45 | 45 | ||
46 | 46 | ||
47 | QPEMenuToolFocusManager *QPEMenuToolFocusManager::me = 0; | 47 | QPEMenuToolFocusManager *QPEMenuToolFocusManager::me = 0; |
48 | 48 | ||
49 | QPEMenuToolFocusManager::QPEMenuToolFocusManager() : QObject() | 49 | QPEMenuToolFocusManager::QPEMenuToolFocusManager() : QObject() |
50 | { | 50 | { |
51 | qApp->installEventFilter( this ); | 51 | qApp->installEventFilter( this ); |
52 | } | 52 | } |
53 | 53 | ||
54 | void QPEMenuToolFocusManager::addWidget( QWidget *w ) | 54 | void QPEMenuToolFocusManager::addWidget( QWidget *w ) |
55 | { | 55 | { |
56 | list.append( GuardedWidget(w) ); | 56 | list.append( GuardedWidget(w) ); |
57 | } | 57 | } |
58 | 58 | ||
59 | void QPEMenuToolFocusManager::removeWidget( QWidget *w ) | 59 | void QPEMenuToolFocusManager::removeWidget( QWidget *w ) |
60 | { | 60 | { |
61 | list.remove( GuardedWidget(w) ); | 61 | list.remove( GuardedWidget(w) ); |
62 | } | 62 | } |
63 | 63 | ||
64 | void QPEMenuToolFocusManager::setActive( bool a ) | 64 | void QPEMenuToolFocusManager::setActive( bool a ) |
65 | { | 65 | { |
66 | if ( a ) { | 66 | if ( a ) { |
67 | oldFocus = qApp->focusWidget(); | 67 | oldFocus = qApp->focusWidget(); |
68 | QValueList<GuardedWidget>::Iterator it; | 68 | QValueList<GuardedWidget>::Iterator it; |
69 | it = list.begin(); | 69 | it = list.begin(); |
70 | while ( it != list.end() ) { | 70 | while ( it != list.end() ) { |
71 | QWidget *w = (*it); | 71 | QWidget *w = (*it); |
72 | if ( w && w->isEnabled() && w->isVisible() && | 72 | if ( w && w->isEnabled() && w->isVisible() && |
73 | w->topLevelWidget() == qApp->activeWindow() ) { | 73 | w->topLevelWidget() == qApp->activeWindow() ) { |
74 | setFocus( w ); | 74 | setFocus( w ); |
75 | return; | 75 | return; |
76 | } | 76 | } |
77 | ++it; | 77 | ++it; |
78 | } | 78 | } |
79 | } else { | 79 | } else { |
80 | if ( inFocus ) { | 80 | if ( inFocus ) { |
81 | if ( inFocus->inherits( "QMenuBar" ) ) | 81 | if ( inFocus->inherits( "QMenuBar" ) ) |
82 | ((QMenuBarHack *)(QWidget *)inFocus)->goodbye(); | 82 | ((QMenuBarHack *)(QWidget *)inFocus)->goodbye(); |
83 | if ( inFocus->hasFocus() ) { | 83 | if ( inFocus->hasFocus() ) { |
84 | if ( oldFocus && oldFocus->isVisible() && oldFocus->isEnabled() ) { | 84 | if ( oldFocus && oldFocus->isVisible() && oldFocus->isEnabled() ) { |
85 | oldFocus->setFocus(); | 85 | oldFocus->setFocus(); |
86 | } else { | 86 | } else { |
87 | inFocus->clearFocus(); | 87 | inFocus->clearFocus(); |
88 | } | 88 | } |
89 | } | 89 | } |
90 | } | 90 | } |
91 | inFocus = 0; | 91 | inFocus = 0; |
92 | oldFocus = 0; | 92 | oldFocus = 0; |
93 | } | 93 | } |
94 | } | 94 | } |
95 | 95 | ||
96 | bool QPEMenuToolFocusManager::isActive() const | 96 | bool QPEMenuToolFocusManager::isActive() const |
97 | { | 97 | { |
98 | return !inFocus.isNull(); | 98 | return !inFocus.isNull(); |
99 | } | 99 | } |
100 | 100 | ||
101 | void QPEMenuToolFocusManager::moveFocus( bool next ) | 101 | void QPEMenuToolFocusManager::moveFocus( bool next ) |
102 | { | 102 | { |
103 | if ( !isActive() ) | 103 | if ( !isActive() ) |
104 | return; | 104 | return; |
105 | 105 | ||
106 | int n = list.count(); | 106 | int n = list.count(); |
107 | QValueList<GuardedWidget>::Iterator it; | 107 | QValueList<GuardedWidget>::Iterator it; |
108 | it = list.find( inFocus ); | 108 | it = list.find( inFocus ); |
109 | if ( it == list.end() ) | 109 | if ( it == list.end() ) |
110 | it = list.begin(); | 110 | it = list.begin(); |
111 | while ( --n ) { | 111 | while ( --n ) { |
112 | if ( next ) { | 112 | if ( next ) { |
113 | ++it; | 113 | ++it; |
114 | if ( it == list.end() ) | 114 | if ( it == list.end() ) |
115 | it = list.begin(); | 115 | it = list.begin(); |
116 | } else { | 116 | } else { |
117 | if ( it == list.begin() ) | 117 | if ( it == list.begin() ) |
118 | it = list.end(); | 118 | it = list.end(); |
119 | --it; | 119 | --it; |
120 | } | 120 | } |
121 | QWidget *w = (*it); | 121 | QWidget *w = (*it); |
122 | if ( w && w->isEnabled() && w->isVisible() && | 122 | if ( w && w->isEnabled() && w->isVisible() && |
123 | w->topLevelWidget() == qApp->activeWindow() ) { | 123 | w->topLevelWidget() == qApp->activeWindow() ) { |
124 | setFocus( w, next ); | 124 | setFocus( w, next ); |
125 | return; | 125 | return; |
126 | } | 126 | } |
127 | } | 127 | } |
128 | } | 128 | } |
129 | 129 | ||
130 | void QPEMenuToolFocusManager::initialize() | 130 | void QPEMenuToolFocusManager::initialize() |
131 | { | 131 | { |
132 | if ( !me ) | 132 | if ( !me ) |
133 | me = new QPEMenuToolFocusManager; | 133 | me = new QPEMenuToolFocusManager; |
134 | } | 134 | } |
135 | 135 | ||
136 | QPEMenuToolFocusManager *QPEMenuToolFocusManager::manager() | 136 | QPEMenuToolFocusManager *QPEMenuToolFocusManager::manager() |
137 | { | 137 | { |
138 | if ( !me ) | 138 | if ( !me ) |
139 | me = new QPEMenuToolFocusManager; | 139 | me = new QPEMenuToolFocusManager; |
140 | 140 | ||
141 | return me; | 141 | return me; |
142 | } | 142 | } |
143 | 143 | ||
144 | void QPEMenuToolFocusManager::setFocus( QWidget *w, bool next ) | 144 | void QPEMenuToolFocusManager::setFocus( QWidget *w, bool next ) |
145 | { | 145 | { |
146 | inFocus = w; | 146 | inFocus = w; |
147 | // qDebug( "Set focus on %s", w->className() ); | 147 | // qDebug( "Set focus on %s", w->className() ); |
148 | if ( inFocus->inherits( "QMenuBar" ) ) { | 148 | if ( inFocus->inherits( "QMenuBar" ) ) { |
149 | QMenuBar *mb = (QMenuBar *)(QWidget *)inFocus; | 149 | QMenuBar *mb = (QMenuBar *)(QWidget *)inFocus; |
150 | if ( next ) | 150 | if ( next ) |
151 | mb->activateItemAt( 0 ); | 151 | mb->activateItemAt( 0 ); |
152 | else | 152 | else |
153 | mb->activateItemAt( mb->count()-1 ); | 153 | mb->activateItemAt( mb->count()-1 ); |
154 | } | 154 | } |
155 | inFocus->setFocus(); | 155 | inFocus->setFocus(); |
156 | } | 156 | } |
157 | 157 | ||
158 | bool QPEMenuToolFocusManager::eventFilter( QObject *object, QEvent *event ) | 158 | bool QPEMenuToolFocusManager::eventFilter( QObject *object, QEvent *event ) |
159 | { | 159 | { |
160 | if ( event->type() == QEvent::KeyPress ) { | 160 | if ( event->type() == QEvent::KeyPress ) { |
161 | QKeyEvent *ke = (QKeyEvent *)event; | 161 | QKeyEvent *ke = (QKeyEvent *)event; |
162 | if ( isActive() ) { | 162 | if ( isActive() ) { |
163 | if ( object->inherits( "QButton" ) ) { | 163 | if ( object->inherits( "QButton" ) ) { |
164 | switch ( ke->key() ) { | 164 | switch ( ke->key() ) { |
165 | case Key_Left: | 165 | case Key_Left: |
166 | moveFocus( FALSE ); | 166 | moveFocus( FALSE ); |
167 | return TRUE; | 167 | return TRUE; |
168 | 168 | ||
169 | case Key_Right: | 169 | case Key_Right: |
170 | moveFocus( TRUE ); | 170 | moveFocus( TRUE ); |
171 | return TRUE; | 171 | return TRUE; |
172 | 172 | ||
173 | case Key_Up: | 173 | case Key_Up: |
174 | case Key_Down: | 174 | case Key_Down: |
175 | return TRUE; | 175 | return TRUE; |
176 | } | 176 | } |
177 | } else if ( object->inherits( "QPopupMenu" ) ) { | 177 | } else if ( object->inherits( "QPopupMenu" ) ) { |
178 | // Deactivate when a menu item is selected | 178 | // Deactivate when a menu item is selected |
179 | if ( ke->key() == Key_Enter || ke->key() == Key_Return || | 179 | if ( ke->key() == Key_Enter || ke->key() == Key_Return || |
180 | ke->key() == Key_Escape ) { | 180 | ke->key() == Key_Escape ) { |
181 | QTimer::singleShot( 0, this, SLOT(deactivate()) ); | 181 | QTimer::singleShot( 0, this, SLOT(deactivate()) ); |
182 | } | 182 | } |
183 | } else if ( object->inherits( "QMenuBar" ) ) { | 183 | } else if ( object->inherits( "QMenuBar" ) ) { |
184 | int dx = 0; | 184 | int dx = 0; |
185 | switch ( ke->key() ) { | 185 | switch ( ke->key() ) { |
186 | case Key_Left: | 186 | case Key_Left: |
187 | dx = -1; | 187 | dx = -1; |
188 | break; | 188 | break; |
189 | 189 | ||
190 | case Key_Right: | 190 | case Key_Right: |
191 | dx = 1; | 191 | dx = 1; |
192 | break; | 192 | break; |
193 | } | 193 | } |
194 | 194 | ||
195 | QMenuBarHack *mb = (QMenuBarHack *)object; | 195 | QMenuBarHack *mb = (QMenuBarHack *)object; |
196 | if ( dx && mb->activeItem() >= 0 ) { | 196 | if ( dx && mb->activeItem() >= 0 ) { |
197 | int i = mb->activeItem(); | 197 | int i = mb->activeItem(); |
198 | int c = mb->count(); | 198 | int c = mb->count(); |
199 | int n = c; | 199 | int n = c; |
200 | while ( n-- ) { | 200 | while ( n-- ) { |
201 | i = i + dx; | 201 | i = i + dx; |
202 | if ( i == c ) { | 202 | if ( i == c ) { |
203 | mb->goodbye(); | 203 | mb->goodbye(); |
204 | moveFocus( TRUE ); | 204 | moveFocus( TRUE ); |
205 | return TRUE; | 205 | return TRUE; |
206 | } else if ( i < 0 ) { | 206 | } else if ( i < 0 ) { |
207 | mb->goodbye(); | 207 | mb->goodbye(); |
208 | moveFocus( FALSE ); | 208 | moveFocus( FALSE ); |
209 | return TRUE; | 209 | return TRUE; |
210 | } | 210 | } |
211 | QMenuItem *mi = mb->findItem( mb->idAt(i) ); | 211 | QMenuItem *mi = mb->findItem( mb->idAt(i) ); |
212 | if ( mi->isEnabled() && !mi->isSeparator() ) { | 212 | if ( mi->isEnabled() && !mi->isSeparator() ) { |
213 | break; | 213 | break; |
214 | } | 214 | } |
215 | } | 215 | } |
216 | } | 216 | } |
217 | } | 217 | } |
218 | } | 218 | } |
219 | if ( ke->key() == Key_F11 ) { | 219 | if ( ke->key() == Key_F11 ) { |
220 | setActive( !isActive() ); | 220 | setActive( !isActive() ); |
221 | return TRUE; | 221 | return TRUE; |
222 | } | 222 | } |
223 | } else if ( event->type() == QEvent::KeyRelease ) { | 223 | } else if ( event->type() == QEvent::KeyRelease ) { |
224 | QKeyEvent *ke = (QKeyEvent *)event; | 224 | QKeyEvent *ke = (QKeyEvent *)event; |
225 | if ( isActive() ) { | 225 | if ( isActive() ) { |
226 | if ( object->inherits( "QButton" ) ) { | 226 | if ( object->inherits( "QButton" ) ) { |
227 | // Deactivate when a button is selected | 227 | // Deactivate when a button is selected |
228 | if ( ke->key() == Key_Space ) | 228 | if ( ke->key() == Key_Space ) |
229 | QTimer::singleShot( 0, this, SLOT(deactivate()) ); | 229 | QTimer::singleShot( 0, this, SLOT(deactivate()) ); |
230 | } | 230 | } |
231 | } | 231 | } |
232 | } else if ( event->type() == QEvent::FocusIn ) { | 232 | } else if ( event->type() == QEvent::FocusIn ) { |
233 | if ( isActive() ) { | 233 | if ( isActive() ) { |
234 | // A non-menu/tool widget has been selected - we're deactivated | 234 | // A non-menu/tool widget has been selected - we're deactivated |
235 | QWidget *w = (QWidget *)object; | 235 | QWidget *w = (QWidget *)object; |
236 | if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) { | 236 | if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) { |
237 | inFocus = 0; | 237 | inFocus = 0; |
238 | } | 238 | } |
239 | } | 239 | } |
240 | } else if ( event->type() == QEvent::Hide ) { | 240 | } else if ( event->type() == QEvent::Hide ) { |
241 | if ( isActive() ) { | 241 | if ( isActive() ) { |
242 | // Deaticvate if a menu/tool has been hidden | 242 | // Deaticvate if a menu/tool has been hidden |
243 | QWidget *w = (QWidget *)object; | 243 | QWidget *w = (QWidget *)object; |
244 | if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) { | 244 | if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) { |
245 | setActive( FALSE ); | 245 | setActive( FALSE ); |
246 | } | 246 | } |
247 | } | 247 | } |
248 | } else if ( event->type() == QEvent::ChildInserted ) { | 248 | } else if ( event->type() == QEvent::ChildInserted ) { |
249 | QChildEvent *ce = (QChildEvent *)event; | 249 | QChildEvent *ce = (QChildEvent *)event; |
250 | if ( ce->child()->inherits( "QMenuBar" ) ) { | 250 | if ( ce->child()->isWidgetType() ) { |
251 | addWidget( (QWidget *)ce->child() ); | 251 | if ( ce->child()->inherits( "QMenuBar" ) ) { |
252 | ce->child()->installEventFilter( this ); | 252 | addWidget( (QWidget *)ce->child() ); |
253 | } else if ( object->inherits( "QToolBar" ) && ce->child()->isWidgetType() ) { | 253 | ce->child()->installEventFilter( this ); |
254 | addWidget( (QWidget *)ce->child() ); | 254 | } else if ( object->inherits( "QToolBar" ) ) { |
255 | addWidget( (QWidget *)ce->child() ); | ||
256 | } | ||
255 | } | 257 | } |
256 | } else if ( event->type() == QEvent::ChildRemoved ) { | 258 | } else if ( event->type() == QEvent::ChildRemoved ) { |
257 | QChildEvent *ce = (QChildEvent *)event; | 259 | QChildEvent *ce = (QChildEvent *)event; |
258 | if ( ce->child()->inherits( "QMenuBar" ) ) { | 260 | if ( ce->child()->isWidgetType() ) { |
259 | removeWidget( (QWidget *)ce->child() ); | 261 | if ( ce->child()->inherits( "QMenuBar" ) ) { |
260 | ce->child()->removeEventFilter( this ); | 262 | removeWidget( (QWidget *)ce->child() ); |
261 | } else if ( object->inherits( "QToolBar" ) && ce->child()->isWidgetType() ) { | 263 | ce->child()->removeEventFilter( this ); |
262 | removeWidget( (QWidget *)ce->child() ); | 264 | } else if ( object->inherits( "QToolBar" ) ) { |
265 | removeWidget( (QWidget *)ce->child() ); | ||
266 | } | ||
263 | } | 267 | } |
264 | } | 268 | } |
265 | 269 | ||
266 | return FALSE; | 270 | return FALSE; |
267 | } | 271 | } |
268 | 272 | ||
269 | void QPEMenuToolFocusManager::deactivate() | 273 | void QPEMenuToolFocusManager::deactivate() |
270 | { | 274 | { |
271 | setActive( FALSE ); | 275 | setActive( FALSE ); |
272 | } | 276 | } |
273 | 277 | ||
274 | /*! | 278 | /*! |
275 | \class QPEMenuBar qpemenubar.h | 279 | \class QPEMenuBar qpemenubar.h |
276 | \brief The QPEMenuBar class is obsolete. Use QMenuBar instead. | 280 | \brief The QPEMenuBar class is obsolete. Use QMenuBar instead. |
277 | 281 | ||
278 | \obsolete | 282 | \obsolete |
279 | 283 | ||
280 | This class is obsolete. Use QMenuBar instead. | 284 | This class is obsolete. Use QMenuBar instead. |
281 | 285 | ||
282 | \sa QMenuBar | ||
283 | */ | 286 | */ |
284 | 287 | ||
285 | /*! | 288 | /*! |
286 | Constructs a QPEMenuBar just as you would construct | 289 | Constructs a QPEMenuBar just as you would construct |
287 | a QMenuBar, passing \a parent and \a name. | 290 | a QMenuBar, passing \a parent and \a name. |
288 | */ | 291 | */ |
289 | QPEMenuBar::QPEMenuBar( QWidget *parent, const char *name ) | 292 | QPEMenuBar::QPEMenuBar( QWidget *parent, const char *name ) |
290 | : QMenuBar( parent, name ) | 293 | : QMenuBar( parent, name ) |
291 | { | 294 | { |
292 | } | 295 | } |
293 | 296 | ||
294 | /*! | 297 | /*! |
295 | \reimp | 298 | \reimp |
296 | */ | 299 | */ |
297 | QPEMenuBar::~QPEMenuBar() | 300 | QPEMenuBar::~QPEMenuBar() |
298 | { | 301 | { |
299 | } | 302 | } |
300 | 303 | ||
301 | /*! | 304 | /*! |
302 | \internal | 305 | \internal |
303 | */ | 306 | */ |
304 | void QPEMenuBar::keyPressEvent( QKeyEvent *e ) | 307 | void QPEMenuBar::keyPressEvent( QKeyEvent *e ) |
305 | { | 308 | { |
306 | QMenuBar::keyPressEvent( e ); | 309 | QMenuBar::keyPressEvent( e ); |
307 | } | 310 | } |
308 | 311 | ||
309 | 312 | /*! | |
310 | void QPEMenuBar::activateItem( int index ) | 313 | \internal |
311 | { | 314 | */ |
315 | void QPEMenuBar::activateItem( int index ) { | ||
312 | activateItemAt( index ); | 316 | activateItemAt( index ); |
313 | } | 317 | } |
314 | 318 | void QPEMenuBar::goodbye() { | |
315 | void QPEMenuBar::goodbye() | ||
316 | { | ||
317 | activateItemAt(-1); | 319 | activateItemAt(-1); |
318 | for ( unsigned int i = 0; i < count(); i++ ) { | 320 | for ( uint i = 0; i < count(); i++ ) { |
319 | QMenuItem *mi = findItem( idAt(i) ); | 321 | QMenuItem* mi = findItem( idAt(i) ); |
320 | if ( mi->popup() ) { | 322 | if (mi->popup() ) |
321 | mi->popup()->hide(); | 323 | mi->popup()->hide(); |
322 | } | ||
323 | } | 324 | } |
324 | } | 325 | } |