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         if (GetCount() > 0) {
 482             LONG item = static_cast<LONG>(SendListMessage(LB_ITEMFROMPOINT, 0, msg->lParam));
 483             if (item != LB_ERR) {
 484                 if (isMultiSelect) {
 485                     if (IsItemSelected(item)) {
 486                         Deselect(item);
 487                     } else {
 488                         Select(item);
 489                     }
 490                 } else {
 491                     Select(item);
 492                 }
 493             }
 494         }
 495         delete msg;
 496         return mrConsume;
 497     }
 498     if (msg->message == WM_KEYDOWN && msg->wParam == VK_RETURN) {
 499         WmNotify(LBN_DBLCLK);
 500     }
 501     return AwtComponent::HandleEvent(msg, synthetic);
 502 }
 503 
 504 // Fix for 4665745.
 505 // Override WmPrint to catch when the list control (not wrapper) should
 506 // operate WM_PRINT to be compatible with the "smooth scrolling" feature.
 507 MsgRouting AwtList::WmPrint(HDC hDC, LPARAM flags)
 508 {
 509     if (!isWrapperPrint &&
 510         (flags & PRF_CLIENT) &&
 511         (GetStyleEx() & WS_EX_CLIENTEDGE))
 512     {
 513         int nOriginalDC = ::SaveDC(hDC);
 514         DASSERT(nOriginalDC != 0);
 515         // Save a copy of the DC for WmPrintClient
 516         VERIFY(::SaveDC(hDC));
 517         DefWindowProc(WM_PRINT, (WPARAM) hDC,
 518             (flags & (PRF_CLIENT | PRF_CHECKVISIBLE | PRF_ERASEBKGND)));
 519         VERIFY(::RestoreDC(hDC, nOriginalDC));
 520 
 521         flags &= ~PRF_CLIENT;
 522     }
 523 
 524     return AwtComponent::WmPrint(hDC, flags);
 525 }
 526 
 527 MsgRouting
 528 AwtList::WmNotify(UINT notifyCode)
 529 {
 530     if (notifyCode == LBN_SELCHANGE || notifyCode == LBN_DBLCLK) {
 531         /* Fixed an asserion failure when clicking on an empty List. */
 532         int nCurrentSelection = SendListMessage(LB_GETCURSEL);
 533         if (nCurrentSelection != LB_ERR && GetCount() > 0) {
 534             if (notifyCode == LBN_SELCHANGE) {
 535                 DoCallback("handleListChanged", "(I)V", nCurrentSelection);
 536             }
 537             else if (notifyCode == LBN_DBLCLK) {
 538                 DoCallback("handleAction", "(IJI)V", nCurrentSelection,
 539                            ::JVM_CurrentTimeMillis(NULL, 0),
 540                            (jint)AwtComponent::GetActionModifiers());
 541             }
 542         }
 543     }
 544     return mrDoDefault;
 545 }
 546 
 547 BOOL AwtList::InheritsNativeMouseWheelBehavior() {return true;}
 548 
 549 jint AwtList::_GetMaxWidth(void *param)
 550 {
 551     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 552 
 553     jobject self = (jobject)param;
 554 
 555     jint result = 0;
 556     AwtList *l = NULL;
 557 
 558     PDATA pData;
 559     JNI_CHECK_PEER_GOTO(self, ret);
 560     l = (AwtList *)pData;
 561     if (::IsWindow(l->GetHWnd()))
 562     {
 563         result = l->GetMaxWidth();
 564     }
 565 ret:
 566     env->DeleteGlobalRef(self);
 567 
 568     return result;
 569 }
 570 
 571 void AwtList::_UpdateMaxItemWidth(void *param)
 572 {
 573     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 574 
 575     jobject self = (jobject)param;
 576 
 577     AwtList *l = NULL;
 578 
 579     PDATA pData;
 580     JNI_CHECK_PEER_GOTO(self, ret);
 581     l = (AwtList *)pData;
 582     if (::IsWindow(l->GetHWnd()))
 583     {
 584         l->UpdateMaxItemWidth();
 585     }
 586 ret:
 587     env->DeleteGlobalRef(self);
 588 }
 589 
 590 void AwtList::_AddItems(void *param)
 591 {
 592     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 593 
 594     AddItemsStruct *ais = (AddItemsStruct *)param;
 595     jobject self = ais->list;
 596     jobjectArray items = ais->items;
 597     jint index = ais->index;
 598     jint width = ais->width;
 599 
 600     int badAlloc = 0;
 601     AwtList *l = NULL;
 602 
 603     PDATA pData;
 604     JNI_CHECK_PEER_GOTO(self, ret);
 605     JNI_CHECK_NULL_GOTO(items, "null items", ret);
 606     l = (AwtList*)pData;
 607     if (::IsWindow(l->GetHWnd()))
 608     {
 609         int itemCount = env->GetArrayLength(items);
 610         if (itemCount > 0)
 611         {
 612             AwtList* l = (AwtList*)pData;
 613             l->SendListMessage(WM_SETREDRAW, (WPARAM)FALSE, 0);
 614             for (jsize i=0; i < itemCount; i++)
 615             {
 616                 LPTSTR itemPtr = NULL;
 617                 jstring item = (jstring)env->GetObjectArrayElement(items, i);
 618                 if (env->ExceptionCheck()) goto ret;
 619                 if (item == NULL) goto next_item;
 620                 itemPtr = (LPTSTR)JNU_GetStringPlatformChars(env, item, 0);
 621                 if (itemPtr == NULL)
 622                 {
 623                     badAlloc = 1;
 624                 }
 625                 else
 626                 {
 627                     l->InsertString(index+i, itemPtr);
 628                     JNU_ReleaseStringPlatformChars(env, item, itemPtr);
 629                 }
 630                 env->DeleteLocalRef(item);
 631 next_item:
 632                 ;
 633             }
 634             l->SendListMessage(WM_SETREDRAW, (WPARAM)TRUE, 0);
 635             l->InvalidateList(NULL, TRUE);
 636             l->CheckMaxWidth(width);
 637         }
 638     }
 639 ret:
 640     env->DeleteGlobalRef(self);
 641     env->DeleteGlobalRef(items);
 642 
 643     delete ais;
 644 
 645     if (badAlloc)
 646     {
 647         throw std::bad_alloc();
 648     }
 649 }
 650 
 651 void AwtList::_DelItems(void *param)
 652 {        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 653 
 654     DelItemsStruct *dis = (DelItemsStruct *)param;
 655     jobject self = dis->list;
 656     jint start = dis->start;
 657     jint end = dis->end;
 658 
 659     AwtList *l = NULL;
 660 
 661     PDATA pData;
 662     JNI_CHECK_PEER_GOTO(self, ret);
 663     l = (AwtList*)pData;
 664     if (::IsWindow(l->GetHWnd()))
 665     {
 666         int count = l->GetCount();
 667 
 668         if (start == 0 && end == count)
 669         {
 670             l->SendListMessage(LB_RESETCONTENT);
 671         }
 672         else
 673         {
 674             for (int i = start; i <= end; i++)
 675             {
 676                 l->SendListMessage(LB_DELETESTRING, start);
 677             }
 678         }
 679 
 680         l->UpdateMaxItemWidth();
 681     }
 682 ret:
 683     env->DeleteGlobalRef(self);
 684 
 685     delete dis;
 686 }
 687 
 688 void AwtList::_Select(void *param)
 689 {
 690     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 691 
 692     SelectElementStruct *ses = (SelectElementStruct *)param;
 693     jobject self = ses->list;
 694     jint index = ses->index;
 695 
 696     AwtList *l = NULL;
 697 
 698     PDATA pData;
 699     JNI_CHECK_PEER_GOTO(self, ret);
 700     l = (AwtList*)pData;
 701     if (::IsWindow(l->GetHWnd()))
 702     {
 703         l->Select(index);
 704     }
 705 ret:
 706     env->DeleteGlobalRef(self);
 707 
 708     delete ses;
 709 }
 710 
 711 void AwtList::_Deselect(void *param)
 712 {
 713     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 714 
 715     SelectElementStruct *ses = (SelectElementStruct *)param;
 716     jobject self = ses->list;
 717     jint index = ses->index;
 718 
 719     AwtList *l = NULL;
 720 
 721     PDATA pData;
 722     JNI_CHECK_PEER_GOTO(self, ret);
 723     l = (AwtList*)pData;
 724     if (::IsWindow(l->GetHWnd()))
 725     {
 726         l->Deselect(index);
 727     }
 728 ret:
 729     env->DeleteGlobalRef(self);
 730 
 731     delete ses;
 732 }
 733 
 734 void AwtList::_MakeVisible(void *param)
 735 {
 736     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 737 
 738     SelectElementStruct *ses = (SelectElementStruct *)param;
 739     jobject self = ses->list;
 740     jint index = ses->index;
 741 
 742     AwtList *l = NULL;
 743 
 744     PDATA pData;
 745     JNI_CHECK_PEER_GOTO(self, ret);
 746     l = (AwtList*)pData;
 747     if (::IsWindow(l->GetHWnd()))
 748     {
 749         l->SendListMessage(LB_SETTOPINDEX, index);
 750     }
 751 ret:
 752     env->DeleteGlobalRef(self);
 753 
 754     delete ses;
 755 }
 756 
 757 jboolean AwtList::_IsSelected(void *param)
 758 {
 759     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 760 
 761     SelectElementStruct *ses = (SelectElementStruct *)param;
 762     jobject self = ses->list;
 763     jint index = ses->index;
 764 
 765     jboolean result = JNI_FALSE;
 766     AwtList *l = NULL;
 767 
 768     PDATA pData;
 769     JNI_CHECK_PEER_GOTO(self, ret);
 770     l = (AwtList*)pData;
 771     if (::IsWindow(l->GetHWnd()))
 772     {
 773         result = l->IsItemSelected(index);
 774     }
 775 ret:
 776     env->DeleteGlobalRef(self);
 777 
 778     delete ses;
 779 
 780     return result;
 781 }
 782 
 783 void AwtList::_SetMultipleSelections(void *param)
 784 {
 785     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 786 
 787     SetMultipleSelectionsStruct *sms = (SetMultipleSelectionsStruct *)param;
 788     jobject self = sms->list;
 789     jboolean on = sms->on;
 790 
 791     AwtList *l = NULL;
 792 
 793     PDATA pData;
 794     JNI_CHECK_PEER_GOTO(self, ret);
 795     l = (AwtList*)pData;
 796     if (::IsWindow(l->GetHWnd()))
 797     {
 798         AwtToolkit::GetInstance().SendMessage(WM_AWT_LIST_SETMULTISELECT,
 799                                               (WPARAM)self, on);
 800     }
 801 ret:
 802     env->DeleteGlobalRef(self);
 803 
 804     delete sms;
 805 }
 806 
 807 /************************************************************************
 808  * WListPeer native methods
 809  *
 810  * This class seems to have numerous bugs in it, but they are all bugs
 811  * which were present before conversion to JNI. -br.
 812  */
 813 
 814 extern "C" {
 815 
 816 /*
 817  * Class:     sun_awt_windows_WListPeer
 818  * Method:    getMaxWidth
 819  * Signature: ()I
 820  */
 821 JNIEXPORT jint JNICALL
 822 Java_sun_awt_windows_WListPeer_getMaxWidth(JNIEnv *env, jobject self)
 823 {
 824     TRY;
 825 
 826     jobject selfGlobalRef = env->NewGlobalRef(self);
 827 
 828     return (jint)((intptr_t)AwtToolkit::GetInstance().SyncCall(
 829         (void *(*)(void *))AwtList::_GetMaxWidth,
 830         (void *)selfGlobalRef));
 831     // selfGlobalRef is deleted in _GetMaxWidth
 832 
 833     CATCH_BAD_ALLOC_RET(0);
 834 }
 835 
 836 /*
 837  * Class:     sun_awt_windows_WListPeer
 838  * Method:    updateMaxItemWidth
 839  * Signature: ()V
 840  */
 841 JNIEXPORT void JNICALL
 842 Java_sun_awt_windows_WListPeer_updateMaxItemWidth(JNIEnv *env, jobject self)
 843 {
 844     TRY;
 845 
 846     jobject selfGlobalRef = env->NewGlobalRef(self);
 847 
 848     AwtToolkit::GetInstance().SyncCall(AwtList::_UpdateMaxItemWidth,
 849         (void *)selfGlobalRef);
 850     // selfGlobalRef is deleted in _UpdateMaxItemWidth
 851 
 852     CATCH_BAD_ALLOC;
 853 }
 854 
 855 /*
 856  * Class:     sun_awt_windows_WListPeer
 857  * Method:    addItems
 858  * Signature: ([Ljava/lang/String;II)V
 859  */
 860 JNIEXPORT void JNICALL
 861 Java_sun_awt_windows_WListPeer_addItems(JNIEnv *env, jobject self,
 862                                         jobjectArray items, jint index, jint width)
 863 {
 864     TRY;
 865 
 866     AddItemsStruct *ais = new AddItemsStruct;
 867     ais->list = env->NewGlobalRef(self);
 868     ais->items = (jobjectArray)env->NewGlobalRef(items);
 869     ais->index = index;
 870     ais->width = width;
 871 
 872     AwtToolkit::GetInstance().SyncCall(AwtList::_AddItems, ais);
 873     // global refs and ais are deleted in _AddItems()
 874 
 875     CATCH_BAD_ALLOC;
 876 }
 877 
 878 /*
 879  * Class:     sun_awt_windows_WListPeer
 880  * Method:    delItems
 881  * Signature: (II)V
 882  */
 883 JNIEXPORT void JNICALL
 884 Java_sun_awt_windows_WListPeer_delItems(JNIEnv *env, jobject self,
 885                                         jint start, jint end)
 886 {
 887     TRY;
 888 
 889     DelItemsStruct *dis = new DelItemsStruct;
 890     dis->list = env->NewGlobalRef(self);
 891     dis->start = start;
 892     dis->end = end;
 893 
 894     AwtToolkit::GetInstance().SyncCall(AwtList::_DelItems, dis);
 895     // global ref and dis are deleted in _DelItems
 896 
 897     CATCH_BAD_ALLOC;
 898 }
 899 
 900 /*
 901  * Class:     sun_awt_windows_WListPeer
 902  * Method:    select
 903  * Signature: (I)V
 904  */
 905 JNIEXPORT void JNICALL
 906 Java_sun_awt_windows_WListPeer_select(JNIEnv *env, jobject self,
 907                                       jint pos)
 908 {
 909     TRY;
 910 
 911     SelectElementStruct *ses = new SelectElementStruct;
 912     ses->list = env->NewGlobalRef(self);
 913     ses->index = pos;
 914 
 915     AwtToolkit::GetInstance().SyncCall(AwtList::_Select, ses);
 916     // global ref and ses are deleted in _Select
 917 
 918     CATCH_BAD_ALLOC;
 919 }
 920 
 921 /*
 922  * Class:     sun_awt_windows_WListPeer
 923  * Method:    deselect
 924  * Signature: (I)V
 925  */
 926 JNIEXPORT void JNICALL
 927 Java_sun_awt_windows_WListPeer_deselect(JNIEnv *env, jobject self,
 928                                         jint pos)
 929 {
 930     TRY;
 931 
 932     SelectElementStruct *ses = new SelectElementStruct;
 933     ses->list = env->NewGlobalRef(self);
 934     ses->index = pos;
 935 
 936     AwtToolkit::GetInstance().SyncCall(AwtList::_Deselect, ses);
 937     // global ref and ses are deleted in _Deselect
 938 
 939     CATCH_BAD_ALLOC;
 940 }
 941 
 942 /*
 943  * Class:     sun_awt_windows_WListPeer
 944  * Method:    makeVisible
 945  * Signature: (I)V
 946  */
 947 JNIEXPORT void JNICALL
 948 Java_sun_awt_windows_WListPeer_makeVisible(JNIEnv *env, jobject self,
 949                                            jint pos)
 950 {
 951     TRY;
 952 
 953     SelectElementStruct *ses = new SelectElementStruct;
 954     ses->list = env->NewGlobalRef(self);
 955     ses->index = pos;
 956 
 957     AwtToolkit::GetInstance().SyncCall(AwtList::_MakeVisible, ses);
 958     // global ref and ses are deleted in _MakeVisible
 959 
 960     CATCH_BAD_ALLOC;
 961 }
 962 
 963 /*
 964  * Class:     sun_awt_windows_WListPeer
 965  * Method:    setMultipleSelections
 966  * Signature: (Z)V
 967  */
 968 JNIEXPORT void JNICALL
 969 Java_sun_awt_windows_WListPeer_setMultipleSelections(JNIEnv *env, jobject self,
 970                                                      jboolean on)
 971 {
 972     TRY;
 973 
 974     SetMultipleSelectionsStruct *sms = new SetMultipleSelectionsStruct;
 975     sms->list = env->NewGlobalRef(self);
 976     sms->on = on;
 977 
 978     AwtToolkit::GetInstance().SyncCall(AwtList::_SetMultipleSelections, sms);
 979     // global ref and sms are deleted in AwtList::_SetMultipleSelections
 980 
 981     CATCH_BAD_ALLOC;
 982 }
 983 
 984 /*
 985  * Class:     sun_awt_windows_WListPeer
 986  * Method:    create
 987  * Signature: (Lsun/awt/windows/WComponentPeer;)V
 988  */
 989 JNIEXPORT void JNICALL
 990 Java_sun_awt_windows_WListPeer_create(JNIEnv *env, jobject self,
 991                                       jobject parent)
 992 {
 993     TRY;
 994 
 995     AwtToolkit::CreateComponent(self, parent,
 996                                 (AwtToolkit::ComponentFactory)AwtList::Create);
 997 
 998     CATCH_BAD_ALLOC;
 999 }
1000 
1001 /*
1002  * Class:     sun_awt_windows_WListPeer
1003  * Method:    isSelected
1004  * Signature: (I)Z
1005  */
1006 JNIEXPORT jboolean JNICALL
1007 Java_sun_awt_windows_WListPeer_isSelected(JNIEnv *env, jobject self,
1008                                           jint index)
1009 {
1010     TRY;
1011 
1012     SelectElementStruct *ses = new SelectElementStruct;
1013     ses->list = env->NewGlobalRef(self);
1014     ses->index = index;
1015 
1016     return (jboolean)((intptr_t)AwtToolkit::GetInstance().SyncCall(
1017         (void *(*)(void *))AwtList::_IsSelected, ses));
1018     // global ref and ses are deleted in _IsSelected
1019 
1020     CATCH_BAD_ALLOC_RET(FALSE);
1021 }
1022 
1023 } /* extern "C" */