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