From: Gabriel Ivăncescu Subject: [PATCH 6/9] user32/listbox: Use a helper to retrieve item string by index Message-Id: <228fbfc01fc43777a1ffd218414bce199479f4b5.1550145143.git.gabrielopcode@gmail.com> Date: Thu, 14 Feb 2019 14:06:45 +0200 In-Reply-To: References: Signed-off-by: Gabriel Ivăncescu --- dlls/user32/listbox.c | 51 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/dlls/user32/listbox.c b/dlls/user32/listbox.c index 70b55e1..7646486 100644 --- a/dlls/user32/listbox.c +++ b/dlls/user32/listbox.c @@ -153,6 +153,11 @@ static ULONG_PTR get_item_data( const LB_DESCR *descr, UINT index ) return (descr->style & LBS_NODATA) ? 0 : descr->items[index].data; } +static WCHAR *get_item_string( const LB_DESCR *descr, UINT index ) +{ + return HAS_STRINGS(descr) ? descr->items[index].str : NULL; +} + static BOOL is_item_selected( const LB_DESCR *descr, UINT index ) { if (!(descr->style & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL))) @@ -551,11 +556,11 @@ static void LISTBOX_PaintItem( LB_DESCR *descr, HDC hdc, const RECT *rect, INT index, UINT action, BOOL ignoreFocus ) { BOOL selected = FALSE, focused; - LB_ITEMDATA *item = NULL; + WCHAR *item_str = NULL; if (index < descr->nb_items) { - item = &descr->items[index]; + item_str = get_item_string(descr, index); selected = is_item_selected(descr, index); } @@ -598,7 +603,7 @@ static void LISTBOX_PaintItem( LB_DESCR *descr, HDC hdc, const RECT *rect, dis.itemData = get_item_data(descr, index); dis.rcItem = *rect; TRACE("[%p]: drawitem %d (%s) action=%02x state=%02x rect=%s\n", - descr->self, index, item ? debugstr_w(item->str) : "", action, + descr->self, index, debugstr_w(item_str), action, dis.itemState, wine_dbgstr_rect(rect) ); SendMessageW(descr->owner, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis); SelectClipRgn( hdc, hrgn ); @@ -620,22 +625,22 @@ static void LISTBOX_PaintItem( LB_DESCR *descr, HDC hdc, const RECT *rect, } TRACE("[%p]: painting %d (%s) action=%02x rect=%s\n", - descr->self, index, item ? debugstr_w(item->str) : "", action, + descr->self, index, debugstr_w(item_str), action, wine_dbgstr_rect(rect) ); - if (!item) + if (!item_str) ExtTextOutW( hdc, rect->left + 1, rect->top, ETO_OPAQUE | ETO_CLIPPED, rect, NULL, 0, NULL ); else if (!(descr->style & LBS_USETABSTOPS)) ExtTextOutW( hdc, rect->left + 1, rect->top, - ETO_OPAQUE | ETO_CLIPPED, rect, item->str, - strlenW(item->str), NULL ); + ETO_OPAQUE | ETO_CLIPPED, rect, item_str, + strlenW(item_str), NULL ); else { /* Output empty string to paint background in the full width. */ ExtTextOutW( hdc, rect->left + 1, rect->top, ETO_OPAQUE | ETO_CLIPPED, rect, NULL, 0, NULL ); TabbedTextOutW( hdc, rect->left + 1 , rect->top, - item->str, strlenW(item->str), + item_str, strlenW(item_str), descr->nb_tabs, descr->tabs, 0); } if (selected) @@ -797,28 +802,29 @@ static LRESULT LISTBOX_GetText( LB_DESCR *descr, INT index, LPWSTR buffer, BOOL } if (HAS_STRINGS(descr)) { + WCHAR *str = get_item_string(descr, index); + if (!buffer) { - len = strlenW(descr->items[index].str); + len = strlenW(str); if( unicode ) return len; - return WideCharToMultiByte( CP_ACP, 0, descr->items[index].str, len, - NULL, 0, NULL, NULL ); + return WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL ); } - TRACE("index %d (0x%04x) %s\n", index, index, debugstr_w(descr->items[index].str)); + TRACE("index %d (0x%04x) %s\n", index, index, debugstr_w(str)); __TRY /* hide a Delphi bug that passes a read-only buffer */ { if(unicode) { - strcpyW( buffer, descr->items[index].str ); + strcpyW(buffer, str); len = strlenW(buffer); } else { - len = WideCharToMultiByte(CP_ACP, 0, descr->items[index].str, -1, - (LPSTR)buffer, 0x7FFFFFFF, NULL, NULL) - 1; + len = WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)buffer, + 0x7FFFFFFF, NULL, NULL) - 1; } } __EXCEPT_PAGE_FAULT @@ -866,7 +872,7 @@ static INT LISTBOX_FindStringPos( LB_DESCR *descr, LPCWSTR str, BOOL exact ) { index = (min + max) / 2; if (HAS_STRINGS(descr)) - res = LISTBOX_lstrcmpiW( descr->locale, descr->items[index].str, str ); + res = LISTBOX_lstrcmpiW( descr->locale, get_item_string(descr, index), str ); else { COMPAREITEMSTRUCT cis; @@ -909,7 +915,7 @@ static INT LISTBOX_FindFileStrPos( LB_DESCR *descr, LPCWSTR str ) while (min != max) { INT index = (min + max) / 2; - LPCWSTR p = descr->items[index].str; + LPCWSTR p = get_item_string(descr, index); if (*p == '[') /* drive or directory */ { if (*str != '[') res = -1; @@ -1655,7 +1661,7 @@ static void LISTBOX_DeleteItem( LB_DESCR *descr, INT index ) { /* save the item data before it gets freed by LB_RESETCONTENT */ ULONG_PTR item_data = get_item_data(descr, index); - LPWSTR item_str = descr->items[index].str; + LPWSTR item_str = get_item_string(descr, index); if (!descr->nb_items) SendMessageW( descr->self, LB_RESETCONTENT, 0, 0 ); @@ -1677,8 +1683,7 @@ static void LISTBOX_DeleteItem( LB_DESCR *descr, INT index ) dis.itemData = item_data; SendMessageW( descr->owner, WM_DELETEITEM, id, (LPARAM)&dis ); } - if (HAS_STRINGS(descr)) - HeapFree( GetProcessHeap(), 0, item_str ); + HeapFree( GetProcessHeap(), 0, item_str ); } @@ -2730,9 +2735,9 @@ LRESULT ListBoxWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam return LB_ERR; } if (!HAS_STRINGS(descr)) return sizeof(ULONG_PTR); - if (unicode) return strlenW( descr->items[wParam].str ); - return WideCharToMultiByte( CP_ACP, 0, descr->items[wParam].str, - strlenW(descr->items[wParam].str), NULL, 0, NULL, NULL ); + if (unicode) return strlenW(get_item_string(descr, wParam)); + return WideCharToMultiByte( CP_ACP, 0, get_item_string(descr, wParam), + strlenW(get_item_string(descr, wParam)), NULL, 0, NULL, NULL ); case LB_GETCURSEL: if (descr->nb_items == 0) -- 2.20.1