#include "config.h"
#include <kglobal.h>
#include <qwhatsthis.h>
#include <klocale.h>

extern "C"
{
	QObject* allocate_config( KConfig* conf, QWidget* parent )
	{
		return(new CdeConfig(conf, parent));
	}
}


/* NOTE: 
 * 'conf' 	is a pointer to the kwindecoration modules open kwin config,
 *			and is by default set to the "Style" group.
 *
 * 'parent'	is the parent of the QObject, which is a VBox inside the
 *			Configure tab in kwindecoration
 */

CdeConfig::CdeConfig( KConfig* conf, QWidget* parent )
	: QObject( parent )
{
	cdeConfig = new KConfig("kwincderc");
	KGlobal::locale()->insertCatalogue("kwin_cde_config");
	
	groupBox = new QGroupBox( 1, Qt::Horizontal, i18n("Decoration Settings"), parent );
	
	gbSlider = new QGroupBox( 1, Qt::Horizontal, i18n("Frame &Width"), groupBox );
	widthSlider = new QSlider( 0, 10, 1, 5, QSlider::Horizontal, gbSlider ); 
	widthSlider->setTickmarks( QSlider::Below );
	QWhatsThis::add( widthSlider, i18n("By moving this slider back and forth, you can adjust the width of the "
					    "window frame to a value of your liking.\n"
					    "Note: If you move the slider to it's leftmost position, the window frames "
					    "will be turned off, which means that you won't be able to resize windows "
					    "by clicking and dragging their borders.") );
	QHBox *hbox = new QHBox( gbSlider );
	hbox->setSpacing( 6 );
	QLabel *label1 = new QLabel( i18n("Thin"), hbox );
	QLabel *label2 = new QLabel( i18n("Medium"), hbox );
	label2->setAlignment( AlignCenter );
	QLabel *label3 = new QLabel( i18n("Wide"), hbox );
	label3->setAlignment( AlignRight );
	
	bgAlign = new QButtonGroup( 3, Qt::Horizontal, i18n("Text &Alignment"), groupBox );
	bgAlign->setExclusive( true );
	QWhatsThis::add( bgAlign, i18n("Use these buttons to set the alignment of the titlebar caption text.") );
	QRadioButton *radio1 = new QRadioButton( i18n("Left"), bgAlign, "AlignLeft" );
	QRadioButton *radio2 = new QRadioButton( i18n("Centered"), bgAlign, "AlignHCenter" );
	radio2->setChecked( true );
	QRadioButton *radio3 = new QRadioButton( i18n("Right"), bgAlign, "AlignRight" );
	
	cbColorBorder = new QCheckBox( i18n("Draw window frames using &titlebar colors"), groupBox );
	QWhatsThis::add( cbColorBorder, i18n("When selected, the window decoration borders "
					     "are drawn using the titlebar colors. Otherwise, they are "
					     "drawn using normal border colors instead.") );
	
	cbTitlebarButton = new QCheckBox( i18n("Titlebar acts like a &pushbutton when clicked"), groupBox );
	QWhatsThis::add( cbTitlebarButton, i18n("When selected, this option causes the window titlebar to behave "
						"as if it was a pushbutton when you click it to move the window.") );
	
	(void) new QLabel( i18n("Tip: If you just want the look of the original Motif(tm) Window Manager,\n"
				"click the \"Buttons\" tab on the left, and then remove the help\n"
				"and close buttons from the titlebar."), groupBox );
	
	// Load configuration options
	load( conf );

	// Ensure we track user changes properly
	connect( cbColorBorder, SIGNAL(clicked()), SLOT(slotSelectionChanged()) );
	connect( cbTitlebarButton, SIGNAL(clicked()), SLOT(slotSelectionChanged()) );
	connect( widthSlider, SIGNAL(valueChanged(int)), SLOT(slotSelectionChanged(int)) );
	connect( bgAlign, SIGNAL(clicked(int)), SLOT(slotSelectionChanged(int)) );

	// Make the widgets visible in kwindecoration
	groupBox->show();
}


CdeConfig::~CdeConfig()
{
	delete gbSlider;
	delete bgAlign;
	delete groupBox;
	delete cdeConfig;
}


void CdeConfig::slotSelectionChanged()
{
	emit changed();
}

void CdeConfig::slotSelectionChanged( int )
{
	emit changed();
}

// Loads the configurable options from the kwinrc config file
// It is passed the open config from kwindecoration to improve efficiency
void CdeConfig::load( KConfig* conf )
{
	cdeConfig->setGroup("General");

	uint frameWidth = cdeConfig->readUnsignedNumEntry( "FrameWidth", 5 );
	widthSlider->setValue( frameWidth );

	QString value = cdeConfig->readEntry( "TextAlignment", "AlignHCenter" );
	QRadioButton *button = (QRadioButton*)bgAlign->child( (const char *)value.latin1() );
	if ( button )
	    button->setChecked( true );

	bool coloredFrame = cdeConfig->readBoolEntry( "UseTitleBarBorderColors", true );
	cbColorBorder->setChecked( coloredFrame );

	bool titlebarButton = cdeConfig->readBoolEntry( "TitlebarButtonMode", true );
	cbTitlebarButton->setChecked( titlebarButton );
}


// Saves the configurable options to the kwinrc config file
void CdeConfig::save( KConfig* conf )
{
	cdeConfig->setGroup("General");
	cdeConfig->writeEntry( "FrameWidth", widthSlider->value() );

	QRadioButton *button = (QRadioButton*)bgAlign->selected();
	if ( button )
	    cdeConfig->writeEntry( "TextAlignment", QString(button->name()) );

	cdeConfig->writeEntry( "UseTitleBarBorderColors", cbColorBorder->isChecked() );
	cdeConfig->writeEntry( "TitlebarButtonMode", cbTitlebarButton->isChecked() );
	
	// Ensure others trying to read this config get updated
	cdeConfig->sync();
}


// Sets UI widget defaults which must correspond to style defaults
void CdeConfig::defaults()
{
	widthSlider->setValue( 5 );

	QRadioButton *button = (QRadioButton*)bgAlign->child( "AlignHCenter" );
	if ( button )
	    button->setChecked( true );

	cbColorBorder->setChecked( true );
	cbTitlebarButton->setChecked( true );
}

#include "config.moc"
// vim: ts=4
