summaryrefslogtreecommitdiffstats
path: root/src/modules/objects/class_toolbutton.cpp
blob: 57107068ffe76cc20f644a39cc0e313095feb77d (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//mdm:
//   File : class_toolbutton.cpp
//   Creation date : Wed Feb 23 16:39:48 CEST 2005
//   by Tonino Imbesi(Grifisx) and Alessandro Carbone(Noldor)
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2005 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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 "class_toolbutton.h"
#include "kvi_error.h"
#include "kvi_debug.h"

#include "kvi_locale.h"
#include "kvi_iconmanager.h"
#include "kvi_tal_popupmenu.h"
/*
	@doc:	toolbutton
	@keyterms:
		toolbutton object class,
	@title:
		toolbutton class
	@type:
		class
	@short:
		Provides a toolbutton for toolbar widget.
	@inherits:
		[class]object[/class]
		[class]widget[/class]
	@description:
		The toolbutton class provides a quick-access button to commands or options, usually used inside a ToolBar.
	@functions:
		!fn:$setImage(<icon_id:string>)
		Sets the image for this button.
		!fn:$setUsesBigPixmap(<bEnabled:boolean>)
		Sets whether this toolbutton uses big pixmaps to enable.
		!fn:$usesBigPixmap(<bEnabled:boolean>)
		This property holds whether this toolbutton uses big pixmaps.
		!fn:setUsesTextLabel(<bEnabled:boolean>)
		Sets whether the toolbutton show a texlabel below the pixmap of the button.
		!fn:$usesTextLabel()
		Return 1 if the setUsesTextLabel is enabled, otherwise return 0.
		!fn:$setTextLabel(<text:String>,[tooltip:string])
		Sets the label of this button to <text> and automatically sets it as a tool tip if <tip> is TRUE.
		!fn:<string> $textLabel()
		Returns the label of tthe button.
		!fn:$setAutoRaise(<bAutoraise:boolean>)
		Sets whether auto-raising is enabled.
		!fn:<boolean> $autoRaise()
		Returns 1 if autoRaise is enabled, otherwise returns 0.
		!fn:$setToggleButton(<bToggle:boolean>)
		Sets whether the tool button is a toggle button 1 enable, 0 disable.
		!fn:<boolean> $toggle()
		Toggles the state of the tool button.
		!fn:$setOn(<bEnabled:boolean>)
		Sets whether the tool button is on to the bool value: 1 enable, 0 disable.
		// findme
		!fn:$setPopup (<toolbutton:object>)
		!fn:$setPopupDelay (<int delay>)
		Sets the time delay between pressing the button and the appearance of the associated popup menu in milliseconds to delay.[br]
		A good value is 100.
		!fn:<integer> $pupupDelay()
		Returns the time delay between pressing the button and the appearance of the associated popup menu in milliseconds.
		!fn:$openPopup()
		Opens the associated popup menu. If there is no such menu, this function does nothing.
		!fn:setTextPosition(<text_position:string>)
		Sets the position of the tool button's textLabel in relation to the tool button's icon.[br]
		Valid texpos values are:[br]
		- BesideIcon : The text appears beside the icon.[br]
		- BelowIcon  : The text appears below the icon.
		!fn:<string> $textPosition()
		Returns the position of the text label of this button.
		!fn: $clickEvent()
		This function is called when the toolbutton is clicked.[br]
		You can reimplement it to handle the user click events.[br]
		The default implementation emits the [classfnc]$clicked[/classfnc]() signal.
	@signals:
		!sg: $clicked()
		This signal is emitted by the default implementation of [classfnc]$clickEvent[/classfnc]().[br]
*/

KVSO_BEGIN_REGISTERCLASS(KviKvsObject_toolbutton,"toolbutton","widget")
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setImage", functionsetImage)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setUsesBigPixmap", functionsetUsesBigPixmap)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"usesBigPixmap", functionusesBigPixmap)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setUsesTextLabel", functionsetUsesTextLabel)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"usesTextLabel", functionusesTextLabel)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setAutoRaise", function_setAutoRaise)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"autoRaise", function_autoRaise)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setTextLabel", functionsetTextLabel)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"textLabel", functiontextLabel)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setPopup", functionsetPopup)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"openPopup", functionopenPopup)

	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setToggleButton", functionsetToggleButton)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"toggle", functiontoggle)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setOn", functionsetOn)

	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setPopupDelay", functionsetPopupDelay)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"popupDelay", functionpopupDelay)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"setTextPosition", functionsetTextPosition)
	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"textPosition", functiontextPosition)

	KVSO_REGISTER_HANDLER(KviKvsObject_toolbutton,"clickEvent",function_clickEvent)
KVSO_END_REGISTERCLASS(KviKvsObject_toolbutton)

KVSO_BEGIN_CONSTRUCTOR(KviKvsObject_toolbutton,KviKvsObject_widget)

KVSO_END_CONSTRUCTOR(KviKvsObject_toolbutton)


KVSO_BEGIN_DESTRUCTOR(KviKvsObject_toolbutton)

KVSO_END_CONSTRUCTOR(KviKvsObject_toolbutton)

bool KviKvsObject_toolbutton::init(KviKvsRunTimeContext * pContext,KviKvsVariantList *pParams)
{
	setObject(TQT_TQOBJECT(new TQToolButton(parentScriptWidget(), name())), true);
	connect(widget(),TQT_SIGNAL(clicked()),this,TQT_SLOT(slotClicked()));
	return true;
}

bool KviKvsObject_toolbutton::functionsetImage(KviKvsObjectFunctionCall *c)
{
	TQString icon;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("icon_id",KVS_PT_STRING,0,icon)
	KVSO_PARAMETERS_END(c)
	if (!widget()) return true;
	TQPixmap * pix = g_pIconManager->getImage(icon);
	if(pix){
		#ifdef COMPILE_USE_QT4
			((TQToolButton *)widget())->setIconSet(TQIconSet(*pix));
		#else
			((TQToolButton *)widget())->setIconSet(TQIconSet(*pix,TQIconSet::Small));
		#endif
	}
	else
		((TQToolButton *)widget())->setIconSet(TQIconSet());
	return true;
}
bool KviKvsObject_toolbutton::functionsetUsesBigPixmap(KviKvsObjectFunctionCall *c)
{
	bool bEnabled;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOL,0,bEnabled)
	KVSO_PARAMETERS_END(c)
	if(widget())
		((TQToolButton *)widget())->setUsesBigPixmap(bEnabled);
	return true;
}
bool KviKvsObject_toolbutton::functionusesBigPixmap(KviKvsObjectFunctionCall *c)
{
	if (widget())
		c->returnValue()->setBoolean(((TQToolButton *)widget())->usesBigPixmap());
	return true;
}
bool KviKvsObject_toolbutton::functionsetUsesTextLabel(KviKvsObjectFunctionCall *c)
{
	bool bEnabled;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOL,0,bEnabled)
	KVSO_PARAMETERS_END(c)
	if(widget())
		((TQToolButton *)widget())->setUsesTextLabel(bEnabled);
	return true;
}
bool KviKvsObject_toolbutton::functionusesTextLabel(KviKvsObjectFunctionCall *c)
{
	if (widget())
		c->returnValue()->setBoolean(((TQToolButton *)widget())->usesTextLabel());
	return true;
}
bool KviKvsObject_toolbutton::function_setAutoRaise(KviKvsObjectFunctionCall *c)
{
	bool bEnabled;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOL,0,bEnabled)
	KVSO_PARAMETERS_END(c)
	if(widget())
		((TQToolButton *)widget())->setAutoRaise(bEnabled);
	return true;
}
bool KviKvsObject_toolbutton::function_autoRaise(KviKvsObjectFunctionCall *c)
{
	if(widget())
		c->returnValue()->setBoolean(((TQToolButton *)widget())->autoRaise());
	return true;
}
bool KviKvsObject_toolbutton::functionsetOn(KviKvsObjectFunctionCall *c)
{
	bool bEnabled;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOL,0,bEnabled)
	KVSO_PARAMETERS_END(c)
	if(widget())
		((TQToolButton *)widget())->setOn(bEnabled);
	return true;
}

bool KviKvsObject_toolbutton::functionsetToggleButton(KviKvsObjectFunctionCall *c)
{
	bool bEnabled;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOL,0,bEnabled)
	KVSO_PARAMETERS_END(c)
	if(widget())
		((TQToolButton *)widget())->setToggleButton(bEnabled);
	return true;
}
bool KviKvsObject_toolbutton::functiontoggle(KviKvsObjectFunctionCall *c)
{
	if (widget())
		((TQToolButton *)widget())->toggle();
	return true;
}

bool KviKvsObject_toolbutton::functionsetTextLabel(KviKvsObjectFunctionCall *c)
{
	TQString szLabel,szTip;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("label",KVS_PT_STRING,0,szLabel)
		KVSO_PARAMETER("tooltip",KVS_PT_STRING,KVS_PF_OPTIONAL,szTip)
	KVSO_PARAMETERS_END(c)
	if(!widget()) return true;

	#ifdef COMPILE_USE_QT4
		((TQToolButton *)widget())->setText(szLabel);
		if (!szTip.isEmpty()) ((TQToolButton *)widget())->setToolTip(szTip);
	#else
		if (szTip.isEmpty()) ((TQToolButton *)widget())->setTextLabel(szLabel);
			else ((TQToolButton *)widget())->setTextLabel(szLabel,szTip);
	#endif
	return true;
}
bool KviKvsObject_toolbutton::functiontextLabel(KviKvsObjectFunctionCall *c)
{
	if (widget())
		c->returnValue()->setString(((TQToolButton *)widget())->textLabel());
	return true;
}
bool KviKvsObject_toolbutton::functionsetPopup(KviKvsObjectFunctionCall *c)
{
	KviKvsObject *ob;
	kvs_hobject_t hObject;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("widget",KVS_PT_HOBJECT,0,hObject)
	KVSO_PARAMETERS_END(c)
	ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject);
	if (!ob)
	{
		c->warning(__tr2qs("Widget parameter is not an object"));
		return true;
	}
	if (!ob->object())
	{
		c->warning(__tr2qs("Widget parameter is not a valid object"));
		return true;
	}
	if(!ob->object()->inherits("KviKvsObject_popupmenu"))
    {
		c->warning(__tr2qs("Can't add a non - popupmenu  object"));
        return TRUE;
    }
	if(widget())
		((TQToolButton *)widget())->setPopup(((KviTalPopupMenu  *)(ob->object())));
	return true;
}
bool KviKvsObject_toolbutton::functionopenPopup(KviKvsObjectFunctionCall *c)
{
	if(widget())
		((TQToolButton *)widget())->openPopup();
	return true;
}
bool KviKvsObject_toolbutton::functionsetPopupDelay(KviKvsObjectFunctionCall *c)
{
	kvs_int_t uDelay;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("delay",KVS_PT_UNSIGNEDINTEGER,0,uDelay)
	KVSO_PARAMETERS_END(c)
	if (widget())
		  ((TQToolButton *)widget())->setPopupDelay(uDelay);
	return true;
}
bool KviKvsObject_toolbutton::functionpopupDelay(KviKvsObjectFunctionCall *c)
{
	if (widget())
		c->returnValue()->setInteger(((TQToolButton *)widget())->popupDelay());
	return true;
}
bool KviKvsObject_toolbutton::functionsetTextPosition(KviKvsObjectFunctionCall *c)
{
	TQString szPos;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("position",KVS_PT_STRING,0,szPos)
	KVSO_PARAMETERS_END(c)
	if(!widget())return true;
	if(KviTQString::equalCI(szPos,"BesideIcon"))
			((TQToolButton *)widget())->setTextPosition(TQToolButton::BesideIcon);
	else if(KviTQString::equalCI(szPos,"BelowIcon"))
			((TQToolButton *)widget())->setTextPosition(TQToolButton::BelowIcon);
	else c->warning(__tr2qs("Unknown text position '%Q'"),&szPos);
	return true;
}
bool KviKvsObject_toolbutton::functiontextPosition(KviKvsObjectFunctionCall *c)
{
	if(!widget()) return true;
	TQString szPos="BelowIcon";
	if ((((TQToolButton *)widget())->textPosition())==(TQToolButton::BesideIcon))
		szPos="BesideIcon";
	c->returnValue()->setString(szPos);
	return true;
}
bool KviKvsObject_toolbutton::function_clickEvent(KviKvsObjectFunctionCall *c)
{
	emitSignal("clicked",c);
	return true;
}

void KviKvsObject_toolbutton::slotClicked()
{
	KviKvsVariantList *params=0;
	callFunction(this,"clickEvent",params);
}

#include "m_class_toolbutton.moc"