include/cinder/Display.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Area.h"
00027 #include "cinder/Function.h"
00028 
00029 #if defined( CINDER_MAC )
00030     #if defined( __OBJC__ )
00031         @class NSScreen;
00032     #else
00033         class NSScreen;
00034     #endif
00035     typedef uint32_t CGDirectDisplayID;
00036 #elif defined( CINDER_COCOA_TOUCH )
00037     #if defined( __OBJC__ )
00038         @class UIScreen;
00039     #else
00040         class UIScreen;
00041     #endif
00042 #elif defined( CINDER_MSW )
00043     #include <windows.h>
00044     #undef min
00045     #undef max
00046 #endif
00047 
00048 #include <map>
00049 #include <vector>
00050 
00051 namespace cinder {
00052 
00053 typedef std::shared_ptr<class Display>  DisplayRef;
00054 
00055 class Display {
00056   public:
00057     ~Display();
00058 
00060     int             getWidth() const { return mArea.getWidth(); }
00062     int             getHeight() const { return mArea.getHeight(); }
00064     Vec2i           getSize() const { return Vec2i( getWidth(), getHeight() ); }
00066     float           getAspectRatio() const { return getWidth() / (float)getHeight(); }
00068     Area            getBounds() const { return mArea; }
00069     
00071     Vec2i           getSystemCoordinate( const Vec2i &displayRelativeCoordinate ) const;
00072 
00074     int     getBitsPerPixel() const { return mBitsPerPixel; }
00076     float   getContentScale() const;
00077 
00079     bool    contains( const Vec2i &pt ) const { return mArea.contains( pt ); }
00080 
00081 #if defined( CINDER_MAC )
00082     NSScreen*           getNsScreen() const { return mScreen; }
00083     CGDirectDisplayID   getCgDirectDisplayId() const { return mDirectDisplayID; }
00084 #elif defined( CINDER_COCOA_TOUCH )
00085     UIScreen*           getUiScreen() const { return mUiScreen; }
00087     const std::vector<Vec2i>&   getSupportedResolutions() const { return mSupportedResolutions; }
00089     void                setResolution( const Vec2i &resolution );
00090 #endif
00091 
00093     static DisplayRef                       getMainDisplay();
00095     static const std::vector<DisplayRef>&   getDisplays();
00097     static DisplayRef                       getDisplayForPoint( const Vec2i &pt );
00099     static Area                             getSpanningArea();
00100     
00101 #if defined( CINDER_MAC )
00102     static DisplayRef           findFromCgDirectDisplayId( CGDirectDisplayID displayID );
00103     static DisplayRef           findFromNsScreen( NSScreen *nsScreen );
00104 #elif defined( CINDER_MSW )
00105 
00106     static DisplayRef           findFromHmonitor( HMONITOR hMonitor );
00107     static BOOL CALLBACK enumMonitorProc( HMONITOR hMonitor, HDC hdc, LPRECT rect, LPARAM lParam );
00108 #endif
00109 
00110     friend std::ostream& operator<<( std::ostream &o, const Display &display )
00111     {
00112         return o << display.mArea << " @ " << display.mBitsPerPixel << "bpp @ scale " << display.mContentScale;
00113     }   
00114     
00115 #if defined( CINDER_COCOA_TOUCH )
00116 
00117     signals::signal<void()>&    getSignalDisplaysChanged() { return mSignalDisplaysChanged; }
00118     template<typename T, typename Y>
00119     void                        connectDisplaysChanged( T fn, Y *inst ) { getSignalDisplaysChanged().connect( std::bind( fn, inst ) ); }
00120 #endif
00121     
00122   private:
00123     Area        mArea;
00124     int         mBitsPerPixel;
00125     float       mContentScale;
00126 #if defined( CINDER_MAC )
00127     NSScreen            *mScreen;
00128     CGDirectDisplayID   mDirectDisplayID;
00129 #elif defined( CINDER_COCOA_TOUCH )
00130     UIScreen                *mUiScreen;
00131     std::vector<Vec2i>      mSupportedResolutions;
00132     signals::signal<void()> mSignalDisplaysChanged;
00133 #elif defined( CINDER_MSW )
00134     HMONITOR            mMonitor;
00135 #endif
00136     
00137     static void     enumerateDisplays();
00138     
00139     static std::vector<DisplayRef>  sDisplays;
00140     static bool                     sDisplaysInitialized;
00141 };
00142 
00143 } // namespace cinder