forked from wtnbgo/win32dialog
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
3108 lines (2865 loc) · 90.3 KB
/
main.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
#ifdef _MSC_VER
#define ISOLATION_AWARE_ENABLED 1
#pragma comment(lib, "comctl32.lib")
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#include "ncbind.hpp"
#include <commctrl.h>
// CreateDialogIndirect のテーブル書き出し用クラス
#include "dialog.hpp"
#include "simplethread.hpp"
// MessageBoxでダイアログをオーナーセンター表示にする独自フラグ
#define MB_OWNER_CENTER 0x40000000L
struct Header;
struct Items;
class Progress;
// BITMAPラッパ
struct Bitmap {
typedef unsigned char PIX;
iTJSDispatch2 *lay;
HBITMAP bitmap;
HICON icon;
Bitmap(iTJSDispatch2 *layer) : lay(layer), bitmap(0), icon(0) { lay->AddRef(); }
~Bitmap() { removeBitmap(); lay->Release(); }
void removeBitmap() {
if (bitmap) ::DeleteObject(bitmap);
bitmap = NULL;
if (icon) ::DestroyIcon(icon);
icon = NULL;
}
HBITMAP createBitmap(HWND hwnd, tjs_int *pBmpW=0, tjs_int *pBmpH=0, bool withAlpha=false) {
removeBitmap();
ncbPropAccessor obj(lay);
tjs_int bmpw = obj.getIntValue(TJS_W("imageWidth"));
tjs_int bmph = obj.getIntValue(TJS_W("imageHeight"));
tjs_int ln = obj.getIntValue(TJS_W("mainImageBufferPitch"));
PIX *pw, *pr = reinterpret_cast<PIX*>(obj.getIntValue(TJS_W("mainImageBuffer")));
BITMAPV5HEADER bh;
ZeroMemory(&bh, sizeof(bh));
bh.bV5Size = sizeof(bh);
bh.bV5Width = bmpw;
bh.bV5Height = bmph;
bh.bV5Planes = 1;
int byte = 3;
if (withAlpha) {
byte = 4;
bh.bV5Compression = BI_BITFIELDS;
bh.bV5RedMask = 0x00FF0000;
bh.bV5GreenMask = 0x0000FF00;
bh.bV5BlueMask = 0x000000FF;
bh.bV5AlphaMask = 0xFF000000;
}
bh.bV5BitCount = byte*8;
HDC dc = GetDC(hwnd);
if ((bitmap = CreateDIBSection(dc, (LPBITMAPINFO)&bh, DIB_RGB_COLORS, (LPVOID*)&pw, NULL, 0)) != NULL) {
for (int y = bmph-1; y >= 0; y--) {
PIX *src = pr + (y * ln);
PIX *dst = pw + ((bmph-1 - y) * ((bmpw*byte+3) & ~3L));
for (int n = bmpw-1; n >= 0; n--, src+=4, dst+=byte) {
for (int b = 0; b < byte; b++) dst[b] = src[b];
}
}
}
ReleaseDC(hwnd, dc);
if (pBmpW) *pBmpW = bmpw;
if (pBmpH) *pBmpH = bmph;
return bitmap;
}
HICON createIcon(HWND hwnd) {
tjs_int w = 0, h = 0;
if (createBitmap(hwnd, &w, &h, true)) {
ICONINFO info;
ZeroMemory(&info, sizeof(info));
info.fIcon = TRUE;
info.hbmMask = CreateBitmap(w, h, 1, 1, NULL);
info.hbmColor = bitmap;
icon = CreateIconIndirect(&info);
DeleteObject(info.hbmMask);
DeleteObject(bitmap);
bitmap = NULL;
}
return icon;
}
};
// HBRUSHラッパ
struct SolidBrush {
HBRUSH brush;
SolidBrush(DWORD rgb) : brush(NULL) {
brush = CreateSolidBrush(RGB(((rgb>>16)&0xFF), ((rgb>>8)&0xFF), (rgb&0xFF)));
}
~SolidBrush() {
if (brush) DeleteObject(brush);
brush = NULL;
}
};
// DRAWITEMアクセス用クラス
struct DrawItem {
DrawItem() : ref(0) {}
DrawItem(DRAWITEMSTRUCT const *p) : ref(p) {}
UINT GetCtlType() const { return ref->CtlType; }
UINT GetCtlID() const { return ref->CtlID; }
UINT GetItemID() const { return ref->itemID; }
UINT GetItemAction() const { return ref->itemAction; }
UINT GetItemState() const { return ref->itemState; }
DWORD GetItemData() const { return ref->itemData; }
tjs_int64 GetHWndItem() const { return (tjs_int64)ref->hwndItem; }
tTJSVariant GetItemRect() const {
ncbDictionaryAccessor dict;
if (dict.IsValid()) {
dict.SetValue(TJS_W("x"), ref->rcItem.left);
dict.SetValue(TJS_W("y"), ref->rcItem.top);
dict.SetValue(TJS_W("w"), ref->rcItem.right - ref->rcItem.left);
dict.SetValue(TJS_W("h"), ref->rcItem.bottom - ref->rcItem.top );
return tTJSVariant(dict, dict);
}
return tTJSVariant();
}
void draw(Bitmap *bmp, tjs_int x, tjs_int y) {
if (!bmp) return;
tjs_int w = 0, h = 0;
HBITMAP bitmap = bmp->createBitmap(ref->hwndItem, &w, &h);
if (bitmap) {
HDC bmpdc = ::CreateCompatibleDC(ref->hDC);
if (bmpdc) {
::SelectObject(bmpdc, bitmap);
::BitBlt(ref->hDC, x, y, w, h, bmpdc, 0, 0, SRCCOPY);
::DeleteDC(bmpdc);
}
}
}
private:
DRAWITEMSTRUCT const *ref;
};
// NMHDR アクセス用クラス
struct NotifyAccessor {
NotifyAccessor() : ptr(0), ref(0) {}
NotifyAccessor(NMHDR const *p) : ptr((BYTE const *)p), ref(p) {}
tjs_int64 GetHWndFrom() const { return (tjs_int64)(ref->hwndFrom); }
UINT GetIdFrom() const { return ref->idFrom; }
UINT GetCode() const { return ref->code; }
inline BYTE GetByte( int ofs) const { return ptr[ofs]; }
inline WORD GetWord( int ofs) const { return ( WORD)GetByte(ofs) | ((( WORD)GetByte(ofs+1))<<8); }
inline DWORD GetDWord(int ofs) const { return (DWORD)GetWord(ofs) | (((DWORD)GetWord(ofs+2))<<16); }
private:
BYTE const *ptr;
NMHDR const* ref;
};
// 汎用バイナリオ操作ブジェクト
struct Blob {
Blob(DWORD size) : ptr(0), size(size) { if (size) { ptr = new BYTE[size]; ZeroMemory(ptr, size); } }
Blob(BYTE *ptr) : ptr(ptr), size(0) {}
~Blob() { if (size) delete ptr; size = 0; ptr = 0; }
tjs_int64 GetPointer() const { return (tjs_int64)(ptr); }
inline BYTE GetByte( int ofs) const { return ptr[ofs]; }
inline WORD GetWord( int ofs) const { return ( WORD)GetByte(ofs) | ((( WORD)GetByte(ofs+1))<<8); }
inline DWORD GetDWord(int ofs) const { return (DWORD)GetWord(ofs) | (((DWORD)GetWord(ofs+2))<<16); }
inline void SetByte( int ofs, BYTE v) { ptr[ofs] = v; }
inline void SetWord( int ofs, WORD v) { SetByte(ofs, (BYTE)v); SetByte(ofs+1, (BYTE)(v>>8)); }
inline void SetDWord(int ofs, DWORD v) { SetWord(ofs, (WORD)v); SetWord(ofs+2, (WORD)(v>>16)); }
ttstr GetText(int ofs) const { return (tjs_char*)GetDWord(ofs); }
void SetText(int ofs, tjs_char const *text) { SetDWord(ofs, (DWORD)text); }
static Blob* ReferPointer(DWORD ptr) { return new Blob((BYTE*)ptr); }
private:
BYTE *ptr;
DWORD size;
};
class WIN32Dialog {
public:
// 型のalias
typedef ncbPropAccessor PropT;
typedef ncbDictionaryAccessor DictT;
typedef tTJSVariant VarT;
typedef iTJSDispatch2 DspT;
typedef float ProgressValueT;
typedef DialogConfig::SizeT SizeT;
typedef DialogConfig::NameT NameT;
typedef DialogConfig::StringT StringT;
private:
typedef ncbInstanceAdaptor<WIN32Dialog> SelfAdaptorT;
typedef ncbInstanceAdaptor<Header> HeadAdaptorT;
typedef ncbInstanceAdaptor<Items> ItemAdaptorT;
typedef ncbInstanceAdaptor<Bitmap> BitmapAdaptorT;
typedef ncbInstanceAdaptor<SolidBrush> SolidBrushAdaptorT;
HWND dialogHWnd;
HICON icon;
HMODULE resource;
HPROPSHEETPAGE propsheet;
Progress *progress;
DspT *owner, *objthis;
bool modeless;
BYTE *buf;
BYTE *ref;
VarT resid, iconBitmap;
HHOOK hHook;
public:
// constructor
WIN32Dialog(DspT *_owner = 0)
: dialogHWnd(0),
icon(0),
resource(0),
propsheet(0),
progress(0),
owner(_owner),
objthis(0),
modeless(false),
buf(0),
hHook(0)
{}
// destructor
virtual ~WIN32Dialog() {
//TVPAddLog(TJS_W("# WIN32Dialog.finalize()"));
if (progress) closeProgress();
progress = 0;
if (propsheet) DestroyPropertySheetPage(propsheet);
propsheet = 0;
if (modeless) close(IDCANCEL);
if (buf) TVP_free(buf);
buf = 0;
if (resource) FreeLibrary(resource);
resource = 0;
}
BYTE* CreateBuffer(SizeT sz) {
// 領域確保
ref = buf = reinterpret_cast<BYTE*>(TVP_malloc(sz + 4));
SizeT c = 0;
DialogTemplate::Alignment(ref, c, 4);
return ref;
}
// コールバック呼び出し
DspT *_hasCallback(NameT event, tTJSVariant &rslt) {
DspT *obj = owner ? owner : objthis;
if (!obj) return NULL;
// コールバックが存在するか
tjs_uint32 hint = 0;
if (TJS_FAILED(obj->PropGet(TJS_MEMBERMUSTEXIST, event, &hint, &rslt, obj))) return NULL;
rslt.Clear();
return obj;
}
LRESULT callback(NameT event, tjs_int numparams, tTJSVariant **params) {
tTJSVariant rslt;
DspT *obj = _hasCallback(event, rslt);
if (!obj) return FALSE;
// 引数を渡してコールバックを呼ぶ
Try_iTJSDispatch2_FuncCall(obj, 0, event, 0, &rslt, numparams, params, obj);
return (rslt.AsInteger() != 0) ? TRUE : FALSE;
}
// 通常コールバック
LRESULT callback(NameT event, UINT msg, WPARAM wparam, LPARAM lparam) {
tTJSVariant vmsg((tjs_int32)msg), vwp((tjs_int32)wparam), vlp((tjs_int64)lparam);
tTJSVariant *params[] = { &vmsg, &vwp, &vlp };
return callback(event, 3, params);
}
// WM_NOTIFY用コールバック
LRESULT callback(NameT event, WPARAM wparam, NMHDR const *nmhdr) {
NotifyAccessor acc(nmhdr);
tTJSVariant vwp((tjs_int32)wparam), vnm;
// ボックス化
ncbNativeObjectBoxing::Boxing box;
box.operator()<NotifyAccessor&> (vnm, acc, ncbTypedefs::Tag<NotifyAccessor&>());
tTJSVariant *params[] = { &vwp, &vnm };
return callback(event, 2, params);
}
// WM_DRAWITEM用コールバック
LRESULT callback(NameT event, WPARAM wparam, DRAWITEMSTRUCT const *drawitem) {
DrawItem diwrap(drawitem);
tTJSVariant vwp((tjs_int32)wparam), vdi;
// ボックス化
ncbNativeObjectBoxing::Boxing box;
box.operator()<DrawItem&> (vdi, diwrap, ncbTypedefs::Tag<DrawItem&>());
tTJSVariant *params[] = { &vwp, &vdi };
return callback(event, 2, params);
}
// WM_CTLCOLOR*用コールバック
LRESULT callback(NameT event, int id, HDC hdc) {
tTJSVariant rslt;
DspT *obj = _hasCallback(event, rslt);
if (!obj) return FALSE;
tTJSVariant vid(id);
tTJSVariant *params[] = { &vid };
Try_iTJSDispatch2_FuncCall(obj, 0, event, 0, &rslt, 1, params, obj);
if (rslt.Type() != tvtObject) return FALSE;
LRESULT r = FALSE;
ncbPropAccessor elm(rslt);
if (elm.IsValid()) {
tTJSVariantType type = tvtVoid;
if (elm.HasValue(TJS_W("fgcolor"), 0, &type) && type == tvtInteger) {
::SetTextColor(hdc, ConvertRGBtoColorRef(elm.getIntValue(TJS_W("fgcolor"))));
}
if (elm.HasValue(TJS_W("bgcolor"), 0, &type)) {
if (type == tvtInteger) {
::SetBkMode (hdc, OPAQUE);
::SetBkColor(hdc, ConvertRGBtoColorRef(elm.getIntValue(TJS_W("bgcolor"))));
} else if (type == tvtObject) {
::SetBkMode (hdc, TRANSPARENT);
}
}
tTJSVariant bgbrush;
if (elm.checkVariant(TJS_W("bgbrush"), bgbrush)) {
type = bgbrush.Type();
if (type == tvtObject) {
iTJSDispatch2 *obj = bgbrush.AsObjectNoAddRef();
if (obj) {
SolidBrush *brush = SolidBrushAdaptorT::GetNativeInstance(obj, true);
if (brush) r = (LRESULT)brush->brush;
}
} else if (type == tvtInteger) {
r = (LRESULT)::GetStockObject((int)bgbrush.AsInteger());
}
}
if (r == FALSE) r = (LRESULT)::GetStockObject(NULL_BRUSH);
}
return r;
}
// リソース読み込み
static tjs_error TJS_INTF_METHOD loadResource(VarT *result, tjs_int numparams, VarT **param, WIN32Dialog *self) {
if (numparams < 1) return TJS_E_BADPARAMCOUNT;
if (param[0]->Type() == tvtString) self->_loadResource(param[0]->GetString());
self->_setResource(numparams >= 2 ? param[1] : NULL);
return TJS_S_OK;
}
protected:
// DLLのリソースを読み込む
void _loadResource(NameT module) {
if (!module) return;
HMODULE oldres = resource;
resource = LoadLibrary(module);
if (oldres) FreeLibrary(oldres);
if (!resource) ThrowLastError();
}
void _setResource(VarT *id) {
resid.Clear();
if (id) resid = *id;
}
LPCWSTR getResourceName() const {
if (resource) {
tTJSVariantType type = resid.Type();
if (type != tvtVoid)
return (type == tvtString) ? (LPCWSTR)resid.GetString() : (LPCWSTR)MAKEINTRESOURCE(resid.AsInteger());
}
return 0;
}
public:
// テンプレート作成
static tjs_error TJS_INTF_METHOD makeTemplate(VarT *result, tjs_int numparams, VarT **param, WIN32Dialog *self);
bool IsValid() const { return dialogHWnd != 0; }
void checkDialogValid() const {
if (!IsValid()) TVPThrowExceptionMessage(TJS_W("Dialog not opened."));
}
// -------------------------------------------------------------
// 各種 Item 操作関数
HWND GetItemHWND(int id) const {
checkDialogValid();
if (!id) return dialogHWnd; // [XXX] 自分自身を操作するためにid==0でdialogHWndを返す
HWND ret = GetDlgItem(dialogHWnd, id);
if (!ret) ThrowLastError();
return ret;
}
int GetDlgCtrlItem(HWND hwnd) const {
checkDialogValid();
int ret = GetDlgCtrlID(hwnd);
if (!ret) ThrowLastError();
return ret;
}
VarT GetItemClassName(int id) const {
HWND hwnd = GetItemHWND(id);
if(hwnd)
{
VarT var;
tjs_char buf[256];
int ret = GetClassName(hwnd, buf, sizeof(buf));
if(!ret) ThrowLastError();
else var = VarT(buf);
return var;
}
return VarT();
}
// for tjs
tjs_int64 GetItem(int id) const { return (tjs_int64)GetItemHWND(id); }
int GetItemID(tjs_int64 hwnd) const { return GetDlgCtrlItem((HWND)hwnd); }
bool IsExistentItem(int id) const { return GetDlgItem(dialogHWnd, id) != NULL; }
long GetItemLong(int id, int index) { return GetWindowLong(GetItemHWND(id), index); }
long SetItemLong(int id, int index, long newlong) {
HWND hwnd = GetItemHWND(id);
long r = SetWindowLong(hwnd, index, newlong);
// スタイル変更の反映
if (!id && (index == GWL_STYLE || GWL_EXSTYLE))
SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
return r;
}
bool LockItemUpdate(int id) const { return !!::LockWindowUpdate(GetItemHWND(id)); }
bool UnlockItemUpdate() const { return !!::LockWindowUpdate(NULL); }
int GetItemInt(int id) const {
checkDialogValid();
BOOL success = FALSE;
int ret = (int)GetDlgItemInt(dialogHWnd, id, &success, TRUE);
if (!success) ThrowLastError();
return ret;
}
void SetItemInt(int id, int value) {
checkDialogValid();
if (!SetDlgItemInt(dialogHWnd, id, (UINT)value, TRUE)) ThrowLastError();
}
StringT GetItemText(int id) const {
checkDialogValid();
SizeT len = GetWindowTextLength(GetItemHWND(id));
tjs_char *p = new tjs_char[len+1];
if(id) GetDlgItemTextW(dialogHWnd, id, p, len+1);
else GetWindowTextW (dialogHWnd, p, len+1);
StringT ret = p;
delete[] p;
return ret;
}
void SetItemText(int id, NameT string) {
checkDialogValid();
BOOL success = id ? SetDlgItemTextW(dialogHWnd, id, string)
: SetWindowTextW (dialogHWnd, string);
if (!success) ThrowLastError();
}
void SetItemEnabled(int id, bool en) {
EnableWindow(GetItemHWND(id), en ? TRUE : FALSE);
}
bool GetItemEnabled(int id) {
return IsWindowEnabled(GetItemHWND(id)) != 0;
}
void SetItemFocus(int id) {
SetFocus(GetItemHWND(id));
}
#define GetItemSize(id, rect, result) \
checkDialogValid(); \
RECT rect; \
::GetWindowRect(GetItemHWND(id), &(rect)); \
return (result)
long GetItemLeft(int id) const { GetItemSize(id, r, r.left); }
long GetItemTop(int id) const { GetItemSize(id, r, r.top); }
long GetItemWidth(int id) const { GetItemSize(id, r, r.right - r.left); }
long GetItemHeight(int id) const { GetItemSize(id, r, r.bottom - r.top); }
void SetItemPos(int id, int x, int y) {
SetWindowPos(GetItemHWND(id), 0, x, y, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
}
void SetItemSize(int id, int w, int h) {
SetWindowPos(GetItemHWND(id), 0, 0, 0, w, h, SWP_NOMOVE|SWP_NOZORDER);
}
static tjs_error TJS_INTF_METHOD SetItemBitmap(VarT *result, tjs_int numparams, VarT **param, WIN32Dialog *self) {
if (numparams < 2) return TJS_E_BADPARAMCOUNT;
int id = (int)param[0]->AsInteger();
Bitmap *bmp = BitmapAdaptorT::GetNativeInstance(param[1]->AsObjectNoAddRef(), true);
if (bmp != NULL)
*result = self->_sendItemMessage(id, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)bmp->createBitmap(self->dialogHWnd));
return TJS_S_OK;
}
void SetPos(int x, int y) { if (dialogHWnd) SetWindowPos(dialogHWnd, 0, x, y, 0, 0, SWP_NOSIZE|SWP_NOZORDER); }
void SetSize(int w, int h) { if (dialogHWnd) SetWindowPos(dialogHWnd, 0, 0, 0, w, h, SWP_NOMOVE|SWP_NOZORDER); }
#define GetDialogSize(rect, result) \
if (!dialogHWnd) return 0; \
RECT rect; \
::GetWindowRect(dialogHWnd, &(rect)); \
return (result)
long GetLeft () const { GetDialogSize(r, r.left); }
long GetTop () const { GetDialogSize(r, r.top); }
long GetWidth () const { GetDialogSize(r, r.right - r.left); }
long GetHeight() const { GetDialogSize(r, r.bottom - r.top); }
VarT GetWindowRect() const {
VarT r;
if (dialogHWnd) {
RECT rect;
::GetWindowRect(dialogHWnd, &rect);
DictT to;
_setRect(rect, to);
r = to;
}
return r;
}
VarT GetClientRect() const {
VarT r;
if (dialogHWnd) {
RECT rect;
::GetClientRect(dialogHWnd, &rect);
DictT to;
_setRect(rect, to);
r = to;
}
return r;
}
static tjs_error TJS_INTF_METHOD sendItemMessage(VarT *result, tjs_int numparams, VarT **param, WIN32Dialog *self) {
if (numparams < 2) return TJS_E_BADPARAMCOUNT;
int id = (int)param[0]->AsInteger();
UINT msg = (UINT)param[1]->AsInteger();
WPARAM wp = (numparams > 2) ? (WPARAM)param[2]->AsInteger() : 0;
LPARAM lp = (numparams > 3) ? ((param[3]->Type() == tvtString) ? (LPARAM)param[3]->GetString() : (LPARAM)param[3]->AsInteger()) : 0;
*result = self->_sendItemMessage(id, msg, wp, lp);
return TJS_S_OK;
}
protected:
LRESULT _sendItemMessage(int id, UINT msg, WPARAM wparam, LPARAM lparam) {
checkDialogValid();
return id ? SendDlgItemMessage(dialogHWnd, id, msg, wparam, lparam)
: SendMessage (dialogHWnd, msg, wparam, lparam); // [XXX]自分自身を操作するためにid==0でdialogHWndへメッセージ
}
// TJS辞書⇒RECTへ値をコピー
static inline bool _getRect(RECT &rect, PropT &prop) {
if (!prop.IsValid()) return false;
PropT::DefsT::Tag<LONG> tagInt;
rect.left = prop.GetValue(TJS_W("left"), tagInt);
rect.top = prop.GetValue(TJS_W("top"), tagInt);
rect.right = prop.GetValue(TJS_W("right"), tagInt);
rect.bottom = prop.GetValue(TJS_W("bottom"), tagInt);
return true;
}
// RECT⇒TJS辞書へ値をコピー
static inline bool _setRect(RECT &rect, PropT &prop) {
if (!prop.IsValid()) return false;
prop.SetValue(TJS_W("left"), rect.left);
prop.SetValue(TJS_W("top"), rect.top);
prop.SetValue(TJS_W("right"), rect.right);
prop.SetValue(TJS_W("bottom"), rect.bottom);
return true;
}
public:
VarT GetBaseUnits() const {
VarT var;
DictT dict;
if (dict.IsValid()) {
LONG units = GetDialogBaseUnits();
dict.SetValue(TJS_W("h"), LOWORD(units));
dict.SetValue(TJS_W("v"), HIWORD(units));
var = dict;
}
return var;
}
VarT MapRect(VarT in) const {
checkDialogValid();
VarT var;
PropT from(in);
DictT to;
RECT rect;
if (_getRect(rect, from) && to.IsValid()) {
if (!MapDialogRect(dialogHWnd, &rect)) ThrowLastError();
_setRect(rect, to);
var = to;
}
return var;
}
bool InvalidateAll(bool erase) {
checkDialogValid();
return !!::InvalidateRect(dialogHWnd, NULL, erase);
}
bool InvalidateRect(VarT vrect, bool erase) {
checkDialogValid();
PropT prop(vrect);
RECT rect;
return (_getRect(rect, prop)) ? !!::InvalidateRect(dialogHWnd, &rect, erase) : false;
}
// -------------------------------------------------------------
// open (tjs raw callback)
static tjs_error TJS_INTF_METHOD open(VarT *result, tjs_int numparams, VarT **param, DspT *objthis) {
WIN32Dialog *self = SelfAdaptorT::GetNativeInstance(objthis);
if (!self) return TJS_E_NATIVECLASSCRASH;
self->objthis = objthis;
*result = (tjs_int32)self->_open(numparams > 0 ? *(param[0]) : VarT());
return TJS_S_OK;
}
void close(DWORD id) {
if (propsheet || progress) return;
if (dialogHWnd) {
if (!modeless) EndDialog(dialogHWnd, id);
else DestroyWindow(dialogHWnd);
}
dialogHWnd = 0;
}
tjs_int64 getHWND() const { return (tjs_int64)dialogHWnd; }
bool getModeless() const { return modeless; }
void setModeless(bool b) {
if (IsValid()) TVPThrowExceptionMessage(TJS_W("Dialog is opened."));
modeless = b;
}
void show(int nCmdShow) {
if (!IsValid()) TVPThrowExceptionMessage(TJS_W("Dialog is not opened."));
ShowWindow(dialogHWnd, nCmdShow);
}
// -------------------------------------------------------------
// stubs
virtual bool onInit( long msg, long wp, long lp) { return false; }
virtual bool onCommand(long msg, long wp, long lp) { return false; }
virtual bool onNotify( long wp, NotifyAccessor *acc) { return false; }
virtual bool onHScroll(long msg, long wp, long lp) { return false; }
virtual bool onVScroll(long msg, long wp, long lp) { return false; }
virtual bool onSize( long msg, long wp, long lp) { return false; }
protected:
// -------------------------------------------------------------
// ダイアログを開く
int _open(VarT win) {
if (propsheet) TVPThrowExceptionMessage(TJS_W("Dialog is already opened by property sheet."));
HINSTANCE hinst = GetModuleHandle(0);
HWND hwnd = _getOpenParent(win);
int ret;
LPCWSTR resname = getResourceName();
if (!modeless) {
ret = resname ?
DialogBoxParamW(resource, resname, hwnd, (DLGPROC)DlgProc, (LPARAM)this) :
/**/DialogBoxIndirectParam(hinst, (LPCDLGTEMPLATE)ref, hwnd, (DLGPROC)DlgProc, (LPARAM)this);
} else {
dialogHWnd = resname ?
CreateDialogParam(resource, resname, hwnd, (DLGPROC)DlgProc, (LPARAM)this) :
/**/CreateDialogIndirectParam(hinst, (LPCDLGTEMPLATE)ref, hwnd, (DLGPROC)DlgProc, (LPARAM)this);
ret = dialogHWnd ? 0 : -1;
}
if (ret == -1) ThrowLastError();
return ret;
}
static HWND _getOpenParent(VarT const &win) {
return (win.Type() == tvtObject) ? _getOpenParent(win.AsObjectNoAddRef()) : NULL;
}
static HWND _getOpenParent(iTJSDispatch2 *obj) {
if (obj) {
VarT val;
obj->PropGet(0, TJS_W("HWND"), NULL, &val, obj);
return (HWND)((tjs_int64)(val));
}
return TVPGetApplicationWindowHandle();
}
// GetLastErrorのエラーメッセージを取得して投げる
static inline void ThrowLastError() {
LPVOID lpMessageBuffer;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lpMessageBuffer, 0, NULL);
ttstr tmp((tjs_char*)lpMessageBuffer);
LocalFree(lpMessageBuffer);
TVPThrowExceptionMessage(tmp.c_str());
}
public:
// ダイアログプロシージャ
static LRESULT CALLBACK DlgProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
WIN32Dialog *inst;
switch (msg) {
case WM_INITDIALOG:
SetWindowLong(hwnd, DWL_USER, (LONG)lparam);
inst = (WIN32Dialog *)lparam;
if (inst) {
inst->dialogHWnd = hwnd;
if (inst->icon && !inst->propsheet) SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)inst->icon);
// モードレスダイアログ用フック
// http://support.microsoft.com/kb/233263/ja に基づいて作成
if(inst->modeless)
inst->hHook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, NULL, GetCurrentThreadId());
return inst->callback(TJS_W("onInit"), msg, wparam, lparam);
}
break;
case WM_SIZE: return NormalCallback(TJS_W("onSize" ), hwnd, msg, wparam, lparam);
case WM_HSCROLL: return NormalCallback(TJS_W("onHScroll"), hwnd, msg, wparam, lparam);
case WM_VSCROLL: return NormalCallback(TJS_W("onVScroll"), hwnd, msg, wparam, lparam);
case WM_COMMAND: return NormalCallback(TJS_W("onCommand"), hwnd, msg, wparam, lparam);
case WM_DRAWITEM:
if ((inst = (WIN32Dialog *)GetWindowLong(hwnd, DWL_USER)) != 0)
return inst->callback(TJS_W("onDrawItem"), wparam, (DRAWITEMSTRUCT*)lparam);
break;
case WM_NOTIFY:
if ((inst = (WIN32Dialog *)GetWindowLong(hwnd, DWL_USER)) != 0)
return inst->callback(TJS_W("onNotify"), wparam, (NMHDR*)lparam);
break;
case WM_DESTROY:
if ((inst = (WIN32Dialog *)GetWindowLong(hwnd, DWL_USER)) != 0 && inst->hHook != NULL)
UnhookWindowsHookEx(inst->hHook);
return FALSE;
case WM_CTLCOLORBTN:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORSTATIC:
// case WM_CTLCOLORLISTBOX:
// case WM_CTLCOLORSCROLLBAR:
if ((inst = (WIN32Dialog *)GetWindowLong(hwnd, DWL_USER)) != 0) {
int id = GetDlgCtrlID((HWND)lparam);
tjs_char *event = NULL;
switch (msg) {
case WM_CTLCOLORBTN: event = TJS_W("onCtrlColorButton" ); break;
case WM_CTLCOLOREDIT: event = TJS_W("onCtrlColorEdit" ); break;
case WM_CTLCOLORSTATIC: event = TJS_W("onCtrlColorStatic" ); break;
// case WM_CTLCOLORLISTBOX: event = TJS_W("onCtrlColorListBox" ); break;
// case WM_CTLCOLORSCROLLBAR: event = TJS_W("onCtrlColorScrollBar"); break;
}
if (event) return inst->callback(event, id, (HDC)wparam);
}
return FALSE;
}
return FALSE;
}
static LRESULT NormalCallback(NameT cbn, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
WIN32Dialog *inst = (WIN32Dialog *)GetWindowLong(hwnd, DWL_USER);
return (inst != 0) ? inst->callback(cbn, msg, wparam, lparam) : FALSE;
}
static LRESULT FAR PASCAL GetMsgProc(int ncode, WPARAM wparam, LPARAM lparam) {
WIN32Dialog *inst;
LPMSG lpmsg = (LPMSG)lparam;
if (ncode >= 0 && PM_REMOVE == wparam) {
if ((lpmsg->message >= WM_KEYFIRST && lpmsg->message <= WM_KEYLAST)) {
HWND parentHWnd = GetParent(lpmsg->hwnd);
if ((inst = (WIN32Dialog *)GetWindowLong(parentHWnd, DWL_USER)) != 0) {
if(inst->modeless) {
if (IsDialogMessage(parentHWnd, lpmsg)) {
lpmsg->message = WM_NULL;
lpmsg->lParam = 0;
lpmsg->wParam = 0;
}
}
}
}
}
if ((inst = (WIN32Dialog *)GetWindowLong(GetParent(lpmsg->hwnd), DWL_USER)) != 0)
return CallNextHookEx(inst->hHook, ncode, wparam, lparam);
return 0;
}
// -------------------------------------------------------------
// プロパティシート用
bool isPropertySheet() const { return propsheet != 0; }
HPROPSHEETPAGE setupPropSheet(iTJSDispatch2 *objthis, LPCWSTR titleov = 0) {
this->objthis = objthis;
if (!propsheet) {
PROPSHEETPAGE page;
ZeroMemory(&page, sizeof(page));
page.dwSize = sizeof(page);
page.dwFlags = PSP_DEFAULT;
page.pszTemplate = getResourceName();
if (page.pszTemplate) {
page.hInstance = (HINSTANCE)resource;
} else {
page.dwFlags |= PSP_DLGINDIRECT;
page.hInstance = GetModuleHandle(0);
page.pResource = (LPCDLGTEMPLATE)ref;
}
if (titleov) {
page.dwFlags |= PSP_USETITLE;
page.pszTitle = titleov;
}
if (icon) {
page.dwFlags |= PSP_USEHICON;
page.hIcon = icon;
}
page.pfnDlgProc = (DLGPROC)PropSheetProc;
page.lParam = (LPARAM)this;
propsheet = CreatePropertySheetPage(&page);
if (!propsheet) ThrowLastError();
}
return propsheet;
}
static LRESULT CALLBACK PropSheetProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
if (msg == WM_INITDIALOG) lparam = ((LPPROPSHEETPAGE)lparam)->lParam;
return DlgProc(hwnd, msg, wparam, lparam);
}
LRESULT propSheetMessage(int msg, VarT wp, VarT lp) {
if (IsValid() && propsheet) {
HWND owner = GetParent(dialogHWnd);
switch (msg) {
case PSM_SETTITLE:
case PSM_SETFINISHTEXT:
#ifdef PSM_SETHEADERTITLE
case PSM_SETHEADERTITLE:
case PSM_SETHEADERSUBTITLE:
#endif
#ifdef PSM_SETNEXTTEXT
case PSM_SETNEXTTEXT:
case PSM_SETBUTTONTEXT:
#endif
return SendMessage(owner, msg, (WPARAM)wp.AsInteger(), (LPARAM)lp.GetString());
case PSM_CHANGED:
case PSM_UNCHANGED:
#ifdef PSM_HWNDTOINDEX
case PSM_HWNDTOINDEX:
#endif
{
HWND hwnd = (HWND)wp.AsInteger();
if (!hwnd) hwnd = dialogHWnd;
return SendMessage(owner, msg, (WPARAM)hwnd, (LPARAM)lp.AsInteger());
}
default:
return SendMessage(owner, msg, (WPARAM)wp.AsInteger(), (LPARAM)lp.AsInteger());
}
}
return -1;
}
void setMessageResult(LONG result) {
if (IsValid()) SetWindowLong(dialogHWnd, DWL_MSGRESULT, result);
}
static tjs_int64 OpenPropertySheet(VarT win, VarT vpages, VarT velm) {
tjs_int pcnt = 0;
PropT pages(vpages), elm(velm);
if (!pages.IsValid() || (pcnt = pages.GetArrayCount()) <= 0)
TVPThrowExceptionMessage(TJS_W("invalid property sheet pages."));
PROPSHEETHEADER head;
ZeroMemory(&head, sizeof(head));
head.dwSize = sizeof(head);
head.dwFlags = PSH_DEFAULT;
head.hInstance = GetModuleHandle(0);
head.hwndParent = _getOpenParent(win);
tjs_int64 ret = -1;
ttstr caption;
HPROPSHEETPAGE *hpages = 0;
WIN32Dialog **sheets = 0;
try {
ncbTypedefs::Tag<WIN32Dialog*> tagDlg;
ncbTypedefs::Tag<iTJSDispatch2*> tagObj;
hpages = new HPROPSHEETPAGE[pcnt];
sheets = new WIN32Dialog*[pcnt];
for (tjs_int i = 0; i < pcnt; i++) {
WIN32Dialog *sheet = sheets[i] = pages.GetValue(i, tagDlg);
if (sheet) {
UINT cnt = head.nPages++;
hpages[cnt] = sheet->setupPropSheet(pages.GetValue(i, tagObj));
}
}
head.phpage = hpages;
if (elm.IsValid()) {
if (elm.HasValue(TJS_W("caption"))) {
caption = elm.getStrValue(TJS_W("caption"));
head.pszCaption = caption.c_str();
head.dwFlags |= PSH_PROPTITLE;
}
if (elm.HasValue(TJS_W("page"))) {
tjs_int page = elm.getIntValue(TJS_W("page"));
if (page >= 0 && (UINT)page < head.nPages) head.nStartPage = page;
}
if (elm.HasValue(TJS_W("icon"))) {
VarT bmp = elm.GetValue(TJS_W("icon"), ncbTypedefs::Tag<VarT>());
HICON icon = getIconHandle(head.hwndParent, bmp);
if (icon) {
head.hIcon = icon;
head.dwFlags |= PSH_USEHICON;
}
}
}
ret = PropertySheet(&head);
// 後始末
for (tjs_int i = 0; i < pcnt; i++) {
if (sheets[i]) sheets[i]->propsheet = 0;
}
} catch (...) {
delete[] hpages;
delete[] sheets;
throw;
}
delete[] hpages;
delete[] sheets;
return ret;
}
// -------------------------------------------------------------
// タブコントロール用
// タブ挿入
tjs_int64 InsertTabItem(int tabid, VarT pos, VarT title) {
// タブ挿入
if(title.Type() != tvtString) return TJS_E_INVALIDPARAM;
ttstr str = title.AsStringNoAddRef();
LPWSTR tx = (LPWSTR)str.c_str();
TC_ITEM tc;
tc.mask = TCIF_TEXT;
tc.pszText = tx;
tjs_int ins = pos.Type() == tvtVoid ? 0 : (tjs_int)pos.AsInteger();
HWND htab= GetItemHWND(tabid);
TabCtrl_InsertItem(htab, ins, &tc);
return TJS_S_OK;
}
// タブ削除
tjs_int64 DeleteTabItem(int tabid, VarT pos) {
HWND htab = GetItemHWND(tabid);
TabCtrl_DeleteItem(htab, pos.AsInteger());
return TJS_S_OK;
}
// すべてのタブを削除
tjs_int64 DeleteAllTabItem(int tabid) {
HWND htab = GetItemHWND(tabid);
TabCtrl_DeleteAllItems(htab);
return TJS_S_OK;
}
// 選択されているタブ
tjs_int GetCurSelTab(int tabid) {
HWND htab = GetItemHWND(tabid);
return TabCtrl_GetCurSel(htab);
}
// タブを選択
tjs_int SetCurSelTab(int tabid, VarT pos) {
HWND htab = GetItemHWND(tabid);
int idx = pos.Type() == tvtVoid ? 0 : (int)pos.AsInteger();
return TabCtrl_SetCurSel(htab, idx);
}
// タブ内にダイアログを表示する
tjs_int64 SelectTab(int tabid, VarT dlg) {
// 取得
HWND htab = GetItemHWND(tabid);
DspT* obj = dlg.AsObjectNoAddRef();
WIN32Dialog* child = SelfAdaptorT::GetNativeInstance(obj);
if(!child) return TJS_E_NATIVECLASSCRASH;
child->checkDialogValid();
// 親ウィンドウの変更
HWND hchild = (HWND)child->getHWND();
if(GetParent(hchild) != dialogHWnd) SetParent(hchild, dialogHWnd);
// 切替
RECT rect;
::GetClientRect(htab, &rect);
TabCtrl_AdjustRect(htab, false, &rect);
MapWindowPoints(htab, dialogHWnd, (LPPOINT)&rect, 2);
MoveWindow(hchild, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, false);
ShowWindow(hchild, SW_SHOW);
return TJS_S_OK;
}
// -------------------------------------------------------------
// プログレスダイアログ用インターフェース
ProgressValueT getProgressValue() const;
void setProgressValue(ProgressValueT);
bool getProgressCanceled() const;
void setProgressCanceled(bool);
static tjs_error TJS_INTF_METHOD openProgress(VarT *result, tjs_int num, VarT **param, iTJSDispatch2 *objthis);
bool _openProgress(iTJSDispatch2*, VarT const&, int, bool, bool);
void closeProgress();
bool isProgress() const { return progress != 0; }
void checkProgress() const { if (!isProgress()) TVPThrowExceptionMessage(TJS_W("dialog is not progress mode.")); }
// -------------------------------------------------------------
// スクロールバー用インターフェース