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