X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/20b6985553b4e01e7960847e3211d940aeff8742..e409b62aaf6b333803027645ecc77de6c92cae2d:/src/mac/carbon/cursor.cpp diff --git a/src/mac/carbon/cursor.cpp b/src/mac/carbon/cursor.cpp index 8c6d751197..bf87cc5a88 100644 --- a/src/mac/carbon/cursor.cpp +++ b/src/mac/carbon/cursor.cpp @@ -1,69 +1,70 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: cursor.cpp +// Name: src/mac/carbon/cursor.cpp // Purpose: wxCursor class // Author: Stefan Csomor // Modified by: // Created: 1998-01-01 // RCS-ID: $Id$ // Copyright: (c) Stefan Csomor -// Licence: wxWindows licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "cursor.h" -#endif - #include "wx/wxprec.h" -#include "wx/app.h" #include "wx/cursor.h" -#include "wx/icon.h" -#include "wx/image.h" + +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/icon.h" + #include "wx/image.h" +#endif // WX_PRECOMP + #include "wx/xpmdecod.h" #include "wx/mac/private.h" -#if !USE_SHARED_LIBRARIES + IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap) -#endif + class WXDLLEXPORT wxCursorRefData: public wxBitmapRefData { DECLARE_NO_COPY_CLASS(wxCursorRefData) - + friend class WXDLLEXPORT wxBitmap; friend class WXDLLEXPORT wxCursor; + public: wxCursorRefData(); - ~wxCursorRefData(); + virtual ~wxCursorRefData(); protected: WXHCURSOR m_hCursor; bool m_disposeHandle; bool m_releaseHandle; - bool m_isColorCursor ; - long m_themeCursor ; + bool m_isColorCursor; + long m_themeCursor; }; #define M_CURSORDATA ((wxCursorRefData *)m_refData) #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData) -const short kwxCursorBullseye = 0 ; -const short kwxCursorBlank = 1 ; -const short kwxCursorPencil = 2 ; -const short kwxCursorMagnifier = 3 ; -const short kwxCursorNoEntry = 4 ; -const short kwxCursorPaintBrush = 5 ; -const short kwxCursorPointRight = 6 ; -const short kwxCursorPointLeft = 7 ; -const short kwxCursorQuestionArrow = 8 ; -const short kwxCursorRightArrow = 9 ; -const short kwxCursorSizeNS = 10 ; -const short kwxCursorSize = 11 ; -const short kwxCursorSizeNESW = 12 ; -const short kwxCursorSizeNWSE = 13 ; -const short kwxCursorRoller = 14 ; -const short kwxCursorLast = kwxCursorRoller ; +const short kwxCursorBullseye = 0; +const short kwxCursorBlank = 1; +const short kwxCursorPencil = 2; +const short kwxCursorMagnifier = 3; +const short kwxCursorNoEntry = 4; +const short kwxCursorPaintBrush = 5; +const short kwxCursorPointRight = 6; +const short kwxCursorPointLeft = 7; +const short kwxCursorQuestionArrow = 8; +const short kwxCursorRightArrow = 9; +const short kwxCursorSizeNS = 10; +const short kwxCursorSize = 11; +const short kwxCursorSizeNESW = 12; +const short kwxCursorSizeNWSE = 13; +const short kwxCursorRoller = 14; +const short kwxCursorLast = kwxCursorRoller; Cursor gMacCursors[kwxCursorLast+1] = { @@ -188,7 +189,7 @@ Cursor gMacCursors[kwxCursorLast+1] = {0x000A, 0x0006} }, -} ; +}; wxCursor gMacCurrentCursor ; @@ -196,19 +197,27 @@ CursHandle wxGetStockCursor( int number ) { wxASSERT_MSG( number >= 0 && number <=kwxCursorLast , wxT("invalid stock cursor id") ) ; CursHandle c = (CursHandle) NewHandle( sizeof(Cursor) ) ; - memcpy( *c , &gMacCursors[number] , sizeof( Cursor ) ) ; - return c ; + memcpy( *c, &gMacCursors[number], sizeof(Cursor) ) ; + +#ifndef WORDS_BIGENDIAN + short *sptr = (short*) *c ; + for ( int i = 0 ; i < 2 * 16 /* image and mask */ ; ++i, ++sptr ) + { + *sptr = CFSwapInt16( *sptr ) ; + } +#endif + return c ; } wxCursorRefData::wxCursorRefData() { - SetWidth( 16 ); + SetWidth( 16 ); SetHeight( 16 ); - m_hCursor = NULL ; - m_disposeHandle = false ; - m_releaseHandle = false ; - m_isColorCursor = false ; - m_themeCursor = -1 ; + m_hCursor = NULL; + m_disposeHandle = false; + m_releaseHandle = false; + m_isColorCursor = false; + m_themeCursor = -1; } wxCursorRefData::~wxCursorRefData() @@ -220,7 +229,7 @@ wxCursorRefData::~wxCursorRefData() else if ( m_disposeHandle ) { ::DisposeHandle( (Handle ) m_hCursor ) ; - } + } else if ( m_releaseHandle ) { // we don't release the resource since it may already @@ -228,7 +237,6 @@ wxCursorRefData::~wxCursorRefData() } } -// Cursors wxCursor::wxCursor() { } @@ -240,197 +248,239 @@ wxCursor::wxCursor(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSE wxCursor::wxCursor( const wxImage &image ) { +#if wxUSE_IMAGE CreateFromImage( image ) ; +#endif } -wxCursor::wxCursor(const char **bits) +wxCursor::wxCursor(const char **bits) { (void) CreateFromXpm(bits); } -wxCursor::wxCursor(char **bits) +wxCursor::wxCursor(char **bits) { (void) CreateFromXpm((const char **)bits); } bool wxCursor::CreateFromXpm(const char **bits) { - wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid cursor data") ) +#if wxUSE_IMAGE + wxCHECK_MSG( bits != NULL, false, wxT("invalid cursor data") ); wxXPMDecoder decoder; wxImage img = decoder.ReadData(bits); - wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid cursor data") ) - CreateFromImage( img ) ; - return TRUE; + wxCHECK_MSG( img.Ok(), false, wxT("invalid cursor data") ); + CreateFromImage( img ) ; + return true; +#else + return false; +#endif } -WXHCURSOR wxCursor::GetHCURSOR() const -{ - return (M_CURSORDATA ? M_CURSORDATA->m_hCursor : 0); +WXHCURSOR wxCursor::GetHCURSOR() const +{ + return (M_CURSORDATA ? M_CURSORDATA->m_hCursor : 0); } -bool wxCursor::Ok() const -{ - return (m_refData != NULL && ( M_CURSORDATA->m_hCursor != NULL || M_CURSORDATA->m_themeCursor != -1 ) ) ; +bool wxCursor::Ok() const +{ + return (m_refData != NULL && ( M_CURSORDATA->m_hCursor != NULL || M_CURSORDATA->m_themeCursor != -1 ) ) ; } short GetCTabIndex( CTabHandle colors , RGBColor *col ) { short retval = 0 ; unsigned long bestdiff = 0xFFFF ; + for ( int i = 0 ; i < (**colors).ctSize ; ++i ) { unsigned long diff = abs(col->red - (**colors).ctTable[i].rgb.red ) + abs(col->green - (**colors).ctTable[i].rgb.green ) + abs(col->blue - (**colors).ctTable[i].rgb.blue ) ; + if ( diff < bestdiff ) { bestdiff = diff ; retval = (**colors).ctTable[i].value ; } } + return retval ; } -void wxCursor::CreateFromImage(const wxImage & image) +#if wxUSE_IMAGE + +void wxCursor::CreateFromImage(const wxImage & image) { m_refData = new wxCursorRefData; - wxImage image16 = image.Scale(16,16) ; - unsigned char * rgbBits = image16.GetData(); - - - int w = image16.GetWidth() ; - int h = image16.GetHeight() ; + int w = 16; + int h = 16; + + int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); + int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); + int image_w = image.GetWidth(); + int image_h = image.GetHeight(); + + wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w && + hotSpotY >= 0 && hotSpotY < image_h, + wxT("invalid cursor hot spot coordinates") ); + + wxImage image16(image); // final image of correct size + + // if image is too small then place it in the center, resize it if too big + if ((w > image_w) && (h > image_h)) + { + wxPoint offset((w - image_w) / 2, (h - image_h) / 2); + hotSpotX = hotSpotX + offset.x; + hotSpotY = hotSpotY + offset.y; + + image16 = image.Size(wxSize(w, h), offset); + } + else if ((w != image_w) || (h != image_h)) + { + hotSpotX = int(hotSpotX * double(w) / double(image_w)); + hotSpotY = int(hotSpotY * double(h) / double(image_h)); + + image16 = image.Scale(w, h); + } + + unsigned char * rgbBits = image16.GetData(); bool bHasMask = image16.HasMask() ; - int hotSpotX = image16.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); - int hotSpotY = image16.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); - if (hotSpotX < 0 || hotSpotX >= w) - hotSpotX = 0; - if (hotSpotY < 0 || hotSpotY >= h) - hotSpotY = 0; - - PixMapHandle pm = (PixMapHandle) NewHandleClear( sizeof (PixMap)) ; + PixMapHandle pm = (PixMapHandle) NewHandleClear( sizeof(PixMap) ) ; short extent = 16 ; short bytesPerPixel = 1 ; short depth = 8 ; Rect bounds = { 0 , 0 , extent , extent } ; - CCrsrHandle ch = (CCrsrHandle) NewHandleClear ( sizeof( CCrsr ) ) ; + CCrsrHandle ch = (CCrsrHandle) NewHandleClear( sizeof(CCrsr) ) ; CTabHandle newColors = GetCTable( 8 ) ; - HandToHand((Handle *) &newColors); + HandToHand( (Handle *) &newColors ); + // set the values to the indices for ( int i = 0 ; i < (**newColors).ctSize ; ++i ) { (**newColors).ctTable[i].value = i ; } - HLock( (Handle) ch) ; - (**ch).crsrType = 0x8001 ; // color cursors - (**ch).crsrMap = pm ; - short bytesPerRow = bytesPerPixel * extent ; - (**pm).baseAddr = 0; + HLock( (Handle)ch ); + (**ch).crsrType = 0x8001; // color cursors + (**ch).crsrMap = pm; + short bytesPerRow = bytesPerPixel * extent; + + (**pm).baseAddr = 0; (**pm).rowBytes = bytesPerRow | 0x8000; - (**pm).bounds = bounds; - (**pm).pmVersion = 0; - (**pm).packType = 0; - (**pm).packSize = 0; - (**pm).hRes = 0x00480000; /* 72 DPI default res */ - (**pm).vRes = 0x00480000; /* 72 DPI default res */ + (**pm).bounds = bounds; + (**pm).pmVersion = 0; + (**pm).packType = 0; + (**pm).packSize = 0; + (**pm).hRes = 0x00480000; // 72 DPI default res + (**pm).vRes = 0x00480000; // 72 DPI default res (**pm).pixelSize = depth; (**pm).pixelType = 0; - (**pm).cmpCount = 1; + (**pm).cmpCount = 1; (**pm).cmpSize = depth; (**pm).pmTable = newColors; - (**ch).crsrData = NewHandleClear( extent * bytesPerRow ) ; + (**ch).crsrData = NewHandleClear( extent * bytesPerRow ) ; (**ch).crsrXData = NULL ; (**ch).crsrXValid = 0; (**ch).crsrXHandle = NULL; (**ch).crsrHotSpot.h = hotSpotX ; (**ch).crsrHotSpot.v = hotSpotY ; - (**ch).crsrXTable = 0 ; + (**ch).crsrXTable = 0 ; (**ch).crsrID = GetCTSeed() ; - + memset( (**ch).crsr1Data , 0 , sizeof( Bits16 ) ) ; memset( (**ch).crsrMask , 0 , sizeof( Bits16 ) ) ; - + unsigned char mr = image16.GetMaskRed() ; unsigned char mg = image16.GetMaskGreen() ; unsigned char mb = image16.GetMaskBlue() ; + for ( int y = 0 ; y < h ; ++y ) { - short rowbits = 0 ; - short maskbits = 0 ; - + short rowbits = 0, maskbits = 0 ; + for ( int x = 0 ; x < w ; ++x ) { long pos = (y * w + x) * 3; unsigned char r = rgbBits[pos] ; - unsigned char g = rgbBits[pos+1] ; - unsigned char b = rgbBits[pos+2] ; - RGBColor col = { 0xFFFF ,0xFFFF, 0xFFFF } ; - - if ( bHasMask && r==mr && g==mg && b==mb ) + unsigned char g = rgbBits[pos + 1] ; + unsigned char b = rgbBits[pos + 2] ; + RGBColor col = { 0xFFFF, 0xFFFF, 0xFFFF } ; + + if ( bHasMask && r == mr && g == mg && b == mb ) { // masked area, does not appear anywhere } else { if ( (int)r + (int)g + (int)b < 0x0200 ) - { - rowbits |= ( 1 << (15-x) ) ; - } - maskbits |= ( 1 << (15-x) ) ; - + rowbits |= ( 1 << (15 - x) ) ; + + maskbits |= ( 1 << (15 - x) ) ; + col = *((RGBColor*) wxColor( r , g , b ).GetPixel()) ; } - *((*(**ch).crsrData) + y * bytesPerRow + x) = + + *((*(**ch).crsrData) + y * bytesPerRow + x) = GetCTabIndex( newColors , &col) ; } +#ifdef WORDS_BIGENDIAN (**ch).crsr1Data[y] = rowbits ; (**ch).crsrMask[y] = maskbits ; +#else + (**ch).crsr1Data[y] = CFSwapInt16(rowbits) ; + (**ch).crsrMask[y] = CFSwapInt16(maskbits) ; +#endif } + if ( !bHasMask ) - { memcpy( (**ch).crsrMask , (**ch).crsr1Data , sizeof( Bits16) ) ; - } - - HUnlock((Handle) ch) ; + + HUnlock( (Handle)ch ) ; M_CURSORDATA->m_hCursor = ch ; M_CURSORDATA->m_isColorCursor = true ; } +#endif //wxUSE_IMAGE + wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int hotSpotY) { m_refData = new wxCursorRefData; if ( flags == wxBITMAP_TYPE_MACCURSOR_RESOURCE ) { Str255 theName ; - wxMacStringToPascal( cursor_file , theName ) ; - + wxMacStringToPascal( cursor_file , theName ) ; + Handle resHandle = ::GetNamedResource( 'crsr' , theName ) ; if ( resHandle ) { short theId = -1 ; OSType theType ; + GetResInfo( resHandle , &theId , &theType , theName ) ; ReleaseResource( resHandle ) ; + M_CURSORDATA->m_hCursor = GetCCursor( theId ) ; if ( M_CURSORDATA->m_hCursor ) M_CURSORDATA->m_isColorCursor = true ; } else - { + { Handle resHandle = ::GetNamedResource( 'CURS' , theName ) ; if ( resHandle ) { short theId = -1 ; OSType theType ; + GetResInfo( resHandle , &theId , &theType , theName ) ; ReleaseResource( resHandle ) ; - M_CURSORDATA->m_hCursor = GetCursor( theId ) ; + + M_CURSORDATA->m_hCursor = GetCursor( theId ) ; if ( M_CURSORDATA->m_hCursor ) M_CURSORDATA->m_releaseHandle = true ; } @@ -438,15 +488,17 @@ wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int ho } else { +#if wxUSE_IMAGE wxImage image ; - image.LoadFile( cursor_file , flags ) ; - if( image.Ok() ) + image.LoadFile( cursor_file, flags ) ; + if ( image.Ok() ) { - image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,hotSpotX ) ; - image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,hotSpotY ) ; + image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX ) ; + image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY ) ; delete m_refData ; - CreateFromImage(image) ; + CreateFromImage( image ) ; } +#endif } } @@ -454,125 +506,112 @@ wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int ho wxCursor::wxCursor(int cursor_type) { m_refData = new wxCursorRefData; - + switch (cursor_type) { case wxCURSOR_COPY_ARROW: - M_CURSORDATA->m_themeCursor = kThemeCopyArrowCursor ; + M_CURSORDATA->m_themeCursor = kThemeCopyArrowCursor; break; + case wxCURSOR_WAIT: - M_CURSORDATA->m_themeCursor = kThemeWatchCursor ; + M_CURSORDATA->m_themeCursor = kThemeWatchCursor; break; + case wxCURSOR_IBEAM: - M_CURSORDATA->m_themeCursor = kThemeIBeamCursor ; + M_CURSORDATA->m_themeCursor = kThemeIBeamCursor; break; + case wxCURSOR_CROSS: M_CURSORDATA->m_themeCursor = kThemeCrossCursor; break; + case wxCURSOR_SIZENWSE: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNWSE); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNWSE); break; + case wxCURSOR_SIZENESW: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNESW); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNESW); break; + case wxCURSOR_SIZEWE: - { - M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor; - } + M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor; break; + case wxCURSOR_SIZENS: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNS); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNS); break; + case wxCURSOR_SIZING: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSize); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSize); break; + case wxCURSOR_HAND: - { - M_CURSORDATA->m_themeCursor = kThemePointingHandCursor; - } + M_CURSORDATA->m_themeCursor = kThemePointingHandCursor; break; + case wxCURSOR_BULLSEYE: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBullseye); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBullseye); break; + case wxCURSOR_PENCIL: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPencil); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPencil); break; + case wxCURSOR_MAGNIFIER: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorMagnifier); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorMagnifier); break; + case wxCURSOR_NO_ENTRY: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorNoEntry); - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorNoEntry); break; + case wxCURSOR_WATCH: - { - M_CURSORDATA->m_themeCursor = kThemeWatchCursor; - break; - } + M_CURSORDATA->m_themeCursor = kThemeWatchCursor; + break; + case wxCURSOR_PAINT_BRUSH: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPaintBrush); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPaintBrush); + break; + case wxCURSOR_POINT_LEFT: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointLeft); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointLeft); + break; + case wxCURSOR_POINT_RIGHT: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointRight); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointRight); + break; + case wxCURSOR_QUESTION_ARROW: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorQuestionArrow); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorQuestionArrow); + break; + case wxCURSOR_BLANK: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBlank); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBlank); + break; + case wxCURSOR_RIGHT_ARROW: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRightArrow); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRightArrow); + break; + case wxCURSOR_SPRAYCAN: - { - M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRoller); - break; - } + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRoller); + break; + case wxCURSOR_CHAR: case wxCURSOR_ARROW: case wxCURSOR_LEFT_BUTTON: case wxCURSOR_RIGHT_BUTTON: case wxCURSOR_MIDDLE_BUTTON: default: - M_CURSORDATA->m_themeCursor = kThemeArrowCursor ; + M_CURSORDATA->m_themeCursor = kThemeArrowCursor; break; } + if ( M_CURSORDATA->m_themeCursor == -1 ) - M_CURSORDATA->m_releaseHandle = true ; + M_CURSORDATA->m_releaseHandle = true; } -void wxCursor::MacInstall() const +void wxCursor::MacInstall() const { gMacCurrentCursor = *this ; if ( m_refData && M_CURSORDATA->m_themeCursor != -1 ) @@ -601,5 +640,3 @@ void wxSetCursor(const wxCursor& cursor) { cursor.MacInstall() ; } - -