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