Ticket #3554: 0001-listbox-code-cleanup.patch

File 0001-listbox-code-cleanup.patch, 4.8 KB (added by mooffie, 7 years ago)
  • lib/widget/listbox.c

    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; 
    5252 
    5353/*** file scope macro definitions ****************************************************************/ 
    5454 
     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 
    5558/*** file scope type declarations ****************************************************************/ 
    5659 
    5760/*** file scope variables ************************************************************************/ 
    listbox_check_hotkey (WListbox * l, int key) 
    212215 
    213216/* --------------------------------------------------------------------------------------------- */ 
    214217 
    215 /* Selects from base the pos element */ 
     218/* Calculates the item displayed at screen row 'y' (y==0 being the widget's 1st row). */ 
    216219static int 
    217 listbox_select_pos (WListbox * l, int base, int pos) 
     220listbox_y_pos (WListbox * l, int y) 
    218221{ 
    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)); 
    229223} 
    230224 
    231225/* --------------------------------------------------------------------------------------------- */ 
    listbox_fwd (WListbox * l) 
    242236/* --------------------------------------------------------------------------------------------- */ 
    243237 
    244238static void 
     239listbox_fwd_n (WListbox * l, int n) 
     240{ 
     241    listbox_select_entry (l, min (l->pos + n, LISTBOX_LAST (l))); 
     242} 
     243 
     244/* --------------------------------------------------------------------------------------------- */ 
     245 
     246static void 
    245247listbox_back (WListbox * l) 
    246248{ 
    247249    if (l->pos <= 0) 
    listbox_back (WListbox * l) 
    252254 
    253255/* --------------------------------------------------------------------------------------------- */ 
    254256 
     257static void 
     258listbox_back_n (WListbox * l, int n) 
     259{ 
     260    listbox_select_entry (l, max (l->pos - n, 0)); 
     261} 
     262 
     263/* --------------------------------------------------------------------------------------------- */ 
     264 
    255265static cb_ret_t 
    256266listbox_execute_cmd (WListbox * l, unsigned long command) 
    257267{ 
    258268    cb_ret_t ret = MSG_HANDLED; 
    259     int i; 
    260269    Widget *w = WIDGET (l); 
    261     int length; 
    262270 
    263271    if (l->list == NULL || g_queue_is_empty (l->list)) 
    264272        return MSG_NOT_HANDLED; 
    listbox_execute_cmd (WListbox * l, unsigned long command) 
    278286        listbox_select_last (l); 
    279287        break; 
    280288    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); 
    283290        break; 
    284291    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); 
    288293        break; 
    289294    case CK_Delete: 
    290295        if (l->deletable) 
    291296        { 
    292297            gboolean is_last, is_more; 
     298            int length; 
    293299 
    294300            length = g_queue_get_length (l->list); 
    295301 
    listbox_event (Gpm_Event * event, void *data) 
    485491    { 
    486492        int ret = MOU_REPEAT; 
    487493        Gpm_Event local; 
    488         int i; 
    489494 
    490495        local = mouse_get_local (event, w); 
    491496        if (local.y < 1) 
    492             for (i = -local.y; i >= 0; i--) 
    493                 listbox_back (l); 
     497            listbox_back_n (l, -local.y + 1); 
    494498        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); 
    497500        else if ((local.buttons & GPM_B_UP) != 0) 
    498501        { 
    499502            listbox_back (l); 
    listbox_event (Gpm_Event * event, void *data) 
    505508            ret = MOU_NORMAL; 
    506509        } 
    507510        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)); 
    509512 
    510513        /* We need to refresh ourselves since the dialog manager doesn't */ 
    511514        /* know about this event */ 
    listbox_event (Gpm_Event * event, void *data) 
    521524 
    522525        local = mouse_get_local (event, w); 
    523526        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)); 
    525528 
    526529        if (l->callback != NULL) 
    527530            action = l->callback (l);