Changeset 61784ed 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:
- 0e80e40
- Parents:
- b36ebb42
- git-author:
- Jiri Svoboda <jiri@…> (2021-10-05 17:07:26)
- git-committer:
- jxsvoboda <5887334+jxsvoboda@…> (2021-10-25 00:32:45)
- Location:
- uspace/app/nav
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nav/panel.c
rb36ebb42 r61784ed 38 38 #include <gfx/render.h> 39 39 #include <stdlib.h> 40 #include <str.h> 40 41 #include <ui/control.h> 41 42 #include <ui/paint.h> … … 81 82 82 83 panel->window = window; 84 list_initialize(&panel->entries); 83 85 *rpanel = panel; 84 86 return EOK; … … 95 97 void panel_destroy(panel_t *panel) 96 98 { 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 97 107 ui_control_delete(panel->control); 98 108 free(panel); … … 196 206 } 197 207 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 */ 215 errno_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 */ 240 void 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 */ 252 panel_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 */ 268 panel_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 } 198 278 199 279 /** @} -
uspace/app/nav/panel.h
rb36ebb42 r61784ed 37 37 #define PANEL_H 38 38 39 #include <adt/list.h> 39 40 #include <errno.h> 40 41 #include <gfx/color.h> … … 43 44 #include <ui/control.h> 44 45 #include <ui/window.h> 46 #include <stdint.h> 45 47 #include "nav.h" 46 48 #include "panel.h" … … 62 64 /** Panel color */ 63 65 gfx_color_t *color; 66 67 /** Panel entries (list of panel_entry_t) */ 68 list_t entries; 64 69 } 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; 65 82 66 83 extern errno_t panel_create(ui_window_t *, panel_t **); … … 70 87 extern ui_control_t *panel_ctl(panel_t *); 71 88 extern void panel_set_rect(panel_t *, gfx_rect_t *); 89 extern errno_t panel_entry_append(panel_t *, const char *, uint64_t); 90 extern void panel_entry_delete(panel_entry_t *); 91 extern panel_entry_t *panel_first(panel_t *); 92 extern panel_entry_t *panel_next(panel_entry_t *); 72 93 73 94 #endif -
uspace/app/nav/test/panel.c
rb36ebb42 r61784ed 47 47 } 48 48 49 50 49 /** Test panel_paint() */ 51 50 PCUT_TEST(paint) … … 142 141 } 143 142 143 /** panel_entry_append() appends new entry */ 144 PCUT_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 */ 166 PCUT_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 */ 197 PCUT_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 */ 233 PCUT_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 144 269 PCUT_EXPORT(panel);
Note:
See TracChangeset
for help on using the changeset viewer.