Changeset be1d74c1 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:
- 8c72f533
- Parents:
- 9f7e9bb
- git-author:
- Jiri Svoboda <jiri@…> (2021-10-12 17:06:35)
- 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
r9f7e9bb rbe1d74c1 49 49 static void panel_ctl_destroy(void *); 50 50 static errno_t panel_ctl_paint(void *); 51 static ui_evclaim_t panel_ctl_kbd_event(void *, kbd_event_t *); 51 52 static ui_evclaim_t panel_ctl_pos_event(void *, pos_event_t *); 52 53 … … 55 56 .destroy = panel_ctl_destroy, 56 57 .paint = panel_ctl_paint, 58 .kbd_event = panel_ctl_kbd_event, 57 59 .pos_event = panel_ctl_pos_event 58 60 }; … … 90 92 panel->window = window; 91 93 list_initialize(&panel->entries); 94 panel->entries_cnt = 0; 92 95 *rpanel = panel; 93 96 return EOK; … … 115 118 } 116 119 117 /** Paint panel. 118 * 119 * @param panel Panel 120 */ 121 errno_t panel_paint(panel_t *panel) 122 { 120 /** Paint panel entry. 121 * 122 * @param entry Panel entry 123 * @param entry_idx Entry index (within list of entries) 124 * @return EOK on success or an error code 125 */ 126 errno_t panel_entry_paint(panel_entry_t *entry, size_t entry_idx) 127 { 128 panel_t *panel = entry->panel; 123 129 gfx_context_t *gc = ui_window_get_gc(panel->window); 124 130 ui_resource_t *res = ui_window_get_res(panel->window); 125 131 gfx_font_t *font = ui_resource_get_font(res); 126 132 gfx_text_fmt_t fmt; 127 panel_entry_t *entry;128 133 gfx_coord2_t pos; 129 134 gfx_rect_t rect; 135 size_t rows; 136 errno_t rc; 137 138 gfx_text_fmt_init(&fmt); 139 140 rows = panel->rect.p1.y - panel->rect.p0.y - 2; 141 142 /* Do not display entry outside of current page */ 143 if (entry_idx < panel->page_idx || 144 entry_idx >= panel->page_idx + rows) 145 return EOK; 146 147 pos.x = panel->rect.p0.x + 1; 148 pos.y = panel->rect.p0.y + 1 + entry_idx - panel->page_idx; 149 150 if (entry == panel->cursor) 151 fmt.color = panel->curs_color; 152 else 153 fmt.color = panel->color; 154 155 /* Draw entry background */ 156 rect.p0 = pos; 157 rect.p1.x = panel->rect.p1.x - 1; 158 rect.p1.y = rect.p0.y + 1; 159 160 rc = gfx_set_color(gc, fmt.color); 161 if (rc != EOK) 162 return rc; 163 164 rc = gfx_fill_rect(gc, &rect); 165 if (rc != EOK) 166 return rc; 167 168 rc = gfx_puttext(font, &pos, &fmt, entry->name); 169 if (rc != EOK) 170 return rc; 171 172 return EOK; 173 } 174 175 /** Paint panel. 176 * 177 * @param panel Panel 178 */ 179 errno_t panel_paint(panel_t *panel) 180 { 181 gfx_context_t *gc = ui_window_get_gc(panel->window); 182 ui_resource_t *res = ui_window_get_res(panel->window); 183 gfx_text_fmt_t fmt; 184 panel_entry_t *entry; 185 int i, lines; 130 186 errno_t rc; 131 187 … … 145 201 return rc; 146 202 147 pos.x = panel->rect.p0.x + 1;148 pos.y = panel->rect.p0.y + 1;203 lines = panel->rect.p1.y - panel->rect.p0.y - 2; 204 i = 0; 149 205 150 206 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); 207 while (entry != NULL && i < lines) { 208 rc = panel_entry_paint(entry, panel->page_idx + i); 172 209 if (rc != EOK) 173 210 return rc; 174 211 175 pos.y++;212 ++i; 176 213 entry = panel_next(entry); 177 214 } … … 184 221 } 185 222 223 /** Handle panel keyboard event. 224 * 225 * @param panel Panel 226 * @param event Keyboard event 227 * @return ui_claimed iff event was claimed 228 */ 229 ui_evclaim_t panel_kbd_event(panel_t *panel, kbd_event_t *event) 230 { 231 if (event->type == KEY_PRESS) { 232 if ((event->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) { 233 switch (event->key) { 234 case KC_UP: 235 panel_cursor_up(panel); 236 break; 237 case KC_DOWN: 238 panel_cursor_down(panel); 239 break; 240 case KC_HOME: 241 panel_cursor_top(panel); 242 break; 243 case KC_END: 244 panel_cursor_bottom(panel); 245 break; 246 default: 247 break; 248 } 249 } 250 } 251 252 return ui_unclaimed; 253 } 254 186 255 /** Handle panel position event. 187 256 * … … 236 305 237 306 return panel_paint(panel); 307 } 308 309 /** Handle panel control keyboard event. 310 * 311 * @param arg Argument (panel_t *) 312 * @param kbd_event Keyboard event 313 * @return @c ui_claimed iff the event is claimed 314 */ 315 ui_evclaim_t panel_ctl_kbd_event(void *arg, kbd_event_t *event) 316 { 317 panel_t *panel = (panel_t *) arg; 318 319 return panel_kbd_event(panel, event); 238 320 } 239 321 … … 276 358 link_initialize(&entry->lentries); 277 359 list_append(&entry->lentries, &panel->entries); 360 ++panel->entries_cnt; 278 361 return EOK; 279 362 } … … 291 374 292 375 list_remove(&entry->lentries); 376 --entry->panel->entries_cnt; 293 377 free(entry->name); 294 378 free(entry); … … 337 421 338 422 panel->cursor = panel_first(panel); 423 panel->cursor_idx = 0; 339 424 panel->page = panel_first(panel); 425 panel->page_idx = 0; 340 426 return EOK; 341 427 error: … … 360 446 } 361 447 448 /** Return last panel entry. 449 * 450 * @panel Panel 451 * @return Last panel entry or @c NULL if there are no entries 452 */ 453 panel_entry_t *panel_last(panel_t *panel) 454 { 455 link_t *link; 456 457 link = list_last(&panel->entries); 458 if (link == NULL) 459 return NULL; 460 461 return list_get_instance(link, panel_entry_t, lentries); 462 } 463 362 464 /** Return next panel entry. 363 465 * … … 376 478 } 377 479 480 /** Return previous panel entry. 481 * 482 * @param cur Current entry 483 * @return Previous entry or @c NULL if @a cur is the first entry 484 */ 485 panel_entry_t *panel_prev(panel_entry_t *cur) 486 { 487 link_t *link; 488 489 link = list_prev(&cur->lentries, &cur->panel->entries); 490 if (link == NULL) 491 return NULL; 492 493 return list_get_instance(link, panel_entry_t, lentries); 494 } 495 496 void panel_cursor_move(panel_t *panel, panel_entry_t *entry, size_t entry_idx) 497 { 498 gfx_context_t *gc = ui_window_get_gc(panel->window); 499 panel_entry_t *old_cursor; 500 size_t old_idx; 501 size_t rows; 502 panel_entry_t *e; 503 size_t i; 504 505 rows = panel->rect.p1.y - panel->rect.p0.y - 2; 506 507 old_cursor = panel->cursor; 508 old_idx = panel->cursor_idx; 509 510 panel->cursor = entry; 511 panel->cursor_idx = entry_idx; 512 513 if (entry_idx >= panel->page_idx && 514 entry_idx < panel->page_idx + rows) { 515 /* 516 * If cursor is still on the current page, we're not 517 * scrolling. Just unpaint old cursor and paint new 518 * cursor. 519 */ 520 panel_entry_paint(old_cursor, old_idx); 521 panel_entry_paint(panel->cursor, panel->cursor_idx); 522 523 (void) gfx_update(gc); 524 } else { 525 /* 526 * Need to scroll and update all rows. 527 */ 528 529 /* Scrolling up */ 530 if (entry_idx < panel->page_idx) { 531 panel->page = entry; 532 panel->page_idx = entry_idx; 533 } 534 535 /* Scrolling down */ 536 if (entry_idx >= panel->page_idx + rows) { 537 if (entry_idx >= rows) { 538 panel->page_idx = entry_idx - rows + 1; 539 /* Find first page entry (go back rows - 1) */ 540 e = entry; 541 for (i = 0; i < rows - 1; i++) { 542 e = panel_prev(e); 543 } 544 545 /* Should be valid */ 546 assert(e != NULL); 547 panel->page = e; 548 } else { 549 panel->page = panel_first(panel); 550 panel->page_idx = 0; 551 } 552 } 553 554 (void) panel_paint(panel); 555 } 556 } 557 558 /** Move cursor one entry up. 559 * 560 * @param panel Panel 561 */ 562 void panel_cursor_up(panel_t *panel) 563 { 564 panel_entry_t *prev; 565 size_t prev_idx; 566 567 prev = panel_prev(panel->cursor); 568 prev_idx = panel->cursor_idx - 1; 569 if (prev != NULL) 570 panel_cursor_move(panel, prev, prev_idx); 571 } 572 573 /** Move cursor one entry down. 574 * 575 * @param panel Panel 576 */ 577 void panel_cursor_down(panel_t *panel) 578 { 579 panel_entry_t *next; 580 size_t next_idx; 581 582 next = panel_next(panel->cursor); 583 next_idx = panel->cursor_idx + 1; 584 if (next != NULL) 585 panel_cursor_move(panel, next, next_idx); 586 } 587 588 /** Move cursor to top. 589 * 590 * @param panel Panel 591 */ 592 void panel_cursor_top(panel_t *panel) 593 { 594 panel_cursor_move(panel, panel_first(panel), 0); 595 } 596 597 /** Move cursor to bottom. 598 * 599 * @param panel Panel 600 */ 601 void panel_cursor_bottom(panel_t *panel) 602 { 603 panel_cursor_move(panel, panel_last(panel), panel->entries_cnt - 1); 604 } 605 378 606 /** @} 379 607 */ -
uspace/app/nav/panel.h
r9f7e9bb rbe1d74c1 41 41 #include <gfx/color.h> 42 42 #include <gfx/coord.h> 43 #include <io/kbd_event.h> 43 44 #include <io/pos_event.h> 44 45 #include <ui/control.h> … … 83 84 list_t entries; 84 85 86 /** Number of entries */ 87 size_t entries_cnt; 88 85 89 /** First entry of current page */ 86 90 panel_entry_t *page; 87 91 92 /** Index of first entry of current page */ 93 size_t page_idx; 94 88 95 /** Cursor position */ 89 96 panel_entry_t *cursor; 97 98 /** Index of entry under cursor */ 99 size_t cursor_idx; 90 100 } panel_t; 91 101 92 102 extern errno_t panel_create(ui_window_t *, panel_t **); 93 103 extern void panel_destroy(panel_t *); 104 extern errno_t panel_entry_paint(panel_entry_t *, size_t); 94 105 extern errno_t panel_paint(panel_t *); 106 extern ui_evclaim_t panel_kbd_event(panel_t *, kbd_event_t *); 95 107 extern ui_evclaim_t panel_pos_event(panel_t *, pos_event_t *); 96 108 extern ui_control_t *panel_ctl(panel_t *); … … 101 113 extern errno_t panel_read_dir(panel_t *, const char *); 102 114 extern panel_entry_t *panel_first(panel_t *); 115 extern panel_entry_t *panel_last(panel_t *); 103 116 extern panel_entry_t *panel_next(panel_entry_t *); 117 extern panel_entry_t *panel_prev(panel_entry_t *); 118 extern void panel_cursor_move(panel_t *, panel_entry_t *, size_t); 119 extern void panel_cursor_up(panel_t *); 120 extern void panel_cursor_down(panel_t *); 121 extern void panel_cursor_top(panel_t *); 122 extern void panel_cursor_bottom(panel_t *); 104 123 105 124 #endif -
uspace/app/nav/test/panel.c
r9f7e9bb rbe1d74c1 28 28 29 29 #include <errno.h> 30 #include <io/kbd_event.h> 31 #include <io/pos_event.h> 30 32 #include <pcut/pcut.h> 31 33 #include <stdio.h> … … 49 51 } 50 52 51 /** Test panel_ paint() */52 PCUT_TEST( paint)53 /** Test panel_entry_paint() */ 54 PCUT_TEST(entry_paint) 53 55 { 54 56 ui_t *ui; … … 70 72 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 71 73 72 rc = panel_paint(panel); 74 rc = panel_entry_append(panel, "a", 1); 75 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 76 77 rc = panel_entry_paint(panel_first(panel), 0); 73 78 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 74 79 … … 78 83 } 79 84 85 /** Test panel_paint() */ 86 PCUT_TEST(paint) 87 { 88 ui_t *ui; 89 ui_window_t *window; 90 ui_wnd_params_t params; 91 panel_t *panel; 92 errno_t rc; 93 94 rc = ui_create_disp(NULL, &ui); 95 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 96 97 ui_wnd_params_init(¶ms); 98 params.caption = "Test"; 99 100 rc = ui_window_create(ui, ¶ms, &window); 101 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 102 103 rc = panel_create(window, &panel); 104 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 105 106 rc = panel_paint(panel); 107 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 108 109 panel_destroy(panel); 110 ui_window_destroy(window); 111 ui_destroy(ui); 112 } 113 80 114 /** panel_ctl() returns a valid UI control */ 81 115 PCUT_TEST(ctl) … … 90 124 control = panel_ctl(panel); 91 125 PCUT_ASSERT_NOT_NULL(control); 126 127 panel_destroy(panel); 128 } 129 130 /** Test panel_kbd_event() */ 131 PCUT_TEST(kbd_event) 132 { 133 panel_t *panel; 134 ui_control_t *control; 135 ui_evclaim_t claimed; 136 kbd_event_t event; 137 errno_t rc; 138 139 rc = panel_create(NULL, &panel); 140 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 141 142 control = panel_ctl(panel); 143 PCUT_ASSERT_NOT_NULL(control); 144 145 event.type = KEY_PRESS; 146 event.key = KC_ENTER; 147 event.mods = 0; 148 event.c = '\0'; 149 150 claimed = panel_kbd_event(panel, &event); 151 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed); 92 152 93 153 panel_destroy(panel); … … 108 168 control = panel_ctl(panel); 109 169 PCUT_ASSERT_NOT_NULL(control); 170 171 event.pos_id = 0; 172 event.type = POS_PRESS; 173 event.btn_num = 1; 174 event.hpos = 0; 175 event.vpos = 0; 110 176 111 177 claimed = panel_pos_event(panel, &event); … … 310 376 } 311 377 378 /** panel_last() returns valid entry or @c NULL as appropriate */ 379 PCUT_TEST(last) 380 { 381 panel_t *panel; 382 panel_entry_t *entry; 383 errno_t rc; 384 385 rc = panel_create(NULL, &panel); 386 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 387 388 entry = panel_last(panel); 389 PCUT_ASSERT_NULL(entry); 390 391 /* Add one entry */ 392 rc = panel_entry_append(panel, "a", 1); 393 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 394 395 /* Now try getting it */ 396 entry = panel_last(panel); 397 PCUT_ASSERT_NOT_NULL(entry); 398 PCUT_ASSERT_STR_EQUALS("a", entry->name); 399 PCUT_ASSERT_INT_EQUALS(1, entry->size); 400 401 /* Add another entry */ 402 rc = panel_entry_append(panel, "b", 2); 403 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 404 405 /* We should get new entry now */ 406 entry = panel_last(panel); 407 PCUT_ASSERT_NOT_NULL(entry); 408 PCUT_ASSERT_STR_EQUALS("b", entry->name); 409 PCUT_ASSERT_INT_EQUALS(2, entry->size); 410 411 panel_destroy(panel); 412 } 413 312 414 /** panel_next() returns the next entry or @c NULL as appropriate */ 313 415 PCUT_TEST(next) … … 347 449 } 348 450 451 /** panel_prev() returns the previous entry or @c NULL as appropriate */ 452 PCUT_TEST(prev) 453 { 454 panel_t *panel; 455 panel_entry_t *entry; 456 errno_t rc; 457 458 rc = panel_create(NULL, &panel); 459 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 460 461 /* Add one entry */ 462 rc = panel_entry_append(panel, "a", 1); 463 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 464 465 /* Now try getting its predecessor */ 466 entry = panel_last(panel); 467 PCUT_ASSERT_NOT_NULL(entry); 468 469 entry = panel_prev(entry); 470 PCUT_ASSERT_NULL(entry); 471 472 /* Add another entry */ 473 rc = panel_entry_append(panel, "b", 2); 474 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 475 476 /* Try getting the predecessor of the new entry */ 477 entry = panel_last(panel); 478 PCUT_ASSERT_NOT_NULL(entry); 479 480 entry = panel_prev(entry); 481 PCUT_ASSERT_NOT_NULL(entry); 482 PCUT_ASSERT_STR_EQUALS("a", entry->name); 483 PCUT_ASSERT_INT_EQUALS(1, entry->size); 484 485 panel_destroy(panel); 486 } 487 488 /** panel_cursor_move() ... */ 489 PCUT_TEST(cursor_move) 490 { 491 } 492 493 /** panel_cursor_up() moves cursor one entry up */ 494 PCUT_TEST(cursor_up) 495 { 496 ui_t *ui; 497 ui_window_t *window; 498 ui_wnd_params_t params; 499 panel_t *panel; 500 gfx_rect_t rect; 501 errno_t rc; 502 503 rc = ui_create_disp(NULL, &ui); 504 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 505 506 ui_wnd_params_init(¶ms); 507 params.caption = "Test"; 508 509 rc = ui_window_create(ui, ¶ms, &window); 510 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 511 512 rc = panel_create(window, &panel); 513 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 514 515 rect.p0.x = 0; 516 rect.p0.y = 0; 517 rect.p1.x = 10; 518 rect.p1.y = 4; // XXX Assuming this makes page size 2 519 panel_set_rect(panel, &rect); 520 521 /* Add tree entries (more than page size, which is 2) */ 522 rc = panel_entry_append(panel, "a", 1); 523 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 524 525 rc = panel_entry_append(panel, "b", 2); 526 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 527 528 rc = panel_entry_append(panel, "c", 3); 529 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 530 531 /* Cursor to the last entry and page start to the next-to-last entry */ 532 panel->cursor = panel_last(panel); 533 panel->cursor_idx = 2; 534 panel->page = panel_prev(panel->cursor); 535 panel->page_idx = 1; 536 537 /* Move cursor one entry up */ 538 panel_cursor_up(panel); 539 540 /* Cursor and page start should now both be at the second entry */ 541 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name); 542 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size); 543 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx); 544 PCUT_ASSERT_EQUALS(panel->cursor, panel->page); 545 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx); 546 547 /* Move cursor one entry up. This should scroll up. */ 548 panel_cursor_up(panel); 549 550 /* Cursor and page start should now both be at the first entry */ 551 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name); 552 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size); 553 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx); 554 PCUT_ASSERT_EQUALS(panel->cursor, panel->page); 555 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx); 556 557 /* Moving further up should do nothing (we are at the top). */ 558 panel_cursor_up(panel); 559 560 /* Cursor and page start should still be at the first entry */ 561 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name); 562 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size); 563 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx); 564 PCUT_ASSERT_EQUALS(panel->cursor, panel->page); 565 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx); 566 567 panel_destroy(panel); 568 ui_window_destroy(window); 569 ui_destroy(ui); 570 } 571 572 /** panel_cursor_down() moves cursor one entry down */ 573 PCUT_TEST(cursor_down) 574 { 575 ui_t *ui; 576 ui_window_t *window; 577 ui_wnd_params_t params; 578 panel_t *panel; 579 gfx_rect_t rect; 580 errno_t rc; 581 582 rc = ui_create_disp(NULL, &ui); 583 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 584 585 ui_wnd_params_init(¶ms); 586 params.caption = "Test"; 587 588 rc = ui_window_create(ui, ¶ms, &window); 589 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 590 591 rc = panel_create(window, &panel); 592 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 593 594 rect.p0.x = 0; 595 rect.p0.y = 0; 596 rect.p1.x = 10; 597 rect.p1.y = 4; // XXX Assuming this makes page size 2 598 panel_set_rect(panel, &rect); 599 600 /* Add tree entries (more than page size, which is 2) */ 601 rc = panel_entry_append(panel, "a", 1); 602 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 603 604 rc = panel_entry_append(panel, "b", 2); 605 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 606 607 rc = panel_entry_append(panel, "c", 3); 608 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 609 610 /* Cursor and page start to the first entry */ 611 panel->cursor = panel_first(panel); 612 panel->cursor_idx = 0; 613 panel->page = panel->cursor; 614 panel->page_idx = 0; 615 616 /* Move cursor one entry down */ 617 panel_cursor_down(panel); 618 619 /* Cursor should now be at the second entry, page stays the same */ 620 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name); 621 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size); 622 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx); 623 PCUT_ASSERT_EQUALS(panel_first(panel), panel->page); 624 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx); 625 626 /* Move cursor one entry down. This should scroll down. */ 627 panel_cursor_down(panel); 628 629 /* Cursor should now be at the third and page at the second entry. */ 630 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name); 631 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size); 632 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx); 633 PCUT_ASSERT_STR_EQUALS("b", panel->page->name); 634 PCUT_ASSERT_INT_EQUALS(2, panel->page->size); 635 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx); 636 637 /* Moving further down should do nothing (we are at the bottom). */ 638 panel_cursor_down(panel); 639 640 /* Cursor should still be at the third and page at the second entry. */ 641 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name); 642 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size); 643 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx); 644 PCUT_ASSERT_STR_EQUALS("b", panel->page->name); 645 PCUT_ASSERT_INT_EQUALS(2, panel->page->size); 646 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx); 647 648 panel_destroy(panel); 649 ui_window_destroy(window); 650 ui_destroy(ui); 651 } 652 653 /** panel_cursor_top() moves cursor to the first entry */ 654 PCUT_TEST(cursor_top) 655 { 656 ui_t *ui; 657 ui_window_t *window; 658 ui_wnd_params_t params; 659 panel_t *panel; 660 gfx_rect_t rect; 661 errno_t rc; 662 663 rc = ui_create_disp(NULL, &ui); 664 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 665 666 ui_wnd_params_init(¶ms); 667 params.caption = "Test"; 668 669 rc = ui_window_create(ui, ¶ms, &window); 670 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 671 672 rc = panel_create(window, &panel); 673 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 674 675 rect.p0.x = 0; 676 rect.p0.y = 0; 677 rect.p1.x = 10; 678 rect.p1.y = 4; // XXX Assuming this makes page size 2 679 panel_set_rect(panel, &rect); 680 681 /* Add tree entries (more than page size, which is 2) */ 682 rc = panel_entry_append(panel, "a", 1); 683 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 684 685 rc = panel_entry_append(panel, "b", 2); 686 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 687 688 rc = panel_entry_append(panel, "c", 3); 689 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 690 691 /* Cursor to the last entry and page start to the next-to-last entry */ 692 panel->cursor = panel_last(panel); 693 panel->cursor_idx = 2; 694 panel->page = panel_prev(panel->cursor); 695 panel->page_idx = 1; 696 697 /* Move cursor to the top. This should scroll up. */ 698 panel_cursor_top(panel); 699 700 /* Cursor and page start should now both be at the first entry */ 701 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name); 702 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size); 703 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx); 704 PCUT_ASSERT_EQUALS(panel->cursor, panel->page); 705 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx); 706 707 panel_destroy(panel); 708 ui_window_destroy(window); 709 ui_destroy(ui); 710 } 711 712 /** panel_cursor_bottom() moves cursor to the last entry */ 713 PCUT_TEST(cursor_bottom) 714 { 715 ui_t *ui; 716 ui_window_t *window; 717 ui_wnd_params_t params; 718 panel_t *panel; 719 gfx_rect_t rect; 720 errno_t rc; 721 722 rc = ui_create_disp(NULL, &ui); 723 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 724 725 ui_wnd_params_init(¶ms); 726 params.caption = "Test"; 727 728 rc = ui_window_create(ui, ¶ms, &window); 729 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 730 731 rc = panel_create(window, &panel); 732 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 733 734 rect.p0.x = 0; 735 rect.p0.y = 0; 736 rect.p1.x = 10; 737 rect.p1.y = 4; // XXX Assuming this makes page size 2 738 panel_set_rect(panel, &rect); 739 740 /* Add tree entries (more than page size, which is 2) */ 741 rc = panel_entry_append(panel, "a", 1); 742 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 743 744 rc = panel_entry_append(panel, "b", 2); 745 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 746 747 rc = panel_entry_append(panel, "c", 3); 748 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 749 750 /* Cursor and page start to the first entry */ 751 panel->cursor = panel_first(panel); 752 panel->cursor_idx = 0; 753 panel->page = panel->cursor; 754 panel->page_idx = 0; 755 756 /* Move cursor to the bottom. This should scroll down. */ 757 panel_cursor_bottom(panel); 758 759 /* Cursor should now be at the third and page at the second entry. */ 760 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name); 761 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size); 762 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx); 763 PCUT_ASSERT_STR_EQUALS("b", panel->page->name); 764 PCUT_ASSERT_INT_EQUALS(2, panel->page->size); 765 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx); 766 767 panel_destroy(panel); 768 ui_window_destroy(window); 769 ui_destroy(ui); 770 } 771 349 772 PCUT_EXPORT(panel);
Note:
See TracChangeset
for help on using the changeset viewer.