#include "baghira.h"
#include "bitmaps.h"
#include "embeddata.h" 

#include <qapplication.h>
#include <qbitmap.h>
#include <qbrush.h>
#include <qbutton.h>
#include <qcleanuphandler.h>
#include <qcolor.h>
#include <qcombobox.h>
#include <qevent.h> 
#include <qfont.h>
#include <qframe.h>
#include <qgroupbox.h>
#include <qheader.h>
#include <qiconset.h>
#include <qimage.h>
//#include <qlayout.h> 
#include <qlistbox.h>
#include <qlistview.h>
#include <qmenubar.h>
#include <qobject.h>
#include <qobjectlist.h>
#include <qpaintdevice.h>
#include <qpainter.h>
#include <qpalette.h>
#include <qpen.h>
#include <qpoint.h>
#include <qpopupmenu.h>
#include <qpushbutton.h>
//#include <qrangecontrol.h>
#include <qrect.h>
#include <qscrollbar.h>
#include <qscrollview.h>
#include <qsize.h>
#include <qslider.h>
#include <qstring.h>
#include <qstyleplugin.h>
#include <qtabbar.h>
#include <qtabwidget.h>
#include <ktoolbar.h>
#include <ktoolbarbutton.h>
#include <kwordwrap.h> 

static const int windowsItemFrame	= 1; // menu item frame width
static const int windowsSepHeight	= 2; // separator item height
static const int windowsItemHMargin	= 3; // menu item hor text margin
static const int windowsItemVMargin	= 1; // menu item ver text margin
static const int windowsArrowHMargin	= 6; // arrow horizontal margin
static const int windowsTabSpacing	= 12; // space between text and tab
static const int windowsCheckMarkHMargin	= 2; // horiz. margins of check mark
static const int windowsRightBorder	= 12; // right border on windows
static const int windowsCheckMarkWidth	= 12; // checkmarks width on windows

// options
bool OptionHandler::stippleMenu;
int OptionHandler::menuTranslucency;
int OptionHandler::opacity;//int customMenuOpacity;
bool OptionHandler::menuColorButton;
bool OptionHandler::menuDropShadows;
bool OptionHandler::shadowText;
//color
QColor OptionHandler::customButtonColor;
//widgets
bool OptionHandler::customWidgetColor;
QColor OptionHandler::customColors[ 10 ];
//menu
bool OptionHandler::useCustomMenuColor;
QColor OptionHandler::color, OptionHandler::fgColor;
//Panel
bool OptionHandler::panelCustom;
QColor OptionHandler::customPanelColor;
//incativeButton
int OptionHandler::inactiveButtonColor;
QColor OptionHandler::customInactiveButtonColor;

//Design
//stipples
bool OptionHandler::bgStipple, OptionHandler::panelStipple, OptionHandler::brushMe, OptionHandler::bevelHighlights_;
int OptionHandler::contrast;
//shadows
bool OptionHandler::groupboxshadow;
int OptionHandler::shadowDarkness;
//istviews;
bool OptionHandler::drawDotlines, OptionHandler::useCustomExpanderColor;
int OptionHandler::expanderStyle;
QColor OptionHandler::expanderColor, OptionHandler::dotlineColor;
int OptionHandler::dotlineStyle;
//slider
bool OptionHandler::squeezesbslider;
bool OptionHandler::shadowsbslider;
bool OptionHandler::animateSlider;
//toolbar
bool OptionHandler::reverseBtnColor, OptionHandler::tbFrame;
//tweaks
//bool OptionHandler::alignStipples;
bool OptionHandler::centerTabs;
//bool OptionHandler::engageFB;
bool OptionHandler::smoothListViewHeaders;
bool OptionHandler::smootherLVH;
bool OptionHandler::icyButtons;
bool OptionHandler::forceRectButton;
//int OptionHandler::saturation;
int OptionHandler::progressBar;
bool OptionHandler::removeKickerBevel;
bool OptionHandler::smoothToolbuttons;
int OptionHandler::shadowOffset;
int OptionHandler::dropShadowDarkness;
bool OptionHandler::drawProgressLabel;
int OptionHandler::menuAnimationSpeed;
QColor OptionHandler::brushedMetalColor;
QColor OptionHandler::brushTint;
bool OptionHandler::tintBrush;
Effects::DirFlags OptionHandler::menuAnimation;
tabStyle OptionHandler::tabStyle_;

//============


class LiquidStylePlugin : public QStylePlugin {
	public:
		LiquidStylePlugin() {
			;
			}
		~LiquidStylePlugin() {
			;
			}

		QStringList keys() const {
			return ( QStringList() << "Baghira" );
			}
		QStyle *create( const QString & );
	};

QStyle *LiquidStylePlugin::create( const QString &s ) {
	if ( s.lower() == "baghira" )
		return ( new LiquidStyle() );
	return ( 0 );
	}

Q_EXPORT_PLUGIN( LiquidStylePlugin )

static const int itemFrame = 1;
static const int itemHMargin = 3;
static const int itemVMargin = 1;
static const int arrowHMargin = 6;
static const int rightBorder = 12;


LiquidStyle::LiquidStyle() : KStyle( /*AllowMenuTransparency |*/ FilledFrameWorkaround, ThreeButtonScrollBar ) {
        headerHoverID = -1;
        headerSortID = -1;
	hoveredTabIndex = -1;
	currentTabBar = NULL;
	highlightWidget = NULL;
	currentTaskContainer = NULL;
	taskContainerHover = false;
	isTaskContainer = false;
	isHTMLButton = false;
	inExitPolish = false;
        inactiveRoundFrame = NULL;
        activeRoundFrame = NULL;

	btnDict.setAutoDelete( true );
	btnShadowedDict.setAutoDelete( true );
	bevelFillDict.setAutoDelete( true );
	smallBevelFillDict.setAutoDelete( true );
//	inverseTabDict.setAutoDelete( true );
//	tabDict.setAutoDelete( true );
	initialPaletteLoaded = false;
	qtrcModificationTime = 0;
	
	// animations kram!
	progAnimShift = 0;
	prog2AnimShift = 0;
	sliderAnimShift = 0;
	QTimer* timer = new QTimer( this );
	timer->start(50, false);
	connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressPos()));
	connect(timer, SIGNAL(timeout()), this, SLOT(updateSliderPos()));
	activeScroller = NULL;
//	activeProgress = NULL;
	//--------------------------------------

	tooltipPalette = qApp->palette();
	tooltipPalette.setBrush( QColorGroup::Background, QColor( 255, 255, 220 ) );
	tooltipPalette.setBrush( QColorGroup::Foreground, Qt::black );

	rMatrix.rotate( 90.0 );
	lMatrix.rotate( 270.0 );
	iMatrix.rotate( 180.0 );
	btnBorderImg = new QImage( liquid_findImage( "button-base" ) );
	if ( btnBorderImg->depth() < 32 )
		* btnBorderImg = btnBorderImg->convertDepth( 32 );
	btnShadowImg = new QImage( liquid_findImage( "button-shadow" ) );
	if ( btnShadowImg->depth() < 32 )
		* btnShadowImg = btnShadowImg->convertDepth( 32 );

	slider_top = new QImage( liquid_findImage( "sbslider_top" ) );
	if ( slider_top->depth() < 32 )
		* slider_top = slider_top->convertDepth( 32 );

	slider_btm = new QImage( liquid_findImage( "sbslider_btm" ) );
	if ( slider_btm->depth() < 32 )
		* slider_btm = slider_btm->convertDepth( 32 );

	slider_top_shd = new QImage( liquid_findImage( "sbslider_top_shd" ) );
	if ( slider_top_shd->depth() < 32 )
		* slider_top_shd = slider_top_shd->convertDepth( 32 );

	slider_btm_shd = new QImage( liquid_findImage( "sbslider_btm_shd" ) );
	if ( slider_btm_shd->depth() < 32 )
		* slider_btm_shd = slider_btm_shd->convertDepth( 32 );

	rectbutton = new QImage( liquid_findImage( "rectbutton" ) );
	if ( rectbutton->depth() < 32 )
		* rectbutton = rectbutton->convertDepth( 32 );

 	combo = new QImage( liquid_findImage( "combo" ) );
	if ( combo->depth() < 32 )
		* combo = combo->convertDepth( 32 );

	comboShadow = new QImage( liquid_findImage( "combo-shadow" ) );
	if ( comboShadow->depth() < 32 )
		* comboShadow = comboShadow->convertDepth( 32 );

	buttonGlow = new QImage( liquid_findImage( "button-glow" ) );
	if ( buttonGlow->depth() < 32 )
		* buttonGlow = buttonGlow->convertDepth( 32 );
                
	roundFrame = new QImage( liquid_findImage( "roundFrame" ) );
	if ( buttonGlow->depth() < 32 )
		* buttonGlow = buttonGlow->convertDepth( 32 );
	
	bevelFillPix = new QPixmap;
	bevelFillPix->convertFromImage( liquid_findImage( "clear_fill_large" ) );

	smallBevelFillPix = new QPixmap;
	smallBevelFillPix->convertFromImage( liquid_findImage( "clear_fill_small" ) );

	menuPix = NULL;
	tmpBtnPix = NULL;

	optionHandler = new OptionHandler( this );

	int i;
	for ( i = 0; i < BITMAP_ITEMS; ++i ) {
		pixmaps[ i ] = NULL;
		}

	sbLeft = new QBitmap( 6, 7, sbarrow_left_bits, true );
	sbLeft->setMask( *sbLeft );
	sbRight = new QBitmap( 6, 7, sbarrow_right_bits, true );
	sbRight->setMask( *sbRight );
	sbUp = new QBitmap( 7, 6, sbarrow_up_bits, true );
	sbUp->setMask( *sbUp );
	sbDown = new QBitmap( 7, 6, sbarrow_down_bits, true );
	sbDown->setMask( *sbDown );
        checkmark = new QBitmap( 6, 7, checkmark_bits, true );
	checkmark->setMask( *checkmark );

	isKicker = ( qstrcmp( qApp->argv() [ 0 ], "kicker" ) == 0 ) ||
	           ( qstrcmp( qApp->argv() [ 0 ], "appletproxy" ) == 0 );

	groupShadow = new QPixmap( 64, 64 );
	//    QColor c((w->palette()).active().background());
	QColor c;// = optionHandler->bgColor();
	( optionHandler->MenuColorButton()) ? c = QColor( optionHandler->buttonColor() ) : c = QColor( optionHandler->bgColor() );
	groupShadow->fill( ( c.dark( 100 + optionHandler->ShadowDarkness() ) ).rgb() );
	if ( !optionHandler->BrushMe() && optionHandler->useBgStipple() ) {
		QPainter p;
		p.begin( groupShadow );
		p.setPen( c.dark( 100 + optionHandler->ShadowDarkness() + (int)(optionHandler->stippleContrast()/2) ) );
		for ( int i = 1; i < 64; i += 4 ) {
			p.drawLine( 0, i, 63, i );
			p.drawLine( 0, i+2, 63, i+2 );
		}
                p.setPen( c.dark( 100 + optionHandler->ShadowDarkness() + optionHandler->stippleContrast()) );
                for ( int i = 2; i < 63; i += 4 ) {
                        p.drawLine( 0, i, 63, i );
                        }
		//    p.~QPainter();
	}
        
	if (optionHandler->BrushMe()){
            plainBrush = optionHandler->TintBrush() ?
                *tintBrush(liquid_findImage( "brushed_plain" ), optionHandler->BrushTint()) :
                liquid_findImage( "brushed_plain" );
            gradBrush = optionHandler->TintBrush() ?
                *tintBrush(liquid_findImage( "brushed_gradient" ), optionHandler->BrushTint()) :
                liquid_findImage( "brushed_gradient" );
        }
}

LiquidStyle::~LiquidStyle() {
//	btnDict.setAutoDelete(true);
	btnDict.clear();
//	btnShadowedDict.setAutoDelete(true);
	btnShadowedDict.clear();
//	buttonGlowDict.setAutoDelete(true);
	buttonGlowDict.clear();
//	comboDict.setAutoDelete(true);
	comboDict.clear();
//	comboShadowedDict.setAutoDelete(true);
	comboShadowedDict.clear();
//	if (! tabDict.isEmpty()) tabDict.clear();
//	if (! inverseTabDict.isEmpty()) inverseTabDict.clear();
//	rectBtnDict.setAutoDelete(true);
	rectBtnDict.clear();
//	bevelFillDict.setAutoDelete(true);
	bevelFillDict.clear();
//	smallBevelFillDict.setAutoDelete(true);
	smallBevelFillDict.clear();
//	progress2Dict.setAutoDelete(true);
	progress2Dict.clear();

	delete optionHandler;

	delete sbUp;
	delete sbDown;
	delete sbLeft;
	delete sbRight;
        delete checkmark;


	if ( btnBorderImg )
		delete btnBorderImg;
	if ( btnShadowImg )
		delete btnShadowImg;

	if ( tmpBtnPix )
		delete tmpBtnPix;

	if ( bevelFillPix )
		delete bevelFillPix;

	if ( smallBevelFillPix )
		delete smallBevelFillPix;

	if ( menuPix )
		delete menuPix;

	int i;
	for ( i = 0; i < BITMAP_ITEMS; ++i ) {
		if ( pixmaps[ i ] )
			delete pixmaps[ i ];
		}
	if ( groupShadow )
		delete groupShadow;

	}
	
inline void LiquidStyle::updateProgressPos()
{
	progAnimShift++;
	prog2AnimShift++;
	if (progAnimShift == 20)
		progAnimShift = 0;
	if (prog2AnimShift == 38)
		prog2AnimShift = 0;
}

inline void LiquidStyle::updateSliderPos(){
	sliderAnimShift++;
	if (sliderAnimShift == 32)
		sliderAnimShift = 0;
}

bool LiquidStyle::isPlain() const {
	return ( !optionHandler->useBgStipple() );
	}


void LiquidStyle::drawPrimitive( PrimitiveElement pe, QPainter *p,
                                 const QRect &r, const QColorGroup &cg,
                                 SFlags flags, const QStyleOption &opt ) const {
	bool down = flags & Style_Down;
	bool on = flags & Style_On;

	switch ( pe ) {
			case PE_TabBarBase: {
                        bool isDialog = false;
                        if (p->device() && p->device()->devType() == QInternal::Widget){
                            QWidget *w = (QWidget*)p->device();
                            if (w->parentWidget() && w->parentWidget()->inherits("KonqFrameTabs") && ((QTabWidget*)w->parentWidget())->count() == 1)//get rid of empty tab
                                break;
                            isDialog = optionHandler->TabStyle() == Clever && w->topLevelWidget()->inherits("QDialog");
                        }
			if (isDialog || optionHandler->TabStyle() == Chooser){
				int x = r.x(); int right = r.right(); int bottom = r.bottom(); int y2 = r.y() + 1;
				QColor tmp;
				optionHandler->BrushMe() ? tmp = optionHandler->brushedMetalColor : tmp = cg.background();
				QColor bright( tmp.dark( 110 ) );
				QColor mid( tmp.dark( 130 ) );
				QColor dark( tmp.dark( 150 ) );
				p->setPen( dark );
				p->drawLine( x + 6, y2, right - 6, y2 );
				p->drawPoint(x+3,y2+1);p->drawPoint(x+4,y2+1);
				p->drawPoint(x+2,y2+2);
				p->drawPoint(x+1,y2+3);
				p->drawPoint(right-3,y2+1);p->drawPoint(right-4,y2+1);
				p->drawPoint(right-2,y2+2);
				p->drawPoint(right-1,y2+3);
				p->drawLine(x,y2+5,x,bottom);
				p->drawLine(right,y2+5,right,bottom);
				
				p->setPen( mid );
				p->drawPoint(x+4, y2);p->drawPoint(x+5,y2);
				p->drawPoint(right-4, y2);p->drawPoint(right-5,y2);
				p->drawPoint(x+2, y2+1);
				p->drawPoint(right-2, y2+1);
				p->drawLine(x+5,y2 + 1, right - 5, y2 + 1);
				p->drawPoint(x+1,y2+2);
				p->drawPoint(right-1,y2+2);
				p->drawPoint(x+2,y2+3);
				p->drawPoint(right-2,y2+3);
				p->drawPoint(x,y2+4);p->drawPoint(x+1,y2+4);
				p->drawPoint(right,y2+4);p->drawPoint(right+1,y2+4);
				
				p->setPen(bright);
				p->drawPoint(x+3,y2);p->drawPoint(right-3,y2);
				p->drawLine(x+4,y2+2,right-4,y2+2);
				p->drawPoint(x,y2+3);p->drawPoint(x+3,y2+3);
				p->drawPoint(right,y2+3);p->drawPoint(right-3,y2+3);
				p->drawPoint(x+2,y2+4);p->drawPoint(right-2,y2+4);
				p->drawLine(x+1,y2+5,x+1,bottom);
				p->drawLine(right-1,y2+5,right-1,bottom);
				break;
			}
			else {
				drawCombo( p, cg, cg.button(), r.x(), r.y(), r.width(), r.height()+2, false, false, false, center);
				p->setPen( cg.button().dark( 158 ) );
 				p->drawRect(r);
			}
				break;
				}
			case PE_ButtonCommand:
			case PE_ButtonDefault: {
				bool sunken = on || down;
				bool hover = flags & Style_MouseOver;
				bool focused = flags & Style_HasFocus;

                                QColor newColor;
				if ( ( optionHandler->InactiveButtonColorType() == 2 ) || cg.button() != qApp->palette().active().button() )
					newColor = sunken ? cg.button().dark( 110 ) :
					           hover ? cg.button().light( 120 ) :
					           focused ? optionHandler->InactiveButtonColor() : cg.button();
				else {
					newColor = sunken ? cg.button().dark( 110 ) : hover ? cg.button() :
					           focused ? cg.button() : optionHandler->InactiveButtonColor();

					}
				if (optionHandler->ForceRectButton() || isHTMLButton){
					drawRectangularButton( p, cg, newColor, r.x(), r.y(), r.width(), r.height(), sunken, hover, false, full, isHTMLButton);
					}
				else{
//                                         if (p->device() && p->device()->devType() == QInternal::Widget){
//                                         QWidget *topL = ((QWidget*)p->device())->topLevelWidget();
//                                         QPoint point = topL ? ((QWidget*)p->device())->mapTo( topL, QPoint(0,0)) : QPoint(0,0);
//                                         }
					drawRoundButton( p, cg, newColor, cg.background(), r.x(), r.y(), r.width(), r.height(), !isHTMLButton, sunken, hover, false, isHTMLButton, btnOffset.x(), btnOffset.y() );
                                }

				break;
				}
			case PE_ButtonBevel:
			case PE_ButtonDropDown: {
				bool sunken = on || down;
				bool hover = flags & Style_MouseOver;
				drawRectangularButton( p, cg, sunken ? cg.button().dark( 110 ) : hover ? cg.button() : optionHandler->InactiveButtonColor(), r.x(), r.y(), r.width(), r.height(), sunken, hover, false, full, isHTMLButton);
				break;
				}
			case PE_ButtonTool: {
				bool sunken = on || down;
				bool hover = flags & Style_MouseOver;
				drawRectangularButton( p, cg, sunken ? cg.button().dark( 110 ) : hover ? cg.button() : optionHandler->InactiveButtonColor(), r.x(), r.y(), r.width(), r.height(), sunken, hover, false, full, isHTMLButton);
				break;
				}
			case PE_HeaderSection: {
				bool sunken = on || down;
				bool hover = flags & Style_MouseOver;
				QPen oldpen(p->pen());
				if ( isTaskContainer ) { // kicker?
					if (optionHandler->SmoothListViewHeaders()){
						drawCombo( p, cg, sunken ? cg.button() : taskContainerHover ? cg.button().light( 110 ) : origPanelBrush.color(), r.x(), r.y(), r.width(), isKicker ? r.height() + 2 : r.height(), sunken, hover, false, optionHandler->SmootherLVH() ? full : center );
						if (isKicker && !optionHandler->SmootherLVH()){
						p->setPen(cg.background().dark(150));
						p->drawRect(r.x(), r.y(), r.width(), r.height());
							if (sunken){
								p->setPen(cg.background().dark(200));
								p->drawRect(r.x()+1, r.y()+1, r.width()-2, r.height()-2);
							}
						}
					}
					else
						drawRectangularButton( p, cg, sunken ? cg.background() : taskContainerHover ? cg.background().light( 110 ) : origPanelBrush.color(), r.x(), r.y(), r.width(), r.height(), sunken, hover, false, center);
					const_cast<LiquidStyle*>( this ) ->isTaskContainer = false;
					const_cast<LiquidStyle*>( this ) ->taskContainerHover = false;
					}
				else {
					if ( (p->device()->devType() == QInternal::Widget) && ::qt_cast<QHeader*>((QWidget*)p->device()) ) {
						int id = ((QHeader*)p->device())->sectionAt( r.x()+ ((QHeader*)p->device())->offset() );
						hover = id != -1 && ((currentHeader == p->device() && id == headerHoverID ) || id == headerSortID);
						}
					if (optionHandler->SmoothListViewHeaders()){
						drawCombo( p, cg, sunken ? cg.button().dark( 110 ) : hover ? cg.button() : optionHandler->InactiveButtonColor(), r.x(), r.y(), r.width(), r.height(), sunken, hover, false, center);
					}
					else
						drawRectangularButton( p, cg, sunken ? cg.button().dark( 110 ) : hover ? cg.button() : optionHandler->InactiveButtonColor(), r.x(), r.y(), r.width(), r.height(), sunken, hover, false, center);
					}
				p->setPen(oldpen);
				break;
				}
			case PE_FocusRect: {
				p->drawWinFocusRect( r );
				break;
				}
			/**********************************************************************************************************
			*********************Scrollbar****************************************************************************
			***********************************************************************************************************/
			case PE_ScrollBarSlider:
			case PE_ScrollBarAddPage:
			case PE_ScrollBarSubPage: {
				QColor sbBgColor = optionHandler->useCustomColors() ?
				                   optionHandler->customColor( CustomSBGroove ) :
				                   isKicker ? origPanelBrush.color() :
				                   qApp->palette().active().background();

				bool isActive = optionHandler->ScrollerActive();
				bool isHover = currentScrollBar->hasMouse();
				bool isPressed = currentScrollBar->draggingSlider();
				bool drawSlider = currentScrollBar->minValue() < currentScrollBar->maxValue();
				
				// horizontal slider
				if ( flags & Style_Horizontal ) {
					int extent = currentScrollBar->height();
					QRect bgR( extent, 0, currentScrollBar->width() - extent * 3 + 1,
					           extent );
					if ( sbBuffer.size() != currentScrollBar->size() )
						const_cast<LiquidStyle*>( this ) ->
						sbBuffer.resize( currentScrollBar->size() );
					QPainter painter;
					painter.begin( &sbBuffer );
					// pixmaps
					if (drawSlider){
						painter.drawPixmap( bgR.x(), bgR.y() + 1, *getPixmap( HSBSliderBtmBg ) );
						painter.drawTiledPixmap( bgR.x() + 8, bgR.y() + 1, bgR.width() - 16, 13, *getPixmap( HSBSliderMidBg ) );
						painter.drawPixmap( bgR.right() - 8, bgR.y() + 1, *getPixmap( HSBSliderTopBg ) );
					}
					else
						painter.drawTiledPixmap( bgR.x(), bgR.y() + 1, bgR.width(), 13, *getPixmap( HSBSliderMidBg ) );
					
					// lines
					painter.setPen( sbBgColor.dark( 110 ) );
					painter.drawLine( bgR.x(), bgR.y(), bgR.right(), bgR.y());
					painter.drawLine( bgR.x(), bgR.bottom(), bgR.right(), bgR.bottom());


					if ( pe == PE_ScrollBarSlider && drawSlider) {
					int offset = isPressed * ( optionHandler->squeezeSbSlider() + optionHandler->shadowSbSlider()*2);
					int offset2 = optionHandler->shadowSbSlider()*2 + (isPressed && optionHandler->squeezeSbSlider());

						painter.drawPixmap( r.x() + offset, r.y() + 1,
						                    isPressed ? *getPixmap( HSBSliderBtmPressed ) :
								    (isHover || isActive) ? *getPixmap( HSBSliderBtmActive ) :
						                    *getPixmap( HSBSliderBtm));
    
						painter.drawPixmap( r.right() - 6 - offset2, r.y() + 1,
						                    isPressed ? *getPixmap( HSBSliderTopPressed ) :
								    (isHover || isActive) ? *getPixmap( HSBSliderTopActive ) :
						                    *getPixmap( HSBSliderTop ) );
						// mid part last, as i have to redefine clip section
						painter.setClipRect(r.x() + 7 + offset2, r.y() + 1, r.width() - 14 - 2*offset2, 13, QPainter::CoordPainter);
						painter.drawTiledPixmap( r.x() + 7 + offset2 + ((isHover || isPressed) ? (sliderAnimShift - 32) : 0), r.y() + 1, r.width() - 14 - 2*offset2 - ((isHover || isPressed) ? (sliderAnimShift - 32) : 0), 13,
						                         isPressed ? *getPixmap( HSBSliderMidPressed ) :
									 (isHover || isActive) ? *getPixmap( HSBSliderMidActive ) :
						                         *getPixmap( HSBSliderMid ) );
					}

					painter.end();
					}

				// vertical slider
				else {
					int extent = currentScrollBar->width();
					QRect bgR( 0, extent, extent, currentScrollBar->height() - extent * 3 + 1 );
					
					if ( sbBuffer.size() != currentScrollBar->size() )
						const_cast<LiquidStyle*>( this ) -> sbBuffer.resize( currentScrollBar->size() );
					QPainter painter;
					painter.begin( &sbBuffer ); // set painter to pixelbuffer
					if (drawSlider){
						// top slidergroove rounder
						painter.drawPixmap( bgR.x() + 1, bgR.y(), *getPixmap( VSBSliderTopBg ) );
						// the groove
						painter.drawTiledPixmap( bgR.x() + 1, bgR.y() + 8, 13, bgR.height() - 16, *getPixmap( VSBSliderMidBg ) );
						// bottom slidergroove rounder
						painter.drawPixmap( bgR.x() + 1, bgR.bottom() - 8, *getPixmap( VSBSliderBtmBg ) );
					}
					else
						painter.drawTiledPixmap( bgR.x() + 1, bgR.y(), 13, bgR.height(), *getPixmap( VSBSliderMidBg ) );
					
					painter.setPen( sbBgColor.dark( 110 ) );
					painter.drawLine( bgR.x(), bgR.y(), bgR.x(), bgR.bottom() );
					painter.drawLine( bgR.right(), bgR.y(), bgR.right(), bgR.bottom() );

					
					// here we are, now the slider
					if ( pe == PE_ScrollBarSlider && drawSlider ) {
						int offset = isPressed * ( optionHandler->squeezeSbSlider() + optionHandler->shadowSbSlider()*2);
						int offset2 = (isPressed * optionHandler->squeezeSbSlider()) + optionHandler->shadowSbSlider()*2;
						
						painter.drawPixmap( r.x() + 1, r.y() + offset,
						                    isPressed ? *getPixmap( VSBSliderTopPressed ) :
								    (isHover || isActive) ? *getPixmap( VSBSliderTopActive ) :
						                    *getPixmap( VSBSliderTop));
						painter.drawPixmap( r.x() + 1, r.bottom()  - 6 - offset2,
						                    isPressed ? *getPixmap( VSBSliderBtmPressed ) :
								    (isHover || isActive) ? *getPixmap( VSBSliderBtmActive ) :
						                    *getPixmap( VSBSliderBtm ) );
						// mid part last, as i have to redefine clipping section
						painter.setClipRect(r.x() + 1, r.y() + 7+offset2, 13, r.height() - 14 - 2*offset2, QPainter::CoordPainter);
						painter.drawTiledPixmap( r.x() + 1, r.y() + 7 + offset2 - ((isHover || isPressed) ? sliderAnimShift : 0), 13, r.height() - 14 - 2*offset2 + ((isHover || isPressed) ? sliderAnimShift : 0),
						                         isPressed ? *getPixmap( VSBSliderMidPressed ) :
									 (isHover || isActive) ? *getPixmap( VSBSliderMidActive ) :
						                         *getPixmap( VSBSliderMid ) );    
					}
					painter.end();
					}
				p->drawPixmap( r.x(), r.y(), sbBuffer, r.x(), r.y(), r.width(), r.height() );

				break;
				}
			/******************************************************************************************
			******************************************************************************************/
			case PE_ScrollBarAddLine: {
				QColor c( optionHandler->useCustomColors() ?
				          optionHandler->customColor( CustomSBGroove ) :
				          isKicker ? origPanelBrush.color() :
				          qApp->palette().active().background() );
				QColor pixColor = flags & Style_Down ?
				                  qApp->palette().active().button() : c;
				
				bool drawSlider = currentScrollBar->minValue() < currentScrollBar->maxValue();  
				
				if ( flags & Style_Horizontal ) {
					p->drawTiledPixmap( r.x(), r.y() + 1, r.width(), 13, *getPixmap(drawSlider ? HSBSliderMidBgI : HSBSliderMidBg));
					p->setPen( c.dark( 110 ) );
					p->drawLine( r.x(), r.y(), r.right(), r.y());
					p->drawLine( r.x(), r.bottom(), r.right(), r.bottom());
				} else {
					p->drawTiledPixmap( r.x() + 1, r.y(), 13, r.height(), *getPixmap(drawSlider ? VSBSliderMidBgI : VSBSliderMidBg));
					p->setPen( c.dark( 110 ) );
					p->drawLine( r.x(), r.y(), r.x(), r.bottom());
					p->drawLine( r.right(), r.y(), r.right(), r.bottom());
				}
				if (drawSlider){
					p->setPen( flags & Style_Down ? cg.button() : QColor(85,85,85) );
					p->setBrush( flags & Style_Down ? cg.button() : QColor(85,85,85) );
					p->drawPixmap( r.x() + 4, r.y() + 4, flags & Style_Horizontal ? *sbRight : *sbDown );
				}
				break;
				}
			case PE_ScrollBarSubLine: {
				QColor c( optionHandler->useCustomColors() ?
				          optionHandler->customColor( CustomSBGroove ) :
				          isKicker ? origPanelBrush.color() :
				          qApp->palette().active().background() );
				QColor pixColor = flags & Style_Down ?
				                  qApp->palette().active().button() : c;
				bool top = flags & Style_Horizontal ? r.x() == 0 :
				           r.y() == 0;
					   
				bool drawSlider = currentScrollBar->minValue() < currentScrollBar->maxValue();
				
				if ( flags & Style_Horizontal ) {
					if ( top ) {
						p->drawTiledPixmap( r.x(), r.y() + 1, r.width(), 13, *getPixmap(drawSlider ? HSBSliderMidBgI : HSBSliderMidBg));
					}
					else {
						p->drawTiledPixmap( r.x(), r.y() + 1, r.width(), 13, *getPixmap(drawSlider ? HSBSliderMidBgI : HSBSliderMidBg));
					}
					p->setPen( c.dark( 110 ) );
					p->drawLine( r.x(), r.y(), r.right(), r.y());
					p->drawLine( r.x(), r.bottom(), r.right(), r.bottom());
				}
				else {
					if ( top ) {
						p->drawTiledPixmap( r.x() + 1, r.y(), 13, r.height(), *getPixmap(drawSlider ? VSBSliderMidBgI : VSBSliderMidBg));
					}
					else {
						p->drawTiledPixmap( r.x() + 1, r.y(), 13, r.height(), *getPixmap(drawSlider ? VSBSliderMidBgI : VSBSliderMidBg));
					}
					p->setPen( c.dark( 110 ) );
					p->drawLine( r.x(), r.y(), r.x(), r.bottom());
					p->drawLine( r.right(), r.y(), r.right(), r.bottom());
				}
				if (drawSlider){
					p->setPen( flags & Style_Down ? cg.button() : QColor(85,85,85) );
					p->setBrush( flags & Style_Down ? cg.button() : QColor(85,85,85) );
					if (currentScrollBar->minValue() < currentScrollBar->maxValue()) p->drawPixmap( r.x() + 4, r.y() + 4, flags & Style_Horizontal ? *sbLeft : *sbUp );
				}
				break;
				}
			case PE_Indicator: {
				bool hover = flags & Style_MouseOver;
				bool pressed = flags & Style_Down;
				bool isMasked = p->device() && p->device() ->devType() == QInternal::Widget && ( ( QWidget * ) p->device() ) ->autoMask();
				if ( isMasked ) {
					if ( !( flags & Style_Off ) )
						p->drawPixmap( r.x(), r.y(),
						               hover ? *getPixmap( HTMLCBDownHover ) :
						               *getPixmap( HTMLCBDown ) );
					else
						p->drawPixmap( r.x(), r.y(), hover ?
						               *getPixmap( HTMLCBHover ) : *getPixmap( HTMLCB ) );
					} else {
					if ( !( flags & Style_Off ) )
						p->drawPixmap( r.x(), r.y(),
						               pressed ? *getPixmap( CBDownPressed ) :
						               hover ? *getPixmap( CBDownHover ) :
						               *getPixmap( CBDown ) );
					else
						p->drawPixmap( r.x(), r.y(),
						               pressed ? *getPixmap( CBPressed ) :
						               hover ? *getPixmap( CBHover ) :
						               *getPixmap( CB ) );
					}
				break;
				}
			case PE_IndicatorMask: {
				p->fillRect( r, Qt::color1 );
				/*p->setPen(Qt::color0);
				p->drawPoint(r.x(), r.y());
				p->drawPoint(r.right(), r.y());
				p->drawPoint(r.x(), r.bottom());
				p->drawPoint(r.right(), r.bottom());*/
				//p->setPen(Qt::color1);
				//p->drawPixmap(r.x(), r.y(), *getPixmap(CB)->mask());
				break;
				}
			case PE_ExclusiveIndicator: {
				bool hover = flags & Style_MouseOver;
				bool isMasked = p->device() && p->device() ->devType() == QInternal::Widget && ( ( QWidget * ) p->device() ) ->autoMask();
				bool pressed = flags & Style_Down;


				if ( isMasked ) {
					if ( on || down )
						p->drawPixmap( r.x(), r.y(), hover ?
						               *getPixmap( HTMLRadioOnHover ) :
						               *getPixmap( HTMLRadioOn ) );
					else
						p->drawPixmap( r.x(), r.y(), hover ?
						               *getPixmap( HTMLRadioOffHover ) :
						               *getPixmap( HTMLRadioOff ) );
					} else {
					if ( on || down )
						p->drawPixmap( r.x(), r.y(),
						               pressed ? *getPixmap( RadioOnPressed ) :
						               hover ? *getPixmap( RadioOnHover ) :
						               *getPixmap( RadioOn ) );
					else
						p->drawPixmap( r.x(), r.y(),
						               pressed ? *getPixmap( RadioOffPressed ) :
						               hover ? *getPixmap( RadioOffHover ) :
						               *getPixmap( RadioOff ) );
					}
				break;
				}
			case PE_ExclusiveIndicatorMask: {
				p->setPen( Qt::color1 );
				p->drawPixmap( r.x(), r.y(), *getPixmap( HTMLRadioOn ) ->mask() );
				break;
				}
			case PE_DockWindowResizeHandle:
			case PE_Splitter: {
				QPen oldpen(p->pen());
				QBrush oldbrush(p->brush());
				if (highlightWidget == p->device()){
					if (flags & Style_Horizontal){ //vertical?
						p->setPen(cg.mid());
						p->drawLine(r.x(), r.y(), r.x(), r.bottom());
						p->drawLine(r.right(), r.y(), r.right(), r.bottom());
						p->setPen(cg.button().dark(140));
						p->setBrush(cg.button().dark(140));
						p->drawEllipse(r.x()+1,r.y() + (int)(r.height() / 2.5), 4, 4);
					}
					else{
						p->setPen(cg.mid());
						p->drawLine(r.x(), r.y(), r.right(), r.y());
						p->drawLine(r.x(), r.bottom(), r.right(), r.bottom());
						p->setPen(cg.button().dark(140));
						p->setBrush(cg.button().dark(140));
						p->drawEllipse(r.x()+r.width()/2,r.y() + 1, 4, 4);
					}

				}
				else{
					if (flags & Style_Horizontal){
						p->setPen(cg.mid());
						p->drawLine(r.x(), r.y(), r.x(), r.bottom());
						p->drawLine(r.right(), r.y(), r.right(), r.bottom());
						p->setPen(cg.background().dark(130));
						p->setBrush(cg.background().dark(150));
						p->drawEllipse(r.x()+1,r.y() + (int)(r.height() / 2.5), 4, 4);
					}
					else{
						p->setPen(cg.mid());
						p->drawLine(r.x(), r.y(), r.right(), r.y());
						p->drawLine(r.x(), r.bottom(), r.right(), r.bottom());
						p->setPen(cg.background().dark(130));
						p->setBrush(cg.background().dark(150));
						p->drawEllipse(r.x()+r.width()/2,r.y() + 1, 4, 4);
					}
				}
				p->setPen(oldpen);
				p->setBrush(oldbrush);
				break;
				}
//			case PE_BoxFrame:
			case PE_PanelGroupBox:
			case PE_GroupBoxFrame: {
				QColor bgColor;
				optionHandler->drawGroupBoxShadow() ? bgColor = QColor(cg.background().dark( 100 + optionHandler->ShadowDarkness() )) : optionHandler->BrushMe() ? bgColor=optionHandler->brushedMetalColor : bgColor = QColor(cg.background());
				QColor bright( bgColor.dark( 110 ) );
				QColor mid( bgColor.dark( 130 ) );
				QColor dark( bgColor.dark( 150 ) );

				int titleHeight = (pe == PE_PanelGroupBox) ? p->fontMetrics().height() : 0;
                                int titleVertOffset = (pe == PE_PanelGroupBox) ? titleHeight / 2 + 1 : 0;
				int x = r.x(); int right = r.right(); int bottom = r.bottom();
				int y2 = r.y() + titleVertOffset;

                                // ok, to get the sunken look somwhat efficient and less glitchy we reset the groupbox brush and only repaint the title
                                // (saves us from several is item in groupbox checks as well)
                                // only problem left: draw the parents brush on the title window aligned...
//                                 QRect textBound = painter.boundingRect ( x + 2 * MARGIN + iconWidth, y, w - MARGIN * 2 - iconWidth, h, BaghiraFactory::titleAlign() | AlignVCenter, caption() );
                                if (optionHandler->drawGroupBoxShadow() && p->device() && p->device()->devType() == QInternal::Widget && ((QWidget*)p->device())->parentWidget()){
                                    p->setClipping(false);
                                    QString title = ((QGroupBox*)p->device())->title();
                                    const QPixmap *pix = ((QWidget*)p->device())->parentWidget()->paletteBackgroundPixmap();
//                                     QWidget *topL = pix ? ((QWidget*)p->device())->topLevelWidget() : NULL;
                                    pix ? p->drawTiledPixmap(QRect(0,0,r.width(),titleHeight),*pix, ((QWidget*)p->device())->backgroundOffset())/* topL ? ((QWidget*)p->device())->mapTo( topL, QPoint(0,0)) : QPoint(0,0))*/ : 
                                          p->fillRect(QRect(0,0,r.width(),titleHeight), ((QWidget*)p->device())->parentWidget()->backgroundBrush());
                                    QFont font( p->font() );
                                    font.setBold( true );
                                    p->setFont( font );
                                    drawItem( p, QRect(10,0,r.width(),titleHeight), ShowPrefix, cg, flags & Style_Enabled, 0, title, -1, &cg.buttonText() );
                                    p->setClipping(true);
                                }
                                
                                //outer rect
				// top
				p->setPen( dark );
				if (pe == PE_PanelGroupBox){
					p->drawLine( x + 6, y2, right - 6, y2 );
					// left points
					p->drawPoint(x+3,y2+1);p->drawPoint(x+4,y2+1);
					p->drawPoint(x+2,y2+2);
					p->drawPoint(x+1,y2+3);
					// right points
					p->drawPoint(right-3,y2+1);p->drawPoint(right-4,y2+1);
					p->drawPoint(right-2,y2+2);
					p->drawPoint(right-1,y2+3);
					// vertcal outer lines
					p->drawLine(x,y2+5,x,bottom-5);
					p->drawLine(right,y2+5,right,bottom-5);
				}
				else{
					// vertcal outer lines
					p->drawLine(x,y2,x,bottom-5);
					p->drawLine(right,y2,right,bottom-5);
				}
				
				p->setPen( mid );
				if (pe == PE_PanelGroupBox){
				// top stuff
					p->drawPoint(x+4, y2);p->drawPoint(x+5,y2);
					p->drawPoint(right-4, y2);p->drawPoint(right-5,y2);
					p->drawPoint(x+2, y2+1);
					p->drawPoint(right-2, y2+1);
					p->drawLine(x+5,y2 + 1, right - 5, y2 + 1);
					p->drawPoint(x+1,y2+2);
					p->drawPoint(right-1,y2+2);
					p->drawPoint(x+2,y2+3);
					p->drawPoint(right-2,y2+3);
					p->drawPoint(x,y2+4);p->drawPoint(x+1,y2+4);
					p->drawPoint(right,y2+4);p->drawPoint(right+1,y2+4);
				}
				// bottom stuff
				p->drawPoint(x,bottom - 4); p->drawPoint(x+1,bottom - 4);
				p->drawPoint(right,bottom - 4); p->drawPoint(right-1,bottom - 4);
				p->drawPoint(x+1,bottom - 3);
				p->drawPoint(right-1,bottom - 3);
				p->drawPoint(x+1,bottom - 2); p->drawPoint(x+2,bottom - 2);
				p->drawPoint(right-1,bottom - 2); p->drawPoint(right-2,bottom - 2);
				p->drawPoint(x+2, bottom - 1); p->drawPoint(x+3, bottom - 1);
				p->drawPoint(right-2, bottom - 1); p->drawPoint(right-3, bottom - 1);
				p->drawLine(x + 4, bottom,right - 4, bottom);
				
				p->setPen(bright);
				// top stuff
				if (pe == PE_PanelGroupBox){
					p->drawPoint(x+3,y2);p->drawPoint(right-3,y2);
					p->drawLine(x+4,y2+2,right-4,y2+2);
					p->drawPoint(x,y2+3);p->drawPoint(x+3,y2+3);
					p->drawPoint(right,y2+3);p->drawPoint(right-3,y2+3);
					p->drawPoint(x+2,y2+4);p->drawPoint(right-2,y2+4);
					// vertical lines
					p->drawLine(x+1,y2+5,x+1,bottom-5);
					p->drawLine(right-1,y2+5,right-1,bottom-5);
				}
				else{
					// vertical lines
					p->drawLine(x+1,y2,x+1,bottom-5);
					p->drawLine(right-1,y2,right-1,bottom-5);
				}
				// bottom stuff
				p->drawPoint(x, bottom-3);p->drawPoint(right, bottom-3);
				p->drawPoint(x+3, bottom);p->drawPoint(right-3, bottom);
				

				break;
				///////////////////////////////////////////////////////////////////////////////
				///////////////////////////////////////////////////////////////////////////////
				}
                                case PE_StatusBarSection: {
//                                     if (p->device() && p->device()->devType() == QInternal::Widget)
//                                         qWarning("%s",((QWidget*)p->device())->className());
//                                     drawEditFrame( p, r, cg, false, false, false );
                                    break;
                                }                               

			case PE_WindowFrame:{
				drawEditFrame( p, r, cg, false, false, true );
				break;
			}
			case PE_Panel: {
 				bool sunken = flags & Style_Sunken;
// 				int lw = opt.isDefault() ? pixelMetric( PM_DefaultFrameWidth ) :
// 				         opt.lineWidth();
// 				QPen oldPen( p->pen() );
// 				if ( lw == 1) {
// 				if (!(isKicker && optionHandler->RemoveKickerBevel())){
// 					QColor light( cg.background().light( 120 ) );
// 					QColor dark( cg.background().dark( 140 ) );
// 					QColor mid( cg.background().dark( 120 ) );
// 					p->setPen( mid );
// 					p->drawPoint( r.x(), r.bottom() );
// 					p->drawPoint( r.right(), r.y() );
// 
// 					p->setPen( sunken ? dark : light );
// 					p->drawLine( r.x(), r.y(), r.right() - 1, r.y() );
// 					p->drawLine( r.x(), r.y() + 1, r.x(), r.bottom() - 1 );
// 					p->setPen( sunken ? light : dark );
// 					p->drawLine( r.x() + 1, r.bottom(), r.right(), r.bottom() );
// 					p->drawLine( r.right(), r.y() + 1, r.right(), r.bottom() - 1 );
// 				}
// 				} else if ( lw == 2) {
// 					if (!(isKicker && optionHandler->RemoveKickerBevel())){
// 					QColor light1, light2, light3, dark1, dark2, dark3;
// 					QColor mid( cg.background().dark( 110 ) );
// 					if ( !sunken ) {
// 						light1 = cg.background().light( 101 );
// 						light2 = cg.background().light( 120 );
// 						light3 = cg.background().light( 110 );
// 						dark1 = cg.background().dark( 115 );
// 						dark2 = cg.background().dark( 130 );
// 						dark3 = cg.background().dark( 110 );
// 					} else {
// 						light1 = cg.background().dark( 115 );
// 						light2 = cg.background().dark( 130 );
// 						light3 = cg.background().dark( 110 );
// 						dark1 = cg.background().light( 101 );
// 						dark2 = cg.background().light( 120 );
// 						dark3 = cg.background().light( 110 );
// 					}
// 					p->setPen( light1 );
// 					p->drawPoint( r.x(), r.y() );
// 					p->setPen( dark1 );
// 					p->drawPoint( r.right(), r.bottom() );
// 					p->setPen( mid );
// 					p->drawPoint( r.x(), r.bottom() );
// 					p->drawPoint( r.right(), r.y() );
// 
// 					p->setPen( light2 );
// 					p->drawLine( r.x() + 1, r.y(), r.right() - 1, r.y() );
// 					p->drawLine( r.x(), r.y() + 1, r.x(), r.bottom() - 1 );
// 
// 					p->setPen( light3 );
// 					p->drawLine( r.x() + 1, r.y() + 1, r.right() - 1, r.y() + 1 );
// 					p->drawLine( r.x() + 1, r.y() + 1, r.x() + 1, r.bottom() - 1 );
// 
// 					p->setPen( dark2 );
// 					p->drawLine( r.x() + 1, r.bottom(), r.right() - 1, r.bottom() );
// 					p->drawLine( r.right(), r.y() + 1, r.right(), r.bottom() - 1 );
// 
// 					p->setPen( dark3 );
// 					p->drawLine( r.x() + 1, r.bottom() - 1, r.right() - 1, r.bottom() - 1 );
// 					p->drawLine( r.right() - 1, r.y() + 1, r.right() - 1, r.bottom() - 1 );
// 					}
// 				} else
// 					KStyle::drawPrimitive( pe, p, r, cg, flags, opt );
				if (!(isKicker && optionHandler->RemoveKickerBevel()))
					sunken ? drawEditFrame( p, r, cg ) : drawEditFrame( p, r, cg, false, false, true );
// 				p->setPen( oldPen );
				break;
				}
			case PE_PanelTabWidget: {
                                // don't ask
                                bool isDialog = optionHandler->TabStyle() == Clever &&
                                (p->device() && p->device()->devType() == QInternal::Widget)&&
                                ((QWidget*)p->device())->topLevelWidget()->inherits("QDialog");
                                
				QColor bgColor;
/*				optionHandler->drawGroupBoxShadow() ? bgColor = QColor(cg.background().dark( 100 + optionHandler->ShadowDarkness() )) :*/ 
				optionHandler->BrushMe() ? bgColor=optionHandler->brushedMetalColor : bgColor = QColor(cg.background());
				QColor bright( bgColor.dark( 110 ) );
				QColor mid( bgColor.dark( 130 ) );
				QColor dark( bgColor.dark( 150 ) );
				int x = r.x(); int right = r.right(); int bottom = r.bottom(); int y2 = r.y();
				if (isDialog || optionHandler->TabStyle() == Chooser){
				// fill (dark sunken color - we just paint us our own box, owerwriting coded style...)
// 				if ( pe == PE_PanelGroupBox && optionHandler->drawGroupBoxShadow()){
// 					if (isKicker){
// 						QPixmap *shadow = new QPixmap( 64, 64 );
// 						QColor c;
// 						( optionHandler->MenuColorButton() ) ? c = QColor( optionHandler->buttonColor() ) : c = QColor( optionHandler->bgColor() );
// 						shadow->fill( ( c.dark( 100 + optionHandler->ShadowDarkness()) ).rgb() );
// 						if ( optionHandler->useBgStipple() ) {
// 							QPainter p;
// 							p.begin( shadow );
// 							p.setPen( c.dark( 100 + optionHandler->ShadowDarkness() + optionHandler->stippleContrast() ) );
// 							for ( int i = 0; i < 63; i += 4 ) {
// 								p.drawLine( 0, i, 63, i );
// 								p.drawLine( 0, i + 1, 63, i + 1 );
// 							}
// 						}
// 						p->drawTiledPixmap( QRect(x + 2, y2 + 3, r.width() - 4, r.height() - titleVertOffset - 4), *shadow/*,xyOffset*/);
// 						delete shadow;
// 					}
// 					else
// 						p->drawTiledPixmap( QRect(x + 2, y2 + 3, r.width() - 4, r.height() - titleVertOffset - 4), *groupShadow/*,xyOffset*/);
// 				}


				//outer rect
				// top
				p->setPen( dark );
				// vertcal outer lines
				p->drawLine(x,y2,x,bottom-5);
				p->drawLine(right,y2,right,bottom-5);
				
				p->setPen( mid );
				// bottom stuff
				p->drawPoint(x,bottom - 4); p->drawPoint(x+1,bottom - 4);
				p->drawPoint(right,bottom - 4); p->drawPoint(right-1,bottom - 4);
				p->drawPoint(x+1,bottom - 3);
				p->drawPoint(right-1,bottom - 3);
				p->drawPoint(x+1,bottom - 2); p->drawPoint(x+2,bottom - 2);
				p->drawPoint(right-1,bottom - 2); p->drawPoint(right-2,bottom - 2);
				p->drawPoint(x+2, bottom - 1); p->drawPoint(x+3, bottom - 1);
				p->drawPoint(right-2, bottom - 1); p->drawPoint(right-3, bottom - 1);
				p->drawLine(x + 4, bottom,right - 4, bottom);
				
				p->setPen(bright);
				// top stuff
				// vertical lines
				p->drawLine(x+1,y2,x+1,bottom-5);
				p->drawLine(right-1,y2,right-1,bottom-5);
				
				// bottom stuff
				p->drawPoint(x, bottom-3);p->drawPoint(right, bottom-3);
				p->drawPoint(x+3, bottom);p->drawPoint(right-3, bottom);
				break;
				}
				else{
					p->setPen( mid );
					p->drawLine( x, y2, x, bottom-2 );
					p->drawLine( right, y2, right, bottom-2 );
					p->setPen( dark );
					p->drawLine( x, bottom-1, right, bottom-1 );
					p->drawLine( x, bottom, right, bottom );
					break;
				}
				}
			case PE_PanelLineEdit: {
				bool focused = flags & Style_HasFocus;
    // 				QWidget *widget = NULL;
    //                                 if (p->device() && p->device()->devType() == QInternal::Widget) widget = (QWidget*)p->device();
                                bool isHTML = p->device() && p->device()->devType() == QInternal::Widget && isHTMLWidget((QWidget*)p->device());
// 				widget && widget->parentWidget() && ::qt_cast<QToolBar*>(widget->parentWidget()) ?
//                                     drawRoundFrame( p, r, cg, focused, widget->backgroundOffset()) :
                                    drawEditFrame( p, r, cg, isHTML, focused, false);
				break;
				}
			case PE_PanelPopup: {
//				if ( (optionHandler->MenuTranslucency() == Solid) && !optionHandler->StippleMenu() ){
//					KStyle::drawPrimitive( pe, p, r, cg, flags, opt );
//					break;
//				}
				int x, y, w , h;
				r.rect( &x, &y, &w, &h );

				optionHandler->prepareMenus();
				QColor c( optionHandler->bgColor() );
				p->setPen( c.dark( 140 ) );
				p->drawRect( x, y, w, h );
				p->setPen( c.light( 120 ) );
				p->drawRect( x + 1, y + 1, w - 2, h - 2 );
				break;
				}
			case PE_PanelMenuBar: {
// 				if ( p->device() && p->device()->devType() == QInternal::Widget ) {
// 					QWidget * w = ( QWidget * ) p->device();
// 					if ( w->backgroundMode() == PaletteButton ){
// 						w->setBackgroundMode( PaletteBackground );
// 						w->setBackgroundOrigin(QWidget::WindowOrigin);
// //						p->fillRect(r, cg.brush(QColorGroup::Background));
// 						w->erase();
// 					}

				}
			case PE_PanelDockWindow: {
// 				// This method draws toolbars
// 				if(p->device() && p->device()->devType() == QInternal::Widget){
// 				    // check if background brush is set properly - Qt sometimes
// 				    // resets it to PaletteButton even after my polish() sets it
// 				    // to PaletteBackground. I think they do it just to mess with
// 				    // my head ;-)
// 				    QWidget *w = (QWidget *)p->device();
// 				    if(w->backgroundMode() == PaletteButton){
// 				        w->setBackgroundMode(PaletteBackground);
// 				        w->setBackgroundOrigin(QWidget::WindowOrigin);
// //					p->fillRect(r, cg.brush(QColorGroup::Background));
// 				        w->erase();
// 				    }
// 					}
// 				break;
				}
			case PE_Separator:
			/*case PE_DockWindowSeparator:*/ {
                                //qWarning("%s",p->device()->className());
                                if (!(p->device() && p->device()->devType() == QInternal::Widget && ::qt_cast<QToolBar*>((QWidget*)p->device()) /*((QWidget*)p->device())->inherits("QToolBar")*/)){
				if ( !( flags & Style_Horizontal ) ) {
					p->setPen( cg.mid() );
					p->drawLine( 4, r.height() / 2, r.width() - 5, r.height() / 2 );
					p->setPen( cg.light() );
					p->drawLine( 4, r.height() / 2 + 1, r.width() - 5, r.height() / 2 + 1 );
					} else {
					p->setPen( cg.mid() );
					p->drawLine( r.width() / 2, 4, r.width() / 2, r.height() - 5 );
					p->setPen( cg.light() );
					p->drawLine( r.width() / 2 + 1, 4, r.width() / 2 + 1, r.height() - 5 );
					}
                                        }
				break;
				}
			case PE_SpinWidgetUp:
			case PE_SpinWidgetDown: {
				bool sunken = on || down;
				//bool hover = flags & Style_MouseOver;
				int x = r.x() + ( r.width() - 7 ) / 2;
				int y = pe == PE_SpinWidgetDown ? r.y() + 1 : r.bottom() - 7;
				QPen oldPen( p->pen() );
				QBrush oldBrush( p->brush() );
//				QPointArray a;
				p->setPen( sunken ? cg.button() : cg.buttonText() );
				p->setBrush( sunken ? cg.button() : cg.buttonText() );
//				pe == PE_SpinWidgetUp ? a.setPoints( 3, x, y + 3, x + 4, y-1, x + 8, y + 3) : a.setPoints( 3, x, y, x + 4, y + 4, x + 8, y );
				p->drawPixmap( x, y, pe == PE_SpinWidgetUp ? *sbUp : *sbDown );
//				p->drawPolygon(a);
				p->setBrush( oldBrush );
				p->setPen( oldPen );
				break;
				}
			case PE_CheckMark:{
                                QPen oldPen( p->pen() );
                                p->setPen( flags & Style_On ? cg.highlightedText() : optionHandler->textColor() );
                                p->drawPixmap(r.x() + r.width()/2 - 4,r.y() + r.height()/2 - 4,*checkmark);
                                p->setPen( oldPen );
				break;
			}
			default: {
				if ( (pe >= PE_ArrowUp && pe <= PE_ArrowLeft ) || pe == PE_HeaderArrow) {
					QBrush oldBrush(p->brush());
                                        QColor tmpC = flags & Style_Enabled ? (flags & Style_Down || flags & Style_Sunken) && pe != PE_HeaderArrow ? cg.light() : cg.buttonText() : cg.mid();
                                        if (tmpC == Qt::black) tmpC = QColor(85,85,85);
                                        p->setBrush(tmpC);
//                                         int h,s,v;
//                                         tmpC.getHsv(&h,&s,&v);
//                                         v = v < 150 ? 150 : v > 210 ? v - 10 : v + 10;
//                                         QColor tmpC2;
//                                         tmpC2.setHsv(h,s,v);
					int x = r.width() > 7 ? r.x() + r.width()/2 - 4 : r.right() - 7;
					int y = r.height() > 7 ? r.y() + r.height()/2 - 4 : r.bottom() - 7;

					switch ( pe ) {
						case PE_ArrowRight:{
							//a.setPoints( 3, x, y, x + 4, y+4, x, y + 8 );
                                                        p->drawPixmap(x,y,*sbRight);
//                                                         p->setPen(tmpC2);
//                                                         p->drawPoint(x+1,y);
//                                                         p->drawPoint(x+3,y+1);
//                                                         p->drawPoint(x+1,y+6);
//                                                         p->drawPoint(x+3,y+5);
							break;
						}
                                                case PE_ArrowUp:{
							//a.setPoints( 3, x, y + 4, x + 4, y, x + 8, y + 4 );
                                                        p->drawPixmap(x,y,*sbUp);
//                                                         p->setPen(tmpC2);
//                                                         p->drawPoint(x,y+5);
//                                                         p->drawPoint(x+6,y+5);
//                                                         p->drawPoint(x+1,y+2);
//                                                         p->drawPoint(x+5,y+2);
							break;
						}
						case PE_ArrowDown:{
							//a.setPoints( 3, x, y, x + 4, y + 4, x + 8, y );
                                                        p->drawPixmap(x,y,*sbDown);
//                                                         p->setPen(tmpC2);
//                                                         p->drawPoint(x,y+1);
//                                                         p->drawPoint(x+6,y+1);
//                                                         p->drawPoint(x+1,y+3);
//                                                         p->drawPoint(x+5,y+3);
							break;
						}
						case PE_HeaderArrow:{
							if (flags & Style_Up){
								//a.setPoints( 3, x, y + 4, x + 4, y, x + 8, y + 4 );
                                                                p->drawPixmap(x,y,*sbUp);
//                                                                 p->setPen(tmpC2);
//                                                                 p->drawPoint(x,y+5);
//                                                                 p->drawPoint(x+6,y+5);
//                                                                 p->drawPoint(x+1,y+2);
//                                                                 p->drawPoint(x+5,y+2);
                                                        }
							else{
								//a.setPoints( 3, x, y, x + 4, y + 4, x + 8, y );
                                                                 p->drawPixmap(x,y,*sbDown);
//                                                                  p->setPen(tmpC2);
//                                                                 p->drawPoint(x,y+1);
//                                                                 p->drawPoint(x+6,y+1);
//                                                                 p->drawPoint(x+1,y+3);
//                                                                 p->drawPoint(x+5,y+3);
                                                        }
							break;
						}
						default:{
                                                        //a.setPoints( 3, x + 4, y, x, y+4, x + 4, y + 8 );
                                                        p->drawPixmap(x,y,*sbLeft);
//                                                         p->setPen(tmpC2);
//                                                         p->drawPoint(x+4,y);
//                                                         p->drawPoint(x+2,y+1);
//                                                         p->drawPoint(x+4,y+6);
//                                                         p->drawPoint(x+2,y+5);
							break;
						}
					}
					p->setBrush( oldBrush );
				}
				else
					KStyle::drawPrimitive( pe, p, r, cg, flags, opt );
			}
		}
	}

void LiquidStyle::drawKStylePrimitive( KStylePrimitive kpe, QPainter* p,
                                       const QWidget* widget, const QRect &r,
                                       const QColorGroup &cg, SFlags flags,
                                       const QStyleOption &opt ) const {
	switch ( kpe ) {
			case KPE_ToolBarHandle: {
                                if (widget && widget->parentWidget() && widget->parentWidget()->hasMouse()){
                                    int x, y, w, h;
                                    r.rect( &x, &y, &w, &h );
                                    w = (int)((w-3)/2);
                                    QColor c( optionHandler->BrushMe() ? cg.background() : cg.background().dark(120) );
                                    p->setPen(c);
                                    p->setBrush(c);
                                    for (int i = 0; i < h-3; i += 5 )
                                            p->drawEllipse(x+w,y+i,3,3);
                                }
				break;
				}
			case KPE_GeneralHandle: {
				optionHandler->prepareMenus();
				if ( widget->inherits( "AppletHandleDrag" ) ) {
					int x, y, w, h;
					r.rect( &x, &y, &w, &h );
					w = (int)((w-3)/2);
					QColor c( widget->hasMouse() ? cg.button() : cg.background().dark(110) );
					p->setPen(c);
					p->setBrush(c);
					for (int i = 0; i < h-3; i += 5 )
						p->drawEllipse(x+w,y+i,3,3);
					break;
				}
                                else if (widget->hasMouse()){
                                    if (optionHandler->BrushMe()){
                                        drawCombo( p, cg, cg.button(), r.x(), r.y(), r.width(), r.height()+2, false, false, false, center);
                                    }
                                    else {
                                        p->fillRect(r,cg.button());
					p->setPen(cg.button().dark( 100 + optionHandler->stippleContrast() ));
					int x=r.x();
					int y=r.y();
					int w=r.width();
					int h=r.height();
					for (int i = y;i < y+h; i += 4 ) {
						p->drawLine( x, i, x+w, i );
						p->drawLine( x, i + 1, x+w, i + 1 );
					}
                                    }
                                }
				break;
				}
			case KPE_SliderGroove: {
// 				QColor c = (optionHandler->drawGroupBoxShadow() && widget->parentWidget() && widget->parentWidget()->inherits("QGroupBox")/* && ((QGroupBox *)(widget->parentWidget()))->frameShape() == QFrame::GroupBoxPanel*/) ? cg.background().dark(100 + optionHandler->ShadowDarkness()) : widget->hasFocus() ? cg.background().dark( 120 ) : cg.background();
				QColor c = widget->hasFocus() ? cg.background().dark( 120 ) : cg.background();
				int x, y, w, h;
				r.rect( &x, &y, &w, &h );
				if (qstrcmp( qApp->argv() [ 0 ], "kmix" ) == 0) x = x+3;
				if ( ( ( const QSlider * ) widget ) ->orientation() == Qt::Horizontal ) {
					int x2 = x + w - 1;
					y = y + ( h - 5 ) / 2;
					p->setPen( c.dark( 130 ) );
					p->drawLine( x + 1, y, x2 - 1, y );
					p->setPen( c.dark( 150 ) );
					p->drawLine( x, y + 1, x2, y + 1 );
					p->setPen( c.dark( 125 ) );
					p->drawLine( x, y + 2, x2, y + 2 );
					p->setPen( c.dark( 130 ) );
					p->drawLine( x, y + 3, x2, y + 3 );
					p->setPen( c.dark( 120 ) );
					p->drawLine( x, y + 4, x2, y + 4 );
					p->setPen( c.light( 110 ) );
					p->drawLine( x + 1, y + 5, x2 - 1, y + 5 );
					} else {
					int y2 = y + h - 1;
					x = x + ( w - 5 ) / 2;
					p->setPen( c.dark( 130 ) );
					p->drawLine( x, y + 1, x, y2 - 1 );
					p->setPen( c.dark( 150 ) );
					p->drawLine( x + 1, y, x + 1, y2 );
					p->setPen( c.dark( 125 ) );
					p->drawLine( x + 2, y, x + 2, y2 );
					p->setPen( c.dark( 130 ) );
					p->drawLine( x + 3, y, x + 3, y2 );
					p->setPen( c.dark( 120 ) );
					p->drawLine( x + 4, y, x + 4, y2 );
					p->setPen( c.light( 110 ) );
					p->drawLine( x + 5, y + 1, x + 5, y2 - 1 );
					}
				break;
				}
			case KPE_SliderHandle: {
			if ((( const QSlider * ) widget ) ->orientation() == Qt::Horizontal)
				p->drawPixmap( r.x(), r.y()+2, *getPixmap( flags & Style_Enabled ? HSlider : HSliderInactive));
			else{
				bool right = (( const QSlider * ) widget )->tickmarks() == QSlider::NoMarks || (( const QSlider * ) widget )->tickmarks() == QSlider::Right;
				p->drawPixmap( /*(qstrcmp( qApp->argv() [ 0 ], "kmix" ) == 0) ? r.x() :*/ right ? r.x()+5 : r.x()-3, r.y(), *getPixmap( flags & Style_Enabled ? (right ? VSlider : VSliderAbove) : (right ? VSliderInactive : VSliderAboveInactive)));
			}
			break;
				}
			case KPE_ListViewExpander: {
// 				int radius = ( r.width() - 4 ) / 2;
// 				int centerx = r.x() + r.width() / 2;
// 				int centery = r.y() + r.height() / 2;
// 
// 				int red, green, blue;
// 				red = ( cg.dark().red() >> 1 ) + ( cg.base().red() >> 1 );
// 				green = ( cg.dark().green() >> 1 ) + ( cg.base().green() >> 1 );
// 				blue = ( cg.dark().blue() >> 1 ) + ( cg.base().blue() >> 1 );
// 				QColor blendColor( red, green, blue );
// 
// 				p->setPen( cg.dark() );
// 				p->drawLine( r.x() + 1, r.y(), r.right() - 1, r.y() );
// 				p->drawLine( r.x() + 1, r.bottom(), r.right() - 1, r.bottom() );
// 				p->drawLine( r.x(), r.y() + 1, r.x(), r.bottom() - 1 );
// 				p->drawLine( r.right(), r.y() + 1, r.right(), r.bottom() - 1 );
// 				p->setPen( blendColor );
// 				p->drawPoint( r.x(), r.y() );
// 				p->drawPoint( r.right(), r.y() );
// 				p->drawPoint( r.x(), r.bottom() );
// 				p->drawPoint( r.right(), r.bottom() );
// 				p->setPen( cg.light() );
// 				p->drawRect( r.x() + 1, r.y() + 1, r.width() - 2, r.height() - 2 );
// 				p->fillRect( r.x() + 2, r.y() + 2, r.width() - 4, r.height() - 4,
// 				             cg.background() );
// 				p->setPen( cg.text() );
// 				p->drawLine( centerx - radius, centery, centerx + radius, centery );
// 				if ( flags & Style_On )
// 					p->drawLine( centerx, centery - radius, centerx, centery + radius );
				break;
				}
			default:
			KStyle::drawKStylePrimitive( kpe, p, widget, r, cg, flags, opt );
		}
	}

void LiquidStyle::drawControl( ControlElement element, QPainter *p,
                               const QWidget *widget, const QRect &r,
                               const QColorGroup &cg, SFlags flags,
                               const QStyleOption &opt ) const {
	switch ( element ) {
			case CE_ProgressBarGroove: {
					p->setPen(cg.background().dark(160));
				        p->drawRect(r);
//					if (/*optionHandler->useLiquidProgressBar()*/false){
//				        	p->fillRect(r.x()+1, r.y()+1, r.width()-2, r.height()-2, cg.base());
/*						int y = r.y()+(int)((r.height()-20)/2);
						QPixmap *pix = getPixmap(Progress);
						p->drawTiledPixmap(r.x(), y+1, r.width()-1, 20, *pix);*/
//					}
//					else{
						drawCombo( p, cg, cg.background(), r.x()+1, r.y()+1, r.width()-2, r.height()+1, false, false, false, center );
//					}
//				QPen oldPen(p->pen());
//				p->setPen(cg.background().dark(160));
//				p->drawRect(r);
//				p->setPen(oldPen);
//				drawClearBevel( p, r.x() + 1, r.y() + 1, r.width() - 2, r.height() - 2, cg.background(), cg.background() );
				break;
				}
			case CE_ProgressBarContents: {
				const QProgressBar *progress = ( const QProgressBar * ) widget;
				QRect contentsR( subRect( SR_ProgressBarContents, widget ) );
				double val = progress->progress();
				bool reverse = QApplication::reverseLayout();
				val = val / progress->totalSteps();
				if ( val > 0.0 ) {
					int w = QMIN( contentsR.width(), ( int ) (val * contentsR.width() ) );
					if ( w > 1 ) {
//						progress->progress() < 0 ? activeProgress = NULL : activeProgress = progress;
						int x = contentsR.x();
						int y = contentsR.y();
						int x2 = contentsR.x() + w - 1;
						int y2 = contentsR.bottom();
//						if ( w > 2 ) {
							QRect progressRect;
							if (reverse)
								progressRect = QRect(contentsR.x()+(contentsR.width()-w), contentsR.y(), w, contentsR.height()+1);
							else
								progressRect = QRect(contentsR.x(), contentsR.y(), w, contentsR.height()+1);


							
							if (optionHandler->ProgressBar() == liquid){
								//Clip to the old rectangle
								p->setClipRect(progressRect, QPainter::CoordPainter);
								//Expand
								progressRect.setLeft(progressRect.x() - progAnimShift);
								p->setPen(cg.button().dark(120));
								p->drawLine(x, y, x2, y);
								p->drawLine(x, y, x, y2);
								p->drawLine(x+1, y2+1, x2-1, y2+1);
								p->setPen(cg.button().dark(110));
								p->drawLine(x2, y, x2, y2);
								p->drawLine(x, y2, x2, y2);
								QPixmap * pix = getPixmap( Progress );
								p->drawTiledPixmap(/*x+1, y+1, w-2, contentsR.height()-2*/progressRect, *pix);
							}
							else if (optionHandler->ProgressBar() == baghira) {
								drawClearBevel( p, r.x(), r.y(), w - 1, contentsR.height()+1, cg.button(), cg.button() );
							}
							else{
								ButtonTile *progress2Tile = progress2Dict.find( cg.button().rgb() );
								if ( !progress2Tile ) {
									QPixmap *pix = getPixmap(progress2);
									progress2Tile = separateTiles( pix, 0, 15, 38, 1, 0, true, false );
								}
								if ( !progress2Tile ) {
									qWarning( "Tab tile is NULL!" );
									return ;
								} else{
									LiquidStyle *ptr = const_cast<LiquidStyle*>( this );
									ptr->progress2Dict.insert( cg.button().rgb(), progress2Tile );
								}
								//Clip to the old rectangle
								p->setClipRect(progressRect, QPainter::CoordPainter);
								//Expand
								progressRect.setLeft(progressRect.x() - 38 + prog2AnimShift);
								int x = progressRect.x();int y = progressRect.y();int w = progressRect.width();int h = progressRect.height();
								bool tooSmall = h < 32;
								int mid = h/2;
								p->drawTiledPixmap(x,tooSmall ? mid + 1 : progressRect.bottom()-15,w,16, *progress2Tile->pixmap(TileBtm));
								p->drawTiledPixmap(x,tooSmall ? mid : y+15,w,tooSmall ? 1 : h-31, *progress2Tile->pixmap(TileMiddle));
								p->drawTiledPixmap(x,tooSmall ? mid - 15 : y,w,15, *progress2Tile->pixmap(TileTop));
//								p->setPen(cg.background().dark(160));
//				        			p->drawLine(r.x()+1,r.y()+contentsR.height()+1,r.x()+w-1,r.y()+contentsR.height()+1);
//								p->setPen(cg.button().dark(140));
//								p->drawLine(r.x()+w-1,r.y()+2,r.x()+w-1,r.y()+contentsR.height()-2);
							}
//						}
					}
				}
				break;
			}
			case CE_ProgressBarLabel: {
				if (optionHandler->DrawProgressLabel()){
					QRect contentsR( subRect( SR_ProgressBarContents, widget ) );
					if ( !contentsR.isValid() )
						return ;
					QFont font( p->font() );
					font.setBold( true );
					p->setFont( font );
					p->setPen( widget->colorGroup().buttonText() );
					p->setClipRect(r, QPainter::CoordPainter);
					p->drawText( contentsR, AlignCenter, ( ( const QProgressBar * ) widget ) ->progressString() );
				}
				break;
			}

			case CE_TabBarTab: { // new style with tiled pixmaps
				if ( !widget || !widget->parentWidget(true) )
					break;
				const QTabBar *tabBar = ( const QTabBar * ) widget;
				bool selected = flags & Style_Selected;
				bool hover = currentTabBar && currentTabBar == tabBar && tabBar->indexOf(opt.tab()->identifier()) == hoveredTabIndex;
				
				myPosition pos;
				if (tabBar->count() == 1)
					pos = full;
				else if ((tabBar->indexOf(opt.tab()->identifier()) == 0))
					pos = left;
				else if (tabBar->indexOf(opt.tab()->identifier()) == tabBar->count() - 1)
					pos = right;
				else 
					pos = center;
				
				QColor c;
// 				if (hover){
// 					if (optionHandler->useCustomColors())
// 						c = optionHandler->customColor(CustomTabOn).light(110);
// 					else
// 						c = optionHandler->buttonColor().light(110);
// 				}
				if (hover || selected){
					if (optionHandler->useCustomColors())
						c = optionHandler->customColor(CustomTabOn);
					else
						c = optionHandler->buttonColor();
				}
				else{
					if (optionHandler->useCustomColors())
						c = optionHandler->customColor(CustomTabOff);
					else
						c = optionHandler->InactiveButtonColor();
				}
				
				bool isDialog = optionHandler->TabStyle() == Clever && widget->topLevelWidget()->inherits("QDialog");
				if (isDialog || optionHandler->TabStyle() == Chooser){
				drawCombo( p, cg, c, r.x(), r.y(), r.width(), r.height(), false, false, false, pos );
				}
				else {
					LiquidStyle *ptr = const_cast<LiquidStyle*>( this );
					bool above = tabBar->shape() == QTabBar::RoundedAbove || tabBar->shape() == QTabBar::TriangularAbove;
//					bool below = tabBar->shape() == QTabBar::RoundedBelow || tabBar->shape() == QTabBar::TriangularBelow;
					QPixmap *pix;
					if (above)
						pix = selected ? getPixmap( TabDown ) : getPixmap( Tab );
					else
						pix = selected ? getPixmap( belowTabDown ) : getPixmap( belowTab );
					ButtonTile *tabTile = above ? tabDict.find( c.rgb() ) : inverseTabDict.find(c.rgb());
					if ( !tabTile ) {
						tabTile = separateTiles( pix, 9, 15, 67, 1, 0, false, true );
						}
					if ( !tabTile ) {
						qWarning( "Tab tile is NULL!" );
						return ;
						} else
						above ? ptr->tabDict.insert( c.rgb(), tabTile ) : ptr->inverseTabDict.insert( c.rgb(), tabTile );
	
					int offset;
					selected ? offset = 0 : offset = 2;
	
					if ( r.width() > 18 ) {
						p->drawTiledPixmap( r.x() + 9, r.y() + offset, r.width() - 18, 15, *tabTile->pixmap( TileTop ) );
						p->drawTiledPixmap( r.x() + 9, r.height() - 11, r.width() - 18, 11, *tabTile->pixmap( TileBtm ) );
						}
					if ( r.height() + offset > 26 ) {
						p->drawTiledPixmap( r.x() + 0, r.y() + 15 + offset, 9, r.height() - 26 - offset, *tabTile->pixmap( TileLeft ) );
						p->drawTiledPixmap( r.right() - 8, r.y() + 15 + offset, 9, r.height() - 26 - offset, *tabTile->pixmap( TileRight ) );
						}
					if ( r.width() > 18 && r.height() + offset > 22 )
						p->drawTiledPixmap( r.x() + 9, r.y() + 15 + offset, r.width() - 18, r.height() - 26 - offset, *tabTile->pixmap( TileMiddle ) );
					p->drawPixmap( r.x() + 0, r.y() + offset, *tabTile->pixmap( TileTopLeft ) );
					p->drawPixmap( r.right() - 8, r.y() + offset, *tabTile->pixmap( TileTopRight ) );
					p->drawPixmap( r.x() + 0, r.height() - 11, *tabTile->pixmap( TileBtmLeft ) );
					p->drawPixmap( r.right() - 8, r.height() - 11, *tabTile->pixmap( TileBtmRight ) );
	
 					p->setPen( optionHandler->buttonColor());
					bool drawPoint = optionHandler->CenterTabs() || pos == center || pos == right;
					if ( selected ) {
						if (drawPoint) p->drawPoint(r.x(), above ? r.bottom() : r.y());
						p->drawPoint(r.right(), above ? r.bottom() : r.y());
 					}
					else{
						p->drawLine( r.x() + drawPoint ? 0 : 1, above ? r.bottom() : r.y(), r.right(), above ? r.bottom() : r.y() );
 						p->setPen( optionHandler->buttonColor().dark( 158 ));
						p->drawLine( r.x() + drawPoint ? 0 : 1, above ? r.bottom()-1 : r.y()+1, r.right(), above ? r.bottom()-1 : r.y() + 1 );
					}
				}
				break;
				}

			case CE_TabBarLabel: {
				if ( opt.isDefault() )
					return ;
				QTab *t = opt.tab();
				const QTabBar * tb = ( const QTabBar * ) widget;
//				int vertOffs = 1;
//				int horOffs = 1;
//				int y = t->rect().y();
				/*        if((flags & Style_HasFocus) && !t->text().isEmpty()){
				            const QColor tmp(cg.button().dark(130));
				            drawItem(p, QRect(r.x()+1, r.y()+1, r.width(), r.height()), AlignCenter | ShowPrefix, cg, flags & Style_Enabled, 0, t->text(), -1, &tmp);
				            drawItem(p, QRect(r.x()+2, r.y()+2, r.width(), r.height()), AlignCenter | ShowPrefix, cg, flags & Style_Enabled, 0, t->text(), -1, &tmp);
				        }*/
				if ( ( tb->currentTab() != t->identifier() )){
					if (optionHandler->TabStyle() == TrueTab ||
					(optionHandler->TabStyle() == Clever && !(widget->topLevelWidget()->inherits("QDialog")))){
						drawItem( p, QRect( r.x(), r.y() + 1, r.width(), r.height() ), AlignCenter | ShowPrefix, cg, flags & Style_Enabled, 0, t->text() );
						break;
					}
				}
				drawItem( p, QRect( r.x(), r.y(), r.width(), r.height() ), AlignCenter | ShowPrefix, cg, flags & Style_Enabled, 0, t->text() );
				break;
				}
			case CE_PushButton: {
				const QPushButton *btn = ( const QPushButton* ) widget;
				const_cast<LiquidStyle*>( this ) ->isHTMLButton = isHTMLWidget( btn );
				if ( widget == highlightWidget )
					flags |= Style_MouseOver;
				// Qt messes this up with WindowOrigin, so we do it ourselves :P
				if ( !isPlain() && !btn->autoMask() )
					const_cast<LiquidStyle*>( this ) ->btnOffset = btn->backgroundOffset();



				if ( btn->isDefault() )
					drawPrimitive( PE_ButtonDefault, p, r, cg, flags );
				else
					drawPrimitive( PE_ButtonCommand, p, r, cg, flags );
				break;
				}
			case CE_PushButtonLabel: {
				const QPushButton *btn = ( const QPushButton* ) widget;
				int x, y, w, h;
				r.rect( &x, &y, &w, &h );
				bool highlighted = btn->isOn() || btn->isDown() || btn->hasFocus() || btn == highlightWidget;

				if ( btn->isOn() || btn->isDown() ) {
					flags |= Style_Sunken;
					}

				if ( btn->isMenuButton() ) {
					drawPrimitive( PE_ArrowDown, p, QRect( w-14, (int)(h/2), 7, 7 ) , cg, flags, opt );
					w -= 14;
					}
				if ( btn->iconSet() && !btn->iconSet() ->isNull() ) {
					QIconSet::Mode mode = btn->isEnabled() ? QIconSet::Normal : QIconSet::Disabled;
					if ( mode == QIconSet::Normal && btn->hasFocus() )
						mode = QIconSet::Active;

					QIconSet::State state = QIconSet::Off;
					if ( btn->isToggleButton() && btn->isOn() )
						state = QIconSet::On;

					QPixmap pix( btn->iconSet() ->pixmap( QIconSet::Small, mode, state ) );
					int pixW = pix.width();
					int pixH = pix.height();
					(btn->parent() && ::qt_cast<QTabBar*>(btn->parent()) /*btn->parent()->inherits("QTabBar")*/) ?
						p->drawPixmap( x, y + ( h - pixH ) / 2, pix ) :
						p->drawPixmap( x + 4, y + ( h - pixH ) / 2, pix );
					x += pixW + 4;
					w -= pixW + 4;
					}

				QColor tmp;
				if(!(btn->isDown())){ //shadow
					tmp = (highlighted) ? btn->colorGroup().button().dark(130) : optionHandler->InactiveButtonColor().dark(130);
					drawItem(p, QRect(x, y+1, w, h), AlignCenter | ShowPrefix, cg, btn->isEnabled(), btn->pixmap(), btn->text(), -1, &tmp);
				}
				tmp = btn->isDown() && !optionHandler->IcyButtons() ? btn->colorGroup().highlightedText() : btn->colorGroup().buttonText();
				drawItem( p, QRect( x, y, w, h ), AlignCenter | ShowPrefix, cg, btn->isEnabled(), btn->pixmap(), btn->text(), -1, &tmp );
				break;
				}
			case CE_MenuBarItem: {
// 					qWarning("device is %s", (p->device()->devType() == QInternal::Widget) ?
// 									"Widget":
// 								 (p->device()->devType() == QInternal::Pixmap) ?
// 									"Pixmap":
// 								 (p->device()->devType() == QInternal::Printer) ?
// 									"Printer":
// 								 (p->device()->devType() == QInternal::Picture) ?
// 									"Picture":
// 								 (p->device()->devType() == QInternal::UndefinedDevice) ?
// 									"UndefinedDevice":
// 									"fuckdevice!" );
				QMenuItem *mi = opt.menuItem();
				bool active = flags & Style_Active;
				bool focused = flags & Style_HasFocus;

				if ( active && focused ) {
					optionHandler->daddyWasAPopup = false;
					p->fillRect(r,cg.highlight());
				}

				QColor tmpColor( cg.background().dark( 120 ) );
				if ( optionHandler->useShadowText() && !( active && focused )) {
					drawItem( p, QRect( r.x() + 1, r.y() + 1, r.width(), r.height() ), AlignCenter | AlignVCenter | ShowPrefix | DontClip | SingleLine, cg, flags & Style_Enabled, mi->pixmap(), mi->text(), -1, &tmpColor );
					}
				drawItem( p, r, AlignCenter | AlignVCenter | ShowPrefix | DontClip | SingleLine, cg, flags & Style_Enabled, mi->pixmap(), mi->text(), -1, ( active && focused ) ? &(cg.highlightedText()) : &(cg.text()) );
				break;
				}
			case CE_PopupMenuItem: {
			
				int x,x1, y,y1, w, h;
				r.rect( &x1, &y1, &w, &h );

				const QPopupMenu *popupmenu = ( const QPopupMenu * ) widget;
				QMenuItem *mi = opt.menuItem();
				
				int tab = opt.tabWidth();
				int maxpmw = opt.maxIconWidth();
				bool dis = mi && !mi->isEnabled();
				bool checkable = popupmenu->isCheckable();
				bool act = flags & Style_Active;
				bool enabled = flags & Style_Enabled;
				int checkcol = maxpmw;

				optionHandler->prepareMenus();
				QColor menuBgColor(  optionHandler->UseCustomMenuColor() ? optionHandler->Color() : optionHandler->bgColor() );

				if ( checkable )
					checkcol = QMAX( maxpmw, 20 );

				if ( mi && mi->isSeparator() ) {
					p->setPen( menuBgColor.dark( 130 ) );
					p->drawLine( x1 + 1, y1, x1 + w - 1, y1 );
					p->setPen( menuBgColor.light( 120 ) );
					p->drawLine( x1+1, y1 + 1, x1 + w - 1 , y1 + 1 );
					return ;
					}
				
				// if we have an item for childs, we'll have to do some store stuff for providing indicators under the shadow ;-)
				QPaintDevice *oldPaintDevice = NULL;
				if (mi && mi->popup()){
					QFont oldFont;
					if (act && enabled){
						x = 0;y=0;
						oldFont = p->font();
						oldPaintDevice = p->device();
						if (optionHandler->indicatedMenuItemPix)
							optionHandler->indicatedMenuItemPix->resize(r.width(),r.height());
						else
							optionHandler->indicatedMenuItemPix = new QPixmap(r.width(),r.height());
						p->end();
						p->begin(optionHandler->indicatedMenuItemPix);
						p->setFont(oldFont);
					}
					else if ((WId)(mi->id()) == optionHandler->lastIndicatedMenuItemID){
						x = 0;y=0;
						oldFont = p->font();
						oldPaintDevice = p->device();
						if (optionHandler->lastIndicatedMenuItemPix)
							optionHandler->lastIndicatedMenuItemPix->resize(r.width(),r.height());
						else
							optionHandler->lastIndicatedMenuItemPix = new QPixmap(r.width(),r.height());
						p->end();
						p->begin(optionHandler->lastIndicatedMenuItemPix);
						p->setFont(oldFont);
					}
					else {
						x = x1;
						y = y1;
					}
				}
				else{
					x = x1;
					y = y1;
				}
				
//				p->fillRect(x, y, _STRIPEWIDTH_, h, Qt::white);
				
				if ( act && mi && enabled){
					optionHandler->daddyWasAPopup = false;
					p->fillRect(x,y,w,h,cg.highlight());
				}
				else if ( (optionHandler->MenuTranslucency() >= DefaultTrans) && (!popupmenu->parent() || !(popupmenu->parent()->inherits("KPopupMenu")) || popupmenu->parent()->inherits("KPanelMenu"))) {
					PopupStuff *popupStuff = optionHandler->popup( popupmenu->winId() );
					KPixmap * pixmap = popupStuff ? popupStuff->pix : NULL;
					if ( pixmap ) {
						int tmp = popupStuff ? popupStuff->leftOffset : 0;
//						qWarning("tmp: %d",tmp);
						p->drawPixmap( x, y, *pixmap, x1+ (tmp  > optionHandler->ShadowOffset() ? optionHandler->ShadowOffset() : tmp), y1, w, h );
					}
					// fallback
					else if (optionHandler->StippleMenu()){
						p->drawTiledPixmap( x, y, w, h, *optionHandler->bgPixmap(), x, y );
					}
					else{
						p->fillRect( x, y, w, h, menuBgColor );
					}
					// end fallback
					if (act){
						p->setPen(cg.highlight());
						p->drawRect(x,y,w,h);
					}
				}
				else if ( optionHandler->StippleMenu() ) {
					p->drawTiledPixmap( x, y, w, h, *optionHandler->bgPixmap(), x, y );
					if (act){
						p->setPen(cg.highlight());
						p->drawRect(x,y,w,h);
					}
				}
				else {
					p->fillRect( x, y, w, h, menuBgColor );
					if (act){
						p->setPen(cg.highlight());
						p->drawRect(x,y,w,h);
					}
				}
				if ( !mi )
					return ;

				// Menu contents drawing code based on Qt's styles. Qt is property of
				// TrollTech: www.trolltech.com. Used with permission.
				int xpos = x;
				QRect vrect = visualRect( QRect( xpos, y, checkcol, h ), r );
				int xvis = vrect.x();

				// checkbox
/*				if ( mi->isChecked() ) {
					if ( act && !dis )
						qDrawShadePanel( p, xvis, y, checkcol, h,
						                 cg, TRUE, 1, &cg.brush( QColorGroup::Button ) );
					else {
						qDrawShadePanel( p, xvis, y, checkcol, h, cg, TRUE, 1 );
						}
					}*/

				// icon
				if ( mi->iconSet() ) {
					QIconSet::Mode mode = dis ? QIconSet::Disabled : QIconSet::Normal;
					if ( act && !dis )
						mode = QIconSet::Active;
					QPixmap pixmap;
					if ( checkable && mi->isChecked() )
						pixmap = mi->iconSet() ->pixmap( QIconSet::Small, mode, QIconSet::On );
					else
						pixmap = mi->iconSet() ->pixmap( QIconSet::Small, mode );
					int pixw = pixmap.width();
					int pixh = pixmap.height();
					QRect pmr( 0, 0, pixw, pixh );
					pmr.moveCenter( vrect.center() );
					p->setPen( cg.text() );
					p->drawPixmap( pmr.topLeft(), pixmap );
				}
				else if ( checkable ) {
					if ( mi->isChecked() ) {
						int xp = xpos + windowsItemFrame;

						SFlags cflags = Style_Default;
						if ( !dis )
							cflags |= Style_Enabled;
						if ( act )
							cflags |= Style_On;
						drawPrimitive( PE_CheckMark, p, visualRect( QRect( xp, y + windowsItemFrame, checkcol - 2 * windowsItemFrame, h - 2 * windowsItemFrame ), r ), cg, cflags );
						}
					}
				QColor discol;
				if ( dis ) {
					discol = cg.text();
					p->setPen( discol );
					}

				int xm = windowsItemFrame + checkcol + windowsItemHMargin;
				xpos += xm;

				vrect = visualRect( QRect( xpos, y + windowsItemVMargin,
				                           w - xm - tab + 1, h - 2 * windowsItemVMargin ), r );
				xvis = vrect.x();
				if ( mi->custom() ) {
					p->save();
					if ( dis && !act ) {
						p->setPen( cg.light() );
						mi->custom() ->paint( p, cg, act, !dis,
						                      xvis + 1, y + windowsItemVMargin + 1,
						                      w - xm - tab + 1, h - 2 * windowsItemVMargin );
						p->setPen( discol );
						}
					mi->custom() ->paint( p, cg, act, !dis, xvis, y + windowsItemVMargin,
					                      w - xm - tab + 1, h - 2 * windowsItemVMargin );
					p->restore();
					}

				// Text
				QString s = mi->text();
				if ( !s.isNull() ) {
					QColor textColor, shadowColor;
					shadowColor = optionHandler->bgColor().dark( 130 );

					if ( dis )
						textColor = shadowColor.light( 115 );
					else if (act && mi)
						textColor = cg.highlightedText();
					else if ( optionHandler->UseCustomMenuColor())
						textColor = optionHandler->textColor();
					else
						textColor = cg.text();

					int t = s.find( '\t' );
					int text_flags = AlignVCenter | ShowPrefix | DontClip | SingleLine;
					text_flags |= ( QApplication::reverseLayout() ? AlignRight : AlignLeft );
					if ( t >= 0 ) {                         // draw tab text
						int xp;
						xp = x + w - tab - 20 - windowsItemHMargin - windowsItemFrame + 1;
						QString tabStr( s.right( s.length() - t - 1 ) );
						if ( !tabStr.isEmpty() ) {
							if ( optionHandler->useShadowText() ) {
								p->setPen( shadowColor );
								p->drawText( xp + 1, y + windowsItemVMargin + 1, w - xm - tab + 1, h - 2 * windowsItemVMargin, text_flags, tabStr );
							}
							p->setPen( textColor );
							p->drawText( xp, y + windowsItemVMargin, w - xm - tab + 1, h - 2 * windowsItemVMargin, text_flags, tabStr );
						}
						s = s.left( t );
						}
					if ( optionHandler->useShadowText() && !(act && mi) ) {
						p->setPen( shadowColor );
						p->drawText( xvis + 1, y + windowsItemVMargin + 1, w - xm - tab + 1, h - 2 * windowsItemVMargin, text_flags, s, t );
					}
					p->setPen( textColor );
					p->drawText( xvis, y + windowsItemVMargin, w - xm - tab + 1, h - 2 * windowsItemVMargin, text_flags, s, t );
				}
				else if ( mi->pixmap() ) {                        // draw pixmap
					QPixmap * pixmap = mi->pixmap();
					if ( pixmap->depth() == 1 )
						p->setBackgroundMode( OpaqueMode );
					p->drawPixmap( xvis, y + windowsItemFrame, *pixmap );
					if ( pixmap->depth() == 1 )
						p->setBackgroundMode( TransparentMode );
				}

				// Child menu
				if ( mi->popup() ) {
					int dim = (int)((float)( h - 2 * windowsItemFrame ) / 2);
					PrimitiveElement arrow;
					arrow = ( QApplication::reverseLayout() ? PE_ArrowLeft : PE_ArrowRight );
					xpos = x + w - windowsArrowHMargin - windowsItemFrame/* - dim*/-8;
					vrect = visualRect( QRect( xpos, y + h / 2 - dim / 2, /*dim*/8, dim ), r );
					if ( act ) {
						if ( !dis )
							discol = cg.highlightedText();
						QColorGroup g2( discol, cg.highlight(), white, white, dis ? discol : white, discol, white );
						drawPrimitive( arrow, p, vrect, g2, Style_Enabled );
					if (enabled){
					optionHandler->lastIndicatedMenuItemID = mi->id();
					*(optionHandler->lastIndicatedMenuItemRect) = *(optionHandler->indicatedMenuItemRect);
					optionHandler->indicatedMenuItemRect->setRect(r.x()+popupmenu->x(),r.y()+popupmenu->y(),r.width(),r.height());
					optionHandler->daddyWasAPopup = true;
					p->end();
					p->begin(oldPaintDevice);
					p->drawPixmap(r.x(),r.y(),*optionHandler->indicatedMenuItemPix);
					}
					}
					else {
						if ( !dis )
							discol = optionHandler->textColor();
/*							p->setPen(dis ? discol : white);
							p->setBrush(dis ? discol : white);
							p->drawPixmap(vrect.x(), vrect.y(), *sbRight);*/
						QColorGroup g2( discol, cg.background(), white, white, dis ? discol : white, discol, white );
						drawPrimitive( arrow, p, vrect, g2, mi->isEnabled() ? Style_Enabled : Style_Default );
						if ((WId)(mi->id()) == optionHandler->lastIndicatedMenuItemID){
							p->end();
							p->begin(oldPaintDevice);
							p->drawPixmap(r.x(),r.y(),*optionHandler->lastIndicatedMenuItemPix);
						}
						
					}
				}
				break;
			}
			default:
			KStyle::drawControl( element, p, widget, r, cg, flags, opt );
		}
	}


// void LiquidStyle::drawControlMask( ControlElement element, QPainter *p, const QWidget *widget, const QRect &r, const QStyleOption& opt ) const {
// 	switch ( element ) {
// 			case CE_PushButton: {
// 				p->fillRect( r, Qt::color1 );
// 				break;
// 				}
// 			default:
// 			KStyle::drawControlMask( element, p, widget, r, opt );
// 			break;
// 		}
// 	}

// void LiquidStyle::drawComplexControlMask( ComplexControl control, QPainter *p, const QWidget *widget, const QRect &r, const QStyleOption &opt ) const {
// // 	switch ( control ) {
// // 			case CC_ComboBox: {
// // 				drawControlMask( CE_PushButton, p, widget, r, opt );
// // 				break;
// // 				}
// // 			default:
// 			KStyle::drawComplexControlMask( control, p, widget, r, opt );
// //		}
// 	}

void LiquidStyle::drawComplexControl( ComplexControl control, QPainter *p, const QWidget *widget, const QRect &r, const QColorGroup &cg, SFlags flags, SCFlags controls, SCFlags active, const QStyleOption &opt ) const {
	switch ( control ) {
			case CC_ComboBox: {
                                bool sunken = (active & SC_ComboBoxArrow);// || (flags & Style_Active) || (flags & Style_Sunken) || (active & SC_ComboBoxListBoxPopup);
                                bool hovered = (widget == highlightWidget) || (flags & Style_HasFocus);
                                bool isHtml = isHTMLWidget( widget ); 
				QPen oldPen( p->pen() );
				if ( controls & SC_ComboBoxArrow ) {
                                    if ( controls & SC_ComboBoxEditField && ( ( const QComboBox * ) widget ) ->editable()) { // editable?
                                        if (widget->parentWidget() && ::qt_cast<QToolBar*>(widget->parentWidget()) || widget->parentWidget()->inherits("KPanelApplet")){ // toolbar combo?
                                            drawRoundFrame( p, r, cg, (flags & Style_HasFocus) || sunken , widget->backgroundOffset());
                                            p->setPen(cg.mid());
                                            p->drawPixmap(r.right() - 16, r.height()/2 - 2, *sbDown);
                                            p->setPen( oldPen );
                                            return;
                                        }
                                        else if ( isHtml || optionHandler->ForceRectButton()) { // rectangular?
                                            drawRectangularButton( p, cg, sunken ? cg.button().light( 110 ) : cg.button(), r.x(), r.y(), r.width(), r.height(), sunken, hovered, true, full, isHtml);
                                            p->fillRect( r.x() + 6, r.y() +2, r.width()-32, r.height() - 7, cg.background());
                                            p->setPen( oldPen );
                                            return ;
					}
                                        else{ // default!
                                            drawCombo( p, cg, active == SC_ComboBoxArrow ? cg.button().light( 110 ) : cg.button(), r.x(), r.y(), r.width(), r.height(), sunken, hovered, true, full );
                                            p->fillRect( r.x() + 6, r.y() +2, r.width()-32, r.height() - 7, cg.background());
                                            p->setPen( oldPen );
                                            return;
                                        }
                                    }
                                    else { // end editable - default handling
                                        if ( isHtml || optionHandler->ForceRectButton()) { // retangular?
                                            drawRectangularButton( p, cg, sunken ? cg.button().light( 110 ) : cg.button(), r.x(), r.y(), r.width(), r.height(), sunken, hovered, true, full, isHtml);
                                            p->setPen( oldPen );
                                            return ;
					}
                                        else { //default plain good old combobox... ;)
                                            drawCombo( p, cg, active == SC_ComboBoxArrow ? cg.button().light( 110 ) : cg.button(), r.x(), r.y(), r.width(), r.height(), sunken, hovered, true, full );
                                            p->setPen( oldPen );
                                            return;
                                        }
                                    }
				}
				break;
			}
			case CC_ToolButton: {
				const QToolButton *toolbutton = ( const QToolButton * ) widget;
                                QRect button, menuarea;
				button = querySubControlMetrics( control, widget, SC_ToolButton, opt );
				menuarea = querySubControlMetrics( control, widget, SC_ToolButtonMenu, opt );
				menuarea.setX(menuarea.x() - 1);
				SFlags bflags = flags,
				                mflags = flags;

				if ( active & SC_ToolButton )
					bflags |= Style_Down;
				if ( active & SC_ToolButtonMenu )
					mflags |= Style_Down;

				if ( widget == highlightWidget )
					bflags |= Style_MouseOver;
				if ( controls & SC_ToolButton ) {
					bool sunken = mflags & ( Style_Down | Style_On );
					bool hover = bflags & Style_MouseOver;
                                        
                                        if ( !optionHandler->useToolButtonFrame() || !(toolbutton->parentWidget() && ( ::qt_cast<QToolBar*>(toolbutton->parentWidget()) || toolbutton->parentWidget()->inherits("KonqFrameTabs")))) {
                                            if (hover || sunken){
                                                if (optionHandler->SmoothToolbuttons())
                                                    drawCombo( p, cg, sunken ? cg.button().light( 110 ) : (hover ||  (cg.button() != qApp->palette().active().button())) ? cg.button() : optionHandler->InactiveButtonColor(), button.x(), button.y(), button.width(), button.height()+2, sunken, hover, false, full );
                                                else
                                                    drawRectangularButton( p, cg, sunken ? cg.button().light( 110 ) : cg.button(), button.x(), button.y(), button.width(), button.height(), sunken, hover, false);
                                            }                                        
                                            else if (toolbutton->parentWidget() && !::qt_cast<QToolBar*>(toolbutton->parent()) && toolbutton->parentWidget()->paletteBackgroundPixmap() && !toolbutton->parentWidget()->paletteBackgroundPixmap()->isNull()) {
//                                                 qWarning("%s",toolbutton->parentWidget()->className());
                    				QPixmap pixmap = *(toolbutton->parentWidget()->paletteBackgroundPixmap());
                    				p->drawTiledPixmap( r, pixmap, toolbutton->pos() );
				            }
					}
					else{
						myPosition pos = full;
						KToolBar *bar = NULL;
						
						if (::qt_cast<KToolBarButton*>(toolbutton)/*toolbutton->inherits("KToolBarButton")*/){ //position calculation - would be nice if ktoolbarbutton provided this as flag
							const KToolBarButton *toolbutton2 = (const KToolBarButton*)toolbutton;
							bar = (KToolBar*)toolbutton2->parentWidget();
							int buttonId = toolbutton2->id();
							int buttonIndex = bar->itemIndex(buttonId);
							
							// ok, this is a very hackish heck of a hack...
							// kbookmarkbar (e.g. used by konqueror) accesses the iterator of the toolbar
							// this confuses the iterator of the toolbar a bit, as it is used from the bookmark stuff and my function at the same time (seems, as if kbookmarkbar acts on shown toolbars)
							// unfortunately, there's no way to figure out if it does for this specific toolbar
							// so i check for the toolbars name, knowing this will work for current konqueror - but may fail on further versions
							// even worse - the stuff is hardcoded (assuming first item to be left aligned, last one right and the rest centered)
							// this is crap at all - so if the best thing would be to patch qtoolbutton to provide conjuncted buttons - i'll ask them (maybe qt4)
							if (bar && ( qstrcmp( bar->name(), "bookmarkToolBar" ) == 0 )){
								buttonIndex == 0 ?
									pos = left :
									buttonIndex == bar->count() - 1 ?
									pos = right :
									pos = center;
							}
							else if (bar->orientation() == Qt::Horizontal){
								if (buttonIndex == 0){
									KToolBarButton *tmpButton = bar->getButton(bar->idAt(buttonIndex+1));
									(tmpButton && tmpButton->x() == toolbutton2->x()+toolbutton2->width()) ? pos = left : pos = full;
								}
								else if (buttonIndex == bar->count() - 1){
									KToolBarButton *tmpButton = NULL;
									tmpButton = bar->getButton(bar->idAt(buttonIndex-1));
									(tmpButton && tmpButton->x() + tmpButton->width() == toolbutton2->x()) ? pos = right : pos = full;
								}
								else{
									KToolBarButton *tmpButton1 = bar->getButton(bar->idAt(buttonIndex-1));
									KToolBarButton *tmpButton2 = bar->getButton(bar->idAt(buttonIndex+1));
									tmpButton1 && tmpButton1->x() + tmpButton1->width() == toolbutton2->x() ?
																pos = center :
																pos = left;
									if (!(tmpButton2 && tmpButton2->x() == toolbutton2->x()+toolbutton2->width()))
										pos == left ?  pos = full :  pos = right;
								}
							}
							else{
								if (buttonIndex == 0){
									KToolBarButton *tmpButton = bar->getButton(bar->idAt(buttonIndex+1));
									(tmpButton && tmpButton->y() == toolbutton2->y()+toolbutton2->height()) ? pos = left : pos = full;
								}
								else if (buttonIndex == bar->count() - 1){
									KToolBarButton *tmpButton = NULL;
									tmpButton = bar->getButton(bar->idAt(buttonIndex-1));
									(tmpButton && tmpButton->y() + tmpButton->height() == toolbutton2->y()) ? pos = right : pos = full;
								}
								else{
									KToolBarButton *tmpButton1 = bar->getButton(bar->idAt(buttonIndex-1));
									KToolBarButton *tmpButton2 = bar->getButton(bar->idAt(buttonIndex+1));
									tmpButton1 && tmpButton1->y() + tmpButton1->height() == toolbutton2->y() ?
																pos = center :
																pos = left;
									if (!(tmpButton2 && tmpButton2->y() == toolbutton2->y()+toolbutton2->height()))
										pos == left ?  pos = full :  pos = right;
								}
							}
						}

						if (optionHandler->SmoothToolbuttons()){
							(bar && bar->orientation() == Qt::Vertical) ?
							drawCombo( p, cg, sunken ? cg.button().light( 110 ) : (hover ||  (cg.button() != qApp->palette().active().button())) ? cg.button() : optionHandler->InactiveButtonColor(), button.x(), (pos == full || pos == left) ? button.y() : button.y() - 7, button.width(), (pos == full) ?
													button.height()+2 :
													(pos == center) ?
													button.height()+16 :
													button.height()+9,
													sunken, hover, false, center ) :
							drawCombo( p, cg, sunken ? cg.button().light( 110 ) : (hover ||  (cg.button() != qApp->palette().active().button())) ? cg.button() : optionHandler->InactiveButtonColor(), button.x(), button.y(), button.width(), button.height()+2, sunken, hover, false, pos );
						}
						else{
							(bar && bar->orientation() == Qt::Vertical) ?
							drawRectangularButton( p, cg, sunken ? cg.button().light( 110 ) : (hover ||  (cg.button() != qApp->palette().active().button())) ? cg.button() : optionHandler->InactiveButtonColor(), button.x(), (pos == full || pos == left) ? button.y() : button.y()-4, button.width(), (pos == full) ?
														button.height() :
														(pos == center) ?
														button.height()+8 :
														button.height()+4, 
														sunken, hover, false) :
							drawRectangularButton( p, cg, sunken ? cg.button().light( 110 ) : (hover ||  (cg.button() != qApp->palette().active().button())) ? cg.button() : optionHandler->InactiveButtonColor(), button.x(), button.y(), button.width(), button.height(), sunken, hover, false, pos);
						}
						}
					}
				// Draw a toolbutton menu indicator if required
				if ( controls & SC_ToolButtonMenu ) {
					if ( mflags & ( Style_Down | Style_On | Style_Raised ) )
						drawPrimitive( PE_ButtonDropDown, p, menuarea, cg, mflags, opt );
					drawPrimitive( PE_ArrowDown, p, menuarea, cg, mflags, opt );
					}

				if ( toolbutton->hasFocus() && !toolbutton->focusProxy() ) {
					QRect fr = toolbutton->rect();
					fr.addCoords( 3, 3, -3, -3 );
					drawPrimitive( PE_FocusRect, p, fr, cg );
					}
				break;
				}
			case CC_ScrollBar: {
				const_cast<LiquidStyle*>( this ) ->currentScrollBar =
				    ( QScrollBar * ) widget;
				KStyle::drawComplexControl( control, p, widget, r, cg, flags,
				                            controls, active, opt );
				break;
				}
/*			case CC_Slider:{
			if (optionHandler->drawGroupBoxShadow() && widget->parentWidget() && widget->parentWidget()->inherits("QGroupBox")){
			    QRect groove = querySubControlMetrics(CC_Slider, widget,
			                                          SC_SliderGroove, opt);
			    QRect handle = querySubControlMetrics(CC_Slider, widget,
			                                          SC_SliderHandle, opt);
			    if((controls & SC_SliderGroove) && groove.isValid())
			        drawKStylePrimitive(KPE_SliderGroove, p, widget, groove, cg,
			                            flags, opt);
			    if(controls & SC_SliderTickmarks)
			        QCommonStyle::drawComplexControl(control, p, widget, r, cg,
			                                         flags, SC_SliderTickmarks,
			                                         active, opt);
			    if((controls & SC_SliderHandle) && handle.isValid())
			        drawKStylePrimitive(KPE_SliderHandle, p, widget, handle, cg,
			                            flags, opt);
			    break;
			}else{
				KStyle::drawComplexControl( control, p, widget, r, cg, flags, controls, active, opt );
				break;
				}
			}*/

		    case CC_SpinWidget: {
			const QSpinWidget * sw = (const QSpinWidget *) widget;
			SFlags flags;
			PrimitiveElement pe;
		
			if ( controls & SC_SpinWidgetFrame )
			drawEditFrame( p, r, cg, false, sw->hasFocus());
		
			if ( controls & SC_SpinWidgetUp ) {
			flags = Style_Default | Style_Enabled;
			if (active == SC_SpinWidgetUp ) {
				flags |= Style_On;
				flags |= Style_Sunken;
			} else
				flags |= Style_Raised;
			pe = PE_SpinWidgetUp;
		
			QRect re = sw->upRect();
			QColorGroup ucg = sw->isUpEnabled() ? cg : sw->palette().disabled();
			p->fillRect(re, ucg.base());
			drawPrimitive(pe, p, re, ucg, flags);
			}
		
			if ( controls & SC_SpinWidgetDown ) {
			flags = Style_Default | Style_Enabled;
			if (active == SC_SpinWidgetDown ) {
				flags |= Style_On;
				flags |= Style_Sunken;
			} else
				flags |= Style_Raised;
			pe = PE_SpinWidgetDown;
		
			QRect re = sw->downRect();
			QColorGroup dcg = sw->isDownEnabled() ? cg : sw->palette().disabled();
			p->fillRect(re, dcg.base());
			drawPrimitive(pe, p, re, dcg, flags);
			}
			break; }
			
		// SLIDER - KDE code for double buffering
		// -------------------------------------------------------------------
		case CC_Slider: {
			const QSlider* slider = (const QSlider*)widget;
			QRect groove = querySubControlMetrics(CC_Slider, widget, SC_SliderGroove, opt);
			QRect handle = querySubControlMetrics(CC_Slider, widget, SC_SliderHandle, opt);

			// Double-buffer slider for no flicker
			QPixmap pix(widget->size());
			QPainter p2;
			p2.begin(&pix);
// 			bool gbs;
                        
// 			if ( slider->parentWidget() &&
// 				 ((gbs = (optionHandler->drawGroupBoxShadow() && slider->parentWidget()->inherits("QGroupBox"))) || (slider->parentWidget()->paletteBackgroundPixmap() && !slider->parentWidget()->paletteBackgroundPixmap()->isNull() ))) {
// 				QPixmap pixmap = gbs ? *groupShadow : *(slider->parentWidget()->paletteBackgroundPixmap());
			if ( slider->parentWidget() &&
				 (slider->parentWidget()->paletteBackgroundPixmap() && !slider->parentWidget()->paletteBackgroundPixmap()->isNull() )) {
				QPixmap pixmap = *(slider->parentWidget()->paletteBackgroundPixmap());
				if (QWidget *topDaddy = slider->topLevelWidget()){
                                    QPoint zero = QPoint(0,0);
                                    p2.drawTiledPixmap(r, pixmap, slider->mapTo(topDaddy, zero));
                                } else
                                    p2.drawTiledPixmap(r, pixmap, slider->pos());
			} else
				pix.fill(cg.background());

			// Draw slider groove
			if ((controls & SC_SliderGroove) && groove.isValid()) {
				drawKStylePrimitive( KPE_SliderGroove, &p2, widget, groove, cg, flags, opt );

				// Draw the focus rect around the groove
/*				if (slider->hasFocus())
					drawPrimitive(PE_FocusRect, &p2, groove, cg);*/
			}

			// Draw the tickmarks
			if (controls & SC_SliderTickmarks)
				QCommonStyle::drawComplexControl(control, &p2, widget,
						r, cg, flags, SC_SliderTickmarks, active, opt);

			// Draw the slider handle
			if ((controls & SC_SliderHandle) && handle.isValid()) {
				if (active == SC_SliderHandle)
					flags |= Style_Active;
				drawKStylePrimitive( KPE_SliderHandle, &p2, widget, handle, cg, flags, opt );
			}

			p2.end();
			bitBlt((QWidget*)widget, r.x(), r.y(), &pix);
			break;
		}

#ifndef QT_NO_LISTVIEW
			case CC_ListView: {
				if ( controls & SC_ListView ) {
					QCommonStyle::drawComplexControl( control, p, widget, r, cg, flags, controls, active, opt );
					}
				if ( controls & ( SC_ListViewBranch | SC_ListViewExpand ) ) {
					if ( opt.isDefault() )
						break;

					QListViewItem *item = opt.listViewItem();
					QListViewItem *child = item->firstChild();
					QListView* v = item->listView();
					bool drawDotlines = optionHandler->DrawDotlines();
					static QBitmap *verticalLine = 0, *horizontalLine = 0;
					static QCleanupHandler<QBitmap> qlv_cleanup_bitmap;
					int dotoffset = 0;

					if (drawDotlines && (optionHandler->DotlineStyle() == Dots)){
						if ( !verticalLine ) {
							// make 128*1 and 1*128 bitmaps that can be used for
							// drawing the right sort of lines.
							verticalLine = new QBitmap( 1, 128, TRUE );
							horizontalLine = new QBitmap( 128, 1, TRUE );
							QPointArray a( 64 );
							QPainter p;
							p.begin( verticalLine );
							int i;
							for( i=0; i<64; i++ )
								a.setPoint( i, 0, i*2+1 );
							p.setPen( optionHandler->DotlineColor() );
							p.drawPoints( a );
							p.end();
							QApplication::flushX();
							verticalLine->setMask( *verticalLine );
							p.begin( horizontalLine );
							for( i=0; i<64; i++ )
								a.setPoint( i, i*2+1, 0 );
							p.setPen( optionHandler->DotlineColor() );
							p.drawPoints( a );
							p.end();
							QApplication::flushX();
							horizontalLine->setMask( *horizontalLine );
							qlv_cleanup_bitmap.add( &verticalLine );
							qlv_cleanup_bitmap.add( &horizontalLine );
						}
					}


					int y = r.y();
					int c;
					QPointArray dotlines;
					if ( drawDotlines && active == SC_All && controls == SC_ListViewExpand ) {
						c = 2;
						dotlines.resize( 2 );
						dotlines[ 0 ] = QPoint( r.right(), r.top() );
						dotlines[ 1 ] = QPoint( r.right(), r.bottom() );
					}
					else {
						int linetop = 0, linebot = 0;
						// each branch needs at most two lines, ie. four end points
						dotoffset = (item->itemPos() + item->height() - y) %2;
						dotlines.resize( item->childCount() * 4 );
						c = 0;

						// skip the stuff above the exposed rectangle
						while ( child && y + child->height() <= 0 ) {
							y += child->totalHeight();
							child = child->nextSibling();
							}

						int bx = r.width() / 2;

						// paint stuff in the magical area
						while ( child && y < r.height() ) {
							int lh;
							if ( !item->multiLinesEnabled() )
								lh = child ? child->height() : 0;
							else
								lh = p->fontMetrics().height() + 2 * v->itemMargin();
							lh = QMAX( lh, QApplication::globalStrut().height() );
							if ( lh % 2 > 0 )
								lh++;
							linebot = y + lh / 2;
							if ( ( child->isExpandable() || child->childCount() ) && ( child->height() > 0 ) ) {
								if ( optionHandler->ExpanderStyle() == MS ) { // f****** M$ +/- style :-(
									// needs a box
									p->setPen( optionHandler->DotlineColor() );
									p->drawRect( bx - 4, linebot - 4, 9, 9 );
									// plus or minus
									p->setPen( optionHandler->UseCustomExpanderColor() ? optionHandler->CustomExpanderColor() : cg.text() );
									p->drawLine( bx - 2, linebot, bx + 2, linebot );
									if ( !child->isOpen() )
										p->drawLine( bx, linebot - 2, bx, linebot + 2 );
									} else { // ultracool triangles ;-)
									QPointArray a;
									p->setPen( optionHandler->UseCustomExpanderColor() ? optionHandler->CustomExpanderColor() : cg.text() );
									if ( child->isOpen() ) {
										a.setPoints( 3, bx - 4, linebot - 2,
										             bx, linebot + 2,
										             bx + 4, linebot - 2 );
										} //DownArrow
									else {
										a.setPoints( 3, bx - 2, linebot - 4,
										             bx + 2, linebot,
										             bx - 2, linebot + 4 );
										} //RightArrow
									p->setBrush( optionHandler->UseCustomExpanderColor() ? optionHandler->CustomExpanderColor() : cg.text() );
									p->drawPolygon( a );

									p->setBrush( NoBrush );
									}
								// dotlinery
								if ( drawDotlines ) {
									dotlines[ c++ ] = QPoint( bx, linetop );
									dotlines[ c++ ] = QPoint( bx, linebot - (/* optionHandler->plusMinus() ? 4 : */5 ) );
									dotlines[ c++ ] = QPoint( bx + 5, linebot );
									dotlines[ c++ ] = QPoint( r.width(), linebot );
									linetop = linebot + 5;
									}
								} else {
								// just dotlinery
								if ( drawDotlines ) {
									dotlines[ c++ ] = QPoint( bx + 2, linebot );
									dotlines[ c++ ] = QPoint( r.width(), linebot );
									}
								}

							y += child->totalHeight();
							child = child->nextSibling();
							}

						// Expand line height to edge of rectangle if there's a
						// child, and it's visible
						if ( child && ( child->height() > 0 ) ) {
							linebot = r.height();
							}

						if ( drawDotlines ) {
							if ( linetop < linebot ) {
								dotlines[ c++ ] = QPoint( bx, linetop );
								dotlines[ c++ ] = QPoint( bx, linebot );
								}
							}
					}

					if ( drawDotlines && optionHandler->DotlineStyle() == Line) {
						int line; // index into dotlines
						p->setPen( optionHandler->DotlineColor() );

						if ( controls & SC_ListViewBranch )
							for ( line = 0; line < c; line += 2 ) {
								p->drawLine( dotlines[ line ].x(), dotlines[ line ].y(),
								             dotlines[ line + 1 ].x(), dotlines[ line + 1 ].y() );
								}

						}
					else if (drawDotlines && (optionHandler->DotlineStyle() == Dots)) {
						int line; // index into dotlines
						if ( controls & SC_ListViewBranch ){
							p->setPen( optionHandler->DotlineColor() );
							for( line = 0; line < c; line += 2 ) {
						// assumptions here: lines are horizontal or vertical.
						// lines always start with the numerically lowest
						// coordinate.

						// point ... relevant coordinate of current point
						// end ..... same coordinate of the end of the current line
						// other ... the other coordinate of the current point/line
								if ( dotlines[line].y() == dotlines[line+1].y() ) {
									int end = dotlines[line+1].x();
									int point = dotlines[line].x();
									int other = dotlines[line].y();
									while( point < end ) {
										int i = 128;
										if ( i+point > end )
										i = end-point;
										p->drawPixmap( point, other, *horizontalLine, 0, 0, i, 1 );
										point += i;
									}
								}
								else {
									int end = dotlines[line+1].y();
									int point = dotlines[line].y();
									int other = dotlines[line].x();
									int pixmapoffset = ((point & 1) != dotoffset ) ? 1 : 0;
									while( point < end ) {
										int i = 128;
										if ( i+point > end )
											i = end-point;
										p->drawPixmap( other, point, *verticalLine, 0, pixmapoffset, 1, i );
										point += i;
									}
								}
							}
						}
					}
				}

				break;
				}
#endif // QT_NO_LISTVIEW

			default:
			KStyle::drawComplexControl( control, p, widget,
			                            r, cg, flags, controls, active, opt );
			break;
		}
	}

int LiquidStyle::styleHint(StyleHint sh, const QWidget * w, const QStyleOption & opt, QStyleHintReturn * shr) const{
		switch (sh){
 			case QStyle::SH_GUIStyle:
				return MacStyle;
 				break;
			case QStyle::SH_TabBar_Alignment:
				if (optionHandler->CenterTabs()){
				if (!QApplication::reverseLayout())
					return AlignHCenter;
				else
					return KStyle::styleHint(sh, w, opt, shr);
				}
				break;
			default:
				return KStyle::styleHint(sh, w, opt, shr);
		}
		return KStyle::styleHint(sh, w, opt, shr);
}

QRect LiquidStyle::subRect( SubRect r, const QWidget *widget ) const {
	if (r == SR_ComboBoxFocusRect){
		return querySubControlMetrics( CC_ComboBox, widget, SC_ComboBoxEditField );
	}
	return ( KStyle::subRect( r, widget ) );
	}

int LiquidStyle::pixelMetric( PixelMetric m, const QWidget *widget ) const {
	switch ( m ) {
			case PM_ButtonMargin:
			return ( 5 );
			case PM_ButtonDefaultIndicator:
			return ( 0 );
//			case PM_MenuButtonIndicator:
//			return (8);
			case PM_ExclusiveIndicatorWidth:
			case PM_ExclusiveIndicatorHeight:
			return ( 16 );
			case PM_IndicatorWidth:
			case PM_IndicatorHeight:
			return ( 16 );
			case PM_ScrollBarExtent:
			return ( 15 );
			case PM_ScrollBarSliderMin:
			return ( 40 );
			case PM_SplitterWidth:
			return 6;
			case PM_SliderControlThickness:
			return(15);
			case PM_SliderThickness:
			return ( 22 );
			case PM_SliderLength:
			return ( 13 );
			case PM_TabBarTabOverlap:{
				return 0;
			}
//                         case PM_DockWindowFrameWidth:{
//                             qWarning("PM_DockWindowFrameWidth");
//                             return 2;
//                         }
                        case PM_DefaultFrameWidth:{
                            if (widget && widget->parentWidget() && ::qt_cast<QTabWidget*>(widget->parentWidget()) && widget->parentWidget()->parentWidget() && (widget->parentWidget()->parentWidget()->inherits("KonqMainWindow") || widget->parentWidget()->parentWidget()->inherits("KonqFrameContainer"))) //for the konqueror statusbar offset, thanks konqueror team... ... ... f***f***f***!!!
                                return 0;
                            else
                                return 2;
                        }
			case PM_TabBarBaseOverlap:{
				bool isChooser = optionHandler->TabStyle() == Chooser ||
				(optionHandler->TabStyle() == Clever && widget && widget->topLevelWidget()->inherits("QDialog"));
				if (isChooser) return 12; else return 2;
			}
			case PM_TabBarBaseHeight:{
				bool isChooser = optionHandler->TabStyle() == Chooser ||
				(optionHandler->TabStyle() == Clever && widget && widget->topLevelWidget()->inherits("QDialog"));
				if (isChooser) return 12; else return 7;
			}
			case PM_ButtonShiftHorizontal:
			return( 0 );
//			case PM_ButtonShiftVertical:{
// //				if (widget->inherits("QToolButton") && !optionHandler->useToolButtonFrame() && optionHandler->SmoothToolbuttons())
// 					return 10;
// // 				else
// // 					return 0;
// 			}
			default:
				return KStyle::pixelMetric( m, widget );
		}
	
	}

QSize LiquidStyle::sizeFromContents( ContentsType contents,
                                     const QWidget *widget,
                                     const QSize &contentSize,
                                     const QStyleOption& opt ) const {
	switch ( contents ) {
			case CT_PushButton: {
				// this is a little funky - we give values not based on pixelMetric
				// because we want a custom width/height and we can only give one
				// value in pixelMetric (used in sizeHint). Odd but works well
				const QPushButton * button = ( const QPushButton* ) widget;

				int w = contentSize.width() + 26 > 80 ? contentSize.width() + 26 : 80;
				int h = contentSize.height() > 24 ? contentSize.height() : 25;

				if ( button->text().isEmpty() )
					return ( QSize( contentSize.width() + 2 * pixelMetric( PM_ButtonMargin, widget ), h ) );

				return ( QSize( w, h ) );
				}

			case CT_ToolButton: {
				if (widget->parentWidget() && ::qt_cast<QTabWidget*>(widget->parentWidget())){
                                    return QSize(contentSize.width() + 6, contentSize.height() + 5);
                                    }
                                else{
                                    int w = contentSize.width() + 6;
                                    int sh = contentSize.height() + 5;
                                    int sw = (int)(1.3*sh);
                                    if (w < sw)
                                            return QSize(sw, sh);
                                    else
                                            return QSize(w, sh);
                                     }
                                }

			case CT_Slider: {
				int h = contentSize.height() > 24 ? contentSize.height() : 24;
				return ( QSize( contentSize.width(), h ) );
				}

			case CT_ComboBox: {
				QSize sz = KStyle::sizeFromContents( contents, widget, contentSize, opt );
				return ( QSize( sz.width()+3, sz.height() > 25 ? sz.height() : 25 ) );
				}
			case CT_PopupMenuItem: {
				if ( ! widget || opt.isDefault() )
					break;

				const QPopupMenu *popup = ( const QPopupMenu * ) widget;
				bool checkable = popup->isCheckable();
				QMenuItem *mi = opt.menuItem();
				int maxpmw = opt.maxIconWidth();
				int w = contentSize.width();
				int h = contentSize.height();

				if ( mi->custom() ) {
					w = mi->custom() ->sizeHint().width();
					h = mi->custom() ->sizeHint().height();
					if ( !mi->custom() ->fullSpan() )
						h += 2 * windowsItemVMargin + 2 * windowsItemFrame;
					} else if ( mi->widget() ) {}
				else if ( mi->isSeparator() ) {
					w = 10;
					h = windowsSepHeight;
					} else {
					if ( mi->pixmap() )
						h = QMAX( h, mi->pixmap() ->height() + 2 * windowsItemFrame );
					else if ( ! mi->text().isNull() )
						h = QMAX( h, popup->fontMetrics().height() + 2 * windowsItemVMargin +
						          2 * windowsItemFrame );
					if ( mi->iconSet() != 0 )
						h = QMAX( h, mi->iconSet() ->pixmap( QIconSet::Small,
						                                     QIconSet::Normal ).height() +
						          2 * windowsItemFrame );
					}
				if ( !mi->text().isNull() && mi->text().find( '\t' ) >= 0 )
					w += windowsTabSpacing;
				else if ( mi->popup() )
					w += 2 * windowsArrowHMargin;
				if ( checkable && maxpmw < 20 )
					w += 20 - maxpmw;
				if ( maxpmw )
					w += maxpmw + 6;
				if ( checkable || maxpmw > 0 )
					w += windowsCheckMarkHMargin;
				w += 20;
				return ( QSize( w, h ) );
				}
			default:
			break;
		}
	return KStyle::sizeFromContents( contents, widget, contentSize, opt );
	}


QPixmap LiquidStyle::stylePixmap( StylePixmap stylepixmap,
                                  const QWidget* widget,
                                  const QStyleOption& opt ) const {
    return KStyle::stylePixmap( stylepixmap, widget, opt );
}

#include "eventfilter.cpp"

QRect LiquidStyle::querySubControlMetrics( ComplexControl control, const QWidget *widget, SubControl subcontrol, const QStyleOption &opt ) const {
	if ( control == CC_ComboBox && subcontrol == SC_ComboBoxEditField ) {
		return ( QRect( 8, 3, widget->width() - 32, widget->height() - 7 ) );
		} else
		return ( KStyle::querySubControlMetrics( control, widget, subcontrol, opt ) );
	}

QImage LiquidStyle::fetchImage( const char *name ){
	return liquid_findImage( name );
}
	
QPixmap* LiquidStyle::processEmbedded( const char *label, const QColor &c, bool blend, const QColor *bg ) const {
	QImage img( liquid_findImage( label ) );
	img.detach();
	if ( img.isNull() ) { // shouldn't happen, been tested
		qWarning( "Invalid embedded label %s", label );
		return ( NULL );
		}
	return ( adjustHSV( img, c, blend, bg ) );
	}

QPixmap* LiquidStyle::getPixmap( BitmapData item ) const {
	QColor bgColor( isKicker ? origPanelBrush.color() :
	                qApp->palette().active().background() );
	QColor btnColor( qApp->palette().active().button() );
	QColor btnHoverColor( btnColor.light( 120 ) );
	QColor sbGrooveColor( optionHandler->useCustomColors() ?
	                      optionHandler->customColor( CustomSBGroove ) : bgColor );
	QColor sbSliderColor( optionHandler->useCustomColors() ?
	                      optionHandler->customColor( CustomSBSlider ) : bgColor );
	QColor sbSliderHoverColor( optionHandler->useCustomColors() ?
	                      optionHandler->customColor( CustomSBSliderHover ) : btnColor );
	QColor sbSliderPressedColor( optionHandler->useCustomColors() ?
	                      optionHandler->customColor( CustomSBSliderPressed ) : btnColor.dark(110) );

	if ( pixmaps[ item ] )
		return ( pixmaps[ item ] );
	LiquidStyle *ptr = const_cast<LiquidStyle*>( this );
	switch ( item ) {
/*			case transsel:
			ptr->pixmaps[ transsel ] = processEmbedded( "transsel", btnColor, true );
			break;*/
			case RadioOn:
			ptr->pixmaps[ RadioOn ] = processEmbedded( "radio_down", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ) : btnColor, true );
			break;
			case RadioOnHover:
			ptr->pixmaps[ RadioOnHover ] = processEmbedded( "radio_down", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ).light( 110 ) : btnHoverColor, true );
			break;
			case RadioOffHover:
			ptr->pixmaps[ RadioOffHover ] = processEmbedded( "radio", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ) : btnColor, true );
			case RadioOnPressed:
			ptr->pixmaps[ RadioOnPressed ] = processEmbedded( "radio_down", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ).dark( 120 ) : btnHoverColor.dark( 120 ), true );
			break;
			case RadioOffPressed:
			ptr->pixmaps[ RadioOffPressed ] = processEmbedded( "radio", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ).dark( 120 ) : btnColor.dark( 120 ), true );
			break;
			case TabDown:
			ptr->pixmaps[ TabDown ] = processEmbedded( "tab", optionHandler->useCustomColors() ? optionHandler->customColor( CustomTabOn ) : btnColor, true );
			break;
			case belowTabDown:
			ptr->pixmaps[ belowTabDown ] = processEmbedded( "tab", optionHandler->useCustomColors() ? optionHandler->customColor( CustomTabOn ) : btnColor, true );
			*ptr->pixmaps[ belowTabDown ] = ptr->pixmaps[ belowTabDown ] ->xForm( iMatrix );
			break;
			case TabFocus:
			ptr->pixmaps[ TabFocus ] = processEmbedded( "tab", optionHandler->useCustomColors() ? optionHandler->customColor( CustomTabOn ).light( 120 ) : btnHoverColor, true );
			break;
			case CBDown:
			ptr->pixmaps[ CBDown ] = processEmbedded( "checkboxdown", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ) : btnColor, true );
			break;
			case CBDownHover:
			ptr->pixmaps[ CBDownHover ] = processEmbedded( "checkboxdown", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ).light( 110 ) : btnHoverColor, true );
			break;
			case CBHover:
			ptr->pixmaps[ CBHover ] = processEmbedded( "checkbox", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ) : btnColor, true );
			break;
			case CBDownPressed:
			ptr->pixmaps[ CBDownPressed ] = processEmbedded( "checkboxdown", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ).dark( 120 ) : btnHoverColor.dark( 120 ), true );
			break;
			case CBPressed:
			ptr->pixmaps[ CBPressed ] = processEmbedded( "checkbox", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ).dark( 120 ) : btnColor.dark( 120 ), true );
			break;
			case HSlider:
			ptr->pixmaps[ HSlider ] = processEmbedded( "sliderarrow", btnColor, true );
			break;
			case VSlider:
			ptr->pixmaps[ VSlider ] = processEmbedded( "sliderarrow", btnColor, true );
			*ptr->pixmaps[ VSlider ] = ptr->pixmaps[ VSlider ] ->xForm( lMatrix );
			break;
			case VSliderAbove:
			ptr->pixmaps[ VSliderAbove ] = processEmbedded( "sliderarrow", btnColor, true );
			*ptr->pixmaps[ VSliderAbove ] = ptr->pixmaps[ VSliderAbove ] ->xForm( rMatrix );
			break;
			case HSliderInactive:
			ptr->pixmaps[ HSliderInactive ] = processEmbedded( "sliderarrow", bgColor, true );
			break;
			case VSliderInactive:
			ptr->pixmaps[ VSliderInactive ] = processEmbedded( "sliderarrow", bgColor, true );
			*ptr->pixmaps[ VSliderInactive ] = ptr->pixmaps[ VSliderInactive ] ->xForm( lMatrix );
			break;
			case VSliderAboveInactive:
			ptr->pixmaps[ VSliderAboveInactive ] = processEmbedded( "sliderarrow", bgColor, true );
			*ptr->pixmaps[ VSliderAboveInactive ] = ptr->pixmaps[ VSliderAboveInactive ] ->xForm( rMatrix );
			break;
			case RadioOff:
			ptr->pixmaps[ RadioOff ] = processEmbedded( "radio", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOff ) : bgColor, true );
			break;
			case Tab:
			ptr->pixmaps[ Tab ] = processEmbedded( "tab", optionHandler->useCustomColors() ? optionHandler->customColor( CustomTabOff ) : optionHandler->InactiveButtonColor(), true );
			break;
			case belowTab:
			ptr->pixmaps[ belowTab ] = processEmbedded( "tab", optionHandler->useCustomColors() ? optionHandler->customColor( CustomTabOff ) : optionHandler->InactiveButtonColor(), true );
			*ptr->pixmaps[ belowTab ] = ptr->pixmaps[ belowTab ] ->xForm( iMatrix );
			break;			
			case CB:
			ptr->pixmaps[ CB ] = processEmbedded( "checkbox", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOff ) : bgColor, true );
			break;
			case VSBSliderTop:
			ptr->pixmaps[ VSBSliderTop ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderColor, sbGrooveColor.dark( 120 ), true) : processEmbedded( "sbslider_top", sbSliderColor, true, &sbGrooveColor );
			break;
			case VSBSliderBtm:
			ptr->pixmaps[ VSBSliderBtm ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderColor, sbGrooveColor.dark( 120 ), false) : processEmbedded( "sbslider_btm", sbSliderColor, true, &sbGrooveColor );
			break;
			case VSBSliderMid:
			ptr->pixmaps[ VSBSliderMid ] = processEmbedded( "sbIslider_mid", sbSliderColor, &sbGrooveColor );
			break;
			case VSBSliderTopActive:
			ptr->pixmaps[ VSBSliderTopActive ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor, sbGrooveColor.dark( 120 ), true) : processEmbedded( "sbslider_top", sbSliderHoverColor, true, &sbGrooveColor );
			break;
			case VSBSliderBtmActive:
			ptr->pixmaps[ VSBSliderBtmActive ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor, sbGrooveColor.dark( 120 ), false) : processEmbedded( "sbslider_btm", sbSliderHoverColor, true, &sbGrooveColor );
			break;
			case VSBSliderMidActive:
			ptr->pixmaps[ VSBSliderMidActive ] = processEmbedded( "sbslider_mid", sbSliderHoverColor, false, &sbGrooveColor );
			break;
			case VSBSliderTopHover:
			ptr->pixmaps[ VSBSliderTopHover ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor.light(120), sbGrooveColor.dark( 120 ), true) : processEmbedded( "sbslider_top", sbSliderHoverColor.light(120), true, &sbGrooveColor );
			break;
			case VSBSliderBtmHover:
			ptr->pixmaps[ VSBSliderBtmHover ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor.light(120), sbGrooveColor.dark( 120 ), false) : processEmbedded( "sbslider_btm", sbSliderHoverColor.light(120), true, &sbGrooveColor );
			break;
			case VSBSliderMidHover:
			ptr->pixmaps[ VSBSliderMidHover ] = processEmbedded( "sbslider_mid", sbSliderHoverColor.light(120), false, &sbGrooveColor );
			break;
			case VSBSliderTopPressed:
			ptr->pixmaps[ VSBSliderTopPressed ] = processEmbedded( "sbslider_top", sbSliderPressedColor, true, &sbGrooveColor );
			break;
			case VSBSliderBtmPressed:
			ptr->pixmaps[ VSBSliderBtmPressed ] = processEmbedded( "sbslider_btm", sbSliderPressedColor, true, &sbGrooveColor );
			break;
			case VSBSliderMidPressed:
			ptr->pixmaps[ VSBSliderMidPressed ] = processEmbedded( "sbslider_mid", sbSliderPressedColor, false, &sbGrooveColor );
			break;
                       
                        case HSBSliderTop:
			ptr->pixmaps[ HSBSliderTop ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderColor, sbGrooveColor.dark( 120 ), true) : processEmbedded( "sbslider_top", sbSliderColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderTop ] = ptr->pixmaps[ HSBSliderTop ]->xForm( rMatrix );
			break;
			case HSBSliderBtm:
			ptr->pixmaps[ HSBSliderBtm ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderColor, sbGrooveColor.dark( 120 ), false) : processEmbedded( "sbslider_btm", sbSliderColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderBtm ] = ptr->pixmaps[ HSBSliderBtm ] ->xForm( rMatrix );
			break;
			case HSBSliderMid:
			ptr->pixmaps[ HSBSliderMid ] = processEmbedded( "sbIslider_mid", sbSliderColor, false, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderMid ] = ptr->pixmaps[ HSBSliderMid ]->xForm( rMatrix );
			break;
			case HSBSliderTopActive:
			ptr->pixmaps[ HSBSliderTopActive ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor, sbGrooveColor.dark( 120 ), true) : processEmbedded( "sbslider_top", sbSliderHoverColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderTopActive ] = ptr->pixmaps[ HSBSliderTopActive ] ->xForm( rMatrix );
			break;
			case HSBSliderBtmActive:
			ptr->pixmaps[ HSBSliderBtmActive ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor, sbGrooveColor.dark( 120 ), false) : processEmbedded( "sbslider_btm", sbSliderHoverColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderBtmActive ] = ptr->pixmaps[ HSBSliderBtmActive ] ->xForm( rMatrix );
			break;
			case HSBSliderMidActive:
			ptr->pixmaps[ HSBSliderMidActive ] = processEmbedded( "sbslider_mid", sbSliderHoverColor, false, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderMidActive ] = ptr->pixmaps[ HSBSliderMidActive ] ->xForm( rMatrix );
			break;
			case HSBSliderTopHover:
			ptr->pixmaps[ HSBSliderTopHover ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor.light(120), sbGrooveColor.dark( 120 ), true) : processEmbedded( "sbslider_top", sbSliderHoverColor.light(120), true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderTopHover ] = ptr->pixmaps[ HSBSliderTopHover ] ->xForm( rMatrix );
			break;
			case HSBSliderBtmHover:
			ptr->pixmaps[ HSBSliderBtmHover ] = optionHandler->shadowSbSlider() ? createSliderEnd( sbSliderHoverColor.light(120), sbGrooveColor.dark( 120 ), false) : processEmbedded( "sbslider_btm", sbSliderHoverColor.light(120), true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderBtmHover ] = ptr->pixmaps[ HSBSliderBtmHover ] ->xForm( rMatrix );
			break;
			case HSBSliderMidHover:
			ptr->pixmaps[ HSBSliderMidHover ] = processEmbedded( "sbslider_mid", sbSliderHoverColor.light(120), false, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderMidHover ] = ptr->pixmaps[ HSBSliderMidHover ] ->xForm( rMatrix );
			break;
			case HSBSliderTopPressed:
			ptr->pixmaps[ HSBSliderTopPressed ] = processEmbedded( "sbslider_top", sbSliderPressedColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderTopPressed ] = ptr->pixmaps[ HSBSliderTopPressed ] ->xForm( rMatrix );
			break;
			case HSBSliderBtmPressed:
			ptr->pixmaps[ HSBSliderBtmPressed ] = processEmbedded( "sbslider_btm", sbSliderPressedColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderBtmPressed ] = ptr->pixmaps[ HSBSliderBtmPressed ] ->xForm( rMatrix );
			break;
			case HSBSliderMidPressed:
			ptr->pixmaps[ HSBSliderMidPressed ] = processEmbedded( "sbslider_mid", sbSliderPressedColor, false, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderMidPressed ] = ptr->pixmaps[ HSBSliderMidPressed ] ->xForm( rMatrix );
			break;
                        
			case VSBSliderTopBg:
			ptr->pixmaps[ VSBSliderTopBg ] = processEmbedded( "sbgroove_top", sbGrooveColor, true, &sbGrooveColor );
			break;
			case VSBSliderBtmBg:
			ptr->pixmaps[ VSBSliderBtmBg ] = processEmbedded( "sbgroove_btm", sbGrooveColor, true, &sbGrooveColor );
			break;
			case VSBSliderMidBg:
			ptr->pixmaps[ VSBSliderMidBg ] = processEmbedded( "sbgroove_mid", sbGrooveColor, false, &sbGrooveColor );
			break;
			case VSBSliderMidBgI:
			ptr->pixmaps[ VSBSliderMidBgI ] = processEmbedded( "sb_subadd", sbGrooveColor, false, &sbGrooveColor );
			break;
			case HSBSliderTopBg:
			ptr->pixmaps[ HSBSliderTopBg ] = processEmbedded( "sbgroove_top", sbGrooveColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderTopBg ] = ptr->pixmaps[ HSBSliderTopBg ] ->xForm( rMatrix );
			break;
			case HSBSliderBtmBg:
			ptr->pixmaps[ HSBSliderBtmBg ] = processEmbedded( "sbgroove_btm", sbGrooveColor, true, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderBtmBg ] = ptr->pixmaps[ HSBSliderBtmBg ] ->xForm( rMatrix );
			break;
			case HSBSliderMidBg:
			ptr->pixmaps[ HSBSliderMidBg ] = processEmbedded( "sbgroove_mid", sbGrooveColor, false, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderMidBg ] = ptr->pixmaps[ HSBSliderMidBg ] ->xForm( rMatrix );
			break;
			case HSBSliderMidBgI:
			ptr->pixmaps[ HSBSliderMidBgI ] = processEmbedded( "sb_subadd", sbGrooveColor, false, &sbGrooveColor );
			*ptr->pixmaps[ HSBSliderMidBgI ] = ptr->pixmaps[ HSBSliderMidBgI ] ->xForm( rMatrix );
			break;

			case Progress:
			ptr->pixmaps[ Progress ] = processEmbedded( "progress", btnColor );
			break;
			case progress2:
			ptr->pixmaps[ progress2 ] = processEmbedded( "progress2", btnColor );
			break;

			case HTMLRadioOn:
			ptr->pixmaps[ HTMLRadioOn ] = processEmbedded( "htmlradio_down", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ) : btnColor, true );
			break;
			case HTMLRadioOnHover:
			ptr->pixmaps[ HTMLRadioOnHover ] = processEmbedded( "htmlradio_down", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ).light( 110 ) : btnHoverColor, true );
			break;
			case HTMLRadioOff:
			ptr->pixmaps[ HTMLRadioOff ] = processEmbedded( "htmlradio", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOff ) : bgColor, true );
			break;
			case HTMLRadioOffHover:
			ptr->pixmaps[ HTMLRadioOffHover ] = processEmbedded( "htmlradio", optionHandler->useCustomColors() ? optionHandler->customColor( CustomRadioOn ) : btnColor, true );
			break;

			case HTMLCBDown:
			ptr->pixmaps[ HTMLCBDown ] = processEmbedded( "checkboxdown", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ) : btnColor, true );
			drawHTMLCBBorder( *ptr->pixmaps[ HTMLCBDown ], optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ) : btnColor );
			break;
			case HTMLCBDownHover:
			ptr->pixmaps[ HTMLCBDownHover ] = processEmbedded( "checkboxdown", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ).light( 110 ) : btnHoverColor, true );
			drawHTMLCBBorder( *ptr->pixmaps[ HTMLCBDownHover ], optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ).light( 110 ) : btnHoverColor );
			break;
			case HTMLCBHover:
			ptr->pixmaps[ HTMLCBHover ] = processEmbedded( "checkbox", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ) : btnColor, true );
			drawHTMLCBBorder( *ptr->pixmaps[ HTMLCBHover ], optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOn ) : btnColor );
			break;
			case HTMLCB:
			ptr->pixmaps[ HTMLCB ] = processEmbedded( "checkbox", optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOff ) : bgColor, true );
			drawHTMLCBBorder( *ptr->pixmaps[ HTMLCB ], optionHandler->useCustomColors() ? optionHandler->customColor( CustomCBOff ) : bgColor );
			break;
			default:
			break;
		}
	return ( pixmaps[ item ] );
	}


#include "baghira.moc"
