From 063864481a2c872076cf4dd335008da990f66fdb Mon Sep 17 00:00:00 2001 From: Ryan Norton Date: Fri, 8 Oct 2004 23:19:39 +0000 Subject: [PATCH] final null-char wxString stuff, a couple of bugfixes and lots of code cleanups - fixups to changes.txt as per someone's suggestions - support for a lot more stock cursors on wxCocoa using the cursors from the carbon port git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29741 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 24 +-- src/cocoa/cursor.mm | 397 ++++++++++++++++++++++++++++++++---------- src/common/string.cpp | 202 +++++++++++---------- 3 files changed, 427 insertions(+), 196 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 97ccb2b81d..ad81c9d99e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -219,7 +219,7 @@ All: - sped up wxHTTP and wxFTP - added wxStringInput/OutputStreams - added wxFileConfig::Save(wxOutputStream) -- fixed wxString's behavior with inserted null characters (RN) +- fixed wxString's behavior with inserted null characters All (GUI): @@ -258,21 +258,21 @@ Unix: builds of wxWidgets and to return flags/libs for selected libraries only - wx-config has new --version-full option -wxCOCOA: +wxCocoa: -- Added UNICODE layer for OSX so that you can build wxWidgets in UNICODE mode in OSX 10.2 (and possibly lower) with wxMAC and wxCOCOA (RN) -- Fixed so that wxCOCOA runs in 10.2 (and possibly lower) (RN) -- Tooltips now supported - Enable and SetDelay not implemented (RN) -- wxSound now supported (RN) -- wxDisplay now supported (RN) -- Cursors (hopefully) now supported (RN) +- added Unicode compatability layer for OSX 10.2 +- fixed so that wxCocoa runs in OSX 10.2 +- Tooltips now supported +- wxSound now supported +- wxDisplay now supported +- Some stock cursors now supported wxMac: -- Fixed MLTE text control GetLineText and GetLineLength on OSX (RN) -- Added OSX wxTaskBarIcon implementation for the OSX Dock (RN) -- Added UNICODE layer for OSX so that you can build wxWidgets in UNICODE mode in OSX 10.2 (and possibly lower) with wxMAC and wxCOCOA (RN) -- Made wxGetKeyState work with nearly all keys (RN) +- fixed MLTE text control GetLineText and GetLineLength on OSX +- added OSX wxTaskBarIcon implementation for the OSX Dock +- added Unicode compatability layer for OSX 10.2 +- wxGetKeyState now works with nearly all wx key codes wxGTK: diff --git a/src/cocoa/cursor.mm b/src/cocoa/cursor.mm index 6b5011c61d..c7535c4d0a 100644 --- a/src/cocoa/cursor.mm +++ b/src/cocoa/cursor.mm @@ -27,6 +27,216 @@ IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap) #endif +typedef struct tagClassicCursor +{ + wxUint16 bits[16]; + wxUint16 mask[16]; + wxUint16 hotspot[2]; +}ClassicCursor; + +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 ; + +ClassicCursor gMacCursors[kwxCursorLast+1] = +{ + +{ +{0x0000, 0x03E0, 0x0630, 0x0808, 0x1004, 0x31C6, 0x2362, 0x2222, +0x2362, 0x31C6, 0x1004, 0x0808, 0x0630, 0x03E0, 0x0000, 0x0000}, +{0x0000, 0x03E0, 0x07F0, 0x0FF8, 0x1FFC, 0x3FFE, 0x3FFE, 0x3FFE, +0x3FFE, 0x3FFE, 0x1FFC, 0x0FF8, 0x07F0, 0x03E0, 0x0000, 0x0000}, +{0x0007, 0x0008} +}, + +{ +{0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, +{0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, +{0x0000, 0x0000} +}, + +{ +{0x00F0, 0x0088, 0x0108, 0x0190, 0x0270, 0x0220, 0x0440, 0x0440, +0x0880, 0x0880, 0x1100, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000}, +{0x00F0, 0x00F8, 0x01F8, 0x01F0, 0x03F0, 0x03E0, 0x07C0, 0x07C0, +0x0F80, 0x0F80, 0x1F00, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000}, +{0x000E, 0x0003} +}, + +{ +{0x0000, 0x1E00, 0x2100, 0x4080, 0x4080, 0x4080, 0x4080, 0x2180, +0x1FC0, 0x00E0, 0x0070, 0x0038, 0x001C, 0x000E, 0x0006, 0x0000}, +{0x3F00, 0x7F80, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0x7FC0, +0x3FE0, 0x1FF0, 0x00F8, 0x007C, 0x003E, 0x001F, 0x000F, 0x0007}, +{0x0004, 0x0004} +}, + +{ +{0x0000, 0x07E0, 0x1FF0, 0x3838, 0x3C0C, 0x6E0E, 0x6706, 0x6386, +0x61C6, 0x60E6, 0x7076, 0x303C, 0x1C1C, 0x0FF8, 0x07E0, 0x0000}, +{0x0540, 0x0FF0, 0x3FF8, 0x3C3C, 0x7E0E, 0xFF0F, 0x6F86, 0xE7C7, +0x63E6, 0xE1F7, 0x70FE, 0x707E, 0x3C3C, 0x1FFC, 0x0FF0, 0x0540}, +{0x0007, 0x0007} +}, + +{ +{0x0000, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0FE0, +0x1FF0, 0x1FF0, 0x0000, 0x1FF0, 0x1FF0, 0x1550, 0x1550, 0x1550}, +{0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x0FE0, 0x1FF0, +0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8}, +{0x000B, 0x0007} +}, + +{ +{0x00C0, 0x0140, 0x0640, 0x08C0, 0x3180, 0x47FE, 0x8001, 0x8001, +0x81FE, 0x8040, 0x01C0, 0x0040, 0x03C0, 0xC080, 0x3F80, 0x0000}, +{0x00C0, 0x01C0, 0x07C0, 0x0FC0, 0x3F80, 0x7FFE, 0xFFFF, 0xFFFF, +0xFFFE, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFF80, 0x3F80, 0x0000}, +{0x0006, 0x000F} +}, + +{ +{0x0100, 0x0280, 0x0260, 0x0310, 0x018C, 0x7FE3, 0x8000, 0x8000, +0x7F80, 0x0200, 0x0380, 0x0200, 0x03C0, 0x0107, 0x01F8, 0x0000}, +{0x0100, 0x0380, 0x03E0, 0x03F0, 0x01FC, 0x7FFF, 0xFFFF, 0xFFFF, +0xFFFF, 0x03FF, 0x03FF, 0x03FF, 0x03FF, 0x01FF, 0x01F8, 0x0000}, +{0x0006, 0x0000} +}, + +{ +{0x0000, 0x4078, 0x60FC, 0x71CE, 0x7986, 0x7C06, 0x7E0E, 0x7F1C, +0x7FB8, 0x7C30, 0x6C30, 0x4600, 0x0630, 0x0330, 0x0300, 0x0000}, +{0xC078, 0xE0FC, 0xF1FE, 0xFBFF, 0xFFCF, 0xFF8F, 0xFF1F, 0xFFBE, +0xFFFC, 0xFE78, 0xFF78, 0xEFF8, 0xCFF8, 0x87F8, 0x07F8, 0x0300}, +{0x0001, 0x0001} +}, + +{ +{0x0000, 0x0002, 0x0006, 0x000E, 0x001E, 0x003E, 0x007E, 0x00FE, +0x01FE, 0x003E, 0x0036, 0x0062, 0x0060, 0x00C0, 0x00C0, 0x0000}, +{0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, +0x03FF, 0x07FF, 0x007F, 0x00F7, 0x00F3, 0x01E1, 0x01E0, 0x01C0}, +{0x0001, 0x000E} +}, + +{ +{0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0080, 0x0080, 0x1FFC, +0x1FFC, 0x0080, 0x0080, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000}, +{0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0FF8, 0x01C0, 0x3FFE, 0x3FFE, +0x3FFE, 0x3FFE, 0x01C0, 0x0FF8, 0x07F0, 0x03E0, 0x01C0, 0x0080}, +{0x0007, 0x0008} +}, + +{ +{0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0888, 0x188C, 0x3FFE, +0x188C, 0x0888, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000, 0x0000}, +{0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0BE8, 0x1DDC, 0x3FFE, 0x7FFF, +0x3FFE, 0x1DDC, 0x0BE8, 0x07F0, 0x03E0, 0x01C0, 0x0080, 0x0000}, +{0x0007, 0x0008} +}, + +{ +{0x0000, 0x001E, 0x000E, 0x060E, 0x0712, 0x03A0, 0x01C0, 0x00E0, +0x0170, 0x1238, 0x1C18, 0x1C00, 0x1E00, 0x0000, 0x0000, 0x0000}, +{0x007F, 0x003F, 0x0E1F, 0x0F0F, 0x0F97, 0x07E3, 0x03E1, 0x21F0, +0x31F8, 0x3A7C, 0x3C3C, 0x3E1C, 0x3F00, 0x3F80, 0x0000, 0x0000}, +{0x0006, 0x0009} +}, + +{ +{0x0000, 0x7800, 0x7000, 0x7060, 0x48E0, 0x05C0, 0x0380, 0x0700, +0x0E80, 0x1C48, 0x1838, 0x0038, 0x0078, 0x0000, 0x0000, 0x0000}, +{0xFE00, 0xFC00, 0xF870, 0xF0F0, 0xE9F0, 0xC7E0, 0x87C0, 0x0F84, +0x1F8C, 0x3E5C, 0x3C3C, 0x387C, 0x00FC, 0x01FC, 0x0000, 0x0000}, +{0x0006, 0x0006} +}, + +{ +{0x0006, 0x000E, 0x001C, 0x0018, 0x0020, 0x0040, 0x00F8, 0x0004, +0x1FF4, 0x200C, 0x2AA8, 0x1FF0, 0x1F80, 0x3800, 0x6000, 0x8000}, +{0x000F, 0x001F, 0x003E, 0x007C, 0x0070, 0x00E0, 0x01FC, 0x3FF6, +0x7FF6, 0x7FFE, 0x7FFC, 0x7FF8, 0x3FF0, 0x7FC0, 0xF800, 0xE000}, +{0x000A, 0x0006} +}, + +} ; + +NSCursor* wxGetStockCursor( short sIndex ) +{ + ClassicCursor* pCursor = &gMacCursors[sIndex]; + + //Classic mac cursors are 1bps 16x16 black and white with a + //identical mask that is 1 for on and 0 for off + NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)]; + + //NSCursor takes an NSImage takes a number of Representations - here + //we need only one for the raw data + NSBitmapImageRep *theRep = + [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes: nil // Allocate the buffer for us :) + pixelsWide: 16 + pixelsHigh: 16 + bitsPerSample: 1 + samplesPerPixel: 2 + hasAlpha: YES // Well, more like a mask... + isPlanar: NO + colorSpaceName: NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white + bytesPerRow: 0 // I don't care - figure it out for me :) + bitsPerPixel: 2]; // bitsPerSample * samplesPerPixel + + //unsigned int is better to put data in then a void* + //note that working with bitfields would be a lot better here - + //but since it breaks some compilers... + wxUint32 *data = (wxUint32 *)[theRep bitmapData]; + + //traverse through the bitmap data + for (int i = 0; i < 16; ++i) + { + //bit alpha bit alpha ... :D + + //Notice the = instead of |= - + //this is to avoid doing a memset earlier + data[i] = 0; + + //do the rest of those bits and alphas :) + for (int shift = 0; shift < 32; ++shift) + { + data[i] |= (!!(pCursor->bits[i] & (1 << (shift >> 1) ))) << shift; + data[i] |= (!(pCursor->mask[i] & (1 << (shift >> 1) ))) << ++shift; + } + } + + //add the representation (data) to the image + [theImage addRepresentation:theRep]; + + //create the new cursor + NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage + hotSpot:NSMakePoint(pCursor->hotspot[0], pCursor->hotspot[1]) + ]; + + //do the usual cleanups + [theRep release]; + [theImage release]; + + //return the new cursor + return theCursor; +} + wxCursorRefData::wxCursorRefData() : m_width(32), m_height(32), m_hCursor(nil) { @@ -90,115 +300,120 @@ wxCursor::wxCursor(int cursor_type) case wxCURSOR_ARROW: M_CURSORDATA->m_hCursor = [[NSCursor arrowCursor] retain]; break; -/* TODO +/* TODO: + case wxCURSOR_COPY_ARROW: + M_CURSORDATA->m_themeCursor = kThemeCopyArrowCursor ; + break; case wxCURSOR_WAIT: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_WAIT); - break; + M_CURSORDATA->m_themeCursor = kThemeWatchCursor ; + break; case wxCURSOR_CROSS: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_CROSS); - break; + M_CURSORDATA->m_themeCursor = kThemeCrossCursor; + break; case wxCURSOR_SIZENWSE: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZENWSE); - break; + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNWSE); + } + break; +*/ case wxCURSOR_SIZENESW: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZENESW); - break; + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNESW); + } + break; +/* TODO: case wxCURSOR_SIZEWE: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZEWE); - break; + { + M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor; + } + break; +*/ case wxCURSOR_SIZENS: - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_SIZENS); - break; - case wxCURSOR_CHAR: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(NULL, IDC_ARROW); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNS); + } + break; + case wxCURSOR_SIZING: + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSize); + } + break; +/* TODO: case wxCURSOR_HAND: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_HAND"); - break; - } + { + M_CURSORDATA->m_themeCursor = kThemePointingHandCursor; + } + break; +*/ case wxCURSOR_BULLSEYE: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_BULLSEYE"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBullseye); + } + break; case wxCURSOR_PENCIL: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PENCIL"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPencil); + } + break; case wxCURSOR_MAGNIFIER: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_MAGNIFIER"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(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; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorNoEntry); + } + break; +/* TODO: 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"); - break; - } + { + M_CURSORDATA->m_themeCursor = kThemeWatchCursor; + break; + } +*/ case wxCURSOR_PAINT_BRUSH: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PBRUSH"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPaintBrush); + break; + } case wxCURSOR_POINT_LEFT: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PLEFT"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointLeft); + break; + } case wxCURSOR_POINT_RIGHT: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_PRIGHT"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointRight); + break; + } case wxCURSOR_QUESTION_ARROW: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_QARROW"); - break; - } + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorQuestionArrow); + break; + } case wxCURSOR_BLANK: - { - M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), "wxCURSOR_BLANK"); - break; - } -*/ + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBlank); + break; + } + case wxCURSOR_RIGHT_ARROW: + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRightArrow); + break; + } + case wxCURSOR_SPRAYCAN: + { + M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRoller); + break; + } + case wxCURSOR_CHAR: + case wxCURSOR_LEFT_BUTTON: + case wxCURSOR_RIGHT_BUTTON: + case wxCURSOR_MIDDLE_BUTTON: default: - break; - } - + break; + } } wxCursor::~wxCursor() diff --git a/src/common/string.cpp b/src/common/string.cpp index f27e32da9b..2c81c15393 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -208,7 +208,7 @@ void wxStringBase::InitWith(const wxChar *psz, size_t nPos, size_t nLength) wxFAIL_MSG( _T("out of memory in wxStringBase::InitWith") ); return; } - memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar)); + wxMemcpy(m_pchData, psz + nPos, nLength); } } @@ -985,7 +985,96 @@ int STRINGCLASS::compare(size_t nStart, size_t nLen, // =========================================================================== // --------------------------------------------------------------------------- -// construction +// common conversion routines +// --------------------------------------------------------------------------- + +#if wxUSE_WCHAR_T + +//Convert a wide character string to a multi-byte character string of +//a specified length, ignoring intermittent null characters +inline wxCharBuffer wxMbstr(const wchar_t* szString, size_t nStringLen, wxMBConv& conv) +{ + const wchar_t* szEnd = szString + nStringLen + 1; + const wchar_t* szPos = szString; + const wchar_t* szStart = szPos; + + //Create the buffer we'll return on success + wxCharBuffer buffer(nStringLen + 1); + + //Convert the string until the length() is reached, continuing the + //loop every time a null character is reached + while(szPos != szEnd) + { + wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true + + //Get the length of the current (sub)string + size_t nLen = conv.WC2MB(NULL, szPos, 0); + + wxASSERT(nLen != (size_t)-1); //should not be true! If it is system wctomb could be bad + + //Convert the current (sub)string + if ( conv.WC2MB(&buffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) + { + //error - return empty buffer + wxFAIL_MSG(wxT("Error converting wide-character string to a multi-byte string")); + buffer.data()[0] = '\0'; + return buffer; + } + + //Increment to next (sub)string + //Note that we have to use wxWcslen here instead of nLen + //here because XX2XX gives us the size of the output buffer, + //not neccessarly the length of the string + szPos += wxWcslen(szPos) + 1; + } + + return buffer; //success - return converted string +} + +//Convert a multi-byte character string to a wide character string of +//a specified length, ignoring intermittent null characters +inline wxWCharBuffer wxWcstr(const char* szString, size_t nStringLen, wxMBConv& conv) +{ + const char* szEnd = szString + nStringLen + 1; + const char* szPos = szString; + const char* szStart = szPos; + + wxWCharBuffer buffer(nStringLen + 1); + + //Convert the string until the length() is reached, continuing the + //loop every time a null character is reached + while(szPos != szEnd) + { + wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true + + //Get the length of the current (sub)string + size_t nLen = conv.MB2WC(NULL, szPos, 0); + + wxASSERT(nLen != (size_t)-1); //should not be true! If it is system mbtowc could be bad + + //Convert the current (sub)string + if ( conv.MB2WC(&buffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) + { + //error - return empty buffer + wxFAIL_MSG(wxT("Error converting multi-byte string to a wide-character string")); + buffer.data()[0] = '\0'; + return buffer; + } + + //Increment to next (sub)string + //Note that we have to use strlen here instead of nLen + //here because XX2XX gives us the size of the output buffer, + //not neccessarly the length of the string + szPos += strlen(szPos) + 1; + } + + return buffer; //success - return converted string +} + +#endif //wxUSE_WCHAR_T + +// --------------------------------------------------------------------------- +// construction and conversion // --------------------------------------------------------------------------- #if wxUSE_UNICODE @@ -1012,7 +1101,10 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) if ( psz ) { // calculate the needed size ourselves or use the provided one - nLen = conv.MB2WC(NULL, psz, 0); + if (nLength == npos) + nLen = strlen(psz); + else + nLen = nLength; } else { @@ -1023,56 +1115,15 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) // anything to do? if ( (nLen != 0) && (nLen != (size_t)-1) ) { - if ( !Alloc(nLen) ) - { - wxFAIL_MSG( _T("out of memory in wxString::wxString") ); - } - else - { - wxWCharBuffer buf(nLen); - // MB2WC wants the buffer size, not the string length hence +1 - nLen = conv.MB2WC(buf.data(), psz, nLen + 1); - - if ( nLen != (size_t)-1 ) - { - // initialized ok, set the real length as nLength specified by - // the caller could be greater than the real string length - assign(buf.data(), nLen); - return; - } - //else: the conversion failed -- leave the string empty (what else?) - } + //do the actual conversion (if it fails we get an empty string) + (*this) = wxWcstr(psz, nLen, conv); } } +//Convert wxString in Unicode mode to a multi-byte string const wxCharBuffer wxString::mb_str(wxMBConv& conv) const { - const wxChar* szEnd = (*this).c_str() + length() + 1; - const wxChar* szPos = (*this).c_str(); - const wxChar* szStart = szPos; - - wxCharBuffer buffer(length() + 1); - - //Convert the string until the length() is reached, continuing the - //loop every time a null character is reached - while(szPos != szEnd) - { - size_t nLen = conv.WC2MB(NULL, szPos, 0); - - wxASSERT(nLen != (size_t)-1); //should not be true! If it is system wctomb could be bad - - if ( conv.WC2MB(&buffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) - { - //error - return empty buffer - wxFAIL_MSG(wxT("Error converting wide-character string to a multi-byte string")); - buffer.data()[0] = '\0'; - return buffer; - } - - szPos += nLen + 1; - } - - return buffer; + return wxMbstr((*this).c_str(), length(), conv); } #else // ANSI @@ -1100,7 +1151,10 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) if ( pwz ) { // calculate the needed size ourselves or use the provided one - nLen = conv.WC2MB(NULL, pwz, 0); + if (nLength == npos) + nLen = wxWcslen(pwz); + else + nLen = nLength; } else { @@ -1111,55 +1165,17 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) // anything to do? if ( (nLen != 0) && (nLen != (size_t)-1) ) { - if ( !Alloc(nLen) ) - { - wxFAIL_MSG( _T("out of memory in wxString::wxString") ); - } - else - { - wxCharBuffer buf(nLen); - // WC2MB wants the buffer size, not the string length - if ( conv.WC2MB(buf.data(), pwz, nLen + 1) != (size_t)-1 ) - { - // initialized ok - assign(buf.data(), nLen); - return; - } - //else: the conversion failed -- leave the string empty (what else?) - } + //do the actual conversion (if it fails we get an empty string) + (*this) = wxMbstr(pwz, nLen, conv); } - - // leave empty } +//Converts this string to a wide character string if unicode +//mode is not enabled and wxUSE_WCHAR_T is enabled const wxWCharBuffer wxString::wc_str(wxMBConv& conv) const { - const wxChar* szEnd = (*this).c_str() + length() + 1; - const wxChar* szPos = (*this).c_str(); - const wxChar* szStart = szPos; - - wxWCharBuffer buffer(length() + 1); - - //Convert the string until the length() is reached, continuing the - //loop every time a null character is reached - while(szPos != szEnd) - { - size_t nLen = conv.MB2WC(NULL, szPos, 0); - - wxASSERT(nLen != (size_t)-1); //should not be true! If it is system mbtowc could be bad - - if ( conv.MB2WC(&buffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) - { - //error - return empty buffer - wxFAIL_MSG(wxT("Error converting multi-byte string to a wide-character string")); - buffer.data()[0] = '\0'; - return buffer; - } - - szPos += nLen + 1; - } - - return buffer; + //Do the actual conversion (will return a blank string on error) + return wxWcstr((*this).c_str(), length(), conv); } #endif // wxUSE_WCHAR_T -- 2.45.2