/*
* Gdiplus text measurement test program
* 
* Copyright (c) 2012 Vincent Povirk for CodeWeavers
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "windows.h"
#include "stdio.h"
#include "gdiplus.h"

int width;
int height;

static void draw_text(GpGraphics *graphics)
{
    static const WCHAR test_string[] = {'t','e','s','t',' ','s','t','r','i','n','g',0};
    RectF rc = {80,80,width*4-80,height*2-80};
    GpStringFormat *format;
    CharacterRange ranges[4] = {{0,1},{3,1},{5,1},{10,1}};
    RectF bounds;
    GpRegion *regions[4];
    GpFontFamily *family;
    GpFont *font;
    GpBrush *brush;
    GpPen *pen;
    int i;

    GdipScaleWorldTransform(graphics, 0.25, 0.25, MatrixOrderAppend);

    GdipGetGenericFontFamilySansSerif(&family);

    GdipCreateFont(family, 200, FontStyleRegular, UnitPixel, &font);

    GdipDeleteFontFamily(family);

    GdipCreateStringFormat(0, LANG_NEUTRAL, &format);

    GdipSetStringFormatMeasurableCharacterRanges(format, 4, ranges);

    for (i=0; i<4; i++)
    {
        GdipCreateRegion(&regions[i]);

        GdipSetEmpty(regions[i]);
    }

    GdipMeasureCharacterRanges(graphics, test_string, -1, font, &rc, format, 4, regions);

    GdipCreateSolidFill(0x80ff0000, (GpSolidFill**)&brush);

    for (i=0; i<4; i++)
    {
        GdipFillRegion(graphics, brush, regions[i]);

        GdipDeleteRegion(regions[i]);
    }

    GdipDeleteBrush(brush);

    GdipMeasureString(graphics, test_string, -1, font, &rc, format, &bounds, NULL, NULL);

    GdipCreatePen1(0xffffff00, 1, UnitPixel, &pen);

    GdipDrawRectangles(graphics, pen, &bounds, 1);

    GdipDeletePen(pen);

    GdipCreateSolidFill(0xff000000, (GpSolidFill**)&brush);

    GdipDrawString(graphics, test_string, -1, font, &rc, format, brush);

    GdipDeleteBrush(brush);

    GdipDeleteStringFormat(format);

    GdipDeleteFont(font);
}

static LRESULT CALLBACK testwindow_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch(msg)
    {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            GpGraphics *graphics;

            BeginPaint(hwnd, &ps);
            GdipCreateFromHDC(ps.hdc, &graphics);

            draw_text(graphics);

            GdipDeleteGraphics(graphics);
            EndPaint(hwnd, &ps);
            return 0;
        }
        case WM_SIZE:
            width = LOWORD(lparam);
            height = HIWORD(lparam);
            InvalidateRect(hwnd, NULL, TRUE); /* may not be necessary */
            break;
        case WM_LBUTTONDOWN:
            InvalidateRect(hwnd, NULL, TRUE);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    }

    return DefWindowProc(hwnd, msg, wparam, lparam);
}

static void loop(void)
{
    MSG msg;

    while (GetMessageA(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
    }
}

static void register_testwindow_class(void)
{
    WNDCLASSEXA cls;

    ZeroMemory(&cls, sizeof(cls));
    cls.cbSize = sizeof(cls);
    cls.style = 0;
    cls.lpfnWndProc = testwindow_wndproc;
    cls.hInstance = NULL;
    cls.hCursor = LoadCursor(0, IDC_ARROW);
    cls.hbrBackground = (HBRUSH) COLOR_WINDOW;
    cls.lpszClassName = "testwindow";

    RegisterClassExA(&cls);
}

static HWND create_window(LPSTR title)
{
   register_testwindow_class();
   
   return CreateWindowExA(0, "testwindow", title, WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
}
 
int main(int argc, char* argv[])
{
    struct GdiplusStartupInput input;
    ULONG_PTR token;

    input.GdiplusVersion = 1;
    input.DebugEventCallback = NULL;
    input.SuppressBackgroundThread = FALSE;
    input.SuppressExternalCodecs = TRUE;
    GdiplusStartup(&token, &input, NULL);

    create_window(argv[1]);
    
    loop();

    GdiplusShutdown(token);

    return 0;
}
