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