//
// C++ Implementation: popupmenu
//
// Description: 
//
//
// Author: Andras Mantia <amantia@kdewebdev.org>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "popupmenu.h"
#include "specials.h"

#include <tqcursor.h>

#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kommanderplugin.h>

#define INSERTMENUITEM 100
#define INSERTSEPARATOR 101
#define CHANGEMENUITEM 102
#define SETITEMENABLED 103
#define ITEMENABLED 104
#define SETITEMVISIBLE 105
#define SETITEMCHECKED 106
#define ITEMVISIBLE 107
#define ITEMCHECKED 108
#define INSERTSUBMENU 109
#define LAST_FUNCTION INSERTSUBMENU 

PopupMenu::PopupMenu(TQWidget *parent, const char *name)
 : TQLabel(parent, name), KommanderWidget(this)
{
  TQStringList states;
  states << "default";
  setStates(states);
  setDisplayStates(states);
  if (KommanderWidget::inEditor)
  {
    setPixmap(TDEGlobal::iconLoader()->loadIcon("contents", TDEIcon::NoGroup, TDEIcon::SizeMedium));
    setFrameStyle(TQFrame::Box | TQFrame::Plain);
    setLineWidth(1);
    setFixedSize(pixmap()->size());
  }
  else
    setHidden(true);

  m_menu = new TDEPopupMenu(this);
  connect(m_menu, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotMenuItemActivated(int)));

  KommanderPlugin::setDefaultGroup(Group::DCOP);
  KommanderPlugin::registerFunction(INSERTMENUITEM, "insertMenuItem(TQString widget, TQString text, TQString executeWidget, int index, TQString icon)",  i18n("Insert an item into the popup menu. The executeWidget's execute method will be run when this item is selected. Returns the id of the inserted item. Use -1 for index to insert to the end. The icon is optional."), 4, 5);
  KommanderPlugin::registerFunction(INSERTSEPARATOR, "insertSeparator(TQString widget, int index)",  i18n("Insert a separator item into the popup menu. Use -1 for index to insert to the end."), 2);
  KommanderPlugin::registerFunction(CHANGEMENUITEM, "changeMenuItem(TQString widget, int id, TQString text, TQString executeWidget, TQString icon)",  i18n("Change an item specified by id in the popup menu. The executeWidget's execute method will be run when this item is selected."), 4, 5);
  KommanderPlugin::registerFunction(SETITEMENABLED, "setItemEnabled(TQString widget, int id, bool enable)",  i18n("Enable the item specified by id in the popup menu."), 3);
  KommanderPlugin::registerFunction(ITEMENABLED, "itemEnabled(TQString widget, int id)",  i18n("Check if the item specified by id is enabled."), 2);
  KommanderPlugin::registerFunction(SETITEMVISIBLE, "setItemVisible(TQString widget, int id, bool enable)",  i18n("Make the item specified by id visible."), 3);
  KommanderPlugin::registerFunction(SETITEMCHECKED, "setItemChecked(TQString widget, int id, bool enable)",  i18n("Apply checked status for the item specified by id."), 3);
  KommanderPlugin::registerFunction(ITEMVISIBLE, "itemVisible(TQString widget, int id)",  i18n("Check if the item specified by id is visible."), 2);
  KommanderPlugin::registerFunction(ITEMCHECKED, "itemChecked(TQString widget, int id)",  i18n("Verify if the item specified by id is checked."), 2);
  KommanderPlugin::registerFunction(INSERTSUBMENU, "insertSubmenu(TQString widget, TQString text, TQString menuWidget, int index, TQString icon)",  i18n("Insert submenu widget into the popup menu. Use -1 for index to insert to the end. The icon is optional."), 4, 5);
}


PopupMenu::~PopupMenu()
{
  delete m_menu;
}

TQString PopupMenu::currentState() const
{
  return TQString("default");
}

bool PopupMenu::isKommanderWidget() const
{
  return true;
}

TQStringList PopupMenu::associatedText() const
{
  return KommanderWidget::associatedText();
}

void PopupMenu::setAssociatedText(const TQStringList& a_at)
{
  KommanderWidget::setAssociatedText(a_at);
}

void PopupMenu::setWidgetText(const TQString& a_text)
{
  KommanderWidget::setAssociatedText(a_text);
}

void PopupMenu::setPopulationText(const TQString& a_text)
{
  KommanderWidget::setPopulationText(a_text);
}

TQString PopupMenu::populationText() const
{
  return KommanderWidget::populationText();
}

void PopupMenu::popup(int x, int y)
{
  m_menu->exec(TQPoint(x, y));
}

void PopupMenu::slotMenuItemActivated(int id)
{
  TQString widget = m_associations[id];
  KommanderWidget::evalAssociatedText(TQString("#!kommander\n%1.execute(%2)").arg(widget).arg(id)); 
}

void PopupMenu::populate()
{
  setAssociatedText(KommanderWidget::evalAssociatedText( populationText()));
}

TQString PopupMenu::insertSubmenu(const TQString& title, const TQString &menuWidget, int index, const TQString& icon)
{
  KommanderWidget *w = widgetByName(menuWidget);
  if (dynamic_cast<PopupMenu*>(w))
  {
    if (icon.isEmpty())
     return TQString::number(m_menu->insertItem(title, dynamic_cast<PopupMenu*>(w)->menu(), index));
    else
     return TQString::number( m_menu->insertItem(TDEGlobal::iconLoader()->loadIcon(icon, TDEIcon::NoGroup, TDEIcon::SizeMedium), title, dynamic_cast<PopupMenu*>(w)->menu(), index));
  }
  return TQString();
}


bool PopupMenu::isFunctionSupported(int f)
{
  return f == DCOP::clear || f == DCOP::execute || f == DCOP::item || (f >= INSERTMENUITEM && f <= LAST_FUNCTION) || f == DCOP::count || f == DCOP::geometry;
}

TQString PopupMenu::handleDCOP(int function, const TQStringList& args)
{
  switch (function) {
    case DCOP::clear:
      m_menu->clear();
      m_associations.clear();
      break;
    case DCOP::execute:
      m_params = args;
      evalAssociatedText();
      break;
    case INSERTMENUITEM:
    {
      uint index = args[2].toInt();
      int id = -1;
      if (args[3].isEmpty())
        id = m_menu->insertItem(args[0], index);
      else
        id = m_menu->insertItem(TDEGlobal::iconLoader()->loadIcon(args[3], TDEIcon::NoGroup, TDEIcon::SizeMedium), args[0], index);
      m_associations[id] = args[1];
      return TQString::number(id);
      break;
    }
    case INSERTSUBMENU:
    {
      return insertSubmenu(args[0], args[1], args[2].toInt(), args[3]);
      break;
    }
    case CHANGEMENUITEM:
    {
      uint id = args[0].toInt();
      if (args[3].isEmpty())
        m_menu->changeItem(id, args[1]);
      else
        m_menu->changeItem(id, TDEGlobal::iconLoader()->loadIcon(args[3], TDEIcon::NoGroup, TDEIcon::SizeMedium), args[1]);
      m_associations[id] = args[2];
      break;
    }
    case INSERTSEPARATOR:
    {
      uint index = args[0].toInt();
      m_menu->insertSeparator(index);     
    }
    case SETITEMENABLED:
    {
      uint id = args[0].toInt();
      m_menu->setItemEnabled(id, args[1] == "true" || args[1] == "1" ? true : false);
      break;
    }
    case ITEMENABLED:
    {
      uint id = args[0].toInt();
      return m_menu->isItemEnabled(id) ? "1" : "0";
      break;
    }
    case SETITEMVISIBLE:
    {
      uint id = args[0].toInt();
      m_menu->setItemVisible(id, args[1] == "true" || args[1] == "1" ? true : false);
      break;
    }
    case SETITEMCHECKED:
    {
      uint id = args[0].toInt();
      m_menu->setItemChecked(id, args[1] == "true" || args[1] == "1" ? true : false);
      break;
    }
    case ITEMVISIBLE:
    {
      uint id = args[0].toInt();
      return m_menu->isItemVisible(id) ? "1" : "0";
      break;
    }
    case ITEMCHECKED:
    {
      uint id = args[0].toInt();
      return m_menu->isItemChecked(id) ? "1" : "0";
      break;
    }
    case DCOP::item:
    {
      uint index = args[0].toInt();
      return index < m_params.count() ? m_params[index] : TQString();
      break;
    }
    case DCOP::count:
      return TQString::number(m_menu->count());
      break;
    case DCOP::geometry:
    {
      TQString geo = TQString::number(this->x())+" "+TQString::number(this->y())+" "+TQString::number(this->width())+" "+TQString::number(this->height());
      return geo;
      break;
    }
    default:
      return KommanderWidget::handleDCOP(function, args);
  }
  return TQString();
}



#include "popupmenu.moc"
