/**********************************************************************/
/*   TimeMon (c)  1994  Helmut Maierhofer			      */
/*   KDE-ified M. Maierhofer 1998                                     */
/**********************************************************************/

/*
 * confdlg.h
 *
 * Definitions for the timemon configuration dialog.
 */

#include <config.h>
#include <stdio.h>
#include <tqgroupbox.h>
#include <tqlineedit.h>
#include <tqslider.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqvgroupbox.h>
#include <tqcheckbox.h>
#include <tqcombobox.h>

#include <kcolorbutton.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kurlrequester.h>

#include "confdlg.h"
#include "timemon.h"

// -- KConfDialog definition ---------------------------------------------

KConfDialog::KConfDialog(KTimeMon *t)
    : KDialogBase( Tabbed, i18n("Configuration" ),
          Ok|Cancel|Apply, Ok, t, 0, false ),
      timemon(t)
{
    TQFrame *page;
    TQBoxLayout *bl;
    TQGridLayout *gl;
    TQLabel *l;
    TQGroupBox *b;
    KColorButton *cb;
    unsigned i, j;

    setIcon( SmallIcon( "ktimemon" ) );

    // first tab: general
    page = addPage( i18n( "&General" ) );

    bl = new TQVBoxLayout(page, 0, spacingHint());

    b = new TQVGroupBox(i18n("Sample &Rate"), page);
    bl->addWidget(b);

    intervalEdit = new KIntNumInput(250, b);
    intervalEdit->setRange(20, 1000, 10);
    intervalEdit->setSuffix(i18n(" msec"));

    // scaling group box
    b = new TQVGroupBox(i18n("Scaling"), page);
    bl->addWidget(b);

    bl->addStretch();

    autoScaleBox = new TQCheckBox(i18n("&Automatic"), b);
    connect(autoScaleBox, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(toggle(bool)));

    pageScaleEdit = new KIntNumInput(intervalEdit, 1000, b);
    pageScaleEdit->setRange(10, 10000, 10);
    pageScaleEdit->setLabel(i18n("&Paging:"), AlignVCenter | AlignLeft);

    swapScaleEdit = new KIntNumInput(pageScaleEdit, 1000, b);
    swapScaleEdit->setRange(1, 10000, 5);
    swapScaleEdit->setLabel(i18n("&Swapping:"), AlignVCenter | AlignLeft);

    ctxScaleEdit  = new KIntNumInput(swapScaleEdit, 10000, b);
    ctxScaleEdit->setLabel(i18n("&Context switch:"), AlignVCenter | AlignLeft);
    ctxScaleEdit->setRange(1, 10000, 30);

    bl->addStretch(1);

    // second tab: colours
    page = addPage( i18n( "C&olors" ) );

    gl = new TQGridLayout(page, 12, 10, 0, spacingHint());

    gl->setColStretch(3, 1); gl->setColStretch(6, 1); // eat up horizontal space
    gl->setRowStretch(11, 1);	// eat up vertical space

    gl->addRowSpacing(0, 20); gl->addRowSpacing(4, 20); gl->addRowSpacing(8, 20);
    gl->addRowSpacing(2, 8); gl->addRowSpacing(6, 8); gl->addRowSpacing(10, 8);

    gl->addColSpacing(0, 10); gl->addColSpacing(9, 25);

    TQString cpuColourLabels[4];
    cpuColourLabels[0] = i18n("Kernel:");
    cpuColourLabels[1] = i18n("User:");
    cpuColourLabels[2] = i18n("Nice:");
    cpuColourLabels[3] = i18n("IOWait:");

    TQString memColourLabels[4];
    memColourLabels[0] = i18n("Kernel:");
    memColourLabels[1] = i18n("Used:");
    memColourLabels[2] = i18n("Buffers:");
    memColourLabels[3] = i18n("Cached:");

    KColorButton **cpuColourButtons[4] = { &kernelCB, &userCB, &niceCB, &iowaitCB };
    b = new TQGroupBox(i18n("CPU"), page);
    gl->addMultiCellWidget(b, 0, 2, 0, 13);
    
    for (j = 0; j < 4; j++) {
      l = new TQLabel(cpuColourLabels[j], page);
      gl->addWidget(l, 1, 3*j+1, AlignVCenter | AlignRight);

      cb = *cpuColourButtons[j] = new KColorButton(white, page);
      gl->addWidget(cb, 1, 3*j+2, AlignCenter);
      
      connect(cb, TQT_SIGNAL(changed(const TQColor &)),
              this, TQT_SLOT(updateSampleWidget(const TQColor &)));
    }

    KColorButton **memColourButtons[4] = { &mkernelCB, &usedCB, &buffersCB, &cachedCB };
    b = new TQGroupBox(i18n("Memory"), page);
    gl->addMultiCellWidget(b, 4, 6, 0, 13);

    for (j = 0; j < 4; j++) {
      l = new TQLabel(memColourLabels[j], page);
      gl->addWidget(l, 5, 3*j+1, AlignVCenter | AlignRight);

      cb = *memColourButtons[j] = new KColorButton(white, page);
      gl->addWidget(cb, 5, 3*j+2, AlignCenter);
      
      connect(cb, TQT_SIGNAL(changed(const TQColor &)),
              this, TQT_SLOT(updateSampleWidget(const TQColor &)));
    }

    b = new TQGroupBox(i18n("Swap"), page);
    gl->addMultiCellWidget(b, 8, 10, 0, 6);

    l = new TQLabel(i18n("Swap:"), page);
    gl->addWidget(l, 9, 1, AlignVCenter | AlignRight);

    cb = swapCB = new KColorButton(red, page);
    gl->addWidget(cb, 9, 2);

    connect(cb, TQT_SIGNAL(changed(const TQColor &)),
            this, TQT_SLOT(updateSampleWidget(const TQColor &)));

    l = new TQLabel(i18n("Backgd:"), page);
    gl->addWidget(l, 9, 4, AlignVCenter | AlignRight);

    cb = bgCB = new KColorButton(blue, page);
    gl->addWidget(cb, 9, 5);

  connect(cb, TQT_SIGNAL(changed(const TQColor &)),
	  this, TQT_SLOT(updateSampleWidget(const TQColor &)));

  //b = new TQGroupBox(i18n("Sample"), page);
  //gl->addMultiCellWidget(b, 8, 10, 7, 9);

  // third tab: interaction
  page = addPage( i18n( "&Interaction" ) );

  bl = new TQVBoxLayout(page, 0, spacingHint());

  b = new TQGroupBox(i18n("Mouse Events"), page);
  b->setColumnLayout( 0, Qt::Vertical );
  bl->addWidget(b);
  bl->addStretch();

  TQVBoxLayout *vbox = new TQVBoxLayout( b->layout() );

  gl = new TQGridLayout(b, MAX_MOUSE_ACTIONS + 1, 3, 0, 6 );

  vbox->addLayout( gl );

  for (i = 1; i < MAX_MOUSE_ACTIONS + 1; i++)
    gl->setRowStretch(i, 1);
  gl->setColStretch(2, 1);

  TQString buttonText[MAX_MOUSE_ACTIONS] = { i18n("Left button:"),
						i18n("Middle button:"),
						i18n("Right button:") };

  for (i = 0; i < (int) MAX_MOUSE_ACTIONS; i++) {

    l = new TQLabel(buttonText[i], b);
    gl->addWidget(l, i+1, 0);

    mouseC[i] = new KComboBox(false, b);
    mouseC[i]->insertItem(i18n("Is Ignored"), KTimeMon::NOTHING);
//  SWITCH doesn't DO anything. remove it from config dialog for now
//    mouseC[i]->insertItem(i18n("Switches Mode"), KTimeMon::SWITCH);
    mouseC[i]->insertItem(i18n("Pops Up Menu"), KTimeMon::MENU - 1);
    mouseC[i]->insertItem(i18n("Starts"), KTimeMon::COMMAND - 1);
    gl->addWidget(mouseC[i], i+1, 1);

    connect( mouseC[ i ], TQT_SIGNAL( activated( int ) ), this,
           TQT_SLOT( mouseCommandEnable() ) );

    mouseLE[i] = new KURLRequester(b);
    mouseLE[i]->lineEdit()->setText(t->mouseActionCommand[i]);
    gl->addWidget(mouseLE[i], i+1, 2);
  }

  gl->activate();

  resize(380, 300);

  connect(this, TQT_SIGNAL(applyClicked()), timemon, TQT_SLOT(apply()));
  connect(this, TQT_SIGNAL(okClicked()), timemon, TQT_SLOT(apply()));
}

// Adjust the colours of the sample widget in the configuration dialog.
void KConfDialog::updateSampleWidget(const TQColor &)
{
#if 0
  sample->kernelColour = kernelCB->color();
  sample->userColour = userCB->color();
  sample->niceColour = niceCB->color();
  sample->iowaitColour = iowaitCB->color();
  sample->kernelColour = kernelCB->color();
  sample->cachedColour = cachedCB->color();
  sample->usedColour = usedCB->color();
  sample->buffersColour = buffersCB->color();
  sample->swapColour = swapCB->color();
  sample->bgColour = bgCB->color();
  sample->update();
#endif
}

// -----------------------------------------------------------------------------

// enable/disable the scale widgets
void KConfDialog::toggle(bool state)
{
    swapScaleEdit->setEnabled(!state);
    pageScaleEdit->setEnabled(!state);
    ctxScaleEdit->setEnabled(!state);
}

void KConfDialog::mouseCommandEnable()
{
  for ( int i = 0; i < MAX_MOUSE_ACTIONS; i++ ) {
    unsigned action = mouseC[ i ]->currentItem();

    // the - 1 is for compat with the no longer shown Switch option
    mouseLE[ i ]->setEnabled( action == KTimeMon::COMMAND - 1);
  }
}

// update the dialog fields
void KConfDialog::update()
{
  intervalEdit->setValue(timemon->interval);
  kernelCB->setColor(timemon->kernelColour);
  userCB->setColor(timemon->userColour);
  niceCB->setColor(timemon->niceColour);
  iowaitCB->setColor(timemon->iowaitColour);
  buffersCB->setColor(timemon->buffersColour);
  mkernelCB->setColor(timemon->mkernelColour);
  usedCB->setColor(timemon->usedColour);
  cachedCB->setColor(timemon->cachedColour);
  swapCB->setColor(timemon->swapColour);
  bgCB->setColor(timemon->bgColour);
  pageScaleEdit->setValue(timemon->pageScale);
  swapScaleEdit->setValue(timemon->swapScale);
  ctxScaleEdit->setValue(timemon->ctxScale);
  autoScaleBox->setChecked(timemon->autoScale);

  for ( int i = 0; i < MAX_MOUSE_ACTIONS; i++ )
  {
    int action = timemon->mouseAction[i];
    if (action > 0)
        --action; // compat for the no longer shown Switch action
    mouseC[i]->setCurrentItem(action);
  }
  mouseCommandEnable();

  updateSampleWidget(white);	// fake colour
}

unsigned int KConfDialog::getMouseAction(int i) const
{
    int action = mouseC[i]->currentItem();

    if (action > 0)
        ++action; // compat for the no longer shown Switch action

    return action;
}

#include "confdlg.moc"
