summaryrefslogtreecommitdiffstats
path: root/src/modules/dialog/libkvidialog.cpp
blob: cea62db455e1798a74a54ec9c5b22df197301cac (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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
//=============================================================================
//
//   File : libkvidialog.cpp
//   Creation date : Sat Sep 15 2001 01:13:25 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001-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 "libkvidialog.h"

#include <tqmessagebox.h>
#include <layout.h>
#include "kvi_tal_hbox.h"
#include <tqlineedit.h>
#ifdef COMPILE_USE_QT4
	#include <tq3multilineedit.h>
	#define TQMultiLineEdit Q3MultiLineEdit
	#include <tqdesktopwidget.h>
#else
	#include <tqmultilineedit.h>
#endif
#include <tqevent.h>
#include <tqlabel.h>
#include <tqpushbutton.h>

#include "kvi_locale.h"
#include "kvi_module.h"
#include "kvi_modulemanager.h"
#include "kvi_error.h"
#include "kvi_app.h"
#include "kvi_iconmanager.h"
#include "kvi_console.h"
#include "kvi_iconmanager.h"
#include "kvi_kvs_script.h"
#include "kvi_msgbox.h"

static KviPointerList<TQWidget> * g_pDialogModuleDialogList;

KviKvsCallbackMessageBox::KviKvsCallbackMessageBox(
	const TQString &szCaption,
	const TQString &szText,
	const TQString &szIcon,
	const TQString &szButton0,
	const TQString &szButton1,
	const TQString &szButton2,
	const TQString &szCode,
	KviKvsVariantList * pMagicParams,
	KviWindow * pWindow,bool modal)
: TQMessageBox(
	szCaption,
	szText,
	TQMessageBox::NoIcon,
	szButton0.isEmpty() ? TQMessageBox::NoButton : TQMessageBox::Ok | TQMessageBox::Default,
	szButton1.isEmpty() ? TQMessageBox::NoButton : (szButton2.isEmpty() ? TQMessageBox::No | TQMessageBox::Escape : TQMessageBox::No),
	szButton2.isEmpty() ? TQMessageBox::NoButton : TQMessageBox::Cancel | TQMessageBox::Escape,
	0,0,modal) ,
	KviKvsCallbackObject("dialog.message",pWindow,szCode,pMagicParams,0)
{
	g_pDialogModuleDialogList->append(this);
#ifndef COMPILE_USE_QT4
	setIcon(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_KVIRC)));
#endif

	TQPixmap * pix = g_pIconManager->getImage(szIcon);

	if(pix)setIconPixmap(*pix);
	else {
		if(KviTQString::equalCI(szIcon,"information"))setIcon(TQMessageBox::Information);
		else if(KviTQString::equalCI(szIcon,"warning"))setIcon(TQMessageBox::Warning);
		else if(KviTQString::equalCI(szIcon,"critical"))setIcon(TQMessageBox::Critical);
	}
	if(!szButton0.isEmpty())setButtonText(TQMessageBox::Ok,szButton0);
	if(!szButton1.isEmpty())setButtonText(TQMessageBox::No,szButton1);
	if(!szButton2.isEmpty())setButtonText(TQMessageBox::Cancel,szButton2);
}

KviKvsCallbackMessageBox::~KviKvsCallbackMessageBox()
{
	g_pDialogModuleDialogList->removeRef(this);
}

void KviKvsCallbackMessageBox::done(int code)
{
	TQMessageBox::done(code);

	kvs_int_t iVal = 0;
	
	switch(code)
	{
		case TQMessageBox::No: iVal = 1; break;
		case TQMessageBox::Cancel: iVal = 2; break;
	}

	KviKvsVariantList params;
	params.append(new KviKvsVariant(iVal));

	execute(&params);

	delete this;
}


/*
	@doc: dialog.message
	@type:
		command
	@title:
		dialog.message
	@short:
		Shows a message box
	@syntax:
		dialog.message [-b] (<caption>,<message_text>,<icon>,<button0>[,<button1>[,<button2>[,<magic1>[,<magic2>[...]]]]])
		{
			<callback_command>
		}
	@description:
		Shows a message dialog box with the specified <caption> , <message_text> , <icon> and
		buttons.[br]
		<caption> is a text string that will appear in the caption of the dialog box.[br]
		<message_text> is a text string that will appear in the dialog box and can contain HTML formatting.[br]
		<icon> is an [doc:image_id]image identifier[/doc] that defines an icon to be placed in the dialog box.
		<icon> can be a relative or absolute path to an image file , a signed number (in that case it defines
		an internal KVIrc image) or one of the special strings "critical", "information" and "warning".[br]
		<button0> is the text of the first button (on the left).[br]
		<button1> is the text of the second button (if empty or not given at all, only one button will appear in the dialog).[br]
		<button2> is the text of the third button (if empty or not given, only two buttons will appear in the dialog).[br]
		The first button is always the default button: it is activated when the user presses the
		enter key. The thirs, or the second if the third is not present, is the escape button
		and is activated when the user presses the Esc key.[br]
		<magic1>,<magic2>... are the magic parameters: evaluated at dialog.message call time and passed
		to the <callback_command> as positional parameters.[br]
		If the -b or -modal switch is specified the dialog will have non-blocking modal behaviour:
		it will appear above its parent widget and block its input until it's closed.[br] 
		Once the dialog has been shown , the user will click one of the buttons. At this point the dialog
		is hidden and the <callback_command> is executed passing the number of the button clicked
		as $0 and the magic parameters as positional parameters $1 , $2 , $3....[br]
		Please note that if the user closes the window with the window manager close button , 
		the action is interpreted as a button2 click (that is usually sth as "Cancel").[br]
	@examples:
		[example]
		[comment]# Just a warning dialog[/comment]
		dialog.message("Warning","You're being <b>warned</b>",warning,"Ok"){ echo The user clicked OK; }
		[comment]# A question[/comment]
		dialog.message("And now ?","What do you want to do ?",information,"Go home","Watch TV","Scream")
		{
			if($0 == 0)echo "The user want's to go home"
			else if($0 == 1)echo "The user want's to watch TV"
			else echo "The user wants to scream!"
		}
		[/example]
*/

static bool dialog_kvs_cmd_message(KviKvsModuleCallbackCommandCall * c)
{
	TQString szCaption,szMessage,szIcon,szButton0,szButton1,szButton2;
	KviKvsVariantList params;

	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("caption",KVS_PT_STRING,0,szCaption)
		KVSM_PARAMETER("message",KVS_PT_STRING,0,szMessage)
		KVSM_PARAMETER("icon",KVS_PT_STRING,0,szIcon)
		KVSM_PARAMETER("button0",KVS_PT_STRING,KVS_PF_OPTIONAL,szButton0)
		KVSM_PARAMETER("button1",KVS_PT_STRING,KVS_PF_OPTIONAL,szButton1)
		KVSM_PARAMETER("button2",KVS_PT_STRING,KVS_PF_OPTIONAL,szButton2)
		KVSM_PARAMETER("magic",KVS_PT_VARIANTLIST,KVS_PF_OPTIONAL,params)
	KVSM_PARAMETERS_END(c)
	bool modal;
	if(c->hasSwitch('b',"modal")) modal=true;
	else modal=false;
	TQString szCmd = c->callback()->code();

	KviKvsCallbackMessageBox * box = new KviKvsCallbackMessageBox(
			szCaption,szMessage,szIcon,szButton0,szButton1,szButton2,szCmd,&params,c->window(),modal);
	box->show();

	return true;
}



KviKvsCallbackTextInput::KviKvsCallbackTextInput(
		const TQString &szCaption,
		const TQString &szLabel,
		const TQString &szDefaultText,
		const TQString &szIcon,
		bool bMultiLine,
		const TQString &szButton0,
		const TQString &szButton1,
		const TQString &szButton2,
		const TQString &szCode,
		KviKvsVariantList * pMagicParams,
		KviWindow * pWindow,bool modal)
	: TQDialog(), KviKvsCallbackObject("dialog.textinput",pWindow,szCode,pMagicParams,0)
{
	g_pDialogModuleDialogList->append(this);
	setIcon(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_KVIRC)));
	setModal(modal);
	setCaption(szCaption);

	TQGridLayout * g = new TQGridLayout(this,2,3,5,5);

	TQPixmap * pix = g_pIconManager->getImage(szIcon);

	if(pix)
	{
		TQLabel * il = new TQLabel(this);
		il->setPixmap(*pix);
		il->setAlignment(TQt::AlignCenter);
		g->addWidget(il,0,0);
		TQLabel * tl = new TQLabel(szLabel,this);
		g->addWidget(tl,0,1);
	} else {
		TQLabel * tl = new TQLabel(szLabel,this);
		g->addMultiCellWidget(tl,0,0,0,1);
	}

	g->setColStretch(1,1);

	m_bMultiLine = bMultiLine;

	if(m_bMultiLine)
	{
		m_pEdit = new TQMultiLineEdit(this);
		((TQMultiLineEdit *)m_pEdit)->setText(szDefaultText);
	} else {
		m_pEdit = new TQLineEdit(this);
		((TQLineEdit *)m_pEdit)->setText(szDefaultText);
	}

	g->addMultiCellWidget(m_pEdit,1,1,0,1);

	KviTalHBox * box = new KviTalHBox(this);
	g->addMultiCellWidget(box,2,2,0,1);

	m_iEscapeButton = 0;
	m_iDefaultButton = 0;

	if(!szButton0.isEmpty())
	{
		TQString szB = szButton0;
		bool bDef = false;
		if(KviTQString::equalCIN(szB,"default=",8))
		{
			bDef = true;
			szB.remove(0,8);
			m_iDefaultButton = 0;
		} else if(KviTQString::equalCIN(szB,"escape=",7))
		{
			szB.remove(0,7);
			m_iEscapeButton = 0;
		}
		TQPushButton * pb1 = new TQPushButton(szB,box);
		if(bDef)pb1->setDefault(true);
		connect(pb1,TQT_SIGNAL(clicked()),this,TQT_SLOT(b0Clicked()));
	}

	if(!szButton1.isEmpty())
	{
		TQString szB = szButton1;
		bool bDef = false;
		if(KviTQString::equalCIN(szB,"default=",8))
		{
			bDef = true;
			szB.remove(0,8);
			m_iDefaultButton = 1;
		} else if(KviTQString::equalCIN(szB,"escape=",7))
		{
			szB.remove(0,7);
			m_iEscapeButton = 1;
		}
		TQPushButton * pb2 = new TQPushButton(szB,box);
		if(bDef)pb2->setDefault(true);
		connect(pb2,TQT_SIGNAL(clicked()),this,TQT_SLOT(b1Clicked()));
	}

	if(!szButton2.isEmpty())
	{
		TQString szB = szButton2;
		bool bDef = false;
		if(KviTQString::equalCIN(szB,"default=",8))
		{
			bDef = true;
			szB.remove(0,8);
			m_iDefaultButton = 2;
		} else if(KviTQString::equalCIN(szB,"escape=",7))
		{
			szB.remove(0,7);
			m_iEscapeButton = 2;
		}
		TQPushButton * pb3 = new TQPushButton(szB,box);
		if(bDef)pb3->setDefault(true);
		connect(pb3,TQT_SIGNAL(clicked()),this,TQT_SLOT(b2Clicked()));
	}

}

KviKvsCallbackTextInput::~KviKvsCallbackTextInput()
{
	g_pDialogModuleDialogList->removeRef(this);
}

void KviKvsCallbackTextInput::b0Clicked()
{
	done(0+10);
}

void KviKvsCallbackTextInput::b1Clicked()
{
	done(1+10);
}

void KviKvsCallbackTextInput::b2Clicked()
{
	done(2+10);
}

void KviKvsCallbackTextInput::closeEvent(TQCloseEvent *e)
{
	e->ignore();
	done(m_iEscapeButton+10);
}

void KviKvsCallbackTextInput::done(int code)
{
	if(code >= 10)
	{
		code -= 10;
	} else {
		switch(code)
		{
			case TQDialog::Accepted:
				code = m_iDefaultButton;
			break;
			default:
				code = m_iEscapeButton;
			break;
		}
	}

	TQString txt;
	
	if(m_bMultiLine)
	{
		txt = ((TQMultiLineEdit *)m_pEdit)->text();
	} else {
		txt = ((TQLineEdit *)m_pEdit)->text();
	}

	KviKvsVariantList params;
	params.append(new KviKvsVariant((kvs_int_t)code));
	params.append(new KviKvsVariant(txt));

	execute(&params);

	//TQDialog::done(code);

	delete this;
}

void KviKvsCallbackTextInput::showEvent(TQShowEvent *e)
{
	move((g_pApp->desktop()->width() - width())/2,(g_pApp->desktop()->height() - height()) / 2);
	TQDialog::showEvent(e);
}



/*
	@doc: dialog.textinput
	@type:
		command
	@title:
		dialog.textinput
	@short:
		Shows a dialog that accepts user input as text
	@syntax:
		dialog.textinput [-d=<default text>] [-i=<icon>] [-m] [-b] (<caption>,<info_text>,<button0>[,<button1>[,<button2>[,<magic1>[,<magic2>[...]]]]])
		{
			<callback_command>
		}
	@switches:
		!sw: -d=<default_text> | --default=<default_text>
		Set the initial text input value to <default_text>
		!sw: -i=<icon> | --icon=<icon>
		Display the specified icon, just of the left of the informational text
		!sw: -m | --multiline
		Input multiline text instead of single line
	@description:
		Shows a text input dialog box with the specified <caption> , <info_text> , <icon> and
		buttons.[br]
		<caption> is a text string that will appear in the caption of the dialog box.[br]
		<info_text> is a fixed text string that will appear in the dialog box and can contain HTML formatting.[br]
		<button0> is the text of the first button (on the left).[br]
		<button1> is the text of the second button (if empty or not given at all, only one button will appear in the dialog).[br]
		<button2> is the text of the third button (if empty or not given, only two buttons will appear in the dialog).[br]
		If one of the text strings starts with a "default=" prefix then the button is assumed
		to be the default button of the dialog and will be also activated when the user presses enter.
		If the -m switch is used , the dialog will be a multi-line text input, otherwise the user will be able to
		input only a single line of text.[br]
		If the -d switch is used , the initial text input value is set to <default text>.[br]
		If the -i switch is used , the dialog displays also the icon <icon> , just on the left ot the <info_text>[br]
		If the -b or -modal switch is specified the dialog will have non-blocking modal behaviour:
		it will appear above its parent widget and block its input until it's closed.[br] 
		In that case <icon> is an [doc:image_id]image identifier[/doc] (can be a relative or absolute
		path to an image file or a signed number (in that case it defines an internal KVIrc image).[br]
		<magic1>,<magic2>... are the magic parameters: evaluated at dialog.textinput call time and passed
		to the <callback_command> as positional parameters.[br]
		Once the dialog has been shown , the user will click one of the buttons. At this point the dialog
		is hidden and the <callback_command> is executed passing the text input value in $1, the number of the button clicked
		as $0, and the magic parameters as positional parameters $2 , $3 , $4....[br]
		Please note that if the user closes the window with the window manager close button , 
		the action is interpreted as a button2 click (that is usually sth as "Cancel").[br]
	@examples:
		[example]
		[comment]# We need a single line "reason"[/comment]
		dialog.textinput -d="Working !" (Away,<center>Please enter the <h1>away message</h1></center>,"Ok","Cancel")
		{
			switch($0)
			{
				case(0):
					away $1-
				break;
				default:
					# Cancelled
				break;
			}
		}
		[/example]
*/

static bool dialog_kvs_cmd_textinput(KviKvsModuleCallbackCommandCall * c)
{
	TQString szCaption,szInfoText,szIcon,szDefaultText,szButton0,szButton1,szButton2;
	KviKvsVariantList params;

	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("caption",KVS_PT_STRING,0,szCaption)
		KVSM_PARAMETER("info_text",KVS_PT_STRING,0,szInfoText)
		KVSM_PARAMETER("button0",KVS_PT_STRING,KVS_PF_OPTIONAL,szButton0)
		KVSM_PARAMETER("button1",KVS_PT_STRING,KVS_PF_OPTIONAL,szButton1)
		KVSM_PARAMETER("button2",KVS_PT_STRING,KVS_PF_OPTIONAL,szButton2)
		KVSM_PARAMETER("magic",KVS_PT_VARIANTLIST,KVS_PF_OPTIONAL,params)
	KVSM_PARAMETERS_END(c)

	TQString szCmd = c->callback()->code();

	c->switches()->getAsStringIfExisting('i',"icon",szIcon);
	c->switches()->getAsStringIfExisting('d',"default",szDefaultText);
	bool modal;
	if(c->hasSwitch('b',"modal")) modal=true;
	else modal=false;
	KviKvsCallbackTextInput * box = new KviKvsCallbackTextInput(
			szCaption,szInfoText,szDefaultText,szIcon,c->switches()->find('m',"multiline"),
			szButton0,szButton1,szButton2,szCmd,&params,c->window(),modal);
	box->show();

	return true;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// New KVS
KviKvsCallbackFileDialog::KviKvsCallbackFileDialog(
		const TQString &szCaption,
		const TQString &szInitialSelection,
		const TQString &szFilter,
		const TQString &szCode,
		KviKvsVariantList * pMagicParams,
		KviWindow * pWindow,bool modal)
	: KviFileDialog(szInitialSelection,szFilter,0,0,modal), KviKvsCallbackObject("dialog.file",pWindow,szCode,pMagicParams,0)
{
	g_pDialogModuleDialogList->append(this);
	setCaption(szCaption);
}

KviKvsCallbackFileDialog::~KviKvsCallbackFileDialog()
{
	g_pDialogModuleDialogList->removeRef(this);
}

void KviKvsCallbackFileDialog::done(int code)
{
	KviFileDialog::done(code);
	KviKvsVariantList params;

	if(code == TQDialog::Accepted)
	{
#ifdef COMPILE_KDE_SUPPORT
		if(mode() == KFile::ExistingOnly)
#else
		if(mode() == TQFileDialog::ExistingFiles)
#endif
		{
			KviKvsArray * a = new KviKvsArray();
			TQStringList sl = selectedFiles();
			int idx = 0;
			for(TQStringList::Iterator it = sl.begin();it != sl.end();++it)
			{
				a->set(idx,new KviKvsVariant(*it));
				idx++;
			}
			params.append(new KviKvsVariant(a));
		} else {
			params.append(new KviKvsVariant(selectedFile()));
		}
	} else {
		params.append(new KviKvsVariant(TQString("")));
	}

	hide(); // ensure we're hidden

	// ugly workaround for the TQt filedialog "destructive accept() before this reference" bug
	// we can't delete ourselves in this moment.... :(((
	// ...so skip out of this call stack and ask KviApp to destroy us just
	// when the control returns to the main loop.
	// If the module is unloaded then , KviApp will notice it and will NOT delete the dialog
	g_pApp->collectGarbage(TQT_TQOBJECT(this));

	// calling dialog.unload here WILL lead to a sigsegv (this is SURE
	// with a lot of qt versions that have the ugly file dialog "accept before this reference" bug)
	// to avoid it, we can execute the callback triggered by a timer...
	// ... umpf ...
	execute(&params);
}

/*
	@doc: dialog.file
	@type:
		command
	@title:
		dialog.file
	@short:
		Shows a file dialog
	@syntax:
		dialog.file [-b] (<mode>,<caption>[,<initial_selection[,<file_filter>[,<magic1>[,<magic2>[...]]]]]])
		{
			<callback_command>
		}
	@description:
		Shows an openfile dialog box with the specified <caption> , <initial_selection> , and <file_filter>.[br]
		<mode> can be "open" , "openm" , "save" or "dir":[br]
		"open" causes the dialog to return an existing file[br]
		"openm" is similar to open but allows returning multiple files as a comma separated list[br]
		"save" causes the dialog to return any file name (no overwrite confirmation is built in the dialog!)[br]
		"dir" causes the dialog to return an existing directory name[br]
		<mode> defaults to "open".[br]
		<caption> is a text string that will appear in the caption of the dialog box.[br]
		<initial_selection> can be a directory or filename that will be initially selected in the dialog.[br]
		Only files matching <file_filter> are selectable. If filter is an empty string, all files are selectable.[br]
		In the filter string multiple filters can be specified separated by either two semicolons next to each
		other or separated by newlines. To add two filters, one to show all C++ files and one to show all 
		header files, the filter string could look like "C++ Files (*.cpp *.cc *.C *.cxx *.c++);;Header Files (*.h *.hxx *.h++)" 
		<magic1>,<magic2>... are the magic parameters: evaluated at dialog.message call time and passed
		to the <callback_command> as positional parameters.[br]
		If the -b or -modal switch is specified the dialog will have non-blocking modal behaviour:
		it will appear above its parent widget and block its input until it's closed.[br] 
		Once the dialog has been shown , the user will select an EXISTING file and click either
		Ok or Cancel. At this point the dialog is hidden and the <callback_command> is executed passing the selected file(s) as $0
		and the magic parameters as positional parameters $1 , $2 , $3....[br]
		If the user clicks "Cancel" or does not select any file the positional parameter $0 will be empty.[br]
	@examples:
		[example]
			dialog.file(open,Choose an audio file,/home/pragma/TheAudio.au,"Audio files (*.au *.wav *.snd)")
			{
				if("$0" != "")run play $0
			}
		[/example]
*/

//#warning "Examples for these dialogs!"


static bool dialog_kvs_cmd_file(KviKvsModuleCallbackCommandCall * c)
{
	TQString szMode,szCaption,szInitialSelection,szFilter;
	KviKvsVariantList params;

	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("mode",KVS_PT_STRING,0,szMode)
		KVSM_PARAMETER("caption",KVS_PT_STRING,0,szCaption)
		KVSM_PARAMETER("initial_selection",KVS_PT_STRING,KVS_PF_OPTIONAL,szInitialSelection)
		KVSM_PARAMETER("filter",KVS_PT_STRING,KVS_PF_OPTIONAL,szFilter)
		KVSM_PARAMETER("magic",KVS_PT_VARIANTLIST,KVS_PF_OPTIONAL,params)
	KVSM_PARAMETERS_END(c)
	bool modal;
	if(c->hasSwitch('b',"modal")) modal=true;
	else modal=false;
	TQString szCmd = c->callback()->code();

	KviKvsCallbackFileDialog * box = new KviKvsCallbackFileDialog(szCaption,szInitialSelection,szFilter,szCmd,&params,c->window(),modal);

	KviFileDialog::FileMode md = KviFileDialog::ExistingFile;

	if(KviTQString::equalCI(szMode,"openm"))md = KviFileDialog::ExistingFiles;
	else if(KviTQString::equalCI(szMode,"save"))md = KviFileDialog::AnyFile;
	else if(KviTQString::equalCI(szMode,"dir"))md = KviFileDialog::DirectoryOnly;

	box->setFileMode(md);

	box->show();

	return true;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// New KVS
KviKvsCallbackImageDialog::KviKvsCallbackImageDialog(
		const TQString &szCaption,
		const TQString &szInitialSelection,
		int iType,
		int iMaxSize,
		const TQString &szCode,
		KviKvsVariantList * pMagicParams,
		KviWindow * pWindow,bool modal)
	: KviImageDialog(0,szCaption,iType,0,szInitialSelection,iMaxSize,modal), KviKvsCallbackObject("dialog.image",pWindow,szCode,pMagicParams,0)
{
	g_pDialogModuleDialogList->append(this);
}

KviKvsCallbackImageDialog::~KviKvsCallbackImageDialog()
{
	g_pDialogModuleDialogList->removeRef(this);
}

void KviKvsCallbackImageDialog::done(int code)
{
	KviImageDialog::done(code);
	KviKvsVariantList params;

	if(code == TQDialog::Accepted)
	{
		params.append(new KviKvsVariant(selectedImage()));
	} else {
		params.append(new KviKvsVariant(TQString("")));
	}

	hide(); // ensure we're hidden

	// ugly workaround for the TQt filedialog "destructive accept() before this reference" bug
	// we can't delete ourselves in this moment.... :(((
	// ...so skip out of this call stack and ask KviApp to destroy us just
	// when the control returns to the main loop.
	// If the module is unloaded then , KviApp will notice it and will NOT delete the dialog
	g_pApp->collectGarbage(TQT_TQOBJECT(this));

	// calling dialog.unload here WILL lead to a sigsegv (this is SURE
	// with a lot of qt versions that have the ugly file dialog "accept before this reference" bug)
	// to avoid it, we can execute the callback triggered by a timer...
	// ... umpf ...
	execute(&params);
}


/*
	@doc: dialog.image
	@type:
		command
	@title:
		dialog.image
	@short:
		Shows a image dialog
	@syntax:
		dialog.image [-b] (<type>,<caption>,<initial_directory>,[<maxsize>,[,<magic1>[,<magic2>[...]]]]]])
		{
			<callback_command>
		}
	@description:
		Shows a dialog that allows selecting an [doc:image_id]image_id[doc].
		The <type> parameter must be a combination of the following flags:<br>
		's' : allow selecting from the KVIrc builtin small icons<br>
		'f' : allow browsing the local directories<br>
		'a' : all of the above<br>
		The default for <type> is 'a'.<br>
		<caption> is the caption string for the dialog.<br>
		<initial_directory> makes sense only if 'f' is specified (if <initial_directory> is empty
		then the last path used by the image dialog will be used).<br>
		<maxsize> is the maximum size of the images for that the preview will be generated:
		this is 256000 bytes by default (if unspecified). Don't make it a lot bigger : it can take a lot to make
		the thumbnails for bigger images (and it can eat a considerable amount of memory).<br>
		<magic1>,<magic2>... are the magic parameters: evaluated at dialog.image call time and passed
		to the <callback_command> as positional parameters.[br]
		If the -b or -modal switch is specified the dialog will have non-blocking modal behaviour:
		it will appear above its parent widget and block its input until it's closed.[br] 
		Once the dialog has been shown , the user will select an EXISTING file and click either
		Ok or Cancel. At this point the dialog is hidden and the <callback_command> is executed passing the selected file(s) as $0
		and the magic parameters as positional parameters $1 , $2 , $3....[br]
		If the user clicks "Cancel" or does not select any image the positional parameter $0 will be empty.[br]
	@examples:
		[example]
			dialog.image(f,Choose an image file,/home/pragma/,"256000")
			{
				if("$0" != "")run kview $0
			}
		[/example]
*/

//#warning "Examples for these dialogs!"


static bool dialog_kvs_cmd_image(KviKvsModuleCallbackCommandCall * c)
{
	TQString szType,szCaption,szInitialSelection;
	kvs_uint_t iMaxSize;
	KviKvsVariantList params;

	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("mode",KVS_PT_STRING,0,szType)
		KVSM_PARAMETER("caption",KVS_PT_STRING,0,szCaption)
		KVSM_PARAMETER("initial_directory",KVS_PT_STRING,0,szInitialSelection)
		KVSM_PARAMETER("maxsize",KVS_PT_UINT,KVS_PF_OPTIONAL,iMaxSize)
		KVSM_PARAMETER("magic",KVS_PT_VARIANTLIST,KVS_PF_OPTIONAL,params)
	KVSM_PARAMETERS_END(c)
	bool modal;
	if(c->hasSwitch('b',"modal")) modal=true;
	else modal=false;
	TQString szCmd = c->callback()->code();

	int iType = 0;

	if(szType.contains('s'))iType |= KID_TYPE_BUILTIN_IMAGES_SMALL;
	if(szType.contains('f'))iType |= KID_TYPE_FULL_PATH;
	if(szType.isEmpty())iType = KID_TYPE_ALL;

	if(iMaxSize < 1)iMaxSize = 256000;

	KviKvsCallbackImageDialog * box = new KviKvsCallbackImageDialog(szCaption,szInitialSelection,iType,iMaxSize,szCmd,&params,c->window(),modal);

	box->show();

	return true;
}

/*
	@doc: dialog.yesno
	@type:
		function
	@title:
		$dialog.yesno
	@short:
		Shows a simple yes/no dialog
	@syntax:
		$dialog.yesno(<caption:string>,<szText:string>)
	@description:
		Shows a simple yes/no dialog. Returns 1 if user clicks "Yes" and 0 if (s)he clicks "No".
		Please note that this dialog is BLOCKING: it blocks execution of the script
		until the user has selected either YES or NO.
	@examples:
	@seealso:
*/

static bool dialog_kvs_fnc_yesno(KviKvsModuleFunctionCall * c)
{
	TQString szCaption;
	TQString szText;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("caption",KVS_PT_STRING,0,szCaption)
		KVSM_PARAMETER("text",KVS_PT_STRING,0,szText)
	KVSM_PARAMETERS_END(c)
	
	c->enterBlockingSection();
	bool yes=KviMessageBox::yesNo(szCaption,szText); // this will happily crash on quit ?
	if(!c->leaveBlockingSection())return true; // just die
	c->returnValue()->setBoolean(yes);
	return true;
}

/*

static int g_iLocalEventLoops = 0;

static bool dialog_module_cmd_unload(KviModule *m,KviCommand *c)
{
	// We use local loops in this module: we must FORBID explicit unloading of the
	// module while local even loops are running
	ENTER_STACK_FRAME(c,"dialog_module_cmd_unload");
	c->warning(__tr("The dialog module can't be explicitly unloaded: a modal dialog is currently open"));
	return c->leaveStackFrame();
}

static void dialog_module_entering_local_loop(KviModule * m)
{
	// Replace unload
	g_iLocalEventLoops++;
	if(g_iLocalEventLoops == 1)m->registerCommand("unload",dialog_module_cmd_unload);
}

static void dialog_module_exiting_local_loop(KviModule * m)
{
	g_pModuleManager->registerDefaultCommands(m);
	g_iLocalEventLoops--;
}

static bool dialog_module_fnc_textline(KviModule *m,KviCommand *c,KviParameterList * parms,KviStr &buffer)
{
	ENTER_STACK_FRAME(c,"dialog_module_fnc_textline");

	KviStr caption = parms->safeFirstParam();
	KviStr info    = parms->safeNextParam();
	KviStr initial = parms->safeNextParam();

	dialog_module_entering_local_loop(m);
	TQMessageBox::information(0,caption.ptr(),info.ptr(),TQMessageBox::Ok);
	dialog_module_exiting_local_loop(m);

	// It might be that the current window is no longer available!!!

	return c->leaveStackFrame();
}

*/

/*
	@doc: noblockingdialogs
	@type:
		generic
	@title:
		Why there are no blocking dialogs in KVIrc ?
	@short:
		Tecnical answer
	@description:
		Why there are no blocking dialogs in KVIrc ?[br]
		The answer is simple: because they're more confusing and tricky than it seems.[br]
		Blocking the entire program control flow while showing a dialog is
		rather a bad idea since we have to deal with external entities (servers and other users)
		that are NOT blocked. This means that the blocking dialogs must block only the
		script control-flow but let the rest of the application running.
		Such blocking dialogs actually seem to simplify scripting because
		the programmer "feels" that the control is always left in the script snippet that he is writing.
		This is actually confusing: the control IS in the script snippet but while the dialog
		is open the whole world can change: you can return from the dialog call and discover
		that the server connection no longer exists and the application is about to quit.[br]
		This may happen even with non-blocking dialogs ,but in non-blocking mode you have
		a way to handle this event. Consider the following snippet of code:[br]
		[example]
			echo My name is $?
		[/example]
		Where $? stands for a blocking input dialog that asks the user for some text.[br]
		When the input dialog returns the window that the echo was directed to no longer
		exists and you have no way to stop the echo! (Well...I could add extra code
		in the executable to handle all these situations but that would be really too expensive).[br]
		With object scripting this is actually dangerous: you might use a blocking dialog
		in an object signal handler and when returning discover that this object has been deleted!
		(The example refers to a simple object , but think about a complex hierarchy of objects
		where one random gets deleted...).[br]
		This is why the dialogs in KVIrc are non-blocking :)[br]
		That's REAL programming.
*/







static bool dialog_module_init(KviModule * m)
{
	g_pDialogModuleDialogList = new KviPointerList<TQWidget>;
	g_pDialogModuleDialogList->setAutoDelete(false);

	KVSM_REGISTER_CALLBACK_COMMAND(m,"message",dialog_kvs_cmd_message);
	KVSM_REGISTER_CALLBACK_COMMAND(m,"textinput",dialog_kvs_cmd_textinput);
	KVSM_REGISTER_CALLBACK_COMMAND(m,"file",dialog_kvs_cmd_file);
	KVSM_REGISTER_CALLBACK_COMMAND(m,"image",dialog_kvs_cmd_image);

	KVSM_REGISTER_FUNCTION(m,"yesno",dialog_kvs_fnc_yesno);

	return true;
}

static bool dialog_module_cleanup(KviModule *m)
{
	// Here we get a tragedy if g_iLocalEventLoops > 0!
	while(g_pDialogModuleDialogList->first())delete g_pDialogModuleDialogList->first();
	delete g_pDialogModuleDialogList;
	g_pDialogModuleDialogList = 0;
	return true;
}

static bool dialog_module_can_unload(KviModule *m)
{
	return g_pDialogModuleDialogList->isEmpty();
}


KVIRC_MODULE(
	"KVIrc script dialogs",
	"1.0.0",
	"Szymon Stefanek <pragma at kvirc dot net>" ,
	"Adds the /dialog.* commands functionality\n",
	dialog_module_init ,
	dialog_module_can_unload,
	0,
	dialog_module_cleanup
)

#include "libkvidialog.moc"