src/events/SDL_windowevents.c
author Sam Lantinga
Wed, 19 Jan 2011 16:02:15 -0800
changeset 5042 8c39b82dc7b0
parent 4429 faa9fc8e7f67
child 5113 686d795b1b29
permissions -rw-r--r--
Window move events have the same problem as size events on Windows
     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         SDL_OnWindowResized(window);
   110         break;
   111     case SDL_WINDOWEVENT_MINIMIZED:
   112         if (window->flags & SDL_WINDOW_MINIMIZED) {
   113             return 0;
   114         }
   115         window->flags |= SDL_WINDOW_MINIMIZED;
   116         SDL_OnWindowMinimized(window);
   117         break;
   118     case SDL_WINDOWEVENT_MAXIMIZED:
   119         if (window->flags & SDL_WINDOW_MAXIMIZED) {
   120             return 0;
   121         }
   122         window->flags |= SDL_WINDOW_MAXIMIZED;
   123         break;
   124     case SDL_WINDOWEVENT_RESTORED:
   125         if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
   126             return 0;
   127         }
   128         window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
   129         SDL_OnWindowRestored(window);
   130         break;
   131     case SDL_WINDOWEVENT_ENTER:
   132         if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
   133             return 0;
   134         }
   135         window->flags |= SDL_WINDOW_MOUSE_FOCUS;
   136         break;
   137     case SDL_WINDOWEVENT_LEAVE:
   138         if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
   139             return 0;
   140         }
   141         window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
   142         break;
   143     case SDL_WINDOWEVENT_FOCUS_GAINED:
   144         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   145             return 0;
   146         }
   147         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   148         SDL_OnWindowFocusGained(window);
   149         break;
   150     case SDL_WINDOWEVENT_FOCUS_LOST:
   151         if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   152             return 0;
   153         }
   154         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
   155         SDL_OnWindowFocusLost(window);
   156         break;
   157     }
   158 
   159     /* Post the event, if desired */
   160     posted = 0;
   161     if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
   162         SDL_Event event;
   163         event.type = SDL_WINDOWEVENT;
   164         event.window.event = windowevent;
   165         event.window.data1 = data1;
   166         event.window.data2 = data2;
   167         event.window.windowID = window->id;
   168 
   169         /* Fixes queue overflow with resize events that aren't processed */
   170         if (windowevent == SDL_WINDOWEVENT_RESIZED) {
   171             SDL_FilterEvents(RemovePendingSizeEvents, &event);
   172         }
   173         if (windowevent == SDL_WINDOWEVENT_MOVED) {
   174             SDL_FilterEvents(RemovePendingMoveEvents, &event);
   175         }
   176 
   177         posted = (SDL_PushEvent(&event) > 0);
   178     }
   179     return (posted);
   180 }
   181 
   182 /* vi: set ts=4 sw=4 expandtab: */