Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 3b67e95 in mainline


Ignore:
Timestamp:
2021-10-06T15:07:44Z (4 months ago)
Author:
Jiri Svoboda <jiri@…>
Children:
8e5f39d
Parents:
0e5ed803
git-author:
Jiri Svoboda <jiri@…> (2021-10-05 17:07:26)
git-committer:
Jiri Svoboda <jiri@…> (2021-10-06 15:07:44)
Message:

Creating and enumerating panel entries

Location:
uspace/app/nav
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nav/panel.c

    r0e5ed803 r3b67e95  
    3838#include <gfx/render.h>
    3939#include <stdlib.h>
     40#include <str.h>
    4041#include <ui/control.h>
    4142#include <ui/paint.h>
     
    8182
    8283        panel->window = window;
     84        list_initialize(&panel->entries);
    8385        *rpanel = panel;
    8486        return EOK;
     
    9597void panel_destroy(panel_t *panel)
    9698{
     99        panel_entry_t *entry;
     100
     101        entry = panel_first(panel);
     102        while (entry != NULL) {
     103                panel_entry_delete(entry);
     104                entry = panel_first(panel);
     105        }
     106
    97107        ui_control_delete(panel->control);
    98108        free(panel);
     
    196206}
    197207
     208/** Append new panel entry.
     209 *
     210 * @param panel Panel
     211 * @param name File name
     212 * @param size File size;
     213 * @return EOK on success or an error code
     214 */
     215errno_t panel_entry_append(panel_t *panel, const char *name, uint64_t size)
     216{
     217        panel_entry_t *entry;
     218
     219        entry = calloc(1, sizeof(panel_entry_t));
     220        if (entry == NULL)
     221                return ENOMEM;
     222
     223        entry->panel = panel;
     224        entry->name = str_dup(name);
     225        if (entry->name == NULL) {
     226                free(entry);
     227                return ENOMEM;
     228        }
     229
     230        entry->size = size;
     231        link_initialize(&entry->lentries);
     232        list_append(&entry->lentries, &panel->entries);
     233        return EOK;
     234}
     235
     236/** Delete panel entry.
     237 *
     238 * @param entry Panel entry
     239 */
     240void panel_entry_delete(panel_entry_t *entry)
     241{
     242        list_remove(&entry->lentries);
     243        free(entry->name);
     244        free(entry);
     245}
     246
     247/** Return first panel entry.
     248 *
     249 * @panel Panel
     250 * @return First panel entry or @c NULL if there are no entries
     251 */
     252panel_entry_t *panel_first(panel_t *panel)
     253{
     254        link_t *link;
     255
     256        link = list_first(&panel->entries);
     257        if (link == NULL)
     258                return NULL;
     259
     260        return list_get_instance(link, panel_entry_t, lentries);
     261}
     262
     263/** Return next panel entry.
     264 *
     265 * @param cur Current entry
     266 * @return Next entry or @c NULL if @a cur is the last entry
     267 */
     268panel_entry_t *panel_next(panel_entry_t *cur)
     269{
     270        link_t *link;
     271
     272        link = list_next(&cur->lentries, &cur->panel->entries);
     273        if (link == NULL)
     274                return NULL;
     275
     276        return list_get_instance(link, panel_entry_t, lentries);
     277}
    198278
    199279/** @}
  • uspace/app/nav/panel.h

    r0e5ed803 r3b67e95  
    3737#define PANEL_H
    3838
     39#include <adt/list.h>
    3940#include <errno.h>
    4041#include <gfx/color.h>
     
    4344#include <ui/control.h>
    4445#include <ui/window.h>
     46#include <stdint.h>
    4547#include "nav.h"
    4648#include "panel.h"
     
    6264        /** Panel color */
    6365        gfx_color_t *color;
     66
     67        /** Panel entries (list of panel_entry_t) */
     68        list_t entries;
    6469} panel_t;
     70
     71/** Panel entry */
     72typedef struct {
     73        /** Containing panel */
     74        panel_t *panel;
     75        /** Link to @c panel->entries */
     76        link_t lentries;
     77        /** File name */
     78        char *name;
     79        /** File size */
     80        uint64_t size;
     81} panel_entry_t;
    6582
    6683extern errno_t panel_create(ui_window_t *, panel_t **);
     
    7087extern ui_control_t *panel_ctl(panel_t *);
    7188extern void panel_set_rect(panel_t *, gfx_rect_t *);
     89extern errno_t panel_entry_append(panel_t *, const char *, uint64_t);
     90extern void panel_entry_delete(panel_entry_t *);
     91extern panel_entry_t *panel_first(panel_t *);
     92extern panel_entry_t *panel_next(panel_entry_t *);
    7293
    7394#endif
  • uspace/app/nav/test/panel.c

    r0e5ed803 r3b67e95  
    4747}
    4848
    49 
    5049/** Test panel_paint() */
    5150PCUT_TEST(paint)
     
    142141}
    143142
     143/** panel_entry_append() appends new entry */
     144PCUT_TEST(entry_append)
     145{
     146        panel_t *panel;
     147        errno_t rc;
     148
     149        rc = panel_create(NULL, &panel);
     150        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     151
     152        rc = panel_entry_append(panel, "a", 1);
     153        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     154
     155        PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
     156
     157        rc = panel_entry_append(panel, "b", 2);
     158        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     159
     160        PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
     161
     162        panel_destroy(panel);
     163}
     164
     165/** panel_entry_delete() deletes entry */
     166PCUT_TEST(entry_delete)
     167{
     168        panel_t *panel;
     169        panel_entry_t *entry;
     170        errno_t rc;
     171
     172        rc = panel_create(NULL, &panel);
     173        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     174
     175        rc = panel_entry_append(panel, "a", 1);
     176        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     177
     178        rc = panel_entry_append(panel, "b", 2);
     179        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     180
     181        PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
     182
     183        entry = panel_first(panel);
     184        panel_entry_delete(entry);
     185
     186        PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
     187
     188        entry = panel_first(panel);
     189        panel_entry_delete(entry);
     190
     191        PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
     192
     193        panel_destroy(panel);
     194}
     195
     196/** panel_first() returns valid entry or @c NULL as appropriate */
     197PCUT_TEST(first)
     198{
     199        panel_t *panel;
     200        panel_entry_t *entry;
     201        errno_t rc;
     202
     203        rc = panel_create(NULL, &panel);
     204        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     205
     206        entry = panel_first(panel);
     207        PCUT_ASSERT_NULL(entry);
     208
     209        /* Add one entry */
     210        rc = panel_entry_append(panel, "a", 1);
     211        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     212
     213        /* Now try getting it */
     214        entry = panel_first(panel);
     215        PCUT_ASSERT_NOT_NULL(entry);
     216        PCUT_ASSERT_STR_EQUALS("a", entry->name);
     217        PCUT_ASSERT_INT_EQUALS(1, entry->size);
     218
     219        /* Add another entry */
     220        rc = panel_entry_append(panel, "b", 2);
     221        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     222
     223        /* We should still get the first entry */
     224        entry = panel_first(panel);
     225        PCUT_ASSERT_NOT_NULL(entry);
     226        PCUT_ASSERT_STR_EQUALS("a", entry->name);
     227        PCUT_ASSERT_INT_EQUALS(1, entry->size);
     228
     229        panel_destroy(panel);
     230}
     231
     232/** panel_next() returns the next entry or @c NULL as appropriate */
     233PCUT_TEST(next)
     234{
     235        panel_t *panel;
     236        panel_entry_t *entry;
     237        errno_t rc;
     238
     239        rc = panel_create(NULL, &panel);
     240        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     241
     242        /* Add one entry */
     243        rc = panel_entry_append(panel, "a", 1);
     244        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     245
     246        /* Now try getting its successor */
     247        entry = panel_first(panel);
     248        PCUT_ASSERT_NOT_NULL(entry);
     249
     250        entry = panel_next(entry);
     251        PCUT_ASSERT_NULL(entry);
     252
     253        /* Add another entry */
     254        rc = panel_entry_append(panel, "b", 2);
     255        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     256
     257        /* Try getting the successor of first entry again */
     258        entry = panel_first(panel);
     259        PCUT_ASSERT_NOT_NULL(entry);
     260
     261        entry = panel_next(entry);
     262        PCUT_ASSERT_NOT_NULL(entry);
     263        PCUT_ASSERT_STR_EQUALS("b", entry->name);
     264        PCUT_ASSERT_INT_EQUALS(2, entry->size);
     265
     266        panel_destroy(panel);
     267}
     268
    144269PCUT_EXPORT(panel);
Note: See TracChangeset for help on using the changeset viewer.