src/events/SDL_windowevents.c
author krogoway
Thu, 27 Jan 2011 15:58:30 -0600
changeset 5113 686d795b1b29
parent 5042 8c39b82dc7b0
child 5147 ad50b3db78bd
permissions -rw-r--r--
When the last window is closed and the SDL_WINDOWEVENT_CLOSE event is sent, send the SDL_QUIT event.
Common.c now destroys the SDL_Window upon a SDL_WINDOWEVENT_CLOSE event to ensure that all windows get closed properly and the new code to handle the last window closes gets executed.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
/* Window event handling code for SDL */
slouken@1895
    25
slouken@1895
    26
#include "SDL_events.h"
slouken@1895
    27
#include "SDL_events_c.h"
slouken@2794
    28
#include "SDL_mouse_c.h"
slouken@1895
    29
#include "../video/SDL_sysvideo.h"
slouken@1895
    30
slouken@3573
    31
slouken@3573
    32
static int
slouken@3573
    33
RemovePendingSizeEvents(void * userdata, SDL_Event *event)
slouken@3573
    34
{
slouken@3573
    35
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@3573
    36
slouken@3573
    37
    if (event->type == SDL_WINDOWEVENT &&
slouken@3573
    38
        event->window.event == SDL_WINDOWEVENT_RESIZED &&
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@1895
    74
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
    75
        SDL_OnWindowShown(window);
slouken@1895
    76
        break;
slouken@1895
    77
    case SDL_WINDOWEVENT_HIDDEN:
slouken@1895
    78
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1895
    79
            return 0;
slouken@1895
    80
        }
slouken@1895
    81
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
    82
        SDL_OnWindowHidden(window);
slouken@1895
    83
        break;
slouken@1895
    84
    case SDL_WINDOWEVENT_MOVED:
slouken@1895
    85
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
    86
            return 0;
slouken@1895
    87
        }
slouken@2875
    88
        if (data1 == SDL_WINDOWPOS_UNDEFINED) {
slouken@2875
    89
            data1 = window->x;
slouken@2875
    90
        }
slouken@2875
    91
        if (data2 == SDL_WINDOWPOS_UNDEFINED) {
slouken@2875
    92
            data2 = window->y;
slouken@2875
    93
        }
slouken@1895
    94
        if (data1 == window->x && data2 == window->y) {
slouken@1895
    95
            return 0;
slouken@1895
    96
        }
slouken@1915
    97
        window->x = data1;
slouken@1915
    98
        window->y = data2;
slouken@1895
    99
        break;
slouken@1895
   100
    case SDL_WINDOWEVENT_RESIZED:
slouken@1895
   101
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   102
            return 0;
slouken@1895
   103
        }
slouken@1895
   104
        if (data1 == window->w && data2 == window->h) {
slouken@1895
   105
            return 0;
slouken@1895
   106
        }
slouken@1915
   107
        window->w = data1;
slouken@1915
   108
        window->h = data2;
slouken@1970
   109
        SDL_OnWindowResized(window);
slouken@1895
   110
        break;
slouken@1895
   111
    case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   112
        if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   113
            return 0;
slouken@1895
   114
        }
slouken@1895
   115
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@3502
   116
        SDL_OnWindowMinimized(window);
slouken@1895
   117
        break;
slouken@1895
   118
    case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1895
   119
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   120
            return 0;
slouken@1895
   121
        }
slouken@1895
   122
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   123
        break;
slouken@1895
   124
    case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   125
        if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
slouken@1895
   126
            return 0;
slouken@1895
   127
        }
slouken@1895
   128
        window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
slouken@3502
   129
        SDL_OnWindowRestored(window);
slouken@1895
   130
        break;
slouken@1895
   131
    case SDL_WINDOWEVENT_ENTER:
slouken@1895
   132
        if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   133
            return 0;
slouken@1895
   134
        }
slouken@1895
   135
        window->flags |= SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   136
        break;
slouken@1895
   137
    case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   138
        if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
slouken@1895
   139
            return 0;
slouken@1895
   140
        }
slouken@1895
   141
        window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   142
        break;
slouken@1895
   143
    case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   144
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   145
            return 0;
slouken@1895
   146
        }
slouken@1895
   147
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   148
        SDL_OnWindowFocusGained(window);
slouken@1895
   149
        break;
slouken@1895
   150
    case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   151
        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   152
            return 0;
slouken@1895
   153
        }
slouken@1895
   154
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   155
        SDL_OnWindowFocusLost(window);
slouken@1895
   156
        break;
slouken@1895
   157
    }
slouken@1895
   158
slouken@1895
   159
    /* Post the event, if desired */
slouken@1895
   160
    posted = 0;
slouken@4429
   161
    if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
slouken@1895
   162
        SDL_Event event;
slouken@1895
   163
        event.type = SDL_WINDOWEVENT;
slouken@1895
   164
        event.window.event = windowevent;
slouken@1895
   165
        event.window.data1 = data1;
slouken@1895
   166
        event.window.data2 = data2;
slouken@3685
   167
        event.window.windowID = window->id;
slouken@3573
   168
slouken@3573
   169
        /* Fixes queue overflow with resize events that aren't processed */
slouken@3573
   170
        if (windowevent == SDL_WINDOWEVENT_RESIZED) {
slouken@3573
   171
            SDL_FilterEvents(RemovePendingSizeEvents, &event);
slouken@3573
   172
        }
slouken@5042
   173
        if (windowevent == SDL_WINDOWEVENT_MOVED) {
slouken@5042
   174
            SDL_FilterEvents(RemovePendingMoveEvents, &event);
slouken@5042
   175
        }
slouken@3573
   176
slouken@1895
   177
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   178
    }
krogoway@5113
   179
	
krogoway@5113
   180
	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
krogoway@5113
   181
		if ( !window->prev && !window->next ) {
krogoway@5113
   182
			// This is the last window in the list so send the SDL_QUIT event
krogoway@5113
   183
			SDL_SendQuit();
krogoway@5113
   184
		}
krogoway@5113
   185
	}
krogoway@5113
   186
slouken@1895
   187
    return (posted);
slouken@1895
   188
}
slouken@1895
   189
slouken@1895
   190
/* vi: set ts=4 sw=4 expandtab: */