[2] | 1 | /****************************************************************************
|
---|
| 2 | ** $Id: qcompactstyle.cpp 8 2005-11-16 19:36:46Z 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();
|
---|
[8] | 154 |
|
---|
| 155 | // QPopupMenu has WResizeNoErase and WRepaintNoErase flags, so we
|
---|
| 156 | // must erase areas not covered by menu items (this is requested by
|
---|
| 157 | // QPopupMenu using 0 as the menu item argument).
|
---|
| 158 | // [Win32 version feels ok without this, because it doesn't actually
|
---|
| 159 | // fully obey WResizeNoErase and WRepaintNoErase: WM_ERASEBKGND always
|
---|
| 160 | // erases the background before WM_PAINT and after every resize].
|
---|
| 161 | #if !defined (Q_WS_PM)
|
---|
[2] | 162 | if ( !mi )
|
---|
| 163 | break;
|
---|
[8] | 164 | #endif
|
---|
[2] | 165 |
|
---|
| 166 | int tab = opt.tabWidth();
|
---|
| 167 | int maxpmw = opt.maxIconWidth();
|
---|
| 168 | bool dis = !(flags & Style_Enabled);
|
---|
| 169 | bool checkable = popupmenu->isCheckable();
|
---|
| 170 | bool act = flags & Style_Active;
|
---|
| 171 | int x, y, w, h;
|
---|
| 172 | r.rect( &x, &y, &w, &h );
|
---|
| 173 |
|
---|
| 174 | QColorGroup itemg = g;
|
---|
| 175 |
|
---|
| 176 | if ( checkable )
|
---|
| 177 | maxpmw = QMAX( maxpmw, 8 ); // space for the checkmarks
|
---|
| 178 |
|
---|
| 179 | int checkcol = maxpmw;
|
---|
| 180 |
|
---|
| 181 | if ( mi && mi->isSeparator() ) { // draw separator
|
---|
| 182 | p->setPen( g.dark() );
|
---|
| 183 | p->drawLine( x, y, x+w, y );
|
---|
| 184 | p->setPen( g.light() );
|
---|
| 185 | p->drawLine( x, y+1, x+w, y+1 );
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | QBrush fill = act? g.brush( QColorGroup::Highlight ) :
|
---|
| 190 | g.brush( QColorGroup::Button );
|
---|
| 191 | p->fillRect( x, y, w, h, fill);
|
---|
| 192 |
|
---|
| 193 | if ( !mi )
|
---|
| 194 | return;
|
---|
| 195 |
|
---|
| 196 | if ( mi->isChecked() ) {
|
---|
| 197 | if ( act && !dis ) {
|
---|
| 198 | qDrawShadePanel( p, x, y, checkcol, h,
|
---|
| 199 | g, TRUE, 1, &g.brush( QColorGroup::Button ) );
|
---|
| 200 | } else {
|
---|
| 201 | qDrawShadePanel( p, x, y, checkcol, h,
|
---|
| 202 | g, TRUE, 1, &g.brush( QColorGroup::Midlight ) );
|
---|
| 203 | }
|
---|
| 204 | } else if ( !act ) {
|
---|
| 205 | p->fillRect(x, y, checkcol , h,
|
---|
| 206 | g.brush( QColorGroup::Button ));
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | if ( mi->iconSet() ) { // draw iconset
|
---|
| 210 | QIconSet::Mode mode = dis ? QIconSet::Disabled : QIconSet::Normal;
|
---|
| 211 | if (act && !dis )
|
---|
| 212 | mode = QIconSet::Active;
|
---|
| 213 | QPixmap pixmap;
|
---|
| 214 | if ( checkable && mi->isChecked() )
|
---|
| 215 | pixmap = mi->iconSet()->pixmap( QIconSet::Small, mode, QIconSet::On );
|
---|
| 216 | else
|
---|
| 217 | pixmap = mi->iconSet()->pixmap( QIconSet::Small, mode );
|
---|
| 218 | int pixw = pixmap.width();
|
---|
| 219 | int pixh = pixmap.height();
|
---|
| 220 | if ( act && !dis ) {
|
---|
| 221 | if ( !mi->isChecked() )
|
---|
| 222 | qDrawShadePanel( p, x, y, checkcol, h, g, FALSE, 1, &g.brush( QColorGroup::Button ) );
|
---|
| 223 | }
|
---|
| 224 | QRect cr( x, y, checkcol, h );
|
---|
| 225 | QRect pmr( 0, 0, pixw, pixh );
|
---|
| 226 | pmr.moveCenter( cr.center() );
|
---|
| 227 | p->setPen( itemg.text() );
|
---|
| 228 | p->drawPixmap( pmr.topLeft(), pixmap );
|
---|
| 229 |
|
---|
| 230 | QBrush fill = act? g.brush( QColorGroup::Highlight ) :
|
---|
| 231 | g.brush( QColorGroup::Button );
|
---|
| 232 | p->fillRect( x+checkcol + 1, y, w - checkcol - 1, h, fill);
|
---|
| 233 | } else if ( checkable ) { // just "checking"...
|
---|
| 234 | int mw = checkcol + motifItemFrame;
|
---|
| 235 | int mh = h - 2*motifItemFrame;
|
---|
| 236 | if ( mi->isChecked() ) {
|
---|
| 237 |
|
---|
| 238 | SFlags cflags = Style_Default;
|
---|
| 239 | if (! dis)
|
---|
| 240 | cflags |= Style_Enabled;
|
---|
| 241 | if (act)
|
---|
| 242 | cflags |= Style_On;
|
---|
| 243 |
|
---|
| 244 | drawPrimitive( PE_CheckMark, p, QRect(x + motifItemFrame + 2, y + motifItemFrame,
|
---|
| 245 | mw, mh), itemg, cflags, opt );
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | p->setPen( act ? g.highlightedText() : g.buttonText() );
|
---|
| 250 |
|
---|
| 251 | QColor discol;
|
---|
| 252 | if ( dis ) {
|
---|
| 253 | discol = itemg.text();
|
---|
| 254 | p->setPen( discol );
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | int xm = motifItemFrame + checkcol + motifItemHMargin;
|
---|
| 258 |
|
---|
| 259 | if ( mi->custom() ) {
|
---|
| 260 | int m = motifItemVMargin;
|
---|
| 261 | p->save();
|
---|
| 262 | if ( dis && !act ) {
|
---|
| 263 | p->setPen( g.light() );
|
---|
| 264 | mi->custom()->paint( p, itemg, act, !dis,
|
---|
| 265 | x+xm+1, y+m+1, w-xm-tab+1, h-2*m );
|
---|
| 266 | p->setPen( discol );
|
---|
| 267 | }
|
---|
| 268 | mi->custom()->paint( p, itemg, act, !dis,
|
---|
| 269 | x+xm, y+m, w-xm-tab+1, h-2*m );
|
---|
| 270 | p->restore();
|
---|
| 271 | }
|
---|
| 272 | QString s = mi->text();
|
---|
| 273 | if ( !s.isNull() ) { // draw text
|
---|
| 274 | int t = s.find( '\t' );
|
---|
| 275 | int m = motifItemVMargin;
|
---|
| 276 | const int text_flags = AlignVCenter|ShowPrefix | DontClip | SingleLine;
|
---|
| 277 | if ( t >= 0 ) { // draw tab text
|
---|
| 278 | if ( dis && !act ) {
|
---|
| 279 | p->setPen( g.light() );
|
---|
| 280 | p->drawText( x+w-tab-windowsRightBorder-motifItemHMargin-motifItemFrame+1,
|
---|
| 281 | y+m+1, tab, h-2*m, text_flags, s.mid( t+1 ));
|
---|
| 282 | p->setPen( discol );
|
---|
| 283 | }
|
---|
| 284 | p->drawText( x+w-tab-windowsRightBorder-motifItemHMargin-motifItemFrame,
|
---|
| 285 | y+m, tab, h-2*m, text_flags, s.mid( t+1 ) );
|
---|
| 286 | s = s.left( t );
|
---|
| 287 | }
|
---|
| 288 | if ( dis && !act ) {
|
---|
| 289 | p->setPen( g.light() );
|
---|
| 290 | p->drawText( x+xm+1, y+m+1, w-xm+1, h-2*m, text_flags, s, t );
|
---|
| 291 | p->setPen( discol );
|
---|
| 292 | }
|
---|
| 293 | p->drawText( x+xm, y+m, w-xm-tab+1, h-2*m, text_flags, s, t );
|
---|
| 294 | } else if ( mi->pixmap() ) { // draw pixmap
|
---|
| 295 | QPixmap *pixmap = mi->pixmap();
|
---|
| 296 | if ( pixmap->depth() == 1 )
|
---|
| 297 | p->setBackgroundMode( OpaqueMode );
|
---|
| 298 | p->drawPixmap( x+xm, y+motifItemFrame, *pixmap );
|
---|
| 299 | if ( pixmap->depth() == 1 )
|
---|
| 300 | p->setBackgroundMode( TransparentMode );
|
---|
| 301 | }
|
---|
| 302 | if ( mi->popup() ) { // draw sub menu arrow
|
---|
| 303 | int dim = (h-2*motifItemFrame) / 2;
|
---|
| 304 | if ( act ) {
|
---|
| 305 | if ( !dis )
|
---|
| 306 | discol = white;
|
---|
| 307 | QColorGroup g2( discol, g.highlight(),
|
---|
| 308 | white, white,
|
---|
| 309 | dis ? discol : white,
|
---|
| 310 | discol, white );
|
---|
| 311 | drawPrimitive(PE_ArrowRight, p, QRect(x+w - motifArrowHMargin - motifItemFrame - dim, y + h / 2 - dim / 2, dim, dim),
|
---|
| 312 | g2, Style_Enabled);
|
---|
| 313 | } else {
|
---|
| 314 | drawPrimitive(PE_ArrowRight, p, QRect(x+w - motifArrowHMargin - motifItemFrame - dim, y + h / 2 - dim / 2, dim, dim),
|
---|
| 315 | g, !dis ? Style_Enabled : Style_Default);
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | break;
|
---|
| 320 |
|
---|
| 321 | default:
|
---|
| 322 | QWindowsStyle::drawControl( element, p, widget, r, g, flags, opt );
|
---|
| 323 | break;
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | #endif
|
---|