Changeset 0e80e40 in mainline for uspace/app/nav/panel.c


Ignore:
Timestamp:
2021-10-25T00:32:45Z (2 years ago)
Author:
jxsvoboda <5887334+jxsvoboda@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9f7e9bb
Parents:
61784ed
git-author:
Jiri Svoboda <jiri@…> (2021-10-07 17:17:36)
git-committer:
jxsvoboda <5887334+jxsvoboda@…> (2021-10-25 00:32:45)
Message:

Read and display directory contents

File:
1 edited

Legend:

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

    r61784ed r0e80e40  
    3535 */
    3636
     37#include <dirent.h>
    3738#include <errno.h>
    3839#include <gfx/render.h>
     40#include <gfx/text.h>
    3941#include <stdlib.h>
    4042#include <str.h>
    4143#include <ui/control.h>
    4244#include <ui/paint.h>
     45#include <ui/resource.h>
    4346#include "panel.h"
    4447#include "nav.h"
     
    8184                goto error;
    8285
     86        rc = gfx_color_new_ega(0x30, &panel->curs_color);
     87        if (rc != EOK)
     88                goto error;
     89
    8390        panel->window = window;
    8491        list_initialize(&panel->entries);
     
    8693        return EOK;
    8794error:
     95        if (panel->color != NULL)
     96                gfx_color_delete(panel->color);
     97        if (panel->curs_color != NULL)
     98                gfx_color_delete(panel->curs_color);
    8899        ui_control_delete(panel->control);
    89100        free(panel);
     
    97108void panel_destroy(panel_t *panel)
    98109{
    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 
     110        gfx_color_delete(panel->color);
     111        gfx_color_delete(panel->curs_color);
     112        panel_clear_entries(panel);
    107113        ui_control_delete(panel->control);
    108114        free(panel);
     
    117123        gfx_context_t *gc = ui_window_get_gc(panel->window);
    118124        ui_resource_t *res = ui_window_get_res(panel->window);
     125        gfx_font_t *font = ui_resource_get_font(res);
     126        gfx_text_fmt_t fmt;
     127        panel_entry_t *entry;
     128        gfx_coord2_t pos;
     129        gfx_rect_t rect;
    119130        errno_t rc;
     131
     132        gfx_text_fmt_init(&fmt);
    120133
    121134        rc = gfx_set_color(gc, panel->color);
     
    131144        if (rc != EOK)
    132145                return rc;
     146
     147        pos.x = panel->rect.p0.x + 1;
     148        pos.y = panel->rect.p0.y + 1;
     149
     150        entry = panel->page;
     151        while (entry != NULL && pos.y < panel->rect.p1.y - 1) {
     152                if (entry == panel->cursor) {
     153                        /* Draw cursor background */
     154                        rect.p0 = pos;
     155                        rect.p1.x = panel->rect.p1.x - 1;
     156                        rect.p1.y = rect.p0.y + 1;
     157
     158                        rc = gfx_set_color(gc, panel->curs_color);
     159                        if (rc != EOK)
     160                                return rc;
     161
     162                        rc = gfx_fill_rect(gc, &rect);
     163                        if (rc != EOK)
     164                                return rc;
     165
     166                        fmt.color = panel->curs_color;
     167                } else {
     168                        fmt.color = panel->color;
     169                }
     170
     171                rc = gfx_puttext(font, &pos, &fmt, entry->name);
     172                if (rc != EOK)
     173                        return rc;
     174
     175                pos.y++;
     176                entry = panel_next(entry);
     177        }
    133178
    134179        rc = gfx_update(gc);
     
    240285void panel_entry_delete(panel_entry_t *entry)
    241286{
     287        if (entry->panel->cursor == entry)
     288                entry->panel->cursor = NULL;
     289        if (entry->panel->page == entry)
     290                entry->panel->page = NULL;
     291
    242292        list_remove(&entry->lentries);
    243293        free(entry->name);
     
    245295}
    246296
     297/** Clear panel entry list.
     298 *
     299 * @param panel Panel
     300 */
     301void panel_clear_entries(panel_t *panel)
     302{
     303        panel_entry_t *entry;
     304
     305        entry = panel_first(panel);
     306        while (entry != NULL) {
     307                panel_entry_delete(entry);
     308                entry = panel_first(panel);
     309        }
     310}
     311
     312/** Read directory into panel entry list.
     313 *
     314 * @param panel Panel
     315 * @param dirname Directory path
     316 * @return EOK on success or an error code
     317 */
     318errno_t panel_read_dir(panel_t *panel, const char *dirname)
     319{
     320        DIR *dir;
     321        struct dirent *dirent;
     322        errno_t rc;
     323
     324        dir = opendir(dirname);
     325        if (dir == NULL)
     326                return errno;
     327
     328        dirent = readdir(dir);
     329        while (dirent != NULL) {
     330                rc = panel_entry_append(panel, dirent->d_name, 1);
     331                if (rc != EOK)
     332                        goto error;
     333                dirent = readdir(dir);
     334        }
     335
     336        closedir(dir);
     337
     338        panel->cursor = panel_first(panel);
     339        panel->page = panel_first(panel);
     340        return EOK;
     341error:
     342        closedir(dir);
     343        return rc;
     344}
     345
    247346/** Return first panel entry.
    248347 *
Note: See TracChangeset for help on using the changeset viewer.