Qt 4.8
qdesktopwidget_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
42 /****************************************************************************
43 **
44 ** Copyright (c) 2007-2008, Apple, Inc.
45 **
46 ** All rights reserved.
47 **
48 ** Redistribution and use in source and binary forms, with or without
49 ** modification, are permitted provided that the following conditions are met:
50 **
51 ** * Redistributions of source code must retain the above copyright notice,
52 ** this list of conditions and the following disclaimer.
53 **
54 ** * Redistributions in binary form must reproduce the above copyright notice,
55 ** this list of conditions and the following disclaimer in the documentation
56 ** and/or other materials provided with the distribution.
57 **
58 ** * Neither the name of Apple, Inc. nor the names of its contributors
59 ** may be used to endorse or promote products derived from this software
60 ** without specific prior written permission.
61 **
62 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
66 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 **
74 ****************************************************************************/
75 
76 #import <Cocoa/Cocoa.h>
77 
78 #include "qapplication.h"
79 #include "qdesktopwidget.h"
80 #include <private/qt_mac_p.h>
81 #include "qwidget_p.h"
82 #include <private/qt_cocoa_helpers_mac_p.h>
83 #include <private/qdesktopwidget_mac_p.h>
84 
86 
88 
89 /*****************************************************************************
90  Externals
91  *****************************************************************************/
92 
93 /*****************************************************************************
94  QDesktopWidget member functions
95  *****************************************************************************/
96 
97 Q_GLOBAL_STATIC(QDesktopWidgetImplementation, qdesktopWidgetImplementation)
98 
100  : appScreen(0)
101 {
102  onResize();
103 }
104 
106 {
107 }
108 
110 {
111  return qdesktopWidgetImplementation();
112 }
113 
115 {
116  if (screenIndex < 0 || screenIndex >= screenCount)
117  screenIndex = appScreen;
118 
119  return availableRects[screenIndex].toRect();
120 }
121 
123 {
124  if (screenIndex < 0 || screenIndex >= screenCount)
125  screenIndex = appScreen;
126 
127  return screenRects[screenIndex].toRect();
128 }
129 
131 {
133  NSArray *displays = [NSScreen screens];
134  screenCount = [displays count];
135 
136  screenRects.clear();
138  NSRect primaryRect = [[displays objectAtIndex:0] frame];
139  for (int i = 0; i<screenCount; i++) {
140  NSRect r = [[displays objectAtIndex:i] frame];
141  int flippedY = - r.origin.y + // account for position offset and
142  primaryRect.size.height - r.size.height; // height difference.
143  screenRects.append(QRectF(r.origin.x, flippedY,
144  r.size.width, r.size.height));
145 
146  r = [[displays objectAtIndex:i] visibleFrame];
147  flippedY = - r.origin.y + // account for position offset and
148  primaryRect.size.height - r.size.height; // height difference.
149  availableRects.append(QRectF(r.origin.x, flippedY,
150  r.size.width, r.size.height));
151  }
152 }
153 
154 
155 
157  : QWidget(0, Qt::Desktop)
158 {
159  setObjectName(QLatin1String("desktop"));
161 }
162 
164 {
165 }
166 
168 {
169  return true;
170 }
171 
173 {
174  return qdesktopWidgetImplementation()->appScreen;
175 }
176 
178 {
179  return qdesktopWidgetImplementation()->screenCount;
180 }
181 
183 {
184  return this;
185 }
186 
188 {
189  return qdesktopWidgetImplementation()->availableRect(screen);
190 }
191 
193 {
194  return qdesktopWidgetImplementation()->screenRect(screen);
195 }
196 
198 {
199  QDesktopWidgetImplementation *d = qdesktopWidgetImplementation();
200  if (!widget)
201  return d->appScreen;
202  QRect frame = widget->frameGeometry();
203  if (!widget->isWindow())
204  frame.moveTopLeft(widget->mapToGlobal(QPoint(0,0)));
205  int maxSize = -1, maxScreen = -1;
206  for (int i = 0; i < d->screenCount; ++i) {
207  QRect rr = d->screenRect(i);
208  QRect sect = rr.intersected(frame);
209  int size = sect.width() * sect.height();
210  if (size > maxSize && sect.width() > 0 && sect.height() > 0) {
211  maxSize = size;
212  maxScreen = i;
213  }
214  }
215  return maxScreen;
216 }
217 
218 int QDesktopWidget::screenNumber(const QPoint &point) const
219 {
220  QDesktopWidgetImplementation *d = qdesktopWidgetImplementation();
221  int closestScreen = -1;
222  int shortestDistance = INT_MAX;
223  for (int i = 0; i < d->screenCount; ++i) {
224  QRect rr = d->screenRect(i);
225  int thisDistance = QWidgetPrivate::pointToRect(point, rr);
226  if (thisDistance < shortestDistance) {
227  shortestDistance = thisDistance;
228  closestScreen = i;
229  }
230  }
231  return closestScreen;
232 }
233 
235 {
236  QDesktopWidgetImplementation *d = qdesktopWidgetImplementation();
237 
238  const int oldScreenCount = d->screenCount;
239  const QVector<QRectF> oldRects(d->screenRects);
240  const QVector<QRectF> oldWorks(d->availableRects);
241 
242  d->onResize();
243 
244  for (int i = 0; i < qMin(oldScreenCount, d->screenCount); ++i) {
245  if (oldRects.at(i) != d->screenRects.at(i))
246  emit resized(i);
247  }
248  for (int i = 0; i < qMin(oldScreenCount, d->screenCount); ++i) {
249  if (oldWorks.at(i) != d->availableRects.at(i))
251  }
252 
253  if (oldScreenCount != d->screenCount)
255 }
256 
double d
Definition: qnumeric_p.h:62
void resizeEvent(QResizeEvent *e)
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QSize size() const
QPointer< QWidget > widget
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QRect frameGeometry
geometry of the widget relative to its parent including any window frame
Definition: qwidget.h:159
void workAreaResized(int)
int primaryScreen() const
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QRect intersected(const QRect &other) const
Returns the intersection of this rectangle and the given rectangle.
Definition: qrect.h:481
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
static QDesktopWidgetImplementation * instance()
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
static int pointToRect(const QPoint &p, const QRect &r)
Definition: qwidget.cpp:7128
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QRect screenRect(int screenIndex) const
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
#define emit
Definition: qobjectdefs.h:76
bool isVirtualDesktop() const
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
void resized(int)
int numScreens() const
void moveTopLeft(const QPoint &p)
Moves the rectangle, leaving the top-left corner at the given position.
Definition: qrect.h:368
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QWidget * screen(int screen=-1)
const QRect screenGeometry(int screen=-1) const
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void screenCountChanged(int)
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
int screenNumber(const QWidget *widget=0) const
const QRect availableGeometry(int screen=-1) const
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.
#define INT_MAX
QRect availableRect(int screenIndex) const