PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/qt/qtbase/src/gui/kernel/qplatformwindow.cpp

https://gitlab.com/x33n/phantomjs
C++ | 645 lines | 241 code | 57 blank | 347 comment | 23 complexity | 2067a33f4a3885a8e57c60fbd4e097c4 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtGui module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qplatformwindow.h"
  42. #include "qplatformwindow_p.h"
  43. #include "qplatformscreen.h"
  44. #include <private/qguiapplication_p.h>
  45. #include <qpa/qwindowsysteminterface.h>
  46. #include <QtGui/qwindow.h>
  47. #include <QtGui/qscreen.h>
  48. #include <private/qwindow_p.h>
  49. QT_BEGIN_NAMESPACE
  50. /*!
  51. Constructs a platform window with the given top level window.
  52. */
  53. QPlatformWindow::QPlatformWindow(QWindow *window)
  54. : QPlatformSurface(window)
  55. , d_ptr(new QPlatformWindowPrivate)
  56. {
  57. Q_D(QPlatformWindow);
  58. d->rect = window->geometry();
  59. }
  60. /*!
  61. Virtual destructor does not delete its top level window.
  62. */
  63. QPlatformWindow::~QPlatformWindow()
  64. {
  65. }
  66. /*!
  67. Returns the window which belongs to the QPlatformWindow
  68. */
  69. QWindow *QPlatformWindow::window() const
  70. {
  71. return static_cast<QWindow *>(m_surface);
  72. }
  73. /*!
  74. Returns the parent platform window (or 0 if orphan).
  75. */
  76. QPlatformWindow *QPlatformWindow::parent() const
  77. {
  78. return window()->parent() ? window()->parent()->handle() : 0;
  79. }
  80. /*!
  81. Returns the platform screen handle corresponding to this platform window.
  82. */
  83. QPlatformScreen *QPlatformWindow::screen() const
  84. {
  85. return window()->screen()->handle();
  86. }
  87. /*!
  88. Returns the actual surface format of the window.
  89. */
  90. QSurfaceFormat QPlatformWindow::format() const
  91. {
  92. return QSurfaceFormat();
  93. }
  94. /*!
  95. This function is called by Qt whenever a window is moved or the window is resized. The resize
  96. can happen programatically(from ie. user application) or by the window manager. This means that
  97. there is no need to call this function specifically from the window manager callback, instead
  98. call QWindowSystemInterface::handleGeometryChange(QWindow *w, const QRect &newRect);
  99. The position(x, y) part of the rect might be inclusive or exclusive of the window frame
  100. as returned by frameMargins(). You can detect this in the plugin by checking
  101. qt_window_private(window())->positionPolicy.
  102. */
  103. void QPlatformWindow::setGeometry(const QRect &rect)
  104. {
  105. Q_D(QPlatformWindow);
  106. d->rect = rect;
  107. }
  108. /*!
  109. Returnes the current geometry of a window
  110. */
  111. QRect QPlatformWindow::geometry() const
  112. {
  113. Q_D(const QPlatformWindow);
  114. return d->rect;
  115. }
  116. /*!
  117. Returns the geometry of a window in 'normal' state
  118. (neither maximized, fullscreen nor minimized) for saving geometries to
  119. application settings.
  120. \since 5.3
  121. */
  122. QRect QPlatformWindow::normalGeometry() const
  123. {
  124. return QRect();
  125. }
  126. QMargins QPlatformWindow::frameMargins() const
  127. {
  128. return QMargins();
  129. }
  130. /*!
  131. Reimplemented in subclasses to show the surface
  132. if \a visible is \c true, and hide it if \a visible is \c false.
  133. The default implementation sends a synchronous expose event.
  134. */
  135. void QPlatformWindow::setVisible(bool visible)
  136. {
  137. Q_UNUSED(visible);
  138. QRect rect(QPoint(), geometry().size());
  139. QWindowSystemInterface::handleExposeEvent(window(), rect);
  140. QWindowSystemInterface::flushWindowSystemEvents();
  141. }
  142. /*!
  143. Requests setting the window flags of this surface
  144. to \a flags.
  145. */
  146. void QPlatformWindow::setWindowFlags(Qt::WindowFlags flags)
  147. {
  148. Q_UNUSED(flags);
  149. }
  150. /*!
  151. Returns if this window is exposed in the windowing system.
  152. An exposeEvent() is sent every time this value changes.
  153. */
  154. bool QPlatformWindow::isExposed() const
  155. {
  156. return window()->isVisible();
  157. }
  158. /*!
  159. Returns \c true if the window should appear active from a style perspective.
  160. This function can make platform-specific isActive checks, such as checking
  161. if the QWindow is embedded in an active native window.
  162. */
  163. bool QPlatformWindow::isActive() const
  164. {
  165. return false;
  166. }
  167. /*!
  168. Returns \c true if the window is a descendant of an embedded non-Qt window.
  169. Example of an embedded non-Qt window is the parent window of an in-process QAxServer.
  170. If \a parentWindow is nonzero, only check if the window is embedded in the
  171. specified \a parentWindow.
  172. */
  173. bool QPlatformWindow::isEmbedded(const QPlatformWindow *parentWindow) const
  174. {
  175. Q_UNUSED(parentWindow);
  176. return false;
  177. }
  178. /*!
  179. Translates the window coordinate \a pos to global screen
  180. coordinates using native methods. This is required for embedded windows,
  181. where the topmost QWindow coordinates are not global screen coordinates.
  182. Returns \a pos if there is no platform specific implementation.
  183. */
  184. QPoint QPlatformWindow::mapToGlobal(const QPoint &pos) const
  185. {
  186. const QPlatformWindow *p = this;
  187. QPoint result = pos;
  188. while (p) {
  189. result += p->geometry().topLeft();
  190. p = p->parent();
  191. }
  192. return result;
  193. }
  194. /*!
  195. Translates the global screen coordinate \a pos to window
  196. coordinates using native methods. This is required for embedded windows,
  197. where the topmost QWindow coordinates are not global screen coordinates.
  198. Returns \a pos if there is no platform specific implementation.
  199. */
  200. QPoint QPlatformWindow::mapFromGlobal(const QPoint &pos) const
  201. {
  202. const QPlatformWindow *p = this;
  203. QPoint result = pos;
  204. while (p) {
  205. result -= p->geometry().topLeft();
  206. p = p->parent();
  207. }
  208. return result;
  209. }
  210. /*!
  211. Requests setting the window state of this surface
  212. to \a type.
  213. Qt::WindowActive can be ignored.
  214. */
  215. void QPlatformWindow::setWindowState(Qt::WindowState)
  216. {
  217. }
  218. /*!
  219. Reimplement in subclasses to return a handle to the native window
  220. */
  221. WId QPlatformWindow::winId() const
  222. {
  223. // Return anything but 0. Returning 0 would cause havoc with QWidgets on
  224. // very basic platform plugins that do not reimplement this function,
  225. // because the top-level widget's internalWinId() would always be 0 which
  226. // would mean top-levels are never treated as native.
  227. return WId(1);
  228. }
  229. /*!
  230. This function is called to enable native child window in QPA. It is common not to support this
  231. feature in Window systems, but can be faked. When this function is called all geometry of this
  232. platform window will be relative to the parent.
  233. */
  234. //jl: It would be useful to have a property on the platform window which indicated if the sub-class
  235. // supported the setParent. If not, then geometry would be in screen coordinates.
  236. void QPlatformWindow::setParent(const QPlatformWindow *parent)
  237. {
  238. Q_UNUSED(parent);
  239. qWarning("This plugin does not support setParent!");
  240. }
  241. /*!
  242. Reimplement to set the window title to \a title.
  243. The implementation might want to append the application display name to
  244. the window title, like Windows and Linux do.
  245. \sa QGuiApplication::applicationDisplayName()
  246. */
  247. void QPlatformWindow::setWindowTitle(const QString &title) { Q_UNUSED(title); }
  248. /*!
  249. Reimplement to set the window file path to \a filePath
  250. */
  251. void QPlatformWindow::setWindowFilePath(const QString &filePath) { Q_UNUSED(filePath); }
  252. /*!
  253. Reimplement to set the window icon to \a icon
  254. */
  255. void QPlatformWindow::setWindowIcon(const QIcon &icon) { Q_UNUSED(icon); }
  256. /*!
  257. Reimplement to be able to let Qt raise windows to the top of the desktop
  258. */
  259. void QPlatformWindow::raise() { qWarning("This plugin does not support raise()"); }
  260. /*!
  261. Reimplement to be able to let Qt lower windows to the bottom of the desktop
  262. */
  263. void QPlatformWindow::lower() { qWarning("This plugin does not support lower()"); }
  264. /*!
  265. Reimplement to propagate the size hints of the QWindow.
  266. The size hints include QWindow::minimumSize(), QWindow::maximumSize(),
  267. QWindow::sizeIncrement(), and QWindow::baseSize().
  268. */
  269. void QPlatformWindow::propagateSizeHints() {qWarning("This plugin does not support propagateSizeHints()"); }
  270. /*!
  271. Reimplement to be able to let Qt set the opacity level of a window
  272. */
  273. void QPlatformWindow::setOpacity(qreal level)
  274. {
  275. Q_UNUSED(level);
  276. qWarning("This plugin does not support setting window opacity");
  277. }
  278. /*!
  279. Reimplement to be able to let Qt set the mask of a window
  280. */
  281. void QPlatformWindow::setMask(const QRegion &region)
  282. {
  283. Q_UNUSED(region);
  284. qWarning("This plugin does not support setting window masks");
  285. }
  286. /*!
  287. Reimplement to let Qt be able to request activation/focus for a window
  288. Some window systems will probably not have callbacks for this functionality,
  289. and then calling QWindowSystemInterface::handleWindowActivated(QWindow *w)
  290. would be sufficient.
  291. If the window system has some event handling/callbacks then call
  292. QWindowSystemInterface::handleWindowActivated(QWindow *w) when the window system
  293. gives the notification.
  294. Default implementation calls QWindowSystem::handleWindowActivated(QWindow *w)
  295. */
  296. void QPlatformWindow::requestActivateWindow()
  297. {
  298. QWindowSystemInterface::handleWindowActivated(window());
  299. }
  300. /*!
  301. Handle changes to the orientation of the platform window's contents.
  302. This is a hint to the window manager in case it needs to display
  303. additional content like popups, dialogs, status bars, or similar
  304. in relation to the window.
  305. \sa QWindow::reportContentOrientationChange()
  306. */
  307. void QPlatformWindow::handleContentOrientationChange(Qt::ScreenOrientation orientation)
  308. {
  309. Q_UNUSED(orientation);
  310. }
  311. /*!
  312. Reimplement this function in subclass to return the device pixel ratio
  313. for the window. This is the ratio between physical pixels
  314. and device-independent pixels.
  315. \sa QPlatformWindow::devicePixelRatio();
  316. */
  317. qreal QPlatformWindow::devicePixelRatio() const
  318. {
  319. return 1.0;
  320. }
  321. bool QPlatformWindow::setKeyboardGrabEnabled(bool grab)
  322. {
  323. Q_UNUSED(grab);
  324. qWarning("This plugin does not support grabbing the keyboard");
  325. return false;
  326. }
  327. bool QPlatformWindow::setMouseGrabEnabled(bool grab)
  328. {
  329. Q_UNUSED(grab);
  330. qWarning("This plugin does not support grabbing the mouse");
  331. return false;
  332. }
  333. /*!
  334. Reimplement to be able to let Qt indicate that the window has been
  335. modified. Return true if the native window supports setting the modified
  336. flag, false otherwise.
  337. */
  338. bool QPlatformWindow::setWindowModified(bool modified)
  339. {
  340. Q_UNUSED(modified);
  341. return false;
  342. }
  343. /*!
  344. Reimplement this method to be able to do any platform specific event
  345. handling. All events for window() are passed to this function before being
  346. sent to QWindow::event().
  347. The default implementation is empty and does nothing with \a event.
  348. */
  349. void QPlatformWindow::windowEvent(QEvent *event)
  350. {
  351. Q_UNUSED(event);
  352. }
  353. /*!
  354. Reimplement this method to start a system size grip drag
  355. operation if the system supports it and return true to indicate
  356. success.
  357. It is called from the mouse press event handler of the size grip.
  358. The default implementation is empty and does nothing with \a pos
  359. and \a corner.
  360. */
  361. bool QPlatformWindow::startSystemResize(const QPoint &pos, Qt::Corner corner)
  362. {
  363. Q_UNUSED(pos)
  364. Q_UNUSED(corner)
  365. return false;
  366. }
  367. /*!
  368. Reimplement this method to set whether frame strut events
  369. should be sent to \a enabled.
  370. \sa frameStrutEventsEnabled
  371. */
  372. void QPlatformWindow::setFrameStrutEventsEnabled(bool enabled)
  373. {
  374. Q_UNUSED(enabled) // Do not warn as widgets enable it by default causing warnings with XCB.
  375. }
  376. /*!
  377. Reimplement this method to return whether
  378. frame strut events are enabled.
  379. */
  380. bool QPlatformWindow::frameStrutEventsEnabled() const
  381. {
  382. return false;
  383. }
  384. /*!
  385. Call this method to put together a window title composed of
  386. \a title
  387. \a separator
  388. the application display name
  389. If the display name isn't set, and the title is empty, the raw app name is used.
  390. */
  391. QString QPlatformWindow::formatWindowTitle(const QString &title, const QString &separator)
  392. {
  393. QString fullTitle = title;
  394. if (QGuiApplicationPrivate::displayName) {
  395. // Append display name, if set.
  396. if (!fullTitle.isEmpty())
  397. fullTitle += separator;
  398. fullTitle += *QGuiApplicationPrivate::displayName;
  399. } else if (fullTitle.isEmpty()) {
  400. // Don't let the window title be completely empty, use the app name as fallback.
  401. fullTitle = QCoreApplication::applicationName();
  402. }
  403. return fullTitle;
  404. }
  405. /*!
  406. Reimplement this method to set whether the window demands attention
  407. (for example, by flashing the taskbar icon) depending on \a enabled.
  408. \sa isAlertState()
  409. \since 5.1
  410. */
  411. void QPlatformWindow::setAlertState(bool enable)
  412. {
  413. Q_UNUSED(enable)
  414. }
  415. /*!
  416. Reimplement this method return whether the window is in
  417. an alert state.
  418. \sa setAlertState()
  419. \since 5.1
  420. */
  421. bool QPlatformWindow::isAlertState() const
  422. {
  423. return false;
  424. }
  425. // Return the effective screen for the initial geometry of a window. In a
  426. // multimonitor-setup, try to find the right screen by checking the transient
  427. // parent or the mouse cursor for parentless windows (cf QTBUG-34204,
  428. // QDialog::adjustPosition()).
  429. static inline const QScreen *effectiveScreen(const QWindow *window)
  430. {
  431. if (!window)
  432. return QGuiApplication::primaryScreen();
  433. const QScreen *screen = window->screen();
  434. if (!screen)
  435. return QGuiApplication::primaryScreen();
  436. const QList<QScreen *> siblings = screen->virtualSiblings();
  437. if (siblings.size() > 1) {
  438. const QPoint referencePoint = window->transientParent() ? window->transientParent()->geometry().center() : QCursor::pos();
  439. foreach (const QScreen *sibling, siblings)
  440. if (sibling->geometry().contains(referencePoint))
  441. return sibling;
  442. }
  443. return screen;
  444. }
  445. /*!
  446. Invalidates the window's surface by releasing its surface buffers.
  447. Many platforms do not support releasing the surface memory,
  448. and the default implementation does nothing.
  449. The platform window is expected to recreate the surface again if
  450. it is needed. For instance, if an OpenGL context is made current
  451. on this window.
  452. */
  453. void QPlatformWindow::invalidateSurface()
  454. {
  455. }
  456. /*!
  457. Helper function to get initial geometry on windowing systems which do not
  458. do smart positioning and also do not provide a means of centering a
  459. transient window w.r.t. its parent. For example this is useful on Windows
  460. and MacOS but not X11, because an X11 window manager typically tries to
  461. layout new windows to optimize usage of the available desktop space.
  462. However if the given window already has geometry which the application has
  463. initialized, it takes priority.
  464. */
  465. QRect QPlatformWindow::initialGeometry(const QWindow *w,
  466. const QRect &initialGeometry, int defaultWidth, int defaultHeight)
  467. {
  468. QRect rect(initialGeometry);
  469. if (rect.width() == 0) {
  470. const int minWidth = w->minimumWidth();
  471. rect.setWidth(minWidth > 0 ? minWidth : defaultWidth);
  472. }
  473. if (rect.height() == 0) {
  474. const int minHeight = w->minimumHeight();
  475. rect.setHeight(minHeight > 0 ? minHeight : defaultHeight);
  476. }
  477. if (w->isTopLevel() && qt_window_private(const_cast<QWindow*>(w))->positionAutomatic
  478. && w->type() != Qt::Popup) {
  479. if (const QScreen *screen = effectiveScreen(w)) {
  480. const QRect availableGeometry = screen->availableGeometry();
  481. // Center unless the geometry ( + unknown window frame) is too large for the screen).
  482. if (rect.height() < (availableGeometry.height() * 8) / 9
  483. && rect.width() < (availableGeometry.width() * 8) / 9) {
  484. const QWindow *tp = w->transientParent();
  485. if (tp) {
  486. // A transient window should be centered w.r.t. its transient parent.
  487. rect.moveCenter(tp->geometry().center());
  488. } else {
  489. // Center the window on the screen. (Only applicable on platforms
  490. // which do not provide a better way.)
  491. rect.moveCenter(availableGeometry.center());
  492. }
  493. }
  494. }
  495. }
  496. return rect;
  497. }
  498. /*!
  499. \class QPlatformWindow
  500. \since 4.8
  501. \internal
  502. \preliminary
  503. \ingroup qpa
  504. \brief The QPlatformWindow class provides an abstraction for top-level windows.
  505. The QPlatformWindow abstraction is used by QWindow for all its top level windows. It is being
  506. created by calling the createPlatformWindow function in the loaded QPlatformIntegration
  507. instance.
  508. QPlatformWindow is used to signal to the windowing system, how Qt perceives its frame.
  509. However, it is not concerned with how Qt renders into the window it represents.
  510. Visible QWindows will always have a QPlatformWindow. However, it is not necessary for
  511. all windows to have a QBackingStore. This is the case for QOpenGLWidget. And could be the case for
  512. windows where some 3.party renders into it.
  513. The platform specific window handle can be retrieved by the winId function.
  514. QPlatformWindow is also the way QPA defines how native child windows should be supported
  515. through the setParent function.
  516. \section1 Implementation Aspects
  517. \list 1
  518. \li Mouse grab: Qt expects windows to automatically grab the mouse if the user presses
  519. a button until the button is released.
  520. Automatic grab should be released if some window is explicitly grabbed.
  521. \li Enter/Leave events: If there is a window explicitly grabbing mouse events
  522. (\c{setMouseGrabEnabled()}), enter and leave events should only be sent to the
  523. grabbing window when mouse cursor passes over the grabbing window boundary.
  524. Other windows will not receive enter or leave events while the grab is active.
  525. While an automatic mouse grab caused by a mouse button press is active, no window
  526. will receive enter or leave events. When the last mouse button is released, the
  527. autograbbing window will receive leave event if mouse cursor is no longer within
  528. the window boundary.
  529. When any grab starts, the window under cursor will receive a leave event unless
  530. it is the grabbing window.
  531. When any grab ends, the window under cursor will receive an enter event unless it
  532. was the grabbing window.
  533. \li Window positioning: When calling \c{QWindow::setFramePosition()}, the flag
  534. \c{QWindowPrivate::positionPolicy} is set to \c{QWindowPrivate::WindowFrameInclusive}.
  535. This means the position includes the window frame, whose size is at this point
  536. unknown and the geometry's topleft point is the position of the window frame.
  537. \endlist
  538. Apart from the auto-tests (\c{tests/auto/gui/kernel/qwindow},
  539. \c{tests/auto/gui/kernel/qguiapplication} and \c{tests/auto/widgets/kernel/qwidget}),
  540. there are a number of manual tests and examples that can help testing a platform plugin:
  541. \list 1
  542. \li \c{examples/qpa/windows}: Basic \c{QWindow} creation.
  543. \li \c{examples/opengl/hellowindow}: Basic Open GL windows.
  544. \li \c{tests/manual/windowflags}: Tests setting the window flags.
  545. \li \c{tests/manual/windowgeometry} Tests setting the window geometry.
  546. \li \c{tests/manual/windowmodality} Tests setting the window modality.
  547. \li \c{tests/manual/widgetgrab} Tests mouse grab and dialogs.
  548. \endlist
  549. \sa QBackingStore, QWindow
  550. */
  551. QT_END_NAMESPACE