///////////////////////////////////////////////////////////////////////
// -------------------
// 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.
///////////////////////////////////////////////////////////////////////

#if defined(HAVE_CONFIG_H)
# include "config.h"
#endif

#include "deKoratorclient.h"
#include "imageholder.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 TOPFRAMESIZE = 0;
static int LEFTFRAMESIZE = 0;
static int BOTTOMFRAMESIZE = 0;
static int BOTTOMSHADEDFRAMESIZE = 0;
static int RIGHTFRAMESIZE = 0;

static bool HASTOPFRAME = false;
static bool HASBOTTOMSHADEDFRAME = false;

// deco
static int LEFTUPPERCORNERWIDTH;
static int RIGHTUPPERCORNERWIDTH;
static int TITLELEFTWIDTH;
static int TITLERIGHTWIDTH;
static int LEFTUPPERFRAMEHEIGHT;
static int LEFTLOWERCORNERFRAMEHEIGHT;
static int RIGHTUPPERFRAMEHEIGHT;
static int RIGHTLOWERCORNERFRAMEHEIGTH;
static int BOTTOMLEFTFRAMEWIDTH;
static int BOTTOMRIGHTFRAMEWIDTH;
static int BOTTOMSHADEDLEFTFRAMEWIDTH;
static int BOTTOMSHADEDRIGHTFRAMEWIDTH;
static int TOPLEFTFRAMEWIDTH;
static int TOPRIGHTFRAMEWIDTH;

// 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 BOTTOMSHADEDLEFTMASKWIDTH = 0;
static int BOTTOMSHADEDMIDMASKWIDTH = 0;
static int BOTTOMSHADEDRIGHTMASKWIDTH = 0;

static int BOTTOMLEFTMASKHEIGHT = 0;
static int BOTTOMMIDMASKHEIGHT = 0;
static int BOTTOMRIGHTMASKHEIGHT = 0;
static int BOTTOMSHADEDLEFTMASKHEIGHT = 0;
static int BOTTOMSHADEDMIDMASKHEIGHT = 0;
static int BOTTOMSHADEDRIGHTMASKHEIGHT = 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 = 0;
static int ACTIVESHDTEXTY = 0;
static TQColor ACTIVESHADECOLOR = TQColor( 150, 150, 150 );
static int INACTIVESHDTEXTX = 0;
static int INACTIVESHDTEXTY = 0;
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 bool 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_ = "";

bool DeKoratorFactory::transparency_ = false;

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

//////////////////////////////////////////////////////////////////////////////
// DeKoratorFactory()
// ----------------
// Constructor
DeKoratorFactory::DeKoratorFactory()
{
    TDEGlobal::locale()->insertCatalogue("twin_clients");
    TDEGlobal::locale()->insertCatalogue("twin-style-dekorator");

    readConfig();
    initialized_ = true;

    imageHolder = new QImageHolder(TQImage(), TQImage());

    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;

    if (imageHolder)
        delete imageHolder;

    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 oldTransparency = DeKoratorFactory::transparency_;
    DeKoratorFactory::transparency_ = config.readBoolEntry( "Transparency", false );

    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 &&
            oldTransparency == DeKoratorFactory::transparency_ &&
            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_ ||
                oldTransparency != DeKoratorFactory::transparency_
           )
            DeKoratorFactory::needReload_ = true;
        else
            DeKoratorFactory::needReload_ = false;

        return true;
    }
}

//////////////////////////////////////////////////////////////////////////////
// loadBorderPixmap()
// ------------
// Load border pixmaps and try to also get pixmaps for inactive state.
bool DeKoratorFactory::loadBorderPixmap(decoType borderPart, const TQString& newName, const TQString& oldName) {
    TQString decoPixDir = DeKoratorFactory::framesPath_;
    TQString preferredName;
    TQString fallbackName;

    // Solve collision on parts 'bottomLeftFrame' and 'bottomRightFrame' between new and old image names.
    if ( borderPart == bottomLeftFrame || borderPart == bottomRightFrame ) {
        preferredName = oldName;
        fallbackName = newName;
    }
    else
    {
        preferredName = newName;
        fallbackName = oldName;
    }

    bool imgLoaded = DECOARR[ borderPart ][ orig ] ->load( decoPixDir + "/" + preferredName + "Bg.png" );
    if ( imgLoaded ) {
        if ( !DECOARR[ borderPart ][ inActOrig ]->load( decoPixDir + "/" + preferredName + "InactiveBg.png" ) )
            DECOARR[ borderPart ][ inActOrig ] ->load( decoPixDir + "/" + preferredName + "Bg.png" );
    }
    else
    {
        imgLoaded = DECOARR[ borderPart ][ orig ] ->load( decoPixDir + "/" + fallbackName + "Bg.png" );
        if ( !DECOARR[ borderPart ][ inActOrig ]->load( decoPixDir + "/" + fallbackName + "InactiveBg.png" ) )
            DECOARR[ borderPart ][ inActOrig ] ->load( decoPixDir + "/" + fallbackName + "Bg.png" );
    }
    if ( imgLoaded )
        return true;
    else
        return false;
}

//////////////////////////////////////////////////////////////////////////////
// loadButtonPixmap()
// ------------
// Load button pixmaps and try to also get pixmaps for inactive state.
void DeKoratorFactory::loadButtonPixmap(buttonTypeAll btnType, buttonState btnState, const TQString& imageName) {
    TQString btnPixDir = DeKoratorFactory::buttonsPath_;
    TQString statePath;

    switch (btnState) {
        case regular:
            statePath = "/normal/";
            break;
        case press:
            statePath = "/press/";
            break;
        case hover:
            statePath = "/hover/";
            break;
    }

    bool origLoaded = BUTTONSARR[ btnType ][ btnState ][ normal ] ->load( btnPixDir + statePath + imageName + ".png" );
    if ( !BUTTONSARR[ btnType ][ btnState ][ inAct ]->load( btnPixDir + statePath + imageName + "Inactive.png" ) ) {
        BUTTONSARR[ btnType ][ btnState ][ inAct ] ->load( btnPixDir + statePath + imageName + ".png" );
    }
}


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


    // deco
    // top frame from left to right
    // enable only if all images exist
    bool optLeft = loadBorderPixmap( topLeftFrame, "topLeftFrame", "leftTopFrame");
    bool optMid = loadBorderPixmap( topMidFrame, "topMidFrame", "midTopFrame");
    bool optRight = loadBorderPixmap( topRightFrame, "topRightFrame", "rightTopFrame");
    HASTOPFRAME = optLeft && optMid && optRight;

    // title bar from left to right
    loadBorderPixmap( leftUpperCornerFrame, "leftUpperCornerFrame", "topLeftCorner");
    loadBorderPixmap( buttonsLeft, "buttonsLeft", "leftButtons");
    loadBorderPixmap( titleLeft, "titleLeft", "leftTitle");
    loadBorderPixmap( titleMid, "titleMid", "midTitle");
    loadBorderPixmap( titleRight, "titleRight", "rightTitle");
    loadBorderPixmap( buttonsRight, "buttonsRight", "rightButtons");
    loadBorderPixmap( rightUpperCornerFrame, "rightUpperCornerFrame", "topRightCorner");
    // left frame from top to bottom
    loadBorderPixmap( leftUpperFrame, "leftUpperFrame", "topLeftFrame");
    loadBorderPixmap( leftMidFrame, "leftMidFrame", "midLeftFrame");
    loadBorderPixmap( leftLowerCornerFrame, "leftLowerCornerFrame", "bottomLeftFrame");
    // bottom frame from left to right
    loadBorderPixmap( bottomLeftFrame, "bottomLeftFrame", "leftBottomFrame");
    loadBorderPixmap( bottomMidFrame, "bottomMidFrame", "midBottomFrame");
    loadBorderPixmap( bottomRightFrame, "bottomRightFrame", "rightBottomFrame");
    // bottom shaded frame from left to right
    // enable only if all images exist
    optLeft = loadBorderPixmap( bottomShadedLeftFrame, "bottomShadedLeftFrame", "leftBottomShadedFrame");
    optMid = loadBorderPixmap( bottomShadedMidFrame, "bottomShadedMidFrame", "midBottomShadedFrame");
    optRight = loadBorderPixmap( bottomShadedRightFrame, "bottomShadedRightFrame", "rightBottomShadedFrame");
    HASBOTTOMSHADEDFRAME = optLeft && optMid && optRight;

    // right frame from top to bottom
    loadBorderPixmap( rightUpperFrame, "rightUpperFrame", "topRightFrame");
    loadBorderPixmap( rightMidFrame, "rightMidFrame", "midRightFrame");
    loadBorderPixmap( rightLowerCornerFrame, "rightLowerCornerFrame", "bottomRightFrame");


    // buttons
    loadButtonPixmap( restore, regular, "buttonRestore");
    loadButtonPixmap( restore, press, "buttonRestorePress");
    loadButtonPixmap( restore, hover, "buttonRestoreHover");

    loadButtonPixmap( help, regular, "buttonHelp");
    loadButtonPixmap( help, press, "buttonHelpPress");
    loadButtonPixmap( help, hover, "buttonHelpHover");

    loadButtonPixmap( max, regular, "buttonMax");
    loadButtonPixmap( max, press, "buttonMaxPress");
    loadButtonPixmap( max, hover, "buttonMaxHover");

    loadButtonPixmap( min, regular, "buttonMin");
    loadButtonPixmap( min, press, "buttonMinPress");
    loadButtonPixmap( min, hover, "buttonMinHover");

    loadButtonPixmap( close, regular, "buttonClose");
    loadButtonPixmap( close, press, "buttonClosePress");
    loadButtonPixmap( close, hover, "buttonCloseHover");

    loadButtonPixmap( sticky, regular, "buttonSticky");
    loadButtonPixmap( sticky, press, "buttonStickyDownPress");
    loadButtonPixmap( sticky, hover, "buttonStickyHover");

    loadButtonPixmap( stickydown, regular, "buttonStickyDown");
    loadButtonPixmap( stickydown, press, "buttonStickyDownPress");
    loadButtonPixmap( stickydown, hover, "buttonStickyDownHover");

    loadButtonPixmap( above, regular, "buttonAbove");
    loadButtonPixmap( above, press, "buttonAbovePress");
    loadButtonPixmap( above, hover, "buttonAboveHover");

    loadButtonPixmap( abovedown, regular, "buttonAboveDown");
    loadButtonPixmap( abovedown, press, "buttonAboveDownPress");
    loadButtonPixmap( abovedown, hover, "buttonAboveDownHover");

    loadButtonPixmap( below, regular, "buttonBelow");
    loadButtonPixmap( below, press, "buttonBelowPress");
    loadButtonPixmap( below, hover, "buttonBelowHover");

    loadButtonPixmap( belowdown, regular, "buttonBelowDown");
    loadButtonPixmap( belowdown, press, "buttonBelowDownPress");
    loadButtonPixmap( belowdown, hover, "buttonBelowDownHover");

    loadButtonPixmap( shade, regular, "buttonShade");
    loadButtonPixmap( shade, press, "buttonShadePress");
    loadButtonPixmap( shade, hover, "buttonShadeHover");

    loadButtonPixmap( shadedown, regular, "buttonShadeDown");
    loadButtonPixmap( shadedown, press, "buttonShadeDownPress");
    loadButtonPixmap( shadedown, hover, "buttonShadeDownHover");

    loadButtonPixmap( menu, regular, "buttonMenu");
    loadButtonPixmap( menu, press, "buttonMenuPress");
    loadButtonPixmap( menu, hover, "buttonMenuHover");


    //masks
    topLeftCornerMaskBitmap_ = TQBitmap( masksPixDir + "/topLeftCornerBitmap.png" );
    topMidMaskBitmap_ = TQBitmap( masksPixDir + "/topMidBitmap.png" );
    topRightCornerMaskBitmap_ = TQBitmap( masksPixDir + "/topRightCornerBitmap.png" );
    buttomLeftCornerMaskBitmap_ = TQBitmap( masksPixDir + "/buttomLeftCornerBitmap.png" );
    buttomMidMaskBitmap_ = TQBitmap( masksPixDir + "/buttomMidBitmap.png" );
    buttomRightCornerMaskBitmap_ = TQBitmap( masksPixDir + "/buttomRightCornerBitmap.png" );
    buttomShadedLeftCornerMaskBitmap_ = TQBitmap( masksPixDir + "/buttomShadedLeftCornerBitmap.png" );
    buttomShadedMidMaskBitmap_ = TQBitmap( masksPixDir + "/buttomShadedMidBitmap.png" );
    buttomShadedRightCornerMaskBitmap_ = TQBitmap( masksPixDir + "/buttomShadedRightCornerBitmap.png" );

    determineSizes();

    //prepareDecoWithBgCol();

}

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

    // frames
    TOPFRAMESIZE = HASTOPFRAME ? DECOARR[ topMidFrame ][ orig ] ->height() : 0;
    TITLESIZE = DECOARR[ titleMid ][ orig ] ->height();
    LEFTFRAMESIZE = DECOARR[ leftMidFrame ][ orig ] ->width();
    BOTTOMFRAMESIZE = DECOARR[ bottomMidFrame ][ orig ] ->height();
    BOTTOMSHADEDFRAMESIZE = DECOARR[ bottomShadedMidFrame ][ orig ] ->height();
    RIGHTFRAMESIZE = DECOARR[ rightMidFrame ][ orig ] ->width();

    LEFTUPPERCORNERWIDTH = DECOARR[ leftUpperCornerFrame ][ orig ] ->width();
    RIGHTUPPERCORNERWIDTH = DECOARR[ rightUpperCornerFrame ][ orig ] ->width();
    TITLELEFTWIDTH = DECOARR[ titleLeft ][ orig ] ->width();
    TITLERIGHTWIDTH = DECOARR[ titleRight ][ orig ] ->width();
    LEFTUPPERFRAMEHEIGHT = DECOARR[ leftUpperFrame ][ orig ] ->height();
    LEFTLOWERCORNERFRAMEHEIGHT = DECOARR[ leftLowerCornerFrame ][ orig ] ->height();
    RIGHTUPPERFRAMEHEIGHT = DECOARR[ rightUpperFrame ][ orig ] ->height();
    RIGHTLOWERCORNERFRAMEHEIGTH = DECOARR[ rightLowerCornerFrame ][ orig ] ->height();
    BOTTOMLEFTFRAMEWIDTH = DECOARR[ bottomLeftFrame ][ orig ] ->width();
    BOTTOMRIGHTFRAMEWIDTH = DECOARR[ bottomRightFrame ][ orig ] ->width();
    BOTTOMSHADEDLEFTFRAMEWIDTH = DECOARR[ bottomShadedLeftFrame ][ orig ] ->width();
    BOTTOMSHADEDRIGHTFRAMEWIDTH = DECOARR[ bottomShadedRightFrame ][ orig ] ->width();
    TOPLEFTFRAMEWIDTH = DECOARR[ topLeftFrame ][ orig ] ->width();
    TOPRIGHTFRAMEWIDTH = DECOARR[ topRightFrame ][ 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 = topLeftCornerMaskBitmap_.width();
    TOPMIDMASKWIDTH = topMidMaskBitmap_.width();
    TOPRIGHTMASKWIDTH = topRightCornerMaskBitmap_.width();
    BOTTOMLEFTMASKWIDTH = buttomLeftCornerMaskBitmap_.width();
    BOTTOMMIDMASKWIDTH = buttomMidMaskBitmap_.width();
    BOTTOMRIGHTMASKWIDTH = buttomRightCornerMaskBitmap_.width();
    BOTTOMSHADEDLEFTMASKWIDTH = buttomShadedLeftCornerMaskBitmap_.width();
    BOTTOMSHADEDMIDMASKWIDTH = buttomShadedMidMaskBitmap_.width();
    BOTTOMSHADEDRIGHTMASKWIDTH = buttomShadedRightCornerMaskBitmap_.width();

    BOTTOMLEFTMASKHEIGHT = buttomLeftCornerMaskBitmap_.height();
    BOTTOMMIDMASKHEIGHT = buttomMidMaskBitmap_.height();
    BOTTOMRIGHTMASKHEIGHT = buttomRightCornerMaskBitmap_.height();
    BOTTOMSHADEDLEFTMASKHEIGHT = buttomShadedLeftCornerMaskBitmap_.height();
    BOTTOMSHADEDMIDMASKHEIGHT = buttomShadedMidMaskBitmap_.height();
    BOTTOMSHADEDRIGHTMASKHEIGHT = buttomShadedRightCornerMaskBitmap_.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 ][ inActOrig ] );
            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 ][ inAct ] );
                    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 ][ inAct ] );
                    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 ][ inAct ];
        }
    }



    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 ][ inAct ];
            }
        }
    }

}

//////////////////////////////////////////////////////////////////////////////
// prepareDecoWithBgCol()
//
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++ )
        {
            if (transparency_) {
                //DECOARR[ i ][ actCol ] = DECOARR[ i ][ actCol ];
                continue;
            }

            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++ )
        {
            if (transparency_) {
                DECOARR[ i ][ normal ] = new TQPixmap(*DECOARR[ i ][ orig ]);
                continue;
            }

            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++ )
        {
            if (transparency_) {
                //DECOARR[ i ][ inActCol ] = DECOARR[ i ][ inActCol ];
                continue;
            }

            tempPix.resize( DECOARR[ i ][ inActOrig ] ->width(), DECOARR[ i ][ inActOrig ] ->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++ )
        {
            if (transparency_) {
                DECOARR[ i ][ inAct ] = new TQPixmap(*DECOARR[ i ][ inActOrig ]);
                continue;
            }

            tempPix.resize( DECOARR[ i ][ inActOrig ] ->width(), DECOARR[ i ][ inActOrig ] ->height() );

            tempPix.fill( col );

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

            *( DECOARR[ i ][ inAct ] ) = 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, TQ_SIGNAL( timeout() ), this, TQ_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;
    TQPixmap *background = client_->decoFactory_->imageHolder->image(act);

    if (background && DeKoratorFactory::transparency_) {
        TQRect r = rect();
        TQPoint p = mapToGlobal( TQPoint(0,0) );
        r.moveBy( p.x(), p.y() );
        painter->drawPixmap( TQPoint(0,0), *background, r );
    }

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

    // 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 )
    {
        TQColorGroup g;
        g = KDecoration::options()->colorGroup(KDecoration::ColorButtonBg, true);
        painter->setPen( TQColor( g.dark().dark(155) ) );
        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 ) , topSpacer_( 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 topSpacer_;
    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" );*/

    // spacers
    topSpacer_ = new TQSpacerItem( 1, TOPFRAMESIZE,
                                     TQSizePolicy::Expanding, TQSizePolicy::Fixed );
    leftTitleBarSpacer_ = new TQSpacerItem( LEFTUPPERCORNERWIDTH, TITLESIZE,
                                           TQSizePolicy::Fixed, TQSizePolicy::Fixed );
    titleBarSpacer_ = new TQSpacerItem( 1, TITLESIZE, TQSizePolicy::Expanding,
                                       TQSizePolicy::Fixed );
    rightTitleBarSpacer_ = new TQSpacerItem( RIGHTUPPERCORNERWIDTH, 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 ) ? BOTTOMFRAMESIZE : 0,
                                     TQSizePolicy::Expanding, TQSizePolicy::Fixed );


    // layouts
    mainLayout_ = new TQVBoxLayout( widget(), 0, 0 );
    //add top spacer atop the other layouts
    mainLayout_->addItem( topSpacer_ );

    titleLayout_ = new TQHBoxLayout( mainLayout_, 0, 0 );
    midLayout_ = new TQHBoxLayout( mainLayout_, 0, 0 );

    // 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, TQ_SIGNAL( keepAboveChanged( bool ) ), TQ_SLOT( keepAboveChange( bool ) ) );
    connect( this, TQ_SIGNAL( keepBelowChanged( bool ) ), TQ_SLOT( keepBelowChange( bool ) ) );

    if (DeKoratorFactory::transparency_)
    {
        connect( decoFactory_->imageHolder, TQ_SIGNAL( repaintNeeded() ), this, TQ_SLOT( repaint() ) );
        connect( &repaintTimer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( repaint() ) );
    }

    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 ], TQ_SIGNAL( pressed() ),
                             this, TQ_SLOT( menuButtonPressed() ) );
                    connect( button[ ButtonMenu ], TQ_SIGNAL( released() ), this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 ], TQ_SIGNAL( clicked() ),
                             this, TQ_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 = TOPFRAMESIZE + TITLESIZE ;
    //     if ( SHOWBTMBORDER )
    //b = 10;
    //     else
    //         b = isShade() ? 0 : BOTTOMFRAMESIZE;
    //b = SHOWBTMBORDER ? BOTTOMFRAMESIZE : isShade() ? 1 : BOTTOMFRAMESIZE;
    if ( !isShade() || SHOWBTMBORDER )
    {
        if ( isShade() && HASBOTTOMSHADEDFRAME )
        {
            b = BOTTOMSHADEDFRAMESIZE;
            bottomSpacer_->changeSize( 1, BOTTOMSHADEDFRAMESIZE, TQSizePolicy::Expanding, TQSizePolicy::Fixed );
        }
        else
        {
            b = BOTTOMFRAMESIZE;
            bottomSpacer_->changeSize( 1, BOTTOMFRAMESIZE, 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() + LEFTUPPERCORNERWIDTH + RIGHTUPPERCORNERWIDTH );

    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 ( HASTOPFRAME == true && point.y() <= TOPFRAMESIZE )
    {
        // inside top frame
        if ( point.x() <= TQMAX( TOPLEFTFRAMEWIDTH, LEFTFRAMESIZE ) )
            pos = PositionTopLeft;
        else if ( point.x() >= ( width() - TQMAX( TOPRIGHTFRAMEWIDTH, RIGHTFRAMESIZE ) ) )
            pos = PositionTopRight;
        else
            pos = PositionTop;
    }
    else if ( HASTOPFRAME == false && point.y() <= 5 )
    {
        // inside titlebar
        if ( point.x() <= TQMAX( BOTTOMLEFTFRAMEWIDTH, LEFTFRAMESIZE ) )
            pos = PositionTopLeft;
        else if ( point.x() >= ( width() - TQMAX( BOTTOMRIGHTFRAMEWIDTH, RIGHTFRAMESIZE ) ) )
            pos = PositionTopRight;
        else
            pos = PositionTop;
    }
    else if ( point.y() >= ( height() - BOTTOMFRAMESIZE ) )
    {
        // inside handle
        if ( point.x() <= TQMAX( BOTTOMLEFTFRAMEWIDTH, LEFTFRAMESIZE ) )
            pos = PositionBottomLeft;
        else if ( point.x() >= ( width() - TQMAX( BOTTOMRIGHTFRAMEWIDTH, RIGHTFRAMESIZE ) ) )
            pos = PositionBottomRight;
        else
            pos = PositionBottom;
    }
    else if ( point.x() <= LEFTFRAMESIZE )
    {
        // on left frame
        if ( HASTOPFRAME == true && point.y() <= TOPFRAMESIZE + TITLESIZE )
            pos = PositionTopLeft;
        else if ( HASTOPFRAME == false && point.y() <= TITLESIZE )
            pos = PositionTopLeft;
        else if ( point.y() >= ( height() - LEFTLOWERCORNERFRAMEHEIGHT - BOTTOMFRAMESIZE ) )
            pos = PositionBottomLeft;
        else
            pos = PositionLeft;
    }
    else if ( point.x() >= width() - RIGHTFRAMESIZE )
    {
        // on right frame
        if ( HASTOPFRAME == true && point.y() <= TOPFRAMESIZE + TITLESIZE )
            pos = PositionTopRight;
        else if ( HASTOPFRAME == false && point.y() <= TITLESIZE )
            pos = PositionTopRight;
        else if ( point.y() >= ( height() - RIGHTLOWERCORNERFRAMEHEIGTH - BOTTOMFRAMESIZE ) )
            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() );
}

//////////////////////////////////////////////////////////////////////////////
// repaint()
// -----------------------
//
void DeKoratorClient::repaint() {
    widget()->repaint();
    for (int n=0; n<ButtonTypeCount; n++)
        if (button[n]) button[n]->reset();
}

//////////////////////////////////////////////////////////////////////////////
// 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;
        TQPixmap* background = decoFactory_->imageHolder->image(isActive());
        TQPoint p = widget()->mapToGlobal(TQPoint(0,0));

        if (background && DeKoratorFactory::transparency_)
            painter.drawPixmap( 0, 0, *background, p.x(), p.y(), w, h );

        TQPainter painter2;
        TQPixmap pix( w, TOPFRAMESIZE + TITLESIZE );
        painter2.begin( &pix );
        {
            if (background && DeKoratorFactory::transparency_)
                painter2.drawPixmap( 0, 0, *background, p.x(), p.y(), w, TOPFRAMESIZE + TITLESIZE );

            // top frame
            if ( w > 0 )
            {            // left
                rect.setRect( 0 , 0, TQMIN( TOPLEFTFRAMEWIDTH, w ) , TOPFRAMESIZE );

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

                // mid
                if ( w > TOPLEFTFRAMEWIDTH + TOPRIGHTFRAMEWIDTH )
                {
                    rect.setRect( TOPLEFTFRAMEWIDTH , 0, w - TOPLEFTFRAMEWIDTH - TOPRIGHTFRAMEWIDTH, TOPFRAMESIZE );

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

                }

                // right
                if ( w > TOPLEFTFRAMEWIDTH )
                {
                    rect.setRect( TQMAX( w - TOPRIGHTFRAMEWIDTH, TOPLEFTFRAMEWIDTH ) , 0, TQMIN( TOPRIGHTFRAMEWIDTH, w - TOPLEFTFRAMEWIDTH ) , TOPFRAMESIZE );

                    painter2.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ topRightFrame ] ) : *( DECOPIXINACTARR[ topRightFrame ] ), ( w > TOPLEFTFRAMEWIDTH + TOPRIGHTFRAMEWIDTH ) ? 0 : TOPLEFTFRAMEWIDTH - ( w - TOPRIGHTFRAMEWIDTH ), 0 );

                }
            }


            // leftTopCorner

            rect.setRect( 0, TOPFRAMESIZE, LEFTUPPERCORNERWIDTH, TITLESIZE );
            painter2.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ leftUpperCornerFrame ] ) : *( DECOPIXINACTARR[ leftUpperCornerFrame ] ) );

            //         rect.setRect( 0, 0, LEFTUPPERCORNERWIDTH, 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[ buttonsLeft ] ) : *( DECOPIXINACTARR[ buttonsLeft ] ) );

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

                //leftTitleR
                rect.setRect( tx, TOPFRAMESIZE, TQMIN( TITLELEFTWIDTH, tw ), TITLESIZE );

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

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

                //midTitle
                if ( tw > TITLELEFTWIDTH + TITLERIGHTWIDTH )
                {
                    rect.setRect( tx + TITLELEFTWIDTH, TOPFRAMESIZE, tw - ( TITLERIGHTWIDTH + TITLELEFTWIDTH ), th );

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

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

                //rightTitleR
                if ( tw > TITLELEFTWIDTH )
                {
                    rect.setRect( TQMAX( tx + tw - TITLERIGHTWIDTH, tx + TITLELEFTWIDTH ), TOPFRAMESIZE, TQMIN( TITLERIGHTWIDTH, tw - TITLELEFTWIDTH ), th );

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

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

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


            //topRightCorner
            rect.setRect( widget() ->width() - RIGHTUPPERCORNERWIDTH, TOPFRAMESIZE, RIGHTUPPERCORNERWIDTH, TITLESIZE );

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

            // 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 + TOPFRAMESIZE, 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 - TOPFRAMESIZE - TITLESIZE - BOTTOMFRAMESIZE > 0 )
            {
                int leftRightFramesHeight = h - TOPFRAMESIZE - TITLESIZE - BOTTOMFRAMESIZE ;
                int topOffset = TOPFRAMESIZE + TITLESIZE ;

                //left frame
                //top
                rect.setRect( 0, topOffset , LEFTFRAMESIZE, TQMIN( LEFTUPPERFRAMEHEIGHT, leftRightFramesHeight ) );

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

                // mid
                if ( leftRightFramesHeight > LEFTUPPERFRAMEHEIGHT + LEFTLOWERCORNERFRAMEHEIGHT )
                {
                    rect.setRect( 0, topOffset + LEFTUPPERFRAMEHEIGHT , LEFTFRAMESIZE, leftRightFramesHeight - LEFTUPPERFRAMEHEIGHT - LEFTLOWERCORNERFRAMEHEIGHT );

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

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

                // bottom corner
                if ( leftRightFramesHeight > LEFTUPPERFRAMEHEIGHT )
                {
                    rect.setRect( 0, TQMAX( h - BOTTOMFRAMESIZE - LEFTLOWERCORNERFRAMEHEIGHT, topOffset + LEFTUPPERFRAMEHEIGHT ) , LEFTFRAMESIZE, TQMIN( LEFTLOWERCORNERFRAMEHEIGHT, leftRightFramesHeight - LEFTUPPERFRAMEHEIGHT ) );

                    painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ leftLowerCornerFrame ] ) : *( DECOPIXINACTARR[ leftLowerCornerFrame ] ), 0, ( leftRightFramesHeight > LEFTUPPERFRAMEHEIGHT + LEFTLOWERCORNERFRAMEHEIGHT ) ? 0 : topOffset + LEFTUPPERFRAMEHEIGHT - ( h - BOTTOMFRAMESIZE - LEFTLOWERCORNERFRAMEHEIGHT ) );

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


                //rightFrame
                // top
                rect.setRect( w - RIGHTFRAMESIZE, topOffset , RIGHTFRAMESIZE, TQMIN( RIGHTUPPERFRAMEHEIGHT, leftRightFramesHeight ) );

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

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

                // mid
                if ( leftRightFramesHeight > RIGHTUPPERFRAMEHEIGHT + RIGHTLOWERCORNERFRAMEHEIGTH )
                {
                    rect.setRect( w - RIGHTFRAMESIZE, topOffset + RIGHTUPPERFRAMEHEIGHT, RIGHTFRAMESIZE, leftRightFramesHeight - RIGHTUPPERFRAMEHEIGHT - RIGHTLOWERCORNERFRAMEHEIGTH );

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

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

                // bottom corner
                if ( leftRightFramesHeight > RIGHTUPPERFRAMEHEIGHT )
                {
                    rect.setRect( w - RIGHTFRAMESIZE, TQMAX( h - BOTTOMFRAMESIZE - RIGHTLOWERCORNERFRAMEHEIGTH, topOffset + RIGHTUPPERFRAMEHEIGHT ) , RIGHTFRAMESIZE, TQMIN( RIGHTLOWERCORNERFRAMEHEIGTH, leftRightFramesHeight - RIGHTUPPERFRAMEHEIGHT ) );

                    painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ rightLowerCornerFrame ] ) : *( DECOPIXINACTARR[ rightLowerCornerFrame ] ), 0, ( leftRightFramesHeight > RIGHTUPPERFRAMEHEIGHT + RIGHTLOWERCORNERFRAMEHEIGTH ) ? 0 : topOffset + RIGHTUPPERFRAMEHEIGHT - ( h - BOTTOMFRAMESIZE - RIGHTLOWERCORNERFRAMEHEIGTH ) );

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


            // bottom frame
            if ( w > 0 )
            {
                // shaded bottom border
                if ( isShade() && HASBOTTOMSHADEDFRAME == true )
                {
                    // left
                    rect.setRect( 0 , h - BOTTOMSHADEDFRAMESIZE, TQMIN( BOTTOMSHADEDLEFTFRAMEWIDTH, w ) , BOTTOMSHADEDFRAMESIZE );
                    painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ bottomShadedLeftFrame ] ) : *( DECOPIXINACTARR[ bottomShadedLeftFrame ] ) );

                    // mid
                    if ( w > BOTTOMSHADEDLEFTFRAMEWIDTH + BOTTOMSHADEDRIGHTFRAMEWIDTH )
                    {
                        rect.setRect( BOTTOMSHADEDLEFTFRAMEWIDTH , h - BOTTOMSHADEDFRAMESIZE, w - BOTTOMSHADEDLEFTFRAMEWIDTH - BOTTOMSHADEDRIGHTFRAMEWIDTH, BOTTOMSHADEDFRAMESIZE );
                        painter.drawTiledPixmap( rect, isActive() ? *( DECOPIXACTARR[ bottomShadedMidFrame ] ) : *( DECOPIXINACTARR[ bottomShadedMidFrame ] ) );
                    }
                    // right
                    if ( w > BOTTOMSHADEDLEFTFRAMEWIDTH )
                    {
                        rect.setRect( TQMAX( w - BOTTOMSHADEDRIGHTFRAMEWIDTH, BOTTOMSHADEDLEFTFRAMEWIDTH ) , h - BOTTOMSHADEDFRAMESIZE, TQMIN( BOTTOMSHADEDRIGHTFRAMEWIDTH, w - BOTTOMSHADEDLEFTFRAMEWIDTH ) , BOTTOMSHADEDFRAMESIZE );

                        painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? *( DECOPIXACTARR[ bottomShadedRightFrame ] ) : *( DECOPIXINACTARR[ bottomShadedRightFrame ] ), ( w > BOTTOMSHADEDLEFTFRAMEWIDTH + BOTTOMSHADEDRIGHTFRAMEWIDTH ) ? 0 : BOTTOMSHADEDLEFTFRAMEWIDTH - ( w - BOTTOMSHADEDRIGHTFRAMEWIDTH ), 0 );
                    }
                }
                else
                {
                    // left
                    rect.setRect( 0 , h - BOTTOMFRAMESIZE, TQMIN( BOTTOMLEFTFRAMEWIDTH, w ) , BOTTOMFRAMESIZE );

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

                    // mid
                    if ( w > BOTTOMLEFTFRAMEWIDTH + BOTTOMRIGHTFRAMEWIDTH )
                    {
                        rect.setRect( BOTTOMLEFTFRAMEWIDTH , h - BOTTOMFRAMESIZE, w - BOTTOMLEFTFRAMEWIDTH - BOTTOMRIGHTFRAMEWIDTH, BOTTOMFRAMESIZE );

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

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

                    // right
                    if ( w > BOTTOMLEFTFRAMEWIDTH )
                    {
                        rect.setRect( TQMAX( w - BOTTOMRIGHTFRAMEWIDTH, BOTTOMLEFTFRAMEWIDTH ) , h - BOTTOMFRAMESIZE, TQMIN( BOTTOMRIGHTFRAMEWIDTH, w - BOTTOMLEFTFRAMEWIDTH ) , BOTTOMFRAMESIZE );

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

                        //                 painter.drawTiledPixmap( rect.x(), rect.y(), rect.width(), rect.height(), isActive() ? decoFactory_->rightButtomFrameBg_ : decoFactory_->rightButtomFrameBgInAct_, ( w > BOTTOMLEFTFRAMEWIDTH + BOTTOMRIGHTFRAMEWIDTH ) ? 0 : BOTTOMLEFTFRAMEWIDTH - ( w - BOTTOMRIGHTFRAMEWIDTH ), 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 ( !repaintTimer.isActive() && DeKoratorFactory::transparency_ )
        repaintTimer.start(0, true);
    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 ] )
    {
        maximize( button[ ButtonMax ] ->lastMousePress() );
    }
}

//////////////////////////////////////////////////////////////////////////////
// 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_->topLeftCornerMaskBitmap_ );
        mask -= TQRegion( m );


        // top mid
        if ( TOPMIDMASKWIDTH >= 1 )
        {
            int pos = TOPLEFTMASKWIDTH;
            int rep = ( w - TOPLEFTMASKWIDTH - TOPRIGHTMASKWIDTH ) / TOPMIDMASKWIDTH;
            m = TQRegion( decoFactory_->topMidMaskBitmap_ );
            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_->topRightCornerMaskBitmap_ );
        m.translate( width() - TOPRIGHTMASKWIDTH, 0 );
        mask -= TQRegion( m );


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

                // bottom mid
                if ( BOTTOMSHADEDMIDMASKWIDTH >= 1 )
                {
                    int pos = BOTTOMSHADEDLEFTMASKWIDTH;
                    int rep = ( w - BOTTOMSHADEDLEFTMASKWIDTH - BOTTOMSHADEDRIGHTMASKWIDTH ) / BOTTOMSHADEDMIDMASKWIDTH;
                    int hm = h - BOTTOMSHADEDMIDMASKHEIGHT;
                    m = TQRegion( decoFactory_->buttomShadedMidMaskBitmap_ );
                    TQRegion mBak = m;

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

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

                //buttom right
                m = TQRegion( decoFactory_->buttomShadedRightCornerMaskBitmap_ );
                m.translate( width() - BOTTOMSHADEDRIGHTMASKWIDTH, h - BOTTOMSHADEDRIGHTMASKHEIGHT );
                mask -= TQRegion( m );
            }
            else
            {
                //buttom left
                m = TQRegion( decoFactory_->buttomLeftCornerMaskBitmap_ );
                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_->buttomMidMaskBitmap_ );
                    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_->buttomRightCornerMaskBitmap_ );
                m.translate( width() - BOTTOMRIGHTMASKWIDTH, h - BOTTOMRIGHTMASKHEIGHT );
                mask -= TQRegion( m );
            } 
        }
        mask_ = mask;
    }

    setMask( mask_ );
}


#include "deKoratorclient.moc"
