Changeset dc5c303 in mainline for uspace/lib/ui/src/list.c


Ignore:
Timestamp:
2023-12-28T13:59:23Z (2 years ago)
Author:
GitHub <noreply@…>
Children:
6b66de6b
Parents:
42c2e65 (diff), f87ff8e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
boba-buba <120932204+boba-buba@…> (2023-12-28 13:59:23)
git-committer:
GitHub <noreply@…> (2023-12-28 13:59:23)
Message:

Merge branch 'master' into topic/packet-capture

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/src/list.c

    r42c2e65 rdc5c303  
    149149        list->cb = cb;
    150150        list->cb_arg = arg;
     151}
     152
     153/** Get UI list callback argument.
     154 *
     155 * @param list UI list
     156 * @return Callback argument
     157 */
     158void *ui_list_get_cb_arg(ui_list_t *list)
     159{
     160        return list->cb_arg;
    151161}
    152162
     
    701711}
    702712
    703 /** Delete UI list entry.
     713/** Destroy UI list entry.
     714 *
     715 * This is the quick way, but does not update cursor or page position.
    704716 *
    705717 * @param entry UI list entry
    706718 */
    707 void ui_list_entry_delete(ui_list_entry_t *entry)
     719void ui_list_entry_destroy(ui_list_entry_t *entry)
    708720{
    709721        if (entry->list->cursor == entry)
     
    718730}
    719731
     732/** Delete UI list entry.
     733 *
     734 * If required, update cursor and page position and repaint.
     735 *
     736 * @param entry UI list entry
     737 */
     738void ui_list_entry_delete(ui_list_entry_t *entry)
     739{
     740        ui_list_t *list = entry->list;
     741
     742        /* Try to make sure entry does not disappear under cursor or page */
     743        if (entry->list->cursor == entry)
     744                ui_list_cursor_up(entry->list);
     745        if (entry->list->cursor == entry)
     746                ui_list_cursor_down(entry->list);
     747        if (entry->list->page == entry)
     748                ui_list_scroll_up(entry->list);
     749        if (entry->list->page == entry)
     750                ui_list_scroll_down(entry->list);
     751
     752        ui_list_entry_destroy(entry);
     753
     754        /*
     755         * But it could still happen if there are not enough entries.
     756         * In that case just move page and/or cursor to the first
     757         * entry.
     758         */
     759        if (list->page == NULL) {
     760                list->page = ui_list_first(list);
     761                list->page_idx = 0;
     762        } else {
     763                /*
     764                 * Entry index might have changed if earlier entry
     765                 * was deleted.
     766                 */
     767                list->page_idx = ui_list_entry_get_idx(list->page);
     768        }
     769
     770        if (list->cursor == NULL) {
     771                list->cursor = ui_list_first(list);
     772                list->cursor_idx = 0;
     773        } else {
     774                /*
     775                 * Entry index might have changed if earlier entry
     776                 * was deleted.
     777                 */
     778                list->cursor_idx = ui_list_entry_get_idx(list->cursor);
     779        }
     780}
     781
    720782/** Get entry argument.
    721783 *
     
    728790}
    729791
     792/** Get containing list.
     793 *
     794 * @param entry UI list entry
     795 * @return Containing list
     796 */
     797ui_list_t *ui_list_entry_get_list(ui_list_entry_t *entry)
     798{
     799        return entry->list;
     800}
     801
     802/** Change list entry caption.
     803 *
     804 * @param entry UI list entry
     805 * @param caption New caption
     806 *
     807 * @return EOK on success, ENOMEM if out of memory
     808 */
     809errno_t ui_list_entry_set_caption(ui_list_entry_t *entry, const char *caption)
     810{
     811        char *dcaption;
     812
     813        dcaption = str_dup(caption);
     814        if (dcaption == NULL)
     815                return ENOMEM;
     816
     817        free(entry->caption);
     818        entry->caption = dcaption;
     819
     820        (void)ui_list_entry_paint(entry, ui_list_entry_get_idx(entry));
     821        return EOK;
     822}
     823
    730824/** Clear UI list entry list.
    731825 *
     
    738832        entry = ui_list_first(list);
    739833        while (entry != NULL) {
    740                 ui_list_entry_delete(entry);
     834                ui_list_entry_destroy(entry);
    741835                entry = ui_list_first(list);
    742836        }
     
    858952{
    859953        return list->cursor;
     954}
     955
     956/** Set new cursor position.
     957 *
     958 * O(N) in list size, use with caution.
     959 *
     960 * @param list UI list
     961 * @param entry New cursor position
     962 */
     963void ui_list_set_cursor(ui_list_t *list, ui_list_entry_t *entry)
     964{
     965        size_t idx;
     966
     967        idx = ui_list_entry_get_idx(entry);
     968        ui_list_cursor_move(list, entry, idx);
    860969}
    861970
     
    9121021                                /* Find first page entry (go back rows - 1) */
    9131022                                e = entry;
    914                                 for (i = 0; i < rows - 1; i++) {
     1023                                for (i = 0; i + 1 < rows; i++) {
    9151024                                        e = ui_list_prev(e);
    9161025                                }
     
    10981207        ui_list_entry_t *prev;
    10991208
     1209        if (list->page == NULL)
     1210                return;
     1211
    11001212        prev = ui_list_prev(list->page);
    11011213        if (prev == NULL)
     
    11201232        size_t i;
    11211233        size_t rows;
     1234
     1235        if (list->page == NULL)
     1236                return;
    11221237
    11231238        next = ui_list_next(list->page);
Note: See TracChangeset for help on using the changeset viewer.