combo.c 64.1 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2
 * Combo controls
3
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1997 Alex Korobka
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 *
20 21 22 23 24 25 26 27 28 29 30 31 32
 * NOTES
 *
 * This code was audited for completeness against the documented features
 * of Comctl32.dll version 6.0 on Oct. 4, 2004, by Dimitrie O. Paun.
 * 
 * Unless otherwise noted, we believe this code to be complete, as per
 * the specification mentioned above.
 * If you discover missing features, or bugs, please note them below.
 * 
 * TODO:
 *   - ComboBox_[GS]etMinVisible()
 *   - CB_GETMINVISIBLE, CB_SETMINVISIBLE
 *   - CB_SETTOPINDEX
Alexandre Julliard's avatar
Alexandre Julliard committed
33
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
34

35
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
36
#include <string.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
37

38 39
#define OEMRESOURCE

40
#include "windef.h"
41
#include "winbase.h"
42
#include "wingdi.h"
43
#include "winuser.h"
44
#include "wine/unicode.h"
45
#include "user_private.h"
46
#include "win.h"
47
#include "controls.h"
48
#include "winternl.h"
49
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
50

51
WINE_DEFAULT_DEBUG_CHANNEL(combo);
52

Alexandre Julliard's avatar
Alexandre Julliard committed
53 54 55
  /* bits in the dwKeyData */
#define KEYDATA_ALT             0x2000
#define KEYDATA_PREVSTATE       0x4000
Alexandre Julliard's avatar
Alexandre Julliard committed
56

Alexandre Julliard's avatar
Alexandre Julliard committed
57 58 59
/*
 * Additional combo box definitions
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
60

Alexandre Julliard's avatar
Alexandre Julliard committed
61
#define CB_NOTIFY( lphc, code ) \
62
    (SendMessageW((lphc)->owner, WM_COMMAND, \
63
                  MAKEWPARAM(GetWindowLongPtrW((lphc)->self,GWLP_ID), (code)), (LPARAM)(lphc)->self))
64 65 66 67 68

#define CB_DISABLED( lphc )   (!IsWindowEnabled((lphc)->self))
#define CB_OWNERDRAWN( lphc ) ((lphc)->dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
#define CB_HASSTRINGS( lphc ) ((lphc)->dwStyle & CBS_HASSTRINGS)
#define CB_HWND( lphc )       ((lphc)->self)
69
#define CB_GETTYPE( lphc )    ((lphc)->dwStyle & (CBS_DROPDOWNLIST))
Alexandre Julliard's avatar
Alexandre Julliard committed
70

71 72
#define ISWIN31 (LOWORD(GetVersion()) == 0x0a03)

73 74 75
/*
 * Drawing globals
 */
76
static HBITMAP 	hComboBmp = 0;
77 78 79
static UINT	CBitHeight, CBitWidth;

/*
80
 * Look and feel dependent "constants"
81
 */
82 83

#define COMBO_YBORDERGAP         5
84 85 86 87
#define COMBO_XBORDERSIZE()      2
#define COMBO_YBORDERSIZE()      2
#define COMBO_EDITBUTTONSPACE()  0
#define EDIT_CONTROL_PADDING()   1
Alexandre Julliard's avatar
Alexandre Julliard committed
88

89 90 91
/*********************************************************************
 * combo class descriptor
 */
92
static const WCHAR comboboxW[] = {'C','o','m','b','o','B','o','x',0};
93 94
const struct builtin_class_descr COMBO_builtin_class =
{
95
    comboboxW,            /* name */
96
    CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, /* style  */
97
    WINPROC_COMBO,        /* proc */
98
    sizeof(HEADCOMBO *),  /* extra */
99
    IDC_ARROW,            /* cursor */
100 101 102 103
    0                     /* brush */
};


Alexandre Julliard's avatar
Alexandre Julliard committed
104 105 106 107 108
/***********************************************************************
 *           COMBO_Init
 *
 * Load combo button bitmap.
 */
109
static BOOL COMBO_Init(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
110
{
111
  HDC		hDC;
112

Alexandre Julliard's avatar
Alexandre Julliard committed
113
  if( hComboBmp ) return TRUE;
114
  if( (hDC = CreateCompatibleDC(0)) )
Alexandre Julliard's avatar
Alexandre Julliard committed
115
  {
116
    BOOL	bRet = FALSE;
117
    if( (hComboBmp = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_COMBO))) )
Alexandre Julliard's avatar
Alexandre Julliard committed
118
    {
119 120 121
      BITMAP      bm;
      HBITMAP     hPrevB;
      RECT        r;
Alexandre Julliard's avatar
Alexandre Julliard committed
122

123
      GetObjectW( hComboBmp, sizeof(bm), &bm );
Alexandre Julliard's avatar
Alexandre Julliard committed
124 125
      CBitHeight = bm.bmHeight;
      CBitWidth  = bm.bmWidth;
Alexandre Julliard's avatar
Alexandre Julliard committed
126

127
      TRACE("combo bitmap [%i,%i]\n", CBitWidth, CBitHeight );
Alexandre Julliard's avatar
Alexandre Julliard committed
128

129
      hPrevB = SelectObject( hDC, hComboBmp);
130 131 132
      SetRect( &r, 0, 0, CBitWidth, CBitHeight );
      InvertRect( hDC, &r );
      SelectObject( hDC, hPrevB );
Alexandre Julliard's avatar
Alexandre Julliard committed
133 134
      bRet = TRUE;
    }
135
    DeleteDC( hDC );
Alexandre Julliard's avatar
Alexandre Julliard committed
136 137 138
    return bRet;
  }
  return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
139 140 141
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
142
 *           COMBO_NCCreate
Alexandre Julliard's avatar
Alexandre Julliard committed
143
 */
144
static LRESULT COMBO_NCCreate(HWND hwnd, LONG style)
Alexandre Julliard's avatar
Alexandre Julliard committed
145
{
146
    LPHEADCOMBO lphc;
Alexandre Julliard's avatar
Alexandre Julliard committed
147

148 149 150
    if (COMBO_Init() && (lphc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEADCOMBO))) )
    {
        lphc->self = hwnd;
151
        SetWindowLongPtrW( hwnd, 0, (LONG_PTR)lphc );
Alexandre Julliard's avatar
Alexandre Julliard committed
152 153

       /* some braindead apps do try to use scrollbar/border flags */
Alexandre Julliard's avatar
Alexandre Julliard committed
154

155
	lphc->dwStyle = style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL);
156
        SetWindowLongW( hwnd, GWL_STYLE, style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL) );
Alexandre Julliard's avatar
Alexandre Julliard committed
157

158 159 160 161
	/*
	 * We also have to remove the client edge style to make sure
	 * we don't end-up with a non client area.
	 */
162 163
        SetWindowLongW( hwnd, GWL_EXSTYLE,
                        GetWindowLongW( hwnd, GWL_EXSTYLE ) & ~WS_EX_CLIENTEDGE );
164

165
	if( !(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
Alexandre Julliard's avatar
Alexandre Julliard committed
166
              lphc->dwStyle |= CBS_HASSTRINGS;
167
	if( !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) )
Alexandre Julliard's avatar
Alexandre Julliard committed
168
	      lphc->wState |= CBF_NOTIFY;
Alexandre Julliard's avatar
Alexandre Julliard committed
169

170
        TRACE("[%p], style = %08x\n", lphc, lphc->dwStyle );
171
        return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
172
    }
173
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
174 175 176
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
177
 *           COMBO_NCDestroy
Alexandre Julliard's avatar
Alexandre Julliard committed
178
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
179
static LRESULT COMBO_NCDestroy( LPHEADCOMBO lphc )
Alexandre Julliard's avatar
Alexandre Julliard committed
180
{
Alexandre Julliard's avatar
Alexandre Julliard committed
181

Alexandre Julliard's avatar
Alexandre Julliard committed
182 183
   if( lphc )
   {
184
       TRACE("[%p]: freeing storage\n", lphc->self);
Alexandre Julliard's avatar
Alexandre Julliard committed
185

186
       if( (CB_GETTYPE(lphc) != CBS_SIMPLE) && lphc->hWndLBox )
187
   	   DestroyWindow( lphc->hWndLBox );
Alexandre Julliard's avatar
Alexandre Julliard committed
188

189
       SetWindowLongPtrW( lphc->self, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
190 191
       HeapFree( GetProcessHeap(), 0, lphc );
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
192
   return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
193 194
}

195 196 197
/***********************************************************************
 *           CBGetTextAreaHeight
 *
198
 * This method will calculate the height of the text area of the
199
 * combobox.
200
 * The height of the text area is set in two ways.
201
 * It can be set explicitly through a combobox message or through a
202
 * WM_MEASUREITEM callback.
203
 * If this is not the case, the height is set to font height + 4px
204
 * This height was determined through experimentation.
205
 * CBCalcPlacement will add 2*COMBO_YBORDERSIZE pixels for the border
206 207 208 209 210 211
 */
static INT CBGetTextAreaHeight(
  HWND        hwnd,
  LPHEADCOMBO lphc)
{
  INT iTextItemHeight;
212

213 214 215 216 217 218
  if( lphc->editHeight ) /* explicitly set height */
  {
    iTextItemHeight = lphc->editHeight;
  }
  else
  {
219
    TEXTMETRICW tm;
220 221 222
    HDC         hDC       = GetDC(hwnd);
    HFONT       hPrevFont = 0;
    INT         baseUnitY;
223

224 225
    if (lphc->hFont)
      hPrevFont = SelectObject( hDC, lphc->hFont );
226

227
    GetTextMetricsW(hDC, &tm);
228

229
    baseUnitY = tm.tmHeight;
230

231 232
    if( hPrevFont )
      SelectObject( hDC, hPrevFont );
233

234
    ReleaseDC(hwnd, hDC);
235

236
    iTextItemHeight = baseUnitY + 4;
237
  }
238

239 240 241 242 243 244 245 246 247 248
  /*
   * Check the ownerdraw case if we haven't asked the parent the size
   * of the item yet.
   */
  if ( CB_OWNERDRAWN(lphc) &&
       (lphc->wState & CBF_MEASUREITEM) )
  {
    MEASUREITEMSTRUCT measureItem;
    RECT              clientRect;
    INT               originalItemHeight = iTextItemHeight;
249
    UINT id = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
250

251 252 253 254
    /*
     * We use the client rect for the width of the item.
     */
    GetClientRect(hwnd, &clientRect);
255

256
    lphc->wState &= ~CBF_MEASUREITEM;
257

258 259 260 261
    /*
     * Send a first one to measure the size of the text area
     */
    measureItem.CtlType    = ODT_COMBOBOX;
262
    measureItem.CtlID      = id;
263 264 265 266
    measureItem.itemID     = -1;
    measureItem.itemWidth  = clientRect.right;
    measureItem.itemHeight = iTextItemHeight - 6; /* ownerdrawn cb is taller */
    measureItem.itemData   = 0;
267
    SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
268 269 270 271 272 273 274 275 276
    iTextItemHeight = 6 + measureItem.itemHeight;

    /*
     * Send a second one in the case of a fixed ownerdraw list to calculate the
     * size of the list items. (we basically do this on behalf of the listbox)
     */
    if (lphc->dwStyle & CBS_OWNERDRAWFIXED)
    {
      measureItem.CtlType    = ODT_COMBOBOX;
277
      measureItem.CtlID      = id;
278 279 280 281
      measureItem.itemID     = 0;
      measureItem.itemWidth  = clientRect.right;
      measureItem.itemHeight = originalItemHeight;
      measureItem.itemData   = 0;
282
      SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
283 284
      lphc->fixedOwnerDrawHeight = measureItem.itemHeight;
    }
285

286
    /*
287
     * Keep the size for the next time
288 289 290
     */
    lphc->editHeight = iTextItemHeight;
  }
291

292
  return iTextItemHeight;
293 294
}

295 296 297 298 299 300 301 302 303 304 305 306 307
/***********************************************************************
 *           CBForceDummyResize
 *
 * The dummy resize is used for listboxes that have a popup to trigger
 * a re-arranging of the contents of the combobox and the recalculation
 * of the size of the "real" control window.
 */
static void CBForceDummyResize(
  LPHEADCOMBO lphc)
{
  RECT windowRect;
  int newComboHeight;

308
  newComboHeight = CBGetTextAreaHeight(lphc->self,lphc) + 2*COMBO_YBORDERSIZE();
309

310
  GetWindowRect(lphc->self, &windowRect);
311 312 313 314 315 316 317 318 319

  /*
   * We have to be careful, resizing a combobox also has the meaning that the
   * dropped rect will be resized. In this case, we want to trigger a resize
   * to recalculate layout but we don't want to change the dropped rectangle
   * So, we pass the height of text area of control as the height.
   * this will cancel-out in the processing of the WM_WINDOWPOSCHANGING
   * message.
   */
320
  SetWindowPos( lphc->self,
321
		NULL,
322 323 324 325 326
		0, 0,
		windowRect.right  - windowRect.left,
		newComboHeight,
		SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
}
Alexandre Julliard's avatar
Alexandre Julliard committed
327

Alexandre Julliard's avatar
Alexandre Julliard committed
328
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
329 330 331
 *           CBCalcPlacement
 *
 * Set up component coordinates given valid lphc->RectCombo.
Alexandre Julliard's avatar
Alexandre Julliard committed
332
 */
333 334 335
static void CBCalcPlacement(
  HWND        hwnd,
  LPHEADCOMBO lphc,
336 337
  LPRECT      lprEdit,
  LPRECT      lprButton,
338
  LPRECT      lprLB)
Alexandre Julliard's avatar
Alexandre Julliard committed
339
{
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  /*
   * Again, start with the client rectangle.
   */
  GetClientRect(hwnd, lprEdit);

  /*
   * Remove the borders
   */
  InflateRect(lprEdit, -COMBO_XBORDERSIZE(), -COMBO_YBORDERSIZE());

  /*
   * Chop off the bottom part to fit with the height of the text area.
   */
  lprEdit->bottom = lprEdit->top + CBGetTextAreaHeight(hwnd, lphc);

  /*
   * The button starts the same vertical position as the text area.
   */
  CopyRect(lprButton, lprEdit);

  /*
   * If the combobox is "simple" there is no button.
   */
363
  if( CB_GETTYPE(lphc) == CBS_SIMPLE )
364 365 366 367 368 369 370 371 372 373 374
    lprButton->left = lprButton->right = lprButton->bottom = 0;
  else
  {
    /*
     * Let's assume the combobox button is the same width as the
     * scrollbar button.
     * size the button horizontally and cut-off the text area.
     */
    lprButton->left = lprButton->right - GetSystemMetrics(SM_CXVSCROLL);
    lprEdit->right  = lprButton->left;
  }
375

376 377 378 379 380 381 382 383
  /*
   * In the case of a dropdown, there is an additional spacing between the
   * text area and the button.
   */
  if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
  {
    lprEdit->right -= COMBO_EDITBUTTONSPACE();
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
384

385 386 387 388 389 390 391
  /*
   * If we have an edit control, we space it away from the borders slightly.
   */
  if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
  {
    InflateRect(lprEdit, -EDIT_CONTROL_PADDING(), -EDIT_CONTROL_PADDING());
  }
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
  /*
   * Adjust the size of the listbox popup.
   */
  if( CB_GETTYPE(lphc) == CBS_SIMPLE )
  {
    /*
     * Use the client rectangle to initialize the listbox rectangle
     */
    GetClientRect(hwnd, lprLB);

    /*
     * Then, chop-off the top part.
     */
    lprLB->top = lprEdit->bottom + COMBO_YBORDERSIZE();
  }
  else
  {
    /*
     * Make sure the dropped width is as large as the combobox itself.
     */
    if (lphc->droppedWidth < (lprButton->right + COMBO_XBORDERSIZE()))
    {
      lprLB->right  = lprLB->left + (lprButton->right + COMBO_XBORDERSIZE());

      /*
       * In the case of a dropdown, the popup listbox is offset to the right.
419
       * so, we want to make sure it's flush with the right side of the
420 421 422 423 424 425
       * combobox
       */
      if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
	lprLB->right -= COMBO_EDITBUTTONSPACE();
    }
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
426
       lprLB->right = lprLB->left + lphc->droppedWidth;
427
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
428

429 430 431 432
  /* don't allow negative window width */
  if (lprEdit->right < lprEdit->left)
    lprEdit->right = lprEdit->left;

433
  TRACE("\ttext\t= (%s)\n", wine_dbgstr_rect(lprEdit));
434

435
  TRACE("\tbutton\t= (%s)\n", wine_dbgstr_rect(lprButton));
436

437
  TRACE("\tlbox\t= (%s)\n", wine_dbgstr_rect(lprLB));
Alexandre Julliard's avatar
Alexandre Julliard committed
438 439 440
}

/***********************************************************************
441
 *           CBGetDroppedControlRect
Alexandre Julliard's avatar
Alexandre Julliard committed
442
 */
443
static void CBGetDroppedControlRect( LPHEADCOMBO lphc, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
444
{
445 446
    /* In windows, CB_GETDROPPEDCONTROLRECT returns the upper left corner
     of the combo box and the lower right corner of the listbox */
447

448
    GetWindowRect(lphc->self, lpRect);
449 450 451 452

    lpRect->right =  lpRect->left + lphc->droppedRect.right - lphc->droppedRect.left;
    lpRect->bottom = lpRect->top + lphc->droppedRect.bottom - lphc->droppedRect.top;

453 454
}

Alexandre Julliard's avatar
Alexandre Julliard committed
455
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
456
 *           COMBO_Create
Alexandre Julliard's avatar
Alexandre Julliard committed
457
 */
458 459
static LRESULT COMBO_Create( HWND hwnd, LPHEADCOMBO lphc, HWND hwndParent, LONG style,
                             BOOL unicode )
Alexandre Julliard's avatar
Alexandre Julliard committed
460
{
461 462
  static const WCHAR clbName[] = {'C','o','m','b','o','L','B','o','x',0};
  static const WCHAR editName[] = {'E','d','i','t',0};
Alexandre Julliard's avatar
Alexandre Julliard committed
463

Alexandre Julliard's avatar
Alexandre Julliard committed
464
  if( !CB_GETTYPE(lphc) ) lphc->dwStyle |= CBS_SIMPLE;
465
  if( CB_GETTYPE(lphc) != CBS_DROPDOWNLIST ) lphc->wState |= CBF_EDIT;
466

467
  lphc->owner = hwndParent;
Alexandre Julliard's avatar
Alexandre Julliard committed
468

469 470 471 472 473 474 475 476 477 478 479
  /*
   * The item height and dropped width are not set when the control
   * is created.
   */
  lphc->droppedWidth = lphc->editHeight = 0;

  /*
   * The first time we go through, we want to measure the ownerdraw item
   */
  lphc->wState |= CBF_MEASUREITEM;

Alexandre Julliard's avatar
Alexandre Julliard committed
480
  /* M$ IE 3.01 actually creates (and rapidly destroys) an ownerless combobox */
Alexandre Julliard's avatar
Alexandre Julliard committed
481

482
  if( lphc->owner || !(style & WS_VISIBLE) )
Alexandre Julliard's avatar
Alexandre Julliard committed
483
  {
484 485
      UINT lbeStyle   = 0;
      UINT lbeExStyle = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
486

487 488 489 490 491
      /*
       * Initialize the dropped rect to the size of the client area of the
       * control and then, force all the areas of the combobox to be
       * recalculated.
       */
492 493
      GetClientRect( hwnd, &lphc->droppedRect );
      CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );
494 495 496 497 498 499 500

      /*
       * Adjust the position of the popup listbox if it's necessary
       */
      if ( CB_GETTYPE(lphc) != CBS_SIMPLE )
      {
	lphc->droppedRect.top   = lphc->textRect.bottom + COMBO_YBORDERSIZE();
Alexandre Julliard's avatar
Alexandre Julliard committed
501

502 503 504 505 506 507
	/*
	 * If it's a dropdown, the listbox is offset
	 */
	if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
	  lphc->droppedRect.left += COMBO_EDITBUTTONSPACE();

508 509 510 511 512
        if (lphc->droppedRect.bottom < lphc->droppedRect.top)
            lphc->droppedRect.bottom = lphc->droppedRect.top;
        if (lphc->droppedRect.right < lphc->droppedRect.left)
            lphc->droppedRect.right = lphc->droppedRect.left;
        MapWindowPoints( hwnd, 0, (LPPOINT)&lphc->droppedRect, 2 );
513
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
514 515 516

      /* create listbox popup */

517
      lbeStyle = (LBS_NOTIFY | LBS_COMBOBOX | WS_BORDER | WS_CLIPSIBLINGS | WS_CHILD) |
518
                 (style & (WS_VSCROLL | CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE));
Alexandre Julliard's avatar
Alexandre Julliard committed
519 520 521 522 523 524 525 526 527

      if( lphc->dwStyle & CBS_SORT )
	lbeStyle |= LBS_SORT;
      if( lphc->dwStyle & CBS_HASSTRINGS )
	lbeStyle |= LBS_HASSTRINGS;
      if( lphc->dwStyle & CBS_NOINTEGRALHEIGHT )
	lbeStyle |= LBS_NOINTEGRALHEIGHT;
      if( lphc->dwStyle & CBS_DISABLENOSCROLL )
	lbeStyle |= LBS_DISABLENOSCROLL;
528

Alexandre Julliard's avatar
Alexandre Julliard committed
529
      if( CB_GETTYPE(lphc) == CBS_SIMPLE ) 	/* child listbox */
530
      {
531
	lbeStyle |= WS_VISIBLE;
532 533 534 535 536

	/*
	 * In win 95 look n feel, the listbox in the simple combobox has
	 * the WS_EXCLIENTEDGE style instead of the WS_BORDER style.
	 */
537 538
	lbeStyle   &= ~WS_BORDER;
	lbeExStyle |= WS_EX_CLIENTEDGE;
539
      }
540 541 542 543
      else
      {
        lbeExStyle |= (WS_EX_TOPMOST | WS_EX_TOOLWINDOW);
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
544

545 546 547 548 549 550 551
      if (unicode)
          lphc->hWndLBox = CreateWindowExW(lbeExStyle, clbName, NULL, lbeStyle,
                                           lphc->droppedRect.left,
                                           lphc->droppedRect.top,
                                           lphc->droppedRect.right - lphc->droppedRect.left,
                                           lphc->droppedRect.bottom - lphc->droppedRect.top,
                                           hwnd, (HMENU)ID_CB_LISTBOX,
552
                                           (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
553 554 555 556 557 558 559
      else
          lphc->hWndLBox = CreateWindowExA(lbeExStyle, "ComboLBox", NULL, lbeStyle,
                                           lphc->droppedRect.left,
                                           lphc->droppedRect.top,
                                           lphc->droppedRect.right - lphc->droppedRect.left,
                                           lphc->droppedRect.bottom - lphc->droppedRect.top,
                                           hwnd, (HMENU)ID_CB_LISTBOX,
560
                                           (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
561

Alexandre Julliard's avatar
Alexandre Julliard committed
562 563
      if( lphc->hWndLBox )
      {
564
	  BOOL	bEdit = TRUE;
565
	  lbeStyle = WS_CHILD | WS_VISIBLE | ES_NOHIDESEL | ES_LEFT | ES_COMBO;
566

567
	  if( lphc->wState & CBF_EDIT )
Alexandre Julliard's avatar
Alexandre Julliard committed
568 569 570 571 572 573 574 575 576
	  {
	      if( lphc->dwStyle & CBS_OEMCONVERT )
		  lbeStyle |= ES_OEMCONVERT;
	      if( lphc->dwStyle & CBS_AUTOHSCROLL )
		  lbeStyle |= ES_AUTOHSCROLL;
	      if( lphc->dwStyle & CBS_LOWERCASE )
		  lbeStyle |= ES_LOWERCASE;
	      else if( lphc->dwStyle & CBS_UPPERCASE )
		  lbeStyle |= ES_UPPERCASE;
577

578
              if (!IsWindowEnabled(hwnd)) lbeStyle |= WS_DISABLED;
579

580 581 582 583 584 585
              if (unicode)
                  lphc->hWndEdit = CreateWindowExW(0, editName, NULL, lbeStyle,
                                                   lphc->textRect.left, lphc->textRect.top,
                                                   lphc->textRect.right - lphc->textRect.left,
                                                   lphc->textRect.bottom - lphc->textRect.top,
                                                   hwnd, (HMENU)ID_CB_EDIT,
586
                                                   (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
587 588 589 590 591 592
              else
                  lphc->hWndEdit = CreateWindowExA(0, "Edit", NULL, lbeStyle,
                                                   lphc->textRect.left, lphc->textRect.top,
                                                   lphc->textRect.right - lphc->textRect.left,
                                                   lphc->textRect.bottom - lphc->textRect.top,
                                                   hwnd, (HMENU)ID_CB_EDIT,
593
                                                   (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
594 595 596

	      if( !lphc->hWndEdit )
		bEdit = FALSE;
597
	  }
Alexandre Julliard's avatar
Alexandre Julliard committed
598 599 600

          if( bEdit )
	  {
601 602
	    if( CB_GETTYPE(lphc) != CBS_SIMPLE )
	    {
603 604
              /* Now do the trick with parent */
	      SetParent(lphc->hWndLBox, HWND_DESKTOP);
605
              /*
606 607 608 609
               * If the combo is a dropdown, we must resize the control
	       * to fit only the text area and button. To do this,
	       * we send a dummy resize and the WM_WINDOWPOSCHANGING message
	       * will take care of setting the height for us.
610
               */
611
	      CBForceDummyResize(lphc);
612
	    }
613

614
	    TRACE("init done\n");
615
	    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
616
	  }
617 618 619
	  ERR("edit control failure.\n");
      } else ERR("listbox failure.\n");
  } else ERR("no owner for visible combo.\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
620 621 622 623

  /* CreateWindow() will send WM_NCDESTROY to cleanup */

  return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
624 625 626
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
627 628 629
 *           CBPaintButton
 *
 * Paint combo button (normal, pressed, and disabled states).
Alexandre Julliard's avatar
Alexandre Julliard committed
630
 */
631
static void CBPaintButton( LPHEADCOMBO lphc, HDC hdc, RECT rectButton)
Alexandre Julliard's avatar
Alexandre Julliard committed
632
{
633 634
    UINT buttonState = DFCS_SCROLLCOMBOBOX;

635
    if( lphc->wState & CBF_NOREDRAW )
636
      return;
Alexandre Julliard's avatar
Alexandre Julliard committed
637

638

639 640
    if (lphc->wState & CBF_BUTTONDOWN)
	buttonState |= DFCS_PUSHED;
641

642 643
    if (CB_DISABLED(lphc))
	buttonState |= DFCS_INACTIVE;
644

645
    DrawFrameControl(hdc, &rectButton, DFC_SCROLL, buttonState);
Alexandre Julliard's avatar
Alexandre Julliard committed
646
}
Alexandre Julliard's avatar
Alexandre Julliard committed
647

Alexandre Julliard's avatar
Alexandre Julliard committed
648
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
649 650 651
 *           CBPaintText
 *
 * Paint CBS_DROPDOWNLIST text field / update edit control contents.
Alexandre Julliard's avatar
Alexandre Julliard committed
652
 */
653
static void CBPaintText(
654
  LPHEADCOMBO lphc,
655 656
  HDC         hdc,
  RECT        rectEdit)
Alexandre Julliard's avatar
Alexandre Julliard committed
657
{
658
   INT	id, size = 0;
659
   LPWSTR pText = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
660

Alexandre Julliard's avatar
Alexandre Julliard committed
661
   if( lphc->wState & CBF_NOREDRAW ) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
662

663 664
   TRACE("\n");

665
   /* follow Windows combobox that sends a bunch of text
Alexandre Julliard's avatar
Alexandre Julliard committed
666
    * inquiries to its listbox while processing WM_PAINT. */
Alexandre Julliard's avatar
Alexandre Julliard committed
667

668
   if( (id = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0) ) != LB_ERR )
Alexandre Julliard's avatar
Alexandre Julliard committed
669
   {
670
        size = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, id, 0);
671 672
	if (size == LB_ERR)
	  FIXME("LB_ERR probably not handled yet\n");
673
        if( (pText = HeapAlloc( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))) )
Alexandre Julliard's avatar
Alexandre Julliard committed
674
	{
675
            /* size from LB_GETTEXTLEN may be too large, from LB_GETTEXT is accurate */
676
           size=SendMessageW(lphc->hWndLBox, LB_GETTEXT, id, (LPARAM)pText);
Alexandre Julliard's avatar
Alexandre Julliard committed
677 678 679
	    pText[size] = '\0';	/* just in case */
	} else return;
   }
680
   else
681 682
       if( !CB_OWNERDRAWN(lphc) )
	   return;
Alexandre Julliard's avatar
Alexandre Julliard committed
683

Alexandre Julliard's avatar
Alexandre Julliard committed
684 685
   if( lphc->wState & CBF_EDIT )
   {
686 687
        static const WCHAR empty_stringW[] = { 0 };
	if( CB_HASSTRINGS(lphc) ) SetWindowTextW( lphc->hWndEdit, pText ? pText : empty_stringW );
688
	if( lphc->wState & CBF_FOCUSED )
689
           SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
Alexandre Julliard's avatar
Alexandre Julliard committed
690 691 692
   }
   else /* paint text field ourselves */
   {
693
     UINT	itemState = ODS_COMBOBOXEDIT;
694
     HFONT	hPrevFont = (lphc->hFont) ? SelectObject(hdc, lphc->hFont) : 0;
695

696 697 698 699
     /*
      * Give ourselves some space.
      */
     InflateRect( &rectEdit, -1, -1 );
700

701 702 703 704
     if( CB_OWNERDRAWN(lphc) )
     {
       DRAWITEMSTRUCT dis;
       HRGN           clipRegion;
705
       UINT ctlid = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
706

707
       /* setup state for DRAWITEM message. Owner will highlight */
708
       if ( (lphc->wState & CBF_FOCUSED) &&
709 710 711
	    !(lphc->wState & CBF_DROPPED) )
	   itemState |= ODS_SELECTED | ODS_FOCUS;

Andrew Talbot's avatar
Andrew Talbot committed
712
       if (!IsWindowEnabled(lphc->self)) itemState |= ODS_DISABLED;
713

714
       dis.CtlType	= ODT_COMBOBOX;
715 716
       dis.CtlID	= ctlid;
       dis.hwndItem	= lphc->self;
717 718 719 720 721
       dis.itemAction	= ODA_DRAWENTIRE;
       dis.itemID	= id;
       dis.itemState	= itemState;
       dis.hDC		= hdc;
       dis.rcItem	= rectEdit;
722
       dis.itemData     = SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, id, 0);
723

724 725 726
       /*
	* Clip the DC and have the parent draw the item.
	*/
727
       clipRegion = set_control_clipping( hdc, &rectEdit );
728 729 730

       SendMessageW(lphc->owner, WM_DRAWITEM, ctlid, (LPARAM)&dis );

731 732
       SelectClipRgn( hdc, clipRegion );
       if (clipRegion) DeleteObject( clipRegion );
733 734 735
     }
     else
     {
736 737
       static const WCHAR empty_stringW[] = { 0 };

738
       if ( (lphc->wState & CBF_FOCUSED) &&
739 740 741 742 743 744
	    !(lphc->wState & CBF_DROPPED) ) {

	   /* highlight */
	   FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
	   SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
	   SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
745 746 747 748
       }

       ExtTextOutW( hdc,
		    rectEdit.left + 1,
749
		    rectEdit.top + 1,
750
		    ETO_OPAQUE | ETO_CLIPPED,
751
		    &rectEdit,
752
		    pText ? pText : empty_stringW , size, NULL );
753

754 755 756
       if(lphc->wState & CBF_FOCUSED && !(lphc->wState & CBF_DROPPED))
	 DrawFocusRect( hdc, &rectEdit );
     }
757 758

     if( hPrevFont )
759
       SelectObject(hdc, hPrevFont );
Alexandre Julliard's avatar
Alexandre Julliard committed
760
   }
761
   HeapFree( GetProcessHeap(), 0, pText );
Alexandre Julliard's avatar
Alexandre Julliard committed
762 763
}

764 765 766 767
/***********************************************************************
 *           CBPaintBorder
 */
static void CBPaintBorder(
768 769 770
  HWND            hwnd,
  const HEADCOMBO *lphc,
  HDC             hdc)
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
{
  RECT clientRect;

  if (CB_GETTYPE(lphc) != CBS_SIMPLE)
  {
    GetClientRect(hwnd, &clientRect);
  }
  else
  {
    CopyRect(&clientRect, &lphc->textRect);

    InflateRect(&clientRect, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
    InflateRect(&clientRect, COMBO_XBORDERSIZE(), COMBO_YBORDERSIZE());
  }

  DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_RECT);
}

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
/***********************************************************************
 *           COMBO_PrepareColors
 *
 * This method will sent the appropriate WM_CTLCOLOR message to
 * prepare and setup the colors for the combo's DC.
 *
 * It also returns the brush to use for the background.
 */
static HBRUSH COMBO_PrepareColors(
  LPHEADCOMBO lphc,
  HDC         hDC)
{
  HBRUSH  hBkgBrush;

  /*
   * Get the background brush for this control.
   */
  if (CB_DISABLED(lphc))
  {
808 809
    hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORSTATIC,
				     (WPARAM)hDC, (LPARAM)lphc->self );
810

811 812 813 814 815 816 817 818 819
    /*
     * We have to change the text color since WM_CTLCOLORSTATIC will
     * set it to the "enabled" color. This is the same behavior as the
     * edit control
     */
    SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
  }
  else
  {
820
      /* FIXME: In which cases WM_CTLCOLORLISTBOX should be sent? */
821 822
      hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLOREDIT,
				       (WPARAM)hDC, (LPARAM)lphc->self );
823 824 825 826 827
  }

  /*
   * Catch errors.
   */
828
  if( !hBkgBrush )
829 830 831 832 833
    hBkgBrush = GetSysColorBrush(COLOR_WINDOW);

  return hBkgBrush;
}

834

Alexandre Julliard's avatar
Alexandre Julliard committed
835
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
836
 *           COMBO_Paint
Alexandre Julliard's avatar
Alexandre Julliard committed
837
 */
838
static LRESULT COMBO_Paint(LPHEADCOMBO lphc, HDC hParamDC)
Alexandre Julliard's avatar
Alexandre Julliard committed
839
{
840 841
  PAINTSTRUCT ps;
  HDC 	hDC;
842

Alexandre Julliard's avatar
Alexandre Julliard committed
843
  hDC = (hParamDC) ? hParamDC
844
		   : BeginPaint( lphc->self, &ps);
845

846
  TRACE("hdc=%p\n", hDC);
847

Alexandre Julliard's avatar
Alexandre Julliard committed
848
  if( hDC && !(lphc->wState & CBF_NOREDRAW) )
Alexandre Julliard's avatar
Alexandre Julliard committed
849
  {
850
      HBRUSH	hPrevBrush, hBkgBrush;
Alexandre Julliard's avatar
Alexandre Julliard committed
851

852 853 854 855
      /*
       * Retrieve the background brush and select it in the
       * DC.
       */
856
      hBkgBrush = COMBO_PrepareColors(lphc, hDC);
Alexandre Julliard's avatar
Alexandre Julliard committed
857

858
      hPrevBrush = SelectObject( hDC, hBkgBrush );
859 860
      if (!(lphc->wState & CBF_EDIT))
        FillRect(hDC, &lphc->textRect, hBkgBrush);
861 862 863 864

      /*
       * In non 3.1 look, there is a sunken border on the combobox
       */
865
      CBPaintBorder(lphc->self, lphc, hDC);
Alexandre Julliard's avatar
Alexandre Julliard committed
866

867 868 869
      if( !IsRectEmpty(&lphc->buttonRect) )
      {
	CBPaintButton(lphc, hDC, lphc->buttonRect);
Alexandre Julliard's avatar
Alexandre Julliard committed
870 871
      }

872 873 874 875 876 877 878
      /* paint the edit control padding area */
      if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
      {
          RECT rPadEdit = lphc->textRect;

          InflateRect(&rPadEdit, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());

879
          FrameRect( hDC, &rPadEdit, GetSysColorBrush(COLOR_WINDOW) );
880
      }
881

Alexandre Julliard's avatar
Alexandre Julliard committed
882
      if( !(lphc->wState & CBF_EDIT) )
883 884 885 886
	CBPaintText( lphc, hDC, lphc->textRect);

      if( hPrevBrush )
	SelectObject( hDC, hPrevBrush );
Alexandre Julliard's avatar
Alexandre Julliard committed
887
  }
888

889
  if( !hParamDC )
890
    EndPaint(lphc->self, &ps);
891

Alexandre Julliard's avatar
Alexandre Julliard committed
892 893 894 895
  return 0;
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
896 897 898
 *           CBUpdateLBox
 *
 * Select listbox entry according to the contents of the edit control.
Alexandre Julliard's avatar
Alexandre Julliard committed
899
 */
900
static INT CBUpdateLBox( LPHEADCOMBO lphc, BOOL bSelect )
Alexandre Julliard's avatar
Alexandre Julliard committed
901
{
902
   INT	length, idx;
903
   LPWSTR pText = NULL;
904

905
   idx = LB_ERR;
906 907
   length = SendMessageW( lphc->hWndEdit, WM_GETTEXTLENGTH, 0, 0 );

908
   if( length > 0 )
909
       pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
Alexandre Julliard's avatar
Alexandre Julliard committed
910

911
   TRACE("\t edit text length %i\n", length );
Alexandre Julliard's avatar
Alexandre Julliard committed
912 913 914

   if( pText )
   {
915
       GetWindowTextW( lphc->hWndEdit, pText, length + 1);
916
       idx = SendMessageW(lphc->hWndLBox, LB_FINDSTRING, -1, (LPARAM)pText);
Alexandre Julliard's avatar
Alexandre Julliard committed
917 918
       HeapFree( GetProcessHeap(), 0, pText );
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
919

920
   SendMessageW(lphc->hWndLBox, LB_SETCURSEL, bSelect ? idx : -1, 0);
921 922

   /* probably superfluous but Windows sends this too */
923 924
   SendMessageW(lphc->hWndLBox, LB_SETCARETINDEX, idx < 0 ? 0 : idx, 0);
   SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, idx < 0 ? 0 : idx, 0);
925

926
   return idx;
Alexandre Julliard's avatar
Alexandre Julliard committed
927 928 929
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
930 931 932
 *           CBUpdateEdit
 *
 * Copy a listbox entry to the edit control.
Alexandre Julliard's avatar
Alexandre Julliard committed
933
 */
934
static void CBUpdateEdit( LPHEADCOMBO lphc , INT index )
Alexandre Julliard's avatar
Alexandre Julliard committed
935
{
936
   INT	length;
937
   LPWSTR pText = NULL;
938
   static const WCHAR empty_stringW[] = { 0 };
Alexandre Julliard's avatar
Alexandre Julliard committed
939

940
   TRACE("\t %i\n", index );
Alexandre Julliard's avatar
Alexandre Julliard committed
941

Alexandre Julliard's avatar
Alexandre Julliard committed
942 943
   if( index >= 0 ) /* got an entry */
   {
944
       length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, index, 0);
945
       if( length != LB_ERR)
Alexandre Julliard's avatar
Alexandre Julliard committed
946
       {
947
	   if( (pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR))) )
Alexandre Julliard's avatar
Alexandre Julliard committed
948
	   {
949
               SendMessageW(lphc->hWndLBox, LB_GETTEXT, index, (LPARAM)pText);
Alexandre Julliard's avatar
Alexandre Julliard committed
950 951
	   }
       }
952
   }
953

954 955 956 957 958 959
   if( CB_HASSTRINGS(lphc) )
   {
      lphc->wState |= (CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
      SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, pText ? (LPARAM)pText : (LPARAM)empty_stringW);
      lphc->wState &= ~(CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
   }
960

961
   if( lphc->wState & CBF_FOCUSED )
962
      SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
963

964
   HeapFree( GetProcessHeap(), 0, pText );
Alexandre Julliard's avatar
Alexandre Julliard committed
965 966 967
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
968
 *           CBDropDown
969
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
970
 * Show listbox popup.
Alexandre Julliard's avatar
Alexandre Julliard committed
971
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
972
static void CBDropDown( LPHEADCOMBO lphc )
Alexandre Julliard's avatar
Alexandre Julliard committed
973
{
974 975
    HMONITOR monitor;
    MONITORINFO mon_info;
976
   RECT rect,r;
977
   int nItems = 0;
978
   int nDroppedHeight;
Alexandre Julliard's avatar
Alexandre Julliard committed
979

980
   TRACE("[%p]: drop down\n", lphc->self);
Alexandre Julliard's avatar
Alexandre Julliard committed
981 982 983 984 985 986 987 988

   CB_NOTIFY( lphc, CBN_DROPDOWN );

   /* set selection */

   lphc->wState |= CBF_DROPPED;
   if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
   {
989
       lphc->droppedIndex = CBUpdateLBox( lphc, TRUE );
990

991 992
       /* Update edit only if item is in the list */
       if( !(lphc->wState & CBF_CAPTURE) && lphc->droppedIndex >= 0)
993
	 CBUpdateEdit( lphc, lphc->droppedIndex );
Alexandre Julliard's avatar
Alexandre Julliard committed
994 995 996
   }
   else
   {
997
       lphc->droppedIndex = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
998

999
       SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX,
1000
                    lphc->droppedIndex == LB_ERR ? 0 : lphc->droppedIndex, 0);
1001
       SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1002
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1003

Alexandre Julliard's avatar
Alexandre Julliard committed
1004
   /* now set popup position */
1005
   GetWindowRect( lphc->self, &rect );
1006

1007 1008 1009 1010 1011 1012
   /*
    * If it's a dropdown, the listbox is offset
    */
   if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
     rect.left += COMBO_EDITBUTTONSPACE();

1013 1014 1015 1016
  /* if the dropped height is greater than the total height of the dropped
     items list, then force the drop down list height to be the total height
     of the items in the dropped list */

1017
  /* And Remove any extra space (Best Fit) */
1018
   nDroppedHeight = lphc->droppedRect.bottom - lphc->droppedRect.top;
1019 1020 1021 1022
  /* if listbox length has been set directly by its handle */
   GetWindowRect(lphc->hWndLBox, &r);
   if (nDroppedHeight < r.bottom - r.top)
       nDroppedHeight = r.bottom - r.top;
1023
   nItems = (int)SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
1024 1025

   if (nItems > 0)
1026
   {
1027
      int nHeight;
1028
      int nIHeight;
1029

1030 1031 1032
      nIHeight = (int)SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, 0, 0);

      nHeight = nIHeight*nItems;
1033 1034 1035

      if (nHeight < nDroppedHeight - COMBO_YBORDERSIZE())
         nDroppedHeight = nHeight + COMBO_YBORDERSIZE();
1036

1037
      if (nDroppedHeight < nHeight)
1038 1039
      {
            if (nItems < 5)
1040
                nDroppedHeight = (nItems+1)*nIHeight;
1041
            else if (nDroppedHeight < 6*nIHeight)
1042
                nDroppedHeight = 6*nIHeight;
1043
      }
1044
   }
1045

1046
   /*If height of dropped rectangle gets beyond a screen size it should go up, otherwise down.*/
1047 1048 1049 1050 1051
   monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
   mon_info.cbSize = sizeof(mon_info);
   GetMonitorInfoW( monitor, &mon_info );

   if( (rect.bottom + nDroppedHeight) >= mon_info.rcWork.bottom )
1052
      rect.bottom = rect.top - nDroppedHeight;
1053

1054
   SetWindowPos( lphc->hWndLBox, HWND_TOP, rect.left, rect.bottom,
1055
		 lphc->droppedRect.right - lphc->droppedRect.left,
1056 1057 1058
		 nDroppedHeight,
		 SWP_NOACTIVATE | SWP_SHOWWINDOW);

Alexandre Julliard's avatar
Alexandre Julliard committed
1059

Alexandre Julliard's avatar
Alexandre Julliard committed
1060
   if( !(lphc->wState & CBF_NOREDRAW) )
1061
     RedrawWindow( lphc->self, NULL, 0, RDW_INVALIDATE |
Alexandre Julliard's avatar
Alexandre Julliard committed
1062
			   RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1063

Alexandre Julliard's avatar
Alexandre Julliard committed
1064
   EnableWindow( lphc->hWndLBox, TRUE );
1065
   if (GetCapture() != lphc->self)
1066
      SetCapture(lphc->hWndLBox);
Alexandre Julliard's avatar
Alexandre Julliard committed
1067 1068 1069
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1070 1071 1072
 *           CBRollUp
 *
 * Hide listbox popup.
Alexandre Julliard's avatar
Alexandre Julliard committed
1073
 */
1074
static void CBRollUp( LPHEADCOMBO lphc, BOOL ok, BOOL bButton )
Alexandre Julliard's avatar
Alexandre Julliard committed
1075
{
1076
   HWND	hWnd = lphc->self;
Alexandre Julliard's avatar
Alexandre Julliard committed
1077

1078
   TRACE("[%p]: sel ok? [%i] dropped? [%i]\n",
1079
	 lphc->self, ok, (INT)(lphc->wState & CBF_DROPPED));
1080

Alexandre Julliard's avatar
Alexandre Julliard committed
1081 1082
   CB_NOTIFY( lphc, (ok) ? CBN_SELENDOK : CBN_SELENDCANCEL );

1083
   if( IsWindow( hWnd ) && CB_GETTYPE(lphc) != CBS_SIMPLE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1084 1085
   {

1086
       if( lphc->wState & CBF_DROPPED )
Alexandre Julliard's avatar
Alexandre Julliard committed
1087
       {
1088
	   RECT	rect;
Alexandre Julliard's avatar
Alexandre Julliard committed
1089 1090

	   lphc->wState &= ~CBF_DROPPED;
1091
	   ShowWindow( lphc->hWndLBox, SW_HIDE );
1092

1093 1094 1095 1096 1097
           if(GetCapture() == lphc->hWndLBox)
           {
               ReleaseCapture();
           }

Alexandre Julliard's avatar
Alexandre Julliard committed
1098 1099
	   if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
	   {
1100
	       rect = lphc->buttonRect;
Alexandre Julliard's avatar
Alexandre Julliard committed
1101
	   }
1102
	   else
Alexandre Julliard's avatar
Alexandre Julliard committed
1103 1104
           {
	       if( bButton )
1105 1106 1107 1108 1109
	       {
		 UnionRect( &rect,
			    &lphc->buttonRect,
			    &lphc->textRect);
	       }
Alexandre Julliard's avatar
Alexandre Julliard committed
1110
	       else
1111 1112
		 rect = lphc->textRect;

Alexandre Julliard's avatar
Alexandre Julliard committed
1113 1114 1115
	       bButton = TRUE;
	   }

Alexandre Julliard's avatar
Alexandre Julliard committed
1116
	   if( bButton && !(lphc->wState & CBF_NOREDRAW) )
1117
	       RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE |
Alexandre Julliard's avatar
Alexandre Julliard committed
1118 1119 1120 1121
			       RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
	   CB_NOTIFY( lphc, CBN_CLOSEUP );
       }
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1122 1123 1124
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1125 1126 1127
 *           COMBO_FlipListbox
 *
 * Used by the ComboLBox to show/hide itself in response to VK_F4, etc...
Alexandre Julliard's avatar
Alexandre Julliard committed
1128
 */
1129
BOOL COMBO_FlipListbox( LPHEADCOMBO lphc, BOOL ok, BOOL bRedrawButton )
Alexandre Julliard's avatar
Alexandre Julliard committed
1130
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1131 1132
   if( lphc->wState & CBF_DROPPED )
   {
1133
       CBRollUp( lphc, ok, bRedrawButton );
Alexandre Julliard's avatar
Alexandre Julliard committed
1134 1135 1136 1137 1138
       return FALSE;
   }

   CBDropDown( lphc );
   return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1139 1140 1141
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1142
 *           CBRepaintButton
Alexandre Julliard's avatar
Alexandre Julliard committed
1143
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1144 1145
static void CBRepaintButton( LPHEADCOMBO lphc )
   {
1146 1147
  InvalidateRect(lphc->self, &lphc->buttonRect, TRUE);
  UpdateWindow(lphc->self);
Alexandre Julliard's avatar
Alexandre Julliard committed
1148 1149 1150
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1151
 *           COMBO_SetFocus
Alexandre Julliard's avatar
Alexandre Julliard committed
1152
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1153
static void COMBO_SetFocus( LPHEADCOMBO lphc )
Alexandre Julliard's avatar
Alexandre Julliard committed
1154
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1155 1156 1157
   if( !(lphc->wState & CBF_FOCUSED) )
   {
       if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1158
           SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1159

1160 1161 1162
       /* This is wrong. Message sequences seem to indicate that this
          is set *after* the notify. */
       /* lphc->wState |= CBF_FOCUSED;  */
1163

1164
       if( !(lphc->wState & CBF_EDIT) )
1165
	 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1166

Alexandre Julliard's avatar
Alexandre Julliard committed
1167
       CB_NOTIFY( lphc, CBN_SETFOCUS );
1168
       lphc->wState |= CBF_FOCUSED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1169
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1170 1171 1172
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1173
 *           COMBO_KillFocus
Alexandre Julliard's avatar
Alexandre Julliard committed
1174
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1175
static void COMBO_KillFocus( LPHEADCOMBO lphc )
Alexandre Julliard's avatar
Alexandre Julliard committed
1176
{
1177
   HWND	hWnd = lphc->self;
Alexandre Julliard's avatar
Alexandre Julliard committed
1178

Alexandre Julliard's avatar
Alexandre Julliard committed
1179 1180 1181
   if( lphc->wState & CBF_FOCUSED )
   {
       CBRollUp( lphc, FALSE, TRUE );
1182
       if( IsWindow( hWnd ) )
Alexandre Julliard's avatar
Alexandre Julliard committed
1183 1184
       {
           if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1185
               SendMessageW(lphc->hWndLBox, LB_CARETOFF, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1186

Alexandre Julliard's avatar
Alexandre Julliard committed
1187 1188 1189
 	   lphc->wState &= ~CBF_FOCUSED;

           /* redraw text */
1190
	   if( !(lphc->wState & CBF_EDIT) )
1191
	     InvalidateRect(lphc->self, &lphc->textRect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1192

Alexandre Julliard's avatar
Alexandre Julliard committed
1193 1194 1195
           CB_NOTIFY( lphc, CBN_KILLFOCUS );
       }
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1196 1197
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1198
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1199
 *           COMBO_Command
Alexandre Julliard's avatar
Alexandre Julliard committed
1200
 */
1201
static LRESULT COMBO_Command( LPHEADCOMBO lphc, WPARAM wParam, HWND hWnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1202
{
1203
   if ( lphc->wState & CBF_EDIT && lphc->hWndEdit == hWnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1204
   {
Alexandre Julliard's avatar
Alexandre Julliard committed
1205
       /* ">> 8" makes gcc generate jump-table instead of cmp ladder */
Alexandre Julliard's avatar
Alexandre Julliard committed
1206

Alexandre Julliard's avatar
Alexandre Julliard committed
1207
       switch( HIWORD(wParam) >> 8 )
1208
       {
Alexandre Julliard's avatar
Alexandre Julliard committed
1209
	   case (EN_SETFOCUS >> 8):
Alexandre Julliard's avatar
Alexandre Julliard committed
1210

1211
               TRACE("[%p]: edit [%p] got focus\n", lphc->self, lphc->hWndEdit );
Alexandre Julliard's avatar
Alexandre Julliard committed
1212

1213
		COMBO_SetFocus( lphc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1214
	        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1215

Alexandre Julliard's avatar
Alexandre Julliard committed
1216
	   case (EN_KILLFOCUS >> 8):
Alexandre Julliard's avatar
Alexandre Julliard committed
1217

1218
               TRACE("[%p]: edit [%p] lost focus\n", lphc->self, lphc->hWndEdit );
Alexandre Julliard's avatar
Alexandre Julliard committed
1219

Alexandre Julliard's avatar
Alexandre Julliard committed
1220 1221
		/* NOTE: it seems that Windows' edit control sends an
		 * undocumented message WM_USER + 0x1B instead of this
1222
		 * notification (only when it happens to be a part of
Alexandre Julliard's avatar
Alexandre Julliard committed
1223 1224 1225 1226 1227
		 * the combo). ?? - AK.
		 */

		COMBO_KillFocus( lphc );
		break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1228

Alexandre Julliard's avatar
Alexandre Julliard committed
1229

Alexandre Julliard's avatar
Alexandre Julliard committed
1230
	   case (EN_CHANGE >> 8):
1231 1232
	       /*
	        * In some circumstances (when the selection of the combobox
1233
		* is changed for example) we don't want the EN_CHANGE notification
1234
		* to be forwarded to the parent of the combobox. This code
1235
		* checks a flag that is set in these occasions and ignores the
1236 1237
		* notification.
	        */
1238 1239 1240 1241 1242 1243
		if (lphc->wState & CBF_NOLBSELECT)
		{
		  lphc->wState &= ~CBF_NOLBSELECT;
		}
		else
		{
1244
		  CBUpdateLBox( lphc, lphc->wState & CBF_DROPPED );
1245
		}
1246 1247 1248

	        if (!(lphc->wState & CBF_NOEDITNOTIFY))
		  CB_NOTIFY( lphc, CBN_EDITCHANGE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1249 1250 1251
		break;

	   case (EN_UPDATE >> 8):
1252 1253
	        if (!(lphc->wState & CBF_NOEDITNOTIFY))
		  CB_NOTIFY( lphc, CBN_EDITUPDATE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1254 1255 1256 1257 1258 1259 1260 1261
		break;

	   case (EN_ERRSPACE >> 8):
		CB_NOTIFY( lphc, CBN_ERRSPACE );
       }
   }
   else if( lphc->hWndLBox == hWnd )
   {
1262
       switch( (short)HIWORD(wParam) )
Alexandre Julliard's avatar
Alexandre Julliard committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
       {
	   case LBN_ERRSPACE:
		CB_NOTIFY( lphc, CBN_ERRSPACE );
		break;

	   case LBN_DBLCLK:
		CB_NOTIFY( lphc, CBN_DBLCLK );
		break;

	   case LBN_SELCHANGE:
	   case LBN_SELCANCEL:

1275 1276
                TRACE("[%p]: lbox selection change [%x]\n", lphc->self, lphc->wState );

1277
                /* do not roll up if selection is being tracked
Austin English's avatar
Austin English committed
1278
                 * by arrow keys in the dropdown listbox */
1279 1280 1281 1282 1283 1284
                if (!(lphc->wState & CBF_NOROLLUP))
                {
                    CBRollUp( lphc, (HIWORD(wParam) == LBN_SELCHANGE), TRUE );
                }
                else lphc->wState &= ~CBF_NOROLLUP;

1285
		CB_NOTIFY( lphc, CBN_SELCHANGE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1286

1287
		if( HIWORD(wParam) == LBN_SELCHANGE)
1288
		{
1289 1290
		   if( lphc->wState & CBF_EDIT )
		   {
1291
		       INT index = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1292 1293 1294
		       lphc->wState |= CBF_NOLBSELECT;
		       CBUpdateEdit( lphc, index );
		       /* select text in edit, as Windows does */
1295
                      SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
1296 1297
		   }
		   else
1298
                   {
1299
		       InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1300 1301
                       UpdateWindow(lphc->self);
                   }
1302
		}
1303
                break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1304 1305 1306 1307 1308

	   case LBN_SETFOCUS:
	   case LBN_KILLFOCUS:
		/* nothing to do here since ComboLBox always resets the focus to its
		 * combo/edit counterpart */
Alexandre Julliard's avatar
Alexandre Julliard committed
1309
		 break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1310 1311 1312
       }
   }
   return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1313
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1314

Alexandre Julliard's avatar
Alexandre Julliard committed
1315
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1316 1317 1318
 *           COMBO_ItemOp
 *
 * Fixup an ownerdrawn item operation and pass it up to the combobox owner.
Alexandre Julliard's avatar
Alexandre Julliard committed
1319
 */
1320
static LRESULT COMBO_ItemOp( LPHEADCOMBO lphc, UINT msg, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1321
{
1322
   HWND hWnd = lphc->self;
1323
   UINT id = (UINT)GetWindowLongPtrW( hWnd, GWLP_ID );
Alexandre Julliard's avatar
Alexandre Julliard committed
1324

1325
   TRACE("[%p]: ownerdraw op %04x\n", lphc->self, msg );
Alexandre Julliard's avatar
Alexandre Julliard committed
1326

1327
   switch( msg )
Alexandre Julliard's avatar
Alexandre Julliard committed
1328
   {
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
   case WM_DELETEITEM:
       {
           DELETEITEMSTRUCT *lpIS = (DELETEITEMSTRUCT *)lParam;
           lpIS->CtlType  = ODT_COMBOBOX;
           lpIS->CtlID    = id;
           lpIS->hwndItem = hWnd;
           break;
       }
   case WM_DRAWITEM:
       {
           DRAWITEMSTRUCT *lpIS = (DRAWITEMSTRUCT *)lParam;
           lpIS->CtlType  = ODT_COMBOBOX;
           lpIS->CtlID    = id;
           lpIS->hwndItem = hWnd;
           break;
       }
   case WM_COMPAREITEM:
       {
           COMPAREITEMSTRUCT *lpIS = (COMPAREITEMSTRUCT *)lParam;
           lpIS->CtlType  = ODT_COMBOBOX;
           lpIS->CtlID    = id;
           lpIS->hwndItem = hWnd;
           break;
       }
   case WM_MEASUREITEM:
       {
           MEASUREITEMSTRUCT *lpIS = (MEASUREITEMSTRUCT *)lParam;
           lpIS->CtlType  = ODT_COMBOBOX;
           lpIS->CtlID    = id;
           break;
       }
Alexandre Julliard's avatar
Alexandre Julliard committed
1360
   }
1361
   return SendMessageW(lphc->owner, msg, id, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1362 1363
}

1364

Alexandre Julliard's avatar
Alexandre Julliard committed
1365
/***********************************************************************
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
 *           COMBO_GetTextW
 */
static LRESULT COMBO_GetTextW( LPHEADCOMBO lphc, INT count, LPWSTR buf )
{
    INT length;

    if( lphc->wState & CBF_EDIT )
        return SendMessageW( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );

    /* get it from the listbox */

    if (!count || !buf) return 0;
    if( lphc->hWndLBox )
    {
        INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
        if (idx == LB_ERR) goto error;
        length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
        if (length == LB_ERR) goto error;

        /* 'length' is without the terminating character */
        if (length >= count)
        {
            LPWSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
            if (!lpBuffer) goto error;
            length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);

            /* truncate if buffer is too short */
            if (length != LB_ERR)
            {
                lstrcpynW( buf, lpBuffer, count );
                length = count;
            }
            HeapFree( GetProcessHeap(), 0, lpBuffer );
        }
        else length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);

        if (length == LB_ERR) return 0;
        return length;
    }

 error:  /* error - truncate string, return zero */
    buf[0] = 0;
    return 0;
}


/***********************************************************************
 *           COMBO_GetTextA
1414 1415 1416
 *
 * NOTE! LB_GETTEXT does not count terminating \0, WM_GETTEXT does.
 *       also LB_GETTEXT might return values < 0, WM_GETTEXT doesn't.
Alexandre Julliard's avatar
Alexandre Julliard committed
1417
 */
1418
static LRESULT COMBO_GetTextA( LPHEADCOMBO lphc, INT count, LPSTR buf )
Alexandre Julliard's avatar
Alexandre Julliard committed
1419
{
1420
    INT length;
Alexandre Julliard's avatar
Alexandre Julliard committed
1421

1422 1423
    if( lphc->wState & CBF_EDIT )
        return SendMessageA( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
Alexandre Julliard's avatar
Alexandre Julliard committed
1424

1425
    /* get it from the listbox */
Alexandre Julliard's avatar
Alexandre Julliard committed
1426

1427 1428 1429
    if (!count || !buf) return 0;
    if( lphc->hWndLBox )
    {
1430
        INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
        if (idx == LB_ERR) goto error;
        length = SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
        if (length == LB_ERR) goto error;

        /* 'length' is without the terminating character */
        if (length >= count)
        {
            LPSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) );
            if (!lpBuffer) goto error;
            length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);

            /* truncate if buffer is too short */
            if (length != LB_ERR)
            {
                lstrcpynA( buf, lpBuffer, count );
                length = count;
            }
            HeapFree( GetProcessHeap(), 0, lpBuffer );
        }
        else length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1451

1452 1453 1454
        if (length == LB_ERR) return 0;
        return length;
    }
1455

1456 1457 1458
 error:  /* error - truncate string, return zero */
    buf[0] = 0;
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1459 1460
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1461

Alexandre Julliard's avatar
Alexandre Julliard committed
1462
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1463 1464
 *           CBResetPos
 *
1465
 * This function sets window positions according to the updated
Alexandre Julliard's avatar
Alexandre Julliard committed
1466
 * component placement struct.
Alexandre Julliard's avatar
Alexandre Julliard committed
1467
 */
1468
static void CBResetPos(
1469
  LPHEADCOMBO lphc,
1470 1471
  const RECT  *rectEdit,
  const RECT  *rectLB,
1472
  BOOL        bRedraw)
Alexandre Julliard's avatar
Alexandre Julliard committed
1473
{
1474
   BOOL	bDrop = (CB_GETTYPE(lphc) != CBS_SIMPLE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1475 1476 1477 1478 1479

   /* NOTE: logs sometimes have WM_LBUTTONUP before a cascade of
    * sizing messages */

   if( lphc->wState & CBF_EDIT )
1480
     SetWindowPos( lphc->hWndEdit, 0,
1481 1482 1483
		   rectEdit->left, rectEdit->top,
		   rectEdit->right - rectEdit->left,
		   rectEdit->bottom - rectEdit->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
1484 1485
                       SWP_NOZORDER | SWP_NOACTIVATE | ((bDrop) ? SWP_NOREDRAW : 0) );

1486 1487
   SetWindowPos( lphc->hWndLBox, 0,
		 rectLB->left, rectLB->top,
1488 1489
                 rectLB->right - rectLB->left,
		 rectLB->bottom - rectLB->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
1490 1491 1492 1493 1494 1495 1496
		   SWP_NOACTIVATE | SWP_NOZORDER | ((bDrop) ? SWP_NOREDRAW : 0) );

   if( bDrop )
   {
       if( lphc->wState & CBF_DROPPED )
       {
           lphc->wState &= ~CBF_DROPPED;
1497
           ShowWindow( lphc->hWndLBox, SW_HIDE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1498 1499
       }

Alexandre Julliard's avatar
Alexandre Julliard committed
1500
       if( bRedraw && !(lphc->wState & CBF_NOREDRAW) )
1501
           RedrawWindow( lphc->self, NULL, 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1502 1503
                           RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1504 1505
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1506

Alexandre Julliard's avatar
Alexandre Julliard committed
1507
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1508
 *           COMBO_Size
Alexandre Julliard's avatar
Alexandre Julliard committed
1509
 */
1510 1511 1512 1513 1514 1515 1516
static void COMBO_Size( LPHEADCOMBO lphc, LPARAM lParam )
{
  /*
   * Those controls are always the same height. So we have to make sure
   * they are not resized to another value.
   */
  if( CB_GETTYPE(lphc) != CBS_SIMPLE )
1517
  {
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
    int newComboHeight;

    newComboHeight = CBGetTextAreaHeight(lphc->self, lphc) + 2*COMBO_YBORDERSIZE();

    /*
     * Resizing a combobox has another side effect, it resizes the dropped
     * rectangle as well. However, it does it only if the new height for the
     * combobox is more than the height it should have. In other words,
     * if the application resizing the combobox only had the intention to resize
     * the actual control, for example, to do the layout of a dialog that is
     * resized, the height of the dropdown is not changed.
     */
    if( HIWORD(lParam) > newComboHeight )
    {
      TRACE("oldComboHeight=%d, newComboHeight=%d, oldDropBottom=%d, oldDropTop=%d\n",
            HIWORD(lParam), newComboHeight, lphc->droppedRect.bottom,
            lphc->droppedRect.top);
      lphc->droppedRect.bottom = lphc->droppedRect.top + HIWORD(lParam) - newComboHeight;
    }
    /*
     * Restore original height
     */
    if( HIWORD(lParam) != newComboHeight )
      SetWindowPos(lphc->self, 0, 0, 0, LOWORD(lParam), newComboHeight,
            SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOREDRAW);
  }

1545
  CBCalcPlacement(lphc->self,
1546 1547 1548
		  lphc,
		  &lphc->textRect,
		  &lphc->buttonRect,
1549
		  &lphc->droppedRect);
1550

1551
  CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1552 1553
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1554

Alexandre Julliard's avatar
Alexandre Julliard committed
1555
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1556
 *           COMBO_Font
Alexandre Julliard's avatar
Alexandre Julliard committed
1557
 */
1558
static void COMBO_Font( LPHEADCOMBO lphc, HFONT hFont, BOOL bRedraw )
Alexandre Julliard's avatar
Alexandre Julliard committed
1559
{
1560 1561 1562
  /*
   * Set the font
   */
Alexandre Julliard's avatar
Alexandre Julliard committed
1563 1564
  lphc->hFont = hFont;

1565 1566 1567
  /*
   * Propagate to owned windows.
   */
Alexandre Julliard's avatar
Alexandre Julliard committed
1568
  if( lphc->wState & CBF_EDIT )
1569 1570
      SendMessageW(lphc->hWndEdit, WM_SETFONT, (WPARAM)hFont, bRedraw);
  SendMessageW(lphc->hWndLBox, WM_SETFONT, (WPARAM)hFont, bRedraw);
Alexandre Julliard's avatar
Alexandre Julliard committed
1571

1572 1573 1574 1575 1576
  /*
   * Redo the layout of the control.
   */
  if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
  {
1577
    CBCalcPlacement(lphc->self,
1578 1579 1580
		    lphc,
		    &lphc->textRect,
		    &lphc->buttonRect,
1581
		    &lphc->droppedRect);
1582

1583 1584 1585 1586
    CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
  }
  else
  {
1587
    CBForceDummyResize(lphc);
1588
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1589 1590
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1591

Alexandre Julliard's avatar
Alexandre Julliard committed
1592
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1593
 *           COMBO_SetItemHeight
Alexandre Julliard's avatar
Alexandre Julliard committed
1594
 */
1595
static LRESULT COMBO_SetItemHeight( LPHEADCOMBO lphc, INT index, INT height )
Alexandre Julliard's avatar
Alexandre Julliard committed
1596
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1597 1598 1599 1600 1601 1602
   LRESULT	lRet = CB_ERR;

   if( index == -1 ) /* set text field height */
   {
       if( height < 32768 )
       {
1603
           lphc->editHeight = height + 2;  /* Is the 2 for 2*EDIT_CONTROL_PADDING? */
1604 1605 1606 1607 1608 1609

	 /*
	  * Redo the layout of the control.
	  */
	 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
	 {
1610
	   CBCalcPlacement(lphc->self,
1611 1612 1613
			   lphc,
			   &lphc->textRect,
			   &lphc->buttonRect,
1614
			   &lphc->droppedRect);
1615

1616 1617 1618 1619
	   CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
	 }
	 else
	 {
1620
	   CBForceDummyResize(lphc);
1621
	 }
1622

Alexandre Julliard's avatar
Alexandre Julliard committed
1623 1624
	   lRet = height;
       }
1625
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1626
   else if ( CB_OWNERDRAWN(lphc) )	/* set listbox item height */
1627
       lRet = SendMessageW(lphc->hWndLBox, LB_SETITEMHEIGHT, index, height);
Alexandre Julliard's avatar
Alexandre Julliard committed
1628
   return lRet;
Alexandre Julliard's avatar
Alexandre Julliard committed
1629 1630 1631
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1632
 *           COMBO_SelectString
Alexandre Julliard's avatar
Alexandre Julliard committed
1633
 */
1634
static LRESULT COMBO_SelectString( LPHEADCOMBO lphc, INT start, LPARAM pText, BOOL unicode )
Alexandre Julliard's avatar
Alexandre Julliard committed
1635
{
1636 1637
   INT index = unicode ? SendMessageW(lphc->hWndLBox, LB_SELECTSTRING, start, pText) :
                         SendMessageA(lphc->hWndLBox, LB_SELECTSTRING, start, pText);
1638 1639
   if( index >= 0 )
   {
1640 1641 1642 1643
     if( lphc->wState & CBF_EDIT )
       CBUpdateEdit( lphc, index );
     else
     {
1644
       InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1645
     }
Alexandre Julliard's avatar
Alexandre Julliard committed
1646
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1647
   return (LRESULT)index;
Alexandre Julliard's avatar
Alexandre Julliard committed
1648 1649 1650
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1651
 *           COMBO_LButtonDown
Alexandre Julliard's avatar
Alexandre Julliard committed
1652
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1653
static void COMBO_LButtonDown( LPHEADCOMBO lphc, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1654
{
1655
   POINT     pt;
1656
   BOOL      bButton;
1657
   HWND      hWnd = lphc->self;
Alexandre Julliard's avatar
Alexandre Julliard committed
1658

1659 1660
   pt.x = (short)LOWORD(lParam);
   pt.y = (short)HIWORD(lParam);
1661 1662
   bButton = PtInRect(&lphc->buttonRect, pt);

Alexandre Julliard's avatar
Alexandre Julliard committed
1663 1664 1665 1666 1667 1668 1669 1670
   if( (CB_GETTYPE(lphc) == CBS_DROPDOWNLIST) ||
       (bButton && (CB_GETTYPE(lphc) == CBS_DROPDOWN)) )
   {
       lphc->wState |= CBF_BUTTONDOWN;
       if( lphc->wState & CBF_DROPPED )
       {
	   /* got a click to cancel selection */

1671
           lphc->wState &= ~CBF_BUTTONDOWN;
Alexandre Julliard's avatar
Alexandre Julliard committed
1672
           CBRollUp( lphc, TRUE, FALSE );
1673
	   if( !IsWindow( hWnd ) ) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685

           if( lphc->wState & CBF_CAPTURE )
           {
               lphc->wState &= ~CBF_CAPTURE;
               ReleaseCapture();
           }
       }
       else
       {
	   /* drop down the listbox and start tracking */

           lphc->wState |= CBF_CAPTURE;
1686
           SetCapture( hWnd );
1687
           CBDropDown( lphc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1688 1689 1690
       }
       if( bButton ) CBRepaintButton( lphc );
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1691 1692 1693
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1694 1695 1696
 *           COMBO_LButtonUp
 *
 * Release capture and stop tracking if needed.
Alexandre Julliard's avatar
Alexandre Julliard committed
1697
 */
1698
static void COMBO_LButtonUp( LPHEADCOMBO lphc )
Alexandre Julliard's avatar
Alexandre Julliard committed
1699
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1700 1701 1702 1703 1704
   if( lphc->wState & CBF_CAPTURE )
   {
       lphc->wState &= ~CBF_CAPTURE;
       if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
       {
1705
	   INT index = CBUpdateLBox( lphc, TRUE );
1706 1707 1708 1709 1710 1711 1712
	   /* Update edit only if item is in the list */
	   if(index >= 0)
	   {
	       lphc->wState |= CBF_NOLBSELECT;
	       CBUpdateEdit( lphc, index );
	       lphc->wState &= ~CBF_NOLBSELECT;
	   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1713 1714
       }
       ReleaseCapture();
1715
       SetCapture(lphc->hWndLBox);
Alexandre Julliard's avatar
Alexandre Julliard committed
1716
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1717

Alexandre Julliard's avatar
Alexandre Julliard committed
1718 1719 1720 1721 1722
   if( lphc->wState & CBF_BUTTONDOWN )
   {
       lphc->wState &= ~CBF_BUTTONDOWN;
       CBRepaintButton( lphc );
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1723 1724 1725
}

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1726 1727 1728 1729
 *           COMBO_MouseMove
 *
 * Two things to do - track combo button and release capture when
 * pointer goes into the listbox.
Alexandre Julliard's avatar
Alexandre Julliard committed
1730
 */
1731
static void COMBO_MouseMove( LPHEADCOMBO lphc, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1732
{
1733
   POINT  pt;
1734
   RECT   lbRect;
1735

1736 1737
   pt.x = (short)LOWORD(lParam);
   pt.y = (short)HIWORD(lParam);
1738

Alexandre Julliard's avatar
Alexandre Julliard committed
1739 1740
   if( lphc->wState & CBF_BUTTONDOWN )
   {
1741
     BOOL bButton;
Alexandre Julliard's avatar
Alexandre Julliard committed
1742

1743 1744 1745 1746 1747 1748 1749
     bButton = PtInRect(&lphc->buttonRect, pt);

     if( !bButton )
     {
       lphc->wState &= ~CBF_BUTTONDOWN;
       CBRepaintButton( lphc );
     }
Alexandre Julliard's avatar
Alexandre Julliard committed
1750
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1751

1752
   GetClientRect( lphc->hWndLBox, &lbRect );
1753
   MapWindowPoints( lphc->self, lphc->hWndLBox, &pt, 1 );
1754
   if( PtInRect(&lbRect, pt) )
Alexandre Julliard's avatar
Alexandre Julliard committed
1755 1756 1757
   {
       lphc->wState &= ~CBF_CAPTURE;
       ReleaseCapture();
1758
       if( CB_GETTYPE(lphc) == CBS_DROPDOWN ) CBUpdateLBox( lphc, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1759

Alexandre Julliard's avatar
Alexandre Julliard committed
1760
       /* hand over pointer tracking */
1761
       SendMessageW(lphc->hWndLBox, WM_LBUTTONDOWN, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1762
   }
Alexandre Julliard's avatar
Alexandre Julliard committed
1763 1764
}

1765
static LRESULT COMBO_GetComboBoxInfo(const HEADCOMBO *lphc, COMBOBOXINFO *pcbi)
1766 1767 1768 1769 1770 1771 1772
{
    if (!pcbi || (pcbi->cbSize < sizeof(COMBOBOXINFO)))
        return FALSE;

    pcbi->rcItem = lphc->textRect;
    pcbi->rcButton = lphc->buttonRect;
    pcbi->stateButton = 0;
1773
    if (lphc->wState & CBF_BUTTONDOWN)
1774 1775 1776 1777 1778 1779 1780 1781 1782
        pcbi->stateButton |= STATE_SYSTEM_PRESSED;
    if (IsRectEmpty(&lphc->buttonRect))
        pcbi->stateButton |= STATE_SYSTEM_INVISIBLE;
    pcbi->hwndCombo = lphc->self;
    pcbi->hwndItem = lphc->hWndEdit;
    pcbi->hwndList = lphc->hWndLBox;
    return TRUE;
}

1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
static char *strdupA(LPCSTR str)
{
    char *ret;
    DWORD len;

    if(!str) return NULL;

    len = strlen(str);
    ret = HeapAlloc(GetProcessHeap(), 0, len + 1);
    memcpy(ret, str, len + 1);
    return ret;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1795 1796

/***********************************************************************
1797
 *           ComboWndProc_common
Alexandre Julliard's avatar
Alexandre Julliard committed
1798
 */
1799
LRESULT ComboWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
Alexandre Julliard's avatar
Alexandre Julliard committed
1800
{
1801
      LPHEADCOMBO lphc = (LPHEADCOMBO)GetWindowLongPtrW( hwnd, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1802

1803
      TRACE("[%p]: msg %s wp %08lx lp %08lx\n",
1804
            hwnd, SPY_GetMsgName(message, hwnd), wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1805

1806 1807
      if (!IsWindow(hwnd)) return 0;

Alexandre Julliard's avatar
Alexandre Julliard committed
1808
      if( lphc || message == WM_NCCREATE )
1809 1810
      switch(message)
      {
Alexandre Julliard's avatar
Alexandre Julliard committed
1811 1812 1813

	/* System messages */

1814
     	case WM_NCCREATE:
1815 1816 1817
	{
		LONG style = unicode ? ((LPCREATESTRUCTW)lParam)->style :
				       ((LPCREATESTRUCTA)lParam)->style;
1818
                return COMBO_NCCreate(hwnd, style);
1819
	}
1820
     	case WM_NCDESTROY:
Alexandre Julliard's avatar
Alexandre Julliard committed
1821
		COMBO_NCDestroy(lphc);
1822
		break;/* -> DefWindowProc */
Alexandre Julliard's avatar
Alexandre Julliard committed
1823

1824 1825
     	case WM_CREATE:
	{
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
		HWND hwndParent;
		LONG style;
		if(unicode)
		{
		    hwndParent = ((LPCREATESTRUCTW)lParam)->hwndParent;
		    style = ((LPCREATESTRUCTW)lParam)->style;
		}
		else
		{
		    hwndParent = ((LPCREATESTRUCTA)lParam)->hwndParent;
		    style = ((LPCREATESTRUCTA)lParam)->style;
		}
1838
                return COMBO_Create(hwnd, lphc, hwndParent, style, unicode);
1839
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1840

1841 1842
        case WM_PRINTCLIENT:
		/* Fallthrough */
Alexandre Julliard's avatar
Alexandre Julliard committed
1843 1844
     	case WM_PAINT:
		/* wParam may contain a valid HDC! */
1845
		return  COMBO_Paint(lphc, (HDC)wParam);
1846

Alexandre Julliard's avatar
Alexandre Julliard committed
1847
	case WM_ERASEBKGND:
1848 1849 1850
                /* do all painting in WM_PAINT like Windows does */
                return 1;

1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
	case WM_GETDLGCODE:
	{
		LRESULT result = DLGC_WANTARROWS | DLGC_WANTCHARS;
		if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
		{
		   int vk = (int)((LPMSG)lParam)->wParam;

		   if ((vk == VK_RETURN || vk == VK_ESCAPE) && (lphc->wState & CBF_DROPPED))
		       result |= DLGC_WANTMESSAGE;
		}
		return  result;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1863
	case WM_SIZE:
1864
	        if( lphc->hWndLBox &&
1865
		  !(lphc->wState & CBF_NORESIZE) ) COMBO_Size( lphc, lParam );
1866
		return  TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1867
	case WM_SETFONT:
1868
		COMBO_Font( lphc, (HFONT)wParam, (BOOL)lParam );
1869
		return  TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1870
	case WM_GETFONT:
1871
		return  (LRESULT)lphc->hFont;
Alexandre Julliard's avatar
Alexandre Julliard committed
1872
	case WM_SETFOCUS:
1873 1874 1875 1876 1877 1878 1879 1880
               if( lphc->wState & CBF_EDIT ) {
                   SetFocus( lphc->hWndEdit );
                   /* The first time focus is received, select all the text */
                   if( !(lphc->wState & CBF_BEENFOCUSED) ) {
                       SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
                       lphc->wState |= CBF_BEENFOCUSED;
                   }
               }
Alexandre Julliard's avatar
Alexandre Julliard committed
1881 1882
		else
		    COMBO_SetFocus( lphc );
1883
		return  TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1884
	case WM_KILLFOCUS:
1885 1886
            {
                HWND hwndFocus = WIN_GetFullHandle( (HWND)wParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1887 1888 1889
		if( !hwndFocus ||
		    (hwndFocus != lphc->hWndEdit && hwndFocus != lphc->hWndLBox ))
		    COMBO_KillFocus( lphc );
1890
		return  TRUE;
1891
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1892
	case WM_COMMAND:
1893
		return  COMBO_Command( lphc, wParam, WIN_GetFullHandle( (HWND)lParam ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1894
	case WM_GETTEXT:
1895 1896
            return unicode ? COMBO_GetTextW( lphc, wParam, (LPWSTR)lParam )
                           : COMBO_GetTextA( lphc, wParam, (LPSTR)lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1897 1898 1899
	case WM_SETTEXT:
	case WM_GETTEXTLENGTH:
	case WM_CLEAR:
1900 1901
                if ((message == WM_GETTEXTLENGTH) && !ISWIN31 && !(lphc->wState & CBF_EDIT))
                {
1902 1903 1904 1905
                    int j = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
                    if (j == -1) return 0;
                    return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, j, 0) :
                                     SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, j, 0);
1906
                }
1907
		else if( lphc->wState & CBF_EDIT )
1908
		{
1909
		    LRESULT ret;
1910
		    lphc->wState |= CBF_NOEDITNOTIFY;
1911 1912
		    ret = unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
				    SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1913 1914
		    lphc->wState &= ~CBF_NOEDITNOTIFY;
		    return ret;
1915
		}
1916
		else return CB_ERR;
1917 1918 1919
	case WM_CUT:
        case WM_PASTE:
	case WM_COPY:
1920
		if( lphc->wState & CBF_EDIT )
1921 1922 1923 1924
		{
		    return unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
				     SendMessageA(lphc->hWndEdit, message, wParam, lParam);
		}
1925
		else return  CB_ERR;
1926

Alexandre Julliard's avatar
Alexandre Julliard committed
1927 1928 1929 1930
	case WM_DRAWITEM:
	case WM_DELETEITEM:
	case WM_COMPAREITEM:
	case WM_MEASUREITEM:
1931
		return COMBO_ItemOp(lphc, message, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1932 1933
	case WM_ENABLE:
		if( lphc->wState & CBF_EDIT )
1934
		    EnableWindow( lphc->hWndEdit, (BOOL)wParam );
1935
		EnableWindow( lphc->hWndLBox, (BOOL)wParam );
1936 1937

		/* Force the control to repaint when the enabled state changes. */
1938
		InvalidateRect(lphc->self, NULL, TRUE);
1939
		return  TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1940
	case WM_SETREDRAW:
Alexandre Julliard's avatar
Alexandre Julliard committed
1941 1942 1943 1944 1945
		if( wParam )
		    lphc->wState &= ~CBF_NOREDRAW;
		else
		    lphc->wState |= CBF_NOREDRAW;

Alexandre Julliard's avatar
Alexandre Julliard committed
1946
		if( lphc->wState & CBF_EDIT )
1947 1948
		    SendMessageW(lphc->hWndEdit, message, wParam, lParam);
		SendMessageW(lphc->hWndLBox, message, wParam, lParam);
1949
		return  0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1950 1951 1952
	case WM_SYSKEYDOWN:
		if( KEYDATA_ALT & HIWORD(lParam) )
		    if( wParam == VK_UP || wParam == VK_DOWN )
1953 1954
			COMBO_FlipListbox( lphc, FALSE, FALSE );
                return  0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1955 1956

	case WM_KEYDOWN:
1957
		if ((wParam == VK_RETURN || wParam == VK_ESCAPE) &&
1958 1959
		     (lphc->wState & CBF_DROPPED))
		{
1960
		   CBRollUp( lphc, wParam == VK_RETURN, FALSE );
1961 1962
		   return TRUE;
		}
1963 1964 1965 1966 1967
               else if ((wParam == VK_F4) && !(lphc->wState & CBF_EUI))
               {
                  COMBO_FlipListbox( lphc, FALSE, FALSE );
                  return TRUE;
               }
1968 1969 1970 1971 1972
               /* fall through */
	case WM_CHAR:
	case WM_IME_CHAR:
	{
		HWND hwndTarget;
1973

Alexandre Julliard's avatar
Alexandre Julliard committed
1974
		if( lphc->wState & CBF_EDIT )
1975
		    hwndTarget = lphc->hWndEdit;
Alexandre Julliard's avatar
Alexandre Julliard committed
1976
		else
1977 1978 1979 1980 1981
		    hwndTarget = lphc->hWndLBox;

		return unicode ? SendMessageW(hwndTarget, message, wParam, lParam) :
				 SendMessageA(hwndTarget, message, wParam, lParam);
	}
1982
	case WM_LBUTTONDOWN:
1983
		if( !(lphc->wState & CBF_FOCUSED) ) SetFocus( lphc->self );
Alexandre Julliard's avatar
Alexandre Julliard committed
1984
		if( lphc->wState & CBF_FOCUSED ) COMBO_LButtonDown( lphc, lParam );
1985
		return  TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1986
	case WM_LBUTTONUP:
1987
		COMBO_LButtonUp( lphc );
1988
		return  TRUE;
1989 1990
	case WM_MOUSEMOVE:
		if( lphc->wState & CBF_CAPTURE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1991
		    COMBO_MouseMove( lphc, wParam, lParam );
1992
		return  TRUE;
1993 1994 1995

        case WM_MOUSEWHEEL:
                if (wParam & (MK_SHIFT | MK_CONTROL))
1996 1997
                    return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
				     DefWindowProcA(hwnd, message, wParam, lParam);
1998 1999 2000

                if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_UP, 0);
                if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_DOWN, 0);
2001 2002
                return TRUE;

Alexandre Julliard's avatar
Alexandre Julliard committed
2003 2004
	/* Combo messages */

2005
	case CB_ADDSTRING:
2006 2007 2008
		if( unicode )
                {
                    if( lphc->dwStyle & CBS_LOWERCASE )
2009
                        CharLowerW((LPWSTR)lParam);
2010
                    else if( lphc->dwStyle & CBS_UPPERCASE )
2011
                        CharUpperW((LPWSTR)lParam);
2012 2013
                    return SendMessageW(lphc->hWndLBox, LB_ADDSTRING, 0, lParam);
                }
2014 2015
                else /* unlike the unicode version, the ansi version does not overwrite
                        the string if converting case */
2016
                {
2017
                    char *string = NULL;
2018
                    LRESULT ret;
2019
                    if( lphc->dwStyle & CBS_LOWERCASE )
2020 2021
                    {
                        string = strdupA((LPSTR)lParam);
2022
                        CharLowerA(string);
2023
                    }
2024

2025
                    else if( lphc->dwStyle & CBS_UPPERCASE )
2026 2027
                    {
                        string = strdupA((LPSTR)lParam);
2028
                        CharUpperA(string);
2029
                    }
2030

2031 2032 2033
                    ret = SendMessageA(lphc->hWndLBox, LB_ADDSTRING, 0, string ? (LPARAM)string : lParam);
                    HeapFree(GetProcessHeap(), 0, string);
                    return ret;
2034
                }
2035
	case CB_INSERTSTRING:
2036 2037 2038
		if( unicode )
                {
                    if( lphc->dwStyle & CBS_LOWERCASE )
2039
                        CharLowerW((LPWSTR)lParam);
2040
                    else if( lphc->dwStyle & CBS_UPPERCASE )
2041
                        CharUpperW((LPWSTR)lParam);
2042 2043 2044 2045 2046
                    return SendMessageW(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
                }
                else
                {
                    if( lphc->dwStyle & CBS_LOWERCASE )
2047
                        CharLowerA((LPSTR)lParam);
2048
                    else if( lphc->dwStyle & CBS_UPPERCASE )
2049 2050
                        CharUpperA((LPSTR)lParam);

2051 2052
                    return SendMessageA(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
                }
2053
	case CB_DELETESTRING:
2054 2055
		return unicode ? SendMessageW(lphc->hWndLBox, LB_DELETESTRING, wParam, 0) :
				 SendMessageA(lphc->hWndLBox, LB_DELETESTRING, wParam, 0);
2056
	case CB_SELECTSTRING:
2057
		return COMBO_SelectString(lphc, (INT)wParam, lParam, unicode);
2058
	case CB_FINDSTRING:
2059 2060
		return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam) :
				 SendMessageA(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam);
2061
	case CB_FINDSTRINGEXACT:
2062 2063
		return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam) :
				 SendMessageA(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam);
2064
	case CB_SETITEMHEIGHT:
2065
		return  COMBO_SetItemHeight( lphc, (INT)wParam, (INT)lParam);
2066 2067
	case CB_GETITEMHEIGHT:
		if( (INT)wParam >= 0 )	/* listbox item */
2068
                    return SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, wParam, 0);
2069
                return  CBGetTextAreaHeight(hwnd, lphc);
2070
	case CB_RESETCONTENT:
2071
		SendMessageW(lphc->hWndLBox, LB_RESETCONTENT, 0, 0);
2072
                if( (lphc->wState & CBF_EDIT) && CB_HASSTRINGS(lphc) )
2073 2074 2075 2076
		{
		    static const WCHAR empty_stringW[] = { 0 };
                    SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, (LPARAM)empty_stringW);
		}
2077
                else
2078
                    InvalidateRect(lphc->self, NULL, TRUE);
2079
		return  TRUE;
2080
	case CB_INITSTORAGE:
2081
		return SendMessageW(lphc->hWndLBox, LB_INITSTORAGE, wParam, lParam);
2082
	case CB_GETHORIZONTALEXTENT:
2083
		return SendMessageW(lphc->hWndLBox, LB_GETHORIZONTALEXTENT, 0, 0);
2084
	case CB_SETHORIZONTALEXTENT:
2085
		return SendMessageW(lphc->hWndLBox, LB_SETHORIZONTALEXTENT, wParam, 0);
2086
	case CB_GETTOPINDEX:
2087
		return SendMessageW(lphc->hWndLBox, LB_GETTOPINDEX, 0, 0);
2088
	case CB_GETLOCALE:
2089
		return SendMessageW(lphc->hWndLBox, LB_GETLOCALE, 0, 0);
2090
	case CB_SETLOCALE:
2091
		return SendMessageW(lphc->hWndLBox, LB_SETLOCALE, wParam, 0);
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
	case CB_SETDROPPEDWIDTH:
		if( (CB_GETTYPE(lphc) == CBS_SIMPLE) ||
		    (INT)wParam >= 32768 )
		    return CB_ERR;
		/* new value must be higher than combobox width */
		if((INT)wParam >= lphc->droppedRect.right - lphc->droppedRect.left)
		    lphc->droppedWidth = wParam;
		else if(wParam)
		    lphc->droppedWidth = 0;

		/* recalculate the combobox area */
		CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );

		/* fall through */
2106
	case CB_GETDROPPEDWIDTH:
Alexandre Julliard's avatar
Alexandre Julliard committed
2107
		if( lphc->droppedWidth )
2108 2109
                    return  lphc->droppedWidth;
		return  lphc->droppedRect.right - lphc->droppedRect.left;
2110 2111
	case CB_GETDROPPEDCONTROLRECT:
		if( lParam ) CBGetDroppedControlRect(lphc, (LPRECT)lParam );
2112
		return  CB_OKAY;
2113
	case CB_GETDROPPEDSTATE:
2114
		return (lphc->wState & CBF_DROPPED) != 0;
2115
	case CB_DIR:
Carlos Lozano's avatar
Carlos Lozano committed
2116 2117
		return unicode ? SendMessageW(lphc->hWndLBox, LB_DIR, wParam, lParam) :
				 SendMessageA(lphc->hWndLBox, LB_DIR, wParam, lParam);
2118

2119
	case CB_SHOWDROPDOWN:
2120 2121 2122 2123
		if( CB_GETTYPE(lphc) != CBS_SIMPLE )
		{
		    if( wParam )
		    {
Alexandre Julliard's avatar
Alexandre Julliard committed
2124 2125
			if( !(lphc->wState & CBF_DROPPED) )
			    CBDropDown( lphc );
2126
		    }
2127 2128
		    else
			if( lphc->wState & CBF_DROPPED )
Alexandre Julliard's avatar
Alexandre Julliard committed
2129
		            CBRollUp( lphc, FALSE, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
2130
		}
2131
		return  TRUE;
2132
	case CB_GETCOUNT:
2133
		return SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
2134
	case CB_GETCURSEL:
2135
		return SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
2136
	case CB_SETCURSEL:
2137
		lParam = SendMessageW(lphc->hWndLBox, LB_SETCURSEL, wParam, 0);
2138
	        if( lParam >= 0 )
2139
	            SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, wParam, 0);
2140 2141 2142 2143 2144

		/* no LBN_SELCHANGE in this case, update manually */
		if( lphc->wState & CBF_EDIT )
		    CBUpdateEdit( lphc, (INT)wParam );
		else
2145
		    InvalidateRect(lphc->self, &lphc->textRect, TRUE);
2146
		lphc->wState &= ~CBF_SELCHANGE;
2147
	        return  lParam;
2148
	case CB_GETLBTEXT:
2149 2150
		return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXT, wParam, lParam) :
				 SendMessageA(lphc->hWndLBox, LB_GETTEXT, wParam, lParam);
2151
	case CB_GETLBTEXTLEN:
2152 2153
                return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0) :
                                 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0);
2154
	case CB_GETITEMDATA:
2155
		return SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, wParam, 0);
2156
	case CB_SETITEMDATA:
2157
		return SendMessageW(lphc->hWndLBox, LB_SETITEMDATA, wParam, lParam);
2158
	case CB_GETEDITSEL:
2159
		/* Edit checks passed parameters itself */
Alexandre Julliard's avatar
Alexandre Julliard committed
2160
		if( lphc->wState & CBF_EDIT )
2161
		    return SendMessageW(lphc->hWndEdit, EM_GETSEL, wParam, lParam);
2162
		return  CB_ERR;
2163
	case CB_SETEDITSEL:
2164
		if( lphc->wState & CBF_EDIT )
2165
                    return SendMessageW(lphc->hWndEdit, EM_SETSEL,
2166
			  (INT)(SHORT)LOWORD(lParam), (INT)(SHORT)HIWORD(lParam) );
2167
		return  CB_ERR;
2168
	case CB_SETEXTENDEDUI:
2169
                if( CB_GETTYPE(lphc) == CBS_SIMPLE )
2170
                    return  CB_ERR;
Alexandre Julliard's avatar
Alexandre Julliard committed
2171 2172 2173
		if( wParam )
		    lphc->wState |= CBF_EUI;
		else lphc->wState &= ~CBF_EUI;
2174
		return  CB_OKAY;
2175
	case CB_GETEXTENDEDUI:
2176
		return (lphc->wState & CBF_EUI) != 0;
2177 2178
	case CB_GETCOMBOBOXINFO:
		return COMBO_GetComboBoxInfo(lphc, (COMBOBOXINFO *)lParam);
2179 2180 2181
	case CB_LIMITTEXT:
		if( lphc->wState & CBF_EDIT )
			return SendMessageW(lphc->hWndEdit, EM_LIMITTEXT, wParam, lParam);
2182
		return  TRUE;
2183 2184
	default:
		if (message >= WM_USER)
2185
		    WARN("unknown msg WM_USER+%04x wp=%04lx lp=%08lx\n",
2186 2187
			message - WM_USER, wParam, lParam );
		break;
2188 2189 2190
      }
      return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
                       DefWindowProcA(hwnd, message, wParam, lParam);
2191 2192
}

2193 2194 2195
/*************************************************************************
 *           GetComboBoxInfo   (USER32.@)
 */
2196 2197
BOOL WINAPI GetComboBoxInfo(HWND hwndCombo,      /* [in] handle to combo box */
			    PCOMBOBOXINFO pcbi   /* [in/out] combo box information */)
2198
{
2199 2200
    TRACE("(%p, %p)\n", hwndCombo, pcbi);
    return SendMessageW(hwndCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)pcbi);
2201
}