X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e9576ca53db96b462ed4c0b4bdf47d64c40203e4..94311eef78b66304c9a2f78bcbdd396c798f19a6:/src/mac/cursor.cpp?ds=sidebyside diff --git a/src/mac/cursor.cpp b/src/mac/cursor.cpp index 4333364c93..ad1d54abb0 100644 --- a/src/mac/cursor.cpp +++ b/src/mac/cursor.cpp @@ -13,25 +13,60 @@ #pragma implementation "cursor.h" #endif +#include "wx/defs.h" + +#include "wx/app.h" #include "wx/cursor.h" #include "wx/icon.h" +#include "wx/image.h" +#include "wx/mac/private.h" #if !USE_SHARED_LIBRARIES IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap) #endif +const short kwxCursorBullseye = 10 ; +const short kwxCursorBlank = 11 ; +const short kwxCursorPencil = 12 ; +const short kwxCursorMagnifier = 13 ; +const short kwxCursorNoEntry = 14 ; +const short kwxCursorPaintBrush = 15 ; +const short kwxCursorPointRight = 16 ; +const short kwxCursorPointLeft = 17 ; +const short kwxCursorQuestionArrow = 18 ; +const short kwxCursorRightArrow = 19 ; +const short kwxCursorSizeNS = 20 ; +const short kwxCursorSize = 21 ; +const short kwxCursorSizeNESW = 22 ; +const short kwxCursorSizeNWSE = 23 ; +const short kwxCursorRoller = 24 ; + +wxCursor gMacCurrentCursor ; + wxCursorRefData::wxCursorRefData() { - m_width = 32; m_height = 32; - -/* TODO - m_hCursor = 0 ; -*/ + m_width = 16; + m_height = 16; + m_hCursor = NULL ; + m_disposeHandle = false ; + m_releaseHandle = false ; + m_isColorCursor = false ; + m_themeCursor = -1 ; } wxCursorRefData::~wxCursorRefData() { - // TODO: destroy cursor + if ( m_isColorCursor ) + { + ::DisposeCCursor( (CCrsrHandle) m_hCursor ) ; + } + else if ( m_disposeHandle ) + { + ::DisposeHandle( (Handle ) m_hCursor ) ; + } else if ( m_releaseHandle ) + { + ::ReleaseResource( (Handle ) m_hCursor ) ; + } } // Cursors @@ -44,11 +79,117 @@ wxCursor::wxCursor(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSE { } +wxCursor::wxCursor( const wxImage &image ) +{ + CreateFromImage( 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() ; + bool bHasMask = image16.HasMask() ; + + int hotSpotX = image16.GetOptionInt(wxCUR_HOTSPOT_X); + int hotSpotY = image16.GetOptionInt(wxCUR_HOTSPOT_Y); + if (hotSpotX < 0 || hotSpotX >= w) + hotSpotX = 0; + if (hotSpotY < 0 || hotSpotY >= h) + hotSpotY = 0; + + M_CURSORDATA->m_hCursor = NewHandle( sizeof( Cursor ) ) ; + M_CURSORDATA->m_disposeHandle = true ; + HLock( (Handle) M_CURSORDATA->m_hCursor ) ; + CursPtr cp = *(CursHandle)M_CURSORDATA->m_hCursor ; + memset( cp->data , 0 , sizeof( Bits16 ) ) ; + memset( cp->mask , 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 ; + + 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] ; + if ( bHasMask && r==mr && g==mg && b==mb ) + { + // masked area, does not appear anywhere + } + else + { + if ( (int)r + (int)g + (int)b < 0x60 ) + { + rowbits |= ( 1 << (15-x) ) ; + } + maskbits |= ( 1 << (15-x) ) ; + } + } + cp->data[y] = rowbits ; + cp->mask[y] = maskbits ; + } + if ( !bHasMask ) + { + memcpy( cp->mask , cp->data , sizeof( Bits16) ) ; + } + + cp->hotSpot.h = hotSpotX ; + cp->hotSpot.v = hotSpotY ; + HUnlock( (Handle) M_CURSORDATA->m_hCursor ) ; +} + wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int hotSpotY) { m_refData = new wxCursorRefData; + if ( flags == wxBITMAP_TYPE_MACCURSOR_RESOURCE ) + { + Str255 theName ; - // TODO: create cursor from a file + #if TARGET_CARBON + c2pstrcpy( (StringPtr) theName , cursor_file ) ; + #else + strcpy( (char *) theName , cursor_file ) ; + c2pstr( (char *) theName ) ; + #endif + + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetNamedResource( 'crsr' , theName ) ; + if ( M_CURSORDATA->m_hCursor ) + { + M_CURSORDATA->m_isColorCursor = true ; + } + else + { + M_CURSORDATA->m_hCursor = ::GetNamedResource( 'CURS' , theName ) ; + if ( M_CURSORDATA->m_hCursor ) + M_CURSORDATA->m_releaseHandle = true ; + } + } + else + { + wxImage image ; + image.LoadFile( cursor_file , flags ) ; + if( image.Ok() ) + { + image.SetOption(wxCUR_HOTSPOT_X,hotSpotX ) ; + image.SetOption(wxCUR_HOTSPOT_Y,hotSpotY ) ; + delete m_refData ; + CreateFromImage(image) ; + } + } } // Cursors by stock number @@ -56,122 +197,153 @@ wxCursor::wxCursor(int cursor_type) { m_refData = new wxCursorRefData; -/* TODO switch (cursor_type) { case wxCURSOR_WAIT: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_WAIT); + M_CURSORDATA->m_themeCursor = kThemeWatchCursor ; break; case wxCURSOR_IBEAM: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_IBEAM); + M_CURSORDATA->m_themeCursor = kThemeIBeamCursor ; break; case wxCURSOR_CROSS: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_CROSS); + M_CURSORDATA->m_themeCursor = kThemeCrossCursor; break; case wxCURSOR_SIZENWSE: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZENWSE); + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNWSE); + } break; case wxCURSOR_SIZENESW: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZENESW); + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNESW); + } break; case wxCURSOR_SIZEWE: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZEWE); + { + M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor; + } break; case wxCURSOR_SIZENS: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZENS); + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNS); + } break; - case wxCURSOR_CHAR: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_ARROW); + case wxCURSOR_SIZING: + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSize); + } break; - } case wxCURSOR_HAND: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_HAND"); + { + M_CURSORDATA->m_themeCursor = kThemePointingHandCursor; + } break; - } case wxCURSOR_BULLSEYE: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_BULLSEYE"); - break; - } + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorBullseye); + } + break; case wxCURSOR_PENCIL: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PENCIL"); - break; - } + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPencil); + } + break; case wxCURSOR_MAGNIFIER: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_MAGNIFIER"); - break; - } + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorMagnifier); + } + break; case wxCURSOR_NO_ENTRY: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_NO_ENTRY"); - break; - } - case wxCURSOR_LEFT_BUTTON: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_ARROW); - break; - } - case wxCURSOR_RIGHT_BUTTON: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_ARROW); - break; - } - case wxCURSOR_MIDDLE_BUTTON: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_ARROW); - break; - } - case wxCURSOR_SIZING: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_SIZING"); - break; - } + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorNoEntry); + } + break; case wxCURSOR_WATCH: { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_WATCH"); - break; - } - case wxCURSOR_SPRAYCAN: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_ROLLER"); + M_CURSORDATA->m_themeCursor = kThemeWatchCursor; break; } case wxCURSOR_PAINT_BRUSH: { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PBRUSH"); + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPaintBrush); break; } case wxCURSOR_POINT_LEFT: { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PLEFT"); + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPointLeft); break; } case wxCURSOR_POINT_RIGHT: { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PRIGHT"); + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPointRight); break; } case wxCURSOR_QUESTION_ARROW: { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_QARROW"); + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorQuestionArrow); break; } case wxCURSOR_BLANK: { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_BLANK"); + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorBlank); + break; + } + case wxCURSOR_RIGHT_ARROW: + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorRightArrow); + break; + } + case wxCURSOR_SPRAYCAN: + { + wxStAppResource resload ; + M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorRoller); break; } - default: + case wxCURSOR_CHAR: case wxCURSOR_ARROW: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_ARROW); + case wxCURSOR_LEFT_BUTTON: + case wxCURSOR_RIGHT_BUTTON: + case wxCURSOR_MIDDLE_BUTTON: + default: + M_CURSORDATA->m_themeCursor = kThemeArrowCursor ; break; - } -*/ + } + if ( M_CURSORDATA->m_themeCursor == -1 ) + M_CURSORDATA->m_releaseHandle = true ; +} +void wxCursor::MacInstall() const +{ + gMacCurrentCursor = *this ; + if ( m_refData && M_CURSORDATA->m_themeCursor != -1 ) + { + SetThemeCursor( M_CURSORDATA->m_themeCursor ) ; + } + else if ( m_refData && M_CURSORDATA->m_hCursor ) + { + if ( M_CURSORDATA->m_isColorCursor ) + ::SetCCursor( (CCrsrHandle) M_CURSORDATA->m_hCursor ) ; + else + ::SetCursor( * (CursHandle) M_CURSORDATA->m_hCursor ) ; + } + else + { + SetThemeCursor( kThemeArrowCursor ) ; + } } wxCursor::~wxCursor() @@ -181,7 +353,7 @@ wxCursor::~wxCursor() // Global cursor setting void wxSetCursor(const wxCursor& cursor) { - // TODO (optional on platforms with no global cursor) + cursor.MacInstall() ; }