#include <stdio.h>


#include <tqpixmap.h>

#include <kdebug.h>

#include "pbutton.h"

PObject *
PButton::createWidget(CreateArgs &ca)
{
  PButton *pb = new PButton(ca.parent);
  TQButton *qb;
  if(ca.parent != 0 && ca.parent->widget()->isWidgetType())
    qb = new TQButton((TQWidget *) ca.parent->widget());
  else
    qb = new TQButton();
  pb->setWidget(qb);
  pb->setWidgetId(ca.pwI);
  return pb;
}


PButton::PButton(PObject *parent)
  : PWidget(parent)
{
  //  kdDebug(5008) << "PLineEdit PLineEdit called" << endl;
  button = 0;
  setWidget(0);
}

PButton::~PButton()
{
  //  kdDebug(5008) << "PLineEdit: in destructor" << endl;
  /*
  delete widget();     // Delete the frame
  button=0;          // Set it to 0
  setWidget(button); // Now set all widget() calls to 0.
  */
}

void PButton::messageHandler(int fd, PukeMessage *pm)
{
  PukeMessage pmRet;
  switch(pm->iCommand){
    case PUKE_BUTTON_SET_TEXT:
    if(!checkWidget())
      return;

    widget()->setText(pm->cArg);    // set the text

    pmRet.iCommand = - pm->iCommand;// Create ack
    pmRet.iWinId = pm->iWinId;
    pmRet.cArg = (char*) widget()->text().ascii(); // It's const, but we don't mess with it anyways
    pmRet.iTextSize = strlen(pmRet.cArg);
    emit outputMessage(fd, &pmRet);
    break;
  case PUKE_BUTTON_SET_PIXMAP:
    if(!checkWidget())
      return;
    
    widget()->setPixmap(TQPixmap(pm->cArg));

    pmRet.iCommand = - pm->iCommand;
    pmRet.iWinId = pm->iWinId;
    pmRet.iArg = widget()->pixmap()->isNull();
    pmRet.cArg = 0;
    emit outputMessage(fd, &pmRet);
    break;
  case PUKE_BUTTON_SET_AUTORESIZE:
    if(!checkWidget())
      return;

    pmRet.iCommand = - pm->iCommand;
    pmRet.iWinId = - pm->iWinId;
    pmRet.cArg = 0;
    emit outputMessage(fd, &pmRet);
    break;
  default:
    PWidget::messageHandler(fd, pm);
  }
}

void PButton::setWidget(TQObject *_qb)
{
  if(_qb != 0 && !_qb->inherits("TQButton"))
  {
    errorInvalidSet(_qb);
    return;
  }

  button = (TQButton *) _qb;
  if(button != 0){
    connect(button, TQ_SIGNAL(pressed()),
	    this, TQ_SLOT(buttonPressed()));
    connect(button, TQ_SIGNAL(released()),
	    this, TQ_SLOT(buttonReleased()));
    connect(button, TQ_SIGNAL(clicked()),
	    this, TQ_SLOT(buttonClicked()));
    connect(button, TQ_SIGNAL(toggled(bool)),
	    this, TQ_SLOT(buttonToggled(bool)));
    
  }
  PWidget::setWidget(_qb);

}


TQButton *PButton::widget()
{
  return button;
}

void PButton::buttonMessage(int iCommand)
{
  PukeMessage pmRet;

  pmRet.iCommand = iCommand;
  pmRet.iArg = 0;
  pmRet.iWinId = widgetIden().iWinId;
  pmRet.cArg = 0;

  emit outputMessage(widgetIden().fd, &pmRet);
}

void PButton::buttonPressed()
{
  buttonMessage(PUKE_BUTTON_PRESSED_ACK);
}

void PButton::buttonReleased()
{
  buttonMessage(PUKE_BUTTON_RELEASED_ACK);
}

void PButton::buttonClicked()
{
  buttonMessage(PUKE_BUTTON_CLICKED_ACK);
}

void PButton::buttonToggled(bool)
{
  buttonMessage(PUKE_BUTTON_TOGGLED_ACK);
}

bool PButton::checkWidget()
{
  if(widget() == 0){
    kdDebug(5008) << "PButton: No Widget set" << endl;
    return false;
  }
  return true;
}

#include "pbutton.moc"

