source: trunk/src/styles/qcdestyle.cpp@ 7

Last change on this file since 7 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 10.9 KB
Line 
1/****************************************************************************
2** $Id: qcdestyle.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of CDE-like style class
5**
6** Created : 981231
7**
8** Copyright (C) 1998-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the widgets module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qcdestyle.h"
39
40#if !defined(QT_NO_STYLE_CDE) || defined(QT_PLUGIN)
41
42#include "qpainter.h"
43#include "qdrawutil.h"
44#include "qbutton.h"
45#include <limits.h>
46
47/*!
48 \class QCDEStyle qcdestyle.h
49 \brief The QCDEStyle class provides a CDE look and feel.
50
51 \ingroup appearance
52
53 This style provides a slightly improved Motif look similar to some
54 versions of the Common Desktop Environment (CDE). The main
55 differences are thinner frames and more modern radio buttons and
56 checkboxes. Together with a dark background and a bright
57 text/foreground color, the style looks quite attractive (at least
58 for Motif fans).
59
60 Note that the functions provided by QCDEStyle are
61 reimplementations of QStyle functions; see QStyle for their
62 documentation.
63*/
64
65/*!
66 Constructs a QCDEStyle.
67
68 If \a useHighlightCols is FALSE (the default), then the style will
69 polish the application's color palette to emulate the Motif way of
70 highlighting, which is a simple inversion between the base and the
71 text color.
72*/
73QCDEStyle::QCDEStyle( bool useHighlightCols ) : QMotifStyle( useHighlightCols )
74{
75}
76
77/*!
78 Destroys the style.
79*/
80QCDEStyle::~QCDEStyle()
81{
82}
83
84
85/*!\reimp
86*/
87int QCDEStyle::pixelMetric( PixelMetric metric, const QWidget *widget ) const
88{
89 int ret;
90
91 switch( metric ) {
92 case PM_DefaultFrameWidth:
93 ret = 1;
94 break ;
95 case PM_MenuBarFrameWidth:
96 ret = 1;
97 break;
98 case PM_ScrollBarExtent:
99 ret = 13;
100 break;
101 default:
102 ret = QMotifStyle::pixelMetric( metric, widget );
103 break;
104 }
105 return ret;
106}
107
108/*! \reimp
109*/
110void QCDEStyle::drawControl( ControlElement element,
111 QPainter *p,
112 const QWidget *widget,
113 const QRect &r,
114 const QColorGroup &cg,
115 SFlags how,
116 const QStyleOption& opt ) const
117{
118
119 switch( element ) {
120 case CE_MenuBarItem:
121 {
122 if ( how & Style_Active ) // active item
123 qDrawShadePanel( p, r, cg, TRUE, 1,
124 &cg.brush( QColorGroup::Button ) );
125 else // other item
126 p->fillRect( r, cg.brush( QColorGroup::Button ) );
127 QCommonStyle::drawControl( element, p, widget, r, cg, how, opt );
128 break;
129 }
130 default:
131 QMotifStyle::drawControl( element, p, widget, r, cg, how, opt );
132 break;
133 }
134
135
136}
137
138/*! \reimp
139*/
140void QCDEStyle::drawPrimitive( PrimitiveElement pe,
141 QPainter *p,
142 const QRect &r,
143 const QColorGroup &cg,
144 SFlags flags,
145 const QStyleOption& opt ) const
146{
147 switch( pe ) {
148 case PE_Indicator: {
149#ifndef QT_NO_BUTTON
150 bool down = flags & Style_Down;
151 bool on = flags & Style_On;
152 bool showUp = !( down ^ on );
153 QBrush fill = showUp || flags & Style_NoChange ? cg.brush( QColorGroup::Button ) : cg.brush( QColorGroup::Mid );
154 qDrawShadePanel( p, r, cg, !showUp, pixelMetric( PM_DefaultFrameWidth ), &cg.brush( QColorGroup::Button ) );
155
156 if ( !( flags & Style_Off ) ) {
157 QPointArray a( 7 * 2 );
158 int i, xx, yy;
159 xx = r.x() + 3;
160 yy = r.y() + 5;
161 for ( i = 0; i < 3; i++ ) {
162 a.setPoint( 2 * i, xx, yy );
163 a.setPoint( 2 * i + 1, xx, yy + 2 );
164 xx++; yy++;
165 }
166 yy -= 2;
167 for ( i = 3; i < 7; i++ ) {
168 a.setPoint( 2 * i, xx, yy );
169 a.setPoint( 2 * i + 1, xx, yy + 2 );
170 xx++; yy--;
171 }
172 if ( flags & Style_NoChange )
173 p->setPen( cg.dark() );
174 else
175 p->setPen( cg.foreground() );
176 p->drawLineSegments( a );
177 }
178#endif
179 }
180 break;
181 case PE_ExclusiveIndicator:
182 {
183#define QCOORDARRLEN(x) sizeof(x)/(sizeof(QCOORD)*2)
184 static const QCOORD pts1[] = { // up left lines
185 1,9, 1,8, 0,7, 0,4, 1,3, 1,2, 2,1, 3,1, 4,0, 7,0, 8,1, 9,1 };
186 static const QCOORD pts4[] = { // bottom right lines
187 2,10, 3,10, 4,11, 7,11, 8,10, 9,10, 10,9, 10,8, 11,7,
188 11,4, 10,3, 10,2 };
189 static const QCOORD pts5[] = { // inner fill
190 4,2, 7,2, 9,4, 9,7, 7,9, 4,9, 2,7, 2,4 };
191 bool down = flags & Style_Down;
192 bool on = flags & Style_On;
193 p->eraseRect( r );
194 QPointArray a( QCOORDARRLEN(pts1), pts1 );
195 a.translate( r.x(), r.y() );
196 p->setPen( ( down || on ) ? cg.dark() : cg.light() );
197 p->drawPolyline( a );
198 a.setPoints( QCOORDARRLEN(pts4), pts4 );
199 a.translate( r.x(), r.y() );
200 p->setPen( ( down || on ) ? cg.light() : cg.dark() );
201 p->drawPolyline( a );
202 a.setPoints( QCOORDARRLEN(pts5), pts5 );
203 a.translate( r.x(), r.y() );
204 QColor fillColor = on ? cg.dark() : cg.background();
205 p->setPen( fillColor );
206 p->setBrush( on ? cg.brush( QColorGroup::Dark ) :
207 cg.brush( QColorGroup::Background ) );
208 p->drawPolygon( a );
209 break;
210 }
211
212 case PE_ExclusiveIndicatorMask:
213 {
214 static const QCOORD pts1[] = {
215 // up left lines
216 1,9, 1,8, 0,7, 0,4, 1,3, 1,2, 2,1, 3,1, 4,0, 7,0, 8,1, 9,1,
217 // bottom right lines
218 10,2, 10,3, 11,4, 11,7, 10,8, 10,9, 9,10, 8,10, 7,11, 4,11, 3,10, 2,10
219 };
220 QPointArray a(QCOORDARRLEN(pts1), pts1);
221 a.translate(r.x(), r.y());
222 p->setPen(color1);
223 p->setBrush(color1);
224 p->drawPolygon(a);
225 break;
226 }
227 case PE_ArrowUp:
228 case PE_ArrowDown:
229 case PE_ArrowRight:
230 case PE_ArrowLeft: {
231 QRect rect = r;
232 QPointArray bFill; // fill polygon
233 QPointArray bTop; // top shadow.
234 QPointArray bBot; // bottom shadow.
235 QPointArray bLeft; // left shadow.
236 QWMatrix matrix; // xform matrix
237 bool vertical = pe == PE_ArrowUp || pe == PE_ArrowDown;
238 bool horizontal = !vertical;
239 int dim = rect.width() < rect.height() ? rect.width() : rect.height();
240 int colspec = 0x0000; // color specification array
241
242 if ( dim < 2 ) // too small arrow
243 return;
244
245 // adjust size and center (to fix rotation below)
246 if ( rect.width() > dim ) {
247 rect.setX( rect.x() + ( ( rect.width() - dim ) / 2 ) );
248 rect.setWidth( dim );
249 }
250 if ( rect.height() > dim ) {
251 rect.setY( rect.y() + ( ( rect.height() - dim ) / 2 ) );
252 rect.setHeight( dim );
253 }
254
255 if ( dim > 3 ) {
256 bFill.resize( dim & 1 ? 3 : 4 );
257 bTop.resize( 2 );
258 bBot.resize( 2 );
259 bLeft.resize( 2 );
260 bLeft.putPoints( 0, 2, 0, 0, 0, dim-1 );
261 bTop.putPoints( 0, 2, 1, 0, dim-1, dim/2 );
262 bBot.putPoints( 0, 2, 1, dim-1, dim-1, dim/2 );
263
264 if ( dim > 6 ) { // dim>6: must fill interior
265 bFill.putPoints( 0, 2, 1, dim-1, 1, 1 );
266 if ( dim & 1 ) // if size is an odd number
267 bFill.setPoint( 2, dim - 2, dim / 2 );
268 else
269 bFill.putPoints( 2, 2, dim-2, dim/2-1, dim-2, dim/2 );
270 }
271 } else {
272 if ( dim == 3 ) { // 3x3 arrow pattern
273 bLeft.setPoints( 4, 0,0, 0,2, 1,1, 1,1 );
274 bTop .setPoints( 2, 1,0, 1,0 );
275 bBot .setPoints( 2, 1,2, 2,1 );
276 } else { // 2x2 arrow pattern
277 bLeft.setPoints( 2, 0,0, 0,1 );
278 bTop .setPoints( 2, 1,0, 1,0 );
279 bBot .setPoints( 2, 1,1, 1,1 );
280 }
281 }
282
283 if ( pe == PE_ArrowUp || pe == PE_ArrowLeft ) {
284 matrix.translate( rect.x(), rect.y() );
285 if ( vertical ) {
286 matrix.translate( 0, rect.height() - 1 );
287 matrix.rotate( -90 );
288 } else {
289 matrix.translate( rect.width() - 1, rect.height() - 1 );
290 matrix.rotate( 180 );
291 }
292 if ( flags & Style_Down )
293 colspec = horizontal ? 0x2334 : 0x2343;
294 else
295 colspec = horizontal ? 0x1443 : 0x1434;
296 } else if ( pe == PE_ArrowDown || pe == PE_ArrowRight ) {
297 matrix.translate( rect.x(), rect.y() );
298 if ( vertical ) {
299 matrix.translate( rect.width()-1, 0 );
300 matrix.rotate( 90 );
301 }
302 if ( flags & Style_Down )
303 colspec = horizontal ? 0x2443 : 0x2434;
304 else
305 colspec = horizontal ? 0x1334 : 0x1343;
306 }
307
308 QColor *cols[5];
309 if ( flags & Style_Enabled ) {
310 cols[0] = 0;
311 cols[1] = (QColor *)&cg.button();
312 cols[2] = (QColor *)&cg.mid();
313 cols[3] = (QColor *)&cg.light();
314 cols[4] = (QColor *)&cg.dark();
315 } else {
316 cols[0] = 0;
317 cols[1] = (QColor *)&cg.button();
318 cols[2] = (QColor *)&cg.button();
319 cols[3] = (QColor *)&cg.button();
320 cols[4] = (QColor *)&cg.button();
321 }
322
323#define CMID *cols[ (colspec>>12) & 0xf ]
324#define CLEFT *cols[ (colspec>>8) & 0xf ]
325#define CTOP *cols[ (colspec>>4) & 0xf ]
326#define CBOT *cols[ colspec & 0xf ]
327
328 QPen savePen = p->pen(); // save current pen
329 QBrush saveBrush = p->brush(); // save current brush
330 QWMatrix wxm = p->worldMatrix();
331 QPen pen( NoPen );
332 QBrush brush = cg.brush( flags & Style_Enabled ? QColorGroup::Button :
333 QColorGroup::Mid );
334
335 p->setPen( pen );
336 p->setBrush( brush );
337 p->setWorldMatrix( matrix, TRUE ); // set transformation matrix
338 p->drawPolygon( bFill ); // fill arrow
339 p->setBrush( NoBrush ); // don't fill
340
341 p->setPen( CLEFT );
342 p->drawLineSegments( bLeft );
343 p->setPen( CBOT );
344 p->drawLineSegments( bBot );
345 p->setPen( CTOP );
346 p->drawLineSegments( bTop );
347
348 p->setWorldMatrix( wxm );
349 p->setBrush( saveBrush ); // restore brush
350 p->setPen( savePen ); // restore pen
351
352#undef CMID
353#undef CLEFT
354#undef CTOP
355#undef CBOT
356
357 }
358 break;
359 default:
360 QMotifStyle::drawPrimitive( pe, p, r, cg, flags, opt );
361 }
362}
363
364#endif
Note: See TracBrowser for help on using the repository browser.