src/events/SDL_windowevents.c
author Sam Lantinga
Mon, 28 Mar 2011 23:44:51 -0700
changeset 5524 b7f53a566ef1
parent 5398 83816c8a2b58
child 5525 e427bb6266a4
permissions -rw-r--r--
Filter out SDL_WINDOWEVENT_SIZE_CHANGED events too
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@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@5276
    38
        (event->window.event == SDL_WINDOWEVENT_RESIZED ||
slouken@5276
    39
         event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) &&
slouken@3573
    40
        event->window.windowID == new_event->window.windowID) {
slouken@3573
    41
        /* We're about to post a new size event, drop the old one */
slouken@3573
    42
        return 0;
slouken@3573
    43
    }
slouken@3573
    44
    return 1;
slouken@3573
    45
}
slouken@3573
    46
slouken@5042
    47
static int
slouken@5042
    48
RemovePendingMoveEvents(void * userdata, SDL_Event *event)
slouken@5042
    49
{
slouken@5042
    50
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@5042
    51
slouken@5042
    52
    if (event->type == SDL_WINDOWEVENT &&
slouken@5042
    53
        event->window.event == SDL_WINDOWEVENT_MOVED &&
slouken@5042
    54
        event->window.windowID == new_event->window.windowID) {
slouken@5042
    55
        /* We're about to post a new move event, drop the old one */
slouken@5042
    56
        return 0;
slouken@5042
    57
    }
slouken@5042
    58
    return 1;
slouken@5042
    59
}
slouken@5042
    60
slouken@1895
    61
int
slouken@3685
    62
SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
slouken@1895
    63
                    int data2)
slouken@1895
    64
{
slouken@1895
    65
    int posted;
slouken@1895
    66
slouken@1895
    67
    if (!window) {
slouken@1895
    68
        return 0;
slouken@1895
    69
    }
slouken@1895
    70
    switch (windowevent) {
slouken@1895
    71
    case SDL_WINDOWEVENT_SHOWN:
slouken@1895
    72
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1895
    73
            return 0;
slouken@1895
    74
        }
slouken@5383
    75
        window->flags &= ~SDL_WINDOW_HIDDEN;
slouken@1895
    76
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
    77
        SDL_OnWindowShown(window);
slouken@1895
    78
        break;
slouken@1895
    79
    case SDL_WINDOWEVENT_HIDDEN:
slouken@1895
    80
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1895
    81
            return 0;
slouken@1895
    82
        }
slouken@1895
    83
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@5383
    84
        window->flags |= SDL_WINDOW_HIDDEN;
slouken@1895
    85
        SDL_OnWindowHidden(window);
slouken@1895
    86
        break;
slouken@1895
    87
    case SDL_WINDOWEVENT_MOVED:
slouken@5249
    88
        if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
slouken@5249
    89
            SDL_WINDOWPOS_ISUNDEFINED(data2)) {
slouken@1895
    90
            return 0;
slouken@1895
    91
        }
slouken@5398
    92
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
    93
            window->windowed.x = data1;
slouken@5398
    94
            window->windowed.y = data2;
slouken@5398
    95
        }
slouken@1895
    96
        if (data1 == window->x && data2 == window->y) {
slouken@1895
    97
            return 0;
slouken@1895
    98
        }
slouken@1915
    99
        window->x = data1;
slouken@1915
   100
        window->y = data2;
slouken@1895
   101
        break;
slouken@1895
   102
    case SDL_WINDOWEVENT_RESIZED:
slouken@5398
   103
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
   104
            window->windowed.w = data1;
slouken@5398
   105
            window->windowed.h = data2;
slouken@5398
   106
        }
slouken@1895
   107
        if (data1 == window->w && data2 == window->h) {
slouken@1895
   108
            return 0;
slouken@1895
   109
        }
slouken@1915
   110
        window->w = data1;
slouken@1915
   111
        window->h = data2;
slouken@5166
   112
        SDL_OnWindowResized(window);
slouken@1895
   113
        break;
slouken@1895
   114
    case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   115
        if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   116
            return 0;
slouken@1895
   117
        }
slouken@1895
   118
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@3502
   119
        SDL_OnWindowMinimized(window);
slouken@1895
   120
        break;
slouken@1895
   121
    case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1895
   122
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   123
            return 0;
slouken@1895
   124
        }
slouken@1895
   125
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   126
        break;
slouken@1895
   127
    case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   128
        if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
slouken@1895
   129
            return 0;
slouken@1895
   130
        }
slouken@1895
   131
        window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
slouken@3502
   132
        SDL_OnWindowRestored(window);
slouken@1895
   133
        break;
slouken@1895
   134
    case SDL_WINDOWEVENT_ENTER:
slouken@1895
   135
        if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   136
            return 0;
slouken@1895
   137
        }
slouken@1895
   138
        window->flags |= SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   139
        break;
slouken@1895
   140
    case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   141
        if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
slouken@1895
   142
            return 0;
slouken@1895
   143
        }
slouken@1895
   144
        window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   145
        break;
slouken@1895
   146
    case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   147
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   148
            return 0;
slouken@1895
   149
        }
slouken@1895
   150
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   151
        SDL_OnWindowFocusGained(window);
slouken@1895
   152
        break;
slouken@1895
   153
    case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   154
        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   155
            return 0;
slouken@1895
   156
        }
slouken@1895
   157
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   158
        SDL_OnWindowFocusLost(window);
slouken@1895
   159
        break;
slouken@1895
   160
    }
slouken@1895
   161
slouken@1895
   162
    /* Post the event, if desired */
slouken@1895
   163
    posted = 0;
slouken@4429
   164
    if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
slouken@1895
   165
        SDL_Event event;
slouken@1895
   166
        event.type = SDL_WINDOWEVENT;
slouken@1895
   167
        event.window.event = windowevent;
slouken@1895
   168
        event.window.data1 = data1;
slouken@1895
   169
        event.window.data2 = data2;
slouken@3685
   170
        event.window.windowID = window->id;
slouken@3573
   171
slouken@3573
   172
        /* Fixes queue overflow with resize events that aren't processed */
slouken@5524
   173
        if (windowevent == SDL_WINDOWEVENT_RESIZED ||
slouken@5524
   174
            windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) &&
slouken@3573
   175
            SDL_FilterEvents(RemovePendingSizeEvents, &event);
slouken@3573
   176
        }
slouken@5042
   177
        if (windowevent == SDL_WINDOWEVENT_MOVED) {
slouken@5042
   178
            SDL_FilterEvents(RemovePendingMoveEvents, &event);
slouken@5042
   179
        }
slouken@3573
   180
slouken@1895
   181
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   182
    }
krogoway@5113
   183
	
krogoway@5113
   184
	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
krogoway@5113
   185
		if ( !window->prev && !window->next ) {
krogoway@5113
   186
			// This is the last window in the list so send the SDL_QUIT event
krogoway@5113
   187
			SDL_SendQuit();
krogoway@5113
   188
		}
krogoway@5113
   189
	}
krogoway@5113
   190
slouken@1895
   191
    return (posted);
slouken@1895
   192
}
slouken@1895
   193
slouken@1895
   194
/* vi: set ts=4 sw=4 expandtab: */