/
lua_utils.cpp
2182 lines (1661 loc) · 60.2 KB
/
lua_utils.cpp
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Various Lua utilities:
// Implements:
// utils.activatenotepad
// utils.appendtonotepad
// utils.callbackslist
// utils.choose
// utils.directorypicker
// utils.edit_distance
// utils.editbox
// utils.filepicker
// utils.filterpicker
// utils.fontpicker
// utils.functionargs
// utils.functionlist
// utils.getfontfamilies
// utils.glyph_available
// utils.info
// utils.infotypes
// utils.inputbox
// utils.listbox
// utils.metaphone
// utils.msgbox
// utils.multilistbox
// utils.reload_global_prefs
// utils.sendtofront
// utils.shellexecute
// utils.showdebugstatus
// utils.spellcheckdialog
// utils.timer
// utils.umsgbox
// utils.utf8decode
// utils.utf8encode
// utils.utf8convert
// utils.utf8sub
// utils.utf8valid
// utils.xmlread
// Also see: lua_compress.c for some more utils.xxx functions
#include "stdafx.h"
#include "..\mainfrm.h"
#include "..\MUSHclient.h"
#include "..\doc.h"
#include "..\dialogs\LuaInputBox.h"
#include "..\dialogs\LuaInputEditDlg.h"
#include "..\dialogs\SpellCheckDlg.h"
#include "..\dialogs\LuaChooseBox.h"
#include "..\dialogs\LuaChooseList.h"
#include "..\dialogs\LuaChooseListMulti.h"
#include "..\dialogs\FunctionListDlg.h"
#include "..\dmetaph.h"
#include <direct.h>
#include <wingdi.h>
extern char working_dir [_MAX_PATH];
extern tInfoTypeMapping InfoTypes [];
// recursive node outputter
static void xmlOuputNode (lua_State *L, CXMLelement & node)
{
POSITION pos;
lua_newtable(L); // table of this node
MakeTableItem (L, "name", node.strName); // node name, eg. <trigger>
MakeTableItem (L, "content", node.strContent); // content (inside element)
if (node.bEmpty)
MakeTableItemBool (L, "empty", node.bEmpty); // empty, eg. <br/>
MakeTableItem (L, "line", node.iLine); // line it appeared on
if (!node.AttributeMap.IsEmpty ())
{
CString strName;
CAttribute * attribute;
lua_pushliteral (L, "attributes");
lua_newtable(L); // table of attributes
// attributes
for (pos = node.AttributeMap.GetStartPosition(); pos; )
{
node.AttributeMap.GetNextAssoc (pos, strName, attribute);
MakeTableItem (L, strName, attribute->strValue); // attribute name -> value
} // end of checking each attribute
lua_rawset(L, -3); // bind attributes table to "attributes" entry
} // end of having attributes
if (!node.ChildrenList.IsEmpty ())
{
lua_pushliteral (L, "nodes");
lua_newtable(L); // table of attributes
int item = 1;
// children
for (pos = node.ChildrenList.GetHeadPosition (); pos; item++)
{
CXMLelement * pElement = node.ChildrenList.GetNext (pos);
xmlOuputNode (L, *pElement);
lua_rawseti (L, -2, item); // number each node in sequence
} // end of checking each tag
lua_rawset(L, -3); // bind children table to "children" entry
} // end of having children
}
static int xmlread (lua_State *L)
{
// get xml text
size_t xmlLength;
const char * xml = luaL_checklstring (L, 1, &xmlLength);
CXMLparser parser;
try
{
CMemFile f ((unsigned char *) xml, xmlLength);
CArchive ar (&f, CArchive::load);
parser.BuildStructure (ar.GetFile ());
} // end of try block
catch (CException* e)
{
char sMessage [1000];
e->GetErrorMessage (sMessage, sizeof sMessage);
e->Delete ();
lua_pushnil (L); // nil indicates error
lua_pushstring (L, sMessage); // nature of error
lua_pushnumber (L, parser.m_xmlLine); // which line it occurred on
return 3;
}
int iResults = 2;
// output root node
xmlOuputNode (L, parser.m_xmlRoot);
lua_pushstring (L, parser.m_strDocumentName); // root document name
if (!parser.m_CustomEntityMap.IsEmpty ())
{
lua_newtable(L); // table of custom entities
iResults++;
CString strName, strValue;
// entities
for (POSITION pos = parser.m_CustomEntityMap.GetStartPosition(); pos; )
{
parser.m_CustomEntityMap.GetNextAssoc (pos, strName, strValue);
MakeTableItem (L, strName, strValue); // entity name -> value
} // end of doing each entity
}
return iResults; // root node, plus document name, and maybe entities table
} // end of xmlread
// message-box display routine
// arg1 = message to display
// arg2 = title of box - if nil, defaults to "MUSHclient"
// arg3 = type of box: ok, abortretryignore, okcancel, retrycancel, yesno, yesnocancel
// arg4 = type of icon: ?, !, i, .
// arg5 = default button (1 - 3)
//
// returns: yes, no, ok, retry, ignore, cancel, abort, other
static int generic_msgbox (lua_State *L, const bool bUnicode)
{
const char * boxmsg = luaL_checkstring (L, 1);
const char * boxtitle = luaL_optstring (L, 2, "MUSHclient");
const char * boxtype = luaL_optstring (L, 3, "ok");
const char * boxicon = luaL_optstring (L, 4, "!");
const int boxdefault = luaL_optnumber (L, 5, 1);
if (strlen (boxmsg) > 1000)
luaL_error (L, "msgbox message too long (max 1000 characters)");
if (strlen (boxtitle) > 100)
luaL_error (L, "msgbox title too long (max 100 characters)");
int msgboxtype = MB_APPLMODAL;
// type of box
if (strcmp (boxtype, "ok") == 0)
msgboxtype = MB_OK;
else if (strcmp (boxtype, "abortretryignore") == 0)
msgboxtype = MB_ABORTRETRYIGNORE;
else if (strcmp (boxtype, "okcancel") == 0)
msgboxtype = MB_OKCANCEL;
else if (strcmp (boxtype, "retrycancel") == 0)
msgboxtype = MB_RETRYCANCEL;
else if (strcmp (boxtype, "yesno") == 0)
msgboxtype = MB_YESNO;
else if (strcmp (boxtype, "yesnocancel") == 0)
msgboxtype = MB_YESNOCANCEL;
else
luaL_error (L, "msgbox type unknown");
// icon
switch (tolower (boxicon [0]))
{
case '?' : msgboxtype |= MB_ICONQUESTION; break;
case '!' : msgboxtype |= MB_ICONEXCLAMATION; break;
case 'i' : msgboxtype |= MB_ICONINFORMATION; break;
case '.' : msgboxtype |= MB_ICONSTOP; break;
default : luaL_error (L, "msgbox icon unknown");
// default is ?
} // end of switch on icon
// default button
switch (boxdefault)
{
case 1: msgboxtype |= MB_DEFBUTTON1; break;
case 2: msgboxtype |= MB_DEFBUTTON2; break;
case 3: msgboxtype |= MB_DEFBUTTON3; break;
default: luaL_error (L, "msgbox default button must be 1, 2 or 3");
}
int result;
if (bUnicode)
result = ::UMessageBox (boxmsg, msgboxtype, boxtitle);
else
result = ::MessageBox (Frame, boxmsg, boxtitle, msgboxtype);
char * p;
switch (result)
{
case IDYES: p = "yes"; break;
case IDNO: p = "no"; break;
case IDOK: p = "ok"; break;
case IDRETRY: p = "retry"; break;
case IDIGNORE: p = "ignore"; break;
case IDCANCEL: p = "cancel"; break;
case IDABORT: p = "abort"; break;
default: p = "other"; break;
} // end of switch
lua_pushstring (L, p);
return 1;
} // end of generic_msgbox
static int msgbox (lua_State *L)
{
return generic_msgbox (L, false); // not Unicode
} // end of msgbox
static int umsgbox (lua_State *L)
{
return generic_msgbox (L, true); // Unicode
} // end of umsgbox
static int get_extra_integer (lua_State *L, const int narg, const char * name)
{
int iResult;
lua_getfield (L, narg, name);
iResult = luaL_optinteger (L, -1, 0);
lua_pop (L, 1);
return iResult;
} // end of get_extra_integer
static bool get_extra_boolean (lua_State *L, const int narg, const char * name)
{
bool bResult;
lua_getfield (L, narg, name);
bResult = optboolean (L, -1, 0);
lua_pop (L, 1);
return bResult;
} // end of get_extra_boolean
static CString get_extra_string (lua_State *L, const int narg, const char * name)
{
CString strResult;
lua_getfield (L, narg, name);
strResult = luaL_optstring (L, -1, "");
lua_pop (L, 1);
return strResult;
} // end of get_extra_string
// input-box display routine
// arg1 = message to display
// arg2 = title of box - if nil, defaults to "MUSHclient"
// arg3 = default text
// arg4 = input font
// arg5 = input font size
// arg6 = table of extra stuff:
// box_width --> width of message box in pixels (min 180)
// box_height --> height of message box in pixels (min 125)
// prompt_width --> width of prompt text in pixels (min 10)
// prompt_height --> height of prompt text in pixels (min 12)
// reply_width --> width of reply in pixels (min 10)
// reply_height --> height of reply in pixels (min 10)
// max_length --> max characters they can type (min 1)
// validate --> validation function
// ok_button --> what to put on the OK button (default: OK)
// cancel_button --> what to put on the Cancel button (default: Cancel)
// read_only --> text cannot be changed
// ok_button_width --> width of OK button in pixels
// cancel_button_width --> width of Cancel button in pixels
// no_default --> there is no default button
//
// returns: what they typed, or nil if cancelled
template <typename T>
static int gen_inputbox (lua_State *L, T & msg)
{
const char * inputmsg = luaL_checkstring (L, 1);
const char * inputtitle = luaL_optstring (L, 2, "MUSHclient");
const char * inputdefault = luaL_optstring (L, 3, "");
const char * inputfont = luaL_optstring (L, 4, "");
const int inputsize = luaL_optnumber (L, 5, 9);
const int nExtraStuffArg = 6; // arg 6 is extra stuff
// zero means take dialog default
int iBoxWidth = 0;
int iBoxHeight = 0;
int iPromptWidth = 0;
int iPromptHeight = 0;
int iReplyWidth = 0;
int iReplyHeight = 0;
int iMaxReplyLength = 0;
bool bReadOnly = false;
bool bNoDefault = false;
CString strOKbuttonLabel;
CString strCancelbuttonLabel;
int iOKbuttonWidth = 0;
int iCancelbuttonWidth = 0;
// if arg6 present, and a table, grab extra stuff
if (lua_istable (L, nExtraStuffArg))
{
iBoxWidth = get_extra_integer (L, nExtraStuffArg, "box_width");
iBoxHeight = get_extra_integer (L, nExtraStuffArg, "box_height");
iPromptWidth = get_extra_integer (L, nExtraStuffArg, "prompt_width");
iPromptHeight = get_extra_integer (L, nExtraStuffArg, "prompt_height");
iReplyWidth = get_extra_integer (L, nExtraStuffArg, "reply_width");
iReplyHeight = get_extra_integer (L, nExtraStuffArg, "reply_height");
iMaxReplyLength = get_extra_integer (L, nExtraStuffArg, "max_length");
bReadOnly = get_extra_boolean (L, nExtraStuffArg, "read_only");
strOKbuttonLabel = get_extra_string (L, nExtraStuffArg, "ok_button");
strCancelbuttonLabel = get_extra_string (L, nExtraStuffArg, "cancel_button");
iOKbuttonWidth = get_extra_integer (L, nExtraStuffArg, "ok_button_width");
iCancelbuttonWidth = get_extra_integer (L, nExtraStuffArg, "cancel_button_width");
bNoDefault = get_extra_boolean (L, nExtraStuffArg, "no_default");
// see if validation function there - do this last so we can leave it on the stack
lua_getfield (L, nExtraStuffArg, "validate");
if (!lua_isnil (L, -1))
{
if (!lua_isfunction (L, -1))
luaL_error (L, "inputbox argument #6 value for 'validate' must be a function");
lua_pushvalue (L, -1); // function is now on top of stack
msg.m_L = L; // non-NULL indicates we have function there
// we can't leave the function on the stack, that gets cleared from time to time
// while the dialog box is running - so we store it in the registry and get the
// unique index back
msg.m_iValidationIndex = luaL_ref (L, LUA_REGISTRYINDEX);
} // validate function there
} // table of extra stuff there
if (strlen (inputmsg) > 1000)
luaL_error (L, "inputbox message too long (max 1000 characters)");
// if we leave in & it will make the next letter underlined
string sInputMsg = FindAndReplace (inputmsg, "&", "&&");
if (strlen (inputtitle) > 100)
luaL_error (L, "inputbox title too long (max 100 characters)");
msg.m_strMessage = sInputMsg.c_str ();
msg.m_strTitle = inputtitle;
msg.m_strReply = inputdefault;
msg.m_strFont = inputfont;
msg.m_iFontSize = inputsize;
// extra stuff from table which is argument 6 (if present)
msg.m_iBoxWidth = iBoxWidth ;
msg.m_iBoxHeight = iBoxHeight ;
msg.m_iPromptWidth = iPromptWidth ;
msg.m_iPromptHeight = iPromptHeight;
msg.m_iReplyWidth = iReplyWidth ;
msg.m_iReplyHeight = iReplyHeight ;
msg.m_iMaxReplyLength = iMaxReplyLength;
msg.m_bReadOnly = bReadOnly;
msg.m_strOKbuttonLabel = strOKbuttonLabel ;
msg.m_strCancelbuttonLabel = strCancelbuttonLabel;
msg.m_iOKbuttonWidth = iOKbuttonWidth ;
msg.m_iCancelbuttonWidth = iCancelbuttonWidth;
msg.m_bNoDefault = bNoDefault;
lua_settop (L, 0);
if (msg.DoModal () != IDOK)
lua_pushnil (L);
else
lua_pushstring (L, msg.m_strReply);
// free up registry entry (is OK with LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, msg.m_iValidationIndex);
return 1;
} // end of gen_inputbox
static int inputbox (lua_State *L)
{
CLuaInputBox msg;
return gen_inputbox (L, msg);
} // end of inputbox
static int editbox (lua_State *L)
{
CLuaInputEditDlg msg;
return gen_inputbox (L, msg);
} // end of editbox
// combo-box choose routine
// arg1 = message to display
// arg2 = title of box - if nil, defaults to "MUSHclient"
// arg3 = table of key/value pairs (value is displayed)
// arg4 = default *key*
//
// returns: chosen key, or nil if: cancelled, or none selected
template <typename T>
static int gen_choose (lua_State *L, T & msg)
{
const char * choosemsg = luaL_checkstring (L, 1);
const char * choosetitle = luaL_optstring (L, 2, "MUSHclient");
// arg3 is table
// arg4 is default
bool bDefaultIsNumber = lua_type (L, 4) == LUA_TNUMBER;
bool bHaveDefault = lua_gettop (L) >= 4 && !lua_isnil (L, 4);
string defaultstring;
lua_Number defaultnumber = 0;
if (bHaveDefault)
{
if (!lua_isstring (L, 4))
luaL_error (L, "default key must be string or number");
if (bDefaultIsNumber)
defaultnumber = lua_tonumber (L, 4);
else
defaultstring = lua_tostring (L, 4);
}
// if we leave in & it will make the next letter underlined
string sChooseMsg = FindAndReplace (choosemsg, "&", "&&");
if (sChooseMsg.length () > 1000)
luaL_error (L, "message too long (max 1000 characters)");
if (strlen (choosetitle) > 100)
luaL_error (L, "title too long (max 100 characters)");
const int table = 3;
if (!lua_istable (L, table))
luaL_error (L, "must have table of choices as 3rd argument");
msg.m_strMessage = sChooseMsg.c_str ();
msg.m_strTitle = choosetitle;
// standard Lua table iteration
for (lua_pushnil (L); lua_next (L, table) != 0; lua_pop (L, 1))
{
if (!lua_isstring (L, -2))
luaL_error (L, "table must have string or number keys");
if (!lua_isstring (L, -1))
luaL_error (L, "table must have string or number values");
// value can simply be converted to a string
string sValue = lua_tostring (L, -1);
// get key
CKeyValuePair kv (sValue);
if (lua_type (L, -2) == LUA_TSTRING)
{
kv.sKey_ = lua_tostring (L, -2);
// remember default position
if (bHaveDefault &&
!bDefaultIsNumber &&
kv.sKey_ == defaultstring)
msg.m_iDefault = msg.m_data.size ();
}
else
{ // not string, should be number :)
// cannot do lua_tostring because that confuses lua_next
kv.bNumber_ = true;
kv.iKey_ = lua_tonumber (L, -2);
// remember default position
if (bHaveDefault &&
bDefaultIsNumber &&
kv.iKey_ == defaultnumber)
msg.m_iDefault = msg.m_data.size ();
} // end of key being a number
msg.m_data.push_back (kv);
} // end of looping through table
if (msg.DoModal () != IDOK)
lua_pushnil (L);
else
{ // not cancelled
if (msg.m_iResult == CB_ERR)
lua_pushnil (L); // no choice made
else
{ // get key of choice
if (msg.m_data [msg.m_iResult].bNumber_)
lua_pushnumber (L, msg.m_data [msg.m_iResult].iKey_);
else
lua_pushstring (L, msg.m_data [msg.m_iResult].sKey_.c_str ());
}
}
return 1; // 1 result
} // end of gen_choose
static int choose (lua_State *L)
{
CLuaChooseBox msg;
return gen_choose (L, msg); // templated function above
} // end of choose
static int listbox (lua_State *L)
{
CLuaChooseList msg;
return gen_choose (L, msg); // templated function above
} // end of listbox
// combo-box choose routine
// arg1 = message to display
// arg2 = title of box - if nil, defaults to "MUSHclient"
// arg3 = table of key/value pairs (value is displayed)
// arg4 = default *key*
//
// returns: chosen key, or nil if: cancelled, or none selected
static int multilistbox (lua_State *L)
{
CLuaChooseListMulti msg;
const char * choosemsg = luaL_checkstring (L, 1);
const char * choosetitle = luaL_optstring (L, 2, "MUSHclient");
// arg3 is table
// arg4 is table of defaults
bool bHaveDefaults = lua_gettop (L) >= 4 && !lua_isnil (L, 4);
if (bHaveDefaults)
{
if (!lua_istable (L, 4))
luaL_error (L, "defaults must be a table, or nil");
}
// if we leave in & it will make the next letter underlined
string sChooseMsg = FindAndReplace (choosemsg, "&", "&&");
if (sChooseMsg.length () > 1000)
luaL_error (L, "message too long (max 1000 characters)");
if (strlen (choosetitle) > 100)
luaL_error (L, "title too long (max 100 characters)");
if (!lua_istable (L, 3))
luaL_error (L, "must have table of choices as 3rd argument");
msg.m_strMessage = sChooseMsg.c_str ();
msg.m_strTitle = choosetitle;
const int table = 3;
// standard Lua table iteration
for (lua_pushnil (L); lua_next (L, table) != 0; lua_pop (L, 1))
{
if (!lua_isstring (L, -2))
luaL_error (L, "table must have string or number keys");
if (!lua_isstring (L, -1))
luaL_error (L, "table must have string or number values");
// value can simply be converted to a string
string sValue = lua_tostring (L, -1);
// get key
CKeyValuePair kv (sValue);
if (lua_type (L, -2) == LUA_TSTRING)
{
kv.sKey_ = lua_tostring (L, -2);
} // end string key
else
{ // not string, should be number :)
kv.bNumber_ = true;
kv.iKey_ = lua_tonumber (L, -2);
} // end of key being a number
// remember default positions
if (bHaveDefaults)
{
lua_pushvalue (L, -2); // copy key to top of stack
lua_gettable (L, 4); // see if this key is in defaults table
if (!(lua_isnil (L, -1) ||
lua_isboolean (L, -1) && !lua_toboolean (L, -1)))
msg.m_iDefaults.insert (msg.m_data.size ());
lua_pop (L, 1); // remove result of table-lookup
} // end of having a defaults table
msg.m_data.push_back (kv);
} // end of looping through table
lua_settop (L, 0); // get rid of stuff lying around
if (msg.DoModal () != IDOK)
lua_pushnil (L);
else
{ // not cancelled
lua_newtable(L); // table of results
set<int>::const_iterator iter;
for (iter = msg.m_iResults.begin ();
iter != msg.m_iResults.end ();
iter++)
{
if (msg.m_data [*iter].bNumber_)
lua_pushnumber (L, msg.m_data [*iter].iKey_);
else
lua_pushstring (L, msg.m_data [*iter].sKey_.c_str ());
lua_pushboolean (L, 1); //
lua_settable (L, -3); // add key = true to table
} // end of processing each result
}
return 1; // 1 result
} // end of multilistbox
extern tInternalFunctionsTable InternalFunctionsTable [1];
// returns table of internal functions
static int functionlist (lua_State *L)
{
lua_newtable(L); // table of function names
for (int i = 0; InternalFunctionsTable [i].sFunction [0]; i++)
{
lua_pushstring (L, InternalFunctionsTable [i].sFunction);
lua_rawseti(L, -2, i + 1); // make 1-relative
}
return 1; // 1 table
} // end of functionlist
// returns table of internal functions keyed by name, value is argument list
static int functionargs (lua_State *L)
{
lua_newtable(L); // table of function names
for (int i = 0; InternalFunctionsTable [i].sFunction [0]; i++)
{
lua_pushstring (L, InternalFunctionsTable [i].sFunction);
lua_pushstring (L, InternalFunctionsTable [i].sArgs);
lua_rawset(L, -3);
}
return 1; // 1 table
} // end of functionargs
// returns table of internal functions
static int callbackslist (lua_State *L)
{
lua_newtable(L); // table of function names
for (int i = 0; PluginCallbacksNames [i] != ""; i++)
{
lua_pushstring (L, PluginCallbacksNames [i].c_str ());
lua_rawseti(L, -2, i + 1); // make 1-relative
}
return 1; // 1 table
} // end of callbackslist
// file picker routine
// arg1 = title of dialog
// arg2 = default file name
// arg3 = default extension
// arg4 = filter table
// arg5 = true to save, false or nil to load
//
// returns: chosen file name, or nil if: cancelled, or none selected
static int filepicker (lua_State *L)
{
BOOL bLoad; // true to load, false to save
DWORD flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
const char * sTitle = luaL_optstring (L, 1, "");
const char * sDefaultName = luaL_optstring (L, 2, "");
const char * sDefaultExtension = luaL_optstring (L, 3, "");
string sFilter; // arg 4 = table
bool bSave = optboolean (L, 5, 0);
if (bSave)
{
bLoad = false;
}
else
{
bLoad = TRUE;
flags |= OFN_FILEMUSTEXIST;
}
if (sTitle [0] == 0)
sTitle = NULL;
if (sDefaultName [0] == 0)
sDefaultName = NULL;
if (sDefaultExtension [0] == 0)
sDefaultExtension = NULL;
const int table = 4;
if (lua_gettop (L) >= table && !lua_isnil (L, table))
{
if (!lua_istable (L, table))
luaL_error (L, "argument 4 must be a table of filters, or nil");
// construct filter
// standard Lua table iteration
for (lua_pushnil (L); lua_next (L, table) != 0; lua_pop (L, 1))
{
if (lua_type (L, -1) != LUA_TSTRING ||
lua_type (L, -2) != LUA_TSTRING )
luaL_error (L, "table of filters must be suffix/description pair");
string sDescription = lua_tostring (L, -1);
string sSuffix = lua_tostring (L, -2);
sFilter += sDescription;
sFilter += " (*.";
sFilter += sSuffix;
sFilter += ")|*.";
sFilter += sSuffix;
sFilter += "|";
} // end of looping through table
} // end of not nil for argument 4
sFilter += "|";
CFileDialog dlg (
bLoad,
sDefaultExtension,
sDefaultName,
flags,
sFilter.c_str (),
NULL); // parent window
if (sTitle)
dlg.m_ofn.lpstrTitle = sTitle;
ChangeToFileBrowsingDirectory ();
int nResult = dlg.DoModal();
ChangeToStartupDirectory ();
if (nResult != IDOK)
lua_pushnil (L);
else
lua_pushstring (L, dlg.GetPathName());
return 1;
} // end of filepicker
// The field below is needed to initialise the browse directory dialog
// with the initial directory
extern CString strStartingDirectory;
// function prototypes needed for folder browsing
extern int __stdcall BrowseCallbackProc(
HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData
);
// directory picker routine
// arg1 = title of dialog
// arg2 = default directory name
//
// returns: chosen directory name, or nil if: cancelled, or none selected
// test: /print (utils.directorypicker ("test", "c:\\source"))
static int directorypicker (lua_State *L)
{
const char * sTitle = luaL_optstring (L, 1, "");
const char * sDefaultName = luaL_optstring (L, 2, "");
// Gets the Shell's default allocator
LPMALLOC pMalloc;
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
char pszBuffer[MAX_PATH]; // receives result
BROWSEINFO bi;
LPITEMIDLIST pidl;
// Get help on BROWSEINFO struct - it's got all the bit settings.
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = sTitle;
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
// if possible, let them create one
if (!bWine)
bi.ulFlags |= BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // requires CoInitialize
bi.lpfn = BrowseCallbackProc;
bi.lParam = 0;
strStartingDirectory = sDefaultName;
// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (::SHGetPathFromIDList(pidl, pszBuffer))
lua_pushstring (L, pszBuffer);
else
lua_pushnil (L);
// Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
else
lua_pushnil (L);
// Release the shell's allocator.
pMalloc->Release();
}
return 1;
} // end of directorypicker
static int spellcheckdialog (lua_State *L)
{
CSpellCheckDlg dlg;
dlg.m_strMisspeltWord = luaL_checkstring (L, 1);
if (lua_gettop (L) >= 2 && !lua_isnil (L, 2))
{
if (!lua_istable (L, 2))
luaL_error (L, "argument 2 must be a table of suggestions, or nil");
for (int i = 1; ; i++)
{
lua_rawgeti (L, 2, i); // get i'th item
if (lua_isnil (L, -1))
break; // first nil key, leave loop
dlg.m_suggestions.push_back (luaL_checkstring (L, -1));
lua_pop (L, 1); // remove value
} // end of looping through table
} // end of not nil for argument 2
if (dlg.DoModal () != IDOK)
{
lua_pushnil (L);
return 1;
}
lua_pushstring (L, dlg.m_sAction.c_str ());
lua_pushstring (L, dlg.m_strMisspeltWord);
return 2;
} // end of spellcheckdialog
static int metaphone (lua_State *L)
{
MString m (luaL_checkstring (L, 1), luaL_optnumber (L, 2, 4));
CString str1, str2;
m.DoubleMetaphone (str1, str2);
lua_pushstring (L, str1); // first result
if (str2.IsEmpty ())
lua_pushnil (L);
else
lua_pushstring (L, str2); // second result
return 2;
} // end of metaphone
static int info (lua_State *L)
{
lua_newtable(L); // table to hold this stuff
char * p = _getcwd (NULL, 1024);
if (p)
{
CString strPath (p);
free (p);
if (strPath.Right (1) != "\\")
strPath += "\\"; // append trailing backslash
MakeTableItem (L, "current_directory", strPath);
}
MakeTableItem (L, "app_directory", ExtractDirectory (App.m_strMUSHclientFileName));
MakeTableItem (L, "world_files_directory", App.m_strDefaultWorldFileDirectory);
MakeTableItem (L, "state_files_directory", App.m_strDefaultStateFilesDirectory);
MakeTableItem (L, "locale", App.m_strLocale);
MakeTableItem (L, "fixed_pitch_font", App.m_strFixedPitchFont);
MakeTableItem (L, "fixed_pitch_font_size", App.m_iFixedPitchFontSize);
MakeTableItem (L, "log_files_directory", App.m_strDefaultLogFileDirectory);
MakeTableItem (L, "plugins_directory", App.m_strPluginsDirectory);
MakeTableItem (L, "startup_directory", CString (working_dir));
MakeTableItem (L, "translator_file", App.m_strTranslatorFile);
return 1;
} // end of metaphone
static int fontpicker (lua_State *L)
{
const char * fontname = luaL_optstring (L, 1, ""); // preferred name
const int fontsize = luaL_optnumber (L, 2, 10); // preferred size
const int fontcolour = luaL_optnumber (L, 3, 0); // preferred colour
CDC dc;
dc.CreateCompatibleDC (NULL);
LOGFONT lf;
ZeroMemory (&lf, sizeof lf);
lf.lfHeight = -MulDiv(fontsize, dc.GetDeviceCaps(LOGPIXELSY), 72);
strncpy (lf.lfFaceName, fontname, sizeof (lf.lfFaceName) - 1);
lf.lfFaceName [sizeof (lf.lfFaceName) - 1] = 0;
lf.lfWeight = FW_NORMAL;
CFontDialog dlg (&lf,