src/events/SDL_windowevents.c
author Sam Lantinga
Tue, 29 Mar 2011 03:05:26 -0700
changeset 5525 e427bb6266a4
parent 5524 b7f53a566ef1
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed compile error.
     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_HIDDEN;
    76         window->flags |= SDL_WINDOW_SHOWN;
    77         SDL_OnWindowShown(window);
    78         break;
    79     case SDL_WINDOWEVENT_HIDDEN:
    80         if (!(window->flags & SDL_WINDOW_SHOWN)) {
    81             return 0;
    82         }
    83         window->flags &= ~SDL_WINDOW_SHOWN;
    84         window->flags |= SDL_WINDOW_HIDDEN;
    85         SDL_OnWindowHidden(window);
    86         break;
    87     case SDL_WINDOWEVENT_MOVED:
    88         if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
    89             SDL_WINDOWPOS_ISUNDEFINED(data2)) {
    90             return 0;
    91         }
    92         if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
    93             window->windowed.x = data1;
    94             window->windowed.y = data2;
    95         }
    96         if (data1 == window->x && data2 == window->y) {
    97             return 0;
    98         }
    99         window->x = data1;
   100         window->y = data2;
   101         break;
   102     case SDL_WINDOWEVENT_RESIZED:
   103         if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
   104             window->windowed.w = data1;
   105             window->windowed.h = data2;
   106         }
   107         if (data1 == window->w && data2 == window->h) {
   108             return 0;
   109         }
   110         window->w = data1;
   111         window->h = data2;
   112         SDL_OnWindowResized(window);
   113         break;
   114     case SDL_WINDOWEVENT_MINIMIZED:
   115         if (window->flags & SDL_WINDOW_MINIMIZED) {
   116             return 0;
   117         }
   118         window->flags |= SDL_WINDOW_MINIMIZED;
   119         SDL_OnWindowMinimized(window);
   120         break;
   121     case SDL_WINDOWEVENT_MAXIMIZED:
   122         if (window->flags & SDL_WINDOW_MAXIMIZED) {
   123             return 0;
   124         }
   125         window->flags |= SDL_WINDOW_MAXIMIZED;
   126         break;
   127     case SDL_WINDOWEVENT_RESTORED:
   128         if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
   129             return 0;
   130         }
   131         window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
   132         SDL_OnWindowRestored(window);
   133         break;
   134     case SDL_WINDOWEVENT_ENTER:
   135         if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
   136             return 0;
   137         }
   138         window->flags |= SDL_WINDOW_MOUSE_FOCUS;
   139         break;
   140     case SDL_WINDOWEVENT_LEAVE:
   141         if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
   142             return 0;
   143         }
   144         window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
   145         break;
   146     case SDL_WINDOWEVENT_FOCUS_GAINED:
   147         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   148             return 0;
   149         }
   150         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   151         SDL_OnWindowFocusGained(window);
   152         break;
   153     case SDL_WINDOWEVENT_FOCUS_LOST:
   154         if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   155             return 0;
   156         }
   157         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
   158         SDL_OnWindowFocusLost(window);
   159         break;
   160     }
   161 
   162     /* Post the event, if desired */
   163     posted = 0;
   164     if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
   165         SDL_Event event;
   166         event.type = SDL_WINDOWEVENT;
   167         event.window.event = windowevent;
   168         event.window.data1 = data1;
   169         event.window.data2 = data2;
   170         event.window.windowID = window->id;
   171 
   172         /* Fixes queue overflow with resize events that aren't processed */
   173         if (windowevent == SDL_WINDOWEVENT_RESIZED ||
   174             windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) {
   175             SDL_FilterEvents(RemovePendingSizeEvents, &event);
   176         }
   177         if (windowevent == SDL_WINDOWEVENT_MOVED) {
   178             SDL_FilterEvents(RemovePendingMoveEvents, &event);
   179         }
   180 
   181         posted = (SDL_PushEvent(&event) > 0);
   182     }
   183 	
   184 	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
   185 		if ( !window->prev && !window->next ) {
   186 			// This is the last window in the list so send the SDL_QUIT event
   187 			SDL_SendQuit();
   188 		}
   189 	}
   190 
   191     return (posted);
   192 }
   193 
   194 /* vi: set ts=4 sw=4 expandtab: */