src/events/SDL_windowevents.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 5535 96594ac5fd1a
child 6512 4beb596ccfae
permissions -rwxr-xr-x
Happy New Year!
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@1895
    23
/* Window event handling code for SDL */
slouken@1895
    24
slouken@1895
    25
#include "SDL_events.h"
slouken@1895
    26
#include "SDL_events_c.h"
slouken@2794
    27
#include "SDL_mouse_c.h"
slouken@1895
    28
#include "../video/SDL_sysvideo.h"
slouken@1895
    29
slouken@3573
    30
slouken@3573
    31
static int
slouken@3573
    32
RemovePendingSizeEvents(void * userdata, SDL_Event *event)
slouken@3573
    33
{
slouken@3573
    34
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@3573
    35
slouken@3573
    36
    if (event->type == SDL_WINDOWEVENT &&
slouken@5276
    37
        (event->window.event == SDL_WINDOWEVENT_RESIZED ||
slouken@5276
    38
         event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) &&
slouken@3573
    39
        event->window.windowID == new_event->window.windowID) {
slouken@3573
    40
        /* We're about to post a new size event, drop the old one */
slouken@3573
    41
        return 0;
slouken@3573
    42
    }
slouken@3573
    43
    return 1;
slouken@3573
    44
}
slouken@3573
    45
slouken@5042
    46
static int
slouken@5042
    47
RemovePendingMoveEvents(void * userdata, SDL_Event *event)
slouken@5042
    48
{
slouken@5042
    49
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@5042
    50
slouken@5042
    51
    if (event->type == SDL_WINDOWEVENT &&
slouken@5042
    52
        event->window.event == SDL_WINDOWEVENT_MOVED &&
slouken@5042
    53
        event->window.windowID == new_event->window.windowID) {
slouken@5042
    54
        /* We're about to post a new move event, drop the old one */
slouken@5042
    55
        return 0;
slouken@5042
    56
    }
slouken@5042
    57
    return 1;
slouken@5042
    58
}
slouken@5042
    59
slouken@1895
    60
int
slouken@3685
    61
SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
slouken@1895
    62
                    int data2)
slouken@1895
    63
{
slouken@1895
    64
    int posted;
slouken@1895
    65
slouken@1895
    66
    if (!window) {
slouken@1895
    67
        return 0;
slouken@1895
    68
    }
slouken@1895
    69
    switch (windowevent) {
slouken@1895
    70
    case SDL_WINDOWEVENT_SHOWN:
slouken@1895
    71
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1895
    72
            return 0;
slouken@1895
    73
        }
slouken@5383
    74
        window->flags &= ~SDL_WINDOW_HIDDEN;
slouken@1895
    75
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
    76
        SDL_OnWindowShown(window);
slouken@1895
    77
        break;
slouken@1895
    78
    case SDL_WINDOWEVENT_HIDDEN:
slouken@1895
    79
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1895
    80
            return 0;
slouken@1895
    81
        }
slouken@1895
    82
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@5383
    83
        window->flags |= SDL_WINDOW_HIDDEN;
slouken@1895
    84
        SDL_OnWindowHidden(window);
slouken@1895
    85
        break;
slouken@1895
    86
    case SDL_WINDOWEVENT_MOVED:
slouken@5249
    87
        if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
slouken@5249
    88
            SDL_WINDOWPOS_ISUNDEFINED(data2)) {
slouken@1895
    89
            return 0;
slouken@1895
    90
        }
slouken@5398
    91
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
    92
            window->windowed.x = data1;
slouken@5398
    93
            window->windowed.y = data2;
slouken@5398
    94
        }
slouken@1895
    95
        if (data1 == window->x && data2 == window->y) {
slouken@1895
    96
            return 0;
slouken@1895
    97
        }
slouken@1915
    98
        window->x = data1;
slouken@1915
    99
        window->y = data2;
slouken@1895
   100
        break;
slouken@1895
   101
    case SDL_WINDOWEVENT_RESIZED:
slouken@5398
   102
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
   103
            window->windowed.w = data1;
slouken@5398
   104
            window->windowed.h = data2;
slouken@5398
   105
        }
slouken@1895
   106
        if (data1 == window->w && data2 == window->h) {
slouken@1895
   107
            return 0;
slouken@1895
   108
        }
slouken@1915
   109
        window->w = data1;
slouken@1915
   110
        window->h = data2;
slouken@5166
   111
        SDL_OnWindowResized(window);
slouken@1895
   112
        break;
slouken@1895
   113
    case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   114
        if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   115
            return 0;
slouken@1895
   116
        }
slouken@1895
   117
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@3502
   118
        SDL_OnWindowMinimized(window);
slouken@1895
   119
        break;
slouken@1895
   120
    case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1895
   121
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   122
            return 0;
slouken@1895
   123
        }
slouken@1895
   124
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   125
        break;
slouken@1895
   126
    case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   127
        if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
slouken@1895
   128
            return 0;
slouken@1895
   129
        }
slouken@1895
   130
        window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
slouken@3502
   131
        SDL_OnWindowRestored(window);
slouken@1895
   132
        break;
slouken@1895
   133
    case SDL_WINDOWEVENT_ENTER:
slouken@1895
   134
        if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   135
            return 0;
slouken@1895
   136
        }
slouken@1895
   137
        window->flags |= SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   138
        break;
slouken@1895
   139
    case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   140
        if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
slouken@1895
   141
            return 0;
slouken@1895
   142
        }
slouken@1895
   143
        window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   144
        break;
slouken@1895
   145
    case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   146
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   147
            return 0;
slouken@1895
   148
        }
slouken@1895
   149
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   150
        SDL_OnWindowFocusGained(window);
slouken@1895
   151
        break;
slouken@1895
   152
    case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   153
        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   154
            return 0;
slouken@1895
   155
        }
slouken@1895
   156
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   157
        SDL_OnWindowFocusLost(window);
slouken@1895
   158
        break;
slouken@1895
   159
    }
slouken@1895
   160
slouken@1895
   161
    /* Post the event, if desired */
slouken@1895
   162
    posted = 0;
slouken@4429
   163
    if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
slouken@1895
   164
        SDL_Event event;
slouken@1895
   165
        event.type = SDL_WINDOWEVENT;
slouken@1895
   166
        event.window.event = windowevent;
slouken@1895
   167
        event.window.data1 = data1;
slouken@1895
   168
        event.window.data2 = data2;
slouken@3685
   169
        event.window.windowID = window->id;
slouken@3573
   170
slouken@3573
   171
        /* Fixes queue overflow with resize events that aren't processed */
slouken@5524
   172
        if (windowevent == SDL_WINDOWEVENT_RESIZED ||
slouken@5525
   173
            windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@3573
   174
            SDL_FilterEvents(RemovePendingSizeEvents, &event);
slouken@3573
   175
        }
slouken@5042
   176
        if (windowevent == SDL_WINDOWEVENT_MOVED) {
slouken@5042
   177
            SDL_FilterEvents(RemovePendingMoveEvents, &event);
slouken@5042
   178
        }
slouken@3573
   179
slouken@1895
   180
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   181
    }
krogoway@5113
   182
	
krogoway@5113
   183
	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
krogoway@5113
   184
		if ( !window->prev && !window->next ) {
krogoway@5113
   185
			// This is the last window in the list so send the SDL_QUIT event
krogoway@5113
   186
			SDL_SendQuit();
krogoway@5113
   187
		}
krogoway@5113
   188
	}
krogoway@5113
   189
slouken@1895
   190
    return (posted);
slouken@1895
   191
}
slouken@1895
   192
slouken@1895
   193
/* vi: set ts=4 sw=4 expandtab: */