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