src/events/SDL_windowevents.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 23 Aug 2010 23:44:28 -0700
changeset 4865 fff50e86c891
parent 4429 faa9fc8e7f67
child 5042 8c39b82dc7b0
permissions -rw-r--r--
Update VS2010 project to add new files; update new files so code builds on Win32/Win64
     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 int
    47 SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
    48                     int data2)
    49 {
    50     int posted;
    51 
    52     if (!window) {
    53         return 0;
    54     }
    55     switch (windowevent) {
    56     case SDL_WINDOWEVENT_SHOWN:
    57         if (window->flags & SDL_WINDOW_SHOWN) {
    58             return 0;
    59         }
    60         window->flags |= SDL_WINDOW_SHOWN;
    61         SDL_OnWindowShown(window);
    62         break;
    63     case SDL_WINDOWEVENT_HIDDEN:
    64         if (!(window->flags & SDL_WINDOW_SHOWN)) {
    65             return 0;
    66         }
    67         window->flags &= ~SDL_WINDOW_SHOWN;
    68         SDL_OnWindowHidden(window);
    69         break;
    70     case SDL_WINDOWEVENT_MOVED:
    71         if (window->flags & SDL_WINDOW_FULLSCREEN) {
    72             return 0;
    73         }
    74         if (data1 == SDL_WINDOWPOS_UNDEFINED) {
    75             data1 = window->x;
    76         }
    77         if (data2 == SDL_WINDOWPOS_UNDEFINED) {
    78             data2 = window->y;
    79         }
    80         if (data1 == window->x && data2 == window->y) {
    81             return 0;
    82         }
    83         window->x = data1;
    84         window->y = data2;
    85         break;
    86     case SDL_WINDOWEVENT_RESIZED:
    87         if (window->flags & SDL_WINDOW_FULLSCREEN) {
    88             return 0;
    89         }
    90         if (data1 == window->w && data2 == window->h) {
    91             return 0;
    92         }
    93         window->w = data1;
    94         window->h = data2;
    95         SDL_OnWindowResized(window);
    96         break;
    97     case SDL_WINDOWEVENT_MINIMIZED:
    98         if (window->flags & SDL_WINDOW_MINIMIZED) {
    99             return 0;
   100         }
   101         window->flags |= SDL_WINDOW_MINIMIZED;
   102         SDL_OnWindowMinimized(window);
   103         break;
   104     case SDL_WINDOWEVENT_MAXIMIZED:
   105         if (window->flags & SDL_WINDOW_MAXIMIZED) {
   106             return 0;
   107         }
   108         window->flags |= SDL_WINDOW_MAXIMIZED;
   109         break;
   110     case SDL_WINDOWEVENT_RESTORED:
   111         if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
   112             return 0;
   113         }
   114         window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
   115         SDL_OnWindowRestored(window);
   116         break;
   117     case SDL_WINDOWEVENT_ENTER:
   118         if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
   119             return 0;
   120         }
   121         window->flags |= SDL_WINDOW_MOUSE_FOCUS;
   122         break;
   123     case SDL_WINDOWEVENT_LEAVE:
   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_FOCUS_GAINED:
   130         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   131             return 0;
   132         }
   133         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   134         SDL_OnWindowFocusGained(window);
   135         break;
   136     case SDL_WINDOWEVENT_FOCUS_LOST:
   137         if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   138             return 0;
   139         }
   140         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
   141         SDL_OnWindowFocusLost(window);
   142         break;
   143     }
   144 
   145     /* Post the event, if desired */
   146     posted = 0;
   147     if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
   148         SDL_Event event;
   149         event.type = SDL_WINDOWEVENT;
   150         event.window.event = windowevent;
   151         event.window.data1 = data1;
   152         event.window.data2 = data2;
   153         event.window.windowID = window->id;
   154 
   155         /* Fixes queue overflow with resize events that aren't processed */
   156         if (windowevent == SDL_WINDOWEVENT_RESIZED) {
   157             SDL_FilterEvents(RemovePendingSizeEvents, &event);
   158         }
   159 
   160         posted = (SDL_PushEvent(&event) > 0);
   161     }
   162     return (posted);
   163 }
   164 
   165 /* vi: set ts=4 sw=4 expandtab: */