-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
622 lines (556 loc) · 16.3 KB
/
Main.cpp
File metadata and controls
622 lines (556 loc) · 16.3 KB
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
#include <windows.h>
#include <tchar.h>
#include <process.h>
#include <commctrl.h>
#include <vector>
#include "ncbind.hpp"
#define CLASSNAME _T("WindowExProgress")
#define KRKRDISPWINDOWCLASS _T("TScrollBox")
#ifndef ID_CANCEL
#define ID_CANCEL 3
#endif
/**
* 表示画像情報
*/
class ImageInfo {
public:
ImageInfo() : bitmap(0), bmpdc(0), color(0), left(0), top(0), width(0), height(0) {
}
~ImageInfo() {
removeBitmap();
}
// 描画処理
void show(HDC dc, PAINTSTRUCT &ps) {
if (bitmap) {
// ビットマップ描画
::BitBlt(dc, left, top, width, height, bmpdc, 0, 0, SRCCOPY);
} else {
// 矩形描画
if (width > 0 && height > 0) {
SelectObject(dc, CreateSolidBrush(color));
::Rectangle(dc, left, top, width, height);
DeleteObject(SelectObject(dc, GetStockObject(WHITE_BRUSH)));
}
}
}
/**
* ビットマップを設定
*/
void setColor(int left, int top, int width, int height, int color) {
removeBitmap();
this->left = left;
this->top = top;
this->width = width;
this->height = height;
this->color = color;
}
/**
* ビットマップを設定
*/
bool setBitmap(int left, int top, iTJSDispatch2 *lay) {
typedef unsigned char PIX;
if (!lay || !lay->IsInstanceOf(0, 0, 0, TJS_W("Layer"), lay)) return false;
this->left = left;
this->top = top;
ncbPropAccessor obj(lay);
width = obj.getIntValue(TJS_W("imageWidth"));
height = obj.getIntValue(TJS_W("imageHeight"));
tjs_int ln = obj.getIntValue(TJS_W("mainImageBufferPitch"));
PIX *pw, *pr = reinterpret_cast<unsigned char *>(obj.getIntPtrValue(TJS_W("mainImageBuffer")));
BITMAPINFO info;
ZeroMemory(&info, sizeof(info));
info.bmiHeader.biSize = sizeof(BITMAPINFO);
info.bmiHeader.biWidth = width;
info.bmiHeader.biHeight = height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 24;
removeBitmap();
bmpdc = ::CreateCompatibleDC(NULL);
bitmap = ::CreateDIBSection(bmpdc, (LPBITMAPINFO)&info, DIB_RGB_COLORS, (LPVOID*)&pw, NULL, 0);
if (!bitmap || !bmpdc) return false;
for (int y = height-1; y >= 0; y--) {
PIX *src = pr + (y * ln);
PIX *dst = pw + ((height-1 - y) * ((width*3+3) & ~3L));
for (int n = width-1; n >= 0; n--, src+=4, dst+=3) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
}
}
::SelectObject(bmpdc, bitmap);
return true;
}
protected:
void removeBitmap() {
if (bmpdc) {
::DeleteDC(bmpdc);
bmpdc = NULL;
}
if (bitmap) {
::DeleteObject(bitmap);
bitmap = NULL;
}
}
HBITMAP bitmap;
HDC bmpdc;
HBRUSH brush;
tjs_int left, top, width, height;
int color;
};
/**
* 表示メッセージ情報
*/
class MessageInfo {
public:
MessageInfo() : left(0), top(0), size(0), color(0xffffff), useShadow(false), shadowColor(0), shadowDistanceX(1), shadowDistanceY(1) {
}
~MessageInfo() {
}
bool setText(iTJSDispatch2 *init) {
ncbPropAccessor info(init);
#define GETINTVALUE(a,def) a = info.getIntValue(TJS_W(#a), def)
#define GETBOOLVALUE(a,def) a = info.getIntValue(TJS_W(#a),def?1:0) != 0
#define GETSTRVALUE(a,def) a = info.getStrValue(TJS_W(#a),def)
GETINTVALUE(left, 0);
GETINTVALUE(top, 0);
GETINTVALUE(size, 12);
GETINTVALUE(color, 0xffffff);
GETINTVALUE(shadowColor,-1);
GETINTVALUE(shadowDistanceX,1);
GETINTVALUE(shadowDistanceY,1);
useShadow = shadowColor > 0;
}
void show(HDC dc, PAINTSTRUCT &ps) {
if (useShadow) {
// OutputText(left+shadowDistanceX, top+shadowDistanceY, text.c_str());
}
// OutputText(left, top, text.c_str());
}
protected:
int left;
int top;
ttstr text;
int size;
int color;
bool useShadow;
int shadowColor;
int shadowDistanceX;
int shadowDistanceY;
HFONT font;
};
/**
* セーブ処理スレッド用情報
* プログレス処理を実行するウインドウ
*/
class ProgressWindow {
public:
/**
* ウインドウクラスの登録
*/
static void registerWindowClass() {
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof wcex);
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle(NULL);
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_WAIT);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = CLASSNAME;
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
}
/**
* ウインドウクラスの削除
*/
static void unregisterWindowClass() {
UnregisterClass(CLASSNAME, GetModuleHandle(NULL));
}
/**
* コンストラクタ
*/
ProgressWindow(iTJSDispatch2 *window, iTJSDispatch2 *init) : window(window), hParent(0), hWnd(0), thread(0), doneflag(false), cancelflag(false), percent(0),
progressBarEnable(true), progressBarHandle(0), progressBarStyle(0),
progressBarLeft(-1), progressBarTop(-1), progressBarWidth(-1), progressBarHeight(-1),
progressBarColor(0xff000000), progressBarBackColor(0xff000000),
cancelButtonEnable(true), cancelButtonHandle(0), cancelButtonCaption(TJS_W("Cancel")),
cancelButtonLeft(-1), cancelButtonTop(-1), cancelButtonWidth(-1), cancelButtonHeight(-1) {
prepare = CreateEvent(NULL, FALSE, FALSE, NULL);
if (init) {
ncbPropAccessor info(init);
GETINTVALUE(progressBarStyle, 0);
GETINTVALUE(progressBarTop, -1);
GETINTVALUE(progressBarLeft, -1);
GETINTVALUE(progressBarWidth, -1);
GETINTVALUE(progressBarHeight, -1);
GETINTVALUE(progressBarColor, 0xff000000);
GETINTVALUE(progressBarBackColor, 0xff000000);
GETBOOLVALUE(progressBarEnable, true);
GETSTRVALUE(cancelButtonCaption, ttstr("Cancel"));
GETINTVALUE(cancelButtonLeft, -1);
GETINTVALUE(cancelButtonTop, -1);
GETINTVALUE(cancelButtonWidth, -1);
GETINTVALUE(cancelButtonHeight, -1);
GETBOOLVALUE(cancelButtonEnable, true);
}
setReceiver(true);
start();
}
/**
* デストラクタ
*/
~ProgressWindow() {
CloseHandle(prepare);
setReceiver(false);
end();
}
/**
* プログレス通知
* @return キャンセルされてたら true
*/
bool doProgress(int percent) {
if (percent != this->percent) {
this->percent = percent;
if (progressBarHandle) {
SendMessage(progressBarHandle, PBM_SETPOS, (WPARAM)percent, 0 );
}
}
return !hWnd || cancelflag;
}
/**
* プログレス処理のテキストを差し替える
* @param name 識別名
* @param text 表示テキスト
*/
void setProgressMessage(const tjs_char *name, const tjs_char *text) {
}
protected:
iTJSDispatch2 *window; //< 親ウインドウ
HWND hParent; //< 親ハンドル
HWND hWnd; //< 自分のハンドル
HANDLE thread; //< プログレス処理のスレッド
HANDLE prepare; //< 準備待ちイベント
bool doneflag; // 終了フラグ
bool cancelflag; // キャンセルフラグ
int percent; // パーセント指定
bool progressBarEnable;
HWND progressBarHandle; //< プログレスバーのハンドラ
int progressBarLeft;
int progressBarStyle;
int progressBarTop;
int progressBarWidth;
int progressBarHeight;
int progressBarColor;
int progressBarBackColor;
bool cancelButtonEnable;
HWND cancelButtonHandle;
ttstr cancelButtonCaption;
int cancelButtonLeft;
int cancelButtonTop;
int cancelButtonWidth;
int cancelButtonHeight;
ImageInfo *backGround;
/**
* ウインドウプロシージャ
*/
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
ProgressWindow *self = (ProgressWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (self) {
switch (message) {
case WM_PAINT: // 画面更新
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(hWnd, &ps);
self->show(dc, ps);
EndPaint(hWnd, &ps);
}
return 0;
case WM_COMMAND: // キャンセル通知
switch (wParam) {
case ID_CANCEL:
self->cancel();
break;
}
break;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
/*
* ウインドウイベント処理レシーバ
*/
static bool __stdcall receiver(void *userdata, tTVPWindowMessage *Message) {
ProgressWindow *self = (ProgressWindow*)userdata;
switch (Message->Msg) {
case TVP_WM_ATTACH:
self->start();
break;
case TVP_WM_DETACH:
self->end();
break;
default:
break;
}
return false;
}
// ユーザメッセージレシーバの登録/解除
void setReceiver(bool enable) {
tTJSVariant mode = enable ? (tTVInteger)(tjs_int)wrmRegister : (tTVInteger)(tjs_int)wrmUnregister;
tTJSVariant proc = (tTVInteger)(tjs_intptr_t)receiver;
tTJSVariant userdata = (tTVInteger)(tjs_intptr_t)this;
tTJSVariant *p[] = {&mode, &proc, &userdata};
if (window->FuncCall(0, TJS_W("registerMessageReceiver"), NULL, NULL, 4, p, window) != TJS_S_OK) {
TVPThrowExceptionMessage(TJS_W("can't regist user message receiver"));
}
}
// 実行スレッド
static unsigned __stdcall threadFunc(void *data) {
((ProgressWindow*)data)->main();
_endthreadex(0);
return 0;
}
/**
* 処理開始
*/
void start() {
end();
doneflag = false;
tTJSVariant krkrHwnd;
if (TJS_SUCCEEDED(window->PropGet(0, TJS_W("HWND"), NULL, &krkrHwnd, window))) {
hParent = ::FindWindowEx((HWND)(tjs_intptr_t)krkrHwnd, NULL, KRKRDISPWINDOWCLASS, NULL);
// KRKRDISPWINDOWCLASSが見つからない場合は吉里吉里Zなので自身をhParentとする
if (! hParent)
hParent = (HWND)(tjs_intptr_t)krkrHwnd;
if (hParent) {
thread = (HANDLE)_beginthreadex(NULL, 0, threadFunc, this, 0, NULL);
if (thread) {
WaitForSingleObject(prepare, 1000 * 3);
}
}
}
}
/**
* 処理終了
*/
void end() {
doneflag = true;
if (thread) {
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
thread = 0;
}
hParent = 0;
}
/**
* 実行メイン処理
* ウインドウの生成から破棄までを独立したスレッドで行う
*/
void main() {
// ウインドウ生成
if (hParent && !hWnd) {
RECT rect;
POINT point;
point.x = 0;
point.y = 0;
::GetClientRect(hParent, &rect);
::ClientToScreen(hParent, &point);
int left = point.x;
int top = point.y;
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
hWnd = ::CreateWindowEx(0, CLASSNAME, _T(""), WS_POPUP, left, top, width, height, hParent, 0, GetModuleHandle(NULL), NULL);
if (hWnd && !doneflag) {
::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(tjs_intptr_t)this);
::ShowWindow(hWnd,TRUE);
create();
// 待ち合わせ完了
SetEvent(prepare);
// メッセージループの実行
MSG msg;
while (!doneflag) {
if (::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
if (GetMessage(&msg, NULL, 0, 0)) {
::TranslateMessage (&msg);
::DispatchMessage (&msg);
} else {
break;
}
} else {
Sleep(0);
}
}
// ウインドウの破棄
::DestroyWindow(hWnd);
hWnd = 0;
}
}
}
// -------------------------------------------------------------
static int RGBTOBGR(int c) {
unsigned int a = (unsigned int)c & 0xff000000;
unsigned int r = (unsigned int)c & 0x00ff0000;
unsigned int g = (unsigned int)c & 0x0000ff00;
unsigned int b = (unsigned int)c & 0x000000ff;
return (int)(a | r>>16 | g | b<<16);
}
/**
* 描画内容生成
*/
void create() {
RECT rect;
GetClientRect(hWnd, &rect);
int swidth = rect.right - rect.left;
int sheight = rect.bottom - rect.top;
if (progressBarEnable) {
// プログレスバーの配置決定
if (progressBarWidth < 0) {
progressBarWidth = swidth / 3;
}
if (progressBarHeight < 0) {
progressBarHeight = sheight/10;
}
if (progressBarLeft < 0) {
progressBarLeft = (swidth - progressBarWidth)/2;
}
if (progressBarTop < 0) {
progressBarTop = (sheight - progressBarHeight)/2;
}
// プログレスバーを作成
progressBarHandle = CreateWindowEx(0, PROGRESS_CLASS, _T(""),
WS_VISIBLE | WS_CHILD | (progressBarStyle & (PBS_SMOOTH|PBS_VERTICAL)),
progressBarLeft, progressBarTop, progressBarWidth, progressBarHeight,
hWnd, (HMENU)1, GetModuleHandle(NULL), NULL);
SendMessage(progressBarHandle, PBM_SETBARCOLOR, 0, RGBTOBGR(progressBarColor));
SendMessage(progressBarHandle, PBM_SETBKCOLOR, 0, RGBTOBGR(progressBarBackColor));
SendMessage(progressBarHandle, PBM_SETRANGE , 0, MAKELPARAM(0, 100));
SendMessage(progressBarHandle, PBM_SETSTEP, 1, 0 );
SendMessage(progressBarHandle, PBM_SETPOS, percent, 0);
}
if (cancelButtonEnable) {
// キャンセルボタンの配置決定
if (cancelButtonWidth < 0) {
cancelButtonWidth = cancelButtonCaption.length() * 16 + 8;
}
if (cancelButtonHeight < 0) {
cancelButtonHeight = 24;
}
if (cancelButtonLeft < 0) {
cancelButtonLeft = (swidth - cancelButtonWidth)/2;
}
if (cancelButtonTop < 0) {
cancelButtonTop = sheight - cancelButtonHeight * 3;
}
// キャンセルボタンを作成
cancelButtonHandle = CreateWindow(_T("BUTTON"), cancelButtonCaption.c_str(),
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
cancelButtonLeft, cancelButtonTop, cancelButtonWidth, cancelButtonHeight,
hWnd, (HMENU)ID_CANCEL, GetModuleHandle(NULL), NULL);
}
}
/**
* 画面更新処理
*/
void show(HDC dc, PAINTSTRUCT &ps) {
// 背景で塗りつぶし
// アニメパターンを描画
// テキストを表示
}
/**
* キャンセル通知
*/
void cancel() {
cancelflag = true;
}
};
/**
* ウインドウにレイヤセーブ機能を拡張
*/
class WindowExProgress {
protected:
iTJSDispatch2 *objthis; //< オブジェクト情報の参照
ProgressWindow *progressWindow; //< プログレス表示用
public:
/**
* コンストラクタ
*/
WindowExProgress(iTJSDispatch2 *objthis) : objthis(objthis), progressWindow(NULL) {}
/**
* デストラクタ
*/
~WindowExProgress() {
delete progressWindow;
}
/**
* プログレス処理を開始する。
* 吉里吉里が実行ブロック中でも正常に表示継続します。
* @param init 初期化データ(辞書)
*/
void startProgress(iTJSDispatch2 *init) {
if (progressWindow) {
TVPThrowExceptionMessage(TJS_W("already running progress"));
}
progressWindow = new ProgressWindow(objthis, init);
}
/**
* プログレス処理の経過状態を通知する。
* @param percent 経過状態をパーセント指定
* @return キャンセル要求があれば true
*/
bool doProgress(int percent) {
if (!progressWindow) {
TVPThrowExceptionMessage(TJS_W("not running progress"));
}
return progressWindow->doProgress(percent);
}
/**
* プログレス処理を終了する。
*/
void endProgress() {
if (!progressWindow) {
TVPThrowExceptionMessage(TJS_W("not running progress"));
}
delete progressWindow;
progressWindow = NULL;
}
};
//---------------------------------------------------------------------------
// インスタンスゲッタ
NCB_GET_INSTANCE_HOOK(WindowExProgress)
{
NCB_INSTANCE_GETTER(objthis) { // objthis を iTJSDispatch2* 型の引数とする
ClassT* obj = GetNativeInstance(objthis); // ネイティブインスタンスポインタ取得
if (!obj) {
obj = new ClassT(objthis); // ない場合は生成する
SetNativeInstance(objthis, obj); // objthis に obj をネイティブインスタンスとして登録する
}
return obj;
}
};
#define ENUM(n) Variant(#n, (int)n)
NCB_ATTACH_CLASS_WITH_HOOK(WindowExProgress, Window) {
ENUM(PBS_SMOOTH);
ENUM(PBS_VERTICAL);
NCB_METHOD(startProgress);
NCB_METHOD(doProgress);
NCB_METHOD(endProgress);
};
/**
* 登録処理後
*/
static void PreRegistCallback()
{
ProgressWindow::registerWindowClass();
}
/**
* 開放処理前
*/
static void PostUnregistCallback()
{
ProgressWindow::unregisterWindowClass();
}
NCB_PRE_REGIST_CALLBACK(PreRegistCallback);
NCB_POST_UNREGIST_CALLBACK(PostUnregistCallback);