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