mirrored from git://source.winehq.org/git/wine.git
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
listbox.c
3245 lines (2868 loc) · 103 KB
/
listbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Listbox controls
*
* Copyright 1996 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "wingdi.h"
#include "user_private.h"
#include "controls.h"
#include "wine/exception.h"
#include "wine/debug.h"
#include "wine/heap.h"
WINE_DEFAULT_DEBUG_CHANNEL(listbox);
/* Items array granularity (must be power of 2) */
#define LB_ARRAY_GRANULARITY 16
/* Scrolling timeout in ms */
#define LB_SCROLL_TIMEOUT 50
/* Listbox system timer id */
#define LB_TIMER_ID 2
/* flag listbox changed while setredraw false - internal style */
#define LBS_DISPLAYCHANGED 0x80000000
/* Item structure */
typedef struct
{
LPWSTR str; /* Item text */
BOOL selected; /* Is item selected? */
UINT height; /* Item height (only for OWNERDRAWVARIABLE) */
ULONG_PTR data; /* User data */
} LB_ITEMDATA;
/* Listbox structure */
typedef struct
{
HWND self; /* Our own window handle */
HWND owner; /* Owner window to send notifications to */
UINT style; /* Window style */
INT width; /* Window width */
INT height; /* Window height */
union
{
LB_ITEMDATA *items; /* Array of items */
BYTE *nodata_items; /* For multi-selection LBS_NODATA */
} u;
INT nb_items; /* Number of items */
UINT items_size; /* Total number of allocated items in the array */
INT top_item; /* Top visible item */
INT selected_item; /* Selected item */
INT focus_item; /* Item that has the focus */
INT anchor_item; /* Anchor item for extended selection */
INT item_height; /* Default item height */
INT page_size; /* Items per listbox page */
INT column_width; /* Column width for multi-column listboxes */
INT horz_extent; /* Horizontal extent */
INT horz_pos; /* Horizontal position */
INT nb_tabs; /* Number of tabs in array */
INT *tabs; /* Array of tabs */
INT avg_char_width; /* Average width of characters */
INT wheel_remain; /* Left over scroll amount */
BOOL caret_on; /* Is caret on? */
BOOL captured; /* Is mouse captured? */
BOOL in_focus;
HFONT font; /* Current font */
LCID locale; /* Current locale for string comparisons */
HEADCOMBO *lphc; /* ComboLBox */
} LB_DESCR;
#define IS_OWNERDRAW(descr) \
((descr)->style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE))
#define HAS_STRINGS(descr) \
(!IS_OWNERDRAW(descr) || ((descr)->style & LBS_HASSTRINGS))
#define IS_MULTISELECT(descr) \
((descr)->style & (LBS_MULTIPLESEL|LBS_EXTENDEDSEL) && \
!((descr)->style & LBS_NOSEL))
#define SEND_NOTIFICATION(descr,code) \
(SendMessageW( (descr)->owner, WM_COMMAND, \
MAKEWPARAM( GetWindowLongPtrW((descr->self),GWLP_ID), (code)), (LPARAM)(descr->self) ))
#define ISWIN31 (LOWORD(GetVersion()) == 0x0a03)
/* Current timer status */
typedef enum
{
LB_TIMER_NONE,
LB_TIMER_UP,
LB_TIMER_LEFT,
LB_TIMER_DOWN,
LB_TIMER_RIGHT
} TIMER_DIRECTION;
static TIMER_DIRECTION LISTBOX_Timer = LB_TIMER_NONE;
static LRESULT LISTBOX_GetItemRect( const LB_DESCR *descr, INT index, RECT *rect );
/*
For listboxes without LBS_NODATA, an array of LB_ITEMDATA is allocated
to store the states of each item into descr->u.items.
For single-selection LBS_NODATA listboxes, no storage is allocated,
and thus descr->u.nodata_items will always be NULL.
For multi-selection LBS_NODATA listboxes, one byte per item is stored
for the item's selection state into descr->u.nodata_items.
*/
static size_t get_sizeof_item( const LB_DESCR *descr )
{
return (descr->style & LBS_NODATA) ? sizeof(BYTE) : sizeof(LB_ITEMDATA);
}
static BOOL resize_storage(LB_DESCR *descr, UINT items_size)
{
LB_ITEMDATA *items;
if (items_size > descr->items_size ||
items_size + LB_ARRAY_GRANULARITY * 2 < descr->items_size)
{
items_size = (items_size + LB_ARRAY_GRANULARITY - 1) & ~(LB_ARRAY_GRANULARITY - 1);
if ((descr->style & (LBS_NODATA | LBS_MULTIPLESEL | LBS_EXTENDEDSEL)) != LBS_NODATA)
{
items = heap_realloc(descr->u.items, items_size * get_sizeof_item(descr));
if (!items)
{
SEND_NOTIFICATION(descr, LBN_ERRSPACE);
return FALSE;
}
descr->u.items = items;
}
descr->items_size = items_size;
}
if ((descr->style & LBS_NODATA) && descr->u.nodata_items && items_size > descr->nb_items)
{
memset(descr->u.nodata_items + descr->nb_items, 0,
(items_size - descr->nb_items) * get_sizeof_item(descr));
}
return TRUE;
}
static ULONG_PTR get_item_data( const LB_DESCR *descr, UINT index )
{
return (descr->style & LBS_NODATA) ? 0 : descr->u.items[index].data;
}
static void set_item_data( LB_DESCR *descr, UINT index, ULONG_PTR data )
{
if (!(descr->style & LBS_NODATA)) descr->u.items[index].data = data;
}
static WCHAR *get_item_string( const LB_DESCR *descr, UINT index )
{
return HAS_STRINGS(descr) ? descr->u.items[index].str : NULL;
}
static void set_item_string( const LB_DESCR *descr, UINT index, WCHAR *string )
{
if (!(descr->style & LBS_NODATA)) descr->u.items[index].str = string;
}
static UINT get_item_height( const LB_DESCR *descr, UINT index )
{
return (descr->style & LBS_NODATA) ? 0 : descr->u.items[index].height;
}
static void set_item_height( LB_DESCR *descr, UINT index, UINT height )
{
if (!(descr->style & LBS_NODATA)) descr->u.items[index].height = height;
}
static BOOL is_item_selected( const LB_DESCR *descr, UINT index )
{
if (!(descr->style & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
return index == descr->selected_item;
if (descr->style & LBS_NODATA)
return descr->u.nodata_items[index];
else
return descr->u.items[index].selected;
}
static void set_item_selected_state(LB_DESCR *descr, UINT index, BOOL state)
{
if (descr->style & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL))
{
if (descr->style & LBS_NODATA)
descr->u.nodata_items[index] = state;
else
descr->u.items[index].selected = state;
}
}
static void insert_item_data(LB_DESCR *descr, UINT index)
{
size_t size = get_sizeof_item(descr);
BYTE *p = descr->u.nodata_items + index * size;
if (!descr->u.items) return;
if (index < descr->nb_items)
memmove(p + size, p, (descr->nb_items - index) * size);
}
static void remove_item_data(LB_DESCR *descr, UINT index)
{
size_t size = get_sizeof_item(descr);
BYTE *p = descr->u.nodata_items + index * size;
if (!descr->u.items) return;
if (index < descr->nb_items)
memmove(p, p + size, (descr->nb_items - index) * size);
}
/***********************************************************************
* LISTBOX_GetCurrentPageSize
*
* Return the current page size
*/
static INT LISTBOX_GetCurrentPageSize( const LB_DESCR *descr )
{
INT i, height;
if (!(descr->style & LBS_OWNERDRAWVARIABLE)) return descr->page_size;
for (i = descr->top_item, height = 0; i < descr->nb_items; i++)
{
if ((height += get_item_height(descr, i)) > descr->height) break;
}
if (i == descr->top_item) return 1;
else return i - descr->top_item;
}
/***********************************************************************
* LISTBOX_GetMaxTopIndex
*
* Return the maximum possible index for the top of the listbox.
*/
static INT LISTBOX_GetMaxTopIndex( const LB_DESCR *descr )
{
INT max, page;
if (descr->style & LBS_OWNERDRAWVARIABLE)
{
page = descr->height;
for (max = descr->nb_items - 1; max >= 0; max--)
if ((page -= get_item_height(descr, max)) < 0) break;
if (max < descr->nb_items - 1) max++;
}
else if (descr->style & LBS_MULTICOLUMN)
{
if ((page = descr->width / descr->column_width) < 1) page = 1;
max = (descr->nb_items + descr->page_size - 1) / descr->page_size;
max = (max - page) * descr->page_size;
}
else
{
max = descr->nb_items - descr->page_size;
}
if (max < 0) max = 0;
return max;
}
/***********************************************************************
* LISTBOX_UpdateScroll
*
* Update the scrollbars. Should be called whenever the content
* of the listbox changes.
*/
static void LISTBOX_UpdateScroll( LB_DESCR *descr )
{
SCROLLINFO info;
/* Check the listbox scroll bar flags individually before we call
NtUserSetScrollInfo otherwise when the listbox style is WS_HSCROLL and
no WS_VSCROLL, we end up with an uninitialized, visible horizontal
scroll bar when we do not need one.
if (!(descr->style & WS_VSCROLL)) return;
*/
/* It is important that we check descr->style, and not wnd->dwStyle,
for WS_VSCROLL, as the former is exactly the one passed in
argument to CreateWindow.
In Windows (and from now on in Wine :) a listbox created
with such a style (no WS_SCROLL) does not update
the scrollbar with listbox-related data, thus letting
the programmer use it for his/her own purposes. */
if (descr->style & LBS_NOREDRAW) return;
info.cbSize = sizeof(info);
if (descr->style & LBS_MULTICOLUMN)
{
info.nMin = 0;
info.nMax = (descr->nb_items - 1) / descr->page_size;
info.nPos = descr->top_item / descr->page_size;
info.nPage = descr->width / descr->column_width;
if (info.nPage < 1) info.nPage = 1;
info.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
if (descr->style & LBS_DISABLENOSCROLL)
info.fMask |= SIF_DISABLENOSCROLL;
if (descr->style & WS_HSCROLL)
NtUserSetScrollInfo( descr->self, SB_HORZ, &info, TRUE );
info.nMax = 0;
info.fMask = SIF_RANGE;
if (descr->style & WS_VSCROLL)
NtUserSetScrollInfo( descr->self, SB_VERT, &info, TRUE );
}
else
{
info.nMin = 0;
info.nMax = descr->nb_items - 1;
info.nPos = descr->top_item;
info.nPage = LISTBOX_GetCurrentPageSize( descr );
info.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
if (descr->style & LBS_DISABLENOSCROLL)
info.fMask |= SIF_DISABLENOSCROLL;
if (descr->style & WS_VSCROLL)
NtUserSetScrollInfo( descr->self, SB_VERT, &info, TRUE );
if ((descr->style & WS_HSCROLL) && descr->horz_extent)
{
info.nPos = descr->horz_pos;
info.nPage = descr->width;
info.fMask = SIF_POS | SIF_PAGE;
if (descr->style & LBS_DISABLENOSCROLL)
info.fMask |= SIF_DISABLENOSCROLL;
NtUserSetScrollInfo( descr->self, SB_HORZ, &info, TRUE );
}
else
{
if (descr->style & LBS_DISABLENOSCROLL)
{
info.nMin = 0;
info.nMax = 0;
info.fMask = SIF_RANGE | SIF_DISABLENOSCROLL;
NtUserSetScrollInfo( descr->self, SB_HORZ, &info, TRUE );
}
else
{
NtUserShowScrollBar( descr->self, SB_HORZ, FALSE );
}
}
}
}
/***********************************************************************
* LISTBOX_SetTopItem
*
* Set the top item of the listbox, scrolling up or down if necessary.
*/
static LRESULT LISTBOX_SetTopItem( LB_DESCR *descr, INT index, BOOL scroll )
{
INT max = LISTBOX_GetMaxTopIndex( descr );
TRACE("setting top item %d, scroll %d\n", index, scroll);
if (index > max) index = max;
if (index < 0) index = 0;
if (descr->style & LBS_MULTICOLUMN) index -= index % descr->page_size;
if (descr->top_item == index) return LB_OKAY;
if (scroll)
{
INT dx = 0, dy = 0;
if (descr->style & LBS_MULTICOLUMN)
dx = (descr->top_item - index) / descr->page_size * descr->column_width;
else if (descr->style & LBS_OWNERDRAWVARIABLE)
{
INT i;
if (index > descr->top_item)
{
for (i = index - 1; i >= descr->top_item; i--)
dy -= get_item_height(descr, i);
}
else
{
for (i = index; i < descr->top_item; i++)
dy += get_item_height(descr, i);
}
}
else
dy = (descr->top_item - index) * descr->item_height;
NtUserScrollWindowEx( descr->self, dx, dy, NULL, NULL, 0, NULL,
SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
}
else
NtUserInvalidateRect( descr->self, NULL, TRUE );
descr->top_item = index;
LISTBOX_UpdateScroll( descr );
return LB_OKAY;
}
/***********************************************************************
* LISTBOX_UpdatePage
*
* Update the page size. Should be called when the size of
* the client area or the item height changes.
*/
static void LISTBOX_UpdatePage( LB_DESCR *descr )
{
INT page_size;
if ((descr->item_height == 0) || (page_size = descr->height / descr->item_height) < 1)
page_size = 1;
if (page_size == descr->page_size) return;
descr->page_size = page_size;
if (descr->style & LBS_MULTICOLUMN)
NtUserInvalidateRect( descr->self, NULL, TRUE );
LISTBOX_SetTopItem( descr, descr->top_item, FALSE );
}
/***********************************************************************
* LISTBOX_UpdateSize
*
* Update the size of the listbox. Should be called when the size of
* the client area changes.
*/
static void LISTBOX_UpdateSize( LB_DESCR *descr )
{
RECT rect;
GetClientRect( descr->self, &rect );
descr->width = rect.right - rect.left;
descr->height = rect.bottom - rect.top;
if (!(descr->style & LBS_NOINTEGRALHEIGHT) && !(descr->style & LBS_OWNERDRAWVARIABLE))
{
INT remaining;
RECT rect;
GetWindowRect( descr->self, &rect );
if(descr->item_height != 0)
remaining = descr->height % descr->item_height;
else
remaining = 0;
if ((descr->height > descr->item_height) && remaining)
{
TRACE("[%p]: changing height %d -> %d\n",
descr->self, descr->height, descr->height - remaining );
NtUserSetWindowPos( descr->self, 0, 0, 0, rect.right - rect.left,
rect.bottom - rect.top - remaining,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE );
return;
}
}
TRACE("[%p]: new size = %d,%d\n", descr->self, descr->width, descr->height );
LISTBOX_UpdatePage( descr );
LISTBOX_UpdateScroll( descr );
/* Invalidate the focused item so it will be repainted correctly */
if (LISTBOX_GetItemRect( descr, descr->focus_item, &rect ) == 1)
{
NtUserInvalidateRect( descr->self, &rect, FALSE );
}
}
/***********************************************************************
* LISTBOX_GetItemRect
*
* Get the rectangle enclosing an item, in listbox client coordinates.
* Return 1 if the rectangle is (partially) visible, 0 if hidden, -1 on error.
*/
static LRESULT LISTBOX_GetItemRect( const LB_DESCR *descr, INT index, RECT *rect )
{
/* Index <= 0 is legal even on empty listboxes */
if (index && (index >= descr->nb_items))
{
SetRectEmpty(rect);
SetLastError(ERROR_INVALID_INDEX);
return LB_ERR;
}
SetRect( rect, 0, 0, descr->width, descr->height );
if (descr->style & LBS_MULTICOLUMN)
{
INT col = (index / descr->page_size) -
(descr->top_item / descr->page_size);
rect->left += col * descr->column_width;
rect->right = rect->left + descr->column_width;
rect->top += (index % descr->page_size) * descr->item_height;
rect->bottom = rect->top + descr->item_height;
}
else if (descr->style & LBS_OWNERDRAWVARIABLE)
{
INT i;
rect->right += descr->horz_pos;
if ((index >= 0) && (index < descr->nb_items))
{
if (index < descr->top_item)
{
for (i = descr->top_item-1; i >= index; i--)
rect->top -= get_item_height(descr, i);
}
else
{
for (i = descr->top_item; i < index; i++)
rect->top += get_item_height(descr, i);
}
rect->bottom = rect->top + get_item_height(descr, index);
}
}
else
{
rect->top += (index - descr->top_item) * descr->item_height;
rect->bottom = rect->top + descr->item_height;
rect->right += descr->horz_pos;
}
TRACE("item %d, rect %s\n", index, wine_dbgstr_rect(rect));
return ((rect->left < descr->width) && (rect->right > 0) &&
(rect->top < descr->height) && (rect->bottom > 0));
}
/***********************************************************************
* LISTBOX_GetItemFromPoint
*
* Return the item nearest from point (x,y) (in client coordinates).
*/
static INT LISTBOX_GetItemFromPoint( const LB_DESCR *descr, INT x, INT y )
{
INT index = descr->top_item;
if (!descr->nb_items) return -1; /* No items */
if (descr->style & LBS_OWNERDRAWVARIABLE)
{
INT pos = 0;
if (y >= 0)
{
while (index < descr->nb_items)
{
if ((pos += get_item_height(descr, index)) > y) break;
index++;
}
}
else
{
while (index > 0)
{
index--;
if ((pos -= get_item_height(descr, index)) <= y) break;
}
}
}
else if (descr->style & LBS_MULTICOLUMN)
{
if (y >= descr->item_height * descr->page_size) return -1;
if (y >= 0) index += y / descr->item_height;
if (x >= 0) index += (x / descr->column_width) * descr->page_size;
else index -= (((x + 1) / descr->column_width) - 1) * descr->page_size;
}
else
{
index += (y / descr->item_height);
}
if (index < 0) return 0;
if (index >= descr->nb_items) return -1;
return index;
}
/***********************************************************************
* LISTBOX_PaintItem
*
* Paint an item.
*/
static void LISTBOX_PaintItem( LB_DESCR *descr, HDC hdc, const RECT *rect,
INT index, UINT action, BOOL ignoreFocus )
{
BOOL selected = FALSE, focused;
WCHAR *item_str = NULL;
if (index < descr->nb_items)
{
item_str = get_item_string(descr, index);
selected = is_item_selected(descr, index);
}
focused = !ignoreFocus && descr->focus_item == index && descr->caret_on && descr->in_focus;
if (IS_OWNERDRAW(descr))
{
DRAWITEMSTRUCT dis;
RECT r;
HRGN hrgn;
if (index >= descr->nb_items)
{
if (action == ODA_FOCUS)
DrawFocusRect( hdc, rect );
else
ERR("called with an out of bounds index %d(%d) in owner draw, Not good.\n",index,descr->nb_items);
return;
}
/* some programs mess with the clipping region when
drawing the item, *and* restore the previous region
after they are done, so a region has better to exist
else everything ends clipped */
GetClientRect(descr->self, &r);
hrgn = set_control_clipping( hdc, &r );
dis.CtlType = ODT_LISTBOX;
dis.CtlID = GetWindowLongPtrW( descr->self, GWLP_ID );
dis.hwndItem = descr->self;
dis.itemAction = action;
dis.hDC = hdc;
dis.itemID = index;
dis.itemState = 0;
if (selected)
dis.itemState |= ODS_SELECTED;
if (focused)
dis.itemState |= ODS_FOCUS;
if (!IsWindowEnabled(descr->self)) dis.itemState |= ODS_DISABLED;
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, debugstr_w(item_str), action,
dis.itemState, wine_dbgstr_rect(rect) );
SendMessageW(descr->owner, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
SelectClipRgn( hdc, hrgn );
if (hrgn) DeleteObject( hrgn );
}
else
{
COLORREF oldText = 0, oldBk = 0;
if (action == ODA_FOCUS)
{
DrawFocusRect( hdc, rect );
return;
}
if (selected)
{
oldBk = SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
oldText = SetTextColor( hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
}
TRACE("[%p]: painting %d (%s) action=%02x rect=%s\n",
descr->self, index, debugstr_w(item_str), action,
wine_dbgstr_rect(rect) );
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,
lstrlenW(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, lstrlenW(item_str),
descr->nb_tabs, descr->tabs, 0);
}
if (selected)
{
SetBkColor( hdc, oldBk );
SetTextColor( hdc, oldText );
}
if (focused)
DrawFocusRect( hdc, rect );
}
}
/***********************************************************************
* LISTBOX_SetRedraw
*
* Change the redraw flag.
*/
static void LISTBOX_SetRedraw( LB_DESCR *descr, BOOL on )
{
if (on)
{
if (!(descr->style & LBS_NOREDRAW)) return;
descr->style &= ~LBS_NOREDRAW;
if (descr->style & LBS_DISPLAYCHANGED)
{ /* page was changed while setredraw false, refresh automatically */
NtUserInvalidateRect(descr->self, NULL, TRUE);
if ((descr->top_item + descr->page_size) > descr->nb_items)
{ /* reset top of page if less than number of items/page */
descr->top_item = descr->nb_items - descr->page_size;
if (descr->top_item < 0) descr->top_item = 0;
}
descr->style &= ~LBS_DISPLAYCHANGED;
}
LISTBOX_UpdateScroll( descr );
}
else descr->style |= LBS_NOREDRAW;
}
/***********************************************************************
* LISTBOX_RepaintItem
*
* Repaint a single item synchronously.
*/
static void LISTBOX_RepaintItem( LB_DESCR *descr, INT index, UINT action )
{
HDC hdc;
RECT rect;
HFONT oldFont = 0;
HBRUSH hbrush, oldBrush = 0;
/* Do not repaint the item if the item is not visible */
if (!IsWindowVisible(descr->self)) return;
if (descr->style & LBS_NOREDRAW)
{
descr->style |= LBS_DISPLAYCHANGED;
return;
}
if (LISTBOX_GetItemRect( descr, index, &rect ) != 1) return;
if (!(hdc = NtUserGetDCEx( descr->self, 0, DCX_CACHE ))) return;
if (descr->font) oldFont = SelectObject( hdc, descr->font );
hbrush = (HBRUSH)SendMessageW( descr->owner, WM_CTLCOLORLISTBOX,
(WPARAM)hdc, (LPARAM)descr->self );
if (hbrush) oldBrush = SelectObject( hdc, hbrush );
if (!IsWindowEnabled(descr->self))
SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
SetWindowOrgEx( hdc, descr->horz_pos, 0, NULL );
LISTBOX_PaintItem( descr, hdc, &rect, index, action, TRUE );
if (oldFont) SelectObject( hdc, oldFont );
if (oldBrush) SelectObject( hdc, oldBrush );
NtUserReleaseDC( descr->self, hdc );
}
/***********************************************************************
* LISTBOX_DrawFocusRect
*/
static void LISTBOX_DrawFocusRect( LB_DESCR *descr, BOOL on )
{
HDC hdc;
RECT rect;
HFONT oldFont = 0;
/* Do not repaint the item if the item is not visible */
if (!IsWindowVisible(descr->self)) return;
if (descr->focus_item == -1) return;
if (!descr->caret_on || !descr->in_focus) return;
if (LISTBOX_GetItemRect( descr, descr->focus_item, &rect ) != 1) return;
if (!(hdc = NtUserGetDCEx( descr->self, 0, DCX_CACHE ))) return;
if (descr->font) oldFont = SelectObject( hdc, descr->font );
if (!IsWindowEnabled(descr->self))
SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
SetWindowOrgEx( hdc, descr->horz_pos, 0, NULL );
LISTBOX_PaintItem( descr, hdc, &rect, descr->focus_item, ODA_FOCUS, !on );
if (oldFont) SelectObject( hdc, oldFont );
NtUserReleaseDC( descr->self, hdc );
}
/***********************************************************************
* LISTBOX_InitStorage
*/
static LRESULT LISTBOX_InitStorage( LB_DESCR *descr, INT nb_items )
{
UINT new_size = descr->nb_items + nb_items;
if (new_size > descr->items_size && !resize_storage(descr, new_size))
return LB_ERRSPACE;
return descr->items_size;
}
/***********************************************************************
* LISTBOX_SetTabStops
*/
static BOOL LISTBOX_SetTabStops( LB_DESCR *descr, INT count, LPINT tabs )
{
INT i;
if (!(descr->style & LBS_USETABSTOPS))
{
SetLastError(ERROR_LB_WITHOUT_TABSTOPS);
return FALSE;
}
HeapFree( GetProcessHeap(), 0, descr->tabs );
if (!(descr->nb_tabs = count))
{
descr->tabs = NULL;
return TRUE;
}
if (!(descr->tabs = HeapAlloc( GetProcessHeap(), 0,
descr->nb_tabs * sizeof(INT) )))
return FALSE;
memcpy( descr->tabs, tabs, descr->nb_tabs * sizeof(INT) );
/* convert into "dialog units"*/
for (i = 0; i < descr->nb_tabs; i++)
descr->tabs[i] = MulDiv(descr->tabs[i], descr->avg_char_width, 4);
return TRUE;
}
/***********************************************************************
* LISTBOX_GetText
*/
static LRESULT LISTBOX_GetText( LB_DESCR *descr, INT index, LPWSTR buffer, BOOL unicode )
{
DWORD len;
if ((index < 0) || (index >= descr->nb_items))
{
SetLastError(ERROR_INVALID_INDEX);
return LB_ERR;
}
if (HAS_STRINGS(descr))
{
WCHAR *str = get_item_string(descr, index);
if (!buffer)
{
len = lstrlenW(str);
if( unicode )
return len;
return WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
}
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)
{
lstrcpyW(buffer, str);
len = lstrlenW(buffer);
}
else
{
len = WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)buffer,
0x7FFFFFFF, NULL, NULL) - 1;
}
}
__EXCEPT_PAGE_FAULT
{
WARN( "got an invalid buffer (Delphi bug?)\n" );
SetLastError( ERROR_INVALID_PARAMETER );
return LB_ERR;
}
__ENDTRY
} else
{
if (buffer)
*((ULONG_PTR *)buffer) = get_item_data(descr, index);
len = sizeof(ULONG_PTR);
}
return len;
}
static inline INT LISTBOX_lstrcmpiW( LCID lcid, LPCWSTR str1, LPCWSTR str2, int len )
{
INT ret = CompareStringW( lcid, NORM_IGNORECASE, str1, len, str2, len );
if (ret == CSTR_LESS_THAN)
return -1;
if (ret == CSTR_EQUAL)
return 0;
if (ret == CSTR_GREATER_THAN)
return 1;
return -1;
}
/***********************************************************************
* LISTBOX_FindStringPos
*
* Find the nearest string located before a given string in sort order.
* If 'exact' is TRUE, return an error if we don't get an exact match.
*/
static INT LISTBOX_FindStringPos( LB_DESCR *descr, LPCWSTR str, BOOL exact )
{
INT index, min, max, res;
if (!descr->nb_items || !(descr->style & LBS_SORT)) return -1; /* Add it at the end */
min = 0;
max = descr->nb_items - 1;
while (min <= max)
{
index = (min + max) / 2;
if (HAS_STRINGS(descr))
res = LISTBOX_lstrcmpiW( descr->locale, get_item_string(descr, index), str, -1 );
else
{
COMPAREITEMSTRUCT cis;
UINT id = (UINT)GetWindowLongPtrW( descr->self, GWLP_ID );
cis.CtlType = ODT_LISTBOX;
cis.CtlID = id;
cis.hwndItem = descr->self;
/* note that some application (MetaStock) expects the second item
* to be in the listbox */
cis.itemID1 = index;
cis.itemData1 = get_item_data(descr, index);
cis.itemID2 = -1;
cis.itemData2 = (ULONG_PTR)str;
cis.dwLocaleId = descr->locale;
res = SendMessageW( descr->owner, WM_COMPAREITEM, id, (LPARAM)&cis );
}
if (!res) return index;
if (res > 0) max = index - 1;
else min = index + 1;
}
return exact ? -1 : min;
}
/***********************************************************************
* LISTBOX_FindFileStrPos
*
* Find the nearest string located before a given string in directory
* sort order (i.e. first files, then directories, then drives).
*/
static INT LISTBOX_FindFileStrPos( LB_DESCR *descr, LPCWSTR str )
{
INT min, max, res;
if (!HAS_STRINGS(descr))
return LISTBOX_FindStringPos( descr, str, FALSE );
min = 0;
max = descr->nb_items;
while (min != max)
{
INT index = (min + max) / 2;
LPCWSTR p = get_item_string(descr, index);
if (*p == '[') /* drive or directory */
{
if (*str != '[') res = -1;
else if (p[1] == '-') /* drive */
{
if (str[1] == '-') res = str[2] - p[2];
else res = -1;
}
else /* directory */
{
if (str[1] == '-') res = 1;
else res = LISTBOX_lstrcmpiW( descr->locale, str, p, -1 );
}
}
else /* filename */
{
if (*str == '[') res = 1;
else res = LISTBOX_lstrcmpiW( descr->locale, str, p, -1 );
}
if (!res) return index;
if (res < 0) max = index;
else min = index + 1;
}
return max;
}
/***********************************************************************
* LISTBOX_FindString
*
* Find the item beginning with a given string.
*/
static INT LISTBOX_FindString( LB_DESCR *descr, INT start, LPCWSTR str, BOOL exact )