1 /*
   2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "awt_List.h"
  27 #include "awt_Canvas.h"
  28 #include "awt_Dimension.h"
  29 #include "awt_Toolkit.h"
  30 #include "awt_Window.h"
  31 
  32 #include "ComCtl32Util.h"
  33 
  34 /* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code.
  35  */
  36 
  37 /***********************************************************************/
  38 // struct for _AddItems() method
  39 struct AddItemsStruct {
  40     jobject list;
  41     jobjectArray items;
  42     jint index;
  43     jint width;
  44 };
  45 // struct for _DelItems() method
  46 struct DelItemsStruct {
  47     jobject list;
  48     jint start, end;
  49 };
  50 // struct for _IsSelected(), _Select(), _Deselect() and _MakeVisible() methods
  51 struct SelectElementStruct {
  52     jobject list;
  53     jint index;
  54 };
  55 // struct for _SetMultipleSelections() method
  56 struct SetMultipleSelectionsStruct {
  57     jobject list;
  58     jboolean on;
  59 };
  60 /************************************************************************
  61  * AwtList methods
  62  */
  63 
  64 AwtList::AwtList() {
  65     isMultiSelect = FALSE;
  66     isWrapperPrint = FALSE;
  67 }
  68 
  69 AwtList::~AwtList()
  70 {
  71 }
  72 
  73 LPCTSTR AwtList::GetClassName() {
  74     return TEXT("LISTBOX");
  75 }
  76 
  77 /* Create a new AwtList object and window.   */
  78 AwtList* AwtList::Create(jobject peer, jobject parent)
  79 {
  80     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
  81 
  82     jobject target = NULL;
  83     AwtList* c = NULL;
  84 
  85     try {
  86         if (env->EnsureLocalCapacity(1) < 0) {
  87             return NULL;
  88         }
  89 
  90         PDATA pData;
  91         AwtCanvas* awtParent;
  92 
  93         JNI_CHECK_PEER_GOTO(parent, done);
  94         awtParent = (AwtCanvas*)pData;
  95 
  96         /* target is Hjava_awt_List * */
  97         target = env->GetObjectField(peer, AwtObject::targetID);
  98         JNI_CHECK_NULL_GOTO(target, "null target", done);
  99 
 100         c = new AwtList();
 101 
 102         {
 103 
 104             /*
 105              * NOTE: WS_CLIPCHILDREN is excluded so that repaint requests
 106              * from Java will pass through the wrap to the native listbox.
 107              */
 108             DWORD wrapStyle = WS_CHILD | WS_CLIPSIBLINGS;
 109             DWORD wrapExStyle = 0;
 110 
 111             DWORD style = WS_CHILD | WS_CLIPSIBLINGS | WS_VSCROLL | WS_HSCROLL |
 112                 LBS_NOINTEGRALHEIGHT | LBS_NOTIFY | LBS_OWNERDRAWFIXED;
 113             DWORD exStyle = WS_EX_CLIENTEDGE;
 114 
 115             /*
 116              * NOTE: WS_VISIBLE is always set for the listbox. Listbox
 117              * visibility is controlled by toggling the wrap's WS_VISIBLE bit.
 118              */
 119             style |= WS_VISIBLE;
 120 
 121             if (GetRTL()) {
 122                 exStyle |= WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR;
 123                 if (GetRTLReadingOrder())
 124                     exStyle |= WS_EX_RTLREADING;
 125             }
 126 
 127             jint x = env->GetIntField(target, AwtComponent::xID);
 128             jint y = env->GetIntField(target, AwtComponent::yID);
 129             jint width = env->GetIntField(target, AwtComponent::widthID);
 130             jint height = env->GetIntField(target, AwtComponent::heightID);
 131 
 132             c->CreateHWnd(env, L"", style, exStyle,
 133                           x, y, width, height,
 134                           awtParent->GetHWnd(),
 135                           NULL,
 136                           ::GetSysColor(COLOR_WINDOWTEXT),
 137                           ::GetSysColor(COLOR_WINDOW),
 138                           peer
 139                           );
 140 
 141             /* suppress inheriting awtParent's color. */
 142             c->m_backgroundColorSet = TRUE;
 143             c->UpdateBackground(env, target);
 144         }
 145     } catch (...) {
 146         env->DeleteLocalRef(target);
 147         throw;
 148     }
 149 
 150 done:
 151     env->DeleteLocalRef(target);
 152     return c;
 153 }
 154 
 155 void AwtList::SetDragCapture(UINT flags)
 156 {
 157     // don't want to interfere with other controls
 158     if (::GetCapture() == NULL) {
 159         ::SetCapture(GetListHandle());
 160     }
 161 }
 162 
 163 void AwtList::ReleaseDragCapture(UINT flags)
 164 {
 165     if ((::GetCapture() == GetListHandle()) && ((flags & ALL_MK_BUTTONS) == 0)) {
 166         ::ReleaseCapture();
 167     }
 168 }
 169 
 170 void AwtList::Reshape(int x, int y, int w, int h)
 171 {
 172     AwtComponent::Reshape(x, y, w, h);
 173 
 174 /*
 175     HWND hList = GetListHandle();
 176     if (hList != NULL) {
 177         long flags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOCOPYBITS;
 178         /*
 179          * Fix for bug 4046446.
 180          * /
 181         SetWindowPos(hList, 0, 0, 0, w, h, flags);
 182     }
 183 */
 184 }
 185 
 186 //Netscape : Override the AwtComponent method so we can set the item height
 187 //for each item in the list.  Modified by echawkes to avoid race condition.
 188 
 189 void AwtList::SetFont(AwtFont* font)
 190 {
 191     DASSERT(font != NULL);
 192     if (font->GetAscent() < 0)
 193     {
 194         AwtFont::SetupAscent(font);
 195     }
 196     HANDLE hFont = font->GetHFont();
 197     SendListMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE, 0));
 198 
 199     HDC hDC = ::GetDC(GetHWnd());
 200 
 201     TEXTMETRIC tm;
 202     VERIFY(::SelectObject(hDC, hFont) != NULL);
 203     VERIFY(::GetTextMetrics(hDC, &tm));
 204 
 205     ::ReleaseDC(GetHWnd(), hDC);
 206 
 207     long h = tm.tmHeight + tm.tmExternalLeading;
 208     // Listbox is LBS_OWNERDRAWFIXED so the items have the same height
 209     VERIFY(SendListMessage(LB_SETITEMHEIGHT, 0, MAKELPARAM(h, 0)) != LB_ERR);
 210     VERIFY(::RedrawWindow(GetHWnd(), NULL, NULL, RDW_INVALIDATE |RDW_FRAME |RDW_ERASE));
 211 }
 212 
 213 void AwtList::SetMultiSelect(BOOL ms) {
 214     if (ms == isMultiSelect) {
 215         return;
 216     }
 217 
 218     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 219 
 220     /* Copy current box's contents to string array */
 221     const int nCount = GetCount();
 222     LPTSTR * strings = new LPTSTR[nCount];
 223     int i;
 224 
 225     for(i = 0; i < nCount; i++) {
 226         LRESULT len = SendListMessage(LB_GETTEXTLEN, i);
 227         LPTSTR text = NULL;
 228         try {
 229             text = new TCHAR[len + 1];
 230         } catch (std::bad_alloc&) {
 231             // free char * already allocated
 232             for (int j = 0; j < i; j++) {
 233                 delete [] strings[j];
 234             }
 235             delete [] strings;
 236             throw;
 237         }
 238 
 239         VERIFY(SendListMessage(LB_GETTEXT, i, (LPARAM)text) != LB_ERR);
 240         strings[i] = text;
 241     }
 242 
 243     // index for selected item after multi-select mode change
 244     int toSelect = SendListMessage(LB_GETCURSEL);
 245     if (!isMultiSelect)
 246     {
 247         // MSDN: for single-select lists LB_GETCURSEL returns
 248         // index of selected item or LB_ERR if no item is selected
 249         if (toSelect == LB_ERR)
 250         {
 251             toSelect = -1;
 252         }
 253     }
 254     else
 255     {
 256         // MSDN: for multi-select lists LB_GETCURSEL returns index
 257         // of the focused item or 0 if no items are selected; if
 258         // some item has focus and is not selected then LB_GETCURSEL
 259         // return its index, so we need IsItemSelected too
 260         if ((toSelect == LB_ERR) ||
 261             (SendListMessage(LB_GETSELCOUNT) == 0) ||
 262             (IsItemSelected(toSelect) == 0))
 263         {
 264             toSelect = -1;
 265         }
 266     }
 267 
 268     isMultiSelect = ms;
 269 
 270     HWND parentHWnd = GetParent()->GetHWnd();
 271 
 272     /* Save old list box's attributes */
 273     RECT rect;
 274     GetWindowRect(GetListHandle(), &rect);
 275     MapWindowPoints(0, parentHWnd, (LPPOINT)&rect, 2);
 276 
 277     HANDLE font = (HANDLE)SendListMessage(WM_GETFONT);
 278     LRESULT itemHeight = SendListMessage(LB_GETITEMHEIGHT, 0);
 279     DWORD style = ::GetWindowLong(GetListHandle(), GWL_STYLE) | WS_VSCROLL | WS_HSCROLL;
 280     if (isMultiSelect) {
 281         style |= LBS_MULTIPLESEL;
 282     } else {
 283         style &= ~LBS_MULTIPLESEL;
 284     }
 285     DWORD exStyle = ::GetWindowLong(GetListHandle(), GWL_EXSTYLE);
 286 
 287     jobject peer = GetPeer(env);
 288 
 289     UnsubclassHWND();
 290     AwtToolkit::DestroyComponentHWND(m_hwnd);
 291     CreateHWnd(env, L"", style, exStyle,
 292                rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
 293                parentHWnd,
 294                NULL,
 295                ::GetSysColor(COLOR_WINDOWTEXT),
 296                ::GetSysColor(COLOR_WINDOW),
 297                peer);
 298 
 299     SendListMessage(WM_SETFONT, (WPARAM)font, (LPARAM)FALSE);
 300     SendListMessage(LB_SETITEMHEIGHT, 0, MAKELPARAM(itemHeight, 0));
 301     SendListMessage(LB_RESETCONTENT);
 302     for (i = 0; i < nCount; i++) {
 303         InsertString(i, strings[i]);
 304         delete [] strings[i];
 305     }
 306     delete[] strings;
 307     if (toSelect != -1) {
 308         Select(toSelect);
 309     }
 310 
 311     AdjustHorizontalScrollbar();
 312 }
 313 
 314 /*
 315  * There currently is no good place to cache java.awt.Dimension field
 316  * ids. If this method gets called a lot, one such place should be found.
 317  * -- br 07/18/97.
 318  */
 319 jobject AwtList::PreferredItemSize(JNIEnv *env)
 320 {
 321     jobject peer = GetPeer(env);
 322     jobject dimension = JNU_CallMethodByName(env, NULL, peer, "getPreferredSize",
 323                                              "(I)Ljava/awt/Dimension;",
 324                                              1).l;
 325 
 326     DASSERT(!safe_ExceptionOccurred(env));
 327     if (dimension == NULL) {
 328         return NULL;
 329     }
 330     /* This size is too big for each item height. */
 331     (env)->SetIntField(dimension, AwtDimension::heightID, GetFontHeight(env));
 332 
 333     return dimension;
 334 }
 335 
 336 // Every time something gets added to the list, we increase the max width
 337 // of items that have ever been added.  If it surpasses the width of the
 338 // listbox, we show the scrollbar.  When things get deleted, we shrink
 339 // the scroll region back down and hide the scrollbar, if needed.
 340 void AwtList::AdjustHorizontalScrollbar()
 341 {
 342     // The border width is added to the horizontal extent to ensure that we
 343     // can view all of the text when we move the horz. scrollbar to the end.
 344     int  cxBorders = GetSystemMetrics( SM_CXBORDER ) * 2;
 345     RECT rect;
 346     VERIFY(::GetClientRect(GetListHandle(), &rect));
 347     LRESULT iHorzExt = SendListMessage(LB_GETHORIZONTALEXTENT, 0, 0L ) - cxBorders;
 348     if ( (m_nMaxWidth > rect.left)  // if strings wider than listbox
 349       || (iHorzExt != m_nMaxWidth) ) //   or scrollbar not needed anymore.
 350     {
 351         SendListMessage(LB_SETHORIZONTALEXTENT, m_nMaxWidth + cxBorders, 0L);
 352     }
 353 }
 354 
 355 // This function goes through all strings in the list to find the width,
 356 // in pixels, of the longest string in the list.
 357 void AwtList::UpdateMaxItemWidth()
 358 {
 359     m_nMaxWidth = 0;
 360 
 361     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 362     if (env->EnsureLocalCapacity(2) < 0)
 363         return;
 364 
 365     HDC hDC = ::GetDC(GetHWnd());
 366 
 367     jobject self = GetPeer(env);
 368     DASSERT(self);
 369 
 370     /* target is java.awt.List */
 371     jobject target = env->GetObjectField(self, AwtObject::targetID);
 372     jobject font = GET_FONT(target, self);
 373 
 374     int nCount = GetCount();
 375     for ( int i=0; i < nCount; i++ )
 376     {
 377         jstring jstr = GetItemString( env, target, i );
 378         SIZE size = AwtFont::getMFStringSize( hDC, font, jstr );
 379         if ( size.cx > m_nMaxWidth )
 380             m_nMaxWidth = size.cx;
 381         env->DeleteLocalRef( jstr );
 382     }
 383 
 384     // free up the shared DC and release local refs
 385     ::ReleaseDC(GetHWnd(), hDC);
 386     env->DeleteLocalRef( target );
 387     env->DeleteLocalRef( font );
 388 
 389     // Now adjust the horizontal scrollbar extent
 390     AdjustHorizontalScrollbar();
 391 }
 392 
 393 MsgRouting
 394 AwtList::WmSize(UINT type, int w, int h)
 395 {
 396     AdjustHorizontalScrollbar();
 397     return AwtComponent::WmSize(type, w, h);
 398 }
 399 
 400 MsgRouting
 401 AwtList::OwnerDrawItem(UINT /*ctrlId*/, DRAWITEMSTRUCT& drawInfo)
 402 {
 403     AwtComponent::DrawListItem((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), drawInfo);
 404     return mrConsume;
 405 }
 406 
 407 MsgRouting
 408 AwtList::OwnerMeasureItem(UINT /*ctrlId*/, MEASUREITEMSTRUCT& measureInfo)
 409 {
 410     AwtComponent::MeasureListItem((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), measureInfo);
 411     return mrConsume;
 412 }
 413 
 414 MsgRouting
 415 AwtList::WmNcHitTest(UINT x, UINT y, LRESULT& retVal)
 416 {
 417     if (::IsWindow(AwtWindow::GetModalBlocker(AwtComponent::GetTopLevelParentForWindow(GetHWnd())))) {
 418         retVal = HTCLIENT;
 419         return mrConsume;
 420     }
 421     return AwtComponent::WmNcHitTest(x, y, retVal);
 422 }
 423 
 424 MsgRouting
 425 AwtList::WmMouseUp(UINT flags, int x, int y, int button)
 426 {
 427     MsgRouting result = mrDoDefault;
 428     // if this list is in the modal blocked window, this message should be consumed,
 429     // however AwtComponent::WmMouseUp must be called anyway
 430     if (::IsWindow(AwtWindow::GetModalBlocker(AwtComponent::GetTopLevelParentForWindow(GetHWnd())))) {
 431         result = mrConsume;
 432     } else {
 433         if (button == LEFT_BUTTON) {
 434             WmCommand(0, GetListHandle(), LBN_SELCHANGE);
 435         }
 436     }
 437     MsgRouting compResult = AwtComponent::WmMouseUp(flags, x, y, button);
 438     return (result == mrConsume) ? result : compResult;
 439 }
 440 
 441 MsgRouting
 442 AwtList::WmMouseDown(UINT flags, int x, int y, int button)
 443 {
 444     MsgRouting mrResult = AwtComponent::WmMouseDown(flags, x, y, button);
 445 
 446     if (::IsWindow(AwtWindow::GetModalBlocker(AwtComponent::GetTopLevelParentForWindow(GetHWnd()))))
 447     {
 448         return mrConsume;
 449     }
 450 
 451     /*
 452      * As we consume WM_LBUTONDOWN the list won't trigger ActionEvent by double click.
 453      * We trigger it ourselves. (see also 6240202)
 454      */
 455     int clickCount = GetClickCount();
 456     if (button == LEFT_BUTTON && clickCount >= 2 && clickCount % 2 == 0) {
 457         WmCommand(0, GetListHandle(), LBN_DBLCLK);
 458     }
 459     return mrResult;
 460 }
 461 
 462 MsgRouting
 463 AwtList::WmCtlColor(HDC hDC, HWND hCtrl, UINT ctlColor, HBRUSH& retBrush)
 464 {
 465     DASSERT(ctlColor == CTLCOLOR_LISTBOX);
 466     DASSERT(hCtrl == GetListHandle());
 467     ::SetBkColor(hDC, GetBackgroundColor());
 468     ::SetTextColor(hDC, GetColor());
 469     retBrush = GetBackgroundBrush();
 470     return mrConsume;
 471 }
 472 
 473 BOOL AwtList::IsFocusingMouseMessage(MSG *pMsg)
 474 {
 475     return pMsg->message == WM_LBUTTONDOWN || pMsg->message == WM_LBUTTONDBLCLK;
 476 }
 477 
 478 MsgRouting AwtList::HandleEvent(MSG *msg, BOOL synthetic)
 479 {
 480     if (IsFocusingMouseMessage(msg)) {
 481         LONG count = GetCount();
 482         if (count > 0) {
 483             LONG item = static_cast<LONG>(SendListMessage(LB_ITEMFROMPOINT, 0, msg->lParam));
 484             if (HIWORD(item) == 0) {
 485                 item = LOWORD(item);
 486                 if (item >= 0 && item < count) {
 487                     if (isMultiSelect) {
 488                         if (IsItemSelected(item)) {
 489                             Deselect(item);
 490                         } else {
 491                             Select(item);
 492                         }
 493                     } else {
 494                         Select(item);
 495                     }
 496                 }
 497             }
 498         }
 499         delete msg;
 500         return mrConsume;
 501     }
 502     if (msg->message == WM_KEYDOWN && msg->wParam == VK_RETURN) {
 503         WmNotify(LBN_DBLCLK);
 504     }
 505     return AwtComponent::HandleEvent(msg, synthetic);
 506 }
 507 
 508 // Fix for 4665745.
 509 // Override WmPrint to catch when the list control (not wrapper) should
 510 // operate WM_PRINT to be compatible with the "smooth scrolling" feature.
 511 MsgRouting AwtList::WmPrint(HDC hDC, LPARAM flags)
 512 {
 513     if (!isWrapperPrint &&
 514         (flags & PRF_CLIENT) &&
 515         (GetStyleEx() & WS_EX_CLIENTEDGE))
 516     {
 517         int nOriginalDC = ::SaveDC(hDC);
 518         DASSERT(nOriginalDC != 0);
 519         // Save a copy of the DC for WmPrintClient
 520         VERIFY(::SaveDC(hDC));
 521         DefWindowProc(WM_PRINT, (WPARAM) hDC,
 522             (flags & (PRF_CLIENT | PRF_CHECKVISIBLE | PRF_ERASEBKGND)));
 523         VERIFY(::RestoreDC(hDC, nOriginalDC));
 524 
 525         flags &= ~PRF_CLIENT;
 526     }
 527 
 528     return AwtComponent::WmPrint(hDC, flags);
 529 }
 530 
 531 MsgRouting
 532 AwtList::WmNotify(UINT notifyCode)
 533 {
 534     if (notifyCode == LBN_SELCHANGE || notifyCode == LBN_DBLCLK) {
 535         /* Fixed an asserion failure when clicking on an empty List. */
 536         int nCurrentSelection = SendListMessage(LB_GETCURSEL);
 537         if (nCurrentSelection != LB_ERR && GetCount() > 0) {
 538             if (notifyCode == LBN_SELCHANGE) {
 539                 DoCallback("handleListChanged", "(I)V", nCurrentSelection);
 540             }
 541             else if (notifyCode == LBN_DBLCLK) {
 542                 DoCallback("handleAction", "(IJI)V", nCurrentSelection,
 543                            ::JVM_CurrentTimeMillis(NULL, 0),
 544                            (jint)AwtComponent::GetActionModifiers());
 545             }
 546         }
 547     }
 548     return mrDoDefault;
 549 }
 550 
 551 BOOL AwtList::InheritsNativeMouseWheelBehavior() {return true;}
 552 
 553 jint AwtList::_GetMaxWidth(void *param)
 554 {
 555     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 556 
 557     jobject self = (jobject)param;
 558 
 559     jint result = 0;
 560     AwtList *l = NULL;
 561 
 562     PDATA pData;
 563     JNI_CHECK_PEER_GOTO(self, ret);
 564     l = (AwtList *)pData;
 565     if (::IsWindow(l->GetHWnd()))
 566     {
 567         result = l->GetMaxWidth();
 568     }
 569 ret:
 570     env->DeleteGlobalRef(self);
 571 
 572     return result;
 573 }
 574 
 575 void AwtList::_UpdateMaxItemWidth(void *param)
 576 {
 577     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 578 
 579     jobject self = (jobject)param;
 580 
 581     AwtList *l = NULL;
 582 
 583     PDATA pData;
 584     JNI_CHECK_PEER_GOTO(self, ret);
 585     l = (AwtList *)pData;
 586     if (::IsWindow(l->GetHWnd()))
 587     {
 588         l->UpdateMaxItemWidth();
 589     }
 590 ret:
 591     env->DeleteGlobalRef(self);
 592 }
 593 
 594 void AwtList::_AddItems(void *param)
 595 {
 596     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 597 
 598     AddItemsStruct *ais = (AddItemsStruct *)param;
 599     jobject self = ais->list;
 600     jobjectArray items = ais->items;
 601     jint index = ais->index;
 602     jint width = ais->width;
 603 
 604     int badAlloc = 0;
 605     AwtList *l = NULL;
 606 
 607     PDATA pData;
 608     JNI_CHECK_PEER_GOTO(self, ret);
 609     JNI_CHECK_NULL_GOTO(items, "null items", ret);
 610     l = (AwtList*)pData;
 611     if (::IsWindow(l->GetHWnd()))
 612     {
 613         int itemCount = env->GetArrayLength(items);
 614         if (itemCount > 0)
 615         {
 616             AwtList* l = (AwtList*)pData;
 617             l->SendListMessage(WM_SETREDRAW, (WPARAM)FALSE, 0);
 618             for (jsize i=0; i < itemCount; i++)
 619             {
 620                 LPTSTR itemPtr = NULL;
 621                 jstring item = (jstring)env->GetObjectArrayElement(items, i);
 622                 if (env->ExceptionCheck()) goto ret;
 623                 if (item == NULL) goto next_item;
 624                 itemPtr = (LPTSTR)JNU_GetStringPlatformChars(env, item, 0);
 625                 if (itemPtr == NULL)
 626                 {
 627                     badAlloc = 1;
 628                 }
 629                 else
 630                 {
 631                     l->InsertString(index+i, itemPtr);
 632                     JNU_ReleaseStringPlatformChars(env, item, itemPtr);
 633                 }
 634                 env->DeleteLocalRef(item);
 635 next_item:
 636                 ;
 637             }
 638             l->SendListMessage(WM_SETREDRAW, (WPARAM)TRUE, 0);
 639             l->InvalidateList(NULL, TRUE);
 640             l->CheckMaxWidth(width);
 641         }
 642     }
 643 ret:
 644     env->DeleteGlobalRef(self);
 645     env->DeleteGlobalRef(items);
 646 
 647     delete ais;
 648 
 649     if (badAlloc)
 650     {
 651         throw std::bad_alloc();
 652     }
 653 }
 654 
 655 void AwtList::_DelItems(void *param)
 656 {        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 657 
 658     DelItemsStruct *dis = (DelItemsStruct *)param;
 659     jobject self = dis->list;
 660     jint start = dis->start;
 661     jint end = dis->end;
 662 
 663     AwtList *l = NULL;
 664 
 665     PDATA pData;
 666     JNI_CHECK_PEER_GOTO(self, ret);
 667     l = (AwtList*)pData;
 668     if (::IsWindow(l->GetHWnd()))
 669     {
 670         int count = l->GetCount();
 671 
 672         if (start == 0 && end == count)
 673         {
 674             l->SendListMessage(LB_RESETCONTENT);
 675         }
 676         else
 677         {
 678             for (int i = start; i <= end; i++)
 679             {
 680                 l->SendListMessage(LB_DELETESTRING, start);
 681             }
 682         }
 683 
 684         l->UpdateMaxItemWidth();
 685     }
 686 ret:
 687     env->DeleteGlobalRef(self);
 688 
 689     delete dis;
 690 }
 691 
 692 void AwtList::_Select(void *param)
 693 {
 694     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 695 
 696     SelectElementStruct *ses = (SelectElementStruct *)param;
 697     jobject self = ses->list;
 698     jint index = ses->index;
 699 
 700     AwtList *l = NULL;
 701 
 702     PDATA pData;
 703     JNI_CHECK_PEER_GOTO(self, ret);
 704     l = (AwtList*)pData;
 705     if (::IsWindow(l->GetHWnd()))
 706     {
 707         l->Select(index);
 708     }
 709 ret:
 710     env->DeleteGlobalRef(self);
 711 
 712     delete ses;
 713 }
 714 
 715 void AwtList::_Deselect(void *param)
 716 {
 717     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 718 
 719     SelectElementStruct *ses = (SelectElementStruct *)param;
 720     jobject self = ses->list;
 721     jint index = ses->index;
 722 
 723     AwtList *l = NULL;
 724 
 725     PDATA pData;
 726     JNI_CHECK_PEER_GOTO(self, ret);
 727     l = (AwtList*)pData;
 728     if (::IsWindow(l->GetHWnd()))
 729     {
 730         l->Deselect(index);
 731     }
 732 ret:
 733     env->DeleteGlobalRef(self);
 734 
 735     delete ses;
 736 }
 737 
 738 void AwtList::_MakeVisible(void *param)
 739 {
 740     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 741 
 742     SelectElementStruct *ses = (SelectElementStruct *)param;
 743     jobject self = ses->list;
 744     jint index = ses->index;
 745 
 746     AwtList *l = NULL;
 747 
 748     PDATA pData;
 749     JNI_CHECK_PEER_GOTO(self, ret);
 750     l = (AwtList*)pData;
 751     if (::IsWindow(l->GetHWnd()))
 752     {
 753         l->SendListMessage(LB_SETTOPINDEX, index);
 754     }
 755 ret:
 756     env->DeleteGlobalRef(self);
 757 
 758     delete ses;
 759 }
 760 
 761 jboolean AwtList::_IsSelected(void *param)
 762 {
 763     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 764 
 765     SelectElementStruct *ses = (SelectElementStruct *)param;
 766     jobject self = ses->list;
 767     jint index = ses->index;
 768 
 769     jboolean result = JNI_FALSE;
 770     AwtList *l = NULL;
 771 
 772     PDATA pData;
 773     JNI_CHECK_PEER_GOTO(self, ret);
 774     l = (AwtList*)pData;
 775     if (::IsWindow(l->GetHWnd()))
 776     {
 777         result = l->IsItemSelected(index);
 778     }
 779 ret:
 780     env->DeleteGlobalRef(self);
 781 
 782     delete ses;
 783 
 784     return result;
 785 }
 786 
 787 void AwtList::_SetMultipleSelections(void *param)
 788 {
 789     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 790 
 791     SetMultipleSelectionsStruct *sms = (SetMultipleSelectionsStruct *)param;
 792     jobject self = sms->list;
 793     jboolean on = sms->on;
 794 
 795     AwtList *l = NULL;
 796 
 797     PDATA pData;
 798     JNI_CHECK_PEER_GOTO(self, ret);
 799     l = (AwtList*)pData;
 800     if (::IsWindow(l->GetHWnd()))
 801     {
 802         AwtToolkit::GetInstance().SendMessage(WM_AWT_LIST_SETMULTISELECT,
 803                                               (WPARAM)self, on);
 804     }
 805 ret:
 806     env->DeleteGlobalRef(self);
 807 
 808     delete sms;
 809 }
 810 
 811 /************************************************************************
 812  * WListPeer native methods
 813  *
 814  * This class seems to have numerous bugs in it, but they are all bugs
 815  * which were present before conversion to JNI. -br.
 816  */
 817 
 818 extern "C" {
 819 
 820 /*
 821  * Class:     sun_awt_windows_WListPeer
 822  * Method:    getMaxWidth
 823  * Signature: ()I
 824  */
 825 JNIEXPORT jint JNICALL
 826 Java_sun_awt_windows_WListPeer_getMaxWidth(JNIEnv *env, jobject self)
 827 {
 828     TRY;
 829 
 830     jobject selfGlobalRef = env->NewGlobalRef(self);
 831 
 832     return (jint)((intptr_t)AwtToolkit::GetInstance().SyncCall(
 833         (void *(*)(void *))AwtList::_GetMaxWidth,
 834         (void *)selfGlobalRef));
 835     // selfGlobalRef is deleted in _GetMaxWidth
 836 
 837     CATCH_BAD_ALLOC_RET(0);
 838 }
 839 
 840 /*
 841  * Class:     sun_awt_windows_WListPeer
 842  * Method:    updateMaxItemWidth
 843  * Signature: ()V
 844  */
 845 JNIEXPORT void JNICALL
 846 Java_sun_awt_windows_WListPeer_updateMaxItemWidth(JNIEnv *env, jobject self)
 847 {
 848     TRY;
 849 
 850     jobject selfGlobalRef = env->NewGlobalRef(self);
 851 
 852     AwtToolkit::GetInstance().SyncCall(AwtList::_UpdateMaxItemWidth,
 853         (void *)selfGlobalRef);
 854     // selfGlobalRef is deleted in _UpdateMaxItemWidth
 855 
 856     CATCH_BAD_ALLOC;
 857 }
 858 
 859 /*
 860  * Class:     sun_awt_windows_WListPeer
 861  * Method:    addItems
 862  * Signature: ([Ljava/lang/String;II)V
 863  */
 864 JNIEXPORT void JNICALL
 865 Java_sun_awt_windows_WListPeer_addItems(JNIEnv *env, jobject self,
 866                                         jobjectArray items, jint index, jint width)
 867 {
 868     TRY;
 869 
 870     AddItemsStruct *ais = new AddItemsStruct;
 871     ais->list = env->NewGlobalRef(self);
 872     ais->items = (jobjectArray)env->NewGlobalRef(items);
 873     ais->index = index;
 874     ais->width = width;
 875 
 876     AwtToolkit::GetInstance().SyncCall(AwtList::_AddItems, ais);
 877     // global refs and ais are deleted in _AddItems()
 878 
 879     CATCH_BAD_ALLOC;
 880 }
 881 
 882 /*
 883  * Class:     sun_awt_windows_WListPeer
 884  * Method:    delItems
 885  * Signature: (II)V
 886  */
 887 JNIEXPORT void JNICALL
 888 Java_sun_awt_windows_WListPeer_delItems(JNIEnv *env, jobject self,
 889                                         jint start, jint end)
 890 {
 891     TRY;
 892 
 893     DelItemsStruct *dis = new DelItemsStruct;
 894     dis->list = env->NewGlobalRef(self);
 895     dis->start = start;
 896     dis->end = end;
 897 
 898     AwtToolkit::GetInstance().SyncCall(AwtList::_DelItems, dis);
 899     // global ref and dis are deleted in _DelItems
 900 
 901     CATCH_BAD_ALLOC;
 902 }
 903 
 904 /*
 905  * Class:     sun_awt_windows_WListPeer
 906  * Method:    select
 907  * Signature: (I)V
 908  */
 909 JNIEXPORT void JNICALL
 910 Java_sun_awt_windows_WListPeer_select(JNIEnv *env, jobject self,
 911                                       jint pos)
 912 {
 913     TRY;
 914 
 915     SelectElementStruct *ses = new SelectElementStruct;
 916     ses->list = env->NewGlobalRef(self);
 917     ses->index = pos;
 918 
 919     AwtToolkit::GetInstance().SyncCall(AwtList::_Select, ses);
 920     // global ref and ses are deleted in _Select
 921 
 922     CATCH_BAD_ALLOC;
 923 }
 924 
 925 /*
 926  * Class:     sun_awt_windows_WListPeer
 927  * Method:    deselect
 928  * Signature: (I)V
 929  */
 930 JNIEXPORT void JNICALL
 931 Java_sun_awt_windows_WListPeer_deselect(JNIEnv *env, jobject self,
 932                                         jint pos)
 933 {
 934     TRY;
 935 
 936     SelectElementStruct *ses = new SelectElementStruct;
 937     ses->list = env->NewGlobalRef(self);
 938     ses->index = pos;
 939 
 940     AwtToolkit::GetInstance().SyncCall(AwtList::_Deselect, ses);
 941     // global ref and ses are deleted in _Deselect
 942 
 943     CATCH_BAD_ALLOC;
 944 }
 945 
 946 /*
 947  * Class:     sun_awt_windows_WListPeer
 948  * Method:    makeVisible
 949  * Signature: (I)V
 950  */
 951 JNIEXPORT void JNICALL
 952 Java_sun_awt_windows_WListPeer_makeVisible(JNIEnv *env, jobject self,
 953                                            jint pos)
 954 {
 955     TRY;
 956 
 957     SelectElementStruct *ses = new SelectElementStruct;
 958     ses->list = env->NewGlobalRef(self);
 959     ses->index = pos;
 960 
 961     AwtToolkit::GetInstance().SyncCall(AwtList::_MakeVisible, ses);
 962     // global ref and ses are deleted in _MakeVisible
 963 
 964     CATCH_BAD_ALLOC;
 965 }
 966 
 967 /*
 968  * Class:     sun_awt_windows_WListPeer
 969  * Method:    setMultipleSelections
 970  * Signature: (Z)V
 971  */
 972 JNIEXPORT void JNICALL
 973 Java_sun_awt_windows_WListPeer_setMultipleSelections(JNIEnv *env, jobject self,
 974                                                      jboolean on)
 975 {
 976     TRY;
 977 
 978     SetMultipleSelectionsStruct *sms = new SetMultipleSelectionsStruct;
 979     sms->list = env->NewGlobalRef(self);
 980     sms->on = on;
 981 
 982     AwtToolkit::GetInstance().SyncCall(AwtList::_SetMultipleSelections, sms);
 983     // global ref and sms are deleted in AwtList::_SetMultipleSelections
 984 
 985     CATCH_BAD_ALLOC;
 986 }
 987 
 988 /*
 989  * Class:     sun_awt_windows_WListPeer
 990  * Method:    create
 991  * Signature: (Lsun/awt/windows/WComponentPeer;)V
 992  */
 993 JNIEXPORT void JNICALL
 994 Java_sun_awt_windows_WListPeer_create(JNIEnv *env, jobject self,
 995                                       jobject parent)
 996 {
 997     TRY;
 998 
 999     AwtToolkit::CreateComponent(self, parent,
1000                                 (AwtToolkit::ComponentFactory)AwtList::Create);
1001 
1002     CATCH_BAD_ALLOC;
1003 }
1004 
1005 /*
1006  * Class:     sun_awt_windows_WListPeer
1007  * Method:    isSelected
1008  * Signature: (I)Z
1009  */
1010 JNIEXPORT jboolean JNICALL
1011 Java_sun_awt_windows_WListPeer_isSelected(JNIEnv *env, jobject self,
1012                                           jint index)
1013 {
1014     TRY;
1015 
1016     SelectElementStruct *ses = new SelectElementStruct;
1017     ses->list = env->NewGlobalRef(self);
1018     ses->index = index;
1019 
1020     return (jboolean)((intptr_t)AwtToolkit::GetInstance().SyncCall(
1021         (void *(*)(void *))AwtList::_IsSelected, ses));
1022     // global ref and ses are deleted in _IsSelected
1023 
1024     CATCH_BAD_ALLOC_RET(FALSE);
1025 }
1026 
1027 } /* extern "C" */