src/video/windows/SDL_windowsmouse.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Feb 2011 23:50:32 -0800
changeset 5421 f8588afb6486
parent 5336 66d3c4a6d6f8
child 5472 674d45edfaeb
permissions -rwxr-xr-x
Added support for Windows cursors
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@2710
    22
slouken@1895
    23
#include "SDL_config.h"
slouken@1895
    24
slouken@5062
    25
#include "SDL_windowsvideo.h"
slouken@1895
    26
slouken@5421
    27
#include "../../events/SDL_mouse_c.h"
slouken@5421
    28
slouken@5421
    29
slouken@5421
    30
HCURSOR SDL_cursor = NULL;
slouken@5421
    31
slouken@5421
    32
slouken@5421
    33
static SDL_Cursor *
slouken@5421
    34
WIN_CreateDefaultCursor()
slouken@5421
    35
{
slouken@5421
    36
    SDL_Cursor *cursor;
slouken@5421
    37
slouken@5421
    38
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
    39
    if (cursor) {
slouken@5421
    40
        cursor->driverdata = LoadCursor(NULL, IDC_ARROW);
slouken@5421
    41
    } else {
slouken@5421
    42
        SDL_OutOfMemory();
slouken@5421
    43
    }
slouken@5421
    44
slouken@5421
    45
    return cursor;
slouken@5421
    46
}
slouken@5421
    47
slouken@5421
    48
static SDL_Cursor *
slouken@5421
    49
WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5421
    50
{
slouken@5421
    51
    SDL_Cursor *cursor;
slouken@5421
    52
    SDL_Surface *cvt;
slouken@5421
    53
    HICON hicon;
slouken@5421
    54
    HDC hdc;
slouken@5421
    55
    BITMAPV4HEADER bmh;
slouken@5421
    56
    LPVOID pixels;
slouken@5421
    57
    ICONINFO ii;
slouken@5421
    58
slouken@5421
    59
    SDL_zero(bmh);
slouken@5421
    60
    bmh.bV4Size = sizeof(bmh);
slouken@5421
    61
    bmh.bV4Width = surface->w;
slouken@5421
    62
    bmh.bV4Height = -surface->h; /* Invert the image */
slouken@5421
    63
    bmh.bV4Planes = 1;
slouken@5421
    64
    bmh.bV4BitCount = 32;
slouken@5421
    65
    bmh.bV4V4Compression = BI_BITFIELDS;
slouken@5421
    66
    bmh.bV4AlphaMask = 0xFF000000;
slouken@5421
    67
    bmh.bV4RedMask   = 0x00FF0000;
slouken@5421
    68
    bmh.bV4GreenMask = 0x0000FF00;
slouken@5421
    69
    bmh.bV4BlueMask  = 0x000000FF;
slouken@5421
    70
slouken@5421
    71
    hdc = GetDC(NULL);
slouken@5421
    72
    SDL_zero(ii);
slouken@5421
    73
    ii.fIcon = FALSE;
slouken@5421
    74
    ii.xHotspot = (DWORD)hot_x;
slouken@5421
    75
    ii.yHotspot = (DWORD)hot_y;
slouken@5421
    76
    ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
slouken@5421
    77
    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL);
slouken@5421
    78
    ReleaseDC(NULL, hdc);
slouken@5421
    79
slouken@5421
    80
    cvt = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@5421
    81
    if (!cvt) {
slouken@5421
    82
        return NULL;
slouken@5421
    83
    }
slouken@5421
    84
    SDL_memcpy(pixels, cvt->pixels, cvt->h * cvt->pitch);
slouken@5421
    85
    SDL_FreeSurface(cvt);
slouken@5421
    86
slouken@5421
    87
    hicon = CreateIconIndirect(&ii);
slouken@5421
    88
slouken@5421
    89
    DeleteObject(ii.hbmColor);
slouken@5421
    90
    DeleteObject(ii.hbmMask);
slouken@5421
    91
slouken@5421
    92
    if (!hicon) {
slouken@5421
    93
        WIN_SetError("CreateIconIndirect()");
slouken@5421
    94
        return NULL;
slouken@5421
    95
    }
slouken@5421
    96
slouken@5421
    97
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
    98
    if (cursor) {
slouken@5421
    99
        cursor->driverdata = hicon;
slouken@5421
   100
    } else {
slouken@5421
   101
        DestroyIcon(hicon);
slouken@5421
   102
        SDL_OutOfMemory();
slouken@5421
   103
    }
slouken@5421
   104
slouken@5421
   105
    return cursor;
slouken@5421
   106
}
slouken@5421
   107
slouken@5421
   108
static void
slouken@5421
   109
WIN_FreeCursor(SDL_Cursor * cursor)
slouken@5421
   110
{
slouken@5421
   111
    HICON hicon = (HICON)cursor->driverdata;
slouken@5421
   112
slouken@5421
   113
    DestroyIcon(hicon);
slouken@5421
   114
    SDL_free(cursor);
slouken@5421
   115
}
slouken@5421
   116
slouken@5421
   117
static int
slouken@5421
   118
WIN_ShowCursor(SDL_Cursor * cursor)
slouken@5421
   119
{
slouken@5421
   120
    if (cursor) {
slouken@5421
   121
        SDL_cursor = (HCURSOR)cursor->driverdata;
slouken@5421
   122
    } else {
slouken@5421
   123
        SDL_cursor = NULL;
slouken@5421
   124
    }
slouken@5421
   125
    if (SDL_GetMouseFocus() != NULL) {
slouken@5421
   126
        SetCursor(SDL_cursor);
slouken@5421
   127
    }
slouken@5421
   128
    return 0;
slouken@5421
   129
}
slouken@5421
   130
slouken@5421
   131
static void
slouken@5421
   132
WIN_WarpMouse(SDL_Window * window, int x, int y)
slouken@5421
   133
{
slouken@5421
   134
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@5421
   135
    POINT pt;
slouken@5421
   136
slouken@5421
   137
    pt.x = x;
slouken@5421
   138
    pt.y = y;
slouken@5421
   139
    ClientToScreen(hwnd, &pt);
slouken@5421
   140
    SetCursorPos(pt.x, pt.y);
slouken@5421
   141
}
slouken@5421
   142
slouken@5421
   143
static int
slouken@5421
   144
WIN_SetRelativeMouseMode(SDL_bool enabled)
slouken@5421
   145
{
slouken@5421
   146
    SDL_Unsupported();
slouken@5421
   147
    return -1;
slouken@5421
   148
}
slouken@5421
   149
slouken@1895
   150
void
slouken@1895
   151
WIN_InitMouse(_THIS)
slouken@1895
   152
{
slouken@5421
   153
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5421
   154
slouken@5421
   155
    mouse->CreateCursor = WIN_CreateCursor;
slouken@5421
   156
    mouse->ShowCursor = WIN_ShowCursor;
slouken@5421
   157
    mouse->FreeCursor = WIN_FreeCursor;
slouken@5421
   158
    mouse->WarpMouse = WIN_WarpMouse;
slouken@5421
   159
    mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
slouken@5421
   160
slouken@5421
   161
    SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
slouken@1895
   162
}
slouken@1895
   163
slouken@1895
   164
void
slouken@1895
   165
WIN_QuitMouse(_THIS)
slouken@1895
   166
{
slouken@1895
   167
}
slouken@1895
   168
slouken@1895
   169
/* vi: set ts=4 sw=4 expandtab: */