Qt 4.8
qregion_win.cpp
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 #include "qatomic.h"
43 #include "qbitmap.h"
44 #include "qbuffer.h"
45 #include "qimage.h"
46 #include "qpolygon.h"
47 #include "qregion.h"
48 #include "qt_windows.h"
49 #include "qpainterpath.h"
50 
52 
54 
55 HRGN qt_tryCreateRegion(QRegion::RegionType type, int left, int top, int right, int bottom)
56 {
57  const int tries = 10;
58  for (int i = 0; i < tries; ++i) {
59  HRGN region = 0;
60  switch (type) {
61  case QRegion::Rectangle:
62  region = CreateRectRgn(left, top, right, bottom);
63  break;
64  case QRegion::Ellipse:
65 #ifndef Q_OS_WINCE
66  region = CreateEllipticRgn(left, top, right, bottom);
67 #endif
68  break;
69  }
70  if (region) {
71  if (GetRegionData(region, 0, 0))
72  return region;
73  else
74  DeleteObject(region);
75  }
76  }
77  return 0;
78 }
79 
81 {
82  int numBytes = GetRegionData(rgn, 0, 0);
83  if (numBytes == 0)
84  return QRegion();
85 
86  char *buf = new char[numBytes];
87  if (buf == 0)
88  return QRegion();
89 
90  RGNDATA *rd = reinterpret_cast<RGNDATA*>(buf);
91  if (GetRegionData(rgn, numBytes, rd) == 0) {
92  delete [] buf;
93  return QRegion();
94  }
95 
96  QRegion region;
97  RECT *r = reinterpret_cast<RECT*>(rd->Buffer);
98  for (uint i = 0; i < rd->rdh.nCount; ++i) {
99  QRect rect;
100  rect.setCoords(r->left, r->top, r->right - 1, r->bottom - 1);
101  ++r;
102  region |= rect;
103  }
104 
105  delete [] buf;
106 
107  return region;
108 }
109 
110 void qt_win_dispose_rgn(HRGN r)
111 {
112  if (r)
113  DeleteObject(r);
114 }
115 
116 static void qt_add_rect(HRGN &winRegion, QRect r)
117 {
118  HRGN rgn = CreateRectRgn(r.left(), r.top(), r.x() + r.width(), r.y() + r.height());
119  if (rgn) {
120  HRGN dest = CreateRectRgn(0,0,0,0);
121  int result = CombineRgn(dest, winRegion, rgn, RGN_OR);
122  if (result) {
123  DeleteObject(winRegion);
124  winRegion = dest;
125  }
126  DeleteObject(rgn);
127  }
128 }
129 
131 {
132  if (d->rgn)
133  DeleteObject(d->rgn);
134  d->rgn = CreateRectRgn(0,0,0,0);
135  if (d->qt_rgn) {
136  if (d->qt_rgn->numRects == 1) {
137  QRect r = d->qt_rgn->extents;
138  qt_add_rect(d->rgn, r);
139  return;
140  }
141  for (int i = 0;i < d->qt_rgn->numRects;i++) {
142  QRect r = d->qt_rgn->rects.at(i);
143  qt_add_rect(d->rgn, r);
144  }
145  }
146 }
147 
148 
HRGN qt_tryCreateRegion(QRegion::RegionType type, int left, int top, int right, int bottom)
Definition: qregion_win.cpp:55
void qt_win_dispose_rgn(HRGN r)
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
RegionType
Specifies the shape of the region to be created.
Definition: qregion.h:71
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
QRegionPrivate * qt_rgn
Definition: qregion.h:211
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218
static struct QRegionData shared_empty
Definition: qregion.h:218
QTextStream & right(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignRight) on stream and returns stream...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void ensureHandle() const
unsigned int uint
Definition: qglobal.h:996
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
void setCoords(int x1, int y1, int x2, int y2)
Sets the coordinates of the rectangle&#39;s top-left corner to (x1, y1), and the coordinates of its botto...
Definition: qrect.h:416
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< QRect > rects
Definition: qregion.cpp:1227
static void qt_add_rect(HRGN &winRegion, QRect r)
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QRegion qt_region_from_HRGN(HRGN rgn)
Definition: qregion_win.cpp:80
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
struct QRegionData * d
Definition: qregion.h:217
QTextStream & left(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignLeft) on stream and returns stream...