Mercurial > dwindows
comparison android/DWindows.kt @ 2492:e2ca6c1a4661
Android: Implement: slider, percent, scrollbar, checkbox_set/get box_unpack_at_index(),
and entryfield_set_limit().
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 01 May 2021 00:41:53 +0000 |
parents | bf4fe6bb512b |
children | bca7e0ab0ccc |
comparison
equal
deleted
inserted
replaced
2491:bf4fe6bb512b | 2492:e2ca6c1a4661 |
---|---|
8 import android.media.AudioManager | 8 import android.media.AudioManager |
9 import android.media.ToneGenerator | 9 import android.media.ToneGenerator |
10 import android.os.Bundle | 10 import android.os.Bundle |
11 import android.os.Handler | 11 import android.os.Handler |
12 import android.os.Looper | 12 import android.os.Looper |
13 import android.text.InputFilter | |
14 import android.text.InputFilter.LengthFilter | |
13 import android.text.method.PasswordTransformationMethod | 15 import android.text.method.PasswordTransformationMethod |
14 import android.util.Log | 16 import android.util.Log |
15 import android.view.Gravity | 17 import android.view.Gravity |
16 import android.view.LayoutInflater | 18 import android.view.LayoutInflater |
17 import android.view.View | 19 import android.view.View |
18 import android.view.ViewGroup | 20 import android.view.ViewGroup |
19 import android.widget.* | 21 import android.widget.* |
22 import android.widget.SeekBar.OnSeekBarChangeListener | |
20 import androidx.appcompat.app.AlertDialog | 23 import androidx.appcompat.app.AlertDialog |
21 import androidx.appcompat.app.AppCompatActivity | 24 import androidx.appcompat.app.AppCompatActivity |
22 import androidx.collection.SimpleArrayMap | 25 import androidx.collection.SimpleArrayMap |
23 import androidx.recyclerview.widget.RecyclerView | 26 import androidx.recyclerview.widget.RecyclerView |
24 import androidx.viewpager2.widget.ViewPager2 | 27 import androidx.viewpager2.widget.ViewPager2 |
66 | 69 |
67 /* | 70 /* |
68 * These are the Android calls to actually create the UI... | 71 * These are the Android calls to actually create the UI... |
69 * forwarded from the C Dynamic Windows API | 72 * forwarded from the C Dynamic Windows API |
70 */ | 73 */ |
71 fun windowNew(title: String, style: Int): LinearLayout? | 74 fun windowNew(title: String, style: Int): LinearLayout? { |
72 { | 75 if (firstWindow) { |
73 if(firstWindow) | |
74 { | |
75 var windowLayout: LinearLayout = LinearLayout(this) | 76 var windowLayout: LinearLayout = LinearLayout(this) |
76 var dataArrayMap = SimpleArrayMap<String, Long>() | 77 var dataArrayMap = SimpleArrayMap<String, Long>() |
77 | 78 |
78 windowLayout.tag = dataArrayMap | 79 windowLayout.tag = dataArrayMap |
79 setContentView(windowLayout) | 80 setContentView(windowLayout) |
84 return windowLayout | 85 return windowLayout |
85 } | 86 } |
86 return null | 87 return null |
87 } | 88 } |
88 | 89 |
89 fun windowFromId(window: View, cid: Int): View | 90 fun windowFromId(window: View, cid: Int): View { |
90 { | |
91 return window.findViewById(cid) | 91 return window.findViewById(cid) |
92 } | 92 } |
93 | 93 |
94 fun windowSetData(window: View, name: String, data: Long) | 94 fun windowSetData(window: View, name: String, data: Long) { |
95 { | 95 if (window.tag != null) { |
96 if(window.tag != null) | |
97 { | |
98 var dataArrayMap: SimpleArrayMap<String, Long> = window.tag as SimpleArrayMap<String, Long> | 96 var dataArrayMap: SimpleArrayMap<String, Long> = window.tag as SimpleArrayMap<String, Long> |
99 | 97 |
100 if(data != 0L) { | 98 if (data != 0L) { |
101 dataArrayMap.put(name, data) | 99 dataArrayMap.put(name, data) |
102 } | 100 } else { |
103 else | |
104 { | |
105 dataArrayMap.remove(name) | 101 dataArrayMap.remove(name) |
106 } | 102 } |
107 } | 103 } |
108 } | 104 } |
109 | 105 |
110 fun windowGetData(window: View, name: String): Long | 106 fun windowGetData(window: View, name: String): Long { |
111 { | |
112 var retval: Long = 0L | 107 var retval: Long = 0L |
113 | 108 |
114 if (window.tag != null) | 109 if (window.tag != null) { |
115 { | |
116 var dataArrayMap: SimpleArrayMap<String, Long> = window.tag as SimpleArrayMap<String, Long> | 110 var dataArrayMap: SimpleArrayMap<String, Long> = window.tag as SimpleArrayMap<String, Long> |
117 | 111 |
118 retval = dataArrayMap.get(name)!! | 112 retval = dataArrayMap.get(name)!! |
119 } | 113 } |
120 return retval | 114 return retval |
121 } | 115 } |
122 | 116 |
123 fun windowSetEnabled(window: View, state: Boolean) | 117 fun windowSetEnabled(window: View, state: Boolean) { |
124 { | |
125 window.setEnabled(state) | 118 window.setEnabled(state) |
126 } | 119 } |
127 | 120 |
128 fun windowSetText(window: View, text: String) | 121 fun windowSetText(window: View, text: String) { |
129 { | 122 if (window is TextView) { |
130 if(window is TextView) | |
131 { | |
132 var textview: TextView = window | 123 var textview: TextView = window |
133 textview.text = text | 124 textview.text = text |
134 } | 125 } else if (window is Button) { |
135 else if(window is Button) | |
136 { | |
137 var button: Button = window | 126 var button: Button = window |
138 button.text = text | 127 button.text = text |
139 } | 128 } else if (window is LinearLayout) { |
140 else if(window is LinearLayout) | |
141 { | |
142 // TODO: Make sure this is actually the top-level layout, not just a box | 129 // TODO: Make sure this is actually the top-level layout, not just a box |
143 this.title = text | 130 this.title = text |
144 } | 131 } |
145 } | 132 } |
146 | 133 |
147 fun windowGetText(window: View): String? | 134 fun windowGetText(window: View): String? { |
148 { | 135 if (window is TextView) { |
149 if(window is TextView) | |
150 { | |
151 var textview: TextView = window | 136 var textview: TextView = window |
152 return textview.text.toString() | 137 return textview.text.toString() |
153 } | 138 } else if (window is Button) { |
154 else if(window is Button) | |
155 { | |
156 var button: Button = window | 139 var button: Button = window |
157 return button.text.toString() | 140 return button.text.toString() |
158 } | 141 } else if (window is LinearLayout) { |
159 else if(window is LinearLayout) | |
160 { | |
161 // TODO: Make sure this is actually the top-level layout, not just a box | 142 // TODO: Make sure this is actually the top-level layout, not just a box |
162 return this.title.toString() | 143 return this.title.toString() |
163 } | 144 } |
164 return null | 145 return null |
165 } | 146 } |
166 | 147 |
167 fun clipboardGetText(): String | 148 fun clipboardGetText(): String { |
168 { | |
169 var cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager | 149 var cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager |
170 var clipdata = cm.primaryClip | 150 var clipdata = cm.primaryClip |
171 | 151 |
172 if(clipdata != null && clipdata.itemCount > 0) | 152 if (clipdata != null && clipdata.itemCount > 0) { |
173 { | |
174 return clipdata.getItemAt(0).coerceToText(this).toString() | 153 return clipdata.getItemAt(0).coerceToText(this).toString() |
175 } | 154 } |
176 return "" | 155 return "" |
177 } | 156 } |
178 | 157 |
179 fun clipboardSetText(text: String) | 158 fun clipboardSetText(text: String) { |
180 { | |
181 var cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager | 159 var cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager |
182 var clipdata = ClipData.newPlainText("text", text) | 160 var clipdata = ClipData.newPlainText("text", text) |
183 | 161 |
184 cm.setPrimaryClip(clipdata) | 162 cm.setPrimaryClip(clipdata) |
185 } | 163 } |
186 | 164 |
187 fun boxNew(type: Int, pad: Int): LinearLayout | 165 fun boxNew(type: Int, pad: Int): LinearLayout { |
188 { | |
189 val box = LinearLayout(this) | 166 val box = LinearLayout(this) |
190 var dataArrayMap = SimpleArrayMap<String, Long>() | 167 var dataArrayMap = SimpleArrayMap<String, Long>() |
191 | 168 |
192 box.tag = dataArrayMap | 169 box.tag = dataArrayMap |
193 box.layoutParams = | 170 box.layoutParams = |
194 LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT) | 171 LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT) |
195 if (type > 0) { | 172 if (type > 0) { |
196 box.orientation = LinearLayout.VERTICAL | 173 box.orientation = LinearLayout.VERTICAL |
197 } else { | 174 } else { |
198 box.orientation = LinearLayout.HORIZONTAL | 175 box.orientation = LinearLayout.HORIZONTAL |
199 } | 176 } |
200 box.setPadding(pad, pad, pad, pad) | 177 box.setPadding(pad, pad, pad, pad) |
201 return box | 178 return box |
202 } | 179 } |
203 | 180 |
204 fun boxPack(box: LinearLayout, item: View, index: Int, width: Int, height: Int, hsize: Int, vsize: Int, pad: Int) | 181 fun boxPack(box: LinearLayout, item: View, index: Int, width: Int, height: Int, hsize: Int, vsize: Int, pad: Int) { |
205 { | |
206 var w: Int = LinearLayout.LayoutParams.WRAP_CONTENT | 182 var w: Int = LinearLayout.LayoutParams.WRAP_CONTENT |
207 var h: Int = LinearLayout.LayoutParams.WRAP_CONTENT | 183 var h: Int = LinearLayout.LayoutParams.WRAP_CONTENT |
208 | 184 |
209 if(item is LinearLayout) { | 185 if (item is LinearLayout) { |
210 if(box.orientation == LinearLayout.VERTICAL) { | 186 if (box.orientation == LinearLayout.VERTICAL) { |
211 if (hsize > 0) { | 187 if (hsize > 0) { |
212 w = LinearLayout.LayoutParams.MATCH_PARENT | 188 w = LinearLayout.LayoutParams.MATCH_PARENT |
213 } | 189 } |
214 } else { | 190 } else { |
215 if (vsize > 0) { | 191 if (vsize > 0) { |
217 } | 193 } |
218 } | 194 } |
219 } | 195 } |
220 var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(w, h) | 196 var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(w, h) |
221 | 197 |
222 if(item !is LinearLayout && (width != -1 || height != -1)) { | 198 if (item !is LinearLayout && (width != -1 || height != -1)) { |
223 item.measure(0, 0) | 199 item.measure(0, 0) |
224 if (width > 0) { | 200 if (width > 0) { |
225 w = width | 201 w = width |
226 } else if(width == -1) { | 202 } else if (width == -1) { |
227 w = item.getMeasuredWidth() | 203 w = item.getMeasuredWidth() |
228 } | 204 } |
229 if (height > 0) { | 205 if (height > 0) { |
230 h = height | 206 h = height |
231 } else if(height == -1) { | 207 } else if (height == -1) { |
232 h = item.getMeasuredHeight() | 208 h = item.getMeasuredHeight() |
233 } | 209 } |
234 } | 210 } |
235 if(box.orientation == LinearLayout.VERTICAL) { | 211 if (box.orientation == LinearLayout.VERTICAL) { |
236 if (vsize > 0) { | 212 if (vsize > 0) { |
237 if (w > 0) { | 213 if (w > 0) { |
238 params.weight = w.toFloat() | 214 params.weight = w.toFloat() |
239 } else { | 215 } else { |
240 params.weight = 1F | 216 params.weight = 1F |
247 } else { | 223 } else { |
248 params.weight = 1F | 224 params.weight = 1F |
249 } | 225 } |
250 } | 226 } |
251 } | 227 } |
252 if(pad > 0) { | 228 if (pad > 0) { |
253 params.setMargins(pad, pad, pad, pad) | 229 params.setMargins(pad, pad, pad, pad) |
254 } | 230 } |
255 var grav: Int = Gravity.CLIP_HORIZONTAL or Gravity.CLIP_VERTICAL | 231 var grav: Int = Gravity.CLIP_HORIZONTAL or Gravity.CLIP_VERTICAL |
256 if(hsize > 0 && vsize > 0) { | 232 if (hsize > 0 && vsize > 0) { |
257 params.gravity = Gravity.FILL or grav | 233 params.gravity = Gravity.FILL or grav |
258 } else if(hsize > 0) { | 234 } else if (hsize > 0) { |
259 params.gravity = Gravity.FILL_HORIZONTAL or grav | 235 params.gravity = Gravity.FILL_HORIZONTAL or grav |
260 } else if(vsize > 0) { | 236 } else if (vsize > 0) { |
261 params.gravity = Gravity.FILL_VERTICAL or grav | 237 params.gravity = Gravity.FILL_VERTICAL or grav |
262 } | 238 } |
263 item.layoutParams = params | 239 item.layoutParams = params |
264 box.addView(item, index) | 240 box.addView(item, index) |
265 } | 241 } |
266 | 242 |
267 fun boxUnpack(item: View) | 243 fun boxUnpack(item: View) { |
268 { | |
269 var box: LinearLayout = item.parent as LinearLayout | 244 var box: LinearLayout = item.parent as LinearLayout |
270 box.removeView(item) | 245 box.removeView(item) |
271 } | 246 } |
272 | 247 |
273 fun buttonNew(text: String, cid: Int): Button | 248 fun boxUnpackAtIndex(box: LinearLayout, index: Int): View? { |
274 { | 249 var item: View = box.getChildAt(index) |
250 | |
251 if (item != null) { | |
252 box.removeView(item) | |
253 } | |
254 return item | |
255 } | |
256 | |
257 fun buttonNew(text: String, cid: Int): Button { | |
275 val button = Button(this) | 258 val button = Button(this) |
276 var dataArrayMap = SimpleArrayMap<String, Long>() | 259 var dataArrayMap = SimpleArrayMap<String, Long>() |
277 | 260 |
278 button.tag = dataArrayMap | 261 button.tag = dataArrayMap |
279 button.text = text | 262 button.text = text |
282 eventHandlerSimple(button, 8) | 265 eventHandlerSimple(button, 8) |
283 } | 266 } |
284 return button | 267 return button |
285 } | 268 } |
286 | 269 |
287 fun entryfieldNew(text: String, cid: Int, password: Int): EditText | 270 fun entryfieldNew(text: String, cid: Int, password: Int): EditText { |
288 { | |
289 val entryfield = EditText(this) | 271 val entryfield = EditText(this) |
290 var dataArrayMap = SimpleArrayMap<String, Long>() | 272 var dataArrayMap = SimpleArrayMap<String, Long>() |
291 | 273 |
292 entryfield.tag = dataArrayMap | 274 entryfield.tag = dataArrayMap |
293 entryfield.id = cid | 275 entryfield.id = cid |
294 if(password > 0) | 276 if (password > 0) { |
295 { | |
296 entryfield.transformationMethod = PasswordTransformationMethod.getInstance() | 277 entryfield.transformationMethod = PasswordTransformationMethod.getInstance() |
297 } | 278 } |
298 entryfield.setText(text) | 279 entryfield.setText(text) |
299 return entryfield | 280 return entryfield |
300 } | 281 } |
301 | 282 |
302 fun radioButtonNew(text: String, cid: Int): RadioButton | 283 fun entryfieldSetLimit(entryfield: EditText, limit: Long) { |
303 { | 284 entryfield.filters = arrayOf<InputFilter>(LengthFilter(limit.toInt())) |
285 } | |
286 | |
287 fun radioButtonNew(text: String, cid: Int): RadioButton { | |
304 val radiobutton = RadioButton(this) | 288 val radiobutton = RadioButton(this) |
305 var dataArrayMap = SimpleArrayMap<String, Long>() | 289 var dataArrayMap = SimpleArrayMap<String, Long>() |
306 | 290 |
307 radiobutton.tag = dataArrayMap | 291 radiobutton.tag = dataArrayMap |
308 radiobutton.id = cid | 292 radiobutton.id = cid |
311 eventHandlerSimple(radiobutton, 8) | 295 eventHandlerSimple(radiobutton, 8) |
312 } | 296 } |
313 return radiobutton | 297 return radiobutton |
314 } | 298 } |
315 | 299 |
316 fun checkboxNew(text: String, cid: Int): CheckBox | 300 fun checkboxNew(text: String, cid: Int): CheckBox { |
317 { | |
318 val checkbox = CheckBox(this) | 301 val checkbox = CheckBox(this) |
319 var dataArrayMap = SimpleArrayMap<String, Long>() | 302 var dataArrayMap = SimpleArrayMap<String, Long>() |
320 | 303 |
321 checkbox.tag = dataArrayMap | 304 checkbox.tag = dataArrayMap |
322 checkbox.id = cid | 305 checkbox.id = cid |
325 eventHandlerSimple(checkbox, 8) | 308 eventHandlerSimple(checkbox, 8) |
326 } | 309 } |
327 return checkbox | 310 return checkbox |
328 } | 311 } |
329 | 312 |
330 fun textNew(text: String, cid: Int, status: Int): TextView | 313 fun checkOrRadioSetChecked(control: View, state: Int) |
331 { | 314 { |
315 if(control is CheckBox) { | |
316 var checkbox: CheckBox = control | |
317 checkbox.isChecked = state != 0 | |
318 } else if(control is RadioButton) { | |
319 var radiobutton: RadioButton = control | |
320 radiobutton.isChecked = state != 0 | |
321 } | |
322 } | |
323 | |
324 fun checkOrRadioGetChecked(control: View): Boolean | |
325 { | |
326 if(control is CheckBox) { | |
327 var checkbox: CheckBox = control | |
328 return checkbox.isChecked | |
329 } else if(control is RadioButton) { | |
330 var radiobutton: RadioButton = control | |
331 return radiobutton.isChecked | |
332 } | |
333 return false | |
334 } | |
335 | |
336 fun textNew(text: String, cid: Int, status: Int): TextView { | |
332 val textview = TextView(this) | 337 val textview = TextView(this) |
333 var dataArrayMap = SimpleArrayMap<String, Long>() | 338 var dataArrayMap = SimpleArrayMap<String, Long>() |
334 | 339 |
335 textview.tag = dataArrayMap | 340 textview.tag = dataArrayMap |
336 textview.id = cid | 341 textview.id = cid |
337 textview.text = text | 342 textview.text = text |
338 if(status != 0) | 343 if (status != 0) { |
339 { | |
340 val border = GradientDrawable() | 344 val border = GradientDrawable() |
341 | 345 |
342 // Set a black border on white background... | 346 // Set a black border on white background... |
343 // might need to change this to invisible... | 347 // might need to change this to invisible... |
344 // or the color from windowSetColor | 348 // or the color from windowSetColor |
347 textview.background = border | 351 textview.background = border |
348 } | 352 } |
349 return textview | 353 return textview |
350 } | 354 } |
351 | 355 |
352 fun notebookNew(cid: Int, top: Int) | 356 fun notebookNew(cid: Int, top: Int) { |
353 { | |
354 val notebook = RelativeLayout(this) | 357 val notebook = RelativeLayout(this) |
355 val pager = ViewPager2(this) | 358 val pager = ViewPager2(this) |
356 val tabs = TabLayout(this) | 359 val tabs = TabLayout(this) |
357 var w: Int = RelativeLayout.LayoutParams.MATCH_PARENT | 360 var w: Int = RelativeLayout.LayoutParams.MATCH_PARENT |
358 var h: Int = RelativeLayout.LayoutParams.WRAP_CONTENT | 361 var h: Int = RelativeLayout.LayoutParams.WRAP_CONTENT |
366 }.attach() | 369 }.attach() |
367 | 370 |
368 var params: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(w, h) | 371 var params: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(w, h) |
369 tabs.layoutParams = params | 372 tabs.layoutParams = params |
370 notebook.addView(tabs) | 373 notebook.addView(tabs) |
371 if(top != 0) { | 374 if (top != 0) { |
372 notebook.addView(pager, 0) | 375 notebook.addView(pager, 0) |
373 } else { | 376 } else { |
374 notebook.addView(pager) | 377 notebook.addView(pager) |
375 } | 378 } |
379 } | |
380 | |
381 fun sliderNew(vertical: Int, increments: Int, cid: Int): SeekBar | |
382 { | |
383 val slider = SeekBar(this) | |
384 var dataArrayMap = SimpleArrayMap<String, Long>() | |
385 | |
386 slider.tag = dataArrayMap | |
387 slider.id = cid | |
388 slider.max = increments | |
389 if(vertical != 0) { | |
390 slider.rotation = 270F | |
391 } | |
392 slider.setOnSeekBarChangeListener(object : OnSeekBarChangeListener { | |
393 override fun onStopTrackingTouch(seekBar: SeekBar) { | |
394 } | |
395 override fun onStartTrackingTouch(seekBar: SeekBar) { | |
396 } | |
397 override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { | |
398 eventHandler(slider, null, 14, null, null, slider.progress, 0, 0, 0) | |
399 } | |
400 }) | |
401 return slider | |
402 } | |
403 | |
404 fun percentNew(cid: Int): ProgressBar | |
405 { | |
406 val percent = ProgressBar(this) | |
407 var dataArrayMap = SimpleArrayMap<String, Long>() | |
408 | |
409 percent.tag = dataArrayMap | |
410 percent.id = cid | |
411 percent.max = 100 | |
412 return percent | |
413 } | |
414 | |
415 fun percentGetPos(percent: ProgressBar): Int | |
416 { | |
417 return percent.progress | |
418 } | |
419 | |
420 fun percentSetPos(percent: ProgressBar, position: Int) | |
421 { | |
422 percent.progress = position | |
423 } | |
424 | |
425 fun percentSetRange(percent: ProgressBar, range: Int) | |
426 { | |
427 percent.max = range | |
376 } | 428 } |
377 | 429 |
378 fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer | 430 fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer |
379 { | 431 { |
380 // creating timer task, timer | 432 // creating timer task, timer |