src/events/SDL_windowevents.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:19:43 -0800
changeset 5147 ad50b3db78bd
parent 5113 686d795b1b29
child 5166 4d39eeaad00b
permissions -rw-r--r--
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 (window->flags & SDL_WINDOW_FULLSCREEN) {
    86             return 0;
    87         }
    88         if (data1 == SDL_WINDOWPOS_UNDEFINED) {
    89             data1 = window->x;
    90         }
    91         if (data2 == SDL_WINDOWPOS_UNDEFINED) {
    92             data2 = window->y;
    93         }
    94         if (data1 == window->x && data2 == window->y) {
    95             return 0;
    96         }
    97         window->x = data1;
    98         window->y = data2;
    99         break;
   100     case SDL_WINDOWEVENT_RESIZED:
   101         if (window->flags & SDL_WINDOW_FULLSCREEN) {
   102             return 0;
   103         }
   104         if (data1 == window->w && data2 == window->h) {
   105             return 0;
   106         }
   107         window->w = data1;
   108         window->h = data2;
   109         break;
   110     case SDL_WINDOWEVENT_MINIMIZED:
   111         if (window->flags & SDL_WINDOW_MINIMIZED) {
   112             return 0;
   113         }
   114         window->flags |= SDL_WINDOW_MINIMIZED;
   115         SDL_OnWindowMinimized(window);
   116         break;
   117     case SDL_WINDOWEVENT_MAXIMIZED:
   118         if (window->flags & SDL_WINDOW_MAXIMIZED) {
   119             return 0;
   120         }
   121         window->flags |= SDL_WINDOW_MAXIMIZED;
   122         break;
   123     case SDL_WINDOWEVENT_RESTORED:
   124         if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
   125             return 0;
   126         }
   127         window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
   128         SDL_OnWindowRestored(window);
   129         break;
   130     case SDL_WINDOWEVENT_ENTER:
   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_LEAVE:
   137         if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
   138             return 0;
   139         }
   140         window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
   141         break;
   142     case SDL_WINDOWEVENT_FOCUS_GAINED:
   143         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   144             return 0;
   145         }
   146         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   147         SDL_OnWindowFocusGained(window);
   148         break;
   149     case SDL_WINDOWEVENT_FOCUS_LOST:
   150         if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   151             return 0;
   152         }
   153         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
   154         SDL_OnWindowFocusLost(window);
   155         break;
   156     }
   157 
   158     /* Post the event, if desired */
   159     posted = 0;
   160     if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
   161         SDL_Event event;
   162         event.type = SDL_WINDOWEVENT;
   163         event.window.event = windowevent;
   164         event.window.data1 = data1;
   165         event.window.data2 = data2;
   166         event.window.windowID = window->id;
   167 
   168         /* Fixes queue overflow with resize events that aren't processed */
   169         if (windowevent == SDL_WINDOWEVENT_RESIZED) {
   170             SDL_FilterEvents(RemovePendingSizeEvents, &event);
   171         }
   172         if (windowevent == SDL_WINDOWEVENT_MOVED) {
   173             SDL_FilterEvents(RemovePendingMoveEvents, &event);
   174         }
   175 
   176         posted = (SDL_PushEvent(&event) > 0);
   177     }
   178 	
   179 	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
   180 		if ( !window->prev && !window->next ) {
   181 			// This is the last window in the list so send the SDL_QUIT event
   182 			SDL_SendQuit();
   183 		}
   184 	}
   185 
   186     return (posted);
   187 }
   188 
   189 /* vi: set ts=4 sw=4 expandtab: */