/*
 * Kivio - Visual Modelling and Flowcharting
 * Copyright (C) 2000 theKompany.com
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "ttdeaction.h"
#include "ttdetoolbarbutton.h"
#include "tkcombobox.h"

#include <tqlabel.h>
#include <tqlayout.h>

#include <tdetoolbar.h>
#include <kiconloader.h>

#define SET_FOR_ALL_CONTAINER(WIDGET_TYPE,METHOD_NAME,VALUE)             \
  for( int id = 0; id < containerCount(); ++id ) {                       \
    TQWidget* w = container(id);                                          \
    if ( w->inherits("TDEToolBar") ) {                                     \
      TQWidget* r = static_cast<TDEToolBar*>(w)->getWidget(itemId(id));     \
      if (qstrcmp(r->name(),"KTToolBarLayout")==0)                       \
        r = (TQWidget*)r->child("widget");                                \
      if ( r && r->inherits(#WIDGET_TYPE) ) {                            \
        WIDGET_TYPE* b = static_cast<WIDGET_TYPE*>(r);                   \
        b->METHOD_NAME(VALUE);                                         \
      }                                                                  \
    }                                                                    \
  }

TTDEAction::TTDEAction(TQObject* parent, const char* name)
: TDEAction( "", 0, parent, name )
{
  m_imode = TK::IconOnly;
}

TTDEAction::~TTDEAction()
{
}

int TTDEAction::plug(TQWidget* widget, int index)
{
  if ( widget->inherits("TDEToolBar") ) {
    TDEToolBar* bar = static_cast<TDEToolBar*>(widget);
    int id_ = TDEAction::getToolButtonID();
    TDEInstance *instance;

    if ( parentCollection() )
      instance = parentCollection()->instance();
    else
      instance = TDEGlobal::instance();

    TTDEToolBarButton* b = new TTDEToolBarButton(icon(),plainText(),bar,name(),instance);
    // we don't need clicked() and buttonClicked(), do we?
    // connect(b,TQ_SIGNAL(clicked()),TQ_SLOT(slotActivated()));
    b->setIconMode(m_imode);
    initToolBarButton(b);

    bar->insertWidget( id_, 100, b, index );
    addContainer(bar,id_);
    connect( bar, TQ_SIGNAL(destroyed()), this, TQ_SLOT(slotDestroyed()) );

    return containerCount() - 1;
  }
  return TDEAction::plug(widget,index);
}

void TTDEAction::initToolBarButton(TTDEToolBarButton* button)
{
  connect(button,TQ_SIGNAL(buttonClicked()),TQ_SLOT(slotActivated()));
}

TK::IconMode TTDEAction::iconMode()
{
  return m_imode;
}

void TTDEAction::setIconMode(TK::IconMode mode)
{
  m_imode = mode;
  SET_FOR_ALL_CONTAINER(TTDEToolBarButton,setIconMode,mode)
}

void TTDEAction::setText(const TQString& text)
{
  TDEAction::setText(text);
  updateLayout();
}

void TTDEAction::setIcon(const TQString& icon)
{
  TDEAction::setIcon(icon);
  updateLayout();
}

void TTDEAction::updateLayout()
{
  int len = containerCount();
  for( int id = 0; id < len; ++id ) {
    TQWidget* w = container( id );
    if (w->inherits("TDEToolBar")) {
      TQWidget* r = static_cast<TDEToolBar*>(w)->getWidget(itemId(id));
      if (qstrcmp(r->name(),"KTToolBarLayout")==0) {
        updateLayout(r);
      }
    }
  }
}

TQWidget* TTDEAction::createLayout(TQWidget* parent, TQWidget* children)
{
  TQWidget* base = new TQWidget(parent,"KTToolBarLayout");
  TQLabel* textLabel = new TQLabel(base,"text");
  textLabel->setMinimumHeight(1);
  TQLabel* pixLabel = new TQLabel(base,"pixmap");
  children->reparent(base,TQPoint(0,0));
  children->setName("widget");
  TQHBoxLayout* layout = new TQHBoxLayout(base,0,3);
  layout->setResizeMode(TQLayout::Minimum);
  layout->addWidget(textLabel);
  layout->addWidget(pixLabel);
  layout->addWidget(children,1);

  updateLayout(base);
  return base;
}

void TTDEAction::updateLayout(TQWidget* base)
{
  TQLabel* textLabel = (TQLabel*)base->child("text");
  TQLabel* pixLabel = (TQLabel*)base->child("pixmap");
  TQWidget* w = (TQWidget*)base->child("widget");

  if (!textLabel || !pixLabel || !w)
    return;

  if (!text().isEmpty() && m_imode != TK::IconOnly ) {
    textLabel->setText(text());
    textLabel->show();
  } else
    textLabel->hide();

  TQPixmap pix;
  if (hasIcon())
    pix = iconSet(TDEIcon::Small).pixmap();

  if (!icon().isEmpty())
    pix = BarIcon(icon());

  if (!pix.isNull() && m_imode != TK::TextOnly) {
    pixLabel->setPixmap(pix);
    pixLabel->show();
  } else
    pixLabel->hide();

  base->setFixedWidth( w->sizeHint().width() +
                       (textLabel->isVisible() ? textLabel->sizeHint().width():0) +
                       (pixLabel->isVisible() ? pixLabel->sizeHint().width():0) );
}
/******************************************************************************/
TKBaseSelectAction::TKBaseSelectAction( TQObject* parent, const char* name )
: TTDEAction(parent,name)
{
  m_current = 0;
  m_editable = false;
}

TKBaseSelectAction::~TKBaseSelectAction()
{
}

int TKBaseSelectAction::plug(TQWidget* widget, int index)
{
  if ( widget->inherits("TDEToolBar") )
  {
    TDEToolBar* bar = static_cast<TDEToolBar*>( widget );
    int id_ = TDEAction::getToolButtonID();

    TKComboBox* cb = new TKComboBox(m_editable,bar);
    initComboBox(cb);
    cb->setMinimumWidth( cb->sizeHint().width() );
    TQWidget* base = createLayout(bar,cb);

    bar->insertWidget( id_, 100, base, index );
    addContainer( bar, id_ );

    connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );

    setCurrentItem(currentItem());

    return containerCount() - 1;
  }
  return -1;
}

int TKBaseSelectAction::currentItem()
{
  return m_current;
}

void TKBaseSelectAction::initComboBox(TKComboBox* cb)
{
  connect(cb,TQ_SIGNAL(activated(int)),TQ_SLOT(slotActivated(int)));
}

void TKBaseSelectAction::setEditable(bool editable)
{
  m_editable = editable;
  SET_FOR_ALL_CONTAINER(TKComboBox,setEditable,editable)
}

bool TKBaseSelectAction::isEditable()
{
  return m_editable;
}

void TKBaseSelectAction::setCurrentItem(int index)
{
  m_current = index;
  SET_FOR_ALL_CONTAINER(TKComboBox,setCurrentItem,index)
}

void TKBaseSelectAction::slotActivated(int id)
{
  if ( m_current == id )
    return;

  m_current = id;
  setCurrentItem(id);
  activate(id);
}

void TKBaseSelectAction::activate(int id)
{
  emit activated(id);
}
/******************************************************************************/
TTDESelectAction::TTDESelectAction( TQObject* parent, const char* name )
: TKBaseSelectAction(parent,name)
{
}

TTDESelectAction::~TTDESelectAction()
{
}

void TTDESelectAction::initComboBox(TKComboBox* cb)
{
  TKBaseSelectAction::initComboBox(cb);
  connect(cb,TQ_SIGNAL(activated(const TQString&)),TQ_SLOT(slotActivated(const TQString&)));
  cb->insertStringList(items());
}

void TTDESelectAction::slotActivated(const TQString& text)
{
  emit activated(text);
}

void TTDESelectAction::setItems(const TQStringList& lst )
{
  m_list = lst;
  m_current = -1;

  SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
  SET_FOR_ALL_CONTAINER(TKComboBox,insertStringList,lst)

  // Disable if empty and not editable
  setEnabled ( lst.count() > 0 || m_editable );
}

TQStringList TTDESelectAction::items() const
{
  return m_list;
}

void TTDESelectAction::clear()
{
  SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
}

void TTDESelectAction::setEditText(const TQString& text)
{
  SET_FOR_ALL_CONTAINER(TKComboBox,setEditText,text)
}

#undef SET_FOR_ALL_CONTAINER
#include "ttdeaction.moc"
