Torque3D Documentation / _generateds / macWindowManager.mm

macWindowManager.mm

Engine/source/windowManager/mac/macWindowManager.mm

More...

Public Functions

Global function to allocate a new platform window manager.

Detailed Description

Public Functions

convertCGRectToRectI(NSRect r)

CreatePlatformWindowManager()

Global function to allocate a new platform window manager.

This returns an instance of the appropriate window manager for the current OS.

Depending on situation (for instance, if we are a web plugin) we may need to get the window manager from somewhere else.

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include <Cocoa/Cocoa.h>
 25#include "windowManager/mac/macWindowManager.h"
 26#include "windowManager/mac/macWindow.h"
 27#include "core/util/journal/process.h"
 28#include "console/console.h"
 29#include "gfx/gfxDevice.h"
 30
 31PlatformWindowManager* CreatePlatformWindowManager()
 32{
 33   return new MacWindowManager();
 34}
 35
 36static inline RectI convertCGRectToRectI(NSRect r)
 37{
 38   return RectI(r.origin.x, r.origin.y, r.size.width, r.size.height);
 39}
 40
 41MacWindowManager::MacWindowManager() : mNotifyShutdownDelegate(this, &MacWindowManager::onShutdown), mIsShuttingDown(false)
 42{
 43   mWindowList.clear();
 44   Process::notifyShutdown(mNotifyShutdownDelegate);
 45}
 46
 47MacWindowManager::~MacWindowManager()
 48{  
 49   for(U32 i = 0; i < mWindowList.size(); i++)
 50      delete mWindowList[i];
 51   mWindowList.clear();
 52   
 53   CGReleaseDisplayFadeReservation(mFadeToken);
 54}
 55
 56RectI MacWindowManager::getPrimaryDesktopArea()
 57{
 58   // Get the area of the main desktop that isn't taken by the dock or menu bar.
 59   return convertCGRectToRectI([[NSScreen mainScreen] visibleFrame]);
 60}
 61
 62void MacWindowManager::getMonitorRegions(Vector<RectI> &regions)
 63{
 64   // Populate a vector with all monitors and their extents in window space.
 65   NSArray *screenList = [NSScreen screens];
 66   for(U32 i = 0; i < [screenList count]; i++)
 67   {
 68      NSRect screenBounds = [[screenList objectAtIndex: i] frame];
 69      regions.push_back(convertCGRectToRectI(screenBounds));
 70   }
 71}
 72
 73S32 MacWindowManager::getDesktopBitDepth()
 74{
 75   // get the current desktop bit depth
 76   // TODO: return -1 if an error occurred
 77   return NSBitsPerPixelFromDepth([[NSScreen mainScreen] depth]);
 78}
 79
 80Point2I MacWindowManager::getDesktopResolution()
 81{
 82   // get the current desktop width/height
 83   // TODO: return Point2I(-1,-1) if an error occurred
 84   NSRect desktopBounds = [[NSScreen mainScreen] frame];
 85   return Point2I((U32)desktopBounds.size.width, (U32)desktopBounds.size.height);
 86}
 87
 88S32 MacWindowManager::getWindowCount()
 89{
 90   // Get the number of PlatformWindow's in this manager
 91   return mWindowList.size();
 92}
 93
 94void MacWindowManager::getWindows(VectorPtr<PlatformWindow*> &windows)
 95{
 96   // Populate a list with references to all the windows created from this manager.
 97   windows.merge(mWindowList);
 98}
 99
100PlatformWindow * MacWindowManager::getFirstWindow()
101{
102   if (mWindowList.size() > 0)
103      return mWindowList[0];
104      
105   return NULL;
106}
107
108
109PlatformWindow* MacWindowManager::getFocusedWindow()
110{
111   for (U32 i = 0; i < mWindowList.size(); i++)
112   {
113      if( mWindowList[i]->isFocused() )
114         return mWindowList[i];
115   }
116
117   return NULL;
118}
119
120PlatformWindow* MacWindowManager::getWindowById(WindowId zid)
121{
122   // Find the window by its arbirary WindowId.
123   for(U32 i = 0; i < mWindowList.size(); i++)
124   {
125      PlatformWindow* w = mWindowList[i];
126      if( w->getWindowId() == zid)
127         return w;
128   }
129   return NULL;
130}
131
132void MacWindowManager::lowerCurtain()
133{
134   // fade all displays.
135   CGError err;
136   err = CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &mFadeToken);
137   AssertWarn(!err, "MacWindowManager::lowerCurtain() could not get a token");
138   if(err) return;
139   
140   err = CGDisplayFade(mFadeToken, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, true);
141   AssertWarn(!err, "MacWindowManager::lowerCurtain() failed the fade");
142   if(err) return;
143   
144   // we do not release the token, because that will un-fade the screen!
145   // the token will last for 15 sec, and then the screen will un-fade regardless.
146   //CGReleaseDisplayFadeReservation(mFadeToken);
147}
148
149void MacWindowManager::raiseCurtain()
150{
151   // release the fade on all displays
152   CGError err;
153   err = CGDisplayFade(mFadeToken, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, false);
154   AssertWarn(!err, "MacWindowManager::raiseCurtain() failed the fade");
155   
156   err = CGReleaseDisplayFadeReservation(mFadeToken);
157   AssertWarn(!err, "MacWindowManager::raiseCurtain() failed releasing the token");
158}
159
160
161void MacWindowManager::_processCmdLineArgs(const S32 argc, const char **argv)
162{
163   // TODO: accept command line args if necessary.
164}
165
166PlatformWindow *MacWindowManager::createWindow(GFXDevice *device, const GFXVideoMode &mode)
167{
168   MacWindow* window = new MacWindow(getNextId(), getEngineProductString(), mode.resolution);
169   _addWindow(window);
170   
171   // Set the video mode on the window
172   window->setVideoMode(mode);
173
174   // Make sure our window is shown and drawn to.
175   window->show();
176
177   // Bind the window to the specified device.
178   if(device)
179   {
180      window->mDevice = device;
181      window->mTarget = device->allocWindowTarget(window);
182      AssertISV(window->mTarget, 
183         "MacWindowManager::createWindow - failed to get a window target back from the device.");
184   }
185   else
186   {
187      Con::warnf("MacWindowManager::createWindow - created a window with no device!");
188   }
189
190   return window;
191}
192
193void MacWindowManager::_addWindow(MacWindow* window)
194{
195#ifdef TORQUE_DEBUG
196   // Make sure we aren't adding the window twice
197   for(U32 i = 0; i < mWindowList.size(); i++)
198      AssertFatal(window != mWindowList[i], "MacWindowManager::_addWindow - Should not add a window more than once");
199#endif
200   if (mWindowList.size() > 0)
201      window->mNextWindow = mWindowList.last();
202   else
203      window->mNextWindow = NULL;
204
205   mWindowList.push_back(window);
206   window->mOwningWindowManager = this;
207   window->appEvent.notify(this, &MacWindowManager::_onAppSignal);
208}
209
210void MacWindowManager::_removeWindow(MacWindow* window)
211{
212   for(WindowList::iterator i = mWindowList.begin(); i != mWindowList.end(); i++)
213   {
214      if(*i == window)
215      {
216         mWindowList.erase(i);
217         return;
218      }
219   }
220   AssertFatal(false, avar("MacWindowManager::_removeWindow - Failed to remove window %x, perhaps it was already removed?", window));
221}
222
223void MacWindowManager::_onAppSignal(WindowId wnd, S32 event)
224{
225   if(event != WindowHidden)
226      return;
227   
228   for(U32 i = 0; i < mWindowList.size(); i++)
229   {
230      if(mWindowList[i]->getWindowId() == wnd)
231         continue;
232      
233      mWindowList[i]->signalGainFocus();
234   }
235}
236
237bool MacWindowManager::onShutdown()
238{
239   mIsShuttingDown = true;
240   return true;
241}
242
243bool MacWindowManager::canWindowGainFocus(MacWindow* window)
244{
245   return !mIsShuttingDown;
246}
247