This repository has been archived by the owner on Sep 6, 2019. It is now read-only.
/
IGraphicsWin.cpp
1335 lines (1165 loc) · 32.9 KB
/
IGraphicsWin.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
#include "IGraphicsWin.h"
#include "IControl.h"
#include "Log.h"
#include <wininet.h>
#include <Shlobj.h>
#include <commctrl.h>
#ifdef RTAS_API
#include "PlugInUtils.h"
#endif
#pragma warning(disable:4244) // Pointer size cast mismatch.
#pragma warning(disable:4312) // Pointer size cast mismatch.
#pragma warning(disable:4311) // Pointer size cast mismatch.
static int nWndClassReg = 0;
static const char* wndClassName = "IPlugWndClass";
static double sFPS = 0.0;
#define PARAM_EDIT_ID 99
enum EParamEditMsg
{
kNone,
kEditing,
kUpdate,
kCancel,
kCommit
};
#define IPLUG_TIMER_ID 2
inline IMouseMod GetMouseMod(WPARAM wParam)
{
return IMouseMod((wParam & MK_LBUTTON),
(wParam & MK_RBUTTON),
(wParam & MK_SHIFT),
(wParam & MK_CONTROL),
#ifdef RTAS_API
IsOptionKeyDown()
#elif defined(AAX_API)
GetAsyncKeyState(VK_MENU) < 0
#else
GetKeyState(VK_MENU) < 0
#endif
);
}
// static
LRESULT CALLBACK IGraphicsWin::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_CREATE)
{
LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LPARAM) (lpcs->lpCreateParams));
int mSec = int(1000.0 / sFPS);
SetTimer(hWnd, IPLUG_TIMER_ID, mSec, NULL);
SetFocus(hWnd); // gets scroll wheel working straight away
return 0;
}
IGraphicsWin* pGraphics = (IGraphicsWin*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
char txt[MAX_PARAM_LEN];
double v;
if (!pGraphics || hWnd != pGraphics->mPlugWnd)
{
return DefWindowProc(hWnd, msg, wParam, lParam);
}
if (pGraphics->mParamEditWnd && pGraphics->mParamEditMsg == kEditing)
{
if (msg == WM_RBUTTONDOWN || (msg == WM_LBUTTONDOWN))
{
pGraphics->mParamEditMsg = kCancel;
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
switch (msg)
{
case WM_TIMER:
{
if (wParam == IPLUG_TIMER_ID)
{
if (pGraphics->mParamEditWnd && pGraphics->mParamEditMsg != kNone)
{
switch (pGraphics->mParamEditMsg)
{
case kCommit:
{
SendMessage(pGraphics->mParamEditWnd, WM_GETTEXT, MAX_PARAM_LEN, (LPARAM) txt);
if(pGraphics->mEdParam)
{
IParam::EParamType type = pGraphics->mEdParam->Type();
if ( type == IParam::kTypeEnum || type == IParam::kTypeBool)
{
int vi = 0;
pGraphics->mEdParam->MapDisplayText(txt, &vi);
v = (double) vi;
}
else
{
v = atof(txt);
if (pGraphics->mEdParam->DisplayIsNegated())
{
v = -v;
}
}
pGraphics->mEdControl->SetValueFromUserInput(pGraphics->mEdParam->GetNormalized(v));
}
else
{
pGraphics->mEdControl->TextFromTextEntry(txt);
}
// Fall through.
}
case kCancel:
{
SetWindowLongPtr(pGraphics->mParamEditWnd, GWLP_WNDPROC, (LPARAM) pGraphics->mDefEditProc);
DestroyWindow(pGraphics->mParamEditWnd);
pGraphics->mParamEditWnd = 0;
pGraphics->mEdParam = 0;
pGraphics->mEdControl = 0;
pGraphics->mDefEditProc = 0;
}
break;
}
pGraphics->mParamEditMsg = kNone;
return 0; // TODO: check this!
}
IRECT dirtyR;
if (pGraphics->IsDirty(&dirtyR))
{
RECT r = { dirtyR.L, dirtyR.T, dirtyR.R, dirtyR.B };
InvalidateRect(hWnd, &r, FALSE);
if (pGraphics->mParamEditWnd)
{
IRECT* notDirtyR = pGraphics->mEdControl->GetRECT();
RECT r2 = { notDirtyR->L, notDirtyR->T, notDirtyR->R, notDirtyR->B };
ValidateRect(hWnd, &r2); // make sure we dont redraw the edit box area
UpdateWindow(hWnd);
pGraphics->mParamEditMsg = kUpdate;
}
else
{
UpdateWindow(hWnd);
}
}
}
return 0;
}
case WM_RBUTTONDOWN:
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
pGraphics->HideTooltip();
if (pGraphics->mParamEditWnd)
{
pGraphics->mParamEditMsg = kCommit;
return 0;
}
SetFocus(hWnd); // Added to get keyboard focus again when user clicks in window
SetCapture(hWnd);
#ifdef RTAS_API
// pass ctrl-start-alt-click or ctrl-start-click to host window (Pro Tools)
if ((IsControlKeyDown() && IsOptionKeyDown() && IsCommandKeyDown() ) || (IsControlKeyDown() && IsCommandKeyDown()))
{
HWND rootHWnd = GetAncestor( hWnd, GA_ROOT);
union point
{
long lp;
struct
{
short x;
short y;
} s;
} mousePoint;
// Get global coordinates of local window
RECT childRect;
GetWindowRect(hWnd, &childRect);
// Convert global coords to parent window coords
POINT p;
p.x = childRect.left;
p.y = childRect.top;
ScreenToClient(rootHWnd, &p);
// offset the local click-event coordinates to the parent window's values
mousePoint.lp = lParam;
mousePoint.s.x += p.x;
mousePoint.s.y += p.y;
if( pGraphics->GetParamIdxForPTAutomation(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)) > -1)
{
// Send converted coords to parent window's event handler for regular processing
LRESULT result = SendMessage(rootHWnd, msg, wParam, mousePoint.lp);
}
return 0;
}
#endif
pGraphics->OnMouseDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &GetMouseMod(wParam));
return 0;
case WM_MOUSEMOVE:
{
if (!(wParam & (MK_LBUTTON | MK_RBUTTON)))
{
if (pGraphics->OnMouseOver(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &GetMouseMod(wParam)))
{
TRACKMOUSEEVENT eventTrack = { sizeof(TRACKMOUSEEVENT), TME_LEAVE, hWnd, HOVER_DEFAULT };
if (pGraphics->TooltipsEnabled())
{
int c = pGraphics->GetMouseOver();
if (c != pGraphics->mTooltipIdx)
{
if (c >= 0) eventTrack.dwFlags |= TME_HOVER;
pGraphics->mTooltipIdx = c;
pGraphics->HideTooltip();
}
}
TrackMouseEvent(&eventTrack);
}
}
else if (GetCapture() == hWnd && !pGraphics->mParamEditWnd)
{
pGraphics->OnMouseDrag(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &GetMouseMod(wParam));
}
return 0;
}
case WM_MOUSEHOVER:
{
pGraphics->ShowTooltip();
return 0;
}
case WM_MOUSELEAVE:
{
pGraphics->HideTooltip();
pGraphics->OnMouseOut();
return 0;
}
case WM_LBUTTONUP:
case WM_RBUTTONUP:
{
ReleaseCapture();
pGraphics->OnMouseUp(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &GetMouseMod(wParam));
return 0;
}
case WM_LBUTTONDBLCLK:
{
if (pGraphics->OnMouseDblClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &GetMouseMod(wParam)))
{
SetCapture(hWnd);
}
return 0;
}
case WM_MOUSEWHEEL:
{
if (pGraphics->mParamEditWnd)
{
pGraphics->mParamEditMsg = kCancel;
return 0;
}
else
{
int d = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
int x = GET_X_LPARAM(lParam), y = GET_Y_LPARAM(lParam);
RECT r;
GetWindowRect(hWnd, &r);
pGraphics->OnMouseWheel(x - r.left, y - r.top, &GetMouseMod(wParam), d);
return 0;
}
}
case WM_KEYDOWN:
{
bool handle = true;
int key;
if (wParam == VK_SPACE) key = KEY_SPACE;
else if (wParam == VK_UP) key = KEY_UPARROW;
else if (wParam == VK_DOWN) key = KEY_DOWNARROW;
else if (wParam == VK_LEFT) key = KEY_LEFTARROW;
else if (wParam == VK_RIGHT) key = KEY_RIGHTARROW;
else if (wParam >= '0' && wParam <= '9') key = KEY_DIGIT_0+wParam-'0';
else if (wParam >= 'A' && wParam <= 'Z') key = KEY_ALPHA_A+wParam-'A';
else if (wParam >= 'a' && wParam <= 'z') key = KEY_ALPHA_A+wParam-'a';
else handle = false;
if (handle)
{
POINT p;
GetCursorPos(&p);
ScreenToClient(hWnd, &p);
handle = pGraphics->OnKeyDown(p.x, p.y, key);
}
if (!handle)
{
HWND rootHWnd = GetAncestor( hWnd, GA_ROOT);
SendMessage(rootHWnd, WM_KEYDOWN, wParam, lParam);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
else
return 0;
}
case WM_KEYUP:
{
HWND rootHWnd = GetAncestor(hWnd, GA_ROOT);
SendMessage(rootHWnd, msg, wParam, lParam);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
case WM_PAINT:
{
RECT r;
if (GetUpdateRect(hWnd, &r, FALSE))
{
IRECT ir(r.left, r.top, r.right, r.bottom);
pGraphics->Draw(&ir);
}
return 0;
}
case WM_CTLCOLOREDIT:
{
if(!pGraphics->mEdControl)
return 0;
IText* pText = pGraphics->mEdControl->GetText();
HDC dc = (HDC) wParam;
SetBkColor(dc, RGB(pText->mTextEntryBGColor.R, pText->mTextEntryBGColor.G, pText->mTextEntryBGColor.B));
SetTextColor(dc, RGB(pText->mTextEntryFGColor.R, pText->mTextEntryFGColor.G, pText->mTextEntryFGColor.B));
SetBkMode(dc, OPAQUE);
return (BOOL)GetStockObject(DC_BRUSH);
}
case WM_CLOSE:
{
pGraphics->CloseWindow();
return 0;
}
#ifdef RTAS_API
case WM_MEASUREITEM :
{
HWND rootHWnd = GetAncestor( hWnd, GA_ROOT );
LRESULT result = SendMessage(rootHWnd, msg, wParam, lParam);
return result;
}
case WM_DRAWITEM :
{
HWND rootHWnd = GetAncestor( hWnd, GA_ROOT );
LRESULT result = SendMessage(rootHWnd, msg, wParam, lParam);
return result;
}
#endif
case WM_SETFOCUS:
{
return 0;
}
case WM_KILLFOCUS:
{
return 0;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// static
LRESULT CALLBACK IGraphicsWin::ParamEditProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
IGraphicsWin* pGraphics = (IGraphicsWin*) GetWindowLongPtr(GetParent(hWnd), GWLP_USERDATA);
if (pGraphics && pGraphics->mParamEditWnd && pGraphics->mParamEditWnd == hWnd)
{
pGraphics->HideTooltip();
switch (msg)
{
case WM_CHAR:
{
// limit to numbers for text entry on appropriate parameters
if(pGraphics->mEdParam)
{
char c = wParam;
if(c == 0x08) break; // backspace
switch ( pGraphics->mEdParam->Type() )
{
case IParam::kTypeEnum:
case IParam::kTypeInt:
case IParam::kTypeBool:
if (c >= '0' && c <= '9') break;
else if (c == '-') break;
else if (c == '+') break;
else return 0;
case IParam::kTypeDouble:
if (c >= '0' && c <= '9') break;
else if (c == '-') break;
else if (c == '+') break;
else if (c == '.') break;
else return 0;
default:
break;
}
}
break;
}
case WM_KEYDOWN:
{
if (wParam == VK_RETURN)
{
pGraphics->mParamEditMsg = kCommit;
return 0;
}
else if (wParam == VK_ESCAPE)
{
pGraphics->mParamEditMsg = kCancel;
return 0;
}
break;
}
case WM_SETFOCUS:
{
pGraphics->mParamEditMsg = kEditing;
break;
}
case WM_KILLFOCUS:
{
pGraphics->mParamEditMsg = kCommit;
break;
}
// handle WM_GETDLGCODE so that we can say that we want the return key message
// (normally single line edit boxes don't get sent return key messages)
case WM_GETDLGCODE:
{
if (pGraphics->mEdParam) break;
LPARAM lres;
// find out if the original control wants it
lres = CallWindowProc(pGraphics->mDefEditProc, hWnd, WM_GETDLGCODE, wParam, lParam);
// add in that we want it if it is a return keydown
if (lParam && ((MSG*)lParam)->message == WM_KEYDOWN && wParam == VK_RETURN)
{
lres |= DLGC_WANTMESSAGE;
}
return lres;
}
case WM_COMMAND:
{
switch HIWORD(wParam)
{
case CBN_SELCHANGE:
{
if (pGraphics->mParamEditWnd)
{
pGraphics->mParamEditMsg = kCommit;
return 0;
}
}
}
break; // Else let the default proc handle it.
}
}
return CallWindowProc(pGraphics->mDefEditProc, hWnd, msg, wParam, lParam);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
IGraphicsWin::IGraphicsWin(IPlugBase* pPlug, int w, int h, int refreshFPS)
: IGraphics(pPlug, w, h, refreshFPS), mPlugWnd(0), mParamEditWnd(0),
mPID(0), mParentWnd(0), mMainWnd(0), mCustomColorStorage(0),
mEdControl(0), mEdParam(0), mDefEditProc(0), mParamEditMsg(kNone),
mTooltipWnd(0), mShowingTooltip(false), mTooltipIdx(-1),
mHInstance(0)
{}
IGraphicsWin::~IGraphicsWin()
{
CloseWindow();
FREE_NULL(mCustomColorStorage);
}
LICE_IBitmap* IGraphicsWin::OSLoadBitmap(int ID, const char* name)
{
const char* ext = name+strlen(name)-1;
while (ext > name && *ext != '.') --ext;
++ext;
if (!stricmp(ext, "png")) return _LICE::LICE_LoadPNGFromResource(mHInstance, MAKEINTRESOURCE(ID), 0);
#ifdef IPLUG_JPEG_SUPPORT
if (!stricmp(ext, "jpg") || !stricmp(ext, "jpeg")) return _LICE::LICE_LoadJPGFromResource(mHInstance, ID, 0);
#endif
return 0;
}
void GetWindowSize(HWND pWnd, int* pW, int* pH)
{
if (pWnd)
{
RECT r;
GetWindowRect(pWnd, &r);
*pW = r.right - r.left;
*pH = r.bottom - r.top;
}
else
{
*pW = *pH = 0;
}
}
bool IsChildWindow(HWND pWnd)
{
if (pWnd)
{
int style = GetWindowLong(pWnd, GWL_STYLE);
int exStyle = GetWindowLong(pWnd, GWL_EXSTYLE);
return ((style & WS_CHILD) && !(exStyle & WS_EX_MDICHILD));
}
return false;
}
void IGraphicsWin::ForceEndUserEdit()
{
mParamEditMsg = kCancel;
}
#define SETPOS_FLAGS SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE
void IGraphicsWin::Resize(int w, int h)
{
if (w == Width() && h == Height()) return;
int dw = w - Width(), dh = h - Height();
IGraphics::Resize(w, h);
if (WindowIsOpen())
{
HWND pParent = 0, pGrandparent = 0;
int plugW = 0, plugH = 0, parentW = 0, parentH = 0, grandparentW = 0, grandparentH = 0;
GetWindowSize(mPlugWnd, &plugW, &plugH);
if (IsChildWindow(mPlugWnd))
{
pParent = GetParent(mPlugWnd);
GetWindowSize(pParent, &parentW, &parentH);
if (IsChildWindow(pParent))
{
pGrandparent = GetParent(pParent);
GetWindowSize(pGrandparent, &grandparentW, &grandparentH);
}
}
SetWindowPos(mPlugWnd, 0, 0, 0, plugW + dw, plugH + dh, SETPOS_FLAGS);
// don't want to touch the host window in VST3 or RTAS
if(mPlug->GetAPI() != kAPIVST3 && mPlug->GetAPI() != kAPIRTAS)
{
if(pParent)
{
SetWindowPos(pParent, 0, 0, 0, parentW + dw, parentH + dh, SETPOS_FLAGS);
}
if(pGrandparent)
{
SetWindowPos(pGrandparent, 0, 0, 0, grandparentW + dw, grandparentH + dh, SETPOS_FLAGS);
}
}
RECT r = { 0, 0, Width(), Height() };
InvalidateRect(mPlugWnd, &r, FALSE);
}
}
void IGraphicsWin::HideMouseCursor()
{
if (!mCursorHidden)
{
POINT p;
GetCursorPos(&p);
mHiddenMousePointX = p.x;
mHiddenMousePointY = p.y;
ShowCursor(false);
mCursorHidden=true;
}
}
void IGraphicsWin::ShowMouseCursor()
{
if (mCursorHidden)
{
SetCursorPos(mHiddenMousePointX, mHiddenMousePointY);
ShowCursor(true);
mCursorHidden=false;
}
}
int IGraphicsWin::ShowMessageBox(const char* pText, const char* pCaption, int type)
{
return MessageBox(GetMainWnd(), pText, pCaption, type);
}
bool IGraphicsWin::DrawScreen(IRECT* pR)
{
PAINTSTRUCT ps;
HWND hWnd = (HWND) GetWindow();
HDC dc = BeginPaint(hWnd, &ps);
BitBlt(dc, pR->L, pR->T, pR->W(), pR->H(), mDrawBitmap->getDC(), pR->L, pR->T, SRCCOPY);
EndPaint(hWnd, &ps);
return true;
}
void* IGraphicsWin::OpenWindow(void* pParentWnd)
{
int x = 0, y = 0, w = Width(), h = Height();
mParentWnd = (HWND) pParentWnd;
if (mPlugWnd)
{
RECT pR, cR;
GetWindowRect((HWND) pParentWnd, &pR);
GetWindowRect(mPlugWnd, &cR);
CloseWindow();
x = cR.left - pR.left;
y = cR.top - pR.top;
w = cR.right - cR.left;
h = cR.bottom - cR.top;
}
if (nWndClassReg++ == 0)
{
WNDCLASS wndClass = { CS_DBLCLKS, WndProc, 0, 0, mHInstance, 0, LoadCursor(NULL, IDC_ARROW), 0, 0, wndClassName };
RegisterClass(&wndClass);
}
sFPS = FPS();
mPlugWnd = CreateWindow(wndClassName, "IPlug", WS_CHILD | WS_VISIBLE, // | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
x, y, w, h, (HWND) pParentWnd, 0, mHInstance, this);
//SetWindowLong(mPlugWnd, GWL_USERDATA, (LPARAM) this);
if (!mPlugWnd && --nWndClassReg == 0)
{
UnregisterClass(wndClassName, mHInstance);
}
else
{
SetAllControlsDirty();
}
if (mPlugWnd && TooltipsEnabled())
{
bool ok = false;
static const INITCOMMONCONTROLSEX iccex = { sizeof(INITCOMMONCONTROLSEX), ICC_TAB_CLASSES };
if (InitCommonControlsEx(&iccex))
{
mTooltipWnd = CreateWindowEx(0, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, mPlugWnd, NULL, mHInstance, NULL);
if (mTooltipWnd)
{
SetWindowPos(mTooltipWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
TOOLINFO ti = { TTTOOLINFOA_V2_SIZE, TTF_IDISHWND | TTF_SUBCLASS, mPlugWnd, (UINT_PTR)mPlugWnd };
ti.lpszText = (LPTSTR)NULL;
SendMessage(mTooltipWnd, TTM_ADDTOOL, 0, (LPARAM)&ti);
ok = true;
}
}
if (!ok) EnableTooltips(ok);
}
return mPlugWnd;
}
#define MAX_CLASSNAME_LEN 128
void GetWndClassName(HWND hWnd, WDL_String* pStr)
{
char cStr[MAX_CLASSNAME_LEN];
cStr[0] = '\0';
GetClassName(hWnd, cStr, MAX_CLASSNAME_LEN);
pStr->Set(cStr);
}
BOOL CALLBACK IGraphicsWin::FindMainWindow(HWND hWnd, LPARAM lParam)
{
IGraphicsWin* pGraphics = (IGraphicsWin*) lParam;
if (pGraphics)
{
DWORD wPID;
GetWindowThreadProcessId(hWnd, &wPID);
WDL_String str;
GetWndClassName(hWnd, &str);
if (wPID == pGraphics->mPID && !strcmp(str.Get(), pGraphics->mMainWndClassName.Get()))
{
pGraphics->mMainWnd = hWnd;
return FALSE; // Stop enumerating.
}
}
return TRUE;
}
HWND IGraphicsWin::GetMainWnd()
{
if (!mMainWnd)
{
if (mParentWnd)
{
HWND parentWnd = mParentWnd;
while (parentWnd)
{
mMainWnd = parentWnd;
parentWnd = GetParent(mMainWnd);
}
GetWndClassName(mMainWnd, &mMainWndClassName);
}
else if (CSTR_NOT_EMPTY(mMainWndClassName.Get()))
{
mPID = GetCurrentProcessId();
EnumWindows(FindMainWindow, (LPARAM) this);
}
}
return mMainWnd;
}
#define TOOLWIN_BORDER_W 6
#define TOOLWIN_BORDER_H 23
IRECT IGraphicsWin::GetWindowRECT()
{
if (mPlugWnd)
{
RECT r;
GetWindowRect(mPlugWnd, &r);
r.right -= TOOLWIN_BORDER_W;
r.bottom -= TOOLWIN_BORDER_H;
return IRECT(r.left, r.top, r.right, r.bottom);
}
return IRECT();
}
void IGraphicsWin::SetWindowTitle(char* str)
{
SetWindowText(mPlugWnd, str);
}
void IGraphicsWin::CloseWindow()
{
if (mPlugWnd)
{
if (mTooltipWnd)
{
DestroyWindow(mTooltipWnd);
mTooltipWnd = 0;
mShowingTooltip = false;
mTooltipIdx = -1;
}
DestroyWindow(mPlugWnd);
mPlugWnd = 0;
if (--nWndClassReg == 0)
{
UnregisterClass(wndClassName, mHInstance);
}
}
}
IPopupMenu* IGraphicsWin::GetItemMenu(long idx, long &idxInMenu, long &offsetIdx, IPopupMenu* pMenu)
{
long oldIDx = offsetIdx;
offsetIdx += pMenu->GetNItems();
if (idx < offsetIdx)
{
idxInMenu = idx - oldIDx;
return pMenu;
}
IPopupMenu* menu = 0;
for(int i = 0; i< pMenu->GetNItems(); i++)
{
IPopupMenuItem* menuItem = pMenu->GetItem(i);
if(menuItem->GetSubmenu())
{
menu = GetItemMenu(idx, idxInMenu, offsetIdx, menuItem->GetSubmenu());
if(menu)
break;
}
}
return menu;
}
HMENU IGraphicsWin::CreateMenu(IPopupMenu* pMenu, long* offsetIdx)
{
HMENU hMenu = CreatePopupMenu();
int flags = 0;
long idxSubmenu = 0;
long offset = *offsetIdx;
long nItems = pMenu->GetNItems();
*offsetIdx += nItems;
long inc = 0;
for(int i = 0; i< nItems; i++)
{
IPopupMenuItem* menuItem = pMenu->GetItem(i);
if (menuItem->GetIsSeparator())
{
AppendMenu (hMenu, MF_SEPARATOR, 0, 0);
}
else
{
const char* str = menuItem->GetText();
char* titleWithPrefixNumbers = 0;
if (pMenu->GetPrefix())
{
titleWithPrefixNumbers = (char*)malloc(strlen(str) + 50);
switch (pMenu->GetPrefix())
{
case 1:
{
sprintf(titleWithPrefixNumbers, "%1d: %s", i+1, str); break;
}
case 2:
{
sprintf(titleWithPrefixNumbers, "%02d: %s", i+1, str); break;
}
case 3:
{
sprintf(titleWithPrefixNumbers, "%03d: %s", i+1, str); break;
}
}
}
const char* entryText (titleWithPrefixNumbers ? titleWithPrefixNumbers : str);
flags = MF_STRING;
//if (nItems < 160 && pMenu->getNbItemsPerColumn () > 0 && inc && !(inc % _menu->getNbItemsPerColumn ()))
// flags |= MF_MENUBARBREAK;
if (menuItem->GetSubmenu())
{
HMENU submenu = CreateMenu(menuItem->GetSubmenu(), offsetIdx);
if (submenu)
{
AppendMenu(hMenu, flags|MF_POPUP|MF_ENABLED, (UINT_PTR)submenu, (const TCHAR*)entryText);
}
}
else
{
if (menuItem->GetEnabled())
flags |= MF_ENABLED;
else
flags |= MF_GRAYED;
if (menuItem->GetIsTitle())
flags |= MF_DISABLED;
if (menuItem->GetChecked())
flags |= MF_CHECKED;
else
flags |= MF_UNCHECKED;
AppendMenu(hMenu, flags, offset + inc, entryText);
}
if(titleWithPrefixNumbers)
FREE_NULL(titleWithPrefixNumbers);
}
inc++;
}
return hMenu;
}
IPopupMenu* IGraphicsWin::CreateIPopupMenu(IPopupMenu* pMenu, IRECT* pAreaRect)
{
ReleaseMouseCapture();
long offsetIdx = 0;
HMENU hMenu = CreateMenu(pMenu, &offsetIdx);
IPopupMenu* result = 0;
if(hMenu)
{
POINT cPos;
cPos.x = pAreaRect->L;
cPos.y = pAreaRect->B;
ClientToScreen(mPlugWnd, &cPos);
if (TrackPopupMenu(hMenu,
TPM_LEFTALIGN,
cPos.x,
cPos.y,
0,
mPlugWnd,
0))
{
MSG msg;
if (PeekMessage(&msg, mPlugWnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
{
if (HIWORD(msg.wParam) == 0)
{
long res = LOWORD(msg.wParam);
if (res != -1)
{
long idx = 0;
offsetIdx = 0;
IPopupMenu* resultMenu = GetItemMenu(res, idx, offsetIdx, pMenu);
if(resultMenu)
{
result = resultMenu;
result->SetChosenItemIdx(idx);
}
}
}
}
}
DestroyMenu(hMenu);
RECT r = { 0, 0, Width(), Height() };
InvalidateRect(mPlugWnd, &r, FALSE);
}
return result;
}
void IGraphicsWin::CreateTextEntry(IControl* pControl, IText* pText, IRECT* pTextRect, const char* pString, IParam* pParam)
{
if (!pControl || mParamEditWnd) return;
DWORD editStyle;
switch ( pText->mAlign )
{
case IText::kAlignNear: editStyle = ES_LEFT; break;
case IText::kAlignFar: editStyle = ES_RIGHT; break;
case IText::kAlignCenter:
default: editStyle = ES_CENTER; break;
}
mParamEditWnd = CreateWindow("EDIT", pString, ES_AUTOHSCROLL /*only works for left aligned text*/ | WS_CHILD | WS_VISIBLE | ES_MULTILINE | editStyle,
pTextRect->L, pTextRect->T, pTextRect->W()+1, pTextRect->H()+1,
mPlugWnd, (HMENU) PARAM_EDIT_ID, mHInstance, 0);
HFONT font = CreateFont(pText->mSize, 0, 0, 0, pText->mStyle == IText::kStyleBold ? FW_BOLD : 0, pText->mStyle == IText::kStyleItalic ? TRUE : 0, 0, 0, 0, 0, 0, 0, 0, pText->mFont);
SendMessage(mParamEditWnd, EM_LIMITTEXT, (WPARAM) pControl->GetTextEntryLength(), 0);
SendMessage(mParamEditWnd, WM_SETFONT, (WPARAM) font, 0);
SendMessage(mParamEditWnd, EM_SETSEL, 0, -1);
SetFocus(mParamEditWnd);
mDefEditProc = (WNDPROC) SetWindowLongPtr(mParamEditWnd, GWLP_WNDPROC, (LONG_PTR) ParamEditProc);
SetWindowLongPtr(mParamEditWnd, GWLP_USERDATA, 0xdeadf00b);
//DeleteObject(font);
mEdControl = pControl;
mEdParam = pParam; // could be 0
}
#define MAX_PATH_LEN 256
void GetModulePath(HMODULE hModule, WDL_String* pPath)
{
pPath->Set("");
char pathCStr[MAX_PATH_LEN];
pathCStr[0] = '\0';
if (GetModuleFileName(hModule, pathCStr, MAX_PATH_LEN))
{
int s = -1;
for (int i = 0; i < strlen(pathCStr); ++i)
{
if (pathCStr[i] == '\\')