///////////////////////////////////////////////////////////////////////
// -------------------
// DeKorator window decoration for KDE
// -------------------
// Copyright (C) 2005  moty rahamim <moty.rahamim@gmail.com>
//
// Based on Example-0.8, some ideas and code have been taken from plastik and KCMicons
//
// Example window decoration for KDE
// Copyright (c) 2003, 2004 David Johnson <david@usermode.org>
//
// Plastik KWin window decoration
// Copyright (C) 2003-2005 Sandro Giessl <sandro@giessl.com>
//
// KCMicons for KDE
// Copyright (c) 2000 Antonio Larrosa <larrosa@kde.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////





#include "deKoratorclient.h"

#include <tqsettings.h>

using namespace DeKorator;

// global constants

static int MARGIN = 4;
//static int GRUBMARGIN = 5;

static TQColor STYLEBGCOL;

// frames
static int TITLESIZE = 0;
static int LEFTFRAMESIZE = 0;
static int BUTTOMFRAMESIZE = 0;
static int RIGHTFRAMESIZE = 0;

// deco
static int TOPLEFTCORNERWIDTH;
static int TOPRIGHTCORNERWIDTH;
static int LEFTTITLEWIDTH;
static int RIGHTTITLEWIDTH;
static int TOPLEFTFRAMEHEIGHT;
static int BOTTOMLEFTFRAMEHEIGHT;
static int TOPRIGHTFRAMEHEIGHT;
static int BOTTOMRIGHTFRAMEHEIGHT;
static int LEFTBOTTOMFRAMEWIDTH;
static int RIGHTBOTTOMFRAMEWIDTH;

// buttons
//static const int DECOSIZE = 24;

static int BUTTONSHEIGHT = 0;

static int BTNHELPEWIDTH = 0;
static int BTNMAXWIDTH = 0;
static int BTNCLOSEWIDTH = 0;
static int BTNMINWIDTH = 0;
static int BTNSTICKYWIDTH = 0;
static int BTNABOVEWIDTH = 0;
static int BTNBELOWWIDTH = 0;
static int BTNSHADEWIDTH = 0;
static int BTNMENUWIDTH = 0;

static int BTNHELPEHEIGHT = 0;
static int BTNMAXHEIGHT = 0;
static int BTNCLOSEHEIGHT = 0;
static int BTNMINHEIGHT = 0;
static int BTNSTICKYHEIGHT = 0;
static int BTNABOVEHEIGHT = 0;
static int BTNBELOWHEIGHT = 0;
static int BTNSHADEHEIGHT = 0;
static int BTNMENUHEIGHT = 0;

// masks
static int TOPLEFTMASKWIDTH = 0;
static int TOPMIDMASKWIDTH = 0;
static int TOPRIGHTMASKWIDTH = 0;
static int BOTTOMLEFTMASKWIDTH = 0;
static int BOTTOMMIDMASKWIDTH = 0;
static int BOTTOMRIGHTMASKWIDTH = 0;

static int BOTTOMLEFTMASKHEIGHT = 0;
static int BOTTOMMIDMASKHEIGHT = 0;
static int BOTTOMRIGHTMASKHEIGHT = 0;

// config
// misc
static TQt::AlignmentFlags TITLEALIGN = TQt::AlignHCenter;
static bool USEMENUEIMAGE = FALSE;
static bool IGNOREAPPICNCOL = FALSE;
static bool DBLCLKCLOSE = FALSE;
static bool SHOWBTMBORDER = FALSE;
static bool USESHDTEXT = FALSE;
static int ACTIVESHDTEXTX = FALSE;
static int ACTIVESHDTEXTY = FALSE;
static TQColor ACTIVESHADECOLOR = TQColor( 150, 150, 150 );
static int INACTIVESHDTEXTX = FALSE;
static int INACTIVESHDTEXTY = FALSE;
static TQColor INACTIVESHADECOLOR = TQColor( 150, 150, 150 );
static int BTNSHIFTX = 0;
static int BTNSHIFTY = 0;


// colors

static bool USEANIMATION = TRUE;
static TQString ANIMATIONTYPE = "Intensity";

static int STEPS = 5;
static int INTERVAL = 5;
static int KEEPANIMATING = FALSE;

static TQString BUTTONHOVERTYPE = "To Gray";
static float EFFECTAMOUNT = 5;
static TQColor ACTIVEHIGHLIGHTCOLOR = TQColor( 150, 150, 150 );
static TQColor INACTIVEHIGHLIGHTCOLOR = TQColor( 150, 150, 150 );
static TQString DECOCOLORIZE = "Liquid Method";
static TQString BUTTONSCOLORIZE = "Liquid Method";
//static TQString BUTTONSHOVERCOLORIZE = "Liquid Method";
static const uint TIMERINTERVAL = 30; // msec
static const uint ANIMATIONSTEPS = 100;

//

// paths
static bool USEMASKS = FALSE;

// pix arrays
static TQPixmap *DECOARR[ decoCount ][ pixTypeCount ];
static TQPixmap *DECOPIXACTARR[ decoCount ];
static TQPixmap *DECOPIXINACTARR[ decoCount ];

static TQPixmap *BUTTONSARR[ buttonTypeAllCount ][ buttonStateCount ][ pixTypeCount ];
static TQPixmap *BUTTONPIXACTARR[ buttonTypeAllCount ][ buttonStateCount ];
static TQPixmap *BUTTONPIXINACTARR[ buttonTypeAllCount ][ buttonStateCount ];

//////////////////////////////////////////////////////////////////////////////
// DeKoratorFactory Class                                                     //
//////////////////////////////////////////////////////////////////////////////

bool DeKoratorFactory::initialized_ = FALSE;
bool DeKoratorFactory::colorizeActFrames_ = FALSE;
bool DeKoratorFactory::colorizeActButtons_ = FALSE;
bool DeKoratorFactory::colorizeInActFrames_ = FALSE;
bool DeKoratorFactory::colorizeInActButtons_ = FALSE;

bool DeKoratorFactory::useCustomButtonsColors_ = FALSE;
bool DeKoratorFactory::customColorsActiveButtons_ = FALSE;
bool DeKoratorFactory::customColorsInActiveButtons_ = FALSE;
TQColor DeKoratorFactory::cusBtnCol_[ buttonTypeAllCount ];

bool DeKoratorFactory::needInit_ = FALSE;
bool DeKoratorFactory::needReload_ = FALSE;
TQString DeKoratorFactory::framesPath_ = "";
TQString DeKoratorFactory::buttonsPath_ = "";
TQString DeKoratorFactory::masksPath_ = "";


extern "C" KDecorationFactory* create_factory()
{
    return new DeKorator::DeKoratorFactory();
}

//////////////////////////////////////////////////////////////////////////////
// DeKoratorFactory()
// ----------------
// Constructor
DeKoratorFactory::DeKoratorFactory()
{
    readConfig();
    initialized_ = TRUE;

    initPixmaps();

    loadPixmaps();

    bool isActive = false;
    if ( colorizeInActFrames_ )
        colorizeDecoPixmaps( isActive );
    if ( colorizeInActButtons_ )
        colorizeButtonsPixmaps( isActive );

    isActive = true;
    if ( colorizeActFrames_ )
        colorizeDecoPixmaps( isActive );
    if ( colorizeActButtons_ )
        colorizeButtonsPixmaps( isActive );

    prepareDecoWithBgCol();

    chooseRightPixmaps();
}

//////////////////////////////////////////////////////////////////////////////
// ~DeKoratorFactory()
// -----------------
// Destructor
DeKoratorFactory::~DeKoratorFactory()
{
    initialized_ = FALSE;
    int i, j, k;

    for ( i = 0; i < decoCount; i++ )
    {
        for ( j = 0; j < pixTypeCount; j++ )
        {
            if ( DECOARR[ i ][ j ] )
                delete DECOARR[ i ][ j ];
        }
    }

    for ( i = 0; i < buttonTypeAllCount; i++ )
    {
        for ( j = 0; j < buttonStateCount; j++ )
        {
            for ( k = 0; k < pixTypeCount; k++ )
            {
                if ( BUTTONSARR[ i ][ j ][ k ] )
                    delete BUTTONSARR[ i ][ j ][ k ];
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// createDecoration()
// -----------------
// Create the decoration
KDecoration* DeKoratorFactory::createDecoration( KDecorationBridge* b )
{
    return new DeKoratorClient( b, this );
}

//////////////////////////////////////////////////////////////////////////////
// reset()
// -------
// Reset the handler. Returns TRUE if decorations need to be remade, FALSE if
// only a repaint is necessary
bool DeKoratorFactory::reset( unsigned long changed )
{
    //    captionBufferDirty_ = true;
    // read in the configuration
    initialized_ = FALSE;
    bool confchange = readConfig();
    initialized_ = TRUE;


    if ( confchange || ( changed & ( SettingDecoration | SettingButtons | SettingBorder | SettingColors ) ) )
    {
        //tqWarning( "aaaaaaaaaaaaa" );
        if ( DeKoratorFactory::needReload_ )
        {
            loadPixmaps();

            //             bool isActive = false;
            //             if ( colorizeInActFrames_ )
            //                 colorizeDecoPixmaps( isActive );
            //             if ( colorizeInActButtons_ )
            //                 colorizeButtonsPixmaps( isActive );
            //
            //             isActive = true;
            //             if ( colorizeActFrames_ )
            //                 colorizeDecoPixmaps( isActive );
            //             if ( colorizeActButtons_ )
            //                 colorizeButtonsPixmaps( isActive );
            //
            //             prepareDecoWithBgCol();
            //
            //             chooseRightPixmaps();
            //
            //             return TRUE;
        }

        //if ( DeKoratorFactory::needInit_ )
        {
            bool isActive = false;
            if ( colorizeInActFrames_ )
                colorizeDecoPixmaps( isActive );
            if ( colorizeInActButtons_ )
                colorizeButtonsPixmaps( isActive );

            isActive = true;
            if ( colorizeActFrames_ )
                colorizeDecoPixmaps( isActive );
            if ( colorizeActButtons_ )
                colorizeButtonsPixmaps( isActive );

            prepareDecoWithBgCol();

            chooseRightPixmaps();

            //return TRUE;
        }
        return TRUE;
    }
    else
    {
        resetDecorations( changed );
        return FALSE;
    }
}

//////////////////////////////////////////////////////////////////////////////
// readConfig()
// ------------
// Read in the configuration file
bool DeKoratorFactory::readConfig()
{
    // create a config object
    TDEConfig config( "twindeKoratorrc" );
    TQColor col = TQColor( 150, 150, 150 );

    // grab settings

    // misc settings
    config.setGroup( "MISC" );

    TQt::AlignmentFlags oldalign = TITLEALIGN;
    TQString value = config.readEntry( "TitleAlignment", "AlignHCenter" );
    if ( value == "AlignLeft" )
        TITLEALIGN = TQt::AlignLeft;
    else if ( value == "AlignHCenter" )
        TITLEALIGN = TQt::AlignHCenter;
    else if ( value == "AlignRight" )
        TITLEALIGN = TQt::AlignRight;

    bool oldUseMenuImage = USEMENUEIMAGE;
    USEMENUEIMAGE = config.readBoolEntry( "UseMenuImage", "false" );

    bool oldIgnoreAppIcnCol = IGNOREAPPICNCOL;
    IGNOREAPPICNCOL = config.readBoolEntry( "IgnoreAppIconCol", "false" );

//    bool oldDblClkClose = DBLCLKCLOSE;
    DBLCLKCLOSE = config.readBoolEntry( "DblClkClose", "false" );

    bool oldShowBtmBorder = SHOWBTMBORDER;
    SHOWBTMBORDER = config.readBoolEntry( "ShowBtmBorder", "false" );

    bool oldUseShdtext = USESHDTEXT;
    USESHDTEXT = config.readBoolEntry( "UseShdtext", "false" );

    int oldActiveShdtextX = ACTIVESHDTEXTX;
    ACTIVESHDTEXTX = config.readNumEntry( "ActiveShdtextX", 0 );

    int oldActiveShdtextY = ACTIVESHDTEXTY;
    ACTIVESHDTEXTY = config.readNumEntry( "ActiveShdtextY", 0 );

    TQColor oldActiveShadeColor = ACTIVESHADECOLOR;
    ACTIVESHADECOLOR = config.readColorEntry( "ActiveShadowColor", &col );

    int oldInActiveShdtextX = INACTIVESHDTEXTX;
    INACTIVESHDTEXTX = config.readNumEntry( "InActiveShdtextX", 0 );

    int oldInActiveShdtextY = INACTIVESHDTEXTY;
    INACTIVESHDTEXTY = config.readNumEntry( "InActiveShdtextY", 0 );

    TQColor oldInActiveShadeColor = INACTIVESHADECOLOR;
    INACTIVESHADECOLOR = config.readColorEntry( "InActiveShadowColor", &col );


    //int oldBtnShiftX = BTNSHIFTX;
    BTNSHIFTX = config.readNumEntry( "BtnShiftX", 0 );

    //int oldBtnShiftY = BTNSHIFTY;
    BTNSHIFTY = config.readNumEntry( "BtnShiftY", 0 );


    // color's settings
    config.setGroup( "COLORS" );

    bool oldColorizeActFrames = DeKoratorFactory::colorizeActFrames_;
    DeKoratorFactory::colorizeActFrames_ = config.readBoolEntry( "ColorizeActFrames", "false" );

    bool oldColorizeActButtons = DeKoratorFactory::colorizeActButtons_;
    DeKoratorFactory::colorizeActButtons_ = config.readBoolEntry( "ColorizeActButtons", "false" );

    bool oldColorizeInActFrames = DeKoratorFactory::colorizeInActFrames_;
    DeKoratorFactory::colorizeInActFrames_ = config.readBoolEntry( "ColorizeInActFrames", "false" );

    bool oldColorizeInActButtons = DeKoratorFactory::colorizeInActButtons_;
    DeKoratorFactory::colorizeInActButtons_ = config.readBoolEntry( "ColorizeInActButtons", "false" );

    USEANIMATION = config.readBoolEntry( "UseAnimation", "false" );

    ANIMATIONTYPE = config.readEntry( "AnimationType", "Liquid Method" );

    STEPS = config.readNumEntry( "AnimSteps", 5 );
    INTERVAL = config.readNumEntry( "AnimInterval", 30 );
    KEEPANIMATING = config.readBoolEntry( "KeepAnimating", "false" );

    BUTTONHOVERTYPE = config.readEntry( "ButtonsHoverType", "To Gray" );

    EFFECTAMOUNT = ( ( float ) config.readNumEntry( "EffectAmount", 5 ) / 10 );

    ACTIVEHIGHLIGHTCOLOR = config.readColorEntry( "ActiveHighlightColor", &col );

    INACTIVEHIGHLIGHTCOLOR = config.readColorEntry( "InActiveHighlightColor", &col );

    TQString oldDecoColorize = DECOCOLORIZE;
    DECOCOLORIZE = config.readEntry( "DecoColorize", "Liquid Method" );

    TQString oldButtonsColorize = BUTTONSCOLORIZE;
    BUTTONSCOLORIZE = config.readEntry( "ButtonsColorize", "Liquid Method" );
    /*
        TQString oldButtonsHoverColorize = BUTTONSHOVERCOLORIZE;
        BUTTONSHOVERCOLORIZE = config.readEntry( "ButtonsHoverColorize", "Liquid Method" );*/

    // buttons settings
    config.setGroup( "BUTTONS" );

    bool oldUseCustomButtonsColors = DeKoratorFactory::useCustomButtonsColors_;
    DeKoratorFactory::useCustomButtonsColors_ = config.readBoolEntry( "UseCustomButtonsColors", "false" );

    bool oldCustomColorsActiveButtons = DeKoratorFactory::customColorsActiveButtons_;
    DeKoratorFactory::customColorsActiveButtons_ = config.readBoolEntry( "customColorsActiveButtons", "false" );

    bool oldCustomColorsInActiveButtons = DeKoratorFactory::customColorsInActiveButtons_;
    DeKoratorFactory::customColorsInActiveButtons_ = config.readBoolEntry( "customColorsInActiveButtons", "false" );

    // load buttons colors
    TQColor colArr[ buttonTypeAllCount ];
    for ( int i = 0 ; i < buttonTypeAllCount ; i++ )
    {
        colArr[ i ] = DeKoratorFactory::cusBtnCol_[ i ];
    }
    DeKoratorFactory::cusBtnCol_[ restore ] = config.readColorEntry( "RestoreButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ help ] = config.readColorEntry( "HelpButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ max ] = config.readColorEntry( "MaxButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ min ] = config.readColorEntry( "MinButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ close ] = config.readColorEntry( "CloseButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ sticky ] = config.readColorEntry( "StickyButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ stickydown ] = config.readColorEntry( "StickyDownButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ above ] = config.readColorEntry( "AboveButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ abovedown ] = config.readColorEntry( "AboveDownButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ below ] = config.readColorEntry( "BelowButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ belowdown ] = config.readColorEntry( "BelowDownButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ shade ] = config.readColorEntry( "ShadeButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ shadedown ] = config.readColorEntry( "ShadeDownButtonColor", &col );
    DeKoratorFactory::cusBtnCol_[ menu ] = config.readColorEntry( "MenuButtonColor", &col );

    bool cusColChanged = false;
    for ( int i = 0 ; i < buttonTypeAllCount ; i++ )
    {
        if ( colArr[ i ] != DeKoratorFactory::cusBtnCol_[ i ] )
        {
            cusColChanged = true;
            i = buttonTypeAllCount;
            tqWarning( "true" );
        }

    }

    // path's settings
    config.setGroup( "PATHS" );

    TQString oldFramesPath = DeKoratorFactory::framesPath_;
    DeKoratorFactory::framesPath_ = config.readEntry( "FramesPath", "" );

    TQString oldButtonsPath = DeKoratorFactory::buttonsPath_;
    DeKoratorFactory::buttonsPath_ = config.readEntry( "ButtonsPath", "" );

    bool oldUseMasks = USEMASKS;
    USEMASKS = config.readBoolEntry( "UseMasks", "false" );

    TQString oldMasksPath = DeKoratorFactory::masksPath_;
    DeKoratorFactory::masksPath_ = config.readEntry( "MasksPath", "" );


    //style backgrond
    TQColor oldStyleBgCol = STYLEBGCOL;

    TQString colStr = TQSettings().readEntry( "/TQt/Palette/active", "aaaa" );
    colStr = colStr.section( "#", 3, 3 ) ;
    colStr.insert( 0, '#' );
    colStr.truncate( 7 );

    TQColor c;
    c.setNamedColor( colStr );
    STYLEBGCOL = c;



    if ( oldalign == TITLEALIGN &&
            oldUseMenuImage == USEMENUEIMAGE &&
            oldIgnoreAppIcnCol == IGNOREAPPICNCOL &&
            oldShowBtmBorder == SHOWBTMBORDER &&
            oldUseShdtext == USESHDTEXT &&
            oldActiveShdtextX == ACTIVESHDTEXTX &&
            oldActiveShdtextY == ACTIVESHDTEXTY &&
            oldActiveShadeColor == ACTIVESHADECOLOR &&
            oldInActiveShdtextX == INACTIVESHDTEXTX &&
            oldInActiveShdtextY == INACTIVESHDTEXTY &&
            oldInActiveShadeColor == INACTIVESHADECOLOR &&
            oldColorizeActFrames == DeKoratorFactory::colorizeActFrames_ &&
            oldColorizeActButtons == DeKoratorFactory::colorizeActButtons_ &&
            oldColorizeInActFrames == DeKoratorFactory::colorizeInActFrames_ &&
            oldColorizeInActButtons == DeKoratorFactory::colorizeInActButtons_ &&
            oldDecoColorize == DECOCOLORIZE &&
            oldButtonsColorize == BUTTONSCOLORIZE &&
            oldUseCustomButtonsColors == DeKoratorFactory::useCustomButtonsColors_ &&
            oldCustomColorsActiveButtons == DeKoratorFactory::customColorsActiveButtons_ &&
            oldCustomColorsInActiveButtons == DeKoratorFactory::customColorsInActiveButtons_ &&
            cusColChanged == false &&
            oldFramesPath == DeKoratorFactory::framesPath_ &&
            oldButtonsPath == DeKoratorFactory::buttonsPath_ &&
            oldMasksPath == DeKoratorFactory::masksPath_ &&
            oldUseMasks == USEMASKS &&
            oldStyleBgCol == STYLEBGCOL
       )
        return FALSE;
    else
    {
        //         if ( oldColorizeActFrames != DeKoratorFactory::colorizeActFrames_ ||
        //                 oldColorizeActButtons != DeKoratorFactory::colorizeActButtons_ ||
        //                 oldColorizeInActFrames != DeKoratorFactory::colorizeInActFrames_ ||
        //                 oldColorizeInActButtons != DeKoratorFactory::colorizeInActButtons_ ||
        //                 oldDecoColorize != DECOCOLORIZE ||
        //                 oldButtonsColorize != BUTTONSCOLORIZE ||
        //                 cusColChanged == true ||
        //                 oldStyleBgCol != STYLEBGCOL
        //            )
        //             DeKoratorFactory::needInit_ = TRUE;
        //         else
        //             DeKoratorFactory::needInit_ = FALSE;

        if ( oldFramesPath != DeKoratorFactory::framesPath_ ||
                oldButtonsPath != DeKoratorFactory::buttonsPath_ ||
                oldMasksPath != DeKoratorFactory::masksPath_
           )
            DeKoratorFactory::needReload_ = TRUE;
        else
            DeKoratorFactory::needReload_ = FALSE;

        return TRUE;
    }
}

//////////////////////////////////////////////////////////////////////////////
// loadPixmaps()
// ------------
// Read in the configuration file
void DeKoratorFactory::loadPixmaps()
{
    TQString decoPixDir = DeKoratorFactory::framesPath_;
    TQString btnPixDir = DeKoratorFactory::buttonsPath_;
    TQString masksPixDir = DeKoratorFactory::masksPath_;


    // deco
    // top bar from left to right
    DECOARR[ topLeftCorner ][ orig ] ->load( decoPixDir + "/topLeftCornerBg.png" );
    DECOARR[ leftButtons ][ orig ] ->load( decoPixDir + "/leftButtonsBg.png" );
    DECOARR[ leftTitle ][ orig ] ->load( decoPixDir + "/leftTitleBg.png" );
    DECOARR[ midTitle ][ orig ] ->load( decoPixDir + "/midTitleBg.png" );
    DECOARR[ rightTitle ][ orig ] ->load( decoPixDir + "/rightTitleBg.png" );
    DECOARR[ rightButtons ][ orig ] ->load( decoPixDir + "/rightButtonsBg.png" );
    DECOARR[ topRightCorner ][ orig ] ->load( decoPixDir + "/topRightCornerBg.png" );
    // left frame from top to buttom
    DECOARR[ topLeftFrame ][ orig ] ->load( decoPixDir + "/topLeftFrameBg.png" );
    DECOARR[ midLeftFrame ][ orig ] ->load( decoPixDir + "/midLeftFrameBg.png" );
    DECOARR[ buttomLeftFrame ][ orig ] ->load( decoPixDir + "/bottomLeftFrameBg.png" );
    // buttom frame from left to right
    DECOARR[ leftButtomFrame ][ orig ] ->load( decoPixDir + "/leftBottomFrameBg.png" );
    DECOARR[ midButtomFrame ][ orig ] ->load( decoPixDir + "/midBottomFrameBg.png" );
    DECOARR[ rightButtomFrame ][ orig ] ->load( decoPixDir + "/rightBottomFrameBg.png" );
    // right frame from top to buttom
    DECOARR[ topRightFrame ][ orig ] ->load( decoPixDir + "/topRightFrameBg.png" );
    DECOARR[ midRightFrame ][ orig ] ->load( decoPixDir + "/midRightFrameBg.png" );
    DECOARR[ buttomRightFrame ][ orig ] ->load( decoPixDir + "/bottomRightFrameBg.png" );


    // buttons
    BUTTONSARR[ restore ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonRestore.png" );
    BUTTONSARR[ restore ][ press ][ normal ] ->load( btnPixDir + "/press/buttonRestorePress.png" );
    BUTTONSARR[ restore ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonRestoreHover.png" );

    BUTTONSARR[ help ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonHelp.png" );
    BUTTONSARR[ help ][ press ][ normal ] ->load( btnPixDir + "/press/buttonHelpPress.png" );
    BUTTONSARR[ help ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonHelpHover.png" );

    BUTTONSARR[ max ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonMax.png" );
    BUTTONSARR[ max ][ press ][ normal ] ->load( btnPixDir + "/press/buttonMaxPress.png" );
    BUTTONSARR[ max ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonMaxHover.png" );

    BUTTONSARR[ min ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonMin.png" );
    BUTTONSARR[ min ][ press ][ normal ] ->load( btnPixDir + "/press/buttonMinPress.png" );
    BUTTONSARR[ min ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonMinHover.png" );

    BUTTONSARR[ close ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonClose.png" );
    BUTTONSARR[ close ][ press ][ normal ] ->load( btnPixDir + "/press/buttonClosePress.png" );
    BUTTONSARR[ close ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonCloseHover.png" );

    BUTTONSARR[ sticky ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonSticky.png" );
    BUTTONSARR[ sticky ][ press ][ normal ] ->load( btnPixDir + "/press/buttonStickyDownPress.png" );
    BUTTONSARR[ sticky ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonStickyHover.png" );

    BUTTONSARR[ stickydown ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonStickyDown.png" );
    BUTTONSARR[ stickydown ][ press ][ normal ] ->load( btnPixDir + "/press/buttonStickyDownPress.png" );
    BUTTONSARR[ stickydown ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonStickyDownHover.png" );

    BUTTONSARR[ above ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonAbove.png" );
    BUTTONSARR[ above ][ press ][ normal ] ->load( btnPixDir + "/press/buttonAbovePress.png" );
    BUTTONSARR[ above ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonAboveHover.png" );

    BUTTONSARR[ abovedown ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonAboveDown.png" );
    BUTTONSARR[ abovedown ][ press ][ normal ] ->load( btnPixDir + "/press/buttonAboveDownPress.png" );
    BUTTONSARR[ abovedown ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonAboveDownHover.png" );

    BUTTONSARR[ below ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonBelow.png" );
    BUTTONSARR[ below ][ press ][ normal ] ->load( btnPixDir + "/press/buttonBelowPress.png" );
    BUTTONSARR[ below ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonBelowHover.png" );

    BUTTONSARR[ belowdown ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonBelowDown.png" );
    BUTTONSARR[ belowdown ][ press ][ normal ] ->load( btnPixDir + "/press/buttonBelowDownPress.png" );
    BUTTONSARR[ belowdown ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonBelowDownHover.png" );

    BUTTONSARR[ shade ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonShade.png" );
    BUTTONSARR[ shade ][ press ][ normal ] ->load( btnPixDir + "/press/buttonShadePress.png" );
    BUTTONSARR[ shade ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonShadeHover.png" );

    BUTTONSARR[ shadedown ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonShadeDown.png" );
    BUTTONSARR[ shadedown ][ press ][ normal ] ->load( btnPixDir + "/press/buttonShadeDownPress.png" );
    BUTTONSARR[ shadedown ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonShadeDownHover.png" );

    BUTTONSARR[ menu ][ regular ][ normal ] ->load( btnPixDir + "/normal/buttonMenu.png" );
    BUTTONSARR[ menu ][ press ][ normal ] ->load( btnPixDir + "/press/buttonMenuPress.png" );
    BUTTONSARR[ menu ][ hover ][ normal ] ->load( btnPixDir + "/hover/buttonMenuHover.png" );


    //masks
    topLeftCornerBitmap_ = TQBitmap( masksPixDir + "/topLeftCornerBitmap.png" );
    topMidBitmap_ = TQBitmap( masksPixDir + "/topMidBitmap.png" );
    topRightCornerBitmap_ = TQBitmap( masksPixDir + "/topRightCornerBitmap.png" );
    buttomLeftCornerBitmap_ = TQBitmap( masksPixDir + "/buttomLeftCornerBitmap.png" );
    buttomMidBitmap_ = TQBitmap( masksPixDir + "/buttomMidBitmap.png" );
    buttomRightCornerBitmap_ = TQBitmap( masksPixDir + "/buttomRightCornerBitmap.png" );

    determineSizes();

    //prepareDecoWithBgCol();

}

//////////////////////////////////////////////////////////////////////////////
// determineSizes()
// ------------
//
void DeKoratorFactory::determineSizes()
{
    //MARGIN = midButtomFrameBg_.height();

    // frames
    TITLESIZE = DECOARR[ midTitle ][ orig ] ->height();
    LEFTFRAMESIZE = DECOARR[ midLeftFrame ][ orig ] ->width();
    BUTTOMFRAMESIZE = DECOARR[ midButtomFrame ][ orig ] ->height();
    RIGHTFRAMESIZE = DECOARR[ midRightFrame ][ orig ] ->width();

    TOPLEFTCORNERWIDTH = DECOARR[ topLeftCorner ][ orig ] ->width();
    TOPRIGHTCORNERWIDTH = DECOARR[ topRightCorner ][ orig ] ->width();
    LEFTTITLEWIDTH = DECOARR[ leftTitle ][ orig ] ->width();
    RIGHTTITLEWIDTH = DECOARR[ rightTitle ][ orig ] ->width();
    TOPLEFTFRAMEHEIGHT = DECOARR[ topLeftFrame ][ orig ] ->height();
    BOTTOMLEFTFRAMEHEIGHT = DECOARR[ buttomLeftFrame ][ orig ] ->height();
    TOPRIGHTFRAMEHEIGHT = DECOARR[ topRightFrame ][ orig ] ->height();
    BOTTOMRIGHTFRAMEHEIGHT = DECOARR[ buttomRightFrame ][ orig ] ->height();
    LEFTBOTTOMFRAMEWIDTH = DECOARR[ leftButtomFrame ][ orig ] ->width();
    RIGHTBOTTOMFRAMEWIDTH = DECOARR[ rightButtomFrame ][ orig ] ->width();


    // buttons
    BUTTONSHEIGHT = TITLESIZE;

    BTNHELPEWIDTH = BUTTONSARR[ help ][ regular ][ normal ] ->width();
    BTNMAXWIDTH = BUTTONSARR[ max ][ regular ][ normal ] ->width();
    BTNCLOSEWIDTH = BUTTONSARR[ close ][ regular ][ normal ] ->width();
    BTNMINWIDTH = BUTTONSARR[ min ][ regular ][ normal ] ->width();
    BTNSTICKYWIDTH = BUTTONSARR[ sticky ][ regular ][ normal ] ->width();
    BTNABOVEWIDTH = BUTTONSARR[ above ][ regular ][ normal ] ->width();
    BTNBELOWWIDTH = BUTTONSARR[ below ][ regular ][ normal ] ->width();
    BTNSHADEWIDTH = BUTTONSARR[ shade ][ regular ][ normal ] ->width();
    BTNMENUWIDTH = BUTTONSARR[ menu ][ regular ][ normal ] ->width();

    BTNHELPEHEIGHT = BUTTONSARR[ help ][ regular ][ normal ] ->height();
    BTNMAXHEIGHT = BUTTONSARR[ max ][ regular ][ normal ] ->height();
    BTNCLOSEHEIGHT = BUTTONSARR[ close ][ regular ][ normal ] ->height();
    BTNMINHEIGHT = BUTTONSARR[ min ][ regular ][ normal ] ->height();
    BTNSTICKYHEIGHT = BUTTONSARR[ sticky ][ regular ][ normal ] ->height();
    BTNABOVEHEIGHT = BUTTONSARR[ above ][ regular ][ normal ] ->height();
    BTNBELOWHEIGHT = BUTTONSARR[ below ][ regular ][ normal ] ->height();
    BTNSHADEHEIGHT = BUTTONSARR[ shade ][ regular ][ normal ] ->height();
    BTNMENUHEIGHT = BUTTONSARR[ menu ][ regular ][ normal ] ->height();

    // masks
    TOPLEFTMASKWIDTH = topLeftCornerBitmap_.width();
    TOPMIDMASKWIDTH = topMidBitmap_.width();
    TOPRIGHTMASKWIDTH = topRightCornerBitmap_.width();
    BOTTOMLEFTMASKWIDTH = buttomLeftCornerBitmap_.width();
    BOTTOMMIDMASKWIDTH = buttomMidBitmap_.width();
    BOTTOMRIGHTMASKWIDTH = buttomRightCornerBitmap_.width();

    BOTTOMLEFTMASKHEIGHT = buttomLeftCornerBitmap_.height();
    BOTTOMMIDMASKHEIGHT = buttomMidBitmap_.height();
    BOTTOMRIGHTMASKHEIGHT = buttomRightCornerBitmap_.height();

}


//////////////////////////////////////////////////////////////////////////////
// colorizeDecoPixmaps()
// ------------
//
void DeKoratorFactory::colorizeDecoPixmaps( bool isActive )
{
    int i;
    TQColor col = options() ->colorGroup( KDecoration::ColorTitleBar, isActive ).background();

    if ( isActive )
    {
        for ( i = 0; i < decoCount; i++ )
        {
            *( DECOARR[ i ][ actCol ] ) = *( DECOARR[ i ][ orig ] );
            colorizePixmap( DECOARR[ i ][ actCol ], col, DECOCOLORIZE );
        }
    }
    else
    {
        for ( i = 0; i < decoCount; i++ )
        {
            *( DECOARR[ i ][ inActCol ] ) = *( DECOARR[ i ][ orig ] );
            colorizePixmap( DECOARR[ i ][ inActCol ], col, DECOCOLORIZE );
        }

    }

    prepareDecoWithBgCol();
}

//////////////////////////////////////////////////////////////////////////////
// colorizeButtonsPixmaps()
// ------------
//
void DeKoratorFactory::colorizeButtonsPixmaps( bool isActive )
{
    int i, j;
    TQColor col = options() ->colorGroup( KDecoration::ColorButtonBg, isActive ).background();

    if ( isActive )
    {
        if ( useCustomButtonsColors_ && customColorsActiveButtons_ )
        {
            for ( i = 0; i < buttonTypeAllCount; i++ )
            {
                for ( j = 0; j < buttonStateCount; j++ )
                {
                    *( BUTTONSARR[ i ][ j ][ actCol ] ) = *( BUTTONSARR[ i ][ j ][ normal ] );
                    colorizePixmap( BUTTONSARR[ i ][ j ][ actCol ], cusBtnCol_[ i ], BUTTONSCOLORIZE );
                }
            }
        }
        else
        {
            for ( i = 0; i < buttonTypeAllCount; i++ )
            {
                for ( j = 0; j < buttonStateCount; j++ )
                {
                    *( BUTTONSARR[ i ][ j ][ actCol ] ) = *( BUTTONSARR[ i ][ j ][ normal ] );
                    colorizePixmap( BUTTONSARR[ i ][ j ][ actCol ], col, BUTTONSCOLORIZE );
                }
            }
        }
    }
    else
    {
        if ( ( useCustomButtonsColors_ && customColorsInActiveButtons_ ) )
        {
            for ( i = 0; i < buttonTypeAllCount; i++ )
            {
                for ( j = 0; j < buttonStateCount; j++ )
                {
                    *( BUTTONSARR[ i ][ j ][ inActCol ] ) = *( BUTTONSARR[ i ][ j ][ normal ] );
                    colorizePixmap( BUTTONSARR[ i ][ j ][ inActCol ], cusBtnCol_[ i ], BUTTONSCOLORIZE );
                }
            }
        }
        else
        {
            for ( i = 0; i < buttonTypeAllCount; i++ )
            {
                for ( j = 0; j < buttonStateCount; j++ )
                {
                    *( BUTTONSARR[ i ][ j ][ inActCol ] ) = *( BUTTONSARR[ i ][ j ][ normal ] );
                    colorizePixmap( BUTTONSARR[ i ][ j ][ inActCol ], col, BUTTONSCOLORIZE );
                }
            }
        }
    }
}


//////////////////////////////////////////////////////////////////////////////
// colorizePixmap()
// ------------
//
void DeKoratorFactory::colorizePixmap( TQPixmap *pix, TQColor c, TQString colorizeMethod )
{
    TQImage img;
    if ( colorizeMethod == "Liquid Method" )
    {
        img = pix->convertToImage();
        //TDEIconEffect::toGray( img, 1.0 );

        if ( img.depth() != 32 )
            img = img.convertDepth( 32 );
        TQImage *dest = new TQImage( img.width(), img.height(), 32 );
        dest->setAlphaBuffer( TRUE );
        unsigned int *data = ( unsigned int * ) img.bits();
        unsigned int *destData = ( unsigned int* ) dest->bits();
        int total = img.width() * img.height();
        int current;
        int delta;
        int destR, destG, destB, alpha;
        int srcR = c.red() + 100;
        int srcG = c.green() + 100;
        int srcB = c.blue() + 100;

        for ( current = 0; current < total; ++current )
        {
            alpha = tqAlpha( data[ current ] );

            //         tqWarning( "--------------------------" );
            //         tqWarning( "red - %d", tqRed( data[ current ] ) );
            //         tqWarning( "green - %d", tqRed( data[ current ] ) );
            //         tqWarning( "blue - %d", tqRed( data[ current ] ) );

            delta = 255 - tqGray( data[ current ] );

            destR = srcR - delta;
            destG = srcG - delta;
            destB = srcB - delta;

            if ( destR < 0 )
                destR = 0;
            if ( destG < 0 )
                destG = 0;
            if ( destB < 0 )
                destB = 0;
            if ( destR > 255 )
                destR = 255;
            if ( destG > 255 )
                destG = 255;
            if ( destB > 255 )
                destB = 255;
            destData[ current ] = tqRgba( destR, destG, destB, alpha );
        }
        pix->convertFromImage( *dest );
    }
    else if ( colorizeMethod == "Kde Method" )
    {
        img = pix->convertToImage();
        TDEIconEffect::colorize( img, c, 1.0 );
        pix->convertFromImage( img, 0 );
    }
    else if ( colorizeMethod == "Hue Adgustment" )
    {
        img = pix->convertToImage();
        //TDEIconEffect::toGray( img, 1.0 );

        if ( img.depth() != 32 )
            img = img.convertDepth( 32 );

        //         TQImage *dest;
        //         *dest = img;
        TQImage *dest = new TQImage( img.width(), img.height(), 32 );
        dest->setAlphaBuffer( TRUE );
        unsigned int *data = ( unsigned int * ) img.bits();
        unsigned int *destData = ( unsigned int* ) dest->bits();
        int total = img.width() * img.height();
        int current;
//        int delta;
        int destR, destG, destB, alpha;
//        int srcR = c.red();
//        int srcG = c.green();
//        int srcB = c.blue();
        int h, s, v, ch;
//        int *r, *g, *b, *a;
        TQColor col ;
//        float srcPercent, destPercent;

        for ( current = 0; current < total; ++current )
        {
            //             int tqRed ( data[ current ] );
            //             int tqGreen ( data[ current ] );
            //             int tqBlue ( data[ current ] );
            //             alpha = tqAlpha( destData[ current ] );
            //
            //             //destData[ current ] = data[ current ];
            //
            //             TQColor col = destData[ current ];
            //             col.getHsv( h, s, v );
            //             //*h += 50;
            //
            //             col.setHsv( 50, *s, *v );
            //
            //             col.getRgb( r, g, b );

            col.setRgb( data[ current ] );
            //col = TQt::red;
            c.hsv( &h, &s, &v );
            ch = h;
            col.hsv( &h, &s, &v );
            //             s = 0;
            //v += 100;
            col.setHsv( ch, s, v );
            destR = col.red();
            destG = col.green();
            destB = col.blue();
            alpha = tqAlpha( data[ current ] );


            //             if ( destR < 0 )
            //                 destR = 0;
            //             if ( destG < 0 )
            //                 destG = 0;
            //             if ( destB < 0 )
            //                 destB = 0;
            //             if ( destR > 255 )
            //                 destR = 255;
            //             if ( destG > 255 )
            //                 destG = 255;
            //             if ( destB > 255 )
            //                 destB = 255;

            destData[ current ] = tqRgba( destR, destG, destB, alpha );
            //destData[ current ] = data[ current ];
        }
        pix->convertFromImage( *dest,                                                             /*TQt::ColorOnly | TQt::DiffuseDither | TQt::DiffuseAlphaDither |*/ TQt::AvoidDither );
    }
}


//////////////////////////////////////////////////////////////////////////////
// colorizePixmap()
//
void DeKoratorFactory::initPixmaps()
{
    int i, j, k;

    for ( i = 0; i < decoCount; i++ )
    {
        for ( j = 0; j < pixTypeCount; j++ )
        {
            DECOARR[ i ][ j ] = new TQPixmap();
        }
    }

    for ( i = 0; i < buttonTypeAllCount; i++ )
    {
        for ( j = 0; j < buttonStateCount; j++ )
        {
            for ( k = 0; k < pixTypeCount; k++ )
            {
                BUTTONSARR[ i ][ j ][ k ] = new TQPixmap();
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// colorizePixmap()
//
void DeKoratorFactory::chooseRightPixmaps()
{
    int i, j;

    if ( DeKoratorFactory::colorizeActFrames_ )
    {
        for ( i = 0; i < decoCount; i++ )
        {
            DECOPIXACTARR[ i ] = DECOARR[ i ][ actCol ];
        }
    }
    else
    {
        for ( i = 0; i < decoCount; i++ )
        {
            DECOPIXACTARR[ i ] = DECOARR[ i ][ normal ];
        }
    }

    if ( DeKoratorFactory::colorizeInActFrames_ )
    {
        for ( i = 0; i < decoCount; i++ )
        {
            DECOPIXINACTARR[ i ] = DECOARR[ i ][ inActCol ];
        }
    }
    else
    {
        for ( i = 0; i < decoCount; i++ )
        {
            DECOPIXINACTARR[ i ] = DECOARR[ i ][ normal ];
        }
    }



    if ( DeKoratorFactory::colorizeActButtons_ )
    {

        for ( i = 0; i < buttonTypeAllCount; i++ )
        {
            for ( j = 0; j < buttonStateCount; j++ )
            {
                BUTTONPIXACTARR[ i ][ j ] = BUTTONSARR[ i ][ j ][ actCol ];
            }
        }
    }
    else
    {
        for ( i = 0; i < buttonTypeAllCount; i++ )
        {
            for ( j = 0; j < buttonStateCount; j++ )
            {
                BUTTONPIXACTARR[ i ][ j ] = BUTTONSARR[ i ][ j ][ normal ];
            }
        }
    }


    if ( DeKoratorFactory::colorizeInActButtons_ )
    {
        for ( i = 0; i < buttonTypeAllCount; i++ )
        {
            for ( j = 0; j < buttonStateCount; j++ )
            {
                BUTTONPIXINACTARR[ i ][ j ] = BUTTONSARR[ i ][ j ][ inActCol ];
            }
        }
    }
    else
    {
        for ( i = 0; i < buttonTypeAllCount; i++ )
        {
            for ( j = 0; j < buttonStateCount; j++ )
            {
                BUTTONPIXINACTARR[ i ][ j ] = BUTTONSARR[ i ][ j ][ normal ];
            }
        }
    }

}

//////////////////////////////////////////////////////////////////////////////
// prepareDecoWithBbCol()
//
void DeKoratorFactory::prepareDecoWithBgCol()
{
    int i;
    TQPainter painter;
    TQPixmap tempPix;
    TQWidget widget;
    TQColor col = widget.colorGroup().background();


    if ( DeKoratorFactory::colorizeActFrames_ )
    {
        for ( i = 0 ; i < decoCount ; i++ )
        {
            tempPix.resize( DECOARR[ i ][ orig ] ->width(), DECOARR[ i ][ orig ] ->height() );

            tempPix.fill( col );

            painter.begin( &tempPix );
            {
                painter.drawPixmap( 0, 0, *( DECOARR[ i ][ actCol ] ) );
            }
            painter.end();

            *( DECOARR[ i ][ actCol ] ) = tempPix;
        }
    }
    else
    {
        for ( i = 0 ; i < decoCount ; i++ )
        {
            tempPix.resize( DECOARR[ i ][ orig ] ->width(), DECOARR[ i ][ orig ] ->height() );

            tempPix.fill( col );

            painter.begin( &tempPix );
            {
                painter.drawPixmap( 0, 0, *( DECOARR[ i ][ orig ] ) );
            }
            painter.end();

            *( DECOARR[ i ][ normal ] ) = tempPix;
        }
    }

    if ( DeKoratorFactory::colorizeInActFrames_ )
    {
        for ( i = 0 ; i < decoCount ; i++ )
        {
            tempPix.resize( DECOARR[ i ][ orig ] ->width(), DECOARR[ i ][ orig ] ->height() );

            tempPix.fill( col );

            painter.begin( &tempPix );
            {
                painter.drawPixmap( 0, 0, *( DECOARR[ i ][ inActCol ] ) );
            }
            painter.end();

            *( DECOARR[ i ][ inActCol ] ) = tempPix;
        }
    }
    else
    {
        for ( i = 0 ; i < decoCount ; i++ )
        {
            tempPix.resize( DECOARR[ i ][ orig ] ->width(), DECOARR[ i ][ orig ] ->height() );

            tempPix.fill( col );

            painter.begin( &tempPix );
            {
                painter.drawPixmap( 0, 0, *( DECOARR[ i ][ orig ] ) );
            }
            painter.end();

            *( DECOARR[ i ][ normal ] ) = tempPix;
        }
    }


    //     if ( !DeKoratorFactory::colorizeActFrames_ || !DeKoratorFactory::colorizeInActFrames_ )
    //     {
    //         for ( i = 0 ; i < decoCount ; i++ )
    //         {
    //             tempPix.resize( DECOARR[ i ][ orig ] ->width(), DECOARR[ i ][ orig ] ->height() );
    //
    //             tempPix.fill( col );
    //
    //             painter.begin( &tempPix );
    //             {
    //                 painter.drawPixmap( 0, 0, *( DECOARR[ i ][ orig ] ) );
    //             }
    //             painter.end();
    //
    //             *( DECOARR[ i ][ normal ] ) = tempPix;
    //         }
    //     }
}






//////////////////////////////////////////////////////////////////////////////
// DeKoratorButton Class                                                      //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// DeKoratorButton()
// ---------------
// Constructor
DeKoratorButton::DeKoratorButton( bool isLeft, int buttonWidth, int buttonHeight, DeKoratorClient * parent, const char * name,
                                  const TQString & tip, ButtonType type, buttonTypeAll btnType )
        : TQButton( parent->widget(), name ), isLeft_( isLeft ), buttonWidth_( buttonWidth ), client_( parent ), type_( type ), lastmouse_( NoButton ), decoPixHeight_( buttonHeight )
{
    //decoPixInAct_ = buttonPixInAct;
    animProgress = 0;
    hover_ = FALSE;
    setBackgroundMode( NoBackground );
    setFixedSize( buttonWidth_, BUTTONSHEIGHT );
    setCursor( arrowCursor );



    //if ( buttonPix )
    setPixmap( btnType );
    TQToolTip::add
        ( this, tip );
    animTmr = new TQTimer( this );
    connect( animTmr, SIGNAL( timeout() ), this, SLOT( animate() ) );
}

//////////////////////////////////////////////////////////////////////////////
// DeKoratorButton()
// ---------------
// destructor
DeKoratorButton::~DeKoratorButton()
{
    //if ( deco_ ) delete deco_;
}

//////////////////////////////////////////////////////////////////////////////
// setPixmap()

// -----------
// Set the button decoration
void DeKoratorButton::setPixmap( buttonTypeAll btnType )
{
    //   decoPix_ = buttonPix;
    //   decoPixPress_ = buttonPixPress;
    //   decoPixHover_ = buttonPixHover;
    //   decoPixInAct_ = buttonPixInAct;

    //   btnPixAct_ = btnPixAct;
    //   btnPixInAct_ = btnPixInAct;
    btnType_ = btnType;


    //     btnPixAct_ = btnPixAct;
    //     btnPixInAct_ = btnPixInAct;

    //decoPix_->setMask(*deco_);
    //   }
    //   else
    //   {
    //     deco_ = new TQBitmap(DECOSIZE, DECOSIZE, bitmap, TRUE);
    //     deco_->setMask(*deco_);
    //   }
    repaint( FALSE );
}

//////////////////////////////////////////////////////////////////////////////
// sizeHint()
// ----------
// Return size hint
TQSize DeKoratorButton::sizeHint() const
{
    return TQSize( buttonWidth_, BUTTONSHEIGHT );
}

//////////////////////////////////////////////////////////////////////////////
// enterEvent()
// ------------
// Mouse has entered the button
void DeKoratorButton::enterEvent( TQEvent * e )
{
    // if we wanted to do mouseovers, we would keep track of it here
    TQButton::enterEvent( e );
    s = STEPS;
    hover_ = TRUE;
    setCursor( TQt::PointingHandCursor );

    if ( USEANIMATION )
        animate();
    else
        repaint( FALSE );
}

//////////////////////////////////////////////////////////////////////////////
// leaveEvent()
// ------------
// Mouse has left the button
void DeKoratorButton::leaveEvent( TQEvent * e )
{
    // if we wanted to do mouseovers, we would keep track of it here

    TQButton::leaveEvent( e );
    //STEPS = s;
    hover_ = FALSE;
    unsetCursor ();

    if ( USEANIMATION )
        animate();
    else
        repaint( FALSE );
}

//////////////////////////////////////////////////////////////////////////////
// mousePressEvent()
// -----------------
// Button has been pressed
void DeKoratorButton::mousePressEvent( TQMouseEvent * e )
{
    lastmouse_ = e->button();

    // translate and pass on mouse event
    int button = LeftButton;
    if ( ( type_ != ButtonMax ) && ( e->button() != LeftButton ) )
    {
        button = NoButton; // middle & right buttons inappropriate
    }
    TQMouseEvent me( e->type(), e->pos(), e->globalPos(),
                    button, e->state() );
    TQButton::mousePressEvent( &me );
}

//////////////////////////////////////////////////////////////////////////////
// mouseReleaseEvent()
// -----------------
// Button has been released
void DeKoratorButton::mouseReleaseEvent( TQMouseEvent * e )
{
    lastmouse_ = e->button();

    // translate and pass on mouse event
    int button = LeftButton;
    if ( ( type_ != ButtonMax ) && ( e->button() != LeftButton ) )
    {
        button = NoButton; // middle & right buttons inappropriate
    }
    TQMouseEvent me( e->type(), e->pos(), e->globalPos(), button, e->state() );
    TQButton::mouseReleaseEvent( &me );
}

//////////////////////////////////////////////////////////////////////////////
// drawButton()
// ------------
// Draw the button
void DeKoratorButton::drawButton( TQPainter * painter )
{
    if ( !DeKoratorFactory::initialized() )
        return ;

    TQColorGroup group;
    int dx = 0, dy = 0;
    bool act = client_->isActive();
    TQImage buttonImgBak;

    // fill background
    if ( isLeft_ )
        painter->drawTiledPixmap( rect(), act ? *( DECOPIXACTARR[ leftButtons ] ) : *( DECOPIXINACTARR[ leftButtons ] ) );
    else
        painter->drawTiledPixmap( rect(), act ? *( DECOPIXACTARR[ rightButtons ] ) : *( DECOPIXINACTARR[ rightButtons ] ) );

    // apply app icon effects
    if ( type_ == ButtonMenu && !USEMENUEIMAGE )
    {
        dy = ( height() - 16 ) / 2;

        TQPixmap appIconPix = client_->icon().pixmap( TQIconSet::Small, TQIconSet::Normal );

        if ( !IGNOREAPPICNCOL )
        {
            if ( act )
            {
                if ( client_->decoFactory_->useCustomButtonsColors_ && client_->decoFactory_->customColorsActiveButtons_ )
                {
                    DeKoratorFactory::colorizePixmap( &appIconPix, client_->decoFactory_->cusBtnCol_[ menu ], BUTTONSCOLORIZE );
                }
                else if ( client_->decoFactory_->colorizeActButtons_ )
                {
                    TQColor col = client_->decoFactory_->options() ->colorGroup( KDecoration::ColorButtonBg, act ).background();

                    DeKoratorFactory::colorizePixmap( &appIconPix, col, BUTTONSCOLORIZE );
                }
            }
            else
            {
                if ( client_->decoFactory_->useCustomButtonsColors_ && client_->decoFactory_->customColorsInActiveButtons_ )
                {
                    DeKoratorFactory::colorizePixmap( &appIconPix, client_->decoFactory_->cusBtnCol_[ menu ], BUTTONSCOLORIZE );
                }
                else if ( client_->decoFactory_->colorizeInActButtons_ )
                {
                    TQColor col = client_->decoFactory_->options() ->colorGroup( KDecoration::ColorButtonBg, act ).background();

                    DeKoratorFactory::colorizePixmap( &appIconPix, col, BUTTONSCOLORIZE );
                }
            }
        }
        buttonImgBak = appIconPix;

    }
    else
    {
        dy = ( BUTTONSHEIGHT - decoPixHeight_ ) / 2;
    }


    // down
    if ( isDown() )
    {
        if ( !( type_ == ButtonMenu && !USEMENUEIMAGE ) )
        {
            buttonImgBak = act ? *( BUTTONPIXACTARR[ btnType_ ][ press ] ) : *( BUTTONPIXINACTARR[ btnType_ ][ press ] );
        }
        dx += BTNSHIFTX;
        dy += BTNSHIFTY;

        chooseRightHoverEffect( &buttonImgBak, BUTTONHOVERTYPE );
    }
    // hover
    else if ( hover_ )
    {
        if ( !( type_ == ButtonMenu && !USEMENUEIMAGE ) )
        {
            buttonImgBak = act ? *( BUTTONPIXACTARR[ btnType_ ][ hover ] ) : *( BUTTONPIXINACTARR[ btnType_ ][ hover ] );
        }
        chooseRightHoverEffect( &buttonImgBak, USEANIMATION ? ANIMATIONTYPE : BUTTONHOVERTYPE );
    }
    // regular
    else
    {
        if ( !( type_ == ButtonMenu && !USEMENUEIMAGE ) )
        {
            buttonImgBak = act ? *( BUTTONPIXACTARR[ btnType_ ][ regular ] ) : *( BUTTONPIXINACTARR[ btnType_ ][ regular ] );
        }

        if ( USEANIMATION && animProgress > 0 )
            chooseRightHoverEffect( &buttonImgBak, ANIMATIONTYPE );
    }

    painter->drawPixmap( dx, dy, buttonImgBak );


    if ( client_->isShade() && !SHOWBTMBORDER )
    {
        painter->setPen( TQColor( 70, 70, 70 ) );
        painter->drawLine( 0, BUTTONSHEIGHT - 1, buttonWidth_ - 1, BUTTONSHEIGHT - 1 );
    }
}

//////////////////////////////////////////////////////////////////////////////
// chooseRightHoverEffect()
// ------------
//
TQImage* DeKoratorButton::chooseRightHoverEffect( TQImage * img, TQString res )
{

    if ( USEANIMATION && !isDown() )
    {
        float f = ( ( float ) animProgress / 100 );

        if ( res == "Intensity" )
        {
            f /= 1.5;

            * img = KImageEffect::intensity ( *img, f );
        }
        else if ( res == "Blend" )
        {
            f /= 1.7;
            TQColor col = client_->isActive() ? ACTIVEHIGHLIGHTCOLOR : INACTIVEHIGHLIGHTCOLOR;

            * img = KImageEffect::blend ( col, *img, f );
        }
        else if ( res == "Solarize" )
        {
            KImageEffect::solarize ( *img, 100 - animProgress );
        }
        else if ( res == "Fade" )
        {
            f /= 3;
            TQColor col = client_->isActive() ? ACTIVEHIGHLIGHTCOLOR : INACTIVEHIGHLIGHTCOLOR;

            * img = KImageEffect::fade ( *img, f, col );
        }
        else if ( res == "Trashed" )
        {
            * img = KImageEffect::fade ( *img, animProgress, TQt::black );
        }
        else if ( res == "Hue Shift" )
        {
            int h, s, v;
            TQColor col ;
            int shift = ( int ) ( animProgress * 3.5 );
            col.setRgb( 255, 0, 0 );
            col.hsv( &h, &s, &v );
            col.setHsv( shift, s, v );

            TQPixmap pix;
            pix.convertFromImage( *img, TQt::AvoidDither );
            DeKoratorFactory::colorizePixmap( &pix, col, "Hue Adgustment" );
            *img = pix.convertToImage();
        }
    }
    else
    {
        if ( BUTTONHOVERTYPE == "To Gray" )
            TDEIconEffect::toGray( *img, EFFECTAMOUNT );
        else if ( BUTTONHOVERTYPE == "Colorize" )
        {
            TQColor col = client_->isActive() ? ACTIVEHIGHLIGHTCOLOR : INACTIVEHIGHLIGHTCOLOR;

            TQPixmap pix;
            pix.convertFromImage( *img, TQt::AvoidDither );
            DeKoratorFactory::colorizePixmap( &pix, col, BUTTONSCOLORIZE );
            *img = pix.convertToImage();
        }
        else if ( BUTTONHOVERTYPE == "DeSaturate" )
            TDEIconEffect::deSaturate( *img, EFFECTAMOUNT );
        else if ( BUTTONHOVERTYPE == "To Gamma" )
            TDEIconEffect::toGamma( *img, EFFECTAMOUNT );
    }
    return img;
}


//////////////////////////////////////////////////////////////////////////////
// animate()
// ------------
//
void DeKoratorButton::animate()
{
    animTmr->stop();

    if ( hover_ )
    {
        if ( KEEPANIMATING )
        {
            animProgress += s;

            if ( animProgress >= ANIMATIONSTEPS || animProgress <= 0 )
            {
                s *= -1;
            }
            animTmr->start( INTERVAL, true ); // single-shot
        }
        else
        {
            if ( !KEEPANIMATING )
            {
                if ( animProgress < ANIMATIONSTEPS )
                    animProgress += STEPS;
                else
                {
                    animProgress = ANIMATIONSTEPS;
                }
            }
            if ( animProgress < ANIMATIONSTEPS )
                animTmr->start( INTERVAL, true ); // single-shot
        }
    }
    else
    {
        if ( animProgress > 0 )
            animProgress -= STEPS;
        else
        {
            animProgress = 0;
        }
        if ( animProgress > 0 )
            animTmr->start( INTERVAL, true ); // single-shot
    }
    //tqWarning( "STEPS: %d", STEPS );
    //tqWarning( "animProgress: %d", animProgress );
    //tqWarning( "INTERVAL: %d", INTERVAL );
    repaint( false );
}










//////////////////////////////////////////////////////////////////////////////
// DeKoratorClient Class                                                      //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// DeKoratorClient()
// ---------------
// Constructor
DeKoratorClient::DeKoratorClient( KDecorationBridge * b, KDecorationFactory * f )
        : KDecoration( b, f ), mainLayout_( NULL ), titleLayout_( NULL ), midLayout_( NULL ) , leftTitleBarSpacer_( NULL ), titleBarSpacer_( NULL ), rightTitleBarSpacer_( NULL ), leftSpacer_( NULL ), rightSpacer_( NULL ), bottomSpacer_( NULL ), captionBufferDirty_( true )
{
    //captionBufferDirty_ = true;
    //maskDirty_ = true;
    decoFactory_ = ( DeKoratorFactory* ) f;
}

DeKoratorClient::~DeKoratorClient()
{
    for ( int n = 0; n < ButtonTypeCount; n++ )
    {
        if ( button[ n ] )
            delete button[ n ];
    }
}

//////////////////////////////////////////////////////////////////////////////
// init()
// ------
// Actual initializer for class


// basic layout:

//  __________________________________________________________________________
// | leftTitleBarSpacer | btns | titleBarSpacer_ | bts | rightTitleBarSpacer |
// |____________________|______|_________________|_____|_____________________|
// | |                                                                     | |
// | |                         contentsFake                                | |
// | |                                                                     | |
// |leftSpacer                                                    rightSpacer|
// |_|_____________________________________________________________________|_|
// |                                 bottomSpacer                            |
// |_________________________________________________________________________|
//

void DeKoratorClient::init()
{
    createMainWidget( WNoAutoErase );
    widget() ->installEventFilter( this );

    // for flicker-free redraws
    widget() ->setBackgroundMode( NoBackground );

    // layouts
    delete mainLayout_;
    delete titleLayout_;
    delete midLayout_;
    // spacers
    delete leftTitleBarSpacer_;
    delete titleBarSpacer_;
    delete rightTitleBarSpacer_;
    delete leftSpacer_;
    delete rightSpacer_;
    delete bottomSpacer_;
    /*  topLeftCornerBg[ normal ] ->load( decoPixDir + "/topLeftCornerBg.png" );
            leftButtonsBg[ normal ] ->load( decoPixDir + "/leftButtonsBg.png" );
            leftTitleBg[ normal ] ->load( decoPixDir + "/leftTitleBg.png" );
            midTitleBg[ normal ] ->load( decoPixDir + "/midTitleBg.png" );
            rightTitleBg[ normal ] ->load( decoPixDir + "/rightTitleBg.png" );
            rightButtonsBg[ normal ] ->load( decoPixDir + "/rightButtonsBg.png" );
            topRightCornerBg[ normal ] ->load( decoPixDir + "/topRightCornerBg.png" );
            // left frame from top to buttom
            topLeftFrameBg[ normal ] ->load( decoPixDir + "/topLeftFrameBg.png" );
            midLeftFrameBg[ normal ] ->load( decoPixDir + "/midLeftFrameBg.png" );
            buttomLeftFrameBg[ normal ] ->load( decoPixDir + "/bottomLeftFrameBg.png" );
            // buttom frame from left to right
            leftButtomFrameBg[ normal ] ->load( decoPixDir + "/leftBottomFrameBg.png" );
            midButtomFrameBg[ normal ] ->load( decoPixDir + "/midBottomFrameBg.png" );
            rightButtomFrameBg[ normal ] ->load( decoPixDir + "/rightBottomFrameBg.png" );
            // right frame from top to buttom
            topRightFrameBg[ normal ] ->load( decoPixDir + "/topRightFrameBg.png" );
            midRightFrameBg[ normal ] ->load( decoPixDir + "/midRightFrameBg.png" );
            buttomRightFrameBg[ normal ] ->load( decoPixDir + "/bottomRightFrameBg.png" );*/

    // layouts
    mainLayout_ = new TQVBoxLayout( widget(), 0, 0 );
    titleLayout_ = new TQHBoxLayout( mainLayout_, 0, 0 );
    midLayout_ = new TQHBoxLayout( mainLayout_, 0, 0 );

    // spacers
    leftTitleBarSpacer_ = new TQSpacerItem( TOPLEFTCORNERWIDTH, TITLESIZE,
                                           TQSizePolicy::Fixed, TQSizePolicy::Fixed );
    titleBarSpacer_ = new TQSpacerItem( 1, TITLESIZE, TQSizePolicy::Expanding,
                                       TQSizePolicy::Fixed );
    rightTitleBarSpacer_ = new TQSpacerItem( TOPRIGHTCORNERWIDTH, TITLESIZE,
                                            TQSizePolicy::Fixed, TQSizePolicy::Fixed );
    leftSpacer_ = new TQSpacerItem( LEFTFRAMESIZE, 1,
                                   TQSizePolicy::Fixed, TQSizePolicy::Expanding );
    rightSpacer_ = new TQSpacerItem( RIGHTFRAMESIZE, 1,
                                    TQSizePolicy::Fixed, TQSizePolicy::Expanding );
    bottomSpacer_ = new TQSpacerItem( 1, ( !isSetShade() || SHOWBTMBORDER ) ? BUTTOMFRAMESIZE : 0,
                                     TQSizePolicy::Expanding, TQSizePolicy::Fixed );


    // setup layout

    //left spacer
    titleLayout_->addItem( leftTitleBarSpacer_ );
    // setup titlebar buttons
    for ( int n = 0; n < ButtonTypeCount; n++ )
        button[ n ] = 0;
    addButtons( titleLayout_, options() ->titleButtonsLeft(), TRUE );
    titleLayout_->addItem( titleBarSpacer_ );
    addButtons( titleLayout_, options() ->titleButtonsRight(), FALSE );
    //right spacer
    titleLayout_->addItem( rightTitleBarSpacer_ );

    // mid layout
    midLayout_->addItem( leftSpacer_ );
    if ( isPreview() )
    {
        midLayout_->addWidget( new TQLabel( "<center><b>" + i18n( "Dekorator preview (Version 0.3)" ) + "</b></center>", widget() ) );
    }
    else
    {
        midLayout_->addItem( new TQSpacerItem( 0, 0 ) );
    }
    midLayout_->addItem( rightSpacer_ );


    //Bottom
    //if(!isSetShade() || SHOWBTMBORDER)
    mainLayout_->addItem( bottomSpacer_ );

    // connections
    connect( this, SIGNAL( keepAboveChanged( bool ) ), SLOT( keepAboveChange( bool ) ) );
    connect( this, SIGNAL( keepBelowChanged( bool ) ), SLOT( keepBelowChange( bool ) ) );

    captionBufferDirty_ = true;
    //maskDirty_ = true;
    widget() ->update( titleBarSpacer_->geometry() );
}

//////////////////////////////////////////////////////////////////////////////
// addButtons()
// ------------
// Add buttons to title layout
void DeKoratorClient::addButtons( TQBoxLayout * layout, const TQString & s, bool isLeft )
{
    //const unsigned char * bitmap;
//    TQPixmap * pix1, *pix2, *pix3, *pix4;
    TQString tip;
    buttonTypeAll btnType;

    if ( s.length() > 0 )
    {
        for ( unsigned n = 0; n < s.length(); n++ )
        {
            switch ( s[ n ] )
            {
            case 'M':
                // Menu button
                if ( !button[ ButtonMenu ] )
                {
                    button[ ButtonMenu ] =
                        new DeKoratorButton( isLeft, ( BTNMENUWIDTH < 16 ) ? 16 : BTNMENUWIDTH, BTNMENUHEIGHT, this, "menu", i18n( "Menu" ), ButtonMenu, menu );
                    connect( button[ ButtonMenu ], SIGNAL( pressed() ),
                             this, SLOT( menuButtonPressed() ) );
                    connect( button[ ButtonMenu ], SIGNAL( released() ), this, SLOT( menuButtonReleased() ) );

                    layout->addWidget( button[ ButtonMenu ] );
                }
                break;

            case 'S':
                // Sticky button
                if ( !button[ ButtonSticky ] )
                {
                    bool d = isOnAllDesktops();
                    if ( d )
                    {
                        btnType = stickydown;
                    }
                    else
                    {
                        //             pix1 = &( decoFactory_->buttonStickyPix_ );
                        //             pix2 = &( decoFactory_->buttonStickyPressPix_ );
                        //             pix3 = &( decoFactory_->buttonStickyHoverPix_ );
                        //             pix4 = &( decoFactory_->buttonStickyDownPixInAct_ );


                        btnType = sticky;
                    }
                    button[ ButtonSticky ] =
                        new DeKoratorButton( isLeft, BTNSTICKYWIDTH, BTNSTICKYHEIGHT, this, "sticky", d ? i18n( "Sticky" ) : i18n( "Un-Sticky" ), ButtonSticky, btnType );
                    connect( button[ ButtonSticky ], SIGNAL( clicked() ),
                             this, SLOT( toggleOnAllDesktops() ) );
                    layout->addWidget( button[ ButtonSticky ] );
                }
                break;

            case 'H':
                // Help button
                if ( ( !button[ ButtonHelp ] ) && providesContextHelp() )
                {
                    button[ ButtonHelp ] =
                        new DeKoratorButton( isLeft, BTNHELPEWIDTH, BTNHELPEHEIGHT, this, "help", i18n( "Help" ),
                                             ButtonHelp, help );
                    connect( button[ ButtonHelp ], SIGNAL( clicked() ),
                             this, SLOT( showContextHelp() ) );
                    layout->addWidget( button[ ButtonHelp ] );
                }
                break;

            case 'I':
                // Minimize button
                if ( ( !button[ ButtonMin ] ) && isMinimizable() )
                {
                    button[ ButtonMin ] =
                        new DeKoratorButton( isLeft, BTNMINWIDTH, BTNMINHEIGHT, this, "iconify", i18n( "Minimize" ), ButtonMin, min );
                    connect( button[ ButtonMin ], SIGNAL( clicked() ),
                             this, SLOT( minimize() ) );
                    layout->addWidget( button[ ButtonMin ] );
                }
                break;

            case 'A':
                // Maximize button
                if ( ( !button[ ButtonMax ] ) && isMaximizable() )
                {
                    bool m = ( maximizeMode() == MaximizeFull );
                    if ( m )
                    {
                        //            pix1 = &( decoFactory_->buttonRestorePix_ );
                        //             pix2 = &( decoFactory_->buttonRestorePressPix_ );
                        //             pix3 = &( decoFactory_->buttonRestoreHoverPix_ );
                        //             pix4 = &( decoFactory_->buttonRestorePixInAct_ );


                        btnType = restore;
                    }
                    else
                    {
                        //             pix1 = &( decoFactory_->buttonMaxPix_ );
                        //             pix2 = &( decoFactory_->buttonMaxPressPix_ );
                        //             pix3 = &( decoFactory_->buttonMaxHoverPix_ );
                        //             pix4 = &( decoFactory_->buttonMaxPixInAct_ );


                        btnType = max;
                    }
                    button[ ButtonMax ] =
                        new DeKoratorButton( isLeft, BTNMAXWIDTH, BTNMAXHEIGHT, this, "maximize", m ? i18n( "Restore" ) : i18n( "Maximize" ),
                                             ButtonMax, btnType );
                    connect( button[ ButtonMax ], SIGNAL( clicked() ),
                             this, SLOT( maxButtonPressed() ) );
                    layout->addWidget( button[ ButtonMax ] );
                }
                break;

            case 'X':
                // Close button
                if ( ( !button[ ButtonClose ] ) && isCloseable() )
                {
                    button[ ButtonClose ] =
                        new DeKoratorButton( isLeft, BTNCLOSEWIDTH, BTNCLOSEHEIGHT, this, "close", i18n( "Close" ),
                                             ButtonClose, close );
                    connect( button[ ButtonClose ], SIGNAL( clicked() ),
                             this, SLOT( closeWindow() ) );
                    layout->addWidget( button[ ButtonClose ] );
                }
                break;

            case 'F':
                // Above button
                if ( ( !button[ ButtonAbove ] ) )
                {
                    bool a = keepAbove();
                    if ( a )
                    {
                        btnType = abovedown;
                    }
                    else
                    {
                        btnType = above;
                    }
                    //           pix1 = &( decoFactory_->buttonAbovePix_ );
                    button[ ButtonAbove ] =
                        new DeKoratorButton( isLeft, BTNABOVEWIDTH, BTNABOVEHEIGHT, this, "above",
                                             i18n( "Keep Above Others" ), ButtonAbove, btnType );
                    connect( button[ ButtonAbove ], SIGNAL( clicked() ),
                             this, SLOT( aboveButtonPressed() ) );
                    layout->addWidget( button[ ButtonAbove ] );
                }
                break;

            case 'B':
                // Below button
                if ( ( !button[ ButtonBelow ] ) )
                {
                    bool b = keepBelow();
                    if ( b )
                    {
                        btnType = belowdown;
                    }
                    else
                    {
                        btnType = below;
                    }
                    //          pix1 = &( decoFactory_->buttonBelowPix_ );
                    button[ ButtonBelow ] =
                        new DeKoratorButton( isLeft, BTNBELOWWIDTH, BTNBELOWHEIGHT, this, "below",
                                             i18n( "Keep Below Others" ), ButtonBelow, btnType );
                    connect( button[ ButtonBelow ], SIGNAL( clicked() ),
                             this, SLOT( belowButtonPressed() ) );
                    layout->addWidget( button[ ButtonBelow ] );
                }
                break;

            case 'L':
                // Shade button
                if ( ( !button[ ButtonShade && isShadeable() ] ) )
                {
                    bool s = isSetShade();
                    if ( s )
                    {
                        btnType = shadedown;
                    }
                    else
                    {
                        btnType = shade;
                    }
                    button[ ButtonShade ] =
                        new DeKoratorButton( isLeft, BTNSHADEWIDTH, BTNSHADEHEIGHT, this, "shade", s ? i18n( "Unshade" ) : i18n( "Shade" ),
                                             ButtonShade, btnType );
                    connect( button[ ButtonShade ], SIGNAL( clicked() ),
                             this, SLOT( shadeButtonPressed() ) );
                    layout->addWidget( button[ ButtonShade ] );
                }
                break;

            case '_':
                // Spacer item
                layout->addSpacing( 3 );
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// activeChange()
// --------------
// window active state has changed
void DeKoratorClient::activeChange()
{
    for ( int n = 0; n < ButtonTypeCount; n++ )
        if ( button[ n ] )
            button[ n ] ->reset();
    widget() ->repaint( FALSE );
}

//////////////////////////////////////////////////////////////////////////////
// captionChange()
// ---------------
// The title has changed
void DeKoratorClient::captionChange()
{
    captionBufferDirty_ = true;
    widget() ->repaint( titleBarSpacer_->geometry() );
}

//////////////////////////////////////////////////////////////////////////////
// desktopChange()
// ---------------
// Called when desktop/sticky changes
void DeKoratorClient::desktopChange()
{
    bool d = isOnAllDesktops();
    buttonTypeAll btnType;

    if ( d )
    {
        btnType = stickydown;
    }
    else
    {
        btnType = sticky;
    }
    if ( button[ ButtonSticky ] )
    {
        button[ ButtonSticky ] ->setPixmap( btnType );
        TQToolTip::remove
            ( button[ ButtonSticky ] );
        TQToolTip::add
            ( button[ ButtonSticky ], d ? i18n( "Sticky" ) : i18n( "Un-Sticky" ) );
    }
}

//////////////////////////////////////////////////////////////////////////////
// iconChange()
// ------------
// The title has changed
void DeKoratorClient::iconChange()
{
    if ( !USEMENUEIMAGE && button[ ButtonMenu ] )
    {
        //button[ ButtonMenu ] ->setPixmap( 0 );
        button[ ButtonMenu ] ->repaint( FALSE );
    }
}

//////////////////////////////////////////////////////////////////////////////
// maximizeChange()
// ----------------
// Maximized state has changed
void DeKoratorClient::maximizeChange()
{
    bool m = ( maximizeMode() == MaximizeFull );
    buttonTypeAll btnType;

    if ( m )
    {
        btnType = restore;
    }
    else
    {
        btnType = max;
    }
    if ( button[ ButtonMax ] )
    {
        button[ ButtonMax ] ->setPixmap( btnType );
        TQToolTip::remove
            ( button[ ButtonMax ] );
        TQToolTip::add
            ( button[ ButtonMax ], m ? i18n( "Restore" ) : i18n( "Maximize" ) );
    }
}

//////////////////////////////////////////////////////////////////////////////
// shadeChange()
// -------------
// Called when window shading changes
void DeKoratorClient::shadeChange()
{
    bool s = isSetShade();
    buttonTypeAll btnType;

    if ( s )
    {
        btnType = shadedown;
    }
    else
    {
        btnType = shade;
    }
    if ( button[ ButtonShade ] )
    {
        button[ ButtonShade ] ->setPixmap( btnType );
        TQToolTip::remove
            ( button[ ButtonShade ] );
        TQToolTip::add
            ( button[ ButtonShade ], s ? i18n( "Unshade" ) : i18n( "Shade" ) );
    }

    //mainlayout_->setRowSpacing( 3, isSetShade() ? 0 : MARGIN );
    for ( int n = 0; n < ButtonTypeCount; n++ )
        if ( button[ n ] )
            button[ n ] ->reset();
    widget() ->repaint( FALSE );

    //mainlayout_->setRowSpacing( 3, isSetShade() ? 0 : MARGIN );
    //     if ( DeKoratorFactory::useMasks_ )
    //         doShape();
}

//////////////////////////////////////////////////////////////////////////////
// keepAboveChange()
// ------------
// The above state has changed
void DeKoratorClient::keepAboveChange( bool a )
{
    buttonTypeAll btnType;

    if ( a )
    {
        btnType = abovedown;
    }
    else
    {
        btnType = above;
    }
    if ( button[ ButtonAbove ] )
    {
        button[ ButtonAbove ] ->setPixmap( btnType );
    }
}

//////////////////////////////////////////////////////////////////////////////
// keepBelowChange()
// ------------
// The below state has changed
void DeKoratorClient::keepBelowChange( bool b )
{
    buttonTypeAll btnType;

    if ( b )
    {
        btnType = belowdown;
    }
    else
    {
        btnType = below;
    }
    if ( button[ ButtonBelow ] )
    {
        button[ ButtonBelow ] ->setPixmap( btnType );
    }
}

//////////////////////////////////////////////////////////////////////////////
// borders()
// ----------
// Get the size of the borders
void DeKoratorClient::borders( int & l, int & r, int & t, int & b ) const
{
    l = LEFTFRAMESIZE;
    r = RIGHTFRAMESIZE;
    t = TITLESIZE ;
    //     if ( SHOWBTMBORDER )
    //b = 10;
    //     else
    //         b = isShade() ? 0 : BUTTOMFRAMESIZE;
    //b = SHOWBTMBORDER ? BUTTOMFRAMESIZE : isShade() ? 1 : BUTTOMFRAMESIZE;
    if ( !isShade() || SHOWBTMBORDER )
    {
        b = BUTTOMFRAMESIZE;
        bottomSpacer_->changeSize( 1, BUTTOMFRAMESIZE, TQSizePolicy::Expanding, TQSizePolicy::Fixed );
    }
    else
    {
        b = 0;
        bottomSpacer_->changeSize( 1, b, TQSizePolicy::Expanding, TQSizePolicy::Fixed );
    }
    widget() ->layout() ->activate();
}

//////////////////////////////////////////////////////////////////////////////
// resize()
// --------
// Called to resize the window
void DeKoratorClient::resize( const TQSize & size )
{
    widget() ->resize( size );
}

//////////////////////////////////////////////////////////////////////////////
// minimumSize()
// -------------
// Return the minimum allowable size for this window
TQSize DeKoratorClient::minimumSize() const
{
    TQSize size = widget() ->minimumSize();
    size.setWidth( size.width() + TOPLEFTCORNERWIDTH + TOPRIGHTCORNERWIDTH );

    return size;

}

//////////////////////////////////////////////////////////////////////////////
// mousePosition()
// ---------------
// Return logical mouse position
KDecoration::Position DeKoratorClient::mousePosition( const TQPoint & point ) const
{
    //    bool res = TRUE;
    Position pos;
    if ( isShade() )
    {
        return PositionCenter;
    }

    if ( point.y() <= 5 )
    {
        // inside top frame
        if ( point.x() <= LEFTFRAMESIZE )
            pos = PositionTopLeft;
        else if ( point.x() >= ( width() - RIGHTFRAMESIZE ) )
            pos = PositionTopRight;
        else
            pos = PositionTop;
    }
    else if ( point.y() >= ( height() - BUTTOMFRAMESIZE ) )
    {
        // inside handle
        if ( point.x() <= LEFTFRAMESIZE )
            pos = PositionBottomLeft;
        else if ( point.x() >= ( width() - RIGHTFRAMESIZE ) )
            pos = PositionBottomRight;
        else
            pos = PositionBottom;
    }
    else if ( point.x() <= LEFTFRAMESIZE )
    {
        // on left frame
        if ( point.y() <= TITLESIZE )
            pos = PositionTopLeft;
        else if ( point.y() >= ( height() - BUTTOMFRAMESIZE ) )
            pos = PositionBottomLeft;
        else
            pos = PositionLeft;
    }
    else if ( point.x() >= width() - RIGHTFRAMESIZE )
    {
        // on right frame
        if ( point.y() <= TITLESIZE )
            pos = PositionTopRight;
        else if ( point.y() >= ( height() - BUTTOMFRAMESIZE ) )
            pos = PositionBottomRight;
        else
            pos = PositionRight;
    }
    else
    {
        // inside the frame
        pos = PositionCenter;
    }
    return pos;

    //     const int corner = 24;
    //     Position pos;
    //
    //     if (point.y() <= MARGIN) {
    //         // inside top frame
    //         if (point.x() <= corner)                 pos = PositionTopLeft;
    //         else if (point.x() >= (width()-corner))  pos = PositionTopRight;
    //         else                                     pos = PositionTop;
    //     } else if (point.y() >= (height()-MARGIN*2)) {
    //         // inside handle
    //         if (point.x() <= corner)                 pos = PositionBottomLeft;
    //         else if (point.x() >= (width()-corner))  pos = PositionBottomRight;
    //         else                                     pos = PositionBottom;
    //     } else if (point.x() <= MARGIN) {
    //         // on left frame
    //         if (point.y() <= corner)                 pos = PositionTopLeft;
    //         else if (point.y() >= (height()-corner)) pos = PositionBottomLeft;
    //         else                                     pos = PositionLeft;
    //     } else if (point.x() >= width()-MARGIN) {
    //         // on right frame
    //         if (point.y() <= corner)                 pos = PositionTopRight;
    //         else if (point.y() >= (height()-corner)) pos = PositionBottomRight;
    //         else                                     pos = PositionRight;
    //     } else {
    //         // inside the frame
    //         pos = PositionCenter;
    //     }
    //     return pos;

}


//////////////////////////////////////////////////////////////////////////////
// eventFilter()
// -------------
// Event filter
bool DeKoratorClient::eventFilter( TQObject * obj, TQEvent * e )
{
    if ( obj != widget() )
        return FALSE;

    switch ( e->type() )
    {

    case TQEvent::MouseButtonDblClick:
        {
            mouseDoubleClickEvent( static_cast<TQMouseEvent *>( e ) );
            return TRUE;
        }
    case TQEvent::Wheel:
        {
            wheelEvent( static_cast<TQWheelEvent *>( e ) );
            return TRUE;
        }
    case TQEvent::MouseButtonPress:
        {
            processMousePressEvent( static_cast<TQMouseEvent *>( e ) );
            if ( USEMASKS )
                doShape();
            return TRUE;
        }
    case TQEvent::Paint:
        {
            paintEvent( static_cast<TQPaintEvent *>( e ) );
            return TRUE;
        }
    case TQEvent::Resize:
        {
            resizeEvent( static_cast<TQResizeEvent *>( e ) );
            return TRUE;
        }
    case TQEvent::Show:
        {
            showEvent( static_cast<TQShowEvent *>( e ) );
            return TRUE;
        }
    default:
        {
            return FALSE;
        }
    }

    return FALSE;
}

//////////////////////////////////////////////////////////////////////////////
// mouseDoubleClickEvent()
// -----------------------
// Doubleclick on title
void DeKoratorClient::mouseDoubleClickEvent( TQMouseEvent * e )
{
    if ( titleBarSpacer_->geometry().contains( e->pos() ) )
        titlebarDblClickOperation();
}

//////////////////////////////////////////////////////////////////////////////
// wheelEvent()
// -----------------------
//
void DeKoratorClient::wheelEvent( TQWheelEvent *e )
{
    if ( titleLayout_->geometry().contains( e->pos() ) )
        titlebarMouseWheelOperation( e->delta() );
}


//////////////////////////////////////////////////////////////////////////////
// paintEvent()
// ------------
// Repaint the window
void DeKoratorClient::paintEvent( TQPaintEvent* )
{
    if ( !DeKoratorFactory::initialized() )
        return ;
    if ( TITLESIZE )
    {
        if ( captionBufferDirty_ && USESHDTEXT )
            updateCaptionBuffer();

        TQPainter painter( widget() );
        int tx, ty, tw, th;
        int w = width() , h = height();
        TQRect titleR( titleBarSpacer_->geometry() );
        TQRect leftTitleR( leftTitleBarSpacer_->geometry() );
        TQRect rightTitleR( rightTitleBarSpacer_->geometry() );
        titleR.rect( &tx, &ty, &tw, &th );
        TQRect rect;

        TQPainter painter2;
        TQPixmap pix( w, TITLESIZE );
        painter2.begin( &pix );
        {
            // topLeftCorner

            rect.setRect( 0, 0, TOPLEFTCORNERWIDTH, TITLESIZE );
            painter2.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ topLeftCorner ] ) : *( DECOPIXINACTARR[ topLeftCorner ] ) );

            //         rect.setRect( 0, 0, TOPLEFTCORNERWIDTH, TITLESIZE );
            //         painter2.drawTiledPixmap( rect, isActive() ? decoFactory_->topLeftCornerBg_ : decoFactory_->topLeftCornerBgInAct_ );

            // Space under the left button group
            painter2.drawTiledPixmap( leftTitleR.right() + 1, titleR.top(),
                                      ( titleR.left() - 1 ) - leftTitleR.right(), titleR.height(), isActive() ? *( DECOPIXACTARR[ leftButtons ] ) : *( DECOPIXINACTARR[ leftButtons ] ) );

            if ( tw > 0 )
            {
                //tqWarning("%d",titleR.width());

                //leftTitleR
                rect.setRect( tx, 0, TQMIN( LEFTTITLEWIDTH, tw ), TITLESIZE );

                painter2.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ leftTitle ] ) : *( DECOPIXINACTARR[ leftTitle ] ) );

                //             rect.setRect( tx, 0, TQMIN( LEFTTITLEWIDTH, tw ), TITLESIZE );
                //             painter2.drawTiledPixmap( rect, isActive() ? decoFactory_->leftTitleBg_ : decoFactory_->leftTitleBgInAct_ );

                //midTitle
                if ( tw > LEFTTITLEWIDTH + RIGHTTITLEWIDTH )
                {
                    rect.setRect( tx + LEFTTITLEWIDTH, 0, tw - ( RIGHTTITLEWIDTH + LEFTTITLEWIDTH ), th );

                    painter2.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ midTitle ] ) : *( DECOPIXINACTARR[ midTitle ] ) );

                    //                 painter2.drawTiledPixmap( rect, isActive() ? decoFactory_->midTitleBg_ : decoFactory_->midTitleBgInAct_ );
                }

                //rightTitleR
                if ( tw > LEFTTITLEWIDTH )
                {
                    rect.setRect( TQMAX( tx + tw - RIGHTTITLEWIDTH, tx + LEFTTITLEWIDTH ), 0, TQMIN( RIGHTTITLEWIDTH, tw - LEFTTITLEWIDTH ), th );

                    painter2.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ rightTitle ] ) : *( DECOPIXINACTARR[ rightTitle ] ), ( tw > LEFTTITLEWIDTH + RIGHTTITLEWIDTH ) ? 0 : LEFTTITLEWIDTH - ( tw - RIGHTTITLEWIDTH ), 0 );

                    //                 painter2.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? decoFactory_->rightTitleBg_ : decoFactory_->rightTitleBgInAct_, ( tw > LEFTTITLEWIDTH + RIGHTTITLEWIDTH ) ? 0 : LEFTTITLEWIDTH - ( tw - RIGHTTITLEWIDTH ), 0 );
                }
            }

            // Space under the right button group
            painter2.drawTiledPixmap( titleR.right() + 1, titleR.top(),
                                      ( rightTitleR.left() - 1 ) - titleR.right(), titleR.height(), isActive() ? *( DECOPIXACTARR[ rightButtons ] ) : *( DECOPIXINACTARR[ rightButtons ] ) );


            //topRightCorner
            rect.setRect( widget() ->width() - TOPRIGHTCORNERWIDTH, 0, TOPRIGHTCORNERWIDTH, TITLESIZE );

            painter2.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ topRightCorner ] ) : *( DECOPIXINACTARR[ topRightCorner ] ) );

            // painter2.drawTiledPixmap( rect, isActive() ? decoFactory_->topRightCornerBg_ : decoFactory_->topRightCornerBgInAct_ );


            TQString c( caption() );
            TQFontMetrics fm( options() ->font( isActive() ) );
            int captionWidth = fm.width( c );
            // shadow text effect
            if ( USESHDTEXT )
            {

                int captionHeight = fm.height( );
                int dx = 0, dy = 0;

                if ( TITLEALIGN == TQt::AlignLeft || captionWidth > titleR.width() )
                {
                    dx = tx + 1 + ( isActive() ? ACTIVESHDTEXTX : INACTIVESHDTEXTX ) ;
                    dy = ( TITLESIZE / 2 ) - ( captionHeight / 2 ) + ( isActive() ? ACTIVESHDTEXTY : INACTIVESHDTEXTY ) ;
                }
                else if ( TITLEALIGN == TQt::AlignHCenter )
                {
                    dx = tx + ( tw / 2 ) - ( ( captionWidth + ( MARGIN * 2 ) ) / 2 ) + ( isActive() ? ACTIVESHDTEXTX : INACTIVESHDTEXTX ) ;
                    dy = ( TITLESIZE / 2 ) - ( captionHeight / 2 ) + ( isActive() ? ACTIVESHDTEXTY : INACTIVESHDTEXTY ) ;
                }
                else if ( TITLEALIGN == TQt::AlignRight )
                {
                    dx = tx + tw - ( captionWidth + ( MARGIN * 2 ) ) - 1 + ( isActive() ? ACTIVESHDTEXTX : INACTIVESHDTEXTX ) ;
                    dy = ( TITLESIZE / 2 ) - ( captionHeight / 2 ) + ( isActive() ? ACTIVESHDTEXTY : INACTIVESHDTEXTY ) ;
                }

                painter2.drawImage( dx, dy, isActive() ? activeShadowImg_ : inActiveShadowImg_, 0, 0, isActive() ? TQMIN( activeShadowImg_.width(), titleR.width() ) : TQMIN( inActiveShadowImg_.width(), titleR.width() ), isActive() ? activeShadowImg_.height() : inActiveShadowImg_.height() );
            }


            //draw titleR text
            painter2.setFont( options() ->font( isActive(), FALSE ) );
            painter2.setPen( options() ->color( KDecoration::ColorFont, isActive() ) );

            TQt::AlignmentFlags titleAlignBak = TITLEALIGN;
            if ( captionWidth > titleR.width() )
                titleAlignBak = TQt::AlignLeft;

            painter2.drawText( tx + MARGIN, ty, tw - ( MARGIN * 2 ), th, titleAlignBak | AlignVCenter, caption() );
        }
        painter2.end();
        painter.drawPixmap( 0, 0, pix );


        // draw frames
        if ( ! isShade() || SHOWBTMBORDER )
        {

            if ( h - TITLESIZE - BUTTOMFRAMESIZE > 0 )
            {
                int leftRightFramesHeight = h - TITLESIZE - BUTTOMFRAMESIZE ;

                //left frame
                //top
                rect.setRect( 0, TITLESIZE , LEFTFRAMESIZE, TQMIN( TOPLEFTFRAMEHEIGHT, leftRightFramesHeight ) );

                painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ topLeftFrame ] ) : *( DECOPIXINACTARR[ topLeftFrame ] ) );

                // mid
                if ( leftRightFramesHeight > TOPLEFTFRAMEHEIGHT + BOTTOMLEFTFRAMEHEIGHT )
                {
                    rect.setRect( 0, TITLESIZE + TOPLEFTFRAMEHEIGHT , LEFTFRAMESIZE, leftRightFramesHeight - TOPLEFTFRAMEHEIGHT - BOTTOMLEFTFRAMEHEIGHT );

                    painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ midLeftFrame ] ) : *( DECOPIXINACTARR[ midLeftFrame ] ) );

                    //                 painter.drawTiledPixmap( rect, isActive() ? decoFactory_->midLeftFrameBg_ : decoFactory_->midLeftFrameBgInAct_ );
                }

                // buttom
                if ( leftRightFramesHeight > TOPLEFTFRAMEHEIGHT )
                {
                    rect.setRect( 0, TQMAX( h - BUTTOMFRAMESIZE - BOTTOMLEFTFRAMEHEIGHT, TITLESIZE + TOPLEFTFRAMEHEIGHT ) , LEFTFRAMESIZE, TQMIN( BOTTOMLEFTFRAMEHEIGHT, leftRightFramesHeight - TOPLEFTFRAMEHEIGHT ) );

                    painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ buttomLeftFrame ] ) : *( DECOPIXINACTARR[ buttomLeftFrame ] ), 0, ( leftRightFramesHeight > TOPLEFTFRAMEHEIGHT + BOTTOMLEFTFRAMEHEIGHT ) ? 0 : TITLESIZE + TOPLEFTFRAMEHEIGHT - ( h - BUTTOMFRAMESIZE - BOTTOMLEFTFRAMEHEIGHT ) );

                    //                 painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? decoFactory_->buttomLeftFrameBg_ : decoFactory_->buttomLeftFrameBgInAct_, 0, ( leftRightFramesHeight > TOPLEFTFRAMEHEIGHT + BOTTOMLEFTFRAMEHEIGHT ) ? 0 : TITLESIZE + TOPLEFTFRAMEHEIGHT - ( h - BUTTOMFRAMESIZE - BOTTOMLEFTFRAMEHEIGHT ) );
                }


                //rightFrame
                // top
                rect.setRect( w - RIGHTFRAMESIZE, TITLESIZE , RIGHTFRAMESIZE, TQMIN( TOPRIGHTFRAMEHEIGHT, leftRightFramesHeight ) );

                painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ topRightFrame ] ) : *( DECOPIXINACTARR[ topRightFrame ] ) );

                //             painter.drawTiledPixmap( rect, isActive() ? decoFactory_->topRightFrameBg_ : decoFactory_->topRightFrameBgInAct_ );

                // mid
                if ( leftRightFramesHeight > TOPRIGHTFRAMEHEIGHT + BOTTOMRIGHTFRAMEHEIGHT )
                {
                    rect.setRect( w - RIGHTFRAMESIZE, TITLESIZE + TOPRIGHTFRAMEHEIGHT, RIGHTFRAMESIZE, leftRightFramesHeight - TOPRIGHTFRAMEHEIGHT - BOTTOMRIGHTFRAMEHEIGHT );

                    painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ midRightFrame ] ) : *( DECOPIXINACTARR[ midRightFrame ] ) );

                    //                 painter.drawTiledPixmap( rect, isActive() ? decoFactory_->midRightFrameBg_ : decoFactory_->rightFrameBgInAct_ );
                }

                // bottom
                if ( leftRightFramesHeight > TOPRIGHTFRAMEHEIGHT )
                {
                    rect.setRect( w - RIGHTFRAMESIZE, TQMAX( h - BUTTOMFRAMESIZE - BOTTOMRIGHTFRAMEHEIGHT, TITLESIZE + TOPRIGHTFRAMEHEIGHT ) , RIGHTFRAMESIZE, TQMIN( BOTTOMRIGHTFRAMEHEIGHT, leftRightFramesHeight - TOPRIGHTFRAMEHEIGHT ) );

                    painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ buttomRightFrame ] ) : *( DECOPIXINACTARR[ buttomRightFrame ] ), 0, ( leftRightFramesHeight > TOPRIGHTFRAMEHEIGHT + BOTTOMRIGHTFRAMEHEIGHT ) ? 0 : TITLESIZE + TOPRIGHTFRAMEHEIGHT - ( h - BUTTOMFRAMESIZE - BOTTOMRIGHTFRAMEHEIGHT ) );

                    //                 painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? decoFactory_->buttomRightFrameBg_ : decoFactory_->buttomRightFrameBgInAct_, 0, ( leftRightFramesHeight > TOPRIGHTFRAMEHEIGHT + BOTTOMRIGHTFRAMEHEIGHT ) ? 0 : TITLESIZE + TOPRIGHTFRAMEHEIGHT - ( h - BUTTOMFRAMESIZE - BOTTOMRIGHTFRAMEHEIGHT ) );
                }
            }


            // buttom frame
            if ( w > 0 )
            {            // left
                rect.setRect( 0 , h - BUTTOMFRAMESIZE, TQMIN( LEFTBOTTOMFRAMEWIDTH, w ) , BUTTOMFRAMESIZE );

                painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ leftButtomFrame ] ) : *( DECOPIXINACTARR[ leftButtomFrame ] ) );

                // mid
                if ( w > LEFTBOTTOMFRAMEWIDTH + RIGHTBOTTOMFRAMEWIDTH )
                {
                    rect.setRect( LEFTBOTTOMFRAMEWIDTH , h - BUTTOMFRAMESIZE, w - LEFTBOTTOMFRAMEWIDTH - RIGHTBOTTOMFRAMEWIDTH, BUTTOMFRAMESIZE );

                    painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ midButtomFrame ] ) : *( DECOPIXINACTARR[ midButtomFrame ] ) );

                    //                 painter.drawTiledPixmap( rect, isActive() ? decoFactory_->midButtomFrameBg_ : decoFactory_->midButtomFrameBgInAct_ );
                }

                // right
                if ( w > LEFTBOTTOMFRAMEWIDTH )
                {
                    rect.setRect( TQMAX( w - RIGHTBOTTOMFRAMEWIDTH, LEFTBOTTOMFRAMEWIDTH ) , h - BUTTOMFRAMESIZE, TQMIN( RIGHTBOTTOMFRAMEWIDTH, w - LEFTBOTTOMFRAMEWIDTH ) , BUTTOMFRAMESIZE );

                    painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ rightButtomFrame ] ) : *( DECOPIXINACTARR[ rightButtomFrame ] ), ( w > LEFTBOTTOMFRAMEWIDTH + RIGHTBOTTOMFRAMEWIDTH ) ? 0 : LEFTBOTTOMFRAMEWIDTH - ( w - RIGHTBOTTOMFRAMEWIDTH ), 0 );

                    //                 painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? decoFactory_->rightButtomFrameBg_ : decoFactory_->rightButtomFrameBgInAct_, ( w > LEFTBOTTOMFRAMEWIDTH + RIGHTBOTTOMFRAMEWIDTH ) ? 0 : LEFTBOTTOMFRAMEWIDTH - ( w - RIGHTBOTTOMFRAMEWIDTH ), 0 );
                }
            }
        }
        else
        {
            painter.setPen( TQColor( 70, 70, 70 ) );
            painter.drawLine( 0 , h - 1, w, h - 1 );
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// update_captionBuffer()
// ------------
//
void DeKoratorClient::updateCaptionBuffer()
{
    if ( !DeKoratorFactory::initialized() )
        return ;

    TQPainter painter;
    TQString c( caption() );
    TQFontMetrics fm( options() ->font( isActive() ) );
    int captionWidth = fm.width( c );
    int captionHeight = fm.height( );
    TQPixmap textPixmap;

    // prepare the shadow
    textPixmap = TQPixmap( captionWidth + ( MARGIN * 2 ) , captionHeight ); // 2*2 px shadow space
    textPixmap.fill( TQColor( 0, 0, 0 ) );
    textPixmap.setMask( textPixmap.createHeuristicMask( TRUE ) );
    painter.begin( &textPixmap );
    painter.setFont( options() ->font( isActive(), FALSE ) );
    painter.setPen( white );
    painter.drawText( textPixmap.rect(), AlignCenter, caption() );
    painter.end();


    ShadowEngine se;
    //if ( isActive() )
    activeShadowImg_ = se.makeShadow( textPixmap, ACTIVESHADECOLOR );
    //else
    inActiveShadowImg_ = se.makeShadow( textPixmap, INACTIVESHADECOLOR );

    captionBufferDirty_ = false;
}

//////////////////////////////////////////////////////////////////////////////
// resizeEvent()
// -------------
// Window is being resized
void DeKoratorClient::resizeEvent( TQResizeEvent *e )
{
    if ( widget() ->isShown() )
    {
        TQRegion region = widget() ->rect();
        region = region.subtract( titleBarSpacer_->geometry() );
        widget() ->erase( region );
    }
    if ( USEMASKS )
    {

        if ( oldSize_ != e->size() )
        {
            //tqWarning("TQResizeEvent");
            sizeChanged = true;
        }
        else
            sizeChanged = false;

        oldSize_ = e->size();

        // 		if(e->type() == TQEvent::Resize)
        // 		{
        //
        // 			//oldSize((TQResizeEvent*)e)->size());
        // 			tqWarning( "resizeEvent" );
        // 		}

        //        maskDirty_ = true;
        doShape();
    }
}

//////////////////////////////////////////////////////////////////////////////
// showEvent()
// -----------
// Window is being shown
void DeKoratorClient::showEvent( TQShowEvent * )
{
    widget() ->repaint();
    if ( USEMASKS )
        doShape();
}

//////////////////////////////////////////////////////////////////////////////
// maxButtonPressed()
// -----------------
// Max button was pressed
void DeKoratorClient::maxButtonPressed()
{
    if ( button[ ButtonMax ] )
    {
#if KDE_IS_VERSION(3, 3, 0)
        maximize( button[ ButtonMax ] ->lastMousePress() );
#else

        switch ( button[ ButtonMax ] ->lastMousePress() )
        {
        case MidButton:
            maximize( maximizeMode() ^ MaximizeVertical );
            break;
        case RightButton:
            maximize( maximizeMode() ^ MaximizeHorizontal );
            break;
        default:
            ( maximizeMode() == MaximizeFull ) ? maximize( MaximizeRestore )
            : maximize( MaximizeFull );
        }
#endif

    }
}

//////////////////////////////////////////////////////////////////////////////
// shadeButtonPressed()
// -----------------
// Shade button was pressed
void DeKoratorClient::shadeButtonPressed()
{
    if ( button[ ButtonShade ] )
    {
        setShade( !isSetShade() );
    }
}

//////////////////////////////////////////////////////////////////////////////
// aboveButtonPressed()
// -----------------
// Above button was pressed
void DeKoratorClient::aboveButtonPressed()
{
    if ( button[ ButtonAbove ] )
    {
        setKeepAbove( !keepAbove() );
    }
}

//////////////////////////////////////////////////////////////////////////////
// belowButtonPressed()
// -----------------
// Below buttTQt::green; //on was pressed
void DeKoratorClient::belowButtonPressed()
{
    if ( button[ ButtonBelow ] )
    {
        setKeepBelow( !keepBelow() );
    }
}

//////////////////////////////////////////////////////////////////////////////
// menuButtonPressed()
// -------------------
// Menu button was pressed (popup the menu)
void DeKoratorClient::menuButtonPressed()
{
    //     if ( button[ ButtonMenu ] )
    //     {
    //         TQPoint p( button[ ButtonMenu ] ->rect().bottomLeft().x(),
    //                   button[ ButtonMenu ] ->rect().bottomLeft().y() );
    //         KDecorationFactory* f = factory();
    //         showWindowMenu( button[ ButtonMenu ] ->mapToGlobal( p ) );
    //         if ( !f->exists( this ) ) return ; // decoration was destroyed
    //         button[ ButtonMenu ] ->setDown( FALSE );
    //     }

    static TQTime * t = NULL;
    static DeKoratorClient* lastClient = NULL;
    if ( t == NULL )
        t = new TQTime;
    bool dbl = ( lastClient == this && t->elapsed() <= TQApplication::doubleClickInterval() );
    lastClient = this;
    t->start();
    if ( !dbl || !DBLCLKCLOSE )
    {
        TQPoint p( button[ ButtonMenu ] ->rect().bottomLeft().x(),
                  button[ ButtonMenu ] ->rect().bottomLeft().y() );
        KDecorationFactory* f = factory();
        showWindowMenu( button[ ButtonMenu ] ->mapToGlobal( p ) );
        if ( !f->exists( this ) )
            return ; // decoration was destroyed
        button[ ButtonMenu ] ->setDown( FALSE );
    }
    else
        closing_ = true;
}

//////////////////////////////////////////////////////////////////////////////
// menuButtonReleased()
// -------------------
//
void DeKoratorClient::menuButtonReleased()
{
    if ( closing_ )
        closeWindow();
}

//////////////////////////////////////////////////////////////////////////////
// doShape()
// -------------------
//
void DeKoratorClient::doShape()
{
    int w = width();
    int h = height();
    TQRegion mask( 0, 0, w, h );

    if ( sizeChanged )
    {
        //tqWarning("doShape");

        // top left
        TQRegion mtr;
        TQRegion m = TQRegion( decoFactory_->topLeftCornerBitmap_ );
        mask -= TQRegion( m );


        // top mid
        if ( TOPMIDMASKWIDTH >= 1 )
        {
            int pos = TOPLEFTMASKWIDTH;
            int rep = ( w - TOPLEFTMASKWIDTH - TOPRIGHTMASKWIDTH ) / TOPMIDMASKWIDTH;
            m = TQRegion( decoFactory_->topMidBitmap_ );
            TQRegion mBak = m;

            for ( int i = 0 ; i < rep ; i++ )
            {
                m = mBak;
                m.translate( TOPLEFTMASKWIDTH + ( i * TOPMIDMASKWIDTH ), 0 );
                mask -= TQRegion( m );
                pos += TOPMIDMASKWIDTH;
            }

            m = mBak;
            mtr = mBak;
            mtr.translate( w - pos - TOPRIGHTMASKWIDTH, 0 );
            m -= mtr;
            m.translate( pos, 0 );
            mask -= m;
        }

        {
            //top right
            m = TQRegion( decoFactory_->topRightCornerBitmap_ );
            m.translate( width() - TOPRIGHTMASKWIDTH, 0 );
            mask -= TQRegion( m );
        }


        if ( !isShade() || SHOWBTMBORDER )
        {
            //buttom left
            m = TQRegion( decoFactory_->buttomLeftCornerBitmap_ );
            m.translate( 0, h - BOTTOMLEFTMASKHEIGHT );
            mask -= TQRegion( m );

            // bottom mid
            if ( BOTTOMMIDMASKWIDTH >= 1 )
            {
                int pos = BOTTOMLEFTMASKWIDTH;
                int rep = ( w - BOTTOMLEFTMASKWIDTH - BOTTOMRIGHTMASKWIDTH ) / BOTTOMMIDMASKWIDTH;
                int hm = h - BOTTOMMIDMASKHEIGHT;
                m = TQRegion( decoFactory_->buttomMidBitmap_ );
                TQRegion mBak = m;

                for ( int i = 0 ; i < rep ; i++ )
                {
                    m = mBak;
                    m.translate( BOTTOMLEFTMASKWIDTH + ( i * BOTTOMMIDMASKWIDTH ), hm );
                    mask -= TQRegion( m );
                    pos += BOTTOMMIDMASKWIDTH;
                }

                m = mBak;
                mtr = mBak;
                mtr.translate( w - pos - BOTTOMRIGHTMASKWIDTH, 0 );
                m -= mtr;
                m.translate( pos, hm );
                mask -= m;
            }

            //buttom right
            m = TQRegion( decoFactory_->buttomRightCornerBitmap_ );
            m.translate( width() - BOTTOMRIGHTMASKWIDTH, h - BOTTOMRIGHTMASKHEIGHT );
            mask -= TQRegion( m );
        }
        mask_ = mask;
    }

    setMask( mask_ );
}


#include "deKoratorclient.moc"