summaryrefslogtreecommitdiffstats
path: root/kommander/widgets/popupmenu.cpp
blob: e00ee4851c275deab19bf0f233d4c3705a933763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// C++ Implementation: popupmenu
//
// Description: 
//
//
// Author: Andras Mantia <amantia@kdewebdev.org>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "popupmenu.h"
#include "specials.h"

#include <tqcursor.h>

#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kpopupmenu.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 *tqparent, const char *name)
 : TQLabel(tqparent, name), KommanderWidget(TQT_TQOBJECT(this))
{
  TQStringList states;
  states << "default";
  setStates(states);
  setDisplayStates(states);
  if (KommanderWidget::inEditor)
  {
    setPixmap(KGlobal::iconLoader()->loadIcon("contents", KIcon::NoGroup, KIcon::SizeMedium));
    setFrameStyle(TQFrame::Box | TQFrame::Plain);
    setLineWidth(1);
    setFixedSize(pixmap()->size());
  }
  else
    setHidden(true);

  m_menu = new KPopupMenu(this);
  connect(m_menu, TQT_SIGNAL(activated(int)), this, TQT_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)").tqarg(widget).tqarg(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(KGlobal::iconLoader()->loadIcon(icon, KIcon::NoGroup, KIcon::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::tqgeometry;
}

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(KGlobal::iconLoader()->loadIcon(args[3], KIcon::NoGroup, KIcon::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, KGlobal::iconLoader()->loadIcon(args[3], KIcon::NoGroup, KIcon::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::tqgeometry:
    {
      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"