summaryrefslogtreecommitdiff
path: root/library/qpemenubar.cpp
blob: 1d8eff413a2f1dcfd47f134e4b1b296f4e5d40ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#define INCLUDE_MENUITEM_DEF

#include "qpemenubar.h"
#include <qapplication.h>
#include <qtimer.h>


class QMenuBarHack : public QMenuBar
{
public:
    int activeItem() const { return actItem; }

    void goodbye()
    {
	activateItemAt(-1);
	for ( unsigned int i = 0; i < count(); i++ ) {
	    QMenuItem *mi = findItem( idAt(i) );
	    if ( mi->popup() ) {
		mi->popup()->hide();
	    }
	}
    }
};


// Sharp ROM compatibility
void QPEMenuToolFocusManager::setMenukeyEnabled ( bool )
{
}
int QPEMenuBar::getOldFocus ( )
{
	return 0;
}

QPEMenuToolFocusManager *QPEMenuToolFocusManager::me = 0;

QPEMenuToolFocusManager::QPEMenuToolFocusManager() : QObject()
{
    qApp->installEventFilter( this );
}

void QPEMenuToolFocusManager::addWidget( QWidget *w )
{
    list.append( GuardedWidget(w) );
}

void QPEMenuToolFocusManager::removeWidget( QWidget *w )
{
    list.remove( GuardedWidget(w) );
}

void QPEMenuToolFocusManager::setActive( bool a )
{
    if ( a ) {
	oldFocus = qApp->focusWidget();
	QValueList<GuardedWidget>::Iterator it;
	it = list.begin();
	while ( it != list.end() ) {
	    QWidget *w = (*it);
	    if ( w && w->isEnabled() && w->isVisible() &&
		 w->topLevelWidget() == qApp->activeWindow() ) {
		setFocus( w );
		return;
	    }
	    ++it;
	}
    } else {
	if ( inFocus ) {
	    if ( inFocus->inherits( "QMenuBar" ) )
		((QMenuBarHack *)(QWidget *)inFocus)->goodbye();
	    if ( inFocus->hasFocus() ) {
		if ( oldFocus && oldFocus->isVisible() && oldFocus->isEnabled() ) {
		    oldFocus->setFocus();
		} else {
		    inFocus->clearFocus();
		}
	    }
	}
	inFocus = 0;
	oldFocus = 0;
    }
}

bool QPEMenuToolFocusManager::isActive() const
{
    return !inFocus.isNull();
}

void QPEMenuToolFocusManager::moveFocus( bool next )
{
    if ( !isActive() )
	return;

    int n = list.count();
    QValueList<GuardedWidget>::Iterator it;
    it = list.find( inFocus );
    if ( it == list.end() )
	it = list.begin();
    while ( --n ) {
	if ( next ) {
	    ++it;
	    if ( it == list.end() )
		it = list.begin();
	} else {
	    if ( it == list.begin() )
		it = list.end();
	    --it;
	}
	QWidget *w = (*it);
	if ( w && w->isEnabled() && w->isVisible() && !w->inherits("QToolBarSeparator") &&
	     w->topLevelWidget() == qApp->activeWindow() ) {
	    setFocus( w, next );
	    return;
	}
    }
}

void QPEMenuToolFocusManager::initialize()
{
    if ( !me )
	me = new QPEMenuToolFocusManager;
}

QPEMenuToolFocusManager *QPEMenuToolFocusManager::manager()
{
    if ( !me )
	me = new QPEMenuToolFocusManager;

    return me;
}

void QPEMenuToolFocusManager::setFocus( QWidget *w, bool next )
{
    inFocus = w;
//    qDebug( "Set focus on %s", w->className() );
    if ( inFocus->inherits( "QMenuBar" ) ) {
	QMenuBar *mb = (QMenuBar *)(QWidget *)inFocus;
	if ( next )
	    mb->activateItemAt( 0 );
	else
	    mb->activateItemAt( mb->count()-1 );
    }
    inFocus->setFocus();
}

bool QPEMenuToolFocusManager::eventFilter( QObject *object, QEvent *event )
{
    if ( event->type() == QEvent::KeyPress ) {
	QKeyEvent *ke = (QKeyEvent *)event;
	if ( isActive() ) {
	    if ( object->inherits( "QButton" ) ) {
		switch ( ke->key() ) {
		    case Key_Left:
			moveFocus( FALSE );
			return TRUE;

		    case Key_Right:
			moveFocus( TRUE );
			return TRUE;

		    case Key_Up:
		    case Key_Down:
			return TRUE;
		}
	    } else if ( object->inherits( "QPopupMenu" ) ) {
		// Deactivate when a menu item is selected
		if ( ke->key() == Key_Enter || ke->key() == Key_Return ||
		     ke->key() == Key_Escape ) {
		    QTimer::singleShot( 0, this, SLOT(deactivate()) );
		}
	    } else if ( object->inherits( "QMenuBar" ) ) {
		int dx = 0;
		switch ( ke->key() ) {
		    case Key_Left:
			dx = -1;
			break;

		    case Key_Right:
			dx = 1;
			break;
		}

		QMenuBarHack *mb = (QMenuBarHack *)object;
		if ( dx && mb->activeItem() >= 0 ) {
		    int i = mb->activeItem();
		    int c = mb->count();
		    int n = c;
		    while ( n-- ) {
			i = i + dx;
			if ( i == c ) {
			    mb->goodbye();
			    moveFocus( TRUE );
			    return TRUE;
			} else if ( i < 0 ) {
			    mb->goodbye();
			    moveFocus( FALSE );
			    return TRUE;
			}
			QMenuItem *mi = mb->findItem( mb->idAt(i) );
			if ( mi->isEnabled() && !mi->isSeparator() ) {
			    break;
			}
		    }
		}
	    }
	}
    } else if ( event->type() == QEvent::KeyRelease ) {
	QKeyEvent *ke = (QKeyEvent *)event;
	if ( isActive() ) {
	    if ( object->inherits( "QButton" ) ) {
		// Deactivate when a button is selected
		if ( ke->key() == Key_Space )
		    QTimer::singleShot( 0, this, SLOT(deactivate()) );
	    }
	}
    } else if ( event->type() == QEvent::FocusIn ) {
	if ( isActive() ) {
	    // A non-menu/tool widget has been selected - we're deactivated
	    QWidget *w = (QWidget *)object;
	    if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) {
		inFocus = 0;
	    }
	}
    } else if ( event->type() == QEvent::Hide ) {
	if ( isActive() ) {
	    // Deaticvate if a menu/tool has been hidden
	    QWidget *w = (QWidget *)object;
	    if ( !w->isPopup() && !list.contains( GuardedWidget( w ) ) ) {
		setActive( FALSE );
	    }
	}
    } else if ( event->type() == QEvent::ChildInserted ) {
	QChildEvent *ce = (QChildEvent *)event;
	if ( ce->child()->isWidgetType() ) {
	    if ( ce->child()->inherits( "QMenuBar" ) ) {
		addWidget( (QWidget *)ce->child() );
		ce->child()->installEventFilter( this );
	    } else if ( object->inherits( "QToolBar" ) ) {
		addWidget( (QWidget *)ce->child() );
	    }
	}
    } else if ( event->type() == QEvent::ChildRemoved ) {
	QChildEvent *ce = (QChildEvent *)event;
	if ( ce->child()->isWidgetType() ) {
	    if ( ce->child()->inherits( "QMenuBar" ) ) {
		removeWidget( (QWidget *)ce->child() );
		ce->child()->removeEventFilter( this );
	    } else if ( object->inherits( "QToolBar" ) ) {
		removeWidget( (QWidget *)ce->child() );
	    }
	}
    }

    return FALSE;
}

void QPEMenuToolFocusManager::deactivate()
{
    setActive( FALSE );
}

/*!
  \class QPEMenuBar qpemenubar.h
  \brief The QPEMenuBar class is obsolete.  Use QMenuBar instead.

  \obsolete

  This class is obsolete.  Use QMenuBar instead.

*/

/*!
  Constructs a QPEMenuBar just as you would construct
  a QMenuBar, passing \a parent and \a name.
*/
QPEMenuBar::QPEMenuBar( QWidget *parent, const char *name )
    : QMenuBar( parent, name )
{
}

/*!
  \reimp
*/
QPEMenuBar::~QPEMenuBar()
{
}

/*!
  \internal
*/
void QPEMenuBar::keyPressEvent( QKeyEvent *e )
{
    QMenuBar::keyPressEvent( e );
}

/*!
  \internal
*/
void QPEMenuBar::activateItem( int index ) {
    activateItemAt( index );
}
void QPEMenuBar::goodbye() {
    activateItemAt(-1);
    for ( uint i = 0; i < count(); i++ ) {
        QMenuItem* mi = findItem( idAt(i) );
        if (mi->popup() )
            mi->popup()->hide();
    }
}