summaryrefslogtreecommitdiffstats
path: root/src/modules/objects/class_label.cpp
blob: 1a23aa4cbd4daf0e5628e62c093578e065838d4c (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//vim: ts=8
//   File : class_label.cpp
//   Creation date : Mon Sep 18 14:21:48 CEST 2000 by Krzysztof Godlewski
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Krzysztof Godlewski
//   Copyright (C) 1999-2000 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 <tqfont.h>
#include <tqpixmap.h>
#include <tqmessagebox.h>
#include "class_label.h"
//#include "kvi_fileutils.h"
#include "kvi_error.h"
#include "kvi_debug.h"

#include "kvi_locale.h"
#include "kvi_iconmanager.h"



// Tables used in $tqsetAlignment & $tqalignment
const char * const align_tbl[] = {
			"Left", 
			"Right",
			"HCenter",
			"VCenter",
			"Center",
			"Top",
			"Bottom",
			"WordBreak"
			   };
#ifdef COMPILE_USE_QT4
	#define TQT_LABEL_ALIGNLEFT TQt::AlignLeft
	#define	TQT_LABEL_ALIGNRIGHT TQt::AlignRight
	#define TQT_LABEL_ALIGNHCENTER TQt::AlignHCenter
	#define TQT_LABEL_ALIGNVCENTER TQt::AlignVCenter
	#define TQT_LABEL_ALIGNCENTER TQt::AlignCenter
	#define	TQT_LABEL_ALIGNTOP TQt::AlignTop
	#define TQT_LABEL_ALIGNBOTTOM TQt::AlignBottom
	#define TQT_LABEL_JUSTIFY TQt::AlignJustify
#else
	#define TQT_LABEL_ALIGNLEFT TQLabel::AlignLeft
	#define	TQT_LABEL_ALIGNRIGHT TQLabel::AlignRight
	#define TQT_LABEL_ALIGNHCENTER TQLabel::AlignHCenter
	#define TQT_LABEL_ALIGNVCENTER TQLabel::AlignVCenter
	#define TQT_LABEL_ALIGNCENTER TQLabel::AlignCenter
	#define	TQT_LABEL_ALIGNTOP TQLabel::AlignTop
	#define TQT_LABEL_ALIGNBOTTOM TQLabel::AlignBottom
	#define TQT_LABEL_JUSTIFY TQLabel::WordBreak
#endif

const int align_cod[] = {
		TQT_LABEL_ALIGNLEFT,
		TQT_LABEL_ALIGNRIGHT,
	    TQT_LABEL_ALIGNHCENTER,
	    TQT_LABEL_ALIGNVCENTER,
	    TQT_LABEL_ALIGNCENTER,
	 	TQT_LABEL_ALIGNTOP,
	    TQT_LABEL_ALIGNBOTTOM,
	    TQT_LABEL_JUSTIFY,
	};

#define align_num	(sizeof(align_tbl) / sizeof(align_tbl[0]))
		  
// used in $frameStyle & $setFrameStyle
const char * const frame_tbl[] = {
				"NoFrame",
				"Box",
				"Panel",
				"WinPanel",
				"Hline",
			// shadow styles
				"Plain",
				"Raised",
				"Sunken"
			   };

const int frame_cod[] = {
				TQFrame::NoFrame,
				TQFrame::Box,
				TQFrame::Panel,
				TQFrame::WinPanel,
				TQFrame::HLine,
				TQFrame::Plain,
				TQFrame::Raised,
				TQFrame::Sunken
			 };

#define frame_num	(sizeof(frame_tbl) / sizeof(frame_tbl[0]))


/*
	@doc:	label
	@keyterms:
		label object class, show image
	@title:
		label class
	@type:
		class
	@short:
		Displays text or an image
	@inherits:
		[class]object[/class]
		[class]widget[/class]
	@description:
		This widget can be used to display a text or an image. It can
		have different frame styles and text/image tqalignment.
	@functions:
		!fn: $setText(<text:string>)
		Sets the text to be displayed by the label.
		The text can contain limited html tags.
		See also [classfnc]$text[/classfnc]().
		!fn: <string> $text()
		Returns the text currently displayed by the label. 
		See also [classfnc]$setText[/classfnc]().
		!fn: <integer> $margin()
		Returns current value of margin for this label (default is 0).
		See also [classfnc]$setMargin[/classfnc]().
		!fn: $setMargin(<margin:uint>)
		Sets margin width / height to <margin>.
		See also [classfnc]$margin[/classfnc]().
		!fn: <boolean> $autoResize()
		Returns 1, if auto-resize ability of the label is enabled. 
		See also [classfnc]$setAutoResize[/classfnc]().
		!fn: $setAutoResize(<benabled:boolean>)
		Sets auto-resize ability to enabled (if <benabled> is 1) or disabled
		 (if <benabled> is 0). If auto-resize is enabled, the label will 
		automagically resize itself accordingly to the 'size' of the
		text it contains. 
		See also [classfnc]$autoResize[/classfnc]().
		!fn: <string> $tqalignment()
		Returns a string containing tqalignment flags that are set for 
		this label. The flags are separated by commas. An example output
		could look like this:[br]
		[pre]Bottom, Right[/pre][br]
		See [classfnc]$tqsetAlignment[/classfnc]() for explanation of all
		tqalignment flags.
		!fn: $tqsetAlignment(<flag1:string>, <flag2:string>, ...)
		This function sets tqalignment flags, given as parameters, for 
		this label. Valid flags are:
		[pre]
		Right     - Text is aligned to right border[br]
		Left      - Text is aligned to left border[br]
		Top       - Text is aligned to the top border[br]
		Bottom    - Text is aligned to the bottom border[br]
		HCenter   - Text is horizontally centered[br]
		VCenter   - Text is vertically centered[br]
		Center    - Equals HCenter + VCenter[br]
		WordBreak - Enables automatic word breaking[br]
		[/pre]
		It is obvious that you can not set for example [i]Right[/i] 
		and [i]Left[/i] simoultaneously - this will [b]NOT[/b] 
		result in an error message - this will simply not work :)
		!fn: $clear()
		Clears the label. Equal to calling 
		[classfnc]$setText[/classfnc]("")
		!fn: $frameStyle()
		Returns a string containing this label's frame-style flags,
		separated with commas. Output from this function could look like
		this:
		[pre]Panel, Raised[/pre]
		See [classfnc]$setFrameStyle[/classfnc]() for a list of all
		frame-style flags and their explenation.
		!fn: $setFrameStyle(<flag1>, <flag2>, ...)
		Sets the frame-style flags to the ones passed as arguments.
		The flags can either decide of the tqshape or shadow of the
		label's frame. Valid tqshape flags are:[br]
		[pre]
		NoFrame     - draw no frame. You shouldn't specify a shadow when
		using this.[br]
		Box         - draws a rectangular box. Its borders can be 
		[i]Raised[/i] or [i]Sunken[/i][br]
		Panel       - draws a rectangular panel which can be 
		[i]Raised[/i] or [i]Sunken[/i][br]
		WinPanel    - similar to [i]Panel[/i], but is more in Win95
		style[br]
		Hline        - draws a horizontal line that frames nothing (useful as separator) 
		[/pre]
		Valid shadow flags are:[br]
		[pre]
		Plain       - no 3D effect (draws using foreground color)[br]
		Raised      - makes the label look like it was raised above the
		tqparent widget[br]
		Sunken      - makes the label look like it was "pushed" inside
		the tqparent widget[br]
		[/pre]
		!fn: $setImage(<image_id>)
		Sets the image to be displayed on this label. 
		Giving empty argument clears the pixmap[br]
		See the [doc:image_id]image identifier[/doc] documentation for
		the explaination of the <image_id> parameter.

*/


KVSO_BEGIN_REGISTERCLASS(KviKvsObject_label,"label","widget")
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"setText",functionSetText)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"text", functionText)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"margin", functionMargin)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"setMargin", functionSetMargin)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"autoResize", functionAutoResize)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"setAutoResize", functionSetAutoResize)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"tqalignment", functionAlignment)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"tqsetAlignment", functionSetAlignment)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"clear", functionClear)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"frameStyle", functionFrameStyle)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"setFrameStyle", functionSetFrameStyle)
	KVSO_REGISTER_HANDLER(KviKvsObject_label,"setImage", functionSetImage)
KVSO_END_REGISTERCLASS(KviKvsObject_label)

KVSO_BEGIN_CONSTRUCTOR(KviKvsObject_label,KviKvsObject_widget)

KVSO_END_CONSTRUCTOR(KviKvsObject_label)


KVSO_BEGIN_DESTRUCTOR(KviKvsObject_label)

KVSO_END_CONSTRUCTOR(KviKvsObject_label)

bool KviKvsObject_label::init(KviKvsRunTimeContext * pContext,KviKvsVariantList *pParams)
{
	setObject(TQT_TQOBJECT(new TQLabel(parentScriptWidget(), name())), true);
	return true;
}

bool KviKvsObject_label::functionSetText(KviKvsObjectFunctionCall *c)
{
	TQString szText;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("text",KVS_PT_STRING,0,szText)
	KVSO_PARAMETERS_END(c)
	if (widget())
		((TQLabel *)widget())->setText(szText);
	return true;
}
bool KviKvsObject_label::functionText(KviKvsObjectFunctionCall *c)
{
	if (widget()) c->returnValue()->setString(((TQLabel *)widget())->text());
	return true;
}

bool KviKvsObject_label::functionSetMargin(KviKvsObjectFunctionCall *c)
{
	kvs_uint_t iMargin;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("margin",KVS_PT_UNSIGNEDINTEGER,0,iMargin)
	KVSO_PARAMETERS_END(c)
    if (widget()) ((TQLabel *)widget())->setMargin(iMargin);
	return true;
}
bool KviKvsObject_label::functionMargin(KviKvsObjectFunctionCall *c)
{
	if (widget()) c->returnValue()->setInteger(((TQLabel *)widget())->margin());
	return true;
}

// FIX ME
bool KviKvsObject_label::functionSetAutoResize(KviKvsObjectFunctionCall *c)
{
	bool bEnabled;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOL,0,bEnabled)
	KVSO_PARAMETERS_END(c)
	#ifdef COMPILE_USE_QT4
	 return true;
	#else
	if(widget())
		((TQLabel *)widget())->setAutoResize(bEnabled);
	return true;
	#endif
}
bool KviKvsObject_label::functionAutoResize(KviKvsObjectFunctionCall *c)
{
	#ifdef COMPILE_USE_QT4
		if (widget()) c->returnValue()->setBoolean(true);
	#else
		if (widget()) c->returnValue()->setBoolean(((TQLabel *)widget())->autoResize());
	#endif
	return true;
}
//
bool KviKvsObject_label::functionSetAlignment(KviKvsObjectFunctionCall *c)
{
	TQStringList tqalignment;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("tqalignment",KVS_PT_STRINGLIST,KVS_PF_OPTIONAL,tqalignment)
	KVSO_PARAMETERS_END(c)
	if (!widget()) return true;
	int align,sum=0;
	for ( TQStringList::Iterator it = tqalignment.begin(); it != tqalignment.end(); ++it )
		{
		
			align = 0;
			for(unsigned int j = 0; j < align_num; j++)
			{
				if(KviTQString::equalCI((*it), align_tbl[j]))
				{
					align=align_cod[j];
					break;
				}
			}
			if(align)
				sum = sum | align;
			else
				c->warning(__tr2qs("Unknown tqalignment: '%Q'"),&(*it));
			
		}
	((TQLabel *)widget())->tqsetAlignment(sum);
	return true;
}
bool KviKvsObject_label::functionAlignment(KviKvsObjectFunctionCall *c)
{
	int mode = ((TQLabel *)widget())->tqalignment();
	TQString szAlignment="";
	for(unsigned int i = 0; i < align_num; i++)
	{
		if(mode == align_cod[i])
		{
			szAlignment=align_tbl[i];
			break;
		}
	}
	c->returnValue()->setString(szAlignment);
	return true;
}
bool KviKvsObject_label::functionClear(KviKvsObjectFunctionCall *c)
{
	if(widget())
		((TQLabel *)widget())->clear();
	return true;
}


bool KviKvsObject_label::functionSetFrameStyle(KviKvsObjectFunctionCall *c)
{

	TQStringList style;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("style",KVS_PT_STRINGLIST,KVS_PF_OPTIONAL,style)
	KVSO_PARAMETERS_END(c)
	if (!widget()) return true;
	int framestyle,sum=0;
	for ( TQStringList::Iterator it = style.begin(); it != style.end(); ++it )
		{
			framestyle = 0;
			for(unsigned int j = 0; j < align_num; j++)
			{
				if(KviTQString::equalCI((*it), frame_tbl[j]))
				{
					framestyle=frame_cod[j];
					break;
				}
			}
			if(framestyle)
				sum = sum | framestyle;
			else
				c->warning(__tr2qs("Unknown style: '%Q'"),&(*it));
			
		}
	((TQLabel *)widget())->setFrameStyle(sum);
	return true;




}
bool KviKvsObject_label::functionFrameStyle(KviKvsObjectFunctionCall *c)
{
	int mode = ((TQLabel *)widget())->frameStyle();
	TQString szStyle="";
	for(unsigned int i = 0; i < frame_num; i++)
	{
		if(mode == frame_cod[i])
		{
			szStyle=frame_tbl[i];
			break;
		}
	}
	c->returnValue()->setString(szStyle);
	return true;
}
bool KviKvsObject_label::functionSetImage(KviKvsObjectFunctionCall *c)
{
	
	TQString icon;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("icon",KVS_PT_STRING,0,icon)
	KVSO_PARAMETERS_END(c)
	if(!widget())return true;
	TQPixmap * pix = g_pIconManager->getImage(icon);
	if(pix) ((TQLabel *)widget())->setPixmap(*pix);
	return true;
}