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