source: trunk/src/gui/kernel/qcocoaview_mac.mm@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 54.4 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
[561]24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#import <private/qcocoaview_mac_p.h>
43#ifdef QT_MAC_USE_COCOA
44
45#include <private/qwidget_p.h>
46#include <private/qt_mac_p.h>
47#include <private/qapplication_p.h>
48#include <private/qabstractscrollarea_p.h>
49#include <private/qt_cocoa_helpers_mac_p.h>
50#include <private/qdnd_p.h>
51#include <private/qmacinputcontext_p.h>
[561]52#include <private/qmultitouch_mac_p.h>
53#include <private/qevent_p.h>
54#include <private/qbackingstore_p.h>
[2]55
56#include <qscrollarea.h>
57#include <qhash.h>
58#include <qtextformat.h>
59#include <qpaintengine.h>
60#include <QUrl>
61#include <QAccessible>
62#include <QFileInfo>
63#include <QFile>
64
65#include <qdebug.h>
66
[561]67@interface NSEvent (Qt_Compile_Leopard_DeviceDelta)
68 - (CGFloat)deviceDeltaX;
69 - (CGFloat)deviceDeltaY;
70 - (CGFloat)deviceDeltaZ;
71@end
[2]72
[561]73@interface NSEvent (Qt_Compile_Leopard_Gestures)
74 - (CGFloat)magnification;
75@end
76
[2]77QT_BEGIN_NAMESPACE
78
79Q_GLOBAL_STATIC(DnDParams, qMacDnDParams);
80
81extern void qt_mac_update_cursor_at_global_pos(const QPoint &globalPos); // qcursor_mac.mm
82extern bool qt_sendSpontaneousEvent(QObject *, QEvent *); // qapplication.cpp
83extern OSViewRef qt_mac_nativeview_for(const QWidget *w); // qwidget_mac.mm
84extern const QStringList& qEnabledDraggedTypes(); // qmime_mac.cpp
85extern QPointer<QWidget> qt_mouseover; //qapplication_mac.mm
[561]86extern QPointer<QWidget> qt_button_down; //qapplication_mac.cpp
[2]87
88Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum)
89{
90 if (buttonNum == 0)
91 return Qt::LeftButton;
92 if (buttonNum == 1)
93 return Qt::RightButton;
94 if (buttonNum == 2)
95 return Qt::MidButton;
96 if (buttonNum == 3)
97 return Qt::XButton1;
98 if (buttonNum == 4)
99 return Qt::XButton2;
100 return Qt::NoButton;
101}
102
103struct dndenum_mapper
104{
105 NSDragOperation mac_code;
106 Qt::DropAction qt_code;
107 bool Qt2Mac;
108};
109
110static dndenum_mapper dnd_enums[] = {
111 { NSDragOperationLink, Qt::LinkAction, true },
112 { NSDragOperationMove, Qt::MoveAction, true },
113 { NSDragOperationCopy, Qt::CopyAction, true },
114 { NSDragOperationGeneric, Qt::CopyAction, false },
115 { NSDragOperationEvery, Qt::ActionMask, false },
[561]116 { NSDragOperationNone, Qt::IgnoreAction, false }
[2]117};
118
119static NSDragOperation qt_mac_mapDropAction(Qt::DropAction action)
120{
121 for (int i=0; dnd_enums[i].qt_code; i++) {
122 if (dnd_enums[i].Qt2Mac && (action & dnd_enums[i].qt_code)) {
123 return dnd_enums[i].mac_code;
124 }
125 }
126 return NSDragOperationNone;
127}
128
129static NSDragOperation qt_mac_mapDropActions(Qt::DropActions actions)
130{
131 NSDragOperation nsActions = NSDragOperationNone;
132 for (int i=0; dnd_enums[i].qt_code; i++) {
133 if (dnd_enums[i].Qt2Mac && (actions & dnd_enums[i].qt_code))
134 nsActions |= dnd_enums[i].mac_code;
135 }
136 return nsActions;
137}
138
139static Qt::DropAction qt_mac_mapNSDragOperation(NSDragOperation nsActions)
140{
141 Qt::DropAction action = Qt::IgnoreAction;
142 for (int i=0; dnd_enums[i].mac_code; i++) {
143 if (nsActions & dnd_enums[i].mac_code)
144 return dnd_enums[i].qt_code;
145 }
146 return action;
147}
148
149static Qt::DropActions qt_mac_mapNSDragOperations(NSDragOperation nsActions)
150{
151 Qt::DropActions actions = Qt::IgnoreAction;
152 for (int i=0; dnd_enums[i].mac_code; i++) {
153 if (nsActions & dnd_enums[i].mac_code)
154 actions |= dnd_enums[i].qt_code;
155 }
156 return actions;
157}
158
159static QColor colorFrom(NSColor *color)
160{
161 QColor qtColor;
162 NSString *colorSpace = [color colorSpaceName];
163 if (colorSpace == NSDeviceCMYKColorSpace) {
164 CGFloat cyan, magenta, yellow, black, alpha;
165 [color getCyan:&cyan magenta:&magenta yellow:&yellow black:&black alpha:&alpha];
166 qtColor.setCmykF(cyan, magenta, yellow, black, alpha);
167 } else {
168 NSColor *tmpColor;
169 tmpColor = [color colorUsingColorSpaceName:NSDeviceRGBColorSpace];
170 CGFloat red, green, blue, alpha;
171 [tmpColor getRed:&red green:&green blue:&blue alpha:&alpha];
172 qtColor.setRgbF(red, green, blue, alpha);
173 }
174 return qtColor;
175}
176
177QT_END_NAMESPACE
178
179QT_FORWARD_DECLARE_CLASS(QMacCocoaAutoReleasePool)
180QT_FORWARD_DECLARE_CLASS(QCFString)
181QT_FORWARD_DECLARE_CLASS(QDragManager)
182QT_FORWARD_DECLARE_CLASS(QMimeData)
183QT_FORWARD_DECLARE_CLASS(QPoint)
184QT_FORWARD_DECLARE_CLASS(QApplication)
185QT_FORWARD_DECLARE_CLASS(QApplicationPrivate)
186QT_FORWARD_DECLARE_CLASS(QDragEnterEvent)
187QT_FORWARD_DECLARE_CLASS(QDragMoveEvent)
188QT_FORWARD_DECLARE_CLASS(QStringList)
189QT_FORWARD_DECLARE_CLASS(QString)
190QT_FORWARD_DECLARE_CLASS(QRect)
191QT_FORWARD_DECLARE_CLASS(QRegion)
192QT_FORWARD_DECLARE_CLASS(QAbstractScrollArea)
193QT_FORWARD_DECLARE_CLASS(QAbstractScrollAreaPrivate)
194QT_FORWARD_DECLARE_CLASS(QPaintEvent)
195QT_FORWARD_DECLARE_CLASS(QPainter)
196QT_FORWARD_DECLARE_CLASS(QHoverEvent)
[561]197QT_FORWARD_DECLARE_CLASS(QCursor)
[2]198QT_USE_NAMESPACE
199extern "C" {
200 extern NSString *NSTextInputReplacementRangeAttributeName;
201}
202
203
204@implementation QT_MANGLE_NAMESPACE(QCocoaView)
205
206- (id)initWithQWidget:(QWidget *)widget widgetPrivate:(QWidgetPrivate *)widgetprivate
207{
208 self = [super init];
209 if (self) {
210 [self finishInitWithQWidget:widget widgetPrivate:widgetprivate];
211 }
[561]212 composingText = new QString();
[2]213 composing = false;
214 sendKeyEvents = true;
[561]215 currentCustomTypes = 0;
[2]216 [self setHidden:YES];
217 return self;
218}
219
220- (void) finishInitWithQWidget:(QWidget *)widget widgetPrivate:(QWidgetPrivate *)widgetprivate
221{
222 qwidget = widget;
223 qwidgetprivate = widgetprivate;
224 [[NSNotificationCenter defaultCenter] addObserver:self
225 selector:@selector(frameDidChange:)
226 name:@"NSViewFrameDidChangeNotification"
227 object:self];
228}
229
[561]230-(void)registerDragTypes
[2]231{
232 QMacCocoaAutoReleasePool pool;
[561]233 // Calling registerForDraggedTypes is slow, so only do it once for each widget
234 // or when the custom types change.
235 const QStringList& customTypes = qEnabledDraggedTypes();
236 if (currentCustomTypes == 0 || *currentCustomTypes != customTypes) {
237 if (currentCustomTypes == 0)
238 currentCustomTypes = new QStringList();
239 *currentCustomTypes = customTypes;
[2]240 const NSString* mimeTypeGeneric = @"com.trolltech.qt.MimeTypeName";
[561]241 NSMutableArray *supportedTypes = [NSMutableArray arrayWithObjects:NSColorPboardType,
242 NSFilenamesPboardType, NSStringPboardType,
243 NSFilenamesPboardType, NSPostScriptPboardType, NSTIFFPboardType,
244 NSRTFPboardType, NSTabularTextPboardType, NSFontPboardType,
245 NSRulerPboardType, NSFileContentsPboardType, NSColorPboardType,
246 NSRTFDPboardType, NSHTMLPboardType, NSPICTPboardType,
[2]247 NSURLPboardType, NSPDFPboardType, NSVCardPboardType,
[561]248 NSFilesPromisePboardType, NSInkTextPboardType,
[2]249 NSMultipleTextSelectionPboardType, mimeTypeGeneric, nil];
250 // Add custom types supported by the application.
251 for (int i = 0; i < customTypes.size(); i++) {
252 [supportedTypes addObject:reinterpret_cast<const NSString *>(QCFString::toCFStringRef(customTypes[i]))];
253 }
254 [self registerForDraggedTypes:supportedTypes];
[561]255 }
256}
257
258- (void)resetCursorRects
259{
260 QWidget *cursorWidget = qwidget;
261
262 if (cursorWidget->testAttribute(Qt::WA_TransparentForMouseEvents))
263 cursorWidget = QApplication::widgetAt(qwidget->mapToGlobal(qwidget->rect().center()));
264
265 if (cursorWidget == 0)
266 return;
267
268 if (!cursorWidget->testAttribute(Qt::WA_SetCursor)) {
269 [super resetCursorRects];
270 return;
271 }
272
273 QRegion mask = qt_widget_private(cursorWidget)->extra->mask;
274 NSCursor *nscursor = static_cast<NSCursor *>(qt_mac_nsCursorForQCursor(cursorWidget->cursor()));
275 if (mask.isEmpty()) {
276 [self addCursorRect:[qt_mac_nativeview_for(cursorWidget) visibleRect] cursor:nscursor];
[2]277 } else {
[561]278 const QVector<QRect> &rects = mask.rects();
279 for (int i = 0; i < rects.size(); ++i) {
280 const QRect &rect = rects.at(i);
281 [self addCursorRect:NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height()) cursor:nscursor];
282 }
[2]283 }
284}
285
286- (void)removeDropData
287{
288 if (dropData) {
289 delete dropData;
290 dropData = 0;
291 }
292}
[561]293
294- (void)addDropData:(id <NSDraggingInfo>)sender
[2]295{
296 [self removeDropData];
[561]297 CFStringRef dropPasteboard = (CFStringRef) [[sender draggingPasteboard] name];
[2]298 dropData = new QCocoaDropData(dropPasteboard);
[561]299}
[2]300
[561]301- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
302{
303 if (qwidget->testAttribute(Qt::WA_DropSiteRegistered) == false)
304 return NSDragOperationNone;
305 NSPoint windowPoint = [sender draggingLocation];
306 if (qwidget->testAttribute(Qt::WA_TransparentForMouseEvents)) {
307 // pass the drag enter event to the view underneath.
308 NSView *candidateView = [[[self window] contentView] hitTest:windowPoint];
309 if (candidateView && candidateView != self)
310 return [candidateView draggingEntered:sender];
311 }
312 dragEnterSequence = [sender draggingSequenceNumber];
[2]313 [self addDropData:sender];
314 QMimeData *mimeData = dropData;
315 if (QDragManager::self()->source())
316 mimeData = QDragManager::self()->dragPrivate()->data;
317 NSPoint globalPoint = [[sender draggingDestinationWindow] convertBaseToScreen:windowPoint];
318 NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
319 QPoint posDrag(localPoint.x, localPoint.y);
[561]320 NSDragOperation nsActions = [sender draggingSourceOperationMask];
[2]321 Qt::DropActions qtAllowed = qt_mac_mapNSDragOperations(nsActions);
[561]322 QT_PREPEND_NAMESPACE(qt_mac_dnd_answer_rec.lastOperation) = nsActions;
323 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
324 if ([sender draggingSource] != nil) {
325 // modifier flags might have changed, update it here since we don't send any input events.
326 QApplicationPrivate::modifier_buttons = qt_cocoaModifiers2QtModifiers([[NSApp currentEvent] modifierFlags]);
327 modifiers = QApplication::keyboardModifiers();
328 } else {
329 // when the source is from another application the above technique will not work.
330 modifiers = qt_cocoaDragOperation2QtModifiers(nsActions);
331 }
[2]332 // send the drag enter event to the widget.
[561]333 QDragEnterEvent qDEEvent(posDrag, qtAllowed, mimeData, QApplication::mouseButtons(), modifiers);
[2]334 QApplication::sendEvent(qwidget, &qDEEvent);
335 if (!qDEEvent.isAccepted()) {
336 // widget is not interested in this drag, so ignore this drop data.
337 [self removeDropData];
338 return NSDragOperationNone;
339 } else {
[561]340 // save the mouse position, used by draggingExited handler.
341 DnDParams *dndParams = [QT_MANGLE_NAMESPACE(QCocoaView) currentMouseEvent];
342 dndParams->activeDragEnterPos = windowPoint;
[2]343 // send a drag move event immediately after a drag enter event (as per documentation).
[561]344 QDragMoveEvent qDMEvent(posDrag, qtAllowed, mimeData, QApplication::mouseButtons(), modifiers);
[2]345 qDMEvent.setDropAction(qDEEvent.dropAction());
346 qDMEvent.accept(); // accept by default, since enter event was accepted.
347 QApplication::sendEvent(qwidget, &qDMEvent);
348 if (!qDMEvent.isAccepted() || qDMEvent.dropAction() == Qt::IgnoreAction) {
[561]349 // since we accepted the drag enter event, the widget expects
350 // future drage move events.
351 // ### check if we need to treat this like the drag enter event.
352 nsActions = NSDragOperationNone;
353 // Save as ignored in the answer rect.
354 qDMEvent.setDropAction(Qt::IgnoreAction);
[2]355 } else {
356 nsActions = QT_PREPEND_NAMESPACE(qt_mac_mapDropAction)(qDMEvent.dropAction());
[561]357 }
[2]358 QT_PREPEND_NAMESPACE(qt_mac_copy_answer_rect)(qDMEvent);
[561]359 return nsActions;
360 }
[2]361 }
362- (NSDragOperation)draggingUpdated:(id < NSDraggingInfo >)sender
363{
[561]364 NSPoint windowPoint = [sender draggingLocation];
365 if (qwidget->testAttribute(Qt::WA_TransparentForMouseEvents)) {
366 // pass the drag move event to the view underneath.
367 NSView *candidateView = [[[self window] contentView] hitTest:windowPoint];
368 if (candidateView && candidateView != self)
369 return [candidateView draggingUpdated:sender];
370 }
371 // in cases like QFocusFrame, the view under the mouse might
372 // not have received the drag enter. Generate a synthetic
373 // drag enter event for that view.
374 if (dragEnterSequence != [sender draggingSequenceNumber])
375 [self draggingEntered:sender];
376 // drag enter event was rejected, so ignore the move event.
[2]377 if (dropData == 0)
378 return NSDragOperationNone;
379 // return last value, if we are still in the answerRect.
380 NSPoint globalPoint = [[sender draggingDestinationWindow] convertBaseToScreen:windowPoint];
381 NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
382 NSDragOperation nsActions = [sender draggingSourceOperationMask];
383 QPoint posDrag(localPoint.x, localPoint.y);
384 if (qt_mac_mouse_inside_answer_rect(posDrag)
385 && QT_PREPEND_NAMESPACE(qt_mac_dnd_answer_rec.lastOperation) == nsActions)
386 return QT_PREPEND_NAMESPACE(qt_mac_mapDropActions)(QT_PREPEND_NAMESPACE(qt_mac_dnd_answer_rec.lastAction));
[561]387 // send drag move event to the widget
[2]388 QT_PREPEND_NAMESPACE(qt_mac_dnd_answer_rec.lastOperation) = nsActions;
389 Qt::DropActions qtAllowed = QT_PREPEND_NAMESPACE(qt_mac_mapNSDragOperations)(nsActions);
[561]390 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
391 if ([sender draggingSource] != nil) {
392 // modifier flags might have changed, update it here since we don't send any input events.
393 QApplicationPrivate::modifier_buttons = qt_cocoaModifiers2QtModifiers([[NSApp currentEvent] modifierFlags]);
394 modifiers = QApplication::keyboardModifiers();
395 } else {
396 // when the source is from another application the above technique will not work.
397 modifiers = qt_cocoaDragOperation2QtModifiers(nsActions);
398 }
[2]399 QMimeData *mimeData = dropData;
400 if (QDragManager::self()->source())
401 mimeData = QDragManager::self()->dragPrivate()->data;
[561]402 QDragMoveEvent qDMEvent(posDrag, qtAllowed, mimeData, QApplication::mouseButtons(), modifiers);
[2]403 qDMEvent.setDropAction(QT_PREPEND_NAMESPACE(qt_mac_dnd_answer_rec).lastAction);
404 qDMEvent.accept();
405 QApplication::sendEvent(qwidget, &qDMEvent);
406
407 NSDragOperation operation = qt_mac_mapDropAction(qDMEvent.dropAction());
408 if (!qDMEvent.isAccepted() || qDMEvent.dropAction() == Qt::IgnoreAction) {
409 // ignore this event (we will still receive further notifications)
410 operation = NSDragOperationNone;
[561]411 // Save as ignored in the answer rect.
412 qDMEvent.setDropAction(Qt::IgnoreAction);
[2]413 }
[561]414 qt_mac_copy_answer_rect(qDMEvent);
[2]415 return operation;
416}
417
418- (void)draggingExited:(id < NSDraggingInfo >)sender
419{
[561]420 dragEnterSequence = -1;
421 if (qwidget->testAttribute(Qt::WA_TransparentForMouseEvents)) {
422 // try sending the leave event to the last view which accepted drag enter.
423 DnDParams *dndParams = [QT_MANGLE_NAMESPACE(QCocoaView) currentMouseEvent];
424 NSView *candidateView = [[[self window] contentView] hitTest:dndParams->activeDragEnterPos];
425 if (candidateView && candidateView != self)
426 return [candidateView draggingExited:sender];
427 }
428 // drag enter event was rejected, so ignore the move event.
[2]429 if (dropData) {
430 QDragLeaveEvent de;
431 QApplication::sendEvent(qwidget, &de);
432 [self removeDropData];
433 }
434}
435
436- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
437{
[561]438 NSPoint windowPoint = [sender draggingLocation];
439 dragEnterSequence = -1;
440 if (qwidget->testAttribute(Qt::WA_TransparentForMouseEvents)) {
441 // pass the drop event to the view underneath.
442 NSView *candidateView = [[[self window] contentView] hitTest:windowPoint];
443 if (candidateView && candidateView != self)
444 return [candidateView performDragOperation:sender];
445 }
[2]446 [self addDropData:sender];
[561]447
[2]448 NSPoint globalPoint = [[sender draggingDestinationWindow] convertBaseToScreen:windowPoint];
449 NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
450 QPoint posDrop(localPoint.x, localPoint.y);
[561]451
452 NSDragOperation nsActions = [sender draggingSourceOperationMask];
[2]453 Qt::DropActions qtAllowed = qt_mac_mapNSDragOperations(nsActions);
454 QMimeData *mimeData = dropData;
455 if (QDragManager::self()->source())
456 mimeData = QDragManager::self()->dragPrivate()->data;
457 // send the drop event to the widget.
458 QDropEvent de(posDrop, qtAllowed, mimeData,
459 QApplication::mouseButtons(), QApplication::keyboardModifiers());
460 if (QDragManager::self()->object)
461 QDragManager::self()->dragPrivate()->target = qwidget;
462 QApplication::sendEvent(qwidget, &de);
[561]463 if (QDragManager::self()->object)
464 QDragManager::self()->dragPrivate()->executed_action = de.dropAction();
[2]465 if (!de.isAccepted())
466 return NO;
467 else
468 return YES;
469}
470
471- (void)dealloc
472{
[561]473 delete composingText;
[2]474 [[NSNotificationCenter defaultCenter] removeObserver:self];
[561]475 delete currentCustomTypes;
476 [self unregisterDraggedTypes];
[2]477 [super dealloc];
478}
479
480- (BOOL)isOpaque;
481{
482 return qwidgetprivate->isOpaque;
483}
484
485- (BOOL)isFlipped;
486{
487 return YES;
488}
489
490- (BOOL) preservesContentDuringLiveResize;
491{
492 return qwidget->testAttribute(Qt::WA_StaticContents);
493}
494
495- (void) setFrameSize:(NSSize)newSize
496{
497 [super setFrameSize:newSize];
[561]498
[2]499 // A change in size has required the view to be invalidated.
[561]500 if ([self inLiveResize]) {
[2]501 NSRect rects[4];
502 NSInteger count;
503 [self getRectsExposedDuringLiveResize:rects count:&count];
504 while (count-- > 0)
505 {
506 [self setNeedsDisplayInRect:rects[count]];
507 }
[561]508 } else {
[2]509 [self setNeedsDisplay:YES];
510 }
[651]511
512 // Make sure the opengl context is updated on resize.
513 if (qwidgetprivate->isGLWidget) {
514 qwidgetprivate->needWindowChange = true;
515 QEvent event(QEvent::MacGLWindowChange);
516 qApp->sendEvent(qwidget, &event);
517 }
[2]518}
519
520- (void)drawRect:(NSRect)aRect
521{
[561]522 if (QApplicationPrivate::graphicsSystem() != 0) {
523 if (QWidgetBackingStore *bs = qwidgetprivate->maybeBackingStore())
524 bs->markDirty(qwidget->rect(), qwidget);
525 qwidgetprivate->syncBackingStore(qwidget->rect());
526 return;
527 }
[2]528 CGContextRef cg = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
529 qwidgetprivate->hd = cg;
530 CGContextSaveGState(cg);
531
532 if (qwidget->isVisible() && qwidget->updatesEnabled()) { //process the actual paint event.
533 if (qwidget->testAttribute(Qt::WA_WState_InPaintEvent))
534 qWarning("QWidget::repaint: Recursive repaint detected");
535
536 const QRect qrect = QRect(aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
537 QRegion qrgn(qrect);
538
539 if (!qwidget->isWindow() && !qobject_cast<QAbstractScrollArea *>(qwidget->parent())) {
540 const QRegion &parentMask = qwidget->window()->mask();
541 if (!parentMask.isEmpty()) {
542 const QPoint mappedPoint = qwidget->mapTo(qwidget->window(), qrect.topLeft());
543 qrgn.translate(mappedPoint);
544 qrgn &= parentMask;
545 qrgn.translate(-mappedPoint.x(), -mappedPoint.y());
546 }
547 }
548
549 QPoint redirectionOffset(0, 0);
550 //setup the context
551 qwidget->setAttribute(Qt::WA_WState_InPaintEvent);
552 QPaintEngine *engine = qwidget->paintEngine();
553 if (engine)
554 engine->setSystemClip(qrgn);
555 if (qwidgetprivate->extra && qwidgetprivate->extra->hasMask) {
556 CGRect widgetRect = CGRectMake(0, 0, qwidget->width(), qwidget->height());
557 CGContextTranslateCTM (cg, 0, widgetRect.size.height);
558 CGContextScaleCTM(cg, 1, -1);
559 if (qwidget->isWindow())
560 CGContextClearRect(cg, widgetRect);
561 CGContextClipToMask(cg, widgetRect, qwidgetprivate->extra->imageMask);
562 CGContextScaleCTM(cg, 1, -1);
563 CGContextTranslateCTM (cg, 0, -widgetRect.size.height);
564 }
565
566 if (qwidget->isWindow() && !qwidgetprivate->isOpaque
567 && !qwidget->testAttribute(Qt::WA_MacBrushedMetal)) {
568 CGContextClearRect(cg, NSRectToCGRect(aRect));
569 }
570
571 if (engine && !qwidget->testAttribute(Qt::WA_NoSystemBackground)
572 && (qwidget->isWindow() || qwidget->autoFillBackground())
573 || qwidget->testAttribute(Qt::WA_TintedBackground)
574 || qwidget->testAttribute(Qt::WA_StyledBackground)) {
575#ifdef DEBUG_WIDGET_PAINT
576 if(doDebug)
577 qDebug(" Handling erase for [%s::%s]", qwidget->metaObject()->className(),
578 qwidget->objectName().local8Bit().data());
579#endif
580 QPainter p(qwidget);
[561]581 qwidgetprivate->paintBackground(&p, qrgn,
[2]582 qwidget->isWindow() ? QWidgetPrivate::DrawAsRoot : 0);
583 p.end();
584 }
585 QPaintEvent e(qrgn);
586#ifdef QT3_SUPPORT
587 e.setErased(true);
588#endif
589 qt_sendSpontaneousEvent(qwidget, &e);
590 if (!redirectionOffset.isNull())
591 QPainter::restoreRedirected(qwidget);
592 if (engine)
593 engine->setSystemClip(QRegion());
594 qwidget->setAttribute(Qt::WA_WState_InPaintEvent, false);
595 if(!qwidget->testAttribute(Qt::WA_PaintOutsidePaintEvent) && qwidget->paintingActive())
596 qWarning("QWidget: It is dangerous to leave painters active on a"
597 " widget outside of the PaintEvent");
598 }
599 qwidgetprivate->hd = 0;
600 CGContextRestoreGState(cg);
601}
602
603- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
604{
605 Q_UNUSED(theEvent);
606 return !qwidget->testAttribute(Qt::WA_MacNoClickThrough);
607}
608
[561]609- (NSView *)hitTest:(NSPoint)aPoint
610{
611 if (qwidget->testAttribute(Qt::WA_TransparentForMouseEvents))
612 return nil; // You cannot hit a transparent for mouse event widget.
613 return [super hitTest:aPoint];
614}
615
[2]616- (void)updateTrackingAreas
617{
618 QMacCocoaAutoReleasePool pool;
619 if (NSArray *trackingArray = [self trackingAreas]) {
620 NSUInteger size = [trackingArray count];
621 for (NSUInteger i = 0; i < size; ++i) {
622 NSTrackingArea *t = [trackingArray objectAtIndex:i];
623 [self removeTrackingArea:t];
624 }
625 }
[561]626
627 // Ideally, we shouldn't have NSTrackingMouseMoved events included below, it should
628 // only be turned on if mouseTracking, hover is on or a tool tip is set.
629 // Unfortunately, Qt will send "tooltip" events on mouse moves, so we need to
630 // turn it on in ALL case. That means EVERY QCocoaView gets to pay the cost of
631 // mouse moves delivered to it (Apple recommends keeping it OFF because there
632 // is a performance hit). So it goes.
[2]633 NSUInteger trackingOptions = NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp
[561]634 | NSTrackingInVisibleRect | NSTrackingMouseMoved;
[2]635 NSTrackingArea *ta = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0, 0,
636 qwidget->width(),
637 qwidget->height())
638 options:trackingOptions
639 owner:self
640 userInfo:nil];
641 [self addTrackingArea:ta];
642 [ta release];
643}
644
645- (void)mouseEntered:(NSEvent *)event
646{
647 QEvent enterEvent(QEvent::Enter);
648 NSPoint windowPoint = [event locationInWindow];
649 NSPoint globalPoint = [[event window] convertBaseToScreen:windowPoint];
650 NSPoint viewPoint = [self convertPoint:windowPoint fromView:nil];
651 if (!qAppInstance()->activeModalWidget() || QApplicationPrivate::tryModalHelper(qwidget, 0)) {
652 QApplication::sendEvent(qwidget, &enterEvent);
653 qt_mouseover = qwidget;
[561]654
[2]655 // Update cursor and dispatch hover events.
656 qt_mac_update_cursor_at_global_pos(flipPoint(globalPoint).toPoint());
657 if (qwidget->testAttribute(Qt::WA_Hover) &&
658 (!qAppInstance()->activePopupWidget() || qAppInstance()->activePopupWidget() == qwidget->window())) {
659 QHoverEvent he(QEvent::HoverEnter, QPoint(viewPoint.x, viewPoint.y), QPoint(-1, -1));
660 QApplicationPrivate::instance()->notify_helper(qwidget, &he);
661 }
[561]662 }
[2]663}
664
665- (void)mouseExited:(NSEvent *)event
666{
667 QEvent leaveEvent(QEvent::Leave);
668 NSPoint globalPoint = [[event window] convertBaseToScreen:[event locationInWindow]];
669 if (!qAppInstance()->activeModalWidget() || QApplicationPrivate::tryModalHelper(qwidget, 0)) {
670 QApplication::sendEvent(qwidget, &leaveEvent);
[561]671
[2]672 // ### Think about if it is necessary to update the cursor, should only be for a few cases.
673 qt_mac_update_cursor_at_global_pos(flipPoint(globalPoint).toPoint());
674 if (qwidget->testAttribute(Qt::WA_Hover)
675 && (!qAppInstance()->activePopupWidget() || qAppInstance()->activePopupWidget() == qwidget->window())) {
676 QHoverEvent he(QEvent::HoverLeave, QPoint(-1, -1),
677 qwidget->mapFromGlobal(QApplicationPrivate::instance()->hoverGlobalPos));
678 QApplicationPrivate::instance()->notify_helper(qwidget, &he);
679 }
680 }
681}
682
683- (void)flagsChanged:(NSEvent *)theEvent
684{
685 QWidget *widgetToGetKey = qwidget;
686
687 QWidget *popup = qAppInstance()->activePopupWidget();
688 if (popup && popup != qwidget->window())
689 widgetToGetKey = popup->focusWidget() ? popup->focusWidget() : popup;
690 qt_dispatchModifiersChanged(theEvent, widgetToGetKey);
691 [super flagsChanged:theEvent];
692}
693
694- (void)mouseMoved:(NSEvent *)theEvent
695{
[561]696 // We always enable mouse tracking for all QCocoaView-s. In cases where we have
697 // child views, we will receive mouseMoved for both parent & the child (if
698 // mouse is over the child). We need to ignore the parent mouseMoved in such
699 // cases.
700 NSPoint windowPoint = [theEvent locationInWindow];
701 NSView *candidateView = [[[self window] contentView] hitTest:windowPoint];
702 if (candidateView && candidateView == self) {
703 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseMove, Qt::NoButton);
[2]704 }
705}
706
707- (void)mouseDown:(NSEvent *)theEvent
708{
[561]709 if (!qt_button_down)
710 qt_button_down = qwidget;
711
[2]712 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseButtonPress, Qt::LeftButton);
713 // Don't call super here. This prevents us from getting the mouseUp event,
[561]714 // which we need to send even if the mouseDown event was not accepted.
[2]715 // (this is standard Qt behavior.)
716}
717
718
719- (void)mouseUp:(NSEvent *)theEvent
720{
[561]721 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseButtonRelease, Qt::LeftButton);
[2]722
[561]723 qt_button_down = 0;
[2]724}
725
726- (void)rightMouseDown:(NSEvent *)theEvent
[561]727{
728 if (!qt_button_down)
729 qt_button_down = qwidget;
[2]730
[561]731 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseButtonPress, Qt::RightButton);
[2]732}
733
734- (void)rightMouseUp:(NSEvent *)theEvent
735{
[561]736 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseButtonRelease, Qt::RightButton);
[2]737
[561]738 qt_button_down = 0;
[2]739}
740
741- (void)otherMouseDown:(NSEvent *)theEvent
742{
[561]743 if (!qt_button_down)
744 qt_button_down = qwidget;
745
[2]746 Qt::MouseButton mouseButton = cocoaButton2QtButton([theEvent buttonNumber]);
[561]747 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseButtonPress, mouseButton);
[2]748}
749
750- (void)otherMouseUp:(NSEvent *)theEvent
751{
752 Qt::MouseButton mouseButton = cocoaButton2QtButton([theEvent buttonNumber]);
[561]753 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseButtonRelease, mouseButton);
[2]754
[561]755 qt_button_down = 0;
[2]756}
757
758- (void)mouseDragged:(NSEvent *)theEvent
759{
760 qMacDnDParams()->view = self;
761 qMacDnDParams()->theEvent = theEvent;
[561]762 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseMove, Qt::NoButton);
[2]763}
764
765- (void)rightMouseDragged:(NSEvent *)theEvent
766{
767 qMacDnDParams()->view = self;
768 qMacDnDParams()->theEvent = theEvent;
[561]769 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseMove, Qt::NoButton);
[2]770}
771
772- (void)otherMouseDragged:(NSEvent *)theEvent
773{
774 qMacDnDParams()->view = self;
775 qMacDnDParams()->theEvent = theEvent;
[561]776 qt_mac_handleMouseEvent(self, theEvent, QEvent::MouseMove, Qt::NoButton);
[2]777}
778
779- (void)scrollWheel:(NSEvent *)theEvent
780{
781 // Give the Input Manager a chance to process the wheel event.
782 NSInputManager *currentIManager = [NSInputManager currentInputManager];
783 if (currentIManager && [currentIManager wantsToHandleMouseEvents]) {
784 [currentIManager handleMouseEvent:theEvent];
785 }
[561]786
[2]787 NSPoint windowPoint = [theEvent locationInWindow];
788 NSPoint globalPoint = [[theEvent window] convertBaseToScreen:windowPoint];
789 NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
790 QPoint qlocal = QPoint(localPoint.x, localPoint.y);
[561]791 QPoint qglobal = QPoint(globalPoint.x, flipYCoordinate(globalPoint.y));
792 Qt::MouseButtons buttons = QApplication::mouseButtons();
[2]793 bool wheelOK = false;
794 Qt::KeyboardModifiers keyMods = qt_cocoaModifiers2QtModifiers([theEvent modifierFlags]);
[561]795 QWidget *widgetToGetMouse = qwidget;
796 // if popup is open it should get wheel events if the cursor is over the popup,
797 // otherwise the event should be ignored.
798 if (QWidget *popup = qAppInstance()->activePopupWidget()) {
799 if (!popup->geometry().contains(qglobal))
800 return;
801 }
[2]802
[561]803 int deltaX = 0;
804 int deltaY = 0;
805 int deltaZ = 0;
806
807 const EventRef carbonEvent = (EventRef)[theEvent eventRef];
808 const UInt32 carbonEventKind = carbonEvent ? ::GetEventKind(carbonEvent) : 0;
809 const bool scrollEvent = carbonEventKind == kEventMouseScroll;
810
811 if (scrollEvent) {
812 // The mouse device containts pixel scroll wheel support (Mighty Mouse, Trackpad).
813 // Since deviceDelta is delivered as pixels rather than degrees, we need to
814 // convert from pixels to degrees in a sensible manner.
815 // It looks like four degrees per pixel behaves most native.
816 // Qt expects the unit for delta to be 1/8 of a degree:
817 deltaX = [theEvent deviceDeltaX];
818 deltaY = [theEvent deviceDeltaY];
819 deltaZ = [theEvent deviceDeltaZ];
820 } else {
821 // carbonEventKind == kEventMouseWheelMoved
822 // Remove acceleration, and use either -120 or 120 as delta:
823 deltaX = qBound(-120, int([theEvent deltaX] * 10000), 120);
824 deltaY = qBound(-120, int([theEvent deltaY] * 10000), 120);
825 deltaZ = qBound(-120, int([theEvent deltaZ] * 10000), 120);
826 }
827
[2]828 if (deltaX != 0) {
829 QWheelEvent qwe(qlocal, qglobal, deltaX, buttons, keyMods, Qt::Horizontal);
[561]830 qt_sendSpontaneousEvent(widgetToGetMouse, &qwe);
[2]831 wheelOK = qwe.isAccepted();
832 if (!wheelOK && QApplicationPrivate::focus_widget
[561]833 && QApplicationPrivate::focus_widget != widgetToGetMouse) {
[2]834 QWheelEvent qwe2(QApplicationPrivate::focus_widget->mapFromGlobal(qglobal), qglobal,
835 deltaX, buttons, keyMods, Qt::Horizontal);
836 qt_sendSpontaneousEvent(QApplicationPrivate::focus_widget, &qwe2);
837 wheelOK = qwe2.isAccepted();
838 }
839 }
[561]840
[2]841 if (deltaY) {
842 QWheelEvent qwe(qlocal, qglobal, deltaY, buttons, keyMods, Qt::Vertical);
[561]843 qt_sendSpontaneousEvent(widgetToGetMouse, &qwe);
[2]844 wheelOK = qwe.isAccepted();
[561]845 if (!wheelOK && QApplicationPrivate::focus_widget
846 && QApplicationPrivate::focus_widget != widgetToGetMouse) {
[2]847 QWheelEvent qwe2(QApplicationPrivate::focus_widget->mapFromGlobal(qglobal), qglobal,
[561]848 deltaY, buttons, keyMods, Qt::Vertical);
[2]849 qt_sendSpontaneousEvent(QApplicationPrivate::focus_widget, &qwe2);
850 wheelOK = qwe2.isAccepted();
851 }
852 }
[561]853
[2]854 if (deltaZ) {
855 // Qt doesn't explicitly support wheels with a Z component. In a misguided attempt to
856 // try to be ahead of the pack, I'm adding this extra value.
857 QWheelEvent qwe(qlocal, qglobal, deltaZ, buttons, keyMods, (Qt::Orientation)3);
[561]858 qt_sendSpontaneousEvent(widgetToGetMouse, &qwe);
[2]859 wheelOK = qwe.isAccepted();
860 if (!wheelOK && QApplicationPrivate::focus_widget
[561]861 && QApplicationPrivate::focus_widget != widgetToGetMouse) {
[2]862 QWheelEvent qwe2(QApplicationPrivate::focus_widget->mapFromGlobal(qglobal), qglobal,
863 deltaZ, buttons, keyMods, (Qt::Orientation)3);
864 qt_sendSpontaneousEvent(QApplicationPrivate::focus_widget, &qwe2);
865 wheelOK = qwe2.isAccepted();
866 }
867 }
868 if (!wheelOK) {
869 return [super scrollWheel:theEvent];
870 }
871}
872
873- (void)tabletProximity:(NSEvent *)tabletEvent
874{
875 qt_dispatchTabletProximityEvent(tabletEvent);
876}
877
878- (void)tabletPoint:(NSEvent *)tabletEvent
879{
880 if (!qt_mac_handleTabletEvent(self, tabletEvent))
881 [super tabletPoint:tabletEvent];
882}
883
[561]884#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
885- (void)touchesBeganWithEvent:(NSEvent *)event;
886{
887 bool all = qwidget->testAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
888 qt_translateRawTouchEvent(qwidget, QTouchEvent::TouchPad, QCocoaTouch::getCurrentTouchPointList(event, all));
889}
890
891- (void)touchesMovedWithEvent:(NSEvent *)event;
892{
893 bool all = qwidget->testAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
894 qt_translateRawTouchEvent(qwidget, QTouchEvent::TouchPad, QCocoaTouch::getCurrentTouchPointList(event, all));
895}
896
897- (void)touchesEndedWithEvent:(NSEvent *)event;
898{
899 bool all = qwidget->testAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
900 qt_translateRawTouchEvent(qwidget, QTouchEvent::TouchPad, QCocoaTouch::getCurrentTouchPointList(event, all));
901}
902
903- (void)touchesCancelledWithEvent:(NSEvent *)event;
904{
905 bool all = qwidget->testAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
906 qt_translateRawTouchEvent(qwidget, QTouchEvent::TouchPad, QCocoaTouch::getCurrentTouchPointList(event, all));
907}
908#endif // MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
909
910- (void)magnifyWithEvent:(NSEvent *)event;
911{
912 if (!QApplicationPrivate::tryModalHelper(qwidget, 0))
913 return;
914
915 QNativeGestureEvent qNGEvent;
916 qNGEvent.gestureType = QNativeGestureEvent::Zoom;
917 NSPoint p = [[event window] convertBaseToScreen:[event locationInWindow]];
918 qNGEvent.position = flipPoint(p).toPoint();
919 qNGEvent.percentage = [event magnification];
920 qt_sendSpontaneousEvent(qwidget, &qNGEvent);
921}
922
923- (void)rotateWithEvent:(NSEvent *)event;
924{
925 if (!QApplicationPrivate::tryModalHelper(qwidget, 0))
926 return;
927
928 QNativeGestureEvent qNGEvent;
929 qNGEvent.gestureType = QNativeGestureEvent::Rotate;
930 NSPoint p = [[event window] convertBaseToScreen:[event locationInWindow]];
931 qNGEvent.position = flipPoint(p).toPoint();
932 qNGEvent.percentage = -[event rotation];
933 qt_sendSpontaneousEvent(qwidget, &qNGEvent);
934}
935
936- (void)swipeWithEvent:(NSEvent *)event;
937{
938 if (!QApplicationPrivate::tryModalHelper(qwidget, 0))
939 return;
940
941 QNativeGestureEvent qNGEvent;
942 qNGEvent.gestureType = QNativeGestureEvent::Swipe;
943 NSPoint p = [[event window] convertBaseToScreen:[event locationInWindow]];
944 qNGEvent.position = flipPoint(p).toPoint();
945 if ([event deltaX] == 1)
946 qNGEvent.angle = 180.0f;
947 else if ([event deltaX] == -1)
948 qNGEvent.angle = 0.0f;
949 else if ([event deltaY] == 1)
950 qNGEvent.angle = 90.0f;
951 else if ([event deltaY] == -1)
952 qNGEvent.angle = 270.0f;
953 qt_sendSpontaneousEvent(qwidget, &qNGEvent);
954}
955
956- (void)beginGestureWithEvent:(NSEvent *)event;
957{
958 if (!QApplicationPrivate::tryModalHelper(qwidget, 0))
959 return;
960
961 QNativeGestureEvent qNGEvent;
962 qNGEvent.gestureType = QNativeGestureEvent::GestureBegin;
963 NSPoint p = [[event window] convertBaseToScreen:[event locationInWindow]];
964 qNGEvent.position = flipPoint(p).toPoint();
965 qt_sendSpontaneousEvent(qwidget, &qNGEvent);
966}
967
968- (void)endGestureWithEvent:(NSEvent *)event;
969{
970 if (!QApplicationPrivate::tryModalHelper(qwidget, 0))
971 return;
972
973 QNativeGestureEvent qNGEvent;
974 qNGEvent.gestureType = QNativeGestureEvent::GestureEnd;
975 NSPoint p = [[event window] convertBaseToScreen:[event locationInWindow]];
976 qNGEvent.position = flipPoint(p).toPoint();
977 qt_sendSpontaneousEvent(qwidget, &qNGEvent);
978}
979
[2]980- (void)frameDidChange:(NSNotification *)note
981{
982 Q_UNUSED(note);
983 if (qwidget->isWindow())
984 return;
985 NSRect newFrame = [self frame];
986 QRect newGeo(newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height);
987 bool moved = qwidget->testAttribute(Qt::WA_Moved);
988 bool resized = qwidget->testAttribute(Qt::WA_Resized);
989 qwidget->setGeometry(newGeo);
990 qwidget->setAttribute(Qt::WA_Moved, moved);
991 qwidget->setAttribute(Qt::WA_Resized, resized);
992 qwidgetprivate->syncCocoaMask();
993}
994
995- (BOOL)isEnabled
996{
997 if (!qwidget)
998 return [super isEnabled];
999 return [super isEnabled] && qwidget->isEnabled();
1000}
1001
1002- (void)setEnabled:(BOOL)flag
1003{
1004 QMacCocoaAutoReleasePool pool;
1005 [super setEnabled:flag];
1006 if (qwidget->isEnabled() != flag)
1007 qwidget->setEnabled(flag);
1008}
1009
1010+ (Class)cellClass
1011{
1012 return [NSActionCell class];
1013}
1014
1015- (BOOL)acceptsFirstResponder
1016{
1017 if (qwidget->isWindow())
1018 return YES; // Always do it, so that windows can accept key press events.
1019 return qwidget->focusPolicy() != Qt::NoFocus;
1020}
1021
[561]1022- (BOOL)resignFirstResponder
1023{
1024 // Seems like the following test only triggers if this
1025 // view is inside a QMacNativeWidget:
1026 if (qwidget == QApplication::focusWidget())
1027 QApplicationPrivate::setFocusWidget(0, Qt::OtherFocusReason);
1028 return YES;
1029}
1030
[2]1031- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
1032{
1033 Q_UNUSED(isLocal);
1034 return supportedActions;
1035}
1036
1037- (void)setSupportedActions:(NSDragOperation)actions
1038{
1039 supportedActions = actions;
1040}
1041
1042- (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
1043{
1044 Q_UNUSED(anImage);
1045 Q_UNUSED(aPoint);
1046 qMacDnDParams()->performedAction = operation;
[561]1047 if (QDragManager::self()->object
1048 && QDragManager::self()->dragPrivate()->executed_action != Qt::ActionMask) {
1049 qMacDnDParams()->performedAction =
1050 qt_mac_mapDropAction(QDragManager::self()->dragPrivate()->executed_action);
1051 }
[2]1052}
1053
1054- (QWidget *)qt_qwidget
1055{
1056 return qwidget;
1057}
1058
1059- (BOOL)qt_leftButtonIsRightButton
1060{
1061 return leftButtonIsRightButton;
1062}
1063
1064- (void)qt_setLeftButtonIsRightButton:(BOOL)isSwapped
1065{
1066 leftButtonIsRightButton = isSwapped;
1067}
1068
1069+ (DnDParams*)currentMouseEvent
1070{
1071 return qMacDnDParams();
1072}
1073
1074- (void)keyDown:(NSEvent *)theEvent
1075{
1076 sendKeyEvents = true;
1077
1078 QWidget *widgetToGetKey = qwidget;
1079
1080 QWidget *popup = qAppInstance()->activePopupWidget();
1081 bool sendToPopup = false;
1082 if (popup && popup != qwidget->window()) {
1083 widgetToGetKey = popup->focusWidget() ? popup->focusWidget() : popup;
1084 sendToPopup = true;
1085 }
1086
[561]1087 if (widgetToGetKey->testAttribute(Qt::WA_InputMethodEnabled)
1088 && !(widgetToGetKey->inputMethodHints() & Qt::ImhDigitsOnly
1089 || widgetToGetKey->inputMethodHints() & Qt::ImhFormattedNumbersOnly
1090 || widgetToGetKey->inputMethodHints() & Qt::ImhHiddenText)) {
[2]1091 [qt_mac_nativeview_for(widgetToGetKey) interpretKeyEvents:[NSArray arrayWithObject: theEvent]];
1092 }
1093 if (sendKeyEvents && !composing) {
1094 bool keyOK = qt_dispatchKeyEvent(theEvent, widgetToGetKey);
1095 if (!keyOK && !sendToPopup)
1096 [super keyDown:theEvent];
1097 }
1098}
1099
1100
1101- (void)keyUp:(NSEvent *)theEvent
1102{
1103 if (sendKeyEvents) {
1104 bool keyOK = qt_dispatchKeyEvent(theEvent, qwidget);
1105 if (!keyOK)
1106 [super keyUp:theEvent];
1107 }
1108}
1109
1110- (void)viewWillMoveToWindow:(NSWindow *)window
1111{
1112 if (qwidget->windowFlags() & Qt::MSWindowsOwnDC
1113 && (window != [self window])) { // OpenGL Widget
1114 // Create a stupid ClearDrawable Event
1115 QEvent event(QEvent::MacGLClearDrawable);
1116 qApp->sendEvent(qwidget, &event);
1117 }
1118}
1119
1120- (void)viewDidMoveToWindow
1121{
1122 if (qwidget->windowFlags() & Qt::MSWindowsOwnDC && [self window]) {
1123 // call update paint event
1124 qwidgetprivate->needWindowChange = true;
1125 QEvent event(QEvent::MacGLWindowChange);
1126 qApp->sendEvent(qwidget, &event);
1127 }
1128}
1129
1130
1131// NSTextInput Protocol implementation
1132
1133- (void) insertText:(id)aString
1134{
[561]1135 QString commitText;
1136 if ([aString length]) {
[2]1137 if ([aString isKindOfClass:[NSAttributedString class]]) {
[561]1138 commitText = QCFString::toQString(reinterpret_cast<CFStringRef>([aString string]));
[2]1139 } else {
1140 commitText = QCFString::toQString(reinterpret_cast<CFStringRef>(aString));
1141 };
[561]1142 }
1143
1144 if ([aString length] && composing) {
1145 // Send the commit string to the widget.
[2]1146 composing = false;
1147 sendKeyEvents = false;
1148 QInputMethodEvent e;
1149 e.setCommitString(commitText);
1150 qt_sendSpontaneousEvent(qwidget, &e);
[561]1151 } else {
1152 // The key sequence "`q" on a French Keyboard will generate two calls to insertText before
1153 // it returns from interpretKeyEvents. The first call will turn off 'composing' and accept
1154 // the "`" key. The last keyDown event needs to be processed by the widget to get the
1155 // character "q". The string parameter is ignored for the second call.
1156 sendKeyEvents = true;
[2]1157 }
[561]1158
1159 composingText->clear();
[2]1160}
1161
1162- (void) setMarkedText:(id)aString selectedRange:(NSRange)selRange
1163{
1164 // Generate the QInputMethodEvent with preedit string and the attributes
1165 // for rendering it. The attributes handled here are 'underline',
1166 // 'underline color' and 'cursor position'.
1167 sendKeyEvents = false;
1168 composing = true;
1169 QString qtText;
1170 // Cursor position is retrived from the range.
1171 QList<QInputMethodEvent::Attribute> attrs;
1172 attrs<<QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, selRange.location, 1, QVariant());
1173 if ([aString isKindOfClass:[NSAttributedString class]]) {
1174 qtText = QCFString::toQString(reinterpret_cast<CFStringRef>([aString string]));
1175 composingLength = qtText.length();
1176 int index = 0;
1177 // Create attributes for individual sections of preedit text
1178 while (index < composingLength) {
1179 NSRange effectiveRange;
1180 NSRange range = NSMakeRange(index, composingLength-index);
[561]1181 NSDictionary *attributes = [aString attributesAtIndex:index
[2]1182 longestEffectiveRange:&effectiveRange
1183 inRange:range];
1184 NSNumber *underlineStyle = [attributes objectForKey:NSUnderlineStyleAttributeName];
1185 if (underlineStyle) {
1186 QColor clr (Qt::black);
1187 NSColor *color = [attributes objectForKey:NSUnderlineColorAttributeName];
1188 if (color) {
1189 clr = colorFrom(color);
[561]1190 }
[2]1191 QTextCharFormat format;
1192 format.setFontUnderline(true);
1193 format.setUnderlineColor(clr);
1194 attrs<<QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
1195 effectiveRange.location,
1196 effectiveRange.length,
1197 format);
1198 }
1199 index = effectiveRange.location + effectiveRange.length;
1200 }
1201 } else {
1202 // No attributes specified, take only the preedit text.
1203 qtText = QCFString::toQString(reinterpret_cast<CFStringRef>(aString));
1204 composingLength = qtText.length();
1205 }
1206 // Make sure that we have at least one text format.
1207 if (attrs.size() <= 1) {
1208 QTextCharFormat format;
1209 format.setFontUnderline(true);
1210 attrs<<QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
1211 0, composingLength, format);