summaryrefslogtreecommitdiffstats
path: root/src/modules/toolbar/libkvitoolbar.cpp
blob: 714de08fcde006a5c052b2358e49c2647b614669 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//=============================================================================//
//
//   File : libkvitoolbar.cpp
//   Creation date : Wed 6 Nov 22:51:59 2002 GMT by Szymon Stefanek
//
//   This toolbar is part of the KVirc irc client distribution
//   Copyright (C) 2002-2004 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 "kvi_module.h"

#include "kvi_locale.h"

#include "kvi_window.h"
#include "kvi_app.h"
#include "kvi_error.h"
#include "kvi_cmdformatter.h"
#include "kvi_customtoolbar.h"
#include "kvi_customtoolbardescriptor.h"
#include "kvi_customtoolbarmanager.h"
#include "kvi_actionmanager.h"
#include "kvi_kvs_useraction.h"

/*
	@doc: toolbar.create
	@type:
		command
	@title:
		toolbar.create
	@keyterms:
		Creating toolbars
	@short:
		Creates a toolbar
	@syntax:
		toolbar.create [-p] <id:string> <label:string> [icon_id:string]
	@switches:
		!sw: -p | --preserve
		Preserve the existing toolbar, if any
	@description:
		Creates a toolbar with the specified <id> and with the visible <label> (caption).[br]
		If a toolbar with the same <id> already exists then this command is equivalent to the
		sequence of [cmd]toolbar.hide[/cmd], [cmd]toolbar.clear[/cmd] and [cmd]toolbar.setlabel[/cmd] unless the -p
		switch is specified that causes the [cmd]toolbar.clear[/cmd] call to be skipped (contents preserved).[br]
		This command doesn't show the toolbar (you need to use [cmd]toolbar.show[/cmd]).[br]
		Toolbars are saved and preserved across KVIrc sessions thus you don't need to create them
		on application startup: you only create them once.
		If [icon_id] is specified then it is interpreted as an [doc:image_id]image identifier[/doc]
		for the small (16x16) icon that will be put in the toolbar listing popup menu.
		The toolbar will exist until [cmd]toolbar.destroy[/cmd] is called (you might want to add
		an "uninstall" feature to your script).[br]
	@seealso:
		[fnc]$toolbar.exists[/fnc],
		[cmd]toolbar.remove[/cmd],
		[cmd]toolbar.show[/cmd],
		[cmd]toolbar.destroy[/cmd]
*/

static bool toolbar_kvs_cmd_create(KviKvsModuleCommandCall * c)
{
	TQString szId,szLabel,szIconId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
		KVSM_PARAMETER("label",KVS_PT_STRING,KVS_PF_OPTIONAL,szLabel)
		KVSM_PARAMETER("icon_id",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szIconId)
	KVSM_PARAMETERS_END(c)

/*	if(!c->getParameterCode(1,szLabel))
	{
		c->error(__tr2qs("Internal error: call a head-shrinker"));
		return false;
	}
*/	
	if(szLabel.isEmpty())szLabel = "$tr(Unnamed)";

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);
	if(d)
	{
		if(!c->switches()->tqfind('p',"preserve"))d->clear();
		d->rename(szLabel);
	} else {
		d = KviCustomToolBarManager::instance()->create(szId,szLabel);
	}
	
	if(!szIconId.isEmpty())
		d->setIconId(szIconId);
	
	return true;
}

/*
	@doc: toolbar.clear
	@type:
		command
	@title:
		toolbar.clear
	@short:
		Clears a toolbar
	@syntax:
		toolbar.clear [-q] <id:string>
	@switches:
		!sw: -q | --quiet
		Run quietly
	@description:
		Clears the contents of the toolbar specified by <id>. If the
		toolbar does not exist then a warning is printed unless the -q switch is used.[br]
	@seealso:
		[fnc]$toolbar.exists[/fnc],
		[cmd]toolbar.remove[/cmd],
		[cmd]toolbar.show[/cmd],
		[cmd]toolbar.destroy[/cmd]
*/

static bool toolbar_kvs_cmd_clear(KviKvsModuleCommandCall * c)
{
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);
	if(d)d->clear();
	else {
		if(!c->switches()->tqfind('q',"quiet"))
			c->warning(__tr2qs("The specified toolbar doesn't exist"));
	}

	return true;
}


/*
	@doc: toolbar.destroy
	@type:
		command
	@title:
		toolbar.destroy
	@keyterms:
		Destroying a toolbar
	@short:
		Destroys a toolbar
	@syntax:
		toolbar.destroy [-q] <id:string>
	@switches:
		!sw: -q | --quiet
		Run quietly
	@description:
		Destroys the toolbar with the specified <id>.[br]
		The toolbar definition is definitively destroyed with all the items contained.[br]
		If the -q switch is specified then this command runs in quiet mode and
		does not warn if the toolbar was not existing.
	@seealso:
		[fnc]$toolbar.exists[/fnc],
		[cmd]toolbar.create[/cmd]
*/


static bool toolbar_kvs_cmd_destroy(KviKvsModuleCommandCall * c)
{
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)

	if(!KviCustomToolBarManager::instance()->destroyDescriptor(szId))
	{
		if(!c->switches()->tqfind('q',"quiet"))
			c->warning(__tr2qs("The specified toolbar doesn't exist"));
	}

	return true;
}


/*
	@doc: toolbar.show
	@type:
		command
	@title:
		toolbar.show
	@short:
		Shows a toolbar
	@syntax:
		toolbar.show [-q] <id:string>
	@switches:
		!sw: -q | --quiet
		Run quietly
	@description:
		Shows the toolbar with the specified <id>.[br]
		If -q is specified the command runs in quiet mode and does not complain if the
		specified toolbar doesn't exist.[br]
		Please note that you don't need to show the toolbar at application startup: KVIrc will
		take care of this for you (if the user chooses to keep the toolbar visible).[br]
		You should execute this command only if you create the toolbar after KVIrc has been
		started.[br]
	@seealso:
		[fnc]$toolbar.exists[/fnc],
		[cmd]toolbar.create[/cmd]
*/

static bool toolbar_kvs_cmd_show(KviKvsModuleCommandCall * c)
{
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);

	if(!d)
	{
		if(!c->switches()->tqfind('q',"quiet"))
			c->warning(__tr2qs("The specified toolbar doesn't exist"));
		return true;
	}

	if(!d->toolBar())
		d->createToolBar();

	return true;
}

/*
	@doc: toolbar.hide
	@type:
		command
	@title:
		toolbar.hide
	@short:
		Hides a toolbar
	@syntax:
		toolbar.hide [-q] <id:string>
	@switches:
		!sw: -q | --quiet
		Run quietly
	@description:
		Hides the toolbar with the specified <id>.[br]
		If -q is specified the command runs in quiet mode and does not complain if the
		specified toolbar doesn't exist.[br]
		Please note that this command doesn't destroy the toolbar definition: the toolbar
		can be shown again by using [cmd]toolbar.show[/cmd].[br]
		If you want to completely remove the toolbar definition use [cmd]toolbar.destroy[/cmd].[br]
	@seealso:
		[fnc]$toolbar.exists[/fnc],
		[cmd]toolbar.create[/cmd],
		[cmd]toolbar.destroy[/cmd]
*/


static bool toolbar_kvs_cmd_hide(KviKvsModuleCommandCall * c)
{
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);

	if(!d)
	{
		if(!c->switches()->tqfind('q',"quiet"))
			c->warning(__tr2qs("The specified toolbar doesn't exist"));
		return true;
	}

	if(d->toolBar()) KviCustomToolBarManager::instance()->destroyDescriptor(d->id());

	return true;
}

/*
	@doc: toolbar.exists
	@type:
		function
	@title:
		$toolbar.exists
	@short:
		Checks if a toolbar exists
	@syntax:
		<boolean> $toolbar.exists(<id:string>)
	@description:
		Returns 1 if the toolbar with the specified <id> has been defined.[br]
		Note that this doesn't mean that the toolbar is visible!
	@seealso:
		[cmd]toolbar.create[/cmd]
*/


static bool toolbar_kvs_fnc_exists(KviKvsModuleFunctionCall * c)
{ 
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)

	c->returnValue()->setBoolean(KviCustomToolBarManager::instance()->tqfind(szId) ? true : false);
	return true;
}

/*
	@doc: toolbar.isVisible
	@type:
		function
	@title:
		$toolbar.isVisible
	@short:
		Checks if a toolbar is visible
	@syntax:
		<boolean> $toolbar.isVisible(<id:string>)
	@description:
		Returns 1 if the toolbar with the specified <id> is actually visible and 0 otherwise.[br]
		If the toolbar is not defined at all this function still returns 0.[br]
	@seealso:
		[cmd]toolbar.show[/cmd]
*/

static bool toolbar_kvs_fnc_isVisible(KviKvsModuleFunctionCall * c)
{ 
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)
	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);
	c->returnValue()->setBoolean(d ? (d->toolBar() ? true : false) : false);
	return true;
}

/*
	@doc: toolbar.list
	@type:
		function
	@title:
		$toolbar.list
	@short:
		Returns a list of defined toolbars
	@syntax:
		<array> $toolbar.list()
	@description:
		Returns a list of defined toolbar identifiers.[br]
		You can use this function to loop thru all the toolbar definitions.[br]
	@seealso:
*/

static bool toolbar_kvs_fnc_list(KviKvsModuleFunctionCall * c)
{ 
	KviKvsArray * a = new KviKvsArray();

	KviPointerHashTableIterator<TQString,KviCustomToolBarDescriptor> it(*(KviCustomToolBarManager::instance()->descriptors()));

	kvs_uint_t id = 0;

	while(KviCustomToolBarDescriptor * d = it.current())
	{
		a->set(id,new KviKvsVariant(d->id()));
		id++;
		++it;
	}

	c->returnValue()->setArray(a);
	return true;
}



/*
	@doc: toolbar.remove
	@type:
		command
	@title:
		toolbar.remove
	@keyterms:
		Removing toolbar items
	@short:
		Removes a toolbar item
	@syntax:
		toolbar.remove [-q] [-i] <toolbarid:string> <action:variant>
	@switches:
		!sw: -q | --quiet
		Run quietly
		!sw: -i | --index
		Treat <action> as the zero-based index of the action to be removed
		instead of the action name.
	@description:
		Removes the specified <action> from the toolbar with the specified <toolbarid>.[br]
		If no such item exists in the specified toolbar this command does nothing.[br]
		If the <toolbar> parameter doesn't identify an existing toolbar then
		a warning is printed unless the -q switch is used.[br]
		If the -i switch is specified the <action> is interpreted as zero based
		index of the action to be removed (instead of the action name).[br]
		Please note that this is an expensive operation if the toolbar is visible (basically
		the toolbar needs to be cleared and filled back again). It is a good idea to hide
		the toolbar before removing items.[br]
	@seealso:
*/

static bool toolbar_kvs_cmd_removeitem(KviKvsModuleCommandCall * c)
{
	TQString szId;
	KviKvsVariant * pvAction;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
		KVSM_PARAMETER("action",KVS_PT_VARIANT,0,pvAction)
	KVSM_PARAMETERS_END(c)

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);

	if(!pvAction)
	{
		if(!c->switches()->tqfind('q',"quiet"))
			c->error(__tr2qs("No action name/index specified")); // syntax error
		return false;
	}

	if(!d)
	{
		if(!c->switches()->tqfind('q',"quiet"))
			c->warning(__tr2qs("The specified toolbar doesn't exist"));
		return true;
	}

	if(c->switches()->tqfind('i',"index"))
	{
		kvs_int_t iAction;
		if(!pvAction->asInteger(iAction))
		{
			if(!c->switches()->tqfind('q',"quiet"))
				c->warning(__tr2qs("The action parameter didn't evaluate to an index"));
		} else {
			if(!d->removeAction(iAction))
			{
				if(!c->switches()->tqfind('q',"quiet"))
					c->warning(__tr2qs("The toolbar does not contain the specified item"));
			}
		}
	} else {
		TQString szAction;
		pvAction->asString(szAction);
		if(!d->removeAction(szAction))
		{
			if(!c->switches()->tqfind('q',"quiet"))
				c->warning(__tr2qs("The toolbar does not contain the specified item"));
		}
	}

	return true;
}

/*
	@doc: toolbar.additem
	@type:
		command
	@title:
		toolbar.additem
	@keyterms:
		Adding toolbar items
	@short:
		Adds a new item to a toolbar
	@syntax:
		toolbar.additem [-q] <toolbarid:string> <action:string>
	@switches:
		!sw: -q | --quiet
		Run quietly
	@description:
		Adds the specified <action> from the toolbar with the specified <toolbarid>.[br]
		If no such action exists this command does nothing (beside printing a warning unless -q is used).[br]
		If the <toolbar> parameter doesn't identify an existing toolbar then
		a warning is printed unless the -q switch is used.[br]
	@examples:
		[example]
			toolbar.create test $tr("My Toolbar")
			toolbar.additem test connect
			toolbar.additem test separator
			toolbar.additem test optionsdialog
			toolbar.show test
		[/example]
	@seealso:
*/

static bool toolbar_kvs_cmd_additem(KviKvsModuleCommandCall * c)
{
	TQString szId;
	TQString szAction;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
		KVSM_PARAMETER("action",KVS_PT_NONEMPTYSTRING,0,szAction)
	KVSM_PARAMETERS_END(c)

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);

	if(!d)
	{
		if(!c->switches()->tqfind('q',"quiet"))
			c->warning(__tr2qs("The specified toolbar doesn't exist"));
		return true;
	}

	if(!d->addAction(szAction))
	{
		if(!c->switches()->tqfind('q',"quiet"))c->warning(__tr2qs("The action \"%Q\" doesn't exist"),&szAction);
	}

	return true;
}


/*
	@doc: toolbar.items
	@type:
		function
	@title:
		$toolbar.items
	@short:
		Returns the list of items in a specified toolbar
	@syntax:
		<array> $toolbar.items(<toolbarid:string>)
	@description:
		Returns the array of items in the specified toolbar.
		If the toolbar doesn't exist then an empty list is returned.
	@seealso:
*/

static bool toolbar_kvs_fnc_items(KviKvsModuleFunctionCall * c)
{ 
	TQString szId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("id",KVS_PT_NONEMPTYSTRING,0,szId)
	KVSM_PARAMETERS_END(c)

	KviCustomToolBarDescriptor * d = KviCustomToolBarManager::instance()->tqfind(szId);

	if(!d)
	{
		c->warning(__tr2qs("The specified toolbar doesn't exist"));
		return true;
	}

	KviKvsArray * a = new KviKvsArray();

	KviPointerHashTableIterator<TQString,KviCustomToolBarDescriptor> it(*(KviCustomToolBarManager::instance()->descriptors()));

	kvs_uint_t id = 0;

	for(TQString * s = d->actions()->first();s;s = d->actions()->next())
	{
		a->set(id,new KviKvsVariant(*s));
		id++;
		++it;
	}

	c->returnValue()->setArray(a);
	return true;
}



static bool toolbar_module_init(KviModule * m)
{
	KVSM_REGISTER_SIMPLE_COMMAND(m,"create",toolbar_kvs_cmd_create)
	KVSM_REGISTER_SIMPLE_COMMAND(m,"clear",toolbar_kvs_cmd_clear)
	KVSM_REGISTER_SIMPLE_COMMAND(m,"destroy",toolbar_kvs_cmd_destroy)
	KVSM_REGISTER_SIMPLE_COMMAND(m,"additem",toolbar_kvs_cmd_additem)
	KVSM_REGISTER_SIMPLE_COMMAND(m,"remove",toolbar_kvs_cmd_removeitem)
	KVSM_REGISTER_SIMPLE_COMMAND(m,"show",toolbar_kvs_cmd_show)
	KVSM_REGISTER_SIMPLE_COMMAND(m,"hide",toolbar_kvs_cmd_hide)
	
	KVSM_REGISTER_FUNCTION(m,"exists",toolbar_kvs_fnc_exists)
	KVSM_REGISTER_FUNCTION(m,"isVisible",toolbar_kvs_fnc_isVisible)
	KVSM_REGISTER_FUNCTION(m,"list",toolbar_kvs_fnc_list)
	KVSM_REGISTER_FUNCTION(m,"items",toolbar_kvs_fnc_items)
	
	return true;
}

static bool toolbar_module_cleanup(KviModule *m)
{
	return true;
}

KVIRC_MODULE(
	"toolbar",                                                 // module name
	"1.0.0",                                                // module version
	"Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)", // author & (C)
	"Interface to the scriptable toolbars",
	toolbar_module_init,
	0,
	0,
	toolbar_module_cleanup
)