Changeset 0e80e40 in mainline
- Timestamp:
- 2021-10-25T00:32:45Z (3 years ago)
- 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)
- Location:
- uspace
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nav/nav.c
r61784ed r0e80e40 136 136 if (rc != EOK) { 137 137 printf("Error adding control to layout.\n"); 138 return rc; 138 goto error; 139 } 140 141 rc = panel_read_dir(navigator->panel[i], "."); 142 if (rc != EOK) { 143 printf("Error reading directory.\n"); 144 goto error; 139 145 } 140 146 } -
uspace/app/nav/panel.c
r61784ed r0e80e40 35 35 */ 36 36 37 #include <dirent.h> 37 38 #include <errno.h> 38 39 #include <gfx/render.h> 40 #include <gfx/text.h> 39 41 #include <stdlib.h> 40 42 #include <str.h> 41 43 #include <ui/control.h> 42 44 #include <ui/paint.h> 45 #include <ui/resource.h> 43 46 #include "panel.h" 44 47 #include "nav.h" … … 81 84 goto error; 82 85 86 rc = gfx_color_new_ega(0x30, &panel->curs_color); 87 if (rc != EOK) 88 goto error; 89 83 90 panel->window = window; 84 91 list_initialize(&panel->entries); … … 86 93 return EOK; 87 94 error: 95 if (panel->color != NULL) 96 gfx_color_delete(panel->color); 97 if (panel->curs_color != NULL) 98 gfx_color_delete(panel->curs_color); 88 99 ui_control_delete(panel->control); 89 100 free(panel); … … 97 108 void panel_destroy(panel_t *panel) 98 109 { 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); 107 113 ui_control_delete(panel->control); 108 114 free(panel); … … 117 123 gfx_context_t *gc = ui_window_get_gc(panel->window); 118 124 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; 119 130 errno_t rc; 131 132 gfx_text_fmt_init(&fmt); 120 133 121 134 rc = gfx_set_color(gc, panel->color); … … 131 144 if (rc != EOK) 132 145 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 } 133 178 134 179 rc = gfx_update(gc); … … 240 285 void panel_entry_delete(panel_entry_t *entry) 241 286 { 287 if (entry->panel->cursor == entry) 288 entry->panel->cursor = NULL; 289 if (entry->panel->page == entry) 290 entry->panel->page = NULL; 291 242 292 list_remove(&entry->lentries); 243 293 free(entry->name); … … 245 295 } 246 296 297 /** Clear panel entry list. 298 * 299 * @param panel Panel 300 */ 301 void 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 */ 318 errno_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; 341 error: 342 closedir(dir); 343 return rc; 344 } 345 247 346 /** Return first panel entry. 248 347 * -
uspace/app/nav/panel.h
r61784ed r0e80e40 48 48 #include "panel.h" 49 49 50 /** Panel entry */ 51 typedef struct { 52 /** Containing panel */ 53 struct panel *panel; 54 /** Link to @c panel->entries */ 55 link_t lentries; 56 /** File name */ 57 char *name; 58 /** File size */ 59 uint64_t size; 60 } panel_entry_t; 61 50 62 /** Navigator panel 51 63 * … … 65 77 gfx_color_t *color; 66 78 79 /** Panel cursor color */ 80 gfx_color_t *curs_color; 81 67 82 /** Panel entries (list of panel_entry_t) */ 68 83 list_t entries; 84 85 /** First entry of current page */ 86 panel_entry_t *page; 87 88 /** Cursor position */ 89 panel_entry_t *cursor; 69 90 } panel_t; 70 71 /** Panel entry */72 typedef 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;82 91 83 92 extern errno_t panel_create(ui_window_t *, panel_t **); … … 89 98 extern errno_t panel_entry_append(panel_t *, const char *, uint64_t); 90 99 extern void panel_entry_delete(panel_entry_t *); 100 extern void panel_clear_entries(panel_t *); 101 extern errno_t panel_read_dir(panel_t *, const char *); 91 102 extern panel_entry_t *panel_first(panel_t *); 92 103 extern panel_entry_t *panel_next(panel_entry_t *); -
uspace/app/nav/test/panel.c
r61784ed r0e80e40 29 29 #include <errno.h> 30 30 #include <pcut/pcut.h> 31 #include <stdio.h> 32 #include <vfs/vfs.h> 31 33 #include "../panel.h" 32 34 … … 194 196 } 195 197 198 /** panel_clear_entries() removes all entries from panel */ 199 PCUT_TEST(clear_entries) 200 { 201 panel_t *panel; 202 errno_t rc; 203 204 rc = panel_create(NULL, &panel); 205 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 206 207 rc = panel_entry_append(panel, "a", 1); 208 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 209 210 rc = panel_entry_append(panel, "b", 2); 211 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 212 213 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries)); 214 215 panel_clear_entries(panel); 216 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries)); 217 218 panel_destroy(panel); 219 } 220 221 /** panel_read_dir() reads the contents of a directory */ 222 PCUT_TEST(read_dir) 223 { 224 panel_t *panel; 225 panel_entry_t *entry; 226 char buf[L_tmpnam]; 227 char *fname; 228 char *p; 229 errno_t rc; 230 FILE *f; 231 int rv; 232 233 /* Create name for temporary directory */ 234 p = tmpnam(buf); 235 PCUT_ASSERT_NOT_NULL(p); 236 237 /* Create temporary directory */ 238 rc = vfs_link_path(p, KIND_DIRECTORY, NULL); 239 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 240 241 rv = asprintf(&fname, "%s/%s", p, "a"); 242 PCUT_ASSERT_TRUE(rv >= 0); 243 244 f = fopen(fname, "wb"); 245 PCUT_ASSERT_NOT_NULL(f); 246 247 rv = fprintf(f, "X"); 248 PCUT_ASSERT_TRUE(rv >= 0); 249 250 rv = fclose(f); 251 PCUT_ASSERT_INT_EQUALS(0, rv); 252 253 rc = panel_create(NULL, &panel); 254 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 255 256 rc = panel_read_dir(panel, p); 257 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 258 259 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries)); 260 261 entry = panel_first(panel); 262 PCUT_ASSERT_NOT_NULL(entry); 263 PCUT_ASSERT_STR_EQUALS("a", entry->name); 264 // PCUT_ASSERT_INT_EQUALS(1, entry->size); 265 266 panel_destroy(panel); 267 268 rv = remove(fname); 269 PCUT_ASSERT_INT_EQUALS(0, rv); 270 271 rv = remove(p); 272 PCUT_ASSERT_INT_EQUALS(0, rv); 273 free(fname); 274 } 275 196 276 /** panel_first() returns valid entry or @c NULL as appropriate */ 197 277 PCUT_TEST(first) -
uspace/lib/gfxfont/include/gfx/text.h
r61784ed r0e80e40 38 38 39 39 #include <errno.h> 40 #include <stddef.h> 40 41 #include <types/gfx/coord.h> 41 42 #include <types/gfx/font.h>
Note:
See TracChangeset
for help on using the changeset viewer.