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 auto cleanup = qScopeGuard([&] { self.previousSuperview = nil; });
219
220 if (self.superview == self.previousSuperview) {
221 qCDebug(lcQpaWindow) << "Done re-ordering" << self << "new index:"
222 << [self.superview.subviews indexOfObject:self];
223 return;
224 }
225
226 qCDebug(lcQpaWindow) << "Done re-parenting" << self << "into" << self.superview;
227
228 // Note: at this point the view's window property hasn't been updated to match the window
229 // of the new superview. We have to wait for viewDidMoveToWindow for that to be reflected.
230
231 if (!m_platformWindow)
232 return;
233
234 if (!m_platformWindow->isEmbedded())
235 return;
236
237 if ([self superview]) {
238 QWindowSystemInterface::handleGeometryChange(m_platformWindow->window(), m_platformWindow->geometry());
239 [self setNeedsDisplay:YES];
240 QWindowSystemInterface::flushWindowSystemEvents();
241 }
242}
243
244- (void)viewWillMoveToWindow:(NSWindow *)newWindow
245{
246 Q_ASSERT(!self.previousWindow);
247 self.previousWindow = self.window;
248
249 // This callback is documented to be called also when a view is just moved between
250 // subviews in the same NSWindow, so we're not necessarily moving between NSWindows.
251 if (newWindow == self.window)
252 return;
253
254 qCDebug(lcQpaWindow) << "Moving" << self << "from" << self.window << "to" << newWindow;
255
256 // Note: at this point the superview has already been updated, so we know which view inside
257 // the new window the view will be a child of.
258}
259
260- (void)viewDidMoveToWindow
261{
262 auto cleanup = qScopeGuard([&] { self.previousWindow = nil; });
263
264 // This callback is documented to be called also when a view is just moved between
265 // subviews in the same NSWindow, so we're not necessarily moving between NSWindows.
266 if (self.window == self.previousWindow)
267 return;
268
269 qCDebug(lcQpaWindow) << "Done moving" << self << "to" << self.window;
270}
271
272// ----------------------------------------------------------------------------
273
274- (QWindow *)topLevelWindow
275{
276 if (!m_platformWindow)
277 return nullptr;
278
279 QWindow *focusWindow = m_platformWindow->window();
280
281 // For widgets we need to do a bit of trickery as the window
282 // to activate is the window of the top-level widget.
283 if (qstrcmp(focusWindow->metaObject()->className(), "QWidgetWindow") == 0) {
284 while (focusWindow->parent()) {
285 focusWindow = focusWindow->parent();
286 }
287 }
288
289 return focusWindow;
290}
291
292/*
293 Invoked when the view is hidden, either directly,
294 or in response to an ancestor being hidden.
295*/
296- (void)viewDidHide
297{
298 qCDebug(lcQpaWindow) << "Did hide" << self;
299
300 if (!m_platformWindow->isExposed())
301 return;
302
303 m_platformWindow->handleExposeEvent(QRegion());
304}
305
306/*
307 Invoked when the view is unhidden, either directly,
308 or in response to an ancestor being unhidden.
309*/
310- (void)viewDidUnhide
311{
312 qCDebug(lcQpaWindow) << "Did unhide" << self;
313
314 [self setNeedsDisplay:YES];
315}
316
317- (BOOL)isTransparentForUserInput
318{
319 return m_platformWindow->window() &&
320 m_platformWindow->window()->flags() & Qt::WindowTransparentForInput;
321}
322
323- (BOOL)becomeFirstResponder
324{
325 if (!m_platformWindow)
326 return NO;
327 if ([self isTransparentForUserInput])
328 return NO;
329
330 if (!m_platformWindow->windowIsPopupType()
331 && (!self.window.canBecomeKeyWindow || self.window.keyWindow)) {
332 // Calling handleWindowActivated for a QWindow has two effects: first, it
333 // will set the QWindow (and all other QWindows in the same hierarchy)
334 // as Active. Being Active means that the window should appear active from
335 // a style perspective (according to QWindow::isActive()). The second
336 // effect is that it will set QQuiApplication::focusWindow() to point to
337 // the QWindow. The latter means that the QWindow should have keyboard
338 // focus. But those two are not necessarily the same; A tool window could e.g be
339 // rendered as Active while the parent window, which is also Active, has
340 // input focus. But we currently don't distinguish between that cleanly in Qt.
341 // Since we don't want a QWindow to be rendered as Active when the NSWindow
342 // it belongs to is not key, we skip calling handleWindowActivated when
343 // that is the case. Instead, we wait for the window to become key, and handle
344 // QWindow activation from QCocoaWindow::windowDidBecomeKey instead. The only
345 // exception is if the window can never become key, in which case we naturally
346 // cannot wait for that to happen.
347 QWindowSystemInterface::handleFocusWindowChanged<QWindowSystemInterface::SynchronousDelivery>(
348 [self topLevelWindow], Qt::ActiveWindowFocusReason);
349 }
350
351 return YES;
352}
353
354- (BOOL)acceptsFirstResponder
355{
356 if (!m_platformWindow)
357 return NO;
358 if (m_platformWindow->shouldRefuseKeyWindowAndFirstResponder())
359 return NO;
360 if ([self isTransparentForUserInput])
361 return NO;
362 if ((m_platformWindow->window()->flags() & Qt::ToolTip) == Qt::ToolTip)
363 return NO;
364 return YES;
365}
366
367- (NSView *)hitTest:(NSPoint)aPoint
368{
369 NSView *candidate = [super hitTest:aPoint];
370 if (candidate == self) {
371 if ([self isTransparentForUserInput])
372 return nil;
373 }
374 return candidate;
375}
376
377- (void)convertFromScreen:(NSPoint)mouseLocation toWindowPoint:(QPointF *)qtWindowPoint andScreenPoint:(QPointF *)qtScreenPoint
378{
379 // Calculate the mouse position in the QWindow and Qt screen coordinate system,
380 // starting from coordinates in the NSWindow coordinate system.
381 //
382 // This involves translating according to the window location on screen,
383 // as well as inverting the y coordinate due to the origin change.
384 //
385 // Coordinate system overview, outer to innermost:
386 //
387 // Name Origin
388 //
389 // OS X screen bottom-left
390 // Qt screen top-left
391 // NSWindow bottom-left
392 // NSView/QWindow top-left
393 //
394 // NSView and QWindow are equal coordinate systems: the QWindow covers the
395 // entire NSView, and we've set the NSView's isFlipped property to true.
396
397 NSWindow *window = [self window];
398 NSPoint nsWindowPoint;
399 NSRect windowRect = [window convertRectFromScreen:NSMakeRect(mouseLocation.x, mouseLocation.y, 1, 1)];
400 nsWindowPoint = windowRect.origin; // NSWindow coordinates
401 NSPoint nsViewPoint = [self convertPoint: nsWindowPoint fromView: nil]; // NSView/QWindow coordinates
402 *qtWindowPoint = QPointF(nsViewPoint.x, nsViewPoint.y); // NSView/QWindow coordinates
403 *qtScreenPoint = QCocoaScreen::mapFromNative(mouseLocation);
404}
405
406@end
407
409#include "qnsview_mouse.mm"
410#include "qnsview_touch.mm"
412#include "qnsview_tablet.mm"
414#include "qnsview_keys.mm"
416#include "qnsview_menus.mm"
417#if QT_CONFIG(accessibility)
418#include "qnsview_accessibility.mm"
419#endif
420
421// -----------------------------------------------------
422
423@implementation QNSView (QtExtras)
424
425- (QCocoaWindow*)platformWindow
426{
427 return m_platformWindow.data();;
428}
429
430@end
\inmodule QtGui
Definition qwindow.h:63
Q_FORWARD_DECLARE_OBJC_CLASS(NSView)
Q_FORWARD_DECLARE_OBJC_CLASS(NSString)