/**
cairo-gdi-demo.cpp

Demonstrates how to get Cairo Graphics working with the Windows API and GDI.
                     
Author: Andrew Lim
Email:  danteshamest@gmail.com
Site: windrealm.com
*/

#include <windows.h>
#include <cmath>
#include "cairo-win32.h"

/**
  Gradient demonstration.
  Taken from http://cairographics.org/samples/
*/
void gradientExample( cairo_t* cr ) {
  cairo_pattern_t *pat;

  pat = cairo_pattern_create_linear (0.0, 0.0,  0.0, 256.0);
  cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1);
  cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1);
  cairo_rectangle (cr, 0, 0, 256, 256);
  cairo_set_source (cr, pat);
  cairo_fill (cr);
  cairo_pattern_destroy (pat);

  pat = cairo_pattern_create_radial (115.2, 102.4, 25.6,
                                     102.4,  102.4, 128.0);
  cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1);
  cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1);
  cairo_set_source (cr, pat);
  cairo_arc (cr, 128.0, 128.0, 76.8, 0, 2 * M_PI);
  cairo_fill (cr);
  cairo_pattern_destroy (pat);
}

/**
  Changes the dimensions of a window's client area.
*/
void SetClientSize( HWND hwnd, int clientWidth, int clientHeight ) {
  if ( IsWindow( hwnd ) ) {
    DWORD dwStyle = GetWindowLongPtr( hwnd, GWL_STYLE ) ;
    DWORD dwExStyle = GetWindowLongPtr( hwnd, GWL_EXSTYLE ) ;
    HMENU menu = GetMenu( hwnd ) ;
    RECT rc = { 0, 0, clientWidth, clientHeight } ;
    AdjustWindowRectEx( &rc, dwStyle, menu ? TRUE : FALSE, dwExStyle );
    SetWindowPos( hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
                  SWP_NOZORDER | SWP_NOMOVE ) ;
  }
}

/**
  Handles WM_PAINT.
*/
LRESULT onPaint( HWND hwnd, WPARAM wParam, LPARAM lParam ) {
  PAINTSTRUCT ps ;
  HDC hdc = BeginPaint( hwnd, &ps );

  // Create the cairo surface and context.
  cairo_surface_t *surface = cairo_win32_surface_create (hdc);
  cairo_t *cr = cairo_create (surface);
  
  // Draw on the cairo context.
  gradientExample( cr );

  // Cleanup.
  cairo_destroy (cr);
  cairo_surface_destroy (surface);

  EndPaint( hwnd, &ps );
  return 0 ;
}

/**
  Handles WM_CLOSE.
*/
LRESULT onClose( HWND hwnd, WPARAM wParam, LPARAM lParam ) {
  PostQuitMessage( 0 );
  return 0 ;
}

/**
  Handles our window's messages.
*/
LRESULT CALLBACK WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam ) {
  switch(msg) {
    case WM_PAINT: return onPaint( hwnd, wParam, lParam );
    case WM_CLOSE: return onClose( hwnd, wParam, lParam );
    default: return DefWindowProc(hwnd,msg,wParam,lParam);
  }
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow ) {
  MSG  msg ;
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "CairoGdiWndClass" );
  wc.hInstance     = hInst ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);

  RegisterClass(&wc);
  HWND hwnd = CreateWindow( wc.lpszClassName,TEXT("Cairo & GDI Demo"),
                            WS_OVERLAPPEDWINDOW, 0,0,256,256,0,0,hInst,0);
  SetClientSize( hwnd, 256, 256 );
  ShowWindow( hwnd, SW_SHOWNORMAL );

  while( GetMessage(&msg,0,0,0) > 0 ) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int)msg.wParam;
}
