src/events/SDL_windowevents.c
author Sam Lantinga
Sat, 12 Feb 2011 19:02:14 -0800
changeset 5276 8e421890cdb8
parent 5262 b530ef003506
child 5383 40c76519000e
permissions -rw-r--r--
Fixed bug #1117

There's a new event that's always sent when the window changes size, and that event is what the renderers listen for to determine if they need to rebind their context.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Window event handling code for SDL */
    25 
    26 #include "SDL_events.h"
    27 #include "SDL_events_c.h"
    28 #include "SDL_mouse_c.h"
    29 #include "../video/SDL_sysvideo.h"
    30 
    31 
    32 static int
    33 RemovePendingSizeEvents(void * userdata, SDL_Event *event)
    34 {
    35     SDL_Event *new_event = (SDL_Event *)userdata;
    36 
    37     if (event->type == SDL_WINDOWEVENT &&
    38         (event->window.event == SDL_WINDOWEVENT_RESIZED ||
    39          event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) &&
    40         event->window.windowID == new_event->window.windowID) {
    41         /* We're about to post a new size event, drop the old one */
    42         return 0;
    43     }
    44     return 1;
    45 }
    46 
    47 static int
    48 RemovePendingMoveEvents(void * userdata, SDL_Event *event)
    49 {
    50     SDL_Event *new_event = (SDL_Event *)userdata;
    51 
    52     if (event->type == SDL_WINDOWEVENT &&
    53         event->window.event == SDL_WINDOWEVENT_MOVED &&
    54         event->window.windowID == new_event->window.windowID) {
    55         /* We're about to post a new move event, drop the old one */
    56         return 0;
    57     }
    58     return 1;
    59 }
    60 
    61 int
    62 SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
    63                     int data2)
    64 {
    65     int posted;
    66 
    67     if (!window) {
    68         return 0;
    69     }
    70     switch (windowevent) {
    71     case SDL_WINDOWEVENT_SHOWN:
    72         if (window->flags & SDL_WINDOW_SHOWN) {
    73             return 0;
    74         }
    75         window->flags |= SDL_WINDOW_SHOWN;
    76         SDL_OnWindowShown(window);
    77         break;
    78     case SDL_WINDOWEVENT_HIDDEN:
    79         if (!(window->flags & SDL_WINDOW_SHOWN)) {
    80             return 0;
    81         }
    82         window->flags &= ~SDL_WINDOW_SHOWN;
    83         SDL_OnWindowHidden(window);
    84         break;
    85     case SDL_WINDOWEVENT_MOVED:
    86         if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
    87             SDL_WINDOWPOS_ISUNDEFINED(data2)) {
    88             return 0;
    89         }
    90         if (data1 == window->x && data2 == window->y) {
    91             return 0;
    92         }
    93         window->x = data1;
    94         window->y = data2;
    95         break;
    96     case SDL_WINDOWEVENT_RESIZED:
    97         if (data1 == window->w && data2 == window->h) {
    98             return 0;
    99         }
   100         window->w = data1;
   101         window->h = data2;
   102         SDL_OnWindowResized(window);
   103         break;
   104     case SDL_WINDOWEVENT_MINIMIZED:
   105         if (window->flags & SDL_WINDOW_MINIMIZED) {
   106             return 0;
   107         }
   108         window->flags |= SDL_WINDOW_MINIMIZED;
   109         SDL_OnWindowMinimized(window);
   110         break;
   111     case SDL_WINDOWEVENT_MAXIMIZED:
   112         if (window->flags & SDL_WINDOW_MAXIMIZED) {
   113             return 0;
   114         }
   115         window->flags |= SDL_WINDOW_MAXIMIZED;
   116         break;
   117     case SDL_WINDOWEVENT_RESTORED:
   118         if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
   119             return 0;
   120         }
   121         window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
   122         SDL_OnWindowRestored(window);
   123         break;
   124     case SDL_WINDOWEVENT_ENTER:
   125         if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
   126             return 0;
   127         }
   128         window->flags |= SDL_WINDOW_MOUSE_FOCUS;
   129         break;
   130     case SDL_WINDOWEVENT_LEAVE:
   131         if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
   132             return 0;
   133         }
   134         window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
   135         break;
   136     case SDL_WINDOWEVENT_FOCUS_GAINED:
   137         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   138             return 0;
   139         }
   140         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   141         SDL_OnWindowFocusGained(window);
   142         break;
   143     case SDL_WINDOWEVENT_FOCUS_LOST:
   144         if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   145             return 0;
   146         }
   147         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
   148         SDL_OnWindowFocusLost(window);
   149         break;
   150     }
   151 
   152     /* Post the event, if desired */
   153     posted = 0;
   154     if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
   155         SDL_Event event;
   156         event.type = SDL_WINDOWEVENT;
   157         event.window.event = windowevent;
   158         event.window.data1 = data1;
   159         event.window.data2 = data2;
   160         event.window.windowID = window->id;
   161 
   162         /* Fixes queue overflow with resize events that aren't processed */
   163         if (windowevent == SDL_WINDOWEVENT_RESIZED) {
   164             SDL_FilterEvents(RemovePendingSizeEvents, &event);
   165         }
   166         if (windowevent == SDL_WINDOWEVENT_MOVED) {
   167             SDL_FilterEvents(RemovePendingMoveEvents, &event);
   168         }
   169 
   170         posted = (SDL_PushEvent(&event) > 0);
   171     }
   172 	
   173 	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
   174 		if ( !window->prev && !window->next ) {
   175 			// This is the last window in the list so send the SDL_QUIT event
   176 			SDL_SendQuit();
   177 		}
   178 	}
   179 
   180     return (posted);
   181 }
   182 
   183 /* vi: set ts=4 sw=4 expandtab: */