Reading joystick 5 when we only support 4 should fail instead of
[wine/multimedia.git] / controls / listbox.c
blob3405121b27c94a3541c980b06871d523a5c6022f
1 /*
2 * Listbox controls
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <string.h>
8 #include "wine/winuser16.h"
9 #include "winuser.h"
10 #include "winerror.h"
11 #include "drive.h"
12 #include "heap.h"
13 #include "spy.h"
14 #include "win.h"
15 #include "combo.h"
16 #include "debug.h"
17 #include "tweak.h"
19 /* Unimplemented yet:
20 * - LBS_NOSEL
21 * - LBS_USETABSTOPS
22 * - Unicode
23 * - Locale handling
26 /* Items array granularity */
27 #define LB_ARRAY_GRANULARITY 16
29 /* Scrolling timeout in ms */
30 #define LB_SCROLL_TIMEOUT 50
32 /* Listbox system timer id */
33 #define LB_TIMER_ID 2
35 /* Item structure */
36 typedef struct
38 LPSTR str; /* Item text */
39 BOOL32 selected; /* Is item selected? */
40 UINT32 height; /* Item height (only for OWNERDRAWVARIABLE) */
41 DWORD data; /* User data */
42 } LB_ITEMDATA;
44 /* Listbox structure */
45 typedef struct
47 HANDLE32 heap; /* Heap for this listbox */
48 HWND32 owner; /* Owner window to send notifications to */
49 UINT32 style; /* Window style */
50 INT32 width; /* Window width */
51 INT32 height; /* Window height */
52 LB_ITEMDATA *items; /* Array of items */
53 INT32 nb_items; /* Number of items */
54 INT32 top_item; /* Top visible item */
55 INT32 selected_item; /* Selected item */
56 INT32 focus_item; /* Item that has the focus */
57 INT32 anchor_item; /* Anchor item for extended selection */
58 INT32 item_height; /* Default item height */
59 INT32 page_size; /* Items per listbox page */
60 INT32 column_width; /* Column width for multi-column listboxes */
61 INT32 horz_extent; /* Horizontal extent (0 if no hscroll) */
62 INT32 horz_pos; /* Horizontal position */
63 INT32 nb_tabs; /* Number of tabs in array */
64 INT32 *tabs; /* Array of tabs */
65 BOOL32 caret_on; /* Is caret on? */
66 HFONT32 font; /* Current font */
67 LCID locale; /* Current locale for string comparisons */
68 LPHEADCOMBO lphc; /* ComboLBox */
69 } LB_DESCR;
72 #define IS_OWNERDRAW(descr) \
73 ((descr)->style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE))
75 #define HAS_STRINGS(descr) \
76 (!IS_OWNERDRAW(descr) || ((descr)->style & LBS_HASSTRINGS))
78 #define SEND_NOTIFICATION(wnd,descr,code) \
79 (SendMessage32A( (descr)->owner, WM_COMMAND, \
80 MAKEWPARAM((((descr)->lphc)?ID_CB_LISTBOX:(wnd)->wIDmenu), (code) ), (wnd)->hwndSelf ))
82 /* Current timer status */
83 typedef enum
85 LB_TIMER_NONE,
86 LB_TIMER_UP,
87 LB_TIMER_LEFT,
88 LB_TIMER_DOWN,
89 LB_TIMER_RIGHT
90 } TIMER_DIRECTION;
92 static TIMER_DIRECTION LISTBOX_Timer = LB_TIMER_NONE;
95 /***********************************************************************
96 * LISTBOX_Dump
98 void LISTBOX_Dump( WND *wnd )
100 INT32 i;
101 LB_ITEMDATA *item;
102 LB_DESCR *descr = *(LB_DESCR **)wnd->wExtra;
104 DUMP( "Listbox:\n" );
105 DUMP( "hwnd=%04x descr=%08x heap=%08x items=%d top=%d\n",
106 wnd->hwndSelf, (UINT32)descr, descr->heap, descr->nb_items,
107 descr->top_item );
108 for (i = 0, item = descr->items; i < descr->nb_items; i++, item++)
110 DUMP( "%4d: %-40s %d %08lx %3d\n",
111 i, item->str, item->selected, item->data, item->height );
116 /***********************************************************************
117 * LISTBOX_GetCurrentPageSize
119 * Return the current page size
121 static INT32 LISTBOX_GetCurrentPageSize( WND *wnd, LB_DESCR *descr )
123 INT32 i, height;
124 if (!(descr->style & LBS_OWNERDRAWVARIABLE)) return descr->page_size;
125 for (i = descr->top_item, height = 0; i < descr->nb_items; i++)
127 if ((height += descr->items[i].height) > descr->height) break;
129 if (i == descr->top_item) return 1;
130 else return i - descr->top_item;
134 /***********************************************************************
135 * LISTBOX_GetMaxTopIndex
137 * Return the maximum possible index for the top of the listbox.
139 static INT32 LISTBOX_GetMaxTopIndex( WND *wnd, LB_DESCR *descr )
141 INT32 max, page;
143 if (descr->style & LBS_OWNERDRAWVARIABLE)
145 page = descr->height;
146 for (max = descr->nb_items - 1; max >= 0; max--)
147 if ((page -= descr->items[max].height) < 0) break;
148 if (max < descr->nb_items - 1) max++;
150 else if (descr->style & LBS_MULTICOLUMN)
152 if ((page = descr->width / descr->column_width) < 1) page = 1;
153 max = (descr->nb_items + descr->page_size - 1) / descr->page_size;
154 max = (max - page) * descr->page_size;
156 else
158 max = descr->nb_items - descr->page_size;
160 if (max < 0) max = 0;
161 return max;
165 /***********************************************************************
166 * LISTBOX_UpdateScroll
168 * Update the scrollbars. Should be called whenever the content
169 * of the listbox changes.
171 static void LISTBOX_UpdateScroll( WND *wnd, LB_DESCR *descr )
173 SCROLLINFO info;
175 if (!(descr->style & WS_VSCROLL)) return;
176 /* It is important that we check descr->style, and not wnd->dwStyle,
177 for WS_VSCROLL, as the former is exactly the one passed in
178 argument to CreateWindow.
179 In Windows (and from now on in Wine :) a listbox created
180 with such a style (no WS_SCROLL) does not update
181 the scrollbar with listbox-related data, thus letting
182 the programmer use it for his/her own purposes. */
184 if (descr->style & LBS_NOREDRAW) return;
185 info.cbSize = sizeof(info);
187 if (descr->style & LBS_MULTICOLUMN)
189 info.nMin = 0;
190 info.nMax = (descr->nb_items - 1) / descr->page_size;
191 info.nPos = descr->top_item / descr->page_size;
192 info.nPage = descr->width / descr->column_width;
193 if (info.nPage < 1) info.nPage = 1;
194 info.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
195 if (descr->style & LBS_DISABLENOSCROLL)
196 info.fMask |= SIF_DISABLENOSCROLL;
197 SetScrollInfo32( wnd->hwndSelf, SB_HORZ, &info, TRUE );
198 info.nMax = 0;
199 info.fMask = SIF_RANGE;
200 SetScrollInfo32( wnd->hwndSelf, SB_VERT, &info, TRUE );
202 else
204 info.nMin = 0;
205 info.nMax = descr->nb_items - 1;
206 info.nPos = descr->top_item;
207 info.nPage = LISTBOX_GetCurrentPageSize( wnd, descr );
208 info.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
209 if (descr->style & LBS_DISABLENOSCROLL)
210 info.fMask |= SIF_DISABLENOSCROLL;
211 SetScrollInfo32( wnd->hwndSelf, SB_VERT, &info, TRUE );
213 if (descr->horz_extent)
215 info.nMin = 0;
216 info.nMax = descr->horz_extent - 1;
217 info.nPos = descr->horz_pos;
218 info.nPage = descr->width;
219 info.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
220 if (descr->style & LBS_DISABLENOSCROLL)
221 info.fMask |= SIF_DISABLENOSCROLL;
222 SetScrollInfo32( wnd->hwndSelf, SB_HORZ, &info, TRUE );
228 /***********************************************************************
229 * LISTBOX_SetTopItem
231 * Set the top item of the listbox, scrolling up or down if necessary.
233 static LRESULT LISTBOX_SetTopItem( WND *wnd, LB_DESCR *descr, INT32 index,
234 BOOL32 scroll )
236 INT32 max = LISTBOX_GetMaxTopIndex( wnd, descr );
237 if (index > max) index = max;
238 if (index < 0) index = 0;
239 if (descr->style & LBS_MULTICOLUMN) index -= index % descr->page_size;
240 if (descr->top_item == index) return LB_OKAY;
241 if (descr->style & LBS_MULTICOLUMN)
243 INT32 diff = (descr->top_item - index) / descr->page_size * descr->column_width;
244 if (scroll && (abs(diff) < descr->width))
245 ScrollWindowEx32( wnd->hwndSelf, diff, 0, NULL, NULL, 0, NULL,
246 SW_INVALIDATE | SW_ERASE );
247 else
248 scroll = FALSE;
250 else if (scroll)
252 INT32 diff;
253 if (descr->style & LBS_OWNERDRAWVARIABLE)
255 INT32 i;
256 diff = 0;
257 if (index > descr->top_item)
259 for (i = index - 1; i >= descr->top_item; i--)
260 diff -= descr->items[i].height;
262 else
264 for (i = index; i < descr->top_item; i++)
265 diff += descr->items[i].height;
268 else
269 diff = (descr->top_item - index) * descr->item_height;
271 if (abs(diff) < descr->height)
272 ScrollWindowEx32( wnd->hwndSelf, 0, diff, NULL, NULL, 0, NULL,
273 SW_INVALIDATE | SW_ERASE );
274 else
275 scroll = FALSE;
277 if (!scroll) InvalidateRect32( wnd->hwndSelf, NULL, TRUE );
278 descr->top_item = index;
279 LISTBOX_UpdateScroll( wnd, descr );
280 return LB_OKAY;
284 /***********************************************************************
285 * LISTBOX_UpdatePage
287 * Update the page size. Should be called when the size of
288 * the client area or the item height changes.
290 static void LISTBOX_UpdatePage( WND *wnd, LB_DESCR *descr )
292 INT32 page_size;
294 if ((page_size = descr->height / descr->item_height) < 1) page_size = 1;
295 if (page_size == descr->page_size) return;
296 descr->page_size = page_size;
297 if (descr->style & LBS_MULTICOLUMN)
298 InvalidateRect32( wnd->hwndSelf, NULL, TRUE );
299 LISTBOX_SetTopItem( wnd, descr, descr->top_item, FALSE );
303 /***********************************************************************
304 * LISTBOX_UpdateSize
306 * Update the size of the listbox. Should be called when the size of
307 * the client area changes.
309 static void LISTBOX_UpdateSize( WND *wnd, LB_DESCR *descr )
311 RECT32 rect;
313 GetClientRect32( wnd->hwndSelf, &rect );
314 descr->width = rect.right - rect.left;
315 descr->height = rect.bottom - rect.top;
316 if (!(descr->style & LBS_NOINTEGRALHEIGHT) && !IS_OWNERDRAW(descr))
318 if ((descr->height > descr->item_height) &&
319 (descr->height % descr->item_height))
321 TRACE(listbox, "[%04x]: changing height %d -> %d\n",
322 wnd->hwndSelf, descr->height,
323 descr->height - descr->height%descr->item_height );
324 SetWindowPos32( wnd->hwndSelf, 0, 0, 0,
325 wnd->rectWindow.right - wnd->rectWindow.left,
326 wnd->rectWindow.bottom - wnd->rectWindow.top -
327 (descr->height % descr->item_height),
328 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE );
329 return;
332 TRACE(listbox, "[%04x]: new size = %d,%d\n",
333 wnd->hwndSelf, descr->width, descr->height );
334 LISTBOX_UpdatePage( wnd, descr );
335 LISTBOX_UpdateScroll( wnd, descr );
339 /***********************************************************************
340 * LISTBOX_GetItemRect
342 * Get the rectangle enclosing an item, in listbox client coordinates.
343 * Return 1 if the rectangle is (partially) visible, 0 if hidden, -1 on error.
345 static LRESULT LISTBOX_GetItemRect( WND *wnd, LB_DESCR *descr, INT32 index,
346 RECT32 *rect )
348 /* Index <= 0 is legal even on empty listboxes */
349 if (index && (index >= descr->nb_items)) return -1;
350 SetRect32( rect, 0, 0, descr->width, descr->height );
351 if (descr->style & LBS_MULTICOLUMN)
353 INT32 col = (index / descr->page_size) -
354 (descr->top_item / descr->page_size);
355 rect->left += col * descr->column_width;
356 rect->right = rect->left + descr->column_width;
357 rect->top += (index % descr->page_size) * descr->item_height;
358 rect->bottom = rect->top + descr->item_height;
360 else if (descr->style & LBS_OWNERDRAWVARIABLE)
362 INT32 i;
363 rect->right += descr->horz_pos;
364 if ((index >= 0) && (index < descr->nb_items))
366 if (index < descr->top_item)
368 for (i = descr->top_item-1; i >= index; i--)
369 rect->top -= descr->items[i].height;
371 else
373 for (i = descr->top_item; i < index; i++)
374 rect->top += descr->items[i].height;
376 rect->bottom = rect->top + descr->items[index].height;
380 else
382 rect->top += (index - descr->top_item) * descr->item_height;
383 rect->bottom = rect->top + descr->item_height;
384 rect->right += descr->horz_pos;
387 return ((rect->left < descr->width) && (rect->right > 0) &&
388 (rect->top < descr->height) && (rect->bottom > 0));
392 /***********************************************************************
393 * LISTBOX_GetItemFromPoint
395 * Return the item nearest from point (x,y) (in client coordinates).
397 static INT32 LISTBOX_GetItemFromPoint( WND *wnd, LB_DESCR *descr,
398 INT32 x, INT32 y )
400 INT32 index = descr->top_item;
402 if (!descr->nb_items) return -1; /* No items */
403 if (descr->style & LBS_OWNERDRAWVARIABLE)
405 INT32 pos = 0;
406 if (y >= 0)
408 while (index < descr->nb_items)
410 if ((pos += descr->items[index].height) > y) break;
411 index++;
414 else
416 while (index > 0)
418 index--;
419 if ((pos -= descr->items[index].height) <= y) break;
423 else if (descr->style & LBS_MULTICOLUMN)
425 if (y >= descr->item_height * descr->page_size) return -1;
426 if (y >= 0) index += y / descr->item_height;
427 if (x >= 0) index += (x / descr->column_width) * descr->page_size;
428 else index -= (((x + 1) / descr->column_width) - 1) * descr->page_size;
430 else
432 index += (y / descr->item_height);
434 if (index < 0) return 0;
435 if (index >= descr->nb_items) return -1;
436 return index;
440 /***********************************************************************
441 * LISTBOX_PaintItem
443 * Paint an item.
445 static void LISTBOX_PaintItem( WND *wnd, LB_DESCR *descr, HDC32 hdc,
446 const RECT32 *rect, INT32 index, UINT32 action )
448 LB_ITEMDATA *item = NULL;
449 if (index < descr->nb_items) item = &descr->items[index];
451 if (IS_OWNERDRAW(descr))
453 DRAWITEMSTRUCT32 dis;
454 UINT32 id = (descr->lphc) ? ID_CB_LISTBOX : wnd->wIDmenu;
456 dis.CtlType = ODT_LISTBOX;
457 dis.CtlID = id;
458 dis.hwndItem = wnd->hwndSelf;
459 dis.itemAction = action;
460 dis.hDC = hdc;
461 dis.itemID = index;
462 dis.itemState = 0;
463 if (item && item->selected) dis.itemState |= ODS_SELECTED;
464 if ((descr->focus_item == index) &&
465 (descr->caret_on) &&
466 (GetFocus32() == wnd->hwndSelf)) dis.itemState |= ODS_FOCUS;
467 if (wnd->dwStyle & WS_DISABLED) dis.itemState |= ODS_DISABLED;
468 dis.itemData = item ? item->data : 0;
469 dis.rcItem = *rect;
470 TRACE(listbox, "[%04x]: drawitem %d (%s) action=%02x "
471 "state=%02x rect=%d,%d-%d,%d\n",
472 wnd->hwndSelf, index, item ? item->str : "", action,
473 dis.itemState, rect->left, rect->top,
474 rect->right, rect->bottom );
475 SendMessage32A(descr->owner, WM_DRAWITEM, id, (LPARAM)&dis);
477 else
479 COLORREF oldText = 0, oldBk = 0;
481 if (action == ODA_FOCUS)
483 DrawFocusRect32( hdc, rect );
484 return;
486 if (item && item->selected)
488 oldBk = SetBkColor32( hdc, GetSysColor32( COLOR_HIGHLIGHT ) );
489 oldText = SetTextColor32( hdc, GetSysColor32(COLOR_HIGHLIGHTTEXT));
492 TRACE(listbox, "[%04x]: painting %d (%s) action=%02x "
493 "rect=%d,%d-%d,%d\n",
494 wnd->hwndSelf, index, item ? item->str : "", action,
495 rect->left, rect->top, rect->right, rect->bottom );
496 if (!item)
497 ExtTextOut32A( hdc, rect->left + 1, rect->top + 1,
498 ETO_OPAQUE | ETO_CLIPPED, rect, NULL, 0, NULL );
499 else if (!(descr->style & LBS_USETABSTOPS))
500 ExtTextOut32A( hdc, rect->left + 1, rect->top + 1,
501 ETO_OPAQUE | ETO_CLIPPED, rect, item->str,
502 strlen(item->str), NULL );
503 else
505 /* Output empty string to paint background in the full width. */
506 ExtTextOut32A( hdc, rect->left + 1, rect->top + 1,
507 ETO_OPAQUE | ETO_CLIPPED, rect, NULL, 0, NULL );
508 TabbedTextOut32A( hdc, rect->left + 1 , rect->top + 1,
509 item->str, strlen(item->str),
510 descr->nb_tabs, descr->tabs, 0);
512 if (item && item->selected)
514 SetBkColor32( hdc, oldBk );
515 SetTextColor32( hdc, oldText );
517 if ((descr->focus_item == index) &&
518 (descr->caret_on) &&
519 (GetFocus32() == wnd->hwndSelf)) DrawFocusRect32( hdc, rect );
524 /***********************************************************************
525 * LISTBOX_SetRedraw
527 * Change the redraw flag.
529 static void LISTBOX_SetRedraw( WND *wnd, LB_DESCR *descr, BOOL32 on )
531 if (on)
533 if (!(descr->style & LBS_NOREDRAW)) return;
534 descr->style &= ~LBS_NOREDRAW;
535 LISTBOX_UpdateScroll( wnd, descr );
537 else descr->style |= LBS_NOREDRAW;
541 /***********************************************************************
542 * LISTBOX_RepaintItem
544 * Repaint a single item synchronously.
546 static void LISTBOX_RepaintItem( WND *wnd, LB_DESCR *descr, INT32 index,
547 UINT32 action )
549 HDC32 hdc;
550 RECT32 rect;
551 HFONT32 oldFont = 0;
552 HBRUSH32 hbrush, oldBrush = 0;
554 if (descr->style & LBS_NOREDRAW) return;
555 if (LISTBOX_GetItemRect( wnd, descr, index, &rect ) != 1) return;
556 if (!(hdc = GetDCEx32( wnd->hwndSelf, 0, DCX_CACHE ))) return;
557 if (descr->font) oldFont = SelectObject32( hdc, descr->font );
558 hbrush = SendMessage32A( descr->owner, WM_CTLCOLORLISTBOX,
559 hdc, (LPARAM)wnd->hwndSelf );
560 if (hbrush) oldBrush = SelectObject32( hdc, hbrush );
561 if (wnd->dwStyle & WS_DISABLED)
562 SetTextColor32( hdc, GetSysColor32( COLOR_GRAYTEXT ) );
563 SetWindowOrgEx32( hdc, descr->horz_pos, 0, NULL );
564 LISTBOX_PaintItem( wnd, descr, hdc, &rect, index, action );
565 if (oldFont) SelectObject32( hdc, oldFont );
566 if (oldBrush) SelectObject32( hdc, oldBrush );
567 ReleaseDC32( wnd->hwndSelf, hdc );
571 /***********************************************************************
572 * LISTBOX_InitStorage
574 static LRESULT LISTBOX_InitStorage( WND *wnd, LB_DESCR *descr, INT32 nb_items,
575 DWORD bytes )
577 LB_ITEMDATA *item;
579 nb_items += LB_ARRAY_GRANULARITY - 1;
580 nb_items -= (nb_items % LB_ARRAY_GRANULARITY);
581 if (descr->items)
582 nb_items += HeapSize( descr->heap, 0, descr->items ) / sizeof(*item);
583 if (!(item = HeapReAlloc( descr->heap, 0, descr->items,
584 nb_items * sizeof(LB_ITEMDATA) )))
586 SEND_NOTIFICATION( wnd, descr, LBN_ERRSPACE );
587 return LB_ERRSPACE;
589 descr->items = item;
590 return LB_OKAY;
594 /***********************************************************************
595 * LISTBOX_SetTabStops
597 static BOOL32 LISTBOX_SetTabStops( WND *wnd, LB_DESCR *descr, INT32 count,
598 LPINT32 tabs, BOOL32 short_ints )
600 if (!(descr->style & LBS_USETABSTOPS)) return TRUE;
601 if (descr->tabs) HeapFree( descr->heap, 0, descr->tabs );
602 if (!(descr->nb_tabs = count))
604 descr->tabs = NULL;
605 return TRUE;
607 /* FIXME: count = 1 */
608 if (!(descr->tabs = (INT32 *)HeapAlloc( descr->heap, 0,
609 descr->nb_tabs * sizeof(INT32) )))
610 return FALSE;
611 if (short_ints)
613 INT32 i;
614 LPINT16 p = (LPINT16)tabs;
615 dbg_decl_str(listbox, 256);
617 for (i = 0; i < descr->nb_tabs; i++) {
618 descr->tabs[i] = *p++<<1; /* FIXME */
619 if(TRACE_ON(listbox))
620 dsprintf(listbox, "%hd ", descr->tabs[i]);
622 TRACE(listbox, "[%04x]: settabstops %s\n",
623 wnd->hwndSelf, dbg_str(listbox));
625 else memcpy( descr->tabs, tabs, descr->nb_tabs * sizeof(INT32) );
626 /* FIXME: repaint the window? */
627 return TRUE;
631 /***********************************************************************
632 * LISTBOX_GetText
634 static LRESULT LISTBOX_GetText( WND *wnd, LB_DESCR *descr, INT32 index,
635 LPSTR buffer )
637 if ((index < 0) || (index >= descr->nb_items)) return LB_ERR;
638 if (HAS_STRINGS(descr))
640 if (!buffer)
641 return strlen(descr->items[index].str);
642 lstrcpy32A( buffer, descr->items[index].str );
643 return strlen(buffer);
644 } else {
645 if (buffer)
646 *((LPDWORD)buffer)=*(LPDWORD)(&descr->items[index].data);
647 return sizeof(DWORD);
652 /***********************************************************************
653 * LISTBOX_FindStringPos
655 * Find the nearest string located before a given string in sort order.
656 * If 'exact' is TRUE, return an error if we don't get an exact match.
658 static INT32 LISTBOX_FindStringPos( WND *wnd, LB_DESCR *descr, LPCSTR str,
659 BOOL32 exact )
661 INT32 index, min, max, res = -1;
663 if (!(descr->style & LBS_SORT)) return -1; /* Add it at the end */
664 min = 0;
665 max = descr->nb_items;
666 while (min != max)
668 index = (min + max) / 2;
669 if (HAS_STRINGS(descr))
670 res = lstrcmpi32A( descr->items[index].str, str );
671 else
673 COMPAREITEMSTRUCT32 cis;
674 UINT32 id = (descr->lphc) ? ID_CB_LISTBOX : wnd->wIDmenu;
676 cis.CtlType = ODT_LISTBOX;
677 cis.CtlID = id;
678 cis.hwndItem = wnd->hwndSelf;
679 cis.itemID1 = index;
680 cis.itemData1 = descr->items[index].data;
681 cis.itemID2 = -1;
682 cis.itemData2 = (DWORD)str;
683 cis.dwLocaleId = descr->locale;
684 res = SendMessage32A( descr->owner, WM_COMPAREITEM,
685 id, (LPARAM)&cis );
687 if (!res) return index;
688 if (res > 0) max = index;
689 else min = index + 1;
691 return exact ? -1 : max;
695 /***********************************************************************
696 * LISTBOX_FindFileStrPos
698 * Find the nearest string located before a given string in directory
699 * sort order (i.e. first files, then directories, then drives).
701 static INT32 LISTBOX_FindFileStrPos( WND *wnd, LB_DESCR *descr, LPCSTR str )
703 INT32 min, max, res = -1;
705 if (!HAS_STRINGS(descr))
706 return LISTBOX_FindStringPos( wnd, descr, str, FALSE );
707 min = 0;
708 max = descr->nb_items;
709 while (min != max)
711 INT32 index = (min + max) / 2;
712 const char *p = descr->items[index].str;
713 if (*p == '[') /* drive or directory */
715 if (*str != '[') res = -1;
716 else if (p[1] == '-') /* drive */
718 if (str[1] == '-') res = str[2] - p[2];
719 else res = -1;
721 else /* directory */
723 if (str[1] == '-') res = 1;
724 else res = lstrcmpi32A( str, p );
727 else /* filename */
729 if (*str == '[') res = 1;
730 else res = lstrcmpi32A( str, p );
732 if (!res) return index;
733 if (res < 0) max = index;
734 else min = index + 1;
736 return max;
740 /***********************************************************************
741 * LISTBOX_FindString
743 * Find the item beginning with a given string.
745 static INT32 LISTBOX_FindString( WND *wnd, LB_DESCR *descr, INT32 start,
746 LPCSTR str, BOOL32 exact )
748 INT32 i;
749 LB_ITEMDATA *item;
751 if (start >= descr->nb_items) start = -1;
752 item = descr->items + start + 1;
753 if (HAS_STRINGS(descr))
755 if (!str) return LB_ERR;
756 if (exact)
758 for (i = start + 1; i < descr->nb_items; i++, item++)
759 if (!lstrcmpi32A( str, item->str )) return i;
760 for (i = 0, item = descr->items; i <= start; i++, item++)
761 if (!lstrcmpi32A( str, item->str )) return i;
763 else
765 /* Special case for drives and directories: ignore prefix */
766 #define CHECK_DRIVE(item) \
767 if ((item)->str[0] == '[') \
769 if (!lstrncmpi32A( str, (item)->str+1, len )) return i; \
770 if (((item)->str[1] == '-') && !lstrncmpi32A(str,(item)->str+2,len)) \
771 return i; \
774 INT32 len = strlen(str);
775 for (i = start + 1; i < descr->nb_items; i++, item++)
777 if (!lstrncmpi32A( str, item->str, len )) return i;
778 CHECK_DRIVE(item);
780 for (i = 0, item = descr->items; i <= start; i++, item++)
782 if (!lstrncmpi32A( str, item->str, len )) return i;
783 CHECK_DRIVE(item);
785 #undef CHECK_DRIVE
788 else
790 if (exact && (descr->style & LBS_SORT))
791 /* If sorted, use a WM_COMPAREITEM binary search */
792 return LISTBOX_FindStringPos( wnd, descr, str, TRUE );
794 /* Otherwise use a linear search */
795 for (i = start + 1; i < descr->nb_items; i++, item++)
796 if (item->data == (DWORD)str) return i;
797 for (i = 0, item = descr->items; i <= start; i++, item++)
798 if (item->data == (DWORD)str) return i;
800 return LB_ERR;
804 /***********************************************************************
805 * LISTBOX_GetSelCount
807 static LRESULT LISTBOX_GetSelCount( WND *wnd, LB_DESCR *descr )
809 INT32 i, count;
810 LB_ITEMDATA *item = descr->items;
812 if (!(descr->style & LBS_MULTIPLESEL)) return LB_ERR;
813 for (i = count = 0; i < descr->nb_items; i++, item++)
814 if (item->selected) count++;
815 return count;
819 /***********************************************************************
820 * LISTBOX_GetSelItems16
822 static LRESULT LISTBOX_GetSelItems16( WND *wnd, LB_DESCR *descr, INT16 max,
823 LPINT16 array )
825 INT32 i, count;
826 LB_ITEMDATA *item = descr->items;
828 if (!(descr->style & LBS_MULTIPLESEL)) return LB_ERR;
829 for (i = count = 0; (i < descr->nb_items) && (count < max); i++, item++)
830 if (item->selected) array[count++] = (INT16)i;
831 return count;
835 /***********************************************************************
836 * LISTBOX_GetSelItems32
838 static LRESULT LISTBOX_GetSelItems32( WND *wnd, LB_DESCR *descr, INT32 max,
839 LPINT32 array )
841 INT32 i, count;
842 LB_ITEMDATA *item = descr->items;
844 if (!(descr->style & LBS_MULTIPLESEL)) return LB_ERR;
845 for (i = count = 0; (i < descr->nb_items) && (count < max); i++, item++)
846 if (item->selected) array[count++] = i;
847 return count;
851 /***********************************************************************
852 * LISTBOX_Paint
854 static LRESULT LISTBOX_Paint( WND *wnd, LB_DESCR *descr, HDC32 hdc )
856 INT32 i, col_pos = descr->page_size - 1;
857 RECT32 rect;
858 HFONT32 oldFont = 0;
859 HBRUSH32 hbrush, oldBrush = 0;
861 SetRect32( &rect, 0, 0, descr->width, descr->height );
862 if (descr->style & LBS_NOREDRAW) return 0;
863 if (descr->style & LBS_MULTICOLUMN)
864 rect.right = rect.left + descr->column_width;
865 else if (descr->horz_pos)
867 SetWindowOrgEx32( hdc, descr->horz_pos, 0, NULL );
868 rect.right += descr->horz_pos;
871 if (descr->font) oldFont = SelectObject32( hdc, descr->font );
872 hbrush = SendMessage32A( descr->owner, WM_CTLCOLORLISTBOX,
873 hdc, (LPARAM)wnd->hwndSelf );
874 if (hbrush) oldBrush = SelectObject32( hdc, hbrush );
875 if (wnd->dwStyle & WS_DISABLED)
876 SetTextColor32( hdc, GetSysColor32( COLOR_GRAYTEXT ) );
878 if (!descr->nb_items && (descr->focus_item != -1) && descr->caret_on &&
879 (GetFocus32() == wnd->hwndSelf))
881 /* Special case for empty listbox: paint focus rect */
882 rect.bottom = rect.top + descr->item_height;
883 LISTBOX_PaintItem( wnd, descr, hdc, &rect, descr->focus_item,
884 ODA_FOCUS );
885 rect.top = rect.bottom;
888 for (i = descr->top_item; i < descr->nb_items; i++)
890 if (!(descr->style & LBS_OWNERDRAWVARIABLE))
891 rect.bottom = rect.top + descr->item_height;
892 else
893 rect.bottom = rect.top + descr->items[i].height;
895 LISTBOX_PaintItem( wnd, descr, hdc, &rect, i, ODA_DRAWENTIRE );
896 rect.top = rect.bottom;
898 if ((descr->style & LBS_MULTICOLUMN) && !col_pos)
900 if (!IS_OWNERDRAW(descr))
902 /* Clear the bottom of the column */
903 SetBkColor32( hdc, GetSysColor32( COLOR_WINDOW ) );
904 if (rect.top < descr->height)
906 rect.bottom = descr->height;
907 ExtTextOut32A( hdc, 0, 0, ETO_OPAQUE | ETO_CLIPPED,
908 &rect, NULL, 0, NULL );
912 /* Go to the next column */
913 rect.left += descr->column_width;
914 rect.right += descr->column_width;
915 rect.top = 0;
916 col_pos = descr->page_size - 1;
918 else
920 col_pos--;
921 if (rect.top >= descr->height) break;
925 if (!IS_OWNERDRAW(descr))
927 /* Clear the remainder of the client area */
928 SetBkColor32( hdc, GetSysColor32( COLOR_WINDOW ) );
929 if (rect.top < descr->height)
931 rect.bottom = descr->height;
932 ExtTextOut32A( hdc, 0, 0, ETO_OPAQUE | ETO_CLIPPED,
933 &rect, NULL, 0, NULL );
935 if (rect.right < descr->width)
937 rect.left = rect.right;
938 rect.right = descr->width;
939 rect.top = 0;
940 rect.bottom = descr->height;
941 ExtTextOut32A( hdc, 0, 0, ETO_OPAQUE | ETO_CLIPPED,
942 &rect, NULL, 0, NULL );
945 if (oldFont) SelectObject32( hdc, oldFont );
946 if (oldBrush) SelectObject32( hdc, oldBrush );
947 return 0;
951 /***********************************************************************
952 * LISTBOX_InvalidateItems
954 * Invalidate all items from a given item. If the specified item is not
955 * visible, nothing happens.
957 static void LISTBOX_InvalidateItems( WND *wnd, LB_DESCR *descr, INT32 index )
959 RECT32 rect;
961 if (LISTBOX_GetItemRect( wnd, descr, index, &rect ) == 1)
963 rect.bottom = descr->height;
964 InvalidateRect32( wnd->hwndSelf, &rect, TRUE );
965 if (descr->style & LBS_MULTICOLUMN)
967 /* Repaint the other columns */
968 rect.left = rect.right;
969 rect.right = descr->width;
970 rect.top = 0;
971 InvalidateRect32( wnd->hwndSelf, &rect, TRUE );
977 /***********************************************************************
978 * LISTBOX_GetItemHeight
980 static LRESULT LISTBOX_GetItemHeight( WND *wnd, LB_DESCR *descr, INT32 index )
982 if (descr->style & LBS_OWNERDRAWVARIABLE)
984 if ((index < 0) || (index >= descr->nb_items)) return LB_ERR;
985 return descr->items[index].height;
987 else return descr->item_height;
991 /***********************************************************************
992 * LISTBOX_SetItemHeight
994 static LRESULT LISTBOX_SetItemHeight( WND *wnd, LB_DESCR *descr, INT32 index,
995 UINT32 height )
997 if (!height) height = 1;
999 if (descr->style & LBS_OWNERDRAWVARIABLE)
1001 if ((index < 0) || (index >= descr->nb_items)) return LB_ERR;
1002 TRACE(listbox, "[%04x]: item %d height = %d\n",
1003 wnd->hwndSelf, index, height );
1004 descr->items[index].height = height;
1005 LISTBOX_UpdateScroll( wnd, descr );
1006 LISTBOX_InvalidateItems( wnd, descr, index );
1008 else if (height != descr->item_height)
1010 TRACE(listbox, "[%04x]: new height = %d\n",
1011 wnd->hwndSelf, height );
1012 descr->item_height = height;
1013 LISTBOX_UpdatePage( wnd, descr );
1014 LISTBOX_UpdateScroll( wnd, descr );
1015 InvalidateRect32( wnd->hwndSelf, 0, TRUE );
1017 return LB_OKAY;
1021 /***********************************************************************
1022 * LISTBOX_SetHorizontalPos
1024 static void LISTBOX_SetHorizontalPos( WND *wnd, LB_DESCR *descr, INT32 pos )
1026 INT32 diff;
1028 if (pos > descr->horz_extent - descr->width)
1029 pos = descr->horz_extent - descr->width;
1030 if (pos < 0) pos = 0;
1031 if (!(diff = descr->horz_pos - pos)) return;
1032 TRACE(listbox, "[%04x]: new horz pos = %d\n",
1033 wnd->hwndSelf, pos );
1034 descr->horz_pos = pos;
1035 LISTBOX_UpdateScroll( wnd, descr );
1036 if (abs(diff) < descr->width)
1037 ScrollWindowEx32( wnd->hwndSelf, diff, 0, NULL, NULL, 0, NULL,
1038 SW_INVALIDATE | SW_ERASE );
1039 else
1040 InvalidateRect32( wnd->hwndSelf, NULL, TRUE );
1044 /***********************************************************************
1045 * LISTBOX_SetHorizontalExtent
1047 static LRESULT LISTBOX_SetHorizontalExtent( WND *wnd, LB_DESCR *descr,
1048 UINT32 extent )
1050 if (!descr->horz_extent || (descr->style & LBS_MULTICOLUMN))
1051 return LB_OKAY;
1052 if (extent <= 0) extent = 1;
1053 if (extent == descr->horz_extent) return LB_OKAY;
1054 TRACE(listbox, "[%04x]: new horz extent = %d\n",
1055 wnd->hwndSelf, extent );
1056 descr->horz_extent = extent;
1057 if (descr->horz_pos > extent - descr->width)
1058 LISTBOX_SetHorizontalPos( wnd, descr, extent - descr->width );
1059 else
1060 LISTBOX_UpdateScroll( wnd, descr );
1061 return LB_OKAY;
1065 /***********************************************************************
1066 * LISTBOX_SetColumnWidth
1068 static LRESULT LISTBOX_SetColumnWidth( WND *wnd, LB_DESCR *descr, UINT32 width)
1070 width += 2; /* For left and right margin */
1071 if (width == descr->column_width) return LB_OKAY;
1072 TRACE(listbox, "[%04x]: new column width = %d\n",
1073 wnd->hwndSelf, width );
1074 descr->column_width = width;
1075 LISTBOX_UpdatePage( wnd, descr );
1076 return LB_OKAY;
1080 /***********************************************************************
1081 * LISTBOX_SetFont
1083 * Returns the item height.
1085 static INT32 LISTBOX_SetFont( WND *wnd, LB_DESCR *descr, HFONT32 font )
1087 HDC32 hdc;
1088 HFONT32 oldFont = 0;
1089 TEXTMETRIC32A tm;
1091 descr->font = font;
1093 if (!(hdc = GetDCEx32( wnd->hwndSelf, 0, DCX_CACHE )))
1095 ERR(listbox, "unable to get DC.\n" );
1096 return 16;
1098 if (font) oldFont = SelectObject32( hdc, font );
1099 GetTextMetrics32A( hdc, &tm );
1100 if (oldFont) SelectObject32( hdc, oldFont );
1101 ReleaseDC32( wnd->hwndSelf, hdc );
1102 if (!IS_OWNERDRAW(descr))
1103 LISTBOX_SetItemHeight( wnd, descr, 0, tm.tmHeight );
1104 return tm.tmHeight ;
1108 /***********************************************************************
1109 * LISTBOX_MakeItemVisible
1111 * Make sure that a given item is partially or fully visible.
1113 static void LISTBOX_MakeItemVisible( WND *wnd, LB_DESCR *descr, INT32 index,
1114 BOOL32 fully )
1116 INT32 top;
1118 if (index <= descr->top_item) top = index;
1119 else if (descr->style & LBS_MULTICOLUMN)
1121 INT32 cols = descr->width;
1122 if (!fully) cols += descr->column_width - 1;
1123 if (cols >= descr->column_width) cols /= descr->column_width;
1124 else cols = 1;
1125 if (index < descr->top_item + (descr->page_size * cols)) return;
1126 top = index - descr->page_size * (cols - 1);
1128 else if (descr->style & LBS_OWNERDRAWVARIABLE)
1130 INT32 height = fully ? descr->items[index].height : 1;
1131 for (top = index; top > descr->top_item; top--)
1132 if ((height += descr->items[top-1].height) > descr->height) break;
1134 else
1136 if (index < descr->top_item + descr->page_size) return;
1137 if (!fully && (index == descr->top_item + descr->page_size) &&
1138 (descr->height > (descr->page_size * descr->item_height))) return;
1139 top = index - descr->page_size + 1;
1141 LISTBOX_SetTopItem( wnd, descr, top, TRUE );
1145 /***********************************************************************
1146 * LISTBOX_SelectItemRange
1148 * Select a range of items. Should only be used on a MULTIPLESEL listbox.
1150 static LRESULT LISTBOX_SelectItemRange( WND *wnd, LB_DESCR *descr, INT32 first,
1151 INT32 last, BOOL32 on )
1153 INT32 i;
1155 /* A few sanity checks */
1157 if ((last == -1) && (descr->nb_items == 0)) return LB_OKAY;
1158 if (!(descr->style & LBS_MULTIPLESEL)) return LB_ERR;
1159 if (last == -1) last = descr->nb_items - 1;
1160 if ((first < 0) || (first >= descr->nb_items)) return LB_ERR;
1161 if ((last < 0) || (last >= descr->nb_items)) return LB_ERR;
1162 /* selected_item reflects last selected/unselected item on multiple sel */
1163 descr->selected_item = last;
1165 if (on) /* Turn selection on */
1167 for (i = first; i <= last; i++)
1169 if (descr->items[i].selected) continue;
1170 descr->items[i].selected = TRUE;
1171 LISTBOX_RepaintItem( wnd, descr, i, ODA_SELECT );
1174 else /* Turn selection off */
1176 for (i = first; i <= last; i++)
1178 if (!descr->items[i].selected) continue;
1179 descr->items[i].selected = FALSE;
1180 LISTBOX_RepaintItem( wnd, descr, i, ODA_SELECT );
1183 return LB_OKAY;
1187 /***********************************************************************
1188 * LISTBOX_SetCaretIndex
1190 static LRESULT LISTBOX_SetCaretIndex( WND *wnd, LB_DESCR *descr, INT32 index,
1191 BOOL32 fully_visible )
1193 INT32 oldfocus = descr->focus_item;
1195 if ((index < -1) || (index >= descr->nb_items)) return LB_ERR;
1196 if (index == oldfocus) return LB_OKAY;
1197 descr->focus_item = index;
1198 if ((oldfocus != -1) && descr->caret_on && (GetFocus32() == wnd->hwndSelf))
1199 LISTBOX_RepaintItem( wnd, descr, oldfocus, ODA_FOCUS );
1200 if (index != -1)
1202 LISTBOX_MakeItemVisible( wnd, descr, index, fully_visible );
1203 if (descr->caret_on && (GetFocus32() == wnd->hwndSelf))
1204 LISTBOX_RepaintItem( wnd, descr, index, ODA_FOCUS );
1206 return LB_OKAY;
1210 /***********************************************************************
1211 * LISTBOX_SetSelection
1213 static LRESULT LISTBOX_SetSelection( WND *wnd, LB_DESCR *descr, INT32 index,
1214 BOOL32 on, BOOL32 send_notify )
1216 if ((index < -1) || (index >= descr->nb_items)) return LB_ERR;
1217 if (descr->style & LBS_MULTIPLESEL)
1219 if (index == -1) /* Select all items */
1220 return LISTBOX_SelectItemRange( wnd, descr, 0, -1, on );
1221 else /* Only one item */
1222 return LISTBOX_SelectItemRange( wnd, descr, index, index, on );
1224 else
1226 INT32 oldsel = descr->selected_item;
1227 if (index == oldsel) return LB_OKAY;
1228 if (oldsel != -1) descr->items[oldsel].selected = FALSE;
1229 if (index != -1) descr->items[index].selected = TRUE;
1230 descr->selected_item = index;
1231 if (oldsel != -1) LISTBOX_RepaintItem( wnd, descr, oldsel, ODA_SELECT);
1232 if (index != -1) LISTBOX_RepaintItem( wnd, descr, index, ODA_SELECT );
1233 if (send_notify) SEND_NOTIFICATION( wnd, descr,
1234 (index != -1) ? LBN_SELCHANGE : LBN_SELCANCEL );
1235 else
1236 if( descr->lphc ) /* set selection change flag for parent combo */
1237 descr->lphc->wState |= CBF_SELCHANGE;
1239 return LB_OKAY;
1243 /***********************************************************************
1244 * LISTBOX_MoveCaret
1246 * Change the caret position and extend the selection to the new caret.
1248 static void LISTBOX_MoveCaret( WND *wnd, LB_DESCR *descr, INT32 index,
1249 BOOL32 fully_visible )
1251 LISTBOX_SetCaretIndex( wnd, descr, index, fully_visible );
1252 if (descr->style & LBS_EXTENDEDSEL)
1254 if (descr->anchor_item != -1)
1256 INT32 first = MIN( descr->focus_item, descr->anchor_item );
1257 INT32 last = MAX( descr->focus_item, descr->anchor_item );
1258 if (first > 0)
1259 LISTBOX_SelectItemRange( wnd, descr, 0, first - 1, FALSE );
1260 LISTBOX_SelectItemRange( wnd, descr, last + 1, -1, FALSE );
1261 LISTBOX_SelectItemRange( wnd, descr, first, last, TRUE );
1264 else if (!(descr->style & LBS_MULTIPLESEL) && (descr->selected_item != -1))
1266 /* Set selection to new caret item */
1267 LISTBOX_SetSelection( wnd, descr, index, TRUE, FALSE );
1272 /***********************************************************************
1273 * LISTBOX_InsertItem
1275 static LRESULT LISTBOX_InsertItem( WND *wnd, LB_DESCR *descr, INT32 index,
1276 LPSTR str, DWORD data )
1278 LB_ITEMDATA *item;
1279 INT32 max_items;
1281 if (index == -1) index = descr->nb_items;
1282 else if ((index < 0) || (index > descr->nb_items)) return LB_ERR;
1283 if (!descr->items) max_items = 0;
1284 else max_items = HeapSize( descr->heap, 0, descr->items ) / sizeof(*item);
1285 if (descr->nb_items == max_items)
1287 /* We need to grow the array */
1288 max_items += LB_ARRAY_GRANULARITY;
1289 if (!(item = HeapReAlloc( descr->heap, 0, descr->items,
1290 max_items * sizeof(LB_ITEMDATA) )))
1292 SEND_NOTIFICATION( wnd, descr, LBN_ERRSPACE );
1293 return LB_ERRSPACE;
1295 descr->items = item;
1298 /* Insert the item structure */
1300 item = &descr->items[index];
1301 if (index < descr->nb_items)
1302 RtlMoveMemory( item + 1, item,
1303 (descr->nb_items - index) * sizeof(LB_ITEMDATA) );
1304 item->str = str;
1305 item->data = data;
1306 item->height = 0;
1307 item->selected = FALSE;
1308 descr->nb_items++;
1310 /* Get item height */
1312 if (descr->style & LBS_OWNERDRAWVARIABLE)
1314 MEASUREITEMSTRUCT32 mis;
1315 UINT32 id = (descr->lphc) ? ID_CB_LISTBOX : wnd->wIDmenu;
1317 mis.CtlType = ODT_LISTBOX;
1318 mis.CtlID = id;
1319 mis.itemID = index;
1320 mis.itemData = descr->items[index].data;
1321 mis.itemHeight = descr->item_height;
1322 SendMessage32A( descr->owner, WM_MEASUREITEM, id, (LPARAM)&mis );
1323 item->height = mis.itemHeight ? mis.itemHeight : 1;
1324 TRACE(listbox, "[%04x]: measure item %d (%s) = %d\n",
1325 wnd->hwndSelf, index, str ? str : "", item->height );
1328 /* Repaint the items */
1330 LISTBOX_UpdateScroll( wnd, descr );
1331 LISTBOX_InvalidateItems( wnd, descr, index );
1333 /* Move selection and focused item */
1335 if (index <= descr->selected_item) descr->selected_item++;
1336 if (index <= descr->focus_item)
1338 descr->focus_item++;
1339 LISTBOX_MoveCaret( wnd, descr, descr->focus_item, FALSE );
1342 /* If listbox was empty, set focus to the first item */
1344 if (descr->nb_items == 1) LISTBOX_SetCaretIndex( wnd, descr, 0, FALSE );
1345 return LB_OKAY;
1349 /***********************************************************************
1350 * LISTBOX_InsertString
1352 static LRESULT LISTBOX_InsertString( WND *wnd, LB_DESCR *descr, INT32 index,
1353 LPCSTR str )
1355 LPSTR new_str = NULL;
1356 DWORD data = 0;
1357 LRESULT ret;
1359 if (HAS_STRINGS(descr))
1361 if (!(new_str = HEAP_strdupA( descr->heap, 0, str )))
1363 SEND_NOTIFICATION( wnd, descr, LBN_ERRSPACE );
1364 return LB_ERRSPACE;
1367 else data = (DWORD)str;
1369 if (index == -1) index = descr->nb_items;
1370 if ((ret = LISTBOX_InsertItem( wnd, descr, index, new_str, data )) != 0)
1372 if (new_str) HeapFree( descr->heap, 0, new_str );
1373 return ret;
1376 TRACE(listbox, "[%04x]: added item %d '%s'\n",
1377 wnd->hwndSelf, index, HAS_STRINGS(descr) ? new_str : "" );
1378 return index;
1382 /***********************************************************************
1383 * LISTBOX_DeleteItem
1385 * Delete the content of an item. 'index' must be a valid index.
1387 static void LISTBOX_DeleteItem( WND *wnd, LB_DESCR *descr, INT32 index )
1389 /* Note: Win 3.1 only sends DELETEITEM on owner-draw items,
1390 * while Win95 sends it for all items with user data.
1391 * It's probably better to send it too often than not
1392 * often enough, so this is what we do here.
1394 if (IS_OWNERDRAW(descr) || descr->items[index].data)
1396 DELETEITEMSTRUCT32 dis;
1397 UINT32 id = (descr->lphc) ? ID_CB_LISTBOX : wnd->wIDmenu;
1399 dis.CtlType = ODT_LISTBOX;
1400 dis.CtlID = id;
1401 dis.itemID = index;
1402 dis.hwndItem = wnd->hwndSelf;
1403 dis.itemData = descr->items[index].data;
1404 SendMessage32A( descr->owner, WM_DELETEITEM, id, (LPARAM)&dis );
1406 if (HAS_STRINGS(descr) && descr->items[index].str)
1407 HeapFree( descr->heap, 0, descr->items[index].str );
1411 /***********************************************************************
1412 * LISTBOX_RemoveItem
1414 * Remove an item from the listbox and delete its content.
1416 static LRESULT LISTBOX_RemoveItem( WND *wnd, LB_DESCR *descr, INT32 index )
1418 LB_ITEMDATA *item;
1419 INT32 max_items;
1421 if (index == -1) index = descr->nb_items - 1;
1422 else if ((index < 0) || (index >= descr->nb_items)) return LB_ERR;
1423 LISTBOX_DeleteItem( wnd, descr, index );
1425 /* Remove the item */
1427 item = &descr->items[index];
1428 if (index < descr->nb_items-1)
1429 RtlMoveMemory( item, item + 1,
1430 (descr->nb_items - index - 1) * sizeof(LB_ITEMDATA) );
1431 descr->nb_items--;
1432 if (descr->anchor_item == descr->nb_items) descr->anchor_item--;
1434 /* Shrink the item array if possible */
1436 max_items = HeapSize( descr->heap, 0, descr->items ) / sizeof(LB_ITEMDATA);
1437 if (descr->nb_items < max_items - 2*LB_ARRAY_GRANULARITY)
1439 max_items -= LB_ARRAY_GRANULARITY;
1440 item = HeapReAlloc( descr->heap, 0, descr->items,
1441 max_items * sizeof(LB_ITEMDATA) );
1442 if (item) descr->items = item;
1445 /* Repaint the items */
1447 LISTBOX_UpdateScroll( wnd, descr );
1448 LISTBOX_InvalidateItems( wnd, descr, index );
1450 /* Move selection and focused item */
1452 if (index <= descr->selected_item) descr->selected_item--;
1453 if (index <= descr->focus_item)
1455 descr->focus_item--;
1456 LISTBOX_MoveCaret( wnd, descr, descr->focus_item, FALSE );
1458 return LB_OKAY;
1462 /***********************************************************************
1463 * LISTBOX_ResetContent
1465 static void LISTBOX_ResetContent( WND *wnd, LB_DESCR *descr )
1467 INT32 i;
1469 for (i = 0; i < descr->nb_items; i++) LISTBOX_DeleteItem( wnd, descr, i );
1470 if (descr->items) HeapFree( descr->heap, 0, descr->items );
1471 descr->nb_items = 0;
1472 descr->top_item = 0;
1473 descr->selected_item = -1;
1474 descr->focus_item = 0;
1475 descr->anchor_item = -1;
1476 descr->items = NULL;
1477 LISTBOX_UpdateScroll( wnd, descr );
1478 InvalidateRect32( wnd->hwndSelf, NULL, TRUE );
1482 /***********************************************************************
1483 * LISTBOX_SetCount
1485 static LRESULT LISTBOX_SetCount( WND *wnd, LB_DESCR *descr, INT32 count )
1487 LRESULT ret;
1489 if (HAS_STRINGS(descr)) return LB_ERR;
1490 /* FIXME: this is far from optimal... */
1491 if (count > descr->nb_items)
1493 while (count > descr->nb_items)
1494 if ((ret = LISTBOX_InsertString( wnd, descr, -1, 0 )) < 0)
1495 return ret;
1497 else if (count < descr->nb_items)
1499 while (count < descr->nb_items)
1500 if ((ret = LISTBOX_RemoveItem( wnd, descr, -1 )) < 0)
1501 return ret;
1503 return LB_OKAY;
1507 /***********************************************************************
1508 * LISTBOX_Directory
1510 static LRESULT LISTBOX_Directory( WND *wnd, LB_DESCR *descr, UINT32 attrib,
1511 LPCSTR filespec, BOOL32 long_names )
1513 HANDLE32 handle;
1514 LRESULT ret = LB_OKAY;
1515 WIN32_FIND_DATA32A entry;
1516 int pos;
1518 if ((handle = FindFirstFile32A(filespec,&entry)) == INVALID_HANDLE_VALUE32)
1520 if (GetLastError() != ERROR_NO_MORE_FILES) return LB_ERR;
1522 else
1526 char buffer[270];
1527 if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1529 if (!(attrib & DDL_DIRECTORY) ||
1530 !strcmp( entry.cAlternateFileName, "." )) continue;
1531 if (long_names) sprintf( buffer, "[%s]", entry.cFileName );
1532 else sprintf( buffer, "[%s]", entry.cAlternateFileName );
1534 else /* not a directory */
1536 #define ATTRIBS (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | \
1537 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE)
1539 if ((attrib & DDL_EXCLUSIVE) &&
1540 ((attrib & ATTRIBS) != (entry.dwFileAttributes & ATTRIBS)))
1541 continue;
1542 #undef ATTRIBS
1543 if (long_names) strcpy( buffer, entry.cFileName );
1544 else strcpy( buffer, entry.cAlternateFileName );
1546 if (!long_names) CharLower32A( buffer );
1547 pos = LISTBOX_FindFileStrPos( wnd, descr, buffer );
1548 if ((ret = LISTBOX_InsertString( wnd, descr, pos, buffer )) < 0)
1549 break;
1550 } while (FindNextFile32A( handle, &entry ));
1551 FindClose32( handle );
1554 if ((ret >= 0) && (attrib & DDL_DRIVES))
1556 char buffer[] = "[-a-]";
1557 int drive;
1558 for (drive = 0; drive < MAX_DOS_DRIVES; drive++, buffer[2]++)
1560 if (!DRIVE_IsValid(drive)) continue;
1561 if ((ret = LISTBOX_InsertString( wnd, descr, -1, buffer )) < 0)
1562 break;
1565 return ret;
1569 /***********************************************************************
1570 * LISTBOX_HandleVScroll
1572 static LRESULT LISTBOX_HandleVScroll( WND *wnd, LB_DESCR *descr,
1573 WPARAM32 wParam, LPARAM lParam )
1575 SCROLLINFO info;
1577 if (descr->style & LBS_MULTICOLUMN) return 0;
1578 switch(LOWORD(wParam))
1580 case SB_LINEUP:
1581 LISTBOX_SetTopItem( wnd, descr, descr->top_item - 1, TRUE );
1582 break;
1583 case SB_LINEDOWN:
1584 LISTBOX_SetTopItem( wnd, descr, descr->top_item + 1, TRUE );
1585 break;
1586 case SB_PAGEUP:
1587 LISTBOX_SetTopItem( wnd, descr, descr->top_item -
1588 LISTBOX_GetCurrentPageSize( wnd, descr ), TRUE );
1589 break;
1590 case SB_PAGEDOWN:
1591 LISTBOX_SetTopItem( wnd, descr, descr->top_item +
1592 LISTBOX_GetCurrentPageSize( wnd, descr ), TRUE );
1593 break;
1594 case SB_THUMBPOSITION:
1595 LISTBOX_SetTopItem( wnd, descr, HIWORD(wParam), TRUE );
1596 break;
1597 case SB_THUMBTRACK:
1598 info.cbSize = sizeof(info);
1599 info.fMask = SIF_TRACKPOS;
1600 GetScrollInfo32( wnd->hwndSelf, SB_VERT, &info );
1601 LISTBOX_SetTopItem( wnd, descr, info.nTrackPos, TRUE );
1602 break;
1603 case SB_TOP:
1604 LISTBOX_SetTopItem( wnd, descr, 0, TRUE );
1605 break;
1606 case SB_BOTTOM:
1607 LISTBOX_SetTopItem( wnd, descr, descr->nb_items, TRUE );
1608 break;
1610 return 0;
1614 /***********************************************************************
1615 * LISTBOX_HandleHScroll
1617 static LRESULT LISTBOX_HandleHScroll( WND *wnd, LB_DESCR *descr,
1618 WPARAM32 wParam, LPARAM lParam )
1620 SCROLLINFO info;
1621 INT32 page;
1623 if (descr->style & LBS_MULTICOLUMN)
1625 switch(LOWORD(wParam))
1627 case SB_LINELEFT:
1628 LISTBOX_SetTopItem( wnd, descr, descr->top_item-descr->page_size,
1629 TRUE );
1630 break;
1631 case SB_LINERIGHT:
1632 LISTBOX_SetTopItem( wnd, descr, descr->top_item+descr->page_size,
1633 TRUE );
1634 break;
1635 case SB_PAGELEFT:
1636 page = descr->width / descr->column_width;
1637 if (page < 1) page = 1;
1638 LISTBOX_SetTopItem( wnd, descr,
1639 descr->top_item - page * descr->page_size, TRUE );
1640 break;
1641 case SB_PAGERIGHT:
1642 page = descr->width / descr->column_width;
1643 if (page < 1) page = 1;
1644 LISTBOX_SetTopItem( wnd, descr,
1645 descr->top_item + page * descr->page_size, TRUE );
1646 break;
1647 case SB_THUMBPOSITION:
1648 LISTBOX_SetTopItem( wnd, descr, HIWORD(wParam)*descr->page_size,
1649 TRUE );
1650 break;
1651 case SB_THUMBTRACK:
1652 info.cbSize = sizeof(info);
1653 info.fMask = SIF_TRACKPOS;
1654 GetScrollInfo32( wnd->hwndSelf, SB_VERT, &info );
1655 LISTBOX_SetTopItem( wnd, descr, info.nTrackPos*descr->page_size,
1656 TRUE );
1657 break;
1658 case SB_LEFT:
1659 LISTBOX_SetTopItem( wnd, descr, 0, TRUE );
1660 break;
1661 case SB_RIGHT:
1662 LISTBOX_SetTopItem( wnd, descr, descr->nb_items, TRUE );
1663 break;
1666 else if (descr->horz_extent)
1668 switch(LOWORD(wParam))
1670 case SB_LINELEFT:
1671 LISTBOX_SetHorizontalPos( wnd, descr, descr->horz_pos - 1 );
1672 break;
1673 case SB_LINERIGHT:
1674 LISTBOX_SetHorizontalPos( wnd, descr, descr->horz_pos + 1 );
1675 break;
1676 case SB_PAGELEFT:
1677 LISTBOX_SetHorizontalPos( wnd, descr,
1678 descr->horz_pos - descr->width );
1679 break;
1680 case SB_PAGERIGHT:
1681 LISTBOX_SetHorizontalPos( wnd, descr,
1682 descr->horz_pos + descr->width );
1683 break;
1684 case SB_THUMBPOSITION:
1685 LISTBOX_SetHorizontalPos( wnd, descr, HIWORD(wParam) );
1686 break;
1687 case SB_THUMBTRACK:
1688 info.cbSize = sizeof(info);
1689 info.fMask = SIF_TRACKPOS;
1690 GetScrollInfo32( wnd->hwndSelf, SB_HORZ, &info );
1691 LISTBOX_SetHorizontalPos( wnd, descr, info.nTrackPos );
1692 break;
1693 case SB_LEFT:
1694 LISTBOX_SetHorizontalPos( wnd, descr, 0 );
1695 break;
1696 case SB_RIGHT:
1697 LISTBOX_SetHorizontalPos( wnd, descr,
1698 descr->horz_extent - descr->width );
1699 break;
1702 return 0;
1706 /***********************************************************************
1707 * LISTBOX_HandleLButtonDown
1709 static LRESULT LISTBOX_HandleLButtonDown( WND *wnd, LB_DESCR *descr,
1710 WPARAM32 wParam, INT32 x, INT32 y )
1712 INT32 index = LISTBOX_GetItemFromPoint( wnd, descr, x, y );
1713 TRACE(listbox, "[%04x]: lbuttondown %d,%d item %d\n",
1714 wnd->hwndSelf, x, y, index );
1715 if (!descr->caret_on && (GetFocus32() == wnd->hwndSelf)) return 0;
1716 if (index != -1)
1718 if (descr->style & LBS_EXTENDEDSEL)
1720 if (!(wParam & MK_SHIFT)) descr->anchor_item = index;
1721 if (wParam & MK_CONTROL)
1723 LISTBOX_SetCaretIndex( wnd, descr, index, FALSE );
1724 LISTBOX_SetSelection( wnd, descr, index,
1725 !descr->items[index].selected, FALSE );
1727 else LISTBOX_MoveCaret( wnd, descr, index, FALSE );
1729 else
1731 LISTBOX_MoveCaret( wnd, descr, index, FALSE );
1732 LISTBOX_SetSelection( wnd, descr, index,
1733 (!(descr->style & LBS_MULTIPLESEL) ||
1734 !descr->items[index].selected), FALSE );
1738 if( !descr->lphc ) SetFocus32( wnd->hwndSelf );
1739 else SetFocus32( (descr->lphc->hWndEdit) ? descr->lphc->hWndEdit
1740 : descr->lphc->self->hwndSelf ) ;
1742 SetCapture32( wnd->hwndSelf );
1743 if (index != -1 && !descr->lphc)
1745 if (descr->style & LBS_NOTIFY )
1746 SendMessage32A( descr->owner, WM_LBTRACKPOINT, index,
1747 MAKELPARAM( x, y ) );
1748 if (wnd->dwExStyle & WS_EX_DRAGDETECT)
1750 POINT32 pt = { x, y };
1751 if (DragDetect32( wnd->hwndSelf, pt ))
1752 SendMessage32A( descr->owner, WM_BEGINDRAG, 0, 0 );
1755 return 0;
1759 /***********************************************************************
1760 * LISTBOX_HandleLButtonUp
1762 static LRESULT LISTBOX_HandleLButtonUp( WND *wnd, LB_DESCR *descr )
1764 if (LISTBOX_Timer != LB_TIMER_NONE)
1765 KillSystemTimer32( wnd->hwndSelf, LB_TIMER_ID );
1766 LISTBOX_Timer = LB_TIMER_NONE;
1767 if (GetCapture32() == wnd->hwndSelf)
1769 ReleaseCapture();
1770 if (descr->style & LBS_NOTIFY)
1771 SEND_NOTIFICATION( wnd, descr, LBN_SELCHANGE );
1773 return 0;
1777 /***********************************************************************
1778 * LISTBOX_HandleTimer
1780 * Handle scrolling upon a timer event.
1781 * Return TRUE if scrolling should continue.
1783 static LRESULT LISTBOX_HandleTimer( WND *wnd, LB_DESCR *descr,
1784 INT32 index, TIMER_DIRECTION dir )
1786 switch(dir)
1788 case LB_TIMER_UP:
1789 if (descr->top_item) index = descr->top_item - 1;
1790 else index = 0;
1791 break;
1792 case LB_TIMER_LEFT:
1793 if (descr->top_item) index -= descr->page_size;
1794 break;
1795 case LB_TIMER_DOWN:
1796 index = descr->top_item + LISTBOX_GetCurrentPageSize( wnd, descr );
1797 if (index == descr->focus_item) index++;
1798 if (index >= descr->nb_items) index = descr->nb_items - 1;
1799 break;
1800 case LB_TIMER_RIGHT:
1801 if (index + descr->page_size < descr->nb_items)
1802 index += descr->page_size;
1803 break;
1804 case LB_TIMER_NONE:
1805 break;
1807 if (index == descr->focus_item) return FALSE;
1808 LISTBOX_MoveCaret( wnd, descr, index, FALSE );
1809 return TRUE;
1813 /***********************************************************************
1814 * LISTBOX_HandleSystemTimer
1816 * WM_SYSTIMER handler.
1818 static LRESULT LISTBOX_HandleSystemTimer( WND *wnd, LB_DESCR *descr )
1820 if (!LISTBOX_HandleTimer( wnd, descr, descr->focus_item, LISTBOX_Timer ))
1822 KillSystemTimer32( wnd->hwndSelf, LB_TIMER_ID );
1823 LISTBOX_Timer = LB_TIMER_NONE;
1825 return 0;
1829 /***********************************************************************
1830 * LISTBOX_HandleMouseMove
1832 * WM_MOUSEMOVE handler.
1834 static void LISTBOX_HandleMouseMove( WND *wnd, LB_DESCR *descr,
1835 INT32 x, INT32 y )
1837 INT32 index;
1838 TIMER_DIRECTION dir;
1840 if (descr->style & LBS_MULTICOLUMN)
1842 if (y < 0) y = 0;
1843 else if (y >= descr->item_height * descr->page_size)
1844 y = descr->item_height * descr->page_size - 1;
1846 if (x < 0)
1848 dir = LB_TIMER_LEFT;
1849 x = 0;
1851 else if (x >= descr->width)
1853 dir = LB_TIMER_RIGHT;
1854 x = descr->width - 1;
1856 else dir = LB_TIMER_NONE; /* inside */
1858 else
1860 if (y < 0) dir = LB_TIMER_UP; /* above */
1861 else if (y >= descr->height) dir = LB_TIMER_DOWN; /* below */
1862 else dir = LB_TIMER_NONE; /* inside */
1865 index = LISTBOX_GetItemFromPoint( wnd, descr, x, y );
1866 if (index == -1) index = descr->focus_item;
1867 if (!LISTBOX_HandleTimer( wnd, descr, index, dir )) dir = LB_TIMER_NONE;
1869 /* Start/stop the system timer */
1871 if (dir != LB_TIMER_NONE)
1872 SetSystemTimer32( wnd->hwndSelf, LB_TIMER_ID, LB_SCROLL_TIMEOUT, NULL);
1873 else if (LISTBOX_Timer != LB_TIMER_NONE)
1874 KillSystemTimer32( wnd->hwndSelf, LB_TIMER_ID );
1875 LISTBOX_Timer = dir;
1879 /***********************************************************************
1880 * LISTBOX_HandleKeyDown
1882 static LRESULT LISTBOX_HandleKeyDown( WND *wnd, LB_DESCR *descr, WPARAM32 wParam )
1884 INT32 caret = -1;
1885 if (descr->style & LBS_WANTKEYBOARDINPUT)
1887 caret = SendMessage32A( descr->owner, WM_VKEYTOITEM,
1888 MAKEWPARAM(LOWORD(wParam), descr->focus_item),
1889 wnd->hwndSelf );
1890 if (caret == -2) return 0;
1892 if (caret == -1) switch(wParam)
1894 case VK_LEFT:
1895 if (descr->style & LBS_MULTICOLUMN)
1897 if (descr->focus_item >= descr->page_size)
1898 caret = descr->focus_item - descr->page_size;
1899 break;
1901 /* fall through */
1902 case VK_UP:
1903 caret = descr->focus_item - 1;
1904 if (caret < 0) caret = 0;
1905 break;
1906 case VK_RIGHT:
1907 if (descr->style & LBS_MULTICOLUMN)
1909 if (descr->focus_item + descr->page_size < descr->nb_items)
1910 caret = descr->focus_item + descr->page_size;
1911 break;
1913 /* fall through */
1914 case VK_DOWN:
1915 caret = descr->focus_item + 1;
1916 if (caret >= descr->nb_items) caret = descr->nb_items - 1;
1917 break;
1918 case VK_PRIOR:
1919 if (descr->style & LBS_MULTICOLUMN)
1921 INT32 page = descr->width / descr->column_width;
1922 if (page < 1) page = 1;
1923 caret = descr->focus_item - (page * descr->page_size) + 1;
1925 else caret = descr->focus_item-LISTBOX_GetCurrentPageSize(wnd,descr)+1;
1926 if (caret < 0) caret = 0;
1927 break;
1928 case VK_NEXT:
1929 if (descr->style & LBS_MULTICOLUMN)
1931 INT32 page = descr->width / descr->column_width;
1932 if (page < 1) page = 1;
1933 caret = descr->focus_item + (page * descr->page_size) - 1;
1935 else caret = descr->focus_item+LISTBOX_GetCurrentPageSize(wnd,descr)-1;
1936 if (caret >= descr->nb_items) caret = descr->nb_items - 1;
1937 break;
1938 case VK_HOME:
1939 caret = 0;
1940 break;
1941 case VK_END:
1942 caret = descr->nb_items - 1;
1943 break;
1944 case VK_SPACE:
1945 if (descr->style & LBS_EXTENDEDSEL) caret = descr->focus_item;
1946 else if (descr->style & LBS_MULTIPLESEL)
1948 LISTBOX_SetSelection( wnd, descr, descr->focus_item,
1949 !descr->items[descr->focus_item].selected,
1950 (descr->style & LBS_NOTIFY) != 0 );
1952 else if (descr->selected_item == -1)
1954 LISTBOX_SetSelection( wnd, descr, descr->focus_item, TRUE,
1955 (descr->style & LBS_NOTIFY) != 0 );
1957 break;
1959 if (caret >= 0)
1961 if ((descr->style & LBS_EXTENDEDSEL) &&
1962 !(GetKeyState32( VK_SHIFT ) & 0x8000))
1963 descr->anchor_item = caret;
1964 LISTBOX_MoveCaret( wnd, descr, caret, TRUE );
1965 if (descr->style & LBS_NOTIFY)
1967 if( descr->lphc && CB_GETTYPE(descr->lphc) != CBS_SIMPLE )
1969 /* make sure that combo parent doesn't hide us */
1970 descr->lphc->wState |= CBF_NOROLLUP;
1972 SEND_NOTIFICATION( wnd, descr, LBN_SELCHANGE );
1975 return 0;
1979 /***********************************************************************
1980 * LISTBOX_HandleChar
1982 static LRESULT LISTBOX_HandleChar( WND *wnd, LB_DESCR *descr,
1983 WPARAM32 wParam )
1985 INT32 caret = -1;
1986 char str[2] = { wParam & 0xff, '\0' };
1988 if (descr->style & LBS_WANTKEYBOARDINPUT)
1990 caret = SendMessage32A( descr->owner, WM_CHARTOITEM,
1991 MAKEWPARAM(LOWORD(wParam), descr->focus_item),
1992 wnd->hwndSelf );
1993 if (caret == -2) return 0;
1995 if (caret == -1)
1996 caret = LISTBOX_FindString( wnd, descr, descr->focus_item, str, FALSE);
1997 if (caret != -1)
1999 LISTBOX_MoveCaret( wnd, descr, caret, TRUE );
2000 if (descr->style & LBS_NOTIFY)
2001 SEND_NOTIFICATION( wnd, descr, LBN_SELCHANGE );
2003 return 0;
2007 /***********************************************************************
2008 * LISTBOX_Create
2010 static BOOL32 LISTBOX_Create( WND *wnd, LPHEADCOMBO lphc )
2012 LB_DESCR *descr;
2013 MEASUREITEMSTRUCT32 mis;
2014 RECT32 rect;
2016 if (!(descr = HeapAlloc( GetProcessHeap(), 0, sizeof(*descr) )))
2017 return FALSE;
2018 if (!(descr->heap = HeapCreate( 0, 0x10000, 0 )))
2020 HeapFree( GetProcessHeap(), 0, descr );
2021 return FALSE;
2023 GetClientRect32( wnd->hwndSelf, &rect );
2024 descr->owner = GetParent32( wnd->hwndSelf );
2025 descr->style = wnd->dwStyle;
2026 descr->width = rect.right - rect.left;
2027 descr->height = rect.bottom - rect.top;
2028 descr->items = NULL;
2029 descr->nb_items = 0;
2030 descr->top_item = 0;
2031 descr->selected_item = -1;
2032 descr->focus_item = 0;
2033 descr->anchor_item = -1;
2034 descr->item_height = 1;
2035 descr->page_size = 1;
2036 descr->column_width = 150;
2037 descr->horz_extent = (wnd->dwStyle & WS_HSCROLL) ? 1 : 0;
2038 descr->horz_pos = 0;
2039 descr->nb_tabs = 0;
2040 descr->tabs = NULL;
2041 descr->caret_on = TRUE;
2042 descr->font = 0;
2043 descr->locale = 0; /* FIXME */
2044 descr->lphc = lphc;
2046 if( lphc )
2048 TRACE(combo,"[%04x]: resetting owner %04x -> %04x\n",
2049 wnd->hwndSelf, descr->owner, lphc->self->hwndSelf );
2050 descr->owner = lphc->self->hwndSelf;
2053 *(LB_DESCR **)wnd->wExtra = descr;
2055 /* if (wnd->dwExStyle & WS_EX_NOPARENTNOTIFY) descr->style &= ~LBS_NOTIFY;
2057 if (descr->style & LBS_EXTENDEDSEL) descr->style |= LBS_MULTIPLESEL;
2058 if (descr->style & LBS_MULTICOLUMN) descr->style &= ~LBS_OWNERDRAWVARIABLE;
2059 if (descr->style & LBS_OWNERDRAWVARIABLE) descr->style |= LBS_NOINTEGRALHEIGHT;
2060 descr->item_height = LISTBOX_SetFont( wnd, descr, 0 );
2062 if (descr->style & LBS_OWNERDRAWFIXED)
2064 if( descr->lphc && (descr->lphc->dwStyle & CBS_DROPDOWN))
2066 /* WinWord gets VERY unhappy if we send WM_MEASUREITEM from here */
2067 descr->item_height = lphc->RectButton.bottom - lphc->RectButton.top - 6;
2069 else
2071 UINT32 id = (descr->lphc ) ? ID_CB_LISTBOX : wnd->wIDmenu;
2073 mis.CtlType = ODT_LISTBOX;
2074 mis.CtlID = id;
2075 mis.itemID = -1;
2076 mis.itemWidth = 0;
2077 mis.itemData = 0;
2078 mis.itemHeight = descr->item_height;
2079 SendMessage32A( descr->owner, WM_MEASUREITEM, id, (LPARAM)&mis );
2080 descr->item_height = mis.itemHeight ? mis.itemHeight : 1;
2084 return TRUE;
2088 /***********************************************************************
2089 * LISTBOX_Destroy
2091 static BOOL32 LISTBOX_Destroy( WND *wnd, LB_DESCR *descr )
2093 LISTBOX_ResetContent( wnd, descr );
2094 HeapDestroy( descr->heap );
2095 HeapFree( GetProcessHeap(), 0, descr );
2096 wnd->wExtra[0] = 0;
2097 return TRUE;
2101 /***********************************************************************
2102 * ListBoxWndProc
2104 LRESULT WINAPI ListBoxWndProc( HWND32 hwnd, UINT32 msg,
2105 WPARAM32 wParam, LPARAM lParam )
2107 LRESULT ret;
2108 LB_DESCR *descr;
2109 WND *wnd = WIN_FindWndPtr( hwnd );
2111 if (!wnd) return 0;
2112 if (!(descr = *(LB_DESCR **)wnd->wExtra))
2114 if (msg == WM_CREATE)
2116 if (!LISTBOX_Create( wnd, NULL )) return -1;
2117 TRACE(listbox, "creating wnd=%04x descr=%p\n",
2118 hwnd, *(LB_DESCR **)wnd->wExtra );
2119 return 0;
2121 /* Ignore all other messages before we get a WM_CREATE */
2122 return DefWindowProc32A( hwnd, msg, wParam, lParam );
2125 TRACE(listbox, "[%04x]: msg %s wp %08x lp %08lx\n",
2126 wnd->hwndSelf, SPY_GetMsgName(msg), wParam, lParam );
2127 switch(msg)
2129 case LB_RESETCONTENT16:
2130 case LB_RESETCONTENT32:
2131 LISTBOX_ResetContent( wnd, descr );
2132 return 0;
2134 case LB_ADDSTRING16:
2135 if (HAS_STRINGS(descr)) lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2136 /* fall through */
2137 case LB_ADDSTRING32:
2138 wParam = LISTBOX_FindStringPos( wnd, descr, (LPCSTR)lParam, FALSE );
2139 return LISTBOX_InsertString( wnd, descr, wParam, (LPCSTR)lParam );
2141 case LB_INSERTSTRING16:
2142 if (HAS_STRINGS(descr)) lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2143 wParam = (INT32)(INT16)wParam;
2144 /* fall through */
2145 case LB_INSERTSTRING32:
2146 return LISTBOX_InsertString( wnd, descr, wParam, (LPCSTR)lParam );
2148 case LB_ADDFILE16:
2149 if (HAS_STRINGS(descr)) lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2150 /* fall through */
2151 case LB_ADDFILE32:
2152 wParam = LISTBOX_FindFileStrPos( wnd, descr, (LPCSTR)lParam );
2153 return LISTBOX_InsertString( wnd, descr, wParam, (LPCSTR)lParam );
2155 case LB_DELETESTRING16:
2156 case LB_DELETESTRING32:
2157 return LISTBOX_RemoveItem( wnd, descr, wParam );
2159 case LB_GETITEMDATA16:
2160 case LB_GETITEMDATA32:
2161 if (((INT32)wParam < 0) || ((INT32)wParam >= descr->nb_items))
2162 return LB_ERR;
2163 return descr->items[wParam].data;
2165 case LB_SETITEMDATA16:
2166 case LB_SETITEMDATA32:
2167 if (((INT32)wParam < 0) || ((INT32)wParam >= descr->nb_items))
2168 return LB_ERR;
2169 descr->items[wParam].data = (DWORD)lParam;
2170 return LB_OKAY;
2172 case LB_GETCOUNT16:
2173 case LB_GETCOUNT32:
2174 return descr->nb_items;
2176 case LB_GETTEXT16:
2177 lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2178 /* fall through */
2179 case LB_GETTEXT32:
2180 return LISTBOX_GetText( wnd, descr, wParam, (LPSTR)lParam );
2182 case LB_GETTEXTLEN16:
2183 /* fall through */
2184 case LB_GETTEXTLEN32:
2185 if (wParam >= descr->nb_items) return LB_ERR;
2186 return (HAS_STRINGS(descr) ? strlen(descr->items[wParam].str)
2187 : sizeof(DWORD));
2189 case LB_GETCURSEL16:
2190 case LB_GETCURSEL32:
2191 return descr->selected_item;
2193 case LB_GETTOPINDEX16:
2194 case LB_GETTOPINDEX32:
2195 return descr->top_item;
2197 case LB_GETITEMHEIGHT16:
2198 case LB_GETITEMHEIGHT32:
2199 return LISTBOX_GetItemHeight( wnd, descr, wParam );
2201 case LB_SETITEMHEIGHT16:
2202 lParam = LOWORD(lParam);
2203 /* fall through */
2204 case LB_SETITEMHEIGHT32:
2205 return LISTBOX_SetItemHeight( wnd, descr, wParam, lParam );
2207 case LB_ITEMFROMPOINT32:
2209 POINT32 pt = { LOWORD(lParam), HIWORD(lParam) };
2210 RECT32 rect = { 0, 0, descr->width, descr->height };
2211 return MAKELONG( LISTBOX_GetItemFromPoint(wnd, descr, pt.x, pt.y),
2212 PtInRect32( &rect, pt ) );
2215 case LB_SETCARETINDEX16:
2216 case LB_SETCARETINDEX32:
2217 return LISTBOX_SetCaretIndex( wnd, descr, wParam, !lParam );
2219 case LB_GETCARETINDEX16:
2220 case LB_GETCARETINDEX32:
2221 return descr->focus_item;
2223 case LB_SETTOPINDEX16:
2224 case LB_SETTOPINDEX32:
2225 return LISTBOX_SetTopItem( wnd, descr, wParam, TRUE );
2227 case LB_SETCOLUMNWIDTH16:
2228 case LB_SETCOLUMNWIDTH32:
2229 return LISTBOX_SetColumnWidth( wnd, descr, wParam );
2231 case LB_GETITEMRECT16:
2233 RECT32 rect;
2234 ret = LISTBOX_GetItemRect( wnd, descr, (INT16)wParam, &rect );
2235 CONV_RECT32TO16( &rect, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
2237 return ret;
2239 case LB_GETITEMRECT32:
2240 return LISTBOX_GetItemRect( wnd, descr, wParam, (RECT32 *)lParam );
2242 case LB_FINDSTRING16:
2243 wParam = (INT32)(INT16)wParam;
2244 if (HAS_STRINGS(descr)) lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2245 /* fall through */
2246 case LB_FINDSTRING32:
2247 return LISTBOX_FindString( wnd, descr, wParam, (LPCSTR)lParam, FALSE );
2249 case LB_FINDSTRINGEXACT16:
2250 wParam = (INT32)(INT16)wParam;
2251 if (HAS_STRINGS(descr)) lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2252 /* fall through */
2253 case LB_FINDSTRINGEXACT32:
2254 return LISTBOX_FindString( wnd, descr, wParam, (LPCSTR)lParam, TRUE );
2256 case LB_SELECTSTRING16:
2257 wParam = (INT32)(INT16)wParam;
2258 if (HAS_STRINGS(descr)) lParam = (LPARAM)PTR_SEG_TO_LIN(lParam);
2259 /* fall through */
2260 case LB_SELECTSTRING32:
2262 INT32 index = LISTBOX_FindString( wnd, descr, wParam,
2263 (LPCSTR)lParam, FALSE );
2264 if (index == LB_ERR) return LB_ERR;
2265 LISTBOX_SetSelection( wnd, descr, index, TRUE, FALSE );
2266 return index;
2269 case LB_GETSEL16:
2270 wParam = (INT32)(INT16)wParam;
2271 /* fall through */
2272 case LB_GETSEL32:
2273 if (((INT32)wParam < 0) || ((INT32)wParam >= descr->nb_items))
2274 return LB_ERR;
2275 return descr->items[wParam].selected;
2277 case LB_SETSEL16:
2278 lParam = (INT32)(INT16)lParam;
2279 /* fall through */
2280 case LB_SETSEL32:
2281 return LISTBOX_SetSelection( wnd, descr, lParam, wParam, FALSE );
2283 case LB_SETCURSEL16:
2284 wParam = (INT32)(INT16)wParam;
2285 /* fall through */
2286 case LB_SETCURSEL32:
2287 if (wParam != -1) LISTBOX_MakeItemVisible( wnd, descr, wParam, TRUE );
2288 return LISTBOX_SetSelection( wnd, descr, wParam, TRUE, FALSE );
2290 case LB_GETSELCOUNT16:
2291 case LB_GETSELCOUNT32:
2292 return LISTBOX_GetSelCount( wnd, descr );
2294 case LB_GETSELITEMS16:
2295 return LISTBOX_GetSelItems16( wnd, descr, wParam,
2296 (LPINT16)PTR_SEG_TO_LIN(lParam) );
2298 case LB_GETSELITEMS32:
2299 return LISTBOX_GetSelItems32( wnd, descr, wParam, (LPINT32)lParam );
2301 case LB_SELITEMRANGE16:
2302 case LB_SELITEMRANGE32:
2303 if (LOWORD(lParam) <= HIWORD(lParam))
2304 return LISTBOX_SelectItemRange( wnd, descr, LOWORD(lParam),
2305 HIWORD(lParam), wParam );
2306 else
2307 return LISTBOX_SelectItemRange( wnd, descr, HIWORD(lParam),
2308 LOWORD(lParam), wParam );
2310 case LB_SELITEMRANGEEX16:
2311 case LB_SELITEMRANGEEX32:
2312 if ((INT32)lParam >= (INT32)wParam)
2313 return LISTBOX_SelectItemRange( wnd, descr, wParam, lParam, TRUE );
2314 else
2315 return LISTBOX_SelectItemRange( wnd, descr, lParam, wParam, FALSE);
2317 case LB_GETHORIZONTALEXTENT16:
2318 case LB_GETHORIZONTALEXTENT32:
2319 return descr->horz_extent;
2321 case LB_SETHORIZONTALEXTENT16:
2322 case LB_SETHORIZONTALEXTENT32:
2323 return LISTBOX_SetHorizontalExtent( wnd, descr, wParam );
2325 case LB_GETANCHORINDEX16:
2326 case LB_GETANCHORINDEX32:
2327 return descr->anchor_item;
2329 case LB_SETANCHORINDEX16:
2330 wParam = (INT32)(INT16)wParam;
2331 /* fall through */
2332 case LB_SETANCHORINDEX32:
2333 if (((INT32)wParam < -1) || ((INT32)wParam >= descr->nb_items))
2334 return LB_ERR;
2335 descr->anchor_item = (INT32)wParam;
2336 return LB_OKAY;
2338 case LB_DIR16:
2339 return LISTBOX_Directory( wnd, descr, wParam,
2340 (LPCSTR)PTR_SEG_TO_LIN(lParam), FALSE );
2342 case LB_DIR32:
2343 return LISTBOX_Directory( wnd, descr, wParam, (LPCSTR)lParam, TRUE );
2345 case LB_GETLOCALE32:
2346 return descr->locale;
2348 case LB_SETLOCALE32:
2349 descr->locale = (LCID)wParam; /* FIXME: should check for valid lcid */
2350 return LB_OKAY;
2352 case LB_INITSTORAGE32:
2353 return LISTBOX_InitStorage( wnd, descr, wParam, (DWORD)lParam );
2355 case LB_SETCOUNT32:
2356 return LISTBOX_SetCount( wnd, descr, (INT32)wParam );
2358 case LB_SETTABSTOPS16:
2359 return LISTBOX_SetTabStops( wnd, descr, (INT32)(INT16)wParam,
2360 (LPINT32)PTR_SEG_TO_LIN(lParam), TRUE );
2362 case LB_SETTABSTOPS32:
2363 return LISTBOX_SetTabStops( wnd, descr, wParam,
2364 (LPINT32)lParam, FALSE );
2366 case LB_CARETON16:
2367 case LB_CARETON32:
2368 if (descr->caret_on) return LB_OKAY;
2369 descr->caret_on = TRUE;
2370 if ((descr->focus_item != -1) && (GetFocus32() == wnd->hwndSelf))
2371 LISTBOX_RepaintItem( wnd, descr, descr->focus_item, ODA_FOCUS );
2372 return LB_OKAY;
2374 case LB_CARETOFF16:
2375 case LB_CARETOFF32:
2376 if (!descr->caret_on) return LB_OKAY;
2377 descr->caret_on = FALSE;
2378 if ((descr->focus_item != -1) && (GetFocus32() == wnd->hwndSelf))
2379 LISTBOX_RepaintItem( wnd, descr, descr->focus_item, ODA_FOCUS );
2380 return LB_OKAY;
2382 case WM_DESTROY:
2383 return LISTBOX_Destroy( wnd, descr );
2385 case WM_ENABLE:
2386 InvalidateRect32( hwnd, NULL, TRUE );
2387 return 0;
2389 case WM_SETREDRAW:
2390 LISTBOX_SetRedraw( wnd, descr, wParam != 0 );
2391 return 0;
2393 case WM_GETDLGCODE:
2394 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2396 case WM_PAINT:
2398 PAINTSTRUCT32 ps;
2399 HDC32 hdc = ( wParam ) ? ((HDC32)wParam)
2400 : BeginPaint32( hwnd, &ps );
2401 ret = LISTBOX_Paint( wnd, descr, hdc );
2402 if( !wParam ) EndPaint32( hwnd, &ps );
2404 return ret;
2406 case WM_SIZE:
2407 LISTBOX_UpdateSize( wnd, descr );
2408 return 0;
2410 case WM_GETFONT:
2411 return descr->font;
2413 case WM_SETFONT:
2414 LISTBOX_SetFont( wnd, descr, (HFONT32)wParam );
2415 if (lParam) InvalidateRect32( wnd->hwndSelf, 0, TRUE );
2416 return 0;
2418 case WM_SETFOCUS:
2419 descr->caret_on = TRUE;
2420 if (descr->focus_item != -1)
2421 LISTBOX_RepaintItem( wnd, descr, descr->focus_item, ODA_FOCUS );
2422 SEND_NOTIFICATION( wnd, descr, LBN_SETFOCUS );
2423 return 0;
2425 case WM_KILLFOCUS:
2426 if ((descr->focus_item != -1) && descr->caret_on)
2427 LISTBOX_RepaintItem( wnd, descr, descr->focus_item, ODA_FOCUS );
2428 SEND_NOTIFICATION( wnd, descr, LBN_KILLFOCUS );
2429 return 0;
2431 case WM_HSCROLL:
2432 return LISTBOX_HandleHScroll( wnd, descr, wParam, lParam );
2434 case WM_VSCROLL:
2435 return LISTBOX_HandleVScroll( wnd, descr, wParam, lParam );
2437 case WM_LBUTTONDOWN:
2438 return LISTBOX_HandleLButtonDown( wnd, descr, wParam,
2439 (INT16)LOWORD(lParam),
2440 (INT16)HIWORD(lParam) );
2442 case WM_LBUTTONDBLCLK:
2443 if (descr->style & LBS_NOTIFY)
2444 SEND_NOTIFICATION( wnd, descr, LBN_DBLCLK );
2445 return 0;
2447 case WM_MOUSEMOVE:
2448 if (GetCapture32() == hwnd)
2449 LISTBOX_HandleMouseMove( wnd, descr, (INT16)LOWORD(lParam),
2450 (INT16)HIWORD(lParam) );
2451 return 0;
2453 case WM_LBUTTONUP:
2454 return LISTBOX_HandleLButtonUp( wnd, descr );
2456 case WM_KEYDOWN:
2457 return LISTBOX_HandleKeyDown( wnd, descr, wParam );
2459 case WM_CHAR:
2460 return LISTBOX_HandleChar( wnd, descr, wParam );
2462 case WM_SYSTIMER:
2463 return LISTBOX_HandleSystemTimer( wnd, descr );
2465 case WM_ERASEBKGND:
2466 if (IS_OWNERDRAW(descr))
2468 RECT32 rect = { 0, 0, descr->width, descr->height };
2469 HBRUSH32 hbrush = SendMessage32A( descr->owner, WM_CTLCOLORLISTBOX,
2470 wParam, (LPARAM)wnd->hwndSelf );
2471 if (hbrush) FillRect32( (HDC32)wParam, &rect, hbrush );
2473 return 1;
2475 case WM_DROPFILES:
2476 if( !descr->lphc )
2477 return SendMessage32A( descr->owner, msg, wParam, lParam );
2478 break;
2480 case WM_DROPOBJECT:
2481 case WM_QUERYDROPOBJECT:
2482 case WM_DRAGSELECT:
2483 case WM_DRAGMOVE:
2484 if( !descr->lphc )
2486 LPDRAGINFO dragInfo = (LPDRAGINFO)PTR_SEG_TO_LIN( (SEGPTR)lParam );
2487 dragInfo->l = LISTBOX_GetItemFromPoint( wnd, descr, dragInfo->pt.x,
2488 dragInfo->pt.y );
2489 return SendMessage32A( descr->owner, msg, wParam, lParam );
2491 break;
2493 case WM_NCCREATE:
2494 if (TWEAK_WineLook > WIN31_LOOK)
2495 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
2496 return DefWindowProc32A( hwnd, msg, wParam, lParam );
2498 default:
2499 if ((msg >= WM_USER) && (msg < 0xc000))
2500 WARN(listbox, "[%04x]: unknown msg %04x wp %08x lp %08lx\n",
2501 hwnd, msg, wParam, lParam );
2502 return DefWindowProc32A( hwnd, msg, wParam, lParam );
2504 return 0;
2507 /***********************************************************************
2508 * COMBO_Directory
2510 LRESULT COMBO_Directory( LPHEADCOMBO lphc, UINT32 attrib, LPSTR dir, BOOL32 bLong)
2512 WND *wnd = WIN_FindWndPtr( lphc->hWndLBox );
2514 if( wnd )
2516 LB_DESCR *descr = *(LB_DESCR **)wnd->wExtra;
2517 if( descr )
2519 LRESULT lRet = LISTBOX_Directory( wnd, descr, attrib, dir, bLong );
2521 RedrawWindow32( lphc->self->hwndSelf, NULL, 0,
2522 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
2523 return lRet;
2526 return CB_ERR;
2529 /***********************************************************************
2530 * ComboLBWndProc
2532 * NOTE: in Windows, winproc address of the ComboLBox is the same
2533 * as that of the Listbox.
2535 LRESULT WINAPI ComboLBWndProc( HWND32 hwnd, UINT32 msg,
2536 WPARAM32 wParam, LPARAM lParam )
2538 LRESULT lRet = 0;
2539 WND *wnd = WIN_FindWndPtr( hwnd );
2541 if (wnd)
2543 LB_DESCR *descr = *(LB_DESCR **)wnd->wExtra;
2545 TRACE(combo, "[%04x]: msg %s wp %08x lp %08lx\n",
2546 wnd->hwndSelf, SPY_GetMsgName(msg), wParam, lParam );
2548 if( descr || msg == WM_CREATE )
2550 LPHEADCOMBO lphc = (descr) ? descr->lphc : NULL;
2552 switch( msg )
2554 case WM_CREATE:
2555 #define lpcs ((LPCREATESTRUCT32A)lParam)
2556 TRACE(combo, "\tpassed parent handle = 0x%08x\n",
2557 (UINT32)lpcs->lpCreateParams);
2559 lphc = (LPHEADCOMBO)(lpcs->lpCreateParams);
2560 #undef lpcs
2561 return LISTBOX_Create( wnd, lphc );
2563 case WM_LBUTTONDOWN:
2564 return LISTBOX_HandleLButtonDown( wnd, descr, wParam,
2565 (INT16)LOWORD(lParam), (INT16)HIWORD(lParam));
2567 /* avoid activation at all costs */
2569 case WM_MOUSEACTIVATE:
2570 return MA_NOACTIVATE;
2572 case WM_NCACTIVATE:
2573 return FALSE;
2575 case WM_KEYDOWN:
2576 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
2578 /* for some reason(?) Windows makes it possible to
2579 * show/hide ComboLBox by sending it WM_KEYDOWNs */
2581 if( (!(lphc->wState & CBF_EUI) && wParam == VK_F4) ||
2582 ( (lphc->wState & CBF_EUI) && !(lphc->wState & CBF_DROPPED)
2583 && (wParam == VK_DOWN || wParam == VK_UP)) )
2585 COMBO_FlipListbox( lphc, FALSE );
2586 return 0;
2589 return LISTBOX_HandleKeyDown( wnd, descr, wParam );
2591 case LB_SETCURSEL16:
2592 case LB_SETCURSEL32:
2593 lRet = ListBoxWndProc( hwnd, msg, wParam, lParam );
2594 return (lRet == LB_ERR) ? lRet : descr->selected_item;
2596 case WM_NCDESTROY:
2597 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
2598 lphc->hWndLBox = 0;
2599 /* fall through */
2601 default:
2602 return ListBoxWndProc( hwnd, msg, wParam, lParam );
2605 lRet = DefWindowProc32A( hwnd, msg, wParam, lParam );
2607 TRACE(combo,"\t default on msg [%04x]\n", (UINT16)msg );
2610 return lRet;