From 2069546e715e679fb3bbeef2b69277e71d8516fb Mon Sep 17 00:00:00 2001
From: Mooffie <mooffie@gmail.com>
Date: Tue, 17 Nov 2015 22:31:17 +0200
Subject: [PATCH] Listbox code cleanup.
---
lib/widget/listbox.c | 55 +++++++++++++++++++++++++++-------------------------
1 file changed, 29 insertions(+), 26 deletions(-)
diff --git a/lib/widget/listbox.c b/lib/widget/listbox.c
index edd15b5..02e850f 100644
a
|
b
|
const global_keymap_t *listbox_map = NULL; |
52 | 52 | |
53 | 53 | /*** file scope macro definitions ****************************************************************/ |
54 | 54 | |
| 55 | /* Gives the position of the last item. */ |
| 56 | #define LISTBOX_LAST(l) (g_queue_is_empty ((l)->list) ? 0 : (int) g_queue_get_length ((l)->list) - 1) |
| 57 | |
55 | 58 | /*** file scope type declarations ****************************************************************/ |
56 | 59 | |
57 | 60 | /*** file scope variables ************************************************************************/ |
… |
… |
listbox_check_hotkey (WListbox * l, int key) |
212 | 215 | |
213 | 216 | /* --------------------------------------------------------------------------------------------- */ |
214 | 217 | |
215 | | /* Selects from base the pos element */ |
| 218 | /* Calculates the item displayed at screen row 'y' (y==0 being the widget's 1st row). */ |
216 | 219 | static int |
217 | | listbox_select_pos (WListbox * l, int base, int pos) |
| 220 | listbox_y_pos (WListbox * l, int y) |
218 | 221 | { |
219 | | int last = 0; |
220 | | |
221 | | base += pos; |
222 | | |
223 | | if (!listbox_is_empty (l)) |
224 | | last = g_queue_get_length (l->list) - 1; |
225 | | |
226 | | base = min (base, last); |
227 | | |
228 | | return base; |
| 222 | return min (l->top + y, LISTBOX_LAST (l)); |
229 | 223 | } |
230 | 224 | |
231 | 225 | /* --------------------------------------------------------------------------------------------- */ |
… |
… |
listbox_fwd (WListbox * l) |
242 | 236 | /* --------------------------------------------------------------------------------------------- */ |
243 | 237 | |
244 | 238 | static void |
| 239 | listbox_fwd_n (WListbox * l, int n) |
| 240 | { |
| 241 | listbox_select_entry (l, min (l->pos + n, LISTBOX_LAST (l))); |
| 242 | } |
| 243 | |
| 244 | /* --------------------------------------------------------------------------------------------- */ |
| 245 | |
| 246 | static void |
245 | 247 | listbox_back (WListbox * l) |
246 | 248 | { |
247 | 249 | if (l->pos <= 0) |
… |
… |
listbox_back (WListbox * l) |
252 | 254 | |
253 | 255 | /* --------------------------------------------------------------------------------------------- */ |
254 | 256 | |
| 257 | static void |
| 258 | listbox_back_n (WListbox * l, int n) |
| 259 | { |
| 260 | listbox_select_entry (l, max (l->pos - n, 0)); |
| 261 | } |
| 262 | |
| 263 | /* --------------------------------------------------------------------------------------------- */ |
| 264 | |
255 | 265 | static cb_ret_t |
256 | 266 | listbox_execute_cmd (WListbox * l, unsigned long command) |
257 | 267 | { |
258 | 268 | cb_ret_t ret = MSG_HANDLED; |
259 | | int i; |
260 | 269 | Widget *w = WIDGET (l); |
261 | | int length; |
262 | 270 | |
263 | 271 | if (l->list == NULL || g_queue_is_empty (l->list)) |
264 | 272 | return MSG_NOT_HANDLED; |
… |
… |
listbox_execute_cmd (WListbox * l, unsigned long command) |
278 | 286 | listbox_select_last (l); |
279 | 287 | break; |
280 | 288 | case CK_PageUp: |
281 | | for (i = 0; (i < w->lines - 1) && (l->pos > 0); i++) |
282 | | listbox_back (l); |
| 289 | listbox_back_n (l, w->lines - 1); |
283 | 290 | break; |
284 | 291 | case CK_PageDown: |
285 | | length = g_queue_get_length (l->list); |
286 | | for (i = 0; i < w->lines - 1 && l->pos < length - 1; i++) |
287 | | listbox_fwd (l); |
| 292 | listbox_fwd_n (l, w->lines - 1); |
288 | 293 | break; |
289 | 294 | case CK_Delete: |
290 | 295 | if (l->deletable) |
291 | 296 | { |
292 | 297 | gboolean is_last, is_more; |
| 298 | int length; |
293 | 299 | |
294 | 300 | length = g_queue_get_length (l->list); |
295 | 301 | |
… |
… |
listbox_event (Gpm_Event * event, void *data) |
485 | 491 | { |
486 | 492 | int ret = MOU_REPEAT; |
487 | 493 | Gpm_Event local; |
488 | | int i; |
489 | 494 | |
490 | 495 | local = mouse_get_local (event, w); |
491 | 496 | if (local.y < 1) |
492 | | for (i = -local.y; i >= 0; i--) |
493 | | listbox_back (l); |
| 497 | listbox_back_n (l, -local.y + 1); |
494 | 498 | else if (local.y > w->lines) |
495 | | for (i = local.y - w->lines; i > 0; i--) |
496 | | listbox_fwd (l); |
| 499 | listbox_fwd_n (l, local.y - w->lines); |
497 | 500 | else if ((local.buttons & GPM_B_UP) != 0) |
498 | 501 | { |
499 | 502 | listbox_back (l); |
… |
… |
listbox_event (Gpm_Event * event, void *data) |
505 | 508 | ret = MOU_NORMAL; |
506 | 509 | } |
507 | 510 | else |
508 | | listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1)); |
| 511 | listbox_select_entry (l, listbox_y_pos (l, local.y - 1)); |
509 | 512 | |
510 | 513 | /* We need to refresh ourselves since the dialog manager doesn't */ |
511 | 514 | /* know about this event */ |
… |
… |
listbox_event (Gpm_Event * event, void *data) |
521 | 524 | |
522 | 525 | local = mouse_get_local (event, w); |
523 | 526 | dlg_select_widget (l); |
524 | | listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1)); |
| 527 | listbox_select_entry (l, listbox_y_pos (l, local.y - 1)); |
525 | 528 | |
526 | 529 | if (l->callback != NULL) |
527 | 530 | action = l->callback (l); |