//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
			{
				Graphics graphics(hdc);

				Color color;
				color.SetFromCOLORREF(params.m_ccBackground.rgbResult);
				graphics.Clear(color);

				HFONT fnIndirect = CreateFontIndirect(params.m_cf.lpLogFont);
				Font font(hdc, fnIndirect);
				if (params.m_bAntialiasing)
					graphics.SetSmoothingMode(SmoothingModeHighQuality);
				
				Matrix matrix;
				
				RectF rect;
				graphics.MeasureString(params.m_text.c_str(), params.m_text.length(), &font, params.m_pStart, &rect);
				matrix.Translate(params.m_pStart.X + rect.Width / 2, params.m_pStart.Y + rect.Height / 2);
				matrix.Rotate(params.m_fRotAngle);
				matrix.Scale(params.m_fScale, params.m_fScale, MatrixOrderAppend);
				graphics.MultiplyTransform(&matrix);

				StringFormat strformat;
				GraphicsPath path;
				FontFamily fnFamily;
				font.GetFamily(&fnFamily);
				path.AddString(params.m_text.c_str(), params.m_text.length(), &fnFamily, font.GetStyle(), font.GetSize(), PointF(-rect.Width / 2, -rect.Height / 2), strformat.GenericTypographic());

				color.SetFromCOLORREF(params.m_ccCircuit.rgbResult);
				Pen pen(color, 3);
				pen.SetLineJoin(LineJoinRound);

				graphics.DrawPath(&pen, &path);

				color.SetFromCOLORREF(params.m_ccFill.rgbResult);
				SolidBrush brush(color);
				graphics.FillPath(&brush, &path);
							
				Region region(&path);
				hRgn = region.GetHRGN(&graphics);

				graphics.ResetTransform();

				RECT rc;
				GetWindowRect(hWnd, &rc);

				POINT pt;
				pt.x = rc.left;
				pt.y = rc.top;
				ScreenToClient(hWnd, &pt);

				matrix.Translate((REAL)-pt.x, (REAL)-pt.y, MatrixOrderAppend);
				region.Transform(&matrix);
				
				HRGN hRgnFr = region.GetHRGN(&graphics);
				OffsetRect(&rc, -rc.left, -rc.top);

				/*HRGN hRgnWnd = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);

				CombineRgn(hRgn, hRgn, hRgnWnd, RGN_XOR);
				CombineRgn(hRgnFr, hRgnFr, hRgnWnd, RGN_XOR);*/
				SetWindowRgn(hWnd, params.m_bNonRectRg ? hRgnFr : NULL, TRUE);
			}
			EndPaint(hWnd, &ps);
        }
        break;
	case WM_LBUTTONDOWN:
		{
			int xPos = GET_X_LPARAM(lParam);
			int yPos = GET_Y_LPARAM(lParam);
				
			if (PtInRegion(hRgn, xPos, yPos))
			{
				DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_TEXT_PARAMS), hWnd, TextParams);
				InvalidateRect(hWnd, NULL, TRUE);
			}
		}
		break;
	case WM_ERASEBKGND:
		return TRUE;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}