1 /*
   2  * Copyright (c) 1996, 2013, 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 #ifndef AWT_LIST_H
  27 #define AWT_LIST_H
  28 
  29 #include "awt_Component.h"
  30 #include <cmath>
  31 #include "sun_awt_windows_WListPeer.h"
  32 
  33 
  34 /************************************************************************
  35  * AwtList class
  36  */
  37 
  38 class AwtList : public AwtComponent {
  39 public:
  40     const UINT max_index = std::pow(2, (sizeof(WORD) * 8)) - 1;
  41     AwtList();
  42     virtual ~AwtList();
  43 
  44     virtual LPCTSTR GetClassName();
  45 
  46     static AwtList* Create(jobject peer, jobject parent);
  47 
  48     virtual BOOL NeedDblClick() { return TRUE; }
  49 
  50     INLINE void Select(int pos) {
  51         if (isMultiSelect) {
  52             SendListMessage(LB_SETSEL, TRUE, pos);
  53         }
  54         else {
  55             SendListMessage(LB_SETCURSEL, pos);
  56         }
  57     }
  58     INLINE void Deselect(int pos) {
  59         if (isMultiSelect) {
  60             SendListMessage(LB_SETCARETINDEX, pos, FALSE);
  61             SendListMessage(LB_SETSEL, FALSE, pos);
  62         }
  63         else {
  64             SendListMessage(LB_SETCURSEL, (WPARAM)-1);
  65         }
  66     }
  67     INLINE UINT GetCount() {
  68         LRESULT index = SendListMessage(LB_GETCOUNT);
  69         DASSERT(index != LB_ERR);
  70         return static_cast<UINT>(index);
  71     }
  72 
  73     INLINE void InsertString(WPARAM index, LPTSTR str) {
  74         VERIFY(SendListMessage(LB_INSERTSTRING, index, (LPARAM)str) != LB_ERR);
  75     }
  76     INLINE BOOL IsItemSelected(UINT index) {
  77         LRESULT ret = SendListMessage(LB_GETSEL, index);
  78         DASSERT(ret != LB_ERR);
  79         return (ret > 0);
  80     }
  81     INLINE BOOL InvalidateList(CONST RECT* lpRect, BOOL bErase) {
  82         DASSERT(GetListHandle());
  83         return InvalidateRect(GetListHandle(), lpRect, bErase);
  84     }
  85 
  86     // Adjust the horizontal scrollbar as necessary
  87     void AdjustHorizontalScrollbar();
  88     void UpdateMaxItemWidth();
  89 
  90     INLINE long GetMaxWidth() {
  91         return m_nMaxWidth;
  92     }
  93 
  94     INLINE void CheckMaxWidth(long nWidth) {
  95         if (nWidth > m_nMaxWidth) {
  96             m_nMaxWidth = nWidth;
  97             AdjustHorizontalScrollbar();
  98         }
  99     }
 100 
 101     // Netscape : Change the font on the list and redraw the
 102     // items nicely.
 103     virtual void SetFont(AwtFont *pFont);
 104 
 105     /* Set whether a list accepts single or multiple selections. */
 106     void SetMultiSelect(BOOL ms);
 107 
 108     /*for multifont list */
 109     jobject PreferredItemSize(JNIEnv *envx);
 110 
 111     /*
 112      * Windows message handler functions
 113      */
 114     MsgRouting WmNcHitTest(UINT x, UINT y, LRESULT& retVal);
 115     MsgRouting WmMouseDown(UINT flags, int x, int y, int button);
 116     MsgRouting WmMouseUp(UINT flags, int x, int y, int button);
 117     MsgRouting WmNotify(UINT notifyCode);
 118 
 119     /* for multifont list */
 120     MsgRouting OwnerDrawItem(UINT ctrlId, DRAWITEMSTRUCT& drawInfo);
 121     MsgRouting OwnerMeasureItem(UINT ctrlId, MEASUREITEMSTRUCT& measureInfo);
 122 
 123     //for horizontal scrollbar
 124     MsgRouting WmSize(UINT type, int w, int h);
 125 
 126     MsgRouting WmCtlColor(HDC hDC, HWND hCtrl, UINT ctlColor,
 127                           HBRUSH& retBrush);
 128 
 129     MsgRouting HandleEvent(MSG *msg, BOOL synthetic);
 130 
 131     MsgRouting WmPrint(HDC hDC, LPARAM flags);
 132 
 133     virtual void SetDragCapture(UINT flags);
 134     virtual void ReleaseDragCapture(UINT flags);
 135     void Reshape(int x, int y, int w, int h);
 136 
 137     INLINE LRESULT SendListMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0)
 138     {
 139         DASSERT(GetListHandle() != NULL);
 140         return ::SendMessage(GetListHandle(), msg, wParam, lParam);
 141     }
 142     INLINE virtual LONG GetStyle() {
 143         DASSERT(GetListHandle());
 144         return ::GetWindowLong(GetListHandle(), GWL_STYLE);
 145     }
 146     INLINE virtual void SetStyle(LONG style) {
 147         DASSERT(GetListHandle());
 148         // SetWindowLong() error handling as recommended by Win32 API doc.
 149         ::SetLastError(0);
 150         LONG ret = ::SetWindowLong(GetListHandle(), GWL_STYLE, style);
 151         DASSERT(ret != 0 || ::GetLastError() == 0);
 152     }
 153     INLINE virtual LONG GetStyleEx() {
 154         DASSERT(GetListHandle());
 155         return ::GetWindowLong(GetListHandle(), GWL_EXSTYLE);
 156     }
 157     INLINE virtual void SetStyleEx(LONG style) {
 158         DASSERT(GetListHandle());
 159         // SetWindowLong() error handling as recommended by Win32 API doc.
 160         ::SetLastError(0);
 161         LONG ret = ::SetWindowLong(GetListHandle(), GWL_EXSTYLE, style);
 162         DASSERT(ret != 0 || ::GetLastError() == 0);
 163     }
 164 
 165     INLINE HWND GetDBCSEditHandle() { return GetListHandle(); }
 166 
 167     virtual BOOL InheritsNativeMouseWheelBehavior();
 168 
 169     virtual BOOL IsFocusingMouseMessage(MSG *pMsg);
 170 
 171     // some methods called on Toolkit thread
 172     static jint _GetMaxWidth(void *param);
 173     static void _UpdateMaxItemWidth(void *param);
 174     static void _AddItems(void *param);
 175     static void _DelItems(void *param);
 176     static void _Select(void *param);
 177     static void _Deselect(void *param);
 178     static void _MakeVisible(void *param);
 179     static void _SetMultipleSelections(void *param);
 180     static jboolean _IsSelected(void *param);
 181 
 182 protected:
 183     INLINE HWND GetListHandle() { return GetHWnd(); }
 184 
 185     static BOOL IsListOwnerMessage(UINT message) {
 186         switch (message) {
 187         case WM_DRAWITEM:
 188         case WM_MEASUREITEM:
 189         case WM_COMMAND:
 190 #if defined(WIN32)
 191         case WM_CTLCOLORLISTBOX:
 192 #else
 193         case WM_CTLCOLOR:
 194 #endif
 195             return TRUE;
 196         }
 197         return FALSE;
 198     }
 199 
 200     static BOOL IsAwtMessage(UINT message) {
 201         return (message >= WM_APP);
 202     }
 203 
 204 private:
 205     BOOL isMultiSelect;
 206     BOOL isWrapperPrint;
 207 
 208     // The width of the longest item in the listbox
 209     long m_nMaxWidth;
 210 };
 211 
 212 #endif /* AWT_LIST_H */