#include <gtk/gtk.h>
#include <gdk/gdkwin32.h>
#include <Windows.h>

unsigned int appWidth = 640;
unsigned int appHeight = 400;
HWND gWnd = NULL;
WNDCLASSEX wcex;
#define CLASS_NAME "TEST"

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hWnd, message, wParam, lParam);
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, NULL);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = CLASS_NAME;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, NULL);

    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    RECT inRect = { 0, 0, appWidth, appHeight };
    AdjustWindowRect(&inRect, WS_OVERLAPPEDWINDOW, FALSE);

    gWnd = CreateWindow(CLASS_NAME, "Test Window", WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0, inRect.right - inRect.left,
                        inRect.bottom - inRect.top, NULL, NULL, hInstance, NULL);

    if (!gWnd) {
        return FALSE;
    }

    return TRUE;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    GdkWindow *win;
    int x, y;

    gtk_init(NULL, NULL);

    MyRegisterClass(hInstance);
    if (!InitInstance(hInstance, nCmdShow))
    {
        return 1;
    }

    ShowWindow(gWnd, nCmdShow);

    win = gdk_win32_window_foreign_new_for_display(gdk_display_get_default(), gWnd);
    g_print("win: %d", win);
    gdk_window_get_origin(win, &x, &y);
    g_print("x, y: %dx%d", x, y);

    gtk_main();

    return 0;
}
