Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnsview.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtGui/qtguiglobal.h>
6
7#include <AppKit/AppKit.h>
8#include <MetalKit/MetalKit.h>
9#include <UniformTypeIdentifiers/UTCoreTypes.h>
10
11#include "qnsview.h"
12#include "qcocoawindow.h"
13#include "qcocoahelpers.h"
14#include "qcocoascreen.h"
16#include "qcocoadrag.h"
18#include <qpa/qplatformintegration.h>
19
20#include <qpa/qwindowsysteminterface.h>
21#include <QtGui/QTextFormat>
22#include <QtCore/QDebug>
23#include <QtCore/QPointer>
24#include <QtCore/QSet>
25#include <QtCore/private/qcore_mac_p.h>
26#include <QtGui/QAccessible>
27#include <QtGui/QImage>
28#include <private/qguiapplication_p.h>
29#include <private/qcoregraphics_p.h>
30#include <private/qwindow_p.h>
31#include <private/qpointingdevice_p.h>
32#include <private/qhighdpiscaling_p.h>
34#ifndef QT_NO_OPENGL
36#endif
38#include <QtGui/private/qmacmimeregistry_p.h>
39#include <QtGui/private/qmetallayer_p.h>
40
41#include <QuartzCore/CATransaction.h>
42
43@interface QNSView (Drawing) <CALayerDelegate>
44- (void)initDrawing;
45@end
46
47@interface QT_MANGLE_NAMESPACE(QNSViewMouseMoveHelper) : NSObject
48- (instancetype)initWithView:(QNSView *)theView;
49- (void)mouseMoved:(NSEvent *)theEvent;
50- (void)mouseEntered:(NSEvent *)theEvent;
51- (void)mouseExited:(NSEvent *)theEvent;
52- (void)cursorUpdate:(NSEvent *)theEvent;
53@end
54
55QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSViewMouseMoveHelper);
56
57@interface QNSView (Mouse)
58- (void)initMouse;
59- (NSPoint)screenMousePoint:(NSEvent *)theEvent;
60- (void)mouseMovedImpl:(NSEvent *)theEvent;
61- (void)mouseEnteredImpl:(NSEvent *)theEvent;
62- (void)mouseExitedImpl:(NSEvent *)theEvent;
63@end
64
65@interface QNSView (Touch)
66@end
67
68@interface QNSView (Tablet)
69- (bool)handleTabletEvent:(NSEvent *)theEvent;
70@end
71
72@interface QNSView (Gestures)
73@end
74
75@interface QNSView (Dragging)
76-(void)registerDragTypes;
77@end
78
79@interface QNSView (Keys)
80@end
81
82@interface QNSView (ComplexText) <NSTextInputClient>
83@property (readonly) QObject* focusObject;
84@end
85
86@interface QNSView (ServicesMenu) <NSServicesMenuRequestor>
87@end
88
89#if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(150000)
90@interface QNSView (ContentSelectionInfo) <NSViewContentSelectionInfo>
91@end
92#endif
93
94@interface QT_MANGLE_NAMESPACE(QNSViewMenuHelper) : NSObject
95- (instancetype)initWithView:(QNSView *)theView;
96@end
97QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSViewMenuHelper);
98
99// Private interface
100@interface QNSView ()
101- (BOOL)isTransparentForUserInput;
102@property (assign) NSView* previousSuperview;
103@property (assign) NSWindow* previousWindow;
104@property (retain) QNSViewMenuHelper* menuHelper;
105@property (nonatomic, retain) NSColorSpace *colorSpace;
106@end
107
108@implementation QNSView {
109 QPointer<QCocoaWindow> m_platformWindow;
110
111 // Mouse
112 QNSViewMouseMoveHelper *m_mouseMoveHelper;
113 Qt::MouseButtons m_buttons;
114 Qt::MouseButtons m_acceptedMouseDowns;
115 Qt::MouseButtons m_frameStrutButtons;
116 Qt::KeyboardModifiers m_currentWheelModifiers;
117 bool m_dontOverrideCtrlLMB;
118 bool m_sendUpAsRightButton;
119 bool m_scrolling;
120 bool m_updatingDrag;
121
122 // Keys
123 bool m_lastKeyDead;
124 bool m_sendKeyEvent;
125 bool m_sendKeyEventWithoutText;
126 NSEvent *m_currentlyInterpretedKeyEvent;
127 QSet<quint32> m_acceptedKeyDowns;
128
129 // Text
130 QString m_composingText;
131 QPointer<QObject> m_composingFocusObject;
132}
133
134@synthesize colorSpace = m_colorSpace;
135
136- (instancetype)initWithCocoaWindow:(QCocoaWindow *)platformWindow
137{
138 if ((self = [super initWithFrame:NSZeroRect])) {
139 m_platformWindow = platformWindow;
140
141 // NSViews are by default visible, but QWindows are not.
142 // We should ideally pick up the actual QWindow state here,
143 // but QWindowPrivate::setVisible() expects to control the
144 // order of events tightly, so we need to wait for a call
145 // to QCocoaWindow::setVisible().
146 self.hidden = YES;
147
148 self.focusRingType = NSFocusRingTypeNone;
149
150 self.previousSuperview = nil;
151 self.previousWindow = nil;
152
153 [self initDrawing];
154 [self initMouse];
155 [self registerDragTypes];
156
157 m_updatingDrag = false;
158
159 m_lastKeyDead = false;
160 m_sendKeyEvent = false;
161 m_currentlyInterpretedKeyEvent = nil;
162
163 self.menuHelper = [[[QNSViewMenuHelper alloc] initWithView:self] autorelease];
164 }
165 return self;
166}
167
168- (void)dealloc
169{
170 qCDebug(lcQpaWindow) << "Deallocating" << self;
171
172 self.menuHelper = nil;
173
174 [[NSNotificationCenter defaultCenter] removeObserver:self];
175 [m_mouseMoveHelper release];
176
177 [super dealloc];
178}
179
180- (NSString *)description
181{
182 NSMutableString *description = [NSMutableString stringWithString:[super description]];
183
184#ifndef QT_NO_DEBUG_STREAM
185 QString platformWindowDescription;
186 QDebug debug(&platformWindowDescription);
187 debug.nospace() << "; " << m_platformWindow << ">";
188
189 NSRange lastCharacter = [description rangeOfComposedCharacterSequenceAtIndex:description.length - 1];
190 [description replaceCharactersInRange:lastCharacter withString:platformWindowDescription.toNSString()];
191#endif
192
193 return description;
194}
195
196// ----------------------------- Re-parenting ---------------------------------
197
198- (void)removeFromSuperview
199{
200 qCDebug(lcQpaWindow) << "Removing" << self << "from" << self.superview;
201 QMacAutoReleasePool pool;
202 [super removeFromSuperview];
203}
204
205- (void)viewWillMoveToSuperview:(NSView *)newSuperview
206{
207 Q_ASSERT(!self.previousSuperview);
208 self.previousSuperview = self.superview;
209
210 if (newSuperview == self.superview)
211 qCDebug(lcQpaWindow) << "Re-ordering" << self << "inside" << self.superview;
212 else
213 qCDebug(lcQpaWindow) << "Re-parenting" << self << "from" << self.superview << "to" << newSuperview;
214}
215
216- (void)viewDidMoveToSuperview
217{
218 // We reset previousSuperview in didMoveToWindow below, as that's
219 // when we also have a fully formed picture of what the window is.
220
221 if (self.superview == self.previousSuperview) {
222 qCDebug(lcQpaWindow) << "Done re-ordering" << self << "new index:"
223 << [self.superview.subviews indexOfObject:self];
224 return;
225 }
226
227 // Note: at this point the view's window property hasn't been updated to match the window
228 // of the new superview. We have to wait for viewDidMoveToWindow for that to be reflected.
229}
230
231- (void)viewWillMoveToWindow:(NSWindow *)newWindow
232{
233 Q_ASSERT(!self.previousWindow);
234 self.previousWindow = self.window;
235
236 // This callback is documented to be called also when a view is just moved between
237 // subviews in the same NSWindow, so we're not necessarily moving between NSWindows.
238 if (newWindow != self.window)
239 qCDebug(lcQpaWindow) << "Moving" << self << "from" << self.window << "to" << newWindow;
240}
241
242- (void)viewDidMoveToWindow
243{
244 // Reset up front, in case the callbacks below trigger another reparent
245 auto previousSuperview = self.previousSuperview;
246 self.previousSuperview = nil;
247 auto previousWindow = self.previousWindow;
248 self.previousWindow = nil;
249
250 if (!m_platformWindow)
251 return;
252
253 // This callback is documented to be called also when a view is just
254 // moved between subviews in the same NSWindow. And now we also know
255 // what the new window will be, and it's reflected though view.window.
256
257 if (self.superview != previousSuperview)
258 m_platformWindow->viewDidMoveToSuperview(previousSuperview);
259
260 if (self.window != previousWindow)
261 m_platformWindow->viewDidMoveToWindow(previousWindow);
262}
263
264// QWindow::setParent() promises that the child window will be clipped
265// to its parent, which we rely on in e.g. Qt Widgets when a native window
266// is added to a scroll area. We try to be smart and only enable clipping
267// if we have potential child QWindows that rely on this behavior.
268// FIXME: Be even smarter, and only consider QWindow based subviews,
269// in a way that also includes foreign windows.
270
271- (void)didAddSubview:(NSView *)subview
272{
273 self.clipsToBounds = YES;
274}
275
276- (void)willRemoveSubview:(NSView *)subview
277{
278 self.clipsToBounds = self.subviews.count > 1;
279}
280
281// ----------------------------------------------------------------------------
282
283- (QWindow *)topLevelWindow
284{
285 if (!m_platformWindow)
286 return nullptr;
287
288 QWindow *focusWindow = m_platformWindow->window();
289
290 // For widgets we need to do a bit of trickery as the window
291 // to activate is the window of the top-level widget.
292 if (qstrcmp(focusWindow->metaObject()->className(), "QWidgetWindow") == 0) {
293 while (focusWindow->parent()) {
294 focusWindow = focusWindow->parent();
295 }
296 }
297
298 return focusWindow;
299}
300
301/*
302 Invoked when the view is hidden, either directly,
303 or in response to an ancestor being hidden.
304*/
305- (void)viewDidHide
306{
307 qCDebug(lcQpaWindow) << "Did hide" << self;
308
309 if (!m_platformWindow->isExposed())
310 return;
311
312 m_platformWindow->handleExposeEvent(QRegion());
313}
314
315/*
316 Invoked when the view is unhidden, either directly,
317 or in response to an ancestor being unhidden.
318*/
319- (void)viewDidUnhide
320{
321 qCDebug(lcQpaWindow) << "Did unhide" << self;
322
323 [self setNeedsDisplay:YES];
324}
325
326- (BOOL)isTransparentForUserInput
327{
328 return m_platformWindow->window() &&
329 m_platformWindow->window()->flags() & Qt::WindowTransparentForInput;
330}
331
332- (BOOL)becomeFirstResponder
333{
334 if (!m_platformWindow)
335 return NO;
336 if ([self isTransparentForUserInput])
337 return NO;
338
339 if (!m_platformWindow->windowIsPopupType()
340 && (!self.window.canBecomeKeyWindow || self.window.keyWindow)) {
341 // Calling handleWindowActivated for a QWindow has two effects: first, it
342 // will set the QWindow (and all other QWindows in the same hierarchy)
343 // as Active. Being Active means that the window should appear active from
344 // a style perspective (according to QWindow::isActive()). The second
345 // effect is that it will set QQuiApplication::focusWindow() to point to
346 // the QWindow. The latter means that the QWindow should have keyboard
347 // focus. But those two are not necessarily the same; A tool window could e.g be
348 // rendered as Active while the parent window, which is also Active, has
349 // input focus. But we currently don't distinguish between that cleanly in Qt.
350 // Since we don't want a QWindow to be rendered as Active when the NSWindow
351 // it belongs to is not key, we skip calling handleWindowActivated when
352 // that is the case. Instead, we wait for the window to become key, and handle
353 // QWindow activation from QCocoaWindow::windowDidBecomeKey instead. The only
354 // exception is if the window can never become key, in which case we naturally
355 // cannot wait for that to happen.
356 QWindowSystemInterface::handleFocusWindowChanged<QWindowSystemInterface::SynchronousDelivery>(
357 [self topLevelWindow], Qt::ActiveWindowFocusReason);
358 }
359
360 return YES;
361}
362
363- (BOOL)acceptsFirstResponder
364{
365 if (!m_platformWindow)
366 return NO;
367 if (m_platformWindow->shouldRefuseKeyWindowAndFirstResponder())
368 return NO;
369 if ([self isTransparentForUserInput])
370 return NO;
371 if ((m_platformWindow->window()->flags() & Qt::ToolTip) == Qt::ToolTip)
372 return NO;
373 return YES;
374}
375
376- (NSView *)hitTest:(NSPoint)aPoint
377{
378 NSView *candidate = [super hitTest:aPoint];
379 if (candidate == self) {
380 if ([self isTransparentForUserInput])
381 return nil;
382 }
383 return candidate;
384}
385
386- (void)convertFromScreen:(NSPoint)mouseLocation toWindowPoint:(QPointF *)qtWindowPoint andScreenPoint:(QPointF *)qtScreenPoint
387{
388 // Calculate the mouse position in the QWindow and Qt screen coordinate system,
389 // starting from coordinates in the NSWindow coordinate system.
390 //
391 // This involves translating according to the window location on screen,
392 // as well as inverting the y coordinate due to the origin change.
393 //
394 // Coordinate system overview, outer to innermost:
395 //
396 // Name Origin
397 //
398 // OS X screen bottom-left
399 // Qt screen top-left
400 // NSWindow bottom-left
401 // NSView/QWindow top-left
402 //
403 // NSView and QWindow are equal coordinate systems: the QWindow covers the
404 // entire NSView, and we've set the NSView's isFlipped property to true.
405
406 NSWindow *window = [self window];
407 NSPoint nsWindowPoint;
408 NSRect windowRect = [window convertRectFromScreen:NSMakeRect(mouseLocation.x, mouseLocation.y, 1, 1)];
409 nsWindowPoint = windowRect.origin; // NSWindow coordinates
410 NSPoint nsViewPoint = [self convertPoint: nsWindowPoint fromView: nil]; // NSView/QWindow coordinates
411 *qtWindowPoint = QPointF(nsViewPoint.x, nsViewPoint.y); // NSView/QWindow coordinates
412 *qtScreenPoint = QCocoaScreen::mapFromNative(mouseLocation);
413}
414
415@end
416
418#include "qnsview_mouse.mm"
419#include "qnsview_touch.mm"
421#include "qnsview_tablet.mm"
423#include "qnsview_keys.mm"
425#include "qnsview_menus.mm"
426#if QT_CONFIG(accessibility)
427#include "qnsview_accessibility.mm"
428#endif
429
430// -----------------------------------------------------
431
432@implementation QNSView (QtExtras)
433
434- (QCocoaWindow*)platformWindow
435{
436 return m_platformWindow.data();;
437}
438
439@end
\inmodule QtGui
Definition qwindow.h:64
Q_FORWARD_DECLARE_OBJC_CLASS(NSView)
Q_FORWARD_DECLARE_OBJC_CLASS(NSString)