1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * Declarations of utility classes and functions
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef __VBoxUtils_h__
|
---|
24 | #define __VBoxUtils_h__
|
---|
25 |
|
---|
26 | /* Qt includes */
|
---|
27 | #include <QMouseEvent>
|
---|
28 | #include <Q3ListView>
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Simple ListView filter to disable unselecting all items by clicking in the
|
---|
32 | * unused area of the list (which is actually very annoying for the Single
|
---|
33 | * selection mode).
|
---|
34 | */
|
---|
35 | class QIListViewSelectionPreserver : protected QObject
|
---|
36 | {
|
---|
37 | public:
|
---|
38 |
|
---|
39 | QIListViewSelectionPreserver (QObject *parent, Q3ListView *alv)
|
---|
40 | : QObject (parent), lv (alv)
|
---|
41 | {
|
---|
42 | lv->viewport()->installEventFilter (this);
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected:
|
---|
46 |
|
---|
47 | bool eventFilter (QObject * /* o */, QEvent *e)
|
---|
48 | {
|
---|
49 | if (e->type() == QEvent::MouseButtonPress ||
|
---|
50 | e->type() == QEvent::MouseButtonRelease ||
|
---|
51 | e->type() == QEvent::MouseButtonDblClick)
|
---|
52 | {
|
---|
53 | QMouseEvent *me = static_cast<QMouseEvent *> (e);
|
---|
54 | if (!lv->itemAt (me->pos()))
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private:
|
---|
62 |
|
---|
63 | Q3ListView *lv;
|
---|
64 | };
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Simple class that filters out presses and releases of the given key
|
---|
68 | * directed to a widget (the widget acts like if it would never handle
|
---|
69 | * this key).
|
---|
70 | */
|
---|
71 | class QIKeyFilter : protected QObject
|
---|
72 | {
|
---|
73 | public:
|
---|
74 |
|
---|
75 | QIKeyFilter (QObject *aParent, Qt::Key aKey) : QObject (aParent), mKey (aKey) {}
|
---|
76 |
|
---|
77 | void watchOn (QObject *o) { o->installEventFilter (this); }
|
---|
78 |
|
---|
79 | protected:
|
---|
80 |
|
---|
81 | bool eventFilter (QObject * /*o*/, QEvent *e)
|
---|
82 | {
|
---|
83 | if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
|
---|
84 | {
|
---|
85 | QKeyEvent *ke = static_cast<QKeyEvent *> (e);
|
---|
86 | if (ke->key() == mKey ||
|
---|
87 | (mKey == Qt::Key_Enter && ke->key() == Qt::Key_Return))
|
---|
88 | {
|
---|
89 | ke->ignore();
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | Qt::Key mKey;
|
---|
98 | };
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Simple class that filters out all key presses and releases
|
---|
102 | * got while the Alt key is pressed. For some very strange reason,
|
---|
103 | * QLineEdit accepts those combinations that are not used as accelerators,
|
---|
104 | * and inserts the corresponding characters to the entry field.
|
---|
105 | */
|
---|
106 | class QIAltKeyFilter : protected QObject
|
---|
107 | {
|
---|
108 | public:
|
---|
109 |
|
---|
110 | QIAltKeyFilter (QObject *aParent) : QObject (aParent) {}
|
---|
111 |
|
---|
112 | void watchOn (QObject *o) { o->installEventFilter (this); }
|
---|
113 |
|
---|
114 | protected:
|
---|
115 |
|
---|
116 | bool eventFilter (QObject * /*o*/, QEvent *e)
|
---|
117 | {
|
---|
118 | if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
|
---|
119 | {
|
---|
120 | QKeyEvent *ke = static_cast<QKeyEvent *> (e);
|
---|
121 | if (ke->modifiers() & Qt::AltModifier)
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | };
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Watches the given widget and makes sure the minimum widget size set by the layout
|
---|
130 | * manager does never get smaller than the previous minimum size set by the
|
---|
131 | * layout manager. This way, widgets with dynamic contents (i.e. text on some
|
---|
132 | * toggle buttons) will be able only to grow, never shrink, to avoid flicker
|
---|
133 | * during alternate contents updates (Pause -> Resume -> Pause -> ...).
|
---|
134 | *
|
---|
135 | * @todo not finished
|
---|
136 | */
|
---|
137 | class QIConstraintKeeper : public QObject
|
---|
138 | {
|
---|
139 | Q_OBJECT
|
---|
140 |
|
---|
141 | public:
|
---|
142 |
|
---|
143 | QIConstraintKeeper (QWidget *aParent) : QObject (aParent)
|
---|
144 | {
|
---|
145 | aParent->setMinimumSize (aParent->size());
|
---|
146 | aParent->installEventFilter (this);
|
---|
147 | }
|
---|
148 |
|
---|
149 | private:
|
---|
150 |
|
---|
151 | bool eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
152 | {
|
---|
153 | if (aObject == parent() && aEvent->type() == QEvent::Resize)
|
---|
154 | {
|
---|
155 | QResizeEvent *ev = static_cast<QResizeEvent *> (aEvent);
|
---|
156 | QSize oldSize = ev->oldSize();
|
---|
157 | QSize newSize = ev->size();
|
---|
158 | int maxWidth = newSize.width() > oldSize.width() ?
|
---|
159 | newSize.width() : oldSize.width();
|
---|
160 | int maxHeight = newSize.height() > oldSize.height() ?
|
---|
161 | newSize.height() : oldSize.height();
|
---|
162 | if (maxWidth > oldSize.width() || maxHeight > oldSize.height())
|
---|
163 | qobject_cast<QWidget *> (parent())->setMinimumSize (maxWidth, maxHeight);
|
---|
164 | }
|
---|
165 | return QObject::eventFilter (aObject, aEvent);
|
---|
166 | }
|
---|
167 | };
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Simple class which simulates focus-proxy rule redirecting widget
|
---|
171 | * assigned shortcur to desired widget.
|
---|
172 | */
|
---|
173 | class QIFocusProxy : protected QObject
|
---|
174 | {
|
---|
175 | public:
|
---|
176 |
|
---|
177 | QIFocusProxy (QWidget *aFrom, QWidget *aTo)
|
---|
178 | : QObject (aFrom), mFrom (aFrom), mTo (aTo)
|
---|
179 | {
|
---|
180 | mFrom->installEventFilter (this);
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected:
|
---|
184 |
|
---|
185 | bool eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
186 | {
|
---|
187 | if (aObject == mFrom && aEvent->type() == QEvent::Shortcut)
|
---|
188 | {
|
---|
189 | mTo->setFocus();
|
---|
190 | return true;
|
---|
191 | }
|
---|
192 | return QObject::eventFilter (aObject, aEvent);
|
---|
193 | }
|
---|
194 |
|
---|
195 | QWidget *mFrom;
|
---|
196 | QWidget *mTo;
|
---|
197 | };
|
---|
198 |
|
---|
199 | #ifdef Q_WS_MAC
|
---|
200 | # undef PAGE_SIZE
|
---|
201 | # undef PAGE_SHIFT
|
---|
202 | # include <Carbon/Carbon.h>
|
---|
203 |
|
---|
204 | /* Asserts if a != noErr and prints the error code */
|
---|
205 | #define AssertCarbonOSStatus(a) AssertMsg ((a) == noErr, ("Carbon OSStatus: %d\n", static_cast<int> (a)))
|
---|
206 |
|
---|
207 | class QImage;
|
---|
208 | class QPixmap;
|
---|
209 | class VBoxFrameBuffer;
|
---|
210 |
|
---|
211 | /* Converting stuff */
|
---|
212 | CGImageRef darwinToCGImageRef (const QImage *aImage);
|
---|
213 | CGImageRef darwinToCGImageRef (const QPixmap *aPixmap);
|
---|
214 | CGImageRef darwinToCGImageRef (const char *aSource);
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Returns a reference to the HIView of the QWidget.
|
---|
218 | *
|
---|
219 | * @returns HIViewRef of the QWidget.
|
---|
220 | * @param aWidget Pointer to the QWidget
|
---|
221 | */
|
---|
222 | inline HIViewRef darwinToHIViewRef (QWidget *aWidget)
|
---|
223 | {
|
---|
224 | return HIViewRef(aWidget->winId());
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Returns a reference to the Window of the HIView.
|
---|
229 | *
|
---|
230 | * @returns WindowRef of the HIView.
|
---|
231 | * @param aViewRef Reference to the HIView
|
---|
232 | */
|
---|
233 | inline WindowRef darwinToWindowRef (HIViewRef aViewRef)
|
---|
234 | {
|
---|
235 | return reinterpret_cast<WindowRef> (HIViewGetWindow(aViewRef));
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Returns a reference to the Window of the QWidget.
|
---|
240 | *
|
---|
241 | * @returns WindowRef of the QWidget.
|
---|
242 | * @param aWidget Pointer to the QWidget
|
---|
243 | */
|
---|
244 | inline WindowRef darwinToWindowRef (QWidget *aWidget)
|
---|
245 | {
|
---|
246 | return ::darwinToWindowRef (::darwinToHIViewRef (aWidget));
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Returns a reference to the CGContext of the QWidget.
|
---|
251 | *
|
---|
252 | * @returns CGContextRef of the QWidget.
|
---|
253 | * @param aWidget Pointer to the QWidget
|
---|
254 | */
|
---|
255 | inline CGContextRef darwinToCGContextRef (QWidget *aWidget)
|
---|
256 | {
|
---|
257 | return static_cast<CGContext *> (aWidget->macCGHandle());
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Converts a QRect to a HIRect.
|
---|
262 | *
|
---|
263 | * @returns HIRect for the converted QRect.
|
---|
264 | * @param aRect the QRect to convert
|
---|
265 | */
|
---|
266 | inline HIRect darwinToHIRect (const QRect &aRect)
|
---|
267 | {
|
---|
268 | return CGRectMake (aRect.x(), aRect.y(), aRect.width(), aRect.height());
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* Proxy icon creation */
|
---|
272 | QPixmap darwinCreateDragPixmap (const QPixmap& aPixmap, const QString &aText);
|
---|
273 |
|
---|
274 | /* Special routines for the dock handling */
|
---|
275 | CGImageRef darwinCreateDockBadge (const char *aSource);
|
---|
276 | void darwinUpdateDockPreview (CGImageRef aVMImage, CGImageRef aOverlayImage, CGImageRef aStateImage = NULL);
|
---|
277 | void darwinUpdateDockPreview (VBoxFrameBuffer *aFrameBuffer, CGImageRef aOverlayImage);
|
---|
278 |
|
---|
279 | /* Icons in the menu of an mac application is unusual. */
|
---|
280 | void darwinDisableIconsInMenus();
|
---|
281 |
|
---|
282 | /* Experimental region handler for the seamless mode */
|
---|
283 | OSStatus darwinRegionHandler (EventHandlerCallRef aInHandlerCallRef, EventRef aInEvent, void *aInUserData);
|
---|
284 |
|
---|
285 | # ifdef DEBUG
|
---|
286 | void darwinDebugPrintEvent (const char *aPrefix, EventRef aEvent);
|
---|
287 | # endif
|
---|
288 | #endif /* Q_WS_MAC */
|
---|
289 |
|
---|
290 | #endif // __VBoxUtils_h__
|
---|
291 |
|
---|