src/events/SDL_windowevents.c
author Sam Lantinga
Mon, 21 Feb 2011 22:50:15 -0800
changeset 5383 40c76519000e
parent 5276 8e421890cdb8
child 5398 83816c8a2b58
permissions -rw-r--r--
Clear the hidden flag when shown and the shown flag when hidden.
     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 (data1 == window->x && data2 == window->y) {
    93             return 0;
    94         }
    95         window->x = data1;
    96         window->y = data2;
    97         break;
    98     case SDL_WINDOWEVENT_RESIZED:
    99         if (data1 == window->w && data2 == window->h) {
   100             return 0;
   101         }
   102         window->w = data1;
   103         window->h = data2;
   104         SDL_OnWindowResized(window);
   105         break;
   106     case SDL_WINDOWEVENT_MINIMIZED:
   107         if (window->flags & SDL_WINDOW_MINIMIZED) {
   108             return 0;
   109         }
   110         window->flags |= SDL_WINDOW_MINIMIZED;
   111         SDL_OnWindowMinimized(window);
   112         break;
   113     case SDL_WINDOWEVENT_MAXIMIZED:
   114         if (window->flags & SDL_WINDOW_MAXIMIZED) {
   115             return 0;
   116         }
   117         window->flags |= SDL_WINDOW_MAXIMIZED;
   118         break;
   119     case SDL_WINDOWEVENT_RESTORED:
   120         if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
   121             return 0;
   122         }
   123         window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
   124         SDL_OnWindowRestored(window);
   125         break;
   126     case SDL_WINDOWEVENT_ENTER:
   127         if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
   128             return 0;
   129         }
   130         window->flags |= SDL_WINDOW_MOUSE_FOCUS;
   131         break;
   132     case SDL_WINDOWEVENT_LEAVE:
   133         if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
   134             return 0;
   135         }
   136         window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
   137         break;
   138     case SDL_WINDOWEVENT_FOCUS_GAINED:
   139         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   140             return 0;
   141         }
   142         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   143         SDL_OnWindowFocusGained(window);
   144         break;
   145     case SDL_WINDOWEVENT_FOCUS_LOST:
   146         if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   147             return 0;
   148         }
   149         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
   150         SDL_OnWindowFocusLost(window);
   151         break;
   152     }
   153 
   154     /* Post the event, if desired */
   155     posted = 0;
   156     if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
   157         SDL_Event event;
   158         event.type = SDL_WINDOWEVENT;
   159         event.window.event = windowevent;
   160         event.window.data1 = data1;
   161         event.window.data2 = data2;
   162         event.window.windowID = window->id;
   163 
   164         /* Fixes queue overflow with resize events that aren't processed */
   165         if (windowevent == SDL_WINDOWEVENT_RESIZED) {
   166             SDL_FilterEvents(RemovePendingSizeEvents, &event);
   167         }
   168         if (windowevent == SDL_WINDOWEVENT_MOVED) {
   169             SDL_FilterEvents(RemovePendingMoveEvents, &event);
   170         }
   171 
   172         posted = (SDL_PushEvent(&event) > 0);
   173     }
   174 	
   175 	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
   176 		if ( !window->prev && !window->next ) {
   177 			// This is the last window in the list so send the SDL_QUIT event
   178 			SDL_SendQuit();
   179 		}
   180 	}
   181 
   182     return (posted);
   183 }
   184 
   185 /* vi: set ts=4 sw=4 expandtab: */