-
Notifications
You must be signed in to change notification settings - Fork 2
/
CWindow.cpp
908 lines (740 loc) · 21.9 KB
/
CWindow.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
//This file was copied and adapted from DeSmuME: http://sourceforge.net/projects/desmume/
//Authors: DeSmuME team
/* Copyright (C) 2006 yopyop
Copyright (C) 2006-2010 DeSmuME team
This file is part of DeSmuME
DeSmuME is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DeSmuME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DeSmuME; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define NOMINMAX
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include "types.h"
#include "main.h"
#include "CWindow.h"
#include "resource.h"
using namespace std;
#define hAppInst g_hInstance
//-----------------------------------------------------------------------------
// The Toolkit - Helpers
//-----------------------------------------------------------------------------
DWORD GetFontQuality()
{
BOOL aaEnabled = FALSE;
UINT aaType = FE_FONTSMOOTHINGSTANDARD;
SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &aaEnabled, 0);
if (aaEnabled == FALSE)
return NONANTIALIASED_QUALITY;
if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &aaType, 0) == FALSE)
return ANTIALIASED_QUALITY;
if (aaType == FE_FONTSMOOTHINGCLEARTYPE)
return CLEARTYPE_QUALITY;
else
return ANTIALIASED_QUALITY;
}
int DrawText(HDC hDC, char* text, int X, int Y, int Width, int Height, UINT format)
{
RECT rc;
SetRect(&rc, X, Y, X+Width, Y+Height);
return DrawText(hDC, text, -1, &rc, format);
}
void GetFontSize(HWND hWnd, HFONT hFont, LPSIZE size)
{
HDC dc = GetDC(hWnd);
HFONT oldfont = (HFONT)SelectObject(dc, hFont);
GetTextExtentPoint32(dc, " ", 1, size);
SelectObject(dc, oldfont);
ReleaseDC(hWnd, dc);
}
void MakeBitmapPseudoTransparent(HBITMAP hBmp, COLORREF cKeyColor, COLORREF cNewKeyColor)
{
u8 keyr = (cKeyColor >> 16) & 0xFF;
u8 keyg = (cKeyColor >> 8) & 0xFF;
u8 keyb = cKeyColor & 0xFF;
u8 nkeyr = (cNewKeyColor >> 16) & 0xFF;
u8 nkeyg = (cNewKeyColor >> 8) & 0xFF;
u8 nkeyb = cNewKeyColor & 0xFF;
BITMAP bmp;
BITMAPINFO bmpinfo;
u8* bmpdata = NULL;
HDC dc = CreateCompatibleDC(NULL);
GetObject(hBmp, sizeof(BITMAP), &bmp);
memset(&bmpinfo, 0, sizeof(BITMAPINFO));
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFO);
bmpinfo.bmiHeader.biBitCount = 24;
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biHeight = bmp.bmHeight;
bmpinfo.bmiHeader.biWidth = bmp.bmWidth;
bmpinfo.bmiHeader.biPlanes = bmp.bmPlanes;
bmpdata = new u8[bmp.bmHeight * bmp.bmWidth * 3];
GetDIBits(dc, hBmp, 0, bmp.bmHeight, (LPVOID)bmpdata, &bmpinfo, DIB_RGB_COLORS);
int y2 = 0;
for (int y = 0; y < bmp.bmHeight; y++)
{
for (int x = 0; x < bmp.bmWidth; x++)
{
int idx = y2 + x*3;
u8 r = bmpdata[idx + 0];
u8 g = bmpdata[idx + 1];
u8 b = bmpdata[idx + 2];
if ((r == keyr) && (g == keyg) && (b == keyb))
{
bmpdata[idx + 0] = nkeyr;
bmpdata[idx + 1] = nkeyg;
bmpdata[idx + 2] = nkeyb;
}
}
y2 += bmp.bmWidth * 3;
}
SetDIBits(dc, hBmp, 0, bmp.bmHeight, (LPVOID)bmpdata, &bmpinfo, DIB_RGB_COLORS);
DeleteDC(dc);
delete[] bmpdata;
}
//-----------------------------------------------------------------------------
// Window class handling
//-----------------------------------------------------------------------------
vector<string> ReggedWndClasses;
bool RegWndClass(string name, WNDPROC wndProc, UINT style, int extraSize)
{
return RegWndClass(name, wndProc, style, NULL, extraSize);
}
bool RegWndClass(string name, WNDPROC wndProc, UINT style, HICON icon, int extraSize)
{
// If the class is already regged, don't re-reg it
if (find(ReggedWndClasses.begin(), ReggedWndClasses.end(), name) != ReggedWndClasses.end())
return true;
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.lpszClassName = name.c_str();
wc.hInstance = hAppInst;
wc.lpfnWndProc = wndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = icon;
wc.lpszMenuName = 0;
wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wc.style = style;
wc.cbClsExtra = 0;
wc.cbWndExtra = DWLP_USER + extraSize;
wc.hIconSm = 0;
if (RegisterClassEx(&wc) != 0)
{
// If registration succeeded, add the class name into the list
ReggedWndClasses.push_back(name);
return true;
}
else
return false;
}
void UnregWndClass(string name)
{
vector<string>::iterator it = find(ReggedWndClasses.begin(), ReggedWndClasses.end(), name);
// If the class wasn't regged, we can't unreg it :P
if (it == ReggedWndClasses.end())
return;
// Otherwise unreg the class and remove its name from the list
// ONLY if unregging was successful. Unregging will fail if one
// or more windows using the class still exist.
if (UnregisterClass(name.c_str(), hAppInst) != 0)
ReggedWndClasses.erase(it);
}
//-----------------------------------------------------------------------------
// Base toolwindow class
//-----------------------------------------------------------------------------
CToolWindow::CToolWindow(char* _className, WNDPROC _proc, char* _title, int _width, int _height)
: hWnd(NULL)
, className(_className)
, proc((DLGPROC)_proc)
, title(_title)
, width(_width)
, height(_height)
, whichInit(0)
{
}
CToolWindow::CToolWindow(int _ID, DLGPROC _proc, char* _title)
: hWnd(NULL)
, ID(_ID)
, proc(_proc)
, title(_title)
, whichInit(1)
{
}
void CToolWindow::PostInitialize()
{
if(whichInit==0)
{
DWORD style = WS_CAPTION | WS_SYSMENU | WS_SIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
RECT rc;
rc.left = 0;
rc.right = width;
rc.top = 0;
rc.bottom = height;
AdjustWindowRect(&rc, style, FALSE);
hWnd = CreateWindow(className, title.c_str(), style, CW_USEDEFAULT, CW_USEDEFAULT,
rc.right - rc.left, rc.bottom - rc.top, HWND_DESKTOP, NULL, hAppInst, (LPVOID)this);
}
else
{
hWnd = CreateDialogParam(hAppInst, MAKEINTRESOURCE(ID), HWND_DESKTOP, proc, (LPARAM)this);
if (hWnd == NULL)
return;
SetWindowText(hWnd, title.c_str());
//SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hAppInst, MAKEINTRESOURCE(ICONDESMUME)));
}
}
CToolWindow::~CToolWindow()
{
}
//-----------------------------------------------------------------------------
// Toolwindow handling
//-----------------------------------------------------------------------------
CToolWindow* ToolWindowList = NULL;
bool OpenToolWindow(CToolWindow* wnd)
{
// A hWnd value of NULL indicates failure to create the window.
// In this case, just delete the toolwindow and return failure.
if (wnd->hWnd == NULL)
{
delete wnd;
return false;
}
// Add the toolwindow to the list
if (ToolWindowList == NULL)
{
ToolWindowList = wnd;
wnd->prev = NULL;
wnd->next = NULL;
}
else
{
wnd->prev = NULL;
wnd->next = ToolWindowList;
wnd->next->prev = wnd;
ToolWindowList = wnd;
}
// Show the toolwindow (otherwise it won't show :P )
wnd->Show();
return true;
}
void CloseToolWindow(CToolWindow* wnd)
{
// Remove the toolwindow from the list
if (wnd == ToolWindowList)
{
ToolWindowList = wnd->next;
if (wnd->next) wnd->next->prev = NULL;
wnd->next = NULL;
}
else
{
wnd->prev->next = wnd->next;
if (wnd->next) wnd->next->prev = wnd->prev;
wnd->prev = NULL;
wnd->next = NULL;
}
// Delete the toolwindow object
// its destructor will destroy the window
delete wnd;
}
void CloseAllToolWindows()
{
CToolWindow* wnd;
CToolWindow* wnd_next;
wnd = ToolWindowList;
while (wnd)
{
wnd_next = wnd->next;
wnd->prev = NULL;
wnd->next = NULL;
delete wnd;
wnd = wnd_next;
}
ToolWindowList = NULL;
}
void RefreshAllToolWindows()
{
CToolWindow* wnd;
if (ToolWindowList == NULL)
return;
wnd = ToolWindowList;
while (wnd)
{
wnd->Refresh();
wnd = wnd->next;
}
}
//-----------------------------------------------------------------------------
// The Toolkit - Toolbar API wrapper
//-----------------------------------------------------------------------------
CToolBar::CToolBar(HWND hParent)
: hidden(false)
{
// Create the toolbar
// Note: dropdown buttons look like crap without TBSTYLE_FLAT
hWnd = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS,
0, 0, 0, 0, hParent, NULL, hAppInst, NULL);
// Send it a few messages to finish setting it up
SendMessage(hWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
SendMessage(hWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
}
CToolBar::~CToolBar()
{
// Delete all the HBITMAPs we kept stored
for (TBitmapList::iterator it = hBitmaps.begin();
it != hBitmaps.end(); it++)
{
DeleteObject(it->second.second);
}
hBitmaps.clear();
}
void CToolBar::Show(bool bShow)
{
DWORD style = GetWindowLong(hWnd, GWL_STYLE);
if (bShow)
{
hidden = false;
style |= WS_VISIBLE;
}
else
{
hidden = true;
style &= ~WS_VISIBLE;
}
SetWindowLong(hWnd, GWL_STYLE, style);
}
void CToolBar::OnSize()
{
// Not-fully-working wraparound handling code
// the toolbar behaves weirdly when it comes to separators
// TODO: figure out why.
// Note: right now this code is useless, but it may be useful
// if we use more toolbars
#if 0
RECT rc;
int parentwidth;
GetClientRect(GetParent(hWnd), &rc);
parentwidth = rc.right - rc.left;
// When there's not enough space to fit all the buttons in one line,
// we have to add the 'linebreaks' ourselves
int numbtns = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
int curwidth = 0;
for (int i = 0; i < numbtns; i++)
{
SendMessage(hWnd, TB_GETITEMRECT, i, (LPARAM)&rc);
curwidth += (rc.right - rc.left);
if (i > 1)
{
TBBUTTON btn;
TBBUTTONINFO btninfo;
int cmdid;
SendMessage(hWnd, TB_GETBUTTON, i-1, (LPARAM)&btn);
if (btn.idCommand == -1)
SendMessage(hWnd, TB_GETBUTTON, i-2, (LPARAM)&btn);
cmdid = btn.idCommand;
// Add/remove the TBSTATE_WRAP style if needed
btninfo.cbSize = sizeof(TBBUTTONINFO);
btninfo.dwMask = TBIF_STATE;
SendMessage(hWnd, TB_GETBUTTONINFO, cmdid, (LPARAM)&btninfo);
btninfo.dwMask = TBIF_STATE;
if (curwidth > parentwidth) btninfo.fsState |= TBSTATE_WRAP;
else btninfo.fsState &= ~TBSTATE_WRAP;
SendMessage(hWnd, TB_SETBUTTONINFO, cmdid, (LPARAM)&btninfo);
if (curwidth > parentwidth) curwidth = 0;
}
}
#endif
SetWindowPos(hWnd, NULL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
SWP_NOZORDER | SWP_NOMOVE);
}
void CToolBar::AppendButton(int uID, int uBitmapID, DWORD dwState, bool bDropdown)
{
HBITMAP hbmp;
TBADDBITMAP bmp;
TBBUTTON btn;
// Get the bitmap and replace the key color (magenta) with the right color
hbmp = LoadBitmap(hAppInst, MAKEINTRESOURCE(uBitmapID));
MakeBitmapPseudoTransparent(hbmp, RGB(255, 0, 255), GetSysColor(COLOR_BTNFACE));
// Add the bitmap to the toolbar's image list
bmp.hInst = NULL;
bmp.nID = (UINT_PTR)hbmp;
int bmpid = SendMessage(hWnd, TB_ADDBITMAP, 1, (LPARAM)&bmp);
// Save the bitmap (if it gets deleted, the toolbar is too)
hBitmaps[uBitmapID] = TBitmapPair(bmpid, hbmp);
// And finally add the button
memset(&btn, 0, sizeof(TBBUTTON));
btn.fsStyle = bDropdown ? TBSTYLE_DROPDOWN : TBSTYLE_BUTTON;
btn.fsState = (BYTE) dwState;
btn.idCommand = uID;
btn.iString = -1;
btn.iBitmap = bmpid;
SendMessage(hWnd, TB_ADDBUTTONS, 1, (LPARAM)&btn);
}
void CToolBar::AppendSeparator()
{
TBBUTTON btn;
memset(&btn, 0, sizeof(TBBUTTON));
btn.fsStyle = TBSTYLE_SEP;
btn.idCommand = -1;
btn.iString = -1;
SendMessage(hWnd, TB_ADDBUTTONS, 1, (LPARAM)&btn);
}
void CToolBar::ChangeButtonBitmap(int uID, int uBitmapID)
{
int bmpid = 0;
// If we don't already have the bitmap, retrieve it,
// adapt it and store it in the list
TBitmapList::iterator it = hBitmaps.find(uBitmapID);
if (it == hBitmaps.end())
{
HBITMAP hbmp;
TBADDBITMAP bmp;
hbmp = LoadBitmap(hAppInst, MAKEINTRESOURCE(uBitmapID));
MakeBitmapPseudoTransparent(hbmp, RGB(255, 0, 255), GetSysColor(COLOR_BTNFACE));
bmp.hInst = NULL;
bmp.nID = (UINT_PTR)hbmp;
bmpid = SendMessage(hWnd, TB_ADDBITMAP, 1, (LPARAM)&bmp);
hBitmaps[uBitmapID] = TBitmapPair(bmpid, hbmp);
}
else
bmpid = hBitmaps[uBitmapID].first;
// Finally change the bitmap
SendMessage(hWnd, TB_CHANGEBITMAP, uID, MAKELPARAM(bmpid, 0));
}
void CToolBar::EnableButtonDropdown(int uID, bool bDropdown)
{
TBBUTTONINFO btninfo;
memset(&btninfo, 0, sizeof(TBBUTTONINFO));
btninfo.cbSize = sizeof(TBBUTTONINFO);
btninfo.dwMask = TBIF_STYLE;
SendMessage(hWnd, TB_GETBUTTONINFO, uID, (LPARAM)&btninfo);
btninfo.dwMask = TBIF_STYLE;
if (bDropdown)
{
btninfo.fsStyle &= ~TBSTYLE_BUTTON;
btninfo.fsStyle |= TBSTYLE_DROPDOWN;
}
else
{
btninfo.fsStyle |= TBSTYLE_BUTTON;
btninfo.fsStyle &= ~TBSTYLE_DROPDOWN;
}
SendMessage(hWnd, TB_SETBUTTONINFO, uID, (LPARAM)&btninfo);
}
int CToolBar::GetHeight()
{
if (hidden) return 0;
RECT rc; GetWindowRect(hWnd, &rc);
return rc.bottom - rc.top - 1;
}
WINCLASS::WINCLASS(LPSTR rclass, HINSTANCE hInst)
{
memset(regclass, 0, sizeof(regclass));
memcpy(regclass, rclass, strlen(rclass));
hwnd = NULL;
hmenu = NULL;
hInstance = hInst;
minWidth = 0;
minHeight = 0;
}
WINCLASS::~WINCLASS()
{
}
bool WINCLASS::create(LPSTR caption, int x, int y, int width, int height, int style, HMENU menu)
{
if (hwnd != NULL) return false;
hwnd = CreateWindow(regclass, caption, style, x, y, width, height, NULL, menu, hInstance, NULL);
if (hwnd != NULL) return true;
return false;
}
bool WINCLASS::createEx(LPSTR caption, int x, int y, int width, int height, int style, int styleEx, HMENU menu)
{
if (hwnd != NULL) return false;
hwnd = CreateWindowEx(styleEx, regclass, caption, style, x, y, width, height, NULL, menu, hInstance, NULL);
if (hwnd != NULL) return true;
return false;
}
bool WINCLASS::setMenu(HMENU menu)
{
hmenu = menu;
return SetMenu(hwnd, hmenu)!=FALSE;
}
bool WINCLASS::addMenuItem(u32 item, bool byPos, LPCMENUITEMINFO info)
{
return InsertMenuItem(hmenu, item, byPos ? TRUE : FALSE, info) != FALSE;
}
DWORD WINCLASS::checkMenu(UINT idd, bool check)
{
return CheckMenuItem(hmenu, idd, MF_BYCOMMAND | (check?MF_CHECKED:MF_UNCHECKED));
}
HWND WINCLASS::getHWnd()
{
return hwnd;
}
void WINCLASS::Show(int mode)
{
ShowWindow(hwnd, mode);
}
void WINCLASS::Hide()
{
ShowWindow(hwnd, SW_HIDE);
}
void WINCLASS::setMinSize(int width, int height)
{
minWidth = width;
minHeight = height;
}
static void MyAdjustWindowRectEx(RECT* rect, HWND hwnd)
{
AdjustWindowRectEx(rect,GetWindowStyle(hwnd),TRUE,GetWindowExStyle(hwnd));
//get height of one menu to subtract off
int cymenu = GetSystemMetrics(SM_CYMENU);
//get the height of the actual menu to add back on
MENUBARINFO mbi;
ZeroMemory(&mbi, sizeof(mbi));
mbi.cbSize = sizeof(mbi);
GetMenuBarInfo(hwnd, OBJID_MENU, 0, &mbi);
int menuHeight = (mbi.rcBar.bottom - mbi.rcBar.top + 1);
rect->bottom -= cymenu;
rect->bottom += menuHeight;
}
void WINCLASS::sizingMsg(WPARAM wParam, LPARAM lParam, LONG keepRatio)
{
RECT *rect = (RECT*)lParam;
int prevRight = rect->right;
int prevBottom = rect->bottom;
int _minWidth, _minHeight;
int tbheight = 0/*MainWindowToolbar->GetHeight()*/;
RECT adjr;
SetRect(&adjr,0,0,minWidth,minHeight);
MyAdjustWindowRectEx(&adjr,hwnd);
RECT frameInfo;
SetRect(&frameInfo,0,0,0,0);
MyAdjustWindowRectEx(&frameInfo,hwnd);
int frameWidth = frameInfo.right-frameInfo.left;
int frameHeight = frameInfo.bottom-frameInfo.top + tbheight;
// Calculate the minimum size in pixels
_minWidth = adjr.right-adjr.left;
_minHeight = adjr.bottom-adjr.top + tbheight;
/* Clamp the size to the minimum size (256x384) */
rect->right = (rect->left + std::max(_minWidth, (int)(rect->right - rect->left)));
rect->bottom = (rect->top + std::max(_minHeight, (int)(rect->bottom - rect->top)));
bool horizontalDrag = (wParam == WMSZ_LEFT) || (wParam == WMSZ_RIGHT);
bool verticalDrag = (wParam == WMSZ_TOP) || (wParam == WMSZ_BOTTOM);
if(verticalDrag && !(keepRatio & KEEPY))
{
int clientHeight = rect->bottom - rect->top - frameHeight;
if(clientHeight < minHeight)
rect->bottom += minHeight - clientHeight;
}
else if(horizontalDrag && !(keepRatio & KEEPX))
{
int clientWidth = rect->right - rect->left - frameWidth;
if(clientWidth < minWidth)
rect->right += minWidth - clientWidth;
}
else
{
//Apply the ratio stuff
float ratio1 = ((rect->right - rect->left - frameWidth ) / (float)minWidth);
float ratio2 = ((rect->bottom - rect->top - frameHeight) / (float)minHeight);
float ratio = std::min(ratio1,ratio2);
if(keepRatio & FULLSCREEN)
{
keepRatio |= WINCLASS::KEEPX | WINCLASS::KEEPY;
ratio1 = ratio;
ratio2 = ratio;
}
LONG correctedHeight = (LONG)((rect->top + frameHeight + (minHeight * ratio1)));
LONG correctedWidth = (LONG)((rect->left + frameWidth + (minWidth * ratio2)));
if(keepRatio & KEEPX)
{
if((keepRatio & KEEPY) || (rect->bottom < correctedHeight))
{
if(verticalDrag)
rect->right = correctedWidth;
else
rect->bottom = correctedHeight;
}
}
//else
{
if((keepRatio & KEEPY) && (rect->right < correctedWidth) || (keepRatio&FULLSCREEN))
{
if(horizontalDrag)
rect->bottom = correctedHeight;
else
rect->right = correctedWidth;
}
}
}
// prevent "pushing" the window across the screen when resizing from the left or top
if(wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT)
{
rect->left -= rect->right - prevRight;
rect->right = prevRight;
}
if(wParam == WMSZ_TOP || wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT)
{
rect->top -= rect->bottom - prevBottom;
rect->bottom = prevBottom;
}
// windows screws up the window size if the top of the window goes too high above the top of the screen
if(keepRatio & KEEPY)
{
int titleBarHeight = GetSystemMetrics(SM_CYSIZE);
int topExceeded = -(titleBarHeight + rect->top);
if(topExceeded > 0)
{
rect->top += topExceeded;
rect->bottom += topExceeded;
}
}
}
void WINCLASS::setClientSize(int width, int height)
{
height += 0/*MainWindowToolbar->GetHeight()*/;
//yep, do it twice, once in case the menu wraps, and once to accomodate that wrap
for(int i=0;i<2;i++)
{
RECT rect;
SetRect(&rect,0,0,width,height);
MyAdjustWindowRectEx(&rect,hwnd);
SetWindowPos(hwnd,0,0,0,rect.right-rect.left,rect.bottom-rect.top,SWP_NOMOVE|SWP_NOZORDER);
}
}
//========================================================= Thread class
extern DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
THREADCLASS *tmp = (THREADCLASS *)lpParameter;
return tmp->ThreadFunc();
}
THREADCLASS::THREADCLASS()
{
hThread = NULL;
}
THREADCLASS::~THREADCLASS()
{
closeThread();
}
void THREADCLASS::closeThread()
{
if (hThread)
{
CloseHandle(hThread);
hThread = NULL;
}
}
bool THREADCLASS::createThread()
{
if (hThread) return false;
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, this, 0, &threadID);
if (!hThread) return false;
//WaitForSingleObject(hThread, INFINITE);
return true;
}
//========================================================= Tools class
TOOLSCLASS::TOOLSCLASS(HINSTANCE hInst, int IDD, DLGPROC dlgproc)
{
this->dlgproc = dlgproc;
hwnd = NULL;
hInstance = hInst;
idd=IDD;
memset(class_name, 0, sizeof(class_name));
memset(class_name2, 0, sizeof(class_name2));
}
TOOLSCLASS::~TOOLSCLASS()
{
close();
}
bool TOOLSCLASS::open(bool useThread)
{
if(useThread)
{
if (!createThread()) return false;
else return true;
}
if(doOpen()) return false;
else return true;
}
bool TOOLSCLASS::close()
{
return true;
}
DWORD TOOLSCLASS::doOpen()
{
GetLastError();
hwnd = CreateDialogW(hInstance, MAKEINTRESOURCEW(idd), NULL, (DLGPROC) dlgproc);
if (!hwnd)
{
//LOG("error creating dialog\n");
return (-2);
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return 0;
}
void TOOLSCLASS::doClose()
{
unregClass();
hwnd = NULL;
}
DWORD TOOLSCLASS::ThreadFunc()
{
//LOG("Start thread\n");
DWORD ret = doOpen();
if(ret) return ret;
MSG messages;
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
doClose();
closeThread();
return 0;
}
void TOOLSCLASS::regClass(LPSTR class_name, WNDPROC wproc, bool SecondReg)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
if (SecondReg)
strcpy(this->class_name2, class_name);
else
strcpy(this->class_name, class_name);
wc.lpszClassName = class_name;
wc.hInstance = hInstance;
wc.lpfnWndProc = wproc;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hIcon = 0;
wc.lpszMenuName = 0;
wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIconSm = 0;
RegisterClassEx(&wc);
}
void TOOLSCLASS::unregClass()
{
if (class_name[0])
{
UnregisterClass(class_name, hInstance);
}
if (class_name2[0])
{
UnregisterClass(class_name2, hInstance);
}
memset(class_name, 0, sizeof(class_name));
memset(class_name2, 0, sizeof(class_name2));
}