diff --git a/dlls/comctl32/toolbar.c b/dlls/comctl32/toolbar.c index 8f92a17f06e..4787fcebf0b 100644 --- a/dlls/comctl32/toolbar.c +++ b/dlls/comctl32/toolbar.c @@ -148,13 +148,28 @@ typedef struct #define TOOLBAR_HasDropDownArrows(exStyle) ((exStyle & TBSTYLE_EX_DRAWDDARROWS) ? TRUE : FALSE) +static LPWSTR +TOOLBAR_GetText(TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr) +{ + LPWSTR lpText = NULL; + + /* FIXME: iString == -1 is undocumented */ + if ((HIWORD(btnPtr->iString) != 0) && (btnPtr->iString != -1)) + lpText = (LPWSTR)btnPtr->iString; + else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings)) + lpText = infoPtr->strings[btnPtr->iString]; + + return lpText; +} + static void TOOLBAR_DumpButton(TOOLBAR_INFO *infoPtr, TBUTTON_INFO *bP, INT btn_num, BOOL internal) { if (TRACE_ON(toolbar)){ - TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, string=%d\n", + TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=%x\n", btn_num, bP->idCommand, bP->iBitmap, bP->fsState, bP->fsStyle, bP->dwData, bP->iString); + TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP))); if (internal) TRACE("button %d id %d, hot=%s, row=%d, rect=(%d,%d)-(%d,%d)\n", btn_num, bP->idCommand, @@ -226,20 +241,6 @@ TOOLBAR_SendNotify (NMHDR *nmhdr, TOOLBAR_INFO *infoPtr, UINT code) (WPARAM) nmhdr->idFrom, (LPARAM)nmhdr); } -static LPWSTR -TOOLBAR_GetText(TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr) -{ - LPWSTR lpText = NULL; - - /* FIXME: iString == -1 is undocumented */ - if ((HIWORD(btnPtr->iString) != 0) && (btnPtr->iString != -1)) - lpText = (LPWSTR)btnPtr->iString; - else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings)) - lpText = infoPtr->strings[btnPtr->iString]; - - return lpText; -} - /*********************************************************************** * TOOLBAR_GetBitmapIndex * @@ -443,7 +444,7 @@ TOOLBAR_DrawString (TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, /* get a pointer to the text */ lpText = TOOLBAR_GetText(infoPtr, btnPtr); - TRACE ("lpText: %s\n", debugstr_w(lpText)); + TRACE ("Stringtext: %s\n", debugstr_w(lpText)); /* draw text */ if (lpText) { @@ -814,11 +815,9 @@ TOOLBAR_MeasureString(TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, lpSize->cx = 0; lpSize->cy = 0; - if (!(btnPtr->fsState & TBSTATE_HIDDEN) && - (btnPtr->iString > -1) && - (btnPtr->iString < infoPtr->nNumStrings)) + if (!(btnPtr->fsState & TBSTATE_HIDDEN) ) { - LPWSTR lpText = infoPtr->strings[btnPtr->iString]; + LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr); /* first get size of all the text */ GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize); @@ -2154,7 +2153,7 @@ TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam) if ((wParam) && (HIWORD(lParam) == 0)) { char szString[256]; - INT len, lenW; + INT len; TRACE("adding string from resource!\n"); len = LoadStringA ((HINSTANCE)wParam, (UINT)lParam, @@ -2175,15 +2174,13 @@ TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam) COMCTL32_Free (oldStrings); } - lenW = MultiByteToWideChar( CP_ACP, 0, szString, -1, NULL, 0 ); - infoPtr->strings[infoPtr->nNumStrings] = COMCTL32_Alloc (sizeof(WCHAR)*lenW); - MultiByteToWideChar( CP_ACP, 0, szString, -1, - infoPtr->strings[infoPtr->nNumStrings], lenW ); + /*COMCTL32_Alloc zeros out the allocated memory*/ + Str_SetPtrAtoW (&infoPtr->strings[infoPtr->nNumStrings], szString ); infoPtr->nNumStrings++; } else { LPSTR p = (LPSTR)lParam; - INT len, lenW; + INT len; if (p == NULL) return -1; @@ -2207,10 +2204,7 @@ TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam) COMCTL32_Free (oldStrings); } - lenW = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 ); - infoPtr->strings[infoPtr->nNumStrings] = COMCTL32_Alloc (sizeof(WCHAR)*lenW); - MultiByteToWideChar( CP_ACP, 0, p, -1, - infoPtr->strings[infoPtr->nNumStrings], lenW ); + Str_SetPtrAtoW (&infoPtr->strings[infoPtr->nNumStrings], p ); infoPtr->nNumStrings++; p += (len+1); @@ -2284,9 +2278,7 @@ TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam) COMCTL32_Free (oldStrings); } - infoPtr->strings[infoPtr->nNumStrings] = - COMCTL32_Alloc (sizeof(WCHAR)*(len+1)); - strcpyW (infoPtr->strings[infoPtr->nNumStrings], szString); + Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], szString); infoPtr->nNumStrings++; } } @@ -2315,9 +2307,7 @@ TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam) COMCTL32_Free (oldStrings); } - infoPtr->strings[infoPtr->nNumStrings] = - COMCTL32_Alloc (sizeof(WCHAR)*(len+1)); - strcpyW (infoPtr->strings[infoPtr->nNumStrings], p); + Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p); infoPtr->nNumStrings++; p += (len+1); @@ -2723,14 +2713,9 @@ TOOLBAR_GetButtonInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam) if (lpTbInfo->dwMask & TBIF_STYLE) lpTbInfo->fsStyle = btnPtr->fsStyle; if (lpTbInfo->dwMask & TBIF_TEXT) { - if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings)) - { - if (!WideCharToMultiByte( CP_ACP, 0, (LPWSTR)infoPtr->strings[btnPtr->iString], -1, - lpTbInfo->pszText, lpTbInfo->cchText, NULL, NULL )) - lpTbInfo->pszText[lpTbInfo->cchText-1] = 0; + LPWSTR lpText = TOOLBAR_GetText(infoPtr,btnPtr); + Str_GetPtrWtoA (lpText, lpTbInfo->pszText,lpTbInfo->cchText); } - else lpTbInfo->pszText[0]=0; - } return nIndex; } @@ -2770,10 +2755,8 @@ TOOLBAR_GetButtonInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam) if (lpTbInfo->dwMask & TBIF_STYLE) lpTbInfo->fsStyle = btnPtr->fsStyle; if (lpTbInfo->dwMask & TBIF_TEXT) { - if ((btnPtr->iString >= 0) || (btnPtr->iString < infoPtr->nNumStrings)) - lstrcpynW (lpTbInfo->pszText, - (LPWSTR)infoPtr->strings[btnPtr->iString], - lpTbInfo->cchText); + LPWSTR lpText = TOOLBAR_GetText(infoPtr,btnPtr); + Str_GetPtrW (lpText,lpTbInfo->pszText,lpTbInfo->cchText); } return nIndex; @@ -2797,23 +2780,19 @@ static LRESULT TOOLBAR_GetButtonTextA (HWND hwnd, WPARAM wParam, LPARAM lParam) { TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd); - INT nIndex, nStringIndex; + INT nIndex; + LPWSTR lpText; + + if (lParam == 0) + return -1; nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE); if (nIndex == -1) return -1; - nStringIndex = infoPtr->buttons[nIndex].iString; + lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]); - TRACE("index=%d stringIndex=%d\n", nIndex, nStringIndex); - - if ((nStringIndex < 0) || (nStringIndex >= infoPtr->nNumStrings)) - return -1; - - if (lParam == 0) - return -1; - - return WideCharToMultiByte( CP_ACP, 0, (LPWSTR)infoPtr->strings[nStringIndex], -1, + return WideCharToMultiByte( CP_ACP, 0, lpText, -1, (LPSTR)lParam, 0x7fffffff, NULL, NULL ) - 1; } @@ -2822,25 +2801,21 @@ static LRESULT TOOLBAR_GetButtonTextW (HWND hwnd, WPARAM wParam, LPARAM lParam) { TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd); - INT nIndex, nStringIndex; + INT nIndex; + LPWSTR lpText; + + if (lParam == 0) + return -1; nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE); if (nIndex == -1) return -1; - nStringIndex = infoPtr->buttons[nIndex].iString; + lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]); - TRACE("index=%d stringIndex=%d\n", nIndex, nStringIndex); + strcpyW ((LPWSTR)lParam, lpText); - if ((nStringIndex < 0) || (nStringIndex >= infoPtr->nNumStrings)) - return -1; - - if (lParam == 0) - return -1; - - strcpyW ((LPWSTR)lParam, (LPWSTR)infoPtr->strings[nStringIndex]); - - return strlenW ((LPWSTR)infoPtr->strings[nStringIndex]); + return strlenW (lpText); } @@ -3163,26 +3138,18 @@ TOOLBAR_InsertButtonA (HWND hwnd, WPARAM wParam, LPARAM lParam) nIndex * sizeof(TBUTTON_INFO)); } - /* if passed string and not index, then add string */ - if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) { - int len; - LPSTR ptr; - - len = strlen((char*)lpTbb->iString) + 2; - ptr = COMCTL32_Alloc(len*sizeof(char)); - strcpy(ptr, (char*)lpTbb->iString); - ptr[len - 1] = 0; /* ended by two '\0' */ - lpTbb->iString = TOOLBAR_AddStringA(hwnd, 0, (LPARAM)ptr); - COMCTL32_Free(ptr); - } - /* insert new button */ infoPtr->buttons[nIndex].iBitmap = lpTbb->iBitmap; infoPtr->buttons[nIndex].idCommand = lpTbb->idCommand; infoPtr->buttons[nIndex].fsState = lpTbb->fsState; infoPtr->buttons[nIndex].fsStyle = lpTbb->fsStyle; infoPtr->buttons[nIndex].dwData = lpTbb->dwData; - infoPtr->buttons[nIndex].iString = lpTbb->iString; + /* if passed string and not index, then add string */ + if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) { + Str_SetPtrAtoW ((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPCSTR )lpTbb->iString); + } + else + infoPtr->buttons[nIndex].iString = lpTbb->iString; if ((infoPtr->hwndToolTip) && !(lpTbb->fsStyle & TBSTYLE_SEP)) { TTTOOLINFOA ti; @@ -3242,18 +3209,6 @@ TOOLBAR_InsertButtonW (HWND hwnd, WPARAM wParam, LPARAM lParam) nIndex * sizeof(TBUTTON_INFO)); } - /* if passed string and not index, then add string */ - if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) { - int len; - LPWSTR ptr; - - len = strlenW((WCHAR*)lpTbb->iString) + 2; - ptr = COMCTL32_Alloc(len*sizeof(WCHAR)); - strcpyW(ptr, (WCHAR*)lpTbb->iString); - ptr[len - 1] = 0; /* ended by two '\0' */ - lpTbb->iString = TOOLBAR_AddStringW(hwnd, 0, (LPARAM)ptr); - COMCTL32_Free(ptr); - } /* insert new button */ infoPtr->buttons[nIndex].iBitmap = lpTbb->iBitmap; @@ -3261,7 +3216,12 @@ TOOLBAR_InsertButtonW (HWND hwnd, WPARAM wParam, LPARAM lParam) infoPtr->buttons[nIndex].fsState = lpTbb->fsState; infoPtr->buttons[nIndex].fsStyle = lpTbb->fsStyle; infoPtr->buttons[nIndex].dwData = lpTbb->dwData; - infoPtr->buttons[nIndex].iString = lpTbb->iString; + /* if passed string and not index, then add string */ + if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) { + Str_SetPtrW ((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPWSTR)lpTbb->iString); + } + else + infoPtr->buttons[nIndex].iString = lpTbb->iString; if ((infoPtr->hwndToolTip) && !(lpTbb->fsStyle & TBSTYLE_SEP)) { TTTOOLINFOW ti; @@ -3542,21 +3502,13 @@ TOOLBAR_SetButtonInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam) if (lptbbi->dwMask & TBIF_STYLE) btnPtr->fsStyle = lptbbi->fsStyle; - if (lptbbi->dwMask & TBIF_TEXT) { - if ((btnPtr->iString >= 0) || - (btnPtr->iString < infoPtr->nNumStrings)) { - TRACE("Ooooooch\n"); -#if 0 - WCHAR **lpString = &infoPtr->strings[btnPtr->iString]; - INT len = lstrlenA (lptbbi->pszText); - *lpString = COMCTL32_ReAlloc (lpString, sizeof(WCHAR)*(len+1)); -#endif - - /* this is the ultimate sollution */ -/* Str_SetPtrA (&infoPtr->strings[btnPtr->iString], lptbbi->pszText); */ - } + if ((lptbbi->dwMask & TBIF_TEXT) && ((INT)lptbbi->pszText != -1)) { + if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1)) + /* iString is index, zero it to make Str_SetPtr succeed */ + btnPtr->iString=0; + + Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText); } - return TRUE; } @@ -3593,20 +3545,12 @@ TOOLBAR_SetButtonInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam) if (lptbbi->dwMask & TBIF_STYLE) btnPtr->fsStyle = lptbbi->fsStyle; - if (lptbbi->dwMask & TBIF_TEXT) { - if ((btnPtr->iString >= 0) || - (btnPtr->iString < infoPtr->nNumStrings)) { -#if 0 - WCHAR **lpString = &infoPtr->strings[btnPtr->iString]; - INT len = lstrlenW (lptbbi->pszText); - *lpString = COMCTL32_ReAlloc (lpString, sizeof(WCHAR)*(len+1)); -#endif - - /* this is the ultimate solution */ -/* Str_SetPtrA (&infoPtr->strings[btnPtr->iString], lptbbi->pszText); */ - } + if ((lptbbi->dwMask & TBIF_TEXT) && ((INT)lptbbi->pszText != -1)) { + if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1)) + /* iString is index, zero it to make Str_SetPtr succeed */ + btnPtr->iString=0; + Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText); } - return TRUE; }