1 | 'Classes/ActiveBasic/Windows/UI/ListBox.ab
|
---|
2 | #require <Classes/ActiveBasic/Windows/UI/Control.ab>
|
---|
3 |
|
---|
4 | Namespace ActiveBasic
|
---|
5 | Namespace Windows
|
---|
6 | Namespace UI
|
---|
7 |
|
---|
8 | /*!
|
---|
9 | @brief リストボックスコントロールのクラス
|
---|
10 | @date 2008/07/21
|
---|
11 | @auther Egtra
|
---|
12 | */
|
---|
13 | Class ListBox
|
---|
14 | Inherits WmCommandControl
|
---|
15 | Public
|
---|
16 | Sub ListBox()
|
---|
17 | items = New Detail.ListBoxItemList(This)
|
---|
18 | End Sub
|
---|
19 |
|
---|
20 | Function Items() As System.Collections.Generic.IList<String>
|
---|
21 | Items = items
|
---|
22 | End Function
|
---|
23 |
|
---|
24 | Sub SetSelected(i As Long, select As Boolean)
|
---|
25 | Dim now = SendMessage(LB_GETSEL, i As WPARAM, 0)
|
---|
26 | lrCheck(now, i, "ListBox.SetSelected")
|
---|
27 | If (now > 0) <> (select As Long <> 0) Then
|
---|
28 | '現状とselectが異なればLB_SETCURSELで反転させる。
|
---|
29 | lrCheck(SendMessage(LB_SETCURSEL, i As WPARAM, 0), i, "ListBox.SetSelected")
|
---|
30 | End If
|
---|
31 | End Sub
|
---|
32 |
|
---|
33 | /*
|
---|
34 | @date 2008/10/18
|
---|
35 | @todo 対応するイベントをまだ設けていない通知メッセージへの対応
|
---|
36 | */
|
---|
37 | Override Function RaiseCommandEvent(notificationCode As Word) As Boolean
|
---|
38 | Dim lr As LRESULT
|
---|
39 | RaiseCommandEvent = False
|
---|
40 | Select Case notificationCode
|
---|
41 | Case LBN_ERRSPACE
|
---|
42 | RaiseCommandEvent = False
|
---|
43 | Case LBN_SELCHANGE
|
---|
44 | RaiseCommandEvent = False
|
---|
45 | Case BN_CLICKED
|
---|
46 | RaiseCommandEvent = OnClick(Args.Empty)
|
---|
47 | Case LBN_DBLCLK
|
---|
48 | RaiseCommandEvent = OnDoubleClick(Args.Empty)
|
---|
49 | Case LBN_SELCANCEL
|
---|
50 | RaiseCommandEvent = False
|
---|
51 | Case LBN_SETFOCUS
|
---|
52 | RaiseCommandEvent = ProcessMessage(WM_SETFOCUS, 0, 0, lr)
|
---|
53 | Case LBN_KILLFOCUS
|
---|
54 | RaiseCommandEvent = ProcessMessage(WM_KILLFOCUS, 0, 0, lr)
|
---|
55 | End Select
|
---|
56 | End Function
|
---|
57 | Protected
|
---|
58 | Override Sub GetCreateStruct(ByRef cs As CREATESTRUCT)
|
---|
59 | With cs
|
---|
60 | .lpszClass = "LISTBOX"
|
---|
61 | End With
|
---|
62 | End Sub
|
---|
63 |
|
---|
64 | Private
|
---|
65 | items As Detail.ListBoxItemList
|
---|
66 |
|
---|
67 | Sub lrCheck(lr As LRESULT, i As Long, s As String)
|
---|
68 | Detail.LRCheck(lr, i, items.Count, s)
|
---|
69 | End Sub
|
---|
70 |
|
---|
71 | Sub lrCheck(lr As LRESULT, s As String)
|
---|
72 | Detail.LRCheck(lr, s)
|
---|
73 | End Sub
|
---|
74 | End Class
|
---|
75 |
|
---|
76 | Namespace Detail
|
---|
77 |
|
---|
78 | Class ListBoxItemList
|
---|
79 | Implements System.Collections.Generic.IList<String>
|
---|
80 | Public
|
---|
81 | Sub ListBoxItemList(l As ListBox)
|
---|
82 | base = l
|
---|
83 | End Sub
|
---|
84 |
|
---|
85 | Function GetEnumerator() As System.Collections.Generic.IEnumerator<String>
|
---|
86 | End Function
|
---|
87 |
|
---|
88 | Function Count() As Long
|
---|
89 | Count = base.SendMessage(LB_GETCOUNT) As Long
|
---|
90 | lrCheck(Count, "ListBox.Item.Count")
|
---|
91 | End Function
|
---|
92 |
|
---|
93 | Function IsReadOnly() As Boolean
|
---|
94 | Return False
|
---|
95 | End Function
|
---|
96 |
|
---|
97 | Sub Add(s As String)
|
---|
98 | lrCheck(base.SendMessage(LB_ADDSTRING, 0, ToTCStr(s) As LPARAM), "ListBox.Item.Add")
|
---|
99 | End Sub
|
---|
100 |
|
---|
101 | Sub Clear()
|
---|
102 | Do : Loop Until removeAtImpl(0) = LB_ERR
|
---|
103 | End Sub
|
---|
104 |
|
---|
105 | Function Remove(s As String) As Boolean
|
---|
106 | Dim i = IndexOf(s)
|
---|
107 | If i = -1 Then
|
---|
108 | Remove = False
|
---|
109 | Else
|
---|
110 | removeAtImpl(i)
|
---|
111 | Remove = True
|
---|
112 | End If
|
---|
113 | End Function
|
---|
114 |
|
---|
115 | Sub Operator [](index As Long, item As String)
|
---|
116 | Item[index] = item
|
---|
117 | End Sub
|
---|
118 |
|
---|
119 | Function Operator [](index As Long) As String
|
---|
120 | Return Item[index]
|
---|
121 | End Function
|
---|
122 |
|
---|
123 | Virtual Sub Item(i As Long, s As String)
|
---|
124 | lrCheck(removeAtImpl(i), i, "ListBox.Item[] (set)")
|
---|
125 | lrCheck(base.SendMessage(LB_INSERTSTRING, 0, ToTCStr(s) As LPARAM), i, "ListBox.Item[] (set)")
|
---|
126 | End Sub
|
---|
127 |
|
---|
128 | Virtual Function Item(i As Long) As String
|
---|
129 | Dim len = base.SendMessage(LB_GETTEXTLEN, i As WPARAM, 0)
|
---|
130 | lrCheck(len, i, "ListBox.Item[] (get)")
|
---|
131 | Dim sb = New System.Text.StringBuilder(len)
|
---|
132 | sb.Length = len
|
---|
133 | len = base.SendMessage(LB_GETTEXT, i As WPARAM, StrPtr(sb) As LPARAM)
|
---|
134 | lrCheck(len, i, "ListBox.Item[] (get)")
|
---|
135 | sb.Length = len
|
---|
136 | Item = sb.ToString
|
---|
137 | End Function
|
---|
138 |
|
---|
139 | Virtual Function IndexOf(s As String) As Long
|
---|
140 | '見つからなかったときに返るLB_ERRはちょうど-1。
|
---|
141 | IndexOf = base.SendMessage(LB_FINDSTRINGEXACT, -1 As WPARAM, ToTCStr(s) As LPARAM) As Long
|
---|
142 | End Function
|
---|
143 |
|
---|
144 | Virtual Sub Insert(i As Long, s As String)
|
---|
145 | Dim lr = base.SendMessage(LB_INSERTSTRING, 0, ToTCStr(s) As LPARAM)
|
---|
146 | lrCheck(lr, i, "ListBox.Item.Insert")
|
---|
147 | End Sub
|
---|
148 |
|
---|
149 | Virtual Sub RemoveAt(i As Long)
|
---|
150 | Dim lr = removeAtImpl(i)
|
---|
151 | lrCheck(lr, i, "ListBox.Item.RemoveAt")
|
---|
152 | End Sub
|
---|
153 | Private
|
---|
154 | Function removeAtImpl(i As Long) As LRESULT
|
---|
155 | removeAtImpl = base.SendMessage(LB_DELETESTRING, i As WPARAM, 0)
|
---|
156 | End Function
|
---|
157 |
|
---|
158 | Sub lrCheck(lr As LRESULT, s As String)
|
---|
159 | LRCheck(lr, s)
|
---|
160 | End Sub
|
---|
161 |
|
---|
162 | Sub lrCheck(lr As LRESULT, i As Long, s As String)
|
---|
163 | LRCheck(lr, i, Count, s)
|
---|
164 | End Sub
|
---|
165 |
|
---|
166 | base As ListBox
|
---|
167 | End Class
|
---|
168 |
|
---|
169 | /*!
|
---|
170 | @brief List Boxからの戻り値がエラー値の場合に例外を投げる。
|
---|
171 | @param[in] lr SendMessageなどの戻り値
|
---|
172 | @param[in] s 例外を投げるときに添加するメッセージ
|
---|
173 | @throw SystemException lrがエラー値のとき
|
---|
174 | @date 2008/08/21
|
---|
175 | @auther Egtra
|
---|
176 | */
|
---|
177 | Sub LRCheck(lr As LRESULT, s As String)
|
---|
178 | If lr = LB_ERR Then
|
---|
179 | Throw New System.SystemException(s & " (LB_ERR)")
|
---|
180 | ElseIf lr = LB_ERRSPACE Then
|
---|
181 | Throw New System.SystemException(s & " (LB_ERRSPACE)")
|
---|
182 | End If
|
---|
183 | End Sub
|
---|
184 |
|
---|
185 | /*!
|
---|
186 | @brief List Boxからの戻り値がエラー値の場合に例外を投げる。
|
---|
187 | @param[in] lr SendMessageなどの戻り値
|
---|
188 | @param[in] i インデックス
|
---|
189 | @param[in] max インデックスの許容される最大値
|
---|
190 | @param[in] s 例外を投げるときに添加するメッセージ
|
---|
191 | @throw SystemException lrがエラー値のとき
|
---|
192 | @throw OutOfRangeException lrがLB_ERRで、なおかつiが範囲外のとき
|
---|
193 | @date 2008/08/21
|
---|
194 | @auther Egtra
|
---|
195 | こちらは、iが範囲外かならOutOfRangeExceptionを投げるのが特徴
|
---|
196 | */
|
---|
197 | Sub LRCheck(lr As LRESULT, i As Long, max As Long, s As String)
|
---|
198 | If lr = LB_ERR Then
|
---|
199 | If i < 0 Or i >= max Then
|
---|
200 | Throw New System.ArgumentOutOfRangeException(s)
|
---|
201 | Else
|
---|
202 | Throw New System.SystemException(s & " (LB_ERR)")
|
---|
203 | End If
|
---|
204 | ElseIf lr = LB_ERRSPACE Then
|
---|
205 | Throw New System.SystemException(s & " (LB_ERRSPACE)")
|
---|
206 | End If
|
---|
207 | End Sub
|
---|
208 |
|
---|
209 | End Namespace
|
---|
210 |
|
---|
211 | End Namespace 'UI
|
---|
212 | End Namespace 'Widnows
|
---|
213 | End Namespace 'ActiveBasic
|
---|