1 | /****************************************************************************
|
---|
2 | ** $Id: qcompactstyle.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of compact style class
|
---|
5 | **
|
---|
6 | ** Created : 006231
|
---|
7 | **
|
---|
8 | ** Copyright (C) 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 "qcompactstyle.h"
|
---|
39 |
|
---|
40 | #if !defined(QT_NO_STYLE_COMPACT) || defined(QT_PLUGIN)
|
---|
41 |
|
---|
42 | #include "qfontmetrics.h"
|
---|
43 | #include "qpalette.h"
|
---|
44 | #include "qpainter.h"
|
---|
45 | #include "qdrawutil.h"
|
---|
46 | #include "qmenudata.h"
|
---|
47 | #include "qpopupmenu.h"
|
---|
48 |
|
---|
49 | QCompactStyle::QCompactStyle()
|
---|
50 | : QWindowsStyle()
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | /*! \reimp */
|
---|
55 | int QCompactStyle::pixelMetric( PixelMetric metric, const QWidget *widget )
|
---|
56 | {
|
---|
57 | int ret;
|
---|
58 | switch ( metric ) {
|
---|
59 | case PM_ButtonMargin:
|
---|
60 | ret = 2;
|
---|
61 | break;
|
---|
62 | // tws - I added this in to stop this "Windows Scroll behaivor." Remove it
|
---|
63 | // if you don't want it.
|
---|
64 | case PM_MaximumDragDistance:
|
---|
65 | ret = -1;
|
---|
66 | break;
|
---|
67 | default:
|
---|
68 | ret = QWindowsStyle::pixelMetric( metric, widget );
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | return ret;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static const int motifItemFrame = 0; // menu item frame width
|
---|
75 | static const int motifSepHeight = 2; // separator item height
|
---|
76 | static const int motifItemHMargin = 1; // menu item hor text margin
|
---|
77 | static const int motifItemVMargin = 2; // menu item ver text margin
|
---|
78 | static const int motifArrowHMargin = 0; // arrow horizontal margin
|
---|
79 | static const int motifTabSpacing = 4; // space between text and tab
|
---|
80 | static const int motifCheckMarkHMargin = 1; // horiz. margins of check mark
|
---|
81 | static const int windowsRightBorder = 8; // right border on windows
|
---|
82 | static const int windowsCheckMarkWidth = 2; // checkmarks width on windows
|
---|
83 |
|
---|
84 | static int extraPopupMenuItemWidth( bool checkable, int maxpmw, QMenuItem* mi, const QFontMetrics& /*fm*/ )
|
---|
85 | {
|
---|
86 | int w = 2*motifItemHMargin + 2*motifItemFrame; // a little bit of border can never harm
|
---|
87 |
|
---|
88 | if ( mi->isSeparator() )
|
---|
89 | return 10; // arbitrary
|
---|
90 | else if ( mi->pixmap() )
|
---|
91 | w += mi->pixmap()->width(); // pixmap only
|
---|
92 |
|
---|
93 | if ( !mi->text().isNull() ) {
|
---|
94 | if ( mi->text().find('\t') >= 0 ) // string contains tab
|
---|
95 | w += motifTabSpacing;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if ( maxpmw ) { // we have iconsets
|
---|
99 | w += maxpmw;
|
---|
100 | w += 6; // add a little extra border around the iconset
|
---|
101 | }
|
---|
102 |
|
---|
103 | if ( checkable && maxpmw < windowsCheckMarkWidth ) {
|
---|
104 | w += windowsCheckMarkWidth - maxpmw; // space for the checkmarks
|
---|
105 | }
|
---|
106 |
|
---|
107 | if ( maxpmw > 0 || checkable ) // we have a check-column ( iconsets or checkmarks)
|
---|
108 | w += motifCheckMarkHMargin; // add space to separate the columns
|
---|
109 |
|
---|
110 | w += windowsRightBorder; // windows has a strange wide border on the right side
|
---|
111 |
|
---|
112 | return w;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static int popupMenuItemHeight( bool /*checkable*/, QMenuItem* mi, const QFontMetrics& fm )
|
---|
116 | {
|
---|
117 | int h = 0;
|
---|
118 | if ( mi->isSeparator() ) // separator height
|
---|
119 | h = motifSepHeight;
|
---|
120 | else if ( mi->pixmap() ) // pixmap height
|
---|
121 | h = mi->pixmap()->height() + 2*motifItemFrame;
|
---|
122 | else // text height
|
---|
123 | h = fm.height() + 2*motifItemVMargin + 2*motifItemFrame - 1;
|
---|
124 |
|
---|
125 | if ( !mi->isSeparator() && mi->iconSet() != 0 ) {
|
---|
126 | h = QMAX( h, mi->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).height() + 2*motifItemFrame );
|
---|
127 | }
|
---|
128 | if ( mi->custom() )
|
---|
129 | h = QMAX( h, mi->custom()->sizeHint().height() + 2*motifItemVMargin + 2*motifItemFrame ) - 1;
|
---|
130 | return h;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void drawPopupMenuItem( QPainter* p, bool checkable,
|
---|
134 | int maxpmw, int tab, QMenuItem* mi,
|
---|
135 | const QPalette& pal, bool act,
|
---|
136 | bool enabled,
|
---|
137 | int x, int y, int w, int h)
|
---|
138 | {
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*! \reimp */
|
---|
143 | void QCompactStyle::drawControl( ControlElement element, QPainter *p, const QWidget *widget, const QRect &r,
|
---|
144 | const QColorGroup &g, SFlags flags, const QStyleOption& opt )
|
---|
145 | {
|
---|
146 | switch ( element ) {
|
---|
147 | case CE_PopupMenuItem:
|
---|
148 | {
|
---|
149 | if (! widget || opt.isDefault())
|
---|
150 | break;
|
---|
151 |
|
---|
152 | const QPopupMenu *popupmenu = (const QPopupMenu *) widget;
|
---|
153 | QMenuItem *mi = opt.menuItem();
|
---|
154 | if ( !mi )
|
---|
155 | break;
|
---|
156 |
|
---|
157 | int tab = opt.tabWidth();
|
---|
158 | int maxpmw = opt.maxIconWidth();
|
---|
159 | bool dis = !(flags & Style_Enabled);
|
---|
160 | bool checkable = popupmenu->isCheckable();
|
---|
161 | bool act = flags & Style_Active;
|
---|
162 | int x, y, w, h;
|
---|
163 | r.rect( &x, &y, &w, &h );
|
---|
164 |
|
---|
165 | QColorGroup itemg = g;
|
---|
166 |
|
---|
167 | if ( checkable )
|
---|
168 | maxpmw = QMAX( maxpmw, 8 ); // space for the checkmarks
|
---|
169 |
|
---|
170 | int checkcol = maxpmw;
|
---|
171 |
|
---|
172 | if ( mi && mi->isSeparator() ) { // draw separator
|
---|
173 | p->setPen( g.dark() );
|
---|
174 | p->drawLine( x, y, x+w, y );
|
---|
175 | p->setPen( g.light() );
|
---|
176 | p->drawLine( x, y+1, x+w, y+1 );
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | QBrush fill = act? g.brush( QColorGroup::Highlight ) :
|
---|
181 | g.brush( QColorGroup::Button );
|
---|
182 | p->fillRect( x, y, w, h, fill);
|
---|
183 |
|
---|
184 | if ( !mi )
|
---|
185 | return;
|
---|
186 |
|
---|
187 | if ( mi->isChecked() ) {
|
---|
188 | if ( act && !dis ) {
|
---|
189 | qDrawShadePanel( p, x, y, checkcol, h,
|
---|
190 | g, TRUE, 1, &g.brush( QColorGroup::Button ) );
|
---|
191 | } else {
|
---|
192 | qDrawShadePanel( p, x, y, checkcol, h,
|
---|
193 | g, TRUE, 1, &g.brush( QColorGroup::Midlight ) );
|
---|
194 | }
|
---|
195 | } else if ( !act ) {
|
---|
196 | p->fillRect(x, y, checkcol , h,
|
---|
197 | g.brush( QColorGroup::Button ));
|
---|
198 | }
|
---|
199 |
|
---|
200 | if ( mi->iconSet() ) { // draw iconset
|
---|
201 | QIconSet::Mode mode = dis ? QIconSet::Disabled : QIconSet::Normal;
|
---|
202 | if (act && !dis )
|
---|
203 | mode = QIconSet::Active;
|
---|
204 | QPixmap pixmap;
|
---|
205 | if ( checkable && mi->isChecked() )
|
---|
206 | pixmap = mi->iconSet()->pixmap( QIconSet::Small, mode, QIconSet::On );
|
---|
207 | else
|
---|
208 | pixmap = mi->iconSet()->pixmap( QIconSet::Small, mode );
|
---|
209 | int pixw = pixmap.width();
|
---|
210 | int pixh = pixmap.height();
|
---|
211 | if ( act && !dis ) {
|
---|
212 | if ( !mi->isChecked() )
|
---|
213 | qDrawShadePanel( p, x, y, checkcol, h, g, FALSE, 1, &g.brush( QColorGroup::Button ) );
|
---|
214 | }
|
---|
215 | QRect cr( x, y, checkcol, h );
|
---|
216 | QRect pmr( 0, 0, pixw, pixh );
|
---|
217 | pmr.moveCenter( cr.center() );
|
---|
218 | p->setPen( itemg.text() );
|
---|
219 | p->drawPixmap( pmr.topLeft(), pixmap );
|
---|
220 |
|
---|
221 | QBrush fill = act? g.brush( QColorGroup::Highlight ) :
|
---|
222 | g.brush( QColorGroup::Button );
|
---|
223 | p->fillRect( x+checkcol + 1, y, w - checkcol - 1, h, fill);
|
---|
224 | } else if ( checkable ) { // just "checking"...
|
---|
225 | int mw = checkcol + motifItemFrame;
|
---|
226 | int mh = h - 2*motifItemFrame;
|
---|
227 | if ( mi->isChecked() ) {
|
---|
228 |
|
---|
229 | SFlags cflags = Style_Default;
|
---|
230 | if (! dis)
|
---|
231 | cflags |= Style_Enabled;
|
---|
232 | if (act)
|
---|
233 | cflags |= Style_On;
|
---|
234 |
|
---|
235 | drawPrimitive( PE_CheckMark, p, QRect(x + motifItemFrame + 2, y + motifItemFrame,
|
---|
236 | mw, mh), itemg, cflags, opt );
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | p->setPen( act ? g.highlightedText() : g.buttonText() );
|
---|
241 |
|
---|
242 | QColor discol;
|
---|
243 | if ( dis ) {
|
---|
244 | discol = itemg.text();
|
---|
245 | p->setPen( discol );
|
---|
246 | }
|
---|
247 |
|
---|
248 | int xm = motifItemFrame + checkcol + motifItemHMargin;
|
---|
249 |
|
---|
250 | if ( mi->custom() ) {
|
---|
251 | int m = motifItemVMargin;
|
---|
252 | p->save();
|
---|
253 | if ( dis && !act ) {
|
---|
254 | p->setPen( g.light() );
|
---|
255 | mi->custom()->paint( p, itemg, act, !dis,
|
---|
256 | x+xm+1, y+m+1, w-xm-tab+1, h-2*m );
|
---|
257 | p->setPen( discol );
|
---|
258 | }
|
---|
259 | mi->custom()->paint( p, itemg, act, !dis,
|
---|
260 | x+xm, y+m, w-xm-tab+1, h-2*m );
|
---|
261 | p->restore();
|
---|
262 | }
|
---|
263 | QString s = mi->text();
|
---|
264 | if ( !s.isNull() ) { // draw text
|
---|
265 | int t = s.find( '\t' );
|
---|
266 | int m = motifItemVMargin;
|
---|
267 | const int text_flags = AlignVCenter|ShowPrefix | DontClip | SingleLine;
|
---|
268 | if ( t >= 0 ) { // draw tab text
|
---|
269 | if ( dis && !act ) {
|
---|
270 | p->setPen( g.light() );
|
---|
271 | p->drawText( x+w-tab-windowsRightBorder-motifItemHMargin-motifItemFrame+1,
|
---|
272 | y+m+1, tab, h-2*m, text_flags, s.mid( t+1 ));
|
---|
273 | p->setPen( discol );
|
---|
274 | }
|
---|
275 | p->drawText( x+w-tab-windowsRightBorder-motifItemHMargin-motifItemFrame,
|
---|
276 | y+m, tab, h-2*m, text_flags, s.mid( t+1 ) );
|
---|
277 | s = s.left( t );
|
---|
278 | }
|
---|
279 | if ( dis && !act ) {
|
---|
280 | p->setPen( g.light() );
|
---|
281 | p->drawText( x+xm+1, y+m+1, w-xm+1, h-2*m, text_flags, s, t );
|
---|
282 | p->setPen( discol );
|
---|
283 | }
|
---|
284 | p->drawText( x+xm, y+m, w-xm-tab+1, h-2*m, text_flags, s, t );
|
---|
285 | } else if ( mi->pixmap() ) { // draw pixmap
|
---|
286 | QPixmap *pixmap = mi->pixmap();
|
---|
287 | if ( pixmap->depth() == 1 )
|
---|
288 | p->setBackgroundMode( OpaqueMode );
|
---|
289 | p->drawPixmap( x+xm, y+motifItemFrame, *pixmap );
|
---|
290 | if ( pixmap->depth() == 1 )
|
---|
291 | p->setBackgroundMode( TransparentMode );
|
---|
292 | }
|
---|
293 | if ( mi->popup() ) { // draw sub menu arrow
|
---|
294 | int dim = (h-2*motifItemFrame) / 2;
|
---|
295 | if ( act ) {
|
---|
296 | if ( !dis )
|
---|
297 | discol = white;
|
---|
298 | QColorGroup g2( discol, g.highlight(),
|
---|
299 | white, white,
|
---|
300 | dis ? discol : white,
|
---|
301 | discol, white );
|
---|
302 | drawPrimitive(PE_ArrowRight, p, QRect(x+w - motifArrowHMargin - motifItemFrame - dim, y + h / 2 - dim / 2, dim, dim),
|
---|
303 | g2, Style_Enabled);
|
---|
304 | } else {
|
---|
305 | drawPrimitive(PE_ArrowRight, p, QRect(x+w - motifArrowHMargin - motifItemFrame - dim, y + h / 2 - dim / 2, dim, dim),
|
---|
306 | g, !dis ? Style_Enabled : Style_Default);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
310 | break;
|
---|
311 |
|
---|
312 | default:
|
---|
313 | QWindowsStyle::drawControl( element, p, widget, r, g, flags, opt );
|
---|
314 | break;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | #endif
|
---|