user32: Fix F1 message sequence.
[wine] / dlls / user32 / combo.c
1 /*
2  * Combo controls
3  *
4  * Copyright 1997 Alex Korobka
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  *
22  * This code was audited for completeness against the documented features
23  * of Comctl32.dll version 6.0 on Oct. 4, 2004, by Dimitrie O. Paun.
24  * 
25  * Unless otherwise noted, we believe this code to be complete, as per
26  * the specification mentioned above.
27  * If you discover missing features, or bugs, please note them below.
28  * 
29  * TODO:
30  *   - ComboBox_[GS]etMinVisible()
31  *   - CB_GETMINVISIBLE, CB_SETMINVISIBLE
32  *   - CB_SETTOPINDEX
33  */
34
35 #include <stdarg.h>
36 #include <string.h>
37
38 #define OEMRESOURCE
39
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "wine/winuser16.h"
45 #include "wine/unicode.h"
46 #include "user_private.h"
47 #include "win.h"
48 #include "controls.h"
49 #include "winternl.h"
50 #include "wine/debug.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(combo);
53
54   /* bits in the dwKeyData */
55 #define KEYDATA_ALT             0x2000
56 #define KEYDATA_PREVSTATE       0x4000
57
58 /*
59  * Additional combo box definitions
60  */
61
62 #define CB_NOTIFY( lphc, code ) \
63     (SendMessageW((lphc)->owner, WM_COMMAND, \
64                   MAKEWPARAM(GetWindowLongPtrW((lphc)->self,GWLP_ID), (code)), (LPARAM)(lphc)->self))
65
66 #define CB_DISABLED( lphc )   (!IsWindowEnabled((lphc)->self))
67 #define CB_OWNERDRAWN( lphc ) ((lphc)->dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
68 #define CB_HASSTRINGS( lphc ) ((lphc)->dwStyle & CBS_HASSTRINGS)
69 #define CB_HWND( lphc )       ((lphc)->self)
70 #define CB_GETTYPE( lphc )    ((lphc)->dwStyle & (CBS_DROPDOWNLIST))
71
72 #define ISWIN31 (LOWORD(GetVersion()) == 0x0a03)
73
74 /*
75  * Drawing globals
76  */
77 static HBITMAP  hComboBmp = 0;
78 static UINT     CBitHeight, CBitWidth;
79
80 /*
81  * Look and feel dependent "constants"
82  */
83
84 #define COMBO_YBORDERGAP         5
85 #define COMBO_XBORDERSIZE()      2
86 #define COMBO_YBORDERSIZE()      2
87 #define COMBO_EDITBUTTONSPACE()  0
88 #define EDIT_CONTROL_PADDING()   1
89
90 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
91 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
92
93 /*********************************************************************
94  * combo class descriptor
95  */
96 static const WCHAR comboboxW[] = {'C','o','m','b','o','B','o','x',0};
97 const struct builtin_class_descr COMBO_builtin_class =
98 {
99     comboboxW,            /* name */
100     CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, /* style  */
101     ComboWndProcA,        /* procA */
102     ComboWndProcW,        /* procW */
103     sizeof(HEADCOMBO *),  /* extra */
104     IDC_ARROW,            /* cursor */
105     0                     /* brush */
106 };
107
108
109 /***********************************************************************
110  *           COMBO_Init
111  *
112  * Load combo button bitmap.
113  */
114 static BOOL COMBO_Init(void)
115 {
116   HDC           hDC;
117
118   if( hComboBmp ) return TRUE;
119   if( (hDC = CreateCompatibleDC(0)) )
120   {
121     BOOL        bRet = FALSE;
122     if( (hComboBmp = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_COMBO))) )
123     {
124       BITMAP      bm;
125       HBITMAP     hPrevB;
126       RECT        r;
127
128       GetObjectW( hComboBmp, sizeof(bm), &bm );
129       CBitHeight = bm.bmHeight;
130       CBitWidth  = bm.bmWidth;
131
132       TRACE("combo bitmap [%i,%i]\n", CBitWidth, CBitHeight );
133
134       hPrevB = SelectObject( hDC, hComboBmp);
135       SetRect( &r, 0, 0, CBitWidth, CBitHeight );
136       InvertRect( hDC, &r );
137       SelectObject( hDC, hPrevB );
138       bRet = TRUE;
139     }
140     DeleteDC( hDC );
141     return bRet;
142   }
143   return FALSE;
144 }
145
146 /***********************************************************************
147  *           COMBO_NCCreate
148  */
149 static LRESULT COMBO_NCCreate(HWND hwnd, LONG style)
150 {
151     LPHEADCOMBO lphc;
152
153     if (COMBO_Init() && (lphc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEADCOMBO))) )
154     {
155         lphc->self = hwnd;
156         SetWindowLongPtrW( hwnd, 0, (LONG_PTR)lphc );
157
158        /* some braindead apps do try to use scrollbar/border flags */
159
160         lphc->dwStyle = style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL);
161         SetWindowLongW( hwnd, GWL_STYLE, style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL) );
162
163         /*
164          * We also have to remove the client edge style to make sure
165          * we don't end-up with a non client area.
166          */
167         SetWindowLongW( hwnd, GWL_EXSTYLE,
168                         GetWindowLongW( hwnd, GWL_EXSTYLE ) & ~WS_EX_CLIENTEDGE );
169
170         if( !(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
171               lphc->dwStyle |= CBS_HASSTRINGS;
172         if( !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) )
173               lphc->wState |= CBF_NOTIFY;
174
175         TRACE("[%p], style = %08x\n", lphc, lphc->dwStyle );
176         return TRUE;
177     }
178     return FALSE;
179 }
180
181 /***********************************************************************
182  *           COMBO_NCDestroy
183  */
184 static LRESULT COMBO_NCDestroy( LPHEADCOMBO lphc )
185 {
186
187    if( lphc )
188    {
189        TRACE("[%p]: freeing storage\n", lphc->self);
190
191        if( (CB_GETTYPE(lphc) != CBS_SIMPLE) && lphc->hWndLBox )
192            DestroyWindow( lphc->hWndLBox );
193
194        SetWindowLongPtrW( lphc->self, 0, 0 );
195        HeapFree( GetProcessHeap(), 0, lphc );
196    }
197    return 0;
198 }
199
200 /***********************************************************************
201  *           CBGetTextAreaHeight
202  *
203  * This method will calculate the height of the text area of the
204  * combobox.
205  * The height of the text area is set in two ways.
206  * It can be set explicitly through a combobox message or through a
207  * WM_MEASUREITEM callback.
208  * If this is not the case, the height is set to font height + 4px
209  * This height was determined through experimentation.
210  * CBCalcPlacement will add 2*COMBO_YBORDERSIZE pixels for the border
211  */
212 static INT CBGetTextAreaHeight(
213   HWND        hwnd,
214   LPHEADCOMBO lphc)
215 {
216   INT iTextItemHeight;
217
218   if( lphc->editHeight ) /* explicitly set height */
219   {
220     iTextItemHeight = lphc->editHeight;
221   }
222   else
223   {
224     TEXTMETRICW tm;
225     HDC         hDC       = GetDC(hwnd);
226     HFONT       hPrevFont = 0;
227     INT         baseUnitY;
228
229     if (lphc->hFont)
230       hPrevFont = SelectObject( hDC, lphc->hFont );
231
232     GetTextMetricsW(hDC, &tm);
233
234     baseUnitY = tm.tmHeight;
235
236     if( hPrevFont )
237       SelectObject( hDC, hPrevFont );
238
239     ReleaseDC(hwnd, hDC);
240
241     iTextItemHeight = baseUnitY + 4;
242   }
243
244   /*
245    * Check the ownerdraw case if we haven't asked the parent the size
246    * of the item yet.
247    */
248   if ( CB_OWNERDRAWN(lphc) &&
249        (lphc->wState & CBF_MEASUREITEM) )
250   {
251     MEASUREITEMSTRUCT measureItem;
252     RECT              clientRect;
253     INT               originalItemHeight = iTextItemHeight;
254     UINT id = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
255
256     /*
257      * We use the client rect for the width of the item.
258      */
259     GetClientRect(hwnd, &clientRect);
260
261     lphc->wState &= ~CBF_MEASUREITEM;
262
263     /*
264      * Send a first one to measure the size of the text area
265      */
266     measureItem.CtlType    = ODT_COMBOBOX;
267     measureItem.CtlID      = id;
268     measureItem.itemID     = -1;
269     measureItem.itemWidth  = clientRect.right;
270     measureItem.itemHeight = iTextItemHeight - 6; /* ownerdrawn cb is taller */
271     measureItem.itemData   = 0;
272     SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
273     iTextItemHeight = 6 + measureItem.itemHeight;
274
275     /*
276      * Send a second one in the case of a fixed ownerdraw list to calculate the
277      * size of the list items. (we basically do this on behalf of the listbox)
278      */
279     if (lphc->dwStyle & CBS_OWNERDRAWFIXED)
280     {
281       measureItem.CtlType    = ODT_COMBOBOX;
282       measureItem.CtlID      = id;
283       measureItem.itemID     = 0;
284       measureItem.itemWidth  = clientRect.right;
285       measureItem.itemHeight = originalItemHeight;
286       measureItem.itemData   = 0;
287       SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
288       lphc->fixedOwnerDrawHeight = measureItem.itemHeight;
289     }
290
291     /*
292      * Keep the size for the next time
293      */
294     lphc->editHeight = iTextItemHeight;
295   }
296
297   return iTextItemHeight;
298 }
299
300 /***********************************************************************
301  *           CBForceDummyResize
302  *
303  * The dummy resize is used for listboxes that have a popup to trigger
304  * a re-arranging of the contents of the combobox and the recalculation
305  * of the size of the "real" control window.
306  */
307 static void CBForceDummyResize(
308   LPHEADCOMBO lphc)
309 {
310   RECT windowRect;
311   int newComboHeight;
312
313   newComboHeight = CBGetTextAreaHeight(lphc->self,lphc) + 2*COMBO_YBORDERSIZE();
314
315   GetWindowRect(lphc->self, &windowRect);
316
317   /*
318    * We have to be careful, resizing a combobox also has the meaning that the
319    * dropped rect will be resized. In this case, we want to trigger a resize
320    * to recalculate layout but we don't want to change the dropped rectangle
321    * So, we pass the height of text area of control as the height.
322    * this will cancel-out in the processing of the WM_WINDOWPOSCHANGING
323    * message.
324    */
325   SetWindowPos( lphc->self,
326                 NULL,
327                 0, 0,
328                 windowRect.right  - windowRect.left,
329                 newComboHeight,
330                 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
331 }
332
333 /***********************************************************************
334  *           CBCalcPlacement
335  *
336  * Set up component coordinates given valid lphc->RectCombo.
337  */
338 static void CBCalcPlacement(
339   HWND        hwnd,
340   LPHEADCOMBO lphc,
341   LPRECT      lprEdit,
342   LPRECT      lprButton,
343   LPRECT      lprLB)
344 {
345   /*
346    * Again, start with the client rectangle.
347    */
348   GetClientRect(hwnd, lprEdit);
349
350   /*
351    * Remove the borders
352    */
353   InflateRect(lprEdit, -COMBO_XBORDERSIZE(), -COMBO_YBORDERSIZE());
354
355   /*
356    * Chop off the bottom part to fit with the height of the text area.
357    */
358   lprEdit->bottom = lprEdit->top + CBGetTextAreaHeight(hwnd, lphc);
359
360   /*
361    * The button starts the same vertical position as the text area.
362    */
363   CopyRect(lprButton, lprEdit);
364
365   /*
366    * If the combobox is "simple" there is no button.
367    */
368   if( CB_GETTYPE(lphc) == CBS_SIMPLE )
369     lprButton->left = lprButton->right = lprButton->bottom = 0;
370   else
371   {
372     /*
373      * Let's assume the combobox button is the same width as the
374      * scrollbar button.
375      * size the button horizontally and cut-off the text area.
376      */
377     lprButton->left = lprButton->right - GetSystemMetrics(SM_CXVSCROLL);
378     lprEdit->right  = lprButton->left;
379   }
380
381   /*
382    * In the case of a dropdown, there is an additional spacing between the
383    * text area and the button.
384    */
385   if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
386   {
387     lprEdit->right -= COMBO_EDITBUTTONSPACE();
388   }
389
390   /*
391    * If we have an edit control, we space it away from the borders slightly.
392    */
393   if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
394   {
395     InflateRect(lprEdit, -EDIT_CONTROL_PADDING(), -EDIT_CONTROL_PADDING());
396   }
397
398   /*
399    * Adjust the size of the listbox popup.
400    */
401   if( CB_GETTYPE(lphc) == CBS_SIMPLE )
402   {
403     /*
404      * Use the client rectangle to initialize the listbox rectangle
405      */
406     GetClientRect(hwnd, lprLB);
407
408     /*
409      * Then, chop-off the top part.
410      */
411     lprLB->top = lprEdit->bottom + COMBO_YBORDERSIZE();
412   }
413   else
414   {
415     /*
416      * Make sure the dropped width is as large as the combobox itself.
417      */
418     if (lphc->droppedWidth < (lprButton->right + COMBO_XBORDERSIZE()))
419     {
420       lprLB->right  = lprLB->left + (lprButton->right + COMBO_XBORDERSIZE());
421
422       /*
423        * In the case of a dropdown, the popup listbox is offset to the right.
424        * so, we want to make sure it's flush with the right side of the
425        * combobox
426        */
427       if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
428         lprLB->right -= COMBO_EDITBUTTONSPACE();
429     }
430     else
431        lprLB->right = lprLB->left + lphc->droppedWidth;
432   }
433
434   /* don't allow negative window width */
435   if (lprEdit->right < lprEdit->left)
436     lprEdit->right = lprEdit->left;
437
438   TRACE("\ttext\t= (%s)\n", wine_dbgstr_rect(lprEdit));
439
440   TRACE("\tbutton\t= (%s)\n", wine_dbgstr_rect(lprButton));
441
442   TRACE("\tlbox\t= (%s)\n", wine_dbgstr_rect(lprLB));
443 }
444
445 /***********************************************************************
446  *           CBGetDroppedControlRect
447  */
448 static void CBGetDroppedControlRect( LPHEADCOMBO lphc, LPRECT lpRect)
449 {
450     /* In windows, CB_GETDROPPEDCONTROLRECT returns the upper left corner
451      of the combo box and the lower right corner of the listbox */
452
453     GetWindowRect(lphc->self, lpRect);
454
455     lpRect->right =  lpRect->left + lphc->droppedRect.right - lphc->droppedRect.left;
456     lpRect->bottom = lpRect->top + lphc->droppedRect.bottom - lphc->droppedRect.top;
457
458 }
459
460 /***********************************************************************
461  *           COMBO_WindowPosChanging
462  */
463 static LRESULT COMBO_WindowPosChanging(
464   HWND        hwnd,
465   LPHEADCOMBO lphc,
466   WINDOWPOS*  posChanging)
467 {
468   /*
469    * We need to override the WM_WINDOWPOSCHANGING method to handle all
470    * the non-simple comboboxes. The problem is that those controls are
471    * always the same height. We have to make sure they are not resized
472    * to another value.
473    */
474   if ( ( CB_GETTYPE(lphc) != CBS_SIMPLE ) &&
475        ((posChanging->flags & SWP_NOSIZE) == 0) )
476   {
477     int newComboHeight;
478
479     newComboHeight = CBGetTextAreaHeight(hwnd,lphc) +
480                       2*COMBO_YBORDERSIZE();
481
482     /*
483      * Resizing a combobox has another side effect, it resizes the dropped
484      * rectangle as well. However, it does it only if the new height for the
485      * combobox is different from the height it should have. In other words,
486      * if the application resizing the combobox only had the intention to resize
487      * the actual control, for example, to do the layout of a dialog that is
488      * resized, the height of the dropdown is not changed.
489      */
490     if (posChanging->cy != newComboHeight)
491     {
492         TRACE("posChanging->cy=%d, newComboHeight=%d, oldbot=%d, oldtop=%d\n",
493               posChanging->cy, newComboHeight, lphc->droppedRect.bottom,
494               lphc->droppedRect.top);
495       lphc->droppedRect.bottom = lphc->droppedRect.top + posChanging->cy - newComboHeight;
496
497       posChanging->cy = newComboHeight;
498     }
499   }
500
501   return 0;
502 }
503
504 /***********************************************************************
505  *           COMBO_Create
506  */
507 static LRESULT COMBO_Create( HWND hwnd, LPHEADCOMBO lphc, HWND hwndParent, LONG style,
508                              BOOL unicode )
509 {
510   static const WCHAR clbName[] = {'C','o','m','b','o','L','B','o','x',0};
511   static const WCHAR editName[] = {'E','d','i','t',0};
512
513   if( !CB_GETTYPE(lphc) ) lphc->dwStyle |= CBS_SIMPLE;
514   if( CB_GETTYPE(lphc) != CBS_DROPDOWNLIST ) lphc->wState |= CBF_EDIT;
515
516   lphc->owner = hwndParent;
517
518   /*
519    * The item height and dropped width are not set when the control
520    * is created.
521    */
522   lphc->droppedWidth = lphc->editHeight = 0;
523
524   /*
525    * The first time we go through, we want to measure the ownerdraw item
526    */
527   lphc->wState |= CBF_MEASUREITEM;
528
529   /* M$ IE 3.01 actually creates (and rapidly destroys) an ownerless combobox */
530
531   if( lphc->owner || !(style & WS_VISIBLE) )
532   {
533       UINT lbeStyle   = 0;
534       UINT lbeExStyle = 0;
535
536       /*
537        * Initialize the dropped rect to the size of the client area of the
538        * control and then, force all the areas of the combobox to be
539        * recalculated.
540        */
541       GetClientRect( hwnd, &lphc->droppedRect );
542       CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );
543
544       /*
545        * Adjust the position of the popup listbox if it's necessary
546        */
547       if ( CB_GETTYPE(lphc) != CBS_SIMPLE )
548       {
549         lphc->droppedRect.top   = lphc->textRect.bottom + COMBO_YBORDERSIZE();
550
551         /*
552          * If it's a dropdown, the listbox is offset
553          */
554         if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
555           lphc->droppedRect.left += COMBO_EDITBUTTONSPACE();
556
557         if (lphc->droppedRect.bottom < lphc->droppedRect.top)
558             lphc->droppedRect.bottom = lphc->droppedRect.top;
559         if (lphc->droppedRect.right < lphc->droppedRect.left)
560             lphc->droppedRect.right = lphc->droppedRect.left;
561         MapWindowPoints( hwnd, 0, (LPPOINT)&lphc->droppedRect, 2 );
562       }
563
564       /* create listbox popup */
565
566       lbeStyle = (LBS_NOTIFY | LBS_COMBOBOX | WS_BORDER | WS_CLIPSIBLINGS | WS_CHILD) |
567                  (style & (WS_VSCROLL | CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE));
568
569       if( lphc->dwStyle & CBS_SORT )
570         lbeStyle |= LBS_SORT;
571       if( lphc->dwStyle & CBS_HASSTRINGS )
572         lbeStyle |= LBS_HASSTRINGS;
573       if( lphc->dwStyle & CBS_NOINTEGRALHEIGHT )
574         lbeStyle |= LBS_NOINTEGRALHEIGHT;
575       if( lphc->dwStyle & CBS_DISABLENOSCROLL )
576         lbeStyle |= LBS_DISABLENOSCROLL;
577
578       if( CB_GETTYPE(lphc) == CBS_SIMPLE )      /* child listbox */
579       {
580         lbeStyle |= WS_VISIBLE;
581
582         /*
583          * In win 95 look n feel, the listbox in the simple combobox has
584          * the WS_EXCLIENTEDGE style instead of the WS_BORDER style.
585          */
586         lbeStyle   &= ~WS_BORDER;
587         lbeExStyle |= WS_EX_CLIENTEDGE;
588       }
589       else
590       {
591         lbeExStyle |= (WS_EX_TOPMOST | WS_EX_TOOLWINDOW);
592       }
593
594       if (unicode)
595           lphc->hWndLBox = CreateWindowExW(lbeExStyle, clbName, NULL, lbeStyle,
596                                            lphc->droppedRect.left,
597                                            lphc->droppedRect.top,
598                                            lphc->droppedRect.right - lphc->droppedRect.left,
599                                            lphc->droppedRect.bottom - lphc->droppedRect.top,
600                                            hwnd, (HMENU)ID_CB_LISTBOX,
601                                            (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
602       else
603           lphc->hWndLBox = CreateWindowExA(lbeExStyle, "ComboLBox", NULL, lbeStyle,
604                                            lphc->droppedRect.left,
605                                            lphc->droppedRect.top,
606                                            lphc->droppedRect.right - lphc->droppedRect.left,
607                                            lphc->droppedRect.bottom - lphc->droppedRect.top,
608                                            hwnd, (HMENU)ID_CB_LISTBOX,
609                                            (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
610
611       if( lphc->hWndLBox )
612       {
613           BOOL  bEdit = TRUE;
614           lbeStyle = WS_CHILD | WS_VISIBLE | ES_NOHIDESEL | ES_LEFT | ES_COMBO;
615
616           if( lphc->wState & CBF_EDIT )
617           {
618               if( lphc->dwStyle & CBS_OEMCONVERT )
619                   lbeStyle |= ES_OEMCONVERT;
620               if( lphc->dwStyle & CBS_AUTOHSCROLL )
621                   lbeStyle |= ES_AUTOHSCROLL;
622               if( lphc->dwStyle & CBS_LOWERCASE )
623                   lbeStyle |= ES_LOWERCASE;
624               else if( lphc->dwStyle & CBS_UPPERCASE )
625                   lbeStyle |= ES_UPPERCASE;
626
627               if (!IsWindowEnabled(hwnd)) lbeStyle |= WS_DISABLED;
628
629               if (unicode)
630                   lphc->hWndEdit = CreateWindowExW(0, editName, NULL, lbeStyle,
631                                                    lphc->textRect.left, lphc->textRect.top,
632                                                    lphc->textRect.right - lphc->textRect.left,
633                                                    lphc->textRect.bottom - lphc->textRect.top,
634                                                    hwnd, (HMENU)ID_CB_EDIT,
635                                                    (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
636               else
637                   lphc->hWndEdit = CreateWindowExA(0, "Edit", NULL, lbeStyle,
638                                                    lphc->textRect.left, lphc->textRect.top,
639                                                    lphc->textRect.right - lphc->textRect.left,
640                                                    lphc->textRect.bottom - lphc->textRect.top,
641                                                    hwnd, (HMENU)ID_CB_EDIT,
642                                                    (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
643
644               if( !lphc->hWndEdit )
645                 bEdit = FALSE;
646           }
647
648           if( bEdit )
649           {
650             if( CB_GETTYPE(lphc) != CBS_SIMPLE )
651             {
652               /* Now do the trick with parent */
653               SetParent(lphc->hWndLBox, HWND_DESKTOP);
654               /*
655                * If the combo is a dropdown, we must resize the control
656                * to fit only the text area and button. To do this,
657                * we send a dummy resize and the WM_WINDOWPOSCHANGING message
658                * will take care of setting the height for us.
659                */
660               CBForceDummyResize(lphc);
661             }
662
663             TRACE("init done\n");
664             return 0;
665           }
666           ERR("edit control failure.\n");
667       } else ERR("listbox failure.\n");
668   } else ERR("no owner for visible combo.\n");
669
670   /* CreateWindow() will send WM_NCDESTROY to cleanup */
671
672   return -1;
673 }
674
675 /***********************************************************************
676  *           CBPaintButton
677  *
678  * Paint combo button (normal, pressed, and disabled states).
679  */
680 static void CBPaintButton( LPHEADCOMBO lphc, HDC hdc, RECT rectButton)
681 {
682     UINT buttonState = DFCS_SCROLLCOMBOBOX;
683
684     if( lphc->wState & CBF_NOREDRAW )
685       return;
686
687
688     if (lphc->wState & CBF_BUTTONDOWN)
689         buttonState |= DFCS_PUSHED;
690
691     if (CB_DISABLED(lphc))
692         buttonState |= DFCS_INACTIVE;
693
694     DrawFrameControl(hdc, &rectButton, DFC_SCROLL, buttonState);
695 }
696
697 /***********************************************************************
698  *           CBPaintText
699  *
700  * Paint CBS_DROPDOWNLIST text field / update edit control contents.
701  */
702 static void CBPaintText(
703   LPHEADCOMBO lphc,
704   HDC         hdc,
705   RECT        rectEdit)
706 {
707    INT  id, size = 0;
708    LPWSTR pText = NULL;
709
710    if( lphc->wState & CBF_NOREDRAW ) return;
711
712    TRACE("\n");
713
714    /* follow Windows combobox that sends a bunch of text
715     * inquiries to its listbox while processing WM_PAINT. */
716
717    if( (id = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0) ) != LB_ERR )
718    {
719         size = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, id, 0);
720         if (size == LB_ERR)
721           FIXME("LB_ERR probably not handled yet\n");
722         if( (pText = HeapAlloc( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))) )
723         {
724             /* size from LB_GETTEXTLEN may be too large, from LB_GETTEXT is accurate */
725             size=SendMessageW(lphc->hWndLBox, LB_GETTEXT, (WPARAM)id, (LPARAM)pText);
726             pText[size] = '\0'; /* just in case */
727         } else return;
728    }
729    else
730        if( !CB_OWNERDRAWN(lphc) )
731            return;
732
733    if( lphc->wState & CBF_EDIT )
734    {
735         static const WCHAR empty_stringW[] = { 0 };
736         if( CB_HASSTRINGS(lphc) ) SetWindowTextW( lphc->hWndEdit, pText ? pText : empty_stringW );
737         if( lphc->wState & CBF_FOCUSED )
738             SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
739    }
740    else /* paint text field ourselves */
741    {
742      UINT       itemState = ODS_COMBOBOXEDIT;
743      HFONT      hPrevFont = (lphc->hFont) ? SelectObject(hdc, lphc->hFont) : 0;
744
745      /*
746       * Give ourselves some space.
747       */
748      InflateRect( &rectEdit, -1, -1 );
749
750      if( CB_OWNERDRAWN(lphc) )
751      {
752        DRAWITEMSTRUCT dis;
753        HRGN           clipRegion;
754        UINT ctlid = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
755
756        /* setup state for DRAWITEM message. Owner will highlight */
757        if ( (lphc->wState & CBF_FOCUSED) &&
758             !(lphc->wState & CBF_DROPPED) )
759            itemState |= ODS_SELECTED | ODS_FOCUS;
760
761        /*
762         * Save the current clip region.
763         * To retrieve the clip region, we need to create one "dummy"
764         * clip region.
765         */
766        clipRegion = CreateRectRgnIndirect(&rectEdit);
767
768        if (GetClipRgn(hdc, clipRegion)!=1)
769        {
770          DeleteObject(clipRegion);
771          clipRegion=NULL;
772        }
773
774        if (!IsWindowEnabled(lphc->self)) itemState |= ODS_DISABLED;
775
776        dis.CtlType      = ODT_COMBOBOX;
777        dis.CtlID        = ctlid;
778        dis.hwndItem     = lphc->self;
779        dis.itemAction   = ODA_DRAWENTIRE;
780        dis.itemID       = id;
781        dis.itemState    = itemState;
782        dis.hDC          = hdc;
783        dis.rcItem       = rectEdit;
784        dis.itemData     = SendMessageW(lphc->hWndLBox, LB_GETITEMDATA,
785                                         (WPARAM)id, 0 );
786
787        /*
788         * Clip the DC and have the parent draw the item.
789         */
790        IntersectClipRect(hdc,
791                          rectEdit.left,  rectEdit.top,
792                          rectEdit.right, rectEdit.bottom);
793
794        SendMessageW(lphc->owner, WM_DRAWITEM, ctlid, (LPARAM)&dis );
795
796        /*
797         * Reset the clipping region.
798         */
799        SelectClipRgn(hdc, clipRegion);
800      }
801      else
802      {
803        static const WCHAR empty_stringW[] = { 0 };
804
805        if ( (lphc->wState & CBF_FOCUSED) &&
806             !(lphc->wState & CBF_DROPPED) ) {
807
808            /* highlight */
809            FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
810            SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
811            SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
812        }
813
814        ExtTextOutW( hdc,
815                     rectEdit.left + 1,
816                     rectEdit.top + 1,
817                     ETO_OPAQUE | ETO_CLIPPED,
818                     &rectEdit,
819                     pText ? pText : empty_stringW , size, NULL );
820
821        if(lphc->wState & CBF_FOCUSED && !(lphc->wState & CBF_DROPPED))
822          DrawFocusRect( hdc, &rectEdit );
823      }
824
825      if( hPrevFont )
826        SelectObject(hdc, hPrevFont );
827    }
828    HeapFree( GetProcessHeap(), 0, pText );
829 }
830
831 /***********************************************************************
832  *           CBPaintBorder
833  */
834 static void CBPaintBorder(
835   HWND            hwnd,
836   const HEADCOMBO *lphc,
837   HDC             hdc)
838 {
839   RECT clientRect;
840
841   if (CB_GETTYPE(lphc) != CBS_SIMPLE)
842   {
843     GetClientRect(hwnd, &clientRect);
844   }
845   else
846   {
847     CopyRect(&clientRect, &lphc->textRect);
848
849     InflateRect(&clientRect, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
850     InflateRect(&clientRect, COMBO_XBORDERSIZE(), COMBO_YBORDERSIZE());
851   }
852
853   DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_RECT);
854 }
855
856 /***********************************************************************
857  *           COMBO_PrepareColors
858  *
859  * This method will sent the appropriate WM_CTLCOLOR message to
860  * prepare and setup the colors for the combo's DC.
861  *
862  * It also returns the brush to use for the background.
863  */
864 static HBRUSH COMBO_PrepareColors(
865   LPHEADCOMBO lphc,
866   HDC         hDC)
867 {
868   HBRUSH  hBkgBrush;
869
870   /*
871    * Get the background brush for this control.
872    */
873   if (CB_DISABLED(lphc))
874   {
875     hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORSTATIC,
876                                      (WPARAM)hDC, (LPARAM)lphc->self );
877
878     /*
879      * We have to change the text color since WM_CTLCOLORSTATIC will
880      * set it to the "enabled" color. This is the same behavior as the
881      * edit control
882      */
883     SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
884   }
885   else
886   {
887     if (lphc->wState & CBF_EDIT)
888     {
889       hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLOREDIT,
890                                        (WPARAM)hDC, (LPARAM)lphc->self );
891     }
892     else
893     {
894       hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORLISTBOX,
895                                        (WPARAM)hDC, (LPARAM)lphc->self );
896     }
897   }
898
899   /*
900    * Catch errors.
901    */
902   if( !hBkgBrush )
903     hBkgBrush = GetSysColorBrush(COLOR_WINDOW);
904
905   return hBkgBrush;
906 }
907
908
909 /***********************************************************************
910  *           COMBO_Paint
911  */
912 static LRESULT COMBO_Paint(LPHEADCOMBO lphc, HDC hParamDC)
913 {
914   PAINTSTRUCT ps;
915   HDC   hDC;
916
917   hDC = (hParamDC) ? hParamDC
918                    : BeginPaint( lphc->self, &ps);
919
920   TRACE("hdc=%p\n", hDC);
921
922   if( hDC && !(lphc->wState & CBF_NOREDRAW) )
923   {
924       HBRUSH    hPrevBrush, hBkgBrush;
925
926       /*
927        * Retrieve the background brush and select it in the
928        * DC.
929        */
930       hBkgBrush = COMBO_PrepareColors(lphc, hDC);
931
932       hPrevBrush = SelectObject( hDC, hBkgBrush );
933       if (!(lphc->wState & CBF_EDIT))
934         FillRect(hDC, &lphc->textRect, hBkgBrush);
935
936       /*
937        * In non 3.1 look, there is a sunken border on the combobox
938        */
939       CBPaintBorder(lphc->self, lphc, hDC);
940
941       if( !IsRectEmpty(&lphc->buttonRect) )
942       {
943         CBPaintButton(lphc, hDC, lphc->buttonRect);
944       }
945
946       /* paint the edit control padding area */
947       if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
948       {
949           RECT rPadEdit = lphc->textRect;
950
951           InflateRect(&rPadEdit, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
952
953           FrameRect( hDC, &rPadEdit, GetSysColorBrush(COLOR_WINDOW) );
954       }
955
956       if( !(lphc->wState & CBF_EDIT) )
957         CBPaintText( lphc, hDC, lphc->textRect);
958
959       if( hPrevBrush )
960         SelectObject( hDC, hPrevBrush );
961   }
962
963   if( !hParamDC )
964     EndPaint(lphc->self, &ps);
965
966   return 0;
967 }
968
969 /***********************************************************************
970  *           CBUpdateLBox
971  *
972  * Select listbox entry according to the contents of the edit control.
973  */
974 static INT CBUpdateLBox( LPHEADCOMBO lphc, BOOL bSelect )
975 {
976    INT  length, idx;
977    LPWSTR pText = NULL;
978
979    idx = LB_ERR;
980    length = SendMessageW( lphc->hWndEdit, WM_GETTEXTLENGTH, 0, 0 );
981
982    if( length > 0 )
983        pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
984
985    TRACE("\t edit text length %i\n", length );
986
987    if( pText )
988    {
989        GetWindowTextW( lphc->hWndEdit, pText, length + 1);
990        idx = SendMessageW(lphc->hWndLBox, LB_FINDSTRING,
991                              (WPARAM)(-1), (LPARAM)pText );
992        HeapFree( GetProcessHeap(), 0, pText );
993    }
994
995    SendMessageW(lphc->hWndLBox, LB_SETCURSEL, (WPARAM)(bSelect ? idx : -1), 0);
996
997    /* probably superfluous but Windows sends this too */
998    SendMessageW(lphc->hWndLBox, LB_SETCARETINDEX, (WPARAM)(idx < 0 ? 0 : idx), 0);
999    SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, (WPARAM)(idx < 0 ? 0 : idx), 0);
1000
1001    return idx;
1002 }
1003
1004 /***********************************************************************
1005  *           CBUpdateEdit
1006  *
1007  * Copy a listbox entry to the edit control.
1008  */
1009 static void CBUpdateEdit( LPHEADCOMBO lphc , INT index )
1010 {
1011    INT  length;
1012    LPWSTR pText = NULL;
1013    static const WCHAR empty_stringW[] = { 0 };
1014
1015    TRACE("\t %i\n", index );
1016
1017    if( index >= 0 ) /* got an entry */
1018    {
1019        length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, (WPARAM)index, 0);
1020        if( length != LB_ERR)
1021        {
1022            if( (pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR))) )
1023            {
1024                 SendMessageW(lphc->hWndLBox, LB_GETTEXT,
1025                                 (WPARAM)index, (LPARAM)pText );
1026            }
1027        }
1028    }
1029
1030    if( CB_HASSTRINGS(lphc) )
1031    {
1032       lphc->wState |= (CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
1033       SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, pText ? (LPARAM)pText : (LPARAM)empty_stringW);
1034       lphc->wState &= ~(CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
1035    }
1036
1037    if( lphc->wState & CBF_FOCUSED )
1038       SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
1039
1040    HeapFree( GetProcessHeap(), 0, pText );
1041 }
1042
1043 /***********************************************************************
1044  *           CBDropDown
1045  *
1046  * Show listbox popup.
1047  */
1048 static void CBDropDown( LPHEADCOMBO lphc )
1049 {
1050     HMONITOR monitor;
1051     MONITORINFO mon_info;
1052    RECT rect,r;
1053    int nItems = 0;
1054    int nDroppedHeight;
1055
1056    TRACE("[%p]: drop down\n", lphc->self);
1057
1058    CB_NOTIFY( lphc, CBN_DROPDOWN );
1059
1060    /* set selection */
1061
1062    lphc->wState |= CBF_DROPPED;
1063    if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1064    {
1065        lphc->droppedIndex = CBUpdateLBox( lphc, TRUE );
1066
1067        /* Update edit only if item is in the list */
1068        if( !(lphc->wState & CBF_CAPTURE) && lphc->droppedIndex >= 0)
1069          CBUpdateEdit( lphc, lphc->droppedIndex );
1070    }
1071    else
1072    {
1073        lphc->droppedIndex = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1074
1075        SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX,
1076                      (WPARAM)(lphc->droppedIndex == LB_ERR ? 0 : lphc->droppedIndex), 0 );
1077        SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1078    }
1079
1080    /* now set popup position */
1081    GetWindowRect( lphc->self, &rect );
1082
1083    /*
1084     * If it's a dropdown, the listbox is offset
1085     */
1086    if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1087      rect.left += COMBO_EDITBUTTONSPACE();
1088
1089   /* if the dropped height is greater than the total height of the dropped
1090      items list, then force the drop down list height to be the total height
1091      of the items in the dropped list */
1092
1093   /* And Remove any extra space (Best Fit) */
1094    nDroppedHeight = lphc->droppedRect.bottom - lphc->droppedRect.top;
1095   /* if listbox length has been set directly by its handle */
1096    GetWindowRect(lphc->hWndLBox, &r);
1097    if (nDroppedHeight < r.bottom - r.top)
1098        nDroppedHeight = r.bottom - r.top;
1099    nItems = (int)SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
1100
1101    if (nItems > 0)
1102    {
1103       int nHeight;
1104       int nIHeight;
1105
1106       nIHeight = (int)SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, 0, 0);
1107
1108       nHeight = nIHeight*nItems;
1109
1110       if (nHeight < nDroppedHeight - COMBO_YBORDERSIZE())
1111          nDroppedHeight = nHeight + COMBO_YBORDERSIZE();
1112
1113       if (nDroppedHeight < nHeight)
1114       {
1115             if (nItems < 5)
1116                 nDroppedHeight = (nItems+1)*nIHeight;
1117             else
1118                 nDroppedHeight = 6*nIHeight;
1119       }
1120    }
1121
1122    /*If height of dropped rectangle gets beyond a screen size it should go up, otherwise down.*/
1123    monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
1124    mon_info.cbSize = sizeof(mon_info);
1125    GetMonitorInfoW( monitor, &mon_info );
1126
1127    if( (rect.bottom + nDroppedHeight) >= mon_info.rcWork.bottom )
1128       rect.bottom = rect.top - nDroppedHeight;
1129
1130    SetWindowPos( lphc->hWndLBox, HWND_TOP, rect.left, rect.bottom,
1131                  lphc->droppedRect.right - lphc->droppedRect.left,
1132                  nDroppedHeight,
1133                  SWP_NOACTIVATE | SWP_SHOWWINDOW);
1134
1135
1136    if( !(lphc->wState & CBF_NOREDRAW) )
1137      RedrawWindow( lphc->self, NULL, 0, RDW_INVALIDATE |
1138                            RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1139
1140    EnableWindow( lphc->hWndLBox, TRUE );
1141    if (GetCapture() != lphc->self)
1142       SetCapture(lphc->hWndLBox);
1143 }
1144
1145 /***********************************************************************
1146  *           CBRollUp
1147  *
1148  * Hide listbox popup.
1149  */
1150 static void CBRollUp( LPHEADCOMBO lphc, BOOL ok, BOOL bButton )
1151 {
1152    HWND hWnd = lphc->self;
1153
1154    TRACE("[%p]: sel ok? [%i] dropped? [%i]\n",
1155          lphc->self, ok, (INT)(lphc->wState & CBF_DROPPED));
1156
1157    CB_NOTIFY( lphc, (ok) ? CBN_SELENDOK : CBN_SELENDCANCEL );
1158
1159    if( IsWindow( hWnd ) && CB_GETTYPE(lphc) != CBS_SIMPLE )
1160    {
1161
1162        if( lphc->wState & CBF_DROPPED )
1163        {
1164            RECT rect;
1165
1166            lphc->wState &= ~CBF_DROPPED;
1167            ShowWindow( lphc->hWndLBox, SW_HIDE );
1168
1169            if(GetCapture() == lphc->hWndLBox)
1170            {
1171                ReleaseCapture();
1172            }
1173
1174            if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1175            {
1176                rect = lphc->buttonRect;
1177            }
1178            else
1179            {
1180                if( bButton )
1181                {
1182                  UnionRect( &rect,
1183                             &lphc->buttonRect,
1184                             &lphc->textRect);
1185                }
1186                else
1187                  rect = lphc->textRect;
1188
1189                bButton = TRUE;
1190            }
1191
1192            if( bButton && !(lphc->wState & CBF_NOREDRAW) )
1193                RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE |
1194                                RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1195            CB_NOTIFY( lphc, CBN_CLOSEUP );
1196        }
1197    }
1198 }
1199
1200 /***********************************************************************
1201  *           COMBO_FlipListbox
1202  *
1203  * Used by the ComboLBox to show/hide itself in response to VK_F4, etc...
1204  */
1205 BOOL COMBO_FlipListbox( LPHEADCOMBO lphc, BOOL ok, BOOL bRedrawButton )
1206 {
1207    if( lphc->wState & CBF_DROPPED )
1208    {
1209        CBRollUp( lphc, ok, bRedrawButton );
1210        return FALSE;
1211    }
1212
1213    CBDropDown( lphc );
1214    return TRUE;
1215 }
1216
1217 /***********************************************************************
1218  *           CBRepaintButton
1219  */
1220 static void CBRepaintButton( LPHEADCOMBO lphc )
1221    {
1222   InvalidateRect(lphc->self, &lphc->buttonRect, TRUE);
1223   UpdateWindow(lphc->self);
1224 }
1225
1226 /***********************************************************************
1227  *           COMBO_SetFocus
1228  */
1229 static void COMBO_SetFocus( LPHEADCOMBO lphc )
1230 {
1231    if( !(lphc->wState & CBF_FOCUSED) )
1232    {
1233        if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1234            SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1235
1236        /* This is wrong. Message sequences seem to indicate that this
1237           is set *after* the notify. */
1238        /* lphc->wState |= CBF_FOCUSED;  */
1239
1240        if( !(lphc->wState & CBF_EDIT) )
1241          InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1242
1243        CB_NOTIFY( lphc, CBN_SETFOCUS );
1244        lphc->wState |= CBF_FOCUSED;
1245    }
1246 }
1247
1248 /***********************************************************************
1249  *           COMBO_KillFocus
1250  */
1251 static void COMBO_KillFocus( LPHEADCOMBO lphc )
1252 {
1253    HWND hWnd = lphc->self;
1254
1255    if( lphc->wState & CBF_FOCUSED )
1256    {
1257        CBRollUp( lphc, FALSE, TRUE );
1258        if( IsWindow( hWnd ) )
1259        {
1260            if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1261                SendMessageW(lphc->hWndLBox, LB_CARETOFF, 0, 0);
1262
1263            lphc->wState &= ~CBF_FOCUSED;
1264
1265            /* redraw text */
1266            if( !(lphc->wState & CBF_EDIT) )
1267              InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1268
1269            CB_NOTIFY( lphc, CBN_KILLFOCUS );
1270        }
1271    }
1272 }
1273
1274 /***********************************************************************
1275  *           COMBO_Command
1276  */
1277 static LRESULT COMBO_Command( LPHEADCOMBO lphc, WPARAM wParam, HWND hWnd )
1278 {
1279    if ( lphc->wState & CBF_EDIT && lphc->hWndEdit == hWnd )
1280    {
1281        /* ">> 8" makes gcc generate jump-table instead of cmp ladder */
1282
1283        switch( HIWORD(wParam) >> 8 )
1284        {
1285            case (EN_SETFOCUS >> 8):
1286
1287                TRACE("[%p]: edit [%p] got focus\n", lphc->self, lphc->hWndEdit );
1288
1289                 COMBO_SetFocus( lphc );
1290                 break;
1291
1292            case (EN_KILLFOCUS >> 8):
1293
1294                TRACE("[%p]: edit [%p] lost focus\n", lphc->self, lphc->hWndEdit );
1295
1296                 /* NOTE: it seems that Windows' edit control sends an
1297                  * undocumented message WM_USER + 0x1B instead of this
1298                  * notification (only when it happens to be a part of
1299                  * the combo). ?? - AK.
1300                  */
1301
1302                 COMBO_KillFocus( lphc );
1303                 break;
1304
1305
1306            case (EN_CHANGE >> 8):
1307                /*
1308                 * In some circumstances (when the selection of the combobox
1309                 * is changed for example) we don't want the EN_CHANGE notification
1310                 * to be forwarded to the parent of the combobox. This code
1311                 * checks a flag that is set in these occasions and ignores the
1312                 * notification.
1313                 */
1314                 if (lphc->wState & CBF_NOLBSELECT)
1315                 {
1316                   lphc->wState &= ~CBF_NOLBSELECT;
1317                 }
1318                 else
1319                 {
1320                   CBUpdateLBox( lphc, lphc->wState & CBF_DROPPED );
1321                 }
1322
1323                 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1324                   CB_NOTIFY( lphc, CBN_EDITCHANGE );
1325                 break;
1326
1327            case (EN_UPDATE >> 8):
1328                 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1329                   CB_NOTIFY( lphc, CBN_EDITUPDATE );
1330                 break;
1331
1332            case (EN_ERRSPACE >> 8):
1333                 CB_NOTIFY( lphc, CBN_ERRSPACE );
1334        }
1335    }
1336    else if( lphc->hWndLBox == hWnd )
1337    {
1338        switch( (short)HIWORD(wParam) )
1339        {
1340            case LBN_ERRSPACE:
1341                 CB_NOTIFY( lphc, CBN_ERRSPACE );
1342                 break;
1343
1344            case LBN_DBLCLK:
1345                 CB_NOTIFY( lphc, CBN_DBLCLK );
1346                 break;
1347
1348            case LBN_SELCHANGE:
1349            case LBN_SELCANCEL:
1350
1351                 TRACE("[%p]: lbox selection change [%x]\n", lphc->self, lphc->wState );
1352
1353                 CB_NOTIFY( lphc, CBN_SELCHANGE );
1354
1355                 if( HIWORD(wParam) == LBN_SELCHANGE)
1356                 {
1357                    if( lphc->wState & CBF_EDIT )
1358                    {
1359                        INT index = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1360                        lphc->wState |= CBF_NOLBSELECT;
1361                        CBUpdateEdit( lphc, index );
1362                        /* select text in edit, as Windows does */
1363                        SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
1364                    }
1365                    else
1366                        InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1367                 }
1368
1369                 /* do not roll up if selection is being tracked
1370                  * by arrowkeys in the dropdown listbox */
1371                 if( ((lphc->wState & CBF_DROPPED) && !(lphc->wState & CBF_NOROLLUP)) )
1372                 {
1373                    CBRollUp( lphc, (HIWORD(wParam) == LBN_SELCHANGE), TRUE );
1374                 }
1375                 else lphc->wState &= ~CBF_NOROLLUP;
1376
1377                 break;
1378
1379            case LBN_SETFOCUS:
1380            case LBN_KILLFOCUS:
1381                 /* nothing to do here since ComboLBox always resets the focus to its
1382                  * combo/edit counterpart */
1383                  break;
1384        }
1385    }
1386    return 0;
1387 }
1388
1389 /***********************************************************************
1390  *           COMBO_ItemOp
1391  *
1392  * Fixup an ownerdrawn item operation and pass it up to the combobox owner.
1393  */
1394 static LRESULT COMBO_ItemOp( LPHEADCOMBO lphc, UINT msg, LPARAM lParam )
1395 {
1396    HWND hWnd = lphc->self;
1397    UINT id = (UINT)GetWindowLongPtrW( hWnd, GWLP_ID );
1398
1399    TRACE("[%p]: ownerdraw op %04x\n", lphc->self, msg );
1400
1401    switch( msg )
1402    {
1403    case WM_DELETEITEM:
1404        {
1405            DELETEITEMSTRUCT *lpIS = (DELETEITEMSTRUCT *)lParam;
1406            lpIS->CtlType  = ODT_COMBOBOX;
1407            lpIS->CtlID    = id;
1408            lpIS->hwndItem = hWnd;
1409            break;
1410        }
1411    case WM_DRAWITEM:
1412        {
1413            DRAWITEMSTRUCT *lpIS = (DRAWITEMSTRUCT *)lParam;
1414            lpIS->CtlType  = ODT_COMBOBOX;
1415            lpIS->CtlID    = id;
1416            lpIS->hwndItem = hWnd;
1417            break;
1418        }
1419    case WM_COMPAREITEM:
1420        {
1421            COMPAREITEMSTRUCT *lpIS = (COMPAREITEMSTRUCT *)lParam;
1422            lpIS->CtlType  = ODT_COMBOBOX;
1423            lpIS->CtlID    = id;
1424            lpIS->hwndItem = hWnd;
1425            break;
1426        }
1427    case WM_MEASUREITEM:
1428        {
1429            MEASUREITEMSTRUCT *lpIS = (MEASUREITEMSTRUCT *)lParam;
1430            lpIS->CtlType  = ODT_COMBOBOX;
1431            lpIS->CtlID    = id;
1432            break;
1433        }
1434    }
1435    return SendMessageW(lphc->owner, msg, id, lParam);
1436 }
1437
1438
1439 /***********************************************************************
1440  *           COMBO_GetTextW
1441  */
1442 static LRESULT COMBO_GetTextW( LPHEADCOMBO lphc, INT count, LPWSTR buf )
1443 {
1444     INT length;
1445
1446     if( lphc->wState & CBF_EDIT )
1447         return SendMessageW( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1448
1449     /* get it from the listbox */
1450
1451     if (!count || !buf) return 0;
1452     if( lphc->hWndLBox )
1453     {
1454         INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1455         if (idx == LB_ERR) goto error;
1456         length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1457         if (length == LB_ERR) goto error;
1458
1459         /* 'length' is without the terminating character */
1460         if (length >= count)
1461         {
1462             LPWSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
1463             if (!lpBuffer) goto error;
1464             length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1465
1466             /* truncate if buffer is too short */
1467             if (length != LB_ERR)
1468             {
1469                 lstrcpynW( buf, lpBuffer, count );
1470                 length = count;
1471             }
1472             HeapFree( GetProcessHeap(), 0, lpBuffer );
1473         }
1474         else length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1475
1476         if (length == LB_ERR) return 0;
1477         return length;
1478     }
1479
1480  error:  /* error - truncate string, return zero */
1481     buf[0] = 0;
1482     return 0;
1483 }
1484
1485
1486 /***********************************************************************
1487  *           COMBO_GetTextA
1488  *
1489  * NOTE! LB_GETTEXT does not count terminating \0, WM_GETTEXT does.
1490  *       also LB_GETTEXT might return values < 0, WM_GETTEXT doesn't.
1491  */
1492 static LRESULT COMBO_GetTextA( LPHEADCOMBO lphc, INT count, LPSTR buf )
1493 {
1494     INT length;
1495
1496     if( lphc->wState & CBF_EDIT )
1497         return SendMessageA( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1498
1499     /* get it from the listbox */
1500
1501     if (!count || !buf) return 0;
1502     if( lphc->hWndLBox )
1503     {
1504         INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1505         if (idx == LB_ERR) goto error;
1506         length = SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1507         if (length == LB_ERR) goto error;
1508
1509         /* 'length' is without the terminating character */
1510         if (length >= count)
1511         {
1512             LPSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) );
1513             if (!lpBuffer) goto error;
1514             length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1515
1516             /* truncate if buffer is too short */
1517             if (length != LB_ERR)
1518             {
1519                 lstrcpynA( buf, lpBuffer, count );
1520                 length = count;
1521             }
1522             HeapFree( GetProcessHeap(), 0, lpBuffer );
1523         }
1524         else length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1525
1526         if (length == LB_ERR) return 0;
1527         return length;
1528     }
1529
1530  error:  /* error - truncate string, return zero */
1531     buf[0] = 0;
1532     return 0;
1533 }
1534
1535
1536 /***********************************************************************
1537  *           CBResetPos
1538  *
1539  * This function sets window positions according to the updated
1540  * component placement struct.
1541  */
1542 static void CBResetPos(
1543   LPHEADCOMBO lphc,
1544   const RECT  *rectEdit,
1545   const RECT  *rectLB,
1546   BOOL        bRedraw)
1547 {
1548    BOOL bDrop = (CB_GETTYPE(lphc) != CBS_SIMPLE);
1549
1550    /* NOTE: logs sometimes have WM_LBUTTONUP before a cascade of
1551     * sizing messages */
1552
1553    if( lphc->wState & CBF_EDIT )
1554      SetWindowPos( lphc->hWndEdit, 0,
1555                    rectEdit->left, rectEdit->top,
1556                    rectEdit->right - rectEdit->left,
1557                    rectEdit->bottom - rectEdit->top,
1558                        SWP_NOZORDER | SWP_NOACTIVATE | ((bDrop) ? SWP_NOREDRAW : 0) );
1559
1560    SetWindowPos( lphc->hWndLBox, 0,
1561                  rectLB->left, rectLB->top,
1562                  rectLB->right - rectLB->left,
1563                  rectLB->bottom - rectLB->top,
1564                    SWP_NOACTIVATE | SWP_NOZORDER | ((bDrop) ? SWP_NOREDRAW : 0) );
1565
1566    if( bDrop )
1567    {
1568        if( lphc->wState & CBF_DROPPED )
1569        {
1570            lphc->wState &= ~CBF_DROPPED;
1571            ShowWindow( lphc->hWndLBox, SW_HIDE );
1572        }
1573
1574        if( bRedraw && !(lphc->wState & CBF_NOREDRAW) )
1575            RedrawWindow( lphc->self, NULL, 0,
1576                            RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
1577    }
1578 }
1579
1580
1581 /***********************************************************************
1582  *           COMBO_Size
1583  */
1584 static void COMBO_Size( LPHEADCOMBO lphc, BOOL bRedraw )
1585   {
1586   CBCalcPlacement(lphc->self,
1587                   lphc,
1588                   &lphc->textRect,
1589                   &lphc->buttonRect,
1590                   &lphc->droppedRect);
1591
1592   CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, bRedraw );
1593 }
1594
1595
1596 /***********************************************************************
1597  *           COMBO_Font
1598  */
1599 static void COMBO_Font( LPHEADCOMBO lphc, HFONT hFont, BOOL bRedraw )
1600 {
1601   /*
1602    * Set the font
1603    */
1604   lphc->hFont = hFont;
1605
1606   /*
1607    * Propagate to owned windows.
1608    */
1609   if( lphc->wState & CBF_EDIT )
1610       SendMessageW(lphc->hWndEdit, WM_SETFONT, (WPARAM)hFont, bRedraw);
1611   SendMessageW(lphc->hWndLBox, WM_SETFONT, (WPARAM)hFont, bRedraw);
1612
1613   /*
1614    * Redo the layout of the control.
1615    */
1616   if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1617   {
1618     CBCalcPlacement(lphc->self,
1619                     lphc,
1620                     &lphc->textRect,
1621                     &lphc->buttonRect,
1622                     &lphc->droppedRect);
1623
1624     CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1625   }
1626   else
1627   {
1628     CBForceDummyResize(lphc);
1629   }
1630 }
1631
1632
1633 /***********************************************************************
1634  *           COMBO_SetItemHeight
1635  */
1636 static LRESULT COMBO_SetItemHeight( LPHEADCOMBO lphc, INT index, INT height )
1637 {
1638    LRESULT      lRet = CB_ERR;
1639
1640    if( index == -1 ) /* set text field height */
1641    {
1642        if( height < 32768 )
1643        {
1644            lphc->editHeight = height + 2;  /* Is the 2 for 2*EDIT_CONTROL_PADDING? */
1645
1646          /*
1647           * Redo the layout of the control.
1648           */
1649          if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1650          {
1651            CBCalcPlacement(lphc->self,
1652                            lphc,
1653                            &lphc->textRect,
1654                            &lphc->buttonRect,
1655                            &lphc->droppedRect);
1656
1657            CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1658          }
1659          else
1660          {
1661            CBForceDummyResize(lphc);
1662          }
1663
1664            lRet = height;
1665        }
1666    }
1667    else if ( CB_OWNERDRAWN(lphc) )      /* set listbox item height */
1668         lRet = SendMessageW(lphc->hWndLBox, LB_SETITEMHEIGHT,
1669                               (WPARAM)index, (LPARAM)height );
1670    return lRet;
1671 }
1672
1673 /***********************************************************************
1674  *           COMBO_SelectString
1675  */
1676 static LRESULT COMBO_SelectString( LPHEADCOMBO lphc, INT start, LPARAM pText, BOOL unicode )
1677 {
1678    INT index = unicode ? SendMessageW(lphc->hWndLBox, LB_SELECTSTRING, (WPARAM)start, pText) :
1679                          SendMessageA(lphc->hWndLBox, LB_SELECTSTRING, (WPARAM)start, pText);
1680    if( index >= 0 )
1681    {
1682      if( lphc->wState & CBF_EDIT )
1683        CBUpdateEdit( lphc, index );
1684      else
1685      {
1686        InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1687      }
1688    }
1689    return (LRESULT)index;
1690 }
1691
1692 /***********************************************************************
1693  *           COMBO_LButtonDown
1694  */
1695 static void COMBO_LButtonDown( LPHEADCOMBO lphc, LPARAM lParam )
1696 {
1697    POINT     pt;
1698    BOOL      bButton;
1699    HWND      hWnd = lphc->self;
1700
1701    pt.x = (short)LOWORD(lParam);
1702    pt.y = (short)HIWORD(lParam);
1703    bButton = PtInRect(&lphc->buttonRect, pt);
1704
1705    if( (CB_GETTYPE(lphc) == CBS_DROPDOWNLIST) ||
1706        (bButton && (CB_GETTYPE(lphc) == CBS_DROPDOWN)) )
1707    {
1708        lphc->wState |= CBF_BUTTONDOWN;
1709        if( lphc->wState & CBF_DROPPED )
1710        {
1711            /* got a click to cancel selection */
1712
1713            lphc->wState &= ~CBF_BUTTONDOWN;
1714            CBRollUp( lphc, TRUE, FALSE );
1715            if( !IsWindow( hWnd ) ) return;
1716
1717            if( lphc->wState & CBF_CAPTURE )
1718            {
1719                lphc->wState &= ~CBF_CAPTURE;
1720                ReleaseCapture();
1721            }
1722        }
1723        else
1724        {
1725            /* drop down the listbox and start tracking */
1726
1727            lphc->wState |= CBF_CAPTURE;
1728            SetCapture( hWnd );
1729            CBDropDown( lphc );
1730        }
1731        if( bButton ) CBRepaintButton( lphc );
1732    }
1733 }
1734
1735 /***********************************************************************
1736  *           COMBO_LButtonUp
1737  *
1738  * Release capture and stop tracking if needed.
1739  */
1740 static void COMBO_LButtonUp( LPHEADCOMBO lphc )
1741 {
1742    if( lphc->wState & CBF_CAPTURE )
1743    {
1744        lphc->wState &= ~CBF_CAPTURE;
1745        if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1746        {
1747            INT index = CBUpdateLBox( lphc, TRUE );
1748            /* Update edit only if item is in the list */
1749            if(index >= 0)
1750            {
1751                lphc->wState |= CBF_NOLBSELECT;
1752                CBUpdateEdit( lphc, index );
1753                lphc->wState &= ~CBF_NOLBSELECT;
1754            }
1755        }
1756        ReleaseCapture();
1757        SetCapture(lphc->hWndLBox);
1758    }
1759
1760    if( lphc->wState & CBF_BUTTONDOWN )
1761    {
1762        lphc->wState &= ~CBF_BUTTONDOWN;
1763        CBRepaintButton( lphc );
1764    }
1765 }
1766
1767 /***********************************************************************
1768  *           COMBO_MouseMove
1769  *
1770  * Two things to do - track combo button and release capture when
1771  * pointer goes into the listbox.
1772  */
1773 static void COMBO_MouseMove( LPHEADCOMBO lphc, WPARAM wParam, LPARAM lParam )
1774 {
1775    POINT  pt;
1776    RECT   lbRect;
1777
1778    pt.x = (short)LOWORD(lParam);
1779    pt.y = (short)HIWORD(lParam);
1780
1781    if( lphc->wState & CBF_BUTTONDOWN )
1782    {
1783      BOOL bButton;
1784
1785      bButton = PtInRect(&lphc->buttonRect, pt);
1786
1787      if( !bButton )
1788      {
1789        lphc->wState &= ~CBF_BUTTONDOWN;
1790        CBRepaintButton( lphc );
1791      }
1792    }
1793
1794    GetClientRect( lphc->hWndLBox, &lbRect );
1795    MapWindowPoints( lphc->self, lphc->hWndLBox, &pt, 1 );
1796    if( PtInRect(&lbRect, pt) )
1797    {
1798        lphc->wState &= ~CBF_CAPTURE;
1799        ReleaseCapture();
1800        if( CB_GETTYPE(lphc) == CBS_DROPDOWN ) CBUpdateLBox( lphc, TRUE );
1801
1802        /* hand over pointer tracking */
1803        SendMessageW(lphc->hWndLBox, WM_LBUTTONDOWN, wParam, lParam);
1804    }
1805 }
1806
1807 static LRESULT COMBO_GetComboBoxInfo(const HEADCOMBO *lphc, COMBOBOXINFO *pcbi)
1808 {
1809     if (!pcbi || (pcbi->cbSize < sizeof(COMBOBOXINFO)))
1810         return FALSE;
1811
1812     pcbi->rcItem = lphc->textRect;
1813     pcbi->rcButton = lphc->buttonRect;
1814     pcbi->stateButton = 0;
1815     if (lphc->wState & CBF_BUTTONDOWN)
1816         pcbi->stateButton |= STATE_SYSTEM_PRESSED;
1817     if (IsRectEmpty(&lphc->buttonRect))
1818         pcbi->stateButton |= STATE_SYSTEM_INVISIBLE;
1819     pcbi->hwndCombo = lphc->self;
1820     pcbi->hwndItem = lphc->hWndEdit;
1821     pcbi->hwndList = lphc->hWndLBox;
1822     return TRUE;
1823 }
1824
1825 static char *strdupA(LPCSTR str)
1826 {
1827     char *ret;
1828     DWORD len;
1829
1830     if(!str) return NULL;
1831
1832     len = strlen(str);
1833     ret = HeapAlloc(GetProcessHeap(), 0, len + 1);
1834     memcpy(ret, str, len + 1);
1835     return ret;
1836 }
1837
1838 /***********************************************************************
1839  *           ComboWndProc_common
1840  */
1841 static LRESULT ComboWndProc_common( HWND hwnd, UINT message,
1842                                     WPARAM wParam, LPARAM lParam, BOOL unicode )
1843 {
1844       LPHEADCOMBO lphc = (LPHEADCOMBO)GetWindowLongPtrW( hwnd, 0 );
1845
1846       TRACE("[%p]: msg %s wp %08lx lp %08lx\n",
1847             hwnd, SPY_GetMsgName(message, hwnd), wParam, lParam );
1848
1849       if( lphc || message == WM_NCCREATE )
1850       switch(message)
1851       {
1852
1853         /* System messages */
1854
1855         case WM_NCCREATE:
1856         {
1857                 LONG style = unicode ? ((LPCREATESTRUCTW)lParam)->style :
1858                                        ((LPCREATESTRUCTA)lParam)->style;
1859                 return COMBO_NCCreate(hwnd, style);
1860         }
1861         case WM_NCDESTROY:
1862                 COMBO_NCDestroy(lphc);
1863                 break;/* -> DefWindowProc */
1864
1865         case WM_CREATE:
1866         {
1867                 HWND hwndParent;
1868                 LONG style;
1869                 if(unicode)
1870                 {
1871                     hwndParent = ((LPCREATESTRUCTW)lParam)->hwndParent;
1872                     style = ((LPCREATESTRUCTW)lParam)->style;
1873                 }
1874                 else
1875                 {
1876                     hwndParent = ((LPCREATESTRUCTA)lParam)->hwndParent;
1877                     style = ((LPCREATESTRUCTA)lParam)->style;
1878                 }
1879                 return COMBO_Create(hwnd, lphc, hwndParent, style, unicode);
1880         }
1881
1882         case WM_PRINTCLIENT:
1883                 /* Fallthrough */
1884         case WM_PAINT:
1885                 /* wParam may contain a valid HDC! */
1886                 return  COMBO_Paint(lphc, (HDC)wParam);
1887
1888         case WM_ERASEBKGND:
1889                 /* do all painting in WM_PAINT like Windows does */
1890                 return 1;
1891
1892         case WM_GETDLGCODE:
1893         {
1894                 LRESULT result = DLGC_WANTARROWS | DLGC_WANTCHARS;
1895                 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1896                 {
1897                    int vk = (int)((LPMSG)lParam)->wParam;
1898
1899                    if ((vk == VK_RETURN || vk == VK_ESCAPE) && (lphc->wState & CBF_DROPPED))
1900                        result |= DLGC_WANTMESSAGE;
1901                 }
1902                 return  result;
1903         }
1904         case WM_WINDOWPOSCHANGING:
1905                 return  COMBO_WindowPosChanging(hwnd, lphc, (LPWINDOWPOS)lParam);
1906     case WM_WINDOWPOSCHANGED:
1907         /* SetWindowPos can be called on a Combobox to resize its Listbox.
1908          * In that case, the Combobox itself will not be resized, so we won't
1909          * get a WM_SIZE. Since we still want to update the Listbox, we have to
1910          * do it here.
1911          */
1912         /* we should not force repainting on WM_WINDOWPOSCHANGED, it breaks
1913          * Z-order based painting.
1914          */
1915         /* fall through */
1916         case WM_SIZE:
1917                 if( lphc->hWndLBox &&
1918                   !(lphc->wState & CBF_NORESIZE) ) COMBO_Size( lphc, message == WM_SIZE );
1919                 return  TRUE;
1920         case WM_SETFONT:
1921                 COMBO_Font( lphc, (HFONT)wParam, (BOOL)lParam );
1922                 return  TRUE;
1923         case WM_GETFONT:
1924                 return  (LRESULT)lphc->hFont;
1925         case WM_SETFOCUS:
1926                 if( lphc->wState & CBF_EDIT )
1927                     SetFocus( lphc->hWndEdit );
1928                 else
1929                     COMBO_SetFocus( lphc );
1930                 return  TRUE;
1931         case WM_KILLFOCUS:
1932             {
1933                 HWND hwndFocus = WIN_GetFullHandle( (HWND)wParam );
1934                 if( !hwndFocus ||
1935                     (hwndFocus != lphc->hWndEdit && hwndFocus != lphc->hWndLBox ))
1936                     COMBO_KillFocus( lphc );
1937                 return  TRUE;
1938             }
1939         case WM_COMMAND:
1940                 return  COMBO_Command( lphc, wParam, WIN_GetFullHandle( (HWND)lParam ) );
1941         case WM_GETTEXT:
1942             return unicode ? COMBO_GetTextW( lphc, wParam, (LPWSTR)lParam )
1943                            : COMBO_GetTextA( lphc, wParam, (LPSTR)lParam );
1944         case WM_SETTEXT:
1945         case WM_GETTEXTLENGTH:
1946         case WM_CLEAR:
1947                 if ((message == WM_GETTEXTLENGTH) && !ISWIN31 && !(lphc->wState & CBF_EDIT))
1948                 {
1949                     int j = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1950                     if (j == -1) return 0;
1951                     return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, j, 0) :
1952                                      SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, j, 0);
1953                 }
1954                 else if( lphc->wState & CBF_EDIT )
1955                 {
1956                     LRESULT ret;
1957                     lphc->wState |= CBF_NOEDITNOTIFY;
1958                     ret = unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1959                                     SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1960                     lphc->wState &= ~CBF_NOEDITNOTIFY;
1961                     return ret;
1962                 }
1963                 else return CB_ERR;
1964         case WM_CUT:
1965         case WM_PASTE:
1966         case WM_COPY:
1967                 if( lphc->wState & CBF_EDIT )
1968                 {
1969                     return unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1970                                      SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1971                 }
1972                 else return  CB_ERR;
1973
1974         case WM_DRAWITEM:
1975         case WM_DELETEITEM:
1976         case WM_COMPAREITEM:
1977         case WM_MEASUREITEM:
1978                 return COMBO_ItemOp(lphc, message, lParam);
1979         case WM_ENABLE:
1980                 if( lphc->wState & CBF_EDIT )
1981                     EnableWindow( lphc->hWndEdit, (BOOL)wParam );
1982                 EnableWindow( lphc->hWndLBox, (BOOL)wParam );
1983
1984                 /* Force the control to repaint when the enabled state changes. */
1985                 InvalidateRect(lphc->self, NULL, TRUE);
1986                 return  TRUE;
1987         case WM_SETREDRAW:
1988                 if( wParam )
1989                     lphc->wState &= ~CBF_NOREDRAW;
1990                 else
1991                     lphc->wState |= CBF_NOREDRAW;
1992
1993                 if( lphc->wState & CBF_EDIT )
1994                     SendMessageW(lphc->hWndEdit, message, wParam, lParam);
1995                 SendMessageW(lphc->hWndLBox, message, wParam, lParam);
1996                 return  0;
1997         case WM_SYSKEYDOWN:
1998                 if( KEYDATA_ALT & HIWORD(lParam) )
1999                     if( wParam == VK_UP || wParam == VK_DOWN )
2000                         COMBO_FlipListbox( lphc, FALSE, FALSE );
2001                 return  0;
2002
2003         case WM_CHAR:
2004         case WM_IME_CHAR:
2005         case WM_KEYDOWN:
2006         {
2007                 HWND hwndTarget;
2008
2009                 if ((wParam == VK_RETURN || wParam == VK_ESCAPE) &&
2010                      (lphc->wState & CBF_DROPPED))
2011                 {
2012                    CBRollUp( lphc, wParam == VK_RETURN, FALSE );
2013                    return TRUE;
2014                 }
2015                else if ((wParam == VK_F4) && !(lphc->wState & CBF_EUI))
2016                {
2017                   COMBO_FlipListbox( lphc, FALSE, FALSE );
2018                   return TRUE;
2019                }
2020
2021                 if( lphc->wState & CBF_EDIT )
2022                     hwndTarget = lphc->hWndEdit;
2023                 else
2024                     hwndTarget = lphc->hWndLBox;
2025
2026                 return unicode ? SendMessageW(hwndTarget, message, wParam, lParam) :
2027                                  SendMessageA(hwndTarget, message, wParam, lParam);
2028         }
2029         case WM_LBUTTONDOWN:
2030                 if( !(lphc->wState & CBF_FOCUSED) ) SetFocus( lphc->self );
2031                 if( lphc->wState & CBF_FOCUSED ) COMBO_LButtonDown( lphc, lParam );
2032                 return  TRUE;
2033         case WM_LBUTTONUP:
2034                 COMBO_LButtonUp( lphc );
2035                 return  TRUE;
2036         case WM_MOUSEMOVE:
2037                 if( lphc->wState & CBF_CAPTURE )
2038                     COMBO_MouseMove( lphc, wParam, lParam );
2039                 return  TRUE;
2040
2041         case WM_MOUSEWHEEL:
2042                 if (wParam & (MK_SHIFT | MK_CONTROL))
2043                     return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2044                                      DefWindowProcA(hwnd, message, wParam, lParam);
2045
2046                 if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_UP, 0);
2047                 if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_DOWN, 0);
2048                 return TRUE;
2049
2050         /* Combo messages */
2051
2052         case CB_ADDSTRING16:
2053                 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2054                 /* fall through */
2055         case CB_ADDSTRING:
2056                 if( unicode )
2057                 {
2058                     if( lphc->dwStyle & CBS_LOWERCASE )
2059                         CharLowerW((LPWSTR)lParam);
2060                     else if( lphc->dwStyle & CBS_UPPERCASE )
2061                         CharUpperW((LPWSTR)lParam);
2062                     return SendMessageW(lphc->hWndLBox, LB_ADDSTRING, 0, lParam);
2063                 }
2064                 else /* unlike the unicode version, the ansi version does not overwrite
2065                         the string if converting case */
2066                 {
2067                     char *string = NULL;
2068                     LRESULT ret;
2069                     if( lphc->dwStyle & CBS_LOWERCASE )
2070                     {
2071                         string = strdupA((LPSTR)lParam);
2072                         CharLowerA(string);
2073                     }
2074
2075                     else if( lphc->dwStyle & CBS_UPPERCASE )
2076                     {
2077                         string = strdupA((LPSTR)lParam);
2078                         CharUpperA(string);
2079                     }
2080
2081                     ret = SendMessageA(lphc->hWndLBox, LB_ADDSTRING, 0, string ? (LPARAM)string : lParam);
2082                     HeapFree(GetProcessHeap(), 0, string);
2083                     return ret;
2084                 }
2085         case CB_INSERTSTRING16:
2086                 wParam = (INT)(INT16)wParam;
2087                 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2088                 /* fall through */
2089         case CB_INSERTSTRING:
2090                 if( unicode )
2091                 {
2092                     if( lphc->dwStyle & CBS_LOWERCASE )
2093                         CharLowerW((LPWSTR)lParam);
2094                     else if( lphc->dwStyle & CBS_UPPERCASE )
2095                         CharUpperW((LPWSTR)lParam);
2096                     return SendMessageW(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2097                 }
2098                 else
2099                 {
2100                     if( lphc->dwStyle & CBS_LOWERCASE )
2101                         CharLowerA((LPSTR)lParam);
2102                     else if( lphc->dwStyle & CBS_UPPERCASE )
2103                         CharUpperA((LPSTR)lParam);
2104
2105                     return SendMessageA(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2106                 }
2107         case CB_DELETESTRING16:
2108         case CB_DELETESTRING:
2109                 return unicode ? SendMessageW(lphc->hWndLBox, LB_DELETESTRING, wParam, 0) :
2110                                  SendMessageA(lphc->hWndLBox, LB_DELETESTRING, wParam, 0);
2111         case CB_SELECTSTRING16:
2112                 wParam = (INT)(INT16)wParam;
2113                 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2114                 /* fall through */
2115         case CB_SELECTSTRING:
2116                 return COMBO_SelectString(lphc, (INT)wParam, lParam, unicode);
2117         case CB_FINDSTRING16:
2118                 wParam = (INT)(INT16)wParam;
2119                 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2120                 /* fall through */
2121         case CB_FINDSTRING:
2122                 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam) :
2123                                  SendMessageA(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam);
2124         case CB_FINDSTRINGEXACT16:
2125                 wParam = (INT)(INT16)wParam;
2126                 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2127                 /* fall through */
2128         case CB_FINDSTRINGEXACT:
2129                 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam) :
2130                                  SendMessageA(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam);
2131         case CB_SETITEMHEIGHT16:
2132                 wParam = (INT)(INT16)wParam;    /* signed integer */
2133                 /* fall through */
2134         case CB_SETITEMHEIGHT:
2135                 return  COMBO_SetItemHeight( lphc, (INT)wParam, (INT)lParam);
2136         case CB_GETITEMHEIGHT16:
2137                 wParam = (INT)(INT16)wParam;
2138                 /* fall through */
2139         case CB_GETITEMHEIGHT:
2140                 if( (INT)wParam >= 0 )  /* listbox item */
2141                     return SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, wParam, 0);
2142                 return  CBGetTextAreaHeight(hwnd, lphc);
2143         case CB_RESETCONTENT16:
2144         case CB_RESETCONTENT:
2145                 SendMessageW(lphc->hWndLBox, LB_RESETCONTENT, 0, 0);
2146                 if( (lphc->wState & CBF_EDIT) && CB_HASSTRINGS(lphc) )
2147                 {
2148                     static const WCHAR empty_stringW[] = { 0 };
2149                     SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, (LPARAM)empty_stringW);
2150                 }
2151                 else
2152                     InvalidateRect(lphc->self, NULL, TRUE);
2153                 return  TRUE;
2154         case CB_INITSTORAGE:
2155                 return SendMessageW(lphc->hWndLBox, LB_INITSTORAGE, wParam, lParam);
2156         case CB_GETHORIZONTALEXTENT:
2157                 return SendMessageW(lphc->hWndLBox, LB_GETHORIZONTALEXTENT, 0, 0);
2158         case CB_SETHORIZONTALEXTENT:
2159                 return SendMessageW(lphc->hWndLBox, LB_SETHORIZONTALEXTENT, wParam, 0);
2160         case CB_GETTOPINDEX:
2161                 return SendMessageW(lphc->hWndLBox, LB_GETTOPINDEX, 0, 0);
2162         case CB_GETLOCALE:
2163                 return SendMessageW(lphc->hWndLBox, LB_GETLOCALE, 0, 0);
2164         case CB_SETLOCALE:
2165                 return SendMessageW(lphc->hWndLBox, LB_SETLOCALE, wParam, 0);
2166         case CB_GETDROPPEDWIDTH:
2167                 if( lphc->droppedWidth )
2168                     return  lphc->droppedWidth;
2169                 return  lphc->droppedRect.right - lphc->droppedRect.left;
2170         case CB_SETDROPPEDWIDTH:
2171                 if( (CB_GETTYPE(lphc) != CBS_SIMPLE) &&
2172                     (INT)wParam < 32768 ) lphc->droppedWidth = (INT)wParam;
2173                 return  CB_ERR;
2174         case CB_GETDROPPEDCONTROLRECT16:
2175                 lParam = (LPARAM)MapSL(lParam);
2176                 if( lParam )
2177                 {
2178                     RECT r;
2179                     RECT16 *r16 = (RECT16 *)lParam;
2180                     CBGetDroppedControlRect( lphc, &r );
2181                     r16->left   = r.left;
2182                     r16->top    = r.top;
2183                     r16->right  = r.right;
2184                     r16->bottom = r.bottom;
2185                 }
2186                 return  CB_OKAY;
2187         case CB_GETDROPPEDCONTROLRECT:
2188                 if( lParam ) CBGetDroppedControlRect(lphc, (LPRECT)lParam );
2189                 return  CB_OKAY;
2190         case CB_GETDROPPEDSTATE16:
2191         case CB_GETDROPPEDSTATE:
2192                 return  (lphc->wState & CBF_DROPPED) ? TRUE : FALSE;
2193         case CB_DIR16:
2194                 return SendMessageA(lphc->hWndLBox, LB_DIR16, wParam, lParam);
2195         case CB_DIR:
2196                 return unicode ? SendMessageW(lphc->hWndLBox, LB_DIR, wParam, lParam) :
2197                                  SendMessageA(lphc->hWndLBox, LB_DIR, wParam, lParam);
2198
2199         case CB_SHOWDROPDOWN16:
2200         case CB_SHOWDROPDOWN:
2201                 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
2202                 {
2203                     if( wParam )
2204                     {
2205                         if( !(lphc->wState & CBF_DROPPED) )
2206                             CBDropDown( lphc );
2207                     }
2208                     else
2209                         if( lphc->wState & CBF_DROPPED )
2210                             CBRollUp( lphc, FALSE, TRUE );
2211                 }
2212                 return  TRUE;
2213         case CB_GETCOUNT16:
2214         case CB_GETCOUNT:
2215                 return SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
2216         case CB_GETCURSEL16:
2217         case CB_GETCURSEL:
2218                 return SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
2219         case CB_SETCURSEL16:
2220                 wParam = (INT)(INT16)wParam;
2221                 /* fall through */
2222         case CB_SETCURSEL:
2223                 lParam = SendMessageW(lphc->hWndLBox, LB_SETCURSEL, wParam, 0);
2224                 if( lParam >= 0 )
2225                     SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, wParam, 0);
2226
2227                 /* no LBN_SELCHANGE in this case, update manually */
2228                 if( lphc->wState & CBF_EDIT )
2229                     CBUpdateEdit( lphc, (INT)wParam );
2230                 else
2231                     InvalidateRect(lphc->self, &lphc->textRect, TRUE);
2232                 lphc->wState &= ~CBF_SELCHANGE;
2233                 return  lParam;
2234         case CB_GETLBTEXT16:
2235                 wParam = (INT)(INT16)wParam;
2236                 lParam = (LPARAM)MapSL(lParam);
2237                 /* fall through */
2238         case CB_GETLBTEXT:
2239                 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXT, wParam, lParam) :
2240                                  SendMessageA(lphc->hWndLBox, LB_GETTEXT, wParam, lParam);
2241         case CB_GETLBTEXTLEN16:
2242                 wParam = (INT)(INT16)wParam;
2243                 /* fall through */
2244         case CB_GETLBTEXTLEN:
2245                 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0) :
2246                                  SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0);
2247         case CB_GETITEMDATA16:
2248                 wParam = (INT)(INT16)wParam;
2249                 /* fall through */
2250         case CB_GETITEMDATA:
2251                 return SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, wParam, 0);
2252         case CB_SETITEMDATA16:
2253                 wParam = (INT)(INT16)wParam;
2254                 /* fall through */
2255         case CB_SETITEMDATA:
2256                 return SendMessageW(lphc->hWndLBox, LB_SETITEMDATA, wParam, lParam);
2257         case CB_GETEDITSEL16:
2258                 wParam = lParam = 0;   /* just in case */
2259                 /* fall through */
2260         case CB_GETEDITSEL:
2261                 /* Edit checks passed parameters itself */
2262                 if( lphc->wState & CBF_EDIT )
2263                     return SendMessageW(lphc->hWndEdit, EM_GETSEL, wParam, lParam);
2264                 return  CB_ERR;
2265         case CB_SETEDITSEL16:
2266         case CB_SETEDITSEL:
2267                 if( lphc->wState & CBF_EDIT )
2268                     return SendMessageW(lphc->hWndEdit, EM_SETSEL,
2269                           (INT)(INT16)LOWORD(lParam), (INT)(INT16)HIWORD(lParam) );
2270                 return  CB_ERR;
2271         case CB_SETEXTENDEDUI16:
2272         case CB_SETEXTENDEDUI:
2273                 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
2274                     return  CB_ERR;
2275                 if( wParam )
2276                     lphc->wState |= CBF_EUI;
2277                 else lphc->wState &= ~CBF_EUI;
2278                 return  CB_OKAY;
2279         case CB_GETEXTENDEDUI16:
2280         case CB_GETEXTENDEDUI:
2281                 return  (lphc->wState & CBF_EUI) ? TRUE : FALSE;
2282         case CB_GETCOMBOBOXINFO:
2283                 return COMBO_GetComboBoxInfo(lphc, (COMBOBOXINFO *)lParam);
2284         case CB_LIMITTEXT:
2285                 if( lphc->wState & CBF_EDIT )
2286                         return SendMessageW(lphc->hWndEdit, EM_LIMITTEXT, wParam, lParam);
2287         default:
2288                 if (message >= WM_USER)
2289                     WARN("unknown msg WM_USER+%04x wp=%04lx lp=%08lx\n",
2290                         message - WM_USER, wParam, lParam );
2291                 break;
2292       }
2293       return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2294                        DefWindowProcA(hwnd, message, wParam, lParam);
2295 }
2296
2297 /***********************************************************************
2298  *           ComboWndProcA
2299  *
2300  * This is just a wrapper for the real ComboWndProc which locks/unlocks
2301  * window structs.
2302  */
2303 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
2304 {
2305     if (!IsWindow(hwnd)) return 0;
2306     return ComboWndProc_common( hwnd, message, wParam, lParam, FALSE );
2307 }
2308
2309 /***********************************************************************
2310  *           ComboWndProcW
2311  */
2312 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
2313 {
2314     if (!IsWindow(hwnd)) return 0;
2315     return ComboWndProc_common( hwnd, message, wParam, lParam, TRUE );
2316 }
2317
2318 /*************************************************************************
2319  *           GetComboBoxInfo   (USER32.@)
2320  */
2321 BOOL WINAPI GetComboBoxInfo(HWND hwndCombo,      /* [in] handle to combo box */
2322                             PCOMBOBOXINFO pcbi   /* [in/out] combo box information */)
2323 {
2324     TRACE("(%p, %p)\n", hwndCombo, pcbi);
2325     return SendMessageW(hwndCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)pcbi);
2326 }