1/*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org
6
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28
29#import "OgreOSXCocoaWindow.h"
30
31#import "OgreOSXCocoaWindowDelegate.h"
32#import "OgreWindowEventUtilities.h"
33
34using namespace Ogre;
35
36@implementation CocoaWindowDelegate
37
38
39-(id)initWithNSWindow:(NSWindow*)nswin ogreWindow:(RenderWindow*)ogrewin
40{
41    if ((self = [super init]))
42    {
43		window = nswin;
44		ogreWindow = ogrewin;
45
46        // Register ourselves for several window event notifications
47		// Note that
48        [[NSNotificationCenter defaultCenter] addObserver:self
49                                                 selector:@selector(windowDidResize:)
50                                                     name:@"NSWindowDidResizeNotification"
51												   object:window];
52
53        [[NSNotificationCenter defaultCenter] addObserver:self
54                                                 selector:@selector(windowWillMove:)
55                                                     name:@"NSWindowWillMoveNotification"
56												   object:window];
57
58        [[NSNotificationCenter defaultCenter] addObserver:self
59                                                 selector:@selector(windowWillClose:)
60                                                     name:@"NSWindowWillCloseNotification"
61												   object:window];
62
63        [[NSNotificationCenter defaultCenter] addObserver:self
64                                                 selector:@selector(windowDidBecomeKey:)
65                                                     name:@"NSWindowDidBecomeKeyNotification"
66												   object:window];
67
68        [[NSNotificationCenter defaultCenter] addObserver:self
69                                                 selector:@selector(windowDidResignKey:)
70                                                     name:@"NSWindowDidResignKeyNotification"
71												   object:window];
72
73        [[NSNotificationCenter defaultCenter] addObserver:self
74                                                 selector:@selector(windowDidMiniaturize:)
75                                                     name:@"NSWindowDidMiniaturizeNotification"
76												   object:window];
77
78        [[NSNotificationCenter defaultCenter] addObserver:self
79                                                 selector:@selector(windowDidDeminiaturize:)
80                                                     name:@"NSWindowDidDeminiaturizeNotification"
81												   object:window];
82    }
83    return self;
84}
85
86- (void)dealloc {
87    // Stop observing notifications
88    [[NSNotificationCenter defaultCenter] removeObserver:self];
89
90    [super dealloc];
91}
92
93- (void)windowDidResize:(NSNotification *)notification
94{
95	// Update the Ogre window
96	CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
97	WindowEventUtilities::WindowEventListeners::iterator
98	start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
99	end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
100
101	curWindow->windowMovedOrResized();
102
103	for( ; start != end; ++start )
104		(start->second)->windowResized(curWindow);
105
106}
107
108- (void)windowWillMove:(NSNotification *)notification
109{
110    CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
111
112    WindowEventUtilities::WindowEventListeners::iterator
113        start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
114        end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
115
116    curWindow->windowMovedOrResized();
117    for( ; start != end; ++start )
118        (start->second)->windowMoved(curWindow);
119}
120
121- (void)windowWillClose:(NSNotification *)notification
122{
123    CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
124
125    WindowEventUtilities::WindowEventListeners::iterator
126    start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
127    end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
128
129    for( ; start != end; ++start )
130    {
131        (start->second)->windowClosing(curWindow);
132    }
133}
134
135- (void)windowDidBecomeKey:(NSNotification *)notification
136{
137    CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
138
139    WindowEventUtilities::WindowEventListeners::iterator
140    start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
141    end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
142
143    curWindow->setActive( true );
144    for( ; start != end; ++start )
145        (start->second)->windowFocusChange(curWindow);
146}
147
148- (void)windowDidResignKey:(NSNotification *)notification
149{
150    CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
151
152    WindowEventUtilities::WindowEventListeners::iterator
153    start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
154    end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
155
156    if( curWindow->isDeactivatedOnFocusChange() )
157    {
158        curWindow->setActive( false );
159    }
160
161    for( ; start != end; ++start )
162        (start->second)->windowFocusChange(curWindow);
163}
164
165- (void)windowDidMiniaturize:(NSNotification *)notification
166{
167    CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
168
169    WindowEventUtilities::WindowEventListeners::iterator
170    start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
171    end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
172
173    curWindow->setActive( false );
174    curWindow->setVisible( false );
175    for( ; start != end; ++start )
176        (start->second)->windowFocusChange(curWindow);
177}
178
179- (void)windowDidDeminiaturize:(NSNotification *)notification
180{
181    CocoaWindow * curWindow = static_cast<CocoaWindow *>(ogreWindow);
182
183    WindowEventUtilities::WindowEventListeners::iterator
184    start = WindowEventUtilities::_msListeners.lower_bound(curWindow),
185    end = WindowEventUtilities::_msListeners.upper_bound(curWindow);
186
187    curWindow->setActive( true );
188    curWindow->setVisible( true );
189    for( ; start != end; ++start )
190        (start->second)->windowFocusChange(curWindow);
191}
192@end
193