-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_ext.cpp
1597 lines (1428 loc) · 36.4 KB
/
gui_ext.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
/* gui_ext.cpp
This is a simple set of predefined GUI windows for SciTE,
built using the YAWL library.
Steve Donovan, 2007.
*/
#include <windows.h>
#include "yawl.h"
#include <string.h>
#include <vector>
#include <io.h>
#include <direct.h>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
const char* WINDOW_CLASS = "WINDOW*";
const int BUFFER_SIZE = 2*0xFFFF;
static TWin* s_parent = NULL;
static TWin* s_last_parent = NULL;
#define EQ(s1,s2) (wcscmp((s1),(s2))==0)
// ==========================================================================
enum { SURROGATE_LEAD_FIRST = 0xD800 };
enum { SURROGATE_TRAIL_FIRST = 0xDC00 };
enum { SURROGATE_TRAIL_LAST = 0xDFFF };
static unsigned int UTF8Length(const wchar_t *uptr, size_t tlen) {
size_t len = 0;
for (size_t i = 0; i < tlen && uptr[i];) {
unsigned int uch = uptr[i];
if (uch < 0x80) {
len++;
} else if (uch < 0x800) {
len += 2;
} else if ((uch >= SURROGATE_LEAD_FIRST) &&
(uch <= SURROGATE_TRAIL_LAST)) {
len += 4;
i++;
} else {
len += 3;
}
i++;
}
return len;
}
static void UTF8FromUTF16(const wchar_t *uptr, size_t tlen, char *putf, size_t len) {
int k = 0;
for (size_t i = 0; i < tlen && uptr[i];) {
unsigned int uch = uptr[i];
if (uch < 0x80) {
putf[k++] = static_cast<char>(uch);
} else if (uch < 0x800) {
putf[k++] = static_cast<char>(0xC0 | (uch >> 6));
putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
} else if ((uch >= SURROGATE_LEAD_FIRST) &&
(uch <= SURROGATE_TRAIL_LAST)) {
// Half a surrogate pair
i++;
unsigned int xch = 0x10000 + ((uch & 0x3ff) << 10) + (uptr[i] & 0x3ff);
putf[k++] = static_cast<char>(0xF0 | (xch >> 18));
putf[k++] = static_cast<char>(0x80 | ((xch >> 12) & 0x3f));
putf[k++] = static_cast<char>(0x80 | ((xch >> 6) & 0x3f));
putf[k++] = static_cast<char>(0x80 | (xch & 0x3f));
} else {
putf[k++] = static_cast<char>(0xE0 | (uch >> 12));
putf[k++] = static_cast<char>(0x80 | ((uch >> 6) & 0x3f));
putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
}
i++;
}
putf[len] = '\0';
}
static size_t UTF16Length(const char *s, unsigned int len) {
size_t ulen = 0;
size_t charLen;
for (size_t i=0; i<len;) {
unsigned char ch = static_cast<unsigned char>(s[i]);
if (ch < 0x80) {
charLen = 1;
} else if (ch < 0x80 + 0x40 + 0x20) {
charLen = 2;
} else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
charLen = 3;
} else {
charLen = 4;
ulen++;
}
i += charLen;
ulen++;
}
return ulen;
}
static size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen) {
size_t ui=0;
const unsigned char *us = reinterpret_cast<const unsigned char *>(s);
size_t i=0;
while ((i<len) && (ui<tlen)) {
unsigned char ch = us[i++];
if (ch < 0x80) {
tbuf[ui] = ch;
} else if (ch < 0x80 + 0x40 + 0x20) {
tbuf[ui] = static_cast<wchar_t>((ch & 0x1F) << 6);
ch = us[i++];
tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
} else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
tbuf[ui] = static_cast<wchar_t>((ch & 0xF) << 12);
ch = us[i++];
tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + ((ch & 0x7F) << 6));
ch = us[i++];
tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
} else {
// Outside the BMP so need two surrogates
int val = (ch & 0x7) << 18;
ch = us[i++];
val += (ch & 0x3F) << 12;
ch = us[i++];
val += (ch & 0x3F) << 6;
ch = us[i++];
val += (ch & 0x3F);
tbuf[ui] = static_cast<wchar_t>(((val - 0x10000) >> 10) + SURROGATE_LEAD_FIRST);
ui++;
tbuf[ui] = static_cast<wchar_t>((val & 0x3ff) + SURROGATE_TRAIL_FIRST);
}
ui++;
}
return ui;
}
wchar_t* StringFromUTF8(const char *s) {
size_t sLen = s ? strlen(s) : 0;
size_t wideLen = UTF16Length(s, sLen);
wchar_t* vgc = new wchar_t[wideLen + 1];
size_t outLen = UTF16FromUTF8(s, sLen, vgc, wideLen);
vgc[outLen] = 0;
return vgc;
}
char* UTF8FromString(const wchar_t *s) {
size_t sLen = wcslen(s);
size_t narrowLen = UTF8Length(s, sLen);
char* vc = new char[narrowLen + 1];
UTF8FromUTF16(s, sLen, vc, narrowLen);
return vc;
}
// -------------------------------------------------------------------
TWin* get_parent()
{
return s_parent;
}
TWin* get_last_parent()
{
return s_last_parent;
}
TWin *get_desktop_window()
{
static TWin *desk = NULL;
if (desk == NULL) {
desk = new TWin(GetDesktopWindow());
}
return desk;
}
class PromptDlg: public TModalDlg {
public:
wchar_t m_val[256];
const wchar_t *m_field_name;
PromptDlg(TEventWindow *parent, pchar field_name, const wchar_t* val)
: TModalDlg(parent,L"Enter:"), m_field_name(field_name)
{
wcscpy(m_val,val);
}
void layout(Layout& lo)
{
lo << Field(m_field_name,m_val);
}
};
// show a message on the SciTE output window
void OutputMessage(lua_State *L)
{
if (lua_isstring(L,-1)) {
size_t len;
const char *msg = lua_tolstring(L,-1,&len);
char *buff = new char[len+2];
strncpy(buff,msg,len);
buff[len] = '\n';
buff[len+1] = '\0';
lua_pop(L,1);
if (lua_checkstack(L,3)) {
lua_getglobal(L,"output");
lua_getfield(L,-1,"AddText");
lua_insert(L,-2);
lua_pushstring(L,buff);
lua_pcall(L,2,0,0);
}
delete[] buff;
}
}
class LuaWindow: public TEventWindow
{
protected:
lua_State* L;
public:
LuaWindow(const wchar_t* caption,lua_State* l, TWin *parent, int stylex = 0, bool is_child = false, int style = -1)
: TEventWindow(caption,parent,stylex,is_child,style),L(l)
{}
void handler(Item* item)
{
wchar_t* name = (wchar_t*)item->data;
if (wcsncmp(name,L"IDM_",4) == 0) {
wchar_t buff[128];
swprintf(buff,L"scite.MenuCommand(%s)",name); //
luaL_dostring(L,UTF8FromString(buff));
} else {
lua_getglobal(L,UTF8FromString(name));
if (lua_pcall(L,0,0,0)) {
OutputMessage(L);
}
}
}
};
class PanelWindow: public LuaWindow
{
public:
PanelWindow(lua_State* l)
: LuaWindow(L"",l,get_parent(),0,true)
{}
};
class PaletteWindow;
static PaletteWindow* instances[50];
static int n_instances = 0;
class PaletteWindow: public LuaWindow
{
protected:
bool m_shown;
public:
PaletteWindow(const wchar_t* caption, lua_State* l, int stylex = 0, int style = -1)
: LuaWindow(caption,l,NULL,stylex,false,style)
{
instances[n_instances++] = this;
instances[n_instances] = NULL;
}
void show(int how = SW_SHOW)
{
TEventWindow::show(how);
m_shown = true;
}
void hide()
{
TEventWindow::hide();
m_shown = false;
}
virtual bool query_close()
{
hide();
return false;
}
void really_show()
{
TEventWindow::show();
}
void really_hide()
{
TEventWindow::hide();
}
static void set_visibility(bool yesno)
{
for (int i = 0; instances[i]; i++) {
PaletteWindow *w = instances[i];
if (w->m_shown) {
if (yesno) w->really_show();
else w->really_hide();
}
}
}
};
class ToolbarWindow: public PaletteWindow
{
public:
ToolbarWindow(const wchar_t* caption, wchar_t** item, int sz, const wchar_t* path, lua_State* l)
: PaletteWindow(caption,l,WS_EX_PALETTEWINDOW, WS_OVERLAPPED)
{
TToolbar tbar(this,sz,sz);
tbar.set_path(path);
for (;*item; item++) {
wchar_t* img_text = wcstok(*item,L"|");
wchar_t* fun = wcstok(NULL,L"");
tbar << Item(img_text,(EH)&LuaWindow::handler,fun);
}
tbar.release();
SIZE sze = tbar.get_size();
client_resize(sze.cx, sze.cy + 10);
}
};
////// This acts as the top-level frame window containing these controls; it supports
////// adding extra buttons and actions.
class ContainerWindow: public PaletteWindow
{
public:
TListView* listv;
const wchar_t* select_name;
const wchar_t* double_name;
ContainerWindow(const wchar_t* caption, lua_State* l)
: PaletteWindow(caption,l),select_name(NULL),double_name(NULL)
{
set_icon_from_window(s_parent);
}
void dispatch(const wchar_t* name, int ival)
{
if (name != NULL) {
lua_getglobal(L,UTF8FromString(name));
lua_pushnumber(L,ival);
if (lua_pcall(L,1,0,0)) {
OutputMessage(L);
}
}
}
void on_button(Item* item)
{
dispatch((wchar_t*)item->data,0); //listv->selected_id()
}
void add_buttons(lua_State* L)
{
int nargs = lua_gettop(L);
int i = 2;
TEW* panel = new TEW(NULL,this,0,true);
panel->align(alBottom,50);
Layout layout(panel,this);
while (i < nargs) {
layout << Button(StringFromUTF8(luaL_checkstring(L,i)),(EH)&ContainerWindow::on_button,(void*)StringFromUTF8(luaL_checkstring(L,i+1)));
i += 2;
}
layout.release();
add(panel);
size();
}
};
void dispatch_ref(lua_State* L,int idx, int ival)
{
if (idx != 0) {
lua_rawgeti(L,LUA_REGISTRYINDEX,idx);
lua_pushnumber(L,ival);
if (lua_pcall(L,1,0,0)) {
OutputMessage(L);
}
}
}
bool function_ref(lua_State* L, int idx, int* pr)
{
if (*pr != 0) {
luaL_unref(L,LUA_REGISTRYINDEX,*pr);
}
lua_pushvalue(L,idx);
*pr = luaL_ref(L,LUA_REGISTRYINDEX);
return true;
}
class LuaControl
{
protected:
lua_State *L;
int select_idx;
int double_idx;
int onkey_idx;
int rclick_idx;
Handle m_hpopup_menu;
public:
LuaControl(lua_State *l)
: L(l), select_idx(0), double_idx(0), onkey_idx(0), rclick_idx(0)
, m_hpopup_menu(0)
{}
void set_popup_menu(Handle menu)
{
m_hpopup_menu = menu;
}
virtual void set_select(int iarg)
{
function_ref(L,iarg,&select_idx);
}
virtual void set_double_click(int iarg)
{
function_ref(L,2,&double_idx);
}
virtual void set_onkey(int iarg)
{
function_ref(L,iarg,&onkey_idx);
}
virtual void set_rclick(int iarg)
{
function_ref(L,iarg,&rclick_idx);
}
};
class TListViewLua: public TListViewB, public LuaControl
{
public:
TListViewLua(TWin *parent, lua_State *l,bool multiple_columns = false, bool single_select = true)
: TListViewB(parent, false, multiple_columns, single_select),
LuaControl(l)
{
if (! multiple_columns) {
add_column(L"*",200);
}
}
// implement
virtual void handle_select(int id)
{
dispatch_ref(L,select_idx,id);
}
virtual void handle_double_click(int id)
{
dispatch_ref(L,double_idx,id);
}
virtual void handle_onkey(int id)
{
dispatch_ref(L,onkey_idx,id);
}
virtual int handle_rclick(int id)
{
if(m_hpopup_menu) {
POINT p;
GetCursorPos(&p);
HWND hwnd = (HWND)(get_parent_win()->handle());
TrackPopupMenu((HMENU)m_hpopup_menu, TPM_LEFTALIGN | TPM_TOPALIGN, p.x, p.y, 0, hwnd, NULL);
return 1;
}
return 0;
}
};
class TTabControlLua: public TTabControlB, public LuaControl
{
int selection_changing_idx;
TEventWindow *form;
public:
TTabControlLua (TEventWindow *parent,lua_State* l)
: TTabControlB(parent),LuaControl(l),selection_changing_idx(0),form(parent)
{
}
void set_selection_changing(int iarg)
{
function_ref(L,iarg,&selection_changing_idx);
}
// implement
virtual void handle_select(int id)
{
TWin *page = (TWin*)get_data(id);
form->set_client(page);
form->size();
dispatch_ref(L,select_idx,id);
}
virtual int handle_selection_changing(int id)
{
if (selection_changing_idx) {
dispatch_ref(L,selection_changing_idx,id);
return 1;
} else {
return 0;
}
}
};
struct WinWrap {
TWin *window;
void *data;
};
static int wrap_window(lua_State* L, TWin* win)
{
WinWrap *wrp = (WinWrap*)lua_newuserdata(L,sizeof(WinWrap));
wrp->window = win;
wrp->data = NULL;
luaL_getmetatable(L,WINDOW_CLASS);
lua_setmetatable(L,-2);
return 1;
}
static void throw_error(lua_State* L, const wchar_t *msg)
{
lua_pushstring(L,UTF8FromString(msg));
lua_error(L);
}
static TWin* window_arg(lua_State* L, int idx = 1)
{
WinWrap *wrp = (WinWrap*)lua_touserdata(L,idx);
if (! wrp) throw_error(L,L"not a window");
return (PaletteWindow*)wrp->window;
}
static void *& window_data(lua_State* L, int idx = 1)
{
WinWrap *wrp = (WinWrap*)lua_touserdata(L,idx);
if (! wrp) throw_error(L,L"not a window");
return wrp->data;
}
static wchar_t** table_to_str_array(lua_State *L, int idx, int* psz = NULL)
{
if (! lua_istable(L,idx)) {
throw_error(L,L"table argument expected");
}
wchar_t** p = new wchar_t*[100];
int i = 0;
lua_pushnil(L); // first key
while (lua_next(L, idx) != 0) {
/* `key' is at index -2 and `value' at index -1 */
p[i++] = _wcsdup(StringFromUTF8(lua_tostring(L,-1)));
lua_pop(L, 1); /* removes `value'; keeps `key' for next iteration */
}
p[i] = NULL; // conventional way of indicating end of string array
if (psz) *psz = i;
return p;
}
static int conv(const char* s, int i1)
{
char args[] = {s[i1],s[i1+1],'\0'};
int val;
sscanf(args,"%X",&val);
return val;
}
static unsigned int convert_colour_spec(const char* clrs)
{
return RGB(conv(clrs,1),conv(clrs,3),conv(clrs,5));
}
static bool optboolean(lua_State* L, int idx, bool res)
{
if (lua_isnoneornil(L,idx)) {
return res;
} else {
return lua_toboolean(L,idx);
}
}
///// Exported Lua functions ///////
/** gui.message(message_string, is_warning)
@param message_string
@param is_warning (0 for plain message, 1 for warning box)
MSG_ERROR=2,MSG_WARNING=1, MSG_QUERY=3;
*/
int do_message(lua_State* L)
{
const wchar_t* msg = StringFromUTF8(luaL_checkstring(L,1));
const wchar_t* kind = StringFromUTF8(luaL_optstring(L,2,"message"));
int type = 0;
if (EQ(kind,L"message")) type = 0; else
if (EQ(kind,L"warning")) type = 1; else
if (EQ(kind,L"error")) type = 2; else
if (EQ(kind,L"query")) type = 3;
lua_pushboolean(L,get_parent()->message(msg,type));
return 1;
}
/** gui.prompt_value(prompt_string, default_value)
@param prompt_string
@param default_value (must be a string)
@return string value if OK, nil if Cancel.
*/
int do_prompt_value(lua_State* L)
{
const wchar_t* varname = StringFromUTF8(luaL_checkstring(L,1));
const wchar_t* value = StringFromUTF8(luaL_optstring(L,2,""));
PromptDlg dlg((TEventWindow*)get_desktop_window(),varname, value);
if (dlg.show_modal()) {
lua_pushstring(L,UTF8FromString(dlg.m_val));
} else {
lua_pushnil(L);
}
return 1;
}
int do_run(lua_State* L)
{
const wchar_t* lpFile = StringFromUTF8(luaL_checkstring(L,1));
const wchar_t* lpParameters = StringFromUTF8(lua_tostring(L,2));
const wchar_t* lpDirectory = StringFromUTF8(lua_tostring(L,3));
int res = (int)ShellExecute (
NULL,
L"open",
lpFile,
lpParameters,
lpDirectory,
SW_SHOWDEFAULT
);
if (res <= 32) {
lua_pushboolean(L,0);
lua_pushinteger(L,res);
return 2;
} else {
lua_pushinteger(L,res);
return 1;
}
}
/** gui.colour_dlg(default_colour)
@param default_colour colour either in form '#RRGGBB" or as a 32-bit integer
@return chosen colour, in same form as default_colour
*/
int do_colour_dlg(lua_State* L)
{
bool in_rgb = lua_isstring(L,1);
unsigned int cval;
if (in_rgb) {
cval = convert_colour_spec(lua_tostring(L,1));
} else {
cval = luaL_optinteger(L,1,0);
}
TColourDialog dlg(get_parent(),cval);
if (dlg.go()) {
cval = dlg.result();
if (in_rgb) {
char buff[12];
sprintf(buff,"#%02X%02X%02X",GetRValue(cval),GetGValue(cval),GetBValue(cval));
lua_pushstring(L,buff);
} else {
lua_pushnumber(L,cval);
}
} else {
lua_pushnil(L);
}
return 1;
}
/** gui.open_dlg(caption,filter)
@param caption (defaults to "Open File")
@param filter (defaults to "All (*.*)|*.*")
@return chosen filename
*/
int do_open_dlg(lua_State* L)
{
const wchar_t* caption = StringFromUTF8(luaL_optstring(L,1,"Open File"));
const wchar_t* filter = StringFromUTF8(luaL_optstring(L,2,"All (*.*)|*.*"));
TOpenFile tof (get_parent(),caption,filter);
if (tof.go()) {
lua_pushstring(L,UTF8FromString(tof.file_name()));
} else {
lua_pushnil(L);
}
return 1;
}
int do_save_dlg(lua_State* L)
{
const wchar_t* caption = StringFromUTF8(luaL_optstring(L,1,"Save File"));
const wchar_t* filter = StringFromUTF8(luaL_optstring(L,2,"All (*.*)|*.*"));
TSaveFile tof (get_parent(),caption,filter);
if (tof.go()) {
lua_pushstring(L,UTF8FromString(tof.file_name()));
} else {
lua_pushnil(L);
}
return 1;
}
/** gui.select_dir_dlg(description,initialdir)
@param description (defaults to empty string)
@return chosen directory
*/
int do_select_dir_dlg(lua_State* L)
{
const wchar_t* descr = StringFromUTF8(luaL_optstring(L,1,""));
const wchar_t* initdir = StringFromUTF8(luaL_optstring(L,2,""));
TSelectDir dir(get_parent(), descr, initdir);
if (dir.go()) {
lua_pushstring(L, UTF8FromString(dir.path()));
} else {
lua_pushnil(L);
}
return 1;
}
int new_toolbar(lua_State* L)
{
const wchar_t* caption = StringFromUTF8(luaL_checkstring(L,1));
wchar_t** items = table_to_str_array(L,2);
int sz = luaL_optinteger(L,3,16);
const wchar_t* path = StringFromUTF8(lua_tostring(L,4));
ToolbarWindow* ew = new ToolbarWindow(caption,items,sz,path,L);
ew->show();
delete[] items;
return wrap_window(L,ew);
}
int new_window(lua_State* L)
{
const wchar_t* caption = StringFromUTF8(luaL_checkstring(L,1));
ContainerWindow* cw = new ContainerWindow(caption,L);
s_last_parent = cw;
return wrap_window(L,cw);
}
int new_panel(lua_State* L)
{
PanelWindow* pw = new PanelWindow(L);
pw->align(alLeft,luaL_checkinteger(L,1));
s_last_parent = pw;
return wrap_window(L,pw);
}
int window_client(lua_State* L)
{
TEventWindow *cw = (TEventWindow*)window_arg(L,1);
TWin* child = window_arg(L,2);
if (! child) throw_error(L,L"must provide a child window");
child->set_parent(cw);
cw->set_client(child);
return 0;
}
int window_add(lua_State* L)
{
TEventWindow *cw = (TEventWindow*)window_arg(L,1);
TWin* child = window_arg(L,2);
const wchar_t *align = StringFromUTF8(luaL_optstring(L,3,"client"));
bool splitter = optboolean(L,5,true);
int sz = luaL_optinteger(L,4,100);
child->set_parent(cw);
if (EQ(align,L"top")) {
child->align(alTop,sz);
} else
if (EQ(align,L"bottom")) {
child->align(alBottom,sz);
} else
if (EQ(align,L"left")) {
child->align(alLeft,sz);
} else
if (EQ(align,L"right")) {
child->align(alRight,sz);
} else {
child->align(alClient,sz);
}
cw->add(child);
if (splitter && child->align() != alClient) {
TSplitter *split = new TSplitter(cw,child);
cw->add(split);
window_data(L,2) = split;
}
return 0;
}
int window_remove(lua_State* L)
{
TEventWindow* form = (TEventWindow*)window_arg(L,1);
form->remove(window_arg(L,2));
TWin *split = (TWin*)window_data(L,2);
if (split) {
form->remove(split);
}
return 0;
}
int window_context_menu(lua_State* L)
{
TWin* w = window_arg(L,1);
if(LuaWindow *cw = dynamic_cast<LuaWindow*> (w)) {
wchar_t** items = table_to_str_array(L,2);
ContextMenu mnu(cw);
for (;*items; items++) {
wchar_t* text = wcstok(*items,L"|");
wchar_t* fun = wcstok(NULL,L"");
if ( ( text == 0 || *text == 0 )
&& ( fun == 0 || *fun == 0 ) )
{
mnu.add_separator();
}
else
{
mnu << Item(text,(EH)&LuaWindow::handler,fun);
}
}
} else if(TListViewLua* lv = dynamic_cast<TListViewLua*>(w)) {
TEventWindow* parent = dynamic_cast<TEventWindow*>(lv->get_parent_win());
wchar_t** items = table_to_str_array(L,2);
HMENU hm = CreatePopupMenu();
Popup mnu(hm);
for (;*items; items++) {
wchar_t* text = wcstok(*items,L"|");
wchar_t* fun = wcstok(NULL,L"");
if ( ( text == 0 || *text == 0 )
&& ( fun == 0 || *fun == 0 ) )
{
mnu.add_separator();
}
else
{
mnu << Item(text,(EH)&LuaWindow::handler,fun);
}
}
lv->set_popup_menu(hm);
mnu.get_menu_handler()->set_form(parent);
parent->add_handler(mnu.get_menu_handler());
}
return 0;
}
int new_tabbar(lua_State* L)
{
TEventWindow* form = (TEventWindow*)window_arg(L,1);
TTabControlLua *tab = new TTabControlLua(form,L);
s_last_parent = form;
tab->align(alTop);
form->add(tab);
return wrap_window(L,tab);
}
int tabbar_add(lua_State* L)
{
TTabControl *tab = (TTabControl*)window_arg(L,1);
tab->add(StringFromUTF8(luaL_checkstring(L,2)),window_arg(L,3));
return 0;
}
int new_memo(lua_State* L)
{
TMemo *m = new TMemo(get_last_parent(),1);
return wrap_window(L,m);
}
int memo_set(lua_State* L)
{
TMemo *m = (TMemo*)window_arg(L,1);
m->set_text(StringFromUTF8(luaL_checkstring(L,2)));
return 0;
}
int memo_set_colour(lua_State* L)
{
TMemo *m = (TMemo*)window_arg(L,1);
m->set_text_colour(convert_colour_spec(luaL_checkstring(L,2))); // Must be only ASCII
m->set_background_colour(convert_colour_spec(luaL_checkstring(L,3)));
return 0;
}
/** lw:list(multiple_columns,multiple_selection)
@param multiple_columns (default false)
@param single_selection (default true)
@return new ListWindow instance.
*/
int new_list_window(lua_State* L)
{
bool multiple_columns = optboolean(L,1,false);
bool single_select = optboolean(L,2,true);
TListViewLua *lv = new TListViewLua(get_last_parent(),L,multiple_columns,single_select);
return wrap_window(L,lv);
}
/** w:show()
@param self
*/
int window_show(lua_State* L)
{
window_arg(L)->show();
return 0;
}
/** w:hide()
@param self
*/
int window_hide(lua_State* L)
{
window_arg(L)->hide();
return 0;
}
/** w:size()
@param self
@param width
@param height
*/
int window_size(lua_State* L)
{
int w = luaL_checkinteger(L,2);
int h = luaL_checkinteger(L,3);
window_arg(L)->resize(w,h);
return 0;
}
/** w:size()
@param self
@param x
@param y
*/
int window_position(lua_State* L)
{
int x = luaL_checkinteger(L,2);
int y = luaL_checkinteger(L,3);
window_arg(L)->move(x,y);
return 0;
}
int window_get_bounds(lua_State* L)
{
TWin *win = (TWin*)window_arg(L);
Rect rt;
win->get_rect(rt);
lua_pushboolean(L,win->visible());
lua_pushinteger(L,rt.left);
lua_pushinteger(L,rt.right);
lua_pushinteger(L,rt.width());
lua_pushinteger(L,rt.height());
return 5;
}
TListViewLua* list_window_arg(lua_State* L)
{
// ListWindow* lw = dynamic_cast<ListWindow*>(window_arg(L));
// if (! lw) lua_error(L);
// return lw->listv;
return (TListViewLua*)(window_arg(L));
}
/** w:add_column()
@param self
@param column_title
@param column_size
*/
int window_add_column(lua_State* L)
{
list_window_arg(L)->add_column(StringFromUTF8(luaL_checkstring(L,2)),luaL_checkinteger(L,3));
return 0;
}
void window_aux_item(lua_State* L, bool at_index)
{
TListViewLua* lv = list_window_arg(L);
int next_arg,ipos;