Cocoa : correctly handle late events sent after window deactivate
[blender.git] / intern / ghost / intern / GHOST_WindowManager.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /**
30
31  * $Id$
32  * Copyright (C) 2001 NaN Technologies B.V.
33  * @author      Maarten Gribnau
34  * @date        May 11, 2001
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "GHOST_WindowManager.h"
42 #include <algorithm>
43 #include "GHOST_Debug.h"
44 #include "GHOST_Window.h"
45
46
47 GHOST_WindowManager::GHOST_WindowManager() : 
48         m_fullScreenWindow(0),
49         m_activeWindow(0),
50         m_activeWindowBeforeFullScreen(0)
51 {
52 }
53
54
55 GHOST_WindowManager::~GHOST_WindowManager()
56 {
57         /* m_windows is freed by GHOST_System::disposeWindow */
58 }
59
60
61 GHOST_TSuccess GHOST_WindowManager::addWindow(GHOST_IWindow* window)
62 {
63         GHOST_TSuccess success = GHOST_kFailure;
64         if (window) {
65                 if (!getWindowFound(window)) {
66                         // Store the pointer to the window 
67                         m_windows.push_back(window);
68                         success = GHOST_kSuccess;
69                 }
70         }
71         return success;
72 }
73
74
75 GHOST_TSuccess GHOST_WindowManager::removeWindow(const GHOST_IWindow* window)
76 {
77         GHOST_TSuccess success = GHOST_kFailure;
78         if (window) {
79                 if (window == m_fullScreenWindow) {
80                         endFullScreen();
81                 }
82                 else {
83                         vector<GHOST_IWindow*>::iterator result = find(m_windows.begin(), m_windows.end(), window);
84                         if (result != m_windows.end()) {
85                                 setWindowInactive(window);
86                                 m_windows.erase(result);
87                                 success = GHOST_kSuccess;
88                         }
89                 }
90         }
91         return success;
92 }
93
94
95 bool GHOST_WindowManager::getWindowFound(const GHOST_IWindow* window) const
96 {
97         bool found = false;
98         if (window) {
99         if (getFullScreen() && (window == m_fullScreenWindow)) {
100             found = true;
101         }
102         else {
103             vector<GHOST_IWindow*>::const_iterator result = find(m_windows.begin(), m_windows.end(), window);
104             if (result != m_windows.end()) {
105                 found = true;
106             }
107         }
108         }
109         return found;
110 }
111
112
113 bool GHOST_WindowManager::getFullScreen(void) const
114 {
115         return m_fullScreenWindow != 0;
116 }
117
118
119 GHOST_IWindow* GHOST_WindowManager::getFullScreenWindow(void) const
120 {
121     return m_fullScreenWindow;
122 }
123
124
125 GHOST_TSuccess GHOST_WindowManager::beginFullScreen(GHOST_IWindow* window,
126                 bool stereoVisual)
127 {
128         GHOST_TSuccess success = GHOST_kFailure;
129         GHOST_ASSERT(window, "GHOST_WindowManager::beginFullScreen(): invalid window");
130         GHOST_ASSERT(window->getValid(), "GHOST_WindowManager::beginFullScreen(): invalid window");
131         if (!getFullScreen()) {
132                 m_fullScreenWindow = window;
133                 m_activeWindowBeforeFullScreen = getActiveWindow();
134                 setActiveWindow(m_fullScreenWindow);
135                 success = GHOST_kSuccess;
136         }
137         return success;
138 }
139
140
141 GHOST_TSuccess GHOST_WindowManager::endFullScreen(void)
142 {
143         GHOST_TSuccess success = GHOST_kFailure;
144         if (getFullScreen()) {
145                 if (m_fullScreenWindow != 0) {
146                         //GHOST_PRINT("GHOST_WindowManager::endFullScreen(): deleting full-screen window\n");
147                         setWindowInactive(m_fullScreenWindow);
148                         delete m_fullScreenWindow;
149                         //GHOST_PRINT("GHOST_WindowManager::endFullScreen(): done\n");
150                         m_fullScreenWindow = 0;
151                         if (m_activeWindowBeforeFullScreen) {
152                                 setActiveWindow(m_activeWindowBeforeFullScreen);
153                         }
154                 }
155         success = GHOST_kSuccess;
156         }
157         return success;
158 }
159
160
161 GHOST_TSuccess GHOST_WindowManager::setActiveWindow(GHOST_IWindow* window)
162 {
163         GHOST_TSuccess success = GHOST_kSuccess;
164         if (window != m_activeWindow) {
165                 if (getWindowFound(window)) {
166                         m_activeWindow = window;
167                 }
168                 else {
169                         success = GHOST_kFailure;
170                 }
171         }
172         return success;
173 }
174         
175
176 GHOST_IWindow* GHOST_WindowManager::getActiveWindow(void) const
177 {
178         return m_activeWindow;
179 }
180
181
182 void GHOST_WindowManager::setWindowInactive(const GHOST_IWindow* window)
183 {
184         if (window == m_activeWindow) {
185                 m_activeWindow = 0;
186         }
187 }
188
189
190 std::vector<GHOST_IWindow *> &GHOST_WindowManager::getWindows()
191 {
192         return m_windows;
193 }
194
195
196 GHOST_IWindow* GHOST_WindowManager::getWindowAssociatedWithOSWindow(void* osWindow)
197 {
198         std::vector<GHOST_IWindow*>::iterator iter;
199
200         for (iter = m_windows.begin(); iter != m_windows.end(); iter++) {
201                 if ((*iter)->getOSWindow() == osWindow)
202                         return *iter;
203         }
204         
205         return NULL;
206 }
207
208 bool GHOST_WindowManager::getAnyModifiedState()
209 {
210         bool isAnyModified = false;
211         std::vector<GHOST_IWindow*>::iterator iter;
212         
213         for (iter = m_windows.begin(); iter != m_windows.end(); iter++) {
214                 if ((*iter)->getModifiedState())
215                         isAnyModified = true;
216         }
217
218         return isAnyModified;
219 }