2011-04-27 08:57:31 +00:00
|
|
|
static LRESULT CALLBACK Canvas_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
|
2012-01-26 06:50:09 +00:00
|
|
|
Object *object = (Object*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
|
|
|
if(object == nullptr) return DefWindowProc(hwnd, msg, wparam, lparam);
|
|
|
|
if(!dynamic_cast<Canvas*>(object)) return DefWindowProc(hwnd, msg, wparam, lparam);
|
|
|
|
Canvas &canvas = (Canvas&)*object;
|
|
|
|
|
2011-11-04 11:57:54 +00:00
|
|
|
if(msg == WM_GETDLGCODE) {
|
|
|
|
return DLGC_STATIC | DLGC_WANTCHARS;
|
|
|
|
}
|
|
|
|
|
2011-04-27 08:57:31 +00:00
|
|
|
if(msg == WM_PAINT) {
|
2012-01-26 06:50:09 +00:00
|
|
|
canvas.p.paint();
|
2011-11-04 11:57:54 +00:00
|
|
|
return TRUE;
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
if(msg == WM_MOUSEMOVE) {
|
|
|
|
TRACKMOUSEEVENT tracker = { sizeof(TRACKMOUSEEVENT), TME_LEAVE, hwnd };
|
|
|
|
TrackMouseEvent(&tracker);
|
|
|
|
if(canvas.onMouseMove) canvas.onMouseMove({ (int16_t)LOWORD(lparam), (int16_t)HIWORD(lparam) });
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg == WM_MOUSELEAVE) {
|
|
|
|
if(canvas.onMouseLeave) canvas.onMouseLeave();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) {
|
|
|
|
if(canvas.onMousePress) switch(msg) {
|
|
|
|
case WM_LBUTTONDOWN: canvas.onMousePress(Mouse::Button::Left); break;
|
|
|
|
case WM_MBUTTONDOWN: canvas.onMousePress(Mouse::Button::Middle); break;
|
|
|
|
case WM_RBUTTONDOWN: canvas.onMousePress(Mouse::Button::Right); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg == WM_LBUTTONUP || msg == WM_MBUTTONUP || msg == WM_RBUTTONUP) {
|
|
|
|
if(canvas.onMouseRelease) switch(msg) {
|
|
|
|
case WM_LBUTTONUP: canvas.onMouseRelease(Mouse::Button::Left); break;
|
|
|
|
case WM_MBUTTONUP: canvas.onMouseRelease(Mouse::Button::Middle); break;
|
|
|
|
case WM_RBUTTONUP: canvas.onMouseRelease(Mouse::Button::Right); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 11:57:54 +00:00
|
|
|
return DefWindowProc(hwnd, msg, wparam, lparam);
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 11:57:54 +00:00
|
|
|
void pCanvas::setSize(const Size &size) {
|
|
|
|
delete[] data;
|
|
|
|
data = new uint32_t[size.width * size.height];
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pCanvas::update() {
|
2011-12-12 10:59:53 +00:00
|
|
|
memcpy(data, canvas.state.data, canvas.state.width * canvas.state.height * sizeof(uint32_t));
|
2011-04-27 08:57:31 +00:00
|
|
|
InvalidateRect(hwnd, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pCanvas::constructor() {
|
2011-11-04 11:57:54 +00:00
|
|
|
data = new uint32_t[canvas.state.width * canvas.state.height];
|
|
|
|
memcpy(data, canvas.state.data, canvas.state.width * canvas.state.height * sizeof(uint32_t));
|
2011-09-05 03:48:23 +00:00
|
|
|
hwnd = CreateWindow(L"phoenix_canvas", L"", WS_CHILD, 0, 0, 0, 0, parentWindow->p.hwnd, (HMENU)id, GetModuleHandle(0), 0);
|
|
|
|
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&canvas);
|
|
|
|
synchronize();
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 03:48:23 +00:00
|
|
|
void pCanvas::destructor() {
|
|
|
|
DestroyWindow(hwnd);
|
2011-11-04 11:57:54 +00:00
|
|
|
delete[] data;
|
2011-09-05 03:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pCanvas::orphan() {
|
|
|
|
destructor();
|
|
|
|
constructor();
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
2011-12-12 10:59:53 +00:00
|
|
|
|
|
|
|
void pCanvas::paint() {
|
|
|
|
RECT rc;
|
|
|
|
GetClientRect(hwnd, &rc);
|
|
|
|
unsigned width = canvas.state.width, height = canvas.state.height;
|
|
|
|
|
|
|
|
BITMAPINFO bmi;
|
|
|
|
memset(&bmi, 0, sizeof(BITMAPINFO));
|
|
|
|
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
|
|
bmi.bmiHeader.biPlanes = 1;
|
|
|
|
bmi.bmiHeader.biBitCount = 32;
|
|
|
|
bmi.bmiHeader.biCompression = BI_RGB;
|
|
|
|
bmi.bmiHeader.biWidth = width;
|
|
|
|
bmi.bmiHeader.biHeight = -height; //GDI stores bitmaps upside now; negative height flips bitmap
|
|
|
|
bmi.bmiHeader.biSizeImage = sizeof(uint32_t) * width * height;
|
|
|
|
|
|
|
|
PAINTSTRUCT ps;
|
|
|
|
BeginPaint(hwnd, &ps);
|
|
|
|
SetDIBitsToDevice(ps.hdc, 0, 0, width, height, 0, 0, 0, height, (void*)data, &bmi, DIB_RGB_COLORS);
|
|
|
|
EndPaint(hwnd, &ps);
|
|
|
|
}
|